zero.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331
  1. /*
  2. * zero.c -- Gadget Zero, for USB development
  3. *
  4. * Copyright (C) 2003-2004 David Brownell
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions, and the following disclaimer,
  12. * without modification.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 3. The names of the above-listed copyright holders may not be used
  17. * to endorse or promote products derived from this software without
  18. * specific prior written permission.
  19. *
  20. * ALTERNATIVELY, this software may be distributed under the terms of the
  21. * GNU General Public License ("GPL") as published by the Free Software
  22. * Foundation, either version 2 of that License or (at your option) any
  23. * later version.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  26. * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  27. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  28. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  29. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  30. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  31. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  32. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  34. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. /*
  38. * Gadget Zero only needs two bulk endpoints, and is an example of how you
  39. * can write a hardware-agnostic gadget driver running inside a USB device.
  40. *
  41. * Hardware details are visible (see CONFIG_USB_ZERO_* below) but don't
  42. * affect most of the driver.
  43. *
  44. * Use it with the Linux host/master side "usbtest" driver to get a basic
  45. * functional test of your device-side usb stack, or with "usb-skeleton".
  46. *
  47. * It supports two similar configurations. One sinks whatever the usb host
  48. * writes, and in return sources zeroes. The other loops whatever the host
  49. * writes back, so the host can read it. Module options include:
  50. *
  51. * buflen=N default N=4096, buffer size used
  52. * qlen=N default N=32, how many buffers in the loopback queue
  53. * loopdefault default false, list loopback config first
  54. *
  55. * Many drivers will only have one configuration, letting them be much
  56. * simpler if they also don't support high speed operation (like this
  57. * driver does).
  58. */
  59. #define DEBUG 1
  60. // #define VERBOSE
  61. #include <linux/config.h>
  62. #include <linux/module.h>
  63. #include <linux/kernel.h>
  64. #include <linux/delay.h>
  65. #include <linux/ioport.h>
  66. #include <linux/sched.h>
  67. #include <linux/slab.h>
  68. #include <linux/smp_lock.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/timer.h>
  72. #include <linux/list.h>
  73. #include <linux/interrupt.h>
  74. #include <linux/utsname.h>
  75. #include <linux/device.h>
  76. #include <linux/moduleparam.h>
  77. #include <asm/byteorder.h>
  78. #include <asm/io.h>
  79. #include <asm/irq.h>
  80. #include <asm/system.h>
  81. #include <asm/unaligned.h>
  82. #include <linux/usb_ch9.h>
  83. #include <linux/usb_gadget.h>
  84. #include "gadget_chips.h"
  85. /*-------------------------------------------------------------------------*/
  86. #define DRIVER_VERSION "St Patrick's Day 2004"
  87. static const char shortname [] = "zero";
  88. static const char longname [] = "Gadget Zero";
  89. static const char source_sink [] = "source and sink data";
  90. static const char loopback [] = "loop input to output";
  91. /*-------------------------------------------------------------------------*/
  92. /*
  93. * driver assumes self-powered hardware, and
  94. * has no way for users to trigger remote wakeup.
  95. *
  96. * this version autoconfigures as much as possible,
  97. * which is reasonable for most "bulk-only" drivers.
  98. */
  99. static const char *EP_IN_NAME; /* source */
  100. static const char *EP_OUT_NAME; /* sink */
  101. /*-------------------------------------------------------------------------*/
  102. /* big enough to hold our biggest descriptor */
  103. #define USB_BUFSIZ 256
  104. struct zero_dev {
  105. spinlock_t lock;
  106. struct usb_gadget *gadget;
  107. struct usb_request *req; /* for control responses */
  108. /* when configured, we have one of two configs:
  109. * - source data (in to host) and sink it (out from host)
  110. * - or loop it back (out from host back in to host)
  111. */
  112. u8 config;
  113. struct usb_ep *in_ep, *out_ep;
  114. /* autoresume timer */
  115. struct timer_list resume;
  116. };
  117. #define xprintk(d,level,fmt,args...) \
  118. dev_printk(level , &(d)->gadget->dev , fmt , ## args)
  119. #ifdef DEBUG
  120. #define DBG(dev,fmt,args...) \
  121. xprintk(dev , KERN_DEBUG , fmt , ## args)
  122. #else
  123. #define DBG(dev,fmt,args...) \
  124. do { } while (0)
  125. #endif /* DEBUG */
  126. #ifdef VERBOSE
  127. #define VDBG DBG
  128. #else
  129. #define VDBG(dev,fmt,args...) \
  130. do { } while (0)
  131. #endif /* VERBOSE */
  132. #define ERROR(dev,fmt,args...) \
  133. xprintk(dev , KERN_ERR , fmt , ## args)
  134. #define WARN(dev,fmt,args...) \
  135. xprintk(dev , KERN_WARNING , fmt , ## args)
  136. #define INFO(dev,fmt,args...) \
  137. xprintk(dev , KERN_INFO , fmt , ## args)
  138. /*-------------------------------------------------------------------------*/
  139. static unsigned buflen = 4096;
  140. static unsigned qlen = 32;
  141. static unsigned pattern = 0;
  142. module_param (buflen, uint, S_IRUGO|S_IWUSR);
  143. module_param (qlen, uint, S_IRUGO|S_IWUSR);
  144. module_param (pattern, uint, S_IRUGO|S_IWUSR);
  145. /*
  146. * if it's nonzero, autoresume says how many seconds to wait
  147. * before trying to wake up the host after suspend.
  148. */
  149. static unsigned autoresume = 0;
  150. module_param (autoresume, uint, 0);
  151. /*
  152. * Normally the "loopback" configuration is second (index 1) so
  153. * it's not the default. Here's where to change that order, to
  154. * work better with hosts where config changes are problematic.
  155. * Or controllers (like superh) that only support one config.
  156. */
  157. static int loopdefault = 0;
  158. module_param (loopdefault, bool, S_IRUGO|S_IWUSR);
  159. /*-------------------------------------------------------------------------*/
  160. /* Thanks to NetChip Technologies for donating this product ID.
  161. *
  162. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  163. * Instead: allocate your own, using normal USB-IF procedures.
  164. */
  165. #ifndef CONFIG_USB_ZERO_HNPTEST
  166. #define DRIVER_VENDOR_NUM 0x0525 /* NetChip */
  167. #define DRIVER_PRODUCT_NUM 0xa4a0 /* Linux-USB "Gadget Zero" */
  168. #else
  169. #define DRIVER_VENDOR_NUM 0x1a0a /* OTG test device IDs */
  170. #define DRIVER_PRODUCT_NUM 0xbadd
  171. #endif
  172. /*-------------------------------------------------------------------------*/
  173. /*
  174. * DESCRIPTORS ... most are static, but strings and (full)
  175. * configuration descriptors are built on demand.
  176. */
  177. #define STRING_MANUFACTURER 25
  178. #define STRING_PRODUCT 42
  179. #define STRING_SERIAL 101
  180. #define STRING_SOURCE_SINK 250
  181. #define STRING_LOOPBACK 251
  182. /*
  183. * This device advertises two configurations; these numbers work
  184. * on a pxa250 as well as more flexible hardware.
  185. */
  186. #define CONFIG_SOURCE_SINK 3
  187. #define CONFIG_LOOPBACK 2
  188. static struct usb_device_descriptor
  189. device_desc = {
  190. .bLength = sizeof device_desc,
  191. .bDescriptorType = USB_DT_DEVICE,
  192. .bcdUSB = __constant_cpu_to_le16 (0x0200),
  193. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  194. .idVendor = __constant_cpu_to_le16 (DRIVER_VENDOR_NUM),
  195. .idProduct = __constant_cpu_to_le16 (DRIVER_PRODUCT_NUM),
  196. .iManufacturer = STRING_MANUFACTURER,
  197. .iProduct = STRING_PRODUCT,
  198. .iSerialNumber = STRING_SERIAL,
  199. .bNumConfigurations = 2,
  200. };
  201. static struct usb_config_descriptor
  202. source_sink_config = {
  203. .bLength = sizeof source_sink_config,
  204. .bDescriptorType = USB_DT_CONFIG,
  205. /* compute wTotalLength on the fly */
  206. .bNumInterfaces = 1,
  207. .bConfigurationValue = CONFIG_SOURCE_SINK,
  208. .iConfiguration = STRING_SOURCE_SINK,
  209. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  210. .bMaxPower = 1, /* self-powered */
  211. };
  212. static struct usb_config_descriptor
  213. loopback_config = {
  214. .bLength = sizeof loopback_config,
  215. .bDescriptorType = USB_DT_CONFIG,
  216. /* compute wTotalLength on the fly */
  217. .bNumInterfaces = 1,
  218. .bConfigurationValue = CONFIG_LOOPBACK,
  219. .iConfiguration = STRING_LOOPBACK,
  220. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  221. .bMaxPower = 1, /* self-powered */
  222. };
  223. static struct usb_otg_descriptor
  224. otg_descriptor = {
  225. .bLength = sizeof otg_descriptor,
  226. .bDescriptorType = USB_DT_OTG,
  227. .bmAttributes = USB_OTG_SRP,
  228. };
  229. /* one interface in each configuration */
  230. static const struct usb_interface_descriptor
  231. source_sink_intf = {
  232. .bLength = sizeof source_sink_intf,
  233. .bDescriptorType = USB_DT_INTERFACE,
  234. .bNumEndpoints = 2,
  235. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  236. .iInterface = STRING_SOURCE_SINK,
  237. };
  238. static const struct usb_interface_descriptor
  239. loopback_intf = {
  240. .bLength = sizeof loopback_intf,
  241. .bDescriptorType = USB_DT_INTERFACE,
  242. .bNumEndpoints = 2,
  243. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  244. .iInterface = STRING_LOOPBACK,
  245. };
  246. /* two full speed bulk endpoints; their use is config-dependent */
  247. static struct usb_endpoint_descriptor
  248. fs_source_desc = {
  249. .bLength = USB_DT_ENDPOINT_SIZE,
  250. .bDescriptorType = USB_DT_ENDPOINT,
  251. .bEndpointAddress = USB_DIR_IN,
  252. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  253. };
  254. static struct usb_endpoint_descriptor
  255. fs_sink_desc = {
  256. .bLength = USB_DT_ENDPOINT_SIZE,
  257. .bDescriptorType = USB_DT_ENDPOINT,
  258. .bEndpointAddress = USB_DIR_OUT,
  259. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  260. };
  261. static const struct usb_descriptor_header *fs_source_sink_function [] = {
  262. (struct usb_descriptor_header *) &otg_descriptor,
  263. (struct usb_descriptor_header *) &source_sink_intf,
  264. (struct usb_descriptor_header *) &fs_sink_desc,
  265. (struct usb_descriptor_header *) &fs_source_desc,
  266. NULL,
  267. };
  268. static const struct usb_descriptor_header *fs_loopback_function [] = {
  269. (struct usb_descriptor_header *) &otg_descriptor,
  270. (struct usb_descriptor_header *) &loopback_intf,
  271. (struct usb_descriptor_header *) &fs_sink_desc,
  272. (struct usb_descriptor_header *) &fs_source_desc,
  273. NULL,
  274. };
  275. #ifdef CONFIG_USB_GADGET_DUALSPEED
  276. /*
  277. * usb 2.0 devices need to expose both high speed and full speed
  278. * descriptors, unless they only run at full speed.
  279. *
  280. * that means alternate endpoint descriptors (bigger packets)
  281. * and a "device qualifier" ... plus more construction options
  282. * for the config descriptor.
  283. */
  284. static struct usb_endpoint_descriptor
  285. hs_source_desc = {
  286. .bLength = USB_DT_ENDPOINT_SIZE,
  287. .bDescriptorType = USB_DT_ENDPOINT,
  288. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  289. .wMaxPacketSize = __constant_cpu_to_le16 (512),
  290. };
  291. static struct usb_endpoint_descriptor
  292. hs_sink_desc = {
  293. .bLength = USB_DT_ENDPOINT_SIZE,
  294. .bDescriptorType = USB_DT_ENDPOINT,
  295. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  296. .wMaxPacketSize = __constant_cpu_to_le16 (512),
  297. };
  298. static struct usb_qualifier_descriptor
  299. dev_qualifier = {
  300. .bLength = sizeof dev_qualifier,
  301. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  302. .bcdUSB = __constant_cpu_to_le16 (0x0200),
  303. .bDeviceClass = USB_CLASS_VENDOR_SPEC,
  304. .bNumConfigurations = 2,
  305. };
  306. static const struct usb_descriptor_header *hs_source_sink_function [] = {
  307. (struct usb_descriptor_header *) &otg_descriptor,
  308. (struct usb_descriptor_header *) &source_sink_intf,
  309. (struct usb_descriptor_header *) &hs_source_desc,
  310. (struct usb_descriptor_header *) &hs_sink_desc,
  311. NULL,
  312. };
  313. static const struct usb_descriptor_header *hs_loopback_function [] = {
  314. (struct usb_descriptor_header *) &otg_descriptor,
  315. (struct usb_descriptor_header *) &loopback_intf,
  316. (struct usb_descriptor_header *) &hs_source_desc,
  317. (struct usb_descriptor_header *) &hs_sink_desc,
  318. NULL,
  319. };
  320. /* maxpacket and other transfer characteristics vary by speed. */
  321. #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  322. #else
  323. /* if there's no high speed support, maxpacket doesn't change. */
  324. #define ep_desc(g,hs,fs) fs
  325. #endif /* !CONFIG_USB_GADGET_DUALSPEED */
  326. static char manufacturer [50];
  327. static char serial [40];
  328. /* static strings, in UTF-8 */
  329. static struct usb_string strings [] = {
  330. { STRING_MANUFACTURER, manufacturer, },
  331. { STRING_PRODUCT, longname, },
  332. { STRING_SERIAL, serial, },
  333. { STRING_LOOPBACK, loopback, },
  334. { STRING_SOURCE_SINK, source_sink, },
  335. { } /* end of list */
  336. };
  337. static struct usb_gadget_strings stringtab = {
  338. .language = 0x0409, /* en-us */
  339. .strings = strings,
  340. };
  341. /*
  342. * config descriptors are also handcrafted. these must agree with code
  343. * that sets configurations, and with code managing interfaces and their
  344. * altsettings. other complexity may come from:
  345. *
  346. * - high speed support, including "other speed config" rules
  347. * - multiple configurations
  348. * - interfaces with alternate settings
  349. * - embedded class or vendor-specific descriptors
  350. *
  351. * this handles high speed, and has a second config that could as easily
  352. * have been an alternate interface setting (on most hardware).
  353. *
  354. * NOTE: to demonstrate (and test) more USB capabilities, this driver
  355. * should include an altsetting to test interrupt transfers, including
  356. * high bandwidth modes at high speed. (Maybe work like Intel's test
  357. * device?)
  358. */
  359. static int
  360. config_buf (struct usb_gadget *gadget,
  361. u8 *buf, u8 type, unsigned index)
  362. {
  363. int is_source_sink;
  364. int len;
  365. const struct usb_descriptor_header **function;
  366. #ifdef CONFIG_USB_GADGET_DUALSPEED
  367. int hs = (gadget->speed == USB_SPEED_HIGH);
  368. #endif
  369. /* two configurations will always be index 0 and index 1 */
  370. if (index > 1)
  371. return -EINVAL;
  372. is_source_sink = loopdefault ? (index == 1) : (index == 0);
  373. #ifdef CONFIG_USB_GADGET_DUALSPEED
  374. if (type == USB_DT_OTHER_SPEED_CONFIG)
  375. hs = !hs;
  376. if (hs)
  377. function = is_source_sink
  378. ? hs_source_sink_function
  379. : hs_loopback_function;
  380. else
  381. #endif
  382. function = is_source_sink
  383. ? fs_source_sink_function
  384. : fs_loopback_function;
  385. /* for now, don't advertise srp-only devices */
  386. if (!gadget->is_otg)
  387. function++;
  388. len = usb_gadget_config_buf (is_source_sink
  389. ? &source_sink_config
  390. : &loopback_config,
  391. buf, USB_BUFSIZ, function);
  392. if (len < 0)
  393. return len;
  394. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  395. return len;
  396. }
  397. /*-------------------------------------------------------------------------*/
  398. static struct usb_request *
  399. alloc_ep_req (struct usb_ep *ep, unsigned length)
  400. {
  401. struct usb_request *req;
  402. req = usb_ep_alloc_request (ep, GFP_ATOMIC);
  403. if (req) {
  404. req->length = length;
  405. req->buf = usb_ep_alloc_buffer (ep, length,
  406. &req->dma, GFP_ATOMIC);
  407. if (!req->buf) {
  408. usb_ep_free_request (ep, req);
  409. req = NULL;
  410. }
  411. }
  412. return req;
  413. }
  414. static void free_ep_req (struct usb_ep *ep, struct usb_request *req)
  415. {
  416. if (req->buf)
  417. usb_ep_free_buffer (ep, req->buf, req->dma, req->length);
  418. usb_ep_free_request (ep, req);
  419. }
  420. /*-------------------------------------------------------------------------*/
  421. /* optionally require specific source/sink data patterns */
  422. static int
  423. check_read_data (
  424. struct zero_dev *dev,
  425. struct usb_ep *ep,
  426. struct usb_request *req
  427. )
  428. {
  429. unsigned i;
  430. u8 *buf = req->buf;
  431. for (i = 0; i < req->actual; i++, buf++) {
  432. switch (pattern) {
  433. /* all-zeroes has no synchronization issues */
  434. case 0:
  435. if (*buf == 0)
  436. continue;
  437. break;
  438. /* mod63 stays in sync with short-terminated transfers,
  439. * or otherwise when host and gadget agree on how large
  440. * each usb transfer request should be. resync is done
  441. * with set_interface or set_config.
  442. */
  443. case 1:
  444. if (*buf == (u8)(i % 63))
  445. continue;
  446. break;
  447. }
  448. ERROR (dev, "bad OUT byte, buf [%d] = %d\n", i, *buf);
  449. usb_ep_set_halt (ep);
  450. return -EINVAL;
  451. }
  452. return 0;
  453. }
  454. static void
  455. reinit_write_data (
  456. struct zero_dev *dev,
  457. struct usb_ep *ep,
  458. struct usb_request *req
  459. )
  460. {
  461. unsigned i;
  462. u8 *buf = req->buf;
  463. switch (pattern) {
  464. case 0:
  465. memset (req->buf, 0, req->length);
  466. break;
  467. case 1:
  468. for (i = 0; i < req->length; i++)
  469. *buf++ = (u8) (i % 63);
  470. break;
  471. }
  472. }
  473. /* if there is only one request in the queue, there'll always be an
  474. * irq delay between end of one request and start of the next.
  475. * that prevents using hardware dma queues.
  476. */
  477. static void source_sink_complete (struct usb_ep *ep, struct usb_request *req)
  478. {
  479. struct zero_dev *dev = ep->driver_data;
  480. int status = req->status;
  481. switch (status) {
  482. case 0: /* normal completion? */
  483. if (ep == dev->out_ep)
  484. check_read_data (dev, ep, req);
  485. else
  486. reinit_write_data (dev, ep, req);
  487. break;
  488. /* this endpoint is normally active while we're configured */
  489. case -ECONNABORTED: /* hardware forced ep reset */
  490. case -ECONNRESET: /* request dequeued */
  491. case -ESHUTDOWN: /* disconnect from host */
  492. VDBG (dev, "%s gone (%d), %d/%d\n", ep->name, status,
  493. req->actual, req->length);
  494. if (ep == dev->out_ep)
  495. check_read_data (dev, ep, req);
  496. free_ep_req (ep, req);
  497. return;
  498. case -EOVERFLOW: /* buffer overrun on read means that
  499. * we didn't provide a big enough
  500. * buffer.
  501. */
  502. default:
  503. #if 1
  504. DBG (dev, "%s complete --> %d, %d/%d\n", ep->name,
  505. status, req->actual, req->length);
  506. #endif
  507. case -EREMOTEIO: /* short read */
  508. break;
  509. }
  510. status = usb_ep_queue (ep, req, GFP_ATOMIC);
  511. if (status) {
  512. ERROR (dev, "kill %s: resubmit %d bytes --> %d\n",
  513. ep->name, req->length, status);
  514. usb_ep_set_halt (ep);
  515. /* FIXME recover later ... somehow */
  516. }
  517. }
  518. static struct usb_request *
  519. source_sink_start_ep (struct usb_ep *ep, unsigned gfp_flags)
  520. {
  521. struct usb_request *req;
  522. int status;
  523. req = alloc_ep_req (ep, buflen);
  524. if (!req)
  525. return NULL;
  526. memset (req->buf, 0, req->length);
  527. req->complete = source_sink_complete;
  528. if (strcmp (ep->name, EP_IN_NAME) == 0)
  529. reinit_write_data (ep->driver_data, ep, req);
  530. status = usb_ep_queue (ep, req, gfp_flags);
  531. if (status) {
  532. struct zero_dev *dev = ep->driver_data;
  533. ERROR (dev, "start %s --> %d\n", ep->name, status);
  534. free_ep_req (ep, req);
  535. req = NULL;
  536. }
  537. return req;
  538. }
  539. static int
  540. set_source_sink_config (struct zero_dev *dev, unsigned gfp_flags)
  541. {
  542. int result = 0;
  543. struct usb_ep *ep;
  544. struct usb_gadget *gadget = dev->gadget;
  545. gadget_for_each_ep (ep, gadget) {
  546. const struct usb_endpoint_descriptor *d;
  547. /* one endpoint writes (sources) zeroes in (to the host) */
  548. if (strcmp (ep->name, EP_IN_NAME) == 0) {
  549. d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
  550. result = usb_ep_enable (ep, d);
  551. if (result == 0) {
  552. ep->driver_data = dev;
  553. if (source_sink_start_ep (ep, gfp_flags) != 0) {
  554. dev->in_ep = ep;
  555. continue;
  556. }
  557. usb_ep_disable (ep);
  558. result = -EIO;
  559. }
  560. /* one endpoint reads (sinks) anything out (from the host) */
  561. } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
  562. d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
  563. result = usb_ep_enable (ep, d);
  564. if (result == 0) {
  565. ep->driver_data = dev;
  566. if (source_sink_start_ep (ep, gfp_flags) != 0) {
  567. dev->out_ep = ep;
  568. continue;
  569. }
  570. usb_ep_disable (ep);
  571. result = -EIO;
  572. }
  573. /* ignore any other endpoints */
  574. } else
  575. continue;
  576. /* stop on error */
  577. ERROR (dev, "can't start %s, result %d\n", ep->name, result);
  578. break;
  579. }
  580. if (result == 0)
  581. DBG (dev, "buflen %d\n", buflen);
  582. /* caller is responsible for cleanup on error */
  583. return result;
  584. }
  585. /*-------------------------------------------------------------------------*/
  586. static void loopback_complete (struct usb_ep *ep, struct usb_request *req)
  587. {
  588. struct zero_dev *dev = ep->driver_data;
  589. int status = req->status;
  590. switch (status) {
  591. case 0: /* normal completion? */
  592. if (ep == dev->out_ep) {
  593. /* loop this OUT packet back IN to the host */
  594. req->zero = (req->actual < req->length);
  595. req->length = req->actual;
  596. status = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
  597. if (status == 0)
  598. return;
  599. /* "should never get here" */
  600. ERROR (dev, "can't loop %s to %s: %d\n",
  601. ep->name, dev->in_ep->name,
  602. status);
  603. }
  604. /* queue the buffer for some later OUT packet */
  605. req->length = buflen;
  606. status = usb_ep_queue (dev->out_ep, req, GFP_ATOMIC);
  607. if (status == 0)
  608. return;
  609. /* "should never get here" */
  610. /* FALLTHROUGH */
  611. default:
  612. ERROR (dev, "%s loop complete --> %d, %d/%d\n", ep->name,
  613. status, req->actual, req->length);
  614. /* FALLTHROUGH */
  615. /* NOTE: since this driver doesn't maintain an explicit record
  616. * of requests it submitted (just maintains qlen count), we
  617. * rely on the hardware driver to clean up on disconnect or
  618. * endpoint disable.
  619. */
  620. case -ECONNABORTED: /* hardware forced ep reset */
  621. case -ECONNRESET: /* request dequeued */
  622. case -ESHUTDOWN: /* disconnect from host */
  623. free_ep_req (ep, req);
  624. return;
  625. }
  626. }
  627. static int
  628. set_loopback_config (struct zero_dev *dev, unsigned gfp_flags)
  629. {
  630. int result = 0;
  631. struct usb_ep *ep;
  632. struct usb_gadget *gadget = dev->gadget;
  633. gadget_for_each_ep (ep, gadget) {
  634. const struct usb_endpoint_descriptor *d;
  635. /* one endpoint writes data back IN to the host */
  636. if (strcmp (ep->name, EP_IN_NAME) == 0) {
  637. d = ep_desc (gadget, &hs_source_desc, &fs_source_desc);
  638. result = usb_ep_enable (ep, d);
  639. if (result == 0) {
  640. ep->driver_data = dev;
  641. dev->in_ep = ep;
  642. continue;
  643. }
  644. /* one endpoint just reads OUT packets */
  645. } else if (strcmp (ep->name, EP_OUT_NAME) == 0) {
  646. d = ep_desc (gadget, &hs_sink_desc, &fs_sink_desc);
  647. result = usb_ep_enable (ep, d);
  648. if (result == 0) {
  649. ep->driver_data = dev;
  650. dev->out_ep = ep;
  651. continue;
  652. }
  653. /* ignore any other endpoints */
  654. } else
  655. continue;
  656. /* stop on error */
  657. ERROR (dev, "can't enable %s, result %d\n", ep->name, result);
  658. break;
  659. }
  660. /* allocate a bunch of read buffers and queue them all at once.
  661. * we buffer at most 'qlen' transfers; fewer if any need more
  662. * than 'buflen' bytes each.
  663. */
  664. if (result == 0) {
  665. struct usb_request *req;
  666. unsigned i;
  667. ep = dev->out_ep;
  668. for (i = 0; i < qlen && result == 0; i++) {
  669. req = alloc_ep_req (ep, buflen);
  670. if (req) {
  671. req->complete = loopback_complete;
  672. result = usb_ep_queue (ep, req, GFP_ATOMIC);
  673. if (result)
  674. DBG (dev, "%s queue req --> %d\n",
  675. ep->name, result);
  676. } else
  677. result = -ENOMEM;
  678. }
  679. }
  680. if (result == 0)
  681. DBG (dev, "qlen %d, buflen %d\n", qlen, buflen);
  682. /* caller is responsible for cleanup on error */
  683. return result;
  684. }
  685. /*-------------------------------------------------------------------------*/
  686. static void zero_reset_config (struct zero_dev *dev)
  687. {
  688. if (dev->config == 0)
  689. return;
  690. DBG (dev, "reset config\n");
  691. /* just disable endpoints, forcing completion of pending i/o.
  692. * all our completion handlers free their requests in this case.
  693. */
  694. if (dev->in_ep) {
  695. usb_ep_disable (dev->in_ep);
  696. dev->in_ep = NULL;
  697. }
  698. if (dev->out_ep) {
  699. usb_ep_disable (dev->out_ep);
  700. dev->out_ep = NULL;
  701. }
  702. dev->config = 0;
  703. del_timer (&dev->resume);
  704. }
  705. /* change our operational config. this code must agree with the code
  706. * that returns config descriptors, and altsetting code.
  707. *
  708. * it's also responsible for power management interactions. some
  709. * configurations might not work with our current power sources.
  710. *
  711. * note that some device controller hardware will constrain what this
  712. * code can do, perhaps by disallowing more than one configuration or
  713. * by limiting configuration choices (like the pxa2xx).
  714. */
  715. static int
  716. zero_set_config (struct zero_dev *dev, unsigned number, unsigned gfp_flags)
  717. {
  718. int result = 0;
  719. struct usb_gadget *gadget = dev->gadget;
  720. if (number == dev->config)
  721. return 0;
  722. if (gadget_is_sa1100 (gadget) && dev->config) {
  723. /* tx fifo is full, but we can't clear it...*/
  724. INFO (dev, "can't change configurations\n");
  725. return -ESPIPE;
  726. }
  727. zero_reset_config (dev);
  728. switch (number) {
  729. case CONFIG_SOURCE_SINK:
  730. result = set_source_sink_config (dev, gfp_flags);
  731. break;
  732. case CONFIG_LOOPBACK:
  733. result = set_loopback_config (dev, gfp_flags);
  734. break;
  735. default:
  736. result = -EINVAL;
  737. /* FALL THROUGH */
  738. case 0:
  739. return result;
  740. }
  741. if (!result && (!dev->in_ep || !dev->out_ep))
  742. result = -ENODEV;
  743. if (result)
  744. zero_reset_config (dev);
  745. else {
  746. char *speed;
  747. switch (gadget->speed) {
  748. case USB_SPEED_LOW: speed = "low"; break;
  749. case USB_SPEED_FULL: speed = "full"; break;
  750. case USB_SPEED_HIGH: speed = "high"; break;
  751. default: speed = "?"; break;
  752. }
  753. dev->config = number;
  754. INFO (dev, "%s speed config #%d: %s\n", speed, number,
  755. (number == CONFIG_SOURCE_SINK)
  756. ? source_sink : loopback);
  757. }
  758. return result;
  759. }
  760. /*-------------------------------------------------------------------------*/
  761. static void zero_setup_complete (struct usb_ep *ep, struct usb_request *req)
  762. {
  763. if (req->status || req->actual != req->length)
  764. DBG ((struct zero_dev *) ep->driver_data,
  765. "setup complete --> %d, %d/%d\n",
  766. req->status, req->actual, req->length);
  767. }
  768. /*
  769. * The setup() callback implements all the ep0 functionality that's
  770. * not handled lower down, in hardware or the hardware driver (like
  771. * device and endpoint feature flags, and their status). It's all
  772. * housekeeping for the gadget function we're implementing. Most of
  773. * the work is in config-specific setup.
  774. */
  775. static int
  776. zero_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  777. {
  778. struct zero_dev *dev = get_gadget_data (gadget);
  779. struct usb_request *req = dev->req;
  780. int value = -EOPNOTSUPP;
  781. u16 w_index = le16_to_cpu(ctrl->wIndex);
  782. u16 w_value = le16_to_cpu(ctrl->wValue);
  783. u16 w_length = le16_to_cpu(ctrl->wLength);
  784. /* usually this stores reply data in the pre-allocated ep0 buffer,
  785. * but config change events will reconfigure hardware.
  786. */
  787. req->zero = 0;
  788. switch (ctrl->bRequest) {
  789. case USB_REQ_GET_DESCRIPTOR:
  790. if (ctrl->bRequestType != USB_DIR_IN)
  791. goto unknown;
  792. switch (w_value >> 8) {
  793. case USB_DT_DEVICE:
  794. value = min (w_length, (u16) sizeof device_desc);
  795. memcpy (req->buf, &device_desc, value);
  796. break;
  797. #ifdef CONFIG_USB_GADGET_DUALSPEED
  798. case USB_DT_DEVICE_QUALIFIER:
  799. if (!gadget->is_dualspeed)
  800. break;
  801. value = min (w_length, (u16) sizeof dev_qualifier);
  802. memcpy (req->buf, &dev_qualifier, value);
  803. break;
  804. case USB_DT_OTHER_SPEED_CONFIG:
  805. if (!gadget->is_dualspeed)
  806. break;
  807. // FALLTHROUGH
  808. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  809. case USB_DT_CONFIG:
  810. value = config_buf (gadget, req->buf,
  811. w_value >> 8,
  812. w_value & 0xff);
  813. if (value >= 0)
  814. value = min (w_length, (u16) value);
  815. break;
  816. case USB_DT_STRING:
  817. /* wIndex == language code.
  818. * this driver only handles one language, you can
  819. * add string tables for other languages, using
  820. * any UTF-8 characters
  821. */
  822. value = usb_gadget_get_string (&stringtab,
  823. w_value & 0xff, req->buf);
  824. if (value >= 0)
  825. value = min (w_length, (u16) value);
  826. break;
  827. }
  828. break;
  829. /* currently two configs, two speeds */
  830. case USB_REQ_SET_CONFIGURATION:
  831. if (ctrl->bRequestType != 0)
  832. goto unknown;
  833. if (gadget->a_hnp_support)
  834. DBG (dev, "HNP available\n");
  835. else if (gadget->a_alt_hnp_support)
  836. DBG (dev, "HNP needs a different root port\n");
  837. else
  838. VDBG (dev, "HNP inactive\n");
  839. spin_lock (&dev->lock);
  840. value = zero_set_config (dev, w_value, GFP_ATOMIC);
  841. spin_unlock (&dev->lock);
  842. break;
  843. case USB_REQ_GET_CONFIGURATION:
  844. if (ctrl->bRequestType != USB_DIR_IN)
  845. goto unknown;
  846. *(u8 *)req->buf = dev->config;
  847. value = min (w_length, (u16) 1);
  848. break;
  849. /* until we add altsetting support, or other interfaces,
  850. * only 0/0 are possible. pxa2xx only supports 0/0 (poorly)
  851. * and already killed pending endpoint I/O.
  852. */
  853. case USB_REQ_SET_INTERFACE:
  854. if (ctrl->bRequestType != USB_RECIP_INTERFACE)
  855. goto unknown;
  856. spin_lock (&dev->lock);
  857. if (dev->config && w_index == 0 && w_value == 0) {
  858. u8 config = dev->config;
  859. /* resets interface configuration, forgets about
  860. * previous transaction state (queued bufs, etc)
  861. * and re-inits endpoint state (toggle etc)
  862. * no response queued, just zero status == success.
  863. * if we had more than one interface we couldn't
  864. * use this "reset the config" shortcut.
  865. */
  866. zero_reset_config (dev);
  867. zero_set_config (dev, config, GFP_ATOMIC);
  868. value = 0;
  869. }
  870. spin_unlock (&dev->lock);
  871. break;
  872. case USB_REQ_GET_INTERFACE:
  873. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE))
  874. goto unknown;
  875. if (!dev->config)
  876. break;
  877. if (w_index != 0) {
  878. value = -EDOM;
  879. break;
  880. }
  881. *(u8 *)req->buf = 0;
  882. value = min (w_length, (u16) 1);
  883. break;
  884. /*
  885. * These are the same vendor-specific requests supported by
  886. * Intel's USB 2.0 compliance test devices. We exceed that
  887. * device spec by allowing multiple-packet requests.
  888. */
  889. case 0x5b: /* control WRITE test -- fill the buffer */
  890. if (ctrl->bRequestType != (USB_DIR_OUT|USB_TYPE_VENDOR))
  891. goto unknown;
  892. if (w_value || w_index)
  893. break;
  894. /* just read that many bytes into the buffer */
  895. if (w_length > USB_BUFSIZ)
  896. break;
  897. value = w_length;
  898. break;
  899. case 0x5c: /* control READ test -- return the buffer */
  900. if (ctrl->bRequestType != (USB_DIR_IN|USB_TYPE_VENDOR))
  901. goto unknown;
  902. if (w_value || w_index)
  903. break;
  904. /* expect those bytes are still in the buffer; send back */
  905. if (w_length > USB_BUFSIZ
  906. || w_length != req->length)
  907. break;
  908. value = w_length;
  909. break;
  910. default:
  911. unknown:
  912. VDBG (dev,
  913. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  914. ctrl->bRequestType, ctrl->bRequest,
  915. w_value, w_index, w_length);
  916. }
  917. /* respond with data transfer before status phase? */
  918. if (value >= 0) {
  919. req->length = value;
  920. req->zero = value < w_length;
  921. value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  922. if (value < 0) {
  923. DBG (dev, "ep_queue --> %d\n", value);
  924. req->status = 0;
  925. zero_setup_complete (gadget->ep0, req);
  926. }
  927. }
  928. /* device either stalls (value < 0) or reports success */
  929. return value;
  930. }
  931. static void
  932. zero_disconnect (struct usb_gadget *gadget)
  933. {
  934. struct zero_dev *dev = get_gadget_data (gadget);
  935. unsigned long flags;
  936. spin_lock_irqsave (&dev->lock, flags);
  937. zero_reset_config (dev);
  938. /* a more significant application might have some non-usb
  939. * activities to quiesce here, saving resources like power
  940. * or pushing the notification up a network stack.
  941. */
  942. spin_unlock_irqrestore (&dev->lock, flags);
  943. /* next we may get setup() calls to enumerate new connections;
  944. * or an unbind() during shutdown (including removing module).
  945. */
  946. }
  947. static void
  948. zero_autoresume (unsigned long _dev)
  949. {
  950. struct zero_dev *dev = (struct zero_dev *) _dev;
  951. int status;
  952. /* normally the host would be woken up for something
  953. * more significant than just a timer firing...
  954. */
  955. if (dev->gadget->speed != USB_SPEED_UNKNOWN) {
  956. status = usb_gadget_wakeup (dev->gadget);
  957. DBG (dev, "wakeup --> %d\n", status);
  958. }
  959. }
  960. /*-------------------------------------------------------------------------*/
  961. static void
  962. zero_unbind (struct usb_gadget *gadget)
  963. {
  964. struct zero_dev *dev = get_gadget_data (gadget);
  965. DBG (dev, "unbind\n");
  966. /* we've already been disconnected ... no i/o is active */
  967. if (dev->req)
  968. free_ep_req (gadget->ep0, dev->req);
  969. del_timer_sync (&dev->resume);
  970. kfree (dev);
  971. set_gadget_data (gadget, NULL);
  972. }
  973. static int
  974. zero_bind (struct usb_gadget *gadget)
  975. {
  976. struct zero_dev *dev;
  977. struct usb_ep *ep;
  978. int gcnum;
  979. /* FIXME this can't yet work right with SH ... it has only
  980. * one configuration, numbered one.
  981. */
  982. if (gadget_is_sh(gadget))
  983. return -ENODEV;
  984. /* Bulk-only drivers like this one SHOULD be able to
  985. * autoconfigure on any sane usb controller driver,
  986. * but there may also be important quirks to address.
  987. */
  988. usb_ep_autoconfig_reset (gadget);
  989. ep = usb_ep_autoconfig (gadget, &fs_source_desc);
  990. if (!ep) {
  991. autoconf_fail:
  992. printk (KERN_ERR "%s: can't autoconfigure on %s\n",
  993. shortname, gadget->name);
  994. return -ENODEV;
  995. }
  996. EP_IN_NAME = ep->name;
  997. ep->driver_data = ep; /* claim */
  998. ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
  999. if (!ep)
  1000. goto autoconf_fail;
  1001. EP_OUT_NAME = ep->name;
  1002. ep->driver_data = ep; /* claim */
  1003. gcnum = usb_gadget_controller_number (gadget);
  1004. if (gcnum >= 0)
  1005. device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
  1006. else {
  1007. /* gadget zero is so simple (for now, no altsettings) that
  1008. * it SHOULD NOT have problems with bulk-capable hardware.
  1009. * so warn about unrcognized controllers, don't panic.
  1010. *
  1011. * things like configuration and altsetting numbering
  1012. * can need hardware-specific attention though.
  1013. */
  1014. printk (KERN_WARNING "%s: controller '%s' not recognized\n",
  1015. shortname, gadget->name);
  1016. device_desc.bcdDevice = __constant_cpu_to_le16 (0x9999);
  1017. }
  1018. /* ok, we made sense of the hardware ... */
  1019. dev = kmalloc (sizeof *dev, SLAB_KERNEL);
  1020. if (!dev)
  1021. return -ENOMEM;
  1022. memset (dev, 0, sizeof *dev);
  1023. spin_lock_init (&dev->lock);
  1024. dev->gadget = gadget;
  1025. set_gadget_data (gadget, dev);
  1026. /* preallocate control response and buffer */
  1027. dev->req = usb_ep_alloc_request (gadget->ep0, GFP_KERNEL);
  1028. if (!dev->req)
  1029. goto enomem;
  1030. dev->req->buf = usb_ep_alloc_buffer (gadget->ep0, USB_BUFSIZ,
  1031. &dev->req->dma, GFP_KERNEL);
  1032. if (!dev->req->buf)
  1033. goto enomem;
  1034. dev->req->complete = zero_setup_complete;
  1035. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1036. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1037. /* assume ep0 uses the same value for both speeds ... */
  1038. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1039. /* and that all endpoints are dual-speed */
  1040. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  1041. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  1042. #endif
  1043. if (gadget->is_otg) {
  1044. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1045. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1046. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1047. }
  1048. if (gadget->is_otg) {
  1049. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1050. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1051. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1052. }
  1053. usb_gadget_set_selfpowered (gadget);
  1054. init_timer (&dev->resume);
  1055. dev->resume.function = zero_autoresume;
  1056. dev->resume.data = (unsigned long) dev;
  1057. if (autoresume) {
  1058. source_sink_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1059. loopback_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1060. }
  1061. gadget->ep0->driver_data = dev;
  1062. INFO (dev, "%s, version: " DRIVER_VERSION "\n", longname);
  1063. INFO (dev, "using %s, OUT %s IN %s\n", gadget->name,
  1064. EP_OUT_NAME, EP_IN_NAME);
  1065. snprintf (manufacturer, sizeof manufacturer, "%s %s with %s",
  1066. system_utsname.sysname, system_utsname.release,
  1067. gadget->name);
  1068. return 0;
  1069. enomem:
  1070. zero_unbind (gadget);
  1071. return -ENOMEM;
  1072. }
  1073. /*-------------------------------------------------------------------------*/
  1074. static void
  1075. zero_suspend (struct usb_gadget *gadget)
  1076. {
  1077. struct zero_dev *dev = get_gadget_data (gadget);
  1078. if (gadget->speed == USB_SPEED_UNKNOWN)
  1079. return;
  1080. if (autoresume) {
  1081. mod_timer (&dev->resume, jiffies + (HZ * autoresume));
  1082. DBG (dev, "suspend, wakeup in %d seconds\n", autoresume);
  1083. } else
  1084. DBG (dev, "suspend\n");
  1085. }
  1086. static void
  1087. zero_resume (struct usb_gadget *gadget)
  1088. {
  1089. struct zero_dev *dev = get_gadget_data (gadget);
  1090. DBG (dev, "resume\n");
  1091. del_timer (&dev->resume);
  1092. }
  1093. /*-------------------------------------------------------------------------*/
  1094. static struct usb_gadget_driver zero_driver = {
  1095. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1096. .speed = USB_SPEED_HIGH,
  1097. #else
  1098. .speed = USB_SPEED_FULL,
  1099. #endif
  1100. .function = (char *) longname,
  1101. .bind = zero_bind,
  1102. .unbind = zero_unbind,
  1103. .setup = zero_setup,
  1104. .disconnect = zero_disconnect,
  1105. .suspend = zero_suspend,
  1106. .resume = zero_resume,
  1107. .driver = {
  1108. .name = (char *) shortname,
  1109. // .shutdown = ...
  1110. // .suspend = ...
  1111. // .resume = ...
  1112. },
  1113. };
  1114. MODULE_AUTHOR ("David Brownell");
  1115. MODULE_LICENSE ("Dual BSD/GPL");
  1116. static int __init init (void)
  1117. {
  1118. /* a real value would likely come through some id prom
  1119. * or module option. this one takes at least two packets.
  1120. */
  1121. strlcpy (serial, "0123456789.0123456789.0123456789", sizeof serial);
  1122. return usb_gadget_register_driver (&zero_driver);
  1123. }
  1124. module_init (init);
  1125. static void __exit cleanup (void)
  1126. {
  1127. usb_gadget_unregister_driver (&zero_driver);
  1128. }
  1129. module_exit (cleanup);