at76c50x-usb.c 70 KB

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