f_adb.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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. atomic_t online;
  39. atomic_t 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. bool notify_close;
  49. bool close_notified;
  50. };
  51. static struct usb_interface_descriptor adb_interface_desc = {
  52. .bLength = USB_DT_INTERFACE_SIZE,
  53. .bDescriptorType = USB_DT_INTERFACE,
  54. .bInterfaceNumber = 0,
  55. .bNumEndpoints = 2,
  56. .bInterfaceClass = 0xFF,
  57. .bInterfaceSubClass = 0x42,
  58. .bInterfaceProtocol = 1,
  59. };
  60. static struct usb_endpoint_descriptor adb_superspeed_in_desc = {
  61. .bLength = USB_DT_ENDPOINT_SIZE,
  62. .bDescriptorType = USB_DT_ENDPOINT,
  63. .bEndpointAddress = USB_DIR_IN,
  64. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  65. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  66. };
  67. static struct usb_ss_ep_comp_descriptor adb_superspeed_in_comp_desc = {
  68. .bLength = sizeof adb_superspeed_in_comp_desc,
  69. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  70. /* the following 2 values can be tweaked if necessary */
  71. /* .bMaxBurst = 0, */
  72. /* .bmAttributes = 0, */
  73. };
  74. static struct usb_endpoint_descriptor adb_superspeed_out_desc = {
  75. .bLength = USB_DT_ENDPOINT_SIZE,
  76. .bDescriptorType = USB_DT_ENDPOINT,
  77. .bEndpointAddress = USB_DIR_OUT,
  78. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  79. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  80. };
  81. static struct usb_ss_ep_comp_descriptor adb_superspeed_out_comp_desc = {
  82. .bLength = sizeof adb_superspeed_out_comp_desc,
  83. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  84. /* the following 2 values can be tweaked if necessary */
  85. /* .bMaxBurst = 0, */
  86. /* .bmAttributes = 0, */
  87. };
  88. static struct usb_endpoint_descriptor adb_highspeed_in_desc = {
  89. .bLength = USB_DT_ENDPOINT_SIZE,
  90. .bDescriptorType = USB_DT_ENDPOINT,
  91. .bEndpointAddress = USB_DIR_IN,
  92. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  93. .wMaxPacketSize = __constant_cpu_to_le16(512),
  94. };
  95. static struct usb_endpoint_descriptor adb_highspeed_out_desc = {
  96. .bLength = USB_DT_ENDPOINT_SIZE,
  97. .bDescriptorType = USB_DT_ENDPOINT,
  98. .bEndpointAddress = USB_DIR_OUT,
  99. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  100. .wMaxPacketSize = __constant_cpu_to_le16(512),
  101. };
  102. static struct usb_endpoint_descriptor adb_fullspeed_in_desc = {
  103. .bLength = USB_DT_ENDPOINT_SIZE,
  104. .bDescriptorType = USB_DT_ENDPOINT,
  105. .bEndpointAddress = USB_DIR_IN,
  106. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  107. };
  108. static struct usb_endpoint_descriptor adb_fullspeed_out_desc = {
  109. .bLength = USB_DT_ENDPOINT_SIZE,
  110. .bDescriptorType = USB_DT_ENDPOINT,
  111. .bEndpointAddress = USB_DIR_OUT,
  112. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  113. };
  114. static struct usb_descriptor_header *fs_adb_descs[] = {
  115. (struct usb_descriptor_header *) &adb_interface_desc,
  116. (struct usb_descriptor_header *) &adb_fullspeed_in_desc,
  117. (struct usb_descriptor_header *) &adb_fullspeed_out_desc,
  118. NULL,
  119. };
  120. static struct usb_descriptor_header *hs_adb_descs[] = {
  121. (struct usb_descriptor_header *) &adb_interface_desc,
  122. (struct usb_descriptor_header *) &adb_highspeed_in_desc,
  123. (struct usb_descriptor_header *) &adb_highspeed_out_desc,
  124. NULL,
  125. };
  126. static struct usb_descriptor_header *ss_adb_descs[] = {
  127. (struct usb_descriptor_header *) &adb_interface_desc,
  128. (struct usb_descriptor_header *) &adb_superspeed_in_desc,
  129. (struct usb_descriptor_header *) &adb_superspeed_in_comp_desc,
  130. (struct usb_descriptor_header *) &adb_superspeed_out_desc,
  131. (struct usb_descriptor_header *) &adb_superspeed_out_comp_desc,
  132. NULL,
  133. };
  134. static void adb_ready_callback(void);
  135. static void adb_closed_callback(void);
  136. /* temporary variable used between adb_open() and adb_gadget_bind() */
  137. static struct adb_dev *_adb_dev;
  138. static inline struct adb_dev *func_to_adb(struct usb_function *f)
  139. {
  140. return container_of(f, struct adb_dev, function);
  141. }
  142. static struct usb_request *adb_request_new(struct usb_ep *ep, int buffer_size)
  143. {
  144. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  145. if (!req)
  146. return NULL;
  147. /* now allocate buffers for the requests */
  148. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  149. if (!req->buf) {
  150. usb_ep_free_request(ep, req);
  151. return NULL;
  152. }
  153. return req;
  154. }
  155. static void adb_request_free(struct usb_request *req, struct usb_ep *ep)
  156. {
  157. if (req) {
  158. kfree(req->buf);
  159. usb_ep_free_request(ep, req);
  160. }
  161. }
  162. static inline int adb_lock(atomic_t *excl)
  163. {
  164. if (atomic_inc_return(excl) == 1) {
  165. return 0;
  166. } else {
  167. atomic_dec(excl);
  168. return -1;
  169. }
  170. }
  171. static inline void adb_unlock(atomic_t *excl)
  172. {
  173. atomic_dec(excl);
  174. }
  175. /* add a request to the tail of a list */
  176. void adb_req_put(struct adb_dev *dev, struct list_head *head,
  177. struct usb_request *req)
  178. {
  179. unsigned long flags;
  180. spin_lock_irqsave(&dev->lock, flags);
  181. list_add_tail(&req->list, head);
  182. spin_unlock_irqrestore(&dev->lock, flags);
  183. }
  184. /* remove a request from the head of a list */
  185. struct usb_request *adb_req_get(struct adb_dev *dev, struct list_head *head)
  186. {
  187. unsigned long flags;
  188. struct usb_request *req;
  189. spin_lock_irqsave(&dev->lock, flags);
  190. if (list_empty(head)) {
  191. req = 0;
  192. } else {
  193. req = list_first_entry(head, struct usb_request, list);
  194. list_del(&req->list);
  195. }
  196. spin_unlock_irqrestore(&dev->lock, flags);
  197. return req;
  198. }
  199. static void adb_complete_in(struct usb_ep *ep, struct usb_request *req)
  200. {
  201. struct adb_dev *dev = _adb_dev;
  202. if (req->status != 0)
  203. atomic_set(&dev->error, 1);
  204. adb_req_put(dev, &dev->tx_idle, req);
  205. wake_up(&dev->write_wq);
  206. }
  207. static void adb_complete_out(struct usb_ep *ep, struct usb_request *req)
  208. {
  209. struct adb_dev *dev = _adb_dev;
  210. dev->rx_done = 1;
  211. if (req->status != 0 && req->status != -ECONNRESET)
  212. atomic_set(&dev->error, 1);
  213. wake_up(&dev->read_wq);
  214. }
  215. static int adb_create_bulk_endpoints(struct adb_dev *dev,
  216. struct usb_endpoint_descriptor *in_desc,
  217. struct usb_endpoint_descriptor *out_desc)
  218. {
  219. struct usb_composite_dev *cdev = dev->cdev;
  220. struct usb_request *req;
  221. struct usb_ep *ep;
  222. int i;
  223. DBG(cdev, "create_bulk_endpoints dev: %pK\n", dev);
  224. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  225. if (!ep) {
  226. DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
  227. return -ENODEV;
  228. }
  229. DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
  230. ep->driver_data = dev; /* claim the endpoint */
  231. dev->ep_in = ep;
  232. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  233. if (!ep) {
  234. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  235. return -ENODEV;
  236. }
  237. DBG(cdev, "usb_ep_autoconfig for adb ep_out got %s\n", ep->name);
  238. ep->driver_data = dev; /* claim the endpoint */
  239. dev->ep_out = ep;
  240. /* now allocate requests for our endpoints */
  241. req = adb_request_new(dev->ep_out, ADB_BULK_BUFFER_SIZE);
  242. if (!req)
  243. goto fail;
  244. req->complete = adb_complete_out;
  245. dev->rx_req = req;
  246. for (i = 0; i < TX_REQ_MAX; i++) {
  247. req = adb_request_new(dev->ep_in, ADB_BULK_BUFFER_SIZE);
  248. if (!req)
  249. goto fail;
  250. req->complete = adb_complete_in;
  251. adb_req_put(dev, &dev->tx_idle, req);
  252. }
  253. return 0;
  254. fail:
  255. printk(KERN_ERR "adb_bind() could not allocate requests\n");
  256. return -1;
  257. }
  258. static ssize_t adb_read(struct file *fp, char __user *buf,
  259. size_t count, loff_t *pos)
  260. {
  261. struct adb_dev *dev = fp->private_data;
  262. struct usb_request *req;
  263. int r = count, xfer;
  264. int ret;
  265. pr_debug("adb_read(%d)\n", count);
  266. if (!_adb_dev)
  267. return -ENODEV;
  268. if (count > ADB_BULK_BUFFER_SIZE)
  269. return -EINVAL;
  270. if (adb_lock(&dev->read_excl))
  271. return -EBUSY;
  272. /* we will block until we're online */
  273. while (!(atomic_read(&dev->online) || atomic_read(&dev->error))) {
  274. pr_debug("adb_read: waiting for online state\n");
  275. ret = wait_event_interruptible(dev->read_wq,
  276. (atomic_read(&dev->online) ||
  277. atomic_read(&dev->error)));
  278. if (ret < 0) {
  279. adb_unlock(&dev->read_excl);
  280. return ret;
  281. }
  282. }
  283. if (atomic_read(&dev->error)) {
  284. r = -EIO;
  285. goto done;
  286. }
  287. requeue_req:
  288. /* queue a request */
  289. req = dev->rx_req;
  290. req->length = ADB_BULK_BUFFER_SIZE;
  291. dev->rx_done = 0;
  292. ret = usb_ep_queue(dev->ep_out, req, GFP_ATOMIC);
  293. if (ret < 0) {
  294. pr_debug("adb_read: failed to queue req %pK (%d)\n", req, ret);
  295. r = -EIO;
  296. atomic_set(&dev->error, 1);
  297. goto done;
  298. } else {
  299. pr_debug("rx %pK queue\n", req);
  300. }
  301. /* wait for a request to complete */
  302. ret = wait_event_interruptible(dev->read_wq, dev->rx_done ||
  303. atomic_read(&dev->error));
  304. if (ret < 0) {
  305. if (ret != -ERESTARTSYS)
  306. atomic_set(&dev->error, 1);
  307. r = ret;
  308. usb_ep_dequeue(dev->ep_out, req);
  309. goto done;
  310. }
  311. if (!atomic_read(&dev->error)) {
  312. /* If we got a 0-len packet, throw it back and try again. */
  313. if (req->actual == 0)
  314. goto requeue_req;
  315. pr_debug("rx %pK %d\n", req, req->actual);
  316. xfer = (req->actual < count) ? req->actual : count;
  317. if (copy_to_user(buf, req->buf, xfer))
  318. r = -EFAULT;
  319. } else
  320. r = -EIO;
  321. done:
  322. if (atomic_read(&dev->error))
  323. wake_up(&dev->write_wq);
  324. adb_unlock(&dev->read_excl);
  325. pr_debug("adb_read returning %d\n", r);
  326. return r;
  327. }
  328. static ssize_t adb_write(struct file *fp, const char __user *buf,
  329. size_t count, loff_t *pos)
  330. {
  331. struct adb_dev *dev = fp->private_data;
  332. struct usb_request *req = 0;
  333. int r = count, xfer;
  334. int ret;
  335. if (!_adb_dev)
  336. return -ENODEV;
  337. pr_debug("adb_write(%d)\n", count);
  338. if (adb_lock(&dev->write_excl))
  339. return -EBUSY;
  340. while (count > 0) {
  341. if (atomic_read(&dev->error)) {
  342. pr_debug("adb_write dev->error\n");
  343. r = -EIO;
  344. break;
  345. }
  346. /* get an idle tx request to use */
  347. req = 0;
  348. ret = wait_event_interruptible(dev->write_wq,
  349. ((req = adb_req_get(dev, &dev->tx_idle)) ||
  350. atomic_read(&dev->error)));
  351. if (ret < 0) {
  352. r = ret;
  353. break;
  354. }
  355. if (req != 0) {
  356. if (count > ADB_BULK_BUFFER_SIZE)
  357. xfer = ADB_BULK_BUFFER_SIZE;
  358. else
  359. xfer = count;
  360. if (copy_from_user(req->buf, buf, xfer)) {
  361. r = -EFAULT;
  362. break;
  363. }
  364. req->length = xfer;
  365. ret = usb_ep_queue(dev->ep_in, req, GFP_ATOMIC);
  366. if (ret < 0) {
  367. pr_debug("adb_write: xfer error %d\n", ret);
  368. atomic_set(&dev->error, 1);
  369. r = -EIO;
  370. break;
  371. }
  372. buf += xfer;
  373. count -= xfer;
  374. /* zero this so we don't try to free it on error exit */
  375. req = 0;
  376. }
  377. }
  378. if (req)
  379. adb_req_put(dev, &dev->tx_idle, req);
  380. if (atomic_read(&dev->error))
  381. wake_up(&dev->read_wq);
  382. adb_unlock(&dev->write_excl);
  383. pr_debug("adb_write returning %d\n", r);
  384. return r;
  385. }
  386. static int adb_open(struct inode *ip, struct file *fp)
  387. {
  388. static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
  389. if (__ratelimit(&rl))
  390. pr_info("adb_open\n");
  391. if (!_adb_dev)
  392. return -ENODEV;
  393. if (adb_lock(&_adb_dev->open_excl))
  394. return -EBUSY;
  395. fp->private_data = _adb_dev;
  396. /* clear the error latch */
  397. atomic_set(&_adb_dev->error, 0);
  398. if (_adb_dev->close_notified) {
  399. _adb_dev->close_notified = false;
  400. adb_ready_callback();
  401. }
  402. _adb_dev->notify_close = true;
  403. return 0;
  404. }
  405. static int adb_release(struct inode *ip, struct file *fp)
  406. {
  407. static DEFINE_RATELIMIT_STATE(rl, 10*HZ, 1);
  408. if (__ratelimit(&rl))
  409. pr_info("adb_release\n");
  410. /*
  411. * ADB daemon closes the device file after I/O error. The
  412. * I/O error happen when Rx requests are flushed during
  413. * cable disconnect or bus reset in configured state. Disabling
  414. * USB configuration and pull-up during these scenarios are
  415. * undesired. We want to force bus reset only for certain
  416. * commands like "adb root" and "adb usb".
  417. */
  418. if (_adb_dev->notify_close) {
  419. adb_closed_callback();
  420. _adb_dev->close_notified = true;
  421. }
  422. adb_unlock(&_adb_dev->open_excl);
  423. return 0;
  424. }
  425. /* file operations for ADB device /dev/android_adb */
  426. static const struct file_operations adb_fops = {
  427. .owner = THIS_MODULE,
  428. .read = adb_read,
  429. .write = adb_write,
  430. .open = adb_open,
  431. .release = adb_release,
  432. };
  433. static struct miscdevice adb_device = {
  434. .minor = MISC_DYNAMIC_MINOR,
  435. .name = adb_shortname,
  436. .fops = &adb_fops,
  437. };
  438. static int
  439. adb_function_bind(struct usb_configuration *c, struct usb_function *f)
  440. {
  441. struct usb_composite_dev *cdev = c->cdev;
  442. struct adb_dev *dev = func_to_adb(f);
  443. int id;
  444. int ret;
  445. dev->cdev = cdev;
  446. DBG(cdev, "adb_function_bind dev: %pK\n", dev);
  447. /* allocate interface ID(s) */
  448. id = usb_interface_id(c, f);
  449. if (id < 0)
  450. return id;
  451. adb_interface_desc.bInterfaceNumber = id;
  452. /* allocate endpoints */
  453. ret = adb_create_bulk_endpoints(dev, &adb_fullspeed_in_desc,
  454. &adb_fullspeed_out_desc);
  455. if (ret)
  456. return ret;
  457. /* support high speed hardware */
  458. if (gadget_is_dualspeed(c->cdev->gadget)) {
  459. adb_highspeed_in_desc.bEndpointAddress =
  460. adb_fullspeed_in_desc.bEndpointAddress;
  461. adb_highspeed_out_desc.bEndpointAddress =
  462. adb_fullspeed_out_desc.bEndpointAddress;
  463. }
  464. /* support super speed hardware */
  465. if (gadget_is_superspeed(c->cdev->gadget)) {
  466. adb_superspeed_in_desc.bEndpointAddress =
  467. adb_fullspeed_in_desc.bEndpointAddress;
  468. adb_superspeed_out_desc.bEndpointAddress =
  469. adb_fullspeed_out_desc.bEndpointAddress;
  470. }
  471. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  472. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  473. f->name, dev->ep_in->name, dev->ep_out->name);
  474. return 0;
  475. }
  476. static void
  477. adb_function_unbind(struct usb_configuration *c, struct usb_function *f)
  478. {
  479. struct adb_dev *dev = func_to_adb(f);
  480. struct usb_request *req;
  481. atomic_set(&dev->online, 0);
  482. atomic_set(&dev->error, 1);
  483. wake_up(&dev->read_wq);
  484. adb_request_free(dev->rx_req, dev->ep_out);
  485. while ((req = adb_req_get(dev, &dev->tx_idle)))
  486. adb_request_free(req, dev->ep_in);
  487. }
  488. static int adb_function_set_alt(struct usb_function *f,
  489. unsigned intf, unsigned alt)
  490. {
  491. struct adb_dev *dev = func_to_adb(f);
  492. struct usb_composite_dev *cdev = f->config->cdev;
  493. int ret;
  494. DBG(cdev, "adb_function_set_alt intf: %d alt: %d\n", intf, alt);
  495. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
  496. if (ret) {
  497. dev->ep_in->desc = NULL;
  498. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  499. dev->ep_in->name, ret);
  500. return ret;
  501. }
  502. ret = usb_ep_enable(dev->ep_in);
  503. if (ret) {
  504. ERROR(cdev, "failed to enable ep %s, result %d\n",
  505. dev->ep_in->name, ret);
  506. return ret;
  507. }
  508. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
  509. if (ret) {
  510. dev->ep_out->desc = NULL;
  511. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  512. dev->ep_out->name, ret);
  513. usb_ep_disable(dev->ep_in);
  514. return ret;
  515. }
  516. ret = usb_ep_enable(dev->ep_out);
  517. if (ret) {
  518. ERROR(cdev, "failed to enable ep %s, result %d\n",
  519. dev->ep_out->name, ret);
  520. usb_ep_disable(dev->ep_in);
  521. return ret;
  522. }
  523. atomic_set(&dev->online, 1);
  524. /* readers may be blocked waiting for us to go online */
  525. wake_up(&dev->read_wq);
  526. return 0;
  527. }
  528. static void adb_function_disable(struct usb_function *f)
  529. {
  530. struct adb_dev *dev = func_to_adb(f);
  531. struct usb_composite_dev *cdev = dev->cdev;
  532. DBG(cdev, "adb_function_disable cdev %pK\n", cdev);
  533. /*
  534. * Bus reset happened or cable disconnected. No
  535. * need to disable the configuration now. We will
  536. * set noify_close to true when device file is re-opened.
  537. */
  538. dev->notify_close = false;
  539. atomic_set(&dev->online, 0);
  540. atomic_set(&dev->error, 1);
  541. usb_ep_disable(dev->ep_in);
  542. usb_ep_disable(dev->ep_out);
  543. /* readers may be blocked waiting for us to go online */
  544. wake_up(&dev->read_wq);
  545. VDBG(cdev, "%s disabled\n", dev->function.name);
  546. }
  547. static int adb_bind_config(struct usb_configuration *c)
  548. {
  549. struct adb_dev *dev = _adb_dev;
  550. printk(KERN_INFO "adb_bind_config\n");
  551. dev->cdev = c->cdev;
  552. dev->function.name = "adb";
  553. dev->function.fs_descriptors = fs_adb_descs;
  554. dev->function.hs_descriptors = hs_adb_descs;
  555. if (gadget_is_superspeed(c->cdev->gadget))
  556. dev->function.ss_descriptors = ss_adb_descs;
  557. dev->function.bind = adb_function_bind;
  558. dev->function.unbind = adb_function_unbind;
  559. dev->function.set_alt = adb_function_set_alt;
  560. dev->function.disable = adb_function_disable;
  561. return usb_add_function(c, &dev->function);
  562. }
  563. static int adb_setup(void)
  564. {
  565. struct adb_dev *dev;
  566. int ret;
  567. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  568. if (!dev)
  569. return -ENOMEM;
  570. spin_lock_init(&dev->lock);
  571. init_waitqueue_head(&dev->read_wq);
  572. init_waitqueue_head(&dev->write_wq);
  573. atomic_set(&dev->open_excl, 0);
  574. atomic_set(&dev->read_excl, 0);
  575. atomic_set(&dev->write_excl, 0);
  576. /* config is disabled by default if adb is present. */
  577. dev->close_notified = true;
  578. INIT_LIST_HEAD(&dev->tx_idle);
  579. _adb_dev = dev;
  580. ret = misc_register(&adb_device);
  581. if (ret)
  582. goto err;
  583. return 0;
  584. err:
  585. kfree(dev);
  586. printk(KERN_ERR "adb gadget driver failed to initialize\n");
  587. return ret;
  588. }
  589. static void adb_cleanup(void)
  590. {
  591. misc_deregister(&adb_device);
  592. kfree(_adb_dev);
  593. _adb_dev = NULL;
  594. }