at76c50x-usb.c 71 KB

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