at76c50x-usb.c 69 KB

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