inode.c 51 KB

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