]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/winbond/wbusb.c
Staging: w35und: clean up wblinux.c a bit
[karo-tx-linux.git] / drivers / staging / winbond / wbusb.c
1 /*
2  * Copyright 2008 Pavel Machek <pavel@suse.cz>
3  *
4  * Distribute under GPLv2.
5  */
6 #include <net/mac80211.h>
7 #include <linux/usb.h>
8
9 #include "core.h"
10 #include "mds_f.h"
11 #include "mlmetxrx_f.h"
12 #include "mto_f.h"
13 #include "wbhal_f.h"
14 #include "wblinux_f.h"
15
16 MODULE_AUTHOR("Original by: Jeff Lee<YY_Lee@issc.com.tw> Adapted to 2.6.x by Costantino Leandro (Rxart Desktop) <le_costantino@pixartargentina.com.ar>");
17 MODULE_DESCRIPTION("IS89C35 802.11bg WLAN USB Driver");
18 MODULE_LICENSE("GPL");
19 MODULE_VERSION("0.1");
20
21 static struct usb_device_id wb35_table[] __devinitdata = {
22         {USB_DEVICE(0x0416, 0x0035)},
23         {USB_DEVICE(0x18E8, 0x6201)},
24         {USB_DEVICE(0x18E8, 0x6206)},
25         {USB_DEVICE(0x18E8, 0x6217)},
26         {USB_DEVICE(0x18E8, 0x6230)},
27         {USB_DEVICE(0x18E8, 0x6233)},
28         {USB_DEVICE(0x1131, 0x2035)},
29         { 0, }
30 };
31
32 MODULE_DEVICE_TABLE(usb, wb35_table);
33
34 static struct ieee80211_rate wbsoft_rates[] = {
35         { .bitrate = 10, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
36 };
37
38 static struct ieee80211_channel wbsoft_channels[] = {
39         { .center_freq = 2412},
40 };
41
42 static struct ieee80211_supported_band wbsoft_band_2GHz = {
43         .channels       = wbsoft_channels,
44         .n_channels     = ARRAY_SIZE(wbsoft_channels),
45         .bitrates       = wbsoft_rates,
46         .n_bitrates     = ARRAY_SIZE(wbsoft_rates),
47 };
48
49 int wbsoft_enabled;
50
51 static int wbsoft_add_interface(struct ieee80211_hw *dev,
52                                  struct ieee80211_if_init_conf *conf)
53 {
54         printk("wbsoft_add interface called\n");
55         return 0;
56 }
57
58 static void wbsoft_remove_interface(struct ieee80211_hw *dev,
59                                      struct ieee80211_if_init_conf *conf)
60 {
61         printk("wbsoft_remove interface called\n");
62 }
63
64 static void wbsoft_stop(struct ieee80211_hw *hw)
65 {
66         printk(KERN_INFO "%s called\n", __func__);
67 }
68
69 static int wbsoft_get_stats(struct ieee80211_hw *hw,
70                             struct ieee80211_low_level_stats *stats)
71 {
72         printk(KERN_INFO "%s called\n", __func__);
73         return 0;
74 }
75
76 static int wbsoft_get_tx_stats(struct ieee80211_hw *hw,
77                                struct ieee80211_tx_queue_stats *stats)
78 {
79         printk(KERN_INFO "%s called\n", __func__);
80         return 0;
81 }
82
83 static void wbsoft_configure_filter(struct ieee80211_hw *dev,
84                                      unsigned int changed_flags,
85                                      unsigned int *total_flags,
86                                      int mc_count, struct dev_mc_list *mclist)
87 {
88         unsigned int bit_nr, new_flags;
89         u32 mc_filter[2];
90         int i;
91
92         new_flags = 0;
93
94         if (*total_flags & FIF_PROMISC_IN_BSS) {
95                 new_flags |= FIF_PROMISC_IN_BSS;
96                 mc_filter[1] = mc_filter[0] = ~0;
97         } else if ((*total_flags & FIF_ALLMULTI) || (mc_count > 32)) {
98                 new_flags |= FIF_ALLMULTI;
99                 mc_filter[1] = mc_filter[0] = ~0;
100         } else {
101                 mc_filter[1] = mc_filter[0] = 0;
102                 for (i = 0; i < mc_count; i++) {
103                         if (!mclist)
104                                 break;
105                         printk("Should call ether_crc here\n");
106                         //bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
107                         bit_nr = 0;
108
109                         bit_nr &= 0x3F;
110                         mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
111                         mclist = mclist->next;
112                 }
113         }
114
115         dev->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
116
117         *total_flags = new_flags;
118 }
119
120 static int wbsoft_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
121 {
122         struct wbsoft_priv *priv = dev->priv;
123
124         MLMESendFrame(priv, skb->data, skb->len, FRAME_TYPE_802_11_MANAGEMENT);
125
126         return NETDEV_TX_OK;
127 }
128
129
130 static int wbsoft_start(struct ieee80211_hw *dev)
131 {
132         wbsoft_enabled = 1;
133         printk("wbsoft_start called\n");
134         return 0;
135 }
136
137 static int wbsoft_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
138 {
139         struct wbsoft_priv *priv = dev->priv;
140
141         ChanInfo ch;
142         printk("wbsoft_config called\n");
143
144         ch.band = 1;
145         ch.ChanNo = 1;  /* Should use channel_num, or something, as that is already pre-translated */
146
147
148         hal_set_current_channel(&priv->sHwData, ch);
149         hal_set_beacon_period(&priv->sHwData, conf->beacon_int);
150 //      hal_set_cap_info(&priv->sHwData, ?? );
151 // hal_set_ssid(phw_data_t pHwData,  u8 * pssid,  u8 ssid_len); ??
152         hal_set_accept_broadcast(&priv->sHwData, 1);
153         hal_set_accept_promiscuous(&priv->sHwData,  1);
154         hal_set_accept_multicast(&priv->sHwData,  1);
155         hal_set_accept_beacon(&priv->sHwData,  1);
156         hal_set_radio_mode(&priv->sHwData,  0);
157         //hal_set_antenna_number(  phw_data_t pHwData, u8 number )
158         //hal_set_rf_power(phw_data_t pHwData, u8 PowerIndex)
159
160
161 //      hal_start_bss(&priv->sHwData, WLAN_BSSTYPE_INFRASTRUCTURE);     ??
162
163 //void hal_set_rates(phw_data_t pHwData, u8 * pbss_rates,
164 //                 u8 length, unsigned char basic_rate_set)
165
166         return 0;
167 }
168
169 static int wbsoft_config_interface(struct ieee80211_hw *dev,
170                                     struct ieee80211_vif *vif,
171                                     struct ieee80211_if_conf *conf)
172 {
173         printk("wbsoft_config_interface called\n");
174         return 0;
175 }
176
177 static u64 wbsoft_get_tsf(struct ieee80211_hw *dev)
178 {
179         printk("wbsoft_get_tsf called\n");
180         return 0;
181 }
182
183 static const struct ieee80211_ops wbsoft_ops = {
184         .tx                     = wbsoft_tx,
185         .start                  = wbsoft_start,         /* Start can be pretty much empty as we do wb35_hw_init() during probe? */
186         .stop                   = wbsoft_stop,
187         .add_interface          = wbsoft_add_interface,
188         .remove_interface       = wbsoft_remove_interface,
189         .config                 = wbsoft_config,
190         .config_interface       = wbsoft_config_interface,
191         .configure_filter       = wbsoft_configure_filter,
192         .get_stats              = wbsoft_get_stats,
193         .get_tx_stats           = wbsoft_get_tx_stats,
194         .get_tsf                = wbsoft_get_tsf,
195 // conf_tx: hal_set_cwmin()/hal_set_cwmax;
196 };
197
198 static unsigned char wb35_hw_init(struct ieee80211_hw *hw)
199 {
200         struct wbsoft_priv *priv = hw->priv;
201         phw_data_t      pHwData;
202         u8              *pMacAddr;
203         u8              *pMacAddr2;
204         u32             InitStep = 0;
205         u8              EEPROM_region;
206         u8              HwRadioOff;
207
208         //
209         // Setting default value for Linux
210         //
211         priv->sLocalPara.region_INF = REGION_AUTO;
212         priv->sLocalPara.TxRateMode = RATE_AUTO;
213         priv->sLocalPara.bMacOperationMode = MODE_802_11_BG;    // B/G mode
214         priv->Mds.TxRTSThreshold = DEFAULT_RTSThreshold;
215         priv->Mds.TxFragmentThreshold = DEFAULT_FRAGMENT_THRESHOLD;
216         hal_set_phy_type( &priv->sHwData, RF_WB_242_1 );
217         priv->sLocalPara.MTUsize = MAX_ETHERNET_PACKET_SIZE;
218         priv->sLocalPara.bPreambleMode = AUTO_MODE;
219         priv->sLocalPara.RadioOffStatus.boSwRadioOff = false;
220         pHwData = &priv->sHwData;
221         hal_set_phy_type( pHwData, RF_DECIDE_BY_INF );
222
223         // Initial Software variable
224         priv->sLocalPara.ShutDowned = false;
225
226         //added by ws for wep key error detection
227         priv->sLocalPara.bWepKeyError= false;
228         priv->sLocalPara.bToSelfPacketReceived = false;
229         priv->sLocalPara.WepKeyDetectTimerCount= 2 * 100; /// 2 seconds
230
231         // Initial USB hal
232         InitStep = 1;
233         pHwData = &priv->sHwData;
234         if (!hal_init_hardware(hw))
235                 goto error;
236
237         EEPROM_region = hal_get_region_from_EEPROM( pHwData );
238         if (EEPROM_region != REGION_AUTO)
239                 priv->sLocalPara.region = EEPROM_region;
240         else {
241                 if (priv->sLocalPara.region_INF != REGION_AUTO)
242                         priv->sLocalPara.region = priv->sLocalPara.region_INF;
243                 else
244                         priv->sLocalPara.region = REGION_USA;   //default setting
245         }
246
247         // Get Software setting flag from hal
248         priv->sLocalPara.boAntennaDiversity = false;
249         if (hal_software_set(pHwData) & 0x00000001)
250                 priv->sLocalPara.boAntennaDiversity = true;
251
252         //
253         // For TS module
254         //
255         InitStep = 2;
256
257         // For MDS module
258         InitStep = 3;
259         Mds_initial(priv);
260
261         //=======================================
262         // Initialize the SME, SCAN, MLME, ROAM
263         //=======================================
264         InitStep = 4;
265         InitStep = 5;
266         InitStep = 6;
267
268         // If no user-defined address in the registry, use the addresss "burned" on the NIC instead.
269         pMacAddr = priv->sLocalPara.ThisMacAddress;
270         pMacAddr2 = priv->sLocalPara.PermanentAddress;
271         hal_get_permanent_address( pHwData, priv->sLocalPara.PermanentAddress );// Reading ethernet address from EEPROM
272         if (memcmp(pMacAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) == 0)
273                 memcpy(pMacAddr, pMacAddr2, MAC_ADDR_LENGTH);
274         else {
275                 // Set the user define MAC address
276                 hal_set_ethernet_address(pHwData, priv->sLocalPara.ThisMacAddress);
277         }
278
279         //get current antenna
280         priv->sLocalPara.bAntennaNo = hal_get_antenna_number(pHwData);
281 #ifdef _PE_STATE_DUMP_
282         WBDEBUG(("Driver init, antenna no = %d\n", psLOCAL->bAntennaNo));
283 #endif
284         hal_get_hw_radio_off( pHwData );
285
286         // Waiting for HAL setting OK
287         while (!hal_idle(pHwData))
288                 msleep(10);
289
290         MTO_Init(priv);
291
292         HwRadioOff = hal_get_hw_radio_off( pHwData );
293         priv->sLocalPara.RadioOffStatus.boHwRadioOff = !!HwRadioOff;
294
295         hal_set_radio_mode( pHwData, (unsigned char)(priv->sLocalPara.RadioOffStatus.boSwRadioOff || priv->sLocalPara.RadioOffStatus.boHwRadioOff) );
296
297         hal_driver_init_OK(pHwData) = 1; // Notify hal that the driver is ready now.
298         //set a tx power for reference.....
299 //      sme_set_tx_power_level(priv, 12);       FIXME?
300         return true;
301
302 error:
303         switch (InitStep) {
304         case 5:
305         case 4:
306         case 3: Mds_Destroy( priv );
307         case 2:
308         case 1: WBLINUX_stop(priv);
309                 hal_halt( pHwData, NULL );
310         case 0: break;
311         }
312
313         return false;
314 }
315
316 static int wb35_probe(struct usb_interface *intf, const struct usb_device_id *id_table)
317 {
318         PWBUSB          pWbUsb;
319         struct usb_host_interface *interface;
320         struct usb_endpoint_descriptor *endpoint;
321         u32     ltmp;
322         struct usb_device *udev = interface_to_usbdev(intf);
323         struct wbsoft_priv *priv;
324         struct ieee80211_hw *dev;
325         int err;
326
327         usb_get_dev(udev);
328
329         // 20060630.2 Check the device if it already be opened
330         err = usb_control_msg(udev, usb_rcvctrlpipe( udev, 0 ),
331                               0x01, USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_IN,
332                               0x0, 0x400, &ltmp, 4, HZ*100 );
333         if (err)
334                 goto error;
335
336         ltmp = cpu_to_le32(ltmp);
337         if (ltmp) {  // Is already initialized?
338                 err = -EBUSY;
339                 goto error;
340         }
341
342         dev = ieee80211_alloc_hw(sizeof(*priv), &wbsoft_ops);
343         if (!dev)
344                 goto error;
345
346         priv = dev->priv;
347
348         spin_lock_init(&priv->SpinLock);
349
350         pWbUsb = &priv->sHwData.WbUsb;
351         pWbUsb->udev = udev;
352
353         interface = intf->cur_altsetting;
354         endpoint = &interface->endpoint[0].desc;
355
356         if (endpoint[2].wMaxPacketSize == 512) {
357                 printk("[w35und] Working on USB 2.0\n");
358                 pWbUsb->IsUsb20 = 1;
359         }
360
361         if (!wb35_hw_init(dev)) {
362                 err = -EINVAL;
363                 goto error_free_hw;
364         }
365
366         SET_IEEE80211_DEV(dev, &udev->dev);
367         {
368                 phw_data_t pHwData = &priv->sHwData;
369                 unsigned char           dev_addr[MAX_ADDR_LEN];
370                 hal_get_permanent_address(pHwData, dev_addr);
371                 SET_IEEE80211_PERM_ADDR(dev, dev_addr);
372         }
373
374         dev->extra_tx_headroom = 12;    /* FIXME */
375         dev->flags = 0;
376
377         dev->channel_change_time = 1000;
378         dev->queues = 1;
379
380         dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &wbsoft_band_2GHz;
381
382         err = ieee80211_register_hw(dev);
383         if (err)
384                 goto error_free_hw;
385
386         usb_set_intfdata(intf, priv);
387
388         return 0;
389
390 error_free_hw:
391         ieee80211_free_hw(dev);
392 error:
393         usb_put_dev(udev);
394         return err;
395 }
396
397 void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress, int PacketSize)
398 {
399         struct sk_buff *skb;
400         struct ieee80211_rx_status rx_status = {0};
401
402         if (!wbsoft_enabled)
403                 return;
404
405         skb = dev_alloc_skb(PacketSize);
406         if (!skb) {
407                 printk("Not enough memory for packet, FIXME\n");
408                 return;
409         }
410
411         memcpy(skb_put(skb, PacketSize),
412                pRxBufferAddress,
413                PacketSize);
414
415 /*
416         rx_status.rate = 10;
417         rx_status.channel = 1;
418         rx_status.freq = 12345;
419         rx_status.phymode = MODE_IEEE80211B;
420 */
421
422         ieee80211_rx_irqsafe(hw, skb, &rx_status);
423 }
424
425 static void wb35_hw_halt(struct wbsoft_priv *adapter)
426 {
427         //---------------------
428         adapter->sLocalPara.ShutDowned = true;
429
430         Mds_Destroy( adapter );
431
432         // Turn off Rx and Tx hardware ability
433         hal_stop( &adapter->sHwData );
434 #ifdef _PE_USB_INI_DUMP_
435         WBDEBUG(("[w35und] Hal_stop O.K.\n"));
436 #endif
437         msleep(100);// Waiting Irp completed
438
439         // Destroy the NDIS module
440         WBLINUX_stop(adapter);
441
442         // Halt the HAL
443         hal_halt(&adapter->sHwData, NULL);
444 }
445
446
447 static void wb35_disconnect(struct usb_interface *intf)
448 {
449         struct wbsoft_priv *priv = usb_get_intfdata(intf);
450
451         wb35_hw_halt(priv);
452
453         usb_set_intfdata(intf, NULL);
454         usb_put_dev(interface_to_usbdev(intf));
455 }
456
457 static struct usb_driver wb35_driver = {
458         .name           = "w35und",
459         .id_table       = wb35_table,
460         .probe          = wb35_probe,
461         .disconnect     = wb35_disconnect,
462 };
463
464 static int __init wb35_init(void)
465 {
466         return usb_register(&wb35_driver);
467 }
468
469 static void __exit wb35_exit(void)
470 {
471         usb_deregister(&wb35_driver);
472 }
473
474 module_init(wb35_init);
475 module_exit(wb35_exit);