inode.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * inode.c -- user mode filesystem api for usb gadget controllers
  4. *
  5. * Copyright (C) 2003-2004 David Brownell
  6. * Copyright (C) 2003 Agilent Technologies
  7. */
  8. /* #define VERBOSE_DEBUG */
  9. #include <linux/init.h>
  10. #include <linux/module.h>
  11. #include <linux/fs.h>
  12. #include <linux/fs_context.h>
  13. #include <linux/pagemap.h>
  14. #include <linux/uts.h>
  15. #include <linux/wait.h>
  16. #include <linux/compiler.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/sched.h>
  19. #include <linux/slab.h>
  20. #include <linux/poll.h>
  21. #include <linux/kthread.h>
  22. #include <linux/aio.h>
  23. #include <linux/uio.h>
  24. #include <linux/refcount.h>
  25. #include <linux/delay.h>
  26. #include <linux/device.h>
  27. #include <linux/moduleparam.h>
  28. #include <linux/usb/gadgetfs.h>
  29. #include <linux/usb/gadget.h>
  30. #include <linux/usb/composite.h> /* for USB_GADGET_DELAYED_STATUS */
  31. /* Undef helpers from linux/usb/composite.h as gadgetfs redefines them */
  32. #undef DBG
  33. #undef ERROR
  34. #undef INFO
  35. /*
  36. * The gadgetfs API maps each endpoint to a file descriptor so that you
  37. * can use standard synchronous read/write calls for I/O. There's some
  38. * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
  39. * drivers show how this works in practice. You can also use AIO to
  40. * eliminate I/O gaps between requests, to help when streaming data.
  41. *
  42. * Key parts that must be USB-specific are protocols defining how the
  43. * read/write operations relate to the hardware state machines. There
  44. * are two types of files. One type is for the device, implementing ep0.
  45. * The other type is for each IN or OUT endpoint. In both cases, the
  46. * user mode driver must configure the hardware before using it.
  47. *
  48. * - First, dev_config() is called when /dev/gadget/$CHIP is configured
  49. * (by writing configuration and device descriptors). Afterwards it
  50. * may serve as a source of device events, used to handle all control
  51. * requests other than basic enumeration.
  52. *
  53. * - Then, after a SET_CONFIGURATION control request, ep_config() is
  54. * called when each /dev/gadget/ep* file is configured (by writing
  55. * endpoint descriptors). Afterwards these files are used to write()
  56. * IN data or to read() OUT data. To halt the endpoint, a "wrong
  57. * direction" request is issued (like reading an IN endpoint).
  58. *
  59. * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
  60. * not possible on all hardware. For example, precise fault handling with
  61. * respect to data left in endpoint fifos after aborted operations; or
  62. * selective clearing of endpoint halts, to implement SET_INTERFACE.
  63. */
  64. #define DRIVER_DESC "USB Gadget filesystem"
  65. #define DRIVER_VERSION "24 Aug 2004"
  66. static const char driver_desc [] = DRIVER_DESC;
  67. static const char shortname [] = "gadgetfs";
  68. MODULE_DESCRIPTION (DRIVER_DESC);
  69. MODULE_AUTHOR ("David Brownell");
  70. MODULE_LICENSE ("GPL");
  71. static int ep_open(struct inode *, struct file *);
  72. /*----------------------------------------------------------------------*/
  73. #define GADGETFS_MAGIC 0xaee71ee7
  74. /* /dev/gadget/$CHIP represents ep0 and the whole device */
  75. enum ep0_state {
  76. /* DISABLED is the initial state. */
  77. STATE_DEV_DISABLED = 0,
  78. /* Only one open() of /dev/gadget/$CHIP; only one file tracks
  79. * ep0/device i/o modes and binding to the controller. Driver
  80. * must always write descriptors to initialize the device, then
  81. * the device becomes UNCONNECTED until enumeration.
  82. */
  83. STATE_DEV_OPENED,
  84. /* From then on, ep0 fd is in either of two basic modes:
  85. * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
  86. * - SETUP: read/write will transfer control data and succeed;
  87. * or if "wrong direction", performs protocol stall
  88. */
  89. STATE_DEV_UNCONNECTED,
  90. STATE_DEV_CONNECTED,
  91. STATE_DEV_SETUP,
  92. /* UNBOUND means the driver closed ep0, so the device won't be
  93. * accessible again (DEV_DISABLED) until all fds are closed.
  94. */
  95. STATE_DEV_UNBOUND,
  96. };
  97. /* enough for the whole queue: most events invalidate others */
  98. #define N_EVENT 5
  99. #define RBUF_SIZE 256
  100. struct dev_data {
  101. spinlock_t lock;
  102. refcount_t count;
  103. int udc_usage;
  104. enum ep0_state state; /* P: lock */
  105. struct usb_gadgetfs_event event [N_EVENT];
  106. unsigned ev_next;
  107. struct fasync_struct *fasync;
  108. u8 current_config;
  109. /* drivers reading ep0 MUST handle control requests (SETUP)
  110. * reported that way; else the host will time out.
  111. */
  112. unsigned usermode_setup : 1,
  113. setup_in : 1,
  114. setup_can_stall : 1,
  115. setup_out_ready : 1,
  116. setup_out_error : 1,
  117. setup_abort : 1,
  118. gadget_registered : 1;
  119. unsigned setup_wLength;
  120. /* the rest is basically write-once */
  121. struct usb_config_descriptor *config, *hs_config;
  122. struct usb_device_descriptor *dev;
  123. struct usb_request *req;
  124. struct usb_gadget *gadget;
  125. struct list_head epfiles;
  126. void *buf;
  127. wait_queue_head_t wait;
  128. struct super_block *sb;
  129. struct dentry *dentry;
  130. /* except this scratch i/o buffer for ep0 */
  131. u8 rbuf[RBUF_SIZE];
  132. };
  133. static inline void get_dev (struct dev_data *data)
  134. {
  135. refcount_inc (&data->count);
  136. }
  137. static void put_dev (struct dev_data *data)
  138. {
  139. if (likely (!refcount_dec_and_test (&data->count)))
  140. return;
  141. /* needs no more cleanup */
  142. BUG_ON (waitqueue_active (&data->wait));
  143. kfree (data);
  144. }
  145. static struct dev_data *dev_new (void)
  146. {
  147. struct dev_data *dev;
  148. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  149. if (!dev)
  150. return NULL;
  151. dev->state = STATE_DEV_DISABLED;
  152. refcount_set (&dev->count, 1);
  153. spin_lock_init (&dev->lock);
  154. INIT_LIST_HEAD (&dev->epfiles);
  155. init_waitqueue_head (&dev->wait);
  156. return dev;
  157. }
  158. /*----------------------------------------------------------------------*/
  159. /* other /dev/gadget/$ENDPOINT files represent endpoints */
  160. enum ep_state {
  161. STATE_EP_DISABLED = 0,
  162. STATE_EP_READY,
  163. STATE_EP_ENABLED,
  164. STATE_EP_UNBOUND,
  165. };
  166. struct ep_data {
  167. struct mutex lock;
  168. enum ep_state state;
  169. refcount_t count;
  170. struct dev_data *dev;
  171. /* must hold dev->lock before accessing ep or req */
  172. struct usb_ep *ep;
  173. struct usb_request *req;
  174. ssize_t status;
  175. char name [16];
  176. struct usb_endpoint_descriptor desc, hs_desc;
  177. struct list_head epfiles;
  178. wait_queue_head_t wait;
  179. struct dentry *dentry;
  180. };
  181. static inline void get_ep (struct ep_data *data)
  182. {
  183. refcount_inc (&data->count);
  184. }
  185. static void put_ep (struct ep_data *data)
  186. {
  187. if (likely (!refcount_dec_and_test (&data->count)))
  188. return;
  189. put_dev (data->dev);
  190. /* needs no more cleanup */
  191. BUG_ON (!list_empty (&data->epfiles));
  192. BUG_ON (waitqueue_active (&data->wait));
  193. kfree (data);
  194. }
  195. /*----------------------------------------------------------------------*/
  196. /* most "how to use the hardware" policy choices are in userspace:
  197. * mapping endpoint roles (which the driver needs) to the capabilities
  198. * which the usb controller has. most of those capabilities are exposed
  199. * implicitly, starting with the driver name and then endpoint names.
  200. */
  201. static const char *CHIP;
  202. static DEFINE_MUTEX(sb_mutex); /* Serialize superblock operations */
  203. /*----------------------------------------------------------------------*/
  204. /* NOTE: don't use dev_printk calls before binding to the gadget
  205. * at the end of ep0 configuration, or after unbind.
  206. */
  207. /* too wordy: dev_printk(level , &(d)->gadget->dev , fmt , ## args) */
  208. #define xprintk(d,level,fmt,args...) \
  209. printk(level "%s: " fmt , shortname , ## args)
  210. #ifdef DEBUG
  211. #define DBG(dev,fmt,args...) \
  212. xprintk(dev , KERN_DEBUG , fmt , ## args)
  213. #else
  214. #define DBG(dev,fmt,args...) \
  215. do { } while (0)
  216. #endif /* DEBUG */
  217. #ifdef VERBOSE_DEBUG
  218. #define VDEBUG DBG
  219. #else
  220. #define VDEBUG(dev,fmt,args...) \
  221. do { } while (0)
  222. #endif /* DEBUG */
  223. #define ERROR(dev,fmt,args...) \
  224. xprintk(dev , KERN_ERR , fmt , ## args)
  225. #define INFO(dev,fmt,args...) \
  226. xprintk(dev , KERN_INFO , fmt , ## args)
  227. /*----------------------------------------------------------------------*/
  228. /* SYNCHRONOUS ENDPOINT OPERATIONS (bulk/intr/iso)
  229. *
  230. * After opening, configure non-control endpoints. Then use normal
  231. * stream read() and write() requests; and maybe ioctl() to get more
  232. * precise FIFO status when recovering from cancellation.
  233. */
  234. static void epio_complete (struct usb_ep *ep, struct usb_request *req)
  235. {
  236. struct ep_data *epdata = ep->driver_data;
  237. if (!req->context)
  238. return;
  239. if (req->status)
  240. epdata->status = req->status;
  241. else
  242. epdata->status = req->actual;
  243. complete ((struct completion *)req->context);
  244. }
  245. /* tasklock endpoint, returning when it's connected.
  246. * still need dev->lock to use epdata->ep.
  247. */
  248. static int
  249. get_ready_ep (unsigned f_flags, struct ep_data *epdata, bool is_write)
  250. {
  251. int val;
  252. if (f_flags & O_NONBLOCK) {
  253. if (!mutex_trylock(&epdata->lock))
  254. goto nonblock;
  255. if (epdata->state != STATE_EP_ENABLED &&
  256. (!is_write || epdata->state != STATE_EP_READY)) {
  257. mutex_unlock(&epdata->lock);
  258. nonblock:
  259. val = -EAGAIN;
  260. } else
  261. val = 0;
  262. return val;
  263. }
  264. val = mutex_lock_interruptible(&epdata->lock);
  265. if (val < 0)
  266. return val;
  267. switch (epdata->state) {
  268. case STATE_EP_ENABLED:
  269. return 0;
  270. case STATE_EP_READY: /* not configured yet */
  271. if (is_write)
  272. return 0;
  273. fallthrough;
  274. case STATE_EP_UNBOUND: /* clean disconnect */
  275. break;
  276. // case STATE_EP_DISABLED: /* "can't happen" */
  277. default: /* error! */
  278. pr_debug ("%s: ep %p not available, state %d\n",
  279. shortname, epdata, epdata->state);
  280. }
  281. mutex_unlock(&epdata->lock);
  282. return -ENODEV;
  283. }
  284. static ssize_t
  285. ep_io (struct ep_data *epdata, void *buf, unsigned len)
  286. {
  287. DECLARE_COMPLETION_ONSTACK (done);
  288. int value;
  289. spin_lock_irq (&epdata->dev->lock);
  290. if (likely (epdata->ep != NULL)) {
  291. struct usb_request *req = epdata->req;
  292. req->context = &done;
  293. req->complete = epio_complete;
  294. req->buf = buf;
  295. req->length = len;
  296. value = usb_ep_queue (epdata->ep, req, GFP_ATOMIC);
  297. } else
  298. value = -ENODEV;
  299. spin_unlock_irq (&epdata->dev->lock);
  300. if (likely (value == 0)) {
  301. value = wait_for_completion_interruptible(&done);
  302. if (value != 0) {
  303. spin_lock_irq (&epdata->dev->lock);
  304. if (likely (epdata->ep != NULL)) {
  305. DBG (epdata->dev, "%s i/o interrupted\n",
  306. epdata->name);
  307. usb_ep_dequeue (epdata->ep, epdata->req);
  308. spin_unlock_irq (&epdata->dev->lock);
  309. wait_for_completion(&done);
  310. if (epdata->status == -ECONNRESET)
  311. epdata->status = -EINTR;
  312. } else {
  313. spin_unlock_irq (&epdata->dev->lock);
  314. DBG (epdata->dev, "endpoint gone\n");
  315. wait_for_completion(&done);
  316. epdata->status = -ENODEV;
  317. }
  318. }
  319. return epdata->status;
  320. }
  321. return value;
  322. }
  323. static int
  324. ep_release (struct inode *inode, struct file *fd)
  325. {
  326. struct ep_data *data = fd->private_data;
  327. int value;
  328. value = mutex_lock_interruptible(&data->lock);
  329. if (value < 0)
  330. return value;
  331. /* clean up if this can be reopened */
  332. if (data->state != STATE_EP_UNBOUND) {
  333. data->state = STATE_EP_DISABLED;
  334. data->desc.bDescriptorType = 0;
  335. data->hs_desc.bDescriptorType = 0;
  336. usb_ep_disable(data->ep);
  337. }
  338. mutex_unlock(&data->lock);
  339. put_ep (data);
  340. return 0;
  341. }
  342. static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
  343. {
  344. struct ep_data *data = fd->private_data;
  345. int status;
  346. if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
  347. return status;
  348. spin_lock_irq (&data->dev->lock);
  349. if (likely (data->ep != NULL)) {
  350. switch (code) {
  351. case GADGETFS_FIFO_STATUS:
  352. status = usb_ep_fifo_status (data->ep);
  353. break;
  354. case GADGETFS_FIFO_FLUSH:
  355. usb_ep_fifo_flush (data->ep);
  356. break;
  357. case GADGETFS_CLEAR_HALT:
  358. status = usb_ep_clear_halt (data->ep);
  359. break;
  360. default:
  361. status = -ENOTTY;
  362. }
  363. } else
  364. status = -ENODEV;
  365. spin_unlock_irq (&data->dev->lock);
  366. mutex_unlock(&data->lock);
  367. return status;
  368. }
  369. /*----------------------------------------------------------------------*/
  370. /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
  371. struct kiocb_priv {
  372. struct usb_request *req;
  373. struct ep_data *epdata;
  374. struct kiocb *iocb;
  375. struct mm_struct *mm;
  376. struct work_struct work;
  377. void *buf;
  378. struct iov_iter to;
  379. const void *to_free;
  380. unsigned actual;
  381. };
  382. static int ep_aio_cancel(struct kiocb *iocb)
  383. {
  384. struct kiocb_priv *priv = iocb->private;
  385. struct ep_data *epdata;
  386. int value;
  387. local_irq_disable();
  388. epdata = priv->epdata;
  389. // spin_lock(&epdata->dev->lock);
  390. if (likely(epdata && epdata->ep && priv->req))
  391. value = usb_ep_dequeue (epdata->ep, priv->req);
  392. else
  393. value = -EINVAL;
  394. // spin_unlock(&epdata->dev->lock);
  395. local_irq_enable();
  396. return value;
  397. }
  398. static void ep_user_copy_worker(struct work_struct *work)
  399. {
  400. struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
  401. struct mm_struct *mm = priv->mm;
  402. struct kiocb *iocb = priv->iocb;
  403. size_t ret;
  404. kthread_use_mm(mm);
  405. ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
  406. kthread_unuse_mm(mm);
  407. if (!ret)
  408. ret = -EFAULT;
  409. /* completing the iocb can drop the ctx and mm, don't touch mm after */
  410. iocb->ki_complete(iocb, ret);
  411. kfree(priv->buf);
  412. kfree(priv->to_free);
  413. kfree(priv);
  414. }
  415. static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
  416. {
  417. struct kiocb *iocb = req->context;
  418. struct kiocb_priv *priv = iocb->private;
  419. struct ep_data *epdata = priv->epdata;
  420. /* lock against disconnect (and ideally, cancel) */
  421. spin_lock(&epdata->dev->lock);
  422. priv->req = NULL;
  423. priv->epdata = NULL;
  424. /* if this was a write or a read returning no data then we
  425. * don't need to copy anything to userspace, so we can
  426. * complete the aio request immediately.
  427. */
  428. if (priv->to_free == NULL || unlikely(req->actual == 0)) {
  429. kfree(req->buf);
  430. kfree(priv->to_free);
  431. kfree(priv);
  432. iocb->private = NULL;
  433. iocb->ki_complete(iocb,
  434. req->actual ? req->actual : (long)req->status);
  435. } else {
  436. /* ep_copy_to_user() won't report both; we hide some faults */
  437. if (unlikely(0 != req->status))
  438. DBG(epdata->dev, "%s fault %d len %d\n",
  439. ep->name, req->status, req->actual);
  440. priv->buf = req->buf;
  441. priv->actual = req->actual;
  442. INIT_WORK(&priv->work, ep_user_copy_worker);
  443. schedule_work(&priv->work);
  444. }
  445. usb_ep_free_request(ep, req);
  446. spin_unlock(&epdata->dev->lock);
  447. put_ep(epdata);
  448. }
  449. static ssize_t ep_aio(struct kiocb *iocb,
  450. struct kiocb_priv *priv,
  451. struct ep_data *epdata,
  452. char *buf,
  453. size_t len)
  454. {
  455. struct usb_request *req;
  456. ssize_t value;
  457. iocb->private = priv;
  458. priv->iocb = iocb;
  459. kiocb_set_cancel_fn(iocb, ep_aio_cancel);
  460. get_ep(epdata);
  461. priv->epdata = epdata;
  462. priv->actual = 0;
  463. priv->mm = current->mm; /* mm teardown waits for iocbs in exit_aio() */
  464. /* each kiocb is coupled to one usb_request, but we can't
  465. * allocate or submit those if the host disconnected.
  466. */
  467. spin_lock_irq(&epdata->dev->lock);
  468. value = -ENODEV;
  469. if (unlikely(epdata->ep == NULL))
  470. goto fail;
  471. req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
  472. value = -ENOMEM;
  473. if (unlikely(!req))
  474. goto fail;
  475. priv->req = req;
  476. req->buf = buf;
  477. req->length = len;
  478. req->complete = ep_aio_complete;
  479. req->context = iocb;
  480. value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
  481. if (unlikely(0 != value)) {
  482. usb_ep_free_request(epdata->ep, req);
  483. goto fail;
  484. }
  485. spin_unlock_irq(&epdata->dev->lock);
  486. return -EIOCBQUEUED;
  487. fail:
  488. spin_unlock_irq(&epdata->dev->lock);
  489. kfree(priv->to_free);
  490. kfree(priv);
  491. put_ep(epdata);
  492. return value;
  493. }
  494. static ssize_t
  495. ep_read_iter(struct kiocb *iocb, struct iov_iter *to)
  496. {
  497. struct file *file = iocb->ki_filp;
  498. struct ep_data *epdata = file->private_data;
  499. size_t len = iov_iter_count(to);
  500. ssize_t value;
  501. char *buf;
  502. if ((value = get_ready_ep(file->f_flags, epdata, false)) < 0)
  503. return value;
  504. /* halt any endpoint by doing a "wrong direction" i/o call */
  505. if (usb_endpoint_dir_in(&epdata->desc)) {
  506. if (usb_endpoint_xfer_isoc(&epdata->desc) ||
  507. !is_sync_kiocb(iocb)) {
  508. mutex_unlock(&epdata->lock);
  509. return -EINVAL;
  510. }
  511. DBG (epdata->dev, "%s halt\n", epdata->name);
  512. spin_lock_irq(&epdata->dev->lock);
  513. if (likely(epdata->ep != NULL))
  514. usb_ep_set_halt(epdata->ep);
  515. spin_unlock_irq(&epdata->dev->lock);
  516. mutex_unlock(&epdata->lock);
  517. return -EBADMSG;
  518. }
  519. buf = kmalloc(len, GFP_KERNEL);
  520. if (unlikely(!buf)) {
  521. mutex_unlock(&epdata->lock);
  522. return -ENOMEM;
  523. }
  524. if (is_sync_kiocb(iocb)) {
  525. value = ep_io(epdata, buf, len);
  526. if (value >= 0 && (copy_to_iter(buf, value, to) != value))
  527. value = -EFAULT;
  528. } else {
  529. struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
  530. value = -ENOMEM;
  531. if (!priv)
  532. goto fail;
  533. priv->to_free = dup_iter(&priv->to, to, GFP_KERNEL);
  534. if (!iter_is_ubuf(&priv->to) && !priv->to_free) {
  535. kfree(priv);
  536. goto fail;
  537. }
  538. value = ep_aio(iocb, priv, epdata, buf, len);
  539. if (value == -EIOCBQUEUED)
  540. buf = NULL;
  541. }
  542. fail:
  543. kfree(buf);
  544. mutex_unlock(&epdata->lock);
  545. return value;
  546. }
  547. static ssize_t ep_config(struct ep_data *, const char *, size_t);
  548. static ssize_t
  549. ep_write_iter(struct kiocb *iocb, struct iov_iter *from)
  550. {
  551. struct file *file = iocb->ki_filp;
  552. struct ep_data *epdata = file->private_data;
  553. size_t len = iov_iter_count(from);
  554. bool configured;
  555. ssize_t value;
  556. char *buf;
  557. if ((value = get_ready_ep(file->f_flags, epdata, true)) < 0)
  558. return value;
  559. configured = epdata->state == STATE_EP_ENABLED;
  560. /* halt any endpoint by doing a "wrong direction" i/o call */
  561. if (configured && !usb_endpoint_dir_in(&epdata->desc)) {
  562. if (usb_endpoint_xfer_isoc(&epdata->desc) ||
  563. !is_sync_kiocb(iocb)) {
  564. mutex_unlock(&epdata->lock);
  565. return -EINVAL;
  566. }
  567. DBG (epdata->dev, "%s halt\n", epdata->name);
  568. spin_lock_irq(&epdata->dev->lock);
  569. if (likely(epdata->ep != NULL))
  570. usb_ep_set_halt(epdata->ep);
  571. spin_unlock_irq(&epdata->dev->lock);
  572. mutex_unlock(&epdata->lock);
  573. return -EBADMSG;
  574. }
  575. buf = kmalloc(len, GFP_KERNEL);
  576. if (unlikely(!buf)) {
  577. mutex_unlock(&epdata->lock);
  578. return -ENOMEM;
  579. }
  580. if (unlikely(!copy_from_iter_full(buf, len, from))) {
  581. value = -EFAULT;
  582. goto out;
  583. }
  584. if (unlikely(!configured)) {
  585. value = ep_config(epdata, buf, len);
  586. } else if (is_sync_kiocb(iocb)) {
  587. value = ep_io(epdata, buf, len);
  588. } else {
  589. struct kiocb_priv *priv = kzalloc(sizeof *priv, GFP_KERNEL);
  590. value = -ENOMEM;
  591. if (priv) {
  592. value = ep_aio(iocb, priv, epdata, buf, len);
  593. if (value == -EIOCBQUEUED)
  594. buf = NULL;
  595. }
  596. }
  597. out:
  598. kfree(buf);
  599. mutex_unlock(&epdata->lock);
  600. return value;
  601. }
  602. /*----------------------------------------------------------------------*/
  603. /* used after endpoint configuration */
  604. static const struct file_operations ep_io_operations = {
  605. .owner = THIS_MODULE,
  606. .open = ep_open,
  607. .release = ep_release,
  608. .unlocked_ioctl = ep_ioctl,
  609. .read_iter = ep_read_iter,
  610. .write_iter = ep_write_iter,
  611. };
  612. /* ENDPOINT INITIALIZATION
  613. *
  614. * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
  615. * status = write (fd, descriptors, sizeof descriptors)
  616. *
  617. * That write establishes the endpoint configuration, configuring
  618. * the controller to process bulk, interrupt, or isochronous transfers
  619. * at the right maxpacket size, and so on.
  620. *
  621. * The descriptors are message type 1, identified by a host order u32
  622. * at the beginning of what's written. Descriptor order is: full/low
  623. * speed descriptor, then optional high speed descriptor.
  624. */
  625. static ssize_t
  626. ep_config (struct ep_data *data, const char *buf, size_t len)
  627. {
  628. struct usb_ep *ep;
  629. u32 tag;
  630. int value, length = len;
  631. if (data->state != STATE_EP_READY) {
  632. value = -EL2HLT;
  633. goto fail;
  634. }
  635. value = len;
  636. if (len < USB_DT_ENDPOINT_SIZE + 4)
  637. goto fail0;
  638. /* we might need to change message format someday */
  639. memcpy(&tag, buf, 4);
  640. if (tag != 1) {
  641. DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
  642. goto fail0;
  643. }
  644. buf += 4;
  645. len -= 4;
  646. /* NOTE: audio endpoint extensions not accepted here;
  647. * just don't include the extra bytes.
  648. */
  649. /* full/low speed descriptor, then high speed */
  650. memcpy(&data->desc, buf, USB_DT_ENDPOINT_SIZE);
  651. if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
  652. || data->desc.bDescriptorType != USB_DT_ENDPOINT)
  653. goto fail0;
  654. if (len != USB_DT_ENDPOINT_SIZE) {
  655. if (len != 2 * USB_DT_ENDPOINT_SIZE)
  656. goto fail0;
  657. memcpy(&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
  658. USB_DT_ENDPOINT_SIZE);
  659. if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
  660. || data->hs_desc.bDescriptorType
  661. != USB_DT_ENDPOINT) {
  662. DBG(data->dev, "config %s, bad hs length or type\n",
  663. data->name);
  664. goto fail0;
  665. }
  666. }
  667. spin_lock_irq (&data->dev->lock);
  668. if (data->dev->state == STATE_DEV_UNBOUND) {
  669. value = -ENOENT;
  670. goto gone;
  671. } else {
  672. ep = data->ep;
  673. if (ep == NULL) {
  674. value = -ENODEV;
  675. goto gone;
  676. }
  677. }
  678. switch (data->dev->gadget->speed) {
  679. case USB_SPEED_LOW:
  680. case USB_SPEED_FULL:
  681. ep->desc = &data->desc;
  682. break;
  683. case USB_SPEED_HIGH:
  684. /* fails if caller didn't provide that descriptor... */
  685. ep->desc = &data->hs_desc;
  686. break;
  687. default:
  688. DBG(data->dev, "unconnected, %s init abandoned\n",
  689. data->name);
  690. value = -EINVAL;
  691. goto gone;
  692. }
  693. value = usb_ep_enable(ep);
  694. if (value == 0) {
  695. data->state = STATE_EP_ENABLED;
  696. value = length;
  697. }
  698. gone:
  699. spin_unlock_irq (&data->dev->lock);
  700. if (value < 0) {
  701. fail:
  702. data->desc.bDescriptorType = 0;
  703. data->hs_desc.bDescriptorType = 0;
  704. }
  705. return value;
  706. fail0:
  707. value = -EINVAL;
  708. goto fail;
  709. }
  710. static int
  711. ep_open (struct inode *inode, struct file *fd)
  712. {
  713. struct ep_data *data = inode->i_private;
  714. int value = -EBUSY;
  715. if (mutex_lock_interruptible(&data->lock) != 0)
  716. return -EINTR;
  717. spin_lock_irq (&data->dev->lock);
  718. if (data->dev->state == STATE_DEV_UNBOUND)
  719. value = -ENOENT;
  720. else if (data->state == STATE_EP_DISABLED) {
  721. value = 0;
  722. data->state = STATE_EP_READY;
  723. get_ep (data);
  724. fd->private_data = data;
  725. VDEBUG (data->dev, "%s ready\n", data->name);
  726. } else
  727. DBG (data->dev, "%s state %d\n",
  728. data->name, data->state);
  729. spin_unlock_irq (&data->dev->lock);
  730. mutex_unlock(&data->lock);
  731. return value;
  732. }
  733. /*----------------------------------------------------------------------*/
  734. /* EP0 IMPLEMENTATION can be partly in userspace.
  735. *
  736. * Drivers that use this facility receive various events, including
  737. * control requests the kernel doesn't handle. Drivers that don't
  738. * use this facility may be too simple-minded for real applications.
  739. */
  740. static inline void ep0_readable (struct dev_data *dev)
  741. {
  742. wake_up (&dev->wait);
  743. kill_fasync (&dev->fasync, SIGIO, POLL_IN);
  744. }
  745. static void clean_req (struct usb_ep *ep, struct usb_request *req)
  746. {
  747. struct dev_data *dev = ep->driver_data;
  748. if (req->buf != dev->rbuf) {
  749. kfree(req->buf);
  750. req->buf = dev->rbuf;
  751. }
  752. req->complete = epio_complete;
  753. dev->setup_out_ready = 0;
  754. }
  755. static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
  756. {
  757. struct dev_data *dev = ep->driver_data;
  758. unsigned long flags;
  759. int free = 1;
  760. /* for control OUT, data must still get to userspace */
  761. spin_lock_irqsave(&dev->lock, flags);
  762. if (!dev->setup_in) {
  763. dev->setup_out_error = (req->status != 0);
  764. if (!dev->setup_out_error)
  765. free = 0;
  766. dev->setup_out_ready = 1;
  767. ep0_readable (dev);
  768. }
  769. /* clean up as appropriate */
  770. if (free && req->buf != &dev->rbuf)
  771. clean_req (ep, req);
  772. req->complete = epio_complete;
  773. spin_unlock_irqrestore(&dev->lock, flags);
  774. }
  775. static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
  776. {
  777. struct dev_data *dev = ep->driver_data;
  778. if (dev->setup_out_ready) {
  779. DBG (dev, "ep0 request busy!\n");
  780. return -EBUSY;
  781. }
  782. if (len > sizeof (dev->rbuf))
  783. req->buf = kmalloc(len, GFP_ATOMIC);
  784. if (req->buf == NULL) {
  785. req->buf = dev->rbuf;
  786. return -ENOMEM;
  787. }
  788. req->complete = ep0_complete;
  789. req->length = len;
  790. req->zero = 0;
  791. return 0;
  792. }
  793. static ssize_t
  794. ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  795. {
  796. struct dev_data *dev = fd->private_data;
  797. ssize_t retval;
  798. enum ep0_state state;
  799. spin_lock_irq (&dev->lock);
  800. if (dev->state <= STATE_DEV_OPENED) {
  801. retval = -EINVAL;
  802. goto done;
  803. }
  804. /* report fd mode change before acting on it */
  805. if (dev->setup_abort) {
  806. dev->setup_abort = 0;
  807. retval = -EIDRM;
  808. goto done;
  809. }
  810. /* control DATA stage */
  811. if ((state = dev->state) == STATE_DEV_SETUP) {
  812. if (dev->setup_in) { /* stall IN */
  813. VDEBUG(dev, "ep0in stall\n");
  814. (void) usb_ep_set_halt (dev->gadget->ep0);
  815. retval = -EL2HLT;
  816. dev->state = STATE_DEV_CONNECTED;
  817. } else if (len == 0) { /* ack SET_CONFIGURATION etc */
  818. struct usb_ep *ep = dev->gadget->ep0;
  819. struct usb_request *req = dev->req;
  820. if ((retval = setup_req (ep, req, 0)) == 0) {
  821. ++dev->udc_usage;
  822. spin_unlock_irq (&dev->lock);
  823. retval = usb_ep_queue (ep, req, GFP_KERNEL);
  824. spin_lock_irq (&dev->lock);
  825. --dev->udc_usage;
  826. }
  827. dev->state = STATE_DEV_CONNECTED;
  828. /* assume that was SET_CONFIGURATION */
  829. if (dev->current_config) {
  830. unsigned power;
  831. if (gadget_is_dualspeed(dev->gadget)
  832. && (dev->gadget->speed
  833. == USB_SPEED_HIGH))
  834. power = dev->hs_config->bMaxPower;
  835. else
  836. power = dev->config->bMaxPower;
  837. usb_gadget_vbus_draw(dev->gadget, 2 * power);
  838. }
  839. } else { /* collect OUT data */
  840. if ((fd->f_flags & O_NONBLOCK) != 0
  841. && !dev->setup_out_ready) {
  842. retval = -EAGAIN;
  843. goto done;
  844. }
  845. spin_unlock_irq (&dev->lock);
  846. retval = wait_event_interruptible (dev->wait,
  847. dev->setup_out_ready != 0);
  848. /* FIXME state could change from under us */
  849. spin_lock_irq (&dev->lock);
  850. if (retval)
  851. goto done;
  852. if (dev->state != STATE_DEV_SETUP) {
  853. retval = -ECANCELED;
  854. goto done;
  855. }
  856. dev->state = STATE_DEV_CONNECTED;
  857. if (dev->setup_out_error)
  858. retval = -EIO;
  859. else {
  860. len = min (len, (size_t)dev->req->actual);
  861. ++dev->udc_usage;
  862. spin_unlock_irq(&dev->lock);
  863. if (copy_to_user (buf, dev->req->buf, len))
  864. retval = -EFAULT;
  865. else
  866. retval = len;
  867. spin_lock_irq(&dev->lock);
  868. --dev->udc_usage;
  869. clean_req (dev->gadget->ep0, dev->req);
  870. /* NOTE userspace can't yet choose to stall */
  871. }
  872. }
  873. goto done;
  874. }
  875. /* else normal: return event data */
  876. if (len < sizeof dev->event [0]) {
  877. retval = -EINVAL;
  878. goto done;
  879. }
  880. len -= len % sizeof (struct usb_gadgetfs_event);
  881. dev->usermode_setup = 1;
  882. scan:
  883. /* return queued events right away */
  884. if (dev->ev_next != 0) {
  885. unsigned i, n;
  886. n = len / sizeof (struct usb_gadgetfs_event);
  887. if (dev->ev_next < n)
  888. n = dev->ev_next;
  889. /* ep0 i/o has special semantics during STATE_DEV_SETUP */
  890. for (i = 0; i < n; i++) {
  891. if (dev->event [i].type == GADGETFS_SETUP) {
  892. dev->state = STATE_DEV_SETUP;
  893. n = i + 1;
  894. break;
  895. }
  896. }
  897. spin_unlock_irq (&dev->lock);
  898. len = n * sizeof (struct usb_gadgetfs_event);
  899. if (copy_to_user (buf, &dev->event, len))
  900. retval = -EFAULT;
  901. else
  902. retval = len;
  903. if (len > 0) {
  904. /* NOTE this doesn't guard against broken drivers;
  905. * concurrent ep0 readers may lose events.
  906. */
  907. spin_lock_irq (&dev->lock);
  908. if (dev->ev_next > n) {
  909. memmove(&dev->event[0], &dev->event[n],
  910. sizeof (struct usb_gadgetfs_event)
  911. * (dev->ev_next - n));
  912. }
  913. dev->ev_next -= n;
  914. spin_unlock_irq (&dev->lock);
  915. }
  916. return retval;
  917. }
  918. if (fd->f_flags & O_NONBLOCK) {
  919. retval = -EAGAIN;
  920. goto done;
  921. }
  922. switch (state) {
  923. default:
  924. DBG (dev, "fail %s, state %d\n", __func__, state);
  925. retval = -ESRCH;
  926. break;
  927. case STATE_DEV_UNCONNECTED:
  928. case STATE_DEV_CONNECTED:
  929. spin_unlock_irq (&dev->lock);
  930. DBG (dev, "%s wait\n", __func__);
  931. /* wait for events */
  932. retval = wait_event_interruptible (dev->wait,
  933. dev->ev_next != 0);
  934. if (retval < 0)
  935. return retval;
  936. spin_lock_irq (&dev->lock);
  937. goto scan;
  938. }
  939. done:
  940. spin_unlock_irq (&dev->lock);
  941. return retval;
  942. }
  943. static struct usb_gadgetfs_event *
  944. next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
  945. {
  946. struct usb_gadgetfs_event *event;
  947. unsigned i;
  948. switch (type) {
  949. /* these events purge the queue */
  950. case GADGETFS_DISCONNECT:
  951. if (dev->state == STATE_DEV_SETUP)
  952. dev->setup_abort = 1;
  953. fallthrough;
  954. case GADGETFS_CONNECT:
  955. dev->ev_next = 0;
  956. break;
  957. case GADGETFS_SETUP: /* previous request timed out */
  958. case GADGETFS_SUSPEND: /* same effect */
  959. /* these events can't be repeated */
  960. for (i = 0; i != dev->ev_next; i++) {
  961. if (dev->event [i].type != type)
  962. continue;
  963. DBG(dev, "discard old event[%d] %d\n", i, type);
  964. dev->ev_next--;
  965. if (i == dev->ev_next)
  966. break;
  967. /* indices start at zero, for simplicity */
  968. memmove (&dev->event [i], &dev->event [i + 1],
  969. sizeof (struct usb_gadgetfs_event)
  970. * (dev->ev_next - i));
  971. }
  972. break;
  973. default:
  974. BUG ();
  975. }
  976. VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
  977. event = &dev->event [dev->ev_next++];
  978. BUG_ON (dev->ev_next > N_EVENT);
  979. memset (event, 0, sizeof *event);
  980. event->type = type;
  981. return event;
  982. }
  983. static ssize_t
  984. ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  985. {
  986. struct dev_data *dev = fd->private_data;
  987. ssize_t retval = -ESRCH;
  988. /* report fd mode change before acting on it */
  989. if (dev->setup_abort) {
  990. dev->setup_abort = 0;
  991. retval = -EIDRM;
  992. /* data and/or status stage for control request */
  993. } else if (dev->state == STATE_DEV_SETUP) {
  994. len = min_t(size_t, len, dev->setup_wLength);
  995. if (dev->setup_in) {
  996. retval = setup_req (dev->gadget->ep0, dev->req, len);
  997. if (retval == 0) {
  998. dev->state = STATE_DEV_CONNECTED;
  999. ++dev->udc_usage;
  1000. spin_unlock_irq (&dev->lock);
  1001. if (copy_from_user (dev->req->buf, buf, len))
  1002. retval = -EFAULT;
  1003. else {
  1004. if (len < dev->setup_wLength)
  1005. dev->req->zero = 1;
  1006. retval = usb_ep_queue (
  1007. dev->gadget->ep0, dev->req,
  1008. GFP_KERNEL);
  1009. }
  1010. spin_lock_irq(&dev->lock);
  1011. --dev->udc_usage;
  1012. if (retval < 0) {
  1013. clean_req (dev->gadget->ep0, dev->req);
  1014. } else
  1015. retval = len;
  1016. return retval;
  1017. }
  1018. /* can stall some OUT transfers */
  1019. } else if (dev->setup_can_stall) {
  1020. VDEBUG(dev, "ep0out stall\n");
  1021. (void) usb_ep_set_halt (dev->gadget->ep0);
  1022. retval = -EL2HLT;
  1023. dev->state = STATE_DEV_CONNECTED;
  1024. } else {
  1025. DBG(dev, "bogus ep0out stall!\n");
  1026. }
  1027. } else
  1028. DBG (dev, "fail %s, state %d\n", __func__, dev->state);
  1029. return retval;
  1030. }
  1031. static int
  1032. ep0_fasync (int f, struct file *fd, int on)
  1033. {
  1034. struct dev_data *dev = fd->private_data;
  1035. // caller must F_SETOWN before signal delivery happens
  1036. VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
  1037. return fasync_helper (f, fd, on, &dev->fasync);
  1038. }
  1039. static struct usb_gadget_driver gadgetfs_driver;
  1040. static int
  1041. dev_release (struct inode *inode, struct file *fd)
  1042. {
  1043. struct dev_data *dev = fd->private_data;
  1044. /* closing ep0 === shutdown all */
  1045. if (dev->gadget_registered) {
  1046. usb_gadget_unregister_driver (&gadgetfs_driver);
  1047. dev->gadget_registered = false;
  1048. }
  1049. /* at this point "good" hardware has disconnected the
  1050. * device from USB; the host won't see it any more.
  1051. * alternatively, all host requests will time out.
  1052. */
  1053. kfree (dev->buf);
  1054. dev->buf = NULL;
  1055. /* other endpoints were all decoupled from this device */
  1056. spin_lock_irq(&dev->lock);
  1057. dev->state = STATE_DEV_DISABLED;
  1058. spin_unlock_irq(&dev->lock);
  1059. put_dev (dev);
  1060. return 0;
  1061. }
  1062. static __poll_t
  1063. ep0_poll (struct file *fd, poll_table *wait)
  1064. {
  1065. struct dev_data *dev = fd->private_data;
  1066. __poll_t mask = 0;
  1067. if (dev->state <= STATE_DEV_OPENED)
  1068. return DEFAULT_POLLMASK;
  1069. poll_wait(fd, &dev->wait, wait);
  1070. spin_lock_irq(&dev->lock);
  1071. /* report fd mode change before acting on it */
  1072. if (dev->setup_abort) {
  1073. dev->setup_abort = 0;
  1074. mask = EPOLLHUP;
  1075. goto out;
  1076. }
  1077. if (dev->state == STATE_DEV_SETUP) {
  1078. if (dev->setup_in || dev->setup_can_stall)
  1079. mask = EPOLLOUT;
  1080. } else {
  1081. if (dev->ev_next != 0)
  1082. mask = EPOLLIN;
  1083. }
  1084. out:
  1085. spin_unlock_irq(&dev->lock);
  1086. return mask;
  1087. }
  1088. static long gadget_dev_ioctl (struct file *fd, unsigned code, unsigned long value)
  1089. {
  1090. struct dev_data *dev = fd->private_data;
  1091. struct usb_gadget *gadget = dev->gadget;
  1092. long ret = -ENOTTY;
  1093. spin_lock_irq(&dev->lock);
  1094. if (dev->state == STATE_DEV_OPENED ||
  1095. dev->state == STATE_DEV_UNBOUND) {
  1096. /* Not bound to a UDC */
  1097. } else if (gadget->ops->ioctl) {
  1098. ++dev->udc_usage;
  1099. spin_unlock_irq(&dev->lock);
  1100. ret = gadget->ops->ioctl (gadget, code, value);
  1101. spin_lock_irq(&dev->lock);
  1102. --dev->udc_usage;
  1103. }
  1104. spin_unlock_irq(&dev->lock);
  1105. return ret;
  1106. }
  1107. /*----------------------------------------------------------------------*/
  1108. /* The in-kernel gadget driver handles most ep0 issues, in particular
  1109. * enumerating the single configuration (as provided from user space).
  1110. *
  1111. * Unrecognized ep0 requests may be handled in user space.
  1112. */
  1113. static void make_qualifier (struct dev_data *dev)
  1114. {
  1115. struct usb_qualifier_descriptor qual;
  1116. struct usb_device_descriptor *desc;
  1117. qual.bLength = sizeof qual;
  1118. qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  1119. qual.bcdUSB = cpu_to_le16 (0x0200);
  1120. desc = dev->dev;
  1121. qual.bDeviceClass = desc->bDeviceClass;
  1122. qual.bDeviceSubClass = desc->bDeviceSubClass;
  1123. qual.bDeviceProtocol = desc->bDeviceProtocol;
  1124. /* assumes ep0 uses the same value for both speeds ... */
  1125. qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1126. qual.bNumConfigurations = 1;
  1127. qual.bRESERVED = 0;
  1128. memcpy (dev->rbuf, &qual, sizeof qual);
  1129. }
  1130. static int
  1131. config_buf (struct dev_data *dev, u8 type, unsigned index)
  1132. {
  1133. int len;
  1134. int hs = 0;
  1135. /* only one configuration */
  1136. if (index > 0)
  1137. return -EINVAL;
  1138. if (gadget_is_dualspeed(dev->gadget)) {
  1139. hs = (dev->gadget->speed == USB_SPEED_HIGH);
  1140. if (type == USB_DT_OTHER_SPEED_CONFIG)
  1141. hs = !hs;
  1142. }
  1143. if (hs) {
  1144. dev->req->buf = dev->hs_config;
  1145. len = le16_to_cpu(dev->hs_config->wTotalLength);
  1146. } else {
  1147. dev->req->buf = dev->config;
  1148. len = le16_to_cpu(dev->config->wTotalLength);
  1149. }
  1150. ((u8 *)dev->req->buf) [1] = type;
  1151. return len;
  1152. }
  1153. static int
  1154. gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1155. {
  1156. struct dev_data *dev = get_gadget_data (gadget);
  1157. struct usb_request *req = dev->req;
  1158. int value = -EOPNOTSUPP;
  1159. struct usb_gadgetfs_event *event;
  1160. u16 w_value = le16_to_cpu(ctrl->wValue);
  1161. u16 w_length = le16_to_cpu(ctrl->wLength);
  1162. if (w_length > RBUF_SIZE) {
  1163. if (ctrl->bRequestType & USB_DIR_IN) {
  1164. /* Cast away the const, we are going to overwrite on purpose. */
  1165. __le16 *temp = (__le16 *)&ctrl->wLength;
  1166. *temp = cpu_to_le16(RBUF_SIZE);
  1167. w_length = RBUF_SIZE;
  1168. } else {
  1169. return value;
  1170. }
  1171. }
  1172. spin_lock (&dev->lock);
  1173. dev->setup_abort = 0;
  1174. if (dev->state == STATE_DEV_UNCONNECTED) {
  1175. if (gadget_is_dualspeed(gadget)
  1176. && gadget->speed == USB_SPEED_HIGH
  1177. && dev->hs_config == NULL) {
  1178. spin_unlock(&dev->lock);
  1179. ERROR (dev, "no high speed config??\n");
  1180. return -EINVAL;
  1181. }
  1182. dev->state = STATE_DEV_CONNECTED;
  1183. INFO (dev, "connected\n");
  1184. event = next_event (dev, GADGETFS_CONNECT);
  1185. event->u.speed = gadget->speed;
  1186. ep0_readable (dev);
  1187. /* host may have given up waiting for response. we can miss control
  1188. * requests handled lower down (device/endpoint status and features);
  1189. * then ep0_{read,write} will report the wrong status. controller
  1190. * driver will have aborted pending i/o.
  1191. */
  1192. } else if (dev->state == STATE_DEV_SETUP)
  1193. dev->setup_abort = 1;
  1194. req->buf = dev->rbuf;
  1195. req->context = NULL;
  1196. switch (ctrl->bRequest) {
  1197. case USB_REQ_GET_DESCRIPTOR:
  1198. if (ctrl->bRequestType != USB_DIR_IN)
  1199. goto unrecognized;
  1200. switch (w_value >> 8) {
  1201. case USB_DT_DEVICE:
  1202. value = min (w_length, (u16) sizeof *dev->dev);
  1203. dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1204. req->buf = dev->dev;
  1205. break;
  1206. case USB_DT_DEVICE_QUALIFIER:
  1207. if (!dev->hs_config)
  1208. break;
  1209. value = min (w_length, (u16)
  1210. sizeof (struct usb_qualifier_descriptor));
  1211. make_qualifier (dev);
  1212. break;
  1213. case USB_DT_OTHER_SPEED_CONFIG:
  1214. case USB_DT_CONFIG:
  1215. value = config_buf (dev,
  1216. w_value >> 8,
  1217. w_value & 0xff);
  1218. if (value >= 0)
  1219. value = min (w_length, (u16) value);
  1220. break;
  1221. case USB_DT_STRING:
  1222. goto unrecognized;
  1223. default: // all others are errors
  1224. break;
  1225. }
  1226. break;
  1227. /* currently one config, two speeds */
  1228. case USB_REQ_SET_CONFIGURATION:
  1229. if (ctrl->bRequestType != 0)
  1230. goto unrecognized;
  1231. if (0 == (u8) w_value) {
  1232. value = 0;
  1233. dev->current_config = 0;
  1234. usb_gadget_vbus_draw(gadget, 8 /* mA */ );
  1235. // user mode expected to disable endpoints
  1236. } else {
  1237. u8 config, power;
  1238. if (gadget_is_dualspeed(gadget)
  1239. && gadget->speed == USB_SPEED_HIGH) {
  1240. config = dev->hs_config->bConfigurationValue;
  1241. power = dev->hs_config->bMaxPower;
  1242. } else {
  1243. config = dev->config->bConfigurationValue;
  1244. power = dev->config->bMaxPower;
  1245. }
  1246. if (config == (u8) w_value) {
  1247. value = 0;
  1248. dev->current_config = config;
  1249. usb_gadget_vbus_draw(gadget, 2 * power);
  1250. }
  1251. }
  1252. /* report SET_CONFIGURATION like any other control request,
  1253. * except that usermode may not stall this. the next
  1254. * request mustn't be allowed start until this finishes:
  1255. * endpoints and threads set up, etc.
  1256. *
  1257. * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
  1258. * has bad/racey automagic that prevents synchronizing here.
  1259. * even kernel mode drivers often miss them.
  1260. */
  1261. if (value == 0) {
  1262. INFO (dev, "configuration #%d\n", dev->current_config);
  1263. usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
  1264. if (dev->usermode_setup) {
  1265. dev->setup_can_stall = 0;
  1266. goto delegate;
  1267. }
  1268. }
  1269. break;
  1270. #ifndef CONFIG_USB_PXA25X
  1271. /* PXA automagically handles this request too */
  1272. case USB_REQ_GET_CONFIGURATION:
  1273. if (ctrl->bRequestType != 0x80)
  1274. goto unrecognized;
  1275. *(u8 *)req->buf = dev->current_config;
  1276. value = min (w_length, (u16) 1);
  1277. break;
  1278. #endif
  1279. default:
  1280. unrecognized:
  1281. VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
  1282. dev->usermode_setup ? "delegate" : "fail",
  1283. ctrl->bRequestType, ctrl->bRequest,
  1284. w_value, le16_to_cpu(ctrl->wIndex), w_length);
  1285. /* if there's an ep0 reader, don't stall */
  1286. if (dev->usermode_setup) {
  1287. dev->setup_can_stall = 1;
  1288. delegate:
  1289. dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
  1290. ? 1 : 0;
  1291. dev->setup_wLength = w_length;
  1292. dev->setup_out_ready = 0;
  1293. dev->setup_out_error = 0;
  1294. /* read DATA stage for OUT right away */
  1295. if (unlikely (!dev->setup_in && w_length)) {
  1296. value = setup_req (gadget->ep0, dev->req,
  1297. w_length);
  1298. if (value < 0)
  1299. break;
  1300. ++dev->udc_usage;
  1301. spin_unlock (&dev->lock);
  1302. value = usb_ep_queue (gadget->ep0, dev->req,
  1303. GFP_KERNEL);
  1304. spin_lock (&dev->lock);
  1305. --dev->udc_usage;
  1306. if (value < 0) {
  1307. clean_req (gadget->ep0, dev->req);
  1308. break;
  1309. }
  1310. /* we can't currently stall these */
  1311. dev->setup_can_stall = 0;
  1312. }
  1313. /* state changes when reader collects event */
  1314. event = next_event (dev, GADGETFS_SETUP);
  1315. event->u.setup = *ctrl;
  1316. ep0_readable (dev);
  1317. spin_unlock (&dev->lock);
  1318. /*
  1319. * Return USB_GADGET_DELAYED_STATUS as a workaround to
  1320. * stop some UDC drivers (e.g. dwc3) from automatically
  1321. * proceeding with the status stage for 0-length
  1322. * transfers.
  1323. * Should be removed once all UDC drivers are fixed to
  1324. * always delay the status stage until a response is
  1325. * queued to EP0.
  1326. */
  1327. return w_length == 0 ? USB_GADGET_DELAYED_STATUS : 0;
  1328. }
  1329. }
  1330. /* proceed with data transfer and status phases? */
  1331. if (value >= 0 && dev->state != STATE_DEV_SETUP) {
  1332. req->length = value;
  1333. req->zero = value < w_length;
  1334. ++dev->udc_usage;
  1335. spin_unlock (&dev->lock);
  1336. value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL);
  1337. spin_lock(&dev->lock);
  1338. --dev->udc_usage;
  1339. spin_unlock(&dev->lock);
  1340. if (value < 0) {
  1341. DBG (dev, "ep_queue --> %d\n", value);
  1342. req->status = 0;
  1343. }
  1344. return value;
  1345. }
  1346. /* device stalls when value < 0 */
  1347. spin_unlock (&dev->lock);
  1348. return value;
  1349. }
  1350. static void destroy_ep_files (struct dev_data *dev)
  1351. {
  1352. DBG (dev, "%s %d\n", __func__, dev->state);
  1353. /* dev->state must prevent interference */
  1354. spin_lock_irq (&dev->lock);
  1355. while (!list_empty(&dev->epfiles)) {
  1356. struct ep_data *ep;
  1357. struct inode *parent;
  1358. struct dentry *dentry;
  1359. /* break link to FS */
  1360. ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
  1361. list_del_init (&ep->epfiles);
  1362. spin_unlock_irq (&dev->lock);
  1363. dentry = ep->dentry;
  1364. ep->dentry = NULL;
  1365. parent = d_inode(dentry->d_parent);
  1366. /* break link to controller */
  1367. mutex_lock(&ep->lock);
  1368. if (ep->state == STATE_EP_ENABLED)
  1369. (void) usb_ep_disable (ep->ep);
  1370. ep->state = STATE_EP_UNBOUND;
  1371. usb_ep_free_request (ep->ep, ep->req);
  1372. ep->ep = NULL;
  1373. mutex_unlock(&ep->lock);
  1374. wake_up (&ep->wait);
  1375. put_ep (ep);
  1376. /* break link to dcache */
  1377. inode_lock(parent);
  1378. d_delete (dentry);
  1379. dput (dentry);
  1380. inode_unlock(parent);
  1381. spin_lock_irq (&dev->lock);
  1382. }
  1383. spin_unlock_irq (&dev->lock);
  1384. }
  1385. static struct dentry *
  1386. gadgetfs_create_file (struct super_block *sb, char const *name,
  1387. void *data, const struct file_operations *fops);
  1388. static int activate_ep_files (struct dev_data *dev)
  1389. {
  1390. struct usb_ep *ep;
  1391. struct ep_data *data;
  1392. gadget_for_each_ep (ep, dev->gadget) {
  1393. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1394. if (!data)
  1395. goto enomem0;
  1396. data->state = STATE_EP_DISABLED;
  1397. mutex_init(&data->lock);
  1398. init_waitqueue_head (&data->wait);
  1399. strncpy (data->name, ep->name, sizeof (data->name) - 1);
  1400. refcount_set (&data->count, 1);
  1401. data->dev = dev;
  1402. get_dev (dev);
  1403. data->ep = ep;
  1404. ep->driver_data = data;
  1405. data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
  1406. if (!data->req)
  1407. goto enomem1;
  1408. data->dentry = gadgetfs_create_file (dev->sb, data->name,
  1409. data, &ep_io_operations);
  1410. if (!data->dentry)
  1411. goto enomem2;
  1412. list_add_tail (&data->epfiles, &dev->epfiles);
  1413. }
  1414. return 0;
  1415. enomem2:
  1416. usb_ep_free_request (ep, data->req);
  1417. enomem1:
  1418. put_dev (dev);
  1419. kfree (data);
  1420. enomem0:
  1421. DBG (dev, "%s enomem\n", __func__);
  1422. destroy_ep_files (dev);
  1423. return -ENOMEM;
  1424. }
  1425. static void
  1426. gadgetfs_unbind (struct usb_gadget *gadget)
  1427. {
  1428. struct dev_data *dev = get_gadget_data (gadget);
  1429. DBG (dev, "%s\n", __func__);
  1430. spin_lock_irq (&dev->lock);
  1431. dev->state = STATE_DEV_UNBOUND;
  1432. while (dev->udc_usage > 0) {
  1433. spin_unlock_irq(&dev->lock);
  1434. usleep_range(1000, 2000);
  1435. spin_lock_irq(&dev->lock);
  1436. }
  1437. spin_unlock_irq (&dev->lock);
  1438. destroy_ep_files (dev);
  1439. gadget->ep0->driver_data = NULL;
  1440. set_gadget_data (gadget, NULL);
  1441. /* we've already been disconnected ... no i/o is active */
  1442. if (dev->req)
  1443. usb_ep_free_request (gadget->ep0, dev->req);
  1444. DBG (dev, "%s done\n", __func__);
  1445. put_dev (dev);
  1446. }
  1447. static struct dev_data *the_device;
  1448. static int gadgetfs_bind(struct usb_gadget *gadget,
  1449. struct usb_gadget_driver *driver)
  1450. {
  1451. struct dev_data *dev = the_device;
  1452. if (!dev)
  1453. return -ESRCH;
  1454. if (0 != strcmp (CHIP, gadget->name)) {
  1455. pr_err("%s expected %s controller not %s\n",
  1456. shortname, CHIP, gadget->name);
  1457. return -ENODEV;
  1458. }
  1459. set_gadget_data (gadget, dev);
  1460. dev->gadget = gadget;
  1461. gadget->ep0->driver_data = dev;
  1462. /* preallocate control response and buffer */
  1463. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1464. if (!dev->req)
  1465. goto enomem;
  1466. dev->req->context = NULL;
  1467. dev->req->complete = epio_complete;
  1468. if (activate_ep_files (dev) < 0)
  1469. goto enomem;
  1470. INFO (dev, "bound to %s driver\n", gadget->name);
  1471. spin_lock_irq(&dev->lock);
  1472. dev->state = STATE_DEV_UNCONNECTED;
  1473. spin_unlock_irq(&dev->lock);
  1474. get_dev (dev);
  1475. return 0;
  1476. enomem:
  1477. gadgetfs_unbind (gadget);
  1478. return -ENOMEM;
  1479. }
  1480. static void
  1481. gadgetfs_disconnect (struct usb_gadget *gadget)
  1482. {
  1483. struct dev_data *dev = get_gadget_data (gadget);
  1484. unsigned long flags;
  1485. spin_lock_irqsave (&dev->lock, flags);
  1486. if (dev->state == STATE_DEV_UNCONNECTED)
  1487. goto exit;
  1488. dev->state = STATE_DEV_UNCONNECTED;
  1489. INFO (dev, "disconnected\n");
  1490. next_event (dev, GADGETFS_DISCONNECT);
  1491. ep0_readable (dev);
  1492. exit:
  1493. spin_unlock_irqrestore (&dev->lock, flags);
  1494. }
  1495. static void
  1496. gadgetfs_suspend (struct usb_gadget *gadget)
  1497. {
  1498. struct dev_data *dev = get_gadget_data (gadget);
  1499. unsigned long flags;
  1500. INFO (dev, "suspended from state %d\n", dev->state);
  1501. spin_lock_irqsave(&dev->lock, flags);
  1502. switch (dev->state) {
  1503. case STATE_DEV_SETUP: // VERY odd... host died??
  1504. case STATE_DEV_CONNECTED:
  1505. case STATE_DEV_UNCONNECTED:
  1506. next_event (dev, GADGETFS_SUSPEND);
  1507. ep0_readable (dev);
  1508. fallthrough;
  1509. default:
  1510. break;
  1511. }
  1512. spin_unlock_irqrestore(&dev->lock, flags);
  1513. }
  1514. static struct usb_gadget_driver gadgetfs_driver = {
  1515. .function = (char *) driver_desc,
  1516. .bind = gadgetfs_bind,
  1517. .unbind = gadgetfs_unbind,
  1518. .setup = gadgetfs_setup,
  1519. .reset = gadgetfs_disconnect,
  1520. .disconnect = gadgetfs_disconnect,
  1521. .suspend = gadgetfs_suspend,
  1522. .driver = {
  1523. .name = shortname,
  1524. },
  1525. };
  1526. /*----------------------------------------------------------------------*/
  1527. /* DEVICE INITIALIZATION
  1528. *
  1529. * fd = open ("/dev/gadget/$CHIP", O_RDWR)
  1530. * status = write (fd, descriptors, sizeof descriptors)
  1531. *
  1532. * That write establishes the device configuration, so the kernel can
  1533. * bind to the controller ... guaranteeing it can handle enumeration
  1534. * at all necessary speeds. Descriptor order is:
  1535. *
  1536. * . message tag (u32, host order) ... for now, must be zero; it
  1537. * would change to support features like multi-config devices
  1538. * . full/low speed config ... all wTotalLength bytes (with interface,
  1539. * class, altsetting, endpoint, and other descriptors)
  1540. * . high speed config ... all descriptors, for high speed operation;
  1541. * this one's optional except for high-speed hardware
  1542. * . device descriptor
  1543. *
  1544. * Endpoints are not yet enabled. Drivers must wait until device
  1545. * configuration and interface altsetting changes create
  1546. * the need to configure (or unconfigure) them.
  1547. *
  1548. * After initialization, the device stays active for as long as that
  1549. * $CHIP file is open. Events must then be read from that descriptor,
  1550. * such as configuration notifications.
  1551. */
  1552. static int is_valid_config(struct usb_config_descriptor *config,
  1553. unsigned int total)
  1554. {
  1555. return config->bDescriptorType == USB_DT_CONFIG
  1556. && config->bLength == USB_DT_CONFIG_SIZE
  1557. && total >= USB_DT_CONFIG_SIZE
  1558. && config->bConfigurationValue != 0
  1559. && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
  1560. && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
  1561. /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
  1562. /* FIXME check lengths: walk to end */
  1563. }
  1564. static ssize_t
  1565. dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1566. {
  1567. struct dev_data *dev = fd->private_data;
  1568. ssize_t value, length = len;
  1569. unsigned total;
  1570. u32 tag;
  1571. char *kbuf;
  1572. spin_lock_irq(&dev->lock);
  1573. if (dev->state > STATE_DEV_OPENED) {
  1574. value = ep0_write(fd, buf, len, ptr);
  1575. spin_unlock_irq(&dev->lock);
  1576. return value;
  1577. }
  1578. spin_unlock_irq(&dev->lock);
  1579. if ((len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) ||
  1580. (len > PAGE_SIZE * 4))
  1581. return -EINVAL;
  1582. /* we might need to change message format someday */
  1583. if (copy_from_user (&tag, buf, 4))
  1584. return -EFAULT;
  1585. if (tag != 0)
  1586. return -EINVAL;
  1587. buf += 4;
  1588. length -= 4;
  1589. kbuf = memdup_user(buf, length);
  1590. if (IS_ERR(kbuf))
  1591. return PTR_ERR(kbuf);
  1592. spin_lock_irq (&dev->lock);
  1593. value = -EINVAL;
  1594. if (dev->buf) {
  1595. spin_unlock_irq(&dev->lock);
  1596. kfree(kbuf);
  1597. return value;
  1598. }
  1599. dev->buf = kbuf;
  1600. /* full or low speed config */
  1601. dev->config = (void *) kbuf;
  1602. total = le16_to_cpu(dev->config->wTotalLength);
  1603. if (!is_valid_config(dev->config, total) ||
  1604. total > length - USB_DT_DEVICE_SIZE)
  1605. goto fail;
  1606. kbuf += total;
  1607. length -= total;
  1608. /* optional high speed config */
  1609. if (kbuf [1] == USB_DT_CONFIG) {
  1610. dev->hs_config = (void *) kbuf;
  1611. total = le16_to_cpu(dev->hs_config->wTotalLength);
  1612. if (!is_valid_config(dev->hs_config, total) ||
  1613. total > length - USB_DT_DEVICE_SIZE)
  1614. goto fail;
  1615. kbuf += total;
  1616. length -= total;
  1617. } else {
  1618. dev->hs_config = NULL;
  1619. }
  1620. /* could support multiple configs, using another encoding! */
  1621. /* device descriptor (tweaked for paranoia) */
  1622. if (length != USB_DT_DEVICE_SIZE)
  1623. goto fail;
  1624. dev->dev = (void *)kbuf;
  1625. if (dev->dev->bLength != USB_DT_DEVICE_SIZE
  1626. || dev->dev->bDescriptorType != USB_DT_DEVICE
  1627. || dev->dev->bNumConfigurations != 1)
  1628. goto fail;
  1629. dev->dev->bcdUSB = cpu_to_le16 (0x0200);
  1630. /* triggers gadgetfs_bind(); then we can enumerate. */
  1631. spin_unlock_irq (&dev->lock);
  1632. if (dev->hs_config)
  1633. gadgetfs_driver.max_speed = USB_SPEED_HIGH;
  1634. else
  1635. gadgetfs_driver.max_speed = USB_SPEED_FULL;
  1636. value = usb_gadget_register_driver(&gadgetfs_driver);
  1637. if (value != 0) {
  1638. spin_lock_irq(&dev->lock);
  1639. goto fail;
  1640. } else {
  1641. /* at this point "good" hardware has for the first time
  1642. * let the USB the host see us. alternatively, if users
  1643. * unplug/replug that will clear all the error state.
  1644. *
  1645. * note: everything running before here was guaranteed
  1646. * to choke driver model style diagnostics. from here
  1647. * on, they can work ... except in cleanup paths that
  1648. * kick in after the ep0 descriptor is closed.
  1649. */
  1650. value = len;
  1651. dev->gadget_registered = true;
  1652. }
  1653. return value;
  1654. fail:
  1655. dev->config = NULL;
  1656. dev->hs_config = NULL;
  1657. dev->dev = NULL;
  1658. spin_unlock_irq (&dev->lock);
  1659. pr_debug ("%s: %s fail %zd, %p\n", shortname, __func__, value, dev);
  1660. kfree (dev->buf);
  1661. dev->buf = NULL;
  1662. return value;
  1663. }
  1664. static int
  1665. gadget_dev_open (struct inode *inode, struct file *fd)
  1666. {
  1667. struct dev_data *dev = inode->i_private;
  1668. int value = -EBUSY;
  1669. spin_lock_irq(&dev->lock);
  1670. if (dev->state == STATE_DEV_DISABLED) {
  1671. dev->ev_next = 0;
  1672. dev->state = STATE_DEV_OPENED;
  1673. fd->private_data = dev;
  1674. get_dev (dev);
  1675. value = 0;
  1676. }
  1677. spin_unlock_irq(&dev->lock);
  1678. return value;
  1679. }
  1680. static const struct file_operations ep0_operations = {
  1681. .open = gadget_dev_open,
  1682. .read = ep0_read,
  1683. .write = dev_config,
  1684. .fasync = ep0_fasync,
  1685. .poll = ep0_poll,
  1686. .unlocked_ioctl = gadget_dev_ioctl,
  1687. .release = dev_release,
  1688. };
  1689. /*----------------------------------------------------------------------*/
  1690. /* FILESYSTEM AND SUPERBLOCK OPERATIONS
  1691. *
  1692. * Mounting the filesystem creates a controller file, used first for
  1693. * device configuration then later for event monitoring.
  1694. */
  1695. /* FIXME PAM etc could set this security policy without mount options
  1696. * if epfiles inherited ownership and permissons from ep0 ...
  1697. */
  1698. static unsigned default_uid;
  1699. static unsigned default_gid;
  1700. static unsigned default_perm = S_IRUSR | S_IWUSR;
  1701. module_param (default_uid, uint, 0644);
  1702. module_param (default_gid, uint, 0644);
  1703. module_param (default_perm, uint, 0644);
  1704. static struct inode *
  1705. gadgetfs_make_inode (struct super_block *sb,
  1706. void *data, const struct file_operations *fops,
  1707. int mode)
  1708. {
  1709. struct inode *inode = new_inode (sb);
  1710. if (inode) {
  1711. inode->i_ino = get_next_ino();
  1712. inode->i_mode = mode;
  1713. inode->i_uid = make_kuid(&init_user_ns, default_uid);
  1714. inode->i_gid = make_kgid(&init_user_ns, default_gid);
  1715. simple_inode_init_ts(inode);
  1716. inode->i_private = data;
  1717. inode->i_fop = fops;
  1718. }
  1719. return inode;
  1720. }
  1721. /* creates in fs root directory, so non-renamable and non-linkable.
  1722. * so inode and dentry are paired, until device reconfig.
  1723. */
  1724. static struct dentry *
  1725. gadgetfs_create_file (struct super_block *sb, char const *name,
  1726. void *data, const struct file_operations *fops)
  1727. {
  1728. struct dentry *dentry;
  1729. struct inode *inode;
  1730. dentry = d_alloc_name(sb->s_root, name);
  1731. if (!dentry)
  1732. return NULL;
  1733. inode = gadgetfs_make_inode (sb, data, fops,
  1734. S_IFREG | (default_perm & S_IRWXUGO));
  1735. if (!inode) {
  1736. dput(dentry);
  1737. return NULL;
  1738. }
  1739. d_add (dentry, inode);
  1740. return dentry;
  1741. }
  1742. static const struct super_operations gadget_fs_operations = {
  1743. .statfs = simple_statfs,
  1744. .drop_inode = generic_delete_inode,
  1745. };
  1746. static int
  1747. gadgetfs_fill_super (struct super_block *sb, struct fs_context *fc)
  1748. {
  1749. struct inode *inode;
  1750. struct dev_data *dev;
  1751. int rc;
  1752. mutex_lock(&sb_mutex);
  1753. if (the_device) {
  1754. rc = -ESRCH;
  1755. goto Done;
  1756. }
  1757. CHIP = usb_get_gadget_udc_name();
  1758. if (!CHIP) {
  1759. rc = -ENODEV;
  1760. goto Done;
  1761. }
  1762. /* superblock */
  1763. sb->s_blocksize = PAGE_SIZE;
  1764. sb->s_blocksize_bits = PAGE_SHIFT;
  1765. sb->s_magic = GADGETFS_MAGIC;
  1766. sb->s_op = &gadget_fs_operations;
  1767. sb->s_time_gran = 1;
  1768. /* root inode */
  1769. inode = gadgetfs_make_inode (sb,
  1770. NULL, &simple_dir_operations,
  1771. S_IFDIR | S_IRUGO | S_IXUGO);
  1772. if (!inode)
  1773. goto Enomem;
  1774. inode->i_op = &simple_dir_inode_operations;
  1775. if (!(sb->s_root = d_make_root (inode)))
  1776. goto Enomem;
  1777. /* the ep0 file is named after the controller we expect;
  1778. * user mode code can use it for sanity checks, like we do.
  1779. */
  1780. dev = dev_new ();
  1781. if (!dev)
  1782. goto Enomem;
  1783. dev->sb = sb;
  1784. dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
  1785. if (!dev->dentry) {
  1786. put_dev(dev);
  1787. goto Enomem;
  1788. }
  1789. /* other endpoint files are available after hardware setup,
  1790. * from binding to a controller.
  1791. */
  1792. the_device = dev;
  1793. rc = 0;
  1794. goto Done;
  1795. Enomem:
  1796. kfree(CHIP);
  1797. CHIP = NULL;
  1798. rc = -ENOMEM;
  1799. Done:
  1800. mutex_unlock(&sb_mutex);
  1801. return rc;
  1802. }
  1803. /* "mount -t gadgetfs path /dev/gadget" ends up here */
  1804. static int gadgetfs_get_tree(struct fs_context *fc)
  1805. {
  1806. return get_tree_single(fc, gadgetfs_fill_super);
  1807. }
  1808. static const struct fs_context_operations gadgetfs_context_ops = {
  1809. .get_tree = gadgetfs_get_tree,
  1810. };
  1811. static int gadgetfs_init_fs_context(struct fs_context *fc)
  1812. {
  1813. fc->ops = &gadgetfs_context_ops;
  1814. return 0;
  1815. }
  1816. static void
  1817. gadgetfs_kill_sb (struct super_block *sb)
  1818. {
  1819. mutex_lock(&sb_mutex);
  1820. kill_litter_super (sb);
  1821. if (the_device) {
  1822. put_dev (the_device);
  1823. the_device = NULL;
  1824. }
  1825. kfree(CHIP);
  1826. CHIP = NULL;
  1827. mutex_unlock(&sb_mutex);
  1828. }
  1829. /*----------------------------------------------------------------------*/
  1830. static struct file_system_type gadgetfs_type = {
  1831. .owner = THIS_MODULE,
  1832. .name = shortname,
  1833. .init_fs_context = gadgetfs_init_fs_context,
  1834. .kill_sb = gadgetfs_kill_sb,
  1835. };
  1836. MODULE_ALIAS_FS("gadgetfs");
  1837. /*----------------------------------------------------------------------*/
  1838. static int __init gadgetfs_init (void)
  1839. {
  1840. int status;
  1841. status = register_filesystem (&gadgetfs_type);
  1842. if (status == 0)
  1843. pr_info ("%s: %s, version " DRIVER_VERSION "\n",
  1844. shortname, driver_desc);
  1845. return status;
  1846. }
  1847. module_init (gadgetfs_init);
  1848. static void __exit gadgetfs_cleanup (void)
  1849. {
  1850. pr_debug ("unregister %s\n", shortname);
  1851. unregister_filesystem (&gadgetfs_type);
  1852. }
  1853. module_exit (gadgetfs_cleanup);