inode.c 52 KB

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