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