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
;
92 struct list_head list
;
99 struct i2c_adapter
*adapter
;
104 /* IR transceiver instance object list */
105 static DEFINE_MUTEX(ir_devices_lock
);
106 static LIST_HEAD(ir_devices_list
);
108 /* Block size for IR transmitter */
109 #define TX_BLOCK_SIZE 99
111 /* Hauppauge IR transmitter data */
112 struct tx_data_struct
{
114 unsigned char *boot_data
;
116 /* Start of binary data block */
117 unsigned char *datap
;
119 /* End of binary data block */
122 /* Number of installed codesets */
123 unsigned int num_code_sets
;
125 /* Pointers to codesets */
126 unsigned char **code_sets
;
128 /* Global fixed data template */
129 int fixed
[TX_BLOCK_SIZE
];
132 static struct tx_data_struct
*tx_data
;
133 static struct mutex tx_data_lock
;
135 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
137 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
138 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
140 /* module parameters */
141 static int debug
; /* debug output */
142 static int tx_only
; /* only handle the IR Tx function */
143 static int minor
= -1; /* minor number */
145 #define dprintk(fmt, args...) \
148 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
152 static int add_to_buf(struct IR
*ir
)
155 unsigned char codes
[2];
156 unsigned char keybuf
[6];
160 unsigned char sendbuf
[1] = { 0 };
161 struct IR_rx
*rx
= ir
->rx
;
166 if (lirc_buffer_full(&rx
->buf
)) {
167 dprintk("buffer overflow\n");
172 * service the device as long as it is returning
173 * data and we have space
176 if (kthread_should_stop())
180 * Lock i2c bus for the duration. RX/TX chips interfere so
183 mutex_lock(&ir
->ir_lock
);
185 if (kthread_should_stop()) {
186 mutex_unlock(&ir
->ir_lock
);
191 * Send random "poll command" (?) Windows driver does this
192 * and it is a good point to detect chip failure.
194 ret
= i2c_master_send(rx
->c
, sendbuf
, 1);
196 zilog_error("i2c_master_send failed with %d\n", ret
);
198 mutex_unlock(&ir
->ir_lock
);
199 zilog_error("unable to read from the IR chip "
200 "after 3 resets, giving up\n");
204 /* Looks like the chip crashed, reset it */
205 zilog_error("polling the IR receiver chip failed, "
208 set_current_state(TASK_UNINTERRUPTIBLE
);
209 if (kthread_should_stop()) {
210 mutex_unlock(&ir
->ir_lock
);
213 schedule_timeout((100 * HZ
+ 999) / 1000);
215 ir
->tx
->need_boot
= 1;
218 mutex_unlock(&ir
->ir_lock
);
222 if (kthread_should_stop()) {
223 mutex_unlock(&ir
->ir_lock
);
226 ret
= i2c_master_recv(rx
->c
, keybuf
, sizeof(keybuf
));
227 mutex_unlock(&ir
->ir_lock
);
228 if (ret
!= sizeof(keybuf
)) {
229 zilog_error("i2c_master_recv failed with %d -- "
230 "keeping last read buffer\n", ret
);
232 rx
->b
[0] = keybuf
[3];
233 rx
->b
[1] = keybuf
[4];
234 rx
->b
[2] = keybuf
[5];
235 dprintk("key (0x%02x/0x%02x)\n", rx
->b
[0], rx
->b
[1]);
239 if (rx
->hdpvr_data_fmt
) {
240 if (got_data
&& (keybuf
[0] == 0x80))
242 else if (got_data
&& (keybuf
[0] == 0x00))
244 } else if ((rx
->b
[0] & 0x80) == 0)
245 return got_data
? 0 : -ENODATA
;
247 /* look what we have */
248 code
= (((__u16
)rx
->b
[0] & 0x7f) << 6) | (rx
->b
[1] >> 2);
250 codes
[0] = (code
>> 8) & 0xff;
251 codes
[1] = code
& 0xff;
254 lirc_buffer_write(&rx
->buf
, codes
);
256 } while (!lirc_buffer_full(&rx
->buf
));
262 * Main function of the polling thread -- from lirc_dev.
263 * We don't fit the LIRC model at all anymore. This is horrible, but
264 * basically we have a single RX/TX device with a nasty failure mode
265 * that needs to be accounted for across the pair. lirc lets us provide
266 * fops, but prevents us from using the internal polling, etc. if we do
267 * so. Hence the replication. Might be neater to extend the LIRC model
268 * to account for this but I'd think it's a very special case of seriously
269 * messed up hardware.
271 static int lirc_thread(void *arg
)
274 struct IR_rx
*rx
= ir
->rx
;
276 dprintk("poll thread started\n");
278 while (!kthread_should_stop()) {
279 set_current_state(TASK_INTERRUPTIBLE
);
281 /* if device not opened, we can sleep half a second */
282 if (atomic_read(&ir
->open_count
) == 0) {
283 schedule_timeout(HZ
/2);
288 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
289 * We use this interval as the chip resets every time you poll
290 * it (bad!). This is therefore just sufficient to catch all
291 * of the button presses. It makes the remote much more
292 * responsive. You can see the difference by running irw and
293 * holding down a button. With 100ms, the old polling
294 * interval, you'll notice breaks in the repeat sequence
295 * corresponding to lost keypresses.
297 schedule_timeout((260 * HZ
) / 1000);
298 if (kthread_should_stop())
301 wake_up_interruptible(&rx
->buf
.wait_poll
);
304 dprintk("poll thread ended\n");
308 static int set_use_inc(void *data
)
313 static void set_use_dec(void *data
)
318 /* safe read of a uint32 (always network byte order) */
319 static int read_uint32(unsigned char **data
,
320 unsigned char *endp
, unsigned int *val
)
322 if (*data
+ 4 > endp
)
324 *val
= ((*data
)[0] << 24) | ((*data
)[1] << 16) |
325 ((*data
)[2] << 8) | (*data
)[3];
330 /* safe read of a uint8 */
331 static int read_uint8(unsigned char **data
,
332 unsigned char *endp
, unsigned char *val
)
334 if (*data
+ 1 > endp
)
340 /* safe skipping of N bytes */
341 static int skip(unsigned char **data
,
342 unsigned char *endp
, unsigned int distance
)
344 if (*data
+ distance
> endp
)
350 /* decompress key data into the given buffer */
351 static int get_key_data(unsigned char *buf
,
352 unsigned int codeset
, unsigned int key
)
354 unsigned char *data
, *endp
, *diffs
, *key_block
;
355 unsigned char keys
, ndiffs
, id
;
356 unsigned int base
, lim
, pos
, i
;
358 /* Binary search for the codeset */
359 for (base
= 0, lim
= tx_data
->num_code_sets
; lim
; lim
>>= 1) {
360 pos
= base
+ (lim
>> 1);
361 data
= tx_data
->code_sets
[pos
];
363 if (!read_uint32(&data
, tx_data
->endp
, &i
))
368 else if (codeset
> i
) {
377 /* Set end of data block */
378 endp
= pos
< tx_data
->num_code_sets
- 1 ?
379 tx_data
->code_sets
[pos
+ 1] : tx_data
->endp
;
381 /* Read the block header */
382 if (!read_uint8(&data
, endp
, &keys
) ||
383 !read_uint8(&data
, endp
, &ndiffs
) ||
384 ndiffs
> TX_BLOCK_SIZE
|| keys
== 0)
387 /* Save diffs & skip */
389 if (!skip(&data
, endp
, ndiffs
))
392 /* Read the id of the first key */
393 if (!read_uint8(&data
, endp
, &id
))
396 /* Unpack the first key's data */
397 for (i
= 0; i
< TX_BLOCK_SIZE
; ++i
) {
398 if (tx_data
->fixed
[i
] == -1) {
399 if (!read_uint8(&data
, endp
, &buf
[i
]))
402 buf
[i
] = (unsigned char)tx_data
->fixed
[i
];
406 /* Early out key found/not found */
414 if (!skip(&data
, endp
, (keys
- 1) * (ndiffs
+ 1)))
417 /* Binary search for the key */
418 for (base
= 0, lim
= keys
- 1; lim
; lim
>>= 1) {
420 unsigned char *key_data
;
421 pos
= base
+ (lim
>> 1);
422 key_data
= key_block
+ (ndiffs
+ 1) * pos
;
424 if (*key_data
== key
) {
428 /* found, so unpack the diffs */
429 for (i
= 0; i
< ndiffs
; ++i
) {
431 if (!read_uint8(&key_data
, endp
, &val
) ||
432 diffs
[i
] >= TX_BLOCK_SIZE
)
438 } else if (key
> *key_data
) {
447 zilog_error("firmware is corrupt\n");
451 /* send a block of data to the IR TX device */
452 static int send_data_block(struct IR_tx
*tx
, unsigned char *data_block
)
455 unsigned char buf
[5];
457 for (i
= 0; i
< TX_BLOCK_SIZE
;) {
458 int tosend
= TX_BLOCK_SIZE
- i
;
461 buf
[0] = (unsigned char)(i
+ 1);
462 for (j
= 0; j
< tosend
; ++j
)
463 buf
[1 + j
] = data_block
[i
+ j
];
464 dprintk("%02x %02x %02x %02x %02x",
465 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4]);
466 ret
= i2c_master_send(tx
->c
, buf
, tosend
+ 1);
467 if (ret
!= tosend
+ 1) {
468 zilog_error("i2c_master_send failed with %d\n", ret
);
469 return ret
< 0 ? ret
: -EFAULT
;
476 /* send boot data to the IR TX device */
477 static int send_boot_data(struct IR_tx
*tx
)
480 unsigned char buf
[4];
482 /* send the boot block */
483 ret
= send_data_block(tx
, tx_data
->boot_data
);
487 /* Hit the go button to activate the new boot data */
490 ret
= i2c_master_send(tx
->c
, buf
, 2);
492 zilog_error("i2c_master_send failed with %d\n", ret
);
493 return ret
< 0 ? ret
: -EFAULT
;
497 * Wait for zilog to settle after hitting go post boot block upload.
498 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
499 * upon attempting to get firmware revision, and tx probe thus fails.
501 for (i
= 0; i
< 10; i
++) {
502 ret
= i2c_master_send(tx
->c
, buf
, 1);
509 zilog_error("i2c_master_send failed with %d\n", ret
);
510 return ret
< 0 ? ret
: -EFAULT
;
513 /* Here comes the firmware version... (hopefully) */
514 ret
= i2c_master_recv(tx
->c
, buf
, 4);
516 zilog_error("i2c_master_recv failed with %d\n", ret
);
519 if ((buf
[0] != 0x80) && (buf
[0] != 0xa0)) {
520 zilog_error("unexpected IR TX init response: %02x\n", buf
[0]);
523 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
524 "%d.%d.%d loaded\n", buf
[1], buf
[2], buf
[3]);
529 /* unload "firmware", lock held */
530 static void fw_unload_locked(void)
533 if (tx_data
->code_sets
)
534 vfree(tx_data
->code_sets
);
537 vfree(tx_data
->datap
);
541 dprintk("successfully unloaded IR blaster firmware\n");
545 /* unload "firmware" for the IR TX device */
546 static void fw_unload(void)
548 mutex_lock(&tx_data_lock
);
550 mutex_unlock(&tx_data_lock
);
553 /* load "firmware" for the IR TX device */
554 static int fw_load(struct IR_tx
*tx
)
558 unsigned char *data
, version
, num_global_fixed
;
559 const struct firmware
*fw_entry
;
561 /* Already loaded? */
562 mutex_lock(&tx_data_lock
);
568 /* Request codeset data file */
569 ret
= request_firmware(&fw_entry
, "haup-ir-blaster.bin", &tx
->c
->dev
);
571 zilog_error("firmware haup-ir-blaster.bin not available "
573 ret
= ret
< 0 ? ret
: -EFAULT
;
576 dprintk("firmware of size %zu loaded\n", fw_entry
->size
);
579 tx_data
= vmalloc(sizeof(*tx_data
));
580 if (tx_data
== NULL
) {
581 zilog_error("out of memory\n");
582 release_firmware(fw_entry
);
586 tx_data
->code_sets
= NULL
;
588 /* Copy the data so hotplug doesn't get confused and timeout */
589 tx_data
->datap
= vmalloc(fw_entry
->size
);
590 if (tx_data
->datap
== NULL
) {
591 zilog_error("out of memory\n");
592 release_firmware(fw_entry
);
597 memcpy(tx_data
->datap
, fw_entry
->data
, fw_entry
->size
);
598 tx_data
->endp
= tx_data
->datap
+ fw_entry
->size
;
599 release_firmware(fw_entry
); fw_entry
= NULL
;
602 data
= tx_data
->datap
;
603 if (!read_uint8(&data
, tx_data
->endp
, &version
))
606 zilog_error("unsupported code set file version (%u, expected"
607 "1) -- please upgrade to a newer driver",
614 /* Save boot block for later */
615 tx_data
->boot_data
= data
;
616 if (!skip(&data
, tx_data
->endp
, TX_BLOCK_SIZE
))
619 if (!read_uint32(&data
, tx_data
->endp
,
620 &tx_data
->num_code_sets
))
623 dprintk("%u IR blaster codesets loaded\n", tx_data
->num_code_sets
);
625 tx_data
->code_sets
= vmalloc(
626 tx_data
->num_code_sets
* sizeof(char *));
627 if (tx_data
->code_sets
== NULL
) {
633 for (i
= 0; i
< TX_BLOCK_SIZE
; ++i
)
634 tx_data
->fixed
[i
] = -1;
636 /* Read global fixed data template */
637 if (!read_uint8(&data
, tx_data
->endp
, &num_global_fixed
) ||
638 num_global_fixed
> TX_BLOCK_SIZE
)
640 for (i
= 0; i
< num_global_fixed
; ++i
) {
641 unsigned char pos
, val
;
642 if (!read_uint8(&data
, tx_data
->endp
, &pos
) ||
643 !read_uint8(&data
, tx_data
->endp
, &val
) ||
644 pos
>= TX_BLOCK_SIZE
)
646 tx_data
->fixed
[pos
] = (int)val
;
649 /* Filch out the position of each code set */
650 for (i
= 0; i
< tx_data
->num_code_sets
; ++i
) {
653 unsigned char ndiffs
;
655 /* Save the codeset position */
656 tx_data
->code_sets
[i
] = data
;
659 if (!read_uint32(&data
, tx_data
->endp
, &id
) ||
660 !read_uint8(&data
, tx_data
->endp
, &keys
) ||
661 !read_uint8(&data
, tx_data
->endp
, &ndiffs
) ||
662 ndiffs
> TX_BLOCK_SIZE
|| keys
== 0)
665 /* skip diff positions */
666 if (!skip(&data
, tx_data
->endp
, ndiffs
))
670 * After the diffs we have the first key id + data -
673 if (!skip(&data
, tx_data
->endp
,
674 1 + TX_BLOCK_SIZE
- num_global_fixed
))
677 /* Then we have keys-1 blocks of key id+diffs */
678 if (!skip(&data
, tx_data
->endp
,
679 (ndiffs
+ 1) * (keys
- 1)))
686 zilog_error("firmware is corrupt\n");
691 mutex_unlock(&tx_data_lock
);
695 /* initialise the IR TX device */
696 static int tx_init(struct IR_tx
*tx
)
700 /* Load 'firmware' */
705 /* Send boot block */
706 ret
= send_boot_data(tx
);
715 /* do nothing stub to make LIRC happy */
716 static loff_t
lseek(struct file
*filep
, loff_t offset
, int orig
)
721 /* copied from lirc_dev */
722 static ssize_t
read(struct file
*filep
, char *outbuf
, size_t n
, loff_t
*ppos
)
724 struct IR
*ir
= filep
->private_data
;
725 struct IR_rx
*rx
= ir
->rx
;
726 int ret
= 0, written
= 0;
727 DECLARE_WAITQUEUE(wait
, current
);
729 dprintk("read called\n");
733 if (mutex_lock_interruptible(&rx
->buf_lock
))
736 if (n
% rx
->buf
.chunk_size
) {
737 dprintk("read result = -EINVAL\n");
738 mutex_unlock(&rx
->buf_lock
);
743 * we add ourselves to the task queue before buffer check
744 * to avoid losing scan code (in case when queue is awaken somewhere
745 * between while condition checking and scheduling)
747 add_wait_queue(&rx
->buf
.wait_poll
, &wait
);
748 set_current_state(TASK_INTERRUPTIBLE
);
751 * while we didn't provide 'length' bytes, device is opened in blocking
752 * mode and 'copy_to_user' is happy, wait for data.
754 while (written
< n
&& ret
== 0) {
755 if (lirc_buffer_empty(&rx
->buf
)) {
757 * According to the read(2) man page, 'written' can be
758 * returned as less than 'n', instead of blocking
759 * again, returning -EWOULDBLOCK, or returning
764 if (filep
->f_flags
& O_NONBLOCK
) {
768 if (signal_pending(current
)) {
773 set_current_state(TASK_INTERRUPTIBLE
);
775 unsigned char buf
[rx
->buf
.chunk_size
];
776 lirc_buffer_read(&rx
->buf
, buf
);
777 ret
= copy_to_user((void *)outbuf
+written
, buf
,
779 written
+= rx
->buf
.chunk_size
;
783 remove_wait_queue(&rx
->buf
.wait_poll
, &wait
);
784 set_current_state(TASK_RUNNING
);
785 mutex_unlock(&rx
->buf_lock
);
787 dprintk("read result = %s (%d)\n",
788 ret
? "-EFAULT" : "OK", ret
);
790 return ret
? ret
: written
;
793 /* send a keypress to the IR TX device */
794 static int send_code(struct IR_tx
*tx
, unsigned int code
, unsigned int key
)
796 unsigned char data_block
[TX_BLOCK_SIZE
];
797 unsigned char buf
[2];
800 /* Get data for the codeset/key */
801 ret
= get_key_data(data_block
, code
, key
);
803 if (ret
== -EPROTO
) {
804 zilog_error("failed to get data for code %u, key %u -- check "
805 "lircd.conf entries\n", code
, key
);
810 /* Send the data block */
811 ret
= send_data_block(tx
, data_block
);
815 /* Send data block length? */
818 ret
= i2c_master_send(tx
->c
, buf
, 2);
820 zilog_error("i2c_master_send failed with %d\n", ret
);
821 return ret
< 0 ? ret
: -EFAULT
;
824 /* Give the z8 a moment to process data block */
825 for (i
= 0; i
< 10; i
++) {
826 ret
= i2c_master_send(tx
->c
, buf
, 1);
833 zilog_error("i2c_master_send failed with %d\n", ret
);
834 return ret
< 0 ? ret
: -EFAULT
;
837 /* Send finished download? */
838 ret
= i2c_master_recv(tx
->c
, buf
, 1);
840 zilog_error("i2c_master_recv failed with %d\n", ret
);
841 return ret
< 0 ? ret
: -EFAULT
;
843 if (buf
[0] != 0xA0) {
844 zilog_error("unexpected IR TX response #1: %02x\n",
849 /* Send prepare command? */
852 ret
= i2c_master_send(tx
->c
, buf
, 2);
854 zilog_error("i2c_master_send failed with %d\n", ret
);
855 return ret
< 0 ? ret
: -EFAULT
;
859 * The sleep bits aren't necessary on the HD PVR, and in fact, the
860 * last i2c_master_recv always fails with a -5, so for now, we're
861 * going to skip this whole mess and say we're done on the HD PVR
863 if (!tx
->post_tx_ready_poll
) {
864 dprintk("sent code %u, key %u\n", code
, key
);
869 * This bit NAKs until the device is ready, so we retry it
870 * sleeping a bit each time. This seems to be what the windows
871 * driver does, approximately.
874 for (i
= 0; i
< 20; ++i
) {
875 set_current_state(TASK_UNINTERRUPTIBLE
);
876 schedule_timeout((50 * HZ
+ 999) / 1000);
877 ret
= i2c_master_send(tx
->c
, buf
, 1);
880 dprintk("NAK expected: i2c_master_send "
881 "failed with %d (try %d)\n", ret
, i
+1);
884 zilog_error("IR TX chip never got ready: last i2c_master_send "
885 "failed with %d\n", ret
);
886 return ret
< 0 ? ret
: -EFAULT
;
889 /* Seems to be an 'ok' response */
890 i
= i2c_master_recv(tx
->c
, buf
, 1);
892 zilog_error("i2c_master_recv failed with %d\n", ret
);
895 if (buf
[0] != 0x80) {
896 zilog_error("unexpected IR TX response #2: %02x\n", buf
[0]);
900 /* Oh good, it worked */
901 dprintk("sent code %u, key %u\n", code
, key
);
906 * Write a code to the device. We take in a 32-bit number (an int) and then
907 * decode this to a codeset/key index. The key data is then decompressed and
908 * sent to the device. We have a spin lock as per i2c documentation to prevent
909 * multiple concurrent sends which would probably cause the device to explode.
911 static ssize_t
write(struct file
*filep
, const char *buf
, size_t n
,
914 struct IR
*ir
= filep
->private_data
;
915 struct IR_tx
*tx
= ir
->tx
;
922 /* Validate user parameters */
926 /* Lock i2c bus for the duration */
927 mutex_lock(&ir
->ir_lock
);
929 /* Send each keypress */
930 for (i
= 0; i
< n
;) {
934 if (copy_from_user(&command
, buf
+ i
, sizeof(command
))) {
935 mutex_unlock(&ir
->ir_lock
);
939 /* Send boot data first if required */
940 if (tx
->need_boot
== 1) {
941 ret
= send_boot_data(tx
);
948 ret
= send_code(tx
, (unsigned)command
>> 16,
949 (unsigned)command
& 0xFFFF);
950 if (ret
== -EPROTO
) {
951 mutex_unlock(&ir
->ir_lock
);
957 * Hmm, a failure. If we've had a few then give up, otherwise
961 /* Looks like the chip crashed, reset it */
962 zilog_error("sending to the IR transmitter chip "
963 "failed, trying reset\n");
966 zilog_error("unable to send to the IR chip "
967 "after 3 resets, giving up\n");
968 mutex_unlock(&ir
->ir_lock
);
971 set_current_state(TASK_UNINTERRUPTIBLE
);
972 schedule_timeout((100 * HZ
+ 999) / 1000);
979 /* Release i2c bus */
980 mutex_unlock(&ir
->ir_lock
);
986 /* copied from lirc_dev */
987 static unsigned int poll(struct file
*filep
, poll_table
*wait
)
989 struct IR
*ir
= filep
->private_data
;
990 struct IR_rx
*rx
= ir
->rx
;
993 dprintk("poll called\n");
997 mutex_lock(&rx
->buf_lock
);
999 poll_wait(filep
, &rx
->buf
.wait_poll
, wait
);
1001 dprintk("poll result = %s\n",
1002 lirc_buffer_empty(&rx
->buf
) ? "0" : "POLLIN|POLLRDNORM");
1004 ret
= lirc_buffer_empty(&rx
->buf
) ? 0 : (POLLIN
|POLLRDNORM
);
1006 mutex_unlock(&rx
->buf_lock
);
1010 static long ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
1012 struct IR
*ir
= filep
->private_data
;
1014 unsigned long mode
, features
= 0;
1017 features
|= LIRC_CAN_REC_LIRCCODE
;
1019 features
|= LIRC_CAN_SEND_PULSE
;
1022 case LIRC_GET_LENGTH
:
1023 result
= put_user((unsigned long)13,
1024 (unsigned long *)arg
);
1026 case LIRC_GET_FEATURES
:
1027 result
= put_user(features
, (unsigned long *) arg
);
1029 case LIRC_GET_REC_MODE
:
1030 if (!(features
&LIRC_CAN_REC_MASK
))
1033 result
= put_user(LIRC_REC2MODE
1034 (features
&LIRC_CAN_REC_MASK
),
1035 (unsigned long *)arg
);
1037 case LIRC_SET_REC_MODE
:
1038 if (!(features
&LIRC_CAN_REC_MASK
))
1041 result
= get_user(mode
, (unsigned long *)arg
);
1042 if (!result
&& !(LIRC_MODE2REC(mode
) & features
))
1045 case LIRC_GET_SEND_MODE
:
1046 if (!(features
&LIRC_CAN_SEND_MASK
))
1049 result
= put_user(LIRC_MODE_PULSE
, (unsigned long *) arg
);
1051 case LIRC_SET_SEND_MODE
:
1052 if (!(features
&LIRC_CAN_SEND_MASK
))
1055 result
= get_user(mode
, (unsigned long *) arg
);
1056 if (!result
&& mode
!= LIRC_MODE_PULSE
)
1065 /* ir_devices_lock must be held */
1066 static struct IR
*find_ir_device_by_minor(unsigned int minor
)
1070 if (list_empty(&ir_devices_list
))
1073 list_for_each_entry(ir
, &ir_devices_list
, list
)
1074 if (ir
->l
.minor
== minor
)
1081 * Open the IR device. Get hold of our IR structure and
1082 * stash it in private_data for the file
1084 static int open(struct inode
*node
, struct file
*filep
)
1087 unsigned int minor
= MINOR(node
->i_rdev
);
1089 /* find our IR struct */
1090 mutex_lock(&ir_devices_lock
);
1091 ir
= find_ir_device_by_minor(minor
);
1092 mutex_unlock(&ir_devices_lock
);
1097 atomic_inc(&ir
->open_count
);
1099 /* stash our IR struct */
1100 filep
->private_data
= ir
;
1105 /* Close the IR device */
1106 static int close(struct inode
*node
, struct file
*filep
)
1108 /* find our IR struct */
1109 struct IR
*ir
= filep
->private_data
;
1111 zilog_error("close: no private_data attached to the file!\n");
1115 atomic_dec(&ir
->open_count
);
1120 static struct lirc_driver lirc_template
= {
1121 .name
= "lirc_zilog",
1122 .set_use_inc
= set_use_inc
,
1123 .set_use_dec
= set_use_dec
,
1124 .owner
= THIS_MODULE
1127 static int ir_remove(struct i2c_client
*client
);
1128 static int ir_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
);
1130 #define ID_FLAG_TX 0x01
1131 #define ID_FLAG_HDPVR 0x02
1133 static const struct i2c_device_id ir_transceiver_id
[] = {
1134 { "ir_tx_z8f0811_haup", ID_FLAG_TX
},
1135 { "ir_rx_z8f0811_haup", 0 },
1136 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR
| ID_FLAG_TX
},
1137 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR
},
1141 static struct i2c_driver driver
= {
1143 .owner
= THIS_MODULE
,
1144 .name
= "Zilog/Hauppauge i2c IR",
1147 .remove
= ir_remove
,
1148 .id_table
= ir_transceiver_id
,
1151 static const struct file_operations lirc_fops
= {
1152 .owner
= THIS_MODULE
,
1157 .unlocked_ioctl
= ioctl
,
1158 #ifdef CONFIG_COMPAT
1159 .compat_ioctl
= ioctl
,
1165 static void destroy_rx_kthread(struct IR_rx
*rx
)
1167 /* end up polling thread */
1168 if (rx
!= NULL
&& !IS_ERR_OR_NULL(rx
->task
)) {
1169 kthread_stop(rx
->task
);
1174 /* ir_devices_lock must be held */
1175 static int add_ir_device(struct IR
*ir
)
1177 list_add_tail(&ir
->list
, &ir_devices_list
);
1181 /* ir_devices_lock must be held */
1182 static void del_ir_device(struct IR
*ir
)
1186 if (list_empty(&ir_devices_list
))
1189 list_for_each_entry(p
, &ir_devices_list
, list
)
1196 static int ir_remove(struct i2c_client
*client
)
1198 struct IR
*ir
= i2c_get_clientdata(client
);
1200 mutex_lock(&ir_devices_lock
);
1203 /* We destroyed everything when the first client came through */
1204 mutex_unlock(&ir_devices_lock
);
1209 lirc_unregister_driver(ir
->l
.minor
);
1212 destroy_rx_kthread(ir
->rx
);
1213 if (ir
->rx
!= NULL
) {
1214 if (ir
->rx
->buf
.fifo_initialized
)
1215 lirc_buffer_free(&ir
->rx
->buf
);
1216 i2c_set_clientdata(ir
->rx
->c
, NULL
);
1221 if (ir
->tx
!= NULL
) {
1222 i2c_set_clientdata(ir
->tx
->c
, NULL
);
1230 mutex_unlock(&ir_devices_lock
);
1235 /* ir_devices_lock must be held */
1236 static struct IR
*find_ir_device_by_adapter(struct i2c_adapter
*adapter
)
1240 if (list_empty(&ir_devices_list
))
1243 list_for_each_entry(ir
, &ir_devices_list
, list
)
1244 if (ir
->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 INIT_LIST_HEAD(&ir
->list
);
1285 ret
= add_ir_device(ir
);
1290 mutex_init(&ir
->ir_lock
);
1291 atomic_set(&ir
->open_count
, 0);
1293 /* set lirc_dev stuff */
1294 memcpy(&ir
->l
, &lirc_template
, sizeof(struct lirc_driver
));
1295 ir
->l
.minor
= minor
; /* module option */
1296 ir
->l
.code_length
= 13;
1298 ir
->l
.fops
= &lirc_fops
;
1300 ir
->l
.dev
= &adap
->dev
;
1301 ir
->l
.sample_rate
= 0;
1305 /* Set up a struct IR_tx instance */
1306 ir
->tx
= kzalloc(sizeof(struct IR_tx
), GFP_KERNEL
);
1307 if (ir
->tx
== NULL
) {
1313 ir
->tx
->need_boot
= 1;
1314 ir
->tx
->post_tx_ready_poll
=
1315 (id
->driver_data
& ID_FLAG_HDPVR
) ? false : true;
1317 /* Set up a struct IR_rx instance */
1318 ir
->rx
= kzalloc(sizeof(struct IR_rx
), GFP_KERNEL
);
1319 if (ir
->rx
== NULL
) {
1324 ret
= lirc_buffer_init(&ir
->rx
->buf
, 2, BUFLEN
/ 2);
1328 mutex_init(&ir
->rx
->buf_lock
);
1330 ir
->rx
->hdpvr_data_fmt
=
1331 (id
->driver_data
& ID_FLAG_HDPVR
) ? true : false;
1333 /* set lirc_dev stuff */
1334 ir
->l
.rbuf
= &ir
->rx
->buf
;
1337 i2c_set_clientdata(client
, ir
);
1339 /* Proceed only if we have the required Tx and Rx clients ready to go */
1340 if (ir
->tx
== NULL
||
1341 (ir
->rx
== NULL
&& !tx_only
)) {
1342 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1343 "IR %s.\n", tx_probe
? "Tx" : "Rx", adap
->name
,
1344 adap
->nr
, tx_probe
? "Rx" : "Tx");
1348 /* initialise RX device */
1349 if (ir
->rx
!= NULL
) {
1350 /* try to fire up polling thread */
1351 ir
->rx
->task
= kthread_run(lirc_thread
, ir
,
1352 "zilog-rx-i2c-%d", adap
->nr
);
1353 if (IS_ERR(ir
->rx
->task
)) {
1354 ret
= PTR_ERR(ir
->rx
->task
);
1355 zilog_error("%s: could not start IR Rx polling thread"
1361 /* register with lirc */
1362 ir
->l
.minor
= lirc_register_driver(&ir
->l
);
1363 if (ir
->l
.minor
< 0 || ir
->l
.minor
>= MAX_IRCTL_DEVICES
) {
1364 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1365 __func__
, MAX_IRCTL_DEVICES
-1, ir
->l
.minor
);
1367 goto out_free_thread
;
1371 * if we have the tx device, load the 'firmware'. We do this
1372 * after registering with lirc as otherwise hotplug seems to take
1373 * 10s to create the lirc device.
1375 if (ir
->tx
!= NULL
) {
1376 /* Special TX init */
1377 ret
= tx_init(ir
->tx
);
1379 goto out_unregister
;
1382 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1383 tx_probe
? "Tx" : "Rx", adap
->name
, adap
->nr
);
1385 mutex_unlock(&ir_devices_lock
);
1389 lirc_unregister_driver(ir
->l
.minor
);
1391 destroy_rx_kthread(ir
->rx
);
1393 if (ir
->rx
!= NULL
) {
1394 if (ir
->rx
->buf
.fifo_initialized
)
1395 lirc_buffer_free(&ir
->rx
->buf
);
1396 if (ir
->rx
->c
!= NULL
)
1397 i2c_set_clientdata(ir
->rx
->c
, NULL
);
1400 if (ir
->tx
!= NULL
) {
1401 if (ir
->tx
->c
!= NULL
)
1402 i2c_set_clientdata(ir
->tx
->c
, NULL
);
1409 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1410 __func__
, tx_probe
? "Tx" : "Rx", adap
->name
, adap
->nr
,
1412 mutex_unlock(&ir_devices_lock
);
1416 static int __init
zilog_init(void)
1420 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1422 mutex_init(&tx_data_lock
);
1424 request_module("firmware_class");
1426 ret
= i2c_add_driver(&driver
);
1428 zilog_error("initialization failed\n");
1430 zilog_notify("initialization complete\n");
1435 static void __exit
zilog_exit(void)
1437 i2c_del_driver(&driver
);
1440 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1443 module_init(zilog_init
);
1444 module_exit(zilog_exit
);
1446 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1447 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1448 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1450 MODULE_LICENSE("GPL");
1451 /* for compat with old name, which isn't all that accurate anymore */
1452 MODULE_ALIAS("lirc_pvr150");
1454 module_param(minor
, int, 0444);
1455 MODULE_PARM_DESC(minor
, "Preferred minor device number");
1457 module_param(debug
, bool, 0644);
1458 MODULE_PARM_DESC(debug
, "Enable debugging messages");
1460 module_param(tx_only
, bool, 0644);
1461 MODULE_PARM_DESC(tx_only
, "Only handle the IR transmit function");