usbnet.c 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918
  1. /*
  2. * USB Networking Links
  3. * Copyright (C) 2000-2005 by David Brownell
  4. * Copyright (C) 2002 Pavel Machek <pavel@ucw.cz>
  5. * Copyright (C) 2003-2005 David Hollis <dhollis@davehollis.com>
  6. * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
  7. * Copyright (c) 2002-2003 TiVo Inc.
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  22. */
  23. /*
  24. * This is a generic "USB networking" framework that works with several
  25. * kinds of full and high speed networking devices:
  26. *
  27. * + USB host-to-host "network cables", used for IP-over-USB links.
  28. * These are often used for Laplink style connectivity products.
  29. * - AnchorChip 2720
  30. * - Belkin, eTEK (interops with Win32 drivers)
  31. * - GeneSys GL620USB-A
  32. * - NetChip 1080 (interoperates with NetChip Win32 drivers)
  33. * - Prolific PL-2301/2302 (replaces "plusb" driver)
  34. * - KC Technology KC2190
  35. *
  36. * + Smart USB devices can support such links directly, using Internet
  37. * standard protocols instead of proprietary host-to-device links.
  38. * - Linux PDAs like iPaq, Yopy, and Zaurus
  39. * - The BLOB boot loader (for diskless booting)
  40. * - Linux "gadgets", perhaps using PXA-2xx or Net2280 controllers
  41. * - Devices using EPSON's sample USB firmware
  42. * - CDC-Ethernet class devices, such as many cable modems
  43. *
  44. * + Adapters to networks such as Ethernet.
  45. * - AX8817X based USB 2.0 products
  46. *
  47. * Links to these devices can be bridged using Linux Ethernet bridging.
  48. * With minor exceptions, these all use similar USB framing for network
  49. * traffic, but need different protocols for control traffic.
  50. *
  51. * USB devices can implement their side of this protocol at the cost
  52. * of two bulk endpoints; it's not restricted to "cable" applications.
  53. * See the SA1110, Zaurus, or EPSON device/client support in this driver;
  54. * slave/target drivers such as "usb-eth" (on most SA-1100 PDAs) or
  55. * "g_ether" (in the Linux "gadget" framework) implement that behavior
  56. * within devices.
  57. *
  58. *
  59. * CHANGELOG:
  60. *
  61. * 13-sep-2000 experimental, new
  62. * 10-oct-2000 usb_device_id table created.
  63. * 28-oct-2000 misc fixes; mostly, discard more TTL-mangled rx packets.
  64. * 01-nov-2000 usb_device_id table and probing api update by
  65. * Adam J. Richter <adam@yggdrasil.com>.
  66. * 18-dec-2000 (db) tx watchdog, "net1080" renaming to "usbnet", device_info
  67. * and prolific support, isolate net1080-specific bits, cleanup.
  68. * fix unlink_urbs oops in D3 PM resume code path.
  69. *
  70. * 02-feb-2001 (db) fix tx skb sharing, packet length, match_flags, ...
  71. * 08-feb-2001 stubbed in "linuxdev", maybe the SA-1100 folk can use it;
  72. * AnchorChips 2720 support (from spec) for testing;
  73. * fix bit-ordering problem with ethernet multicast addr
  74. * 19-feb-2001 Support for clearing halt conditions. SA1100 UDC support
  75. * updates. Oleg Drokin (green@iXcelerator.com)
  76. * 25-mar-2001 More SA-1100 updates, including workaround for ip problem
  77. * expecting cleared skb->cb and framing change to match latest
  78. * handhelds.org version (Oleg). Enable device IDs from the
  79. * Win32 Belkin driver; other cleanups (db).
  80. * 16-jul-2001 Bugfixes for uhci oops-on-unplug, Belkin support, various
  81. * cleanups for problems not yet seen in the field. (db)
  82. * 17-oct-2001 Handle "Advance USBNET" product, like Belkin/eTEK devices,
  83. * from Ioannis Mavroukakis <i.mavroukakis@btinternet.com>;
  84. * rx unlinks somehow weren't async; minor cleanup.
  85. * 03-nov-2001 Merged GeneSys driver; original code from Jiun-Jie Huang
  86. * <huangjj@genesyslogic.com.tw>, updated by Stanislav Brabec
  87. * <utx@penguin.cz>. Made framing options (NetChip/GeneSys)
  88. * tie mostly to (sub)driver info. Workaround some PL-2302
  89. * chips that seem to reject SET_INTERFACE requests.
  90. *
  91. * 06-apr-2002 Added ethtool support, based on a patch from Brad Hards.
  92. * Level of diagnostics is more configurable; they use device
  93. * location (usb_device->devpath) instead of address (2.5).
  94. * For tx_fixup, memflags can't be NOIO.
  95. * 07-may-2002 Generalize/cleanup keventd support, handling rx stalls (mostly
  96. * for USB 2.0 TTs) and memory shortages (potential) too. (db)
  97. * Use "locally assigned" IEEE802 address space. (Brad Hards)
  98. * 18-oct-2002 Support for Zaurus (Pavel Machek), related cleanup (db).
  99. * 14-dec-2002 Remove Zaurus-private crc32 code (Pavel); 2.5 oops fix,
  100. * cleanups and stubbed PXA-250 support (db), fix for framing
  101. * issues on Z, net1080, and gl620a (Toby Milne)
  102. *
  103. * 31-mar-2003 Use endpoint descriptors: high speed support, simpler sa1100
  104. * vs pxa25x, and CDC Ethernet. Throttle down log floods on
  105. * disconnect; other cleanups. (db) Flush net1080 fifos
  106. * after several sequential framing errors. (Johannes Erdfelt)
  107. * 22-aug-2003 AX8817X support (Dave Hollis).
  108. * 14-jun-2004 Trivial patch for AX8817X based Buffalo LUA-U2-KTX in Japan
  109. * (Neil Bortnak)
  110. * 03-nov-2004 Trivial patch for KC2190 (KC-190) chip. (Jonathan McDowell)
  111. *
  112. * 01-feb-2005 AX88772 support (Phil Chang & Dave Hollis)
  113. *-------------------------------------------------------------------------*/
  114. // #define DEBUG // error path messages, extra info
  115. // #define VERBOSE // more; success messages
  116. #include <linux/config.h>
  117. #ifdef CONFIG_USB_DEBUG
  118. # define DEBUG
  119. #endif
  120. #include <linux/module.h>
  121. #include <linux/kmod.h>
  122. #include <linux/sched.h>
  123. #include <linux/init.h>
  124. #include <linux/netdevice.h>
  125. #include <linux/etherdevice.h>
  126. #include <linux/ethtool.h>
  127. #include <linux/workqueue.h>
  128. #include <linux/mii.h>
  129. #include <linux/usb.h>
  130. #include <linux/mm.h>
  131. #include <linux/dma-mapping.h>
  132. #include <asm/unaligned.h>
  133. #include "usbnet.h"
  134. #define DRIVER_VERSION "22-Aug-2005"
  135. /*-------------------------------------------------------------------------*/
  136. /*
  137. * Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
  138. * Several dozen bytes of IPv4 data can fit in two such transactions.
  139. * One maximum size Ethernet packet takes twenty four of them.
  140. * For high speed, each frame comfortably fits almost 36 max size
  141. * Ethernet packets (so queues should be bigger).
  142. *
  143. * REVISIT qlens should be members of 'struct usbnet'; the goal is to
  144. * let the USB host controller be busy for 5msec or more before an irq
  145. * is required, under load. Jumbograms change the equation.
  146. */
  147. #define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  148. #define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
  149. /* packets are always ethernet, sometimes wrapped in other framing */
  150. #define MIN_PACKET sizeof(struct ethhdr)
  151. // reawaken network queue this soon after stopping; else watchdog barks
  152. #define TX_TIMEOUT_JIFFIES (5*HZ)
  153. // throttle rx/tx briefly after some faults, so khubd might disconnect()
  154. // us (it polls at HZ/4 usually) before we report too many false errors.
  155. #define THROTTLE_JIFFIES (HZ/8)
  156. // for vendor-specific control operations
  157. #define CONTROL_TIMEOUT_MS USB_CTRL_GET_TIMEOUT
  158. // between wakeups
  159. #define UNLINK_TIMEOUT_MS 3
  160. /*-------------------------------------------------------------------------*/
  161. // randomly generated ethernet address
  162. static u8 node_id [ETH_ALEN];
  163. static const char driver_name [] = "usbnet";
  164. /* use ethtool to change the level for any given device */
  165. static int msg_level = -1;
  166. module_param (msg_level, int, 0);
  167. MODULE_PARM_DESC (msg_level, "Override default message level");
  168. /*-------------------------------------------------------------------------*/
  169. static void usbnet_get_drvinfo (struct net_device *, struct ethtool_drvinfo *);
  170. static u32 usbnet_get_link (struct net_device *);
  171. static u32 usbnet_get_msglevel (struct net_device *);
  172. static void usbnet_set_msglevel (struct net_device *, u32);
  173. static void defer_kevent (struct usbnet *, int);
  174. /* mostly for PDA style devices, which are always connected if present */
  175. static int always_connected (struct usbnet *dev)
  176. {
  177. return 0;
  178. }
  179. /* handles CDC Ethernet and many other network "bulk data" interfaces */
  180. static int
  181. get_endpoints (struct usbnet *dev, struct usb_interface *intf)
  182. {
  183. int tmp;
  184. struct usb_host_interface *alt = NULL;
  185. struct usb_host_endpoint *in = NULL, *out = NULL;
  186. struct usb_host_endpoint *status = NULL;
  187. for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
  188. unsigned ep;
  189. in = out = status = NULL;
  190. alt = intf->altsetting + tmp;
  191. /* take the first altsetting with in-bulk + out-bulk;
  192. * remember any status endpoint, just in case;
  193. * ignore other endpoints and altsetttings.
  194. */
  195. for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
  196. struct usb_host_endpoint *e;
  197. int intr = 0;
  198. e = alt->endpoint + ep;
  199. switch (e->desc.bmAttributes) {
  200. case USB_ENDPOINT_XFER_INT:
  201. if (!(e->desc.bEndpointAddress & USB_DIR_IN))
  202. continue;
  203. intr = 1;
  204. /* FALLTHROUGH */
  205. case USB_ENDPOINT_XFER_BULK:
  206. break;
  207. default:
  208. continue;
  209. }
  210. if (e->desc.bEndpointAddress & USB_DIR_IN) {
  211. if (!intr && !in)
  212. in = e;
  213. else if (intr && !status)
  214. status = e;
  215. } else {
  216. if (!out)
  217. out = e;
  218. }
  219. }
  220. if (in && out)
  221. break;
  222. }
  223. if (!alt || !in || !out)
  224. return -EINVAL;
  225. if (alt->desc.bAlternateSetting != 0
  226. || !(dev->driver_info->flags & FLAG_NO_SETINT)) {
  227. tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
  228. alt->desc.bAlternateSetting);
  229. if (tmp < 0)
  230. return tmp;
  231. }
  232. dev->in = usb_rcvbulkpipe (dev->udev,
  233. in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  234. dev->out = usb_sndbulkpipe (dev->udev,
  235. out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
  236. dev->status = status;
  237. return 0;
  238. }
  239. static void intr_complete (struct urb *urb, struct pt_regs *regs);
  240. static int init_status (struct usbnet *dev, struct usb_interface *intf)
  241. {
  242. char *buf = NULL;
  243. unsigned pipe = 0;
  244. unsigned maxp;
  245. unsigned period;
  246. if (!dev->driver_info->status)
  247. return 0;
  248. pipe = usb_rcvintpipe (dev->udev,
  249. dev->status->desc.bEndpointAddress
  250. & USB_ENDPOINT_NUMBER_MASK);
  251. maxp = usb_maxpacket (dev->udev, pipe, 0);
  252. /* avoid 1 msec chatter: min 8 msec poll rate */
  253. period = max ((int) dev->status->desc.bInterval,
  254. (dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
  255. buf = kmalloc (maxp, SLAB_KERNEL);
  256. if (buf) {
  257. dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
  258. if (!dev->interrupt) {
  259. kfree (buf);
  260. return -ENOMEM;
  261. } else {
  262. usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
  263. buf, maxp, intr_complete, dev, period);
  264. dev_dbg(&intf->dev,
  265. "status ep%din, %d bytes period %d\n",
  266. usb_pipeendpoint(pipe), maxp, period);
  267. }
  268. }
  269. return 0;
  270. }
  271. static void skb_return (struct usbnet *dev, struct sk_buff *skb)
  272. {
  273. int status;
  274. skb->dev = dev->net;
  275. skb->protocol = eth_type_trans (skb, dev->net);
  276. dev->stats.rx_packets++;
  277. dev->stats.rx_bytes += skb->len;
  278. if (netif_msg_rx_status (dev))
  279. devdbg (dev, "< rx, len %zu, type 0x%x",
  280. skb->len + sizeof (struct ethhdr), skb->protocol);
  281. memset (skb->cb, 0, sizeof (struct skb_data));
  282. status = netif_rx (skb);
  283. if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
  284. devdbg (dev, "netif_rx status %d", status);
  285. }
  286. #ifdef CONFIG_USB_AX8817X
  287. /* ASIX AX8817X based USB 2.0 Ethernet Devices */
  288. #define HAVE_HARDWARE
  289. #define NEED_MII
  290. #include <linux/crc32.h>
  291. #define AX_CMD_SET_SW_MII 0x06
  292. #define AX_CMD_READ_MII_REG 0x07
  293. #define AX_CMD_WRITE_MII_REG 0x08
  294. #define AX_CMD_SET_HW_MII 0x0a
  295. #define AX_CMD_READ_EEPROM 0x0b
  296. #define AX_CMD_WRITE_EEPROM 0x0c
  297. #define AX_CMD_WRITE_ENABLE 0x0d
  298. #define AX_CMD_WRITE_DISABLE 0x0e
  299. #define AX_CMD_WRITE_RX_CTL 0x10
  300. #define AX_CMD_READ_IPG012 0x11
  301. #define AX_CMD_WRITE_IPG0 0x12
  302. #define AX_CMD_WRITE_IPG1 0x13
  303. #define AX_CMD_WRITE_IPG2 0x14
  304. #define AX_CMD_WRITE_MULTI_FILTER 0x16
  305. #define AX_CMD_READ_NODE_ID 0x17
  306. #define AX_CMD_READ_PHY_ID 0x19
  307. #define AX_CMD_READ_MEDIUM_STATUS 0x1a
  308. #define AX_CMD_WRITE_MEDIUM_MODE 0x1b
  309. #define AX_CMD_READ_MONITOR_MODE 0x1c
  310. #define AX_CMD_WRITE_MONITOR_MODE 0x1d
  311. #define AX_CMD_WRITE_GPIOS 0x1f
  312. #define AX_CMD_SW_RESET 0x20
  313. #define AX_CMD_SW_PHY_STATUS 0x21
  314. #define AX_CMD_SW_PHY_SELECT 0x22
  315. #define AX88772_CMD_READ_NODE_ID 0x13
  316. #define AX_MONITOR_MODE 0x01
  317. #define AX_MONITOR_LINK 0x02
  318. #define AX_MONITOR_MAGIC 0x04
  319. #define AX_MONITOR_HSFS 0x10
  320. /* AX88172 Medium Status Register values */
  321. #define AX_MEDIUM_FULL_DUPLEX 0x02
  322. #define AX_MEDIUM_TX_ABORT_ALLOW 0x04
  323. #define AX_MEDIUM_FLOW_CONTROL_EN 0x10
  324. #define AX_MCAST_FILTER_SIZE 8
  325. #define AX_MAX_MCAST 64
  326. #define AX_EEPROM_LEN 0x40
  327. #define AX_SWRESET_CLEAR 0x00
  328. #define AX_SWRESET_RR 0x01
  329. #define AX_SWRESET_RT 0x02
  330. #define AX_SWRESET_PRTE 0x04
  331. #define AX_SWRESET_PRL 0x08
  332. #define AX_SWRESET_BZ 0x10
  333. #define AX_SWRESET_IPRL 0x20
  334. #define AX_SWRESET_IPPD 0x40
  335. #define AX88772_IPG0_DEFAULT 0x15
  336. #define AX88772_IPG1_DEFAULT 0x0c
  337. #define AX88772_IPG2_DEFAULT 0x12
  338. #define AX88772_MEDIUM_FULL_DUPLEX 0x0002
  339. #define AX88772_MEDIUM_RESERVED 0x0004
  340. #define AX88772_MEDIUM_RX_FC_ENABLE 0x0010
  341. #define AX88772_MEDIUM_TX_FC_ENABLE 0x0020
  342. #define AX88772_MEDIUM_PAUSE_FORMAT 0x0080
  343. #define AX88772_MEDIUM_RX_ENABLE 0x0100
  344. #define AX88772_MEDIUM_100MB 0x0200
  345. #define AX88772_MEDIUM_DEFAULT \
  346. (AX88772_MEDIUM_FULL_DUPLEX | AX88772_MEDIUM_RX_FC_ENABLE | \
  347. AX88772_MEDIUM_TX_FC_ENABLE | AX88772_MEDIUM_100MB | \
  348. AX88772_MEDIUM_RESERVED | AX88772_MEDIUM_RX_ENABLE )
  349. #define AX_EEPROM_MAGIC 0xdeadbeef
  350. /* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
  351. struct ax8817x_data {
  352. u8 multi_filter[AX_MCAST_FILTER_SIZE];
  353. };
  354. struct ax88172_int_data {
  355. u16 res1;
  356. u8 link;
  357. u16 res2;
  358. u8 status;
  359. u16 res3;
  360. } __attribute__ ((packed));
  361. static int ax8817x_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  362. u16 size, void *data)
  363. {
  364. return usb_control_msg(
  365. dev->udev,
  366. usb_rcvctrlpipe(dev->udev, 0),
  367. cmd,
  368. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  369. value,
  370. index,
  371. data,
  372. size,
  373. CONTROL_TIMEOUT_MS);
  374. }
  375. static int ax8817x_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  376. u16 size, void *data)
  377. {
  378. return usb_control_msg(
  379. dev->udev,
  380. usb_sndctrlpipe(dev->udev, 0),
  381. cmd,
  382. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  383. value,
  384. index,
  385. data,
  386. size,
  387. CONTROL_TIMEOUT_MS);
  388. }
  389. static void ax8817x_async_cmd_callback(struct urb *urb, struct pt_regs *regs)
  390. {
  391. struct usb_ctrlrequest *req = (struct usb_ctrlrequest *)urb->context;
  392. if (urb->status < 0)
  393. printk(KERN_DEBUG "ax8817x_async_cmd_callback() failed with %d",
  394. urb->status);
  395. kfree(req);
  396. usb_free_urb(urb);
  397. }
  398. static void ax8817x_status(struct usbnet *dev, struct urb *urb)
  399. {
  400. struct ax88172_int_data *event;
  401. int link;
  402. if (urb->actual_length < 8)
  403. return;
  404. event = urb->transfer_buffer;
  405. link = event->link & 0x01;
  406. if (netif_carrier_ok(dev->net) != link) {
  407. if (link) {
  408. netif_carrier_on(dev->net);
  409. defer_kevent (dev, EVENT_LINK_RESET );
  410. } else
  411. netif_carrier_off(dev->net);
  412. devdbg(dev, "ax8817x - Link Status is: %d", link);
  413. }
  414. }
  415. static void ax8817x_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value, u16 index,
  416. u16 size, void *data)
  417. {
  418. struct usb_ctrlrequest *req;
  419. int status;
  420. struct urb *urb;
  421. if ((urb = usb_alloc_urb(0, GFP_ATOMIC)) == NULL) {
  422. devdbg(dev, "Error allocating URB in write_cmd_async!");
  423. return;
  424. }
  425. if ((req = kmalloc(sizeof(struct usb_ctrlrequest), GFP_ATOMIC)) == NULL) {
  426. deverr(dev, "Failed to allocate memory for control request");
  427. usb_free_urb(urb);
  428. return;
  429. }
  430. req->bRequestType = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  431. req->bRequest = cmd;
  432. req->wValue = cpu_to_le16(value);
  433. req->wIndex = cpu_to_le16(index);
  434. req->wLength = cpu_to_le16(size);
  435. usb_fill_control_urb(urb, dev->udev,
  436. usb_sndctrlpipe(dev->udev, 0),
  437. (void *)req, data, size,
  438. ax8817x_async_cmd_callback, req);
  439. if((status = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
  440. deverr(dev, "Error submitting the control message: status=%d", status);
  441. kfree(req);
  442. usb_free_urb(urb);
  443. }
  444. }
  445. static void ax8817x_set_multicast(struct net_device *net)
  446. {
  447. struct usbnet *dev = netdev_priv(net);
  448. struct ax8817x_data *data = (struct ax8817x_data *)&dev->data;
  449. u8 rx_ctl = 0x8c;
  450. if (net->flags & IFF_PROMISC) {
  451. rx_ctl |= 0x01;
  452. } else if (net->flags & IFF_ALLMULTI
  453. || net->mc_count > AX_MAX_MCAST) {
  454. rx_ctl |= 0x02;
  455. } else if (net->mc_count == 0) {
  456. /* just broadcast and directed */
  457. } else {
  458. /* We use the 20 byte dev->data
  459. * for our 8 byte filter buffer
  460. * to avoid allocating memory that
  461. * is tricky to free later */
  462. struct dev_mc_list *mc_list = net->mc_list;
  463. u32 crc_bits;
  464. int i;
  465. memset(data->multi_filter, 0, AX_MCAST_FILTER_SIZE);
  466. /* Build the multicast hash filter. */
  467. for (i = 0; i < net->mc_count; i++) {
  468. crc_bits =
  469. ether_crc(ETH_ALEN,
  470. mc_list->dmi_addr) >> 26;
  471. data->multi_filter[crc_bits >> 3] |=
  472. 1 << (crc_bits & 7);
  473. mc_list = mc_list->next;
  474. }
  475. ax8817x_write_cmd_async(dev, AX_CMD_WRITE_MULTI_FILTER, 0, 0,
  476. AX_MCAST_FILTER_SIZE, data->multi_filter);
  477. rx_ctl |= 0x10;
  478. }
  479. ax8817x_write_cmd_async(dev, AX_CMD_WRITE_RX_CTL, rx_ctl, 0, 0, NULL);
  480. }
  481. static int ax8817x_mdio_read(struct net_device *netdev, int phy_id, int loc)
  482. {
  483. struct usbnet *dev = netdev_priv(netdev);
  484. u16 res;
  485. u8 buf[1];
  486. ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
  487. ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
  488. ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
  489. return res & 0xffff;
  490. }
  491. static void ax8817x_mdio_write(struct net_device *netdev, int phy_id, int loc, int val)
  492. {
  493. struct usbnet *dev = netdev_priv(netdev);
  494. u16 res = val;
  495. u8 buf[1];
  496. ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, &buf);
  497. ax8817x_write_cmd(dev, AX_CMD_WRITE_MII_REG, phy_id, (__u16)loc, 2, (u16 *)&res);
  498. ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf);
  499. }
  500. static int ax88172_link_reset(struct usbnet *dev)
  501. {
  502. u16 lpa;
  503. u8 mode;
  504. mode = AX_MEDIUM_TX_ABORT_ALLOW | AX_MEDIUM_FLOW_CONTROL_EN;
  505. lpa = ax8817x_mdio_read(dev->net, dev->mii.phy_id, MII_LPA);
  506. if (lpa & LPA_DUPLEX)
  507. mode |= AX_MEDIUM_FULL_DUPLEX;
  508. ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  509. return 0;
  510. }
  511. static void ax8817x_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  512. {
  513. struct usbnet *dev = netdev_priv(net);
  514. u8 opt;
  515. if (ax8817x_read_cmd(dev, AX_CMD_READ_MONITOR_MODE, 0, 0, 1, &opt) < 0) {
  516. wolinfo->supported = 0;
  517. wolinfo->wolopts = 0;
  518. return;
  519. }
  520. wolinfo->supported = WAKE_PHY | WAKE_MAGIC;
  521. wolinfo->wolopts = 0;
  522. if (opt & AX_MONITOR_MODE) {
  523. if (opt & AX_MONITOR_LINK)
  524. wolinfo->wolopts |= WAKE_PHY;
  525. if (opt & AX_MONITOR_MAGIC)
  526. wolinfo->wolopts |= WAKE_MAGIC;
  527. }
  528. }
  529. static int ax8817x_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo)
  530. {
  531. struct usbnet *dev = netdev_priv(net);
  532. u8 opt = 0;
  533. u8 buf[1];
  534. if (wolinfo->wolopts & WAKE_PHY)
  535. opt |= AX_MONITOR_LINK;
  536. if (wolinfo->wolopts & WAKE_MAGIC)
  537. opt |= AX_MONITOR_MAGIC;
  538. if (opt != 0)
  539. opt |= AX_MONITOR_MODE;
  540. if (ax8817x_write_cmd(dev, AX_CMD_WRITE_MONITOR_MODE,
  541. opt, 0, 0, &buf) < 0)
  542. return -EINVAL;
  543. return 0;
  544. }
  545. static int ax8817x_get_eeprom_len(struct net_device *net)
  546. {
  547. return AX_EEPROM_LEN;
  548. }
  549. static int ax8817x_get_eeprom(struct net_device *net,
  550. struct ethtool_eeprom *eeprom, u8 *data)
  551. {
  552. struct usbnet *dev = netdev_priv(net);
  553. u16 *ebuf = (u16 *)data;
  554. int i;
  555. /* Crude hack to ensure that we don't overwrite memory
  556. * if an odd length is supplied
  557. */
  558. if (eeprom->len % 2)
  559. return -EINVAL;
  560. eeprom->magic = AX_EEPROM_MAGIC;
  561. /* ax8817x returns 2 bytes from eeprom on read */
  562. for (i=0; i < eeprom->len / 2; i++) {
  563. if (ax8817x_read_cmd(dev, AX_CMD_READ_EEPROM,
  564. eeprom->offset + i, 0, 2, &ebuf[i]) < 0)
  565. return -EINVAL;
  566. }
  567. return 0;
  568. }
  569. static void ax8817x_get_drvinfo (struct net_device *net,
  570. struct ethtool_drvinfo *info)
  571. {
  572. /* Inherit standard device info */
  573. usbnet_get_drvinfo(net, info);
  574. info->eedump_len = 0x3e;
  575. }
  576. static int ax8817x_get_settings(struct net_device *net, struct ethtool_cmd *cmd)
  577. {
  578. struct usbnet *dev = netdev_priv(net);
  579. return mii_ethtool_gset(&dev->mii,cmd);
  580. }
  581. static int ax8817x_set_settings(struct net_device *net, struct ethtool_cmd *cmd)
  582. {
  583. struct usbnet *dev = netdev_priv(net);
  584. return mii_ethtool_sset(&dev->mii,cmd);
  585. }
  586. /* We need to override some ethtool_ops so we require our
  587. own structure so we don't interfere with other usbnet
  588. devices that may be connected at the same time. */
  589. static struct ethtool_ops ax8817x_ethtool_ops = {
  590. .get_drvinfo = ax8817x_get_drvinfo,
  591. .get_link = ethtool_op_get_link,
  592. .get_msglevel = usbnet_get_msglevel,
  593. .set_msglevel = usbnet_set_msglevel,
  594. .get_wol = ax8817x_get_wol,
  595. .set_wol = ax8817x_set_wol,
  596. .get_eeprom_len = ax8817x_get_eeprom_len,
  597. .get_eeprom = ax8817x_get_eeprom,
  598. .get_settings = ax8817x_get_settings,
  599. .set_settings = ax8817x_set_settings,
  600. };
  601. static int ax8817x_bind(struct usbnet *dev, struct usb_interface *intf)
  602. {
  603. int ret = 0;
  604. void *buf;
  605. int i;
  606. unsigned long gpio_bits = dev->driver_info->data;
  607. get_endpoints(dev,intf);
  608. buf = kmalloc(ETH_ALEN, GFP_KERNEL);
  609. if(!buf) {
  610. ret = -ENOMEM;
  611. goto out1;
  612. }
  613. /* Toggle the GPIOs in a manufacturer/model specific way */
  614. for (i = 2; i >= 0; i--) {
  615. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  616. (gpio_bits >> (i * 8)) & 0xff, 0, 0,
  617. buf)) < 0)
  618. goto out2;
  619. msleep(5);
  620. }
  621. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x80, 0, 0, buf)) < 0) {
  622. dbg("send AX_CMD_WRITE_RX_CTL failed: %d", ret);
  623. goto out2;
  624. }
  625. /* Get the MAC address */
  626. memset(buf, 0, ETH_ALEN);
  627. if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_NODE_ID, 0, 0, 6, buf)) < 0) {
  628. dbg("read AX_CMD_READ_NODE_ID failed: %d", ret);
  629. goto out2;
  630. }
  631. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  632. /* Get the PHY id */
  633. if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
  634. dbg("error on read AX_CMD_READ_PHY_ID: %02x", ret);
  635. goto out2;
  636. } else if (ret < 2) {
  637. /* this should always return 2 bytes */
  638. dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x", ret);
  639. ret = -EIO;
  640. goto out2;
  641. }
  642. /* Initialize MII structure */
  643. dev->mii.dev = dev->net;
  644. dev->mii.mdio_read = ax8817x_mdio_read;
  645. dev->mii.mdio_write = ax8817x_mdio_write;
  646. dev->mii.phy_id_mask = 0x3f;
  647. dev->mii.reg_num_mask = 0x1f;
  648. dev->mii.phy_id = *((u8 *)buf + 1);
  649. dev->net->set_multicast_list = ax8817x_set_multicast;
  650. dev->net->ethtool_ops = &ax8817x_ethtool_ops;
  651. ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  652. ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  653. ADVERTISE_ALL | ADVERTISE_CSMA | ADVERTISE_PAUSE_CAP);
  654. mii_nway_restart(&dev->mii);
  655. if (dev->driver_info->flags & FLAG_FRAMING_AX) {
  656. /* REVISIT: adjust hard_header_len too */
  657. dev->hard_mtu = 2048;
  658. }
  659. return 0;
  660. out2:
  661. kfree(buf);
  662. out1:
  663. return ret;
  664. }
  665. static struct ethtool_ops ax88772_ethtool_ops = {
  666. .get_drvinfo = ax8817x_get_drvinfo,
  667. .get_link = ethtool_op_get_link,
  668. .get_msglevel = usbnet_get_msglevel,
  669. .set_msglevel = usbnet_set_msglevel,
  670. .get_wol = ax8817x_get_wol,
  671. .set_wol = ax8817x_set_wol,
  672. .get_eeprom_len = ax8817x_get_eeprom_len,
  673. .get_eeprom = ax8817x_get_eeprom,
  674. .get_settings = ax8817x_get_settings,
  675. .set_settings = ax8817x_set_settings,
  676. };
  677. static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
  678. {
  679. int ret;
  680. void *buf;
  681. get_endpoints(dev,intf);
  682. buf = kmalloc(6, GFP_KERNEL);
  683. if(!buf) {
  684. dbg ("Cannot allocate memory for buffer");
  685. ret = -ENOMEM;
  686. goto out1;
  687. }
  688. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_GPIOS,
  689. 0x00B0, 0, 0, buf)) < 0)
  690. goto out2;
  691. msleep(5);
  692. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SW_PHY_SELECT, 0x0001, 0, 0, buf)) < 0) {
  693. dbg("Select PHY #1 failed: %d", ret);
  694. goto out2;
  695. }
  696. if ((ret =
  697. ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPPD, 0, 0, buf)) < 0) {
  698. dbg("Failed to power down internal PHY: %d", ret);
  699. goto out2;
  700. }
  701. msleep(150);
  702. if ((ret =
  703. ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_CLEAR, 0, 0, buf)) < 0) {
  704. dbg("Failed to perform software reset: %d", ret);
  705. goto out2;
  706. }
  707. msleep(150);
  708. if ((ret =
  709. ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) {
  710. dbg("Failed to set Internal/External PHY reset control: %d", ret);
  711. goto out2;
  712. }
  713. msleep(150);
  714. if ((ret =
  715. ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0000, 0, 0,
  716. buf)) < 0) {
  717. dbg("Failed to reset RX_CTL: %d", ret);
  718. goto out2;
  719. }
  720. /* Get the MAC address */
  721. memset(buf, 0, ETH_ALEN);
  722. if ((ret = ax8817x_read_cmd(dev, AX88772_CMD_READ_NODE_ID, 0, 0, ETH_ALEN, buf)) < 0) {
  723. dbg("Failed to read MAC address: %d", ret);
  724. goto out2;
  725. }
  726. memcpy(dev->net->dev_addr, buf, ETH_ALEN);
  727. if ((ret = ax8817x_write_cmd(dev, AX_CMD_SET_SW_MII, 0, 0, 0, buf)) < 0) {
  728. dbg("Enabling software MII failed: %d", ret);
  729. goto out2;
  730. }
  731. if (((ret =
  732. ax8817x_read_cmd(dev, AX_CMD_READ_MII_REG, 0x0010, 2, 2, buf)) < 0)
  733. || (*((u16 *)buf) != 0x003b)) {
  734. dbg("Read PHY register 2 must be 0x3b00: %d", ret);
  735. goto out2;
  736. }
  737. /* Initialize MII structure */
  738. dev->mii.dev = dev->net;
  739. dev->mii.mdio_read = ax8817x_mdio_read;
  740. dev->mii.mdio_write = ax8817x_mdio_write;
  741. dev->mii.phy_id_mask = 0xff;
  742. dev->mii.reg_num_mask = 0xff;
  743. /* Get the PHY id */
  744. if ((ret = ax8817x_read_cmd(dev, AX_CMD_READ_PHY_ID, 0, 0, 2, buf)) < 0) {
  745. dbg("Error reading PHY ID: %02x", ret);
  746. goto out2;
  747. } else if (ret < 2) {
  748. /* this should always return 2 bytes */
  749. dbg("AX_CMD_READ_PHY_ID returned less than 2 bytes: ret=%02x",
  750. ret);
  751. ret = -EIO;
  752. goto out2;
  753. }
  754. dev->mii.phy_id = *((u8 *)buf + 1);
  755. if ((ret =
  756. ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_PRL, 0, 0, buf)) < 0) {
  757. dbg("Set external PHY reset pin level: %d", ret);
  758. goto out2;
  759. }
  760. msleep(150);
  761. if ((ret =
  762. ax8817x_write_cmd(dev, AX_CMD_SW_RESET, AX_SWRESET_IPRL | AX_SWRESET_PRL, 0, 0, buf)) < 0) {
  763. dbg("Set Internal/External PHY reset control: %d", ret);
  764. goto out2;
  765. }
  766. msleep(150);
  767. dev->net->set_multicast_list = ax8817x_set_multicast;
  768. dev->net->ethtool_ops = &ax88772_ethtool_ops;
  769. ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_BMCR, BMCR_RESET);
  770. ax8817x_mdio_write(dev->net, dev->mii.phy_id, MII_ADVERTISE,
  771. ADVERTISE_ALL | ADVERTISE_CSMA);
  772. mii_nway_restart(&dev->mii);
  773. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, AX88772_MEDIUM_DEFAULT, 0, 0, buf)) < 0) {
  774. dbg("Write medium mode register: %d", ret);
  775. goto out2;
  776. }
  777. if ((ret = ax8817x_write_cmd(dev, AX_CMD_WRITE_IPG0, AX88772_IPG0_DEFAULT | AX88772_IPG1_DEFAULT,AX88772_IPG2_DEFAULT, 0, buf)) < 0) {
  778. dbg("Write IPG,IPG1,IPG2 failed: %d", ret);
  779. goto out2;
  780. }
  781. if ((ret =
  782. ax8817x_write_cmd(dev, AX_CMD_SET_HW_MII, 0, 0, 0, &buf)) < 0) {
  783. dbg("Failed to set hardware MII: %02x", ret);
  784. goto out2;
  785. }
  786. /* Set RX_CTL to default values with 2k buffer, and enable cactus */
  787. if ((ret =
  788. ax8817x_write_cmd(dev, AX_CMD_WRITE_RX_CTL, 0x0088, 0, 0,
  789. buf)) < 0) {
  790. dbg("Reset RX_CTL failed: %d", ret);
  791. goto out2;
  792. }
  793. kfree(buf);
  794. return 0;
  795. out2:
  796. kfree(buf);
  797. out1:
  798. return ret;
  799. }
  800. static int ax88772_rx_fixup(struct usbnet *dev, struct sk_buff *skb)
  801. {
  802. u32 *header;
  803. char *packet;
  804. struct sk_buff *ax_skb;
  805. u16 size;
  806. header = (u32 *) skb->data;
  807. le32_to_cpus(header);
  808. packet = (char *)(header + 1);
  809. skb_pull(skb, 4);
  810. while (skb->len > 0) {
  811. if ((short)(*header & 0x0000ffff) !=
  812. ~((short)((*header & 0xffff0000) >> 16))) {
  813. devdbg(dev,"header length data is error");
  814. }
  815. /* get the packet length */
  816. size = (u16) (*header & 0x0000ffff);
  817. if ((skb->len) - ((size + 1) & 0xfffe) == 0)
  818. return 2;
  819. if (size > ETH_FRAME_LEN) {
  820. devdbg(dev,"invalid rx length %d", size);
  821. return 0;
  822. }
  823. ax_skb = skb_clone(skb, GFP_ATOMIC);
  824. if (ax_skb) {
  825. ax_skb->len = size;
  826. ax_skb->data = packet;
  827. ax_skb->tail = packet + size;
  828. skb_return(dev, ax_skb);
  829. } else {
  830. return 0;
  831. }
  832. skb_pull(skb, (size + 1) & 0xfffe);
  833. if (skb->len == 0)
  834. break;
  835. header = (u32 *) skb->data;
  836. le32_to_cpus(header);
  837. packet = (char *)(header + 1);
  838. skb_pull(skb, 4);
  839. }
  840. if (skb->len < 0) {
  841. devdbg(dev,"invalid rx length %d", skb->len);
  842. return 0;
  843. }
  844. return 1;
  845. }
  846. static struct sk_buff *ax88772_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
  847. unsigned flags)
  848. {
  849. int padlen;
  850. int headroom = skb_headroom(skb);
  851. int tailroom = skb_tailroom(skb);
  852. u32 *packet_len;
  853. u32 *padbytes_ptr;
  854. padlen = ((skb->len + 4) % 512) ? 0 : 4;
  855. if ((!skb_cloned(skb))
  856. && ((headroom + tailroom) >= (4 + padlen))) {
  857. if ((headroom < 4) || (tailroom < padlen)) {
  858. skb->data = memmove(skb->head + 4, skb->data, skb->len);
  859. skb->tail = skb->data + skb->len;
  860. }
  861. } else {
  862. struct sk_buff *skb2;
  863. skb2 = skb_copy_expand(skb, 4, padlen, flags);
  864. dev_kfree_skb_any(skb);
  865. skb = skb2;
  866. if (!skb)
  867. return NULL;
  868. }
  869. packet_len = (u32 *) skb_push(skb, 4);
  870. packet_len = (u32 *) skb->data;
  871. *packet_len = (((skb->len - 4) ^ 0x0000ffff) << 16) + (skb->len - 4);
  872. if ((skb->len % 512) == 0) {
  873. padbytes_ptr = (u32 *) skb->tail;
  874. *padbytes_ptr = 0xffff0000;
  875. skb_put(skb, padlen);
  876. }
  877. return skb;
  878. }
  879. static int ax88772_link_reset(struct usbnet *dev)
  880. {
  881. u16 lpa;
  882. u16 mode;
  883. mode = AX88772_MEDIUM_DEFAULT;
  884. lpa = ax8817x_mdio_read(dev->net, dev->mii.phy_id, MII_LPA);
  885. if ((lpa & LPA_DUPLEX) == 0)
  886. mode &= ~AX88772_MEDIUM_FULL_DUPLEX;
  887. if ((lpa & LPA_100) == 0)
  888. mode &= ~AX88772_MEDIUM_100MB;
  889. ax8817x_write_cmd(dev, AX_CMD_WRITE_MEDIUM_MODE, mode, 0, 0, NULL);
  890. return 0;
  891. }
  892. static const struct driver_info ax8817x_info = {
  893. .description = "ASIX AX8817x USB 2.0 Ethernet",
  894. .bind = ax8817x_bind,
  895. .status = ax8817x_status,
  896. .link_reset = ax88172_link_reset,
  897. .reset = ax88172_link_reset,
  898. .flags = FLAG_ETHER,
  899. .data = 0x00130103,
  900. };
  901. static const struct driver_info dlink_dub_e100_info = {
  902. .description = "DLink DUB-E100 USB Ethernet",
  903. .bind = ax8817x_bind,
  904. .status = ax8817x_status,
  905. .link_reset = ax88172_link_reset,
  906. .reset = ax88172_link_reset,
  907. .flags = FLAG_ETHER,
  908. .data = 0x009f9d9f,
  909. };
  910. static const struct driver_info netgear_fa120_info = {
  911. .description = "Netgear FA-120 USB Ethernet",
  912. .bind = ax8817x_bind,
  913. .status = ax8817x_status,
  914. .link_reset = ax88172_link_reset,
  915. .reset = ax88172_link_reset,
  916. .flags = FLAG_ETHER,
  917. .data = 0x00130103,
  918. };
  919. static const struct driver_info hawking_uf200_info = {
  920. .description = "Hawking UF200 USB Ethernet",
  921. .bind = ax8817x_bind,
  922. .status = ax8817x_status,
  923. .link_reset = ax88172_link_reset,
  924. .reset = ax88172_link_reset,
  925. .flags = FLAG_ETHER,
  926. .data = 0x001f1d1f,
  927. };
  928. static const struct driver_info ax88772_info = {
  929. .description = "ASIX AX88772 USB 2.0 Ethernet",
  930. .bind = ax88772_bind,
  931. .status = ax8817x_status,
  932. .link_reset = ax88772_link_reset,
  933. .reset = ax88772_link_reset,
  934. .flags = FLAG_ETHER | FLAG_FRAMING_AX,
  935. .rx_fixup = ax88772_rx_fixup,
  936. .tx_fixup = ax88772_tx_fixup,
  937. .data = 0x00130103,
  938. };
  939. #endif /* CONFIG_USB_AX8817X */
  940. /*-------------------------------------------------------------------------
  941. *
  942. * Communications Device Class declarations.
  943. * Used by CDC Ethernet, and some CDC variants
  944. *
  945. *-------------------------------------------------------------------------*/
  946. #ifdef CONFIG_USB_CDCETHER
  947. #define NEED_GENERIC_CDC
  948. #endif
  949. #ifdef CONFIG_USB_ZAURUS
  950. /* Ethernet variant uses funky framing, broken ethernet addressing */
  951. #define NEED_GENERIC_CDC
  952. #endif
  953. #ifdef CONFIG_USB_RNDIS
  954. /* ACM variant uses even funkier framing, complex control RPC scheme */
  955. #define NEED_GENERIC_CDC
  956. #endif
  957. #ifdef NEED_GENERIC_CDC
  958. #include <linux/usb_cdc.h>
  959. struct cdc_state {
  960. struct usb_cdc_header_desc *header;
  961. struct usb_cdc_union_desc *u;
  962. struct usb_cdc_ether_desc *ether;
  963. struct usb_interface *control;
  964. struct usb_interface *data;
  965. };
  966. static struct usb_driver usbnet_driver;
  967. /*
  968. * probes control interface, claims data interface, collects the bulk
  969. * endpoints, activates data interface (if needed), maybe sets MTU.
  970. * all pure cdc, except for certain firmware workarounds.
  971. */
  972. static int generic_cdc_bind (struct usbnet *dev, struct usb_interface *intf)
  973. {
  974. u8 *buf = intf->cur_altsetting->extra;
  975. int len = intf->cur_altsetting->extralen;
  976. struct usb_interface_descriptor *d;
  977. struct cdc_state *info = (void *) &dev->data;
  978. int status;
  979. int rndis;
  980. if (sizeof dev->data < sizeof *info)
  981. return -EDOM;
  982. /* expect strict spec conformance for the descriptors, but
  983. * cope with firmware which stores them in the wrong place
  984. */
  985. if (len == 0 && dev->udev->actconfig->extralen) {
  986. /* Motorola SB4100 (and others: Brad Hards says it's
  987. * from a Broadcom design) put CDC descriptors here
  988. */
  989. buf = dev->udev->actconfig->extra;
  990. len = dev->udev->actconfig->extralen;
  991. if (len)
  992. dev_dbg (&intf->dev,
  993. "CDC descriptors on config\n");
  994. }
  995. /* this assumes that if there's a non-RNDIS vendor variant
  996. * of cdc-acm, it'll fail RNDIS requests cleanly.
  997. */
  998. rndis = (intf->cur_altsetting->desc.bInterfaceProtocol == 0xff);
  999. memset (info, 0, sizeof *info);
  1000. info->control = intf;
  1001. while (len > 3) {
  1002. if (buf [1] != USB_DT_CS_INTERFACE)
  1003. goto next_desc;
  1004. /* use bDescriptorSubType to identify the CDC descriptors.
  1005. * We expect devices with CDC header and union descriptors.
  1006. * For CDC Ethernet we need the ethernet descriptor.
  1007. * For RNDIS, ignore two (pointless) CDC modem descriptors
  1008. * in favor of a complicated OID-based RPC scheme doing what
  1009. * CDC Ethernet achieves with a simple descriptor.
  1010. */
  1011. switch (buf [2]) {
  1012. case USB_CDC_HEADER_TYPE:
  1013. if (info->header) {
  1014. dev_dbg (&intf->dev, "extra CDC header\n");
  1015. goto bad_desc;
  1016. }
  1017. info->header = (void *) buf;
  1018. if (info->header->bLength != sizeof *info->header) {
  1019. dev_dbg (&intf->dev, "CDC header len %u\n",
  1020. info->header->bLength);
  1021. goto bad_desc;
  1022. }
  1023. break;
  1024. case USB_CDC_UNION_TYPE:
  1025. if (info->u) {
  1026. dev_dbg (&intf->dev, "extra CDC union\n");
  1027. goto bad_desc;
  1028. }
  1029. info->u = (void *) buf;
  1030. if (info->u->bLength != sizeof *info->u) {
  1031. dev_dbg (&intf->dev, "CDC union len %u\n",
  1032. info->u->bLength);
  1033. goto bad_desc;
  1034. }
  1035. /* we need a master/control interface (what we're
  1036. * probed with) and a slave/data interface; union
  1037. * descriptors sort this all out.
  1038. */
  1039. info->control = usb_ifnum_to_if(dev->udev,
  1040. info->u->bMasterInterface0);
  1041. info->data = usb_ifnum_to_if(dev->udev,
  1042. info->u->bSlaveInterface0);
  1043. if (!info->control || !info->data) {
  1044. dev_dbg (&intf->dev,
  1045. "master #%u/%p slave #%u/%p\n",
  1046. info->u->bMasterInterface0,
  1047. info->control,
  1048. info->u->bSlaveInterface0,
  1049. info->data);
  1050. goto bad_desc;
  1051. }
  1052. if (info->control != intf) {
  1053. dev_dbg (&intf->dev, "bogus CDC Union\n");
  1054. /* Ambit USB Cable Modem (and maybe others)
  1055. * interchanges master and slave interface.
  1056. */
  1057. if (info->data == intf) {
  1058. info->data = info->control;
  1059. info->control = intf;
  1060. } else
  1061. goto bad_desc;
  1062. }
  1063. /* a data interface altsetting does the real i/o */
  1064. d = &info->data->cur_altsetting->desc;
  1065. if (d->bInterfaceClass != USB_CLASS_CDC_DATA) {
  1066. dev_dbg (&intf->dev, "slave class %u\n",
  1067. d->bInterfaceClass);
  1068. goto bad_desc;
  1069. }
  1070. break;
  1071. case USB_CDC_ETHERNET_TYPE:
  1072. if (info->ether) {
  1073. dev_dbg (&intf->dev, "extra CDC ether\n");
  1074. goto bad_desc;
  1075. }
  1076. info->ether = (void *) buf;
  1077. if (info->ether->bLength != sizeof *info->ether) {
  1078. dev_dbg (&intf->dev, "CDC ether len %u\n",
  1079. info->ether->bLength);
  1080. goto bad_desc;
  1081. }
  1082. dev->hard_mtu = le16_to_cpu(
  1083. info->ether->wMaxSegmentSize);
  1084. /* because of Zaurus, we may be ignoring the host
  1085. * side link address we were given.
  1086. */
  1087. break;
  1088. }
  1089. next_desc:
  1090. len -= buf [0]; /* bLength */
  1091. buf += buf [0];
  1092. }
  1093. if (!info->header || !info->u || (!rndis && !info->ether)) {
  1094. dev_dbg (&intf->dev, "missing cdc %s%s%sdescriptor\n",
  1095. info->header ? "" : "header ",
  1096. info->u ? "" : "union ",
  1097. info->ether ? "" : "ether ");
  1098. goto bad_desc;
  1099. }
  1100. /* claim data interface and set it up ... with side effects.
  1101. * network traffic can't flow until an altsetting is enabled.
  1102. */
  1103. status = usb_driver_claim_interface (&usbnet_driver, info->data, dev);
  1104. if (status < 0)
  1105. return status;
  1106. status = get_endpoints (dev, info->data);
  1107. if (status < 0) {
  1108. /* ensure immediate exit from usbnet_disconnect */
  1109. usb_set_intfdata(info->data, NULL);
  1110. usb_driver_release_interface (&usbnet_driver, info->data);
  1111. return status;
  1112. }
  1113. /* status endpoint: optional for CDC Ethernet, not RNDIS (or ACM) */
  1114. dev->status = NULL;
  1115. if (info->control->cur_altsetting->desc.bNumEndpoints == 1) {
  1116. struct usb_endpoint_descriptor *desc;
  1117. dev->status = &info->control->cur_altsetting->endpoint [0];
  1118. desc = &dev->status->desc;
  1119. if (desc->bmAttributes != USB_ENDPOINT_XFER_INT
  1120. || !(desc->bEndpointAddress & USB_DIR_IN)
  1121. || (le16_to_cpu(desc->wMaxPacketSize)
  1122. < sizeof (struct usb_cdc_notification))
  1123. || !desc->bInterval) {
  1124. dev_dbg (&intf->dev, "bad notification endpoint\n");
  1125. dev->status = NULL;
  1126. }
  1127. }
  1128. if (rndis && !dev->status) {
  1129. dev_dbg (&intf->dev, "missing RNDIS status endpoint\n");
  1130. usb_set_intfdata(info->data, NULL);
  1131. usb_driver_release_interface (&usbnet_driver, info->data);
  1132. return -ENODEV;
  1133. }
  1134. return 0;
  1135. bad_desc:
  1136. dev_info (&dev->udev->dev, "bad CDC descriptors\n");
  1137. return -ENODEV;
  1138. }
  1139. static void cdc_unbind (struct usbnet *dev, struct usb_interface *intf)
  1140. {
  1141. struct cdc_state *info = (void *) &dev->data;
  1142. /* disconnect master --> disconnect slave */
  1143. if (intf == info->control && info->data) {
  1144. /* ensure immediate exit from usbnet_disconnect */
  1145. usb_set_intfdata(info->data, NULL);
  1146. usb_driver_release_interface (&usbnet_driver, info->data);
  1147. info->data = NULL;
  1148. }
  1149. /* and vice versa (just in case) */
  1150. else if (intf == info->data && info->control) {
  1151. /* ensure immediate exit from usbnet_disconnect */
  1152. usb_set_intfdata(info->control, NULL);
  1153. usb_driver_release_interface (&usbnet_driver, info->control);
  1154. info->control = NULL;
  1155. }
  1156. }
  1157. #endif /* NEED_GENERIC_CDC */
  1158. #ifdef CONFIG_USB_CDCETHER
  1159. #define HAVE_HARDWARE
  1160. /*-------------------------------------------------------------------------
  1161. *
  1162. * Communications Device Class, Ethernet Control model
  1163. *
  1164. * Takes two interfaces. The DATA interface is inactive till an altsetting
  1165. * is selected. Configuration data includes class descriptors.
  1166. *
  1167. * This should interop with whatever the 2.4 "CDCEther.c" driver
  1168. * (by Brad Hards) talked with.
  1169. *
  1170. *-------------------------------------------------------------------------*/
  1171. #include <linux/ctype.h>
  1172. static void dumpspeed (struct usbnet *dev, __le32 *speeds)
  1173. {
  1174. if (netif_msg_timer (dev))
  1175. devinfo (dev, "link speeds: %u kbps up, %u kbps down",
  1176. __le32_to_cpu(speeds[0]) / 1000,
  1177. __le32_to_cpu(speeds[1]) / 1000);
  1178. }
  1179. static void cdc_status (struct usbnet *dev, struct urb *urb)
  1180. {
  1181. struct usb_cdc_notification *event;
  1182. if (urb->actual_length < sizeof *event)
  1183. return;
  1184. /* SPEED_CHANGE can get split into two 8-byte packets */
  1185. if (test_and_clear_bit (EVENT_STS_SPLIT, &dev->flags)) {
  1186. dumpspeed (dev, (__le32 *) urb->transfer_buffer);
  1187. return;
  1188. }
  1189. event = urb->transfer_buffer;
  1190. switch (event->bNotificationType) {
  1191. case USB_CDC_NOTIFY_NETWORK_CONNECTION:
  1192. if (netif_msg_timer (dev))
  1193. devdbg (dev, "CDC: carrier %s",
  1194. event->wValue ? "on" : "off");
  1195. if (event->wValue)
  1196. netif_carrier_on(dev->net);
  1197. else
  1198. netif_carrier_off(dev->net);
  1199. break;
  1200. case USB_CDC_NOTIFY_SPEED_CHANGE: /* tx/rx rates */
  1201. if (netif_msg_timer (dev))
  1202. devdbg (dev, "CDC: speed change (len %d)",
  1203. urb->actual_length);
  1204. if (urb->actual_length != (sizeof *event + 8))
  1205. set_bit (EVENT_STS_SPLIT, &dev->flags);
  1206. else
  1207. dumpspeed (dev, (__le32 *) &event[1]);
  1208. break;
  1209. // case USB_CDC_NOTIFY_RESPONSE_AVAILABLE: /* RNDIS; or unsolicited */
  1210. default:
  1211. deverr (dev, "CDC: unexpected notification %02x!",
  1212. event->bNotificationType);
  1213. break;
  1214. }
  1215. }
  1216. static u8 nibble (unsigned char c)
  1217. {
  1218. if (likely (isdigit (c)))
  1219. return c - '0';
  1220. c = toupper (c);
  1221. if (likely (isxdigit (c)))
  1222. return 10 + c - 'A';
  1223. return 0;
  1224. }
  1225. static inline int
  1226. get_ethernet_addr (struct usbnet *dev, struct usb_cdc_ether_desc *e)
  1227. {
  1228. int tmp, i;
  1229. unsigned char buf [13];
  1230. tmp = usb_string (dev->udev, e->iMACAddress, buf, sizeof buf);
  1231. if (tmp != 12) {
  1232. dev_dbg (&dev->udev->dev,
  1233. "bad MAC string %d fetch, %d\n", e->iMACAddress, tmp);
  1234. if (tmp >= 0)
  1235. tmp = -EINVAL;
  1236. return tmp;
  1237. }
  1238. for (i = tmp = 0; i < 6; i++, tmp += 2)
  1239. dev->net->dev_addr [i] =
  1240. (nibble (buf [tmp]) << 4) + nibble (buf [tmp + 1]);
  1241. return 0;
  1242. }
  1243. static int cdc_bind (struct usbnet *dev, struct usb_interface *intf)
  1244. {
  1245. int status;
  1246. struct cdc_state *info = (void *) &dev->data;
  1247. status = generic_cdc_bind (dev, intf);
  1248. if (status < 0)
  1249. return status;
  1250. status = get_ethernet_addr (dev, info->ether);
  1251. if (status < 0) {
  1252. usb_set_intfdata(info->data, NULL);
  1253. usb_driver_release_interface (&usbnet_driver, info->data);
  1254. return status;
  1255. }
  1256. /* FIXME cdc-ether has some multicast code too, though it complains
  1257. * in routine cases. info->ether describes the multicast support.
  1258. */
  1259. return 0;
  1260. }
  1261. static const struct driver_info cdc_info = {
  1262. .description = "CDC Ethernet Device",
  1263. .flags = FLAG_ETHER,
  1264. // .check_connect = cdc_check_connect,
  1265. .bind = cdc_bind,
  1266. .unbind = cdc_unbind,
  1267. .status = cdc_status,
  1268. };
  1269. #endif /* CONFIG_USB_CDCETHER */
  1270. #ifdef CONFIG_USB_GENESYS
  1271. #define HAVE_HARDWARE
  1272. /*-------------------------------------------------------------------------
  1273. *
  1274. * GeneSys GL620USB-A (www.genesyslogic.com.tw)
  1275. *
  1276. * ... should partially interop with the Win32 driver for this hardware
  1277. * The GeneSys docs imply there's some NDIS issue motivating this framing.
  1278. *
  1279. * Some info from GeneSys:
  1280. * - GL620USB-A is full duplex; GL620USB is only half duplex for bulk.
  1281. * (Some cables, like the BAFO-100c, use the half duplex version.)
  1282. * - For the full duplex model, the low bit of the version code says
  1283. * which side is which ("left/right").
  1284. * - For the half duplex type, a control/interrupt handshake settles
  1285. * the transfer direction. (That's disabled here, partially coded.)
  1286. * A control URB would block until other side writes an interrupt.
  1287. *
  1288. * Original code from Jiun-Jie Huang <huangjj@genesyslogic.com.tw>
  1289. * and merged into "usbnet" by Stanislav Brabec <utx@penguin.cz>.
  1290. *
  1291. *-------------------------------------------------------------------------*/
  1292. // control msg write command
  1293. #define GENELINK_CONNECT_WRITE 0xF0
  1294. // interrupt pipe index
  1295. #define GENELINK_INTERRUPT_PIPE 0x03
  1296. // interrupt read buffer size
  1297. #define INTERRUPT_BUFSIZE 0x08
  1298. // interrupt pipe interval value
  1299. #define GENELINK_INTERRUPT_INTERVAL 0x10
  1300. // max transmit packet number per transmit
  1301. #define GL_MAX_TRANSMIT_PACKETS 32
  1302. // max packet length
  1303. #define GL_MAX_PACKET_LEN 1514
  1304. // max receive buffer size
  1305. #define GL_RCV_BUF_SIZE \
  1306. (((GL_MAX_PACKET_LEN + 4) * GL_MAX_TRANSMIT_PACKETS) + 4)
  1307. struct gl_packet {
  1308. u32 packet_length;
  1309. char packet_data [1];
  1310. };
  1311. struct gl_header {
  1312. u32 packet_count;
  1313. struct gl_packet packets;
  1314. };
  1315. #ifdef GENELINK_ACK
  1316. // FIXME: this code is incomplete, not debugged; it doesn't
  1317. // handle interrupts correctly. interrupts should be generic
  1318. // code like all other device I/O, anyway.
  1319. struct gl_priv {
  1320. struct urb *irq_urb;
  1321. char irq_buf [INTERRUPT_BUFSIZE];
  1322. };
  1323. static inline int gl_control_write (struct usbnet *dev, u8 request, u16 value)
  1324. {
  1325. int retval;
  1326. retval = usb_control_msg (dev->udev,
  1327. usb_sndctrlpipe (dev->udev, 0),
  1328. request,
  1329. USB_DIR_OUT | USB_TYPE_CLASS | USB_RECIP_INTERFACE,
  1330. value,
  1331. 0, // index
  1332. 0, // data buffer
  1333. 0, // size
  1334. CONTROL_TIMEOUT_MS);
  1335. return retval;
  1336. }
  1337. static void gl_interrupt_complete (struct urb *urb, struct pt_regs *regs)
  1338. {
  1339. int status = urb->status;
  1340. switch (status) {
  1341. case 0:
  1342. /* success */
  1343. break;
  1344. case -ECONNRESET:
  1345. case -ENOENT:
  1346. case -ESHUTDOWN:
  1347. /* this urb is terminated, clean up */
  1348. dbg("%s - urb shutting down with status: %d",
  1349. __FUNCTION__, status);
  1350. return;
  1351. default:
  1352. dbg("%s - nonzero urb status received: %d",
  1353. __FUNCTION__, urb->status);
  1354. }
  1355. status = usb_submit_urb (urb, GFP_ATOMIC);
  1356. if (status)
  1357. err ("%s - usb_submit_urb failed with result %d",
  1358. __FUNCTION__, status);
  1359. }
  1360. static int gl_interrupt_read (struct usbnet *dev)
  1361. {
  1362. struct gl_priv *priv = dev->priv_data;
  1363. int retval;
  1364. // issue usb interrupt read
  1365. if (priv && priv->irq_urb) {
  1366. // submit urb
  1367. if ((retval = usb_submit_urb (priv->irq_urb, GFP_KERNEL)) != 0)
  1368. dbg ("gl_interrupt_read: submit fail - %X...", retval);
  1369. else
  1370. dbg ("gl_interrupt_read: submit success...");
  1371. }
  1372. return 0;
  1373. }
  1374. // check whether another side is connected
  1375. static int genelink_check_connect (struct usbnet *dev)
  1376. {
  1377. int retval;
  1378. dbg ("genelink_check_connect...");
  1379. // detect whether another side is connected
  1380. if ((retval = gl_control_write (dev, GENELINK_CONNECT_WRITE, 0)) != 0) {
  1381. dbg ("%s: genelink_check_connect write fail - %X",
  1382. dev->net->name, retval);
  1383. return retval;
  1384. }
  1385. // usb interrupt read to ack another side
  1386. if ((retval = gl_interrupt_read (dev)) != 0) {
  1387. dbg ("%s: genelink_check_connect read fail - %X",
  1388. dev->net->name, retval);
  1389. return retval;
  1390. }
  1391. dbg ("%s: genelink_check_connect read success", dev->net->name);
  1392. return 0;
  1393. }
  1394. // allocate and initialize the private data for genelink
  1395. static int genelink_init (struct usbnet *dev)
  1396. {
  1397. struct gl_priv *priv;
  1398. // allocate the private data structure
  1399. if ((priv = kmalloc (sizeof *priv, GFP_KERNEL)) == 0) {
  1400. dbg ("%s: cannot allocate private data per device",
  1401. dev->net->name);
  1402. return -ENOMEM;
  1403. }
  1404. // allocate irq urb
  1405. if ((priv->irq_urb = usb_alloc_urb (0, GFP_KERNEL)) == 0) {
  1406. dbg ("%s: cannot allocate private irq urb per device",
  1407. dev->net->name);
  1408. kfree (priv);
  1409. return -ENOMEM;
  1410. }
  1411. // fill irq urb
  1412. usb_fill_int_urb (priv->irq_urb, dev->udev,
  1413. usb_rcvintpipe (dev->udev, GENELINK_INTERRUPT_PIPE),
  1414. priv->irq_buf, INTERRUPT_BUFSIZE,
  1415. gl_interrupt_complete, 0,
  1416. GENELINK_INTERRUPT_INTERVAL);
  1417. // set private data pointer
  1418. dev->priv_data = priv;
  1419. return 0;
  1420. }
  1421. // release the private data
  1422. static int genelink_free (struct usbnet *dev)
  1423. {
  1424. struct gl_priv *priv = dev->priv_data;
  1425. if (!priv)
  1426. return 0;
  1427. // FIXME: can't cancel here; it's synchronous, and
  1428. // should have happened earlier in any case (interrupt
  1429. // handling needs to be generic)
  1430. // cancel irq urb first
  1431. usb_kill_urb (priv->irq_urb);
  1432. // free irq urb
  1433. usb_free_urb (priv->irq_urb);
  1434. // free the private data structure
  1435. kfree (priv);
  1436. return 0;
  1437. }
  1438. #endif
  1439. static int genelink_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
  1440. {
  1441. struct gl_header *header;
  1442. struct gl_packet *packet;
  1443. struct sk_buff *gl_skb;
  1444. u32 size;
  1445. header = (struct gl_header *) skb->data;
  1446. // get the packet count of the received skb
  1447. le32_to_cpus (&header->packet_count);
  1448. if ((header->packet_count > GL_MAX_TRANSMIT_PACKETS)
  1449. || (header->packet_count < 0)) {
  1450. dbg ("genelink: invalid received packet count %d",
  1451. header->packet_count);
  1452. return 0;
  1453. }
  1454. // set the current packet pointer to the first packet
  1455. packet = &header->packets;
  1456. // decrement the length for the packet count size 4 bytes
  1457. skb_pull (skb, 4);
  1458. while (header->packet_count > 1) {
  1459. // get the packet length
  1460. size = packet->packet_length;
  1461. // this may be a broken packet
  1462. if (size > GL_MAX_PACKET_LEN) {
  1463. dbg ("genelink: invalid rx length %d", size);
  1464. return 0;
  1465. }
  1466. // allocate the skb for the individual packet
  1467. gl_skb = alloc_skb (size, GFP_ATOMIC);
  1468. if (gl_skb) {
  1469. // copy the packet data to the new skb
  1470. memcpy(skb_put(gl_skb, size), packet->packet_data, size);
  1471. skb_return (dev, gl_skb);
  1472. }
  1473. // advance to the next packet
  1474. packet = (struct gl_packet *)
  1475. &packet->packet_data [size];
  1476. header->packet_count--;
  1477. // shift the data pointer to the next gl_packet
  1478. skb_pull (skb, size + 4);
  1479. }
  1480. // skip the packet length field 4 bytes
  1481. skb_pull (skb, 4);
  1482. if (skb->len > GL_MAX_PACKET_LEN) {
  1483. dbg ("genelink: invalid rx length %d", skb->len);
  1484. return 0;
  1485. }
  1486. return 1;
  1487. }
  1488. static struct sk_buff *
  1489. genelink_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
  1490. {
  1491. int padlen;
  1492. int length = skb->len;
  1493. int headroom = skb_headroom (skb);
  1494. int tailroom = skb_tailroom (skb);
  1495. u32 *packet_count;
  1496. u32 *packet_len;
  1497. // FIXME: magic numbers, bleech
  1498. padlen = ((skb->len + (4 + 4*1)) % 64) ? 0 : 1;
  1499. if ((!skb_cloned (skb))
  1500. && ((headroom + tailroom) >= (padlen + (4 + 4*1)))) {
  1501. if ((headroom < (4 + 4*1)) || (tailroom < padlen)) {
  1502. skb->data = memmove (skb->head + (4 + 4*1),
  1503. skb->data, skb->len);
  1504. skb->tail = skb->data + skb->len;
  1505. }
  1506. } else {
  1507. struct sk_buff *skb2;
  1508. skb2 = skb_copy_expand (skb, (4 + 4*1) , padlen, flags);
  1509. dev_kfree_skb_any (skb);
  1510. skb = skb2;
  1511. if (!skb)
  1512. return NULL;
  1513. }
  1514. // attach the packet count to the header
  1515. packet_count = (u32 *) skb_push (skb, (4 + 4*1));
  1516. packet_len = packet_count + 1;
  1517. // FIXME little endian?
  1518. *packet_count = 1;
  1519. *packet_len = length;
  1520. // add padding byte
  1521. if ((skb->len % dev->maxpacket) == 0)
  1522. skb_put (skb, 1);
  1523. return skb;
  1524. }
  1525. static int genelink_bind (struct usbnet *dev, struct usb_interface *intf)
  1526. {
  1527. dev->hard_mtu = GL_RCV_BUF_SIZE;
  1528. dev->net->hard_header_len += 4;
  1529. return 0;
  1530. }
  1531. static const struct driver_info genelink_info = {
  1532. .description = "Genesys GeneLink",
  1533. .flags = FLAG_FRAMING_GL | FLAG_NO_SETINT,
  1534. .bind = genelink_bind,
  1535. .rx_fixup = genelink_rx_fixup,
  1536. .tx_fixup = genelink_tx_fixup,
  1537. .in = 1, .out = 2,
  1538. #ifdef GENELINK_ACK
  1539. .check_connect =genelink_check_connect,
  1540. #endif
  1541. };
  1542. #endif /* CONFIG_USB_GENESYS */
  1543. #ifdef CONFIG_USB_NET1080
  1544. #define HAVE_HARDWARE
  1545. /*-------------------------------------------------------------------------
  1546. *
  1547. * Netchip 1080 driver ... http://www.netchip.com
  1548. * Used in LapLink cables
  1549. *
  1550. *-------------------------------------------------------------------------*/
  1551. #define frame_errors data[1]
  1552. /*
  1553. * NetChip framing of ethernet packets, supporting additional error
  1554. * checks for links that may drop bulk packets from inside messages.
  1555. * Odd USB length == always short read for last usb packet.
  1556. * - nc_header
  1557. * - Ethernet header (14 bytes)
  1558. * - payload
  1559. * - (optional padding byte, if needed so length becomes odd)
  1560. * - nc_trailer
  1561. *
  1562. * This framing is to be avoided for non-NetChip devices.
  1563. */
  1564. struct nc_header { // packed:
  1565. __le16 hdr_len; // sizeof nc_header (LE, all)
  1566. __le16 packet_len; // payload size (including ethhdr)
  1567. __le16 packet_id; // detects dropped packets
  1568. #define MIN_HEADER 6
  1569. // all else is optional, and must start with:
  1570. // u16 vendorId; // from usb-if
  1571. // u16 productId;
  1572. } __attribute__((__packed__));
  1573. #define PAD_BYTE ((unsigned char)0xAC)
  1574. struct nc_trailer {
  1575. __le16 packet_id;
  1576. } __attribute__((__packed__));
  1577. // packets may use FLAG_FRAMING_NC and optional pad
  1578. #define FRAMED_SIZE(mtu) (sizeof (struct nc_header) \
  1579. + sizeof (struct ethhdr) \
  1580. + (mtu) \
  1581. + 1 \
  1582. + sizeof (struct nc_trailer))
  1583. #define MIN_FRAMED FRAMED_SIZE(0)
  1584. /* packets _could_ be up to 64KB... */
  1585. #define NC_MAX_PACKET 32767
  1586. /*
  1587. * Zero means no timeout; else, how long a 64 byte bulk packet may be queued
  1588. * before the hardware drops it. If that's done, the driver will need to
  1589. * frame network packets to guard against the dropped USB packets. The win32
  1590. * driver sets this for both sides of the link.
  1591. */
  1592. #define NC_READ_TTL_MS ((u8)255) // ms
  1593. /*
  1594. * We ignore most registers and EEPROM contents.
  1595. */
  1596. #define REG_USBCTL ((u8)0x04)
  1597. #define REG_TTL ((u8)0x10)
  1598. #define REG_STATUS ((u8)0x11)
  1599. /*
  1600. * Vendor specific requests to read/write data
  1601. */
  1602. #define REQUEST_REGISTER ((u8)0x10)
  1603. #define REQUEST_EEPROM ((u8)0x11)
  1604. static int
  1605. nc_vendor_read (struct usbnet *dev, u8 req, u8 regnum, u16 *retval_ptr)
  1606. {
  1607. int status = usb_control_msg (dev->udev,
  1608. usb_rcvctrlpipe (dev->udev, 0),
  1609. req,
  1610. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  1611. 0, regnum,
  1612. retval_ptr, sizeof *retval_ptr,
  1613. CONTROL_TIMEOUT_MS);
  1614. if (status > 0)
  1615. status = 0;
  1616. if (!status)
  1617. le16_to_cpus (retval_ptr);
  1618. return status;
  1619. }
  1620. static inline int
  1621. nc_register_read (struct usbnet *dev, u8 regnum, u16 *retval_ptr)
  1622. {
  1623. return nc_vendor_read (dev, REQUEST_REGISTER, regnum, retval_ptr);
  1624. }
  1625. // no retval ... can become async, usable in_interrupt()
  1626. static void
  1627. nc_vendor_write (struct usbnet *dev, u8 req, u8 regnum, u16 value)
  1628. {
  1629. usb_control_msg (dev->udev,
  1630. usb_sndctrlpipe (dev->udev, 0),
  1631. req,
  1632. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  1633. value, regnum,
  1634. NULL, 0, // data is in setup packet
  1635. CONTROL_TIMEOUT_MS);
  1636. }
  1637. static inline void
  1638. nc_register_write (struct usbnet *dev, u8 regnum, u16 value)
  1639. {
  1640. nc_vendor_write (dev, REQUEST_REGISTER, regnum, value);
  1641. }
  1642. #if 0
  1643. static void nc_dump_registers (struct usbnet *dev)
  1644. {
  1645. u8 reg;
  1646. u16 *vp = kmalloc (sizeof (u16));
  1647. if (!vp) {
  1648. dbg ("no memory?");
  1649. return;
  1650. }
  1651. dbg ("%s registers:", dev->net->name);
  1652. for (reg = 0; reg < 0x20; reg++) {
  1653. int retval;
  1654. // reading some registers is trouble
  1655. if (reg >= 0x08 && reg <= 0xf)
  1656. continue;
  1657. if (reg >= 0x12 && reg <= 0x1e)
  1658. continue;
  1659. retval = nc_register_read (dev, reg, vp);
  1660. if (retval < 0)
  1661. dbg ("%s reg [0x%x] ==> error %d",
  1662. dev->net->name, reg, retval);
  1663. else
  1664. dbg ("%s reg [0x%x] = 0x%x",
  1665. dev->net->name, reg, *vp);
  1666. }
  1667. kfree (vp);
  1668. }
  1669. #endif
  1670. /*-------------------------------------------------------------------------*/
  1671. /*
  1672. * Control register
  1673. */
  1674. #define USBCTL_WRITABLE_MASK 0x1f0f
  1675. // bits 15-13 reserved, r/o
  1676. #define USBCTL_ENABLE_LANG (1 << 12)
  1677. #define USBCTL_ENABLE_MFGR (1 << 11)
  1678. #define USBCTL_ENABLE_PROD (1 << 10)
  1679. #define USBCTL_ENABLE_SERIAL (1 << 9)
  1680. #define USBCTL_ENABLE_DEFAULTS (1 << 8)
  1681. // bits 7-4 reserved, r/o
  1682. #define USBCTL_FLUSH_OTHER (1 << 3)
  1683. #define USBCTL_FLUSH_THIS (1 << 2)
  1684. #define USBCTL_DISCONN_OTHER (1 << 1)
  1685. #define USBCTL_DISCONN_THIS (1 << 0)
  1686. static inline void nc_dump_usbctl (struct usbnet *dev, u16 usbctl)
  1687. {
  1688. if (!netif_msg_link (dev))
  1689. return;
  1690. devdbg (dev, "net1080 %s-%s usbctl 0x%x:%s%s%s%s%s;"
  1691. " this%s%s;"
  1692. " other%s%s; r/o 0x%x",
  1693. dev->udev->bus->bus_name, dev->udev->devpath,
  1694. usbctl,
  1695. (usbctl & USBCTL_ENABLE_LANG) ? " lang" : "",
  1696. (usbctl & USBCTL_ENABLE_MFGR) ? " mfgr" : "",
  1697. (usbctl & USBCTL_ENABLE_PROD) ? " prod" : "",
  1698. (usbctl & USBCTL_ENABLE_SERIAL) ? " serial" : "",
  1699. (usbctl & USBCTL_ENABLE_DEFAULTS) ? " defaults" : "",
  1700. (usbctl & USBCTL_FLUSH_OTHER) ? " FLUSH" : "",
  1701. (usbctl & USBCTL_DISCONN_OTHER) ? " DIS" : "",
  1702. (usbctl & USBCTL_FLUSH_THIS) ? " FLUSH" : "",
  1703. (usbctl & USBCTL_DISCONN_THIS) ? " DIS" : "",
  1704. usbctl & ~USBCTL_WRITABLE_MASK
  1705. );
  1706. }
  1707. /*-------------------------------------------------------------------------*/
  1708. /*
  1709. * Status register
  1710. */
  1711. #define STATUS_PORT_A (1 << 15)
  1712. #define STATUS_CONN_OTHER (1 << 14)
  1713. #define STATUS_SUSPEND_OTHER (1 << 13)
  1714. #define STATUS_MAILBOX_OTHER (1 << 12)
  1715. #define STATUS_PACKETS_OTHER(n) (((n) >> 8) && 0x03)
  1716. #define STATUS_CONN_THIS (1 << 6)
  1717. #define STATUS_SUSPEND_THIS (1 << 5)
  1718. #define STATUS_MAILBOX_THIS (1 << 4)
  1719. #define STATUS_PACKETS_THIS(n) (((n) >> 0) && 0x03)
  1720. #define STATUS_UNSPEC_MASK 0x0c8c
  1721. #define STATUS_NOISE_MASK ((u16)~(0x0303|STATUS_UNSPEC_MASK))
  1722. static inline void nc_dump_status (struct usbnet *dev, u16 status)
  1723. {
  1724. if (!netif_msg_link (dev))
  1725. return;
  1726. devdbg (dev, "net1080 %s-%s status 0x%x:"
  1727. " this (%c) PKT=%d%s%s%s;"
  1728. " other PKT=%d%s%s%s; unspec 0x%x",
  1729. dev->udev->bus->bus_name, dev->udev->devpath,
  1730. status,
  1731. // XXX the packet counts don't seem right
  1732. // (1 at reset, not 0); maybe UNSPEC too
  1733. (status & STATUS_PORT_A) ? 'A' : 'B',
  1734. STATUS_PACKETS_THIS (status),
  1735. (status & STATUS_CONN_THIS) ? " CON" : "",
  1736. (status & STATUS_SUSPEND_THIS) ? " SUS" : "",
  1737. (status & STATUS_MAILBOX_THIS) ? " MBOX" : "",
  1738. STATUS_PACKETS_OTHER (status),
  1739. (status & STATUS_CONN_OTHER) ? " CON" : "",
  1740. (status & STATUS_SUSPEND_OTHER) ? " SUS" : "",
  1741. (status & STATUS_MAILBOX_OTHER) ? " MBOX" : "",
  1742. status & STATUS_UNSPEC_MASK
  1743. );
  1744. }
  1745. /*-------------------------------------------------------------------------*/
  1746. /*
  1747. * TTL register
  1748. */
  1749. #define TTL_THIS(ttl) (0x00ff & ttl)
  1750. #define TTL_OTHER(ttl) (0x00ff & (ttl >> 8))
  1751. #define MK_TTL(this,other) ((u16)(((other)<<8)|(0x00ff&(this))))
  1752. static inline void nc_dump_ttl (struct usbnet *dev, u16 ttl)
  1753. {
  1754. if (netif_msg_link (dev))
  1755. devdbg (dev, "net1080 %s-%s ttl 0x%x this = %d, other = %d",
  1756. dev->udev->bus->bus_name, dev->udev->devpath,
  1757. ttl, TTL_THIS (ttl), TTL_OTHER (ttl));
  1758. }
  1759. /*-------------------------------------------------------------------------*/
  1760. static int net1080_reset (struct usbnet *dev)
  1761. {
  1762. u16 usbctl, status, ttl;
  1763. u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL);
  1764. int retval;
  1765. if (!vp)
  1766. return -ENOMEM;
  1767. // nc_dump_registers (dev);
  1768. if ((retval = nc_register_read (dev, REG_STATUS, vp)) < 0) {
  1769. dbg ("can't read %s-%s status: %d",
  1770. dev->udev->bus->bus_name, dev->udev->devpath, retval);
  1771. goto done;
  1772. }
  1773. status = *vp;
  1774. nc_dump_status (dev, status);
  1775. if ((retval = nc_register_read (dev, REG_USBCTL, vp)) < 0) {
  1776. dbg ("can't read USBCTL, %d", retval);
  1777. goto done;
  1778. }
  1779. usbctl = *vp;
  1780. nc_dump_usbctl (dev, usbctl);
  1781. nc_register_write (dev, REG_USBCTL,
  1782. USBCTL_FLUSH_THIS | USBCTL_FLUSH_OTHER);
  1783. if ((retval = nc_register_read (dev, REG_TTL, vp)) < 0) {
  1784. dbg ("can't read TTL, %d", retval);
  1785. goto done;
  1786. }
  1787. ttl = *vp;
  1788. // nc_dump_ttl (dev, ttl);
  1789. nc_register_write (dev, REG_TTL,
  1790. MK_TTL (NC_READ_TTL_MS, TTL_OTHER (ttl)) );
  1791. dbg ("%s: assigned TTL, %d ms", dev->net->name, NC_READ_TTL_MS);
  1792. if (netif_msg_link (dev))
  1793. devinfo (dev, "port %c, peer %sconnected",
  1794. (status & STATUS_PORT_A) ? 'A' : 'B',
  1795. (status & STATUS_CONN_OTHER) ? "" : "dis"
  1796. );
  1797. retval = 0;
  1798. done:
  1799. kfree (vp);
  1800. return retval;
  1801. }
  1802. static int net1080_check_connect (struct usbnet *dev)
  1803. {
  1804. int retval;
  1805. u16 status;
  1806. u16 *vp = kmalloc (sizeof (u16), GFP_KERNEL);
  1807. if (!vp)
  1808. return -ENOMEM;
  1809. retval = nc_register_read (dev, REG_STATUS, vp);
  1810. status = *vp;
  1811. kfree (vp);
  1812. if (retval != 0) {
  1813. dbg ("%s net1080_check_conn read - %d", dev->net->name, retval);
  1814. return retval;
  1815. }
  1816. if ((status & STATUS_CONN_OTHER) != STATUS_CONN_OTHER)
  1817. return -ENOLINK;
  1818. return 0;
  1819. }
  1820. static void nc_flush_complete (struct urb *urb, struct pt_regs *regs)
  1821. {
  1822. kfree (urb->context);
  1823. usb_free_urb(urb);
  1824. }
  1825. static void nc_ensure_sync (struct usbnet *dev)
  1826. {
  1827. dev->frame_errors++;
  1828. if (dev->frame_errors > 5) {
  1829. struct urb *urb;
  1830. struct usb_ctrlrequest *req;
  1831. int status;
  1832. /* Send a flush */
  1833. urb = usb_alloc_urb (0, SLAB_ATOMIC);
  1834. if (!urb)
  1835. return;
  1836. req = kmalloc (sizeof *req, GFP_ATOMIC);
  1837. if (!req) {
  1838. usb_free_urb (urb);
  1839. return;
  1840. }
  1841. req->bRequestType = USB_DIR_OUT
  1842. | USB_TYPE_VENDOR
  1843. | USB_RECIP_DEVICE;
  1844. req->bRequest = REQUEST_REGISTER;
  1845. req->wValue = cpu_to_le16 (USBCTL_FLUSH_THIS
  1846. | USBCTL_FLUSH_OTHER);
  1847. req->wIndex = cpu_to_le16 (REG_USBCTL);
  1848. req->wLength = cpu_to_le16 (0);
  1849. /* queue an async control request, we don't need
  1850. * to do anything when it finishes except clean up.
  1851. */
  1852. usb_fill_control_urb (urb, dev->udev,
  1853. usb_sndctrlpipe (dev->udev, 0),
  1854. (unsigned char *) req,
  1855. NULL, 0,
  1856. nc_flush_complete, req);
  1857. status = usb_submit_urb (urb, GFP_ATOMIC);
  1858. if (status) {
  1859. kfree (req);
  1860. usb_free_urb (urb);
  1861. return;
  1862. }
  1863. if (netif_msg_rx_err (dev))
  1864. devdbg (dev, "flush net1080; too many framing errors");
  1865. dev->frame_errors = 0;
  1866. }
  1867. }
  1868. static int net1080_rx_fixup (struct usbnet *dev, struct sk_buff *skb)
  1869. {
  1870. struct nc_header *header;
  1871. struct net_device *net = dev->net;
  1872. struct nc_trailer *trailer;
  1873. u16 hdr_len, packet_len;
  1874. if (!(skb->len & 0x01)) {
  1875. dev->stats.rx_frame_errors++;
  1876. dbg ("rx framesize %d range %d..%d mtu %d", skb->len,
  1877. net->hard_header_len, dev->hard_mtu, net->mtu);
  1878. nc_ensure_sync (dev);
  1879. return 0;
  1880. }
  1881. header = (struct nc_header *) skb->data;
  1882. hdr_len = le16_to_cpup (&header->hdr_len);
  1883. packet_len = le16_to_cpup (&header->packet_len);
  1884. if (FRAMED_SIZE (packet_len) > NC_MAX_PACKET) {
  1885. dev->stats.rx_frame_errors++;
  1886. dbg ("packet too big, %d", packet_len);
  1887. nc_ensure_sync (dev);
  1888. return 0;
  1889. } else if (hdr_len < MIN_HEADER) {
  1890. dev->stats.rx_frame_errors++;
  1891. dbg ("header too short, %d", hdr_len);
  1892. nc_ensure_sync (dev);
  1893. return 0;
  1894. } else if (hdr_len > MIN_HEADER) {
  1895. // out of band data for us?
  1896. dbg ("header OOB, %d bytes", hdr_len - MIN_HEADER);
  1897. nc_ensure_sync (dev);
  1898. // switch (vendor/product ids) { ... }
  1899. }
  1900. skb_pull (skb, hdr_len);
  1901. trailer = (struct nc_trailer *)
  1902. (skb->data + skb->len - sizeof *trailer);
  1903. skb_trim (skb, skb->len - sizeof *trailer);
  1904. if ((packet_len & 0x01) == 0) {
  1905. if (skb->data [packet_len] != PAD_BYTE) {
  1906. dev->stats.rx_frame_errors++;
  1907. dbg ("bad pad");
  1908. return 0;
  1909. }
  1910. skb_trim (skb, skb->len - 1);
  1911. }
  1912. if (skb->len != packet_len) {
  1913. dev->stats.rx_frame_errors++;
  1914. dbg ("bad packet len %d (expected %d)",
  1915. skb->len, packet_len);
  1916. nc_ensure_sync (dev);
  1917. return 0;
  1918. }
  1919. if (header->packet_id != get_unaligned (&trailer->packet_id)) {
  1920. dev->stats.rx_fifo_errors++;
  1921. dbg ("(2+ dropped) rx packet_id mismatch 0x%x 0x%x",
  1922. le16_to_cpu (header->packet_id),
  1923. le16_to_cpu (trailer->packet_id));
  1924. return 0;
  1925. }
  1926. #if 0
  1927. devdbg (dev, "frame <rx h %d p %d id %d", header->hdr_len,
  1928. header->packet_len, header->packet_id);
  1929. #endif
  1930. dev->frame_errors = 0;
  1931. return 1;
  1932. }
  1933. static struct sk_buff *
  1934. net1080_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
  1935. {
  1936. int padlen;
  1937. struct sk_buff *skb2;
  1938. padlen = ((skb->len + sizeof (struct nc_header)
  1939. + sizeof (struct nc_trailer)) & 0x01) ? 0 : 1;
  1940. if (!skb_cloned (skb)) {
  1941. int headroom = skb_headroom (skb);
  1942. int tailroom = skb_tailroom (skb);
  1943. if ((padlen + sizeof (struct nc_trailer)) <= tailroom
  1944. && sizeof (struct nc_header) <= headroom)
  1945. /* There's enough head and tail room */
  1946. return skb;
  1947. if ((sizeof (struct nc_header) + padlen
  1948. + sizeof (struct nc_trailer)) <
  1949. (headroom + tailroom)) {
  1950. /* There's enough total room, so just readjust */
  1951. skb->data = memmove (skb->head
  1952. + sizeof (struct nc_header),
  1953. skb->data, skb->len);
  1954. skb->tail = skb->data + skb->len;
  1955. return skb;
  1956. }
  1957. }
  1958. /* Create a new skb to use with the correct size */
  1959. skb2 = skb_copy_expand (skb,
  1960. sizeof (struct nc_header),
  1961. sizeof (struct nc_trailer) + padlen,
  1962. flags);
  1963. dev_kfree_skb_any (skb);
  1964. return skb2;
  1965. }
  1966. static int net1080_bind (struct usbnet *dev, struct usb_interface *intf)
  1967. {
  1968. unsigned extra = sizeof (struct nc_header)
  1969. + 1
  1970. + sizeof (struct nc_trailer);
  1971. dev->net->hard_header_len += extra;
  1972. dev->hard_mtu = NC_MAX_PACKET;
  1973. return 0;
  1974. }
  1975. static const struct driver_info net1080_info = {
  1976. .description = "NetChip TurboCONNECT",
  1977. .flags = FLAG_FRAMING_NC,
  1978. .bind = net1080_bind,
  1979. .reset = net1080_reset,
  1980. .check_connect = net1080_check_connect,
  1981. .rx_fixup = net1080_rx_fixup,
  1982. .tx_fixup = net1080_tx_fixup,
  1983. };
  1984. #endif /* CONFIG_USB_NET1080 */
  1985. #ifdef CONFIG_USB_PL2301
  1986. #define HAVE_HARDWARE
  1987. /*-------------------------------------------------------------------------
  1988. *
  1989. * Prolific PL-2301/PL-2302 driver ... http://www.prolifictech.com
  1990. *
  1991. * The protocol and handshaking used here should be bug-compatible
  1992. * with the Linux 2.2 "plusb" driver, by Deti Fliegl.
  1993. *
  1994. *-------------------------------------------------------------------------*/
  1995. /*
  1996. * Bits 0-4 can be used for software handshaking; they're set from
  1997. * one end, cleared from the other, "read" with the interrupt byte.
  1998. */
  1999. #define PL_S_EN (1<<7) /* (feature only) suspend enable */
  2000. /* reserved bit -- rx ready (6) ? */
  2001. #define PL_TX_READY (1<<5) /* (interrupt only) transmit ready */
  2002. #define PL_RESET_OUT (1<<4) /* reset output pipe */
  2003. #define PL_RESET_IN (1<<3) /* reset input pipe */
  2004. #define PL_TX_C (1<<2) /* transmission complete */
  2005. #define PL_TX_REQ (1<<1) /* transmission received */
  2006. #define PL_PEER_E (1<<0) /* peer exists */
  2007. static inline int
  2008. pl_vendor_req (struct usbnet *dev, u8 req, u8 val, u8 index)
  2009. {
  2010. return usb_control_msg (dev->udev,
  2011. usb_rcvctrlpipe (dev->udev, 0),
  2012. req,
  2013. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  2014. val, index,
  2015. NULL, 0,
  2016. CONTROL_TIMEOUT_MS);
  2017. }
  2018. static inline int
  2019. pl_clear_QuickLink_features (struct usbnet *dev, int val)
  2020. {
  2021. return pl_vendor_req (dev, 1, (u8) val, 0);
  2022. }
  2023. static inline int
  2024. pl_set_QuickLink_features (struct usbnet *dev, int val)
  2025. {
  2026. return pl_vendor_req (dev, 3, (u8) val, 0);
  2027. }
  2028. /*-------------------------------------------------------------------------*/
  2029. static int pl_reset (struct usbnet *dev)
  2030. {
  2031. /* some units seem to need this reset, others reject it utterly.
  2032. * FIXME be more like "naplink" or windows drivers.
  2033. */
  2034. (void) pl_set_QuickLink_features (dev,
  2035. PL_S_EN|PL_RESET_OUT|PL_RESET_IN|PL_PEER_E);
  2036. return 0;
  2037. }
  2038. static const struct driver_info prolific_info = {
  2039. .description = "Prolific PL-2301/PL-2302",
  2040. .flags = FLAG_NO_SETINT,
  2041. /* some PL-2302 versions seem to fail usb_set_interface() */
  2042. .reset = pl_reset,
  2043. };
  2044. #endif /* CONFIG_USB_PL2301 */
  2045. #ifdef CONFIG_USB_ZAURUS
  2046. #define HAVE_HARDWARE
  2047. #include <linux/crc32.h>
  2048. /*-------------------------------------------------------------------------
  2049. *
  2050. * Zaurus is also a SA-1110 based PDA, but one using a different driver
  2051. * (and framing) for its USB slave/gadget controller than the case above.
  2052. *
  2053. * For the current version of that driver, the main way that framing is
  2054. * nonstandard (also from perspective of the CDC ethernet model!) is a
  2055. * crc32, added to help detect when some sa1100 usb-to-memory DMA errata
  2056. * haven't been fully worked around. Also, all Zaurii use the same
  2057. * default Ethernet address.
  2058. *
  2059. * PXA based models use the same framing, and also can't implement
  2060. * set_interface properly.
  2061. *
  2062. * All known Zaurii lie about their standards conformance. Most lie by
  2063. * saying they support CDC Ethernet. Some lie and say they support CDC
  2064. * MDLM (as if for access to cell phone modems). Someone, please beat
  2065. * on Sharp (and other such vendors) for a while with a cluestick.
  2066. *
  2067. *-------------------------------------------------------------------------*/
  2068. static struct sk_buff *
  2069. zaurus_tx_fixup (struct usbnet *dev, struct sk_buff *skb, unsigned flags)
  2070. {
  2071. int padlen;
  2072. struct sk_buff *skb2;
  2073. padlen = 2;
  2074. if (!skb_cloned (skb)) {
  2075. int tailroom = skb_tailroom (skb);
  2076. if ((padlen + 4) <= tailroom)
  2077. goto done;
  2078. }
  2079. skb2 = skb_copy_expand (skb, 0, 4 + padlen, flags);
  2080. dev_kfree_skb_any (skb);
  2081. skb = skb2;
  2082. if (skb) {
  2083. u32 fcs;
  2084. done:
  2085. fcs = crc32_le (~0, skb->data, skb->len);
  2086. fcs = ~fcs;
  2087. *skb_put (skb, 1) = fcs & 0xff;
  2088. *skb_put (skb, 1) = (fcs>> 8) & 0xff;
  2089. *skb_put (skb, 1) = (fcs>>16) & 0xff;
  2090. *skb_put (skb, 1) = (fcs>>24) & 0xff;
  2091. }
  2092. return skb;
  2093. }
  2094. static int zaurus_bind (struct usbnet *dev, struct usb_interface *intf)
  2095. {
  2096. /* Belcarra's funky framing has other options; mostly
  2097. * TRAILERS (!) with 4 bytes CRC, and maybe 2 pad bytes.
  2098. */
  2099. dev->net->hard_header_len += 6;
  2100. return generic_cdc_bind(dev, intf);
  2101. }
  2102. static const struct driver_info zaurus_sl5x00_info = {
  2103. .description = "Sharp Zaurus SL-5x00",
  2104. .flags = FLAG_FRAMING_Z,
  2105. .check_connect = always_connected,
  2106. .bind = zaurus_bind,
  2107. .unbind = cdc_unbind,
  2108. .tx_fixup = zaurus_tx_fixup,
  2109. };
  2110. #define ZAURUS_STRONGARM_INFO ((unsigned long)&zaurus_sl5x00_info)
  2111. static const struct driver_info zaurus_pxa_info = {
  2112. .description = "Sharp Zaurus, PXA-2xx based",
  2113. .flags = FLAG_FRAMING_Z,
  2114. .check_connect = always_connected,
  2115. .bind = zaurus_bind,
  2116. .unbind = cdc_unbind,
  2117. .tx_fixup = zaurus_tx_fixup,
  2118. };
  2119. #define ZAURUS_PXA_INFO ((unsigned long)&zaurus_pxa_info)
  2120. static const struct driver_info olympus_mxl_info = {
  2121. .description = "Olympus R1000",
  2122. .flags = FLAG_FRAMING_Z,
  2123. .check_connect = always_connected,
  2124. .bind = zaurus_bind,
  2125. .unbind = cdc_unbind,
  2126. .tx_fixup = zaurus_tx_fixup,
  2127. };
  2128. #define OLYMPUS_MXL_INFO ((unsigned long)&olympus_mxl_info)
  2129. /* Some more recent products using Lineo/Belcarra code will wrongly claim
  2130. * CDC MDLM conformance. They aren't conformant: data endpoints live
  2131. * in the control interface, there's no data interface, and it's not used
  2132. * to talk to a cell phone radio. But at least we can detect these two
  2133. * pseudo-classes, rather than growing this product list with entries for
  2134. * each new nonconformant product (sigh).
  2135. */
  2136. static const u8 safe_guid[16] = {
  2137. 0x5d, 0x34, 0xcf, 0x66, 0x11, 0x18, 0x11, 0xd6,
  2138. 0xa2, 0x1a, 0x00, 0x01, 0x02, 0xca, 0x9a, 0x7f,
  2139. };
  2140. static const u8 blan_guid[16] = {
  2141. 0x74, 0xf0, 0x3d, 0xbd, 0x1e, 0xc1, 0x44, 0x70,
  2142. 0xa3, 0x67, 0x71, 0x34, 0xc9, 0xf5, 0x54, 0x37,
  2143. };
  2144. static int blan_mdlm_bind (struct usbnet *dev, struct usb_interface *intf)
  2145. {
  2146. u8 *buf = intf->cur_altsetting->extra;
  2147. int len = intf->cur_altsetting->extralen;
  2148. struct usb_cdc_mdlm_desc *desc = NULL;
  2149. struct usb_cdc_mdlm_detail_desc *detail = NULL;
  2150. while (len > 3) {
  2151. if (buf [1] != USB_DT_CS_INTERFACE)
  2152. goto next_desc;
  2153. /* use bDescriptorSubType, and just verify that we get a
  2154. * "BLAN" (or "SAFE") descriptor.
  2155. */
  2156. switch (buf [2]) {
  2157. case USB_CDC_MDLM_TYPE:
  2158. if (desc) {
  2159. dev_dbg (&intf->dev, "extra MDLM\n");
  2160. goto bad_desc;
  2161. }
  2162. desc = (void *) buf;
  2163. if (desc->bLength != sizeof *desc) {
  2164. dev_dbg (&intf->dev, "MDLM len %u\n",
  2165. desc->bLength);
  2166. goto bad_desc;
  2167. }
  2168. /* expect bcdVersion 1.0, ignore */
  2169. if (memcmp(&desc->bGUID, blan_guid, 16)
  2170. && memcmp(&desc->bGUID, safe_guid, 16) ) {
  2171. /* hey, this one might _really_ be MDLM! */
  2172. dev_dbg (&intf->dev, "MDLM guid\n");
  2173. goto bad_desc;
  2174. }
  2175. break;
  2176. case USB_CDC_MDLM_DETAIL_TYPE:
  2177. if (detail) {
  2178. dev_dbg (&intf->dev, "extra MDLM detail\n");
  2179. goto bad_desc;
  2180. }
  2181. detail = (void *) buf;
  2182. switch (detail->bGuidDescriptorType) {
  2183. case 0: /* "SAFE" */
  2184. if (detail->bLength != (sizeof *detail + 2))
  2185. goto bad_detail;
  2186. break;
  2187. case 1: /* "BLAN" */
  2188. if (detail->bLength != (sizeof *detail + 3))
  2189. goto bad_detail;
  2190. break;
  2191. default:
  2192. goto bad_detail;
  2193. }
  2194. /* assuming we either noticed BLAN already, or will
  2195. * find it soon, there are some data bytes here:
  2196. * - bmNetworkCapabilities (unused)
  2197. * - bmDataCapabilities (bits, see below)
  2198. * - bPad (ignored, for PADAFTER -- BLAN-only)
  2199. * bits are:
  2200. * - 0x01 -- Zaurus framing (add CRC)
  2201. * - 0x02 -- PADBEFORE (CRC includes some padding)
  2202. * - 0x04 -- PADAFTER (some padding after CRC)
  2203. * - 0x08 -- "fermat" packet mangling (for hw bugs)
  2204. * the PADBEFORE appears not to matter; we interop
  2205. * with devices that use it and those that don't.
  2206. */
  2207. if ((detail->bDetailData[1] & ~02) != 0x01) {
  2208. /* bmDataCapabilites == 0 would be fine too,
  2209. * but framing is minidriver-coupled for now.
  2210. */
  2211. bad_detail:
  2212. dev_dbg (&intf->dev,
  2213. "bad MDLM detail, %d %d %d\n",
  2214. detail->bLength,
  2215. detail->bDetailData[0],
  2216. detail->bDetailData[2]);
  2217. goto bad_desc;
  2218. }
  2219. break;
  2220. }
  2221. next_desc:
  2222. len -= buf [0]; /* bLength */
  2223. buf += buf [0];
  2224. }
  2225. if (!desc || !detail) {
  2226. dev_dbg (&intf->dev, "missing cdc mdlm %s%sdescriptor\n",
  2227. desc ? "" : "func ",
  2228. detail ? "" : "detail ");
  2229. goto bad_desc;
  2230. }
  2231. /* There's probably a CDC Ethernet descriptor there, but we can't
  2232. * rely on the Ethernet address it provides since not all vendors
  2233. * bother to make it unique. Likewise there's no point in tracking
  2234. * of the CDC event notifications.
  2235. */
  2236. return get_endpoints (dev, intf);
  2237. bad_desc:
  2238. dev_info (&dev->udev->dev, "unsupported MDLM descriptors\n");
  2239. return -ENODEV;
  2240. }
  2241. static const struct driver_info bogus_mdlm_info = {
  2242. .description = "pseudo-MDLM (BLAN) device",
  2243. .flags = FLAG_FRAMING_Z,
  2244. .check_connect = always_connected,
  2245. .tx_fixup = zaurus_tx_fixup,
  2246. .bind = blan_mdlm_bind,
  2247. };
  2248. #else
  2249. /* blacklist all those devices */
  2250. #define ZAURUS_STRONGARM_INFO 0
  2251. #define ZAURUS_PXA_INFO 0
  2252. #define OLYMPUS_MXL_INFO 0
  2253. #endif
  2254. /*-------------------------------------------------------------------------
  2255. *
  2256. * Network Device Driver (peer link to "Host Device", from USB host)
  2257. *
  2258. *-------------------------------------------------------------------------*/
  2259. static int usbnet_change_mtu (struct net_device *net, int new_mtu)
  2260. {
  2261. struct usbnet *dev = netdev_priv(net);
  2262. int ll_mtu = new_mtu + net->hard_header_len;
  2263. if (new_mtu <= 0 || ll_mtu > dev->hard_mtu)
  2264. return -EINVAL;
  2265. // no second zero-length packet read wanted after mtu-sized packets
  2266. if ((ll_mtu % dev->maxpacket) == 0)
  2267. return -EDOM;
  2268. net->mtu = new_mtu;
  2269. return 0;
  2270. }
  2271. /*-------------------------------------------------------------------------*/
  2272. static struct net_device_stats *usbnet_get_stats (struct net_device *net)
  2273. {
  2274. struct usbnet *dev = netdev_priv(net);
  2275. return &dev->stats;
  2276. }
  2277. /*-------------------------------------------------------------------------*/
  2278. /* some LK 2.4 HCDs oopsed if we freed or resubmitted urbs from
  2279. * completion callbacks. 2.5 should have fixed those bugs...
  2280. */
  2281. static void defer_bh(struct usbnet *dev, struct sk_buff *skb, struct sk_buff_head *list)
  2282. {
  2283. unsigned long flags;
  2284. spin_lock_irqsave(&list->lock, flags);
  2285. __skb_unlink(skb, list);
  2286. spin_unlock(&list->lock);
  2287. spin_lock(&dev->done.lock);
  2288. __skb_queue_tail(&dev->done, skb);
  2289. if (dev->done.qlen == 1)
  2290. tasklet_schedule(&dev->bh);
  2291. spin_unlock_irqrestore(&dev->done.lock, flags);
  2292. }
  2293. /* some work can't be done in tasklets, so we use keventd
  2294. *
  2295. * NOTE: annoying asymmetry: if it's active, schedule_work() fails,
  2296. * but tasklet_schedule() doesn't. hope the failure is rare.
  2297. */
  2298. static void defer_kevent (struct usbnet *dev, int work)
  2299. {
  2300. set_bit (work, &dev->flags);
  2301. if (!schedule_work (&dev->kevent))
  2302. deverr (dev, "kevent %d may have been dropped", work);
  2303. else
  2304. devdbg (dev, "kevent %d scheduled", work);
  2305. }
  2306. /*-------------------------------------------------------------------------*/
  2307. static void rx_complete (struct urb *urb, struct pt_regs *regs);
  2308. static void rx_submit (struct usbnet *dev, struct urb *urb, unsigned flags)
  2309. {
  2310. struct sk_buff *skb;
  2311. struct skb_data *entry;
  2312. int retval = 0;
  2313. unsigned long lockflags;
  2314. size_t size;
  2315. size = max(dev->net->hard_header_len + dev->net->mtu,
  2316. (unsigned)ETH_FRAME_LEN);
  2317. if ((skb = alloc_skb (size + NET_IP_ALIGN, flags)) == NULL) {
  2318. if (netif_msg_rx_err (dev))
  2319. devdbg (dev, "no rx skb");
  2320. defer_kevent (dev, EVENT_RX_MEMORY);
  2321. usb_free_urb (urb);
  2322. return;
  2323. }
  2324. skb_reserve (skb, NET_IP_ALIGN);
  2325. entry = (struct skb_data *) skb->cb;
  2326. entry->urb = urb;
  2327. entry->dev = dev;
  2328. entry->state = rx_start;
  2329. entry->length = 0;
  2330. usb_fill_bulk_urb (urb, dev->udev, dev->in,
  2331. skb->data, size, rx_complete, skb);
  2332. spin_lock_irqsave (&dev->rxq.lock, lockflags);
  2333. if (netif_running (dev->net)
  2334. && netif_device_present (dev->net)
  2335. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  2336. switch (retval = usb_submit_urb (urb, GFP_ATOMIC)){
  2337. case -EPIPE:
  2338. defer_kevent (dev, EVENT_RX_HALT);
  2339. break;
  2340. case -ENOMEM:
  2341. defer_kevent (dev, EVENT_RX_MEMORY);
  2342. break;
  2343. case -ENODEV:
  2344. if (netif_msg_ifdown (dev))
  2345. devdbg (dev, "device gone");
  2346. netif_device_detach (dev->net);
  2347. break;
  2348. default:
  2349. if (netif_msg_rx_err (dev))
  2350. devdbg (dev, "rx submit, %d", retval);
  2351. tasklet_schedule (&dev->bh);
  2352. break;
  2353. case 0:
  2354. __skb_queue_tail (&dev->rxq, skb);
  2355. }
  2356. } else {
  2357. if (netif_msg_ifdown (dev))
  2358. devdbg (dev, "rx: stopped");
  2359. retval = -ENOLINK;
  2360. }
  2361. spin_unlock_irqrestore (&dev->rxq.lock, lockflags);
  2362. if (retval) {
  2363. dev_kfree_skb_any (skb);
  2364. usb_free_urb (urb);
  2365. }
  2366. }
  2367. /*-------------------------------------------------------------------------*/
  2368. static inline void rx_process (struct usbnet *dev, struct sk_buff *skb)
  2369. {
  2370. if (dev->driver_info->rx_fixup
  2371. && !dev->driver_info->rx_fixup (dev, skb))
  2372. goto error;
  2373. // else network stack removes extra byte if we forced a short packet
  2374. if (skb->len)
  2375. skb_return (dev, skb);
  2376. else {
  2377. if (netif_msg_rx_err (dev))
  2378. devdbg (dev, "drop");
  2379. error:
  2380. dev->stats.rx_errors++;
  2381. skb_queue_tail (&dev->done, skb);
  2382. }
  2383. }
  2384. /*-------------------------------------------------------------------------*/
  2385. static void rx_complete (struct urb *urb, struct pt_regs *regs)
  2386. {
  2387. struct sk_buff *skb = (struct sk_buff *) urb->context;
  2388. struct skb_data *entry = (struct skb_data *) skb->cb;
  2389. struct usbnet *dev = entry->dev;
  2390. int urb_status = urb->status;
  2391. skb_put (skb, urb->actual_length);
  2392. entry->state = rx_done;
  2393. entry->urb = NULL;
  2394. switch (urb_status) {
  2395. // success
  2396. case 0:
  2397. if (skb->len < dev->net->hard_header_len) {
  2398. entry->state = rx_cleanup;
  2399. dev->stats.rx_errors++;
  2400. dev->stats.rx_length_errors++;
  2401. if (netif_msg_rx_err (dev))
  2402. devdbg (dev, "rx length %d", skb->len);
  2403. }
  2404. break;
  2405. // stalls need manual reset. this is rare ... except that
  2406. // when going through USB 2.0 TTs, unplug appears this way.
  2407. // we avoid the highspeed version of the ETIMEOUT/EILSEQ
  2408. // storm, recovering as needed.
  2409. case -EPIPE:
  2410. dev->stats.rx_errors++;
  2411. defer_kevent (dev, EVENT_RX_HALT);
  2412. // FALLTHROUGH
  2413. // software-driven interface shutdown
  2414. case -ECONNRESET: // async unlink
  2415. case -ESHUTDOWN: // hardware gone
  2416. if (netif_msg_ifdown (dev))
  2417. devdbg (dev, "rx shutdown, code %d", urb_status);
  2418. goto block;
  2419. // we get controller i/o faults during khubd disconnect() delays.
  2420. // throttle down resubmits, to avoid log floods; just temporarily,
  2421. // so we still recover when the fault isn't a khubd delay.
  2422. case -EPROTO: // ehci
  2423. case -ETIMEDOUT: // ohci
  2424. case -EILSEQ: // uhci
  2425. dev->stats.rx_errors++;
  2426. if (!timer_pending (&dev->delay)) {
  2427. mod_timer (&dev->delay, jiffies + THROTTLE_JIFFIES);
  2428. if (netif_msg_link (dev))
  2429. devdbg (dev, "rx throttle %d", urb_status);
  2430. }
  2431. block:
  2432. entry->state = rx_cleanup;
  2433. entry->urb = urb;
  2434. urb = NULL;
  2435. break;
  2436. // data overrun ... flush fifo?
  2437. case -EOVERFLOW:
  2438. dev->stats.rx_over_errors++;
  2439. // FALLTHROUGH
  2440. default:
  2441. entry->state = rx_cleanup;
  2442. dev->stats.rx_errors++;
  2443. if (netif_msg_rx_err (dev))
  2444. devdbg (dev, "rx status %d", urb_status);
  2445. break;
  2446. }
  2447. defer_bh(dev, skb, &dev->rxq);
  2448. if (urb) {
  2449. if (netif_running (dev->net)
  2450. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  2451. rx_submit (dev, urb, GFP_ATOMIC);
  2452. return;
  2453. }
  2454. usb_free_urb (urb);
  2455. }
  2456. if (netif_msg_rx_err (dev))
  2457. devdbg (dev, "no read resubmitted");
  2458. }
  2459. static void intr_complete (struct urb *urb, struct pt_regs *regs)
  2460. {
  2461. struct usbnet *dev = urb->context;
  2462. int status = urb->status;
  2463. switch (status) {
  2464. /* success */
  2465. case 0:
  2466. dev->driver_info->status(dev, urb);
  2467. break;
  2468. /* software-driven interface shutdown */
  2469. case -ENOENT: // urb killed
  2470. case -ESHUTDOWN: // hardware gone
  2471. if (netif_msg_ifdown (dev))
  2472. devdbg (dev, "intr shutdown, code %d", status);
  2473. return;
  2474. /* NOTE: not throttling like RX/TX, since this endpoint
  2475. * already polls infrequently
  2476. */
  2477. default:
  2478. devdbg (dev, "intr status %d", status);
  2479. break;
  2480. }
  2481. if (!netif_running (dev->net))
  2482. return;
  2483. memset(urb->transfer_buffer, 0, urb->transfer_buffer_length);
  2484. status = usb_submit_urb (urb, GFP_ATOMIC);
  2485. if (status != 0 && netif_msg_timer (dev))
  2486. deverr(dev, "intr resubmit --> %d", status);
  2487. }
  2488. /*-------------------------------------------------------------------------*/
  2489. // unlink pending rx/tx; completion handlers do all other cleanup
  2490. static int unlink_urbs (struct usbnet *dev, struct sk_buff_head *q)
  2491. {
  2492. unsigned long flags;
  2493. struct sk_buff *skb, *skbnext;
  2494. int count = 0;
  2495. spin_lock_irqsave (&q->lock, flags);
  2496. for (skb = q->next; skb != (struct sk_buff *) q; skb = skbnext) {
  2497. struct skb_data *entry;
  2498. struct urb *urb;
  2499. int retval;
  2500. entry = (struct skb_data *) skb->cb;
  2501. urb = entry->urb;
  2502. skbnext = skb->next;
  2503. // during some PM-driven resume scenarios,
  2504. // these (async) unlinks complete immediately
  2505. retval = usb_unlink_urb (urb);
  2506. if (retval != -EINPROGRESS && retval != 0)
  2507. devdbg (dev, "unlink urb err, %d", retval);
  2508. else
  2509. count++;
  2510. }
  2511. spin_unlock_irqrestore (&q->lock, flags);
  2512. return count;
  2513. }
  2514. /*-------------------------------------------------------------------------*/
  2515. // precondition: never called in_interrupt
  2516. static int usbnet_stop (struct net_device *net)
  2517. {
  2518. struct usbnet *dev = netdev_priv(net);
  2519. int temp;
  2520. DECLARE_WAIT_QUEUE_HEAD (unlink_wakeup);
  2521. DECLARE_WAITQUEUE (wait, current);
  2522. netif_stop_queue (net);
  2523. if (netif_msg_ifdown (dev))
  2524. devinfo (dev, "stop stats: rx/tx %ld/%ld, errs %ld/%ld",
  2525. dev->stats.rx_packets, dev->stats.tx_packets,
  2526. dev->stats.rx_errors, dev->stats.tx_errors
  2527. );
  2528. // ensure there are no more active urbs
  2529. add_wait_queue (&unlink_wakeup, &wait);
  2530. dev->wait = &unlink_wakeup;
  2531. temp = unlink_urbs (dev, &dev->txq) + unlink_urbs (dev, &dev->rxq);
  2532. // maybe wait for deletions to finish.
  2533. while (!skb_queue_empty(&dev->rxq) &&
  2534. !skb_queue_empty(&dev->txq) &&
  2535. !skb_queue_empty(&dev->done)) {
  2536. msleep(UNLINK_TIMEOUT_MS);
  2537. if (netif_msg_ifdown (dev))
  2538. devdbg (dev, "waited for %d urb completions", temp);
  2539. }
  2540. dev->wait = NULL;
  2541. remove_wait_queue (&unlink_wakeup, &wait);
  2542. usb_kill_urb(dev->interrupt);
  2543. /* deferred work (task, timer, softirq) must also stop.
  2544. * can't flush_scheduled_work() until we drop rtnl (later),
  2545. * else workers could deadlock; so make workers a NOP.
  2546. */
  2547. dev->flags = 0;
  2548. del_timer_sync (&dev->delay);
  2549. tasklet_kill (&dev->bh);
  2550. return 0;
  2551. }
  2552. /*-------------------------------------------------------------------------*/
  2553. // posts reads, and enables write queuing
  2554. // precondition: never called in_interrupt
  2555. static int usbnet_open (struct net_device *net)
  2556. {
  2557. struct usbnet *dev = netdev_priv(net);
  2558. int retval = 0;
  2559. struct driver_info *info = dev->driver_info;
  2560. // put into "known safe" state
  2561. if (info->reset && (retval = info->reset (dev)) < 0) {
  2562. if (netif_msg_ifup (dev))
  2563. devinfo (dev,
  2564. "open reset fail (%d) usbnet usb-%s-%s, %s",
  2565. retval,
  2566. dev->udev->bus->bus_name, dev->udev->devpath,
  2567. info->description);
  2568. goto done;
  2569. }
  2570. // insist peer be connected
  2571. if (info->check_connect && (retval = info->check_connect (dev)) < 0) {
  2572. if (netif_msg_ifup (dev))
  2573. devdbg (dev, "can't open; %d", retval);
  2574. goto done;
  2575. }
  2576. /* start any status interrupt transfer */
  2577. if (dev->interrupt) {
  2578. retval = usb_submit_urb (dev->interrupt, GFP_KERNEL);
  2579. if (retval < 0) {
  2580. if (netif_msg_ifup (dev))
  2581. deverr (dev, "intr submit %d", retval);
  2582. goto done;
  2583. }
  2584. }
  2585. netif_start_queue (net);
  2586. if (netif_msg_ifup (dev)) {
  2587. char *framing;
  2588. if (dev->driver_info->flags & FLAG_FRAMING_NC)
  2589. framing = "NetChip";
  2590. else if (dev->driver_info->flags & FLAG_FRAMING_GL)
  2591. framing = "GeneSys";
  2592. else if (dev->driver_info->flags & FLAG_FRAMING_Z)
  2593. framing = "Zaurus";
  2594. else if (dev->driver_info->flags & FLAG_FRAMING_RN)
  2595. framing = "RNDIS";
  2596. else if (dev->driver_info->flags & FLAG_FRAMING_AX)
  2597. framing = "ASIX";
  2598. else
  2599. framing = "simple";
  2600. devinfo (dev, "open: enable queueing "
  2601. "(rx %d, tx %d) mtu %d %s framing",
  2602. RX_QLEN (dev), TX_QLEN (dev), dev->net->mtu,
  2603. framing);
  2604. }
  2605. // delay posting reads until we're fully open
  2606. tasklet_schedule (&dev->bh);
  2607. done:
  2608. return retval;
  2609. }
  2610. /*-------------------------------------------------------------------------*/
  2611. static void usbnet_get_drvinfo (struct net_device *net, struct ethtool_drvinfo *info)
  2612. {
  2613. struct usbnet *dev = netdev_priv(net);
  2614. strncpy (info->driver, driver_name, sizeof info->driver);
  2615. strncpy (info->version, DRIVER_VERSION, sizeof info->version);
  2616. strncpy (info->fw_version, dev->driver_info->description,
  2617. sizeof info->fw_version);
  2618. usb_make_path (dev->udev, info->bus_info, sizeof info->bus_info);
  2619. }
  2620. static u32 usbnet_get_link (struct net_device *net)
  2621. {
  2622. struct usbnet *dev = netdev_priv(net);
  2623. /* If a check_connect is defined, return its result */
  2624. if (dev->driver_info->check_connect)
  2625. return dev->driver_info->check_connect (dev) == 0;
  2626. /* Otherwise, say we're up (to avoid breaking scripts) */
  2627. return 1;
  2628. }
  2629. static u32 usbnet_get_msglevel (struct net_device *net)
  2630. {
  2631. struct usbnet *dev = netdev_priv(net);
  2632. return dev->msg_enable;
  2633. }
  2634. static void usbnet_set_msglevel (struct net_device *net, u32 level)
  2635. {
  2636. struct usbnet *dev = netdev_priv(net);
  2637. dev->msg_enable = level;
  2638. }
  2639. static int usbnet_ioctl (struct net_device *net, struct ifreq *rq, int cmd)
  2640. {
  2641. #ifdef NEED_MII
  2642. {
  2643. struct usbnet *dev = netdev_priv(net);
  2644. if (dev->mii.mdio_read != NULL && dev->mii.mdio_write != NULL)
  2645. return generic_mii_ioctl(&dev->mii, if_mii(rq), cmd, NULL);
  2646. }
  2647. #endif
  2648. return -EOPNOTSUPP;
  2649. }
  2650. /*-------------------------------------------------------------------------*/
  2651. /* work that cannot be done in interrupt context uses keventd.
  2652. *
  2653. * NOTE: with 2.5 we could do more of this using completion callbacks,
  2654. * especially now that control transfers can be queued.
  2655. */
  2656. static void
  2657. kevent (void *data)
  2658. {
  2659. struct usbnet *dev = data;
  2660. int status;
  2661. /* usb_clear_halt() needs a thread context */
  2662. if (test_bit (EVENT_TX_HALT, &dev->flags)) {
  2663. unlink_urbs (dev, &dev->txq);
  2664. status = usb_clear_halt (dev->udev, dev->out);
  2665. if (status < 0
  2666. && status != -EPIPE
  2667. && status != -ESHUTDOWN) {
  2668. if (netif_msg_tx_err (dev))
  2669. deverr (dev, "can't clear tx halt, status %d",
  2670. status);
  2671. } else {
  2672. clear_bit (EVENT_TX_HALT, &dev->flags);
  2673. if (status != -ESHUTDOWN)
  2674. netif_wake_queue (dev->net);
  2675. }
  2676. }
  2677. if (test_bit (EVENT_RX_HALT, &dev->flags)) {
  2678. unlink_urbs (dev, &dev->rxq);
  2679. status = usb_clear_halt (dev->udev, dev->in);
  2680. if (status < 0
  2681. && status != -EPIPE
  2682. && status != -ESHUTDOWN) {
  2683. if (netif_msg_rx_err (dev))
  2684. deverr (dev, "can't clear rx halt, status %d",
  2685. status);
  2686. } else {
  2687. clear_bit (EVENT_RX_HALT, &dev->flags);
  2688. tasklet_schedule (&dev->bh);
  2689. }
  2690. }
  2691. /* tasklet could resubmit itself forever if memory is tight */
  2692. if (test_bit (EVENT_RX_MEMORY, &dev->flags)) {
  2693. struct urb *urb = NULL;
  2694. if (netif_running (dev->net))
  2695. urb = usb_alloc_urb (0, GFP_KERNEL);
  2696. else
  2697. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  2698. if (urb != NULL) {
  2699. clear_bit (EVENT_RX_MEMORY, &dev->flags);
  2700. rx_submit (dev, urb, GFP_KERNEL);
  2701. tasklet_schedule (&dev->bh);
  2702. }
  2703. }
  2704. if (test_bit (EVENT_LINK_RESET, &dev->flags)) {
  2705. struct driver_info *info = dev->driver_info;
  2706. int retval = 0;
  2707. clear_bit (EVENT_LINK_RESET, &dev->flags);
  2708. if(info->link_reset && (retval = info->link_reset(dev)) < 0) {
  2709. devinfo(dev, "link reset failed (%d) usbnet usb-%s-%s, %s",
  2710. retval,
  2711. dev->udev->bus->bus_name, dev->udev->devpath,
  2712. info->description);
  2713. }
  2714. }
  2715. if (dev->flags)
  2716. devdbg (dev, "kevent done, flags = 0x%lx",
  2717. dev->flags);
  2718. }
  2719. /*-------------------------------------------------------------------------*/
  2720. static void tx_complete (struct urb *urb, struct pt_regs *regs)
  2721. {
  2722. struct sk_buff *skb = (struct sk_buff *) urb->context;
  2723. struct skb_data *entry = (struct skb_data *) skb->cb;
  2724. struct usbnet *dev = entry->dev;
  2725. if (urb->status == 0) {
  2726. dev->stats.tx_packets++;
  2727. dev->stats.tx_bytes += entry->length;
  2728. } else {
  2729. dev->stats.tx_errors++;
  2730. switch (urb->status) {
  2731. case -EPIPE:
  2732. defer_kevent (dev, EVENT_TX_HALT);
  2733. break;
  2734. /* software-driven interface shutdown */
  2735. case -ECONNRESET: // async unlink
  2736. case -ESHUTDOWN: // hardware gone
  2737. break;
  2738. // like rx, tx gets controller i/o faults during khubd delays
  2739. // and so it uses the same throttling mechanism.
  2740. case -EPROTO: // ehci
  2741. case -ETIMEDOUT: // ohci
  2742. case -EILSEQ: // uhci
  2743. if (!timer_pending (&dev->delay)) {
  2744. mod_timer (&dev->delay,
  2745. jiffies + THROTTLE_JIFFIES);
  2746. if (netif_msg_link (dev))
  2747. devdbg (dev, "tx throttle %d",
  2748. urb->status);
  2749. }
  2750. netif_stop_queue (dev->net);
  2751. break;
  2752. default:
  2753. if (netif_msg_tx_err (dev))
  2754. devdbg (dev, "tx err %d", entry->urb->status);
  2755. break;
  2756. }
  2757. }
  2758. urb->dev = NULL;
  2759. entry->state = tx_done;
  2760. defer_bh(dev, skb, &dev->txq);
  2761. }
  2762. /*-------------------------------------------------------------------------*/
  2763. static void usbnet_tx_timeout (struct net_device *net)
  2764. {
  2765. struct usbnet *dev = netdev_priv(net);
  2766. unlink_urbs (dev, &dev->txq);
  2767. tasklet_schedule (&dev->bh);
  2768. // FIXME: device recovery -- reset?
  2769. }
  2770. /*-------------------------------------------------------------------------*/
  2771. static int usbnet_start_xmit (struct sk_buff *skb, struct net_device *net)
  2772. {
  2773. struct usbnet *dev = netdev_priv(net);
  2774. int length;
  2775. int retval = NET_XMIT_SUCCESS;
  2776. struct urb *urb = NULL;
  2777. struct skb_data *entry;
  2778. struct driver_info *info = dev->driver_info;
  2779. unsigned long flags;
  2780. #ifdef CONFIG_USB_NET1080
  2781. struct nc_header *header = NULL;
  2782. struct nc_trailer *trailer = NULL;
  2783. #endif /* CONFIG_USB_NET1080 */
  2784. // some devices want funky USB-level framing, for
  2785. // win32 driver (usually) and/or hardware quirks
  2786. if (info->tx_fixup) {
  2787. skb = info->tx_fixup (dev, skb, GFP_ATOMIC);
  2788. if (!skb) {
  2789. if (netif_msg_tx_err (dev))
  2790. devdbg (dev, "can't tx_fixup skb");
  2791. goto drop;
  2792. }
  2793. }
  2794. length = skb->len;
  2795. if (!(urb = usb_alloc_urb (0, GFP_ATOMIC))) {
  2796. if (netif_msg_tx_err (dev))
  2797. devdbg (dev, "no urb");
  2798. goto drop;
  2799. }
  2800. entry = (struct skb_data *) skb->cb;
  2801. entry->urb = urb;
  2802. entry->dev = dev;
  2803. entry->state = tx_start;
  2804. entry->length = length;
  2805. // FIXME: reorganize a bit, so that fixup() fills out NetChip
  2806. // framing too. (Packet ID update needs the spinlock...)
  2807. // [ BETTER: we already own net->xmit_lock, that's enough ]
  2808. #ifdef CONFIG_USB_NET1080
  2809. if (info->flags & FLAG_FRAMING_NC) {
  2810. header = (struct nc_header *) skb_push (skb, sizeof *header);
  2811. header->hdr_len = cpu_to_le16 (sizeof (*header));
  2812. header->packet_len = cpu_to_le16 (length);
  2813. if (!((skb->len + sizeof *trailer) & 0x01))
  2814. *skb_put (skb, 1) = PAD_BYTE;
  2815. trailer = (struct nc_trailer *) skb_put (skb, sizeof *trailer);
  2816. }
  2817. #endif /* CONFIG_USB_NET1080 */
  2818. usb_fill_bulk_urb (urb, dev->udev, dev->out,
  2819. skb->data, skb->len, tx_complete, skb);
  2820. /* don't assume the hardware handles USB_ZERO_PACKET
  2821. * NOTE: strictly conforming cdc-ether devices should expect
  2822. * the ZLP here, but ignore the one-byte packet.
  2823. *
  2824. * FIXME zero that byte, if it doesn't require a new skb.
  2825. */
  2826. if ((length % dev->maxpacket) == 0)
  2827. urb->transfer_buffer_length++;
  2828. spin_lock_irqsave (&dev->txq.lock, flags);
  2829. #ifdef CONFIG_USB_NET1080
  2830. if (info->flags & FLAG_FRAMING_NC) {
  2831. header->packet_id = cpu_to_le16 ((u16)dev->xid++);
  2832. put_unaligned (header->packet_id, &trailer->packet_id);
  2833. #if 0
  2834. devdbg (dev, "frame >tx h %d p %d id %d",
  2835. header->hdr_len, header->packet_len,
  2836. header->packet_id);
  2837. #endif
  2838. }
  2839. #endif /* CONFIG_USB_NET1080 */
  2840. switch ((retval = usb_submit_urb (urb, GFP_ATOMIC))) {
  2841. case -EPIPE:
  2842. netif_stop_queue (net);
  2843. defer_kevent (dev, EVENT_TX_HALT);
  2844. break;
  2845. default:
  2846. if (netif_msg_tx_err (dev))
  2847. devdbg (dev, "tx: submit urb err %d", retval);
  2848. break;
  2849. case 0:
  2850. net->trans_start = jiffies;
  2851. __skb_queue_tail (&dev->txq, skb);
  2852. if (dev->txq.qlen >= TX_QLEN (dev))
  2853. netif_stop_queue (net);
  2854. }
  2855. spin_unlock_irqrestore (&dev->txq.lock, flags);
  2856. if (retval) {
  2857. if (netif_msg_tx_err (dev))
  2858. devdbg (dev, "drop, code %d", retval);
  2859. drop:
  2860. retval = NET_XMIT_SUCCESS;
  2861. dev->stats.tx_dropped++;
  2862. if (skb)
  2863. dev_kfree_skb_any (skb);
  2864. usb_free_urb (urb);
  2865. } else if (netif_msg_tx_queued (dev)) {
  2866. devdbg (dev, "> tx, len %d, type 0x%x",
  2867. length, skb->protocol);
  2868. }
  2869. return retval;
  2870. }
  2871. /*-------------------------------------------------------------------------*/
  2872. // tasklet (work deferred from completions, in_irq) or timer
  2873. static void usbnet_bh (unsigned long param)
  2874. {
  2875. struct usbnet *dev = (struct usbnet *) param;
  2876. struct sk_buff *skb;
  2877. struct skb_data *entry;
  2878. while ((skb = skb_dequeue (&dev->done))) {
  2879. entry = (struct skb_data *) skb->cb;
  2880. switch (entry->state) {
  2881. case rx_done:
  2882. entry->state = rx_cleanup;
  2883. rx_process (dev, skb);
  2884. continue;
  2885. case tx_done:
  2886. case rx_cleanup:
  2887. usb_free_urb (entry->urb);
  2888. dev_kfree_skb (skb);
  2889. continue;
  2890. default:
  2891. devdbg (dev, "bogus skb state %d", entry->state);
  2892. }
  2893. }
  2894. // waiting for all pending urbs to complete?
  2895. if (dev->wait) {
  2896. if ((dev->txq.qlen + dev->rxq.qlen + dev->done.qlen) == 0) {
  2897. wake_up (dev->wait);
  2898. }
  2899. // or are we maybe short a few urbs?
  2900. } else if (netif_running (dev->net)
  2901. && netif_device_present (dev->net)
  2902. && !timer_pending (&dev->delay)
  2903. && !test_bit (EVENT_RX_HALT, &dev->flags)) {
  2904. int temp = dev->rxq.qlen;
  2905. int qlen = RX_QLEN (dev);
  2906. if (temp < qlen) {
  2907. struct urb *urb;
  2908. int i;
  2909. // don't refill the queue all at once
  2910. for (i = 0; i < 10 && dev->rxq.qlen < qlen; i++) {
  2911. urb = usb_alloc_urb (0, GFP_ATOMIC);
  2912. if (urb != NULL)
  2913. rx_submit (dev, urb, GFP_ATOMIC);
  2914. }
  2915. if (temp != dev->rxq.qlen && netif_msg_link (dev))
  2916. devdbg (dev, "rxqlen %d --> %d",
  2917. temp, dev->rxq.qlen);
  2918. if (dev->rxq.qlen < qlen)
  2919. tasklet_schedule (&dev->bh);
  2920. }
  2921. if (dev->txq.qlen < TX_QLEN (dev))
  2922. netif_wake_queue (dev->net);
  2923. }
  2924. }
  2925. /*-------------------------------------------------------------------------
  2926. *
  2927. * USB Device Driver support
  2928. *
  2929. *-------------------------------------------------------------------------*/
  2930. // precondition: never called in_interrupt
  2931. void usbnet_disconnect (struct usb_interface *intf)
  2932. {
  2933. struct usbnet *dev;
  2934. struct usb_device *xdev;
  2935. struct net_device *net;
  2936. dev = usb_get_intfdata(intf);
  2937. usb_set_intfdata(intf, NULL);
  2938. if (!dev)
  2939. return;
  2940. xdev = interface_to_usbdev (intf);
  2941. if (netif_msg_probe (dev))
  2942. devinfo (dev, "unregister '%s' usb-%s-%s, %s",
  2943. intf->dev.driver->name,
  2944. xdev->bus->bus_name, xdev->devpath,
  2945. dev->driver_info->description);
  2946. net = dev->net;
  2947. unregister_netdev (net);
  2948. /* we don't hold rtnl here ... */
  2949. flush_scheduled_work ();
  2950. if (dev->driver_info->unbind)
  2951. dev->driver_info->unbind (dev, intf);
  2952. free_netdev(net);
  2953. usb_put_dev (xdev);
  2954. }
  2955. EXPORT_SYMBOL_GPL(usbnet_disconnect);
  2956. /*-------------------------------------------------------------------------*/
  2957. static struct ethtool_ops usbnet_ethtool_ops;
  2958. // precondition: never called in_interrupt
  2959. int
  2960. usbnet_probe (struct usb_interface *udev, const struct usb_device_id *prod)
  2961. {
  2962. struct usbnet *dev;
  2963. struct net_device *net;
  2964. struct usb_host_interface *interface;
  2965. struct driver_info *info;
  2966. struct usb_device *xdev;
  2967. int status;
  2968. info = (struct driver_info *) prod->driver_info;
  2969. if (!info) {
  2970. dev_dbg (&udev->dev, "blacklisted by %s\n", driver_name);
  2971. return -ENODEV;
  2972. }
  2973. xdev = interface_to_usbdev (udev);
  2974. interface = udev->cur_altsetting;
  2975. usb_get_dev (xdev);
  2976. status = -ENOMEM;
  2977. // set up our own records
  2978. net = alloc_etherdev(sizeof(*dev));
  2979. if (!net) {
  2980. dbg ("can't kmalloc dev");
  2981. goto out;
  2982. }
  2983. dev = netdev_priv(net);
  2984. dev->udev = xdev;
  2985. dev->driver_info = info;
  2986. dev->msg_enable = netif_msg_init (msg_level, NETIF_MSG_DRV
  2987. | NETIF_MSG_PROBE | NETIF_MSG_LINK);
  2988. skb_queue_head_init (&dev->rxq);
  2989. skb_queue_head_init (&dev->txq);
  2990. skb_queue_head_init (&dev->done);
  2991. dev->bh.func = usbnet_bh;
  2992. dev->bh.data = (unsigned long) dev;
  2993. INIT_WORK (&dev->kevent, kevent, dev);
  2994. dev->delay.function = usbnet_bh;
  2995. dev->delay.data = (unsigned long) dev;
  2996. init_timer (&dev->delay);
  2997. SET_MODULE_OWNER (net);
  2998. dev->net = net;
  2999. strcpy (net->name, "usb%d");
  3000. memcpy (net->dev_addr, node_id, sizeof node_id);
  3001. dev->hard_mtu = net->mtu + net->hard_header_len;
  3002. #if 0
  3003. // dma_supported() is deeply broken on almost all architectures
  3004. // possible with some EHCI controllers
  3005. if (dma_supported (&udev->dev, DMA_64BIT_MASK))
  3006. net->features |= NETIF_F_HIGHDMA;
  3007. #endif
  3008. net->change_mtu = usbnet_change_mtu;
  3009. net->get_stats = usbnet_get_stats;
  3010. net->hard_start_xmit = usbnet_start_xmit;
  3011. net->open = usbnet_open;
  3012. net->stop = usbnet_stop;
  3013. net->watchdog_timeo = TX_TIMEOUT_JIFFIES;
  3014. net->tx_timeout = usbnet_tx_timeout;
  3015. net->do_ioctl = usbnet_ioctl;
  3016. net->ethtool_ops = &usbnet_ethtool_ops;
  3017. // allow device-specific bind/init procedures
  3018. // NOTE net->name still not usable ...
  3019. if (info->bind) {
  3020. status = info->bind (dev, udev);
  3021. // heuristic: "usb%d" for links we know are two-host,
  3022. // else "eth%d" when there's reasonable doubt. userspace
  3023. // can rename the link if it knows better.
  3024. if ((dev->driver_info->flags & FLAG_ETHER) != 0
  3025. && (net->dev_addr [0] & 0x02) == 0)
  3026. strcpy (net->name, "eth%d");
  3027. /* maybe the remote can't receive an Ethernet MTU */
  3028. if (net->mtu > (dev->hard_mtu - net->hard_header_len))
  3029. net->mtu = dev->hard_mtu - net->hard_header_len;
  3030. } else if (!info->in || info->out)
  3031. status = get_endpoints (dev, udev);
  3032. else {
  3033. dev->in = usb_rcvbulkpipe (xdev, info->in);
  3034. dev->out = usb_sndbulkpipe (xdev, info->out);
  3035. if (!(info->flags & FLAG_NO_SETINT))
  3036. status = usb_set_interface (xdev,
  3037. interface->desc.bInterfaceNumber,
  3038. interface->desc.bAlternateSetting);
  3039. else
  3040. status = 0;
  3041. }
  3042. if (status == 0 && dev->status)
  3043. status = init_status (dev, udev);
  3044. if (status < 0)
  3045. goto out1;
  3046. dev->maxpacket = usb_maxpacket (dev->udev, dev->out, 1);
  3047. SET_NETDEV_DEV(net, &udev->dev);
  3048. status = register_netdev (net);
  3049. if (status)
  3050. goto out3;
  3051. if (netif_msg_probe (dev))
  3052. devinfo (dev, "register '%s' at usb-%s-%s, %s, "
  3053. "%02x:%02x:%02x:%02x:%02x:%02x",
  3054. udev->dev.driver->name,
  3055. xdev->bus->bus_name, xdev->devpath,
  3056. dev->driver_info->description,
  3057. net->dev_addr [0], net->dev_addr [1],
  3058. net->dev_addr [2], net->dev_addr [3],
  3059. net->dev_addr [4], net->dev_addr [5]);
  3060. // ok, it's ready to go.
  3061. usb_set_intfdata (udev, dev);
  3062. // start as if the link is up
  3063. netif_device_attach (net);
  3064. return 0;
  3065. out3:
  3066. if (info->unbind)
  3067. info->unbind (dev, udev);
  3068. out1:
  3069. free_netdev(net);
  3070. out:
  3071. usb_put_dev(xdev);
  3072. return status;
  3073. }
  3074. EXPORT_SYMBOL_GPL(usbnet_probe);
  3075. /*-------------------------------------------------------------------------*/
  3076. /* FIXME these suspend/resume methods assume non-CDC style
  3077. * devices, with only one interface.
  3078. */
  3079. int usbnet_suspend (struct usb_interface *intf, pm_message_t message)
  3080. {
  3081. struct usbnet *dev = usb_get_intfdata(intf);
  3082. /* accelerate emptying of the rx and queues, to avoid
  3083. * having everything error out.
  3084. */
  3085. netif_device_detach (dev->net);
  3086. (void) unlink_urbs (dev, &dev->rxq);
  3087. (void) unlink_urbs (dev, &dev->txq);
  3088. intf->dev.power.power_state = PMSG_SUSPEND;
  3089. return 0;
  3090. }
  3091. EXPORT_SYMBOL_GPL(usbnet_suspend);
  3092. int usbnet_resume (struct usb_interface *intf)
  3093. {
  3094. struct usbnet *dev = usb_get_intfdata(intf);
  3095. intf->dev.power.power_state = PMSG_ON;
  3096. netif_device_attach (dev->net);
  3097. tasklet_schedule (&dev->bh);
  3098. return 0;
  3099. }
  3100. EXPORT_SYMBOL_GPL(usbnet_resume);
  3101. /*-------------------------------------------------------------------------*/
  3102. #ifndef HAVE_HARDWARE
  3103. #error You need to configure some hardware for this driver
  3104. #endif
  3105. /*
  3106. * chip vendor names won't normally be on the cables, and
  3107. * may not be on the device.
  3108. */
  3109. static const struct usb_device_id products [] = {
  3110. #ifdef CONFIG_USB_AX8817X
  3111. {
  3112. // Linksys USB200M
  3113. USB_DEVICE (0x077b, 0x2226),
  3114. .driver_info = (unsigned long) &ax8817x_info,
  3115. }, {
  3116. // Netgear FA120
  3117. USB_DEVICE (0x0846, 0x1040),
  3118. .driver_info = (unsigned long) &netgear_fa120_info,
  3119. }, {
  3120. // DLink DUB-E100
  3121. USB_DEVICE (0x2001, 0x1a00),
  3122. .driver_info = (unsigned long) &dlink_dub_e100_info,
  3123. }, {
  3124. // Intellinet, ST Lab USB Ethernet
  3125. USB_DEVICE (0x0b95, 0x1720),
  3126. .driver_info = (unsigned long) &ax8817x_info,
  3127. }, {
  3128. // Hawking UF200, TrendNet TU2-ET100
  3129. USB_DEVICE (0x07b8, 0x420a),
  3130. .driver_info = (unsigned long) &hawking_uf200_info,
  3131. }, {
  3132. // Billionton Systems, USB2AR
  3133. USB_DEVICE (0x08dd, 0x90ff),
  3134. .driver_info = (unsigned long) &ax8817x_info,
  3135. }, {
  3136. // ATEN UC210T
  3137. USB_DEVICE (0x0557, 0x2009),
  3138. .driver_info = (unsigned long) &ax8817x_info,
  3139. }, {
  3140. // Buffalo LUA-U2-KTX
  3141. USB_DEVICE (0x0411, 0x003d),
  3142. .driver_info = (unsigned long) &ax8817x_info,
  3143. }, {
  3144. // Sitecom LN-029 "USB 2.0 10/100 Ethernet adapter"
  3145. USB_DEVICE (0x6189, 0x182d),
  3146. .driver_info = (unsigned long) &ax8817x_info,
  3147. }, {
  3148. // corega FEther USB2-TX
  3149. USB_DEVICE (0x07aa, 0x0017),
  3150. .driver_info = (unsigned long) &ax8817x_info,
  3151. }, {
  3152. // Surecom EP-1427X-2
  3153. USB_DEVICE (0x1189, 0x0893),
  3154. .driver_info = (unsigned long) &ax8817x_info,
  3155. }, {
  3156. // goodway corp usb gwusb2e
  3157. USB_DEVICE (0x1631, 0x6200),
  3158. .driver_info = (unsigned long) &ax8817x_info,
  3159. }, {
  3160. // ASIX AX88772 10/100
  3161. USB_DEVICE (0x0b95, 0x7720),
  3162. .driver_info = (unsigned long) &ax88772_info,
  3163. },
  3164. #endif
  3165. #ifdef CONFIG_USB_GENESYS
  3166. {
  3167. USB_DEVICE (0x05e3, 0x0502), // GL620USB-A
  3168. .driver_info = (unsigned long) &genelink_info,
  3169. },
  3170. /* NOT: USB_DEVICE (0x05e3, 0x0501), // GL620USB
  3171. * that's half duplex, not currently supported
  3172. */
  3173. #endif
  3174. #ifdef CONFIG_USB_NET1080
  3175. {
  3176. USB_DEVICE (0x0525, 0x1080), // NetChip ref design
  3177. .driver_info = (unsigned long) &net1080_info,
  3178. }, {
  3179. USB_DEVICE (0x06D0, 0x0622), // Laplink Gold
  3180. .driver_info = (unsigned long) &net1080_info,
  3181. },
  3182. #endif
  3183. #ifdef CONFIG_USB_PL2301
  3184. {
  3185. USB_DEVICE (0x067b, 0x0000), // PL-2301
  3186. .driver_info = (unsigned long) &prolific_info,
  3187. }, {
  3188. USB_DEVICE (0x067b, 0x0001), // PL-2302
  3189. .driver_info = (unsigned long) &prolific_info,
  3190. },
  3191. #endif
  3192. #ifdef CONFIG_USB_RNDIS
  3193. {
  3194. /* RNDIS is MSFT's un-official variant of CDC ACM */
  3195. USB_INTERFACE_INFO (USB_CLASS_COMM, 2 /* ACM */, 0x0ff),
  3196. .driver_info = (unsigned long) &rndis_info,
  3197. },
  3198. #endif
  3199. #if defined(CONFIG_USB_ZAURUS) || defined(CONFIG_USB_CDCETHER)
  3200. /*
  3201. * SA-1100 based Sharp Zaurus ("collie"), or compatible.
  3202. * Same idea as above, but different framing.
  3203. *
  3204. * PXA-2xx based models are also lying-about-cdc.
  3205. * Some models don't even tell the same lies ...
  3206. *
  3207. * NOTE: OpenZaurus versions with 2.6 kernels won't use these entries,
  3208. * unlike the older ones with 2.4 "embedix" kernels.
  3209. *
  3210. * NOTE: These entries do double-duty, serving as blacklist entries
  3211. * whenever Zaurus support isn't enabled, but CDC Ethernet is.
  3212. */
  3213. #define ZAURUS_MASTER_INTERFACE \
  3214. .bInterfaceClass = USB_CLASS_COMM, \
  3215. .bInterfaceSubClass = USB_CDC_SUBCLASS_ETHERNET, \
  3216. .bInterfaceProtocol = USB_CDC_PROTO_NONE
  3217. {
  3218. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3219. | USB_DEVICE_ID_MATCH_DEVICE,
  3220. .idVendor = 0x04DD,
  3221. .idProduct = 0x8004,
  3222. ZAURUS_MASTER_INTERFACE,
  3223. .driver_info = ZAURUS_STRONGARM_INFO,
  3224. }, {
  3225. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3226. | USB_DEVICE_ID_MATCH_DEVICE,
  3227. .idVendor = 0x04DD,
  3228. .idProduct = 0x8005, /* A-300 */
  3229. ZAURUS_MASTER_INTERFACE,
  3230. .driver_info = ZAURUS_PXA_INFO,
  3231. }, {
  3232. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3233. | USB_DEVICE_ID_MATCH_DEVICE,
  3234. .idVendor = 0x04DD,
  3235. .idProduct = 0x8006, /* B-500/SL-5600 */
  3236. ZAURUS_MASTER_INTERFACE,
  3237. .driver_info = ZAURUS_PXA_INFO,
  3238. }, {
  3239. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3240. | USB_DEVICE_ID_MATCH_DEVICE,
  3241. .idVendor = 0x04DD,
  3242. .idProduct = 0x8007, /* C-700 */
  3243. ZAURUS_MASTER_INTERFACE,
  3244. .driver_info = ZAURUS_PXA_INFO,
  3245. }, {
  3246. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3247. | USB_DEVICE_ID_MATCH_DEVICE,
  3248. .idVendor = 0x04DD,
  3249. .idProduct = 0x9031, /* C-750 C-760 */
  3250. ZAURUS_MASTER_INTERFACE,
  3251. .driver_info = ZAURUS_PXA_INFO,
  3252. }, {
  3253. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3254. | USB_DEVICE_ID_MATCH_DEVICE,
  3255. .idVendor = 0x04DD,
  3256. .idProduct = 0x9032, /* SL-6000 */
  3257. ZAURUS_MASTER_INTERFACE,
  3258. .driver_info = ZAURUS_PXA_INFO,
  3259. }, {
  3260. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3261. | USB_DEVICE_ID_MATCH_DEVICE,
  3262. .idVendor = 0x04DD,
  3263. /* reported with some C860 units */
  3264. .idProduct = 0x9050, /* C-860 */
  3265. ZAURUS_MASTER_INTERFACE,
  3266. .driver_info = ZAURUS_PXA_INFO,
  3267. },
  3268. #ifdef CONFIG_USB_ZAURUS
  3269. /* At least some (reports vary) PXA units have very different lies
  3270. * about their standards support: they claim to be cell phones with
  3271. * direct access to their radios. (They don't conform to CDC MDLM.)
  3272. */
  3273. {
  3274. USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_MDLM,
  3275. USB_CDC_PROTO_NONE),
  3276. .driver_info = (unsigned long) &bogus_mdlm_info,
  3277. },
  3278. #endif
  3279. /* Olympus has some models with a Zaurus-compatible option.
  3280. * R-1000 uses a FreeScale i.MXL cpu (ARMv4T)
  3281. */
  3282. {
  3283. .match_flags = USB_DEVICE_ID_MATCH_INT_INFO
  3284. | USB_DEVICE_ID_MATCH_DEVICE,
  3285. .idVendor = 0x07B4,
  3286. .idProduct = 0x0F02, /* R-1000 */
  3287. ZAURUS_MASTER_INTERFACE,
  3288. .driver_info = OLYMPUS_MXL_INFO,
  3289. },
  3290. #endif
  3291. #ifdef CONFIG_USB_CDCETHER
  3292. {
  3293. /* CDC Ether uses two interfaces, not necessarily consecutive.
  3294. * We match the main interface, ignoring the optional device
  3295. * class so we could handle devices that aren't exclusively
  3296. * CDC ether.
  3297. *
  3298. * NOTE: this match must come AFTER entries working around
  3299. * bugs/quirks in a given product (like Zaurus, above).
  3300. */
  3301. USB_INTERFACE_INFO (USB_CLASS_COMM, USB_CDC_SUBCLASS_ETHERNET,
  3302. USB_CDC_PROTO_NONE),
  3303. .driver_info = (unsigned long) &cdc_info,
  3304. },
  3305. #endif
  3306. { }, // END
  3307. };
  3308. MODULE_DEVICE_TABLE (usb, products);
  3309. static struct usb_driver usbnet_driver = {
  3310. .owner = THIS_MODULE,
  3311. .name = driver_name,
  3312. .id_table = products,
  3313. .probe = usbnet_probe,
  3314. .disconnect = usbnet_disconnect,
  3315. .suspend = usbnet_suspend,
  3316. .resume = usbnet_resume,
  3317. };
  3318. /* Default ethtool_ops assigned. Devices can override in their bind() routine */
  3319. static struct ethtool_ops usbnet_ethtool_ops = {
  3320. .get_drvinfo = usbnet_get_drvinfo,
  3321. .get_link = usbnet_get_link,
  3322. .get_msglevel = usbnet_get_msglevel,
  3323. .set_msglevel = usbnet_set_msglevel,
  3324. };
  3325. /*-------------------------------------------------------------------------*/
  3326. static int __init usbnet_init(void)
  3327. {
  3328. // compiler should optimize these out
  3329. BUG_ON (sizeof (((struct sk_buff *)0)->cb)
  3330. < sizeof (struct skb_data));
  3331. #ifdef CONFIG_USB_CDCETHER
  3332. BUG_ON ((sizeof (((struct usbnet *)0)->data)
  3333. < sizeof (struct cdc_state)));
  3334. #endif
  3335. random_ether_addr(node_id);
  3336. return usb_register(&usbnet_driver);
  3337. }
  3338. module_init(usbnet_init);
  3339. static void __exit usbnet_exit(void)
  3340. {
  3341. usb_deregister(&usbnet_driver);
  3342. }
  3343. module_exit(usbnet_exit);
  3344. MODULE_AUTHOR("David Brownell");
  3345. MODULE_DESCRIPTION("USB Host-to-Host Link Drivers (numerous vendors)");
  3346. MODULE_LICENSE("GPL");