]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - drivers/staging/lirc/lirc_zilog.c
sched: Reverse the topology list
[mv-sheeva.git] / drivers / staging / lirc / lirc_zilog.c
index 0aad0d7a74a3789c6a416b9391e254d01843d51c..dd6a57c3c3a3149bac53eb36fd4ddeabed804050 100644 (file)
 #include <media/lirc_dev.h>
 #include <media/lirc.h>
 
+struct IR;
+
 struct IR_rx {
+       struct kref ref;
+       struct IR *ir;
+
        /* RX device */
+       struct mutex client_lock;
        struct i2c_client *c;
 
-       /* RX device buffer & lock */
-       struct lirc_buffer buf;
-       struct mutex buf_lock;
-
        /* RX polling thread data */
        struct task_struct *task;
 
@@ -80,7 +82,11 @@ struct IR_rx {
 };
 
 struct IR_tx {
+       struct kref ref;
+       struct IR *ir;
+
        /* TX device */
+       struct mutex client_lock;
        struct i2c_client *c;
 
        /* TX additional actions needed */
@@ -89,19 +95,34 @@ struct IR_tx {
 };
 
 struct IR {
+       struct kref ref;
+       struct list_head list;
+
+       /* FIXME spinlock access to l.features */
        struct lirc_driver l;
+       struct lirc_buffer rbuf;
 
        struct mutex ir_lock;
-       int open;
+       atomic_t open_count;
 
        struct i2c_adapter *adapter;
+
+       spinlock_t rx_ref_lock; /* struct IR_rx kref get()/put() */
        struct IR_rx *rx;
+
+       spinlock_t tx_ref_lock; /* struct IR_tx kref get()/put() */
        struct IR_tx *tx;
 };
 
-/* Minor -> data mapping */
-static struct mutex ir_devices_lock;
-static struct IR *ir_devices[MAX_IRCTL_DEVICES];
+/* IR transceiver instance object list */
+/*
+ * This lock is used for the following:
+ * a. ir_devices_list access, insertions, deletions
+ * b. struct IR kref get()s and put()s
+ * c. serialization of ir_probe() for the two i2c_clients for a Z8
+ */
+static DEFINE_MUTEX(ir_devices_lock);
+static LIST_HEAD(ir_devices_list);
 
 /* Block size for IR transmitter */
 #define TX_BLOCK_SIZE  99
@@ -147,6 +168,157 @@ static int minor = -1;    /* minor number */
                                 ## args);                              \
        } while (0)
 
+
+/* struct IR reference counting */
+static struct IR *get_ir_device(struct IR *ir, bool ir_devices_lock_held)
+{
+       if (ir_devices_lock_held) {
+               kref_get(&ir->ref);
+       } else {
+               mutex_lock(&ir_devices_lock);
+               kref_get(&ir->ref);
+               mutex_unlock(&ir_devices_lock);
+       }
+       return ir;
+}
+
+static void release_ir_device(struct kref *ref)
+{
+       struct IR *ir = container_of(ref, struct IR, ref);
+
+       /*
+        * Things should be in this state by now:
+        * ir->rx set to NULL and deallocated - happens before ir->rx->ir put()
+        * ir->rx->task kthread stopped - happens before ir->rx->ir put()
+        * ir->tx set to NULL and deallocated - happens before ir->tx->ir put()
+        * ir->open_count ==  0 - happens on final close()
+        * ir_lock, tx_ref_lock, rx_ref_lock, all released
+        */
+       if (ir->l.minor >= 0 && ir->l.minor < MAX_IRCTL_DEVICES) {
+               lirc_unregister_driver(ir->l.minor);
+               ir->l.minor = MAX_IRCTL_DEVICES;
+       }
+       if (ir->rbuf.fifo_initialized)
+               lirc_buffer_free(&ir->rbuf);
+       list_del(&ir->list);
+       kfree(ir);
+}
+
+static int put_ir_device(struct IR *ir, bool ir_devices_lock_held)
+{
+       int released;
+
+       if (ir_devices_lock_held)
+               return kref_put(&ir->ref, release_ir_device);
+
+       mutex_lock(&ir_devices_lock);
+       released = kref_put(&ir->ref, release_ir_device);
+       mutex_unlock(&ir_devices_lock);
+
+       return released;
+}
+
+/* struct IR_rx reference counting */
+static struct IR_rx *get_ir_rx(struct IR *ir)
+{
+       struct IR_rx *rx;
+
+       spin_lock(&ir->rx_ref_lock);
+       rx = ir->rx;
+       if (rx != NULL)
+               kref_get(&rx->ref);
+       spin_unlock(&ir->rx_ref_lock);
+       return rx;
+}
+
+static void destroy_rx_kthread(struct IR_rx *rx, bool ir_devices_lock_held)
+{
+       /* end up polling thread */
+       if (!IS_ERR_OR_NULL(rx->task)) {
+               kthread_stop(rx->task);
+               rx->task = NULL;
+               /* Put the ir ptr that ir_probe() gave to the rx poll thread */
+               put_ir_device(rx->ir, ir_devices_lock_held);
+       }
+}
+
+static void release_ir_rx(struct kref *ref)
+{
+       struct IR_rx *rx = container_of(ref, struct IR_rx, ref);
+       struct IR *ir = rx->ir;
+
+       /*
+        * This release function can't do all the work, as we want
+        * to keep the rx_ref_lock a spinlock, and killing the poll thread
+        * and releasing the ir reference can cause a sleep.  That work is
+        * performed by put_ir_rx()
+        */
+       ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
+       /* Don't put_ir_device(rx->ir) here; lock can't be freed yet */
+       ir->rx = NULL;
+       /* Don't do the kfree(rx) here; we still need to kill the poll thread */
+       return;
+}
+
+static int put_ir_rx(struct IR_rx *rx, bool ir_devices_lock_held)
+{
+       int released;
+       struct IR *ir = rx->ir;
+
+       spin_lock(&ir->rx_ref_lock);
+       released = kref_put(&rx->ref, release_ir_rx);
+       spin_unlock(&ir->rx_ref_lock);
+       /* Destroy the rx kthread while not holding the spinlock */
+       if (released) {
+               destroy_rx_kthread(rx, ir_devices_lock_held);
+               kfree(rx);
+               /* Make sure we're not still in a poll_table somewhere */
+               wake_up_interruptible(&ir->rbuf.wait_poll);
+       }
+       /* Do a reference put() for the rx->ir reference, if we released rx */
+       if (released)
+               put_ir_device(ir, ir_devices_lock_held);
+       return released;
+}
+
+/* struct IR_tx reference counting */
+static struct IR_tx *get_ir_tx(struct IR *ir)
+{
+       struct IR_tx *tx;
+
+       spin_lock(&ir->tx_ref_lock);
+       tx = ir->tx;
+       if (tx != NULL)
+               kref_get(&tx->ref);
+       spin_unlock(&ir->tx_ref_lock);
+       return tx;
+}
+
+static void release_ir_tx(struct kref *ref)
+{
+       struct IR_tx *tx = container_of(ref, struct IR_tx, ref);
+       struct IR *ir = tx->ir;
+
+       ir->l.features &= ~LIRC_CAN_SEND_PULSE;
+       /* Don't put_ir_device(tx->ir) here, so our lock doesn't get freed */
+       ir->tx = NULL;
+       kfree(tx);
+}
+
+static int put_ir_tx(struct IR_tx *tx, bool ir_devices_lock_held)
+{
+       int released;
+       struct IR *ir = tx->ir;
+
+       spin_lock(&ir->tx_ref_lock);
+       released = kref_put(&tx->ref, release_ir_tx);
+       spin_unlock(&ir->tx_ref_lock);
+       /* Do a reference put() for the tx->ir reference, if we released tx */
+       if (released)
+               put_ir_device(ir, ir_devices_lock_held);
+       return released;
+}
+
 static int add_to_buf(struct IR *ir)
 {
        __u16 code;
@@ -156,23 +328,38 @@ static int add_to_buf(struct IR *ir)
        int ret;
        int failures = 0;
        unsigned char sendbuf[1] = { 0 };
-       struct IR_rx *rx = ir->rx;
+       struct lirc_buffer *rbuf = ir->l.rbuf;
+       struct IR_rx *rx;
+       struct IR_tx *tx;
 
+       if (lirc_buffer_full(rbuf)) {
+               dprintk("buffer overflow\n");
+               return -EOVERFLOW;
+       }
+
+       rx = get_ir_rx(ir);
        if (rx == NULL)
                return -ENXIO;
 
-       if (lirc_buffer_full(&rx->buf)) {
-               dprintk("buffer overflow\n");
-               return -EOVERFLOW;
+       /* Ensure our rx->c i2c_client remains valid for the duration */
+       mutex_lock(&rx->client_lock);
+       if (rx->c == NULL) {
+               mutex_unlock(&rx->client_lock);
+               put_ir_rx(rx, false);
+               return -ENXIO;
        }
 
+       tx = get_ir_tx(ir);
+
        /*
         * service the device as long as it is returning
         * data and we have space
         */
        do {
-               if (kthread_should_stop())
-                       return -ENODATA;
+               if (kthread_should_stop()) {
+                       ret = -ENODATA;
+                       break;
+               }
 
                /*
                 * Lock i2c bus for the duration.  RX/TX chips interfere so
@@ -182,7 +369,8 @@ static int add_to_buf(struct IR *ir)
 
                if (kthread_should_stop()) {
                        mutex_unlock(&ir->ir_lock);
-                       return -ENODATA;
+                       ret = -ENODATA;
+                       break;
                }
 
                /*
@@ -196,7 +384,7 @@ static int add_to_buf(struct IR *ir)
                                mutex_unlock(&ir->ir_lock);
                                zilog_error("unable to read from the IR chip "
                                            "after 3 resets, giving up\n");
-                               return ret;
+                               break;
                        }
 
                        /* Looks like the chip crashed, reset it */
@@ -206,19 +394,23 @@ static int add_to_buf(struct IR *ir)
                        set_current_state(TASK_UNINTERRUPTIBLE);
                        if (kthread_should_stop()) {
                                mutex_unlock(&ir->ir_lock);
-                               return -ENODATA;
+                               ret = -ENODATA;
+                               break;
                        }
                        schedule_timeout((100 * HZ + 999) / 1000);
-                       ir->tx->need_boot = 1;
+                       if (tx != NULL)
+                               tx->need_boot = 1;
 
                        ++failures;
                        mutex_unlock(&ir->ir_lock);
+                       ret = 0;
                        continue;
                }
 
                if (kthread_should_stop()) {
                        mutex_unlock(&ir->ir_lock);
-                       return -ENODATA;
+                       ret = -ENODATA;
+                       break;
                }
                ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
                mutex_unlock(&ir->ir_lock);
@@ -234,12 +426,17 @@ static int add_to_buf(struct IR *ir)
 
                /* key pressed ? */
                if (rx->hdpvr_data_fmt) {
-                       if (got_data && (keybuf[0] == 0x80))
-                               return 0;
-                       else if (got_data && (keybuf[0] == 0x00))
-                               return -ENODATA;
-               } else if ((rx->b[0] & 0x80) == 0)
-                       return got_data ? 0 : -ENODATA;
+                       if (got_data && (keybuf[0] == 0x80)) {
+                               ret = 0;
+                               break;
+                       } else if (got_data && (keybuf[0] == 0x00)) {
+                               ret = -ENODATA;
+                               break;
+                       }
+               } else if ((rx->b[0] & 0x80) == 0) {
+                       ret = got_data ? 0 : -ENODATA;
+                       break;
+               }
 
                /* look what we have */
                code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
@@ -248,11 +445,16 @@ static int add_to_buf(struct IR *ir)
                codes[1] = code & 0xff;
 
                /* return it */
-               lirc_buffer_write(&rx->buf, codes);
+               lirc_buffer_write(rbuf, codes);
                ++got_data;
-       } while (!lirc_buffer_full(&rx->buf));
+               ret = 0;
+       } while (!lirc_buffer_full(rbuf));
 
-       return 0;
+       mutex_unlock(&rx->client_lock);
+       if (tx != NULL)
+               put_ir_tx(tx, false);
+       put_ir_rx(rx, false);
+       return ret;
 }
 
 /*
@@ -268,19 +470,19 @@ static int add_to_buf(struct IR *ir)
 static int lirc_thread(void *arg)
 {
        struct IR *ir = arg;
-       struct IR_rx *rx = ir->rx;
+       struct lirc_buffer *rbuf = ir->l.rbuf;
 
        dprintk("poll thread started\n");
 
        while (!kthread_should_stop()) {
-               set_current_state(TASK_INTERRUPTIBLE);
-
                /* if device not opened, we can sleep half a second */
-               if (!ir->open) {
+               if (atomic_read(&ir->open_count) == 0) {
                        schedule_timeout(HZ/2);
                        continue;
                }
 
+               set_current_state(TASK_INTERRUPTIBLE);
+
                /*
                 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
                 * We use this interval as the chip resets every time you poll
@@ -295,7 +497,7 @@ static int lirc_thread(void *arg)
                if (kthread_should_stop())
                        break;
                if (!add_to_buf(ir))
-                       wake_up_interruptible(&rx->buf.wait_poll);
+                       wake_up_interruptible(&rbuf->wait_poll);
        }
 
        dprintk("poll thread ended\n");
@@ -304,34 +506,12 @@ static int lirc_thread(void *arg)
 
 static int set_use_inc(void *data)
 {
-       struct IR *ir = data;
-
-       if (ir->l.owner == NULL || try_module_get(ir->l.owner) == 0)
-               return -ENODEV;
-
-       /* lock bttv in memory while /dev/lirc is in use  */
-       /*
-        * this is completely broken code. lirc_unregister_driver()
-        * must be possible even when the device is open
-        */
-       if (ir->rx != NULL)
-               i2c_use_client(ir->rx->c);
-       if (ir->tx != NULL)
-               i2c_use_client(ir->tx->c);
-
        return 0;
 }
 
 static void set_use_dec(void *data)
 {
-       struct IR *ir = data;
-
-       if (ir->rx)
-               i2c_release_client(ir->rx->c);
-       if (ir->tx)
-               i2c_release_client(ir->tx->c);
-       if (ir->l.owner != NULL)
-               module_put(ir->l.owner);
+       return;
 }
 
 /* safe read of a uint32 (always network byte order) */
@@ -585,7 +765,7 @@ static int fw_load(struct IR_tx *tx)
        }
 
        /* Request codeset data file */
-       ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
+       ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", tx->ir->l.dev);
        if (ret != 0) {
                zilog_error("firmware haup-ir-blaster.bin not available "
                            "(%d)\n", ret);
@@ -711,59 +891,32 @@ out:
        return ret;
 }
 
-/* initialise the IR TX device */
-static int tx_init(struct IR_tx *tx)
-{
-       int ret;
-
-       /* Load 'firmware' */
-       ret = fw_load(tx);
-       if (ret != 0)
-               return ret;
-
-       /* Send boot block */
-       ret = send_boot_data(tx);
-       if (ret != 0)
-               return ret;
-       tx->need_boot = 0;
-
-       /* Looks good */
-       return 0;
-}
-
-/* do nothing stub to make LIRC happy */
-static loff_t lseek(struct file *filep, loff_t offset, int orig)
-{
-       return -ESPIPE;
-}
-
 /* copied from lirc_dev */
 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
 {
        struct IR *ir = filep->private_data;
-       struct IR_rx *rx = ir->rx;
-       int ret = 0, written = 0;
+       struct IR_rx *rx;
+       struct lirc_buffer *rbuf = ir->l.rbuf;
+       int ret = 0, written = 0, retries = 0;
+       unsigned int m;
        DECLARE_WAITQUEUE(wait, current);
 
        dprintk("read called\n");
-       if (rx == NULL)
-               return -ENODEV;
-
-       if (mutex_lock_interruptible(&rx->buf_lock))
-               return -ERESTARTSYS;
-
-       if (n % rx->buf.chunk_size) {
+       if (n % rbuf->chunk_size) {
                dprintk("read result = -EINVAL\n");
-               mutex_unlock(&rx->buf_lock);
                return -EINVAL;
        }
 
+       rx = get_ir_rx(ir);
+       if (rx == NULL)
+               return -ENXIO;
+
        /*
         * we add ourselves to the task queue before buffer check
         * to avoid losing scan code (in case when queue is awaken somewhere
         * between while condition checking and scheduling)
         */
-       add_wait_queue(&rx->buf.wait_poll, &wait);
+       add_wait_queue(&rbuf->wait_poll, &wait);
        set_current_state(TASK_INTERRUPTIBLE);
 
        /*
@@ -771,7 +924,7 @@ static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
         * mode and 'copy_to_user' is happy, wait for data.
         */
        while (written < n && ret == 0) {
-               if (lirc_buffer_empty(&rx->buf)) {
+               if (lirc_buffer_empty(rbuf)) {
                        /*
                         * According to the read(2) man page, 'written' can be
                         * returned as less than 'n', instead of blocking
@@ -791,20 +944,27 @@ static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
                        schedule();
                        set_current_state(TASK_INTERRUPTIBLE);
                } else {
-                       unsigned char buf[rx->buf.chunk_size];
-                       lirc_buffer_read(&rx->buf, buf);
-                       ret = copy_to_user((void *)outbuf+written, buf,
-                                          rx->buf.chunk_size);
-                       written += rx->buf.chunk_size;
+                       unsigned char buf[rbuf->chunk_size];
+                       m = lirc_buffer_read(rbuf, buf);
+                       if (m == rbuf->chunk_size) {
+                               ret = copy_to_user((void *)outbuf+written, buf,
+                                                  rbuf->chunk_size);
+                               written += rbuf->chunk_size;
+                       } else {
+                               retries++;
+                       }
+                       if (retries >= 5) {
+                               zilog_error("Buffer read failed!\n");
+                               ret = -EIO;
+                       }
                }
        }
 
-       remove_wait_queue(&rx->buf.wait_poll, &wait);
+       remove_wait_queue(&rbuf->wait_poll, &wait);
+       put_ir_rx(rx, false);
        set_current_state(TASK_RUNNING);
-       mutex_unlock(&rx->buf_lock);
 
-       dprintk("read result = %s (%d)\n",
-               ret ? "-EFAULT" : "OK", ret);
+       dprintk("read result = %d (%s)\n", ret, ret ? "Error" : "OK");
 
        return ret ? ret : written;
 }
@@ -931,17 +1091,27 @@ static ssize_t write(struct file *filep, const char *buf, size_t n,
                          loff_t *ppos)
 {
        struct IR *ir = filep->private_data;
-       struct IR_tx *tx = ir->tx;
+       struct IR_tx *tx;
        size_t i;
        int failures = 0;
 
-       if (tx == NULL)
-               return -ENODEV;
-
        /* Validate user parameters */
        if (n % sizeof(int))
                return -EINVAL;
 
+       /* Get a struct IR_tx reference */
+       tx = get_ir_tx(ir);
+       if (tx == NULL)
+               return -ENXIO;
+
+       /* Ensure our tx->c i2c_client remains valid for the duration */
+       mutex_lock(&tx->client_lock);
+       if (tx->c == NULL) {
+               mutex_unlock(&tx->client_lock);
+               put_ir_tx(tx, false);
+               return -ENXIO;
+       }
+
        /* Lock i2c bus for the duration */
        mutex_lock(&ir->ir_lock);
 
@@ -952,11 +1122,24 @@ static ssize_t write(struct file *filep, const char *buf, size_t n,
 
                if (copy_from_user(&command, buf + i, sizeof(command))) {
                        mutex_unlock(&ir->ir_lock);
+                       mutex_unlock(&tx->client_lock);
+                       put_ir_tx(tx, false);
                        return -EFAULT;
                }
 
                /* Send boot data first if required */
                if (tx->need_boot == 1) {
+                       /* Make sure we have the 'firmware' loaded, first */
+                       ret = fw_load(tx);
+                       if (ret != 0) {
+                               mutex_unlock(&ir->ir_lock);
+                               mutex_unlock(&tx->client_lock);
+                               put_ir_tx(tx, false);
+                               if (ret != -ENOMEM)
+                                       ret = -EIO;
+                               return ret;
+                       }
+                       /* Prep the chip for transmitting codes */
                        ret = send_boot_data(tx);
                        if (ret == 0)
                                tx->need_boot = 0;
@@ -968,6 +1151,8 @@ static ssize_t write(struct file *filep, const char *buf, size_t n,
                                            (unsigned)command & 0xFFFF);
                        if (ret == -EPROTO) {
                                mutex_unlock(&ir->ir_lock);
+                               mutex_unlock(&tx->client_lock);
+                               put_ir_tx(tx, false);
                                return ret;
                        }
                }
@@ -985,6 +1170,8 @@ static ssize_t write(struct file *filep, const char *buf, size_t n,
                                zilog_error("unable to send to the IR chip "
                                            "after 3 resets, giving up\n");
                                mutex_unlock(&ir->ir_lock);
+                               mutex_unlock(&tx->client_lock);
+                               put_ir_tx(tx, false);
                                return ret;
                        }
                        set_current_state(TASK_UNINTERRUPTIBLE);
@@ -998,6 +1185,11 @@ static ssize_t write(struct file *filep, const char *buf, size_t n,
        /* Release i2c bus */
        mutex_unlock(&ir->ir_lock);
 
+       mutex_unlock(&tx->client_lock);
+
+       /* Give back our struct IR_tx reference */
+       put_ir_tx(tx, false);
+
        /* All looks good */
        return n;
 }
@@ -1006,23 +1198,32 @@ static ssize_t write(struct file *filep, const char *buf, size_t n,
 static unsigned int poll(struct file *filep, poll_table *wait)
 {
        struct IR *ir = filep->private_data;
-       struct IR_rx *rx = ir->rx;
+       struct IR_rx *rx;
+       struct lirc_buffer *rbuf = ir->l.rbuf;
        unsigned int ret;
 
        dprintk("poll called\n");
-       if (rx == NULL)
-               return -ENODEV;
-
-       mutex_lock(&rx->buf_lock);
 
-       poll_wait(filep, &rx->buf.wait_poll, wait);
+       rx = get_ir_rx(ir);
+       if (rx == NULL) {
+               /*
+                * Revisit this, if our poll function ever reports writeable
+                * status for Tx
+                */
+               dprintk("poll result = POLLERR\n");
+               return POLLERR;
+       }
 
-       dprintk("poll result = %s\n",
-               lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
+       /*
+        * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
+        * that buffer's wait queue indicates we may have a new poll status.
+        */
+       poll_wait(filep, &rbuf->wait_poll, wait);
 
-       ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
+       /* Indicate what ops could happen immediately without blocking */
+       ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN|POLLRDNORM);
 
-       mutex_unlock(&rx->buf_lock);
+       dprintk("poll result = %s\n", ret ? "POLLIN|POLLRDNORM" : "none");
        return ret;
 }
 
@@ -1030,11 +1231,9 @@ static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
 {
        struct IR *ir = filep->private_data;
        int result;
-       unsigned long mode, features = 0;
+       unsigned long mode, features;
 
-       features |= LIRC_CAN_SEND_PULSE;
-       if (ir->rx != NULL)
-               features |= LIRC_CAN_REC_LIRCCODE;
+       features = ir->l.features;
 
        switch (cmd) {
        case LIRC_GET_LENGTH:
@@ -1061,9 +1260,15 @@ static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
                        result = -EINVAL;
                break;
        case LIRC_GET_SEND_MODE:
+               if (!(features&LIRC_CAN_SEND_MASK))
+                       return -ENOSYS;
+
                result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
                break;
        case LIRC_SET_SEND_MODE:
+               if (!(features&LIRC_CAN_SEND_MASK))
+                       return -ENOSYS;
+
                result = get_user(mode, (unsigned long *) arg);
                if (!result && mode != LIRC_MODE_PULSE)
                        return -EINVAL;
@@ -1074,13 +1279,24 @@ static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
        return result;
 }
 
-/* ir_devices_lock must be held */
-static struct IR *find_ir_device_by_minor(unsigned int minor)
+static struct IR *get_ir_device_by_minor(unsigned int minor)
 {
-       if (minor >= MAX_IRCTL_DEVICES)
-               return NULL;
+       struct IR *ir;
+       struct IR *ret = NULL;
+
+       mutex_lock(&ir_devices_lock);
+
+       if (!list_empty(&ir_devices_list)) {
+               list_for_each_entry(ir, &ir_devices_list, list) {
+                       if (ir->l.minor == minor) {
+                               ret = get_ir_device(ir, true);
+                               break;
+                       }
+               }
+       }
 
-       return ir_devices[minor];
+       mutex_unlock(&ir_devices_lock);
+       return ret;
 }
 
 /*
@@ -1090,31 +1306,20 @@ static struct IR *find_ir_device_by_minor(unsigned int minor)
 static int open(struct inode *node, struct file *filep)
 {
        struct IR *ir;
-       int ret;
        unsigned int minor = MINOR(node->i_rdev);
 
        /* find our IR struct */
-       mutex_lock(&ir_devices_lock);
-       ir = find_ir_device_by_minor(minor);
-       mutex_unlock(&ir_devices_lock);
+       ir = get_ir_device_by_minor(minor);
 
        if (ir == NULL)
                return -ENODEV;
 
-       /* increment in use count */
-       mutex_lock(&ir->ir_lock);
-       ++ir->open;
-       ret = set_use_inc(ir);
-       if (ret != 0) {
-               --ir->open;
-               mutex_unlock(&ir->ir_lock);
-               return ret;
-       }
-       mutex_unlock(&ir->ir_lock);
+       atomic_inc(&ir->open_count);
 
        /* stash our IR struct */
        filep->private_data = ir;
 
+       nonseekable_open(node, filep);
        return 0;
 }
 
@@ -1128,22 +1333,12 @@ static int close(struct inode *node, struct file *filep)
                return -ENODEV;
        }
 
-       /* decrement in use count */
-       mutex_lock(&ir->ir_lock);
-       --ir->open;
-       set_use_dec(ir);
-       mutex_unlock(&ir->ir_lock);
+       atomic_dec(&ir->open_count);
 
+       put_ir_device(ir, false);
        return 0;
 }
 
-static struct lirc_driver lirc_template = {
-       .name           = "lirc_zilog",
-       .set_use_inc    = set_use_inc,
-       .set_use_dec    = set_use_dec,
-       .owner          = THIS_MODULE
-};
-
 static int ir_remove(struct i2c_client *client);
 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
 
@@ -1170,7 +1365,7 @@ static struct i2c_driver driver = {
 
 static const struct file_operations lirc_fops = {
        .owner          = THIS_MODULE,
-       .llseek         = lseek,
+       .llseek         = no_llseek,
        .read           = read,
        .write          = write,
        .poll           = poll,
@@ -1182,97 +1377,64 @@ static const struct file_operations lirc_fops = {
        .release        = close
 };
 
-static void destroy_rx_kthread(struct IR_rx *rx)
-{
-       /* end up polling thread */
-       if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
-               kthread_stop(rx->task);
-               rx->task = NULL;
-       }
-}
+static struct lirc_driver lirc_template = {
+       .name           = "lirc_zilog",
+       .minor          = -1,
+       .code_length    = 13,
+       .buffer_size    = BUFLEN / 2,
+       .sample_rate    = 0, /* tell lirc_dev to not start its own kthread */
+       .chunk_size     = 2,
+       .set_use_inc    = set_use_inc,
+       .set_use_dec    = set_use_dec,
+       .fops           = &lirc_fops,
+       .owner          = THIS_MODULE,
+};
 
-/* ir_devices_lock must be held */
-static int add_ir_device(struct IR *ir)
+static int ir_remove(struct i2c_client *client)
 {
-       int i;
-
-       for (i = 0; i < MAX_IRCTL_DEVICES; i++)
-               if (ir_devices[i] == NULL) {
-                       ir_devices[i] = ir;
-                       break;
+       if (strncmp("ir_tx_z8", client->name, 8) == 0) {
+               struct IR_tx *tx = i2c_get_clientdata(client);
+               if (tx != NULL) {
+                       mutex_lock(&tx->client_lock);
+                       tx->c = NULL;
+                       mutex_unlock(&tx->client_lock);
+                       put_ir_tx(tx, false);
                }
-
-       return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
-}
-
-/* ir_devices_lock must be held */
-static void del_ir_device(struct IR *ir)
-{
-       int i;
-
-       for (i = 0; i < MAX_IRCTL_DEVICES; i++)
-               if (ir_devices[i] == ir) {
-                       ir_devices[i] = NULL;
-                       break;
+       } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
+               struct IR_rx *rx = i2c_get_clientdata(client);
+               if (rx != NULL) {
+                       mutex_lock(&rx->client_lock);
+                       rx->c = NULL;
+                       mutex_unlock(&rx->client_lock);
+                       put_ir_rx(rx, false);
                }
-}
-
-static int ir_remove(struct i2c_client *client)
-{
-       struct IR *ir = i2c_get_clientdata(client);
-
-       mutex_lock(&ir_devices_lock);
-
-       if (ir == NULL) {
-               /* We destroyed everything when the first client came through */
-               mutex_unlock(&ir_devices_lock);
-               return 0;
        }
-
-       /* Good-bye LIRC */
-       lirc_unregister_driver(ir->l.minor);
-
-       /* Good-bye Rx */
-       destroy_rx_kthread(ir->rx);
-       if (ir->rx != NULL) {
-               if (ir->rx->buf.fifo_initialized)
-                       lirc_buffer_free(&ir->rx->buf);
-               i2c_set_clientdata(ir->rx->c, NULL);
-               kfree(ir->rx);
-       }
-
-       /* Good-bye Tx */
-       i2c_set_clientdata(ir->tx->c, NULL);
-       kfree(ir->tx);
-
-       /* Good-bye IR */
-       del_ir_device(ir);
-       kfree(ir);
-
-       mutex_unlock(&ir_devices_lock);
        return 0;
 }
 
 
 /* ir_devices_lock must be held */
-static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
+static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
 {
-       int i;
-       struct IR *ir = NULL;
+       struct IR *ir;
 
-       for (i = 0; i < MAX_IRCTL_DEVICES; i++)
-               if (ir_devices[i] != NULL &&
-                   ir_devices[i]->adapter == adapter) {
-                       ir = ir_devices[i];
-                       break;
+       if (list_empty(&ir_devices_list))
+               return NULL;
+
+       list_for_each_entry(ir, &ir_devices_list, list)
+               if (ir->adapter == adapter) {
+                       get_ir_device(ir, true);
+                       return ir;
                }
 
-       return ir;
+       return NULL;
 }
 
 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
 {
        struct IR *ir;
+       struct IR_tx *tx;
+       struct IR_rx *rx;
        struct i2c_adapter *adap = client->adapter;
        int ret;
        bool tx_probe = false;
@@ -1296,133 +1458,170 @@ static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
        mutex_lock(&ir_devices_lock);
 
        /* Use a single struct IR instance for both the Rx and Tx functions */
-       ir = find_ir_device_by_adapter(adap);
+       ir = get_ir_device_by_adapter(adap);
        if (ir == NULL) {
                ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
                if (ir == NULL) {
                        ret = -ENOMEM;
                        goto out_no_ir;
                }
+               kref_init(&ir->ref);
+
                /* store for use in ir_probe() again, and open() later on */
-               ret = add_ir_device(ir);
-               if (ret)
-                       goto out_free_ir;
+               INIT_LIST_HEAD(&ir->list);
+               list_add_tail(&ir->list, &ir_devices_list);
 
                ir->adapter = adap;
                mutex_init(&ir->ir_lock);
+               atomic_set(&ir->open_count, 0);
+               spin_lock_init(&ir->tx_ref_lock);
+               spin_lock_init(&ir->rx_ref_lock);
 
                /* set lirc_dev stuff */
                memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
-               ir->l.minor       = minor; /* module option */
-               ir->l.code_length = 13;
-               ir->l.rbuf        = NULL;
-               ir->l.fops        = &lirc_fops;
-               ir->l.data        = ir;
-               ir->l.dev         = &adap->dev;
-               ir->l.sample_rate = 0;
+               /*
+                * FIXME this is a pointer reference to us, but no refcount.
+                *
+                * This OK for now, since lirc_dev currently won't touch this
+                * buffer as we provide our own lirc_fops.
+                *
+                * Currently our own lirc_fops rely on this ir->l.rbuf pointer
+                */
+               ir->l.rbuf = &ir->rbuf;
+               ir->l.dev  = &adap->dev;
+               ret = lirc_buffer_init(ir->l.rbuf,
+                                      ir->l.chunk_size, ir->l.buffer_size);
+               if (ret)
+                       goto out_put_ir;
        }
 
        if (tx_probe) {
+               /* Get the IR_rx instance for later, if already allocated */
+               rx = get_ir_rx(ir);
+
                /* Set up a struct IR_tx instance */
-               ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
-               if (ir->tx == NULL) {
+               tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
+               if (tx == NULL) {
                        ret = -ENOMEM;
-                       goto out_free_xx;
+                       goto out_put_xx;
                }
-
-               ir->tx->c = client;
-               ir->tx->need_boot = 1;
-               ir->tx->post_tx_ready_poll =
+               kref_init(&tx->ref);
+               ir->tx = tx;
+
+               ir->l.features |= LIRC_CAN_SEND_PULSE;
+               mutex_init(&tx->client_lock);
+               tx->c = client;
+               tx->need_boot = 1;
+               tx->post_tx_ready_poll =
                               (id->driver_data & ID_FLAG_HDPVR) ? false : true;
+
+               /* An ir ref goes to the struct IR_tx instance */
+               tx->ir = get_ir_device(ir, true);
+
+               /* A tx ref goes to the i2c_client */
+               i2c_set_clientdata(client, get_ir_tx(ir));
+
+               /*
+                * Load the 'firmware'.  We do this before registering with
+                * lirc_dev, so the first firmware load attempt does not happen
+                * after a open() or write() call on the device.
+                *
+                * Failure here is not deemed catastrophic, so the receiver will
+                * still be usable.  Firmware load will be retried in write(),
+                * if it is needed.
+                */
+               fw_load(tx);
+
+               /* Proceed only if the Rx client is also ready or not needed */
+               if (rx == NULL && !tx_only) {
+                       zilog_info("probe of IR Tx on %s (i2c-%d) done. Waiting"
+                                  " on IR Rx.\n", adap->name, adap->nr);
+                       goto out_ok;
+               }
        } else {
+               /* Get the IR_tx instance for later, if already allocated */
+               tx = get_ir_tx(ir);
+
                /* Set up a struct IR_rx instance */
-               ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
-               if (ir->rx == NULL) {
+               rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
+               if (rx == NULL) {
                        ret = -ENOMEM;
-                       goto out_free_xx;
+                       goto out_put_xx;
                }
+               kref_init(&rx->ref);
+               ir->rx = rx;
 
-               ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
-               if (ret)
-                       goto out_free_xx;
-
-               mutex_init(&ir->rx->buf_lock);
-               ir->rx->c = client;
-               ir->rx->hdpvr_data_fmt =
+               ir->l.features |= LIRC_CAN_REC_LIRCCODE;
+               mutex_init(&rx->client_lock);
+               rx->c = client;
+               rx->hdpvr_data_fmt =
                               (id->driver_data & ID_FLAG_HDPVR) ? true : false;
 
-               /* set lirc_dev stuff */
-               ir->l.rbuf = &ir->rx->buf;
-       }
-
-       i2c_set_clientdata(client, ir);
+               /* An ir ref goes to the struct IR_rx instance */
+               rx->ir = get_ir_device(ir, true);
 
-       /* Proceed only if we have the required Tx and Rx clients ready to go */
-       if (ir->tx == NULL ||
-           (ir->rx == NULL && !tx_only)) {
-               zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
-                          "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
-                          adap->nr, tx_probe ? "Rx" : "Tx");
-               goto out_ok;
-       }
+               /* An rx ref goes to the i2c_client */
+               i2c_set_clientdata(client, get_ir_rx(ir));
 
-       /* initialise RX device */
-       if (ir->rx != NULL) {
-               /* try to fire up polling thread */
-               ir->rx->task = kthread_run(lirc_thread, ir,
-                                          "zilog-rx-i2c-%d", adap->nr);
-               if (IS_ERR(ir->rx->task)) {
-                       ret = PTR_ERR(ir->rx->task);
+               /*
+                * Start the polling thread.
+                * It will only perform an empty loop around schedule_timeout()
+                * until we register with lirc_dev and the first user open()
+                */
+               /* An ir ref goes to the new rx polling kthread */
+               rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
+                                      "zilog-rx-i2c-%d", adap->nr);
+               if (IS_ERR(rx->task)) {
+                       ret = PTR_ERR(rx->task);
                        zilog_error("%s: could not start IR Rx polling thread"
                                    "\n", __func__);
-                       goto out_free_xx;
+                       /* Failed kthread, so put back the ir ref */
+                       put_ir_device(ir, true);
+                       /* Failure exit, so put back rx ref from i2c_client */
+                       i2c_set_clientdata(client, NULL);
+                       put_ir_rx(rx, true);
+                       ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
+                       goto out_put_xx;
+               }
+
+               /* Proceed only if the Tx client is also ready */
+               if (tx == NULL) {
+                       zilog_info("probe of IR Rx on %s (i2c-%d) done. Waiting"
+                                  " on IR Tx.\n", adap->name, adap->nr);
+                       goto out_ok;
                }
        }
 
        /* register with lirc */
+       ir->l.minor = minor; /* module option: user requested minor number */
        ir->l.minor = lirc_register_driver(&ir->l);
        if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
                zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
                            __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
                ret = -EBADRQC;
-               goto out_free_thread;
+               goto out_put_xx;
        }
+       zilog_info("IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
+                  adap->name, adap->nr, ir->l.minor);
 
-       /*
-        * if we have the tx device, load the 'firmware'.  We do this
-        * after registering with lirc as otherwise hotplug seems to take
-        * 10s to create the lirc device.
-        */
-       ret = tx_init(ir->tx);
-       if (ret != 0)
-               goto out_unregister;
-
-       zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
-                  tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
 out_ok:
+       if (rx != NULL)
+               put_ir_rx(rx, true);
+       if (tx != NULL)
+               put_ir_tx(tx, true);
+       put_ir_device(ir, true);
+       zilog_info("probe of IR %s on %s (i2c-%d) done\n",
+                  tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
        mutex_unlock(&ir_devices_lock);
        return 0;
 
-out_unregister:
-       lirc_unregister_driver(ir->l.minor);
-out_free_thread:
-       destroy_rx_kthread(ir->rx);
-out_free_xx:
-       if (ir->rx != NULL) {
-               if (ir->rx->buf.fifo_initialized)
-                       lirc_buffer_free(&ir->rx->buf);
-               if (ir->rx->c != NULL)
-                       i2c_set_clientdata(ir->rx->c, NULL);
-               kfree(ir->rx);
-       }
-       if (ir->tx != NULL) {
-               if (ir->tx->c != NULL)
-                       i2c_set_clientdata(ir->tx->c, NULL);
-               kfree(ir->tx);
-       }
-out_free_ir:
-       del_ir_device(ir);
-       kfree(ir);
+out_put_xx:
+       if (rx != NULL)
+               put_ir_rx(rx, true);
+       if (tx != NULL)
+               put_ir_tx(tx, true);
+out_put_ir:
+       put_ir_device(ir, true);
 out_no_ir:
        zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
                    __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
@@ -1438,7 +1637,6 @@ static int __init zilog_init(void)
        zilog_notify("Zilog/Hauppauge IR driver initializing\n");
 
        mutex_init(&tx_data_lock);
-       mutex_init(&ir_devices_lock);
 
        request_module("firmware_class");