bd708a2d14848042ed31077c05d94eba22d75b57
[firefly-linux-kernel-4.4.55.git] / drivers / staging / winbond / linux / wbusb.c
1 /*
2  * Copyright 2008 Pavel Machek <pavel@suse.cz>
3  *
4  * Distribute under GPLv2.
5  */
6 #include "sysdef.h"
7 #include <net/mac80211.h>
8
9 MODULE_AUTHOR(DRIVER_AUTHOR);
10 MODULE_DESCRIPTION(DRIVER_DESC);
11 MODULE_LICENSE("GPL");
12 MODULE_VERSION("0.1");
13
14 static struct usb_device_id wb35_table[] __devinitdata = {
15         {USB_DEVICE(0x0416, 0x0035)},
16         {USB_DEVICE(0x18E8, 0x6201)},
17         {USB_DEVICE(0x18E8, 0x6206)},
18         {USB_DEVICE(0x18E8, 0x6217)},
19         {USB_DEVICE(0x18E8, 0x6230)},
20         {USB_DEVICE(0x18E8, 0x6233)},
21         {USB_DEVICE(0x1131, 0x2035)},
22         { 0, }
23 };
24
25 MODULE_DEVICE_TABLE(usb, wb35_table);
26
27 static struct ieee80211_rate wbsoft_rates[] = {
28         { .bitrate = 10, .flags = IEEE80211_RATE_SHORT_PREAMBLE },
29 };
30
31 static struct ieee80211_channel wbsoft_channels[] = {
32         { .center_freq = 2412},
33 };
34
35 static struct ieee80211_supported_band wbsoft_band_2GHz = {
36         .channels       = wbsoft_channels,
37         .n_channels     = ARRAY_SIZE(wbsoft_channels),
38         .bitrates       = wbsoft_rates,
39         .n_bitrates     = ARRAY_SIZE(wbsoft_rates),
40 };
41
42 int wbsoft_enabled;
43 struct ieee80211_hw *my_dev;
44 struct wb35_adapter * my_adapter;
45
46 static int wbsoft_add_interface(struct ieee80211_hw *dev,
47                                  struct ieee80211_if_init_conf *conf)
48 {
49         printk("wbsoft_add interface called\n");
50         return 0;
51 }
52
53 static void wbsoft_remove_interface(struct ieee80211_hw *dev,
54                                      struct ieee80211_if_init_conf *conf)
55 {
56         printk("wbsoft_remove interface called\n");
57 }
58
59 static void wbsoft_stop(struct ieee80211_hw *hw)
60 {
61         printk(KERN_INFO "%s called\n", __func__);
62 }
63
64 static int wbsoft_get_stats(struct ieee80211_hw *hw,
65                             struct ieee80211_low_level_stats *stats)
66 {
67         printk(KERN_INFO "%s called\n", __func__);
68         return 0;
69 }
70
71 static int wbsoft_get_tx_stats(struct ieee80211_hw *hw,
72                                struct ieee80211_tx_queue_stats *stats)
73 {
74         printk(KERN_INFO "%s called\n", __func__);
75         return 0;
76 }
77
78 static void wbsoft_configure_filter(struct ieee80211_hw *dev,
79                                      unsigned int changed_flags,
80                                      unsigned int *total_flags,
81                                      int mc_count, struct dev_mc_list *mclist)
82 {
83         unsigned int bit_nr, new_flags;
84         u32 mc_filter[2];
85         int i;
86
87         new_flags = 0;
88
89         if (*total_flags & FIF_PROMISC_IN_BSS) {
90                 new_flags |= FIF_PROMISC_IN_BSS;
91                 mc_filter[1] = mc_filter[0] = ~0;
92         } else if ((*total_flags & FIF_ALLMULTI) || (mc_count > 32)) {
93                 new_flags |= FIF_ALLMULTI;
94                 mc_filter[1] = mc_filter[0] = ~0;
95         } else {
96                 mc_filter[1] = mc_filter[0] = 0;
97                 for (i = 0; i < mc_count; i++) {
98                         if (!mclist)
99                                 break;
100                         printk("Should call ether_crc here\n");
101                         //bit_nr = ether_crc(ETH_ALEN, mclist->dmi_addr) >> 26;
102                         bit_nr = 0;
103
104                         bit_nr &= 0x3F;
105                         mc_filter[bit_nr >> 5] |= 1 << (bit_nr & 31);
106                         mclist = mclist->next;
107                 }
108         }
109
110         dev->flags &= ~IEEE80211_HW_RX_INCLUDES_FCS;
111
112         *total_flags = new_flags;
113 }
114
115 static int wbsoft_tx(struct ieee80211_hw *dev, struct sk_buff *skb)
116 {
117         MLMESendFrame(my_adapter, skb->data, skb->len, FRAME_TYPE_802_11_MANAGEMENT);
118
119         return NETDEV_TX_OK;
120 }
121
122
123 static int wbsoft_start(struct ieee80211_hw *dev)
124 {
125         wbsoft_enabled = 1;
126         printk("wbsoft_start called\n");
127         return 0;
128 }
129
130 static int wbsoft_config(struct ieee80211_hw *dev, struct ieee80211_conf *conf)
131 {
132         ChanInfo ch;
133         printk("wbsoft_config called\n");
134
135         ch.band = 1;
136         ch.ChanNo = 1;  /* Should use channel_num, or something, as that is already pre-translated */
137
138
139         hal_set_current_channel(&my_adapter->sHwData, ch);
140         hal_set_beacon_period(&my_adapter->sHwData, conf->beacon_int);
141 //      hal_set_cap_info(&my_adapter->sHwData, ?? );
142 // hal_set_ssid(phw_data_t pHwData,  u8 * pssid,  u8 ssid_len); ??
143         hal_set_accept_broadcast(&my_adapter->sHwData, 1);
144         hal_set_accept_promiscuous(&my_adapter->sHwData,  1);
145         hal_set_accept_multicast(&my_adapter->sHwData,  1);
146         hal_set_accept_beacon(&my_adapter->sHwData,  1);
147         hal_set_radio_mode(&my_adapter->sHwData,  0);
148         //hal_set_antenna_number(  phw_data_t pHwData, u8 number )
149         //hal_set_rf_power(phw_data_t pHwData, u8 PowerIndex)
150
151
152 //      hal_start_bss(&my_adapter->sHwData, WLAN_BSSTYPE_INFRASTRUCTURE);       ??
153
154 //void hal_set_rates(phw_data_t pHwData, u8 * pbss_rates,
155 //                 u8 length, unsigned char basic_rate_set)
156
157         return 0;
158 }
159
160 static int wbsoft_config_interface(struct ieee80211_hw *dev,
161                                     struct ieee80211_vif *vif,
162                                     struct ieee80211_if_conf *conf)
163 {
164         printk("wbsoft_config_interface called\n");
165         return 0;
166 }
167
168 static u64 wbsoft_get_tsf(struct ieee80211_hw *dev)
169 {
170         printk("wbsoft_get_tsf called\n");
171         return 0;
172 }
173
174 static const struct ieee80211_ops wbsoft_ops = {
175         .tx                     = wbsoft_tx,
176         .start                  = wbsoft_start,         /* Start can be pretty much empty as we do WbWLanInitialize() during probe? */
177         .stop                   = wbsoft_stop,
178         .add_interface          = wbsoft_add_interface,
179         .remove_interface       = wbsoft_remove_interface,
180         .config                 = wbsoft_config,
181         .config_interface       = wbsoft_config_interface,
182         .configure_filter       = wbsoft_configure_filter,
183         .get_stats              = wbsoft_get_stats,
184         .get_tx_stats           = wbsoft_get_tx_stats,
185         .get_tsf                = wbsoft_get_tsf,
186 // conf_tx: hal_set_cwmin()/hal_set_cwmax;
187 };
188
189 struct wbsoft_priv {
190 };
191
192 static int wb35_probe(struct usb_interface *intf, const struct usb_device_id *id_table)
193 {
194         struct wb35_adapter *adapter;
195         PWBUSB          pWbUsb;
196         struct usb_host_interface *interface;
197         struct usb_endpoint_descriptor *endpoint;
198         u32     ltmp;
199         struct usb_device *udev = interface_to_usbdev(intf);
200         struct wbsoft_priv *priv;
201         struct ieee80211_hw *dev;
202         int err;
203
204         usb_get_dev(udev);
205
206         // 20060630.2 Check the device if it already be opened
207         err = usb_control_msg(udev, usb_rcvctrlpipe( udev, 0 ),
208                               0x01, USB_TYPE_VENDOR|USB_RECIP_DEVICE|USB_DIR_IN,
209                               0x0, 0x400, &ltmp, 4, HZ*100 );
210         if (err)
211                 goto error;
212
213         ltmp = cpu_to_le32(ltmp);
214         if (ltmp) {  // Is already initialized?
215                 err = -EBUSY;
216                 goto error;
217         }
218
219         adapter = kzalloc(sizeof(*adapter), GFP_KERNEL);
220         if (!adapter) {
221                 err = -ENOMEM;
222                 goto error;
223         }
224
225         my_adapter = adapter;
226         pWbUsb = &adapter->sHwData.WbUsb;
227         pWbUsb->udev = udev;
228
229         interface = intf->cur_altsetting;
230         endpoint = &interface->endpoint[0].desc;
231
232         if (endpoint[2].wMaxPacketSize == 512) {
233                 printk("[w35und] Working on USB 2.0\n");
234                 pWbUsb->IsUsb20 = 1;
235         }
236
237         if (!WbWLanInitialize(adapter)) {
238                 err = -EINVAL;
239                 goto error_free_adapter;
240         }
241
242         dev = ieee80211_alloc_hw(sizeof(*priv), &wbsoft_ops);
243         if (!dev)
244                 goto error_free_adapter;
245
246         my_dev = dev;
247
248         SET_IEEE80211_DEV(dev, &udev->dev);
249         {
250                 phw_data_t pHwData = &adapter->sHwData;
251                 unsigned char dev_addr[MAX_ADDR_LEN];
252                 hal_get_permanent_address(pHwData, dev_addr);
253                 SET_IEEE80211_PERM_ADDR(dev, dev_addr);
254         }
255
256         dev->extra_tx_headroom = 12;    /* FIXME */
257         dev->flags = 0;
258
259         dev->channel_change_time = 1000;
260         dev->queues = 1;
261
262         dev->wiphy->bands[IEEE80211_BAND_2GHZ] = &wbsoft_band_2GHz;
263
264         err = ieee80211_register_hw(dev);
265         if (err)
266                 goto error_free_hw;
267
268         usb_set_intfdata(intf, adapter);
269
270         return 0;
271
272 error_free_hw:
273         ieee80211_free_hw(dev);
274 error_free_adapter:
275         kfree(adapter);
276 error:
277         usb_put_dev(udev);
278         return err;
279 }
280
281 void packet_came(char *pRxBufferAddress, int PacketSize)
282 {
283         struct sk_buff *skb;
284         struct ieee80211_rx_status rx_status = {0};
285
286         if (!wbsoft_enabled)
287                 return;
288
289         skb = dev_alloc_skb(PacketSize);
290         if (!skb) {
291                 printk("Not enough memory for packet, FIXME\n");
292                 return;
293         }
294
295         memcpy(skb_put(skb, PacketSize),
296                pRxBufferAddress,
297                PacketSize);
298
299 /*
300         rx_status.rate = 10;
301         rx_status.channel = 1;
302         rx_status.freq = 12345;
303         rx_status.phymode = MODE_IEEE80211B;
304 */
305
306         ieee80211_rx_irqsafe(my_dev, skb, &rx_status);
307 }
308
309 static void wb35_disconnect(struct usb_interface *intf)
310 {
311         struct wb35_adapter *adapter = usb_get_intfdata(intf);
312
313         WbWlanHalt(adapter);
314
315         usb_set_intfdata(intf, NULL);
316         usb_put_dev(interface_to_usbdev(intf));
317 }
318
319 static struct usb_driver wb35_driver = {
320         .name           = "w35und",
321         .id_table       = wb35_table,
322         .probe          = wb35_probe,
323         .disconnect     = wb35_disconnect,
324 };
325
326 static int __init wb35_init(void)
327 {
328         return usb_register(&wb35_driver);
329 }
330
331 static void __exit wb35_exit(void)
332 {
333         usb_deregister(&wb35_driver);
334 }
335
336 module_init(wb35_init);
337 module_exit(wb35_exit);