inode.c 52 KB

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