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