ether.c 52 KB

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