ether.c 72 KB

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