ether.c 52 KB

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