inode.c 52 KB

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