[CPUFREQ] ondemand,conservative governor store the idle ticks for all cpus
[linux-2.6/mini2440.git] / drivers / bluetooth / bluecard_cs.c
blobe481cc411b5dba5d3bd957b43a86f0762655f7d5
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/version.h>
44 #include <pcmcia/cs_types.h>
45 #include <pcmcia/cs.h>
46 #include <pcmcia/cistpl.h>
47 #include <pcmcia/ciscode.h>
48 #include <pcmcia/ds.h>
49 #include <pcmcia/cisreg.h>
51 #include <net/bluetooth/bluetooth.h>
52 #include <net/bluetooth/hci_core.h>
56 /* ======================== Module parameters ======================== */
59 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
60 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
61 MODULE_LICENSE("GPL");
65 /* ======================== Local structures ======================== */
68 typedef struct bluecard_info_t {
69 dev_link_t link;
70 dev_node_t node;
72 struct hci_dev *hdev;
74 spinlock_t lock; /* For serializing operations */
75 struct timer_list timer; /* For LED control */
77 struct sk_buff_head txq;
78 unsigned long tx_state;
80 unsigned long rx_state;
81 unsigned long rx_count;
82 struct sk_buff *rx_skb;
84 unsigned char ctrl_reg;
85 unsigned long hw_state; /* Status of the hardware and LED control */
86 } bluecard_info_t;
89 static void bluecard_config(dev_link_t *link);
90 static void bluecard_release(dev_link_t *link);
91 static int bluecard_event(event_t event, int priority, event_callback_args_t *args);
93 static dev_info_t dev_info = "bluecard_cs";
95 static dev_link_t *bluecard_attach(void);
96 static void bluecard_detach(dev_link_t *);
98 static dev_link_t *dev_list = NULL;
101 /* Default baud rate: 57600, 115200, 230400 or 460800 */
102 #define DEFAULT_BAUD_RATE 230400
105 /* Hardware states */
106 #define CARD_READY 1
107 #define CARD_HAS_PCCARD_ID 4
108 #define CARD_HAS_POWER_LED 5
109 #define CARD_HAS_ACTIVITY_LED 6
111 /* Transmit states */
112 #define XMIT_SENDING 1
113 #define XMIT_WAKEUP 2
114 #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
115 #define XMIT_BUF_ONE_READY 6
116 #define XMIT_BUF_TWO_READY 7
117 #define XMIT_SENDING_READY 8
119 /* Receiver states */
120 #define RECV_WAIT_PACKET_TYPE 0
121 #define RECV_WAIT_EVENT_HEADER 1
122 #define RECV_WAIT_ACL_HEADER 2
123 #define RECV_WAIT_SCO_HEADER 3
124 #define RECV_WAIT_DATA 4
126 /* Special packet types */
127 #define PKT_BAUD_RATE_57600 0x80
128 #define PKT_BAUD_RATE_115200 0x81
129 #define PKT_BAUD_RATE_230400 0x82
130 #define PKT_BAUD_RATE_460800 0x83
133 /* These are the register offsets */
134 #define REG_COMMAND 0x20
135 #define REG_INTERRUPT 0x21
136 #define REG_CONTROL 0x22
137 #define REG_RX_CONTROL 0x24
138 #define REG_CARD_RESET 0x30
139 #define REG_LED_CTRL 0x30
141 /* REG_COMMAND */
142 #define REG_COMMAND_TX_BUF_ONE 0x01
143 #define REG_COMMAND_TX_BUF_TWO 0x02
144 #define REG_COMMAND_RX_BUF_ONE 0x04
145 #define REG_COMMAND_RX_BUF_TWO 0x08
146 #define REG_COMMAND_RX_WIN_ONE 0x00
147 #define REG_COMMAND_RX_WIN_TWO 0x10
149 /* REG_CONTROL */
150 #define REG_CONTROL_BAUD_RATE_57600 0x00
151 #define REG_CONTROL_BAUD_RATE_115200 0x01
152 #define REG_CONTROL_BAUD_RATE_230400 0x02
153 #define REG_CONTROL_BAUD_RATE_460800 0x03
154 #define REG_CONTROL_RTS 0x04
155 #define REG_CONTROL_BT_ON 0x08
156 #define REG_CONTROL_BT_RESET 0x10
157 #define REG_CONTROL_BT_RES_PU 0x20
158 #define REG_CONTROL_INTERRUPT 0x40
159 #define REG_CONTROL_CARD_RESET 0x80
161 /* REG_RX_CONTROL */
162 #define RTS_LEVEL_SHIFT_BITS 0x02
166 /* ======================== LED handling routines ======================== */
169 static void bluecard_activity_led_timeout(u_long arg)
171 bluecard_info_t *info = (bluecard_info_t *)arg;
172 unsigned int iobase = info->link.io.BasePort1;
174 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
175 return;
177 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
178 /* Disable activity LED */
179 outb(0x08 | 0x20, iobase + 0x30);
180 } else {
181 /* Disable power LED */
182 outb(0x00, iobase + 0x30);
187 static void bluecard_enable_activity_led(bluecard_info_t *info)
189 unsigned int iobase = info->link.io.BasePort1;
191 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
192 return;
194 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
195 /* Enable activity LED */
196 outb(0x10 | 0x40, iobase + 0x30);
198 /* Stop the LED after HZ/4 */
199 mod_timer(&(info->timer), jiffies + HZ / 4);
200 } else {
201 /* Enable power LED */
202 outb(0x08 | 0x20, iobase + 0x30);
204 /* Stop the LED after HZ/2 */
205 mod_timer(&(info->timer), jiffies + HZ / 2);
211 /* ======================== Interrupt handling ======================== */
214 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
216 int i, actual;
218 actual = (len > 15) ? 15 : len;
220 outb_p(actual, iobase + offset);
222 for (i = 0; i < actual; i++)
223 outb_p(buf[i], iobase + offset + i + 1);
225 return actual;
229 static void bluecard_write_wakeup(bluecard_info_t *info)
231 if (!info) {
232 BT_ERR("Unknown device");
233 return;
236 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
237 return;
239 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
240 set_bit(XMIT_WAKEUP, &(info->tx_state));
241 return;
244 do {
245 register unsigned int iobase = info->link.io.BasePort1;
246 register unsigned int offset;
247 register unsigned char command;
248 register unsigned long ready_bit;
249 register struct sk_buff *skb;
250 register int len;
252 clear_bit(XMIT_WAKEUP, &(info->tx_state));
254 if (!(info->link.state & DEV_PRESENT))
255 return;
257 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
258 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
259 break;
260 offset = 0x10;
261 command = REG_COMMAND_TX_BUF_TWO;
262 ready_bit = XMIT_BUF_TWO_READY;
263 } else {
264 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
265 break;
266 offset = 0x00;
267 command = REG_COMMAND_TX_BUF_ONE;
268 ready_bit = XMIT_BUF_ONE_READY;
271 if (!(skb = skb_dequeue(&(info->txq))))
272 break;
274 if (skb->pkt_type & 0x80) {
275 /* Disable RTS */
276 info->ctrl_reg |= REG_CONTROL_RTS;
277 outb(info->ctrl_reg, iobase + REG_CONTROL);
280 /* Activate LED */
281 bluecard_enable_activity_led(info);
283 /* Send frame */
284 len = bluecard_write(iobase, offset, skb->data, skb->len);
286 /* Tell the FPGA to send the data */
287 outb_p(command, iobase + REG_COMMAND);
289 /* Mark the buffer as dirty */
290 clear_bit(ready_bit, &(info->tx_state));
292 if (skb->pkt_type & 0x80) {
293 DECLARE_WAIT_QUEUE_HEAD(wq);
294 DEFINE_WAIT(wait);
296 unsigned char baud_reg;
298 switch (skb->pkt_type) {
299 case PKT_BAUD_RATE_460800:
300 baud_reg = REG_CONTROL_BAUD_RATE_460800;
301 break;
302 case PKT_BAUD_RATE_230400:
303 baud_reg = REG_CONTROL_BAUD_RATE_230400;
304 break;
305 case PKT_BAUD_RATE_115200:
306 baud_reg = REG_CONTROL_BAUD_RATE_115200;
307 break;
308 case PKT_BAUD_RATE_57600:
309 /* Fall through... */
310 default:
311 baud_reg = REG_CONTROL_BAUD_RATE_57600;
312 break;
315 /* Wait until the command reaches the baseband */
316 prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
317 schedule_timeout(HZ/10);
318 finish_wait(&wq, &wait);
320 /* Set baud on baseband */
321 info->ctrl_reg &= ~0x03;
322 info->ctrl_reg |= baud_reg;
323 outb(info->ctrl_reg, iobase + REG_CONTROL);
325 /* Enable RTS */
326 info->ctrl_reg &= ~REG_CONTROL_RTS;
327 outb(info->ctrl_reg, iobase + REG_CONTROL);
329 /* Wait before the next HCI packet can be send */
330 prepare_to_wait(&wq, &wait, TASK_INTERRUPTIBLE);
331 schedule_timeout(HZ);
332 finish_wait(&wq, &wait);
335 if (len == skb->len) {
336 kfree_skb(skb);
337 } else {
338 skb_pull(skb, len);
339 skb_queue_head(&(info->txq), skb);
342 info->hdev->stat.byte_tx += len;
344 /* Change buffer */
345 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
347 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
349 clear_bit(XMIT_SENDING, &(info->tx_state));
353 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
355 int i, n, len;
357 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
359 len = inb(iobase + offset);
360 n = 0;
361 i = 1;
363 while (n < len) {
365 if (i == 16) {
366 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
367 i = 0;
370 buf[n] = inb(iobase + offset + i);
372 n++;
373 i++;
377 return len;
381 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
383 unsigned int iobase;
384 unsigned char buf[31];
385 int i, len;
387 if (!info) {
388 BT_ERR("Unknown device");
389 return;
392 iobase = info->link.io.BasePort1;
394 if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
395 bluecard_enable_activity_led(info);
397 len = bluecard_read(iobase, offset, buf, sizeof(buf));
399 for (i = 0; i < len; i++) {
401 /* Allocate packet */
402 if (info->rx_skb == NULL) {
403 info->rx_state = RECV_WAIT_PACKET_TYPE;
404 info->rx_count = 0;
405 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
406 BT_ERR("Can't allocate mem for new packet");
407 return;
411 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
413 info->rx_skb->dev = (void *) info->hdev;
414 info->rx_skb->pkt_type = buf[i];
416 switch (info->rx_skb->pkt_type) {
418 case 0x00:
419 /* init packet */
420 if (offset != 0x00) {
421 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
422 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
423 set_bit(XMIT_SENDING_READY, &(info->tx_state));
424 bluecard_write_wakeup(info);
427 kfree_skb(info->rx_skb);
428 info->rx_skb = NULL;
429 break;
431 case HCI_EVENT_PKT:
432 info->rx_state = RECV_WAIT_EVENT_HEADER;
433 info->rx_count = HCI_EVENT_HDR_SIZE;
434 break;
436 case HCI_ACLDATA_PKT:
437 info->rx_state = RECV_WAIT_ACL_HEADER;
438 info->rx_count = HCI_ACL_HDR_SIZE;
439 break;
441 case HCI_SCODATA_PKT:
442 info->rx_state = RECV_WAIT_SCO_HEADER;
443 info->rx_count = HCI_SCO_HDR_SIZE;
444 break;
446 default:
447 /* unknown packet */
448 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
449 info->hdev->stat.err_rx++;
451 kfree_skb(info->rx_skb);
452 info->rx_skb = NULL;
453 break;
457 } else {
459 *skb_put(info->rx_skb, 1) = buf[i];
460 info->rx_count--;
462 if (info->rx_count == 0) {
464 int dlen;
465 struct hci_event_hdr *eh;
466 struct hci_acl_hdr *ah;
467 struct hci_sco_hdr *sh;
469 switch (info->rx_state) {
471 case RECV_WAIT_EVENT_HEADER:
472 eh = (struct hci_event_hdr *)(info->rx_skb->data);
473 info->rx_state = RECV_WAIT_DATA;
474 info->rx_count = eh->plen;
475 break;
477 case RECV_WAIT_ACL_HEADER:
478 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
479 dlen = __le16_to_cpu(ah->dlen);
480 info->rx_state = RECV_WAIT_DATA;
481 info->rx_count = dlen;
482 break;
484 case RECV_WAIT_SCO_HEADER:
485 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
486 info->rx_state = RECV_WAIT_DATA;
487 info->rx_count = sh->dlen;
488 break;
490 case RECV_WAIT_DATA:
491 hci_recv_frame(info->rx_skb);
492 info->rx_skb = NULL;
493 break;
504 info->hdev->stat.byte_rx += len;
508 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
510 bluecard_info_t *info = dev_inst;
511 unsigned int iobase;
512 unsigned char reg;
514 if (!info || !info->hdev) {
515 BT_ERR("Call of irq %d for unknown device", irq);
516 return IRQ_NONE;
519 if (!test_bit(CARD_READY, &(info->hw_state)))
520 return IRQ_HANDLED;
522 iobase = info->link.io.BasePort1;
524 spin_lock(&(info->lock));
526 /* Disable interrupt */
527 info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
528 outb(info->ctrl_reg, iobase + REG_CONTROL);
530 reg = inb(iobase + REG_INTERRUPT);
532 if ((reg != 0x00) && (reg != 0xff)) {
534 if (reg & 0x04) {
535 bluecard_receive(info, 0x00);
536 outb(0x04, iobase + REG_INTERRUPT);
537 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
540 if (reg & 0x08) {
541 bluecard_receive(info, 0x10);
542 outb(0x08, iobase + REG_INTERRUPT);
543 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
546 if (reg & 0x01) {
547 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
548 outb(0x01, iobase + REG_INTERRUPT);
549 bluecard_write_wakeup(info);
552 if (reg & 0x02) {
553 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
554 outb(0x02, iobase + REG_INTERRUPT);
555 bluecard_write_wakeup(info);
560 /* Enable interrupt */
561 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
562 outb(info->ctrl_reg, iobase + REG_CONTROL);
564 spin_unlock(&(info->lock));
566 return IRQ_HANDLED;
571 /* ======================== Device specific HCI commands ======================== */
574 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
576 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
577 struct sk_buff *skb;
579 /* Ericsson baud rate command */
580 unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
582 if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
583 BT_ERR("Can't allocate mem for new packet");
584 return -1;
587 switch (baud) {
588 case 460800:
589 cmd[4] = 0x00;
590 skb->pkt_type = PKT_BAUD_RATE_460800;
591 break;
592 case 230400:
593 cmd[4] = 0x01;
594 skb->pkt_type = PKT_BAUD_RATE_230400;
595 break;
596 case 115200:
597 cmd[4] = 0x02;
598 skb->pkt_type = PKT_BAUD_RATE_115200;
599 break;
600 case 57600:
601 /* Fall through... */
602 default:
603 cmd[4] = 0x03;
604 skb->pkt_type = PKT_BAUD_RATE_57600;
605 break;
608 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
610 skb_queue_tail(&(info->txq), skb);
612 bluecard_write_wakeup(info);
614 return 0;
619 /* ======================== HCI interface ======================== */
622 static int bluecard_hci_flush(struct hci_dev *hdev)
624 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
626 /* Drop TX queue */
627 skb_queue_purge(&(info->txq));
629 return 0;
633 static int bluecard_hci_open(struct hci_dev *hdev)
635 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
636 unsigned int iobase = info->link.io.BasePort1;
638 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
639 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
641 if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
642 return 0;
644 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
645 /* Enable LED */
646 outb(0x08 | 0x20, iobase + 0x30);
649 return 0;
653 static int bluecard_hci_close(struct hci_dev *hdev)
655 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
656 unsigned int iobase = info->link.io.BasePort1;
658 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
659 return 0;
661 bluecard_hci_flush(hdev);
663 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
664 /* Disable LED */
665 outb(0x00, iobase + 0x30);
668 return 0;
672 static int bluecard_hci_send_frame(struct sk_buff *skb)
674 bluecard_info_t *info;
675 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
677 if (!hdev) {
678 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
679 return -ENODEV;
682 info = (bluecard_info_t *)(hdev->driver_data);
684 switch (skb->pkt_type) {
685 case HCI_COMMAND_PKT:
686 hdev->stat.cmd_tx++;
687 break;
688 case HCI_ACLDATA_PKT:
689 hdev->stat.acl_tx++;
690 break;
691 case HCI_SCODATA_PKT:
692 hdev->stat.sco_tx++;
693 break;
696 /* Prepend skb with frame type */
697 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
698 skb_queue_tail(&(info->txq), skb);
700 bluecard_write_wakeup(info);
702 return 0;
706 static void bluecard_hci_destruct(struct hci_dev *hdev)
711 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
713 return -ENOIOCTLCMD;
718 /* ======================== Card services HCI interaction ======================== */
721 static int bluecard_open(bluecard_info_t *info)
723 unsigned int iobase = info->link.io.BasePort1;
724 struct hci_dev *hdev;
725 unsigned char id;
727 spin_lock_init(&(info->lock));
729 init_timer(&(info->timer));
730 info->timer.function = &bluecard_activity_led_timeout;
731 info->timer.data = (u_long)info;
733 skb_queue_head_init(&(info->txq));
735 info->rx_state = RECV_WAIT_PACKET_TYPE;
736 info->rx_count = 0;
737 info->rx_skb = NULL;
739 /* Initialize HCI device */
740 hdev = hci_alloc_dev();
741 if (!hdev) {
742 BT_ERR("Can't allocate HCI device");
743 return -ENOMEM;
746 info->hdev = hdev;
748 hdev->type = HCI_PCCARD;
749 hdev->driver_data = info;
751 hdev->open = bluecard_hci_open;
752 hdev->close = bluecard_hci_close;
753 hdev->flush = bluecard_hci_flush;
754 hdev->send = bluecard_hci_send_frame;
755 hdev->destruct = bluecard_hci_destruct;
756 hdev->ioctl = bluecard_hci_ioctl;
758 hdev->owner = THIS_MODULE;
760 id = inb(iobase + 0x30);
762 if ((id & 0x0f) == 0x02)
763 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
765 if (id & 0x10)
766 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
768 if (id & 0x20)
769 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
771 /* Reset card */
772 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
773 outb(info->ctrl_reg, iobase + REG_CONTROL);
775 /* Turn FPGA off */
776 outb(0x80, iobase + 0x30);
778 /* Wait some time */
779 msleep(10);
781 /* Turn FPGA on */
782 outb(0x00, iobase + 0x30);
784 /* Activate card */
785 info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
786 outb(info->ctrl_reg, iobase + REG_CONTROL);
788 /* Enable interrupt */
789 outb(0xff, iobase + REG_INTERRUPT);
790 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
791 outb(info->ctrl_reg, iobase + REG_CONTROL);
793 if ((id & 0x0f) == 0x03) {
794 /* Disable RTS */
795 info->ctrl_reg |= REG_CONTROL_RTS;
796 outb(info->ctrl_reg, iobase + REG_CONTROL);
798 /* Set baud rate */
799 info->ctrl_reg |= 0x03;
800 outb(info->ctrl_reg, iobase + REG_CONTROL);
802 /* Enable RTS */
803 info->ctrl_reg &= ~REG_CONTROL_RTS;
804 outb(info->ctrl_reg, iobase + REG_CONTROL);
806 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
807 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
808 set_bit(XMIT_SENDING_READY, &(info->tx_state));
811 /* Start the RX buffers */
812 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
813 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
815 /* Signal that the hardware is ready */
816 set_bit(CARD_READY, &(info->hw_state));
818 /* Drop TX queue */
819 skb_queue_purge(&(info->txq));
821 /* Control the point at which RTS is enabled */
822 outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
824 /* Timeout before it is safe to send the first HCI packet */
825 msleep(1250);
827 /* Register HCI device */
828 if (hci_register_dev(hdev) < 0) {
829 BT_ERR("Can't register HCI device");
830 info->hdev = NULL;
831 hci_free_dev(hdev);
832 return -ENODEV;
835 return 0;
839 static int bluecard_close(bluecard_info_t *info)
841 unsigned int iobase = info->link.io.BasePort1;
842 struct hci_dev *hdev = info->hdev;
844 if (!hdev)
845 return -ENODEV;
847 bluecard_hci_close(hdev);
849 clear_bit(CARD_READY, &(info->hw_state));
851 /* Reset card */
852 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
853 outb(info->ctrl_reg, iobase + REG_CONTROL);
855 /* Turn FPGA off */
856 outb(0x80, iobase + 0x30);
858 if (hci_unregister_dev(hdev) < 0)
859 BT_ERR("Can't unregister HCI device %s", hdev->name);
861 hci_free_dev(hdev);
863 return 0;
866 static dev_link_t *bluecard_attach(void)
868 bluecard_info_t *info;
869 client_reg_t client_reg;
870 dev_link_t *link;
871 int ret;
873 /* Create new info device */
874 info = kmalloc(sizeof(*info), GFP_KERNEL);
875 if (!info)
876 return NULL;
877 memset(info, 0, sizeof(*info));
879 link = &info->link;
880 link->priv = info;
882 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
883 link->io.NumPorts1 = 8;
884 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
885 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
887 link->irq.Handler = bluecard_interrupt;
888 link->irq.Instance = info;
890 link->conf.Attributes = CONF_ENABLE_IRQ;
891 link->conf.Vcc = 50;
892 link->conf.IntType = INT_MEMORY_AND_IO;
894 /* Register with Card Services */
895 link->next = dev_list;
896 dev_list = link;
897 client_reg.dev_info = &dev_info;
898 client_reg.EventMask =
899 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
900 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
901 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
902 client_reg.event_handler = &bluecard_event;
903 client_reg.Version = 0x0210;
904 client_reg.event_callback_args.client_data = link;
906 ret = pcmcia_register_client(&link->handle, &client_reg);
907 if (ret != CS_SUCCESS) {
908 cs_error(link->handle, RegisterClient, ret);
909 bluecard_detach(link);
910 return NULL;
913 return link;
917 static void bluecard_detach(dev_link_t *link)
919 bluecard_info_t *info = link->priv;
920 dev_link_t **linkp;
921 int ret;
923 /* Locate device structure */
924 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
925 if (*linkp == link)
926 break;
928 if (*linkp == NULL)
929 return;
931 if (link->state & DEV_CONFIG)
932 bluecard_release(link);
934 if (link->handle) {
935 ret = pcmcia_deregister_client(link->handle);
936 if (ret != CS_SUCCESS)
937 cs_error(link->handle, DeregisterClient, ret);
940 /* Unlink device structure, free bits */
941 *linkp = link->next;
943 kfree(info);
947 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
949 int i;
951 i = pcmcia_get_first_tuple(handle, tuple);
952 if (i != CS_SUCCESS)
953 return CS_NO_MORE_ITEMS;
955 i = pcmcia_get_tuple_data(handle, tuple);
956 if (i != CS_SUCCESS)
957 return i;
959 return pcmcia_parse_tuple(handle, tuple, parse);
962 static void bluecard_config(dev_link_t *link)
964 client_handle_t handle = link->handle;
965 bluecard_info_t *info = link->priv;
966 tuple_t tuple;
967 u_short buf[256];
968 cisparse_t parse;
969 config_info_t config;
970 int i, n, last_ret, last_fn;
972 tuple.TupleData = (cisdata_t *)buf;
973 tuple.TupleOffset = 0;
974 tuple.TupleDataMax = 255;
975 tuple.Attributes = 0;
977 /* Get configuration register information */
978 tuple.DesiredTuple = CISTPL_CONFIG;
979 last_ret = first_tuple(handle, &tuple, &parse);
980 if (last_ret != CS_SUCCESS) {
981 last_fn = ParseTuple;
982 goto cs_failed;
984 link->conf.ConfigBase = parse.config.base;
985 link->conf.Present = parse.config.rmask[0];
987 /* Configure card */
988 link->state |= DEV_CONFIG;
989 i = pcmcia_get_configuration_info(handle, &config);
990 link->conf.Vcc = config.Vcc;
992 link->conf.ConfigIndex = 0x20;
993 link->io.NumPorts1 = 64;
994 link->io.IOAddrLines = 6;
996 for (n = 0; n < 0x400; n += 0x40) {
997 link->io.BasePort1 = n ^ 0x300;
998 i = pcmcia_request_io(link->handle, &link->io);
999 if (i == CS_SUCCESS)
1000 break;
1003 if (i != CS_SUCCESS) {
1004 cs_error(link->handle, RequestIO, i);
1005 goto failed;
1008 i = pcmcia_request_irq(link->handle, &link->irq);
1009 if (i != CS_SUCCESS) {
1010 cs_error(link->handle, RequestIRQ, i);
1011 link->irq.AssignedIRQ = 0;
1014 i = pcmcia_request_configuration(link->handle, &link->conf);
1015 if (i != CS_SUCCESS) {
1016 cs_error(link->handle, RequestConfiguration, i);
1017 goto failed;
1020 if (bluecard_open(info) != 0)
1021 goto failed;
1023 strcpy(info->node.dev_name, info->hdev->name);
1024 link->dev = &info->node;
1025 link->state &= ~DEV_CONFIG_PENDING;
1027 return;
1029 cs_failed:
1030 cs_error(link->handle, last_fn, last_ret);
1032 failed:
1033 bluecard_release(link);
1037 static void bluecard_release(dev_link_t *link)
1039 bluecard_info_t *info = link->priv;
1041 if (link->state & DEV_PRESENT)
1042 bluecard_close(info);
1044 del_timer(&(info->timer));
1046 link->dev = NULL;
1048 pcmcia_release_configuration(link->handle);
1049 pcmcia_release_io(link->handle, &link->io);
1050 pcmcia_release_irq(link->handle, &link->irq);
1052 link->state &= ~DEV_CONFIG;
1056 static int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1058 dev_link_t *link = args->client_data;
1059 bluecard_info_t *info = link->priv;
1061 switch (event) {
1062 case CS_EVENT_CARD_REMOVAL:
1063 link->state &= ~DEV_PRESENT;
1064 if (link->state & DEV_CONFIG) {
1065 bluecard_close(info);
1066 bluecard_release(link);
1068 break;
1069 case CS_EVENT_CARD_INSERTION:
1070 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1071 bluecard_config(link);
1072 break;
1073 case CS_EVENT_PM_SUSPEND:
1074 link->state |= DEV_SUSPEND;
1075 /* Fall through... */
1076 case CS_EVENT_RESET_PHYSICAL:
1077 if (link->state & DEV_CONFIG)
1078 pcmcia_release_configuration(link->handle);
1079 break;
1080 case CS_EVENT_PM_RESUME:
1081 link->state &= ~DEV_SUSPEND;
1082 /* Fall through... */
1083 case CS_EVENT_CARD_RESET:
1084 if (DEV_OK(link))
1085 pcmcia_request_configuration(link->handle, &link->conf);
1086 break;
1089 return 0;
1092 static struct pcmcia_driver bluecard_driver = {
1093 .owner = THIS_MODULE,
1094 .drv = {
1095 .name = "bluecard_cs",
1097 .attach = bluecard_attach,
1098 .detach = bluecard_detach,
1101 static int __init init_bluecard_cs(void)
1103 return pcmcia_register_driver(&bluecard_driver);
1107 static void __exit exit_bluecard_cs(void)
1109 pcmcia_unregister_driver(&bluecard_driver);
1110 BUG_ON(dev_list != NULL);
1113 module_init(init_bluecard_cs);
1114 module_exit(exit_bluecard_cs);