refresh generic 2.6.24 patches to match upstream style
[openwrt.git] / package / libertas / src / if_sdio.c
1 /*
2 * linux/drivers/net/wireless/libertas/if_sdio.c
3 *
4 * Copyright 2007 Pierre Ossman
5 *
6 * Inspired by if_cs.c, Copyright 2007 Holger Schurig
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
12 *
13 * This hardware has more or less no CMD53 support, so all registers
14 * must be accessed using sdio_readb()/sdio_writeb().
15 *
16 * Transfers must be in one transaction or the firmware goes bonkers.
17 * This means that the transfer must either be small enough to do a
18 * byte based transfer or it must be padded to a multiple of the
19 * current block size.
20 *
21 * As SDIO is still new to the kernel, it is unfortunately common with
22 * bugs in the host controllers related to that. One such bug is that
23 * controllers cannot do transfers that aren't a multiple of 4 bytes.
24 * If you don't have time to fix the host controller driver, you can
25 * work around the problem by modifying if_sdio_host_to_card() and
26 * if_sdio_card_to_host() to pad the data.
27 */
28
29 #include <linux/moduleparam.h>
30 #include <linux/firmware.h>
31 #include <linux/netdevice.h>
32 #include <linux/delay.h>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/sdio_func.h>
35 #include <linux/mmc/sdio_ids.h>
36
37 #include "host.h"
38 #include "decl.h"
39 #include "defs.h"
40 #include "dev.h"
41 #include "if_sdio.h"
42
43 static char *lbs_helper_name = NULL;
44 module_param_named(helper_name, lbs_helper_name, charp, 0644);
45
46 static char *lbs_fw_name = NULL;
47 module_param_named(fw_name, lbs_fw_name, charp, 0644);
48
49 static const struct sdio_device_id if_sdio_ids[] = {
50 { SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
51 { /* end: all zeroes */ },
52 };
53
54 MODULE_DEVICE_TABLE(sdio, if_sdio_ids);
55
56 struct if_sdio_model {
57 int model;
58 const char *helper;
59 const char *firmware;
60 };
61
62 static struct if_sdio_model if_sdio_models[] = {
63 {
64 /* 8385 */
65 .model = 0x04,
66 .helper = "sd8385_helper.bin",
67 .firmware = "sd8385.bin",
68 },
69 {
70 /* 8686 */
71 .model = 0x0B,
72 .helper = "sd8686_helper.bin",
73 .firmware = "sd8686.bin",
74 },
75 };
76
77 struct if_sdio_packet {
78 struct if_sdio_packet *next;
79 u16 nb;
80 u8 buffer[0] __attribute__((aligned(4)));
81 };
82
83 struct if_sdio_card {
84 struct sdio_func *func;
85 struct lbs_private *priv;
86
87 int model;
88 unsigned long ioport;
89
90 const char *helper;
91 const char *firmware;
92
93 u8 buffer[65536];
94 u8 int_cause;
95 u32 event;
96
97 spinlock_t lock;
98 struct if_sdio_packet *packets;
99 struct work_struct packet_worker;
100 };
101
102 /********************************************************************/
103 /* I/O */
104 /********************************************************************/
105
106 static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
107 {
108 int ret, reg;
109 u16 scratch;
110
111 if (card->model == 0x04)
112 reg = IF_SDIO_SCRATCH_OLD;
113 else
114 reg = IF_SDIO_SCRATCH;
115
116 scratch = sdio_readb(card->func, reg, &ret);
117 if (!ret)
118 scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;
119
120 if (err)
121 *err = ret;
122
123 if (ret)
124 return 0xffff;
125
126 return scratch;
127 }
128
129 static int if_sdio_handle_cmd(struct if_sdio_card *card,
130 u8 *buffer, unsigned size)
131 {
132 int ret;
133 unsigned long flags;
134
135 lbs_deb_enter(LBS_DEB_SDIO);
136
137 spin_lock_irqsave(&card->priv->driver_lock, flags);
138
139 if (size > LBS_CMD_BUFFER_SIZE) {
140 lbs_deb_sdio("response packet too large (%d bytes)\n",
141 (int)size);
142 ret = -E2BIG;
143 goto out;
144 }
145
146 memcpy(card->priv->upld_buf, buffer, size);
147 card->priv->upld_len = size;
148
149 card->int_cause |= MRVDRV_CMD_UPLD_RDY;
150
151 lbs_interrupt(card->priv);
152
153 ret = 0;
154
155 out:
156 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
157
158 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
159
160 return ret;
161 }
162
163 static int if_sdio_handle_data(struct if_sdio_card *card,
164 u8 *buffer, unsigned size)
165 {
166 int ret;
167 struct sk_buff *skb;
168 char *data;
169
170 lbs_deb_enter(LBS_DEB_SDIO);
171
172 if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
173 lbs_deb_sdio("response packet too large (%d bytes)\n",
174 (int)size);
175 ret = -E2BIG;
176 goto out;
177 }
178
179 skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
180 if (!skb) {
181 ret = -ENOMEM;
182 goto out;
183 }
184
185 skb_reserve(skb, NET_IP_ALIGN);
186
187 data = skb_put(skb, size);
188
189 memcpy(data, buffer, size);
190
191 lbs_process_rxed_packet(card->priv, skb);
192
193 ret = 0;
194
195 out:
196 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
197
198 return ret;
199 }
200
201 static int if_sdio_handle_event(struct if_sdio_card *card,
202 u8 *buffer, unsigned size)
203 {
204 int ret;
205 unsigned long flags;
206 u32 event;
207
208 lbs_deb_enter(LBS_DEB_SDIO);
209
210 if (card->model == 0x04) {
211 event = sdio_readb(card->func, IF_SDIO_EVENT, &ret);
212 if (ret)
213 goto out;
214 } else {
215 if (size < 4) {
216 lbs_deb_sdio("event packet too small (%d bytes)\n",
217 (int)size);
218 ret = -EINVAL;
219 goto out;
220 }
221 event = buffer[3] << 24;
222 event |= buffer[2] << 16;
223 event |= buffer[1] << 8;
224 event |= buffer[0] << 0;
225 event <<= SBI_EVENT_CAUSE_SHIFT;
226 }
227
228 spin_lock_irqsave(&card->priv->driver_lock, flags);
229
230 card->event = event;
231 card->int_cause |= MRVDRV_CARDEVENT;
232
233 lbs_interrupt(card->priv);
234
235 spin_unlock_irqrestore(&card->priv->driver_lock, flags);
236
237 ret = 0;
238
239 out:
240 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
241
242 return ret;
243 }
244
245 static int if_sdio_card_to_host(struct if_sdio_card *card)
246 {
247 int ret;
248 u8 status;
249 u16 size, type, chunk;
250 unsigned long timeout;
251
252 lbs_deb_enter(LBS_DEB_SDIO);
253
254 size = if_sdio_read_scratch(card, &ret);
255 if (ret)
256 goto out;
257
258 if (size < 4) {
259 lbs_deb_sdio("invalid packet size (%d bytes) from firmware\n",
260 (int)size);
261 ret = -EINVAL;
262 goto out;
263 }
264
265 timeout = jiffies + HZ;
266 while (1) {
267 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
268 if (ret)
269 goto out;
270 if (status & IF_SDIO_IO_RDY)
271 break;
272 if (time_after(jiffies, timeout)) {
273 ret = -ETIMEDOUT;
274 goto out;
275 }
276 mdelay(1);
277 }
278
279 /*
280 * The transfer must be in one transaction or the firmware
281 * goes suicidal.
282 */
283 chunk = size;
284 if ((chunk > card->func->cur_blksize) || (chunk > 512)) {
285 chunk = (chunk + card->func->cur_blksize - 1) /
286 card->func->cur_blksize * card->func->cur_blksize;
287 }
288
289 ret = sdio_readsb(card->func, card->buffer, card->ioport, chunk);
290 if (ret)
291 goto out;
292
293 chunk = card->buffer[0] | (card->buffer[1] << 8);
294 type = card->buffer[2] | (card->buffer[3] << 8);
295
296 lbs_deb_sdio("packet of type %d and size %d bytes\n",
297 (int)type, (int)chunk);
298
299 if (chunk > size) {
300 lbs_deb_sdio("packet fragment (%d > %d)\n",
301 (int)chunk, (int)size);
302 ret = -EINVAL;
303 goto out;
304 }
305
306 if (chunk < size) {
307 lbs_deb_sdio("packet fragment (%d < %d)\n",
308 (int)chunk, (int)size);
309 }
310
311 switch (type) {
312 case MVMS_CMD:
313 ret = if_sdio_handle_cmd(card, card->buffer + 4, chunk - 4);
314 if (ret)
315 goto out;
316 break;
317 case MVMS_DAT:
318 ret = if_sdio_handle_data(card, card->buffer + 4, chunk - 4);
319 if (ret)
320 goto out;
321 break;
322 case MVMS_EVENT:
323 ret = if_sdio_handle_event(card, card->buffer + 4, chunk - 4);
324 if (ret)
325 goto out;
326 break;
327 default:
328 lbs_deb_sdio("invalid type (%d) from firmware\n",
329 (int)type);
330 ret = -EINVAL;
331 goto out;
332 }
333
334 out:
335 if (ret)
336 lbs_pr_err("problem fetching packet from firmware\n");
337
338 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
339
340 return ret;
341 }
342
343 static void if_sdio_host_to_card_worker(struct work_struct *work)
344 {
345 struct if_sdio_card *card;
346 struct if_sdio_packet *packet;
347 unsigned long timeout;
348 u8 status;
349 int ret;
350 unsigned long flags;
351
352 lbs_deb_enter(LBS_DEB_SDIO);
353
354 card = container_of(work, struct if_sdio_card, packet_worker);
355
356 while (1) {
357 spin_lock_irqsave(&card->lock, flags);
358 packet = card->packets;
359 if (packet)
360 card->packets = packet->next;
361 spin_unlock_irqrestore(&card->lock, flags);
362
363 if (!packet)
364 break;
365
366 sdio_claim_host(card->func);
367
368 timeout = jiffies + HZ;
369 while (1) {
370 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
371 if (ret)
372 goto release;
373 if (status & IF_SDIO_IO_RDY)
374 break;
375 if (time_after(jiffies, timeout)) {
376 ret = -ETIMEDOUT;
377 goto release;
378 }
379 mdelay(1);
380 }
381
382 ret = sdio_writesb(card->func, card->ioport,
383 packet->buffer, packet->nb);
384 if (ret)
385 goto release;
386 release:
387 sdio_release_host(card->func);
388
389 kfree(packet);
390 }
391
392 lbs_deb_leave(LBS_DEB_SDIO);
393 }
394
395 /********************************************************************/
396 /* Firmware */
397 /********************************************************************/
398
399 static int if_sdio_prog_helper(struct if_sdio_card *card)
400 {
401 int ret;
402 u8 status;
403 const struct firmware *fw;
404 unsigned long timeout;
405 u8 *chunk_buffer;
406 u32 chunk_size;
407 u8 *firmware;
408 size_t size;
409
410 lbs_deb_enter(LBS_DEB_SDIO);
411
412 ret = request_firmware(&fw, card->helper, &card->func->dev);
413 if (ret) {
414 lbs_pr_err("can't load helper firmware\n");
415 goto out;
416 }
417
418 chunk_buffer = kzalloc(64, GFP_KERNEL);
419 if (!chunk_buffer) {
420 ret = -ENOMEM;
421 goto release_fw;
422 }
423
424 sdio_claim_host(card->func);
425
426 ret = sdio_set_block_size(card->func, 32);
427 if (ret)
428 goto release;
429
430 firmware = fw->data;
431 size = fw->size;
432
433 while (size) {
434 timeout = jiffies + HZ;
435 while (1) {
436 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
437 if (ret)
438 goto release;
439 if ((status & IF_SDIO_IO_RDY) &&
440 (status & IF_SDIO_DL_RDY))
441 break;
442 if (time_after(jiffies, timeout)) {
443 ret = -ETIMEDOUT;
444 goto release;
445 }
446 mdelay(1);
447 }
448
449 chunk_size = min(size, (size_t)60);
450
451 *((__le32*)chunk_buffer) = cpu_to_le32(chunk_size);
452 memcpy(chunk_buffer + 4, firmware, chunk_size);
453 /*
454 lbs_deb_sdio("sending %d bytes chunk\n", chunk_size);
455 */
456 ret = sdio_writesb(card->func, card->ioport,
457 chunk_buffer, 64);
458 if (ret)
459 goto release;
460
461 firmware += chunk_size;
462 size -= chunk_size;
463 }
464
465 /* an empty block marks the end of the transfer */
466 memset(chunk_buffer, 0, 4);
467 ret = sdio_writesb(card->func, card->ioport, chunk_buffer, 64);
468 if (ret)
469 goto release;
470
471 lbs_deb_sdio("waiting for helper to boot...\n");
472
473 /* wait for the helper to boot by looking at the size register */
474 timeout = jiffies + HZ;
475 while (1) {
476 u16 req_size;
477
478 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
479 if (ret)
480 goto release;
481
482 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
483 if (ret)
484 goto release;
485
486 if (req_size != 0)
487 break;
488
489 if (time_after(jiffies, timeout)) {
490 ret = -ETIMEDOUT;
491 goto release;
492 }
493
494 msleep(10);
495 }
496
497 ret = 0;
498
499 release:
500 sdio_set_block_size(card->func, 0);
501 sdio_release_host(card->func);
502 kfree(chunk_buffer);
503 release_fw:
504 release_firmware(fw);
505
506 out:
507 if (ret)
508 lbs_pr_err("failed to load helper firmware\n");
509
510 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
511
512 return ret;
513 }
514
515 static int if_sdio_prog_real(struct if_sdio_card *card)
516 {
517 int ret;
518 u8 status;
519 const struct firmware *fw;
520 unsigned long timeout;
521 u8 *chunk_buffer;
522 u32 chunk_size;
523 u8 *firmware;
524 size_t size, req_size;
525
526 lbs_deb_enter(LBS_DEB_SDIO);
527
528 ret = request_firmware(&fw, card->firmware, &card->func->dev);
529 if (ret) {
530 lbs_pr_err("can't load firmware\n");
531 goto out;
532 }
533
534 chunk_buffer = kzalloc(512, GFP_KERNEL);
535 if (!chunk_buffer) {
536 ret = -ENOMEM;
537 goto release_fw;
538 }
539
540 sdio_claim_host(card->func);
541
542 ret = sdio_set_block_size(card->func, 32);
543 if (ret)
544 goto release;
545
546 firmware = fw->data;
547 size = fw->size;
548
549 while (size) {
550 timeout = jiffies + HZ;
551 while (1) {
552 status = sdio_readb(card->func, IF_SDIO_STATUS, &ret);
553 if (ret)
554 goto release;
555 if ((status & IF_SDIO_IO_RDY) &&
556 (status & IF_SDIO_DL_RDY))
557 break;
558 if (time_after(jiffies, timeout)) {
559 ret = -ETIMEDOUT;
560 goto release;
561 }
562 mdelay(1);
563 }
564
565 req_size = sdio_readb(card->func, IF_SDIO_RD_BASE, &ret);
566 if (ret)
567 goto release;
568
569 req_size |= sdio_readb(card->func, IF_SDIO_RD_BASE + 1, &ret) << 8;
570 if (ret)
571 goto release;
572 /*
573 lbs_deb_sdio("firmware wants %d bytes\n", (int)req_size);
574 */
575 if (req_size == 0) {
576 lbs_deb_sdio("firmware helper gave up early\n");
577 ret = -EIO;
578 goto release;
579 }
580
581 if (req_size & 0x01) {
582 lbs_deb_sdio("firmware helper signalled error\n");
583 ret = -EIO;
584 goto release;
585 }
586
587 if (req_size > size)
588 req_size = size;
589
590 while (req_size) {
591 chunk_size = min(req_size, (size_t)512);
592
593 memcpy(chunk_buffer, firmware, chunk_size);
594 /*
595 lbs_deb_sdio("sending %d bytes (%d bytes) chunk\n",
596 chunk_size, (chunk_size + 31) / 32 * 32);
597 */
598 ret = sdio_writesb(card->func, card->ioport,
599 chunk_buffer, (chunk_size + 31) / 32 * 32);
600 if (ret)
601 goto release;
602
603 firmware += chunk_size;
604 size -= chunk_size;
605 req_size -= chunk_size;
606 }
607 }
608
609 ret = 0;
610
611 lbs_deb_sdio("waiting for firmware to boot...\n");
612
613 /* wait for the firmware to boot */
614 timeout = jiffies + HZ;
615 while (1) {
616 u16 scratch;
617
618 scratch = if_sdio_read_scratch(card, &ret);
619 if (ret)
620 goto release;
621
622 if (scratch == IF_SDIO_FIRMWARE_OK)
623 break;
624
625 if (time_after(jiffies, timeout)) {
626 ret = -ETIMEDOUT;
627 goto release;
628 }
629
630 msleep(10);
631 }
632
633 ret = 0;
634
635 release:
636 sdio_set_block_size(card->func, 0);
637 sdio_release_host(card->func);
638 kfree(chunk_buffer);
639 release_fw:
640 release_firmware(fw);
641
642 out:
643 if (ret)
644 lbs_pr_err("failed to load firmware\n");
645
646 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
647
648 return ret;
649 }
650
651 static int if_sdio_prog_firmware(struct if_sdio_card *card)
652 {
653 int ret;
654 u16 scratch;
655
656 lbs_deb_enter(LBS_DEB_SDIO);
657
658 sdio_claim_host(card->func);
659 scratch = if_sdio_read_scratch(card, &ret);
660 sdio_release_host(card->func);
661
662 if (ret)
663 goto out;
664
665 if (scratch == IF_SDIO_FIRMWARE_OK) {
666 lbs_deb_sdio("firmware already loaded\n");
667 goto success;
668 }
669
670 ret = if_sdio_prog_helper(card);
671 if (ret)
672 goto out;
673
674 ret = if_sdio_prog_real(card);
675 if (ret)
676 goto out;
677
678 success:
679 ret = 0;
680
681 out:
682 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
683
684 return ret;
685 }
686
687 /*******************************************************************/
688 /* Libertas callbacks */
689 /*******************************************************************/
690
691 static int if_sdio_host_to_card(struct lbs_private *priv,
692 u8 type, u8 *buf, u16 nb)
693 {
694 int ret;
695 struct if_sdio_card *card;
696 struct if_sdio_packet *packet, *cur;
697 u16 size;
698 unsigned long flags;
699
700 lbs_deb_enter_args(LBS_DEB_SDIO, "type %d, bytes %d", type, nb);
701
702 card = priv->card;
703
704 if (nb > (65536 - sizeof(struct if_sdio_packet) - 4)) {
705 ret = -EINVAL;
706 goto out;
707 }
708
709 /*
710 * The transfer must be in one transaction or the firmware
711 * goes suicidal.
712 */
713 size = nb + 4;
714 if ((size > card->func->cur_blksize) || (size > 512)) {
715 size = (size + card->func->cur_blksize - 1) /
716 card->func->cur_blksize * card->func->cur_blksize;
717 }
718
719 packet = kzalloc(sizeof(struct if_sdio_packet) + size,
720 GFP_ATOMIC);
721 if (!packet) {
722 ret = -ENOMEM;
723 goto out;
724 }
725
726 packet->next = NULL;
727 packet->nb = size;
728
729 /*
730 * SDIO specific header.
731 */
732 packet->buffer[0] = (nb + 4) & 0xff;
733 packet->buffer[1] = ((nb + 4) >> 8) & 0xff;
734 packet->buffer[2] = type;
735 packet->buffer[3] = 0;
736
737 memcpy(packet->buffer + 4, buf, nb);
738
739 spin_lock_irqsave(&card->lock, flags);
740
741 if (!card->packets)
742 card->packets = packet;
743 else {
744 cur = card->packets;
745 while (cur->next)
746 cur = cur->next;
747 cur->next = packet;
748 }
749
750 switch (type) {
751 case MVMS_CMD:
752 priv->dnld_sent = DNLD_CMD_SENT;
753 break;
754 case MVMS_DAT:
755 priv->dnld_sent = DNLD_DATA_SENT;
756 break;
757 default:
758 lbs_deb_sdio("unknown packet type %d\n", (int)type);
759 }
760
761 spin_unlock_irqrestore(&card->lock, flags);
762
763 schedule_work(&card->packet_worker);
764
765 ret = 0;
766
767 out:
768 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
769
770 return ret;
771 }
772
773 static int if_sdio_get_int_status(struct lbs_private *priv, u8 *ireg)
774 {
775 struct if_sdio_card *card;
776
777 lbs_deb_enter(LBS_DEB_SDIO);
778
779 card = priv->card;
780
781 *ireg = card->int_cause;
782 card->int_cause = 0;
783
784 lbs_deb_leave(LBS_DEB_SDIO);
785
786 return 0;
787 }
788
789 static int if_sdio_read_event_cause(struct lbs_private *priv)
790 {
791 struct if_sdio_card *card;
792
793 lbs_deb_enter(LBS_DEB_SDIO);
794
795 card = priv->card;
796
797 priv->eventcause = card->event;
798
799 lbs_deb_leave(LBS_DEB_SDIO);
800
801 return 0;
802 }
803
804 /*******************************************************************/
805 /* SDIO callbacks */
806 /*******************************************************************/
807
808 static void if_sdio_interrupt(struct sdio_func *func)
809 {
810 int ret;
811 struct if_sdio_card *card;
812 u8 cause;
813
814 lbs_deb_enter(LBS_DEB_SDIO);
815
816 card = sdio_get_drvdata(func);
817
818 cause = sdio_readb(card->func, IF_SDIO_H_INT_STATUS, &ret);
819 if (ret)
820 goto out;
821
822 lbs_deb_sdio("interrupt: 0x%X\n", (unsigned)cause);
823
824 sdio_writeb(card->func, ~cause, IF_SDIO_H_INT_STATUS, &ret);
825 if (ret)
826 goto out;
827
828 /*
829 * Ignore the define name, this really means the card has
830 * successfully received the command.
831 */
832 if (cause & IF_SDIO_H_INT_DNLD)
833 lbs_host_to_card_done(card->priv);
834
835
836 if (cause & IF_SDIO_H_INT_UPLD) {
837 ret = if_sdio_card_to_host(card);
838 if (ret)
839 goto out;
840 }
841
842 ret = 0;
843
844 out:
845 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
846 }
847
848 static int if_sdio_probe(struct sdio_func *func,
849 const struct sdio_device_id *id)
850 {
851 struct if_sdio_card *card;
852 struct lbs_private *priv;
853 int ret, i;
854 unsigned int model;
855 struct if_sdio_packet *packet;
856
857 lbs_deb_enter(LBS_DEB_SDIO);
858
859 for (i = 0;i < func->card->num_info;i++) {
860 if (sscanf(func->card->info[i],
861 "802.11 SDIO ID: %x", &model) == 1)
862 break;
863 if (sscanf(func->card->info[i],
864 "ID: %x", &model) == 1)
865 break;
866 }
867
868 if (i == func->card->num_info) {
869 lbs_pr_err("unable to identify card model\n");
870 return -ENODEV;
871 }
872
873 card = kzalloc(sizeof(struct if_sdio_card), GFP_KERNEL);
874 if (!card)
875 return -ENOMEM;
876
877 card->func = func;
878 card->model = model;
879 spin_lock_init(&card->lock);
880 INIT_WORK(&card->packet_worker, if_sdio_host_to_card_worker);
881
882 for (i = 0;i < ARRAY_SIZE(if_sdio_models);i++) {
883 if (card->model == if_sdio_models[i].model)
884 break;
885 }
886
887 if (i == ARRAY_SIZE(if_sdio_models)) {
888 lbs_pr_err("unkown card model 0x%x\n", card->model);
889 ret = -ENODEV;
890 goto free;
891 }
892
893 card->helper = if_sdio_models[i].helper;
894 card->firmware = if_sdio_models[i].firmware;
895
896 if (lbs_helper_name) {
897 lbs_deb_sdio("overriding helper firmware: %s\n",
898 lbs_helper_name);
899 card->helper = lbs_helper_name;
900 }
901
902 if (lbs_fw_name) {
903 lbs_deb_sdio("overriding firmware: %s\n", lbs_fw_name);
904 card->firmware = lbs_fw_name;
905 }
906
907 sdio_claim_host(func);
908
909 ret = sdio_enable_func(func);
910 if (ret)
911 goto release;
912
913 ret = sdio_claim_irq(func, if_sdio_interrupt);
914 if (ret)
915 goto disable;
916
917 card->ioport = sdio_readb(func, IF_SDIO_IOPORT, &ret);
918 if (ret)
919 goto release_int;
920
921 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 1, &ret) << 8;
922 if (ret)
923 goto release_int;
924
925 card->ioport |= sdio_readb(func, IF_SDIO_IOPORT + 2, &ret) << 16;
926 if (ret)
927 goto release_int;
928
929 sdio_release_host(func);
930
931 sdio_set_drvdata(func, card);
932
933 lbs_deb_sdio("class = 0x%X, vendor = 0x%X, "
934 "device = 0x%X, model = 0x%X, ioport = 0x%X\n",
935 func->class, func->vendor, func->device,
936 model, (unsigned)card->ioport);
937
938 ret = if_sdio_prog_firmware(card);
939 if (ret)
940 goto reclaim;
941
942 priv = lbs_add_card(card, &func->dev);
943 if (!priv) {
944 ret = -ENOMEM;
945 goto reclaim;
946 }
947
948 card->priv = priv;
949
950 priv->card = card;
951 priv->hw_host_to_card = if_sdio_host_to_card;
952 priv->hw_get_int_status = if_sdio_get_int_status;
953 priv->hw_read_event_cause = if_sdio_read_event_cause;
954
955 priv->fw_ready = 1;
956
957 /*
958 * Enable interrupts now that everything is set up
959 */
960 sdio_claim_host(func);
961 sdio_writeb(func, 0x0f, IF_SDIO_H_INT_MASK, &ret);
962 sdio_release_host(func);
963 if (ret)
964 goto reclaim;
965
966 ret = lbs_start_card(priv);
967 if (ret)
968 goto err_activate_card;
969
970 out:
971 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
972
973 return ret;
974
975 err_activate_card:
976 flush_scheduled_work();
977 free_netdev(priv->dev);
978 kfree(priv);
979 reclaim:
980 sdio_claim_host(func);
981 release_int:
982 sdio_release_irq(func);
983 disable:
984 sdio_disable_func(func);
985 release:
986 sdio_release_host(func);
987 free:
988 while (card->packets) {
989 packet = card->packets;
990 card->packets = card->packets->next;
991 kfree(packet);
992 }
993
994 kfree(card);
995
996 goto out;
997 }
998
999 static void if_sdio_remove(struct sdio_func *func)
1000 {
1001 struct if_sdio_card *card;
1002 struct if_sdio_packet *packet;
1003
1004 lbs_deb_enter(LBS_DEB_SDIO);
1005
1006 card = sdio_get_drvdata(func);
1007
1008 card->priv->surpriseremoved = 1;
1009
1010 lbs_deb_sdio("call remove card\n");
1011 lbs_stop_card(card->priv);
1012 lbs_remove_card(card->priv);
1013
1014 flush_scheduled_work();
1015
1016 sdio_claim_host(func);
1017 sdio_release_irq(func);
1018 sdio_disable_func(func);
1019 sdio_release_host(func);
1020
1021 while (card->packets) {
1022 packet = card->packets;
1023 card->packets = card->packets->next;
1024 kfree(packet);
1025 }
1026
1027 kfree(card);
1028
1029 lbs_deb_leave(LBS_DEB_SDIO);
1030 }
1031
1032 static struct sdio_driver if_sdio_driver = {
1033 .name = "libertas_sdio",
1034 .id_table = if_sdio_ids,
1035 .probe = if_sdio_probe,
1036 .remove = if_sdio_remove,
1037 };
1038
1039 /*******************************************************************/
1040 /* Module functions */
1041 /*******************************************************************/
1042
1043 static int __init if_sdio_init_module(void)
1044 {
1045 int ret = 0;
1046
1047 lbs_deb_enter(LBS_DEB_SDIO);
1048
1049 printk(KERN_INFO "libertas_sdio: Libertas SDIO driver\n");
1050 printk(KERN_INFO "libertas_sdio: Copyright Pierre Ossman\n");
1051
1052 ret = sdio_register_driver(&if_sdio_driver);
1053
1054 lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);
1055
1056 return ret;
1057 }
1058
1059 static void __exit if_sdio_exit_module(void)
1060 {
1061 lbs_deb_enter(LBS_DEB_SDIO);
1062
1063 sdio_unregister_driver(&if_sdio_driver);
1064
1065 lbs_deb_leave(LBS_DEB_SDIO);
1066 }
1067
1068 module_init(if_sdio_init_module);
1069 module_exit(if_sdio_exit_module);
1070
1071 MODULE_DESCRIPTION("Libertas SDIO WLAN Driver");
1072 MODULE_AUTHOR("Pierre Ossman");
1073 MODULE_LICENSE("GPL");
This page took 0.122468 seconds and 5 git commands to generate.