ether.c 68 KB

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