1 /*
2  *
3  *  Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)
4  *
5  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
6  *
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 version 2 as
10  *  published by the Free Software Foundation;
11  *
12  *  Software distributed under the License is distributed on an "AS
13  *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14  *  implied. See the License for the specific language governing
15  *  rights and limitations under the License.
16  *
17  *  The initial developer of the original code is David A. Hinds
18  *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
19  *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
20  *
21  */
22 
23 #include <linux/module.h>
24 
25 #include <linux/kernel.h>
26 #include <linux/init.h>
27 #include <linux/slab.h>
28 #include <linux/types.h>
29 #include <linux/sched.h>
30 #include <linux/delay.h>
31 #include <linux/timer.h>
32 #include <linux/errno.h>
33 #include <linux/ptrace.h>
34 #include <linux/ioport.h>
35 #include <linux/spinlock.h>
36 #include <linux/moduleparam.h>
37 #include <linux/wait.h>
38 
39 #include <linux/skbuff.h>
40 #include <linux/io.h>
41 
42 #include <pcmcia/cistpl.h>
43 #include <pcmcia/ciscode.h>
44 #include <pcmcia/ds.h>
45 #include <pcmcia/cisreg.h>
46 
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
49 
50 
51 
52 /* ======================== Module parameters ======================== */
53 
54 
55 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
56 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
57 MODULE_LICENSE("GPL");
58 
59 
60 
61 /* ======================== Local structures ======================== */
62 
63 
64 typedef struct bluecard_info_t {
65 	struct pcmcia_device *p_dev;
66 
67 	struct hci_dev *hdev;
68 
69 	spinlock_t lock;		/* For serializing operations */
70 	struct timer_list timer;	/* For LED control */
71 
72 	struct sk_buff_head txq;
73 	unsigned long tx_state;
74 
75 	unsigned long rx_state;
76 	unsigned long rx_count;
77 	struct sk_buff *rx_skb;
78 
79 	unsigned char ctrl_reg;
80 	unsigned long hw_state;		/* Status of the hardware and LED control */
81 } bluecard_info_t;
82 
83 
84 static int bluecard_config(struct pcmcia_device *link);
85 static void bluecard_release(struct pcmcia_device *link);
86 
87 static void bluecard_detach(struct pcmcia_device *p_dev);
88 
89 
90 /* Default baud rate: 57600, 115200, 230400 or 460800 */
91 #define DEFAULT_BAUD_RATE  230400
92 
93 
94 /* Hardware states */
95 #define CARD_READY             1
96 #define CARD_HAS_PCCARD_ID     4
97 #define CARD_HAS_POWER_LED     5
98 #define CARD_HAS_ACTIVITY_LED  6
99 
100 /* Transmit states  */
101 #define XMIT_SENDING         1
102 #define XMIT_WAKEUP          2
103 #define XMIT_BUFFER_NUMBER   5	/* unset = buffer one, set = buffer two */
104 #define XMIT_BUF_ONE_READY   6
105 #define XMIT_BUF_TWO_READY   7
106 #define XMIT_SENDING_READY   8
107 
108 /* Receiver states */
109 #define RECV_WAIT_PACKET_TYPE   0
110 #define RECV_WAIT_EVENT_HEADER  1
111 #define RECV_WAIT_ACL_HEADER    2
112 #define RECV_WAIT_SCO_HEADER    3
113 #define RECV_WAIT_DATA          4
114 
115 /* Special packet types */
116 #define PKT_BAUD_RATE_57600   0x80
117 #define PKT_BAUD_RATE_115200  0x81
118 #define PKT_BAUD_RATE_230400  0x82
119 #define PKT_BAUD_RATE_460800  0x83
120 
121 
122 /* These are the register offsets */
123 #define REG_COMMAND     0x20
124 #define REG_INTERRUPT   0x21
125 #define REG_CONTROL     0x22
126 #define REG_RX_CONTROL  0x24
127 #define REG_CARD_RESET  0x30
128 #define REG_LED_CTRL    0x30
129 
130 /* REG_COMMAND */
131 #define REG_COMMAND_TX_BUF_ONE  0x01
132 #define REG_COMMAND_TX_BUF_TWO  0x02
133 #define REG_COMMAND_RX_BUF_ONE  0x04
134 #define REG_COMMAND_RX_BUF_TWO  0x08
135 #define REG_COMMAND_RX_WIN_ONE  0x00
136 #define REG_COMMAND_RX_WIN_TWO  0x10
137 
138 /* REG_CONTROL */
139 #define REG_CONTROL_BAUD_RATE_57600   0x00
140 #define REG_CONTROL_BAUD_RATE_115200  0x01
141 #define REG_CONTROL_BAUD_RATE_230400  0x02
142 #define REG_CONTROL_BAUD_RATE_460800  0x03
143 #define REG_CONTROL_RTS               0x04
144 #define REG_CONTROL_BT_ON             0x08
145 #define REG_CONTROL_BT_RESET          0x10
146 #define REG_CONTROL_BT_RES_PU         0x20
147 #define REG_CONTROL_INTERRUPT         0x40
148 #define REG_CONTROL_CARD_RESET        0x80
149 
150 /* REG_RX_CONTROL */
151 #define RTS_LEVEL_SHIFT_BITS  0x02
152 
153 
154 
155 /* ======================== LED handling routines ======================== */
156 
157 
158 static void bluecard_activity_led_timeout(u_long arg)
159 {
160 	bluecard_info_t *info = (bluecard_info_t *)arg;
161 	unsigned int iobase = info->p_dev->resource[0]->start;
162 
163 	if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
164 		return;
165 
166 	if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
167 		/* Disable activity LED */
168 		outb(0x08 | 0x20, iobase + 0x30);
169 	} else {
170 		/* Disable power LED */
171 		outb(0x00, iobase + 0x30);
172 	}
173 }
174 
175 
176 static void bluecard_enable_activity_led(bluecard_info_t *info)
177 {
178 	unsigned int iobase = info->p_dev->resource[0]->start;
179 
180 	if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
181 		return;
182 
183 	if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
184 		/* Enable activity LED */
185 		outb(0x10 | 0x40, iobase + 0x30);
186 
187 		/* Stop the LED after HZ/4 */
188 		mod_timer(&(info->timer), jiffies + HZ / 4);
189 	} else {
190 		/* Enable power LED */
191 		outb(0x08 | 0x20, iobase + 0x30);
192 
193 		/* Stop the LED after HZ/2 */
194 		mod_timer(&(info->timer), jiffies + HZ / 2);
195 	}
196 }
197 
198 
199 
200 /* ======================== Interrupt handling ======================== */
201 
202 
203 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
204 {
205 	int i, actual;
206 
207 	actual = (len > 15) ? 15 : len;
208 
209 	outb_p(actual, iobase + offset);
210 
211 	for (i = 0; i < actual; i++)
212 		outb_p(buf[i], iobase + offset + i + 1);
213 
214 	return actual;
215 }
216 
217 
218 static void bluecard_write_wakeup(bluecard_info_t *info)
219 {
220 	if (!info) {
221 		BT_ERR("Unknown device");
222 		return;
223 	}
224 
225 	if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
226 		return;
227 
228 	if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
229 		set_bit(XMIT_WAKEUP, &(info->tx_state));
230 		return;
231 	}
232 
233 	do {
234 		unsigned int iobase = info->p_dev->resource[0]->start;
235 		unsigned int offset;
236 		unsigned char command;
237 		unsigned long ready_bit;
238 		register struct sk_buff *skb;
239 		int len;
240 
241 		clear_bit(XMIT_WAKEUP, &(info->tx_state));
242 
243 		if (!pcmcia_dev_present(info->p_dev))
244 			return;
245 
246 		if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
247 			if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
248 				break;
249 			offset = 0x10;
250 			command = REG_COMMAND_TX_BUF_TWO;
251 			ready_bit = XMIT_BUF_TWO_READY;
252 		} else {
253 			if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
254 				break;
255 			offset = 0x00;
256 			command = REG_COMMAND_TX_BUF_ONE;
257 			ready_bit = XMIT_BUF_ONE_READY;
258 		}
259 
260 		if (!(skb = skb_dequeue(&(info->txq))))
261 			break;
262 
263 		if (bt_cb(skb)->pkt_type & 0x80) {
264 			/* Disable RTS */
265 			info->ctrl_reg |= REG_CONTROL_RTS;
266 			outb(info->ctrl_reg, iobase + REG_CONTROL);
267 		}
268 
269 		/* Activate LED */
270 		bluecard_enable_activity_led(info);
271 
272 		/* Send frame */
273 		len = bluecard_write(iobase, offset, skb->data, skb->len);
274 
275 		/* Tell the FPGA to send the data */
276 		outb_p(command, iobase + REG_COMMAND);
277 
278 		/* Mark the buffer as dirty */
279 		clear_bit(ready_bit, &(info->tx_state));
280 
281 		if (bt_cb(skb)->pkt_type & 0x80) {
282 			DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq);
283 			DEFINE_WAIT(wait);
284 
285 			unsigned char baud_reg;
286 
287 			switch (bt_cb(skb)->pkt_type) {
288 			case PKT_BAUD_RATE_460800:
289 				baud_reg = REG_CONTROL_BAUD_RATE_460800;
290 				break;
291 			case PKT_BAUD_RATE_230400:
292 				baud_reg = REG_CONTROL_BAUD_RATE_230400;
293 				break;
294 			case PKT_BAUD_RATE_115200:
295 				baud_reg = REG_CONTROL_BAUD_RATE_115200;
296 				break;
297 			case PKT_BAUD_RATE_57600:
298 				/* Fall through... */
299 			default:
300 				baud_reg = REG_CONTROL_BAUD_RATE_57600;
301 				break;
302 			}
303 
304 			/* Wait until the command reaches the baseband */
305 			prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
306 			schedule_timeout(HZ/10);
307 			finish_wait(&wq, &wait);
308 
309 			/* Set baud on baseband */
310 			info->ctrl_reg &= ~0x03;
311 			info->ctrl_reg |= baud_reg;
312 			outb(info->ctrl_reg, iobase + REG_CONTROL);
313 
314 			/* Enable RTS */
315 			info->ctrl_reg &= ~REG_CONTROL_RTS;
316 			outb(info->ctrl_reg, iobase + REG_CONTROL);
317 
318 			/* Wait before the next HCI packet can be send */
319 			prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
320 			schedule_timeout(HZ);
321 			finish_wait(&wq, &wait);
322 		}
323 
324 		if (len == skb->len) {
325 			kfree_skb(skb);
326 		} else {
327 			skb_pull(skb, len);
328 			skb_queue_head(&(info->txq), skb);
329 		}
330 
331 		info->hdev->stat.byte_tx += len;
332 
333 		/* Change buffer */
334 		change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
335 
336 	} while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
337 
338 	clear_bit(XMIT_SENDING, &(info->tx_state));
339 }
340 
341 
342 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
343 {
344 	int i, n, len;
345 
346 	outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
347 
348 	len = inb(iobase + offset);
349 	n = 0;
350 	i = 1;
351 
352 	while (n < len) {
353 
354 		if (i == 16) {
355 			outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
356 			i = 0;
357 		}
358 
359 		buf[n] = inb(iobase + offset + i);
360 
361 		n++;
362 		i++;
363 
364 	}
365 
366 	return len;
367 }
368 
369 
370 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
371 {
372 	unsigned int iobase;
373 	unsigned char buf[31];
374 	int i, len;
375 
376 	if (!info) {
377 		BT_ERR("Unknown device");
378 		return;
379 	}
380 
381 	iobase = info->p_dev->resource[0]->start;
382 
383 	if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
384 		bluecard_enable_activity_led(info);
385 
386 	len = bluecard_read(iobase, offset, buf, sizeof(buf));
387 
388 	for (i = 0; i < len; i++) {
389 
390 		/* Allocate packet */
391 		if (info->rx_skb == NULL) {
392 			info->rx_state = RECV_WAIT_PACKET_TYPE;
393 			info->rx_count = 0;
394 			if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
395 				BT_ERR("Can't allocate mem for new packet");
396 				return;
397 			}
398 		}
399 
400 		if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
401 
402 			bt_cb(info->rx_skb)->pkt_type = buf[i];
403 
404 			switch (bt_cb(info->rx_skb)->pkt_type) {
405 
406 			case 0x00:
407 				/* init packet */
408 				if (offset != 0x00) {
409 					set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
410 					set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
411 					set_bit(XMIT_SENDING_READY, &(info->tx_state));
412 					bluecard_write_wakeup(info);
413 				}
414 
415 				kfree_skb(info->rx_skb);
416 				info->rx_skb = NULL;
417 				break;
418 
419 			case HCI_EVENT_PKT:
420 				info->rx_state = RECV_WAIT_EVENT_HEADER;
421 				info->rx_count = HCI_EVENT_HDR_SIZE;
422 				break;
423 
424 			case HCI_ACLDATA_PKT:
425 				info->rx_state = RECV_WAIT_ACL_HEADER;
426 				info->rx_count = HCI_ACL_HDR_SIZE;
427 				break;
428 
429 			case HCI_SCODATA_PKT:
430 				info->rx_state = RECV_WAIT_SCO_HEADER;
431 				info->rx_count = HCI_SCO_HDR_SIZE;
432 				break;
433 
434 			default:
435 				/* unknown packet */
436 				BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
437 				info->hdev->stat.err_rx++;
438 
439 				kfree_skb(info->rx_skb);
440 				info->rx_skb = NULL;
441 				break;
442 
443 			}
444 
445 		} else {
446 
447 			*skb_put(info->rx_skb, 1) = buf[i];
448 			info->rx_count--;
449 
450 			if (info->rx_count == 0) {
451 
452 				int dlen;
453 				struct hci_event_hdr *eh;
454 				struct hci_acl_hdr *ah;
455 				struct hci_sco_hdr *sh;
456 
457 				switch (info->rx_state) {
458 
459 				case RECV_WAIT_EVENT_HEADER:
460 					eh = hci_event_hdr(info->rx_skb);
461 					info->rx_state = RECV_WAIT_DATA;
462 					info->rx_count = eh->plen;
463 					break;
464 
465 				case RECV_WAIT_ACL_HEADER:
466 					ah = hci_acl_hdr(info->rx_skb);
467 					dlen = __le16_to_cpu(ah->dlen);
468 					info->rx_state = RECV_WAIT_DATA;
469 					info->rx_count = dlen;
470 					break;
471 
472 				case RECV_WAIT_SCO_HEADER:
473 					sh = hci_sco_hdr(info->rx_skb);
474 					info->rx_state = RECV_WAIT_DATA;
475 					info->rx_count = sh->dlen;
476 					break;
477 
478 				case RECV_WAIT_DATA:
479 					hci_recv_frame(info->hdev, info->rx_skb);
480 					info->rx_skb = NULL;
481 					break;
482 
483 				}
484 
485 			}
486 
487 		}
488 
489 
490 	}
491 
492 	info->hdev->stat.byte_rx += len;
493 }
494 
495 
496 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst)
497 {
498 	bluecard_info_t *info = dev_inst;
499 	unsigned int iobase;
500 	unsigned char reg;
501 
502 	if (!info || !info->hdev)
503 		/* our irq handler is shared */
504 		return IRQ_NONE;
505 
506 	if (!test_bit(CARD_READY, &(info->hw_state)))
507 		return IRQ_HANDLED;
508 
509 	iobase = info->p_dev->resource[0]->start;
510 
511 	spin_lock(&(info->lock));
512 
513 	/* Disable interrupt */
514 	info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
515 	outb(info->ctrl_reg, iobase + REG_CONTROL);
516 
517 	reg = inb(iobase + REG_INTERRUPT);
518 
519 	if ((reg != 0x00) && (reg != 0xff)) {
520 
521 		if (reg & 0x04) {
522 			bluecard_receive(info, 0x00);
523 			outb(0x04, iobase + REG_INTERRUPT);
524 			outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
525 		}
526 
527 		if (reg & 0x08) {
528 			bluecard_receive(info, 0x10);
529 			outb(0x08, iobase + REG_INTERRUPT);
530 			outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
531 		}
532 
533 		if (reg & 0x01) {
534 			set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
535 			outb(0x01, iobase + REG_INTERRUPT);
536 			bluecard_write_wakeup(info);
537 		}
538 
539 		if (reg & 0x02) {
540 			set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
541 			outb(0x02, iobase + REG_INTERRUPT);
542 			bluecard_write_wakeup(info);
543 		}
544 
545 	}
546 
547 	/* Enable interrupt */
548 	info->ctrl_reg |= REG_CONTROL_INTERRUPT;
549 	outb(info->ctrl_reg, iobase + REG_CONTROL);
550 
551 	spin_unlock(&(info->lock));
552 
553 	return IRQ_HANDLED;
554 }
555 
556 
557 
558 /* ======================== Device specific HCI commands ======================== */
559 
560 
561 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
562 {
563 	bluecard_info_t *info = hci_get_drvdata(hdev);
564 	struct sk_buff *skb;
565 
566 	/* Ericsson baud rate command */
567 	unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
568 
569 	if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
570 		BT_ERR("Can't allocate mem for new packet");
571 		return -1;
572 	}
573 
574 	switch (baud) {
575 	case 460800:
576 		cmd[4] = 0x00;
577 		bt_cb(skb)->pkt_type = PKT_BAUD_RATE_460800;
578 		break;
579 	case 230400:
580 		cmd[4] = 0x01;
581 		bt_cb(skb)->pkt_type = PKT_BAUD_RATE_230400;
582 		break;
583 	case 115200:
584 		cmd[4] = 0x02;
585 		bt_cb(skb)->pkt_type = PKT_BAUD_RATE_115200;
586 		break;
587 	case 57600:
588 		/* Fall through... */
589 	default:
590 		cmd[4] = 0x03;
591 		bt_cb(skb)->pkt_type = PKT_BAUD_RATE_57600;
592 		break;
593 	}
594 
595 	memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
596 
597 	skb_queue_tail(&(info->txq), skb);
598 
599 	bluecard_write_wakeup(info);
600 
601 	return 0;
602 }
603 
604 
605 
606 /* ======================== HCI interface ======================== */
607 
608 
609 static int bluecard_hci_flush(struct hci_dev *hdev)
610 {
611 	bluecard_info_t *info = hci_get_drvdata(hdev);
612 
613 	/* Drop TX queue */
614 	skb_queue_purge(&(info->txq));
615 
616 	return 0;
617 }
618 
619 
620 static int bluecard_hci_open(struct hci_dev *hdev)
621 {
622 	bluecard_info_t *info = hci_get_drvdata(hdev);
623 
624 	if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
625 		bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
626 
627 	if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
628 		return 0;
629 
630 	if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
631 		unsigned int iobase = info->p_dev->resource[0]->start;
632 
633 		/* Enable LED */
634 		outb(0x08 | 0x20, iobase + 0x30);
635 	}
636 
637 	return 0;
638 }
639 
640 
641 static int bluecard_hci_close(struct hci_dev *hdev)
642 {
643 	bluecard_info_t *info = hci_get_drvdata(hdev);
644 
645 	if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
646 		return 0;
647 
648 	bluecard_hci_flush(hdev);
649 
650 	if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
651 		unsigned int iobase = info->p_dev->resource[0]->start;
652 
653 		/* Disable LED */
654 		outb(0x00, iobase + 0x30);
655 	}
656 
657 	return 0;
658 }
659 
660 
661 static int bluecard_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
662 {
663 	bluecard_info_t *info = hci_get_drvdata(hdev);
664 
665 	switch (bt_cb(skb)->pkt_type) {
666 	case HCI_COMMAND_PKT:
667 		hdev->stat.cmd_tx++;
668 		break;
669 	case HCI_ACLDATA_PKT:
670 		hdev->stat.acl_tx++;
671 		break;
672 	case HCI_SCODATA_PKT:
673 		hdev->stat.sco_tx++;
674 		break;
675 	}
676 
677 	/* Prepend skb with frame type */
678 	memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
679 	skb_queue_tail(&(info->txq), skb);
680 
681 	bluecard_write_wakeup(info);
682 
683 	return 0;
684 }
685 
686 
687 
688 /* ======================== Card services HCI interaction ======================== */
689 
690 
691 static int bluecard_open(bluecard_info_t *info)
692 {
693 	unsigned int iobase = info->p_dev->resource[0]->start;
694 	struct hci_dev *hdev;
695 	unsigned char id;
696 
697 	spin_lock_init(&(info->lock));
698 
699 	init_timer(&(info->timer));
700 	info->timer.function = &bluecard_activity_led_timeout;
701 	info->timer.data = (u_long)info;
702 
703 	skb_queue_head_init(&(info->txq));
704 
705 	info->rx_state = RECV_WAIT_PACKET_TYPE;
706 	info->rx_count = 0;
707 	info->rx_skb = NULL;
708 
709 	/* Initialize HCI device */
710 	hdev = hci_alloc_dev();
711 	if (!hdev) {
712 		BT_ERR("Can't allocate HCI device");
713 		return -ENOMEM;
714 	}
715 
716 	info->hdev = hdev;
717 
718 	hdev->bus = HCI_PCCARD;
719 	hci_set_drvdata(hdev, info);
720 	SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
721 
722 	hdev->open  = bluecard_hci_open;
723 	hdev->close = bluecard_hci_close;
724 	hdev->flush = bluecard_hci_flush;
725 	hdev->send  = bluecard_hci_send_frame;
726 
727 	id = inb(iobase + 0x30);
728 
729 	if ((id & 0x0f) == 0x02)
730 		set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
731 
732 	if (id & 0x10)
733 		set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
734 
735 	if (id & 0x20)
736 		set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
737 
738 	/* Reset card */
739 	info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
740 	outb(info->ctrl_reg, iobase + REG_CONTROL);
741 
742 	/* Turn FPGA off */
743 	outb(0x80, iobase + 0x30);
744 
745 	/* Wait some time */
746 	msleep(10);
747 
748 	/* Turn FPGA on */
749 	outb(0x00, iobase + 0x30);
750 
751 	/* Activate card */
752 	info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
753 	outb(info->ctrl_reg, iobase + REG_CONTROL);
754 
755 	/* Enable interrupt */
756 	outb(0xff, iobase + REG_INTERRUPT);
757 	info->ctrl_reg |= REG_CONTROL_INTERRUPT;
758 	outb(info->ctrl_reg, iobase + REG_CONTROL);
759 
760 	if ((id & 0x0f) == 0x03) {
761 		/* Disable RTS */
762 		info->ctrl_reg |= REG_CONTROL_RTS;
763 		outb(info->ctrl_reg, iobase + REG_CONTROL);
764 
765 		/* Set baud rate */
766 		info->ctrl_reg |= 0x03;
767 		outb(info->ctrl_reg, iobase + REG_CONTROL);
768 
769 		/* Enable RTS */
770 		info->ctrl_reg &= ~REG_CONTROL_RTS;
771 		outb(info->ctrl_reg, iobase + REG_CONTROL);
772 
773 		set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
774 		set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
775 		set_bit(XMIT_SENDING_READY, &(info->tx_state));
776 	}
777 
778 	/* Start the RX buffers */
779 	outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
780 	outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
781 
782 	/* Signal that the hardware is ready */
783 	set_bit(CARD_READY, &(info->hw_state));
784 
785 	/* Drop TX queue */
786 	skb_queue_purge(&(info->txq));
787 
788 	/* Control the point at which RTS is enabled */
789 	outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
790 
791 	/* Timeout before it is safe to send the first HCI packet */
792 	msleep(1250);
793 
794 	/* Register HCI device */
795 	if (hci_register_dev(hdev) < 0) {
796 		BT_ERR("Can't register HCI device");
797 		info->hdev = NULL;
798 		hci_free_dev(hdev);
799 		return -ENODEV;
800 	}
801 
802 	return 0;
803 }
804 
805 
806 static int bluecard_close(bluecard_info_t *info)
807 {
808 	unsigned int iobase = info->p_dev->resource[0]->start;
809 	struct hci_dev *hdev = info->hdev;
810 
811 	if (!hdev)
812 		return -ENODEV;
813 
814 	bluecard_hci_close(hdev);
815 
816 	clear_bit(CARD_READY, &(info->hw_state));
817 
818 	/* Reset card */
819 	info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
820 	outb(info->ctrl_reg, iobase + REG_CONTROL);
821 
822 	/* Turn FPGA off */
823 	outb(0x80, iobase + 0x30);
824 
825 	hci_unregister_dev(hdev);
826 	hci_free_dev(hdev);
827 
828 	return 0;
829 }
830 
831 static int bluecard_probe(struct pcmcia_device *link)
832 {
833 	bluecard_info_t *info;
834 
835 	/* Create new info device */
836 	info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
837 	if (!info)
838 		return -ENOMEM;
839 
840 	info->p_dev = link;
841 	link->priv = info;
842 
843 	link->config_flags |= CONF_ENABLE_IRQ;
844 
845 	return bluecard_config(link);
846 }
847 
848 
849 static void bluecard_detach(struct pcmcia_device *link)
850 {
851 	bluecard_release(link);
852 }
853 
854 
855 static int bluecard_config(struct pcmcia_device *link)
856 {
857 	bluecard_info_t *info = link->priv;
858 	int i, n;
859 
860 	link->config_index = 0x20;
861 
862 	link->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
863 	link->resource[0]->end = 64;
864 	link->io_lines = 6;
865 
866 	for (n = 0; n < 0x400; n += 0x40) {
867 		link->resource[0]->start = n ^ 0x300;
868 		i = pcmcia_request_io(link);
869 		if (i == 0)
870 			break;
871 	}
872 
873 	if (i != 0)
874 		goto failed;
875 
876 	i = pcmcia_request_irq(link, bluecard_interrupt);
877 	if (i != 0)
878 		goto failed;
879 
880 	i = pcmcia_enable_device(link);
881 	if (i != 0)
882 		goto failed;
883 
884 	if (bluecard_open(info) != 0)
885 		goto failed;
886 
887 	return 0;
888 
889 failed:
890 	bluecard_release(link);
891 	return -ENODEV;
892 }
893 
894 
895 static void bluecard_release(struct pcmcia_device *link)
896 {
897 	bluecard_info_t *info = link->priv;
898 
899 	bluecard_close(info);
900 
901 	del_timer(&(info->timer));
902 
903 	pcmcia_disable_device(link);
904 }
905 
906 static const struct pcmcia_device_id bluecard_ids[] = {
907 	PCMCIA_DEVICE_PROD_ID12("BlueCard", "LSE041", 0xbaf16fbf, 0x657cc15e),
908 	PCMCIA_DEVICE_PROD_ID12("BTCFCARD", "LSE139", 0xe3987764, 0x2524b59c),
909 	PCMCIA_DEVICE_PROD_ID12("WSS", "LSE039", 0x0a0736ec, 0x24e6dfab),
910 	PCMCIA_DEVICE_NULL
911 };
912 MODULE_DEVICE_TABLE(pcmcia, bluecard_ids);
913 
914 static struct pcmcia_driver bluecard_driver = {
915 	.owner		= THIS_MODULE,
916 	.name		= "bluecard_cs",
917 	.probe		= bluecard_probe,
918 	.remove		= bluecard_detach,
919 	.id_table	= bluecard_ids,
920 };
921 module_pcmcia_driver(bluecard_driver);
922