ether.c 72 KB

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