ether.c 71 KB

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