ether.c 68 KB

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