f_adb.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. /*
  2. * Gadget Driver for Android ADB
  3. *
  4. * Copyright (C) 2008 Google, Inc.
  5. * Author: Mike Lockwood <lockwood@android.com>
  6. *
  7. * This software is licensed under the terms of the GNU General Public
  8. * License version 2, as published by the Free Software Foundation, and
  9. * may be copied, distributed, and modified under those terms.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/poll.h>
  20. #include <linux/delay.h>
  21. #include <linux/wait.h>
  22. #include <linux/err.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/sched.h>
  25. #include <linux/types.h>
  26. #include <linux/device.h>
  27. #include <linux/miscdevice.h>
  28. #define ADB_BULK_BUFFER_SIZE 4096
  29. /* number of tx requests to allocate */
  30. #define TX_REQ_MAX 4
  31. static const char adb_shortname[] = "android_adb";
  32. struct adb_dev {
  33. struct usb_function function;
  34. struct usb_composite_dev *cdev;
  35. spinlock_t lock;
  36. struct usb_ep *ep_in;
  37. struct usb_ep *ep_out;
  38. int online;
  39. int error;
  40. atomic_t read_excl;
  41. atomic_t write_excl;
  42. atomic_t open_excl;
  43. struct list_head tx_idle;
  44. wait_queue_head_t read_wq;
  45. wait_queue_head_t write_wq;
  46. struct usb_request *rx_req;
  47. int rx_done;
  48. };
  49. static struct usb_interface_descriptor adb_interface_desc = {
  50. .bLength = USB_DT_INTERFACE_SIZE,
  51. .bDescriptorType = USB_DT_INTERFACE,
  52. .bInterfaceNumber = 0,
  53. .bNumEndpoints = 2,
  54. .bInterfaceClass = 0xFF,
  55. .bInterfaceSubClass = 0x42,
  56. .bInterfaceProtocol = 1,
  57. };
  58. static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
  59. .bLength = USB_DT_ENDPOINT_SIZE,
  60. .bDescriptorType = USB_DT_ENDPOINT,
  61. .bEndpointAddress = USB_DIR_IN,
  62. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  63. .wMaxPacketSize = __constant_cpu_to_le16(512),
  64. };
  65. static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
  66. .bLength = USB_DT_ENDPOINT_SIZE,
  67. .bDescriptorType = USB_DT_ENDPOINT,
  68. .bEndpointAddress = USB_DIR_OUT,
  69. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  70. .wMaxPacketSize = __constant_cpu_to_le16(512),
  71. };
  72. static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
  73. .bLength = USB_DT_ENDPOINT_SIZE,
  74. .bDescriptorType = USB_DT_ENDPOINT,
  75. .bEndpointAddress = USB_DIR_IN,
  76. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  77. };
  78. static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
  79. .bLength = USB_DT_ENDPOINT_SIZE,
  80. .bDescriptorType = USB_DT_ENDPOINT,
  81. .bEndpointAddress = USB_DIR_OUT,
  82. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  83. };
  84. static struct usb_descriptor_header *fs_adb_descs[] = {
  85. (struct usb_descriptor_header *) &adb_interface_desc,
  86. (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
  87. (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
  88. NULL,
  89. };
  90. static struct usb_descriptor_header *hs_adb_descs[] = {
  91. (struct usb_descriptor_header *) &adb_interface_desc,
  92. (struct usb_descriptor_header *) &adb_highspeed_in_desc,
  93. (struct usb_descriptor_header *) &adb_highspeed_out_desc,
  94. NULL,
  95. };
  96. static void adb_ready_callback(void);
  97. static void adb_closed_callback(void);
  98. /* temporary variable used between adb_open() and adb_gadget_bind() */
  99. static struct adb_dev *_adb_dev;
  100. static inline struct adb_dev *func_to_adb(struct usb_function *f)
  101. {
  102. return container_of(f, struct adb_dev, function);
  103. }
  104. static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
  105. {
  106. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  107. if (!req)
  108. return NULL;
  109. /* now allocate buffers for the requests */
  110. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  111. if (!req->buf) {
  112. usb_ep_free_request(ep, req);
  113. return NULL;
  114. }
  115. return req;
  116. }
  117. static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
  118. {
  119. if (req) {
  120. kfree(req->buf);
  121. usb_ep_free_request(ep, req);
  122. }
  123. }
  124. static inline int adb_lock(atomic_t *excl)
  125. {
  126. if (atomic_inc_return(excl) == 1) {
  127. return 0;
  128. } else {
  129. atomic_dec(excl);
  130. return -1;
  131. }
  132. }
  133. static inline void adb_unlock(atomic_t *excl)
  134. {
  135. atomic_dec(excl);
  136. }
  137. /* add a request to the tail of a list */
  138. void adb_req_put(struct adb_dev *dev, struct list_head *head,
  139. struct usb_request *req)
  140. {
  141. unsigned long flags;
  142. spin_lock_irqsave(&dev->lock, flags);
  143. list_add_tail(&req->list, head);
  144. spin_unlock_irqrestore(&dev->lock, flags);
  145. }
  146. /* remove a request from the head of a list */
  147. struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
  148. {
  149. unsigned long flags;
  150. struct usb_request *req;
  151. spin_lock_irqsave(&dev->lock, flags);
  152. if (list_empty(head)) {
  153. req = 0;
  154. } else {
  155. req = list_first_entry(head, struct usb_request, list);
  156. list_del(&req->list);
  157. }
  158. spin_unlock_irqrestore(&dev->lock, flags);
  159. return req;
  160. }
  161. static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
  162. {
  163. struct adb_dev *dev = _adb_dev;
  164. if (req->status != 0)
  165. dev->error = 1;
  166. adb_req_put(dev, &dev->tx_idle, req);
  167. wake_up(&dev->write_wq);
  168. }
  169. static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
  170. {
  171. struct adb_dev *dev = _adb_dev;
  172. dev->rx_done = 1;
  173. if (req->status != 0)
  174. dev->error = 1;
  175. wake_up(&dev->read_wq);
  176. }
  177. static int adb_create_bulk_endpoints(struct adb_dev *dev,
  178. struct usb_endpoint_descriptor *in_desc,
  179. struct usb_endpoint_descriptor *out_desc)
  180. {
  181. struct usb_composite_dev *cdev = dev->cdev;
  182. struct usb_request *req;
  183. struct usb_ep *ep;
  184. int i;
  185. DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
  186. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  187. if (!ep) {
  188. DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
  189. return -ENODEV;
  190. }
  191. DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
  192. ep->driver_data = dev; /* claim the endpoint */
  193. dev->ep_in = ep;
  194. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  195. if (!ep) {
  196. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  197. return -ENODEV;
  198. }
  199. DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
  200. ep->driver_data = dev; /* claim the endpoint */
  201. dev->ep_out = ep;
  202. /* now allocate requests for our endpoints */
  203. req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
  204. if (!req)
  205. goto fail;
  206. req->complete = adb_complete_out;
  207. dev->rx_req = req;
  208. for (i = 0; i < TX_REQ_MAX; i++) {
  209. req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
  210. if (!req)
  211. goto fail;
  212. req->complete = adb_complete_in;
  213. adb_req_put(dev, &dev->tx_idle, req);
  214. }
  215. return 0;
  216. fail:
  217. printk(KERN_ERR "adb_bind() could not allocate requests\n");
  218. return -1;
  219. }
  220. static ssize_t adb_read(struct file *fp, char __user *buf,
  221. size_t count, loff_t *pos)
  222. {
  223. struct adb_dev *dev = fp->private_data;
  224. struct usb_request *req;
  225. int r = count, xfer;
  226. int ret;
  227. pr_debug("adb_read(%d)\n", count);
  228. if (!_adb_dev)
  229. return -ENODEV;
  230. if (count > ADB_BULK_BUFFER_SIZE)
  231. return -EINVAL;
  232. if (adb_lock(&dev->read_excl))
  233. return -EBUSY;
  234. /* we will block until we're online */
  235. while (!(dev->online || dev->error)) {
  236. pr_debug("adb_read: waiting for online state\n");
  237. ret = wait_event_interruptible(dev->read_wq,
  238. (dev->online || dev->error));
  239. if (ret < 0) {
  240. adb_unlock(&dev->read_excl);
  241. return ret;
  242. }
  243. }
  244. if (dev->error) {
  245. r = -EIO;
  246. goto done;
  247. }
  248. requeue_req:
  249. /* queue a request */
  250. req = dev->rx_req;
  251. req->length = count;
  252. dev->rx_done = 0;
  253. ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
  254. if (ret < 0) {
  255. pr_debug("adb_read: failed to queue req %p (%d)\n", req, ret);
  256. r = -EIO;
  257. dev->error = 1;
  258. goto done;
  259. } else {
  260. pr_debug("rx %p queue\n", req);
  261. }
  262. /* wait for a request to complete */
  263. ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
  264. if (ret < 0) {
  265. dev->error = 1;
  266. r = ret;
  267. usb_ep_dequeue(dev->ep_out, req);
  268. goto done;
  269. }
  270. if (!dev->error) {
  271. /* If we got a 0-len packet, throw it back and try again. */
  272. if (req->actual == 0)
  273. goto requeue_req;
  274. pr_debug("rx %p %d\n", req, req->actual);
  275. xfer = (req->actual < count) ? req->actual : count;
  276. if (copy_to_user(buf, req->buf, xfer))
  277. r = -EFAULT;
  278. } else
  279. r = -EIO;
  280. done:
  281. adb_unlock(&dev->read_excl);
  282. pr_debug("adb_read returning %d\n", r);
  283. return r;
  284. }
  285. static ssize_t adb_write(struct file *fp, const char __user *buf,
  286. size_t count, loff_t *pos)
  287. {
  288. struct adb_dev *dev = fp->private_data;
  289. struct usb_request *req = 0;
  290. int r = count, xfer;
  291. int ret;
  292. if (!_adb_dev)
  293. return -ENODEV;
  294. pr_debug("adb_write(%d)\n", count);
  295. if (adb_lock(&dev->write_excl))
  296. return -EBUSY;
  297. while (count > 0) {
  298. if (dev->error) {
  299. pr_debug("adb_write dev->error\n");
  300. r = -EIO;
  301. break;
  302. }
  303. /* get an idle tx request to use */
  304. req = 0;
  305. ret = wait_event_interruptible(dev->write_wq,
  306. (req = adb_req_get(dev, &dev->tx_idle)) || dev->error);
  307. if (ret < 0) {
  308. r = ret;
  309. break;
  310. }
  311. if (req != 0) {
  312. if (count > ADB_BULK_BUFFER_SIZE)
  313. xfer = ADB_BULK_BUFFER_SIZE;
  314. else
  315. xfer = count;
  316. if (copy_from_user(req->buf, buf, xfer)) {
  317. r = -EFAULT;
  318. break;
  319. }
  320. req->length = xfer;
  321. ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
  322. if (ret < 0) {
  323. pr_debug("adb_write: xfer error %d\n", ret);
  324. dev->error = 1;
  325. r = -EIO;
  326. break;
  327. }
  328. buf += xfer;
  329. count -= xfer;
  330. /* zero this so we don't try to free it on error exit */
  331. req = 0;
  332. }
  333. }
  334. if (req)
  335. adb_req_put(dev, &dev->tx_idle, req);
  336. adb_unlock(&dev->write_excl);
  337. pr_debug("adb_write returning %d\n", r);
  338. return r;
  339. }
  340. static int adb_open(struct inode *ip, struct file *fp)
  341. {
  342. pr_info("adb_open\n");
  343. if (!_adb_dev)
  344. return -ENODEV;
  345. if (adb_lock(&_adb_dev->open_excl))
  346. return -EBUSY;
  347. fp->private_data = _adb_dev;
  348. /* clear the error latch */
  349. _adb_dev->error = 0;
  350. adb_ready_callback();
  351. return 0;
  352. }
  353. static int adb_release(struct inode *ip, struct file *fp)
  354. {
  355. pr_info("adb_release\n");
  356. adb_closed_callback();
  357. adb_unlock(&_adb_dev->open_excl);
  358. return 0;
  359. }
  360. /* file operations for ADB device /dev/android_adb */
  361. static struct file_operations adb_fops = {
  362. .owner = THIS_MODULE,
  363. .read = adb_read,
  364. .write = adb_write,
  365. .open = adb_open,
  366. .release = adb_release,
  367. };
  368. static struct miscdevice adb_device = {
  369. .minor = MISC_DYNAMIC_MINOR,
  370. .name = adb_shortname,
  371. .fops = &adb_fops,
  372. };
  373. static int
  374. adb_function_bind(struct usb_configuration *c, struct usb_function *f)
  375. {
  376. struct usb_composite_dev *cdev = c->cdev;
  377. struct adb_dev *dev = func_to_adb(f);
  378. int id;
  379. int ret;
  380. dev->cdev = cdev;
  381. DBG(cdev, "adb_function_bind dev: %p\n", dev);
  382. /* allocate interface ID(s) */
  383. id = usb_interface_id(c, f);
  384. if (id < 0)
  385. return id;
  386. adb_interface_desc.bInterfaceNumber = id;
  387. /* allocate endpoints */
  388. ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
  389. &adb_fullspeed_out_desc);
  390. if (ret)
  391. return ret;
  392. /* support high speed hardware */
  393. if (gadget_is_dualspeed(c->cdev->gadget)) {
  394. adb_highspeed_in_desc.bEndpointAddress =
  395. adb_fullspeed_in_desc.bEndpointAddress;
  396. adb_highspeed_out_desc.bEndpointAddress =
  397. adb_fullspeed_out_desc.bEndpointAddress;
  398. }
  399. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  400. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  401. f->name, dev->ep_in->name, dev->ep_out->name);
  402. return 0;
  403. }
  404. static void
  405. adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
  406. {
  407. struct adb_dev *dev = func_to_adb(f);
  408. struct usb_request *req;
  409. dev->online = 0;
  410. dev->error = 1;
  411. wake_up(&dev->read_wq);
  412. adb_request_free(dev->rx_req, dev->ep_out);
  413. while ((req = adb_req_get(dev, &dev->tx_idle)))
  414. adb_request_free(req, dev->ep_in);
  415. }
  416. static int adb_function_set_alt(struct usb_function *f,
  417. unsigned intf, unsigned alt)
  418. {
  419. struct adb_dev *dev = func_to_adb(f);
  420. struct usb_composite_dev *cdev = f->config->cdev;
  421. int ret;
  422. DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
  423. ret = usb_ep_enable(dev->ep_in,
  424. ep_choose(cdev->gadget,
  425. &adb_highspeed_in_desc,
  426. &adb_fullspeed_in_desc));
  427. if (ret)
  428. return ret;
  429. ret = usb_ep_enable(dev->ep_out,
  430. ep_choose(cdev->gadget,
  431. &adb_highspeed_out_desc,
  432. &adb_fullspeed_out_desc));
  433. if (ret) {
  434. usb_ep_disable(dev->ep_in);
  435. return ret;
  436. }
  437. dev->online = 1;
  438. /* readers may be blocked waiting for us to go online */
  439. wake_up(&dev->read_wq);
  440. return 0;
  441. }
  442. static void adb_function_disable(struct usb_function *f)
  443. {
  444. struct adb_dev *dev = func_to_adb(f);
  445. struct usb_composite_dev *cdev = dev->cdev;
  446. DBG(cdev, "adb_function_disable cdev %p\n", cdev);
  447. dev->online = 0;
  448. dev->error = 1;
  449. usb_ep_disable(dev->ep_in);
  450. usb_ep_disable(dev->ep_out);
  451. /* readers may be blocked waiting for us to go online */
  452. wake_up(&dev->read_wq);
  453. VDBG(cdev, "%s disabled\n", dev->function.name);
  454. }
  455. static int adb_bind_config(struct usb_configuration *c)
  456. {
  457. struct adb_dev *dev = _adb_dev;
  458. printk(KERN_INFO "adb_bind_config\n");
  459. dev->cdev = c->cdev;
  460. dev->function.name = "adb";
  461. dev->function.descriptors = fs_adb_descs;
  462. dev->function.hs_descriptors = hs_adb_descs;
  463. dev->function.bind = adb_function_bind;
  464. dev->function.unbind = adb_function_unbind;
  465. dev->function.set_alt = adb_function_set_alt;
  466. dev->function.disable = adb_function_disable;
  467. return usb_add_function(c, &dev->function);
  468. }
  469. static int adb_setup(void)
  470. {
  471. struct adb_dev *dev;
  472. int ret;
  473. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  474. if (!dev)
  475. return -ENOMEM;
  476. spin_lock_init(&dev->lock);
  477. init_waitqueue_head(&dev->read_wq);
  478. init_waitqueue_head(&dev->write_wq);
  479. atomic_set(&dev->open_excl, 0);
  480. atomic_set(&dev->read_excl, 0);
  481. atomic_set(&dev->write_excl, 0);
  482. INIT_LIST_HEAD(&dev->tx_idle);
  483. _adb_dev = dev;
  484. ret = misc_register(&adb_device);
  485. if (ret)
  486. goto err;
  487. return 0;
  488. err:
  489. kfree(dev);
  490. printk(KERN_ERR "adb gadget driver failed to initialize\n");
  491. return ret;
  492. }
  493. static void adb_cleanup(void)
  494. {
  495. misc_deregister(&adb_device);
  496. kfree(_adb_dev);
  497. _adb_dev = NULL;
  498. }