f_hid.c 34 KB

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