3  *  A driver for Nokia Connectivity Card DTL-1 devices
 
   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/errno.h>
 
  33 #include <linux/ptrace.h>
 
  34 #include <linux/ioport.h>
 
  35 #include <linux/spinlock.h>
 
  36 #include <linux/moduleparam.h>
 
  38 #include <linux/skbuff.h>
 
  39 #include <linux/string.h>
 
  40 #include <linux/serial.h>
 
  41 #include <linux/serial_reg.h>
 
  42 #include <linux/bitops.h>
 
  43 #include <asm/system.h>
 
  46 #include <pcmcia/cs_types.h>
 
  47 #include <pcmcia/cs.h>
 
  48 #include <pcmcia/cistpl.h>
 
  49 #include <pcmcia/ciscode.h>
 
  50 #include <pcmcia/ds.h>
 
  51 #include <pcmcia/cisreg.h>
 
  53 #include <net/bluetooth/bluetooth.h>
 
  54 #include <net/bluetooth/hci_core.h>
 
  58 /* ======================== Module parameters ======================== */
 
  61 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
 
  62 MODULE_DESCRIPTION("Bluetooth driver for Nokia Connectivity Card DTL-1");
 
  63 MODULE_LICENSE("GPL");
 
  67 /* ======================== Local structures ======================== */
 
  70 typedef struct dtl1_info_t {
 
  76         spinlock_t lock;                /* For serializing operations */
 
  78         unsigned long flowmask;         /* HCI flow mask */
 
  81         struct sk_buff_head txq;
 
  82         unsigned long tx_state;
 
  84         unsigned long rx_state;
 
  85         unsigned long rx_count;
 
  86         struct sk_buff *rx_skb;
 
  90 static void dtl1_config(dev_link_t *link);
 
  91 static void dtl1_release(dev_link_t *link);
 
  92 static int dtl1_event(event_t event, int priority, event_callback_args_t *args);
 
  94 static dev_info_t dev_info = "dtl1_cs";
 
  96 static dev_link_t *dtl1_attach(void);
 
  97 static void dtl1_detach(dev_link_t *);
 
  99 static dev_link_t *dev_list = NULL;
 
 102 /* Transmit states  */
 
 103 #define XMIT_SENDING  1
 
 104 #define XMIT_WAKEUP   2
 
 105 #define XMIT_WAITING  8
 
 107 /* Receiver States */
 
 108 #define RECV_WAIT_NSH   0
 
 109 #define RECV_WAIT_DATA  1
 
 116 } __attribute__ ((packed)) nsh_t;       /* Nokia Specific Header */
 
 118 #define NSHL  4                         /* Nokia Specific Header Length */
 
 122 /* ======================== Interrupt handling ======================== */
 
 125 static int dtl1_write(unsigned int iobase, int fifo_size, __u8 *buf, int len)
 
 129         /* Tx FIFO should be empty */
 
 130         if (!(inb(iobase + UART_LSR) & UART_LSR_THRE))
 
 133         /* Fill FIFO with current frame */
 
 134         while ((fifo_size-- > 0) && (actual < len)) {
 
 135                 /* Transmit next byte */
 
 136                 outb(buf[actual], iobase + UART_TX);
 
 144 static void dtl1_write_wakeup(dtl1_info_t *info)
 
 147                 BT_ERR("Unknown device");
 
 151         if (test_bit(XMIT_WAITING, &(info->tx_state))) {
 
 152                 set_bit(XMIT_WAKEUP, &(info->tx_state));
 
 156         if (test_and_set_bit(XMIT_SENDING, &(info->tx_state))) {
 
 157                 set_bit(XMIT_WAKEUP, &(info->tx_state));
 
 162                 register unsigned int iobase = info->link.io.BasePort1;
 
 163                 register struct sk_buff *skb;
 
 166                 clear_bit(XMIT_WAKEUP, &(info->tx_state));
 
 168                 if (!(info->link.state & DEV_PRESENT))
 
 171                 if (!(skb = skb_dequeue(&(info->txq))))
 
 175                 len = dtl1_write(iobase, 32, skb->data, skb->len);
 
 177                 if (len == skb->len) {
 
 178                         set_bit(XMIT_WAITING, &(info->tx_state));
 
 182                         skb_queue_head(&(info->txq), skb);
 
 185                 info->hdev->stat.byte_tx += len;
 
 187         } while (test_bit(XMIT_WAKEUP, &(info->tx_state)));
 
 189         clear_bit(XMIT_SENDING, &(info->tx_state));
 
 193 static void dtl1_control(dtl1_info_t *info, struct sk_buff *skb)
 
 195         u8 flowmask = *(u8 *)skb->data;
 
 198         printk(KERN_INFO "Bluetooth: Nokia control data =");
 
 199         for (i = 0; i < skb->len; i++) {
 
 200                 printk(" %02x", skb->data[i]);
 
 204         /* transition to active state */
 
 205         if (((info->flowmask & 0x07) == 0) && ((flowmask & 0x07) != 0)) {
 
 206                 clear_bit(XMIT_WAITING, &(info->tx_state));
 
 207                 dtl1_write_wakeup(info);
 
 210         info->flowmask = flowmask;
 
 216 static void dtl1_receive(dtl1_info_t *info)
 
 223                 BT_ERR("Unknown device");
 
 227         iobase = info->link.io.BasePort1;
 
 230                 info->hdev->stat.byte_rx++;
 
 232                 /* Allocate packet */
 
 233                 if (info->rx_skb == NULL)
 
 234                         if (!(info->rx_skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) {
 
 235                                 BT_ERR("Can't allocate mem for new packet");
 
 236                                 info->rx_state = RECV_WAIT_NSH;
 
 237                                 info->rx_count = NSHL;
 
 241                 *skb_put(info->rx_skb, 1) = inb(iobase + UART_RX);
 
 242                 nsh = (nsh_t *)info->rx_skb->data;
 
 246                 if (info->rx_count == 0) {
 
 248                         switch (info->rx_state) {
 
 250                                 info->rx_state = RECV_WAIT_DATA;
 
 251                                 info->rx_count = nsh->len + (nsh->len & 0x0001);
 
 254                                 bt_cb(info->rx_skb)->pkt_type = nsh->type;
 
 256                                 /* remove PAD byte if it exists */
 
 257                                 if (nsh->len & 0x0001) {
 
 258                                         info->rx_skb->tail--;
 
 263                                 skb_pull(info->rx_skb, NSHL);
 
 265                                 switch (bt_cb(info->rx_skb)->pkt_type) {
 
 267                                         /* control data for the Nokia Card */
 
 268                                         dtl1_control(info, info->rx_skb);
 
 273                                         /* send frame to the HCI layer */
 
 274                                         info->rx_skb->dev = (void *) info->hdev;
 
 275                                         bt_cb(info->rx_skb)->pkt_type &= 0x0f;
 
 276                                         hci_recv_frame(info->rx_skb);
 
 280                                         BT_ERR("Unknown HCI packet with type 0x%02x received", bt_cb(info->rx_skb)->pkt_type);
 
 281                                         kfree_skb(info->rx_skb);
 
 285                                 info->rx_state = RECV_WAIT_NSH;
 
 286                                 info->rx_count = NSHL;
 
 293                 /* Make sure we don't stay here too long */
 
 294                 if (boguscount++ > 32)
 
 297         } while (inb(iobase + UART_LSR) & UART_LSR_DR);
 
 301 static irqreturn_t dtl1_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
 
 303         dtl1_info_t *info = dev_inst;
 
 309         if (!info || !info->hdev) {
 
 310                 BT_ERR("Call of irq %d for unknown device", irq);
 
 314         iobase = info->link.io.BasePort1;
 
 316         spin_lock(&(info->lock));
 
 318         iir = inb(iobase + UART_IIR) & UART_IIR_ID;
 
 321                 /* Clear interrupt */
 
 322                 lsr = inb(iobase + UART_LSR);
 
 329                         /* Receive interrupt */
 
 333                         if (lsr & UART_LSR_THRE) {
 
 334                                 /* Transmitter ready for data */
 
 335                                 dtl1_write_wakeup(info);
 
 339                         BT_ERR("Unhandled IIR=%#x", iir);
 
 343                 /* Make sure we don't stay here too long */
 
 344                 if (boguscount++ > 100)
 
 347                 iir = inb(iobase + UART_IIR) & UART_IIR_ID;
 
 351         msr = inb(iobase + UART_MSR);
 
 353         if (info->ri_latch ^ (msr & UART_MSR_RI)) {
 
 354                 info->ri_latch = msr & UART_MSR_RI;
 
 355                 clear_bit(XMIT_WAITING, &(info->tx_state));
 
 356                 dtl1_write_wakeup(info);
 
 359         spin_unlock(&(info->lock));
 
 366 /* ======================== HCI interface ======================== */
 
 369 static int dtl1_hci_open(struct hci_dev *hdev)
 
 371         set_bit(HCI_RUNNING, &(hdev->flags));
 
 377 static int dtl1_hci_flush(struct hci_dev *hdev)
 
 379         dtl1_info_t *info = (dtl1_info_t *)(hdev->driver_data);
 
 382         skb_queue_purge(&(info->txq));
 
 388 static int dtl1_hci_close(struct hci_dev *hdev)
 
 390         if (!test_and_clear_bit(HCI_RUNNING, &(hdev->flags)))
 
 393         dtl1_hci_flush(hdev);
 
 399 static int dtl1_hci_send_frame(struct sk_buff *skb)
 
 402         struct hci_dev *hdev = (struct hci_dev *)(skb->dev);
 
 407                 BT_ERR("Frame for unknown HCI device (hdev=NULL)");
 
 411         info = (dtl1_info_t *)(hdev->driver_data);
 
 413         switch (bt_cb(skb)->pkt_type) {
 
 414         case HCI_COMMAND_PKT:
 
 418         case HCI_ACLDATA_PKT:
 
 422         case HCI_SCODATA_PKT:
 
 431         s = bt_skb_alloc(NSHL + skb->len + 1, GFP_ATOMIC);
 
 432         skb_reserve(s, NSHL);
 
 433         memcpy(skb_put(s, skb->len), skb->data, skb->len);
 
 434         if (skb->len & 0x0001)
 
 435                 *skb_put(s, 1) = 0;     /* PAD */
 
 437         /* Prepend skb with Nokia frame header and queue */
 
 438         memcpy(skb_push(s, NSHL), &nsh, NSHL);
 
 439         skb_queue_tail(&(info->txq), s);
 
 441         dtl1_write_wakeup(info);
 
 449 static void dtl1_hci_destruct(struct hci_dev *hdev)
 
 454 static int dtl1_hci_ioctl(struct hci_dev *hdev, unsigned int cmd,  unsigned long arg)
 
 461 /* ======================== Card services HCI interaction ======================== */
 
 464 static int dtl1_open(dtl1_info_t *info)
 
 467         unsigned int iobase = info->link.io.BasePort1;
 
 468         struct hci_dev *hdev;
 
 470         spin_lock_init(&(info->lock));
 
 472         skb_queue_head_init(&(info->txq));
 
 474         info->rx_state = RECV_WAIT_NSH;
 
 475         info->rx_count = NSHL;
 
 478         set_bit(XMIT_WAITING, &(info->tx_state));
 
 480         /* Initialize HCI device */
 
 481         hdev = hci_alloc_dev();
 
 483                 BT_ERR("Can't allocate HCI device");
 
 489         hdev->type = HCI_PCCARD;
 
 490         hdev->driver_data = info;
 
 492         hdev->open     = dtl1_hci_open;
 
 493         hdev->close    = dtl1_hci_close;
 
 494         hdev->flush    = dtl1_hci_flush;
 
 495         hdev->send     = dtl1_hci_send_frame;
 
 496         hdev->destruct = dtl1_hci_destruct;
 
 497         hdev->ioctl    = dtl1_hci_ioctl;
 
 499         hdev->owner = THIS_MODULE;
 
 501         spin_lock_irqsave(&(info->lock), flags);
 
 504         outb(0, iobase + UART_MCR);
 
 506         /* Turn off interrupts */
 
 507         outb(0, iobase + UART_IER);
 
 509         /* Initialize UART */
 
 510         outb(UART_LCR_WLEN8, iobase + UART_LCR);        /* Reset DLAB */
 
 511         outb((UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2), iobase + UART_MCR);
 
 513         info->ri_latch = inb(info->link.io.BasePort1 + UART_MSR) & UART_MSR_RI;
 
 515         /* Turn on interrupts */
 
 516         outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
 
 518         spin_unlock_irqrestore(&(info->lock), flags);
 
 520         /* Timeout before it is safe to send the first HCI packet */
 
 523         /* Register HCI device */
 
 524         if (hci_register_dev(hdev) < 0) {
 
 525                 BT_ERR("Can't register HCI device");
 
 535 static int dtl1_close(dtl1_info_t *info)
 
 538         unsigned int iobase = info->link.io.BasePort1;
 
 539         struct hci_dev *hdev = info->hdev;
 
 544         dtl1_hci_close(hdev);
 
 546         spin_lock_irqsave(&(info->lock), flags);
 
 549         outb(0, iobase + UART_MCR);
 
 551         /* Turn off interrupts */
 
 552         outb(0, iobase + UART_IER);
 
 554         spin_unlock_irqrestore(&(info->lock), flags);
 
 556         if (hci_unregister_dev(hdev) < 0)
 
 557                 BT_ERR("Can't unregister HCI device %s", hdev->name);
 
 564 static dev_link_t *dtl1_attach(void)
 
 567         client_reg_t client_reg;
 
 571         /* Create new info device */
 
 572         info = kmalloc(sizeof(*info), GFP_KERNEL);
 
 575         memset(info, 0, sizeof(*info));
 
 580         link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
 
 581         link->io.NumPorts1 = 8;
 
 582         link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
 
 583         link->irq.IRQInfo1 = IRQ_LEVEL_ID;
 
 585         link->irq.Handler = dtl1_interrupt;
 
 586         link->irq.Instance = info;
 
 588         link->conf.Attributes = CONF_ENABLE_IRQ;
 
 590         link->conf.IntType = INT_MEMORY_AND_IO;
 
 592         /* Register with Card Services */
 
 593         link->next = dev_list;
 
 595         client_reg.dev_info = &dev_info;
 
 596         client_reg.Version = 0x0210;
 
 597         client_reg.event_callback_args.client_data = link;
 
 599         ret = pcmcia_register_client(&link->handle, &client_reg);
 
 600         if (ret != CS_SUCCESS) {
 
 601                 cs_error(link->handle, RegisterClient, ret);
 
 610 static void dtl1_detach(dev_link_t *link)
 
 612         dtl1_info_t *info = link->priv;
 
 616         /* Locate device structure */
 
 617         for (linkp = &dev_list; *linkp; linkp = &(*linkp)->next)
 
 624         if (link->state & DEV_CONFIG)
 
 628                 ret = pcmcia_deregister_client(link->handle);
 
 629                 if (ret != CS_SUCCESS)
 
 630                         cs_error(link->handle, DeregisterClient, ret);
 
 633         /* Unlink device structure, free bits */
 
 639 static int get_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
 
 643         i = pcmcia_get_tuple_data(handle, tuple);
 
 647         return pcmcia_parse_tuple(handle, tuple, parse);
 
 650 static int first_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
 
 652         if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
 
 653                 return CS_NO_MORE_ITEMS;
 
 654         return get_tuple(handle, tuple, parse);
 
 657 static int next_tuple(client_handle_t handle, tuple_t *tuple, cisparse_t *parse)
 
 659         if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
 
 660                 return CS_NO_MORE_ITEMS;
 
 661         return get_tuple(handle, tuple, parse);
 
 664 static void dtl1_config(dev_link_t *link)
 
 666         client_handle_t handle = link->handle;
 
 667         dtl1_info_t *info = link->priv;
 
 671         cistpl_cftable_entry_t *cf = &parse.cftable_entry;
 
 672         config_info_t config;
 
 673         int i, last_ret, last_fn;
 
 675         tuple.TupleData = (cisdata_t *)buf;
 
 676         tuple.TupleOffset = 0;
 
 677         tuple.TupleDataMax = 255;
 
 678         tuple.Attributes = 0;
 
 680         /* Get configuration register information */
 
 681         tuple.DesiredTuple = CISTPL_CONFIG;
 
 682         last_ret = first_tuple(handle, &tuple, &parse);
 
 683         if (last_ret != CS_SUCCESS) {
 
 684                 last_fn = ParseTuple;
 
 687         link->conf.ConfigBase = parse.config.base;
 
 688         link->conf.Present = parse.config.rmask[0];
 
 691         link->state |= DEV_CONFIG;
 
 692         i = pcmcia_get_configuration_info(handle, &config);
 
 693         link->conf.Vcc = config.Vcc;
 
 695         tuple.TupleData = (cisdata_t *)buf;
 
 696         tuple.TupleOffset = 0;
 
 697         tuple.TupleDataMax = 255;
 
 698         tuple.Attributes = 0;
 
 699         tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
 
 701         /* Look for a generic full-sized window */
 
 702         link->io.NumPorts1 = 8;
 
 703         i = first_tuple(handle, &tuple, &parse);
 
 704         while (i != CS_NO_MORE_ITEMS) {
 
 705                 if ((i == CS_SUCCESS) && (cf->io.nwin == 1) && (cf->io.win[0].len > 8)) {
 
 706                         link->conf.ConfigIndex = cf->index;
 
 707                         link->io.BasePort1 = cf->io.win[0].base;
 
 708                         link->io.NumPorts1 = cf->io.win[0].len; /*yo */
 
 709                         link->io.IOAddrLines = cf->io.flags & CISTPL_IO_LINES_MASK;
 
 710                         i = pcmcia_request_io(link->handle, &link->io);
 
 714                 i = next_tuple(handle, &tuple, &parse);
 
 717         if (i != CS_SUCCESS) {
 
 718                 cs_error(link->handle, RequestIO, i);
 
 722         i = pcmcia_request_irq(link->handle, &link->irq);
 
 723         if (i != CS_SUCCESS) {
 
 724                 cs_error(link->handle, RequestIRQ, i);
 
 725                 link->irq.AssignedIRQ = 0;
 
 728         i = pcmcia_request_configuration(link->handle, &link->conf);
 
 729         if (i != CS_SUCCESS) {
 
 730                 cs_error(link->handle, RequestConfiguration, i);
 
 734         if (dtl1_open(info) != 0)
 
 737         strcpy(info->node.dev_name, info->hdev->name);
 
 738         link->dev = &info->node;
 
 739         link->state &= ~DEV_CONFIG_PENDING;
 
 744         cs_error(link->handle, last_fn, last_ret);
 
 751 static void dtl1_release(dev_link_t *link)
 
 753         dtl1_info_t *info = link->priv;
 
 755         if (link->state & DEV_PRESENT)
 
 760         pcmcia_release_configuration(link->handle);
 
 761         pcmcia_release_io(link->handle, &link->io);
 
 762         pcmcia_release_irq(link->handle, &link->irq);
 
 764         link->state &= ~DEV_CONFIG;
 
 768 static int dtl1_event(event_t event, int priority, event_callback_args_t *args)
 
 770         dev_link_t *link = args->client_data;
 
 771         dtl1_info_t *info = link->priv;
 
 774         case CS_EVENT_CARD_REMOVAL:
 
 775                 link->state &= ~DEV_PRESENT;
 
 776                 if (link->state & DEV_CONFIG) {
 
 781         case CS_EVENT_CARD_INSERTION:
 
 782                 link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
 
 785         case CS_EVENT_PM_SUSPEND:
 
 786                 link->state |= DEV_SUSPEND;
 
 787                 /* Fall through... */
 
 788         case CS_EVENT_RESET_PHYSICAL:
 
 789                 if (link->state & DEV_CONFIG)
 
 790                         pcmcia_release_configuration(link->handle);
 
 792         case CS_EVENT_PM_RESUME:
 
 793                 link->state &= ~DEV_SUSPEND;
 
 794                 /* Fall through... */
 
 795         case CS_EVENT_CARD_RESET:
 
 797                         pcmcia_request_configuration(link->handle, &link->conf);
 
 804 static struct pcmcia_device_id dtl1_ids[] = {
 
 805         PCMCIA_DEVICE_PROD_ID12("Nokia Mobile Phones", "DTL-1", 0xe1bfdd64, 0xe168480d),
 
 806         PCMCIA_DEVICE_PROD_ID12("Socket", "CF", 0xb38bcc2e, 0x44ebf863),
 
 809 MODULE_DEVICE_TABLE(pcmcia, dtl1_ids);
 
 811 static struct pcmcia_driver dtl1_driver = {
 
 812         .owner          = THIS_MODULE,
 
 816         .attach         = dtl1_attach,
 
 818         .detach         = dtl1_detach,
 
 819         .id_table       = dtl1_ids,
 
 822 static int __init init_dtl1_cs(void)
 
 824         return pcmcia_register_driver(&dtl1_driver);
 
 828 static void __exit exit_dtl1_cs(void)
 
 830         pcmcia_unregister_driver(&dtl1_driver);
 
 831         BUG_ON(dev_list != NULL);
 
 834 module_init(init_dtl1_cs);
 
 835 module_exit(exit_dtl1_cs);