f_accessory.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  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/hid.h>
  33. #include <linux/hiddev.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/ch9.h>
  36. #include <linux/usb/f_accessory.h>
  37. #define BULK_BUFFER_SIZE 16384
  38. #define ACC_STRING_SIZE 256
  39. #define PROTOCOL_VERSION 2
  40. /* String IDs */
  41. #define INTERFACE_STRING_INDEX 0
  42. /* number of tx and rx requests to allocate */
  43. #define TX_REQ_MAX 4
  44. #define RX_REQ_MAX 2
  45. struct acc_hid_dev {
  46. struct list_head list;
  47. struct hid_device *hid;
  48. struct acc_dev *dev;
  49. /* accessory defined ID */
  50. int id;
  51. /* HID report descriptor */
  52. u8 *report_desc;
  53. /* length of HID report descriptor */
  54. int report_desc_len;
  55. /* number of bytes of report_desc we have received so far */
  56. int report_desc_offset;
  57. };
  58. struct acc_dev {
  59. struct usb_function function;
  60. struct usb_composite_dev *cdev;
  61. spinlock_t lock;
  62. struct usb_ep *ep_in;
  63. struct usb_ep *ep_out;
  64. /* set to 1 when we connect */
  65. int online:1;
  66. /* Set to 1 when we disconnect.
  67. * Not cleared until our file is closed.
  68. */
  69. int disconnected:1;
  70. /* strings sent by the host */
  71. char manufacturer[ACC_STRING_SIZE];
  72. char model[ACC_STRING_SIZE];
  73. char description[ACC_STRING_SIZE];
  74. char version[ACC_STRING_SIZE];
  75. char uri[ACC_STRING_SIZE];
  76. char serial[ACC_STRING_SIZE];
  77. /* for acc_complete_set_string */
  78. int string_index;
  79. /* set to 1 if we have a pending start request */
  80. int start_requested;
  81. int audio_mode;
  82. /* synchronize access to our device file */
  83. atomic_t open_excl;
  84. struct list_head tx_idle;
  85. wait_queue_head_t read_wq;
  86. wait_queue_head_t write_wq;
  87. struct usb_request *rx_req[RX_REQ_MAX];
  88. int rx_done;
  89. /* delayed work for handling ACCESSORY_START */
  90. struct delayed_work start_work;
  91. /* worker for registering and unregistering hid devices */
  92. struct work_struct hid_work;
  93. /* list of active HID devices */
  94. struct list_head hid_list;
  95. /* list of new HID devices to register */
  96. struct list_head new_hid_list;
  97. /* list of dead HID devices to unregister */
  98. struct list_head dead_hid_list;
  99. };
  100. static struct usb_interface_descriptor acc_interface_desc = {
  101. .bLength = USB_DT_INTERFACE_SIZE,
  102. .bDescriptorType = USB_DT_INTERFACE,
  103. .bInterfaceNumber = 0,
  104. .bNumEndpoints = 2,
  105. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  106. .bInterfaceSubClass = USB_SUBCLASS_VENDOR_SPEC,
  107. .bInterfaceProtocol = 0,
  108. };
  109. static struct usb_endpoint_descriptor acc_superspeed_in_desc = {
  110. .bLength = USB_DT_ENDPOINT_SIZE,
  111. .bDescriptorType = USB_DT_ENDPOINT,
  112. .bEndpointAddress = USB_DIR_IN,
  113. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  114. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  115. };
  116. static struct usb_ss_ep_comp_descriptor acc_superspeed_in_comp_desc = {
  117. .bLength = sizeof acc_superspeed_in_comp_desc,
  118. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  119. /* the following 2 values can be tweaked if necessary */
  120. /* .bMaxBurst = 0, */
  121. /* .bmAttributes = 0, */
  122. };
  123. static struct usb_endpoint_descriptor acc_superspeed_out_desc = {
  124. .bLength = USB_DT_ENDPOINT_SIZE,
  125. .bDescriptorType = USB_DT_ENDPOINT,
  126. .bEndpointAddress = USB_DIR_OUT,
  127. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  128. .wMaxPacketSize = __constant_cpu_to_le16(1024),
  129. };
  130. static struct usb_ss_ep_comp_descriptor acc_superspeed_out_comp_desc = {
  131. .bLength = sizeof acc_superspeed_out_comp_desc,
  132. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  133. /* the following 2 values can be tweaked if necessary */
  134. /* .bMaxBurst = 0, */
  135. /* .bmAttributes = 0, */
  136. };
  137. static struct usb_endpoint_descriptor acc_highspeed_in_desc = {
  138. .bLength = USB_DT_ENDPOINT_SIZE,
  139. .bDescriptorType = USB_DT_ENDPOINT,
  140. .bEndpointAddress = USB_DIR_IN,
  141. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  142. .wMaxPacketSize = __constant_cpu_to_le16(512),
  143. };
  144. static struct usb_endpoint_descriptor acc_highspeed_out_desc = {
  145. .bLength = USB_DT_ENDPOINT_SIZE,
  146. .bDescriptorType = USB_DT_ENDPOINT,
  147. .bEndpointAddress = USB_DIR_OUT,
  148. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  149. .wMaxPacketSize = __constant_cpu_to_le16(512),
  150. };
  151. static struct usb_endpoint_descriptor acc_fullspeed_in_desc = {
  152. .bLength = USB_DT_ENDPOINT_SIZE,
  153. .bDescriptorType = USB_DT_ENDPOINT,
  154. .bEndpointAddress = USB_DIR_IN,
  155. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  156. };
  157. static struct usb_endpoint_descriptor acc_fullspeed_out_desc = {
  158. .bLength = USB_DT_ENDPOINT_SIZE,
  159. .bDescriptorType = USB_DT_ENDPOINT,
  160. .bEndpointAddress = USB_DIR_OUT,
  161. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  162. };
  163. static struct usb_descriptor_header *fs_acc_descs[] = {
  164. (struct usb_descriptor_header *) &acc_interface_desc,
  165. (struct usb_descriptor_header *) &acc_fullspeed_in_desc,
  166. (struct usb_descriptor_header *) &acc_fullspeed_out_desc,
  167. NULL,
  168. };
  169. static struct usb_descriptor_header *hs_acc_descs[] = {
  170. (struct usb_descriptor_header *) &acc_interface_desc,
  171. (struct usb_descriptor_header *) &acc_highspeed_in_desc,
  172. (struct usb_descriptor_header *) &acc_highspeed_out_desc,
  173. NULL,
  174. };
  175. static struct usb_descriptor_header *ss_acc_descs[] = {
  176. (struct usb_descriptor_header *) &acc_interface_desc,
  177. (struct usb_descriptor_header *) &acc_superspeed_in_desc,
  178. (struct usb_descriptor_header *) &acc_superspeed_in_comp_desc,
  179. (struct usb_descriptor_header *) &acc_superspeed_out_desc,
  180. (struct usb_descriptor_header *) &acc_superspeed_out_comp_desc,
  181. NULL,
  182. };
  183. static struct usb_string acc_string_defs[] = {
  184. [INTERFACE_STRING_INDEX].s = "Android Accessory Interface",
  185. { }, /* end of list */
  186. };
  187. static struct usb_gadget_strings acc_string_table = {
  188. .language = 0x0409, /* en-US */
  189. .strings = acc_string_defs,
  190. };
  191. static struct usb_gadget_strings *acc_strings[] = {
  192. &acc_string_table,
  193. NULL,
  194. };
  195. /* temporary variable used between acc_open() and acc_gadget_bind() */
  196. static struct acc_dev *_acc_dev;
  197. static inline struct acc_dev *func_to_dev(struct usb_function *f)
  198. {
  199. return container_of(f, struct acc_dev, function);
  200. }
  201. static struct usb_request *acc_request_new(struct usb_ep *ep, int buffer_size)
  202. {
  203. struct usb_request *req = usb_ep_alloc_request(ep, GFP_KERNEL);
  204. if (!req)
  205. return NULL;
  206. /* now allocate buffers for the requests */
  207. req->buf = kmalloc(buffer_size, GFP_KERNEL);
  208. if (!req->buf) {
  209. usb_ep_free_request(ep, req);
  210. return NULL;
  211. }
  212. return req;
  213. }
  214. static void acc_request_free(struct usb_request *req, struct usb_ep *ep)
  215. {
  216. if (req) {
  217. kfree(req->buf);
  218. usb_ep_free_request(ep, req);
  219. }
  220. }
  221. /* add a request to the tail of a list */
  222. static void req_put(struct acc_dev *dev, struct list_head *head,
  223. struct usb_request *req)
  224. {
  225. unsigned long flags;
  226. spin_lock_irqsave(&dev->lock, flags);
  227. list_add_tail(&req->list, head);
  228. spin_unlock_irqrestore(&dev->lock, flags);
  229. }
  230. /* remove a request from the head of a list */
  231. static struct usb_request *req_get(struct acc_dev *dev, struct list_head *head)
  232. {
  233. unsigned long flags;
  234. struct usb_request *req;
  235. spin_lock_irqsave(&dev->lock, flags);
  236. if (list_empty(head)) {
  237. req = 0;
  238. } else {
  239. req = list_first_entry(head, struct usb_request, list);
  240. list_del(&req->list);
  241. }
  242. spin_unlock_irqrestore(&dev->lock, flags);
  243. return req;
  244. }
  245. static void acc_set_disconnected(struct acc_dev *dev)
  246. {
  247. dev->online = 0;
  248. dev->disconnected = 1;
  249. }
  250. static void acc_complete_in(struct usb_ep *ep, struct usb_request *req)
  251. {
  252. struct acc_dev *dev = _acc_dev;
  253. if (req->status == -ESHUTDOWN) {
  254. pr_debug("acc_complete_in set disconnected");
  255. acc_set_disconnected(dev);
  256. }
  257. req_put(dev, &dev->tx_idle, req);
  258. wake_up(&dev->write_wq);
  259. }
  260. static void acc_complete_out(struct usb_ep *ep, struct usb_request *req)
  261. {
  262. struct acc_dev *dev = _acc_dev;
  263. dev->rx_done = 1;
  264. if (req->status == -ESHUTDOWN) {
  265. pr_debug("acc_complete_out set disconnected");
  266. acc_set_disconnected(dev);
  267. }
  268. wake_up(&dev->read_wq);
  269. }
  270. static void acc_complete_set_string(struct usb_ep *ep, struct usb_request *req)
  271. {
  272. struct acc_dev *dev = ep->driver_data;
  273. char *string_dest = NULL;
  274. int length = req->actual;
  275. if (req->status != 0) {
  276. pr_err("acc_complete_set_string, err %d\n", req->status);
  277. return;
  278. }
  279. switch (dev->string_index) {
  280. case ACCESSORY_STRING_MANUFACTURER:
  281. string_dest = dev->manufacturer;
  282. break;
  283. case ACCESSORY_STRING_MODEL:
  284. string_dest = dev->model;
  285. break;
  286. case ACCESSORY_STRING_DESCRIPTION:
  287. string_dest = dev->description;
  288. break;
  289. case ACCESSORY_STRING_VERSION:
  290. string_dest = dev->version;
  291. break;
  292. case ACCESSORY_STRING_URI:
  293. string_dest = dev->uri;
  294. break;
  295. case ACCESSORY_STRING_SERIAL:
  296. string_dest = dev->serial;
  297. break;
  298. }
  299. if (string_dest) {
  300. unsigned long flags;
  301. if (length >= ACC_STRING_SIZE)
  302. length = ACC_STRING_SIZE - 1;
  303. spin_lock_irqsave(&dev->lock, flags);
  304. memcpy(string_dest, req->buf, length);
  305. /* ensure zero termination */
  306. string_dest[length] = 0;
  307. spin_unlock_irqrestore(&dev->lock, flags);
  308. } else {
  309. pr_err("unknown accessory string index %d\n",
  310. dev->string_index);
  311. }
  312. }
  313. static void acc_complete_set_hid_report_desc(struct usb_ep *ep,
  314. struct usb_request *req)
  315. {
  316. struct acc_hid_dev *hid = req->context;
  317. struct acc_dev *dev = hid->dev;
  318. int length = req->actual;
  319. if (req->status != 0) {
  320. pr_err("acc_complete_set_hid_report_desc, err %d\n",
  321. req->status);
  322. return;
  323. }
  324. memcpy(hid->report_desc + hid->report_desc_offset, req->buf, length);
  325. hid->report_desc_offset += length;
  326. if (hid->report_desc_offset == hid->report_desc_len) {
  327. /* After we have received the entire report descriptor
  328. * we schedule work to initialize the HID device
  329. */
  330. schedule_work(&dev->hid_work);
  331. }
  332. }
  333. static void acc_complete_send_hid_event(struct usb_ep *ep,
  334. struct usb_request *req)
  335. {
  336. struct acc_hid_dev *hid = req->context;
  337. int length = req->actual;
  338. if (req->status != 0) {
  339. pr_err("acc_complete_send_hid_event, err %d\n", req->status);
  340. return;
  341. }
  342. hid_report_raw_event(hid->hid, HID_INPUT_REPORT, req->buf, length, 1);
  343. }
  344. static int acc_hid_parse(struct hid_device *hid)
  345. {
  346. struct acc_hid_dev *hdev = hid->driver_data;
  347. hid_parse_report(hid, hdev->report_desc, hdev->report_desc_len);
  348. return 0;
  349. }
  350. static int acc_hid_start(struct hid_device *hid)
  351. {
  352. return 0;
  353. }
  354. static void acc_hid_stop(struct hid_device *hid)
  355. {
  356. }
  357. static int acc_hid_open(struct hid_device *hid)
  358. {
  359. return 0;
  360. }
  361. static void acc_hid_close(struct hid_device *hid)
  362. {
  363. }
  364. static struct hid_ll_driver acc_hid_ll_driver = {
  365. .parse = acc_hid_parse,
  366. .start = acc_hid_start,
  367. .stop = acc_hid_stop,
  368. .open = acc_hid_open,
  369. .close = acc_hid_close,
  370. };
  371. static struct acc_hid_dev *acc_hid_new(struct acc_dev *dev,
  372. int id, int desc_len)
  373. {
  374. struct acc_hid_dev *hdev;
  375. hdev = kzalloc(sizeof(*hdev), GFP_ATOMIC);
  376. if (!hdev)
  377. return NULL;
  378. hdev->report_desc = kzalloc(desc_len, GFP_ATOMIC);
  379. if (!hdev->report_desc) {
  380. kfree(hdev);
  381. return NULL;
  382. }
  383. hdev->dev = dev;
  384. hdev->id = id;
  385. hdev->report_desc_len = desc_len;
  386. return hdev;
  387. }
  388. static struct acc_hid_dev *acc_hid_get(struct list_head *list, int id)
  389. {
  390. struct acc_hid_dev *hid;
  391. list_for_each_entry(hid, list, list) {
  392. if (hid->id == id)
  393. return hid;
  394. }
  395. return NULL;
  396. }
  397. static int acc_register_hid(struct acc_dev *dev, int id, int desc_length)
  398. {
  399. struct acc_hid_dev *hid;
  400. unsigned long flags;
  401. /* report descriptor length must be > 0 */
  402. if (desc_length <= 0)
  403. return -EINVAL;
  404. spin_lock_irqsave(&dev->lock, flags);
  405. /* replace HID if one already exists with this ID */
  406. hid = acc_hid_get(&dev->hid_list, id);
  407. if (!hid)
  408. hid = acc_hid_get(&dev->new_hid_list, id);
  409. if (hid)
  410. list_move(&hid->list, &dev->dead_hid_list);
  411. hid = acc_hid_new(dev, id, desc_length);
  412. if (!hid) {
  413. spin_unlock_irqrestore(&dev->lock, flags);
  414. return -ENOMEM;
  415. }
  416. list_add(&hid->list, &dev->new_hid_list);
  417. spin_unlock_irqrestore(&dev->lock, flags);
  418. /* schedule work to register the HID device */
  419. schedule_work(&dev->hid_work);
  420. return 0;
  421. }
  422. static int acc_unregister_hid(struct acc_dev *dev, int id)
  423. {
  424. struct acc_hid_dev *hid;
  425. unsigned long flags;
  426. spin_lock_irqsave(&dev->lock, flags);
  427. hid = acc_hid_get(&dev->hid_list, id);
  428. if (!hid)
  429. hid = acc_hid_get(&dev->new_hid_list, id);
  430. if (!hid) {
  431. spin_unlock_irqrestore(&dev->lock, flags);
  432. return -EINVAL;
  433. }
  434. list_move(&hid->list, &dev->dead_hid_list);
  435. spin_unlock_irqrestore(&dev->lock, flags);
  436. schedule_work(&dev->hid_work);
  437. return 0;
  438. }
  439. static int create_bulk_endpoints(struct acc_dev *dev,
  440. struct usb_endpoint_descriptor *in_desc,
  441. struct usb_endpoint_descriptor *out_desc)
  442. {
  443. struct usb_composite_dev *cdev = dev->cdev;
  444. struct usb_request *req;
  445. struct usb_ep *ep;
  446. int i;
  447. DBG(cdev, "create_bulk_endpoints dev: %pK\n", dev);
  448. ep = usb_ep_autoconfig(cdev->gadget, in_desc);
  449. if (!ep) {
  450. DBG(cdev, "usb_ep_autoconfig for ep_in failed\n");
  451. return -ENODEV;
  452. }
  453. DBG(cdev, "usb_ep_autoconfig for ep_in got %s\n", ep->name);
  454. ep->driver_data = dev; /* claim the endpoint */
  455. dev->ep_in = ep;
  456. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  457. if (!ep) {
  458. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  459. return -ENODEV;
  460. }
  461. DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
  462. ep->driver_data = dev; /* claim the endpoint */
  463. dev->ep_out = ep;
  464. ep = usb_ep_autoconfig(cdev->gadget, out_desc);
  465. if (!ep) {
  466. DBG(cdev, "usb_ep_autoconfig for ep_out failed\n");
  467. return -ENODEV;
  468. }
  469. DBG(cdev, "usb_ep_autoconfig for ep_out got %s\n", ep->name);
  470. ep->driver_data = dev; /* claim the endpoint */
  471. dev->ep_out = ep;
  472. /* now allocate requests for our endpoints */
  473. for (i = 0; i < TX_REQ_MAX; i++) {
  474. req = acc_request_new(dev->ep_in, BULK_BUFFER_SIZE);
  475. if (!req)
  476. goto fail;
  477. req->complete = acc_complete_in;
  478. req_put(dev, &dev->tx_idle, req);
  479. }
  480. for (i = 0; i < RX_REQ_MAX; i++) {
  481. req = acc_request_new(dev->ep_out, BULK_BUFFER_SIZE);
  482. if (!req)
  483. goto fail;
  484. req->complete = acc_complete_out;
  485. dev->rx_req[i] = req;
  486. }
  487. return 0;
  488. fail:
  489. pr_err("acc_bind() could not allocate requests\n");
  490. while ((req = req_get(dev, &dev->tx_idle)))
  491. acc_request_free(req, dev->ep_in);
  492. for (i = 0; i < RX_REQ_MAX; i++)
  493. acc_request_free(dev->rx_req[i], dev->ep_out);
  494. return -1;
  495. }
  496. static ssize_t acc_read(struct file *fp, char __user *buf,
  497. size_t count, loff_t *pos)
  498. {
  499. struct acc_dev *dev = fp->private_data;
  500. struct usb_request *req;
  501. int r = count, xfer, len;
  502. int ret = 0;
  503. pr_debug("acc_read(%d)\n", count);
  504. if (dev->disconnected) {
  505. pr_debug("acc_read disconnected");
  506. return -ENODEV;
  507. }
  508. if (count > BULK_BUFFER_SIZE)
  509. count = BULK_BUFFER_SIZE;
  510. len = ALIGN(count, dev->ep_out->maxpacket);
  511. /* we will block until we're online */
  512. pr_debug("acc_read: waiting for online\n");
  513. ret = wait_event_interruptible(dev->read_wq, dev->online);
  514. if (ret < 0) {
  515. r = ret;
  516. goto done;
  517. }
  518. if (dev->rx_done) {
  519. // last req cancelled. try to get it.
  520. req = dev->rx_req[0];
  521. goto copy_data;
  522. }
  523. requeue_req:
  524. /* queue a request */
  525. req = dev->rx_req[0];
  526. req->length = len;
  527. dev->rx_done = 0;
  528. ret = usb_ep_queue(dev->ep_out, req, GFP_KERNEL);
  529. if (ret < 0) {
  530. r = -EIO;
  531. goto done;
  532. } else {
  533. pr_debug("rx %pK queue\n", req);
  534. }
  535. /* wait for a request to complete */
  536. ret = wait_event_interruptible(dev->read_wq, dev->rx_done);
  537. if (ret < 0) {
  538. r = ret;
  539. ret = usb_ep_dequeue(dev->ep_out, req);
  540. if (ret != 0) {
  541. // cancel failed. There can be a data already received.
  542. // it will be retrieved in the next read.
  543. pr_debug("acc_read: cancelling failed %d", ret);
  544. }
  545. goto done;
  546. }
  547. copy_data:
  548. dev->rx_done = 0;
  549. if (dev->online) {
  550. /* If we got a 0-len packet, throw it back and try again. */
  551. if (req->actual == 0)
  552. goto requeue_req;
  553. pr_debug("rx %pK %d\n", req, req->actual);
  554. xfer = (req->actual < count) ? req->actual : count;
  555. r = xfer;
  556. if (copy_to_user(buf, req->buf, xfer))
  557. r = -EFAULT;
  558. } else
  559. r = -EIO;
  560. done:
  561. pr_debug("acc_read returning %d\n", r);
  562. return r;
  563. }
  564. static ssize_t acc_write(struct file *fp, const char __user *buf,
  565. size_t count, loff_t *pos)
  566. {
  567. struct acc_dev *dev = fp->private_data;
  568. struct usb_request *req = 0;
  569. int r = count, xfer;
  570. int ret;
  571. pr_debug("acc_write(%d)\n", count);
  572. if (!dev->online || dev->disconnected) {
  573. pr_debug("acc_write disconnected or not online");
  574. return -ENODEV;
  575. }
  576. while (count > 0) {
  577. if (!dev->online) {
  578. pr_debug("acc_write dev->error\n");
  579. r = -EIO;
  580. break;
  581. }
  582. /* get an idle tx request to use */
  583. req = 0;
  584. ret = wait_event_interruptible(dev->write_wq,
  585. ((req = req_get(dev, &dev->tx_idle)) || !dev->online));
  586. if (!req) {
  587. r = ret;
  588. break;
  589. }
  590. if (count > BULK_BUFFER_SIZE) {
  591. xfer = BULK_BUFFER_SIZE;
  592. /* ZLP, They will be more TX requests so not yet. */
  593. req->zero = 0;
  594. } else {
  595. xfer = count;
  596. /* If the data length is a multple of the
  597. * maxpacket size then send a zero length packet(ZLP).
  598. */
  599. req->zero = ((xfer % dev->ep_in->maxpacket) == 0);
  600. }
  601. if (copy_from_user(req->buf, buf, xfer)) {
  602. r = -EFAULT;
  603. break;
  604. }
  605. req->length = xfer;
  606. ret = usb_ep_queue(dev->ep_in, req, GFP_KERNEL);
  607. if (ret < 0) {
  608. pr_debug("acc_write: xfer error %d\n", ret);
  609. r = -EIO;
  610. break;
  611. }
  612. buf += xfer;
  613. count -= xfer;
  614. /* zero this so we don't try to free it on error exit */
  615. req = 0;
  616. }
  617. if (req)
  618. req_put(dev, &dev->tx_idle, req);
  619. pr_debug("acc_write returning %d\n", r);
  620. return r;
  621. }
  622. static long acc_ioctl(struct file *fp, unsigned code, unsigned long value)
  623. {
  624. struct acc_dev *dev = fp->private_data;
  625. char *src = NULL;
  626. int ret;
  627. switch (code) {
  628. case ACCESSORY_GET_STRING_MANUFACTURER:
  629. src = dev->manufacturer;
  630. break;
  631. case ACCESSORY_GET_STRING_MODEL:
  632. src = dev->model;
  633. break;
  634. case ACCESSORY_GET_STRING_DESCRIPTION:
  635. src = dev->description;
  636. break;
  637. case ACCESSORY_GET_STRING_VERSION:
  638. src = dev->version;
  639. break;
  640. case ACCESSORY_GET_STRING_URI:
  641. src = dev->uri;
  642. break;
  643. case ACCESSORY_GET_STRING_SERIAL:
  644. src = dev->serial;
  645. break;
  646. case ACCESSORY_IS_START_REQUESTED:
  647. return dev->start_requested;
  648. case ACCESSORY_GET_AUDIO_MODE:
  649. return dev->audio_mode;
  650. }
  651. if (!src)
  652. return -EINVAL;
  653. ret = strlen(src) + 1;
  654. if (copy_to_user((void __user *)value, src, ret))
  655. ret = -EFAULT;
  656. return ret;
  657. }
  658. static int acc_open(struct inode *ip, struct file *fp)
  659. {
  660. printk(KERN_INFO "acc_open\n");
  661. if (atomic_xchg(&_acc_dev->open_excl, 1))
  662. return -EBUSY;
  663. _acc_dev->disconnected = 0;
  664. fp->private_data = _acc_dev;
  665. return 0;
  666. }
  667. static int acc_release(struct inode *ip, struct file *fp)
  668. {
  669. printk(KERN_INFO "acc_release\n");
  670. WARN_ON(!atomic_xchg(&_acc_dev->open_excl, 0));
  671. _acc_dev->disconnected = 0;
  672. return 0;
  673. }
  674. /* file operations for /dev/usb_accessory */
  675. static const struct file_operations acc_fops = {
  676. .owner = THIS_MODULE,
  677. .read = acc_read,
  678. .write = acc_write,
  679. .unlocked_ioctl = acc_ioctl,
  680. .open = acc_open,
  681. .release = acc_release,
  682. };
  683. static int acc_hid_probe(struct hid_device *hdev,
  684. const struct hid_device_id *id)
  685. {
  686. int ret;
  687. ret = hid_parse(hdev);
  688. if (ret)
  689. return ret;
  690. return hid_hw_start(hdev, HID_CONNECT_DEFAULT);
  691. }
  692. static struct miscdevice acc_device = {
  693. .minor = MISC_DYNAMIC_MINOR,
  694. .name = "usb_accessory",
  695. .fops = &acc_fops,
  696. };
  697. static const struct hid_device_id acc_hid_table[] = {
  698. { HID_USB_DEVICE(HID_ANY_ID, HID_ANY_ID) },
  699. { }
  700. };
  701. static struct hid_driver acc_hid_driver = {
  702. .name = "USB accessory",
  703. .id_table = acc_hid_table,
  704. .probe = acc_hid_probe,
  705. };
  706. static int acc_ctrlrequest(struct usb_composite_dev *cdev,
  707. const struct usb_ctrlrequest *ctrl)
  708. {
  709. struct acc_dev *dev = _acc_dev;
  710. int value = -EOPNOTSUPP;
  711. struct acc_hid_dev *hid;
  712. int offset;
  713. u8 b_requestType = ctrl->bRequestType;
  714. u8 b_request = ctrl->bRequest;
  715. u16 w_index = le16_to_cpu(ctrl->wIndex);
  716. u16 w_value = le16_to_cpu(ctrl->wValue);
  717. u16 w_length = le16_to_cpu(ctrl->wLength);
  718. unsigned long flags;
  719. /*
  720. printk(KERN_INFO "acc_ctrlrequest "
  721. "%02x.%02x v%04x i%04x l%u\n",
  722. b_requestType, b_request,
  723. w_value, w_index, w_length);
  724. */
  725. if (b_requestType == (USB_DIR_OUT | USB_TYPE_VENDOR)) {
  726. if (b_request == ACCESSORY_START) {
  727. dev->start_requested = 1;
  728. schedule_delayed_work(
  729. &dev->start_work, msecs_to_jiffies(200));
  730. value = 0;
  731. } else if (b_request == ACCESSORY_SEND_STRING) {
  732. dev->string_index = w_index;
  733. cdev->gadget->ep0->driver_data = dev;
  734. cdev->req->complete = acc_complete_set_string;
  735. value = w_length;
  736. } else if (b_request == ACCESSORY_SET_AUDIO_MODE &&
  737. w_index == 0 && w_length == 0) {
  738. dev->audio_mode = w_value;
  739. value = 0;
  740. } else if (b_request == ACCESSORY_REGISTER_HID) {
  741. value = acc_register_hid(dev, w_value, w_index);
  742. } else if (b_request == ACCESSORY_UNREGISTER_HID) {
  743. value = acc_unregister_hid(dev, w_value);
  744. } else if (b_request == ACCESSORY_SET_HID_REPORT_DESC) {
  745. spin_lock_irqsave(&dev->lock, flags);
  746. hid = acc_hid_get(&dev->new_hid_list, w_value);
  747. spin_unlock_irqrestore(&dev->lock, flags);
  748. if (!hid) {
  749. value = -EINVAL;
  750. goto err;
  751. }
  752. offset = w_index;
  753. if (offset != hid->report_desc_offset
  754. || offset + w_length > hid->report_desc_len) {
  755. value = -EINVAL;
  756. goto err;
  757. }
  758. cdev->req->context = hid;
  759. cdev->req->complete = acc_complete_set_hid_report_desc;
  760. value = w_length;
  761. } else if (b_request == ACCESSORY_SEND_HID_EVENT) {
  762. spin_lock_irqsave(&dev->lock, flags);
  763. hid = acc_hid_get(&dev->hid_list, w_value);
  764. spin_unlock_irqrestore(&dev->lock, flags);
  765. if (!hid) {
  766. value = -EINVAL;
  767. goto err;
  768. }
  769. cdev->req->context = hid;
  770. cdev->req->complete = acc_complete_send_hid_event;
  771. value = w_length;
  772. }
  773. } else if (b_requestType == (USB_DIR_IN | USB_TYPE_VENDOR)) {
  774. if (b_request == ACCESSORY_GET_PROTOCOL) {
  775. *((u16 *)cdev->req->buf) = PROTOCOL_VERSION;
  776. value = sizeof(u16);
  777. /* clear strings left over from a previous session */
  778. memset(dev->manufacturer, 0, sizeof(dev->manufacturer));
  779. memset(dev->model, 0, sizeof(dev->model));
  780. memset(dev->description, 0, sizeof(dev->description));
  781. memset(dev->version, 0, sizeof(dev->version));
  782. memset(dev->uri, 0, sizeof(dev->uri));
  783. memset(dev->serial, 0, sizeof(dev->serial));
  784. dev->start_requested = 0;
  785. dev->audio_mode = 0;
  786. }
  787. }
  788. if (value >= 0) {
  789. cdev->req->zero = 0;
  790. cdev->req->length = value;
  791. value = usb_ep_queue(cdev->gadget->ep0, cdev->req, GFP_ATOMIC);
  792. if (value < 0)
  793. ERROR(cdev, "%s setup response queue error\n",
  794. __func__);
  795. }
  796. err:
  797. if (value == -EOPNOTSUPP)
  798. VDBG(cdev,
  799. "unknown class-specific control req "
  800. "%02x.%02x v%04x i%04x l%u\n",
  801. ctrl->bRequestType, ctrl->bRequest,
  802. w_value, w_index, w_length);
  803. return value;
  804. }
  805. static int
  806. acc_function_bind(struct usb_configuration *c, struct usb_function *f)
  807. {
  808. struct usb_composite_dev *cdev = c->cdev;
  809. struct acc_dev *dev = func_to_dev(f);
  810. int id;
  811. int ret;
  812. DBG(cdev, "acc_function_bind dev: %pK\n", dev);
  813. ret = hid_register_driver(&acc_hid_driver);
  814. if (ret)
  815. return ret;
  816. dev->start_requested = 0;
  817. /* allocate interface ID(s) */
  818. id = usb_interface_id(c, f);
  819. if (id < 0)
  820. return id;
  821. acc_interface_desc.bInterfaceNumber = id;
  822. /* allocate endpoints */
  823. ret = create_bulk_endpoints(dev, &acc_fullspeed_in_desc,
  824. &acc_fullspeed_out_desc);
  825. if (ret)
  826. return ret;
  827. /* support high speed hardware */
  828. if (gadget_is_dualspeed(c->cdev->gadget)) {
  829. acc_highspeed_in_desc.bEndpointAddress =
  830. acc_fullspeed_in_desc.bEndpointAddress;
  831. acc_highspeed_out_desc.bEndpointAddress =
  832. acc_fullspeed_out_desc.bEndpointAddress;
  833. }
  834. /* support super speed hardware */
  835. if (gadget_is_superspeed(c->cdev->gadget)) {
  836. acc_superspeed_in_desc.bEndpointAddress =
  837. acc_fullspeed_in_desc.bEndpointAddress;
  838. acc_superspeed_out_desc.bEndpointAddress =
  839. acc_fullspeed_out_desc.bEndpointAddress;
  840. }
  841. DBG(cdev, "%s speed %s: IN/%s, OUT/%s\n",
  842. gadget_is_dualspeed(c->cdev->gadget) ? "dual" : "full",
  843. f->name, dev->ep_in->name, dev->ep_out->name);
  844. return 0;
  845. }
  846. static void
  847. kill_all_hid_devices(struct acc_dev *dev)
  848. {
  849. struct acc_hid_dev *hid;
  850. struct list_head *entry, *temp;
  851. unsigned long flags;
  852. /* do nothing if usb accessory device doesn't exist */
  853. if (!dev)
  854. return;
  855. spin_lock_irqsave(&dev->lock, flags);
  856. list_for_each_safe(entry, temp, &dev->hid_list) {
  857. hid = list_entry(entry, struct acc_hid_dev, list);
  858. list_del(&hid->list);
  859. list_add(&hid->list, &dev->dead_hid_list);
  860. }
  861. list_for_each_safe(entry, temp, &dev->new_hid_list) {
  862. hid = list_entry(entry, struct acc_hid_dev, list);
  863. list_del(&hid->list);
  864. list_add(&hid->list, &dev->dead_hid_list);
  865. }
  866. spin_unlock_irqrestore(&dev->lock, flags);
  867. schedule_work(&dev->hid_work);
  868. }
  869. static void
  870. acc_hid_unbind(struct acc_dev *dev)
  871. {
  872. hid_unregister_driver(&acc_hid_driver);
  873. kill_all_hid_devices(dev);
  874. }
  875. static void
  876. acc_function_unbind(struct usb_configuration *c, struct usb_function *f)
  877. {
  878. struct acc_dev *dev = func_to_dev(f);
  879. struct usb_request *req;
  880. int i;
  881. while ((req = req_get(dev, &dev->tx_idle)))
  882. acc_request_free(req, dev->ep_in);
  883. for (i = 0; i < RX_REQ_MAX; i++)
  884. acc_request_free(dev->rx_req[i], dev->ep_out);
  885. acc_hid_unbind(dev);
  886. }
  887. static void acc_start_work(struct work_struct *data)
  888. {
  889. char *envp[2] = { "ACCESSORY=START", NULL };
  890. kobject_uevent_env(&acc_device.this_device->kobj, KOBJ_CHANGE, envp);
  891. }
  892. static int acc_hid_init(struct acc_hid_dev *hdev)
  893. {
  894. struct hid_device *hid;
  895. int ret;
  896. hid = hid_allocate_device();
  897. if (IS_ERR(hid))
  898. return PTR_ERR(hid);
  899. hid->ll_driver = &acc_hid_ll_driver;
  900. hid->dev.parent = acc_device.this_device;
  901. hid->bus = BUS_USB;
  902. hid->vendor = HID_ANY_ID;
  903. hid->product = HID_ANY_ID;
  904. hid->driver_data = hdev;
  905. ret = hid_add_device(hid);
  906. if (ret) {
  907. pr_err("can't add hid device: %d\n", ret);
  908. hid_destroy_device(hid);
  909. return ret;
  910. }
  911. hdev->hid = hid;
  912. return 0;
  913. }
  914. static void acc_hid_delete(struct acc_hid_dev *hid)
  915. {
  916. kfree(hid->report_desc);
  917. kfree(hid);
  918. }
  919. static void acc_hid_work(struct work_struct *data)
  920. {
  921. struct acc_dev *dev = _acc_dev;
  922. struct list_head *entry, *temp;
  923. struct acc_hid_dev *hid;
  924. struct list_head new_list, dead_list;
  925. unsigned long flags;
  926. INIT_LIST_HEAD(&new_list);
  927. spin_lock_irqsave(&dev->lock, flags);
  928. /* copy hids that are ready for initialization to new_list */
  929. list_for_each_safe(entry, temp, &dev->new_hid_list) {
  930. hid = list_entry(entry, struct acc_hid_dev, list);
  931. if (hid->report_desc_offset == hid->report_desc_len)
  932. list_move(&hid->list, &new_list);
  933. }
  934. if (list_empty(&dev->dead_hid_list)) {
  935. INIT_LIST_HEAD(&dead_list);
  936. } else {
  937. /* move all of dev->dead_hid_list to dead_list */
  938. dead_list.prev = dev->dead_hid_list.prev;
  939. dead_list.next = dev->dead_hid_list.next;
  940. dead_list.next->prev = &dead_list;
  941. dead_list.prev->next = &dead_list;
  942. INIT_LIST_HEAD(&dev->dead_hid_list);
  943. }
  944. spin_unlock_irqrestore(&dev->lock, flags);
  945. /* register new HID devices */
  946. list_for_each_safe(entry, temp, &new_list) {
  947. hid = list_entry(entry, struct acc_hid_dev, list);
  948. if (acc_hid_init(hid)) {
  949. pr_err("can't add HID device %pK\n", hid);
  950. acc_hid_delete(hid);
  951. } else {
  952. spin_lock_irqsave(&dev->lock, flags);
  953. list_move(&hid->list, &dev->hid_list);
  954. spin_unlock_irqrestore(&dev->lock, flags);
  955. }
  956. }
  957. /* remove dead HID devices */
  958. list_for_each_safe(entry, temp, &dead_list) {
  959. hid = list_entry(entry, struct acc_hid_dev, list);
  960. list_del(&hid->list);
  961. if (hid->hid)
  962. hid_destroy_device(hid->hid);
  963. acc_hid_delete(hid);
  964. }
  965. }
  966. static int acc_function_set_alt(struct usb_function *f,
  967. unsigned intf, unsigned alt)
  968. {
  969. struct acc_dev *dev = func_to_dev(f);
  970. struct usb_composite_dev *cdev = f->config->cdev;
  971. int ret;
  972. DBG(cdev, "acc_function_set_alt intf: %d alt: %d\n", intf, alt);
  973. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_in);
  974. if (ret) {
  975. dev->ep_in->desc = NULL;
  976. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  977. dev->ep_in->name, ret);
  978. return ret;
  979. }
  980. ret = usb_ep_enable(dev->ep_in);
  981. if (ret) {
  982. ERROR(cdev, "failed to enable ep %s, result %d\n",
  983. dev->ep_in->name, ret);
  984. return ret;
  985. }
  986. ret = config_ep_by_speed(cdev->gadget, f, dev->ep_out);
  987. if (ret) {
  988. dev->ep_out->desc = NULL;
  989. ERROR(cdev, "config_ep_by_speed failes for ep %s, result %d\n",
  990. dev->ep_out->name, ret);
  991. usb_ep_disable(dev->ep_in);
  992. return ret;
  993. }
  994. ret = usb_ep_enable(dev->ep_out);
  995. if (ret) {
  996. ERROR(cdev, "failed to enable ep %s, result %d\n",
  997. dev->ep_out->name, ret);
  998. usb_ep_disable(dev->ep_in);
  999. return ret;
  1000. }
  1001. dev->online = 1;
  1002. /* readers may be blocked waiting for us to go online */
  1003. wake_up(&dev->read_wq);
  1004. return 0;
  1005. }
  1006. static void acc_function_disable(struct usb_function *f)
  1007. {
  1008. struct acc_dev *dev = func_to_dev(f);
  1009. struct usb_composite_dev *cdev = dev->cdev;
  1010. DBG(cdev, "acc_function_disable\n");
  1011. acc_set_disconnected(dev);
  1012. usb_ep_disable(dev->ep_in);
  1013. usb_ep_disable(dev->ep_out);
  1014. /* readers may be blocked waiting for us to go online */
  1015. wake_up(&dev->read_wq);
  1016. VDBG(cdev, "%s disabled\n", dev->function.name);
  1017. }
  1018. static int acc_bind_config(struct usb_configuration *c)
  1019. {
  1020. struct acc_dev *dev = _acc_dev;
  1021. int ret;
  1022. printk(KERN_INFO "acc_bind_config\n");
  1023. /* allocate a string ID for our interface */
  1024. if (acc_string_defs[INTERFACE_STRING_INDEX].id == 0) {
  1025. ret = usb_string_id(c->cdev);
  1026. if (ret < 0)
  1027. return ret;
  1028. acc_string_defs[INTERFACE_STRING_INDEX].id = ret;
  1029. acc_interface_desc.iInterface = ret;
  1030. }
  1031. dev->cdev = c->cdev;
  1032. dev->function.name = "accessory";
  1033. dev->function.strings = acc_strings,
  1034. dev->function.fs_descriptors = fs_acc_descs;
  1035. dev->function.hs_descriptors = hs_acc_descs;
  1036. if (gadget_is_superspeed(c->cdev->gadget))
  1037. dev->function.ss_descriptors = ss_acc_descs;
  1038. dev->function.bind = acc_function_bind;
  1039. dev->function.unbind = acc_function_unbind;
  1040. dev->function.set_alt = acc_function_set_alt;
  1041. dev->function.disable = acc_function_disable;
  1042. return usb_add_function(c, &dev->function);
  1043. }
  1044. static int acc_setup(void)
  1045. {
  1046. struct acc_dev *dev;
  1047. int ret;
  1048. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  1049. if (!dev)
  1050. return -ENOMEM;
  1051. spin_lock_init(&dev->lock);
  1052. init_waitqueue_head(&dev->read_wq);
  1053. init_waitqueue_head(&dev->write_wq);
  1054. atomic_set(&dev->open_excl, 0);
  1055. INIT_LIST_HEAD(&dev->tx_idle);
  1056. INIT_LIST_HEAD(&dev->hid_list);
  1057. INIT_LIST_HEAD(&dev->new_hid_list);
  1058. INIT_LIST_HEAD(&dev->dead_hid_list);
  1059. INIT_DELAYED_WORK(&dev->start_work, acc_start_work);
  1060. INIT_WORK(&dev->hid_work, acc_hid_work);
  1061. /* _acc_dev must be set before calling usb_gadget_register_driver */
  1062. _acc_dev = dev;
  1063. ret = misc_register(&acc_device);
  1064. if (ret)
  1065. goto err;
  1066. return 0;
  1067. err:
  1068. kfree(dev);
  1069. pr_err("USB accessory gadget driver failed to initialize\n");
  1070. return ret;
  1071. }
  1072. static void acc_disconnect(void)
  1073. {
  1074. /* unregister all HID devices if USB is disconnected */
  1075. kill_all_hid_devices(_acc_dev);
  1076. }
  1077. static void acc_cleanup(void)
  1078. {
  1079. misc_deregister(&acc_device);
  1080. kfree(_acc_dev);
  1081. _acc_dev = NULL;
  1082. }