28 #endif // HAVE_CONFIG_H
39 #include "../drivers.h"
40 #include "../chips/pn53x.h"
45 #define BUFFER_LENGTH 256
49 static const byte_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
55 get_end_points (
struct usb_device *dev, usb_spec_t * pus)
59 struct usb_interface_descriptor *puid = dev->config->interface->altsetting;
62 for (uiIndex = 0; uiIndex < puid->bNumEndpoints; uiIndex++) {
64 if (puid->endpoint[uiIndex].bmAttributes != USB_ENDPOINT_TYPE_BULK)
68 uiEndPoint = puid->endpoint[uiIndex].bEndpointAddress;
71 if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN) {
72 pus->uiEndPointIn = uiEndPoint;
73 pus->wMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
76 if ((uiEndPoint & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT) {
77 pus->uiEndPointOut = uiEndPoint;
78 pus->wMaxPacketSize = puid->endpoint[uiIndex].wMaxPacketSize;
84 pn53x_usb_list_devices (
nfc_device_desc_t pnddDevices[],
size_t szDevices,
size_t * pszDeviceFound,
85 usb_candidate_t candidates[],
int num_candidates,
char *target_name)
91 struct usb_device *dev;
93 uint32_t uiBusIndex = 0;
100 if ((ret = usb_find_busses () < 0))
103 if ((ret = usb_find_devices () < 0))
108 for (bus = usb_get_busses (); bus; bus = bus->next) {
109 for (dev = bus->devices; dev; dev = dev->next, uiBusIndex++) {
110 for (i = 0; i < num_candidates; ++i) {
112 if (candidates[i].idVendor == dev->descriptor.idVendor && candidates[i].idProduct == dev->descriptor.idProduct) {
115 if (dev->config == NULL || dev->config->interface == NULL || dev->config->interface->altsetting == NULL) {
119 if (dev->config->interface->altsetting->bNumEndpoints < 2) {
123 if (dev->descriptor.iManufacturer || dev->descriptor.iProduct) {
124 udev = usb_open (dev);
126 usb_get_string_simple (udev, dev->descriptor.iManufacturer,
string, sizeof (
string));
127 if (strlen (
string) > 0)
128 strcpy (
string + strlen (
string),
" / ");
129 usb_get_string_simple (udev, dev->descriptor.iProduct,
string + strlen (
string),
130 sizeof (
string) - strlen (
string));
134 if (strlen (
string) == 0)
135 strcpy (pnddDevices[*pszDeviceFound].acDevice, target_name);
137 strcpy (pnddDevices[*pszDeviceFound].acDevice,
string);
138 pnddDevices[*pszDeviceFound].
pcDriver = target_name;
139 pnddDevices[*pszDeviceFound].
uiBusIndex = uiBusIndex;
142 if ((*pszDeviceFound) == szDevices) {
155 pn53x_usb_connect (
const nfc_device_desc_t * pndd,
const char *target_name,
int target_chip)
161 struct usb_device *dev;
165 us.uiEndPointOut = 0;
168 DBG (
"Attempt to connect to %s device", target_name);
173 for (bus = usb_get_busses (); bus; bus = bus->next) {
174 for (dev = bus->devices; dev; dev = dev->next, uiBusIndex--) {
175 DBG (
"Checking device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
176 if (uiBusIndex == 0) {
178 us.pudh = usb_open (dev);
180 get_end_points (dev, &us);
181 if (usb_set_configuration (us.pudh, 1) < 0) {
182 ERR (
"Unable to set USB configuration, please check USB permissions for device %04x:%04x", dev->descriptor.idVendor, dev->descriptor.idProduct);
188 if (usb_claim_interface (us.pudh, 0) < 0) {
189 DBG (
"%s",
"Can't claim interface");
195 us.uc.idVendor = dev->descriptor.idVendor;
196 us.uc.idProduct = dev->descriptor.idProduct;
198 pus = malloc (
sizeof (usb_spec_t));
201 strcpy (pnd->
acName, target_name);
202 pnd->
nc = target_chip;
203 pnd->
nds = (nfc_device_spec_t) pus;
211 byte_t abtTx[] = { 0x00, 0x00, 0xff, 0x02, 0xfe, 0xd4, 0x02, 0x2a, 0x00 };
212 byte_t abtRx[BUFFER_LENGTH];
215 PRINT_HEX (
"TX", abtTx,
sizeof(abtTx));
217 ret = usb_bulk_write (pus->pudh, pus->uiEndPointOut, (
char *) abtTx,
sizeof(abtTx), USB_TIMEOUT);
219 DBG (
"usb_bulk_write failed with error %d", ret);
224 ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (
char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
226 DBG (
"usb_bulk_read failed with error %d", ret);
232 PRINT_HEX (
"RX", abtRx, ret);
235 if (!pn53x_check_ack_frame_callback (pnd, abtRx, ret)) {
236 DBG (
"usb_bulk_read failed getting ACK");
241 ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (
char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
243 DBG (
"usb_bulk_read failed with error %d", ret);
249 PRINT_HEX (
"RX", abtRx, ret);
258 DBG (
"%s",
"Device index not found!");
265 usb_spec_t *pus = (usb_spec_t *) pnd->
nds;
270 if ((ret = usb_release_interface (pus->pudh, 0)) < 0) {
271 ERR (
"usb_release_interface failed (%i)", ret);
274 if ((ret = usb_close (pus->pudh)) < 0) {
275 ERR (
"usb_close failed (%i)", ret);
287 pn53x_usb_transceive (
nfc_device_t * pnd,
const byte_t * pbtTx,
const size_t szTx, byte_t * pbtRx,
size_t * pszRx)
291 byte_t abtTx[BUFFER_LENGTH] = { 0x00, 0x00, 0xff };
292 byte_t abtRx[BUFFER_LENGTH];
293 usb_spec_t *pus = (usb_spec_t *) pnd->
nds;
295 uint8_t ack_frame[] = { 0x00, 0x00, 0xff, 0x00, 0xff, 0x00 };
300 abtTx[4] = 0x0100 - abtTx[3];
302 memmove (abtTx + 5, pbtTx, szTx);
306 for (uiPos = 0; uiPos < szTx; uiPos++) {
307 abtTx[szTx + 5] -= abtTx[uiPos + 5];
314 PRINT_HEX (
"TX", abtTx, szTx + 7);
317 ret = usb_bulk_write (pus->pudh, pus->uiEndPointOut, (
char *) abtTx, szTx + 7, USB_TIMEOUT);
319 if ((ret % pus->wMaxPacketSize) == 0) {
320 usb_bulk_write (pus->pudh, pus->uiEndPointOut,
"\0", 0, USB_TIMEOUT);
324 DBG (
"usb_bulk_write failed with error %d", ret);
329 ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (
char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
331 DBG (
"usb_bulk_read failed with error %d", ret);
338 PRINT_HEX (
"RX", abtRx, ret);
341 if (!pn53x_check_ack_frame_callback (pnd, abtRx, ret))
344 ret = usb_bulk_read (pus->pudh, pus->uiEndPointIn, (
char *) abtRx, BUFFER_LENGTH, USB_TIMEOUT);
346 DBG (
"usb_bulk_read failed with error %d", ret);
353 PRINT_HEX (
"RX", abtRx, ret);
357 PRINT_HEX (
"TX", ack_frame, 6);
359 usb_bulk_write (pus->pudh, pus->uiEndPointOut, (
char *) ack_frame, 6, USB_TIMEOUT);
361 if (!pn53x_check_error_frame_callback (pnd, abtRx, ret))
365 if (pbtRx == NULL || pszRx == NULL)
370 DBG (
"%s",
"No data");
375 *pszRx = ret - 7 - 2;
377 memcpy (pbtRx, abtRx + 7, *pszRx);
385 usb_spec_t *pus = (usb_spec_t *) pnd->
nds;
387 PRINT_HEX (
"TX", ack_frame,
sizeof (ack_frame));
389 usb_bulk_write (pus->pudh, pus->uiEndPointOut, (
char *) ack_frame, sizeof (ack_frame), USB_TIMEOUT);