0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023 #include <linux/module.h>
0024
0025 #include <linux/kernel.h>
0026 #include <linux/init.h>
0027 #include <linux/slab.h>
0028 #include <linux/types.h>
0029 #include <linux/delay.h>
0030 #include <linux/errno.h>
0031 #include <linux/ptrace.h>
0032 #include <linux/ioport.h>
0033 #include <linux/spinlock.h>
0034 #include <linux/moduleparam.h>
0035
0036 #include <linux/skbuff.h>
0037 #include <linux/string.h>
0038 #include <linux/serial.h>
0039 #include <linux/serial_reg.h>
0040 #include <linux/bitops.h>
0041 #include <asm/io.h>
0042
0043 #include <pcmcia/cistpl.h>
0044 #include <pcmcia/ciscode.h>
0045 #include <pcmcia/ds.h>
0046 #include <pcmcia/cisreg.h>
0047
0048 #include <net/bluetooth/bluetooth.h>
0049 #include <net/bluetooth/hci_core.h>
0050
0051
0052
0053
0054
0055
0056 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
0057 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
0058 MODULE_LICENSE("GPL");
0059
0060
0061
0062
0063
0064
0065 struct dtl1_info {
0066 struct pcmcia_device *p_dev;
0067
0068 struct hci_dev *hdev;
0069
0070 spinlock_t lock;
0071
0072 unsigned long flowmask;
0073 int ri_latch;
0074
0075 struct sk_buff_head txq;
0076 unsigned long tx_state;
0077
0078 unsigned long rx_state;
0079 unsigned long rx_count;
0080 struct sk_buff *rx_skb;
0081 };
0082
0083
0084 static int dtl1_config(struct pcmcia_device *link);
0085
0086
0087
0088 #define XMIT_SENDING 1
0089 #define XMIT_WAKEUP 2
0090 #define XMIT_WAITING 8
0091
0092
0093 #define RECV_WAIT_NSH 0
0094 #define RECV_WAIT_DATA 1
0095
0096
0097 struct nsh {
0098 u8 type;
0099 u8 zero;
0100 u16 len;
0101 } __packed;
0102
0103 #define NSHL 4
0104
0105
0106
0107
0108
0109
0110 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
0111 {
0112 int actual = 0;
0113
0114
0115 if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
0116 return 0;
0117
0118
0119 while ((fifo_size-- > 0) && (actual < len)) {
0120
0121 outb(buf[actual], iobase + UART_TX);
0122 actual++;
0123 }
0124
0125 return actual;
0126 }
0127
0128
0129 static void dtl1_write_wakeup(struct dtl1_info *info)
0130 {
0131 if (!info) {
0132 BT_ERR("Unknown device");
0133 return;
0134 }
0135
0136 if (test_bit(XMIT_WAITING, &(info->tx_state))) {
0137 set_bit(XMIT_WAKEUP, &(info->tx_state));
0138 return;
0139 }
0140
0141 if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
0142 set_bit(XMIT_WAKEUP, &(info->tx_state));
0143 return;
0144 }
0145
0146 do {
0147 unsigned int iobase = info->p_dev->resource[0]->start;
0148 register struct sk_buff *skb;
0149 int len;
0150
0151 clear_bit(XMIT_WAKEUP, &(info->tx_state));
0152
0153 if (!pcmcia_dev_present(info->p_dev))
0154 return;
0155
0156 skb = skb_dequeue(&(info->txq));
0157 if (!skb)
0158 break;
0159
0160
0161 len = dtl1_write(iobase, 32, skb->data, skb->len);
0162
0163 if (len == skb->len) {
0164 set_bit(XMIT_WAITING, &(info->tx_state));
0165 kfree_skb(skb);
0166 } else {
0167 skb_pull(skb, len);
0168 skb_queue_head(&(info->txq), skb);
0169 }
0170
0171 info->hdev->stat.byte_tx += len;
0172
0173 } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
0174
0175 clear_bit(XMIT_SENDING, &(info->tx_state));
0176 }
0177
0178
0179 static void dtl1_control(struct dtl1_info *info, struct sk_buff *skb)
0180 {
0181 u8 flowmask = *(u8 *)skb->data;
0182 int i;
0183
0184 printk(KERN_INFO "Bluetooth: Nokia control data =");
0185 for (i = 0; i < skb->len; i++)
0186 printk(" %02x", skb->data[i]);
0187
0188 printk("\n");
0189
0190
0191 if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
0192 clear_bit(XMIT_WAITING, &(info->tx_state));
0193 dtl1_write_wakeup(info);
0194 }
0195
0196 info->flowmask = flowmask;
0197
0198 kfree_skb(skb);
0199 }
0200
0201
0202 static void dtl1_receive(struct dtl1_info *info)
0203 {
0204 unsigned int iobase;
0205 struct nsh *nsh;
0206 int boguscount = 0;
0207
0208 if (!info) {
0209 BT_ERR("Unknown device");
0210 return;
0211 }
0212
0213 iobase = info->p_dev->resource[0]->start;
0214
0215 do {
0216 info->hdev->stat.byte_rx++;
0217
0218
0219 if (info->rx_skb == NULL) {
0220 info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
0221 if (!info->rx_skb) {
0222 BT_ERR("Can't allocate mem for new packet");
0223 info->rx_state = RECV_WAIT_NSH;
0224 info->rx_count = NSHL;
0225 return;
0226 }
0227 }
0228
0229 skb_put_u8(info->rx_skb, inb(iobase + UART_RX));
0230 nsh = (struct nsh *)info->rx_skb->data;
0231
0232 info->rx_count--;
0233
0234 if (info->rx_count == 0) {
0235
0236 switch (info->rx_state) {
0237 case RECV_WAIT_NSH:
0238 info->rx_state = RECV_WAIT_DATA;
0239 info->rx_count = nsh->len + (nsh->len & 0x0001);
0240 break;
0241 case RECV_WAIT_DATA:
0242 hci_skb_pkt_type(info->rx_skb) = nsh->type;
0243
0244
0245 if (nsh->len & 0x0001) {
0246 info->rx_skb->tail--;
0247 info->rx_skb->len--;
0248 }
0249
0250
0251 skb_pull(info->rx_skb, NSHL);
0252
0253 switch (hci_skb_pkt_type(info->rx_skb)) {
0254 case 0x80:
0255
0256 dtl1_control(info, info->rx_skb);
0257 break;
0258 case 0x82:
0259 case 0x83:
0260 case 0x84:
0261
0262 hci_skb_pkt_type(info->rx_skb) &= 0x0f;
0263 hci_recv_frame(info->hdev, info->rx_skb);
0264 break;
0265 default:
0266
0267 BT_ERR("Unknown HCI packet with type 0x%02x received",
0268 hci_skb_pkt_type(info->rx_skb));
0269 kfree_skb(info->rx_skb);
0270 break;
0271 }
0272
0273 info->rx_state = RECV_WAIT_NSH;
0274 info->rx_count = NSHL;
0275 info->rx_skb = NULL;
0276 break;
0277 }
0278
0279 }
0280
0281
0282 if (boguscount++ > 32)
0283 break;
0284
0285 } while (inb(iobase + UART_LSR) & UART_LSR_DR);
0286 }
0287
0288
0289 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst)
0290 {
0291 struct dtl1_info *info = dev_inst;
0292 unsigned int iobase;
0293 unsigned char msr;
0294 int boguscount = 0;
0295 int iir, lsr;
0296 irqreturn_t r = IRQ_NONE;
0297
0298 if (!info || !info->hdev)
0299
0300 return IRQ_NONE;
0301
0302 iobase = info->p_dev->resource[0]->start;
0303
0304 spin_lock(&(info->lock));
0305
0306 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
0307 while (iir) {
0308
0309 r = IRQ_HANDLED;
0310
0311 lsr = inb(iobase + UART_LSR);
0312
0313 switch (iir) {
0314 case UART_IIR_RLSI:
0315 BT_ERR("RLSI");
0316 break;
0317 case UART_IIR_RDI:
0318
0319 dtl1_receive(info);
0320 break;
0321 case UART_IIR_THRI:
0322 if (lsr & UART_LSR_THRE) {
0323
0324 dtl1_write_wakeup(info);
0325 }
0326 break;
0327 default:
0328 BT_ERR("Unhandled IIR=%#x", iir);
0329 break;
0330 }
0331
0332
0333 if (boguscount++ > 100)
0334 break;
0335
0336 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
0337
0338 }
0339
0340 msr = inb(iobase + UART_MSR);
0341
0342 if (info->ri_latch ^ (msr & UART_MSR_RI)) {
0343 info->ri_latch = msr & UART_MSR_RI;
0344 clear_bit(XMIT_WAITING, &(info->tx_state));
0345 dtl1_write_wakeup(info);
0346 r = IRQ_HANDLED;
0347 }
0348
0349 spin_unlock(&(info->lock));
0350
0351 return r;
0352 }
0353
0354
0355
0356
0357
0358
0359 static int dtl1_hci_open(struct hci_dev *hdev)
0360 {
0361 return 0;
0362 }
0363
0364
0365 static int dtl1_hci_flush(struct hci_dev *hdev)
0366 {
0367 struct dtl1_info *info = hci_get_drvdata(hdev);
0368
0369
0370 skb_queue_purge(&(info->txq));
0371
0372 return 0;
0373 }
0374
0375
0376 static int dtl1_hci_close(struct hci_dev *hdev)
0377 {
0378 dtl1_hci_flush(hdev);
0379
0380 return 0;
0381 }
0382
0383
0384 static int dtl1_hci_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
0385 {
0386 struct dtl1_info *info = hci_get_drvdata(hdev);
0387 struct sk_buff *s;
0388 struct nsh nsh;
0389
0390 switch (hci_skb_pkt_type(skb)) {
0391 case HCI_COMMAND_PKT:
0392 hdev->stat.cmd_tx++;
0393 nsh.type = 0x81;
0394 break;
0395 case HCI_ACLDATA_PKT:
0396 hdev->stat.acl_tx++;
0397 nsh.type = 0x82;
0398 break;
0399 case HCI_SCODATA_PKT:
0400 hdev->stat.sco_tx++;
0401 nsh.type = 0x83;
0402 break;
0403 default:
0404 return -EILSEQ;
0405 }
0406
0407 nsh.zero = 0;
0408 nsh.len = skb->len;
0409
0410 s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
0411 if (!s)
0412 return -ENOMEM;
0413
0414 skb_reserve(s, NSHL);
0415 skb_copy_from_linear_data(skb, skb_put(s, skb->len), skb->len);
0416 if (skb->len & 0x0001)
0417 skb_put_u8(s, 0);
0418
0419
0420 memcpy(skb_push(s, NSHL), &nsh, NSHL);
0421 skb_queue_tail(&(info->txq), s);
0422
0423 dtl1_write_wakeup(info);
0424
0425 kfree_skb(skb);
0426
0427 return 0;
0428 }
0429
0430
0431
0432
0433
0434
0435 static int dtl1_open(struct dtl1_info *info)
0436 {
0437 unsigned long flags;
0438 unsigned int iobase = info->p_dev->resource[0]->start;
0439 struct hci_dev *hdev;
0440
0441 spin_lock_init(&(info->lock));
0442
0443 skb_queue_head_init(&(info->txq));
0444
0445 info->rx_state = RECV_WAIT_NSH;
0446 info->rx_count = NSHL;
0447 info->rx_skb = NULL;
0448
0449 set_bit(XMIT_WAITING, &(info->tx_state));
0450
0451
0452 hdev = hci_alloc_dev();
0453 if (!hdev) {
0454 BT_ERR("Can't allocate HCI device");
0455 return -ENOMEM;
0456 }
0457
0458 info->hdev = hdev;
0459
0460 hdev->bus = HCI_PCCARD;
0461 hci_set_drvdata(hdev, info);
0462 SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
0463
0464 hdev->open = dtl1_hci_open;
0465 hdev->close = dtl1_hci_close;
0466 hdev->flush = dtl1_hci_flush;
0467 hdev->send = dtl1_hci_send_frame;
0468
0469 spin_lock_irqsave(&(info->lock), flags);
0470
0471
0472 outb(0, iobase + UART_MCR);
0473
0474
0475 outb(0, iobase + UART_IER);
0476
0477
0478 outb(UART_LCR_WLEN8, iobase + UART_LCR);
0479 outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
0480
0481 info->ri_latch = inb(info->p_dev->resource[0]->start + UART_MSR)
0482 & UART_MSR_RI;
0483
0484
0485 outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
0486
0487 spin_unlock_irqrestore(&(info->lock), flags);
0488
0489
0490 msleep(2000);
0491
0492
0493 if (hci_register_dev(hdev) < 0) {
0494 BT_ERR("Can't register HCI device");
0495 info->hdev = NULL;
0496 hci_free_dev(hdev);
0497 return -ENODEV;
0498 }
0499
0500 return 0;
0501 }
0502
0503
0504 static int dtl1_close(struct dtl1_info *info)
0505 {
0506 unsigned long flags;
0507 unsigned int iobase = info->p_dev->resource[0]->start;
0508 struct hci_dev *hdev = info->hdev;
0509
0510 if (!hdev)
0511 return -ENODEV;
0512
0513 dtl1_hci_close(hdev);
0514
0515 spin_lock_irqsave(&(info->lock), flags);
0516
0517
0518 outb(0, iobase + UART_MCR);
0519
0520
0521 outb(0, iobase + UART_IER);
0522
0523 spin_unlock_irqrestore(&(info->lock), flags);
0524
0525 hci_unregister_dev(hdev);
0526 hci_free_dev(hdev);
0527
0528 return 0;
0529 }
0530
0531 static int dtl1_probe(struct pcmcia_device *link)
0532 {
0533 struct dtl1_info *info;
0534
0535
0536 info = devm_kzalloc(&link->dev, sizeof(*info), GFP_KERNEL);
0537 if (!info)
0538 return -ENOMEM;
0539
0540 info->p_dev = link;
0541 link->priv = info;
0542
0543 link->config_flags |= CONF_ENABLE_IRQ | CONF_AUTO_SET_IO;
0544
0545 return dtl1_config(link);
0546 }
0547
0548
0549 static void dtl1_detach(struct pcmcia_device *link)
0550 {
0551 struct dtl1_info *info = link->priv;
0552
0553 dtl1_close(info);
0554 pcmcia_disable_device(link);
0555 }
0556
0557 static int dtl1_confcheck(struct pcmcia_device *p_dev, void *priv_data)
0558 {
0559 if ((p_dev->resource[1]->end) || (p_dev->resource[1]->end < 8))
0560 return -ENODEV;
0561
0562 p_dev->resource[0]->flags &= ~IO_DATA_PATH_WIDTH;
0563 p_dev->resource[0]->flags |= IO_DATA_PATH_WIDTH_8;
0564
0565 return pcmcia_request_io(p_dev);
0566 }
0567
0568 static int dtl1_config(struct pcmcia_device *link)
0569 {
0570 struct dtl1_info *info = link->priv;
0571 int ret;
0572
0573
0574 link->resource[0]->end = 8;
0575 ret = pcmcia_loop_config(link, dtl1_confcheck, NULL);
0576 if (ret)
0577 goto failed;
0578
0579 ret = pcmcia_request_irq(link, dtl1_interrupt);
0580 if (ret)
0581 goto failed;
0582
0583 ret = pcmcia_enable_device(link);
0584 if (ret)
0585 goto failed;
0586
0587 ret = dtl1_open(info);
0588 if (ret)
0589 goto failed;
0590
0591 return 0;
0592
0593 failed:
0594 dtl1_detach(link);
0595 return ret;
0596 }
0597
0598 static const struct pcmcia_device_id dtl1_ids[] = {
0599 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
0600 PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-4", 0xe1bfdd64, 0x9102bc82),
0601 PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
0602 PCMCIA_DEVICE_PROD_ID12("Socket", "CF+ Personal Network Card", 0xb38bcc2e, 0xe732bae3),
0603 PCMCIA_DEVICE_NULL
0604 };
0605 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
0606
0607 static struct pcmcia_driver dtl1_driver = {
0608 .owner = THIS_MODULE,
0609 .name = "dtl1_cs",
0610 .probe = dtl1_probe,
0611 .remove = dtl1_detach,
0612 .id_table = dtl1_ids,
0613 };
0614 module_pcmcia_driver(dtl1_driver);