ether.c 71 KB

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