ether.c 72 KB

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