1
2
3
4
5
6
7
8
9
10
11
12
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
17
18#include <linux/mmc/core.h>
19#include <linux/mmc/card.h>
20#include <linux/mmc/sdio_func.h>
21#include <linux/mmc/sdio_ids.h>
22
23#include "gdm_sdio.h"
24#include "gdm_wimax.h"
25#include "sdio_boot.h"
26#include "hci.h"
27
28#define TYPE_A_HEADER_SIZE 4
29#define TYPE_A_LOOKAHEAD_SIZE 16
30
31#define MAX_NR_RX_BUF 4
32
33#define SDU_TX_BUF_SIZE 2048
34#define TX_BUF_SIZE 2048
35#define TX_CHUNK_SIZE (2048 - TYPE_A_HEADER_SIZE)
36#define RX_BUF_SIZE (25*1024)
37
38#define TX_HZ 2000
39#define TX_INTERVAL (1000000/TX_HZ)
40
41
42
43static int init_sdio(struct sdiowm_dev *sdev);
44static void release_sdio(struct sdiowm_dev *sdev);
45
46#ifdef DEBUG
47static void hexdump(char *title, u8 *data, int len)
48{
49 int i;
50
51 printk(KERN_DEBUG "%s: length = %d\n", title, len);
52 for (i = 0; i < len; i++) {
53 printk(KERN_DEBUG "%02x ", data[i]);
54 if ((i & 0xf) == 0xf)
55 printk(KERN_DEBUG "\n");
56 }
57 printk(KERN_DEBUG "\n");
58}
59#endif
60
61static struct sdio_tx *alloc_tx_struct(struct tx_cxt *tx)
62{
63 struct sdio_tx *t = kzalloc(sizeof(*t), GFP_ATOMIC);
64
65 if (!t)
66 return NULL;
67
68 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
69 if (!t->buf) {
70 kfree(t);
71 return NULL;
72 }
73
74 t->tx_cxt = tx;
75
76 return t;
77}
78
79static void free_tx_struct(struct sdio_tx *t)
80{
81 if (t) {
82 kfree(t->buf);
83 kfree(t);
84 }
85}
86
87static struct sdio_rx *alloc_rx_struct(struct rx_cxt *rx)
88{
89 struct sdio_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
90
91 if (r)
92 r->rx_cxt = rx;
93
94 return r;
95}
96
97static void free_rx_struct(struct sdio_rx *r)
98{
99 kfree(r);
100}
101
102
103static struct sdio_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
104{
105 struct sdio_tx *t;
106
107 if (list_empty(&tx->free_list))
108 return NULL;
109
110 t = list_entry(tx->free_list.prev, struct sdio_tx, list);
111 list_del(&t->list);
112
113 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
114
115 return t;
116}
117
118
119static void put_tx_struct(struct tx_cxt *tx, struct sdio_tx *t)
120{
121 list_add_tail(&t->list, &tx->free_list);
122}
123
124
125static struct sdio_rx *get_rx_struct(struct rx_cxt *rx)
126{
127 struct sdio_rx *r;
128
129 if (list_empty(&rx->free_list))
130 return NULL;
131
132 r = list_entry(rx->free_list.prev, struct sdio_rx, list);
133 list_del(&r->list);
134
135 return r;
136}
137
138
139static void put_rx_struct(struct rx_cxt *rx, struct sdio_rx *r)
140{
141 list_add_tail(&r->list, &rx->free_list);
142}
143
144static int init_sdio(struct sdiowm_dev *sdev)
145{
146 int ret = 0, i;
147 struct tx_cxt *tx = &sdev->tx;
148 struct rx_cxt *rx = &sdev->rx;
149 struct sdio_tx *t;
150 struct sdio_rx *r;
151
152 INIT_LIST_HEAD(&tx->free_list);
153 INIT_LIST_HEAD(&tx->sdu_list);
154 INIT_LIST_HEAD(&tx->hci_list);
155
156 spin_lock_init(&tx->lock);
157
158 tx->sdu_buf = kmalloc(SDU_TX_BUF_SIZE, GFP_KERNEL);
159 if (tx->sdu_buf == NULL)
160 goto fail;
161
162 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
163 t = alloc_tx_struct(tx);
164 if (t == NULL) {
165 ret = -ENOMEM;
166 goto fail;
167 }
168 list_add(&t->list, &tx->free_list);
169 }
170
171 INIT_LIST_HEAD(&rx->free_list);
172 INIT_LIST_HEAD(&rx->req_list);
173
174 spin_lock_init(&rx->lock);
175
176 for (i = 0; i < MAX_NR_RX_BUF; i++) {
177 r = alloc_rx_struct(rx);
178 if (r == NULL) {
179 ret = -ENOMEM;
180 goto fail;
181 }
182 list_add(&r->list, &rx->free_list);
183 }
184
185 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
186 if (rx->rx_buf == NULL)
187 goto fail;
188
189 return 0;
190
191fail:
192 release_sdio(sdev);
193 return ret;
194}
195
196static void release_sdio(struct sdiowm_dev *sdev)
197{
198 struct tx_cxt *tx = &sdev->tx;
199 struct rx_cxt *rx = &sdev->rx;
200 struct sdio_tx *t, *t_next;
201 struct sdio_rx *r, *r_next;
202
203 kfree(tx->sdu_buf);
204
205 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
206 list_del(&t->list);
207 free_tx_struct(t);
208 }
209
210 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
211 list_del(&t->list);
212 free_tx_struct(t);
213 }
214
215 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
216 list_del(&t->list);
217 free_tx_struct(t);
218 }
219
220 kfree(rx->rx_buf);
221
222 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
223 list_del(&r->list);
224 free_rx_struct(r);
225 }
226
227 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
228 list_del(&r->list);
229 free_rx_struct(r);
230 }
231}
232
233static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
234{
235 int n, blocks, ret, remain;
236
237 sdio_claim_host(func);
238
239 blocks = len / func->cur_blksize;
240 n = blocks * func->cur_blksize;
241 if (blocks) {
242 ret = sdio_memcpy_toio(func, 0, data, n);
243 if (ret < 0) {
244 if (ret != -ENOMEDIUM)
245 dev_err(&func->dev,
246 "gdmwms: %s error: ret = %d\n",
247 __func__, ret);
248 goto end_io;
249 }
250 }
251
252 remain = len - n;
253 remain = (remain + 3) & ~3;
254
255 if (remain) {
256 ret = sdio_memcpy_toio(func, 0, data + n, remain);
257 if (ret < 0) {
258 if (ret != -ENOMEDIUM)
259 dev_err(&func->dev,
260 "gdmwms: %s error: ret = %d\n",
261 __func__, ret);
262 goto end_io;
263 }
264 }
265
266end_io:
267 sdio_release_host(func);
268}
269
270static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
271{
272 struct list_head *l, *next;
273 struct hci_s *hci;
274 struct sdio_tx *t;
275 int pos, len, i, estlen, aggr_num = 0, aggr_len;
276 u8 *buf;
277 unsigned long flags;
278
279 spin_lock_irqsave(&tx->lock, flags);
280
281 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
282 list_for_each_entry(t, &tx->sdu_list, list) {
283 estlen = ((t->len + 3) & ~3) + 4;
284 if ((pos + estlen) > SDU_TX_BUF_SIZE)
285 break;
286
287 aggr_num++;
288 memcpy(tx->sdu_buf + pos, t->buf, t->len);
289 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
290 pos += estlen;
291 }
292 aggr_len = pos;
293
294 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
295 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
296 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
297
298 spin_unlock_irqrestore(&tx->lock, flags);
299
300#ifdef DEBUG
301 hexdump("sdio_send", tx->sdu_buf + TYPE_A_HEADER_SIZE,
302 aggr_len - TYPE_A_HEADER_SIZE);
303#endif
304
305 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
306 len = aggr_len - pos;
307 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
308 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
309
310 buf[0] = len & 0xff;
311 buf[1] = (len >> 8) & 0xff;
312 buf[2] = (len >> 16) & 0xff;
313 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
314 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
315 }
316
317 spin_lock_irqsave(&tx->lock, flags);
318
319 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
320 next = l->next;
321 t = list_entry(l, struct sdio_tx, list);
322 if (t->callback)
323 t->callback(t->cb_data);
324
325 list_del(l);
326 put_tx_struct(t->tx_cxt, t);
327 }
328
329 do_gettimeofday(&tx->sdu_stamp);
330 spin_unlock_irqrestore(&tx->lock, flags);
331}
332
333static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
334 struct sdio_tx *t)
335{
336 unsigned long flags;
337
338#ifdef DEBUG
339 hexdump("sdio_send", t->buf + TYPE_A_HEADER_SIZE,
340 t->len - TYPE_A_HEADER_SIZE);
341#endif
342 send_sdio_pkt(func, t->buf, t->len);
343
344 spin_lock_irqsave(&tx->lock, flags);
345 if (t->callback)
346 t->callback(t->cb_data);
347 free_tx_struct(t);
348 spin_unlock_irqrestore(&tx->lock, flags);
349}
350
351static void do_tx(struct work_struct *work)
352{
353 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
354 struct sdio_func *func = sdev->func;
355 struct tx_cxt *tx = &sdev->tx;
356 struct sdio_tx *t = NULL;
357 struct timeval now, *before;
358 int is_sdu = 0;
359 long diff;
360 unsigned long flags;
361
362 spin_lock_irqsave(&tx->lock, flags);
363 if (!tx->can_send) {
364 spin_unlock_irqrestore(&tx->lock, flags);
365 return;
366 }
367
368 if (!list_empty(&tx->hci_list)) {
369 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
370 list_del(&t->list);
371 is_sdu = 0;
372 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
373 do_gettimeofday(&now);
374 before = &tx->sdu_stamp;
375
376 diff = (now.tv_sec - before->tv_sec) * 1000000 +
377 (now.tv_usec - before->tv_usec);
378 if (diff >= 0 && diff < TX_INTERVAL) {
379 schedule_work(&sdev->ws);
380 spin_unlock_irqrestore(&tx->lock, flags);
381 return;
382 }
383 is_sdu = 1;
384 }
385
386 if (!is_sdu && t == NULL) {
387 spin_unlock_irqrestore(&tx->lock, flags);
388 return;
389 }
390
391 tx->can_send = 0;
392
393 spin_unlock_irqrestore(&tx->lock, flags);
394
395 if (is_sdu)
396 send_sdu(func, tx);
397 else
398 send_hci(func, tx, t);
399}
400
401static int gdm_sdio_send(void *priv_dev, void *data, int len,
402 void (*cb)(void *data), void *cb_data)
403{
404 struct sdiowm_dev *sdev = priv_dev;
405 struct tx_cxt *tx = &sdev->tx;
406 struct sdio_tx *t;
407 u8 *pkt = data;
408 int no_spc = 0;
409 u16 cmd_evt;
410 unsigned long flags;
411
412 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
413
414 spin_lock_irqsave(&tx->lock, flags);
415
416 cmd_evt = (pkt[0] << 8) | pkt[1];
417 if (cmd_evt == WIMAX_TX_SDU) {
418 t = get_tx_struct(tx, &no_spc);
419 if (t == NULL) {
420
421 spin_unlock_irqrestore(&tx->lock, flags);
422 return -ENOSPC;
423 }
424 list_add_tail(&t->list, &tx->sdu_list);
425
426 memcpy(t->buf, data, len);
427
428 t->len = len;
429 t->callback = cb;
430 t->cb_data = cb_data;
431 } else {
432 t = alloc_tx_struct(tx);
433 if (t == NULL) {
434 spin_unlock_irqrestore(&tx->lock, flags);
435 return -ENOMEM;
436 }
437 list_add_tail(&t->list, &tx->hci_list);
438
439 t->buf[0] = len & 0xff;
440 t->buf[1] = (len >> 8) & 0xff;
441 t->buf[2] = (len >> 16) & 0xff;
442 t->buf[3] = 2;
443 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
444
445 t->len = len + TYPE_A_HEADER_SIZE;
446 t->callback = cb;
447 t->cb_data = cb_data;
448 }
449
450 if (tx->can_send)
451 schedule_work(&sdev->ws);
452
453 spin_unlock_irqrestore(&tx->lock, flags);
454
455 if (no_spc)
456 return -ENOSPC;
457
458 return 0;
459}
460
461
462
463
464static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
465{
466 struct tx_cxt *tx = &sdev->tx;
467 u16 cmd_evt;
468 unsigned long flags;
469
470 spin_lock_irqsave(&tx->lock, flags);
471
472 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
473 if (cmd_evt != WIMAX_SDU_TX_FLOW)
474 goto out;
475
476 if (hci_data[4] == 0) {
477#ifdef DEBUG
478 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
479#endif
480 tx->stop_sdu_tx = 1;
481 } else if (hci_data[4] == 1) {
482#ifdef DEBUG
483 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
484#endif
485 tx->stop_sdu_tx = 0;
486 if (tx->can_send)
487 schedule_work(&sdev->ws);
488
489
490
491
492
493 if (list_empty(&tx->free_list))
494 len = 0;
495 }
496
497out:
498 spin_unlock_irqrestore(&tx->lock, flags);
499 return len;
500}
501
502static void gdm_sdio_irq(struct sdio_func *func)
503{
504 struct phy_dev *phy_dev = sdio_get_drvdata(func);
505 struct sdiowm_dev *sdev = phy_dev->priv_dev;
506 struct tx_cxt *tx = &sdev->tx;
507 struct rx_cxt *rx = &sdev->rx;
508 struct sdio_rx *r;
509 unsigned long flags;
510 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
511 u32 len, blocks, n;
512 int ret, remain;
513
514
515 val = sdio_readb(func, 0x13, &ret);
516 if (val & 0x01)
517 sdio_writeb(func, 0x01, 0x13, &ret);
518 else
519 return;
520
521 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
522 if (ret) {
523 dev_err(&func->dev,
524 "Cannot read from function %d\n", func->num);
525 goto done;
526 }
527
528 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
529 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
530 dev_err(&func->dev, "Too big Type-A size: %d\n", len);
531 goto done;
532 }
533
534 if (hdr[3] == 1) {
535#ifdef DEBUG
536 u32 *ack_seq = (u32 *)&hdr[4];
537#endif
538 spin_lock_irqsave(&tx->lock, flags);
539 tx->can_send = 1;
540
541 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
542 schedule_work(&sdev->ws);
543 spin_unlock_irqrestore(&tx->lock, flags);
544#ifdef DEBUG
545 printk(KERN_DEBUG "Ack... %0x\n", ntohl(*ack_seq));
546#endif
547 goto done;
548 }
549
550 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
551 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
552
553 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
554 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
555 if (remain <= 0)
556 goto end_io;
557
558 blocks = remain / func->cur_blksize;
559
560 if (blocks) {
561 n = blocks * func->cur_blksize;
562 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
563 if (ret) {
564 dev_err(&func->dev,
565 "Cannot read from function %d\n", func->num);
566 goto done;
567 }
568 buf += n;
569 remain -= n;
570 }
571
572 if (remain) {
573 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
574 if (ret) {
575 dev_err(&func->dev,
576 "Cannot read from function %d\n", func->num);
577 goto done;
578 }
579 }
580
581end_io:
582#ifdef DEBUG
583 hexdump("sdio_receive", rx->rx_buf, len);
584#endif
585 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
586
587 spin_lock_irqsave(&rx->lock, flags);
588
589 if (!list_empty(&rx->req_list)) {
590 r = list_entry(rx->req_list.next, struct sdio_rx, list);
591 spin_unlock_irqrestore(&rx->lock, flags);
592 if (r->callback)
593 r->callback(r->cb_data, rx->rx_buf, len);
594 spin_lock_irqsave(&rx->lock, flags);
595 list_del(&r->list);
596 put_rx_struct(rx, r);
597 }
598
599 spin_unlock_irqrestore(&rx->lock, flags);
600
601done:
602 sdio_writeb(func, 0x00, 0x10, &ret);
603 if (!phy_dev->netdev)
604 register_wimax_device(phy_dev, &func->dev);
605}
606
607static int gdm_sdio_receive(void *priv_dev,
608 void (*cb)(void *cb_data, void *data, int len),
609 void *cb_data)
610{
611 struct sdiowm_dev *sdev = priv_dev;
612 struct rx_cxt *rx = &sdev->rx;
613 struct sdio_rx *r;
614 unsigned long flags;
615
616 spin_lock_irqsave(&rx->lock, flags);
617 r = get_rx_struct(rx);
618 if (r == NULL) {
619 spin_unlock_irqrestore(&rx->lock, flags);
620 return -ENOMEM;
621 }
622
623 r->callback = cb;
624 r->cb_data = cb_data;
625
626 list_add_tail(&r->list, &rx->req_list);
627 spin_unlock_irqrestore(&rx->lock, flags);
628
629 return 0;
630}
631
632static int sdio_wimax_probe(struct sdio_func *func,
633 const struct sdio_device_id *id)
634{
635 int ret;
636 struct phy_dev *phy_dev = NULL;
637 struct sdiowm_dev *sdev = NULL;
638
639 dev_info(&func->dev, "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
640 func->vendor, func->device);
641 dev_info(&func->dev, "GCT WiMax driver version %s\n", DRIVER_VERSION);
642
643 sdio_claim_host(func);
644 sdio_enable_func(func);
645 sdio_claim_irq(func, gdm_sdio_irq);
646
647 ret = sdio_boot(func);
648 if (ret)
649 return ret;
650
651 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
652 if (phy_dev == NULL) {
653 ret = -ENOMEM;
654 goto out;
655 }
656 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
657 if (sdev == NULL) {
658 ret = -ENOMEM;
659 goto out;
660 }
661
662 phy_dev->priv_dev = (void *)sdev;
663 phy_dev->send_func = gdm_sdio_send;
664 phy_dev->rcv_func = gdm_sdio_receive;
665
666 ret = init_sdio(sdev);
667 if (ret < 0)
668 goto out;
669
670 sdev->func = func;
671
672 sdio_writeb(func, 1, 0x14, &ret);
673 sdio_release_host(func);
674
675 INIT_WORK(&sdev->ws, do_tx);
676
677 sdio_set_drvdata(func, phy_dev);
678out:
679 if (ret) {
680 kfree(phy_dev);
681 kfree(sdev);
682 }
683
684 return ret;
685}
686
687static void sdio_wimax_remove(struct sdio_func *func)
688{
689 struct phy_dev *phy_dev = sdio_get_drvdata(func);
690 struct sdiowm_dev *sdev = phy_dev->priv_dev;
691
692 cancel_work_sync(&sdev->ws);
693 if (phy_dev->netdev)
694 unregister_wimax_device(phy_dev);
695 sdio_claim_host(func);
696 sdio_release_irq(func);
697 sdio_disable_func(func);
698 sdio_release_host(func);
699 release_sdio(sdev);
700
701 kfree(sdev);
702 kfree(phy_dev);
703}
704
705static const struct sdio_device_id sdio_wimax_ids[] = {
706 { SDIO_DEVICE(0x0296, 0x5347) },
707 {0}
708};
709
710MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
711
712static struct sdio_driver sdio_wimax_driver = {
713 .probe = sdio_wimax_probe,
714 .remove = sdio_wimax_remove,
715 .name = "sdio_wimax",
716 .id_table = sdio_wimax_ids,
717};
718
719static int __init sdio_gdm_wimax_init(void)
720{
721 return sdio_register_driver(&sdio_wimax_driver);
722}
723
724static void __exit sdio_gdm_wimax_exit(void)
725{
726 sdio_unregister_driver(&sdio_wimax_driver);
727}
728
729module_init(sdio_gdm_wimax_init);
730module_exit(sdio_gdm_wimax_exit);
731
732MODULE_VERSION(DRIVER_VERSION);
733MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
734MODULE_AUTHOR("Ethan Park");
735MODULE_LICENSE("GPL");
736