ether.c 51 KB

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