Debian Codegraph @

xpp/xtalk/xusb_libusb.c

raw
1 /*
2 * Written by Oron Peled <[email protected]>
3 * Copyright (C) 2008, Xorcom
4 *
5 * All rights reserved.
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 *
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27 #include <errno.h>
28 #include <stdarg.h>
29 #include <syslog.h>
30 #include <arpa/inet.h>
31 #include <sys/time.h>
32 #include <sys/types.h>
33 #include <sys/ipc.h>
34 #include <sys/sem.h>
35 #include <usb.h>
36 #include <xtalk/debug.h>
37 #include <xtalk/xusb.h>
38 #include <autoconfig.h>
39 #include "xusb_common.h"
40
41 #define DBG_MASK 0x01
42 #define TIMEOUT 500
43 #define MAX_RETRIES 10
44
45 #define EP_IS_IN(ep) (((ep) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_IN)
46 #define EP_IS_OUT(ep) (((ep) & USB_ENDPOINT_DIR_MASK) == USB_ENDPOINT_OUT)
47
48 struct libusb_implementation {
49 struct usb_device *dev;
50 usb_dev_handle *handle;
51 struct usb_config_descriptor *config_desc;
52 };
53
54 static void xusb_init();
55
56 /*
57 * USB handling
58 */
59
60 static int get_usb_string(struct xusb_device *xusb_device, uint8_t item, char *buf)
61 {
62 char tmp[BUFSIZ];
63 int ret;
64
65 if (!xusb_device->impl->handle) {
66 ERR("%s: device closed\n", xusb_device->devpath_tail);
67 return -ENXIO;
68 }
69 if (!item)
70 return 0;
71 ret = usb_get_string_simple(xusb_device->impl->handle, item, tmp, BUFSIZ);
72 if (ret <= 0)
73 return ret;
74 return snprintf(buf, BUFSIZ, "%s", tmp);
75 }
76
77 static const struct usb_interface_descriptor *get_iface_descriptor(
78 const struct xusb_device *xusb_device, int i)
79 {
80 const struct usb_config_descriptor *config_desc;
81 const struct usb_interface *interface;
82 const struct usb_interface_descriptor *iface_desc;
83 int num_altsetting;
84
85 assert(xusb_device);
86 assert(xusb_device->impl);
87 config_desc = xusb_device->impl->config_desc;
88 assert(config_desc);
89 assert(config_desc->bNumInterfaces < XUSB_MAX_INTERFACES);
90 if (i >= XUSB_MAX_INTERFACES)
91 return NULL;
92 interface = &config_desc->interface[i];
93 assert(interface != NULL);
94 iface_desc = interface->altsetting;
95 num_altsetting = interface->num_altsetting;
96 assert(num_altsetting != 0);
97 assert(iface_desc != NULL);
98 return iface_desc;
99 }
100
101 #define GET_USB_STRING(xusb_device, from, item) \
102 get_usb_string((xusb_device), (from)->item, (xusb_device)->item)
103
104 static int xusb_fill_strings(struct xusb_device *xusb_device, int interface_num)
105 {
106 const struct usb_device_descriptor *dev_desc;
107 const struct usb_interface_descriptor *iface_desc;
108 struct xusb_iface *iface = xusb_device->interfaces[interface_num];
109 int ret;
110
111 dev_desc = &xusb_device->impl->dev->descriptor;
112 assert(dev_desc);
113 ret = GET_USB_STRING(xusb_device, dev_desc, iManufacturer);
114 if (ret < 0) {
115 XUSB_ERR(iface, "Failed reading iManufacturer string: %s\n",
116 usb_strerror());
117 return 0;
118 }
119 ret = GET_USB_STRING(xusb_device, dev_desc, iProduct);
120 if (ret < 0) {
121 XUSB_ERR(iface, "Failed reading iProduct string: %s\n",
122 usb_strerror());
123 return 0;
124 }
125 ret = GET_USB_STRING(xusb_device, dev_desc, iSerialNumber);
126 if (ret < 0) {
127 XUSB_ERR(iface, "Failed reading iSerialNumber string: %s\n",
128 usb_strerror());
129 return 0;
130 }
131 iface_desc = get_iface_descriptor(xusb_device, interface_num);
132 if (!iface_desc) {
133 XUSB_ERR(iface, "Could not get interface descriptor of device\n");
134 return 0;
135 }
136 ret = GET_USB_STRING(xusb_device, iface_desc, iInterface);
137 if (ret < 0) {
138 XUSB_ERR(iface, "Failed reading iInterface string: %s\n",
139 usb_strerror());
140 return 0;
141 }
142 return 1;
143 }
144
145 static int xusb_open(struct xusb_device *xusb_device)
146 {
147 int ret = 0;
148
149 assert(xusb_device);
150 DBG("%s\n", xusb_device->devpath_tail);
151 if (xusb_device->impl->handle) {
152 ERR("%s: already open\n", xusb_device->devpath_tail);
153 ret = -EBUSY;
154 goto out;
155 }
156 xusb_device->impl->handle = usb_open(xusb_device->impl->dev);
157 if (!xusb_device->impl->handle) {
158 ERR("%s: Failed to open usb device: %s\n",
159 xusb_device->devpath_tail,
160 usb_strerror());
161 xusb_device->impl->handle = NULL;
162 goto out;
163 }
164 return 1;
165 out:
166 return ret;
167 }
168
169 void xusb_release(struct xusb_iface *iface)
170 {
171 if (iface && iface->is_claimed) {
172 usb_dev_handle *handle;
173 int ret;
174
175 assert(iface->xusb_device);
176 handle = iface->xusb_device->impl->handle;
177 XUSB_DBG(iface, "Releasing interface\n");
178 if (!handle) {
179 XUSB_ERR(iface, "device closed\n");
180 iface->is_claimed = 0;
181 return;
182 }
183 ret = usb_release_interface(handle, iface->interface_num);
184 if (ret < 0)
185 XUSB_ERR(iface, "Releasing interface: %s\n",
186 usb_strerror());
187 iface->is_claimed = 0;
188 }
189 }
190
191 static int xusb_clear_halt(struct xusb_iface *xusb_iface)
192 {
193 struct xusb_device *xusb_device;
194 int ret = 0;
195 int ep;
196
197 xusb_device = xusb_iface->xusb_device;
198 /*
199 * WE DO NOT CALL HALT for problematic devices:
200 * - It cause problem with our usb-dongle (cypress CY7C63803, interrupt driven)
201 */
202 if (xusb_device->idVendor == 0xe4e4 && xusb_device->idProduct == 0x11a3) {
203 XUSB_DBG(xusb_iface, "Skipping clear_halt()\n");
204 goto out;
205 }
206 if (!xtalk_option_use_clear_halt()) {
207 XUSB_DBG(xusb_iface, "Don't use clear_halt()\n");
208 goto out;
209 }
210 ep = EP_OUT(xusb_iface);
211 ret = usb_clear_halt(xusb_device->impl->handle, ep);
212 if (ret < 0) {
213 XUSB_ERR(xusb_iface, "Clearing output endpoint 0x%02X: %s\n",
214 ep, usb_strerror());
215 goto out;
216 }
217 ep = EP_IN(xusb_iface);
218 ret = usb_clear_halt(xusb_device->impl->handle, ep);
219 if (ret < 0) {
220 XUSB_ERR(xusb_iface, "Clearing input endpoint 0x%02X: %s\n",
221 ep, usb_strerror());
222 goto out;
223 }
224 out:
225 return ret;
226 }
227
228 int xusb_claim(struct xusb_device *xusb_device, unsigned int interface_num,
229 struct xusb_iface **xusb_iface)
230 {
231 struct xusb_iface *iface = NULL;
232 enum xusb_transfer_type iface_tt = XUSB_TT_ILLEGAL;
233 int ret = 0;
234
235 *xusb_iface = NULL;
236 assert(xusb_device);
237 if (!xusb_device->impl->handle) {
238 XUSB_ERR(iface, "device closed\n");
239 ret = -ENXIO;
240 goto failed;
241 }
242 if (interface_num >= XUSB_MAX_INTERFACES) {
243 ERR("%s: interface number %d is too big\n",
244 xusb_device->devpath_tail, interface_num);
245 ret = -EINVAL;
246 goto failed;
247 }
248 iface = xusb_device->interfaces[interface_num];
249 if (!iface) {
250 ERR("%s: No interface number %d\n",
251 xusb_device->devpath_tail, interface_num);
252 ret = -EINVAL;
253 goto failed;
254 }
255 if (iface->is_claimed) {
256 XUSB_ERR(iface, "Already claimed\n");
257 ret = -EBUSY;
258 goto failed;
259 }
260 ret = usb_claim_interface(xusb_device->impl->handle, iface->interface_num);
261 if (ret < 0) {
262 XUSB_ERR(iface, "usb_claim_interface: %s\n", usb_strerror());
263 goto failed;
264 }
265 iface->is_claimed = 1;
266 iface_tt = xusb_transfer_type(iface);
267 if (iface_tt == XUSB_TT_ILLEGAL) {
268 ret = -ENOTSUP;
269 goto failed;
270 }
271 iface->transfer_type = iface_tt;
272 ret = xusb_clear_halt(iface);
273 if (ret < 0)
274 goto failed;
275 ret = xusb_flushread(iface);
276 if (ret < 0) {
277 XUSB_ERR(iface, "xusb_flushread failed: %s\n", usb_strerror());
278 goto failed;
279 }
280 xusb_fill_strings(xusb_device, interface_num);
281 XUSB_DBG(iface, "ID=%04X:%04X Manufacturer=[%s] Product=[%s] "
282 "SerialNumber=[%s] Interface=[%s] TT=%s\n",
283 xusb_device->idVendor,
284 xusb_device->idProduct,
285 xusb_device->iManufacturer,
286 xusb_device->iProduct,
287 xusb_device->iSerialNumber,
288 iface->iInterface,
289 xusb_tt_name(iface->transfer_type));
290 *xusb_iface = iface;
291 return 0;
292 failed:
293 xusb_release(iface);
294 return ret;
295 }
296
297 void xusb_destroy(struct xusb_device *xusb_device)
298 {
299 if (xusb_device) {
300 struct xusb_iface **piface;
301 struct libusb_implementation *impl;
302
303 for (piface = xusb_device->interfaces; *piface; piface++) {
304 xusb_destroy_interface(*piface);
305 *piface = NULL;
306 }
307 impl = xusb_device->impl;
308 if (impl) {
309 if (impl->handle) {
310 xusb_close(xusb_device);
311 impl->handle = NULL;
312 }
313 }
314 DBG("%s: MEM: FREE device\n", xusb_device->devpath_tail);
315 memset(xusb_device, 0, sizeof(*xusb_device));
316 free(xusb_device);
317 xusb_device = NULL;
318 }
319 }
320
321 static int init_interfaces(struct xusb_device *xusb_device)
322 {
323 const struct usb_config_descriptor *config_desc;
324 const struct usb_interface_descriptor *iface_desc;
325 struct xusb_iface *iface;
326 int max_packet_size = 0;
327 int packet_size;
328 int if_idx;
329
330 assert(xusb_device);
331 assert(xusb_device->impl);
332 config_desc = xusb_device->impl->config_desc;
333 assert(config_desc);
334 assert(config_desc->bNumInterfaces < XUSB_MAX_INTERFACES);
335 for (if_idx = 0; if_idx < config_desc->bNumInterfaces; if_idx++) {
336 int ep_idx;
337
338 iface_desc = get_iface_descriptor(xusb_device, if_idx);
339 if (iface_desc->bInterfaceNumber != if_idx) {
340 ERR("%s: interface %d is number %d\n",
341 xusb_device->devpath_tail,
342 if_idx, iface_desc->bInterfaceNumber);
343 return -EINVAL;
344 }
345 if (iface_desc->bNumEndpoints != 2) {
346 ERR("%s: interface %d has %d endpoints\n",
347 xusb_device->devpath_tail,
348 if_idx, iface_desc->bNumEndpoints);
349 return -EINVAL;
350 }
351 iface = calloc(sizeof(*iface), 1);
352 if (!iface) {
353 ERR("%s: interface %d -- out of memory\n",
354 xusb_device->devpath_tail, if_idx);
355 return -ENOMEM;
356 }
357 DBG("MEM: ALLOC interface: %p\n", iface);
358 xusb_device->interfaces[if_idx] = iface;
359 iface->xusb_device = xusb_device;
360 iface->interface_num = iface_desc->bInterfaceNumber;
361
362 /* Search Endpoints */
363 iface->ep_in = 0;
364 iface->ep_out = 0;
365 for (ep_idx = 0; ep_idx < iface_desc->bNumEndpoints; ep_idx++) {
366 int ep_num;
367
368 ep_num = iface_desc->endpoint[ep_idx].bEndpointAddress;
369 packet_size = iface_desc->endpoint[ep_idx].wMaxPacketSize;
370 if (!max_packet_size || packet_size < max_packet_size)
371 max_packet_size = packet_size;
372 if (EP_IS_OUT(ep_num))
373 iface->ep_out = ep_num;
374 if (EP_IS_IN(ep_num))
375 iface->ep_in = ep_num;
376 }
377 /* Validation */
378 if (!iface->ep_out) {
379 ERR("%s[%d]: Missing output endpoint\n",
380 xusb_device->devpath_tail, if_idx);
381 return -EINVAL;
382 }
383 if (!iface->ep_in) {
384 ERR("%s[%d]: Missing input endpoint\n",
385 xusb_device->devpath_tail, if_idx);
386 return -EINVAL;
387 }
388
389 iface->is_claimed = 0;
390 XUSB_DBG(iface, "ep_out=0x%X ep_in=0x%X packet_size=%d\n",
391 iface->ep_out, iface->ep_in, max_packet_size);
392 }
393 if (xusb_device->packet_size < max_packet_size)
394 xusb_device->packet_size = max_packet_size;
395 xusb_device->is_usb2 = (xusb_device->packet_size == 512);
396 return 0;
397 }
398
399 static struct xusb_device *xusb_new(struct usb_device *dev,
400 const struct xusb_spec *spec,
401 xusb_filter_t filterfunc,
402 void *data)
403 {
404 struct usb_device_descriptor *dev_desc;
405 int ret;
406 struct xusb_device *xusb_device = NULL;
407
408 xusb_device = calloc(sizeof(*xusb_device) + sizeof(struct libusb_implementation), 1);
409 if (!xusb_device) {
410 ERR("Out of memory");
411 goto fail;
412 }
413 DBG("MEM: ALLOC device: %p\n", xusb_device);
414 /* Fill xusb_device */
415 xusb_device->impl = (void *)xusb_device + sizeof(*xusb_device);
416 xusb_device->impl->dev = dev;
417 xusb_device->spec = spec;
418 /*
419 * Get information from the usb_device
420 */
421 dev_desc = &dev->descriptor;
422 if (!dev_desc) {
423 ERR("usb device without a device descriptor\n");
424 goto fail;
425 }
426 xusb_device->idVendor = dev_desc->idVendor;
427 xusb_device->idProduct = dev_desc->idProduct;
428 sscanf(dev->bus->dirname, "%d", &xusb_device->bus_num);
429 sscanf(dev->filename, "%d", &xusb_device->device_num);
430 snprintf(xusb_device->devpath_tail, PATH_MAX, "%03d/%03d",
431 xusb_device->bus_num, xusb_device->device_num);
432 if (!match_device(xusb_device, spec)) {
433 DBG("[%04X:%04X] did not match\n",
434 xusb_device->idVendor, xusb_device->idProduct);
435 goto fail;
436 }
437 xusb_device->impl->config_desc = dev->config;
438 if (!xusb_device->impl->config_desc) {
439 ERR("usb device without a configuration descriptor\n");
440 goto fail;
441 }
442 ret = init_interfaces(xusb_device);
443 if (ret < 0) {
444 ERR("%s: init_interfaces() failed (ret = %d)\n",
445 xusb_device->devpath_tail, ret);
446 goto fail;
447 }
448 if (!xusb_open(xusb_device)) {
449 ERR("%s: Failed opening device: %04X:%04X\n",
450 xusb_device->devpath_tail,
451 xusb_device->idVendor,
452 xusb_device->idProduct);
453 goto fail;
454 }
455 DBG("%s: %04X:%04X\n",
456 xusb_device->devpath_tail,
457 xusb_device->idVendor,
458 xusb_device->idProduct);
459 return xusb_device;
460 fail:
461 xusb_destroy(xusb_device);
462 return NULL;
463 }
464
465 struct xusb_iface *xusb_find_iface(const char *devpath,
466 int iface_num,
467 int ep_out,
468 int ep_in,
469 struct xusb_spec *dummy_spec)
470 {
471 return NULL;
472 }
473
474 struct xusb_device *xusb_find_bypath(const char *path)
475 {
476 struct usb_bus *bus;
477 struct usb_device *dev;
478 struct xusb_spec *spec;
479 char devpath_tail[2*(PATH_MAX+2)] = { '\0' };
480
481 DBG("path='%s'\n", path);
482 spec = calloc(sizeof(*spec), 1);
483 if (!spec) {
484 ERR("Failed allocating spec\n");
485 goto failed;
486 }
487 xusb_init();
488 for (bus = usb_get_busses(); bus; bus = bus->next) {
489 for (dev = bus->devices; dev; dev = dev->next) {
490 struct usb_device_descriptor *dev_desc;
491 struct xusb_device *xusb_device = NULL;
492
493 dev_desc = &dev->descriptor;
494 assert(dev_desc);
495 DBG("usb:%s/%s: ID=%04X:%04X\n",
496 dev->bus->dirname,
497 dev->filename,
498 dev_desc->idVendor,
499 dev_desc->idProduct);
500 snprintf(devpath_tail, sizeof(devpath_tail), "%.3s/%.3s",
501 dev->bus->dirname, dev->filename);
502 if (!match_devpath(path, devpath_tail))
503 continue;
504 DBG("Found: usb:%s/%s: ID=%04X:%04X\n",
505 dev->bus->dirname,
506 dev->filename,
507 dev_desc->idVendor,
508 dev_desc->idProduct);
509 xusb_init_spec(spec, "<BYPATH>",
510 dev_desc->idVendor, dev_desc->idProduct);
511 xusb_device = xusb_new(dev, spec, NULL, NULL);
512 if (!xusb_device) {
513 ERR("Failed creating xusb for %s\n",
514 devpath_tail);
515 xusb_init_spec(spec, "<EMPTY>", 0, 0);
516 continue;
517 }
518 return xusb_device;
519 }
520 }
521 failed:
522 if (spec)
523 free(spec);
524 return NULL;
525 }
526
527 struct xlist_node *xusb_find_byproduct(const struct xusb_spec *specs,
528 int numspecs, xusb_filter_t filterfunc, void *data)
529 {
530 struct xlist_node *xlist;
531 struct usb_bus *bus;
532 struct usb_device *dev;
533
534 DBG("specs(%d)\n", numspecs);
535 xlist = xlist_new(NULL);
536 if (!xlist) {
537 ERR("Failed allocation new xlist");
538 goto failed;
539 }
540 xusb_init();
541 for (bus = usb_get_busses(); bus; bus = bus->next) {
542 for (dev = bus->devices; dev; dev = dev->next) {
543 struct usb_device_descriptor *dev_desc;
544 struct xlist_node *item;
545 int i;
546
547 dev_desc = &dev->descriptor;
548 assert(dev_desc);
549 DBG("usb:%s/%s: ID=%04X:%04X\n",
550 dev->bus->dirname,
551 dev->filename,
552 dev_desc->idVendor,
553 dev_desc->idProduct);
554 for (i = 0; i < numspecs; i++) {
555 struct xusb_device *xusb_device;
556 const struct xusb_spec *sp = &specs[i];
557
558 xusb_device = xusb_new(dev, sp, filterfunc, data);
559 if (!xusb_device)
560 continue;
561 if (filterfunc && !filterfunc(xusb_device, data)) {
562 DBG("%s: %04X:%04X filtered out\n",
563 xusb_device->devpath_tail,
564 dev_desc->idVendor,
565 dev_desc->idProduct);
566 xusb_destroy(xusb_device);
567 continue;
568 }
569 item = xlist_new(xusb_device);
570 xlist_append_item(xlist, item);
571 break;
572 }
573 }
574 }
575 xusb_list_dump(xlist);
576 return xlist;
577 failed:
578 if (xlist)
579 xlist_destroy(xlist, NULL);
580 return NULL;
581 }
582
583 void xusb_showinfo(const struct xusb_device *xusb_device)
584 {
585 struct usb_device_descriptor *dev_desc;
586 struct usb_device *dev;
587 const struct xusb_iface **piface;
588
589 assert(xusb_device);
590 dev = xusb_device->impl->dev;
591 assert(dev);
592 dev_desc = &dev->descriptor;
593 assert(dev_desc);
594 if (verbose <= LOG_INFO) {
595 INFO("%s: [%04X:%04X] [%s / %s / %s]\n",
596 xusb_device->devpath_tail,
597 dev_desc->idVendor,
598 dev_desc->idProduct,
599 xusb_device->iManufacturer,
600 xusb_device->iProduct,
601 xusb_device->iSerialNumber);
602 } else {
603 printf("USB Bus/Device: [%s/%s] (%s)\n",
604 dev->bus->dirname,
605 dev->filename,
606 (xusb_device->impl->handle) ? "open" : "closed");
607 printf("USB Spec name: [%s]\n", xusb_device->spec->name);
608 printf("USB iManufacturer: [%s]\n", xusb_device->iManufacturer);
609 printf("USB iProduct: [%s]\n", xusb_device->iProduct);
610 printf("USB iSerialNumber: [%s]\n", xusb_device->iSerialNumber);
611 piface = (const struct xusb_iface **)xusb_device->interfaces;
612 for (; *piface; piface++) {
613 printf("USB Interface[%d]: ep_out=0x%02X ep_in=0x%02X claimed=%d [%s]\n",
614 (*piface)->interface_num,
615 (*piface)->ep_out,
616 (*piface)->ep_in,
617 (*piface)->is_claimed,
618 (*piface)->iInterface);
619 }
620 }
621 }
622
623 int xusb_close(struct xusb_device *xusb_device)
624 {
625 if (xusb_device && xusb_device->impl && xusb_device->impl->handle) {
626 assert(xusb_device->spec);
627 assert(xusb_device->spec->name);
628 DBG("%s: Closing device \"%s\"\n",
629 xusb_device->devpath_tail,
630 xusb_device->spec->name);
631 usb_close(xusb_device->impl->handle);
632 xusb_device->impl->handle = NULL;
633 }
634 return 0;
635 }
636
637 enum xusb_transfer_type xusb_transfer_type(const struct xusb_iface *iface)
638 {
639 const struct xusb_device *xusb_device;
640 const struct usb_interface_descriptor *iface_desc;
641 const struct usb_endpoint_descriptor *ep;
642 enum xusb_transfer_type ret = XUSB_TT_ILLEGAL;
643
644 assert(iface);
645 xusb_device = iface->xusb_device;
646 assert(xusb_device);
647 iface_desc = get_iface_descriptor(xusb_device, iface->interface_num);
648 assert(iface_desc);
649 ep = iface_desc->endpoint;
650 assert(ep);
651 switch (ep->bmAttributes) {
652 case USB_ENDPOINT_TYPE_CONTROL:
653 case USB_ENDPOINT_TYPE_ISOCHRONOUS:
654 break;
655 case USB_ENDPOINT_TYPE_BULK:
656 ret = XUSB_TT_BULK;
657 break;
658 case USB_ENDPOINT_TYPE_INTERRUPT:
659 ret = XUSB_TT_INTERRUPT;
660 break;
661 }
662 return ret;
663 }
664
665 int xusb_send(struct xusb_iface *iface, const char *buf, int len, int timeout)
666 {
667 struct xusb_device *xusb_device = iface->xusb_device;
668 int ep_out = EP_OUT(iface);
669 int retries = 0;
670 int ret;
671
672 dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, len);
673 if (!xusb_device->impl->handle) {
674 XUSB_ERR(iface, "device closed\n");
675 return -ENXIO;
676 }
677 if (!EP_IS_OUT(ep_out)) {
678 XUSB_ERR(iface, "%s called with an input endpoint 0x%x\n",
679 __func__, ep_out);
680 return -EINVAL;
681 }
682 retry_write:
683 switch (iface->transfer_type) {
684 case XUSB_TT_BULK:
685 ret = usb_bulk_write(xusb_device->impl->handle, ep_out, (char *)buf, len, timeout);
686 break;
687 case XUSB_TT_INTERRUPT:
688 ret = usb_interrupt_write(xusb_device->impl->handle, ep_out, (char *)buf, len, timeout);
689 break;
690 default:
691 ret = -EINVAL;
692 break;
693 }
694 if (ret < 0) {
695 /*
696 * If the device was gone, it may be the
697 * result of renumeration. Ignore it.
698 */
699 if (ret != -ENODEV) {
700 XUSB_ERR(iface, "write to endpoint 0x%x failed: (%d) %s\n",
701 ep_out, ret, usb_strerror());
702 dump_packet(LOG_ERR, DBG_MASK, "xusb_send[ERR]",
703 buf, len);
704 /*exit(2);*/
705 } else {
706 XUSB_DBG(iface, "write to endpoint 0x%x got ENODEV\n",
707 ep_out);
708 xusb_close(xusb_device);
709 }
710 return ret;
711 }
712 if (!ret) {
713
714 XUSB_ERR(iface, "write to endpoint 0x%x short write[%d]: (%d)\n",
715 ep_out, retries, ret);
716 if (retries++ > MAX_RETRIES)
717 return -EFAULT;
718 usleep(100);
719 goto retry_write;
720 }
721 if (ret != len) {
722 XUSB_ERR(iface, "write to endpoint 0x%x short write: (%d) %s\n",
723 ep_out, ret, usb_strerror());
724 dump_packet(LOG_ERR, DBG_MASK, "xusb_send[ERR]", buf, len);
725 return -EFAULT;
726 }
727 return ret;
728 }
729
730 int xusb_recv(struct xusb_iface *iface, char *buf, size_t len, int timeout)
731 {
732 struct xusb_device *xusb_device = iface->xusb_device;
733 int ep_in = EP_IN(iface);
734 int retries = 0;
735 int ret;
736
737 if (!xusb_device->impl->handle) {
738 XUSB_ERR(iface, "device closed\n");
739 return -ENXIO;
740 }
741 if (!EP_IS_IN(ep_in)) {
742 XUSB_ERR(iface, "called with an output endpoint 0x%x\n", ep_in);
743 return -EINVAL;
744 }
745 retry_read:
746 switch (iface->transfer_type) {
747 case XUSB_TT_BULK:
748 ret = usb_bulk_read(xusb_device->impl->handle, ep_in, buf, len, timeout);
749 break;
750 case XUSB_TT_INTERRUPT:
751 ret = usb_interrupt_read(xusb_device->impl->handle, ep_in, buf, len, timeout);
752 break;
753 default:
754 ret = -EAFNOSUPPORT;
755 break;
756 }
757 if (ret < 0) {
758 XUSB_DBG(iface, "read from endpoint 0x%x failed: (%d) %s\n",
759 ep_in, ret, usb_strerror());
760 memset(buf, 0, len);
761 return ret;
762 }
763 if (!ret) {
764 XUSB_ERR(iface, "read to endpoint 0x%x short read[%d]: (%d)\n",
765 ep_in, retries, ret);
766 if (retries++ > MAX_RETRIES)
767 return -EFAULT;
768 usleep(100);
769 goto retry_read;
770 }
771 dump_packet(LOG_DEBUG, DBG_MASK, __func__, buf, ret);
772 return ret;
773 }
774
775 /*
776 * Serialize calls to usb_find_busses()/usb_find_devices()
777 */
778
779 static const key_t SEM_KEY = 0x1a2b3c4d;
780 static int semid = -1; /* Failure */
781
782 static void xusb_lock_usb()
783 {
784 struct sembuf sembuf;
785
786 while (semid < 0) {
787 /* Maybe it was already created? */
788 semid = semget(SEM_KEY, 1, 0);
789 if (semid < 0) {
790 /* No, let's create ourselves */
791 semid = semget(SEM_KEY, 1, IPC_CREAT | IPC_EXCL | 0644);
792 if (semid < 0) {
793 /* Someone else won the race to create it */
794 if (errno != ENOENT)
795 ERR("%s: semget() failed: %s\n",
796 __func__, strerror(errno));
797 /* Retry */
798 continue;
799 }
800 /* Initialize */
801 if (semctl(semid, 0, SETVAL, 1) < 0)
802 ERR("%s: SETVAL() failed: %s\n",
803 __func__, strerror(errno));
804 }
805 }
806 DBG("%d: LOCKING\n", getpid());
807 sembuf.sem_num = 0;
808 sembuf.sem_op = -1;
809 sembuf.sem_flg = SEM_UNDO;
810 if (semop(semid, &sembuf, 1) < 0)
811 ERR("%s: semop() failed: %s\n", __func__, strerror(errno));
812 DBG("%d: LOCKED\n", getpid());
813 }
814
815 static void xusb_unlock_usb()
816 {
817 struct sembuf sembuf;
818
819 DBG("%d: UNLOCKING\n", getpid());
820 sembuf.sem_num = 0;
821 sembuf.sem_op = 1;
822 sembuf.sem_flg = SEM_UNDO;
823 if (semop(semid, &sembuf, 1) < 0)
824 ERR("%s: semop() failed: %s\n", __func__, strerror(errno));
825 DBG("%d: UNLOCKED\n", getpid());
826 }
827
828 static int initizalized;
829
830 void __attribute__((constructor)) xusb_init(void)
831 {
832 xtalk_parse_options();
833 if (!getenv("XUSB_NOLOCK"))
834 xusb_lock_usb();
835 if (!initizalized) {
836 usb_init();
837 initizalized = 1;
838 }
839 usb_find_busses();
840 usb_find_devices();
841 if (!getenv("XUSB_NOLOCK"))
842 xusb_unlock_usb();
843 }