ether.c 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558
  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. int gcnum;
  1827. /* these flags are only ever cleared; compiler take note */
  1828. #ifndef DEV_CONFIG_CDC
  1829. cdc = 0;
  1830. #endif
  1831. #ifndef CONFIG_USB_ETH_RNDIS
  1832. rndis = 0;
  1833. #endif
  1834. /* Because most host side USB stacks handle CDC Ethernet, that
  1835. * standard protocol is _strongly_ preferred for interop purposes.
  1836. * (By everyone except Microsoft.)
  1837. */
  1838. if (gadget_is_pxa (gadget)) {
  1839. /* pxa doesn't support altsettings */
  1840. cdc = 0;
  1841. } else if (gadget_is_sh(gadget)) {
  1842. /* sh doesn't support multiple interfaces or configs */
  1843. cdc = 0;
  1844. rndis = 0;
  1845. } else if (gadget_is_sa1100 (gadget)) {
  1846. /* hardware can't write zlps */
  1847. zlp = 0;
  1848. /* sa1100 CAN do CDC, without status endpoint ... we use
  1849. * non-CDC to be compatible with ARM Linux-2.4 "usb-eth".
  1850. */
  1851. cdc = 0;
  1852. }
  1853. gcnum = usb_gadget_controller_number (gadget);
  1854. if (gcnum >= 0)
  1855. device_desc.bcdDevice = cpu_to_le16 (0x0200 + gcnum);
  1856. else {
  1857. /* can't assume CDC works. don't want to default to
  1858. * anything less functional on CDC-capable hardware,
  1859. * so we fail in this case.
  1860. */
  1861. dev_err (&gadget->dev,
  1862. "controller '%s' not recognized\n",
  1863. gadget->name);
  1864. return -ENODEV;
  1865. }
  1866. snprintf (manufacturer, sizeof manufacturer, "%s %s/%s",
  1867. system_utsname.sysname, system_utsname.release,
  1868. gadget->name);
  1869. /* If there's an RNDIS configuration, that's what Windows wants to
  1870. * be using ... so use these product IDs here and in the "linux.inf"
  1871. * needed to install MSFT drivers. Current Linux kernels will use
  1872. * the second configuration if it's CDC Ethernet, and need some help
  1873. * to choose the right configuration otherwise.
  1874. */
  1875. if (rndis) {
  1876. device_desc.idVendor =
  1877. __constant_cpu_to_le16(RNDIS_VENDOR_NUM);
  1878. device_desc.idProduct =
  1879. __constant_cpu_to_le16(RNDIS_PRODUCT_NUM);
  1880. snprintf (product_desc, sizeof product_desc,
  1881. "RNDIS/%s", driver_desc);
  1882. /* CDC subset ... recognized by Linux since 2.4.10, but Windows
  1883. * drivers aren't widely available.
  1884. */
  1885. } else if (!cdc) {
  1886. device_desc.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  1887. device_desc.idVendor =
  1888. __constant_cpu_to_le16(SIMPLE_VENDOR_NUM);
  1889. device_desc.idProduct =
  1890. __constant_cpu_to_le16(SIMPLE_PRODUCT_NUM);
  1891. }
  1892. /* support optional vendor/distro customization */
  1893. if (idVendor) {
  1894. if (!idProduct) {
  1895. dev_err (&gadget->dev, "idVendor needs idProduct!\n");
  1896. return -ENODEV;
  1897. }
  1898. device_desc.idVendor = cpu_to_le16(idVendor);
  1899. device_desc.idProduct = cpu_to_le16(idProduct);
  1900. if (bcdDevice)
  1901. device_desc.bcdDevice = cpu_to_le16(bcdDevice);
  1902. }
  1903. if (iManufacturer)
  1904. strlcpy (manufacturer, iManufacturer, sizeof manufacturer);
  1905. if (iProduct)
  1906. strlcpy (product_desc, iProduct, sizeof product_desc);
  1907. /* all we really need is bulk IN/OUT */
  1908. usb_ep_autoconfig_reset (gadget);
  1909. in_ep = usb_ep_autoconfig (gadget, &fs_source_desc);
  1910. if (!in_ep) {
  1911. autoconf_fail:
  1912. dev_err (&gadget->dev,
  1913. "can't autoconfigure on %s\n",
  1914. gadget->name);
  1915. return -ENODEV;
  1916. }
  1917. in_ep->driver_data = in_ep; /* claim */
  1918. out_ep = usb_ep_autoconfig (gadget, &fs_sink_desc);
  1919. if (!out_ep)
  1920. goto autoconf_fail;
  1921. out_ep->driver_data = out_ep; /* claim */
  1922. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1923. /* CDC Ethernet control interface doesn't require a status endpoint.
  1924. * Since some hosts expect one, try to allocate one anyway.
  1925. */
  1926. if (cdc || rndis) {
  1927. status_ep = usb_ep_autoconfig (gadget, &fs_status_desc);
  1928. if (status_ep) {
  1929. status_ep->driver_data = status_ep; /* claim */
  1930. } else if (rndis) {
  1931. dev_err (&gadget->dev,
  1932. "can't run RNDIS on %s\n",
  1933. gadget->name);
  1934. return -ENODEV;
  1935. #ifdef DEV_CONFIG_CDC
  1936. /* pxa25x only does CDC subset; often used with RNDIS */
  1937. } else if (cdc) {
  1938. control_intf.bNumEndpoints = 0;
  1939. /* FIXME remove endpoint from descriptor list */
  1940. #endif
  1941. }
  1942. }
  1943. #endif
  1944. /* one config: cdc, else minimal subset */
  1945. if (!cdc) {
  1946. eth_config.bNumInterfaces = 1;
  1947. eth_config.iConfiguration = STRING_SUBSET;
  1948. fs_subset_descriptors();
  1949. hs_subset_descriptors();
  1950. }
  1951. /* For now RNDIS is always a second config */
  1952. if (rndis)
  1953. device_desc.bNumConfigurations = 2;
  1954. #ifdef CONFIG_USB_GADGET_DUALSPEED
  1955. if (rndis)
  1956. dev_qualifier.bNumConfigurations = 2;
  1957. else if (!cdc)
  1958. dev_qualifier.bDeviceClass = USB_CLASS_VENDOR_SPEC;
  1959. /* assumes ep0 uses the same value for both speeds ... */
  1960. dev_qualifier.bMaxPacketSize0 = device_desc.bMaxPacketSize0;
  1961. /* and that all endpoints are dual-speed */
  1962. hs_source_desc.bEndpointAddress = fs_source_desc.bEndpointAddress;
  1963. hs_sink_desc.bEndpointAddress = fs_sink_desc.bEndpointAddress;
  1964. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  1965. if (status_ep)
  1966. hs_status_desc.bEndpointAddress =
  1967. fs_status_desc.bEndpointAddress;
  1968. #endif
  1969. #endif /* DUALSPEED */
  1970. device_desc.bMaxPacketSize0 = gadget->ep0->maxpacket;
  1971. usb_gadget_set_selfpowered (gadget);
  1972. if (gadget->is_otg) {
  1973. otg_descriptor.bmAttributes |= USB_OTG_HNP,
  1974. eth_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1975. eth_config.bMaxPower = 4;
  1976. #ifdef CONFIG_USB_ETH_RNDIS
  1977. rndis_config.bmAttributes |= USB_CONFIG_ATT_WAKEUP;
  1978. rndis_config.bMaxPower = 4;
  1979. #endif
  1980. }
  1981. net = alloc_etherdev (sizeof *dev);
  1982. if (!net)
  1983. return status;
  1984. dev = netdev_priv(net);
  1985. spin_lock_init (&dev->lock);
  1986. INIT_WORK (&dev->work, eth_work, dev);
  1987. INIT_LIST_HEAD (&dev->tx_reqs);
  1988. INIT_LIST_HEAD (&dev->rx_reqs);
  1989. /* network device setup */
  1990. dev->net = net;
  1991. SET_MODULE_OWNER (net);
  1992. strcpy (net->name, "usb%d");
  1993. dev->cdc = cdc;
  1994. dev->zlp = zlp;
  1995. dev->in_ep = in_ep;
  1996. dev->out_ep = out_ep;
  1997. dev->status_ep = status_ep;
  1998. /* Module params for these addresses should come from ID proms.
  1999. * The host side address is used with CDC and RNDIS, and commonly
  2000. * ends up in a persistent config database.
  2001. */
  2002. get_ether_addr(dev_addr, net->dev_addr);
  2003. if (cdc || rndis) {
  2004. get_ether_addr(host_addr, dev->host_mac);
  2005. #ifdef DEV_CONFIG_CDC
  2006. snprintf (ethaddr, sizeof ethaddr, "%02X%02X%02X%02X%02X%02X",
  2007. dev->host_mac [0], dev->host_mac [1],
  2008. dev->host_mac [2], dev->host_mac [3],
  2009. dev->host_mac [4], dev->host_mac [5]);
  2010. #endif
  2011. }
  2012. if (rndis) {
  2013. status = rndis_init();
  2014. if (status < 0) {
  2015. dev_err (&gadget->dev, "can't init RNDIS, %d\n",
  2016. status);
  2017. goto fail;
  2018. }
  2019. }
  2020. net->change_mtu = eth_change_mtu;
  2021. net->get_stats = eth_get_stats;
  2022. net->hard_start_xmit = eth_start_xmit;
  2023. net->open = eth_open;
  2024. net->stop = eth_stop;
  2025. // watchdog_timeo, tx_timeout ...
  2026. // set_multicast_list
  2027. SET_ETHTOOL_OPS(net, &ops);
  2028. /* preallocate control message data and buffer */
  2029. dev->req = eth_req_alloc (gadget->ep0, USB_BUFSIZ, GFP_KERNEL);
  2030. if (!dev->req)
  2031. goto fail;
  2032. dev->req->complete = eth_setup_complete;
  2033. /* ... and maybe likewise for status transfer */
  2034. #if defined(DEV_CONFIG_CDC) || defined(CONFIG_USB_ETH_RNDIS)
  2035. if (dev->status_ep) {
  2036. dev->stat_req = eth_req_alloc (dev->status_ep,
  2037. STATUS_BYTECOUNT, GFP_KERNEL);
  2038. if (!dev->stat_req) {
  2039. eth_req_free (gadget->ep0, dev->req);
  2040. goto fail;
  2041. }
  2042. dev->stat_req->context = NULL;
  2043. }
  2044. #endif
  2045. /* finish hookup to lower layer ... */
  2046. dev->gadget = gadget;
  2047. set_gadget_data (gadget, dev);
  2048. gadget->ep0->driver_data = dev;
  2049. /* two kinds of host-initiated state changes:
  2050. * - iff DATA transfer is active, carrier is "on"
  2051. * - tx queueing enabled if open *and* carrier is "on"
  2052. */
  2053. netif_stop_queue (dev->net);
  2054. netif_carrier_off (dev->net);
  2055. SET_NETDEV_DEV (dev->net, &gadget->dev);
  2056. status = register_netdev (dev->net);
  2057. if (status < 0)
  2058. goto fail1;
  2059. INFO (dev, "%s, version: " DRIVER_VERSION "\n", driver_desc);
  2060. INFO (dev, "using %s, OUT %s IN %s%s%s\n", gadget->name,
  2061. out_ep->name, in_ep->name,
  2062. status_ep ? " STATUS " : "",
  2063. status_ep ? status_ep->name : ""
  2064. );
  2065. INFO (dev, "MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  2066. net->dev_addr [0], net->dev_addr [1],
  2067. net->dev_addr [2], net->dev_addr [3],
  2068. net->dev_addr [4], net->dev_addr [5]);
  2069. if (cdc || rndis)
  2070. INFO (dev, "HOST MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
  2071. dev->host_mac [0], dev->host_mac [1],
  2072. dev->host_mac [2], dev->host_mac [3],
  2073. dev->host_mac [4], dev->host_mac [5]);
  2074. if (rndis) {
  2075. u32 vendorID = 0;
  2076. /* FIXME RNDIS vendor id == "vendor NIC code" == ? */
  2077. dev->rndis_config = rndis_register (rndis_control_ack);
  2078. if (dev->rndis_config < 0) {
  2079. fail0:
  2080. unregister_netdev (dev->net);
  2081. status = -ENODEV;
  2082. goto fail;
  2083. }
  2084. /* these set up a lot of the OIDs that RNDIS needs */
  2085. rndis_set_host_mac (dev->rndis_config, dev->host_mac);
  2086. if (rndis_set_param_dev (dev->rndis_config, dev->net,
  2087. &dev->stats, &dev->cdc_filter))
  2088. goto fail0;
  2089. if (rndis_set_param_vendor (dev->rndis_config, vendorID,
  2090. manufacturer))
  2091. goto fail0;
  2092. if (rndis_set_param_medium (dev->rndis_config,
  2093. NDIS_MEDIUM_802_3,
  2094. 0))
  2095. goto fail0;
  2096. INFO (dev, "RNDIS ready\n");
  2097. }
  2098. return status;
  2099. fail1:
  2100. dev_dbg(&gadget->dev, "register_netdev failed, %d\n", status);
  2101. fail:
  2102. eth_unbind (gadget);
  2103. return status;
  2104. }
  2105. /*-------------------------------------------------------------------------*/
  2106. static void
  2107. eth_suspend (struct usb_gadget *gadget)
  2108. {
  2109. struct eth_dev *dev = get_gadget_data (gadget);
  2110. DEBUG (dev, "suspend\n");
  2111. dev->suspended = 1;
  2112. }
  2113. static void
  2114. eth_resume (struct usb_gadget *gadget)
  2115. {
  2116. struct eth_dev *dev = get_gadget_data (gadget);
  2117. DEBUG (dev, "resume\n");
  2118. dev->suspended = 0;
  2119. }
  2120. /*-------------------------------------------------------------------------*/
  2121. static struct usb_gadget_driver eth_driver = {
  2122. .speed = DEVSPEED,
  2123. .function = (char *) driver_desc,
  2124. .bind = eth_bind,
  2125. .unbind = eth_unbind,
  2126. .setup = eth_setup,
  2127. .disconnect = eth_disconnect,
  2128. .suspend = eth_suspend,
  2129. .resume = eth_resume,
  2130. .driver = {
  2131. .name = (char *) shortname,
  2132. // .shutdown = ...
  2133. // .suspend = ...
  2134. // .resume = ...
  2135. },
  2136. };
  2137. MODULE_DESCRIPTION (DRIVER_DESC);
  2138. MODULE_AUTHOR ("David Brownell, Benedikt Spanger");
  2139. MODULE_LICENSE ("GPL");
  2140. static int __init init (void)
  2141. {
  2142. return usb_gadget_register_driver (&eth_driver);
  2143. }
  2144. module_init (init);
  2145. static void __exit cleanup (void)
  2146. {
  2147. usb_gadget_unregister_driver (&eth_driver);
  2148. }
  2149. module_exit (cleanup);