ether.c 70 KB

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