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)
41 static struct sdio_tx
*alloc_tx_struct(struct tx_cxt
*tx
)
43 struct sdio_tx
*t
= kzalloc(sizeof(*t
), GFP_ATOMIC
);
48 t
->buf
= kmalloc(TX_BUF_SIZE
, GFP_ATOMIC
);
59 static void free_tx_struct(struct sdio_tx
*t
)
67 static struct sdio_rx
*alloc_rx_struct(struct rx_cxt
*rx
)
69 struct sdio_rx
*r
= kzalloc(sizeof(*r
), GFP_ATOMIC
);
77 static void free_rx_struct(struct sdio_rx
*r
)
82 /* Before this function is called, spin lock should be locked. */
83 static struct sdio_tx
*get_tx_struct(struct tx_cxt
*tx
, int *no_spc
)
87 if (list_empty(&tx
->free_list
))
90 t
= list_entry(tx
->free_list
.prev
, struct sdio_tx
, list
);
93 *no_spc
= list_empty(&tx
->free_list
) ? 1 : 0;
98 /* Before this function is called, spin lock should be locked. */
99 static void put_tx_struct(struct tx_cxt
*tx
, struct sdio_tx
*t
)
101 list_add_tail(&t
->list
, &tx
->free_list
);
104 /* Before this function is called, spin lock should be locked. */
105 static struct sdio_rx
*get_rx_struct(struct rx_cxt
*rx
)
109 if (list_empty(&rx
->free_list
))
112 r
= list_entry(rx
->free_list
.prev
, struct sdio_rx
, list
);
118 /* Before this function is called, spin lock should be locked. */
119 static void put_rx_struct(struct rx_cxt
*rx
, struct sdio_rx
*r
)
121 list_add_tail(&r
->list
, &rx
->free_list
);
124 static void release_sdio(struct sdiowm_dev
*sdev
)
126 struct tx_cxt
*tx
= &sdev
->tx
;
127 struct rx_cxt
*rx
= &sdev
->rx
;
128 struct sdio_tx
*t
, *t_next
;
129 struct sdio_rx
*r
, *r_next
;
133 list_for_each_entry_safe(t
, t_next
, &tx
->free_list
, list
) {
138 list_for_each_entry_safe(t
, t_next
, &tx
->sdu_list
, list
) {
143 list_for_each_entry_safe(t
, t_next
, &tx
->hci_list
, list
) {
150 list_for_each_entry_safe(r
, r_next
, &rx
->free_list
, list
) {
155 list_for_each_entry_safe(r
, r_next
, &rx
->req_list
, list
) {
161 static int init_sdio(struct sdiowm_dev
*sdev
)
164 struct tx_cxt
*tx
= &sdev
->tx
;
165 struct rx_cxt
*rx
= &sdev
->rx
;
169 INIT_LIST_HEAD(&tx
->free_list
);
170 INIT_LIST_HEAD(&tx
->sdu_list
);
171 INIT_LIST_HEAD(&tx
->hci_list
);
173 spin_lock_init(&tx
->lock
);
175 tx
->sdu_buf
= kmalloc(SDU_TX_BUF_SIZE
, GFP_KERNEL
);
176 if (tx
->sdu_buf
== NULL
)
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
)
213 static void send_sdio_pkt(struct sdio_func
*func
, u8
*data
, int len
)
215 int n
, blocks
, ret
, remain
;
217 sdio_claim_host(func
);
219 blocks
= len
/ func
->cur_blksize
;
220 n
= blocks
* func
->cur_blksize
;
222 ret
= sdio_memcpy_toio(func
, 0, data
, n
);
224 if (ret
!= -ENOMEDIUM
)
226 "gdmwms: %s error: ret = %d\n",
233 remain
= (remain
+ 3) & ~3;
236 ret
= sdio_memcpy_toio(func
, 0, data
+ n
, remain
);
238 if (ret
!= -ENOMEDIUM
)
240 "gdmwms: %s error: ret = %d\n",
247 sdio_release_host(func
);
250 static void send_sdu(struct sdio_func
*func
, struct tx_cxt
*tx
)
252 struct list_head
*l
, *next
;
255 int pos
, len
, i
, estlen
, aggr_num
= 0, aggr_len
;
259 spin_lock_irqsave(&tx
->lock
, flags
);
261 pos
= TYPE_A_HEADER_SIZE
+ HCI_HEADER_SIZE
;
262 list_for_each_entry(t
, &tx
->sdu_list
, list
) {
263 estlen
= ((t
->len
+ 3) & ~3) + 4;
264 if ((pos
+ estlen
) > SDU_TX_BUF_SIZE
)
268 memcpy(tx
->sdu_buf
+ pos
, t
->buf
, t
->len
);
269 memset(tx
->sdu_buf
+ pos
+ t
->len
, 0, estlen
- t
->len
);
274 hci
= (struct hci_s
*)(tx
->sdu_buf
+ TYPE_A_HEADER_SIZE
);
275 hci
->cmd_evt
= cpu_to_be16(WIMAX_TX_SDU_AGGR
);
276 hci
->length
= cpu_to_be16(aggr_len
- TYPE_A_HEADER_SIZE
-
279 spin_unlock_irqrestore(&tx
->lock
, flags
);
281 dev_dbg(&func
->dev
, "sdio_send: %*ph\n", aggr_len
- TYPE_A_HEADER_SIZE
,
282 tx
->sdu_buf
+ TYPE_A_HEADER_SIZE
);
284 for (pos
= TYPE_A_HEADER_SIZE
; pos
< aggr_len
; pos
+= TX_CHUNK_SIZE
) {
285 len
= aggr_len
- pos
;
286 len
= len
> TX_CHUNK_SIZE
? TX_CHUNK_SIZE
: len
;
287 buf
= tx
->sdu_buf
+ pos
- TYPE_A_HEADER_SIZE
;
290 buf
[1] = (len
>> 8) & 0xff;
291 buf
[2] = (len
>> 16) & 0xff;
292 buf
[3] = (pos
+ len
) >= aggr_len
? 0 : 1;
293 send_sdio_pkt(func
, buf
, len
+ TYPE_A_HEADER_SIZE
);
296 spin_lock_irqsave(&tx
->lock
, flags
);
298 for (l
= tx
->sdu_list
.next
, i
= 0; i
< aggr_num
; i
++, l
= next
) {
300 t
= list_entry(l
, struct sdio_tx
, list
);
302 t
->callback(t
->cb_data
);
305 put_tx_struct(t
->tx_cxt
, t
);
308 do_gettimeofday(&tx
->sdu_stamp
);
309 spin_unlock_irqrestore(&tx
->lock
, flags
);
312 static void send_hci(struct sdio_func
*func
, struct tx_cxt
*tx
,
317 dev_dbg(&func
->dev
, "sdio_send: %*ph\n", t
->len
- TYPE_A_HEADER_SIZE
,
318 t
->buf
+ TYPE_A_HEADER_SIZE
);
320 send_sdio_pkt(func
, t
->buf
, t
->len
);
322 spin_lock_irqsave(&tx
->lock
, flags
);
324 t
->callback(t
->cb_data
);
326 spin_unlock_irqrestore(&tx
->lock
, flags
);
329 static void do_tx(struct work_struct
*work
)
331 struct sdiowm_dev
*sdev
= container_of(work
, struct sdiowm_dev
, ws
);
332 struct sdio_func
*func
= sdev
->func
;
333 struct tx_cxt
*tx
= &sdev
->tx
;
334 struct sdio_tx
*t
= NULL
;
335 struct timeval now
, *before
;
340 spin_lock_irqsave(&tx
->lock
, flags
);
342 spin_unlock_irqrestore(&tx
->lock
, flags
);
346 if (!list_empty(&tx
->hci_list
)) {
347 t
= list_entry(tx
->hci_list
.next
, struct sdio_tx
, list
);
350 } else if (!tx
->stop_sdu_tx
&& !list_empty(&tx
->sdu_list
)) {
351 do_gettimeofday(&now
);
352 before
= &tx
->sdu_stamp
;
354 diff
= (now
.tv_sec
- before
->tv_sec
) * 1000000 +
355 (now
.tv_usec
- before
->tv_usec
);
356 if (diff
>= 0 && diff
< TX_INTERVAL
) {
357 schedule_work(&sdev
->ws
);
358 spin_unlock_irqrestore(&tx
->lock
, flags
);
364 if (!is_sdu
&& t
== NULL
) {
365 spin_unlock_irqrestore(&tx
->lock
, flags
);
371 spin_unlock_irqrestore(&tx
->lock
, flags
);
376 send_hci(func
, tx
, t
);
379 static int gdm_sdio_send(void *priv_dev
, void *data
, int len
,
380 void (*cb
)(void *data
), void *cb_data
)
382 struct sdiowm_dev
*sdev
= priv_dev
;
383 struct tx_cxt
*tx
= &sdev
->tx
;
390 if (len
> TX_BUF_SIZE
- TYPE_A_HEADER_SIZE
)
393 spin_lock_irqsave(&tx
->lock
, flags
);
395 cmd_evt
= (pkt
[0] << 8) | pkt
[1];
396 if (cmd_evt
== WIMAX_TX_SDU
) {
397 t
= get_tx_struct(tx
, &no_spc
);
399 /* This case must not happen. */
400 spin_unlock_irqrestore(&tx
->lock
, flags
);
403 list_add_tail(&t
->list
, &tx
->sdu_list
);
405 memcpy(t
->buf
, data
, len
);
409 t
->cb_data
= cb_data
;
411 t
= alloc_tx_struct(tx
);
413 spin_unlock_irqrestore(&tx
->lock
, flags
);
416 list_add_tail(&t
->list
, &tx
->hci_list
);
418 t
->buf
[0] = len
& 0xff;
419 t
->buf
[1] = (len
>> 8) & 0xff;
420 t
->buf
[2] = (len
>> 16) & 0xff;
422 memcpy(t
->buf
+ TYPE_A_HEADER_SIZE
, data
, len
);
424 t
->len
= len
+ TYPE_A_HEADER_SIZE
;
426 t
->cb_data
= cb_data
;
430 schedule_work(&sdev
->ws
);
432 spin_unlock_irqrestore(&tx
->lock
, flags
);
440 /* Handle the HCI, WIMAX_SDU_TX_FLOW. */
441 static int control_sdu_tx_flow(struct sdiowm_dev
*sdev
, u8
*hci_data
, int len
)
443 struct tx_cxt
*tx
= &sdev
->tx
;
447 spin_lock_irqsave(&tx
->lock
, flags
);
449 cmd_evt
= (hci_data
[0] << 8) | (hci_data
[1]);
450 if (cmd_evt
!= WIMAX_SDU_TX_FLOW
)
453 if (hci_data
[4] == 0) {
454 dev_dbg(&sdev
->func
->dev
, "WIMAX ==> STOP SDU TX\n");
456 } else if (hci_data
[4] == 1) {
457 dev_dbg(&sdev
->func
->dev
, "WIMAX ==> START SDU TX\n");
460 schedule_work(&sdev
->ws
);
461 /* If free buffer for sdu tx doesn't exist, then tx queue
462 * should not be woken. For this reason, don't pass the command,
465 if (list_empty(&tx
->free_list
))
470 spin_unlock_irqrestore(&tx
->lock
, flags
);
474 static void gdm_sdio_irq(struct sdio_func
*func
)
476 struct phy_dev
*phy_dev
= sdio_get_drvdata(func
);
477 struct sdiowm_dev
*sdev
= phy_dev
->priv_dev
;
478 struct tx_cxt
*tx
= &sdev
->tx
;
479 struct rx_cxt
*rx
= &sdev
->rx
;
482 u8 val
, hdr
[TYPE_A_LOOKAHEAD_SIZE
], *buf
;
486 /* Check interrupt */
487 val
= sdio_readb(func
, 0x13, &ret
);
489 sdio_writeb(func
, 0x01, 0x13, &ret
); /* clear interrupt */
493 ret
= sdio_memcpy_fromio(func
, hdr
, 0x0, TYPE_A_LOOKAHEAD_SIZE
);
496 "Cannot read from function %d\n", func
->num
);
500 len
= (hdr
[2] << 16) | (hdr
[1] << 8) | hdr
[0];
501 if (len
> (RX_BUF_SIZE
- TYPE_A_HEADER_SIZE
)) {
502 dev_err(&func
->dev
, "Too big Type-A size: %d\n", len
);
506 if (hdr
[3] == 1) { /* Ack */
507 u32
*ack_seq
= (u32
*)&hdr
[4];
509 spin_lock_irqsave(&tx
->lock
, flags
);
512 if (!list_empty(&tx
->sdu_list
) || !list_empty(&tx
->hci_list
))
513 schedule_work(&sdev
->ws
);
514 spin_unlock_irqrestore(&tx
->lock
, flags
);
515 dev_dbg(&func
->dev
, "Ack... %0x\n", ntohl(*ack_seq
));
519 memcpy(rx
->rx_buf
, hdr
+ TYPE_A_HEADER_SIZE
,
520 TYPE_A_LOOKAHEAD_SIZE
- TYPE_A_HEADER_SIZE
);
522 buf
= rx
->rx_buf
+ TYPE_A_LOOKAHEAD_SIZE
- TYPE_A_HEADER_SIZE
;
523 remain
= len
- TYPE_A_LOOKAHEAD_SIZE
+ TYPE_A_HEADER_SIZE
;
527 blocks
= remain
/ func
->cur_blksize
;
530 n
= blocks
* func
->cur_blksize
;
531 ret
= sdio_memcpy_fromio(func
, buf
, 0x0, n
);
534 "Cannot read from function %d\n", func
->num
);
542 ret
= sdio_memcpy_fromio(func
, buf
, 0x0, remain
);
545 "Cannot read from function %d\n", func
->num
);
551 dev_dbg(&func
->dev
, "sdio_receive: %*ph\n", len
, rx
->rx_buf
);
553 len
= control_sdu_tx_flow(sdev
, rx
->rx_buf
, len
);
555 spin_lock_irqsave(&rx
->lock
, flags
);
557 if (!list_empty(&rx
->req_list
)) {
558 r
= list_entry(rx
->req_list
.next
, struct sdio_rx
, list
);
559 spin_unlock_irqrestore(&rx
->lock
, flags
);
561 r
->callback(r
->cb_data
, rx
->rx_buf
, len
);
562 spin_lock_irqsave(&rx
->lock
, flags
);
564 put_rx_struct(rx
, r
);
567 spin_unlock_irqrestore(&rx
->lock
, flags
);
570 sdio_writeb(func
, 0x00, 0x10, &ret
); /* PCRRT */
571 if (!phy_dev
->netdev
)
572 register_wimax_device(phy_dev
, &func
->dev
);
575 static int gdm_sdio_receive(void *priv_dev
,
576 void (*cb
)(void *cb_data
, void *data
, int len
),
579 struct sdiowm_dev
*sdev
= priv_dev
;
580 struct rx_cxt
*rx
= &sdev
->rx
;
584 spin_lock_irqsave(&rx
->lock
, flags
);
585 r
= get_rx_struct(rx
);
587 spin_unlock_irqrestore(&rx
->lock
, flags
);
592 r
->cb_data
= cb_data
;
594 list_add_tail(&r
->list
, &rx
->req_list
);
595 spin_unlock_irqrestore(&rx
->lock
, flags
);
600 static int sdio_wimax_probe(struct sdio_func
*func
,
601 const struct sdio_device_id
*id
)
604 struct phy_dev
*phy_dev
= NULL
;
605 struct sdiowm_dev
*sdev
= NULL
;
607 dev_info(&func
->dev
, "Found GDM SDIO VID = 0x%04x PID = 0x%04x...\n",
608 func
->vendor
, func
->device
);
609 dev_info(&func
->dev
, "GCT WiMax driver version %s\n", DRIVER_VERSION
);
611 sdio_claim_host(func
);
612 sdio_enable_func(func
);
613 sdio_claim_irq(func
, gdm_sdio_irq
);
615 ret
= sdio_boot(func
);
619 phy_dev
= kzalloc(sizeof(*phy_dev
), GFP_KERNEL
);
620 if (phy_dev
== NULL
) {
624 sdev
= kzalloc(sizeof(*sdev
), GFP_KERNEL
);
630 phy_dev
->priv_dev
= (void *)sdev
;
631 phy_dev
->send_func
= gdm_sdio_send
;
632 phy_dev
->rcv_func
= gdm_sdio_receive
;
634 ret
= init_sdio(sdev
);
640 sdio_writeb(func
, 1, 0x14, &ret
); /* Enable interrupt */
641 sdio_release_host(func
);
643 INIT_WORK(&sdev
->ws
, do_tx
);
645 sdio_set_drvdata(func
, phy_dev
);
655 static void sdio_wimax_remove(struct sdio_func
*func
)
657 struct phy_dev
*phy_dev
= sdio_get_drvdata(func
);
658 struct sdiowm_dev
*sdev
= phy_dev
->priv_dev
;
660 cancel_work_sync(&sdev
->ws
);
662 unregister_wimax_device(phy_dev
);
663 sdio_claim_host(func
);
664 sdio_release_irq(func
);
665 sdio_disable_func(func
);
666 sdio_release_host(func
);
673 static const struct sdio_device_id sdio_wimax_ids
[] = {
674 { SDIO_DEVICE(0x0296, 0x5347) },
678 MODULE_DEVICE_TABLE(sdio
, sdio_wimax_ids
);
680 static struct sdio_driver sdio_wimax_driver
= {
681 .probe
= sdio_wimax_probe
,
682 .remove
= sdio_wimax_remove
,
683 .name
= "sdio_wimax",
684 .id_table
= sdio_wimax_ids
,
687 static int __init
sdio_gdm_wimax_init(void)
689 return sdio_register_driver(&sdio_wimax_driver
);
692 static void __exit
sdio_gdm_wimax_exit(void)
694 sdio_unregister_driver(&sdio_wimax_driver
);
697 module_init(sdio_gdm_wimax_init
);
698 module_exit(sdio_gdm_wimax_exit
);
700 MODULE_VERSION(DRIVER_VERSION
);
701 MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
702 MODULE_AUTHOR("Ethan Park");
703 MODULE_LICENSE("GPL");