]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/lirc/lirc_zilog.c
[media] lirc: silence some compile warnings
[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
5bd6b046
AW
66struct IR;
67
06da95a3 68struct IR_rx {
5bd6b046
AW
69 struct kref ref;
70 struct IR *ir;
71
69b1214c 72 /* RX device */
d6dbd939 73 struct mutex client_lock;
e9b351f6 74 struct i2c_client *c;
69b1214c 75
69b1214c 76 /* RX polling thread data */
69b1214c
JW
77 struct task_struct *task;
78
79 /* RX read data */
80 unsigned char b[3];
06da95a3
AW
81 bool hdpvr_data_fmt;
82};
69b1214c 83
06da95a3 84struct IR_tx {
5bd6b046
AW
85 struct kref ref;
86 struct IR *ir;
87
69b1214c 88 /* TX device */
d6dbd939 89 struct mutex client_lock;
e9b351f6 90 struct i2c_client *c;
06da95a3
AW
91
92 /* TX additional actions needed */
69b1214c 93 int need_boot;
06da95a3
AW
94 bool post_tx_ready_poll;
95};
96
97struct IR {
5bd6b046 98 struct kref ref;
5c07134f
AW
99 struct list_head list;
100
5bd6b046 101 /* FIXME spinlock access to l.features */
06da95a3 102 struct lirc_driver l;
bcbd1655 103 struct lirc_buffer rbuf;
06da95a3
AW
104
105 struct mutex ir_lock;
a3064855 106 atomic_t open_count;
06da95a3 107
a68a9b73 108 struct i2c_adapter *adapter;
5bd6b046
AW
109
110 spinlock_t rx_ref_lock; /* struct IR_rx kref get()/put() */
06da95a3 111 struct IR_rx *rx;
5bd6b046
AW
112
113 spinlock_t tx_ref_lock; /* struct IR_tx kref get()/put() */
06da95a3 114 struct IR_tx *tx;
69b1214c
JW
115};
116
5c07134f 117/* IR transceiver instance object list */
5bd6b046
AW
118/*
119 * This lock is used for the following:
120 * a. ir_devices_list access, insertions, deletions
121 * b. struct IR kref get()s and put()s
122 * c. serialization of ir_probe() for the two i2c_clients for a Z8
123 */
5c07134f
AW
124static DEFINE_MUTEX(ir_devices_lock);
125static LIST_HEAD(ir_devices_list);
69b1214c
JW
126
127/* Block size for IR transmitter */
128#define TX_BLOCK_SIZE 99
129
130/* Hauppauge IR transmitter data */
131struct tx_data_struct {
132 /* Boot block */
133 unsigned char *boot_data;
134
135 /* Start of binary data block */
136 unsigned char *datap;
137
138 /* End of binary data block */
139 unsigned char *endp;
140
141 /* Number of installed codesets */
142 unsigned int num_code_sets;
143
144 /* Pointers to codesets */
145 unsigned char **code_sets;
146
147 /* Global fixed data template */
148 int fixed[TX_BLOCK_SIZE];
149};
150
151static struct tx_data_struct *tx_data;
152static struct mutex tx_data_lock;
153
154#define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
155 ## args)
156#define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
a68a9b73 157#define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
69b1214c 158
69b1214c
JW
159/* module parameters */
160static int debug; /* debug output */
a68a9b73 161static int tx_only; /* only handle the IR Tx function */
69b1214c
JW
162static int minor = -1; /* minor number */
163
164#define dprintk(fmt, args...) \
165 do { \
166 if (debug) \
167 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
168 ## args); \
169 } while (0)
170
5bd6b046
AW
171
172/* struct IR reference counting */
173static struct IR *get_ir_device(struct IR *ir, bool ir_devices_lock_held)
174{
175 if (ir_devices_lock_held) {
176 kref_get(&ir->ref);
177 } else {
178 mutex_lock(&ir_devices_lock);
179 kref_get(&ir->ref);
180 mutex_unlock(&ir_devices_lock);
181 }
182 return ir;
183}
184
185static void release_ir_device(struct kref *ref)
186{
187 struct IR *ir = container_of(ref, struct IR, ref);
188
189 /*
190 * Things should be in this state by now:
191 * ir->rx set to NULL and deallocated - happens before ir->rx->ir put()
192 * ir->rx->task kthread stopped - happens before ir->rx->ir put()
193 * ir->tx set to NULL and deallocated - happens before ir->tx->ir put()
194 * ir->open_count == 0 - happens on final close()
195 * ir_lock, tx_ref_lock, rx_ref_lock, all released
196 */
197 if (ir->l.minor >= 0 && ir->l.minor < MAX_IRCTL_DEVICES) {
198 lirc_unregister_driver(ir->l.minor);
199 ir->l.minor = MAX_IRCTL_DEVICES;
200 }
201 if (ir->rbuf.fifo_initialized)
202 lirc_buffer_free(&ir->rbuf);
203 list_del(&ir->list);
204 kfree(ir);
205}
206
207static int put_ir_device(struct IR *ir, bool ir_devices_lock_held)
208{
209 int released;
210
211 if (ir_devices_lock_held)
212 return kref_put(&ir->ref, release_ir_device);
213
214 mutex_lock(&ir_devices_lock);
215 released = kref_put(&ir->ref, release_ir_device);
216 mutex_unlock(&ir_devices_lock);
217
218 return released;
219}
220
221/* struct IR_rx reference counting */
222static struct IR_rx *get_ir_rx(struct IR *ir)
223{
224 struct IR_rx *rx;
225
226 spin_lock(&ir->rx_ref_lock);
227 rx = ir->rx;
228 if (rx != NULL)
229 kref_get(&rx->ref);
230 spin_unlock(&ir->rx_ref_lock);
231 return rx;
232}
233
234static void destroy_rx_kthread(struct IR_rx *rx, bool ir_devices_lock_held)
235{
236 /* end up polling thread */
237 if (!IS_ERR_OR_NULL(rx->task)) {
238 kthread_stop(rx->task);
239 rx->task = NULL;
240 /* Put the ir ptr that ir_probe() gave to the rx poll thread */
241 put_ir_device(rx->ir, ir_devices_lock_held);
242 }
243}
244
245static void release_ir_rx(struct kref *ref)
246{
247 struct IR_rx *rx = container_of(ref, struct IR_rx, ref);
248 struct IR *ir = rx->ir;
249
250 /*
251 * This release function can't do all the work, as we want
252 * to keep the rx_ref_lock a spinlock, and killing the poll thread
253 * and releasing the ir reference can cause a sleep. That work is
254 * performed by put_ir_rx()
255 */
256 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
257 /* Don't put_ir_device(rx->ir) here; lock can't be freed yet */
258 ir->rx = NULL;
259 /* Don't do the kfree(rx) here; we still need to kill the poll thread */
260 return;
261}
262
263static int put_ir_rx(struct IR_rx *rx, bool ir_devices_lock_held)
264{
265 int released;
266 struct IR *ir = rx->ir;
267
268 spin_lock(&ir->rx_ref_lock);
269 released = kref_put(&rx->ref, release_ir_rx);
270 spin_unlock(&ir->rx_ref_lock);
271 /* Destroy the rx kthread while not holding the spinlock */
272 if (released) {
273 destroy_rx_kthread(rx, ir_devices_lock_held);
274 kfree(rx);
275 /* Make sure we're not still in a poll_table somewhere */
276 wake_up_interruptible(&ir->rbuf.wait_poll);
277 }
278 /* Do a reference put() for the rx->ir reference, if we released rx */
279 if (released)
280 put_ir_device(ir, ir_devices_lock_held);
281 return released;
282}
283
284/* struct IR_tx reference counting */
285static struct IR_tx *get_ir_tx(struct IR *ir)
286{
287 struct IR_tx *tx;
288
289 spin_lock(&ir->tx_ref_lock);
290 tx = ir->tx;
291 if (tx != NULL)
292 kref_get(&tx->ref);
293 spin_unlock(&ir->tx_ref_lock);
294 return tx;
295}
296
297static void release_ir_tx(struct kref *ref)
298{
299 struct IR_tx *tx = container_of(ref, struct IR_tx, ref);
300 struct IR *ir = tx->ir;
301
302 ir->l.features &= ~LIRC_CAN_SEND_PULSE;
303 /* Don't put_ir_device(tx->ir) here, so our lock doesn't get freed */
304 ir->tx = NULL;
305 kfree(tx);
306}
307
308static int put_ir_tx(struct IR_tx *tx, bool ir_devices_lock_held)
309{
310 int released;
311 struct IR *ir = tx->ir;
312
313 spin_lock(&ir->tx_ref_lock);
314 released = kref_put(&tx->ref, release_ir_tx);
315 spin_unlock(&ir->tx_ref_lock);
316 /* Do a reference put() for the tx->ir reference, if we released tx */
317 if (released)
318 put_ir_device(ir, ir_devices_lock_held);
319 return released;
320}
321
69b1214c
JW
322static int add_to_buf(struct IR *ir)
323{
324 __u16 code;
325 unsigned char codes[2];
326 unsigned char keybuf[6];
327 int got_data = 0;
328 int ret;
329 int failures = 0;
330 unsigned char sendbuf[1] = { 0 };
bcbd1655 331 struct lirc_buffer *rbuf = ir->l.rbuf;
5bd6b046
AW
332 struct IR_rx *rx;
333 struct IR_tx *tx;
69b1214c 334
bcbd1655 335 if (lirc_buffer_full(rbuf)) {
69b1214c
JW
336 dprintk("buffer overflow\n");
337 return -EOVERFLOW;
338 }
339
5bd6b046
AW
340 rx = get_ir_rx(ir);
341 if (rx == NULL)
342 return -ENXIO;
343
d6dbd939
AW
344 /* Ensure our rx->c i2c_client remains valid for the duration */
345 mutex_lock(&rx->client_lock);
346 if (rx->c == NULL) {
347 mutex_unlock(&rx->client_lock);
348 put_ir_rx(rx, false);
349 return -ENXIO;
350 }
351
5bd6b046
AW
352 tx = get_ir_tx(ir);
353
69b1214c
JW
354 /*
355 * service the device as long as it is returning
356 * data and we have space
357 */
358 do {
5bd6b046
AW
359 if (kthread_should_stop()) {
360 ret = -ENODATA;
361 break;
362 }
b757730b 363
69b1214c
JW
364 /*
365 * Lock i2c bus for the duration. RX/TX chips interfere so
366 * this is worth it
367 */
368 mutex_lock(&ir->ir_lock);
369
b757730b
AW
370 if (kthread_should_stop()) {
371 mutex_unlock(&ir->ir_lock);
5bd6b046
AW
372 ret = -ENODATA;
373 break;
b757730b
AW
374 }
375
69b1214c
JW
376 /*
377 * Send random "poll command" (?) Windows driver does this
378 * and it is a good point to detect chip failure.
379 */
e9b351f6 380 ret = i2c_master_send(rx->c, sendbuf, 1);
69b1214c
JW
381 if (ret != 1) {
382 zilog_error("i2c_master_send failed with %d\n", ret);
383 if (failures >= 3) {
384 mutex_unlock(&ir->ir_lock);
385 zilog_error("unable to read from the IR chip "
386 "after 3 resets, giving up\n");
5bd6b046 387 break;
69b1214c
JW
388 }
389
390 /* Looks like the chip crashed, reset it */
391 zilog_error("polling the IR receiver chip failed, "
392 "trying reset\n");
393
394 set_current_state(TASK_UNINTERRUPTIBLE);
b757730b
AW
395 if (kthread_should_stop()) {
396 mutex_unlock(&ir->ir_lock);
5bd6b046
AW
397 ret = -ENODATA;
398 break;
b757730b 399 }
69b1214c 400 schedule_timeout((100 * HZ + 999) / 1000);
5bd6b046
AW
401 if (tx != NULL)
402 tx->need_boot = 1;
69b1214c
JW
403
404 ++failures;
405 mutex_unlock(&ir->ir_lock);
5bd6b046 406 ret = 0;
69b1214c
JW
407 continue;
408 }
409
b757730b
AW
410 if (kthread_should_stop()) {
411 mutex_unlock(&ir->ir_lock);
5bd6b046
AW
412 ret = -ENODATA;
413 break;
b757730b 414 }
e9b351f6 415 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
69b1214c
JW
416 mutex_unlock(&ir->ir_lock);
417 if (ret != sizeof(keybuf)) {
418 zilog_error("i2c_master_recv failed with %d -- "
419 "keeping last read buffer\n", ret);
420 } else {
06da95a3
AW
421 rx->b[0] = keybuf[3];
422 rx->b[1] = keybuf[4];
423 rx->b[2] = keybuf[5];
424 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
69b1214c
JW
425 }
426
427 /* key pressed ? */
06da95a3 428 if (rx->hdpvr_data_fmt) {
5bd6b046
AW
429 if (got_data && (keybuf[0] == 0x80)) {
430 ret = 0;
431 break;
432 } else if (got_data && (keybuf[0] == 0x00)) {
433 ret = -ENODATA;
434 break;
435 }
436 } else if ((rx->b[0] & 0x80) == 0) {
437 ret = got_data ? 0 : -ENODATA;
438 break;
439 }
69b1214c
JW
440
441 /* look what we have */
06da95a3 442 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
69b1214c
JW
443
444 codes[0] = (code >> 8) & 0xff;
445 codes[1] = code & 0xff;
446
447 /* return it */
bcbd1655 448 lirc_buffer_write(rbuf, codes);
69b1214c 449 ++got_data;
5bd6b046 450 ret = 0;
bcbd1655 451 } while (!lirc_buffer_full(rbuf));
69b1214c 452
d6dbd939 453 mutex_unlock(&rx->client_lock);
5bd6b046
AW
454 if (tx != NULL)
455 put_ir_tx(tx, false);
456 put_ir_rx(rx, false);
457 return ret;
69b1214c
JW
458}
459
460/*
461 * Main function of the polling thread -- from lirc_dev.
462 * We don't fit the LIRC model at all anymore. This is horrible, but
463 * basically we have a single RX/TX device with a nasty failure mode
464 * that needs to be accounted for across the pair. lirc lets us provide
465 * fops, but prevents us from using the internal polling, etc. if we do
466 * so. Hence the replication. Might be neater to extend the LIRC model
467 * to account for this but I'd think it's a very special case of seriously
468 * messed up hardware.
469 */
470static int lirc_thread(void *arg)
471{
472 struct IR *ir = arg;
bcbd1655 473 struct lirc_buffer *rbuf = ir->l.rbuf;
69b1214c 474
69b1214c
JW
475 dprintk("poll thread started\n");
476
b757730b 477 while (!kthread_should_stop()) {
b757730b 478 /* if device not opened, we can sleep half a second */
a3064855 479 if (atomic_read(&ir->open_count) == 0) {
69b1214c 480 schedule_timeout(HZ/2);
b757730b 481 continue;
69b1214c 482 }
69b1214c 483
5bd6b046
AW
484 set_current_state(TASK_INTERRUPTIBLE);
485
b757730b
AW
486 /*
487 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
488 * We use this interval as the chip resets every time you poll
489 * it (bad!). This is therefore just sufficient to catch all
490 * of the button presses. It makes the remote much more
491 * responsive. You can see the difference by running irw and
492 * holding down a button. With 100ms, the old polling
493 * interval, you'll notice breaks in the repeat sequence
494 * corresponding to lost keypresses.
495 */
496 schedule_timeout((260 * HZ) / 1000);
497 if (kthread_should_stop())
498 break;
499 if (!add_to_buf(ir))
bcbd1655 500 wake_up_interruptible(&rbuf->wait_poll);
b757730b 501 }
69b1214c
JW
502
503 dprintk("poll thread ended\n");
504 return 0;
505}
506
507static int set_use_inc(void *data)
508{
69b1214c
JW
509 return 0;
510}
511
512static void set_use_dec(void *data)
513{
9b28500a 514 return;
69b1214c
JW
515}
516
517/* safe read of a uint32 (always network byte order) */
518static int read_uint32(unsigned char **data,
519 unsigned char *endp, unsigned int *val)
520{
521 if (*data + 4 > endp)
522 return 0;
523 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
524 ((*data)[2] << 8) | (*data)[3];
525 *data += 4;
526 return 1;
527}
528
529/* safe read of a uint8 */
530static int read_uint8(unsigned char **data,
531 unsigned char *endp, unsigned char *val)
532{
533 if (*data + 1 > endp)
534 return 0;
535 *val = *((*data)++);
536 return 1;
537}
538
539/* safe skipping of N bytes */
540static int skip(unsigned char **data,
541 unsigned char *endp, unsigned int distance)
542{
543 if (*data + distance > endp)
544 return 0;
545 *data += distance;
546 return 1;
547}
548
549/* decompress key data into the given buffer */
550static int get_key_data(unsigned char *buf,
551 unsigned int codeset, unsigned int key)
552{
553 unsigned char *data, *endp, *diffs, *key_block;
554 unsigned char keys, ndiffs, id;
555 unsigned int base, lim, pos, i;
556
557 /* Binary search for the codeset */
558 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
559 pos = base + (lim >> 1);
560 data = tx_data->code_sets[pos];
561
562 if (!read_uint32(&data, tx_data->endp, &i))
563 goto corrupt;
564
565 if (i == codeset)
566 break;
567 else if (codeset > i) {
568 base = pos + 1;
569 --lim;
570 }
571 }
572 /* Not found? */
573 if (!lim)
574 return -EPROTO;
575
576 /* Set end of data block */
577 endp = pos < tx_data->num_code_sets - 1 ?
578 tx_data->code_sets[pos + 1] : tx_data->endp;
579
580 /* Read the block header */
581 if (!read_uint8(&data, endp, &keys) ||
582 !read_uint8(&data, endp, &ndiffs) ||
583 ndiffs > TX_BLOCK_SIZE || keys == 0)
584 goto corrupt;
585
586 /* Save diffs & skip */
587 diffs = data;
588 if (!skip(&data, endp, ndiffs))
589 goto corrupt;
590
591 /* Read the id of the first key */
592 if (!read_uint8(&data, endp, &id))
593 goto corrupt;
594
595 /* Unpack the first key's data */
596 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
597 if (tx_data->fixed[i] == -1) {
598 if (!read_uint8(&data, endp, &buf[i]))
599 goto corrupt;
600 } else {
601 buf[i] = (unsigned char)tx_data->fixed[i];
602 }
603 }
604
605 /* Early out key found/not found */
606 if (key == id)
607 return 0;
608 if (keys == 1)
609 return -EPROTO;
610
611 /* Sanity check */
612 key_block = data;
613 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
614 goto corrupt;
615
616 /* Binary search for the key */
617 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
618 /* Seek to block */
619 unsigned char *key_data;
620 pos = base + (lim >> 1);
621 key_data = key_block + (ndiffs + 1) * pos;
622
623 if (*key_data == key) {
624 /* skip key id */
625 ++key_data;
626
627 /* found, so unpack the diffs */
628 for (i = 0; i < ndiffs; ++i) {
629 unsigned char val;
630 if (!read_uint8(&key_data, endp, &val) ||
631 diffs[i] >= TX_BLOCK_SIZE)
632 goto corrupt;
633 buf[diffs[i]] = val;
634 }
635
636 return 0;
637 } else if (key > *key_data) {
638 base = pos + 1;
639 --lim;
640 }
641 }
642 /* Key not found */
643 return -EPROTO;
644
645corrupt:
646 zilog_error("firmware is corrupt\n");
647 return -EFAULT;
648}
649
650/* send a block of data to the IR TX device */
06da95a3 651static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
69b1214c
JW
652{
653 int i, j, ret;
654 unsigned char buf[5];
655
656 for (i = 0; i < TX_BLOCK_SIZE;) {
657 int tosend = TX_BLOCK_SIZE - i;
658 if (tosend > 4)
659 tosend = 4;
660 buf[0] = (unsigned char)(i + 1);
661 for (j = 0; j < tosend; ++j)
662 buf[1 + j] = data_block[i + j];
663 dprintk("%02x %02x %02x %02x %02x",
664 buf[0], buf[1], buf[2], buf[3], buf[4]);
e9b351f6 665 ret = i2c_master_send(tx->c, buf, tosend + 1);
69b1214c
JW
666 if (ret != tosend + 1) {
667 zilog_error("i2c_master_send failed with %d\n", ret);
668 return ret < 0 ? ret : -EFAULT;
669 }
670 i += tosend;
671 }
672 return 0;
673}
674
675/* send boot data to the IR TX device */
06da95a3 676static int send_boot_data(struct IR_tx *tx)
69b1214c 677{
5766d204 678 int ret, i;
69b1214c
JW
679 unsigned char buf[4];
680
681 /* send the boot block */
06da95a3 682 ret = send_data_block(tx, tx_data->boot_data);
69b1214c
JW
683 if (ret != 0)
684 return ret;
685
5766d204 686 /* Hit the go button to activate the new boot data */
69b1214c
JW
687 buf[0] = 0x00;
688 buf[1] = 0x20;
e9b351f6 689 ret = i2c_master_send(tx->c, buf, 2);
69b1214c
JW
690 if (ret != 2) {
691 zilog_error("i2c_master_send failed with %d\n", ret);
692 return ret < 0 ? ret : -EFAULT;
693 }
5766d204
JW
694
695 /*
696 * Wait for zilog to settle after hitting go post boot block upload.
697 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
698 * upon attempting to get firmware revision, and tx probe thus fails.
699 */
700 for (i = 0; i < 10; i++) {
701 ret = i2c_master_send(tx->c, buf, 1);
702 if (ret == 1)
703 break;
704 udelay(100);
705 }
706
69b1214c
JW
707 if (ret != 1) {
708 zilog_error("i2c_master_send failed with %d\n", ret);
709 return ret < 0 ? ret : -EFAULT;
710 }
711
712 /* Here comes the firmware version... (hopefully) */
e9b351f6 713 ret = i2c_master_recv(tx->c, buf, 4);
69b1214c
JW
714 if (ret != 4) {
715 zilog_error("i2c_master_recv failed with %d\n", ret);
716 return 0;
717 }
5766d204
JW
718 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
719 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
69b1214c
JW
720 return 0;
721 }
722 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
723 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
724
725 return 0;
726}
727
728/* unload "firmware", lock held */
729static void fw_unload_locked(void)
730{
731 if (tx_data) {
732 if (tx_data->code_sets)
733 vfree(tx_data->code_sets);
734
735 if (tx_data->datap)
736 vfree(tx_data->datap);
737
738 vfree(tx_data);
739 tx_data = NULL;
740 dprintk("successfully unloaded IR blaster firmware\n");
741 }
742}
743
744/* unload "firmware" for the IR TX device */
745static void fw_unload(void)
746{
747 mutex_lock(&tx_data_lock);
748 fw_unload_locked();
749 mutex_unlock(&tx_data_lock);
750}
751
752/* load "firmware" for the IR TX device */
06da95a3 753static int fw_load(struct IR_tx *tx)
69b1214c
JW
754{
755 int ret;
756 unsigned int i;
757 unsigned char *data, version, num_global_fixed;
758 const struct firmware *fw_entry;
759
760 /* Already loaded? */
761 mutex_lock(&tx_data_lock);
762 if (tx_data) {
763 ret = 0;
764 goto out;
765 }
766
767 /* Request codeset data file */
5bd6b046 768 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", tx->ir->l.dev);
69b1214c
JW
769 if (ret != 0) {
770 zilog_error("firmware haup-ir-blaster.bin not available "
771 "(%d)\n", ret);
772 ret = ret < 0 ? ret : -EFAULT;
773 goto out;
774 }
775 dprintk("firmware of size %zu loaded\n", fw_entry->size);
776
777 /* Parse the file */
778 tx_data = vmalloc(sizeof(*tx_data));
779 if (tx_data == NULL) {
780 zilog_error("out of memory\n");
781 release_firmware(fw_entry);
782 ret = -ENOMEM;
783 goto out;
784 }
785 tx_data->code_sets = NULL;
786
787 /* Copy the data so hotplug doesn't get confused and timeout */
788 tx_data->datap = vmalloc(fw_entry->size);
789 if (tx_data->datap == NULL) {
790 zilog_error("out of memory\n");
791 release_firmware(fw_entry);
792 vfree(tx_data);
793 ret = -ENOMEM;
794 goto out;
795 }
796 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
797 tx_data->endp = tx_data->datap + fw_entry->size;
798 release_firmware(fw_entry); fw_entry = NULL;
799
800 /* Check version */
801 data = tx_data->datap;
802 if (!read_uint8(&data, tx_data->endp, &version))
803 goto corrupt;
804 if (version != 1) {
805 zilog_error("unsupported code set file version (%u, expected"
806 "1) -- please upgrade to a newer driver",
807 version);
808 fw_unload_locked();
809 ret = -EFAULT;
810 goto out;
811 }
812
813 /* Save boot block for later */
814 tx_data->boot_data = data;
815 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
816 goto corrupt;
817
818 if (!read_uint32(&data, tx_data->endp,
819 &tx_data->num_code_sets))
820 goto corrupt;
821
822 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
823
824 tx_data->code_sets = vmalloc(
825 tx_data->num_code_sets * sizeof(char *));
826 if (tx_data->code_sets == NULL) {
827 fw_unload_locked();
828 ret = -ENOMEM;
829 goto out;
830 }
831
832 for (i = 0; i < TX_BLOCK_SIZE; ++i)
833 tx_data->fixed[i] = -1;
834
835 /* Read global fixed data template */
836 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
837 num_global_fixed > TX_BLOCK_SIZE)
838 goto corrupt;
839 for (i = 0; i < num_global_fixed; ++i) {
840 unsigned char pos, val;
841 if (!read_uint8(&data, tx_data->endp, &pos) ||
842 !read_uint8(&data, tx_data->endp, &val) ||
843 pos >= TX_BLOCK_SIZE)
844 goto corrupt;
845 tx_data->fixed[pos] = (int)val;
846 }
847
848 /* Filch out the position of each code set */
849 for (i = 0; i < tx_data->num_code_sets; ++i) {
850 unsigned int id;
851 unsigned char keys;
852 unsigned char ndiffs;
853
854 /* Save the codeset position */
855 tx_data->code_sets[i] = data;
856
857 /* Read header */
858 if (!read_uint32(&data, tx_data->endp, &id) ||
859 !read_uint8(&data, tx_data->endp, &keys) ||
860 !read_uint8(&data, tx_data->endp, &ndiffs) ||
861 ndiffs > TX_BLOCK_SIZE || keys == 0)
862 goto corrupt;
863
864 /* skip diff positions */
865 if (!skip(&data, tx_data->endp, ndiffs))
866 goto corrupt;
867
868 /*
869 * After the diffs we have the first key id + data -
870 * global fixed
871 */
872 if (!skip(&data, tx_data->endp,
873 1 + TX_BLOCK_SIZE - num_global_fixed))
874 goto corrupt;
875
876 /* Then we have keys-1 blocks of key id+diffs */
877 if (!skip(&data, tx_data->endp,
878 (ndiffs + 1) * (keys - 1)))
879 goto corrupt;
880 }
881 ret = 0;
882 goto out;
883
884corrupt:
885 zilog_error("firmware is corrupt\n");
886 fw_unload_locked();
887 ret = -EFAULT;
888
889out:
890 mutex_unlock(&tx_data_lock);
891 return ret;
892}
893
69b1214c
JW
894/* copied from lirc_dev */
895static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
896{
e0ac7da0 897 struct IR *ir = filep->private_data;
5bd6b046 898 struct IR_rx *rx;
bcbd1655 899 struct lirc_buffer *rbuf = ir->l.rbuf;
69b1214c 900 int ret = 0, written = 0;
8152b760 901 unsigned int m;
69b1214c
JW
902 DECLARE_WAITQUEUE(wait, current);
903
904 dprintk("read called\n");
bcbd1655 905 if (n % rbuf->chunk_size) {
69b1214c 906 dprintk("read result = -EINVAL\n");
69b1214c
JW
907 return -EINVAL;
908 }
909
5bd6b046
AW
910 rx = get_ir_rx(ir);
911 if (rx == NULL)
912 return -ENXIO;
913
69b1214c
JW
914 /*
915 * we add ourselves to the task queue before buffer check
916 * to avoid losing scan code (in case when queue is awaken somewhere
917 * between while condition checking and scheduling)
918 */
bcbd1655 919 add_wait_queue(&rbuf->wait_poll, &wait);
69b1214c
JW
920 set_current_state(TASK_INTERRUPTIBLE);
921
922 /*
923 * while we didn't provide 'length' bytes, device is opened in blocking
924 * mode and 'copy_to_user' is happy, wait for data.
925 */
926 while (written < n && ret == 0) {
bcbd1655 927 if (lirc_buffer_empty(rbuf)) {
69b1214c
JW
928 /*
929 * According to the read(2) man page, 'written' can be
930 * returned as less than 'n', instead of blocking
931 * again, returning -EWOULDBLOCK, or returning
932 * -ERESTARTSYS
933 */
934 if (written)
935 break;
936 if (filep->f_flags & O_NONBLOCK) {
937 ret = -EWOULDBLOCK;
938 break;
939 }
940 if (signal_pending(current)) {
941 ret = -ERESTARTSYS;
942 break;
943 }
944 schedule();
945 set_current_state(TASK_INTERRUPTIBLE);
946 } else {
bcbd1655
AW
947 unsigned char buf[rbuf->chunk_size];
948 m = lirc_buffer_read(rbuf, buf);
949 if (m == rbuf->chunk_size) {
8152b760 950 ret = copy_to_user((void *)outbuf+written, buf,
bcbd1655
AW
951 rbuf->chunk_size);
952 written += rbuf->chunk_size;
8152b760 953 }
69b1214c
JW
954 }
955 }
956
bcbd1655 957 remove_wait_queue(&rbuf->wait_poll, &wait);
5bd6b046 958 put_ir_rx(rx, false);
69b1214c 959 set_current_state(TASK_RUNNING);
69b1214c 960
8152b760 961 dprintk("read result = %d (%s)\n", ret, ret ? "Error" : "OK");
69b1214c
JW
962
963 return ret ? ret : written;
964}
965
966/* send a keypress to the IR TX device */
06da95a3 967static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
69b1214c
JW
968{
969 unsigned char data_block[TX_BLOCK_SIZE];
970 unsigned char buf[2];
971 int i, ret;
972
973 /* Get data for the codeset/key */
974 ret = get_key_data(data_block, code, key);
975
976 if (ret == -EPROTO) {
977 zilog_error("failed to get data for code %u, key %u -- check "
978 "lircd.conf entries\n", code, key);
979 return ret;
980 } else if (ret != 0)
981 return ret;
982
983 /* Send the data block */
06da95a3 984 ret = send_data_block(tx, data_block);
69b1214c
JW
985 if (ret != 0)
986 return ret;
987
988 /* Send data block length? */
989 buf[0] = 0x00;
990 buf[1] = 0x40;
e9b351f6 991 ret = i2c_master_send(tx->c, buf, 2);
69b1214c
JW
992 if (ret != 2) {
993 zilog_error("i2c_master_send failed with %d\n", ret);
994 return ret < 0 ? ret : -EFAULT;
995 }
5766d204
JW
996
997 /* Give the z8 a moment to process data block */
998 for (i = 0; i < 10; i++) {
999 ret = i2c_master_send(tx->c, buf, 1);
1000 if (ret == 1)
1001 break;
1002 udelay(100);
1003 }
1004
69b1214c
JW
1005 if (ret != 1) {
1006 zilog_error("i2c_master_send failed with %d\n", ret);
1007 return ret < 0 ? ret : -EFAULT;
1008 }
1009
1010 /* Send finished download? */
e9b351f6 1011 ret = i2c_master_recv(tx->c, buf, 1);
69b1214c
JW
1012 if (ret != 1) {
1013 zilog_error("i2c_master_recv failed with %d\n", ret);
1014 return ret < 0 ? ret : -EFAULT;
1015 }
1016 if (buf[0] != 0xA0) {
1017 zilog_error("unexpected IR TX response #1: %02x\n",
1018 buf[0]);
1019 return -EFAULT;
1020 }
1021
1022 /* Send prepare command? */
1023 buf[0] = 0x00;
1024 buf[1] = 0x80;
e9b351f6 1025 ret = i2c_master_send(tx->c, buf, 2);
69b1214c
JW
1026 if (ret != 2) {
1027 zilog_error("i2c_master_send failed with %d\n", ret);
1028 return ret < 0 ? ret : -EFAULT;
1029 }
1030
69b1214c
JW
1031 /*
1032 * The sleep bits aren't necessary on the HD PVR, and in fact, the
1033 * last i2c_master_recv always fails with a -5, so for now, we're
1034 * going to skip this whole mess and say we're done on the HD PVR
1035 */
06da95a3 1036 if (!tx->post_tx_ready_poll) {
d7c72356
AW
1037 dprintk("sent code %u, key %u\n", code, key);
1038 return 0;
1039 }
69b1214c
JW
1040
1041 /*
1042 * This bit NAKs until the device is ready, so we retry it
1043 * sleeping a bit each time. This seems to be what the windows
1044 * driver does, approximately.
1045 * Try for up to 1s.
1046 */
1047 for (i = 0; i < 20; ++i) {
1048 set_current_state(TASK_UNINTERRUPTIBLE);
1049 schedule_timeout((50 * HZ + 999) / 1000);
e9b351f6 1050 ret = i2c_master_send(tx->c, buf, 1);
69b1214c
JW
1051 if (ret == 1)
1052 break;
1053 dprintk("NAK expected: i2c_master_send "
1054 "failed with %d (try %d)\n", ret, i+1);
1055 }
1056 if (ret != 1) {
1057 zilog_error("IR TX chip never got ready: last i2c_master_send "
1058 "failed with %d\n", ret);
1059 return ret < 0 ? ret : -EFAULT;
1060 }
1061
1062 /* Seems to be an 'ok' response */
e9b351f6 1063 i = i2c_master_recv(tx->c, buf, 1);
69b1214c
JW
1064 if (i != 1) {
1065 zilog_error("i2c_master_recv failed with %d\n", ret);
1066 return -EFAULT;
1067 }
1068 if (buf[0] != 0x80) {
1069 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
1070 return -EFAULT;
1071 }
1072
69b1214c
JW
1073 /* Oh good, it worked */
1074 dprintk("sent code %u, key %u\n", code, key);
1075 return 0;
1076}
1077
1078/*
1079 * Write a code to the device. We take in a 32-bit number (an int) and then
1080 * decode this to a codeset/key index. The key data is then decompressed and
1081 * sent to the device. We have a spin lock as per i2c documentation to prevent
1082 * multiple concurrent sends which would probably cause the device to explode.
1083 */
1084static ssize_t write(struct file *filep, const char *buf, size_t n,
1085 loff_t *ppos)
1086{
e0ac7da0 1087 struct IR *ir = filep->private_data;
5bd6b046 1088 struct IR_tx *tx;
69b1214c
JW
1089 size_t i;
1090 int failures = 0;
1091
69b1214c
JW
1092 /* Validate user parameters */
1093 if (n % sizeof(int))
1094 return -EINVAL;
1095
5bd6b046
AW
1096 /* Get a struct IR_tx reference */
1097 tx = get_ir_tx(ir);
1098 if (tx == NULL)
1099 return -ENXIO;
1100
d6dbd939
AW
1101 /* Ensure our tx->c i2c_client remains valid for the duration */
1102 mutex_lock(&tx->client_lock);
1103 if (tx->c == NULL) {
1104 mutex_unlock(&tx->client_lock);
1105 put_ir_tx(tx, false);
1106 return -ENXIO;
1107 }
1108
69b1214c
JW
1109 /* Lock i2c bus for the duration */
1110 mutex_lock(&ir->ir_lock);
1111
1112 /* Send each keypress */
1113 for (i = 0; i < n;) {
1114 int ret = 0;
1115 int command;
1116
1117 if (copy_from_user(&command, buf + i, sizeof(command))) {
1118 mutex_unlock(&ir->ir_lock);
d6dbd939 1119 mutex_unlock(&tx->client_lock);
5bd6b046 1120 put_ir_tx(tx, false);
69b1214c
JW
1121 return -EFAULT;
1122 }
1123
1124 /* Send boot data first if required */
06da95a3 1125 if (tx->need_boot == 1) {
5bd6b046
AW
1126 /* Make sure we have the 'firmware' loaded, first */
1127 ret = fw_load(tx);
1128 if (ret != 0) {
1129 mutex_unlock(&ir->ir_lock);
d6dbd939 1130 mutex_unlock(&tx->client_lock);
5bd6b046
AW
1131 put_ir_tx(tx, false);
1132 if (ret != -ENOMEM)
1133 ret = -EIO;
1134 return ret;
1135 }
1136 /* Prep the chip for transmitting codes */
06da95a3 1137 ret = send_boot_data(tx);
69b1214c 1138 if (ret == 0)
06da95a3 1139 tx->need_boot = 0;
69b1214c
JW
1140 }
1141
1142 /* Send the code */
1143 if (ret == 0) {
06da95a3 1144 ret = send_code(tx, (unsigned)command >> 16,
69b1214c
JW
1145 (unsigned)command & 0xFFFF);
1146 if (ret == -EPROTO) {
1147 mutex_unlock(&ir->ir_lock);
d6dbd939 1148 mutex_unlock(&tx->client_lock);
5bd6b046 1149 put_ir_tx(tx, false);
69b1214c
JW
1150 return ret;
1151 }
1152 }
1153
1154 /*
1155 * Hmm, a failure. If we've had a few then give up, otherwise
1156 * try a reset
1157 */
1158 if (ret != 0) {
1159 /* Looks like the chip crashed, reset it */
1160 zilog_error("sending to the IR transmitter chip "
1161 "failed, trying reset\n");
1162
1163 if (failures >= 3) {
1164 zilog_error("unable to send to the IR chip "
1165 "after 3 resets, giving up\n");
1166 mutex_unlock(&ir->ir_lock);
d6dbd939 1167 mutex_unlock(&tx->client_lock);
5bd6b046 1168 put_ir_tx(tx, false);
69b1214c
JW
1169 return ret;
1170 }
1171 set_current_state(TASK_UNINTERRUPTIBLE);
1172 schedule_timeout((100 * HZ + 999) / 1000);
06da95a3 1173 tx->need_boot = 1;
69b1214c
JW
1174 ++failures;
1175 } else
1176 i += sizeof(int);
1177 }
1178
1179 /* Release i2c bus */
1180 mutex_unlock(&ir->ir_lock);
1181
d6dbd939
AW
1182 mutex_unlock(&tx->client_lock);
1183
5bd6b046
AW
1184 /* Give back our struct IR_tx reference */
1185 put_ir_tx(tx, false);
1186
69b1214c
JW
1187 /* All looks good */
1188 return n;
1189}
1190
1191/* copied from lirc_dev */
1192static unsigned int poll(struct file *filep, poll_table *wait)
1193{
e0ac7da0 1194 struct IR *ir = filep->private_data;
5bd6b046 1195 struct IR_rx *rx;
bcbd1655 1196 struct lirc_buffer *rbuf = ir->l.rbuf;
69b1214c
JW
1197 unsigned int ret;
1198
1199 dprintk("poll called\n");
69b1214c 1200
5bd6b046 1201 rx = get_ir_rx(ir);
915e5473
AW
1202 if (rx == NULL) {
1203 /*
1204 * Revisit this, if our poll function ever reports writeable
1205 * status for Tx
1206 */
1207 dprintk("poll result = POLLERR\n");
1208 return POLLERR;
1209 }
69b1214c 1210
915e5473
AW
1211 /*
1212 * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
1213 * that buffer's wait queue indicates we may have a new poll status.
1214 */
bcbd1655 1215 poll_wait(filep, &rbuf->wait_poll, wait);
69b1214c 1216
915e5473 1217 /* Indicate what ops could happen immediately without blocking */
bcbd1655 1218 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN|POLLRDNORM);
69b1214c 1219
915e5473 1220 dprintk("poll result = %s\n", ret ? "POLLIN|POLLRDNORM" : "none");
69b1214c
JW
1221 return ret;
1222}
1223
1224static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1225{
e0ac7da0 1226 struct IR *ir = filep->private_data;
69b1214c 1227 int result;
5bd6b046 1228 unsigned long mode, features;
69b1214c 1229
5bd6b046 1230 features = ir->l.features;
69b1214c
JW
1231
1232 switch (cmd) {
1233 case LIRC_GET_LENGTH:
1234 result = put_user((unsigned long)13,
1235 (unsigned long *)arg);
1236 break;
1237 case LIRC_GET_FEATURES:
1238 result = put_user(features, (unsigned long *) arg);
1239 break;
1240 case LIRC_GET_REC_MODE:
1241 if (!(features&LIRC_CAN_REC_MASK))
1242 return -ENOSYS;
1243
1244 result = put_user(LIRC_REC2MODE
1245 (features&LIRC_CAN_REC_MASK),
1246 (unsigned long *)arg);
1247 break;
1248 case LIRC_SET_REC_MODE:
1249 if (!(features&LIRC_CAN_REC_MASK))
1250 return -ENOSYS;
1251
1252 result = get_user(mode, (unsigned long *)arg);
1253 if (!result && !(LIRC_MODE2REC(mode) & features))
1254 result = -EINVAL;
1255 break;
1256 case LIRC_GET_SEND_MODE:
12d896e1
AW
1257 if (!(features&LIRC_CAN_SEND_MASK))
1258 return -ENOSYS;
1259
69b1214c
JW
1260 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1261 break;
1262 case LIRC_SET_SEND_MODE:
12d896e1
AW
1263 if (!(features&LIRC_CAN_SEND_MASK))
1264 return -ENOSYS;
1265
69b1214c
JW
1266 result = get_user(mode, (unsigned long *) arg);
1267 if (!result && mode != LIRC_MODE_PULSE)
1268 return -EINVAL;
1269 break;
1270 default:
1271 return -EINVAL;
1272 }
1273 return result;
1274}
1275
5bd6b046 1276static struct IR *get_ir_device_by_minor(unsigned int minor)
a68a9b73 1277{
5c07134f 1278 struct IR *ir;
5bd6b046 1279 struct IR *ret = NULL;
5c07134f 1280
5bd6b046 1281 mutex_lock(&ir_devices_lock);
a68a9b73 1282
5bd6b046
AW
1283 if (!list_empty(&ir_devices_list)) {
1284 list_for_each_entry(ir, &ir_devices_list, list) {
1285 if (ir->l.minor == minor) {
1286 ret = get_ir_device(ir, true);
1287 break;
1288 }
1289 }
1290 }
5c07134f 1291
5bd6b046
AW
1292 mutex_unlock(&ir_devices_lock);
1293 return ret;
a68a9b73
AW
1294}
1295
69b1214c
JW
1296/*
1297 * Open the IR device. Get hold of our IR structure and
1298 * stash it in private_data for the file
1299 */
1300static int open(struct inode *node, struct file *filep)
1301{
1302 struct IR *ir;
a68a9b73 1303 unsigned int minor = MINOR(node->i_rdev);
69b1214c
JW
1304
1305 /* find our IR struct */
5bd6b046 1306 ir = get_ir_device_by_minor(minor);
a68a9b73
AW
1307
1308 if (ir == NULL)
69b1214c 1309 return -ENODEV;
69b1214c 1310
a3064855 1311 atomic_inc(&ir->open_count);
69b1214c
JW
1312
1313 /* stash our IR struct */
1314 filep->private_data = ir;
1315
9a55a2b3 1316 nonseekable_open(node, filep);
69b1214c
JW
1317 return 0;
1318}
1319
1320/* Close the IR device */
1321static int close(struct inode *node, struct file *filep)
1322{
1323 /* find our IR struct */
e0ac7da0 1324 struct IR *ir = filep->private_data;
69b1214c
JW
1325 if (ir == NULL) {
1326 zilog_error("close: no private_data attached to the file!\n");
1327 return -ENODEV;
1328 }
1329
a3064855 1330 atomic_dec(&ir->open_count);
69b1214c 1331
5bd6b046 1332 put_ir_device(ir, false);
69b1214c
JW
1333 return 0;
1334}
1335
69b1214c
JW
1336static int ir_remove(struct i2c_client *client);
1337static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
69b1214c 1338
d7c72356
AW
1339#define ID_FLAG_TX 0x01
1340#define ID_FLAG_HDPVR 0x02
1341
69b1214c 1342static const struct i2c_device_id ir_transceiver_id[] = {
d7c72356
AW
1343 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1344 { "ir_rx_z8f0811_haup", 0 },
1345 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1346 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
69b1214c
JW
1347 { }
1348};
1349
1350static struct i2c_driver driver = {
1351 .driver = {
1352 .owner = THIS_MODULE,
1353 .name = "Zilog/Hauppauge i2c IR",
1354 },
1355 .probe = ir_probe,
1356 .remove = ir_remove,
69b1214c
JW
1357 .id_table = ir_transceiver_id,
1358};
1359
0f9313ad 1360static const struct file_operations lirc_fops = {
69b1214c 1361 .owner = THIS_MODULE,
9a55a2b3 1362 .llseek = no_llseek,
69b1214c
JW
1363 .read = read,
1364 .write = write,
1365 .poll = poll,
1366 .unlocked_ioctl = ioctl,
8be292cc
JW
1367#ifdef CONFIG_COMPAT
1368 .compat_ioctl = ioctl,
1369#endif
69b1214c
JW
1370 .open = open,
1371 .release = close
1372};
1373
534c1eab
AW
1374static struct lirc_driver lirc_template = {
1375 .name = "lirc_zilog",
1376 .minor = -1,
1377 .code_length = 13,
1378 .buffer_size = BUFLEN / 2,
1379 .sample_rate = 0, /* tell lirc_dev to not start its own kthread */
1380 .chunk_size = 2,
1381 .set_use_inc = set_use_inc,
1382 .set_use_dec = set_use_dec,
1383 .fops = &lirc_fops,
1384 .owner = THIS_MODULE,
1385};
1386
a68a9b73
AW
1387static int ir_remove(struct i2c_client *client)
1388{
5bd6b046
AW
1389 if (strncmp("ir_tx_z8", client->name, 8) == 0) {
1390 struct IR_tx *tx = i2c_get_clientdata(client);
d6dbd939
AW
1391 if (tx != NULL) {
1392 mutex_lock(&tx->client_lock);
1393 tx->c = NULL;
1394 mutex_unlock(&tx->client_lock);
5bd6b046 1395 put_ir_tx(tx, false);
d6dbd939 1396 }
5bd6b046
AW
1397 } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
1398 struct IR_rx *rx = i2c_get_clientdata(client);
d6dbd939
AW
1399 if (rx != NULL) {
1400 mutex_lock(&rx->client_lock);
1401 rx->c = NULL;
1402 mutex_unlock(&rx->client_lock);
5bd6b046 1403 put_ir_rx(rx, false);
d6dbd939 1404 }
69b1214c 1405 }
69b1214c
JW
1406 return 0;
1407}
1408
a68a9b73
AW
1409
1410/* ir_devices_lock must be held */
5bd6b046 1411static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
69b1214c 1412{
5c07134f 1413 struct IR *ir;
a68a9b73 1414
5c07134f
AW
1415 if (list_empty(&ir_devices_list))
1416 return NULL;
1417
1418 list_for_each_entry(ir, &ir_devices_list, list)
5bd6b046
AW
1419 if (ir->adapter == adapter) {
1420 get_ir_device(ir, true);
5c07134f 1421 return ir;
5bd6b046 1422 }
a68a9b73 1423
5c07134f 1424 return NULL;
a68a9b73
AW
1425}
1426
1427static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1428{
1429 struct IR *ir;
5bd6b046
AW
1430 struct IR_tx *tx;
1431 struct IR_rx *rx;
69b1214c 1432 struct i2c_adapter *adap = client->adapter;
69b1214c 1433 int ret;
a68a9b73 1434 bool tx_probe = false;
69b1214c 1435
86e52428
AW
1436 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1437 __func__, id->name, adap->nr, adap->name, client->addr);
69b1214c 1438
d7c72356 1439 /*
a68a9b73
AW
1440 * The IR receiver is at i2c address 0x71.
1441 * The IR transmitter is at i2c address 0x70.
69b1214c 1442 */
69b1214c 1443
a68a9b73
AW
1444 if (id->driver_data & ID_FLAG_TX)
1445 tx_probe = true;
1446 else if (tx_only) /* module option */
e9b351f6 1447 return -ENXIO;
69b1214c 1448
b757730b
AW
1449 zilog_info("probing IR %s on %s (i2c-%d)\n",
1450 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
69b1214c 1451
a68a9b73 1452 mutex_lock(&ir_devices_lock);
69b1214c 1453
a68a9b73 1454 /* Use a single struct IR instance for both the Rx and Tx functions */
5bd6b046 1455 ir = get_ir_device_by_adapter(adap);
a68a9b73
AW
1456 if (ir == NULL) {
1457 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1458 if (ir == NULL) {
1459 ret = -ENOMEM;
1460 goto out_no_ir;
06da95a3 1461 }
5bd6b046
AW
1462 kref_init(&ir->ref);
1463
a68a9b73 1464 /* store for use in ir_probe() again, and open() later on */
5c07134f 1465 INIT_LIST_HEAD(&ir->list);
5bd6b046 1466 list_add_tail(&ir->list, &ir_devices_list);
a68a9b73
AW
1467
1468 ir->adapter = adap;
1469 mutex_init(&ir->ir_lock);
a3064855 1470 atomic_set(&ir->open_count, 0);
5bd6b046
AW
1471 spin_lock_init(&ir->tx_ref_lock);
1472 spin_lock_init(&ir->rx_ref_lock);
a68a9b73
AW
1473
1474 /* set lirc_dev stuff */
1475 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
5bd6b046
AW
1476 /*
1477 * FIXME this is a pointer reference to us, but no refcount.
1478 *
1479 * This OK for now, since lirc_dev currently won't touch this
1480 * buffer as we provide our own lirc_fops.
1481 *
1482 * Currently our own lirc_fops rely on this ir->l.rbuf pointer
1483 */
1484 ir->l.rbuf = &ir->rbuf;
1485 ir->l.dev = &adap->dev;
bcbd1655
AW
1486 ret = lirc_buffer_init(ir->l.rbuf,
1487 ir->l.chunk_size, ir->l.buffer_size);
1488 if (ret)
5bd6b046 1489 goto out_put_ir;
06da95a3
AW
1490 }
1491
a68a9b73 1492 if (tx_probe) {
5bd6b046
AW
1493 /* Get the IR_rx instance for later, if already allocated */
1494 rx = get_ir_rx(ir);
1495
a68a9b73 1496 /* Set up a struct IR_tx instance */
5bd6b046
AW
1497 tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1498 if (tx == NULL) {
a68a9b73 1499 ret = -ENOMEM;
5bd6b046 1500 goto out_put_xx;
a68a9b73 1501 }
5bd6b046
AW
1502 kref_init(&tx->ref);
1503 ir->tx = tx;
06da95a3 1504
534c1eab 1505 ir->l.features |= LIRC_CAN_SEND_PULSE;
d6dbd939 1506 mutex_init(&tx->client_lock);
5bd6b046
AW
1507 tx->c = client;
1508 tx->need_boot = 1;
1509 tx->post_tx_ready_poll =
a68a9b73 1510 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
5bd6b046
AW
1511
1512 /* An ir ref goes to the struct IR_tx instance */
1513 tx->ir = get_ir_device(ir, true);
1514
1515 /* A tx ref goes to the i2c_client */
1516 i2c_set_clientdata(client, get_ir_tx(ir));
1517
1518 /*
1519 * Load the 'firmware'. We do this before registering with
1520 * lirc_dev, so the first firmware load attempt does not happen
1521 * after a open() or write() call on the device.
1522 *
1523 * Failure here is not deemed catastrophic, so the receiver will
1524 * still be usable. Firmware load will be retried in write(),
1525 * if it is needed.
1526 */
1527 fw_load(tx);
1528
1529 /* Proceed only if the Rx client is also ready or not needed */
1530 if (rx == NULL && !tx_only) {
1531 zilog_info("probe of IR Tx on %s (i2c-%d) done. Waiting"
1532 " on IR Rx.\n", adap->name, adap->nr);
1533 goto out_ok;
1534 }
a68a9b73 1535 } else {
5bd6b046
AW
1536 /* Get the IR_tx instance for later, if already allocated */
1537 tx = get_ir_tx(ir);
1538
a68a9b73 1539 /* Set up a struct IR_rx instance */
5bd6b046
AW
1540 rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1541 if (rx == NULL) {
06da95a3 1542 ret = -ENOMEM;
5bd6b046 1543 goto out_put_xx;
06da95a3 1544 }
5bd6b046
AW
1545 kref_init(&rx->ref);
1546 ir->rx = rx;
06da95a3 1547
534c1eab 1548 ir->l.features |= LIRC_CAN_REC_LIRCCODE;
d6dbd939 1549 mutex_init(&rx->client_lock);
5bd6b046
AW
1550 rx->c = client;
1551 rx->hdpvr_data_fmt =
a68a9b73 1552 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
69b1214c 1553
5bd6b046
AW
1554 /* An ir ref goes to the struct IR_rx instance */
1555 rx->ir = get_ir_device(ir, true);
69b1214c 1556
5bd6b046
AW
1557 /* An rx ref goes to the i2c_client */
1558 i2c_set_clientdata(client, get_ir_rx(ir));
a68a9b73 1559
5bd6b046
AW
1560 /*
1561 * Start the polling thread.
1562 * It will only perform an empty loop around schedule_timeout()
1563 * until we register with lirc_dev and the first user open()
1564 */
1565 /* An ir ref goes to the new rx polling kthread */
1566 rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
1567 "zilog-rx-i2c-%d", adap->nr);
1568 if (IS_ERR(rx->task)) {
1569 ret = PTR_ERR(rx->task);
a68a9b73
AW
1570 zilog_error("%s: could not start IR Rx polling thread"
1571 "\n", __func__);
5bd6b046
AW
1572 /* Failed kthread, so put back the ir ref */
1573 put_ir_device(ir, true);
1574 /* Failure exit, so put back rx ref from i2c_client */
1575 i2c_set_clientdata(client, NULL);
1576 put_ir_rx(rx, true);
1577 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
1578 goto out_put_xx;
1579 }
1580
1581 /* Proceed only if the Tx client is also ready */
1582 if (tx == NULL) {
1583 zilog_info("probe of IR Rx on %s (i2c-%d) done. Waiting"
1584 " on IR Tx.\n", adap->name, adap->nr);
1585 goto out_ok;
69b1214c 1586 }
69b1214c
JW
1587 }
1588
69b1214c 1589 /* register with lirc */
5bd6b046 1590 ir->l.minor = minor; /* module option: user requested minor number */
69b1214c
JW
1591 ir->l.minor = lirc_register_driver(&ir->l);
1592 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
a68a9b73
AW
1593 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1594 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
69b1214c 1595 ret = -EBADRQC;
5bd6b046 1596 goto out_put_xx;
12d896e1 1597 }
4933fc9d
AW
1598 zilog_info("IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
1599 adap->name, adap->nr, ir->l.minor);
69b1214c 1600
5bd6b046
AW
1601out_ok:
1602 if (rx != NULL)
1603 put_ir_rx(rx, true);
1604 if (tx != NULL)
1605 put_ir_tx(tx, true);
1606 put_ir_device(ir, true);
4933fc9d 1607 zilog_info("probe of IR %s on %s (i2c-%d) done\n",
b757730b 1608 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
a68a9b73 1609 mutex_unlock(&ir_devices_lock);
69b1214c
JW
1610 return 0;
1611
5bd6b046
AW
1612out_put_xx:
1613 if (rx != NULL)
1614 put_ir_rx(rx, true);
1615 if (tx != NULL)
1616 put_ir_tx(tx, true);
1617out_put_ir:
1618 put_ir_device(ir, true);
a68a9b73
AW
1619out_no_ir:
1620 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1621 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1622 ret);
1623 mutex_unlock(&ir_devices_lock);
1624 return ret;
69b1214c
JW
1625}
1626
69b1214c
JW
1627static int __init zilog_init(void)
1628{
1629 int ret;
1630
1631 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1632
1633 mutex_init(&tx_data_lock);
1634
1635 request_module("firmware_class");
1636
1637 ret = i2c_add_driver(&driver);
1638 if (ret)
1639 zilog_error("initialization failed\n");
1640 else
1641 zilog_notify("initialization complete\n");
1642
1643 return ret;
1644}
1645
1646static void __exit zilog_exit(void)
1647{
1648 i2c_del_driver(&driver);
1649 /* if loaded */
1650 fw_unload();
1651 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1652}
1653
1654module_init(zilog_init);
1655module_exit(zilog_exit);
1656
1657MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1658MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
c2790c71
AW
1659 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1660 "Andy Walls");
69b1214c
JW
1661MODULE_LICENSE("GPL");
1662/* for compat with old name, which isn't all that accurate anymore */
1663MODULE_ALIAS("lirc_pvr150");
1664
1665module_param(minor, int, 0444);
1666MODULE_PARM_DESC(minor, "Preferred minor device number");
1667
1668module_param(debug, bool, 0644);
1669MODULE_PARM_DESC(debug, "Enable debugging messages");
1670
a68a9b73
AW
1671module_param(tx_only, bool, 0644);
1672MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");