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);
210 ir->tx->need_boot = 1;
213 mutex_unlock(&ir->ir_lock);
217 if (kthread_should_stop()) {
218 mutex_unlock(&ir->ir_lock);
221 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
222 mutex_unlock(&ir->ir_lock);
223 if (ret != sizeof(keybuf)) {
224 zilog_error("i2c_master_recv failed with %d -- "
225 "keeping last read buffer\n", ret);
227 rx->b[0] = keybuf[3];
228 rx->b[1] = keybuf[4];
229 rx->b[2] = keybuf[5];
230 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
234 if (rx->hdpvr_data_fmt) {
235 if (got_data && (keybuf[0] == 0x80))
237 else if (got_data && (keybuf[0] == 0x00))
239 } else if ((rx->b[0] & 0x80) == 0)
240 return got_data ? 0 : -ENODATA;
242 /* look what we have */
243 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
245 codes[0] = (code >> 8) & 0xff;
246 codes[1] = code & 0xff;
249 lirc_buffer_write(&rx->buf, codes);
251 } while (!lirc_buffer_full(&rx->buf));
257 * Main function of the polling thread -- from lirc_dev.
258 * We don't fit the LIRC model at all anymore. This is horrible, but
259 * basically we have a single RX/TX device with a nasty failure mode
260 * that needs to be accounted for across the pair. lirc lets us provide
261 * fops, but prevents us from using the internal polling, etc. if we do
262 * so. Hence the replication. Might be neater to extend the LIRC model
263 * to account for this but I'd think it's a very special case of seriously
264 * messed up hardware.
266 static int lirc_thread(void *arg)
269 struct IR_rx *rx = ir->rx;
271 dprintk("poll thread started\n");
273 while (!kthread_should_stop()) {
274 set_current_state(TASK_INTERRUPTIBLE);
276 /* if device not opened, we can sleep half a second */
278 schedule_timeout(HZ/2);
283 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
284 * We use this interval as the chip resets every time you poll
285 * it (bad!). This is therefore just sufficient to catch all
286 * of the button presses. It makes the remote much more
287 * responsive. You can see the difference by running irw and
288 * holding down a button. With 100ms, the old polling
289 * interval, you'll notice breaks in the repeat sequence
290 * corresponding to lost keypresses.
292 schedule_timeout((260 * HZ) / 1000);
293 if (kthread_should_stop())
296 wake_up_interruptible(&rx->buf.wait_poll);
299 dprintk("poll thread ended\n");
303 static int set_use_inc(void *data)
305 struct IR *ir = data;
307 if (ir->l.owner == NULL || try_module_get(ir->l.owner) == 0)
310 /* lock bttv in memory while /dev/lirc is in use */
312 * this is completely broken code. lirc_unregister_driver()
313 * must be possible even when the device is open
316 i2c_use_client(ir->rx->c);
318 i2c_use_client(ir->tx->c);
323 static void set_use_dec(void *data)
325 struct IR *ir = data;
328 i2c_release_client(ir->rx->c);
330 i2c_release_client(ir->tx->c);
331 if (ir->l.owner != NULL)
332 module_put(ir->l.owner);
335 /* safe read of a uint32 (always network byte order) */
336 static int read_uint32(unsigned char **data,
337 unsigned char *endp, unsigned int *val)
339 if (*data + 4 > endp)
341 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
342 ((*data)[2] << 8) | (*data)[3];
347 /* safe read of a uint8 */
348 static int read_uint8(unsigned char **data,
349 unsigned char *endp, unsigned char *val)
351 if (*data + 1 > endp)
357 /* safe skipping of N bytes */
358 static int skip(unsigned char **data,
359 unsigned char *endp, unsigned int distance)
361 if (*data + distance > endp)
367 /* decompress key data into the given buffer */
368 static int get_key_data(unsigned char *buf,
369 unsigned int codeset, unsigned int key)
371 unsigned char *data, *endp, *diffs, *key_block;
372 unsigned char keys, ndiffs, id;
373 unsigned int base, lim, pos, i;
375 /* Binary search for the codeset */
376 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
377 pos = base + (lim >> 1);
378 data = tx_data->code_sets[pos];
380 if (!read_uint32(&data, tx_data->endp, &i))
385 else if (codeset > i) {
394 /* Set end of data block */
395 endp = pos < tx_data->num_code_sets - 1 ?
396 tx_data->code_sets[pos + 1] : tx_data->endp;
398 /* Read the block header */
399 if (!read_uint8(&data, endp, &keys) ||
400 !read_uint8(&data, endp, &ndiffs) ||
401 ndiffs > TX_BLOCK_SIZE || keys == 0)
404 /* Save diffs & skip */
406 if (!skip(&data, endp, ndiffs))
409 /* Read the id of the first key */
410 if (!read_uint8(&data, endp, &id))
413 /* Unpack the first key's data */
414 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
415 if (tx_data->fixed[i] == -1) {
416 if (!read_uint8(&data, endp, &buf[i]))
419 buf[i] = (unsigned char)tx_data->fixed[i];
423 /* Early out key found/not found */
431 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
434 /* Binary search for the key */
435 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
437 unsigned char *key_data;
438 pos = base + (lim >> 1);
439 key_data = key_block + (ndiffs + 1) * pos;
441 if (*key_data == key) {
445 /* found, so unpack the diffs */
446 for (i = 0; i < ndiffs; ++i) {
448 if (!read_uint8(&key_data, endp, &val) ||
449 diffs[i] >= TX_BLOCK_SIZE)
455 } else if (key > *key_data) {
464 zilog_error("firmware is corrupt\n");
468 /* send a block of data to the IR TX device */
469 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
472 unsigned char buf[5];
474 for (i = 0; i < TX_BLOCK_SIZE;) {
475 int tosend = TX_BLOCK_SIZE - i;
478 buf[0] = (unsigned char)(i + 1);
479 for (j = 0; j < tosend; ++j)
480 buf[1 + j] = data_block[i + j];
481 dprintk("%02x %02x %02x %02x %02x",
482 buf[0], buf[1], buf[2], buf[3], buf[4]);
483 ret = i2c_master_send(tx->c, buf, tosend + 1);
484 if (ret != tosend + 1) {
485 zilog_error("i2c_master_send failed with %d\n", ret);
486 return ret < 0 ? ret : -EFAULT;
493 /* send boot data to the IR TX device */
494 static int send_boot_data(struct IR_tx *tx)
497 unsigned char buf[4];
499 /* send the boot block */
500 ret = send_data_block(tx, tx_data->boot_data);
507 ret = i2c_master_send(tx->c, buf, 2);
509 zilog_error("i2c_master_send failed with %d\n", ret);
510 return ret < 0 ? ret : -EFAULT;
512 ret = i2c_master_send(tx->c, buf, 1);
514 zilog_error("i2c_master_send failed with %d\n", ret);
515 return ret < 0 ? ret : -EFAULT;
518 /* Here comes the firmware version... (hopefully) */
519 ret = i2c_master_recv(tx->c, buf, 4);
521 zilog_error("i2c_master_recv failed with %d\n", ret);
524 if (buf[0] != 0x80) {
525 zilog_error("unexpected IR TX response: %02x\n", buf[0]);
528 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
529 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
534 /* unload "firmware", lock held */
535 static void fw_unload_locked(void)
538 if (tx_data->code_sets)
539 vfree(tx_data->code_sets);
542 vfree(tx_data->datap);
546 dprintk("successfully unloaded IR blaster firmware\n");
550 /* unload "firmware" for the IR TX device */
551 static void fw_unload(void)
553 mutex_lock(&tx_data_lock);
555 mutex_unlock(&tx_data_lock);
558 /* load "firmware" for the IR TX device */
559 static int fw_load(struct IR_tx *tx)
563 unsigned char *data, version, num_global_fixed;
564 const struct firmware *fw_entry;
566 /* Already loaded? */
567 mutex_lock(&tx_data_lock);
573 /* Request codeset data file */
574 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
576 zilog_error("firmware haup-ir-blaster.bin not available "
578 ret = ret < 0 ? ret : -EFAULT;
581 dprintk("firmware of size %zu loaded\n", fw_entry->size);
584 tx_data = vmalloc(sizeof(*tx_data));
585 if (tx_data == NULL) {
586 zilog_error("out of memory\n");
587 release_firmware(fw_entry);
591 tx_data->code_sets = NULL;
593 /* Copy the data so hotplug doesn't get confused and timeout */
594 tx_data->datap = vmalloc(fw_entry->size);
595 if (tx_data->datap == NULL) {
596 zilog_error("out of memory\n");
597 release_firmware(fw_entry);
602 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
603 tx_data->endp = tx_data->datap + fw_entry->size;
604 release_firmware(fw_entry); fw_entry = NULL;
607 data = tx_data->datap;
608 if (!read_uint8(&data, tx_data->endp, &version))
611 zilog_error("unsupported code set file version (%u, expected"
612 "1) -- please upgrade to a newer driver",
619 /* Save boot block for later */
620 tx_data->boot_data = data;
621 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
624 if (!read_uint32(&data, tx_data->endp,
625 &tx_data->num_code_sets))
628 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
630 tx_data->code_sets = vmalloc(
631 tx_data->num_code_sets * sizeof(char *));
632 if (tx_data->code_sets == NULL) {
638 for (i = 0; i < TX_BLOCK_SIZE; ++i)
639 tx_data->fixed[i] = -1;
641 /* Read global fixed data template */
642 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
643 num_global_fixed > TX_BLOCK_SIZE)
645 for (i = 0; i < num_global_fixed; ++i) {
646 unsigned char pos, val;
647 if (!read_uint8(&data, tx_data->endp, &pos) ||
648 !read_uint8(&data, tx_data->endp, &val) ||
649 pos >= TX_BLOCK_SIZE)
651 tx_data->fixed[pos] = (int)val;
654 /* Filch out the position of each code set */
655 for (i = 0; i < tx_data->num_code_sets; ++i) {
658 unsigned char ndiffs;
660 /* Save the codeset position */
661 tx_data->code_sets[i] = data;
664 if (!read_uint32(&data, tx_data->endp, &id) ||
665 !read_uint8(&data, tx_data->endp, &keys) ||
666 !read_uint8(&data, tx_data->endp, &ndiffs) ||
667 ndiffs > TX_BLOCK_SIZE || keys == 0)
670 /* skip diff positions */
671 if (!skip(&data, tx_data->endp, ndiffs))
675 * After the diffs we have the first key id + data -
678 if (!skip(&data, tx_data->endp,
679 1 + TX_BLOCK_SIZE - num_global_fixed))
682 /* Then we have keys-1 blocks of key id+diffs */
683 if (!skip(&data, tx_data->endp,
684 (ndiffs + 1) * (keys - 1)))
691 zilog_error("firmware is corrupt\n");
696 mutex_unlock(&tx_data_lock);
700 /* initialise the IR TX device */
701 static int tx_init(struct IR_tx *tx)
705 /* Load 'firmware' */
710 /* Send boot block */
711 ret = send_boot_data(tx);
720 /* do nothing stub to make LIRC happy */
721 static loff_t lseek(struct file *filep, loff_t offset, int orig)
726 /* copied from lirc_dev */
727 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
729 struct IR *ir = filep->private_data;
730 struct IR_rx *rx = ir->rx;
731 int ret = 0, written = 0;
732 DECLARE_WAITQUEUE(wait, current);
734 dprintk("read called\n");
738 if (mutex_lock_interruptible(&rx->buf_lock))
741 if (n % rx->buf.chunk_size) {
742 dprintk("read result = -EINVAL\n");
743 mutex_unlock(&rx->buf_lock);
748 * we add ourselves to the task queue before buffer check
749 * to avoid losing scan code (in case when queue is awaken somewhere
750 * between while condition checking and scheduling)
752 add_wait_queue(&rx->buf.wait_poll, &wait);
753 set_current_state(TASK_INTERRUPTIBLE);
756 * while we didn't provide 'length' bytes, device is opened in blocking
757 * mode and 'copy_to_user' is happy, wait for data.
759 while (written < n && ret == 0) {
760 if (lirc_buffer_empty(&rx->buf)) {
762 * According to the read(2) man page, 'written' can be
763 * returned as less than 'n', instead of blocking
764 * again, returning -EWOULDBLOCK, or returning
769 if (filep->f_flags & O_NONBLOCK) {
773 if (signal_pending(current)) {
778 set_current_state(TASK_INTERRUPTIBLE);
780 unsigned char buf[rx->buf.chunk_size];
781 lirc_buffer_read(&rx->buf, buf);
782 ret = copy_to_user((void *)outbuf+written, buf,
784 written += rx->buf.chunk_size;
788 remove_wait_queue(&rx->buf.wait_poll, &wait);
789 set_current_state(TASK_RUNNING);
790 mutex_unlock(&rx->buf_lock);
792 dprintk("read result = %s (%d)\n",
793 ret ? "-EFAULT" : "OK", ret);
795 return ret ? ret : written;
798 /* send a keypress to the IR TX device */
799 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
801 unsigned char data_block[TX_BLOCK_SIZE];
802 unsigned char buf[2];
805 /* Get data for the codeset/key */
806 ret = get_key_data(data_block, code, key);
808 if (ret == -EPROTO) {
809 zilog_error("failed to get data for code %u, key %u -- check "
810 "lircd.conf entries\n", code, key);
815 /* Send the data block */
816 ret = send_data_block(tx, data_block);
820 /* Send data block length? */
823 ret = i2c_master_send(tx->c, buf, 2);
825 zilog_error("i2c_master_send failed with %d\n", ret);
826 return ret < 0 ? ret : -EFAULT;
828 ret = i2c_master_send(tx->c, buf, 1);
830 zilog_error("i2c_master_send failed with %d\n", ret);
831 return ret < 0 ? ret : -EFAULT;
834 /* Send finished download? */
835 ret = i2c_master_recv(tx->c, buf, 1);
837 zilog_error("i2c_master_recv failed with %d\n", ret);
838 return ret < 0 ? ret : -EFAULT;
840 if (buf[0] != 0xA0) {
841 zilog_error("unexpected IR TX response #1: %02x\n",
846 /* Send prepare command? */
849 ret = i2c_master_send(tx->c, buf, 2);
851 zilog_error("i2c_master_send failed with %d\n", ret);
852 return ret < 0 ? ret : -EFAULT;
856 * The sleep bits aren't necessary on the HD PVR, and in fact, the
857 * last i2c_master_recv always fails with a -5, so for now, we're
858 * going to skip this whole mess and say we're done on the HD PVR
860 if (!tx->post_tx_ready_poll) {
861 dprintk("sent code %u, key %u\n", code, key);
866 * This bit NAKs until the device is ready, so we retry it
867 * sleeping a bit each time. This seems to be what the windows
868 * driver does, approximately.
871 for (i = 0; i < 20; ++i) {
872 set_current_state(TASK_UNINTERRUPTIBLE);
873 schedule_timeout((50 * HZ + 999) / 1000);
874 ret = i2c_master_send(tx->c, buf, 1);
877 dprintk("NAK expected: i2c_master_send "
878 "failed with %d (try %d)\n", ret, i+1);
881 zilog_error("IR TX chip never got ready: last i2c_master_send "
882 "failed with %d\n", ret);
883 return ret < 0 ? ret : -EFAULT;
886 /* Seems to be an 'ok' response */
887 i = i2c_master_recv(tx->c, buf, 1);
889 zilog_error("i2c_master_recv failed with %d\n", ret);
892 if (buf[0] != 0x80) {
893 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
897 /* Oh good, it worked */
898 dprintk("sent code %u, key %u\n", code, key);
903 * Write a code to the device. We take in a 32-bit number (an int) and then
904 * decode this to a codeset/key index. The key data is then decompressed and
905 * sent to the device. We have a spin lock as per i2c documentation to prevent
906 * multiple concurrent sends which would probably cause the device to explode.
908 static ssize_t write(struct file *filep, const char *buf, size_t n,
911 struct IR *ir = filep->private_data;
912 struct IR_tx *tx = ir->tx;
919 /* Validate user parameters */
923 /* Lock i2c bus for the duration */
924 mutex_lock(&ir->ir_lock);
926 /* Send each keypress */
927 for (i = 0; i < n;) {
931 if (copy_from_user(&command, buf + i, sizeof(command))) {
932 mutex_unlock(&ir->ir_lock);
936 /* Send boot data first if required */
937 if (tx->need_boot == 1) {
938 ret = send_boot_data(tx);
945 ret = send_code(tx, (unsigned)command >> 16,
946 (unsigned)command & 0xFFFF);
947 if (ret == -EPROTO) {
948 mutex_unlock(&ir->ir_lock);
954 * Hmm, a failure. If we've had a few then give up, otherwise
958 /* Looks like the chip crashed, reset it */
959 zilog_error("sending to the IR transmitter chip "
960 "failed, trying reset\n");
963 zilog_error("unable to send to the IR chip "
964 "after 3 resets, giving up\n");
965 mutex_unlock(&ir->ir_lock);
968 set_current_state(TASK_UNINTERRUPTIBLE);
969 schedule_timeout((100 * HZ + 999) / 1000);
976 /* Release i2c bus */
977 mutex_unlock(&ir->ir_lock);
983 /* copied from lirc_dev */
984 static unsigned int poll(struct file *filep, poll_table *wait)
986 struct IR *ir = filep->private_data;
987 struct IR_rx *rx = ir->rx;
990 dprintk("poll called\n");
994 mutex_lock(&rx->buf_lock);
996 poll_wait(filep, &rx->buf.wait_poll, wait);
998 dprintk("poll result = %s\n",
999 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1001 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1003 mutex_unlock(&rx->buf_lock);
1007 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1009 struct IR *ir = filep->private_data;
1011 unsigned long mode, features = 0;
1014 features |= LIRC_CAN_REC_LIRCCODE;
1016 features |= LIRC_CAN_SEND_PULSE;
1019 case LIRC_GET_LENGTH:
1020 result = put_user((unsigned long)13,
1021 (unsigned long *)arg);
1023 case LIRC_GET_FEATURES:
1024 result = put_user(features, (unsigned long *) arg);
1026 case LIRC_GET_REC_MODE:
1027 if (!(features&LIRC_CAN_REC_MASK))
1030 result = put_user(LIRC_REC2MODE
1031 (features&LIRC_CAN_REC_MASK),
1032 (unsigned long *)arg);
1034 case LIRC_SET_REC_MODE:
1035 if (!(features&LIRC_CAN_REC_MASK))
1038 result = get_user(mode, (unsigned long *)arg);
1039 if (!result && !(LIRC_MODE2REC(mode) & features))
1042 case LIRC_GET_SEND_MODE:
1043 if (!(features&LIRC_CAN_SEND_MASK))
1046 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1048 case LIRC_SET_SEND_MODE:
1049 if (!(features&LIRC_CAN_SEND_MASK))
1052 result = get_user(mode, (unsigned long *) arg);
1053 if (!result && mode != LIRC_MODE_PULSE)
1062 /* ir_devices_lock must be held */
1063 static struct IR *find_ir_device_by_minor(unsigned int minor)
1065 if (minor >= MAX_IRCTL_DEVICES)
1068 return ir_devices[minor];
1072 * Open the IR device. Get hold of our IR structure and
1073 * stash it in private_data for the file
1075 static int open(struct inode *node, struct file *filep)
1079 unsigned int minor = MINOR(node->i_rdev);
1081 /* find our IR struct */
1082 mutex_lock(&ir_devices_lock);
1083 ir = find_ir_device_by_minor(minor);
1084 mutex_unlock(&ir_devices_lock);
1089 /* increment in use count */
1090 mutex_lock(&ir->ir_lock);
1092 ret = set_use_inc(ir);
1095 mutex_unlock(&ir->ir_lock);
1098 mutex_unlock(&ir->ir_lock);
1100 /* stash our IR struct */
1101 filep->private_data = ir;
1106 /* Close the IR device */
1107 static int close(struct inode *node, struct file *filep)
1109 /* find our IR struct */
1110 struct IR *ir = filep->private_data;
1112 zilog_error("close: no private_data attached to the file!\n");
1116 /* decrement in use count */
1117 mutex_lock(&ir->ir_lock);
1120 mutex_unlock(&ir->ir_lock);
1125 static struct lirc_driver lirc_template = {
1126 .name = "lirc_zilog",
1127 .set_use_inc = set_use_inc,
1128 .set_use_dec = set_use_dec,
1129 .owner = THIS_MODULE
1132 static int ir_remove(struct i2c_client *client);
1133 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1134 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg);
1136 #define ID_FLAG_TX 0x01
1137 #define ID_FLAG_HDPVR 0x02
1139 static const struct i2c_device_id ir_transceiver_id[] = {
1140 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1141 { "ir_rx_z8f0811_haup", 0 },
1142 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1143 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1147 static struct i2c_driver driver = {
1149 .owner = THIS_MODULE,
1150 .name = "Zilog/Hauppauge i2c IR",
1153 .remove = ir_remove,
1154 .command = ir_command,
1155 .id_table = ir_transceiver_id,
1158 static const struct file_operations lirc_fops = {
1159 .owner = THIS_MODULE,
1164 .unlocked_ioctl = ioctl,
1165 #ifdef CONFIG_COMPAT
1166 .compat_ioctl = ioctl,
1172 static void destroy_rx_kthread(struct IR_rx *rx)
1174 /* end up polling thread */
1175 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1176 kthread_stop(rx->task);
1181 /* ir_devices_lock must be held */
1182 static int add_ir_device(struct IR *ir)
1186 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1187 if (ir_devices[i] == NULL) {
1192 return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1195 /* ir_devices_lock must be held */
1196 static void del_ir_device(struct IR *ir)
1200 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1201 if (ir_devices[i] == ir) {
1202 ir_devices[i] = NULL;
1207 static int ir_remove(struct i2c_client *client)
1209 struct IR *ir = i2c_get_clientdata(client);
1211 mutex_lock(&ir_devices_lock);
1214 /* We destroyed everything when the first client came through */
1215 mutex_unlock(&ir_devices_lock);
1220 lirc_unregister_driver(ir->l.minor);
1223 destroy_rx_kthread(ir->rx);
1224 if (ir->rx != NULL) {
1225 if (ir->rx->buf.fifo_initialized)
1226 lirc_buffer_free(&ir->rx->buf);
1227 i2c_set_clientdata(ir->rx->c, NULL);
1232 i2c_set_clientdata(ir->tx->c, NULL);
1239 mutex_unlock(&ir_devices_lock);
1244 /* ir_devices_lock must be held */
1245 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1248 struct IR *ir = NULL;
1250 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1251 if (ir_devices[i] != NULL &&
1252 ir_devices[i]->adapter == adapter) {
1260 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1263 struct i2c_adapter *adap = client->adapter;
1265 bool tx_probe = false;
1267 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1268 __func__, id->name, adap->nr, adap->name, client->addr);
1271 * The IR receiver is at i2c address 0x71.
1272 * The IR transmitter is at i2c address 0x70.
1275 if (id->driver_data & ID_FLAG_TX)
1277 else if (tx_only) /* module option */
1280 zilog_info("probing IR %s on %s (i2c-%d)\n",
1281 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1283 mutex_lock(&ir_devices_lock);
1285 /* Use a single struct IR instance for both the Rx and Tx functions */
1286 ir = find_ir_device_by_adapter(adap);
1288 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1293 /* store for use in ir_probe() again, and open() later on */
1294 ret = add_ir_device(ir);
1299 mutex_init(&ir->ir_lock);
1301 /* set lirc_dev stuff */
1302 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1303 ir->l.minor = minor; /* module option */
1304 ir->l.code_length = 13;
1306 ir->l.fops = &lirc_fops;
1308 ir->l.dev = &adap->dev;
1309 ir->l.sample_rate = 0;
1313 /* Set up a struct IR_tx instance */
1314 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1315 if (ir->tx == NULL) {
1321 ir->tx->need_boot = 1;
1322 ir->tx->post_tx_ready_poll =
1323 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1325 /* Set up a struct IR_rx instance */
1326 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1327 if (ir->rx == NULL) {
1332 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1336 mutex_init(&ir->rx->buf_lock);
1338 ir->rx->hdpvr_data_fmt =
1339 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1341 /* set lirc_dev stuff */
1342 ir->l.rbuf = &ir->rx->buf;
1345 i2c_set_clientdata(client, ir);
1347 /* Proceed only if we have the required Tx and Rx clients ready to go */
1348 if (ir->tx == NULL ||
1349 (ir->rx == NULL && !tx_only)) {
1350 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1351 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1352 adap->nr, tx_probe ? "Rx" : "Tx");
1356 /* initialise RX device */
1357 if (ir->rx != NULL) {
1358 /* try to fire up polling thread */
1359 ir->rx->task = kthread_run(lirc_thread, ir,
1360 "zilog-rx-i2c-%d", adap->nr);
1361 if (IS_ERR(ir->rx->task)) {
1362 ret = PTR_ERR(ir->rx->task);
1363 zilog_error("%s: could not start IR Rx polling thread"
1369 /* register with lirc */
1370 ir->l.minor = lirc_register_driver(&ir->l);
1371 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1372 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1373 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1375 goto out_free_thread;
1379 * if we have the tx device, load the 'firmware'. We do this
1380 * after registering with lirc as otherwise hotplug seems to take
1381 * 10s to create the lirc device.
1383 if (ir->tx != NULL) {
1384 /* Special TX init */
1385 ret = tx_init(ir->tx);
1387 goto out_unregister;
1390 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1391 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1393 mutex_unlock(&ir_devices_lock);
1397 lirc_unregister_driver(ir->l.minor);
1399 destroy_rx_kthread(ir->rx);
1401 if (ir->rx != NULL) {
1402 if (ir->rx->buf.fifo_initialized)
1403 lirc_buffer_free(&ir->rx->buf);
1404 if (ir->rx->c != NULL)
1405 i2c_set_clientdata(ir->rx->c, NULL);
1408 if (ir->tx != NULL) {
1409 if (ir->tx->c != NULL)
1410 i2c_set_clientdata(ir->tx->c, NULL);
1417 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1418 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1420 mutex_unlock(&ir_devices_lock);
1424 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
1430 static int __init zilog_init(void)
1434 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1436 mutex_init(&tx_data_lock);
1437 mutex_init(&ir_devices_lock);
1439 request_module("firmware_class");
1441 ret = i2c_add_driver(&driver);
1443 zilog_error("initialization failed\n");
1445 zilog_notify("initialization complete\n");
1450 static void __exit zilog_exit(void)
1452 i2c_del_driver(&driver);
1455 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1458 module_init(zilog_init);
1459 module_exit(zilog_exit);
1461 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1462 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1463 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver");
1464 MODULE_LICENSE("GPL");
1465 /* for compat with old name, which isn't all that accurate anymore */
1466 MODULE_ALIAS("lirc_pvr150");
1468 module_param(minor, int, 0444);
1469 MODULE_PARM_DESC(minor, "Preferred minor device number");
1471 module_param(debug, bool, 0644);
1472 MODULE_PARM_DESC(debug, "Enable debugging messages");
1474 module_param(tx_only, bool, 0644);
1475 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");