ether.c 72 KB

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