ether.c 69 KB

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