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>
17 #include <linux/version.h>
19 #include <linux/mmc/core.h>
20 #include <linux/mmc/card.h>
21 #include <linux/mmc/sdio_func.h>
22 #include <linux/mmc/sdio_ids.h>
25 #include "gdm_wimax.h"
26 #include "sdio_boot.h"
29 #define TYPE_A_HEADER_SIZE 4
30 #define TYPE_A_LOOKAHEAD_SIZE 16
32 #define MAX_NR_RX_BUF 4
34 #define SDU_TX_BUF_SIZE 2048
35 #define TX_BUF_SIZE 2048
36 #define TX_CHUNK_SIZE (2048 - TYPE_A_HEADER_SIZE)
37 #define RX_BUF_SIZE (25*1024)
40 #define TX_INTERVAL (1000000/TX_HZ)
44 static int init_sdio(struct sdiowm_dev *sdev);
45 static void release_sdio(struct sdiowm_dev *sdev);
48 static void hexdump(char *title, u8 *data, int len)
52 printk(KERN_DEBUG "%s: length = %d\n", title, len);
53 for (i = 0; i < len; i++) {
54 printk(KERN_DEBUG "%02x ", data[i]);
56 printk(KERN_DEBUG "\n");
58 printk(KERN_DEBUG "\n");
62 static struct sdio_tx *alloc_tx_struct(struct tx_cxt *tx)
64 struct sdio_tx *t = NULL;
66 t = kmalloc(sizeof(*t), GFP_ATOMIC);
70 memset(t, 0, sizeof(*t));
72 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
87 static void free_tx_struct(struct sdio_tx *t)
95 static struct sdio_rx *alloc_rx_struct(struct rx_cxt *rx)
97 struct sdio_rx *r = NULL;
99 r = kmalloc(sizeof(*r), GFP_ATOMIC);
103 memset(r, 0, sizeof(*r));
113 static void free_rx_struct(struct sdio_rx *r)
118 /* Before this function is called, spin lock should be locked. */
119 static struct sdio_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
123 if (list_empty(&tx->free_list))
126 t = list_entry(tx->free_list.prev, struct sdio_tx, list);
129 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
134 /* Before this function is called, spin lock should be locked. */
135 static void put_tx_struct(struct tx_cxt *tx, struct sdio_tx *t)
137 list_add_tail(&t->list, &tx->free_list);
140 /* Before this function is called, spin lock should be locked. */
141 static struct sdio_rx *get_rx_struct(struct rx_cxt *rx)
145 if (list_empty(&rx->free_list))
148 r = list_entry(rx->free_list.prev, struct sdio_rx, list);
154 /* Before this function is called, spin lock should be locked. */
155 static void put_rx_struct(struct rx_cxt *rx, struct sdio_rx *r)
157 list_add_tail(&r->list, &rx->free_list);
160 static int init_sdio(struct sdiowm_dev *sdev)
163 struct tx_cxt *tx = &sdev->tx;
164 struct rx_cxt *rx = &sdev->rx;
168 INIT_LIST_HEAD(&tx->free_list);
169 INIT_LIST_HEAD(&tx->sdu_list);
170 INIT_LIST_HEAD(&tx->hci_list);
172 spin_lock_init(&tx->lock);
174 tx->sdu_buf = kmalloc(SDU_TX_BUF_SIZE, GFP_KERNEL);
175 if (tx->sdu_buf == NULL) {
176 printk(KERN_ERR "Failed to allocate SDU tx buffer.\n");
180 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
181 t = alloc_tx_struct(tx);
186 list_add(&t->list, &tx->free_list);
189 INIT_LIST_HEAD(&rx->free_list);
190 INIT_LIST_HEAD(&rx->req_list);
192 spin_lock_init(&rx->lock);
194 for (i = 0; i < MAX_NR_RX_BUF; i++) {
195 r = alloc_rx_struct(rx);
200 list_add(&r->list, &rx->free_list);
203 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
204 if (rx->rx_buf == NULL) {
205 printk(KERN_ERR "Failed to allocate rx buffer.\n");
216 static void release_sdio(struct sdiowm_dev *sdev)
218 struct tx_cxt *tx = &sdev->tx;
219 struct rx_cxt *rx = &sdev->rx;
220 struct sdio_tx *t, *t_next;
221 struct sdio_rx *r, *r_next;
225 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
230 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
235 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
242 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
247 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
253 static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
255 int n, blocks, ret, remain;
257 sdio_claim_host(func);
259 blocks = len / func->cur_blksize;
260 n = blocks * func->cur_blksize;
262 ret = sdio_memcpy_toio(func, 0, data, n);
264 if (ret != -ENOMEDIUM)
265 printk(KERN_ERR "gdmwms: %s error: ret = %d\n",
272 remain = (remain + 3) & ~3;
275 ret = sdio_memcpy_toio(func, 0, data + n, remain);
277 if (ret != -ENOMEDIUM)
278 printk(KERN_ERR "gdmwms: %s error: ret = %d\n",
285 sdio_release_host(func);
288 static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
290 struct list_head *l, *next;
293 int pos, len, i, estlen, aggr_num = 0, aggr_len;
297 spin_lock_irqsave(&tx->lock, flags);
299 pos = TYPE_A_HEADER_SIZE + HCI_HEADER_SIZE;
300 list_for_each_entry(t, &tx->sdu_list, list) {
301 estlen = ((t->len + 3) & ~3) + 4;
302 if ((pos + estlen) > SDU_TX_BUF_SIZE)
306 memcpy(tx->sdu_buf + pos, t->buf, t->len);
307 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
312 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
313 hci->cmd_evt = H2B(WIMAX_TX_SDU_AGGR);
314 hci->length = H2B(aggr_len - TYPE_A_HEADER_SIZE - HCI_HEADER_SIZE);
316 spin_unlock_irqrestore(&tx->lock, flags);
319 hexdump("sdio_send", tx->sdu_buf + TYPE_A_HEADER_SIZE,
320 aggr_len - TYPE_A_HEADER_SIZE);
323 for (pos = TYPE_A_HEADER_SIZE; pos < aggr_len; pos += TX_CHUNK_SIZE) {
324 len = aggr_len - pos;
325 len = len > TX_CHUNK_SIZE ? TX_CHUNK_SIZE : len;
326 buf = tx->sdu_buf + pos - TYPE_A_HEADER_SIZE;
329 buf[1] = (len >> 8) & 0xff;
330 buf[2] = (len >> 16) & 0xff;
331 buf[3] = (pos + len) >= aggr_len ? 0 : 1;
332 send_sdio_pkt(func, buf, len + TYPE_A_HEADER_SIZE);
335 spin_lock_irqsave(&tx->lock, flags);
337 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
339 t = list_entry(l, struct sdio_tx, list);
341 t->callback(t->cb_data);
344 put_tx_struct(t->tx_cxt, t);
347 do_gettimeofday(&tx->sdu_stamp);
348 spin_unlock_irqrestore(&tx->lock, flags);
351 static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
357 hexdump("sdio_send", t->buf + TYPE_A_HEADER_SIZE,
358 t->len - TYPE_A_HEADER_SIZE);
360 send_sdio_pkt(func, t->buf, t->len);
362 spin_lock_irqsave(&tx->lock, flags);
364 t->callback(t->cb_data);
366 spin_unlock_irqrestore(&tx->lock, flags);
369 static void do_tx(struct work_struct *work)
371 struct sdiowm_dev *sdev = container_of(work, struct sdiowm_dev, ws);
372 struct sdio_func *func = sdev->func;
373 struct tx_cxt *tx = &sdev->tx;
374 struct sdio_tx *t = NULL;
375 struct timeval now, *before;
380 spin_lock_irqsave(&tx->lock, flags);
382 spin_unlock_irqrestore(&tx->lock, flags);
386 if (!list_empty(&tx->hci_list)) {
387 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
390 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
391 do_gettimeofday(&now);
392 before = &tx->sdu_stamp;
394 diff = (now.tv_sec - before->tv_sec) * 1000000 +
395 (now.tv_usec - before->tv_usec);
396 if (diff >= 0 && diff < TX_INTERVAL) {
397 schedule_work(&sdev->ws);
398 spin_unlock_irqrestore(&tx->lock, flags);
404 if (!is_sdu && t == NULL) {
405 spin_unlock_irqrestore(&tx->lock, flags);
411 spin_unlock_irqrestore(&tx->lock, flags);
416 send_hci(func, tx, t);
419 static int gdm_sdio_send(void *priv_dev, void *data, int len,
420 void (*cb)(void *data), void *cb_data)
422 struct sdiowm_dev *sdev = priv_dev;
423 struct tx_cxt *tx = &sdev->tx;
430 BUG_ON(len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE);
432 spin_lock_irqsave(&tx->lock, flags);
434 cmd_evt = (pkt[0] << 8) | pkt[1];
435 if (cmd_evt == WIMAX_TX_SDU) {
436 t = get_tx_struct(tx, &no_spc);
438 /* This case must not happen. */
439 spin_unlock_irqrestore(&tx->lock, flags);
442 list_add_tail(&t->list, &tx->sdu_list);
444 memcpy(t->buf, data, len);
448 t->cb_data = cb_data;
450 t = alloc_tx_struct(tx);
452 spin_unlock_irqrestore(&tx->lock, flags);
455 list_add_tail(&t->list, &tx->hci_list);
457 t->buf[0] = len & 0xff;
458 t->buf[1] = (len >> 8) & 0xff;
459 t->buf[2] = (len >> 16) & 0xff;
461 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
463 t->len = len + TYPE_A_HEADER_SIZE;
465 t->cb_data = cb_data;
469 schedule_work(&sdev->ws);
471 spin_unlock_irqrestore(&tx->lock, flags);
480 * Handle the HCI, WIMAX_SDU_TX_FLOW.
482 static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
484 struct tx_cxt *tx = &sdev->tx;
488 spin_lock_irqsave(&tx->lock, flags);
490 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
491 if (cmd_evt != WIMAX_SDU_TX_FLOW)
494 if (hci_data[4] == 0) {
496 printk(KERN_DEBUG "WIMAX ==> STOP SDU TX\n");
499 } else if (hci_data[4] == 1) {
501 printk(KERN_DEBUG "WIMAX ==> START SDU TX\n");
505 schedule_work(&sdev->ws);
507 * If free buffer for sdu tx doesn't exist, then tx queue
508 * should not be woken. For this reason, don't pass the command,
511 if (list_empty(&tx->free_list))
516 spin_unlock_irqrestore(&tx->lock, flags);
520 static void gdm_sdio_irq(struct sdio_func *func)
522 struct phy_dev *phy_dev = sdio_get_drvdata(func);
523 struct sdiowm_dev *sdev = phy_dev->priv_dev;
524 struct tx_cxt *tx = &sdev->tx;
525 struct rx_cxt *rx = &sdev->rx;
528 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
532 /* Check interrupt */
533 val = sdio_readb(func, 0x13, &ret);
535 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
539 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
541 printk(KERN_ERR "Cannot read from function %d\n", func->num);
545 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
546 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
547 printk(KERN_ERR "Too big Type-A size: %d\n", len);
551 if (hdr[3] == 1) { /* Ack */
553 u32 *ack_seq = (u32 *)&hdr[4];
555 spin_lock_irqsave(&tx->lock, flags);
558 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
559 schedule_work(&sdev->ws);
560 spin_unlock_irqrestore(&tx->lock, flags);
562 printk(KERN_DEBUG "Ack... %0x\n", ntohl(*ack_seq));
567 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
568 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
570 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
571 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
575 blocks = remain / func->cur_blksize;
578 n = blocks * func->cur_blksize;
579 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
581 printk(KERN_ERR "Cannot read from function %d\n",
590 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
592 printk(KERN_ERR "Cannot read from function %d\n",
600 hexdump("sdio_receive", rx->rx_buf, len);
602 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
604 spin_lock_irqsave(&rx->lock, flags);
606 if (!list_empty(&rx->req_list)) {
607 r = list_entry(rx->req_list.next, struct sdio_rx, list);
608 spin_unlock_irqrestore(&rx->lock, flags);
610 r->callback(r->cb_data, rx->rx_buf, len);
611 spin_lock_irqsave(&rx->lock, flags);
613 put_rx_struct(rx, r);
616 spin_unlock_irqrestore(&rx->lock, flags);
619 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
620 if (!phy_dev->netdev)
621 register_wimax_device(phy_dev, &func->dev);
624 static int gdm_sdio_receive(void *priv_dev,
625 void (*cb)(void *cb_data, void *data, int len),
628 struct sdiowm_dev *sdev = priv_dev;
629 struct rx_cxt *rx = &sdev->rx;
633 spin_lock_irqsave(&rx->lock, flags);
634 r = get_rx_struct(rx);
636 spin_unlock_irqrestore(&rx->lock, flags);
641 r->cb_data = cb_data;
643 list_add_tail(&r->list, &rx->req_list);
644 spin_unlock_irqrestore(&rx->lock, flags);
649 static int sdio_wimax_probe(struct sdio_func *func,
650 const struct sdio_device_id *id)
653 struct phy_dev *phy_dev = NULL;
654 struct sdiowm_dev *sdev = NULL;
656 printk(KERN_INFO "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
657 func->vendor, func->device);
658 printk(KERN_INFO "GCT WiMax driver version %s\n", DRIVER_VERSION);
660 sdio_claim_host(func);
661 sdio_enable_func(func);
662 sdio_claim_irq(func, gdm_sdio_irq);
664 ret = sdio_boot(func);
668 phy_dev = kmalloc(sizeof(*phy_dev), GFP_KERNEL);
669 if (phy_dev == NULL) {
673 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
679 memset(phy_dev, 0, sizeof(*phy_dev));
680 memset(sdev, 0, sizeof(*sdev));
682 phy_dev->priv_dev = (void *)sdev;
683 phy_dev->send_func = gdm_sdio_send;
684 phy_dev->rcv_func = gdm_sdio_receive;
686 ret = init_sdio(sdev);
692 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
693 sdio_release_host(func);
695 INIT_WORK(&sdev->ws, do_tx);
697 sdio_set_drvdata(func, phy_dev);
707 static void sdio_wimax_remove(struct sdio_func *func)
709 struct phy_dev *phy_dev = sdio_get_drvdata(func);
710 struct sdiowm_dev *sdev = phy_dev->priv_dev;
713 unregister_wimax_device(phy_dev);
714 sdio_claim_host(func);
715 sdio_release_irq(func);
716 sdio_disable_func(func);
717 sdio_release_host(func);
724 static const struct sdio_device_id sdio_wimax_ids[] = {
725 { SDIO_DEVICE(0x0296, 0x5347) },
729 MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
731 static struct sdio_driver sdio_wimax_driver = {
732 .probe = sdio_wimax_probe,
733 .remove = sdio_wimax_remove,
734 .name = "sdio_wimax",
735 .id_table = sdio_wimax_ids,
738 static int __init sdio_gdm_wimax_init(void)
740 return sdio_register_driver(&sdio_wimax_driver);
743 static void __exit sdio_gdm_wimax_exit(void)
745 sdio_unregister_driver(&sdio_wimax_driver);
748 module_init(sdio_gdm_wimax_init);
749 module_exit(sdio_gdm_wimax_exit);
751 MODULE_VERSION(DRIVER_VERSION);
752 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
753 MODULE_AUTHOR("Ethan Park");
754 MODULE_LICENSE("GPL");