xref: /openbmc/linux/drivers/bluetooth/bt3c_cs.c (revision 1da177e4c3f41524e886b7f1b8a0c1fc7321cac2)
1 /*
2  *
3  *  Driver for the 3Com Bluetooth PCMCIA card
4  *
5  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
6  *                           Jose Orlando Pereira <jop@di.uminho.pt>
7  *
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License version 2 as
11  *  published by the Free Software Foundation;
12  *
13  *  Software distributed under the License is distributed on an "AS
14  *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15  *  implied. See the License for the specific language governing
16  *  rights and limitations under the License.
17  *
18  *  The initial developer of the original code is David A. Hinds
19  *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
20  *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
21  *
22  */
23 
24 #include <linux/config.h>
25 #include <linux/module.h>
26 
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/sched.h>
32 #include <linux/delay.h>
33 #include <linux/errno.h>
34 #include <linux/ptrace.h>
35 #include <linux/ioport.h>
36 #include <linux/spinlock.h>
37 #include <linux/moduleparam.h>
38 
39 #include <linux/skbuff.h>
40 #include <linux/string.h>
41 #include <linux/serial.h>
42 #include <linux/serial_reg.h>
43 #include <linux/bitops.h>
44 #include <asm/system.h>
45 #include <asm/io.h>
46 
47 #include <linux/device.h>
48 #include <linux/firmware.h>
49 
50 #include <pcmcia/version.h>
51 #include <pcmcia/cs_types.h>
52 #include <pcmcia/cs.h>
53 #include <pcmcia/cistpl.h>
54 #include <pcmcia/ciscode.h>
55 #include <pcmcia/ds.h>
56 #include <pcmcia/cisreg.h>
57 
58 #include <net/bluetooth/bluetooth.h>
59 #include <net/bluetooth/hci_core.h>
60 
61 
62 
63 /* ======================== Module parameters ======================== */
64 
65 
66 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
67 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
68 MODULE_LICENSE("GPL");
69 
70 
71 
72 /* ======================== Local structures ======================== */
73 
74 
75 typedef struct bt3c_info_t {
76 	dev_link_t link;
77 	dev_node_t node;
78 
79 	struct hci_dev *hdev;
80 
81 	spinlock_t lock;		/* For serializing operations */
82 
83 	struct sk_buff_head txq;
84 	unsigned long tx_state;
85 
86 	unsigned long rx_state;
87 	unsigned long rx_count;
88 	struct sk_buff *rx_skb;
89 } bt3c_info_t;
90 
91 
92 static void bt3c_config(dev_link_t *link);
93 static void bt3c_release(dev_link_t *link);
94 static int bt3c_event(event_t event, int priority, event_callback_args_t *args);
95 
96 static dev_info_t dev_info = "bt3c_cs";
97 
98 static dev_link_t *bt3c_attach(void);
99 static void bt3c_detach(dev_link_t *);
100 
101 static dev_link_t *dev_list = NULL;
102 
103 
104 /* Transmit states  */
105 #define XMIT_SENDING  1
106 #define XMIT_WAKEUP   2
107 #define XMIT_WAITING  8
108 
109 /* Receiver states */
110 #define RECV_WAIT_PACKET_TYPE   0
111 #define RECV_WAIT_EVENT_HEADER  1
112 #define RECV_WAIT_ACL_HEADER    2
113 #define RECV_WAIT_SCO_HEADER    3
114 #define RECV_WAIT_DATA          4
115 
116 
117 
118 /* ======================== Special I/O functions ======================== */
119 
120 
121 #define DATA_L   0
122 #define DATA_H   1
123 #define ADDR_L   2
124 #define ADDR_H   3
125 #define CONTROL  4
126 
127 
128 static inline void bt3c_address(unsigned int iobase, unsigned short addr)
129 {
130 	outb(addr & 0xff, iobase + ADDR_L);
131 	outb((addr >> 8) & 0xff, iobase + ADDR_H);
132 }
133 
134 
135 static inline void bt3c_put(unsigned int iobase, unsigned short value)
136 {
137 	outb(value & 0xff, iobase + DATA_L);
138 	outb((value >> 8) & 0xff, iobase + DATA_H);
139 }
140 
141 
142 static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
143 {
144 	bt3c_address(iobase, addr);
145 	bt3c_put(iobase, value);
146 }
147 
148 
149 static inline unsigned short bt3c_get(unsigned int iobase)
150 {
151 	unsigned short value = inb(iobase + DATA_L);
152 
153 	value |= inb(iobase + DATA_H) << 8;
154 
155 	return value;
156 }
157 
158 
159 static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
160 {
161 	bt3c_address(iobase, addr);
162 
163 	return bt3c_get(iobase);
164 }
165 
166 
167 
168 /* ======================== Interrupt handling ======================== */
169 
170 
171 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
172 {
173 	int actual = 0;
174 
175 	bt3c_address(iobase, 0x7080);
176 
177 	/* Fill FIFO with current frame */
178 	while (actual < len) {
179 		/* Transmit next byte */
180 		bt3c_put(iobase, buf[actual]);
181 		actual++;
182 	}
183 
184 	bt3c_io_write(iobase, 0x7005, actual);
185 
186 	return actual;
187 }
188 
189 
190 static void bt3c_write_wakeup(bt3c_info_t *info)
191 {
192 	if (!info) {
193 		BT_ERR("Unknown device");
194 		return;
195 	}
196 
197 	if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
198 		return;
199 
200 	do {
201 		register unsigned int iobase = info->link.io.BasePort1;
202 		register struct sk_buff *skb;
203 		register int len;
204 
205 		if (!(info->link.state & DEV_PRESENT))
206 			break;
207 
208 
209 		if (!(skb = skb_dequeue(&(info->txq)))) {
210 			clear_bit(XMIT_SENDING, &(info->tx_state));
211 			break;
212 		}
213 
214 		/* Send frame */
215 		len = bt3c_write(iobase, 256, skb->data, skb->len);
216 
217 		if (len != skb->len) {
218 			BT_ERR("Very strange");
219 		}
220 
221 		kfree_skb(skb);
222 
223 		info->hdev->stat.byte_tx += len;
224 
225 	} while (0);
226 }
227 
228 
229 static void bt3c_receive(bt3c_info_t *info)
230 {
231 	unsigned int iobase;
232 	int size = 0, avail;
233 
234 	if (!info) {
235 		BT_ERR("Unknown device");
236 		return;
237 	}
238 
239 	iobase = info->link.io.BasePort1;
240 
241 	avail = bt3c_read(iobase, 0x7006);
242 	//printk("bt3c_cs: receiving %d bytes\n", avail);
243 
244 	bt3c_address(iobase, 0x7480);
245 	while (size < avail) {
246 		size++;
247 		info->hdev->stat.byte_rx++;
248 
249 		/* Allocate packet */
250 		if (info->rx_skb == NULL) {
251 			info->rx_state = RECV_WAIT_PACKET_TYPE;
252 			info->rx_count = 0;
253 			if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
254 				BT_ERR("Can't allocate mem for new packet");
255 				return;
256 			}
257 		}
258 
259 
260 		if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
261 
262 			info->rx_skb->dev = (void *) info->hdev;
263 			info->rx_skb->pkt_type = inb(iobase + DATA_L);
264 			inb(iobase + DATA_H);
265 			//printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
266 
267 			switch (info->rx_skb->pkt_type) {
268 
269 			case HCI_EVENT_PKT:
270 				info->rx_state = RECV_WAIT_EVENT_HEADER;
271 				info->rx_count = HCI_EVENT_HDR_SIZE;
272 				break;
273 
274 			case HCI_ACLDATA_PKT:
275 				info->rx_state = RECV_WAIT_ACL_HEADER;
276 				info->rx_count = HCI_ACL_HDR_SIZE;
277 				break;
278 
279 			case HCI_SCODATA_PKT:
280 				info->rx_state = RECV_WAIT_SCO_HEADER;
281 				info->rx_count = HCI_SCO_HDR_SIZE;
282 				break;
283 
284 			default:
285 				/* Unknown packet */
286 				BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
287 				info->hdev->stat.err_rx++;
288 				clear_bit(HCI_RUNNING, &(info->hdev->flags));
289 
290 				kfree_skb(info->rx_skb);
291 				info->rx_skb = NULL;
292 				break;
293 
294 			}
295 
296 		} else {
297 
298 			__u8 x = inb(iobase + DATA_L);
299 
300 			*skb_put(info->rx_skb, 1) = x;
301 			inb(iobase + DATA_H);
302 			info->rx_count--;
303 
304 			if (info->rx_count == 0) {
305 
306 				int dlen;
307 				struct hci_event_hdr *eh;
308 				struct hci_acl_hdr *ah;
309 				struct hci_sco_hdr *sh;
310 
311 				switch (info->rx_state) {
312 
313 				case RECV_WAIT_EVENT_HEADER:
314 					eh = (struct hci_event_hdr *)(info->rx_skb->data);
315 					info->rx_state = RECV_WAIT_DATA;
316 					info->rx_count = eh->plen;
317 					break;
318 
319 				case RECV_WAIT_ACL_HEADER:
320 					ah = (struct hci_acl_hdr *)(info->rx_skb->data);
321 					dlen = __le16_to_cpu(ah->dlen);
322 					info->rx_state = RECV_WAIT_DATA;
323 					info->rx_count = dlen;
324 					break;
325 
326 				case RECV_WAIT_SCO_HEADER:
327 					sh = (struct hci_sco_hdr *)(info->rx_skb->data);
328 					info->rx_state = RECV_WAIT_DATA;
329 					info->rx_count = sh->dlen;
330 					break;
331 
332 				case RECV_WAIT_DATA:
333 					hci_recv_frame(info->rx_skb);
334 					info->rx_skb = NULL;
335 					break;
336 
337 				}
338 
339 			}
340 
341 		}
342 
343 	}
344 
345 	bt3c_io_write(iobase, 0x7006, 0x0000);
346 }
347 
348 
349 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
350 {
351 	bt3c_info_t *info = dev_inst;
352 	unsigned int iobase;
353 	int iir;
354 
355 	if (!info || !info->hdev) {
356 		BT_ERR("Call of irq %d for unknown device", irq);
357 		return IRQ_NONE;
358 	}
359 
360 	iobase = info->link.io.BasePort1;
361 
362 	spin_lock(&(info->lock));
363 
364 	iir = inb(iobase + CONTROL);
365 	if (iir & 0x80) {
366 		int stat = bt3c_read(iobase, 0x7001);
367 
368 		if ((stat & 0xff) == 0x7f) {
369 			BT_ERR("Very strange (stat=0x%04x)", stat);
370 		} else if ((stat & 0xff) != 0xff) {
371 			if (stat & 0x0020) {
372 				int stat = bt3c_read(iobase, 0x7002) & 0x10;
373 				BT_INFO("%s: Antenna %s", info->hdev->name,
374 							stat ? "out" : "in");
375 			}
376 			if (stat & 0x0001)
377 				bt3c_receive(info);
378 			if (stat & 0x0002) {
379 				//BT_ERR("Ack (stat=0x%04x)", stat);
380 				clear_bit(XMIT_SENDING, &(info->tx_state));
381 				bt3c_write_wakeup(info);
382 			}
383 
384 			bt3c_io_write(iobase, 0x7001, 0x0000);
385 
386 			outb(iir, iobase + CONTROL);
387 		}
388 	}
389 
390 	spin_unlock(&(info->lock));
391 
392 	return IRQ_HANDLED;
393 }
394 
395 
396 
397 /* ======================== HCI interface ======================== */
398 
399 
400 static int bt3c_hci_flush(struct hci_dev *hdev)
401 {
402 	bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
403 
404 	/* Drop TX queue */
405 	skb_queue_purge(&(info->txq));
406 
407 	return 0;
408 }
409 
410 
411 static int bt3c_hci_open(struct hci_dev *hdev)
412 {
413 	set_bit(HCI_RUNNING, &(hdev->flags));
414 
415 	return 0;
416 }
417 
418 
419 static int bt3c_hci_close(struct hci_dev *hdev)
420 {
421 	if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
422 		return 0;
423 
424 	bt3c_hci_flush(hdev);
425 
426 	return 0;
427 }
428 
429 
430 static int bt3c_hci_send_frame(struct sk_buff *skb)
431 {
432 	bt3c_info_t *info;
433 	struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
434 	unsigned long flags;
435 
436 	if (!hdev) {
437 		BT_ERR("Frame for unknown HCI device (hdev=NULL)");
438 		return -ENODEV;
439 	}
440 
441 	info = (bt3c_info_t *) (hdev->driver_data);
442 
443 	switch (skb->pkt_type) {
444 	case HCI_COMMAND_PKT:
445 		hdev->stat.cmd_tx++;
446 		break;
447 	case HCI_ACLDATA_PKT:
448 		hdev->stat.acl_tx++;
449 		break;
450 	case HCI_SCODATA_PKT:
451 		hdev->stat.sco_tx++;
452 		break;
453 	};
454 
455 	/* Prepend skb with frame type */
456 	memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
457 	skb_queue_tail(&(info->txq), skb);
458 
459 	spin_lock_irqsave(&(info->lock), flags);
460 
461 	bt3c_write_wakeup(info);
462 
463 	spin_unlock_irqrestore(&(info->lock), flags);
464 
465 	return 0;
466 }
467 
468 
469 static void bt3c_hci_destruct(struct hci_dev *hdev)
470 {
471 }
472 
473 
474 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
475 {
476 	return -ENOIOCTLCMD;
477 }
478 
479 
480 
481 /* ======================== Card services HCI interaction ======================== */
482 
483 
484 static struct device *bt3c_device(void)
485 {
486 	static struct device dev = {
487 		.bus_id = "pcmcia",
488 	};
489 	kobject_set_name(&dev.kobj, "bt3c");
490 	kobject_init(&dev.kobj);
491 
492 	return &dev;
493 }
494 
495 
496 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
497 {
498 	char *ptr = (char *) firmware;
499 	char b[9];
500 	unsigned int iobase, size, addr, fcs, tmp;
501 	int i, err = 0;
502 
503 	iobase = info->link.io.BasePort1;
504 
505 	/* Reset */
506 	bt3c_io_write(iobase, 0x8040, 0x0404);
507 	bt3c_io_write(iobase, 0x8040, 0x0400);
508 
509 	udelay(1);
510 
511 	bt3c_io_write(iobase, 0x8040, 0x0404);
512 
513 	udelay(17);
514 
515 	/* Load */
516 	while (count) {
517 		if (ptr[0] != 'S') {
518 			BT_ERR("Bad address in firmware");
519 			err = -EFAULT;
520 			goto error;
521 		}
522 
523 		memset(b, 0, sizeof(b));
524 		memcpy(b, ptr + 2, 2);
525 		size = simple_strtol(b, NULL, 16);
526 
527 		memset(b, 0, sizeof(b));
528 		memcpy(b, ptr + 4, 8);
529 		addr = simple_strtol(b, NULL, 16);
530 
531 		memset(b, 0, sizeof(b));
532 		memcpy(b, ptr + (size * 2) + 2, 2);
533 		fcs = simple_strtol(b, NULL, 16);
534 
535 		memset(b, 0, sizeof(b));
536 		for (tmp = 0, i = 0; i < size; i++) {
537 			memcpy(b, ptr + (i * 2) + 2, 2);
538 			tmp += simple_strtol(b, NULL, 16);
539 		}
540 
541 		if (((tmp + fcs) & 0xff) != 0xff) {
542 			BT_ERR("Checksum error in firmware");
543 			err = -EILSEQ;
544 			goto error;
545 		}
546 
547 		if (ptr[1] == '3') {
548 			bt3c_address(iobase, addr);
549 
550 			memset(b, 0, sizeof(b));
551 			for (i = 0; i < (size - 4) / 2; i++) {
552 				memcpy(b, ptr + (i * 4) + 12, 4);
553 				tmp = simple_strtol(b, NULL, 16);
554 				bt3c_put(iobase, tmp);
555 			}
556 		}
557 
558 		ptr   += (size * 2) + 6;
559 		count -= (size * 2) + 6;
560 	}
561 
562 	udelay(17);
563 
564 	/* Boot */
565 	bt3c_address(iobase, 0x3000);
566 	outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
567 
568 error:
569 	udelay(17);
570 
571 	/* Clear */
572 	bt3c_io_write(iobase, 0x7006, 0x0000);
573 	bt3c_io_write(iobase, 0x7005, 0x0000);
574 	bt3c_io_write(iobase, 0x7001, 0x0000);
575 
576 	return err;
577 }
578 
579 
580 static int bt3c_open(bt3c_info_t *info)
581 {
582 	const struct firmware *firmware;
583 	struct hci_dev *hdev;
584 	int err;
585 
586 	spin_lock_init(&(info->lock));
587 
588 	skb_queue_head_init(&(info->txq));
589 
590 	info->rx_state = RECV_WAIT_PACKET_TYPE;
591 	info->rx_count = 0;
592 	info->rx_skb = NULL;
593 
594 	/* Initialize HCI device */
595 	hdev = hci_alloc_dev();
596 	if (!hdev) {
597 		BT_ERR("Can't allocate HCI device");
598 		return -ENOMEM;
599 	}
600 
601 	info->hdev = hdev;
602 
603 	hdev->type = HCI_PCCARD;
604 	hdev->driver_data = info;
605 
606 	hdev->open     = bt3c_hci_open;
607 	hdev->close    = bt3c_hci_close;
608 	hdev->flush    = bt3c_hci_flush;
609 	hdev->send     = bt3c_hci_send_frame;
610 	hdev->destruct = bt3c_hci_destruct;
611 	hdev->ioctl    = bt3c_hci_ioctl;
612 
613 	hdev->owner = THIS_MODULE;
614 
615 	/* Load firmware */
616 	err = request_firmware(&firmware, "BT3CPCC.bin", bt3c_device());
617 	if (err < 0) {
618 		BT_ERR("Firmware request failed");
619 		goto error;
620 	}
621 
622 	err = bt3c_load_firmware(info, firmware->data, firmware->size);
623 
624 	release_firmware(firmware);
625 
626 	if (err < 0) {
627 		BT_ERR("Firmware loading failed");
628 		goto error;
629 	}
630 
631 	/* Timeout before it is safe to send the first HCI packet */
632 	msleep(1000);
633 
634 	/* Register HCI device */
635 	err = hci_register_dev(hdev);
636 	if (err < 0) {
637 		BT_ERR("Can't register HCI device");
638 		goto error;
639 	}
640 
641 	return 0;
642 
643 error:
644 	info->hdev = NULL;
645 	hci_free_dev(hdev);
646 	return err;
647 }
648 
649 
650 static int bt3c_close(bt3c_info_t *info)
651 {
652 	struct hci_dev *hdev = info->hdev;
653 
654 	if (!hdev)
655 		return -ENODEV;
656 
657 	bt3c_hci_close(hdev);
658 
659 	if (hci_unregister_dev(hdev) < 0)
660 		BT_ERR("Can't unregister HCI device %s", hdev->name);
661 
662 	hci_free_dev(hdev);
663 
664 	return 0;
665 }
666 
667 static dev_link_t *bt3c_attach(void)
668 {
669 	bt3c_info_t *info;
670 	client_reg_t client_reg;
671 	dev_link_t *link;
672 	int ret;
673 
674 	/* Create new info device */
675 	info = kmalloc(sizeof(*info), GFP_KERNEL);
676 	if (!info)
677 		return NULL;
678 	memset(info, 0, sizeof(*info));
679 
680 	link = &info->link;
681 	link->priv = info;
682 
683 	link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
684 	link->io.NumPorts1 = 8;
685 	link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
686 	link->irq.IRQInfo1 = IRQ_LEVEL_ID;
687 
688 	link->irq.Handler = bt3c_interrupt;
689 	link->irq.Instance = info;
690 
691 	link->conf.Attributes = CONF_ENABLE_IRQ;
692 	link->conf.Vcc = 50;
693 	link->conf.IntType = INT_MEMORY_AND_IO;
694 
695 	/* Register with Card Services */
696 	link->next = dev_list;
697 	dev_list = link;
698 	client_reg.dev_info = &dev_info;
699 	client_reg.EventMask =
700 	    CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
701 	    CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
702 	    CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
703 	client_reg.event_handler = &bt3c_event;
704 	client_reg.Version = 0x0210;
705 	client_reg.event_callback_args.client_data = link;
706 
707 	ret = pcmcia_register_client(&link->handle, &client_reg);
708 	if (ret != CS_SUCCESS) {
709 		cs_error(link->handle, RegisterClient, ret);
710 		bt3c_detach(link);
711 		return NULL;
712 	}
713 
714 	return link;
715 }
716 
717 
718 static void bt3c_detach(dev_link_t *link)
719 {
720 	bt3c_info_t *info = link->priv;
721 	dev_link_t **linkp;
722 	int ret;
723 
724 	/* Locate device structure */
725 	for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
726 		if (*linkp == link)
727 			break;
728 
729 	if (*linkp == NULL)
730 		return;
731 
732 	if (link->state & DEV_CONFIG)
733 		bt3c_release(link);
734 
735 	if (link->handle) {
736 		ret = pcmcia_deregister_client(link->handle);
737 		if (ret != CS_SUCCESS)
738 			cs_error(link->handle, DeregisterClient, ret);
739 	}
740 
741 	/* Unlink device structure, free bits */
742 	*linkp = link->next;
743 
744 	kfree(info);
745 }
746 
747 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
748 {
749 	int i;
750 
751 	i = pcmcia_get_tuple_data(handle, tuple);
752 	if (i != CS_SUCCESS)
753 		return i;
754 
755 	return pcmcia_parse_tuple(handle, tuple, parse);
756 }
757 
758 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
759 {
760 	if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
761 		return CS_NO_MORE_ITEMS;
762 	return get_tuple(handle, tuple, parse);
763 }
764 
765 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
766 {
767 	if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
768 		return CS_NO_MORE_ITEMS;
769 	return get_tuple(handle, tuple, parse);
770 }
771 
772 static void bt3c_config(dev_link_t *link)
773 {
774 	static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
775 	client_handle_t handle = link->handle;
776 	bt3c_info_t *info = link->priv;
777 	tuple_t tuple;
778 	u_short buf[256];
779 	cisparse_t parse;
780 	cistpl_cftable_entry_t *cf = &parse.cftable_entry;
781 	config_info_t config;
782 	int i, j, try, last_ret, last_fn;
783 
784 	tuple.TupleData = (cisdata_t *)buf;
785 	tuple.TupleOffset = 0;
786 	tuple.TupleDataMax = 255;
787 	tuple.Attributes = 0;
788 
789 	/* Get configuration register information */
790 	tuple.DesiredTuple = CISTPL_CONFIG;
791 	last_ret = first_tuple(handle, &tuple, &parse);
792 	if (last_ret != CS_SUCCESS) {
793 		last_fn = ParseTuple;
794 		goto cs_failed;
795 	}
796 	link->conf.ConfigBase = parse.config.base;
797 	link->conf.Present = parse.config.rmask[0];
798 
799 	/* Configure card */
800 	link->state |= DEV_CONFIG;
801 	i = pcmcia_get_configuration_info(handle, &config);
802 	link->conf.Vcc = config.Vcc;
803 
804 	/* First pass: look for a config entry that looks normal. */
805 	tuple.TupleData = (cisdata_t *)buf;
806 	tuple.TupleOffset = 0;
807 	tuple.TupleDataMax = 255;
808 	tuple.Attributes = 0;
809 	tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
810 	/* Two tries: without IO aliases, then with aliases */
811 	for (try = 0; try < 2; try++) {
812 		i = first_tuple(handle, &tuple, &parse);
813 		while (i != CS_NO_MORE_ITEMS) {
814 			if (i != CS_SUCCESS)
815 				goto next_entry;
816 			if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
817 				link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
818 			if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
819 				link->conf.ConfigIndex = cf->index;
820 				link->io.BasePort1 = cf->io.win[0].base;
821 				link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
822 				i = pcmcia_request_io(link->handle, &link->io);
823 				if (i == CS_SUCCESS)
824 					goto found_port;
825 			}
826 next_entry:
827 			i = next_tuple(handle, &tuple, &parse);
828 		}
829 	}
830 
831 	/* Second pass: try to find an entry that isn't picky about
832 	   its base address, then try to grab any standard serial port
833 	   address, and finally try to get any free port. */
834 	i = first_tuple(handle, &tuple, &parse);
835 	while (i != CS_NO_MORE_ITEMS) {
836 		if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
837 			link->conf.ConfigIndex = cf->index;
838 			for (j = 0; j < 5; j++) {
839 				link->io.BasePort1 = base[j];
840 				link->io.IOAddrLines = base[j] ? 16 : 3;
841 				i = pcmcia_request_io(link->handle, &link->io);
842 				if (i == CS_SUCCESS)
843 					goto found_port;
844 			}
845 		}
846 		i = next_tuple(handle, &tuple, &parse);
847 	}
848 
849 found_port:
850 	if (i != CS_SUCCESS) {
851 		BT_ERR("No usable port range found");
852 		cs_error(link->handle, RequestIO, i);
853 		goto failed;
854 	}
855 
856 	i = pcmcia_request_irq(link->handle, &link->irq);
857 	if (i != CS_SUCCESS) {
858 		cs_error(link->handle, RequestIRQ, i);
859 		link->irq.AssignedIRQ = 0;
860 	}
861 
862 	i = pcmcia_request_configuration(link->handle, &link->conf);
863 	if (i != CS_SUCCESS) {
864 		cs_error(link->handle, RequestConfiguration, i);
865 		goto failed;
866 	}
867 
868 	if (bt3c_open(info) != 0)
869 		goto failed;
870 
871 	strcpy(info->node.dev_name, info->hdev->name);
872 	link->dev = &info->node;
873 	link->state &= ~DEV_CONFIG_PENDING;
874 
875 	return;
876 
877 cs_failed:
878 	cs_error(link->handle, last_fn, last_ret);
879 
880 failed:
881 	bt3c_release(link);
882 }
883 
884 
885 static void bt3c_release(dev_link_t *link)
886 {
887 	bt3c_info_t *info = link->priv;
888 
889 	if (link->state & DEV_PRESENT)
890 		bt3c_close(info);
891 
892 	link->dev = NULL;
893 
894 	pcmcia_release_configuration(link->handle);
895 	pcmcia_release_io(link->handle, &link->io);
896 	pcmcia_release_irq(link->handle, &link->irq);
897 
898 	link->state &= ~DEV_CONFIG;
899 }
900 
901 
902 static int bt3c_event(event_t event, int priority, event_callback_args_t *args)
903 {
904 	dev_link_t *link = args->client_data;
905 	bt3c_info_t *info = link->priv;
906 
907 	switch (event) {
908 	case CS_EVENT_CARD_REMOVAL:
909 		link->state &= ~DEV_PRESENT;
910 		if (link->state & DEV_CONFIG) {
911 			bt3c_close(info);
912 			bt3c_release(link);
913 		}
914 		break;
915 	case CS_EVENT_CARD_INSERTION:
916 		link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
917 		bt3c_config(link);
918 		break;
919 	case CS_EVENT_PM_SUSPEND:
920 		link->state |= DEV_SUSPEND;
921 		/* Fall through... */
922 	case CS_EVENT_RESET_PHYSICAL:
923 		if (link->state & DEV_CONFIG)
924 			pcmcia_release_configuration(link->handle);
925 		break;
926 	case CS_EVENT_PM_RESUME:
927 		link->state &= ~DEV_SUSPEND;
928 		/* Fall through... */
929 	case CS_EVENT_CARD_RESET:
930 		if (DEV_OK(link))
931 			pcmcia_request_configuration(link->handle, &link->conf);
932 		break;
933 	}
934 
935 	return 0;
936 }
937 
938 static struct pcmcia_driver bt3c_driver = {
939 	.owner		= THIS_MODULE,
940 	.drv		= {
941 		.name	= "bt3c_cs",
942 	},
943 	.attach		= bt3c_attach,
944 	.detach		= bt3c_detach,
945 };
946 
947 static int __init init_bt3c_cs(void)
948 {
949 	return pcmcia_register_driver(&bt3c_driver);
950 }
951 
952 
953 static void __exit exit_bt3c_cs(void)
954 {
955 	pcmcia_unregister_driver(&bt3c_driver);
956 	BUG_ON(dev_list != NULL);
957 }
958 
959 module_init(init_bt3c_cs);
960 module_exit(exit_bt3c_cs);
961