if_cs.c 22.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*

  Driver for the Marvell 8385 based compact flash WLAN cards.

  (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  Boston, MA 02110-1301, USA.

*/

#include <linux/module.h>
#include <linux/delay.h>
#include <linux/moduleparam.h>
#include <linux/firmware.h>
#include <linux/netdevice.h>

#include <pcmcia/cs_types.h>
#include <pcmcia/cs.h>
#include <pcmcia/cistpl.h>
#include <pcmcia/ds.h>

#include <linux/io.h>

#define DRV_NAME "libertas_cs"

#include "decl.h"
#include "defs.h"
#include "dev.h"


/********************************************************************/
/* Module stuff                                                     */
/********************************************************************/

MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");
MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");
MODULE_LICENSE("GPL");



/********************************************************************/
/* Data structures                                                  */
/********************************************************************/

struct if_cs_card {
	struct pcmcia_device *p_dev;
60
	struct lbs_private *priv;
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
	void __iomem *iobase;
};



/********************************************************************/
/* Hardware access                                                  */
/********************************************************************/

/* This define enables wrapper functions which allow you
   to dump all register accesses. You normally won't this,
   except for development */
/* #define DEBUG_IO */

#ifdef DEBUG_IO
static int debug_output = 0;
#else
/* This way the compiler optimizes the printk's away */
#define debug_output 0
#endif

static inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg)
{
	unsigned int val = ioread8(card->iobase + reg);
	if (debug_output)
86
		printk(KERN_INFO "inb %08x<%02x\n", reg, val);
87
88
89
90
91
92
	return val;
}
static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg)
{
	unsigned int val = ioread16(card->iobase + reg);
	if (debug_output)
93
		printk(KERN_INFO "inw %08x<%04x\n", reg, val);
94
95
96
97
98
99
100
101
102
	return val;
}
static inline void if_cs_read16_rep(
	struct if_cs_card *card,
	uint reg,
	void *buf,
	unsigned long count)
{
	if (debug_output)
103
		printk(KERN_INFO "insw %08x<(0x%lx words)\n",
104
105
106
107
108
109
110
			reg, count);
	ioread16_rep(card->iobase + reg, buf, count);
}

static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val)
{
	if (debug_output)
111
		printk(KERN_INFO "outb %08x>%02x\n", reg, val);
112
113
114
115
116
117
	iowrite8(val, card->iobase + reg);
}

static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val)
{
	if (debug_output)
118
		printk(KERN_INFO "outw %08x>%04x\n", reg, val);
119
120
121
122
123
124
125
126
127
128
	iowrite16(val, card->iobase + reg);
}

static inline void if_cs_write16_rep(
	struct if_cs_card *card,
	uint reg,
	void *buf,
	unsigned long count)
{
	if (debug_output)
129
		printk(KERN_INFO "outsw %08x>(0x%lx words)\n",
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
			reg, count);
	iowrite16_rep(card->iobase + reg, buf, count);
}


/*
 * I know that polling/delaying is frowned upon. However, this procedure
 * with polling is needed while downloading the firmware. At this stage,
 * the hardware does unfortunately not create any interrupts.
 *
 * Fortunately, this function is never used once the firmware is in
 * the card. :-)
 *
 * As a reference, see the "Firmware Specification v5.1", page 18
 * and 19. I did not follow their suggested timing to the word,
 * but this works nice & fast anyway.
 */
static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg)
{
	int i;

151
	for (i = 0; i < 100000; i++) {
152
153
154
		u8 val = if_cs_read8(card, addr);
		if (val == reg)
			return i;
155
		udelay(5);
156
157
158
159
160
161
	}
	return -ETIME;
}



162
163
164
165
166
167
168
/* First the bitmasks for the host/card interrupt/status registers: */
#define IF_CS_BIT_TX			0x0001
#define IF_CS_BIT_RX			0x0002
#define IF_CS_BIT_COMMAND		0x0004
#define IF_CS_BIT_RESP			0x0008
#define IF_CS_BIT_EVENT			0x0010
#define	IF_CS_BIT_MASK			0x001f
169

170
171
/* And now the individual registers and assorted masks */
#define IF_CS_HOST_STATUS		0x00000000
172

173
#define IF_CS_HOST_INT_CAUSE		0x00000002
174

175
#define IF_CS_HOST_INT_MASK		0x00000004
176

177
178
#define IF_CS_HOST_WRITE		0x00000016
#define IF_CS_HOST_WRITE_LEN		0x00000014
179

180
181
#define IF_CS_HOST_CMD			0x0000001A
#define IF_CS_HOST_CMD_LEN		0x00000018
182

183
184
#define IF_CS_READ			0x00000010
#define IF_CS_READ_LEN			0x00000024
185

186
187
#define IF_CS_CARD_CMD			0x00000012
#define IF_CS_CARD_CMD_LEN		0x00000030
188

189
190
#define IF_CS_CARD_STATUS		0x00000020
#define IF_CS_CARD_STATUS_MASK		0x7f00
191

192
#define IF_CS_CARD_INT_CAUSE		0x00000022
193

194
195
#define IF_CS_CARD_SQ_READ_LOW		0x00000028
#define IF_CS_CARD_SQ_HELPER_OK		0x10
196
197
198
199
200
201

#define IF_CS_SCRATCH			0x0000003F



/********************************************************************/
202
/* I/O and interrupt handling                                       */
203
204
/********************************************************************/

205
206
207
static inline void if_cs_enable_ints(struct if_cs_card *card)
{
	lbs_deb_enter(LBS_DEB_CS);
208
	if_cs_write16(card, IF_CS_HOST_INT_MASK, 0);
209
210
211
212
213
}

static inline void if_cs_disable_ints(struct if_cs_card *card)
{
	lbs_deb_enter(LBS_DEB_CS);
214
	if_cs_write16(card, IF_CS_HOST_INT_MASK, IF_CS_BIT_MASK);
215
216
}

217
218
219
/*
 * Called from if_cs_host_to_card to send a command to the hardware
 */
220
static int if_cs_send_cmd(struct lbs_private *priv, u8 *buf, u16 nb)
221
222
223
224
225
226
{
	struct if_cs_card *card = (struct if_cs_card *)priv->card;
	int ret = -1;
	int loops = 0;

	lbs_deb_enter(LBS_DEB_CS);
227
	if_cs_disable_ints(card);
228
229
230

	/* Is hardware ready? */
	while (1) {
231
232
		u16 val = if_cs_read16(card, IF_CS_CARD_STATUS);
		if (val & IF_CS_BIT_COMMAND)
233
234
235
236
237
238
239
240
			break;
		if (++loops > 100) {
			lbs_pr_err("card not ready for commands\n");
			goto done;
		}
		mdelay(1);
	}

241
	if_cs_write16(card, IF_CS_HOST_CMD_LEN, nb);
242

243
	if_cs_write16_rep(card, IF_CS_HOST_CMD, buf, nb / 2);
244
245
	/* Are we supposed to transfer an odd amount of bytes? */
	if (nb & 1)
246
		if_cs_write8(card, IF_CS_HOST_CMD, buf[nb-1]);
247
248
249

	/* "Assert the download over interrupt command in the Host
	 * status register" */
250
	if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
251
252
253

	/* "Assert the download over interrupt command in the Card
	 * interrupt case register" */
254
	if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
255
256
257
	ret = 0;

done:
258
	if_cs_enable_ints(card);
259
260
261
262
263
264
265
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}

/*
 * Called from if_cs_host_to_card to send a data to the hardware
 */
266
static void if_cs_send_data(struct lbs_private *priv, u8 *buf, u16 nb)
267
268
{
	struct if_cs_card *card = (struct if_cs_card *)priv->card;
269
	u16 status;
270
271

	lbs_deb_enter(LBS_DEB_CS);
272
273
	if_cs_disable_ints(card);

274
275
	status = if_cs_read16(card, IF_CS_CARD_STATUS);
	BUG_ON((status & IF_CS_BIT_TX) == 0);
276

277
	if_cs_write16(card, IF_CS_HOST_WRITE_LEN, nb);
278
279

	/* write even number of bytes, then odd byte if necessary */
280
	if_cs_write16_rep(card, IF_CS_HOST_WRITE, buf, nb / 2);
281
	if (nb & 1)
282
		if_cs_write8(card, IF_CS_HOST_WRITE, buf[nb-1]);
283

284
285
	if_cs_write16(card, IF_CS_HOST_STATUS, IF_CS_BIT_TX);
	if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_TX);
286
	if_cs_enable_ints(card);
287
288
289
290
291
292
293

	lbs_deb_leave(LBS_DEB_CS);
}

/*
 * Get the command result out of the card.
 */
294
static int if_cs_receive_cmdres(struct lbs_private *priv, u8 *data, u32 *len)
295
{
296
	unsigned long flags;
297
	int ret = -1;
298
	u16 status;
299
300
301
302

	lbs_deb_enter(LBS_DEB_CS);

	/* is hardware ready? */
303
304
305
306
	status = if_cs_read16(priv->card, IF_CS_CARD_STATUS);
	if ((status & IF_CS_BIT_RESP) == 0) {
		lbs_pr_err("no cmd response in card\n");
		*len = 0;
307
308
309
		goto out;
	}

310
	*len = if_cs_read16(priv->card, IF_CS_CARD_CMD_LEN);
311
	if ((*len == 0) || (*len > LBS_CMD_BUFFER_SIZE)) {
312
313
314
315
316
		lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);
		goto out;
	}

	/* read even number of bytes, then odd byte if necessary */
317
	if_cs_read16_rep(priv->card, IF_CS_CARD_CMD, data, *len/sizeof(u16));
318
	if (*len & 1)
319
		data[*len-1] = if_cs_read8(priv->card, IF_CS_CARD_CMD);
320

321
322
323
	/* This is a workaround for a firmware that reports too much
	 * bytes */
	*len -= 8;
324
	ret = 0;
325
326
327
328
329
330

	/* Clear this flag again */
	spin_lock_irqsave(&priv->driver_lock, flags);
	priv->dnld_sent = DNLD_RES_RECEIVED;
	spin_unlock_irqrestore(&priv->driver_lock, flags);

331
332
333
334
335
out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);
	return ret;
}

336
static struct sk_buff *if_cs_receive_data(struct lbs_private *priv)
337
338
339
340
341
342
343
{
	struct sk_buff *skb = NULL;
	u16 len;
	u8 *data;

	lbs_deb_enter(LBS_DEB_CS);

344
	len = if_cs_read16(priv->card, IF_CS_READ_LEN);
345
346
347
348
349
350
	if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
		lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);
		priv->stats.rx_dropped++;
		goto dat_err;
	}

351
	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);
352
353
	if (!skb)
		goto out;
354
355
356
	skb_put(skb, len);
	skb_reserve(skb, 2);/* 16 byte align */
	data = skb->data;
357
358

	/* read even number of bytes, then odd byte if necessary */
359
	if_cs_read16_rep(priv->card, IF_CS_READ, data, len/sizeof(u16));
360
	if (len & 1)
361
		data[len-1] = if_cs_read8(priv->card, IF_CS_READ);
362
363

dat_err:
364
365
	if_cs_write16(priv->card, IF_CS_HOST_STATUS, IF_CS_BIT_RX);
	if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_RX);
366
367
368
369
370
371

out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);
	return skb;
}

372
373
374
375
376
377
378
379
static irqreturn_t if_cs_interrupt(int irq, void *data)
{
	struct if_cs_card *card = data;
	struct lbs_private *priv = card->priv;
	u16 cause;

	lbs_deb_enter(LBS_DEB_CS);

380
	/* Ask card interrupt cause register if there is something for us */
381
	cause = if_cs_read16(card, IF_CS_CARD_INT_CAUSE);
Holger Schurig's avatar
Holger Schurig committed
382
383
	lbs_deb_cs("cause 0x%04x\n", cause);

384
385
386
387
388
389
390
391
392
393
394
	if (cause == 0) {
		/* Not for us */
		return IRQ_NONE;
	}

	if (cause == 0xffff) {
		/* Read in junk, the card has probably been removed */
		card->priv->surpriseremoved = 1;
		return IRQ_HANDLED;
	}

395
	if (cause & IF_CS_BIT_RX) {
396
397
398
399
400
401
402
		struct sk_buff *skb;
		lbs_deb_cs("rx packet\n");
		skb = if_cs_receive_data(priv);
		if (skb)
			lbs_process_rxed_packet(priv, skb);
	}

403
	if (cause & IF_CS_BIT_TX) {
404
		lbs_deb_cs("tx done\n");
405
406
407
		lbs_host_to_card_done(priv);
	}

408
	if (cause & IF_CS_BIT_RESP) {
409
410
411
		unsigned long flags;
		u8 i;

412
		lbs_deb_cs("cmd resp\n");
413
414
415
416
417
418
419
420
421
422
423
424
425
		spin_lock_irqsave(&priv->driver_lock, flags);
		i = (priv->resp_idx == 0) ? 1 : 0;
		spin_unlock_irqrestore(&priv->driver_lock, flags);

		BUG_ON(priv->resp_len[i]);
		if_cs_receive_cmdres(priv, priv->resp_buf[i],
			&priv->resp_len[i]);

		spin_lock_irqsave(&priv->driver_lock, flags);
		lbs_notify_command_response(priv, i);
		spin_unlock_irqrestore(&priv->driver_lock, flags);
	}

426
427
428
429
430
	if (cause & IF_CS_BIT_EVENT) {
		u16 event = if_cs_read16(priv->card, IF_CS_CARD_STATUS)
			& IF_CS_CARD_STATUS_MASK;
		if_cs_write16(priv->card, IF_CS_HOST_INT_CAUSE,
			IF_CS_BIT_EVENT);
431
		lbs_deb_cs("host event 0x%04x\n", event);
432
433
434
		lbs_queue_event(priv, event >> 8 & 0xff);
	}

Holger Schurig's avatar
Holger Schurig committed
435
436
437
	/* Clear interrupt cause */
	if_cs_write16(card, IF_CS_CARD_INT_CAUSE, cause & IF_CS_BIT_MASK);

438
	lbs_deb_leave(LBS_DEB_CS);
439
440
441
442
443
444
	return IRQ_HANDLED;
}




445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/********************************************************************/
/* Firmware                                                         */
/********************************************************************/

/*
 * Tries to program the helper firmware.
 *
 * Return 0 on success
 */
static int if_cs_prog_helper(struct if_cs_card *card)
{
	int ret = 0;
	int sent = 0;
	u8  scratch;
	const struct firmware *fw;

	lbs_deb_enter(LBS_DEB_CS);

	scratch = if_cs_read8(card, IF_CS_SCRATCH);

	/* "If the value is 0x5a, the firmware is already
	 * downloaded successfully"
	 */
	if (scratch == 0x5a)
		goto done;

	/* "If the value is != 00, it is invalid value of register */
	if (scratch != 0x00) {
		ret = -ENODEV;
		goto done;
	}

	/* TODO: make firmware file configurable */
	ret = request_firmware(&fw, "libertas_cs_helper.fw",
		&handle_to_dev(card->p_dev));
	if (ret) {
		lbs_pr_err("can't load helper firmware\n");
		ret = -ENODEV;
		goto done;
	}
485
	lbs_deb_cs("helper size %td\n", fw->size);
486
487
488
489
490
491
492
493
494
495
496
497
498
499

	/* "Set the 5 bytes of the helper image to 0" */
	/* Not needed, this contains an ARM branch instruction */

	for (;;) {
		/* "the number of bytes to send is 256" */
		int count = 256;
		int remain = fw->size - sent;

		if (remain < count)
			count = remain;

		/* "write the number of bytes to be sent to the I/O Command
		 * write length register" */
500
		if_cs_write16(card, IF_CS_HOST_CMD_LEN, count);
501
502
503

		/* "write this to I/O Command port register as 16 bit writes */
		if (count)
504
			if_cs_write16_rep(card, IF_CS_HOST_CMD,
505
506
507
508
509
				&fw->data[sent],
				count >> 1);

		/* "Assert the download over interrupt command in the Host
		 * status register" */
510
		if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
511
512
513

		/* "Assert the download over interrupt command in the Card
		 * interrupt case register" */
514
		if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
515
516
517

		/* "The host polls the Card Status register ... for 50 ms before
		   declaring a failure */
518
519
		ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
			IF_CS_BIT_COMMAND);
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
		if (ret < 0) {
			lbs_pr_err("can't download helper at 0x%x, ret %d\n",
				sent, ret);
			goto done;
		}

		if (count == 0)
			break;

		sent += count;
	}

	release_firmware(fw);
	ret = 0;

done:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


static int if_cs_prog_real(struct if_cs_card *card)
{
	const struct firmware *fw;
	int ret = 0;
	int retry = 0;
	int len = 0;
	int sent;

	lbs_deb_enter(LBS_DEB_CS);

	/* TODO: make firmware file configurable */
	ret = request_firmware(&fw, "libertas_cs.fw",
		&handle_to_dev(card->p_dev));
	if (ret) {
		lbs_pr_err("can't load firmware\n");
		ret = -ENODEV;
		goto done;
	}
559
	lbs_deb_cs("fw size %td\n", fw->size);
560

561
562
	ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_SQ_READ_LOW,
		IF_CS_CARD_SQ_HELPER_OK);
563
564
565
566
567
568
	if (ret < 0) {
		lbs_pr_err("helper firmware doesn't answer\n");
		goto err_release;
	}

	for (sent = 0; sent < fw->size; sent += len) {
569
		len = if_cs_read16(card, IF_CS_CARD_SQ_READ_LOW);
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
		if (len & 1) {
			retry++;
			lbs_pr_info("odd, need to retry this firmware block\n");
		} else {
			retry = 0;
		}

		if (retry > 20) {
			lbs_pr_err("could not download firmware\n");
			ret = -ENODEV;
			goto err_release;
		}
		if (retry) {
			sent -= len;
		}


587
		if_cs_write16(card, IF_CS_HOST_CMD_LEN, len);
588

589
		if_cs_write16_rep(card, IF_CS_HOST_CMD,
590
591
			&fw->data[sent],
			(len+1) >> 1);
592
593
		if_cs_write8(card, IF_CS_HOST_STATUS, IF_CS_BIT_COMMAND);
		if_cs_write16(card, IF_CS_HOST_INT_CAUSE, IF_CS_BIT_COMMAND);
594

595
596
		ret = if_cs_poll_while_fw_download(card, IF_CS_CARD_STATUS,
			IF_CS_BIT_COMMAND);
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
		if (ret < 0) {
			lbs_pr_err("can't download firmware at 0x%x\n", sent);
			goto err_release;
		}
	}

	ret = if_cs_poll_while_fw_download(card, IF_CS_SCRATCH, 0x5a);
	if (ret < 0) {
		lbs_pr_err("firmware download failed\n");
		goto err_release;
	}

	ret = 0;
	goto done;


err_release:
	release_firmware(fw);

done:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}



/********************************************************************/
/* Callback functions for libertas.ko                               */
/********************************************************************/

/* Send commands or data packets to the card */
628
629
630
631
static int if_cs_host_to_card(struct lbs_private *priv,
	u8 type,
	u8 *buf,
	u16 nb)
632
633
634
635
636
637
638
{
	int ret = -1;

	lbs_deb_enter_args(LBS_DEB_CS, "type %d, bytes %d", type, nb);

	switch (type) {
	case MVMS_DAT:
639
		priv->dnld_sent = DNLD_DATA_SENT;
640
641
642
643
		if_cs_send_data(priv, buf, nb);
		ret = 0;
		break;
	case MVMS_CMD:
644
		priv->dnld_sent = DNLD_CMD_SENT;
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
		ret = if_cs_send_cmd(priv, buf, nb);
		break;
	default:
		lbs_pr_err("%s: unsupported type %d\n", __FUNCTION__, type);
	}

	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


/********************************************************************/
/* Card Services                                                    */
/********************************************************************/

/*
 * After a card is removed, if_cs_release() will unregister the
 * device, and release the PCMCIA configuration.  If the device is
 * still open, this will be postponed until it is closed.
 */
static void if_cs_release(struct pcmcia_device *p_dev)
{
	struct if_cs_card *card = p_dev->priv;

	lbs_deb_enter(LBS_DEB_CS);

	free_irq(p_dev->irq.AssignedIRQ, card);
672
	pcmcia_disable_device(p_dev);
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
	if (card->iobase)
		ioport_unmap(card->iobase);

	lbs_deb_leave(LBS_DEB_CS);
}


/*
 * This creates an "instance" of the driver, allocating local data
 * structures for one device.  The device is registered with Card
 * Services.
 *
 * The dev_link structure is initialized, but we don't actually
 * configure the card at this point -- we wait until we receive a card
 * insertion event.
 */
static int if_cs_probe(struct pcmcia_device *p_dev)
{
	int ret = -ENOMEM;
692
	struct lbs_private *priv;
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
	struct if_cs_card *card;
	/* CIS parsing */
	tuple_t tuple;
	cisparse_t parse;
	cistpl_cftable_entry_t *cfg = &parse.cftable_entry;
	cistpl_io_t *io = &cfg->io;
	u_char buf[64];

	lbs_deb_enter(LBS_DEB_CS);

	card = kzalloc(sizeof(struct if_cs_card), GFP_KERNEL);
	if (!card) {
		lbs_pr_err("error in kzalloc\n");
		goto out;
	}
	card->p_dev = p_dev;
	p_dev->priv = card;

	p_dev->irq.Attributes = IRQ_TYPE_DYNAMIC_SHARING;
	p_dev->irq.Handler = NULL;
	p_dev->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;

	p_dev->conf.Attributes = 0;
	p_dev->conf.IntType = INT_MEMORY_AND_IO;

	tuple.Attributes = 0;
	tuple.TupleData = buf;
	tuple.TupleDataMax = sizeof(buf);
	tuple.TupleOffset = 0;

	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
	if ((ret = pcmcia_get_first_tuple(p_dev, &tuple)) != 0 ||
	    (ret = pcmcia_get_tuple_data(p_dev, &tuple)) != 0 ||
	    (ret = pcmcia_parse_tuple(p_dev, &tuple, &parse)) != 0)
	{
		lbs_pr_err("error in pcmcia_get_first_tuple etc\n");
		goto out1;
	}

	p_dev->conf.ConfigIndex = cfg->index;

	/* Do we need to allocate an interrupt? */
	if (cfg->irq.IRQInfo1) {
		p_dev->conf.Attributes |= CONF_ENABLE_IRQ;
	}

	/* IO window settings */
	if (cfg->io.nwin != 1) {
		lbs_pr_err("wrong CIS (check number of IO windows)\n");
		ret = -ENODEV;
		goto out1;
	}
	p_dev->io.Attributes1 = IO_DATA_PATH_WIDTH_AUTO;
	p_dev->io.BasePort1 = io->win[0].base;
	p_dev->io.NumPorts1 = io->win[0].len;

	/* This reserves IO space but doesn't actually enable it */
	ret = pcmcia_request_io(p_dev, &p_dev->io);
	if (ret) {
		lbs_pr_err("error in pcmcia_request_io\n");
		goto out1;
	}

	/*
	 * Allocate an interrupt line.  Note that this does not assign
	 * a handler to the interrupt, unless the 'Handler' member of
	 * the irq structure is initialized.
	 */
	if (p_dev->conf.Attributes & CONF_ENABLE_IRQ) {
		ret = pcmcia_request_irq(p_dev, &p_dev->irq);
		if (ret) {
			lbs_pr_err("error in pcmcia_request_irq\n");
			goto out1;
		}
	}

	/* Initialize io access */
	card->iobase = ioport_map(p_dev->io.BasePort1, p_dev->io.NumPorts1);
	if (!card->iobase) {
		lbs_pr_err("error in ioport_map\n");
		ret = -EIO;
		goto out1;
	}

	/*
	 * This actually configures the PCMCIA socket -- setting up
	 * the I/O windows and the interrupt mapping, and putting the
	 * card and host interface into "Memory and IO" mode.
	 */
	ret = pcmcia_request_configuration(p_dev, &p_dev->conf);
	if (ret) {
		lbs_pr_err("error in pcmcia_request_configuration\n");
		goto out2;
	}

	/* Finally, report what we've done */
	lbs_deb_cs("irq %d, io 0x%04x-0x%04x\n",
	       p_dev->irq.AssignedIRQ, p_dev->io.BasePort1,
	       p_dev->io.BasePort1 + p_dev->io.NumPorts1 - 1);


	/* Load the firmware early, before calling into libertas.ko */
	ret = if_cs_prog_helper(card);
	if (ret == 0)
		ret = if_cs_prog_real(card);
	if (ret)
		goto out2;

	/* Make this card known to the libertas driver */
802
	priv = lbs_add_card(card, &p_dev->dev);
803
804
805
806
807
	if (!priv) {
		ret = -ENOMEM;
		goto out2;
	}

808
	/* Finish setting up fields in lbs_private */
809
	card->priv = priv;
810
	priv->card = card;
811
	priv->hw_host_to_card = if_cs_host_to_card;
812
	priv->fw_ready = 1;
813

814
815
816
817
818
819
820
821
	/* Now actually get the IRQ */
	ret = request_irq(p_dev->irq.AssignedIRQ, if_cs_interrupt,
		IRQF_SHARED, DRV_NAME, card);
	if (ret) {
		lbs_pr_err("error in request_irq\n");
		goto out3;
	}

822
823
	/* Clear any interrupt cause that happend while sending
	 * firmware/initializing card */
824
	if_cs_write16(card, IF_CS_CARD_INT_CAUSE, IF_CS_BIT_MASK);
825
826
	if_cs_enable_ints(card);

827
	/* And finally bring the card up */
828
	if (lbs_start_card(priv) != 0) {
829
830
831
832
		lbs_pr_err("could not activate card\n");
		goto out3;
	}

833
834
835
	/* The firmware for the CF card supports powersave */
	priv->ps_supported = 1;

836
837
838
839
	ret = 0;
	goto out;

out3:
840
	lbs_remove_card(priv);
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
out2:
	ioport_unmap(card->iobase);
out1:
	pcmcia_disable_device(p_dev);
out:
	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);
	return ret;
}


/*
 * This deletes a driver "instance".  The device is de-registered with
 * Card Services.  If it has been released, all local data structures
 * are freed.  Otherwise, the structures will be freed when the device
 * is released.
 */
static void if_cs_detach(struct pcmcia_device *p_dev)
{
	struct if_cs_card *card = p_dev->priv;

	lbs_deb_enter(LBS_DEB_CS);

863
864
	lbs_stop_card(card->priv);
	lbs_remove_card(card->priv);
865
	if_cs_disable_ints(card);
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
	if_cs_release(p_dev);
	kfree(card);

	lbs_deb_leave(LBS_DEB_CS);
}



/********************************************************************/
/* Module initialization                                            */
/********************************************************************/

static struct pcmcia_device_id if_cs_ids[] = {
	PCMCIA_DEVICE_MANF_CARD(0x02df, 0x8103),
	PCMCIA_DEVICE_NULL,
};
MODULE_DEVICE_TABLE(pcmcia, if_cs_ids);


885
static struct pcmcia_driver lbs_driver = {
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
	.owner		= THIS_MODULE,
	.drv		= {
		.name	= DRV_NAME,
	},
	.probe		= if_cs_probe,
	.remove		= if_cs_detach,
	.id_table       = if_cs_ids,
};


static int __init if_cs_init(void)
{
	int ret;

	lbs_deb_enter(LBS_DEB_CS);
901
	ret = pcmcia_register_driver(&lbs_driver);
902
903
904
905
906
907
908
909
	lbs_deb_leave(LBS_DEB_CS);
	return ret;
}


static void __exit if_cs_exit(void)
{
	lbs_deb_enter(LBS_DEB_CS);
910
	pcmcia_unregister_driver(&lbs_driver);
911
912
913
914
915
916
	lbs_deb_leave(LBS_DEB_CS);
}


module_init(if_cs_init);
module_exit(if_cs_exit);