inode.c 52 KB

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