ether.c 72 KB

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