inode.c 52 KB

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