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>
64 /* Max transfer size done by I2C transfer functions */
65 #define MAX_XFER_SIZE 64
74 struct mutex client_lock;
77 /* RX polling thread data */
78 struct task_struct *task;
90 struct mutex client_lock;
93 /* TX additional actions needed */
95 bool post_tx_ready_poll;
100 struct list_head list;
102 /* FIXME spinlock access to l.features */
103 struct lirc_driver l;
104 struct lirc_buffer rbuf;
106 struct mutex ir_lock;
109 struct i2c_adapter *adapter;
111 spinlock_t rx_ref_lock; /* struct IR_rx kref get()/put() */
114 spinlock_t tx_ref_lock; /* struct IR_tx kref get()/put() */
118 /* IR transceiver instance object list */
120 * This lock is used for the following:
121 * a. ir_devices_list access, insertions, deletions
122 * b. struct IR kref get()s and put()s
123 * c. serialization of ir_probe() for the two i2c_clients for a Z8
125 static DEFINE_MUTEX(ir_devices_lock);
126 static LIST_HEAD(ir_devices_list);
128 /* Block size for IR transmitter */
129 #define TX_BLOCK_SIZE 99
131 /* Hauppauge IR transmitter data */
132 struct tx_data_struct {
134 unsigned char *boot_data;
136 /* Start of binary data block */
137 unsigned char *datap;
139 /* End of binary data block */
142 /* Number of installed codesets */
143 unsigned int num_code_sets;
145 /* Pointers to codesets */
146 unsigned char **code_sets;
148 /* Global fixed data template */
149 int fixed[TX_BLOCK_SIZE];
152 static struct tx_data_struct *tx_data;
153 static struct mutex tx_data_lock;
155 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
157 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
158 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
160 /* module parameters */
161 static bool debug; /* debug output */
162 static bool tx_only; /* only handle the IR Tx function */
163 static int minor = -1; /* minor number */
165 #define dprintk(fmt, args...) \
168 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
173 /* struct IR reference counting */
174 static struct IR *get_ir_device(struct IR *ir, bool ir_devices_lock_held)
176 if (ir_devices_lock_held) {
179 mutex_lock(&ir_devices_lock);
181 mutex_unlock(&ir_devices_lock);
186 static void release_ir_device(struct kref *ref)
188 struct IR *ir = container_of(ref, struct IR, ref);
191 * Things should be in this state by now:
192 * ir->rx set to NULL and deallocated - happens before ir->rx->ir put()
193 * ir->rx->task kthread stopped - happens before ir->rx->ir put()
194 * ir->tx set to NULL and deallocated - happens before ir->tx->ir put()
195 * ir->open_count == 0 - happens on final close()
196 * ir_lock, tx_ref_lock, rx_ref_lock, all released
198 if (ir->l.minor >= 0 && ir->l.minor < MAX_IRCTL_DEVICES) {
199 lirc_unregister_driver(ir->l.minor);
200 ir->l.minor = MAX_IRCTL_DEVICES;
202 if (ir->rbuf.fifo_initialized)
203 lirc_buffer_free(&ir->rbuf);
208 static int put_ir_device(struct IR *ir, bool ir_devices_lock_held)
212 if (ir_devices_lock_held)
213 return kref_put(&ir->ref, release_ir_device);
215 mutex_lock(&ir_devices_lock);
216 released = kref_put(&ir->ref, release_ir_device);
217 mutex_unlock(&ir_devices_lock);
222 /* struct IR_rx reference counting */
223 static struct IR_rx *get_ir_rx(struct IR *ir)
227 spin_lock(&ir->rx_ref_lock);
231 spin_unlock(&ir->rx_ref_lock);
235 static void destroy_rx_kthread(struct IR_rx *rx, bool ir_devices_lock_held)
237 /* end up polling thread */
238 if (!IS_ERR_OR_NULL(rx->task)) {
239 kthread_stop(rx->task);
241 /* Put the ir ptr that ir_probe() gave to the rx poll thread */
242 put_ir_device(rx->ir, ir_devices_lock_held);
246 static void release_ir_rx(struct kref *ref)
248 struct IR_rx *rx = container_of(ref, struct IR_rx, ref);
249 struct IR *ir = rx->ir;
252 * This release function can't do all the work, as we want
253 * to keep the rx_ref_lock a spinlock, and killing the poll thread
254 * and releasing the ir reference can cause a sleep. That work is
255 * performed by put_ir_rx()
257 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
258 /* Don't put_ir_device(rx->ir) here; lock can't be freed yet */
260 /* Don't do the kfree(rx) here; we still need to kill the poll thread */
264 static int put_ir_rx(struct IR_rx *rx, bool ir_devices_lock_held)
267 struct IR *ir = rx->ir;
269 spin_lock(&ir->rx_ref_lock);
270 released = kref_put(&rx->ref, release_ir_rx);
271 spin_unlock(&ir->rx_ref_lock);
272 /* Destroy the rx kthread while not holding the spinlock */
274 destroy_rx_kthread(rx, ir_devices_lock_held);
276 /* Make sure we're not still in a poll_table somewhere */
277 wake_up_interruptible(&ir->rbuf.wait_poll);
279 /* Do a reference put() for the rx->ir reference, if we released rx */
281 put_ir_device(ir, ir_devices_lock_held);
285 /* struct IR_tx reference counting */
286 static struct IR_tx *get_ir_tx(struct IR *ir)
290 spin_lock(&ir->tx_ref_lock);
294 spin_unlock(&ir->tx_ref_lock);
298 static void release_ir_tx(struct kref *ref)
300 struct IR_tx *tx = container_of(ref, struct IR_tx, ref);
301 struct IR *ir = tx->ir;
303 ir->l.features &= ~LIRC_CAN_SEND_PULSE;
304 /* Don't put_ir_device(tx->ir) here, so our lock doesn't get freed */
309 static int put_ir_tx(struct IR_tx *tx, bool ir_devices_lock_held)
312 struct IR *ir = tx->ir;
314 spin_lock(&ir->tx_ref_lock);
315 released = kref_put(&tx->ref, release_ir_tx);
316 spin_unlock(&ir->tx_ref_lock);
317 /* Do a reference put() for the tx->ir reference, if we released tx */
319 put_ir_device(ir, ir_devices_lock_held);
323 static int add_to_buf(struct IR *ir)
326 unsigned char codes[2];
327 unsigned char keybuf[6];
331 unsigned char sendbuf[1] = { 0 };
332 struct lirc_buffer *rbuf = ir->l.rbuf;
336 if (lirc_buffer_full(rbuf)) {
337 dprintk("buffer overflow\n");
345 /* Ensure our rx->c i2c_client remains valid for the duration */
346 mutex_lock(&rx->client_lock);
348 mutex_unlock(&rx->client_lock);
349 put_ir_rx(rx, false);
356 * service the device as long as it is returning
357 * data and we have space
360 if (kthread_should_stop()) {
366 * Lock i2c bus for the duration. RX/TX chips interfere so
369 mutex_lock(&ir->ir_lock);
371 if (kthread_should_stop()) {
372 mutex_unlock(&ir->ir_lock);
378 * Send random "poll command" (?) Windows driver does this
379 * and it is a good point to detect chip failure.
381 ret = i2c_master_send(rx->c, sendbuf, 1);
383 zilog_error("i2c_master_send failed with %d\n", ret);
385 mutex_unlock(&ir->ir_lock);
386 zilog_error("unable to read from the IR chip "
387 "after 3 resets, giving up\n");
391 /* Looks like the chip crashed, reset it */
392 zilog_error("polling the IR receiver chip failed, "
395 set_current_state(TASK_UNINTERRUPTIBLE);
396 if (kthread_should_stop()) {
397 mutex_unlock(&ir->ir_lock);
401 schedule_timeout((100 * HZ + 999) / 1000);
406 mutex_unlock(&ir->ir_lock);
411 if (kthread_should_stop()) {
412 mutex_unlock(&ir->ir_lock);
416 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
417 mutex_unlock(&ir->ir_lock);
418 if (ret != sizeof(keybuf)) {
419 zilog_error("i2c_master_recv failed with %d -- "
420 "keeping last read buffer\n", ret);
422 rx->b[0] = keybuf[3];
423 rx->b[1] = keybuf[4];
424 rx->b[2] = keybuf[5];
425 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
429 if (rx->hdpvr_data_fmt) {
430 if (got_data && (keybuf[0] == 0x80)) {
433 } else if (got_data && (keybuf[0] == 0x00)) {
437 } else if ((rx->b[0] & 0x80) == 0) {
438 ret = got_data ? 0 : -ENODATA;
442 /* look what we have */
443 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
445 codes[0] = (code >> 8) & 0xff;
446 codes[1] = code & 0xff;
449 lirc_buffer_write(rbuf, codes);
452 } while (!lirc_buffer_full(rbuf));
454 mutex_unlock(&rx->client_lock);
456 put_ir_tx(tx, false);
457 put_ir_rx(rx, false);
462 * Main function of the polling thread -- from lirc_dev.
463 * We don't fit the LIRC model at all anymore. This is horrible, but
464 * basically we have a single RX/TX device with a nasty failure mode
465 * that needs to be accounted for across the pair. lirc lets us provide
466 * fops, but prevents us from using the internal polling, etc. if we do
467 * so. Hence the replication. Might be neater to extend the LIRC model
468 * to account for this but I'd think it's a very special case of seriously
469 * messed up hardware.
471 static int lirc_thread(void *arg)
474 struct lirc_buffer *rbuf = ir->l.rbuf;
476 dprintk("poll thread started\n");
478 while (!kthread_should_stop()) {
479 set_current_state(TASK_INTERRUPTIBLE);
481 /* if device not opened, we can sleep half a second */
482 if (atomic_read(&ir->open_count) == 0) {
483 schedule_timeout(HZ/2);
488 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
489 * We use this interval as the chip resets every time you poll
490 * it (bad!). This is therefore just sufficient to catch all
491 * of the button presses. It makes the remote much more
492 * responsive. You can see the difference by running irw and
493 * holding down a button. With 100ms, the old polling
494 * interval, you'll notice breaks in the repeat sequence
495 * corresponding to lost keypresses.
497 schedule_timeout((260 * HZ) / 1000);
498 if (kthread_should_stop())
501 wake_up_interruptible(&rbuf->wait_poll);
504 dprintk("poll thread ended\n");
508 static int set_use_inc(void *data)
513 static void set_use_dec(void *data)
518 /* safe read of a uint32 (always network byte order) */
519 static int read_uint32(unsigned char **data,
520 unsigned char *endp, unsigned int *val)
522 if (*data + 4 > endp)
524 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
525 ((*data)[2] << 8) | (*data)[3];
530 /* safe read of a uint8 */
531 static int read_uint8(unsigned char **data,
532 unsigned char *endp, unsigned char *val)
534 if (*data + 1 > endp)
540 /* safe skipping of N bytes */
541 static int skip(unsigned char **data,
542 unsigned char *endp, unsigned int distance)
544 if (*data + distance > endp)
550 /* decompress key data into the given buffer */
551 static int get_key_data(unsigned char *buf,
552 unsigned int codeset, unsigned int key)
554 unsigned char *data, *endp, *diffs, *key_block;
555 unsigned char keys, ndiffs, id;
556 unsigned int base, lim, pos, i;
558 /* Binary search for the codeset */
559 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
560 pos = base + (lim >> 1);
561 data = tx_data->code_sets[pos];
563 if (!read_uint32(&data, tx_data->endp, &i))
568 else if (codeset > i) {
577 /* Set end of data block */
578 endp = pos < tx_data->num_code_sets - 1 ?
579 tx_data->code_sets[pos + 1] : tx_data->endp;
581 /* Read the block header */
582 if (!read_uint8(&data, endp, &keys) ||
583 !read_uint8(&data, endp, &ndiffs) ||
584 ndiffs > TX_BLOCK_SIZE || keys == 0)
587 /* Save diffs & skip */
589 if (!skip(&data, endp, ndiffs))
592 /* Read the id of the first key */
593 if (!read_uint8(&data, endp, &id))
596 /* Unpack the first key's data */
597 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
598 if (tx_data->fixed[i] == -1) {
599 if (!read_uint8(&data, endp, &buf[i]))
602 buf[i] = (unsigned char)tx_data->fixed[i];
606 /* Early out key found/not found */
614 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
617 /* Binary search for the key */
618 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
620 unsigned char *key_data;
621 pos = base + (lim >> 1);
622 key_data = key_block + (ndiffs + 1) * pos;
624 if (*key_data == key) {
628 /* found, so unpack the diffs */
629 for (i = 0; i < ndiffs; ++i) {
631 if (!read_uint8(&key_data, endp, &val) ||
632 diffs[i] >= TX_BLOCK_SIZE)
638 } else if (key > *key_data) {
647 zilog_error("firmware is corrupt\n");
651 /* send a block of data to the IR TX device */
652 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
655 unsigned char buf[5];
657 for (i = 0; i < TX_BLOCK_SIZE;) {
658 int tosend = TX_BLOCK_SIZE - i;
661 buf[0] = (unsigned char)(i + 1);
662 for (j = 0; j < tosend; ++j)
663 buf[1 + j] = data_block[i + j];
664 dprintk("%*ph", 5, buf);
665 ret = i2c_master_send(tx->c, buf, tosend + 1);
666 if (ret != tosend + 1) {
667 zilog_error("i2c_master_send failed with %d\n", ret);
668 return ret < 0 ? ret : -EFAULT;
675 /* send boot data to the IR TX device */
676 static int send_boot_data(struct IR_tx *tx)
679 unsigned char buf[4];
681 /* send the boot block */
682 ret = send_data_block(tx, tx_data->boot_data);
686 /* Hit the go button to activate the new boot data */
689 ret = i2c_master_send(tx->c, buf, 2);
691 zilog_error("i2c_master_send failed with %d\n", ret);
692 return ret < 0 ? ret : -EFAULT;
696 * Wait for zilog to settle after hitting go post boot block upload.
697 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
698 * upon attempting to get firmware revision, and tx probe thus fails.
700 for (i = 0; i < 10; i++) {
701 ret = i2c_master_send(tx->c, buf, 1);
708 zilog_error("i2c_master_send failed with %d\n", ret);
709 return ret < 0 ? ret : -EFAULT;
712 /* Here comes the firmware version... (hopefully) */
713 ret = i2c_master_recv(tx->c, buf, 4);
715 zilog_error("i2c_master_recv failed with %d\n", ret);
718 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
719 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
722 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
723 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
728 /* unload "firmware", lock held */
729 static void fw_unload_locked(void)
732 if (tx_data->code_sets)
733 vfree(tx_data->code_sets);
736 vfree(tx_data->datap);
740 dprintk("successfully unloaded IR blaster firmware\n");
744 /* unload "firmware" for the IR TX device */
745 static void fw_unload(void)
747 mutex_lock(&tx_data_lock);
749 mutex_unlock(&tx_data_lock);
752 /* load "firmware" for the IR TX device */
753 static int fw_load(struct IR_tx *tx)
757 unsigned char *data, version, num_global_fixed;
758 const struct firmware *fw_entry;
760 /* Already loaded? */
761 mutex_lock(&tx_data_lock);
767 /* Request codeset data file */
768 ret = reject_firmware(&fw_entry, "/*(DEBLOBBED)*/", tx->ir->l.dev);
770 zilog_error("firmware /*(DEBLOBBED)*/ not available (%d)\n",
772 ret = ret < 0 ? ret : -EFAULT;
775 dprintk("firmware of size %zu loaded\n", fw_entry->size);
778 tx_data = vmalloc(sizeof(*tx_data));
779 if (tx_data == NULL) {
780 zilog_error("out of memory\n");
781 release_firmware(fw_entry);
785 tx_data->code_sets = NULL;
787 /* Copy the data so hotplug doesn't get confused and timeout */
788 tx_data->datap = vmalloc(fw_entry->size);
789 if (tx_data->datap == NULL) {
790 zilog_error("out of memory\n");
791 release_firmware(fw_entry);
796 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
797 tx_data->endp = tx_data->datap + fw_entry->size;
798 release_firmware(fw_entry); fw_entry = NULL;
801 data = tx_data->datap;
802 if (!read_uint8(&data, tx_data->endp, &version))
805 zilog_error("unsupported code set file version (%u, expected"
806 "1) -- please upgrade to a newer driver",
813 /* Save boot block for later */
814 tx_data->boot_data = data;
815 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
818 if (!read_uint32(&data, tx_data->endp,
819 &tx_data->num_code_sets))
822 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
824 tx_data->code_sets = vmalloc(
825 tx_data->num_code_sets * sizeof(char *));
826 if (tx_data->code_sets == NULL) {
832 for (i = 0; i < TX_BLOCK_SIZE; ++i)
833 tx_data->fixed[i] = -1;
835 /* Read global fixed data template */
836 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
837 num_global_fixed > TX_BLOCK_SIZE)
839 for (i = 0; i < num_global_fixed; ++i) {
840 unsigned char pos, val;
841 if (!read_uint8(&data, tx_data->endp, &pos) ||
842 !read_uint8(&data, tx_data->endp, &val) ||
843 pos >= TX_BLOCK_SIZE)
845 tx_data->fixed[pos] = (int)val;
848 /* Filch out the position of each code set */
849 for (i = 0; i < tx_data->num_code_sets; ++i) {
852 unsigned char ndiffs;
854 /* Save the codeset position */
855 tx_data->code_sets[i] = data;
858 if (!read_uint32(&data, tx_data->endp, &id) ||
859 !read_uint8(&data, tx_data->endp, &keys) ||
860 !read_uint8(&data, tx_data->endp, &ndiffs) ||
861 ndiffs > TX_BLOCK_SIZE || keys == 0)
864 /* skip diff positions */
865 if (!skip(&data, tx_data->endp, ndiffs))
869 * After the diffs we have the first key id + data -
872 if (!skip(&data, tx_data->endp,
873 1 + TX_BLOCK_SIZE - num_global_fixed))
876 /* Then we have keys-1 blocks of key id+diffs */
877 if (!skip(&data, tx_data->endp,
878 (ndiffs + 1) * (keys - 1)))
885 zilog_error("firmware is corrupt\n");
890 mutex_unlock(&tx_data_lock);
894 /* copied from lirc_dev */
895 static ssize_t read(struct file *filep, char __user *outbuf, size_t n,
898 struct IR *ir = filep->private_data;
900 struct lirc_buffer *rbuf = ir->l.rbuf;
901 int ret = 0, written = 0, retries = 0;
903 DECLARE_WAITQUEUE(wait, current);
905 dprintk("read called\n");
906 if (n % rbuf->chunk_size) {
907 dprintk("read result = -EINVAL\n");
916 * we add ourselves to the task queue before buffer check
917 * to avoid losing scan code (in case when queue is awaken somewhere
918 * between while condition checking and scheduling)
920 add_wait_queue(&rbuf->wait_poll, &wait);
921 set_current_state(TASK_INTERRUPTIBLE);
924 * while we didn't provide 'length' bytes, device is opened in blocking
925 * mode and 'copy_to_user' is happy, wait for data.
927 while (written < n && ret == 0) {
928 if (lirc_buffer_empty(rbuf)) {
930 * According to the read(2) man page, 'written' can be
931 * returned as less than 'n', instead of blocking
932 * again, returning -EWOULDBLOCK, or returning
937 if (filep->f_flags & O_NONBLOCK) {
941 if (signal_pending(current)) {
946 set_current_state(TASK_INTERRUPTIBLE);
948 unsigned char buf[MAX_XFER_SIZE];
950 if (rbuf->chunk_size > sizeof(buf)) {
951 zilog_error("chunk_size is too big (%d)!\n",
956 m = lirc_buffer_read(rbuf, buf);
957 if (m == rbuf->chunk_size) {
958 ret = copy_to_user(outbuf + written, buf,
960 written += rbuf->chunk_size;
965 zilog_error("Buffer read failed!\n");
971 remove_wait_queue(&rbuf->wait_poll, &wait);
972 put_ir_rx(rx, false);
973 set_current_state(TASK_RUNNING);
975 dprintk("read result = %d (%s)\n", ret, ret ? "Error" : "OK");
977 return ret ? ret : written;
980 /* send a keypress to the IR TX device */
981 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
983 unsigned char data_block[TX_BLOCK_SIZE];
984 unsigned char buf[2];
987 /* Get data for the codeset/key */
988 ret = get_key_data(data_block, code, key);
990 if (ret == -EPROTO) {
991 zilog_error("failed to get data for code %u, key %u -- check "
992 "lircd.conf entries\n", code, key);
997 /* Send the data block */
998 ret = send_data_block(tx, data_block);
1002 /* Send data block length? */
1005 ret = i2c_master_send(tx->c, buf, 2);
1007 zilog_error("i2c_master_send failed with %d\n", ret);
1008 return ret < 0 ? ret : -EFAULT;
1011 /* Give the z8 a moment to process data block */
1012 for (i = 0; i < 10; i++) {
1013 ret = i2c_master_send(tx->c, buf, 1);
1020 zilog_error("i2c_master_send failed with %d\n", ret);
1021 return ret < 0 ? ret : -EFAULT;
1024 /* Send finished download? */
1025 ret = i2c_master_recv(tx->c, buf, 1);
1027 zilog_error("i2c_master_recv failed with %d\n", ret);
1028 return ret < 0 ? ret : -EFAULT;
1030 if (buf[0] != 0xA0) {
1031 zilog_error("unexpected IR TX response #1: %02x\n",
1036 /* Send prepare command? */
1039 ret = i2c_master_send(tx->c, buf, 2);
1041 zilog_error("i2c_master_send failed with %d\n", ret);
1042 return ret < 0 ? ret : -EFAULT;
1046 * The sleep bits aren't necessary on the HD PVR, and in fact, the
1047 * last i2c_master_recv always fails with a -5, so for now, we're
1048 * going to skip this whole mess and say we're done on the HD PVR
1050 if (!tx->post_tx_ready_poll) {
1051 dprintk("sent code %u, key %u\n", code, key);
1056 * This bit NAKs until the device is ready, so we retry it
1057 * sleeping a bit each time. This seems to be what the windows
1058 * driver does, approximately.
1061 for (i = 0; i < 20; ++i) {
1062 set_current_state(TASK_UNINTERRUPTIBLE);
1063 schedule_timeout((50 * HZ + 999) / 1000);
1064 ret = i2c_master_send(tx->c, buf, 1);
1067 dprintk("NAK expected: i2c_master_send "
1068 "failed with %d (try %d)\n", ret, i+1);
1071 zilog_error("IR TX chip never got ready: last i2c_master_send "
1072 "failed with %d\n", ret);
1073 return ret < 0 ? ret : -EFAULT;
1076 /* Seems to be an 'ok' response */
1077 i = i2c_master_recv(tx->c, buf, 1);
1079 zilog_error("i2c_master_recv failed with %d\n", ret);
1082 if (buf[0] != 0x80) {
1083 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
1087 /* Oh good, it worked */
1088 dprintk("sent code %u, key %u\n", code, key);
1093 * Write a code to the device. We take in a 32-bit number (an int) and then
1094 * decode this to a codeset/key index. The key data is then decompressed and
1095 * sent to the device. We have a spin lock as per i2c documentation to prevent
1096 * multiple concurrent sends which would probably cause the device to explode.
1098 static ssize_t write(struct file *filep, const char __user *buf, size_t n,
1101 struct IR *ir = filep->private_data;
1106 /* Validate user parameters */
1107 if (n % sizeof(int))
1110 /* Get a struct IR_tx reference */
1115 /* Ensure our tx->c i2c_client remains valid for the duration */
1116 mutex_lock(&tx->client_lock);
1117 if (tx->c == NULL) {
1118 mutex_unlock(&tx->client_lock);
1119 put_ir_tx(tx, false);
1123 /* Lock i2c bus for the duration */
1124 mutex_lock(&ir->ir_lock);
1126 /* Send each keypress */
1127 for (i = 0; i < n;) {
1131 if (copy_from_user(&command, buf + i, sizeof(command))) {
1132 mutex_unlock(&ir->ir_lock);
1133 mutex_unlock(&tx->client_lock);
1134 put_ir_tx(tx, false);
1138 /* Send boot data first if required */
1139 if (tx->need_boot == 1) {
1140 /* Make sure we have the 'firmware' loaded, first */
1143 mutex_unlock(&ir->ir_lock);
1144 mutex_unlock(&tx->client_lock);
1145 put_ir_tx(tx, false);
1150 /* Prep the chip for transmitting codes */
1151 ret = send_boot_data(tx);
1158 ret = send_code(tx, (unsigned)command >> 16,
1159 (unsigned)command & 0xFFFF);
1160 if (ret == -EPROTO) {
1161 mutex_unlock(&ir->ir_lock);
1162 mutex_unlock(&tx->client_lock);
1163 put_ir_tx(tx, false);
1169 * Hmm, a failure. If we've had a few then give up, otherwise
1173 /* Looks like the chip crashed, reset it */
1174 zilog_error("sending to the IR transmitter chip "
1175 "failed, trying reset\n");
1177 if (failures >= 3) {
1178 zilog_error("unable to send to the IR chip "
1179 "after 3 resets, giving up\n");
1180 mutex_unlock(&ir->ir_lock);
1181 mutex_unlock(&tx->client_lock);
1182 put_ir_tx(tx, false);
1185 set_current_state(TASK_UNINTERRUPTIBLE);
1186 schedule_timeout((100 * HZ + 999) / 1000);
1193 /* Release i2c bus */
1194 mutex_unlock(&ir->ir_lock);
1196 mutex_unlock(&tx->client_lock);
1198 /* Give back our struct IR_tx reference */
1199 put_ir_tx(tx, false);
1201 /* All looks good */
1205 /* copied from lirc_dev */
1206 static unsigned int poll(struct file *filep, poll_table *wait)
1208 struct IR *ir = filep->private_data;
1210 struct lirc_buffer *rbuf = ir->l.rbuf;
1213 dprintk("poll called\n");
1218 * Revisit this, if our poll function ever reports writeable
1221 dprintk("poll result = POLLERR\n");
1226 * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
1227 * that buffer's wait queue indicates we may have a new poll status.
1229 poll_wait(filep, &rbuf->wait_poll, wait);
1231 /* Indicate what ops could happen immediately without blocking */
1232 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN|POLLRDNORM);
1234 dprintk("poll result = %s\n", ret ? "POLLIN|POLLRDNORM" : "none");
1238 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1240 struct IR *ir = filep->private_data;
1241 unsigned long __user *uptr = (unsigned long __user *)arg;
1243 unsigned long mode, features;
1245 features = ir->l.features;
1248 case LIRC_GET_LENGTH:
1249 result = put_user(13UL, uptr);
1251 case LIRC_GET_FEATURES:
1252 result = put_user(features, uptr);
1254 case LIRC_GET_REC_MODE:
1255 if (!(features&LIRC_CAN_REC_MASK))
1258 result = put_user(LIRC_REC2MODE
1259 (features&LIRC_CAN_REC_MASK),
1262 case LIRC_SET_REC_MODE:
1263 if (!(features&LIRC_CAN_REC_MASK))
1266 result = get_user(mode, uptr);
1267 if (!result && !(LIRC_MODE2REC(mode) & features))
1270 case LIRC_GET_SEND_MODE:
1271 if (!(features&LIRC_CAN_SEND_MASK))
1274 result = put_user(LIRC_MODE_PULSE, uptr);
1276 case LIRC_SET_SEND_MODE:
1277 if (!(features&LIRC_CAN_SEND_MASK))
1280 result = get_user(mode, uptr);
1281 if (!result && mode != LIRC_MODE_PULSE)
1290 static struct IR *get_ir_device_by_minor(unsigned int minor)
1293 struct IR *ret = NULL;
1295 mutex_lock(&ir_devices_lock);
1297 if (!list_empty(&ir_devices_list)) {
1298 list_for_each_entry(ir, &ir_devices_list, list) {
1299 if (ir->l.minor == minor) {
1300 ret = get_ir_device(ir, true);
1306 mutex_unlock(&ir_devices_lock);
1311 * Open the IR device. Get hold of our IR structure and
1312 * stash it in private_data for the file
1314 static int open(struct inode *node, struct file *filep)
1317 unsigned int minor = MINOR(node->i_rdev);
1319 /* find our IR struct */
1320 ir = get_ir_device_by_minor(minor);
1325 atomic_inc(&ir->open_count);
1327 /* stash our IR struct */
1328 filep->private_data = ir;
1330 nonseekable_open(node, filep);
1334 /* Close the IR device */
1335 static int close(struct inode *node, struct file *filep)
1337 /* find our IR struct */
1338 struct IR *ir = filep->private_data;
1340 zilog_error("close: no private_data attached to the file!\n");
1344 atomic_dec(&ir->open_count);
1346 put_ir_device(ir, false);
1350 static int ir_remove(struct i2c_client *client);
1351 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1353 #define ID_FLAG_TX 0x01
1354 #define ID_FLAG_HDPVR 0x02
1356 static const struct i2c_device_id ir_transceiver_id[] = {
1357 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1358 { "ir_rx_z8f0811_haup", 0 },
1359 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1360 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1364 static struct i2c_driver driver = {
1366 .owner = THIS_MODULE,
1367 .name = "Zilog/Hauppauge i2c IR",
1370 .remove = ir_remove,
1371 .id_table = ir_transceiver_id,
1374 static const struct file_operations lirc_fops = {
1375 .owner = THIS_MODULE,
1376 .llseek = no_llseek,
1380 .unlocked_ioctl = ioctl,
1381 #ifdef CONFIG_COMPAT
1382 .compat_ioctl = ioctl,
1388 static struct lirc_driver lirc_template = {
1389 .name = "lirc_zilog",
1392 .buffer_size = BUFLEN / 2,
1393 .sample_rate = 0, /* tell lirc_dev to not start its own kthread */
1395 .set_use_inc = set_use_inc,
1396 .set_use_dec = set_use_dec,
1398 .owner = THIS_MODULE,
1401 static int ir_remove(struct i2c_client *client)
1403 if (strncmp("ir_tx_z8", client->name, 8) == 0) {
1404 struct IR_tx *tx = i2c_get_clientdata(client);
1406 mutex_lock(&tx->client_lock);
1408 mutex_unlock(&tx->client_lock);
1409 put_ir_tx(tx, false);
1411 } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
1412 struct IR_rx *rx = i2c_get_clientdata(client);
1414 mutex_lock(&rx->client_lock);
1416 mutex_unlock(&rx->client_lock);
1417 put_ir_rx(rx, false);
1424 /* ir_devices_lock must be held */
1425 static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
1429 if (list_empty(&ir_devices_list))
1432 list_for_each_entry(ir, &ir_devices_list, list)
1433 if (ir->adapter == adapter) {
1434 get_ir_device(ir, true);
1441 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1446 struct i2c_adapter *adap = client->adapter;
1448 bool tx_probe = false;
1450 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1451 __func__, id->name, adap->nr, adap->name, client->addr);
1454 * The IR receiver is at i2c address 0x71.
1455 * The IR transmitter is at i2c address 0x70.
1458 if (id->driver_data & ID_FLAG_TX)
1460 else if (tx_only) /* module option */
1463 zilog_info("probing IR %s on %s (i2c-%d)\n",
1464 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1466 mutex_lock(&ir_devices_lock);
1468 /* Use a single struct IR instance for both the Rx and Tx functions */
1469 ir = get_ir_device_by_adapter(adap);
1471 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1476 kref_init(&ir->ref);
1478 /* store for use in ir_probe() again, and open() later on */
1479 INIT_LIST_HEAD(&ir->list);
1480 list_add_tail(&ir->list, &ir_devices_list);
1483 mutex_init(&ir->ir_lock);
1484 atomic_set(&ir->open_count, 0);
1485 spin_lock_init(&ir->tx_ref_lock);
1486 spin_lock_init(&ir->rx_ref_lock);
1488 /* set lirc_dev stuff */
1489 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1491 * FIXME this is a pointer reference to us, but no refcount.
1493 * This OK for now, since lirc_dev currently won't touch this
1494 * buffer as we provide our own lirc_fops.
1496 * Currently our own lirc_fops rely on this ir->l.rbuf pointer
1498 ir->l.rbuf = &ir->rbuf;
1499 ir->l.dev = &adap->dev;
1500 ret = lirc_buffer_init(ir->l.rbuf,
1501 ir->l.chunk_size, ir->l.buffer_size);
1507 /* Get the IR_rx instance for later, if already allocated */
1510 /* Set up a struct IR_tx instance */
1511 tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1516 kref_init(&tx->ref);
1519 ir->l.features |= LIRC_CAN_SEND_PULSE;
1520 mutex_init(&tx->client_lock);
1523 tx->post_tx_ready_poll =
1524 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1526 /* An ir ref goes to the struct IR_tx instance */
1527 tx->ir = get_ir_device(ir, true);
1529 /* A tx ref goes to the i2c_client */
1530 i2c_set_clientdata(client, get_ir_tx(ir));
1533 * Load the 'firmware'. We do this before registering with
1534 * lirc_dev, so the first firmware load attempt does not happen
1535 * after a open() or write() call on the device.
1537 * Failure here is not deemed catastrophic, so the receiver will
1538 * still be usable. Firmware load will be retried in write(),
1543 /* Proceed only if the Rx client is also ready or not needed */
1544 if (rx == NULL && !tx_only) {
1545 zilog_info("probe of IR Tx on %s (i2c-%d) done. Waiting"
1546 " on IR Rx.\n", adap->name, adap->nr);
1550 /* Get the IR_tx instance for later, if already allocated */
1553 /* Set up a struct IR_rx instance */
1554 rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1559 kref_init(&rx->ref);
1562 ir->l.features |= LIRC_CAN_REC_LIRCCODE;
1563 mutex_init(&rx->client_lock);
1565 rx->hdpvr_data_fmt =
1566 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1568 /* An ir ref goes to the struct IR_rx instance */
1569 rx->ir = get_ir_device(ir, true);
1571 /* An rx ref goes to the i2c_client */
1572 i2c_set_clientdata(client, get_ir_rx(ir));
1575 * Start the polling thread.
1576 * It will only perform an empty loop around schedule_timeout()
1577 * until we register with lirc_dev and the first user open()
1579 /* An ir ref goes to the new rx polling kthread */
1580 rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
1581 "zilog-rx-i2c-%d", adap->nr);
1582 if (IS_ERR(rx->task)) {
1583 ret = PTR_ERR(rx->task);
1584 zilog_error("%s: could not start IR Rx polling thread"
1586 /* Failed kthread, so put back the ir ref */
1587 put_ir_device(ir, true);
1588 /* Failure exit, so put back rx ref from i2c_client */
1589 i2c_set_clientdata(client, NULL);
1590 put_ir_rx(rx, true);
1591 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
1595 /* Proceed only if the Tx client is also ready */
1597 zilog_info("probe of IR Rx on %s (i2c-%d) done. Waiting"
1598 " on IR Tx.\n", adap->name, adap->nr);
1603 /* register with lirc */
1604 ir->l.minor = minor; /* module option: user requested minor number */
1605 ir->l.minor = lirc_register_driver(&ir->l);
1606 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1607 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1608 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1612 zilog_info("IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
1613 adap->name, adap->nr, ir->l.minor);
1617 put_ir_rx(rx, true);
1619 put_ir_tx(tx, true);
1620 put_ir_device(ir, true);
1621 zilog_info("probe of IR %s on %s (i2c-%d) done\n",
1622 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1623 mutex_unlock(&ir_devices_lock);
1628 put_ir_rx(rx, true);
1630 put_ir_tx(tx, true);
1632 put_ir_device(ir, true);
1634 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1635 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1637 mutex_unlock(&ir_devices_lock);
1641 static int __init zilog_init(void)
1645 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1647 mutex_init(&tx_data_lock);
1649 request_module("firmware_class");
1651 ret = i2c_add_driver(&driver);
1653 zilog_error("initialization failed\n");
1655 zilog_notify("initialization complete\n");
1660 static void __exit zilog_exit(void)
1662 i2c_del_driver(&driver);
1665 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1668 module_init(zilog_init);
1669 module_exit(zilog_exit);
1671 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1672 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1673 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1675 MODULE_LICENSE("GPL");
1676 /* for compat with old name, which isn't all that accurate anymore */
1677 MODULE_ALIAS("lirc_pvr150");
1679 module_param(minor, int, 0444);
1680 MODULE_PARM_DESC(minor, "Preferred minor device number");
1682 module_param(debug, bool, 0644);
1683 MODULE_PARM_DESC(debug, "Enable debugging messages");
1685 module_param(tx_only, bool, 0644);
1686 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");