ether.c 69 KB

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