f_apple_hid_sim.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  1. /*
  2. * f_hid.c -- USB HID function driver
  3. *
  4. * Copyright (C) 2010 Fabien Chouteau <fabien.chouteau@barco.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/hid.h>
  14. #include <linux/idr.h>
  15. #include <linux/cdev.h>
  16. #include <linux/mutex.h>
  17. #include <linux/poll.h>
  18. #include <linux/uaccess.h>
  19. #include <linux/wait.h>
  20. #include <linux/sched.h>
  21. #include <linux/usb/g_hid.h>
  22. #include "u_f.h"
  23. #include "u_hid.h"
  24. #include "f_apple_common.h"
  25. #define HIDG_MINORS 4
  26. #define USE_OUTPUT_EP 0
  27. static int major, minors;
  28. static struct class *hidg_class;
  29. static DEFINE_IDA(hidg_ida);
  30. static DEFINE_MUTEX(hidg_ida_lock); /* protects access to hidg_ida */
  31. /*-------------------------------------------------------------------------*/
  32. /* HID gadget struct */
  33. struct f_hidg_req_list {
  34. #if USE_OUTPUT_EP
  35. struct usb_request *req;
  36. #else
  37. char* set_report_buff;
  38. unsigned int set_report_length;
  39. #endif
  40. unsigned int pos;
  41. struct list_head list;
  42. };
  43. struct f_hidg {
  44. /* configuration */
  45. unsigned char bInterfaceSubClass;
  46. unsigned char bInterfaceProtocol;
  47. unsigned char protocol;
  48. unsigned short report_desc_length;
  49. char *report_desc;
  50. unsigned short report_length;
  51. /* recv report */
  52. struct list_head completed_out_req;
  53. spinlock_t read_spinlock;
  54. wait_queue_head_t read_queue;
  55. unsigned int qlen;
  56. /* send report */
  57. spinlock_t write_spinlock;
  58. bool write_pending;
  59. wait_queue_head_t write_queue;
  60. struct usb_request *req;
  61. int minor;
  62. struct cdev cdev;
  63. struct usb_function func;
  64. struct usb_ep *in_ep;
  65. #if USE_OUTPUT_EP
  66. struct usb_ep *out_ep;
  67. #endif
  68. uint8_t ep_addr_in;
  69. };
  70. int usb_dev_ready_notify(int is_ready);
  71. static inline struct f_hidg *func_to_hidg(struct usb_function *f)
  72. {
  73. return container_of(f, struct f_hidg, func);
  74. }
  75. /*-------------------------------------------------------------------------*/
  76. /* Static descriptors */
  77. static struct usb_interface_descriptor hidg_interface_desc = {
  78. .bLength = sizeof hidg_interface_desc,
  79. .bDescriptorType = USB_DT_INTERFACE,
  80. /* .bInterfaceNumber = DYNAMIC */
  81. .bAlternateSetting = 0,
  82. #if USE_OUTPUT_EP
  83. .bNumEndpoints = 2,
  84. #else
  85. .bNumEndpoints = 1,
  86. #endif
  87. .bInterfaceClass = USB_CLASS_HID,
  88. /* .bInterfaceSubClass = DYNAMIC */
  89. /* .bInterfaceProtocol = DYNAMIC */
  90. /* .iInterface = DYNAMIC */
  91. };
  92. static struct hid_descriptor hidg_desc = {
  93. .bLength = sizeof hidg_desc,
  94. .bDescriptorType = HID_DT_HID,
  95. .bcdHID = 0x0111,
  96. .bCountryCode = 0x00,
  97. .bNumDescriptors = 0x1,
  98. /*.desc[0].bDescriptorType = DYNAMIC */
  99. /*.desc[0].wDescriptorLenght = DYNAMIC */
  100. };
  101. /* Super-Speed Support */
  102. static struct usb_endpoint_descriptor hidg_ss_in_ep_desc = {
  103. .bLength = USB_DT_ENDPOINT_SIZE,
  104. .bDescriptorType = USB_DT_ENDPOINT,
  105. .bEndpointAddress = USB_DIR_IN,
  106. .bmAttributes = USB_ENDPOINT_XFER_INT,
  107. /*.wMaxPacketSize = DYNAMIC */
  108. .bInterval = 1, /* FIXME: Add this field in the
  109. * HID gadget configuration?
  110. * (struct hidg_func_descriptor)
  111. */
  112. };
  113. static struct usb_ss_ep_comp_descriptor hidg_ss_in_comp_desc = {
  114. .bLength = sizeof(hidg_ss_in_comp_desc),
  115. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  116. /* .bMaxBurst = 0, */
  117. /* .bmAttributes = 0, */
  118. /* .wBytesPerInterval = DYNAMIC */
  119. };
  120. #if USE_OUTPUT_EP
  121. static struct usb_endpoint_descriptor hidg_ss_out_ep_desc = {
  122. .bLength = USB_DT_ENDPOINT_SIZE,
  123. .bDescriptorType = USB_DT_ENDPOINT,
  124. .bEndpointAddress = USB_DIR_OUT,
  125. .bmAttributes = USB_ENDPOINT_XFER_INT,
  126. /*.wMaxPacketSize = DYNAMIC */
  127. .bInterval = 4, /* FIXME: Add this field in the
  128. * HID gadget configuration?
  129. * (struct hidg_func_descriptor)
  130. */
  131. };
  132. static struct usb_ss_ep_comp_descriptor hidg_ss_out_comp_desc = {
  133. .bLength = sizeof(hidg_ss_out_comp_desc),
  134. .bDescriptorType = USB_DT_SS_ENDPOINT_COMP,
  135. /* .bMaxBurst = 0, */
  136. /* .bmAttributes = 0, */
  137. /* .wBytesPerInterval = DYNAMIC */
  138. };
  139. #endif
  140. static struct usb_descriptor_header *hidg_ss_descriptors[] = {
  141. (struct usb_descriptor_header *)&hidg_interface_desc,
  142. (struct usb_descriptor_header *)&hidg_desc,
  143. (struct usb_descriptor_header *)&hidg_ss_in_ep_desc,
  144. (struct usb_descriptor_header *)&hidg_ss_in_comp_desc,
  145. #if USE_OUTPUT_EP
  146. (struct usb_descriptor_header *)&hidg_ss_out_ep_desc,
  147. (struct usb_descriptor_header *)&hidg_ss_out_comp_desc,
  148. #endif
  149. NULL,
  150. };
  151. /* High-Speed Support */
  152. static struct usb_endpoint_descriptor hidg_hs_in_ep_desc = {
  153. .bLength = USB_DT_ENDPOINT_SIZE,
  154. .bDescriptorType = USB_DT_ENDPOINT,
  155. .bEndpointAddress = USB_DIR_IN,
  156. .bmAttributes = USB_ENDPOINT_XFER_INT,
  157. /*.wMaxPacketSize = DYNAMIC */
  158. .bInterval = 1, /* FIXME: Add this field in the
  159. * HID gadget configuration?
  160. * (struct hidg_func_descriptor)
  161. */
  162. };
  163. #if USE_OUTPUT_EP
  164. static struct usb_endpoint_descriptor hidg_hs_out_ep_desc = {
  165. .bLength = USB_DT_ENDPOINT_SIZE,
  166. .bDescriptorType = USB_DT_ENDPOINT,
  167. .bEndpointAddress = USB_DIR_OUT,
  168. .bmAttributes = USB_ENDPOINT_XFER_INT,
  169. /*.wMaxPacketSize = DYNAMIC */
  170. .bInterval = 4, /* FIXME: Add this field in the
  171. * HID gadget configuration?
  172. * (struct hidg_func_descriptor)
  173. */
  174. };
  175. #endif
  176. static struct usb_descriptor_header *hidg_hs_descriptors[] = {
  177. (struct usb_descriptor_header *)&hidg_interface_desc,
  178. (struct usb_descriptor_header *)&hidg_desc,
  179. (struct usb_descriptor_header *)&hidg_hs_in_ep_desc,
  180. #if USE_OUTPUT_EP
  181. (struct usb_descriptor_header *)&hidg_hs_out_ep_desc,
  182. #endif
  183. NULL,
  184. };
  185. /* Full-Speed Support */
  186. static struct usb_endpoint_descriptor hidg_fs_in_ep_desc = {
  187. .bLength = USB_DT_ENDPOINT_SIZE,
  188. .bDescriptorType = USB_DT_ENDPOINT,
  189. .bEndpointAddress = USB_DIR_IN,
  190. .bmAttributes = USB_ENDPOINT_XFER_INT,
  191. /*.wMaxPacketSize = DYNAMIC */
  192. .bInterval = 1, /* FIXME: Add this field in the
  193. * HID gadget configuration?
  194. * (struct hidg_func_descriptor)
  195. */
  196. };
  197. #if USE_OUTPUT_EP
  198. static struct usb_endpoint_descriptor hidg_fs_out_ep_desc = {
  199. .bLength = USB_DT_ENDPOINT_SIZE,
  200. .bDescriptorType = USB_DT_ENDPOINT,
  201. .bEndpointAddress = USB_DIR_OUT,
  202. .bmAttributes = USB_ENDPOINT_XFER_INT,
  203. /*.wMaxPacketSize = DYNAMIC */
  204. .bInterval = 10, /* FIXME: Add this field in the
  205. * HID gadget configuration?
  206. * (struct hidg_func_descriptor)
  207. */
  208. };
  209. #endif
  210. static struct usb_descriptor_header *hidg_fs_descriptors[] = {
  211. (struct usb_descriptor_header *)&hidg_interface_desc,
  212. (struct usb_descriptor_header *)&hidg_desc,
  213. (struct usb_descriptor_header *)&hidg_fs_in_ep_desc,
  214. #if USE_OUTPUT_EP
  215. (struct usb_descriptor_header *)&hidg_fs_out_ep_desc,
  216. #endif
  217. NULL,
  218. };
  219. /*-------------------------------------------------------------------------*/
  220. /* Strings */
  221. #if 0
  222. #define CT_FUNC_HID_IDX 0
  223. static struct usb_string ct_func_string_defs[] = {
  224. [CT_FUNC_HID_IDX].s = "HID Interface",
  225. {}, /* end of list */
  226. };
  227. static struct usb_gadget_strings ct_func_string_table = {
  228. .language = 0x0409, /* en-US */
  229. .strings = ct_func_string_defs,
  230. };
  231. static struct usb_gadget_strings *ct_func_strings[] = {
  232. &ct_func_string_table,
  233. NULL,
  234. };
  235. #endif
  236. /*-------------------------------------------------------------------------*/
  237. /* Char Device */
  238. #if USE_OUTPUT_EP
  239. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  240. size_t count, loff_t *ptr)
  241. {
  242. struct f_hidg *hidg = file->private_data;
  243. struct f_hidg_req_list *list;
  244. struct usb_request *req;
  245. unsigned long flags;
  246. int ret;
  247. //printk("%s:%d count=%d\n", __func__, __LINE__, count);
  248. if (!count){printk("%s:%d err\n", __func__, __LINE__);
  249. return 0;}
  250. if (!access_ok(VERIFY_WRITE, buffer, count))
  251. return -EFAULT;
  252. spin_lock_irqsave(&hidg->read_spinlock, flags);
  253. #define READ_COND (!list_empty(&hidg->completed_out_req))
  254. /* wait for at least one buffer to complete */
  255. while (!READ_COND) {
  256. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  257. if (file->f_flags & O_NONBLOCK)
  258. return -EAGAIN;
  259. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  260. return -ERESTARTSYS;
  261. spin_lock_irqsave(&hidg->read_spinlock, flags);
  262. }
  263. /* pick the first one */
  264. list = list_first_entry(&hidg->completed_out_req,
  265. struct f_hidg_req_list, list);
  266. /*
  267. * Remove this from list to protect it from beign free()
  268. * while host disables our function
  269. */
  270. list_del(&list->list);
  271. req = list->req;
  272. count = min_t(unsigned int, count, req->actual - list->pos);
  273. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  274. /* copy to user outside spinlock */
  275. count -= copy_to_user(buffer, req->buf + list->pos, count);
  276. list->pos += count;
  277. /*
  278. * if this request is completely handled and transfered to
  279. * userspace, remove its entry from the list and requeue it
  280. * again. Otherwise, we will revisit it again upon the next
  281. * call, taking into account its current read position.
  282. */
  283. if (list->pos == req->actual) {
  284. kfree(list);
  285. req->length = hidg->report_length;
  286. ret = usb_ep_queue(hidg->out_ep, req, GFP_KERNEL);
  287. if (ret < 0) {
  288. free_ep_req(hidg->out_ep, req);
  289. return ret;
  290. }
  291. } else {
  292. spin_lock_irqsave(&hidg->read_spinlock, flags);
  293. list_add(&list->list, &hidg->completed_out_req);
  294. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  295. wake_up(&hidg->read_queue);
  296. }
  297. //printk("%s:%d end\n", __func__, __LINE__);
  298. return count;
  299. }
  300. #else
  301. static ssize_t f_hidg_read(struct file *file, char __user *buffer,
  302. size_t count, loff_t *ptr)
  303. {
  304. struct f_hidg *hidg = file->private_data;
  305. struct f_hidg_req_list *list;
  306. unsigned long flags;
  307. if (!count)
  308. return 0;
  309. if (!access_ok(VERIFY_WRITE, buffer, count))
  310. return -EFAULT;
  311. spin_lock_irqsave(&hidg->read_spinlock, flags);
  312. #define READ_COND (!list_empty(&hidg->completed_out_req))
  313. /* wait for at least one buffer to complete */
  314. while (!READ_COND) {
  315. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  316. if (file->f_flags & O_NONBLOCK)
  317. return -EAGAIN;
  318. if (wait_event_interruptible(hidg->read_queue, READ_COND))
  319. return -ERESTARTSYS;
  320. spin_lock_irqsave(&hidg->read_spinlock, flags);
  321. }
  322. /* pick the first one */
  323. list = list_first_entry(&hidg->completed_out_req,
  324. struct f_hidg_req_list, list);
  325. /*
  326. * Remove this from list to protect it from beign free()
  327. * while host disables our function
  328. */
  329. list_del(&list->list);
  330. count = min_t(unsigned int, count, list->set_report_length - list->pos);
  331. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  332. /* copy to user outside spinlock */
  333. count -= copy_to_user(buffer, list->set_report_buff + list->pos, count);
  334. list->pos += count;
  335. /*
  336. * if this request is completely handled and transfered to
  337. * userspace, remove its entry from the list and requeue it
  338. * again. Otherwise, we will revisit it again upon the next
  339. * call, taking into account its current read position.
  340. */
  341. if (list->pos == list->set_report_length) {
  342. if (NULL != list->set_report_buff)
  343. kfree(list->set_report_buff);
  344. kfree(list);
  345. } else {
  346. spin_lock_irqsave(&hidg->read_spinlock, flags);
  347. list_add(&list->list, &hidg->completed_out_req);
  348. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  349. wake_up(&hidg->read_queue);
  350. }
  351. return count;
  352. }
  353. #endif
  354. static void f_hidg_req_complete(struct usb_ep *ep, struct usb_request *req)
  355. {
  356. struct f_hidg *hidg = (struct f_hidg *)ep->driver_data;
  357. unsigned long flags;
  358. if (req->status != 0) {
  359. ERROR(hidg->func.config->cdev,
  360. "End Point Request ERROR: %d\n", req->status);
  361. }
  362. spin_lock_irqsave(&hidg->write_spinlock, flags);
  363. hidg->write_pending = 0;
  364. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  365. wake_up(&hidg->write_queue);
  366. }
  367. static ssize_t f_hidg_write(struct file *file, const char __user *buffer,
  368. size_t count, loff_t *offp)
  369. {
  370. struct f_hidg *hidg = file->private_data;
  371. struct usb_request *req;
  372. unsigned long flags;
  373. ssize_t status = -ENOMEM;
  374. if (!access_ok(VERIFY_READ, buffer, count))
  375. return -EFAULT;
  376. spin_lock_irqsave(&hidg->write_spinlock, flags);
  377. #define WRITE_COND (!hidg->write_pending)
  378. try_again:
  379. /* write queue */
  380. while (!WRITE_COND) {
  381. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  382. if (file->f_flags & O_NONBLOCK)
  383. return -EAGAIN;
  384. if (wait_event_interruptible_exclusive(
  385. hidg->write_queue, WRITE_COND))
  386. return -ERESTARTSYS;
  387. spin_lock_irqsave(&hidg->write_spinlock, flags);
  388. }
  389. hidg->write_pending = 1;
  390. req = hidg->req;
  391. count = min_t(unsigned, count, hidg->report_length);
  392. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  393. status = copy_from_user(req->buf, buffer, count);
  394. if (status != 0) {
  395. ERROR(hidg->func.config->cdev,
  396. "copy_from_user error\n");
  397. status = -EINVAL;
  398. goto release_write_pending;
  399. }
  400. spin_lock_irqsave(&hidg->write_spinlock, flags);
  401. /* when our function has been disabled by host */
  402. if (!hidg->req) {
  403. free_ep_req(hidg->in_ep, req);
  404. /*
  405. * TODO
  406. * Should we fail with error here?
  407. */
  408. goto try_again;
  409. }
  410. req->status = 0;
  411. req->zero = 0;
  412. req->length = count;
  413. req->complete = f_hidg_req_complete;
  414. req->context = hidg;
  415. status = usb_ep_queue(hidg->in_ep, req, GFP_ATOMIC);
  416. if (status < 0) {
  417. ERROR(hidg->func.config->cdev,
  418. "usb_ep_queue error on int endpoint %zd\n", status);
  419. goto release_write_pending_unlocked;
  420. } else {
  421. status = count;
  422. }
  423. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  424. return status;
  425. release_write_pending:
  426. spin_lock_irqsave(&hidg->write_spinlock, flags);
  427. release_write_pending_unlocked:
  428. hidg->write_pending = 0;
  429. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  430. wake_up(&hidg->write_queue);
  431. return status;
  432. }
  433. static unsigned int f_hidg_poll(struct file *file, poll_table *wait)
  434. {
  435. struct f_hidg *hidg = file->private_data;
  436. unsigned int ret = 0;
  437. poll_wait(file, &hidg->read_queue, wait);
  438. poll_wait(file, &hidg->write_queue, wait);
  439. if (WRITE_COND)
  440. ret |= POLLOUT | POLLWRNORM;
  441. if (READ_COND)
  442. ret |= POLLIN | POLLRDNORM;
  443. return ret;
  444. }
  445. #undef WRITE_COND
  446. #undef READ_COND
  447. static int f_hidg_release(struct inode *inode, struct file *fd)
  448. {
  449. fd->private_data = NULL;
  450. return 0;
  451. }
  452. static int f_hidg_open(struct inode *inode, struct file *fd)
  453. {
  454. struct f_hidg *hidg =
  455. container_of(inode->i_cdev, struct f_hidg, cdev);
  456. fd->private_data = hidg;
  457. return 0;
  458. }
  459. /*-------------------------------------------------------------------------*/
  460. /* usb_function */
  461. static inline struct usb_request *hidg_alloc_ep_req(struct usb_ep *ep,
  462. unsigned length)
  463. {
  464. return alloc_ep_req(ep, length);
  465. }
  466. //#if USE_OUTPUT_EP
  467. static void hidg_set_report_complete(struct usb_ep *ep, struct usb_request *req)
  468. {
  469. struct f_hidg *hidg = (struct f_hidg *) req->context;
  470. struct usb_composite_dev *cdev = hidg->func.config->cdev;
  471. struct f_hidg_req_list *req_list;
  472. unsigned long flags;
  473. switch (req->status) {
  474. case 0:
  475. req_list = kzalloc(sizeof(*req_list), GFP_ATOMIC);
  476. if (!req_list) {
  477. ERROR(cdev, "Unable to allocate mem for req_list\n");
  478. goto free_req;
  479. }
  480. #if USE_OUTPUT_EP
  481. req_list->req = req;
  482. #else
  483. req_list->set_report_buff = kzalloc(req->actual, GFP_ATOMIC);
  484. if (NULL == req_list->set_report_buff) {
  485. ERROR(cdev, "Unable to allocate mem for set_report_buff\n");
  486. goto free_req;
  487. }
  488. req_list->set_report_length = req->actual;
  489. memcpy(req_list->set_report_buff, req->buf, req->actual);
  490. #endif
  491. spin_lock_irqsave(&hidg->read_spinlock, flags);
  492. list_add_tail(&req_list->list, &hidg->completed_out_req);
  493. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  494. wake_up(&hidg->read_queue);
  495. break;
  496. default:
  497. ERROR(cdev, "Set report failed %d\n", req->status);
  498. /* FALLTHROUGH */
  499. case -ECONNABORTED: /* hardware forced ep reset */
  500. case -ECONNRESET: /* request dequeued */
  501. case -ESHUTDOWN: /* disconnect from host */
  502. free_req:
  503. #if USE_OUTPUT_EP
  504. free_ep_req(ep, req);
  505. #endif
  506. return;
  507. }
  508. }
  509. //#endif
  510. #if 0
  511. static bool f_hid_sim_req_match(struct usb_function *f,
  512. const struct usb_ctrlrequest *ctrl,
  513. bool config0)
  514. {
  515. /*printk("###hid req match %02x.%02x v%04x i%04x l%d\n",
  516. ctrl->bRequestType, ctrl->bRequest,
  517. ctrl->wValue, ctrl->wIndex, ctrl->wLength);*/
  518. switch (ctrl->bRequest) {
  519. case 0x53:
  520. if (0xc0 == ctrl->bRequestType)
  521. return true;
  522. case 0x51:
  523. if (0x40 == ctrl->bRequestType)
  524. return true;
  525. default:
  526. return false;
  527. }
  528. return false;
  529. }
  530. #endif
  531. static int hidg_setup(struct usb_function *f,
  532. const struct usb_ctrlrequest *ctrl)
  533. {
  534. struct f_hidg *hidg = func_to_hidg(f);
  535. struct usb_composite_dev *cdev = f->config->cdev;
  536. struct usb_request *req = cdev->req;
  537. int status = 0;
  538. __u16 value, length;
  539. //char result[4] = {0x01, 0x00, 0x00, 0x00};
  540. value = __le16_to_cpu(ctrl->wValue);
  541. length = __le16_to_cpu(ctrl->wLength);
  542. VDBG(cdev,
  543. "%s crtl_request : bRequestType:0x%x bRequest:0x%x Value:0x%x\n",
  544. __func__, ctrl->bRequestType, ctrl->bRequest, value);
  545. switch ((ctrl->bRequestType << 8) | ctrl->bRequest) {
  546. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  547. | HID_REQ_GET_REPORT):
  548. VDBG(cdev, "get_report\n");
  549. printk("get_report\n");
  550. /* send an empty report */
  551. length = min_t(unsigned, length, hidg->report_length);
  552. memset(req->buf, 0x1, length);
  553. goto stall;
  554. break;
  555. case ((USB_DIR_IN | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  556. | HID_REQ_GET_PROTOCOL):
  557. VDBG(cdev, "get_protocol\n");
  558. length = min_t(unsigned int, length, 1);
  559. ((u8 *) req->buf)[0] = hidg->protocol;
  560. goto stall;
  561. break;
  562. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  563. | HID_REQ_SET_REPORT):
  564. VDBG(cdev, "set_report | wLength=%d\n", ctrl->wLength);
  565. //goto stall;
  566. req->complete = hidg_set_report_complete;
  567. req->context = (void*)hidg;
  568. goto respond;
  569. break;
  570. case ((USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE) << 8
  571. | HID_REQ_SET_PROTOCOL):
  572. VDBG(cdev, "set_protocol\n");
  573. if (value > HID_REPORT_PROTOCOL)
  574. goto stall;
  575. length = 0;
  576. /*
  577. * We assume that programs implementing the Boot protocol
  578. * are also compatible with the Report Protocol
  579. */
  580. if (hidg->bInterfaceSubClass == USB_INTERFACE_SUBCLASS_BOOT) {
  581. hidg->protocol = value;
  582. goto respond;
  583. }
  584. goto stall;
  585. break;
  586. case ((USB_DIR_IN | USB_TYPE_STANDARD | USB_RECIP_INTERFACE) << 8
  587. | USB_REQ_GET_DESCRIPTOR):
  588. switch (value >> 8) {
  589. case HID_DT_HID:
  590. {
  591. struct hid_descriptor hidg_desc_copy = hidg_desc;
  592. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: HID\n");
  593. hidg_desc_copy.desc[0].bDescriptorType = HID_DT_REPORT;
  594. hidg_desc_copy.desc[0].wDescriptorLength =
  595. cpu_to_le16(hidg->report_desc_length);
  596. length = min_t(unsigned short, length,
  597. hidg_desc_copy.bLength);
  598. memcpy(req->buf, &hidg_desc_copy, length);
  599. goto respond;
  600. break;
  601. }
  602. case HID_DT_REPORT:
  603. VDBG(cdev, "USB_REQ_GET_DESCRIPTOR: REPORT\n");
  604. length = min_t(unsigned short, length,
  605. hidg->report_desc_length);
  606. memcpy(req->buf, hidg->report_desc, length);
  607. goto respond;
  608. break;
  609. default:
  610. VDBG(cdev, "Unknown descriptor request 0x%x\n",
  611. value >> 8);
  612. goto stall;
  613. break;
  614. }
  615. break;
  616. #if 0
  617. case (0x40 << 8) | 0x51 :
  618. if (ctrl->bRequest != 0x51)
  619. break;
  620. printk("%s %x-->receive switch ctrl\n", __func__, ctrl->bRequest);
  621. value = 0;
  622. usb_dev_ready_notify(1);
  623. goto respond;
  624. break;
  625. case (0xc0 << 8) | 0x53 :
  626. if (ctrl->bRequest != 0x53)
  627. break;
  628. printk("%s %x-->##receive query ctrl\n", __func__, ctrl->bRequest);
  629. length = 4;
  630. memcpy(req->buf, result, length);
  631. goto respond;
  632. break;
  633. #endif
  634. default:
  635. VDBG(cdev, "Unknown request 0x%x\n",
  636. ctrl->bRequest);
  637. goto stall;
  638. break;
  639. }
  640. stall:
  641. return -EOPNOTSUPP;
  642. respond:
  643. req->zero = 0;
  644. req->length = length;
  645. status = usb_ep_queue(cdev->gadget->ep0, req, GFP_ATOMIC);
  646. if (status < 0)
  647. ERROR(cdev, "usb_ep_queue error on ep0 %d\n", value);
  648. return status;
  649. }
  650. static void hidg_disable(struct usb_function *f)
  651. {
  652. struct f_hidg *hidg = func_to_hidg(f);
  653. struct f_hidg_req_list *list, *next;
  654. unsigned long flags;
  655. //usb_dev_ready_notify(0);
  656. usb_ep_disable(hidg->in_ep);
  657. #if USE_OUTPUT_EP
  658. usb_ep_disable(hidg->out_ep);
  659. #endif
  660. spin_lock_irqsave(&hidg->read_spinlock, flags);
  661. list_for_each_entry_safe(list, next, &hidg->completed_out_req, list) {
  662. #if USE_OUTPUT_EP
  663. free_ep_req(hidg->out_ep, list->req);
  664. #else
  665. if (list->set_report_buff)
  666. kfree(list->set_report_buff);
  667. #endif
  668. list_del(&list->list);
  669. kfree(list);
  670. }
  671. spin_unlock_irqrestore(&hidg->read_spinlock, flags);
  672. spin_lock_irqsave(&hidg->write_spinlock, flags);
  673. if (!hidg->write_pending) {
  674. free_ep_req(hidg->in_ep, hidg->req);
  675. hidg->write_pending = 1;
  676. }
  677. hidg->req = NULL;
  678. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  679. #if EP_ASSIGN_AT_CONN
  680. usb_ep_autoconfig_release(hidg->in_ep);
  681. hidg->in_ep = NULL;
  682. #endif
  683. }
  684. static int hidg_set_alt(struct usb_function *f, unsigned intf, unsigned alt)
  685. {
  686. struct usb_composite_dev *cdev = f->config->cdev;
  687. struct f_hidg *hidg = func_to_hidg(f);
  688. struct usb_request *req_in = NULL;
  689. unsigned long flags;
  690. #if USE_OUTPUT_EP
  691. int i, status = 0;
  692. #else
  693. int status = 0;
  694. #endif
  695. VDBG(cdev, "hidg_set_alt intf:%d alt:%d\n", intf, alt);printk("hidg_set_alt intf:%d alt:%d\n", intf, alt);
  696. printk("%s:%d\n", __func__, __LINE__);
  697. #if EP_ASSIGN_AT_CONN
  698. if (hidg->in_ep == NULL) {
  699. struct usb_ep *ep;
  700. ep = usb_ep_autoconfig_ex(f->config->cdev->gadget, &hidg_fs_in_ep_desc, hidg->ep_addr_in);
  701. if (!ep)
  702. goto fail;
  703. hidg->in_ep = ep;
  704. }
  705. #endif
  706. if (hidg->in_ep != NULL) {
  707. /* restart endpoint */
  708. usb_ep_disable(hidg->in_ep);
  709. status = config_ep_by_speed(f->config->cdev->gadget, f,
  710. hidg->in_ep);
  711. if (status) {
  712. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  713. goto fail;
  714. }
  715. status = usb_ep_enable(hidg->in_ep);
  716. if (status < 0) {
  717. ERROR(cdev, "Enable IN endpoint FAILED!\n");
  718. goto fail;
  719. }
  720. hidg->in_ep->driver_data = hidg;
  721. req_in = hidg_alloc_ep_req(hidg->in_ep, hidg->report_length);
  722. if (!req_in) {
  723. status = -ENOMEM;
  724. goto disable_ep_in;
  725. }
  726. usb_dev_ready_notify(2);
  727. }
  728. #if USE_OUTPUT_EP
  729. if (hidg->out_ep != NULL) {
  730. /* restart endpoint */
  731. usb_ep_disable(hidg->out_ep);
  732. status = config_ep_by_speed(f->config->cdev->gadget, f,
  733. hidg->out_ep);
  734. if (status) {
  735. ERROR(cdev, "config_ep_by_speed FAILED!\n");
  736. goto free_req_in;
  737. }
  738. status = usb_ep_enable(hidg->out_ep);
  739. if (status < 0) {
  740. ERROR(cdev, "Enable OUT endpoint FAILED!\n");
  741. goto free_req_in;
  742. }
  743. hidg->out_ep->driver_data = hidg;
  744. /*
  745. * allocate a bunch of read buffers and queue them all at once.
  746. */
  747. for (i = 0; i < hidg->qlen && status == 0; i++) {
  748. struct usb_request *req =
  749. hidg_alloc_ep_req(hidg->out_ep,
  750. hidg->report_length);
  751. if (req) {
  752. req->complete = hidg_set_report_complete;
  753. req->context = hidg;
  754. status = usb_ep_queue(hidg->out_ep, req,
  755. GFP_ATOMIC);
  756. if (status) {
  757. ERROR(cdev, "%s queue req --> %d\n",
  758. hidg->out_ep->name, status);
  759. free_ep_req(hidg->out_ep, req);
  760. }
  761. } else {
  762. status = -ENOMEM;
  763. goto disable_out_ep;
  764. }
  765. }
  766. }
  767. #endif
  768. if (hidg->in_ep != NULL) {
  769. spin_lock_irqsave(&hidg->write_spinlock, flags);
  770. hidg->req = req_in;
  771. hidg->write_pending = 0;
  772. spin_unlock_irqrestore(&hidg->write_spinlock, flags);
  773. wake_up(&hidg->write_queue);
  774. }
  775. return 0;
  776. #if USE_OUTPUT_EP
  777. disable_out_ep:
  778. usb_ep_disable(hidg->out_ep);
  779. free_req_in:
  780. if (req_in)
  781. free_ep_req(hidg->in_ep, req_in);
  782. #endif
  783. disable_ep_in:
  784. if (hidg->in_ep)
  785. usb_ep_disable(hidg->in_ep);
  786. fail:
  787. return status;
  788. }
  789. static const struct file_operations f_hidg_fops = {
  790. .owner = THIS_MODULE,
  791. .open = f_hidg_open,
  792. .release = f_hidg_release,
  793. .write = f_hidg_write,
  794. .read = f_hidg_read,
  795. .poll = f_hidg_poll,
  796. .llseek = noop_llseek,
  797. };
  798. static int hidg_bind(struct usb_configuration *c, struct usb_function *f)
  799. {
  800. #if EP_ASSIGN_AT_CONN == 0
  801. struct usb_ep *ep;
  802. struct usb_ep *ep1;
  803. #endif
  804. struct f_hidg *hidg = func_to_hidg(f);
  805. //struct usb_string *us;
  806. struct device *device;
  807. int status;
  808. dev_t dev;
  809. printk("%s:%d\n", __func__, __LINE__);
  810. /* maybe allocate device-global string IDs, and patch descriptors */
  811. /*us = usb_gstrings_attach(c->cdev, ct_func_strings,
  812. ARRAY_SIZE(ct_func_string_defs));
  813. if (IS_ERR(us))
  814. return PTR_ERR(us);
  815. hidg_interface_desc.iInterface = us[CT_FUNC_HID_IDX].id;*/
  816. /* allocate instance-specific interface IDs, and patch descriptors */
  817. status = usb_interface_id(c, f);
  818. if (status < 0)
  819. goto fail;
  820. hidg_interface_desc.bInterfaceNumber = status;
  821. /* allocate instance-specific endpoints */
  822. status = -ENODEV;
  823. #if EP_ASSIGN_AT_CONN
  824. hidg->ep_addr_in = 0x83;
  825. hidg_fs_in_ep_desc.bEndpointAddress = hidg->ep_addr_in;
  826. #else
  827. ep1 = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  828. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_in_ep_desc);
  829. if (!ep)
  830. goto fail;
  831. hidg->in_ep = ep;
  832. usb_ep_autoconfig_release(ep1);
  833. #endif
  834. #if USE_OUTPUT_EP
  835. ep = usb_ep_autoconfig(c->cdev->gadget, &hidg_fs_out_ep_desc);
  836. if (!ep)
  837. goto fail;
  838. hidg->out_ep = ep;
  839. #endif
  840. /* set descriptor dynamic values */
  841. hidg_interface_desc.bInterfaceSubClass = hidg->bInterfaceSubClass;
  842. hidg_interface_desc.bInterfaceProtocol = hidg->bInterfaceProtocol;
  843. hidg->protocol = HID_REPORT_PROTOCOL;
  844. hidg_ss_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  845. hidg_ss_in_comp_desc.wBytesPerInterval =
  846. cpu_to_le16(hidg->report_length);
  847. hidg_hs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  848. hidg_fs_in_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  849. #if USE_OUTPUT_EP
  850. hidg_ss_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  851. hidg_ss_out_comp_desc.wBytesPerInterval =
  852. cpu_to_le16(hidg->report_length);
  853. hidg_hs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  854. hidg_fs_out_ep_desc.wMaxPacketSize = cpu_to_le16(hidg->report_length);
  855. #endif
  856. /*
  857. * We can use hidg_desc struct here but we should not relay
  858. * that its content won't change after returning from this function.
  859. */
  860. hidg_desc.desc[0].bDescriptorType = HID_DT_REPORT;
  861. hidg_desc.desc[0].wDescriptorLength =
  862. cpu_to_le16(hidg->report_desc_length);
  863. hidg_hs_in_ep_desc.bEndpointAddress =
  864. hidg_fs_in_ep_desc.bEndpointAddress;
  865. #if USE_OUTPUT_EP
  866. hidg_hs_out_ep_desc.bEndpointAddress =
  867. hidg_fs_out_ep_desc.bEndpointAddress;
  868. #endif
  869. hidg_ss_in_ep_desc.bEndpointAddress =
  870. hidg_fs_in_ep_desc.bEndpointAddress;
  871. #if USE_OUTPUT_EP
  872. hidg_ss_out_ep_desc.bEndpointAddress =
  873. hidg_fs_out_ep_desc.bEndpointAddress;
  874. #endif
  875. status = usb_assign_descriptors(f, hidg_fs_descriptors,
  876. hidg_hs_descriptors, hidg_ss_descriptors, NULL);
  877. if (status)
  878. goto fail;
  879. spin_lock_init(&hidg->write_spinlock);
  880. hidg->write_pending = 1;
  881. hidg->req = NULL;
  882. spin_lock_init(&hidg->read_spinlock);
  883. init_waitqueue_head(&hidg->write_queue);
  884. init_waitqueue_head(&hidg->read_queue);
  885. INIT_LIST_HEAD(&hidg->completed_out_req);
  886. /* create char device */
  887. cdev_init(&hidg->cdev, &f_hidg_fops);
  888. dev = MKDEV(major, hidg->minor);
  889. status = cdev_add(&hidg->cdev, dev, 1);
  890. if (status)
  891. goto fail_free_descs;
  892. device = device_create(hidg_class, NULL, dev, NULL,
  893. "%s%d", "hidg", hidg->minor);
  894. if (IS_ERR(device)) {
  895. status = PTR_ERR(device);
  896. goto del;
  897. }
  898. return 0;
  899. del:
  900. cdev_del(&hidg->cdev);
  901. fail_free_descs:
  902. usb_free_all_descriptors(f);
  903. fail:
  904. ERROR(f->config->cdev, "hidg_bind FAILED\n");
  905. if (hidg->req != NULL)
  906. free_ep_req(hidg->in_ep, hidg->req);
  907. return status;
  908. }
  909. static inline int hidg_get_minor(void)
  910. {
  911. int ret;
  912. ret = ida_simple_get(&hidg_ida, 0, 0, GFP_KERNEL);
  913. if (ret >= HIDG_MINORS) {
  914. ida_simple_remove(&hidg_ida, ret);
  915. ret = -ENODEV;
  916. }
  917. return ret;
  918. }
  919. static inline struct f_hid_opts *to_f_hid_opts(struct config_item *item)
  920. {
  921. return container_of(to_config_group(item), struct f_hid_opts,
  922. func_inst.group);
  923. }
  924. static void hid_attr_release(struct config_item *item)
  925. {
  926. struct f_hid_opts *opts = to_f_hid_opts(item);
  927. usb_put_function_instance(&opts->func_inst);
  928. }
  929. static struct configfs_item_operations hidg_item_ops = {
  930. .release = hid_attr_release,
  931. };
  932. #define F_HID_OPT(name, prec, limit) \
  933. static ssize_t f_hid_opts_##name##_show(struct config_item *item, char *page)\
  934. { \
  935. struct f_hid_opts *opts = to_f_hid_opts(item); \
  936. int result; \
  937. \
  938. mutex_lock(&opts->lock); \
  939. result = sprintf(page, "%d\n", opts->name); \
  940. mutex_unlock(&opts->lock); \
  941. \
  942. return result; \
  943. } \
  944. \
  945. static ssize_t f_hid_opts_##name##_store(struct config_item *item, \
  946. const char *page, size_t len) \
  947. { \
  948. struct f_hid_opts *opts = to_f_hid_opts(item); \
  949. int ret; \
  950. u##prec num; \
  951. \
  952. mutex_lock(&opts->lock); \
  953. if (opts->refcnt) { \
  954. ret = -EBUSY; \
  955. goto end; \
  956. } \
  957. \
  958. ret = kstrtou##prec(page, 0, &num); \
  959. if (ret) \
  960. goto end; \
  961. \
  962. if (num > limit) { \
  963. ret = -EINVAL; \
  964. goto end; \
  965. } \
  966. opts->name = num; \
  967. ret = len; \
  968. \
  969. end: \
  970. mutex_unlock(&opts->lock); \
  971. return ret; \
  972. } \
  973. \
  974. CONFIGFS_ATTR(f_hid_opts_, name)
  975. F_HID_OPT(subclass, 8, 255);
  976. F_HID_OPT(protocol, 8, 255);
  977. F_HID_OPT(report_length, 16, 65535);
  978. static ssize_t f_hid_opts_report_desc_show(struct config_item *item, char *page)
  979. {
  980. struct f_hid_opts *opts = to_f_hid_opts(item);
  981. int result;
  982. mutex_lock(&opts->lock);
  983. result = opts->report_desc_length;
  984. memcpy(page, opts->report_desc, opts->report_desc_length);
  985. mutex_unlock(&opts->lock);
  986. return result;
  987. }
  988. static ssize_t f_hid_opts_report_desc_store(struct config_item *item,
  989. const char *page, size_t len)
  990. {
  991. struct f_hid_opts *opts = to_f_hid_opts(item);
  992. int ret = -EBUSY;
  993. char *d;
  994. mutex_lock(&opts->lock);
  995. if (opts->refcnt)
  996. goto end;
  997. if (len > PAGE_SIZE) {
  998. ret = -ENOSPC;
  999. goto end;
  1000. }
  1001. d = kmemdup(page, len, GFP_KERNEL);
  1002. if (!d) {
  1003. ret = -ENOMEM;
  1004. goto end;
  1005. }
  1006. kfree(opts->report_desc);
  1007. opts->report_desc = d;
  1008. opts->report_desc_length = len;
  1009. opts->report_desc_alloc = true;
  1010. ret = len;
  1011. end:
  1012. mutex_unlock(&opts->lock);
  1013. return ret;
  1014. }
  1015. CONFIGFS_ATTR(f_hid_opts_, report_desc);
  1016. static ssize_t f_hid_opts_dev_show(struct config_item *item, char *page)
  1017. {
  1018. struct f_hid_opts *opts = to_f_hid_opts(item);
  1019. return sprintf(page, "%d:%d\n", major, opts->minor);
  1020. }
  1021. CONFIGFS_ATTR_RO(f_hid_opts_, dev);
  1022. static struct configfs_attribute *hid_attrs[] = {
  1023. &f_hid_opts_attr_subclass,
  1024. &f_hid_opts_attr_protocol,
  1025. &f_hid_opts_attr_report_length,
  1026. &f_hid_opts_attr_report_desc,
  1027. &f_hid_opts_attr_dev,
  1028. NULL,
  1029. };
  1030. static struct config_item_type hid_func_type = {
  1031. .ct_item_ops = &hidg_item_ops,
  1032. .ct_attrs = hid_attrs,
  1033. .ct_owner = THIS_MODULE,
  1034. };
  1035. static inline void hidg_put_minor(int minor)
  1036. {
  1037. ida_simple_remove(&hidg_ida, minor);
  1038. }
  1039. static void hidg_free_inst(struct usb_function_instance *f)
  1040. {
  1041. struct f_hid_opts *opts;
  1042. opts = container_of(f, struct f_hid_opts, func_inst);
  1043. mutex_lock(&hidg_ida_lock);
  1044. hidg_put_minor(opts->minor);
  1045. if (ida_is_empty(&hidg_ida))
  1046. ghid_cleanup();
  1047. mutex_unlock(&hidg_ida_lock);
  1048. if (opts->report_desc_alloc)
  1049. kfree(opts->report_desc);
  1050. printk("%s:%d\n", __func__, __LINE__);
  1051. kfree(opts);
  1052. }
  1053. static char g_report_desc[] = {
  1054. 0x06, 0x00, 0xFF, //HID_UsagePageVendor(0)
  1055. 0x09, 0x01, //HID_Usage(1)
  1056. 0xA1, 0x01, //HID_Collection(1) application
  1057. 0x75, 0x08, //HID_ReportSize(8)
  1058. 0x26, 0x80, 0x00, //HID_LogicalMax(128)
  1059. 0x15, 0x00, //HID_LogicalMin(0)
  1060. 0x09, 0x01, //HID_Usage(1)
  1061. 0x85, 0x01, //HID_ReportID(1)
  1062. 0x95, 0x05, //HID_ReportCount(5)
  1063. 0x82, 0x02, 0x01, //input(data, var, abs)
  1064. 0x09, 0x01, //HID_Usage(1)
  1065. 0x85, 0x02, //HID_ReportID(2)
  1066. 0x95, 0x09, //HID_ReportCount(9)
  1067. 0x82, 0x02, 0x01, //input(data, var, abs)
  1068. 0x09, 0x01, //HID_Usage(1)
  1069. 0x85, 0x03, //HID_ReportID(3)
  1070. 0x95, 0x0D, //HID_ReportCount(0x0D)
  1071. 0x82, 0x02, 0x01, //input(data, var, abs)
  1072. 0x09, 0x01, //HID_Usage(1)
  1073. 0x85, 0x04, //HID_ReportID(4)
  1074. 0x95, 0x11, //HID_ReportCount(0x11)
  1075. 0x82, 0x02, 0x01, //input(data, var, abs)
  1076. 0x09, 0x01, //HID_Usage(1)
  1077. 0x85, 0x05, //HID_ReportID(5)
  1078. 0x95, 0x19, //HID_ReportCount(0x19)
  1079. 0x82, 0x02, 0x01, //input(data, var, abs)
  1080. 0x09, 0x01, //HID_Usage(1)
  1081. 0x85, 0x06, //HID_ReportID(6)
  1082. 0x95, 0x31, //HID_ReportCount(0x31)
  1083. 0x82, 0x02, 0x01, //input(data, var, abs)
  1084. 0x09, 0x01, //HID_Usage(1)
  1085. 0x85, 0x07, //HID_ReportID(7)
  1086. 0x95, 0x5F, //HID_ReportCount(0x5F)
  1087. 0x82, 0x02, 0x01, //input(data, var, abs)
  1088. 0x09, 0x01, //HID_Usage(1)
  1089. 0x85, 0x08, //HID_ReportID(8)
  1090. 0x95, 0xC1, //HID_ReportCount(0xC1)
  1091. 0x82, 0x02, 0x01, //input(data, var, abs)
  1092. 0x09, 0x01, //HID_Usage(1)
  1093. 0x85, 0x09, //HID_ReportID(9)
  1094. 0x96, 0x01, 0x01, //HID_ReportCount(0x101)
  1095. 0x82, 0x02, 0x01, //input(data, var, abs)
  1096. 0x09, 0x01, //HID_Usage(1)
  1097. 0x85, 0x0A, //HID_ReportID(0x0A)
  1098. 0x96, 0x81, 0x01, //HID_ReportCount(0x181)
  1099. 0x82, 0x02, 0x01, //input(data, var, abs)
  1100. 0x09, 0x01, //HID_Usage(1)
  1101. 0x85, 0x0B, //HID_ReportID(0x0B)
  1102. 0x96, 0x01, 0x02, //HID_ReportCount(0x201)
  1103. 0x82, 0x02, 0x01, //input(data, var, abs)
  1104. 0x09, 0x01, //HID_Usage(1)
  1105. 0x85, 0x0C, //HID_ReportID(0x0C)
  1106. 0x96, 0xFF, 0x02, //HID_ReportCount(0x2FF)
  1107. 0x82, 0x02, 0x01, //input(data, var, abs)
  1108. 0x09, 0x01, //HID_Usage(1)
  1109. //output
  1110. 0x85, 0x0D, //HID_ReportID(0x0D)
  1111. 0x95, 0x05, //HID_ReportCount(0x05)
  1112. 0x92, 0x02, 0x01, //output(data, var, abs)
  1113. 0x09, 0x01, //HID_Usage(1)
  1114. 0x85, 0x0E, //HID_ReportID(0x0E)
  1115. 0x95, 0x09, //HID_ReportCount(0x09)
  1116. 0x92, 0x02, 0x01, //output(data, var, abs)
  1117. 0x09, 0x01, //HID_Usage(1)
  1118. 0x85, 0x0F, //HID_ReportID(0x0F)
  1119. 0x95, 0x0D, //HID_ReportCount(0x0D)
  1120. 0x92, 0x02, 0x01, //output(data, var, abs)
  1121. 0x09, 0x01, //HID_Usage(1)
  1122. 0x85, 0x10, //HID_ReportID(0x10)
  1123. 0x95, 0x11, //HID_ReportCount(0x11)
  1124. 0x92, 0x02, 0x01, //output(data, var, abs)
  1125. 0x09, 0x01, //HID_Usage(1)
  1126. 0x85, 0x11, //HID_ReportID(0x11)
  1127. 0x95, 0x19, //HID_ReportCount(0x19)
  1128. 0x92, 0x02, 0x01, //output(data, var, abs)
  1129. 0x09, 0x01, //HID_Usage(1)
  1130. 0x85, 0x12, //HID_ReportID(0x12)
  1131. 0x95, 0x31, //HID_ReportCount(0x31)
  1132. 0x92, 0x02, 0x01, //output(data, var, abs)
  1133. 0x09, 0x01, //HID_Usage(1)
  1134. 0x85, 0x13, //HID_ReportID(0x13)
  1135. 0x95, 0x5F, //HID_ReportCount(0x5F)
  1136. 0x92, 0x02, 0x01, //output(data, var, abs)
  1137. 0x09, 0x01, //HID_Usage(1)
  1138. 0x85, 0x14, //HID_ReportID(0x14)
  1139. 0x95, 0xC1, //HID_ReportCount(0xC1)
  1140. 0x92, 0x02, 0x01, //output(data, var, abs)
  1141. 0x09, 0x01, //HID_Usage(1)
  1142. 0x85, 0x15, //HID_ReportID(0x15)
  1143. 0x95, 0xFF, //HID_ReportCount(0xFF)
  1144. 0x92, 0x02, 0x01, //output(data, var, abs)
  1145. 0xC0 //end collection
  1146. };
  1147. static struct usb_function_instance *hidg_alloc_inst(void)
  1148. {
  1149. struct f_hid_opts *opts;
  1150. struct usb_function_instance *ret;
  1151. int status = 0;
  1152. char *desc = NULL;
  1153. printk("%s:%d\n", __func__, __LINE__);
  1154. opts = kzalloc(sizeof(*opts), GFP_KERNEL);
  1155. if (!opts)
  1156. return ERR_PTR(-ENOMEM);
  1157. desc = kmemdup(g_report_desc, sizeof(g_report_desc), GFP_KERNEL);
  1158. if (!desc) {
  1159. ret = ERR_PTR(-ENOMEM);
  1160. goto unlock;
  1161. }
  1162. opts->report_desc = desc;
  1163. opts->report_desc_length = sizeof(g_report_desc);
  1164. opts->report_length = 64;
  1165. mutex_init(&opts->lock);
  1166. opts->func_inst.free_func_inst = hidg_free_inst;
  1167. ret = &opts->func_inst;
  1168. mutex_lock(&hidg_ida_lock);
  1169. if (ida_is_empty(&hidg_ida)) {
  1170. status = ghid_setup(NULL, HIDG_MINORS);
  1171. if (status) {
  1172. ret = ERR_PTR(status);
  1173. kfree(opts);
  1174. goto unlock;
  1175. }
  1176. }
  1177. opts->minor = hidg_get_minor();
  1178. if (opts->minor < 0) {
  1179. ret = ERR_PTR(opts->minor);
  1180. kfree(opts);
  1181. if (ida_is_empty(&hidg_ida))
  1182. ghid_cleanup();
  1183. goto unlock;
  1184. }
  1185. config_group_init_type_name(&opts->func_inst.group, "", &hid_func_type);
  1186. unlock:
  1187. mutex_unlock(&hidg_ida_lock);
  1188. return ret;
  1189. }
  1190. static void hidg_free(struct usb_function *f)
  1191. {
  1192. struct f_hidg *hidg;
  1193. struct f_hid_opts *opts;
  1194. printk("%s:%d\n", __func__, __LINE__);
  1195. hidg = func_to_hidg(f);
  1196. opts = container_of(f->fi, struct f_hid_opts, func_inst);
  1197. kfree(hidg->report_desc);
  1198. kfree(hidg);
  1199. mutex_lock(&opts->lock);
  1200. --opts->refcnt;
  1201. mutex_unlock(&opts->lock);
  1202. }
  1203. static void hidg_unbind(struct usb_configuration *c, struct usb_function *f)
  1204. {
  1205. struct f_hidg *hidg = func_to_hidg(f);
  1206. printk("%s:%d\n", __func__, __LINE__);
  1207. device_destroy(hidg_class, MKDEV(major, hidg->minor));
  1208. cdev_del(&hidg->cdev);
  1209. usb_free_all_descriptors(f);
  1210. }
  1211. static struct usb_function *hidg_alloc(struct usb_function_instance *fi)
  1212. {
  1213. struct f_hidg *hidg;
  1214. struct f_hid_opts *opts;
  1215. /* allocate and initialize one new instance */
  1216. hidg = kzalloc(sizeof(*hidg), GFP_KERNEL);
  1217. if (!hidg)
  1218. return ERR_PTR(-ENOMEM);
  1219. printk("%s:%d\n", __func__, __LINE__);
  1220. opts = container_of(fi, struct f_hid_opts, func_inst);
  1221. mutex_lock(&opts->lock);
  1222. ++opts->refcnt;
  1223. hidg->minor = opts->minor;
  1224. hidg->bInterfaceSubClass = opts->subclass;
  1225. hidg->bInterfaceProtocol = opts->protocol;
  1226. hidg->report_length = opts->report_length;
  1227. hidg->report_desc_length = opts->report_desc_length;
  1228. if (opts->report_desc) {
  1229. hidg->report_desc = kmemdup(opts->report_desc,
  1230. opts->report_desc_length,
  1231. GFP_KERNEL);
  1232. if (!hidg->report_desc) {
  1233. kfree(hidg);
  1234. mutex_unlock(&opts->lock);
  1235. return ERR_PTR(-ENOMEM);
  1236. }
  1237. }
  1238. mutex_unlock(&opts->lock);
  1239. hidg->func.name = "hid";
  1240. hidg->func.bind = hidg_bind;
  1241. hidg->func.unbind = hidg_unbind;
  1242. hidg->func.set_alt = hidg_set_alt;
  1243. hidg->func.disable = hidg_disable;
  1244. hidg->func.setup = hidg_setup;
  1245. hidg->func.free_func = hidg_free;
  1246. //hidg->func.req_match = f_hid_sim_req_match;
  1247. /* this could me made configurable at some point */
  1248. hidg->qlen = 4;
  1249. return &hidg->func;
  1250. }
  1251. DECLARE_USB_FUNCTION_INIT(hid, hidg_alloc_inst, hidg_alloc);
  1252. MODULE_LICENSE("GPL");
  1253. MODULE_AUTHOR("Fabien Chouteau");
  1254. int ghid_setup(struct usb_gadget *g, int count)
  1255. {
  1256. int status;
  1257. dev_t dev;
  1258. hidg_class = class_create(THIS_MODULE, "hidg");
  1259. if (IS_ERR(hidg_class)) {
  1260. status = PTR_ERR(hidg_class);
  1261. hidg_class = NULL;
  1262. return status;
  1263. }
  1264. status = alloc_chrdev_region(&dev, 0, count, "hidg");
  1265. if (status) {
  1266. class_destroy(hidg_class);
  1267. hidg_class = NULL;
  1268. return status;
  1269. }
  1270. major = MAJOR(dev);
  1271. minors = count;
  1272. return 0;
  1273. }
  1274. void ghid_cleanup(void)
  1275. {
  1276. if (major) {
  1277. unregister_chrdev_region(MKDEV(major, 0), minors);
  1278. major = minors = 0;
  1279. }
  1280. class_destroy(hidg_class);
  1281. hidg_class = NULL;
  1282. }