ether.c 69 KB

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