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 * This program is free software; you can redistribute it and/or modify
24 * it under the terms of the GNU General Public License as published by
25 * the Free Software Foundation; either version 2 of the License, or
26 * (at your option) any later version.
28 * This program is distributed in the hope that it will be useful,
29 * but WITHOUT ANY WARRANTY; without even the implied warranty of
30 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 * GNU General Public License for more details.
33 * You should have received a copy of the GNU General Public License
34 * along with this program; if not, write to the Free Software
35 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include <linux/version.h>
41 #include <linux/module.h>
42 #include <linux/kmod.h>
43 #include <linux/kernel.h>
44 #include <linux/sched.h>
46 #include <linux/poll.h>
47 #include <linux/string.h>
48 #include <linux/timer.h>
49 #include <linux/delay.h>
50 #include <linux/completion.h>
51 #include <linux/errno.h>
52 #include <linux/slab.h>
53 #include <linux/i2c.h>
54 #include <linux/firmware.h>
55 #include <linux/vmalloc.h>
57 #include <linux/mutex.h>
58 #include <linux/kthread.h>
60 #include <media/lirc_dev.h>
61 #include <media/lirc.h>
72 struct i2c_client c_rx;
75 /* RX device buffer & lock */
76 struct lirc_buffer buf;
77 struct mutex buf_lock;
79 /* RX polling thread data */
80 struct completion *t_notify;
81 struct completion *t_notify2;
83 struct task_struct *task;
89 struct i2c_client c_tx;
94 /* Minor -> data mapping */
95 static struct IR *ir_devices[MAX_IRCTL_DEVICES];
97 /* Block size for IR transmitter */
98 #define TX_BLOCK_SIZE 99
100 /* Hauppauge IR transmitter data */
101 struct tx_data_struct {
103 unsigned char *boot_data;
105 /* Start of binary data block */
106 unsigned char *datap;
108 /* End of binary data block */
111 /* Number of installed codesets */
112 unsigned int num_code_sets;
114 /* Pointers to codesets */
115 unsigned char **code_sets;
117 /* Global fixed data template */
118 int fixed[TX_BLOCK_SIZE];
121 static struct tx_data_struct *tx_data;
122 static struct mutex tx_data_lock;
124 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
126 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
128 #define ZILOG_HAUPPAUGE_IR_RX_NAME "Zilog/Hauppauge IR RX"
129 #define ZILOG_HAUPPAUGE_IR_TX_NAME "Zilog/Hauppauge IR TX"
131 /* module parameters */
132 static int debug; /* debug output */
133 static int disable_rx; /* disable RX device */
134 static int disable_tx; /* disable TX device */
135 static int minor = -1; /* minor number */
137 #define dprintk(fmt, args...) \
140 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
144 static int add_to_buf(struct IR *ir)
147 unsigned char codes[2];
148 unsigned char keybuf[6];
152 unsigned char sendbuf[1] = { 0 };
154 if (lirc_buffer_full(&ir->buf)) {
155 dprintk("buffer overflow\n");
160 * service the device as long as it is returning
161 * data and we have space
165 * Lock i2c bus for the duration. RX/TX chips interfere so
168 mutex_lock(&ir->ir_lock);
171 * Send random "poll command" (?) Windows driver does this
172 * and it is a good point to detect chip failure.
174 ret = i2c_master_send(&ir->c_rx, sendbuf, 1);
176 zilog_error("i2c_master_send failed with %d\n", ret);
178 mutex_unlock(&ir->ir_lock);
179 zilog_error("unable to read from the IR chip "
180 "after 3 resets, giving up\n");
184 /* Looks like the chip crashed, reset it */
185 zilog_error("polling the IR receiver chip failed, "
188 set_current_state(TASK_UNINTERRUPTIBLE);
189 schedule_timeout((100 * HZ + 999) / 1000);
193 mutex_unlock(&ir->ir_lock);
197 ret = i2c_master_recv(&ir->c_rx, keybuf, sizeof(keybuf));
198 mutex_unlock(&ir->ir_lock);
199 if (ret != sizeof(keybuf)) {
200 zilog_error("i2c_master_recv failed with %d -- "
201 "keeping last read buffer\n", ret);
203 ir->b[0] = keybuf[3];
204 ir->b[1] = keybuf[4];
205 ir->b[2] = keybuf[5];
206 dprintk("key (0x%02x/0x%02x)\n", ir->b[0], ir->b[1]);
211 if (got_data && (keybuf[0] == 0x80))
213 else if (got_data && (keybuf[0] == 0x00))
215 } else if ((ir->b[0] & 0x80) == 0)
216 return got_data ? 0 : -ENODATA;
218 /* look what we have */
219 code = (((__u16)ir->b[0] & 0x7f) << 6) | (ir->b[1] >> 2);
221 codes[0] = (code >> 8) & 0xff;
222 codes[1] = code & 0xff;
225 lirc_buffer_write(&ir->buf, codes);
227 } while (!lirc_buffer_full(&ir->buf));
233 * Main function of the polling thread -- from lirc_dev.
234 * We don't fit the LIRC model at all anymore. This is horrible, but
235 * basically we have a single RX/TX device with a nasty failure mode
236 * that needs to be accounted for across the pair. lirc lets us provide
237 * fops, but prevents us from using the internal polling, etc. if we do
238 * so. Hence the replication. Might be neater to extend the LIRC model
239 * to account for this but I'd think it's a very special case of seriously
240 * messed up hardware.
242 static int lirc_thread(void *arg)
246 if (ir->t_notify != NULL)
247 complete(ir->t_notify);
249 dprintk("poll thread started\n");
253 set_current_state(TASK_INTERRUPTIBLE);
256 * This is ~113*2 + 24 + jitter (2*repeat gap +
257 * code length). We use this interval as the chip
258 * resets every time you poll it (bad!). This is
259 * therefore just sufficient to catch all of the
260 * button presses. It makes the remote much more
261 * responsive. You can see the difference by
262 * running irw and holding down a button. With
263 * 100ms, the old polling interval, you'll notice
264 * breaks in the repeat sequence corresponding to
267 schedule_timeout((260 * HZ) / 1000);
271 wake_up_interruptible(&ir->buf.wait_poll);
273 /* if device not opened so we can sleep half a second */
274 set_current_state(TASK_INTERRUPTIBLE);
275 schedule_timeout(HZ/2);
277 } while (!ir->shutdown);
279 if (ir->t_notify2 != NULL)
280 wait_for_completion(ir->t_notify2);
283 if (ir->t_notify != NULL)
284 complete(ir->t_notify);
286 dprintk("poll thread ended\n");
290 static int set_use_inc(void *data)
292 struct IR *ir = data;
294 if (ir->l.owner == NULL || try_module_get(ir->l.owner) == 0)
297 /* lock bttv in memory while /dev/lirc is in use */
299 * this is completely broken code. lirc_unregister_driver()
300 * must be possible even when the device is open
303 i2c_use_client(&ir->c_rx);
305 i2c_use_client(&ir->c_tx);
310 static void set_use_dec(void *data)
312 struct IR *ir = data;
315 i2c_release_client(&ir->c_rx);
317 i2c_release_client(&ir->c_tx);
318 if (ir->l.owner != NULL)
319 module_put(ir->l.owner);
322 /* safe read of a uint32 (always network byte order) */
323 static int read_uint32(unsigned char **data,
324 unsigned char *endp, unsigned int *val)
326 if (*data + 4 > endp)
328 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
329 ((*data)[2] << 8) | (*data)[3];
334 /* safe read of a uint8 */
335 static int read_uint8(unsigned char **data,
336 unsigned char *endp, unsigned char *val)
338 if (*data + 1 > endp)
344 /* safe skipping of N bytes */
345 static int skip(unsigned char **data,
346 unsigned char *endp, unsigned int distance)
348 if (*data + distance > endp)
354 /* decompress key data into the given buffer */
355 static int get_key_data(unsigned char *buf,
356 unsigned int codeset, unsigned int key)
358 unsigned char *data, *endp, *diffs, *key_block;
359 unsigned char keys, ndiffs, id;
360 unsigned int base, lim, pos, i;
362 /* Binary search for the codeset */
363 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
364 pos = base + (lim >> 1);
365 data = tx_data->code_sets[pos];
367 if (!read_uint32(&data, tx_data->endp, &i))
372 else if (codeset > i) {
381 /* Set end of data block */
382 endp = pos < tx_data->num_code_sets - 1 ?
383 tx_data->code_sets[pos + 1] : tx_data->endp;
385 /* Read the block header */
386 if (!read_uint8(&data, endp, &keys) ||
387 !read_uint8(&data, endp, &ndiffs) ||
388 ndiffs > TX_BLOCK_SIZE || keys == 0)
391 /* Save diffs & skip */
393 if (!skip(&data, endp, ndiffs))
396 /* Read the id of the first key */
397 if (!read_uint8(&data, endp, &id))
400 /* Unpack the first key's data */
401 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
402 if (tx_data->fixed[i] == -1) {
403 if (!read_uint8(&data, endp, &buf[i]))
406 buf[i] = (unsigned char)tx_data->fixed[i];
410 /* Early out key found/not found */
418 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
421 /* Binary search for the key */
422 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
424 unsigned char *key_data;
425 pos = base + (lim >> 1);
426 key_data = key_block + (ndiffs + 1) * pos;
428 if (*key_data == key) {
432 /* found, so unpack the diffs */
433 for (i = 0; i < ndiffs; ++i) {
435 if (!read_uint8(&key_data, endp, &val) ||
436 diffs[i] >= TX_BLOCK_SIZE)
442 } else if (key > *key_data) {
451 zilog_error("firmware is corrupt\n");
455 /* send a block of data to the IR TX device */
456 static int send_data_block(struct IR *ir, unsigned char *data_block)
459 unsigned char buf[5];
461 for (i = 0; i < TX_BLOCK_SIZE;) {
462 int tosend = TX_BLOCK_SIZE - i;
465 buf[0] = (unsigned char)(i + 1);
466 for (j = 0; j < tosend; ++j)
467 buf[1 + j] = data_block[i + j];
468 dprintk("%02x %02x %02x %02x %02x",
469 buf[0], buf[1], buf[2], buf[3], buf[4]);
470 ret = i2c_master_send(&ir->c_tx, buf, tosend + 1);
471 if (ret != tosend + 1) {
472 zilog_error("i2c_master_send failed with %d\n", ret);
473 return ret < 0 ? ret : -EFAULT;
480 /* send boot data to the IR TX device */
481 static int send_boot_data(struct IR *ir)
484 unsigned char buf[4];
486 /* send the boot block */
487 ret = send_data_block(ir, tx_data->boot_data);
494 ret = i2c_master_send(&ir->c_tx, buf, 2);
496 zilog_error("i2c_master_send failed with %d\n", ret);
497 return ret < 0 ? ret : -EFAULT;
499 ret = i2c_master_send(&ir->c_tx, buf, 1);
501 zilog_error("i2c_master_send failed with %d\n", ret);
502 return ret < 0 ? ret : -EFAULT;
505 /* Here comes the firmware version... (hopefully) */
506 ret = i2c_master_recv(&ir->c_tx, buf, 4);
508 zilog_error("i2c_master_recv failed with %d\n", ret);
511 if (buf[0] != 0x80) {
512 zilog_error("unexpected IR TX response: %02x\n", buf[0]);
515 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
516 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
521 /* unload "firmware", lock held */
522 static void fw_unload_locked(void)
525 if (tx_data->code_sets)
526 vfree(tx_data->code_sets);
529 vfree(tx_data->datap);
533 dprintk("successfully unloaded IR blaster firmware\n");
537 /* unload "firmware" for the IR TX device */
538 static void fw_unload(void)
540 mutex_lock(&tx_data_lock);
542 mutex_unlock(&tx_data_lock);
545 /* load "firmware" for the IR TX device */
546 static int fw_load(struct IR *ir)
550 unsigned char *data, version, num_global_fixed;
551 const struct firmware *fw_entry;
553 /* Already loaded? */
554 mutex_lock(&tx_data_lock);
560 /* Request codeset data file */
561 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &ir->c_tx.dev);
563 zilog_error("firmware haup-ir-blaster.bin not available "
565 ret = ret < 0 ? ret : -EFAULT;
568 dprintk("firmware of size %zu loaded\n", fw_entry->size);
571 tx_data = vmalloc(sizeof(*tx_data));
572 if (tx_data == NULL) {
573 zilog_error("out of memory\n");
574 release_firmware(fw_entry);
578 tx_data->code_sets = NULL;
580 /* Copy the data so hotplug doesn't get confused and timeout */
581 tx_data->datap = vmalloc(fw_entry->size);
582 if (tx_data->datap == NULL) {
583 zilog_error("out of memory\n");
584 release_firmware(fw_entry);
589 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
590 tx_data->endp = tx_data->datap + fw_entry->size;
591 release_firmware(fw_entry); fw_entry = NULL;
594 data = tx_data->datap;
595 if (!read_uint8(&data, tx_data->endp, &version))
598 zilog_error("unsupported code set file version (%u, expected"
599 "1) -- please upgrade to a newer driver",
606 /* Save boot block for later */
607 tx_data->boot_data = data;
608 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
611 if (!read_uint32(&data, tx_data->endp,
612 &tx_data->num_code_sets))
615 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
617 tx_data->code_sets = vmalloc(
618 tx_data->num_code_sets * sizeof(char *));
619 if (tx_data->code_sets == NULL) {
625 for (i = 0; i < TX_BLOCK_SIZE; ++i)
626 tx_data->fixed[i] = -1;
628 /* Read global fixed data template */
629 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
630 num_global_fixed > TX_BLOCK_SIZE)
632 for (i = 0; i < num_global_fixed; ++i) {
633 unsigned char pos, val;
634 if (!read_uint8(&data, tx_data->endp, &pos) ||
635 !read_uint8(&data, tx_data->endp, &val) ||
636 pos >= TX_BLOCK_SIZE)
638 tx_data->fixed[pos] = (int)val;
641 /* Filch out the position of each code set */
642 for (i = 0; i < tx_data->num_code_sets; ++i) {
645 unsigned char ndiffs;
647 /* Save the codeset position */
648 tx_data->code_sets[i] = data;
651 if (!read_uint32(&data, tx_data->endp, &id) ||
652 !read_uint8(&data, tx_data->endp, &keys) ||
653 !read_uint8(&data, tx_data->endp, &ndiffs) ||
654 ndiffs > TX_BLOCK_SIZE || keys == 0)
657 /* skip diff positions */
658 if (!skip(&data, tx_data->endp, ndiffs))
662 * After the diffs we have the first key id + data -
665 if (!skip(&data, tx_data->endp,
666 1 + TX_BLOCK_SIZE - num_global_fixed))
669 /* Then we have keys-1 blocks of key id+diffs */
670 if (!skip(&data, tx_data->endp,
671 (ndiffs + 1) * (keys - 1)))
678 zilog_error("firmware is corrupt\n");
683 mutex_unlock(&tx_data_lock);
687 /* initialise the IR TX device */
688 static int tx_init(struct IR *ir)
692 /* Load 'firmware' */
697 /* Send boot block */
698 ret = send_boot_data(ir);
707 /* do nothing stub to make LIRC happy */
708 static loff_t lseek(struct file *filep, loff_t offset, int orig)
713 /* copied from lirc_dev */
714 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
716 struct IR *ir = filep->private_data;
717 unsigned char buf[ir->buf.chunk_size];
718 int ret = 0, written = 0;
719 DECLARE_WAITQUEUE(wait, current);
721 dprintk("read called\n");
722 if (ir->c_rx.addr == 0)
725 if (mutex_lock_interruptible(&ir->buf_lock))
728 if (n % ir->buf.chunk_size) {
729 dprintk("read result = -EINVAL\n");
730 mutex_unlock(&ir->buf_lock);
735 * we add ourselves to the task queue before buffer check
736 * to avoid losing scan code (in case when queue is awaken somewhere
737 * between while condition checking and scheduling)
739 add_wait_queue(&ir->buf.wait_poll, &wait);
740 set_current_state(TASK_INTERRUPTIBLE);
743 * while we didn't provide 'length' bytes, device is opened in blocking
744 * mode and 'copy_to_user' is happy, wait for data.
746 while (written < n && ret == 0) {
747 if (lirc_buffer_empty(&ir->buf)) {
749 * According to the read(2) man page, 'written' can be
750 * returned as less than 'n', instead of blocking
751 * again, returning -EWOULDBLOCK, or returning
756 if (filep->f_flags & O_NONBLOCK) {
760 if (signal_pending(current)) {
765 set_current_state(TASK_INTERRUPTIBLE);
767 lirc_buffer_read(&ir->buf, buf);
768 ret = copy_to_user((void *)outbuf+written, buf,
770 written += ir->buf.chunk_size;
774 remove_wait_queue(&ir->buf.wait_poll, &wait);
775 set_current_state(TASK_RUNNING);
776 mutex_unlock(&ir->buf_lock);
778 dprintk("read result = %s (%d)\n",
779 ret ? "-EFAULT" : "OK", ret);
781 return ret ? ret : written;
784 /* send a keypress to the IR TX device */
785 static int send_code(struct IR *ir, unsigned int code, unsigned int key)
787 unsigned char data_block[TX_BLOCK_SIZE];
788 unsigned char buf[2];
791 /* Get data for the codeset/key */
792 ret = get_key_data(data_block, code, key);
794 if (ret == -EPROTO) {
795 zilog_error("failed to get data for code %u, key %u -- check "
796 "lircd.conf entries\n", code, key);
801 /* Send the data block */
802 ret = send_data_block(ir, data_block);
806 /* Send data block length? */
809 ret = i2c_master_send(&ir->c_tx, buf, 2);
811 zilog_error("i2c_master_send failed with %d\n", ret);
812 return ret < 0 ? ret : -EFAULT;
814 ret = i2c_master_send(&ir->c_tx, buf, 1);
816 zilog_error("i2c_master_send failed with %d\n", ret);
817 return ret < 0 ? ret : -EFAULT;
820 /* Send finished download? */
821 ret = i2c_master_recv(&ir->c_tx, buf, 1);
823 zilog_error("i2c_master_recv failed with %d\n", ret);
824 return ret < 0 ? ret : -EFAULT;
826 if (buf[0] != 0xA0) {
827 zilog_error("unexpected IR TX response #1: %02x\n",
832 /* Send prepare command? */
835 ret = i2c_master_send(&ir->c_tx, buf, 2);
837 zilog_error("i2c_master_send failed with %d\n", ret);
838 return ret < 0 ? ret : -EFAULT;
842 * The sleep bits aren't necessary on the HD PVR, and in fact, the
843 * last i2c_master_recv always fails with a -5, so for now, we're
844 * going to skip this whole mess and say we're done on the HD PVR
847 dprintk("sent code %u, key %u\n", code, key);
852 * This bit NAKs until the device is ready, so we retry it
853 * sleeping a bit each time. This seems to be what the windows
854 * driver does, approximately.
857 for (i = 0; i < 20; ++i) {
858 set_current_state(TASK_UNINTERRUPTIBLE);
859 schedule_timeout((50 * HZ + 999) / 1000);
860 ret = i2c_master_send(&ir->c_tx, buf, 1);
863 dprintk("NAK expected: i2c_master_send "
864 "failed with %d (try %d)\n", ret, i+1);
867 zilog_error("IR TX chip never got ready: last i2c_master_send "
868 "failed with %d\n", ret);
869 return ret < 0 ? ret : -EFAULT;
872 /* Seems to be an 'ok' response */
873 i = i2c_master_recv(&ir->c_tx, buf, 1);
875 zilog_error("i2c_master_recv failed with %d\n", ret);
878 if (buf[0] != 0x80) {
879 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
883 /* Oh good, it worked */
884 dprintk("sent code %u, key %u\n", code, key);
889 * Write a code to the device. We take in a 32-bit number (an int) and then
890 * decode this to a codeset/key index. The key data is then decompressed and
891 * sent to the device. We have a spin lock as per i2c documentation to prevent
892 * multiple concurrent sends which would probably cause the device to explode.
894 static ssize_t write(struct file *filep, const char *buf, size_t n,
897 struct IR *ir = filep->private_data;
901 if (ir->c_tx.addr == 0)
904 /* Validate user parameters */
908 /* Lock i2c bus for the duration */
909 mutex_lock(&ir->ir_lock);
911 /* Send each keypress */
912 for (i = 0; i < n;) {
916 if (copy_from_user(&command, buf + i, sizeof(command))) {
917 mutex_unlock(&ir->ir_lock);
921 /* Send boot data first if required */
922 if (ir->need_boot == 1) {
923 ret = send_boot_data(ir);
930 ret = send_code(ir, (unsigned)command >> 16,
931 (unsigned)command & 0xFFFF);
932 if (ret == -EPROTO) {
933 mutex_unlock(&ir->ir_lock);
939 * Hmm, a failure. If we've had a few then give up, otherwise
943 /* Looks like the chip crashed, reset it */
944 zilog_error("sending to the IR transmitter chip "
945 "failed, trying reset\n");
948 zilog_error("unable to send to the IR chip "
949 "after 3 resets, giving up\n");
950 mutex_unlock(&ir->ir_lock);
953 set_current_state(TASK_UNINTERRUPTIBLE);
954 schedule_timeout((100 * HZ + 999) / 1000);
961 /* Release i2c bus */
962 mutex_unlock(&ir->ir_lock);
968 /* copied from lirc_dev */
969 static unsigned int poll(struct file *filep, poll_table *wait)
971 struct IR *ir = filep->private_data;
974 dprintk("poll called\n");
975 if (ir->c_rx.addr == 0)
978 mutex_lock(&ir->buf_lock);
980 poll_wait(filep, &ir->buf.wait_poll, wait);
982 dprintk("poll result = %s\n",
983 lirc_buffer_empty(&ir->buf) ? "0" : "POLLIN|POLLRDNORM");
985 ret = lirc_buffer_empty(&ir->buf) ? 0 : (POLLIN|POLLRDNORM);
987 mutex_unlock(&ir->buf_lock);
991 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
993 struct IR *ir = filep->private_data;
995 unsigned long mode, features = 0;
997 if (ir->c_rx.addr != 0)
998 features |= LIRC_CAN_REC_LIRCCODE;
999 if (ir->c_tx.addr != 0)
1000 features |= LIRC_CAN_SEND_PULSE;
1003 case LIRC_GET_LENGTH:
1004 result = put_user((unsigned long)13,
1005 (unsigned long *)arg);
1007 case LIRC_GET_FEATURES:
1008 result = put_user(features, (unsigned long *) arg);
1010 case LIRC_GET_REC_MODE:
1011 if (!(features&LIRC_CAN_REC_MASK))
1014 result = put_user(LIRC_REC2MODE
1015 (features&LIRC_CAN_REC_MASK),
1016 (unsigned long *)arg);
1018 case LIRC_SET_REC_MODE:
1019 if (!(features&LIRC_CAN_REC_MASK))
1022 result = get_user(mode, (unsigned long *)arg);
1023 if (!result && !(LIRC_MODE2REC(mode) & features))
1026 case LIRC_GET_SEND_MODE:
1027 if (!(features&LIRC_CAN_SEND_MASK))
1030 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1032 case LIRC_SET_SEND_MODE:
1033 if (!(features&LIRC_CAN_SEND_MASK))
1036 result = get_user(mode, (unsigned long *) arg);
1037 if (!result && mode != LIRC_MODE_PULSE)
1047 * Open the IR device. Get hold of our IR structure and
1048 * stash it in private_data for the file
1050 static int open(struct inode *node, struct file *filep)
1055 /* find our IR struct */
1056 unsigned minor = MINOR(node->i_rdev);
1057 if (minor >= MAX_IRCTL_DEVICES) {
1058 dprintk("minor %d: open result = -ENODEV\n",
1062 ir = ir_devices[minor];
1064 /* increment in use count */
1065 mutex_lock(&ir->ir_lock);
1067 ret = set_use_inc(ir);
1070 mutex_unlock(&ir->ir_lock);
1073 mutex_unlock(&ir->ir_lock);
1075 /* stash our IR struct */
1076 filep->private_data = ir;
1081 /* Close the IR device */
1082 static int close(struct inode *node, struct file *filep)
1084 /* find our IR struct */
1085 struct IR *ir = filep->private_data;
1087 zilog_error("close: no private_data attached to the file!\n");
1091 /* decrement in use count */
1092 mutex_lock(&ir->ir_lock);
1095 mutex_unlock(&ir->ir_lock);
1100 static struct lirc_driver lirc_template = {
1101 .name = "lirc_zilog",
1102 .set_use_inc = set_use_inc,
1103 .set_use_dec = set_use_dec,
1104 .owner = THIS_MODULE
1107 static int ir_remove(struct i2c_client *client);
1108 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1109 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg);
1111 #define ID_FLAG_TX 0x01
1112 #define ID_FLAG_HDPVR 0x02
1114 static const struct i2c_device_id ir_transceiver_id[] = {
1115 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1116 { "ir_rx_z8f0811_haup", 0 },
1117 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1118 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1122 static struct i2c_driver driver = {
1124 .owner = THIS_MODULE,
1125 .name = "Zilog/Hauppauge i2c IR",
1128 .remove = ir_remove,
1129 .command = ir_command,
1130 .id_table = ir_transceiver_id,
1133 static const struct file_operations lirc_fops = {
1134 .owner = THIS_MODULE,
1139 .unlocked_ioctl = ioctl,
1140 #ifdef CONFIG_COMPAT
1141 .compat_ioctl = ioctl,
1147 static int ir_remove(struct i2c_client *client)
1149 struct IR *ir = i2c_get_clientdata(client);
1151 mutex_lock(&ir->ir_lock);
1153 if (ir->have_rx || ir->have_tx) {
1154 DECLARE_COMPLETION(tn);
1155 DECLARE_COMPLETION(tn2);
1157 /* end up polling thread */
1158 if (ir->task && !IS_ERR(ir->task)) {
1160 ir->t_notify2 = &tn2;
1162 wake_up_process(ir->task);
1164 wait_for_completion(&tn);
1165 ir->t_notify = NULL;
1166 ir->t_notify2 = NULL;
1170 mutex_unlock(&ir->ir_lock);
1171 zilog_error("%s: detached from something we didn't "
1172 "attach to\n", __func__);
1176 /* unregister lirc driver */
1177 if (ir->l.minor >= 0 && ir->l.minor < MAX_IRCTL_DEVICES) {
1178 lirc_unregister_driver(ir->l.minor);
1179 ir_devices[ir->l.minor] = NULL;
1183 lirc_buffer_free(&ir->buf);
1184 mutex_unlock(&ir->ir_lock);
1190 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1192 struct IR *ir = NULL;
1193 struct i2c_adapter *adap = client->adapter;
1196 int have_rx = 0, have_tx = 0;
1198 dprintk("%s: adapter name (%s) nr %d, i2c_device_id name (%s), "
1199 "client addr=0x%02x\n",
1200 __func__, adap->name, adap->nr, id->name, client->addr);
1203 * FIXME - This probe function probes both the Tx and Rx
1204 * addresses of the IR microcontroller.
1206 * However, the I2C subsystem is passing along one I2C client at a
1207 * time, based on matches to the ir_transceiver_id[] table above.
1208 * The expectation is that each i2c_client address will be probed
1209 * individually by drivers so the I2C subsystem can mark all client
1210 * addresses as claimed or not.
1212 * This probe routine causes only one of the client addresses, TX or RX,
1213 * to be claimed. This will cause a problem if the I2C subsystem is
1214 * subsequently triggered to probe unclaimed clients again.
1217 * The external IR receiver is at i2c address 0x71.
1218 * The IR transmitter is at 0x70.
1220 client->addr = 0x70;
1223 if (i2c_master_recv(client, &buf, 1) == 1)
1225 dprintk("probe 0x70 @ %s: %s\n",
1226 adap->name, have_tx ? "success" : "failed");
1230 client->addr = 0x71;
1231 if (i2c_master_recv(client, &buf, 1) == 1)
1233 dprintk("probe 0x71 @ %s: %s\n",
1234 adap->name, have_rx ? "success" : "failed");
1237 if (!(have_rx || have_tx)) {
1238 zilog_error("%s: no devices found\n", adap->name);
1242 printk(KERN_INFO "lirc_zilog: chip found with %s\n",
1243 have_rx && have_tx ? "RX and TX" :
1244 have_rx ? "RX only" : "TX only");
1246 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1251 ret = lirc_buffer_init(&ir->buf, 2, BUFLEN / 2);
1255 mutex_init(&ir->ir_lock);
1256 mutex_init(&ir->buf_lock);
1258 ir->is_hdpvr = (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1260 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1263 /* I2C attach to device */
1264 i2c_set_clientdata(client, ir);
1266 /* initialise RX device */
1268 DECLARE_COMPLETION(tn);
1269 memcpy(&ir->c_rx, client, sizeof(struct i2c_client));
1271 ir->c_rx.addr = 0x71;
1272 strlcpy(ir->c_rx.name, ZILOG_HAUPPAUGE_IR_RX_NAME,
1275 /* try to fire up polling thread */
1277 ir->task = kthread_run(lirc_thread, ir, "lirc_zilog");
1278 if (IS_ERR(ir->task)) {
1279 ret = PTR_ERR(ir->task);
1280 zilog_error("lirc_register_driver: cannot run "
1281 "poll thread %d\n", ret);
1284 wait_for_completion(&tn);
1285 ir->t_notify = NULL;
1289 /* initialise TX device */
1291 memcpy(&ir->c_tx, client, sizeof(struct i2c_client));
1292 ir->c_tx.addr = 0x70;
1293 strlcpy(ir->c_tx.name, ZILOG_HAUPPAUGE_IR_TX_NAME,
1298 /* set lirc_dev stuff */
1299 ir->l.code_length = 13;
1300 ir->l.rbuf = &ir->buf;
1301 ir->l.fops = &lirc_fops;
1303 ir->l.minor = minor;
1304 ir->l.dev = &adap->dev;
1305 ir->l.sample_rate = 0;
1307 /* register with lirc */
1308 ir->l.minor = lirc_register_driver(&ir->l);
1309 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1310 zilog_error("ir_attach: \"minor\" must be between 0 and %d "
1311 "(%d)!\n", MAX_IRCTL_DEVICES-1, ir->l.minor);
1316 /* store this for getting back in open() later on */
1317 ir_devices[ir->l.minor] = ir;
1320 * if we have the tx device, load the 'firmware'. We do this
1321 * after registering with lirc as otherwise hotplug seems to take
1322 * 10s to create the lirc device.
1325 /* Special TX init */
1334 /* undo everything, hopefully... */
1336 ir_remove(&ir->c_rx);
1338 ir_remove(&ir->c_tx);
1342 zilog_error("no device found\n");
1346 zilog_error("memory allocation failure\n");
1351 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
1357 static int __init zilog_init(void)
1361 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1363 mutex_init(&tx_data_lock);
1365 request_module("firmware_class");
1367 ret = i2c_add_driver(&driver);
1369 zilog_error("initialization failed\n");
1371 zilog_notify("initialization complete\n");
1376 static void __exit zilog_exit(void)
1378 i2c_del_driver(&driver);
1381 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1384 module_init(zilog_init);
1385 module_exit(zilog_exit);
1387 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1388 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1389 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver");
1390 MODULE_LICENSE("GPL");
1391 /* for compat with old name, which isn't all that accurate anymore */
1392 MODULE_ALIAS("lirc_pvr150");
1394 module_param(minor, int, 0444);
1395 MODULE_PARM_DESC(minor, "Preferred minor device number");
1397 module_param(debug, bool, 0644);
1398 MODULE_PARM_DESC(debug, "Enable debugging messages");
1400 module_param(disable_rx, bool, 0644);
1401 MODULE_PARM_DESC(disable_rx, "Disable the IR receiver device");
1403 module_param(disable_tx, bool, 0644);
1404 MODULE_PARM_DESC(disable_tx, "Disable the IR transmitter device");