at76c50x-usb.c 70 KB

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