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);
506 /* Hit the go button to activate the new 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;
516 * Wait for zilog to settle after hitting go post boot block upload.
517 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
518 * upon attempting to get firmware revision, and tx probe thus fails.
520 for (i = 0; i < 10; i++) {
521 ret = i2c_master_send(tx->c, buf, 1);
528 zilog_error("i2c_master_send failed with %d\n", ret);
529 return ret < 0 ? ret : -EFAULT;
532 /* Here comes the firmware version... (hopefully) */
533 ret = i2c_master_recv(tx->c, buf, 4);
535 zilog_error("i2c_master_recv failed with %d\n", ret);
538 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
539 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
542 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
543 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
548 /* unload "firmware", lock held */
549 static void fw_unload_locked(void)
552 if (tx_data->code_sets)
553 vfree(tx_data->code_sets);
556 vfree(tx_data->datap);
560 dprintk("successfully unloaded IR blaster firmware\n");
564 /* unload "firmware" for the IR TX device */
565 static void fw_unload(void)
567 mutex_lock(&tx_data_lock);
569 mutex_unlock(&tx_data_lock);
572 /* load "firmware" for the IR TX device */
573 static int fw_load(struct IR_tx *tx)
577 unsigned char *data, version, num_global_fixed;
578 const struct firmware *fw_entry;
580 /* Already loaded? */
581 mutex_lock(&tx_data_lock);
587 /* Request codeset data file */
588 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
590 zilog_error("firmware haup-ir-blaster.bin not available "
592 ret = ret < 0 ? ret : -EFAULT;
595 dprintk("firmware of size %zu loaded\n", fw_entry->size);
598 tx_data = vmalloc(sizeof(*tx_data));
599 if (tx_data == NULL) {
600 zilog_error("out of memory\n");
601 release_firmware(fw_entry);
605 tx_data->code_sets = NULL;
607 /* Copy the data so hotplug doesn't get confused and timeout */
608 tx_data->datap = vmalloc(fw_entry->size);
609 if (tx_data->datap == NULL) {
610 zilog_error("out of memory\n");
611 release_firmware(fw_entry);
616 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
617 tx_data->endp = tx_data->datap + fw_entry->size;
618 release_firmware(fw_entry); fw_entry = NULL;
621 data = tx_data->datap;
622 if (!read_uint8(&data, tx_data->endp, &version))
625 zilog_error("unsupported code set file version (%u, expected"
626 "1) -- please upgrade to a newer driver",
633 /* Save boot block for later */
634 tx_data->boot_data = data;
635 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
638 if (!read_uint32(&data, tx_data->endp,
639 &tx_data->num_code_sets))
642 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
644 tx_data->code_sets = vmalloc(
645 tx_data->num_code_sets * sizeof(char *));
646 if (tx_data->code_sets == NULL) {
652 for (i = 0; i < TX_BLOCK_SIZE; ++i)
653 tx_data->fixed[i] = -1;
655 /* Read global fixed data template */
656 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
657 num_global_fixed > TX_BLOCK_SIZE)
659 for (i = 0; i < num_global_fixed; ++i) {
660 unsigned char pos, val;
661 if (!read_uint8(&data, tx_data->endp, &pos) ||
662 !read_uint8(&data, tx_data->endp, &val) ||
663 pos >= TX_BLOCK_SIZE)
665 tx_data->fixed[pos] = (int)val;
668 /* Filch out the position of each code set */
669 for (i = 0; i < tx_data->num_code_sets; ++i) {
672 unsigned char ndiffs;
674 /* Save the codeset position */
675 tx_data->code_sets[i] = data;
678 if (!read_uint32(&data, tx_data->endp, &id) ||
679 !read_uint8(&data, tx_data->endp, &keys) ||
680 !read_uint8(&data, tx_data->endp, &ndiffs) ||
681 ndiffs > TX_BLOCK_SIZE || keys == 0)
684 /* skip diff positions */
685 if (!skip(&data, tx_data->endp, ndiffs))
689 * After the diffs we have the first key id + data -
692 if (!skip(&data, tx_data->endp,
693 1 + TX_BLOCK_SIZE - num_global_fixed))
696 /* Then we have keys-1 blocks of key id+diffs */
697 if (!skip(&data, tx_data->endp,
698 (ndiffs + 1) * (keys - 1)))
705 zilog_error("firmware is corrupt\n");
710 mutex_unlock(&tx_data_lock);
714 /* initialise the IR TX device */
715 static int tx_init(struct IR_tx *tx)
719 /* Load 'firmware' */
724 /* Send boot block */
725 ret = send_boot_data(tx);
734 /* do nothing stub to make LIRC happy */
735 static loff_t lseek(struct file *filep, loff_t offset, int orig)
740 /* copied from lirc_dev */
741 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
743 struct IR *ir = filep->private_data;
744 struct IR_rx *rx = ir->rx;
745 int ret = 0, written = 0;
746 DECLARE_WAITQUEUE(wait, current);
748 dprintk("read called\n");
752 if (mutex_lock_interruptible(&rx->buf_lock))
755 if (n % rx->buf.chunk_size) {
756 dprintk("read result = -EINVAL\n");
757 mutex_unlock(&rx->buf_lock);
762 * we add ourselves to the task queue before buffer check
763 * to avoid losing scan code (in case when queue is awaken somewhere
764 * between while condition checking and scheduling)
766 add_wait_queue(&rx->buf.wait_poll, &wait);
767 set_current_state(TASK_INTERRUPTIBLE);
770 * while we didn't provide 'length' bytes, device is opened in blocking
771 * mode and 'copy_to_user' is happy, wait for data.
773 while (written < n && ret == 0) {
774 if (lirc_buffer_empty(&rx->buf)) {
776 * According to the read(2) man page, 'written' can be
777 * returned as less than 'n', instead of blocking
778 * again, returning -EWOULDBLOCK, or returning
783 if (filep->f_flags & O_NONBLOCK) {
787 if (signal_pending(current)) {
792 set_current_state(TASK_INTERRUPTIBLE);
794 unsigned char buf[rx->buf.chunk_size];
795 lirc_buffer_read(&rx->buf, buf);
796 ret = copy_to_user((void *)outbuf+written, buf,
798 written += rx->buf.chunk_size;
802 remove_wait_queue(&rx->buf.wait_poll, &wait);
803 set_current_state(TASK_RUNNING);
804 mutex_unlock(&rx->buf_lock);
806 dprintk("read result = %s (%d)\n",
807 ret ? "-EFAULT" : "OK", ret);
809 return ret ? ret : written;
812 /* send a keypress to the IR TX device */
813 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
815 unsigned char data_block[TX_BLOCK_SIZE];
816 unsigned char buf[2];
819 /* Get data for the codeset/key */
820 ret = get_key_data(data_block, code, key);
822 if (ret == -EPROTO) {
823 zilog_error("failed to get data for code %u, key %u -- check "
824 "lircd.conf entries\n", code, key);
829 /* Send the data block */
830 ret = send_data_block(tx, data_block);
834 /* Send data block length? */
837 ret = i2c_master_send(tx->c, buf, 2);
839 zilog_error("i2c_master_send failed with %d\n", ret);
840 return ret < 0 ? ret : -EFAULT;
843 /* Give the z8 a moment to process data block */
844 for (i = 0; i < 10; i++) {
845 ret = i2c_master_send(tx->c, buf, 1);
852 zilog_error("i2c_master_send failed with %d\n", ret);
853 return ret < 0 ? ret : -EFAULT;
856 /* Send finished download? */
857 ret = i2c_master_recv(tx->c, buf, 1);
859 zilog_error("i2c_master_recv failed with %d\n", ret);
860 return ret < 0 ? ret : -EFAULT;
862 if (buf[0] != 0xA0) {
863 zilog_error("unexpected IR TX response #1: %02x\n",
868 /* Send prepare command? */
871 ret = i2c_master_send(tx->c, buf, 2);
873 zilog_error("i2c_master_send failed with %d\n", ret);
874 return ret < 0 ? ret : -EFAULT;
878 * The sleep bits aren't necessary on the HD PVR, and in fact, the
879 * last i2c_master_recv always fails with a -5, so for now, we're
880 * going to skip this whole mess and say we're done on the HD PVR
882 if (!tx->post_tx_ready_poll) {
883 dprintk("sent code %u, key %u\n", code, key);
888 * This bit NAKs until the device is ready, so we retry it
889 * sleeping a bit each time. This seems to be what the windows
890 * driver does, approximately.
893 for (i = 0; i < 20; ++i) {
894 set_current_state(TASK_UNINTERRUPTIBLE);
895 schedule_timeout((50 * HZ + 999) / 1000);
896 ret = i2c_master_send(tx->c, buf, 1);
899 dprintk("NAK expected: i2c_master_send "
900 "failed with %d (try %d)\n", ret, i+1);
903 zilog_error("IR TX chip never got ready: last i2c_master_send "
904 "failed with %d\n", ret);
905 return ret < 0 ? ret : -EFAULT;
908 /* Seems to be an 'ok' response */
909 i = i2c_master_recv(tx->c, buf, 1);
911 zilog_error("i2c_master_recv failed with %d\n", ret);
914 if (buf[0] != 0x80) {
915 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
919 /* Oh good, it worked */
920 dprintk("sent code %u, key %u\n", code, key);
925 * Write a code to the device. We take in a 32-bit number (an int) and then
926 * decode this to a codeset/key index. The key data is then decompressed and
927 * sent to the device. We have a spin lock as per i2c documentation to prevent
928 * multiple concurrent sends which would probably cause the device to explode.
930 static ssize_t write(struct file *filep, const char *buf, size_t n,
933 struct IR *ir = filep->private_data;
934 struct IR_tx *tx = ir->tx;
941 /* Validate user parameters */
945 /* Lock i2c bus for the duration */
946 mutex_lock(&ir->ir_lock);
948 /* Send each keypress */
949 for (i = 0; i < n;) {
953 if (copy_from_user(&command, buf + i, sizeof(command))) {
954 mutex_unlock(&ir->ir_lock);
958 /* Send boot data first if required */
959 if (tx->need_boot == 1) {
960 ret = send_boot_data(tx);
967 ret = send_code(tx, (unsigned)command >> 16,
968 (unsigned)command & 0xFFFF);
969 if (ret == -EPROTO) {
970 mutex_unlock(&ir->ir_lock);
976 * Hmm, a failure. If we've had a few then give up, otherwise
980 /* Looks like the chip crashed, reset it */
981 zilog_error("sending to the IR transmitter chip "
982 "failed, trying reset\n");
985 zilog_error("unable to send to the IR chip "
986 "after 3 resets, giving up\n");
987 mutex_unlock(&ir->ir_lock);
990 set_current_state(TASK_UNINTERRUPTIBLE);
991 schedule_timeout((100 * HZ + 999) / 1000);
998 /* Release i2c bus */
999 mutex_unlock(&ir->ir_lock);
1001 /* All looks good */
1005 /* copied from lirc_dev */
1006 static unsigned int poll(struct file *filep, poll_table *wait)
1008 struct IR *ir = filep->private_data;
1009 struct IR_rx *rx = ir->rx;
1012 dprintk("poll called\n");
1016 mutex_lock(&rx->buf_lock);
1018 poll_wait(filep, &rx->buf.wait_poll, wait);
1020 dprintk("poll result = %s\n",
1021 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1023 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1025 mutex_unlock(&rx->buf_lock);
1029 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1031 struct IR *ir = filep->private_data;
1033 unsigned long mode, features = 0;
1035 features |= LIRC_CAN_SEND_PULSE;
1037 features |= LIRC_CAN_REC_LIRCCODE;
1040 case LIRC_GET_LENGTH:
1041 result = put_user((unsigned long)13,
1042 (unsigned long *)arg);
1044 case LIRC_GET_FEATURES:
1045 result = put_user(features, (unsigned long *) arg);
1047 case LIRC_GET_REC_MODE:
1048 if (!(features&LIRC_CAN_REC_MASK))
1051 result = put_user(LIRC_REC2MODE
1052 (features&LIRC_CAN_REC_MASK),
1053 (unsigned long *)arg);
1055 case LIRC_SET_REC_MODE:
1056 if (!(features&LIRC_CAN_REC_MASK))
1059 result = get_user(mode, (unsigned long *)arg);
1060 if (!result && !(LIRC_MODE2REC(mode) & features))
1063 case LIRC_GET_SEND_MODE:
1064 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1066 case LIRC_SET_SEND_MODE:
1067 result = get_user(mode, (unsigned long *) arg);
1068 if (!result && mode != LIRC_MODE_PULSE)
1077 /* ir_devices_lock must be held */
1078 static struct IR *find_ir_device_by_minor(unsigned int minor)
1080 if (minor >= MAX_IRCTL_DEVICES)
1083 return ir_devices[minor];
1087 * Open the IR device. Get hold of our IR structure and
1088 * stash it in private_data for the file
1090 static int open(struct inode *node, struct file *filep)
1094 unsigned int minor = MINOR(node->i_rdev);
1096 /* find our IR struct */
1097 mutex_lock(&ir_devices_lock);
1098 ir = find_ir_device_by_minor(minor);
1099 mutex_unlock(&ir_devices_lock);
1104 /* increment in use count */
1105 mutex_lock(&ir->ir_lock);
1107 ret = set_use_inc(ir);
1110 mutex_unlock(&ir->ir_lock);
1113 mutex_unlock(&ir->ir_lock);
1115 /* stash our IR struct */
1116 filep->private_data = ir;
1121 /* Close the IR device */
1122 static int close(struct inode *node, struct file *filep)
1124 /* find our IR struct */
1125 struct IR *ir = filep->private_data;
1127 zilog_error("close: no private_data attached to the file!\n");
1131 /* decrement in use count */
1132 mutex_lock(&ir->ir_lock);
1135 mutex_unlock(&ir->ir_lock);
1140 static struct lirc_driver lirc_template = {
1141 .name = "lirc_zilog",
1142 .set_use_inc = set_use_inc,
1143 .set_use_dec = set_use_dec,
1144 .owner = THIS_MODULE
1147 static int ir_remove(struct i2c_client *client);
1148 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1150 #define ID_FLAG_TX 0x01
1151 #define ID_FLAG_HDPVR 0x02
1153 static const struct i2c_device_id ir_transceiver_id[] = {
1154 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1155 { "ir_rx_z8f0811_haup", 0 },
1156 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1157 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1161 static struct i2c_driver driver = {
1163 .owner = THIS_MODULE,
1164 .name = "Zilog/Hauppauge i2c IR",
1167 .remove = ir_remove,
1168 .id_table = ir_transceiver_id,
1171 static const struct file_operations lirc_fops = {
1172 .owner = THIS_MODULE,
1177 .unlocked_ioctl = ioctl,
1178 #ifdef CONFIG_COMPAT
1179 .compat_ioctl = ioctl,
1185 static void destroy_rx_kthread(struct IR_rx *rx)
1187 /* end up polling thread */
1188 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1189 kthread_stop(rx->task);
1194 /* ir_devices_lock must be held */
1195 static int add_ir_device(struct IR *ir)
1199 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1200 if (ir_devices[i] == NULL) {
1205 return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1208 /* ir_devices_lock must be held */
1209 static void del_ir_device(struct IR *ir)
1213 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1214 if (ir_devices[i] == ir) {
1215 ir_devices[i] = NULL;
1220 static int ir_remove(struct i2c_client *client)
1222 struct IR *ir = i2c_get_clientdata(client);
1224 mutex_lock(&ir_devices_lock);
1227 /* We destroyed everything when the first client came through */
1228 mutex_unlock(&ir_devices_lock);
1233 lirc_unregister_driver(ir->l.minor);
1236 destroy_rx_kthread(ir->rx);
1237 if (ir->rx != NULL) {
1238 if (ir->rx->buf.fifo_initialized)
1239 lirc_buffer_free(&ir->rx->buf);
1240 i2c_set_clientdata(ir->rx->c, NULL);
1245 i2c_set_clientdata(ir->tx->c, NULL);
1252 mutex_unlock(&ir_devices_lock);
1257 /* ir_devices_lock must be held */
1258 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1261 struct IR *ir = NULL;
1263 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1264 if (ir_devices[i] != NULL &&
1265 ir_devices[i]->adapter == adapter) {
1273 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1276 struct i2c_adapter *adap = client->adapter;
1278 bool tx_probe = false;
1280 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1281 __func__, id->name, adap->nr, adap->name, client->addr);
1284 * The IR receiver is at i2c address 0x71.
1285 * The IR transmitter is at i2c address 0x70.
1288 if (id->driver_data & ID_FLAG_TX)
1290 else if (tx_only) /* module option */
1293 zilog_info("probing IR %s on %s (i2c-%d)\n",
1294 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1296 mutex_lock(&ir_devices_lock);
1298 /* Use a single struct IR instance for both the Rx and Tx functions */
1299 ir = find_ir_device_by_adapter(adap);
1301 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1306 /* store for use in ir_probe() again, and open() later on */
1307 ret = add_ir_device(ir);
1312 mutex_init(&ir->ir_lock);
1314 /* set lirc_dev stuff */
1315 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1316 ir->l.minor = minor; /* module option */
1317 ir->l.code_length = 13;
1319 ir->l.fops = &lirc_fops;
1321 ir->l.dev = &adap->dev;
1322 ir->l.sample_rate = 0;
1326 /* Set up a struct IR_tx instance */
1327 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1328 if (ir->tx == NULL) {
1334 ir->tx->need_boot = 1;
1335 ir->tx->post_tx_ready_poll =
1336 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1338 /* Set up a struct IR_rx instance */
1339 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1340 if (ir->rx == NULL) {
1345 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1349 mutex_init(&ir->rx->buf_lock);
1351 ir->rx->hdpvr_data_fmt =
1352 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1354 /* set lirc_dev stuff */
1355 ir->l.rbuf = &ir->rx->buf;
1358 i2c_set_clientdata(client, ir);
1360 /* Proceed only if we have the required Tx and Rx clients ready to go */
1361 if (ir->tx == NULL ||
1362 (ir->rx == NULL && !tx_only)) {
1363 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1364 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1365 adap->nr, tx_probe ? "Rx" : "Tx");
1369 /* initialise RX device */
1370 if (ir->rx != NULL) {
1371 /* try to fire up polling thread */
1372 ir->rx->task = kthread_run(lirc_thread, ir,
1373 "zilog-rx-i2c-%d", adap->nr);
1374 if (IS_ERR(ir->rx->task)) {
1375 ret = PTR_ERR(ir->rx->task);
1376 zilog_error("%s: could not start IR Rx polling thread"
1382 /* register with lirc */
1383 ir->l.minor = lirc_register_driver(&ir->l);
1384 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1385 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1386 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1388 goto out_free_thread;
1392 * if we have the tx device, load the 'firmware'. We do this
1393 * after registering with lirc as otherwise hotplug seems to take
1394 * 10s to create the lirc device.
1396 ret = tx_init(ir->tx);
1398 goto out_unregister;
1400 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1401 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1403 mutex_unlock(&ir_devices_lock);
1407 lirc_unregister_driver(ir->l.minor);
1409 destroy_rx_kthread(ir->rx);
1411 if (ir->rx != NULL) {
1412 if (ir->rx->buf.fifo_initialized)
1413 lirc_buffer_free(&ir->rx->buf);
1414 if (ir->rx->c != NULL)
1415 i2c_set_clientdata(ir->rx->c, NULL);
1418 if (ir->tx != NULL) {
1419 if (ir->tx->c != NULL)
1420 i2c_set_clientdata(ir->tx->c, NULL);
1427 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1428 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1430 mutex_unlock(&ir_devices_lock);
1434 static int __init zilog_init(void)
1438 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1440 mutex_init(&tx_data_lock);
1441 mutex_init(&ir_devices_lock);
1443 request_module("firmware_class");
1445 ret = i2c_add_driver(&driver);
1447 zilog_error("initialization failed\n");
1449 zilog_notify("initialization complete\n");
1454 static void __exit zilog_exit(void)
1456 i2c_del_driver(&driver);
1459 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1462 module_init(zilog_init);
1463 module_exit(zilog_exit);
1465 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1466 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1467 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1469 MODULE_LICENSE("GPL");
1470 /* for compat with old name, which isn't all that accurate anymore */
1471 MODULE_ALIAS("lirc_pvr150");
1473 module_param(minor, int, 0444);
1474 MODULE_PARM_DESC(minor, "Preferred minor device number");
1476 module_param(debug, bool, 0644);
1477 MODULE_PARM_DESC(debug, "Enable debugging messages");
1479 module_param(tx_only, bool, 0644);
1480 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");