ether.c 53 KB

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