2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
4 * This software is licensed under the terms of the GNU General Public
5 * License version 2, as published by the Free Software Foundation, and
6 * may be copied, distributed, and modified under those terms.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/kernel.h>
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>
24 #include "gdm_wimax.h"
25 #include "sdio_boot.h"
28 #define TYPE_A_HEADER_SIZE 4
29 #define TYPE_A_LOOKAHEAD_SIZE 16
31 #define MAX_NR_RX_BUF 4
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)
39 #define TX_INTERVAL (1000000/TX_HZ)
43 static int init_sdio(struct sdiowm_dev *sdev);
44 static void release_sdio(struct sdiowm_dev *sdev);
47 static void hexdump(char *title, u8 *data, int len)
51 printk(KERN_DEBUG "%s: length = %d\n", title, len);
52 for (i = 0; i < len; i++) {
53 printk(KERN_DEBUG "%02x ", data[i]);
55 printk(KERN_DEBUG "\n");
57 printk(KERN_DEBUG "\n");
61 static struct sdio_tx *alloc_tx_struct(struct tx_cxt *tx)
63 struct sdio_tx *t = NULL;
65 t = kmalloc(sizeof(*t), GFP_ATOMIC);
69 memset(t, 0, sizeof(*t));
71 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
86 static void free_tx_struct(struct sdio_tx *t)
94 static struct sdio_rx *alloc_rx_struct(struct rx_cxt *rx)
96 struct sdio_rx *r = NULL;
98 r = kmalloc(sizeof(*r), GFP_ATOMIC);
102 memset(r, 0, sizeof(*r));
112 static void free_rx_struct(struct sdio_rx *r)
117 /* Before this function is called, spin lock should be locked. */
118 static struct sdio_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
122 if (list_empty(&tx->free_list))
125 t = list_entry(tx->free_list.prev, struct sdio_tx, list);
128 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
133 /* Before this function is called, spin lock should be locked. */
134 static void put_tx_struct(struct tx_cxt *tx, struct sdio_tx *t)
136 list_add_tail(&t->list, &tx->free_list);
139 /* Before this function is called, spin lock should be locked. */
140 static struct sdio_rx *get_rx_struct(struct rx_cxt *rx)
144 if (list_empty(&rx->free_list))
147 r = list_entry(rx->free_list.prev, struct sdio_rx, list);
153 /* Before this function is called, spin lock should be locked. */
154 static void put_rx_struct(struct rx_cxt *rx, struct sdio_rx *r)
156 list_add_tail(&r->list, &rx->free_list);
159 static int init_sdio(struct sdiowm_dev *sdev)
162 struct tx_cxt *tx = &sdev->tx;
163 struct rx_cxt *rx = &sdev->rx;
167 INIT_LIST_HEAD(&tx->free_list);
168 INIT_LIST_HEAD(&tx->sdu_list);
169 INIT_LIST_HEAD(&tx->hci_list);
171 spin_lock_init(&tx->lock);
173 tx->sdu_buf = kmalloc(SDU_TX_BUF_SIZE, GFP_KERNEL);
174 if (tx->sdu_buf == NULL) {
175 printk(KERN_ERR "Failed to allocate SDU tx buffer.\n");
179 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
180 t = alloc_tx_struct(tx);
185 list_add(&t->list, &tx->free_list);
188 INIT_LIST_HEAD(&rx->free_list);
189 INIT_LIST_HEAD(&rx->req_list);
191 spin_lock_init(&rx->lock);
193 for (i = 0; i < MAX_NR_RX_BUF; i++) {
194 r = alloc_rx_struct(rx);
199 list_add(&r->list, &rx->free_list);
202 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
203 if (rx->rx_buf == NULL) {
204 printk(KERN_ERR "Failed to allocate rx buffer.\n");
215 static void release_sdio(struct sdiowm_dev *sdev)
217 struct tx_cxt *tx = &sdev->tx;
218 struct rx_cxt *rx = &sdev->rx;
219 struct sdio_tx *t, *t_next;
220 struct sdio_rx *r, *r_next;
224 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
229 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
234 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
241 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
246 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
252 static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
254 int n, blocks, ret, remain;
256 sdio_claim_host(func);
258 blocks = len / func->cur_blksize;
259 n = blocks * func->cur_blksize;
261 ret = sdio_memcpy_toio(func, 0, data, n);
263 if (ret != -ENOMEDIUM)
264 printk(KERN_ERR "gdmwms: %s error: ret = %d\n",
271 remain = (remain + 3) & ~3;
274 ret = sdio_memcpy_toio(func, 0, data + n, remain);
276 if (ret != -ENOMEDIUM)
277 printk(KERN_ERR "gdmwms: %s error: ret = %d\n",
284 sdio_release_host(func);
287 static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
289 struct list_head *l, *next;
292 int pos, len, i, estlen, aggr_num = 0, aggr_len;
296 spin_lock_irqsave(&tx->lock, flags);
298 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
299 list_for_each_entry(t, &tx->sdu_list, list) {
300 estlen = ((t->len + 3) & ~3) + 4;
301 if ((pos + estlen) > SDU_TX_BUF_SIZE)
305 memcpy(tx->sdu_buf + pos, t->buf, t->len);
306 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
311 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
312 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
313 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
315 spin_unlock_irqrestore(&tx->lock, flags);
318 hexdump("sdio_send", tx->sdu_buf + TYPE_A_HEADER_SIZE,
319 aggr_len - TYPE_A_HEADER_SIZE);
322 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
323 len = aggr_len - pos;
324 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
325 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
328 buf[1] = (len >> 8) & 0xff;
329 buf[2] = (len >> 16) & 0xff;
330 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
331 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
334 spin_lock_irqsave(&tx->lock, flags);
336 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
338 t = list_entry(l, struct sdio_tx, list);
340 t->callback(t->cb_data);
343 put_tx_struct(t->tx_cxt, t);
346 do_gettimeofday(&tx->sdu_stamp);
347 spin_unlock_irqrestore(&tx->lock, flags);
350 static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
356 hexdump("sdio_send", t->buf + TYPE_A_HEADER_SIZE,
357 t->len - TYPE_A_HEADER_SIZE);
359 send_sdio_pkt(func, t->buf, t->len);
361 spin_lock_irqsave(&tx->lock, flags);
363 t->callback(t->cb_data);
365 spin_unlock_irqrestore(&tx->lock, flags);
368 static void do_tx(struct work_struct *work)
370 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
371 struct sdio_func *func = sdev->func;
372 struct tx_cxt *tx = &sdev->tx;
373 struct sdio_tx *t = NULL;
374 struct timeval now, *before;
379 spin_lock_irqsave(&tx->lock, flags);
381 spin_unlock_irqrestore(&tx->lock, flags);
385 if (!list_empty(&tx->hci_list)) {
386 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
389 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
390 do_gettimeofday(&now);
391 before = &tx->sdu_stamp;
393 diff = (now.tv_sec - before->tv_sec) * 1000000 +
394 (now.tv_usec - before->tv_usec);
395 if (diff >= 0 && diff < TX_INTERVAL) {
396 schedule_work(&sdev->ws);
397 spin_unlock_irqrestore(&tx->lock, flags);
403 if (!is_sdu && t == NULL) {
404 spin_unlock_irqrestore(&tx->lock, flags);
410 spin_unlock_irqrestore(&tx->lock, flags);
415 send_hci(func, tx, t);
418 static int gdm_sdio_send(void *priv_dev, void *data, int len,
419 void (*cb)(void *data), void *cb_data)
421 struct sdiowm_dev *sdev = priv_dev;
422 struct tx_cxt *tx = &sdev->tx;
429 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
431 spin_lock_irqsave(&tx->lock, flags);
433 cmd_evt = (pkt[0] << 8) | pkt[1];
434 if (cmd_evt == WIMAX_TX_SDU) {
435 t = get_tx_struct(tx, &no_spc);
437 /* This case must not happen. */
438 spin_unlock_irqrestore(&tx->lock, flags);
441 list_add_tail(&t->list, &tx->sdu_list);
443 memcpy(t->buf, data, len);
447 t->cb_data = cb_data;
449 t = alloc_tx_struct(tx);
451 spin_unlock_irqrestore(&tx->lock, flags);
454 list_add_tail(&t->list, &tx->hci_list);
456 t->buf[0] = len & 0xff;
457 t->buf[1] = (len >> 8) & 0xff;
458 t->buf[2] = (len >> 16) & 0xff;
460 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
462 t->len = len + TYPE_A_HEADER_SIZE;
464 t->cb_data = cb_data;
468 schedule_work(&sdev->ws);
470 spin_unlock_irqrestore(&tx->lock, flags);
479 * Handle the HCI, WIMAX_SDU_TX_FLOW.
481 static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
483 struct tx_cxt *tx = &sdev->tx;
487 spin_lock_irqsave(&tx->lock, flags);
489 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
490 if (cmd_evt != WIMAX_SDU_TX_FLOW)
493 if (hci_data[4] == 0) {
495 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
498 } else if (hci_data[4] == 1) {
500 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
504 schedule_work(&sdev->ws);
506 * If free buffer for sdu tx doesn't exist, then tx queue
507 * should not be woken. For this reason, don't pass the command,
510 if (list_empty(&tx->free_list))
515 spin_unlock_irqrestore(&tx->lock, flags);
519 static void gdm_sdio_irq(struct sdio_func *func)
521 struct phy_dev *phy_dev = sdio_get_drvdata(func);
522 struct sdiowm_dev *sdev = phy_dev->priv_dev;
523 struct tx_cxt *tx = &sdev->tx;
524 struct rx_cxt *rx = &sdev->rx;
527 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
531 /* Check interrupt */
532 val = sdio_readb(func, 0x13, &ret);
534 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
538 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
540 printk(KERN_ERR "Cannot read from function %d\n", func->num);
544 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
545 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
546 printk(KERN_ERR "Too big Type-A size: %d\n", len);
550 if (hdr[3] == 1) { /* Ack */
552 u32 *ack_seq = (u32 *)&hdr[4];
554 spin_lock_irqsave(&tx->lock, flags);
557 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
558 schedule_work(&sdev->ws);
559 spin_unlock_irqrestore(&tx->lock, flags);
561 printk(KERN_DEBUG "Ack... %0x\n", ntohl(*ack_seq));
566 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
567 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
569 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
570 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
574 blocks = remain / func->cur_blksize;
577 n = blocks * func->cur_blksize;
578 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
580 printk(KERN_ERR "Cannot read from function %d\n",
589 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
591 printk(KERN_ERR "Cannot read from function %d\n",
599 hexdump("sdio_receive", rx->rx_buf, len);
601 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
603 spin_lock_irqsave(&rx->lock, flags);
605 if (!list_empty(&rx->req_list)) {
606 r = list_entry(rx->req_list.next, struct sdio_rx, list);
607 spin_unlock_irqrestore(&rx->lock, flags);
609 r->callback(r->cb_data, rx->rx_buf, len);
610 spin_lock_irqsave(&rx->lock, flags);
612 put_rx_struct(rx, r);
615 spin_unlock_irqrestore(&rx->lock, flags);
618 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
619 if (!phy_dev->netdev)
620 register_wimax_device(phy_dev, &func->dev);
623 static int gdm_sdio_receive(void *priv_dev,
624 void (*cb)(void *cb_data, void *data, int len),
627 struct sdiowm_dev *sdev = priv_dev;
628 struct rx_cxt *rx = &sdev->rx;
632 spin_lock_irqsave(&rx->lock, flags);
633 r = get_rx_struct(rx);
635 spin_unlock_irqrestore(&rx->lock, flags);
640 r->cb_data = cb_data;
642 list_add_tail(&r->list, &rx->req_list);
643 spin_unlock_irqrestore(&rx->lock, flags);
648 static int sdio_wimax_probe(struct sdio_func *func,
649 const struct sdio_device_id *id)
652 struct phy_dev *phy_dev = NULL;
653 struct sdiowm_dev *sdev = NULL;
655 printk(KERN_INFO "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
656 func->vendor, func->device);
657 printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
659 sdio_claim_host(func);
660 sdio_enable_func(func);
661 sdio_claim_irq(func, gdm_sdio_irq);
663 ret = sdio_boot(func);
667 phy_dev = kmalloc(sizeof(*phy_dev), GFP_KERNEL);
668 if (phy_dev == NULL) {
672 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
678 memset(phy_dev, 0, sizeof(*phy_dev));
679 memset(sdev, 0, sizeof(*sdev));
681 phy_dev->priv_dev = (void *)sdev;
682 phy_dev->send_func = gdm_sdio_send;
683 phy_dev->rcv_func = gdm_sdio_receive;
685 ret = init_sdio(sdev);
691 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
692 sdio_release_host(func);
694 INIT_WORK(&sdev->ws, do_tx);
696 sdio_set_drvdata(func, phy_dev);
706 static void sdio_wimax_remove(struct sdio_func *func)
708 struct phy_dev *phy_dev = sdio_get_drvdata(func);
709 struct sdiowm_dev *sdev = phy_dev->priv_dev;
712 unregister_wimax_device(phy_dev);
713 sdio_claim_host(func);
714 sdio_release_irq(func);
715 sdio_disable_func(func);
716 sdio_release_host(func);
723 static const struct sdio_device_id sdio_wimax_ids[] = {
724 { SDIO_DEVICE(0x0296, 0x5347) },
728 MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
730 static struct sdio_driver sdio_wimax_driver = {
731 .probe = sdio_wimax_probe,
732 .remove = sdio_wimax_remove,
733 .name = "sdio_wimax",
734 .id_table = sdio_wimax_ids,
737 static int __init sdio_gdm_wimax_init(void)
739 return sdio_register_driver(&sdio_wimax_driver);
742 static void __exit sdio_gdm_wimax_exit(void)
744 sdio_unregister_driver(&sdio_wimax_driver);
747 module_init(sdio_gdm_wimax_init);
748 module_exit(sdio_gdm_wimax_exit);
750 MODULE_VERSION(DRIVER_VERSION);
751 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
752 MODULE_AUTHOR("Ethan Park");
753 MODULE_LICENSE("GPL");