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>
71 struct i2c_client c_rx
;
74 /* RX device buffer & lock */
75 struct lirc_buffer buf
;
76 struct mutex buf_lock
;
78 /* RX polling thread data */
79 struct completion
*t_notify
;
80 struct completion
*t_notify2
;
82 struct task_struct
*task
;
88 struct i2c_client c_tx
;
93 /* Minor -> data mapping */
94 static struct IR
*ir_devices
[MAX_IRCTL_DEVICES
];
96 /* Block size for IR transmitter */
97 #define TX_BLOCK_SIZE 99
99 /* Hauppauge IR transmitter data */
100 struct tx_data_struct
{
102 unsigned char *boot_data
;
104 /* Start of binary data block */
105 unsigned char *datap
;
107 /* End of binary data block */
110 /* Number of installed codesets */
111 unsigned int num_code_sets
;
113 /* Pointers to codesets */
114 unsigned char **code_sets
;
116 /* Global fixed data template */
117 int fixed
[TX_BLOCK_SIZE
];
120 static struct tx_data_struct
*tx_data
;
121 static struct mutex tx_data_lock
;
123 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
125 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
127 #define ZILOG_HAUPPAUGE_IR_RX_NAME "Zilog/Hauppauge IR RX"
128 #define ZILOG_HAUPPAUGE_IR_TX_NAME "Zilog/Hauppauge IR TX"
130 /* module parameters */
131 static int debug
; /* debug output */
132 static int disable_rx
; /* disable RX device */
133 static int disable_tx
; /* disable TX device */
134 static int minor
= -1; /* minor number */
136 #define dprintk(fmt, args...) \
139 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
143 static int add_to_buf(struct IR
*ir
)
146 unsigned char codes
[2];
147 unsigned char keybuf
[6];
151 unsigned char sendbuf
[1] = { 0 };
153 if (lirc_buffer_full(&ir
->buf
)) {
154 dprintk("buffer overflow\n");
159 * service the device as long as it is returning
160 * data and we have space
164 * Lock i2c bus for the duration. RX/TX chips interfere so
167 mutex_lock(&ir
->ir_lock
);
170 * Send random "poll command" (?) Windows driver does this
171 * and it is a good point to detect chip failure.
173 ret
= i2c_master_send(&ir
->c_rx
, sendbuf
, 1);
175 zilog_error("i2c_master_send failed with %d\n", ret
);
177 mutex_unlock(&ir
->ir_lock
);
178 zilog_error("unable to read from the IR chip "
179 "after 3 resets, giving up\n");
183 /* Looks like the chip crashed, reset it */
184 zilog_error("polling the IR receiver chip failed, "
187 set_current_state(TASK_UNINTERRUPTIBLE
);
188 schedule_timeout((100 * HZ
+ 999) / 1000);
192 mutex_unlock(&ir
->ir_lock
);
196 ret
= i2c_master_recv(&ir
->c_rx
, keybuf
, sizeof(keybuf
));
197 mutex_unlock(&ir
->ir_lock
);
198 if (ret
!= sizeof(keybuf
)) {
199 zilog_error("i2c_master_recv failed with %d -- "
200 "keeping last read buffer\n", ret
);
202 ir
->b
[0] = keybuf
[3];
203 ir
->b
[1] = keybuf
[4];
204 ir
->b
[2] = keybuf
[5];
205 dprintk("key (0x%02x/0x%02x)\n", ir
->b
[0], ir
->b
[1]);
209 #ifdef I2C_HW_B_HDPVR
210 if (ir
->c_rx
.adapter
->id
== I2C_HW_B_HDPVR
) {
211 if (got_data
&& (keybuf
[0] == 0x80))
213 else if (got_data
&& (keybuf
[0] == 0x00))
215 } else if ((ir
->b
[0] & 0x80) == 0)
217 if ((ir
->b
[0] & 0x80) == 0)
219 return got_data
? 0 : -ENODATA
;
221 /* look what we have */
222 code
= (((__u16
)ir
->b
[0] & 0x7f) << 6) | (ir
->b
[1] >> 2);
224 codes
[0] = (code
>> 8) & 0xff;
225 codes
[1] = code
& 0xff;
228 lirc_buffer_write(&ir
->buf
, codes
);
230 } while (!lirc_buffer_full(&ir
->buf
));
236 * Main function of the polling thread -- from lirc_dev.
237 * We don't fit the LIRC model at all anymore. This is horrible, but
238 * basically we have a single RX/TX device with a nasty failure mode
239 * that needs to be accounted for across the pair. lirc lets us provide
240 * fops, but prevents us from using the internal polling, etc. if we do
241 * so. Hence the replication. Might be neater to extend the LIRC model
242 * to account for this but I'd think it's a very special case of seriously
243 * messed up hardware.
245 static int lirc_thread(void *arg
)
249 if (ir
->t_notify
!= NULL
)
250 complete(ir
->t_notify
);
252 dprintk("poll thread started\n");
256 set_current_state(TASK_INTERRUPTIBLE
);
259 * This is ~113*2 + 24 + jitter (2*repeat gap +
260 * code length). We use this interval as the chip
261 * resets every time you poll it (bad!). This is
262 * therefore just sufficient to catch all of the
263 * button presses. It makes the remote much more
264 * responsive. You can see the difference by
265 * running irw and holding down a button. With
266 * 100ms, the old polling interval, you'll notice
267 * breaks in the repeat sequence corresponding to
270 schedule_timeout((260 * HZ
) / 1000);
274 wake_up_interruptible(&ir
->buf
.wait_poll
);
276 /* if device not opened so we can sleep half a second */
277 set_current_state(TASK_INTERRUPTIBLE
);
278 schedule_timeout(HZ
/2);
280 } while (!ir
->shutdown
);
282 if (ir
->t_notify2
!= NULL
)
283 wait_for_completion(ir
->t_notify2
);
286 if (ir
->t_notify
!= NULL
)
287 complete(ir
->t_notify
);
289 dprintk("poll thread ended\n");
293 static int set_use_inc(void *data
)
295 struct IR
*ir
= data
;
297 if (ir
->l
.owner
== NULL
|| try_module_get(ir
->l
.owner
) == 0)
300 /* lock bttv in memory while /dev/lirc is in use */
302 * this is completely broken code. lirc_unregister_driver()
303 * must be possible even when the device is open
306 i2c_use_client(&ir
->c_rx
);
308 i2c_use_client(&ir
->c_tx
);
313 static void set_use_dec(void *data
)
315 struct IR
*ir
= data
;
318 i2c_release_client(&ir
->c_rx
);
320 i2c_release_client(&ir
->c_tx
);
321 if (ir
->l
.owner
!= NULL
)
322 module_put(ir
->l
.owner
);
325 /* safe read of a uint32 (always network byte order) */
326 static int read_uint32(unsigned char **data
,
327 unsigned char *endp
, unsigned int *val
)
329 if (*data
+ 4 > endp
)
331 *val
= ((*data
)[0] << 24) | ((*data
)[1] << 16) |
332 ((*data
)[2] << 8) | (*data
)[3];
337 /* safe read of a uint8 */
338 static int read_uint8(unsigned char **data
,
339 unsigned char *endp
, unsigned char *val
)
341 if (*data
+ 1 > endp
)
347 /* safe skipping of N bytes */
348 static int skip(unsigned char **data
,
349 unsigned char *endp
, unsigned int distance
)
351 if (*data
+ distance
> endp
)
357 /* decompress key data into the given buffer */
358 static int get_key_data(unsigned char *buf
,
359 unsigned int codeset
, unsigned int key
)
361 unsigned char *data
, *endp
, *diffs
, *key_block
;
362 unsigned char keys
, ndiffs
, id
;
363 unsigned int base
, lim
, pos
, i
;
365 /* Binary search for the codeset */
366 for (base
= 0, lim
= tx_data
->num_code_sets
; lim
; lim
>>= 1) {
367 pos
= base
+ (lim
>> 1);
368 data
= tx_data
->code_sets
[pos
];
370 if (!read_uint32(&data
, tx_data
->endp
, &i
))
375 else if (codeset
> i
) {
384 /* Set end of data block */
385 endp
= pos
< tx_data
->num_code_sets
- 1 ?
386 tx_data
->code_sets
[pos
+ 1] : tx_data
->endp
;
388 /* Read the block header */
389 if (!read_uint8(&data
, endp
, &keys
) ||
390 !read_uint8(&data
, endp
, &ndiffs
) ||
391 ndiffs
> TX_BLOCK_SIZE
|| keys
== 0)
394 /* Save diffs & skip */
396 if (!skip(&data
, endp
, ndiffs
))
399 /* Read the id of the first key */
400 if (!read_uint8(&data
, endp
, &id
))
403 /* Unpack the first key's data */
404 for (i
= 0; i
< TX_BLOCK_SIZE
; ++i
) {
405 if (tx_data
->fixed
[i
] == -1) {
406 if (!read_uint8(&data
, endp
, &buf
[i
]))
409 buf
[i
] = (unsigned char)tx_data
->fixed
[i
];
413 /* Early out key found/not found */
421 if (!skip(&data
, endp
, (keys
- 1) * (ndiffs
+ 1)))
424 /* Binary search for the key */
425 for (base
= 0, lim
= keys
- 1; lim
; lim
>>= 1) {
427 unsigned char *key_data
;
428 pos
= base
+ (lim
>> 1);
429 key_data
= key_block
+ (ndiffs
+ 1) * pos
;
431 if (*key_data
== key
) {
435 /* found, so unpack the diffs */
436 for (i
= 0; i
< ndiffs
; ++i
) {
438 if (!read_uint8(&key_data
, endp
, &val
) ||
439 diffs
[i
] >= TX_BLOCK_SIZE
)
445 } else if (key
> *key_data
) {
454 zilog_error("firmware is corrupt\n");
458 /* send a block of data to the IR TX device */
459 static int send_data_block(struct IR
*ir
, unsigned char *data_block
)
462 unsigned char buf
[5];
464 for (i
= 0; i
< TX_BLOCK_SIZE
;) {
465 int tosend
= TX_BLOCK_SIZE
- i
;
468 buf
[0] = (unsigned char)(i
+ 1);
469 for (j
= 0; j
< tosend
; ++j
)
470 buf
[1 + j
] = data_block
[i
+ j
];
471 dprintk("%02x %02x %02x %02x %02x",
472 buf
[0], buf
[1], buf
[2], buf
[3], buf
[4]);
473 ret
= i2c_master_send(&ir
->c_tx
, buf
, tosend
+ 1);
474 if (ret
!= tosend
+ 1) {
475 zilog_error("i2c_master_send failed with %d\n", ret
);
476 return ret
< 0 ? ret
: -EFAULT
;
483 /* send boot data to the IR TX device */
484 static int send_boot_data(struct IR
*ir
)
487 unsigned char buf
[4];
489 /* send the boot block */
490 ret
= send_data_block(ir
, tx_data
->boot_data
);
497 ret
= i2c_master_send(&ir
->c_tx
, buf
, 2);
499 zilog_error("i2c_master_send failed with %d\n", ret
);
500 return ret
< 0 ? ret
: -EFAULT
;
502 ret
= i2c_master_send(&ir
->c_tx
, buf
, 1);
504 zilog_error("i2c_master_send failed with %d\n", ret
);
505 return ret
< 0 ? ret
: -EFAULT
;
508 /* Here comes the firmware version... (hopefully) */
509 ret
= i2c_master_recv(&ir
->c_tx
, buf
, 4);
511 zilog_error("i2c_master_recv failed with %d\n", ret
);
514 if (buf
[0] != 0x80) {
515 zilog_error("unexpected IR TX response: %02x\n", buf
[0]);
518 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
519 "%d.%d.%d loaded\n", buf
[1], buf
[2], buf
[3]);
524 /* unload "firmware", lock held */
525 static void fw_unload_locked(void)
528 if (tx_data
->code_sets
)
529 vfree(tx_data
->code_sets
);
532 vfree(tx_data
->datap
);
536 dprintk("successfully unloaded IR blaster firmware\n");
540 /* unload "firmware" for the IR TX device */
541 static void fw_unload(void)
543 mutex_lock(&tx_data_lock
);
545 mutex_unlock(&tx_data_lock
);
548 /* load "firmware" for the IR TX device */
549 static int fw_load(struct IR
*ir
)
553 unsigned char *data
, version
, num_global_fixed
;
554 const struct firmware
*fw_entry
;
556 /* Already loaded? */
557 mutex_lock(&tx_data_lock
);
563 /* Request codeset data file */
564 ret
= request_firmware(&fw_entry
, "haup-ir-blaster.bin", &ir
->c_tx
.dev
);
566 zilog_error("firmware haup-ir-blaster.bin not available "
568 ret
= ret
< 0 ? ret
: -EFAULT
;
571 dprintk("firmware of size %zu loaded\n", fw_entry
->size
);
574 tx_data
= vmalloc(sizeof(*tx_data
));
575 if (tx_data
== NULL
) {
576 zilog_error("out of memory\n");
577 release_firmware(fw_entry
);
581 tx_data
->code_sets
= NULL
;
583 /* Copy the data so hotplug doesn't get confused and timeout */
584 tx_data
->datap
= vmalloc(fw_entry
->size
);
585 if (tx_data
->datap
== NULL
) {
586 zilog_error("out of memory\n");
587 release_firmware(fw_entry
);
592 memcpy(tx_data
->datap
, fw_entry
->data
, fw_entry
->size
);
593 tx_data
->endp
= tx_data
->datap
+ fw_entry
->size
;
594 release_firmware(fw_entry
); fw_entry
= NULL
;
597 data
= tx_data
->datap
;
598 if (!read_uint8(&data
, tx_data
->endp
, &version
))
601 zilog_error("unsupported code set file version (%u, expected"
602 "1) -- please upgrade to a newer driver",
609 /* Save boot block for later */
610 tx_data
->boot_data
= data
;
611 if (!skip(&data
, tx_data
->endp
, TX_BLOCK_SIZE
))
614 if (!read_uint32(&data
, tx_data
->endp
,
615 &tx_data
->num_code_sets
))
618 dprintk("%u IR blaster codesets loaded\n", tx_data
->num_code_sets
);
620 tx_data
->code_sets
= vmalloc(
621 tx_data
->num_code_sets
* sizeof(char *));
622 if (tx_data
->code_sets
== NULL
) {
628 for (i
= 0; i
< TX_BLOCK_SIZE
; ++i
)
629 tx_data
->fixed
[i
] = -1;
631 /* Read global fixed data template */
632 if (!read_uint8(&data
, tx_data
->endp
, &num_global_fixed
) ||
633 num_global_fixed
> TX_BLOCK_SIZE
)
635 for (i
= 0; i
< num_global_fixed
; ++i
) {
636 unsigned char pos
, val
;
637 if (!read_uint8(&data
, tx_data
->endp
, &pos
) ||
638 !read_uint8(&data
, tx_data
->endp
, &val
) ||
639 pos
>= TX_BLOCK_SIZE
)
641 tx_data
->fixed
[pos
] = (int)val
;
644 /* Filch out the position of each code set */
645 for (i
= 0; i
< tx_data
->num_code_sets
; ++i
) {
648 unsigned char ndiffs
;
650 /* Save the codeset position */
651 tx_data
->code_sets
[i
] = data
;
654 if (!read_uint32(&data
, tx_data
->endp
, &id
) ||
655 !read_uint8(&data
, tx_data
->endp
, &keys
) ||
656 !read_uint8(&data
, tx_data
->endp
, &ndiffs
) ||
657 ndiffs
> TX_BLOCK_SIZE
|| keys
== 0)
660 /* skip diff positions */
661 if (!skip(&data
, tx_data
->endp
, ndiffs
))
665 * After the diffs we have the first key id + data -
668 if (!skip(&data
, tx_data
->endp
,
669 1 + TX_BLOCK_SIZE
- num_global_fixed
))
672 /* Then we have keys-1 blocks of key id+diffs */
673 if (!skip(&data
, tx_data
->endp
,
674 (ndiffs
+ 1) * (keys
- 1)))
681 zilog_error("firmware is corrupt\n");
686 mutex_unlock(&tx_data_lock
);
690 /* initialise the IR TX device */
691 static int tx_init(struct IR
*ir
)
695 /* Load 'firmware' */
700 /* Send boot block */
701 ret
= send_boot_data(ir
);
710 /* do nothing stub to make LIRC happy */
711 static loff_t
lseek(struct file
*filep
, loff_t offset
, int orig
)
716 /* copied from lirc_dev */
717 static ssize_t
read(struct file
*filep
, char *outbuf
, size_t n
, loff_t
*ppos
)
719 struct IR
*ir
= (struct IR
*)filep
->private_data
;
720 unsigned char buf
[ir
->buf
.chunk_size
];
721 int ret
= 0, written
= 0;
722 DECLARE_WAITQUEUE(wait
, current
);
724 dprintk("read called\n");
725 if (ir
->c_rx
.addr
== 0)
728 if (mutex_lock_interruptible(&ir
->buf_lock
))
731 if (n
% ir
->buf
.chunk_size
) {
732 dprintk("read result = -EINVAL\n");
733 mutex_unlock(&ir
->buf_lock
);
738 * we add ourselves to the task queue before buffer check
739 * to avoid losing scan code (in case when queue is awaken somewhere
740 * between while condition checking and scheduling)
742 add_wait_queue(&ir
->buf
.wait_poll
, &wait
);
743 set_current_state(TASK_INTERRUPTIBLE
);
746 * while we didn't provide 'length' bytes, device is opened in blocking
747 * mode and 'copy_to_user' is happy, wait for data.
749 while (written
< n
&& ret
== 0) {
750 if (lirc_buffer_empty(&ir
->buf
)) {
752 * According to the read(2) man page, 'written' can be
753 * returned as less than 'n', instead of blocking
754 * again, returning -EWOULDBLOCK, or returning
759 if (filep
->f_flags
& O_NONBLOCK
) {
763 if (signal_pending(current
)) {
768 set_current_state(TASK_INTERRUPTIBLE
);
770 lirc_buffer_read(&ir
->buf
, buf
);
771 ret
= copy_to_user((void *)outbuf
+written
, buf
,
773 written
+= ir
->buf
.chunk_size
;
777 remove_wait_queue(&ir
->buf
.wait_poll
, &wait
);
778 set_current_state(TASK_RUNNING
);
779 mutex_unlock(&ir
->buf_lock
);
781 dprintk("read result = %s (%d)\n",
782 ret
? "-EFAULT" : "OK", ret
);
784 return ret
? ret
: written
;
787 /* send a keypress to the IR TX device */
788 static int send_code(struct IR
*ir
, unsigned int code
, unsigned int key
)
790 unsigned char data_block
[TX_BLOCK_SIZE
];
791 unsigned char buf
[2];
794 /* Get data for the codeset/key */
795 ret
= get_key_data(data_block
, code
, key
);
797 if (ret
== -EPROTO
) {
798 zilog_error("failed to get data for code %u, key %u -- check "
799 "lircd.conf entries\n", code
, key
);
804 /* Send the data block */
805 ret
= send_data_block(ir
, data_block
);
809 /* Send data block length? */
812 ret
= i2c_master_send(&ir
->c_tx
, buf
, 2);
814 zilog_error("i2c_master_send failed with %d\n", ret
);
815 return ret
< 0 ? ret
: -EFAULT
;
817 ret
= i2c_master_send(&ir
->c_tx
, buf
, 1);
819 zilog_error("i2c_master_send failed with %d\n", ret
);
820 return ret
< 0 ? ret
: -EFAULT
;
823 /* Send finished download? */
824 ret
= i2c_master_recv(&ir
->c_tx
, buf
, 1);
826 zilog_error("i2c_master_recv failed with %d\n", ret
);
827 return ret
< 0 ? ret
: -EFAULT
;
829 if (buf
[0] != 0xA0) {
830 zilog_error("unexpected IR TX response #1: %02x\n",
835 /* Send prepare command? */
838 ret
= i2c_master_send(&ir
->c_tx
, buf
, 2);
840 zilog_error("i2c_master_send failed with %d\n", ret
);
841 return ret
< 0 ? ret
: -EFAULT
;
844 #ifdef I2C_HW_B_HDPVR
846 * The sleep bits aren't necessary on the HD PVR, and in fact, the
847 * last i2c_master_recv always fails with a -5, so for now, we're
848 * going to skip this whole mess and say we're done on the HD PVR
850 if (ir
->c_rx
.adapter
->id
== I2C_HW_B_HDPVR
)
855 * This bit NAKs until the device is ready, so we retry it
856 * sleeping a bit each time. This seems to be what the windows
857 * driver does, approximately.
860 for (i
= 0; i
< 20; ++i
) {
861 set_current_state(TASK_UNINTERRUPTIBLE
);
862 schedule_timeout((50 * HZ
+ 999) / 1000);
863 ret
= i2c_master_send(&ir
->c_tx
, buf
, 1);
866 dprintk("NAK expected: i2c_master_send "
867 "failed with %d (try %d)\n", ret
, i
+1);
870 zilog_error("IR TX chip never got ready: last i2c_master_send "
871 "failed with %d\n", ret
);
872 return ret
< 0 ? ret
: -EFAULT
;
875 /* Seems to be an 'ok' response */
876 i
= i2c_master_recv(&ir
->c_tx
, buf
, 1);
878 zilog_error("i2c_master_recv failed with %d\n", ret
);
881 if (buf
[0] != 0x80) {
882 zilog_error("unexpected IR TX response #2: %02x\n", buf
[0]);
887 /* Oh good, it worked */
888 dprintk("sent code %u, key %u\n", code
, key
);
893 * Write a code to the device. We take in a 32-bit number (an int) and then
894 * decode this to a codeset/key index. The key data is then decompressed and
895 * sent to the device. We have a spin lock as per i2c documentation to prevent
896 * multiple concurrent sends which would probably cause the device to explode.
898 static ssize_t
write(struct file
*filep
, const char *buf
, size_t n
,
901 struct IR
*ir
= (struct IR
*)filep
->private_data
;
905 if (ir
->c_tx
.addr
== 0)
908 /* Validate user parameters */
912 /* Lock i2c bus for the duration */
913 mutex_lock(&ir
->ir_lock
);
915 /* Send each keypress */
916 for (i
= 0; i
< n
;) {
920 if (copy_from_user(&command
, buf
+ i
, sizeof(command
))) {
921 mutex_unlock(&ir
->ir_lock
);
925 /* Send boot data first if required */
926 if (ir
->need_boot
== 1) {
927 ret
= send_boot_data(ir
);
934 ret
= send_code(ir
, (unsigned)command
>> 16,
935 (unsigned)command
& 0xFFFF);
936 if (ret
== -EPROTO
) {
937 mutex_unlock(&ir
->ir_lock
);
943 * Hmm, a failure. If we've had a few then give up, otherwise
947 /* Looks like the chip crashed, reset it */
948 zilog_error("sending to the IR transmitter chip "
949 "failed, trying reset\n");
952 zilog_error("unable to send to the IR chip "
953 "after 3 resets, giving up\n");
954 mutex_unlock(&ir
->ir_lock
);
957 set_current_state(TASK_UNINTERRUPTIBLE
);
958 schedule_timeout((100 * HZ
+ 999) / 1000);
965 /* Release i2c bus */
966 mutex_unlock(&ir
->ir_lock
);
972 /* copied from lirc_dev */
973 static unsigned int poll(struct file
*filep
, poll_table
*wait
)
975 struct IR
*ir
= (struct IR
*)filep
->private_data
;
978 dprintk("poll called\n");
979 if (ir
->c_rx
.addr
== 0)
982 mutex_lock(&ir
->buf_lock
);
984 poll_wait(filep
, &ir
->buf
.wait_poll
, wait
);
986 dprintk("poll result = %s\n",
987 lirc_buffer_empty(&ir
->buf
) ? "0" : "POLLIN|POLLRDNORM");
989 ret
= lirc_buffer_empty(&ir
->buf
) ? 0 : (POLLIN
|POLLRDNORM
);
991 mutex_unlock(&ir
->buf_lock
);
995 static long ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
997 struct IR
*ir
= (struct IR
*)filep
->private_data
;
999 unsigned long mode
, features
= 0;
1001 if (ir
->c_rx
.addr
!= 0)
1002 features
|= LIRC_CAN_REC_LIRCCODE
;
1003 if (ir
->c_tx
.addr
!= 0)
1004 features
|= LIRC_CAN_SEND_PULSE
;
1007 case LIRC_GET_LENGTH
:
1008 result
= put_user((unsigned long)13,
1009 (unsigned long *)arg
);
1011 case LIRC_GET_FEATURES
:
1012 result
= put_user(features
, (unsigned long *) arg
);
1014 case LIRC_GET_REC_MODE
:
1015 if (!(features
&LIRC_CAN_REC_MASK
))
1018 result
= put_user(LIRC_REC2MODE
1019 (features
&LIRC_CAN_REC_MASK
),
1020 (unsigned long *)arg
);
1022 case LIRC_SET_REC_MODE
:
1023 if (!(features
&LIRC_CAN_REC_MASK
))
1026 result
= get_user(mode
, (unsigned long *)arg
);
1027 if (!result
&& !(LIRC_MODE2REC(mode
) & features
))
1030 case LIRC_GET_SEND_MODE
:
1031 if (!(features
&LIRC_CAN_SEND_MASK
))
1034 result
= put_user(LIRC_MODE_PULSE
, (unsigned long *) arg
);
1036 case LIRC_SET_SEND_MODE
:
1037 if (!(features
&LIRC_CAN_SEND_MASK
))
1040 result
= get_user(mode
, (unsigned long *) arg
);
1041 if (!result
&& mode
!= LIRC_MODE_PULSE
)
1051 * Open the IR device. Get hold of our IR structure and
1052 * stash it in private_data for the file
1054 static int open(struct inode
*node
, struct file
*filep
)
1059 /* find our IR struct */
1060 unsigned minor
= MINOR(node
->i_rdev
);
1061 if (minor
>= MAX_IRCTL_DEVICES
) {
1062 dprintk("minor %d: open result = -ENODEV\n",
1066 ir
= ir_devices
[minor
];
1068 /* increment in use count */
1069 mutex_lock(&ir
->ir_lock
);
1071 ret
= set_use_inc(ir
);
1074 mutex_unlock(&ir
->ir_lock
);
1077 mutex_unlock(&ir
->ir_lock
);
1079 /* stash our IR struct */
1080 filep
->private_data
= ir
;
1085 /* Close the IR device */
1086 static int close(struct inode
*node
, struct file
*filep
)
1088 /* find our IR struct */
1089 struct IR
*ir
= (struct IR
*)filep
->private_data
;
1091 zilog_error("close: no private_data attached to the file!\n");
1095 /* decrement in use count */
1096 mutex_lock(&ir
->ir_lock
);
1099 mutex_unlock(&ir
->ir_lock
);
1104 static struct lirc_driver lirc_template
= {
1105 .name
= "lirc_zilog",
1106 .set_use_inc
= set_use_inc
,
1107 .set_use_dec
= set_use_dec
,
1108 .owner
= THIS_MODULE
1111 static int ir_remove(struct i2c_client
*client
);
1112 static int ir_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
);
1113 static int ir_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
);
1115 static const struct i2c_device_id ir_transceiver_id
[] = {
1116 /* Generic entry for any IR transceiver */
1118 /* IR device specific entries should be added here */
1119 { "ir_tx_z8f0811_haup", 0 },
1120 { "ir_rx_z8f0811_haup", 0 },
1124 static struct i2c_driver driver
= {
1126 .owner
= THIS_MODULE
,
1127 .name
= "Zilog/Hauppauge i2c IR",
1130 .remove
= ir_remove
,
1131 .command
= ir_command
,
1132 .id_table
= ir_transceiver_id
,
1135 static const struct file_operations lirc_fops
= {
1136 .owner
= THIS_MODULE
,
1141 .unlocked_ioctl
= ioctl
,
1146 static int ir_remove(struct i2c_client
*client
)
1148 struct IR
*ir
= i2c_get_clientdata(client
);
1150 mutex_lock(&ir
->ir_lock
);
1152 if (ir
->have_rx
|| ir
->have_tx
) {
1153 DECLARE_COMPLETION(tn
);
1154 DECLARE_COMPLETION(tn2
);
1156 /* end up polling thread */
1157 if (ir
->task
&& !IS_ERR(ir
->task
)) {
1159 ir
->t_notify2
= &tn2
;
1161 wake_up_process(ir
->task
);
1163 wait_for_completion(&tn
);
1164 ir
->t_notify
= NULL
;
1165 ir
->t_notify2
= NULL
;
1169 mutex_unlock(&ir
->ir_lock
);
1170 zilog_error("%s: detached from something we didn't "
1171 "attach to\n", __func__
);
1175 /* unregister lirc driver */
1176 if (ir
->l
.minor
>= 0 && ir
->l
.minor
< MAX_IRCTL_DEVICES
) {
1177 lirc_unregister_driver(ir
->l
.minor
);
1178 ir_devices
[ir
->l
.minor
] = NULL
;
1182 lirc_buffer_free(&ir
->buf
);
1183 mutex_unlock(&ir
->ir_lock
);
1189 static int ir_probe(struct i2c_client
*client
, const struct i2c_device_id
*id
)
1191 struct IR
*ir
= NULL
;
1192 struct i2c_adapter
*adap
= client
->adapter
;
1195 int have_rx
= 0, have_tx
= 0;
1197 dprintk("%s: adapter id=0x%x, client addr=0x%02x\n",
1198 __func__
, adap
->id
, client
->addr
);
1201 * The external IR receiver is at i2c address 0x71.
1202 * The IR transmitter is at 0x70.
1204 client
->addr
= 0x70;
1207 if (i2c_master_recv(client
, &buf
, 1) == 1)
1209 dprintk("probe 0x70 @ %s: %s\n",
1210 adap
->name
, have_tx
? "success" : "failed");
1214 client
->addr
= 0x71;
1215 if (i2c_master_recv(client
, &buf
, 1) == 1)
1217 dprintk("probe 0x71 @ %s: %s\n",
1218 adap
->name
, have_rx
? "success" : "failed");
1221 if (!(have_rx
|| have_tx
)) {
1222 zilog_error("%s: no devices found\n", adap
->name
);
1226 printk(KERN_INFO
"lirc_zilog: chip found with %s\n",
1227 have_rx
&& have_tx
? "RX and TX" :
1228 have_rx
? "RX only" : "TX only");
1230 ir
= kzalloc(sizeof(struct IR
), GFP_KERNEL
);
1235 ret
= lirc_buffer_init(&ir
->buf
, 2, BUFLEN
/ 2);
1239 mutex_init(&ir
->ir_lock
);
1240 mutex_init(&ir
->buf_lock
);
1243 memcpy(&ir
->l
, &lirc_template
, sizeof(struct lirc_driver
));
1246 /* I2C attach to device */
1247 i2c_set_clientdata(client
, ir
);
1249 /* initialise RX device */
1251 DECLARE_COMPLETION(tn
);
1252 memcpy(&ir
->c_rx
, client
, sizeof(struct i2c_client
));
1254 ir
->c_rx
.addr
= 0x71;
1255 strlcpy(ir
->c_rx
.name
, ZILOG_HAUPPAUGE_IR_RX_NAME
,
1258 /* try to fire up polling thread */
1260 ir
->task
= kthread_run(lirc_thread
, ir
, "lirc_zilog");
1261 if (IS_ERR(ir
->task
)) {
1262 ret
= PTR_ERR(ir
->task
);
1263 zilog_error("lirc_register_driver: cannot run "
1264 "poll thread %d\n", ret
);
1267 wait_for_completion(&tn
);
1268 ir
->t_notify
= NULL
;
1272 /* initialise TX device */
1274 memcpy(&ir
->c_tx
, client
, sizeof(struct i2c_client
));
1275 ir
->c_tx
.addr
= 0x70;
1276 strlcpy(ir
->c_tx
.name
, ZILOG_HAUPPAUGE_IR_TX_NAME
,
1281 /* set lirc_dev stuff */
1282 ir
->l
.code_length
= 13;
1283 ir
->l
.rbuf
= &ir
->buf
;
1284 ir
->l
.fops
= &lirc_fops
;
1286 ir
->l
.minor
= minor
;
1287 ir
->l
.dev
= &adap
->dev
;
1288 ir
->l
.sample_rate
= 0;
1290 /* register with lirc */
1291 ir
->l
.minor
= lirc_register_driver(&ir
->l
);
1292 if (ir
->l
.minor
< 0 || ir
->l
.minor
>= MAX_IRCTL_DEVICES
) {
1293 zilog_error("ir_attach: \"minor\" must be between 0 and %d "
1294 "(%d)!\n", MAX_IRCTL_DEVICES
-1, ir
->l
.minor
);
1299 /* store this for getting back in open() later on */
1300 ir_devices
[ir
->l
.minor
] = ir
;
1303 * if we have the tx device, load the 'firmware'. We do this
1304 * after registering with lirc as otherwise hotplug seems to take
1305 * 10s to create the lirc device.
1308 /* Special TX init */
1317 /* undo everything, hopefully... */
1319 ir_remove(&ir
->c_rx
);
1321 ir_remove(&ir
->c_tx
);
1325 zilog_error("no device found\n");
1329 zilog_error("memory allocation failure\n");
1334 static int ir_command(struct i2c_client
*client
, unsigned int cmd
, void *arg
)
1340 static int __init
zilog_init(void)
1344 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1346 mutex_init(&tx_data_lock
);
1348 request_module("firmware_class");
1350 ret
= i2c_add_driver(&driver
);
1352 zilog_error("initialization failed\n");
1354 zilog_notify("initialization complete\n");
1359 static void __exit
zilog_exit(void)
1361 i2c_del_driver(&driver
);
1364 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1367 module_init(zilog_init
);
1368 module_exit(zilog_exit
);
1370 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1371 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1372 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver");
1373 MODULE_LICENSE("GPL");
1374 /* for compat with old name, which isn't all that accurate anymore */
1375 MODULE_ALIAS("lirc_pvr150");
1377 module_param(minor
, int, 0444);
1378 MODULE_PARM_DESC(minor
, "Preferred minor device number");
1380 module_param(debug
, bool, 0644);
1381 MODULE_PARM_DESC(debug
, "Enable debugging messages");
1383 module_param(disable_rx
, bool, 0644);
1384 MODULE_PARM_DESC(disable_rx
, "Disable the IR receiver device");
1386 module_param(disable_tx
, bool, 0644);
1387 MODULE_PARM_DESC(disable_tx
, "Disable the IR transmitter device");