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>
39 #include <linux/skbuff.h>
42 #include <pcmcia/version.h>
43 #include <pcmcia/cs_types.h>
44 #include <pcmcia/cs.h>
45 #include <pcmcia/cistpl.h>
46 #include <pcmcia/ciscode.h>
47 #include <pcmcia/ds.h>
48 #include <pcmcia/cisreg.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
55 /* ======================== Module parameters ======================== */
58 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
59 MODULE_DESCRIPTION("Bluetooth driver for the Anycom BlueCard (LSE039/LSE041)");
60 MODULE_LICENSE("GPL");
64 /* ======================== Local structures ======================== */
67 typedef struct bluecard_info_t {
73 spinlock_t lock; /* For serializing operations */
74 struct timer_list timer; /* For LED control */
76 struct sk_buff_head txq;
77 unsigned long tx_state;
79 unsigned long rx_state;
80 unsigned long rx_count;
81 struct sk_buff *rx_skb;
83 unsigned char ctrl_reg;
84 unsigned long hw_state; /* Status of the hardware and LED control */
88 static void bluecard_config(dev_link_t *link);
89 static void bluecard_release(dev_link_t *link);
90 static int bluecard_event(event_t event, int priority, event_callback_args_t *args);
92 static dev_info_t dev_info = "bluecard_cs";
94 static dev_link_t *bluecard_attach(void);
95 static void bluecard_detach(dev_link_t *);
97 static dev_link_t *dev_list = NULL;
100 /* Default baud rate: 57600, 115200, 230400 or 460800 */
101 #define DEFAULT_BAUD_RATE 230400
104 /* Hardware states */
106 #define CARD_HAS_PCCARD_ID 4
107 #define CARD_HAS_POWER_LED 5
108 #define CARD_HAS_ACTIVITY_LED 6
110 /* Transmit states */
111 #define XMIT_SENDING 1
112 #define XMIT_WAKEUP 2
113 #define XMIT_BUFFER_NUMBER 5 /* unset = buffer one, set = buffer two */
114 #define XMIT_BUF_ONE_READY 6
115 #define XMIT_BUF_TWO_READY 7
116 #define XMIT_SENDING_READY 8
118 /* Receiver states */
119 #define RECV_WAIT_PACKET_TYPE 0
120 #define RECV_WAIT_EVENT_HEADER 1
121 #define RECV_WAIT_ACL_HEADER 2
122 #define RECV_WAIT_SCO_HEADER 3
123 #define RECV_WAIT_DATA 4
125 /* Special packet types */
126 #define PKT_BAUD_RATE_57600 0x80
127 #define PKT_BAUD_RATE_115200 0x81
128 #define PKT_BAUD_RATE_230400 0x82
129 #define PKT_BAUD_RATE_460800 0x83
132 /* These are the register offsets */
133 #define REG_COMMAND 0x20
134 #define REG_INTERRUPT 0x21
135 #define REG_CONTROL 0x22
136 #define REG_RX_CONTROL 0x24
137 #define REG_CARD_RESET 0x30
138 #define REG_LED_CTRL 0x30
141 #define REG_COMMAND_TX_BUF_ONE 0x01
142 #define REG_COMMAND_TX_BUF_TWO 0x02
143 #define REG_COMMAND_RX_BUF_ONE 0x04
144 #define REG_COMMAND_RX_BUF_TWO 0x08
145 #define REG_COMMAND_RX_WIN_ONE 0x00
146 #define REG_COMMAND_RX_WIN_TWO 0x10
149 #define REG_CONTROL_BAUD_RATE_57600 0x00
150 #define REG_CONTROL_BAUD_RATE_115200 0x01
151 #define REG_CONTROL_BAUD_RATE_230400 0x02
152 #define REG_CONTROL_BAUD_RATE_460800 0x03
153 #define REG_CONTROL_RTS 0x04
154 #define REG_CONTROL_BT_ON 0x08
155 #define REG_CONTROL_BT_RESET 0x10
156 #define REG_CONTROL_BT_RES_PU 0x20
157 #define REG_CONTROL_INTERRUPT 0x40
158 #define REG_CONTROL_CARD_RESET 0x80
161 #define RTS_LEVEL_SHIFT_BITS 0x02
165 /* ======================== LED handling routines ======================== */
168 static void bluecard_activity_led_timeout(u_long arg)
170 bluecard_info_t *info = (bluecard_info_t *)arg;
171 unsigned int iobase = info->link.io.BasePort1;
173 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
176 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
177 /* Disable activity LED */
178 outb(0x08 | 0x20, iobase + 0x30);
180 /* Disable power LED */
181 outb(0x00, iobase + 0x30);
186 static void bluecard_enable_activity_led(bluecard_info_t *info)
188 unsigned int iobase = info->link.io.BasePort1;
190 if (!test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
193 if (test_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state))) {
194 /* Enable activity LED */
195 outb(0x10 | 0x40, iobase + 0x30);
197 /* Stop the LED after HZ/4 */
198 mod_timer(&(info->timer), jiffies + HZ / 4);
200 /* Enable power LED */
201 outb(0x08 | 0x20, iobase + 0x30);
203 /* Stop the LED after HZ/2 */
204 mod_timer(&(info->timer), jiffies + HZ / 2);
210 /* ======================== Interrupt handling ======================== */
213 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
217 actual = (len > 15) ? 15 : len;
219 outb_p(actual, iobase + offset);
221 for (i = 0; i < actual; i++)
222 outb_p(buf[i], iobase + offset + i + 1);
228 static void bluecard_write_wakeup(bluecard_info_t *info)
231 BT_ERR("Unknown device");
235 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
238 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
239 set_bit(XMIT_WAKEUP, &(info->tx_state));
244 register unsigned int iobase = info->link.io.BasePort1;
245 register unsigned int offset;
246 register unsigned char command;
247 register unsigned long ready_bit;
248 register struct sk_buff *skb;
251 clear_bit(XMIT_WAKEUP, &(info->tx_state));
253 if (!(info->link.state & DEV_PRESENT))
256 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
257 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
260 command = REG_COMMAND_TX_BUF_TWO;
261 ready_bit = XMIT_BUF_TWO_READY;
263 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
266 command = REG_COMMAND_TX_BUF_ONE;
267 ready_bit = XMIT_BUF_ONE_READY;
270 if (!(skb = skb_dequeue(&(info->txq))))
273 if (skb->pkt_type & 0x80) {
275 info->ctrl_reg |= REG_CONTROL_RTS;
276 outb(info->ctrl_reg, iobase + REG_CONTROL);
280 bluecard_enable_activity_led(info);
283 len = bluecard_write(iobase, offset, skb->data, skb->len);
285 /* Tell the FPGA to send the data */
286 outb_p(command, iobase + REG_COMMAND);
288 /* Mark the buffer as dirty */
289 clear_bit(ready_bit, &(info->tx_state));
291 if (skb->pkt_type & 0x80) {
293 wait_queue_head_t wait;
294 unsigned char baud_reg;
296 switch (skb->pkt_type) {
297 case PKT_BAUD_RATE_460800:
298 baud_reg = REG_CONTROL_BAUD_RATE_460800;
300 case PKT_BAUD_RATE_230400:
301 baud_reg = REG_CONTROL_BAUD_RATE_230400;
303 case PKT_BAUD_RATE_115200:
304 baud_reg = REG_CONTROL_BAUD_RATE_115200;
306 case PKT_BAUD_RATE_57600:
307 /* Fall through... */
309 baud_reg = REG_CONTROL_BAUD_RATE_57600;
313 /* Wait until the command reaches the baseband */
314 init_waitqueue_head(&wait);
315 interruptible_sleep_on_timeout(&wait, HZ / 10);
317 /* Set baud on baseband */
318 info->ctrl_reg &= ~0x03;
319 info->ctrl_reg |= baud_reg;
320 outb(info->ctrl_reg, iobase + REG_CONTROL);
323 info->ctrl_reg &= ~REG_CONTROL_RTS;
324 outb(info->ctrl_reg, iobase + REG_CONTROL);
326 /* Wait before the next HCI packet can be send */
327 interruptible_sleep_on_timeout(&wait, HZ);
331 if (len == skb->len) {
335 skb_queue_head(&(info->txq), skb);
338 info->hdev->stat.byte_tx += len;
341 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
343 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
345 clear_bit(XMIT_SENDING, &(info->tx_state));
349 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
353 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
355 len = inb(iobase + offset);
362 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
366 buf[n] = inb(iobase + offset + i);
377 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
380 unsigned char buf[31];
384 BT_ERR("Unknown device");
388 iobase = info->link.io.BasePort1;
390 if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
391 bluecard_enable_activity_led(info);
393 len = bluecard_read(iobase, offset, buf, sizeof(buf));
395 for (i = 0; i < len; i++) {
397 /* Allocate packet */
398 if (info->rx_skb == NULL) {
399 info->rx_state = RECV_WAIT_PACKET_TYPE;
401 if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
402 BT_ERR("Can't allocate mem for new packet");
407 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
409 info->rx_skb->dev = (void *) info->hdev;
410 info->rx_skb->pkt_type = buf[i];
412 switch (info->rx_skb->pkt_type) {
416 if (offset != 0x00) {
417 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
418 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
419 set_bit(XMIT_SENDING_READY, &(info->tx_state));
420 bluecard_write_wakeup(info);
423 kfree_skb(info->rx_skb);
428 info->rx_state = RECV_WAIT_EVENT_HEADER;
429 info->rx_count = HCI_EVENT_HDR_SIZE;
432 case HCI_ACLDATA_PKT:
433 info->rx_state = RECV_WAIT_ACL_HEADER;
434 info->rx_count = HCI_ACL_HDR_SIZE;
437 case HCI_SCODATA_PKT:
438 info->rx_state = RECV_WAIT_SCO_HEADER;
439 info->rx_count = HCI_SCO_HDR_SIZE;
444 BT_ERR("Unknown HCI packet with type 0x%02x received", info->rx_skb->pkt_type);
445 info->hdev->stat.err_rx++;
447 kfree_skb(info->rx_skb);
455 *skb_put(info->rx_skb, 1) = buf[i];
458 if (info->rx_count == 0) {
461 struct hci_event_hdr *eh;
462 struct hci_acl_hdr *ah;
463 struct hci_sco_hdr *sh;
465 switch (info->rx_state) {
467 case RECV_WAIT_EVENT_HEADER:
468 eh = (struct hci_event_hdr *)(info->rx_skb->data);
469 info->rx_state = RECV_WAIT_DATA;
470 info->rx_count = eh->plen;
473 case RECV_WAIT_ACL_HEADER:
474 ah = (struct hci_acl_hdr *)(info->rx_skb->data);
475 dlen = __le16_to_cpu(ah->dlen);
476 info->rx_state = RECV_WAIT_DATA;
477 info->rx_count = dlen;
480 case RECV_WAIT_SCO_HEADER:
481 sh = (struct hci_sco_hdr *)(info->rx_skb->data);
482 info->rx_state = RECV_WAIT_DATA;
483 info->rx_count = sh->dlen;
487 hci_recv_frame(info->rx_skb);
500 info->hdev->stat.byte_rx += len;
504 static irqreturn_t bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
506 bluecard_info_t *info = dev_inst;
510 if (!info || !info->hdev) {
511 BT_ERR("Call of irq %d for unknown device", irq);
515 if (!test_bit(CARD_READY, &(info->hw_state)))
518 iobase = info->link.io.BasePort1;
520 spin_lock(&(info->lock));
522 /* Disable interrupt */
523 info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
524 outb(info->ctrl_reg, iobase + REG_CONTROL);
526 reg = inb(iobase + REG_INTERRUPT);
528 if ((reg != 0x00) && (reg != 0xff)) {
531 bluecard_receive(info, 0x00);
532 outb(0x04, iobase + REG_INTERRUPT);
533 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
537 bluecard_receive(info, 0x10);
538 outb(0x08, iobase + REG_INTERRUPT);
539 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
543 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
544 outb(0x01, iobase + REG_INTERRUPT);
545 bluecard_write_wakeup(info);
549 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
550 outb(0x02, iobase + REG_INTERRUPT);
551 bluecard_write_wakeup(info);
556 /* Enable interrupt */
557 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
558 outb(info->ctrl_reg, iobase + REG_CONTROL);
560 spin_unlock(&(info->lock));
567 /* ======================== Device specific HCI commands ======================== */
570 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
572 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
575 /* Ericsson baud rate command */
576 unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
578 if (!(skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
579 BT_ERR("Can't allocate mem for new packet");
586 skb->pkt_type = PKT_BAUD_RATE_460800;
590 skb->pkt_type = PKT_BAUD_RATE_230400;
594 skb->pkt_type = PKT_BAUD_RATE_115200;
597 /* Fall through... */
600 skb->pkt_type = PKT_BAUD_RATE_57600;
604 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
606 skb_queue_tail(&(info->txq), skb);
608 bluecard_write_wakeup(info);
615 /* ======================== HCI interface ======================== */
618 static int bluecard_hci_flush(struct hci_dev *hdev)
620 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
623 skb_queue_purge(&(info->txq));
629 static int bluecard_hci_open(struct hci_dev *hdev)
631 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
632 unsigned int iobase = info->link.io.BasePort1;
634 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state)))
635 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
637 if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
640 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
642 outb(0x08 | 0x20, iobase + 0x30);
649 static int bluecard_hci_close(struct hci_dev *hdev)
651 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
652 unsigned int iobase = info->link.io.BasePort1;
654 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
657 bluecard_hci_flush(hdev);
659 if (test_bit(CARD_HAS_PCCARD_ID, &(info->hw_state))) {
661 outb(0x00, iobase + 0x30);
668 static int bluecard_hci_send_frame(struct sk_buff *skb)
670 bluecard_info_t *info;
671 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
674 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
678 info = (bluecard_info_t *)(hdev->driver_data);
680 switch (skb->pkt_type) {
681 case HCI_COMMAND_PKT:
684 case HCI_ACLDATA_PKT:
687 case HCI_SCODATA_PKT:
692 /* Prepend skb with frame type */
693 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
694 skb_queue_tail(&(info->txq), skb);
696 bluecard_write_wakeup(info);
702 static void bluecard_hci_destruct(struct hci_dev *hdev)
707 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
714 /* ======================== Card services HCI interaction ======================== */
717 static int bluecard_open(bluecard_info_t *info)
719 unsigned int iobase = info->link.io.BasePort1;
720 struct hci_dev *hdev;
723 spin_lock_init(&(info->lock));
725 init_timer(&(info->timer));
726 info->timer.function = &bluecard_activity_led_timeout;
727 info->timer.data = (u_long)info;
729 skb_queue_head_init(&(info->txq));
731 info->rx_state = RECV_WAIT_PACKET_TYPE;
735 /* Initialize HCI device */
736 hdev = hci_alloc_dev();
738 BT_ERR("Can't allocate HCI device");
744 hdev->type = HCI_PCCARD;
745 hdev->driver_data = info;
747 hdev->open = bluecard_hci_open;
748 hdev->close = bluecard_hci_close;
749 hdev->flush = bluecard_hci_flush;
750 hdev->send = bluecard_hci_send_frame;
751 hdev->destruct = bluecard_hci_destruct;
752 hdev->ioctl = bluecard_hci_ioctl;
754 hdev->owner = THIS_MODULE;
756 id = inb(iobase + 0x30);
758 if ((id & 0x0f) == 0x02)
759 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
762 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
765 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
768 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
769 outb(info->ctrl_reg, iobase + REG_CONTROL);
772 outb(0x80, iobase + 0x30);
778 outb(0x00, iobase + 0x30);
781 info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
782 outb(info->ctrl_reg, iobase + REG_CONTROL);
784 /* Enable interrupt */
785 outb(0xff, iobase + REG_INTERRUPT);
786 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
787 outb(info->ctrl_reg, iobase + REG_CONTROL);
789 if ((id & 0x0f) == 0x03) {
791 info->ctrl_reg |= REG_CONTROL_RTS;
792 outb(info->ctrl_reg, iobase + REG_CONTROL);
795 info->ctrl_reg |= 0x03;
796 outb(info->ctrl_reg, iobase + REG_CONTROL);
799 info->ctrl_reg &= ~REG_CONTROL_RTS;
800 outb(info->ctrl_reg, iobase + REG_CONTROL);
802 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
803 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
804 set_bit(XMIT_SENDING_READY, &(info->tx_state));
807 /* Start the RX buffers */
808 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
809 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
811 /* Signal that the hardware is ready */
812 set_bit(CARD_READY, &(info->hw_state));
815 skb_queue_purge(&(info->txq));
817 /* Control the point at which RTS is enabled */
818 outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
820 /* Timeout before it is safe to send the first HCI packet */
823 /* Register HCI device */
824 if (hci_register_dev(hdev) < 0) {
825 BT_ERR("Can't register HCI device");
835 static int bluecard_close(bluecard_info_t *info)
837 unsigned int iobase = info->link.io.BasePort1;
838 struct hci_dev *hdev = info->hdev;
843 bluecard_hci_close(hdev);
845 clear_bit(CARD_READY, &(info->hw_state));
848 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
849 outb(info->ctrl_reg, iobase + REG_CONTROL);
852 outb(0x80, iobase + 0x30);
854 if (hci_unregister_dev(hdev) < 0)
855 BT_ERR("Can't unregister HCI device %s", hdev->name);
862 static dev_link_t *bluecard_attach(void)
864 bluecard_info_t *info;
865 client_reg_t client_reg;
869 /* Create new info device */
870 info = kmalloc(sizeof(*info), GFP_KERNEL);
873 memset(info, 0, sizeof(*info));
878 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
879 link->io.NumPorts1 = 8;
880 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
881 link->irq.IRQInfo1 = IRQ_LEVEL_ID;
883 link->irq.Handler = bluecard_interrupt;
884 link->irq.Instance = info;
886 link->conf.Attributes = CONF_ENABLE_IRQ;
888 link->conf.IntType = INT_MEMORY_AND_IO;
890 /* Register with Card Services */
891 link->next = dev_list;
893 client_reg.dev_info = &dev_info;
894 client_reg.EventMask =
895 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
896 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
897 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
898 client_reg.event_handler = &bluecard_event;
899 client_reg.Version = 0x0210;
900 client_reg.event_callback_args.client_data = link;
902 ret = pcmcia_register_client(&link->handle, &client_reg);
903 if (ret != CS_SUCCESS) {
904 cs_error(link->handle, RegisterClient, ret);
905 bluecard_detach(link);
913 static void bluecard_detach(dev_link_t *link)
915 bluecard_info_t *info = link->priv;
919 /* Locate device structure */
920 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
927 if (link->state & DEV_CONFIG)
928 bluecard_release(link);
931 ret = pcmcia_deregister_client(link->handle);
932 if (ret != CS_SUCCESS)
933 cs_error(link->handle, DeregisterClient, ret);
936 /* Unlink device structure, free bits */
943 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
947 i = pcmcia_get_first_tuple(handle, tuple);
949 return CS_NO_MORE_ITEMS;
951 i = pcmcia_get_tuple_data(handle, tuple);
955 return pcmcia_parse_tuple(handle, tuple, parse);
958 static void bluecard_config(dev_link_t *link)
960 client_handle_t handle = link->handle;
961 bluecard_info_t *info = link->priv;
965 config_info_t config;
966 int i, n, last_ret, last_fn;
968 tuple.TupleData = (cisdata_t *)buf;
969 tuple.TupleOffset = 0;
970 tuple.TupleDataMax = 255;
971 tuple.Attributes = 0;
973 /* Get configuration register information */
974 tuple.DesiredTuple = CISTPL_CONFIG;
975 last_ret = first_tuple(handle, &tuple, &parse);
976 if (last_ret != CS_SUCCESS) {
977 last_fn = ParseTuple;
980 link->conf.ConfigBase = parse.config.base;
981 link->conf.Present = parse.config.rmask[0];
984 link->state |= DEV_CONFIG;
985 i = pcmcia_get_configuration_info(handle, &config);
986 link->conf.Vcc = config.Vcc;
988 link->conf.ConfigIndex = 0x20;
989 link->io.NumPorts1 = 64;
990 link->io.IOAddrLines = 6;
992 for (n = 0; n < 0x400; n += 0x40) {
993 link->io.BasePort1 = n ^ 0x300;
994 i = pcmcia_request_io(link->handle, &link->io);
999 if (i != CS_SUCCESS) {
1000 cs_error(link->handle, RequestIO, i);
1004 i = pcmcia_request_irq(link->handle, &link->irq);
1005 if (i != CS_SUCCESS) {
1006 cs_error(link->handle, RequestIRQ, i);
1007 link->irq.AssignedIRQ = 0;
1010 i = pcmcia_request_configuration(link->handle, &link->conf);
1011 if (i != CS_SUCCESS) {
1012 cs_error(link->handle, RequestConfiguration, i);
1016 if (bluecard_open(info) != 0)
1019 strcpy(info->node.dev_name, info->hdev->name);
1020 link->dev = &info->node;
1021 link->state &= ~DEV_CONFIG_PENDING;
1026 cs_error(link->handle, last_fn, last_ret);
1029 bluecard_release(link);
1033 static void bluecard_release(dev_link_t *link)
1035 bluecard_info_t *info = link->priv;
1037 if (link->state & DEV_PRESENT)
1038 bluecard_close(info);
1040 del_timer(&(info->timer));
1044 pcmcia_release_configuration(link->handle);
1045 pcmcia_release_io(link->handle, &link->io);
1046 pcmcia_release_irq(link->handle, &link->irq);
1048 link->state &= ~DEV_CONFIG;
1052 static int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1054 dev_link_t *link = args->client_data;
1055 bluecard_info_t *info = link->priv;
1058 case CS_EVENT_CARD_REMOVAL:
1059 link->state &= ~DEV_PRESENT;
1060 if (link->state & DEV_CONFIG) {
1061 bluecard_close(info);
1062 bluecard_release(link);
1065 case CS_EVENT_CARD_INSERTION:
1066 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1067 bluecard_config(link);
1069 case CS_EVENT_PM_SUSPEND:
1070 link->state |= DEV_SUSPEND;
1071 /* Fall through... */
1072 case CS_EVENT_RESET_PHYSICAL:
1073 if (link->state & DEV_CONFIG)
1074 pcmcia_release_configuration(link->handle);
1076 case CS_EVENT_PM_RESUME:
1077 link->state &= ~DEV_SUSPEND;
1078 /* Fall through... */
1079 case CS_EVENT_CARD_RESET:
1081 pcmcia_request_configuration(link->handle, &link->conf);
1088 static struct pcmcia_driver bluecard_driver = {
1089 .owner = THIS_MODULE,
1091 .name = "bluecard_cs",
1093 .attach = bluecard_attach,
1094 .detach = bluecard_detach,
1097 static int __init init_bluecard_cs(void)
1099 return pcmcia_register_driver(&bluecard_driver);
1103 static void __exit exit_bluecard_cs(void)
1105 pcmcia_unregister_driver(&bluecard_driver);
1106 BUG_ON(dev_list != NULL);
1109 module_init(init_bluecard_cs);
1110 module_exit(exit_bluecard_cs);