ether.c 71 KB

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