blob: 7a0a0f2214180dc2da684e3df31f6ba480bbc346 [file] [log] [blame]
Sage Ahn247e9cf2012-05-15 13:20:36 +09001/*
2 * Copyright (c) 2012 GCT Semiconductor, Inc. All rights reserved.
3 *
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.
7 *
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.
12 */
13
14#include <linux/module.h>
15#include <linux/init.h>
16#include <linux/kernel.h>
Sage Ahn247e9cf2012-05-15 13:20:36 +090017
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
Michalis Pappasac1a3bf2014-05-09 18:08:28 +080034#define TX_BUF_SIZE 2048
Sage Ahn247e9cf2012-05-15 13:20:36 +090035#define TX_CHUNK_SIZE (2048 - TYPE_A_HEADER_SIZE)
Michalis Pappasac1a3bf2014-05-09 18:08:28 +080036#define RX_BUF_SIZE (25*1024)
Sage Ahn247e9cf2012-05-15 13:20:36 +090037
Michalis Pappasac1a3bf2014-05-09 18:08:28 +080038#define TX_HZ 2000
Sage Ahn247e9cf2012-05-15 13:20:36 +090039#define TX_INTERVAL (1000000/TX_HZ)
40
Sage Ahn247e9cf2012-05-15 13:20:36 +090041static struct sdio_tx *alloc_tx_struct(struct tx_cxt *tx)
42{
Ben Chan129575f2012-09-12 11:43:41 -070043 struct sdio_tx *t = kzalloc(sizeof(*t), GFP_ATOMIC);
Sage Ahn247e9cf2012-05-15 13:20:36 +090044
Ben Chan129575f2012-09-12 11:43:41 -070045 if (!t)
46 return NULL;
Sage Ahn247e9cf2012-05-15 13:20:36 +090047
Sage Ahn247e9cf2012-05-15 13:20:36 +090048 t->buf = kmalloc(TX_BUF_SIZE, GFP_ATOMIC);
Ben Chan129575f2012-09-12 11:43:41 -070049 if (!t->buf) {
50 kfree(t);
51 return NULL;
52 }
Sage Ahn247e9cf2012-05-15 13:20:36 +090053
54 t->tx_cxt = tx;
55
56 return t;
Sage Ahn247e9cf2012-05-15 13:20:36 +090057}
58
59static void free_tx_struct(struct sdio_tx *t)
60{
61 if (t) {
62 kfree(t->buf);
63 kfree(t);
64 }
65}
66
67static struct sdio_rx *alloc_rx_struct(struct rx_cxt *rx)
68{
Ben Chan129575f2012-09-12 11:43:41 -070069 struct sdio_rx *r = kzalloc(sizeof(*r), GFP_ATOMIC);
Sage Ahn247e9cf2012-05-15 13:20:36 +090070
Ben Chan129575f2012-09-12 11:43:41 -070071 if (r)
72 r->rx_cxt = rx;
Sage Ahn247e9cf2012-05-15 13:20:36 +090073
74 return r;
Sage Ahn247e9cf2012-05-15 13:20:36 +090075}
76
77static void free_rx_struct(struct sdio_rx *r)
78{
79 kfree(r);
80}
81
82/* Before this function is called, spin lock should be locked. */
83static struct sdio_tx *get_tx_struct(struct tx_cxt *tx, int *no_spc)
84{
85 struct sdio_tx *t;
86
87 if (list_empty(&tx->free_list))
88 return NULL;
89
90 t = list_entry(tx->free_list.prev, struct sdio_tx, list);
91 list_del(&t->list);
92
93 *no_spc = list_empty(&tx->free_list) ? 1 : 0;
94
95 return t;
96}
97
98/* Before this function is called, spin lock should be locked. */
99static void put_tx_struct(struct tx_cxt *tx, struct sdio_tx *t)
100{
101 list_add_tail(&t->list, &tx->free_list);
102}
103
104/* Before this function is called, spin lock should be locked. */
105static struct sdio_rx *get_rx_struct(struct rx_cxt *rx)
106{
107 struct sdio_rx *r;
108
109 if (list_empty(&rx->free_list))
110 return NULL;
111
112 r = list_entry(rx->free_list.prev, struct sdio_rx, list);
113 list_del(&r->list);
114
115 return r;
116}
117
118/* Before this function is called, spin lock should be locked. */
119static void put_rx_struct(struct rx_cxt *rx, struct sdio_rx *r)
120{
121 list_add_tail(&r->list, &rx->free_list);
122}
123
Michalis Pappas1aa8ae72014-07-09 20:44:18 +0100124static void release_sdio(struct sdiowm_dev *sdev)
125{
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;
130
131 kfree(tx->sdu_buf);
132
133 list_for_each_entry_safe(t, t_next, &tx->free_list, list) {
134 list_del(&t->list);
135 free_tx_struct(t);
136 }
137
138 list_for_each_entry_safe(t, t_next, &tx->sdu_list, list) {
139 list_del(&t->list);
140 free_tx_struct(t);
141 }
142
143 list_for_each_entry_safe(t, t_next, &tx->hci_list, list) {
144 list_del(&t->list);
145 free_tx_struct(t);
146 }
147
148 kfree(rx->rx_buf);
149
150 list_for_each_entry_safe(r, r_next, &rx->free_list, list) {
151 list_del(&r->list);
152 free_rx_struct(r);
153 }
154
155 list_for_each_entry_safe(r, r_next, &rx->req_list, list) {
156 list_del(&r->list);
157 free_rx_struct(r);
158 }
159}
160
Sage Ahn247e9cf2012-05-15 13:20:36 +0900161static int init_sdio(struct sdiowm_dev *sdev)
162{
163 int ret = 0, i;
Michalis Pappasac1a3bf2014-05-09 18:08:28 +0800164 struct tx_cxt *tx = &sdev->tx;
165 struct rx_cxt *rx = &sdev->rx;
166 struct sdio_tx *t;
167 struct sdio_rx *r;
Sage Ahn247e9cf2012-05-15 13:20:36 +0900168
169 INIT_LIST_HEAD(&tx->free_list);
170 INIT_LIST_HEAD(&tx->sdu_list);
171 INIT_LIST_HEAD(&tx->hci_list);
172
173 spin_lock_init(&tx->lock);
174
175 tx->sdu_buf = kmalloc(SDU_TX_BUF_SIZE, GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800176 if (tx->sdu_buf == NULL)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900177 goto fail;
Sage Ahn247e9cf2012-05-15 13:20:36 +0900178
179 for (i = 0; i < MAX_NR_SDU_BUF; i++) {
180 t = alloc_tx_struct(tx);
181 if (t == NULL) {
182 ret = -ENOMEM;
183 goto fail;
184 }
185 list_add(&t->list, &tx->free_list);
186 }
187
188 INIT_LIST_HEAD(&rx->free_list);
189 INIT_LIST_HEAD(&rx->req_list);
190
191 spin_lock_init(&rx->lock);
192
193 for (i = 0; i < MAX_NR_RX_BUF; i++) {
194 r = alloc_rx_struct(rx);
195 if (r == NULL) {
196 ret = -ENOMEM;
197 goto fail;
198 }
199 list_add(&r->list, &rx->free_list);
200 }
201
202 rx->rx_buf = kmalloc(RX_BUF_SIZE, GFP_KERNEL);
Joe Perches78110bb2013-02-11 09:41:29 -0800203 if (rx->rx_buf == NULL)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900204 goto fail;
Sage Ahn247e9cf2012-05-15 13:20:36 +0900205
206 return 0;
207
208fail:
209 release_sdio(sdev);
210 return ret;
211}
212
Sage Ahn247e9cf2012-05-15 13:20:36 +0900213static void send_sdio_pkt(struct sdio_func *func, u8 *data, int len)
214{
215 int n, blocks, ret, remain;
216
217 sdio_claim_host(func);
218
219 blocks = len / func->cur_blksize;
220 n = blocks * func->cur_blksize;
221 if (blocks) {
222 ret = sdio_memcpy_toio(func, 0, data, n);
223 if (ret < 0) {
224 if (ret != -ENOMEDIUM)
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900225 dev_err(&func->dev,
226 "gdmwms: %s error: ret = %d\n",
Sage Ahn247e9cf2012-05-15 13:20:36 +0900227 __func__, ret);
228 goto end_io;
229 }
230 }
231
232 remain = len - n;
233 remain = (remain + 3) & ~3;
234
235 if (remain) {
236 ret = sdio_memcpy_toio(func, 0, data + n, remain);
237 if (ret < 0) {
238 if (ret != -ENOMEDIUM)
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900239 dev_err(&func->dev,
240 "gdmwms: %s error: ret = %d\n",
Sage Ahn247e9cf2012-05-15 13:20:36 +0900241 __func__, ret);
242 goto end_io;
243 }
244 }
245
246end_io:
247 sdio_release_host(func);
248}
249
250static void send_sdu(struct sdio_func *func, struct tx_cxt *tx)
251{
252 struct list_head *l, *next;
253 struct hci_s *hci;
254 struct sdio_tx *t;
255 int pos, len, i, estlen, aggr_num = 0, aggr_len;
256 u8 *buf;
257 unsigned long flags;
258
259 spin_lock_irqsave(&tx->lock, flags);
260
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)
265 break;
266
267 aggr_num++;
268 memcpy(tx->sdu_buf + pos, t->buf, t->len);
269 memset(tx->sdu_buf + pos + t->len, 0, estlen - t->len);
270 pos += estlen;
271 }
272 aggr_len = pos;
273
274 hci = (struct hci_s *)(tx->sdu_buf + TYPE_A_HEADER_SIZE);
Ben Chana8a175d2014-06-30 23:32:27 -0700275 hci->cmd_evt = cpu_to_be16(WIMAX_TX_SDU_AGGR);
276 hci->length = cpu_to_be16(aggr_len - TYPE_A_HEADER_SIZE -
277 HCI_HEADER_SIZE);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900278
279 spin_unlock_irqrestore(&tx->lock, flags);
280
Greg Kroah-Hartman75533a02014-07-17 18:33:02 -0700281 dev_dbg(&func->dev, "sdio_send: %*ph\n", aggr_len - TYPE_A_HEADER_SIZE,
Michalis Pappasf5439c62014-07-17 17:09:39 +0100282 tx->sdu_buf + TYPE_A_HEADER_SIZE);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900283
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;
288
289 buf[0] = len & 0xff;
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);
294 }
295
296 spin_lock_irqsave(&tx->lock, flags);
297
298 for (l = tx->sdu_list.next, i = 0; i < aggr_num; i++, l = next) {
299 next = l->next;
300 t = list_entry(l, struct sdio_tx, list);
301 if (t->callback)
302 t->callback(t->cb_data);
303
304 list_del(l);
305 put_tx_struct(t->tx_cxt, t);
306 }
307
308 do_gettimeofday(&tx->sdu_stamp);
309 spin_unlock_irqrestore(&tx->lock, flags);
310}
311
Davide Gianfortea3709f72014-05-23 22:06:44 +0200312static void send_hci(struct sdio_func *func, struct tx_cxt *tx,
313 struct sdio_tx *t)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900314{
315 unsigned long flags;
316
Greg Kroah-Hartman75533a02014-07-17 18:33:02 -0700317 dev_dbg(&func->dev, "sdio_send: %*ph\n", t->len - TYPE_A_HEADER_SIZE,
Michalis Pappasf5439c62014-07-17 17:09:39 +0100318 t->buf + TYPE_A_HEADER_SIZE);
319
Sage Ahn247e9cf2012-05-15 13:20:36 +0900320 send_sdio_pkt(func, t->buf, t->len);
321
322 spin_lock_irqsave(&tx->lock, flags);
323 if (t->callback)
324 t->callback(t->cb_data);
325 free_tx_struct(t);
326 spin_unlock_irqrestore(&tx->lock, flags);
327}
328
329static void do_tx(struct work_struct *work)
330{
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;
336 int is_sdu = 0;
337 long diff;
338 unsigned long flags;
339
340 spin_lock_irqsave(&tx->lock, flags);
341 if (!tx->can_send) {
342 spin_unlock_irqrestore(&tx->lock, flags);
343 return;
344 }
345
346 if (!list_empty(&tx->hci_list)) {
347 t = list_entry(tx->hci_list.next, struct sdio_tx, list);
348 list_del(&t->list);
349 is_sdu = 0;
350 } else if (!tx->stop_sdu_tx && !list_empty(&tx->sdu_list)) {
351 do_gettimeofday(&now);
352 before = &tx->sdu_stamp;
353
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);
359 return;
360 }
361 is_sdu = 1;
362 }
363
364 if (!is_sdu && t == NULL) {
365 spin_unlock_irqrestore(&tx->lock, flags);
366 return;
367 }
368
369 tx->can_send = 0;
370
371 spin_unlock_irqrestore(&tx->lock, flags);
372
373 if (is_sdu)
374 send_sdu(func, tx);
375 else
376 send_hci(func, tx, t);
377}
378
379static int gdm_sdio_send(void *priv_dev, void *data, int len,
Michalis Pappas39c511f2014-05-09 18:08:27 +0800380 void (*cb)(void *data), void *cb_data)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900381{
382 struct sdiowm_dev *sdev = priv_dev;
383 struct tx_cxt *tx = &sdev->tx;
384 struct sdio_tx *t;
385 u8 *pkt = data;
386 int no_spc = 0;
387 u16 cmd_evt;
388 unsigned long flags;
389
Ben Chan0fbce842014-06-27 23:17:24 -0700390 if (len > TX_BUF_SIZE - TYPE_A_HEADER_SIZE)
391 return -EINVAL;
Sage Ahn247e9cf2012-05-15 13:20:36 +0900392
393 spin_lock_irqsave(&tx->lock, flags);
394
395 cmd_evt = (pkt[0] << 8) | pkt[1];
396 if (cmd_evt == WIMAX_TX_SDU) {
397 t = get_tx_struct(tx, &no_spc);
398 if (t == NULL) {
399 /* This case must not happen. */
400 spin_unlock_irqrestore(&tx->lock, flags);
401 return -ENOSPC;
402 }
403 list_add_tail(&t->list, &tx->sdu_list);
404
405 memcpy(t->buf, data, len);
406
407 t->len = len;
408 t->callback = cb;
409 t->cb_data = cb_data;
410 } else {
411 t = alloc_tx_struct(tx);
412 if (t == NULL) {
413 spin_unlock_irqrestore(&tx->lock, flags);
414 return -ENOMEM;
415 }
416 list_add_tail(&t->list, &tx->hci_list);
417
418 t->buf[0] = len & 0xff;
419 t->buf[1] = (len >> 8) & 0xff;
420 t->buf[2] = (len >> 16) & 0xff;
421 t->buf[3] = 2;
422 memcpy(t->buf + TYPE_A_HEADER_SIZE, data, len);
423
424 t->len = len + TYPE_A_HEADER_SIZE;
425 t->callback = cb;
426 t->cb_data = cb_data;
427 }
428
429 if (tx->can_send)
430 schedule_work(&sdev->ws);
431
432 spin_unlock_irqrestore(&tx->lock, flags);
433
434 if (no_spc)
435 return -ENOSPC;
436
437 return 0;
438}
439
Ben Chane7d374e2014-06-24 00:55:58 -0700440/* Handle the HCI, WIMAX_SDU_TX_FLOW. */
Sage Ahn247e9cf2012-05-15 13:20:36 +0900441static int control_sdu_tx_flow(struct sdiowm_dev *sdev, u8 *hci_data, int len)
442{
443 struct tx_cxt *tx = &sdev->tx;
444 u16 cmd_evt;
445 unsigned long flags;
446
447 spin_lock_irqsave(&tx->lock, flags);
448
449 cmd_evt = (hci_data[0] << 8) | (hci_data[1]);
450 if (cmd_evt != WIMAX_SDU_TX_FLOW)
451 goto out;
452
453 if (hci_data[4] == 0) {
Kristina Martšenko8943a922014-03-15 01:10:05 +0200454 dev_dbg(&sdev->func->dev, "WIMAX ==> STOP SDU TX\n");
Sage Ahn247e9cf2012-05-15 13:20:36 +0900455 tx->stop_sdu_tx = 1;
456 } else if (hci_data[4] == 1) {
Kristina Martšenko8943a922014-03-15 01:10:05 +0200457 dev_dbg(&sdev->func->dev, "WIMAX ==> START SDU TX\n");
Sage Ahn247e9cf2012-05-15 13:20:36 +0900458 tx->stop_sdu_tx = 0;
459 if (tx->can_send)
460 schedule_work(&sdev->ws);
Ben Chane7d374e2014-06-24 00:55:58 -0700461 /* If free buffer for sdu tx doesn't exist, then tx queue
Sage Ahn247e9cf2012-05-15 13:20:36 +0900462 * should not be woken. For this reason, don't pass the command,
463 * START_SDU_TX.
464 */
465 if (list_empty(&tx->free_list))
466 len = 0;
467 }
468
469out:
470 spin_unlock_irqrestore(&tx->lock, flags);
471 return len;
472}
473
474static void gdm_sdio_irq(struct sdio_func *func)
475{
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;
480 struct sdio_rx *r;
481 unsigned long flags;
482 u8 val, hdr[TYPE_A_LOOKAHEAD_SIZE], *buf;
483 u32 len, blocks, n;
484 int ret, remain;
485
486 /* Check interrupt */
487 val = sdio_readb(func, 0x13, &ret);
488 if (val & 0x01)
489 sdio_writeb(func, 0x01, 0x13, &ret); /* clear interrupt */
490 else
491 return;
492
493 ret = sdio_memcpy_fromio(func, hdr, 0x0, TYPE_A_LOOKAHEAD_SIZE);
494 if (ret) {
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900495 dev_err(&func->dev,
496 "Cannot read from function %d\n", func->num);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900497 goto done;
498 }
499
500 len = (hdr[2] << 16) | (hdr[1] << 8) | hdr[0];
501 if (len > (RX_BUF_SIZE - TYPE_A_HEADER_SIZE)) {
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900502 dev_err(&func->dev, "Too big Type-A size: %d\n", len);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900503 goto done;
504 }
505
506 if (hdr[3] == 1) { /* Ack */
Sage Ahn247e9cf2012-05-15 13:20:36 +0900507 u32 *ack_seq = (u32 *)&hdr[4];
Michalis Pappas39c511f2014-05-09 18:08:27 +0800508
Sage Ahn247e9cf2012-05-15 13:20:36 +0900509 spin_lock_irqsave(&tx->lock, flags);
510 tx->can_send = 1;
511
512 if (!list_empty(&tx->sdu_list) || !list_empty(&tx->hci_list))
513 schedule_work(&sdev->ws);
514 spin_unlock_irqrestore(&tx->lock, flags);
Kristina Martšenko8943a922014-03-15 01:10:05 +0200515 dev_dbg(&func->dev, "Ack... %0x\n", ntohl(*ack_seq));
Sage Ahn247e9cf2012-05-15 13:20:36 +0900516 goto done;
517 }
518
519 memcpy(rx->rx_buf, hdr + TYPE_A_HEADER_SIZE,
Michalis Pappas39c511f2014-05-09 18:08:27 +0800520 TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900521
522 buf = rx->rx_buf + TYPE_A_LOOKAHEAD_SIZE - TYPE_A_HEADER_SIZE;
523 remain = len - TYPE_A_LOOKAHEAD_SIZE + TYPE_A_HEADER_SIZE;
524 if (remain <= 0)
525 goto end_io;
526
527 blocks = remain / func->cur_blksize;
528
529 if (blocks) {
530 n = blocks * func->cur_blksize;
531 ret = sdio_memcpy_fromio(func, buf, 0x0, n);
532 if (ret) {
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900533 dev_err(&func->dev,
534 "Cannot read from function %d\n", func->num);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900535 goto done;
536 }
537 buf += n;
538 remain -= n;
539 }
540
541 if (remain) {
542 ret = sdio_memcpy_fromio(func, buf, 0x0, remain);
543 if (ret) {
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900544 dev_err(&func->dev,
545 "Cannot read from function %d\n", func->num);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900546 goto done;
547 }
548 }
549
550end_io:
Greg Kroah-Hartman75533a02014-07-17 18:33:02 -0700551 dev_dbg(&func->dev, "sdio_receive: %*ph\n", len, rx->rx_buf);
Michalis Pappasf5439c62014-07-17 17:09:39 +0100552
Sage Ahn247e9cf2012-05-15 13:20:36 +0900553 len = control_sdu_tx_flow(sdev, rx->rx_buf, len);
554
555 spin_lock_irqsave(&rx->lock, flags);
556
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);
560 if (r->callback)
561 r->callback(r->cb_data, rx->rx_buf, len);
562 spin_lock_irqsave(&rx->lock, flags);
563 list_del(&r->list);
564 put_rx_struct(rx, r);
565 }
566
567 spin_unlock_irqrestore(&rx->lock, flags);
568
569done:
570 sdio_writeb(func, 0x00, 0x10, &ret); /* PCRRT */
571 if (!phy_dev->netdev)
Paul Stewart54bc1ff2012-05-17 11:15:10 -0700572 register_wimax_device(phy_dev, &func->dev);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900573}
574
575static int gdm_sdio_receive(void *priv_dev,
Michalis Pappas39c511f2014-05-09 18:08:27 +0800576 void (*cb)(void *cb_data, void *data, int len),
577 void *cb_data)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900578{
579 struct sdiowm_dev *sdev = priv_dev;
580 struct rx_cxt *rx = &sdev->rx;
581 struct sdio_rx *r;
582 unsigned long flags;
583
584 spin_lock_irqsave(&rx->lock, flags);
585 r = get_rx_struct(rx);
586 if (r == NULL) {
587 spin_unlock_irqrestore(&rx->lock, flags);
588 return -ENOMEM;
589 }
590
591 r->callback = cb;
592 r->cb_data = cb_data;
593
594 list_add_tail(&r->list, &rx->req_list);
595 spin_unlock_irqrestore(&rx->lock, flags);
596
597 return 0;
598}
599
Davide Gianfortea3709f72014-05-23 22:06:44 +0200600static int sdio_wimax_probe(struct sdio_func *func,
601 const struct sdio_device_id *id)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900602{
603 int ret;
604 struct phy_dev *phy_dev = NULL;
605 struct sdiowm_dev *sdev = NULL;
606
YAMANE Toshiakic9a796d2012-10-29 20:05:16 +0900607 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);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900610
611 sdio_claim_host(func);
612 sdio_enable_func(func);
613 sdio_claim_irq(func, gdm_sdio_irq);
614
615 ret = sdio_boot(func);
616 if (ret)
617 return ret;
618
Devendra Nagac2a17932012-07-12 11:58:37 +0545619 phy_dev = kzalloc(sizeof(*phy_dev), GFP_KERNEL);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900620 if (phy_dev == NULL) {
621 ret = -ENOMEM;
622 goto out;
623 }
Devendra Nagac2a17932012-07-12 11:58:37 +0545624 sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900625 if (sdev == NULL) {
626 ret = -ENOMEM;
627 goto out;
628 }
629
Sage Ahn247e9cf2012-05-15 13:20:36 +0900630 phy_dev->priv_dev = (void *)sdev;
631 phy_dev->send_func = gdm_sdio_send;
632 phy_dev->rcv_func = gdm_sdio_receive;
633
634 ret = init_sdio(sdev);
Alan Coxf1efd9f2012-09-04 16:04:36 +0100635 if (ret < 0)
Sage Ahn247e9cf2012-05-15 13:20:36 +0900636 goto out;
637
638 sdev->func = func;
639
640 sdio_writeb(func, 1, 0x14, &ret); /* Enable interrupt */
641 sdio_release_host(func);
642
643 INIT_WORK(&sdev->ws, do_tx);
644
645 sdio_set_drvdata(func, phy_dev);
646out:
647 if (ret) {
648 kfree(phy_dev);
649 kfree(sdev);
650 }
651
652 return ret;
653}
654
655static void sdio_wimax_remove(struct sdio_func *func)
656{
657 struct phy_dev *phy_dev = sdio_get_drvdata(func);
658 struct sdiowm_dev *sdev = phy_dev->priv_dev;
659
Devendra Nagac0352cb2013-04-13 20:16:51 +0530660 cancel_work_sync(&sdev->ws);
Sage Ahn247e9cf2012-05-15 13:20:36 +0900661 if (phy_dev->netdev)
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);
667 release_sdio(sdev);
668
669 kfree(sdev);
670 kfree(phy_dev);
671}
672
673static const struct sdio_device_id sdio_wimax_ids[] = {
674 { SDIO_DEVICE(0x0296, 0x5347) },
675 {0}
676};
677
678MODULE_DEVICE_TABLE(sdio, sdio_wimax_ids);
679
680static 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,
685};
686
687static int __init sdio_gdm_wimax_init(void)
688{
689 return sdio_register_driver(&sdio_wimax_driver);
690}
691
692static void __exit sdio_gdm_wimax_exit(void)
693{
694 sdio_unregister_driver(&sdio_wimax_driver);
695}
696
697module_init(sdio_gdm_wimax_init);
698module_exit(sdio_gdm_wimax_exit);
699
700MODULE_VERSION(DRIVER_VERSION);
701MODULE_DESCRIPTION("GCT WiMax SDIO Device Driver");
702MODULE_AUTHOR("Ethan Park");
703MODULE_LICENSE("GPL");