at76c50x-usb.c 69 KB

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