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 polling thread data */
71 struct task_struct *task;
82 /* TX additional actions needed */
84 bool post_tx_ready_poll;
88 struct list_head list;
91 struct lirc_buffer rbuf;
96 struct i2c_adapter *adapter;
101 /* IR transceiver instance object list */
102 static DEFINE_MUTEX(ir_devices_lock);
103 static LIST_HEAD(ir_devices_list);
105 /* Block size for IR transmitter */
106 #define TX_BLOCK_SIZE 99
108 /* Hauppauge IR transmitter data */
109 struct tx_data_struct {
111 unsigned char *boot_data;
113 /* Start of binary data block */
114 unsigned char *datap;
116 /* End of binary data block */
119 /* Number of installed codesets */
120 unsigned int num_code_sets;
122 /* Pointers to codesets */
123 unsigned char **code_sets;
125 /* Global fixed data template */
126 int fixed[TX_BLOCK_SIZE];
129 static struct tx_data_struct *tx_data;
130 static struct mutex tx_data_lock;
132 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
134 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
135 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
137 /* module parameters */
138 static int debug; /* debug output */
139 static int tx_only; /* only handle the IR Tx function */
140 static int minor = -1; /* minor number */
142 #define dprintk(fmt, args...) \
145 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
149 static int add_to_buf(struct IR *ir)
152 unsigned char codes[2];
153 unsigned char keybuf[6];
157 unsigned char sendbuf[1] = { 0 };
158 struct lirc_buffer *rbuf = ir->l.rbuf;
159 struct IR_rx *rx = ir->rx;
164 if (lirc_buffer_full(rbuf)) {
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);
213 ir->tx->need_boot = 1;
216 mutex_unlock(&ir->ir_lock);
220 if (kthread_should_stop()) {
221 mutex_unlock(&ir->ir_lock);
224 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
225 mutex_unlock(&ir->ir_lock);
226 if (ret != sizeof(keybuf)) {
227 zilog_error("i2c_master_recv failed with %d -- "
228 "keeping last read buffer\n", ret);
230 rx->b[0] = keybuf[3];
231 rx->b[1] = keybuf[4];
232 rx->b[2] = keybuf[5];
233 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
237 if (rx->hdpvr_data_fmt) {
238 if (got_data && (keybuf[0] == 0x80))
240 else if (got_data && (keybuf[0] == 0x00))
242 } else if ((rx->b[0] & 0x80) == 0)
243 return got_data ? 0 : -ENODATA;
245 /* look what we have */
246 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
248 codes[0] = (code >> 8) & 0xff;
249 codes[1] = code & 0xff;
252 lirc_buffer_write(rbuf, codes);
254 } while (!lirc_buffer_full(rbuf));
260 * Main function of the polling thread -- from lirc_dev.
261 * We don't fit the LIRC model at all anymore. This is horrible, but
262 * basically we have a single RX/TX device with a nasty failure mode
263 * that needs to be accounted for across the pair. lirc lets us provide
264 * fops, but prevents us from using the internal polling, etc. if we do
265 * so. Hence the replication. Might be neater to extend the LIRC model
266 * to account for this but I'd think it's a very special case of seriously
267 * messed up hardware.
269 static int lirc_thread(void *arg)
272 struct lirc_buffer *rbuf = ir->l.rbuf;
274 dprintk("poll thread started\n");
276 while (!kthread_should_stop()) {
277 set_current_state(TASK_INTERRUPTIBLE);
279 /* if device not opened, we can sleep half a second */
280 if (atomic_read(&ir->open_count) == 0) {
281 schedule_timeout(HZ/2);
286 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
287 * We use this interval as the chip resets every time you poll
288 * it (bad!). This is therefore just sufficient to catch all
289 * of the button presses. It makes the remote much more
290 * responsive. You can see the difference by running irw and
291 * holding down a button. With 100ms, the old polling
292 * interval, you'll notice breaks in the repeat sequence
293 * corresponding to lost keypresses.
295 schedule_timeout((260 * HZ) / 1000);
296 if (kthread_should_stop())
299 wake_up_interruptible(&rbuf->wait_poll);
302 dprintk("poll thread ended\n");
306 static int set_use_inc(void *data)
311 static void set_use_dec(void *data)
316 /* safe read of a uint32 (always network byte order) */
317 static int read_uint32(unsigned char **data,
318 unsigned char *endp, unsigned int *val)
320 if (*data + 4 > endp)
322 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
323 ((*data)[2] << 8) | (*data)[3];
328 /* safe read of a uint8 */
329 static int read_uint8(unsigned char **data,
330 unsigned char *endp, unsigned char *val)
332 if (*data + 1 > endp)
338 /* safe skipping of N bytes */
339 static int skip(unsigned char **data,
340 unsigned char *endp, unsigned int distance)
342 if (*data + distance > endp)
348 /* decompress key data into the given buffer */
349 static int get_key_data(unsigned char *buf,
350 unsigned int codeset, unsigned int key)
352 unsigned char *data, *endp, *diffs, *key_block;
353 unsigned char keys, ndiffs, id;
354 unsigned int base, lim, pos, i;
356 /* Binary search for the codeset */
357 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
358 pos = base + (lim >> 1);
359 data = tx_data->code_sets[pos];
361 if (!read_uint32(&data, tx_data->endp, &i))
366 else if (codeset > i) {
375 /* Set end of data block */
376 endp = pos < tx_data->num_code_sets - 1 ?
377 tx_data->code_sets[pos + 1] : tx_data->endp;
379 /* Read the block header */
380 if (!read_uint8(&data, endp, &keys) ||
381 !read_uint8(&data, endp, &ndiffs) ||
382 ndiffs > TX_BLOCK_SIZE || keys == 0)
385 /* Save diffs & skip */
387 if (!skip(&data, endp, ndiffs))
390 /* Read the id of the first key */
391 if (!read_uint8(&data, endp, &id))
394 /* Unpack the first key's data */
395 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
396 if (tx_data->fixed[i] == -1) {
397 if (!read_uint8(&data, endp, &buf[i]))
400 buf[i] = (unsigned char)tx_data->fixed[i];
404 /* Early out key found/not found */
412 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
415 /* Binary search for the key */
416 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
418 unsigned char *key_data;
419 pos = base + (lim >> 1);
420 key_data = key_block + (ndiffs + 1) * pos;
422 if (*key_data == key) {
426 /* found, so unpack the diffs */
427 for (i = 0; i < ndiffs; ++i) {
429 if (!read_uint8(&key_data, endp, &val) ||
430 diffs[i] >= TX_BLOCK_SIZE)
436 } else if (key > *key_data) {
445 zilog_error("firmware is corrupt\n");
449 /* send a block of data to the IR TX device */
450 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
453 unsigned char buf[5];
455 for (i = 0; i < TX_BLOCK_SIZE;) {
456 int tosend = TX_BLOCK_SIZE - i;
459 buf[0] = (unsigned char)(i + 1);
460 for (j = 0; j < tosend; ++j)
461 buf[1 + j] = data_block[i + j];
462 dprintk("%02x %02x %02x %02x %02x",
463 buf[0], buf[1], buf[2], buf[3], buf[4]);
464 ret = i2c_master_send(tx->c, buf, tosend + 1);
465 if (ret != tosend + 1) {
466 zilog_error("i2c_master_send failed with %d\n", ret);
467 return ret < 0 ? ret : -EFAULT;
474 /* send boot data to the IR TX device */
475 static int send_boot_data(struct IR_tx *tx)
478 unsigned char buf[4];
480 /* send the boot block */
481 ret = send_data_block(tx, tx_data->boot_data);
485 /* Hit the go button to activate the new boot data */
488 ret = i2c_master_send(tx->c, buf, 2);
490 zilog_error("i2c_master_send failed with %d\n", ret);
491 return ret < 0 ? ret : -EFAULT;
495 * Wait for zilog to settle after hitting go post boot block upload.
496 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
497 * upon attempting to get firmware revision, and tx probe thus fails.
499 for (i = 0; i < 10; i++) {
500 ret = i2c_master_send(tx->c, buf, 1);
507 zilog_error("i2c_master_send failed with %d\n", ret);
508 return ret < 0 ? ret : -EFAULT;
511 /* Here comes the firmware version... (hopefully) */
512 ret = i2c_master_recv(tx->c, buf, 4);
514 zilog_error("i2c_master_recv failed with %d\n", ret);
517 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
518 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
521 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
522 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
527 /* unload "firmware", lock held */
528 static void fw_unload_locked(void)
531 if (tx_data->code_sets)
532 vfree(tx_data->code_sets);
535 vfree(tx_data->datap);
539 dprintk("successfully unloaded IR blaster firmware\n");
543 /* unload "firmware" for the IR TX device */
544 static void fw_unload(void)
546 mutex_lock(&tx_data_lock);
548 mutex_unlock(&tx_data_lock);
551 /* load "firmware" for the IR TX device */
552 static int fw_load(struct IR_tx *tx)
556 unsigned char *data, version, num_global_fixed;
557 const struct firmware *fw_entry;
559 /* Already loaded? */
560 mutex_lock(&tx_data_lock);
566 /* Request codeset data file */
567 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
569 zilog_error("firmware haup-ir-blaster.bin not available "
571 ret = ret < 0 ? ret : -EFAULT;
574 dprintk("firmware of size %zu loaded\n", fw_entry->size);
577 tx_data = vmalloc(sizeof(*tx_data));
578 if (tx_data == NULL) {
579 zilog_error("out of memory\n");
580 release_firmware(fw_entry);
584 tx_data->code_sets = NULL;
586 /* Copy the data so hotplug doesn't get confused and timeout */
587 tx_data->datap = vmalloc(fw_entry->size);
588 if (tx_data->datap == NULL) {
589 zilog_error("out of memory\n");
590 release_firmware(fw_entry);
595 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
596 tx_data->endp = tx_data->datap + fw_entry->size;
597 release_firmware(fw_entry); fw_entry = NULL;
600 data = tx_data->datap;
601 if (!read_uint8(&data, tx_data->endp, &version))
604 zilog_error("unsupported code set file version (%u, expected"
605 "1) -- please upgrade to a newer driver",
612 /* Save boot block for later */
613 tx_data->boot_data = data;
614 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
617 if (!read_uint32(&data, tx_data->endp,
618 &tx_data->num_code_sets))
621 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
623 tx_data->code_sets = vmalloc(
624 tx_data->num_code_sets * sizeof(char *));
625 if (tx_data->code_sets == NULL) {
631 for (i = 0; i < TX_BLOCK_SIZE; ++i)
632 tx_data->fixed[i] = -1;
634 /* Read global fixed data template */
635 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
636 num_global_fixed > TX_BLOCK_SIZE)
638 for (i = 0; i < num_global_fixed; ++i) {
639 unsigned char pos, val;
640 if (!read_uint8(&data, tx_data->endp, &pos) ||
641 !read_uint8(&data, tx_data->endp, &val) ||
642 pos >= TX_BLOCK_SIZE)
644 tx_data->fixed[pos] = (int)val;
647 /* Filch out the position of each code set */
648 for (i = 0; i < tx_data->num_code_sets; ++i) {
651 unsigned char ndiffs;
653 /* Save the codeset position */
654 tx_data->code_sets[i] = data;
657 if (!read_uint32(&data, tx_data->endp, &id) ||
658 !read_uint8(&data, tx_data->endp, &keys) ||
659 !read_uint8(&data, tx_data->endp, &ndiffs) ||
660 ndiffs > TX_BLOCK_SIZE || keys == 0)
663 /* skip diff positions */
664 if (!skip(&data, tx_data->endp, ndiffs))
668 * After the diffs we have the first key id + data -
671 if (!skip(&data, tx_data->endp,
672 1 + TX_BLOCK_SIZE - num_global_fixed))
675 /* Then we have keys-1 blocks of key id+diffs */
676 if (!skip(&data, tx_data->endp,
677 (ndiffs + 1) * (keys - 1)))
684 zilog_error("firmware is corrupt\n");
689 mutex_unlock(&tx_data_lock);
693 /* initialise the IR TX device */
694 static int tx_init(struct IR_tx *tx)
698 /* Load 'firmware' */
703 /* Send boot block */
704 ret = send_boot_data(tx);
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 struct IR_rx *rx = ir->rx;
718 struct lirc_buffer *rbuf = ir->l.rbuf;
719 int ret = 0, written = 0;
721 DECLARE_WAITQUEUE(wait, current);
723 dprintk("read called\n");
727 if (n % rbuf->chunk_size) {
728 dprintk("read result = -EINVAL\n");
733 * we add ourselves to the task queue before buffer check
734 * to avoid losing scan code (in case when queue is awaken somewhere
735 * between while condition checking and scheduling)
737 add_wait_queue(&rbuf->wait_poll, &wait);
738 set_current_state(TASK_INTERRUPTIBLE);
741 * while we didn't provide 'length' bytes, device is opened in blocking
742 * mode and 'copy_to_user' is happy, wait for data.
744 while (written < n && ret == 0) {
745 if (lirc_buffer_empty(rbuf)) {
747 * According to the read(2) man page, 'written' can be
748 * returned as less than 'n', instead of blocking
749 * again, returning -EWOULDBLOCK, or returning
754 if (filep->f_flags & O_NONBLOCK) {
758 if (signal_pending(current)) {
763 set_current_state(TASK_INTERRUPTIBLE);
765 unsigned char buf[rbuf->chunk_size];
766 m = lirc_buffer_read(rbuf, buf);
767 if (m == rbuf->chunk_size) {
768 ret = copy_to_user((void *)outbuf+written, buf,
770 written += rbuf->chunk_size;
775 remove_wait_queue(&rbuf->wait_poll, &wait);
776 set_current_state(TASK_RUNNING);
778 dprintk("read result = %d (%s)\n", ret, ret ? "Error" : "OK");
780 return ret ? ret : written;
783 /* send a keypress to the IR TX device */
784 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
786 unsigned char data_block[TX_BLOCK_SIZE];
787 unsigned char buf[2];
790 /* Get data for the codeset/key */
791 ret = get_key_data(data_block, code, key);
793 if (ret == -EPROTO) {
794 zilog_error("failed to get data for code %u, key %u -- check "
795 "lircd.conf entries\n", code, key);
800 /* Send the data block */
801 ret = send_data_block(tx, data_block);
805 /* Send data block length? */
808 ret = i2c_master_send(tx->c, buf, 2);
810 zilog_error("i2c_master_send failed with %d\n", ret);
811 return ret < 0 ? ret : -EFAULT;
814 /* Give the z8 a moment to process data block */
815 for (i = 0; i < 10; i++) {
816 ret = i2c_master_send(tx->c, buf, 1);
823 zilog_error("i2c_master_send failed with %d\n", ret);
824 return ret < 0 ? ret : -EFAULT;
827 /* Send finished download? */
828 ret = i2c_master_recv(tx->c, buf, 1);
830 zilog_error("i2c_master_recv failed with %d\n", ret);
831 return ret < 0 ? ret : -EFAULT;
833 if (buf[0] != 0xA0) {
834 zilog_error("unexpected IR TX response #1: %02x\n",
839 /* Send prepare command? */
842 ret = i2c_master_send(tx->c, buf, 2);
844 zilog_error("i2c_master_send failed with %d\n", ret);
845 return ret < 0 ? ret : -EFAULT;
849 * The sleep bits aren't necessary on the HD PVR, and in fact, the
850 * last i2c_master_recv always fails with a -5, so for now, we're
851 * going to skip this whole mess and say we're done on the HD PVR
853 if (!tx->post_tx_ready_poll) {
854 dprintk("sent code %u, key %u\n", code, key);
859 * This bit NAKs until the device is ready, so we retry it
860 * sleeping a bit each time. This seems to be what the windows
861 * driver does, approximately.
864 for (i = 0; i < 20; ++i) {
865 set_current_state(TASK_UNINTERRUPTIBLE);
866 schedule_timeout((50 * HZ + 999) / 1000);
867 ret = i2c_master_send(tx->c, buf, 1);
870 dprintk("NAK expected: i2c_master_send "
871 "failed with %d (try %d)\n", ret, i+1);
874 zilog_error("IR TX chip never got ready: last i2c_master_send "
875 "failed with %d\n", ret);
876 return ret < 0 ? ret : -EFAULT;
879 /* Seems to be an 'ok' response */
880 i = i2c_master_recv(tx->c, buf, 1);
882 zilog_error("i2c_master_recv failed with %d\n", ret);
885 if (buf[0] != 0x80) {
886 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
890 /* Oh good, it worked */
891 dprintk("sent code %u, key %u\n", code, key);
896 * Write a code to the device. We take in a 32-bit number (an int) and then
897 * decode this to a codeset/key index. The key data is then decompressed and
898 * sent to the device. We have a spin lock as per i2c documentation to prevent
899 * multiple concurrent sends which would probably cause the device to explode.
901 static ssize_t write(struct file *filep, const char *buf, size_t n,
904 struct IR *ir = filep->private_data;
905 struct IR_tx *tx = ir->tx;
912 /* Validate user parameters */
916 /* Lock i2c bus for the duration */
917 mutex_lock(&ir->ir_lock);
919 /* Send each keypress */
920 for (i = 0; i < n;) {
924 if (copy_from_user(&command, buf + i, sizeof(command))) {
925 mutex_unlock(&ir->ir_lock);
929 /* Send boot data first if required */
930 if (tx->need_boot == 1) {
931 ret = send_boot_data(tx);
938 ret = send_code(tx, (unsigned)command >> 16,
939 (unsigned)command & 0xFFFF);
940 if (ret == -EPROTO) {
941 mutex_unlock(&ir->ir_lock);
947 * Hmm, a failure. If we've had a few then give up, otherwise
951 /* Looks like the chip crashed, reset it */
952 zilog_error("sending to the IR transmitter chip "
953 "failed, trying reset\n");
956 zilog_error("unable to send to the IR chip "
957 "after 3 resets, giving up\n");
958 mutex_unlock(&ir->ir_lock);
961 set_current_state(TASK_UNINTERRUPTIBLE);
962 schedule_timeout((100 * HZ + 999) / 1000);
969 /* Release i2c bus */
970 mutex_unlock(&ir->ir_lock);
976 /* copied from lirc_dev */
977 static unsigned int poll(struct file *filep, poll_table *wait)
979 struct IR *ir = filep->private_data;
980 struct IR_rx *rx = ir->rx;
981 struct lirc_buffer *rbuf = ir->l.rbuf;
984 dprintk("poll called\n");
988 * Revisit this, if our poll function ever reports writeable
991 dprintk("poll result = POLLERR\n");
996 * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
997 * that buffer's wait queue indicates we may have a new poll status.
999 poll_wait(filep, &rbuf->wait_poll, wait);
1001 /* Indicate what ops could happen immediately without blocking */
1002 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN|POLLRDNORM);
1004 dprintk("poll result = %s\n", ret ? "POLLIN|POLLRDNORM" : "none");
1008 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1010 struct IR *ir = filep->private_data;
1012 unsigned long mode, features = 0;
1015 features |= LIRC_CAN_REC_LIRCCODE;
1017 features |= LIRC_CAN_SEND_PULSE;
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 if (!(features&LIRC_CAN_SEND_MASK))
1047 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1049 case LIRC_SET_SEND_MODE:
1050 if (!(features&LIRC_CAN_SEND_MASK))
1053 result = get_user(mode, (unsigned long *) arg);
1054 if (!result && mode != LIRC_MODE_PULSE)
1063 /* ir_devices_lock must be held */
1064 static struct IR *find_ir_device_by_minor(unsigned int minor)
1068 if (list_empty(&ir_devices_list))
1071 list_for_each_entry(ir, &ir_devices_list, list)
1072 if (ir->l.minor == minor)
1079 * Open the IR device. Get hold of our IR structure and
1080 * stash it in private_data for the file
1082 static int open(struct inode *node, struct file *filep)
1085 unsigned int minor = MINOR(node->i_rdev);
1087 /* find our IR struct */
1088 mutex_lock(&ir_devices_lock);
1089 ir = find_ir_device_by_minor(minor);
1090 mutex_unlock(&ir_devices_lock);
1095 atomic_inc(&ir->open_count);
1097 /* stash our IR struct */
1098 filep->private_data = ir;
1100 nonseekable_open(node, filep);
1104 /* Close the IR device */
1105 static int close(struct inode *node, struct file *filep)
1107 /* find our IR struct */
1108 struct IR *ir = filep->private_data;
1110 zilog_error("close: no private_data attached to the file!\n");
1114 atomic_dec(&ir->open_count);
1119 static struct lirc_driver lirc_template = {
1120 .name = "lirc_zilog",
1121 .set_use_inc = set_use_inc,
1122 .set_use_dec = set_use_dec,
1123 .owner = THIS_MODULE
1126 static int ir_remove(struct i2c_client *client);
1127 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1129 #define ID_FLAG_TX 0x01
1130 #define ID_FLAG_HDPVR 0x02
1132 static const struct i2c_device_id ir_transceiver_id[] = {
1133 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1134 { "ir_rx_z8f0811_haup", 0 },
1135 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1136 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1140 static struct i2c_driver driver = {
1142 .owner = THIS_MODULE,
1143 .name = "Zilog/Hauppauge i2c IR",
1146 .remove = ir_remove,
1147 .id_table = ir_transceiver_id,
1150 static const struct file_operations lirc_fops = {
1151 .owner = THIS_MODULE,
1152 .llseek = no_llseek,
1156 .unlocked_ioctl = ioctl,
1157 #ifdef CONFIG_COMPAT
1158 .compat_ioctl = ioctl,
1164 static void destroy_rx_kthread(struct IR_rx *rx)
1166 /* end up polling thread */
1167 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1168 kthread_stop(rx->task);
1173 /* ir_devices_lock must be held */
1174 static int add_ir_device(struct IR *ir)
1176 list_add_tail(&ir->list, &ir_devices_list);
1180 /* ir_devices_lock must be held */
1181 static void del_ir_device(struct IR *ir)
1185 if (list_empty(&ir_devices_list))
1188 list_for_each_entry(p, &ir_devices_list, list)
1195 static int ir_remove(struct i2c_client *client)
1197 struct IR *ir = i2c_get_clientdata(client);
1199 mutex_lock(&ir_devices_lock);
1202 /* We destroyed everything when the first client came through */
1203 mutex_unlock(&ir_devices_lock);
1208 lirc_unregister_driver(ir->l.minor);
1211 destroy_rx_kthread(ir->rx);
1212 if (ir->rx != NULL) {
1213 i2c_set_clientdata(ir->rx->c, NULL);
1218 if (ir->tx != NULL) {
1219 i2c_set_clientdata(ir->tx->c, NULL);
1224 if (ir->rbuf.fifo_initialized)
1225 lirc_buffer_free(&ir->rbuf);
1229 mutex_unlock(&ir_devices_lock);
1234 /* ir_devices_lock must be held */
1235 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1239 if (list_empty(&ir_devices_list))
1242 list_for_each_entry(ir, &ir_devices_list, list)
1243 if (ir->adapter == adapter)
1249 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1252 struct i2c_adapter *adap = client->adapter;
1254 bool tx_probe = false;
1256 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1257 __func__, id->name, adap->nr, adap->name, client->addr);
1260 * The IR receiver is at i2c address 0x71.
1261 * The IR transmitter is at i2c address 0x70.
1264 if (id->driver_data & ID_FLAG_TX)
1266 else if (tx_only) /* module option */
1269 zilog_info("probing IR %s on %s (i2c-%d)\n",
1270 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1272 mutex_lock(&ir_devices_lock);
1274 /* Use a single struct IR instance for both the Rx and Tx functions */
1275 ir = find_ir_device_by_adapter(adap);
1277 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1282 /* store for use in ir_probe() again, and open() later on */
1283 INIT_LIST_HEAD(&ir->list);
1284 ret = add_ir_device(ir);
1289 mutex_init(&ir->ir_lock);
1290 atomic_set(&ir->open_count, 0);
1292 /* set lirc_dev stuff */
1293 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1294 ir->l.minor = minor; /* module option */
1295 ir->l.code_length = 13;
1296 ir->l.chunk_size = 2;
1297 ir->l.buffer_size = BUFLEN / 2;
1298 ir->l.rbuf = &ir->rbuf;
1299 ir->l.fops = &lirc_fops;
1301 ir->l.dev = &adap->dev;
1302 ir->l.sample_rate = 0;
1303 ret = lirc_buffer_init(ir->l.rbuf,
1304 ir->l.chunk_size, ir->l.buffer_size);
1310 /* Set up a struct IR_tx instance */
1311 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1312 if (ir->tx == NULL) {
1318 ir->tx->need_boot = 1;
1319 ir->tx->post_tx_ready_poll =
1320 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1322 /* Set up a struct IR_rx instance */
1323 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1324 if (ir->rx == NULL) {
1330 ir->rx->hdpvr_data_fmt =
1331 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1334 i2c_set_clientdata(client, ir);
1336 /* Proceed only if we have the required Tx and Rx clients ready to go */
1337 if (ir->tx == NULL ||
1338 (ir->rx == NULL && !tx_only)) {
1339 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1340 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1341 adap->nr, tx_probe ? "Rx" : "Tx");
1345 /* initialise RX device */
1346 if (ir->rx != NULL) {
1347 /* try to fire up polling thread */
1348 ir->rx->task = kthread_run(lirc_thread, ir,
1349 "zilog-rx-i2c-%d", adap->nr);
1350 if (IS_ERR(ir->rx->task)) {
1351 ret = PTR_ERR(ir->rx->task);
1352 zilog_error("%s: could not start IR Rx polling thread"
1358 /* register with lirc */
1359 ir->l.minor = lirc_register_driver(&ir->l);
1360 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1361 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1362 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1364 goto out_free_thread;
1368 * if we have the tx device, load the 'firmware'. We do this
1369 * after registering with lirc as otherwise hotplug seems to take
1370 * 10s to create the lirc device.
1372 if (ir->tx != NULL) {
1373 /* Special TX init */
1374 ret = tx_init(ir->tx);
1376 goto out_unregister;
1379 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1380 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1382 mutex_unlock(&ir_devices_lock);
1386 lirc_unregister_driver(ir->l.minor);
1388 destroy_rx_kthread(ir->rx);
1390 if (ir->rx != NULL) {
1391 if (ir->rx->c != NULL)
1392 i2c_set_clientdata(ir->rx->c, NULL);
1395 if (ir->tx != NULL) {
1396 if (ir->tx->c != NULL)
1397 i2c_set_clientdata(ir->tx->c, NULL);
1400 if (ir->rbuf.fifo_initialized)
1401 lirc_buffer_free(&ir->rbuf);
1406 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1407 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1409 mutex_unlock(&ir_devices_lock);
1413 static int __init zilog_init(void)
1417 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1419 mutex_init(&tx_data_lock);
1421 request_module("firmware_class");
1423 ret = i2c_add_driver(&driver);
1425 zilog_error("initialization failed\n");
1427 zilog_notify("initialization complete\n");
1432 static void __exit zilog_exit(void)
1434 i2c_del_driver(&driver);
1437 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1440 module_init(zilog_init);
1441 module_exit(zilog_exit);
1443 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1444 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1445 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1447 MODULE_LICENSE("GPL");
1448 /* for compat with old name, which isn't all that accurate anymore */
1449 MODULE_ALIAS("lirc_pvr150");
1451 module_param(minor, int, 0444);
1452 MODULE_PARM_DESC(minor, "Preferred minor device number");
1454 module_param(debug, bool, 0644);
1455 MODULE_PARM_DESC(debug, "Enable debugging messages");
1457 module_param(tx_only, bool, 0644);
1458 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");