ether.c 51 KB

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