f_accessory.c 36 KB

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