3291cedf1f873ed1985a9f8094cbf508d9fbaf49
[openwrt/svn-archive/archive.git] / package / libertas / src / 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 <asm/olpc.h>
15
16 #include <net/iw_handler.h>
17 #include <net/ieee80211.h>
18
19 #include "host.h"
20 #include "decl.h"
21 #include "dev.h"
22 #include "wext.h"
23 #include "debugfs.h"
24 #include "scan.h"
25 #include "assoc.h"
26 #include "cmd.h"
27 #include "ioctl.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)->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)->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 static int lbs_add_rtap(struct lbs_private *priv);
263 static void lbs_remove_rtap(struct lbs_private *priv);
264 static int lbs_add_mesh(struct lbs_private *priv);
265 static void lbs_remove_mesh(struct lbs_private *priv);
266
267
268 /**
269 * Get function for sysfs attribute rtap
270 */
271 static ssize_t lbs_rtap_get(struct device *dev,
272 struct device_attribute *attr, char * buf)
273 {
274 struct lbs_private *priv = to_net_dev(dev)->priv;
275 return snprintf(buf, 5, "0x%X\n", priv->monitormode);
276 }
277
278 /**
279 * Set function for sysfs attribute rtap
280 */
281 static ssize_t lbs_rtap_set(struct device *dev,
282 struct device_attribute *attr, const char * buf, size_t count)
283 {
284 int monitor_mode;
285 struct lbs_private *priv = to_net_dev(dev)->priv;
286
287 sscanf(buf, "%x", &monitor_mode);
288 if (monitor_mode) {
289 if (priv->monitormode == monitor_mode)
290 return strlen(buf);
291 if (!priv->monitormode) {
292 if (priv->infra_open || priv->mesh_open)
293 return -EBUSY;
294 if (priv->mode == IW_MODE_INFRA)
295 lbs_send_deauthentication(priv);
296 else if (priv->mode == IW_MODE_ADHOC)
297 lbs_stop_adhoc_network(priv);
298 lbs_add_rtap(priv);
299 }
300 priv->monitormode = monitor_mode;
301 }
302
303 else {
304 if (!priv->monitormode)
305 return strlen(buf);
306 priv->monitormode = 0;
307 lbs_remove_rtap(priv);
308
309 if (priv->currenttxskb) {
310 dev_kfree_skb_any(priv->currenttxskb);
311 priv->currenttxskb = NULL;
312 }
313
314 /* Wake queues, command thread, etc. */
315 lbs_host_to_card_done(priv);
316 }
317
318 lbs_prepare_and_send_command(priv,
319 CMD_802_11_MONITOR_MODE, CMD_ACT_SET,
320 CMD_OPTION_WAITFORRSP, 0, &priv->monitormode);
321 return strlen(buf);
322 }
323
324 /**
325 * lbs_rtap attribute to be exported per ethX interface
326 * through sysfs (/sys/class/net/ethX/lbs_rtap)
327 */
328 static DEVICE_ATTR(lbs_rtap, 0644, lbs_rtap_get, lbs_rtap_set );
329
330 /**
331 * Get function for sysfs attribute mesh
332 */
333 static ssize_t lbs_mesh_get(struct device *dev,
334 struct device_attribute *attr, char * buf)
335 {
336 struct lbs_private *priv = to_net_dev(dev)->priv;
337 return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
338 }
339
340 /**
341 * Set function for sysfs attribute mesh
342 */
343 static ssize_t lbs_mesh_set(struct device *dev,
344 struct device_attribute *attr, const char * buf, size_t count)
345 {
346 struct lbs_private *priv = to_net_dev(dev)->priv;
347 int enable;
348 int ret;
349
350 sscanf(buf, "%x", &enable);
351 enable = !!enable;
352 if (enable == !!priv->mesh_dev)
353 return count;
354
355 ret = lbs_mesh_config(priv, enable, priv->curbssparams.channel);
356 if (ret)
357 return ret;
358
359 if (enable)
360 lbs_add_mesh(priv);
361 else
362 lbs_remove_mesh(priv);
363
364 return count;
365 }
366
367 /**
368 * lbs_mesh attribute to be exported per ethX interface
369 * through sysfs (/sys/class/net/ethX/lbs_mesh)
370 */
371 static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
372
373 /**
374 * anycast_mask attribute to be exported per mshX interface
375 * through sysfs (/sys/class/net/mshX/anycast_mask)
376 */
377 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
378
379 static struct attribute *lbs_mesh_sysfs_entries[] = {
380 &dev_attr_anycast_mask.attr,
381 NULL,
382 };
383
384 static struct attribute_group lbs_mesh_attr_group = {
385 .attrs = lbs_mesh_sysfs_entries,
386 };
387
388 /**
389 * @brief This function opens the ethX or mshX interface
390 *
391 * @param dev A pointer to net_device structure
392 * @return 0 or -EBUSY if monitor mode active
393 */
394 static int lbs_dev_open(struct net_device *dev)
395 {
396 struct lbs_private *priv = (struct lbs_private *) dev->priv ;
397 int ret = 0;
398
399 lbs_deb_enter(LBS_DEB_NET);
400
401 spin_lock_irq(&priv->driver_lock);
402
403 if (priv->monitormode) {
404 ret = -EBUSY;
405 goto out;
406 }
407
408 if (dev == priv->mesh_dev) {
409 priv->mesh_open = 1;
410 priv->mesh_connect_status = LBS_CONNECTED;
411 netif_carrier_on(dev);
412 } else {
413 priv->infra_open = 1;
414
415 if (priv->connect_status == LBS_CONNECTED)
416 netif_carrier_on(dev);
417 else
418 netif_carrier_off(dev);
419 }
420
421 if (!priv->tx_pending_len)
422 netif_wake_queue(dev);
423 out:
424
425 spin_unlock_irq(&priv->driver_lock);
426 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
427 return ret;
428 }
429
430 /**
431 * @brief This function closes the mshX interface
432 *
433 * @param dev A pointer to net_device structure
434 * @return 0
435 */
436 static int lbs_mesh_stop(struct net_device *dev)
437 {
438 struct lbs_private *priv = (struct lbs_private *) (dev->priv);
439
440 lbs_deb_enter(LBS_DEB_MESH);
441 spin_lock_irq(&priv->driver_lock);
442
443 priv->mesh_open = 0;
444 priv->mesh_connect_status = LBS_DISCONNECTED;
445
446 netif_stop_queue(dev);
447 netif_carrier_off(dev);
448
449 spin_unlock_irq(&priv->driver_lock);
450
451 lbs_deb_leave(LBS_DEB_MESH);
452 return 0;
453 }
454
455 /**
456 * @brief This function closes the ethX interface
457 *
458 * @param dev A pointer to net_device structure
459 * @return 0
460 */
461 static int lbs_eth_stop(struct net_device *dev)
462 {
463 struct lbs_private *priv = (struct lbs_private *) dev->priv;
464
465 lbs_deb_enter(LBS_DEB_NET);
466
467 spin_lock_irq(&priv->driver_lock);
468 priv->infra_open = 0;
469 netif_stop_queue(dev);
470 spin_unlock_irq(&priv->driver_lock);
471
472 lbs_deb_leave(LBS_DEB_NET);
473 return 0;
474 }
475
476 static void lbs_tx_timeout(struct net_device *dev)
477 {
478 struct lbs_private *priv = (struct lbs_private *) dev->priv;
479
480 lbs_deb_enter(LBS_DEB_TX);
481
482 lbs_pr_err("tx watch dog timeout\n");
483
484 dev->trans_start = jiffies;
485
486 if (priv->currenttxskb)
487 lbs_send_tx_feedback(priv, 0);
488
489 /* XX: Shouldn't we also call into the hw-specific driver
490 to kick it somehow? */
491 lbs_host_to_card_done(priv);
492
493 /* More often than not, this actually happens because the
494 firmware has crapped itself -- rather than just a very
495 busy medium. So send a harmless command, and if/when
496 _that_ times out, we'll kick it in the head. */
497 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
498 0, 0, NULL);
499
500 lbs_deb_leave(LBS_DEB_TX);
501 }
502
503 void lbs_host_to_card_done(struct lbs_private *priv)
504 {
505 unsigned long flags;
506
507 lbs_deb_enter(LBS_DEB_THREAD);
508
509 spin_lock_irqsave(&priv->driver_lock, flags);
510
511 priv->dnld_sent = DNLD_RES_RECEIVED;
512
513 /* Wake main thread if commands are pending */
514 if (!priv->cur_cmd || priv->tx_pending_len > 0)
515 wake_up_interruptible(&priv->waitq);
516
517 spin_unlock_irqrestore(&priv->driver_lock, flags);
518 lbs_deb_leave(LBS_DEB_THREAD);
519 }
520 EXPORT_SYMBOL_GPL(lbs_host_to_card_done);
521
522 /**
523 * @brief This function returns the network statistics
524 *
525 * @param dev A pointer to struct lbs_private structure
526 * @return A pointer to net_device_stats structure
527 */
528 static struct net_device_stats *lbs_get_stats(struct net_device *dev)
529 {
530 struct lbs_private *priv = (struct lbs_private *) dev->priv;
531
532 lbs_deb_enter(LBS_DEB_NET);
533 return &priv->stats;
534 }
535
536 static int lbs_set_mac_address(struct net_device *dev, void *addr)
537 {
538 int ret = 0;
539 struct lbs_private *priv = (struct lbs_private *) dev->priv;
540 struct sockaddr *phwaddr = addr;
541 struct cmd_ds_802_11_mac_address cmd;
542
543 lbs_deb_enter(LBS_DEB_NET);
544
545 /* In case it was called from the mesh device */
546 dev = priv->dev;
547
548 cmd.hdr.size = cpu_to_le16(sizeof(cmd));
549 cmd.action = cpu_to_le16(CMD_ACT_SET);
550 memcpy(cmd.macadd, phwaddr->sa_data, ETH_ALEN);
551
552 ret = lbs_cmd_with_response(priv, CMD_802_11_MAC_ADDRESS, &cmd);
553 if (ret) {
554 lbs_deb_net("set MAC address failed\n");
555 goto done;
556 }
557
558 memcpy(priv->current_addr, phwaddr->sa_data, ETH_ALEN);
559 memcpy(dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
560 if (priv->mesh_dev)
561 memcpy(priv->mesh_dev->dev_addr, phwaddr->sa_data, ETH_ALEN);
562
563 done:
564 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
565 return ret;
566 }
567
568 static int lbs_copy_multicast_address(struct lbs_private *priv,
569 struct net_device *dev)
570 {
571 int i = 0;
572 struct dev_mc_list *mcptr = dev->mc_list;
573
574 for (i = 0; i < dev->mc_count; i++) {
575 memcpy(&priv->multicastlist[i], mcptr->dmi_addr, ETH_ALEN);
576 mcptr = mcptr->next;
577 }
578 return i;
579 }
580
581 static void lbs_set_multicast_list(struct net_device *dev)
582 {
583 struct lbs_private *priv = dev->priv;
584 int old_mac_control;
585 DECLARE_MAC_BUF(mac);
586
587 lbs_deb_enter(LBS_DEB_NET);
588
589 old_mac_control = priv->mac_control;
590
591 if (dev->flags & IFF_PROMISC) {
592 lbs_deb_net("enable promiscuous mode\n");
593 priv->mac_control |=
594 CMD_ACT_MAC_PROMISCUOUS_ENABLE;
595 priv->mac_control &=
596 ~(CMD_ACT_MAC_ALL_MULTICAST_ENABLE |
597 CMD_ACT_MAC_MULTICAST_ENABLE);
598 } else {
599 /* Multicast */
600 priv->mac_control &=
601 ~CMD_ACT_MAC_PROMISCUOUS_ENABLE;
602
603 if (dev->flags & IFF_ALLMULTI || dev->mc_count >
604 MRVDRV_MAX_MULTICAST_LIST_SIZE) {
605 lbs_deb_net( "enabling all multicast\n");
606 priv->mac_control |=
607 CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
608 priv->mac_control &=
609 ~CMD_ACT_MAC_MULTICAST_ENABLE;
610 } else {
611 priv->mac_control &=
612 ~CMD_ACT_MAC_ALL_MULTICAST_ENABLE;
613
614 if (!dev->mc_count) {
615 lbs_deb_net("no multicast addresses, "
616 "disabling multicast\n");
617 priv->mac_control &=
618 ~CMD_ACT_MAC_MULTICAST_ENABLE;
619 } else {
620 int i;
621
622 priv->mac_control |=
623 CMD_ACT_MAC_MULTICAST_ENABLE;
624
625 priv->nr_of_multicastmacaddr =
626 lbs_copy_multicast_address(priv, dev);
627
628 lbs_deb_net("multicast addresses: %d\n",
629 dev->mc_count);
630
631 for (i = 0; i < dev->mc_count; i++) {
632 lbs_deb_net("Multicast address %d: %s\n",
633 i, print_mac(mac,
634 priv->multicastlist[i]));
635 }
636 /* send multicast addresses to firmware */
637 lbs_prepare_and_send_command(priv,
638 CMD_MAC_MULTICAST_ADR,
639 CMD_ACT_SET, 0, 0,
640 NULL);
641 }
642 }
643 }
644
645 if (priv->mac_control != old_mac_control)
646 lbs_set_mac_control(priv);
647
648 lbs_deb_leave(LBS_DEB_NET);
649 }
650
651 /**
652 * @brief This function handles the major jobs in the LBS driver.
653 * It handles all events generated by firmware, RX data received
654 * from firmware and TX data sent from kernel.
655 *
656 * @param data A pointer to lbs_thread structure
657 * @return 0
658 */
659 static int lbs_thread(void *data)
660 {
661 struct net_device *dev = data;
662 struct lbs_private *priv = dev->priv;
663 wait_queue_t wait;
664
665 lbs_deb_enter(LBS_DEB_THREAD);
666
667 init_waitqueue_entry(&wait, current);
668
669 for (;;) {
670 int shouldsleep;
671 u8 resp_idx;
672
673 lbs_deb_thread("1: currenttxskb %p, dnld_sent %d\n",
674 priv->currenttxskb, priv->dnld_sent);
675
676 add_wait_queue(&priv->waitq, &wait);
677 set_current_state(TASK_INTERRUPTIBLE);
678 spin_lock_irq(&priv->driver_lock);
679
680 if (kthread_should_stop())
681 shouldsleep = 0; /* Bye */
682 else if (priv->surpriseremoved)
683 shouldsleep = 1; /* We need to wait until we're _told_ to die */
684 else if (priv->psstate == PS_STATE_SLEEP)
685 shouldsleep = 1; /* Sleep mode. Nothing we can do till it wakes */
686 else if (priv->cmd_timed_out)
687 shouldsleep = 0; /* Command timed out. Recover */
688 else if (!priv->fw_ready)
689 shouldsleep = 1; /* Firmware not ready. We're waiting for it */
690 else if (priv->dnld_sent)
691 shouldsleep = 1; /* Something is en route to the device already */
692 else if (priv->tx_pending_len > 0)
693 shouldsleep = 0; /* We've a packet to send */
694 else if (priv->cur_cmd)
695 shouldsleep = 1; /* Can't send a command; one already running */
696 else if (!list_empty(&priv->cmdpendingq))
697 shouldsleep = 0; /* We have a command to send */
698 else if (__kfifo_len(priv->event_fifo))
699 shouldsleep = 0; /* We have an event to process */
700 else if (priv->resp_len[priv->resp_idx])
701 shouldsleep = 0; /* We have a command response */
702 else
703 shouldsleep = 1; /* No command */
704
705 if (shouldsleep) {
706 lbs_deb_thread("sleeping, connect_status %d, "
707 "ps_mode %d, ps_state %d\n",
708 priv->connect_status,
709 priv->psmode, priv->psstate);
710 spin_unlock_irq(&priv->driver_lock);
711 schedule();
712 } else
713 spin_unlock_irq(&priv->driver_lock);
714
715 lbs_deb_thread("2: currenttxskb %p, dnld_send %d\n",
716 priv->currenttxskb, priv->dnld_sent);
717
718 set_current_state(TASK_RUNNING);
719 remove_wait_queue(&priv->waitq, &wait);
720
721 lbs_deb_thread("3: currenttxskb %p, dnld_sent %d\n",
722 priv->currenttxskb, priv->dnld_sent);
723
724 if (kthread_should_stop()) {
725 lbs_deb_thread("break from main thread\n");
726 break;
727 }
728
729 if (priv->surpriseremoved) {
730 lbs_deb_thread("adapter removed; waiting to die...\n");
731 continue;
732 }
733
734 lbs_deb_thread("4: currenttxskb %p, dnld_sent %d\n",
735 priv->currenttxskb, priv->dnld_sent);
736
737 /* Process any pending command response */
738 spin_lock_irq(&priv->driver_lock);
739 resp_idx = priv->resp_idx;
740 if (priv->resp_len[resp_idx]) {
741 spin_unlock_irq(&priv->driver_lock);
742 lbs_process_command_response(priv,
743 priv->resp_buf[resp_idx],
744 priv->resp_len[resp_idx]);
745 spin_lock_irq(&priv->driver_lock);
746 priv->resp_len[resp_idx] = 0;
747 }
748 spin_unlock_irq(&priv->driver_lock);
749
750 /* command timeout stuff */
751 if (priv->cmd_timed_out && priv->cur_cmd) {
752 struct cmd_ctrl_node *cmdnode = priv->cur_cmd;
753
754 if (++priv->nr_retries > 10) {
755 lbs_pr_info("Excessive timeouts submitting command %x\n",
756 le16_to_cpu(cmdnode->cmdbuf->command));
757 lbs_complete_command(priv, cmdnode, -ETIMEDOUT);
758 priv->nr_retries = 0;
759 #ifdef CONFIG_OLPC
760 if (machine_is_olpc()) {
761 spin_unlock_irq(&priv->driver_lock);
762 printk(KERN_CRIT "Resetting OLPC wireless via EC...\n");
763 olpc_ec_cmd(0x25, NULL, 0, NULL, 0);
764 spin_lock_irq(&priv->driver_lock);
765 }
766 #endif
767 } else {
768 priv->cur_cmd = NULL;
769 priv->dnld_sent = DNLD_RES_RECEIVED;
770 lbs_pr_info("requeueing command %x due to timeout (#%d)\n",
771 le16_to_cpu(cmdnode->cmdbuf->command), priv->nr_retries);
772
773 /* Stick it back at the _top_ of the pending queue
774 for immediate resubmission */
775 list_add(&cmdnode->list, &priv->cmdpendingq);
776 }
777 }
778 priv->cmd_timed_out = 0;
779
780 /* Process hardware events, e.g. card removed, link lost */
781 spin_lock_irq(&priv->driver_lock);
782 while (__kfifo_len(priv->event_fifo)) {
783 u32 event;
784
785 __kfifo_get(priv->event_fifo, (unsigned char *) &event,
786 sizeof(event));
787 spin_unlock_irq(&priv->driver_lock);
788 lbs_process_event(priv, event);
789 spin_lock_irq(&priv->driver_lock);
790 }
791 spin_unlock_irq(&priv->driver_lock);
792
793 if (!priv->fw_ready)
794 continue;
795
796 /* Check if we need to confirm Sleep Request received previously */
797 if (priv->psstate == PS_STATE_PRE_SLEEP &&
798 !priv->dnld_sent && !priv->cur_cmd) {
799 if (priv->connect_status == LBS_CONNECTED) {
800 lbs_deb_thread("pre-sleep, currenttxskb %p, "
801 "dnld_sent %d, cur_cmd %p\n",
802 priv->currenttxskb, priv->dnld_sent,
803 priv->cur_cmd);
804
805 lbs_ps_confirm_sleep(priv);
806 } else {
807 /* workaround for firmware sending
808 * deauth/linkloss event immediately
809 * after sleep request; remove this
810 * after firmware fixes it
811 */
812 priv->psstate = PS_STATE_AWAKE;
813 lbs_pr_alert("ignore PS_SleepConfirm in "
814 "non-connected state\n");
815 }
816 }
817
818 /* The PS state is changed during processing of Sleep Request
819 * event above
820 */
821 if ((priv->psstate == PS_STATE_SLEEP) ||
822 (priv->psstate == PS_STATE_PRE_SLEEP))
823 continue;
824
825 /* Execute the next command */
826 if (!priv->dnld_sent && !priv->cur_cmd)
827 lbs_execute_next_command(priv);
828
829 /* Wake-up command waiters which can't sleep in
830 * lbs_prepare_and_send_command
831 */
832 if (!list_empty(&priv->cmdpendingq))
833 wake_up_all(&priv->cmd_pending);
834
835 spin_lock_irq(&priv->driver_lock);
836 if (!priv->dnld_sent && priv->tx_pending_len > 0) {
837 int ret = priv->hw_host_to_card(priv, MVMS_DAT,
838 priv->tx_pending_buf,
839 priv->tx_pending_len);
840 if (ret) {
841 lbs_deb_tx("host_to_card failed %d\n", ret);
842 priv->dnld_sent = DNLD_RES_RECEIVED;
843 }
844 priv->tx_pending_len = 0;
845 if (!priv->currenttxskb) {
846 /* We can wake the queues immediately if we aren't
847 waiting for TX feedback */
848 if (priv->connect_status == LBS_CONNECTED)
849 netif_wake_queue(priv->dev);
850 if (priv->mesh_dev &&
851 priv->mesh_connect_status == LBS_CONNECTED)
852 netif_wake_queue(priv->mesh_dev);
853 }
854 }
855 spin_unlock_irq(&priv->driver_lock);
856 }
857
858 del_timer(&priv->command_timer);
859 wake_up_all(&priv->cmd_pending);
860
861 lbs_deb_leave(LBS_DEB_THREAD);
862 return 0;
863 }
864
865 static int lbs_suspend_callback(struct lbs_private *priv, unsigned long dummy,
866 struct cmd_header *cmd)
867 {
868 lbs_deb_enter(LBS_DEB_FW);
869
870 netif_device_detach(priv->dev);
871 if (priv->mesh_dev)
872 netif_device_detach(priv->mesh_dev);
873
874 priv->fw_ready = 0;
875 lbs_deb_leave(LBS_DEB_FW);
876 return 0;
877 }
878
879 int lbs_suspend(struct lbs_private *priv)
880 {
881 struct cmd_header cmd;
882 int ret;
883
884 lbs_deb_enter(LBS_DEB_FW);
885
886 if (priv->wol_criteria == 0xffffffff) {
887 lbs_pr_info("Suspend attempt without configuring wake params!\n");
888 return -EINVAL;
889 }
890
891 memset(&cmd, 0, sizeof(cmd));
892
893 ret = __lbs_cmd(priv, CMD_802_11_HOST_SLEEP_ACTIVATE, &cmd,
894 sizeof(cmd), lbs_suspend_callback, 0);
895 if (ret)
896 lbs_pr_info("HOST_SLEEP_ACTIVATE failed: %d\n", ret);
897
898 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
899 return ret;
900 }
901 EXPORT_SYMBOL_GPL(lbs_suspend);
902
903 int lbs_resume(struct lbs_private *priv)
904 {
905 lbs_deb_enter(LBS_DEB_FW);
906
907 priv->fw_ready = 1;
908
909 /* Firmware doesn't seem to give us RX packets any more
910 until we send it some command. Might as well update */
911 lbs_prepare_and_send_command(priv, CMD_802_11_RSSI, 0,
912 0, 0, NULL);
913
914 netif_device_attach(priv->dev);
915 if (priv->mesh_dev)
916 netif_device_attach(priv->mesh_dev);
917
918 lbs_deb_leave(LBS_DEB_FW);
919 return 0;
920 }
921 EXPORT_SYMBOL_GPL(lbs_resume);
922
923 /**
924 * @brief This function downloads firmware image, gets
925 * HW spec from firmware and set basic parameters to
926 * firmware.
927 *
928 * @param priv A pointer to struct lbs_private structure
929 * @return 0 or -1
930 */
931 static int lbs_setup_firmware(struct lbs_private *priv)
932 {
933 int ret = -1;
934
935 lbs_deb_enter(LBS_DEB_FW);
936
937 /*
938 * Read MAC address from HW
939 */
940 memset(priv->current_addr, 0xff, ETH_ALEN);
941 ret = lbs_update_hw_spec(priv);
942 if (ret) {
943 ret = -1;
944 goto done;
945 }
946
947 lbs_set_mac_control(priv);
948
949 ret = lbs_get_data_rate(priv);
950 if (ret < 0) {
951 ret = -1;
952 goto done;
953 }
954
955 ret = 0;
956 done:
957 lbs_deb_leave_args(LBS_DEB_FW, "ret %d", ret);
958 return ret;
959 }
960
961 /**
962 * This function handles the timeout of command sending.
963 * It will re-send the same command again.
964 */
965 static void command_timer_fn(unsigned long data)
966 {
967 struct lbs_private *priv = (struct lbs_private *)data;
968 unsigned long flags;
969
970 lbs_deb_enter(LBS_DEB_CMD);
971 spin_lock_irqsave(&priv->driver_lock, flags);
972
973 if (!priv->cur_cmd) {
974 lbs_pr_info("Command timer expired; no pending command\n");
975 goto out;
976 }
977
978 lbs_pr_info("Command %x timed out\n", le16_to_cpu(priv->cur_cmd->cmdbuf->command));
979
980 priv->cmd_timed_out = 1;
981 wake_up_interruptible(&priv->waitq);
982 out:
983 spin_unlock_irqrestore(&priv->driver_lock, flags);
984 lbs_deb_leave(LBS_DEB_CMD);
985 }
986
987 static void lbs_sync_channel_worker(struct work_struct *work)
988 {
989 struct lbs_private *priv = container_of(work, struct lbs_private,
990 sync_channel);
991
992 lbs_deb_enter(LBS_DEB_MAIN);
993 if (lbs_update_channel(priv))
994 lbs_pr_info("Channel synchronization failed.");
995 lbs_deb_leave(LBS_DEB_MAIN);
996 }
997
998
999 static int lbs_init_adapter(struct lbs_private *priv)
1000 {
1001 size_t bufsize;
1002 int i, ret = 0;
1003
1004 lbs_deb_enter(LBS_DEB_MAIN);
1005
1006 /* Allocate buffer to store the BSSID list */
1007 bufsize = MAX_NETWORK_COUNT * sizeof(struct bss_descriptor);
1008 priv->networks = kzalloc(bufsize, GFP_KERNEL);
1009 if (!priv->networks) {
1010 lbs_pr_err("Out of memory allocating beacons\n");
1011 ret = -1;
1012 goto out;
1013 }
1014
1015 /* Initialize scan result lists */
1016 INIT_LIST_HEAD(&priv->network_free_list);
1017 INIT_LIST_HEAD(&priv->network_list);
1018 for (i = 0; i < MAX_NETWORK_COUNT; i++) {
1019 list_add_tail(&priv->networks[i].list,
1020 &priv->network_free_list);
1021 }
1022
1023 memset(priv->current_addr, 0xff, ETH_ALEN);
1024
1025 priv->connect_status = LBS_DISCONNECTED;
1026 priv->mesh_connect_status = LBS_DISCONNECTED;
1027 priv->secinfo.auth_mode = IW_AUTH_ALG_OPEN_SYSTEM;
1028 priv->mode = IW_MODE_INFRA;
1029 priv->curbssparams.channel = DEFAULT_AD_HOC_CHANNEL;
1030 priv->mac_control = CMD_ACT_MAC_RX_ON | CMD_ACT_MAC_TX_ON;
1031 priv->radioon = RADIO_ON;
1032 priv->auto_rate = 1;
1033 priv->capability = WLAN_CAPABILITY_SHORT_PREAMBLE;
1034 priv->psmode = LBS802_11POWERMODECAM;
1035 priv->psstate = PS_STATE_FULL_POWER;
1036
1037 mutex_init(&priv->lock);
1038
1039 setup_timer(&priv->command_timer, command_timer_fn,
1040 (unsigned long)priv);
1041
1042 INIT_LIST_HEAD(&priv->cmdfreeq);
1043 INIT_LIST_HEAD(&priv->cmdpendingq);
1044
1045 spin_lock_init(&priv->driver_lock);
1046 init_waitqueue_head(&priv->cmd_pending);
1047
1048 /* Allocate the command buffers */
1049 if (lbs_allocate_cmd_buffer(priv)) {
1050 lbs_pr_err("Out of memory allocating command buffers\n");
1051 ret = -ENOMEM;
1052 goto out;
1053 }
1054 priv->resp_idx = 0;
1055 priv->resp_len[0] = priv->resp_len[1] = 0;
1056
1057 /* Create the event FIFO */
1058 priv->event_fifo = kfifo_alloc(sizeof(u32) * 16, GFP_KERNEL, NULL);
1059 if (IS_ERR(priv->event_fifo)) {
1060 lbs_pr_err("Out of memory allocating event FIFO buffer\n");
1061 ret = -ENOMEM;
1062 goto out;
1063 }
1064
1065 out:
1066 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1067
1068 return ret;
1069 }
1070
1071 static void lbs_free_adapter(struct lbs_private *priv)
1072 {
1073 lbs_deb_enter(LBS_DEB_MAIN);
1074
1075 lbs_free_cmd_buffer(priv);
1076 if (priv->event_fifo)
1077 kfifo_free(priv->event_fifo);
1078 del_timer(&priv->command_timer);
1079 kfree(priv->networks);
1080 priv->networks = NULL;
1081
1082 lbs_deb_leave(LBS_DEB_MAIN);
1083 }
1084
1085 /**
1086 * @brief This function adds the card. it will probe the
1087 * card, allocate the lbs_priv and initialize the device.
1088 *
1089 * @param card A pointer to card
1090 * @return A pointer to struct lbs_private structure
1091 */
1092 struct lbs_private *lbs_add_card(void *card, struct device *dmdev)
1093 {
1094 struct net_device *dev = NULL;
1095 struct lbs_private *priv = NULL;
1096
1097 lbs_deb_enter(LBS_DEB_MAIN);
1098
1099 /* Allocate an Ethernet device and register it */
1100 dev = alloc_etherdev(sizeof(struct lbs_private));
1101 if (!dev) {
1102 lbs_pr_err("init ethX device failed\n");
1103 goto done;
1104 }
1105 priv = dev->priv;
1106
1107 if (lbs_init_adapter(priv)) {
1108 lbs_pr_err("failed to initialize adapter structure.\n");
1109 goto err_init_adapter;
1110 }
1111
1112 priv->dev = dev;
1113 priv->card = card;
1114 priv->mesh_open = 0;
1115 priv->infra_open = 0;
1116
1117 /* Setup the OS Interface to our functions */
1118 dev->open = lbs_dev_open;
1119 dev->hard_start_xmit = lbs_hard_start_xmit;
1120 dev->stop = lbs_eth_stop;
1121 dev->set_mac_address = lbs_set_mac_address;
1122 dev->tx_timeout = lbs_tx_timeout;
1123 dev->do_ioctl = lbs_do_ioctl;
1124 dev->get_stats = lbs_get_stats;
1125 dev->watchdog_timeo = 5 * HZ;
1126 dev->ethtool_ops = &lbs_ethtool_ops;
1127 #ifdef WIRELESS_EXT
1128 dev->wireless_handlers = (struct iw_handler_def *)&lbs_handler_def;
1129 #endif
1130 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1131 dev->set_multicast_list = lbs_set_multicast_list;
1132
1133 SET_NETDEV_DEV(dev, dmdev);
1134
1135 priv->rtap_net_dev = NULL;
1136
1137 lbs_deb_thread("Starting main thread...\n");
1138 init_waitqueue_head(&priv->waitq);
1139 priv->main_thread = kthread_run(lbs_thread, dev, "lbs_main");
1140 if (IS_ERR(priv->main_thread)) {
1141 lbs_deb_thread("Error creating main thread.\n");
1142 goto err_init_adapter;
1143 }
1144
1145 priv->work_thread = create_singlethread_workqueue("lbs_worker");
1146 INIT_DELAYED_WORK(&priv->assoc_work, lbs_association_worker);
1147 INIT_DELAYED_WORK(&priv->scan_work, lbs_scan_worker);
1148 INIT_WORK(&priv->sync_channel, lbs_sync_channel_worker);
1149
1150 sprintf(priv->mesh_ssid, "mesh");
1151 priv->mesh_ssid_len = 4;
1152
1153 priv->wol_criteria = 0xffffffff;
1154 priv->wol_gpio = 0xff;
1155
1156 goto done;
1157
1158 err_init_adapter:
1159 lbs_free_adapter(priv);
1160 free_netdev(dev);
1161 priv = NULL;
1162
1163 done:
1164 lbs_deb_leave_args(LBS_DEB_MAIN, "priv %p", priv);
1165 return priv;
1166 }
1167 EXPORT_SYMBOL_GPL(lbs_add_card);
1168
1169
1170 int lbs_remove_card(struct lbs_private *priv)
1171 {
1172 struct net_device *dev = priv->dev;
1173 union iwreq_data wrqu;
1174
1175 lbs_deb_enter(LBS_DEB_MAIN);
1176
1177 lbs_remove_mesh(priv);
1178 lbs_remove_rtap(priv);
1179
1180 dev = priv->dev;
1181
1182 cancel_delayed_work(&priv->scan_work);
1183 cancel_delayed_work(&priv->assoc_work);
1184 destroy_workqueue(priv->work_thread);
1185
1186 if (priv->psmode == LBS802_11POWERMODEMAX_PSP) {
1187 priv->psmode = LBS802_11POWERMODECAM;
1188 lbs_ps_wakeup(priv, CMD_OPTION_WAITFORRSP);
1189 }
1190
1191 memset(wrqu.ap_addr.sa_data, 0xaa, ETH_ALEN);
1192 wrqu.ap_addr.sa_family = ARPHRD_ETHER;
1193 wireless_send_event(priv->dev, SIOCGIWAP, &wrqu, NULL);
1194
1195 /* Stop the thread servicing the interrupts */
1196 priv->surpriseremoved = 1;
1197 kthread_stop(priv->main_thread);
1198
1199 lbs_free_adapter(priv);
1200
1201 priv->dev = NULL;
1202 free_netdev(dev);
1203
1204 lbs_deb_leave(LBS_DEB_MAIN);
1205 return 0;
1206 }
1207 EXPORT_SYMBOL_GPL(lbs_remove_card);
1208
1209
1210 int lbs_start_card(struct lbs_private *priv)
1211 {
1212 struct net_device *dev = priv->dev;
1213 int ret = -1;
1214
1215 lbs_deb_enter(LBS_DEB_MAIN);
1216
1217 /* poke the firmware */
1218 ret = lbs_setup_firmware(priv);
1219 if (ret)
1220 goto done;
1221
1222 /* init 802.11d */
1223 lbs_init_11d(priv);
1224
1225 if (register_netdev(dev)) {
1226 lbs_pr_err("cannot register ethX device\n");
1227 goto done;
1228 }
1229 if (device_create_file(&dev->dev, &dev_attr_lbs_rtap))
1230 lbs_pr_err("cannot register lbs_rtap attribute\n");
1231
1232 lbs_update_channel(priv);
1233
1234 /* 5.0.16p0 is known to NOT support any mesh */
1235 if (priv->fwrelease > 0x05001000) {
1236 /* Enable mesh, if supported, and work out which TLV it uses.
1237 0x100 + 291 is an unofficial value used in 5.110.20.pXX
1238 0x100 + 37 is the official value used in 5.110.21.pXX
1239 but we check them in that order because 20.pXX doesn't
1240 give an error -- it just silently fails. */
1241
1242 /* 5.110.20.pXX firmware will fail the command if the channel
1243 doesn't match the existing channel. But only if the TLV
1244 is correct. If the channel is wrong, _BOTH_ versions will
1245 give an error to 0x100+291, and allow 0x100+37 to succeed.
1246 It's just that 5.110.20.pXX will not have done anything
1247 useful */
1248
1249 priv->mesh_tlv = 0x100 + 291;
1250 if (lbs_mesh_config(priv, 1, priv->curbssparams.channel)) {
1251 priv->mesh_tlv = 0x100 + 37;
1252 if (lbs_mesh_config(priv, 1, priv->curbssparams.channel))
1253 priv->mesh_tlv = 0;
1254 }
1255 if (priv->mesh_tlv) {
1256 lbs_add_mesh(priv);
1257
1258 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
1259 lbs_pr_err("cannot register lbs_mesh attribute\n");
1260 }
1261 }
1262
1263 lbs_debugfs_init_one(priv, dev);
1264
1265 lbs_pr_info("%s: Marvell WLAN 802.11 adapter\n", dev->name);
1266
1267 ret = 0;
1268
1269 done:
1270 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1271 return ret;
1272 }
1273 EXPORT_SYMBOL_GPL(lbs_start_card);
1274
1275
1276 int lbs_stop_card(struct lbs_private *priv)
1277 {
1278 struct net_device *dev = priv->dev;
1279 int ret = -1;
1280 struct cmd_ctrl_node *cmdnode;
1281 unsigned long flags;
1282
1283 lbs_deb_enter(LBS_DEB_MAIN);
1284
1285 netif_stop_queue(priv->dev);
1286 netif_carrier_off(priv->dev);
1287
1288 lbs_debugfs_remove_one(priv);
1289 device_remove_file(&dev->dev, &dev_attr_lbs_rtap);
1290 if (priv->mesh_tlv)
1291 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
1292
1293 /* Flush pending command nodes */
1294 spin_lock_irqsave(&priv->driver_lock, flags);
1295 list_for_each_entry(cmdnode, &priv->cmdpendingq, list) {
1296 cmdnode->result = -ENOENT;
1297 cmdnode->cmdwaitqwoken = 1;
1298 wake_up_interruptible(&cmdnode->cmdwait_q);
1299 }
1300 spin_unlock_irqrestore(&priv->driver_lock, flags);
1301
1302 unregister_netdev(dev);
1303
1304 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1305 return ret;
1306 }
1307 EXPORT_SYMBOL_GPL(lbs_stop_card);
1308
1309
1310 /**
1311 * @brief This function adds mshX interface
1312 *
1313 * @param priv A pointer to the struct lbs_private structure
1314 * @return 0 if successful, -X otherwise
1315 */
1316 static int lbs_add_mesh(struct lbs_private *priv)
1317 {
1318 struct net_device *mesh_dev = NULL;
1319 int ret = 0;
1320
1321 lbs_deb_enter(LBS_DEB_MESH);
1322
1323 /* Allocate a virtual mesh device */
1324 if (!(mesh_dev = alloc_netdev(0, "msh%d", ether_setup))) {
1325 lbs_deb_mesh("init mshX device failed\n");
1326 ret = -ENOMEM;
1327 goto done;
1328 }
1329 mesh_dev->priv = priv;
1330 priv->mesh_dev = mesh_dev;
1331
1332 mesh_dev->open = lbs_dev_open;
1333 mesh_dev->hard_start_xmit = lbs_hard_start_xmit;
1334 mesh_dev->stop = lbs_mesh_stop;
1335 mesh_dev->do_ioctl = lbs_do_ioctl;
1336 mesh_dev->get_stats = lbs_get_stats;
1337 mesh_dev->set_mac_address = lbs_set_mac_address;
1338 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1339 memcpy(mesh_dev->dev_addr, priv->dev->dev_addr,
1340 sizeof(priv->dev->dev_addr));
1341
1342 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
1343
1344 #ifdef WIRELESS_EXT
1345 mesh_dev->wireless_handlers = (struct iw_handler_def *)&mesh_handler_def;
1346 #endif
1347 /* Register virtual mesh interface */
1348 ret = register_netdev(mesh_dev);
1349 if (ret) {
1350 lbs_pr_err("cannot register mshX virtual interface\n");
1351 goto err_free;
1352 }
1353
1354 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1355 if (ret)
1356 goto err_unregister;
1357
1358 /* Everything successful */
1359 ret = 0;
1360 goto done;
1361
1362 err_unregister:
1363 unregister_netdev(mesh_dev);
1364
1365 err_free:
1366 free_netdev(mesh_dev);
1367
1368 done:
1369 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1370 return ret;
1371 }
1372
1373 static void lbs_remove_mesh(struct lbs_private *priv)
1374 {
1375 struct net_device *mesh_dev;
1376
1377
1378 mesh_dev = priv->mesh_dev;
1379 if (!mesh_dev)
1380 return;
1381
1382 lbs_deb_enter(LBS_DEB_MESH);
1383 netif_stop_queue(mesh_dev);
1384 netif_carrier_off(priv->mesh_dev);
1385 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1386 unregister_netdev(mesh_dev);
1387 priv->mesh_dev = NULL;
1388 free_netdev(mesh_dev);
1389 lbs_deb_leave(LBS_DEB_MESH);
1390 }
1391
1392 /**
1393 * @brief This function finds the CFP in
1394 * region_cfp_table based on region and band parameter.
1395 *
1396 * @param region The region code
1397 * @param band The band
1398 * @param cfp_no A pointer to CFP number
1399 * @return A pointer to CFP
1400 */
1401 struct chan_freq_power *lbs_get_region_cfp_table(u8 region, int *cfp_no)
1402 {
1403 int i, end;
1404
1405 lbs_deb_enter(LBS_DEB_MAIN);
1406
1407 end = ARRAY_SIZE(region_cfp_table);
1408
1409 for (i = 0; i < end ; i++) {
1410 lbs_deb_main("region_cfp_table[i].region=%d\n",
1411 region_cfp_table[i].region);
1412 if (region_cfp_table[i].region == region) {
1413 *cfp_no = region_cfp_table[i].cfp_no_BG;
1414 lbs_deb_leave(LBS_DEB_MAIN);
1415 return region_cfp_table[i].cfp_BG;
1416 }
1417 }
1418
1419 lbs_deb_leave_args(LBS_DEB_MAIN, "ret NULL");
1420 return NULL;
1421 }
1422
1423 int lbs_set_regiontable(struct lbs_private *priv, u8 region, u8 band)
1424 {
1425 int ret = 0;
1426 int i = 0;
1427
1428 struct chan_freq_power *cfp;
1429 int cfp_no;
1430
1431 lbs_deb_enter(LBS_DEB_MAIN);
1432
1433 memset(priv->region_channel, 0, sizeof(priv->region_channel));
1434
1435 cfp = lbs_get_region_cfp_table(region, &cfp_no);
1436 if (cfp != NULL) {
1437 priv->region_channel[i].nrcfp = cfp_no;
1438 priv->region_channel[i].CFP = cfp;
1439 } else {
1440 lbs_deb_main("wrong region code %#x in band B/G\n",
1441 region);
1442 ret = -1;
1443 goto out;
1444 }
1445 priv->region_channel[i].valid = 1;
1446 priv->region_channel[i].region = region;
1447 priv->region_channel[i].band = band;
1448 i++;
1449 out:
1450 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1451 return ret;
1452 }
1453
1454 void lbs_queue_event(struct lbs_private *priv, u32 event)
1455 {
1456 unsigned long flags;
1457
1458 lbs_deb_enter(LBS_DEB_THREAD);
1459 spin_lock_irqsave(&priv->driver_lock, flags);
1460
1461 if (priv->psstate == PS_STATE_SLEEP)
1462 priv->psstate = PS_STATE_AWAKE;
1463
1464 __kfifo_put(priv->event_fifo, (unsigned char *) &event, sizeof(u32));
1465
1466 wake_up_interruptible(&priv->waitq);
1467
1468 spin_unlock_irqrestore(&priv->driver_lock, flags);
1469 lbs_deb_leave(LBS_DEB_THREAD);
1470 }
1471 EXPORT_SYMBOL_GPL(lbs_queue_event);
1472
1473 void lbs_notify_command_response(struct lbs_private *priv, u8 resp_idx)
1474 {
1475 lbs_deb_enter(LBS_DEB_THREAD);
1476
1477 if (priv->psstate == PS_STATE_SLEEP)
1478 priv->psstate = PS_STATE_AWAKE;
1479
1480 /* Swap buffers by flipping the response index */
1481 BUG_ON(resp_idx > 1);
1482 priv->resp_idx = resp_idx;
1483
1484 wake_up_interruptible(&priv->waitq);
1485
1486 lbs_deb_leave(LBS_DEB_THREAD);
1487 }
1488 EXPORT_SYMBOL_GPL(lbs_notify_command_response);
1489
1490 static int __init lbs_init_module(void)
1491 {
1492 lbs_deb_enter(LBS_DEB_MAIN);
1493 memset(&confirm_sleep, 0, sizeof(confirm_sleep));
1494 confirm_sleep.hdr.command = cpu_to_le16(CMD_802_11_PS_MODE);
1495 confirm_sleep.hdr.size = cpu_to_le16(sizeof(confirm_sleep));
1496 confirm_sleep.action = cpu_to_le16(CMD_SUBCMD_SLEEP_CONFIRMED);
1497 lbs_debugfs_init();
1498 lbs_deb_leave(LBS_DEB_MAIN);
1499 return 0;
1500 }
1501
1502 static void __exit lbs_exit_module(void)
1503 {
1504 lbs_deb_enter(LBS_DEB_MAIN);
1505 lbs_debugfs_remove();
1506 lbs_deb_leave(LBS_DEB_MAIN);
1507 }
1508
1509 /*
1510 * rtap interface support fuctions
1511 */
1512
1513 static int lbs_rtap_open(struct net_device *dev)
1514 {
1515 /* Yes, _stop_ the queue. Because we don't support injection */
1516 lbs_deb_enter(LBS_DEB_MAIN);
1517 netif_carrier_off(dev);
1518 netif_stop_queue(dev);
1519 lbs_deb_leave(LBS_DEB_LEAVE);
1520 return 0;
1521 }
1522
1523 static int lbs_rtap_stop(struct net_device *dev)
1524 {
1525 lbs_deb_enter(LBS_DEB_MAIN);
1526 lbs_deb_leave(LBS_DEB_MAIN);
1527 return 0;
1528 }
1529
1530 static int lbs_rtap_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1531 {
1532 netif_stop_queue(dev);
1533 return NETDEV_TX_BUSY;
1534 }
1535
1536 static struct net_device_stats *lbs_rtap_get_stats(struct net_device *dev)
1537 {
1538 struct lbs_private *priv = dev->priv;
1539 lbs_deb_enter(LBS_DEB_NET);
1540 return &priv->stats;
1541 }
1542
1543
1544 static void lbs_remove_rtap(struct lbs_private *priv)
1545 {
1546 lbs_deb_enter(LBS_DEB_MAIN);
1547 if (priv->rtap_net_dev == NULL)
1548 return;
1549 unregister_netdev(priv->rtap_net_dev);
1550 free_netdev(priv->rtap_net_dev);
1551 priv->rtap_net_dev = NULL;
1552 lbs_deb_leave(LBS_DEB_MAIN);
1553 }
1554
1555 static int lbs_add_rtap(struct lbs_private *priv)
1556 {
1557 int ret = 0;
1558 struct net_device *rtap_dev;
1559
1560 lbs_deb_enter(LBS_DEB_MAIN);
1561 if (priv->rtap_net_dev) {
1562 ret = -EPERM;
1563 goto out;
1564 }
1565
1566 rtap_dev = alloc_netdev(0, "rtap%d", ether_setup);
1567 if (rtap_dev == NULL) {
1568 ret = -ENOMEM;
1569 goto out;
1570 }
1571
1572 memcpy(rtap_dev->dev_addr, priv->current_addr, ETH_ALEN);
1573 rtap_dev->type = ARPHRD_IEEE80211_RADIOTAP;
1574 rtap_dev->open = lbs_rtap_open;
1575 rtap_dev->stop = lbs_rtap_stop;
1576 rtap_dev->get_stats = lbs_rtap_get_stats;
1577 rtap_dev->hard_start_xmit = lbs_rtap_hard_start_xmit;
1578 rtap_dev->set_multicast_list = lbs_set_multicast_list;
1579 rtap_dev->priv = priv;
1580 SET_NETDEV_DEV(rtap_dev, priv->dev->dev.parent);
1581
1582 ret = register_netdev(rtap_dev);
1583 if (ret) {
1584 free_netdev(rtap_dev);
1585 goto out;
1586 }
1587 priv->rtap_net_dev = rtap_dev;
1588
1589 out:
1590 lbs_deb_leave_args(LBS_DEB_MAIN, "ret %d", ret);
1591 return ret;
1592 }
1593
1594 #ifndef CONFIG_IEEE80211
1595 const char *escape_essid(const char *essid, u8 essid_len)
1596 {
1597 static char escaped[IW_ESSID_MAX_SIZE * 2 + 1];
1598 const char *s = essid;
1599 char *d = escaped;
1600
1601 if (ieee80211_is_empty_essid(essid, essid_len)) {
1602 memcpy(escaped, "<hidden>", sizeof("<hidden>"));
1603 return escaped;
1604 }
1605
1606 essid_len = min(essid_len, (u8) IW_ESSID_MAX_SIZE);
1607 while (essid_len--) {
1608 if (*s == '\0') {
1609 *d++ = '\\';
1610 *d++ = '0';
1611 s++;
1612 } else {
1613 *d++ = *s++;
1614 }
1615 }
1616 *d = '\0';
1617 return escaped;
1618 }
1619 #endif
1620
1621 module_init(lbs_init_module);
1622 module_exit(lbs_exit_module);
1623
1624 MODULE_DESCRIPTION("Libertas WLAN Driver Library");
1625 MODULE_AUTHOR("Marvell International Ltd.");
1626 MODULE_LICENSE("GPL");