]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/staging/winbond/wbusb.c
Staging: w35und: move global wbsoft_enabled to struct wbsoft_priv
[linux-2.6-omap-h63xx.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 static int wbsoft_add_interface(struct ieee80211_hw *dev,
50                                  struct ieee80211_if_init_conf *conf)
51 {
52         printk("wbsoft_add interface called\n");
53         return 0;
54 }
55
56 static void wbsoft_remove_interface(struct ieee80211_hw *dev,
57                                      struct ieee80211_if_init_conf *conf)
58 {
59         printk("wbsoft_remove interface called\n");
60 }
61
62 static void wbsoft_stop(struct ieee80211_hw *hw)
63 {
64         printk(KERN_INFO "%s called\n", __func__);
65 }
66
67 static int wbsoft_get_stats(struct ieee80211_hw *hw,
68                             struct ieee80211_low_level_stats *stats)
69 {
70         printk(KERN_INFO "%s called\n", __func__);
71         return 0;
72 }
73
74 static int wbsoft_get_tx_stats(struct ieee80211_hw *hw,
75                                struct ieee80211_tx_queue_stats *stats)
76 {
77         printk(KERN_INFO "%s called\n", __func__);
78         return 0;
79 }
80
81 static void wbsoft_configure_filter(struct ieee80211_hw *dev,
82                                      unsigned int changed_flags,
83                                      unsigned int *total_flags,
84                                      int mc_count, struct dev_mc_list *mclist)
85 {
86         unsigned int bit_nr, new_flags;
87         u32 mc_filter[2];
88         int i;
89
90         new_flags = 0;
91
92         if (*total_flags & FIF_PROMISC_IN_BSS) {
93                 new_flags |= FIF_PROMISC_IN_BSS;
94                 mc_filter[1] = mc_filter[0] = ~0;
95         } else if ((*total_flags & FIF_ALLMULTI) || (mc_count > 32)) {
96                 new_flags |= FIF_ALLMULTI;
97                 mc_filter[1] = mc_filter[0] = ~0;
98         } else {
99                 mc_filter[1] = mc_filter[0] = 0;
100                 for (i = 0; i < mc_count; i++) {
101                         if (!mclist)
102                                 break;
103                         printk("Should call ether_crc here\n");
104                         //bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
105                         bit_nr = 0;
106
107                         bit_nr &= 0x3F;
108                         mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
109                         mclist = mclist->next;
110                 }
111         }
112
113         dev->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
114
115         *total_flags = new_flags;
116 }
117
118 static int wbsoft_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
119 {
120         struct wbsoft_priv *priv = dev->priv;
121
122         MLMESendFrame(priv, skb->data, skb->len, FRAME_TYPE_802_11_MANAGEMENT);
123
124         return NETDEV_TX_OK;
125 }
126
127
128 static int wbsoft_start(struct ieee80211_hw *dev)
129 {
130         struct wbsoft_priv *priv = dev->priv;
131
132         priv->enabled = true;
133
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         //added by ws for wep key error detection
224         priv->sLocalPara.bWepKeyError= false;
225         priv->sLocalPara.bToSelfPacketReceived = false;
226         priv->sLocalPara.WepKeyDetectTimerCount= 2 * 100; /// 2 seconds
227
228         // Initial USB hal
229         InitStep = 1;
230         pHwData = &priv->sHwData;
231         if (!hal_init_hardware(hw))
232                 goto error;
233
234         EEPROM_region = hal_get_region_from_EEPROM( pHwData );
235         if (EEPROM_region != REGION_AUTO)
236                 priv->sLocalPara.region = EEPROM_region;
237         else {
238                 if (priv->sLocalPara.region_INF != REGION_AUTO)
239                         priv->sLocalPara.region = priv->sLocalPara.region_INF;
240                 else
241                         priv->sLocalPara.region = REGION_USA;   //default setting
242         }
243
244         // Get Software setting flag from hal
245         priv->sLocalPara.boAntennaDiversity = false;
246         if (hal_software_set(pHwData) & 0x00000001)
247                 priv->sLocalPara.boAntennaDiversity = true;
248
249         //
250         // For TS module
251         //
252         InitStep = 2;
253
254         // For MDS module
255         InitStep = 3;
256         Mds_initial(priv);
257
258         //=======================================
259         // Initialize the SME, SCAN, MLME, ROAM
260         //=======================================
261         InitStep = 4;
262         InitStep = 5;
263         InitStep = 6;
264
265         // If no user-defined address in the registry, use the addresss "burned" on the NIC instead.
266         pMacAddr = priv->sLocalPara.ThisMacAddress;
267         pMacAddr2 = priv->sLocalPara.PermanentAddress;
268         hal_get_permanent_address( pHwData, priv->sLocalPara.PermanentAddress );// Reading ethernet address from EEPROM
269         if (memcmp(pMacAddr, "\x00\x00\x00\x00\x00\x00", MAC_ADDR_LENGTH) == 0)
270                 memcpy(pMacAddr, pMacAddr2, MAC_ADDR_LENGTH);
271         else {
272                 // Set the user define MAC address
273                 hal_set_ethernet_address(pHwData, priv->sLocalPara.ThisMacAddress);
274         }
275
276         //get current antenna
277         priv->sLocalPara.bAntennaNo = hal_get_antenna_number(pHwData);
278 #ifdef _PE_STATE_DUMP_
279         WBDEBUG(("Driver init, antenna no = %d\n", psLOCAL->bAntennaNo));
280 #endif
281         hal_get_hw_radio_off( pHwData );
282
283         // Waiting for HAL setting OK
284         while (!hal_idle(pHwData))
285                 msleep(10);
286
287         MTO_Init(priv);
288
289         HwRadioOff = hal_get_hw_radio_off( pHwData );
290         priv->sLocalPara.RadioOffStatus.boHwRadioOff = !!HwRadioOff;
291
292         hal_set_radio_mode( pHwData, (unsigned char)(priv->sLocalPara.RadioOffStatus.boSwRadioOff || priv->sLocalPara.RadioOffStatus.boHwRadioOff) );
293
294         hal_driver_init_OK(pHwData) = 1; // Notify hal that the driver is ready now.
295         //set a tx power for reference.....
296 //      sme_set_tx_power_level(priv, 12);       FIXME?
297         return true;
298
299 error:
300         switch (InitStep) {
301         case 5:
302         case 4:
303         case 3: Mds_Destroy( priv );
304         case 2:
305         case 1: WBLINUX_stop(priv);
306                 hal_halt( pHwData, NULL );
307         case 0: break;
308         }
309
310         return false;
311 }
312
313 static int wb35_probe(struct usb_interface *intf, const struct usb_device_id *id_table)
314 {
315         PWBUSB          pWbUsb;
316         struct usb_host_interface *interface;
317         struct usb_endpoint_descriptor *endpoint;
318         u32     ltmp;
319         struct usb_device *udev = interface_to_usbdev(intf);
320         struct wbsoft_priv *priv;
321         struct ieee80211_hw *dev;
322         int err;
323
324         usb_get_dev(udev);
325
326         // 20060630.2 Check the device if it already be opened
327         err = usb_control_msg(udev, usb_rcvctrlpipe( udev, 0 ),
328                               0x01, USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_IN,
329                               0x0, 0x400, &ltmp, 4, HZ*100 );
330         if (err)
331                 goto error;
332
333         ltmp = cpu_to_le32(ltmp);
334         if (ltmp) {  // Is already initialized?
335                 err = -EBUSY;
336                 goto error;
337         }
338
339         dev = ieee80211_alloc_hw(sizeof(*priv), &wbsoft_ops);
340         if (!dev)
341                 goto error;
342
343         priv = dev->priv;
344
345         spin_lock_init(&priv->SpinLock);
346
347         pWbUsb = &priv->sHwData.WbUsb;
348         pWbUsb->udev = udev;
349
350         interface = intf->cur_altsetting;
351         endpoint = &interface->endpoint[0].desc;
352
353         if (endpoint[2].wMaxPacketSize == 512) {
354                 printk("[w35und] Working on USB 2.0\n");
355                 pWbUsb->IsUsb20 = 1;
356         }
357
358         if (!wb35_hw_init(dev)) {
359                 err = -EINVAL;
360                 goto error_free_hw;
361         }
362
363         SET_IEEE80211_DEV(dev, &udev->dev);
364         {
365                 phw_data_t pHwData = &priv->sHwData;
366                 unsigned char           dev_addr[MAX_ADDR_LEN];
367                 hal_get_permanent_address(pHwData, dev_addr);
368                 SET_IEEE80211_PERM_ADDR(dev, dev_addr);
369         }
370
371         dev->extra_tx_headroom = 12;    /* FIXME */
372         dev->flags = 0;
373
374         dev->channel_change_time = 1000;
375         dev->queues = 1;
376
377         dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &wbsoft_band_2GHz;
378
379         err = ieee80211_register_hw(dev);
380         if (err)
381                 goto error_free_hw;
382
383         usb_set_intfdata(intf, priv);
384
385         return 0;
386
387 error_free_hw:
388         ieee80211_free_hw(dev);
389 error:
390         usb_put_dev(udev);
391         return err;
392 }
393
394 void packet_came(struct ieee80211_hw *hw, char *pRxBufferAddress, int PacketSize)
395 {
396         struct wbsoft_priv *priv = hw->priv;
397         struct sk_buff *skb;
398         struct ieee80211_rx_status rx_status = {0};
399
400         if (!priv->enabled)
401                 return;
402
403         skb = dev_alloc_skb(PacketSize);
404         if (!skb) {
405                 printk("Not enough memory for packet, FIXME\n");
406                 return;
407         }
408
409         memcpy(skb_put(skb, PacketSize),
410                pRxBufferAddress,
411                PacketSize);
412
413 /*
414         rx_status.rate = 10;
415         rx_status.channel = 1;
416         rx_status.freq = 12345;
417         rx_status.phymode = MODE_IEEE80211B;
418 */
419
420         ieee80211_rx_irqsafe(hw, skb, &rx_status);
421 }
422
423 static void wb35_hw_halt(struct wbsoft_priv *adapter)
424 {
425         Mds_Destroy( adapter );
426
427         // Turn off Rx and Tx hardware ability
428         hal_stop( &adapter->sHwData );
429 #ifdef _PE_USB_INI_DUMP_
430         WBDEBUG(("[w35und] Hal_stop O.K.\n"));
431 #endif
432         msleep(100);// Waiting Irp completed
433
434         // Destroy the NDIS module
435         WBLINUX_stop(adapter);
436
437         // Halt the HAL
438         hal_halt(&adapter->sHwData, NULL);
439 }
440
441
442 static void wb35_disconnect(struct usb_interface *intf)
443 {
444         struct wbsoft_priv *priv = usb_get_intfdata(intf);
445
446         wb35_hw_halt(priv);
447
448         usb_set_intfdata(intf, NULL);
449         usb_put_dev(interface_to_usbdev(intf));
450 }
451
452 static struct usb_driver wb35_driver = {
453         .name           = "w35und",
454         .id_table       = wb35_table,
455         .probe          = wb35_probe,
456         .disconnect     = wb35_disconnect,
457 };
458
459 static int __init wb35_init(void)
460 {
461         return usb_register(&wb35_driver);
462 }
463
464 static void __exit wb35_exit(void)
465 {
466         usb_deregister(&wb35_driver);
467 }
468
469 module_init(wb35_init);
470 module_exit(wb35_exit);