]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/lirc/lirc_zilog.c
[media] lirc_zilog: Convert the instance open count to an atomic_t
[mirror_ubuntu-artful-kernel.git] / drivers / staging / lirc / lirc_zilog.c
CommitLineData
69b1214c
JW
1/*
2 * i2c IR lirc driver for devices with zilog IR processors
3 *
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>
20 *
21 * parts are cut&pasted from the lirc_i2c.c driver
22 *
c2790c71
AW
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>
25 *
69b1214c
JW
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.
30 *
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.
35 *
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
39 *
40 */
41
42
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>
59
60#include <linux/mutex.h>
61#include <linux/kthread.h>
62
63#include <media/lirc_dev.h>
64#include <media/lirc.h>
65
06da95a3 66struct IR_rx {
69b1214c 67 /* RX device */
e9b351f6 68 struct i2c_client *c;
69b1214c
JW
69
70 /* RX device buffer & lock */
71 struct lirc_buffer buf;
72 struct mutex buf_lock;
73
74 /* RX polling thread data */
69b1214c
JW
75 struct task_struct *task;
76
77 /* RX read data */
78 unsigned char b[3];
06da95a3
AW
79 bool hdpvr_data_fmt;
80};
69b1214c 81
06da95a3 82struct IR_tx {
69b1214c 83 /* TX device */
e9b351f6 84 struct i2c_client *c;
06da95a3
AW
85
86 /* TX additional actions needed */
69b1214c 87 int need_boot;
06da95a3
AW
88 bool post_tx_ready_poll;
89};
90
91struct IR {
5c07134f
AW
92 struct list_head list;
93
06da95a3
AW
94 struct lirc_driver l;
95
96 struct mutex ir_lock;
a3064855 97 atomic_t open_count;
06da95a3 98
a68a9b73 99 struct i2c_adapter *adapter;
06da95a3
AW
100 struct IR_rx *rx;
101 struct IR_tx *tx;
69b1214c
JW
102};
103
5c07134f
AW
104/* IR transceiver instance object list */
105static DEFINE_MUTEX(ir_devices_lock);
106static LIST_HEAD(ir_devices_list);
69b1214c
JW
107
108/* Block size for IR transmitter */
109#define TX_BLOCK_SIZE 99
110
111/* Hauppauge IR transmitter data */
112struct tx_data_struct {
113 /* Boot block */
114 unsigned char *boot_data;
115
116 /* Start of binary data block */
117 unsigned char *datap;
118
119 /* End of binary data block */
120 unsigned char *endp;
121
122 /* Number of installed codesets */
123 unsigned int num_code_sets;
124
125 /* Pointers to codesets */
126 unsigned char **code_sets;
127
128 /* Global fixed data template */
129 int fixed[TX_BLOCK_SIZE];
130};
131
132static struct tx_data_struct *tx_data;
133static struct mutex tx_data_lock;
134
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)
a68a9b73 138#define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
69b1214c 139
69b1214c
JW
140/* module parameters */
141static int debug; /* debug output */
a68a9b73 142static int tx_only; /* only handle the IR Tx function */
69b1214c
JW
143static int minor = -1; /* minor number */
144
145#define dprintk(fmt, args...) \
146 do { \
147 if (debug) \
148 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
149 ## args); \
150 } while (0)
151
152static int add_to_buf(struct IR *ir)
153{
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 };
06da95a3
AW
161 struct IR_rx *rx = ir->rx;
162
163 if (rx == NULL)
164 return -ENXIO;
69b1214c 165
06da95a3 166 if (lirc_buffer_full(&rx->buf)) {
69b1214c
JW
167 dprintk("buffer overflow\n");
168 return -EOVERFLOW;
169 }
170
171 /*
172 * service the device as long as it is returning
173 * data and we have space
174 */
175 do {
b757730b
AW
176 if (kthread_should_stop())
177 return -ENODATA;
178
69b1214c
JW
179 /*
180 * Lock i2c bus for the duration. RX/TX chips interfere so
181 * this is worth it
182 */
183 mutex_lock(&ir->ir_lock);
184
b757730b
AW
185 if (kthread_should_stop()) {
186 mutex_unlock(&ir->ir_lock);
187 return -ENODATA;
188 }
189
69b1214c
JW
190 /*
191 * Send random "poll command" (?) Windows driver does this
192 * and it is a good point to detect chip failure.
193 */
e9b351f6 194 ret = i2c_master_send(rx->c, sendbuf, 1);
69b1214c
JW
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;
202 }
203
204 /* Looks like the chip crashed, reset it */
205 zilog_error("polling the IR receiver chip failed, "
206 "trying reset\n");
207
208 set_current_state(TASK_UNINTERRUPTIBLE);
b757730b
AW
209 if (kthread_should_stop()) {
210 mutex_unlock(&ir->ir_lock);
211 return -ENODATA;
212 }
69b1214c 213 schedule_timeout((100 * HZ + 999) / 1000);
12d896e1
AW
214 if (ir->tx != NULL)
215 ir->tx->need_boot = 1;
69b1214c
JW
216
217 ++failures;
218 mutex_unlock(&ir->ir_lock);
219 continue;
220 }
221
b757730b
AW
222 if (kthread_should_stop()) {
223 mutex_unlock(&ir->ir_lock);
224 return -ENODATA;
225 }
e9b351f6 226 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
69b1214c
JW
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 {
06da95a3
AW
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]);
69b1214c
JW
236 }
237
238 /* key pressed ? */
06da95a3 239 if (rx->hdpvr_data_fmt) {
69b1214c
JW
240 if (got_data && (keybuf[0] == 0x80))
241 return 0;
242 else if (got_data && (keybuf[0] == 0x00))
243 return -ENODATA;
06da95a3 244 } else if ((rx->b[0] & 0x80) == 0)
69b1214c
JW
245 return got_data ? 0 : -ENODATA;
246
247 /* look what we have */
06da95a3 248 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
69b1214c
JW
249
250 codes[0] = (code >> 8) & 0xff;
251 codes[1] = code & 0xff;
252
253 /* return it */
06da95a3 254 lirc_buffer_write(&rx->buf, codes);
69b1214c 255 ++got_data;
06da95a3 256 } while (!lirc_buffer_full(&rx->buf));
69b1214c
JW
257
258 return 0;
259}
260
261/*
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.
270 */
271static int lirc_thread(void *arg)
272{
273 struct IR *ir = arg;
06da95a3 274 struct IR_rx *rx = ir->rx;
69b1214c 275
69b1214c
JW
276 dprintk("poll thread started\n");
277
b757730b
AW
278 while (!kthread_should_stop()) {
279 set_current_state(TASK_INTERRUPTIBLE);
69b1214c 280
b757730b 281 /* if device not opened, we can sleep half a second */
a3064855 282 if (atomic_read(&ir->open_count) == 0) {
69b1214c 283 schedule_timeout(HZ/2);
b757730b 284 continue;
69b1214c 285 }
69b1214c 286
b757730b
AW
287 /*
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.
296 */
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);
302 }
69b1214c
JW
303
304 dprintk("poll thread ended\n");
305 return 0;
306}
307
308static int set_use_inc(void *data)
309{
69b1214c
JW
310 return 0;
311}
312
313static void set_use_dec(void *data)
314{
9b28500a 315 return;
69b1214c
JW
316}
317
318/* safe read of a uint32 (always network byte order) */
319static int read_uint32(unsigned char **data,
320 unsigned char *endp, unsigned int *val)
321{
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;
328}
329
330/* safe read of a uint8 */
331static int read_uint8(unsigned char **data,
332 unsigned char *endp, unsigned char *val)
333{
334 if (*data + 1 > endp)
335 return 0;
336 *val = *((*data)++);
337 return 1;
338}
339
340/* safe skipping of N bytes */
341static int skip(unsigned char **data,
342 unsigned char *endp, unsigned int distance)
343{
344 if (*data + distance > endp)
345 return 0;
346 *data += distance;
347 return 1;
348}
349
350/* decompress key data into the given buffer */
351static int get_key_data(unsigned char *buf,
352 unsigned int codeset, unsigned int key)
353{
354 unsigned char *data, *endp, *diffs, *key_block;
355 unsigned char keys, ndiffs, id;
356 unsigned int base, lim, pos, i;
357
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];
362
363 if (!read_uint32(&data, tx_data->endp, &i))
364 goto corrupt;
365
366 if (i == codeset)
367 break;
368 else if (codeset > i) {
369 base = pos + 1;
370 --lim;
371 }
372 }
373 /* Not found? */
374 if (!lim)
375 return -EPROTO;
376
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;
380
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;
386
387 /* Save diffs & skip */
388 diffs = data;
389 if (!skip(&data, endp, ndiffs))
390 goto corrupt;
391
392 /* Read the id of the first key */
393 if (!read_uint8(&data, endp, &id))
394 goto corrupt;
395
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];
403 }
404 }
405
406 /* Early out key found/not found */
407 if (key == id)
408 return 0;
409 if (keys == 1)
410 return -EPROTO;
411
412 /* Sanity check */
413 key_block = data;
414 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
415 goto corrupt;
416
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;
423
424 if (*key_data == key) {
425 /* skip key id */
426 ++key_data;
427
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;
435 }
436
437 return 0;
438 } else if (key > *key_data) {
439 base = pos + 1;
440 --lim;
441 }
442 }
443 /* Key not found */
444 return -EPROTO;
445
446corrupt:
447 zilog_error("firmware is corrupt\n");
448 return -EFAULT;
449}
450
451/* send a block of data to the IR TX device */
06da95a3 452static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
69b1214c
JW
453{
454 int i, j, ret;
455 unsigned char buf[5];
456
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]);
e9b351f6 466 ret = i2c_master_send(tx->c, buf, tosend + 1);
69b1214c
JW
467 if (ret != tosend + 1) {
468 zilog_error("i2c_master_send failed with %d\n", ret);
469 return ret < 0 ? ret : -EFAULT;
470 }
471 i += tosend;
472 }
473 return 0;
474}
475
476/* send boot data to the IR TX device */
06da95a3 477static int send_boot_data(struct IR_tx *tx)
69b1214c 478{
5766d204 479 int ret, i;
69b1214c
JW
480 unsigned char buf[4];
481
482 /* send the boot block */
06da95a3 483 ret = send_data_block(tx, tx_data->boot_data);
69b1214c
JW
484 if (ret != 0)
485 return ret;
486
5766d204 487 /* Hit the go button to activate the new boot data */
69b1214c
JW
488 buf[0] = 0x00;
489 buf[1] = 0x20;
e9b351f6 490 ret = i2c_master_send(tx->c, buf, 2);
69b1214c
JW
491 if (ret != 2) {
492 zilog_error("i2c_master_send failed with %d\n", ret);
493 return ret < 0 ? ret : -EFAULT;
494 }
5766d204
JW
495
496 /*
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.
500 */
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);
506 }
507
69b1214c
JW
508 if (ret != 1) {
509 zilog_error("i2c_master_send failed with %d\n", ret);
510 return ret < 0 ? ret : -EFAULT;
511 }
512
513 /* Here comes the firmware version... (hopefully) */
e9b351f6 514 ret = i2c_master_recv(tx->c, buf, 4);
69b1214c
JW
515 if (ret != 4) {
516 zilog_error("i2c_master_recv failed with %d\n", ret);
517 return 0;
518 }
5766d204
JW
519 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
520 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
69b1214c
JW
521 return 0;
522 }
523 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
524 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
525
526 return 0;
527}
528
529/* unload "firmware", lock held */
530static void fw_unload_locked(void)
531{
532 if (tx_data) {
533 if (tx_data->code_sets)
534 vfree(tx_data->code_sets);
535
536 if (tx_data->datap)
537 vfree(tx_data->datap);
538
539 vfree(tx_data);
540 tx_data = NULL;
541 dprintk("successfully unloaded IR blaster firmware\n");
542 }
543}
544
545/* unload "firmware" for the IR TX device */
546static void fw_unload(void)
547{
548 mutex_lock(&tx_data_lock);
549 fw_unload_locked();
550 mutex_unlock(&tx_data_lock);
551}
552
553/* load "firmware" for the IR TX device */
06da95a3 554static int fw_load(struct IR_tx *tx)
69b1214c
JW
555{
556 int ret;
557 unsigned int i;
558 unsigned char *data, version, num_global_fixed;
559 const struct firmware *fw_entry;
560
561 /* Already loaded? */
562 mutex_lock(&tx_data_lock);
563 if (tx_data) {
564 ret = 0;
565 goto out;
566 }
567
568 /* Request codeset data file */
e9b351f6 569 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
69b1214c
JW
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;
575 }
576 dprintk("firmware of size %zu loaded\n", fw_entry->size);
577
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;
585 }
586 tx_data->code_sets = NULL;
587
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;
596 }
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;
600
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;
612 }
613
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;
618
619 if (!read_uint32(&data, tx_data->endp,
620 &tx_data->num_code_sets))
621 goto corrupt;
622
623 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
624
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;
631 }
632
633 for (i = 0; i < TX_BLOCK_SIZE; ++i)
634 tx_data->fixed[i] = -1;
635
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;
647 }
648
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;
654
655 /* Save the codeset position */
656 tx_data->code_sets[i] = data;
657
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;
664
665 /* skip diff positions */
666 if (!skip(&data, tx_data->endp, ndiffs))
667 goto corrupt;
668
669 /*
670 * After the diffs we have the first key id + data -
671 * global fixed
672 */
673 if (!skip(&data, tx_data->endp,
674 1 + TX_BLOCK_SIZE - num_global_fixed))
675 goto corrupt;
676
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;
681 }
682 ret = 0;
683 goto out;
684
685corrupt:
686 zilog_error("firmware is corrupt\n");
687 fw_unload_locked();
688 ret = -EFAULT;
689
690out:
691 mutex_unlock(&tx_data_lock);
692 return ret;
693}
694
695/* initialise the IR TX device */
06da95a3 696static int tx_init(struct IR_tx *tx)
69b1214c
JW
697{
698 int ret;
699
700 /* Load 'firmware' */
06da95a3 701 ret = fw_load(tx);
69b1214c
JW
702 if (ret != 0)
703 return ret;
704
705 /* Send boot block */
06da95a3 706 ret = send_boot_data(tx);
69b1214c
JW
707 if (ret != 0)
708 return ret;
06da95a3 709 tx->need_boot = 0;
69b1214c
JW
710
711 /* Looks good */
712 return 0;
713}
714
715/* do nothing stub to make LIRC happy */
716static loff_t lseek(struct file *filep, loff_t offset, int orig)
717{
718 return -ESPIPE;
719}
720
721/* copied from lirc_dev */
722static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
723{
e0ac7da0 724 struct IR *ir = filep->private_data;
06da95a3 725 struct IR_rx *rx = ir->rx;
69b1214c
JW
726 int ret = 0, written = 0;
727 DECLARE_WAITQUEUE(wait, current);
728
729 dprintk("read called\n");
06da95a3 730 if (rx == NULL)
69b1214c
JW
731 return -ENODEV;
732
06da95a3 733 if (mutex_lock_interruptible(&rx->buf_lock))
69b1214c
JW
734 return -ERESTARTSYS;
735
06da95a3 736 if (n % rx->buf.chunk_size) {
69b1214c 737 dprintk("read result = -EINVAL\n");
06da95a3 738 mutex_unlock(&rx->buf_lock);
69b1214c
JW
739 return -EINVAL;
740 }
741
742 /*
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)
746 */
06da95a3 747 add_wait_queue(&rx->buf.wait_poll, &wait);
69b1214c
JW
748 set_current_state(TASK_INTERRUPTIBLE);
749
750 /*
751 * while we didn't provide 'length' bytes, device is opened in blocking
752 * mode and 'copy_to_user' is happy, wait for data.
753 */
754 while (written < n && ret == 0) {
06da95a3 755 if (lirc_buffer_empty(&rx->buf)) {
69b1214c
JW
756 /*
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
761 */
762 if (written)
763 break;
764 if (filep->f_flags & O_NONBLOCK) {
765 ret = -EWOULDBLOCK;
766 break;
767 }
768 if (signal_pending(current)) {
769 ret = -ERESTARTSYS;
770 break;
771 }
772 schedule();
773 set_current_state(TASK_INTERRUPTIBLE);
774 } else {
06da95a3
AW
775 unsigned char buf[rx->buf.chunk_size];
776 lirc_buffer_read(&rx->buf, buf);
69b1214c 777 ret = copy_to_user((void *)outbuf+written, buf,
06da95a3
AW
778 rx->buf.chunk_size);
779 written += rx->buf.chunk_size;
69b1214c
JW
780 }
781 }
782
06da95a3 783 remove_wait_queue(&rx->buf.wait_poll, &wait);
69b1214c 784 set_current_state(TASK_RUNNING);
06da95a3 785 mutex_unlock(&rx->buf_lock);
69b1214c
JW
786
787 dprintk("read result = %s (%d)\n",
788 ret ? "-EFAULT" : "OK", ret);
789
790 return ret ? ret : written;
791}
792
793/* send a keypress to the IR TX device */
06da95a3 794static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
69b1214c
JW
795{
796 unsigned char data_block[TX_BLOCK_SIZE];
797 unsigned char buf[2];
798 int i, ret;
799
800 /* Get data for the codeset/key */
801 ret = get_key_data(data_block, code, key);
802
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;
809
810 /* Send the data block */
06da95a3 811 ret = send_data_block(tx, data_block);
69b1214c
JW
812 if (ret != 0)
813 return ret;
814
815 /* Send data block length? */
816 buf[0] = 0x00;
817 buf[1] = 0x40;
e9b351f6 818 ret = i2c_master_send(tx->c, buf, 2);
69b1214c
JW
819 if (ret != 2) {
820 zilog_error("i2c_master_send failed with %d\n", ret);
821 return ret < 0 ? ret : -EFAULT;
822 }
5766d204
JW
823
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);
830 }
831
69b1214c
JW
832 if (ret != 1) {
833 zilog_error("i2c_master_send failed with %d\n", ret);
834 return ret < 0 ? ret : -EFAULT;
835 }
836
837 /* Send finished download? */
e9b351f6 838 ret = i2c_master_recv(tx->c, buf, 1);
69b1214c
JW
839 if (ret != 1) {
840 zilog_error("i2c_master_recv failed with %d\n", ret);
841 return ret < 0 ? ret : -EFAULT;
842 }
843 if (buf[0] != 0xA0) {
844 zilog_error("unexpected IR TX response #1: %02x\n",
845 buf[0]);
846 return -EFAULT;
847 }
848
849 /* Send prepare command? */
850 buf[0] = 0x00;
851 buf[1] = 0x80;
e9b351f6 852 ret = i2c_master_send(tx->c, buf, 2);
69b1214c
JW
853 if (ret != 2) {
854 zilog_error("i2c_master_send failed with %d\n", ret);
855 return ret < 0 ? ret : -EFAULT;
856 }
857
69b1214c
JW
858 /*
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
862 */
06da95a3 863 if (!tx->post_tx_ready_poll) {
d7c72356
AW
864 dprintk("sent code %u, key %u\n", code, key);
865 return 0;
866 }
69b1214c
JW
867
868 /*
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.
873 */
874 for (i = 0; i < 20; ++i) {
875 set_current_state(TASK_UNINTERRUPTIBLE);
876 schedule_timeout((50 * HZ + 999) / 1000);
e9b351f6 877 ret = i2c_master_send(tx->c, buf, 1);
69b1214c
JW
878 if (ret == 1)
879 break;
880 dprintk("NAK expected: i2c_master_send "
881 "failed with %d (try %d)\n", ret, i+1);
882 }
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;
887 }
888
889 /* Seems to be an 'ok' response */
e9b351f6 890 i = i2c_master_recv(tx->c, buf, 1);
69b1214c
JW
891 if (i != 1) {
892 zilog_error("i2c_master_recv failed with %d\n", ret);
893 return -EFAULT;
894 }
895 if (buf[0] != 0x80) {
896 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
897 return -EFAULT;
898 }
899
69b1214c
JW
900 /* Oh good, it worked */
901 dprintk("sent code %u, key %u\n", code, key);
902 return 0;
903}
904
905/*
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.
910 */
911static ssize_t write(struct file *filep, const char *buf, size_t n,
912 loff_t *ppos)
913{
e0ac7da0 914 struct IR *ir = filep->private_data;
06da95a3 915 struct IR_tx *tx = ir->tx;
69b1214c
JW
916 size_t i;
917 int failures = 0;
918
06da95a3 919 if (tx == NULL)
69b1214c
JW
920 return -ENODEV;
921
922 /* Validate user parameters */
923 if (n % sizeof(int))
924 return -EINVAL;
925
926 /* Lock i2c bus for the duration */
927 mutex_lock(&ir->ir_lock);
928
929 /* Send each keypress */
930 for (i = 0; i < n;) {
931 int ret = 0;
932 int command;
933
934 if (copy_from_user(&command, buf + i, sizeof(command))) {
935 mutex_unlock(&ir->ir_lock);
936 return -EFAULT;
937 }
938
939 /* Send boot data first if required */
06da95a3
AW
940 if (tx->need_boot == 1) {
941 ret = send_boot_data(tx);
69b1214c 942 if (ret == 0)
06da95a3 943 tx->need_boot = 0;
69b1214c
JW
944 }
945
946 /* Send the code */
947 if (ret == 0) {
06da95a3 948 ret = send_code(tx, (unsigned)command >> 16,
69b1214c
JW
949 (unsigned)command & 0xFFFF);
950 if (ret == -EPROTO) {
951 mutex_unlock(&ir->ir_lock);
952 return ret;
953 }
954 }
955
956 /*
957 * Hmm, a failure. If we've had a few then give up, otherwise
958 * try a reset
959 */
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");
964
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;
970 }
971 set_current_state(TASK_UNINTERRUPTIBLE);
972 schedule_timeout((100 * HZ + 999) / 1000);
06da95a3 973 tx->need_boot = 1;
69b1214c
JW
974 ++failures;
975 } else
976 i += sizeof(int);
977 }
978
979 /* Release i2c bus */
980 mutex_unlock(&ir->ir_lock);
981
982 /* All looks good */
983 return n;
984}
985
986/* copied from lirc_dev */
987static unsigned int poll(struct file *filep, poll_table *wait)
988{
e0ac7da0 989 struct IR *ir = filep->private_data;
06da95a3 990 struct IR_rx *rx = ir->rx;
69b1214c
JW
991 unsigned int ret;
992
993 dprintk("poll called\n");
06da95a3 994 if (rx == NULL)
69b1214c
JW
995 return -ENODEV;
996
06da95a3 997 mutex_lock(&rx->buf_lock);
69b1214c 998
06da95a3 999 poll_wait(filep, &rx->buf.wait_poll, wait);
69b1214c
JW
1000
1001 dprintk("poll result = %s\n",
06da95a3 1002 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
69b1214c 1003
06da95a3 1004 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
69b1214c 1005
06da95a3 1006 mutex_unlock(&rx->buf_lock);
69b1214c
JW
1007 return ret;
1008}
1009
1010static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1011{
e0ac7da0 1012 struct IR *ir = filep->private_data;
69b1214c
JW
1013 int result;
1014 unsigned long mode, features = 0;
1015
06da95a3 1016 if (ir->rx != NULL)
69b1214c 1017 features |= LIRC_CAN_REC_LIRCCODE;
12d896e1
AW
1018 if (ir->tx != NULL)
1019 features |= LIRC_CAN_SEND_PULSE;
69b1214c
JW
1020
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;
1032
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;
1040
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:
12d896e1
AW
1046 if (!(features&LIRC_CAN_SEND_MASK))
1047 return -ENOSYS;
1048
69b1214c
JW
1049 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1050 break;
1051 case LIRC_SET_SEND_MODE:
12d896e1
AW
1052 if (!(features&LIRC_CAN_SEND_MASK))
1053 return -ENOSYS;
1054
69b1214c
JW
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;
1061 }
1062 return result;
1063}
1064
a68a9b73
AW
1065/* ir_devices_lock must be held */
1066static struct IR *find_ir_device_by_minor(unsigned int minor)
1067{
5c07134f
AW
1068 struct IR *ir;
1069
1070 if (list_empty(&ir_devices_list))
a68a9b73
AW
1071 return NULL;
1072
5c07134f
AW
1073 list_for_each_entry(ir, &ir_devices_list, list)
1074 if (ir->l.minor == minor)
1075 return ir;
1076
1077 return NULL;
a68a9b73
AW
1078}
1079
69b1214c
JW
1080/*
1081 * Open the IR device. Get hold of our IR structure and
1082 * stash it in private_data for the file
1083 */
1084static int open(struct inode *node, struct file *filep)
1085{
1086 struct IR *ir;
a68a9b73 1087 unsigned int minor = MINOR(node->i_rdev);
69b1214c
JW
1088
1089 /* find our IR struct */
a68a9b73
AW
1090 mutex_lock(&ir_devices_lock);
1091 ir = find_ir_device_by_minor(minor);
1092 mutex_unlock(&ir_devices_lock);
1093
1094 if (ir == NULL)
69b1214c 1095 return -ENODEV;
69b1214c 1096
a3064855 1097 atomic_inc(&ir->open_count);
69b1214c
JW
1098
1099 /* stash our IR struct */
1100 filep->private_data = ir;
1101
1102 return 0;
1103}
1104
1105/* Close the IR device */
1106static int close(struct inode *node, struct file *filep)
1107{
1108 /* find our IR struct */
e0ac7da0 1109 struct IR *ir = filep->private_data;
69b1214c
JW
1110 if (ir == NULL) {
1111 zilog_error("close: no private_data attached to the file!\n");
1112 return -ENODEV;
1113 }
1114
a3064855 1115 atomic_dec(&ir->open_count);
69b1214c
JW
1116
1117 return 0;
1118}
1119
1120static 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
1125};
1126
1127static int ir_remove(struct i2c_client *client);
1128static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
69b1214c 1129
d7c72356
AW
1130#define ID_FLAG_TX 0x01
1131#define ID_FLAG_HDPVR 0x02
1132
69b1214c 1133static const struct i2c_device_id ir_transceiver_id[] = {
d7c72356
AW
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 },
69b1214c
JW
1138 { }
1139};
1140
1141static struct i2c_driver driver = {
1142 .driver = {
1143 .owner = THIS_MODULE,
1144 .name = "Zilog/Hauppauge i2c IR",
1145 },
1146 .probe = ir_probe,
1147 .remove = ir_remove,
69b1214c
JW
1148 .id_table = ir_transceiver_id,
1149};
1150
0f9313ad 1151static const struct file_operations lirc_fops = {
69b1214c
JW
1152 .owner = THIS_MODULE,
1153 .llseek = lseek,
1154 .read = read,
1155 .write = write,
1156 .poll = poll,
1157 .unlocked_ioctl = ioctl,
8be292cc
JW
1158#ifdef CONFIG_COMPAT
1159 .compat_ioctl = ioctl,
1160#endif
69b1214c
JW
1161 .open = open,
1162 .release = close
1163};
1164
a68a9b73 1165static void destroy_rx_kthread(struct IR_rx *rx)
69b1214c 1166{
a68a9b73 1167 /* end up polling thread */
b757730b
AW
1168 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1169 kthread_stop(rx->task);
1170 rx->task = NULL;
a68a9b73
AW
1171 }
1172}
69b1214c 1173
a68a9b73
AW
1174/* ir_devices_lock must be held */
1175static int add_ir_device(struct IR *ir)
1176{
5c07134f
AW
1177 list_add_tail(&ir->list, &ir_devices_list);
1178 return 0;
a68a9b73
AW
1179}
1180
1181/* ir_devices_lock must be held */
1182static void del_ir_device(struct IR *ir)
1183{
5c07134f
AW
1184 struct IR *p;
1185
1186 if (list_empty(&ir_devices_list))
1187 return;
a68a9b73 1188
5c07134f
AW
1189 list_for_each_entry(p, &ir_devices_list, list)
1190 if (p == ir) {
1191 list_del(&p->list);
a68a9b73
AW
1192 break;
1193 }
1194}
1195
1196static int ir_remove(struct i2c_client *client)
1197{
1198 struct IR *ir = i2c_get_clientdata(client);
1199
1200 mutex_lock(&ir_devices_lock);
1201
1202 if (ir == NULL) {
1203 /* We destroyed everything when the first client came through */
1204 mutex_unlock(&ir_devices_lock);
1205 return 0;
69b1214c
JW
1206 }
1207
a68a9b73
AW
1208 /* Good-bye LIRC */
1209 lirc_unregister_driver(ir->l.minor);
1210
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);
69b1214c
JW
1218 }
1219
a68a9b73 1220 /* Good-bye Tx */
12d896e1
AW
1221 if (ir->tx != NULL) {
1222 i2c_set_clientdata(ir->tx->c, NULL);
1223 kfree(ir->tx);
1224 }
a68a9b73
AW
1225
1226 /* Good-bye IR */
1227 del_ir_device(ir);
69b1214c
JW
1228 kfree(ir);
1229
a68a9b73 1230 mutex_unlock(&ir_devices_lock);
69b1214c
JW
1231 return 0;
1232}
1233
a68a9b73
AW
1234
1235/* ir_devices_lock must be held */
1236static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
69b1214c 1237{
5c07134f 1238 struct IR *ir;
a68a9b73 1239
5c07134f
AW
1240 if (list_empty(&ir_devices_list))
1241 return NULL;
1242
1243 list_for_each_entry(ir, &ir_devices_list, list)
1244 if (ir->adapter == adapter)
1245 return ir;
a68a9b73 1246
5c07134f 1247 return NULL;
a68a9b73
AW
1248}
1249
1250static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1251{
1252 struct IR *ir;
69b1214c 1253 struct i2c_adapter *adap = client->adapter;
69b1214c 1254 int ret;
a68a9b73 1255 bool tx_probe = false;
69b1214c 1256
86e52428
AW
1257 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1258 __func__, id->name, adap->nr, adap->name, client->addr);
69b1214c 1259
d7c72356 1260 /*
a68a9b73
AW
1261 * The IR receiver is at i2c address 0x71.
1262 * The IR transmitter is at i2c address 0x70.
69b1214c 1263 */
69b1214c 1264
a68a9b73
AW
1265 if (id->driver_data & ID_FLAG_TX)
1266 tx_probe = true;
1267 else if (tx_only) /* module option */
e9b351f6 1268 return -ENXIO;
69b1214c 1269
b757730b
AW
1270 zilog_info("probing IR %s on %s (i2c-%d)\n",
1271 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
69b1214c 1272
a68a9b73 1273 mutex_lock(&ir_devices_lock);
69b1214c 1274
a68a9b73
AW
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;
06da95a3 1282 }
a68a9b73 1283 /* store for use in ir_probe() again, and open() later on */
5c07134f 1284 INIT_LIST_HEAD(&ir->list);
a68a9b73
AW
1285 ret = add_ir_device(ir);
1286 if (ret)
1287 goto out_free_ir;
1288
1289 ir->adapter = adap;
1290 mutex_init(&ir->ir_lock);
a3064855 1291 atomic_set(&ir->open_count, 0);
a68a9b73
AW
1292
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;
06da95a3
AW
1302 }
1303
a68a9b73
AW
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;
1310 }
06da95a3 1311
a68a9b73
AW
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);
06da95a3
AW
1319 if (ir->rx == NULL) {
1320 ret = -ENOMEM;
a68a9b73 1321 goto out_free_xx;
06da95a3
AW
1322 }
1323
a68a9b73
AW
1324 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1325 if (ret)
1326 goto out_free_xx;
69b1214c 1327
a68a9b73
AW
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;
69b1214c 1332
a68a9b73
AW
1333 /* set lirc_dev stuff */
1334 ir->l.rbuf = &ir->rx->buf;
1335 }
69b1214c 1336
69b1214c
JW
1337 i2c_set_clientdata(client, ir);
1338
a68a9b73
AW
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)) {
b757730b
AW
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");
a68a9b73
AW
1345 goto out_ok;
1346 }
1347
69b1214c 1348 /* initialise RX device */
06da95a3 1349 if (ir->rx != NULL) {
69b1214c 1350 /* try to fire up polling thread */
b757730b
AW
1351 ir->rx->task = kthread_run(lirc_thread, ir,
1352 "zilog-rx-i2c-%d", adap->nr);
06da95a3
AW
1353 if (IS_ERR(ir->rx->task)) {
1354 ret = PTR_ERR(ir->rx->task);
a68a9b73
AW
1355 zilog_error("%s: could not start IR Rx polling thread"
1356 "\n", __func__);
1357 goto out_free_xx;
69b1214c 1358 }
69b1214c
JW
1359 }
1360
69b1214c
JW
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) {
a68a9b73
AW
1364 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1365 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
69b1214c 1366 ret = -EBADRQC;
a68a9b73 1367 goto out_free_thread;
69b1214c
JW
1368 }
1369
69b1214c
JW
1370 /*
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.
1374 */
12d896e1
AW
1375 if (ir->tx != NULL) {
1376 /* Special TX init */
1377 ret = tx_init(ir->tx);
1378 if (ret != 0)
1379 goto out_unregister;
1380 }
69b1214c 1381
b757730b
AW
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);
a68a9b73
AW
1384out_ok:
1385 mutex_unlock(&ir_devices_lock);
69b1214c
JW
1386 return 0;
1387
a68a9b73
AW
1388out_unregister:
1389 lirc_unregister_driver(ir->l.minor);
1390out_free_thread:
1391 destroy_rx_kthread(ir->rx);
1392out_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);
1399 }
1400 if (ir->tx != NULL) {
1401 if (ir->tx->c != NULL)
1402 i2c_set_clientdata(ir->tx->c, NULL);
1403 kfree(ir->tx);
1404 }
1405out_free_ir:
1406 del_ir_device(ir);
69b1214c 1407 kfree(ir);
a68a9b73
AW
1408out_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;
69b1214c
JW
1414}
1415
69b1214c
JW
1416static int __init zilog_init(void)
1417{
1418 int ret;
1419
1420 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1421
1422 mutex_init(&tx_data_lock);
1423
1424 request_module("firmware_class");
1425
1426 ret = i2c_add_driver(&driver);
1427 if (ret)
1428 zilog_error("initialization failed\n");
1429 else
1430 zilog_notify("initialization complete\n");
1431
1432 return ret;
1433}
1434
1435static void __exit zilog_exit(void)
1436{
1437 i2c_del_driver(&driver);
1438 /* if loaded */
1439 fw_unload();
1440 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1441}
1442
1443module_init(zilog_init);
1444module_exit(zilog_exit);
1445
1446MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1447MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
c2790c71
AW
1448 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1449 "Andy Walls");
69b1214c
JW
1450MODULE_LICENSE("GPL");
1451/* for compat with old name, which isn't all that accurate anymore */
1452MODULE_ALIAS("lirc_pvr150");
1453
1454module_param(minor, int, 0444);
1455MODULE_PARM_DESC(minor, "Preferred minor device number");
1456
1457module_param(debug, bool, 0644);
1458MODULE_PARM_DESC(debug, "Enable debugging messages");
1459
a68a9b73
AW
1460module_param(tx_only, bool, 0644);
1461MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");