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/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/skbuff.h>
39 #include <pcmcia/version.h>
40 #include <pcmcia/cs_types.h>
41 #include <pcmcia/cs.h>
42 #include <pcmcia/cistpl.h>
43 #include <pcmcia/ciscode.h>
44 #include <pcmcia/ds.h>
45 #include <pcmcia/cisreg.h>
47 #include <net/bluetooth/bluetooth.h>
48 #include <net/bluetooth/hci_core.h>
52 /* ======================== Module parameters ======================== */
55 /* Bit map of interrupts to choose from */
56 static u_int irq_mask = 0x86bc;
57 static int irq_list[4] = { -1 };
59 MODULE_PARM(irq_mask, "i");
60 MODULE_PARM(irq_list, "1-4i");
62 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
63 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
64 MODULE_LICENSE("GPL");
68 /* ======================== Local structures ======================== */
71 typedef struct bluecard_info_t {
77 spinlock_t lock; /* For serializing operations */
78 struct timer_list timer; /* For LED control */
80 struct sk_buff_head txq;
81 unsigned long tx_state;
83 unsigned long rx_state;
84 unsigned long rx_count;
85 struct sk_buff *rx_skb;
87 unsigned char ctrl_reg;
88 unsigned long hw_state; /* Status of the hardware and LED control */
92 void bluecard_config(dev_link_t *link);
93 void bluecard_release(dev_link_t *link);
94 int bluecard_event(event_t event, int priority, event_callback_args_t *args);
96 static dev_info_t dev_info = "bluecard_cs";
98 dev_link_t *bluecard_attach(void);
99 void bluecard_detach(dev_link_t *);
101 static dev_link_t *dev_list = NULL;
104 /* Default baud rate: 57600, 115200, 230400 or 460800 */
105 #define DEFAULT_BAUD_RATE 230400
108 /* Hardware states */
110 #define CARD_HAS_PCCARD_ID 4
111 #define CARD_HAS_POWER_LED 5
112 #define CARD_HAS_ACTIVITY_LED 6
114 /* Transmit states */
115 #define XMIT_SENDING 1
116 #define XMIT_WAKEUP 2
117 #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
118 #define XMIT_BUF_ONE_READY 6
119 #define XMIT_BUF_TWO_READY 7
120 #define XMIT_SENDING_READY 8
122 /* Receiver states */
123 #define RECV_WAIT_PACKET_TYPE 0
124 #define RECV_WAIT_EVENT_HEADER 1
125 #define RECV_WAIT_ACL_HEADER 2
126 #define RECV_WAIT_SCO_HEADER 3
127 #define RECV_WAIT_DATA 4
129 /* Special packet types */
130 #define PKT_BAUD_RATE_57600 0x80
131 #define PKT_BAUD_RATE_115200 0x81
132 #define PKT_BAUD_RATE_230400 0x82
133 #define PKT_BAUD_RATE_460800 0x83
136 /* These are the register offsets */
137 #define REG_COMMAND 0x20
138 #define REG_INTERRUPT 0x21
139 #define REG_CONTROL 0x22
140 #define REG_RX_CONTROL 0x24
141 #define REG_CARD_RESET 0x30
142 #define REG_LED_CTRL 0x30
145 #define REG_COMMAND_TX_BUF_ONE 0x01
146 #define REG_COMMAND_TX_BUF_TWO 0x02
147 #define REG_COMMAND_RX_BUF_ONE 0x04
148 #define REG_COMMAND_RX_BUF_TWO 0x08
149 #define REG_COMMAND_RX_WIN_ONE 0x00
150 #define REG_COMMAND_RX_WIN_TWO 0x10
153 #define REG_CONTROL_BAUD_RATE_57600 0x00
154 #define REG_CONTROL_BAUD_RATE_115200 0x01
155 #define REG_CONTROL_BAUD_RATE_230400 0x02
156 #define REG_CONTROL_BAUD_RATE_460800 0x03
157 #define REG_CONTROL_RTS 0x04
158 #define REG_CONTROL_BT_ON 0x08
159 #define REG_CONTROL_BT_RESET 0x10
160 #define REG_CONTROL_BT_RES_PU 0x20
161 #define REG_CONTROL_INTERRUPT 0x40
162 #define REG_CONTROL_CARD_RESET 0x80
165 #define RTS_LEVEL_SHIFT_BITS 0x02
169 /* ======================== LED handling routines ======================== */
172 void bluecard_activity_led_timeout(u_long arg)
174 bluecard_info_t *info = (bluecard_info_t *)arg;
175 unsigned int iobase = info->link.io.BasePort1;
177 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
180 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
181 /* Disable activity LED */
182 outb(0x08 | 0x20, iobase + 0x30);
184 /* Disable power LED */
185 outb(0x00, iobase + 0x30);
190 static void bluecard_enable_activity_led(bluecard_info_t *info)
192 unsigned int iobase = info->link.io.BasePort1;
194 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
197 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
198 /* Enable activity LED */
199 outb(0x10 | 0x40, iobase + 0x30);
201 /* Stop the LED after HZ/4 */
202 mod_timer(&(info->timer), jiffies + HZ / 4);
204 /* Enable power LED */
205 outb(0x08 | 0x20, iobase + 0x30);
207 /* Stop the LED after HZ/2 */
208 mod_timer(&(info->timer), jiffies + HZ / 2);
214 /* ======================== Interrupt handling ======================== */
217 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
221 actual = (len > 15) ? 15 : len;
223 outb_p(actual, iobase + offset);
225 for (i = 0; i < actual; i++)
226 outb_p(buf[i], iobase + offset + i + 1);
232 static void bluecard_write_wakeup(bluecard_info_t *info)
235 BT_ERR("Unknown device");
239 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
242 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
243 set_bit(XMIT_WAKEUP, &(info->tx_state));
248 register unsigned int iobase = info->link.io.BasePort1;
249 register unsigned int offset;
250 register unsigned char command;
251 register unsigned long ready_bit;
252 register struct sk_buff *skb;
255 clear_bit(XMIT_WAKEUP, &(info->tx_state));
257 if (!(info->link.state & DEV_PRESENT))
260 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
261 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
264 command = REG_COMMAND_TX_BUF_TWO;
265 ready_bit = XMIT_BUF_TWO_READY;
267 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
270 command = REG_COMMAND_TX_BUF_ONE;
271 ready_bit = XMIT_BUF_ONE_READY;
274 if (!(skb = skb_dequeue(&(info->txq))))
277 if (skb->pkt_type & 0x80) {
279 info->ctrl_reg |= REG_CONTROL_RTS;
280 outb(info->ctrl_reg, iobase + REG_CONTROL);
284 bluecard_enable_activity_led(info);
287 len = bluecard_write(iobase, offset, skb->data, skb->len);
289 /* Tell the FPGA to send the data */
290 outb_p(command, iobase + REG_COMMAND);
292 /* Mark the buffer as dirty */
293 clear_bit(ready_bit, &(info->tx_state));
295 if (skb->pkt_type & 0x80) {
297 wait_queue_head_t wait;
298 unsigned char baud_reg;
300 switch (skb->pkt_type) {
301 case PKT_BAUD_RATE_460800:
302 baud_reg = REG_CONTROL_BAUD_RATE_460800;
304 case PKT_BAUD_RATE_230400:
305 baud_reg = REG_CONTROL_BAUD_RATE_230400;
307 case PKT_BAUD_RATE_115200:
308 baud_reg = REG_CONTROL_BAUD_RATE_115200;
310 case PKT_BAUD_RATE_57600:
311 /* Fall through... */
313 baud_reg = REG_CONTROL_BAUD_RATE_57600;
317 /* Wait until the command reaches the baseband */
318 init_waitqueue_head(&wait);
319 interruptible_sleep_on_timeout(&wait, HZ / 10);
321 /* Set baud on baseband */
322 info->ctrl_reg &= ~0x03;
323 info->ctrl_reg |= baud_reg;
324 outb(info->ctrl_reg, iobase + REG_CONTROL);
327 info->ctrl_reg &= ~REG_CONTROL_RTS;
328 outb(info->ctrl_reg, iobase + REG_CONTROL);
330 /* Wait before the next HCI packet can be send */
331 interruptible_sleep_on_timeout(&wait, HZ);
335 if (len == skb->len) {
339 skb_queue_head(&(info->txq), skb);
342 info->hdev->stat.byte_tx += len;
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)
357 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
359 len = inb(iobase + offset);
366 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
370 buf[n] = inb(iobase + offset + i);
381 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
384 unsigned char buf[31];
388 BT_ERR("Unknown device");
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;
405 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
406 BT_ERR("Can't allocate mem for new packet");
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) {
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);
432 info->rx_state = RECV_WAIT_EVENT_HEADER;
433 info->rx_count = HCI_EVENT_HDR_SIZE;
436 case HCI_ACLDATA_PKT:
437 info->rx_state = RECV_WAIT_ACL_HEADER;
438 info->rx_count = HCI_ACL_HDR_SIZE;
441 case HCI_SCODATA_PKT:
442 info->rx_state = RECV_WAIT_SCO_HEADER;
443 info->rx_count = HCI_SCO_HDR_SIZE;
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);
459 *skb_put(info->rx_skb, 1) = buf[i];
462 if (info->rx_count == 0) {
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;
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;
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;
491 hci_recv_frame(info->rx_skb);
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;
514 if (!info || !info->hdev) {
515 BT_ERR("Call of irq %d for unknown device", irq);
519 if (!test_bit(CARD_READY, &(info->hw_state)))
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)) {
535 bluecard_receive(info, 0x00);
536 outb(0x04, iobase + REG_INTERRUPT);
537 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
541 bluecard_receive(info, 0x10);
542 outb(0x08, iobase + REG_INTERRUPT);
543 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
547 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
548 outb(0x01, iobase + REG_INTERRUPT);
549 bluecard_write_wakeup(info);
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));
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);
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");
590 skb->pkt_type = PKT_BAUD_RATE_460800;
594 skb->pkt_type = PKT_BAUD_RATE_230400;
598 skb->pkt_type = PKT_BAUD_RATE_115200;
601 /* Fall through... */
604 skb->pkt_type = PKT_BAUD_RATE_57600;
608 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
610 skb_queue_tail(&(info->txq), skb);
612 bluecard_write_wakeup(info);
619 /* ======================== HCI interface ======================== */
622 static int bluecard_hci_flush(struct hci_dev *hdev)
624 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
627 skb_queue_purge(&(info->txq));
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)))
644 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
646 outb(0x08 | 0x20, iobase + 0x30);
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)))
661 bluecard_hci_flush(hdev);
663 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
665 outb(0x00, iobase + 0x30);
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);
678 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
682 info = (bluecard_info_t *)(hdev->driver_data);
684 switch (skb->pkt_type) {
685 case HCI_COMMAND_PKT:
688 case HCI_ACLDATA_PKT:
691 case HCI_SCODATA_PKT:
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);
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)
718 /* ======================== Card services HCI interaction ======================== */
721 int bluecard_open(bluecard_info_t *info)
723 unsigned int iobase = info->link.io.BasePort1;
724 struct hci_dev *hdev;
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;
739 /* Initialize HCI device */
740 hdev = hci_alloc_dev();
742 BT_ERR("Can't allocate HCI device");
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));
766 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
769 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
772 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
773 outb(info->ctrl_reg, iobase + REG_CONTROL);
776 outb(0x80, iobase + 0x30);
779 set_current_state(TASK_INTERRUPTIBLE);
780 schedule_timeout(HZ / 100);
783 outb(0x00, iobase + 0x30);
786 info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
787 outb(info->ctrl_reg, iobase + REG_CONTROL);
789 /* Enable interrupt */
790 outb(0xff, iobase + REG_INTERRUPT);
791 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
792 outb(info->ctrl_reg, iobase + REG_CONTROL);
794 if ((id & 0x0f) == 0x03) {
796 info->ctrl_reg |= REG_CONTROL_RTS;
797 outb(info->ctrl_reg, iobase + REG_CONTROL);
800 info->ctrl_reg |= 0x03;
801 outb(info->ctrl_reg, iobase + REG_CONTROL);
804 info->ctrl_reg &= ~REG_CONTROL_RTS;
805 outb(info->ctrl_reg, iobase + REG_CONTROL);
807 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
808 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
809 set_bit(XMIT_SENDING_READY, &(info->tx_state));
812 /* Start the RX buffers */
813 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
814 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
816 /* Signal that the hardware is ready */
817 set_bit(CARD_READY, &(info->hw_state));
820 skb_queue_purge(&(info->txq));
822 /* Control the point at which RTS is enabled */
823 outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
825 /* Timeout before it is safe to send the first HCI packet */
826 set_current_state(TASK_INTERRUPTIBLE);
827 schedule_timeout((HZ * 5) / 4); // or set it to 3/2
829 /* Register HCI device */
830 if (hci_register_dev(hdev) < 0) {
831 BT_ERR("Can't register HCI device");
841 int bluecard_close(bluecard_info_t *info)
843 unsigned int iobase = info->link.io.BasePort1;
844 struct hci_dev *hdev = info->hdev;
849 bluecard_hci_close(hdev);
851 clear_bit(CARD_READY, &(info->hw_state));
854 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
855 outb(info->ctrl_reg, iobase + REG_CONTROL);
858 outb(0x80, iobase + 0x30);
860 if (hci_unregister_dev(hdev) < 0)
861 BT_ERR("Can't unregister HCI device %s", hdev->name);
868 dev_link_t *bluecard_attach(void)
870 bluecard_info_t *info;
871 client_reg_t client_reg;
875 /* Create new info device */
876 info = kmalloc(sizeof(*info), GFP_KERNEL);
879 memset(info, 0, sizeof(*info));
884 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
885 link->io.NumPorts1 = 8;
886 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
887 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
889 if (irq_list[0] == -1)
890 link->irq.IRQInfo2 = irq_mask;
892 for (i = 0; i < 4; i++)
893 link->irq.IRQInfo2 |= 1 << irq_list[i];
895 link->irq.Handler = bluecard_interrupt;
896 link->irq.Instance = info;
898 link->conf.Attributes = CONF_ENABLE_IRQ;
900 link->conf.IntType = INT_MEMORY_AND_IO;
902 /* Register with Card Services */
903 link->next = dev_list;
905 client_reg.dev_info = &dev_info;
906 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
907 client_reg.EventMask =
908 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
909 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
910 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
911 client_reg.event_handler = &bluecard_event;
912 client_reg.Version = 0x0210;
913 client_reg.event_callback_args.client_data = link;
915 ret = pcmcia_register_client(&link->handle, &client_reg);
916 if (ret != CS_SUCCESS) {
917 cs_error(link->handle, RegisterClient, ret);
918 bluecard_detach(link);
926 void bluecard_detach(dev_link_t *link)
928 bluecard_info_t *info = link->priv;
932 /* Locate device structure */
933 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
940 if (link->state & DEV_CONFIG)
941 bluecard_release(link);
944 ret = pcmcia_deregister_client(link->handle);
945 if (ret != CS_SUCCESS)
946 cs_error(link->handle, DeregisterClient, ret);
949 /* Unlink device structure, free bits */
956 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
960 i = pcmcia_get_first_tuple(handle, tuple);
962 return CS_NO_MORE_ITEMS;
964 i = pcmcia_get_tuple_data(handle, tuple);
968 return pcmcia_parse_tuple(handle, tuple, parse);
971 void bluecard_config(dev_link_t *link)
973 client_handle_t handle = link->handle;
974 bluecard_info_t *info = link->priv;
978 config_info_t config;
979 int i, n, last_ret, last_fn;
981 tuple.TupleData = (cisdata_t *)buf;
982 tuple.TupleOffset = 0;
983 tuple.TupleDataMax = 255;
984 tuple.Attributes = 0;
986 /* Get configuration register information */
987 tuple.DesiredTuple = CISTPL_CONFIG;
988 last_ret = first_tuple(handle, &tuple, &parse);
989 if (last_ret != CS_SUCCESS) {
990 last_fn = ParseTuple;
993 link->conf.ConfigBase = parse.config.base;
994 link->conf.Present = parse.config.rmask[0];
997 link->state |= DEV_CONFIG;
998 i = pcmcia_get_configuration_info(handle, &config);
999 link->conf.Vcc = config.Vcc;
1001 link->conf.ConfigIndex = 0x20;
1002 link->io.NumPorts1 = 64;
1003 link->io.IOAddrLines = 6;
1005 for (n = 0; n < 0x400; n += 0x40) {
1006 link->io.BasePort1 = n ^ 0x300;
1007 i = pcmcia_request_io(link->handle, &link->io);
1008 if (i == CS_SUCCESS)
1012 if (i != CS_SUCCESS) {
1013 cs_error(link->handle, RequestIO, i);
1017 i = pcmcia_request_irq(link->handle, &link->irq);
1018 if (i != CS_SUCCESS) {
1019 cs_error(link->handle, RequestIRQ, i);
1020 link->irq.AssignedIRQ = 0;
1023 i = pcmcia_request_configuration(link->handle, &link->conf);
1024 if (i != CS_SUCCESS) {
1025 cs_error(link->handle, RequestConfiguration, i);
1029 if (bluecard_open(info) != 0)
1032 strcpy(info->node.dev_name, info->hdev->name);
1033 link->dev = &info->node;
1034 link->state &= ~DEV_CONFIG_PENDING;
1039 cs_error(link->handle, last_fn, last_ret);
1042 bluecard_release(link);
1046 void bluecard_release(dev_link_t *link)
1048 bluecard_info_t *info = link->priv;
1050 if (link->state & DEV_PRESENT)
1051 bluecard_close(info);
1053 del_timer(&(info->timer));
1057 pcmcia_release_configuration(link->handle);
1058 pcmcia_release_io(link->handle, &link->io);
1059 pcmcia_release_irq(link->handle, &link->irq);
1061 link->state &= ~DEV_CONFIG;
1065 int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1067 dev_link_t *link = args->client_data;
1068 bluecard_info_t *info = link->priv;
1071 case CS_EVENT_CARD_REMOVAL:
1072 link->state &= ~DEV_PRESENT;
1073 if (link->state & DEV_CONFIG) {
1074 bluecard_close(info);
1075 bluecard_release(link);
1078 case CS_EVENT_CARD_INSERTION:
1079 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1080 bluecard_config(link);
1082 case CS_EVENT_PM_SUSPEND:
1083 link->state |= DEV_SUSPEND;
1084 /* Fall through... */
1085 case CS_EVENT_RESET_PHYSICAL:
1086 if (link->state & DEV_CONFIG)
1087 pcmcia_release_configuration(link->handle);
1089 case CS_EVENT_PM_RESUME:
1090 link->state &= ~DEV_SUSPEND;
1091 /* Fall through... */
1092 case CS_EVENT_CARD_RESET:
1094 pcmcia_request_configuration(link->handle, &link->conf);
1101 static struct pcmcia_driver bluecard_driver = {
1102 .owner = THIS_MODULE,
1104 .name = "bluecard_cs",
1106 .attach = bluecard_attach,
1107 .detach = bluecard_detach,
1110 static int __init init_bluecard_cs(void)
1112 return pcmcia_register_driver(&bluecard_driver);
1116 static void __exit exit_bluecard_cs(void)
1118 pcmcia_unregister_driver(&bluecard_driver);
1120 /* XXX: this really needs to move into generic code.. */
1121 while (dev_list != NULL)
1122 bluecard_detach(dev_list);
1125 module_init(init_bluecard_cs);
1126 module_exit(exit_bluecard_cs);