inode.c 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155
  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. #define RBUF_SIZE 256
  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 [RBUF_SIZE];
  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 %pK 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. void *buf;
  441. const struct iovec *iv;
  442. unsigned long nr_segs;
  443. unsigned actual;
  444. };
  445. static int ep_aio_cancel(struct kiocb *iocb, struct io_event *e)
  446. {
  447. struct kiocb_priv *priv = iocb->private;
  448. struct ep_data *epdata;
  449. int value;
  450. local_irq_disable();
  451. epdata = priv->epdata;
  452. // spin_lock(&epdata->dev->lock);
  453. kiocbSetCancelled(iocb);
  454. if (likely(epdata && epdata->ep && priv->req))
  455. value = usb_ep_dequeue (epdata->ep, priv->req);
  456. else
  457. value = -EINVAL;
  458. // spin_unlock(&epdata->dev->lock);
  459. local_irq_enable();
  460. aio_put_req(iocb);
  461. return value;
  462. }
  463. static ssize_t ep_aio_read_retry(struct kiocb *iocb)
  464. {
  465. struct kiocb_priv *priv = iocb->private;
  466. ssize_t len, total;
  467. void *to_copy;
  468. int i;
  469. /* we "retry" to get the right mm context for this: */
  470. /* copy stuff into user buffers */
  471. total = priv->actual;
  472. len = 0;
  473. to_copy = priv->buf;
  474. for (i=0; i < priv->nr_segs; i++) {
  475. ssize_t this = min((ssize_t)(priv->iv[i].iov_len), total);
  476. if (copy_to_user(priv->iv[i].iov_base, to_copy, this)) {
  477. if (len == 0)
  478. len = -EFAULT;
  479. break;
  480. }
  481. total -= this;
  482. len += this;
  483. to_copy += this;
  484. if (total == 0)
  485. break;
  486. }
  487. kfree(priv->buf);
  488. kfree(priv->iv);
  489. kfree(priv);
  490. return len;
  491. }
  492. static void ep_aio_complete(struct usb_ep *ep, struct usb_request *req)
  493. {
  494. struct kiocb *iocb = req->context;
  495. struct kiocb_priv *priv = iocb->private;
  496. struct ep_data *epdata = priv->epdata;
  497. /* lock against disconnect (and ideally, cancel) */
  498. spin_lock(&epdata->dev->lock);
  499. priv->req = NULL;
  500. priv->epdata = NULL;
  501. /* if this was a write or a read returning no data then we
  502. * don't need to copy anything to userspace, so we can
  503. * complete the aio request immediately.
  504. */
  505. if (priv->iv == NULL || unlikely(req->actual == 0)) {
  506. kfree(req->buf);
  507. kfree(priv->iv);
  508. kfree(priv);
  509. iocb->private = NULL;
  510. /* aio_complete() reports bytes-transferred _and_ faults */
  511. aio_complete(iocb, req->actual ? req->actual : req->status,
  512. req->status);
  513. } else {
  514. /* retry() won't report both; so we hide some faults */
  515. if (unlikely(0 != req->status))
  516. DBG(epdata->dev, "%s fault %d len %d\n",
  517. ep->name, req->status, req->actual);
  518. priv->buf = req->buf;
  519. priv->actual = req->actual;
  520. kick_iocb(iocb);
  521. }
  522. spin_unlock(&epdata->dev->lock);
  523. usb_ep_free_request(ep, req);
  524. put_ep(epdata);
  525. }
  526. static ssize_t
  527. ep_aio_rwtail(
  528. struct kiocb *iocb,
  529. char *buf,
  530. size_t len,
  531. struct ep_data *epdata,
  532. const struct iovec *iv,
  533. unsigned long nr_segs
  534. )
  535. {
  536. struct kiocb_priv *priv;
  537. struct usb_request *req;
  538. ssize_t value;
  539. priv = kzalloc(sizeof *priv, GFP_KERNEL);
  540. if (!priv) {
  541. value = -ENOMEM;
  542. fail:
  543. kfree(buf);
  544. return value;
  545. }
  546. iocb->private = priv;
  547. if (iv) {
  548. priv->iv = kmemdup(iv, nr_segs * sizeof(struct iovec),
  549. GFP_KERNEL);
  550. if (!priv->iv) {
  551. kfree(priv);
  552. goto fail;
  553. }
  554. }
  555. priv->nr_segs = nr_segs;
  556. value = get_ready_ep(iocb->ki_filp->f_flags, epdata);
  557. if (unlikely(value < 0)) {
  558. kfree(priv->iv);
  559. kfree(priv);
  560. goto fail;
  561. }
  562. iocb->ki_cancel = ep_aio_cancel;
  563. get_ep(epdata);
  564. priv->epdata = epdata;
  565. priv->actual = 0;
  566. /* each kiocb is coupled to one usb_request, but we can't
  567. * allocate or submit those if the host disconnected.
  568. */
  569. spin_lock_irq(&epdata->dev->lock);
  570. if (likely(epdata->ep)) {
  571. req = usb_ep_alloc_request(epdata->ep, GFP_ATOMIC);
  572. if (likely(req)) {
  573. priv->req = req;
  574. req->buf = buf;
  575. req->length = len;
  576. req->complete = ep_aio_complete;
  577. req->context = iocb;
  578. value = usb_ep_queue(epdata->ep, req, GFP_ATOMIC);
  579. if (unlikely(0 != value))
  580. usb_ep_free_request(epdata->ep, req);
  581. } else
  582. value = -EAGAIN;
  583. } else
  584. value = -ENODEV;
  585. spin_unlock_irq(&epdata->dev->lock);
  586. mutex_unlock(&epdata->lock);
  587. if (unlikely(value)) {
  588. kfree(priv->iv);
  589. kfree(priv);
  590. put_ep(epdata);
  591. } else
  592. value = (iv ? -EIOCBRETRY : -EIOCBQUEUED);
  593. return value;
  594. }
  595. static ssize_t
  596. ep_aio_read(struct kiocb *iocb, const struct iovec *iov,
  597. unsigned long nr_segs, loff_t o)
  598. {
  599. struct ep_data *epdata = iocb->ki_filp->private_data;
  600. char *buf;
  601. if (unlikely(usb_endpoint_dir_in(&epdata->desc)))
  602. return -EINVAL;
  603. buf = kmalloc(iocb->ki_left, GFP_KERNEL);
  604. if (unlikely(!buf))
  605. return -ENOMEM;
  606. iocb->ki_retry = ep_aio_read_retry;
  607. return ep_aio_rwtail(iocb, buf, iocb->ki_left, epdata, iov, nr_segs);
  608. }
  609. static ssize_t
  610. ep_aio_write(struct kiocb *iocb, const struct iovec *iov,
  611. unsigned long nr_segs, loff_t o)
  612. {
  613. struct ep_data *epdata = iocb->ki_filp->private_data;
  614. char *buf;
  615. size_t len = 0;
  616. int i = 0;
  617. if (unlikely(!usb_endpoint_dir_in(&epdata->desc)))
  618. return -EINVAL;
  619. buf = kmalloc(iocb->ki_left, GFP_KERNEL);
  620. if (unlikely(!buf))
  621. return -ENOMEM;
  622. for (i=0; i < nr_segs; i++) {
  623. if (unlikely(copy_from_user(&buf[len], iov[i].iov_base,
  624. iov[i].iov_len) != 0)) {
  625. kfree(buf);
  626. return -EFAULT;
  627. }
  628. len += iov[i].iov_len;
  629. }
  630. return ep_aio_rwtail(iocb, buf, len, epdata, NULL, 0);
  631. }
  632. /*----------------------------------------------------------------------*/
  633. /* used after endpoint configuration */
  634. static const struct file_operations ep_io_operations = {
  635. .owner = THIS_MODULE,
  636. .llseek = no_llseek,
  637. .read = ep_read,
  638. .write = ep_write,
  639. .unlocked_ioctl = ep_ioctl,
  640. .release = ep_release,
  641. .aio_read = ep_aio_read,
  642. .aio_write = ep_aio_write,
  643. };
  644. /* ENDPOINT INITIALIZATION
  645. *
  646. * fd = open ("/dev/gadget/$ENDPOINT", O_RDWR)
  647. * status = write (fd, descriptors, sizeof descriptors)
  648. *
  649. * That write establishes the endpoint configuration, configuring
  650. * the controller to process bulk, interrupt, or isochronous transfers
  651. * at the right maxpacket size, and so on.
  652. *
  653. * The descriptors are message type 1, identified by a host order u32
  654. * at the beginning of what's written. Descriptor order is: full/low
  655. * speed descriptor, then optional high speed descriptor.
  656. */
  657. static ssize_t
  658. ep_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  659. {
  660. struct ep_data *data = fd->private_data;
  661. struct usb_ep *ep;
  662. u32 tag;
  663. int value, length = len;
  664. value = mutex_lock_interruptible(&data->lock);
  665. if (value < 0)
  666. return value;
  667. if (data->state != STATE_EP_READY) {
  668. value = -EL2HLT;
  669. goto fail;
  670. }
  671. value = len;
  672. if (len < USB_DT_ENDPOINT_SIZE + 4)
  673. goto fail0;
  674. /* we might need to change message format someday */
  675. if (copy_from_user (&tag, buf, 4)) {
  676. goto fail1;
  677. }
  678. if (tag != 1) {
  679. DBG(data->dev, "config %s, bad tag %d\n", data->name, tag);
  680. goto fail0;
  681. }
  682. buf += 4;
  683. len -= 4;
  684. /* NOTE: audio endpoint extensions not accepted here;
  685. * just don't include the extra bytes.
  686. */
  687. /* full/low speed descriptor, then high speed */
  688. if (copy_from_user (&data->desc, buf, USB_DT_ENDPOINT_SIZE)) {
  689. goto fail1;
  690. }
  691. if (data->desc.bLength != USB_DT_ENDPOINT_SIZE
  692. || data->desc.bDescriptorType != USB_DT_ENDPOINT)
  693. goto fail0;
  694. if (len != USB_DT_ENDPOINT_SIZE) {
  695. if (len != 2 * USB_DT_ENDPOINT_SIZE)
  696. goto fail0;
  697. if (copy_from_user (&data->hs_desc, buf + USB_DT_ENDPOINT_SIZE,
  698. USB_DT_ENDPOINT_SIZE)) {
  699. goto fail1;
  700. }
  701. if (data->hs_desc.bLength != USB_DT_ENDPOINT_SIZE
  702. || data->hs_desc.bDescriptorType
  703. != USB_DT_ENDPOINT) {
  704. DBG(data->dev, "config %s, bad hs length or type\n",
  705. data->name);
  706. goto fail0;
  707. }
  708. }
  709. spin_lock_irq (&data->dev->lock);
  710. if (data->dev->state == STATE_DEV_UNBOUND) {
  711. value = -ENOENT;
  712. goto gone;
  713. } else if ((ep = data->ep) == NULL) {
  714. value = -ENODEV;
  715. goto gone;
  716. }
  717. switch (data->dev->gadget->speed) {
  718. case USB_SPEED_LOW:
  719. case USB_SPEED_FULL:
  720. ep->desc = &data->desc;
  721. value = usb_ep_enable(ep);
  722. if (value == 0)
  723. data->state = STATE_EP_ENABLED;
  724. break;
  725. case USB_SPEED_HIGH:
  726. /* fails if caller didn't provide that descriptor... */
  727. ep->desc = &data->hs_desc;
  728. value = usb_ep_enable(ep);
  729. if (value == 0)
  730. data->state = STATE_EP_ENABLED;
  731. break;
  732. default:
  733. DBG(data->dev, "unconnected, %s init abandoned\n",
  734. data->name);
  735. value = -EINVAL;
  736. }
  737. if (value == 0) {
  738. fd->f_op = &ep_io_operations;
  739. value = length;
  740. }
  741. gone:
  742. spin_unlock_irq (&data->dev->lock);
  743. if (value < 0) {
  744. fail:
  745. data->desc.bDescriptorType = 0;
  746. data->hs_desc.bDescriptorType = 0;
  747. }
  748. mutex_unlock(&data->lock);
  749. return value;
  750. fail0:
  751. value = -EINVAL;
  752. goto fail;
  753. fail1:
  754. value = -EFAULT;
  755. goto fail;
  756. }
  757. static int
  758. ep_open (struct inode *inode, struct file *fd)
  759. {
  760. struct ep_data *data = inode->i_private;
  761. int value = -EBUSY;
  762. if (mutex_lock_interruptible(&data->lock) != 0)
  763. return -EINTR;
  764. spin_lock_irq (&data->dev->lock);
  765. if (data->dev->state == STATE_DEV_UNBOUND)
  766. value = -ENOENT;
  767. else if (data->state == STATE_EP_DISABLED) {
  768. value = 0;
  769. data->state = STATE_EP_READY;
  770. get_ep (data);
  771. fd->private_data = data;
  772. VDEBUG (data->dev, "%s ready\n", data->name);
  773. } else
  774. DBG (data->dev, "%s state %d\n",
  775. data->name, data->state);
  776. spin_unlock_irq (&data->dev->lock);
  777. mutex_unlock(&data->lock);
  778. return value;
  779. }
  780. /* used before endpoint configuration */
  781. static const struct file_operations ep_config_operations = {
  782. .llseek = no_llseek,
  783. .open = ep_open,
  784. .write = ep_config,
  785. .release = ep_release,
  786. };
  787. /*----------------------------------------------------------------------*/
  788. /* EP0 IMPLEMENTATION can be partly in userspace.
  789. *
  790. * Drivers that use this facility receive various events, including
  791. * control requests the kernel doesn't handle. Drivers that don't
  792. * use this facility may be too simple-minded for real applications.
  793. */
  794. static inline void ep0_readable (struct dev_data *dev)
  795. {
  796. wake_up (&dev->wait);
  797. kill_fasync (&dev->fasync, SIGIO, POLL_IN);
  798. }
  799. static void clean_req (struct usb_ep *ep, struct usb_request *req)
  800. {
  801. struct dev_data *dev = ep->driver_data;
  802. if (req->buf != dev->rbuf) {
  803. kfree(req->buf);
  804. req->buf = dev->rbuf;
  805. req->dma = DMA_ADDR_INVALID;
  806. }
  807. req->complete = epio_complete;
  808. dev->setup_out_ready = 0;
  809. }
  810. static void ep0_complete (struct usb_ep *ep, struct usb_request *req)
  811. {
  812. struct dev_data *dev = ep->driver_data;
  813. unsigned long flags;
  814. int free = 1;
  815. /* for control OUT, data must still get to userspace */
  816. spin_lock_irqsave(&dev->lock, flags);
  817. if (!dev->setup_in) {
  818. dev->setup_out_error = (req->status != 0);
  819. if (!dev->setup_out_error)
  820. free = 0;
  821. dev->setup_out_ready = 1;
  822. ep0_readable (dev);
  823. }
  824. /* clean up as appropriate */
  825. if (free && req->buf != &dev->rbuf)
  826. clean_req (ep, req);
  827. req->complete = epio_complete;
  828. spin_unlock_irqrestore(&dev->lock, flags);
  829. }
  830. static int setup_req (struct usb_ep *ep, struct usb_request *req, u16 len)
  831. {
  832. struct dev_data *dev = ep->driver_data;
  833. if (dev->setup_out_ready) {
  834. DBG (dev, "ep0 request busy!\n");
  835. return -EBUSY;
  836. }
  837. if (len > sizeof (dev->rbuf))
  838. req->buf = kmalloc(len, GFP_ATOMIC);
  839. if (req->buf == NULL) {
  840. req->buf = dev->rbuf;
  841. return -ENOMEM;
  842. }
  843. req->complete = ep0_complete;
  844. req->length = len;
  845. req->zero = 0;
  846. return 0;
  847. }
  848. static ssize_t
  849. ep0_read (struct file *fd, char __user *buf, size_t len, loff_t *ptr)
  850. {
  851. struct dev_data *dev = fd->private_data;
  852. ssize_t retval;
  853. enum ep0_state state;
  854. spin_lock_irq (&dev->lock);
  855. /* report fd mode change before acting on it */
  856. if (dev->setup_abort) {
  857. dev->setup_abort = 0;
  858. retval = -EIDRM;
  859. goto done;
  860. }
  861. /* control DATA stage */
  862. if ((state = dev->state) == STATE_DEV_SETUP) {
  863. if (dev->setup_in) { /* stall IN */
  864. VDEBUG(dev, "ep0in stall\n");
  865. (void) usb_ep_set_halt (dev->gadget->ep0);
  866. retval = -EL2HLT;
  867. dev->state = STATE_DEV_CONNECTED;
  868. } else if (len == 0) { /* ack SET_CONFIGURATION etc */
  869. struct usb_ep *ep = dev->gadget->ep0;
  870. struct usb_request *req = dev->req;
  871. if ((retval = setup_req (ep, req, 0)) == 0)
  872. retval = usb_ep_queue (ep, req, GFP_ATOMIC);
  873. dev->state = STATE_DEV_CONNECTED;
  874. /* assume that was SET_CONFIGURATION */
  875. if (dev->current_config) {
  876. unsigned power;
  877. if (gadget_is_dualspeed(dev->gadget)
  878. && (dev->gadget->speed
  879. == USB_SPEED_HIGH))
  880. power = dev->hs_config->bMaxPower;
  881. else
  882. power = dev->config->bMaxPower;
  883. usb_gadget_vbus_draw(dev->gadget, 2 * power);
  884. }
  885. } else { /* collect OUT data */
  886. if ((fd->f_flags & O_NONBLOCK) != 0
  887. && !dev->setup_out_ready) {
  888. retval = -EAGAIN;
  889. goto done;
  890. }
  891. spin_unlock_irq (&dev->lock);
  892. retval = wait_event_interruptible (dev->wait,
  893. dev->setup_out_ready != 0);
  894. /* FIXME state could change from under us */
  895. spin_lock_irq (&dev->lock);
  896. if (retval)
  897. goto done;
  898. if (dev->state != STATE_DEV_SETUP) {
  899. retval = -ECANCELED;
  900. goto done;
  901. }
  902. dev->state = STATE_DEV_CONNECTED;
  903. if (dev->setup_out_error)
  904. retval = -EIO;
  905. else {
  906. len = min (len, (size_t)dev->req->actual);
  907. // FIXME don't call this with the spinlock held ...
  908. if (copy_to_user (buf, dev->req->buf, len))
  909. retval = -EFAULT;
  910. else
  911. retval = len;
  912. clean_req (dev->gadget->ep0, dev->req);
  913. /* NOTE userspace can't yet choose to stall */
  914. }
  915. }
  916. goto done;
  917. }
  918. /* else normal: return event data */
  919. if (len < sizeof dev->event [0]) {
  920. retval = -EINVAL;
  921. goto done;
  922. }
  923. len -= len % sizeof (struct usb_gadgetfs_event);
  924. dev->usermode_setup = 1;
  925. scan:
  926. /* return queued events right away */
  927. if (dev->ev_next != 0) {
  928. unsigned i, n;
  929. n = len / sizeof (struct usb_gadgetfs_event);
  930. if (dev->ev_next < n)
  931. n = dev->ev_next;
  932. /* ep0 i/o has special semantics during STATE_DEV_SETUP */
  933. for (i = 0; i < n; i++) {
  934. if (dev->event [i].type == GADGETFS_SETUP) {
  935. dev->state = STATE_DEV_SETUP;
  936. n = i + 1;
  937. break;
  938. }
  939. }
  940. spin_unlock_irq (&dev->lock);
  941. len = n * sizeof (struct usb_gadgetfs_event);
  942. if (copy_to_user (buf, &dev->event, len))
  943. retval = -EFAULT;
  944. else
  945. retval = len;
  946. if (len > 0) {
  947. /* NOTE this doesn't guard against broken drivers;
  948. * concurrent ep0 readers may lose events.
  949. */
  950. spin_lock_irq (&dev->lock);
  951. if (dev->ev_next > n) {
  952. memmove(&dev->event[0], &dev->event[n],
  953. sizeof (struct usb_gadgetfs_event)
  954. * (dev->ev_next - n));
  955. }
  956. dev->ev_next -= n;
  957. spin_unlock_irq (&dev->lock);
  958. }
  959. return retval;
  960. }
  961. if (fd->f_flags & O_NONBLOCK) {
  962. retval = -EAGAIN;
  963. goto done;
  964. }
  965. switch (state) {
  966. default:
  967. DBG (dev, "fail %s, state %d\n", __func__, state);
  968. retval = -ESRCH;
  969. break;
  970. case STATE_DEV_UNCONNECTED:
  971. case STATE_DEV_CONNECTED:
  972. spin_unlock_irq (&dev->lock);
  973. DBG (dev, "%s wait\n", __func__);
  974. /* wait for events */
  975. retval = wait_event_interruptible (dev->wait,
  976. dev->ev_next != 0);
  977. if (retval < 0)
  978. return retval;
  979. spin_lock_irq (&dev->lock);
  980. goto scan;
  981. }
  982. done:
  983. spin_unlock_irq (&dev->lock);
  984. return retval;
  985. }
  986. static struct usb_gadgetfs_event *
  987. next_event (struct dev_data *dev, enum usb_gadgetfs_event_type type)
  988. {
  989. struct usb_gadgetfs_event *event;
  990. unsigned i;
  991. switch (type) {
  992. /* these events purge the queue */
  993. case GADGETFS_DISCONNECT:
  994. if (dev->state == STATE_DEV_SETUP)
  995. dev->setup_abort = 1;
  996. // FALL THROUGH
  997. case GADGETFS_CONNECT:
  998. dev->ev_next = 0;
  999. break;
  1000. case GADGETFS_SETUP: /* previous request timed out */
  1001. case GADGETFS_SUSPEND: /* same effect */
  1002. /* these events can't be repeated */
  1003. for (i = 0; i != dev->ev_next; i++) {
  1004. if (dev->event [i].type != type)
  1005. continue;
  1006. DBG(dev, "discard old event[%d] %d\n", i, type);
  1007. dev->ev_next--;
  1008. if (i == dev->ev_next)
  1009. break;
  1010. /* indices start at zero, for simplicity */
  1011. memmove (&dev->event [i], &dev->event [i + 1],
  1012. sizeof (struct usb_gadgetfs_event)
  1013. * (dev->ev_next - i));
  1014. }
  1015. break;
  1016. default:
  1017. BUG ();
  1018. }
  1019. VDEBUG(dev, "event[%d] = %d\n", dev->ev_next, type);
  1020. event = &dev->event [dev->ev_next++];
  1021. BUG_ON (dev->ev_next > N_EVENT);
  1022. memset (event, 0, sizeof *event);
  1023. event->type = type;
  1024. return event;
  1025. }
  1026. static ssize_t
  1027. ep0_write (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1028. {
  1029. struct dev_data *dev = fd->private_data;
  1030. ssize_t retval = -ESRCH;
  1031. spin_lock_irq (&dev->lock);
  1032. /* report fd mode change before acting on it */
  1033. if (dev->setup_abort) {
  1034. dev->setup_abort = 0;
  1035. retval = -EIDRM;
  1036. /* data and/or status stage for control request */
  1037. } else if (dev->state == STATE_DEV_SETUP) {
  1038. len = min_t(size_t, len, dev->setup_wLength);
  1039. if (dev->setup_in) {
  1040. retval = setup_req (dev->gadget->ep0, dev->req, len);
  1041. if (retval == 0) {
  1042. dev->state = STATE_DEV_CONNECTED;
  1043. spin_unlock_irq (&dev->lock);
  1044. if (copy_from_user (dev->req->buf, buf, len))
  1045. retval = -EFAULT;
  1046. else {
  1047. if (len < dev->setup_wLength)
  1048. dev->req->zero = 1;
  1049. retval = usb_ep_queue (
  1050. dev->gadget->ep0, dev->req,
  1051. GFP_KERNEL);
  1052. }
  1053. if (retval < 0) {
  1054. spin_lock_irq (&dev->lock);
  1055. clean_req (dev->gadget->ep0, dev->req);
  1056. spin_unlock_irq (&dev->lock);
  1057. } else
  1058. retval = len;
  1059. return retval;
  1060. }
  1061. /* can stall some OUT transfers */
  1062. } else if (dev->setup_can_stall) {
  1063. VDEBUG(dev, "ep0out stall\n");
  1064. (void) usb_ep_set_halt (dev->gadget->ep0);
  1065. retval = -EL2HLT;
  1066. dev->state = STATE_DEV_CONNECTED;
  1067. } else {
  1068. DBG(dev, "bogus ep0out stall!\n");
  1069. }
  1070. } else
  1071. DBG (dev, "fail %s, state %d\n", __func__, dev->state);
  1072. spin_unlock_irq (&dev->lock);
  1073. return retval;
  1074. }
  1075. static int
  1076. ep0_fasync (int f, struct file *fd, int on)
  1077. {
  1078. struct dev_data *dev = fd->private_data;
  1079. // caller must F_SETOWN before signal delivery happens
  1080. VDEBUG (dev, "%s %s\n", __func__, on ? "on" : "off");
  1081. return fasync_helper (f, fd, on, &dev->fasync);
  1082. }
  1083. static struct usb_gadget_driver gadgetfs_driver;
  1084. static int
  1085. dev_release (struct inode *inode, struct file *fd)
  1086. {
  1087. struct dev_data *dev = fd->private_data;
  1088. /* closing ep0 === shutdown all */
  1089. usb_gadget_unregister_driver (&gadgetfs_driver);
  1090. /* at this point "good" hardware has disconnected the
  1091. * device from USB; the host won't see it any more.
  1092. * alternatively, all host requests will time out.
  1093. */
  1094. kfree (dev->buf);
  1095. dev->buf = NULL;
  1096. put_dev (dev);
  1097. /* other endpoints were all decoupled from this device */
  1098. spin_lock_irq(&dev->lock);
  1099. dev->state = STATE_DEV_DISABLED;
  1100. spin_unlock_irq(&dev->lock);
  1101. return 0;
  1102. }
  1103. static unsigned int
  1104. ep0_poll (struct file *fd, poll_table *wait)
  1105. {
  1106. struct dev_data *dev = fd->private_data;
  1107. int mask = 0;
  1108. poll_wait(fd, &dev->wait, wait);
  1109. spin_lock_irq (&dev->lock);
  1110. /* report fd mode change before acting on it */
  1111. if (dev->setup_abort) {
  1112. dev->setup_abort = 0;
  1113. mask = POLLHUP;
  1114. goto out;
  1115. }
  1116. if (dev->state == STATE_DEV_SETUP) {
  1117. if (dev->setup_in || dev->setup_can_stall)
  1118. mask = POLLOUT;
  1119. } else {
  1120. if (dev->ev_next != 0)
  1121. mask = POLLIN;
  1122. }
  1123. out:
  1124. spin_unlock_irq(&dev->lock);
  1125. return mask;
  1126. }
  1127. static long dev_ioctl (struct file *fd, unsigned code, unsigned long value)
  1128. {
  1129. struct dev_data *dev = fd->private_data;
  1130. struct usb_gadget *gadget = dev->gadget;
  1131. long ret = -ENOTTY;
  1132. if (gadget->ops->ioctl)
  1133. ret = gadget->ops->ioctl (gadget, code, value);
  1134. return ret;
  1135. }
  1136. /* used after device configuration */
  1137. static const struct file_operations ep0_io_operations = {
  1138. .owner = THIS_MODULE,
  1139. .llseek = no_llseek,
  1140. .read = ep0_read,
  1141. .write = ep0_write,
  1142. .fasync = ep0_fasync,
  1143. .poll = ep0_poll,
  1144. .unlocked_ioctl = dev_ioctl,
  1145. .release = dev_release,
  1146. };
  1147. /*----------------------------------------------------------------------*/
  1148. /* The in-kernel gadget driver handles most ep0 issues, in particular
  1149. * enumerating the single configuration (as provided from user space).
  1150. *
  1151. * Unrecognized ep0 requests may be handled in user space.
  1152. */
  1153. static void make_qualifier (struct dev_data *dev)
  1154. {
  1155. struct usb_qualifier_descriptor qual;
  1156. struct usb_device_descriptor *desc;
  1157. qual.bLength = sizeof qual;
  1158. qual.bDescriptorType = USB_DT_DEVICE_QUALIFIER;
  1159. qual.bcdUSB = cpu_to_le16 (0x0200);
  1160. desc = dev->dev;
  1161. qual.bDeviceClass = desc->bDeviceClass;
  1162. qual.bDeviceSubClass = desc->bDeviceSubClass;
  1163. qual.bDeviceProtocol = desc->bDeviceProtocol;
  1164. /* assumes ep0 uses the same value for both speeds ... */
  1165. qual.bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1166. qual.bNumConfigurations = 1;
  1167. qual.bRESERVED = 0;
  1168. memcpy (dev->rbuf, &qual, sizeof qual);
  1169. }
  1170. static int
  1171. config_buf (struct dev_data *dev, u8 type, unsigned index)
  1172. {
  1173. int len;
  1174. int hs = 0;
  1175. /* only one configuration */
  1176. if (index > 0)
  1177. return -EINVAL;
  1178. if (gadget_is_dualspeed(dev->gadget)) {
  1179. hs = (dev->gadget->speed == USB_SPEED_HIGH);
  1180. if (type == USB_DT_OTHER_SPEED_CONFIG)
  1181. hs = !hs;
  1182. }
  1183. if (hs) {
  1184. dev->req->buf = dev->hs_config;
  1185. len = le16_to_cpu(dev->hs_config->wTotalLength);
  1186. } else {
  1187. dev->req->buf = dev->config;
  1188. len = le16_to_cpu(dev->config->wTotalLength);
  1189. }
  1190. ((u8 *)dev->req->buf) [1] = type;
  1191. return len;
  1192. }
  1193. static int
  1194. gadgetfs_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1195. {
  1196. struct dev_data *dev = get_gadget_data (gadget);
  1197. struct usb_request *req = dev->req;
  1198. int value = -EOPNOTSUPP;
  1199. struct usb_gadgetfs_event *event;
  1200. u16 w_value = le16_to_cpu(ctrl->wValue);
  1201. u16 w_length = le16_to_cpu(ctrl->wLength);
  1202. if (w_length > RBUF_SIZE) {
  1203. if (ctrl->bRequestType & USB_DIR_IN) {
  1204. /* Cast away the const, we are going to overwrite on purpose. */
  1205. __le16 *temp = (__le16 *)&ctrl->wLength;
  1206. *temp = cpu_to_le16(RBUF_SIZE);
  1207. w_length = RBUF_SIZE;
  1208. } else {
  1209. return value;
  1210. }
  1211. }
  1212. spin_lock (&dev->lock);
  1213. dev->setup_abort = 0;
  1214. if (dev->state == STATE_DEV_UNCONNECTED) {
  1215. if (gadget_is_dualspeed(gadget)
  1216. && gadget->speed == USB_SPEED_HIGH
  1217. && dev->hs_config == NULL) {
  1218. spin_unlock(&dev->lock);
  1219. ERROR (dev, "no high speed config??\n");
  1220. return -EINVAL;
  1221. }
  1222. dev->state = STATE_DEV_CONNECTED;
  1223. INFO (dev, "connected\n");
  1224. event = next_event (dev, GADGETFS_CONNECT);
  1225. event->u.speed = gadget->speed;
  1226. ep0_readable (dev);
  1227. /* host may have given up waiting for response. we can miss control
  1228. * requests handled lower down (device/endpoint status and features);
  1229. * then ep0_{read,write} will report the wrong status. controller
  1230. * driver will have aborted pending i/o.
  1231. */
  1232. } else if (dev->state == STATE_DEV_SETUP)
  1233. dev->setup_abort = 1;
  1234. req->buf = dev->rbuf;
  1235. req->dma = DMA_ADDR_INVALID;
  1236. req->context = NULL;
  1237. value = -EOPNOTSUPP;
  1238. switch (ctrl->bRequest) {
  1239. case USB_REQ_GET_DESCRIPTOR:
  1240. if (ctrl->bRequestType != USB_DIR_IN)
  1241. goto unrecognized;
  1242. switch (w_value >> 8) {
  1243. case USB_DT_DEVICE:
  1244. value = min (w_length, (u16) sizeof *dev->dev);
  1245. dev->dev->bMaxPacketSize0 = dev->gadget->ep0->maxpacket;
  1246. req->buf = dev->dev;
  1247. break;
  1248. case USB_DT_DEVICE_QUALIFIER:
  1249. if (!dev->hs_config)
  1250. break;
  1251. value = min (w_length, (u16)
  1252. sizeof (struct usb_qualifier_descriptor));
  1253. make_qualifier (dev);
  1254. break;
  1255. case USB_DT_OTHER_SPEED_CONFIG:
  1256. // FALLTHROUGH
  1257. case USB_DT_CONFIG:
  1258. value = config_buf (dev,
  1259. w_value >> 8,
  1260. w_value & 0xff);
  1261. if (value >= 0)
  1262. value = min (w_length, (u16) value);
  1263. break;
  1264. case USB_DT_STRING:
  1265. goto unrecognized;
  1266. default: // all others are errors
  1267. break;
  1268. }
  1269. break;
  1270. /* currently one config, two speeds */
  1271. case USB_REQ_SET_CONFIGURATION:
  1272. if (ctrl->bRequestType != 0)
  1273. goto unrecognized;
  1274. if (0 == (u8) w_value) {
  1275. value = 0;
  1276. dev->current_config = 0;
  1277. usb_gadget_vbus_draw(gadget, 8 /* mA */ );
  1278. // user mode expected to disable endpoints
  1279. } else {
  1280. u8 config, power;
  1281. if (gadget_is_dualspeed(gadget)
  1282. && gadget->speed == USB_SPEED_HIGH) {
  1283. config = dev->hs_config->bConfigurationValue;
  1284. power = dev->hs_config->bMaxPower;
  1285. } else {
  1286. config = dev->config->bConfigurationValue;
  1287. power = dev->config->bMaxPower;
  1288. }
  1289. if (config == (u8) w_value) {
  1290. value = 0;
  1291. dev->current_config = config;
  1292. usb_gadget_vbus_draw(gadget, 2 * power);
  1293. }
  1294. }
  1295. /* report SET_CONFIGURATION like any other control request,
  1296. * except that usermode may not stall this. the next
  1297. * request mustn't be allowed start until this finishes:
  1298. * endpoints and threads set up, etc.
  1299. *
  1300. * NOTE: older PXA hardware (before PXA 255: without UDCCFR)
  1301. * has bad/racey automagic that prevents synchronizing here.
  1302. * even kernel mode drivers often miss them.
  1303. */
  1304. if (value == 0) {
  1305. INFO (dev, "configuration #%d\n", dev->current_config);
  1306. if (dev->usermode_setup) {
  1307. dev->setup_can_stall = 0;
  1308. goto delegate;
  1309. }
  1310. }
  1311. break;
  1312. #ifndef CONFIG_USB_PXA25X
  1313. /* PXA automagically handles this request too */
  1314. case USB_REQ_GET_CONFIGURATION:
  1315. if (ctrl->bRequestType != 0x80)
  1316. goto unrecognized;
  1317. *(u8 *)req->buf = dev->current_config;
  1318. value = min (w_length, (u16) 1);
  1319. break;
  1320. #endif
  1321. default:
  1322. unrecognized:
  1323. VDEBUG (dev, "%s req%02x.%02x v%04x i%04x l%d\n",
  1324. dev->usermode_setup ? "delegate" : "fail",
  1325. ctrl->bRequestType, ctrl->bRequest,
  1326. w_value, le16_to_cpu(ctrl->wIndex), w_length);
  1327. /* if there's an ep0 reader, don't stall */
  1328. if (dev->usermode_setup) {
  1329. dev->setup_can_stall = 1;
  1330. delegate:
  1331. dev->setup_in = (ctrl->bRequestType & USB_DIR_IN)
  1332. ? 1 : 0;
  1333. dev->setup_wLength = w_length;
  1334. dev->setup_out_ready = 0;
  1335. dev->setup_out_error = 0;
  1336. value = 0;
  1337. /* read DATA stage for OUT right away */
  1338. if (unlikely (!dev->setup_in && w_length)) {
  1339. value = setup_req (gadget->ep0, dev->req,
  1340. w_length);
  1341. if (value < 0)
  1342. break;
  1343. value = usb_ep_queue (gadget->ep0, dev->req,
  1344. GFP_ATOMIC);
  1345. if (value < 0) {
  1346. clean_req (gadget->ep0, dev->req);
  1347. break;
  1348. }
  1349. /* we can't currently stall these */
  1350. dev->setup_can_stall = 0;
  1351. }
  1352. /* state changes when reader collects event */
  1353. event = next_event (dev, GADGETFS_SETUP);
  1354. event->u.setup = *ctrl;
  1355. ep0_readable (dev);
  1356. spin_unlock (&dev->lock);
  1357. return 0;
  1358. }
  1359. }
  1360. /* proceed with data transfer and status phases? */
  1361. if (value >= 0 && dev->state != STATE_DEV_SETUP) {
  1362. req->length = value;
  1363. req->zero = value < w_length;
  1364. value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  1365. if (value < 0) {
  1366. DBG (dev, "ep_queue --> %d\n", value);
  1367. req->status = 0;
  1368. }
  1369. }
  1370. /* device stalls when value < 0 */
  1371. spin_unlock (&dev->lock);
  1372. return value;
  1373. }
  1374. static void destroy_ep_files (struct dev_data *dev)
  1375. {
  1376. DBG (dev, "%s %d\n", __func__, dev->state);
  1377. /* dev->state must prevent interference */
  1378. spin_lock_irq (&dev->lock);
  1379. while (!list_empty(&dev->epfiles)) {
  1380. struct ep_data *ep;
  1381. struct inode *parent;
  1382. struct dentry *dentry;
  1383. /* break link to FS */
  1384. ep = list_first_entry (&dev->epfiles, struct ep_data, epfiles);
  1385. list_del_init (&ep->epfiles);
  1386. dentry = ep->dentry;
  1387. ep->dentry = NULL;
  1388. parent = dentry->d_parent->d_inode;
  1389. /* break link to controller */
  1390. if (ep->state == STATE_EP_ENABLED)
  1391. (void) usb_ep_disable (ep->ep);
  1392. ep->state = STATE_EP_UNBOUND;
  1393. usb_ep_free_request (ep->ep, ep->req);
  1394. ep->ep = NULL;
  1395. wake_up (&ep->wait);
  1396. put_ep (ep);
  1397. spin_unlock_irq (&dev->lock);
  1398. /* break link to dcache */
  1399. mutex_lock (&parent->i_mutex);
  1400. d_delete (dentry);
  1401. dput (dentry);
  1402. mutex_unlock (&parent->i_mutex);
  1403. spin_lock_irq (&dev->lock);
  1404. }
  1405. spin_unlock_irq (&dev->lock);
  1406. }
  1407. static struct inode *
  1408. gadgetfs_create_file (struct super_block *sb, char const *name,
  1409. void *data, const struct file_operations *fops,
  1410. struct dentry **dentry_p);
  1411. static int activate_ep_files (struct dev_data *dev)
  1412. {
  1413. struct usb_ep *ep;
  1414. struct ep_data *data;
  1415. gadget_for_each_ep (ep, dev->gadget) {
  1416. data = kzalloc(sizeof(*data), GFP_KERNEL);
  1417. if (!data)
  1418. goto enomem0;
  1419. data->state = STATE_EP_DISABLED;
  1420. mutex_init(&data->lock);
  1421. init_waitqueue_head (&data->wait);
  1422. strncpy (data->name, ep->name, sizeof (data->name) - 1);
  1423. atomic_set (&data->count, 1);
  1424. data->dev = dev;
  1425. get_dev (dev);
  1426. data->ep = ep;
  1427. ep->driver_data = data;
  1428. data->req = usb_ep_alloc_request (ep, GFP_KERNEL);
  1429. if (!data->req)
  1430. goto enomem1;
  1431. data->inode = gadgetfs_create_file (dev->sb, data->name,
  1432. data, &ep_config_operations,
  1433. &data->dentry);
  1434. if (!data->inode)
  1435. goto enomem2;
  1436. list_add_tail (&data->epfiles, &dev->epfiles);
  1437. }
  1438. return 0;
  1439. enomem2:
  1440. usb_ep_free_request (ep, data->req);
  1441. enomem1:
  1442. put_dev (dev);
  1443. kfree (data);
  1444. enomem0:
  1445. DBG (dev, "%s enomem\n", __func__);
  1446. destroy_ep_files (dev);
  1447. return -ENOMEM;
  1448. }
  1449. static void
  1450. gadgetfs_unbind (struct usb_gadget *gadget)
  1451. {
  1452. struct dev_data *dev = get_gadget_data (gadget);
  1453. DBG (dev, "%s\n", __func__);
  1454. spin_lock_irq (&dev->lock);
  1455. dev->state = STATE_DEV_UNBOUND;
  1456. spin_unlock_irq (&dev->lock);
  1457. destroy_ep_files (dev);
  1458. gadget->ep0->driver_data = NULL;
  1459. set_gadget_data (gadget, NULL);
  1460. /* we've already been disconnected ... no i/o is active */
  1461. if (dev->req)
  1462. usb_ep_free_request (gadget->ep0, dev->req);
  1463. DBG (dev, "%s done\n", __func__);
  1464. put_dev (dev);
  1465. }
  1466. static struct dev_data *the_device;
  1467. static int
  1468. gadgetfs_bind (struct usb_gadget *gadget)
  1469. {
  1470. struct dev_data *dev = the_device;
  1471. if (!dev)
  1472. return -ESRCH;
  1473. if (0 != strcmp (CHIP, gadget->name)) {
  1474. pr_err("%s expected %s controller not %s\n",
  1475. shortname, CHIP, gadget->name);
  1476. return -ENODEV;
  1477. }
  1478. set_gadget_data (gadget, dev);
  1479. dev->gadget = gadget;
  1480. gadget->ep0->driver_data = dev;
  1481. /* preallocate control response and buffer */
  1482. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1483. if (!dev->req)
  1484. goto enomem;
  1485. dev->req->context = NULL;
  1486. dev->req->complete = epio_complete;
  1487. if (activate_ep_files (dev) < 0)
  1488. goto enomem;
  1489. INFO (dev, "bound to %s driver\n", gadget->name);
  1490. spin_lock_irq(&dev->lock);
  1491. dev->state = STATE_DEV_UNCONNECTED;
  1492. spin_unlock_irq(&dev->lock);
  1493. get_dev (dev);
  1494. return 0;
  1495. enomem:
  1496. gadgetfs_unbind (gadget);
  1497. return -ENOMEM;
  1498. }
  1499. static void
  1500. gadgetfs_disconnect (struct usb_gadget *gadget)
  1501. {
  1502. struct dev_data *dev = get_gadget_data (gadget);
  1503. unsigned long flags;
  1504. spin_lock_irqsave (&dev->lock, flags);
  1505. if (dev->state == STATE_DEV_UNCONNECTED)
  1506. goto exit;
  1507. dev->state = STATE_DEV_UNCONNECTED;
  1508. INFO (dev, "disconnected\n");
  1509. next_event (dev, GADGETFS_DISCONNECT);
  1510. ep0_readable (dev);
  1511. exit:
  1512. spin_unlock_irqrestore (&dev->lock, flags);
  1513. }
  1514. static void
  1515. gadgetfs_suspend (struct usb_gadget *gadget)
  1516. {
  1517. struct dev_data *dev = get_gadget_data (gadget);
  1518. INFO (dev, "suspended from state %d\n", dev->state);
  1519. spin_lock (&dev->lock);
  1520. switch (dev->state) {
  1521. case STATE_DEV_SETUP: // VERY odd... host died??
  1522. case STATE_DEV_CONNECTED:
  1523. case STATE_DEV_UNCONNECTED:
  1524. next_event (dev, GADGETFS_SUSPEND);
  1525. ep0_readable (dev);
  1526. /* FALLTHROUGH */
  1527. default:
  1528. break;
  1529. }
  1530. spin_unlock (&dev->lock);
  1531. }
  1532. static struct usb_gadget_driver gadgetfs_driver = {
  1533. .function = (char *) driver_desc,
  1534. .unbind = gadgetfs_unbind,
  1535. .setup = gadgetfs_setup,
  1536. .disconnect = gadgetfs_disconnect,
  1537. .suspend = gadgetfs_suspend,
  1538. .driver = {
  1539. .name = (char *) shortname,
  1540. },
  1541. };
  1542. /*----------------------------------------------------------------------*/
  1543. static void gadgetfs_nop(struct usb_gadget *arg) { }
  1544. static int gadgetfs_probe (struct usb_gadget *gadget)
  1545. {
  1546. CHIP = gadget->name;
  1547. return -EISNAM;
  1548. }
  1549. static struct usb_gadget_driver probe_driver = {
  1550. .max_speed = USB_SPEED_HIGH,
  1551. .unbind = gadgetfs_nop,
  1552. .setup = (void *)gadgetfs_nop,
  1553. .disconnect = gadgetfs_nop,
  1554. .driver = {
  1555. .name = "nop",
  1556. },
  1557. };
  1558. /* DEVICE INITIALIZATION
  1559. *
  1560. * fd = open ("/dev/gadget/$CHIP", O_RDWR)
  1561. * status = write (fd, descriptors, sizeof descriptors)
  1562. *
  1563. * That write establishes the device configuration, so the kernel can
  1564. * bind to the controller ... guaranteeing it can handle enumeration
  1565. * at all necessary speeds. Descriptor order is:
  1566. *
  1567. * . message tag (u32, host order) ... for now, must be zero; it
  1568. * would change to support features like multi-config devices
  1569. * . full/low speed config ... all wTotalLength bytes (with interface,
  1570. * class, altsetting, endpoint, and other descriptors)
  1571. * . high speed config ... all descriptors, for high speed operation;
  1572. * this one's optional except for high-speed hardware
  1573. * . device descriptor
  1574. *
  1575. * Endpoints are not yet enabled. Drivers must wait until device
  1576. * configuration and interface altsetting changes create
  1577. * the need to configure (or unconfigure) them.
  1578. *
  1579. * After initialization, the device stays active for as long as that
  1580. * $CHIP file is open. Events must then be read from that descriptor,
  1581. * such as configuration notifications.
  1582. */
  1583. static int is_valid_config (struct usb_config_descriptor *config)
  1584. {
  1585. return config->bDescriptorType == USB_DT_CONFIG
  1586. && config->bLength == USB_DT_CONFIG_SIZE
  1587. && config->bConfigurationValue != 0
  1588. && (config->bmAttributes & USB_CONFIG_ATT_ONE) != 0
  1589. && (config->bmAttributes & USB_CONFIG_ATT_WAKEUP) == 0;
  1590. /* FIXME if gadget->is_otg, _must_ include an otg descriptor */
  1591. /* FIXME check lengths: walk to end */
  1592. }
  1593. static ssize_t
  1594. dev_config (struct file *fd, const char __user *buf, size_t len, loff_t *ptr)
  1595. {
  1596. struct dev_data *dev = fd->private_data;
  1597. ssize_t value = len, length = len;
  1598. unsigned total;
  1599. u32 tag;
  1600. char *kbuf;
  1601. if (len < (USB_DT_CONFIG_SIZE + USB_DT_DEVICE_SIZE + 4))
  1602. return -EINVAL;
  1603. /* we might need to change message format someday */
  1604. if (copy_from_user (&tag, buf, 4))
  1605. return -EFAULT;
  1606. if (tag != 0)
  1607. return -EINVAL;
  1608. buf += 4;
  1609. length -= 4;
  1610. kbuf = memdup_user(buf, length);
  1611. if (IS_ERR(kbuf))
  1612. return PTR_ERR(kbuf);
  1613. spin_lock_irq (&dev->lock);
  1614. value = -EINVAL;
  1615. if (dev->buf) {
  1616. kfree(kbuf);
  1617. goto fail;
  1618. }
  1619. dev->buf = kbuf;
  1620. /* full or low speed config */
  1621. dev->config = (void *) kbuf;
  1622. total = le16_to_cpu(dev->config->wTotalLength);
  1623. if (!is_valid_config (dev->config) || total >= length)
  1624. goto fail;
  1625. kbuf += total;
  1626. length -= total;
  1627. /* optional high speed config */
  1628. if (kbuf [1] == USB_DT_CONFIG) {
  1629. dev->hs_config = (void *) kbuf;
  1630. total = le16_to_cpu(dev->hs_config->wTotalLength);
  1631. if (!is_valid_config (dev->hs_config) || total >= length)
  1632. goto fail;
  1633. kbuf += total;
  1634. length -= total;
  1635. }
  1636. /* could support multiple configs, using another encoding! */
  1637. /* device descriptor (tweaked for paranoia) */
  1638. if (length != USB_DT_DEVICE_SIZE)
  1639. goto fail;
  1640. dev->dev = (void *)kbuf;
  1641. if (dev->dev->bLength != USB_DT_DEVICE_SIZE
  1642. || dev->dev->bDescriptorType != USB_DT_DEVICE
  1643. || dev->dev->bNumConfigurations != 1)
  1644. goto fail;
  1645. dev->dev->bNumConfigurations = 1;
  1646. dev->dev->bcdUSB = cpu_to_le16 (0x0200);
  1647. /* triggers gadgetfs_bind(); then we can enumerate. */
  1648. spin_unlock_irq (&dev->lock);
  1649. if (dev->hs_config)
  1650. gadgetfs_driver.max_speed = USB_SPEED_HIGH;
  1651. else
  1652. gadgetfs_driver.max_speed = USB_SPEED_FULL;
  1653. value = usb_gadget_probe_driver(&gadgetfs_driver, gadgetfs_bind);
  1654. if (value != 0) {
  1655. kfree (dev->buf);
  1656. dev->buf = NULL;
  1657. } else {
  1658. /* at this point "good" hardware has for the first time
  1659. * let the USB the host see us. alternatively, if users
  1660. * unplug/replug that will clear all the error state.
  1661. *
  1662. * note: everything running before here was guaranteed
  1663. * to choke driver model style diagnostics. from here
  1664. * on, they can work ... except in cleanup paths that
  1665. * kick in after the ep0 descriptor is closed.
  1666. */
  1667. fd->f_op = &ep0_io_operations;
  1668. value = len;
  1669. }
  1670. return value;
  1671. fail:
  1672. spin_unlock_irq (&dev->lock);
  1673. pr_debug ("%s: %s fail %Zd, %pK\n", shortname, __func__, value, dev);
  1674. kfree (dev->buf);
  1675. dev->buf = NULL;
  1676. return value;
  1677. }
  1678. static int
  1679. dev_open (struct inode *inode, struct file *fd)
  1680. {
  1681. struct dev_data *dev = inode->i_private;
  1682. int value = -EBUSY;
  1683. spin_lock_irq(&dev->lock);
  1684. if (dev->state == STATE_DEV_DISABLED) {
  1685. dev->ev_next = 0;
  1686. dev->state = STATE_DEV_OPENED;
  1687. fd->private_data = dev;
  1688. get_dev (dev);
  1689. value = 0;
  1690. }
  1691. spin_unlock_irq(&dev->lock);
  1692. return value;
  1693. }
  1694. static const struct file_operations dev_init_operations = {
  1695. .llseek = no_llseek,
  1696. .open = dev_open,
  1697. .write = dev_config,
  1698. .fasync = ep0_fasync,
  1699. .unlocked_ioctl = dev_ioctl,
  1700. .release = dev_release,
  1701. };
  1702. /*----------------------------------------------------------------------*/
  1703. /* FILESYSTEM AND SUPERBLOCK OPERATIONS
  1704. *
  1705. * Mounting the filesystem creates a controller file, used first for
  1706. * device configuration then later for event monitoring.
  1707. */
  1708. /* FIXME PAM etc could set this security policy without mount options
  1709. * if epfiles inherited ownership and permissons from ep0 ...
  1710. */
  1711. static unsigned default_uid;
  1712. static unsigned default_gid;
  1713. static unsigned default_perm = S_IRUSR | S_IWUSR;
  1714. module_param (default_uid, uint, 0644);
  1715. module_param (default_gid, uint, 0644);
  1716. module_param (default_perm, uint, 0644);
  1717. static struct inode *
  1718. gadgetfs_make_inode (struct super_block *sb,
  1719. void *data, const struct file_operations *fops,
  1720. int mode)
  1721. {
  1722. struct inode *inode = new_inode (sb);
  1723. if (inode) {
  1724. inode->i_ino = get_next_ino();
  1725. inode->i_mode = mode;
  1726. inode->i_uid = default_uid;
  1727. inode->i_gid = default_gid;
  1728. inode->i_atime = inode->i_mtime = inode->i_ctime
  1729. = CURRENT_TIME;
  1730. inode->i_private = data;
  1731. inode->i_fop = fops;
  1732. }
  1733. return inode;
  1734. }
  1735. /* creates in fs root directory, so non-renamable and non-linkable.
  1736. * so inode and dentry are paired, until device reconfig.
  1737. */
  1738. static struct inode *
  1739. gadgetfs_create_file (struct super_block *sb, char const *name,
  1740. void *data, const struct file_operations *fops,
  1741. struct dentry **dentry_p)
  1742. {
  1743. struct dentry *dentry;
  1744. struct inode *inode;
  1745. dentry = d_alloc_name(sb->s_root, name);
  1746. if (!dentry)
  1747. return NULL;
  1748. inode = gadgetfs_make_inode (sb, data, fops,
  1749. S_IFREG | (default_perm & S_IRWXUGO));
  1750. if (!inode) {
  1751. dput(dentry);
  1752. return NULL;
  1753. }
  1754. d_add (dentry, inode);
  1755. *dentry_p = dentry;
  1756. return inode;
  1757. }
  1758. static const struct super_operations gadget_fs_operations = {
  1759. .statfs = simple_statfs,
  1760. .drop_inode = generic_delete_inode,
  1761. };
  1762. static int
  1763. gadgetfs_fill_super (struct super_block *sb, void *opts, int silent)
  1764. {
  1765. struct inode *inode;
  1766. struct dev_data *dev;
  1767. if (the_device)
  1768. return -ESRCH;
  1769. /* fake probe to determine $CHIP */
  1770. (void) usb_gadget_probe_driver(&probe_driver, gadgetfs_probe);
  1771. if (!CHIP)
  1772. return -ENODEV;
  1773. /* superblock */
  1774. sb->s_blocksize = PAGE_CACHE_SIZE;
  1775. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  1776. sb->s_magic = GADGETFS_MAGIC;
  1777. sb->s_op = &gadget_fs_operations;
  1778. sb->s_time_gran = 1;
  1779. /* root inode */
  1780. inode = gadgetfs_make_inode (sb,
  1781. NULL, &simple_dir_operations,
  1782. S_IFDIR | S_IRUGO | S_IXUGO);
  1783. if (!inode)
  1784. goto Enomem;
  1785. inode->i_op = &simple_dir_inode_operations;
  1786. if (!(sb->s_root = d_make_root (inode)))
  1787. goto Enomem;
  1788. /* the ep0 file is named after the controller we expect;
  1789. * user mode code can use it for sanity checks, like we do.
  1790. */
  1791. dev = dev_new ();
  1792. if (!dev)
  1793. goto Enomem;
  1794. dev->sb = sb;
  1795. if (!gadgetfs_create_file (sb, CHIP,
  1796. dev, &dev_init_operations,
  1797. &dev->dentry)) {
  1798. put_dev(dev);
  1799. goto Enomem;
  1800. }
  1801. /* other endpoint files are available after hardware setup,
  1802. * from binding to a controller.
  1803. */
  1804. the_device = dev;
  1805. return 0;
  1806. Enomem:
  1807. return -ENOMEM;
  1808. }
  1809. /* "mount -t gadgetfs path /dev/gadget" ends up here */
  1810. static struct dentry *
  1811. gadgetfs_mount (struct file_system_type *t, int flags,
  1812. const char *path, void *opts)
  1813. {
  1814. return mount_single (t, flags, opts, gadgetfs_fill_super);
  1815. }
  1816. static void
  1817. gadgetfs_kill_sb (struct super_block *sb)
  1818. {
  1819. kill_litter_super (sb);
  1820. if (the_device) {
  1821. put_dev (the_device);
  1822. the_device = NULL;
  1823. }
  1824. }
  1825. /*----------------------------------------------------------------------*/
  1826. static struct file_system_type gadgetfs_type = {
  1827. .owner = THIS_MODULE,
  1828. .name = shortname,
  1829. .mount = gadgetfs_mount,
  1830. .kill_sb = gadgetfs_kill_sb,
  1831. };
  1832. MODULE_ALIAS_FS("gadgetfs");
  1833. /*----------------------------------------------------------------------*/
  1834. static int __init init (void)
  1835. {
  1836. int status;
  1837. status = register_filesystem (&gadgetfs_type);
  1838. if (status == 0)
  1839. pr_info ("%s: %s, version " DRIVER_VERSION "\n",
  1840. shortname, driver_desc);
  1841. return status;
  1842. }
  1843. module_init (init);
  1844. static void __exit cleanup (void)
  1845. {
  1846. pr_debug ("unregister %s\n", shortname);
  1847. unregister_filesystem (&gadgetfs_type);
  1848. }
  1849. module_exit (cleanup);