inode.c 52 KB

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