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/skbuff.h>
40 #include <pcmcia/version.h>
41 #include <pcmcia/cs_types.h>
42 #include <pcmcia/cs.h>
43 #include <pcmcia/cistpl.h>
44 #include <pcmcia/ciscode.h>
45 #include <pcmcia/ds.h>
46 #include <pcmcia/cisreg.h>
48 #include <net/bluetooth/bluetooth.h>
49 #include <net/bluetooth/hci_core.h>
53 /* ======================== Module parameters ======================== */
56 /* Bit map of interrupts to choose from */
57 static u_int irq_mask = 0x86bc;
58 static int irq_list[4] = { -1 };
60 MODULE_PARM(irq_mask, "i");
61 MODULE_PARM(irq_list, "1-4i");
63 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
64 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
65 MODULE_LICENSE("GPL");
69 /* ======================== Local structures ======================== */
72 typedef struct bluecard_info_t {
78 spinlock_t lock; /* For serializing operations */
79 struct timer_list timer; /* For LED control */
81 struct sk_buff_head txq;
82 unsigned long tx_state;
84 unsigned long rx_state;
85 unsigned long rx_count;
86 struct sk_buff *rx_skb;
88 unsigned char ctrl_reg;
89 unsigned long hw_state; /* Status of the hardware and LED control */
93 void bluecard_config(dev_link_t *link);
94 void bluecard_release(dev_link_t *link);
95 int bluecard_event(event_t event, int priority, event_callback_args_t *args);
97 static dev_info_t dev_info = "bluecard_cs";
99 dev_link_t *bluecard_attach(void);
100 void bluecard_detach(dev_link_t *);
102 static dev_link_t *dev_list = NULL;
105 /* Default baud rate: 57600, 115200, 230400 or 460800 */
106 #define DEFAULT_BAUD_RATE 230400
109 /* Hardware states */
111 #define CARD_HAS_PCCARD_ID 4
112 #define CARD_HAS_POWER_LED 5
113 #define CARD_HAS_ACTIVITY_LED 6
115 /* Transmit states */
116 #define XMIT_SENDING 1
117 #define XMIT_WAKEUP 2
118 #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
119 #define XMIT_BUF_ONE_READY 6
120 #define XMIT_BUF_TWO_READY 7
121 #define XMIT_SENDING_READY 8
123 /* Receiver states */
124 #define RECV_WAIT_PACKET_TYPE 0
125 #define RECV_WAIT_EVENT_HEADER 1
126 #define RECV_WAIT_ACL_HEADER 2
127 #define RECV_WAIT_SCO_HEADER 3
128 #define RECV_WAIT_DATA 4
130 /* Special packet types */
131 #define PKT_BAUD_RATE_57600 0x80
132 #define PKT_BAUD_RATE_115200 0x81
133 #define PKT_BAUD_RATE_230400 0x82
134 #define PKT_BAUD_RATE_460800 0x83
137 /* These are the register offsets */
138 #define REG_COMMAND 0x20
139 #define REG_INTERRUPT 0x21
140 #define REG_CONTROL 0x22
141 #define REG_RX_CONTROL 0x24
142 #define REG_CARD_RESET 0x30
143 #define REG_LED_CTRL 0x30
146 #define REG_COMMAND_TX_BUF_ONE 0x01
147 #define REG_COMMAND_TX_BUF_TWO 0x02
148 #define REG_COMMAND_RX_BUF_ONE 0x04
149 #define REG_COMMAND_RX_BUF_TWO 0x08
150 #define REG_COMMAND_RX_WIN_ONE 0x00
151 #define REG_COMMAND_RX_WIN_TWO 0x10
154 #define REG_CONTROL_BAUD_RATE_57600 0x00
155 #define REG_CONTROL_BAUD_RATE_115200 0x01
156 #define REG_CONTROL_BAUD_RATE_230400 0x02
157 #define REG_CONTROL_BAUD_RATE_460800 0x03
158 #define REG_CONTROL_RTS 0x04
159 #define REG_CONTROL_BT_ON 0x08
160 #define REG_CONTROL_BT_RESET 0x10
161 #define REG_CONTROL_BT_RES_PU 0x20
162 #define REG_CONTROL_INTERRUPT 0x40
163 #define REG_CONTROL_CARD_RESET 0x80
166 #define RTS_LEVEL_SHIFT_BITS 0x02
170 /* ======================== LED handling routines ======================== */
173 void bluecard_activity_led_timeout(u_long arg)
175 bluecard_info_t *info = (bluecard_info_t *)arg;
176 unsigned int iobase = info->link.io.BasePort1;
178 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
181 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
182 /* Disable activity LED */
183 outb(0x08 | 0x20, iobase + 0x30);
185 /* Disable power LED */
186 outb(0x00, iobase + 0x30);
191 static void bluecard_enable_activity_led(bluecard_info_t *info)
193 unsigned int iobase = info->link.io.BasePort1;
195 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
198 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
199 /* Enable activity LED */
200 outb(0x10 | 0x40, iobase + 0x30);
202 /* Stop the LED after HZ/4 */
203 mod_timer(&(info->timer), jiffies + HZ / 4);
205 /* Enable power LED */
206 outb(0x08 | 0x20, iobase + 0x30);
208 /* Stop the LED after HZ/2 */
209 mod_timer(&(info->timer), jiffies + HZ / 2);
215 /* ======================== Interrupt handling ======================== */
218 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
222 actual = (len > 15) ? 15 : len;
224 outb_p(actual, iobase + offset);
226 for (i = 0; i < actual; i++)
227 outb_p(buf[i], iobase + offset + i + 1);
233 static void bluecard_write_wakeup(bluecard_info_t *info)
236 BT_ERR("Unknown device");
240 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
243 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
244 set_bit(XMIT_WAKEUP, &(info->tx_state));
249 register unsigned int iobase = info->link.io.BasePort1;
250 register unsigned int offset;
251 register unsigned char command;
252 register unsigned long ready_bit;
253 register struct sk_buff *skb;
256 clear_bit(XMIT_WAKEUP, &(info->tx_state));
258 if (!(info->link.state & DEV_PRESENT))
261 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
262 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
265 command = REG_COMMAND_TX_BUF_TWO;
266 ready_bit = XMIT_BUF_TWO_READY;
268 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
271 command = REG_COMMAND_TX_BUF_ONE;
272 ready_bit = XMIT_BUF_ONE_READY;
275 if (!(skb = skb_dequeue(&(info->txq))))
278 if (skb->pkt_type & 0x80) {
280 info->ctrl_reg |= REG_CONTROL_RTS;
281 outb(info->ctrl_reg, iobase + REG_CONTROL);
285 bluecard_enable_activity_led(info);
288 len = bluecard_write(iobase, offset, skb->data, skb->len);
290 /* Tell the FPGA to send the data */
291 outb_p(command, iobase + REG_COMMAND);
293 /* Mark the buffer as dirty */
294 clear_bit(ready_bit, &(info->tx_state));
296 if (skb->pkt_type & 0x80) {
298 wait_queue_head_t wait;
299 unsigned char baud_reg;
301 switch (skb->pkt_type) {
302 case PKT_BAUD_RATE_460800:
303 baud_reg = REG_CONTROL_BAUD_RATE_460800;
305 case PKT_BAUD_RATE_230400:
306 baud_reg = REG_CONTROL_BAUD_RATE_230400;
308 case PKT_BAUD_RATE_115200:
309 baud_reg = REG_CONTROL_BAUD_RATE_115200;
311 case PKT_BAUD_RATE_57600:
312 /* Fall through... */
314 baud_reg = REG_CONTROL_BAUD_RATE_57600;
318 /* Wait until the command reaches the baseband */
319 init_waitqueue_head(&wait);
320 interruptible_sleep_on_timeout(&wait, HZ / 10);
322 /* Set baud on baseband */
323 info->ctrl_reg &= ~0x03;
324 info->ctrl_reg |= baud_reg;
325 outb(info->ctrl_reg, iobase + REG_CONTROL);
328 info->ctrl_reg &= ~REG_CONTROL_RTS;
329 outb(info->ctrl_reg, iobase + REG_CONTROL);
331 /* Wait before the next HCI packet can be send */
332 interruptible_sleep_on_timeout(&wait, HZ);
336 if (len == skb->len) {
340 skb_queue_head(&(info->txq), skb);
343 info->hdev->stat.byte_tx += len;
346 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
348 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
350 clear_bit(XMIT_SENDING, &(info->tx_state));
354 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
358 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
360 len = inb(iobase + offset);
367 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
371 buf[n] = inb(iobase + offset + i);
382 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
385 unsigned char buf[31];
389 BT_ERR("Unknown device");
393 iobase = info->link.io.BasePort1;
395 if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
396 bluecard_enable_activity_led(info);
398 len = bluecard_read(iobase, offset, buf, sizeof(buf));
400 for (i = 0; i < len; i++) {
402 /* Allocate packet */
403 if (info->rx_skb == NULL) {
404 info->rx_state = RECV_WAIT_PACKET_TYPE;
406 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
407 BT_ERR("Can't allocate mem for new packet");
412 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
414 info->rx_skb->dev = (void *) info->hdev;
415 info->rx_skb->pkt_type = buf[i];
417 switch (info->rx_skb->pkt_type) {
421 if (offset != 0x00) {
422 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
423 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
424 set_bit(XMIT_SENDING_READY, &(info->tx_state));
425 bluecard_write_wakeup(info);
428 kfree_skb(info->rx_skb);
433 info->rx_state = RECV_WAIT_EVENT_HEADER;
434 info->rx_count = HCI_EVENT_HDR_SIZE;
437 case HCI_ACLDATA_PKT:
438 info->rx_state = RECV_WAIT_ACL_HEADER;
439 info->rx_count = HCI_ACL_HDR_SIZE;
442 case HCI_SCODATA_PKT:
443 info->rx_state = RECV_WAIT_SCO_HEADER;
444 info->rx_count = HCI_SCO_HDR_SIZE;
449 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
450 info->hdev->stat.err_rx++;
452 kfree_skb(info->rx_skb);
460 *skb_put(info->rx_skb, 1) = buf[i];
463 if (info->rx_count == 0) {
466 struct hci_event_hdr *eh;
467 struct hci_acl_hdr *ah;
468 struct hci_sco_hdr *sh;
470 switch (info->rx_state) {
472 case RECV_WAIT_EVENT_HEADER:
473 eh = (struct hci_event_hdr *)(info->rx_skb->data);
474 info->rx_state = RECV_WAIT_DATA;
475 info->rx_count = eh->plen;
478 case RECV_WAIT_ACL_HEADER:
479 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
480 dlen = __le16_to_cpu(ah->dlen);
481 info->rx_state = RECV_WAIT_DATA;
482 info->rx_count = dlen;
485 case RECV_WAIT_SCO_HEADER:
486 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
487 info->rx_state = RECV_WAIT_DATA;
488 info->rx_count = sh->dlen;
492 hci_recv_frame(info->rx_skb);
505 info->hdev->stat.byte_rx += len;
509 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
511 bluecard_info_t *info = dev_inst;
515 if (!info || !info->hdev) {
516 BT_ERR("Call of irq %d for unknown device", irq);
520 if (!test_bit(CARD_READY, &(info->hw_state)))
523 iobase = info->link.io.BasePort1;
525 spin_lock(&(info->lock));
527 /* Disable interrupt */
528 info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
529 outb(info->ctrl_reg, iobase + REG_CONTROL);
531 reg = inb(iobase + REG_INTERRUPT);
533 if ((reg != 0x00) && (reg != 0xff)) {
536 bluecard_receive(info, 0x00);
537 outb(0x04, iobase + REG_INTERRUPT);
538 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
542 bluecard_receive(info, 0x10);
543 outb(0x08, iobase + REG_INTERRUPT);
544 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
548 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
549 outb(0x01, iobase + REG_INTERRUPT);
550 bluecard_write_wakeup(info);
554 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
555 outb(0x02, iobase + REG_INTERRUPT);
556 bluecard_write_wakeup(info);
561 /* Enable interrupt */
562 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
563 outb(info->ctrl_reg, iobase + REG_CONTROL);
565 spin_unlock(&(info->lock));
572 /* ======================== Device specific HCI commands ======================== */
575 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
577 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
580 /* Ericsson baud rate command */
581 unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
583 if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
584 BT_ERR("Can't allocate mem for new packet");
591 skb->pkt_type = PKT_BAUD_RATE_460800;
595 skb->pkt_type = PKT_BAUD_RATE_230400;
599 skb->pkt_type = PKT_BAUD_RATE_115200;
602 /* Fall through... */
605 skb->pkt_type = PKT_BAUD_RATE_57600;
609 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
611 skb_queue_tail(&(info->txq), skb);
613 bluecard_write_wakeup(info);
620 /* ======================== HCI interface ======================== */
623 static int bluecard_hci_flush(struct hci_dev *hdev)
625 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
628 skb_queue_purge(&(info->txq));
634 static int bluecard_hci_open(struct hci_dev *hdev)
636 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
637 unsigned int iobase = info->link.io.BasePort1;
639 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
640 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
642 if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
645 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
647 outb(0x08 | 0x20, iobase + 0x30);
654 static int bluecard_hci_close(struct hci_dev *hdev)
656 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
657 unsigned int iobase = info->link.io.BasePort1;
659 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
662 bluecard_hci_flush(hdev);
664 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
666 outb(0x00, iobase + 0x30);
673 static int bluecard_hci_send_frame(struct sk_buff *skb)
675 bluecard_info_t *info;
676 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
679 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
683 info = (bluecard_info_t *)(hdev->driver_data);
685 switch (skb->pkt_type) {
686 case HCI_COMMAND_PKT:
689 case HCI_ACLDATA_PKT:
692 case HCI_SCODATA_PKT:
697 /* Prepend skb with frame type */
698 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
699 skb_queue_tail(&(info->txq), skb);
701 bluecard_write_wakeup(info);
707 static void bluecard_hci_destruct(struct hci_dev *hdev)
712 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
719 /* ======================== Card services HCI interaction ======================== */
722 int bluecard_open(bluecard_info_t *info)
724 unsigned int iobase = info->link.io.BasePort1;
725 struct hci_dev *hdev;
728 spin_lock_init(&(info->lock));
730 init_timer(&(info->timer));
731 info->timer.function = &bluecard_activity_led_timeout;
732 info->timer.data = (u_long)info;
734 skb_queue_head_init(&(info->txq));
736 info->rx_state = RECV_WAIT_PACKET_TYPE;
740 /* Initialize HCI device */
741 hdev = hci_alloc_dev();
743 BT_ERR("Can't allocate HCI device");
749 hdev->type = HCI_PCCARD;
750 hdev->driver_data = info;
752 hdev->open = bluecard_hci_open;
753 hdev->close = bluecard_hci_close;
754 hdev->flush = bluecard_hci_flush;
755 hdev->send = bluecard_hci_send_frame;
756 hdev->destruct = bluecard_hci_destruct;
757 hdev->ioctl = bluecard_hci_ioctl;
759 hdev->owner = THIS_MODULE;
761 id = inb(iobase + 0x30);
763 if ((id & 0x0f) == 0x02)
764 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
767 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
770 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
773 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
774 outb(info->ctrl_reg, iobase + REG_CONTROL);
777 outb(0x80, iobase + 0x30);
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 */
828 /* Register HCI device */
829 if (hci_register_dev(hdev) < 0) {
830 BT_ERR("Can't register HCI device");
840 int bluecard_close(bluecard_info_t *info)
842 unsigned int iobase = info->link.io.BasePort1;
843 struct hci_dev *hdev = info->hdev;
848 bluecard_hci_close(hdev);
850 clear_bit(CARD_READY, &(info->hw_state));
853 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
854 outb(info->ctrl_reg, iobase + REG_CONTROL);
857 outb(0x80, iobase + 0x30);
859 if (hci_unregister_dev(hdev) < 0)
860 BT_ERR("Can't unregister HCI device %s", hdev->name);
867 dev_link_t *bluecard_attach(void)
869 bluecard_info_t *info;
870 client_reg_t client_reg;
874 /* Create new info device */
875 info = kmalloc(sizeof(*info), GFP_KERNEL);
878 memset(info, 0, sizeof(*info));
883 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
884 link->io.NumPorts1 = 8;
885 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
886 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
888 if (irq_list[0] == -1)
889 link->irq.IRQInfo2 = irq_mask;
891 for (i = 0; i < 4; i++)
892 link->irq.IRQInfo2 |= 1 << irq_list[i];
894 link->irq.Handler = bluecard_interrupt;
895 link->irq.Instance = info;
897 link->conf.Attributes = CONF_ENABLE_IRQ;
899 link->conf.IntType = INT_MEMORY_AND_IO;
901 /* Register with Card Services */
902 link->next = dev_list;
904 client_reg.dev_info = &dev_info;
905 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
906 client_reg.EventMask =
907 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
908 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
909 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
910 client_reg.event_handler = &bluecard_event;
911 client_reg.Version = 0x0210;
912 client_reg.event_callback_args.client_data = link;
914 ret = pcmcia_register_client(&link->handle, &client_reg);
915 if (ret != CS_SUCCESS) {
916 cs_error(link->handle, RegisterClient, ret);
917 bluecard_detach(link);
925 void bluecard_detach(dev_link_t *link)
927 bluecard_info_t *info = link->priv;
931 /* Locate device structure */
932 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
939 if (link->state & DEV_CONFIG)
940 bluecard_release(link);
943 ret = pcmcia_deregister_client(link->handle);
944 if (ret != CS_SUCCESS)
945 cs_error(link->handle, DeregisterClient, ret);
948 /* Unlink device structure, free bits */
955 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
959 i = pcmcia_get_first_tuple(handle, tuple);
961 return CS_NO_MORE_ITEMS;
963 i = pcmcia_get_tuple_data(handle, tuple);
967 return pcmcia_parse_tuple(handle, tuple, parse);
970 void bluecard_config(dev_link_t *link)
972 client_handle_t handle = link->handle;
973 bluecard_info_t *info = link->priv;
977 config_info_t config;
978 int i, n, last_ret, last_fn;
980 tuple.TupleData = (cisdata_t *)buf;
981 tuple.TupleOffset = 0;
982 tuple.TupleDataMax = 255;
983 tuple.Attributes = 0;
985 /* Get configuration register information */
986 tuple.DesiredTuple = CISTPL_CONFIG;
987 last_ret = first_tuple(handle, &tuple, &parse);
988 if (last_ret != CS_SUCCESS) {
989 last_fn = ParseTuple;
992 link->conf.ConfigBase = parse.config.base;
993 link->conf.Present = parse.config.rmask[0];
996 link->state |= DEV_CONFIG;
997 i = pcmcia_get_configuration_info(handle, &config);
998 link->conf.Vcc = config.Vcc;
1000 link->conf.ConfigIndex = 0x20;
1001 link->io.NumPorts1 = 64;
1002 link->io.IOAddrLines = 6;
1004 for (n = 0; n < 0x400; n += 0x40) {
1005 link->io.BasePort1 = n ^ 0x300;
1006 i = pcmcia_request_io(link->handle, &link->io);
1007 if (i == CS_SUCCESS)
1011 if (i != CS_SUCCESS) {
1012 cs_error(link->handle, RequestIO, i);
1016 i = pcmcia_request_irq(link->handle, &link->irq);
1017 if (i != CS_SUCCESS) {
1018 cs_error(link->handle, RequestIRQ, i);
1019 link->irq.AssignedIRQ = 0;
1022 i = pcmcia_request_configuration(link->handle, &link->conf);
1023 if (i != CS_SUCCESS) {
1024 cs_error(link->handle, RequestConfiguration, i);
1028 if (bluecard_open(info) != 0)
1031 strcpy(info->node.dev_name, info->hdev->name);
1032 link->dev = &info->node;
1033 link->state &= ~DEV_CONFIG_PENDING;
1038 cs_error(link->handle, last_fn, last_ret);
1041 bluecard_release(link);
1045 void bluecard_release(dev_link_t *link)
1047 bluecard_info_t *info = link->priv;
1049 if (link->state & DEV_PRESENT)
1050 bluecard_close(info);
1052 del_timer(&(info->timer));
1056 pcmcia_release_configuration(link->handle);
1057 pcmcia_release_io(link->handle, &link->io);
1058 pcmcia_release_irq(link->handle, &link->irq);
1060 link->state &= ~DEV_CONFIG;
1064 int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1066 dev_link_t *link = args->client_data;
1067 bluecard_info_t *info = link->priv;
1070 case CS_EVENT_CARD_REMOVAL:
1071 link->state &= ~DEV_PRESENT;
1072 if (link->state & DEV_CONFIG) {
1073 bluecard_close(info);
1074 bluecard_release(link);
1077 case CS_EVENT_CARD_INSERTION:
1078 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1079 bluecard_config(link);
1081 case CS_EVENT_PM_SUSPEND:
1082 link->state |= DEV_SUSPEND;
1083 /* Fall through... */
1084 case CS_EVENT_RESET_PHYSICAL:
1085 if (link->state & DEV_CONFIG)
1086 pcmcia_release_configuration(link->handle);
1088 case CS_EVENT_PM_RESUME:
1089 link->state &= ~DEV_SUSPEND;
1090 /* Fall through... */
1091 case CS_EVENT_CARD_RESET:
1093 pcmcia_request_configuration(link->handle, &link->conf);
1100 static struct pcmcia_driver bluecard_driver = {
1101 .owner = THIS_MODULE,
1103 .name = "bluecard_cs",
1105 .attach = bluecard_attach,
1106 .detach = bluecard_detach,
1109 static int __init init_bluecard_cs(void)
1111 return pcmcia_register_driver(&bluecard_driver);
1115 static void __exit exit_bluecard_cs(void)
1117 pcmcia_unregister_driver(&bluecard_driver);
1119 /* XXX: this really needs to move into generic code.. */
1120 while (dev_list != NULL)
1121 bluecard_detach(dev_list);
1124 module_init(init_bluecard_cs);
1125 module_exit(exit_bluecard_cs);