inode.c 51 KB

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