ether.c 68 KB

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