ether.c 68 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565
  1. /*
  2. * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
  3. *
  4. * Copyright (C) 2003-2005 David Brownell
  5. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  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 1
  22. // #define VERBOSE
  23. #include <linux/config.h>
  24. #include <linux/module.h>
  25. #include <linux/kernel.h>
  26. #include <linux/delay.h>
  27. #include <linux/ioport.h>
  28. #include <linux/sched.h>
  29. #include <linux/slab.h>
  30. #include <linux/smp_lock.h>
  31. #include <linux/errno.h>
  32. #include <linux/init.h>
  33. #include <linux/timer.h>
  34. #include <linux/list.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/utsname.h>
  37. #include <linux/device.h>
  38. #include <linux/moduleparam.h>
  39. #include <linux/ctype.h>
  40. #include <asm/byteorder.h>
  41. #include <asm/io.h>
  42. #include <asm/irq.h>
  43. #include <asm/system.h>
  44. #include <asm/uaccess.h>
  45. #include <asm/unaligned.h>
  46. #include <linux/usb_ch9.h>
  47. #include <linux/usb_cdc.h>
  48. #include <linux/usb_gadget.h>
  49. #include <linux/random.h>
  50. #include <linux/netdevice.h>
  51. #include <linux/etherdevice.h>
  52. #include <linux/ethtool.h>
  53. #include "gadget_chips.h"
  54. /*-------------------------------------------------------------------------*/
  55. /*
  56. * Ethernet gadget driver -- with CDC and non-CDC options
  57. * Builds on hardware support for a full duplex link.
  58. *
  59. * CDC Ethernet is the standard USB solution for sending Ethernet frames
  60. * using USB. Real hardware tends to use the same framing protocol but look
  61. * different for control features. This driver strongly prefers to use
  62. * this USB-IF standard as its open-systems interoperability solution;
  63. * most host side USB stacks (except from Microsoft) support it.
  64. *
  65. * There's some hardware that can't talk CDC. We make that hardware
  66. * implement a "minimalist" vendor-agnostic CDC core: same framing, but
  67. * link-level setup only requires activating the configuration.
  68. * Linux supports it, but other host operating systems may not.
  69. * (This is a subset of CDC Ethernet.)
  70. *
  71. * A third option is also in use. Rather than CDC Ethernet, or something
  72. * simpler, Microsoft pushes their own approach: RNDIS. The published
  73. * RNDIS specs are ambiguous and appear to be incomplete, and are also
  74. * needlessly complex.
  75. */
  76. #define DRIVER_DESC "Ethernet Gadget"
  77. #define DRIVER_VERSION "May Day 2005"
  78. static const char shortname [] = "ether";
  79. static const char driver_desc [] = DRIVER_DESC;
  80. #define RX_EXTRA 20 /* guard against rx overflows */
  81. #include "rndis.h"
  82. #ifndef CONFIG_USB_ETH_RNDIS
  83. #define rndis_uninit(x) do{}while(0)
  84. #define rndis_deregister(c) do{}while(0)
  85. #define rndis_exit() do{}while(0)
  86. #endif
  87. /* CDC and RNDIS support the same host-chosen outgoing packet filters. */
  88. #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
  89. |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
  90. |USB_CDC_PACKET_TYPE_PROMISCUOUS \
  91. |USB_CDC_PACKET_TYPE_DIRECTED)
  92. /*-------------------------------------------------------------------------*/
  93. struct eth_dev {
  94. spinlock_t lock;
  95. struct usb_gadget *gadget;
  96. struct usb_request *req; /* for control responses */
  97. struct usb_request *stat_req; /* for cdc & rndis status */
  98. u8 config;
  99. struct usb_ep *in_ep, *out_ep, *status_ep;
  100. const struct usb_endpoint_descriptor
  101. *in, *out, *status;
  102. struct list_head tx_reqs, rx_reqs;
  103. struct net_device *net;
  104. struct net_device_stats stats;
  105. atomic_t tx_qlen;
  106. struct work_struct work;
  107. unsigned zlp:1;
  108. unsigned cdc:1;
  109. unsigned rndis:1;
  110. unsigned suspended:1;
  111. u16 cdc_filter;
  112. unsigned long todo;
  113. #define WORK_RX_MEMORY 0
  114. int rndis_config;
  115. u8 host_mac [ETH_ALEN];
  116. };
  117. /* This version autoconfigures as much as possible at run-time.
  118. *
  119. * It also ASSUMES a self-powered device, without remote wakeup,
  120. * although remote wakeup support would make sense.
  121. */
  122. /*-------------------------------------------------------------------------*/
  123. /* DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  124. * Instead: allocate your own, using normal USB-IF procedures.
  125. */
  126. /* Thanks to NetChip Technologies for donating this product ID.
  127. * It's for devices with only CDC Ethernet configurations.
  128. */
  129. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  130. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  131. /* For hardware that can't talk CDC, we use the same vendor ID that
  132. * ARM Linux has used for ethernet-over-usb, both with sa1100 and
  133. * with pxa250. We're protocol-compatible, if the host-side drivers
  134. * use the endpoint descriptors. bcdDevice (version) is nonzero, so
  135. * drivers that need to hard-wire endpoint numbers have a hook.
  136. *
  137. * The protocol is a minimal subset of CDC Ether, which works on any bulk
  138. * hardware that's not deeply broken ... even on hardware that can't talk
  139. * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
  140. * doesn't handle control-OUT).
  141. */
  142. #define SIMPLE_VENDOR_NUM 0x049f
  143. #define SIMPLE_PRODUCT_NUM 0x505a
  144. /* For hardware that can talk RNDIS and either of the above protocols,
  145. * use this ID ... the windows INF files will know it. Unless it's
  146. * used with CDC Ethernet, Linux 2.4 hosts will need updates to choose
  147. * the non-RNDIS configuration.
  148. */
  149. #define RNDIS_VENDOR_NUM 0x0525 /* NetChip */
  150. #define RNDIS_PRODUCT_NUM 0xa4a2 /* Ethernet/RNDIS Gadget */
  151. /* Some systems will want different product identifers published in the
  152. * device descriptor, either numbers or strings or both. These string
  153. * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  154. */
  155. static ushort __initdata idVendor;
  156. module_param(idVendor, ushort, S_IRUGO);
  157. MODULE_PARM_DESC(idVendor, "USB Vendor ID");
  158. static ushort __initdata idProduct;
  159. module_param(idProduct, ushort, S_IRUGO);
  160. MODULE_PARM_DESC(idProduct, "USB Product ID");
  161. static ushort __initdata bcdDevice;
  162. module_param(bcdDevice, ushort, S_IRUGO);
  163. MODULE_PARM_DESC(bcdDevice, "USB Device version (BCD)");
  164. static char *__initdata iManufacturer;
  165. module_param(iManufacturer, charp, S_IRUGO);
  166. MODULE_PARM_DESC(iManufacturer, "USB Manufacturer string");
  167. static char *__initdata iProduct;
  168. module_param(iProduct, charp, S_IRUGO);
  169. MODULE_PARM_DESC(iProduct, "USB Product string");
  170. /* initial value, changed by "ifconfig usb0 hw ether xx:xx:xx:xx:xx:xx" */
  171. static char *__initdata dev_addr;
  172. module_param(dev_addr, charp, S_IRUGO);
  173. MODULE_PARM_DESC(dev_addr, "Device Ethernet Address");
  174. /* this address is invisible to ifconfig */
  175. static char *__initdata host_addr;
  176. module_param(host_addr, charp, S_IRUGO);
  177. MODULE_PARM_DESC(host_addr, "Host Ethernet Address");
  178. /*-------------------------------------------------------------------------*/
  179. /* Include CDC support if we could run on CDC-capable hardware. */
  180. #ifdef CONFIG_USB_GADGET_NET2280
  181. #define DEV_CONFIG_CDC
  182. #endif
  183. #ifdef CONFIG_USB_GADGET_DUMMY_HCD
  184. #define DEV_CONFIG_CDC
  185. #endif
  186. #ifdef CONFIG_USB_GADGET_GOKU
  187. #define DEV_CONFIG_CDC
  188. #endif
  189. #ifdef CONFIG_USB_GADGET_LH7A40X
  190. #define DEV_CONFIG_CDC
  191. #endif
  192. #ifdef CONFIG_USB_GADGET_MQ11XX
  193. #define DEV_CONFIG_CDC
  194. #endif
  195. #ifdef CONFIG_USB_GADGET_OMAP
  196. #define DEV_CONFIG_CDC
  197. #endif
  198. #ifdef CONFIG_USB_GADGET_N9604
  199. #define DEV_CONFIG_CDC
  200. #endif
  201. #ifdef CONFIG_USB_GADGET_PXA27X
  202. #define DEV_CONFIG_CDC
  203. #endif
  204. #ifdef CONFIG_USB_GADGET_AT91
  205. #define DEV_CONFIG_CDC
  206. #endif
  207. #ifdef CONFIG_USB_GADGET_MUSBHSFC
  208. #define DEV_CONFIG_CDC
  209. #endif
  210. #ifdef CONFIG_USB_GADGET_MUSBHDRC
  211. #define DEV_CONFIG_CDC
  212. #endif
  213. /* For CDC-incapable hardware, choose the simple cdc subset.
  214. * Anything that talks bulk (without notable bugs) can do this.
  215. */
  216. #ifdef CONFIG_USB_GADGET_PXA2XX
  217. #define DEV_CONFIG_SUBSET
  218. #endif
  219. #ifdef CONFIG_USB_GADGET_SH
  220. #define DEV_CONFIG_SUBSET
  221. #endif
  222. #ifdef CONFIG_USB_GADGET_SA1100
  223. /* use non-CDC for backwards compatibility */
  224. #define DEV_CONFIG_SUBSET
  225. #endif
  226. #ifdef CONFIG_USB_GADGET_S3C2410
  227. #define DEV_CONFIG_CDC
  228. #endif
  229. /*-------------------------------------------------------------------------*/
  230. /* "main" config is either CDC, or its simple subset */
  231. static inline int is_cdc(struct eth_dev *dev)
  232. {
  233. #if !defined(DEV_CONFIG_SUBSET)
  234. return 1; /* only cdc possible */
  235. #elif !defined (DEV_CONFIG_CDC)
  236. return 0; /* only subset possible */
  237. #else
  238. return dev->cdc; /* depends on what hardware we found */
  239. #endif
  240. }
  241. /* "secondary" RNDIS config may sometimes be activated */
  242. static inline int rndis_active(struct eth_dev *dev)
  243. {
  244. #ifdef CONFIG_USB_ETH_RNDIS
  245. return dev->rndis;
  246. #else
  247. return 0;
  248. #endif
  249. }
  250. #define subset_active(dev) (!is_cdc(dev) && !rndis_active(dev))
  251. #define cdc_active(dev) ( is_cdc(dev) && !rndis_active(dev))
  252. #define DEFAULT_QLEN 2 /* double buffering by default */
  253. /* peak bulk transfer bits-per-second */
  254. #define HS_BPS (13 * 512 * 8 * 1000 * 8)
  255. #define FS_BPS (19 * 64 * 1 * 1000 * 8)
  256. #ifdef CONFIG_USB_GADGET_DUALSPEED
  257. #define DEVSPEED USB_SPEED_HIGH
  258. static unsigned qmult = 5;
  259. module_param (qmult, uint, S_IRUGO|S_IWUSR);
  260. /* for dual-speed hardware, use deeper queues at highspeed */
  261. #define qlen(gadget) \
  262. (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
  263. /* also defer IRQs on highspeed TX */
  264. #define TX_DELAY qmult
  265. static inline int BITRATE(struct usb_gadget *g)
  266. {
  267. return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
  268. }
  269. #else /* full speed (low speed doesn't do bulk) */
  270. #define DEVSPEED USB_SPEED_FULL
  271. #define qlen(gadget) DEFAULT_QLEN
  272. static inline int BITRATE(struct usb_gadget *g)
  273. {
  274. return FS_BPS;
  275. }
  276. #endif
  277. /*-------------------------------------------------------------------------*/
  278. #define xprintk(d,level,fmt,args...) \
  279. printk(level "%s: " fmt , (d)->net->name , ## args)
  280. #ifdef DEBUG
  281. #undef DEBUG
  282. #define DEBUG(dev,fmt,args...) \
  283. xprintk(dev , KERN_DEBUG , fmt , ## args)
  284. #else
  285. #define DEBUG(dev,fmt,args...) \
  286. do { } while (0)
  287. #endif /* DEBUG */
  288. #ifdef VERBOSE
  289. #define VDEBUG DEBUG
  290. #else
  291. #define VDEBUG(dev,fmt,args...) \
  292. do { } while (0)
  293. #endif /* DEBUG */
  294. #define ERROR(dev,fmt,args...) \
  295. xprintk(dev , KERN_ERR , fmt , ## args)
  296. #define WARN(dev,fmt,args...) \
  297. xprintk(dev , KERN_WARNING , fmt , ## args)
  298. #define INFO(dev,fmt,args...) \
  299. xprintk(dev , KERN_INFO , fmt , ## args)
  300. /*-------------------------------------------------------------------------*/
  301. /* USB DRIVER HOOKUP (to the hardware driver, below us), mostly
  302. * ep0 implementation: descriptors, config management, setup().
  303. * also optional class-specific notification interrupt transfer.
  304. */
  305. /*
  306. * DESCRIPTORS ... most are static, but strings and (full) configuration
  307. * descriptors are built on demand. For now we do either full CDC, or
  308. * our simple subset, with RNDIS as an optional second configuration.
  309. *
  310. * RNDIS includes some CDC ACM descriptors ... like CDC Ethernet. But
  311. * the class descriptors match a modem (they're ignored; it's really just
  312. * Ethernet functionality), they don't need the NOP altsetting, and the
  313. * status transfer endpoint isn't optional.
  314. */
  315. #define STRING_MANUFACTURER 1
  316. #define STRING_PRODUCT 2
  317. #define STRING_ETHADDR 3
  318. #define STRING_DATA 4
  319. #define STRING_CONTROL 5
  320. #define STRING_RNDIS_CONTROL 6
  321. #define STRING_CDC 7
  322. #define STRING_SUBSET 8
  323. #define STRING_RNDIS 9
  324. /* holds our biggest descriptor (or RNDIS response) */
  325. #define USB_BUFSIZ 256
  326. /*
  327. * This device advertises one configuration, eth_config, unless RNDIS
  328. * is enabled (rndis_config) on hardware supporting at least two configs.
  329. *
  330. * NOTE: Controllers like superh_udc should probably be able to use
  331. * an RNDIS-only configuration.
  332. *
  333. * FIXME define some higher-powered configurations to make it easier
  334. * to recharge batteries ...
  335. */
  336. #define DEV_CONFIG_VALUE 1 /* cdc or subset */
  337. #define DEV_RNDIS_CONFIG_VALUE 2 /* rndis; optional */
  338. static struct usb_device_descriptor
  339. device_desc = {
  340. .bLength = sizeof device_desc,
  341. .bDescriptorType = USB_DT_DEVICE,
  342. .bcdUSB = __constant_cpu_to_le16 (0x0200),
  343. .bDeviceClass = USB_CLASS_COMM,
  344. .bDeviceSubClass = 0,
  345. .bDeviceProtocol = 0,
  346. .idVendor = __constant_cpu_to_le16 (CDC_VENDOR_NUM),
  347. .idProduct = __constant_cpu_to_le16 (CDC_PRODUCT_NUM),
  348. .iManufacturer = STRING_MANUFACTURER,
  349. .iProduct = STRING_PRODUCT,
  350. .bNumConfigurations = 1,
  351. };
  352. static struct usb_otg_descriptor
  353. otg_descriptor = {
  354. .bLength = sizeof otg_descriptor,
  355. .bDescriptorType = USB_DT_OTG,
  356. .bmAttributes = USB_OTG_SRP,
  357. };
  358. static struct usb_config_descriptor
  359. eth_config = {
  360. .bLength = sizeof eth_config,
  361. .bDescriptorType = USB_DT_CONFIG,
  362. /* compute wTotalLength on the fly */
  363. .bNumInterfaces = 2,
  364. .bConfigurationValue = DEV_CONFIG_VALUE,
  365. .iConfiguration = STRING_CDC,
  366. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  367. .bMaxPower = 50,
  368. };
  369. #ifdef CONFIG_USB_ETH_RNDIS
  370. static struct usb_config_descriptor
  371. rndis_config = {
  372. .bLength = sizeof rndis_config,
  373. .bDescriptorType = USB_DT_CONFIG,
  374. /* compute wTotalLength on the fly */
  375. .bNumInterfaces = 2,
  376. .bConfigurationValue = DEV_RNDIS_CONFIG_VALUE,
  377. .iConfiguration = STRING_RNDIS,
  378. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  379. .bMaxPower = 50,
  380. };
  381. #endif
  382. /*
  383. * Compared to the simple CDC subset, the full CDC Ethernet model adds
  384. * three class descriptors, two interface descriptors, optional status
  385. * endpoint. Both have a "data" interface and two bulk endpoints.
  386. * There are also differences in how control requests are handled.
  387. *
  388. * RNDIS shares a lot with CDC-Ethernet, since it's a variant of
  389. * the CDC-ACM (modem) spec.
  390. */
  391. #ifdef DEV_CONFIG_CDC
  392. static struct usb_interface_descriptor
  393. control_intf = {
  394. .bLength = sizeof control_intf,
  395. .bDescriptorType = USB_DT_INTERFACE,
  396. .bInterfaceNumber = 0,
  397. /* status endpoint is optional; this may be patched later */
  398. .bNumEndpoints = 1,
  399. .bInterfaceClass = USB_CLASS_COMM,
  400. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
  401. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  402. .iInterface = STRING_CONTROL,
  403. };
  404. #endif
  405. #ifdef CONFIG_USB_ETH_RNDIS
  406. static const struct usb_interface_descriptor
  407. rndis_control_intf = {
  408. .bLength = sizeof rndis_control_intf,
  409. .bDescriptorType = USB_DT_INTERFACE,
  410. .bInterfaceNumber = 0,
  411. .bNumEndpoints = 1,
  412. .bInterfaceClass = USB_CLASS_COMM,
  413. .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
  414. .bInterfaceProtocol = USB_CDC_ACM_PROTO_VENDOR,
  415. .iInterface = STRING_RNDIS_CONTROL,
  416. };
  417. #endif
  418. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  419. static const struct usb_cdc_header_desc header_desc = {
  420. .bLength = sizeof header_desc,
  421. .bDescriptorType = USB_DT_CS_INTERFACE,
  422. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  423. .bcdCDC = __constant_cpu_to_le16 (0x0110),
  424. };
  425. static const struct usb_cdc_union_desc union_desc = {
  426. .bLength = sizeof union_desc,
  427. .bDescriptorType = USB_DT_CS_INTERFACE,
  428. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  429. .bMasterInterface0 = 0, /* index of control interface */
  430. .bSlaveInterface0 = 1, /* index of DATA interface */
  431. };
  432. #endif /* CDC || RNDIS */
  433. #ifdef CONFIG_USB_ETH_RNDIS
  434. static const struct usb_cdc_call_mgmt_descriptor call_mgmt_descriptor = {
  435. .bLength = sizeof call_mgmt_descriptor,
  436. .bDescriptorType = USB_DT_CS_INTERFACE,
  437. .bDescriptorSubType = USB_CDC_CALL_MANAGEMENT_TYPE,
  438. .bmCapabilities = 0x00,
  439. .bDataInterface = 0x01,
  440. };
  441. static const struct usb_cdc_acm_descriptor acm_descriptor = {
  442. .bLength = sizeof acm_descriptor,
  443. .bDescriptorType = USB_DT_CS_INTERFACE,
  444. .bDescriptorSubType = USB_CDC_ACM_TYPE,
  445. .bmCapabilities = 0x00,
  446. };
  447. #endif
  448. #ifdef DEV_CONFIG_CDC
  449. static const struct usb_cdc_ether_desc ether_desc = {
  450. .bLength = sizeof ether_desc,
  451. .bDescriptorType = USB_DT_CS_INTERFACE,
  452. .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
  453. /* this descriptor actually adds value, surprise! */
  454. .iMACAddress = STRING_ETHADDR,
  455. .bmEthernetStatistics = __constant_cpu_to_le32 (0), /* no statistics */
  456. .wMaxSegmentSize = __constant_cpu_to_le16 (ETH_FRAME_LEN),
  457. .wNumberMCFilters = __constant_cpu_to_le16 (0),
  458. .bNumberPowerFilters = 0,
  459. };
  460. #endif
  461. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  462. /* include the status endpoint if we can, even where it's optional.
  463. * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  464. * packet, to simplify cancellation; and a big transfer interval, to
  465. * waste less bandwidth.
  466. *
  467. * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
  468. * if they ignore the connect/disconnect notifications that real aether
  469. * can provide. more advanced cdc configurations might want to support
  470. * encapsulated commands (vendor-specific, using control-OUT).
  471. *
  472. * RNDIS requires the status endpoint, since it uses that encapsulation
  473. * mechanism for its funky RPC scheme.
  474. */
  475. #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  476. #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
  477. static struct usb_endpoint_descriptor
  478. fs_status_desc = {
  479. .bLength = USB_DT_ENDPOINT_SIZE,
  480. .bDescriptorType = USB_DT_ENDPOINT,
  481. .bEndpointAddress = USB_DIR_IN,
  482. .bmAttributes = USB_ENDPOINT_XFER_INT,
  483. .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
  484. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  485. };
  486. #endif
  487. #ifdef DEV_CONFIG_CDC
  488. /* the default data interface has no endpoints ... */
  489. static const struct usb_interface_descriptor
  490. data_nop_intf = {
  491. .bLength = sizeof data_nop_intf,
  492. .bDescriptorType = USB_DT_INTERFACE,
  493. .bInterfaceNumber = 1,
  494. .bAlternateSetting = 0,
  495. .bNumEndpoints = 0,
  496. .bInterfaceClass = USB_CLASS_CDC_DATA,
  497. .bInterfaceSubClass = 0,
  498. .bInterfaceProtocol = 0,
  499. };
  500. /* ... but the "real" data interface has two bulk endpoints */
  501. static const struct usb_interface_descriptor
  502. data_intf = {
  503. .bLength = sizeof data_intf,
  504. .bDescriptorType = USB_DT_INTERFACE,
  505. .bInterfaceNumber = 1,
  506. .bAlternateSetting = 1,
  507. .bNumEndpoints = 2,
  508. .bInterfaceClass = USB_CLASS_CDC_DATA,
  509. .bInterfaceSubClass = 0,
  510. .bInterfaceProtocol = 0,
  511. .iInterface = STRING_DATA,
  512. };
  513. #endif
  514. #ifdef CONFIG_USB_ETH_RNDIS
  515. /* RNDIS doesn't activate by changing to the "real" altsetting */
  516. static const struct usb_interface_descriptor
  517. rndis_data_intf = {
  518. .bLength = sizeof rndis_data_intf,
  519. .bDescriptorType = USB_DT_INTERFACE,
  520. .bInterfaceNumber = 1,
  521. .bAlternateSetting = 0,
  522. .bNumEndpoints = 2,
  523. .bInterfaceClass = USB_CLASS_CDC_DATA,
  524. .bInterfaceSubClass = 0,
  525. .bInterfaceProtocol = 0,
  526. .iInterface = STRING_DATA,
  527. };
  528. #endif
  529. #ifdef DEV_CONFIG_SUBSET
  530. /*
  531. * "Simple" CDC-subset option is a simple vendor-neutral model that most
  532. * full speed controllers can handle: one interface, two bulk endpoints.
  533. */
  534. static const struct usb_interface_descriptor
  535. subset_data_intf = {
  536. .bLength = sizeof subset_data_intf,
  537. .bDescriptorType = USB_DT_INTERFACE,
  538. .bInterfaceNumber = 0,
  539. .bAlternateSetting = 0,
  540. .bNumEndpoints = 2,
  541. .bInterfaceClass = USB_CLASS_VENDOR_SPEC,
  542. .bInterfaceSubClass = 0,
  543. .bInterfaceProtocol = 0,
  544. .iInterface = STRING_DATA,
  545. };
  546. #endif /* SUBSET */
  547. static struct usb_endpoint_descriptor
  548. fs_source_desc = {
  549. .bLength = USB_DT_ENDPOINT_SIZE,
  550. .bDescriptorType = USB_DT_ENDPOINT,
  551. .bEndpointAddress = USB_DIR_IN,
  552. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  553. };
  554. static struct usb_endpoint_descriptor
  555. fs_sink_desc = {
  556. .bLength = USB_DT_ENDPOINT_SIZE,
  557. .bDescriptorType = USB_DT_ENDPOINT,
  558. .bEndpointAddress = USB_DIR_OUT,
  559. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  560. };
  561. static const struct usb_descriptor_header *fs_eth_function [11] = {
  562. (struct usb_descriptor_header *) &otg_descriptor,
  563. #ifdef DEV_CONFIG_CDC
  564. /* "cdc" mode descriptors */
  565. (struct usb_descriptor_header *) &control_intf,
  566. (struct usb_descriptor_header *) &header_desc,
  567. (struct usb_descriptor_header *) &union_desc,
  568. (struct usb_descriptor_header *) &ether_desc,
  569. /* NOTE: status endpoint may need to be removed */
  570. (struct usb_descriptor_header *) &fs_status_desc,
  571. /* data interface, with altsetting */
  572. (struct usb_descriptor_header *) &data_nop_intf,
  573. (struct usb_descriptor_header *) &data_intf,
  574. (struct usb_descriptor_header *) &fs_source_desc,
  575. (struct usb_descriptor_header *) &fs_sink_desc,
  576. NULL,
  577. #endif /* DEV_CONFIG_CDC */
  578. };
  579. static inline void __init fs_subset_descriptors(void)
  580. {
  581. #ifdef DEV_CONFIG_SUBSET
  582. fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
  583. fs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
  584. fs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
  585. fs_eth_function[4] = NULL;
  586. #else
  587. fs_eth_function[1] = NULL;
  588. #endif
  589. }
  590. #ifdef CONFIG_USB_ETH_RNDIS
  591. static const struct usb_descriptor_header *fs_rndis_function [] = {
  592. (struct usb_descriptor_header *) &otg_descriptor,
  593. /* control interface matches ACM, not Ethernet */
  594. (struct usb_descriptor_header *) &rndis_control_intf,
  595. (struct usb_descriptor_header *) &header_desc,
  596. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  597. (struct usb_descriptor_header *) &acm_descriptor,
  598. (struct usb_descriptor_header *) &union_desc,
  599. (struct usb_descriptor_header *) &fs_status_desc,
  600. /* data interface has no altsetting */
  601. (struct usb_descriptor_header *) &rndis_data_intf,
  602. (struct usb_descriptor_header *) &fs_source_desc,
  603. (struct usb_descriptor_header *) &fs_sink_desc,
  604. NULL,
  605. };
  606. #endif
  607. #ifdef CONFIG_USB_GADGET_DUALSPEED
  608. /*
  609. * usb 2.0 devices need to expose both high speed and full speed
  610. * descriptors, unless they only run at full speed.
  611. */
  612. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  613. static struct usb_endpoint_descriptor
  614. hs_status_desc = {
  615. .bLength = USB_DT_ENDPOINT_SIZE,
  616. .bDescriptorType = USB_DT_ENDPOINT,
  617. .bmAttributes = USB_ENDPOINT_XFER_INT,
  618. .wMaxPacketSize = __constant_cpu_to_le16 (STATUS_BYTECOUNT),
  619. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  620. };
  621. #endif /* DEV_CONFIG_CDC */
  622. static struct usb_endpoint_descriptor
  623. hs_source_desc = {
  624. .bLength = USB_DT_ENDPOINT_SIZE,
  625. .bDescriptorType = USB_DT_ENDPOINT,
  626. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  627. .wMaxPacketSize = __constant_cpu_to_le16 (512),
  628. };
  629. static struct usb_endpoint_descriptor
  630. hs_sink_desc = {
  631. .bLength = USB_DT_ENDPOINT_SIZE,
  632. .bDescriptorType = USB_DT_ENDPOINT,
  633. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  634. .wMaxPacketSize = __constant_cpu_to_le16 (512),
  635. };
  636. static struct usb_qualifier_descriptor
  637. dev_qualifier = {
  638. .bLength = sizeof dev_qualifier,
  639. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  640. .bcdUSB = __constant_cpu_to_le16 (0x0200),
  641. .bDeviceClass = USB_CLASS_COMM,
  642. .bNumConfigurations = 1,
  643. };
  644. static const struct usb_descriptor_header *hs_eth_function [11] = {
  645. (struct usb_descriptor_header *) &otg_descriptor,
  646. #ifdef DEV_CONFIG_CDC
  647. /* "cdc" mode descriptors */
  648. (struct usb_descriptor_header *) &control_intf,
  649. (struct usb_descriptor_header *) &header_desc,
  650. (struct usb_descriptor_header *) &union_desc,
  651. (struct usb_descriptor_header *) &ether_desc,
  652. /* NOTE: status endpoint may need to be removed */
  653. (struct usb_descriptor_header *) &hs_status_desc,
  654. /* data interface, with altsetting */
  655. (struct usb_descriptor_header *) &data_nop_intf,
  656. (struct usb_descriptor_header *) &data_intf,
  657. (struct usb_descriptor_header *) &hs_source_desc,
  658. (struct usb_descriptor_header *) &hs_sink_desc,
  659. NULL,
  660. #endif /* DEV_CONFIG_CDC */
  661. };
  662. static inline void __init hs_subset_descriptors(void)
  663. {
  664. #ifdef DEV_CONFIG_SUBSET
  665. hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
  666. hs_eth_function[2] = (struct usb_descriptor_header *) &fs_source_desc;
  667. hs_eth_function[3] = (struct usb_descriptor_header *) &fs_sink_desc;
  668. hs_eth_function[4] = NULL;
  669. #else
  670. hs_eth_function[1] = NULL;
  671. #endif
  672. }
  673. #ifdef CONFIG_USB_ETH_RNDIS
  674. static const struct usb_descriptor_header *hs_rndis_function [] = {
  675. (struct usb_descriptor_header *) &otg_descriptor,
  676. /* control interface matches ACM, not Ethernet */
  677. (struct usb_descriptor_header *) &rndis_control_intf,
  678. (struct usb_descriptor_header *) &header_desc,
  679. (struct usb_descriptor_header *) &call_mgmt_descriptor,
  680. (struct usb_descriptor_header *) &acm_descriptor,
  681. (struct usb_descriptor_header *) &union_desc,
  682. (struct usb_descriptor_header *) &hs_status_desc,
  683. /* data interface has no altsetting */
  684. (struct usb_descriptor_header *) &rndis_data_intf,
  685. (struct usb_descriptor_header *) &hs_source_desc,
  686. (struct usb_descriptor_header *) &hs_sink_desc,
  687. NULL,
  688. };
  689. #endif
  690. /* maxpacket and other transfer characteristics vary by speed. */
  691. #define ep_desc(g,hs,fs) (((g)->speed==USB_SPEED_HIGH)?(hs):(fs))
  692. #else
  693. /* if there's no high speed support, maxpacket doesn't change. */
  694. #define ep_desc(g,hs,fs) (((void)(g)), (fs))
  695. static inline void __init hs_subset_descriptors(void)
  696. {
  697. }
  698. #endif /* !CONFIG_USB_GADGET_DUALSPEED */
  699. /*-------------------------------------------------------------------------*/
  700. /* descriptors that are built on-demand */
  701. static char manufacturer [50];
  702. static char product_desc [40] = DRIVER_DESC;
  703. #ifdef DEV_CONFIG_CDC
  704. /* address that the host will use ... usually assigned at random */
  705. static char ethaddr [2 * ETH_ALEN + 1];
  706. #endif
  707. /* static strings, in UTF-8 */
  708. static struct usb_string strings [] = {
  709. { STRING_MANUFACTURER, manufacturer, },
  710. { STRING_PRODUCT, product_desc, },
  711. { STRING_DATA, "Ethernet Data", },
  712. #ifdef DEV_CONFIG_CDC
  713. { STRING_CDC, "CDC Ethernet", },
  714. { STRING_ETHADDR, ethaddr, },
  715. { STRING_CONTROL, "CDC Communications Control", },
  716. #endif
  717. #ifdef DEV_CONFIG_SUBSET
  718. { STRING_SUBSET, "CDC Ethernet Subset", },
  719. #endif
  720. #ifdef CONFIG_USB_ETH_RNDIS
  721. { STRING_RNDIS, "RNDIS", },
  722. { STRING_RNDIS_CONTROL, "RNDIS Communications Control", },
  723. #endif
  724. { } /* end of list */
  725. };
  726. static struct usb_gadget_strings stringtab = {
  727. .language = 0x0409, /* en-us */
  728. .strings = strings,
  729. };
  730. /*
  731. * one config, two interfaces: control, data.
  732. * complications: class descriptors, and an altsetting.
  733. */
  734. static int
  735. config_buf (enum usb_device_speed speed,
  736. u8 *buf, u8 type,
  737. unsigned index, int is_otg)
  738. {
  739. int len;
  740. const struct usb_config_descriptor *config;
  741. const struct usb_descriptor_header **function;
  742. #ifdef CONFIG_USB_GADGET_DUALSPEED
  743. int hs = (speed == USB_SPEED_HIGH);
  744. if (type == USB_DT_OTHER_SPEED_CONFIG)
  745. hs = !hs;
  746. #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
  747. #else
  748. #define which_fn(t) (fs_ ## t ## _function)
  749. #endif
  750. if (index >= device_desc.bNumConfigurations)
  751. return -EINVAL;
  752. #ifdef CONFIG_USB_ETH_RNDIS
  753. /* list the RNDIS config first, to make Microsoft's drivers
  754. * happy. DOCSIS 1.0 needs this too.
  755. */
  756. if (device_desc.bNumConfigurations == 2 && index == 0) {
  757. config = &rndis_config;
  758. function = which_fn (rndis);
  759. } else
  760. #endif
  761. {
  762. config = &eth_config;
  763. function = which_fn (eth);
  764. }
  765. /* for now, don't advertise srp-only devices */
  766. if (!is_otg)
  767. function++;
  768. len = usb_gadget_config_buf (config, buf, USB_BUFSIZ, function);
  769. if (len < 0)
  770. return len;
  771. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  772. return len;
  773. }
  774. /*-------------------------------------------------------------------------*/
  775. static void eth_start (struct eth_dev *dev, gfp_t gfp_flags);
  776. static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
  777. static int
  778. set_ether_config (struct eth_dev *dev, gfp_t gfp_flags)
  779. {
  780. int result = 0;
  781. struct usb_gadget *gadget = dev->gadget;
  782. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  783. /* status endpoint used for RNDIS and (optionally) CDC */
  784. if (!subset_active(dev) && dev->status_ep) {
  785. dev->status = ep_desc (gadget, &hs_status_desc,
  786. &fs_status_desc);
  787. dev->status_ep->driver_data = dev;
  788. result = usb_ep_enable (dev->status_ep, dev->status);
  789. if (result != 0) {
  790. DEBUG (dev, "enable %s --> %d\n",
  791. dev->status_ep->name, result);
  792. goto done;
  793. }
  794. }
  795. #endif
  796. dev->in = ep_desc (dev->gadget, &hs_source_desc, &fs_source_desc);
  797. dev->in_ep->driver_data = dev;
  798. dev->out = ep_desc (dev->gadget, &hs_sink_desc, &fs_sink_desc);
  799. dev->out_ep->driver_data = dev;
  800. /* With CDC, the host isn't allowed to use these two data
  801. * endpoints in the default altsetting for the interface.
  802. * so we don't activate them yet. Reset from SET_INTERFACE.
  803. *
  804. * Strictly speaking RNDIS should work the same: activation is
  805. * a side effect of setting a packet filter. Deactivation is
  806. * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
  807. */
  808. if (!cdc_active(dev)) {
  809. result = usb_ep_enable (dev->in_ep, dev->in);
  810. if (result != 0) {
  811. DEBUG(dev, "enable %s --> %d\n",
  812. dev->in_ep->name, result);
  813. goto done;
  814. }
  815. result = usb_ep_enable (dev->out_ep, dev->out);
  816. if (result != 0) {
  817. DEBUG (dev, "enable %s --> %d\n",
  818. dev->in_ep->name, result);
  819. goto done;
  820. }
  821. }
  822. done:
  823. if (result == 0)
  824. result = alloc_requests (dev, qlen (gadget), gfp_flags);
  825. /* on error, disable any endpoints */
  826. if (result < 0) {
  827. if (!subset_active(dev))
  828. (void) usb_ep_disable (dev->status_ep);
  829. dev->status = NULL;
  830. (void) usb_ep_disable (dev->in_ep);
  831. (void) usb_ep_disable (dev->out_ep);
  832. dev->in = NULL;
  833. dev->out = NULL;
  834. } else
  835. /* activate non-CDC configs right away
  836. * this isn't strictly according to the RNDIS spec
  837. */
  838. if (!cdc_active (dev)) {
  839. netif_carrier_on (dev->net);
  840. if (netif_running (dev->net)) {
  841. spin_unlock (&dev->lock);
  842. eth_start (dev, GFP_ATOMIC);
  843. spin_lock (&dev->lock);
  844. }
  845. }
  846. if (result == 0)
  847. DEBUG (dev, "qlen %d\n", qlen (gadget));
  848. /* caller is responsible for cleanup on error */
  849. return result;
  850. }
  851. static void eth_reset_config (struct eth_dev *dev)
  852. {
  853. struct usb_request *req;
  854. if (dev->config == 0)
  855. return;
  856. DEBUG (dev, "%s\n", __FUNCTION__);
  857. netif_stop_queue (dev->net);
  858. netif_carrier_off (dev->net);
  859. rndis_uninit(dev->rndis_config);
  860. /* disable endpoints, forcing (synchronous) completion of
  861. * pending i/o. then free the requests.
  862. */
  863. if (dev->in) {
  864. usb_ep_disable (dev->in_ep);
  865. while (likely (!list_empty (&dev->tx_reqs))) {
  866. req = container_of (dev->tx_reqs.next,
  867. struct usb_request, list);
  868. list_del (&req->list);
  869. usb_ep_free_request (dev->in_ep, req);
  870. }
  871. }
  872. if (dev->out) {
  873. usb_ep_disable (dev->out_ep);
  874. while (likely (!list_empty (&dev->rx_reqs))) {
  875. req = container_of (dev->rx_reqs.next,
  876. struct usb_request, list);
  877. list_del (&req->list);
  878. usb_ep_free_request (dev->out_ep, req);
  879. }
  880. }
  881. if (dev->status) {
  882. usb_ep_disable (dev->status_ep);
  883. }
  884. dev->rndis = 0;
  885. dev->cdc_filter = 0;
  886. dev->config = 0;
  887. }
  888. /* change our operational config. must agree with the code
  889. * that returns config descriptors, and altsetting code.
  890. */
  891. static int
  892. eth_set_config (struct eth_dev *dev, unsigned number, gfp_t gfp_flags)
  893. {
  894. int result = 0;
  895. struct usb_gadget *gadget = dev->gadget;
  896. if (gadget_is_sa1100 (gadget)
  897. && dev->config
  898. && atomic_read (&dev->tx_qlen) != 0) {
  899. /* tx fifo is full, but we can't clear it...*/
  900. INFO (dev, "can't change configurations\n");
  901. return -ESPIPE;
  902. }
  903. eth_reset_config (dev);
  904. switch (number) {
  905. case DEV_CONFIG_VALUE:
  906. result = set_ether_config (dev, gfp_flags);
  907. break;
  908. #ifdef CONFIG_USB_ETH_RNDIS
  909. case DEV_RNDIS_CONFIG_VALUE:
  910. dev->rndis = 1;
  911. result = set_ether_config (dev, gfp_flags);
  912. break;
  913. #endif
  914. default:
  915. result = -EINVAL;
  916. /* FALL THROUGH */
  917. case 0:
  918. break;
  919. }
  920. if (result) {
  921. if (number)
  922. eth_reset_config (dev);
  923. usb_gadget_vbus_draw(dev->gadget,
  924. dev->gadget->is_otg ? 8 : 100);
  925. } else {
  926. char *speed;
  927. unsigned power;
  928. power = 2 * eth_config.bMaxPower;
  929. usb_gadget_vbus_draw(dev->gadget, power);
  930. switch (gadget->speed) {
  931. case USB_SPEED_FULL: speed = "full"; break;
  932. #ifdef CONFIG_USB_GADGET_DUALSPEED
  933. case USB_SPEED_HIGH: speed = "high"; break;
  934. #endif
  935. default: speed = "?"; break;
  936. }
  937. dev->config = number;
  938. INFO (dev, "%s speed config #%d: %d mA, %s, using %s\n",
  939. speed, number, power, driver_desc,
  940. rndis_active(dev)
  941. ? "RNDIS"
  942. : (cdc_active(dev)
  943. ? "CDC Ethernet"
  944. : "CDC Ethernet Subset"));
  945. }
  946. return result;
  947. }
  948. /*-------------------------------------------------------------------------*/
  949. #ifdef DEV_CONFIG_CDC
  950. /* The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
  951. * only to notify the host about link status changes (which we support) or
  952. * report completion of some encapsulated command (as used in RNDIS). Since
  953. * we want this CDC Ethernet code to be vendor-neutral, we don't use that
  954. * command mechanism; and only one status request is ever queued.
  955. */
  956. static void eth_status_complete (struct usb_ep *ep, struct usb_request *req)
  957. {
  958. struct usb_cdc_notification *event = req->buf;
  959. int value = req->status;
  960. struct eth_dev *dev = ep->driver_data;
  961. /* issue the second notification if host reads the first */
  962. if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
  963. && value == 0) {
  964. __le32 *data = req->buf + sizeof *event;
  965. event->bmRequestType = 0xA1;
  966. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  967. event->wValue = __constant_cpu_to_le16 (0);
  968. event->wIndex = __constant_cpu_to_le16 (1);
  969. event->wLength = __constant_cpu_to_le16 (8);
  970. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  971. data [0] = data [1] = cpu_to_le32 (BITRATE (dev->gadget));
  972. req->length = STATUS_BYTECOUNT;
  973. value = usb_ep_queue (ep, req, GFP_ATOMIC);
  974. DEBUG (dev, "send SPEED_CHANGE --> %d\n", value);
  975. if (value == 0)
  976. return;
  977. } else if (value != -ECONNRESET)
  978. DEBUG (dev, "event %02x --> %d\n",
  979. event->bNotificationType, value);
  980. req->context = NULL;
  981. }
  982. static void issue_start_status (struct eth_dev *dev)
  983. {
  984. struct usb_request *req = dev->stat_req;
  985. struct usb_cdc_notification *event;
  986. int value;
  987. DEBUG (dev, "%s, flush old status first\n", __FUNCTION__);
  988. /* flush old status
  989. *
  990. * FIXME ugly idiom, maybe we'd be better with just
  991. * a "cancel the whole queue" primitive since any
  992. * unlink-one primitive has way too many error modes.
  993. * here, we "know" toggle is already clear...
  994. *
  995. * FIXME iff req->context != null just dequeue it
  996. */
  997. usb_ep_disable (dev->status_ep);
  998. usb_ep_enable (dev->status_ep, dev->status);
  999. /* 3.8.1 says to issue first NETWORK_CONNECTION, then
  1000. * a SPEED_CHANGE. could be useful in some configs.
  1001. */
  1002. event = req->buf;
  1003. event->bmRequestType = 0xA1;
  1004. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  1005. event->wValue = __constant_cpu_to_le16 (1); /* connected */
  1006. event->wIndex = __constant_cpu_to_le16 (1);
  1007. event->wLength = 0;
  1008. req->length = sizeof *event;
  1009. req->complete = eth_status_complete;
  1010. req->context = dev;
  1011. value = usb_ep_queue (dev->status_ep, req, GFP_ATOMIC);
  1012. if (value < 0)
  1013. DEBUG (dev, "status buf queue --> %d\n", value);
  1014. }
  1015. #endif
  1016. /*-------------------------------------------------------------------------*/
  1017. static void eth_setup_complete (struct usb_ep *ep, struct usb_request *req)
  1018. {
  1019. if (req->status || req->actual != req->length)
  1020. DEBUG ((struct eth_dev *) ep->driver_data,
  1021. "setup complete --> %d, %d/%d\n",
  1022. req->status, req->actual, req->length);
  1023. }
  1024. #ifdef CONFIG_USB_ETH_RNDIS
  1025. static void rndis_response_complete (struct usb_ep *ep, struct usb_request *req)
  1026. {
  1027. if (req->status || req->actual != req->length)
  1028. DEBUG ((struct eth_dev *) ep->driver_data,
  1029. "rndis response complete --> %d, %d/%d\n",
  1030. req->status, req->actual, req->length);
  1031. /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
  1032. }
  1033. static void rndis_command_complete (struct usb_ep *ep, struct usb_request *req)
  1034. {
  1035. struct eth_dev *dev = ep->driver_data;
  1036. int status;
  1037. /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
  1038. spin_lock(&dev->lock);
  1039. status = rndis_msg_parser (dev->rndis_config, (u8 *) req->buf);
  1040. if (status < 0)
  1041. ERROR(dev, "%s: rndis parse error %d\n", __FUNCTION__, status);
  1042. spin_unlock(&dev->lock);
  1043. }
  1044. #endif /* RNDIS */
  1045. /*
  1046. * The setup() callback implements all the ep0 functionality that's not
  1047. * handled lower down. CDC has a number of less-common features:
  1048. *
  1049. * - two interfaces: control, and ethernet data
  1050. * - Ethernet data interface has two altsettings: default, and active
  1051. * - class-specific descriptors for the control interface
  1052. * - class-specific control requests
  1053. */
  1054. static int
  1055. eth_setup (struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1056. {
  1057. struct eth_dev *dev = get_gadget_data (gadget);
  1058. struct usb_request *req = dev->req;
  1059. int value = -EOPNOTSUPP;
  1060. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  1061. u16 wValue = le16_to_cpu(ctrl->wValue);
  1062. u16 wLength = le16_to_cpu(ctrl->wLength);
  1063. /* descriptors just go into the pre-allocated ep0 buffer,
  1064. * while config change events may enable network traffic.
  1065. */
  1066. req->complete = eth_setup_complete;
  1067. switch (ctrl->bRequest) {
  1068. case USB_REQ_GET_DESCRIPTOR:
  1069. if (ctrl->bRequestType != USB_DIR_IN)
  1070. break;
  1071. switch (wValue >> 8) {
  1072. case USB_DT_DEVICE:
  1073. value = min (wLength, (u16) sizeof device_desc);
  1074. memcpy (req->buf, &device_desc, value);
  1075. break;
  1076. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1077. case USB_DT_DEVICE_QUALIFIER:
  1078. if (!gadget->is_dualspeed)
  1079. break;
  1080. value = min (wLength, (u16) sizeof dev_qualifier);
  1081. memcpy (req->buf, &dev_qualifier, value);
  1082. break;
  1083. case USB_DT_OTHER_SPEED_CONFIG:
  1084. if (!gadget->is_dualspeed)
  1085. break;
  1086. // FALLTHROUGH
  1087. #endif /* CONFIG_USB_GADGET_DUALSPEED */
  1088. case USB_DT_CONFIG:
  1089. value = config_buf (gadget->speed, req->buf,
  1090. wValue >> 8,
  1091. wValue & 0xff,
  1092. gadget->is_otg);
  1093. if (value >= 0)
  1094. value = min (wLength, (u16) value);
  1095. break;
  1096. case USB_DT_STRING:
  1097. value = usb_gadget_get_string (&stringtab,
  1098. wValue & 0xff, req->buf);
  1099. if (value >= 0)
  1100. value = min (wLength, (u16) value);
  1101. break;
  1102. }
  1103. break;
  1104. case USB_REQ_SET_CONFIGURATION:
  1105. if (ctrl->bRequestType != 0)
  1106. break;
  1107. if (gadget->a_hnp_support)
  1108. DEBUG (dev, "HNP available\n");
  1109. else if (gadget->a_alt_hnp_support)
  1110. DEBUG (dev, "HNP needs a different root port\n");
  1111. spin_lock (&dev->lock);
  1112. value = eth_set_config (dev, wValue, GFP_ATOMIC);
  1113. spin_unlock (&dev->lock);
  1114. break;
  1115. case USB_REQ_GET_CONFIGURATION:
  1116. if (ctrl->bRequestType != USB_DIR_IN)
  1117. break;
  1118. *(u8 *)req->buf = dev->config;
  1119. value = min (wLength, (u16) 1);
  1120. break;
  1121. case USB_REQ_SET_INTERFACE:
  1122. if (ctrl->bRequestType != USB_RECIP_INTERFACE
  1123. || !dev->config
  1124. || wIndex > 1)
  1125. break;
  1126. if (!cdc_active(dev) && wIndex != 0)
  1127. break;
  1128. spin_lock (&dev->lock);
  1129. /* PXA hardware partially handles SET_INTERFACE;
  1130. * we need to kluge around that interference.
  1131. */
  1132. if (gadget_is_pxa (gadget)) {
  1133. value = eth_set_config (dev, DEV_CONFIG_VALUE,
  1134. GFP_ATOMIC);
  1135. goto done_set_intf;
  1136. }
  1137. #ifdef DEV_CONFIG_CDC
  1138. switch (wIndex) {
  1139. case 0: /* control/master intf */
  1140. if (wValue != 0)
  1141. break;
  1142. if (dev->status) {
  1143. usb_ep_disable (dev->status_ep);
  1144. usb_ep_enable (dev->status_ep, dev->status);
  1145. }
  1146. value = 0;
  1147. break;
  1148. case 1: /* data intf */
  1149. if (wValue > 1)
  1150. break;
  1151. usb_ep_disable (dev->in_ep);
  1152. usb_ep_disable (dev->out_ep);
  1153. /* CDC requires the data transfers not be done from
  1154. * the default interface setting ... also, setting
  1155. * the non-default interface resets filters etc.
  1156. */
  1157. if (wValue == 1) {
  1158. if (!cdc_active (dev))
  1159. break;
  1160. usb_ep_enable (dev->in_ep, dev->in);
  1161. usb_ep_enable (dev->out_ep, dev->out);
  1162. dev->cdc_filter = DEFAULT_FILTER;
  1163. netif_carrier_on (dev->net);
  1164. if (dev->status)
  1165. issue_start_status (dev);
  1166. if (netif_running (dev->net)) {
  1167. spin_unlock (&dev->lock);
  1168. eth_start (dev, GFP_ATOMIC);
  1169. spin_lock (&dev->lock);
  1170. }
  1171. } else {
  1172. netif_stop_queue (dev->net);
  1173. netif_carrier_off (dev->net);
  1174. }
  1175. value = 0;
  1176. break;
  1177. }
  1178. #else
  1179. /* FIXME this is wrong, as is the assumption that
  1180. * all non-PXA hardware talks real CDC ...
  1181. */
  1182. dev_warn (&gadget->dev, "set_interface ignored!\n");
  1183. #endif /* DEV_CONFIG_CDC */
  1184. done_set_intf:
  1185. spin_unlock (&dev->lock);
  1186. break;
  1187. case USB_REQ_GET_INTERFACE:
  1188. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
  1189. || !dev->config
  1190. || wIndex > 1)
  1191. break;
  1192. if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
  1193. break;
  1194. /* for CDC, iff carrier is on, data interface is active. */
  1195. if (rndis_active(dev) || wIndex != 1)
  1196. *(u8 *)req->buf = 0;
  1197. else
  1198. *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0;
  1199. value = min (wLength, (u16) 1);
  1200. break;
  1201. #ifdef DEV_CONFIG_CDC
  1202. case USB_CDC_SET_ETHERNET_PACKET_FILTER:
  1203. /* see 6.2.30: no data, wIndex = interface,
  1204. * wValue = packet filter bitmap
  1205. */
  1206. if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1207. || !cdc_active(dev)
  1208. || wLength != 0
  1209. || wIndex > 1)
  1210. break;
  1211. DEBUG (dev, "packet filter %02x\n", wValue);
  1212. dev->cdc_filter = wValue;
  1213. value = 0;
  1214. break;
  1215. /* and potentially:
  1216. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  1217. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  1218. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  1219. * case USB_CDC_GET_ETHERNET_STATISTIC:
  1220. */
  1221. #endif /* DEV_CONFIG_CDC */
  1222. #ifdef CONFIG_USB_ETH_RNDIS
  1223. /* RNDIS uses the CDC command encapsulation mechanism to implement
  1224. * an RPC scheme, with much getting/setting of attributes by OID.
  1225. */
  1226. case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  1227. if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1228. || !rndis_active(dev)
  1229. || wLength > USB_BUFSIZ
  1230. || wValue
  1231. || rndis_control_intf.bInterfaceNumber
  1232. != wIndex)
  1233. break;
  1234. /* read the request, then process it */
  1235. value = wLength;
  1236. req->complete = rndis_command_complete;
  1237. /* later, rndis_control_ack () sends a notification */
  1238. break;
  1239. case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  1240. if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1241. == ctrl->bRequestType
  1242. && rndis_active(dev)
  1243. // && wLength >= 0x0400
  1244. && !wValue
  1245. && rndis_control_intf.bInterfaceNumber
  1246. == wIndex) {
  1247. u8 *buf;
  1248. /* return the result */
  1249. buf = rndis_get_next_response (dev->rndis_config,
  1250. &value);
  1251. if (buf) {
  1252. memcpy (req->buf, buf, value);
  1253. req->complete = rndis_response_complete;
  1254. rndis_free_response(dev->rndis_config, buf);
  1255. }
  1256. /* else stalls ... spec says to avoid that */
  1257. }
  1258. break;
  1259. #endif /* RNDIS */
  1260. default:
  1261. VDEBUG (dev,
  1262. "unknown control req%02x.%02x v%04x i%04x l%d\n",
  1263. ctrl->bRequestType, ctrl->bRequest,
  1264. wValue, wIndex, wLength);
  1265. }
  1266. /* respond with data transfer before status phase? */
  1267. if (value >= 0) {
  1268. req->length = value;
  1269. req->zero = value < wLength
  1270. && (value % gadget->ep0->maxpacket) == 0;
  1271. value = usb_ep_queue (gadget->ep0, req, GFP_ATOMIC);
  1272. if (value < 0) {
  1273. DEBUG (dev, "ep_queue --> %d\n", value);
  1274. req->status = 0;
  1275. eth_setup_complete (gadget->ep0, req);
  1276. }
  1277. }
  1278. /* host either stalls (value < 0) or reports success */
  1279. return value;
  1280. }
  1281. static void
  1282. eth_disconnect (struct usb_gadget *gadget)
  1283. {
  1284. struct eth_dev *dev = get_gadget_data (gadget);
  1285. unsigned long flags;
  1286. spin_lock_irqsave (&dev->lock, flags);
  1287. netif_stop_queue (dev->net);
  1288. netif_carrier_off (dev->net);
  1289. eth_reset_config (dev);
  1290. spin_unlock_irqrestore (&dev->lock, flags);
  1291. /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
  1292. /* next we may get setup() calls to enumerate new connections;
  1293. * or an unbind() during shutdown (including removing module).
  1294. */
  1295. }
  1296. /*-------------------------------------------------------------------------*/
  1297. /* NETWORK DRIVER HOOKUP (to the layer above this driver) */
  1298. static int eth_change_mtu (struct net_device *net, int new_mtu)
  1299. {
  1300. struct eth_dev *dev = netdev_priv(net);
  1301. if (dev->rndis)
  1302. return -EBUSY;
  1303. if (new_mtu <= ETH_HLEN || new_mtu > ETH_FRAME_LEN)
  1304. return -ERANGE;
  1305. /* no zero-length packet read wanted after mtu-sized packets */
  1306. if (((new_mtu + sizeof (struct ethhdr)) % dev->in_ep->maxpacket) == 0)
  1307. return -EDOM;
  1308. net->mtu = new_mtu;
  1309. return 0;
  1310. }
  1311. static struct net_device_stats *eth_get_stats (struct net_device *net)
  1312. {
  1313. return &((struct eth_dev *)netdev_priv(net))->stats;
  1314. }
  1315. static void eth_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *p)
  1316. {
  1317. struct eth_dev *dev = netdev_priv(net);
  1318. strlcpy(p->driver, shortname, sizeof p->driver);
  1319. strlcpy(p->version, DRIVER_VERSION, sizeof p->version);
  1320. strlcpy(p->fw_version, dev->gadget->name, sizeof p->fw_version);
  1321. strlcpy (p->bus_info, dev->gadget->dev.bus_id, sizeof p->bus_info);
  1322. }
  1323. static u32 eth_get_link(struct net_device *net)
  1324. {
  1325. struct eth_dev *dev = netdev_priv(net);
  1326. return dev->gadget->speed != USB_SPEED_UNKNOWN;
  1327. }
  1328. static struct ethtool_ops ops = {
  1329. .get_drvinfo = eth_get_drvinfo,
  1330. .get_link = eth_get_link
  1331. };
  1332. static void defer_kevent (struct eth_dev *dev, int flag)
  1333. {
  1334. if (test_and_set_bit (flag, &dev->todo))
  1335. return;
  1336. if (!schedule_work (&dev->work))
  1337. ERROR (dev, "kevent %d may have been dropped\n", flag);
  1338. else
  1339. DEBUG (dev, "kevent %d scheduled\n", flag);
  1340. }
  1341. static void rx_complete (struct usb_ep *ep, struct usb_request *req);
  1342. static int
  1343. rx_submit (struct eth_dev *dev, struct usb_request *req, gfp_t gfp_flags)
  1344. {
  1345. struct sk_buff *skb;
  1346. int retval = -ENOMEM;
  1347. size_t size;
  1348. /* Padding up to RX_EXTRA handles minor disagreements with host.
  1349. * Normally we use the USB "terminate on short read" convention;
  1350. * so allow up to (N*maxpacket), since that memory is normally
  1351. * already allocated. Some hardware doesn't deal well with short
  1352. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  1353. * byte off the end (to force hardware errors on overflow).
  1354. *
  1355. * RNDIS uses internal framing, and explicitly allows senders to
  1356. * pad to end-of-packet. That's potentially nice for speed,
  1357. * but means receivers can't recover synch on their own.
  1358. */
  1359. size = (sizeof (struct ethhdr) + dev->net->mtu + RX_EXTRA);
  1360. size += dev->out_ep->maxpacket - 1;
  1361. if (rndis_active(dev))
  1362. size += sizeof (struct rndis_packet_msg_type);
  1363. size -= size % dev->out_ep->maxpacket;
  1364. if ((skb = alloc_skb (size + NET_IP_ALIGN, gfp_flags)) == 0) {
  1365. DEBUG (dev, "no rx skb\n");
  1366. goto enomem;
  1367. }
  1368. /* Some platforms perform better when IP packets are aligned,
  1369. * but on at least one, checksumming fails otherwise. Note:
  1370. * RNDIS headers involve variable numbers of LE32 values.
  1371. */
  1372. skb_reserve(skb, NET_IP_ALIGN);
  1373. req->buf = skb->data;
  1374. req->length = size;
  1375. req->complete = rx_complete;
  1376. req->context = skb;
  1377. retval = usb_ep_queue (dev->out_ep, req, gfp_flags);
  1378. if (retval == -ENOMEM)
  1379. enomem:
  1380. defer_kevent (dev, WORK_RX_MEMORY);
  1381. if (retval) {
  1382. DEBUG (dev, "rx submit --> %d\n", retval);
  1383. dev_kfree_skb_any (skb);
  1384. spin_lock (&dev->lock);
  1385. list_add (&req->list, &dev->rx_reqs);
  1386. spin_unlock (&dev->lock);
  1387. }
  1388. return retval;
  1389. }
  1390. static void rx_complete (struct usb_ep *ep, struct usb_request *req)
  1391. {
  1392. struct sk_buff *skb = req->context;
  1393. struct eth_dev *dev = ep->driver_data;
  1394. int status = req->status;
  1395. switch (status) {
  1396. /* normal completion */
  1397. case 0:
  1398. skb_put (skb, req->actual);
  1399. /* we know MaxPacketsPerTransfer == 1 here */
  1400. if (rndis_active(dev))
  1401. status = rndis_rm_hdr (skb);
  1402. if (status < 0
  1403. || ETH_HLEN > skb->len
  1404. || skb->len > ETH_FRAME_LEN) {
  1405. dev->stats.rx_errors++;
  1406. dev->stats.rx_length_errors++;
  1407. DEBUG (dev, "rx length %d\n", skb->len);
  1408. break;
  1409. }
  1410. skb->dev = dev->net;
  1411. skb->protocol = eth_type_trans (skb, dev->net);
  1412. dev->stats.rx_packets++;
  1413. dev->stats.rx_bytes += skb->len;
  1414. /* no buffer copies needed, unless hardware can't
  1415. * use skb buffers.
  1416. */
  1417. status = netif_rx (skb);
  1418. skb = NULL;
  1419. break;
  1420. /* software-driven interface shutdown */
  1421. case -ECONNRESET: // unlink
  1422. case -ESHUTDOWN: // disconnect etc
  1423. VDEBUG (dev, "rx shutdown, code %d\n", status);
  1424. goto quiesce;
  1425. /* for hardware automagic (such as pxa) */
  1426. case -ECONNABORTED: // endpoint reset
  1427. DEBUG (dev, "rx %s reset\n", ep->name);
  1428. defer_kevent (dev, WORK_RX_MEMORY);
  1429. quiesce:
  1430. dev_kfree_skb_any (skb);
  1431. goto clean;
  1432. /* data overrun */
  1433. case -EOVERFLOW:
  1434. dev->stats.rx_over_errors++;
  1435. // FALLTHROUGH
  1436. default:
  1437. dev->stats.rx_errors++;
  1438. DEBUG (dev, "rx status %d\n", status);
  1439. break;
  1440. }
  1441. if (skb)
  1442. dev_kfree_skb_any (skb);
  1443. if (!netif_running (dev->net)) {
  1444. clean:
  1445. /* nobody reading rx_reqs, so no dev->lock */
  1446. list_add (&req->list, &dev->rx_reqs);
  1447. req = NULL;
  1448. }
  1449. if (req)
  1450. rx_submit (dev, req, GFP_ATOMIC);
  1451. }
  1452. static int prealloc (struct list_head *list, struct usb_ep *ep,
  1453. unsigned n, gfp_t gfp_flags)
  1454. {
  1455. unsigned i;
  1456. struct usb_request *req;
  1457. if (!n)
  1458. return -ENOMEM;
  1459. /* queue/recycle up to N requests */
  1460. i = n;
  1461. list_for_each_entry (req, list, list) {
  1462. if (i-- == 0)
  1463. goto extra;
  1464. }
  1465. while (i--) {
  1466. req = usb_ep_alloc_request (ep, gfp_flags);
  1467. if (!req)
  1468. return list_empty (list) ? -ENOMEM : 0;
  1469. list_add (&req->list, list);
  1470. }
  1471. return 0;
  1472. extra:
  1473. /* free extras */
  1474. for (;;) {
  1475. struct list_head *next;
  1476. next = req->list.next;
  1477. list_del (&req->list);
  1478. usb_ep_free_request (ep, req);
  1479. if (next == list)
  1480. break;
  1481. req = container_of (next, struct usb_request, list);
  1482. }
  1483. return 0;
  1484. }
  1485. static int alloc_requests (struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
  1486. {
  1487. int status;
  1488. status = prealloc (&dev->tx_reqs, dev->in_ep, n, gfp_flags);
  1489. if (status < 0)
  1490. goto fail;
  1491. status = prealloc (&dev->rx_reqs, dev->out_ep, n, gfp_flags);
  1492. if (status < 0)
  1493. goto fail;
  1494. return 0;
  1495. fail:
  1496. DEBUG (dev, "can't alloc requests\n");
  1497. return status;
  1498. }
  1499. static void rx_fill (struct eth_dev *dev, gfp_t gfp_flags)
  1500. {
  1501. struct usb_request *req;
  1502. unsigned long flags;
  1503. /* fill unused rxq slots with some skb */
  1504. spin_lock_irqsave (&dev->lock, flags);
  1505. while (!list_empty (&dev->rx_reqs)) {
  1506. req = container_of (dev->rx_reqs.next,
  1507. struct usb_request, list);
  1508. list_del_init (&req->list);
  1509. spin_unlock_irqrestore (&dev->lock, flags);
  1510. if (rx_submit (dev, req, gfp_flags) < 0) {
  1511. defer_kevent (dev, WORK_RX_MEMORY);
  1512. return;
  1513. }
  1514. spin_lock_irqsave (&dev->lock, flags);
  1515. }
  1516. spin_unlock_irqrestore (&dev->lock, flags);
  1517. }
  1518. static void eth_work (void *_dev)
  1519. {
  1520. struct eth_dev *dev = _dev;
  1521. if (test_and_clear_bit (WORK_RX_MEMORY, &dev->todo)) {
  1522. if (netif_running (dev->net))
  1523. rx_fill (dev, GFP_KERNEL);
  1524. }
  1525. if (dev->todo)
  1526. DEBUG (dev, "work done, flags = 0x%lx\n", dev->todo);
  1527. }
  1528. static void tx_complete (struct usb_ep *ep, struct usb_request *req)
  1529. {
  1530. struct sk_buff *skb = req->context;
  1531. struct eth_dev *dev = ep->driver_data;
  1532. switch (req->status) {
  1533. default:
  1534. dev->stats.tx_errors++;
  1535. VDEBUG (dev, "tx err %d\n", req->status);
  1536. /* FALLTHROUGH */
  1537. case -ECONNRESET: // unlink
  1538. case -ESHUTDOWN: // disconnect etc
  1539. break;
  1540. case 0:
  1541. dev->stats.tx_bytes += skb->len;
  1542. }
  1543. dev->stats.tx_packets++;
  1544. spin_lock (&dev->lock);
  1545. list_add (&req->list, &dev->tx_reqs);
  1546. spin_unlock (&dev->lock);
  1547. dev_kfree_skb_any (skb);
  1548. atomic_dec (&dev->tx_qlen);
  1549. if (netif_carrier_ok (dev->net))
  1550. netif_wake_queue (dev->net);
  1551. }
  1552. static inline int eth_is_promisc (struct eth_dev *dev)
  1553. {
  1554. /* no filters for the CDC subset; always promisc */
  1555. if (subset_active (dev))
  1556. return 1;
  1557. return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  1558. }
  1559. static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
  1560. {
  1561. struct eth_dev *dev = netdev_priv(net);
  1562. int length = skb->len;
  1563. int retval;
  1564. struct usb_request *req = NULL;
  1565. unsigned long flags;
  1566. /* apply outgoing CDC or RNDIS filters */
  1567. if (!eth_is_promisc (dev)) {
  1568. u8 *dest = skb->data;
  1569. if (dest [0] & 0x01) {
  1570. u16 type;
  1571. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  1572. * SET_ETHERNET_MULTICAST_FILTERS requests
  1573. */
  1574. if (memcmp (dest, net->broadcast, ETH_ALEN) == 0)
  1575. type = USB_CDC_PACKET_TYPE_BROADCAST;
  1576. else
  1577. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  1578. if (!(dev->cdc_filter & type)) {
  1579. dev_kfree_skb_any (skb);
  1580. return 0;
  1581. }
  1582. }
  1583. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  1584. }
  1585. spin_lock_irqsave (&dev->lock, flags);
  1586. req = container_of (dev->tx_reqs.next, struct usb_request, list);
  1587. list_del (&req->list);
  1588. if (list_empty (&dev->tx_reqs))
  1589. netif_stop_queue (net);
  1590. spin_unlock_irqrestore (&dev->lock, flags);
  1591. /* no buffer copies needed, unless the network stack did it
  1592. * or the hardware can't use skb buffers.
  1593. * or there's not enough space for any RNDIS headers we need
  1594. */
  1595. if (rndis_active(dev)) {
  1596. struct sk_buff *skb_rndis;
  1597. skb_rndis = skb_realloc_headroom (skb,
  1598. sizeof (struct rndis_packet_msg_type));
  1599. if (!skb_rndis)
  1600. goto drop;
  1601. dev_kfree_skb_any (skb);
  1602. skb = skb_rndis;
  1603. rndis_add_hdr (skb);
  1604. length = skb->len;
  1605. }
  1606. req->buf = skb->data;
  1607. req->context = skb;
  1608. req->complete = tx_complete;
  1609. /* use zlp framing on tx for strict CDC-Ether conformance,
  1610. * though any robust network rx path ignores extra padding.
  1611. * and some hardware doesn't like to write zlps.
  1612. */
  1613. req->zero = 1;
  1614. if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
  1615. length++;
  1616. req->length = length;
  1617. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1618. /* throttle highspeed IRQ rate back slightly */
  1619. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
  1620. ? ((atomic_read (&dev->tx_qlen) % TX_DELAY) != 0)
  1621. : 0;
  1622. #endif
  1623. retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
  1624. switch (retval) {
  1625. default:
  1626. DEBUG (dev, "tx queue err %d\n", retval);
  1627. break;
  1628. case 0:
  1629. net->trans_start = jiffies;
  1630. atomic_inc (&dev->tx_qlen);
  1631. }
  1632. if (retval) {
  1633. drop:
  1634. dev->stats.tx_dropped++;
  1635. dev_kfree_skb_any (skb);
  1636. spin_lock_irqsave (&dev->lock, flags);
  1637. if (list_empty (&dev->tx_reqs))
  1638. netif_start_queue (net);
  1639. list_add (&req->list, &dev->tx_reqs);
  1640. spin_unlock_irqrestore (&dev->lock, flags);
  1641. }
  1642. return 0;
  1643. }
  1644. /*-------------------------------------------------------------------------*/
  1645. #ifdef CONFIG_USB_ETH_RNDIS
  1646. /* The interrupt endpoint is used in RNDIS to notify the host when messages
  1647. * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
  1648. * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
  1649. * REMOTE_NDIS_KEEPALIVE_MSG.
  1650. *
  1651. * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
  1652. * normally just one notification will be queued.
  1653. */
  1654. static struct usb_request *eth_req_alloc (struct usb_ep *, unsigned, gfp_t);
  1655. static void eth_req_free (struct usb_ep *ep, struct usb_request *req);
  1656. static void
  1657. rndis_control_ack_complete (struct usb_ep *ep, struct usb_request *req)
  1658. {
  1659. struct eth_dev *dev = ep->driver_data;
  1660. if (req->status || req->actual != req->length)
  1661. DEBUG (dev,
  1662. "rndis control ack complete --> %d, %d/%d\n",
  1663. req->status, req->actual, req->length);
  1664. req->context = NULL;
  1665. if (req != dev->stat_req)
  1666. eth_req_free(ep, req);
  1667. }
  1668. static int rndis_control_ack (struct net_device *net)
  1669. {
  1670. struct eth_dev *dev = netdev_priv(net);
  1671. u32 length;
  1672. struct usb_request *resp = dev->stat_req;
  1673. /* in case RNDIS calls this after disconnect */
  1674. if (!dev->status) {
  1675. DEBUG (dev, "status ENODEV\n");
  1676. return -ENODEV;
  1677. }
  1678. /* in case queue length > 1 */
  1679. if (resp->context) {
  1680. resp = eth_req_alloc (dev->status_ep, 8, GFP_ATOMIC);
  1681. if (!resp)
  1682. return -ENOMEM;
  1683. }
  1684. /* Send RNDIS RESPONSE_AVAILABLE notification;
  1685. * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
  1686. */
  1687. resp->length = 8;
  1688. resp->complete = rndis_control_ack_complete;
  1689. resp->context = dev;
  1690. *((__le32 *) resp->buf) = __constant_cpu_to_le32 (1);
  1691. *((__le32 *) resp->buf + 1) = __constant_cpu_to_le32 (0);
  1692. length = usb_ep_queue (dev->status_ep, resp, GFP_ATOMIC);
  1693. if (length < 0) {
  1694. resp->status = 0;
  1695. rndis_control_ack_complete (dev->status_ep, resp);
  1696. }
  1697. return 0;
  1698. }
  1699. #else
  1700. #define rndis_control_ack NULL
  1701. #endif /* RNDIS */
  1702. static void eth_start (struct eth_dev *dev, gfp_t gfp_flags)
  1703. {
  1704. DEBUG (dev, "%s\n", __FUNCTION__);
  1705. /* fill the rx queue */
  1706. rx_fill (dev, gfp_flags);
  1707. /* and open the tx floodgates */
  1708. atomic_set (&dev->tx_qlen, 0);
  1709. netif_wake_queue (dev->net);
  1710. if (rndis_active(dev)) {
  1711. rndis_set_param_medium (dev->rndis_config,
  1712. NDIS_MEDIUM_802_3,
  1713. BITRATE(dev->gadget)/100);
  1714. (void) rndis_signal_connect (dev->rndis_config);
  1715. }
  1716. }
  1717. static int eth_open (struct net_device *net)
  1718. {
  1719. struct eth_dev *dev = netdev_priv(net);
  1720. DEBUG (dev, "%s\n", __FUNCTION__);
  1721. if (netif_carrier_ok (dev->net))
  1722. eth_start (dev, GFP_KERNEL);
  1723. return 0;
  1724. }
  1725. static int eth_stop (struct net_device *net)
  1726. {
  1727. struct eth_dev *dev = netdev_priv(net);
  1728. VDEBUG (dev, "%s\n", __FUNCTION__);
  1729. netif_stop_queue (net);
  1730. DEBUG (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld\n",
  1731. dev->stats.rx_packets, dev->stats.tx_packets,
  1732. dev->stats.rx_errors, dev->stats.tx_errors
  1733. );
  1734. /* ensure there are no more active requests */
  1735. if (dev->config) {
  1736. usb_ep_disable (dev->in_ep);
  1737. usb_ep_disable (dev->out_ep);
  1738. if (netif_carrier_ok (dev->net)) {
  1739. DEBUG (dev, "host still using in/out endpoints\n");
  1740. // FIXME idiom may leave toggle wrong here
  1741. usb_ep_enable (dev->in_ep, dev->in);
  1742. usb_ep_enable (dev->out_ep, dev->out);
  1743. }
  1744. if (dev->status_ep) {
  1745. usb_ep_disable (dev->status_ep);
  1746. usb_ep_enable (dev->status_ep, dev->status);
  1747. }
  1748. }
  1749. if (rndis_active(dev)) {
  1750. rndis_set_param_medium (dev->rndis_config,
  1751. NDIS_MEDIUM_802_3, 0);
  1752. (void) rndis_signal_disconnect (dev->rndis_config);
  1753. }
  1754. return 0;
  1755. }
  1756. /*-------------------------------------------------------------------------*/
  1757. static struct usb_request *
  1758. eth_req_alloc (struct usb_ep *ep, unsigned size, gfp_t gfp_flags)
  1759. {
  1760. struct usb_request *req;
  1761. req = usb_ep_alloc_request (ep, gfp_flags);
  1762. if (!req)
  1763. return NULL;
  1764. req->buf = kmalloc (size, gfp_flags);
  1765. if (!req->buf) {
  1766. usb_ep_free_request (ep, req);
  1767. req = NULL;
  1768. }
  1769. return req;
  1770. }
  1771. static void
  1772. eth_req_free (struct usb_ep *ep, struct usb_request *req)
  1773. {
  1774. kfree (req->buf);
  1775. usb_ep_free_request (ep, req);
  1776. }
  1777. static void __exit
  1778. eth_unbind (struct usb_gadget *gadget)
  1779. {
  1780. struct eth_dev *dev = get_gadget_data (gadget);
  1781. DEBUG (dev, "unbind\n");
  1782. rndis_deregister (dev->rndis_config);
  1783. rndis_exit ();
  1784. /* we've already been disconnected ... no i/o is active */
  1785. if (dev->req) {
  1786. eth_req_free (gadget->ep0, dev->req);
  1787. dev->req = NULL;
  1788. }
  1789. if (dev->stat_req) {
  1790. eth_req_free (dev->status_ep, dev->stat_req);
  1791. dev->stat_req = NULL;
  1792. }
  1793. unregister_netdev (dev->net);
  1794. free_netdev(dev->net);
  1795. /* assuming we used keventd, it must quiesce too */
  1796. flush_scheduled_work ();
  1797. set_gadget_data (gadget, NULL);
  1798. }
  1799. static u8 __init nibble (unsigned char c)
  1800. {
  1801. if (likely (isdigit (c)))
  1802. return c - '0';
  1803. c = toupper (c);
  1804. if (likely (isxdigit (c)))
  1805. return 10 + c - 'A';
  1806. return 0;
  1807. }
  1808. static void __init get_ether_addr (const char *str, u8 *dev_addr)
  1809. {
  1810. if (str) {
  1811. unsigned i;
  1812. for (i = 0; i < 6; i++) {
  1813. unsigned char num;
  1814. if((*str == '.') || (*str == ':'))
  1815. str++;
  1816. num = nibble(*str++) << 4;
  1817. num |= (nibble(*str++));
  1818. dev_addr [i] = num;
  1819. }
  1820. if (is_valid_ether_addr (dev_addr))
  1821. return;
  1822. }
  1823. random_ether_addr(dev_addr);
  1824. }
  1825. static int __init
  1826. eth_bind (struct usb_gadget *gadget)
  1827. {
  1828. struct eth_dev *dev;
  1829. struct net_device *net;
  1830. u8 cdc = 1, zlp = 1, rndis = 1;
  1831. struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
  1832. int status = -ENOMEM;
  1833. int gcnum;
  1834. /* these flags are only ever cleared; compiler take note */
  1835. #ifndef DEV_CONFIG_CDC
  1836. cdc = 0;
  1837. #endif
  1838. #ifndef CONFIG_USB_ETH_RNDIS
  1839. rndis = 0;
  1840. #endif
  1841. /* Because most host side USB stacks handle CDC Ethernet, that
  1842. * standard protocol is _strongly_ preferred for interop purposes.
  1843. * (By everyone except Microsoft.)
  1844. */
  1845. if (gadget_is_pxa (gadget)) {
  1846. /* pxa doesn't support altsettings */
  1847. cdc = 0;
  1848. } else if (gadget_is_sh(gadget)) {
  1849. /* sh doesn't support multiple interfaces or configs */
  1850. cdc = 0;
  1851. rndis = 0;
  1852. } else if (gadget_is_sa1100 (gadget)) {
  1853. /* hardware can't write zlps */
  1854. zlp = 0;
  1855. /* sa1100 CAN do CDC, without status endpoint ... we use
  1856. * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
  1857. */
  1858. cdc = 0;
  1859. }
  1860. gcnum = usb_gadget_controller_number (gadget);
  1861. if (gcnum >= 0)
  1862. device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
  1863. else {
  1864. /* can't assume CDC works. don't want to default to
  1865. * anything less functional on CDC-capable hardware,
  1866. * so we fail in this case.
  1867. */
  1868. dev_err (&gadget->dev,
  1869. "controller '%s' not recognized\n",
  1870. gadget->name);
  1871. return -ENODEV;
  1872. }
  1873. snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
  1874. system_utsname.sysname, system_utsname.release,
  1875. gadget->name);
  1876. /* If there's an RNDIS configuration, that's what Windows wants to
  1877. * be using ... so use these product IDs here and in the "linux.inf"
  1878. * needed to install MSFT drivers. Current Linux kernels will use
  1879. * the second configuration if it's CDC Ethernet, and need some help
  1880. * to choose the right configuration otherwise.
  1881. */
  1882. if (rndis) {
  1883. device_desc.idVendor =
  1884. __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
  1885. device_desc.idProduct =
  1886. __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
  1887. snprintf (product_desc, sizeof product_desc,
  1888. "RNDIS/%s", driver_desc);
  1889. /* CDC subset ... recognized by Linux since 2.4.10, but Windows
  1890. * drivers aren't widely available.
  1891. */
  1892. } else if (!cdc) {
  1893. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  1894. device_desc.idVendor =
  1895. __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
  1896. device_desc.idProduct =
  1897. __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
  1898. }
  1899. /* support optional vendor/distro customization */
  1900. if (idVendor) {
  1901. if (!idProduct) {
  1902. dev_err (&gadget->dev, "idVendor needs idProduct!\n");
  1903. return -ENODEV;
  1904. }
  1905. device_desc.idVendor = cpu_to_le16(idVendor);
  1906. device_desc.idProduct = cpu_to_le16(idProduct);
  1907. if (bcdDevice)
  1908. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1909. }
  1910. if (iManufacturer)
  1911. strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
  1912. if (iProduct)
  1913. strlcpy (product_desc, iProduct, sizeof product_desc);
  1914. /* all we really need is bulk IN/OUT */
  1915. usb_ep_autoconfig_reset (gadget);
  1916. in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
  1917. if (!in_ep) {
  1918. autoconf_fail:
  1919. dev_err (&gadget->dev,
  1920. "can't autoconfigure on %s\n",
  1921. gadget->name);
  1922. return -ENODEV;
  1923. }
  1924. in_ep->driver_data = in_ep; /* claim */
  1925. out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
  1926. if (!out_ep)
  1927. goto autoconf_fail;
  1928. out_ep->driver_data = out_ep; /* claim */
  1929. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1930. /* CDC Ethernet control interface doesn't require a status endpoint.
  1931. * Since some hosts expect one, try to allocate one anyway.
  1932. */
  1933. if (cdc || rndis) {
  1934. status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
  1935. if (status_ep) {
  1936. status_ep->driver_data = status_ep; /* claim */
  1937. } else if (rndis) {
  1938. dev_err (&gadget->dev,
  1939. "can't run RNDIS on %s\n",
  1940. gadget->name);
  1941. return -ENODEV;
  1942. #ifdef DEV_CONFIG_CDC
  1943. /* pxa25x only does CDC subset; often used with RNDIS */
  1944. } else if (cdc) {
  1945. control_intf.bNumEndpoints = 0;
  1946. /* FIXME remove endpoint from descriptor list */
  1947. #endif
  1948. }
  1949. }
  1950. #endif
  1951. /* one config: cdc, else minimal subset */
  1952. if (!cdc) {
  1953. eth_config.bNumInterfaces = 1;
  1954. eth_config.iConfiguration = STRING_SUBSET;
  1955. fs_subset_descriptors();
  1956. hs_subset_descriptors();
  1957. }
  1958. /* For now RNDIS is always a second config */
  1959. if (rndis)
  1960. device_desc.bNumConfigurations = 2;
  1961. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1962. if (rndis)
  1963. dev_qualifier.bNumConfigurations = 2;
  1964. else if (!cdc)
  1965. dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  1966. /* assumes ep0 uses the same value for both speeds ... */
  1967. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1968. /* and that all endpoints are dual-speed */
  1969. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  1970. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  1971. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1972. if (status_ep)
  1973. hs_status_desc.bEndpointAddress =
  1974. fs_status_desc.bEndpointAddress;
  1975. #endif
  1976. #endif /* DUALSPEED */
  1977. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1978. usb_gadget_set_selfpowered (gadget);
  1979. if (gadget->is_otg) {
  1980. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1981. eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1982. eth_config.bMaxPower = 4;
  1983. #ifdef CONFIG_USB_ETH_RNDIS
  1984. rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1985. rndis_config.bMaxPower = 4;
  1986. #endif
  1987. }
  1988. net = alloc_etherdev (sizeof *dev);
  1989. if (!net)
  1990. return status;
  1991. dev = netdev_priv(net);
  1992. spin_lock_init (&dev->lock);
  1993. INIT_WORK (&dev->work, eth_work, dev);
  1994. INIT_LIST_HEAD (&dev->tx_reqs);
  1995. INIT_LIST_HEAD (&dev->rx_reqs);
  1996. /* network device setup */
  1997. dev->net = net;
  1998. SET_MODULE_OWNER (net);
  1999. strcpy (net->name, "usb%d");
  2000. dev->cdc = cdc;
  2001. dev->zlp = zlp;
  2002. dev->in_ep = in_ep;
  2003. dev->out_ep = out_ep;
  2004. dev->status_ep = status_ep;
  2005. /* Module params for these addresses should come from ID proms.
  2006. * The host side address is used with CDC and RNDIS, and commonly
  2007. * ends up in a persistent config database.
  2008. */
  2009. get_ether_addr(dev_addr, net->dev_addr);
  2010. if (cdc || rndis) {
  2011. get_ether_addr(host_addr, dev->host_mac);
  2012. #ifdef DEV_CONFIG_CDC
  2013. snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
  2014. dev->host_mac [0], dev->host_mac [1],
  2015. dev->host_mac [2], dev->host_mac [3],
  2016. dev->host_mac [4], dev->host_mac [5]);
  2017. #endif
  2018. }
  2019. if (rndis) {
  2020. status = rndis_init();
  2021. if (status < 0) {
  2022. dev_err (&gadget->dev, "can't init RNDIS, %d\n",
  2023. status);
  2024. goto fail;
  2025. }
  2026. }
  2027. net->change_mtu = eth_change_mtu;
  2028. net->get_stats = eth_get_stats;
  2029. net->hard_start_xmit = eth_start_xmit;
  2030. net->open = eth_open;
  2031. net->stop = eth_stop;
  2032. // watchdog_timeo, tx_timeout ...
  2033. // set_multicast_list
  2034. SET_ETHTOOL_OPS(net, &ops);
  2035. /* preallocate control message data and buffer */
  2036. dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
  2037. if (!dev->req)
  2038. goto fail;
  2039. dev->req->complete = eth_setup_complete;
  2040. /* ... and maybe likewise for status transfer */
  2041. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  2042. if (dev->status_ep) {
  2043. dev->stat_req = eth_req_alloc (dev->status_ep,
  2044. STATUS_BYTECOUNT, GFP_KERNEL);
  2045. if (!dev->stat_req) {
  2046. eth_req_free (gadget->ep0, dev->req);
  2047. goto fail;
  2048. }
  2049. dev->stat_req->context = NULL;
  2050. }
  2051. #endif
  2052. /* finish hookup to lower layer ... */
  2053. dev->gadget = gadget;
  2054. set_gadget_data (gadget, dev);
  2055. gadget->ep0->driver_data = dev;
  2056. /* two kinds of host-initiated state changes:
  2057. * - iff DATA transfer is active, carrier is "on"
  2058. * - tx queueing enabled if open *and* carrier is "on"
  2059. */
  2060. netif_stop_queue (dev->net);
  2061. netif_carrier_off (dev->net);
  2062. SET_NETDEV_DEV (dev->net, &gadget->dev);
  2063. status = register_netdev (dev->net);
  2064. if (status < 0)
  2065. goto fail1;
  2066. INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
  2067. INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
  2068. out_ep->name, in_ep->name,
  2069. status_ep ? " STATUS " : "",
  2070. status_ep ? status_ep->name : ""
  2071. );
  2072. INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  2073. net->dev_addr [0], net->dev_addr [1],
  2074. net->dev_addr [2], net->dev_addr [3],
  2075. net->dev_addr [4], net->dev_addr [5]);
  2076. if (cdc || rndis)
  2077. INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  2078. dev->host_mac [0], dev->host_mac [1],
  2079. dev->host_mac [2], dev->host_mac [3],
  2080. dev->host_mac [4], dev->host_mac [5]);
  2081. if (rndis) {
  2082. u32 vendorID = 0;
  2083. /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
  2084. dev->rndis_config = rndis_register (rndis_control_ack);
  2085. if (dev->rndis_config < 0) {
  2086. fail0:
  2087. unregister_netdev (dev->net);
  2088. status = -ENODEV;
  2089. goto fail;
  2090. }
  2091. /* these set up a lot of the OIDs that RNDIS needs */
  2092. rndis_set_host_mac (dev->rndis_config, dev->host_mac);
  2093. if (rndis_set_param_dev (dev->rndis_config, dev->net,
  2094. &dev->stats, &dev->cdc_filter))
  2095. goto fail0;
  2096. if (rndis_set_param_vendor (dev->rndis_config, vendorID,
  2097. manufacturer))
  2098. goto fail0;
  2099. if (rndis_set_param_medium (dev->rndis_config,
  2100. NDIS_MEDIUM_802_3,
  2101. 0))
  2102. goto fail0;
  2103. INFO (dev, "RNDIS ready\n");
  2104. }
  2105. return status;
  2106. fail1:
  2107. dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
  2108. fail:
  2109. eth_unbind (gadget);
  2110. return status;
  2111. }
  2112. /*-------------------------------------------------------------------------*/
  2113. static void
  2114. eth_suspend (struct usb_gadget *gadget)
  2115. {
  2116. struct eth_dev *dev = get_gadget_data (gadget);
  2117. DEBUG (dev, "suspend\n");
  2118. dev->suspended = 1;
  2119. }
  2120. static void
  2121. eth_resume (struct usb_gadget *gadget)
  2122. {
  2123. struct eth_dev *dev = get_gadget_data (gadget);
  2124. DEBUG (dev, "resume\n");
  2125. dev->suspended = 0;
  2126. }
  2127. /*-------------------------------------------------------------------------*/
  2128. static struct usb_gadget_driver eth_driver = {
  2129. .speed = DEVSPEED,
  2130. .function = (char *) driver_desc,
  2131. .bind = eth_bind,
  2132. .unbind = __exit_p(eth_unbind),
  2133. .setup = eth_setup,
  2134. .disconnect = eth_disconnect,
  2135. .suspend = eth_suspend,
  2136. .resume = eth_resume,
  2137. .driver = {
  2138. .name = (char *) shortname,
  2139. .owner = THIS_MODULE,
  2140. },
  2141. };
  2142. MODULE_DESCRIPTION (DRIVER_DESC);
  2143. MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
  2144. MODULE_LICENSE ("GPL");
  2145. static int __init init (void)
  2146. {
  2147. return usb_gadget_register_driver (&eth_driver);
  2148. }
  2149. module_init (init);
  2150. static void __exit cleanup (void)
  2151. {
  2152. usb_gadget_unregister_driver (&eth_driver);
  2153. }
  2154. module_exit (cleanup);