f_accessory.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. * Gadget Function Driver for Android USB accessories
  3. *
  4. * Copyright (C) 2011 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. /* #define DEBUG */
  18. /* #define VERBOSE_DEBUG */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/poll.h>
  22. #include <linux/delay.h>
  23. #include <linux/wait.h>
  24. #include <linux/err.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/kthread.h>
  27. #include <linux/freezer.h>
  28. #include <linux/types.h>
  29. #include <linux/file.h>
  30. #include <linux/device.h>
  31. #include <linux/miscdevice.h>
  32. #include <linux/usb.h>
  33. #include <linux/usb/ch9.h>
  34. #include <linux/usb/f_accessory.h>
  35. #define BULK_BUFFER_SIZE 16384
  36. #define ACC_STRING_SIZE 256
  37. #define PROTOCOL_VERSION 1
  38. /* String IDs */
  39. #define INTERFACE_STRING_INDEX 0
  40. /* number of tx and rx requests to allocate */
  41. #define TX_REQ_MAX 4
  42. #define RX_REQ_MAX 2
  43. struct acc_dev {
  44. struct usb_function function;
  45. struct usb_composite_dev *cdev;
  46. spinlock_t lock;
  47. struct usb_ep *ep_in;
  48. struct usb_ep *ep_out;
  49. /* set to 1 when we connect */
  50. int online:1;
  51. /* Set to 1 when we disconnect.
  52. * Not cleared until our file is closed.
  53. */
  54. int disconnected:1;
  55. /* strings sent by the host */
  56. char manufacturer[ACC_STRING_SIZE];
  57. char model[ACC_STRING_SIZE];
  58. char description[ACC_STRING_SIZE];
  59. char version[ACC_STRING_SIZE];
  60. char uri[ACC_STRING_SIZE];
  61. char serial[ACC_STRING_SIZE];
  62. /* for acc_complete_set_string */
  63. int string_index;
  64. /* set to 1 if we have a pending start request */
  65. int start_requested;
  66. /* synchronize access to our device file */
  67. atomic_t open_excl;
  68. struct list_head tx_idle;
  69. wait_queue_head_t read_wq;
  70. wait_queue_head_t write_wq;
  71. struct usb_request *rx_req[RX_REQ_MAX];
  72. int rx_done;
  73. struct delayed_work work;
  74. };
  75. static struct usb_interface_descriptor acc_interface_desc = {
  76. .bLength = USB_DT_INTERFACE_SIZE,
  77. .bDescriptorType = USB_DT_INTERFACE,
  78. .bInterfaceNumber = 0,
  79. .bNumEndpoints = 2,
  80. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  81. .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
  82. .bInterfaceProtocol = 0,
  83. };
  84. static struct usb_endpoint_descriptor acc_highspeed_in_desc = {
  85. .bLength = USB_DT_ENDPOINT_SIZE,
  86. .bDescriptorType = USB_DT_ENDPOINT,
  87. .bEndpointAddress = USB_DIR_IN,
  88. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  89. .wMaxPacketSize = __constant_cpu_to_le16(512),
  90. };
  91. static struct usb_endpoint_descriptor acc_highspeed_out_desc = {
  92. .bLength = USB_DT_ENDPOINT_SIZE,
  93. .bDescriptorType = USB_DT_ENDPOINT,
  94. .bEndpointAddress = USB_DIR_OUT,
  95. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  96. .wMaxPacketSize = __constant_cpu_to_le16(512),
  97. };
  98. static struct usb_endpoint_descriptor acc_fullspeed_in_desc = {
  99. .bLength = USB_DT_ENDPOINT_SIZE,
  100. .bDescriptorType = USB_DT_ENDPOINT,
  101. .bEndpointAddress = USB_DIR_IN,
  102. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  103. };
  104. static struct usb_endpoint_descriptor acc_fullspeed_out_desc = {
  105. .bLength = USB_DT_ENDPOINT_SIZE,
  106. .bDescriptorType = USB_DT_ENDPOINT,
  107. .bEndpointAddress = USB_DIR_OUT,
  108. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  109. };
  110. static struct usb_descriptor_header *fs_acc_descs[] = {
  111. (struct usb_descriptor_header *) &acc_interface_desc,
  112. (struct usb_descriptor_header *) &acc_fullspeed_in_desc,
  113. (struct usb_descriptor_header *) &acc_fullspeed_out_desc,
  114. NULL,
  115. };
  116. static struct usb_descriptor_header *hs_acc_descs[] = {
  117. (struct usb_descriptor_header *) &acc_interface_desc,
  118. (struct usb_descriptor_header *) &acc_highspeed_in_desc,
  119. (struct usb_descriptor_header *) &acc_highspeed_out_desc,
  120. NULL,
  121. };
  122. static struct usb_string acc_string_defs[] = {
  123. [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
  124. { }, /* end of list */
  125. };
  126. static struct usb_gadget_strings acc_string_table = {
  127. .language = 0x0409, /* en-US */
  128. .strings = acc_string_defs,
  129. };
  130. static struct usb_gadget_strings *acc_strings[] = {
  131. &acc_string_table,
  132. NULL,
  133. };
  134. /* temporary variable used between acc_open() and acc_gadget_bind() */
  135. static struct acc_dev *_acc_dev;
  136. static inline struct acc_dev *func_to_dev(struct usb_function *f)
  137. {
  138. return container_of(f, struct acc_dev, function);
  139. }
  140. static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
  141. {
  142. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  143. if (!req)
  144. return NULL;
  145. /* now allocate buffers for the requests */
  146. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  147. if (!req->buf) {
  148. usb_ep_free_request(ep, req);
  149. return NULL;
  150. }
  151. return req;
  152. }
  153. static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
  154. {
  155. if (req) {
  156. kfree(req->buf);
  157. usb_ep_free_request(ep, req);
  158. }
  159. }
  160. /* add a request to the tail of a list */
  161. static void req_put(struct acc_dev *dev, struct list_head *head,
  162. struct usb_request *req)
  163. {
  164. unsigned long flags;
  165. spin_lock_irqsave(&dev->lock, flags);
  166. list_add_tail(&req->list, head);
  167. spin_unlock_irqrestore(&dev->lock, flags);
  168. }
  169. /* remove a request from the head of a list */
  170. static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
  171. {
  172. unsigned long flags;
  173. struct usb_request *req;
  174. spin_lock_irqsave(&dev->lock, flags);
  175. if (list_empty(head)) {
  176. req = 0;
  177. } else {
  178. req = list_first_entry(head, struct usb_request, list);
  179. list_del(&req->list);
  180. }
  181. spin_unlock_irqrestore(&dev->lock, flags);
  182. return req;
  183. }
  184. static void acc_set_disconnected(struct acc_dev *dev)
  185. {
  186. dev->online = 0;
  187. dev->disconnected = 1;
  188. }
  189. static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
  190. {
  191. struct acc_dev *dev = _acc_dev;
  192. if (req->status != 0)
  193. acc_set_disconnected(dev);
  194. req_put(dev, &dev->tx_idle, req);
  195. wake_up(&dev->write_wq);
  196. }
  197. static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
  198. {
  199. struct acc_dev *dev = _acc_dev;
  200. dev->rx_done = 1;
  201. if (req->status != 0)
  202. acc_set_disconnected(dev);
  203. wake_up(&dev->read_wq);
  204. }
  205. static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
  206. {
  207. struct acc_dev *dev = ep->driver_data;
  208. char *string_dest = NULL;
  209. int length = req->actual;
  210. if (req->status != 0) {
  211. pr_err("acc_complete_set_string, err %d\n", req->status);
  212. return;
  213. }
  214. switch (dev->string_index) {
  215. case ACCESSORY_STRING_MANUFACTURER:
  216. string_dest = dev->manufacturer;
  217. break;
  218. case ACCESSORY_STRING_MODEL:
  219. string_dest = dev->model;
  220. break;
  221. case ACCESSORY_STRING_DESCRIPTION:
  222. string_dest = dev->description;
  223. break;
  224. case ACCESSORY_STRING_VERSION:
  225. string_dest = dev->version;
  226. break;
  227. case ACCESSORY_STRING_URI:
  228. string_dest = dev->uri;
  229. break;
  230. case ACCESSORY_STRING_SERIAL:
  231. string_dest = dev->serial;
  232. break;
  233. }
  234. if (string_dest) {
  235. unsigned long flags;
  236. if (length >= ACC_STRING_SIZE)
  237. length = ACC_STRING_SIZE - 1;
  238. spin_lock_irqsave(&dev->lock, flags);
  239. memcpy(string_dest, req->buf, length);
  240. /* ensure zero termination */
  241. string_dest[length] = 0;
  242. spin_unlock_irqrestore(&dev->lock, flags);
  243. } else {
  244. pr_err("unknown accessory string index %d\n",
  245. dev->string_index);
  246. }
  247. }
  248. static int __init create_bulk_endpoints(struct acc_dev *dev,
  249. struct usb_endpoint_descriptor *in_desc,
  250. struct usb_endpoint_descriptor *out_desc)
  251. {
  252. struct usb_composite_dev *cdev = dev->cdev;
  253. struct usb_request *req;
  254. struct usb_ep *ep;
  255. int i;
  256. DBG(cdev, "create_bulk_endpoints dev: %p\n", dev);
  257. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  258. if (!ep) {
  259. DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
  260. return -ENODEV;
  261. }
  262. DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
  263. ep->driver_data = dev; /* claim the endpoint */
  264. dev->ep_in = ep;
  265. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  266. if (!ep) {
  267. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  268. return -ENODEV;
  269. }
  270. DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
  271. ep->driver_data = dev; /* claim the endpoint */
  272. dev->ep_out = ep;
  273. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  274. if (!ep) {
  275. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  276. return -ENODEV;
  277. }
  278. DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
  279. ep->driver_data = dev; /* claim the endpoint */
  280. dev->ep_out = ep;
  281. /* now allocate requests for our endpoints */
  282. for (i = 0; i < TX_REQ_MAX; i++) {
  283. req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
  284. if (!req)
  285. goto fail;
  286. req->complete = acc_complete_in;
  287. req_put(dev, &dev->tx_idle, req);
  288. }
  289. for (i = 0; i < RX_REQ_MAX; i++) {
  290. req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
  291. if (!req)
  292. goto fail;
  293. req->complete = acc_complete_out;
  294. dev->rx_req[i] = req;
  295. }
  296. return 0;
  297. fail:
  298. printk(KERN_ERR "acc_bind() could not allocate requests\n");
  299. while ((req = req_get(dev, &dev->tx_idle)))
  300. acc_request_free(req, dev->ep_in);
  301. for (i = 0; i < RX_REQ_MAX; i++)
  302. acc_request_free(dev->rx_req[i], dev->ep_out);
  303. return -1;
  304. }
  305. static ssize_t acc_read(struct file *fp, char __user *buf,
  306. size_t count, loff_t *pos)
  307. {
  308. struct acc_dev *dev = fp->private_data;
  309. struct usb_request *req;
  310. int r = count, xfer;
  311. int ret = 0;
  312. pr_debug("acc_read(%d)\n", count);
  313. if (dev->disconnected)
  314. return -ENODEV;
  315. if (count > BULK_BUFFER_SIZE)
  316. count = BULK_BUFFER_SIZE;
  317. /* we will block until we're online */
  318. pr_debug("acc_read: waiting for online\n");
  319. ret = wait_event_interruptible(dev->read_wq, dev->online);
  320. if (ret < 0) {
  321. r = ret;
  322. goto done;
  323. }
  324. requeue_req:
  325. /* queue a request */
  326. req = dev->rx_req[0];
  327. req->length = count;
  328. dev->rx_done = 0;
  329. ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
  330. if (ret < 0) {
  331. r = -EIO;
  332. goto done;
  333. } else {
  334. pr_debug("rx %p queue\n", req);
  335. }
  336. /* wait for a request to complete */
  337. ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
  338. if (ret < 0) {
  339. r = ret;
  340. usb_ep_dequeue(dev->ep_out, req);
  341. goto done;
  342. }
  343. if (dev->online) {
  344. /* If we got a 0-len packet, throw it back and try again. */
  345. if (req->actual == 0)
  346. goto requeue_req;
  347. pr_debug("rx %p %d\n", req, req->actual);
  348. xfer = (req->actual < count) ? req->actual : count;
  349. r = xfer;
  350. if (copy_to_user(buf, req->buf, xfer))
  351. r = -EFAULT;
  352. } else
  353. r = -EIO;
  354. done:
  355. pr_debug("acc_read returning %d\n", r);
  356. return r;
  357. }
  358. static ssize_t acc_write(struct file *fp, const char __user *buf,
  359. size_t count, loff_t *pos)
  360. {
  361. struct acc_dev *dev = fp->private_data;
  362. struct usb_request *req = 0;
  363. int r = count, xfer;
  364. int ret;
  365. pr_debug("acc_write(%d)\n", count);
  366. if (!dev->online || dev->disconnected)
  367. return -ENODEV;
  368. while (count > 0) {
  369. if (!dev->online) {
  370. pr_debug("acc_write dev->error\n");
  371. r = -EIO;
  372. break;
  373. }
  374. /* get an idle tx request to use */
  375. req = 0;
  376. ret = wait_event_interruptible(dev->write_wq,
  377. ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
  378. if (!req) {
  379. r = ret;
  380. break;
  381. }
  382. if (count > BULK_BUFFER_SIZE)
  383. xfer = BULK_BUFFER_SIZE;
  384. else
  385. xfer = count;
  386. if (copy_from_user(req->buf, buf, xfer)) {
  387. r = -EFAULT;
  388. break;
  389. }
  390. req->length = xfer;
  391. ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
  392. if (ret < 0) {
  393. pr_debug("acc_write: xfer error %d\n", ret);
  394. r = -EIO;
  395. break;
  396. }
  397. buf += xfer;
  398. count -= xfer;
  399. /* zero this so we don't try to free it on error exit */
  400. req = 0;
  401. }
  402. if (req)
  403. req_put(dev, &dev->tx_idle, req);
  404. pr_debug("acc_write returning %d\n", r);
  405. return r;
  406. }
  407. static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
  408. {
  409. struct acc_dev *dev = fp->private_data;
  410. char *src = NULL;
  411. int ret;
  412. switch (code) {
  413. case ACCESSORY_GET_STRING_MANUFACTURER:
  414. src = dev->manufacturer;
  415. break;
  416. case ACCESSORY_GET_STRING_MODEL:
  417. src = dev->model;
  418. break;
  419. case ACCESSORY_GET_STRING_DESCRIPTION:
  420. src = dev->description;
  421. break;
  422. case ACCESSORY_GET_STRING_VERSION:
  423. src = dev->version;
  424. break;
  425. case ACCESSORY_GET_STRING_URI:
  426. src = dev->uri;
  427. break;
  428. case ACCESSORY_GET_STRING_SERIAL:
  429. src = dev->serial;
  430. break;
  431. case ACCESSORY_IS_START_REQUESTED:
  432. return dev->start_requested;
  433. }
  434. if (!src)
  435. return -EINVAL;
  436. ret = strlen(src) + 1;
  437. if (copy_to_user((void __user *)value, src, ret))
  438. ret = -EFAULT;
  439. return ret;
  440. }
  441. static int acc_open(struct inode *ip, struct file *fp)
  442. {
  443. printk(KERN_INFO "acc_open\n");
  444. if (atomic_xchg(&_acc_dev->open_excl, 1))
  445. return -EBUSY;
  446. _acc_dev->disconnected = 0;
  447. fp->private_data = _acc_dev;
  448. return 0;
  449. }
  450. static int acc_release(struct inode *ip, struct file *fp)
  451. {
  452. printk(KERN_INFO "acc_release\n");
  453. WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
  454. _acc_dev->disconnected = 0;
  455. return 0;
  456. }
  457. /* file operations for /dev/acc_usb */
  458. static const struct file_operations acc_fops = {
  459. .owner = THIS_MODULE,
  460. .read = acc_read,
  461. .write = acc_write,
  462. .unlocked_ioctl = acc_ioctl,
  463. .open = acc_open,
  464. .release = acc_release,
  465. };
  466. static struct miscdevice acc_device = {
  467. .minor = MISC_DYNAMIC_MINOR,
  468. .name = "usb_accessory",
  469. .fops = &acc_fops,
  470. };
  471. static int acc_ctrlrequest(struct usb_composite_dev *cdev,
  472. const struct usb_ctrlrequest *ctrl)
  473. {
  474. struct acc_dev *dev = _acc_dev;
  475. int value = -EOPNOTSUPP;
  476. u8 b_requestType = ctrl->bRequestType;
  477. u8 b_request = ctrl->bRequest;
  478. u16 w_index = le16_to_cpu(ctrl->wIndex);
  479. u16 w_value = le16_to_cpu(ctrl->wValue);
  480. u16 w_length = le16_to_cpu(ctrl->wLength);
  481. /*
  482. printk(KERN_INFO "acc_ctrlrequest "
  483. "%02x.%02x v%04x i%04x l%u\n",
  484. b_requestType, b_request,
  485. w_value, w_index, w_length);
  486. */
  487. if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
  488. if (b_request == ACCESSORY_START) {
  489. dev->start_requested = 1;
  490. schedule_delayed_work(
  491. &dev->work, msecs_to_jiffies(10));
  492. value = 0;
  493. } else if (b_request == ACCESSORY_SEND_STRING) {
  494. dev->string_index = w_index;
  495. cdev->gadget->ep0->driver_data = dev;
  496. cdev->req->complete = acc_complete_set_string;
  497. value = w_length;
  498. }
  499. } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
  500. if (b_request == ACCESSORY_GET_PROTOCOL) {
  501. *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
  502. value = sizeof(u16);
  503. /* clear any strings left over from a previous session */
  504. memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
  505. memset(dev->model, 0, sizeof(dev->model));
  506. memset(dev->description, 0, sizeof(dev->description));
  507. memset(dev->version, 0, sizeof(dev->version));
  508. memset(dev->uri, 0, sizeof(dev->uri));
  509. memset(dev->serial, 0, sizeof(dev->serial));
  510. dev->start_requested = 0;
  511. }
  512. }
  513. if (value >= 0) {
  514. cdev->req->zero = 0;
  515. cdev->req->length = value;
  516. value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  517. if (value < 0)
  518. ERROR(cdev, "%s setup response queue error\n",
  519. __func__);
  520. }
  521. if (value == -EOPNOTSUPP)
  522. VDBG(cdev,
  523. "unknown class-specific control req "
  524. "%02x.%02x v%04x i%04x l%u\n",
  525. ctrl->bRequestType, ctrl->bRequest,
  526. w_value, w_index, w_length);
  527. return value;
  528. }
  529. static int
  530. acc_function_bind(struct usb_configuration *c, struct usb_function *f)
  531. {
  532. struct usb_composite_dev *cdev = c->cdev;
  533. struct acc_dev *dev = func_to_dev(f);
  534. int id;
  535. int ret;
  536. DBG(cdev, "acc_function_bind dev: %p\n", dev);
  537. dev->start_requested = 0;
  538. /* allocate interface ID(s) */
  539. id = usb_interface_id(c, f);
  540. if (id < 0)
  541. return id;
  542. acc_interface_desc.bInterfaceNumber = id;
  543. /* allocate endpoints */
  544. ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
  545. &acc_fullspeed_out_desc);
  546. if (ret)
  547. return ret;
  548. /* support high speed hardware */
  549. if (gadget_is_dualspeed(c->cdev->gadget)) {
  550. acc_highspeed_in_desc.bEndpointAddress =
  551. acc_fullspeed_in_desc.bEndpointAddress;
  552. acc_highspeed_out_desc.bEndpointAddress =
  553. acc_fullspeed_out_desc.bEndpointAddress;
  554. }
  555. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  556. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  557. f->name, dev->ep_in->name, dev->ep_out->name);
  558. return 0;
  559. }
  560. static void
  561. acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
  562. {
  563. struct acc_dev *dev = func_to_dev(f);
  564. struct usb_request *req;
  565. int i;
  566. while ((req = req_get(dev, &dev->tx_idle)))
  567. acc_request_free(req, dev->ep_in);
  568. for (i = 0; i < RX_REQ_MAX; i++)
  569. acc_request_free(dev->rx_req[i], dev->ep_out);
  570. }
  571. static void acc_work(struct work_struct *data)
  572. {
  573. char *envp[2] = { "ACCESSORY=START", NULL };
  574. kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
  575. }
  576. static int acc_function_set_alt(struct usb_function *f,
  577. unsigned intf, unsigned alt)
  578. {
  579. struct acc_dev *dev = func_to_dev(f);
  580. struct usb_composite_dev *cdev = f->config->cdev;
  581. int ret;
  582. DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
  583. ret = usb_ep_enable(dev->ep_in,
  584. ep_choose(cdev->gadget,
  585. &acc_highspeed_in_desc,
  586. &acc_fullspeed_in_desc));
  587. if (ret)
  588. return ret;
  589. ret = usb_ep_enable(dev->ep_out,
  590. ep_choose(cdev->gadget,
  591. &acc_highspeed_out_desc,
  592. &acc_fullspeed_out_desc));
  593. if (ret) {
  594. usb_ep_disable(dev->ep_in);
  595. return ret;
  596. }
  597. dev->online = 1;
  598. /* readers may be blocked waiting for us to go online */
  599. wake_up(&dev->read_wq);
  600. return 0;
  601. }
  602. static void acc_function_disable(struct usb_function *f)
  603. {
  604. struct acc_dev *dev = func_to_dev(f);
  605. struct usb_composite_dev *cdev = dev->cdev;
  606. DBG(cdev, "acc_function_disable\n");
  607. acc_set_disconnected(dev);
  608. usb_ep_disable(dev->ep_in);
  609. usb_ep_disable(dev->ep_out);
  610. /* readers may be blocked waiting for us to go online */
  611. wake_up(&dev->read_wq);
  612. VDBG(cdev, "%s disabled\n", dev->function.name);
  613. }
  614. static int acc_bind_config(struct usb_configuration *c)
  615. {
  616. struct acc_dev *dev = _acc_dev;
  617. int ret;
  618. printk(KERN_INFO "acc_bind_config\n");
  619. /* allocate a string ID for our interface */
  620. if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
  621. ret = usb_string_id(c->cdev);
  622. if (ret < 0)
  623. return ret;
  624. acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
  625. acc_interface_desc.iInterface = ret;
  626. }
  627. dev->cdev = c->cdev;
  628. dev->function.name = "accessory";
  629. dev->function.strings = acc_strings,
  630. dev->function.descriptors = fs_acc_descs;
  631. dev->function.hs_descriptors = hs_acc_descs;
  632. dev->function.bind = acc_function_bind;
  633. dev->function.unbind = acc_function_unbind;
  634. dev->function.set_alt = acc_function_set_alt;
  635. dev->function.disable = acc_function_disable;
  636. return usb_add_function(c, &dev->function);
  637. }
  638. static int acc_setup(void)
  639. {
  640. struct acc_dev *dev;
  641. int ret;
  642. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  643. if (!dev)
  644. return -ENOMEM;
  645. spin_lock_init(&dev->lock);
  646. init_waitqueue_head(&dev->read_wq);
  647. init_waitqueue_head(&dev->write_wq);
  648. atomic_set(&dev->open_excl, 0);
  649. INIT_LIST_HEAD(&dev->tx_idle);
  650. INIT_DELAYED_WORK(&dev->work, acc_work);
  651. /* _acc_dev must be set before calling usb_gadget_register_driver */
  652. _acc_dev = dev;
  653. ret = misc_register(&acc_device);
  654. if (ret)
  655. goto err;
  656. return 0;
  657. err:
  658. kfree(dev);
  659. printk(KERN_ERR "USB accessory gadget driver failed to initialize\n");
  660. return ret;
  661. }
  662. static void acc_cleanup(void)
  663. {
  664. misc_deregister(&acc_device);
  665. kfree(_acc_dev);
  666. _acc_dev = NULL;
  667. }