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>
67 /* RX device buffer & lock */
68 struct lirc_buffer buf;
69 struct mutex buf_lock;
71 /* RX polling thread data */
72 struct task_struct *task;
83 /* TX additional actions needed */
85 bool post_tx_ready_poll;
94 struct i2c_adapter *adapter;
99 /* Minor -> data mapping */
100 static struct mutex ir_devices_lock;
101 static struct IR *ir_devices[MAX_IRCTL_DEVICES];
103 /* Block size for IR transmitter */
104 #define TX_BLOCK_SIZE 99
106 /* Hauppauge IR transmitter data */
107 struct tx_data_struct {
109 unsigned char *boot_data;
111 /* Start of binary data block */
112 unsigned char *datap;
114 /* End of binary data block */
117 /* Number of installed codesets */
118 unsigned int num_code_sets;
120 /* Pointers to codesets */
121 unsigned char **code_sets;
123 /* Global fixed data template */
124 int fixed[TX_BLOCK_SIZE];
127 static struct tx_data_struct *tx_data;
128 static struct mutex tx_data_lock;
130 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
132 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
133 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
135 /* module parameters */
136 static int debug; /* debug output */
137 static int tx_only; /* only handle the IR Tx function */
138 static int minor = -1; /* minor number */
140 #define dprintk(fmt, args...) \
143 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
147 static int add_to_buf(struct IR *ir)
150 unsigned char codes[2];
151 unsigned char keybuf[6];
155 unsigned char sendbuf[1] = { 0 };
156 struct IR_rx *rx = ir->rx;
161 if (lirc_buffer_full(&rx->buf)) {
162 dprintk("buffer overflow\n");
167 * service the device as long as it is returning
168 * data and we have space
171 if (kthread_should_stop())
175 * Lock i2c bus for the duration. RX/TX chips interfere so
178 mutex_lock(&ir->ir_lock);
180 if (kthread_should_stop()) {
181 mutex_unlock(&ir->ir_lock);
186 * Send random "poll command" (?) Windows driver does this
187 * and it is a good point to detect chip failure.
189 ret = i2c_master_send(rx->c, sendbuf, 1);
191 zilog_error("i2c_master_send failed with %d\n", ret);
193 mutex_unlock(&ir->ir_lock);
194 zilog_error("unable to read from the IR chip "
195 "after 3 resets, giving up\n");
199 /* Looks like the chip crashed, reset it */
200 zilog_error("polling the IR receiver chip failed, "
203 set_current_state(TASK_UNINTERRUPTIBLE);
204 if (kthread_should_stop()) {
205 mutex_unlock(&ir->ir_lock);
208 schedule_timeout((100 * HZ + 999) / 1000);
209 ir->tx->need_boot = 1;
212 mutex_unlock(&ir->ir_lock);
216 if (kthread_should_stop()) {
217 mutex_unlock(&ir->ir_lock);
220 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
221 mutex_unlock(&ir->ir_lock);
222 if (ret != sizeof(keybuf)) {
223 zilog_error("i2c_master_recv failed with %d -- "
224 "keeping last read buffer\n", ret);
226 rx->b[0] = keybuf[3];
227 rx->b[1] = keybuf[4];
228 rx->b[2] = keybuf[5];
229 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
233 if (rx->hdpvr_data_fmt) {
234 if (got_data && (keybuf[0] == 0x80))
236 else if (got_data && (keybuf[0] == 0x00))
238 } else if ((rx->b[0] & 0x80) == 0)
239 return got_data ? 0 : -ENODATA;
241 /* look what we have */
242 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
244 codes[0] = (code >> 8) & 0xff;
245 codes[1] = code & 0xff;
248 lirc_buffer_write(&rx->buf, codes);
250 } while (!lirc_buffer_full(&rx->buf));
256 * Main function of the polling thread -- from lirc_dev.
257 * We don't fit the LIRC model at all anymore. This is horrible, but
258 * basically we have a single RX/TX device with a nasty failure mode
259 * that needs to be accounted for across the pair. lirc lets us provide
260 * fops, but prevents us from using the internal polling, etc. if we do
261 * so. Hence the replication. Might be neater to extend the LIRC model
262 * to account for this but I'd think it's a very special case of seriously
263 * messed up hardware.
265 static int lirc_thread(void *arg)
268 struct IR_rx *rx = ir->rx;
270 dprintk("poll thread started\n");
272 while (!kthread_should_stop()) {
273 set_current_state(TASK_INTERRUPTIBLE);
275 /* if device not opened, we can sleep half a second */
277 schedule_timeout(HZ/2);
282 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
283 * We use this interval as the chip resets every time you poll
284 * it (bad!). This is therefore just sufficient to catch all
285 * of the button presses. It makes the remote much more
286 * responsive. You can see the difference by running irw and
287 * holding down a button. With 100ms, the old polling
288 * interval, you'll notice breaks in the repeat sequence
289 * corresponding to lost keypresses.
291 schedule_timeout((260 * HZ) / 1000);
292 if (kthread_should_stop())
295 wake_up_interruptible(&rx->buf.wait_poll);
298 dprintk("poll thread ended\n");
302 static int set_use_inc(void *data)
304 struct IR *ir = data;
306 if (ir->l.owner == NULL || try_module_get(ir->l.owner) == 0)
309 /* lock bttv in memory while /dev/lirc is in use */
311 * this is completely broken code. lirc_unregister_driver()
312 * must be possible even when the device is open
315 i2c_use_client(ir->rx->c);
317 i2c_use_client(ir->tx->c);
322 static void set_use_dec(void *data)
324 struct IR *ir = data;
327 i2c_release_client(ir->rx->c);
329 i2c_release_client(ir->tx->c);
330 if (ir->l.owner != NULL)
331 module_put(ir->l.owner);
334 /* safe read of a uint32 (always network byte order) */
335 static int read_uint32(unsigned char **data,
336 unsigned char *endp, unsigned int *val)
338 if (*data + 4 > endp)
340 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
341 ((*data)[2] << 8) | (*data)[3];
346 /* safe read of a uint8 */
347 static int read_uint8(unsigned char **data,
348 unsigned char *endp, unsigned char *val)
350 if (*data + 1 > endp)
356 /* safe skipping of N bytes */
357 static int skip(unsigned char **data,
358 unsigned char *endp, unsigned int distance)
360 if (*data + distance > endp)
366 /* decompress key data into the given buffer */
367 static int get_key_data(unsigned char *buf,
368 unsigned int codeset, unsigned int key)
370 unsigned char *data, *endp, *diffs, *key_block;
371 unsigned char keys, ndiffs, id;
372 unsigned int base, lim, pos, i;
374 /* Binary search for the codeset */
375 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
376 pos = base + (lim >> 1);
377 data = tx_data->code_sets[pos];
379 if (!read_uint32(&data, tx_data->endp, &i))
384 else if (codeset > i) {
393 /* Set end of data block */
394 endp = pos < tx_data->num_code_sets - 1 ?
395 tx_data->code_sets[pos + 1] : tx_data->endp;
397 /* Read the block header */
398 if (!read_uint8(&data, endp, &keys) ||
399 !read_uint8(&data, endp, &ndiffs) ||
400 ndiffs > TX_BLOCK_SIZE || keys == 0)
403 /* Save diffs & skip */
405 if (!skip(&data, endp, ndiffs))
408 /* Read the id of the first key */
409 if (!read_uint8(&data, endp, &id))
412 /* Unpack the first key's data */
413 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
414 if (tx_data->fixed[i] == -1) {
415 if (!read_uint8(&data, endp, &buf[i]))
418 buf[i] = (unsigned char)tx_data->fixed[i];
422 /* Early out key found/not found */
430 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
433 /* Binary search for the key */
434 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
436 unsigned char *key_data;
437 pos = base + (lim >> 1);
438 key_data = key_block + (ndiffs + 1) * pos;
440 if (*key_data == key) {
444 /* found, so unpack the diffs */
445 for (i = 0; i < ndiffs; ++i) {
447 if (!read_uint8(&key_data, endp, &val) ||
448 diffs[i] >= TX_BLOCK_SIZE)
454 } else if (key > *key_data) {
463 zilog_error("firmware is corrupt\n");
467 /* send a block of data to the IR TX device */
468 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
471 unsigned char buf[5];
473 for (i = 0; i < TX_BLOCK_SIZE;) {
474 int tosend = TX_BLOCK_SIZE - i;
477 buf[0] = (unsigned char)(i + 1);
478 for (j = 0; j < tosend; ++j)
479 buf[1 + j] = data_block[i + j];
480 dprintk("%02x %02x %02x %02x %02x",
481 buf[0], buf[1], buf[2], buf[3], buf[4]);
482 ret = i2c_master_send(tx->c, buf, tosend + 1);
483 if (ret != tosend + 1) {
484 zilog_error("i2c_master_send failed with %d\n", ret);
485 return ret < 0 ? ret : -EFAULT;
492 /* send boot data to the IR TX device */
493 static int send_boot_data(struct IR_tx *tx)
496 unsigned char buf[4];
498 /* send the boot block */
499 ret = send_data_block(tx, tx_data->boot_data);
506 ret = i2c_master_send(tx->c, buf, 2);
508 zilog_error("i2c_master_send failed with %d\n", ret);
509 return ret < 0 ? ret : -EFAULT;
511 ret = i2c_master_send(tx->c, buf, 1);
513 zilog_error("i2c_master_send failed with %d\n", ret);
514 return ret < 0 ? ret : -EFAULT;
517 /* Here comes the firmware version... (hopefully) */
518 ret = i2c_master_recv(tx->c, buf, 4);
520 zilog_error("i2c_master_recv failed with %d\n", ret);
523 if (buf[0] != 0x80) {
524 zilog_error("unexpected IR TX response: %02x\n", buf[0]);
527 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
528 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
533 /* unload "firmware", lock held */
534 static void fw_unload_locked(void)
537 if (tx_data->code_sets)
538 vfree(tx_data->code_sets);
541 vfree(tx_data->datap);
545 dprintk("successfully unloaded IR blaster firmware\n");
549 /* unload "firmware" for the IR TX device */
550 static void fw_unload(void)
552 mutex_lock(&tx_data_lock);
554 mutex_unlock(&tx_data_lock);
557 /* load "firmware" for the IR TX device */
558 static int fw_load(struct IR_tx *tx)
562 unsigned char *data, version, num_global_fixed;
563 const struct firmware *fw_entry;
565 /* Already loaded? */
566 mutex_lock(&tx_data_lock);
572 /* Request codeset data file */
573 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
575 zilog_error("firmware haup-ir-blaster.bin not available "
577 ret = ret < 0 ? ret : -EFAULT;
580 dprintk("firmware of size %zu loaded\n", fw_entry->size);
583 tx_data = vmalloc(sizeof(*tx_data));
584 if (tx_data == NULL) {
585 zilog_error("out of memory\n");
586 release_firmware(fw_entry);
590 tx_data->code_sets = NULL;
592 /* Copy the data so hotplug doesn't get confused and timeout */
593 tx_data->datap = vmalloc(fw_entry->size);
594 if (tx_data->datap == NULL) {
595 zilog_error("out of memory\n");
596 release_firmware(fw_entry);
601 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
602 tx_data->endp = tx_data->datap + fw_entry->size;
603 release_firmware(fw_entry); fw_entry = NULL;
606 data = tx_data->datap;
607 if (!read_uint8(&data, tx_data->endp, &version))
610 zilog_error("unsupported code set file version (%u, expected"
611 "1) -- please upgrade to a newer driver",
618 /* Save boot block for later */
619 tx_data->boot_data = data;
620 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
623 if (!read_uint32(&data, tx_data->endp,
624 &tx_data->num_code_sets))
627 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
629 tx_data->code_sets = vmalloc(
630 tx_data->num_code_sets * sizeof(char *));
631 if (tx_data->code_sets == NULL) {
637 for (i = 0; i < TX_BLOCK_SIZE; ++i)
638 tx_data->fixed[i] = -1;
640 /* Read global fixed data template */
641 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
642 num_global_fixed > TX_BLOCK_SIZE)
644 for (i = 0; i < num_global_fixed; ++i) {
645 unsigned char pos, val;
646 if (!read_uint8(&data, tx_data->endp, &pos) ||
647 !read_uint8(&data, tx_data->endp, &val) ||
648 pos >= TX_BLOCK_SIZE)
650 tx_data->fixed[pos] = (int)val;
653 /* Filch out the position of each code set */
654 for (i = 0; i < tx_data->num_code_sets; ++i) {
657 unsigned char ndiffs;
659 /* Save the codeset position */
660 tx_data->code_sets[i] = data;
663 if (!read_uint32(&data, tx_data->endp, &id) ||
664 !read_uint8(&data, tx_data->endp, &keys) ||
665 !read_uint8(&data, tx_data->endp, &ndiffs) ||
666 ndiffs > TX_BLOCK_SIZE || keys == 0)
669 /* skip diff positions */
670 if (!skip(&data, tx_data->endp, ndiffs))
674 * After the diffs we have the first key id + data -
677 if (!skip(&data, tx_data->endp,
678 1 + TX_BLOCK_SIZE - num_global_fixed))
681 /* Then we have keys-1 blocks of key id+diffs */
682 if (!skip(&data, tx_data->endp,
683 (ndiffs + 1) * (keys - 1)))
690 zilog_error("firmware is corrupt\n");
695 mutex_unlock(&tx_data_lock);
699 /* initialise the IR TX device */
700 static int tx_init(struct IR_tx *tx)
704 /* Load 'firmware' */
709 /* Send boot block */
710 ret = send_boot_data(tx);
719 /* do nothing stub to make LIRC happy */
720 static loff_t lseek(struct file *filep, loff_t offset, int orig)
725 /* copied from lirc_dev */
726 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
728 struct IR *ir = filep->private_data;
729 struct IR_rx *rx = ir->rx;
730 int ret = 0, written = 0;
731 DECLARE_WAITQUEUE(wait, current);
733 dprintk("read called\n");
737 if (mutex_lock_interruptible(&rx->buf_lock))
740 if (n % rx->buf.chunk_size) {
741 dprintk("read result = -EINVAL\n");
742 mutex_unlock(&rx->buf_lock);
747 * we add ourselves to the task queue before buffer check
748 * to avoid losing scan code (in case when queue is awaken somewhere
749 * between while condition checking and scheduling)
751 add_wait_queue(&rx->buf.wait_poll, &wait);
752 set_current_state(TASK_INTERRUPTIBLE);
755 * while we didn't provide 'length' bytes, device is opened in blocking
756 * mode and 'copy_to_user' is happy, wait for data.
758 while (written < n && ret == 0) {
759 if (lirc_buffer_empty(&rx->buf)) {
761 * According to the read(2) man page, 'written' can be
762 * returned as less than 'n', instead of blocking
763 * again, returning -EWOULDBLOCK, or returning
768 if (filep->f_flags & O_NONBLOCK) {
772 if (signal_pending(current)) {
777 set_current_state(TASK_INTERRUPTIBLE);
779 unsigned char buf[rx->buf.chunk_size];
780 lirc_buffer_read(&rx->buf, buf);
781 ret = copy_to_user((void *)outbuf+written, buf,
783 written += rx->buf.chunk_size;
787 remove_wait_queue(&rx->buf.wait_poll, &wait);
788 set_current_state(TASK_RUNNING);
789 mutex_unlock(&rx->buf_lock);
791 dprintk("read result = %s (%d)\n",
792 ret ? "-EFAULT" : "OK", ret);
794 return ret ? ret : written;
797 /* send a keypress to the IR TX device */
798 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
800 unsigned char data_block[TX_BLOCK_SIZE];
801 unsigned char buf[2];
804 /* Get data for the codeset/key */
805 ret = get_key_data(data_block, code, key);
807 if (ret == -EPROTO) {
808 zilog_error("failed to get data for code %u, key %u -- check "
809 "lircd.conf entries\n", code, key);
814 /* Send the data block */
815 ret = send_data_block(tx, data_block);
819 /* Send data block length? */
822 ret = i2c_master_send(tx->c, buf, 2);
824 zilog_error("i2c_master_send failed with %d\n", ret);
825 return ret < 0 ? ret : -EFAULT;
827 ret = i2c_master_send(tx->c, buf, 1);
829 zilog_error("i2c_master_send failed with %d\n", ret);
830 return ret < 0 ? ret : -EFAULT;
833 /* Send finished download? */
834 ret = i2c_master_recv(tx->c, buf, 1);
836 zilog_error("i2c_master_recv failed with %d\n", ret);
837 return ret < 0 ? ret : -EFAULT;
839 if (buf[0] != 0xA0) {
840 zilog_error("unexpected IR TX response #1: %02x\n",
845 /* Send prepare command? */
848 ret = i2c_master_send(tx->c, buf, 2);
850 zilog_error("i2c_master_send failed with %d\n", ret);
851 return ret < 0 ? ret : -EFAULT;
855 * The sleep bits aren't necessary on the HD PVR, and in fact, the
856 * last i2c_master_recv always fails with a -5, so for now, we're
857 * going to skip this whole mess and say we're done on the HD PVR
859 if (!tx->post_tx_ready_poll) {
860 dprintk("sent code %u, key %u\n", code, key);
865 * This bit NAKs until the device is ready, so we retry it
866 * sleeping a bit each time. This seems to be what the windows
867 * driver does, approximately.
870 for (i = 0; i < 20; ++i) {
871 set_current_state(TASK_UNINTERRUPTIBLE);
872 schedule_timeout((50 * HZ + 999) / 1000);
873 ret = i2c_master_send(tx->c, buf, 1);
876 dprintk("NAK expected: i2c_master_send "
877 "failed with %d (try %d)\n", ret, i+1);
880 zilog_error("IR TX chip never got ready: last i2c_master_send "
881 "failed with %d\n", ret);
882 return ret < 0 ? ret : -EFAULT;
885 /* Seems to be an 'ok' response */
886 i = i2c_master_recv(tx->c, buf, 1);
888 zilog_error("i2c_master_recv failed with %d\n", ret);
891 if (buf[0] != 0x80) {
892 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
896 /* Oh good, it worked */
897 dprintk("sent code %u, key %u\n", code, key);
902 * Write a code to the device. We take in a 32-bit number (an int) and then
903 * decode this to a codeset/key index. The key data is then decompressed and
904 * sent to the device. We have a spin lock as per i2c documentation to prevent
905 * multiple concurrent sends which would probably cause the device to explode.
907 static ssize_t write(struct file *filep, const char *buf, size_t n,
910 struct IR *ir = filep->private_data;
911 struct IR_tx *tx = ir->tx;
918 /* Validate user parameters */
922 /* Lock i2c bus for the duration */
923 mutex_lock(&ir->ir_lock);
925 /* Send each keypress */
926 for (i = 0; i < n;) {
930 if (copy_from_user(&command, buf + i, sizeof(command))) {
931 mutex_unlock(&ir->ir_lock);
935 /* Send boot data first if required */
936 if (tx->need_boot == 1) {
937 ret = send_boot_data(tx);
944 ret = send_code(tx, (unsigned)command >> 16,
945 (unsigned)command & 0xFFFF);
946 if (ret == -EPROTO) {
947 mutex_unlock(&ir->ir_lock);
953 * Hmm, a failure. If we've had a few then give up, otherwise
957 /* Looks like the chip crashed, reset it */
958 zilog_error("sending to the IR transmitter chip "
959 "failed, trying reset\n");
962 zilog_error("unable to send to the IR chip "
963 "after 3 resets, giving up\n");
964 mutex_unlock(&ir->ir_lock);
967 set_current_state(TASK_UNINTERRUPTIBLE);
968 schedule_timeout((100 * HZ + 999) / 1000);
975 /* Release i2c bus */
976 mutex_unlock(&ir->ir_lock);
982 /* copied from lirc_dev */
983 static unsigned int poll(struct file *filep, poll_table *wait)
985 struct IR *ir = filep->private_data;
986 struct IR_rx *rx = ir->rx;
989 dprintk("poll called\n");
993 mutex_lock(&rx->buf_lock);
995 poll_wait(filep, &rx->buf.wait_poll, wait);
997 dprintk("poll result = %s\n",
998 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1000 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1002 mutex_unlock(&rx->buf_lock);
1006 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1008 struct IR *ir = filep->private_data;
1010 unsigned long mode, features = 0;
1012 features |= LIRC_CAN_SEND_PULSE;
1014 features |= LIRC_CAN_REC_LIRCCODE;
1017 case LIRC_GET_LENGTH:
1018 result = put_user((unsigned long)13,
1019 (unsigned long *)arg);
1021 case LIRC_GET_FEATURES:
1022 result = put_user(features, (unsigned long *) arg);
1024 case LIRC_GET_REC_MODE:
1025 if (!(features&LIRC_CAN_REC_MASK))
1028 result = put_user(LIRC_REC2MODE
1029 (features&LIRC_CAN_REC_MASK),
1030 (unsigned long *)arg);
1032 case LIRC_SET_REC_MODE:
1033 if (!(features&LIRC_CAN_REC_MASK))
1036 result = get_user(mode, (unsigned long *)arg);
1037 if (!result && !(LIRC_MODE2REC(mode) & features))
1040 case LIRC_GET_SEND_MODE:
1041 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1043 case LIRC_SET_SEND_MODE:
1044 result = get_user(mode, (unsigned long *) arg);
1045 if (!result && mode != LIRC_MODE_PULSE)
1054 /* ir_devices_lock must be held */
1055 static struct IR *find_ir_device_by_minor(unsigned int minor)
1057 if (minor >= MAX_IRCTL_DEVICES)
1060 return ir_devices[minor];
1064 * Open the IR device. Get hold of our IR structure and
1065 * stash it in private_data for the file
1067 static int open(struct inode *node, struct file *filep)
1071 unsigned int minor = MINOR(node->i_rdev);
1073 /* find our IR struct */
1074 mutex_lock(&ir_devices_lock);
1075 ir = find_ir_device_by_minor(minor);
1076 mutex_unlock(&ir_devices_lock);
1081 /* increment in use count */
1082 mutex_lock(&ir->ir_lock);
1084 ret = set_use_inc(ir);
1087 mutex_unlock(&ir->ir_lock);
1090 mutex_unlock(&ir->ir_lock);
1092 /* stash our IR struct */
1093 filep->private_data = ir;
1098 /* Close the IR device */
1099 static int close(struct inode *node, struct file *filep)
1101 /* find our IR struct */
1102 struct IR *ir = filep->private_data;
1104 zilog_error("close: no private_data attached to the file!\n");
1108 /* decrement in use count */
1109 mutex_lock(&ir->ir_lock);
1112 mutex_unlock(&ir->ir_lock);
1117 static struct lirc_driver lirc_template = {
1118 .name = "lirc_zilog",
1119 .set_use_inc = set_use_inc,
1120 .set_use_dec = set_use_dec,
1121 .owner = THIS_MODULE
1124 static int ir_remove(struct i2c_client *client);
1125 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1127 #define ID_FLAG_TX 0x01
1128 #define ID_FLAG_HDPVR 0x02
1130 static const struct i2c_device_id ir_transceiver_id[] = {
1131 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1132 { "ir_rx_z8f0811_haup", 0 },
1133 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1134 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1138 static struct i2c_driver driver = {
1140 .owner = THIS_MODULE,
1141 .name = "Zilog/Hauppauge i2c IR",
1144 .remove = ir_remove,
1145 .id_table = ir_transceiver_id,
1148 static const struct file_operations lirc_fops = {
1149 .owner = THIS_MODULE,
1154 .unlocked_ioctl = ioctl,
1155 #ifdef CONFIG_COMPAT
1156 .compat_ioctl = ioctl,
1162 static void destroy_rx_kthread(struct IR_rx *rx)
1164 /* end up polling thread */
1165 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1166 kthread_stop(rx->task);
1171 /* ir_devices_lock must be held */
1172 static int add_ir_device(struct IR *ir)
1176 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1177 if (ir_devices[i] == NULL) {
1182 return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1185 /* ir_devices_lock must be held */
1186 static void del_ir_device(struct IR *ir)
1190 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1191 if (ir_devices[i] == ir) {
1192 ir_devices[i] = NULL;
1197 static int ir_remove(struct i2c_client *client)
1199 struct IR *ir = i2c_get_clientdata(client);
1201 mutex_lock(&ir_devices_lock);
1204 /* We destroyed everything when the first client came through */
1205 mutex_unlock(&ir_devices_lock);
1210 lirc_unregister_driver(ir->l.minor);
1213 destroy_rx_kthread(ir->rx);
1214 if (ir->rx != NULL) {
1215 if (ir->rx->buf.fifo_initialized)
1216 lirc_buffer_free(&ir->rx->buf);
1217 i2c_set_clientdata(ir->rx->c, NULL);
1222 i2c_set_clientdata(ir->tx->c, NULL);
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)
1238 struct IR *ir = NULL;
1240 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1241 if (ir_devices[i] != NULL &&
1242 ir_devices[i]->adapter == adapter) {
1250 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1253 struct i2c_adapter *adap = client->adapter;
1255 bool tx_probe = false;
1257 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1258 __func__, id->name, adap->nr, adap->name, client->addr);
1261 * The IR receiver is at i2c address 0x71.
1262 * The IR transmitter is at i2c address 0x70.
1265 if (id->driver_data & ID_FLAG_TX)
1267 else if (tx_only) /* module option */
1270 zilog_info("probing IR %s on %s (i2c-%d)\n",
1271 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1273 mutex_lock(&ir_devices_lock);
1275 /* Use a single struct IR instance for both the Rx and Tx functions */
1276 ir = find_ir_device_by_adapter(adap);
1278 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1283 /* store for use in ir_probe() again, and open() later on */
1284 ret = add_ir_device(ir);
1289 mutex_init(&ir->ir_lock);
1291 /* set lirc_dev stuff */
1292 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1293 ir->l.minor = minor; /* module option */
1294 ir->l.code_length = 13;
1296 ir->l.fops = &lirc_fops;
1298 ir->l.dev = &adap->dev;
1299 ir->l.sample_rate = 0;
1303 /* Set up a struct IR_tx instance */
1304 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1305 if (ir->tx == NULL) {
1311 ir->tx->need_boot = 1;
1312 ir->tx->post_tx_ready_poll =
1313 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1315 /* Set up a struct IR_rx instance */
1316 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1317 if (ir->rx == NULL) {
1322 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1326 mutex_init(&ir->rx->buf_lock);
1328 ir->rx->hdpvr_data_fmt =
1329 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1331 /* set lirc_dev stuff */
1332 ir->l.rbuf = &ir->rx->buf;
1335 i2c_set_clientdata(client, ir);
1337 /* Proceed only if we have the required Tx and Rx clients ready to go */
1338 if (ir->tx == NULL ||
1339 (ir->rx == NULL && !tx_only)) {
1340 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1341 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1342 adap->nr, tx_probe ? "Rx" : "Tx");
1346 /* initialise RX device */
1347 if (ir->rx != NULL) {
1348 /* try to fire up polling thread */
1349 ir->rx->task = kthread_run(lirc_thread, ir,
1350 "zilog-rx-i2c-%d", adap->nr);
1351 if (IS_ERR(ir->rx->task)) {
1352 ret = PTR_ERR(ir->rx->task);
1353 zilog_error("%s: could not start IR Rx polling thread"
1359 /* register with lirc */
1360 ir->l.minor = lirc_register_driver(&ir->l);
1361 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1362 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1363 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1365 goto out_free_thread;
1369 * if we have the tx device, load the 'firmware'. We do this
1370 * after registering with lirc as otherwise hotplug seems to take
1371 * 10s to create the lirc device.
1373 ret = tx_init(ir->tx);
1375 goto out_unregister;
1377 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1378 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1380 mutex_unlock(&ir_devices_lock);
1384 lirc_unregister_driver(ir->l.minor);
1386 destroy_rx_kthread(ir->rx);
1388 if (ir->rx != NULL) {
1389 if (ir->rx->buf.fifo_initialized)
1390 lirc_buffer_free(&ir->rx->buf);
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);
1404 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1405 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1407 mutex_unlock(&ir_devices_lock);
1411 static int __init zilog_init(void)
1415 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1417 mutex_init(&tx_data_lock);
1418 mutex_init(&ir_devices_lock);
1420 request_module("firmware_class");
1422 ret = i2c_add_driver(&driver);
1424 zilog_error("initialization failed\n");
1426 zilog_notify("initialization complete\n");
1431 static void __exit zilog_exit(void)
1433 i2c_del_driver(&driver);
1436 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1439 module_init(zilog_init);
1440 module_exit(zilog_exit);
1442 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1443 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1444 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver");
1445 MODULE_LICENSE("GPL");
1446 /* for compat with old name, which isn't all that accurate anymore */
1447 MODULE_ALIAS("lirc_pvr150");
1449 module_param(minor, int, 0444);
1450 MODULE_PARM_DESC(minor, "Preferred minor device number");
1452 module_param(debug, bool, 0644);
1453 MODULE_PARM_DESC(debug, "Enable debugging messages");
1455 module_param(tx_only, bool, 0644);
1456 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");