ALSA: oxygen: do not show chip revision in card longname
[firewire-audio.git] / drivers / staging / lirc / lirc_zilog.c
blobf0076eb025f1a0e9d412080caab87f627dda4970
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 * 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>
45 #include <linux/fs.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>
63 struct IR {
64 struct lirc_driver l;
66 /* Device info */
67 struct mutex ir_lock;
68 int open;
70 /* RX device */
71 struct i2c_client c_rx;
72 int have_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;
81 int shutdown;
82 struct task_struct *task;
84 /* RX read data */
85 unsigned char b[3];
87 /* TX device */
88 struct i2c_client c_tx;
89 int need_boot;
90 int have_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 {
101 /* Boot block */
102 unsigned char *boot_data;
104 /* Start of binary data block */
105 unsigned char *datap;
107 /* End of binary data block */
108 unsigned char *endp;
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, \
124 ## args)
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...) \
137 do { \
138 if (debug) \
139 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
140 ## args); \
141 } while (0)
143 static int add_to_buf(struct IR *ir)
145 __u16 code;
146 unsigned char codes[2];
147 unsigned char keybuf[6];
148 int got_data = 0;
149 int ret;
150 int failures = 0;
151 unsigned char sendbuf[1] = { 0 };
153 if (lirc_buffer_full(&ir->buf)) {
154 dprintk("buffer overflow\n");
155 return -EOVERFLOW;
159 * service the device as long as it is returning
160 * data and we have space
162 do {
164 * Lock i2c bus for the duration. RX/TX chips interfere so
165 * this is worth it
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);
174 if (ret != 1) {
175 zilog_error("i2c_master_send failed with %d\n", ret);
176 if (failures >= 3) {
177 mutex_unlock(&ir->ir_lock);
178 zilog_error("unable to read from the IR chip "
179 "after 3 resets, giving up\n");
180 return ret;
183 /* Looks like the chip crashed, reset it */
184 zilog_error("polling the IR receiver chip failed, "
185 "trying reset\n");
187 set_current_state(TASK_UNINTERRUPTIBLE);
188 schedule_timeout((100 * HZ + 999) / 1000);
189 ir->need_boot = 1;
191 ++failures;
192 mutex_unlock(&ir->ir_lock);
193 continue;
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);
201 } else {
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]);
208 /* key pressed ? */
209 #ifdef I2C_HW_B_HDPVR
210 if (ir->c_rx.adapter->id == I2C_HW_B_HDPVR) {
211 if (got_data && (keybuf[0] == 0x80))
212 return 0;
213 else if (got_data && (keybuf[0] == 0x00))
214 return -ENODATA;
215 } else if ((ir->b[0] & 0x80) == 0)
216 #else
217 if ((ir->b[0] & 0x80) == 0)
218 #endif
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;
227 /* return it */
228 lirc_buffer_write(&ir->buf, codes);
229 ++got_data;
230 } while (!lirc_buffer_full(&ir->buf));
232 return 0;
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)
247 struct IR *ir = arg;
249 if (ir->t_notify != NULL)
250 complete(ir->t_notify);
252 dprintk("poll thread started\n");
254 do {
255 if (ir->open) {
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
268 * lost keypresses.
270 schedule_timeout((260 * HZ) / 1000);
271 if (ir->shutdown)
272 break;
273 if (!add_to_buf(ir))
274 wake_up_interruptible(&ir->buf.wait_poll);
275 } else {
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);
285 ir->task = NULL;
286 if (ir->t_notify != NULL)
287 complete(ir->t_notify);
289 dprintk("poll thread ended\n");
290 return 0;
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)
298 return -ENODEV;
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
305 if (ir->c_rx.addr)
306 i2c_use_client(&ir->c_rx);
307 if (ir->c_tx.addr)
308 i2c_use_client(&ir->c_tx);
310 return 0;
313 static void set_use_dec(void *data)
315 struct IR *ir = data;
317 if (ir->c_rx.addr)
318 i2c_release_client(&ir->c_rx);
319 if (ir->c_tx.addr)
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)
330 return 0;
331 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
332 ((*data)[2] << 8) | (*data)[3];
333 *data += 4;
334 return 1;
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)
342 return 0;
343 *val = *((*data)++);
344 return 1;
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)
352 return 0;
353 *data += distance;
354 return 1;
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))
371 goto corrupt;
373 if (i == codeset)
374 break;
375 else if (codeset > i) {
376 base = pos + 1;
377 --lim;
380 /* Not found? */
381 if (!lim)
382 return -EPROTO;
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)
392 goto corrupt;
394 /* Save diffs & skip */
395 diffs = data;
396 if (!skip(&data, endp, ndiffs))
397 goto corrupt;
399 /* Read the id of the first key */
400 if (!read_uint8(&data, endp, &id))
401 goto corrupt;
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]))
407 goto corrupt;
408 } else {
409 buf[i] = (unsigned char)tx_data->fixed[i];
413 /* Early out key found/not found */
414 if (key == id)
415 return 0;
416 if (keys == 1)
417 return -EPROTO;
419 /* Sanity check */
420 key_block = data;
421 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
422 goto corrupt;
424 /* Binary search for the key */
425 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
426 /* Seek to block */
427 unsigned char *key_data;
428 pos = base + (lim >> 1);
429 key_data = key_block + (ndiffs + 1) * pos;
431 if (*key_data == key) {
432 /* skip key id */
433 ++key_data;
435 /* found, so unpack the diffs */
436 for (i = 0; i < ndiffs; ++i) {
437 unsigned char val;
438 if (!read_uint8(&key_data, endp, &val) ||
439 diffs[i] >= TX_BLOCK_SIZE)
440 goto corrupt;
441 buf[diffs[i]] = val;
444 return 0;
445 } else if (key > *key_data) {
446 base = pos + 1;
447 --lim;
450 /* Key not found */
451 return -EPROTO;
453 corrupt:
454 zilog_error("firmware is corrupt\n");
455 return -EFAULT;
458 /* send a block of data to the IR TX device */
459 static int send_data_block(struct IR *ir, unsigned char *data_block)
461 int i, j, ret;
462 unsigned char buf[5];
464 for (i = 0; i < TX_BLOCK_SIZE;) {
465 int tosend = TX_BLOCK_SIZE - i;
466 if (tosend > 4)
467 tosend = 4;
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;
478 i += tosend;
480 return 0;
483 /* send boot data to the IR TX device */
484 static int send_boot_data(struct IR *ir)
486 int ret;
487 unsigned char buf[4];
489 /* send the boot block */
490 ret = send_data_block(ir, tx_data->boot_data);
491 if (ret != 0)
492 return ret;
494 /* kick it off? */
495 buf[0] = 0x00;
496 buf[1] = 0x20;
497 ret = i2c_master_send(&ir->c_tx, buf, 2);
498 if (ret != 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);
503 if (ret != 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);
510 if (ret != 4) {
511 zilog_error("i2c_master_recv failed with %d\n", ret);
512 return 0;
514 if (buf[0] != 0x80) {
515 zilog_error("unexpected IR TX response: %02x\n", buf[0]);
516 return 0;
518 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
519 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
521 return 0;
524 /* unload "firmware", lock held */
525 static void fw_unload_locked(void)
527 if (tx_data) {
528 if (tx_data->code_sets)
529 vfree(tx_data->code_sets);
531 if (tx_data->datap)
532 vfree(tx_data->datap);
534 vfree(tx_data);
535 tx_data = NULL;
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);
544 fw_unload_locked();
545 mutex_unlock(&tx_data_lock);
548 /* load "firmware" for the IR TX device */
549 static int fw_load(struct IR *ir)
551 int ret;
552 unsigned int i;
553 unsigned char *data, version, num_global_fixed;
554 const struct firmware *fw_entry;
556 /* Already loaded? */
557 mutex_lock(&tx_data_lock);
558 if (tx_data) {
559 ret = 0;
560 goto out;
563 /* Request codeset data file */
564 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &ir->c_tx.dev);
565 if (ret != 0) {
566 zilog_error("firmware haup-ir-blaster.bin not available "
567 "(%d)\n", ret);
568 ret = ret < 0 ? ret : -EFAULT;
569 goto out;
571 dprintk("firmware of size %zu loaded\n", fw_entry->size);
573 /* Parse the file */
574 tx_data = vmalloc(sizeof(*tx_data));
575 if (tx_data == NULL) {
576 zilog_error("out of memory\n");
577 release_firmware(fw_entry);
578 ret = -ENOMEM;
579 goto out;
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);
588 vfree(tx_data);
589 ret = -ENOMEM;
590 goto out;
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;
596 /* Check version */
597 data = tx_data->datap;
598 if (!read_uint8(&data, tx_data->endp, &version))
599 goto corrupt;
600 if (version != 1) {
601 zilog_error("unsupported code set file version (%u, expected"
602 "1) -- please upgrade to a newer driver",
603 version);
604 fw_unload_locked();
605 ret = -EFAULT;
606 goto out;
609 /* Save boot block for later */
610 tx_data->boot_data = data;
611 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
612 goto corrupt;
614 if (!read_uint32(&data, tx_data->endp,
615 &tx_data->num_code_sets))
616 goto corrupt;
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) {
623 fw_unload_locked();
624 ret = -ENOMEM;
625 goto out;
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)
634 goto corrupt;
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)
640 goto corrupt;
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) {
646 unsigned int id;
647 unsigned char keys;
648 unsigned char ndiffs;
650 /* Save the codeset position */
651 tx_data->code_sets[i] = data;
653 /* Read header */
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)
658 goto corrupt;
660 /* skip diff positions */
661 if (!skip(&data, tx_data->endp, ndiffs))
662 goto corrupt;
665 * After the diffs we have the first key id + data -
666 * global fixed
668 if (!skip(&data, tx_data->endp,
669 1 + TX_BLOCK_SIZE - num_global_fixed))
670 goto corrupt;
672 /* Then we have keys-1 blocks of key id+diffs */
673 if (!skip(&data, tx_data->endp,
674 (ndiffs + 1) * (keys - 1)))
675 goto corrupt;
677 ret = 0;
678 goto out;
680 corrupt:
681 zilog_error("firmware is corrupt\n");
682 fw_unload_locked();
683 ret = -EFAULT;
685 out:
686 mutex_unlock(&tx_data_lock);
687 return ret;
690 /* initialise the IR TX device */
691 static int tx_init(struct IR *ir)
693 int ret;
695 /* Load 'firmware' */
696 ret = fw_load(ir);
697 if (ret != 0)
698 return ret;
700 /* Send boot block */
701 ret = send_boot_data(ir);
702 if (ret != 0)
703 return ret;
704 ir->need_boot = 0;
706 /* Looks good */
707 return 0;
710 /* do nothing stub to make LIRC happy */
711 static loff_t lseek(struct file *filep, loff_t offset, int orig)
713 return -ESPIPE;
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 = 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)
726 return -ENODEV;
728 if (mutex_lock_interruptible(&ir->buf_lock))
729 return -ERESTARTSYS;
731 if (n % ir->buf.chunk_size) {
732 dprintk("read result = -EINVAL\n");
733 mutex_unlock(&ir->buf_lock);
734 return -EINVAL;
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
755 * -ERESTARTSYS
757 if (written)
758 break;
759 if (filep->f_flags & O_NONBLOCK) {
760 ret = -EWOULDBLOCK;
761 break;
763 if (signal_pending(current)) {
764 ret = -ERESTARTSYS;
765 break;
767 schedule();
768 set_current_state(TASK_INTERRUPTIBLE);
769 } else {
770 lirc_buffer_read(&ir->buf, buf);
771 ret = copy_to_user((void *)outbuf+written, buf,
772 ir->buf.chunk_size);
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];
792 int i, ret;
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);
800 return ret;
801 } else if (ret != 0)
802 return ret;
804 /* Send the data block */
805 ret = send_data_block(ir, data_block);
806 if (ret != 0)
807 return ret;
809 /* Send data block length? */
810 buf[0] = 0x00;
811 buf[1] = 0x40;
812 ret = i2c_master_send(&ir->c_tx, buf, 2);
813 if (ret != 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);
818 if (ret != 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);
825 if (ret != 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",
831 buf[0]);
832 return -EFAULT;
835 /* Send prepare command? */
836 buf[0] = 0x00;
837 buf[1] = 0x80;
838 ret = i2c_master_send(&ir->c_tx, buf, 2);
839 if (ret != 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)
851 goto done;
852 #endif
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.
858 * Try for up to 1s.
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);
864 if (ret == 1)
865 break;
866 dprintk("NAK expected: i2c_master_send "
867 "failed with %d (try %d)\n", ret, i+1);
869 if (ret != 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);
877 if (i != 1) {
878 zilog_error("i2c_master_recv failed with %d\n", ret);
879 return -EFAULT;
881 if (buf[0] != 0x80) {
882 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
883 return -EFAULT;
886 done:
887 /* Oh good, it worked */
888 dprintk("sent code %u, key %u\n", code, key);
889 return 0;
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,
899 loff_t *ppos)
901 struct IR *ir = filep->private_data;
902 size_t i;
903 int failures = 0;
905 if (ir->c_tx.addr == 0)
906 return -ENODEV;
908 /* Validate user parameters */
909 if (n % sizeof(int))
910 return -EINVAL;
912 /* Lock i2c bus for the duration */
913 mutex_lock(&ir->ir_lock);
915 /* Send each keypress */
916 for (i = 0; i < n;) {
917 int ret = 0;
918 int command;
920 if (copy_from_user(&command, buf + i, sizeof(command))) {
921 mutex_unlock(&ir->ir_lock);
922 return -EFAULT;
925 /* Send boot data first if required */
926 if (ir->need_boot == 1) {
927 ret = send_boot_data(ir);
928 if (ret == 0)
929 ir->need_boot = 0;
932 /* Send the code */
933 if (ret == 0) {
934 ret = send_code(ir, (unsigned)command >> 16,
935 (unsigned)command & 0xFFFF);
936 if (ret == -EPROTO) {
937 mutex_unlock(&ir->ir_lock);
938 return ret;
943 * Hmm, a failure. If we've had a few then give up, otherwise
944 * try a reset
946 if (ret != 0) {
947 /* Looks like the chip crashed, reset it */
948 zilog_error("sending to the IR transmitter chip "
949 "failed, trying reset\n");
951 if (failures >= 3) {
952 zilog_error("unable to send to the IR chip "
953 "after 3 resets, giving up\n");
954 mutex_unlock(&ir->ir_lock);
955 return ret;
957 set_current_state(TASK_UNINTERRUPTIBLE);
958 schedule_timeout((100 * HZ + 999) / 1000);
959 ir->need_boot = 1;
960 ++failures;
961 } else
962 i += sizeof(int);
965 /* Release i2c bus */
966 mutex_unlock(&ir->ir_lock);
968 /* All looks good */
969 return n;
972 /* copied from lirc_dev */
973 static unsigned int poll(struct file *filep, poll_table *wait)
975 struct IR *ir = filep->private_data;
976 unsigned int ret;
978 dprintk("poll called\n");
979 if (ir->c_rx.addr == 0)
980 return -ENODEV;
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);
992 return ret;
995 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
997 struct IR *ir = filep->private_data;
998 int result;
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;
1006 switch (cmd) {
1007 case LIRC_GET_LENGTH:
1008 result = put_user((unsigned long)13,
1009 (unsigned long *)arg);
1010 break;
1011 case LIRC_GET_FEATURES:
1012 result = put_user(features, (unsigned long *) arg);
1013 break;
1014 case LIRC_GET_REC_MODE:
1015 if (!(features&LIRC_CAN_REC_MASK))
1016 return -ENOSYS;
1018 result = put_user(LIRC_REC2MODE
1019 (features&LIRC_CAN_REC_MASK),
1020 (unsigned long *)arg);
1021 break;
1022 case LIRC_SET_REC_MODE:
1023 if (!(features&LIRC_CAN_REC_MASK))
1024 return -ENOSYS;
1026 result = get_user(mode, (unsigned long *)arg);
1027 if (!result && !(LIRC_MODE2REC(mode) & features))
1028 result = -EINVAL;
1029 break;
1030 case LIRC_GET_SEND_MODE:
1031 if (!(features&LIRC_CAN_SEND_MASK))
1032 return -ENOSYS;
1034 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1035 break;
1036 case LIRC_SET_SEND_MODE:
1037 if (!(features&LIRC_CAN_SEND_MASK))
1038 return -ENOSYS;
1040 result = get_user(mode, (unsigned long *) arg);
1041 if (!result && mode != LIRC_MODE_PULSE)
1042 return -EINVAL;
1043 break;
1044 default:
1045 return -EINVAL;
1047 return result;
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)
1056 struct IR *ir;
1057 int ret;
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",
1063 minor);
1064 return -ENODEV;
1066 ir = ir_devices[minor];
1068 /* increment in use count */
1069 mutex_lock(&ir->ir_lock);
1070 ++ir->open;
1071 ret = set_use_inc(ir);
1072 if (ret != 0) {
1073 --ir->open;
1074 mutex_unlock(&ir->ir_lock);
1075 return ret;
1077 mutex_unlock(&ir->ir_lock);
1079 /* stash our IR struct */
1080 filep->private_data = ir;
1082 return 0;
1085 /* Close the IR device */
1086 static int close(struct inode *node, struct file *filep)
1088 /* find our IR struct */
1089 struct IR *ir = filep->private_data;
1090 if (ir == NULL) {
1091 zilog_error("close: no private_data attached to the file!\n");
1092 return -ENODEV;
1095 /* decrement in use count */
1096 mutex_lock(&ir->ir_lock);
1097 --ir->open;
1098 set_use_dec(ir);
1099 mutex_unlock(&ir->ir_lock);
1101 return 0;
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 */
1117 { "ir_video", 0 },
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 = {
1125 .driver = {
1126 .owner = THIS_MODULE,
1127 .name = "Zilog/Hauppauge i2c IR",
1129 .probe = ir_probe,
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,
1137 .llseek = lseek,
1138 .read = read,
1139 .write = write,
1140 .poll = poll,
1141 .unlocked_ioctl = ioctl,
1142 #ifdef CONFIG_COMPAT
1143 .compat_ioctl = ioctl,
1144 #endif
1145 .open = open,
1146 .release = close
1149 static int ir_remove(struct i2c_client *client)
1151 struct IR *ir = i2c_get_clientdata(client);
1153 mutex_lock(&ir->ir_lock);
1155 if (ir->have_rx || ir->have_tx) {
1156 DECLARE_COMPLETION(tn);
1157 DECLARE_COMPLETION(tn2);
1159 /* end up polling thread */
1160 if (ir->task && !IS_ERR(ir->task)) {
1161 ir->t_notify = &tn;
1162 ir->t_notify2 = &tn2;
1163 ir->shutdown = 1;
1164 wake_up_process(ir->task);
1165 complete(&tn2);
1166 wait_for_completion(&tn);
1167 ir->t_notify = NULL;
1168 ir->t_notify2 = NULL;
1171 } else {
1172 mutex_unlock(&ir->ir_lock);
1173 zilog_error("%s: detached from something we didn't "
1174 "attach to\n", __func__);
1175 return -ENODEV;
1178 /* unregister lirc driver */
1179 if (ir->l.minor >= 0 && ir->l.minor < MAX_IRCTL_DEVICES) {
1180 lirc_unregister_driver(ir->l.minor);
1181 ir_devices[ir->l.minor] = NULL;
1184 /* free memory */
1185 lirc_buffer_free(&ir->buf);
1186 mutex_unlock(&ir->ir_lock);
1187 kfree(ir);
1189 return 0;
1192 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1194 struct IR *ir = NULL;
1195 struct i2c_adapter *adap = client->adapter;
1196 char buf;
1197 int ret;
1198 int have_rx = 0, have_tx = 0;
1200 dprintk("%s: adapter id=0x%x, client addr=0x%02x\n",
1201 __func__, adap->id, client->addr);
1204 * The external IR receiver is at i2c address 0x71.
1205 * The IR transmitter is at 0x70.
1207 client->addr = 0x70;
1209 if (!disable_tx) {
1210 if (i2c_master_recv(client, &buf, 1) == 1)
1211 have_tx = 1;
1212 dprintk("probe 0x70 @ %s: %s\n",
1213 adap->name, have_tx ? "success" : "failed");
1216 if (!disable_rx) {
1217 client->addr = 0x71;
1218 if (i2c_master_recv(client, &buf, 1) == 1)
1219 have_rx = 1;
1220 dprintk("probe 0x71 @ %s: %s\n",
1221 adap->name, have_rx ? "success" : "failed");
1224 if (!(have_rx || have_tx)) {
1225 zilog_error("%s: no devices found\n", adap->name);
1226 goto out_nodev;
1229 printk(KERN_INFO "lirc_zilog: chip found with %s\n",
1230 have_rx && have_tx ? "RX and TX" :
1231 have_rx ? "RX only" : "TX only");
1233 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1235 if (!ir)
1236 goto out_nomem;
1238 ret = lirc_buffer_init(&ir->buf, 2, BUFLEN / 2);
1239 if (ret)
1240 goto out_nomem;
1242 mutex_init(&ir->ir_lock);
1243 mutex_init(&ir->buf_lock);
1244 ir->need_boot = 1;
1246 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1247 ir->l.minor = -1;
1249 /* I2C attach to device */
1250 i2c_set_clientdata(client, ir);
1252 /* initialise RX device */
1253 if (have_rx) {
1254 DECLARE_COMPLETION(tn);
1255 memcpy(&ir->c_rx, client, sizeof(struct i2c_client));
1257 ir->c_rx.addr = 0x71;
1258 strlcpy(ir->c_rx.name, ZILOG_HAUPPAUGE_IR_RX_NAME,
1259 I2C_NAME_SIZE);
1261 /* try to fire up polling thread */
1262 ir->t_notify = &tn;
1263 ir->task = kthread_run(lirc_thread, ir, "lirc_zilog");
1264 if (IS_ERR(ir->task)) {
1265 ret = PTR_ERR(ir->task);
1266 zilog_error("lirc_register_driver: cannot run "
1267 "poll thread %d\n", ret);
1268 goto err;
1270 wait_for_completion(&tn);
1271 ir->t_notify = NULL;
1272 ir->have_rx = 1;
1275 /* initialise TX device */
1276 if (have_tx) {
1277 memcpy(&ir->c_tx, client, sizeof(struct i2c_client));
1278 ir->c_tx.addr = 0x70;
1279 strlcpy(ir->c_tx.name, ZILOG_HAUPPAUGE_IR_TX_NAME,
1280 I2C_NAME_SIZE);
1281 ir->have_tx = 1;
1284 /* set lirc_dev stuff */
1285 ir->l.code_length = 13;
1286 ir->l.rbuf = &ir->buf;
1287 ir->l.fops = &lirc_fops;
1288 ir->l.data = ir;
1289 ir->l.minor = minor;
1290 ir->l.dev = &adap->dev;
1291 ir->l.sample_rate = 0;
1293 /* register with lirc */
1294 ir->l.minor = lirc_register_driver(&ir->l);
1295 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1296 zilog_error("ir_attach: \"minor\" must be between 0 and %d "
1297 "(%d)!\n", MAX_IRCTL_DEVICES-1, ir->l.minor);
1298 ret = -EBADRQC;
1299 goto err;
1302 /* store this for getting back in open() later on */
1303 ir_devices[ir->l.minor] = ir;
1306 * if we have the tx device, load the 'firmware'. We do this
1307 * after registering with lirc as otherwise hotplug seems to take
1308 * 10s to create the lirc device.
1310 if (have_tx) {
1311 /* Special TX init */
1312 ret = tx_init(ir);
1313 if (ret != 0)
1314 goto err;
1317 return 0;
1319 err:
1320 /* undo everything, hopefully... */
1321 if (ir->c_rx.addr)
1322 ir_remove(&ir->c_rx);
1323 if (ir->c_tx.addr)
1324 ir_remove(&ir->c_tx);
1325 return ret;
1327 out_nodev:
1328 zilog_error("no device found\n");
1329 return -ENODEV;
1331 out_nomem:
1332 zilog_error("memory allocation failure\n");
1333 kfree(ir);
1334 return -ENOMEM;
1337 static int ir_command(struct i2c_client *client, unsigned int cmd, void *arg)
1339 /* nothing */
1340 return 0;
1343 static int __init zilog_init(void)
1345 int ret;
1347 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1349 mutex_init(&tx_data_lock);
1351 request_module("firmware_class");
1353 ret = i2c_add_driver(&driver);
1354 if (ret)
1355 zilog_error("initialization failed\n");
1356 else
1357 zilog_notify("initialization complete\n");
1359 return ret;
1362 static void __exit zilog_exit(void)
1364 i2c_del_driver(&driver);
1365 /* if loaded */
1366 fw_unload();
1367 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1370 module_init(zilog_init);
1371 module_exit(zilog_exit);
1373 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1374 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1375 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver");
1376 MODULE_LICENSE("GPL");
1377 /* for compat with old name, which isn't all that accurate anymore */
1378 MODULE_ALIAS("lirc_pvr150");
1380 module_param(minor, int, 0444);
1381 MODULE_PARM_DESC(minor, "Preferred minor device number");
1383 module_param(debug, bool, 0644);
1384 MODULE_PARM_DESC(debug, "Enable debugging messages");
1386 module_param(disable_rx, bool, 0644);
1387 MODULE_PARM_DESC(disable_rx, "Disable the IR receiver device");
1389 module_param(disable_tx, bool, 0644);
1390 MODULE_PARM_DESC(disable_tx, "Disable the IR transmitter device");