ether.c 52 KB

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