at76c50x-usb.c 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616
  1. /*
  2. * at76c503/at76c505 USB driver
  3. *
  4. * Copyright (c) 2002 - 2003 Oliver Kurth
  5. * Copyright (c) 2004 Joerg Albert <joerg.albert@gmx.de>
  6. * Copyright (c) 2004 Nick Jones
  7. * Copyright (c) 2004 Balint Seeber <n0_5p4m_p13453@hotmail.com>
  8. * Copyright (c) 2007 Guido Guenther <agx@sigxcpu.org>
  9. * Copyright (c) 2007 Kalle Valo <kalle.valo@iki.fi>
  10. * Copyright (c) 2010 Sebastian Smolorz <sesmo@gmx.net>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This file is part of the Berlios driver for WLAN USB devices based on the
  18. * Atmel AT76C503A/505/505A.
  19. *
  20. * Some iw_handler code was taken from airo.c, (C) 1999 Benjamin Reed
  21. *
  22. * TODO list is at the wiki:
  23. *
  24. * http://wireless.kernel.org/en/users/Drivers/at76c50x-usb#TODO
  25. *
  26. */
  27. #include <linux/init.h>
  28. #include <linux/kernel.h>
  29. #include <linux/sched.h>
  30. #include <linux/errno.h>
  31. #include <linux/slab.h>
  32. #include <linux/module.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/list.h>
  35. #include <linux/usb.h>
  36. #include <linux/netdevice.h>
  37. #include <linux/if_arp.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/ethtool.h>
  40. #include <linux/wireless.h>
  41. #include <net/iw_handler.h>
  42. #include <net/ieee80211_radiotap.h>
  43. #include <linux/firmware.h>
  44. #include <linux/leds.h>
  45. #include <net/mac80211.h>
  46. #include "at76c50x-usb.h"
  47. /* Version information */
  48. #define DRIVER_NAME "at76c50x-usb"
  49. #define DRIVER_VERSION "0.17"
  50. #define DRIVER_DESC "Atmel at76x USB Wireless LAN Driver"
  51. /* at76_debug bits */
  52. #define DBG_PROGRESS 0x00000001 /* authentication/accociation */
  53. #define DBG_BSS_TABLE 0x00000002 /* show BSS table after scans */
  54. #define DBG_IOCTL 0x00000004 /* ioctl calls / settings */
  55. #define DBG_MAC_STATE 0x00000008 /* MAC state transitions */
  56. #define DBG_TX_DATA 0x00000010 /* tx header */
  57. #define DBG_TX_DATA_CONTENT 0x00000020 /* tx content */
  58. #define DBG_TX_MGMT 0x00000040 /* tx management */
  59. #define DBG_RX_DATA 0x00000080 /* rx data header */
  60. #define DBG_RX_DATA_CONTENT 0x00000100 /* rx data content */
  61. #define DBG_RX_MGMT 0x00000200 /* rx mgmt frame headers */
  62. #define DBG_RX_BEACON 0x00000400 /* rx beacon */
  63. #define DBG_RX_CTRL 0x00000800 /* rx control */
  64. #define DBG_RX_MGMT_CONTENT 0x00001000 /* rx mgmt content */
  65. #define DBG_RX_FRAGS 0x00002000 /* rx data fragment handling */
  66. #define DBG_DEVSTART 0x00004000 /* fw download, device start */
  67. #define DBG_URB 0x00008000 /* rx urb status, ... */
  68. #define DBG_RX_ATMEL_HDR 0x00010000 /* Atmel-specific Rx headers */
  69. #define DBG_PROC_ENTRY 0x00020000 /* procedure entries/exits */
  70. #define DBG_PM 0x00040000 /* power management settings */
  71. #define DBG_BSS_MATCH 0x00080000 /* BSS match failures */
  72. #define DBG_PARAMS 0x00100000 /* show configured parameters */
  73. #define DBG_WAIT_COMPLETE 0x00200000 /* command completion */
  74. #define DBG_RX_FRAGS_SKB 0x00400000 /* skb header of Rx fragments */
  75. #define DBG_BSS_TABLE_RM 0x00800000 /* purging bss table entries */
  76. #define DBG_MONITOR_MODE 0x01000000 /* monitor mode */
  77. #define DBG_MIB 0x02000000 /* dump all MIBs on startup */
  78. #define DBG_MGMT_TIMER 0x04000000 /* dump mgmt_timer ops */
  79. #define DBG_WE_EVENTS 0x08000000 /* dump wireless events */
  80. #define DBG_FW 0x10000000 /* firmware download */
  81. #define DBG_DFU 0x20000000 /* device firmware upgrade */
  82. #define DBG_CMD 0x40000000
  83. #define DBG_MAC80211 0x80000000
  84. #define DBG_DEFAULTS 0
  85. /* Use our own dbg macro */
  86. #define at76_dbg(bits, format, arg...) \
  87. do { \
  88. if (at76_debug & (bits)) \
  89. printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg); \
  90. } while (0)
  91. #define at76_dbg_dump(bits, buf, len, format, arg...) \
  92. do { \
  93. if (at76_debug & (bits)) { \
  94. printk(KERN_DEBUG DRIVER_NAME ": " format "\n", ##arg); \
  95. print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, buf, len); \
  96. } \
  97. } while (0)
  98. static uint at76_debug = DBG_DEFAULTS;
  99. /* Protect against concurrent firmware loading and parsing */
  100. static struct mutex fw_mutex;
  101. static struct fwentry firmwares[] = {
  102. [0] = { "" },
  103. [BOARD_503_ISL3861] = { "atmel_at76c503-i3861.bin" },
  104. [BOARD_503_ISL3863] = { "atmel_at76c503-i3863.bin" },
  105. [BOARD_503] = { "atmel_at76c503-rfmd.bin" },
  106. [BOARD_503_ACC] = { "atmel_at76c503-rfmd-acc.bin" },
  107. [BOARD_505] = { "atmel_at76c505-rfmd.bin" },
  108. [BOARD_505_2958] = { "atmel_at76c505-rfmd2958.bin" },
  109. [BOARD_505A] = { "atmel_at76c505a-rfmd2958.bin" },
  110. [BOARD_505AMX] = { "atmel_at76c505amx-rfmd.bin" },
  111. };
  112. MODULE_FIRMWARE("atmel_at76c503-i3861.bin");
  113. MODULE_FIRMWARE("atmel_at76c503-i3863.bin");
  114. MODULE_FIRMWARE("atmel_at76c503-rfmd.bin");
  115. MODULE_FIRMWARE("atmel_at76c503-rfmd-acc.bin");
  116. MODULE_FIRMWARE("atmel_at76c505-rfmd.bin");
  117. MODULE_FIRMWARE("atmel_at76c505-rfmd2958.bin");
  118. MODULE_FIRMWARE("atmel_at76c505a-rfmd2958.bin");
  119. MODULE_FIRMWARE("atmel_at76c505amx-rfmd.bin");
  120. #define USB_DEVICE_DATA(__ops) .driver_info = (kernel_ulong_t)(__ops)
  121. static const struct usb_device_id dev_table[] = {
  122. /*
  123. * at76c503-i3861
  124. */
  125. /* Generic AT76C503/3861 device */
  126. { USB_DEVICE(0x03eb, 0x7603), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  127. /* Linksys WUSB11 v2.1/v2.6 */
  128. { USB_DEVICE(0x066b, 0x2211), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  129. /* Netgear MA101 rev. A */
  130. { USB_DEVICE(0x0864, 0x4100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  131. /* Tekram U300C / Allnet ALL0193 */
  132. { USB_DEVICE(0x0b3b, 0x1612), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  133. /* HP HN210W J7801A */
  134. { USB_DEVICE(0x03f0, 0x011c), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  135. /* Sitecom/Z-Com/Zyxel M4Y-750 */
  136. { USB_DEVICE(0x0cde, 0x0001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  137. /* Dynalink/Askey WLL013 (intersil) */
  138. { USB_DEVICE(0x069a, 0x0320), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  139. /* EZ connect 11Mpbs Wireless USB Adapter SMC2662W v1 */
  140. { USB_DEVICE(0x0d5c, 0xa001), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  141. /* BenQ AWL300 */
  142. { USB_DEVICE(0x04a5, 0x9000), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  143. /* Addtron AWU-120, Compex WLU11 */
  144. { USB_DEVICE(0x05dd, 0xff31), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  145. /* Intel AP310 AnyPoint II USB */
  146. { USB_DEVICE(0x8086, 0x0200), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  147. /* Dynalink L11U */
  148. { USB_DEVICE(0x0d8e, 0x7100), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  149. /* Arescom WL-210, FCC id 07J-GL2411USB */
  150. { USB_DEVICE(0x0d8e, 0x7110), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  151. /* I-O DATA WN-B11/USB */
  152. { USB_DEVICE(0x04bb, 0x0919), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  153. /* BT Voyager 1010 */
  154. { USB_DEVICE(0x069a, 0x0821), USB_DEVICE_DATA(BOARD_503_ISL3861) },
  155. /*
  156. * at76c503-i3863
  157. */
  158. /* Generic AT76C503/3863 device */
  159. { USB_DEVICE(0x03eb, 0x7604), USB_DEVICE_DATA(BOARD_503_ISL3863) },
  160. /* Samsung SWL-2100U */
  161. { USB_DEVICE(0x055d, 0xa000), USB_DEVICE_DATA(BOARD_503_ISL3863) },
  162. /*
  163. * at76c503-rfmd
  164. */
  165. /* Generic AT76C503/RFMD device */
  166. { USB_DEVICE(0x03eb, 0x7605), USB_DEVICE_DATA(BOARD_503) },
  167. /* Dynalink/Askey WLL013 (rfmd) */
  168. { USB_DEVICE(0x069a, 0x0321), USB_DEVICE_DATA(BOARD_503) },
  169. /* Linksys WUSB11 v2.6 */
  170. { USB_DEVICE(0x077b, 0x2219), USB_DEVICE_DATA(BOARD_503) },
  171. /* Network Everywhere NWU11B */
  172. { USB_DEVICE(0x077b, 0x2227), USB_DEVICE_DATA(BOARD_503) },
  173. /* Netgear MA101 rev. B */
  174. { USB_DEVICE(0x0864, 0x4102), USB_DEVICE_DATA(BOARD_503) },
  175. /* D-Link DWL-120 rev. E */
  176. { USB_DEVICE(0x2001, 0x3200), USB_DEVICE_DATA(BOARD_503) },
  177. /* Actiontec 802UAT1, HWU01150-01UK */
  178. { USB_DEVICE(0x1668, 0x7605), USB_DEVICE_DATA(BOARD_503) },
  179. /* AirVast W-Buddie WN210 */
  180. { USB_DEVICE(0x03eb, 0x4102), USB_DEVICE_DATA(BOARD_503) },
  181. /* Dick Smith Electronics XH1153 802.11b USB adapter */
  182. { USB_DEVICE(0x1371, 0x5743), USB_DEVICE_DATA(BOARD_503) },
  183. /* CNet CNUSB611 */
  184. { USB_DEVICE(0x1371, 0x0001), USB_DEVICE_DATA(BOARD_503) },
  185. /* FiberLine FL-WL200U */
  186. { USB_DEVICE(0x1371, 0x0002), USB_DEVICE_DATA(BOARD_503) },
  187. /* BenQ AWL400 USB stick */
  188. { USB_DEVICE(0x04a5, 0x9001), USB_DEVICE_DATA(BOARD_503) },
  189. /* 3Com 3CRSHEW696 */
  190. { USB_DEVICE(0x0506, 0x0a01), USB_DEVICE_DATA(BOARD_503) },
  191. /* Siemens Santis ADSL WLAN USB adapter WLL 013 */
  192. { USB_DEVICE(0x0681, 0x001b), USB_DEVICE_DATA(BOARD_503) },
  193. /* Belkin F5D6050, version 2 */
  194. { USB_DEVICE(0x050d, 0x0050), USB_DEVICE_DATA(BOARD_503) },
  195. /* iBlitzz, BWU613 (not *B or *SB) */
  196. { USB_DEVICE(0x07b8, 0xb000), USB_DEVICE_DATA(BOARD_503) },
  197. /* Gigabyte GN-WLBM101 */
  198. { USB_DEVICE(0x1044, 0x8003), USB_DEVICE_DATA(BOARD_503) },
  199. /* Planex GW-US11S */
  200. { USB_DEVICE(0x2019, 0x3220), USB_DEVICE_DATA(BOARD_503) },
  201. /* Internal WLAN adapter in h5[4,5]xx series iPAQs */
  202. { USB_DEVICE(0x049f, 0x0032), USB_DEVICE_DATA(BOARD_503) },
  203. /* Corega Wireless LAN USB-11 mini */
  204. { USB_DEVICE(0x07aa, 0x0011), USB_DEVICE_DATA(BOARD_503) },
  205. /* Corega Wireless LAN USB-11 mini2 */
  206. { USB_DEVICE(0x07aa, 0x0018), USB_DEVICE_DATA(BOARD_503) },
  207. /* Uniden PCW100 */
  208. { USB_DEVICE(0x05dd, 0xff35), USB_DEVICE_DATA(BOARD_503) },
  209. /*
  210. * at76c503-rfmd-acc
  211. */
  212. /* SMC2664W */
  213. { USB_DEVICE(0x083a, 0x3501), USB_DEVICE_DATA(BOARD_503_ACC) },
  214. /* Belkin F5D6050, SMC2662W v2, SMC2662W-AR */
  215. { USB_DEVICE(0x0d5c, 0xa002), USB_DEVICE_DATA(BOARD_503_ACC) },
  216. /*
  217. * at76c505-rfmd
  218. */
  219. /* Generic AT76C505/RFMD */
  220. { USB_DEVICE(0x03eb, 0x7606), USB_DEVICE_DATA(BOARD_505) },
  221. /*
  222. * at76c505-rfmd2958
  223. */
  224. /* Generic AT76C505/RFMD, OvisLink WL-1130USB */
  225. { USB_DEVICE(0x03eb, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
  226. /* Fiberline FL-WL240U */
  227. { USB_DEVICE(0x1371, 0x0014), USB_DEVICE_DATA(BOARD_505_2958) },
  228. /* CNet CNUSB-611G */
  229. { USB_DEVICE(0x1371, 0x0013), USB_DEVICE_DATA(BOARD_505_2958) },
  230. /* Linksys WUSB11 v2.8 */
  231. { USB_DEVICE(0x1915, 0x2233), USB_DEVICE_DATA(BOARD_505_2958) },
  232. /* Xterasys XN-2122B, IBlitzz BWU613B/BWU613SB */
  233. { USB_DEVICE(0x12fd, 0x1001), USB_DEVICE_DATA(BOARD_505_2958) },
  234. /* Corega WLAN USB Stick 11 */
  235. { USB_DEVICE(0x07aa, 0x7613), USB_DEVICE_DATA(BOARD_505_2958) },
  236. /* Microstar MSI Box MS6978 */
  237. { USB_DEVICE(0x0db0, 0x1020), USB_DEVICE_DATA(BOARD_505_2958) },
  238. /*
  239. * at76c505a-rfmd2958
  240. */
  241. /* Generic AT76C505A device */
  242. { USB_DEVICE(0x03eb, 0x7614), USB_DEVICE_DATA(BOARD_505A) },
  243. /* Generic AT76C505AS device */
  244. { USB_DEVICE(0x03eb, 0x7617), USB_DEVICE_DATA(BOARD_505A) },
  245. /* Siemens Gigaset USB WLAN Adapter 11 */
  246. { USB_DEVICE(0x1690, 0x0701), USB_DEVICE_DATA(BOARD_505A) },
  247. /* OQO Model 01+ Internal Wi-Fi */
  248. { USB_DEVICE(0x1557, 0x0002), USB_DEVICE_DATA(BOARD_505A) },
  249. /*
  250. * at76c505amx-rfmd
  251. */
  252. /* Generic AT76C505AMX device */
  253. { USB_DEVICE(0x03eb, 0x7615), USB_DEVICE_DATA(BOARD_505AMX) },
  254. { }
  255. };
  256. MODULE_DEVICE_TABLE(usb, dev_table);
  257. /* Supported rates of this hardware, bit 7 marks basic rates */
  258. static const u8 hw_rates[] = { 0x82, 0x84, 0x0b, 0x16 };
  259. static const char *const preambles[] = { "long", "short", "auto" };
  260. /* Firmware download */
  261. /* DFU states */
  262. #define STATE_IDLE 0x00
  263. #define STATE_DETACH 0x01
  264. #define STATE_DFU_IDLE 0x02
  265. #define STATE_DFU_DOWNLOAD_SYNC 0x03
  266. #define STATE_DFU_DOWNLOAD_BUSY 0x04
  267. #define STATE_DFU_DOWNLOAD_IDLE 0x05
  268. #define STATE_DFU_MANIFEST_SYNC 0x06
  269. #define STATE_DFU_MANIFEST 0x07
  270. #define STATE_DFU_MANIFEST_WAIT_RESET 0x08
  271. #define STATE_DFU_UPLOAD_IDLE 0x09
  272. #define STATE_DFU_ERROR 0x0a
  273. /* DFU commands */
  274. #define DFU_DETACH 0
  275. #define DFU_DNLOAD 1
  276. #define DFU_UPLOAD 2
  277. #define DFU_GETSTATUS 3
  278. #define DFU_CLRSTATUS 4
  279. #define DFU_GETSTATE 5
  280. #define DFU_ABORT 6
  281. #define FW_BLOCK_SIZE 1024
  282. struct dfu_status {
  283. unsigned char status;
  284. unsigned char poll_timeout[3];
  285. unsigned char state;
  286. unsigned char string;
  287. } __packed;
  288. static inline int at76_is_intersil(enum board_type board)
  289. {
  290. return (board == BOARD_503_ISL3861 || board == BOARD_503_ISL3863);
  291. }
  292. static inline int at76_is_503rfmd(enum board_type board)
  293. {
  294. return (board == BOARD_503 || board == BOARD_503_ACC);
  295. }
  296. static inline int at76_is_505a(enum board_type board)
  297. {
  298. return (board == BOARD_505A || board == BOARD_505AMX);
  299. }
  300. /* Load a block of the first (internal) part of the firmware */
  301. static int at76_load_int_fw_block(struct usb_device *udev, int blockno,
  302. void *block, int size)
  303. {
  304. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), DFU_DNLOAD,
  305. USB_TYPE_CLASS | USB_DIR_OUT |
  306. USB_RECIP_INTERFACE, blockno, 0, block, size,
  307. USB_CTRL_GET_TIMEOUT);
  308. }
  309. static int at76_dfu_get_status(struct usb_device *udev,
  310. struct dfu_status *status)
  311. {
  312. int ret;
  313. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATUS,
  314. USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
  315. 0, 0, status, sizeof(struct dfu_status),
  316. USB_CTRL_GET_TIMEOUT);
  317. return ret;
  318. }
  319. static int at76_dfu_get_state(struct usb_device *udev, u8 *state)
  320. {
  321. int ret;
  322. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), DFU_GETSTATE,
  323. USB_TYPE_CLASS | USB_DIR_IN | USB_RECIP_INTERFACE,
  324. 0, 0, state, 1, USB_CTRL_GET_TIMEOUT);
  325. return ret;
  326. }
  327. /* Convert timeout from the DFU status to jiffies */
  328. static inline unsigned long at76_get_timeout(struct dfu_status *s)
  329. {
  330. return msecs_to_jiffies((s->poll_timeout[2] << 16)
  331. | (s->poll_timeout[1] << 8)
  332. | (s->poll_timeout[0]));
  333. }
  334. /* Load internal firmware from the buffer. If manifest_sync_timeout > 0, use
  335. * its value in jiffies in the MANIFEST_SYNC state. */
  336. static int at76_usbdfu_download(struct usb_device *udev, u8 *buf, u32 size,
  337. int manifest_sync_timeout)
  338. {
  339. int ret = 0;
  340. int need_dfu_state = 1;
  341. int is_done = 0;
  342. u32 dfu_timeout = 0;
  343. int bsize = 0;
  344. int blockno = 0;
  345. struct dfu_status *dfu_stat_buf = NULL;
  346. u8 *dfu_state = NULL;
  347. u8 *block = NULL;
  348. at76_dbg(DBG_DFU, "%s( %p, %u, %d)", __func__, buf, size,
  349. manifest_sync_timeout);
  350. if (!size) {
  351. dev_err(&udev->dev, "FW buffer length invalid!\n");
  352. return -EINVAL;
  353. }
  354. dfu_stat_buf = kmalloc(sizeof(struct dfu_status), GFP_KERNEL);
  355. if (!dfu_stat_buf) {
  356. ret = -ENOMEM;
  357. goto exit;
  358. }
  359. block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
  360. if (!block) {
  361. ret = -ENOMEM;
  362. goto exit;
  363. }
  364. dfu_state = kmalloc(sizeof(u8), GFP_KERNEL);
  365. if (!dfu_state) {
  366. ret = -ENOMEM;
  367. goto exit;
  368. }
  369. *dfu_state = 0;
  370. do {
  371. if (need_dfu_state) {
  372. ret = at76_dfu_get_state(udev, dfu_state);
  373. if (ret < 0) {
  374. dev_err(&udev->dev,
  375. "cannot get DFU state: %d\n", ret);
  376. goto exit;
  377. }
  378. need_dfu_state = 0;
  379. }
  380. switch (*dfu_state) {
  381. case STATE_DFU_DOWNLOAD_SYNC:
  382. at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_SYNC");
  383. ret = at76_dfu_get_status(udev, dfu_stat_buf);
  384. if (ret >= 0) {
  385. *dfu_state = dfu_stat_buf->state;
  386. dfu_timeout = at76_get_timeout(dfu_stat_buf);
  387. need_dfu_state = 0;
  388. } else
  389. dev_err(&udev->dev,
  390. "at76_dfu_get_status returned %d\n",
  391. ret);
  392. break;
  393. case STATE_DFU_DOWNLOAD_BUSY:
  394. at76_dbg(DBG_DFU, "STATE_DFU_DOWNLOAD_BUSY");
  395. need_dfu_state = 1;
  396. at76_dbg(DBG_DFU, "DFU: Resetting device");
  397. schedule_timeout_interruptible(dfu_timeout);
  398. break;
  399. case STATE_DFU_DOWNLOAD_IDLE:
  400. at76_dbg(DBG_DFU, "DOWNLOAD...");
  401. /* fall through */
  402. case STATE_DFU_IDLE:
  403. at76_dbg(DBG_DFU, "DFU IDLE");
  404. bsize = min_t(int, size, FW_BLOCK_SIZE);
  405. memcpy(block, buf, bsize);
  406. at76_dbg(DBG_DFU, "int fw, size left = %5d, "
  407. "bsize = %4d, blockno = %2d", size, bsize,
  408. blockno);
  409. ret =
  410. at76_load_int_fw_block(udev, blockno, block, bsize);
  411. buf += bsize;
  412. size -= bsize;
  413. blockno++;
  414. if (ret != bsize)
  415. dev_err(&udev->dev,
  416. "at76_load_int_fw_block returned %d\n",
  417. ret);
  418. need_dfu_state = 1;
  419. break;
  420. case STATE_DFU_MANIFEST_SYNC:
  421. at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_SYNC");
  422. ret = at76_dfu_get_status(udev, dfu_stat_buf);
  423. if (ret < 0)
  424. break;
  425. *dfu_state = dfu_stat_buf->state;
  426. dfu_timeout = at76_get_timeout(dfu_stat_buf);
  427. need_dfu_state = 0;
  428. /* override the timeout from the status response,
  429. needed for AT76C505A */
  430. if (manifest_sync_timeout > 0)
  431. dfu_timeout = manifest_sync_timeout;
  432. at76_dbg(DBG_DFU, "DFU: Waiting for manifest phase");
  433. schedule_timeout_interruptible(dfu_timeout);
  434. break;
  435. case STATE_DFU_MANIFEST:
  436. at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST");
  437. is_done = 1;
  438. break;
  439. case STATE_DFU_MANIFEST_WAIT_RESET:
  440. at76_dbg(DBG_DFU, "STATE_DFU_MANIFEST_WAIT_RESET");
  441. is_done = 1;
  442. break;
  443. case STATE_DFU_UPLOAD_IDLE:
  444. at76_dbg(DBG_DFU, "STATE_DFU_UPLOAD_IDLE");
  445. break;
  446. case STATE_DFU_ERROR:
  447. at76_dbg(DBG_DFU, "STATE_DFU_ERROR");
  448. ret = -EPIPE;
  449. break;
  450. default:
  451. at76_dbg(DBG_DFU, "DFU UNKNOWN STATE (%d)", *dfu_state);
  452. ret = -EINVAL;
  453. break;
  454. }
  455. } while (!is_done && (ret >= 0));
  456. exit:
  457. kfree(dfu_state);
  458. kfree(block);
  459. kfree(dfu_stat_buf);
  460. if (ret >= 0)
  461. ret = 0;
  462. return ret;
  463. }
  464. /* LED trigger */
  465. static int tx_activity;
  466. static void at76_ledtrig_tx_timerfunc(struct timer_list *unused);
  467. static DEFINE_TIMER(ledtrig_tx_timer, at76_ledtrig_tx_timerfunc);
  468. DEFINE_LED_TRIGGER(ledtrig_tx);
  469. static void at76_ledtrig_tx_timerfunc(struct timer_list *unused)
  470. {
  471. static int tx_lastactivity;
  472. if (tx_lastactivity != tx_activity) {
  473. tx_lastactivity = tx_activity;
  474. led_trigger_event(ledtrig_tx, LED_FULL);
  475. mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
  476. } else
  477. led_trigger_event(ledtrig_tx, LED_OFF);
  478. }
  479. static void at76_ledtrig_tx_activity(void)
  480. {
  481. tx_activity++;
  482. if (!timer_pending(&ledtrig_tx_timer))
  483. mod_timer(&ledtrig_tx_timer, jiffies + HZ / 4);
  484. }
  485. static int at76_remap(struct usb_device *udev)
  486. {
  487. int ret;
  488. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0a,
  489. USB_TYPE_VENDOR | USB_DIR_OUT |
  490. USB_RECIP_INTERFACE, 0, 0, NULL, 0,
  491. USB_CTRL_GET_TIMEOUT);
  492. if (ret < 0)
  493. return ret;
  494. return 0;
  495. }
  496. static int at76_get_op_mode(struct usb_device *udev)
  497. {
  498. int ret;
  499. u8 saved;
  500. u8 *op_mode;
  501. op_mode = kmalloc(1, GFP_NOIO);
  502. if (!op_mode)
  503. return -ENOMEM;
  504. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
  505. USB_TYPE_VENDOR | USB_DIR_IN |
  506. USB_RECIP_INTERFACE, 0x01, 0, op_mode, 1,
  507. USB_CTRL_GET_TIMEOUT);
  508. saved = *op_mode;
  509. kfree(op_mode);
  510. if (ret < 0)
  511. return ret;
  512. else if (ret < 1)
  513. return -EIO;
  514. else
  515. return saved;
  516. }
  517. /* Load a block of the second ("external") part of the firmware */
  518. static inline int at76_load_ext_fw_block(struct usb_device *udev, int blockno,
  519. void *block, int size)
  520. {
  521. return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
  522. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  523. 0x0802, blockno, block, size,
  524. USB_CTRL_GET_TIMEOUT);
  525. }
  526. static inline int at76_get_hw_cfg(struct usb_device *udev,
  527. union at76_hwcfg *buf, int buf_size)
  528. {
  529. return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
  530. USB_TYPE_VENDOR | USB_DIR_IN |
  531. USB_RECIP_INTERFACE, 0x0a02, 0,
  532. buf, buf_size, USB_CTRL_GET_TIMEOUT);
  533. }
  534. /* Intersil boards use a different "value" for GetHWConfig requests */
  535. static inline int at76_get_hw_cfg_intersil(struct usb_device *udev,
  536. union at76_hwcfg *buf, int buf_size)
  537. {
  538. return usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
  539. USB_TYPE_VENDOR | USB_DIR_IN |
  540. USB_RECIP_INTERFACE, 0x0902, 0,
  541. buf, buf_size, USB_CTRL_GET_TIMEOUT);
  542. }
  543. /* Get the hardware configuration for the adapter and put it to the appropriate
  544. * fields of 'priv' (the GetHWConfig request and interpretation of the result
  545. * depends on the board type) */
  546. static int at76_get_hw_config(struct at76_priv *priv)
  547. {
  548. int ret;
  549. union at76_hwcfg *hwcfg = kmalloc(sizeof(*hwcfg), GFP_KERNEL);
  550. if (!hwcfg)
  551. return -ENOMEM;
  552. if (at76_is_intersil(priv->board_type)) {
  553. ret = at76_get_hw_cfg_intersil(priv->udev, hwcfg,
  554. sizeof(hwcfg->i));
  555. if (ret < 0)
  556. goto exit;
  557. memcpy(priv->mac_addr, hwcfg->i.mac_addr, ETH_ALEN);
  558. priv->regulatory_domain = hwcfg->i.regulatory_domain;
  559. } else if (at76_is_503rfmd(priv->board_type)) {
  560. ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r3));
  561. if (ret < 0)
  562. goto exit;
  563. memcpy(priv->mac_addr, hwcfg->r3.mac_addr, ETH_ALEN);
  564. priv->regulatory_domain = hwcfg->r3.regulatory_domain;
  565. } else {
  566. ret = at76_get_hw_cfg(priv->udev, hwcfg, sizeof(hwcfg->r5));
  567. if (ret < 0)
  568. goto exit;
  569. memcpy(priv->mac_addr, hwcfg->r5.mac_addr, ETH_ALEN);
  570. priv->regulatory_domain = hwcfg->r5.regulatory_domain;
  571. }
  572. exit:
  573. kfree(hwcfg);
  574. if (ret < 0)
  575. wiphy_err(priv->hw->wiphy, "cannot get HW Config (error %d)\n",
  576. ret);
  577. return ret;
  578. }
  579. static struct reg_domain const *at76_get_reg_domain(u16 code)
  580. {
  581. int i;
  582. static struct reg_domain const fd_tab[] = {
  583. { 0x10, "FCC (USA)", 0x7ff }, /* ch 1-11 */
  584. { 0x20, "IC (Canada)", 0x7ff }, /* ch 1-11 */
  585. { 0x30, "ETSI (most of Europe)", 0x1fff }, /* ch 1-13 */
  586. { 0x31, "Spain", 0x600 }, /* ch 10-11 */
  587. { 0x32, "France", 0x1e00 }, /* ch 10-13 */
  588. { 0x40, "MKK (Japan)", 0x2000 }, /* ch 14 */
  589. { 0x41, "MKK1 (Japan)", 0x3fff }, /* ch 1-14 */
  590. { 0x50, "Israel", 0x3fc }, /* ch 3-9 */
  591. { 0x00, "<unknown>", 0xffffffff } /* ch 1-32 */
  592. };
  593. /* Last entry is fallback for unknown domain code */
  594. for (i = 0; i < ARRAY_SIZE(fd_tab) - 1; i++)
  595. if (code == fd_tab[i].code)
  596. break;
  597. return &fd_tab[i];
  598. }
  599. static inline int at76_get_mib(struct usb_device *udev, u16 mib, void *buf,
  600. int buf_size)
  601. {
  602. int ret;
  603. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x33,
  604. USB_TYPE_VENDOR | USB_DIR_IN |
  605. USB_RECIP_INTERFACE, mib << 8, 0, buf, buf_size,
  606. USB_CTRL_GET_TIMEOUT);
  607. if (ret >= 0 && ret != buf_size)
  608. return -EIO;
  609. return ret;
  610. }
  611. /* Return positive number for status, negative for an error */
  612. static inline int at76_get_cmd_status(struct usb_device *udev, u8 cmd)
  613. {
  614. u8 *stat_buf;
  615. int ret;
  616. stat_buf = kmalloc(40, GFP_NOIO);
  617. if (!stat_buf)
  618. return -ENOMEM;
  619. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0), 0x22,
  620. USB_TYPE_VENDOR | USB_DIR_IN |
  621. USB_RECIP_INTERFACE, cmd, 0, stat_buf,
  622. 40, USB_CTRL_GET_TIMEOUT);
  623. if (ret >= 0)
  624. ret = stat_buf[5];
  625. kfree(stat_buf);
  626. return ret;
  627. }
  628. #define MAKE_CMD_CASE(c) case (c): return #c
  629. static const char *at76_get_cmd_string(u8 cmd_status)
  630. {
  631. switch (cmd_status) {
  632. MAKE_CMD_CASE(CMD_SET_MIB);
  633. MAKE_CMD_CASE(CMD_GET_MIB);
  634. MAKE_CMD_CASE(CMD_SCAN);
  635. MAKE_CMD_CASE(CMD_JOIN);
  636. MAKE_CMD_CASE(CMD_START_IBSS);
  637. MAKE_CMD_CASE(CMD_RADIO_ON);
  638. MAKE_CMD_CASE(CMD_RADIO_OFF);
  639. MAKE_CMD_CASE(CMD_STARTUP);
  640. }
  641. return "UNKNOWN";
  642. }
  643. static int at76_set_card_command(struct usb_device *udev, u8 cmd, void *buf,
  644. int buf_size)
  645. {
  646. int ret;
  647. struct at76_command *cmd_buf = kmalloc(sizeof(struct at76_command) +
  648. buf_size, GFP_KERNEL);
  649. if (!cmd_buf)
  650. return -ENOMEM;
  651. cmd_buf->cmd = cmd;
  652. cmd_buf->reserved = 0;
  653. cmd_buf->size = cpu_to_le16(buf_size);
  654. memcpy(cmd_buf->data, buf, buf_size);
  655. at76_dbg_dump(DBG_CMD, cmd_buf, sizeof(struct at76_command) + buf_size,
  656. "issuing command %s (0x%02x)",
  657. at76_get_cmd_string(cmd), cmd);
  658. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x0e,
  659. USB_TYPE_VENDOR | USB_DIR_OUT | USB_RECIP_DEVICE,
  660. 0, 0, cmd_buf,
  661. sizeof(struct at76_command) + buf_size,
  662. USB_CTRL_GET_TIMEOUT);
  663. kfree(cmd_buf);
  664. return ret;
  665. }
  666. #define MAKE_CMD_STATUS_CASE(c) case (c): return #c
  667. static const char *at76_get_cmd_status_string(u8 cmd_status)
  668. {
  669. switch (cmd_status) {
  670. MAKE_CMD_STATUS_CASE(CMD_STATUS_IDLE);
  671. MAKE_CMD_STATUS_CASE(CMD_STATUS_COMPLETE);
  672. MAKE_CMD_STATUS_CASE(CMD_STATUS_UNKNOWN);
  673. MAKE_CMD_STATUS_CASE(CMD_STATUS_INVALID_PARAMETER);
  674. MAKE_CMD_STATUS_CASE(CMD_STATUS_FUNCTION_NOT_SUPPORTED);
  675. MAKE_CMD_STATUS_CASE(CMD_STATUS_TIME_OUT);
  676. MAKE_CMD_STATUS_CASE(CMD_STATUS_IN_PROGRESS);
  677. MAKE_CMD_STATUS_CASE(CMD_STATUS_HOST_FAILURE);
  678. MAKE_CMD_STATUS_CASE(CMD_STATUS_SCAN_FAILED);
  679. }
  680. return "UNKNOWN";
  681. }
  682. /* Wait until the command is completed */
  683. static int at76_wait_completion(struct at76_priv *priv, int cmd)
  684. {
  685. int status = 0;
  686. unsigned long timeout = jiffies + CMD_COMPLETION_TIMEOUT;
  687. do {
  688. status = at76_get_cmd_status(priv->udev, cmd);
  689. if (status < 0) {
  690. wiphy_err(priv->hw->wiphy,
  691. "at76_get_cmd_status failed: %d\n",
  692. status);
  693. break;
  694. }
  695. at76_dbg(DBG_WAIT_COMPLETE,
  696. "%s: Waiting on cmd %d, status = %d (%s)",
  697. wiphy_name(priv->hw->wiphy), cmd, status,
  698. at76_get_cmd_status_string(status));
  699. if (status != CMD_STATUS_IN_PROGRESS
  700. && status != CMD_STATUS_IDLE)
  701. break;
  702. schedule_timeout_interruptible(HZ / 10); /* 100 ms */
  703. if (time_after(jiffies, timeout)) {
  704. wiphy_err(priv->hw->wiphy,
  705. "completion timeout for command %d\n", cmd);
  706. status = -ETIMEDOUT;
  707. break;
  708. }
  709. } while (1);
  710. return status;
  711. }
  712. static int at76_set_mib(struct at76_priv *priv, struct set_mib_buffer *buf)
  713. {
  714. int ret;
  715. ret = at76_set_card_command(priv->udev, CMD_SET_MIB, buf,
  716. offsetof(struct set_mib_buffer,
  717. data) + buf->size);
  718. if (ret < 0)
  719. return ret;
  720. ret = at76_wait_completion(priv, CMD_SET_MIB);
  721. if (ret != CMD_STATUS_COMPLETE) {
  722. wiphy_info(priv->hw->wiphy,
  723. "set_mib: at76_wait_completion failed with %d\n",
  724. ret);
  725. ret = -EIO;
  726. }
  727. return ret;
  728. }
  729. /* Return < 0 on error, == 0 if no command sent, == 1 if cmd sent */
  730. static int at76_set_radio(struct at76_priv *priv, int enable)
  731. {
  732. int ret;
  733. int cmd;
  734. if (priv->radio_on == enable)
  735. return 0;
  736. cmd = enable ? CMD_RADIO_ON : CMD_RADIO_OFF;
  737. ret = at76_set_card_command(priv->udev, cmd, NULL, 0);
  738. if (ret < 0)
  739. wiphy_err(priv->hw->wiphy,
  740. "at76_set_card_command(%d) failed: %d\n", cmd, ret);
  741. else
  742. ret = 1;
  743. priv->radio_on = enable;
  744. return ret;
  745. }
  746. /* Set current power save mode (AT76_PM_OFF/AT76_PM_ON/AT76_PM_SMART) */
  747. static int at76_set_pm_mode(struct at76_priv *priv)
  748. {
  749. int ret = 0;
  750. priv->mib_buf.type = MIB_MAC_MGMT;
  751. priv->mib_buf.size = 1;
  752. priv->mib_buf.index = offsetof(struct mib_mac_mgmt, power_mgmt_mode);
  753. priv->mib_buf.data.byte = priv->pm_mode;
  754. ret = at76_set_mib(priv, &priv->mib_buf);
  755. if (ret < 0)
  756. wiphy_err(priv->hw->wiphy, "set_mib (pm_mode) failed: %d\n",
  757. ret);
  758. return ret;
  759. }
  760. static int at76_set_preamble(struct at76_priv *priv, u8 type)
  761. {
  762. int ret = 0;
  763. priv->mib_buf.type = MIB_LOCAL;
  764. priv->mib_buf.size = 1;
  765. priv->mib_buf.index = offsetof(struct mib_local, preamble_type);
  766. priv->mib_buf.data.byte = type;
  767. ret = at76_set_mib(priv, &priv->mib_buf);
  768. if (ret < 0)
  769. wiphy_err(priv->hw->wiphy, "set_mib (preamble) failed: %d\n",
  770. ret);
  771. return ret;
  772. }
  773. static int at76_set_frag(struct at76_priv *priv, u16 size)
  774. {
  775. int ret = 0;
  776. priv->mib_buf.type = MIB_MAC;
  777. priv->mib_buf.size = 2;
  778. priv->mib_buf.index = offsetof(struct mib_mac, frag_threshold);
  779. priv->mib_buf.data.word = cpu_to_le16(size);
  780. ret = at76_set_mib(priv, &priv->mib_buf);
  781. if (ret < 0)
  782. wiphy_err(priv->hw->wiphy,
  783. "set_mib (frag threshold) failed: %d\n", ret);
  784. return ret;
  785. }
  786. static int at76_set_rts(struct at76_priv *priv, u16 size)
  787. {
  788. int ret = 0;
  789. priv->mib_buf.type = MIB_MAC;
  790. priv->mib_buf.size = 2;
  791. priv->mib_buf.index = offsetof(struct mib_mac, rts_threshold);
  792. priv->mib_buf.data.word = cpu_to_le16(size);
  793. ret = at76_set_mib(priv, &priv->mib_buf);
  794. if (ret < 0)
  795. wiphy_err(priv->hw->wiphy, "set_mib (rts) failed: %d\n", ret);
  796. return ret;
  797. }
  798. static int at76_set_autorate_fallback(struct at76_priv *priv, int onoff)
  799. {
  800. int ret = 0;
  801. priv->mib_buf.type = MIB_LOCAL;
  802. priv->mib_buf.size = 1;
  803. priv->mib_buf.index = offsetof(struct mib_local, txautorate_fallback);
  804. priv->mib_buf.data.byte = onoff;
  805. ret = at76_set_mib(priv, &priv->mib_buf);
  806. if (ret < 0)
  807. wiphy_err(priv->hw->wiphy,
  808. "set_mib (autorate fallback) failed: %d\n", ret);
  809. return ret;
  810. }
  811. static void at76_dump_mib_mac_addr(struct at76_priv *priv)
  812. {
  813. int i;
  814. int ret;
  815. struct mib_mac_addr *m = kmalloc(sizeof(struct mib_mac_addr),
  816. GFP_KERNEL);
  817. if (!m)
  818. return;
  819. ret = at76_get_mib(priv->udev, MIB_MAC_ADDR, m,
  820. sizeof(struct mib_mac_addr));
  821. if (ret < 0) {
  822. wiphy_err(priv->hw->wiphy,
  823. "at76_get_mib (MAC_ADDR) failed: %d\n", ret);
  824. goto exit;
  825. }
  826. at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: mac_addr %pM res 0x%x 0x%x",
  827. wiphy_name(priv->hw->wiphy),
  828. m->mac_addr, m->res[0], m->res[1]);
  829. for (i = 0; i < ARRAY_SIZE(m->group_addr); i++)
  830. at76_dbg(DBG_MIB, "%s: MIB MAC_ADDR: group addr %d: %pM, "
  831. "status %d", wiphy_name(priv->hw->wiphy), i,
  832. m->group_addr[i], m->group_addr_status[i]);
  833. exit:
  834. kfree(m);
  835. }
  836. static void at76_dump_mib_mac_wep(struct at76_priv *priv)
  837. {
  838. int i;
  839. int ret;
  840. int key_len;
  841. struct mib_mac_wep *m = kmalloc(sizeof(struct mib_mac_wep), GFP_KERNEL);
  842. if (!m)
  843. return;
  844. ret = at76_get_mib(priv->udev, MIB_MAC_WEP, m,
  845. sizeof(struct mib_mac_wep));
  846. if (ret < 0) {
  847. wiphy_err(priv->hw->wiphy,
  848. "at76_get_mib (MAC_WEP) failed: %d\n", ret);
  849. goto exit;
  850. }
  851. at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: priv_invoked %u def_key_id %u "
  852. "key_len %u excl_unencr %u wep_icv_err %u wep_excluded %u "
  853. "encr_level %u key %d", wiphy_name(priv->hw->wiphy),
  854. m->privacy_invoked, m->wep_default_key_id,
  855. m->wep_key_mapping_len, m->exclude_unencrypted,
  856. le32_to_cpu(m->wep_icv_error_count),
  857. le32_to_cpu(m->wep_excluded_count), m->encryption_level,
  858. m->wep_default_key_id);
  859. key_len = (m->encryption_level == 1) ?
  860. WEP_SMALL_KEY_LEN : WEP_LARGE_KEY_LEN;
  861. for (i = 0; i < WEP_KEYS; i++)
  862. at76_dbg(DBG_MIB, "%s: MIB MAC_WEP: key %d: %*phD",
  863. wiphy_name(priv->hw->wiphy), i,
  864. key_len, m->wep_default_keyvalue[i]);
  865. exit:
  866. kfree(m);
  867. }
  868. static void at76_dump_mib_mac_mgmt(struct at76_priv *priv)
  869. {
  870. int ret;
  871. struct mib_mac_mgmt *m = kmalloc(sizeof(struct mib_mac_mgmt),
  872. GFP_KERNEL);
  873. if (!m)
  874. return;
  875. ret = at76_get_mib(priv->udev, MIB_MAC_MGMT, m,
  876. sizeof(struct mib_mac_mgmt));
  877. if (ret < 0) {
  878. wiphy_err(priv->hw->wiphy,
  879. "at76_get_mib (MAC_MGMT) failed: %d\n", ret);
  880. goto exit;
  881. }
  882. at76_dbg(DBG_MIB, "%s: MIB MAC_MGMT: beacon_period %d CFP_max_duration "
  883. "%d medium_occupancy_limit %d station_id 0x%x ATIM_window %d "
  884. "CFP_mode %d privacy_opt_impl %d DTIM_period %d CFP_period %d "
  885. "current_bssid %pM current_essid %*phD current_bss_type %d "
  886. "pm_mode %d ibss_change %d res %d "
  887. "multi_domain_capability_implemented %d "
  888. "international_roaming %d country_string %.3s",
  889. wiphy_name(priv->hw->wiphy), le16_to_cpu(m->beacon_period),
  890. le16_to_cpu(m->CFP_max_duration),
  891. le16_to_cpu(m->medium_occupancy_limit),
  892. le16_to_cpu(m->station_id), le16_to_cpu(m->ATIM_window),
  893. m->CFP_mode, m->privacy_option_implemented, m->DTIM_period,
  894. m->CFP_period, m->current_bssid,
  895. IW_ESSID_MAX_SIZE, m->current_essid,
  896. m->current_bss_type, m->power_mgmt_mode, m->ibss_change,
  897. m->res, m->multi_domain_capability_implemented,
  898. m->multi_domain_capability_enabled, m->country_string);
  899. exit:
  900. kfree(m);
  901. }
  902. static void at76_dump_mib_mac(struct at76_priv *priv)
  903. {
  904. int ret;
  905. struct mib_mac *m = kmalloc(sizeof(struct mib_mac), GFP_KERNEL);
  906. if (!m)
  907. return;
  908. ret = at76_get_mib(priv->udev, MIB_MAC, m, sizeof(struct mib_mac));
  909. if (ret < 0) {
  910. wiphy_err(priv->hw->wiphy,
  911. "at76_get_mib (MAC) failed: %d\n", ret);
  912. goto exit;
  913. }
  914. at76_dbg(DBG_MIB, "%s: MIB MAC: max_tx_msdu_lifetime %d "
  915. "max_rx_lifetime %d frag_threshold %d rts_threshold %d "
  916. "cwmin %d cwmax %d short_retry_time %d long_retry_time %d "
  917. "scan_type %d scan_channel %d probe_delay %u "
  918. "min_channel_time %d max_channel_time %d listen_int %d "
  919. "desired_ssid %*phD desired_bssid %pM desired_bsstype %d",
  920. wiphy_name(priv->hw->wiphy),
  921. le32_to_cpu(m->max_tx_msdu_lifetime),
  922. le32_to_cpu(m->max_rx_lifetime),
  923. le16_to_cpu(m->frag_threshold), le16_to_cpu(m->rts_threshold),
  924. le16_to_cpu(m->cwmin), le16_to_cpu(m->cwmax),
  925. m->short_retry_time, m->long_retry_time, m->scan_type,
  926. m->scan_channel, le16_to_cpu(m->probe_delay),
  927. le16_to_cpu(m->min_channel_time),
  928. le16_to_cpu(m->max_channel_time),
  929. le16_to_cpu(m->listen_interval),
  930. IW_ESSID_MAX_SIZE, m->desired_ssid,
  931. m->desired_bssid, m->desired_bsstype);
  932. exit:
  933. kfree(m);
  934. }
  935. static void at76_dump_mib_phy(struct at76_priv *priv)
  936. {
  937. int ret;
  938. struct mib_phy *m = kmalloc(sizeof(struct mib_phy), GFP_KERNEL);
  939. if (!m)
  940. return;
  941. ret = at76_get_mib(priv->udev, MIB_PHY, m, sizeof(struct mib_phy));
  942. if (ret < 0) {
  943. wiphy_err(priv->hw->wiphy,
  944. "at76_get_mib (PHY) failed: %d\n", ret);
  945. goto exit;
  946. }
  947. at76_dbg(DBG_MIB, "%s: MIB PHY: ed_threshold %d slot_time %d "
  948. "sifs_time %d preamble_length %d plcp_header_length %d "
  949. "mpdu_max_length %d cca_mode_supported %d operation_rate_set "
  950. "0x%x 0x%x 0x%x 0x%x channel_id %d current_cca_mode %d "
  951. "phy_type %d current_reg_domain %d",
  952. wiphy_name(priv->hw->wiphy), le32_to_cpu(m->ed_threshold),
  953. le16_to_cpu(m->slot_time), le16_to_cpu(m->sifs_time),
  954. le16_to_cpu(m->preamble_length),
  955. le16_to_cpu(m->plcp_header_length),
  956. le16_to_cpu(m->mpdu_max_length),
  957. le16_to_cpu(m->cca_mode_supported), m->operation_rate_set[0],
  958. m->operation_rate_set[1], m->operation_rate_set[2],
  959. m->operation_rate_set[3], m->channel_id, m->current_cca_mode,
  960. m->phy_type, m->current_reg_domain);
  961. exit:
  962. kfree(m);
  963. }
  964. static void at76_dump_mib_local(struct at76_priv *priv)
  965. {
  966. int ret;
  967. struct mib_local *m = kmalloc(sizeof(*m), GFP_KERNEL);
  968. if (!m)
  969. return;
  970. ret = at76_get_mib(priv->udev, MIB_LOCAL, m, sizeof(*m));
  971. if (ret < 0) {
  972. wiphy_err(priv->hw->wiphy,
  973. "at76_get_mib (LOCAL) failed: %d\n", ret);
  974. goto exit;
  975. }
  976. at76_dbg(DBG_MIB, "%s: MIB LOCAL: beacon_enable %d "
  977. "txautorate_fallback %d ssid_size %d promiscuous_mode %d "
  978. "preamble_type %d", wiphy_name(priv->hw->wiphy),
  979. m->beacon_enable,
  980. m->txautorate_fallback, m->ssid_size, m->promiscuous_mode,
  981. m->preamble_type);
  982. exit:
  983. kfree(m);
  984. }
  985. static void at76_dump_mib_mdomain(struct at76_priv *priv)
  986. {
  987. int ret;
  988. struct mib_mdomain *m = kmalloc(sizeof(struct mib_mdomain), GFP_KERNEL);
  989. if (!m)
  990. return;
  991. ret = at76_get_mib(priv->udev, MIB_MDOMAIN, m,
  992. sizeof(struct mib_mdomain));
  993. if (ret < 0) {
  994. wiphy_err(priv->hw->wiphy,
  995. "at76_get_mib (MDOMAIN) failed: %d\n", ret);
  996. goto exit;
  997. }
  998. at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: channel_list %*phD",
  999. wiphy_name(priv->hw->wiphy),
  1000. (int)sizeof(m->channel_list), m->channel_list);
  1001. at76_dbg(DBG_MIB, "%s: MIB MDOMAIN: tx_powerlevel %*phD",
  1002. wiphy_name(priv->hw->wiphy),
  1003. (int)sizeof(m->tx_powerlevel), m->tx_powerlevel);
  1004. exit:
  1005. kfree(m);
  1006. }
  1007. /* Enable monitor mode */
  1008. static int at76_start_monitor(struct at76_priv *priv)
  1009. {
  1010. struct at76_req_scan scan;
  1011. int ret;
  1012. memset(&scan, 0, sizeof(struct at76_req_scan));
  1013. eth_broadcast_addr(scan.bssid);
  1014. scan.channel = priv->channel;
  1015. scan.scan_type = SCAN_TYPE_PASSIVE;
  1016. scan.international_scan = 0;
  1017. scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
  1018. scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
  1019. scan.probe_delay = cpu_to_le16(0);
  1020. ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
  1021. if (ret >= 0)
  1022. ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
  1023. return ret;
  1024. }
  1025. /* Calculate padding from txbuf->wlength (which excludes the USB TX header),
  1026. likely to compensate a flaw in the AT76C503A USB part ... */
  1027. static inline int at76_calc_padding(int wlen)
  1028. {
  1029. /* add the USB TX header */
  1030. wlen += AT76_TX_HDRLEN;
  1031. wlen = wlen % 64;
  1032. if (wlen < 50)
  1033. return 50 - wlen;
  1034. if (wlen >= 61)
  1035. return 64 + 50 - wlen;
  1036. return 0;
  1037. }
  1038. static void at76_rx_callback(struct urb *urb)
  1039. {
  1040. struct at76_priv *priv = urb->context;
  1041. priv->rx_tasklet.data = (unsigned long)urb;
  1042. tasklet_schedule(&priv->rx_tasklet);
  1043. }
  1044. static int at76_submit_rx_urb(struct at76_priv *priv)
  1045. {
  1046. int ret;
  1047. int size;
  1048. struct sk_buff *skb = priv->rx_skb;
  1049. if (!priv->rx_urb) {
  1050. wiphy_err(priv->hw->wiphy, "%s: priv->rx_urb is NULL\n",
  1051. __func__);
  1052. return -EFAULT;
  1053. }
  1054. if (!skb) {
  1055. skb = dev_alloc_skb(sizeof(struct at76_rx_buffer));
  1056. if (!skb) {
  1057. wiphy_err(priv->hw->wiphy,
  1058. "cannot allocate rx skbuff\n");
  1059. ret = -ENOMEM;
  1060. goto exit;
  1061. }
  1062. priv->rx_skb = skb;
  1063. } else {
  1064. skb_push(skb, skb_headroom(skb));
  1065. skb_trim(skb, 0);
  1066. }
  1067. size = skb_tailroom(skb);
  1068. usb_fill_bulk_urb(priv->rx_urb, priv->udev, priv->rx_pipe,
  1069. skb_put(skb, size), size, at76_rx_callback, priv);
  1070. ret = usb_submit_urb(priv->rx_urb, GFP_ATOMIC);
  1071. if (ret < 0) {
  1072. if (ret == -ENODEV)
  1073. at76_dbg(DBG_DEVSTART,
  1074. "usb_submit_urb returned -ENODEV");
  1075. else
  1076. wiphy_err(priv->hw->wiphy,
  1077. "rx, usb_submit_urb failed: %d\n", ret);
  1078. }
  1079. exit:
  1080. if (ret < 0 && ret != -ENODEV)
  1081. wiphy_err(priv->hw->wiphy,
  1082. "cannot submit rx urb - please unload the driver and/or power cycle the device\n");
  1083. return ret;
  1084. }
  1085. /* Download external firmware */
  1086. static int at76_load_external_fw(struct usb_device *udev, struct fwentry *fwe)
  1087. {
  1088. int ret;
  1089. int op_mode;
  1090. int blockno = 0;
  1091. int bsize;
  1092. u8 *block;
  1093. u8 *buf = fwe->extfw;
  1094. int size = fwe->extfw_size;
  1095. if (!buf || !size)
  1096. return -ENOENT;
  1097. op_mode = at76_get_op_mode(udev);
  1098. at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
  1099. if (op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
  1100. dev_err(&udev->dev, "unexpected opmode %d\n", op_mode);
  1101. return -EINVAL;
  1102. }
  1103. block = kmalloc(FW_BLOCK_SIZE, GFP_KERNEL);
  1104. if (!block)
  1105. return -ENOMEM;
  1106. at76_dbg(DBG_DEVSTART, "downloading external firmware");
  1107. /* for fw >= 0.100, the device needs an extra empty block */
  1108. do {
  1109. bsize = min_t(int, size, FW_BLOCK_SIZE);
  1110. memcpy(block, buf, bsize);
  1111. at76_dbg(DBG_DEVSTART,
  1112. "ext fw, size left = %5d, bsize = %4d, blockno = %2d",
  1113. size, bsize, blockno);
  1114. ret = at76_load_ext_fw_block(udev, blockno, block, bsize);
  1115. if (ret != bsize) {
  1116. dev_err(&udev->dev,
  1117. "loading %dth firmware block failed: %d\n",
  1118. blockno, ret);
  1119. ret = -EIO;
  1120. goto exit;
  1121. }
  1122. buf += bsize;
  1123. size -= bsize;
  1124. blockno++;
  1125. } while (bsize > 0);
  1126. if (at76_is_505a(fwe->board_type)) {
  1127. at76_dbg(DBG_DEVSTART, "200 ms delay for 505a");
  1128. schedule_timeout_interruptible(HZ / 5 + 1);
  1129. }
  1130. exit:
  1131. kfree(block);
  1132. if (ret < 0)
  1133. dev_err(&udev->dev,
  1134. "downloading external firmware failed: %d\n", ret);
  1135. return ret;
  1136. }
  1137. /* Download internal firmware */
  1138. static int at76_load_internal_fw(struct usb_device *udev, struct fwentry *fwe)
  1139. {
  1140. int ret;
  1141. int need_remap = !at76_is_505a(fwe->board_type);
  1142. ret = at76_usbdfu_download(udev, fwe->intfw, fwe->intfw_size,
  1143. need_remap ? 0 : 2 * HZ);
  1144. if (ret < 0) {
  1145. dev_err(&udev->dev,
  1146. "downloading internal fw failed with %d\n", ret);
  1147. goto exit;
  1148. }
  1149. at76_dbg(DBG_DEVSTART, "sending REMAP");
  1150. /* no REMAP for 505A (see SF driver) */
  1151. if (need_remap) {
  1152. ret = at76_remap(udev);
  1153. if (ret < 0) {
  1154. dev_err(&udev->dev,
  1155. "sending REMAP failed with %d\n", ret);
  1156. goto exit;
  1157. }
  1158. }
  1159. at76_dbg(DBG_DEVSTART, "sleeping for 2 seconds");
  1160. schedule_timeout_interruptible(2 * HZ + 1);
  1161. usb_reset_device(udev);
  1162. exit:
  1163. return ret;
  1164. }
  1165. static int at76_startup_device(struct at76_priv *priv)
  1166. {
  1167. struct at76_card_config *ccfg = &priv->card_config;
  1168. int ret;
  1169. at76_dbg(DBG_PARAMS,
  1170. "%s param: ssid %.*s (%*phD) mode %s ch %d wep %s key %d "
  1171. "keylen %d", wiphy_name(priv->hw->wiphy), priv->essid_size,
  1172. priv->essid, IW_ESSID_MAX_SIZE, priv->essid,
  1173. priv->iw_mode == IW_MODE_ADHOC ? "adhoc" : "infra",
  1174. priv->channel, priv->wep_enabled ? "enabled" : "disabled",
  1175. priv->wep_key_id, priv->wep_keys_len[priv->wep_key_id]);
  1176. at76_dbg(DBG_PARAMS,
  1177. "%s param: preamble %s rts %d retry %d frag %d "
  1178. "txrate %s auth_mode %d", wiphy_name(priv->hw->wiphy),
  1179. preambles[priv->preamble_type], priv->rts_threshold,
  1180. priv->short_retry_limit, priv->frag_threshold,
  1181. priv->txrate == TX_RATE_1MBIT ? "1MBit" : priv->txrate ==
  1182. TX_RATE_2MBIT ? "2MBit" : priv->txrate ==
  1183. TX_RATE_5_5MBIT ? "5.5MBit" : priv->txrate ==
  1184. TX_RATE_11MBIT ? "11MBit" : priv->txrate ==
  1185. TX_RATE_AUTO ? "auto" : "<invalid>", priv->auth_mode);
  1186. at76_dbg(DBG_PARAMS,
  1187. "%s param: pm_mode %d pm_period %d auth_mode %s "
  1188. "scan_times %d %d scan_mode %s",
  1189. wiphy_name(priv->hw->wiphy), priv->pm_mode, priv->pm_period,
  1190. priv->auth_mode == WLAN_AUTH_OPEN ? "open" : "shared_secret",
  1191. priv->scan_min_time, priv->scan_max_time,
  1192. priv->scan_mode == SCAN_TYPE_ACTIVE ? "active" : "passive");
  1193. memset(ccfg, 0, sizeof(struct at76_card_config));
  1194. ccfg->promiscuous_mode = 0;
  1195. ccfg->short_retry_limit = priv->short_retry_limit;
  1196. if (priv->wep_enabled) {
  1197. if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
  1198. ccfg->encryption_type = 2;
  1199. else
  1200. ccfg->encryption_type = 1;
  1201. /* jal: always exclude unencrypted if WEP is active */
  1202. ccfg->exclude_unencrypted = 1;
  1203. } else {
  1204. ccfg->exclude_unencrypted = 0;
  1205. ccfg->encryption_type = 0;
  1206. }
  1207. ccfg->rts_threshold = cpu_to_le16(priv->rts_threshold);
  1208. ccfg->fragmentation_threshold = cpu_to_le16(priv->frag_threshold);
  1209. memcpy(ccfg->basic_rate_set, hw_rates, 4);
  1210. /* jal: really needed, we do a set_mib for autorate later ??? */
  1211. ccfg->auto_rate_fallback = (priv->txrate == TX_RATE_AUTO ? 1 : 0);
  1212. ccfg->channel = priv->channel;
  1213. ccfg->privacy_invoked = priv->wep_enabled;
  1214. memcpy(ccfg->current_ssid, priv->essid, IW_ESSID_MAX_SIZE);
  1215. ccfg->ssid_len = priv->essid_size;
  1216. ccfg->wep_default_key_id = priv->wep_key_id;
  1217. memcpy(ccfg->wep_default_key_value, priv->wep_keys,
  1218. sizeof(priv->wep_keys));
  1219. ccfg->short_preamble = priv->preamble_type;
  1220. ccfg->beacon_period = cpu_to_le16(priv->beacon_period);
  1221. ret = at76_set_card_command(priv->udev, CMD_STARTUP, &priv->card_config,
  1222. sizeof(struct at76_card_config));
  1223. if (ret < 0) {
  1224. wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
  1225. ret);
  1226. return ret;
  1227. }
  1228. at76_wait_completion(priv, CMD_STARTUP);
  1229. /* remove BSSID from previous run */
  1230. eth_zero_addr(priv->bssid);
  1231. priv->scanning = false;
  1232. if (at76_set_radio(priv, 1) == 1)
  1233. at76_wait_completion(priv, CMD_RADIO_ON);
  1234. ret = at76_set_preamble(priv, priv->preamble_type);
  1235. if (ret < 0)
  1236. return ret;
  1237. ret = at76_set_frag(priv, priv->frag_threshold);
  1238. if (ret < 0)
  1239. return ret;
  1240. ret = at76_set_rts(priv, priv->rts_threshold);
  1241. if (ret < 0)
  1242. return ret;
  1243. ret = at76_set_autorate_fallback(priv,
  1244. priv->txrate == TX_RATE_AUTO ? 1 : 0);
  1245. if (ret < 0)
  1246. return ret;
  1247. ret = at76_set_pm_mode(priv);
  1248. if (ret < 0)
  1249. return ret;
  1250. if (at76_debug & DBG_MIB) {
  1251. at76_dump_mib_mac(priv);
  1252. at76_dump_mib_mac_addr(priv);
  1253. at76_dump_mib_mac_mgmt(priv);
  1254. at76_dump_mib_mac_wep(priv);
  1255. at76_dump_mib_mdomain(priv);
  1256. at76_dump_mib_phy(priv);
  1257. at76_dump_mib_local(priv);
  1258. }
  1259. return 0;
  1260. }
  1261. /* Enable or disable promiscuous mode */
  1262. static void at76_work_set_promisc(struct work_struct *work)
  1263. {
  1264. struct at76_priv *priv = container_of(work, struct at76_priv,
  1265. work_set_promisc);
  1266. int ret = 0;
  1267. if (priv->device_unplugged)
  1268. return;
  1269. mutex_lock(&priv->mtx);
  1270. priv->mib_buf.type = MIB_LOCAL;
  1271. priv->mib_buf.size = 1;
  1272. priv->mib_buf.index = offsetof(struct mib_local, promiscuous_mode);
  1273. priv->mib_buf.data.byte = priv->promisc ? 1 : 0;
  1274. ret = at76_set_mib(priv, &priv->mib_buf);
  1275. if (ret < 0)
  1276. wiphy_err(priv->hw->wiphy,
  1277. "set_mib (promiscuous_mode) failed: %d\n", ret);
  1278. mutex_unlock(&priv->mtx);
  1279. }
  1280. /* Submit Rx urb back to the device */
  1281. static void at76_work_submit_rx(struct work_struct *work)
  1282. {
  1283. struct at76_priv *priv = container_of(work, struct at76_priv,
  1284. work_submit_rx);
  1285. mutex_lock(&priv->mtx);
  1286. at76_submit_rx_urb(priv);
  1287. mutex_unlock(&priv->mtx);
  1288. }
  1289. /* This is a workaround to make scan working:
  1290. * currently mac80211 does not process frames with no frequency
  1291. * information.
  1292. * However during scan the HW performs a sweep by itself, and we
  1293. * are unable to know where the radio is actually tuned.
  1294. * This function tries to do its best to guess this information..
  1295. * During scan, If the current frame is a beacon or a probe response,
  1296. * the channel information is extracted from it.
  1297. * When not scanning, for other frames, or if it happens that for
  1298. * whatever reason we fail to parse beacons and probe responses, this
  1299. * function returns the priv->channel information, that should be correct
  1300. * at least when we are not scanning.
  1301. */
  1302. static inline int at76_guess_freq(struct at76_priv *priv)
  1303. {
  1304. size_t el_off;
  1305. const u8 *el;
  1306. int channel = priv->channel;
  1307. int len = priv->rx_skb->len;
  1308. struct ieee80211_hdr *hdr = (void *)priv->rx_skb->data;
  1309. if (!priv->scanning)
  1310. goto exit;
  1311. if (len < 24)
  1312. goto exit;
  1313. if (ieee80211_is_probe_resp(hdr->frame_control)) {
  1314. el_off = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  1315. el = ((struct ieee80211_mgmt *)hdr)->u.probe_resp.variable;
  1316. } else if (ieee80211_is_beacon(hdr->frame_control)) {
  1317. el_off = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  1318. el = ((struct ieee80211_mgmt *)hdr)->u.beacon.variable;
  1319. } else {
  1320. goto exit;
  1321. }
  1322. len -= el_off;
  1323. el = cfg80211_find_ie(WLAN_EID_DS_PARAMS, el, len);
  1324. if (el && el[1] > 0)
  1325. channel = el[2];
  1326. exit:
  1327. return ieee80211_channel_to_frequency(channel, NL80211_BAND_2GHZ);
  1328. }
  1329. static void at76_rx_tasklet(unsigned long param)
  1330. {
  1331. struct urb *urb = (struct urb *)param;
  1332. struct at76_priv *priv = urb->context;
  1333. struct at76_rx_buffer *buf;
  1334. struct ieee80211_rx_status rx_status = { 0 };
  1335. if (priv->device_unplugged) {
  1336. at76_dbg(DBG_DEVSTART, "device unplugged");
  1337. at76_dbg(DBG_DEVSTART, "urb status %d", urb->status);
  1338. return;
  1339. }
  1340. if (!priv->rx_skb || !priv->rx_skb->data)
  1341. return;
  1342. buf = (struct at76_rx_buffer *)priv->rx_skb->data;
  1343. if (urb->status != 0) {
  1344. if (urb->status != -ENOENT && urb->status != -ECONNRESET)
  1345. at76_dbg(DBG_URB,
  1346. "%s %s: - nonzero Rx bulk status received: %d",
  1347. __func__, wiphy_name(priv->hw->wiphy),
  1348. urb->status);
  1349. return;
  1350. }
  1351. at76_dbg(DBG_RX_ATMEL_HDR,
  1352. "%s: rx frame: rate %d rssi %d noise %d link %d",
  1353. wiphy_name(priv->hw->wiphy), buf->rx_rate, buf->rssi,
  1354. buf->noise_level, buf->link_quality);
  1355. skb_pull(priv->rx_skb, AT76_RX_HDRLEN);
  1356. skb_trim(priv->rx_skb, le16_to_cpu(buf->wlength));
  1357. at76_dbg_dump(DBG_RX_DATA, priv->rx_skb->data,
  1358. priv->rx_skb->len, "RX: len=%d", priv->rx_skb->len);
  1359. rx_status.signal = buf->rssi;
  1360. rx_status.flag |= RX_FLAG_DECRYPTED;
  1361. rx_status.flag |= RX_FLAG_IV_STRIPPED;
  1362. rx_status.band = NL80211_BAND_2GHZ;
  1363. rx_status.freq = at76_guess_freq(priv);
  1364. at76_dbg(DBG_MAC80211, "calling ieee80211_rx_irqsafe(): %d/%d",
  1365. priv->rx_skb->len, priv->rx_skb->data_len);
  1366. memcpy(IEEE80211_SKB_RXCB(priv->rx_skb), &rx_status, sizeof(rx_status));
  1367. ieee80211_rx_irqsafe(priv->hw, priv->rx_skb);
  1368. /* Use a new skb for the next receive */
  1369. priv->rx_skb = NULL;
  1370. at76_submit_rx_urb(priv);
  1371. }
  1372. /* Load firmware into kernel memory and parse it */
  1373. static struct fwentry *at76_load_firmware(struct usb_device *udev,
  1374. enum board_type board_type)
  1375. {
  1376. int ret;
  1377. char *str;
  1378. struct at76_fw_header *fwh;
  1379. struct fwentry *fwe = &firmwares[board_type];
  1380. mutex_lock(&fw_mutex);
  1381. if (fwe->loaded) {
  1382. at76_dbg(DBG_FW, "re-using previously loaded fw");
  1383. goto exit;
  1384. }
  1385. at76_dbg(DBG_FW, "downloading firmware %s", fwe->fwname);
  1386. ret = request_firmware(&fwe->fw, fwe->fwname, &udev->dev);
  1387. if (ret)
  1388. goto exit;
  1389. at76_dbg(DBG_FW, "got it.");
  1390. fwh = (struct at76_fw_header *)(fwe->fw->data);
  1391. if (fwe->fw->size <= sizeof(*fwh)) {
  1392. dev_err(&udev->dev,
  1393. "firmware is too short (0x%zx)\n", fwe->fw->size);
  1394. goto exit;
  1395. }
  1396. /* CRC currently not checked */
  1397. fwe->board_type = le32_to_cpu(fwh->board_type);
  1398. if (fwe->board_type != board_type) {
  1399. dev_err(&udev->dev,
  1400. "board type mismatch, requested %u, got %u\n",
  1401. board_type, fwe->board_type);
  1402. goto exit;
  1403. }
  1404. fwe->fw_version.major = fwh->major;
  1405. fwe->fw_version.minor = fwh->minor;
  1406. fwe->fw_version.patch = fwh->patch;
  1407. fwe->fw_version.build = fwh->build;
  1408. str = (char *)fwh + le32_to_cpu(fwh->str_offset);
  1409. fwe->intfw = (u8 *)fwh + le32_to_cpu(fwh->int_fw_offset);
  1410. fwe->intfw_size = le32_to_cpu(fwh->int_fw_len);
  1411. fwe->extfw = (u8 *)fwh + le32_to_cpu(fwh->ext_fw_offset);
  1412. fwe->extfw_size = le32_to_cpu(fwh->ext_fw_len);
  1413. fwe->loaded = 1;
  1414. dev_printk(KERN_DEBUG, &udev->dev,
  1415. "using firmware %s (version %d.%d.%d-%d)\n",
  1416. fwe->fwname, fwh->major, fwh->minor, fwh->patch, fwh->build);
  1417. at76_dbg(DBG_DEVSTART, "board %u, int %d:%d, ext %d:%d", board_type,
  1418. le32_to_cpu(fwh->int_fw_offset), le32_to_cpu(fwh->int_fw_len),
  1419. le32_to_cpu(fwh->ext_fw_offset), le32_to_cpu(fwh->ext_fw_len));
  1420. at76_dbg(DBG_DEVSTART, "firmware id %s", str);
  1421. exit:
  1422. mutex_unlock(&fw_mutex);
  1423. if (fwe->loaded)
  1424. return fwe;
  1425. else
  1426. return NULL;
  1427. }
  1428. static int at76_join(struct at76_priv *priv)
  1429. {
  1430. struct at76_req_join join;
  1431. int ret;
  1432. memset(&join, 0, sizeof(struct at76_req_join));
  1433. memcpy(join.essid, priv->essid, priv->essid_size);
  1434. join.essid_size = priv->essid_size;
  1435. memcpy(join.bssid, priv->bssid, ETH_ALEN);
  1436. join.bss_type = INFRASTRUCTURE_MODE;
  1437. join.channel = priv->channel;
  1438. join.timeout = cpu_to_le16(2000);
  1439. at76_dbg(DBG_MAC80211, "%s: sending CMD_JOIN", __func__);
  1440. ret = at76_set_card_command(priv->udev, CMD_JOIN, &join,
  1441. sizeof(struct at76_req_join));
  1442. if (ret < 0) {
  1443. wiphy_err(priv->hw->wiphy, "at76_set_card_command failed: %d\n",
  1444. ret);
  1445. return 0;
  1446. }
  1447. ret = at76_wait_completion(priv, CMD_JOIN);
  1448. at76_dbg(DBG_MAC80211, "%s: CMD_JOIN returned: 0x%02x", __func__, ret);
  1449. if (ret != CMD_STATUS_COMPLETE) {
  1450. wiphy_err(priv->hw->wiphy, "at76_wait_completion failed: %d\n",
  1451. ret);
  1452. return 0;
  1453. }
  1454. at76_set_pm_mode(priv);
  1455. return 0;
  1456. }
  1457. static void at76_work_join_bssid(struct work_struct *work)
  1458. {
  1459. struct at76_priv *priv = container_of(work, struct at76_priv,
  1460. work_join_bssid);
  1461. if (priv->device_unplugged)
  1462. return;
  1463. mutex_lock(&priv->mtx);
  1464. if (is_valid_ether_addr(priv->bssid))
  1465. at76_join(priv);
  1466. mutex_unlock(&priv->mtx);
  1467. }
  1468. static void at76_mac80211_tx_callback(struct urb *urb)
  1469. {
  1470. struct at76_priv *priv = urb->context;
  1471. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(priv->tx_skb);
  1472. at76_dbg(DBG_MAC80211, "%s()", __func__);
  1473. switch (urb->status) {
  1474. case 0:
  1475. /* success */
  1476. info->flags |= IEEE80211_TX_STAT_ACK;
  1477. break;
  1478. case -ENOENT:
  1479. case -ECONNRESET:
  1480. /* fail, urb has been unlinked */
  1481. /* FIXME: add error message */
  1482. break;
  1483. default:
  1484. at76_dbg(DBG_URB, "%s - nonzero tx status received: %d",
  1485. __func__, urb->status);
  1486. break;
  1487. }
  1488. memset(&info->status, 0, sizeof(info->status));
  1489. ieee80211_tx_status_irqsafe(priv->hw, priv->tx_skb);
  1490. priv->tx_skb = NULL;
  1491. ieee80211_wake_queues(priv->hw);
  1492. }
  1493. static void at76_mac80211_tx(struct ieee80211_hw *hw,
  1494. struct ieee80211_tx_control *control,
  1495. struct sk_buff *skb)
  1496. {
  1497. struct at76_priv *priv = hw->priv;
  1498. struct at76_tx_buffer *tx_buffer = priv->bulk_out_buffer;
  1499. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1500. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)skb->data;
  1501. int padding, submit_len, ret;
  1502. at76_dbg(DBG_MAC80211, "%s()", __func__);
  1503. if (priv->tx_urb->status == -EINPROGRESS) {
  1504. wiphy_err(priv->hw->wiphy,
  1505. "%s called while tx urb is pending\n", __func__);
  1506. dev_kfree_skb_any(skb);
  1507. return;
  1508. }
  1509. /* The following code lines are important when the device is going to
  1510. * authenticate with a new bssid. The driver must send CMD_JOIN before
  1511. * an authentication frame is transmitted. For this to succeed, the
  1512. * correct bssid of the AP must be known. As mac80211 does not inform
  1513. * drivers about the bssid prior to the authentication process the
  1514. * following workaround is necessary. If the TX frame is an
  1515. * authentication frame extract the bssid and send the CMD_JOIN. */
  1516. if (mgmt->frame_control & cpu_to_le16(IEEE80211_STYPE_AUTH)) {
  1517. if (!ether_addr_equal_64bits(priv->bssid, mgmt->bssid)) {
  1518. memcpy(priv->bssid, mgmt->bssid, ETH_ALEN);
  1519. ieee80211_queue_work(hw, &priv->work_join_bssid);
  1520. dev_kfree_skb_any(skb);
  1521. return;
  1522. }
  1523. }
  1524. ieee80211_stop_queues(hw);
  1525. at76_ledtrig_tx_activity(); /* tell ledtrigger we send a packet */
  1526. WARN_ON(priv->tx_skb != NULL);
  1527. priv->tx_skb = skb;
  1528. padding = at76_calc_padding(skb->len);
  1529. submit_len = AT76_TX_HDRLEN + skb->len + padding;
  1530. /* setup 'Atmel' header */
  1531. memset(tx_buffer, 0, sizeof(*tx_buffer));
  1532. tx_buffer->padding = padding;
  1533. tx_buffer->wlength = cpu_to_le16(skb->len);
  1534. tx_buffer->tx_rate = ieee80211_get_tx_rate(hw, info)->hw_value;
  1535. memset(tx_buffer->reserved, 0, sizeof(tx_buffer->reserved));
  1536. memcpy(tx_buffer->packet, skb->data, skb->len);
  1537. at76_dbg(DBG_TX_DATA, "%s tx: wlen 0x%x pad 0x%x rate %d hdr",
  1538. wiphy_name(priv->hw->wiphy), le16_to_cpu(tx_buffer->wlength),
  1539. tx_buffer->padding, tx_buffer->tx_rate);
  1540. /* send stuff */
  1541. at76_dbg_dump(DBG_TX_DATA_CONTENT, tx_buffer, submit_len,
  1542. "%s(): tx_buffer %d bytes:", __func__, submit_len);
  1543. usb_fill_bulk_urb(priv->tx_urb, priv->udev, priv->tx_pipe, tx_buffer,
  1544. submit_len, at76_mac80211_tx_callback, priv);
  1545. ret = usb_submit_urb(priv->tx_urb, GFP_ATOMIC);
  1546. if (ret) {
  1547. wiphy_err(priv->hw->wiphy, "error in tx submit urb: %d\n", ret);
  1548. if (ret == -EINVAL)
  1549. wiphy_err(priv->hw->wiphy,
  1550. "-EINVAL: tx urb %p hcpriv %p complete %p\n",
  1551. priv->tx_urb,
  1552. priv->tx_urb->hcpriv, priv->tx_urb->complete);
  1553. }
  1554. }
  1555. static int at76_mac80211_start(struct ieee80211_hw *hw)
  1556. {
  1557. struct at76_priv *priv = hw->priv;
  1558. int ret;
  1559. at76_dbg(DBG_MAC80211, "%s()", __func__);
  1560. mutex_lock(&priv->mtx);
  1561. ret = at76_submit_rx_urb(priv);
  1562. if (ret < 0) {
  1563. wiphy_err(priv->hw->wiphy, "open: submit_rx_urb failed: %d\n",
  1564. ret);
  1565. goto error;
  1566. }
  1567. at76_startup_device(priv);
  1568. at76_start_monitor(priv);
  1569. error:
  1570. mutex_unlock(&priv->mtx);
  1571. return 0;
  1572. }
  1573. static void at76_mac80211_stop(struct ieee80211_hw *hw)
  1574. {
  1575. struct at76_priv *priv = hw->priv;
  1576. at76_dbg(DBG_MAC80211, "%s()", __func__);
  1577. cancel_delayed_work(&priv->dwork_hw_scan);
  1578. cancel_work_sync(&priv->work_join_bssid);
  1579. cancel_work_sync(&priv->work_set_promisc);
  1580. mutex_lock(&priv->mtx);
  1581. if (!priv->device_unplugged) {
  1582. /* We are called by "ifconfig ethX down", not because the
  1583. * device is not available anymore. */
  1584. at76_set_radio(priv, 0);
  1585. /* We unlink rx_urb because at76_open() re-submits it.
  1586. * If unplugged, at76_delete_device() takes care of it. */
  1587. usb_kill_urb(priv->rx_urb);
  1588. }
  1589. mutex_unlock(&priv->mtx);
  1590. }
  1591. static int at76_add_interface(struct ieee80211_hw *hw,
  1592. struct ieee80211_vif *vif)
  1593. {
  1594. struct at76_priv *priv = hw->priv;
  1595. int ret = 0;
  1596. at76_dbg(DBG_MAC80211, "%s()", __func__);
  1597. mutex_lock(&priv->mtx);
  1598. switch (vif->type) {
  1599. case NL80211_IFTYPE_STATION:
  1600. priv->iw_mode = IW_MODE_INFRA;
  1601. break;
  1602. default:
  1603. ret = -EOPNOTSUPP;
  1604. goto exit;
  1605. }
  1606. exit:
  1607. mutex_unlock(&priv->mtx);
  1608. return ret;
  1609. }
  1610. static void at76_remove_interface(struct ieee80211_hw *hw,
  1611. struct ieee80211_vif *vif)
  1612. {
  1613. at76_dbg(DBG_MAC80211, "%s()", __func__);
  1614. }
  1615. static void at76_dwork_hw_scan(struct work_struct *work)
  1616. {
  1617. struct at76_priv *priv = container_of(work, struct at76_priv,
  1618. dwork_hw_scan.work);
  1619. struct cfg80211_scan_info info = {
  1620. .aborted = false,
  1621. };
  1622. int ret;
  1623. if (priv->device_unplugged)
  1624. return;
  1625. mutex_lock(&priv->mtx);
  1626. ret = at76_get_cmd_status(priv->udev, CMD_SCAN);
  1627. at76_dbg(DBG_MAC80211, "%s: CMD_SCAN status 0x%02x", __func__, ret);
  1628. /* FIXME: add maximum time for scan to complete */
  1629. if (ret != CMD_STATUS_COMPLETE) {
  1630. ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
  1631. SCAN_POLL_INTERVAL);
  1632. mutex_unlock(&priv->mtx);
  1633. return;
  1634. }
  1635. if (is_valid_ether_addr(priv->bssid))
  1636. at76_join(priv);
  1637. priv->scanning = false;
  1638. mutex_unlock(&priv->mtx);
  1639. ieee80211_scan_completed(priv->hw, &info);
  1640. ieee80211_wake_queues(priv->hw);
  1641. }
  1642. static int at76_hw_scan(struct ieee80211_hw *hw,
  1643. struct ieee80211_vif *vif,
  1644. struct ieee80211_scan_request *hw_req)
  1645. {
  1646. struct cfg80211_scan_request *req = &hw_req->req;
  1647. struct at76_priv *priv = hw->priv;
  1648. struct at76_req_scan scan;
  1649. u8 *ssid = NULL;
  1650. int ret, len = 0;
  1651. at76_dbg(DBG_MAC80211, "%s():", __func__);
  1652. if (priv->device_unplugged)
  1653. return 0;
  1654. mutex_lock(&priv->mtx);
  1655. ieee80211_stop_queues(hw);
  1656. memset(&scan, 0, sizeof(struct at76_req_scan));
  1657. eth_broadcast_addr(scan.bssid);
  1658. if (req->n_ssids) {
  1659. scan.scan_type = SCAN_TYPE_ACTIVE;
  1660. ssid = req->ssids[0].ssid;
  1661. len = req->ssids[0].ssid_len;
  1662. } else {
  1663. scan.scan_type = SCAN_TYPE_PASSIVE;
  1664. }
  1665. if (len) {
  1666. memcpy(scan.essid, ssid, len);
  1667. scan.essid_size = len;
  1668. }
  1669. scan.min_channel_time = cpu_to_le16(priv->scan_min_time);
  1670. scan.max_channel_time = cpu_to_le16(priv->scan_max_time);
  1671. scan.probe_delay = cpu_to_le16(priv->scan_min_time * 1000);
  1672. scan.international_scan = 0;
  1673. at76_dbg(DBG_MAC80211, "%s: sending CMD_SCAN", __func__);
  1674. ret = at76_set_card_command(priv->udev, CMD_SCAN, &scan, sizeof(scan));
  1675. if (ret < 0) {
  1676. wiphy_err(priv->hw->wiphy, "CMD_SCAN failed: %d\n", ret);
  1677. goto exit;
  1678. }
  1679. priv->scanning = true;
  1680. ieee80211_queue_delayed_work(priv->hw, &priv->dwork_hw_scan,
  1681. SCAN_POLL_INTERVAL);
  1682. exit:
  1683. mutex_unlock(&priv->mtx);
  1684. return 0;
  1685. }
  1686. static int at76_config(struct ieee80211_hw *hw, u32 changed)
  1687. {
  1688. struct at76_priv *priv = hw->priv;
  1689. at76_dbg(DBG_MAC80211, "%s(): channel %d",
  1690. __func__, hw->conf.chandef.chan->hw_value);
  1691. at76_dbg_dump(DBG_MAC80211, priv->bssid, ETH_ALEN, "bssid:");
  1692. mutex_lock(&priv->mtx);
  1693. priv->channel = hw->conf.chandef.chan->hw_value;
  1694. if (is_valid_ether_addr(priv->bssid))
  1695. at76_join(priv);
  1696. else
  1697. at76_start_monitor(priv);
  1698. mutex_unlock(&priv->mtx);
  1699. return 0;
  1700. }
  1701. static void at76_bss_info_changed(struct ieee80211_hw *hw,
  1702. struct ieee80211_vif *vif,
  1703. struct ieee80211_bss_conf *conf,
  1704. u32 changed)
  1705. {
  1706. struct at76_priv *priv = hw->priv;
  1707. at76_dbg(DBG_MAC80211, "%s():", __func__);
  1708. if (!(changed & BSS_CHANGED_BSSID))
  1709. return;
  1710. at76_dbg_dump(DBG_MAC80211, conf->bssid, ETH_ALEN, "bssid:");
  1711. mutex_lock(&priv->mtx);
  1712. memcpy(priv->bssid, conf->bssid, ETH_ALEN);
  1713. if (is_valid_ether_addr(priv->bssid))
  1714. /* mac80211 is joining a bss */
  1715. at76_join(priv);
  1716. mutex_unlock(&priv->mtx);
  1717. }
  1718. /* must be atomic */
  1719. static void at76_configure_filter(struct ieee80211_hw *hw,
  1720. unsigned int changed_flags,
  1721. unsigned int *total_flags, u64 multicast)
  1722. {
  1723. struct at76_priv *priv = hw->priv;
  1724. int flags;
  1725. at76_dbg(DBG_MAC80211, "%s(): changed_flags=0x%08x "
  1726. "total_flags=0x%08x",
  1727. __func__, changed_flags, *total_flags);
  1728. flags = changed_flags & AT76_SUPPORTED_FILTERS;
  1729. *total_flags = AT76_SUPPORTED_FILTERS;
  1730. /* Bail out after updating flags to prevent a WARN_ON in mac80211. */
  1731. if (priv->device_unplugged)
  1732. return;
  1733. /* FIXME: access to priv->promisc should be protected with
  1734. * priv->mtx, but it's impossible because this function needs to be
  1735. * atomic */
  1736. if (flags && !priv->promisc) {
  1737. /* mac80211 wants us to enable promiscuous mode */
  1738. priv->promisc = 1;
  1739. } else if (!flags && priv->promisc) {
  1740. /* we need to disable promiscuous mode */
  1741. priv->promisc = 0;
  1742. } else
  1743. return;
  1744. ieee80211_queue_work(hw, &priv->work_set_promisc);
  1745. }
  1746. static int at76_set_wep(struct at76_priv *priv)
  1747. {
  1748. int ret = 0;
  1749. struct mib_mac_wep *mib_data = &priv->mib_buf.data.wep_mib;
  1750. priv->mib_buf.type = MIB_MAC_WEP;
  1751. priv->mib_buf.size = sizeof(struct mib_mac_wep);
  1752. priv->mib_buf.index = 0;
  1753. memset(mib_data, 0, sizeof(*mib_data));
  1754. if (priv->wep_enabled) {
  1755. if (priv->wep_keys_len[priv->wep_key_id] > WEP_SMALL_KEY_LEN)
  1756. mib_data->encryption_level = 2;
  1757. else
  1758. mib_data->encryption_level = 1;
  1759. /* always exclude unencrypted if WEP is active */
  1760. mib_data->exclude_unencrypted = 1;
  1761. } else {
  1762. mib_data->exclude_unencrypted = 0;
  1763. mib_data->encryption_level = 0;
  1764. }
  1765. mib_data->privacy_invoked = priv->wep_enabled;
  1766. mib_data->wep_default_key_id = priv->wep_key_id;
  1767. memcpy(mib_data->wep_default_keyvalue, priv->wep_keys,
  1768. sizeof(priv->wep_keys));
  1769. ret = at76_set_mib(priv, &priv->mib_buf);
  1770. if (ret < 0)
  1771. wiphy_err(priv->hw->wiphy,
  1772. "set_mib (wep) failed: %d\n", ret);
  1773. return ret;
  1774. }
  1775. static int at76_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  1776. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  1777. struct ieee80211_key_conf *key)
  1778. {
  1779. struct at76_priv *priv = hw->priv;
  1780. int i;
  1781. at76_dbg(DBG_MAC80211, "%s(): cmd %d key->cipher %d key->keyidx %d "
  1782. "key->keylen %d",
  1783. __func__, cmd, key->cipher, key->keyidx, key->keylen);
  1784. if ((key->cipher != WLAN_CIPHER_SUITE_WEP40) &&
  1785. (key->cipher != WLAN_CIPHER_SUITE_WEP104))
  1786. return -EOPNOTSUPP;
  1787. key->hw_key_idx = key->keyidx;
  1788. mutex_lock(&priv->mtx);
  1789. switch (cmd) {
  1790. case SET_KEY:
  1791. memcpy(priv->wep_keys[key->keyidx], key->key, key->keylen);
  1792. priv->wep_keys_len[key->keyidx] = key->keylen;
  1793. /* FIXME: find out how to do this properly */
  1794. priv->wep_key_id = key->keyidx;
  1795. break;
  1796. case DISABLE_KEY:
  1797. default:
  1798. priv->wep_keys_len[key->keyidx] = 0;
  1799. break;
  1800. }
  1801. priv->wep_enabled = 0;
  1802. for (i = 0; i < WEP_KEYS; i++) {
  1803. if (priv->wep_keys_len[i] != 0)
  1804. priv->wep_enabled = 1;
  1805. }
  1806. at76_set_wep(priv);
  1807. mutex_unlock(&priv->mtx);
  1808. return 0;
  1809. }
  1810. static const struct ieee80211_ops at76_ops = {
  1811. .tx = at76_mac80211_tx,
  1812. .add_interface = at76_add_interface,
  1813. .remove_interface = at76_remove_interface,
  1814. .config = at76_config,
  1815. .bss_info_changed = at76_bss_info_changed,
  1816. .configure_filter = at76_configure_filter,
  1817. .start = at76_mac80211_start,
  1818. .stop = at76_mac80211_stop,
  1819. .hw_scan = at76_hw_scan,
  1820. .set_key = at76_set_key,
  1821. };
  1822. /* Allocate network device and initialize private data */
  1823. static struct at76_priv *at76_alloc_new_device(struct usb_device *udev)
  1824. {
  1825. struct ieee80211_hw *hw;
  1826. struct at76_priv *priv;
  1827. hw = ieee80211_alloc_hw(sizeof(struct at76_priv), &at76_ops);
  1828. if (!hw) {
  1829. printk(KERN_ERR DRIVER_NAME ": could not register"
  1830. " ieee80211_hw\n");
  1831. return NULL;
  1832. }
  1833. priv = hw->priv;
  1834. priv->hw = hw;
  1835. priv->udev = udev;
  1836. mutex_init(&priv->mtx);
  1837. INIT_WORK(&priv->work_set_promisc, at76_work_set_promisc);
  1838. INIT_WORK(&priv->work_submit_rx, at76_work_submit_rx);
  1839. INIT_WORK(&priv->work_join_bssid, at76_work_join_bssid);
  1840. INIT_DELAYED_WORK(&priv->dwork_hw_scan, at76_dwork_hw_scan);
  1841. tasklet_init(&priv->rx_tasklet, at76_rx_tasklet, 0);
  1842. priv->pm_mode = AT76_PM_OFF;
  1843. priv->pm_period = 0;
  1844. /* unit us */
  1845. return priv;
  1846. }
  1847. static int at76_alloc_urbs(struct at76_priv *priv,
  1848. struct usb_interface *interface)
  1849. {
  1850. struct usb_endpoint_descriptor *endpoint, *ep_in, *ep_out;
  1851. int i;
  1852. int buffer_size;
  1853. struct usb_host_interface *iface_desc;
  1854. at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
  1855. at76_dbg(DBG_URB, "%s: NumEndpoints %d ", __func__,
  1856. interface->altsetting[0].desc.bNumEndpoints);
  1857. ep_in = NULL;
  1858. ep_out = NULL;
  1859. iface_desc = interface->cur_altsetting;
  1860. for (i = 0; i < iface_desc->desc.bNumEndpoints; i++) {
  1861. endpoint = &iface_desc->endpoint[i].desc;
  1862. at76_dbg(DBG_URB, "%s: %d. endpoint: addr 0x%x attr 0x%x",
  1863. __func__, i, endpoint->bEndpointAddress,
  1864. endpoint->bmAttributes);
  1865. if (!ep_in && usb_endpoint_is_bulk_in(endpoint))
  1866. ep_in = endpoint;
  1867. if (!ep_out && usb_endpoint_is_bulk_out(endpoint))
  1868. ep_out = endpoint;
  1869. }
  1870. if (!ep_in || !ep_out) {
  1871. dev_err(&interface->dev, "bulk endpoints missing\n");
  1872. return -ENXIO;
  1873. }
  1874. priv->rx_pipe = usb_rcvbulkpipe(priv->udev, ep_in->bEndpointAddress);
  1875. priv->tx_pipe = usb_sndbulkpipe(priv->udev, ep_out->bEndpointAddress);
  1876. priv->rx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1877. priv->tx_urb = usb_alloc_urb(0, GFP_KERNEL);
  1878. if (!priv->rx_urb || !priv->tx_urb) {
  1879. dev_err(&interface->dev, "cannot allocate URB\n");
  1880. return -ENOMEM;
  1881. }
  1882. buffer_size = sizeof(struct at76_tx_buffer) + MAX_PADDING_SIZE;
  1883. priv->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
  1884. if (!priv->bulk_out_buffer)
  1885. return -ENOMEM;
  1886. at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
  1887. return 0;
  1888. }
  1889. static struct ieee80211_rate at76_rates[] = {
  1890. { .bitrate = 10, .hw_value = TX_RATE_1MBIT, },
  1891. { .bitrate = 20, .hw_value = TX_RATE_2MBIT, },
  1892. { .bitrate = 55, .hw_value = TX_RATE_5_5MBIT, },
  1893. { .bitrate = 110, .hw_value = TX_RATE_11MBIT, },
  1894. };
  1895. static struct ieee80211_channel at76_channels[] = {
  1896. { .center_freq = 2412, .hw_value = 1 },
  1897. { .center_freq = 2417, .hw_value = 2 },
  1898. { .center_freq = 2422, .hw_value = 3 },
  1899. { .center_freq = 2427, .hw_value = 4 },
  1900. { .center_freq = 2432, .hw_value = 5 },
  1901. { .center_freq = 2437, .hw_value = 6 },
  1902. { .center_freq = 2442, .hw_value = 7 },
  1903. { .center_freq = 2447, .hw_value = 8 },
  1904. { .center_freq = 2452, .hw_value = 9 },
  1905. { .center_freq = 2457, .hw_value = 10 },
  1906. { .center_freq = 2462, .hw_value = 11 },
  1907. { .center_freq = 2467, .hw_value = 12 },
  1908. { .center_freq = 2472, .hw_value = 13 },
  1909. { .center_freq = 2484, .hw_value = 14 }
  1910. };
  1911. static struct ieee80211_supported_band at76_supported_band = {
  1912. .channels = at76_channels,
  1913. .n_channels = ARRAY_SIZE(at76_channels),
  1914. .bitrates = at76_rates,
  1915. .n_bitrates = ARRAY_SIZE(at76_rates),
  1916. };
  1917. /* Register network device and initialize the hardware */
  1918. static int at76_init_new_device(struct at76_priv *priv,
  1919. struct usb_interface *interface)
  1920. {
  1921. struct wiphy *wiphy;
  1922. size_t len;
  1923. int ret;
  1924. /* set up the endpoint information */
  1925. /* check out the endpoints */
  1926. at76_dbg(DBG_DEVSTART, "USB interface: %d endpoints",
  1927. interface->cur_altsetting->desc.bNumEndpoints);
  1928. ret = at76_alloc_urbs(priv, interface);
  1929. if (ret < 0)
  1930. goto exit;
  1931. /* MAC address */
  1932. ret = at76_get_hw_config(priv);
  1933. if (ret < 0) {
  1934. dev_err(&interface->dev, "cannot get MAC address\n");
  1935. goto exit;
  1936. }
  1937. priv->domain = at76_get_reg_domain(priv->regulatory_domain);
  1938. priv->channel = DEF_CHANNEL;
  1939. priv->iw_mode = IW_MODE_INFRA;
  1940. priv->rts_threshold = DEF_RTS_THRESHOLD;
  1941. priv->frag_threshold = DEF_FRAG_THRESHOLD;
  1942. priv->short_retry_limit = DEF_SHORT_RETRY_LIMIT;
  1943. priv->txrate = TX_RATE_AUTO;
  1944. priv->preamble_type = PREAMBLE_TYPE_LONG;
  1945. priv->beacon_period = 100;
  1946. priv->auth_mode = WLAN_AUTH_OPEN;
  1947. priv->scan_min_time = DEF_SCAN_MIN_TIME;
  1948. priv->scan_max_time = DEF_SCAN_MAX_TIME;
  1949. priv->scan_mode = SCAN_TYPE_ACTIVE;
  1950. priv->device_unplugged = 0;
  1951. /* mac80211 initialisation */
  1952. wiphy = priv->hw->wiphy;
  1953. priv->hw->wiphy->max_scan_ssids = 1;
  1954. priv->hw->wiphy->max_scan_ie_len = 0;
  1955. priv->hw->wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION);
  1956. priv->hw->wiphy->bands[NL80211_BAND_2GHZ] = &at76_supported_band;
  1957. ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
  1958. ieee80211_hw_set(priv->hw, SIGNAL_UNSPEC);
  1959. priv->hw->max_signal = 100;
  1960. SET_IEEE80211_DEV(priv->hw, &interface->dev);
  1961. SET_IEEE80211_PERM_ADDR(priv->hw, priv->mac_addr);
  1962. len = sizeof(wiphy->fw_version);
  1963. snprintf(wiphy->fw_version, len, "%d.%d.%d-%d",
  1964. priv->fw_version.major, priv->fw_version.minor,
  1965. priv->fw_version.patch, priv->fw_version.build);
  1966. wiphy->hw_version = priv->board_type;
  1967. wiphy_ext_feature_set(wiphy, NL80211_EXT_FEATURE_CQM_RSSI_LIST);
  1968. ret = ieee80211_register_hw(priv->hw);
  1969. if (ret) {
  1970. printk(KERN_ERR "cannot register mac80211 hw (status %d)!\n",
  1971. ret);
  1972. goto exit;
  1973. }
  1974. priv->mac80211_registered = 1;
  1975. wiphy_info(priv->hw->wiphy, "USB %s, MAC %pM, firmware %d.%d.%d-%d\n",
  1976. dev_name(&interface->dev), priv->mac_addr,
  1977. priv->fw_version.major, priv->fw_version.minor,
  1978. priv->fw_version.patch, priv->fw_version.build);
  1979. wiphy_info(priv->hw->wiphy, "regulatory domain 0x%02x: %s\n",
  1980. priv->regulatory_domain, priv->domain->name);
  1981. exit:
  1982. return ret;
  1983. }
  1984. static void at76_delete_device(struct at76_priv *priv)
  1985. {
  1986. at76_dbg(DBG_PROC_ENTRY, "%s: ENTER", __func__);
  1987. /* The device is gone, don't bother turning it off */
  1988. priv->device_unplugged = 1;
  1989. tasklet_kill(&priv->rx_tasklet);
  1990. if (priv->mac80211_registered)
  1991. ieee80211_unregister_hw(priv->hw);
  1992. if (priv->tx_urb) {
  1993. usb_kill_urb(priv->tx_urb);
  1994. usb_free_urb(priv->tx_urb);
  1995. }
  1996. if (priv->rx_urb) {
  1997. usb_kill_urb(priv->rx_urb);
  1998. usb_free_urb(priv->rx_urb);
  1999. }
  2000. at76_dbg(DBG_PROC_ENTRY, "%s: unlinked urbs", __func__);
  2001. kfree(priv->bulk_out_buffer);
  2002. del_timer_sync(&ledtrig_tx_timer);
  2003. kfree_skb(priv->rx_skb);
  2004. at76_dbg(DBG_PROC_ENTRY, "%s: before freeing priv/ieee80211_hw",
  2005. __func__);
  2006. ieee80211_free_hw(priv->hw);
  2007. at76_dbg(DBG_PROC_ENTRY, "%s: EXIT", __func__);
  2008. }
  2009. static int at76_probe(struct usb_interface *interface,
  2010. const struct usb_device_id *id)
  2011. {
  2012. int ret;
  2013. struct at76_priv *priv;
  2014. struct fwentry *fwe;
  2015. struct usb_device *udev;
  2016. int op_mode;
  2017. int need_ext_fw = 0;
  2018. struct mib_fw_version *fwv = NULL;
  2019. int board_type = (int)id->driver_info;
  2020. udev = usb_get_dev(interface_to_usbdev(interface));
  2021. fwv = kmalloc(sizeof(*fwv), GFP_KERNEL);
  2022. if (!fwv) {
  2023. ret = -ENOMEM;
  2024. goto exit;
  2025. }
  2026. /* Load firmware into kernel memory */
  2027. fwe = at76_load_firmware(udev, board_type);
  2028. if (!fwe) {
  2029. ret = -ENOENT;
  2030. goto exit;
  2031. }
  2032. op_mode = at76_get_op_mode(udev);
  2033. at76_dbg(DBG_DEVSTART, "opmode %d", op_mode);
  2034. /* we get OPMODE_NONE with 2.4.23, SMC2662W-AR ???
  2035. we get 204 with 2.4.23, Fiberline FL-WL240u (505A+RFMD2958) ??? */
  2036. if (op_mode == OPMODE_HW_CONFIG_MODE) {
  2037. dev_err(&interface->dev,
  2038. "cannot handle a device in HW_CONFIG_MODE\n");
  2039. ret = -EBUSY;
  2040. goto exit;
  2041. }
  2042. if (op_mode != OPMODE_NORMAL_NIC_WITH_FLASH
  2043. && op_mode != OPMODE_NORMAL_NIC_WITHOUT_FLASH) {
  2044. /* download internal firmware part */
  2045. dev_printk(KERN_DEBUG, &interface->dev,
  2046. "downloading internal firmware\n");
  2047. ret = at76_load_internal_fw(udev, fwe);
  2048. if (ret < 0) {
  2049. dev_err(&interface->dev,
  2050. "error %d downloading internal firmware\n",
  2051. ret);
  2052. }
  2053. goto exit;
  2054. }
  2055. /* Internal firmware already inside the device. Get firmware
  2056. * version to test if external firmware is loaded.
  2057. * This works only for newer firmware, e.g. the Intersil 0.90.x
  2058. * says "control timeout on ep0in" and subsequent
  2059. * at76_get_op_mode() fail too :-( */
  2060. /* if version >= 0.100.x.y or device with built-in flash we can
  2061. * query the device for the fw version */
  2062. if ((fwe->fw_version.major > 0 || fwe->fw_version.minor >= 100)
  2063. || (op_mode == OPMODE_NORMAL_NIC_WITH_FLASH)) {
  2064. ret = at76_get_mib(udev, MIB_FW_VERSION, fwv, sizeof(*fwv));
  2065. if (ret < 0 || (fwv->major | fwv->minor) == 0)
  2066. need_ext_fw = 1;
  2067. } else
  2068. /* No way to check firmware version, reload to be sure */
  2069. need_ext_fw = 1;
  2070. if (need_ext_fw) {
  2071. dev_printk(KERN_DEBUG, &interface->dev,
  2072. "downloading external firmware\n");
  2073. ret = at76_load_external_fw(udev, fwe);
  2074. if (ret < 0)
  2075. goto exit;
  2076. /* Re-check firmware version */
  2077. ret = at76_get_mib(udev, MIB_FW_VERSION, fwv, sizeof(*fwv));
  2078. if (ret < 0) {
  2079. dev_err(&interface->dev,
  2080. "error %d getting firmware version\n", ret);
  2081. goto exit;
  2082. }
  2083. }
  2084. priv = at76_alloc_new_device(udev);
  2085. if (!priv) {
  2086. ret = -ENOMEM;
  2087. goto exit;
  2088. }
  2089. usb_set_intfdata(interface, priv);
  2090. memcpy(&priv->fw_version, fwv, sizeof(struct mib_fw_version));
  2091. priv->board_type = board_type;
  2092. ret = at76_init_new_device(priv, interface);
  2093. if (ret < 0)
  2094. at76_delete_device(priv);
  2095. exit:
  2096. kfree(fwv);
  2097. if (ret < 0)
  2098. usb_put_dev(udev);
  2099. return ret;
  2100. }
  2101. static void at76_disconnect(struct usb_interface *interface)
  2102. {
  2103. struct at76_priv *priv;
  2104. priv = usb_get_intfdata(interface);
  2105. usb_set_intfdata(interface, NULL);
  2106. /* Disconnect after loading internal firmware */
  2107. if (!priv)
  2108. return;
  2109. wiphy_info(priv->hw->wiphy, "disconnecting\n");
  2110. at76_delete_device(priv);
  2111. usb_put_dev(priv->udev);
  2112. dev_info(&interface->dev, "disconnected\n");
  2113. }
  2114. /* Structure for registering this driver with the USB subsystem */
  2115. static struct usb_driver at76_driver = {
  2116. .name = DRIVER_NAME,
  2117. .probe = at76_probe,
  2118. .disconnect = at76_disconnect,
  2119. .id_table = dev_table,
  2120. .disable_hub_initiated_lpm = 1,
  2121. };
  2122. static int __init at76_mod_init(void)
  2123. {
  2124. int result;
  2125. printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " loading\n");
  2126. mutex_init(&fw_mutex);
  2127. /* register this driver with the USB subsystem */
  2128. result = usb_register(&at76_driver);
  2129. if (result < 0)
  2130. printk(KERN_ERR DRIVER_NAME
  2131. ": usb_register failed (status %d)\n", result);
  2132. else
  2133. led_trigger_register_simple("at76_usb-tx", &ledtrig_tx);
  2134. return result;
  2135. }
  2136. static void __exit at76_mod_exit(void)
  2137. {
  2138. int i;
  2139. printk(KERN_INFO DRIVER_DESC " " DRIVER_VERSION " unloading\n");
  2140. usb_deregister(&at76_driver);
  2141. for (i = 0; i < ARRAY_SIZE(firmwares); i++)
  2142. release_firmware(firmwares[i].fw);
  2143. led_trigger_unregister_simple(ledtrig_tx);
  2144. }
  2145. module_param_named(debug, at76_debug, uint, 0600);
  2146. MODULE_PARM_DESC(debug, "Debugging level");
  2147. module_init(at76_mod_init);
  2148. module_exit(at76_mod_exit);
  2149. MODULE_AUTHOR("Oliver Kurth <oku@masqmail.cx>");
  2150. MODULE_AUTHOR("Joerg Albert <joerg.albert@gmx.de>");
  2151. MODULE_AUTHOR("Alex <alex@foogod.com>");
  2152. MODULE_AUTHOR("Nick Jones");
  2153. MODULE_AUTHOR("Balint Seeber <n0_5p4m_p13453@hotmail.com>");
  2154. MODULE_AUTHOR("Pavel Roskin <proski@gnu.org>");
  2155. MODULE_AUTHOR("Guido Guenther <agx@sigxcpu.org>");
  2156. MODULE_AUTHOR("Kalle Valo <kalle.valo@iki.fi>");
  2157. MODULE_AUTHOR("Sebastian Smolorz <sesmo@gmx.net>");
  2158. MODULE_DESCRIPTION(DRIVER_DESC);
  2159. MODULE_LICENSE("GPL");