ether.c 68 KB

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