block: bd_start_claiming fix module refcount
[safe/jmp/linux-2.6] / drivers / bluetooth / btuart_cs.c
index 5948390..4ed7288 100644 (file)
  *
  */
 
-#include <linux/config.h>
 #include <linux/module.h>
 
 #include <linux/kernel.h>
 #include <linux/init.h>
 #include <linux/slab.h>
 #include <linux/types.h>
-#include <linux/sched.h>
 #include <linux/delay.h>
 #include <linux/errno.h>
 #include <linux/ptrace.h>
@@ -69,7 +67,6 @@ MODULE_LICENSE("GPL");
 
 typedef struct btuart_info_t {
        struct pcmcia_device *p_dev;
-       dev_node_t node;
 
        struct hci_dev *hdev;
 
@@ -84,7 +81,7 @@ typedef struct btuart_info_t {
 } btuart_info_t;
 
 
-static void btuart_config(struct pcmcia_device *link);
+static int btuart_config(struct pcmcia_device *link);
 static void btuart_release(struct pcmcia_device *link);
 
 static void btuart_detach(struct pcmcia_device *p_dev);
@@ -152,7 +149,7 @@ static void btuart_write_wakeup(btuart_info_t *info)
 
                clear_bit(XMIT_WAKEUP, &(info->tx_state));
 
-               if (!(info->p_dev->state & DEV_PRESENT))
+               if (!pcmcia_dev_present(info->p_dev))
                        return;
 
                if (!(skb = skb_dequeue(&(info->txq))))
@@ -252,20 +249,20 @@ static void btuart_receive(btuart_info_t *info)
                                switch (info->rx_state) {
 
                                case RECV_WAIT_EVENT_HEADER:
-                                       eh = (struct hci_event_hdr *)(info->rx_skb->data);
+                                       eh = hci_event_hdr(info->rx_skb);
                                        info->rx_state = RECV_WAIT_DATA;
                                        info->rx_count = eh->plen;
                                        break;
 
                                case RECV_WAIT_ACL_HEADER:
-                                       ah = (struct hci_acl_hdr *)(info->rx_skb->data);
+                                       ah = hci_acl_hdr(info->rx_skb);
                                        dlen = __le16_to_cpu(ah->dlen);
                                        info->rx_state = RECV_WAIT_DATA;
                                        info->rx_count = dlen;
                                        break;
 
                                case RECV_WAIT_SCO_HEADER:
-                                       sh = (struct hci_sco_hdr *)(info->rx_skb->data);
+                                       sh = hci_sco_hdr(info->rx_skb);
                                        info->rx_state = RECV_WAIT_DATA;
                                        info->rx_count = sh->dlen;
                                        break;
@@ -289,17 +286,17 @@ static void btuart_receive(btuart_info_t *info)
 }
 
 
-static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *regs)
+static irqreturn_t btuart_interrupt(int irq, void *dev_inst)
 {
        btuart_info_t *info = dev_inst;
        unsigned int iobase;
        int boguscount = 0;
        int iir, lsr;
+       irqreturn_t r = IRQ_NONE;
 
-       if (!info || !info->hdev) {
-               BT_ERR("Call of irq %d for unknown device", irq);
+       if (!info || !info->hdev)
+               /* our irq handler is shared */
                return IRQ_NONE;
-       }
 
        iobase = info->p_dev->io.BasePort1;
 
@@ -307,6 +304,7 @@ static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *reg
 
        iir = inb(iobase + UART_IIR) & UART_IIR_ID;
        while (iir) {
+               r = IRQ_HANDLED;
 
                /* Clear interrupt */
                lsr = inb(iobase + UART_LSR);
@@ -340,7 +338,7 @@ static irqreturn_t btuart_interrupt(int irq, void *dev_inst, struct pt_regs *reg
 
        spin_unlock(&(info->lock));
 
-       return IRQ_HANDLED;
+       return r;
 }
 
 
@@ -388,7 +386,7 @@ static void btuart_change_speed(btuart_info_t *info, unsigned int speed)
        outb(lcr, iobase + UART_LCR);   /* Set 8N1  */
        outb(fcr, iobase + UART_FCR);   /* Enable FIFO's */
 
-       /* Turn on interrups */
+       /* Turn on interrupts */
        outb(UART_IER_RLSI | UART_IER_RDI | UART_IER_THRI, iobase + UART_IER);
 
        spin_unlock_irqrestore(&(info->lock), flags);
@@ -501,8 +499,9 @@ static int btuart_open(btuart_info_t *info)
 
        info->hdev = hdev;
 
-       hdev->type = HCI_PCCARD;
+       hdev->bus = HCI_PCCARD;
        hdev->driver_data = info;
+       SET_HCIDEV_DEV(hdev, &info->p_dev->dev);
 
        hdev->open     = btuart_hci_open;
        hdev->close    = btuart_hci_close;
@@ -576,7 +575,7 @@ static int btuart_close(btuart_info_t *info)
        return 0;
 }
 
-static int btuart_attach(struct pcmcia_device *link)
+static int btuart_probe(struct pcmcia_device *link)
 {
        btuart_info_t *info;
 
@@ -590,19 +589,11 @@ static int btuart_attach(struct pcmcia_device *link)
 
        link->io.Attributes1 = IO_DATA_PATH_WIDTH_8;
        link->io.NumPorts1 = 8;
-       link->irq.Attributes = IRQ_TYPE_EXCLUSIVE | IRQ_HANDLE_PRESENT;
-       link->irq.IRQInfo1 = IRQ_LEVEL_ID;
-
-       link->irq.Handler = btuart_interrupt;
-       link->irq.Instance = info;
 
        link->conf.Attributes = CONF_ENABLE_IRQ;
        link->conf.IntType = INT_MEMORY_AND_IO;
 
-       link->state |= DEV_PRESENT | DEV_CONFIG_PENDING;
-       btuart_config(link);
-
-       return 0;
+       return btuart_config(link);
 }
 
 
@@ -610,144 +601,89 @@ static void btuart_detach(struct pcmcia_device *link)
 {
        btuart_info_t *info = link->priv;
 
-       if (link->state & DEV_CONFIG)
-               btuart_release(link);
-
+       btuart_release(link);
        kfree(info);
 }
 
-static int get_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
+static int btuart_check_config(struct pcmcia_device *p_dev,
+                              cistpl_cftable_entry_t *cf,
+                              cistpl_cftable_entry_t *dflt,
+                              unsigned int vcc,
+                              void *priv_data)
 {
-       int i;
-
-       i = pcmcia_get_tuple_data(handle, tuple);
-       if (i != CS_SUCCESS)
-               return i;
-
-       return pcmcia_parse_tuple(handle, tuple, parse);
-}
-
-static int first_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
-{
-       if (pcmcia_get_first_tuple(handle, tuple) != CS_SUCCESS)
-               return CS_NO_MORE_ITEMS;
-       return get_tuple(handle, tuple, parse);
+       int *try = priv_data;
+
+       if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
+               p_dev->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
+       if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) &&
+           (cf->io.win[0].base != 0)) {
+               p_dev->io.BasePort1 = cf->io.win[0].base;
+               p_dev->io.IOAddrLines = (*try == 0) ? 16 :
+                       cf->io.flags & CISTPL_IO_LINES_MASK;
+               if (!pcmcia_request_io(p_dev, &p_dev->io))
+                       return 0;
+       }
+       return -ENODEV;
 }
 
-static int next_tuple(struct pcmcia_device *handle, tuple_t *tuple, cisparse_t *parse)
+static int btuart_check_config_notpicky(struct pcmcia_device *p_dev,
+                                       cistpl_cftable_entry_t *cf,
+                                       cistpl_cftable_entry_t *dflt,
+                                       unsigned int vcc,
+                                       void *priv_data)
 {
-       if (pcmcia_get_next_tuple(handle, tuple) != CS_SUCCESS)
-               return CS_NO_MORE_ITEMS;
-       return get_tuple(handle, tuple, parse);
+       static unsigned int base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
+       int j;
+
+       if ((cf->io.nwin > 0) && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
+               for (j = 0; j < 5; j++) {
+                       p_dev->io.BasePort1 = base[j];
+                       p_dev->io.IOAddrLines = base[j] ? 16 : 3;
+                       if (!pcmcia_request_io(p_dev, &p_dev->io))
+                               return 0;
+               }
+       }
+       return -ENODEV;
 }
 
-static void btuart_config(struct pcmcia_device *link)
+static int btuart_config(struct pcmcia_device *link)
 {
-       static kio_addr_t base[5] = { 0x3f8, 0x2f8, 0x3e8, 0x2e8, 0x0 };
        btuart_info_t *info = link->priv;
-       tuple_t tuple;
-       u_short buf[256];
-       cisparse_t parse;
-       cistpl_cftable_entry_t *cf = &parse.cftable_entry;
-       int i, j, try, last_ret, last_fn;
-
-       tuple.TupleData = (cisdata_t *)buf;
-       tuple.TupleOffset = 0;
-       tuple.TupleDataMax = 255;
-       tuple.Attributes = 0;
-
-       /* Get configuration register information */
-       tuple.DesiredTuple = CISTPL_CONFIG;
-       last_ret = first_tuple(link, &tuple, &parse);
-       if (last_ret != CS_SUCCESS) {
-               last_fn = ParseTuple;
-               goto cs_failed;
-       }
-       link->conf.ConfigBase = parse.config.base;
-       link->conf.Present = parse.config.rmask[0];
-
-       /* Configure card */
-       link->state |= DEV_CONFIG;
-
-       /* First pass: look for a config entry that looks normal. */
-       tuple.TupleData = (cisdata_t *) buf;
-       tuple.TupleOffset = 0;
-       tuple.TupleDataMax = 255;
-       tuple.Attributes = 0;
-       tuple.DesiredTuple = CISTPL_CFTABLE_ENTRY;
-       /* Two tries: without IO aliases, then with aliases */
-       for (try = 0; try < 2; try++) {
-               i = first_tuple(link, &tuple, &parse);
-               while (i != CS_NO_MORE_ITEMS) {
-                       if (i != CS_SUCCESS)
-                               goto next_entry;
-                       if (cf->vpp1.present & (1 << CISTPL_POWER_VNOM))
-                               link->conf.Vpp = cf->vpp1.param[CISTPL_POWER_VNOM] / 10000;
-                       if ((cf->io.nwin > 0) && (cf->io.win[0].len == 8) && (cf->io.win[0].base != 0)) {
-                               link->conf.ConfigIndex = cf->index;
-                               link->io.BasePort1 = cf->io.win[0].base;
-                               link->io.IOAddrLines = (try == 0) ? 16 : cf->io.flags & CISTPL_IO_LINES_MASK;
-                               i = pcmcia_request_io(link, &link->io);
-                               if (i == CS_SUCCESS)
-                                       goto found_port;
-                       }
-next_entry:
-                       i = next_tuple(link, &tuple, &parse);
-               }
-       }
+       int i;
+       int try;
+
+       /* First pass: look for a config entry that looks normal.
+          Two tries: without IO aliases, then with aliases */
+       for (try = 0; try < 2; try++)
+               if (!pcmcia_loop_config(link, btuart_check_config, &try))
+                       goto found_port;
 
        /* Second pass: try to find an entry that isn't picky about
           its base address, then try to grab any standard serial port
           address, and finally try to get any free port. */
-       i = first_tuple(link, &tuple, &parse);
-       while (i != CS_NO_MORE_ITEMS) {
-               if ((i == CS_SUCCESS) && (cf->io.nwin > 0)
-                   && ((cf->io.flags & CISTPL_IO_LINES_MASK) <= 3)) {
-                       link->conf.ConfigIndex = cf->index;
-                       for (j = 0; j < 5; j++) {
-                               link->io.BasePort1 = base[j];
-                               link->io.IOAddrLines = base[j] ? 16 : 3;
-                               i = pcmcia_request_io(link, &link->io);
-                               if (i == CS_SUCCESS)
-                                       goto found_port;
-                       }
-               }
-               i = next_tuple(link, &tuple, &parse);
-       }
+       if (!pcmcia_loop_config(link, btuart_check_config_notpicky, NULL))
+               goto found_port;
+
+       BT_ERR("No usable port range found");
+       goto failed;
 
 found_port:
-       if (i != CS_SUCCESS) {
-               BT_ERR("No usable port range found");
-               cs_error(link, RequestIO, i);
+       i = pcmcia_request_irq(link, btuart_interrupt);
+       if (i != 0)
                goto failed;
-       }
-
-       i = pcmcia_request_irq(link, &link->irq);
-       if (i != CS_SUCCESS) {
-               cs_error(link, RequestIRQ, i);
-               link->irq.AssignedIRQ = 0;
-       }
 
        i = pcmcia_request_configuration(link, &link->conf);
-       if (i != CS_SUCCESS) {
-               cs_error(link, RequestConfiguration, i);
+       if (i != 0)
                goto failed;
-       }
 
        if (btuart_open(info) != 0)
                goto failed;
 
-       strcpy(info->node.dev_name, info->hdev->name);
-       link->dev_node = &info->node;
-       link->state &= ~DEV_CONFIG_PENDING;
-
-       return;
-
-cs_failed:
-       cs_error(link, last_fn, last_ret);
+       return 0;
 
 failed:
        btuart_release(link);
+       return -ENODEV;
 }
 
 
@@ -755,8 +691,7 @@ static void btuart_release(struct pcmcia_device *link)
 {
        btuart_info_t *info = link->priv;
 
-       if (link->state & DEV_PRESENT)
-               btuart_close(info);
+       btuart_close(info);
 
        pcmcia_disable_device(link);
 }
@@ -772,7 +707,7 @@ static struct pcmcia_driver btuart_driver = {
        .drv            = {
                .name   = "btuart_cs",
        },
-       .probe          = btuart_attach,
+       .probe          = btuart_probe,
        .remove         = btuart_detach,
        .id_table       = btuart_ids,
 };