Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  *
0003  *  Driver for the 3Com Bluetooth PCMCIA card
0004  *
0005  *  Copyright (C) 2001-2002  Marcel Holtmann <marcel@holtmann.org>
0006  *                           Jose Orlando Pereira <jop@di.uminho.pt>
0007  *
0008  *
0009  *  This program is free software; you can redistribute it and/or modify
0010  *  it under the terms of the GNU General Public License version 2 as
0011  *  published by the Free Software Foundation;
0012  *
0013  *  Software distributed under the License is distributed on an "AS
0014  *  IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
0015  *  implied. See the License for the specific language governing
0016  *  rights and limitations under the License.
0017  *
0018  *  The initial developer of the original code is David A. Hinds
0019  *  <dahinds@users.sourceforge.net>.  Portions created by David A. Hinds
0020  *  are Copyright (C) 1999 David A. Hinds.  All Rights Reserved.
0021  *
0022  */
0023 
0024 #include <linux/module.h>
0025 
0026 #include <linux/kernel.h>
0027 #include <linux/init.h>
0028 #include <linux/slab.h>
0029 #include <linux/types.h>
0030 #include <linux/delay.h>
0031 #include <linux/errno.h>
0032 #include <linux/ptrace.h>
0033 #include <linux/ioport.h>
0034 #include <linux/spinlock.h>
0035 #include <linux/moduleparam.h>
0036 
0037 #include <linux/skbuff.h>
0038 #include <linux/string.h>
0039 #include <linux/serial.h>
0040 #include <linux/serial_reg.h>
0041 #include <linux/bitops.h>
0042 #include <asm/io.h>
0043 
0044 #include <linux/device.h>
0045 #include <linux/firmware.h>
0046 
0047 #include <pcmcia/cistpl.h>
0048 #include <pcmcia/ciscode.h>
0049 #include <pcmcia/ds.h>
0050 #include <pcmcia/cisreg.h>
0051 
0052 #include <net/bluetooth/bluetooth.h>
0053 #include <net/bluetooth/hci_core.h>
0054 
0055 
0056 
0057 /* ======================== Module parameters ======================== */
0058 
0059 
0060 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
0061 MODULE_DESCRIPTION("Bluetooth driver for the 3Com Bluetooth PCMCIA card");
0062 MODULE_LICENSE("GPL");
0063 MODULE_FIRMWARE("BT3CPCC.bin");
0064 
0065 
0066 
0067 /* ======================== Local structures ======================== */
0068 
0069 
0070 struct bt3c_info {
0071     struct pcmcia_device *p_dev;
0072 
0073     struct hci_dev *hdev;
0074 
0075     spinlock_t lock;        /* For serializing operations */
0076 
0077     struct sk_buff_head txq;
0078     unsigned long tx_state;
0079 
0080     unsigned long rx_state;
0081     unsigned long rx_count;
0082     struct sk_buff *rx_skb;
0083 };
0084 
0085 
0086 static int bt3c_config(struct pcmcia_device *link);
0087 static void bt3c_release(struct pcmcia_device *link);
0088 
0089 static void bt3c_detach(struct pcmcia_device *p_dev);
0090 
0091 
0092 /* Transmit states  */
0093 #define XMIT_SENDING  1
0094 #define XMIT_WAKEUP   2
0095 #define XMIT_WAITING  8
0096 
0097 /* Receiver states */
0098 #define RECV_WAIT_PACKET_TYPE   0
0099 #define RECV_WAIT_EVENT_HEADER  1
0100 #define RECV_WAIT_ACL_HEADER    2
0101 #define RECV_WAIT_SCO_HEADER    3
0102 #define RECV_WAIT_DATA          4
0103 
0104 
0105 
0106 /* ======================== Special I/O functions ======================== */
0107 
0108 
0109 #define DATA_L   0
0110 #define DATA_H   1
0111 #define ADDR_L   2
0112 #define ADDR_H   3
0113 #define CONTROL  4
0114 
0115 
0116 static inline void bt3c_address(unsigned int iobase, unsigned short addr)
0117 {
0118     outb(addr & 0xff, iobase + ADDR_L);
0119     outb((addr >> 8) & 0xff, iobase + ADDR_H);
0120 }
0121 
0122 
0123 static inline void bt3c_put(unsigned int iobase, unsigned short value)
0124 {
0125     outb(value & 0xff, iobase + DATA_L);
0126     outb((value >> 8) & 0xff, iobase + DATA_H);
0127 }
0128 
0129 
0130 static inline void bt3c_io_write(unsigned int iobase, unsigned short addr, unsigned short value)
0131 {
0132     bt3c_address(iobase, addr);
0133     bt3c_put(iobase, value);
0134 }
0135 
0136 
0137 static inline unsigned short bt3c_get(unsigned int iobase)
0138 {
0139     unsigned short value = inb(iobase + DATA_L);
0140 
0141     value |= inb(iobase + DATA_H) << 8;
0142 
0143     return value;
0144 }
0145 
0146 
0147 static inline unsigned short bt3c_read(unsigned int iobase, unsigned short addr)
0148 {
0149     bt3c_address(iobase, addr);
0150 
0151     return bt3c_get(iobase);
0152 }
0153 
0154 
0155 
0156 /* ======================== Interrupt handling ======================== */
0157 
0158 
0159 static int bt3c_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
0160 {
0161     int actual = 0;
0162 
0163     bt3c_address(iobase, 0x7080);
0164 
0165     /* Fill FIFO with current frame */
0166     while (actual < len) {
0167         /* Transmit next byte */
0168         bt3c_put(iobase, buf[actual]);
0169         actual++;
0170     }
0171 
0172     bt3c_io_write(iobase, 0x7005, actual);
0173 
0174     return actual;
0175 }
0176 
0177 
0178 static void bt3c_write_wakeup(struct bt3c_info *info)
0179 {
0180     if (!info) {
0181         BT_ERR("Unknown device");
0182         return;
0183     }
0184 
0185     if (test_and_set_bit(XMIT_SENDING, &(info->tx_state)))
0186         return;
0187 
0188     do {
0189         unsigned int iobase = info->p_dev->resource[0]->start;
0190         register struct sk_buff *skb;
0191         int len;
0192 
0193         if (!pcmcia_dev_present(info->p_dev))
0194             break;
0195 
0196         skb = skb_dequeue(&(info->txq));
0197         if (!skb) {
0198             clear_bit(XMIT_SENDING, &(info->tx_state));
0199             break;
0200         }
0201 
0202         /* Send frame */
0203         len = bt3c_write(iobase, 256, skb->data, skb->len);
0204 
0205         if (len != skb->len)
0206             BT_ERR("Very strange");
0207 
0208         kfree_skb(skb);
0209 
0210         info->hdev->stat.byte_tx += len;
0211 
0212     } while (0);
0213 }
0214 
0215 
0216 static void bt3c_receive(struct bt3c_info *info)
0217 {
0218     unsigned int iobase;
0219     int size = 0, avail;
0220 
0221     if (!info) {
0222         BT_ERR("Unknown device");
0223         return;
0224     }
0225 
0226     iobase = info->p_dev->resource[0]->start;
0227 
0228     avail = bt3c_read(iobase, 0x7006);
0229 
0230     bt3c_address(iobase, 0x7480);
0231     while (size < avail) {
0232         size++;
0233         info->hdev->stat.byte_rx++;
0234 
0235         /* Allocate packet */
0236         if (!info->rx_skb) {
0237             info->rx_state = RECV_WAIT_PACKET_TYPE;
0238             info->rx_count = 0;
0239             info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
0240             if (!info->rx_skb) {
0241                 BT_ERR("Can't allocate mem for new packet");
0242                 return;
0243             }
0244         }
0245 
0246 
0247         if (info->rx_state == RECV_WAIT_PACKET_TYPE) {
0248 
0249             hci_skb_pkt_type(info->rx_skb) = inb(iobase + DATA_L);
0250             inb(iobase + DATA_H);
0251 
0252             switch (hci_skb_pkt_type(info->rx_skb)) {
0253 
0254             case HCI_EVENT_PKT:
0255                 info->rx_state = RECV_WAIT_EVENT_HEADER;
0256                 info->rx_count = HCI_EVENT_HDR_SIZE;
0257                 break;
0258 
0259             case HCI_ACLDATA_PKT:
0260                 info->rx_state = RECV_WAIT_ACL_HEADER;
0261                 info->rx_count = HCI_ACL_HDR_SIZE;
0262                 break;
0263 
0264             case HCI_SCODATA_PKT:
0265                 info->rx_state = RECV_WAIT_SCO_HEADER;
0266                 info->rx_count = HCI_SCO_HDR_SIZE;
0267                 break;
0268 
0269             default:
0270                 /* Unknown packet */
0271                 BT_ERR("Unknown HCI packet with type 0x%02x received",
0272                        hci_skb_pkt_type(info->rx_skb));
0273                 info->hdev->stat.err_rx++;
0274 
0275                 kfree_skb(info->rx_skb);
0276                 info->rx_skb = NULL;
0277                 break;
0278 
0279             }
0280 
0281         } else {
0282 
0283             __u8 x = inb(iobase + DATA_L);
0284 
0285             skb_put_u8(info->rx_skb, x);
0286             inb(iobase + DATA_H);
0287             info->rx_count--;
0288 
0289             if (info->rx_count == 0) {
0290 
0291                 int dlen;
0292                 struct hci_event_hdr *eh;
0293                 struct hci_acl_hdr *ah;
0294                 struct hci_sco_hdr *sh;
0295 
0296                 switch (info->rx_state) {
0297 
0298                 case RECV_WAIT_EVENT_HEADER:
0299                     eh = hci_event_hdr(info->rx_skb);
0300                     info->rx_state = RECV_WAIT_DATA;
0301                     info->rx_count = eh->plen;
0302                     break;
0303 
0304                 case RECV_WAIT_ACL_HEADER:
0305                     ah = hci_acl_hdr(info->rx_skb);
0306                     dlen = __le16_to_cpu(ah->dlen);
0307                     info->rx_state = RECV_WAIT_DATA;
0308                     info->rx_count = dlen;
0309                     break;
0310 
0311                 case RECV_WAIT_SCO_HEADER:
0312                     sh = hci_sco_hdr(info->rx_skb);
0313                     info->rx_state = RECV_WAIT_DATA;
0314                     info->rx_count = sh->dlen;
0315                     break;
0316 
0317                 case RECV_WAIT_DATA:
0318                     hci_recv_frame(info->hdev, info->rx_skb);
0319                     info->rx_skb = NULL;
0320                     break;
0321 
0322                 }
0323 
0324             }
0325 
0326         }
0327 
0328     }
0329 
0330     bt3c_io_write(iobase, 0x7006, 0x0000);
0331 }
0332 
0333 
0334 static irqreturn_t bt3c_interrupt(int irq, void *dev_inst)
0335 {
0336     struct bt3c_info *info = dev_inst;
0337     unsigned int iobase;
0338     int iir;
0339     irqreturn_t r = IRQ_NONE;
0340 
0341     if (!info || !info->hdev)
0342         /* our irq handler is shared */
0343         return IRQ_NONE;
0344 
0345     iobase = info->p_dev->resource[0]->start;
0346 
0347     spin_lock(&(info->lock));
0348 
0349     iir = inb(iobase + CONTROL);
0350     if (iir & 0x80) {
0351         int stat = bt3c_read(iobase, 0x7001);
0352 
0353         if ((stat & 0xff) == 0x7f) {
0354             BT_ERR("Very strange (stat=0x%04x)", stat);
0355         } else if ((stat & 0xff) != 0xff) {
0356             if (stat & 0x0020) {
0357                 int status = bt3c_read(iobase, 0x7002) & 0x10;
0358                 bt_dev_info(info->hdev, "Antenna %s",
0359                             status ? "out" : "in");
0360             }
0361             if (stat & 0x0001)
0362                 bt3c_receive(info);
0363             if (stat & 0x0002) {
0364                 clear_bit(XMIT_SENDING, &(info->tx_state));
0365                 bt3c_write_wakeup(info);
0366             }
0367 
0368             bt3c_io_write(iobase, 0x7001, 0x0000);
0369 
0370             outb(iir, iobase + CONTROL);
0371         }
0372         r = IRQ_HANDLED;
0373     }
0374 
0375     spin_unlock(&(info->lock));
0376 
0377     return r;
0378 }
0379 
0380 
0381 
0382 /* ======================== HCI interface ======================== */
0383 
0384 
0385 static int bt3c_hci_flush(struct hci_dev *hdev)
0386 {
0387     struct bt3c_info *info = hci_get_drvdata(hdev);
0388 
0389     /* Drop TX queue */
0390     skb_queue_purge(&(info->txq));
0391 
0392     return 0;
0393 }
0394 
0395 
0396 static int bt3c_hci_open(struct hci_dev *hdev)
0397 {
0398     return 0;
0399 }
0400 
0401 
0402 static int bt3c_hci_close(struct hci_dev *hdev)
0403 {
0404     bt3c_hci_flush(hdev);
0405 
0406     return 0;
0407 }
0408 
0409 
0410 static int bt3c_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
0411 {
0412     struct bt3c_info *info = hci_get_drvdata(hdev);
0413     unsigned long flags;
0414 
0415     switch (hci_skb_pkt_type(skb)) {
0416     case HCI_COMMAND_PKT:
0417         hdev->stat.cmd_tx++;
0418         break;
0419     case HCI_ACLDATA_PKT:
0420         hdev->stat.acl_tx++;
0421         break;
0422     case HCI_SCODATA_PKT:
0423         hdev->stat.sco_tx++;
0424         break;
0425     }
0426 
0427     /* Prepend skb with frame type */
0428     memcpy(skb_push(skb, 1), &hci_skb_pkt_type(skb), 1);
0429     skb_queue_tail(&(info->txq), skb);
0430 
0431     spin_lock_irqsave(&(info->lock), flags);
0432 
0433     bt3c_write_wakeup(info);
0434 
0435     spin_unlock_irqrestore(&(info->lock), flags);
0436 
0437     return 0;
0438 }
0439 
0440 
0441 
0442 /* ======================== Card services HCI interaction ======================== */
0443 
0444 
0445 static int bt3c_load_firmware(struct bt3c_info *info,
0446                   const unsigned char *firmware,
0447                   int count)
0448 {
0449     char *ptr = (char *) firmware;
0450     char b[9];
0451     unsigned int iobase, tmp, tn;
0452     unsigned long size, addr, fcs;
0453     int i, err = 0;
0454 
0455     iobase = info->p_dev->resource[0]->start;
0456 
0457     /* Reset */
0458     bt3c_io_write(iobase, 0x8040, 0x0404);
0459     bt3c_io_write(iobase, 0x8040, 0x0400);
0460 
0461     udelay(1);
0462 
0463     bt3c_io_write(iobase, 0x8040, 0x0404);
0464 
0465     udelay(17);
0466 
0467     /* Load */
0468     while (count) {
0469         if (ptr[0] != 'S') {
0470             BT_ERR("Bad address in firmware");
0471             err = -EFAULT;
0472             goto error;
0473         }
0474 
0475         memset(b, 0, sizeof(b));
0476         memcpy(b, ptr + 2, 2);
0477         if (kstrtoul(b, 16, &size) < 0)
0478             return -EINVAL;
0479 
0480         memset(b, 0, sizeof(b));
0481         memcpy(b, ptr + 4, 8);
0482         if (kstrtoul(b, 16, &addr) < 0)
0483             return -EINVAL;
0484 
0485         memset(b, 0, sizeof(b));
0486         memcpy(b, ptr + (size * 2) + 2, 2);
0487         if (kstrtoul(b, 16, &fcs) < 0)
0488             return -EINVAL;
0489 
0490         memset(b, 0, sizeof(b));
0491         for (tmp = 0, i = 0; i < size; i++) {
0492             memcpy(b, ptr + (i * 2) + 2, 2);
0493             if (kstrtouint(b, 16, &tn))
0494                 return -EINVAL;
0495             tmp += tn;
0496         }
0497 
0498         if (((tmp + fcs) & 0xff) != 0xff) {
0499             BT_ERR("Checksum error in firmware");
0500             err = -EILSEQ;
0501             goto error;
0502         }
0503 
0504         if (ptr[1] == '3') {
0505             bt3c_address(iobase, addr);
0506 
0507             memset(b, 0, sizeof(b));
0508             for (i = 0; i < (size - 4) / 2; i++) {
0509                 memcpy(b, ptr + (i * 4) + 12, 4);
0510                 if (kstrtouint(b, 16, &tmp))
0511                     return -EINVAL;
0512                 bt3c_put(iobase, tmp);
0513             }
0514         }
0515 
0516         ptr   += (size * 2) + 6;
0517         count -= (size * 2) + 6;
0518     }
0519 
0520     udelay(17);
0521 
0522     /* Boot */
0523     bt3c_address(iobase, 0x3000);
0524     outb(inb(iobase + CONTROL) | 0x40, iobase + CONTROL);
0525 
0526 error:
0527     udelay(17);
0528 
0529     /* Clear */
0530     bt3c_io_write(iobase, 0x7006, 0x0000);
0531     bt3c_io_write(iobase, 0x7005, 0x0000);
0532     bt3c_io_write(iobase, 0x7001, 0x0000);
0533 
0534     return err;
0535 }
0536 
0537 
0538 static int bt3c_open(struct bt3c_info *info)
0539 {
0540     const struct firmware *firmware;
0541     struct hci_dev *hdev;
0542     int err;
0543 
0544     spin_lock_init(&(info->lock));
0545 
0546     skb_queue_head_init(&(info->txq));
0547 
0548     info->rx_state = RECV_WAIT_PACKET_TYPE;
0549     info->rx_count = 0;
0550     info->rx_skb = NULL;
0551 
0552     /* Initialize HCI device */
0553     hdev = hci_alloc_dev();
0554     if (!hdev) {
0555         BT_ERR("Can't allocate HCI device");
0556         return -ENOMEM;
0557     }
0558 
0559     info->hdev = hdev;
0560 
0561     hdev->bus = HCI_PCCARD;
0562     hci_set_drvdata(hdev, info);
0563     SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
0564 
0565     hdev->open  = bt3c_hci_open;
0566     hdev->close = bt3c_hci_close;
0567     hdev->flush = bt3c_hci_flush;
0568     hdev->send  = bt3c_hci_send_frame;
0569 
0570     /* Load firmware */
0571     err = request_firmware(&firmware, "BT3CPCC.bin", &info->p_dev->dev);
0572     if (err < 0) {
0573         BT_ERR("Firmware request failed");
0574         goto error;
0575     }
0576 
0577     err = bt3c_load_firmware(info, firmware->data, firmware->size);
0578 
0579     release_firmware(firmware);
0580 
0581     if (err < 0) {
0582         BT_ERR("Firmware loading failed");
0583         goto error;
0584     }
0585 
0586     /* Timeout before it is safe to send the first HCI packet */
0587     msleep(1000);
0588 
0589     /* Register HCI device */
0590     err = hci_register_dev(hdev);
0591     if (err < 0) {
0592         BT_ERR("Can't register HCI device");
0593         goto error;
0594     }
0595 
0596     return 0;
0597 
0598 error:
0599     info->hdev = NULL;
0600     hci_free_dev(hdev);
0601     return err;
0602 }
0603 
0604 
0605 static int bt3c_close(struct bt3c_info *info)
0606 {
0607     struct hci_dev *hdev = info->hdev;
0608 
0609     if (!hdev)
0610         return -ENODEV;
0611 
0612     bt3c_hci_close(hdev);
0613 
0614     hci_unregister_dev(hdev);
0615     hci_free_dev(hdev);
0616 
0617     return 0;
0618 }
0619 
0620 static int bt3c_probe(struct pcmcia_device *link)
0621 {
0622     struct bt3c_info *info;
0623 
0624     /* Create new info device */
0625     info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
0626     if (!info)
0627         return -ENOMEM;
0628 
0629     info->p_dev = link;
0630     link->priv = info;
0631 
0632     link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_VPP |
0633         CONF_AUTO_SET_IO;
0634 
0635     return bt3c_config(link);
0636 }
0637 
0638 
0639 static void bt3c_detach(struct pcmcia_device *link)
0640 {
0641     bt3c_release(link);
0642 }
0643 
0644 static int bt3c_check_config(struct pcmcia_device *p_dev, void *priv_data)
0645 {
0646     int *try = priv_data;
0647 
0648     if (!try)
0649         p_dev->io_lines = 16;
0650 
0651     if ((p_dev->resource[0]->end != 8) || (p_dev->resource[0]->start == 0))
0652         return -EINVAL;
0653 
0654     p_dev->resource[0]->end = 8;
0655     p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
0656     p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
0657 
0658     return pcmcia_request_io(p_dev);
0659 }
0660 
0661 static int bt3c_check_config_notpicky(struct pcmcia_device *p_dev,
0662                       void *priv_data)
0663 {
0664     static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
0665     int j;
0666 
0667     if (p_dev->io_lines > 3)
0668         return -ENODEV;
0669 
0670     p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
0671     p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
0672     p_dev->resource[0]->end = 8;
0673 
0674     for (j = 0; j < 5; j++) {
0675         p_dev->resource[0]->start = base[j];
0676         p_dev->io_lines = base[j] ? 16 : 3;
0677         if (!pcmcia_request_io(p_dev))
0678             return 0;
0679     }
0680     return -ENODEV;
0681 }
0682 
0683 static int bt3c_config(struct pcmcia_device *link)
0684 {
0685     struct bt3c_info *info = link->priv;
0686     int i;
0687     unsigned long try;
0688 
0689     /* First pass: look for a config entry that looks normal.
0690      * Two tries: without IO aliases, then with aliases
0691      */
0692     for (try = 0; try < 2; try++)
0693         if (!pcmcia_loop_config(link, bt3c_check_config, (void *) try))
0694             goto found_port;
0695 
0696     /* Second pass: try to find an entry that isn't picky about
0697      * its base address, then try to grab any standard serial port
0698      * address, and finally try to get any free port.
0699      */
0700     if (!pcmcia_loop_config(link, bt3c_check_config_notpicky, NULL))
0701         goto found_port;
0702 
0703     BT_ERR("No usable port range found");
0704     goto failed;
0705 
0706 found_port:
0707     i = pcmcia_request_irq(link, &bt3c_interrupt);
0708     if (i != 0)
0709         goto failed;
0710 
0711     i = pcmcia_enable_device(link);
0712     if (i != 0)
0713         goto failed;
0714 
0715     if (bt3c_open(info) != 0)
0716         goto failed;
0717 
0718     return 0;
0719 
0720 failed:
0721     bt3c_release(link);
0722     return -ENODEV;
0723 }
0724 
0725 
0726 static void bt3c_release(struct pcmcia_device *link)
0727 {
0728     struct bt3c_info *info = link->priv;
0729 
0730     bt3c_close(info);
0731 
0732     pcmcia_disable_device(link);
0733 }
0734 
0735 
0736 static const struct pcmcia_device_id bt3c_ids[] = {
0737     PCMCIA_DEVICE_PROD_ID13("3COM", "Bluetooth PC Card", 0xefce0a31, 0xd4ce9b02),
0738     PCMCIA_DEVICE_NULL
0739 };
0740 MODULE_DEVICE_TABLE(pcmcia, bt3c_ids);
0741 
0742 static struct pcmcia_driver bt3c_driver = {
0743     .owner      = THIS_MODULE,
0744     .name       = "bt3c_cs",
0745     .probe      = bt3c_probe,
0746     .remove     = bt3c_detach,
0747     .id_table   = bt3c_ids,
0748 };
0749 module_pcmcia_driver(bt3c_driver);