ether.c 72 KB

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