ether.c 71 KB

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