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