ether.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973
  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/usb/ch9.h>
  25. #include <linux/usb/cdc.h>
  26. #include <linux/usb/gadget.h>
  27. #include <net.h>
  28. #include <linux/ctype.h>
  29. #include "gadget_chips.h"
  30. #define USB_NET_NAME "usb_ether"
  31. #define atomic_read
  32. extern struct platform_data brd;
  33. #define spin_lock(x)
  34. #define spin_unlock(x)
  35. unsigned packet_received, packet_sent;
  36. #define DEV_CONFIG_CDC 1
  37. #define GFP_ATOMIC ((gfp_t) 0)
  38. #define GFP_KERNEL ((gfp_t) 0)
  39. /*
  40. * Ethernet gadget driver -- with CDC and non-CDC options
  41. * Builds on hardware support for a full duplex link.
  42. *
  43. * CDC Ethernet is the standard USB solution for sending Ethernet frames
  44. * using USB. Real hardware tends to use the same framing protocol but look
  45. * different for control features. This driver strongly prefers to use
  46. * this USB-IF standard as its open-systems interoperability solution;
  47. * most host side USB stacks (except from Microsoft) support it.
  48. *
  49. * This is sometimes called "CDC ECM" (Ethernet Control Model) to support
  50. * TLA-soup. "CDC ACM" (Abstract Control Model) is for modems, and a new
  51. * "CDC EEM" (Ethernet Emulation Model) is starting to spread.
  52. *
  53. * There's some hardware that can't talk CDC ECM. We make that hardware
  54. * implement a "minimalist" vendor-agnostic CDC core: same framing, but
  55. * link-level setup only requires activating the configuration. Only the
  56. * endpoint descriptors, and product/vendor IDs, are relevant; no control
  57. * operations are available. Linux supports it, but other host operating
  58. * systems may not. (This is a subset of CDC Ethernet.)
  59. *
  60. * It turns out that if you add a few descriptors to that "CDC Subset",
  61. * (Windows) host side drivers from MCCI can treat it as one submode of
  62. * a proprietary scheme called "SAFE" ... without needing to know about
  63. * specific product/vendor IDs. So we do that, making it easier to use
  64. * those MS-Windows drivers. Those added descriptors make it resemble a
  65. * CDC MDLM device, but they don't change device behavior at all. (See
  66. * MCCI Engineering report 950198 "SAFE Networking Functions".)
  67. *
  68. * A third option is also in use. Rather than CDC Ethernet, or something
  69. * simpler, Microsoft pushes their own approach: RNDIS. The published
  70. * RNDIS specs are ambiguous and appear to be incomplete, and are also
  71. * needlessly complex. They borrow more from CDC ACM than CDC ECM.
  72. */
  73. #define ETH_ALEN 6 /* Octets in one ethernet addr */
  74. #define ETH_HLEN 14 /* Total octets in header. */
  75. #define ETH_ZLEN 60 /* Min. octets in frame sans FCS */
  76. #define ETH_DATA_LEN 1500 /* Max. octets in payload */
  77. #define ETH_FRAME_LEN PKTSIZE_ALIGN /* Max. octets in frame sans FCS */
  78. #define ETH_FCS_LEN 4 /* Octets in the FCS */
  79. #define DRIVER_DESC "Ethernet Gadget"
  80. /* Based on linux 2.6.27 version */
  81. #define DRIVER_VERSION "May Day 2005"
  82. static const char shortname[] = "ether";
  83. static const char driver_desc[] = DRIVER_DESC;
  84. #define RX_EXTRA 20 /* guard against rx overflows */
  85. /* CDC support the same host-chosen outgoing packet filters. */
  86. #define DEFAULT_FILTER (USB_CDC_PACKET_TYPE_BROADCAST \
  87. |USB_CDC_PACKET_TYPE_ALL_MULTICAST \
  88. |USB_CDC_PACKET_TYPE_PROMISCUOUS \
  89. |USB_CDC_PACKET_TYPE_DIRECTED)
  90. #define USB_CONNECT_TIMEOUT (3 * CONFIG_SYS_HZ)
  91. /*-------------------------------------------------------------------------*/
  92. static struct eth_dev l_ethdev;
  93. static struct eth_device l_netdev;
  94. static struct usb_gadget_driver eth_driver;
  95. /*-------------------------------------------------------------------------*/
  96. /* "main" config is either CDC, or its simple subset */
  97. static inline int is_cdc(struct eth_dev *dev)
  98. {
  99. #if !defined(DEV_CONFIG_SUBSET)
  100. return 1; /* only cdc possible */
  101. #elif !defined(DEV_CONFIG_CDC)
  102. return 0; /* only subset possible */
  103. #else
  104. return dev->cdc; /* depends on what hardware we found */
  105. #endif
  106. }
  107. #define subset_active(dev) (!is_cdc(dev))
  108. #define cdc_active(dev) (is_cdc(dev))
  109. #define DEFAULT_QLEN 2 /* double buffering by default */
  110. /* peak bulk transfer bits-per-second */
  111. #define HS_BPS (13 * 512 * 8 * 1000 * 8)
  112. #define FS_BPS (19 * 64 * 1 * 1000 * 8)
  113. #ifdef CONFIG_USB_GADGET_DUALSPEED
  114. #define DEVSPEED USB_SPEED_HIGH
  115. #ifdef CONFIG_USB_ETH_QMULT
  116. #define qmult CONFIG_USB_ETH_QMULT
  117. #else
  118. #define qmult 5
  119. #endif
  120. /* for dual-speed hardware, use deeper queues at highspeed */
  121. #define qlen(gadget) \
  122. (DEFAULT_QLEN*((gadget->speed == USB_SPEED_HIGH) ? qmult : 1))
  123. static inline int BITRATE(struct usb_gadget *g)
  124. {
  125. return (g->speed == USB_SPEED_HIGH) ? HS_BPS : FS_BPS;
  126. }
  127. #else /* full speed (low speed doesn't do bulk) */
  128. #define qmult 1
  129. #define DEVSPEED USB_SPEED_FULL
  130. #define qlen(gadget) DEFAULT_QLEN
  131. static inline int BITRATE(struct usb_gadget *g)
  132. {
  133. return FS_BPS;
  134. }
  135. #endif
  136. struct eth_dev {
  137. struct usb_gadget *gadget;
  138. struct usb_request *req; /* for control responses */
  139. struct usb_request *stat_req; /* for cdc status */
  140. u8 config;
  141. struct usb_ep *in_ep, *out_ep, *status_ep;
  142. const struct usb_endpoint_descriptor
  143. *in, *out, *status;
  144. struct usb_request *tx_req, *rx_req;
  145. struct eth_device *net;
  146. unsigned int tx_qlen;
  147. unsigned zlp:1;
  148. unsigned cdc:1;
  149. unsigned suspended:1;
  150. unsigned network_started:1;
  151. u16 cdc_filter;
  152. unsigned long todo;
  153. int mtu;
  154. #define WORK_RX_MEMORY 0
  155. u8 host_mac[ETH_ALEN];
  156. };
  157. /*
  158. * This version autoconfigures as much as possible at run-time.
  159. *
  160. * It also ASSUMES a self-powered device, without remote wakeup,
  161. * although remote wakeup support would make sense.
  162. */
  163. /*-------------------------------------------------------------------------*/
  164. /*
  165. * DO NOT REUSE THESE IDs with a protocol-incompatible driver!! Ever!!
  166. * Instead: allocate your own, using normal USB-IF procedures.
  167. */
  168. /*
  169. * Thanks to NetChip Technologies for donating this product ID.
  170. * It's for devices with only CDC Ethernet configurations.
  171. */
  172. #define CDC_VENDOR_NUM 0x0525 /* NetChip */
  173. #define CDC_PRODUCT_NUM 0xa4a1 /* Linux-USB Ethernet Gadget */
  174. /*
  175. * For hardware that can't talk CDC, we use the same vendor ID that
  176. * ARM Linux has used for ethernet-over-usb, both with sa1100 and
  177. * with pxa250. We're protocol-compatible, if the host-side drivers
  178. * use the endpoint descriptors. bcdDevice (version) is nonzero, so
  179. * drivers that need to hard-wire endpoint numbers have a hook.
  180. *
  181. * The protocol is a minimal subset of CDC Ether, which works on any bulk
  182. * hardware that's not deeply broken ... even on hardware that can't talk
  183. * RNDIS (like SA-1100, with no interrupt endpoint, or anything that
  184. * doesn't handle control-OUT).
  185. */
  186. #define SIMPLE_VENDOR_NUM 0x049f
  187. #define SIMPLE_PRODUCT_NUM 0x505a
  188. /*
  189. * Some systems will want different product identifers published in the
  190. * device descriptor, either numbers or strings or both. These string
  191. * parameters are in UTF-8 (superset of ASCII's 7 bit characters).
  192. */
  193. static ushort bcdDevice;
  194. #if defined(CONFIG_USBNET_MANUFACTURER)
  195. static char *iManufacturer = CONFIG_USBNET_MANUFACTURER;
  196. #else
  197. static char *iManufacturer = "U-boot";
  198. #endif
  199. static char *iProduct;
  200. static char *iSerialNumber;
  201. static char dev_addr[18];
  202. static char host_addr[18];
  203. /*-------------------------------------------------------------------------*/
  204. /*
  205. * USB DRIVER HOOKUP (to the hardware driver, below us), mostly
  206. * ep0 implementation: descriptors, config management, setup().
  207. * also optional class-specific notification interrupt transfer.
  208. */
  209. /*
  210. * DESCRIPTORS ... most are static, but strings and (full) configuration
  211. * descriptors are built on demand. For now we do either full CDC, or
  212. * our simple subset.
  213. */
  214. #define STRING_MANUFACTURER 1
  215. #define STRING_PRODUCT 2
  216. #define STRING_ETHADDR 3
  217. #define STRING_DATA 4
  218. #define STRING_CONTROL 5
  219. #define STRING_CDC 7
  220. #define STRING_SUBSET 8
  221. #define STRING_SERIALNUMBER 10
  222. /* holds our biggest descriptor */
  223. #define USB_BUFSIZ 256
  224. /*
  225. * This device advertises one configuration, eth_config,
  226. * on hardware supporting at least two configs.
  227. *
  228. * FIXME define some higher-powered configurations to make it easier
  229. * to recharge batteries ...
  230. */
  231. #define DEV_CONFIG_VALUE 1 /* cdc or subset */
  232. static struct usb_device_descriptor
  233. device_desc = {
  234. .bLength = sizeof device_desc,
  235. .bDescriptorType = USB_DT_DEVICE,
  236. .bcdUSB = __constant_cpu_to_le16(0x0200),
  237. .bDeviceClass = USB_CLASS_COMM,
  238. .bDeviceSubClass = 0,
  239. .bDeviceProtocol = 0,
  240. .idVendor = __constant_cpu_to_le16(CDC_VENDOR_NUM),
  241. .idProduct = __constant_cpu_to_le16(CDC_PRODUCT_NUM),
  242. .iManufacturer = STRING_MANUFACTURER,
  243. .iProduct = STRING_PRODUCT,
  244. .bNumConfigurations = 1,
  245. };
  246. static struct usb_otg_descriptor
  247. otg_descriptor = {
  248. .bLength = sizeof otg_descriptor,
  249. .bDescriptorType = USB_DT_OTG,
  250. .bmAttributes = USB_OTG_SRP,
  251. };
  252. static struct usb_config_descriptor
  253. eth_config = {
  254. .bLength = sizeof eth_config,
  255. .bDescriptorType = USB_DT_CONFIG,
  256. /* compute wTotalLength on the fly */
  257. .bNumInterfaces = 2,
  258. .bConfigurationValue = DEV_CONFIG_VALUE,
  259. .iConfiguration = STRING_CDC,
  260. .bmAttributes = USB_CONFIG_ATT_ONE | USB_CONFIG_ATT_SELFPOWER,
  261. .bMaxPower = 1,
  262. };
  263. /*
  264. * Compared to the simple CDC subset, the full CDC Ethernet model adds
  265. * three class descriptors, two interface descriptors, optional status
  266. * endpoint. Both have a "data" interface and two bulk endpoints.
  267. * There are also differences in how control requests are handled.
  268. */
  269. #ifdef DEV_CONFIG_CDC
  270. static struct usb_interface_descriptor
  271. control_intf = {
  272. .bLength = sizeof control_intf,
  273. .bDescriptorType = USB_DT_INTERFACE,
  274. .bInterfaceNumber = 0,
  275. /* status endpoint is optional; this may be patched later */
  276. .bNumEndpoints = 1,
  277. .bInterfaceClass = USB_CLASS_COMM,
  278. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET,
  279. .bInterfaceProtocol = USB_CDC_PROTO_NONE,
  280. .iInterface = STRING_CONTROL,
  281. };
  282. #endif
  283. static const struct usb_cdc_header_desc header_desc = {
  284. .bLength = sizeof header_desc,
  285. .bDescriptorType = USB_DT_CS_INTERFACE,
  286. .bDescriptorSubType = USB_CDC_HEADER_TYPE,
  287. .bcdCDC = __constant_cpu_to_le16(0x0110),
  288. };
  289. #if defined(DEV_CONFIG_CDC)
  290. static const struct usb_cdc_union_desc union_desc = {
  291. .bLength = sizeof union_desc,
  292. .bDescriptorType = USB_DT_CS_INTERFACE,
  293. .bDescriptorSubType = USB_CDC_UNION_TYPE,
  294. .bMasterInterface0 = 0, /* index of control interface */
  295. .bSlaveInterface0 = 1, /* index of DATA interface */
  296. };
  297. #endif /* CDC */
  298. #ifndef DEV_CONFIG_CDC
  299. /*
  300. * "SAFE" loosely follows CDC WMC MDLM, violating the spec in various
  301. * ways: data endpoints live in the control interface, there's no data
  302. * interface, and it's not used to talk to a cell phone radio.
  303. */
  304. static const struct usb_cdc_mdlm_desc mdlm_desc = {
  305. .bLength = sizeof mdlm_desc,
  306. .bDescriptorType = USB_DT_CS_INTERFACE,
  307. .bDescriptorSubType = USB_CDC_MDLM_TYPE,
  308. .bcdVersion = __constant_cpu_to_le16(0x0100),
  309. .bGUID = {
  310. 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
  311. 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
  312. },
  313. };
  314. /*
  315. * since "usb_cdc_mdlm_detail_desc" is a variable length structure, we
  316. * can't really use its struct. All we do here is say that we're using
  317. * the submode of "SAFE" which directly matches the CDC Subset.
  318. */
  319. static const u8 mdlm_detail_desc[] = {
  320. 6,
  321. USB_DT_CS_INTERFACE,
  322. USB_CDC_MDLM_DETAIL_TYPE,
  323. 0, /* "SAFE" */
  324. 0, /* network control capabilities (none) */
  325. 0, /* network data capabilities ("raw" encapsulation) */
  326. };
  327. #endif
  328. static const struct usb_cdc_ether_desc ether_desc = {
  329. .bLength = sizeof(ether_desc),
  330. .bDescriptorType = USB_DT_CS_INTERFACE,
  331. .bDescriptorSubType = USB_CDC_ETHERNET_TYPE,
  332. /* this descriptor actually adds value, surprise! */
  333. .iMACAddress = STRING_ETHADDR,
  334. .bmEthernetStatistics = __constant_cpu_to_le32(0), /* no statistics */
  335. .wMaxSegmentSize = __constant_cpu_to_le16(ETH_FRAME_LEN),
  336. .wNumberMCFilters = __constant_cpu_to_le16(0),
  337. .bNumberPowerFilters = 0,
  338. };
  339. #if defined(DEV_CONFIG_CDC)
  340. /*
  341. * include the status endpoint if we can, even where it's optional.
  342. * use wMaxPacketSize big enough to fit CDC_NOTIFY_SPEED_CHANGE in one
  343. * packet, to simplify cancellation; and a big transfer interval, to
  344. * waste less bandwidth.
  345. *
  346. * some drivers (like Linux 2.4 cdc-ether!) "need" it to exist even
  347. * if they ignore the connect/disconnect notifications that real aether
  348. * can provide. more advanced cdc configurations might want to support
  349. * encapsulated commands (vendor-specific, using control-OUT).
  350. */
  351. #define LOG2_STATUS_INTERVAL_MSEC 5 /* 1 << 5 == 32 msec */
  352. #define STATUS_BYTECOUNT 16 /* 8 byte header + data */
  353. static struct usb_endpoint_descriptor
  354. fs_status_desc = {
  355. .bLength = USB_DT_ENDPOINT_SIZE,
  356. .bDescriptorType = USB_DT_ENDPOINT,
  357. .bEndpointAddress = USB_DIR_IN,
  358. .bmAttributes = USB_ENDPOINT_XFER_INT,
  359. .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
  360. .bInterval = 1 << LOG2_STATUS_INTERVAL_MSEC,
  361. };
  362. #endif
  363. #ifdef DEV_CONFIG_CDC
  364. /* the default data interface has no endpoints ... */
  365. static const struct usb_interface_descriptor
  366. data_nop_intf = {
  367. .bLength = sizeof data_nop_intf,
  368. .bDescriptorType = USB_DT_INTERFACE,
  369. .bInterfaceNumber = 1,
  370. .bAlternateSetting = 0,
  371. .bNumEndpoints = 0,
  372. .bInterfaceClass = USB_CLASS_CDC_DATA,
  373. .bInterfaceSubClass = 0,
  374. .bInterfaceProtocol = 0,
  375. };
  376. /* ... but the "real" data interface has two bulk endpoints */
  377. static const struct usb_interface_descriptor
  378. data_intf = {
  379. .bLength = sizeof data_intf,
  380. .bDescriptorType = USB_DT_INTERFACE,
  381. .bInterfaceNumber = 1,
  382. .bAlternateSetting = 1,
  383. .bNumEndpoints = 2,
  384. .bInterfaceClass = USB_CLASS_CDC_DATA,
  385. .bInterfaceSubClass = 0,
  386. .bInterfaceProtocol = 0,
  387. .iInterface = STRING_DATA,
  388. };
  389. #endif
  390. #ifdef DEV_CONFIG_SUBSET
  391. /*
  392. * "Simple" CDC-subset option is a simple vendor-neutral model that most
  393. * full speed controllers can handle: one interface, two bulk endpoints.
  394. *
  395. * To assist host side drivers, we fancy it up a bit, and add descriptors
  396. * so some host side drivers will understand it as a "SAFE" variant.
  397. */
  398. static const struct usb_interface_descriptor
  399. subset_data_intf = {
  400. .bLength = sizeof subset_data_intf,
  401. .bDescriptorType = USB_DT_INTERFACE,
  402. .bInterfaceNumber = 0,
  403. .bAlternateSetting = 0,
  404. .bNumEndpoints = 2,
  405. .bInterfaceClass = USB_CLASS_COMM,
  406. .bInterfaceSubClass = USB_CDC_SUBCLASS_MDLM,
  407. .bInterfaceProtocol = 0,
  408. .iInterface = STRING_DATA,
  409. };
  410. #endif /* SUBSET */
  411. static struct usb_endpoint_descriptor
  412. fs_source_desc = {
  413. .bLength = USB_DT_ENDPOINT_SIZE,
  414. .bDescriptorType = USB_DT_ENDPOINT,
  415. .bEndpointAddress = USB_DIR_IN,
  416. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  417. };
  418. static struct usb_endpoint_descriptor
  419. fs_sink_desc = {
  420. .bLength = USB_DT_ENDPOINT_SIZE,
  421. .bDescriptorType = USB_DT_ENDPOINT,
  422. .bEndpointAddress = USB_DIR_OUT,
  423. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  424. };
  425. static const struct usb_descriptor_header *fs_eth_function[11] = {
  426. (struct usb_descriptor_header *) &otg_descriptor,
  427. #ifdef DEV_CONFIG_CDC
  428. /* "cdc" mode descriptors */
  429. (struct usb_descriptor_header *) &control_intf,
  430. (struct usb_descriptor_header *) &header_desc,
  431. (struct usb_descriptor_header *) &union_desc,
  432. (struct usb_descriptor_header *) &ether_desc,
  433. /* NOTE: status endpoint may need to be removed */
  434. (struct usb_descriptor_header *) &fs_status_desc,
  435. /* data interface, with altsetting */
  436. (struct usb_descriptor_header *) &data_nop_intf,
  437. (struct usb_descriptor_header *) &data_intf,
  438. (struct usb_descriptor_header *) &fs_source_desc,
  439. (struct usb_descriptor_header *) &fs_sink_desc,
  440. NULL,
  441. #endif /* DEV_CONFIG_CDC */
  442. };
  443. static inline void fs_subset_descriptors(void)
  444. {
  445. #ifdef DEV_CONFIG_SUBSET
  446. /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
  447. fs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
  448. fs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
  449. fs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
  450. fs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
  451. fs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
  452. fs_eth_function[6] = (struct usb_descriptor_header *) &fs_source_desc;
  453. fs_eth_function[7] = (struct usb_descriptor_header *) &fs_sink_desc;
  454. fs_eth_function[8] = NULL;
  455. #else
  456. fs_eth_function[1] = NULL;
  457. #endif
  458. }
  459. /*
  460. * usb 2.0 devices need to expose both high speed and full speed
  461. * descriptors, unless they only run at full speed.
  462. */
  463. #if defined(DEV_CONFIG_CDC)
  464. static struct usb_endpoint_descriptor
  465. hs_status_desc = {
  466. .bLength = USB_DT_ENDPOINT_SIZE,
  467. .bDescriptorType = USB_DT_ENDPOINT,
  468. .bmAttributes = USB_ENDPOINT_XFER_INT,
  469. .wMaxPacketSize = __constant_cpu_to_le16(STATUS_BYTECOUNT),
  470. .bInterval = LOG2_STATUS_INTERVAL_MSEC + 4,
  471. };
  472. #endif /* DEV_CONFIG_CDC */
  473. static struct usb_endpoint_descriptor
  474. hs_source_desc = {
  475. .bLength = USB_DT_ENDPOINT_SIZE,
  476. .bDescriptorType = USB_DT_ENDPOINT,
  477. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  478. .wMaxPacketSize = __constant_cpu_to_le16(512),
  479. };
  480. static struct usb_endpoint_descriptor
  481. hs_sink_desc = {
  482. .bLength = USB_DT_ENDPOINT_SIZE,
  483. .bDescriptorType = USB_DT_ENDPOINT,
  484. .bmAttributes = USB_ENDPOINT_XFER_BULK,
  485. .wMaxPacketSize = __constant_cpu_to_le16(512),
  486. };
  487. static struct usb_qualifier_descriptor
  488. dev_qualifier = {
  489. .bLength = sizeof dev_qualifier,
  490. .bDescriptorType = USB_DT_DEVICE_QUALIFIER,
  491. .bcdUSB = __constant_cpu_to_le16(0x0200),
  492. .bDeviceClass = USB_CLASS_COMM,
  493. .bNumConfigurations = 1,
  494. };
  495. static const struct usb_descriptor_header *hs_eth_function[11] = {
  496. (struct usb_descriptor_header *) &otg_descriptor,
  497. #ifdef DEV_CONFIG_CDC
  498. /* "cdc" mode descriptors */
  499. (struct usb_descriptor_header *) &control_intf,
  500. (struct usb_descriptor_header *) &header_desc,
  501. (struct usb_descriptor_header *) &union_desc,
  502. (struct usb_descriptor_header *) &ether_desc,
  503. /* NOTE: status endpoint may need to be removed */
  504. (struct usb_descriptor_header *) &hs_status_desc,
  505. /* data interface, with altsetting */
  506. (struct usb_descriptor_header *) &data_nop_intf,
  507. (struct usb_descriptor_header *) &data_intf,
  508. (struct usb_descriptor_header *) &hs_source_desc,
  509. (struct usb_descriptor_header *) &hs_sink_desc,
  510. NULL,
  511. #endif /* DEV_CONFIG_CDC */
  512. };
  513. static inline void hs_subset_descriptors(void)
  514. {
  515. #ifdef DEV_CONFIG_SUBSET
  516. /* behavior is "CDC Subset"; extra descriptors say "SAFE" */
  517. hs_eth_function[1] = (struct usb_descriptor_header *) &subset_data_intf;
  518. hs_eth_function[2] = (struct usb_descriptor_header *) &header_desc;
  519. hs_eth_function[3] = (struct usb_descriptor_header *) &mdlm_desc;
  520. hs_eth_function[4] = (struct usb_descriptor_header *) &mdlm_detail_desc;
  521. hs_eth_function[5] = (struct usb_descriptor_header *) &ether_desc;
  522. hs_eth_function[6] = (struct usb_descriptor_header *) &hs_source_desc;
  523. hs_eth_function[7] = (struct usb_descriptor_header *) &hs_sink_desc;
  524. hs_eth_function[8] = NULL;
  525. #else
  526. hs_eth_function[1] = NULL;
  527. #endif
  528. }
  529. /* maxpacket and other transfer characteristics vary by speed. */
  530. static inline struct usb_endpoint_descriptor *
  531. ep_desc(struct usb_gadget *g, struct usb_endpoint_descriptor *hs,
  532. struct usb_endpoint_descriptor *fs)
  533. {
  534. if (gadget_is_dualspeed(g) && g->speed == USB_SPEED_HIGH)
  535. return hs;
  536. return fs;
  537. }
  538. /*-------------------------------------------------------------------------*/
  539. /* descriptors that are built on-demand */
  540. static char manufacturer[50];
  541. static char product_desc[40] = DRIVER_DESC;
  542. static char serial_number[20];
  543. /* address that the host will use ... usually assigned at random */
  544. static char ethaddr[2 * ETH_ALEN + 1];
  545. /* static strings, in UTF-8 */
  546. static struct usb_string strings[] = {
  547. { STRING_MANUFACTURER, manufacturer, },
  548. { STRING_PRODUCT, product_desc, },
  549. { STRING_SERIALNUMBER, serial_number, },
  550. { STRING_DATA, "Ethernet Data", },
  551. { STRING_ETHADDR, ethaddr, },
  552. #ifdef DEV_CONFIG_CDC
  553. { STRING_CDC, "CDC Ethernet", },
  554. { STRING_CONTROL, "CDC Communications Control", },
  555. #endif
  556. #ifdef DEV_CONFIG_SUBSET
  557. { STRING_SUBSET, "CDC Ethernet Subset", },
  558. #endif
  559. { } /* end of list */
  560. };
  561. static struct usb_gadget_strings stringtab = {
  562. .language = 0x0409, /* en-us */
  563. .strings = strings,
  564. };
  565. /*============================================================================*/
  566. static u8 control_req[USB_BUFSIZ];
  567. static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
  568. /**
  569. * strlcpy - Copy a %NUL terminated string into a sized buffer
  570. * @dest: Where to copy the string to
  571. * @src: Where to copy the string from
  572. * @size: size of destination buffer
  573. *
  574. * Compatible with *BSD: the result is always a valid
  575. * NUL-terminated string that fits in the buffer (unless,
  576. * of course, the buffer size is zero). It does not pad
  577. * out the result like strncpy() does.
  578. */
  579. size_t strlcpy(char *dest, const char *src, size_t size)
  580. {
  581. size_t ret = strlen(src);
  582. if (size) {
  583. size_t len = (ret >= size) ? size - 1 : ret;
  584. memcpy(dest, src, len);
  585. dest[len] = '\0';
  586. }
  587. return ret;
  588. }
  589. /*============================================================================*/
  590. /*
  591. * one config, two interfaces: control, data.
  592. * complications: class descriptors, and an altsetting.
  593. */
  594. static int
  595. config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
  596. {
  597. int len;
  598. const struct usb_config_descriptor *config;
  599. const struct usb_descriptor_header **function;
  600. int hs = 0;
  601. if (gadget_is_dualspeed(g)) {
  602. hs = (g->speed == USB_SPEED_HIGH);
  603. if (type == USB_DT_OTHER_SPEED_CONFIG)
  604. hs = !hs;
  605. }
  606. #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
  607. if (index >= device_desc.bNumConfigurations)
  608. return -EINVAL;
  609. config = &eth_config;
  610. function = which_fn(eth);
  611. /* for now, don't advertise srp-only devices */
  612. if (!is_otg)
  613. function++;
  614. len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
  615. if (len < 0)
  616. return len;
  617. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  618. return len;
  619. }
  620. /*-------------------------------------------------------------------------*/
  621. static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
  622. static int
  623. set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
  624. {
  625. int result = 0;
  626. struct usb_gadget *gadget = dev->gadget;
  627. #if defined(DEV_CONFIG_CDC)
  628. /* status endpoint used for (optionally) CDC */
  629. if (!subset_active(dev) && dev->status_ep) {
  630. dev->status = ep_desc(gadget, &hs_status_desc,
  631. &fs_status_desc);
  632. dev->status_ep->driver_data = dev;
  633. result = usb_ep_enable(dev->status_ep, dev->status);
  634. if (result != 0) {
  635. debug("enable %s --> %d\n",
  636. dev->status_ep->name, result);
  637. goto done;
  638. }
  639. }
  640. #endif
  641. dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
  642. dev->in_ep->driver_data = dev;
  643. dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
  644. dev->out_ep->driver_data = dev;
  645. /*
  646. * With CDC, the host isn't allowed to use these two data
  647. * endpoints in the default altsetting for the interface.
  648. * so we don't activate them yet. Reset from SET_INTERFACE.
  649. */
  650. if (!cdc_active(dev)) {
  651. result = usb_ep_enable(dev->in_ep, dev->in);
  652. if (result != 0) {
  653. debug("enable %s --> %d\n",
  654. dev->in_ep->name, result);
  655. goto done;
  656. }
  657. result = usb_ep_enable(dev->out_ep, dev->out);
  658. if (result != 0) {
  659. debug("enable %s --> %d\n",
  660. dev->out_ep->name, result);
  661. goto done;
  662. }
  663. }
  664. done:
  665. if (result == 0)
  666. result = alloc_requests(dev, qlen(gadget), gfp_flags);
  667. /* on error, disable any endpoints */
  668. if (result < 0) {
  669. if (!subset_active(dev) && dev->status_ep)
  670. (void) usb_ep_disable(dev->status_ep);
  671. dev->status = NULL;
  672. (void) usb_ep_disable(dev->in_ep);
  673. (void) usb_ep_disable(dev->out_ep);
  674. dev->in = NULL;
  675. dev->out = NULL;
  676. }
  677. /* caller is responsible for cleanup on error */
  678. return result;
  679. }
  680. static void eth_reset_config(struct eth_dev *dev)
  681. {
  682. if (dev->config == 0)
  683. return;
  684. debug("%s\n", __func__);
  685. /*
  686. * disable endpoints, forcing (synchronous) completion of
  687. * pending i/o. then free the requests.
  688. */
  689. if (dev->in) {
  690. usb_ep_disable(dev->in_ep);
  691. if (dev->tx_req) {
  692. usb_ep_free_request(dev->in_ep, dev->tx_req);
  693. dev->tx_req = NULL;
  694. }
  695. }
  696. if (dev->out) {
  697. usb_ep_disable(dev->out_ep);
  698. if (dev->rx_req) {
  699. usb_ep_free_request(dev->out_ep, dev->rx_req);
  700. dev->rx_req = NULL;
  701. }
  702. }
  703. if (dev->status)
  704. usb_ep_disable(dev->status_ep);
  705. dev->cdc_filter = 0;
  706. dev->config = 0;
  707. }
  708. /*
  709. * change our operational config. must agree with the code
  710. * that returns config descriptors, and altsetting code.
  711. */
  712. static int eth_set_config(struct eth_dev *dev, unsigned number,
  713. gfp_t gfp_flags)
  714. {
  715. int result = 0;
  716. struct usb_gadget *gadget = dev->gadget;
  717. if (gadget_is_sa1100(gadget)
  718. && dev->config
  719. && dev->tx_qlen != 0) {
  720. /* tx fifo is full, but we can't clear it...*/
  721. error("can't change configurations");
  722. return -ESPIPE;
  723. }
  724. eth_reset_config(dev);
  725. switch (number) {
  726. case DEV_CONFIG_VALUE:
  727. result = set_ether_config(dev, gfp_flags);
  728. break;
  729. default:
  730. result = -EINVAL;
  731. /* FALL THROUGH */
  732. case 0:
  733. break;
  734. }
  735. if (result) {
  736. if (number)
  737. eth_reset_config(dev);
  738. usb_gadget_vbus_draw(dev->gadget,
  739. gadget_is_otg(dev->gadget) ? 8 : 100);
  740. } else {
  741. char *speed;
  742. unsigned power;
  743. power = 2 * eth_config.bMaxPower;
  744. usb_gadget_vbus_draw(dev->gadget, power);
  745. switch (gadget->speed) {
  746. case USB_SPEED_FULL:
  747. speed = "full"; break;
  748. #ifdef CONFIG_USB_GADGET_DUALSPEED
  749. case USB_SPEED_HIGH:
  750. speed = "high"; break;
  751. #endif
  752. default:
  753. speed = "?"; break;
  754. }
  755. dev->config = number;
  756. printf("%s speed config #%d: %d mA, %s, using %s\n",
  757. speed, number, power, driver_desc,
  758. (cdc_active(dev) ? "CDC Ethernet"
  759. : "CDC Ethernet Subset"));
  760. }
  761. return result;
  762. }
  763. /*-------------------------------------------------------------------------*/
  764. #ifdef DEV_CONFIG_CDC
  765. /*
  766. * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
  767. * only to notify the host about link status changes (which we support) or
  768. * report completion of some encapsulated command. Since
  769. * we want this CDC Ethernet code to be vendor-neutral, we don't use that
  770. * command mechanism; and only one status request is ever queued.
  771. */
  772. static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
  773. {
  774. struct usb_cdc_notification *event = req->buf;
  775. int value = req->status;
  776. struct eth_dev *dev = ep->driver_data;
  777. /* issue the second notification if host reads the first */
  778. if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
  779. && value == 0) {
  780. __le32 *data = req->buf + sizeof *event;
  781. event->bmRequestType = 0xA1;
  782. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  783. event->wValue = __constant_cpu_to_le16(0);
  784. event->wIndex = __constant_cpu_to_le16(1);
  785. event->wLength = __constant_cpu_to_le16(8);
  786. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  787. data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
  788. req->length = STATUS_BYTECOUNT;
  789. value = usb_ep_queue(ep, req, GFP_ATOMIC);
  790. debug("send SPEED_CHANGE --> %d\n", value);
  791. if (value == 0)
  792. return;
  793. } else if (value != -ECONNRESET) {
  794. debug("event %02x --> %d\n",
  795. event->bNotificationType, value);
  796. if (event->bNotificationType ==
  797. USB_CDC_NOTIFY_SPEED_CHANGE) {
  798. l_ethdev.network_started = 1;
  799. printf("USB network up!\n");
  800. }
  801. }
  802. req->context = NULL;
  803. }
  804. static void issue_start_status(struct eth_dev *dev)
  805. {
  806. struct usb_request *req = dev->stat_req;
  807. struct usb_cdc_notification *event;
  808. int value;
  809. /*
  810. * flush old status
  811. *
  812. * FIXME ugly idiom, maybe we'd be better with just
  813. * a "cancel the whole queue" primitive since any
  814. * unlink-one primitive has way too many error modes.
  815. * here, we "know" toggle is already clear...
  816. *
  817. * FIXME iff req->context != null just dequeue it
  818. */
  819. usb_ep_disable(dev->status_ep);
  820. usb_ep_enable(dev->status_ep, dev->status);
  821. /*
  822. * 3.8.1 says to issue first NETWORK_CONNECTION, then
  823. * a SPEED_CHANGE. could be useful in some configs.
  824. */
  825. event = req->buf;
  826. event->bmRequestType = 0xA1;
  827. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  828. event->wValue = __constant_cpu_to_le16(1); /* connected */
  829. event->wIndex = __constant_cpu_to_le16(1);
  830. event->wLength = 0;
  831. req->length = sizeof *event;
  832. req->complete = eth_status_complete;
  833. req->context = dev;
  834. value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
  835. if (value < 0)
  836. debug("status buf queue --> %d\n", value);
  837. }
  838. #endif
  839. /*-------------------------------------------------------------------------*/
  840. static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
  841. {
  842. if (req->status || req->actual != req->length)
  843. debug("setup complete --> %d, %d/%d\n",
  844. req->status, req->actual, req->length);
  845. }
  846. /*
  847. * The setup() callback implements all the ep0 functionality that's not
  848. * handled lower down. CDC has a number of less-common features:
  849. *
  850. * - two interfaces: control, and ethernet data
  851. * - Ethernet data interface has two altsettings: default, and active
  852. * - class-specific descriptors for the control interface
  853. * - class-specific control requests
  854. */
  855. static int
  856. eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  857. {
  858. struct eth_dev *dev = get_gadget_data(gadget);
  859. struct usb_request *req = dev->req;
  860. int value = -EOPNOTSUPP;
  861. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  862. u16 wValue = le16_to_cpu(ctrl->wValue);
  863. u16 wLength = le16_to_cpu(ctrl->wLength);
  864. /*
  865. * descriptors just go into the pre-allocated ep0 buffer,
  866. * while config change events may enable network traffic.
  867. */
  868. debug("%s\n", __func__);
  869. req->complete = eth_setup_complete;
  870. switch (ctrl->bRequest) {
  871. case USB_REQ_GET_DESCRIPTOR:
  872. if (ctrl->bRequestType != USB_DIR_IN)
  873. break;
  874. switch (wValue >> 8) {
  875. case USB_DT_DEVICE:
  876. value = min(wLength, (u16) sizeof device_desc);
  877. memcpy(req->buf, &device_desc, value);
  878. break;
  879. case USB_DT_DEVICE_QUALIFIER:
  880. if (!gadget_is_dualspeed(gadget))
  881. break;
  882. value = min(wLength, (u16) sizeof dev_qualifier);
  883. memcpy(req->buf, &dev_qualifier, value);
  884. break;
  885. case USB_DT_OTHER_SPEED_CONFIG:
  886. if (!gadget_is_dualspeed(gadget))
  887. break;
  888. /* FALLTHROUGH */
  889. case USB_DT_CONFIG:
  890. value = config_buf(gadget, req->buf,
  891. wValue >> 8,
  892. wValue & 0xff,
  893. gadget_is_otg(gadget));
  894. if (value >= 0)
  895. value = min(wLength, (u16) value);
  896. break;
  897. case USB_DT_STRING:
  898. value = usb_gadget_get_string(&stringtab,
  899. wValue & 0xff, req->buf);
  900. if (value >= 0)
  901. value = min(wLength, (u16) value);
  902. break;
  903. }
  904. break;
  905. case USB_REQ_SET_CONFIGURATION:
  906. if (ctrl->bRequestType != 0)
  907. break;
  908. if (gadget->a_hnp_support)
  909. debug("HNP available\n");
  910. else if (gadget->a_alt_hnp_support)
  911. debug("HNP needs a different root port\n");
  912. value = eth_set_config(dev, wValue, GFP_ATOMIC);
  913. break;
  914. case USB_REQ_GET_CONFIGURATION:
  915. if (ctrl->bRequestType != USB_DIR_IN)
  916. break;
  917. *(u8 *)req->buf = dev->config;
  918. value = min(wLength, (u16) 1);
  919. break;
  920. case USB_REQ_SET_INTERFACE:
  921. if (ctrl->bRequestType != USB_RECIP_INTERFACE
  922. || !dev->config
  923. || wIndex > 1)
  924. break;
  925. if (!cdc_active(dev) && wIndex != 0)
  926. break;
  927. /*
  928. * PXA hardware partially handles SET_INTERFACE;
  929. * we need to kluge around that interference.
  930. */
  931. if (gadget_is_pxa(gadget)) {
  932. value = eth_set_config(dev, DEV_CONFIG_VALUE,
  933. GFP_ATOMIC);
  934. goto done_set_intf;
  935. }
  936. #ifdef DEV_CONFIG_CDC
  937. switch (wIndex) {
  938. case 0: /* control/master intf */
  939. if (wValue != 0)
  940. break;
  941. if (dev->status) {
  942. usb_ep_disable(dev->status_ep);
  943. usb_ep_enable(dev->status_ep, dev->status);
  944. }
  945. value = 0;
  946. break;
  947. case 1: /* data intf */
  948. if (wValue > 1)
  949. break;
  950. usb_ep_disable(dev->in_ep);
  951. usb_ep_disable(dev->out_ep);
  952. /*
  953. * CDC requires the data transfers not be done from
  954. * the default interface setting ... also, setting
  955. * the non-default interface resets filters etc.
  956. */
  957. if (wValue == 1) {
  958. if (!cdc_active(dev))
  959. break;
  960. usb_ep_enable(dev->in_ep, dev->in);
  961. usb_ep_enable(dev->out_ep, dev->out);
  962. dev->cdc_filter = DEFAULT_FILTER;
  963. if (dev->status)
  964. issue_start_status(dev);
  965. }
  966. value = 0;
  967. break;
  968. }
  969. #else
  970. /*
  971. * FIXME this is wrong, as is the assumption that
  972. * all non-PXA hardware talks real CDC ...
  973. */
  974. debug("set_interface ignored!\n");
  975. #endif /* DEV_CONFIG_CDC */
  976. done_set_intf:
  977. break;
  978. case USB_REQ_GET_INTERFACE:
  979. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
  980. || !dev->config
  981. || wIndex > 1)
  982. break;
  983. if (!(cdc_active(dev)) && wIndex != 0)
  984. break;
  985. /* for CDC, iff carrier is on, data interface is active. */
  986. if (wIndex != 1)
  987. *(u8 *)req->buf = 0;
  988. else {
  989. /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
  990. /* carrier always ok ...*/
  991. *(u8 *)req->buf = 1 ;
  992. }
  993. value = min(wLength, (u16) 1);
  994. break;
  995. #ifdef DEV_CONFIG_CDC
  996. case USB_CDC_SET_ETHERNET_PACKET_FILTER:
  997. /*
  998. * see 6.2.30: no data, wIndex = interface,
  999. * wValue = packet filter bitmap
  1000. */
  1001. if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1002. || !cdc_active(dev)
  1003. || wLength != 0
  1004. || wIndex > 1)
  1005. break;
  1006. debug("packet filter %02x\n", wValue);
  1007. dev->cdc_filter = wValue;
  1008. value = 0;
  1009. break;
  1010. /*
  1011. * and potentially:
  1012. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  1013. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  1014. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  1015. * case USB_CDC_GET_ETHERNET_STATISTIC:
  1016. */
  1017. #endif /* DEV_CONFIG_CDC */
  1018. default:
  1019. debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
  1020. ctrl->bRequestType, ctrl->bRequest,
  1021. wValue, wIndex, wLength);
  1022. }
  1023. /* respond with data transfer before status phase? */
  1024. if (value >= 0) {
  1025. debug("respond with data transfer before status phase\n");
  1026. req->length = value;
  1027. req->zero = value < wLength
  1028. && (value % gadget->ep0->maxpacket) == 0;
  1029. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1030. if (value < 0) {
  1031. debug("ep_queue --> %d\n", value);
  1032. req->status = 0;
  1033. eth_setup_complete(gadget->ep0, req);
  1034. }
  1035. }
  1036. /* host either stalls (value < 0) or reports success */
  1037. return value;
  1038. }
  1039. /*-------------------------------------------------------------------------*/
  1040. static void rx_complete(struct usb_ep *ep, struct usb_request *req);
  1041. static int rx_submit(struct eth_dev *dev, struct usb_request *req,
  1042. gfp_t gfp_flags)
  1043. {
  1044. int retval = -ENOMEM;
  1045. size_t size;
  1046. /*
  1047. * Padding up to RX_EXTRA handles minor disagreements with host.
  1048. * Normally we use the USB "terminate on short read" convention;
  1049. * so allow up to (N*maxpacket), since that memory is normally
  1050. * already allocated. Some hardware doesn't deal well with short
  1051. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  1052. * byte off the end (to force hardware errors on overflow).
  1053. */
  1054. debug("%s\n", __func__);
  1055. size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
  1056. size += dev->out_ep->maxpacket - 1;
  1057. size -= size % dev->out_ep->maxpacket;
  1058. /*
  1059. * Some platforms perform better when IP packets are aligned,
  1060. * but on at least one, checksumming fails otherwise.
  1061. */
  1062. req->buf = (u8 *) NetRxPackets[0];
  1063. req->length = size;
  1064. req->complete = rx_complete;
  1065. retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
  1066. if (retval)
  1067. error("rx submit --> %d", retval);
  1068. return retval;
  1069. }
  1070. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  1071. {
  1072. struct eth_dev *dev = ep->driver_data;
  1073. debug("%s: status %d\n", __func__, req->status);
  1074. packet_received = 1;
  1075. }
  1076. static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
  1077. {
  1078. dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
  1079. if (!dev->tx_req)
  1080. goto fail1;
  1081. dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
  1082. if (!dev->rx_req)
  1083. goto fail2;
  1084. return 0;
  1085. fail2:
  1086. usb_ep_free_request(dev->in_ep, dev->tx_req);
  1087. fail1:
  1088. error("can't alloc requests");
  1089. return -1;
  1090. }
  1091. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  1092. {
  1093. debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
  1094. packet_sent = 1;
  1095. }
  1096. static inline int eth_is_promisc(struct eth_dev *dev)
  1097. {
  1098. /* no filters for the CDC subset; always promisc */
  1099. if (subset_active(dev))
  1100. return 1;
  1101. return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  1102. }
  1103. #if 0
  1104. static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
  1105. {
  1106. struct eth_dev *dev = netdev_priv(net);
  1107. int length = skb->len;
  1108. int retval;
  1109. struct usb_request *req = NULL;
  1110. unsigned long flags;
  1111. /* apply outgoing CDC or RNDIS filters */
  1112. if (!eth_is_promisc (dev)) {
  1113. u8 *dest = skb->data;
  1114. if (is_multicast_ether_addr(dest)) {
  1115. u16 type;
  1116. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  1117. * SET_ETHERNET_MULTICAST_FILTERS requests
  1118. */
  1119. if (is_broadcast_ether_addr(dest))
  1120. type = USB_CDC_PACKET_TYPE_BROADCAST;
  1121. else
  1122. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  1123. if (!(dev->cdc_filter & type)) {
  1124. dev_kfree_skb_any (skb);
  1125. return 0;
  1126. }
  1127. }
  1128. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  1129. }
  1130. spin_lock_irqsave(&dev->req_lock, flags);
  1131. /*
  1132. * this freelist can be empty if an interrupt triggered disconnect()
  1133. * and reconfigured the gadget (shutting down this queue) after the
  1134. * network stack decided to xmit but before we got the spinlock.
  1135. */
  1136. if (list_empty(&dev->tx_reqs)) {
  1137. spin_unlock_irqrestore(&dev->req_lock, flags);
  1138. return 1;
  1139. }
  1140. req = container_of (dev->tx_reqs.next, struct usb_request, list);
  1141. list_del (&req->list);
  1142. /* temporarily stop TX queue when the freelist empties */
  1143. if (list_empty (&dev->tx_reqs))
  1144. netif_stop_queue (net);
  1145. spin_unlock_irqrestore(&dev->req_lock, flags);
  1146. /* no buffer copies needed, unless the network stack did it
  1147. * or the hardware can't use skb buffers.
  1148. * or there's not enough space for any RNDIS headers we need
  1149. */
  1150. if (rndis_active(dev)) {
  1151. struct sk_buff *skb_rndis;
  1152. skb_rndis = skb_realloc_headroom (skb,
  1153. sizeof (struct rndis_packet_msg_type));
  1154. if (!skb_rndis)
  1155. goto drop;
  1156. dev_kfree_skb_any (skb);
  1157. skb = skb_rndis;
  1158. rndis_add_hdr (skb);
  1159. length = skb->len;
  1160. }
  1161. req->buf = skb->data;
  1162. req->context = skb;
  1163. req->complete = tx_complete;
  1164. /* use zlp framing on tx for strict CDC-Ether conformance,
  1165. * though any robust network rx path ignores extra padding.
  1166. * and some hardware doesn't like to write zlps.
  1167. */
  1168. req->zero = 1;
  1169. if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
  1170. length++;
  1171. req->length = length;
  1172. /* throttle highspeed IRQ rate back slightly */
  1173. if (gadget_is_dualspeed(dev->gadget))
  1174. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
  1175. ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
  1176. : 0;
  1177. retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
  1178. switch (retval) {
  1179. default:
  1180. DEBUG (dev, "tx queue err %d\n", retval);
  1181. break;
  1182. case 0:
  1183. net->trans_start = jiffies;
  1184. atomic_inc (&dev->tx_qlen);
  1185. }
  1186. if (retval) {
  1187. drop:
  1188. dev->stats.tx_dropped++;
  1189. dev_kfree_skb_any (skb);
  1190. spin_lock_irqsave(&dev->req_lock, flags);
  1191. if (list_empty (&dev->tx_reqs))
  1192. netif_start_queue (net);
  1193. list_add (&req->list, &dev->tx_reqs);
  1194. spin_unlock_irqrestore(&dev->req_lock, flags);
  1195. }
  1196. return 0;
  1197. }
  1198. /*-------------------------------------------------------------------------*/
  1199. #endif
  1200. static void eth_unbind(struct usb_gadget *gadget)
  1201. {
  1202. struct eth_dev *dev = get_gadget_data(gadget);
  1203. debug("%s...\n", __func__);
  1204. /* we've already been disconnected ... no i/o is active */
  1205. if (dev->req) {
  1206. usb_ep_free_request(gadget->ep0, dev->req);
  1207. dev->req = NULL;
  1208. }
  1209. if (dev->stat_req) {
  1210. usb_ep_free_request(dev->status_ep, dev->stat_req);
  1211. dev->stat_req = NULL;
  1212. }
  1213. if (dev->tx_req) {
  1214. usb_ep_free_request(dev->in_ep, dev->tx_req);
  1215. dev->tx_req = NULL;
  1216. }
  1217. if (dev->rx_req) {
  1218. usb_ep_free_request(dev->out_ep, dev->rx_req);
  1219. dev->rx_req = NULL;
  1220. }
  1221. /* unregister_netdev (dev->net);*/
  1222. /* free_netdev(dev->net);*/
  1223. dev->gadget = NULL;
  1224. set_gadget_data(gadget, NULL);
  1225. }
  1226. static void eth_disconnect(struct usb_gadget *gadget)
  1227. {
  1228. eth_reset_config(get_gadget_data(gadget));
  1229. }
  1230. static void eth_suspend(struct usb_gadget *gadget)
  1231. {
  1232. /* Not used */
  1233. }
  1234. static void eth_resume(struct usb_gadget *gadget)
  1235. {
  1236. /* Not used */
  1237. }
  1238. /*-------------------------------------------------------------------------*/
  1239. static int is_eth_addr_valid(char *str)
  1240. {
  1241. if (strlen(str) == 17) {
  1242. int i;
  1243. char *p, *q;
  1244. uchar ea[6];
  1245. /* see if it looks like an ethernet address */
  1246. p = str;
  1247. for (i = 0; i < 6; i++) {
  1248. char term = (i == 5 ? '\0' : ':');
  1249. ea[i] = simple_strtol(p, &q, 16);
  1250. if ((q - p) != 2 || *q++ != term)
  1251. break;
  1252. p = q;
  1253. }
  1254. if (i == 6) /* it looks ok */
  1255. return 1;
  1256. }
  1257. return 0;
  1258. }
  1259. static u8 nibble(unsigned char c)
  1260. {
  1261. if (likely(isdigit(c)))
  1262. return c - '0';
  1263. c = toupper(c);
  1264. if (likely(isxdigit(c)))
  1265. return 10 + c - 'A';
  1266. return 0;
  1267. }
  1268. static int get_ether_addr(const char *str, u8 *dev_addr)
  1269. {
  1270. if (str) {
  1271. unsigned i;
  1272. for (i = 0; i < 6; i++) {
  1273. unsigned char num;
  1274. if ((*str == '.') || (*str == ':'))
  1275. str++;
  1276. num = nibble(*str++) << 4;
  1277. num |= (nibble(*str++));
  1278. dev_addr[i] = num;
  1279. }
  1280. if (is_valid_ether_addr(dev_addr))
  1281. return 0;
  1282. }
  1283. return 1;
  1284. }
  1285. static int eth_bind(struct usb_gadget *gadget)
  1286. {
  1287. struct eth_dev *dev = &l_ethdev;
  1288. u8 cdc = 1, zlp = 1;
  1289. struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
  1290. int gcnum;
  1291. u8 tmp[7];
  1292. /* these flags are only ever cleared; compiler take note */
  1293. #ifndef DEV_CONFIG_CDC
  1294. cdc = 0;
  1295. #endif
  1296. /*
  1297. * Because most host side USB stacks handle CDC Ethernet, that
  1298. * standard protocol is _strongly_ preferred for interop purposes.
  1299. * (By everyone except Microsoft.)
  1300. */
  1301. if (gadget_is_pxa(gadget)) {
  1302. /* pxa doesn't support altsettings */
  1303. cdc = 0;
  1304. } else if (gadget_is_musbhdrc(gadget)) {
  1305. /* reduce tx dma overhead by avoiding special cases */
  1306. zlp = 0;
  1307. } else if (gadget_is_sh(gadget)) {
  1308. /* sh doesn't support multiple interfaces or configs */
  1309. cdc = 0;
  1310. } else if (gadget_is_sa1100(gadget)) {
  1311. /* hardware can't write zlps */
  1312. zlp = 0;
  1313. /*
  1314. * sa1100 CAN do CDC, without status endpoint ... we use
  1315. * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
  1316. */
  1317. cdc = 0;
  1318. }
  1319. gcnum = usb_gadget_controller_number(gadget);
  1320. if (gcnum >= 0)
  1321. device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
  1322. else {
  1323. /*
  1324. * can't assume CDC works. don't want to default to
  1325. * anything less functional on CDC-capable hardware,
  1326. * so we fail in this case.
  1327. */
  1328. error("controller '%s' not recognized",
  1329. gadget->name);
  1330. return -ENODEV;
  1331. }
  1332. /*
  1333. * CDC subset ... recognized by Linux since 2.4.10, but Windows
  1334. * drivers aren't widely available. (That may be improved by
  1335. * supporting one submode of the "SAFE" variant of MDLM.)
  1336. */
  1337. if (!cdc) {
  1338. device_desc.idVendor =
  1339. __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
  1340. device_desc.idProduct =
  1341. __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
  1342. }
  1343. /* support optional vendor/distro customization */
  1344. #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
  1345. device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
  1346. device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
  1347. #endif
  1348. if (bcdDevice)
  1349. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1350. if (iManufacturer)
  1351. strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
  1352. if (iProduct)
  1353. strlcpy(product_desc, iProduct, sizeof product_desc);
  1354. if (iSerialNumber) {
  1355. device_desc.iSerialNumber = STRING_SERIALNUMBER,
  1356. strlcpy(serial_number, iSerialNumber, sizeof serial_number);
  1357. }
  1358. /* all we really need is bulk IN/OUT */
  1359. usb_ep_autoconfig_reset(gadget);
  1360. in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
  1361. if (!in_ep) {
  1362. autoconf_fail:
  1363. error("can't autoconfigure on %s\n",
  1364. gadget->name);
  1365. return -ENODEV;
  1366. }
  1367. in_ep->driver_data = in_ep; /* claim */
  1368. out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
  1369. if (!out_ep)
  1370. goto autoconf_fail;
  1371. out_ep->driver_data = out_ep; /* claim */
  1372. #if defined(DEV_CONFIG_CDC)
  1373. /*
  1374. * CDC Ethernet control interface doesn't require a status endpoint.
  1375. * Since some hosts expect one, try to allocate one anyway.
  1376. */
  1377. if (cdc) {
  1378. status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
  1379. if (status_ep) {
  1380. status_ep->driver_data = status_ep; /* claim */
  1381. } else if (cdc) {
  1382. control_intf.bNumEndpoints = 0;
  1383. /* FIXME remove endpoint from descriptor list */
  1384. }
  1385. }
  1386. #endif
  1387. /* one config: cdc, else minimal subset */
  1388. if (!cdc) {
  1389. eth_config.bNumInterfaces = 1;
  1390. eth_config.iConfiguration = STRING_SUBSET;
  1391. /*
  1392. * use functions to set these up, in case we're built to work
  1393. * with multiple controllers and must override CDC Ethernet.
  1394. */
  1395. fs_subset_descriptors();
  1396. hs_subset_descriptors();
  1397. }
  1398. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1399. usb_gadget_set_selfpowered(gadget);
  1400. if (gadget_is_dualspeed(gadget)) {
  1401. if (!cdc)
  1402. dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  1403. /* assumes ep0 uses the same value for both speeds ... */
  1404. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1405. /* and that all endpoints are dual-speed */
  1406. hs_source_desc.bEndpointAddress =
  1407. fs_source_desc.bEndpointAddress;
  1408. hs_sink_desc.bEndpointAddress =
  1409. fs_sink_desc.bEndpointAddress;
  1410. #if defined(DEV_CONFIG_CDC)
  1411. if (status_ep)
  1412. hs_status_desc.bEndpointAddress =
  1413. fs_status_desc.bEndpointAddress;
  1414. #endif
  1415. }
  1416. if (gadget_is_otg(gadget)) {
  1417. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1418. eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1419. eth_config.bMaxPower = 4;
  1420. }
  1421. dev->net = &l_netdev;
  1422. dev->cdc = cdc;
  1423. dev->zlp = zlp;
  1424. dev->in_ep = in_ep;
  1425. dev->out_ep = out_ep;
  1426. dev->status_ep = status_ep;
  1427. /*
  1428. * Module params for these addresses should come from ID proms.
  1429. * The host side address is used with CDC, and commonly
  1430. * ends up in a persistent config database. It's not clear if
  1431. * host side code for the SAFE thing cares -- its original BLAN
  1432. * thing didn't, Sharp never assigned those addresses on Zaurii.
  1433. */
  1434. get_ether_addr(dev_addr, dev->net->enetaddr);
  1435. memset(tmp, 0, sizeof(tmp));
  1436. memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
  1437. get_ether_addr(host_addr, dev->host_mac);
  1438. sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
  1439. dev->host_mac[0], dev->host_mac[1],
  1440. dev->host_mac[2], dev->host_mac[3],
  1441. dev->host_mac[4], dev->host_mac[5]);
  1442. printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
  1443. out_ep->name, in_ep->name,
  1444. status_ep ? " STATUS " : "",
  1445. status_ep ? status_ep->name : ""
  1446. );
  1447. printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1448. dev->net->enetaddr[0], dev->net->enetaddr[1],
  1449. dev->net->enetaddr[2], dev->net->enetaddr[3],
  1450. dev->net->enetaddr[4], dev->net->enetaddr[5]);
  1451. if (cdc) {
  1452. printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1453. dev->host_mac[0], dev->host_mac[1],
  1454. dev->host_mac[2], dev->host_mac[3],
  1455. dev->host_mac[4], dev->host_mac[5]);
  1456. }
  1457. /*
  1458. * use PKTSIZE (or aligned... from u-boot) and set
  1459. * wMaxSegmentSize accordingly
  1460. */
  1461. dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
  1462. /* preallocate control message data and buffer */
  1463. dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1464. if (!dev->req)
  1465. goto fail;
  1466. dev->req->buf = control_req;
  1467. dev->req->complete = eth_setup_complete;
  1468. /* ... and maybe likewise for status transfer */
  1469. #if defined(DEV_CONFIG_CDC)
  1470. if (dev->status_ep) {
  1471. dev->stat_req = usb_ep_alloc_request(dev->status_ep,
  1472. GFP_KERNEL);
  1473. if (!dev->stat_req) {
  1474. usb_ep_free_request(dev->status_ep, dev->req);
  1475. goto fail;
  1476. }
  1477. dev->stat_req->buf = status_req;
  1478. dev->stat_req->context = NULL;
  1479. }
  1480. #endif
  1481. /* finish hookup to lower layer ... */
  1482. dev->gadget = gadget;
  1483. set_gadget_data(gadget, dev);
  1484. gadget->ep0->driver_data = dev;
  1485. /*
  1486. * two kinds of host-initiated state changes:
  1487. * - iff DATA transfer is active, carrier is "on"
  1488. * - tx queueing enabled if open *and* carrier is "on"
  1489. */
  1490. return 0;
  1491. fail:
  1492. error("%s failed", __func__);
  1493. eth_unbind(gadget);
  1494. return -ENOMEM;
  1495. }
  1496. static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
  1497. {
  1498. struct eth_dev *dev = &l_ethdev;
  1499. struct usb_gadget *gadget;
  1500. unsigned long ts;
  1501. unsigned long timeout = USB_CONNECT_TIMEOUT;
  1502. if (!netdev) {
  1503. error("received NULL ptr");
  1504. goto fail;
  1505. }
  1506. if (usb_gadget_register_driver(&eth_driver) < 0)
  1507. goto fail;
  1508. dev->network_started = 0;
  1509. packet_received = 0;
  1510. packet_sent = 0;
  1511. gadget = dev->gadget;
  1512. usb_gadget_connect(gadget);
  1513. if (getenv("cdc_connect_timeout"))
  1514. timeout = simple_strtoul(getenv("cdc_connect_timeout"),
  1515. NULL, 10) * CONFIG_SYS_HZ;
  1516. ts = get_timer(0);
  1517. while (!l_ethdev.network_started) {
  1518. /* Handle control-c and timeouts */
  1519. if (ctrlc() || (get_timer(ts) > timeout)) {
  1520. error("The remote end did not respond in time.");
  1521. goto fail;
  1522. }
  1523. usb_gadget_handle_interrupts();
  1524. }
  1525. packet_received = 0;
  1526. rx_submit(dev, dev->rx_req, 0);
  1527. return 0;
  1528. fail:
  1529. return -1;
  1530. }
  1531. static int usb_eth_send(struct eth_device *netdev,
  1532. volatile void *packet, int length)
  1533. {
  1534. int retval;
  1535. struct eth_dev *dev = &l_ethdev;
  1536. struct usb_request *req = dev->tx_req;
  1537. unsigned long ts;
  1538. unsigned long timeout = USB_CONNECT_TIMEOUT;
  1539. debug("%s:...\n", __func__);
  1540. req->buf = (void *)packet;
  1541. req->context = NULL;
  1542. req->complete = tx_complete;
  1543. /*
  1544. * use zlp framing on tx for strict CDC-Ether conformance,
  1545. * though any robust network rx path ignores extra padding.
  1546. * and some hardware doesn't like to write zlps.
  1547. */
  1548. req->zero = 1;
  1549. if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
  1550. length++;
  1551. req->length = length;
  1552. #if 0
  1553. /* throttle highspeed IRQ rate back slightly */
  1554. if (gadget_is_dualspeed(dev->gadget))
  1555. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
  1556. ? ((dev->tx_qlen % qmult) != 0) : 0;
  1557. #endif
  1558. dev->tx_qlen = 1;
  1559. ts = get_timer(0);
  1560. packet_sent = 0;
  1561. retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
  1562. if (!retval)
  1563. debug("%s: packet queued\n", __func__);
  1564. while (!packet_sent) {
  1565. if (get_timer(ts) > timeout) {
  1566. printf("timeout sending packets to usb ethernet\n");
  1567. return -1;
  1568. }
  1569. usb_gadget_handle_interrupts();
  1570. }
  1571. return 0;
  1572. }
  1573. static int usb_eth_recv(struct eth_device *netdev)
  1574. {
  1575. struct eth_dev *dev = &l_ethdev;
  1576. usb_gadget_handle_interrupts();
  1577. if (packet_received) {
  1578. debug("%s: packet received\n", __func__);
  1579. if (dev->rx_req) {
  1580. NetReceive(NetRxPackets[0], dev->rx_req->length);
  1581. packet_received = 0;
  1582. rx_submit(dev, dev->rx_req, 0);
  1583. } else
  1584. error("dev->rx_req invalid");
  1585. }
  1586. return 0;
  1587. }
  1588. void usb_eth_halt(struct eth_device *netdev)
  1589. {
  1590. struct eth_dev *dev = &l_ethdev;
  1591. if (!netdev) {
  1592. error("received NULL ptr");
  1593. return;
  1594. }
  1595. /* If the gadget not registered, simple return */
  1596. if (!dev->gadget)
  1597. return;
  1598. usb_gadget_disconnect(dev->gadget);
  1599. usb_gadget_unregister_driver(&eth_driver);
  1600. }
  1601. static struct usb_gadget_driver eth_driver = {
  1602. .speed = DEVSPEED,
  1603. .bind = eth_bind,
  1604. .unbind = eth_unbind,
  1605. .setup = eth_setup,
  1606. .disconnect = eth_disconnect,
  1607. .suspend = eth_suspend,
  1608. .resume = eth_resume,
  1609. };
  1610. int usb_eth_initialize(bd_t *bi)
  1611. {
  1612. int status = 0;
  1613. struct eth_device *netdev = &l_netdev;
  1614. strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
  1615. netdev->init = usb_eth_init;
  1616. netdev->send = usb_eth_send;
  1617. netdev->recv = usb_eth_recv;
  1618. netdev->halt = usb_eth_halt;
  1619. #ifdef CONFIG_MCAST_TFTP
  1620. #error not supported
  1621. #endif
  1622. /* Configure default mac-addresses for the USB ethernet device */
  1623. #ifdef CONFIG_USBNET_DEV_ADDR
  1624. strncpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
  1625. #endif
  1626. #ifdef CONFIG_USBNET_HOST_ADDR
  1627. strncpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
  1628. #endif
  1629. /* Check if the user overruled the MAC addresses */
  1630. if (getenv("usbnet_devaddr"))
  1631. strncpy(dev_addr, getenv("usbnet_devaddr"),
  1632. sizeof(dev_addr));
  1633. if (getenv("usbnet_hostaddr"))
  1634. strncpy(host_addr, getenv("usbnet_hostaddr"),
  1635. sizeof(host_addr));
  1636. /* Make sure both strings are terminated */
  1637. dev_addr[sizeof(dev_addr)-1] = '\0';
  1638. host_addr[sizeof(host_addr)-1] = '\0';
  1639. if (!is_eth_addr_valid(dev_addr)) {
  1640. error("Need valid 'usbnet_devaddr' to be set");
  1641. status = -1;
  1642. }
  1643. if (!is_eth_addr_valid(host_addr)) {
  1644. error("Need valid 'usbnet_hostaddr' to be set");
  1645. status = -1;
  1646. }
  1647. if (status)
  1648. goto fail;
  1649. eth_register(netdev);
  1650. return 0;
  1651. fail:
  1652. error("%s failed. error = %d", __func__, status);
  1653. return status;
  1654. }