inode.c 54 KB

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