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>
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 {
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 */
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 */
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
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
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
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)))
177 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
178 /* Disable activity LED */
179 outb(0x08 | 0x20, iobase + 0x30);
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)))
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);
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)
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);
229 static void bluecard_write_wakeup(bluecard_info_t *info)
232 BT_ERR("Unknown device");
236 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
239 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
240 set_bit(XMIT_WAKEUP, &(info->tx_state));
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;
252 clear_bit(XMIT_WAKEUP, &(info->tx_state));
254 if (!(info->link.state & DEV_PRESENT))
257 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
258 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
261 command = REG_COMMAND_TX_BUF_TWO;
262 ready_bit = XMIT_BUF_TWO_READY;
264 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
267 command = REG_COMMAND_TX_BUF_ONE;
268 ready_bit = XMIT_BUF_ONE_READY;
271 if (!(skb = skb_dequeue(&(info->txq))))
274 if (skb->pkt_type & 0x80) {
276 info->ctrl_reg |= REG_CONTROL_RTS;
277 outb(info->ctrl_reg, iobase + REG_CONTROL);
281 bluecard_enable_activity_led(info);
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);
296 unsigned char baud_reg;
298 switch (skb->pkt_type) {
299 case PKT_BAUD_RATE_460800:
300 baud_reg = REG_CONTROL_BAUD_RATE_460800;
302 case PKT_BAUD_RATE_230400:
303 baud_reg = REG_CONTROL_BAUD_RATE_230400;
305 case PKT_BAUD_RATE_115200:
306 baud_reg = REG_CONTROL_BAUD_RATE_115200;
308 case PKT_BAUD_RATE_57600:
309 /* Fall through... */
311 baud_reg = REG_CONTROL_BAUD_RATE_57600;
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);
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) {
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 static 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);
782 outb(0x00, iobase + 0x30);
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) {
795 info->ctrl_reg |= REG_CONTROL_RTS;
796 outb(info->ctrl_reg, iobase + REG_CONTROL);
799 info->ctrl_reg |= 0x03;
800 outb(info->ctrl_reg, iobase + REG_CONTROL);
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));
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 */
827 /* Register HCI device */
828 if (hci_register_dev(hdev) < 0) {
829 BT_ERR("Can't register HCI device");
839 static int bluecard_close(bluecard_info_t *info)
841 unsigned int iobase = info->link.io.BasePort1;
842 struct hci_dev *hdev = info->hdev;
847 bluecard_hci_close(hdev);
849 clear_bit(CARD_READY, &(info->hw_state));
852 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
853 outb(info->ctrl_reg, iobase + REG_CONTROL);
856 outb(0x80, iobase + 0x30);
858 if (hci_unregister_dev(hdev) < 0)
859 BT_ERR("Can't unregister HCI device %s", hdev->name);
866 static dev_link_t *bluecard_attach(void)
868 bluecard_info_t *info;
869 client_reg_t client_reg;
873 /* Create new info device */
874 info = kmalloc(sizeof(*info), GFP_KERNEL);
877 memset(info, 0, sizeof(*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;
892 link->conf.IntType = INT_MEMORY_AND_IO;
894 /* Register with Card Services */
895 link->next = dev_list;
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);
917 static void bluecard_detach(dev_link_t *link)
919 bluecard_info_t *info = link->priv;
923 /* Locate device structure */
924 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
931 if (link->state & DEV_CONFIG)
932 bluecard_release(link);
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 */
947 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
951 i = pcmcia_get_first_tuple(handle, tuple);
953 return CS_NO_MORE_ITEMS;
955 i = pcmcia_get_tuple_data(handle, tuple);
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;
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;
984 link->conf.ConfigBase = parse.config.base;
985 link->conf.Present = parse.config.rmask[0];
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);
1003 if (i != CS_SUCCESS) {
1004 cs_error(link->handle, RequestIO, i);
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);
1020 if (bluecard_open(info) != 0)
1023 strcpy(info->node.dev_name, info->hdev->name);
1024 link->dev = &info->node;
1025 link->state &= ~DEV_CONFIG_PENDING;
1030 cs_error(link->handle, last_fn, last_ret);
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));
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;
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);
1069 case CS_EVENT_CARD_INSERTION:
1070 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1071 bluecard_config(link);
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);
1080 case CS_EVENT_PM_RESUME:
1081 link->state &= ~DEV_SUSPEND;
1082 /* Fall through... */
1083 case CS_EVENT_CARD_RESET:
1085 pcmcia_request_configuration(link->handle, &link->conf);
1092 static struct pcmcia_driver bluecard_driver = {
1093 .owner = THIS_MODULE,
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);