[ALSA] ad1848 - Add PM support
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / bluetooth / bluecard_cs.c
blobf36c563d72c4f49f8334ee8e6a9ee3c8793f51a9
1 /*
3 * Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)
5 * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org>
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;
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.
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.
23 #include <linux/config.h>
24 #include <linux/module.h>
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/sched.h>
31 #include <linux/delay.h>
32 #include <linux/timer.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 #include <linux/wait.h>
40 #include <linux/skbuff.h>
41 #include <asm/io.h>
43 #include <pcmcia/cs_types.h>
44 #include <pcmcia/cs.h>
45 #include <pcmcia/cistpl.h>
46 #include <pcmcia/ciscode.h>
47 #include <pcmcia/ds.h>
48 #include <pcmcia/cisreg.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
55 /* ======================== Module parameters ======================== */
58 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
59 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
60 MODULE_LICENSE("GPL");
64 /* ======================== Local structures ======================== */
67 typedef struct bluecard_info_t {
68 dev_link_t link;
69 dev_node_t node;
71 struct hci_dev *hdev;
73 spinlock_t lock; /* For serializing operations */
74 struct timer_list timer; /* For LED control */
76 struct sk_buff_head txq;
77 unsigned long tx_state;
79 unsigned long rx_state;
80 unsigned long rx_count;
81 struct sk_buff *rx_skb;
83 unsigned char ctrl_reg;
84 unsigned long hw_state; /* Status of the hardware and LED control */
85 } bluecard_info_t;
88 static void bluecard_config(dev_link_t *link);
89 static void bluecard_release(dev_link_t *link);
90 static int bluecard_event(event_t event, int priority, event_callback_args_t *args);
92 static dev_info_t dev_info = "bluecard_cs";
94 static dev_link_t *bluecard_attach(void);
95 static void bluecard_detach(dev_link_t *);
97 static dev_link_t *dev_list = NULL;
100 /* Default baud rate: 57600, 115200, 230400 or 460800 */
101 #define DEFAULT_BAUD_RATE 230400
104 /* Hardware states */
105 #define CARD_READY 1
106 #define CARD_HAS_PCCARD_ID 4
107 #define CARD_HAS_POWER_LED 5
108 #define CARD_HAS_ACTIVITY_LED 6
110 /* Transmit states */
111 #define XMIT_SENDING 1
112 #define XMIT_WAKEUP 2
113 #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
114 #define XMIT_BUF_ONE_READY 6
115 #define XMIT_BUF_TWO_READY 7
116 #define XMIT_SENDING_READY 8
118 /* Receiver states */
119 #define RECV_WAIT_PACKET_TYPE 0
120 #define RECV_WAIT_EVENT_HEADER 1
121 #define RECV_WAIT_ACL_HEADER 2
122 #define RECV_WAIT_SCO_HEADER 3
123 #define RECV_WAIT_DATA 4
125 /* Special packet types */
126 #define PKT_BAUD_RATE_57600 0x80
127 #define PKT_BAUD_RATE_115200 0x81
128 #define PKT_BAUD_RATE_230400 0x82
129 #define PKT_BAUD_RATE_460800 0x83
132 /* These are the register offsets */
133 #define REG_COMMAND 0x20
134 #define REG_INTERRUPT 0x21
135 #define REG_CONTROL 0x22
136 #define REG_RX_CONTROL 0x24
137 #define REG_CARD_RESET 0x30
138 #define REG_LED_CTRL 0x30
140 /* REG_COMMAND */
141 #define REG_COMMAND_TX_BUF_ONE 0x01
142 #define REG_COMMAND_TX_BUF_TWO 0x02
143 #define REG_COMMAND_RX_BUF_ONE 0x04
144 #define REG_COMMAND_RX_BUF_TWO 0x08
145 #define REG_COMMAND_RX_WIN_ONE 0x00
146 #define REG_COMMAND_RX_WIN_TWO 0x10
148 /* REG_CONTROL */
149 #define REG_CONTROL_BAUD_RATE_57600 0x00
150 #define REG_CONTROL_BAUD_RATE_115200 0x01
151 #define REG_CONTROL_BAUD_RATE_230400 0x02
152 #define REG_CONTROL_BAUD_RATE_460800 0x03
153 #define REG_CONTROL_RTS 0x04
154 #define REG_CONTROL_BT_ON 0x08
155 #define REG_CONTROL_BT_RESET 0x10
156 #define REG_CONTROL_BT_RES_PU 0x20
157 #define REG_CONTROL_INTERRUPT 0x40
158 #define REG_CONTROL_CARD_RESET 0x80
160 /* REG_RX_CONTROL */
161 #define RTS_LEVEL_SHIFT_BITS 0x02
165 /* ======================== LED handling routines ======================== */
168 static void bluecard_activity_led_timeout(u_long arg)
170 bluecard_info_t *info = (bluecard_info_t *)arg;
171 unsigned int iobase = info->link.io.BasePort1;
173 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
174 return;
176 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
177 /* Disable activity LED */
178 outb(0x08 | 0x20, iobase + 0x30);
179 } else {
180 /* Disable power LED */
181 outb(0x00, iobase + 0x30);
186 static void bluecard_enable_activity_led(bluecard_info_t *info)
188 unsigned int iobase = info->link.io.BasePort1;
190 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
191 return;
193 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
194 /* Enable activity LED */
195 outb(0x10 | 0x40, iobase + 0x30);
197 /* Stop the LED after HZ/4 */
198 mod_timer(&(info->timer), jiffies + HZ / 4);
199 } else {
200 /* Enable power LED */
201 outb(0x08 | 0x20, iobase + 0x30);
203 /* Stop the LED after HZ/2 */
204 mod_timer(&(info->timer), jiffies + HZ / 2);
210 /* ======================== Interrupt handling ======================== */
213 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
215 int i, actual;
217 actual = (len > 15) ? 15 : len;
219 outb_p(actual, iobase + offset);
221 for (i = 0; i < actual; i++)
222 outb_p(buf[i], iobase + offset + i + 1);
224 return actual;
228 static void bluecard_write_wakeup(bluecard_info_t *info)
230 if (!info) {
231 BT_ERR("Unknown device");
232 return;
235 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
236 return;
238 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
239 set_bit(XMIT_WAKEUP, &(info->tx_state));
240 return;
243 do {
244 register unsigned int iobase = info->link.io.BasePort1;
245 register unsigned int offset;
246 register unsigned char command;
247 register unsigned long ready_bit;
248 register struct sk_buff *skb;
249 register int len;
251 clear_bit(XMIT_WAKEUP, &(info->tx_state));
253 if (!(info->link.state & DEV_PRESENT))
254 return;
256 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
257 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
258 break;
259 offset = 0x10;
260 command = REG_COMMAND_TX_BUF_TWO;
261 ready_bit = XMIT_BUF_TWO_READY;
262 } else {
263 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
264 break;
265 offset = 0x00;
266 command = REG_COMMAND_TX_BUF_ONE;
267 ready_bit = XMIT_BUF_ONE_READY;
270 if (!(skb = skb_dequeue(&(info->txq))))
271 break;
273 if (bt_cb(skb)->pkt_type & 0x80) {
274 /* Disable RTS */
275 info->ctrl_reg |= REG_CONTROL_RTS;
276 outb(info->ctrl_reg, iobase + REG_CONTROL);
279 /* Activate LED */
280 bluecard_enable_activity_led(info);
282 /* Send frame */
283 len = bluecard_write(iobase, offset, skb->data, skb->len);
285 /* Tell the FPGA to send the data */
286 outb_p(command, iobase + REG_COMMAND);
288 /* Mark the buffer as dirty */
289 clear_bit(ready_bit, &(info->tx_state));
291 if (bt_cb(skb)->pkt_type & 0x80) {
292 DECLARE_WAIT_QUEUE_HEAD(wq);
293 DEFINE_WAIT(wait);
295 unsigned char baud_reg;
297 switch (bt_cb(skb)->pkt_type) {
298 case PKT_BAUD_RATE_460800:
299 baud_reg = REG_CONTROL_BAUD_RATE_460800;
300 break;
301 case PKT_BAUD_RATE_230400:
302 baud_reg = REG_CONTROL_BAUD_RATE_230400;
303 break;
304 case PKT_BAUD_RATE_115200:
305 baud_reg = REG_CONTROL_BAUD_RATE_115200;
306 break;
307 case PKT_BAUD_RATE_57600:
308 /* Fall through... */
309 default:
310 baud_reg = REG_CONTROL_BAUD_RATE_57600;
311 break;
314 /* Wait until the command reaches the baseband */
315 prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
316 schedule_timeout(HZ/10);
317 finish_wait(&wq, &wait);
319 /* Set baud on baseband */
320 info->ctrl_reg &= ~0x03;
321 info->ctrl_reg |= baud_reg;
322 outb(info->ctrl_reg, iobase + REG_CONTROL);
324 /* Enable RTS */
325 info->ctrl_reg &= ~REG_CONTROL_RTS;
326 outb(info->ctrl_reg, iobase + REG_CONTROL);
328 /* Wait before the next HCI packet can be send */
329 prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
330 schedule_timeout(HZ);
331 finish_wait(&wq, &wait);
334 if (len == skb->len) {
335 kfree_skb(skb);
336 } else {
337 skb_pull(skb, len);
338 skb_queue_head(&(info->txq), skb);
341 info->hdev->stat.byte_tx += len;
343 /* Change buffer */
344 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
346 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
348 clear_bit(XMIT_SENDING, &(info->tx_state));
352 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
354 int i, n, len;
356 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
358 len = inb(iobase + offset);
359 n = 0;
360 i = 1;
362 while (n < len) {
364 if (i == 16) {
365 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
366 i = 0;
369 buf[n] = inb(iobase + offset + i);
371 n++;
372 i++;
376 return len;
380 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
382 unsigned int iobase;
383 unsigned char buf[31];
384 int i, len;
386 if (!info) {
387 BT_ERR("Unknown device");
388 return;
391 iobase = info->link.io.BasePort1;
393 if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
394 bluecard_enable_activity_led(info);
396 len = bluecard_read(iobase, offset, buf, sizeof(buf));
398 for (i = 0; i < len; i++) {
400 /* Allocate packet */
401 if (info->rx_skb == NULL) {
402 info->rx_state = RECV_WAIT_PACKET_TYPE;
403 info->rx_count = 0;
404 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
405 BT_ERR("Can't allocate mem for new packet");
406 return;
410 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
412 info->rx_skb->dev = (void *) info->hdev;
413 bt_cb(info->rx_skb)->pkt_type = buf[i];
415 switch (bt_cb(info->rx_skb)->pkt_type) {
417 case 0x00:
418 /* init packet */
419 if (offset != 0x00) {
420 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
421 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
422 set_bit(XMIT_SENDING_READY, &(info->tx_state));
423 bluecard_write_wakeup(info);
426 kfree_skb(info->rx_skb);
427 info->rx_skb = NULL;
428 break;
430 case HCI_EVENT_PKT:
431 info->rx_state = RECV_WAIT_EVENT_HEADER;
432 info->rx_count = HCI_EVENT_HDR_SIZE;
433 break;
435 case HCI_ACLDATA_PKT:
436 info->rx_state = RECV_WAIT_ACL_HEADER;
437 info->rx_count = HCI_ACL_HDR_SIZE;
438 break;
440 case HCI_SCODATA_PKT:
441 info->rx_state = RECV_WAIT_SCO_HEADER;
442 info->rx_count = HCI_SCO_HDR_SIZE;
443 break;
445 default:
446 /* unknown packet */
447 BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
448 info->hdev->stat.err_rx++;
450 kfree_skb(info->rx_skb);
451 info->rx_skb = NULL;
452 break;
456 } else {
458 *skb_put(info->rx_skb, 1) = buf[i];
459 info->rx_count--;
461 if (info->rx_count == 0) {
463 int dlen;
464 struct hci_event_hdr *eh;
465 struct hci_acl_hdr *ah;
466 struct hci_sco_hdr *sh;
468 switch (info->rx_state) {
470 case RECV_WAIT_EVENT_HEADER:
471 eh = (struct hci_event_hdr *)(info->rx_skb->data);
472 info->rx_state = RECV_WAIT_DATA;
473 info->rx_count = eh->plen;
474 break;
476 case RECV_WAIT_ACL_HEADER:
477 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
478 dlen = __le16_to_cpu(ah->dlen);
479 info->rx_state = RECV_WAIT_DATA;
480 info->rx_count = dlen;
481 break;
483 case RECV_WAIT_SCO_HEADER:
484 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
485 info->rx_state = RECV_WAIT_DATA;
486 info->rx_count = sh->dlen;
487 break;
489 case RECV_WAIT_DATA:
490 hci_recv_frame(info->rx_skb);
491 info->rx_skb = NULL;
492 break;
503 info->hdev->stat.byte_rx += len;
507 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
509 bluecard_info_t *info = dev_inst;
510 unsigned int iobase;
511 unsigned char reg;
513 if (!info || !info->hdev) {
514 BT_ERR("Call of irq %d for unknown device", irq);
515 return IRQ_NONE;
518 if (!test_bit(CARD_READY, &(info->hw_state)))
519 return IRQ_HANDLED;
521 iobase = info->link.io.BasePort1;
523 spin_lock(&(info->lock));
525 /* Disable interrupt */
526 info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
527 outb(info->ctrl_reg, iobase + REG_CONTROL);
529 reg = inb(iobase + REG_INTERRUPT);
531 if ((reg != 0x00) && (reg != 0xff)) {
533 if (reg & 0x04) {
534 bluecard_receive(info, 0x00);
535 outb(0x04, iobase + REG_INTERRUPT);
536 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
539 if (reg & 0x08) {
540 bluecard_receive(info, 0x10);
541 outb(0x08, iobase + REG_INTERRUPT);
542 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
545 if (reg & 0x01) {
546 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
547 outb(0x01, iobase + REG_INTERRUPT);
548 bluecard_write_wakeup(info);
551 if (reg & 0x02) {
552 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
553 outb(0x02, iobase + REG_INTERRUPT);
554 bluecard_write_wakeup(info);
559 /* Enable interrupt */
560 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
561 outb(info->ctrl_reg, iobase + REG_CONTROL);
563 spin_unlock(&(info->lock));
565 return IRQ_HANDLED;
570 /* ======================== Device specific HCI commands ======================== */
573 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
575 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
576 struct sk_buff *skb;
578 /* Ericsson baud rate command */
579 unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
581 if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
582 BT_ERR("Can't allocate mem for new packet");
583 return -1;
586 switch (baud) {
587 case 460800:
588 cmd[4] = 0x00;
589 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_460800;
590 break;
591 case 230400:
592 cmd[4] = 0x01;
593 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_230400;
594 break;
595 case 115200:
596 cmd[4] = 0x02;
597 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_115200;
598 break;
599 case 57600:
600 /* Fall through... */
601 default:
602 cmd[4] = 0x03;
603 bt_cb(skb)->pkt_type = PKT_BAUD_RATE_57600;
604 break;
607 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
609 skb_queue_tail(&(info->txq), skb);
611 bluecard_write_wakeup(info);
613 return 0;
618 /* ======================== HCI interface ======================== */
621 static int bluecard_hci_flush(struct hci_dev *hdev)
623 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
625 /* Drop TX queue */
626 skb_queue_purge(&(info->txq));
628 return 0;
632 static int bluecard_hci_open(struct hci_dev *hdev)
634 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
635 unsigned int iobase = info->link.io.BasePort1;
637 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
638 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
640 if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
641 return 0;
643 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
644 /* Enable LED */
645 outb(0x08 | 0x20, iobase + 0x30);
648 return 0;
652 static int bluecard_hci_close(struct hci_dev *hdev)
654 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
655 unsigned int iobase = info->link.io.BasePort1;
657 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
658 return 0;
660 bluecard_hci_flush(hdev);
662 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
663 /* Disable LED */
664 outb(0x00, iobase + 0x30);
667 return 0;
671 static int bluecard_hci_send_frame(struct sk_buff *skb)
673 bluecard_info_t *info;
674 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
676 if (!hdev) {
677 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
678 return -ENODEV;
681 info = (bluecard_info_t *)(hdev->driver_data);
683 switch (bt_cb(skb)->pkt_type) {
684 case HCI_COMMAND_PKT:
685 hdev->stat.cmd_tx++;
686 break;
687 case HCI_ACLDATA_PKT:
688 hdev->stat.acl_tx++;
689 break;
690 case HCI_SCODATA_PKT:
691 hdev->stat.sco_tx++;
692 break;
695 /* Prepend skb with frame type */
696 memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
697 skb_queue_tail(&(info->txq), skb);
699 bluecard_write_wakeup(info);
701 return 0;
705 static void bluecard_hci_destruct(struct hci_dev *hdev)
710 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
712 return -ENOIOCTLCMD;
717 /* ======================== Card services HCI interaction ======================== */
720 static int bluecard_open(bluecard_info_t *info)
722 unsigned int iobase = info->link.io.BasePort1;
723 struct hci_dev *hdev;
724 unsigned char id;
726 spin_lock_init(&(info->lock));
728 init_timer(&(info->timer));
729 info->timer.function = &bluecard_activity_led_timeout;
730 info->timer.data = (u_long)info;
732 skb_queue_head_init(&(info->txq));
734 info->rx_state = RECV_WAIT_PACKET_TYPE;
735 info->rx_count = 0;
736 info->rx_skb = NULL;
738 /* Initialize HCI device */
739 hdev = hci_alloc_dev();
740 if (!hdev) {
741 BT_ERR("Can't allocate HCI device");
742 return -ENOMEM;
745 info->hdev = hdev;
747 hdev->type = HCI_PCCARD;
748 hdev->driver_data = info;
750 hdev->open = bluecard_hci_open;
751 hdev->close = bluecard_hci_close;
752 hdev->flush = bluecard_hci_flush;
753 hdev->send = bluecard_hci_send_frame;
754 hdev->destruct = bluecard_hci_destruct;
755 hdev->ioctl = bluecard_hci_ioctl;
757 hdev->owner = THIS_MODULE;
759 id = inb(iobase + 0x30);
761 if ((id & 0x0f) == 0x02)
762 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
764 if (id & 0x10)
765 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
767 if (id & 0x20)
768 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
770 /* Reset card */
771 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
772 outb(info->ctrl_reg, iobase + REG_CONTROL);
774 /* Turn FPGA off */
775 outb(0x80, iobase + 0x30);
777 /* Wait some time */
778 msleep(10);
780 /* Turn FPGA on */
781 outb(0x00, iobase + 0x30);
783 /* Activate card */
784 info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
785 outb(info->ctrl_reg, iobase + REG_CONTROL);
787 /* Enable interrupt */
788 outb(0xff, iobase + REG_INTERRUPT);
789 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
790 outb(info->ctrl_reg, iobase + REG_CONTROL);
792 if ((id & 0x0f) == 0x03) {
793 /* Disable RTS */
794 info->ctrl_reg |= REG_CONTROL_RTS;
795 outb(info->ctrl_reg, iobase + REG_CONTROL);
797 /* Set baud rate */
798 info->ctrl_reg |= 0x03;
799 outb(info->ctrl_reg, iobase + REG_CONTROL);
801 /* Enable RTS */
802 info->ctrl_reg &= ~REG_CONTROL_RTS;
803 outb(info->ctrl_reg, iobase + REG_CONTROL);
805 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
806 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
807 set_bit(XMIT_SENDING_READY, &(info->tx_state));
810 /* Start the RX buffers */
811 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
812 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
814 /* Signal that the hardware is ready */
815 set_bit(CARD_READY, &(info->hw_state));
817 /* Drop TX queue */
818 skb_queue_purge(&(info->txq));
820 /* Control the point at which RTS is enabled */
821 outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
823 /* Timeout before it is safe to send the first HCI packet */
824 msleep(1250);
826 /* Register HCI device */
827 if (hci_register_dev(hdev) < 0) {
828 BT_ERR("Can't register HCI device");
829 info->hdev = NULL;
830 hci_free_dev(hdev);
831 return -ENODEV;
834 return 0;
838 static int bluecard_close(bluecard_info_t *info)
840 unsigned int iobase = info->link.io.BasePort1;
841 struct hci_dev *hdev = info->hdev;
843 if (!hdev)
844 return -ENODEV;
846 bluecard_hci_close(hdev);
848 clear_bit(CARD_READY, &(info->hw_state));
850 /* Reset card */
851 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
852 outb(info->ctrl_reg, iobase + REG_CONTROL);
854 /* Turn FPGA off */
855 outb(0x80, iobase + 0x30);
857 if (hci_unregister_dev(hdev) < 0)
858 BT_ERR("Can't unregister HCI device %s", hdev->name);
860 hci_free_dev(hdev);
862 return 0;
865 static dev_link_t *bluecard_attach(void)
867 bluecard_info_t *info;
868 client_reg_t client_reg;
869 dev_link_t *link;
870 int ret;
872 /* Create new info device */
873 info = kzalloc(sizeof(*info), GFP_KERNEL);
874 if (!info)
875 return NULL;
877 link = &info->link;
878 link->priv = info;
880 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
881 link->io.NumPorts1 = 8;
882 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
883 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
885 link->irq.Handler = bluecard_interrupt;
886 link->irq.Instance = info;
888 link->conf.Attributes = CONF_ENABLE_IRQ;
889 link->conf.Vcc = 50;
890 link->conf.IntType = INT_MEMORY_AND_IO;
892 /* Register with Card Services */
893 link->next = dev_list;
894 dev_list = link;
895 client_reg.dev_info = &dev_info;
896 client_reg.Version = 0x0210;
897 client_reg.event_callback_args.client_data = link;
899 ret = pcmcia_register_client(&link->handle, &client_reg);
900 if (ret != CS_SUCCESS) {
901 cs_error(link->handle, RegisterClient, ret);
902 bluecard_detach(link);
903 return NULL;
906 return link;
910 static void bluecard_detach(dev_link_t *link)
912 bluecard_info_t *info = link->priv;
913 dev_link_t **linkp;
914 int ret;
916 /* Locate device structure */
917 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
918 if (*linkp == link)
919 break;
921 if (*linkp == NULL)
922 return;
924 if (link->state & DEV_CONFIG)
925 bluecard_release(link);
927 if (link->handle) {
928 ret = pcmcia_deregister_client(link->handle);
929 if (ret != CS_SUCCESS)
930 cs_error(link->handle, DeregisterClient, ret);
933 /* Unlink device structure, free bits */
934 *linkp = link->next;
936 kfree(info);
940 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
942 int i;
944 i = pcmcia_get_first_tuple(handle, tuple);
945 if (i != CS_SUCCESS)
946 return CS_NO_MORE_ITEMS;
948 i = pcmcia_get_tuple_data(handle, tuple);
949 if (i != CS_SUCCESS)
950 return i;
952 return pcmcia_parse_tuple(handle, tuple, parse);
955 static void bluecard_config(dev_link_t *link)
957 client_handle_t handle = link->handle;
958 bluecard_info_t *info = link->priv;
959 tuple_t tuple;
960 u_short buf[256];
961 cisparse_t parse;
962 config_info_t config;
963 int i, n, last_ret, last_fn;
965 tuple.TupleData = (cisdata_t *)buf;
966 tuple.TupleOffset = 0;
967 tuple.TupleDataMax = 255;
968 tuple.Attributes = 0;
970 /* Get configuration register information */
971 tuple.DesiredTuple = CISTPL_CONFIG;
972 last_ret = first_tuple(handle, &tuple, &parse);
973 if (last_ret != CS_SUCCESS) {
974 last_fn = ParseTuple;
975 goto cs_failed;
977 link->conf.ConfigBase = parse.config.base;
978 link->conf.Present = parse.config.rmask[0];
980 /* Configure card */
981 link->state |= DEV_CONFIG;
982 i = pcmcia_get_configuration_info(handle, &config);
983 link->conf.Vcc = config.Vcc;
985 link->conf.ConfigIndex = 0x20;
986 link->io.NumPorts1 = 64;
987 link->io.IOAddrLines = 6;
989 for (n = 0; n < 0x400; n += 0x40) {
990 link->io.BasePort1 = n ^ 0x300;
991 i = pcmcia_request_io(link->handle, &link->io);
992 if (i == CS_SUCCESS)
993 break;
996 if (i != CS_SUCCESS) {
997 cs_error(link->handle, RequestIO, i);
998 goto failed;
1001 i = pcmcia_request_irq(link->handle, &link->irq);
1002 if (i != CS_SUCCESS) {
1003 cs_error(link->handle, RequestIRQ, i);
1004 link->irq.AssignedIRQ = 0;
1007 i = pcmcia_request_configuration(link->handle, &link->conf);
1008 if (i != CS_SUCCESS) {
1009 cs_error(link->handle, RequestConfiguration, i);
1010 goto failed;
1013 if (bluecard_open(info) != 0)
1014 goto failed;
1016 strcpy(info->node.dev_name, info->hdev->name);
1017 link->dev = &info->node;
1018 link->state &= ~DEV_CONFIG_PENDING;
1020 return;
1022 cs_failed:
1023 cs_error(link->handle, last_fn, last_ret);
1025 failed:
1026 bluecard_release(link);
1030 static void bluecard_release(dev_link_t *link)
1032 bluecard_info_t *info = link->priv;
1034 if (link->state & DEV_PRESENT)
1035 bluecard_close(info);
1037 del_timer(&(info->timer));
1039 link->dev = NULL;
1041 pcmcia_release_configuration(link->handle);
1042 pcmcia_release_io(link->handle, &link->io);
1043 pcmcia_release_irq(link->handle, &link->irq);
1045 link->state &= ~DEV_CONFIG;
1049 static int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1051 dev_link_t *link = args->client_data;
1052 bluecard_info_t *info = link->priv;
1054 switch (event) {
1055 case CS_EVENT_CARD_REMOVAL:
1056 link->state &= ~DEV_PRESENT;
1057 if (link->state & DEV_CONFIG) {
1058 bluecard_close(info);
1059 bluecard_release(link);
1061 break;
1062 case CS_EVENT_CARD_INSERTION:
1063 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1064 bluecard_config(link);
1065 break;
1066 case CS_EVENT_PM_SUSPEND:
1067 link->state |= DEV_SUSPEND;
1068 /* Fall through... */
1069 case CS_EVENT_RESET_PHYSICAL:
1070 if (link->state & DEV_CONFIG)
1071 pcmcia_release_configuration(link->handle);
1072 break;
1073 case CS_EVENT_PM_RESUME:
1074 link->state &= ~DEV_SUSPEND;
1075 /* Fall through... */
1076 case CS_EVENT_CARD_RESET:
1077 if (DEV_OK(link))
1078 pcmcia_request_configuration(link->handle, &link->conf);
1079 break;
1082 return 0;
1085 static struct pcmcia_device_id bluecard_ids[] = {
1086 PCMCIA_DEVICE_PROD_ID12("BlueCard", "LSE041", 0xbaf16fbf, 0x657cc15e),
1087 PCMCIA_DEVICE_PROD_ID12("BTCFCARD", "LSE139", 0xe3987764, 0x2524b59c),
1088 PCMCIA_DEVICE_PROD_ID12("WSS", "LSE039", 0x0a0736ec, 0x24e6dfab),
1089 PCMCIA_DEVICE_NULL
1091 MODULE_DEVICE_TABLE(pcmcia, bluecard_ids);
1093 static struct pcmcia_driver bluecard_driver = {
1094 .owner = THIS_MODULE,
1095 .drv = {
1096 .name = "bluecard_cs",
1098 .attach = bluecard_attach,
1099 .event = bluecard_event,
1100 .detach = bluecard_detach,
1101 .id_table = bluecard_ids,
1104 static int __init init_bluecard_cs(void)
1106 return pcmcia_register_driver(&bluecard_driver);
1110 static void __exit exit_bluecard_cs(void)
1112 pcmcia_unregister_driver(&bluecard_driver);
1113 BUG_ON(dev_list != NULL);
1116 module_init(init_bluecard_cs);
1117 module_exit(exit_bluecard_cs);