inode.c 53 KB

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