2 * Copyright (c) 1996, 2003 VIA Networking Technologies, Inc.
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.
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.
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.
21 * Purpose: driver entry for initial, open, close, tx and rx.
29 * vt6656_probe - module initial (insmod) driver entry
30 * device_remove1 - module remove entry
31 * device_open - allocate dma/descripter resource & initial mac/bbp function
32 * device_xmit - asynchronous data tx function
33 * device_set_multi - set mac filter
34 * device_ioctl - ioctl entry
35 * device_close - shutdown mac/bbp & free dma/descriptor resource
36 * device_alloc_frag_buf - rx fragement pre-allocated function
37 * device_free_tx_bufs - free tx buffer function
38 * device_dma0_tx_80211- tx 802.11 frame via dma0
39 * device_dma0_xmit- tx PS buffered frame via dma0
40 * device_init_registers- initial MAC & BBP & RF internal registers.
41 * device_init_rings- initial tx/rx ring buffer
42 * device_init_defrag_cb- initial & allocate de-fragement buffer.
43 * device_tx_srv- tx interrupt service function
49 #include <linux/file.h>
73 /* static int msglevel = MSG_LEVEL_DEBUG; */
74 static int msglevel =MSG_LEVEL_INFO;
77 * define module options
80 /* version information */
81 #define DRIVER_AUTHOR \
82 "VIA Networking Technologies, Inc., <lyndonchen@vntek.com.tw>"
83 MODULE_AUTHOR(DRIVER_AUTHOR);
84 MODULE_LICENSE("GPL");
85 MODULE_DESCRIPTION(DEVICE_FULL_DRV_NAM);
87 #define DEVICE_PARAM(N,D) \
88 static int N[MAX_UINTS]=OPTION_DEFAULT;\
89 module_param_array(N, int, NULL, 0);\
90 MODULE_PARM_DESC(N, D);
92 #define RX_DESC_DEF0 64
93 DEVICE_PARAM(RxDescriptors0,"Number of receive usb desc buffer");
95 #define TX_DESC_DEF0 64
96 DEVICE_PARAM(TxDescriptors0,"Number of transmit usb desc buffer");
99 DEVICE_PARAM(Channel, "Channel number");
101 /* PreambleType[] is the preamble length used for transmit.
102 0: indicate allows long preamble type
103 1: indicate allows short preamble type
106 #define PREAMBLE_TYPE_DEF 1
108 DEVICE_PARAM(PreambleType, "Preamble Type");
110 #define RTS_THRESH_DEF 2347
111 DEVICE_PARAM(RTSThreshold, "RTS threshold");
113 #define FRAG_THRESH_DEF 2346
114 DEVICE_PARAM(FragThreshold, "Fragmentation threshold");
116 #define DATA_RATE_DEF 13
118 0: indicate 1 Mbps 0x02
119 1: indicate 2 Mbps 0x04
120 2: indicate 5.5 Mbps 0x0B
121 3: indicate 11 Mbps 0x16
122 4: indicate 6 Mbps 0x0c
123 5: indicate 9 Mbps 0x12
124 6: indicate 12 Mbps 0x18
125 7: indicate 18 Mbps 0x24
126 8: indicate 24 Mbps 0x30
127 9: indicate 36 Mbps 0x48
128 10: indicate 48 Mbps 0x60
129 11: indicate 54 Mbps 0x6c
130 12: indicate 72 Mbps 0x90
131 13: indicate auto rate
134 DEVICE_PARAM(ConnectionRate, "Connection data rate");
136 #define OP_MODE_DEF 0
137 DEVICE_PARAM(OPMode, "Infrastruct, adhoc, AP mode ");
139 /* OpMode[] is used for transmit.
140 0: indicate infrastruct mode used
141 1: indicate adhoc mode used
142 2: indicate AP mode used
146 0: indicate disable power saving mode
147 1: indicate enable power saving mode
150 #define PS_MODE_DEF 0
151 DEVICE_PARAM(PSMode, "Power saving mode");
153 #define SHORT_RETRY_DEF 8
154 DEVICE_PARAM(ShortRetryLimit, "Short frame retry limits");
156 #define LONG_RETRY_DEF 4
157 DEVICE_PARAM(LongRetryLimit, "long frame retry limits");
159 /* BasebandType[] baseband type selected
160 0: indicate 802.11a type
161 1: indicate 802.11b type
162 2: indicate 802.11g type
165 #define BBP_TYPE_DEF 2
166 DEVICE_PARAM(BasebandType, "baseband type");
169 0: indicate disable 802.11h
170 1: indicate enable 802.11h
173 #define X80211h_MODE_DEF 0
175 DEVICE_PARAM(b80211hEnable, "802.11h mode");
178 * Static vars definitions
181 static struct usb_device_id vt6656_table[] = {
182 {USB_DEVICE(VNT_USB_VENDOR_ID, VNT_USB_PRODUCT_ID)},
186 /* frequency list (map channels to frequencies) */
188 static const long frequency_list[] = {
189 2412, 2417, 2422, 2427, 2432, 2437, 2442, 2447, 2452, 2457, 2462, 2467, 2472, 2484,
190 4915, 4920, 4925, 4935, 4940, 4945, 4960, 4980,
191 5035, 5040, 5045, 5055, 5060, 5080, 5170, 5180, 5190, 5200, 5210, 5220, 5230, 5240,
192 5260, 5280, 5300, 5320, 5500, 5520, 5540, 5560, 5580, 5600, 5620, 5640, 5660, 5680,
193 5700, 5745, 5765, 5785, 5805, 5825
196 static const struct iw_handler_def iwctl_handler_def;
199 static int vt6656_probe(struct usb_interface *intf,
200 const struct usb_device_id *id);
201 static void vt6656_disconnect(struct usb_interface *intf);
203 #ifdef CONFIG_PM /* Minimal support for suspend and resume */
204 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message);
205 static int vt6656_resume(struct usb_interface *intf);
206 #endif /* CONFIG_PM */
208 static struct net_device_stats *device_get_stats(struct net_device *dev);
209 static int device_open(struct net_device *dev);
210 static int device_xmit(struct sk_buff *skb, struct net_device *dev);
211 static void device_set_multi(struct net_device *dev);
212 static int device_close(struct net_device *dev);
213 static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
215 static int device_init_registers(struct vnt_private *pDevice);
216 static bool device_init_defrag_cb(struct vnt_private *pDevice);
218 static int ethtool_ioctl(struct net_device *dev, struct ifreq *);
219 static void device_free_tx_bufs(struct vnt_private *pDevice);
220 static void device_free_rx_bufs(struct vnt_private *pDevice);
221 static void device_free_int_bufs(struct vnt_private *pDevice);
222 static void device_free_frag_bufs(struct vnt_private *pDevice);
223 static bool device_alloc_bufs(struct vnt_private *pDevice);
225 static int Read_config_file(struct vnt_private *pDevice);
226 static unsigned char *Config_FileOperation(struct vnt_private *pDevice);
227 static int Config_FileGetParameter(unsigned char *string,
229 unsigned char *source);
231 static void usb_device_reset(struct vnt_private *pDevice);
234 device_set_options(struct vnt_private *pDevice) {
236 u8 abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
237 u8 abySNAP_RFC1042[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0x00};
238 u8 abySNAP_Bridgetunnel[ETH_ALEN] = {0xAA, 0xAA, 0x03, 0x00, 0x00, 0xF8};
240 memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
241 memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
242 memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
244 pDevice->cbTD = TX_DESC_DEF0;
245 pDevice->cbRD = RX_DESC_DEF0;
246 pDevice->uChannel = CHANNEL_DEF;
247 pDevice->wRTSThreshold = RTS_THRESH_DEF;
248 pDevice->wFragmentationThreshold = FRAG_THRESH_DEF;
249 pDevice->byShortRetryLimit = SHORT_RETRY_DEF;
250 pDevice->byLongRetryLimit = LONG_RETRY_DEF;
251 pDevice->wMaxTransmitMSDULifetime = DEFAULT_MSDU_LIFETIME;
252 pDevice->byShortPreamble = PREAMBLE_TYPE_DEF;
253 pDevice->ePSMode = PS_MODE_DEF;
254 pDevice->b11hEnable = X80211h_MODE_DEF;
255 pDevice->op_mode = NL80211_IFTYPE_UNSPECIFIED;
256 pDevice->uConnectionRate = DATA_RATE_DEF;
257 if (pDevice->uConnectionRate < RATE_AUTO) pDevice->bFixRate = true;
258 pDevice->byBBType = BBP_TYPE_DEF;
259 pDevice->byPacketType = pDevice->byBBType;
260 pDevice->byAutoFBCtrl = AUTO_FB_0;
261 pDevice->byPreambleType = 0;
262 pDevice->bExistSWNetAddr = false;
266 * initialization of MAC & BBP registers
268 static int device_init_registers(struct vnt_private *pDevice)
270 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
271 struct vnt_cmd_card_init *init_cmd = &pDevice->init_command;
272 struct vnt_rsp_card_init *init_rsp = &pDevice->init_response;
273 u8 abyBroadcastAddr[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
274 u8 abySNAP_RFC1042[ETH_ALEN] = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0x00};
275 u8 abySNAP_Bridgetunnel[ETH_ALEN]
276 = {0xaa, 0xaa, 0x03, 0x00, 0x00, 0xf8};
279 int ntStatus = STATUS_SUCCESS;
281 u8 byCalibTXIQ = 0, byCalibTXDC = 0, byCalibRXIQ = 0;
283 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "---->INIbInitAdapter. [%d][%d]\n",
284 DEVICE_INIT_COLD, pDevice->byPacketType);
286 memcpy(pDevice->abyBroadcastAddr, abyBroadcastAddr, ETH_ALEN);
287 memcpy(pDevice->abySNAP_RFC1042, abySNAP_RFC1042, ETH_ALEN);
288 memcpy(pDevice->abySNAP_Bridgetunnel, abySNAP_Bridgetunnel, ETH_ALEN);
290 if (!FIRMWAREbCheckVersion(pDevice)) {
291 if (FIRMWAREbDownload(pDevice) == true) {
292 if (FIRMWAREbBrach2Sram(pDevice) == false) {
293 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
294 " FIRMWAREbBrach2Sram fail\n");
298 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
299 " FIRMWAREbDownload fail\n");
304 if (!BBbVT3184Init(pDevice)) {
305 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" BBbVT3184Init fail\n");
309 init_cmd->init_class = DEVICE_INIT_COLD;
310 init_cmd->exist_sw_net_addr = (u8) pDevice->bExistSWNetAddr;
311 for (ii = 0; ii < 6; ii++)
312 init_cmd->sw_net_addr[ii] = pDevice->abyCurrentNetAddr[ii];
313 init_cmd->short_retry_limit = pDevice->byShortRetryLimit;
314 init_cmd->long_retry_limit = pDevice->byLongRetryLimit;
316 /* issue card_init command to device */
317 ntStatus = vnt_control_out(pDevice,
318 MESSAGE_TYPE_CARDINIT, 0, 0,
319 sizeof(struct vnt_cmd_card_init), (u8 *)init_cmd);
320 if (ntStatus != STATUS_SUCCESS) {
321 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO" Issue Card init fail\n");
325 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_INIT_RSP, 0, 0,
326 sizeof(struct vnt_rsp_card_init), (u8 *)init_rsp);
327 if (ntStatus != STATUS_SUCCESS) {
328 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
329 "Cardinit request in status fail!\n");
333 /* local ID for AES functions */
334 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_READ,
335 MAC_REG_LOCALID, MESSAGE_REQUEST_MACREG, 1,
336 &pDevice->byLocalID);
337 if (ntStatus != STATUS_SUCCESS)
340 /* do MACbSoftwareReset in MACvInitialize */
342 pDevice->bProtectMode = false;
343 /* only used in 11g type, sync with ERP IE */
344 pDevice->bNonERPPresent = false;
345 pDevice->bBarkerPreambleMd = false;
346 if (pDevice->bFixRate) {
347 pDevice->wCurrentRate = (u16)pDevice->uConnectionRate;
349 if (pDevice->byBBType == BB_TYPE_11B)
350 pDevice->wCurrentRate = RATE_11M;
352 pDevice->wCurrentRate = RATE_54M;
355 CHvInitChannelTable(pDevice);
357 pDevice->byTopOFDMBasicRate = RATE_24M;
358 pDevice->byTopCCKBasicRate = RATE_1M;
360 /* target to IF pin while programming to RF chip */
361 pDevice->byCurPwr = 0xFF;
363 pDevice->byCCKPwr = pDevice->abyEEPROM[EEP_OFS_PWR_CCK];
364 pDevice->byOFDMPwrG = pDevice->abyEEPROM[EEP_OFS_PWR_OFDMG];
365 /* load power table */
366 for (ii = 0; ii < 14; ii++) {
367 pDevice->abyCCKPwrTbl[ii] =
368 pDevice->abyEEPROM[ii + EEP_OFS_CCK_PWR_TBL];
370 if (pDevice->abyCCKPwrTbl[ii] == 0)
371 pDevice->abyCCKPwrTbl[ii] = pDevice->byCCKPwr;
372 pDevice->abyOFDMPwrTbl[ii] =
373 pDevice->abyEEPROM[ii + EEP_OFS_OFDM_PWR_TBL];
374 if (pDevice->abyOFDMPwrTbl[ii] == 0)
375 pDevice->abyOFDMPwrTbl[ii] = pDevice->byOFDMPwrG;
379 * original zonetype is USA, but custom zonetype is Europe,
380 * then need to recover 12, 13, 14 channels with 11 channel
382 if (((pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Japan) ||
383 (pDevice->abyEEPROM[EEP_OFS_ZONETYPE] == ZoneType_Europe)) &&
384 (pDevice->byOriginalZonetype == ZoneType_USA)) {
385 for (ii = 11; ii < 14; ii++) {
386 pDevice->abyCCKPwrTbl[ii] = pDevice->abyCCKPwrTbl[10];
387 pDevice->abyOFDMPwrTbl[ii] = pDevice->abyOFDMPwrTbl[10];
391 pDevice->byOFDMPwrA = 0x34; /* same as RFbMA2829SelectChannel */
393 /* load OFDM A power table */
394 for (ii = 0; ii < CB_MAX_CHANNEL_5G; ii++) {
395 pDevice->abyOFDMAPwrTbl[ii] =
396 pDevice->abyEEPROM[ii + EEP_OFS_OFDMA_PWR_TBL];
398 if (pDevice->abyOFDMAPwrTbl[ii] == 0)
399 pDevice->abyOFDMAPwrTbl[ii] = pDevice->byOFDMPwrA;
402 byAntenna = pDevice->abyEEPROM[EEP_OFS_ANTENNA];
404 if (byAntenna & EEP_ANTINV)
405 pDevice->bTxRxAntInv = true;
407 pDevice->bTxRxAntInv = false;
409 byAntenna &= (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
411 if (byAntenna == 0) /* if not set default is both */
412 byAntenna = (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN);
414 if (byAntenna == (EEP_ANTENNA_AUX | EEP_ANTENNA_MAIN)) {
415 pDevice->byAntennaCount = 2;
416 pDevice->byTxAntennaMode = ANT_B;
417 pDevice->dwTxAntennaSel = 1;
418 pDevice->dwRxAntennaSel = 1;
420 if (pDevice->bTxRxAntInv == true)
421 pDevice->byRxAntennaMode = ANT_A;
423 pDevice->byRxAntennaMode = ANT_B;
425 pDevice->byAntennaCount = 1;
426 pDevice->dwTxAntennaSel = 0;
427 pDevice->dwRxAntennaSel = 0;
429 if (byAntenna & EEP_ANTENNA_AUX) {
430 pDevice->byTxAntennaMode = ANT_A;
432 if (pDevice->bTxRxAntInv == true)
433 pDevice->byRxAntennaMode = ANT_B;
435 pDevice->byRxAntennaMode = ANT_A;
437 pDevice->byTxAntennaMode = ANT_B;
439 if (pDevice->bTxRxAntInv == true)
440 pDevice->byRxAntennaMode = ANT_A;
442 pDevice->byRxAntennaMode = ANT_B;
446 /* get Auto Fall Back type */
447 pDevice->byAutoFBCtrl = AUTO_FB_0;
449 /* default Auto Mode */
450 /* pDevice->NetworkType = Ndis802_11Automode; */
451 pDevice->eConfigPHYMode = PHY_TYPE_AUTO;
452 pDevice->byBBType = BB_TYPE_11G;
454 /* get channel range */
455 pDevice->byMinChannel = 1;
456 pDevice->byMaxChannel = CB_MAX_CHANNEL;
459 pDevice->byRFType = init_rsp->rf_type;
461 /* load vt3266 calibration parameters in EEPROM */
462 if (pDevice->byRFType == RF_VT3226D0) {
463 if ((pDevice->abyEEPROM[EEP_OFS_MAJOR_VER] == 0x1) &&
464 (pDevice->abyEEPROM[EEP_OFS_MINOR_VER] >= 0x4)) {
466 byCalibTXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_IQ];
467 byCalibTXDC = pDevice->abyEEPROM[EEP_OFS_CALIB_TX_DC];
468 byCalibRXIQ = pDevice->abyEEPROM[EEP_OFS_CALIB_RX_IQ];
469 if (byCalibTXIQ || byCalibTXDC || byCalibRXIQ) {
470 /* CR255, enable TX/RX IQ and DC compensation mode */
471 vnt_control_out_u8(pDevice,
472 MESSAGE_REQUEST_BBREG,
475 /* CR251, TX I/Q Imbalance Calibration */
476 vnt_control_out_u8(pDevice,
477 MESSAGE_REQUEST_BBREG,
480 /* CR252, TX DC-Offset Calibration */
481 vnt_control_out_u8(pDevice,
482 MESSAGE_REQUEST_BBREG,
485 /* CR253, RX I/Q Imbalance Calibration */
486 vnt_control_out_u8(pDevice,
487 MESSAGE_REQUEST_BBREG,
491 /* CR255, turn off BB Calibration compensation */
492 vnt_control_out_u8(pDevice,
493 MESSAGE_REQUEST_BBREG,
500 pMgmt->eScanType = WMAC_SCAN_PASSIVE;
501 pMgmt->uCurrChannel = pDevice->uChannel;
502 pMgmt->uIBSSChannel = pDevice->uChannel;
503 CARDbSetMediaChannel(pDevice, pMgmt->uCurrChannel);
505 /* get permanent network address */
506 memcpy(pDevice->abyPermanentNetAddr, init_rsp->net_addr, 6);
507 memcpy(pDevice->abyCurrentNetAddr,
508 pDevice->abyPermanentNetAddr, ETH_ALEN);
510 /* if exist SW network address, use it */
511 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"Network address = %pM\n",
512 pDevice->abyCurrentNetAddr);
515 * set BB and packet type at the same time
516 * set Short Slot Time, xIFS, and RSPINF
518 if (pDevice->byBBType == BB_TYPE_11A) {
519 CARDbAddBasicRate(pDevice, RATE_6M);
520 pDevice->bShortSlotTime = true;
522 CARDbAddBasicRate(pDevice, RATE_1M);
523 pDevice->bShortSlotTime = false;
526 BBvSetShortSlotTime(pDevice);
527 CARDvSetBSSMode(pDevice);
529 pDevice->byBBVGACurrent = pDevice->abyBBVGA[0];
530 pDevice->byBBVGANew = pDevice->byBBVGACurrent;
532 BBvSetVGAGainOffset(pDevice, pDevice->abyBBVGA[0]);
534 pDevice->byRadioCtl = pDevice->abyEEPROM[EEP_OFS_RADIOCTL];
535 pDevice->bHWRadioOff = false;
537 if ((pDevice->byRadioCtl & EEP_RADIOCTL_ENABLE) != 0) {
538 ntStatus = vnt_control_in(pDevice, MESSAGE_TYPE_READ,
539 MAC_REG_GPIOCTL1, MESSAGE_REQUEST_MACREG, 1, &byTmp);
541 if (ntStatus != STATUS_SUCCESS)
544 if ((byTmp & GPIO3_DATA) == 0) {
545 pDevice->bHWRadioOff = true;
546 MACvRegBitsOn(pDevice, MAC_REG_GPIOCTL1, GPIO3_INTMD);
548 MACvRegBitsOff(pDevice, MAC_REG_GPIOCTL1, GPIO3_INTMD);
549 pDevice->bHWRadioOff = false;
554 vnt_mac_set_led(pDevice, LEDSTS_TMLEN, 0x38);
556 vnt_mac_set_led(pDevice, LEDSTS_STS, LEDSTS_SLOW);
558 MACvRegBitsOn(pDevice, MAC_REG_GPIOCTL0, 0x01);
560 if ((pDevice->bHWRadioOff == true) ||
561 (pDevice->bRadioControlOff == true)) {
562 CARDbRadioPowerOff(pDevice);
564 CARDbRadioPowerOn(pDevice);
567 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO"<----INIbInitAdapter Exit\n");
572 #ifdef CONFIG_PM /* Minimal support for suspend and resume */
574 static int vt6656_suspend(struct usb_interface *intf, pm_message_t message)
576 struct vnt_private *device = usb_get_intfdata(intf);
578 if (!device || !device->dev)
581 if (device->flags & DEVICE_FLAGS_OPENED)
582 device_close(device->dev);
587 static int vt6656_resume(struct usb_interface *intf)
589 struct vnt_private *device = usb_get_intfdata(intf);
591 if (!device || !device->dev)
594 if (!(device->flags & DEVICE_FLAGS_OPENED))
595 device_open(device->dev);
600 #endif /* CONFIG_PM */
602 static const struct net_device_ops device_netdev_ops = {
603 .ndo_open = device_open,
604 .ndo_stop = device_close,
605 .ndo_do_ioctl = device_ioctl,
606 .ndo_get_stats = device_get_stats,
607 .ndo_start_xmit = device_xmit,
608 .ndo_set_rx_mode = device_set_multi,
612 vt6656_probe(struct usb_interface *intf, const struct usb_device_id *id)
614 u8 fake_mac[ETH_ALEN] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
615 struct usb_device *udev = interface_to_usbdev(intf);
617 struct net_device *netdev = NULL;
618 struct vnt_private *pDevice;
620 printk(KERN_NOTICE "%s Ver. %s\n", DEVICE_FULL_DRV_NAM, DEVICE_VERSION);
621 printk(KERN_NOTICE "Copyright (c) 2004 VIA Networking Technologies, Inc.\n");
623 udev = usb_get_dev(udev);
624 netdev = alloc_etherdev(sizeof(struct vnt_private));
626 printk(KERN_ERR DEVICE_NAME ": allocate net device failed\n");
631 pDevice = netdev_priv(netdev);
632 memset(pDevice, 0, sizeof(struct vnt_private));
634 pDevice->dev = netdev;
637 device_set_options(pDevice);
638 spin_lock_init(&pDevice->lock);
639 mutex_init(&pDevice->usb_lock);
641 INIT_DELAYED_WORK(&pDevice->run_command_work, vRunCommand);
642 INIT_DELAYED_WORK(&pDevice->second_callback_work, BSSvSecondCallBack);
643 INIT_WORK(&pDevice->read_work_item, RXvWorkItem);
644 INIT_WORK(&pDevice->rx_mng_work_item, RXvMngWorkItem);
646 pDevice->vnt_mgmt.pAdapter = (void *) pDevice;
648 netdev->netdev_ops = &device_netdev_ops;
649 netdev->wireless_handlers =
650 (struct iw_handler_def *) &iwctl_handler_def;
652 usb_set_intfdata(intf, pDevice);
653 SET_NETDEV_DEV(netdev, &intf->dev);
654 memcpy(pDevice->dev->dev_addr, fake_mac, ETH_ALEN);
656 usb_device_reset(pDevice);
658 rc = register_netdev(netdev);
660 printk(KERN_ERR DEVICE_NAME " Failed to register netdev\n");
674 static void device_free_tx_bufs(struct vnt_private *priv)
676 struct vnt_usb_send_context *tx_context;
679 for (ii = 0; ii < priv->cbTD; ii++) {
680 tx_context = priv->apTD[ii];
681 /* deallocate URBs */
682 if (tx_context->urb) {
683 usb_kill_urb(tx_context->urb);
684 usb_free_urb(tx_context->urb);
693 static void device_free_rx_bufs(struct vnt_private *priv)
698 for (ii = 0; ii < priv->cbRD; ii++) {
699 rcb = priv->apRCB[ii];
701 /* deallocate URBs */
703 usb_kill_urb(rcb->pUrb);
704 usb_free_urb(rcb->pUrb);
709 dev_kfree_skb(rcb->skb);
712 kfree(priv->pRCBMem);
717 static void usb_device_reset(struct vnt_private *pDevice)
720 status = usb_reset_device(pDevice->usb);
722 printk("usb_device_reset fail status=%d\n",status);
726 static void device_free_int_bufs(struct vnt_private *priv)
728 kfree(priv->int_buf.data_buf);
733 static bool device_alloc_bufs(struct vnt_private *priv)
735 struct vnt_usb_send_context *tx_context;
739 for (ii = 0; ii < priv->cbTD; ii++) {
740 tx_context = kmalloc(sizeof(struct vnt_usb_send_context),
742 if (tx_context == NULL) {
743 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
744 "%s : allocate tx usb context failed\n",
749 priv->apTD[ii] = tx_context;
750 tx_context->priv = priv;
753 tx_context->urb = usb_alloc_urb(0, GFP_ATOMIC);
754 if (!tx_context->urb) {
755 DBG_PRT(MSG_LEVEL_ERR,
756 KERN_ERR "alloc tx urb failed\n");
760 tx_context->in_use = false;
763 /* allocate RCB mem */
764 priv->pRCBMem = kzalloc((sizeof(struct vnt_rcb) * priv->cbRD),
766 if (priv->pRCBMem == NULL) {
767 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
768 "%s : alloc rx usb context failed\n",
773 priv->FirstRecvFreeList = NULL;
774 priv->LastRecvFreeList = NULL;
775 priv->FirstRecvMngList = NULL;
776 priv->LastRecvMngList = NULL;
777 priv->NumRecvFreeList = 0;
779 rcb = (struct vnt_rcb *)priv->pRCBMem;
781 for (ii = 0; ii < priv->cbRD; ii++) {
782 priv->apRCB[ii] = rcb;
786 rcb->pUrb = usb_alloc_urb(0, GFP_ATOMIC);
787 if (rcb->pUrb == NULL) {
788 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
789 " Failed to alloc rx urb\n");
793 rcb->skb = netdev_alloc_skb(priv->dev, priv->rx_buf_sz);
794 if (rcb->skb == NULL) {
795 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR
796 " Failed to alloc rx skb\n");
800 rcb->bBoolInUse = false;
802 EnqueueRCB(priv->FirstRecvFreeList,
803 priv->LastRecvFreeList, rcb);
805 priv->NumRecvFreeList++;
809 priv->pInterruptURB = usb_alloc_urb(0, GFP_ATOMIC);
810 if (priv->pInterruptURB == NULL) {
811 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR"Failed to alloc int urb\n");
815 priv->int_buf.data_buf = kmalloc(MAX_INTERRUPT_SIZE, GFP_KERNEL);
816 if (priv->int_buf.data_buf == NULL) {
817 DBG_PRT(MSG_LEVEL_ERR, KERN_ERR"Failed to alloc int buf\n");
818 usb_free_urb(priv->pInterruptURB);
825 device_free_rx_bufs(priv);
828 device_free_tx_bufs(priv);
833 static bool device_init_defrag_cb(struct vnt_private *pDevice)
836 PSDeFragControlBlock pDeF;
838 /* Init the fragment ctl entries */
839 for (i = 0; i < CB_MAX_RX_FRAG; i++) {
840 pDeF = &(pDevice->sRxDFCB[i]);
841 if (!device_alloc_frag_buf(pDevice, pDeF)) {
842 DBG_PRT(MSG_LEVEL_ERR,KERN_ERR "%s: can not alloc frag bufs\n",
847 pDevice->cbDFCB = CB_MAX_RX_FRAG;
848 pDevice->cbFreeDFCB = pDevice->cbDFCB;
852 device_free_frag_bufs(pDevice);
856 static void device_free_frag_bufs(struct vnt_private *pDevice)
858 PSDeFragControlBlock pDeF;
861 for (i = 0; i < CB_MAX_RX_FRAG; i++) {
863 pDeF = &(pDevice->sRxDFCB[i]);
866 dev_kfree_skb(pDeF->skb);
870 int device_alloc_frag_buf(struct vnt_private *pDevice,
871 PSDeFragControlBlock pDeF)
873 pDeF->skb = netdev_alloc_skb(pDevice->dev, pDevice->rx_buf_sz);
880 static int device_open(struct net_device *dev)
882 struct vnt_private *pDevice = netdev_priv(dev);
884 pDevice->fWPA_Authened = false;
886 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_open...\n");
888 pDevice->rx_buf_sz = MAX_TOTAL_SIZE_WITH_ALL_HEADERS;
890 if (device_alloc_bufs(pDevice) == false) {
891 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " device_alloc_bufs fail... \n");
895 if (device_init_defrag_cb(pDevice)== false) {
896 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " Initial defragment cb fail \n");
900 MP_CLEAR_FLAG(pDevice, fMP_DISCONNECTED);
901 MP_SET_FLAG(pDevice, fMP_POST_READS);
902 MP_SET_FLAG(pDevice, fMP_POST_WRITES);
904 /* read config file */
905 Read_config_file(pDevice);
907 if (device_init_registers(pDevice) == false) {
908 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO " init register fail\n");
912 /* init for key management */
913 KeyvInitTable(pDevice,&pDevice->sKey);
914 memcpy(pDevice->vnt_mgmt.abyMACAddr,
915 pDevice->abyCurrentNetAddr, ETH_ALEN);
916 memcpy(pDevice->dev->dev_addr, pDevice->abyCurrentNetAddr, ETH_ALEN);
917 pDevice->bStopTx0Pkt = false;
918 pDevice->bStopDataPkt = false;
919 pDevice->bRoaming = false;
920 pDevice->bIsRoaming = false;
921 pDevice->bEnableRoaming = false;
923 vMgrObjectInit(pDevice);
925 schedule_delayed_work(&pDevice->second_callback_work, HZ);
927 pDevice->int_interval = 1; /* bInterval is set to 1 */
928 pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
930 pDevice->bIsRxWorkItemQueued = true;
932 pDevice->bWPADEVUp = false;
933 pDevice->bwextstep0 = false;
934 pDevice->bwextstep1 = false;
935 pDevice->bwextstep2 = false;
936 pDevice->bwextstep3 = false;
937 pDevice->bWPASuppWextEnabled = false;
938 pDevice->byReAssocCount = 0;
940 schedule_work(&pDevice->read_work_item);
941 INTvWorkItem(pDevice);
943 /* if WEP key already set by iwconfig but device not yet open */
944 if ((pDevice->bEncryptionEnable == true) && (pDevice->bTransmitKey == true)) {
945 KeybSetDefaultKey( pDevice,
947 pDevice->byKeyIndex | (1 << 31),
954 pDevice->eEncryptionStatus = Ndis802_11Encryption1Enabled;
957 if (pDevice->vnt_mgmt.eConfigMode == WMAC_CONFIG_AP)
958 bScheduleCommand((void *) pDevice, WLAN_CMD_RUN_AP, NULL);
960 bScheduleCommand((void *) pDevice, WLAN_CMD_BSSID_SCAN, NULL);
962 netif_stop_queue(pDevice->dev);
963 pDevice->flags |= DEVICE_FLAGS_OPENED;
965 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open success..\n");
969 device_free_frag_bufs(pDevice);
971 device_free_rx_bufs(pDevice);
972 device_free_tx_bufs(pDevice);
973 device_free_int_bufs(pDevice);
974 usb_kill_urb(pDevice->pInterruptURB);
975 usb_free_urb(pDevice->pInterruptURB);
977 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_open fail.. \n");
981 static int device_close(struct net_device *dev)
983 struct vnt_private *pDevice = netdev_priv(dev);
984 struct vnt_manager *pMgmt = &pDevice->vnt_mgmt;
987 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close1\n");
991 if (pDevice->bLinkPass) {
992 bScheduleCommand((void *) pDevice, WLAN_CMD_DISASSOCIATE, NULL);
996 memset(pMgmt->abyDesireSSID, 0, WLAN_IEHDR_LEN + WLAN_SSID_MAXLEN + 1);
997 pMgmt->bShareKeyAlgorithm = false;
998 pDevice->bEncryptionEnable = false;
999 pDevice->eEncryptionStatus = Ndis802_11EncryptionDisabled;
1001 for (uu = 0; uu < MAX_KEY_TABLE; uu++)
1002 MACvDisableKeyEntry(pDevice,uu);
1004 if ((pDevice->flags & DEVICE_FLAGS_UNPLUG) == false) {
1005 MACbShutdown(pDevice);
1007 netif_stop_queue(pDevice->dev);
1008 MP_SET_FLAG(pDevice, fMP_DISCONNECTED);
1009 MP_CLEAR_FLAG(pDevice, fMP_POST_WRITES);
1010 MP_CLEAR_FLAG(pDevice, fMP_POST_READS);
1012 cancel_delayed_work_sync(&pDevice->run_command_work);
1013 cancel_delayed_work_sync(&pDevice->second_callback_work);
1015 cancel_work_sync(&pDevice->rx_mng_work_item);
1016 cancel_work_sync(&pDevice->read_work_item);
1018 pDevice->bRoaming = false;
1019 pDevice->bIsRoaming = false;
1020 pDevice->bEnableRoaming = false;
1021 pDevice->bCmdRunning = false;
1022 pDevice->bLinkPass = false;
1023 memset(pMgmt->abyCurrBSSID, 0, 6);
1024 pMgmt->eCurrState = WMAC_STATE_IDLE;
1026 pDevice->flags &= ~DEVICE_FLAGS_OPENED;
1028 device_free_tx_bufs(pDevice);
1029 device_free_rx_bufs(pDevice);
1030 device_free_int_bufs(pDevice);
1031 device_free_frag_bufs(pDevice);
1033 usb_kill_urb(pDevice->pInterruptURB);
1034 usb_free_urb(pDevice->pInterruptURB);
1036 BSSvClearNodeDBTable(pDevice, 0);
1038 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "device_close2 \n");
1043 static void vt6656_disconnect(struct usb_interface *intf)
1045 struct vnt_private *device = usb_get_intfdata(intf);
1050 usb_set_intfdata(intf, NULL);
1051 usb_put_dev(interface_to_usbdev(intf));
1053 device->flags |= DEVICE_FLAGS_UNPLUG;
1056 unregister_netdev(device->dev);
1057 free_netdev(device->dev);
1061 static int device_xmit(struct sk_buff *skb, struct net_device *dev)
1063 struct vnt_private *pDevice = netdev_priv(dev);
1064 struct net_device_stats *stats = &pDevice->stats;
1065 unsigned long flags;
1067 spin_lock_irqsave(&pDevice->lock, flags);
1069 netif_stop_queue(dev);
1071 if (!pDevice->bLinkPass) {
1072 dev_kfree_skb_irq(skb);
1076 if (pDevice->bStopDataPkt) {
1077 dev_kfree_skb_irq(skb);
1078 stats->tx_dropped++;
1082 if (nsDMA_tx_packet(pDevice, skb)) {
1083 if (netif_queue_stopped(dev))
1084 netif_wake_queue(dev);
1088 spin_unlock_irqrestore(&pDevice->lock, flags);
1090 return NETDEV_TX_OK;
1093 /* find out the start position of str2 from str1 */
1094 static unsigned char *kstrstr(const unsigned char *str1,
1095 const unsigned char *str2) {
1096 int str1_len = strlen(str1);
1097 int str2_len = strlen(str2);
1099 while (str1_len >= str2_len) {
1101 if(memcmp(str1,str2,str2_len)==0)
1102 return (unsigned char *) str1;
1108 static int Config_FileGetParameter(unsigned char *string,
1109 unsigned char *dest,
1110 unsigned char *source)
1112 unsigned char buf1[100];
1113 unsigned char buf2[100];
1114 unsigned char *start_p = NULL, *end_p = NULL, *tmp_p = NULL;
1118 strcat(buf1, string);
1120 source+=strlen(buf1);
1122 /* find target string start point */
1123 start_p = kstrstr(source,buf1);
1124 if (start_p == NULL)
1127 /* check if current config line is marked by "#" */
1128 for (ii = 1; ; ii++) {
1129 if (memcmp(start_p - ii, "\n", 1) == 0)
1131 if (memcmp(start_p - ii, "#", 1) == 0)
1135 /* find target string end point */
1136 end_p = kstrstr(start_p,"\n");
1137 if (end_p == NULL) { /* can't find "\n", but don't care */
1138 end_p = start_p + strlen(start_p); /* no include "\n" */
1142 memcpy(buf2, start_p, end_p-start_p); /* get the target line */
1143 buf2[end_p-start_p]='\0';
1146 start_p = kstrstr(buf2,"=");
1147 if (start_p == NULL)
1150 strcpy(buf1,start_p+1);
1154 while(*tmp_p != 0x00) {
1161 memcpy(dest,tmp_p,strlen(tmp_p));
1165 /* if read fails, return NULL, or return data pointer */
1166 static unsigned char *Config_FileOperation(struct vnt_private *pDevice)
1168 unsigned char *buffer = kmalloc(1024, GFP_KERNEL);
1172 printk("allocate mem for file fail?\n");
1176 file = filp_open(CONFIG_PATH, O_RDONLY, 0);
1179 printk("Config_FileOperation file Not exist\n");
1183 if (kernel_read(file, 0, buffer, 1024) < 0) {
1184 printk("read file error?\n");
1193 /* return --->-1:fail; >=0:successful */
1194 static int Read_config_file(struct vnt_private *pDevice)
1197 unsigned char tmpbuffer[100];
1198 unsigned char *buffer = NULL;
1200 /* init config setting */
1201 pDevice->config_file.ZoneType = -1;
1202 pDevice->config_file.eAuthenMode = -1;
1203 pDevice->config_file.eEncryptionStatus = -1;
1205 buffer = Config_FileOperation(pDevice);
1206 if (buffer == NULL) {
1213 memset(tmpbuffer,0,sizeof(tmpbuffer));
1214 if(Config_FileGetParameter("ZONETYPE",tmpbuffer,buffer) ==true) {
1215 if(memcmp(tmpbuffer,"USA",3)==0) {
1216 pDevice->config_file.ZoneType=ZoneType_USA;
1218 else if(memcmp(tmpbuffer,"JAPAN",5)==0) {
1219 pDevice->config_file.ZoneType=ZoneType_Japan;
1221 else if(memcmp(tmpbuffer,"EUROPE",6)==0) {
1222 pDevice->config_file.ZoneType=ZoneType_Europe;
1225 printk("Unknown Zonetype[%s]?\n",tmpbuffer);
1230 /* get other parameter */
1232 memset(tmpbuffer,0,sizeof(tmpbuffer));
1233 if(Config_FileGetParameter("AUTHENMODE",tmpbuffer,buffer)==true) {
1234 pDevice->config_file.eAuthenMode = (int) simple_strtol(tmpbuffer, NULL, 10);
1237 memset(tmpbuffer,0,sizeof(tmpbuffer));
1238 if(Config_FileGetParameter("ENCRYPTIONMODE",tmpbuffer,buffer)==true) {
1239 pDevice->config_file.eEncryptionStatus= (int) simple_strtol(tmpbuffer, NULL, 10);
1247 static void device_set_multi(struct net_device *dev)
1249 struct vnt_private *priv = netdev_priv(dev);
1250 unsigned long flags;
1252 if (priv->flags & DEVICE_FLAGS_OPENED) {
1253 spin_lock_irqsave(&priv->lock, flags);
1255 bScheduleCommand(priv, WLAN_CMD_CONFIGURE_FILTER, NULL);
1257 spin_unlock_irqrestore(&priv->lock, flags);
1261 void vnt_configure_filter(struct vnt_private *priv)
1263 struct net_device *dev = priv->dev;
1264 struct vnt_manager *mgmt = &priv->vnt_mgmt;
1265 struct netdev_hw_addr *ha;
1270 rc = vnt_control_in(priv, MESSAGE_TYPE_READ,
1271 MAC_REG_RCR, MESSAGE_REQUEST_MACREG, 1, &tmp);
1273 priv->byRxMode = tmp;
1275 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO "priv->byRxMode in= %x\n",
1278 if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
1279 DBG_PRT(MSG_LEVEL_ERR, KERN_NOTICE
1280 "%s: Promiscuous mode enabled.\n", dev->name);
1281 /* unconditionally log net taps */
1282 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST|RCR_UNICAST);
1283 } else if ((netdev_mc_count(dev) > priv->multicast_limit) ||
1284 (dev->flags & IFF_ALLMULTI)) {
1286 MACvWriteMultiAddr(priv, mc_filter);
1288 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1290 netdev_for_each_mc_addr(ha, dev) {
1291 int bit_nr = ether_crc(ETH_ALEN, ha->addr) >> 26;
1293 mc_filter |= 1ULL << (bit_nr & 0x3f);
1296 MACvWriteMultiAddr(priv, mc_filter);
1298 priv->byRxMode &= ~(RCR_UNICAST);
1299 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1302 if (mgmt->eConfigMode == WMAC_CONFIG_AP) {
1304 * If AP mode, don't enable RCR_UNICAST since HW only compares
1305 * addr1 with local MAC
1307 priv->byRxMode |= (RCR_MULTICAST|RCR_BROADCAST);
1308 priv->byRxMode &= ~(RCR_UNICAST);
1311 vnt_control_out_u8(priv, MESSAGE_REQUEST_MACREG,
1312 MAC_REG_RCR, priv->byRxMode);
1314 DBG_PRT(MSG_LEVEL_DEBUG, KERN_INFO
1315 "priv->byRxMode out= %x\n", priv->byRxMode);
1318 static struct net_device_stats *device_get_stats(struct net_device *dev)
1320 struct vnt_private *pDevice = netdev_priv(dev);
1322 return &pDevice->stats;
1325 static int device_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
1331 return ethtool_ioctl(dev, rq);
1338 static int ethtool_ioctl(struct net_device *dev, struct ifreq *rq)
1342 if (copy_from_user(ðcmd, rq->ifr_data, sizeof(ethcmd)))
1346 case ETHTOOL_GDRVINFO: {
1347 struct ethtool_drvinfo info = {ETHTOOL_GDRVINFO};
1348 strncpy(info.driver, DEVICE_NAME, sizeof(info.driver)-1);
1349 strncpy(info.version, DEVICE_VERSION, sizeof(info.version)-1);
1350 if (copy_to_user(rq->ifr_data, &info, sizeof(info)))
1360 MODULE_DEVICE_TABLE(usb, vt6656_table);
1362 static struct usb_driver vt6656_driver = {
1363 .name = DEVICE_NAME,
1364 .probe = vt6656_probe,
1365 .disconnect = vt6656_disconnect,
1366 .id_table = vt6656_table,
1368 .suspend = vt6656_suspend,
1369 .resume = vt6656_resume,
1370 #endif /* CONFIG_PM */
1373 module_usb_driver(vt6656_driver);