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