ether.c 68 KB

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