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
43 #include <linux/version.h>
44 #include <linux/module.h>
45 #include <linux/kmod.h>
46 #include <linux/kernel.h>
47 #include <linux/sched.h>
49 #include <linux/poll.h>
50 #include <linux/string.h>
51 #include <linux/timer.h>
52 #include <linux/delay.h>
53 #include <linux/completion.h>
54 #include <linux/errno.h>
55 #include <linux/slab.h>
56 #include <linux/i2c.h>
57 #include <linux/firmware.h>
58 #include <linux/vmalloc.h>
60 #include <linux/mutex.h>
61 #include <linux/kthread.h>
63 #include <media/lirc_dev.h>
64 #include <media/lirc.h>
70 /* RX device buffer & lock */
71 struct lirc_buffer buf;
72 struct mutex buf_lock;
74 /* RX polling thread data */
75 struct task_struct *task;
86 /* TX additional actions needed */
88 bool post_tx_ready_poll;
97 struct i2c_adapter *adapter;
102 /* Minor -> data mapping */
103 static struct mutex ir_devices_lock;
104 static struct IR *ir_devices[MAX_IRCTL_DEVICES];
106 /* Block size for IR transmitter */
107 #define TX_BLOCK_SIZE 99
109 /* Hauppauge IR transmitter data */
110 struct tx_data_struct {
112 unsigned char *boot_data;
114 /* Start of binary data block */
115 unsigned char *datap;
117 /* End of binary data block */
120 /* Number of installed codesets */
121 unsigned int num_code_sets;
123 /* Pointers to codesets */
124 unsigned char **code_sets;
126 /* Global fixed data template */
127 int fixed[TX_BLOCK_SIZE];
130 static struct tx_data_struct *tx_data;
131 static struct mutex tx_data_lock;
133 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
135 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
136 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
138 /* module parameters */
139 static int debug; /* debug output */
140 static int tx_only; /* only handle the IR Tx function */
141 static int minor = -1; /* minor number */
143 #define dprintk(fmt, args...) \
146 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
150 static int add_to_buf(struct IR *ir)
153 unsigned char codes[2];
154 unsigned char keybuf[6];
158 unsigned char sendbuf[1] = { 0 };
159 struct IR_rx *rx = ir->rx;
164 if (lirc_buffer_full(&rx->buf)) {
165 dprintk("buffer overflow\n");
170 * service the device as long as it is returning
171 * data and we have space
174 if (kthread_should_stop())
178 * Lock i2c bus for the duration. RX/TX chips interfere so
181 mutex_lock(&ir->ir_lock);
183 if (kthread_should_stop()) {
184 mutex_unlock(&ir->ir_lock);
189 * Send random "poll command" (?) Windows driver does this
190 * and it is a good point to detect chip failure.
192 ret = i2c_master_send(rx->c, sendbuf, 1);
194 zilog_error("i2c_master_send failed with %d\n", ret);
196 mutex_unlock(&ir->ir_lock);
197 zilog_error("unable to read from the IR chip "
198 "after 3 resets, giving up\n");
202 /* Looks like the chip crashed, reset it */
203 zilog_error("polling the IR receiver chip failed, "
206 set_current_state(TASK_UNINTERRUPTIBLE);
207 if (kthread_should_stop()) {
208 mutex_unlock(&ir->ir_lock);
211 schedule_timeout((100 * HZ + 999) / 1000);
212 ir->tx->need_boot = 1;
215 mutex_unlock(&ir->ir_lock);
219 if (kthread_should_stop()) {
220 mutex_unlock(&ir->ir_lock);
223 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
224 mutex_unlock(&ir->ir_lock);
225 if (ret != sizeof(keybuf)) {
226 zilog_error("i2c_master_recv failed with %d -- "
227 "keeping last read buffer\n", ret);
229 rx->b[0] = keybuf[3];
230 rx->b[1] = keybuf[4];
231 rx->b[2] = keybuf[5];
232 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
236 if (rx->hdpvr_data_fmt) {
237 if (got_data && (keybuf[0] == 0x80))
239 else if (got_data && (keybuf[0] == 0x00))
241 } else if ((rx->b[0] & 0x80) == 0)
242 return got_data ? 0 : -ENODATA;
244 /* look what we have */
245 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
247 codes[0] = (code >> 8) & 0xff;
248 codes[1] = code & 0xff;
251 lirc_buffer_write(&rx->buf, codes);
253 } while (!lirc_buffer_full(&rx->buf));
259 * Main function of the polling thread -- from lirc_dev.
260 * We don't fit the LIRC model at all anymore. This is horrible, but
261 * basically we have a single RX/TX device with a nasty failure mode
262 * that needs to be accounted for across the pair. lirc lets us provide
263 * fops, but prevents us from using the internal polling, etc. if we do
264 * so. Hence the replication. Might be neater to extend the LIRC model
265 * to account for this but I'd think it's a very special case of seriously
266 * messed up hardware.
268 static int lirc_thread(void *arg)
271 struct IR_rx *rx = ir->rx;
273 dprintk("poll thread started\n");
275 while (!kthread_should_stop()) {
276 set_current_state(TASK_INTERRUPTIBLE);
278 /* if device not opened, we can sleep half a second */
280 schedule_timeout(HZ/2);
285 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
286 * We use this interval as the chip resets every time you poll
287 * it (bad!). This is therefore just sufficient to catch all
288 * of the button presses. It makes the remote much more
289 * responsive. You can see the difference by running irw and
290 * holding down a button. With 100ms, the old polling
291 * interval, you'll notice breaks in the repeat sequence
292 * corresponding to lost keypresses.
294 schedule_timeout((260 * HZ) / 1000);
295 if (kthread_should_stop())
298 wake_up_interruptible(&rx->buf.wait_poll);
301 dprintk("poll thread ended\n");
305 static int set_use_inc(void *data)
307 struct IR *ir = data;
309 if (ir->l.owner == NULL || try_module_get(ir->l.owner) == 0)
312 /* lock bttv in memory while /dev/lirc is in use */
314 * this is completely broken code. lirc_unregister_driver()
315 * must be possible even when the device is open
318 i2c_use_client(ir->rx->c);
320 i2c_use_client(ir->tx->c);
325 static void set_use_dec(void *data)
327 struct IR *ir = data;
330 i2c_release_client(ir->rx->c);
332 i2c_release_client(ir->tx->c);
333 if (ir->l.owner != NULL)
334 module_put(ir->l.owner);
337 /* safe read of a uint32 (always network byte order) */
338 static int read_uint32(unsigned char **data,
339 unsigned char *endp, unsigned int *val)
341 if (*data + 4 > endp)
343 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
344 ((*data)[2] << 8) | (*data)[3];
349 /* safe read of a uint8 */
350 static int read_uint8(unsigned char **data,
351 unsigned char *endp, unsigned char *val)
353 if (*data + 1 > endp)
359 /* safe skipping of N bytes */
360 static int skip(unsigned char **data,
361 unsigned char *endp, unsigned int distance)
363 if (*data + distance > endp)
369 /* decompress key data into the given buffer */
370 static int get_key_data(unsigned char *buf,
371 unsigned int codeset, unsigned int key)
373 unsigned char *data, *endp, *diffs, *key_block;
374 unsigned char keys, ndiffs, id;
375 unsigned int base, lim, pos, i;
377 /* Binary search for the codeset */
378 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
379 pos = base + (lim >> 1);
380 data = tx_data->code_sets[pos];
382 if (!read_uint32(&data, tx_data->endp, &i))
387 else if (codeset > i) {
396 /* Set end of data block */
397 endp = pos < tx_data->num_code_sets - 1 ?
398 tx_data->code_sets[pos + 1] : tx_data->endp;
400 /* Read the block header */
401 if (!read_uint8(&data, endp, &keys) ||
402 !read_uint8(&data, endp, &ndiffs) ||
403 ndiffs > TX_BLOCK_SIZE || keys == 0)
406 /* Save diffs & skip */
408 if (!skip(&data, endp, ndiffs))
411 /* Read the id of the first key */
412 if (!read_uint8(&data, endp, &id))
415 /* Unpack the first key's data */
416 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
417 if (tx_data->fixed[i] == -1) {
418 if (!read_uint8(&data, endp, &buf[i]))
421 buf[i] = (unsigned char)tx_data->fixed[i];
425 /* Early out key found/not found */
433 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
436 /* Binary search for the key */
437 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
439 unsigned char *key_data;
440 pos = base + (lim >> 1);
441 key_data = key_block + (ndiffs + 1) * pos;
443 if (*key_data == key) {
447 /* found, so unpack the diffs */
448 for (i = 0; i < ndiffs; ++i) {
450 if (!read_uint8(&key_data, endp, &val) ||
451 diffs[i] >= TX_BLOCK_SIZE)
457 } else if (key > *key_data) {
466 zilog_error("firmware is corrupt\n");
470 /* send a block of data to the IR TX device */
471 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
474 unsigned char buf[5];
476 for (i = 0; i < TX_BLOCK_SIZE;) {
477 int tosend = TX_BLOCK_SIZE - i;
480 buf[0] = (unsigned char)(i + 1);
481 for (j = 0; j < tosend; ++j)
482 buf[1 + j] = data_block[i + j];
483 dprintk("%02x %02x %02x %02x %02x",
484 buf[0], buf[1], buf[2], buf[3], buf[4]);
485 ret = i2c_master_send(tx->c, buf, tosend + 1);
486 if (ret != tosend + 1) {
487 zilog_error("i2c_master_send failed with %d\n", ret);
488 return ret < 0 ? ret : -EFAULT;
495 /* send boot data to the IR TX device */
496 static int send_boot_data(struct IR_tx *tx)
499 unsigned char buf[4];
501 /* send the boot block */
502 ret = send_data_block(tx, tx_data->boot_data);
509 ret = i2c_master_send(tx->c, buf, 2);
511 zilog_error("i2c_master_send failed with %d\n", ret);
512 return ret < 0 ? ret : -EFAULT;
514 ret = i2c_master_send(tx->c, buf, 1);
516 zilog_error("i2c_master_send failed with %d\n", ret);
517 return ret < 0 ? ret : -EFAULT;
520 /* Here comes the firmware version... (hopefully) */
521 ret = i2c_master_recv(tx->c, buf, 4);
523 zilog_error("i2c_master_recv failed with %d\n", ret);
526 if (buf[0] != 0x80) {
527 zilog_error("unexpected IR TX response: %02x\n", buf[0]);
530 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
531 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
536 /* unload "firmware", lock held */
537 static void fw_unload_locked(void)
540 if (tx_data->code_sets)
541 vfree(tx_data->code_sets);
544 vfree(tx_data->datap);
548 dprintk("successfully unloaded IR blaster firmware\n");
552 /* unload "firmware" for the IR TX device */
553 static void fw_unload(void)
555 mutex_lock(&tx_data_lock);
557 mutex_unlock(&tx_data_lock);
560 /* load "firmware" for the IR TX device */
561 static int fw_load(struct IR_tx *tx)
565 unsigned char *data, version, num_global_fixed;
566 const struct firmware *fw_entry;
568 /* Already loaded? */
569 mutex_lock(&tx_data_lock);
575 /* Request codeset data file */
576 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
578 zilog_error("firmware haup-ir-blaster.bin not available "
580 ret = ret < 0 ? ret : -EFAULT;
583 dprintk("firmware of size %zu loaded\n", fw_entry->size);
586 tx_data = vmalloc(sizeof(*tx_data));
587 if (tx_data == NULL) {
588 zilog_error("out of memory\n");
589 release_firmware(fw_entry);
593 tx_data->code_sets = NULL;
595 /* Copy the data so hotplug doesn't get confused and timeout */
596 tx_data->datap = vmalloc(fw_entry->size);
597 if (tx_data->datap == NULL) {
598 zilog_error("out of memory\n");
599 release_firmware(fw_entry);
604 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
605 tx_data->endp = tx_data->datap + fw_entry->size;
606 release_firmware(fw_entry); fw_entry = NULL;
609 data = tx_data->datap;
610 if (!read_uint8(&data, tx_data->endp, &version))
613 zilog_error("unsupported code set file version (%u, expected"
614 "1) -- please upgrade to a newer driver",
621 /* Save boot block for later */
622 tx_data->boot_data = data;
623 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
626 if (!read_uint32(&data, tx_data->endp,
627 &tx_data->num_code_sets))
630 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
632 tx_data->code_sets = vmalloc(
633 tx_data->num_code_sets * sizeof(char *));
634 if (tx_data->code_sets == NULL) {
640 for (i = 0; i < TX_BLOCK_SIZE; ++i)
641 tx_data->fixed[i] = -1;
643 /* Read global fixed data template */
644 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
645 num_global_fixed > TX_BLOCK_SIZE)
647 for (i = 0; i < num_global_fixed; ++i) {
648 unsigned char pos, val;
649 if (!read_uint8(&data, tx_data->endp, &pos) ||
650 !read_uint8(&data, tx_data->endp, &val) ||
651 pos >= TX_BLOCK_SIZE)
653 tx_data->fixed[pos] = (int)val;
656 /* Filch out the position of each code set */
657 for (i = 0; i < tx_data->num_code_sets; ++i) {
660 unsigned char ndiffs;
662 /* Save the codeset position */
663 tx_data->code_sets[i] = data;
666 if (!read_uint32(&data, tx_data->endp, &id) ||
667 !read_uint8(&data, tx_data->endp, &keys) ||
668 !read_uint8(&data, tx_data->endp, &ndiffs) ||
669 ndiffs > TX_BLOCK_SIZE || keys == 0)
672 /* skip diff positions */
673 if (!skip(&data, tx_data->endp, ndiffs))
677 * After the diffs we have the first key id + data -
680 if (!skip(&data, tx_data->endp,
681 1 + TX_BLOCK_SIZE - num_global_fixed))
684 /* Then we have keys-1 blocks of key id+diffs */
685 if (!skip(&data, tx_data->endp,
686 (ndiffs + 1) * (keys - 1)))
693 zilog_error("firmware is corrupt\n");
698 mutex_unlock(&tx_data_lock);
702 /* initialise the IR TX device */
703 static int tx_init(struct IR_tx *tx)
707 /* Load 'firmware' */
712 /* Send boot block */
713 ret = send_boot_data(tx);
722 /* do nothing stub to make LIRC happy */
723 static loff_t lseek(struct file *filep, loff_t offset, int orig)
728 /* copied from lirc_dev */
729 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
731 struct IR *ir = filep->private_data;
732 struct IR_rx *rx = ir->rx;
733 int ret = 0, written = 0;
734 DECLARE_WAITQUEUE(wait, current);
736 dprintk("read called\n");
740 if (mutex_lock_interruptible(&rx->buf_lock))
743 if (n % rx->buf.chunk_size) {
744 dprintk("read result = -EINVAL\n");
745 mutex_unlock(&rx->buf_lock);
750 * we add ourselves to the task queue before buffer check
751 * to avoid losing scan code (in case when queue is awaken somewhere
752 * between while condition checking and scheduling)
754 add_wait_queue(&rx->buf.wait_poll, &wait);
755 set_current_state(TASK_INTERRUPTIBLE);
758 * while we didn't provide 'length' bytes, device is opened in blocking
759 * mode and 'copy_to_user' is happy, wait for data.
761 while (written < n && ret == 0) {
762 if (lirc_buffer_empty(&rx->buf)) {
764 * According to the read(2) man page, 'written' can be
765 * returned as less than 'n', instead of blocking
766 * again, returning -EWOULDBLOCK, or returning
771 if (filep->f_flags & O_NONBLOCK) {
775 if (signal_pending(current)) {
780 set_current_state(TASK_INTERRUPTIBLE);
782 unsigned char buf[rx->buf.chunk_size];
783 lirc_buffer_read(&rx->buf, buf);
784 ret = copy_to_user((void *)outbuf+written, buf,
786 written += rx->buf.chunk_size;
790 remove_wait_queue(&rx->buf.wait_poll, &wait);
791 set_current_state(TASK_RUNNING);
792 mutex_unlock(&rx->buf_lock);
794 dprintk("read result = %s (%d)\n",
795 ret ? "-EFAULT" : "OK", ret);
797 return ret ? ret : written;
800 /* send a keypress to the IR TX device */
801 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
803 unsigned char data_block[TX_BLOCK_SIZE];
804 unsigned char buf[2];
807 /* Get data for the codeset/key */
808 ret = get_key_data(data_block, code, key);
810 if (ret == -EPROTO) {
811 zilog_error("failed to get data for code %u, key %u -- check "
812 "lircd.conf entries\n", code, key);
817 /* Send the data block */
818 ret = send_data_block(tx, data_block);
822 /* Send data block length? */
825 ret = i2c_master_send(tx->c, buf, 2);
827 zilog_error("i2c_master_send failed with %d\n", ret);
828 return ret < 0 ? ret : -EFAULT;
830 ret = i2c_master_send(tx->c, buf, 1);
832 zilog_error("i2c_master_send failed with %d\n", ret);
833 return ret < 0 ? ret : -EFAULT;
836 /* Send finished download? */
837 ret = i2c_master_recv(tx->c, buf, 1);
839 zilog_error("i2c_master_recv failed with %d\n", ret);
840 return ret < 0 ? ret : -EFAULT;
842 if (buf[0] != 0xA0) {
843 zilog_error("unexpected IR TX response #1: %02x\n",
848 /* Send prepare command? */
851 ret = i2c_master_send(tx->c, buf, 2);
853 zilog_error("i2c_master_send failed with %d\n", ret);
854 return ret < 0 ? ret : -EFAULT;
858 * The sleep bits aren't necessary on the HD PVR, and in fact, the
859 * last i2c_master_recv always fails with a -5, so for now, we're
860 * going to skip this whole mess and say we're done on the HD PVR
862 if (!tx->post_tx_ready_poll) {
863 dprintk("sent code %u, key %u\n", code, key);
868 * This bit NAKs until the device is ready, so we retry it
869 * sleeping a bit each time. This seems to be what the windows
870 * driver does, approximately.
873 for (i = 0; i < 20; ++i) {
874 set_current_state(TASK_UNINTERRUPTIBLE);
875 schedule_timeout((50 * HZ + 999) / 1000);
876 ret = i2c_master_send(tx->c, buf, 1);
879 dprintk("NAK expected: i2c_master_send "
880 "failed with %d (try %d)\n", ret, i+1);
883 zilog_error("IR TX chip never got ready: last i2c_master_send "
884 "failed with %d\n", ret);
885 return ret < 0 ? ret : -EFAULT;
888 /* Seems to be an 'ok' response */
889 i = i2c_master_recv(tx->c, buf, 1);
891 zilog_error("i2c_master_recv failed with %d\n", ret);
894 if (buf[0] != 0x80) {
895 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
899 /* Oh good, it worked */
900 dprintk("sent code %u, key %u\n", code, key);
905 * Write a code to the device. We take in a 32-bit number (an int) and then
906 * decode this to a codeset/key index. The key data is then decompressed and
907 * sent to the device. We have a spin lock as per i2c documentation to prevent
908 * multiple concurrent sends which would probably cause the device to explode.
910 static ssize_t write(struct file *filep, const char *buf, size_t n,
913 struct IR *ir = filep->private_data;
914 struct IR_tx *tx = ir->tx;
921 /* Validate user parameters */
925 /* Lock i2c bus for the duration */
926 mutex_lock(&ir->ir_lock);
928 /* Send each keypress */
929 for (i = 0; i < n;) {
933 if (copy_from_user(&command, buf + i, sizeof(command))) {
934 mutex_unlock(&ir->ir_lock);
938 /* Send boot data first if required */
939 if (tx->need_boot == 1) {
940 ret = send_boot_data(tx);
947 ret = send_code(tx, (unsigned)command >> 16,
948 (unsigned)command & 0xFFFF);
949 if (ret == -EPROTO) {
950 mutex_unlock(&ir->ir_lock);
956 * Hmm, a failure. If we've had a few then give up, otherwise
960 /* Looks like the chip crashed, reset it */
961 zilog_error("sending to the IR transmitter chip "
962 "failed, trying reset\n");
965 zilog_error("unable to send to the IR chip "
966 "after 3 resets, giving up\n");
967 mutex_unlock(&ir->ir_lock);
970 set_current_state(TASK_UNINTERRUPTIBLE);
971 schedule_timeout((100 * HZ + 999) / 1000);
978 /* Release i2c bus */
979 mutex_unlock(&ir->ir_lock);
985 /* copied from lirc_dev */
986 static unsigned int poll(struct file *filep, poll_table *wait)
988 struct IR *ir = filep->private_data;
989 struct IR_rx *rx = ir->rx;
992 dprintk("poll called\n");
996 mutex_lock(&rx->buf_lock);
998 poll_wait(filep, &rx->buf.wait_poll, wait);
1000 dprintk("poll result = %s\n",
1001 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1003 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1005 mutex_unlock(&rx->buf_lock);
1009 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1011 struct IR *ir = filep->private_data;
1013 unsigned long mode, features = 0;
1015 features |= LIRC_CAN_SEND_PULSE;
1017 features |= LIRC_CAN_REC_LIRCCODE;
1020 case LIRC_GET_LENGTH:
1021 result = put_user((unsigned long)13,
1022 (unsigned long *)arg);
1024 case LIRC_GET_FEATURES:
1025 result = put_user(features, (unsigned long *) arg);
1027 case LIRC_GET_REC_MODE:
1028 if (!(features&LIRC_CAN_REC_MASK))
1031 result = put_user(LIRC_REC2MODE
1032 (features&LIRC_CAN_REC_MASK),
1033 (unsigned long *)arg);
1035 case LIRC_SET_REC_MODE:
1036 if (!(features&LIRC_CAN_REC_MASK))
1039 result = get_user(mode, (unsigned long *)arg);
1040 if (!result && !(LIRC_MODE2REC(mode) & features))
1043 case LIRC_GET_SEND_MODE:
1044 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1046 case LIRC_SET_SEND_MODE:
1047 result = get_user(mode, (unsigned long *) arg);
1048 if (!result && mode != LIRC_MODE_PULSE)
1057 /* ir_devices_lock must be held */
1058 static struct IR *find_ir_device_by_minor(unsigned int minor)
1060 if (minor >= MAX_IRCTL_DEVICES)
1063 return ir_devices[minor];
1067 * Open the IR device. Get hold of our IR structure and
1068 * stash it in private_data for the file
1070 static int open(struct inode *node, struct file *filep)
1074 unsigned int minor = MINOR(node->i_rdev);
1076 /* find our IR struct */
1077 mutex_lock(&ir_devices_lock);
1078 ir = find_ir_device_by_minor(minor);
1079 mutex_unlock(&ir_devices_lock);
1084 /* increment in use count */
1085 mutex_lock(&ir->ir_lock);
1087 ret = set_use_inc(ir);
1090 mutex_unlock(&ir->ir_lock);
1093 mutex_unlock(&ir->ir_lock);
1095 /* stash our IR struct */
1096 filep->private_data = ir;
1101 /* Close the IR device */
1102 static int close(struct inode *node, struct file *filep)
1104 /* find our IR struct */
1105 struct IR *ir = filep->private_data;
1107 zilog_error("close: no private_data attached to the file!\n");
1111 /* decrement in use count */
1112 mutex_lock(&ir->ir_lock);
1115 mutex_unlock(&ir->ir_lock);
1120 static struct lirc_driver lirc_template = {
1121 .name = "lirc_zilog",
1122 .set_use_inc = set_use_inc,
1123 .set_use_dec = set_use_dec,
1124 .owner = THIS_MODULE
1127 static int ir_remove(struct i2c_client *client);
1128 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1130 #define ID_FLAG_TX 0x01
1131 #define ID_FLAG_HDPVR 0x02
1133 static const struct i2c_device_id ir_transceiver_id[] = {
1134 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1135 { "ir_rx_z8f0811_haup", 0 },
1136 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1137 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1141 static struct i2c_driver driver = {
1143 .owner = THIS_MODULE,
1144 .name = "Zilog/Hauppauge i2c IR",
1147 .remove = ir_remove,
1148 .id_table = ir_transceiver_id,
1151 static const struct file_operations lirc_fops = {
1152 .owner = THIS_MODULE,
1157 .unlocked_ioctl = ioctl,
1158 #ifdef CONFIG_COMPAT
1159 .compat_ioctl = ioctl,
1165 static void destroy_rx_kthread(struct IR_rx *rx)
1167 /* end up polling thread */
1168 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1169 kthread_stop(rx->task);
1174 /* ir_devices_lock must be held */
1175 static int add_ir_device(struct IR *ir)
1179 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1180 if (ir_devices[i] == NULL) {
1185 return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1188 /* ir_devices_lock must be held */
1189 static void del_ir_device(struct IR *ir)
1193 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1194 if (ir_devices[i] == ir) {
1195 ir_devices[i] = NULL;
1200 static int ir_remove(struct i2c_client *client)
1202 struct IR *ir = i2c_get_clientdata(client);
1204 mutex_lock(&ir_devices_lock);
1207 /* We destroyed everything when the first client came through */
1208 mutex_unlock(&ir_devices_lock);
1213 lirc_unregister_driver(ir->l.minor);
1216 destroy_rx_kthread(ir->rx);
1217 if (ir->rx != NULL) {
1218 if (ir->rx->buf.fifo_initialized)
1219 lirc_buffer_free(&ir->rx->buf);
1220 i2c_set_clientdata(ir->rx->c, NULL);
1225 i2c_set_clientdata(ir->tx->c, NULL);
1232 mutex_unlock(&ir_devices_lock);
1237 /* ir_devices_lock must be held */
1238 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1241 struct IR *ir = NULL;
1243 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1244 if (ir_devices[i] != NULL &&
1245 ir_devices[i]->adapter == adapter) {
1253 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1256 struct i2c_adapter *adap = client->adapter;
1258 bool tx_probe = false;
1260 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1261 __func__, id->name, adap->nr, adap->name, client->addr);
1264 * The IR receiver is at i2c address 0x71.
1265 * The IR transmitter is at i2c address 0x70.
1268 if (id->driver_data & ID_FLAG_TX)
1270 else if (tx_only) /* module option */
1273 zilog_info("probing IR %s on %s (i2c-%d)\n",
1274 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1276 mutex_lock(&ir_devices_lock);
1278 /* Use a single struct IR instance for both the Rx and Tx functions */
1279 ir = find_ir_device_by_adapter(adap);
1281 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1286 /* store for use in ir_probe() again, and open() later on */
1287 ret = add_ir_device(ir);
1292 mutex_init(&ir->ir_lock);
1294 /* set lirc_dev stuff */
1295 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1296 ir->l.minor = minor; /* module option */
1297 ir->l.code_length = 13;
1299 ir->l.fops = &lirc_fops;
1301 ir->l.dev = &adap->dev;
1302 ir->l.sample_rate = 0;
1306 /* Set up a struct IR_tx instance */
1307 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1308 if (ir->tx == NULL) {
1314 ir->tx->need_boot = 1;
1315 ir->tx->post_tx_ready_poll =
1316 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1318 /* Set up a struct IR_rx instance */
1319 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1320 if (ir->rx == NULL) {
1325 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1329 mutex_init(&ir->rx->buf_lock);
1331 ir->rx->hdpvr_data_fmt =
1332 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1334 /* set lirc_dev stuff */
1335 ir->l.rbuf = &ir->rx->buf;
1338 i2c_set_clientdata(client, ir);
1340 /* Proceed only if we have the required Tx and Rx clients ready to go */
1341 if (ir->tx == NULL ||
1342 (ir->rx == NULL && !tx_only)) {
1343 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1344 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1345 adap->nr, tx_probe ? "Rx" : "Tx");
1349 /* initialise RX device */
1350 if (ir->rx != NULL) {
1351 /* try to fire up polling thread */
1352 ir->rx->task = kthread_run(lirc_thread, ir,
1353 "zilog-rx-i2c-%d", adap->nr);
1354 if (IS_ERR(ir->rx->task)) {
1355 ret = PTR_ERR(ir->rx->task);
1356 zilog_error("%s: could not start IR Rx polling thread"
1362 /* register with lirc */
1363 ir->l.minor = lirc_register_driver(&ir->l);
1364 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1365 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1366 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1368 goto out_free_thread;
1372 * if we have the tx device, load the 'firmware'. We do this
1373 * after registering with lirc as otherwise hotplug seems to take
1374 * 10s to create the lirc device.
1376 ret = tx_init(ir->tx);
1378 goto out_unregister;
1380 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1381 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1383 mutex_unlock(&ir_devices_lock);
1387 lirc_unregister_driver(ir->l.minor);
1389 destroy_rx_kthread(ir->rx);
1391 if (ir->rx != NULL) {
1392 if (ir->rx->buf.fifo_initialized)
1393 lirc_buffer_free(&ir->rx->buf);
1394 if (ir->rx->c != NULL)
1395 i2c_set_clientdata(ir->rx->c, NULL);
1398 if (ir->tx != NULL) {
1399 if (ir->tx->c != NULL)
1400 i2c_set_clientdata(ir->tx->c, NULL);
1407 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1408 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1410 mutex_unlock(&ir_devices_lock);
1414 static int __init zilog_init(void)
1418 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1420 mutex_init(&tx_data_lock);
1421 mutex_init(&ir_devices_lock);
1423 request_module("firmware_class");
1425 ret = i2c_add_driver(&driver);
1427 zilog_error("initialization failed\n");
1429 zilog_notify("initialization complete\n");
1434 static void __exit zilog_exit(void)
1436 i2c_del_driver(&driver);
1439 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1442 module_init(zilog_init);
1443 module_exit(zilog_exit);
1445 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1446 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1447 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1449 MODULE_LICENSE("GPL");
1450 /* for compat with old name, which isn't all that accurate anymore */
1451 MODULE_ALIAS("lirc_pvr150");
1453 module_param(minor, int, 0444);
1454 MODULE_PARM_DESC(minor, "Preferred minor device number");
1456 module_param(debug, bool, 0644);
1457 MODULE_PARM_DESC(debug, "Enable debugging messages");
1459 module_param(tx_only, bool, 0644);
1460 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");