ether.c 71 KB

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