ether.c 68 KB

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