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