usb_linux.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/types.h>
  22. #include <sys/time.h>
  23. #include <dirent.h>
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <ctype.h>
  27. #include <linux/usbdevice_fs.h>
  28. #include <linux/version.h>
  29. #if LINUX_VERSION_CODE > KERNEL_VERSION(2, 6, 20)
  30. #include <linux/usb/ch9.h>
  31. #else
  32. #include <linux/usb_ch9.h>
  33. #endif
  34. #include <asm/byteorder.h>
  35. #include "sysdeps.h"
  36. #define TRACE_TAG TRACE_USB
  37. #include "adb.h"
  38. /* usb scan debugging is waaaay too verbose */
  39. #define DBGX(x...)
  40. ADB_MUTEX_DEFINE( usb_lock );
  41. struct usb_handle
  42. {
  43. usb_handle *prev;
  44. usb_handle *next;
  45. char fname[64];
  46. int desc;
  47. unsigned char ep_in;
  48. unsigned char ep_out;
  49. unsigned zero_mask;
  50. unsigned writeable;
  51. struct usbdevfs_urb urb_in;
  52. struct usbdevfs_urb urb_out;
  53. int urb_in_busy;
  54. int urb_out_busy;
  55. int dead;
  56. adb_cond_t notify;
  57. adb_mutex_t lock;
  58. // for garbage collecting disconnected devices
  59. int mark;
  60. // ID of thread currently in REAPURB
  61. pthread_t reaper_thread;
  62. };
  63. static usb_handle handle_list = {
  64. .prev = &handle_list,
  65. .next = &handle_list,
  66. };
  67. static int known_device(const char *dev_name)
  68. {
  69. usb_handle *usb;
  70. adb_mutex_lock(&usb_lock);
  71. for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
  72. if(!strcmp(usb->fname, dev_name)) {
  73. // set mark flag to indicate this device is still alive
  74. usb->mark = 1;
  75. adb_mutex_unlock(&usb_lock);
  76. return 1;
  77. }
  78. }
  79. adb_mutex_unlock(&usb_lock);
  80. return 0;
  81. }
  82. static void kick_disconnected_devices()
  83. {
  84. usb_handle *usb;
  85. adb_mutex_lock(&usb_lock);
  86. // kick any devices in the device list that were not found in the device scan
  87. for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
  88. if (usb->mark == 0) {
  89. usb_kick(usb);
  90. } else {
  91. usb->mark = 0;
  92. }
  93. }
  94. adb_mutex_unlock(&usb_lock);
  95. }
  96. static void register_device(const char *dev_name, const char *devpath,
  97. unsigned char ep_in, unsigned char ep_out,
  98. int ifc, int serial_index, unsigned zero_mask);
  99. static inline int badname(const char *name)
  100. {
  101. while(*name) {
  102. if(!isdigit(*name++)) return 1;
  103. }
  104. return 0;
  105. }
  106. static void find_usb_device(const char *base,
  107. void (*register_device_callback)
  108. (const char *, const char *, unsigned char, unsigned char, int, int, unsigned))
  109. {
  110. char busname[32], devname[32];
  111. unsigned char local_ep_in, local_ep_out;
  112. DIR *busdir , *devdir ;
  113. struct dirent *de;
  114. int fd ;
  115. busdir = opendir(base);
  116. if(busdir == 0) return;
  117. while((de = readdir(busdir)) != 0) {
  118. if(badname(de->d_name)) continue;
  119. snprintf(busname, sizeof busname, "%s/%s", base, de->d_name);
  120. devdir = opendir(busname);
  121. if(devdir == 0) continue;
  122. // DBGX("[ scanning %s ]\n", busname);
  123. while((de = readdir(devdir))) {
  124. unsigned char devdesc[4096];
  125. unsigned char* bufptr = devdesc;
  126. unsigned char* bufend;
  127. struct usb_device_descriptor* device;
  128. struct usb_config_descriptor* config;
  129. struct usb_interface_descriptor* interface;
  130. struct usb_endpoint_descriptor *ep1, *ep2;
  131. unsigned zero_mask = 0;
  132. unsigned vid, pid;
  133. size_t desclength;
  134. if(badname(de->d_name)) continue;
  135. snprintf(devname, sizeof devname, "%s/%s", busname, de->d_name);
  136. if(known_device(devname)) {
  137. DBGX("skipping %s\n", devname);
  138. continue;
  139. }
  140. // DBGX("[ scanning %s ]\n", devname);
  141. if((fd = unix_open(devname, O_RDONLY)) < 0) {
  142. continue;
  143. }
  144. desclength = adb_read(fd, devdesc, sizeof(devdesc));
  145. bufend = bufptr + desclength;
  146. // should have device and configuration descriptors, and atleast two endpoints
  147. if (desclength < USB_DT_DEVICE_SIZE + USB_DT_CONFIG_SIZE) {
  148. D("desclength %d is too small\n", desclength);
  149. adb_close(fd);
  150. continue;
  151. }
  152. device = (struct usb_device_descriptor*)bufptr;
  153. bufptr += USB_DT_DEVICE_SIZE;
  154. if((device->bLength != USB_DT_DEVICE_SIZE) || (device->bDescriptorType != USB_DT_DEVICE)) {
  155. adb_close(fd);
  156. continue;
  157. }
  158. vid = device->idVendor;
  159. pid = device->idProduct;
  160. DBGX("[ %s is V:%04x P:%04x ]\n", devname, vid, pid);
  161. // should have config descriptor next
  162. config = (struct usb_config_descriptor *)bufptr;
  163. bufptr += USB_DT_CONFIG_SIZE;
  164. if (config->bLength != USB_DT_CONFIG_SIZE || config->bDescriptorType != USB_DT_CONFIG) {
  165. D("usb_config_descriptor not found\n");
  166. adb_close(fd);
  167. continue;
  168. }
  169. // loop through all the descriptors and look for the ADB interface
  170. while (bufptr < bufend) {
  171. unsigned char length = bufptr[0];
  172. unsigned char type = bufptr[1];
  173. if (type == USB_DT_INTERFACE) {
  174. interface = (struct usb_interface_descriptor *)bufptr;
  175. bufptr += length;
  176. if (length != USB_DT_INTERFACE_SIZE) {
  177. D("interface descriptor has wrong size\n");
  178. break;
  179. }
  180. DBGX("bInterfaceClass: %d, bInterfaceSubClass: %d,"
  181. "bInterfaceProtocol: %d, bNumEndpoints: %d\n",
  182. interface->bInterfaceClass, interface->bInterfaceSubClass,
  183. interface->bInterfaceProtocol, interface->bNumEndpoints);
  184. if (interface->bNumEndpoints == 2 &&
  185. is_adb_interface(vid, pid, interface->bInterfaceClass,
  186. interface->bInterfaceSubClass, interface->bInterfaceProtocol)) {
  187. struct stat st;
  188. char pathbuf[128];
  189. char link[256];
  190. char *devpath = NULL;
  191. DBGX("looking for bulk endpoints\n");
  192. // looks like ADB...
  193. ep1 = (struct usb_endpoint_descriptor *)bufptr;
  194. bufptr += USB_DT_ENDPOINT_SIZE;
  195. ep2 = (struct usb_endpoint_descriptor *)bufptr;
  196. bufptr += USB_DT_ENDPOINT_SIZE;
  197. if (bufptr > devdesc + desclength ||
  198. ep1->bLength != USB_DT_ENDPOINT_SIZE ||
  199. ep1->bDescriptorType != USB_DT_ENDPOINT ||
  200. ep2->bLength != USB_DT_ENDPOINT_SIZE ||
  201. ep2->bDescriptorType != USB_DT_ENDPOINT) {
  202. D("endpoints not found\n");
  203. break;
  204. }
  205. // both endpoints should be bulk
  206. if (ep1->bmAttributes != USB_ENDPOINT_XFER_BULK ||
  207. ep2->bmAttributes != USB_ENDPOINT_XFER_BULK) {
  208. D("bulk endpoints not found\n");
  209. continue;
  210. }
  211. /* aproto 01 needs 0 termination */
  212. if(interface->bInterfaceProtocol == 0x01) {
  213. zero_mask = ep1->wMaxPacketSize - 1;
  214. }
  215. // we have a match. now we just need to figure out which is in and which is out.
  216. if (ep1->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
  217. local_ep_in = ep1->bEndpointAddress;
  218. local_ep_out = ep2->bEndpointAddress;
  219. } else {
  220. local_ep_in = ep2->bEndpointAddress;
  221. local_ep_out = ep1->bEndpointAddress;
  222. }
  223. // Determine the device path
  224. if (!fstat(fd, &st) && S_ISCHR(st.st_mode)) {
  225. char *slash;
  226. ssize_t link_len;
  227. snprintf(pathbuf, sizeof(pathbuf), "/sys/dev/char/%d:%d",
  228. major(st.st_rdev), minor(st.st_rdev));
  229. link_len = readlink(pathbuf, link, sizeof(link) - 1);
  230. if (link_len > 0) {
  231. link[link_len] = '\0';
  232. slash = strrchr(link, '/');
  233. if (slash) {
  234. snprintf(pathbuf, sizeof(pathbuf),
  235. "usb:%s", slash + 1);
  236. devpath = pathbuf;
  237. }
  238. }
  239. }
  240. register_device_callback(devname, devpath,
  241. local_ep_in, local_ep_out,
  242. interface->bInterfaceNumber, device->iSerialNumber, zero_mask);
  243. break;
  244. }
  245. } else {
  246. bufptr += length;
  247. }
  248. } // end of while
  249. adb_close(fd);
  250. } // end of devdir while
  251. closedir(devdir);
  252. } //end of busdir while
  253. closedir(busdir);
  254. }
  255. void usb_cleanup()
  256. {
  257. }
  258. static int usb_bulk_write(usb_handle *h, const void *data, int len)
  259. {
  260. struct usbdevfs_urb *urb = &h->urb_out;
  261. int res;
  262. struct timeval tv;
  263. struct timespec ts;
  264. memset(urb, 0, sizeof(*urb));
  265. urb->type = USBDEVFS_URB_TYPE_BULK;
  266. urb->endpoint = h->ep_out;
  267. urb->status = -1;
  268. urb->buffer = (void*) data;
  269. urb->buffer_length = len;
  270. D("++ write ++\n");
  271. adb_mutex_lock(&h->lock);
  272. if(h->dead) {
  273. res = -1;
  274. goto fail;
  275. }
  276. do {
  277. res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
  278. } while((res < 0) && (errno == EINTR));
  279. if(res < 0) {
  280. goto fail;
  281. }
  282. res = -1;
  283. h->urb_out_busy = 1;
  284. for(;;) {
  285. /* time out after five seconds */
  286. gettimeofday(&tv, NULL);
  287. ts.tv_sec = tv.tv_sec + 5;
  288. ts.tv_nsec = tv.tv_usec * 1000L;
  289. res = pthread_cond_timedwait(&h->notify, &h->lock, &ts);
  290. if(res < 0 || h->dead) {
  291. break;
  292. }
  293. if(h->urb_out_busy == 0) {
  294. if(urb->status == 0) {
  295. res = urb->actual_length;
  296. }
  297. break;
  298. }
  299. }
  300. fail:
  301. adb_mutex_unlock(&h->lock);
  302. D("-- write --\n");
  303. return res;
  304. }
  305. static int usb_bulk_read(usb_handle *h, void *data, int len)
  306. {
  307. struct usbdevfs_urb *urb = &h->urb_in;
  308. struct usbdevfs_urb *out = NULL;
  309. int res;
  310. memset(urb, 0, sizeof(*urb));
  311. urb->type = USBDEVFS_URB_TYPE_BULK;
  312. urb->endpoint = h->ep_in;
  313. urb->status = -1;
  314. urb->buffer = data;
  315. urb->buffer_length = len;
  316. adb_mutex_lock(&h->lock);
  317. if(h->dead) {
  318. res = -1;
  319. goto fail;
  320. }
  321. do {
  322. res = ioctl(h->desc, USBDEVFS_SUBMITURB, urb);
  323. } while((res < 0) && (errno == EINTR));
  324. if(res < 0) {
  325. goto fail;
  326. }
  327. h->urb_in_busy = 1;
  328. for(;;) {
  329. D("[ reap urb - wait ]\n");
  330. h->reaper_thread = pthread_self();
  331. adb_mutex_unlock(&h->lock);
  332. res = ioctl(h->desc, USBDEVFS_REAPURB, &out);
  333. int saved_errno = errno;
  334. adb_mutex_lock(&h->lock);
  335. h->reaper_thread = 0;
  336. if(h->dead) {
  337. res = -1;
  338. break;
  339. }
  340. if(res < 0) {
  341. if(saved_errno == EINTR) {
  342. continue;
  343. }
  344. D("[ reap urb - error ]\n");
  345. break;
  346. }
  347. D("[ urb @%p status = %d, actual = %d ]\n",
  348. out, out->status, out->actual_length);
  349. if(out == &h->urb_in) {
  350. D("[ reap urb - IN complete ]\n");
  351. h->urb_in_busy = 0;
  352. if(urb->status == 0) {
  353. res = urb->actual_length;
  354. } else {
  355. res = -1;
  356. }
  357. break;
  358. }
  359. if(out == &h->urb_out) {
  360. D("[ reap urb - OUT compelete ]\n");
  361. h->urb_out_busy = 0;
  362. adb_cond_broadcast(&h->notify);
  363. }
  364. }
  365. fail:
  366. adb_mutex_unlock(&h->lock);
  367. return res;
  368. }
  369. int usb_write(usb_handle *h, const void *_data, int len)
  370. {
  371. unsigned char *data = (unsigned char*) _data;
  372. int n;
  373. int need_zero = 0;
  374. if(h->zero_mask) {
  375. /* if we need 0-markers and our transfer
  376. ** is an even multiple of the packet size,
  377. ** we make note of it
  378. */
  379. if(!(len & h->zero_mask)) {
  380. need_zero = 1;
  381. }
  382. }
  383. while(len > 0) {
  384. int xfer = (len > 4096) ? 4096 : len;
  385. n = usb_bulk_write(h, data, xfer);
  386. if(n != xfer) {
  387. D("ERROR: n = %d, errno = %d (%s)\n",
  388. n, errno, strerror(errno));
  389. return -1;
  390. }
  391. len -= xfer;
  392. data += xfer;
  393. }
  394. if(need_zero){
  395. n = usb_bulk_write(h, _data, 0);
  396. return n;
  397. }
  398. return 0;
  399. }
  400. int usb_read(usb_handle *h, void *_data, int len)
  401. {
  402. unsigned char *data = (unsigned char*) _data;
  403. int n;
  404. D("++ usb_read ++\n");
  405. while(len > 0) {
  406. int xfer = (len > 4096) ? 4096 : len;
  407. D("[ usb read %d fd = %d], fname=%s\n", xfer, h->desc, h->fname);
  408. n = usb_bulk_read(h, data, xfer);
  409. D("[ usb read %d ] = %d, fname=%s\n", xfer, n, h->fname);
  410. if(n != xfer) {
  411. if((errno == ETIMEDOUT) && (h->desc != -1)) {
  412. D("[ timeout ]\n");
  413. if(n > 0){
  414. data += n;
  415. len -= n;
  416. }
  417. continue;
  418. }
  419. D("ERROR: n = %d, errno = %d (%s)\n",
  420. n, errno, strerror(errno));
  421. return -1;
  422. }
  423. len -= xfer;
  424. data += xfer;
  425. }
  426. D("-- usb_read --\n");
  427. return 0;
  428. }
  429. void usb_kick(usb_handle *h)
  430. {
  431. D("[ kicking %p (fd = %d) ]\n", h, h->desc);
  432. adb_mutex_lock(&h->lock);
  433. if(h->dead == 0) {
  434. h->dead = 1;
  435. if (h->writeable) {
  436. /* HACK ALERT!
  437. ** Sometimes we get stuck in ioctl(USBDEVFS_REAPURB).
  438. ** This is a workaround for that problem.
  439. */
  440. if (h->reaper_thread) {
  441. pthread_kill(h->reaper_thread, SIGALRM);
  442. }
  443. /* cancel any pending transactions
  444. ** these will quietly fail if the txns are not active,
  445. ** but this ensures that a reader blocked on REAPURB
  446. ** will get unblocked
  447. */
  448. ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_in);
  449. ioctl(h->desc, USBDEVFS_DISCARDURB, &h->urb_out);
  450. h->urb_in.status = -ENODEV;
  451. h->urb_out.status = -ENODEV;
  452. h->urb_in_busy = 0;
  453. h->urb_out_busy = 0;
  454. adb_cond_broadcast(&h->notify);
  455. } else {
  456. unregister_usb_transport(h);
  457. }
  458. }
  459. adb_mutex_unlock(&h->lock);
  460. }
  461. int usb_close(usb_handle *h)
  462. {
  463. D("[ usb close ... ]\n");
  464. adb_mutex_lock(&usb_lock);
  465. h->next->prev = h->prev;
  466. h->prev->next = h->next;
  467. h->prev = 0;
  468. h->next = 0;
  469. adb_close(h->desc);
  470. D("[ usb closed %p (fd = %d) ]\n", h, h->desc);
  471. adb_mutex_unlock(&usb_lock);
  472. free(h);
  473. return 0;
  474. }
  475. static void register_device(const char *dev_name, const char *devpath,
  476. unsigned char ep_in, unsigned char ep_out,
  477. int interface, int serial_index, unsigned zero_mask)
  478. {
  479. usb_handle* usb = 0;
  480. int n = 0;
  481. char serial[256];
  482. /* Since Linux will not reassign the device ID (and dev_name)
  483. ** as long as the device is open, we can add to the list here
  484. ** once we open it and remove from the list when we're finally
  485. ** closed and everything will work out fine.
  486. **
  487. ** If we have a usb_handle on the list 'o handles with a matching
  488. ** name, we have no further work to do.
  489. */
  490. adb_mutex_lock(&usb_lock);
  491. for(usb = handle_list.next; usb != &handle_list; usb = usb->next){
  492. if(!strcmp(usb->fname, dev_name)) {
  493. adb_mutex_unlock(&usb_lock);
  494. return;
  495. }
  496. }
  497. adb_mutex_unlock(&usb_lock);
  498. D("[ usb located new device %s (%d/%d/%d) ]\n",
  499. dev_name, ep_in, ep_out, interface);
  500. usb = calloc(1, sizeof(usb_handle));
  501. strcpy(usb->fname, dev_name);
  502. usb->ep_in = ep_in;
  503. usb->ep_out = ep_out;
  504. usb->zero_mask = zero_mask;
  505. usb->writeable = 1;
  506. adb_cond_init(&usb->notify, 0);
  507. adb_mutex_init(&usb->lock, 0);
  508. /* initialize mark to 1 so we don't get garbage collected after the device scan */
  509. usb->mark = 1;
  510. usb->reaper_thread = 0;
  511. usb->desc = unix_open(usb->fname, O_RDWR);
  512. if(usb->desc < 0) {
  513. /* if we fail, see if have read-only access */
  514. usb->desc = unix_open(usb->fname, O_RDONLY);
  515. if(usb->desc < 0) goto fail;
  516. usb->writeable = 0;
  517. D("[ usb open read-only %s fd = %d]\n", usb->fname, usb->desc);
  518. } else {
  519. D("[ usb open %s fd = %d]\n", usb->fname, usb->desc);
  520. n = ioctl(usb->desc, USBDEVFS_CLAIMINTERFACE, &interface);
  521. if(n != 0) goto fail;
  522. }
  523. /* read the device's serial number */
  524. serial[0] = 0;
  525. memset(serial, 0, sizeof(serial));
  526. if (serial_index) {
  527. struct usbdevfs_ctrltransfer ctrl;
  528. __u16 buffer[128];
  529. __u16 languages[128];
  530. int i, result;
  531. int languageCount = 0;
  532. memset(languages, 0, sizeof(languages));
  533. memset(&ctrl, 0, sizeof(ctrl));
  534. // read list of supported languages
  535. ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
  536. ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
  537. ctrl.wValue = (USB_DT_STRING << 8) | 0;
  538. ctrl.wIndex = 0;
  539. ctrl.wLength = sizeof(languages);
  540. ctrl.data = languages;
  541. ctrl.timeout = 1000;
  542. result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
  543. if (result > 0)
  544. languageCount = (result - 2) / 2;
  545. for (i = 1; i <= languageCount; i++) {
  546. memset(buffer, 0, sizeof(buffer));
  547. memset(&ctrl, 0, sizeof(ctrl));
  548. ctrl.bRequestType = USB_DIR_IN|USB_TYPE_STANDARD|USB_RECIP_DEVICE;
  549. ctrl.bRequest = USB_REQ_GET_DESCRIPTOR;
  550. ctrl.wValue = (USB_DT_STRING << 8) | serial_index;
  551. ctrl.wIndex = __le16_to_cpu(languages[i]);
  552. ctrl.wLength = sizeof(buffer);
  553. ctrl.data = buffer;
  554. ctrl.timeout = 1000;
  555. result = ioctl(usb->desc, USBDEVFS_CONTROL, &ctrl);
  556. if (result > 0) {
  557. int i;
  558. // skip first word, and copy the rest to the serial string, changing shorts to bytes.
  559. result /= 2;
  560. for (i = 1; i < result; i++)
  561. serial[i - 1] = __le16_to_cpu(buffer[i]);
  562. serial[i - 1] = 0;
  563. break;
  564. }
  565. }
  566. }
  567. /* add to the end of the active handles */
  568. adb_mutex_lock(&usb_lock);
  569. usb->next = &handle_list;
  570. usb->prev = handle_list.prev;
  571. usb->prev->next = usb;
  572. usb->next->prev = usb;
  573. adb_mutex_unlock(&usb_lock);
  574. register_usb_transport(usb, serial, devpath, usb->writeable);
  575. return;
  576. fail:
  577. D("[ usb open %s error=%d, err_str = %s]\n",
  578. usb->fname, errno, strerror(errno));
  579. if(usb->desc >= 0) {
  580. adb_close(usb->desc);
  581. }
  582. free(usb);
  583. }
  584. void* device_poll_thread(void* unused)
  585. {
  586. D("Created device thread\n");
  587. for(;;) {
  588. /* XXX use inotify */
  589. find_usb_device("/dev/bus/usb", register_device);
  590. kick_disconnected_devices();
  591. sleep(1);
  592. }
  593. return NULL;
  594. }
  595. static void sigalrm_handler(int signo)
  596. {
  597. // don't need to do anything here
  598. }
  599. void usb_init()
  600. {
  601. adb_thread_t tid;
  602. struct sigaction actions;
  603. memset(&actions, 0, sizeof(actions));
  604. sigemptyset(&actions.sa_mask);
  605. actions.sa_flags = 0;
  606. actions.sa_handler = sigalrm_handler;
  607. sigaction(SIGALRM,& actions, NULL);
  608. if(adb_thread_create(&tid, device_poll_thread, NULL)){
  609. fatal_errno("cannot create input thread");
  610. }
  611. }