at76c50x-usb.c 68 KB

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