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/signal.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;
156 /* module parameters */
157 static bool debug; /* debug output */
158 static bool tx_only; /* only handle the IR Tx function */
161 /* struct IR reference counting */
162 static struct IR *get_ir_device(struct IR *ir, bool ir_devices_lock_held)
164 if (ir_devices_lock_held) {
167 mutex_lock(&ir_devices_lock);
169 mutex_unlock(&ir_devices_lock);
174 static void release_ir_device(struct kref *ref)
176 struct IR *ir = container_of(ref, struct IR, ref);
179 * Things should be in this state by now:
180 * ir->rx set to NULL and deallocated - happens before ir->rx->ir put()
181 * ir->rx->task kthread stopped - happens before ir->rx->ir put()
182 * ir->tx set to NULL and deallocated - happens before ir->tx->ir put()
183 * ir->open_count == 0 - happens on final close()
184 * ir_lock, tx_ref_lock, rx_ref_lock, all released
186 if (ir->l.minor >= 0) {
187 lirc_unregister_driver(ir->l.minor);
191 if (kfifo_initialized(&ir->rbuf.fifo))
192 lirc_buffer_free(&ir->rbuf);
197 static int put_ir_device(struct IR *ir, bool ir_devices_lock_held)
201 if (ir_devices_lock_held)
202 return kref_put(&ir->ref, release_ir_device);
204 mutex_lock(&ir_devices_lock);
205 released = kref_put(&ir->ref, release_ir_device);
206 mutex_unlock(&ir_devices_lock);
211 /* struct IR_rx reference counting */
212 static struct IR_rx *get_ir_rx(struct IR *ir)
216 spin_lock(&ir->rx_ref_lock);
220 spin_unlock(&ir->rx_ref_lock);
224 static void destroy_rx_kthread(struct IR_rx *rx, bool ir_devices_lock_held)
226 /* end up polling thread */
227 if (!IS_ERR_OR_NULL(rx->task)) {
228 kthread_stop(rx->task);
230 /* Put the ir ptr that ir_probe() gave to the rx poll thread */
231 put_ir_device(rx->ir, ir_devices_lock_held);
235 static void release_ir_rx(struct kref *ref)
237 struct IR_rx *rx = container_of(ref, struct IR_rx, ref);
238 struct IR *ir = rx->ir;
241 * This release function can't do all the work, as we want
242 * to keep the rx_ref_lock a spinlock, and killing the poll thread
243 * and releasing the ir reference can cause a sleep. That work is
244 * performed by put_ir_rx()
246 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
247 /* Don't put_ir_device(rx->ir) here; lock can't be freed yet */
249 /* Don't do the kfree(rx) here; we still need to kill the poll thread */
252 static int put_ir_rx(struct IR_rx *rx, bool ir_devices_lock_held)
255 struct IR *ir = rx->ir;
257 spin_lock(&ir->rx_ref_lock);
258 released = kref_put(&rx->ref, release_ir_rx);
259 spin_unlock(&ir->rx_ref_lock);
260 /* Destroy the rx kthread while not holding the spinlock */
262 destroy_rx_kthread(rx, ir_devices_lock_held);
264 /* Make sure we're not still in a poll_table somewhere */
265 wake_up_interruptible(&ir->rbuf.wait_poll);
267 /* Do a reference put() for the rx->ir reference, if we released rx */
269 put_ir_device(ir, ir_devices_lock_held);
273 /* struct IR_tx reference counting */
274 static struct IR_tx *get_ir_tx(struct IR *ir)
278 spin_lock(&ir->tx_ref_lock);
282 spin_unlock(&ir->tx_ref_lock);
286 static void release_ir_tx(struct kref *ref)
288 struct IR_tx *tx = container_of(ref, struct IR_tx, ref);
289 struct IR *ir = tx->ir;
291 ir->l.features &= ~LIRC_CAN_SEND_PULSE;
292 /* Don't put_ir_device(tx->ir) here, so our lock doesn't get freed */
297 static int put_ir_tx(struct IR_tx *tx, bool ir_devices_lock_held)
300 struct IR *ir = tx->ir;
302 spin_lock(&ir->tx_ref_lock);
303 released = kref_put(&tx->ref, release_ir_tx);
304 spin_unlock(&ir->tx_ref_lock);
305 /* Do a reference put() for the tx->ir reference, if we released tx */
307 put_ir_device(ir, ir_devices_lock_held);
311 static int add_to_buf(struct IR *ir)
314 unsigned char codes[2];
315 unsigned char keybuf[6];
319 unsigned char sendbuf[1] = { 0 };
320 struct lirc_buffer *rbuf = ir->l.rbuf;
324 if (lirc_buffer_full(rbuf)) {
325 dev_dbg(ir->l.dev, "buffer overflow\n");
333 /* Ensure our rx->c i2c_client remains valid for the duration */
334 mutex_lock(&rx->client_lock);
336 mutex_unlock(&rx->client_lock);
337 put_ir_rx(rx, false);
344 * service the device as long as it is returning
345 * data and we have space
348 if (kthread_should_stop()) {
354 * Lock i2c bus for the duration. RX/TX chips interfere so
357 mutex_lock(&ir->ir_lock);
359 if (kthread_should_stop()) {
360 mutex_unlock(&ir->ir_lock);
366 * Send random "poll command" (?) Windows driver does this
367 * and it is a good point to detect chip failure.
369 ret = i2c_master_send(rx->c, sendbuf, 1);
371 dev_err(ir->l.dev, "i2c_master_send failed with %d\n",
374 mutex_unlock(&ir->ir_lock);
376 "unable to read from the IR chip after 3 resets, giving up\n");
380 /* Looks like the chip crashed, reset it */
382 "polling the IR receiver chip failed, trying reset\n");
384 set_current_state(TASK_UNINTERRUPTIBLE);
385 if (kthread_should_stop()) {
386 mutex_unlock(&ir->ir_lock);
390 schedule_timeout((100 * HZ + 999) / 1000);
395 mutex_unlock(&ir->ir_lock);
400 if (kthread_should_stop()) {
401 mutex_unlock(&ir->ir_lock);
405 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
406 mutex_unlock(&ir->ir_lock);
407 if (ret != sizeof(keybuf)) {
409 "i2c_master_recv failed with %d -- keeping last read buffer\n",
412 rx->b[0] = keybuf[3];
413 rx->b[1] = keybuf[4];
414 rx->b[2] = keybuf[5];
416 "key (0x%02x/0x%02x)\n",
421 if (rx->hdpvr_data_fmt) {
422 if (got_data && (keybuf[0] == 0x80)) {
425 } else if (got_data && (keybuf[0] == 0x00)) {
429 } else if ((rx->b[0] & 0x80) == 0) {
430 ret = got_data ? 0 : -ENODATA;
434 /* look what we have */
435 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
437 codes[0] = (code >> 8) & 0xff;
438 codes[1] = code & 0xff;
441 lirc_buffer_write(rbuf, codes);
444 } while (!lirc_buffer_full(rbuf));
446 mutex_unlock(&rx->client_lock);
448 put_ir_tx(tx, false);
449 put_ir_rx(rx, false);
454 * Main function of the polling thread -- from lirc_dev.
455 * We don't fit the LIRC model at all anymore. This is horrible, but
456 * basically we have a single RX/TX device with a nasty failure mode
457 * that needs to be accounted for across the pair. lirc lets us provide
458 * fops, but prevents us from using the internal polling, etc. if we do
459 * so. Hence the replication. Might be neater to extend the LIRC model
460 * to account for this but I'd think it's a very special case of seriously
461 * messed up hardware.
463 static int lirc_thread(void *arg)
466 struct lirc_buffer *rbuf = ir->l.rbuf;
468 dev_dbg(ir->l.dev, "poll thread started\n");
470 while (!kthread_should_stop()) {
471 set_current_state(TASK_INTERRUPTIBLE);
473 /* if device not opened, we can sleep half a second */
474 if (atomic_read(&ir->open_count) == 0) {
475 schedule_timeout(HZ / 2);
480 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
481 * We use this interval as the chip resets every time you poll
482 * it (bad!). This is therefore just sufficient to catch all
483 * of the button presses. It makes the remote much more
484 * responsive. You can see the difference by running irw and
485 * holding down a button. With 100ms, the old polling
486 * interval, you'll notice breaks in the repeat sequence
487 * corresponding to lost keypresses.
489 schedule_timeout((260 * HZ) / 1000);
490 if (kthread_should_stop())
493 wake_up_interruptible(&rbuf->wait_poll);
496 dev_dbg(ir->l.dev, "poll thread ended\n");
500 /* safe read of a uint32 (always network byte order) */
501 static int read_uint32(unsigned char **data,
502 unsigned char *endp, unsigned int *val)
504 if (*data + 4 > endp)
506 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
507 ((*data)[2] << 8) | (*data)[3];
512 /* safe read of a uint8 */
513 static int read_uint8(unsigned char **data,
514 unsigned char *endp, unsigned char *val)
516 if (*data + 1 > endp)
522 /* safe skipping of N bytes */
523 static int skip(unsigned char **data,
524 unsigned char *endp, unsigned int distance)
526 if (*data + distance > endp)
532 /* decompress key data into the given buffer */
533 static int get_key_data(unsigned char *buf,
534 unsigned int codeset, unsigned int key)
536 unsigned char *data, *endp, *diffs, *key_block;
537 unsigned char keys, ndiffs, id;
538 unsigned int base, lim, pos, i;
540 /* Binary search for the codeset */
541 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
542 pos = base + (lim >> 1);
543 data = tx_data->code_sets[pos];
545 if (!read_uint32(&data, tx_data->endp, &i))
550 } else if (codeset > i) {
559 /* Set end of data block */
560 endp = pos < tx_data->num_code_sets - 1 ?
561 tx_data->code_sets[pos + 1] : tx_data->endp;
563 /* Read the block header */
564 if (!read_uint8(&data, endp, &keys) ||
565 !read_uint8(&data, endp, &ndiffs) ||
566 ndiffs > TX_BLOCK_SIZE || keys == 0)
569 /* Save diffs & skip */
571 if (!skip(&data, endp, ndiffs))
574 /* Read the id of the first key */
575 if (!read_uint8(&data, endp, &id))
578 /* Unpack the first key's data */
579 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
580 if (tx_data->fixed[i] == -1) {
581 if (!read_uint8(&data, endp, &buf[i]))
584 buf[i] = (unsigned char)tx_data->fixed[i];
588 /* Early out key found/not found */
596 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
599 /* Binary search for the key */
600 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
602 unsigned char *key_data;
604 pos = base + (lim >> 1);
605 key_data = key_block + (ndiffs + 1) * pos;
607 if (*key_data == key) {
611 /* found, so unpack the diffs */
612 for (i = 0; i < ndiffs; ++i) {
615 if (!read_uint8(&key_data, endp, &val) ||
616 diffs[i] >= TX_BLOCK_SIZE)
622 } else if (key > *key_data) {
631 pr_err("firmware is corrupt\n");
635 /* send a block of data to the IR TX device */
636 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
639 unsigned char buf[5];
641 for (i = 0; i < TX_BLOCK_SIZE;) {
642 int tosend = TX_BLOCK_SIZE - i;
646 buf[0] = (unsigned char)(i + 1);
647 for (j = 0; j < tosend; ++j)
648 buf[1 + j] = data_block[i + j];
649 dev_dbg(tx->ir->l.dev, "%*ph", 5, buf);
650 ret = i2c_master_send(tx->c, buf, tosend + 1);
651 if (ret != tosend + 1) {
652 dev_err(tx->ir->l.dev,
653 "i2c_master_send failed with %d\n", ret);
654 return ret < 0 ? ret : -EFAULT;
661 /* send boot data to the IR TX device */
662 static int send_boot_data(struct IR_tx *tx)
665 unsigned char buf[4];
667 /* send the boot block */
668 ret = send_data_block(tx, tx_data->boot_data);
672 /* Hit the go button to activate the new boot data */
675 ret = i2c_master_send(tx->c, buf, 2);
677 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
678 return ret < 0 ? ret : -EFAULT;
682 * Wait for zilog to settle after hitting go post boot block upload.
683 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
684 * upon attempting to get firmware revision, and tx probe thus fails.
686 for (i = 0; i < 10; i++) {
687 ret = i2c_master_send(tx->c, buf, 1);
694 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
695 return ret < 0 ? ret : -EFAULT;
698 /* Here comes the firmware version... (hopefully) */
699 ret = i2c_master_recv(tx->c, buf, 4);
701 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
704 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
705 dev_err(tx->ir->l.dev, "unexpected IR TX init response: %02x\n",
709 dev_notice(tx->ir->l.dev,
710 "Zilog/Hauppauge IR blaster firmware version %d.%d.%d loaded\n",
711 buf[1], buf[2], buf[3]);
716 /* unload "firmware", lock held */
717 static void fw_unload_locked(void)
720 vfree(tx_data->code_sets);
722 vfree(tx_data->datap);
726 pr_debug("successfully unloaded IR blaster firmware\n");
730 /* unload "firmware" for the IR TX device */
731 static void fw_unload(void)
733 mutex_lock(&tx_data_lock);
735 mutex_unlock(&tx_data_lock);
738 /* load "firmware" for the IR TX device */
739 static int fw_load(struct IR_tx *tx)
743 unsigned char *data, version, num_global_fixed;
744 const struct firmware *fw_entry;
746 /* Already loaded? */
747 mutex_lock(&tx_data_lock);
753 /* Request codeset data file */
754 ret = reject_firmware(&fw_entry, "/*(DEBLOBBED)*/", tx->ir->l.dev);
756 dev_err(tx->ir->l.dev,
757 "firmware /*(DEBLOBBED)*/ not available (%d)\n",
759 ret = ret < 0 ? ret : -EFAULT;
762 dev_dbg(tx->ir->l.dev, "firmware of size %zu loaded\n", fw_entry->size);
765 tx_data = vmalloc(sizeof(*tx_data));
767 release_firmware(fw_entry);
771 tx_data->code_sets = NULL;
773 /* Copy the data so hotplug doesn't get confused and timeout */
774 tx_data->datap = vmalloc(fw_entry->size);
775 if (!tx_data->datap) {
776 release_firmware(fw_entry);
781 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
782 tx_data->endp = tx_data->datap + fw_entry->size;
783 release_firmware(fw_entry); fw_entry = NULL;
786 data = tx_data->datap;
787 if (!read_uint8(&data, tx_data->endp, &version))
790 dev_err(tx->ir->l.dev,
791 "unsupported code set file version (%u, expected 1) -- please upgrade to a newer driver\n",
798 /* Save boot block for later */
799 tx_data->boot_data = data;
800 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
803 if (!read_uint32(&data, tx_data->endp,
804 &tx_data->num_code_sets))
807 dev_dbg(tx->ir->l.dev, "%u IR blaster codesets loaded\n",
808 tx_data->num_code_sets);
810 tx_data->code_sets = vmalloc(
811 tx_data->num_code_sets * sizeof(char *));
812 if (!tx_data->code_sets) {
818 for (i = 0; i < TX_BLOCK_SIZE; ++i)
819 tx_data->fixed[i] = -1;
821 /* Read global fixed data template */
822 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
823 num_global_fixed > TX_BLOCK_SIZE)
825 for (i = 0; i < num_global_fixed; ++i) {
826 unsigned char pos, val;
828 if (!read_uint8(&data, tx_data->endp, &pos) ||
829 !read_uint8(&data, tx_data->endp, &val) ||
830 pos >= TX_BLOCK_SIZE)
832 tx_data->fixed[pos] = (int)val;
835 /* Filch out the position of each code set */
836 for (i = 0; i < tx_data->num_code_sets; ++i) {
839 unsigned char ndiffs;
841 /* Save the codeset position */
842 tx_data->code_sets[i] = data;
845 if (!read_uint32(&data, tx_data->endp, &id) ||
846 !read_uint8(&data, tx_data->endp, &keys) ||
847 !read_uint8(&data, tx_data->endp, &ndiffs) ||
848 ndiffs > TX_BLOCK_SIZE || keys == 0)
851 /* skip diff positions */
852 if (!skip(&data, tx_data->endp, ndiffs))
856 * After the diffs we have the first key id + data -
859 if (!skip(&data, tx_data->endp,
860 1 + TX_BLOCK_SIZE - num_global_fixed))
863 /* Then we have keys-1 blocks of key id+diffs */
864 if (!skip(&data, tx_data->endp,
865 (ndiffs + 1) * (keys - 1)))
872 dev_err(tx->ir->l.dev, "firmware is corrupt\n");
877 mutex_unlock(&tx_data_lock);
881 /* copied from lirc_dev */
882 static ssize_t read(struct file *filep, char __user *outbuf, size_t n,
885 struct IR *ir = filep->private_data;
887 struct lirc_buffer *rbuf = ir->l.rbuf;
888 int ret = 0, written = 0, retries = 0;
890 DECLARE_WAITQUEUE(wait, current);
892 dev_dbg(ir->l.dev, "read called\n");
893 if (n % rbuf->chunk_size) {
894 dev_dbg(ir->l.dev, "read result = -EINVAL\n");
903 * we add ourselves to the task queue before buffer check
904 * to avoid losing scan code (in case when queue is awaken somewhere
905 * between while condition checking and scheduling)
907 add_wait_queue(&rbuf->wait_poll, &wait);
908 set_current_state(TASK_INTERRUPTIBLE);
911 * while we didn't provide 'length' bytes, device is opened in blocking
912 * mode and 'copy_to_user' is happy, wait for data.
914 while (written < n && ret == 0) {
915 if (lirc_buffer_empty(rbuf)) {
917 * According to the read(2) man page, 'written' can be
918 * returned as less than 'n', instead of blocking
919 * again, returning -EWOULDBLOCK, or returning
924 if (filep->f_flags & O_NONBLOCK) {
928 if (signal_pending(current)) {
933 set_current_state(TASK_INTERRUPTIBLE);
935 unsigned char buf[MAX_XFER_SIZE];
937 if (rbuf->chunk_size > sizeof(buf)) {
939 "chunk_size is too big (%d)!\n",
944 m = lirc_buffer_read(rbuf, buf);
945 if (m == rbuf->chunk_size) {
946 ret = copy_to_user(outbuf + written, buf,
948 written += rbuf->chunk_size;
953 dev_err(ir->l.dev, "Buffer read failed!\n");
959 remove_wait_queue(&rbuf->wait_poll, &wait);
960 put_ir_rx(rx, false);
961 set_current_state(TASK_RUNNING);
963 dev_dbg(ir->l.dev, "read result = %d (%s)\n", ret,
964 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 dev_err(tx->ir->l.dev,
981 "failed to get data for code %u, key %u -- check lircd.conf entries\n",
984 } else if (ret != 0) {
988 /* Send the data block */
989 ret = send_data_block(tx, data_block);
993 /* Send data block length? */
996 ret = i2c_master_send(tx->c, buf, 2);
998 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
999 return ret < 0 ? ret : -EFAULT;
1002 /* Give the z8 a moment to process data block */
1003 for (i = 0; i < 10; i++) {
1004 ret = i2c_master_send(tx->c, buf, 1);
1011 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
1012 return ret < 0 ? ret : -EFAULT;
1015 /* Send finished download? */
1016 ret = i2c_master_recv(tx->c, buf, 1);
1018 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
1019 return ret < 0 ? ret : -EFAULT;
1021 if (buf[0] != 0xA0) {
1022 dev_err(tx->ir->l.dev, "unexpected IR TX response #1: %02x\n",
1027 /* Send prepare command? */
1030 ret = i2c_master_send(tx->c, buf, 2);
1032 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
1033 return ret < 0 ? ret : -EFAULT;
1037 * The sleep bits aren't necessary on the HD PVR, and in fact, the
1038 * last i2c_master_recv always fails with a -5, so for now, we're
1039 * going to skip this whole mess and say we're done on the HD PVR
1041 if (!tx->post_tx_ready_poll) {
1042 dev_dbg(tx->ir->l.dev, "sent code %u, key %u\n", code, key);
1047 * This bit NAKs until the device is ready, so we retry it
1048 * sleeping a bit each time. This seems to be what the windows
1049 * driver does, approximately.
1052 for (i = 0; i < 20; ++i) {
1053 set_current_state(TASK_UNINTERRUPTIBLE);
1054 schedule_timeout((50 * HZ + 999) / 1000);
1055 ret = i2c_master_send(tx->c, buf, 1);
1058 dev_dbg(tx->ir->l.dev,
1059 "NAK expected: i2c_master_send failed with %d (try %d)\n",
1063 dev_err(tx->ir->l.dev,
1064 "IR TX chip never got ready: last i2c_master_send failed with %d\n",
1066 return ret < 0 ? ret : -EFAULT;
1069 /* Seems to be an 'ok' response */
1070 i = i2c_master_recv(tx->c, buf, 1);
1072 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
1075 if (buf[0] != 0x80) {
1076 dev_err(tx->ir->l.dev, "unexpected IR TX response #2: %02x\n",
1081 /* Oh good, it worked */
1082 dev_dbg(tx->ir->l.dev, "sent code %u, key %u\n", code, key);
1087 * Write a code to the device. We take in a 32-bit number (an int) and then
1088 * decode this to a codeset/key index. The key data is then decompressed and
1089 * sent to the device. We have a spin lock as per i2c documentation to prevent
1090 * multiple concurrent sends which would probably cause the device to explode.
1092 static ssize_t write(struct file *filep, const char __user *buf, size_t n,
1095 struct IR *ir = filep->private_data;
1100 /* Validate user parameters */
1101 if (n % sizeof(int))
1104 /* Get a struct IR_tx reference */
1109 /* Ensure our tx->c i2c_client remains valid for the duration */
1110 mutex_lock(&tx->client_lock);
1112 mutex_unlock(&tx->client_lock);
1113 put_ir_tx(tx, false);
1117 /* Lock i2c bus for the duration */
1118 mutex_lock(&ir->ir_lock);
1120 /* Send each keypress */
1121 for (i = 0; i < n;) {
1125 if (copy_from_user(&command, buf + i, sizeof(command))) {
1126 mutex_unlock(&ir->ir_lock);
1127 mutex_unlock(&tx->client_lock);
1128 put_ir_tx(tx, false);
1132 /* Send boot data first if required */
1133 if (tx->need_boot == 1) {
1134 /* Make sure we have the 'firmware' loaded, first */
1137 mutex_unlock(&ir->ir_lock);
1138 mutex_unlock(&tx->client_lock);
1139 put_ir_tx(tx, false);
1144 /* Prep the chip for transmitting codes */
1145 ret = send_boot_data(tx);
1152 ret = send_code(tx, (unsigned int)command >> 16,
1153 (unsigned int)command & 0xFFFF);
1154 if (ret == -EPROTO) {
1155 mutex_unlock(&ir->ir_lock);
1156 mutex_unlock(&tx->client_lock);
1157 put_ir_tx(tx, false);
1163 * Hmm, a failure. If we've had a few then give up, otherwise
1167 /* Looks like the chip crashed, reset it */
1168 dev_err(tx->ir->l.dev,
1169 "sending to the IR transmitter chip failed, trying reset\n");
1171 if (failures >= 3) {
1172 dev_err(tx->ir->l.dev,
1173 "unable to send to the IR chip after 3 resets, giving up\n");
1174 mutex_unlock(&ir->ir_lock);
1175 mutex_unlock(&tx->client_lock);
1176 put_ir_tx(tx, false);
1179 set_current_state(TASK_UNINTERRUPTIBLE);
1180 schedule_timeout((100 * HZ + 999) / 1000);
1188 /* Release i2c bus */
1189 mutex_unlock(&ir->ir_lock);
1191 mutex_unlock(&tx->client_lock);
1193 /* Give back our struct IR_tx reference */
1194 put_ir_tx(tx, false);
1196 /* All looks good */
1200 /* copied from lirc_dev */
1201 static unsigned int poll(struct file *filep, poll_table *wait)
1203 struct IR *ir = filep->private_data;
1205 struct lirc_buffer *rbuf = ir->l.rbuf;
1208 dev_dbg(ir->l.dev, "%s called\n", __func__);
1213 * Revisit this, if our poll function ever reports writeable
1216 dev_dbg(ir->l.dev, "%s result = POLLERR\n", __func__);
1221 * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
1222 * that buffer's wait queue indicates we may have a new poll status.
1224 poll_wait(filep, &rbuf->wait_poll, wait);
1226 /* Indicate what ops could happen immediately without blocking */
1227 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN | POLLRDNORM);
1229 dev_dbg(ir->l.dev, "%s result = %s\n", __func__,
1230 ret ? "POLLIN|POLLRDNORM" : "none");
1231 put_ir_rx(rx, false);
1235 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1237 struct IR *ir = filep->private_data;
1238 unsigned long __user *uptr = (unsigned long __user *)arg;
1240 unsigned long mode, features;
1242 features = ir->l.features;
1245 case LIRC_GET_LENGTH:
1246 result = put_user(13UL, uptr);
1248 case LIRC_GET_FEATURES:
1249 result = put_user(features, uptr);
1251 case LIRC_GET_REC_MODE:
1252 if (!(features & LIRC_CAN_REC_MASK))
1255 result = put_user(LIRC_REC2MODE
1256 (features & LIRC_CAN_REC_MASK),
1259 case LIRC_SET_REC_MODE:
1260 if (!(features & LIRC_CAN_REC_MASK))
1263 result = get_user(mode, uptr);
1264 if (!result && !(LIRC_MODE2REC(mode) & features))
1267 case LIRC_GET_SEND_MODE:
1268 if (!(features & LIRC_CAN_SEND_MASK))
1271 result = put_user(LIRC_MODE_PULSE, uptr);
1273 case LIRC_SET_SEND_MODE:
1274 if (!(features & LIRC_CAN_SEND_MASK))
1277 result = get_user(mode, uptr);
1278 if (!result && mode != LIRC_MODE_PULSE)
1287 static struct IR *get_ir_device_by_minor(unsigned int minor)
1290 struct IR *ret = NULL;
1292 mutex_lock(&ir_devices_lock);
1294 if (!list_empty(&ir_devices_list)) {
1295 list_for_each_entry(ir, &ir_devices_list, list) {
1296 if (ir->l.minor == minor) {
1297 ret = get_ir_device(ir, true);
1303 mutex_unlock(&ir_devices_lock);
1308 * Open the IR device. Get hold of our IR structure and
1309 * stash it in private_data for the file
1311 static int open(struct inode *node, struct file *filep)
1314 unsigned int minor = MINOR(node->i_rdev);
1316 /* find our IR struct */
1317 ir = get_ir_device_by_minor(minor);
1322 atomic_inc(&ir->open_count);
1324 /* stash our IR struct */
1325 filep->private_data = ir;
1327 nonseekable_open(node, filep);
1331 /* Close the IR device */
1332 static int close(struct inode *node, struct file *filep)
1334 /* find our IR struct */
1335 struct IR *ir = filep->private_data;
1338 pr_err("ir: %s: no private_data attached to the file!\n",
1343 atomic_dec(&ir->open_count);
1345 put_ir_device(ir, false);
1349 static int ir_remove(struct i2c_client *client);
1350 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1352 #define ID_FLAG_TX 0x01
1353 #define ID_FLAG_HDPVR 0x02
1355 static const struct i2c_device_id ir_transceiver_id[] = {
1356 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1357 { "ir_rx_z8f0811_haup", 0 },
1358 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1359 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1362 MODULE_DEVICE_TABLE(i2c, ir_transceiver_id);
1364 static struct i2c_driver driver = {
1366 .name = "Zilog/Hauppauge i2c IR",
1369 .remove = ir_remove,
1370 .id_table = ir_transceiver_id,
1373 static const struct file_operations lirc_fops = {
1374 .owner = THIS_MODULE,
1375 .llseek = no_llseek,
1379 .unlocked_ioctl = ioctl,
1380 #ifdef CONFIG_COMPAT
1381 .compat_ioctl = ioctl,
1387 static struct lirc_driver lirc_template = {
1388 .name = "lirc_zilog",
1391 .buffer_size = BUFLEN / 2,
1394 .owner = THIS_MODULE,
1397 static int ir_remove(struct i2c_client *client)
1399 if (strncmp("ir_tx_z8", client->name, 8) == 0) {
1400 struct IR_tx *tx = i2c_get_clientdata(client);
1403 mutex_lock(&tx->client_lock);
1405 mutex_unlock(&tx->client_lock);
1406 put_ir_tx(tx, false);
1408 } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
1409 struct IR_rx *rx = i2c_get_clientdata(client);
1412 mutex_lock(&rx->client_lock);
1414 mutex_unlock(&rx->client_lock);
1415 put_ir_rx(rx, false);
1421 /* ir_devices_lock must be held */
1422 static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
1426 if (list_empty(&ir_devices_list))
1429 list_for_each_entry(ir, &ir_devices_list, list)
1430 if (ir->adapter == adapter) {
1431 get_ir_device(ir, true);
1438 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1443 struct i2c_adapter *adap = client->adapter;
1445 bool tx_probe = false;
1447 dev_dbg(&client->dev, "%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1448 __func__, id->name, adap->nr, adap->name, client->addr);
1451 * The IR receiver is at i2c address 0x71.
1452 * The IR transmitter is at i2c address 0x70.
1455 if (id->driver_data & ID_FLAG_TX)
1457 else if (tx_only) /* module option */
1460 pr_info("probing IR %s on %s (i2c-%d)\n",
1461 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1463 mutex_lock(&ir_devices_lock);
1465 /* Use a single struct IR instance for both the Rx and Tx functions */
1466 ir = get_ir_device_by_adapter(adap);
1468 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
1473 kref_init(&ir->ref);
1475 /* store for use in ir_probe() again, and open() later on */
1476 INIT_LIST_HEAD(&ir->list);
1477 list_add_tail(&ir->list, &ir_devices_list);
1480 mutex_init(&ir->ir_lock);
1481 atomic_set(&ir->open_count, 0);
1482 spin_lock_init(&ir->tx_ref_lock);
1483 spin_lock_init(&ir->rx_ref_lock);
1485 /* set lirc_dev stuff */
1486 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1488 * FIXME this is a pointer reference to us, but no refcount.
1490 * This OK for now, since lirc_dev currently won't touch this
1491 * buffer as we provide our own lirc_fops.
1493 * Currently our own lirc_fops rely on this ir->l.rbuf pointer
1495 ir->l.rbuf = &ir->rbuf;
1496 ir->l.dev = &adap->dev;
1497 ret = lirc_buffer_init(ir->l.rbuf,
1498 ir->l.chunk_size, ir->l.buffer_size);
1504 /* Get the IR_rx instance for later, if already allocated */
1507 /* Set up a struct IR_tx instance */
1508 tx = kzalloc(sizeof(*tx), GFP_KERNEL);
1513 kref_init(&tx->ref);
1516 ir->l.features |= LIRC_CAN_SEND_PULSE;
1517 mutex_init(&tx->client_lock);
1520 tx->post_tx_ready_poll =
1521 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1523 /* An ir ref goes to the struct IR_tx instance */
1524 tx->ir = get_ir_device(ir, true);
1526 /* A tx ref goes to the i2c_client */
1527 i2c_set_clientdata(client, get_ir_tx(ir));
1530 * Load the 'firmware'. We do this before registering with
1531 * lirc_dev, so the first firmware load attempt does not happen
1532 * after a open() or write() call on the device.
1534 * Failure here is not deemed catastrophic, so the receiver will
1535 * still be usable. Firmware load will be retried in write(),
1540 /* Proceed only if the Rx client is also ready or not needed */
1541 if (!rx && !tx_only) {
1542 dev_info(tx->ir->l.dev,
1543 "probe of IR Tx on %s (i2c-%d) done. Waiting on IR Rx.\n",
1544 adap->name, adap->nr);
1548 /* Get the IR_tx instance for later, if already allocated */
1551 /* Set up a struct IR_rx instance */
1552 rx = kzalloc(sizeof(*rx), GFP_KERNEL);
1557 kref_init(&rx->ref);
1560 ir->l.features |= LIRC_CAN_REC_LIRCCODE;
1561 mutex_init(&rx->client_lock);
1563 rx->hdpvr_data_fmt =
1564 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1566 /* An ir ref goes to the struct IR_rx instance */
1567 rx->ir = get_ir_device(ir, true);
1569 /* An rx ref goes to the i2c_client */
1570 i2c_set_clientdata(client, get_ir_rx(ir));
1573 * Start the polling thread.
1574 * It will only perform an empty loop around schedule_timeout()
1575 * until we register with lirc_dev and the first user open()
1577 /* An ir ref goes to the new rx polling kthread */
1578 rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
1579 "zilog-rx-i2c-%d", adap->nr);
1580 if (IS_ERR(rx->task)) {
1581 ret = PTR_ERR(rx->task);
1582 dev_err(tx->ir->l.dev,
1583 "%s: could not start IR Rx polling thread\n",
1585 /* Failed kthread, so put back the ir ref */
1586 put_ir_device(ir, true);
1587 /* Failure exit, so put back rx ref from i2c_client */
1588 i2c_set_clientdata(client, NULL);
1589 put_ir_rx(rx, true);
1590 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
1594 /* Proceed only if the Tx client is also ready */
1596 pr_info("probe of IR Rx on %s (i2c-%d) done. Waiting on IR Tx.\n",
1597 adap->name, adap->nr);
1602 /* register with lirc */
1603 ir->l.minor = lirc_register_driver(&ir->l);
1604 if (ir->l.minor < 0) {
1605 dev_err(tx->ir->l.dev,
1606 "%s: lirc_register_driver() failed: %i\n",
1607 __func__, ir->l.minor);
1612 "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);
1622 "probe of IR %s on %s (i2c-%d) done\n",
1623 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1624 mutex_unlock(&ir_devices_lock);
1629 put_ir_rx(rx, true);
1632 put_ir_tx(tx, true);
1634 put_ir_device(ir, true);
1636 dev_err(&client->dev,
1637 "%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1638 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr, ret);
1639 mutex_unlock(&ir_devices_lock);
1643 static int __init zilog_init(void)
1647 pr_notice("Zilog/Hauppauge IR driver initializing\n");
1649 mutex_init(&tx_data_lock);
1651 request_module("firmware_class");
1653 ret = i2c_add_driver(&driver);
1655 pr_err("initialization failed\n");
1657 pr_notice("initialization complete\n");
1662 static void __exit zilog_exit(void)
1664 i2c_del_driver(&driver);
1667 pr_notice("Zilog/Hauppauge IR driver unloaded\n");
1670 module_init(zilog_init);
1671 module_exit(zilog_exit);
1673 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1674 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, Andy Walls");
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(debug, bool, 0644);
1680 MODULE_PARM_DESC(debug, "Enable debugging messages");
1682 module_param(tx_only, bool, 0644);
1683 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");