usb_linux_client.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595
  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 <linux/usb/ch9.h>
  21. #include <linux/usb/functionfs.h>
  22. #include <sys/ioctl.h>
  23. #include <sys/types.h>
  24. #include <dirent.h>
  25. #include <errno.h>
  26. #include <dirent.h>
  27. #include "sysdeps.h"
  28. #define TRACE_TAG TRACE_USB
  29. #include "adb.h"
  30. #define MAX_PACKET_SIZE_FS 64
  31. #define MAX_PACKET_SIZE_HS 512
  32. #define __uint16_identity(x) x
  33. #define __uint32_identity(x) x
  34. #define __uint64_identity(x) x
  35. #define __bswap_constant_16(x) \
  36. ((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
  37. #define __bswap_16(x) __bswap_constant_16(x)
  38. #define __bswap_constant_32(x) \
  39. ((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
  40. #define __bswap_32(x) __bswap_constant_32(x)
  41. #define __bswap_constant_64(x) \
  42. ((__uint16_t) ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8)))
  43. #define __bswap_64(x) __bswap_constant_64(x)
  44. # if __BYTE_ORDER == __LITTLE_ENDIAN
  45. # define htobe16(x) __bswap_16 (x)
  46. # define htole16(x) __uint16_identity (x)
  47. # define be16toh(x) __bswap_16 (x)
  48. # define le16toh(x) __uint16_identity (x)
  49. # define htobe32(x) __bswap_32 (x)
  50. # define htole32(x) __uint32_identity (x)
  51. # define be32toh(x) __bswap_32 (x)
  52. # define le32toh(x) __uint32_identity (x)
  53. # define htobe64(x) __bswap_64 (x)
  54. # define htole64(x) __uint64_identity (x)
  55. # define be64toh(x) __bswap_64 (x)
  56. # define le64toh(x) __uint64_identity (x)
  57. # else
  58. # define htobe16(x) __uint16_identity (x)
  59. # define htole16(x) __bswap_16 (x)
  60. # define be16toh(x) __uint16_identity (x)
  61. # define le16toh(x) __bswap_16 (x)
  62. # define htobe32(x) __uint32_identity (x)
  63. # define htole32(x) __bswap_32 (x)
  64. # define be32toh(x) __uint32_identity (x)
  65. # define le32toh(x) __bswap_32 (x)
  66. # define htobe64(x) __uint64_identity (x)
  67. # define htole64(x) __bswap_64 (x)
  68. # define be64toh(x) __uint64_identity (x)
  69. # define le64toh(x) __bswap_64 (x)
  70. #endif
  71. #define cpu_to_le16(x) htole16(x)
  72. #define cpu_to_le32(x) htole32(x)
  73. struct usb_handle
  74. {
  75. adb_cond_t notify;
  76. adb_mutex_t lock;
  77. int (*write)(usb_handle *h, const void *data, int len);
  78. int (*read)(usb_handle *h, void *data, int len);
  79. void (*kick)(usb_handle *h);
  80. // Legacy f_adb
  81. int fd;
  82. // FunctionFS
  83. int control;
  84. int bulk_out; /* "out" from the host's perspective => source for adbd */
  85. int bulk_in; /* "in" from the host's perspective => sink for adbd */
  86. };
  87. static const struct {
  88. struct usb_functionfs_descs_head header;
  89. struct {
  90. struct usb_interface_descriptor intf;
  91. struct usb_endpoint_descriptor_no_audio source;
  92. struct usb_endpoint_descriptor_no_audio sink;
  93. } __attribute__((packed)) fs_descs, hs_descs;
  94. } __attribute__((packed)) descriptors = {
  95. .header = {
  96. .magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC),
  97. .length = cpu_to_le32(sizeof(descriptors)),
  98. .fs_count = 3,
  99. .hs_count = 3,
  100. },
  101. .fs_descs = {
  102. .intf = {
  103. .bLength = sizeof(descriptors.fs_descs.intf),
  104. .bDescriptorType = USB_DT_INTERFACE,
  105. .bInterfaceNumber = 0,
  106. .bNumEndpoints = 2,
  107. .bInterfaceClass = ADB_CLASS,
  108. .bInterfaceSubClass = ADB_SUBCLASS,
  109. .bInterfaceProtocol = ADB_PROTOCOL,
  110. .iInterface = 1, /* first string from the provided table */
  111. },
  112. .source = {
  113. .bLength = sizeof(descriptors.fs_descs.source),
  114. .bDescriptorType = USB_DT_ENDPOINT,
  115. .bEndpointAddress = 1 | USB_DIR_OUT,
  116. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  117. .wMaxPacketSize = MAX_PACKET_SIZE_FS,
  118. },
  119. .sink = {
  120. .bLength = sizeof(descriptors.fs_descs.sink),
  121. .bDescriptorType = USB_DT_ENDPOINT,
  122. .bEndpointAddress = 2 | USB_DIR_IN,
  123. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  124. .wMaxPacketSize = MAX_PACKET_SIZE_FS,
  125. },
  126. },
  127. .hs_descs = {
  128. .intf = {
  129. .bLength = sizeof(descriptors.hs_descs.intf),
  130. .bDescriptorType = USB_DT_INTERFACE,
  131. .bInterfaceNumber = 0,
  132. .bNumEndpoints = 2,
  133. .bInterfaceClass = ADB_CLASS,
  134. .bInterfaceSubClass = ADB_SUBCLASS,
  135. .bInterfaceProtocol = ADB_PROTOCOL,
  136. .iInterface = 1, /* first string from the provided table */
  137. },
  138. .source = {
  139. .bLength = sizeof(descriptors.hs_descs.source),
  140. .bDescriptorType = USB_DT_ENDPOINT,
  141. .bEndpointAddress = 1 | USB_DIR_OUT,
  142. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  143. .wMaxPacketSize = MAX_PACKET_SIZE_HS,
  144. },
  145. .sink = {
  146. .bLength = sizeof(descriptors.hs_descs.sink),
  147. .bDescriptorType = USB_DT_ENDPOINT,
  148. .bEndpointAddress = 2 | USB_DIR_IN,
  149. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  150. .wMaxPacketSize = MAX_PACKET_SIZE_HS,
  151. },
  152. },
  153. };
  154. #define STR_INTERFACE_ "ADB Interface"
  155. static const struct {
  156. struct usb_functionfs_strings_head header;
  157. struct {
  158. __le16 code;
  159. const char str1[sizeof(STR_INTERFACE_)];
  160. } __attribute__((packed)) lang0;
  161. } __attribute__((packed)) strings = {
  162. .header = {
  163. .magic = cpu_to_le32(FUNCTIONFS_STRINGS_MAGIC),
  164. .length = cpu_to_le32(sizeof(strings)),
  165. .str_count = cpu_to_le32(1),
  166. .lang_count = cpu_to_le32(1),
  167. },
  168. .lang0 = {
  169. cpu_to_le16(0x0409), /* en-us */
  170. STR_INTERFACE_,
  171. },
  172. };
  173. static void *usb_adb_open_thread(void *x)
  174. {
  175. struct usb_handle *usb = (struct usb_handle *)x;
  176. int fd;
  177. while (1) {
  178. // wait until the USB device needs opening
  179. adb_mutex_lock(&usb->lock);
  180. while (usb->fd != -1)
  181. adb_cond_wait(&usb->notify, &usb->lock);
  182. adb_mutex_unlock(&usb->lock);
  183. D("[ usb_thread - opening device ]\n");
  184. do {
  185. /* XXX use inotify? */
  186. fd = unix_open("/dev/android_adb", O_RDWR);
  187. if (fd < 0) {
  188. // to support older kernels
  189. fd = unix_open("/dev/android", O_RDWR);
  190. }
  191. if (fd < 0) {
  192. adb_sleep_ms(1000);
  193. }
  194. } while (fd < 0);
  195. D("[ opening device succeeded ]\n");
  196. close_on_exec(fd);
  197. usb->fd = fd;
  198. D("[ usb_thread - registering device ]\n");
  199. register_usb_transport(usb, 0, 0, 1);
  200. }
  201. // never gets here
  202. return 0;
  203. }
  204. static int usb_adb_write(usb_handle *h, const void *data, int len)
  205. {
  206. int n;
  207. D("about to write (fd=%d, len=%d)\n", h->fd, len);
  208. n = adb_write(h->fd, data, len);
  209. if(n != len) {
  210. D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
  211. h->fd, n, errno, strerror(errno));
  212. return -1;
  213. }
  214. D("[ done fd=%d ]\n", h->fd);
  215. return 0;
  216. }
  217. static int usb_adb_read(usb_handle *h, void *data, int len)
  218. {
  219. int n;
  220. D("about to read (fd=%d, len=%d)\n", h->fd, len);
  221. n = adb_read(h->fd, data, len);
  222. if(n != len) {
  223. D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
  224. h->fd, n, errno, strerror(errno));
  225. return -1;
  226. }
  227. D("[ done fd=%d ]\n", h->fd);
  228. return 0;
  229. }
  230. static void usb_adb_kick(usb_handle *h)
  231. {
  232. D("usb_kick\n");
  233. adb_mutex_lock(&h->lock);
  234. adb_close(h->fd);
  235. h->fd = -1;
  236. // notify usb_adb_open_thread that we are disconnected
  237. adb_cond_signal(&h->notify);
  238. adb_mutex_unlock(&h->lock);
  239. }
  240. static void usb_adb_init()
  241. {
  242. usb_handle *h;
  243. adb_thread_t tid;
  244. int fd;
  245. h = calloc(1, sizeof(usb_handle));
  246. h->write = usb_adb_write;
  247. h->read = usb_adb_read;
  248. h->kick = usb_adb_kick;
  249. h->fd = -1;
  250. adb_cond_init(&h->notify, 0);
  251. adb_mutex_init(&h->lock, 0);
  252. // Open the file /dev/android_adb_enable to trigger
  253. // the enabling of the adb USB function in the kernel.
  254. // We never touch this file again - just leave it open
  255. // indefinitely so the kernel will know when we are running
  256. // and when we are not.
  257. fd = unix_open("/dev/android_adb_enable", O_RDWR);
  258. if (fd < 0) {
  259. D("failed to open /dev/android_adb_enable\n");
  260. } else {
  261. close_on_exec(fd);
  262. }
  263. D("[ usb_init - starting thread ]\n");
  264. if(adb_thread_create(&tid, usb_adb_open_thread, h)){
  265. fatal_errno("cannot create usb thread");
  266. }
  267. }
  268. static void set_udc(void)
  269. {
  270. static char g_udc_controller[64];
  271. const char *udc_config_path = "/sys/kernel/config/usb_gadget/g1/UDC";
  272. int fd;
  273. if (strlen(g_udc_controller) <= 0) {
  274. const char *udc_path = "/sys/class/udc";
  275. DIR *dir;
  276. struct dirent *ent;
  277. dir = opendir(udc_path);
  278. if (!dir) {
  279. D("[%s: opendir %s failed]\n", __func__, udc_path);
  280. return;
  281. }
  282. while((ent = readdir(dir)) != NULL) {
  283. if (ent->d_type & DT_REG) {
  284. if (!strcmp(".", ent->d_name))
  285. continue;
  286. else if (!strcmp("..", ent->d_name))
  287. continue;
  288. else
  289. break;
  290. }
  291. }
  292. if (!ent) {
  293. D("cannot found udc dir, udc driver maybe not select\n");
  294. exit(-1);
  295. }
  296. D("%s: get udc controller: %s \n", __func__, ent->d_name);
  297. strncpy(g_udc_controller, ent->d_name, sizeof(g_udc_controller));
  298. closedir(dir);
  299. }
  300. fd = adb_open(udc_config_path, O_RDWR);
  301. if (fd<0) {
  302. D("[%s: open %s failed]\n", __func__, udc_config_path);
  303. return;
  304. }
  305. adb_write(fd, g_udc_controller, strlen(g_udc_controller));
  306. adb_close(fd);
  307. return;
  308. }
  309. static void init_functionfs(struct usb_handle *h)
  310. {
  311. ssize_t ret;
  312. D("OPENING %s\n", USB_FFS_ADB_EP0);
  313. h->control = adb_open(USB_FFS_ADB_EP0, O_RDWR);
  314. if (h->control < 0) {
  315. D("[ %s: cannot open control endpoint: errno=%d]\n", USB_FFS_ADB_EP0, errno);
  316. goto err;
  317. }
  318. ret = adb_write(h->control, &descriptors, sizeof(descriptors));
  319. if (ret < 0) {
  320. D("[ %s: write descriptors failed: errno=%d ]\n", USB_FFS_ADB_EP0, errno);
  321. goto err;
  322. }
  323. ret = adb_write(h->control, &strings, sizeof(strings));
  324. if (ret < 0) {
  325. D("[ %s: writing strings failed: errno=%d]\n", USB_FFS_ADB_EP0, errno);
  326. goto err;
  327. }
  328. h->bulk_out = adb_open(USB_FFS_ADB_OUT, O_RDWR);
  329. if (h->bulk_out < 0) {
  330. D("[ %s: cannot open bulk-out ep: errno=%d ]\n", USB_FFS_ADB_OUT, errno);
  331. goto err;
  332. }
  333. h->bulk_in = adb_open(USB_FFS_ADB_IN, O_RDWR);
  334. if (h->bulk_in < 0) {
  335. D("[ %s: cannot open bulk-in ep: errno=%d ]\n", USB_FFS_ADB_IN, errno);
  336. goto err;
  337. }
  338. /* add by ForeverCai
  339. * In FFS mode, udc name must be set after initializaion
  340. * So add this function.
  341. * */
  342. set_udc();
  343. return;
  344. err:
  345. if (h->bulk_in > 0) {
  346. adb_close(h->bulk_in);
  347. h->bulk_in = -1;
  348. }
  349. if (h->bulk_out > 0) {
  350. adb_close(h->bulk_out);
  351. h->bulk_out = -1;
  352. }
  353. if (h->control > 0) {
  354. adb_close(h->control);
  355. h->control = -1;
  356. }
  357. return;
  358. }
  359. static void *usb_ffs_open_thread(void *x)
  360. {
  361. struct usb_handle *usb = (struct usb_handle *)x;
  362. while (1) {
  363. // wait until the USB device needs opening
  364. adb_mutex_lock(&usb->lock);
  365. while (usb->control != -1)
  366. adb_cond_wait(&usb->notify, &usb->lock);
  367. adb_mutex_unlock(&usb->lock);
  368. while (1) {
  369. init_functionfs(usb);
  370. if (usb->control >= 0)
  371. break;
  372. adb_sleep_ms(1000);
  373. }
  374. D("[ usb_thread - registering device ]\n");
  375. register_usb_transport(usb, 0, 0, 1);
  376. }
  377. // never gets here
  378. return 0;
  379. }
  380. static int bulk_write(int bulk_in, const char *buf, size_t length)
  381. {
  382. size_t count = 0;
  383. int ret;
  384. do {
  385. ret = adb_write(bulk_in, buf + count, length - count);
  386. if (ret < 0) {
  387. if (errno != EINTR)
  388. return ret;
  389. } else {
  390. count += ret;
  391. }
  392. } while (count < length);
  393. D("[ bulk_write done fd=%d ]\n", bulk_in);
  394. return count;
  395. }
  396. static int usb_ffs_write(usb_handle *h, const void *data, int len)
  397. {
  398. int n;
  399. D("about to write (fd=%d, len=%d)\n", h->bulk_in, len);
  400. n = bulk_write(h->bulk_in, data, len);
  401. if (n != len) {
  402. D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
  403. h->bulk_in, n, errno, strerror(errno));
  404. return -1;
  405. }
  406. D("[ done fd=%d ]\n", h->bulk_in);
  407. return 0;
  408. }
  409. static int bulk_read(int bulk_out, char *buf, size_t length)
  410. {
  411. size_t count = 0;
  412. int ret;
  413. do {
  414. ret = adb_read(bulk_out, buf + count, length - count);
  415. if (ret < 0) {
  416. if (errno != EINTR) {
  417. D("[ bulk_read failed fd=%d length=%d count=%d ]\n",
  418. bulk_out, length, count);
  419. return ret;
  420. }
  421. } else {
  422. count += ret;
  423. }
  424. } while (count < length);
  425. return count;
  426. }
  427. static int usb_ffs_read(usb_handle *h, void *data, int len)
  428. {
  429. int n;
  430. D("about to read (fd=%d, len=%d)\n", h->bulk_out, len);
  431. n = bulk_read(h->bulk_out, data, len);
  432. if (n != len) {
  433. D("ERROR: fd = %d, n = %d, errno = %d (%s)\n",
  434. h->bulk_out, n, errno, strerror(errno));
  435. return -1;
  436. }
  437. D("[ done fd=%d ]\n", h->bulk_out);
  438. return 0;
  439. }
  440. static void usb_ffs_kick(usb_handle *h)
  441. {
  442. int err;
  443. err = ioctl(h->bulk_in, FUNCTIONFS_CLEAR_HALT);
  444. if (err < 0)
  445. D("[ kick: source (fd=%d) clear halt failed (%d) ]", h->bulk_in, errno);
  446. err = ioctl(h->bulk_out, FUNCTIONFS_CLEAR_HALT);
  447. if (err < 0)
  448. D("[ kick: sink (fd=%d) clear halt failed (%d) ]", h->bulk_out, errno);
  449. adb_mutex_lock(&h->lock);
  450. adb_close(h->control);
  451. adb_close(h->bulk_out);
  452. adb_close(h->bulk_in);
  453. h->control = h->bulk_out = h->bulk_in = -1;
  454. // notify usb_ffs_open_thread that we are disconnected
  455. adb_cond_signal(&h->notify);
  456. adb_mutex_unlock(&h->lock);
  457. }
  458. static void usb_ffs_init()
  459. {
  460. usb_handle *h;
  461. adb_thread_t tid;
  462. D("[ usb_init - using FunctionFS ]\n");
  463. h = calloc(1, sizeof(usb_handle));
  464. h->write = usb_ffs_write;
  465. h->read = usb_ffs_read;
  466. h->kick = usb_ffs_kick;
  467. h->control = -1;
  468. h->bulk_out = -1;
  469. h->bulk_out = -1;
  470. adb_cond_init(&h->notify, 0);
  471. adb_mutex_init(&h->lock, 0);
  472. D("[ usb_init - starting thread ]\n");
  473. if (adb_thread_create(&tid, usb_ffs_open_thread, h)){
  474. fatal_errno("[ cannot create usb thread ]\n");
  475. }
  476. }
  477. void usb_init()
  478. {
  479. if (access(USB_FFS_ADB_EP0, F_OK) == 0)
  480. usb_ffs_init();
  481. else
  482. usb_adb_init();
  483. }
  484. void usb_cleanup()
  485. {
  486. }
  487. int usb_write(usb_handle *h, const void *data, int len)
  488. {
  489. return h->write(h, data, len);
  490. }
  491. int usb_read(usb_handle *h, void *data, int len)
  492. {
  493. return h->read(h, data, len);
  494. }
  495. int usb_close(usb_handle *h)
  496. {
  497. return 0;
  498. }
  499. void usb_kick(usb_handle *h)
  500. {
  501. h->kick(h);
  502. }