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("BlueZ 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(u_long arg);
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_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_ACTIVITY_LED, &(info->hw_state))) {
192 /* Enable activity LED */
193 outb(0x10 | 0x40, iobase + 0x30);
195 /* Stop the LED after HZ/4 */
196 mod_timer(&(info->timer), jiffies + HZ / 4);
198 /* Enable power LED */
199 outb(0x08 | 0x20, iobase + 0x30);
201 /* Stop the LED after HZ/2 */
202 mod_timer(&(info->timer), jiffies + HZ / 2);
208 /* ======================== Interrupt handling ======================== */
211 static int bluecard_write(unsigned int iobase, unsigned int offset, __u8 *buf, int len)
215 actual = (len > 15) ? 15 : len;
217 outb_p(actual, iobase + offset);
219 for (i = 0; i < actual; i++)
220 outb_p(buf[i], iobase + offset + i + 1);
226 static void bluecard_write_wakeup(bluecard_info_t *info)
229 printk(KERN_WARNING "bluecard_cs: Call of write_wakeup for unknown device.\n");
233 if (!test_bit(XMIT_SENDING_READY, &(info->tx_state)))
236 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
237 set_bit(XMIT_WAKEUP, &(info->tx_state));
242 register unsigned int iobase = info->link.io.BasePort1;
243 register unsigned int offset;
244 register unsigned char command;
245 register unsigned long ready_bit;
246 register struct sk_buff *skb;
249 clear_bit(XMIT_WAKEUP, &(info->tx_state));
251 if (!(info->link.state & DEV_PRESENT))
254 if (test_bit(XMIT_BUFFER_NUMBER, &(info->tx_state))) {
255 if (!test_bit(XMIT_BUF_TWO_READY, &(info->tx_state)))
258 command = REG_COMMAND_TX_BUF_TWO;
259 ready_bit = XMIT_BUF_TWO_READY;
261 if (!test_bit(XMIT_BUF_ONE_READY, &(info->tx_state)))
264 command = REG_COMMAND_TX_BUF_ONE;
265 ready_bit = XMIT_BUF_ONE_READY;
268 if (!(skb = skb_dequeue(&(info->txq))))
271 if (skb->pkt_type & 0x80) {
273 info->ctrl_reg |= REG_CONTROL_RTS;
274 outb(info->ctrl_reg, iobase + REG_CONTROL);
278 bluecard_enable_activity_led(info);
281 len = bluecard_write(iobase, offset, skb->data, skb->len);
283 /* Tell the FPGA to send the data */
284 outb_p(command, iobase + REG_COMMAND);
286 /* Mark the buffer as dirty */
287 clear_bit(ready_bit, &(info->tx_state));
289 if (skb->pkt_type & 0x80) {
291 wait_queue_head_t wait;
292 unsigned char baud_reg;
294 switch (skb->pkt_type) {
295 case PKT_BAUD_RATE_460800:
296 baud_reg = REG_CONTROL_BAUD_RATE_460800;
298 case PKT_BAUD_RATE_230400:
299 baud_reg = REG_CONTROL_BAUD_RATE_230400;
301 case PKT_BAUD_RATE_115200:
302 baud_reg = REG_CONTROL_BAUD_RATE_115200;
304 case PKT_BAUD_RATE_57600:
305 /* Fall through... */
307 baud_reg = REG_CONTROL_BAUD_RATE_57600;
311 /* Wait until the command reaches the baseband */
312 init_waitqueue_head(&wait);
313 interruptible_sleep_on_timeout(&wait, HZ / 10);
315 /* Set baud on baseband */
316 info->ctrl_reg &= ~0x03;
317 info->ctrl_reg |= baud_reg;
318 outb(info->ctrl_reg, iobase + REG_CONTROL);
321 info->ctrl_reg &= ~REG_CONTROL_RTS;
322 outb(info->ctrl_reg, iobase + REG_CONTROL);
324 /* Wait before the next HCI packet can be send */
325 interruptible_sleep_on_timeout(&wait, HZ);
329 if (len == skb->len) {
333 skb_queue_head(&(info->txq), skb);
336 info->hdev.stat.byte_tx += len;
339 change_bit(XMIT_BUFFER_NUMBER, &(info->tx_state));
341 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
343 clear_bit(XMIT_SENDING, &(info->tx_state));
347 static int bluecard_read(unsigned int iobase, unsigned int offset, __u8 *buf, int size)
351 outb(REG_COMMAND_RX_WIN_ONE, iobase + REG_COMMAND);
353 len = inb(iobase + offset);
360 outb(REG_COMMAND_RX_WIN_TWO, iobase + REG_COMMAND);
364 buf[n] = inb(iobase + offset + i);
375 static void bluecard_receive(bluecard_info_t *info, unsigned int offset)
378 unsigned char buf[31];
382 printk(KERN_WARNING "bluecard_cs: Call of receive for unknown device.\n");
386 iobase = info->link.io.BasePort1;
388 if (test_bit(XMIT_SENDING_READY, &(info->tx_state)))
389 bluecard_enable_activity_led(info);
391 len = bluecard_read(iobase, offset, buf, sizeof(buf));
393 for (i = 0; i < len; i++) {
395 /* Allocate packet */
396 if (info->rx_skb == NULL) {
397 info->rx_state = RECV_WAIT_PACKET_TYPE;
399 if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
400 printk(KERN_WARNING "bluecard_cs: Can't allocate mem for new packet.\n");
405 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
407 info->rx_skb->dev = (void *)&(info->hdev);
408 info->rx_skb->pkt_type = buf[i];
410 switch (info->rx_skb->pkt_type) {
414 if (offset != 0x00) {
415 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
416 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
417 set_bit(XMIT_SENDING_READY, &(info->tx_state));
418 bluecard_write_wakeup(info);
421 kfree_skb(info->rx_skb);
426 info->rx_state = RECV_WAIT_EVENT_HEADER;
427 info->rx_count = HCI_EVENT_HDR_SIZE;
430 case HCI_ACLDATA_PKT:
431 info->rx_state = RECV_WAIT_ACL_HEADER;
432 info->rx_count = HCI_ACL_HDR_SIZE;
435 case HCI_SCODATA_PKT:
436 info->rx_state = RECV_WAIT_SCO_HEADER;
437 info->rx_count = HCI_SCO_HDR_SIZE;
442 printk(KERN_WARNING "bluecard_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
443 info->hdev.stat.err_rx++;
445 kfree_skb(info->rx_skb);
453 *skb_put(info->rx_skb, 1) = buf[i];
456 if (info->rx_count == 0) {
463 switch (info->rx_state) {
465 case RECV_WAIT_EVENT_HEADER:
466 eh = (hci_event_hdr *)(info->rx_skb->data);
467 info->rx_state = RECV_WAIT_DATA;
468 info->rx_count = eh->plen;
471 case RECV_WAIT_ACL_HEADER:
472 ah = (hci_acl_hdr *)(info->rx_skb->data);
473 dlen = __le16_to_cpu(ah->dlen);
474 info->rx_state = RECV_WAIT_DATA;
475 info->rx_count = dlen;
478 case RECV_WAIT_SCO_HEADER:
479 sh = (hci_sco_hdr *)(info->rx_skb->data);
480 info->rx_state = RECV_WAIT_DATA;
481 info->rx_count = sh->dlen;
485 hci_recv_frame(info->rx_skb);
498 info->hdev.stat.byte_rx += len;
502 void bluecard_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
504 bluecard_info_t *info = dev_inst;
509 printk(KERN_WARNING "bluecard_cs: Call of irq %d for unknown device.\n", irq);
513 if (!test_bit(CARD_READY, &(info->hw_state)))
516 iobase = info->link.io.BasePort1;
518 spin_lock(&(info->lock));
520 /* Disable interrupt */
521 info->ctrl_reg &= ~REG_CONTROL_INTERRUPT;
522 outb(info->ctrl_reg, iobase + REG_CONTROL);
524 reg = inb(iobase + REG_INTERRUPT);
526 if ((reg != 0x00) && (reg != 0xff)) {
529 bluecard_receive(info, 0x00);
530 outb(0x04, iobase + REG_INTERRUPT);
531 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
535 bluecard_receive(info, 0x10);
536 outb(0x08, iobase + REG_INTERRUPT);
537 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
541 set_bit(XMIT_BUF_ONE_READY, &(info->tx_state));
542 outb(0x01, iobase + REG_INTERRUPT);
543 bluecard_write_wakeup(info);
547 set_bit(XMIT_BUF_TWO_READY, &(info->tx_state));
548 outb(0x02, iobase + REG_INTERRUPT);
549 bluecard_write_wakeup(info);
554 /* Enable interrupt */
555 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
556 outb(info->ctrl_reg, iobase + REG_CONTROL);
558 spin_unlock(&(info->lock));
563 /* ======================== Device specific HCI commands ======================== */
566 static int bluecard_hci_set_baud_rate(struct hci_dev *hdev, int baud)
568 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
571 /* Ericsson baud rate command */
572 unsigned char cmd[] = { HCI_COMMAND_PKT, 0x09, 0xfc, 0x01, 0x03 };
574 if (!(skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
575 printk(KERN_WARNING "bluecard_cs: Can't allocate mem for new packet.\n");
582 skb->pkt_type = PKT_BAUD_RATE_460800;
586 skb->pkt_type = PKT_BAUD_RATE_230400;
590 skb->pkt_type = PKT_BAUD_RATE_115200;
593 /* Fall through... */
596 skb->pkt_type = PKT_BAUD_RATE_57600;
600 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
602 skb_queue_tail(&(info->txq), skb);
604 bluecard_write_wakeup(info);
611 /* ======================== HCI interface ======================== */
614 static int bluecard_hci_flush(struct hci_dev *hdev)
616 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
619 skb_queue_purge(&(info->txq));
625 static int bluecard_hci_open(struct hci_dev *hdev)
627 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
628 unsigned int iobase = info->link.io.BasePort1;
630 bluecard_hci_set_baud_rate(hdev, DEFAULT_BAUD_RATE);
632 if (test_and_set_bit(HCI_RUNNING, &(hdev->flags)))
636 outb(0x08 | 0x20, iobase + 0x30);
642 static int bluecard_hci_close(struct hci_dev *hdev)
644 bluecard_info_t *info = (bluecard_info_t *)(hdev->driver_data);
645 unsigned int iobase = info->link.io.BasePort1;
647 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
650 bluecard_hci_flush(hdev);
653 outb(0x00, iobase + 0x30);
659 static int bluecard_hci_send_frame(struct sk_buff *skb)
661 bluecard_info_t *info;
662 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
665 printk(KERN_WARNING "bluecard_cs: Frame for unknown HCI device (hdev=NULL).");
669 info = (bluecard_info_t *)(hdev->driver_data);
671 switch (skb->pkt_type) {
672 case HCI_COMMAND_PKT:
675 case HCI_ACLDATA_PKT:
678 case HCI_SCODATA_PKT:
683 /* Prepend skb with frame type */
684 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
685 skb_queue_tail(&(info->txq), skb);
687 bluecard_write_wakeup(info);
693 static void bluecard_hci_destruct(struct hci_dev *hdev)
698 static int bluecard_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
705 /* ======================== Card services HCI interaction ======================== */
708 int bluecard_open(bluecard_info_t *info)
710 unsigned int iobase = info->link.io.BasePort1;
711 struct hci_dev *hdev;
714 spin_lock_init(&(info->lock));
716 init_timer(&(info->timer));
717 info->timer.function = &bluecard_activity_led_timeout;
718 info->timer.data = (u_long)info;
720 skb_queue_head_init(&(info->txq));
722 info->rx_state = RECV_WAIT_PACKET_TYPE;
726 id = inb(iobase + 0x30);
728 if ((id & 0x0f) == 0x02)
729 set_bit(CARD_HAS_PCCARD_ID, &(info->hw_state));
732 set_bit(CARD_HAS_POWER_LED, &(info->hw_state));
735 set_bit(CARD_HAS_ACTIVITY_LED, &(info->hw_state));
738 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
739 outb(info->ctrl_reg, iobase + REG_CONTROL);
742 outb(0x80, iobase + 0x30);
745 set_current_state(TASK_INTERRUPTIBLE);
746 schedule_timeout(HZ / 100);
749 outb(0x00, iobase + 0x30);
752 info->ctrl_reg = REG_CONTROL_BT_ON | REG_CONTROL_BT_RES_PU;
753 outb(info->ctrl_reg, iobase + REG_CONTROL);
755 /* Enable interrupt */
756 outb(0xff, iobase + REG_INTERRUPT);
757 info->ctrl_reg |= REG_CONTROL_INTERRUPT;
758 outb(info->ctrl_reg, iobase + REG_CONTROL);
760 /* Start the RX buffers */
761 outb(REG_COMMAND_RX_BUF_ONE, iobase + REG_COMMAND);
762 outb(REG_COMMAND_RX_BUF_TWO, iobase + REG_COMMAND);
764 /* Signal that the hardware is ready */
765 set_bit(CARD_READY, &(info->hw_state));
768 skb_queue_purge(&(info->txq));
770 /* Control the point at which RTS is enabled */
771 outb((0x0f << RTS_LEVEL_SHIFT_BITS) | 1, iobase + REG_RX_CONTROL);
773 /* Timeout before it is safe to send the first HCI packet */
774 set_current_state(TASK_INTERRUPTIBLE);
775 schedule_timeout((HZ * 5) / 4); // or set it to 3/2
778 /* Initialize and register HCI device */
780 hdev = &(info->hdev);
782 hdev->type = HCI_PCCARD;
783 hdev->driver_data = info;
785 hdev->open = bluecard_hci_open;
786 hdev->close = bluecard_hci_close;
787 hdev->flush = bluecard_hci_flush;
788 hdev->send = bluecard_hci_send_frame;
789 hdev->destruct = bluecard_hci_destruct;
790 hdev->ioctl = bluecard_hci_ioctl;
792 if (hci_register_dev(hdev) < 0) {
793 printk(KERN_WARNING "bluecard_cs: Can't register HCI device %s.\n", hdev->name);
801 int bluecard_close(bluecard_info_t *info)
803 unsigned int iobase = info->link.io.BasePort1;
804 struct hci_dev *hdev = &(info->hdev);
806 if (info->link.state & DEV_CONFIG_PENDING)
809 bluecard_hci_close(hdev);
811 clear_bit(CARD_READY, &(info->hw_state));
814 info->ctrl_reg = REG_CONTROL_BT_RESET | REG_CONTROL_CARD_RESET;
815 outb(info->ctrl_reg, iobase + REG_CONTROL);
818 outb(0x80, iobase + 0x30);
820 if (hci_unregister_dev(hdev) < 0)
821 printk(KERN_WARNING "bluecard_cs: Can't unregister HCI device %s.\n", hdev->name);
828 /* ======================== Card services ======================== */
831 static void cs_error(client_handle_t handle, int func, int ret)
833 error_info_t err = { func, ret };
835 CardServices(ReportError, handle, &err);
839 dev_link_t *bluecard_attach(void)
841 bluecard_info_t *info;
842 client_reg_t client_reg;
846 /* Create new info device */
847 info = kmalloc(sizeof(*info), GFP_KERNEL);
850 memset(info, 0, sizeof(*info));
855 link->release.function = &bluecard_release;
856 link->release.data = (u_long)link;
857 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
858 link->io.NumPorts1 = 8;
859 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
860 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
862 if (irq_list[0] == -1)
863 link->irq.IRQInfo2 = irq_mask;
865 for (i = 0; i < 4; i++)
866 link->irq.IRQInfo2 |= 1 << irq_list[i];
868 link->irq.Handler = bluecard_interrupt;
869 link->irq.Instance = info;
871 link->conf.Attributes = CONF_ENABLE_IRQ;
873 link->conf.IntType = INT_MEMORY_AND_IO;
875 /* Register with Card Services */
876 link->next = dev_list;
878 client_reg.dev_info = &dev_info;
879 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
880 client_reg.EventMask =
881 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
882 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
883 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
884 client_reg.event_handler = &bluecard_event;
885 client_reg.Version = 0x0210;
886 client_reg.event_callback_args.client_data = link;
888 ret = CardServices(RegisterClient, &link->handle, &client_reg);
889 if (ret != CS_SUCCESS) {
890 cs_error(link->handle, RegisterClient, ret);
891 bluecard_detach(link);
899 void bluecard_detach(dev_link_t *link)
901 bluecard_info_t *info = link->priv;
905 /* Locate device structure */
906 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
913 del_timer(&link->release);
914 if (link->state & DEV_CONFIG)
915 bluecard_release((u_long)link);
918 ret = CardServices(DeregisterClient, link->handle);
919 if (ret != CS_SUCCESS)
920 cs_error(link->handle, DeregisterClient, ret);
923 /* Unlink device structure, free bits */
930 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
934 i = CardServices(fn, handle, tuple);
936 return CS_NO_MORE_ITEMS;
938 i = CardServices(GetTupleData, handle, tuple);
942 return CardServices(ParseTuple, handle, tuple, parse);
946 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
947 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
949 void bluecard_config(dev_link_t *link)
951 client_handle_t handle = link->handle;
952 bluecard_info_t *info = link->priv;
956 config_info_t config;
957 int i, n, last_ret, last_fn;
959 tuple.TupleData = (cisdata_t *)buf;
960 tuple.TupleOffset = 0;
961 tuple.TupleDataMax = 255;
962 tuple.Attributes = 0;
964 /* Get configuration register information */
965 tuple.DesiredTuple = CISTPL_CONFIG;
966 last_ret = first_tuple(handle, &tuple, &parse);
967 if (last_ret != CS_SUCCESS) {
968 last_fn = ParseTuple;
971 link->conf.ConfigBase = parse.config.base;
972 link->conf.Present = parse.config.rmask[0];
975 link->state |= DEV_CONFIG;
976 i = CardServices(GetConfigurationInfo, handle, &config);
977 link->conf.Vcc = config.Vcc;
979 link->conf.ConfigIndex = 0x20;
980 link->io.NumPorts1 = 64;
981 link->io.IOAddrLines = 6;
983 for (n = 0; n < 0x400; n += 0x40) {
984 link->io.BasePort1 = n ^ 0x300;
985 i = CardServices(RequestIO, link->handle, &link->io);
990 if (i != CS_SUCCESS) {
991 cs_error(link->handle, RequestIO, i);
995 i = CardServices(RequestIRQ, link->handle, &link->irq);
996 if (i != CS_SUCCESS) {
997 cs_error(link->handle, RequestIRQ, i);
998 link->irq.AssignedIRQ = 0;
1001 i = CardServices(RequestConfiguration, link->handle, &link->conf);
1002 if (i != CS_SUCCESS) {
1003 cs_error(link->handle, RequestConfiguration, i);
1009 if (bluecard_open(info) != 0)
1012 strcpy(info->node.dev_name, info->hdev.name);
1013 link->dev = &info->node;
1014 link->state &= ~DEV_CONFIG_PENDING;
1019 cs_error(link->handle, last_fn, last_ret);
1022 bluecard_release((u_long)link);
1026 void bluecard_release(u_long arg)
1028 dev_link_t *link = (dev_link_t *)arg;
1029 bluecard_info_t *info = link->priv;
1031 if (link->state & DEV_PRESENT)
1032 bluecard_close(info);
1038 CardServices(ReleaseConfiguration, link->handle);
1039 CardServices(ReleaseIO, link->handle, &link->io);
1040 CardServices(ReleaseIRQ, link->handle, &link->irq);
1042 link->state &= ~DEV_CONFIG;
1046 int bluecard_event(event_t event, int priority, event_callback_args_t *args)
1048 dev_link_t *link = args->client_data;
1049 bluecard_info_t *info = link->priv;
1052 case CS_EVENT_CARD_REMOVAL:
1053 link->state &= ~DEV_PRESENT;
1054 if (link->state & DEV_CONFIG) {
1055 bluecard_close(info);
1056 mod_timer(&link->release, jiffies + HZ / 20);
1059 case CS_EVENT_CARD_INSERTION:
1060 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
1061 bluecard_config(link);
1063 case CS_EVENT_PM_SUSPEND:
1064 link->state |= DEV_SUSPEND;
1065 /* Fall through... */
1066 case CS_EVENT_RESET_PHYSICAL:
1067 if (link->state & DEV_CONFIG)
1068 CardServices(ReleaseConfiguration, link->handle);
1070 case CS_EVENT_PM_RESUME:
1071 link->state &= ~DEV_SUSPEND;
1072 /* Fall through... */
1073 case CS_EVENT_CARD_RESET:
1075 CardServices(RequestConfiguration, link->handle, &link->conf);
1084 /* ======================== Module initialization ======================== */
1087 int __init init_bluecard_cs(void)
1092 CardServices(GetCardServicesInfo, &serv);
1093 if (serv.Revision != CS_RELEASE_CODE) {
1094 printk(KERN_NOTICE "bluecard_cs: Card Services release does not match!\n");
1098 err = register_pccard_driver(&dev_info, &bluecard_attach, &bluecard_detach);
1104 void __exit exit_bluecard_cs(void)
1106 unregister_pccard_driver(&dev_info);
1108 while (dev_list != NULL)
1109 bluecard_detach(dev_list);
1113 module_init(init_bluecard_cs);
1114 module_exit(exit_bluecard_cs);