inode.c 52 KB

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