ether.c 69 KB

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