]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/net/wireless/libertas/main.c
libertas: Check return status of command functions
[mirror_ubuntu-bionic-kernel.git] / drivers / net / wireless / libertas / main.c
1 /**
2 * This file contains the major functions in WLAN
3 * driver. It includes init, exit, open, close and main
4 * thread etc..
5 */
6
7 #include <linux/moduleparam.h>
8 #include <linux/delay.h>
9 #include <linux/etherdevice.h>
10 #include <linux/netdevice.h>
11 #include <linux/if_arp.h>
12 #include <linux/kthread.h>
13 #include <linux/kfifo.h>
14 #include <linux/stddef.h>
15 #include <linux/ieee80211.h>
16 #include <net/iw_handler.h>
17 #include <net/cfg80211.h>
18
19 #include "host.h"
20 #include "decl.h"
21 #include "dev.h"
22 #include "wext.h"
23 #include "cfg.h"
24 #include "debugfs.h"
25 #include "scan.h"
26 #include "assoc.h"
27 #include "cmd.h"
28
29 #define DRIVER_RELEASE_VERSION "323.p0"
30 const char lbs_driver_version[] = "COMM-USB8388-" DRIVER_RELEASE_VERSION
31 #ifdef DEBUG
32 "-dbg"
33 #endif
34 "";
35
36
37 /* Module parameters */
38 unsigned int lbs_debug;
39 EXPORT_SYMBOL_GPL(lbs_debug);
40 module_param_named(libertas_debug, lbs_debug, int, 0644);
41
42
43 /* This global structure is used to send the confirm_sleep command as
44 * fast as possible down to the firmware. */
45 struct cmd_confirm_sleep confirm_sleep;
46
47
48 #define LBS_TX_PWR_DEFAULT 20 /*100mW */
49 #define LBS_TX_PWR_US_DEFAULT 20 /*100mW */
50 #define LBS_TX_PWR_JP_DEFAULT 16 /*50mW */
51 #define LBS_TX_PWR_FR_DEFAULT 20 /*100mW */
52 #define LBS_TX_PWR_EMEA_DEFAULT 20 /*100mW */
53
54 /* Format { channel, frequency (MHz), maxtxpower } */
55 /* band: 'B/G', region: USA FCC/Canada IC */
56 static struct chan_freq_power channel_freq_power_US_BG[] = {
57 {1, 2412, LBS_TX_PWR_US_DEFAULT},
58 {2, 2417, LBS_TX_PWR_US_DEFAULT},
59 {3, 2422, LBS_TX_PWR_US_DEFAULT},
60 {4, 2427, LBS_TX_PWR_US_DEFAULT},
61 {5, 2432, LBS_TX_PWR_US_DEFAULT},
62 {6, 2437, LBS_TX_PWR_US_DEFAULT},
63 {7, 2442, LBS_TX_PWR_US_DEFAULT},
64 {8, 2447, LBS_TX_PWR_US_DEFAULT},
65 {9, 2452, LBS_TX_PWR_US_DEFAULT},
66 {10, 2457, LBS_TX_PWR_US_DEFAULT},
67 {11, 2462, LBS_TX_PWR_US_DEFAULT}
68 };
69
70 /* band: 'B/G', region: Europe ETSI */
71 static struct chan_freq_power channel_freq_power_EU_BG[] = {
72 {1, 2412, LBS_TX_PWR_EMEA_DEFAULT},
73 {2, 2417, LBS_TX_PWR_EMEA_DEFAULT},
74 {3, 2422, LBS_TX_PWR_EMEA_DEFAULT},
75 {4, 2427, LBS_TX_PWR_EMEA_DEFAULT},
76 {5, 2432, LBS_TX_PWR_EMEA_DEFAULT},
77 {6, 2437, LBS_TX_PWR_EMEA_DEFAULT},
78 {7, 2442, LBS_TX_PWR_EMEA_DEFAULT},
79 {8, 2447, LBS_TX_PWR_EMEA_DEFAULT},
80 {9, 2452, LBS_TX_PWR_EMEA_DEFAULT},
81 {10, 2457, LBS_TX_PWR_EMEA_DEFAULT},
82 {11, 2462, LBS_TX_PWR_EMEA_DEFAULT},
83 {12, 2467, LBS_TX_PWR_EMEA_DEFAULT},
84 {13, 2472, LBS_TX_PWR_EMEA_DEFAULT}
85 };
86
87 /* band: 'B/G', region: Spain */
88 static struct chan_freq_power channel_freq_power_SPN_BG[] = {
89 {10, 2457, LBS_TX_PWR_DEFAULT},
90 {11, 2462, LBS_TX_PWR_DEFAULT}
91 };
92
93 /* band: 'B/G', region: France */
94 static struct chan_freq_power channel_freq_power_FR_BG[] = {
95 {10, 2457, LBS_TX_PWR_FR_DEFAULT},
96 {11, 2462, LBS_TX_PWR_FR_DEFAULT},
97 {12, 2467, LBS_TX_PWR_FR_DEFAULT},
98 {13, 2472, LBS_TX_PWR_FR_DEFAULT}
99 };
100
101 /* band: 'B/G', region: Japan */
102 static struct chan_freq_power channel_freq_power_JPN_BG[] = {
103 {1, 2412, LBS_TX_PWR_JP_DEFAULT},
104 {2, 2417, LBS_TX_PWR_JP_DEFAULT},
105 {3, 2422, LBS_TX_PWR_JP_DEFAULT},
106 {4, 2427, LBS_TX_PWR_JP_DEFAULT},
107 {5, 2432, LBS_TX_PWR_JP_DEFAULT},
108 {6, 2437, LBS_TX_PWR_JP_DEFAULT},
109 {7, 2442, LBS_TX_PWR_JP_DEFAULT},
110 {8, 2447, LBS_TX_PWR_JP_DEFAULT},
111 {9, 2452, LBS_TX_PWR_JP_DEFAULT},
112 {10, 2457, LBS_TX_PWR_JP_DEFAULT},
113 {11, 2462, LBS_TX_PWR_JP_DEFAULT},
114 {12, 2467, LBS_TX_PWR_JP_DEFAULT},
115 {13, 2472, LBS_TX_PWR_JP_DEFAULT},
116 {14, 2484, LBS_TX_PWR_JP_DEFAULT}
117 };
118
119 /**
120 * the structure for channel, frequency and power
121 */
122 struct region_cfp_table {
123 u8 region;
124 struct chan_freq_power *cfp_BG;
125 int cfp_no_BG;
126 };
127
128 /**
129 * the structure for the mapping between region and CFP
130 */
131 static struct region_cfp_table region_cfp_table[] = {
132 {0x10, /*US FCC */
133 channel_freq_power_US_BG,
134 ARRAY_SIZE(channel_freq_power_US_BG),
135 }
136 ,
137 {0x20, /*CANADA IC */
138 channel_freq_power_US_BG,
139 ARRAY_SIZE(channel_freq_power_US_BG),
140 }
141 ,
142 {0x30, /*EU*/ channel_freq_power_EU_BG,
143 ARRAY_SIZE(channel_freq_power_EU_BG),
144 }
145 ,
146 {0x31, /*SPAIN*/ channel_freq_power_SPN_BG,
147 ARRAY_SIZE(channel_freq_power_SPN_BG),
148 }
149 ,
150 {0x32, /*FRANCE*/ channel_freq_power_FR_BG,
151 ARRAY_SIZE(channel_freq_power_FR_BG),
152 }
153 ,
154 {0x40, /*JAPAN*/ channel_freq_power_JPN_BG,
155 ARRAY_SIZE(channel_freq_power_JPN_BG),
156 }
157 ,
158 /*Add new region here */
159 };
160
161 /**
162 * the table to keep region code
163 */
164 u16 lbs_region_code_to_index[MRVDRV_MAX_REGION_CODE] =
165 { 0x10, 0x20, 0x30, 0x31, 0x32, 0x40 };
166
167 /**
168 * 802.11b/g supported bitrates (in 500Kb/s units)
169 */
170 u8 lbs_bg_rates[MAX_RATES] =
171 { 0x02, 0x04, 0x0b, 0x16, 0x0c, 0x12, 0x18, 0x24, 0x30, 0x48, 0x60, 0x6c,
172 0x00, 0x00 };
173
174 /**
175 * FW rate table. FW refers to rates by their index in this table, not by the
176 * rate value itself. Values of 0x00 are
177 * reserved positions.
178 */
179 static u8 fw_data_rates[MAX_RATES] =
180 { 0x02, 0x04, 0x0B, 0x16, 0x00, 0x0C, 0x12,
181 0x18, 0x24, 0x30, 0x48, 0x60, 0x6C, 0x00
182 };
183
184 /**
185 * @brief use index to get the data rate
186 *
187 * @param idx The index of data rate
188 * @return data rate or 0
189 */
190 u32 lbs_fw_index_to_data_rate(u8 idx)
191 {
192 if (idx >= sizeof(fw_data_rates))
193 idx = 0;
194 return fw_data_rates[idx];
195 }
196
197 /**
198 * @brief use rate to get the index
199 *
200 * @param rate data rate
201 * @return index or 0
202 */
203 u8 lbs_data_rate_to_fw_index(u32 rate)
204 {
205 u8 i;
206
207 if (!rate)
208 return 0;
209
210 for (i = 0; i < sizeof(fw_data_rates); i++) {
211 if (rate == fw_data_rates[i])
212 return i;
213 }
214 return 0;
215 }
216
217 /**
218 * Attributes exported through sysfs
219 */
220
221 /**
222 * @brief Get function for sysfs attribute anycast_mask
223 */
224 static ssize_t lbs_anycast_get(struct device *dev,
225 struct device_attribute *attr, char * buf)
226 {
227 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
228 struct cmd_ds_mesh_access mesh_access;
229 int ret;
230
231 memset(&mesh_access, 0, sizeof(mesh_access));
232
233 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
234 if (ret)
235 return ret;
236
237 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
238 }
239
240 /**
241 * @brief Set function for sysfs attribute anycast_mask
242 */
243 static ssize_t lbs_anycast_set(struct device *dev,
244 struct device_attribute *attr, const char * buf, size_t count)
245 {
246 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
247 struct cmd_ds_mesh_access mesh_access;
248 uint32_t datum;
249 int ret;
250
251 memset(&mesh_access, 0, sizeof(mesh_access));
252 sscanf(buf, "%x", &datum);
253 mesh_access.data[0] = cpu_to_le32(datum);
254
255 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
256 if (ret)
257 return ret;
258
259 return strlen(buf);
260 }
261
262 /**
263 * @brief Get function for sysfs attribute prb_rsp_limit
264 */
265 static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
266 struct device_attribute *attr, char *buf)
267 {
268 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
269 struct cmd_ds_mesh_access mesh_access;
270 int ret;
271 u32 retry_limit;
272
273 memset(&mesh_access, 0, sizeof(mesh_access));
274 mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
275
276 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
277 &mesh_access);
278 if (ret)
279 return ret;
280
281 retry_limit = le32_to_cpu(mesh_access.data[1]);
282 return snprintf(buf, 10, "%d\n", retry_limit);
283 }
284
285 /**
286 * @brief Set function for sysfs attribute prb_rsp_limit
287 */
288 static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
289 struct device_attribute *attr, const char *buf, size_t count)
290 {
291 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
292 struct cmd_ds_mesh_access mesh_access;
293 int ret;
294 unsigned long retry_limit;
295
296 memset(&mesh_access, 0, sizeof(mesh_access));
297 mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
298
299 if (!strict_strtoul(buf, 10, &retry_limit))
300 return -ENOTSUPP;
301 if (retry_limit > 15)
302 return -ENOTSUPP;
303
304 mesh_access.data[1] = cpu_to_le32(retry_limit);
305
306 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
307 &mesh_access);
308 if (ret)
309 return ret;
310
311 return strlen(buf);
312 }
313
314 static int lbs_add_rtap(struct lbs_private *priv);
315 static void lbs_remove_rtap(struct lbs_private *priv);
316 static int lbs_add_mesh(struct lbs_private *priv);
317 static void lbs_remove_mesh(struct lbs_private *priv);
318
319
320 /**
321 * Get function for sysfs attribute rtap
322 */
323 static ssize_t lbs_rtap_get(struct device *dev,
324 struct device_attribute *attr, char * buf)
325 {
326 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
327 return snprintf(buf, 5, "0x%X\n", priv->monitormode);
328 }
329
330 /**
331 * Set function for sysfs attribute rtap
332 */
333 static ssize_t lbs_rtap_set(struct device *dev,
334 struct device_attribute *attr, const char * buf, size_t count)
335 {
336 int monitor_mode;
337 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
338
339 sscanf(buf, "%x", &monitor_mode);
340 if (monitor_mode) {
341 if (priv->monitormode == monitor_mode)
342 return strlen(buf);
343 if (!priv->monitormode) {
344 if (priv->infra_open || priv->mesh_open)
345 return -EBUSY;
346 if (priv->mode == IW_MODE_INFRA)
347 lbs_cmd_80211_deauthenticate(priv,
348 priv->curbssparams.bssid,
349 WLAN_REASON_DEAUTH_LEAVING);
350 else if (priv->mode == IW_MODE_ADHOC)
351 lbs_adhoc_stop(priv);
352 lbs_add_rtap(priv);
353 }
354 priv->monitormode = monitor_mode;
355 } else {
356 if (!priv->monitormode)
357 return strlen(buf);
358 priv->monitormode = 0;
359 lbs_remove_rtap(priv);
360
361 if (priv->currenttxskb) {
362 dev_kfree_skb_any(priv->currenttxskb);
363 priv->currenttxskb = NULL;
364 }
365
366 /* Wake queues, command thread, etc. */
367 lbs_host_to_card_done(priv);
368 }
369
370 lbs_prepare_and_send_command(priv,
371 CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
372 CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
373 return strlen(buf);
374 }
375
376 /**
377 * lbs_rtap attribute to be exported per ethX interface
378 * through sysfs (/sys/class/net/ethX/lbs_rtap)
379 */
380 static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
381
382 /**
383 * Get function for sysfs attribute mesh
384 */
385 static ssize_t lbs_mesh_get(struct device *dev,
386 struct device_attribute *attr, char * buf)
387 {
388 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
389 return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
390 }
391
392 /**
393 * Set function for sysfs attribute mesh
394 */
395 static ssize_t lbs_mesh_set(struct device *dev,
396 struct device_attribute *attr, const char * buf, size_t count)
397 {
398 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
399 int enable;
400 int ret, action = CMD_ACT_MESH_CONFIG_STOP;
401
402 sscanf(buf, "%x", &enable);
403 enable = !!enable;
404 if (enable == !!priv->mesh_dev)
405 return count;
406 if (enable)
407 action = CMD_ACT_MESH_CONFIG_START;
408 ret = lbs_mesh_config(priv, action, priv->curbssparams.channel);
409 if (ret)
410 return ret;
411
412 if (enable)
413 lbs_add_mesh(priv);
414 else
415 lbs_remove_mesh(priv);
416
417 return count;
418 }
419
420 /**
421 * lbs_mesh attribute to be exported per ethX interface
422 * through sysfs (/sys/class/net/ethX/lbs_mesh)
423 */
424 static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
425
426 /**
427 * anycast_mask attribute to be exported per mshX interface
428 * through sysfs (/sys/class/net/mshX/anycast_mask)
429 */
430 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
431
432 /**
433 * prb_rsp_limit attribute to be exported per mshX interface
434 * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
435 */
436 static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
437 lbs_prb_rsp_limit_set);
438
439 static struct attribute *lbs_mesh_sysfs_entries[] = {
440 &dev_attr_anycast_mask.attr,
441 &dev_attr_prb_rsp_limit.attr,
442 NULL,
443 };
444
445 static struct attribute_group lbs_mesh_attr_group = {
446 .attrs = lbs_mesh_sysfs_entries,
447 };
448
449 /**
450 * @brief This function opens the ethX or mshX interface
451 *
452 * @param dev A pointer to net_device structure
453 * @return 0 or -EBUSY if monitor mode active
454 */
455 static int lbs_dev_open(struct net_device *dev)
456 {
457 struct lbs_private *priv = dev->ml_priv;
458 int ret = 0;
459
460 lbs_deb_enter(LBS_DEB_NET);
461
462 spin_lock_irq(&priv->driver_lock);
463
464 if (priv->monitormode) {
465 ret = -EBUSY;
466 goto out;
467 }
468
469 if (dev == priv->mesh_dev) {
470 priv->mesh_open = 1;
471 priv->mesh_connect_status = LBS_CONNECTED;
472 netif_carrier_on(dev);
473 } else {
474 priv->infra_open = 1;
475
476 if (priv->connect_status == LBS_CONNECTED)
477 netif_carrier_on(dev);
478 else
479 netif_carrier_off(dev);
480 }
481
482 if (!priv->tx_pending_len)
483 netif_wake_queue(dev);
484 out:
485
486 spin_unlock_irq(&priv->driver_lock);
487 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
488 return ret;
489 }
490
491 /**
492 * @brief This function closes the mshX interface
493 *
494 * @param dev A pointer to net_device structure
495 * @return 0
496 */
497 static int lbs_mesh_stop(struct net_device *dev)
498 {
499 struct lbs_private *priv = dev->ml_priv;
500
501 lbs_deb_enter(LBS_DEB_MESH);
502 spin_lock_irq(&priv->driver_lock);
503
504 priv->mesh_open = 0;
505 priv->mesh_connect_status = LBS_DISCONNECTED;
506
507 netif_stop_queue(dev);
508 netif_carrier_off(dev);
509
510 spin_unlock_irq(&priv->driver_lock);
511
512 schedule_work(&priv->mcast_work);
513
514 lbs_deb_leave(LBS_DEB_MESH);
515 return 0;
516 }
517
518 /**
519 * @brief This function closes the ethX interface
520 *
521 * @param dev A pointer to net_device structure
522 * @return 0
523 */
524 static int lbs_eth_stop(struct net_device *dev)
525 {
526 struct lbs_private *priv = dev->ml_priv;
527
528 lbs_deb_enter(LBS_DEB_NET);
529
530 spin_lock_irq(&priv->driver_lock);
531 priv->infra_open = 0;
532 netif_stop_queue(dev);
533 spin_unlock_irq(&priv->driver_lock);
534
535 schedule_work(&priv->mcast_work);
536
537 lbs_deb_leave(LBS_DEB_NET);
538 return 0;
539 }
540
541 static void lbs_tx_timeout(struct net_device *dev)
542 {
543 struct lbs_private *priv = dev->ml_priv;
544
545 lbs_deb_enter(LBS_DEB_TX);
546
547 lbs_pr_err("tx watch dog timeout\n");
548
549 dev->trans_start = jiffies;
550
551 if (priv->currenttxskb)
552 lbs_send_tx_feedback(priv, 0);
553
554 /* XX: Shouldn't we also call into the hw-specific driver
555 to kick it somehow? */
556 lbs_host_to_card_done(priv);
557
558 /* More often than not, this actually happens because the
559 firmware has crapped itself -- rather than just a very
560 busy medium. So send a harmless command, and if/when
561 _that_ times out, we'll kick it in the head. */
562 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
563 0, 0, NULL);
564
565 lbs_deb_leave(LBS_DEB_TX);
566 }
567
568 void lbs_host_to_card_done(struct lbs_private *priv)
569 {
570 unsigned long flags;
571
572 lbs_deb_enter(LBS_DEB_THREAD);
573
574 spin_lock_irqsave(&priv->driver_lock, flags);
575
576 priv->dnld_sent = DNLD_RES_RECEIVED;
577
578 /* Wake main thread if commands are pending */
579 if (!priv->cur_cmd || priv->tx_pending_len > 0) {
580 if (!priv->wakeup_dev_required)
581 wake_up_interruptible(&priv->waitq);
582 }
583
584 spin_unlock_irqrestore(&priv->driver_lock, flags);
585 lbs_deb_leave(LBS_DEB_THREAD);
586 }
587 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
588
589 static int lbs_set_mac_address(struct net_device *dev, void *addr)
590 {
591 int ret = 0;
592 struct lbs_private *priv = dev->ml_priv;
593 struct sockaddr *phwaddr = addr;
594 struct cmd_ds_802_11_mac_address cmd;
595
596 lbs_deb_enter(LBS_DEB_NET);
597
598 /* In case it was called from the mesh device */
599 dev = priv->dev;
600
601 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
602 cmd.action = cpu_to_le16(CMD_ACT_SET);
603 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
604
605 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
606 if (ret) {
607 lbs_deb_net("set MAC address failed\n");
608 goto done;
609 }
610
611 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
612 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
613 if (priv->mesh_dev)
614 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
615
616 done:
617 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
618 return ret;
619 }
620
621
622 static inline int mac_in_list(unsigned char *list, int list_len,
623 unsigned char *mac)
624 {
625 while (list_len) {
626 if (!memcmp(list, mac, ETH_ALEN))
627 return 1;
628 list += ETH_ALEN;
629 list_len--;
630 }
631 return 0;
632 }
633
634
635 static int lbs_add_mcast_addrs(struct cmd_ds_mac_multicast_adr *cmd,
636 struct net_device *dev, int nr_addrs)
637 {
638 int i = nr_addrs;
639 struct dev_mc_list *mc_list;
640
641 if ((dev->flags & (IFF_UP|IFF_MULTICAST)) != (IFF_UP|IFF_MULTICAST))
642 return nr_addrs;
643
644 netif_addr_lock_bh(dev);
645 for (mc_list = dev->mc_list; mc_list; mc_list = mc_list->next) {
646 if (mac_in_list(cmd->maclist, nr_addrs, mc_list->dmi_addr)) {
647 lbs_deb_net("mcast address %s:%pM skipped\n", dev->name,
648 mc_list->dmi_addr);
649 continue;
650 }
651
652 if (i == MRVDRV_MAX_MULTICAST_LIST_SIZE)
653 break;
654 memcpy(&cmd->maclist[6*i], mc_list->dmi_addr, ETH_ALEN);
655 lbs_deb_net("mcast address %s:%pM added to filter\n", dev->name,
656 mc_list->dmi_addr);
657 i++;
658 }
659 netif_addr_unlock_bh(dev);
660 if (mc_list)
661 return -EOVERFLOW;
662
663 return i;
664 }
665
666 static void lbs_set_mcast_worker(struct work_struct *work)
667 {
668 struct lbs_private *priv = container_of(work, struct lbs_private, mcast_work);
669 struct cmd_ds_mac_multicast_adr mcast_cmd;
670 int dev_flags;
671 int nr_addrs;
672 int old_mac_control = priv->mac_control;
673
674 lbs_deb_enter(LBS_DEB_NET);
675
676 dev_flags = priv->dev->flags;
677 if (priv->mesh_dev)
678 dev_flags |= priv->mesh_dev->flags;
679
680 if (dev_flags & IFF_PROMISC) {
681 priv->mac_control |= CMD_ACT_MAC_PROMISCUOUS_ENABLE;
682 priv->mac_control &= ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
683 CMD_ACT_MAC_MULTICAST_ENABLE);
684 goto out_set_mac_control;
685 } else if (dev_flags & IFF_ALLMULTI) {
686 do_allmulti:
687 priv->mac_control |= CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
688 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
689 CMD_ACT_MAC_MULTICAST_ENABLE);
690 goto out_set_mac_control;
691 }
692
693 /* Once for priv->dev, again for priv->mesh_dev if it exists */
694 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->dev, 0);
695 if (nr_addrs >= 0 && priv->mesh_dev)
696 nr_addrs = lbs_add_mcast_addrs(&mcast_cmd, priv->mesh_dev, nr_addrs);
697 if (nr_addrs < 0)
698 goto do_allmulti;
699
700 if (nr_addrs) {
701 int size = offsetof(struct cmd_ds_mac_multicast_adr,
702 maclist[6*nr_addrs]);
703
704 mcast_cmd.action = cpu_to_le16(CMD_ACT_SET);
705 mcast_cmd.hdr.size = cpu_to_le16(size);
706 mcast_cmd.nr_of_adrs = cpu_to_le16(nr_addrs);
707
708 lbs_cmd_async(priv, CMD_MAC_MULTICAST_ADR, &mcast_cmd.hdr, size);
709
710 priv->mac_control |= CMD_ACT_MAC_MULTICAST_ENABLE;
711 } else
712 priv->mac_control &= ~CMD_ACT_MAC_MULTICAST_ENABLE;
713
714 priv->mac_control &= ~(CMD_ACT_MAC_PROMISCUOUS_ENABLE |
715 CMD_ACT_MAC_ALL_MULTICAST_ENABLE);
716 out_set_mac_control:
717 if (priv->mac_control != old_mac_control)
718 lbs_set_mac_control(priv);
719
720 lbs_deb_leave(LBS_DEB_NET);
721 }
722
723 static void lbs_set_multicast_list(struct net_device *dev)
724 {
725 struct lbs_private *priv = dev->ml_priv;
726
727 schedule_work(&priv->mcast_work);
728 }
729
730 /**
731 * @brief This function handles the major jobs in the LBS driver.
732 * It handles all events generated by firmware, RX data received
733 * from firmware and TX data sent from kernel.
734 *
735 * @param data A pointer to lbs_thread structure
736 * @return 0
737 */
738 static int lbs_thread(void *data)
739 {
740 struct net_device *dev = data;
741 struct lbs_private *priv = dev->ml_priv;
742 wait_queue_t wait;
743
744 lbs_deb_enter(LBS_DEB_THREAD);
745
746 init_waitqueue_entry(&wait, current);
747
748 for (;;) {
749 int shouldsleep;
750 u8 resp_idx;
751
752 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
753 priv->currenttxskb, priv->dnld_sent);
754
755 add_wait_queue(&priv->waitq, &wait);
756 set_current_state(TASK_INTERRUPTIBLE);
757 spin_lock_irq(&priv->driver_lock);
758
759 if (kthread_should_stop())
760 shouldsleep = 0; /* Bye */
761 else if (priv->surpriseremoved)
762 shouldsleep = 1; /* We need to wait until we're _told_ to die */
763 else if (priv->psstate == PS_STATE_SLEEP)
764 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
765 else if (priv->cmd_timed_out)
766 shouldsleep = 0; /* Command timed out. Recover */
767 else if (!priv->fw_ready)
768 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
769 else if (priv->dnld_sent)
770 shouldsleep = 1; /* Something is en route to the device already */
771 else if (priv->tx_pending_len > 0)
772 shouldsleep = 0; /* We've a packet to send */
773 else if (priv->resp_len[priv->resp_idx])
774 shouldsleep = 0; /* We have a command response */
775 else if (priv->cur_cmd)
776 shouldsleep = 1; /* Can't send a command; one already running */
777 else if (!list_empty(&priv->cmdpendingq) &&
778 !(priv->wakeup_dev_required))
779 shouldsleep = 0; /* We have a command to send */
780 else if (__kfifo_len(priv->event_fifo))
781 shouldsleep = 0; /* We have an event to process */
782 else
783 shouldsleep = 1; /* No command */
784
785 if (shouldsleep) {
786 lbs_deb_thread("sleeping, connect_status %d, "
787 "psmode %d, psstate %d\n",
788 priv->connect_status,
789 priv->psmode, priv->psstate);
790 spin_unlock_irq(&priv->driver_lock);
791 schedule();
792 } else
793 spin_unlock_irq(&priv->driver_lock);
794
795 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
796 priv->currenttxskb, priv->dnld_sent);
797
798 set_current_state(TASK_RUNNING);
799 remove_wait_queue(&priv->waitq, &wait);
800
801 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
802 priv->currenttxskb, priv->dnld_sent);
803
804 if (kthread_should_stop()) {
805 lbs_deb_thread("break from main thread\n");
806 break;
807 }
808
809 if (priv->surpriseremoved) {
810 lbs_deb_thread("adapter removed; waiting to die...\n");
811 continue;
812 }
813
814 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
815 priv->currenttxskb, priv->dnld_sent);
816
817 /* Process any pending command response */
818 spin_lock_irq(&priv->driver_lock);
819 resp_idx = priv->resp_idx;
820 if (priv->resp_len[resp_idx]) {
821 spin_unlock_irq(&priv->driver_lock);
822 lbs_process_command_response(priv,
823 priv->resp_buf[resp_idx],
824 priv->resp_len[resp_idx]);
825 spin_lock_irq(&priv->driver_lock);
826 priv->resp_len[resp_idx] = 0;
827 }
828 spin_unlock_irq(&priv->driver_lock);
829
830 /* Process hardware events, e.g. card removed, link lost */
831 spin_lock_irq(&priv->driver_lock);
832 while (__kfifo_len(priv->event_fifo)) {
833 u32 event;
834 __kfifo_get(priv->event_fifo, (unsigned char *) &event,
835 sizeof(event));
836 spin_unlock_irq(&priv->driver_lock);
837 lbs_process_event(priv, event);
838 spin_lock_irq(&priv->driver_lock);
839 }
840 spin_unlock_irq(&priv->driver_lock);
841
842 if (priv->wakeup_dev_required) {
843 lbs_deb_thread("Waking up device...\n");
844 /* Wake up device */
845 if (priv->exit_deep_sleep(priv))
846 lbs_deb_thread("Wakeup device failed\n");
847 continue;
848 }
849
850 /* command timeout stuff */
851 if (priv->cmd_timed_out && priv->cur_cmd) {
852 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
853
854 if (++priv->nr_retries > 3) {
855 lbs_pr_info("Excessive timeouts submitting "
856 "command 0x%04x\n",
857 le16_to_cpu(cmdnode->cmdbuf->command));
858 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
859 priv->nr_retries = 0;
860 if (priv->reset_card)
861 priv->reset_card(priv);
862 } else {
863 priv->cur_cmd = NULL;
864 priv->dnld_sent = DNLD_RES_RECEIVED;
865 lbs_pr_info("requeueing command 0x%04x due "
866 "to timeout (#%d)\n",
867 le16_to_cpu(cmdnode->cmdbuf->command),
868 priv->nr_retries);
869
870 /* Stick it back at the _top_ of the pending queue
871 for immediate resubmission */
872 list_add(&cmdnode->list, &priv->cmdpendingq);
873 }
874 }
875 priv->cmd_timed_out = 0;
876
877
878
879 if (!priv->fw_ready)
880 continue;
881
882 /* Check if we need to confirm Sleep Request received previously */
883 if (priv->psstate == PS_STATE_PRE_SLEEP &&
884 !priv->dnld_sent && !priv->cur_cmd) {
885 if (priv->connect_status == LBS_CONNECTED) {
886 lbs_deb_thread("pre-sleep, currenttxskb %p, "
887 "dnld_sent %d, cur_cmd %p\n",
888 priv->currenttxskb, priv->dnld_sent,
889 priv->cur_cmd);
890
891 lbs_ps_confirm_sleep(priv);
892 } else {
893 /* workaround for firmware sending
894 * deauth/linkloss event immediately
895 * after sleep request; remove this
896 * after firmware fixes it
897 */
898 priv->psstate = PS_STATE_AWAKE;
899 lbs_pr_alert("ignore PS_SleepConfirm in "
900 "non-connected state\n");
901 }
902 }
903
904 /* The PS state is changed during processing of Sleep Request
905 * event above
906 */
907 if ((priv->psstate == PS_STATE_SLEEP) ||
908 (priv->psstate == PS_STATE_PRE_SLEEP))
909 continue;
910
911 if (priv->is_deep_sleep)
912 continue;
913
914 /* Execute the next command */
915 if (!priv->dnld_sent && !priv->cur_cmd)
916 lbs_execute_next_command(priv);
917
918 /* Wake-up command waiters which can't sleep in
919 * lbs_prepare_and_send_command
920 */
921 if (!list_empty(&priv->cmdpendingq))
922 wake_up_all(&priv->cmd_pending);
923
924 spin_lock_irq(&priv->driver_lock);
925 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
926 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
927 priv->tx_pending_buf,
928 priv->tx_pending_len);
929 if (ret) {
930 lbs_deb_tx("host_to_card failed %d\n", ret);
931 priv->dnld_sent = DNLD_RES_RECEIVED;
932 }
933 priv->tx_pending_len = 0;
934 if (!priv->currenttxskb) {
935 /* We can wake the queues immediately if we aren't
936 waiting for TX feedback */
937 if (priv->connect_status == LBS_CONNECTED)
938 netif_wake_queue(priv->dev);
939 if (priv->mesh_dev &&
940 priv->mesh_connect_status == LBS_CONNECTED)
941 netif_wake_queue(priv->mesh_dev);
942 }
943 }
944 spin_unlock_irq(&priv->driver_lock);
945 }
946
947 del_timer(&priv->command_timer);
948 del_timer(&priv->auto_deepsleep_timer);
949 wake_up_all(&priv->cmd_pending);
950
951 lbs_deb_leave(LBS_DEB_THREAD);
952 return 0;
953 }
954
955 static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
956 struct cmd_header *cmd)
957 {
958 lbs_deb_enter(LBS_DEB_FW);
959
960 netif_device_detach(priv->dev);
961 if (priv->mesh_dev)
962 netif_device_detach(priv->mesh_dev);
963
964 priv->fw_ready = 0;
965 lbs_deb_leave(LBS_DEB_FW);
966 return 0;
967 }
968
969 int lbs_suspend(struct lbs_private *priv)
970 {
971 struct cmd_header cmd;
972 int ret;
973
974 lbs_deb_enter(LBS_DEB_FW);
975
976 if (priv->wol_criteria == 0xffffffff) {
977 lbs_pr_info("Suspend attempt without configuring wake params!\n");
978 return -EINVAL;
979 }
980
981 memset(&cmd, 0, sizeof(cmd));
982
983 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
984 sizeof(cmd), lbs_suspend_callback, 0);
985 if (ret)
986 lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
987
988 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
989 return ret;
990 }
991 EXPORT_SYMBOL_GPL(lbs_suspend);
992
993 void lbs_resume(struct lbs_private *priv)
994 {
995 lbs_deb_enter(LBS_DEB_FW);
996
997 priv->fw_ready = 1;
998
999 /* Firmware doesn't seem to give us RX packets any more
1000 until we send it some command. Might as well update */
1001 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
1002 0, 0, NULL);
1003
1004 netif_device_attach(priv->dev);
1005 if (priv->mesh_dev)
1006 netif_device_attach(priv->mesh_dev);
1007
1008 lbs_deb_leave(LBS_DEB_FW);
1009 }
1010 EXPORT_SYMBOL_GPL(lbs_resume);
1011
1012 /**
1013 * @brief This function gets the HW spec from the firmware and sets
1014 * some basic parameters.
1015 *
1016 * @param priv A pointer to struct lbs_private structure
1017 * @return 0 or -1
1018 */
1019 static int lbs_setup_firmware(struct lbs_private *priv)
1020 {
1021 int ret = -1;
1022 s16 curlevel = 0, minlevel = 0, maxlevel = 0;
1023
1024 lbs_deb_enter(LBS_DEB_FW);
1025
1026 /* Read MAC address from firmware */
1027 memset(priv->current_addr, 0xff, ETH_ALEN);
1028 ret = lbs_update_hw_spec(priv);
1029 if (ret)
1030 goto done;
1031
1032 /* Read power levels if available */
1033 ret = lbs_get_tx_power(priv, &curlevel, &minlevel, &maxlevel);
1034 if (ret == 0) {
1035 priv->txpower_cur = curlevel;
1036 priv->txpower_min = minlevel;
1037 priv->txpower_max = maxlevel;
1038 }
1039
1040 lbs_set_mac_control(priv);
1041 done:
1042 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
1043 return ret;
1044 }
1045
1046 /**
1047 * This function handles the timeout of command sending.
1048 * It will re-send the same command again.
1049 */
1050 static void command_timer_fn(unsigned long data)
1051 {
1052 struct lbs_private *priv = (struct lbs_private *)data;
1053 unsigned long flags;
1054
1055 lbs_deb_enter(LBS_DEB_CMD);
1056 spin_lock_irqsave(&priv->driver_lock, flags);
1057
1058 if (!priv->cur_cmd)
1059 goto out;
1060
1061 lbs_pr_info("command 0x%04x timed out\n",
1062 le16_to_cpu(priv->cur_cmd->cmdbuf->command));
1063
1064 priv->cmd_timed_out = 1;
1065 wake_up_interruptible(&priv->waitq);
1066 out:
1067 spin_unlock_irqrestore(&priv->driver_lock, flags);
1068 lbs_deb_leave(LBS_DEB_CMD);
1069 }
1070
1071 /**
1072 * This function put the device back to deep sleep mode when timer expires
1073 * and no activity (command, event, data etc.) is detected.
1074 */
1075 static void auto_deepsleep_timer_fn(unsigned long data)
1076 {
1077 struct lbs_private *priv = (struct lbs_private *)data;
1078 int ret;
1079
1080 lbs_deb_enter(LBS_DEB_CMD);
1081
1082 if (priv->is_activity_detected) {
1083 priv->is_activity_detected = 0;
1084 } else {
1085 if (priv->is_auto_deep_sleep_enabled &&
1086 (!priv->wakeup_dev_required) &&
1087 (priv->connect_status != LBS_CONNECTED)) {
1088 lbs_deb_main("Entering auto deep sleep mode...\n");
1089 ret = lbs_prepare_and_send_command(priv,
1090 CMD_802_11_DEEP_SLEEP, 0,
1091 0, 0, NULL);
1092 if (ret)
1093 lbs_pr_err("Enter Deep Sleep command failed\n");
1094 }
1095 }
1096 mod_timer(&priv->auto_deepsleep_timer , jiffies +
1097 (priv->auto_deep_sleep_timeout * HZ)/1000);
1098 lbs_deb_leave(LBS_DEB_CMD);
1099 }
1100
1101 int lbs_enter_auto_deep_sleep(struct lbs_private *priv)
1102 {
1103 lbs_deb_enter(LBS_DEB_SDIO);
1104
1105 priv->is_auto_deep_sleep_enabled = 1;
1106 if (priv->is_deep_sleep)
1107 priv->wakeup_dev_required = 1;
1108 mod_timer(&priv->auto_deepsleep_timer ,
1109 jiffies + (priv->auto_deep_sleep_timeout * HZ)/1000);
1110
1111 lbs_deb_leave(LBS_DEB_SDIO);
1112 return 0;
1113 }
1114
1115 int lbs_exit_auto_deep_sleep(struct lbs_private *priv)
1116 {
1117 lbs_deb_enter(LBS_DEB_SDIO);
1118
1119 priv->is_auto_deep_sleep_enabled = 0;
1120 priv->auto_deep_sleep_timeout = 0;
1121 del_timer(&priv->auto_deepsleep_timer);
1122
1123 lbs_deb_leave(LBS_DEB_SDIO);
1124 return 0;
1125 }
1126
1127 static void lbs_sync_channel_worker(struct work_struct *work)
1128 {
1129 struct lbs_private *priv = container_of(work, struct lbs_private,
1130 sync_channel);
1131
1132 lbs_deb_enter(LBS_DEB_MAIN);
1133 if (lbs_update_channel(priv))
1134 lbs_pr_info("Channel synchronization failed.");
1135 lbs_deb_leave(LBS_DEB_MAIN);
1136 }
1137
1138
1139 static int lbs_init_adapter(struct lbs_private *priv)
1140 {
1141 size_t bufsize;
1142 int i, ret = 0;
1143
1144 lbs_deb_enter(LBS_DEB_MAIN);
1145
1146 /* Allocate buffer to store the BSSID list */
1147 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
1148 priv->networks = kzalloc(bufsize, GFP_KERNEL);
1149 if (!priv->networks) {
1150 lbs_pr_err("Out of memory allocating beacons\n");
1151 ret = -1;
1152 goto out;
1153 }
1154
1155 /* Initialize scan result lists */
1156 INIT_LIST_HEAD(&priv->network_free_list);
1157 INIT_LIST_HEAD(&priv->network_list);
1158 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
1159 list_add_tail(&priv->networks[i].list,
1160 &priv->network_free_list);
1161 }
1162
1163 memset(priv->current_addr, 0xff, ETH_ALEN);
1164
1165 priv->connect_status = LBS_DISCONNECTED;
1166 priv->mesh_connect_status = LBS_DISCONNECTED;
1167 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1168 priv->mode = IW_MODE_INFRA;
1169 priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1170 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1171 priv->radio_on = 1;
1172 priv->enablehwauto = 1;
1173 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1174 priv->psmode = LBS802_11POWERMODECAM;
1175 priv->psstate = PS_STATE_FULL_POWER;
1176 priv->is_deep_sleep = 0;
1177 priv->is_auto_deep_sleep_enabled = 0;
1178 priv->wakeup_dev_required = 0;
1179 init_waitqueue_head(&priv->ds_awake_q);
1180
1181 mutex_init(&priv->lock);
1182
1183 setup_timer(&priv->command_timer, command_timer_fn,
1184 (unsigned long)priv);
1185 setup_timer(&priv->auto_deepsleep_timer, auto_deepsleep_timer_fn,
1186 (unsigned long)priv);
1187
1188 INIT_LIST_HEAD(&priv->cmdfreeq);
1189 INIT_LIST_HEAD(&priv->cmdpendingq);
1190
1191 spin_lock_init(&priv->driver_lock);
1192 init_waitqueue_head(&priv->cmd_pending);
1193
1194 /* Allocate the command buffers */
1195 if (lbs_allocate_cmd_buffer(priv)) {
1196 lbs_pr_err("Out of memory allocating command buffers\n");
1197 ret = -ENOMEM;
1198 goto out;
1199 }
1200 priv->resp_idx = 0;
1201 priv->resp_len[0] = priv->resp_len[1] = 0;
1202
1203 /* Create the event FIFO */
1204 priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
1205 if (IS_ERR(priv->event_fifo)) {
1206 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
1207 ret = -ENOMEM;
1208 goto out;
1209 }
1210
1211 out:
1212 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1213
1214 return ret;
1215 }
1216
1217 static void lbs_free_adapter(struct lbs_private *priv)
1218 {
1219 lbs_deb_enter(LBS_DEB_MAIN);
1220
1221 lbs_free_cmd_buffer(priv);
1222 if (priv->event_fifo)
1223 kfifo_free(priv->event_fifo);
1224 del_timer(&priv->command_timer);
1225 del_timer(&priv->auto_deepsleep_timer);
1226 kfree(priv->networks);
1227 priv->networks = NULL;
1228
1229 lbs_deb_leave(LBS_DEB_MAIN);
1230 }
1231
1232 static const struct net_device_ops lbs_netdev_ops = {
1233 .ndo_open = lbs_dev_open,
1234 .ndo_stop = lbs_eth_stop,
1235 .ndo_start_xmit = lbs_hard_start_xmit,
1236 .ndo_set_mac_address = lbs_set_mac_address,
1237 .ndo_tx_timeout = lbs_tx_timeout,
1238 .ndo_set_multicast_list = lbs_set_multicast_list,
1239 .ndo_change_mtu = eth_change_mtu,
1240 .ndo_validate_addr = eth_validate_addr,
1241 };
1242
1243 /**
1244 * @brief This function adds the card. it will probe the
1245 * card, allocate the lbs_priv and initialize the device.
1246 *
1247 * @param card A pointer to card
1248 * @return A pointer to struct lbs_private structure
1249 */
1250 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1251 {
1252 struct net_device *dev;
1253 struct wireless_dev *wdev;
1254 struct lbs_private *priv = NULL;
1255
1256 lbs_deb_enter(LBS_DEB_MAIN);
1257
1258 /* Allocate an Ethernet device and register it */
1259 wdev = lbs_cfg_alloc(dmdev);
1260 if (IS_ERR(wdev)) {
1261 lbs_pr_err("cfg80211 init failed\n");
1262 goto done;
1263 }
1264 /* TODO? */
1265 wdev->iftype = NL80211_IFTYPE_STATION;
1266 priv = wdev_priv(wdev);
1267 priv->wdev = wdev;
1268
1269 if (lbs_init_adapter(priv)) {
1270 lbs_pr_err("failed to initialize adapter structure.\n");
1271 goto err_wdev;
1272 }
1273
1274 //TODO? dev = alloc_netdev_mq(0, "wlan%d", ether_setup, IWM_TX_QUEUES);
1275 dev = alloc_netdev(0, "wlan%d", ether_setup);
1276 if (!dev) {
1277 dev_err(dmdev, "no memory for network device instance\n");
1278 goto err_adapter;
1279 }
1280
1281 dev->ieee80211_ptr = wdev;
1282 dev->ml_priv = priv;
1283 SET_NETDEV_DEV(dev, dmdev);
1284 wdev->netdev = dev;
1285 priv->dev = dev;
1286
1287 dev->netdev_ops = &lbs_netdev_ops;
1288 dev->watchdog_timeo = 5 * HZ;
1289 dev->ethtool_ops = &lbs_ethtool_ops;
1290 #ifdef WIRELESS_EXT
1291 dev->wireless_handlers = &lbs_handler_def;
1292 #endif
1293 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1294
1295
1296 // TODO: kzalloc + iwm_init_default_profile(iwm, iwm->umac_profile); ??
1297
1298
1299 priv->card = card;
1300 priv->mesh_open = 0;
1301 priv->infra_open = 0;
1302
1303
1304 priv->rtap_net_dev = NULL;
1305 strcpy(dev->name, "wlan%d");
1306
1307 lbs_deb_thread("Starting main thread...\n");
1308 init_waitqueue_head(&priv->waitq);
1309 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1310 if (IS_ERR(priv->main_thread)) {
1311 lbs_deb_thread("Error creating main thread.\n");
1312 goto err_ndev;
1313 }
1314
1315 priv->work_thread = create_singlethread_workqueue("lbs_worker");
1316 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1317 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
1318 INIT_WORK(&priv->mcast_work, lbs_set_mcast_worker);
1319 INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
1320
1321 sprintf(priv->mesh_ssid, "mesh");
1322 priv->mesh_ssid_len = 4;
1323
1324 priv->wol_criteria = 0xffffffff;
1325 priv->wol_gpio = 0xff;
1326
1327 goto done;
1328
1329 err_ndev:
1330 free_netdev(dev);
1331
1332 err_adapter:
1333 lbs_free_adapter(priv);
1334
1335 err_wdev:
1336 lbs_cfg_free(priv);
1337
1338 priv = NULL;
1339
1340 done:
1341 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
1342 return priv;
1343 }
1344 EXPORT_SYMBOL_GPL(lbs_add_card);
1345
1346
1347 void lbs_remove_card(struct lbs_private *priv)
1348 {
1349 struct net_device *dev = priv->dev;
1350 union iwreq_data wrqu;
1351
1352 lbs_deb_enter(LBS_DEB_MAIN);
1353
1354 lbs_remove_mesh(priv);
1355 lbs_remove_rtap(priv);
1356
1357 dev = priv->dev;
1358
1359 cancel_delayed_work_sync(&priv->scan_work);
1360 cancel_delayed_work_sync(&priv->assoc_work);
1361 cancel_work_sync(&priv->mcast_work);
1362
1363 /* worker thread destruction blocks on the in-flight command which
1364 * should have been cleared already in lbs_stop_card().
1365 */
1366 lbs_deb_main("destroying worker thread\n");
1367 destroy_workqueue(priv->work_thread);
1368 lbs_deb_main("done destroying worker thread\n");
1369
1370 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1371 priv->psmode = LBS802_11POWERMODECAM;
1372 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1373 }
1374
1375 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1376 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1377 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1378
1379 if (priv->is_deep_sleep) {
1380 priv->is_deep_sleep = 0;
1381 wake_up_interruptible(&priv->ds_awake_q);
1382 }
1383
1384 /* Stop the thread servicing the interrupts */
1385 priv->surpriseremoved = 1;
1386 kthread_stop(priv->main_thread);
1387
1388 lbs_free_adapter(priv);
1389 lbs_cfg_free(priv);
1390
1391 priv->dev = NULL;
1392 free_netdev(dev);
1393
1394 lbs_deb_leave(LBS_DEB_MAIN);
1395 }
1396 EXPORT_SYMBOL_GPL(lbs_remove_card);
1397
1398
1399 int lbs_start_card(struct lbs_private *priv)
1400 {
1401 struct net_device *dev = priv->dev;
1402 int ret = -1;
1403
1404 lbs_deb_enter(LBS_DEB_MAIN);
1405
1406 /* poke the firmware */
1407 ret = lbs_setup_firmware(priv);
1408 if (ret)
1409 goto done;
1410
1411 /* init 802.11d */
1412 lbs_init_11d(priv);
1413
1414 if (lbs_cfg_register(priv)) {
1415 lbs_pr_err("cannot register device\n");
1416 goto done;
1417 }
1418
1419 lbs_update_channel(priv);
1420
1421 /* Check mesh FW version and appropriately send the mesh start
1422 * command
1423 */
1424 if (priv->mesh_fw_ver == MESH_FW_OLD) {
1425 /* Enable mesh, if supported, and work out which TLV it uses.
1426 0x100 + 291 is an unofficial value used in 5.110.20.pXX
1427 0x100 + 37 is the official value used in 5.110.21.pXX
1428 but we check them in that order because 20.pXX doesn't
1429 give an error -- it just silently fails. */
1430
1431 /* 5.110.20.pXX firmware will fail the command if the channel
1432 doesn't match the existing channel. But only if the TLV
1433 is correct. If the channel is wrong, _BOTH_ versions will
1434 give an error to 0x100+291, and allow 0x100+37 to succeed.
1435 It's just that 5.110.20.pXX will not have done anything
1436 useful */
1437
1438 priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
1439 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1440 priv->curbssparams.channel)) {
1441 priv->mesh_tlv = TLV_TYPE_MESH_ID;
1442 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1443 priv->curbssparams.channel))
1444 priv->mesh_tlv = 0;
1445 }
1446 } else if (priv->mesh_fw_ver == MESH_FW_NEW) {
1447 /* 10.0.0.pXX new firmwares should succeed with TLV
1448 * 0x100+37; Do not invoke command with old TLV.
1449 */
1450 priv->mesh_tlv = TLV_TYPE_MESH_ID;
1451 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
1452 priv->curbssparams.channel))
1453 priv->mesh_tlv = 0;
1454 }
1455 if (priv->mesh_tlv) {
1456 lbs_add_mesh(priv);
1457
1458 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
1459 lbs_pr_err("cannot register lbs_mesh attribute\n");
1460
1461 /* While rtap isn't related to mesh, only mesh-enabled
1462 * firmware implements the rtap functionality via
1463 * CMD_802_11_MONITOR_MODE.
1464 */
1465 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1466 lbs_pr_err("cannot register lbs_rtap attribute\n");
1467 }
1468
1469 lbs_debugfs_init_one(priv, dev);
1470
1471 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1472
1473 ret = 0;
1474
1475 done:
1476 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1477 return ret;
1478 }
1479 EXPORT_SYMBOL_GPL(lbs_start_card);
1480
1481
1482 void lbs_stop_card(struct lbs_private *priv)
1483 {
1484 struct net_device *dev;
1485 struct cmd_ctrl_node *cmdnode;
1486 unsigned long flags;
1487
1488 lbs_deb_enter(LBS_DEB_MAIN);
1489
1490 if (!priv)
1491 goto out;
1492 dev = priv->dev;
1493
1494 netif_stop_queue(dev);
1495 netif_carrier_off(dev);
1496
1497 lbs_debugfs_remove_one(priv);
1498 if (priv->mesh_tlv) {
1499 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
1500 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
1501 }
1502
1503 /* Delete the timeout of the currently processing command */
1504 del_timer_sync(&priv->command_timer);
1505 del_timer_sync(&priv->auto_deepsleep_timer);
1506
1507 /* Flush pending command nodes */
1508 spin_lock_irqsave(&priv->driver_lock, flags);
1509 lbs_deb_main("clearing pending commands\n");
1510 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
1511 cmdnode->result = -ENOENT;
1512 cmdnode->cmdwaitqwoken = 1;
1513 wake_up_interruptible(&cmdnode->cmdwait_q);
1514 }
1515
1516 /* Flush the command the card is currently processing */
1517 if (priv->cur_cmd) {
1518 lbs_deb_main("clearing current command\n");
1519 priv->cur_cmd->result = -ENOENT;
1520 priv->cur_cmd->cmdwaitqwoken = 1;
1521 wake_up_interruptible(&priv->cur_cmd->cmdwait_q);
1522 }
1523 lbs_deb_main("done clearing commands\n");
1524 spin_unlock_irqrestore(&priv->driver_lock, flags);
1525
1526 unregister_netdev(dev);
1527
1528 out:
1529 lbs_deb_leave(LBS_DEB_MAIN);
1530 }
1531 EXPORT_SYMBOL_GPL(lbs_stop_card);
1532
1533
1534 static const struct net_device_ops mesh_netdev_ops = {
1535 .ndo_open = lbs_dev_open,
1536 .ndo_stop = lbs_mesh_stop,
1537 .ndo_start_xmit = lbs_hard_start_xmit,
1538 .ndo_set_mac_address = lbs_set_mac_address,
1539 .ndo_set_multicast_list = lbs_set_multicast_list,
1540 };
1541
1542 /**
1543 * @brief This function adds mshX interface
1544 *
1545 * @param priv A pointer to the struct lbs_private structure
1546 * @return 0 if successful, -X otherwise
1547 */
1548 static int lbs_add_mesh(struct lbs_private *priv)
1549 {
1550 struct net_device *mesh_dev = NULL;
1551 int ret = 0;
1552
1553 lbs_deb_enter(LBS_DEB_MESH);
1554
1555 /* Allocate a virtual mesh device */
1556 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1557 lbs_deb_mesh("init mshX device failed\n");
1558 ret = -ENOMEM;
1559 goto done;
1560 }
1561 mesh_dev->ml_priv = priv;
1562 priv->mesh_dev = mesh_dev;
1563
1564 mesh_dev->netdev_ops = &mesh_netdev_ops;
1565 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1566 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1567 sizeof(priv->dev->dev_addr));
1568
1569 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
1570
1571 #ifdef WIRELESS_EXT
1572 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1573 #endif
1574 mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1575 /* Register virtual mesh interface */
1576 ret = register_netdev(mesh_dev);
1577 if (ret) {
1578 lbs_pr_err("cannot register mshX virtual interface\n");
1579 goto err_free;
1580 }
1581
1582 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1583 if (ret)
1584 goto err_unregister;
1585
1586 lbs_persist_config_init(mesh_dev);
1587
1588 /* Everything successful */
1589 ret = 0;
1590 goto done;
1591
1592 err_unregister:
1593 unregister_netdev(mesh_dev);
1594
1595 err_free:
1596 free_netdev(mesh_dev);
1597
1598 done:
1599 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1600 return ret;
1601 }
1602
1603 static void lbs_remove_mesh(struct lbs_private *priv)
1604 {
1605 struct net_device *mesh_dev;
1606
1607
1608 mesh_dev = priv->mesh_dev;
1609 if (!mesh_dev)
1610 return;
1611
1612 lbs_deb_enter(LBS_DEB_MESH);
1613 netif_stop_queue(mesh_dev);
1614 netif_carrier_off(mesh_dev);
1615 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1616 lbs_persist_config_remove(mesh_dev);
1617 unregister_netdev(mesh_dev);
1618 priv->mesh_dev = NULL;
1619 free_netdev(mesh_dev);
1620 lbs_deb_leave(LBS_DEB_MESH);
1621 }
1622
1623 /**
1624 * @brief This function finds the CFP in
1625 * region_cfp_table based on region and band parameter.
1626 *
1627 * @param region The region code
1628 * @param band The band
1629 * @param cfp_no A pointer to CFP number
1630 * @return A pointer to CFP
1631 */
1632 struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
1633 {
1634 int i, end;
1635
1636 lbs_deb_enter(LBS_DEB_MAIN);
1637
1638 end = ARRAY_SIZE(region_cfp_table);
1639
1640 for (i = 0; i < end ; i++) {
1641 lbs_deb_main("region_cfp_table[i].region=%d\n",
1642 region_cfp_table[i].region);
1643 if (region_cfp_table[i].region == region) {
1644 *cfp_no = region_cfp_table[i].cfp_no_BG;
1645 lbs_deb_leave(LBS_DEB_MAIN);
1646 return region_cfp_table[i].cfp_BG;
1647 }
1648 }
1649
1650 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1651 return NULL;
1652 }
1653
1654 int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1655 {
1656 int ret = 0;
1657 int i = 0;
1658
1659 struct chan_freq_power *cfp;
1660 int cfp_no;
1661
1662 lbs_deb_enter(LBS_DEB_MAIN);
1663
1664 memset(priv->region_channel, 0, sizeof(priv->region_channel));
1665
1666 cfp = lbs_get_region_cfp_table(region, &cfp_no);
1667 if (cfp != NULL) {
1668 priv->region_channel[i].nrcfp = cfp_no;
1669 priv->region_channel[i].CFP = cfp;
1670 } else {
1671 lbs_deb_main("wrong region code %#x in band B/G\n",
1672 region);
1673 ret = -1;
1674 goto out;
1675 }
1676 priv->region_channel[i].valid = 1;
1677 priv->region_channel[i].region = region;
1678 priv->region_channel[i].band = band;
1679 i++;
1680 out:
1681 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1682 return ret;
1683 }
1684
1685 void lbs_queue_event(struct lbs_private *priv, u32 event)
1686 {
1687 unsigned long flags;
1688
1689 lbs_deb_enter(LBS_DEB_THREAD);
1690 spin_lock_irqsave(&priv->driver_lock, flags);
1691
1692 if (priv->psstate == PS_STATE_SLEEP)
1693 priv->psstate = PS_STATE_AWAKE;
1694
1695 __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1696
1697 wake_up_interruptible(&priv->waitq);
1698
1699 spin_unlock_irqrestore(&priv->driver_lock, flags);
1700 lbs_deb_leave(LBS_DEB_THREAD);
1701 }
1702 EXPORT_SYMBOL_GPL(lbs_queue_event);
1703
1704 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1705 {
1706 lbs_deb_enter(LBS_DEB_THREAD);
1707
1708 if (priv->psstate == PS_STATE_SLEEP)
1709 priv->psstate = PS_STATE_AWAKE;
1710
1711 /* Swap buffers by flipping the response index */
1712 BUG_ON(resp_idx > 1);
1713 priv->resp_idx = resp_idx;
1714
1715 wake_up_interruptible(&priv->waitq);
1716
1717 lbs_deb_leave(LBS_DEB_THREAD);
1718 }
1719 EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1720
1721 static int __init lbs_init_module(void)
1722 {
1723 lbs_deb_enter(LBS_DEB_MAIN);
1724 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1725 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1726 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1727 confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
1728 lbs_debugfs_init();
1729 lbs_deb_leave(LBS_DEB_MAIN);
1730 return 0;
1731 }
1732
1733 static void __exit lbs_exit_module(void)
1734 {
1735 lbs_deb_enter(LBS_DEB_MAIN);
1736 lbs_debugfs_remove();
1737 lbs_deb_leave(LBS_DEB_MAIN);
1738 }
1739
1740 /*
1741 * rtap interface support fuctions
1742 */
1743
1744 static int lbs_rtap_open(struct net_device *dev)
1745 {
1746 /* Yes, _stop_ the queue. Because we don't support injection */
1747 lbs_deb_enter(LBS_DEB_MAIN);
1748 netif_carrier_off(dev);
1749 netif_stop_queue(dev);
1750 lbs_deb_leave(LBS_DEB_LEAVE);
1751 return 0;
1752 }
1753
1754 static int lbs_rtap_stop(struct net_device *dev)
1755 {
1756 lbs_deb_enter(LBS_DEB_MAIN);
1757 lbs_deb_leave(LBS_DEB_MAIN);
1758 return 0;
1759 }
1760
1761 static netdev_tx_t lbs_rtap_hard_start_xmit(struct sk_buff *skb,
1762 struct net_device *dev)
1763 {
1764 netif_stop_queue(dev);
1765 return NETDEV_TX_BUSY;
1766 }
1767
1768 static void lbs_remove_rtap(struct lbs_private *priv)
1769 {
1770 lbs_deb_enter(LBS_DEB_MAIN);
1771 if (priv->rtap_net_dev == NULL)
1772 goto out;
1773 unregister_netdev(priv->rtap_net_dev);
1774 free_netdev(priv->rtap_net_dev);
1775 priv->rtap_net_dev = NULL;
1776 out:
1777 lbs_deb_leave(LBS_DEB_MAIN);
1778 }
1779
1780 static const struct net_device_ops rtap_netdev_ops = {
1781 .ndo_open = lbs_rtap_open,
1782 .ndo_stop = lbs_rtap_stop,
1783 .ndo_start_xmit = lbs_rtap_hard_start_xmit,
1784 };
1785
1786 static int lbs_add_rtap(struct lbs_private *priv)
1787 {
1788 int ret = 0;
1789 struct net_device *rtap_dev;
1790
1791 lbs_deb_enter(LBS_DEB_MAIN);
1792 if (priv->rtap_net_dev) {
1793 ret = -EPERM;
1794 goto out;
1795 }
1796
1797 rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
1798 if (rtap_dev == NULL) {
1799 ret = -ENOMEM;
1800 goto out;
1801 }
1802
1803 memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
1804 rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1805 rtap_dev->netdev_ops = &rtap_netdev_ops;
1806 rtap_dev->ml_priv = priv;
1807 SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
1808
1809 ret = register_netdev(rtap_dev);
1810 if (ret) {
1811 free_netdev(rtap_dev);
1812 goto out;
1813 }
1814 priv->rtap_net_dev = rtap_dev;
1815
1816 out:
1817 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1818 return ret;
1819 }
1820
1821 module_init(lbs_init_module);
1822 module_exit(lbs_exit_module);
1823
1824 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1825 MODULE_AUTHOR("Marvell International Ltd.");
1826 MODULE_LICENSE("GPL");