1
2
3
4
5
6
7
8
9
10#include <linux/atomic.h>
11#include <linux/firmware.h>
12#include <linux/jiffies.h>
13#include <linux/mmc/card.h>
14#include <linux/mmc/sdio_func.h>
15#include <linux/module.h>
16#include <linux/workqueue.h>
17#include "ks_wlan.h"
18#include "ks_hostif.h"
19
20#define ROM_FILE "ks7010sd.rom"
21
22
23#define SDIO_VENDOR_ID_KS_CODE_A 0x005b
24#define SDIO_VENDOR_ID_KS_CODE_B 0x0023
25
26
27#define SDIO_DEVICE_ID_KS_7010 0x7910
28
29
30#define READ_STATUS_REG 0x000000
31#define WRITE_STATUS_REG 0x00000C
32enum reg_status_type {
33 REG_STATUS_BUSY,
34 REG_STATUS_IDLE
35};
36
37
38#define READ_INDEX_REG 0x000004
39
40
41#define READ_DATA_SIZE_REG 0x000008
42
43
44#define WRITE_INDEX_REG 0x000010
45
46
47
48
49
50#define WSTATUS_RSIZE_REG 0x000014
51
52
53#define WSTATUS_MASK 0x80
54
55
56#define RSIZE_MASK 0x7F
57
58
59#define INT_ENABLE_REG 0x000020
60
61#define INT_PENDING_REG 0x000024
62
63#define INT_GCR_B BIT(7)
64#define INT_GCR_A BIT(6)
65#define INT_WRITE_STATUS BIT(5)
66#define INT_WRITE_INDEX BIT(4)
67#define INT_WRITE_SIZE BIT(3)
68#define INT_READ_STATUS BIT(2)
69#define INT_READ_INDEX BIT(1)
70#define INT_READ_SIZE BIT(0)
71
72
73#define GCR_A_REG 0x000028
74enum gen_com_reg_a {
75 GCR_A_INIT,
76 GCR_A_REMAP,
77 GCR_A_RUN
78};
79
80
81#define GCR_B_REG 0x00002C
82enum gen_com_reg_b {
83 GCR_B_ACTIVE,
84 GCR_B_DOZE
85};
86
87
88#define WAKEUP_REG 0x008018
89#define WAKEUP_REQ 0x5a
90
91
92#define DATA_WINDOW 0x010000
93#define WINDOW_SIZE (64 * 1024)
94
95#define KS7010_IRAM_ADDRESS 0x06000000
96
97#define KS7010_IO_BLOCK_SIZE 512
98
99
100
101
102
103
104
105
106
107struct ks_sdio_card {
108 struct sdio_func *func;
109 struct ks_wlan_private *priv;
110};
111
112static struct sdio_func *ks7010_to_func(struct ks_wlan_private *priv)
113{
114 struct ks_sdio_card *ks_sdio = priv->if_hw;
115
116 return ks_sdio->func;
117}
118
119
120static int ks7010_sdio_readb(struct ks_wlan_private *priv,
121 u32 address, u8 *byte)
122{
123 struct sdio_func *func = ks7010_to_func(priv);
124 int ret;
125
126 *byte = sdio_readb(func, address, &ret);
127
128 return ret;
129}
130
131
132static int ks7010_sdio_read(struct ks_wlan_private *priv, u32 address,
133 u8 *buffer, unsigned int length)
134{
135 struct sdio_func *func = ks7010_to_func(priv);
136
137 return sdio_memcpy_fromio(func, buffer, address, length);
138}
139
140
141static int ks7010_sdio_writeb(struct ks_wlan_private *priv,
142 u32 address, u8 byte)
143{
144 struct sdio_func *func = ks7010_to_func(priv);
145 int ret;
146
147 sdio_writeb(func, byte, address, &ret);
148
149 return ret;
150}
151
152
153static int ks7010_sdio_write(struct ks_wlan_private *priv, u32 address,
154 u8 *buffer, unsigned int length)
155{
156 struct sdio_func *func = ks7010_to_func(priv);
157
158 return sdio_memcpy_toio(func, address, buffer, length);
159}
160
161static void ks_wlan_hw_sleep_doze_request(struct ks_wlan_private *priv)
162{
163 int ret;
164
165
166 atomic_set(&priv->sleepstatus.doze_request, 0);
167
168 if (atomic_read(&priv->sleepstatus.status) == 0) {
169 ret = ks7010_sdio_writeb(priv, GCR_B_REG, GCR_B_DOZE);
170 if (ret) {
171 netdev_err(priv->net_dev, "write GCR_B_REG\n");
172 goto set_sleep_mode;
173 }
174 atomic_set(&priv->sleepstatus.status, 1);
175 priv->last_doze = jiffies;
176 }
177
178set_sleep_mode:
179 priv->sleep_mode = atomic_read(&priv->sleepstatus.status);
180}
181
182static void ks_wlan_hw_sleep_wakeup_request(struct ks_wlan_private *priv)
183{
184 int ret;
185
186
187 atomic_set(&priv->sleepstatus.wakeup_request, 0);
188
189 if (atomic_read(&priv->sleepstatus.status) == 1) {
190 ret = ks7010_sdio_writeb(priv, WAKEUP_REG, WAKEUP_REQ);
191 if (ret) {
192 netdev_err(priv->net_dev, "write WAKEUP_REG\n");
193 goto set_sleep_mode;
194 }
195 atomic_set(&priv->sleepstatus.status, 0);
196 priv->last_wakeup = jiffies;
197 ++priv->wakeup_count;
198 }
199
200set_sleep_mode:
201 priv->sleep_mode = atomic_read(&priv->sleepstatus.status);
202}
203
204void ks_wlan_hw_wakeup_request(struct ks_wlan_private *priv)
205{
206 int ret;
207
208 if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
209 ret = ks7010_sdio_writeb(priv, WAKEUP_REG, WAKEUP_REQ);
210 if (ret)
211 netdev_err(priv->net_dev, "write WAKEUP_REG\n");
212
213 priv->last_wakeup = jiffies;
214 ++priv->wakeup_count;
215 }
216}
217
218static void _ks_wlan_hw_power_save(struct ks_wlan_private *priv)
219{
220 u8 byte;
221 int ret;
222
223 if (priv->reg.power_mgmt == POWER_MGMT_ACTIVE)
224 return;
225
226 if (priv->reg.operation_mode != MODE_INFRASTRUCTURE)
227 return;
228
229 if (!is_connect_status(priv->connect_status))
230 return;
231
232 if (priv->dev_state != DEVICE_STATE_SLEEP)
233 return;
234
235 if (atomic_read(&priv->psstatus.status) == PS_SNOOZE)
236 return;
237
238 netdev_dbg(priv->net_dev,
239 "STATUS:\n"
240 "- psstatus.status = %d\n"
241 "- psstatus.confirm_wait = %d\n"
242 "- psstatus.snooze_guard = %d\n"
243 "- txq_count = %d\n",
244 atomic_read(&priv->psstatus.status),
245 atomic_read(&priv->psstatus.confirm_wait),
246 atomic_read(&priv->psstatus.snooze_guard),
247 txq_count(priv));
248
249 if (atomic_read(&priv->psstatus.confirm_wait) ||
250 atomic_read(&priv->psstatus.snooze_guard) ||
251 txq_has_space(priv)) {
252 queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
253 return;
254 }
255
256 ret = ks7010_sdio_readb(priv, INT_PENDING_REG, &byte);
257 if (ret) {
258 netdev_err(priv->net_dev, "read INT_PENDING_REG\n");
259 goto queue_delayed_work;
260 }
261 if (byte)
262 goto queue_delayed_work;
263
264 ret = ks7010_sdio_writeb(priv, GCR_B_REG, GCR_B_DOZE);
265 if (ret) {
266 netdev_err(priv->net_dev, "write GCR_B_REG\n");
267 goto queue_delayed_work;
268 }
269 atomic_set(&priv->psstatus.status, PS_SNOOZE);
270
271 return;
272
273queue_delayed_work:
274 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
275}
276
277int ks_wlan_hw_power_save(struct ks_wlan_private *priv)
278{
279 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
280 return 0;
281}
282
283static int enqueue_txdev(struct ks_wlan_private *priv, unsigned char *p,
284 unsigned long size,
285 void (*complete_handler)(struct ks_wlan_private *priv,
286 struct sk_buff *skb),
287 struct sk_buff *skb)
288{
289 struct tx_device_buffer *sp;
290 int ret;
291
292 if (priv->dev_state < DEVICE_STATE_BOOT) {
293 ret = -EPERM;
294 goto err_complete;
295 }
296
297 if ((TX_DEVICE_BUFF_SIZE - 1) <= txq_count(priv)) {
298 netdev_err(priv->net_dev, "tx buffer overflow\n");
299 ret = -EOVERFLOW;
300 goto err_complete;
301 }
302
303 sp = &priv->tx_dev.tx_dev_buff[priv->tx_dev.qtail];
304 sp->sendp = p;
305 sp->size = size;
306 sp->complete_handler = complete_handler;
307 sp->skb = skb;
308 inc_txqtail(priv);
309
310 return 0;
311
312err_complete:
313 kfree(p);
314 if (complete_handler)
315 (*complete_handler)(priv, skb);
316
317 return ret;
318}
319
320
321static int write_to_device(struct ks_wlan_private *priv, u8 *buffer,
322 unsigned long size)
323{
324 struct hostif_hdr *hdr;
325 int ret;
326
327 hdr = (struct hostif_hdr *)buffer;
328
329 if (le16_to_cpu(hdr->event) < HIF_DATA_REQ ||
330 le16_to_cpu(hdr->event) > HIF_REQ_MAX) {
331 netdev_err(priv->net_dev, "unknown event=%04X\n", hdr->event);
332 return 0;
333 }
334
335 ret = ks7010_sdio_write(priv, DATA_WINDOW, buffer, size);
336 if (ret) {
337 netdev_err(priv->net_dev, "write DATA_WINDOW\n");
338 return ret;
339 }
340
341 ret = ks7010_sdio_writeb(priv, WRITE_STATUS_REG, REG_STATUS_BUSY);
342 if (ret) {
343 netdev_err(priv->net_dev, "write WRITE_STATUS_REG\n");
344 return ret;
345 }
346
347 return 0;
348}
349
350static void tx_device_task(struct ks_wlan_private *priv)
351{
352 struct tx_device_buffer *sp;
353 int ret;
354
355 if (!txq_has_space(priv) ||
356 atomic_read(&priv->psstatus.status) == PS_SNOOZE)
357 return;
358
359 sp = &priv->tx_dev.tx_dev_buff[priv->tx_dev.qhead];
360 if (priv->dev_state >= DEVICE_STATE_BOOT) {
361 ret = write_to_device(priv, sp->sendp, sp->size);
362 if (ret) {
363 netdev_err(priv->net_dev,
364 "write_to_device error !!(%d)\n", ret);
365 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
366 return;
367 }
368 }
369 kfree(sp->sendp);
370 if (sp->complete_handler)
371 (*sp->complete_handler)(priv, sp->skb);
372 inc_txqhead(priv);
373
374 if (txq_has_space(priv))
375 queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
376}
377
378int ks_wlan_hw_tx(struct ks_wlan_private *priv, void *p, unsigned long size,
379 void (*complete_handler)(struct ks_wlan_private *priv,
380 struct sk_buff *skb),
381 struct sk_buff *skb)
382{
383 int result = 0;
384 struct hostif_hdr *hdr;
385
386 hdr = (struct hostif_hdr *)p;
387
388 if (le16_to_cpu(hdr->event) < HIF_DATA_REQ ||
389 le16_to_cpu(hdr->event) > HIF_REQ_MAX) {
390 netdev_err(priv->net_dev, "unknown event=%04X\n", hdr->event);
391 return 0;
392 }
393
394
395 priv->hostt.buff[priv->hostt.qtail] = le16_to_cpu(hdr->event);
396 priv->hostt.qtail = (priv->hostt.qtail + 1) % SME_EVENT_BUFF_SIZE;
397
398 spin_lock(&priv->tx_dev.tx_dev_lock);
399 result = enqueue_txdev(priv, p, size, complete_handler, skb);
400 spin_unlock(&priv->tx_dev.tx_dev_lock);
401
402 if (txq_has_space(priv))
403 queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
404
405 return result;
406}
407
408static void rx_event_task(unsigned long dev)
409{
410 struct ks_wlan_private *priv = (struct ks_wlan_private *)dev;
411 struct rx_device_buffer *rp;
412
413 if (rxq_has_space(priv) && priv->dev_state >= DEVICE_STATE_BOOT) {
414 rp = &priv->rx_dev.rx_dev_buff[priv->rx_dev.qhead];
415 hostif_receive(priv, rp->data, rp->size);
416 inc_rxqhead(priv);
417
418 if (rxq_has_space(priv))
419 tasklet_schedule(&priv->rx_bh_task);
420 }
421}
422
423static void ks_wlan_hw_rx(struct ks_wlan_private *priv, size_t size)
424{
425 int ret;
426 struct rx_device_buffer *rx_buffer;
427 struct hostif_hdr *hdr;
428 u16 event = 0;
429
430
431 if (rxq_count(priv) >= (RX_DEVICE_BUFF_SIZE - 1)) {
432 netdev_err(priv->net_dev, "rx buffer overflow\n");
433 return;
434 }
435 rx_buffer = &priv->rx_dev.rx_dev_buff[priv->rx_dev.qtail];
436
437 ret = ks7010_sdio_read(priv, DATA_WINDOW, &rx_buffer->data[0],
438 hif_align_size(size));
439 if (ret)
440 return;
441
442
443 if (size > 2046 || size == 0) {
444#ifdef DEBUG
445 print_hex_dump_bytes("INVALID DATA dump: ",
446 DUMP_PREFIX_OFFSET,
447 rx_buffer->data, 32);
448#endif
449 ret = ks7010_sdio_writeb(priv, READ_STATUS_REG, REG_STATUS_IDLE);
450 if (ret)
451 netdev_err(priv->net_dev, "write READ_STATUS_REG\n");
452
453
454 return;
455 }
456
457 hdr = (struct hostif_hdr *)&rx_buffer->data[0];
458 rx_buffer->size = le16_to_cpu(hdr->size) + sizeof(hdr->size);
459 event = le16_to_cpu(hdr->event);
460 inc_rxqtail(priv);
461
462 ret = ks7010_sdio_writeb(priv, READ_STATUS_REG, REG_STATUS_IDLE);
463 if (ret)
464 netdev_err(priv->net_dev, "write READ_STATUS_REG\n");
465
466 if (atomic_read(&priv->psstatus.confirm_wait) && is_hif_conf(event)) {
467 netdev_dbg(priv->net_dev, "IS_HIF_CONF true !!\n");
468 atomic_dec(&priv->psstatus.confirm_wait);
469 }
470
471 tasklet_schedule(&priv->rx_bh_task);
472}
473
474static void ks7010_rw_function(struct work_struct *work)
475{
476 struct ks_wlan_private *priv = container_of(work,
477 struct ks_wlan_private,
478 rw_dwork.work);
479 struct sdio_func *func = ks7010_to_func(priv);
480 u8 byte;
481 int ret;
482
483
484 if (time_after(priv->last_doze + msecs_to_jiffies(30), jiffies)) {
485 netdev_dbg(priv->net_dev, "wait after DOZE\n");
486 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
487 return;
488 }
489
490
491 while (time_after(priv->last_wakeup + msecs_to_jiffies(30), jiffies)) {
492 netdev_dbg(priv->net_dev, "wait after WAKEUP\n");
493 dev_info(&func->dev, "wake: %lu %lu\n",
494 priv->last_wakeup + msecs_to_jiffies(30), jiffies);
495 msleep(30);
496 }
497
498 sdio_claim_host(func);
499
500
501 if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
502 if (txq_has_space(priv)) {
503 ks_wlan_hw_wakeup_request(priv);
504 queue_delayed_work(priv->wq, &priv->rw_dwork, 1);
505 }
506 goto release_host;
507 }
508
509
510 if (atomic_read(&priv->sleepstatus.doze_request) == 1) {
511 ks_wlan_hw_sleep_doze_request(priv);
512 goto release_host;
513 }
514
515 if (atomic_read(&priv->sleepstatus.wakeup_request) == 1) {
516 ks_wlan_hw_sleep_wakeup_request(priv);
517 goto release_host;
518 }
519
520
521 ret = ks7010_sdio_readb(priv, WSTATUS_RSIZE_REG, &byte);
522 if (ret) {
523 netdev_err(priv->net_dev, "read WSTATUS_RSIZE_REG psstatus=%d\n",
524 atomic_read(&priv->psstatus.status));
525 goto release_host;
526 }
527
528 if (byte & RSIZE_MASK) {
529 ks_wlan_hw_rx(priv, (size_t)((byte & RSIZE_MASK) << 4));
530 }
531 if ((byte & WSTATUS_MASK))
532 tx_device_task(priv);
533
534 _ks_wlan_hw_power_save(priv);
535
536release_host:
537 sdio_release_host(func);
538}
539
540static void ks_sdio_interrupt(struct sdio_func *func)
541{
542 int ret;
543 struct ks_sdio_card *card;
544 struct ks_wlan_private *priv;
545 u8 status, rsize, byte;
546
547 card = sdio_get_drvdata(func);
548 priv = card->priv;
549
550 if (priv->dev_state < DEVICE_STATE_BOOT)
551 goto queue_delayed_work;
552
553 ret = ks7010_sdio_readb(priv, INT_PENDING_REG, &status);
554 if (ret) {
555 netdev_err(priv->net_dev, "read INT_PENDING_REG\n");
556 goto queue_delayed_work;
557 }
558
559
560
561
562
563
564 if (status & INT_GCR_B ||
565 atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
566 ret = ks7010_sdio_readb(priv, GCR_B_REG, &byte);
567 if (ret) {
568 netdev_err(priv->net_dev, "read GCR_B_REG\n");
569 goto queue_delayed_work;
570 }
571 if (byte == GCR_B_ACTIVE) {
572 if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
573 atomic_set(&priv->psstatus.status, PS_WAKEUP);
574 priv->wakeup_count = 0;
575 }
576 complete(&priv->psstatus.wakeup_wait);
577 }
578 }
579
580 do {
581
582 ret = ks7010_sdio_readb(priv, WSTATUS_RSIZE_REG, &byte);
583 if (ret) {
584 netdev_err(priv->net_dev, "read WSTATUS_RSIZE_REG\n");
585 goto queue_delayed_work;
586 }
587 rsize = byte & RSIZE_MASK;
588 if (rsize != 0)
589 ks_wlan_hw_rx(priv, (size_t)(rsize << 4));
590
591 if (byte & WSTATUS_MASK) {
592 if (atomic_read(&priv->psstatus.status) == PS_SNOOZE) {
593 if (txq_has_space(priv)) {
594 ks_wlan_hw_wakeup_request(priv);
595 queue_delayed_work(priv->wq,
596 &priv->rw_dwork, 1);
597 return;
598 }
599 } else {
600 tx_device_task(priv);
601 }
602 }
603 } while (rsize);
604
605queue_delayed_work:
606 queue_delayed_work(priv->wq, &priv->rw_dwork, 0);
607}
608
609static int trx_device_init(struct ks_wlan_private *priv)
610{
611 priv->tx_dev.qhead = 0;
612 priv->tx_dev.qtail = 0;
613
614 priv->rx_dev.qhead = 0;
615 priv->rx_dev.qtail = 0;
616
617 spin_lock_init(&priv->tx_dev.tx_dev_lock);
618 spin_lock_init(&priv->rx_dev.rx_dev_lock);
619
620 tasklet_init(&priv->rx_bh_task, rx_event_task, (unsigned long)priv);
621
622 return 0;
623}
624
625static void trx_device_exit(struct ks_wlan_private *priv)
626{
627 struct tx_device_buffer *sp;
628
629
630 while (txq_has_space(priv)) {
631 sp = &priv->tx_dev.tx_dev_buff[priv->tx_dev.qhead];
632 kfree(sp->sendp);
633 if (sp->complete_handler)
634 (*sp->complete_handler)(priv, sp->skb);
635 inc_txqhead(priv);
636 }
637
638 tasklet_kill(&priv->rx_bh_task);
639}
640
641static int ks7010_sdio_update_index(struct ks_wlan_private *priv, u32 index)
642{
643 int ret;
644 unsigned char *data_buf;
645
646 data_buf = kmemdup(&index, sizeof(u32), GFP_KERNEL);
647 if (!data_buf)
648 return -ENOMEM;
649
650 ret = ks7010_sdio_write(priv, WRITE_INDEX_REG, data_buf, sizeof(index));
651 if (ret)
652 goto err_free_data_buf;
653
654 ret = ks7010_sdio_write(priv, READ_INDEX_REG, data_buf, sizeof(index));
655 if (ret)
656 goto err_free_data_buf;
657
658 return 0;
659
660err_free_data_buf:
661 kfree(data_buf);
662
663 return ret;
664}
665
666#define ROM_BUFF_SIZE (64 * 1024)
667static int ks7010_sdio_data_compare(struct ks_wlan_private *priv, u32 address,
668 u8 *data, unsigned int size)
669{
670 int ret;
671 u8 *read_buf;
672
673 read_buf = kmalloc(ROM_BUFF_SIZE, GFP_KERNEL);
674 if (!read_buf)
675 return -ENOMEM;
676
677 ret = ks7010_sdio_read(priv, address, read_buf, size);
678 if (ret)
679 goto err_free_read_buf;
680
681 if (memcmp(data, read_buf, size) != 0) {
682 ret = -EIO;
683 netdev_err(priv->net_dev, "data compare error (%d)\n", ret);
684 goto err_free_read_buf;
685 }
686
687 return 0;
688
689err_free_read_buf:
690 kfree(read_buf);
691
692 return ret;
693}
694
695static int ks7010_copy_firmware(struct ks_wlan_private *priv,
696 const struct firmware *fw_entry)
697{
698 unsigned int length;
699 unsigned int size;
700 unsigned int offset;
701 unsigned int n = 0;
702 u8 *rom_buf;
703 int ret;
704
705 rom_buf = kmalloc(ROM_BUFF_SIZE, GFP_KERNEL);
706 if (!rom_buf)
707 return -ENOMEM;
708
709 length = fw_entry->size;
710
711 do {
712 if (length >= ROM_BUFF_SIZE) {
713 size = ROM_BUFF_SIZE;
714 length = length - ROM_BUFF_SIZE;
715 } else {
716 size = length;
717 length = 0;
718 }
719 if (size == 0)
720 break;
721
722 memcpy(rom_buf, fw_entry->data + n, size);
723
724 offset = n;
725 ret = ks7010_sdio_update_index(priv,
726 KS7010_IRAM_ADDRESS + offset);
727 if (ret)
728 goto free_rom_buf;
729
730 ret = ks7010_sdio_write(priv, DATA_WINDOW, rom_buf, size);
731 if (ret)
732 goto free_rom_buf;
733
734 ret = ks7010_sdio_data_compare(priv,
735 DATA_WINDOW, rom_buf, size);
736 if (ret)
737 goto free_rom_buf;
738
739 n += size;
740
741 } while (size);
742
743 ret = ks7010_sdio_writeb(priv, GCR_A_REG, GCR_A_REMAP);
744
745free_rom_buf:
746 kfree(rom_buf);
747 return ret;
748}
749
750static int ks7010_upload_firmware(struct ks_sdio_card *card)
751{
752 struct ks_wlan_private *priv = card->priv;
753 struct sdio_func *func = ks7010_to_func(priv);
754 unsigned int n;
755 u8 byte = 0;
756 int ret;
757 const struct firmware *fw_entry = NULL;
758
759 sdio_claim_host(func);
760
761
762 ret = ks7010_sdio_readb(priv, GCR_A_REG, &byte);
763 if (ret)
764 goto release_host;
765 if (byte == GCR_A_RUN) {
766 netdev_dbg(priv->net_dev, "MAC firmware running ...\n");
767 ret = -EBUSY;
768 goto release_host;
769 }
770
771 ret = request_firmware(&fw_entry, ROM_FILE,
772 &func->dev);
773 if (ret)
774 goto release_host;
775
776 ret = ks7010_copy_firmware(priv, fw_entry);
777 if (ret)
778 goto release_firmware;
779
780
781 for (n = 0; n < 50; ++n) {
782 usleep_range(10000, 11000);
783 ret = ks7010_sdio_readb(priv, GCR_A_REG, &byte);
784 if (ret)
785 goto release_firmware;
786
787 if (byte == GCR_A_RUN)
788 break;
789 }
790 if ((50) <= n) {
791 netdev_err(priv->net_dev, "firmware can't start\n");
792 ret = -EIO;
793 goto release_firmware;
794 }
795
796 ret = 0;
797
798 release_firmware:
799 release_firmware(fw_entry);
800 release_host:
801 sdio_release_host(func);
802
803 return ret;
804}
805
806static void ks7010_sme_enqueue_events(struct ks_wlan_private *priv)
807{
808 static const u16 init_events[] = {
809 SME_GET_EEPROM_CKSUM, SME_STOP_REQUEST,
810 SME_RTS_THRESHOLD_REQUEST, SME_FRAGMENTATION_THRESHOLD_REQUEST,
811 SME_WEP_INDEX_REQUEST, SME_WEP_KEY1_REQUEST,
812 SME_WEP_KEY2_REQUEST, SME_WEP_KEY3_REQUEST,
813 SME_WEP_KEY4_REQUEST, SME_WEP_FLAG_REQUEST,
814 SME_RSN_ENABLED_REQUEST, SME_MODE_SET_REQUEST,
815 SME_START_REQUEST
816 };
817 int ev;
818
819 for (ev = 0; ev < ARRAY_SIZE(init_events); ev++)
820 hostif_sme_enqueue(priv, init_events[ev]);
821}
822
823static void ks7010_card_init(struct ks_wlan_private *priv)
824{
825 init_completion(&priv->confirm_wait);
826
827
828 hostif_sme_enqueue(priv, SME_START);
829
830 if (!wait_for_completion_interruptible_timeout
831 (&priv->confirm_wait, 5 * HZ)) {
832 netdev_dbg(priv->net_dev, "wait time out!! SME_START\n");
833 }
834
835 if (priv->mac_address_valid && priv->version_size != 0)
836 priv->dev_state = DEVICE_STATE_PREINIT;
837
838 ks7010_sme_enqueue_events(priv);
839
840 if (!wait_for_completion_interruptible_timeout
841 (&priv->confirm_wait, 5 * HZ)) {
842 netdev_dbg(priv->net_dev, "wait time out!! wireless parameter set\n");
843 }
844
845 if (priv->dev_state >= DEVICE_STATE_PREINIT) {
846 netdev_dbg(priv->net_dev, "DEVICE READY!!\n");
847 priv->dev_state = DEVICE_STATE_READY;
848 }
849}
850
851static void ks7010_init_defaults(struct ks_wlan_private *priv)
852{
853 priv->reg.tx_rate = TX_RATE_AUTO;
854 priv->reg.preamble = LONG_PREAMBLE;
855 priv->reg.power_mgmt = POWER_MGMT_ACTIVE;
856 priv->reg.scan_type = ACTIVE_SCAN;
857 priv->reg.beacon_lost_count = 20;
858 priv->reg.rts = 2347UL;
859 priv->reg.fragment = 2346UL;
860 priv->reg.phy_type = D_11BG_COMPATIBLE_MODE;
861 priv->reg.cts_mode = CTS_MODE_FALSE;
862 priv->reg.rate_set.body[11] = TX_RATE_54M;
863 priv->reg.rate_set.body[10] = TX_RATE_48M;
864 priv->reg.rate_set.body[9] = TX_RATE_36M;
865 priv->reg.rate_set.body[8] = TX_RATE_18M;
866 priv->reg.rate_set.body[7] = TX_RATE_9M;
867 priv->reg.rate_set.body[6] = TX_RATE_24M | BASIC_RATE;
868 priv->reg.rate_set.body[5] = TX_RATE_12M | BASIC_RATE;
869 priv->reg.rate_set.body[4] = TX_RATE_6M | BASIC_RATE;
870 priv->reg.rate_set.body[3] = TX_RATE_11M | BASIC_RATE;
871 priv->reg.rate_set.body[2] = TX_RATE_5M | BASIC_RATE;
872 priv->reg.rate_set.body[1] = TX_RATE_2M | BASIC_RATE;
873 priv->reg.rate_set.body[0] = TX_RATE_1M | BASIC_RATE;
874 priv->reg.tx_rate = TX_RATE_FULL_AUTO;
875 priv->reg.rate_set.size = 12;
876}
877
878static int ks7010_sdio_setup_irqs(struct sdio_func *func)
879{
880 int ret;
881
882
883 sdio_writeb(func, 0, INT_ENABLE_REG, &ret);
884 if (ret)
885 goto irq_error;
886
887 sdio_writeb(func, 0xff, INT_PENDING_REG, &ret);
888 if (ret)
889 goto irq_error;
890
891
892 ret = sdio_claim_irq(func, ks_sdio_interrupt);
893
894irq_error:
895 return ret;
896}
897
898static void ks7010_sdio_init_irqs(struct sdio_func *func,
899 struct ks_wlan_private *priv)
900{
901 u8 byte;
902 int ret;
903
904
905
906
907
908
909 sdio_claim_host(func);
910 ret = ks7010_sdio_writeb(priv, INT_PENDING_REG, 0xff);
911 sdio_release_host(func);
912 if (ret)
913 netdev_err(priv->net_dev, "write INT_PENDING_REG\n");
914
915
916 byte = (INT_GCR_B | INT_READ_STATUS | INT_WRITE_STATUS);
917 sdio_claim_host(func);
918 ret = ks7010_sdio_writeb(priv, INT_ENABLE_REG, byte);
919 sdio_release_host(func);
920 if (ret)
921 netdev_err(priv->net_dev, "write INT_ENABLE_REG\n");
922}
923
924static void ks7010_private_init(struct ks_wlan_private *priv,
925 struct ks_sdio_card *card,
926 struct net_device *netdev)
927{
928
929 priv->if_hw = card;
930
931 priv->dev_state = DEVICE_STATE_PREBOOT;
932 priv->net_dev = netdev;
933 priv->firmware_version[0] = '\0';
934 priv->version_size = 0;
935 priv->last_doze = jiffies;
936 priv->last_wakeup = jiffies;
937 memset(&priv->nstats, 0, sizeof(priv->nstats));
938 memset(&priv->wstats, 0, sizeof(priv->wstats));
939
940
941 atomic_set(&priv->sleepstatus.doze_request, 0);
942 atomic_set(&priv->sleepstatus.wakeup_request, 0);
943 atomic_set(&priv->sleepstatus.wakeup_request, 0);
944
945 trx_device_init(priv);
946 hostif_init(priv);
947 ks_wlan_net_start(netdev);
948 ks7010_init_defaults(priv);
949}
950
951static int ks7010_sdio_probe(struct sdio_func *func,
952 const struct sdio_device_id *device)
953{
954 struct ks_wlan_private *priv = NULL;
955 struct net_device *netdev = NULL;
956 struct ks_sdio_card *card;
957 int ret;
958
959 card = kzalloc(sizeof(*card), GFP_KERNEL);
960 if (!card)
961 return -ENOMEM;
962
963 card->func = func;
964
965 sdio_claim_host(func);
966
967 ret = sdio_set_block_size(func, KS7010_IO_BLOCK_SIZE);
968 if (ret)
969 goto err_free_card;
970
971 dev_dbg(&card->func->dev, "multi_block=%d sdio_set_block_size()=%d %d\n",
972 func->card->cccr.multi_block, func->cur_blksize, ret);
973
974 ret = sdio_enable_func(func);
975 if (ret)
976 goto err_free_card;
977
978 ret = ks7010_sdio_setup_irqs(func);
979 if (ret)
980 goto err_disable_func;
981
982 sdio_release_host(func);
983
984 sdio_set_drvdata(func, card);
985
986 dev_dbg(&card->func->dev, "class = 0x%X, vendor = 0x%X, device = 0x%X\n",
987 func->class, func->vendor, func->device);
988
989
990 netdev = alloc_etherdev(sizeof(*priv));
991 if (!netdev) {
992 dev_err(&card->func->dev, "Unable to alloc new net device\n");
993 goto err_release_irq;
994 }
995
996 ret = dev_alloc_name(netdev, "wlan%d");
997 if (ret < 0) {
998 dev_err(&card->func->dev, "Couldn't get name!\n");
999 goto err_free_netdev;
1000 }
1001
1002 priv = netdev_priv(netdev);
1003
1004 card->priv = priv;
1005 SET_NETDEV_DEV(netdev, &card->func->dev);
1006
1007 ks7010_private_init(priv, card, netdev);
1008
1009 ret = ks7010_upload_firmware(card);
1010 if (ret) {
1011 netdev_err(priv->net_dev,
1012 "firmware load failed !! ret = %d\n", ret);
1013 goto err_free_netdev;
1014 }
1015
1016 ks7010_sdio_init_irqs(func, priv);
1017
1018 priv->dev_state = DEVICE_STATE_BOOT;
1019
1020 priv->wq = alloc_workqueue("wq", WQ_MEM_RECLAIM, 1);
1021 if (!priv->wq) {
1022 netdev_err(priv->net_dev, "create_workqueue failed !!\n");
1023 goto err_free_netdev;
1024 }
1025
1026 INIT_DELAYED_WORK(&priv->rw_dwork, ks7010_rw_function);
1027 ks7010_card_init(priv);
1028
1029 ret = register_netdev(priv->net_dev);
1030 if (ret)
1031 goto err_free_netdev;
1032
1033 return 0;
1034
1035 err_free_netdev:
1036 free_netdev(netdev);
1037 err_release_irq:
1038 sdio_claim_host(func);
1039 sdio_release_irq(func);
1040 err_disable_func:
1041 sdio_disable_func(func);
1042 err_free_card:
1043 sdio_release_host(func);
1044 sdio_set_drvdata(func, NULL);
1045 kfree(card);
1046
1047 return -ENODEV;
1048}
1049
1050
1051static int send_stop_request(struct sdio_func *func)
1052{
1053 struct hostif_stop_request *pp;
1054 struct ks_sdio_card *card;
1055 size_t size;
1056
1057 card = sdio_get_drvdata(func);
1058
1059 pp = kzalloc(hif_align_size(sizeof(*pp)), GFP_KERNEL);
1060 if (!pp)
1061 return -ENOMEM;
1062
1063 size = sizeof(*pp) - sizeof(pp->header.size);
1064 pp->header.size = cpu_to_le16(size);
1065 pp->header.event = cpu_to_le16(HIF_STOP_REQ);
1066
1067 sdio_claim_host(func);
1068 write_to_device(card->priv, (u8 *)pp, hif_align_size(sizeof(*pp)));
1069 sdio_release_host(func);
1070
1071 kfree(pp);
1072 return 0;
1073}
1074
1075static void ks7010_sdio_remove(struct sdio_func *func)
1076{
1077 int ret;
1078 struct ks_sdio_card *card;
1079 struct ks_wlan_private *priv;
1080
1081 card = sdio_get_drvdata(func);
1082
1083 if (!card)
1084 return;
1085
1086 priv = card->priv;
1087 if (!priv)
1088 goto err_free_card;
1089
1090 ks_wlan_net_stop(priv->net_dev);
1091
1092
1093 sdio_claim_host(func);
1094 sdio_writeb(func, 0, INT_ENABLE_REG, &ret);
1095 sdio_writeb(func, 0xff, INT_PENDING_REG, &ret);
1096 sdio_release_host(func);
1097
1098 ret = send_stop_request(func);
1099 if (ret)
1100 goto err_free_card;
1101
1102 if (priv->wq) {
1103 flush_workqueue(priv->wq);
1104 destroy_workqueue(priv->wq);
1105 }
1106
1107 hostif_exit(priv);
1108
1109 unregister_netdev(priv->net_dev);
1110
1111 trx_device_exit(priv);
1112 free_netdev(priv->net_dev);
1113 card->priv = NULL;
1114
1115 sdio_claim_host(func);
1116 sdio_release_irq(func);
1117 sdio_disable_func(func);
1118 sdio_release_host(func);
1119err_free_card:
1120 sdio_set_drvdata(func, NULL);
1121 kfree(card);
1122}
1123
1124static const struct sdio_device_id ks7010_sdio_ids[] = {
1125 {SDIO_DEVICE(SDIO_VENDOR_ID_KS_CODE_A, SDIO_DEVICE_ID_KS_7010)},
1126 {SDIO_DEVICE(SDIO_VENDOR_ID_KS_CODE_B, SDIO_DEVICE_ID_KS_7010)},
1127 { }
1128};
1129MODULE_DEVICE_TABLE(sdio, ks7010_sdio_ids);
1130
1131static struct sdio_driver ks7010_sdio_driver = {
1132 .name = "ks7010_sdio",
1133 .id_table = ks7010_sdio_ids,
1134 .probe = ks7010_sdio_probe,
1135 .remove = ks7010_sdio_remove,
1136};
1137
1138module_driver(ks7010_sdio_driver, sdio_register_driver, sdio_unregister_driver);
1139MODULE_AUTHOR("Sang Engineering, Qi-Hardware, KeyStream");
1140MODULE_DESCRIPTION("Driver for KeyStream KS7010 based SDIO cards");
1141MODULE_LICENSE("GPL v2");
1142MODULE_FIRMWARE(ROM_FILE);
1143