ether.c 72 KB

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