main_usb.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  1. /*
  2. * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
  3. * All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * File: main_usb.c
  20. *
  21. * Purpose: driver entry for initial, open, close, tx and rx.
  22. *
  23. * Author: Lyndon Chen
  24. *
  25. * Date: Dec 8, 2005
  26. *
  27. * Functions:
  28. *
  29. * vt6656_probe - module initial (insmod) driver entry
  30. * vnt_free_tx_bufs - free tx buffer function
  31. * vnt_init_registers- initial MAC & BBP & RF internal registers.
  32. *
  33. * Revision History:
  34. */
  35. #undef __NO_VERSION__
  36. #include <linux/etherdevice.h>
  37. #include <linux/file.h>
  38. #include "device.h"
  39. #include "card.h"
  40. #include "baseband.h"
  41. #include "mac.h"
  42. #include "power.h"
  43. #include "wcmd.h"
  44. #include "rxtx.h"
  45. #include "dpc.h"
  46. #include "rf.h"
  47. #include "firmware.h"
  48. #include "usbpipe.h"
  49. #include "channel.h"
  50. #include "int.h"
  51. /*
  52. * define module options
  53. */
  54. /* version information */
  55. #define DRIVER_AUTHOR \
  56. "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
  57. MODULE_AUTHOR(DRIVER_AUTHOR);
  58. MODULE_LICENSE("GPL");
  59. MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
  60. #define RX_DESC_DEF0 64
  61. static int vnt_rx_buffers = RX_DESC_DEF0;
  62. module_param_named(rx_buffers, vnt_rx_buffers, int, 0644);
  63. MODULE_PARM_DESC(rx_buffers, "Number of receive usb rx buffers");
  64. #define TX_DESC_DEF0 64
  65. static int vnt_tx_buffers = TX_DESC_DEF0;
  66. module_param_named(tx_buffers, vnt_tx_buffers, int, 0644);
  67. MODULE_PARM_DESC(tx_buffers, "Number of receive usb tx buffers");
  68. #define RTS_THRESH_DEF 2347
  69. #define FRAG_THRESH_DEF 2346
  70. #define SHORT_RETRY_DEF 8
  71. #define LONG_RETRY_DEF 4
  72. /* BasebandType[] baseband type selected
  73. 0: indicate 802.11a type
  74. 1: indicate 802.11b type
  75. 2: indicate 802.11g type
  76. */
  77. #define BBP_TYPE_DEF 2
  78. /*
  79. * Static vars definitions
  80. */
  81. static struct usb_device_id vt6656_table[] = {
  82. {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
  83. {}
  84. };
  85. static void vnt_set_options(struct vnt_private *priv)
  86. {
  87. /* Set number of TX buffers */
  88. if (vnt_tx_buffers < CB_MIN_TX_DESC || vnt_tx_buffers > CB_MAX_TX_DESC)
  89. priv->num_tx_context = TX_DESC_DEF0;
  90. else
  91. priv->num_tx_context = vnt_tx_buffers;
  92. /* Set number of RX buffers */
  93. if (vnt_rx_buffers < CB_MIN_RX_DESC || vnt_rx_buffers > CB_MAX_RX_DESC)
  94. priv->num_rcb = RX_DESC_DEF0;
  95. else
  96. priv->num_rcb = vnt_rx_buffers;
  97. priv->short_retry_limit = SHORT_RETRY_DEF;
  98. priv->long_retry_limit = LONG_RETRY_DEF;
  99. priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
  100. priv->bb_type = BBP_TYPE_DEF;
  101. priv->packet_type = priv->bb_type;
  102. priv->auto_fb_ctrl = AUTO_FB_0;
  103. priv->preamble_type = 0;
  104. priv->exist_sw_net_addr = false;
  105. }
  106. /*
  107. * initialization of MAC & BBP registers
  108. */
  109. static int vnt_init_registers(struct vnt_private *priv)
  110. {
  111. struct vnt_cmd_card_init *init_cmd = &priv->init_command;
  112. struct vnt_rsp_card_init *init_rsp = &priv->init_response;
  113. u8 antenna;
  114. int ii;
  115. int status = STATUS_SUCCESS;
  116. u8 tmp;
  117. u8 calib_tx_iq = 0, calib_tx_dc = 0, calib_rx_iq = 0;
  118. dev_dbg(&priv->usb->dev, "---->INIbInitAdapter. [%d][%d]\n",
  119. DEVICE_INIT_COLD, priv->packet_type);
  120. if (!vnt_check_firmware_version(priv)) {
  121. if (vnt_download_firmware(priv) == true) {
  122. if (vnt_firmware_branch_to_sram(priv) == false) {
  123. dev_dbg(&priv->usb->dev,
  124. " vnt_firmware_branch_to_sram fail\n");
  125. return false;
  126. }
  127. } else {
  128. dev_dbg(&priv->usb->dev, "FIRMWAREbDownload fail\n");
  129. return false;
  130. }
  131. }
  132. if (!vnt_vt3184_init(priv)) {
  133. dev_dbg(&priv->usb->dev, "vnt_vt3184_init fail\n");
  134. return false;
  135. }
  136. init_cmd->init_class = DEVICE_INIT_COLD;
  137. init_cmd->exist_sw_net_addr = priv->exist_sw_net_addr;
  138. for (ii = 0; ii < 6; ii++)
  139. init_cmd->sw_net_addr[ii] = priv->current_net_addr[ii];
  140. init_cmd->short_retry_limit = priv->short_retry_limit;
  141. init_cmd->long_retry_limit = priv->long_retry_limit;
  142. /* issue card_init command to device */
  143. status = vnt_control_out(priv,
  144. MESSAGE_TYPE_CARDINIT, 0, 0,
  145. sizeof(struct vnt_cmd_card_init), (u8 *)init_cmd);
  146. if (status != STATUS_SUCCESS) {
  147. dev_dbg(&priv->usb->dev, "Issue Card init fail\n");
  148. return false;
  149. }
  150. status = vnt_control_in(priv, MESSAGE_TYPE_INIT_RSP, 0, 0,
  151. sizeof(struct vnt_rsp_card_init), (u8 *)init_rsp);
  152. if (status != STATUS_SUCCESS) {
  153. dev_dbg(&priv->usb->dev,
  154. "Cardinit request in status fail!\n");
  155. return false;
  156. }
  157. /* local ID for AES functions */
  158. status = vnt_control_in(priv, MESSAGE_TYPE_READ,
  159. MAC_REG_LOCALID, MESSAGE_REQUEST_MACREG, 1,
  160. &priv->local_id);
  161. if (status != STATUS_SUCCESS)
  162. return false;
  163. /* do MACbSoftwareReset in MACvInitialize */
  164. priv->top_ofdm_basic_rate = RATE_24M;
  165. priv->top_cck_basic_rate = RATE_1M;
  166. /* target to IF pin while programming to RF chip */
  167. priv->power = 0xFF;
  168. priv->cck_pwr = priv->eeprom[EEP_OFS_PWR_CCK];
  169. priv->ofdm_pwr_g = priv->eeprom[EEP_OFS_PWR_OFDMG];
  170. /* load power table */
  171. for (ii = 0; ii < 14; ii++) {
  172. priv->cck_pwr_tbl[ii] =
  173. priv->eeprom[ii + EEP_OFS_CCK_PWR_TBL];
  174. if (priv->cck_pwr_tbl[ii] == 0)
  175. priv->cck_pwr_tbl[ii] = priv->cck_pwr;
  176. priv->ofdm_pwr_tbl[ii] =
  177. priv->eeprom[ii + EEP_OFS_OFDM_PWR_TBL];
  178. if (priv->ofdm_pwr_tbl[ii] == 0)
  179. priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_g;
  180. }
  181. /*
  182. * original zonetype is USA, but custom zonetype is Europe,
  183. * then need to recover 12, 13, 14 channels with 11 channel
  184. */
  185. for (ii = 11; ii < 14; ii++) {
  186. priv->cck_pwr_tbl[ii] = priv->cck_pwr_tbl[10];
  187. priv->ofdm_pwr_tbl[ii] = priv->ofdm_pwr_tbl[10];
  188. }
  189. priv->ofdm_pwr_a = 0x34; /* same as RFbMA2829SelectChannel */
  190. /* load OFDM A power table */
  191. for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
  192. priv->ofdm_a_pwr_tbl[ii] =
  193. priv->eeprom[ii + EEP_OFS_OFDMA_PWR_TBL];
  194. if (priv->ofdm_a_pwr_tbl[ii] == 0)
  195. priv->ofdm_a_pwr_tbl[ii] = priv->ofdm_pwr_a;
  196. }
  197. antenna = priv->eeprom[EEP_OFS_ANTENNA];
  198. if (antenna & EEP_ANTINV)
  199. priv->tx_rx_ant_inv = true;
  200. else
  201. priv->tx_rx_ant_inv = false;
  202. antenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
  203. if (antenna == 0) /* if not set default is both */
  204. antenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
  205. if (antenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
  206. priv->tx_antenna_mode = ANT_B;
  207. priv->rx_antenna_sel = 1;
  208. if (priv->tx_rx_ant_inv == true)
  209. priv->rx_antenna_mode = ANT_A;
  210. else
  211. priv->rx_antenna_mode = ANT_B;
  212. } else {
  213. priv->rx_antenna_sel = 0;
  214. if (antenna & EEP_ANTENNA_AUX) {
  215. priv->tx_antenna_mode = ANT_A;
  216. if (priv->tx_rx_ant_inv == true)
  217. priv->rx_antenna_mode = ANT_B;
  218. else
  219. priv->rx_antenna_mode = ANT_A;
  220. } else {
  221. priv->tx_antenna_mode = ANT_B;
  222. if (priv->tx_rx_ant_inv == true)
  223. priv->rx_antenna_mode = ANT_A;
  224. else
  225. priv->rx_antenna_mode = ANT_B;
  226. }
  227. }
  228. /* Set initial antenna mode */
  229. vnt_set_antenna_mode(priv, priv->rx_antenna_mode);
  230. /* get Auto Fall Back type */
  231. priv->auto_fb_ctrl = AUTO_FB_0;
  232. /* default Auto Mode */
  233. priv->bb_type = BB_TYPE_11G;
  234. /* get RFType */
  235. priv->rf_type = init_rsp->rf_type;
  236. /* load vt3266 calibration parameters in EEPROM */
  237. if (priv->rf_type == RF_VT3226D0) {
  238. if ((priv->eeprom[EEP_OFS_MAJOR_VER] == 0x1) &&
  239. (priv->eeprom[EEP_OFS_MINOR_VER] >= 0x4)) {
  240. calib_tx_iq = priv->eeprom[EEP_OFS_CALIB_TX_IQ];
  241. calib_tx_dc = priv->eeprom[EEP_OFS_CALIB_TX_DC];
  242. calib_rx_iq = priv->eeprom[EEP_OFS_CALIB_RX_IQ];
  243. if (calib_tx_iq || calib_tx_dc || calib_rx_iq) {
  244. /* CR255, enable TX/RX IQ and
  245. DC compensation mode */
  246. vnt_control_out_u8(priv,
  247. MESSAGE_REQUEST_BBREG,
  248. 0xff,
  249. 0x03);
  250. /* CR251, TX I/Q Imbalance Calibration */
  251. vnt_control_out_u8(priv,
  252. MESSAGE_REQUEST_BBREG,
  253. 0xfb,
  254. calib_tx_iq);
  255. /* CR252, TX DC-Offset Calibration */
  256. vnt_control_out_u8(priv,
  257. MESSAGE_REQUEST_BBREG,
  258. 0xfC,
  259. calib_tx_dc);
  260. /* CR253, RX I/Q Imbalance Calibration */
  261. vnt_control_out_u8(priv,
  262. MESSAGE_REQUEST_BBREG,
  263. 0xfd,
  264. calib_rx_iq);
  265. } else {
  266. /* CR255, turn off
  267. BB Calibration compensation */
  268. vnt_control_out_u8(priv,
  269. MESSAGE_REQUEST_BBREG,
  270. 0xff,
  271. 0x0);
  272. }
  273. }
  274. }
  275. /* get permanent network address */
  276. memcpy(priv->permanent_net_addr, init_rsp->net_addr, 6);
  277. ether_addr_copy(priv->current_net_addr, priv->permanent_net_addr);
  278. /* if exist SW network address, use it */
  279. dev_dbg(&priv->usb->dev, "Network address = %pM\n",
  280. priv->current_net_addr);
  281. /*
  282. * set BB and packet type at the same time
  283. * set Short Slot Time, xIFS, and RSPINF
  284. */
  285. if (priv->bb_type == BB_TYPE_11A)
  286. priv->short_slot_time = true;
  287. else
  288. priv->short_slot_time = false;
  289. vnt_set_short_slot_time(priv);
  290. priv->radio_ctl = priv->eeprom[EEP_OFS_RADIOCTL];
  291. if ((priv->radio_ctl & EEP_RADIOCTL_ENABLE) != 0) {
  292. status = vnt_control_in(priv, MESSAGE_TYPE_READ,
  293. MAC_REG_GPIOCTL1, MESSAGE_REQUEST_MACREG, 1, &tmp);
  294. if (status != STATUS_SUCCESS)
  295. return false;
  296. if ((tmp & GPIO3_DATA) == 0)
  297. vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL1,
  298. GPIO3_INTMD);
  299. else
  300. vnt_mac_reg_bits_off(priv, MAC_REG_GPIOCTL1,
  301. GPIO3_INTMD);
  302. }
  303. vnt_mac_set_led(priv, LEDSTS_TMLEN, 0x38);
  304. vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
  305. vnt_mac_reg_bits_on(priv, MAC_REG_GPIOCTL0, 0x01);
  306. vnt_radio_power_on(priv);
  307. dev_dbg(&priv->usb->dev, "<----INIbInitAdapter Exit\n");
  308. return true;
  309. }
  310. static void vnt_free_tx_bufs(struct vnt_private *priv)
  311. {
  312. struct vnt_usb_send_context *tx_context;
  313. int ii;
  314. for (ii = 0; ii < priv->num_tx_context; ii++) {
  315. tx_context = priv->tx_context[ii];
  316. /* deallocate URBs */
  317. if (tx_context->urb) {
  318. usb_kill_urb(tx_context->urb);
  319. usb_free_urb(tx_context->urb);
  320. }
  321. kfree(tx_context);
  322. }
  323. }
  324. static void vnt_free_rx_bufs(struct vnt_private *priv)
  325. {
  326. struct vnt_rcb *rcb;
  327. int ii;
  328. for (ii = 0; ii < priv->num_rcb; ii++) {
  329. rcb = priv->rcb[ii];
  330. if (!rcb)
  331. continue;
  332. /* deallocate URBs */
  333. if (rcb->urb) {
  334. usb_kill_urb(rcb->urb);
  335. usb_free_urb(rcb->urb);
  336. }
  337. /* deallocate skb */
  338. if (rcb->skb)
  339. dev_kfree_skb(rcb->skb);
  340. kfree(rcb);
  341. }
  342. }
  343. static void usb_device_reset(struct vnt_private *priv)
  344. {
  345. int status;
  346. status = usb_reset_device(priv->usb);
  347. if (status)
  348. dev_warn(&priv->usb->dev,
  349. "usb_device_reset fail status=%d\n", status);
  350. }
  351. static void vnt_free_int_bufs(struct vnt_private *priv)
  352. {
  353. kfree(priv->int_buf.data_buf);
  354. }
  355. static bool vnt_alloc_bufs(struct vnt_private *priv)
  356. {
  357. struct vnt_usb_send_context *tx_context;
  358. struct vnt_rcb *rcb;
  359. int ii;
  360. for (ii = 0; ii < priv->num_tx_context; ii++) {
  361. tx_context = kmalloc(sizeof(struct vnt_usb_send_context),
  362. GFP_KERNEL);
  363. if (tx_context == NULL)
  364. goto free_tx;
  365. priv->tx_context[ii] = tx_context;
  366. tx_context->priv = priv;
  367. tx_context->pkt_no = ii;
  368. /* allocate URBs */
  369. tx_context->urb = usb_alloc_urb(0, GFP_ATOMIC);
  370. if (!tx_context->urb) {
  371. dev_err(&priv->usb->dev, "alloc tx urb failed\n");
  372. goto free_tx;
  373. }
  374. tx_context->in_use = false;
  375. }
  376. for (ii = 0; ii < priv->num_rcb; ii++) {
  377. priv->rcb[ii] = kzalloc(sizeof(struct vnt_rcb), GFP_KERNEL);
  378. if (!priv->rcb[ii]) {
  379. dev_err(&priv->usb->dev,
  380. "failed to allocate rcb no %d\n", ii);
  381. goto free_rx_tx;
  382. }
  383. rcb = priv->rcb[ii];
  384. rcb->priv = priv;
  385. /* allocate URBs */
  386. rcb->urb = usb_alloc_urb(0, GFP_ATOMIC);
  387. if (rcb->urb == NULL) {
  388. dev_err(&priv->usb->dev, "Failed to alloc rx urb\n");
  389. goto free_rx_tx;
  390. }
  391. rcb->skb = dev_alloc_skb(priv->rx_buf_sz);
  392. if (rcb->skb == NULL)
  393. goto free_rx_tx;
  394. rcb->in_use = false;
  395. /* submit rx urb */
  396. if (vnt_submit_rx_urb(priv, rcb))
  397. goto free_rx_tx;
  398. }
  399. priv->interrupt_urb = usb_alloc_urb(0, GFP_ATOMIC);
  400. if (priv->interrupt_urb == NULL) {
  401. dev_err(&priv->usb->dev, "Failed to alloc int urb\n");
  402. goto free_rx_tx;
  403. }
  404. priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
  405. if (priv->int_buf.data_buf == NULL) {
  406. usb_free_urb(priv->interrupt_urb);
  407. goto free_rx_tx;
  408. }
  409. return true;
  410. free_rx_tx:
  411. vnt_free_rx_bufs(priv);
  412. free_tx:
  413. vnt_free_tx_bufs(priv);
  414. return false;
  415. }
  416. static void vnt_tx_80211(struct ieee80211_hw *hw,
  417. struct ieee80211_tx_control *control, struct sk_buff *skb)
  418. {
  419. struct vnt_private *priv = hw->priv;
  420. if (vnt_tx_packet(priv, skb))
  421. ieee80211_free_txskb(hw, skb);
  422. }
  423. static int vnt_start(struct ieee80211_hw *hw)
  424. {
  425. struct vnt_private *priv = hw->priv;
  426. priv->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
  427. if (!vnt_alloc_bufs(priv)) {
  428. dev_dbg(&priv->usb->dev, "vnt_alloc_bufs fail...\n");
  429. return -ENOMEM;
  430. }
  431. clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
  432. if (vnt_init_registers(priv) == false) {
  433. dev_dbg(&priv->usb->dev, " init register fail\n");
  434. goto free_all;
  435. }
  436. if (vnt_key_init_table(priv))
  437. goto free_all;
  438. priv->int_interval = 1; /* bInterval is set to 1 */
  439. vnt_int_start_interrupt(priv);
  440. ieee80211_wake_queues(hw);
  441. return 0;
  442. free_all:
  443. vnt_free_rx_bufs(priv);
  444. vnt_free_tx_bufs(priv);
  445. vnt_free_int_bufs(priv);
  446. usb_kill_urb(priv->interrupt_urb);
  447. usb_free_urb(priv->interrupt_urb);
  448. return -ENOMEM;
  449. }
  450. static void vnt_stop(struct ieee80211_hw *hw)
  451. {
  452. struct vnt_private *priv = hw->priv;
  453. int i;
  454. if (!priv)
  455. return;
  456. for (i = 0; i < MAX_KEY_TABLE; i++)
  457. vnt_mac_disable_keyentry(priv, i);
  458. /* clear all keys */
  459. priv->key_entry_inuse = 0;
  460. if (!test_bit(DEVICE_FLAGS_UNPLUG, &priv->flags))
  461. vnt_mac_shutdown(priv);
  462. ieee80211_stop_queues(hw);
  463. set_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
  464. cancel_delayed_work_sync(&priv->run_command_work);
  465. priv->cmd_running = false;
  466. vnt_free_tx_bufs(priv);
  467. vnt_free_rx_bufs(priv);
  468. vnt_free_int_bufs(priv);
  469. usb_kill_urb(priv->interrupt_urb);
  470. usb_free_urb(priv->interrupt_urb);
  471. }
  472. static int vnt_add_interface(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  473. {
  474. struct vnt_private *priv = hw->priv;
  475. priv->vif = vif;
  476. switch (vif->type) {
  477. case NL80211_IFTYPE_STATION:
  478. break;
  479. case NL80211_IFTYPE_ADHOC:
  480. vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
  481. vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
  482. break;
  483. case NL80211_IFTYPE_AP:
  484. vnt_mac_reg_bits_off(priv, MAC_REG_RCR, RCR_UNICAST);
  485. vnt_mac_reg_bits_on(priv, MAC_REG_HOSTCR, HOSTCR_AP);
  486. break;
  487. default:
  488. return -EOPNOTSUPP;
  489. }
  490. priv->op_mode = vif->type;
  491. vnt_set_bss_mode(priv);
  492. /* LED blink on TX */
  493. vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_INTER);
  494. return 0;
  495. }
  496. static void vnt_remove_interface(struct ieee80211_hw *hw,
  497. struct ieee80211_vif *vif)
  498. {
  499. struct vnt_private *priv = hw->priv;
  500. switch (vif->type) {
  501. case NL80211_IFTYPE_STATION:
  502. break;
  503. case NL80211_IFTYPE_ADHOC:
  504. vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
  505. vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
  506. vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_ADHOC);
  507. break;
  508. case NL80211_IFTYPE_AP:
  509. vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
  510. vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
  511. vnt_mac_reg_bits_off(priv, MAC_REG_HOSTCR, HOSTCR_AP);
  512. break;
  513. default:
  514. break;
  515. }
  516. vnt_radio_power_off(priv);
  517. priv->op_mode = NL80211_IFTYPE_UNSPECIFIED;
  518. /* LED slow blink */
  519. vnt_mac_set_led(priv, LEDSTS_STS, LEDSTS_SLOW);
  520. }
  521. static int vnt_config(struct ieee80211_hw *hw, u32 changed)
  522. {
  523. struct vnt_private *priv = hw->priv;
  524. struct ieee80211_conf *conf = &hw->conf;
  525. u8 bb_type;
  526. if (changed & IEEE80211_CONF_CHANGE_PS) {
  527. if (conf->flags & IEEE80211_CONF_PS)
  528. vnt_enable_power_saving(priv, conf->listen_interval);
  529. else
  530. vnt_disable_power_saving(priv);
  531. }
  532. if ((changed & IEEE80211_CONF_CHANGE_CHANNEL) ||
  533. (conf->flags & IEEE80211_CONF_OFFCHANNEL)) {
  534. vnt_set_channel(priv, conf->chandef.chan->hw_value);
  535. if (conf->chandef.chan->band == IEEE80211_BAND_5GHZ)
  536. bb_type = BB_TYPE_11A;
  537. else
  538. bb_type = BB_TYPE_11G;
  539. if (priv->bb_type != bb_type) {
  540. priv->bb_type = bb_type;
  541. vnt_set_bss_mode(priv);
  542. }
  543. }
  544. if (changed & IEEE80211_CONF_CHANGE_POWER) {
  545. if (priv->bb_type == BB_TYPE_11B)
  546. priv->current_rate = RATE_1M;
  547. else
  548. priv->current_rate = RATE_54M;
  549. vnt_rf_setpower(priv, priv->current_rate,
  550. conf->chandef.chan->hw_value);
  551. }
  552. return 0;
  553. }
  554. static void vnt_bss_info_changed(struct ieee80211_hw *hw,
  555. struct ieee80211_vif *vif, struct ieee80211_bss_conf *conf,
  556. u32 changed)
  557. {
  558. struct vnt_private *priv = hw->priv;
  559. priv->current_aid = conf->aid;
  560. if (changed & BSS_CHANGED_BSSID && conf->bssid)
  561. vnt_mac_set_bssid_addr(priv, (u8 *)conf->bssid);
  562. if (changed & BSS_CHANGED_BASIC_RATES) {
  563. priv->basic_rates = conf->basic_rates;
  564. vnt_update_top_rates(priv);
  565. dev_dbg(&priv->usb->dev, "basic rates %x\n", conf->basic_rates);
  566. }
  567. if (changed & BSS_CHANGED_ERP_PREAMBLE) {
  568. if (conf->use_short_preamble) {
  569. vnt_mac_enable_barker_preamble_mode(priv);
  570. priv->preamble_type = true;
  571. } else {
  572. vnt_mac_disable_barker_preamble_mode(priv);
  573. priv->preamble_type = false;
  574. }
  575. }
  576. if (changed & BSS_CHANGED_ERP_CTS_PROT) {
  577. if (conf->use_cts_prot)
  578. vnt_mac_enable_protect_mode(priv);
  579. else
  580. vnt_mac_disable_protect_mode(priv);
  581. }
  582. if (changed & BSS_CHANGED_ERP_SLOT) {
  583. if (conf->use_short_slot)
  584. priv->short_slot_time = true;
  585. else
  586. priv->short_slot_time = false;
  587. vnt_set_short_slot_time(priv);
  588. vnt_set_vga_gain_offset(priv, priv->bb_vga[0]);
  589. vnt_update_pre_ed_threshold(priv, false);
  590. }
  591. if (changed & BSS_CHANGED_TXPOWER)
  592. vnt_rf_setpower(priv, priv->current_rate,
  593. conf->chandef.chan->hw_value);
  594. if (changed & BSS_CHANGED_BEACON_ENABLED) {
  595. dev_dbg(&priv->usb->dev,
  596. "Beacon enable %d\n", conf->enable_beacon);
  597. if (conf->enable_beacon) {
  598. vnt_beacon_enable(priv, vif, conf);
  599. vnt_mac_reg_bits_on(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
  600. } else {
  601. vnt_mac_reg_bits_off(priv, MAC_REG_TCR, TCR_AUTOBCNTX);
  602. }
  603. }
  604. if (changed & (BSS_CHANGED_ASSOC | BSS_CHANGED_BEACON_INFO) &&
  605. priv->op_mode != NL80211_IFTYPE_AP) {
  606. if (conf->assoc && conf->beacon_rate) {
  607. vnt_mac_reg_bits_on(priv, MAC_REG_TFTCTL,
  608. TFTCTL_TSFCNTREN);
  609. vnt_adjust_tsf(priv, conf->beacon_rate->hw_value,
  610. conf->sync_tsf, priv->current_tsf);
  611. vnt_mac_set_beacon_interval(priv, conf->beacon_int);
  612. vnt_reset_next_tbtt(priv, conf->beacon_int);
  613. } else {
  614. vnt_clear_current_tsf(priv);
  615. vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL,
  616. TFTCTL_TSFCNTREN);
  617. }
  618. }
  619. }
  620. static u64 vnt_prepare_multicast(struct ieee80211_hw *hw,
  621. struct netdev_hw_addr_list *mc_list)
  622. {
  623. struct vnt_private *priv = hw->priv;
  624. struct netdev_hw_addr *ha;
  625. u64 mc_filter = 0;
  626. u32 bit_nr = 0;
  627. netdev_hw_addr_list_for_each(ha, mc_list) {
  628. bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
  629. mc_filter |= 1ULL << (bit_nr & 0x3f);
  630. }
  631. priv->mc_list_count = mc_list->count;
  632. return mc_filter;
  633. }
  634. static void vnt_configure(struct ieee80211_hw *hw,
  635. unsigned int changed_flags, unsigned int *total_flags, u64 multicast)
  636. {
  637. struct vnt_private *priv = hw->priv;
  638. u8 rx_mode = 0;
  639. int rc;
  640. *total_flags &= FIF_ALLMULTI | FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC;
  641. rc = vnt_control_in(priv, MESSAGE_TYPE_READ, MAC_REG_RCR,
  642. MESSAGE_REQUEST_MACREG, sizeof(u8), &rx_mode);
  643. if (!rc)
  644. rx_mode = RCR_MULTICAST | RCR_BROADCAST;
  645. dev_dbg(&priv->usb->dev, "rx mode in = %x\n", rx_mode);
  646. if (changed_flags & FIF_ALLMULTI) {
  647. if (*total_flags & FIF_ALLMULTI) {
  648. if (priv->mc_list_count > 2)
  649. vnt_mac_set_filter(priv, ~0);
  650. else
  651. vnt_mac_set_filter(priv, multicast);
  652. rx_mode |= RCR_MULTICAST | RCR_BROADCAST;
  653. } else {
  654. rx_mode &= ~(RCR_MULTICAST | RCR_BROADCAST);
  655. }
  656. }
  657. if (changed_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC)) {
  658. if (*total_flags & (FIF_OTHER_BSS | FIF_BCN_PRBRESP_PROMISC))
  659. rx_mode &= ~RCR_BSSID;
  660. else
  661. rx_mode |= RCR_BSSID;
  662. }
  663. vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG, MAC_REG_RCR, rx_mode);
  664. dev_dbg(&priv->usb->dev, "rx mode out= %x\n", rx_mode);
  665. }
  666. static int vnt_set_key(struct ieee80211_hw *hw, enum set_key_cmd cmd,
  667. struct ieee80211_vif *vif, struct ieee80211_sta *sta,
  668. struct ieee80211_key_conf *key)
  669. {
  670. struct vnt_private *priv = hw->priv;
  671. switch (cmd) {
  672. case SET_KEY:
  673. if (vnt_set_keys(hw, sta, vif, key))
  674. return -EOPNOTSUPP;
  675. break;
  676. case DISABLE_KEY:
  677. if (test_bit(key->hw_key_idx, &priv->key_entry_inuse))
  678. clear_bit(key->hw_key_idx, &priv->key_entry_inuse);
  679. default:
  680. break;
  681. }
  682. return 0;
  683. }
  684. static void vnt_sw_scan_start(struct ieee80211_hw *hw,
  685. struct ieee80211_vif *vif,
  686. const u8 *addr)
  687. {
  688. struct vnt_private *priv = hw->priv;
  689. vnt_set_bss_mode(priv);
  690. /* Set max sensitivity*/
  691. vnt_update_pre_ed_threshold(priv, true);
  692. }
  693. static void vnt_sw_scan_complete(struct ieee80211_hw *hw,
  694. struct ieee80211_vif *vif)
  695. {
  696. struct vnt_private *priv = hw->priv;
  697. /* Return sensitivity to channel level*/
  698. vnt_update_pre_ed_threshold(priv, false);
  699. }
  700. static int vnt_get_stats(struct ieee80211_hw *hw,
  701. struct ieee80211_low_level_stats *stats)
  702. {
  703. struct vnt_private *priv = hw->priv;
  704. memcpy(stats, &priv->low_stats, sizeof(*stats));
  705. return 0;
  706. }
  707. static u64 vnt_get_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  708. {
  709. struct vnt_private *priv = hw->priv;
  710. return priv->current_tsf;
  711. }
  712. static void vnt_set_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif,
  713. u64 tsf)
  714. {
  715. struct vnt_private *priv = hw->priv;
  716. vnt_update_next_tbtt(priv, tsf, vif->bss_conf.beacon_int);
  717. }
  718. static void vnt_reset_tsf(struct ieee80211_hw *hw, struct ieee80211_vif *vif)
  719. {
  720. struct vnt_private *priv = hw->priv;
  721. vnt_mac_reg_bits_off(priv, MAC_REG_TFTCTL, TFTCTL_TSFCNTREN);
  722. vnt_clear_current_tsf(priv);
  723. }
  724. static const struct ieee80211_ops vnt_mac_ops = {
  725. .tx = vnt_tx_80211,
  726. .start = vnt_start,
  727. .stop = vnt_stop,
  728. .add_interface = vnt_add_interface,
  729. .remove_interface = vnt_remove_interface,
  730. .config = vnt_config,
  731. .bss_info_changed = vnt_bss_info_changed,
  732. .prepare_multicast = vnt_prepare_multicast,
  733. .configure_filter = vnt_configure,
  734. .set_key = vnt_set_key,
  735. .sw_scan_start = vnt_sw_scan_start,
  736. .sw_scan_complete = vnt_sw_scan_complete,
  737. .get_stats = vnt_get_stats,
  738. .get_tsf = vnt_get_tsf,
  739. .set_tsf = vnt_set_tsf,
  740. .reset_tsf = vnt_reset_tsf,
  741. };
  742. int vnt_init(struct vnt_private *priv)
  743. {
  744. if (!(vnt_init_registers(priv)))
  745. return -EAGAIN;
  746. SET_IEEE80211_PERM_ADDR(priv->hw, priv->permanent_net_addr);
  747. vnt_init_bands(priv);
  748. if (ieee80211_register_hw(priv->hw))
  749. return -ENODEV;
  750. priv->mac_hw = true;
  751. vnt_radio_power_off(priv);
  752. return 0;
  753. }
  754. static int
  755. vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
  756. {
  757. struct usb_device *udev;
  758. struct vnt_private *priv;
  759. struct ieee80211_hw *hw;
  760. struct wiphy *wiphy;
  761. int rc = 0;
  762. udev = usb_get_dev(interface_to_usbdev(intf));
  763. dev_notice(&udev->dev, "%s Ver. %s\n",
  764. DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
  765. dev_notice(&udev->dev,
  766. "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
  767. hw = ieee80211_alloc_hw(sizeof(struct vnt_private), &vnt_mac_ops);
  768. if (!hw) {
  769. dev_err(&udev->dev, "could not register ieee80211_hw\n");
  770. rc = -ENOMEM;
  771. goto err_nomem;
  772. }
  773. priv = hw->priv;
  774. priv->hw = hw;
  775. priv->usb = udev;
  776. vnt_set_options(priv);
  777. spin_lock_init(&priv->lock);
  778. mutex_init(&priv->usb_lock);
  779. INIT_DELAYED_WORK(&priv->run_command_work, vnt_run_command);
  780. usb_set_intfdata(intf, priv);
  781. wiphy = priv->hw->wiphy;
  782. wiphy->frag_threshold = FRAG_THRESH_DEF;
  783. wiphy->rts_threshold = RTS_THRESH_DEF;
  784. wiphy->interface_modes = BIT(NL80211_IFTYPE_STATION) |
  785. BIT(NL80211_IFTYPE_ADHOC) | BIT(NL80211_IFTYPE_AP);
  786. ieee80211_hw_set(priv->hw, TIMING_BEACON_ONLY);
  787. ieee80211_hw_set(priv->hw, SIGNAL_DBM);
  788. ieee80211_hw_set(priv->hw, RX_INCLUDES_FCS);
  789. ieee80211_hw_set(priv->hw, REPORTS_TX_ACK_STATUS);
  790. ieee80211_hw_set(priv->hw, SUPPORTS_PS);
  791. priv->hw->max_signal = 100;
  792. SET_IEEE80211_DEV(priv->hw, &intf->dev);
  793. usb_device_reset(priv);
  794. clear_bit(DEVICE_FLAGS_DISCONNECTED, &priv->flags);
  795. vnt_reset_command_timer(priv);
  796. vnt_schedule_command(priv, WLAN_CMD_INIT_MAC80211);
  797. return 0;
  798. err_nomem:
  799. usb_put_dev(udev);
  800. return rc;
  801. }
  802. static void vt6656_disconnect(struct usb_interface *intf)
  803. {
  804. struct vnt_private *priv = usb_get_intfdata(intf);
  805. if (!priv)
  806. return;
  807. if (priv->mac_hw)
  808. ieee80211_unregister_hw(priv->hw);
  809. usb_set_intfdata(intf, NULL);
  810. usb_put_dev(interface_to_usbdev(intf));
  811. set_bit(DEVICE_FLAGS_UNPLUG, &priv->flags);
  812. ieee80211_free_hw(priv->hw);
  813. }
  814. #ifdef CONFIG_PM
  815. static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
  816. {
  817. return 0;
  818. }
  819. static int vt6656_resume(struct usb_interface *intf)
  820. {
  821. return 0;
  822. }
  823. #endif /* CONFIG_PM */
  824. MODULE_DEVICE_TABLE(usb, vt6656_table);
  825. static struct usb_driver vt6656_driver = {
  826. .name = DEVICE_NAME,
  827. .probe = vt6656_probe,
  828. .disconnect = vt6656_disconnect,
  829. .id_table = vt6656_table,
  830. #ifdef CONFIG_PM
  831. .suspend = vt6656_suspend,
  832. .resume = vt6656_resume,
  833. #endif /* CONFIG_PM */
  834. };
  835. module_usb_driver(vt6656_driver);