[media] lirc_zilog: Convert the instance open count to an atomic_t
[linux-2.6/btrfs-unstable.git] / drivers / staging / lirc / lirc_zilog.c
blobc857b99e5ace00d1cf3325d812fcfd52c2d3f634
1 /*
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>
48 #include <linux/fs.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>
66 struct IR_rx {
67 /* RX device */
68 struct i2c_client *c;
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;
77 /* RX read data */
78 unsigned char b[3];
79 bool hdpvr_data_fmt;
82 struct IR_tx {
83 /* TX device */
84 struct i2c_client *c;
86 /* TX additional actions needed */
87 int need_boot;
88 bool post_tx_ready_poll;
91 struct IR {
92 struct list_head list;
94 struct lirc_driver l;
96 struct mutex ir_lock;
97 atomic_t open_count;
99 struct i2c_adapter *adapter;
100 struct IR_rx *rx;
101 struct IR_tx *tx;
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 {
113 /* Boot block */
114 unsigned char *boot_data;
116 /* Start of binary data block */
117 unsigned char *datap;
119 /* End of binary data block */
120 unsigned char *endp;
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, \
136 ## args)
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...) \
146 do { \
147 if (debug) \
148 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
149 ## args); \
150 } while (0)
152 static int add_to_buf(struct IR *ir)
154 __u16 code;
155 unsigned char codes[2];
156 unsigned char keybuf[6];
157 int got_data = 0;
158 int ret;
159 int failures = 0;
160 unsigned char sendbuf[1] = { 0 };
161 struct IR_rx *rx = ir->rx;
163 if (rx == NULL)
164 return -ENXIO;
166 if (lirc_buffer_full(&rx->buf)) {
167 dprintk("buffer overflow\n");
168 return -EOVERFLOW;
172 * service the device as long as it is returning
173 * data and we have space
175 do {
176 if (kthread_should_stop())
177 return -ENODATA;
180 * Lock i2c bus for the duration. RX/TX chips interfere so
181 * this is worth it
183 mutex_lock(&ir->ir_lock);
185 if (kthread_should_stop()) {
186 mutex_unlock(&ir->ir_lock);
187 return -ENODATA;
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);
195 if (ret != 1) {
196 zilog_error("i2c_master_send failed with %d\n", ret);
197 if (failures >= 3) {
198 mutex_unlock(&ir->ir_lock);
199 zilog_error("unable to read from the IR chip "
200 "after 3 resets, giving up\n");
201 return ret;
204 /* Looks like the chip crashed, reset it */
205 zilog_error("polling the IR receiver chip failed, "
206 "trying reset\n");
208 set_current_state(TASK_UNINTERRUPTIBLE);
209 if (kthread_should_stop()) {
210 mutex_unlock(&ir->ir_lock);
211 return -ENODATA;
213 schedule_timeout((100 * HZ + 999) / 1000);
214 if (ir->tx != NULL)
215 ir->tx->need_boot = 1;
217 ++failures;
218 mutex_unlock(&ir->ir_lock);
219 continue;
222 if (kthread_should_stop()) {
223 mutex_unlock(&ir->ir_lock);
224 return -ENODATA;
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);
231 } else {
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]);
238 /* key pressed ? */
239 if (rx->hdpvr_data_fmt) {
240 if (got_data && (keybuf[0] == 0x80))
241 return 0;
242 else if (got_data && (keybuf[0] == 0x00))
243 return -ENODATA;
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;
253 /* return it */
254 lirc_buffer_write(&rx->buf, codes);
255 ++got_data;
256 } while (!lirc_buffer_full(&rx->buf));
258 return 0;
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)
273 struct IR *ir = 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);
284 continue;
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())
299 break;
300 if (!add_to_buf(ir))
301 wake_up_interruptible(&rx->buf.wait_poll);
304 dprintk("poll thread ended\n");
305 return 0;
308 static int set_use_inc(void *data)
310 return 0;
313 static void set_use_dec(void *data)
315 return;
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)
323 return 0;
324 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
325 ((*data)[2] << 8) | (*data)[3];
326 *data += 4;
327 return 1;
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)
335 return 0;
336 *val = *((*data)++);
337 return 1;
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)
345 return 0;
346 *data += distance;
347 return 1;
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))
364 goto corrupt;
366 if (i == codeset)
367 break;
368 else if (codeset > i) {
369 base = pos + 1;
370 --lim;
373 /* Not found? */
374 if (!lim)
375 return -EPROTO;
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)
385 goto corrupt;
387 /* Save diffs & skip */
388 diffs = data;
389 if (!skip(&data, endp, ndiffs))
390 goto corrupt;
392 /* Read the id of the first key */
393 if (!read_uint8(&data, endp, &id))
394 goto corrupt;
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]))
400 goto corrupt;
401 } else {
402 buf[i] = (unsigned char)tx_data->fixed[i];
406 /* Early out key found/not found */
407 if (key == id)
408 return 0;
409 if (keys == 1)
410 return -EPROTO;
412 /* Sanity check */
413 key_block = data;
414 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
415 goto corrupt;
417 /* Binary search for the key */
418 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
419 /* Seek to block */
420 unsigned char *key_data;
421 pos = base + (lim >> 1);
422 key_data = key_block + (ndiffs + 1) * pos;
424 if (*key_data == key) {
425 /* skip key id */
426 ++key_data;
428 /* found, so unpack the diffs */
429 for (i = 0; i < ndiffs; ++i) {
430 unsigned char val;
431 if (!read_uint8(&key_data, endp, &val) ||
432 diffs[i] >= TX_BLOCK_SIZE)
433 goto corrupt;
434 buf[diffs[i]] = val;
437 return 0;
438 } else if (key > *key_data) {
439 base = pos + 1;
440 --lim;
443 /* Key not found */
444 return -EPROTO;
446 corrupt:
447 zilog_error("firmware is corrupt\n");
448 return -EFAULT;
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)
454 int i, j, ret;
455 unsigned char buf[5];
457 for (i = 0; i < TX_BLOCK_SIZE;) {
458 int tosend = TX_BLOCK_SIZE - i;
459 if (tosend > 4)
460 tosend = 4;
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;
471 i += tosend;
473 return 0;
476 /* send boot data to the IR TX device */
477 static int send_boot_data(struct IR_tx *tx)
479 int ret, i;
480 unsigned char buf[4];
482 /* send the boot block */
483 ret = send_data_block(tx, tx_data->boot_data);
484 if (ret != 0)
485 return ret;
487 /* Hit the go button to activate the new boot data */
488 buf[0] = 0x00;
489 buf[1] = 0x20;
490 ret = i2c_master_send(tx->c, buf, 2);
491 if (ret != 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);
503 if (ret == 1)
504 break;
505 udelay(100);
508 if (ret != 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);
515 if (ret != 4) {
516 zilog_error("i2c_master_recv failed with %d\n", ret);
517 return 0;
519 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
520 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
521 return 0;
523 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
524 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
526 return 0;
529 /* unload "firmware", lock held */
530 static void fw_unload_locked(void)
532 if (tx_data) {
533 if (tx_data->code_sets)
534 vfree(tx_data->code_sets);
536 if (tx_data->datap)
537 vfree(tx_data->datap);
539 vfree(tx_data);
540 tx_data = NULL;
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);
549 fw_unload_locked();
550 mutex_unlock(&tx_data_lock);
553 /* load "firmware" for the IR TX device */
554 static int fw_load(struct IR_tx *tx)
556 int ret;
557 unsigned int i;
558 unsigned char *data, version, num_global_fixed;
559 const struct firmware *fw_entry;
561 /* Already loaded? */
562 mutex_lock(&tx_data_lock);
563 if (tx_data) {
564 ret = 0;
565 goto out;
568 /* Request codeset data file */
569 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
570 if (ret != 0) {
571 zilog_error("firmware haup-ir-blaster.bin not available "
572 "(%d)\n", ret);
573 ret = ret < 0 ? ret : -EFAULT;
574 goto out;
576 dprintk("firmware of size %zu loaded\n", fw_entry->size);
578 /* Parse the file */
579 tx_data = vmalloc(sizeof(*tx_data));
580 if (tx_data == NULL) {
581 zilog_error("out of memory\n");
582 release_firmware(fw_entry);
583 ret = -ENOMEM;
584 goto out;
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);
593 vfree(tx_data);
594 ret = -ENOMEM;
595 goto out;
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;
601 /* Check version */
602 data = tx_data->datap;
603 if (!read_uint8(&data, tx_data->endp, &version))
604 goto corrupt;
605 if (version != 1) {
606 zilog_error("unsupported code set file version (%u, expected"
607 "1) -- please upgrade to a newer driver",
608 version);
609 fw_unload_locked();
610 ret = -EFAULT;
611 goto out;
614 /* Save boot block for later */
615 tx_data->boot_data = data;
616 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
617 goto corrupt;
619 if (!read_uint32(&data, tx_data->endp,
620 &tx_data->num_code_sets))
621 goto corrupt;
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) {
628 fw_unload_locked();
629 ret = -ENOMEM;
630 goto out;
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)
639 goto corrupt;
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)
645 goto corrupt;
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) {
651 unsigned int id;
652 unsigned char keys;
653 unsigned char ndiffs;
655 /* Save the codeset position */
656 tx_data->code_sets[i] = data;
658 /* Read header */
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)
663 goto corrupt;
665 /* skip diff positions */
666 if (!skip(&data, tx_data->endp, ndiffs))
667 goto corrupt;
670 * After the diffs we have the first key id + data -
671 * global fixed
673 if (!skip(&data, tx_data->endp,
674 1 + TX_BLOCK_SIZE - num_global_fixed))
675 goto corrupt;
677 /* Then we have keys-1 blocks of key id+diffs */
678 if (!skip(&data, tx_data->endp,
679 (ndiffs + 1) * (keys - 1)))
680 goto corrupt;
682 ret = 0;
683 goto out;
685 corrupt:
686 zilog_error("firmware is corrupt\n");
687 fw_unload_locked();
688 ret = -EFAULT;
690 out:
691 mutex_unlock(&tx_data_lock);
692 return ret;
695 /* initialise the IR TX device */
696 static int tx_init(struct IR_tx *tx)
698 int ret;
700 /* Load 'firmware' */
701 ret = fw_load(tx);
702 if (ret != 0)
703 return ret;
705 /* Send boot block */
706 ret = send_boot_data(tx);
707 if (ret != 0)
708 return ret;
709 tx->need_boot = 0;
711 /* Looks good */
712 return 0;
715 /* do nothing stub to make LIRC happy */
716 static loff_t lseek(struct file *filep, loff_t offset, int orig)
718 return -ESPIPE;
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");
730 if (rx == NULL)
731 return -ENODEV;
733 if (mutex_lock_interruptible(&rx->buf_lock))
734 return -ERESTARTSYS;
736 if (n % rx->buf.chunk_size) {
737 dprintk("read result = -EINVAL\n");
738 mutex_unlock(&rx->buf_lock);
739 return -EINVAL;
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
760 * -ERESTARTSYS
762 if (written)
763 break;
764 if (filep->f_flags & O_NONBLOCK) {
765 ret = -EWOULDBLOCK;
766 break;
768 if (signal_pending(current)) {
769 ret = -ERESTARTSYS;
770 break;
772 schedule();
773 set_current_state(TASK_INTERRUPTIBLE);
774 } else {
775 unsigned char buf[rx->buf.chunk_size];
776 lirc_buffer_read(&rx->buf, buf);
777 ret = copy_to_user((void *)outbuf+written, buf,
778 rx->buf.chunk_size);
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];
798 int i, ret;
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);
806 return ret;
807 } else if (ret != 0)
808 return ret;
810 /* Send the data block */
811 ret = send_data_block(tx, data_block);
812 if (ret != 0)
813 return ret;
815 /* Send data block length? */
816 buf[0] = 0x00;
817 buf[1] = 0x40;
818 ret = i2c_master_send(tx->c, buf, 2);
819 if (ret != 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);
827 if (ret == 1)
828 break;
829 udelay(100);
832 if (ret != 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);
839 if (ret != 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",
845 buf[0]);
846 return -EFAULT;
849 /* Send prepare command? */
850 buf[0] = 0x00;
851 buf[1] = 0x80;
852 ret = i2c_master_send(tx->c, buf, 2);
853 if (ret != 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);
865 return 0;
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.
872 * Try for up to 1s.
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);
878 if (ret == 1)
879 break;
880 dprintk("NAK expected: i2c_master_send "
881 "failed with %d (try %d)\n", ret, i+1);
883 if (ret != 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);
891 if (i != 1) {
892 zilog_error("i2c_master_recv failed with %d\n", ret);
893 return -EFAULT;
895 if (buf[0] != 0x80) {
896 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
897 return -EFAULT;
900 /* Oh good, it worked */
901 dprintk("sent code %u, key %u\n", code, key);
902 return 0;
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,
912 loff_t *ppos)
914 struct IR *ir = filep->private_data;
915 struct IR_tx *tx = ir->tx;
916 size_t i;
917 int failures = 0;
919 if (tx == NULL)
920 return -ENODEV;
922 /* Validate user parameters */
923 if (n % sizeof(int))
924 return -EINVAL;
926 /* Lock i2c bus for the duration */
927 mutex_lock(&ir->ir_lock);
929 /* Send each keypress */
930 for (i = 0; i < n;) {
931 int ret = 0;
932 int command;
934 if (copy_from_user(&command, buf + i, sizeof(command))) {
935 mutex_unlock(&ir->ir_lock);
936 return -EFAULT;
939 /* Send boot data first if required */
940 if (tx->need_boot == 1) {
941 ret = send_boot_data(tx);
942 if (ret == 0)
943 tx->need_boot = 0;
946 /* Send the code */
947 if (ret == 0) {
948 ret = send_code(tx, (unsigned)command >> 16,
949 (unsigned)command & 0xFFFF);
950 if (ret == -EPROTO) {
951 mutex_unlock(&ir->ir_lock);
952 return ret;
957 * Hmm, a failure. If we've had a few then give up, otherwise
958 * try a reset
960 if (ret != 0) {
961 /* Looks like the chip crashed, reset it */
962 zilog_error("sending to the IR transmitter chip "
963 "failed, trying reset\n");
965 if (failures >= 3) {
966 zilog_error("unable to send to the IR chip "
967 "after 3 resets, giving up\n");
968 mutex_unlock(&ir->ir_lock);
969 return ret;
971 set_current_state(TASK_UNINTERRUPTIBLE);
972 schedule_timeout((100 * HZ + 999) / 1000);
973 tx->need_boot = 1;
974 ++failures;
975 } else
976 i += sizeof(int);
979 /* Release i2c bus */
980 mutex_unlock(&ir->ir_lock);
982 /* All looks good */
983 return n;
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;
991 unsigned int ret;
993 dprintk("poll called\n");
994 if (rx == NULL)
995 return -ENODEV;
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);
1007 return ret;
1010 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1012 struct IR *ir = filep->private_data;
1013 int result;
1014 unsigned long mode, features = 0;
1016 if (ir->rx != NULL)
1017 features |= LIRC_CAN_REC_LIRCCODE;
1018 if (ir->tx != NULL)
1019 features |= LIRC_CAN_SEND_PULSE;
1021 switch (cmd) {
1022 case LIRC_GET_LENGTH:
1023 result = put_user((unsigned long)13,
1024 (unsigned long *)arg);
1025 break;
1026 case LIRC_GET_FEATURES:
1027 result = put_user(features, (unsigned long *) arg);
1028 break;
1029 case LIRC_GET_REC_MODE:
1030 if (!(features&LIRC_CAN_REC_MASK))
1031 return -ENOSYS;
1033 result = put_user(LIRC_REC2MODE
1034 (features&LIRC_CAN_REC_MASK),
1035 (unsigned long *)arg);
1036 break;
1037 case LIRC_SET_REC_MODE:
1038 if (!(features&LIRC_CAN_REC_MASK))
1039 return -ENOSYS;
1041 result = get_user(mode, (unsigned long *)arg);
1042 if (!result && !(LIRC_MODE2REC(mode) & features))
1043 result = -EINVAL;
1044 break;
1045 case LIRC_GET_SEND_MODE:
1046 if (!(features&LIRC_CAN_SEND_MASK))
1047 return -ENOSYS;
1049 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1050 break;
1051 case LIRC_SET_SEND_MODE:
1052 if (!(features&LIRC_CAN_SEND_MASK))
1053 return -ENOSYS;
1055 result = get_user(mode, (unsigned long *) arg);
1056 if (!result && mode != LIRC_MODE_PULSE)
1057 return -EINVAL;
1058 break;
1059 default:
1060 return -EINVAL;
1062 return result;
1065 /* ir_devices_lock must be held */
1066 static struct IR *find_ir_device_by_minor(unsigned int minor)
1068 struct IR *ir;
1070 if (list_empty(&ir_devices_list))
1071 return NULL;
1073 list_for_each_entry(ir, &ir_devices_list, list)
1074 if (ir->l.minor == minor)
1075 return ir;
1077 return NULL;
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)
1086 struct IR *ir;
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);
1094 if (ir == NULL)
1095 return -ENODEV;
1097 atomic_inc(&ir->open_count);
1099 /* stash our IR struct */
1100 filep->private_data = ir;
1102 return 0;
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;
1110 if (ir == NULL) {
1111 zilog_error("close: no private_data attached to the file!\n");
1112 return -ENODEV;
1115 atomic_dec(&ir->open_count);
1117 return 0;
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 = {
1142 .driver = {
1143 .owner = THIS_MODULE,
1144 .name = "Zilog/Hauppauge i2c IR",
1146 .probe = ir_probe,
1147 .remove = ir_remove,
1148 .id_table = ir_transceiver_id,
1151 static const struct file_operations lirc_fops = {
1152 .owner = THIS_MODULE,
1153 .llseek = lseek,
1154 .read = read,
1155 .write = write,
1156 .poll = poll,
1157 .unlocked_ioctl = ioctl,
1158 #ifdef CONFIG_COMPAT
1159 .compat_ioctl = ioctl,
1160 #endif
1161 .open = open,
1162 .release = close
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);
1170 rx->task = NULL;
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);
1178 return 0;
1181 /* ir_devices_lock must be held */
1182 static void del_ir_device(struct IR *ir)
1184 struct IR *p;
1186 if (list_empty(&ir_devices_list))
1187 return;
1189 list_for_each_entry(p, &ir_devices_list, list)
1190 if (p == ir) {
1191 list_del(&p->list);
1192 break;
1196 static int ir_remove(struct i2c_client *client)
1198 struct IR *ir = i2c_get_clientdata(client);
1200 mutex_lock(&ir_devices_lock);
1202 if (ir == NULL) {
1203 /* We destroyed everything when the first client came through */
1204 mutex_unlock(&ir_devices_lock);
1205 return 0;
1208 /* Good-bye LIRC */
1209 lirc_unregister_driver(ir->l.minor);
1211 /* Good-bye Rx */
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);
1217 kfree(ir->rx);
1220 /* Good-bye Tx */
1221 if (ir->tx != NULL) {
1222 i2c_set_clientdata(ir->tx->c, NULL);
1223 kfree(ir->tx);
1226 /* Good-bye IR */
1227 del_ir_device(ir);
1228 kfree(ir);
1230 mutex_unlock(&ir_devices_lock);
1231 return 0;
1235 /* ir_devices_lock must be held */
1236 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1238 struct IR *ir;
1240 if (list_empty(&ir_devices_list))
1241 return NULL;
1243 list_for_each_entry(ir, &ir_devices_list, list)
1244 if (ir->adapter == adapter)
1245 return ir;
1247 return NULL;
1250 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1252 struct IR *ir;
1253 struct i2c_adapter *adap = client->adapter;
1254 int ret;
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)
1266 tx_probe = true;
1267 else if (tx_only) /* module option */
1268 return -ENXIO;
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);
1277 if (ir == NULL) {
1278 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1279 if (ir == NULL) {
1280 ret = -ENOMEM;
1281 goto out_no_ir;
1283 /* store for use in ir_probe() again, and open() later on */
1284 INIT_LIST_HEAD(&ir->list);
1285 ret = add_ir_device(ir);
1286 if (ret)
1287 goto out_free_ir;
1289 ir->adapter = adap;
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;
1297 ir->l.rbuf = NULL;
1298 ir->l.fops = &lirc_fops;
1299 ir->l.data = ir;
1300 ir->l.dev = &adap->dev;
1301 ir->l.sample_rate = 0;
1304 if (tx_probe) {
1305 /* Set up a struct IR_tx instance */
1306 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1307 if (ir->tx == NULL) {
1308 ret = -ENOMEM;
1309 goto out_free_xx;
1312 ir->tx->c = client;
1313 ir->tx->need_boot = 1;
1314 ir->tx->post_tx_ready_poll =
1315 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1316 } else {
1317 /* Set up a struct IR_rx instance */
1318 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1319 if (ir->rx == NULL) {
1320 ret = -ENOMEM;
1321 goto out_free_xx;
1324 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1325 if (ret)
1326 goto out_free_xx;
1328 mutex_init(&ir->rx->buf_lock);
1329 ir->rx->c = client;
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");
1345 goto out_ok;
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"
1356 "\n", __func__);
1357 goto out_free_xx;
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);
1366 ret = -EBADRQC;
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);
1378 if (ret != 0)
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);
1384 out_ok:
1385 mutex_unlock(&ir_devices_lock);
1386 return 0;
1388 out_unregister:
1389 lirc_unregister_driver(ir->l.minor);
1390 out_free_thread:
1391 destroy_rx_kthread(ir->rx);
1392 out_free_xx:
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);
1398 kfree(ir->rx);
1400 if (ir->tx != NULL) {
1401 if (ir->tx->c != NULL)
1402 i2c_set_clientdata(ir->tx->c, NULL);
1403 kfree(ir->tx);
1405 out_free_ir:
1406 del_ir_device(ir);
1407 kfree(ir);
1408 out_no_ir:
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,
1411 ret);
1412 mutex_unlock(&ir_devices_lock);
1413 return ret;
1416 static int __init zilog_init(void)
1418 int ret;
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);
1427 if (ret)
1428 zilog_error("initialization failed\n");
1429 else
1430 zilog_notify("initialization complete\n");
1432 return ret;
1435 static void __exit zilog_exit(void)
1437 i2c_del_driver(&driver);
1438 /* if loaded */
1439 fw_unload();
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, "
1449 "Andy Walls");
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");