inode.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119
  1. /*
  2. * inode.c -- user mode filesystem api for usb gadget controllers
  3. *
  4. * Copyright (C) 2003-2004 David Brownell
  5. * Copyright (C) 2003 Agilent Technologies
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. /* #define VERBOSE_DEBUG */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/uts.h>
  18. #include <linux/wait.h>
  19. #include <linux/compiler.h>
  20. #include <asm/uaccess.h>
  21. #include <linux/sched.h>
  22. #include <linux/slab.h>
  23. #include <linux/poll.h>
  24. #include <linux/mmu_context.h>
  25. #include <linux/aio.h>
  26. #include <linux/uio.h>
  27. #include <linux/delay.h>
  28. #include <linux/device.h>
  29. #include <linux/moduleparam.h>
  30. #include <linux/usb/gadgetfs.h>
  31. #include <linux/usb/gadget.h>
  32. /*
  33. * The gadgetfs API maps each endpoint to a file descriptor so that you
  34. * can use standard synchronous read/write calls for I/O. There's some
  35. * O_NONBLOCK and O_ASYNC/FASYNC style i/o support. Example usermode
  36. * drivers show how this works in practice. You can also use AIO to
  37. * eliminate I/O gaps between requests, to help when streaming data.
  38. *
  39. * Key parts that must be USB-specific are protocols defining how the
  40. * read/write operations relate to the hardware state machines. There
  41. * are two types of files. One type is for the device, implementing ep0.
  42. * The other type is for each IN or OUT endpoint. In both cases, the
  43. * user mode driver must configure the hardware before using it.
  44. *
  45. * - First, dev_config() is called when /dev/gadget/$CHIP is configured
  46. * (by writing configuration and device descriptors). Afterwards it
  47. * may serve as a source of device events, used to handle all control
  48. * requests other than basic enumeration.
  49. *
  50. * - Then, after a SET_CONFIGURATION control request, ep_config() is
  51. * called when each /dev/gadget/ep* file is configured (by writing
  52. * endpoint descriptors). Afterwards these files are used to write()
  53. * IN data or to read() OUT data. To halt the endpoint, a "wrong
  54. * direction" request is issued (like reading an IN endpoint).
  55. *
  56. * Unlike "usbfs" the only ioctl()s are for things that are rare, and maybe
  57. * not possible on all hardware. For example, precise fault handling with
  58. * respect to data left in endpoint fifos after aborted operations; or
  59. * selective clearing of endpoint halts, to implement SET_INTERFACE.
  60. */
  61. #define DRIVER_DESC "USB Gadget filesystem"
  62. #define DRIVER_VERSION "24 Aug 2004"
  63. static const char driver_desc [] = DRIVER_DESC;
  64. static const char shortname [] = "gadgetfs";
  65. MODULE_DESCRIPTION (DRIVER_DESC);
  66. MODULE_AUTHOR ("David Brownell");
  67. MODULE_LICENSE ("GPL");
  68. static int ep_open(struct inode *, struct file *);
  69. /*----------------------------------------------------------------------*/
  70. #define GADGETFS_MAGIC 0xaee71ee7
  71. /* /dev/gadget/$CHIP represents ep0 and the whole device */
  72. enum ep0_state {
  73. /* DISBLED is the initial state.
  74. */
  75. STATE_DEV_DISABLED = 0,
  76. /* Only one open() of /dev/gadget/$CHIP; only one file tracks
  77. * ep0/device i/o modes and binding to the controller. Driver
  78. * must always write descriptors to initialize the device, then
  79. * the device becomes UNCONNECTED until enumeration.
  80. */
  81. STATE_DEV_OPENED,
  82. /* From then on, ep0 fd is in either of two basic modes:
  83. * - (UN)CONNECTED: read usb_gadgetfs_event(s) from it
  84. * - SETUP: read/write will transfer control data and succeed;
  85. * or if "wrong direction", performs protocol stall
  86. */
  87. STATE_DEV_UNCONNECTED,
  88. STATE_DEV_CONNECTED,
  89. STATE_DEV_SETUP,
  90. /* UNBOUND means the driver closed ep0, so the device won't be
  91. * accessible again (DEV_DISABLED) until all fds are closed.
  92. */
  93. STATE_DEV_UNBOUND,
  94. };
  95. /* enough for the whole queue: most events invalidate others */
  96. #define N_EVENT 5
  97. struct dev_data {
  98. spinlock_t lock;
  99. atomic_t count;
  100. int udc_usage;
  101. enum ep0_state state; /* P: lock */
  102. struct usb_gadgetfs_event event [N_EVENT];
  103. unsigned ev_next;
  104. struct fasync_struct *fasync;
  105. u8 current_config;
  106. /* drivers reading ep0 MUST handle control requests (SETUP)
  107. * reported that way; else the host will time out.
  108. */
  109. unsigned usermode_setup : 1,
  110. setup_in : 1,
  111. setup_can_stall : 1,
  112. setup_out_ready : 1,
  113. setup_out_error : 1,
  114. setup_abort : 1;
  115. unsigned setup_wLength;
  116. /* the rest is basically write-once */
  117. struct usb_config_descriptor *config, *hs_config;
  118. struct usb_device_descriptor *dev;
  119. struct usb_request *req;
  120. struct usb_gadget *gadget;
  121. struct list_head epfiles;
  122. void *buf;
  123. wait_queue_head_t wait;
  124. struct super_block *sb;
  125. struct dentry *dentry;
  126. /* except this scratch i/o buffer for ep0 */
  127. u8 rbuf [256];
  128. };
  129. static inline void get_dev (struct dev_data *data)
  130. {
  131. atomic_inc (&data->count);
  132. }
  133. static void put_dev (struct dev_data *data)
  134. {
  135. if (likely (!atomic_dec_and_test (&data->count)))
  136. return;
  137. /* needs no more cleanup */
  138. BUG_ON (waitqueue_active (&data->wait));
  139. kfree (data);
  140. }
  141. static struct dev_data *dev_new (void)
  142. {
  143. struct dev_data *dev;
  144. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  145. if (!dev)
  146. return NULL;
  147. dev->state = STATE_DEV_DISABLED;
  148. atomic_set (&dev->count, 1);
  149. spin_lock_init (&dev->lock);
  150. INIT_LIST_HEAD (&dev->epfiles);
  151. init_waitqueue_head (&dev->wait);
  152. return dev;
  153. }
  154. /*----------------------------------------------------------------------*/
  155. /* other /dev/gadget/$ENDPOINT files represent endpoints */
  156. enum ep_state {
  157. STATE_EP_DISABLED = 0,
  158. STATE_EP_READY,
  159. STATE_EP_ENABLED,
  160. STATE_EP_UNBOUND,
  161. };
  162. struct ep_data {
  163. struct mutex lock;
  164. enum ep_state state;
  165. atomic_t count;
  166. struct dev_data *dev;
  167. /* must hold dev->lock before accessing ep or req */
  168. struct usb_ep *ep;
  169. struct usb_request *req;
  170. ssize_t status;
  171. char name [16];
  172. struct usb_endpoint_descriptor desc, hs_desc;
  173. struct list_head epfiles;
  174. wait_queue_head_t wait;
  175. struct dentry *dentry;
  176. };
  177. static inline void get_ep (struct ep_data *data)
  178. {
  179. atomic_inc (&data->count);
  180. }
  181. static void put_ep (struct ep_data *data)
  182. {
  183. if (likely (!atomic_dec_and_test (&data->count)))
  184. return;
  185. put_dev (data->dev);
  186. /* needs no more cleanup */
  187. BUG_ON (!list_empty (&data->epfiles));
  188. BUG_ON (waitqueue_active (&data->wait));
  189. kfree (data);
  190. }
  191. /*----------------------------------------------------------------------*/
  192. /* most "how to use the hardware" policy choices are in userspace:
  193. * mapping endpoint roles (which the driver needs) to the capabilities
  194. * which the usb controller has. most of those capabilities are exposed
  195. * implicitly, starting with the driver name and then endpoint names.
  196. */
  197. static const char *CHIP;
  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. // FALLTHRU
  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_event_interruptible (done.wait, done.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_event (done.wait, done.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. epdata->status = -ENODEV;
  311. }
  312. }
  313. return epdata->status;
  314. }
  315. return value;
  316. }
  317. static int
  318. ep_release (struct inode *inode, struct file *fd)
  319. {
  320. struct ep_data *data = fd->private_data;
  321. int value;
  322. value = mutex_lock_interruptible(&data->lock);
  323. if (value < 0)
  324. return value;
  325. /* clean up if this can be reopened */
  326. if (data->state != STATE_EP_UNBOUND) {
  327. data->state = STATE_EP_DISABLED;
  328. data->desc.bDescriptorType = 0;
  329. data->hs_desc.bDescriptorType = 0;
  330. usb_ep_disable(data->ep);
  331. }
  332. mutex_unlock(&data->lock);
  333. put_ep (data);
  334. return 0;
  335. }
  336. static long ep_ioctl(struct file *fd, unsigned code, unsigned long value)
  337. {
  338. struct ep_data *data = fd->private_data;
  339. int status;
  340. if ((status = get_ready_ep (fd->f_flags, data, false)) < 0)
  341. return status;
  342. spin_lock_irq (&data->dev->lock);
  343. if (likely (data->ep != NULL)) {
  344. switch (code) {
  345. case GADGETFS_FIFO_STATUS:
  346. status = usb_ep_fifo_status (data->ep);
  347. break;
  348. case GADGETFS_FIFO_FLUSH:
  349. usb_ep_fifo_flush (data->ep);
  350. break;
  351. case GADGETFS_CLEAR_HALT:
  352. status = usb_ep_clear_halt (data->ep);
  353. break;
  354. default:
  355. status = -ENOTTY;
  356. }
  357. } else
  358. status = -ENODEV;
  359. spin_unlock_irq (&data->dev->lock);
  360. mutex_unlock(&data->lock);
  361. return status;
  362. }
  363. /*----------------------------------------------------------------------*/
  364. /* ASYNCHRONOUS ENDPOINT I/O OPERATIONS (bulk/intr/iso) */
  365. struct kiocb_priv {
  366. struct usb_request *req;
  367. struct ep_data *epdata;
  368. struct kiocb *iocb;
  369. struct mm_struct *mm;
  370. struct work_struct work;
  371. void *buf;
  372. struct iov_iter to;
  373. const void *to_free;
  374. unsigned actual;
  375. };
  376. static int ep_aio_cancel(struct kiocb *iocb)
  377. {
  378. struct kiocb_priv *priv = iocb->private;
  379. struct ep_data *epdata;
  380. int value;
  381. local_irq_disable();
  382. epdata = priv->epdata;
  383. // spin_lock(&epdata->dev->lock);
  384. if (likely(epdata && epdata->ep && priv->req))
  385. value = usb_ep_dequeue (epdata->ep, priv->req);
  386. else
  387. value = -EINVAL;
  388. // spin_unlock(&epdata->dev->lock);
  389. local_irq_enable();
  390. return value;
  391. }
  392. static void ep_user_copy_worker(struct work_struct *work)
  393. {
  394. struct kiocb_priv *priv = container_of(work, struct kiocb_priv, work);
  395. struct mm_struct *mm = priv->mm;
  396. struct kiocb *iocb = priv->iocb;
  397. size_t ret;
  398. use_mm(mm);
  399. ret = copy_to_iter(priv->buf, priv->actual, &priv->to);
  400. unuse_mm(mm);
  401. if (!ret)
  402. ret = -EFAULT;
  403. /* completing the iocb can drop the ctx and mm, don't touch mm after */
  404. iocb->ki_complete(iocb, ret, ret);
  405. kfree(priv->buf);
  406. kfree(priv->to_free);
  407. kfree(priv);
  408. }
  409. static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
  410. {
  411. struct kiocb *iocb = req->context;
  412. struct kiocb_priv *priv = iocb->private;
  413. struct ep_data *epdata = priv->epdata;
  414. /* lock against disconnect (and ideally, cancel) */
  415. spin_lock(&epdata->dev->lock);
  416. priv->req = NULL;
  417. priv->epdata = NULL;
  418. /* if this was a write or a read returning no data then we
  419. * don't need to copy anything to userspace, so we can
  420. * complete the aio request immediately.
  421. */
  422. if (priv->to_free == NULL || unlikely(req->actual == 0)) {
  423. kfree(req->buf);
  424. kfree(priv->to_free);
  425. kfree(priv);
  426. iocb->private = NULL;
  427. /* aio_complete() reports bytes-transferred _and_ faults */
  428. iocb->ki_complete(iocb, req->actual ? req->actual : req->status,
  429. 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))
  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(buf, len, from) != len)) {
  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. // FALL THROUGH
  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. usb_gadget_unregister_driver (&gadgetfs_driver);
  1042. /* at this point "good" hardware has disconnected the
  1043. * device from USB; the host won't see it any more.
  1044. * alternatively, all host requests will time out.
  1045. */
  1046. kfree (dev->buf);
  1047. dev->buf = NULL;
  1048. /* other endpoints were all decoupled from this device */
  1049. spin_lock_irq(&dev->lock);
  1050. dev->state = STATE_DEV_DISABLED;
  1051. spin_unlock_irq(&dev->lock);
  1052. put_dev (dev);
  1053. return 0;
  1054. }
  1055. static unsigned int
  1056. ep0_poll (struct file *fd, poll_table *wait)
  1057. {
  1058. struct dev_data *dev = fd->private_data;
  1059. int mask = 0;
  1060. if (dev->state <= STATE_DEV_OPENED)
  1061. return DEFAULT_POLLMASK;
  1062. poll_wait(fd, &dev->wait, wait);
  1063. spin_lock_irq (&dev->lock);
  1064. /* report fd mode change before acting on it */
  1065. if (dev->setup_abort) {
  1066. dev->setup_abort = 0;
  1067. mask = POLLHUP;
  1068. goto out;
  1069. }
  1070. if (dev->state == STATE_DEV_SETUP) {
  1071. if (dev->setup_in || dev->setup_can_stall)
  1072. mask = POLLOUT;
  1073. } else {
  1074. if (dev->ev_next != 0)
  1075. mask = POLLIN;
  1076. }
  1077. out:
  1078. spin_unlock_irq(&dev->lock);
  1079. return mask;
  1080. }
  1081. static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
  1082. {
  1083. struct dev_data *dev = fd->private_data;
  1084. struct usb_gadget *gadget = dev->gadget;
  1085. long ret = -ENOTTY;
  1086. spin_lock_irq(&dev->lock);
  1087. if (dev->state == STATE_DEV_OPENED ||
  1088. dev->state == STATE_DEV_UNBOUND) {
  1089. /* Not bound to a UDC */
  1090. } else if (gadget->ops->ioctl) {
  1091. ++dev->udc_usage;
  1092. spin_unlock_irq(&dev->lock);
  1093. ret = gadget->ops->ioctl (gadget, code, value);
  1094. spin_lock_irq(&dev->lock);
  1095. --dev->udc_usage;
  1096. }
  1097. spin_unlock_irq(&dev->lock);
  1098. return ret;
  1099. }
  1100. /*----------------------------------------------------------------------*/
  1101. /* The in-kernel gadget driver handles most ep0 issues, in particular
  1102. * enumerating the single configuration (as provided from user space).
  1103. *
  1104. * Unrecognized ep0 requests may be handled in user space.
  1105. */
  1106. static void make_qualifier (struct dev_data *dev)
  1107. {
  1108. struct usb_qualifier_descriptor qual;
  1109. struct usb_device_descriptor *desc;
  1110. qual.bLength = sizeof qual;
  1111. qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  1112. qual.bcdUSB = cpu_to_le16 (0x0200);
  1113. desc = dev->dev;
  1114. qual.bDeviceClass = desc->bDeviceClass;
  1115. qual.bDeviceSubClass = desc->bDeviceSubClass;
  1116. qual.bDeviceProtocol = desc->bDeviceProtocol;
  1117. /* assumes ep0 uses the same value for both speeds ... */
  1118. qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1119. qual.bNumConfigurations = 1;
  1120. qual.bRESERVED = 0;
  1121. memcpy (dev->rbuf, &qual, sizeof qual);
  1122. }
  1123. static int
  1124. config_buf (struct dev_data *dev, u8 type, unsigned index)
  1125. {
  1126. int len;
  1127. int hs = 0;
  1128. /* only one configuration */
  1129. if (index > 0)
  1130. return -EINVAL;
  1131. if (gadget_is_dualspeed(dev->gadget)) {
  1132. hs = (dev->gadget->speed == USB_SPEED_HIGH);
  1133. if (type == USB_DT_OTHER_SPEED_CONFIG)
  1134. hs = !hs;
  1135. }
  1136. if (hs) {
  1137. dev->req->buf = dev->hs_config;
  1138. len = le16_to_cpu(dev->hs_config->wTotalLength);
  1139. } else {
  1140. dev->req->buf = dev->config;
  1141. len = le16_to_cpu(dev->config->wTotalLength);
  1142. }
  1143. ((u8 *)dev->req->buf) [1] = type;
  1144. return len;
  1145. }
  1146. static int
  1147. gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1148. {
  1149. struct dev_data *dev = get_gadget_data (gadget);
  1150. struct usb_request *req = dev->req;
  1151. int value = -EOPNOTSUPP;
  1152. struct usb_gadgetfs_event *event;
  1153. u16 w_value = le16_to_cpu(ctrl->wValue);
  1154. u16 w_length = le16_to_cpu(ctrl->wLength);
  1155. spin_lock (&dev->lock);
  1156. dev->setup_abort = 0;
  1157. if (dev->state == STATE_DEV_UNCONNECTED) {
  1158. if (gadget_is_dualspeed(gadget)
  1159. && gadget->speed == USB_SPEED_HIGH
  1160. && dev->hs_config == NULL) {
  1161. spin_unlock(&dev->lock);
  1162. ERROR (dev, "no high speed config??\n");
  1163. return -EINVAL;
  1164. }
  1165. dev->state = STATE_DEV_CONNECTED;
  1166. INFO (dev, "connected\n");
  1167. event = next_event (dev, GADGETFS_CONNECT);
  1168. event->u.speed = gadget->speed;
  1169. ep0_readable (dev);
  1170. /* host may have given up waiting for response. we can miss control
  1171. * requests handled lower down (device/endpoint status and features);
  1172. * then ep0_{read,write} will report the wrong status. controller
  1173. * driver will have aborted pending i/o.
  1174. */
  1175. } else if (dev->state == STATE_DEV_SETUP)
  1176. dev->setup_abort = 1;
  1177. req->buf = dev->rbuf;
  1178. req->context = NULL;
  1179. value = -EOPNOTSUPP;
  1180. switch (ctrl->bRequest) {
  1181. case USB_REQ_GET_DESCRIPTOR:
  1182. if (ctrl->bRequestType != USB_DIR_IN)
  1183. goto unrecognized;
  1184. switch (w_value >> 8) {
  1185. case USB_DT_DEVICE:
  1186. value = min (w_length, (u16) sizeof *dev->dev);
  1187. dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1188. req->buf = dev->dev;
  1189. break;
  1190. case USB_DT_DEVICE_QUALIFIER:
  1191. if (!dev->hs_config)
  1192. break;
  1193. value = min (w_length, (u16)
  1194. sizeof (struct usb_qualifier_descriptor));
  1195. make_qualifier (dev);
  1196. break;
  1197. case USB_DT_OTHER_SPEED_CONFIG:
  1198. // FALLTHROUGH
  1199. case USB_DT_CONFIG:
  1200. value = config_buf (dev,
  1201. w_value >> 8,
  1202. w_value & 0xff);
  1203. if (value >= 0)
  1204. value = min (w_length, (u16) value);
  1205. break;
  1206. case USB_DT_STRING:
  1207. goto unrecognized;
  1208. default: // all others are errors
  1209. break;
  1210. }
  1211. break;
  1212. /* currently one config, two speeds */
  1213. case USB_REQ_SET_CONFIGURATION:
  1214. if (ctrl->bRequestType != 0)
  1215. goto unrecognized;
  1216. if (0 == (u8) w_value) {
  1217. value = 0;
  1218. dev->current_config = 0;
  1219. usb_gadget_vbus_draw(gadget, 8 /* mA */ );
  1220. // user mode expected to disable endpoints
  1221. } else {
  1222. u8 config, power;
  1223. if (gadget_is_dualspeed(gadget)
  1224. && gadget->speed == USB_SPEED_HIGH) {
  1225. config = dev->hs_config->bConfigurationValue;
  1226. power = dev->hs_config->bMaxPower;
  1227. } else {
  1228. config = dev->config->bConfigurationValue;
  1229. power = dev->config->bMaxPower;
  1230. }
  1231. if (config == (u8) w_value) {
  1232. value = 0;
  1233. dev->current_config = config;
  1234. usb_gadget_vbus_draw(gadget, 2 * power);
  1235. }
  1236. }
  1237. /* report SET_CONFIGURATION like any other control request,
  1238. * except that usermode may not stall this. the next
  1239. * request mustn't be allowed start until this finishes:
  1240. * endpoints and threads set up, etc.
  1241. *
  1242. * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
  1243. * has bad/racey automagic that prevents synchronizing here.
  1244. * even kernel mode drivers often miss them.
  1245. */
  1246. if (value == 0) {
  1247. INFO (dev, "configuration #%d\n", dev->current_config);
  1248. usb_gadget_set_state(gadget, USB_STATE_CONFIGURED);
  1249. if (dev->usermode_setup) {
  1250. dev->setup_can_stall = 0;
  1251. goto delegate;
  1252. }
  1253. }
  1254. break;
  1255. #ifndef CONFIG_USB_PXA25X
  1256. /* PXA automagically handles this request too */
  1257. case USB_REQ_GET_CONFIGURATION:
  1258. if (ctrl->bRequestType != 0x80)
  1259. goto unrecognized;
  1260. *(u8 *)req->buf = dev->current_config;
  1261. value = min (w_length, (u16) 1);
  1262. break;
  1263. #endif
  1264. default:
  1265. unrecognized:
  1266. VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
  1267. dev->usermode_setup ? "delegate" : "fail",
  1268. ctrl->bRequestType, ctrl->bRequest,
  1269. w_value, le16_to_cpu(ctrl->wIndex), w_length);
  1270. /* if there's an ep0 reader, don't stall */
  1271. if (dev->usermode_setup) {
  1272. dev->setup_can_stall = 1;
  1273. delegate:
  1274. dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
  1275. ? 1 : 0;
  1276. dev->setup_wLength = w_length;
  1277. dev->setup_out_ready = 0;
  1278. dev->setup_out_error = 0;
  1279. value = 0;
  1280. /* read DATA stage for OUT right away */
  1281. if (unlikely (!dev->setup_in && w_length)) {
  1282. value = setup_req (gadget->ep0, dev->req,
  1283. w_length);
  1284. if (value < 0)
  1285. break;
  1286. ++dev->udc_usage;
  1287. spin_unlock (&dev->lock);
  1288. value = usb_ep_queue (gadget->ep0, dev->req,
  1289. GFP_KERNEL);
  1290. spin_lock (&dev->lock);
  1291. --dev->udc_usage;
  1292. if (value < 0) {
  1293. clean_req (gadget->ep0, dev->req);
  1294. break;
  1295. }
  1296. /* we can't currently stall these */
  1297. dev->setup_can_stall = 0;
  1298. }
  1299. /* state changes when reader collects event */
  1300. event = next_event (dev, GADGETFS_SETUP);
  1301. event->u.setup = *ctrl;
  1302. ep0_readable (dev);
  1303. spin_unlock (&dev->lock);
  1304. return 0;
  1305. }
  1306. }
  1307. /* proceed with data transfer and status phases? */
  1308. if (value >= 0 && dev->state != STATE_DEV_SETUP) {
  1309. req->length = value;
  1310. req->zero = value < w_length;
  1311. ++dev->udc_usage;
  1312. spin_unlock (&dev->lock);
  1313. value = usb_ep_queue (gadget->ep0, req, GFP_KERNEL);
  1314. spin_lock(&dev->lock);
  1315. --dev->udc_usage;
  1316. spin_unlock(&dev->lock);
  1317. if (value < 0) {
  1318. DBG (dev, "ep_queue --> %d\n", value);
  1319. req->status = 0;
  1320. }
  1321. return value;
  1322. }
  1323. /* device stalls when value < 0 */
  1324. spin_unlock (&dev->lock);
  1325. return value;
  1326. }
  1327. static void destroy_ep_files (struct dev_data *dev)
  1328. {
  1329. DBG (dev, "%s %d\n", __func__, dev->state);
  1330. /* dev->state must prevent interference */
  1331. spin_lock_irq (&dev->lock);
  1332. while (!list_empty(&dev->epfiles)) {
  1333. struct ep_data *ep;
  1334. struct inode *parent;
  1335. struct dentry *dentry;
  1336. /* break link to FS */
  1337. ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
  1338. list_del_init (&ep->epfiles);
  1339. spin_unlock_irq (&dev->lock);
  1340. dentry = ep->dentry;
  1341. ep->dentry = NULL;
  1342. parent = d_inode(dentry->d_parent);
  1343. /* break link to controller */
  1344. mutex_lock(&ep->lock);
  1345. if (ep->state == STATE_EP_ENABLED)
  1346. (void) usb_ep_disable (ep->ep);
  1347. ep->state = STATE_EP_UNBOUND;
  1348. usb_ep_free_request (ep->ep, ep->req);
  1349. ep->ep = NULL;
  1350. mutex_unlock(&ep->lock);
  1351. wake_up (&ep->wait);
  1352. put_ep (ep);
  1353. /* break link to dcache */
  1354. mutex_lock (&parent->i_mutex);
  1355. d_delete (dentry);
  1356. dput (dentry);
  1357. mutex_unlock (&parent->i_mutex);
  1358. spin_lock_irq (&dev->lock);
  1359. }
  1360. spin_unlock_irq (&dev->lock);
  1361. }
  1362. static struct dentry *
  1363. gadgetfs_create_file (struct super_block *sb, char const *name,
  1364. void *data, const struct file_operations *fops);
  1365. static int activate_ep_files (struct dev_data *dev)
  1366. {
  1367. struct usb_ep *ep;
  1368. struct ep_data *data;
  1369. gadget_for_each_ep (ep, dev->gadget) {
  1370. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1371. if (!data)
  1372. goto enomem0;
  1373. data->state = STATE_EP_DISABLED;
  1374. mutex_init(&data->lock);
  1375. init_waitqueue_head (&data->wait);
  1376. strncpy (data->name, ep->name, sizeof (data->name) - 1);
  1377. atomic_set (&data->count, 1);
  1378. data->dev = dev;
  1379. get_dev (dev);
  1380. data->ep = ep;
  1381. ep->driver_data = data;
  1382. data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
  1383. if (!data->req)
  1384. goto enomem1;
  1385. data->dentry = gadgetfs_create_file (dev->sb, data->name,
  1386. data, &ep_io_operations);
  1387. if (!data->dentry)
  1388. goto enomem2;
  1389. list_add_tail (&data->epfiles, &dev->epfiles);
  1390. }
  1391. return 0;
  1392. enomem2:
  1393. usb_ep_free_request (ep, data->req);
  1394. enomem1:
  1395. put_dev (dev);
  1396. kfree (data);
  1397. enomem0:
  1398. DBG (dev, "%s enomem\n", __func__);
  1399. destroy_ep_files (dev);
  1400. return -ENOMEM;
  1401. }
  1402. static void
  1403. gadgetfs_unbind (struct usb_gadget *gadget)
  1404. {
  1405. struct dev_data *dev = get_gadget_data (gadget);
  1406. DBG (dev, "%s\n", __func__);
  1407. spin_lock_irq (&dev->lock);
  1408. dev->state = STATE_DEV_UNBOUND;
  1409. while (dev->udc_usage > 0) {
  1410. spin_unlock_irq(&dev->lock);
  1411. usleep_range(1000, 2000);
  1412. spin_lock_irq(&dev->lock);
  1413. }
  1414. spin_unlock_irq (&dev->lock);
  1415. destroy_ep_files (dev);
  1416. gadget->ep0->driver_data = NULL;
  1417. set_gadget_data (gadget, NULL);
  1418. /* we've already been disconnected ... no i/o is active */
  1419. if (dev->req)
  1420. usb_ep_free_request (gadget->ep0, dev->req);
  1421. DBG (dev, "%s done\n", __func__);
  1422. put_dev (dev);
  1423. }
  1424. static struct dev_data *the_device;
  1425. static int gadgetfs_bind(struct usb_gadget *gadget,
  1426. struct usb_gadget_driver *driver)
  1427. {
  1428. struct dev_data *dev = the_device;
  1429. if (!dev)
  1430. return -ESRCH;
  1431. if (0 != strcmp (CHIP, gadget->name)) {
  1432. pr_err("%s expected %s controller not %s\n",
  1433. shortname, CHIP, gadget->name);
  1434. return -ENODEV;
  1435. }
  1436. set_gadget_data (gadget, dev);
  1437. dev->gadget = gadget;
  1438. gadget->ep0->driver_data = dev;
  1439. /* preallocate control response and buffer */
  1440. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1441. if (!dev->req)
  1442. goto enomem;
  1443. dev->req->context = NULL;
  1444. dev->req->complete = epio_complete;
  1445. if (activate_ep_files (dev) < 0)
  1446. goto enomem;
  1447. INFO (dev, "bound to %s driver\n", gadget->name);
  1448. spin_lock_irq(&dev->lock);
  1449. dev->state = STATE_DEV_UNCONNECTED;
  1450. spin_unlock_irq(&dev->lock);
  1451. get_dev (dev);
  1452. return 0;
  1453. enomem:
  1454. gadgetfs_unbind (gadget);
  1455. return -ENOMEM;
  1456. }
  1457. static void
  1458. gadgetfs_disconnect (struct usb_gadget *gadget)
  1459. {
  1460. struct dev_data *dev = get_gadget_data (gadget);
  1461. unsigned long flags;
  1462. spin_lock_irqsave (&dev->lock, flags);
  1463. if (dev->state == STATE_DEV_UNCONNECTED)
  1464. goto exit;
  1465. dev->state = STATE_DEV_UNCONNECTED;
  1466. INFO (dev, "disconnected\n");
  1467. next_event (dev, GADGETFS_DISCONNECT);
  1468. ep0_readable (dev);
  1469. exit:
  1470. spin_unlock_irqrestore (&dev->lock, flags);
  1471. }
  1472. static void
  1473. gadgetfs_suspend (struct usb_gadget *gadget)
  1474. {
  1475. struct dev_data *dev = get_gadget_data (gadget);
  1476. unsigned long flags;
  1477. INFO (dev, "suspended from state %d\n", dev->state);
  1478. spin_lock_irqsave(&dev->lock, flags);
  1479. switch (dev->state) {
  1480. case STATE_DEV_SETUP: // VERY odd... host died??
  1481. case STATE_DEV_CONNECTED:
  1482. case STATE_DEV_UNCONNECTED:
  1483. next_event (dev, GADGETFS_SUSPEND);
  1484. ep0_readable (dev);
  1485. /* FALLTHROUGH */
  1486. default:
  1487. break;
  1488. }
  1489. spin_unlock_irqrestore(&dev->lock, flags);
  1490. }
  1491. static struct usb_gadget_driver gadgetfs_driver = {
  1492. .function = (char *) driver_desc,
  1493. .bind = gadgetfs_bind,
  1494. .unbind = gadgetfs_unbind,
  1495. .setup = gadgetfs_setup,
  1496. .reset = gadgetfs_disconnect,
  1497. .disconnect = gadgetfs_disconnect,
  1498. .suspend = gadgetfs_suspend,
  1499. .driver = {
  1500. .name = (char *) shortname,
  1501. },
  1502. };
  1503. /*----------------------------------------------------------------------*/
  1504. static void gadgetfs_nop(struct usb_gadget *arg) { }
  1505. static int gadgetfs_probe(struct usb_gadget *gadget,
  1506. struct usb_gadget_driver *driver)
  1507. {
  1508. CHIP = gadget->name;
  1509. return -EISNAM;
  1510. }
  1511. static struct usb_gadget_driver probe_driver = {
  1512. .max_speed = USB_SPEED_HIGH,
  1513. .bind = gadgetfs_probe,
  1514. .unbind = gadgetfs_nop,
  1515. .setup = (void *)gadgetfs_nop,
  1516. .disconnect = gadgetfs_nop,
  1517. .driver = {
  1518. .name = "nop",
  1519. },
  1520. };
  1521. /* DEVICE INITIALIZATION
  1522. *
  1523. * fd = open ("/dev/gadget/$CHIP", O_RDWR)
  1524. * status = write (fd, descriptors, sizeof descriptors)
  1525. *
  1526. * That write establishes the device configuration, so the kernel can
  1527. * bind to the controller ... guaranteeing it can handle enumeration
  1528. * at all necessary speeds. Descriptor order is:
  1529. *
  1530. * . message tag (u32, host order) ... for now, must be zero; it
  1531. * would change to support features like multi-config devices
  1532. * . full/low speed config ... all wTotalLength bytes (with interface,
  1533. * class, altsetting, endpoint, and other descriptors)
  1534. * . high speed config ... all descriptors, for high speed operation;
  1535. * this one's optional except for high-speed hardware
  1536. * . device descriptor
  1537. *
  1538. * Endpoints are not yet enabled. Drivers must wait until device
  1539. * configuration and interface altsetting changes create
  1540. * the need to configure (or unconfigure) them.
  1541. *
  1542. * After initialization, the device stays active for as long as that
  1543. * $CHIP file is open. Events must then be read from that descriptor,
  1544. * such as configuration notifications.
  1545. */
  1546. static int is_valid_config(struct usb_config_descriptor *config,
  1547. unsigned int total)
  1548. {
  1549. return config->bDescriptorType == USB_DT_CONFIG
  1550. && config->bLength == USB_DT_CONFIG_SIZE
  1551. && total >= USB_DT_CONFIG_SIZE
  1552. && config->bConfigurationValue != 0
  1553. && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
  1554. && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
  1555. /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
  1556. /* FIXME check lengths: walk to end */
  1557. }
  1558. static ssize_t
  1559. dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1560. {
  1561. struct dev_data *dev = fd->private_data;
  1562. ssize_t value = len, length = len;
  1563. unsigned total;
  1564. u32 tag;
  1565. char *kbuf;
  1566. spin_lock_irq(&dev->lock);
  1567. if (dev->state > STATE_DEV_OPENED) {
  1568. value = ep0_write(fd, buf, len, ptr);
  1569. spin_unlock_irq(&dev->lock);
  1570. return value;
  1571. }
  1572. spin_unlock_irq(&dev->lock);
  1573. if ((len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4)) ||
  1574. (len > PAGE_SIZE * 4))
  1575. return -EINVAL;
  1576. /* we might need to change message format someday */
  1577. if (copy_from_user (&tag, buf, 4))
  1578. return -EFAULT;
  1579. if (tag != 0)
  1580. return -EINVAL;
  1581. buf += 4;
  1582. length -= 4;
  1583. kbuf = memdup_user(buf, length);
  1584. if (IS_ERR(kbuf))
  1585. return PTR_ERR(kbuf);
  1586. spin_lock_irq (&dev->lock);
  1587. value = -EINVAL;
  1588. if (dev->buf) {
  1589. kfree(kbuf);
  1590. goto fail;
  1591. }
  1592. dev->buf = kbuf;
  1593. /* full or low speed config */
  1594. dev->config = (void *) kbuf;
  1595. total = le16_to_cpu(dev->config->wTotalLength);
  1596. if (!is_valid_config(dev->config, total) ||
  1597. total > length - USB_DT_DEVICE_SIZE)
  1598. goto fail;
  1599. kbuf += total;
  1600. length -= total;
  1601. /* optional high speed config */
  1602. if (kbuf [1] == USB_DT_CONFIG) {
  1603. dev->hs_config = (void *) kbuf;
  1604. total = le16_to_cpu(dev->hs_config->wTotalLength);
  1605. if (!is_valid_config(dev->hs_config, total) ||
  1606. total > length - USB_DT_DEVICE_SIZE)
  1607. goto fail;
  1608. kbuf += total;
  1609. length -= total;
  1610. } else {
  1611. dev->hs_config = NULL;
  1612. }
  1613. /* could support multiple configs, using another encoding! */
  1614. /* device descriptor (tweaked for paranoia) */
  1615. if (length != USB_DT_DEVICE_SIZE)
  1616. goto fail;
  1617. dev->dev = (void *)kbuf;
  1618. if (dev->dev->bLength != USB_DT_DEVICE_SIZE
  1619. || dev->dev->bDescriptorType != USB_DT_DEVICE
  1620. || dev->dev->bNumConfigurations != 1)
  1621. goto fail;
  1622. dev->dev->bNumConfigurations = 1;
  1623. dev->dev->bcdUSB = cpu_to_le16 (0x0200);
  1624. /* triggers gadgetfs_bind(); then we can enumerate. */
  1625. spin_unlock_irq (&dev->lock);
  1626. if (dev->hs_config)
  1627. gadgetfs_driver.max_speed = USB_SPEED_HIGH;
  1628. else
  1629. gadgetfs_driver.max_speed = USB_SPEED_FULL;
  1630. value = usb_gadget_probe_driver(&gadgetfs_driver);
  1631. if (value != 0) {
  1632. kfree (dev->buf);
  1633. dev->buf = NULL;
  1634. } else {
  1635. /* at this point "good" hardware has for the first time
  1636. * let the USB the host see us. alternatively, if users
  1637. * unplug/replug that will clear all the error state.
  1638. *
  1639. * note: everything running before here was guaranteed
  1640. * to choke driver model style diagnostics. from here
  1641. * on, they can work ... except in cleanup paths that
  1642. * kick in after the ep0 descriptor is closed.
  1643. */
  1644. value = len;
  1645. }
  1646. return value;
  1647. fail:
  1648. spin_unlock_irq (&dev->lock);
  1649. pr_debug ("%s: %s fail %Zd, %p\n", shortname, __func__, value, dev);
  1650. kfree (dev->buf);
  1651. dev->buf = NULL;
  1652. return value;
  1653. }
  1654. static int
  1655. dev_open (struct inode *inode, struct file *fd)
  1656. {
  1657. struct dev_data *dev = inode->i_private;
  1658. int value = -EBUSY;
  1659. spin_lock_irq(&dev->lock);
  1660. if (dev->state == STATE_DEV_DISABLED) {
  1661. dev->ev_next = 0;
  1662. dev->state = STATE_DEV_OPENED;
  1663. fd->private_data = dev;
  1664. get_dev (dev);
  1665. value = 0;
  1666. }
  1667. spin_unlock_irq(&dev->lock);
  1668. return value;
  1669. }
  1670. static const struct file_operations ep0_operations = {
  1671. .llseek = no_llseek,
  1672. .open = dev_open,
  1673. .read = ep0_read,
  1674. .write = dev_config,
  1675. .fasync = ep0_fasync,
  1676. .poll = ep0_poll,
  1677. .unlocked_ioctl = dev_ioctl,
  1678. .release = dev_release,
  1679. };
  1680. /*----------------------------------------------------------------------*/
  1681. /* FILESYSTEM AND SUPERBLOCK OPERATIONS
  1682. *
  1683. * Mounting the filesystem creates a controller file, used first for
  1684. * device configuration then later for event monitoring.
  1685. */
  1686. /* FIXME PAM etc could set this security policy without mount options
  1687. * if epfiles inherited ownership and permissons from ep0 ...
  1688. */
  1689. static unsigned default_uid;
  1690. static unsigned default_gid;
  1691. static unsigned default_perm = S_IRUSR | S_IWUSR;
  1692. module_param (default_uid, uint, 0644);
  1693. module_param (default_gid, uint, 0644);
  1694. module_param (default_perm, uint, 0644);
  1695. static struct inode *
  1696. gadgetfs_make_inode (struct super_block *sb,
  1697. void *data, const struct file_operations *fops,
  1698. int mode)
  1699. {
  1700. struct inode *inode = new_inode (sb);
  1701. if (inode) {
  1702. inode->i_ino = get_next_ino();
  1703. inode->i_mode = mode;
  1704. inode->i_uid = make_kuid(&init_user_ns, default_uid);
  1705. inode->i_gid = make_kgid(&init_user_ns, default_gid);
  1706. inode->i_atime = inode->i_mtime = inode->i_ctime
  1707. = CURRENT_TIME;
  1708. inode->i_private = data;
  1709. inode->i_fop = fops;
  1710. }
  1711. return inode;
  1712. }
  1713. /* creates in fs root directory, so non-renamable and non-linkable.
  1714. * so inode and dentry are paired, until device reconfig.
  1715. */
  1716. static struct dentry *
  1717. gadgetfs_create_file (struct super_block *sb, char const *name,
  1718. void *data, const struct file_operations *fops)
  1719. {
  1720. struct dentry *dentry;
  1721. struct inode *inode;
  1722. dentry = d_alloc_name(sb->s_root, name);
  1723. if (!dentry)
  1724. return NULL;
  1725. inode = gadgetfs_make_inode (sb, data, fops,
  1726. S_IFREG | (default_perm & S_IRWXUGO));
  1727. if (!inode) {
  1728. dput(dentry);
  1729. return NULL;
  1730. }
  1731. d_add (dentry, inode);
  1732. return dentry;
  1733. }
  1734. static const struct super_operations gadget_fs_operations = {
  1735. .statfs = simple_statfs,
  1736. .drop_inode = generic_delete_inode,
  1737. };
  1738. static int
  1739. gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
  1740. {
  1741. struct inode *inode;
  1742. struct dev_data *dev;
  1743. if (the_device)
  1744. return -ESRCH;
  1745. /* fake probe to determine $CHIP */
  1746. CHIP = NULL;
  1747. usb_gadget_probe_driver(&probe_driver);
  1748. if (!CHIP)
  1749. return -ENODEV;
  1750. /* superblock */
  1751. sb->s_blocksize = PAGE_CACHE_SIZE;
  1752. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1753. sb->s_magic = GADGETFS_MAGIC;
  1754. sb->s_op = &gadget_fs_operations;
  1755. sb->s_time_gran = 1;
  1756. /* root inode */
  1757. inode = gadgetfs_make_inode (sb,
  1758. NULL, &simple_dir_operations,
  1759. S_IFDIR | S_IRUGO | S_IXUGO);
  1760. if (!inode)
  1761. goto Enomem;
  1762. inode->i_op = &simple_dir_inode_operations;
  1763. if (!(sb->s_root = d_make_root (inode)))
  1764. goto Enomem;
  1765. /* the ep0 file is named after the controller we expect;
  1766. * user mode code can use it for sanity checks, like we do.
  1767. */
  1768. dev = dev_new ();
  1769. if (!dev)
  1770. goto Enomem;
  1771. dev->sb = sb;
  1772. dev->dentry = gadgetfs_create_file(sb, CHIP, dev, &ep0_operations);
  1773. if (!dev->dentry) {
  1774. put_dev(dev);
  1775. goto Enomem;
  1776. }
  1777. /* other endpoint files are available after hardware setup,
  1778. * from binding to a controller.
  1779. */
  1780. the_device = dev;
  1781. return 0;
  1782. Enomem:
  1783. return -ENOMEM;
  1784. }
  1785. /* "mount -t gadgetfs path /dev/gadget" ends up here */
  1786. static struct dentry *
  1787. gadgetfs_mount (struct file_system_type *t, int flags,
  1788. const char *path, void *opts)
  1789. {
  1790. return mount_single (t, flags, opts, gadgetfs_fill_super);
  1791. }
  1792. static void
  1793. gadgetfs_kill_sb (struct super_block *sb)
  1794. {
  1795. kill_litter_super (sb);
  1796. if (the_device) {
  1797. put_dev (the_device);
  1798. the_device = NULL;
  1799. }
  1800. }
  1801. /*----------------------------------------------------------------------*/
  1802. static struct file_system_type gadgetfs_type = {
  1803. .owner = THIS_MODULE,
  1804. .name = shortname,
  1805. .mount = gadgetfs_mount,
  1806. .kill_sb = gadgetfs_kill_sb,
  1807. };
  1808. MODULE_ALIAS_FS("gadgetfs");
  1809. /*----------------------------------------------------------------------*/
  1810. static int __init init (void)
  1811. {
  1812. int status;
  1813. status = register_filesystem (&gadgetfs_type);
  1814. if (status == 0)
  1815. pr_info ("%s: %s, version " DRIVER_VERSION "\n",
  1816. shortname, driver_desc);
  1817. return status;
  1818. }
  1819. module_init (init);
  1820. static void __exit cleanup (void)
  1821. {
  1822. pr_debug ("unregister %s\n", shortname);
  1823. unregister_filesystem (&gadgetfs_type);
  1824. }
  1825. module_exit (cleanup);