f_accessory.c 29 KB

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