inode.c 53 KB

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