inode.c 52 KB

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