2 * i2c IR lirc driver for devices with zilog IR processors
4 * Copyright (c) 2000 Gerd Knorr <kraxel@goldbach.in-berlin.de>
5 * modified for PixelView (BT878P+W/FM) by
6 * Michal Kochanowicz <mkochano@pld.org.pl>
7 * Christoph Bartelmus <lirc@bartelmus.de>
8 * modified for KNC ONE TV Station/Anubis Typhoon TView Tuner by
9 * Ulrich Mueller <ulrich.mueller42@web.de>
10 * modified for Asus TV-Box and Creative/VisionTek BreakOut-Box by
11 * Stefan Jahn <stefan@lkcc.org>
12 * modified for inclusion into kernel sources by
13 * Jerome Brock <jbrock@users.sourceforge.net>
14 * modified for Leadtek Winfast PVR2000 by
15 * Thomas Reitmayr (treitmayr@yahoo.com)
16 * modified for Hauppauge PVR-150 IR TX device by
17 * Mark Weaver <mark@npsl.co.uk>
18 * changed name from lirc_pvr150 to lirc_zilog, works on more than pvr-150
19 * Jarod Wilson <jarod@redhat.com>
21 * parts are cut&pasted from the lirc_i2c.c driver
23 * Numerous changes updating lirc_zilog.c in kernel 2.6.38 and later are
24 * Copyright (C) 2011 Andy Walls <awalls@md.metrocast.net>
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
42 #include <linux/module.h>
43 #include <linux/kmod.h>
44 #include <linux/kernel.h>
45 #include <linux/sched.h>
47 #include <linux/poll.h>
48 #include <linux/string.h>
49 #include <linux/timer.h>
50 #include <linux/delay.h>
51 #include <linux/completion.h>
52 #include <linux/errno.h>
53 #include <linux/slab.h>
54 #include <linux/i2c.h>
55 #include <linux/firmware.h>
56 #include <linux/vmalloc.h>
58 #include <linux/mutex.h>
59 #include <linux/kthread.h>
61 #include <media/lirc_dev.h>
62 #include <media/lirc.h>
71 struct mutex client_lock;
74 /* RX polling thread data */
75 struct task_struct *task;
87 struct mutex client_lock;
90 /* TX additional actions needed */
92 bool post_tx_ready_poll;
97 struct list_head list;
99 /* FIXME spinlock access to l.features */
100 struct lirc_driver l;
101 struct lirc_buffer rbuf;
103 struct mutex ir_lock;
106 struct i2c_adapter *adapter;
108 spinlock_t rx_ref_lock; /* struct IR_rx kref get()/put() */
111 spinlock_t tx_ref_lock; /* struct IR_tx kref get()/put() */
115 /* IR transceiver instance object list */
117 * This lock is used for the following:
118 * a. ir_devices_list access, insertions, deletions
119 * b. struct IR kref get()s and put()s
120 * c. serialization of ir_probe() for the two i2c_clients for a Z8
122 static DEFINE_MUTEX(ir_devices_lock);
123 static LIST_HEAD(ir_devices_list);
125 /* Block size for IR transmitter */
126 #define TX_BLOCK_SIZE 99
128 /* Hauppauge IR transmitter data */
129 struct tx_data_struct {
131 unsigned char *boot_data;
133 /* Start of binary data block */
134 unsigned char *datap;
136 /* End of binary data block */
139 /* Number of installed codesets */
140 unsigned int num_code_sets;
142 /* Pointers to codesets */
143 unsigned char **code_sets;
145 /* Global fixed data template */
146 int fixed[TX_BLOCK_SIZE];
149 static struct tx_data_struct *tx_data;
150 static struct mutex tx_data_lock;
152 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
154 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
155 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
157 /* module parameters */
158 static bool debug; /* debug output */
159 static bool tx_only; /* only handle the IR Tx function */
160 static int minor = -1; /* minor number */
162 #define dprintk(fmt, args...) \
165 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
170 /* struct IR reference counting */
171 static struct IR *get_ir_device(struct IR *ir, bool ir_devices_lock_held)
173 if (ir_devices_lock_held) {
176 mutex_lock(&ir_devices_lock);
178 mutex_unlock(&ir_devices_lock);
183 static void release_ir_device(struct kref *ref)
185 struct IR *ir = container_of(ref, struct IR, ref);
188 * Things should be in this state by now:
189 * ir->rx set to NULL and deallocated - happens before ir->rx->ir put()
190 * ir->rx->task kthread stopped - happens before ir->rx->ir put()
191 * ir->tx set to NULL and deallocated - happens before ir->tx->ir put()
192 * ir->open_count == 0 - happens on final close()
193 * ir_lock, tx_ref_lock, rx_ref_lock, all released
195 if (ir->l.minor >= 0 && ir->l.minor < MAX_IRCTL_DEVICES) {
196 lirc_unregister_driver(ir->l.minor);
197 ir->l.minor = MAX_IRCTL_DEVICES;
199 if (ir->rbuf.fifo_initialized)
200 lirc_buffer_free(&ir->rbuf);
205 static int put_ir_device(struct IR *ir, bool ir_devices_lock_held)
209 if (ir_devices_lock_held)
210 return kref_put(&ir->ref, release_ir_device);
212 mutex_lock(&ir_devices_lock);
213 released = kref_put(&ir->ref, release_ir_device);
214 mutex_unlock(&ir_devices_lock);
219 /* struct IR_rx reference counting */
220 static struct IR_rx *get_ir_rx(struct IR *ir)
224 spin_lock(&ir->rx_ref_lock);
228 spin_unlock(&ir->rx_ref_lock);
232 static void destroy_rx_kthread(struct IR_rx *rx, bool ir_devices_lock_held)
234 /* end up polling thread */
235 if (!IS_ERR_OR_NULL(rx->task)) {
236 kthread_stop(rx->task);
238 /* Put the ir ptr that ir_probe() gave to the rx poll thread */
239 put_ir_device(rx->ir, ir_devices_lock_held);
243 static void release_ir_rx(struct kref *ref)
245 struct IR_rx *rx = container_of(ref, struct IR_rx, ref);
246 struct IR *ir = rx->ir;
249 * This release function can't do all the work, as we want
250 * to keep the rx_ref_lock a spinlock, and killing the poll thread
251 * and releasing the ir reference can cause a sleep. That work is
252 * performed by put_ir_rx()
254 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
255 /* Don't put_ir_device(rx->ir) here; lock can't be freed yet */
257 /* Don't do the kfree(rx) here; we still need to kill the poll thread */
261 static int put_ir_rx(struct IR_rx *rx, bool ir_devices_lock_held)
264 struct IR *ir = rx->ir;
266 spin_lock(&ir->rx_ref_lock);
267 released = kref_put(&rx->ref, release_ir_rx);
268 spin_unlock(&ir->rx_ref_lock);
269 /* Destroy the rx kthread while not holding the spinlock */
271 destroy_rx_kthread(rx, ir_devices_lock_held);
273 /* Make sure we're not still in a poll_table somewhere */
274 wake_up_interruptible(&ir->rbuf.wait_poll);
276 /* Do a reference put() for the rx->ir reference, if we released rx */
278 put_ir_device(ir, ir_devices_lock_held);
282 /* struct IR_tx reference counting */
283 static struct IR_tx *get_ir_tx(struct IR *ir)
287 spin_lock(&ir->tx_ref_lock);
291 spin_unlock(&ir->tx_ref_lock);
295 static void release_ir_tx(struct kref *ref)
297 struct IR_tx *tx = container_of(ref, struct IR_tx, ref);
298 struct IR *ir = tx->ir;
300 ir->l.features &= ~LIRC_CAN_SEND_PULSE;
301 /* Don't put_ir_device(tx->ir) here, so our lock doesn't get freed */
306 static int put_ir_tx(struct IR_tx *tx, bool ir_devices_lock_held)
309 struct IR *ir = tx->ir;
311 spin_lock(&ir->tx_ref_lock);
312 released = kref_put(&tx->ref, release_ir_tx);
313 spin_unlock(&ir->tx_ref_lock);
314 /* Do a reference put() for the tx->ir reference, if we released tx */
316 put_ir_device(ir, ir_devices_lock_held);
320 static int add_to_buf(struct IR *ir)
323 unsigned char codes[2];
324 unsigned char keybuf[6];
328 unsigned char sendbuf[1] = { 0 };
329 struct lirc_buffer *rbuf = ir->l.rbuf;
333 if (lirc_buffer_full(rbuf)) {
334 dprintk("buffer overflow\n");
342 /* Ensure our rx->c i2c_client remains valid for the duration */
343 mutex_lock(&rx->client_lock);
345 mutex_unlock(&rx->client_lock);
346 put_ir_rx(rx, false);
353 * service the device as long as it is returning
354 * data and we have space
357 if (kthread_should_stop()) {
363 * Lock i2c bus for the duration. RX/TX chips interfere so
366 mutex_lock(&ir->ir_lock);
368 if (kthread_should_stop()) {
369 mutex_unlock(&ir->ir_lock);
375 * Send random "poll command" (?) Windows driver does this
376 * and it is a good point to detect chip failure.
378 ret = i2c_master_send(rx->c, sendbuf, 1);
380 zilog_error("i2c_master_send failed with %d\n", ret);
382 mutex_unlock(&ir->ir_lock);
383 zilog_error("unable to read from the IR chip "
384 "after 3 resets, giving up\n");
388 /* Looks like the chip crashed, reset it */
389 zilog_error("polling the IR receiver chip failed, "
392 set_current_state(TASK_UNINTERRUPTIBLE);
393 if (kthread_should_stop()) {
394 mutex_unlock(&ir->ir_lock);
398 schedule_timeout((100 * HZ + 999) / 1000);
403 mutex_unlock(&ir->ir_lock);
408 if (kthread_should_stop()) {
409 mutex_unlock(&ir->ir_lock);
413 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
414 mutex_unlock(&ir->ir_lock);
415 if (ret != sizeof(keybuf)) {
416 zilog_error("i2c_master_recv failed with %d -- "
417 "keeping last read buffer\n", ret);
419 rx->b[0] = keybuf[3];
420 rx->b[1] = keybuf[4];
421 rx->b[2] = keybuf[5];
422 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
426 if (rx->hdpvr_data_fmt) {
427 if (got_data && (keybuf[0] == 0x80)) {
430 } else if (got_data && (keybuf[0] == 0x00)) {
434 } else if ((rx->b[0] & 0x80) == 0) {
435 ret = got_data ? 0 : -ENODATA;
439 /* look what we have */
440 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
442 codes[0] = (code >> 8) & 0xff;
443 codes[1] = code & 0xff;
446 lirc_buffer_write(rbuf, codes);
449 } while (!lirc_buffer_full(rbuf));
451 mutex_unlock(&rx->client_lock);
453 put_ir_tx(tx, false);
454 put_ir_rx(rx, false);
459 * Main function of the polling thread -- from lirc_dev.
460 * We don't fit the LIRC model at all anymore. This is horrible, but
461 * basically we have a single RX/TX device with a nasty failure mode
462 * that needs to be accounted for across the pair. lirc lets us provide
463 * fops, but prevents us from using the internal polling, etc. if we do
464 * so. Hence the replication. Might be neater to extend the LIRC model
465 * to account for this but I'd think it's a very special case of seriously
466 * messed up hardware.
468 static int lirc_thread(void *arg)
471 struct lirc_buffer *rbuf = ir->l.rbuf;
473 dprintk("poll thread started\n");
475 while (!kthread_should_stop()) {
476 set_current_state(TASK_INTERRUPTIBLE);
478 /* if device not opened, we can sleep half a second */
479 if (atomic_read(&ir->open_count) == 0) {
480 schedule_timeout(HZ/2);
485 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
486 * We use this interval as the chip resets every time you poll
487 * it (bad!). This is therefore just sufficient to catch all
488 * of the button presses. It makes the remote much more
489 * responsive. You can see the difference by running irw and
490 * holding down a button. With 100ms, the old polling
491 * interval, you'll notice breaks in the repeat sequence
492 * corresponding to lost keypresses.
494 schedule_timeout((260 * HZ) / 1000);
495 if (kthread_should_stop())
498 wake_up_interruptible(&rbuf->wait_poll);
501 dprintk("poll thread ended\n");
505 static int set_use_inc(void *data)
510 static void set_use_dec(void *data)
515 /* safe read of a uint32 (always network byte order) */
516 static int read_uint32(unsigned char **data,
517 unsigned char *endp, unsigned int *val)
519 if (*data + 4 > endp)
521 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
522 ((*data)[2] << 8) | (*data)[3];
527 /* safe read of a uint8 */
528 static int read_uint8(unsigned char **data,
529 unsigned char *endp, unsigned char *val)
531 if (*data + 1 > endp)
537 /* safe skipping of N bytes */
538 static int skip(unsigned char **data,
539 unsigned char *endp, unsigned int distance)
541 if (*data + distance > endp)
547 /* decompress key data into the given buffer */
548 static int get_key_data(unsigned char *buf,
549 unsigned int codeset, unsigned int key)
551 unsigned char *data, *endp, *diffs, *key_block;
552 unsigned char keys, ndiffs, id;
553 unsigned int base, lim, pos, i;
555 /* Binary search for the codeset */
556 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
557 pos = base + (lim >> 1);
558 data = tx_data->code_sets[pos];
560 if (!read_uint32(&data, tx_data->endp, &i))
565 else if (codeset > i) {
574 /* Set end of data block */
575 endp = pos < tx_data->num_code_sets - 1 ?
576 tx_data->code_sets[pos + 1] : tx_data->endp;
578 /* Read the block header */
579 if (!read_uint8(&data, endp, &keys) ||
580 !read_uint8(&data, endp, &ndiffs) ||
581 ndiffs > TX_BLOCK_SIZE || keys == 0)
584 /* Save diffs & skip */
586 if (!skip(&data, endp, ndiffs))
589 /* Read the id of the first key */
590 if (!read_uint8(&data, endp, &id))
593 /* Unpack the first key's data */
594 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
595 if (tx_data->fixed[i] == -1) {
596 if (!read_uint8(&data, endp, &buf[i]))
599 buf[i] = (unsigned char)tx_data->fixed[i];
603 /* Early out key found/not found */
611 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
614 /* Binary search for the key */
615 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
617 unsigned char *key_data;
618 pos = base + (lim >> 1);
619 key_data = key_block + (ndiffs + 1) * pos;
621 if (*key_data == key) {
625 /* found, so unpack the diffs */
626 for (i = 0; i < ndiffs; ++i) {
628 if (!read_uint8(&key_data, endp, &val) ||
629 diffs[i] >= TX_BLOCK_SIZE)
635 } else if (key > *key_data) {
644 zilog_error("firmware is corrupt\n");
648 /* send a block of data to the IR TX device */
649 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
652 unsigned char buf[5];
654 for (i = 0; i < TX_BLOCK_SIZE;) {
655 int tosend = TX_BLOCK_SIZE - i;
658 buf[0] = (unsigned char)(i + 1);
659 for (j = 0; j < tosend; ++j)
660 buf[1 + j] = data_block[i + j];
661 dprintk("%*ph", 5, buf);
662 ret = i2c_master_send(tx->c, buf, tosend + 1);
663 if (ret != tosend + 1) {
664 zilog_error("i2c_master_send failed with %d\n", ret);
665 return ret < 0 ? ret : -EFAULT;
672 /* send boot data to the IR TX device */
673 static int send_boot_data(struct IR_tx *tx)
676 unsigned char buf[4];
678 /* send the boot block */
679 ret = send_data_block(tx, tx_data->boot_data);
683 /* Hit the go button to activate the new boot data */
686 ret = i2c_master_send(tx->c, buf, 2);
688 zilog_error("i2c_master_send failed with %d\n", ret);
689 return ret < 0 ? ret : -EFAULT;
693 * Wait for zilog to settle after hitting go post boot block upload.
694 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
695 * upon attempting to get firmware revision, and tx probe thus fails.
697 for (i = 0; i < 10; i++) {
698 ret = i2c_master_send(tx->c, buf, 1);
705 zilog_error("i2c_master_send failed with %d\n", ret);
706 return ret < 0 ? ret : -EFAULT;
709 /* Here comes the firmware version... (hopefully) */
710 ret = i2c_master_recv(tx->c, buf, 4);
712 zilog_error("i2c_master_recv failed with %d\n", ret);
715 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
716 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
719 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
720 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
725 /* unload "firmware", lock held */
726 static void fw_unload_locked(void)
729 if (tx_data->code_sets)
730 vfree(tx_data->code_sets);
733 vfree(tx_data->datap);
737 dprintk("successfully unloaded IR blaster firmware\n");
741 /* unload "firmware" for the IR TX device */
742 static void fw_unload(void)
744 mutex_lock(&tx_data_lock);
746 mutex_unlock(&tx_data_lock);
749 /* load "firmware" for the IR TX device */
750 static int fw_load(struct IR_tx *tx)
754 unsigned char *data, version, num_global_fixed;
755 const struct firmware *fw_entry;
757 /* Already loaded? */
758 mutex_lock(&tx_data_lock);
764 /* Request codeset data file */
765 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", tx->ir->l.dev);
767 zilog_error("firmware haup-ir-blaster.bin not available "
769 ret = ret < 0 ? ret : -EFAULT;
772 dprintk("firmware of size %zu loaded\n", fw_entry->size);
775 tx_data = vmalloc(sizeof(*tx_data));
776 if (tx_data == NULL) {
777 zilog_error("out of memory\n");
778 release_firmware(fw_entry);
782 tx_data->code_sets = NULL;
784 /* Copy the data so hotplug doesn't get confused and timeout */
785 tx_data->datap = vmalloc(fw_entry->size);
786 if (tx_data->datap == NULL) {
787 zilog_error("out of memory\n");
788 release_firmware(fw_entry);
793 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
794 tx_data->endp = tx_data->datap + fw_entry->size;
795 release_firmware(fw_entry); fw_entry = NULL;
798 data = tx_data->datap;
799 if (!read_uint8(&data, tx_data->endp, &version))
802 zilog_error("unsupported code set file version (%u, expected"
803 "1) -- please upgrade to a newer driver",
810 /* Save boot block for later */
811 tx_data->boot_data = data;
812 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
815 if (!read_uint32(&data, tx_data->endp,
816 &tx_data->num_code_sets))
819 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
821 tx_data->code_sets = vmalloc(
822 tx_data->num_code_sets * sizeof(char *));
823 if (tx_data->code_sets == NULL) {
829 for (i = 0; i < TX_BLOCK_SIZE; ++i)
830 tx_data->fixed[i] = -1;
832 /* Read global fixed data template */
833 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
834 num_global_fixed > TX_BLOCK_SIZE)
836 for (i = 0; i < num_global_fixed; ++i) {
837 unsigned char pos, val;
838 if (!read_uint8(&data, tx_data->endp, &pos) ||
839 !read_uint8(&data, tx_data->endp, &val) ||
840 pos >= TX_BLOCK_SIZE)
842 tx_data->fixed[pos] = (int)val;
845 /* Filch out the position of each code set */
846 for (i = 0; i < tx_data->num_code_sets; ++i) {
849 unsigned char ndiffs;
851 /* Save the codeset position */
852 tx_data->code_sets[i] = data;
855 if (!read_uint32(&data, tx_data->endp, &id) ||
856 !read_uint8(&data, tx_data->endp, &keys) ||
857 !read_uint8(&data, tx_data->endp, &ndiffs) ||
858 ndiffs > TX_BLOCK_SIZE || keys == 0)
861 /* skip diff positions */
862 if (!skip(&data, tx_data->endp, ndiffs))
866 * After the diffs we have the first key id + data -
869 if (!skip(&data, tx_data->endp,
870 1 + TX_BLOCK_SIZE - num_global_fixed))
873 /* Then we have keys-1 blocks of key id+diffs */
874 if (!skip(&data, tx_data->endp,
875 (ndiffs + 1) * (keys - 1)))
882 zilog_error("firmware is corrupt\n");
887 mutex_unlock(&tx_data_lock);
891 /* copied from lirc_dev */
892 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
894 struct IR *ir = filep->private_data;
896 struct lirc_buffer *rbuf = ir->l.rbuf;
897 int ret = 0, written = 0, retries = 0;
899 DECLARE_WAITQUEUE(wait, current);
901 dprintk("read called\n");
902 if (n % rbuf->chunk_size) {
903 dprintk("read result = -EINVAL\n");
912 * we add ourselves to the task queue before buffer check
913 * to avoid losing scan code (in case when queue is awaken somewhere
914 * between while condition checking and scheduling)
916 add_wait_queue(&rbuf->wait_poll, &wait);
917 set_current_state(TASK_INTERRUPTIBLE);
920 * while we didn't provide 'length' bytes, device is opened in blocking
921 * mode and 'copy_to_user' is happy, wait for data.
923 while (written < n && ret == 0) {
924 if (lirc_buffer_empty(rbuf)) {
926 * According to the read(2) man page, 'written' can be
927 * returned as less than 'n', instead of blocking
928 * again, returning -EWOULDBLOCK, or returning
933 if (filep->f_flags & O_NONBLOCK) {
937 if (signal_pending(current)) {
942 set_current_state(TASK_INTERRUPTIBLE);
944 unsigned char buf[rbuf->chunk_size];
945 m = lirc_buffer_read(rbuf, buf);
946 if (m == rbuf->chunk_size) {
947 ret = copy_to_user((void *)outbuf+written, buf,
949 written += rbuf->chunk_size;
954 zilog_error("Buffer read failed!\n");
960 remove_wait_queue(&rbuf->wait_poll, &wait);
961 put_ir_rx(rx, false);
962 set_current_state(TASK_RUNNING);
964 dprintk("read result = %d (%s)\n", ret, ret ? "Error" : "OK");
966 return ret ? ret : written;
969 /* send a keypress to the IR TX device */
970 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
972 unsigned char data_block[TX_BLOCK_SIZE];
973 unsigned char buf[2];
976 /* Get data for the codeset/key */
977 ret = get_key_data(data_block, code, key);
979 if (ret == -EPROTO) {
980 zilog_error("failed to get data for code %u, key %u -- check "
981 "lircd.conf entries\n", code, key);
986 /* Send the data block */
987 ret = send_data_block(tx, data_block);
991 /* Send data block length? */
994 ret = i2c_master_send(tx->c, buf, 2);
996 zilog_error("i2c_master_send failed with %d\n", ret);
997 return ret < 0 ? ret : -EFAULT;
1000 /* Give the z8 a moment to process data block */
1001 for (i = 0; i < 10; i++) {
1002 ret = i2c_master_send(tx->c, buf, 1);
1009 zilog_error("i2c_master_send failed with %d\n", ret);
1010 return ret < 0 ? ret : -EFAULT;
1013 /* Send finished download? */
1014 ret = i2c_master_recv(tx->c, buf, 1);
1016 zilog_error("i2c_master_recv failed with %d\n", ret);
1017 return ret < 0 ? ret : -EFAULT;
1019 if (buf[0] != 0xA0) {
1020 zilog_error("unexpected IR TX response #1: %02x\n",
1025 /* Send prepare command? */
1028 ret = i2c_master_send(tx->c, buf, 2);
1030 zilog_error("i2c_master_send failed with %d\n", ret);
1031 return ret < 0 ? ret : -EFAULT;
1035 * The sleep bits aren't necessary on the HD PVR, and in fact, the
1036 * last i2c_master_recv always fails with a -5, so for now, we're
1037 * going to skip this whole mess and say we're done on the HD PVR
1039 if (!tx->post_tx_ready_poll) {
1040 dprintk("sent code %u, key %u\n", code, key);
1045 * This bit NAKs until the device is ready, so we retry it
1046 * sleeping a bit each time. This seems to be what the windows
1047 * driver does, approximately.
1050 for (i = 0; i < 20; ++i) {
1051 set_current_state(TASK_UNINTERRUPTIBLE);
1052 schedule_timeout((50 * HZ + 999) / 1000);
1053 ret = i2c_master_send(tx->c, buf, 1);
1056 dprintk("NAK expected: i2c_master_send "
1057 "failed with %d (try %d)\n", ret, i+1);
1060 zilog_error("IR TX chip never got ready: last i2c_master_send "
1061 "failed with %d\n", ret);
1062 return ret < 0 ? ret : -EFAULT;
1065 /* Seems to be an 'ok' response */
1066 i = i2c_master_recv(tx->c, buf, 1);
1068 zilog_error("i2c_master_recv failed with %d\n", ret);
1071 if (buf[0] != 0x80) {
1072 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
1076 /* Oh good, it worked */
1077 dprintk("sent code %u, key %u\n", code, key);
1082 * Write a code to the device. We take in a 32-bit number (an int) and then
1083 * decode this to a codeset/key index. The key data is then decompressed and
1084 * sent to the device. We have a spin lock as per i2c documentation to prevent
1085 * multiple concurrent sends which would probably cause the device to explode.
1087 static ssize_t write(struct file *filep, const char *buf, size_t n,
1090 struct IR *ir = filep->private_data;
1095 /* Validate user parameters */
1096 if (n % sizeof(int))
1099 /* Get a struct IR_tx reference */
1104 /* Ensure our tx->c i2c_client remains valid for the duration */
1105 mutex_lock(&tx->client_lock);
1106 if (tx->c == NULL) {
1107 mutex_unlock(&tx->client_lock);
1108 put_ir_tx(tx, false);
1112 /* Lock i2c bus for the duration */
1113 mutex_lock(&ir->ir_lock);
1115 /* Send each keypress */
1116 for (i = 0; i < n;) {
1120 if (copy_from_user(&command, buf + i, sizeof(command))) {
1121 mutex_unlock(&ir->ir_lock);
1122 mutex_unlock(&tx->client_lock);
1123 put_ir_tx(tx, false);
1127 /* Send boot data first if required */
1128 if (tx->need_boot == 1) {
1129 /* Make sure we have the 'firmware' loaded, first */
1132 mutex_unlock(&ir->ir_lock);
1133 mutex_unlock(&tx->client_lock);
1134 put_ir_tx(tx, false);
1139 /* Prep the chip for transmitting codes */
1140 ret = send_boot_data(tx);
1147 ret = send_code(tx, (unsigned)command >> 16,
1148 (unsigned)command & 0xFFFF);
1149 if (ret == -EPROTO) {
1150 mutex_unlock(&ir->ir_lock);
1151 mutex_unlock(&tx->client_lock);
1152 put_ir_tx(tx, false);
1158 * Hmm, a failure. If we've had a few then give up, otherwise
1162 /* Looks like the chip crashed, reset it */
1163 zilog_error("sending to the IR transmitter chip "
1164 "failed, trying reset\n");
1166 if (failures >= 3) {
1167 zilog_error("unable to send to the IR chip "
1168 "after 3 resets, giving up\n");
1169 mutex_unlock(&ir->ir_lock);
1170 mutex_unlock(&tx->client_lock);
1171 put_ir_tx(tx, false);
1174 set_current_state(TASK_UNINTERRUPTIBLE);
1175 schedule_timeout((100 * HZ + 999) / 1000);
1182 /* Release i2c bus */
1183 mutex_unlock(&ir->ir_lock);
1185 mutex_unlock(&tx->client_lock);
1187 /* Give back our struct IR_tx reference */
1188 put_ir_tx(tx, false);
1190 /* All looks good */
1194 /* copied from lirc_dev */
1195 static unsigned int poll(struct file *filep, poll_table *wait)
1197 struct IR *ir = filep->private_data;
1199 struct lirc_buffer *rbuf = ir->l.rbuf;
1202 dprintk("poll called\n");
1207 * Revisit this, if our poll function ever reports writeable
1210 dprintk("poll result = POLLERR\n");
1215 * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
1216 * that buffer's wait queue indicates we may have a new poll status.
1218 poll_wait(filep, &rbuf->wait_poll, wait);
1220 /* Indicate what ops could happen immediately without blocking */
1221 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN|POLLRDNORM);
1223 dprintk("poll result = %s\n", ret ? "POLLIN|POLLRDNORM" : "none");
1227 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1229 struct IR *ir = filep->private_data;
1231 unsigned long mode, features;
1233 features = ir->l.features;
1236 case LIRC_GET_LENGTH:
1237 result = put_user((unsigned long)13,
1238 (unsigned long *)arg);
1240 case LIRC_GET_FEATURES:
1241 result = put_user(features, (unsigned long *) arg);
1243 case LIRC_GET_REC_MODE:
1244 if (!(features&LIRC_CAN_REC_MASK))
1247 result = put_user(LIRC_REC2MODE
1248 (features&LIRC_CAN_REC_MASK),
1249 (unsigned long *)arg);
1251 case LIRC_SET_REC_MODE:
1252 if (!(features&LIRC_CAN_REC_MASK))
1255 result = get_user(mode, (unsigned long *)arg);
1256 if (!result && !(LIRC_MODE2REC(mode) & features))
1259 case LIRC_GET_SEND_MODE:
1260 if (!(features&LIRC_CAN_SEND_MASK))
1263 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1265 case LIRC_SET_SEND_MODE:
1266 if (!(features&LIRC_CAN_SEND_MASK))
1269 result = get_user(mode, (unsigned long *) arg);
1270 if (!result && mode != LIRC_MODE_PULSE)
1279 static struct IR *get_ir_device_by_minor(unsigned int minor)
1282 struct IR *ret = NULL;
1284 mutex_lock(&ir_devices_lock);
1286 if (!list_empty(&ir_devices_list)) {
1287 list_for_each_entry(ir, &ir_devices_list, list) {
1288 if (ir->l.minor == minor) {
1289 ret = get_ir_device(ir, true);
1295 mutex_unlock(&ir_devices_lock);
1300 * Open the IR device. Get hold of our IR structure and
1301 * stash it in private_data for the file
1303 static int open(struct inode *node, struct file *filep)
1306 unsigned int minor = MINOR(node->i_rdev);
1308 /* find our IR struct */
1309 ir = get_ir_device_by_minor(minor);
1314 atomic_inc(&ir->open_count);
1316 /* stash our IR struct */
1317 filep->private_data = ir;
1319 nonseekable_open(node, filep);
1323 /* Close the IR device */
1324 static int close(struct inode *node, struct file *filep)
1326 /* find our IR struct */
1327 struct IR *ir = filep->private_data;
1329 zilog_error("close: no private_data attached to the file!\n");
1333 atomic_dec(&ir->open_count);
1335 put_ir_device(ir, false);
1339 static int ir_remove(struct i2c_client *client);
1340 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1342 #define ID_FLAG_TX 0x01
1343 #define ID_FLAG_HDPVR 0x02
1345 static const struct i2c_device_id ir_transceiver_id[] = {
1346 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1347 { "ir_rx_z8f0811_haup", 0 },
1348 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1349 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1353 static struct i2c_driver driver = {
1355 .owner = THIS_MODULE,
1356 .name = "Zilog/Hauppauge i2c IR",
1359 .remove = ir_remove,
1360 .id_table = ir_transceiver_id,
1363 static const struct file_operations lirc_fops = {
1364 .owner = THIS_MODULE,
1365 .llseek = no_llseek,
1369 .unlocked_ioctl = ioctl,
1370 #ifdef CONFIG_COMPAT
1371 .compat_ioctl = ioctl,
1377 static struct lirc_driver lirc_template = {
1378 .name = "lirc_zilog",
1381 .buffer_size = BUFLEN / 2,
1382 .sample_rate = 0, /* tell lirc_dev to not start its own kthread */
1384 .set_use_inc = set_use_inc,
1385 .set_use_dec = set_use_dec,
1387 .owner = THIS_MODULE,
1390 static int ir_remove(struct i2c_client *client)
1392 if (strncmp("ir_tx_z8", client->name, 8) == 0) {
1393 struct IR_tx *tx = i2c_get_clientdata(client);
1395 mutex_lock(&tx->client_lock);
1397 mutex_unlock(&tx->client_lock);
1398 put_ir_tx(tx, false);
1400 } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
1401 struct IR_rx *rx = i2c_get_clientdata(client);
1403 mutex_lock(&rx->client_lock);
1405 mutex_unlock(&rx->client_lock);
1406 put_ir_rx(rx, false);
1413 /* ir_devices_lock must be held */
1414 static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
1418 if (list_empty(&ir_devices_list))
1421 list_for_each_entry(ir, &ir_devices_list, list)
1422 if (ir->adapter == adapter) {
1423 get_ir_device(ir, true);
1430 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1435 struct i2c_adapter *adap = client->adapter;
1437 bool tx_probe = false;
1439 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1440 __func__, id->name, adap->nr, adap->name, client->addr);
1443 * The IR receiver is at i2c address 0x71.
1444 * The IR transmitter is at i2c address 0x70.
1447 if (id->driver_data & ID_FLAG_TX)
1449 else if (tx_only) /* module option */
1452 zilog_info("probing IR %s on %s (i2c-%d)\n",
1453 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1455 mutex_lock(&ir_devices_lock);
1457 /* Use a single struct IR instance for both the Rx and Tx functions */
1458 ir = get_ir_device_by_adapter(adap);
1460 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1465 kref_init(&ir->ref);
1467 /* store for use in ir_probe() again, and open() later on */
1468 INIT_LIST_HEAD(&ir->list);
1469 list_add_tail(&ir->list, &ir_devices_list);
1472 mutex_init(&ir->ir_lock);
1473 atomic_set(&ir->open_count, 0);
1474 spin_lock_init(&ir->tx_ref_lock);
1475 spin_lock_init(&ir->rx_ref_lock);
1477 /* set lirc_dev stuff */
1478 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1480 * FIXME this is a pointer reference to us, but no refcount.
1482 * This OK for now, since lirc_dev currently won't touch this
1483 * buffer as we provide our own lirc_fops.
1485 * Currently our own lirc_fops rely on this ir->l.rbuf pointer
1487 ir->l.rbuf = &ir->rbuf;
1488 ir->l.dev = &adap->dev;
1489 ret = lirc_buffer_init(ir->l.rbuf,
1490 ir->l.chunk_size, ir->l.buffer_size);
1496 /* Get the IR_rx instance for later, if already allocated */
1499 /* Set up a struct IR_tx instance */
1500 tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1505 kref_init(&tx->ref);
1508 ir->l.features |= LIRC_CAN_SEND_PULSE;
1509 mutex_init(&tx->client_lock);
1512 tx->post_tx_ready_poll =
1513 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1515 /* An ir ref goes to the struct IR_tx instance */
1516 tx->ir = get_ir_device(ir, true);
1518 /* A tx ref goes to the i2c_client */
1519 i2c_set_clientdata(client, get_ir_tx(ir));
1522 * Load the 'firmware'. We do this before registering with
1523 * lirc_dev, so the first firmware load attempt does not happen
1524 * after a open() or write() call on the device.
1526 * Failure here is not deemed catastrophic, so the receiver will
1527 * still be usable. Firmware load will be retried in write(),
1532 /* Proceed only if the Rx client is also ready or not needed */
1533 if (rx == NULL && !tx_only) {
1534 zilog_info("probe of IR Tx on %s (i2c-%d) done. Waiting"
1535 " on IR Rx.\n", adap->name, adap->nr);
1539 /* Get the IR_tx instance for later, if already allocated */
1542 /* Set up a struct IR_rx instance */
1543 rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1548 kref_init(&rx->ref);
1551 ir->l.features |= LIRC_CAN_REC_LIRCCODE;
1552 mutex_init(&rx->client_lock);
1554 rx->hdpvr_data_fmt =
1555 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1557 /* An ir ref goes to the struct IR_rx instance */
1558 rx->ir = get_ir_device(ir, true);
1560 /* An rx ref goes to the i2c_client */
1561 i2c_set_clientdata(client, get_ir_rx(ir));
1564 * Start the polling thread.
1565 * It will only perform an empty loop around schedule_timeout()
1566 * until we register with lirc_dev and the first user open()
1568 /* An ir ref goes to the new rx polling kthread */
1569 rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
1570 "zilog-rx-i2c-%d", adap->nr);
1571 if (IS_ERR(rx->task)) {
1572 ret = PTR_ERR(rx->task);
1573 zilog_error("%s: could not start IR Rx polling thread"
1575 /* Failed kthread, so put back the ir ref */
1576 put_ir_device(ir, true);
1577 /* Failure exit, so put back rx ref from i2c_client */
1578 i2c_set_clientdata(client, NULL);
1579 put_ir_rx(rx, true);
1580 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
1584 /* Proceed only if the Tx client is also ready */
1586 zilog_info("probe of IR Rx on %s (i2c-%d) done. Waiting"
1587 " on IR Tx.\n", adap->name, adap->nr);
1592 /* register with lirc */
1593 ir->l.minor = minor; /* module option: user requested minor number */
1594 ir->l.minor = lirc_register_driver(&ir->l);
1595 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1596 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1597 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1601 zilog_info("IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
1602 adap->name, adap->nr, ir->l.minor);
1606 put_ir_rx(rx, true);
1608 put_ir_tx(tx, true);
1609 put_ir_device(ir, true);
1610 zilog_info("probe of IR %s on %s (i2c-%d) done\n",
1611 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1612 mutex_unlock(&ir_devices_lock);
1617 put_ir_rx(rx, true);
1619 put_ir_tx(tx, true);
1621 put_ir_device(ir, true);
1623 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1624 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1626 mutex_unlock(&ir_devices_lock);
1630 static int __init zilog_init(void)
1634 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1636 mutex_init(&tx_data_lock);
1638 request_module("firmware_class");
1640 ret = i2c_add_driver(&driver);
1642 zilog_error("initialization failed\n");
1644 zilog_notify("initialization complete\n");
1649 static void __exit zilog_exit(void)
1651 i2c_del_driver(&driver);
1654 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1657 module_init(zilog_init);
1658 module_exit(zilog_exit);
1660 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1661 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1662 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1664 MODULE_LICENSE("GPL");
1665 /* for compat with old name, which isn't all that accurate anymore */
1666 MODULE_ALIAS("lirc_pvr150");
1668 module_param(minor, int, 0444);
1669 MODULE_PARM_DESC(minor, "Preferred minor device number");
1671 module_param(debug, bool, 0644);
1672 MODULE_PARM_DESC(debug, "Enable debugging messages");
1674 module_param(tx_only, bool, 0644);
1675 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");