at76c50x-usb.c 69 KB

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