1 /*
2 *
3 * Driver for the 3Com Bluetooth PCMCIA card
4 *
5 * Copyright (C) 2001-2002 Marcel Holtmann <marcel@holtmann.org>
6 * Jose Orlando Pereira <jop@di.uminho.pt>
7 *
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation;
12 *
13 * Software distributed under the License is distributed on an "AS
14 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
15 * implied. See the License for the specific language governing
16 * rights and limitations under the License.
17 *
18 * The initial developer of the original code is David A. Hinds
19 * <dahinds@users.sourceforge.net>. Portions created by David A. Hinds
20 * are Copyright (C) 1999 David A. Hinds. All Rights Reserved.
21 *
22 */
23
24 #include <linux/config.h>
25 #include <linux/module.h>
26
27 #include <linux/kernel.h>
28 #include <linux/kmod.h>
29 #include <linux/init.h>
30 #include <linux/slab.h>
31 #include <linux/types.h>
32 #include <linux/sched.h>
33 #include <linux/delay.h>
34 #include <linux/timer.h>
35 #include <linux/errno.h>
36 #include <linux/unistd.h>
37 #include <linux/ptrace.h>
38 #include <linux/ioport.h>
39 #include <linux/spinlock.h>
40
41 #include <linux/skbuff.h>
42 #include <linux/string.h>
43 #include <linux/serial.h>
44 #include <linux/serial_reg.h>
45 #include <asm/system.h>
46 #include <asm/bitops.h>
47 #include <asm/io.h>
48
49 #include <linux/firmware.h>
50
51 #include <pcmcia/version.h>
52 #include <pcmcia/cs_types.h>
53 #include <pcmcia/cs.h>
54 #include <pcmcia/cistpl.h>
55 #include <pcmcia/ciscode.h>
56 #include <pcmcia/ds.h>
57 #include <pcmcia/cisreg.h>
58
59 #include <net/bluetooth/bluetooth.h>
60 #include <net/bluetooth/hci_core.h>
61
62
63
64 /* ======================== Module parameters ======================== */
65
66
67 /* Bit map of interrupts to choose from */
68 static u_int irq_mask = 0xffff;
69 static int irq_list[4] = { -1 };
70
71 MODULE_PARM(irq_mask, "i");
72 MODULE_PARM(irq_list, "1-4i");
73
74 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>, Jose Orlando Pereira <jop@di.uminho.pt>");
75 MODULE_DESCRIPTION("BlueZ driver for the 3Com Bluetooth PCMCIA card");
76 MODULE_LICENSE("GPL");
77
78
79
80 /* ======================== Local structures ======================== */
81
82
83 typedef struct bt3c_info_t {
84 dev_link_t link;
85 dev_node_t node;
86
87 struct hci_dev hdev;
88
89 spinlock_t lock; /* For serializing operations */
90
91 struct sk_buff_head txq;
92 unsigned long tx_state;
93
94 unsigned long rx_state;
95 unsigned long rx_count;
96 struct sk_buff *rx_skb;
97 } bt3c_info_t;
98
99
100 void bt3c_config(dev_link_t *link);
101 void bt3c_release(u_long arg);
102 int bt3c_event(event_t event, int priority, event_callback_args_t *args);
103
104 static dev_info_t dev_info = "bt3c_cs";
105
106 dev_link_t *bt3c_attach(void);
107 void bt3c_detach(dev_link_t *);
108
109 static dev_link_t *dev_list = NULL;
110
111
112 /* Transmit states */
113 #define XMIT_SENDING 1
114 #define XMIT_WAKEUP 2
115 #define XMIT_WAITING 8
116
117 /* Receiver states */
118 #define RECV_WAIT_PACKET_TYPE 0
119 #define RECV_WAIT_EVENT_HEADER 1
120 #define RECV_WAIT_ACL_HEADER 2
121 #define RECV_WAIT_SCO_HEADER 3
122 #define RECV_WAIT_DATA 4
123
124
125
126 /* ======================== Special I/O functions ======================== */
127
128
129 #define DATA_L 0
130 #define DATA_H 1
131 #define ADDR_L 2
132 #define ADDR_H 3
133 #define CONTROL 4
134
135
bt3c_address(unsigned int iobase,unsigned short addr)136 inline void bt3c_address(unsigned int iobase, unsigned short addr)
137 {
138 outb(addr & 0xff, iobase + ADDR_L);
139 outb((addr >> 8) & 0xff, iobase + ADDR_H);
140 }
141
142
bt3c_put(unsigned int iobase,unsigned short value)143 inline void bt3c_put(unsigned int iobase, unsigned short value)
144 {
145 outb(value & 0xff, iobase + DATA_L);
146 outb((value >> 8) & 0xff, iobase + DATA_H);
147 }
148
149
bt3c_io_write(unsigned int iobase,unsigned short addr,unsigned short value)150 inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
151 {
152 bt3c_address(iobase, addr);
153 bt3c_put(iobase, value);
154 }
155
156
bt3c_get(unsigned int iobase)157 inline unsigned short bt3c_get(unsigned int iobase)
158 {
159 unsigned short value = inb(iobase + DATA_L);
160
161 value |= inb(iobase + DATA_H) << 8;
162
163 return value;
164 }
165
166
bt3c_read(unsigned int iobase,unsigned short addr)167 inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
168 {
169 bt3c_address(iobase, addr);
170
171 return bt3c_get(iobase);
172 }
173
174
175
176 /* ======================== Interrupt handling ======================== */
177
178
bt3c_write(unsigned int iobase,int fifo_size,__u8 * buf,int len)179 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
180 {
181 int actual = 0;
182
183 bt3c_address(iobase, 0x7080);
184
185 /* Fill FIFO with current frame */
186 while (actual < len) {
187 /* Transmit next byte */
188 bt3c_put(iobase, buf[actual]);
189 actual++;
190 }
191
192 bt3c_io_write(iobase, 0x7005, actual);
193
194 return actual;
195 }
196
197
bt3c_write_wakeup(bt3c_info_t * info,int from)198 static void bt3c_write_wakeup(bt3c_info_t *info, int from)
199 {
200 unsigned long flags;
201
202 if (!info) {
203 printk(KERN_WARNING "bt3c_cs: Call of write_wakeup for unknown device.\n");
204 return;
205 }
206
207 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
208 return;
209
210 spin_lock_irqsave(&(info->lock), flags);
211
212 do {
213 register unsigned int iobase = info->link.io.BasePort1;
214 register struct sk_buff *skb;
215 register int len;
216
217 if (!(info->link.state & DEV_PRESENT))
218 break;
219
220
221 if (!(skb = skb_dequeue(&(info->txq)))) {
222 clear_bit(XMIT_SENDING, &(info->tx_state));
223 break;
224 }
225
226 /* Send frame */
227 len = bt3c_write(iobase, 256, skb->data, skb->len);
228
229 if (len != skb->len) {
230 printk(KERN_WARNING "bt3c_cs: very strange\n");
231 }
232
233 kfree_skb(skb);
234
235 info->hdev.stat.byte_tx += len;
236
237 } while (0);
238
239 spin_unlock_irqrestore(&(info->lock), flags);
240 }
241
242
bt3c_receive(bt3c_info_t * info)243 static void bt3c_receive(bt3c_info_t *info)
244 {
245 unsigned int iobase;
246 int size = 0, avail;
247
248 if (!info) {
249 printk(KERN_WARNING "bt3c_cs: Call of receive for unknown device.\n");
250 return;
251 }
252
253 iobase = info->link.io.BasePort1;
254
255 avail = bt3c_read(iobase, 0x7006);
256 //printk("bt3c_cs: receiving %d bytes\n", avail);
257
258 bt3c_address(iobase, 0x7480);
259 while (size < avail) {
260 size++;
261 info->hdev.stat.byte_rx++;
262
263 /* Allocate packet */
264 if (info->rx_skb == NULL) {
265 info->rx_state = RECV_WAIT_PACKET_TYPE;
266 info->rx_count = 0;
267 if (!(info->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
268 printk(KERN_WARNING "bt3c_cs: Can't allocate mem for new packet.\n");
269 return;
270 }
271 }
272
273
274 if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
275
276 info->rx_skb->dev = (void *)&(info->hdev);
277 info->rx_skb->pkt_type = inb(iobase + DATA_L);
278 inb(iobase + DATA_H);
279 //printk("bt3c: PACKET_TYPE=%02x\n", info->rx_skb->pkt_type);
280
281 switch (info->rx_skb->pkt_type) {
282
283 case HCI_EVENT_PKT:
284 info->rx_state = RECV_WAIT_EVENT_HEADER;
285 info->rx_count = HCI_EVENT_HDR_SIZE;
286 break;
287
288 case HCI_ACLDATA_PKT:
289 info->rx_state = RECV_WAIT_ACL_HEADER;
290 info->rx_count = HCI_ACL_HDR_SIZE;
291 break;
292
293 case HCI_SCODATA_PKT:
294 info->rx_state = RECV_WAIT_SCO_HEADER;
295 info->rx_count = HCI_SCO_HDR_SIZE;
296 break;
297
298 default:
299 /* Unknown packet */
300 printk(KERN_WARNING "bt3c_cs: Unknown HCI packet with type 0x%02x received.\n", info->rx_skb->pkt_type);
301 info->hdev.stat.err_rx++;
302 clear_bit(HCI_RUNNING, &(info->hdev.flags));
303
304 kfree_skb(info->rx_skb);
305 info->rx_skb = NULL;
306 break;
307
308 }
309
310 } else {
311
312 __u8 x = inb(iobase + DATA_L);
313
314 *skb_put(info->rx_skb, 1) = x;
315 inb(iobase + DATA_H);
316 info->rx_count--;
317
318 if (info->rx_count == 0) {
319
320 int dlen;
321 hci_event_hdr *eh;
322 hci_acl_hdr *ah;
323 hci_sco_hdr *sh;
324
325 switch (info->rx_state) {
326
327 case RECV_WAIT_EVENT_HEADER:
328 eh = (hci_event_hdr *)(info->rx_skb->data);
329 info->rx_state = RECV_WAIT_DATA;
330 info->rx_count = eh->plen;
331 break;
332
333 case RECV_WAIT_ACL_HEADER:
334 ah = (hci_acl_hdr *)(info->rx_skb->data);
335 dlen = __le16_to_cpu(ah->dlen);
336 info->rx_state = RECV_WAIT_DATA;
337 info->rx_count = dlen;
338 break;
339
340 case RECV_WAIT_SCO_HEADER:
341 sh = (hci_sco_hdr *)(info->rx_skb->data);
342 info->rx_state = RECV_WAIT_DATA;
343 info->rx_count = sh->dlen;
344 break;
345
346 case RECV_WAIT_DATA:
347 hci_recv_frame(info->rx_skb);
348 info->rx_skb = NULL;
349 break;
350
351 }
352
353 }
354
355 }
356
357 }
358
359 bt3c_io_write(iobase, 0x7006, 0x0000);
360 }
361
362
bt3c_interrupt(int irq,void * dev_inst,struct pt_regs * regs)363 void bt3c_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
364 {
365 bt3c_info_t *info = dev_inst;
366 unsigned int iobase;
367 int iir;
368
369 if (!info) {
370 printk(KERN_WARNING "bt3c_cs: Call of irq %d for unknown device.\n", irq);
371 return;
372 }
373
374 iobase = info->link.io.BasePort1;
375
376 spin_lock(&(info->lock));
377
378 iir = inb(iobase + CONTROL);
379 if (iir & 0x80) {
380 int stat = bt3c_read(iobase, 0x7001);
381
382 if ((stat & 0xff) == 0x7f) {
383 printk(KERN_WARNING "bt3c_cs: STRANGE stat=%04x\n", stat);
384 } else if ((stat & 0xff) != 0xff) {
385 if (stat & 0x0020) {
386 int stat = bt3c_read(iobase, 0x7002) & 0x10;
387 printk(KERN_WARNING "bt3c_cs: antena %s\n", stat ? "OUT" : "IN");
388 }
389 if (stat & 0x0001)
390 bt3c_receive(info);
391 if (stat & 0x0002) {
392 //printk("bt3c_cs: ACK %04x\n", stat);
393 clear_bit(XMIT_SENDING, &(info->tx_state));
394 bt3c_write_wakeup(info, 1);
395 }
396
397 bt3c_io_write(iobase, 0x7001, 0x0000);
398
399 outb(iir, iobase + CONTROL);
400 }
401 }
402
403 spin_unlock(&(info->lock));
404 }
405
406
407
408
409 /* ======================== HCI interface ======================== */
410
411
bt3c_hci_flush(struct hci_dev * hdev)412 static int bt3c_hci_flush(struct hci_dev *hdev)
413 {
414 bt3c_info_t *info = (bt3c_info_t *)(hdev->driver_data);
415
416 /* Drop TX queue */
417 skb_queue_purge(&(info->txq));
418
419 return 0;
420 }
421
422
bt3c_hci_open(struct hci_dev * hdev)423 static int bt3c_hci_open(struct hci_dev *hdev)
424 {
425 set_bit(HCI_RUNNING, &(hdev->flags));
426
427 return 0;
428 }
429
430
bt3c_hci_close(struct hci_dev * hdev)431 static int bt3c_hci_close(struct hci_dev *hdev)
432 {
433 if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
434 return 0;
435
436 bt3c_hci_flush(hdev);
437
438 return 0;
439 }
440
441
bt3c_hci_send_frame(struct sk_buff * skb)442 static int bt3c_hci_send_frame(struct sk_buff *skb)
443 {
444 bt3c_info_t *info;
445 struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
446
447 if (!hdev) {
448 printk(KERN_WARNING "bt3c_cs: Frame for unknown HCI device (hdev=NULL).");
449 return -ENODEV;
450 }
451
452 info = (bt3c_info_t *) (hdev->driver_data);
453
454 switch (skb->pkt_type) {
455 case HCI_COMMAND_PKT:
456 hdev->stat.cmd_tx++;
457 break;
458 case HCI_ACLDATA_PKT:
459 hdev->stat.acl_tx++;
460 break;
461 case HCI_SCODATA_PKT:
462 hdev->stat.sco_tx++;
463 break;
464 };
465
466 /* Prepend skb with frame type */
467 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
468 skb_queue_tail(&(info->txq), skb);
469
470 bt3c_write_wakeup(info, 0);
471
472 return 0;
473 }
474
475
bt3c_hci_destruct(struct hci_dev * hdev)476 static void bt3c_hci_destruct(struct hci_dev *hdev)
477 {
478 }
479
480
bt3c_hci_ioctl(struct hci_dev * hdev,unsigned int cmd,unsigned long arg)481 static int bt3c_hci_ioctl(struct hci_dev *hdev, unsigned int cmd, unsigned long arg)
482 {
483 return -ENOIOCTLCMD;
484 }
485
486
487
488 /* ======================== Card services HCI interaction ======================== */
489
490
bt3c_load_firmware(bt3c_info_t * info,unsigned char * firmware,int count)491 static int bt3c_load_firmware(bt3c_info_t *info, unsigned char *firmware, int count)
492 {
493 char *ptr = (char *) firmware;
494 char b[9];
495 unsigned int iobase, size, addr, fcs, tmp;
496 int i, err = 0;
497
498 iobase = info->link.io.BasePort1;
499
500 /* Reset */
501
502 bt3c_io_write(iobase, 0x8040, 0x0404);
503 bt3c_io_write(iobase, 0x8040, 0x0400);
504
505 udelay(1);
506
507 bt3c_io_write(iobase, 0x8040, 0x0404);
508
509 udelay(17);
510
511 /* Load */
512
513 while (count) {
514 if (ptr[0] != 'S') {
515 printk(KERN_WARNING "bt3c_cs: Bad address in firmware.\n");
516 err = -EFAULT;
517 goto error;
518 }
519
520 memset(b, 0, sizeof(b));
521 memcpy(b, ptr + 2, 2);
522 size = simple_strtol(b, NULL, 16);
523
524 memset(b, 0, sizeof(b));
525 memcpy(b, ptr + 4, 8);
526 addr = simple_strtol(b, NULL, 16);
527
528 memset(b, 0, sizeof(b));
529 memcpy(b, ptr + (size * 2) + 2, 2);
530 fcs = simple_strtol(b, NULL, 16);
531
532 memset(b, 0, sizeof(b));
533 for (tmp = 0, i = 0; i < size; i++) {
534 memcpy(b, ptr + (i * 2) + 2, 2);
535 tmp += simple_strtol(b, NULL, 16);
536 }
537
538 if (((tmp + fcs) & 0xff) != 0xff) {
539 printk(KERN_WARNING "bt3c_cs: Checksum error in firmware.\n");
540 err = -EILSEQ;
541 goto error;
542 }
543
544 if (ptr[1] == '3') {
545 bt3c_address(iobase, addr);
546
547 memset(b, 0, sizeof(b));
548 for (i = 0; i < (size - 4) / 2; i++) {
549 memcpy(b, ptr + (i * 4) + 12, 4);
550 tmp = simple_strtol(b, NULL, 16);
551 bt3c_put(iobase, tmp);
552 }
553 }
554
555 ptr += (size * 2) + 6;
556 count -= (size * 2) + 6;
557 }
558
559 udelay(17);
560
561 /* Boot */
562
563 bt3c_address(iobase, 0x3000);
564 outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
565
566 error:
567 udelay(17);
568
569 /* Clear */
570
571 bt3c_io_write(iobase, 0x7006, 0x0000);
572 bt3c_io_write(iobase, 0x7005, 0x0000);
573 bt3c_io_write(iobase, 0x7001, 0x0000);
574
575 return err;
576 }
577
578
bt3c_open(bt3c_info_t * info)579 int bt3c_open(bt3c_info_t *info)
580 {
581 const struct firmware *firmware;
582 char device[16];
583 struct hci_dev *hdev;
584 int err;
585
586 spin_lock_init(&(info->lock));
587
588 skb_queue_head_init(&(info->txq));
589
590 info->rx_state = RECV_WAIT_PACKET_TYPE;
591 info->rx_count = 0;
592 info->rx_skb = NULL;
593
594 /* Load firmware */
595
596 snprintf(device, sizeof(device), "bt3c%4.4x", info->link.io.BasePort1);
597
598 err = request_firmware(&firmware, "BT3CPCC.bin", device);
599 if (err < 0) {
600 printk(KERN_WARNING "bt3c_cs: Firmware request failed.\n");
601 return err;
602 }
603
604 err = bt3c_load_firmware(info, firmware->data, firmware->size);
605
606 release_firmware(firmware);
607
608 if (err < 0) {
609 printk(KERN_WARNING "bt3c_cs: Firmware loading failed.\n");
610 return err;
611 }
612
613 /* Timeout before it is safe to send the first HCI packet */
614
615 set_current_state(TASK_INTERRUPTIBLE);
616 schedule_timeout(HZ);
617
618
619 /* Initialize and register HCI device */
620
621 hdev = &(info->hdev);
622
623 hdev->type = HCI_PCCARD;
624 hdev->driver_data = info;
625
626 hdev->open = bt3c_hci_open;
627 hdev->close = bt3c_hci_close;
628 hdev->flush = bt3c_hci_flush;
629 hdev->send = bt3c_hci_send_frame;
630 hdev->destruct = bt3c_hci_destruct;
631 hdev->ioctl = bt3c_hci_ioctl;
632
633 if (hci_register_dev(hdev) < 0) {
634 printk(KERN_WARNING "bt3c_cs: Can't register HCI device %s.\n", hdev->name);
635 return -ENODEV;
636 }
637
638 return 0;
639 }
640
641
bt3c_close(bt3c_info_t * info)642 int bt3c_close(bt3c_info_t *info)
643 {
644 struct hci_dev *hdev = &(info->hdev);
645
646 if (info->link.state & DEV_CONFIG_PENDING)
647 return -ENODEV;
648
649 bt3c_hci_close(hdev);
650
651 if (hci_unregister_dev(hdev) < 0)
652 printk(KERN_WARNING "bt3c_cs: Can't unregister HCI device %s.\n", hdev->name);
653
654 return 0;
655 }
656
657
658
659 /* ======================== Card services ======================== */
660
661
cs_error(client_handle_t handle,int func,int ret)662 static void cs_error(client_handle_t handle, int func, int ret)
663 {
664 error_info_t err = { func, ret };
665
666 CardServices(ReportError, handle, &err);
667 }
668
669
bt3c_attach(void)670 dev_link_t *bt3c_attach(void)
671 {
672 bt3c_info_t *info;
673 client_reg_t client_reg;
674 dev_link_t *link;
675 int i, ret;
676
677 /* Create new info device */
678 info = kmalloc(sizeof(*info), GFP_KERNEL);
679 if (!info)
680 return NULL;
681 memset(info, 0, sizeof(*info));
682
683 link = &info->link;
684 link->priv = info;
685
686 link->release.function = &bt3c_release;
687 link->release.data = (u_long)link;
688 link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
689 link->io.NumPorts1 = 8;
690 link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
691 link->irq.IRQInfo1 = IRQ_INFO2_VALID | IRQ_LEVEL_ID;
692
693 if (irq_list[0] == -1)
694 link->irq.IRQInfo2 = irq_mask;
695 else
696 for (i = 0; i < 4; i++)
697 link->irq.IRQInfo2 |= 1 << irq_list[i];
698
699 link->irq.Handler = bt3c_interrupt;
700 link->irq.Instance = info;
701
702 link->conf.Attributes = CONF_ENABLE_IRQ;
703 link->conf.Vcc = 50;
704 link->conf.IntType = INT_MEMORY_AND_IO;
705
706 /* Register with Card Services */
707 link->next = dev_list;
708 dev_list = link;
709 client_reg.dev_info = &dev_info;
710 client_reg.Attributes = INFO_IO_CLIENT | INFO_CARD_SHARE;
711 client_reg.EventMask =
712 CS_EVENT_CARD_INSERTION | CS_EVENT_CARD_REMOVAL |
713 CS_EVENT_RESET_PHYSICAL | CS_EVENT_CARD_RESET |
714 CS_EVENT_PM_SUSPEND | CS_EVENT_PM_RESUME;
715 client_reg.event_handler = &bt3c_event;
716 client_reg.Version = 0x0210;
717 client_reg.event_callback_args.client_data = link;
718
719 ret = CardServices(RegisterClient, &link->handle, &client_reg);
720 if (ret != CS_SUCCESS) {
721 cs_error(link->handle, RegisterClient, ret);
722 bt3c_detach(link);
723 return NULL;
724 }
725
726 return link;
727 }
728
729
bt3c_detach(dev_link_t * link)730 void bt3c_detach(dev_link_t *link)
731 {
732 bt3c_info_t *info = link->priv;
733 dev_link_t **linkp;
734 int ret;
735
736 /* Locate device structure */
737 for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
738 if (*linkp == link)
739 break;
740
741 if (*linkp == NULL)
742 return;
743
744 del_timer(&link->release);
745
746 if (link->state & DEV_CONFIG)
747 bt3c_release((u_long)link);
748
749 if (link->handle) {
750 ret = CardServices(DeregisterClient, link->handle);
751 if (ret != CS_SUCCESS)
752 cs_error(link->handle, DeregisterClient, ret);
753 }
754
755 /* Unlink device structure, free bits */
756 *linkp = link->next;
757
758 kfree(info);
759 }
760
761
get_tuple(int fn,client_handle_t handle,tuple_t * tuple,cisparse_t * parse)762 static int get_tuple(int fn, client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
763 {
764 int i;
765
766 i = CardServices(fn, handle, tuple);
767 if (i != CS_SUCCESS)
768 return CS_NO_MORE_ITEMS;
769
770 i = CardServices(GetTupleData, handle, tuple);
771 if (i != CS_SUCCESS)
772 return i;
773
774 return CardServices(ParseTuple, handle, tuple, parse);
775 }
776
777
778 #define first_tuple(a, b, c) get_tuple(GetFirstTuple, a, b, c)
779 #define next_tuple(a, b, c) get_tuple(GetNextTuple, a, b, c)
780
bt3c_config(dev_link_t * link)781 void bt3c_config(dev_link_t *link)
782 {
783 static ioaddr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
784 client_handle_t handle = link->handle;
785 bt3c_info_t *info = link->priv;
786 tuple_t tuple;
787 u_short buf[256];
788 cisparse_t parse;
789 cistpl_cftable_entry_t *cf = &parse.cftable_entry;
790 config_info_t config;
791 int i, j, try, last_ret, last_fn;
792
793 tuple.TupleData = (cisdata_t *)buf;
794 tuple.TupleOffset = 0;
795 tuple.TupleDataMax = 255;
796 tuple.Attributes = 0;
797
798 /* Get configuration register information */
799 tuple.DesiredTuple = CISTPL_CONFIG;
800 last_ret = first_tuple(handle, &tuple, &parse);
801 if (last_ret != CS_SUCCESS) {
802 last_fn = ParseTuple;
803 goto cs_failed;
804 }
805 link->conf.ConfigBase = parse.config.base;
806 link->conf.Present = parse.config.rmask[0];
807
808 /* Configure card */
809 link->state |= DEV_CONFIG;
810 i = CardServices(GetConfigurationInfo, handle, &config);
811 link->conf.Vcc = config.Vcc;
812
813 /* First pass: look for a config entry that looks normal. */
814 tuple.TupleData = (cisdata_t *)buf;
815 tuple.TupleOffset = 0;
816 tuple.TupleDataMax = 255;
817 tuple.Attributes = 0;
818 tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
819 /* Two tries: without IO aliases, then with aliases */
820 for (try = 0; try < 2; try++) {
821 i = first_tuple(handle, &tuple, &parse);
822 while (i != CS_NO_MORE_ITEMS) {
823 if (i != CS_SUCCESS)
824 goto next_entry;
825 if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
826 link->conf.Vpp1 = link->conf.Vpp2 = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
827 if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
828 link->conf.ConfigIndex = cf->index;
829 link->io.BasePort1 = cf->io.win[0].base;
830 link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
831 i = CardServices(RequestIO, link->handle, &link->io);
832 if (i == CS_SUCCESS)
833 goto found_port;
834 }
835 next_entry:
836 i = next_tuple(handle, &tuple, &parse);
837 }
838 }
839
840 /* Second pass: try to find an entry that isn't picky about
841 its base address, then try to grab any standard serial port
842 address, and finally try to get any free port. */
843 i = first_tuple(handle, &tuple, &parse);
844 while (i != CS_NO_MORE_ITEMS) {
845 if ((i == CS_SUCCESS) && (cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
846 link->conf.ConfigIndex = cf->index;
847 for (j = 0; j < 5; j++) {
848 link->io.BasePort1 = base[j];
849 link->io.IOAddrLines = base[j] ? 16 : 3;
850 i = CardServices(RequestIO, link->handle, &link->io);
851 if (i == CS_SUCCESS)
852 goto found_port;
853 }
854 }
855 i = next_tuple(handle, &tuple, &parse);
856 }
857
858 found_port:
859 if (i != CS_SUCCESS) {
860 printk(KERN_NOTICE "bt3c_cs: No usable port range found. Giving up.\n");
861 cs_error(link->handle, RequestIO, i);
862 goto failed;
863 }
864
865 i = CardServices(RequestIRQ, link->handle, &link->irq);
866 if (i != CS_SUCCESS) {
867 cs_error(link->handle, RequestIRQ, i);
868 link->irq.AssignedIRQ = 0;
869 }
870
871 i = CardServices(RequestConfiguration, link->handle, &link->conf);
872 if (i != CS_SUCCESS) {
873 cs_error(link->handle, RequestConfiguration, i);
874 goto failed;
875 }
876
877 MOD_INC_USE_COUNT;
878
879 if (bt3c_open(info) != 0)
880 goto failed;
881
882 strcpy(info->node.dev_name, info->hdev.name);
883 link->dev = &info->node;
884 link->state &= ~DEV_CONFIG_PENDING;
885
886 return;
887
888 cs_failed:
889 cs_error(link->handle, last_fn, last_ret);
890
891 failed:
892 bt3c_release((u_long)link);
893 }
894
895
bt3c_release(u_long arg)896 void bt3c_release(u_long arg)
897 {
898 dev_link_t *link = (dev_link_t *)arg;
899 bt3c_info_t *info = link->priv;
900
901 if (link->state & DEV_PRESENT)
902 bt3c_close(info);
903
904 MOD_DEC_USE_COUNT;
905
906 link->dev = NULL;
907
908 CardServices(ReleaseConfiguration, link->handle);
909 CardServices(ReleaseIO, link->handle, &link->io);
910 CardServices(ReleaseIRQ, link->handle, &link->irq);
911
912 link->state &= ~DEV_CONFIG;
913 }
914
915
bt3c_event(event_t event,int priority,event_callback_args_t * args)916 int bt3c_event(event_t event, int priority, event_callback_args_t *args)
917 {
918 dev_link_t *link = args->client_data;
919 bt3c_info_t *info = link->priv;
920
921 switch (event) {
922 case CS_EVENT_CARD_REMOVAL:
923 link->state &= ~DEV_PRESENT;
924 if (link->state & DEV_CONFIG) {
925 bt3c_close(info);
926 mod_timer(&link->release, jiffies + HZ / 20);
927 }
928 break;
929 case CS_EVENT_CARD_INSERTION:
930 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
931 bt3c_config(link);
932 break;
933 case CS_EVENT_PM_SUSPEND:
934 link->state |= DEV_SUSPEND;
935 /* Fall through... */
936 case CS_EVENT_RESET_PHYSICAL:
937 if (link->state & DEV_CONFIG)
938 CardServices(ReleaseConfiguration, link->handle);
939 break;
940 case CS_EVENT_PM_RESUME:
941 link->state &= ~DEV_SUSPEND;
942 /* Fall through... */
943 case CS_EVENT_CARD_RESET:
944 if (DEV_OK(link))
945 CardServices(RequestConfiguration, link->handle, &link->conf);
946 break;
947 }
948
949 return 0;
950 }
951
952
953
954 /* ======================== Module initialization ======================== */
955
956
init_bt3c_cs(void)957 int __init init_bt3c_cs(void)
958 {
959 servinfo_t serv;
960 int err;
961
962 CardServices(GetCardServicesInfo, &serv);
963 if (serv.Revision != CS_RELEASE_CODE) {
964 printk(KERN_NOTICE "bt3c_cs: Card Services release does not match!\n");
965 return -1;
966 }
967
968 err = register_pccard_driver(&dev_info, &bt3c_attach, &bt3c_detach);
969
970 return err;
971 }
972
973
exit_bt3c_cs(void)974 void __exit exit_bt3c_cs(void)
975 {
976 unregister_pccard_driver(&dev_info);
977
978 while (dev_list != NULL)
979 bt3c_detach(dev_list);
980 }
981
982
983 module_init(init_bt3c_cs);
984 module_exit(exit_bt3c_cs);
985
986 EXPORT_NO_SYMBOLS;
987