ether.c 68 KB

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