ether.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. /*
  2. * ether.c -- Ethernet gadget driver, with CDC and non-CDC options
  3. *
  4. * Copyright (C) 2003-2005,2008 David Brownell
  5. * Copyright (C) 2003-2004 Robert Schwebel, Benedikt Spranger
  6. * Copyright (C) 2008 Nokia Corporation
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #include <asm/errno.h>
  24. #include <linux/netdevice.h>
  25. #include <linux/usb/ch9.h>
  26. #include <linux/usb/cdc.h>
  27. #include <linux/usb/gadget.h>
  28. #include <net.h>
  29. #include <malloc.h>
  30. #include <linux/ctype.h>
  31. #include "gadget_chips.h"
  32. #include "rndis.h"
  33. #define USB_NET_NAME "usb_ether"
  34. #define atomic_read
  35. extern struct platform_data brd;
  36. #define spin_lock(x)
  37. #define spin_unlock(x)
  38. unsigned packet_received, packet_sent;
  39. #define DEV_CONFIG_CDC 1
  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(DEV_CONFIG_SUBSET)
  139. return 1; /* only cdc possible */
  140. #elif !defined(DEV_CONFIG_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 DEV_CONFIG_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(DEV_CONFIG_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 DEV_CONFIG_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(DEV_CONFIG_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 DEV_CONFIG_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 DEV_CONFIG_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 DEV_CONFIG_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 /* DEV_CONFIG_CDC */
  559. };
  560. static inline void fs_subset_descriptors(void)
  561. {
  562. #ifdef DEV_CONFIG_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(DEV_CONFIG_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 /* DEV_CONFIG_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 DEV_CONFIG_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 /* DEV_CONFIG_CDC */
  646. };
  647. static inline void hs_subset_descriptors(void)
  648. {
  649. #ifdef DEV_CONFIG_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 DEV_CONFIG_CDC
  704. { STRING_CDC, "CDC Ethernet", },
  705. { STRING_CONTROL, "CDC Communications Control", },
  706. #endif
  707. #ifdef DEV_CONFIG_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. static u8 status_req[STATUS_BYTECOUNT] __attribute__ ((aligned(4)));
  723. /**
  724. * strlcpy - Copy a %NUL terminated string into a sized buffer
  725. * @dest: Where to copy the string to
  726. * @src: Where to copy the string from
  727. * @size: size of destination buffer
  728. *
  729. * Compatible with *BSD: the result is always a valid
  730. * NUL-terminated string that fits in the buffer (unless,
  731. * of course, the buffer size is zero). It does not pad
  732. * out the result like strncpy() does.
  733. */
  734. size_t strlcpy(char *dest, const char *src, size_t size)
  735. {
  736. size_t ret = strlen(src);
  737. if (size) {
  738. size_t len = (ret >= size) ? size - 1 : ret;
  739. memcpy(dest, src, len);
  740. dest[len] = '\0';
  741. }
  742. return ret;
  743. }
  744. /*============================================================================*/
  745. /*
  746. * one config, two interfaces: control, data.
  747. * complications: class descriptors, and an altsetting.
  748. */
  749. static int
  750. config_buf(struct usb_gadget *g, u8 *buf, u8 type, unsigned index, int is_otg)
  751. {
  752. int len;
  753. const struct usb_config_descriptor *config;
  754. const struct usb_descriptor_header **function;
  755. int hs = 0;
  756. if (gadget_is_dualspeed(g)) {
  757. hs = (g->speed == USB_SPEED_HIGH);
  758. if (type == USB_DT_OTHER_SPEED_CONFIG)
  759. hs = !hs;
  760. }
  761. #define which_fn(t) (hs ? hs_ ## t ## _function : fs_ ## t ## _function)
  762. if (index >= device_desc.bNumConfigurations)
  763. return -EINVAL;
  764. #ifdef CONFIG_USB_ETH_RNDIS
  765. /*
  766. * list the RNDIS config first, to make Microsoft's drivers
  767. * happy. DOCSIS 1.0 needs this too.
  768. */
  769. if (device_desc.bNumConfigurations == 2 && index == 0) {
  770. config = &rndis_config;
  771. function = which_fn(rndis);
  772. } else
  773. #endif
  774. {
  775. config = &eth_config;
  776. function = which_fn(eth);
  777. }
  778. /* for now, don't advertise srp-only devices */
  779. if (!is_otg)
  780. function++;
  781. len = usb_gadget_config_buf(config, buf, USB_BUFSIZ, function);
  782. if (len < 0)
  783. return len;
  784. ((struct usb_config_descriptor *) buf)->bDescriptorType = type;
  785. return len;
  786. }
  787. /*-------------------------------------------------------------------------*/
  788. static void eth_start(struct eth_dev *dev, gfp_t gfp_flags);
  789. static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags);
  790. static int
  791. set_ether_config(struct eth_dev *dev, gfp_t gfp_flags)
  792. {
  793. int result = 0;
  794. struct usb_gadget *gadget = dev->gadget;
  795. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  796. /* status endpoint used for RNDIS and (optionally) CDC */
  797. if (!subset_active(dev) && dev->status_ep) {
  798. dev->status = ep_desc(gadget, &hs_status_desc,
  799. &fs_status_desc);
  800. dev->status_ep->driver_data = dev;
  801. result = usb_ep_enable(dev->status_ep, dev->status);
  802. if (result != 0) {
  803. debug("enable %s --> %d\n",
  804. dev->status_ep->name, result);
  805. goto done;
  806. }
  807. }
  808. #endif
  809. dev->in = ep_desc(gadget, &hs_source_desc, &fs_source_desc);
  810. dev->in_ep->driver_data = dev;
  811. dev->out = ep_desc(gadget, &hs_sink_desc, &fs_sink_desc);
  812. dev->out_ep->driver_data = dev;
  813. /*
  814. * With CDC, the host isn't allowed to use these two data
  815. * endpoints in the default altsetting for the interface.
  816. * so we don't activate them yet. Reset from SET_INTERFACE.
  817. *
  818. * Strictly speaking RNDIS should work the same: activation is
  819. * a side effect of setting a packet filter. Deactivation is
  820. * from REMOTE_NDIS_HALT_MSG, reset from REMOTE_NDIS_RESET_MSG.
  821. */
  822. if (!cdc_active(dev)) {
  823. result = usb_ep_enable(dev->in_ep, dev->in);
  824. if (result != 0) {
  825. debug("enable %s --> %d\n",
  826. dev->in_ep->name, result);
  827. goto done;
  828. }
  829. result = usb_ep_enable(dev->out_ep, dev->out);
  830. if (result != 0) {
  831. debug("enable %s --> %d\n",
  832. dev->out_ep->name, result);
  833. goto done;
  834. }
  835. }
  836. done:
  837. if (result == 0)
  838. result = alloc_requests(dev, qlen(gadget), gfp_flags);
  839. /* on error, disable any endpoints */
  840. if (result < 0) {
  841. if (!subset_active(dev) && dev->status_ep)
  842. (void) usb_ep_disable(dev->status_ep);
  843. dev->status = NULL;
  844. (void) usb_ep_disable(dev->in_ep);
  845. (void) usb_ep_disable(dev->out_ep);
  846. dev->in = NULL;
  847. dev->out = NULL;
  848. } else if (!cdc_active(dev)) {
  849. /*
  850. * activate non-CDC configs right away
  851. * this isn't strictly according to the RNDIS spec
  852. */
  853. eth_start(dev, GFP_ATOMIC);
  854. }
  855. /* caller is responsible for cleanup on error */
  856. return result;
  857. }
  858. static void eth_reset_config(struct eth_dev *dev)
  859. {
  860. if (dev->config == 0)
  861. return;
  862. debug("%s\n", __func__);
  863. rndis_uninit(dev->rndis_config);
  864. /*
  865. * disable endpoints, forcing (synchronous) completion of
  866. * pending i/o. then free the requests.
  867. */
  868. if (dev->in) {
  869. usb_ep_disable(dev->in_ep);
  870. if (dev->tx_req) {
  871. usb_ep_free_request(dev->in_ep, dev->tx_req);
  872. dev->tx_req = NULL;
  873. }
  874. }
  875. if (dev->out) {
  876. usb_ep_disable(dev->out_ep);
  877. if (dev->rx_req) {
  878. usb_ep_free_request(dev->out_ep, dev->rx_req);
  879. dev->rx_req = NULL;
  880. }
  881. }
  882. if (dev->status)
  883. usb_ep_disable(dev->status_ep);
  884. dev->rndis = 0;
  885. dev->cdc_filter = 0;
  886. dev->config = 0;
  887. }
  888. /*
  889. * change our operational config. must agree with the code
  890. * that returns config descriptors, and altsetting code.
  891. */
  892. static int eth_set_config(struct eth_dev *dev, unsigned number,
  893. gfp_t gfp_flags)
  894. {
  895. int result = 0;
  896. struct usb_gadget *gadget = dev->gadget;
  897. if (gadget_is_sa1100(gadget)
  898. && dev->config
  899. && dev->tx_qlen != 0) {
  900. /* tx fifo is full, but we can't clear it...*/
  901. error("can't change configurations");
  902. return -ESPIPE;
  903. }
  904. eth_reset_config(dev);
  905. switch (number) {
  906. case DEV_CONFIG_VALUE:
  907. result = set_ether_config(dev, gfp_flags);
  908. break;
  909. #ifdef CONFIG_USB_ETH_RNDIS
  910. case DEV_RNDIS_CONFIG_VALUE:
  911. dev->rndis = 1;
  912. result = set_ether_config(dev, gfp_flags);
  913. break;
  914. #endif
  915. default:
  916. result = -EINVAL;
  917. /* FALL THROUGH */
  918. case 0:
  919. break;
  920. }
  921. if (result) {
  922. if (number)
  923. eth_reset_config(dev);
  924. usb_gadget_vbus_draw(dev->gadget,
  925. gadget_is_otg(dev->gadget) ? 8 : 100);
  926. } else {
  927. char *speed;
  928. unsigned power;
  929. power = 2 * eth_config.bMaxPower;
  930. usb_gadget_vbus_draw(dev->gadget, power);
  931. switch (gadget->speed) {
  932. case USB_SPEED_FULL:
  933. speed = "full"; break;
  934. #ifdef CONFIG_USB_GADGET_DUALSPEED
  935. case USB_SPEED_HIGH:
  936. speed = "high"; break;
  937. #endif
  938. default:
  939. speed = "?"; break;
  940. }
  941. dev->config = number;
  942. printf("%s speed config #%d: %d mA, %s, using %s\n",
  943. speed, number, power, driver_desc,
  944. rndis_active(dev)
  945. ? "RNDIS"
  946. : (cdc_active(dev)
  947. ? "CDC Ethernet"
  948. : "CDC Ethernet Subset"));
  949. }
  950. return result;
  951. }
  952. /*-------------------------------------------------------------------------*/
  953. #ifdef DEV_CONFIG_CDC
  954. /*
  955. * The interrupt endpoint is used in CDC networking models (Ethernet, ATM)
  956. * only to notify the host about link status changes (which we support) or
  957. * report completion of some encapsulated command (as used in RNDIS). Since
  958. * we want this CDC Ethernet code to be vendor-neutral, we don't use that
  959. * command mechanism; and only one status request is ever queued.
  960. */
  961. static void eth_status_complete(struct usb_ep *ep, struct usb_request *req)
  962. {
  963. struct usb_cdc_notification *event = req->buf;
  964. int value = req->status;
  965. struct eth_dev *dev = ep->driver_data;
  966. /* issue the second notification if host reads the first */
  967. if (event->bNotificationType == USB_CDC_NOTIFY_NETWORK_CONNECTION
  968. && value == 0) {
  969. __le32 *data = req->buf + sizeof *event;
  970. event->bmRequestType = 0xA1;
  971. event->bNotificationType = USB_CDC_NOTIFY_SPEED_CHANGE;
  972. event->wValue = __constant_cpu_to_le16(0);
  973. event->wIndex = __constant_cpu_to_le16(1);
  974. event->wLength = __constant_cpu_to_le16(8);
  975. /* SPEED_CHANGE data is up/down speeds in bits/sec */
  976. data[0] = data[1] = cpu_to_le32(BITRATE(dev->gadget));
  977. req->length = STATUS_BYTECOUNT;
  978. value = usb_ep_queue(ep, req, GFP_ATOMIC);
  979. debug("send SPEED_CHANGE --> %d\n", value);
  980. if (value == 0)
  981. return;
  982. } else if (value != -ECONNRESET) {
  983. debug("event %02x --> %d\n",
  984. event->bNotificationType, value);
  985. if (event->bNotificationType ==
  986. USB_CDC_NOTIFY_SPEED_CHANGE) {
  987. l_ethdev.network_started = 1;
  988. printf("USB network up!\n");
  989. }
  990. }
  991. req->context = NULL;
  992. }
  993. static void issue_start_status(struct eth_dev *dev)
  994. {
  995. struct usb_request *req = dev->stat_req;
  996. struct usb_cdc_notification *event;
  997. int value;
  998. /*
  999. * flush old status
  1000. *
  1001. * FIXME ugly idiom, maybe we'd be better with just
  1002. * a "cancel the whole queue" primitive since any
  1003. * unlink-one primitive has way too many error modes.
  1004. * here, we "know" toggle is already clear...
  1005. *
  1006. * FIXME iff req->context != null just dequeue it
  1007. */
  1008. usb_ep_disable(dev->status_ep);
  1009. usb_ep_enable(dev->status_ep, dev->status);
  1010. /*
  1011. * 3.8.1 says to issue first NETWORK_CONNECTION, then
  1012. * a SPEED_CHANGE. could be useful in some configs.
  1013. */
  1014. event = req->buf;
  1015. event->bmRequestType = 0xA1;
  1016. event->bNotificationType = USB_CDC_NOTIFY_NETWORK_CONNECTION;
  1017. event->wValue = __constant_cpu_to_le16(1); /* connected */
  1018. event->wIndex = __constant_cpu_to_le16(1);
  1019. event->wLength = 0;
  1020. req->length = sizeof *event;
  1021. req->complete = eth_status_complete;
  1022. req->context = dev;
  1023. value = usb_ep_queue(dev->status_ep, req, GFP_ATOMIC);
  1024. if (value < 0)
  1025. debug("status buf queue --> %d\n", value);
  1026. }
  1027. #endif
  1028. /*-------------------------------------------------------------------------*/
  1029. static void eth_setup_complete(struct usb_ep *ep, struct usb_request *req)
  1030. {
  1031. if (req->status || req->actual != req->length)
  1032. debug("setup complete --> %d, %d/%d\n",
  1033. req->status, req->actual, req->length);
  1034. }
  1035. #ifdef CONFIG_USB_ETH_RNDIS
  1036. static void rndis_response_complete(struct usb_ep *ep, struct usb_request *req)
  1037. {
  1038. if (req->status || req->actual != req->length)
  1039. debug("rndis response complete --> %d, %d/%d\n",
  1040. req->status, req->actual, req->length);
  1041. /* done sending after USB_CDC_GET_ENCAPSULATED_RESPONSE */
  1042. }
  1043. static void rndis_command_complete(struct usb_ep *ep, struct usb_request *req)
  1044. {
  1045. struct eth_dev *dev = ep->driver_data;
  1046. int status;
  1047. /* received RNDIS command from USB_CDC_SEND_ENCAPSULATED_COMMAND */
  1048. status = rndis_msg_parser(dev->rndis_config, (u8 *) req->buf);
  1049. if (status < 0)
  1050. error("%s: rndis parse error %d", __func__, status);
  1051. }
  1052. #endif /* RNDIS */
  1053. /*
  1054. * The setup() callback implements all the ep0 functionality that's not
  1055. * handled lower down. CDC has a number of less-common features:
  1056. *
  1057. * - two interfaces: control, and ethernet data
  1058. * - Ethernet data interface has two altsettings: default, and active
  1059. * - class-specific descriptors for the control interface
  1060. * - class-specific control requests
  1061. */
  1062. static int
  1063. eth_setup(struct usb_gadget *gadget, const struct usb_ctrlrequest *ctrl)
  1064. {
  1065. struct eth_dev *dev = get_gadget_data(gadget);
  1066. struct usb_request *req = dev->req;
  1067. int value = -EOPNOTSUPP;
  1068. u16 wIndex = le16_to_cpu(ctrl->wIndex);
  1069. u16 wValue = le16_to_cpu(ctrl->wValue);
  1070. u16 wLength = le16_to_cpu(ctrl->wLength);
  1071. /*
  1072. * descriptors just go into the pre-allocated ep0 buffer,
  1073. * while config change events may enable network traffic.
  1074. */
  1075. debug("%s\n", __func__);
  1076. req->complete = eth_setup_complete;
  1077. switch (ctrl->bRequest) {
  1078. case USB_REQ_GET_DESCRIPTOR:
  1079. if (ctrl->bRequestType != USB_DIR_IN)
  1080. break;
  1081. switch (wValue >> 8) {
  1082. case USB_DT_DEVICE:
  1083. value = min(wLength, (u16) sizeof device_desc);
  1084. memcpy(req->buf, &device_desc, value);
  1085. break;
  1086. case USB_DT_DEVICE_QUALIFIER:
  1087. if (!gadget_is_dualspeed(gadget))
  1088. break;
  1089. value = min(wLength, (u16) sizeof dev_qualifier);
  1090. memcpy(req->buf, &dev_qualifier, value);
  1091. break;
  1092. case USB_DT_OTHER_SPEED_CONFIG:
  1093. if (!gadget_is_dualspeed(gadget))
  1094. break;
  1095. /* FALLTHROUGH */
  1096. case USB_DT_CONFIG:
  1097. value = config_buf(gadget, req->buf,
  1098. wValue >> 8,
  1099. wValue & 0xff,
  1100. gadget_is_otg(gadget));
  1101. if (value >= 0)
  1102. value = min(wLength, (u16) value);
  1103. break;
  1104. case USB_DT_STRING:
  1105. value = usb_gadget_get_string(&stringtab,
  1106. wValue & 0xff, req->buf);
  1107. if (value >= 0)
  1108. value = min(wLength, (u16) value);
  1109. break;
  1110. }
  1111. break;
  1112. case USB_REQ_SET_CONFIGURATION:
  1113. if (ctrl->bRequestType != 0)
  1114. break;
  1115. if (gadget->a_hnp_support)
  1116. debug("HNP available\n");
  1117. else if (gadget->a_alt_hnp_support)
  1118. debug("HNP needs a different root port\n");
  1119. value = eth_set_config(dev, wValue, GFP_ATOMIC);
  1120. break;
  1121. case USB_REQ_GET_CONFIGURATION:
  1122. if (ctrl->bRequestType != USB_DIR_IN)
  1123. break;
  1124. *(u8 *)req->buf = dev->config;
  1125. value = min(wLength, (u16) 1);
  1126. break;
  1127. case USB_REQ_SET_INTERFACE:
  1128. if (ctrl->bRequestType != USB_RECIP_INTERFACE
  1129. || !dev->config
  1130. || wIndex > 1)
  1131. break;
  1132. if (!cdc_active(dev) && wIndex != 0)
  1133. break;
  1134. /*
  1135. * PXA hardware partially handles SET_INTERFACE;
  1136. * we need to kluge around that interference.
  1137. */
  1138. if (gadget_is_pxa(gadget)) {
  1139. value = eth_set_config(dev, DEV_CONFIG_VALUE,
  1140. GFP_ATOMIC);
  1141. goto done_set_intf;
  1142. }
  1143. #ifdef DEV_CONFIG_CDC
  1144. switch (wIndex) {
  1145. case 0: /* control/master intf */
  1146. if (wValue != 0)
  1147. break;
  1148. if (dev->status) {
  1149. usb_ep_disable(dev->status_ep);
  1150. usb_ep_enable(dev->status_ep, dev->status);
  1151. }
  1152. value = 0;
  1153. break;
  1154. case 1: /* data intf */
  1155. if (wValue > 1)
  1156. break;
  1157. usb_ep_disable(dev->in_ep);
  1158. usb_ep_disable(dev->out_ep);
  1159. /*
  1160. * CDC requires the data transfers not be done from
  1161. * the default interface setting ... also, setting
  1162. * the non-default interface resets filters etc.
  1163. */
  1164. if (wValue == 1) {
  1165. if (!cdc_active(dev))
  1166. break;
  1167. usb_ep_enable(dev->in_ep, dev->in);
  1168. usb_ep_enable(dev->out_ep, dev->out);
  1169. dev->cdc_filter = DEFAULT_FILTER;
  1170. if (dev->status)
  1171. issue_start_status(dev);
  1172. eth_start(dev, GFP_ATOMIC);
  1173. }
  1174. value = 0;
  1175. break;
  1176. }
  1177. #else
  1178. /*
  1179. * FIXME this is wrong, as is the assumption that
  1180. * all non-PXA hardware talks real CDC ...
  1181. */
  1182. debug("set_interface ignored!\n");
  1183. #endif /* DEV_CONFIG_CDC */
  1184. done_set_intf:
  1185. break;
  1186. case USB_REQ_GET_INTERFACE:
  1187. if (ctrl->bRequestType != (USB_DIR_IN|USB_RECIP_INTERFACE)
  1188. || !dev->config
  1189. || wIndex > 1)
  1190. break;
  1191. if (!(cdc_active(dev) || rndis_active(dev)) && wIndex != 0)
  1192. break;
  1193. /* for CDC, iff carrier is on, data interface is active. */
  1194. if (rndis_active(dev) || wIndex != 1)
  1195. *(u8 *)req->buf = 0;
  1196. else {
  1197. /* *(u8 *)req->buf = netif_carrier_ok (dev->net) ? 1 : 0; */
  1198. /* carrier always ok ...*/
  1199. *(u8 *)req->buf = 1 ;
  1200. }
  1201. value = min(wLength, (u16) 1);
  1202. break;
  1203. #ifdef DEV_CONFIG_CDC
  1204. case USB_CDC_SET_ETHERNET_PACKET_FILTER:
  1205. /*
  1206. * see 6.2.30: no data, wIndex = interface,
  1207. * wValue = packet filter bitmap
  1208. */
  1209. if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1210. || !cdc_active(dev)
  1211. || wLength != 0
  1212. || wIndex > 1)
  1213. break;
  1214. debug("packet filter %02x\n", wValue);
  1215. dev->cdc_filter = wValue;
  1216. value = 0;
  1217. break;
  1218. /*
  1219. * and potentially:
  1220. * case USB_CDC_SET_ETHERNET_MULTICAST_FILTERS:
  1221. * case USB_CDC_SET_ETHERNET_PM_PATTERN_FILTER:
  1222. * case USB_CDC_GET_ETHERNET_PM_PATTERN_FILTER:
  1223. * case USB_CDC_GET_ETHERNET_STATISTIC:
  1224. */
  1225. #endif /* DEV_CONFIG_CDC */
  1226. #ifdef CONFIG_USB_ETH_RNDIS
  1227. /*
  1228. * RNDIS uses the CDC command encapsulation mechanism to implement
  1229. * an RPC scheme, with much getting/setting of attributes by OID.
  1230. */
  1231. case USB_CDC_SEND_ENCAPSULATED_COMMAND:
  1232. if (ctrl->bRequestType != (USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1233. || !rndis_active(dev)
  1234. || wLength > USB_BUFSIZ
  1235. || wValue
  1236. || rndis_control_intf.bInterfaceNumber
  1237. != wIndex)
  1238. break;
  1239. /* read the request, then process it */
  1240. value = wLength;
  1241. req->complete = rndis_command_complete;
  1242. /* later, rndis_control_ack () sends a notification */
  1243. break;
  1244. case USB_CDC_GET_ENCAPSULATED_RESPONSE:
  1245. if ((USB_DIR_IN|USB_TYPE_CLASS|USB_RECIP_INTERFACE)
  1246. == ctrl->bRequestType
  1247. && rndis_active(dev)
  1248. /* && wLength >= 0x0400 */
  1249. && !wValue
  1250. && rndis_control_intf.bInterfaceNumber
  1251. == wIndex) {
  1252. u8 *buf;
  1253. u32 n;
  1254. /* return the result */
  1255. buf = rndis_get_next_response(dev->rndis_config, &n);
  1256. if (buf) {
  1257. memcpy(req->buf, buf, n);
  1258. req->complete = rndis_response_complete;
  1259. rndis_free_response(dev->rndis_config, buf);
  1260. value = n;
  1261. }
  1262. /* else stalls ... spec says to avoid that */
  1263. }
  1264. break;
  1265. #endif /* RNDIS */
  1266. default:
  1267. debug("unknown control req%02x.%02x v%04x i%04x l%d\n",
  1268. ctrl->bRequestType, ctrl->bRequest,
  1269. wValue, wIndex, wLength);
  1270. }
  1271. /* respond with data transfer before status phase? */
  1272. if (value >= 0) {
  1273. debug("respond with data transfer before status phase\n");
  1274. req->length = value;
  1275. req->zero = value < wLength
  1276. && (value % gadget->ep0->maxpacket) == 0;
  1277. value = usb_ep_queue(gadget->ep0, req, GFP_ATOMIC);
  1278. if (value < 0) {
  1279. debug("ep_queue --> %d\n", value);
  1280. req->status = 0;
  1281. eth_setup_complete(gadget->ep0, req);
  1282. }
  1283. }
  1284. /* host either stalls (value < 0) or reports success */
  1285. return value;
  1286. }
  1287. /*-------------------------------------------------------------------------*/
  1288. static void rx_complete(struct usb_ep *ep, struct usb_request *req);
  1289. static int rx_submit(struct eth_dev *dev, struct usb_request *req,
  1290. gfp_t gfp_flags)
  1291. {
  1292. int retval = -ENOMEM;
  1293. size_t size;
  1294. /*
  1295. * Padding up to RX_EXTRA handles minor disagreements with host.
  1296. * Normally we use the USB "terminate on short read" convention;
  1297. * so allow up to (N*maxpacket), since that memory is normally
  1298. * already allocated. Some hardware doesn't deal well with short
  1299. * reads (e.g. DMA must be N*maxpacket), so for now don't trim a
  1300. * byte off the end (to force hardware errors on overflow).
  1301. *
  1302. * RNDIS uses internal framing, and explicitly allows senders to
  1303. * pad to end-of-packet. That's potentially nice for speed,
  1304. * but means receivers can't recover synch on their own.
  1305. */
  1306. debug("%s\n", __func__);
  1307. size = (ETHER_HDR_SIZE + dev->mtu + RX_EXTRA);
  1308. size += dev->out_ep->maxpacket - 1;
  1309. if (rndis_active(dev))
  1310. size += sizeof(struct rndis_packet_msg_type);
  1311. size -= size % dev->out_ep->maxpacket;
  1312. /*
  1313. * Some platforms perform better when IP packets are aligned,
  1314. * but on at least one, checksumming fails otherwise. Note:
  1315. * RNDIS headers involve variable numbers of LE32 values.
  1316. */
  1317. req->buf = (u8 *) NetRxPackets[0];
  1318. req->length = size;
  1319. req->complete = rx_complete;
  1320. retval = usb_ep_queue(dev->out_ep, req, gfp_flags);
  1321. if (retval)
  1322. error("rx submit --> %d", retval);
  1323. return retval;
  1324. }
  1325. static void rx_complete(struct usb_ep *ep, struct usb_request *req)
  1326. {
  1327. struct eth_dev *dev = ep->driver_data;
  1328. debug("%s: status %d\n", __func__, req->status);
  1329. switch (req->status) {
  1330. /* normal completion */
  1331. case 0:
  1332. if (rndis_active(dev)) {
  1333. /* we know MaxPacketsPerTransfer == 1 here */
  1334. int length = rndis_rm_hdr(req->buf, req->actual);
  1335. if (length < 0)
  1336. goto length_err;
  1337. req->length -= length;
  1338. req->actual -= length;
  1339. }
  1340. if (req->actual < ETH_HLEN || ETH_FRAME_LEN < req->actual) {
  1341. length_err:
  1342. dev->stats.rx_errors++;
  1343. dev->stats.rx_length_errors++;
  1344. debug("rx length %d\n", req->length);
  1345. break;
  1346. }
  1347. dev->stats.rx_packets++;
  1348. dev->stats.rx_bytes += req->length;
  1349. break;
  1350. /* software-driven interface shutdown */
  1351. case -ECONNRESET: /* unlink */
  1352. case -ESHUTDOWN: /* disconnect etc */
  1353. /* for hardware automagic (such as pxa) */
  1354. case -ECONNABORTED: /* endpoint reset */
  1355. break;
  1356. /* data overrun */
  1357. case -EOVERFLOW:
  1358. dev->stats.rx_over_errors++;
  1359. /* FALLTHROUGH */
  1360. default:
  1361. dev->stats.rx_errors++;
  1362. break;
  1363. }
  1364. packet_received = 1;
  1365. }
  1366. static int alloc_requests(struct eth_dev *dev, unsigned n, gfp_t gfp_flags)
  1367. {
  1368. dev->tx_req = usb_ep_alloc_request(dev->in_ep, 0);
  1369. if (!dev->tx_req)
  1370. goto fail1;
  1371. dev->rx_req = usb_ep_alloc_request(dev->out_ep, 0);
  1372. if (!dev->rx_req)
  1373. goto fail2;
  1374. return 0;
  1375. fail2:
  1376. usb_ep_free_request(dev->in_ep, dev->tx_req);
  1377. fail1:
  1378. error("can't alloc requests");
  1379. return -1;
  1380. }
  1381. static void tx_complete(struct usb_ep *ep, struct usb_request *req)
  1382. {
  1383. struct eth_dev *dev = ep->driver_data;
  1384. debug("%s: status %s\n", __func__, (req->status) ? "failed" : "ok");
  1385. switch (req->status) {
  1386. default:
  1387. dev->stats.tx_errors++;
  1388. debug("tx err %d\n", req->status);
  1389. /* FALLTHROUGH */
  1390. case -ECONNRESET: /* unlink */
  1391. case -ESHUTDOWN: /* disconnect etc */
  1392. break;
  1393. case 0:
  1394. dev->stats.tx_bytes += req->length;
  1395. }
  1396. dev->stats.tx_packets++;
  1397. packet_sent = 1;
  1398. }
  1399. static inline int eth_is_promisc(struct eth_dev *dev)
  1400. {
  1401. /* no filters for the CDC subset; always promisc */
  1402. if (subset_active(dev))
  1403. return 1;
  1404. return dev->cdc_filter & USB_CDC_PACKET_TYPE_PROMISCUOUS;
  1405. }
  1406. #if 0
  1407. static int eth_start_xmit (struct sk_buff *skb, struct net_device *net)
  1408. {
  1409. struct eth_dev *dev = netdev_priv(net);
  1410. int length = skb->len;
  1411. int retval;
  1412. struct usb_request *req = NULL;
  1413. unsigned long flags;
  1414. /* apply outgoing CDC or RNDIS filters */
  1415. if (!eth_is_promisc (dev)) {
  1416. u8 *dest = skb->data;
  1417. if (is_multicast_ether_addr(dest)) {
  1418. u16 type;
  1419. /* ignores USB_CDC_PACKET_TYPE_MULTICAST and host
  1420. * SET_ETHERNET_MULTICAST_FILTERS requests
  1421. */
  1422. if (is_broadcast_ether_addr(dest))
  1423. type = USB_CDC_PACKET_TYPE_BROADCAST;
  1424. else
  1425. type = USB_CDC_PACKET_TYPE_ALL_MULTICAST;
  1426. if (!(dev->cdc_filter & type)) {
  1427. dev_kfree_skb_any (skb);
  1428. return 0;
  1429. }
  1430. }
  1431. /* ignores USB_CDC_PACKET_TYPE_DIRECTED */
  1432. }
  1433. spin_lock_irqsave(&dev->req_lock, flags);
  1434. /*
  1435. * this freelist can be empty if an interrupt triggered disconnect()
  1436. * and reconfigured the gadget (shutting down this queue) after the
  1437. * network stack decided to xmit but before we got the spinlock.
  1438. */
  1439. if (list_empty(&dev->tx_reqs)) {
  1440. spin_unlock_irqrestore(&dev->req_lock, flags);
  1441. return 1;
  1442. }
  1443. req = container_of (dev->tx_reqs.next, struct usb_request, list);
  1444. list_del (&req->list);
  1445. /* temporarily stop TX queue when the freelist empties */
  1446. if (list_empty (&dev->tx_reqs))
  1447. netif_stop_queue (net);
  1448. spin_unlock_irqrestore(&dev->req_lock, flags);
  1449. /* no buffer copies needed, unless the network stack did it
  1450. * or the hardware can't use skb buffers.
  1451. * or there's not enough space for any RNDIS headers we need
  1452. */
  1453. if (rndis_active(dev)) {
  1454. struct sk_buff *skb_rndis;
  1455. skb_rndis = skb_realloc_headroom (skb,
  1456. sizeof (struct rndis_packet_msg_type));
  1457. if (!skb_rndis)
  1458. goto drop;
  1459. dev_kfree_skb_any (skb);
  1460. skb = skb_rndis;
  1461. rndis_add_hdr (skb);
  1462. length = skb->len;
  1463. }
  1464. req->buf = skb->data;
  1465. req->context = skb;
  1466. req->complete = tx_complete;
  1467. /* use zlp framing on tx for strict CDC-Ether conformance,
  1468. * though any robust network rx path ignores extra padding.
  1469. * and some hardware doesn't like to write zlps.
  1470. */
  1471. req->zero = 1;
  1472. if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
  1473. length++;
  1474. req->length = length;
  1475. /* throttle highspeed IRQ rate back slightly */
  1476. if (gadget_is_dualspeed(dev->gadget))
  1477. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
  1478. ? ((atomic_read(&dev->tx_qlen) % qmult) != 0)
  1479. : 0;
  1480. retval = usb_ep_queue (dev->in_ep, req, GFP_ATOMIC);
  1481. switch (retval) {
  1482. default:
  1483. DEBUG (dev, "tx queue err %d\n", retval);
  1484. break;
  1485. case 0:
  1486. net->trans_start = jiffies;
  1487. atomic_inc (&dev->tx_qlen);
  1488. }
  1489. if (retval) {
  1490. drop:
  1491. dev->stats.tx_dropped++;
  1492. dev_kfree_skb_any (skb);
  1493. spin_lock_irqsave(&dev->req_lock, flags);
  1494. if (list_empty (&dev->tx_reqs))
  1495. netif_start_queue (net);
  1496. list_add (&req->list, &dev->tx_reqs);
  1497. spin_unlock_irqrestore(&dev->req_lock, flags);
  1498. }
  1499. return 0;
  1500. }
  1501. /*-------------------------------------------------------------------------*/
  1502. #endif
  1503. static void eth_unbind(struct usb_gadget *gadget)
  1504. {
  1505. struct eth_dev *dev = get_gadget_data(gadget);
  1506. debug("%s...\n", __func__);
  1507. rndis_deregister(dev->rndis_config);
  1508. rndis_exit();
  1509. /* we've already been disconnected ... no i/o is active */
  1510. if (dev->req) {
  1511. usb_ep_free_request(gadget->ep0, dev->req);
  1512. dev->req = NULL;
  1513. }
  1514. if (dev->stat_req) {
  1515. usb_ep_free_request(dev->status_ep, dev->stat_req);
  1516. dev->stat_req = NULL;
  1517. }
  1518. if (dev->tx_req) {
  1519. usb_ep_free_request(dev->in_ep, dev->tx_req);
  1520. dev->tx_req = NULL;
  1521. }
  1522. if (dev->rx_req) {
  1523. usb_ep_free_request(dev->out_ep, dev->rx_req);
  1524. dev->rx_req = NULL;
  1525. }
  1526. /* unregister_netdev (dev->net);*/
  1527. /* free_netdev(dev->net);*/
  1528. dev->gadget = NULL;
  1529. set_gadget_data(gadget, NULL);
  1530. }
  1531. static void eth_disconnect(struct usb_gadget *gadget)
  1532. {
  1533. eth_reset_config(get_gadget_data(gadget));
  1534. /* FIXME RNDIS should enter RNDIS_UNINITIALIZED */
  1535. }
  1536. static void eth_suspend(struct usb_gadget *gadget)
  1537. {
  1538. /* Not used */
  1539. }
  1540. static void eth_resume(struct usb_gadget *gadget)
  1541. {
  1542. /* Not used */
  1543. }
  1544. /*-------------------------------------------------------------------------*/
  1545. #ifdef CONFIG_USB_ETH_RNDIS
  1546. /*
  1547. * The interrupt endpoint is used in RNDIS to notify the host when messages
  1548. * other than data packets are available ... notably the REMOTE_NDIS_*_CMPLT
  1549. * messages, but also REMOTE_NDIS_INDICATE_STATUS_MSG and potentially even
  1550. * REMOTE_NDIS_KEEPALIVE_MSG.
  1551. *
  1552. * The RNDIS control queue is processed by GET_ENCAPSULATED_RESPONSE, and
  1553. * normally just one notification will be queued.
  1554. */
  1555. static void rndis_control_ack_complete(struct usb_ep *ep,
  1556. struct usb_request *req)
  1557. {
  1558. struct eth_dev *dev = ep->driver_data;
  1559. debug("%s...\n", __func__);
  1560. if (req->status || req->actual != req->length)
  1561. debug("rndis control ack complete --> %d, %d/%d\n",
  1562. req->status, req->actual, req->length);
  1563. if (!l_ethdev.network_started) {
  1564. if (rndis_get_state(dev->rndis_config)
  1565. == RNDIS_DATA_INITIALIZED) {
  1566. l_ethdev.network_started = 1;
  1567. printf("USB RNDIS network up!\n");
  1568. }
  1569. }
  1570. req->context = NULL;
  1571. if (req != dev->stat_req)
  1572. usb_ep_free_request(ep, req);
  1573. }
  1574. static char rndis_resp_buf[8] __attribute__((aligned(sizeof(__le32))));
  1575. static int rndis_control_ack(struct eth_device *net)
  1576. {
  1577. struct eth_dev *dev = &l_ethdev;
  1578. int length;
  1579. struct usb_request *resp = dev->stat_req;
  1580. /* in case RNDIS calls this after disconnect */
  1581. if (!dev->status) {
  1582. debug("status ENODEV\n");
  1583. return -ENODEV;
  1584. }
  1585. /* in case queue length > 1 */
  1586. if (resp->context) {
  1587. resp = usb_ep_alloc_request(dev->status_ep, GFP_ATOMIC);
  1588. if (!resp)
  1589. return -ENOMEM;
  1590. resp->buf = rndis_resp_buf;
  1591. }
  1592. /*
  1593. * Send RNDIS RESPONSE_AVAILABLE notification;
  1594. * USB_CDC_NOTIFY_RESPONSE_AVAILABLE should work too
  1595. */
  1596. resp->length = 8;
  1597. resp->complete = rndis_control_ack_complete;
  1598. resp->context = dev;
  1599. *((__le32 *) resp->buf) = __constant_cpu_to_le32(1);
  1600. *((__le32 *) (resp->buf + 4)) = __constant_cpu_to_le32(0);
  1601. length = usb_ep_queue(dev->status_ep, resp, GFP_ATOMIC);
  1602. if (length < 0) {
  1603. resp->status = 0;
  1604. rndis_control_ack_complete(dev->status_ep, resp);
  1605. }
  1606. return 0;
  1607. }
  1608. #else
  1609. #define rndis_control_ack NULL
  1610. #endif /* RNDIS */
  1611. static void eth_start(struct eth_dev *dev, gfp_t gfp_flags)
  1612. {
  1613. if (rndis_active(dev)) {
  1614. rndis_set_param_medium(dev->rndis_config,
  1615. NDIS_MEDIUM_802_3,
  1616. BITRATE(dev->gadget)/100);
  1617. rndis_signal_connect(dev->rndis_config);
  1618. }
  1619. }
  1620. static int eth_stop(struct eth_dev *dev)
  1621. {
  1622. #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
  1623. unsigned long ts;
  1624. unsigned long timeout = CONFIG_SYS_HZ; /* 1 sec to stop RNDIS */
  1625. #endif
  1626. if (rndis_active(dev)) {
  1627. rndis_set_param_medium(dev->rndis_config, NDIS_MEDIUM_802_3, 0);
  1628. rndis_signal_disconnect(dev->rndis_config);
  1629. #ifdef RNDIS_COMPLETE_SIGNAL_DISCONNECT
  1630. /* Wait until host receives OID_GEN_MEDIA_CONNECT_STATUS */
  1631. ts = get_timer(0);
  1632. while (get_timer(ts) < timeout)
  1633. usb_gadget_handle_interrupts();
  1634. #endif
  1635. rndis_uninit(dev->rndis_config);
  1636. dev->rndis = 0;
  1637. }
  1638. return 0;
  1639. }
  1640. /*-------------------------------------------------------------------------*/
  1641. static int is_eth_addr_valid(char *str)
  1642. {
  1643. if (strlen(str) == 17) {
  1644. int i;
  1645. char *p, *q;
  1646. uchar ea[6];
  1647. /* see if it looks like an ethernet address */
  1648. p = str;
  1649. for (i = 0; i < 6; i++) {
  1650. char term = (i == 5 ? '\0' : ':');
  1651. ea[i] = simple_strtol(p, &q, 16);
  1652. if ((q - p) != 2 || *q++ != term)
  1653. break;
  1654. p = q;
  1655. }
  1656. if (i == 6) /* it looks ok */
  1657. return 1;
  1658. }
  1659. return 0;
  1660. }
  1661. static u8 nibble(unsigned char c)
  1662. {
  1663. if (likely(isdigit(c)))
  1664. return c - '0';
  1665. c = toupper(c);
  1666. if (likely(isxdigit(c)))
  1667. return 10 + c - 'A';
  1668. return 0;
  1669. }
  1670. static int get_ether_addr(const char *str, u8 *dev_addr)
  1671. {
  1672. if (str) {
  1673. unsigned i;
  1674. for (i = 0; i < 6; i++) {
  1675. unsigned char num;
  1676. if ((*str == '.') || (*str == ':'))
  1677. str++;
  1678. num = nibble(*str++) << 4;
  1679. num |= (nibble(*str++));
  1680. dev_addr[i] = num;
  1681. }
  1682. if (is_valid_ether_addr(dev_addr))
  1683. return 0;
  1684. }
  1685. return 1;
  1686. }
  1687. static int eth_bind(struct usb_gadget *gadget)
  1688. {
  1689. struct eth_dev *dev = &l_ethdev;
  1690. u8 cdc = 1, zlp = 1, rndis = 1;
  1691. struct usb_ep *in_ep, *out_ep, *status_ep = NULL;
  1692. int status = -ENOMEM;
  1693. int gcnum;
  1694. u8 tmp[7];
  1695. /* these flags are only ever cleared; compiler take note */
  1696. #ifndef DEV_CONFIG_CDC
  1697. cdc = 0;
  1698. #endif
  1699. #ifndef CONFIG_USB_ETH_RNDIS
  1700. rndis = 0;
  1701. #endif
  1702. /*
  1703. * Because most host side USB stacks handle CDC Ethernet, that
  1704. * standard protocol is _strongly_ preferred for interop purposes.
  1705. * (By everyone except Microsoft.)
  1706. */
  1707. if (gadget_is_pxa(gadget)) {
  1708. /* pxa doesn't support altsettings */
  1709. cdc = 0;
  1710. } else if (gadget_is_musbhdrc(gadget)) {
  1711. /* reduce tx dma overhead by avoiding special cases */
  1712. zlp = 0;
  1713. } else if (gadget_is_sh(gadget)) {
  1714. /* sh doesn't support multiple interfaces or configs */
  1715. cdc = 0;
  1716. rndis = 0;
  1717. } else if (gadget_is_sa1100(gadget)) {
  1718. /* hardware can't write zlps */
  1719. zlp = 0;
  1720. /*
  1721. * sa1100 CAN do CDC, without status endpoint ... we use
  1722. * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
  1723. */
  1724. cdc = 0;
  1725. }
  1726. gcnum = usb_gadget_controller_number(gadget);
  1727. if (gcnum >= 0)
  1728. device_desc.bcdDevice = cpu_to_le16(0x0300 + gcnum);
  1729. else {
  1730. /*
  1731. * can't assume CDC works. don't want to default to
  1732. * anything less functional on CDC-capable hardware,
  1733. * so we fail in this case.
  1734. */
  1735. error("controller '%s' not recognized",
  1736. gadget->name);
  1737. return -ENODEV;
  1738. }
  1739. /*
  1740. * If there's an RNDIS configuration, that's what Windows wants to
  1741. * be using ... so use these product IDs here and in the "linux.inf"
  1742. * needed to install MSFT drivers. Current Linux kernels will use
  1743. * the second configuration if it's CDC Ethernet, and need some help
  1744. * to choose the right configuration otherwise.
  1745. */
  1746. if (rndis) {
  1747. #if defined(CONFIG_USB_RNDIS_VENDOR_ID) && defined(CONFIG_USB_RNDIS_PRODUCT_ID)
  1748. device_desc.idVendor =
  1749. __constant_cpu_to_le16(CONFIG_USB_RNDIS_VENDOR_ID);
  1750. device_desc.idProduct =
  1751. __constant_cpu_to_le16(CONFIG_USB_RNDIS_PRODUCT_ID);
  1752. #else
  1753. device_desc.idVendor =
  1754. __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
  1755. device_desc.idProduct =
  1756. __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
  1757. #endif
  1758. sprintf(product_desc, "RNDIS/%s", driver_desc);
  1759. /*
  1760. * CDC subset ... recognized by Linux since 2.4.10, but Windows
  1761. * drivers aren't widely available. (That may be improved by
  1762. * supporting one submode of the "SAFE" variant of MDLM.)
  1763. */
  1764. } else {
  1765. #if defined(CONFIG_USB_CDC_VENDOR_ID) && defined(CONFIG_USB_CDC_PRODUCT_ID)
  1766. device_desc.idVendor = cpu_to_le16(CONFIG_USB_CDC_VENDOR_ID);
  1767. device_desc.idProduct = cpu_to_le16(CONFIG_USB_CDC_PRODUCT_ID);
  1768. #else
  1769. if (!cdc) {
  1770. device_desc.idVendor =
  1771. __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
  1772. device_desc.idProduct =
  1773. __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
  1774. }
  1775. #endif
  1776. }
  1777. /* support optional vendor/distro customization */
  1778. if (bcdDevice)
  1779. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1780. if (iManufacturer)
  1781. strlcpy(manufacturer, iManufacturer, sizeof manufacturer);
  1782. if (iProduct)
  1783. strlcpy(product_desc, iProduct, sizeof product_desc);
  1784. if (iSerialNumber) {
  1785. device_desc.iSerialNumber = STRING_SERIALNUMBER,
  1786. strlcpy(serial_number, iSerialNumber, sizeof serial_number);
  1787. }
  1788. /* all we really need is bulk IN/OUT */
  1789. usb_ep_autoconfig_reset(gadget);
  1790. in_ep = usb_ep_autoconfig(gadget, &fs_source_desc);
  1791. if (!in_ep) {
  1792. autoconf_fail:
  1793. error("can't autoconfigure on %s\n",
  1794. gadget->name);
  1795. return -ENODEV;
  1796. }
  1797. in_ep->driver_data = in_ep; /* claim */
  1798. out_ep = usb_ep_autoconfig(gadget, &fs_sink_desc);
  1799. if (!out_ep)
  1800. goto autoconf_fail;
  1801. out_ep->driver_data = out_ep; /* claim */
  1802. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1803. /*
  1804. * CDC Ethernet control interface doesn't require a status endpoint.
  1805. * Since some hosts expect one, try to allocate one anyway.
  1806. */
  1807. if (cdc || rndis) {
  1808. status_ep = usb_ep_autoconfig(gadget, &fs_status_desc);
  1809. if (status_ep) {
  1810. status_ep->driver_data = status_ep; /* claim */
  1811. } else if (rndis) {
  1812. error("can't run RNDIS on %s", gadget->name);
  1813. return -ENODEV;
  1814. #ifdef DEV_CONFIG_CDC
  1815. } else if (cdc) {
  1816. control_intf.bNumEndpoints = 0;
  1817. /* FIXME remove endpoint from descriptor list */
  1818. #endif
  1819. }
  1820. }
  1821. #endif
  1822. /* one config: cdc, else minimal subset */
  1823. if (!cdc) {
  1824. eth_config.bNumInterfaces = 1;
  1825. eth_config.iConfiguration = STRING_SUBSET;
  1826. /*
  1827. * use functions to set these up, in case we're built to work
  1828. * with multiple controllers and must override CDC Ethernet.
  1829. */
  1830. fs_subset_descriptors();
  1831. hs_subset_descriptors();
  1832. }
  1833. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1834. usb_gadget_set_selfpowered(gadget);
  1835. /* For now RNDIS is always a second config */
  1836. if (rndis)
  1837. device_desc.bNumConfigurations = 2;
  1838. if (gadget_is_dualspeed(gadget)) {
  1839. if (rndis)
  1840. dev_qualifier.bNumConfigurations = 2;
  1841. else if (!cdc)
  1842. dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  1843. /* assumes ep0 uses the same value for both speeds ... */
  1844. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1845. /* and that all endpoints are dual-speed */
  1846. hs_source_desc.bEndpointAddress =
  1847. fs_source_desc.bEndpointAddress;
  1848. hs_sink_desc.bEndpointAddress =
  1849. fs_sink_desc.bEndpointAddress;
  1850. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1851. if (status_ep)
  1852. hs_status_desc.bEndpointAddress =
  1853. fs_status_desc.bEndpointAddress;
  1854. #endif
  1855. }
  1856. if (gadget_is_otg(gadget)) {
  1857. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1858. eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1859. eth_config.bMaxPower = 4;
  1860. #ifdef CONFIG_USB_ETH_RNDIS
  1861. rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1862. rndis_config.bMaxPower = 4;
  1863. #endif
  1864. }
  1865. /* network device setup */
  1866. dev->net = &l_netdev;
  1867. dev->cdc = cdc;
  1868. dev->zlp = zlp;
  1869. dev->in_ep = in_ep;
  1870. dev->out_ep = out_ep;
  1871. dev->status_ep = status_ep;
  1872. /*
  1873. * Module params for these addresses should come from ID proms.
  1874. * The host side address is used with CDC and RNDIS, and commonly
  1875. * ends up in a persistent config database. It's not clear if
  1876. * host side code for the SAFE thing cares -- its original BLAN
  1877. * thing didn't, Sharp never assigned those addresses on Zaurii.
  1878. */
  1879. get_ether_addr(dev_addr, dev->net->enetaddr);
  1880. memset(tmp, 0, sizeof(tmp));
  1881. memcpy(tmp, dev->net->enetaddr, sizeof(dev->net->enetaddr));
  1882. get_ether_addr(host_addr, dev->host_mac);
  1883. sprintf(ethaddr, "%02X%02X%02X%02X%02X%02X",
  1884. dev->host_mac[0], dev->host_mac[1],
  1885. dev->host_mac[2], dev->host_mac[3],
  1886. dev->host_mac[4], dev->host_mac[5]);
  1887. if (rndis) {
  1888. status = rndis_init();
  1889. if (status < 0) {
  1890. error("can't init RNDIS, %d", status);
  1891. goto fail;
  1892. }
  1893. }
  1894. /*
  1895. * use PKTSIZE (or aligned... from u-boot) and set
  1896. * wMaxSegmentSize accordingly
  1897. */
  1898. dev->mtu = PKTSIZE_ALIGN; /* RNDIS does not like this, only 1514, TODO*/
  1899. /* preallocate control message data and buffer */
  1900. dev->req = usb_ep_alloc_request(gadget->ep0, GFP_KERNEL);
  1901. if (!dev->req)
  1902. goto fail;
  1903. dev->req->buf = control_req;
  1904. dev->req->complete = eth_setup_complete;
  1905. /* ... and maybe likewise for status transfer */
  1906. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1907. if (dev->status_ep) {
  1908. dev->stat_req = usb_ep_alloc_request(dev->status_ep,
  1909. GFP_KERNEL);
  1910. if (!dev->stat_req) {
  1911. usb_ep_free_request(dev->status_ep, dev->req);
  1912. goto fail;
  1913. }
  1914. dev->stat_req->buf = status_req;
  1915. dev->stat_req->context = NULL;
  1916. }
  1917. #endif
  1918. /* finish hookup to lower layer ... */
  1919. dev->gadget = gadget;
  1920. set_gadget_data(gadget, dev);
  1921. gadget->ep0->driver_data = dev;
  1922. /*
  1923. * two kinds of host-initiated state changes:
  1924. * - iff DATA transfer is active, carrier is "on"
  1925. * - tx queueing enabled if open *and* carrier is "on"
  1926. */
  1927. printf("using %s, OUT %s IN %s%s%s\n", gadget->name,
  1928. out_ep->name, in_ep->name,
  1929. status_ep ? " STATUS " : "",
  1930. status_ep ? status_ep->name : ""
  1931. );
  1932. printf("MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1933. dev->net->enetaddr[0], dev->net->enetaddr[1],
  1934. dev->net->enetaddr[2], dev->net->enetaddr[3],
  1935. dev->net->enetaddr[4], dev->net->enetaddr[5]);
  1936. if (cdc || rndis)
  1937. printf("HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  1938. dev->host_mac[0], dev->host_mac[1],
  1939. dev->host_mac[2], dev->host_mac[3],
  1940. dev->host_mac[4], dev->host_mac[5]);
  1941. if (rndis) {
  1942. u32 vendorID = 0;
  1943. /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
  1944. dev->rndis_config = rndis_register(rndis_control_ack);
  1945. if (dev->rndis_config < 0) {
  1946. fail0:
  1947. eth_unbind(gadget);
  1948. debug("RNDIS setup failed\n");
  1949. status = -ENODEV;
  1950. goto fail;
  1951. }
  1952. /* these set up a lot of the OIDs that RNDIS needs */
  1953. rndis_set_host_mac(dev->rndis_config, dev->host_mac);
  1954. if (rndis_set_param_dev(dev->rndis_config, dev->net, dev->mtu,
  1955. &dev->stats, &dev->cdc_filter))
  1956. goto fail0;
  1957. if (rndis_set_param_vendor(dev->rndis_config, vendorID,
  1958. manufacturer))
  1959. goto fail0;
  1960. if (rndis_set_param_medium(dev->rndis_config,
  1961. NDIS_MEDIUM_802_3, 0))
  1962. goto fail0;
  1963. printf("RNDIS ready\n");
  1964. }
  1965. return 0;
  1966. fail:
  1967. error("%s failed, status = %d", __func__, status);
  1968. eth_unbind(gadget);
  1969. return status;
  1970. }
  1971. /*-------------------------------------------------------------------------*/
  1972. static int usb_eth_init(struct eth_device *netdev, bd_t *bd)
  1973. {
  1974. struct eth_dev *dev = &l_ethdev;
  1975. struct usb_gadget *gadget;
  1976. unsigned long ts;
  1977. unsigned long timeout = USB_CONNECT_TIMEOUT;
  1978. if (!netdev) {
  1979. error("received NULL ptr");
  1980. goto fail;
  1981. }
  1982. /* Configure default mac-addresses for the USB ethernet device */
  1983. #ifdef CONFIG_USBNET_DEV_ADDR
  1984. strlcpy(dev_addr, CONFIG_USBNET_DEV_ADDR, sizeof(dev_addr));
  1985. #endif
  1986. #ifdef CONFIG_USBNET_HOST_ADDR
  1987. strlcpy(host_addr, CONFIG_USBNET_HOST_ADDR, sizeof(host_addr));
  1988. #endif
  1989. /* Check if the user overruled the MAC addresses */
  1990. if (getenv("usbnet_devaddr"))
  1991. strlcpy(dev_addr, getenv("usbnet_devaddr"),
  1992. sizeof(dev_addr));
  1993. if (getenv("usbnet_hostaddr"))
  1994. strlcpy(host_addr, getenv("usbnet_hostaddr"),
  1995. sizeof(host_addr));
  1996. if (!is_eth_addr_valid(dev_addr)) {
  1997. error("Need valid 'usbnet_devaddr' to be set");
  1998. goto fail;
  1999. }
  2000. if (!is_eth_addr_valid(host_addr)) {
  2001. error("Need valid 'usbnet_hostaddr' to be set");
  2002. goto fail;
  2003. }
  2004. if (usb_gadget_register_driver(&eth_driver) < 0)
  2005. goto fail;
  2006. dev->network_started = 0;
  2007. packet_received = 0;
  2008. packet_sent = 0;
  2009. gadget = dev->gadget;
  2010. usb_gadget_connect(gadget);
  2011. if (getenv("cdc_connect_timeout"))
  2012. timeout = simple_strtoul(getenv("cdc_connect_timeout"),
  2013. NULL, 10) * CONFIG_SYS_HZ;
  2014. ts = get_timer(0);
  2015. while (!l_ethdev.network_started) {
  2016. /* Handle control-c and timeouts */
  2017. if (ctrlc() || (get_timer(ts) > timeout)) {
  2018. error("The remote end did not respond in time.");
  2019. goto fail;
  2020. }
  2021. usb_gadget_handle_interrupts();
  2022. }
  2023. packet_received = 0;
  2024. rx_submit(dev, dev->rx_req, 0);
  2025. return 0;
  2026. fail:
  2027. return -1;
  2028. }
  2029. static int usb_eth_send(struct eth_device *netdev,
  2030. volatile void *packet, int length)
  2031. {
  2032. int retval;
  2033. void *rndis_pkt = NULL;
  2034. struct eth_dev *dev = &l_ethdev;
  2035. struct usb_request *req = dev->tx_req;
  2036. unsigned long ts;
  2037. unsigned long timeout = USB_CONNECT_TIMEOUT;
  2038. debug("%s:...\n", __func__);
  2039. /* new buffer is needed to include RNDIS header */
  2040. if (rndis_active(dev)) {
  2041. rndis_pkt = malloc(length +
  2042. sizeof(struct rndis_packet_msg_type));
  2043. if (!rndis_pkt) {
  2044. error("No memory to alloc RNDIS packet");
  2045. goto drop;
  2046. }
  2047. rndis_add_hdr(rndis_pkt, length);
  2048. memcpy(rndis_pkt + sizeof(struct rndis_packet_msg_type),
  2049. (void *)packet, length);
  2050. packet = rndis_pkt;
  2051. length += sizeof(struct rndis_packet_msg_type);
  2052. }
  2053. req->buf = (void *)packet;
  2054. req->context = NULL;
  2055. req->complete = tx_complete;
  2056. /*
  2057. * use zlp framing on tx for strict CDC-Ether conformance,
  2058. * though any robust network rx path ignores extra padding.
  2059. * and some hardware doesn't like to write zlps.
  2060. */
  2061. req->zero = 1;
  2062. if (!dev->zlp && (length % dev->in_ep->maxpacket) == 0)
  2063. length++;
  2064. req->length = length;
  2065. #if 0
  2066. /* throttle highspeed IRQ rate back slightly */
  2067. if (gadget_is_dualspeed(dev->gadget))
  2068. req->no_interrupt = (dev->gadget->speed == USB_SPEED_HIGH)
  2069. ? ((dev->tx_qlen % qmult) != 0) : 0;
  2070. #endif
  2071. dev->tx_qlen = 1;
  2072. ts = get_timer(0);
  2073. packet_sent = 0;
  2074. retval = usb_ep_queue(dev->in_ep, req, GFP_ATOMIC);
  2075. if (!retval)
  2076. debug("%s: packet queued\n", __func__);
  2077. while (!packet_sent) {
  2078. if (get_timer(ts) > timeout) {
  2079. printf("timeout sending packets to usb ethernet\n");
  2080. return -1;
  2081. }
  2082. usb_gadget_handle_interrupts();
  2083. }
  2084. if (rndis_pkt)
  2085. free(rndis_pkt);
  2086. return 0;
  2087. drop:
  2088. dev->stats.tx_dropped++;
  2089. return -ENOMEM;
  2090. }
  2091. static int usb_eth_recv(struct eth_device *netdev)
  2092. {
  2093. struct eth_dev *dev = &l_ethdev;
  2094. usb_gadget_handle_interrupts();
  2095. if (packet_received) {
  2096. debug("%s: packet received\n", __func__);
  2097. if (dev->rx_req) {
  2098. NetReceive(NetRxPackets[0], dev->rx_req->length);
  2099. packet_received = 0;
  2100. rx_submit(dev, dev->rx_req, 0);
  2101. } else
  2102. error("dev->rx_req invalid");
  2103. }
  2104. return 0;
  2105. }
  2106. void usb_eth_halt(struct eth_device *netdev)
  2107. {
  2108. struct eth_dev *dev = &l_ethdev;
  2109. if (!netdev) {
  2110. error("received NULL ptr");
  2111. return;
  2112. }
  2113. /* If the gadget not registered, simple return */
  2114. if (!dev->gadget)
  2115. return;
  2116. /*
  2117. * Some USB controllers may need additional deinitialization here
  2118. * before dropping pull-up (also due to hardware issues).
  2119. * For example: unhandled interrupt with status stage started may
  2120. * bring the controller to fully broken state (until board reset).
  2121. * There are some variants to debug and fix such cases:
  2122. * 1) In the case of RNDIS connection eth_stop can perform additional
  2123. * interrupt handling. See RNDIS_COMPLETE_SIGNAL_DISCONNECT definition.
  2124. * 2) 'pullup' callback in your UDC driver can be improved to perform
  2125. * this deinitialization.
  2126. */
  2127. eth_stop(dev);
  2128. usb_gadget_disconnect(dev->gadget);
  2129. /* Clear pending interrupt */
  2130. if (dev->network_started) {
  2131. usb_gadget_handle_interrupts();
  2132. dev->network_started = 0;
  2133. }
  2134. usb_gadget_unregister_driver(&eth_driver);
  2135. }
  2136. static struct usb_gadget_driver eth_driver = {
  2137. .speed = DEVSPEED,
  2138. .bind = eth_bind,
  2139. .unbind = eth_unbind,
  2140. .setup = eth_setup,
  2141. .disconnect = eth_disconnect,
  2142. .suspend = eth_suspend,
  2143. .resume = eth_resume,
  2144. };
  2145. int usb_eth_initialize(bd_t *bi)
  2146. {
  2147. struct eth_device *netdev = &l_netdev;
  2148. strlcpy(netdev->name, USB_NET_NAME, sizeof(netdev->name));
  2149. netdev->init = usb_eth_init;
  2150. netdev->send = usb_eth_send;
  2151. netdev->recv = usb_eth_recv;
  2152. netdev->halt = usb_eth_halt;
  2153. #ifdef CONFIG_MCAST_TFTP
  2154. #error not supported
  2155. #endif
  2156. eth_register(netdev);
  2157. return 0;
  2158. }