]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/staging/media/lirc/lirc_zilog.c
[media] staging: media: lirc: lirc_zilog.c: fix quoted strings split across lines
[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
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
AM
371 dev_err(ir->l.dev, "i2c_master_send failed with %d\n",
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];
be4aa815
AM
415 dev_dbg(ir->l.dev, "key (0x%02x/0x%02x)\n",
416 rx->b[0], rx->b[1]);
69b1214c
JW
417 }
418
419 /* key pressed ? */
06da95a3 420 if (rx->hdpvr_data_fmt) {
5bd6b046
AW
421 if (got_data && (keybuf[0] == 0x80)) {
422 ret = 0;
423 break;
424 } else if (got_data && (keybuf[0] == 0x00)) {
425 ret = -ENODATA;
426 break;
427 }
428 } else if ((rx->b[0] & 0x80) == 0) {
429 ret = got_data ? 0 : -ENODATA;
430 break;
431 }
69b1214c
JW
432
433 /* look what we have */
06da95a3 434 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
69b1214c
JW
435
436 codes[0] = (code >> 8) & 0xff;
437 codes[1] = code & 0xff;
438
439 /* return it */
bcbd1655 440 lirc_buffer_write(rbuf, codes);
69b1214c 441 ++got_data;
5bd6b046 442 ret = 0;
bcbd1655 443 } while (!lirc_buffer_full(rbuf));
69b1214c 444
d6dbd939 445 mutex_unlock(&rx->client_lock);
5bd6b046
AW
446 if (tx != NULL)
447 put_ir_tx(tx, false);
448 put_ir_rx(rx, false);
449 return ret;
69b1214c
JW
450}
451
452/*
453 * Main function of the polling thread -- from lirc_dev.
454 * We don't fit the LIRC model at all anymore. This is horrible, but
455 * basically we have a single RX/TX device with a nasty failure mode
456 * that needs to be accounted for across the pair. lirc lets us provide
457 * fops, but prevents us from using the internal polling, etc. if we do
458 * so. Hence the replication. Might be neater to extend the LIRC model
459 * to account for this but I'd think it's a very special case of seriously
460 * messed up hardware.
461 */
462static int lirc_thread(void *arg)
463{
464 struct IR *ir = arg;
bcbd1655 465 struct lirc_buffer *rbuf = ir->l.rbuf;
69b1214c 466
be4aa815 467 dev_dbg(ir->l.dev, "poll thread started\n");
69b1214c 468
b757730b 469 while (!kthread_should_stop()) {
6a8c97ac
JW
470 set_current_state(TASK_INTERRUPTIBLE);
471
b757730b 472 /* if device not opened, we can sleep half a second */
a3064855 473 if (atomic_read(&ir->open_count) == 0) {
69b1214c 474 schedule_timeout(HZ/2);
b757730b 475 continue;
69b1214c 476 }
69b1214c 477
b757730b
AW
478 /*
479 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
480 * We use this interval as the chip resets every time you poll
481 * it (bad!). This is therefore just sufficient to catch all
482 * of the button presses. It makes the remote much more
483 * responsive. You can see the difference by running irw and
484 * holding down a button. With 100ms, the old polling
485 * interval, you'll notice breaks in the repeat sequence
486 * corresponding to lost keypresses.
487 */
488 schedule_timeout((260 * HZ) / 1000);
489 if (kthread_should_stop())
490 break;
491 if (!add_to_buf(ir))
bcbd1655 492 wake_up_interruptible(&rbuf->wait_poll);
b757730b 493 }
69b1214c 494
be4aa815 495 dev_dbg(ir->l.dev, "poll thread ended\n");
69b1214c
JW
496 return 0;
497}
498
499static int set_use_inc(void *data)
500{
69b1214c
JW
501 return 0;
502}
503
504static void set_use_dec(void *data)
505{
69b1214c
JW
506}
507
508/* safe read of a uint32 (always network byte order) */
509static int read_uint32(unsigned char **data,
510 unsigned char *endp, unsigned int *val)
511{
512 if (*data + 4 > endp)
513 return 0;
514 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
515 ((*data)[2] << 8) | (*data)[3];
516 *data += 4;
517 return 1;
518}
519
520/* safe read of a uint8 */
521static int read_uint8(unsigned char **data,
522 unsigned char *endp, unsigned char *val)
523{
524 if (*data + 1 > endp)
525 return 0;
526 *val = *((*data)++);
527 return 1;
528}
529
530/* safe skipping of N bytes */
531static int skip(unsigned char **data,
532 unsigned char *endp, unsigned int distance)
533{
534 if (*data + distance > endp)
535 return 0;
536 *data += distance;
537 return 1;
538}
539
540/* decompress key data into the given buffer */
541static int get_key_data(unsigned char *buf,
542 unsigned int codeset, unsigned int key)
543{
544 unsigned char *data, *endp, *diffs, *key_block;
545 unsigned char keys, ndiffs, id;
546 unsigned int base, lim, pos, i;
547
548 /* Binary search for the codeset */
549 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
550 pos = base + (lim >> 1);
551 data = tx_data->code_sets[pos];
552
553 if (!read_uint32(&data, tx_data->endp, &i))
554 goto corrupt;
555
556 if (i == codeset)
557 break;
558 else if (codeset > i) {
559 base = pos + 1;
560 --lim;
561 }
562 }
563 /* Not found? */
564 if (!lim)
565 return -EPROTO;
566
567 /* Set end of data block */
568 endp = pos < tx_data->num_code_sets - 1 ?
569 tx_data->code_sets[pos + 1] : tx_data->endp;
570
571 /* Read the block header */
572 if (!read_uint8(&data, endp, &keys) ||
573 !read_uint8(&data, endp, &ndiffs) ||
574 ndiffs > TX_BLOCK_SIZE || keys == 0)
575 goto corrupt;
576
577 /* Save diffs & skip */
578 diffs = data;
579 if (!skip(&data, endp, ndiffs))
580 goto corrupt;
581
582 /* Read the id of the first key */
583 if (!read_uint8(&data, endp, &id))
584 goto corrupt;
585
586 /* Unpack the first key's data */
587 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
588 if (tx_data->fixed[i] == -1) {
589 if (!read_uint8(&data, endp, &buf[i]))
590 goto corrupt;
591 } else {
592 buf[i] = (unsigned char)tx_data->fixed[i];
593 }
594 }
595
596 /* Early out key found/not found */
597 if (key == id)
598 return 0;
599 if (keys == 1)
600 return -EPROTO;
601
602 /* Sanity check */
603 key_block = data;
604 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
605 goto corrupt;
606
607 /* Binary search for the key */
608 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
609 /* Seek to block */
610 unsigned char *key_data;
fd8392f3 611
69b1214c
JW
612 pos = base + (lim >> 1);
613 key_data = key_block + (ndiffs + 1) * pos;
614
615 if (*key_data == key) {
616 /* skip key id */
617 ++key_data;
618
619 /* found, so unpack the diffs */
620 for (i = 0; i < ndiffs; ++i) {
621 unsigned char val;
fd8392f3 622
69b1214c
JW
623 if (!read_uint8(&key_data, endp, &val) ||
624 diffs[i] >= TX_BLOCK_SIZE)
625 goto corrupt;
626 buf[diffs[i]] = val;
627 }
628
629 return 0;
630 } else if (key > *key_data) {
631 base = pos + 1;
632 --lim;
633 }
634 }
635 /* Key not found */
636 return -EPROTO;
637
638corrupt:
be4aa815 639 pr_err("firmware is corrupt\n");
69b1214c
JW
640 return -EFAULT;
641}
642
643/* send a block of data to the IR TX device */
06da95a3 644static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
69b1214c
JW
645{
646 int i, j, ret;
647 unsigned char buf[5];
648
649 for (i = 0; i < TX_BLOCK_SIZE;) {
650 int tosend = TX_BLOCK_SIZE - i;
fd8392f3 651
69b1214c
JW
652 if (tosend > 4)
653 tosend = 4;
654 buf[0] = (unsigned char)(i + 1);
655 for (j = 0; j < tosend; ++j)
656 buf[1 + j] = data_block[i + j];
be4aa815 657 dev_dbg(tx->ir->l.dev, "%*ph", 5, buf);
e9b351f6 658 ret = i2c_master_send(tx->c, buf, tosend + 1);
69b1214c 659 if (ret != tosend + 1) {
be4aa815
AM
660 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n",
661 ret);
69b1214c
JW
662 return ret < 0 ? ret : -EFAULT;
663 }
664 i += tosend;
665 }
666 return 0;
667}
668
669/* send boot data to the IR TX device */
06da95a3 670static int send_boot_data(struct IR_tx *tx)
69b1214c 671{
5766d204 672 int ret, i;
69b1214c
JW
673 unsigned char buf[4];
674
675 /* send the boot block */
06da95a3 676 ret = send_data_block(tx, tx_data->boot_data);
69b1214c
JW
677 if (ret != 0)
678 return ret;
679
5766d204 680 /* Hit the go button to activate the new boot data */
69b1214c
JW
681 buf[0] = 0x00;
682 buf[1] = 0x20;
e9b351f6 683 ret = i2c_master_send(tx->c, buf, 2);
69b1214c 684 if (ret != 2) {
be4aa815 685 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
69b1214c
JW
686 return ret < 0 ? ret : -EFAULT;
687 }
5766d204
JW
688
689 /*
690 * Wait for zilog to settle after hitting go post boot block upload.
691 * Without this delay, the HD-PVR and HVR-1950 both return an -EIO
692 * upon attempting to get firmware revision, and tx probe thus fails.
693 */
694 for (i = 0; i < 10; i++) {
695 ret = i2c_master_send(tx->c, buf, 1);
696 if (ret == 1)
697 break;
698 udelay(100);
699 }
700
69b1214c 701 if (ret != 1) {
be4aa815 702 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
69b1214c
JW
703 return ret < 0 ? ret : -EFAULT;
704 }
705
706 /* Here comes the firmware version... (hopefully) */
e9b351f6 707 ret = i2c_master_recv(tx->c, buf, 4);
69b1214c 708 if (ret != 4) {
be4aa815 709 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
69b1214c
JW
710 return 0;
711 }
5766d204 712 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
be4aa815
AM
713 dev_err(tx->ir->l.dev, "unexpected IR TX init response: %02x\n",
714 buf[0]);
69b1214c
JW
715 return 0;
716 }
221ca912
LB
717 dev_notice(tx->ir->l.dev,
718 "Zilog/Hauppauge IR blaster firmware version %d.%d.%d loaded\n",
719 buf[1], buf[2], buf[3]);
69b1214c
JW
720
721 return 0;
722}
723
724/* unload "firmware", lock held */
725static void fw_unload_locked(void)
726{
727 if (tx_data) {
e8379ecf 728 vfree(tx_data->code_sets);
69b1214c 729
e8379ecf 730 vfree(tx_data->datap);
69b1214c
JW
731
732 vfree(tx_data);
733 tx_data = NULL;
be4aa815 734 pr_debug("successfully unloaded IR blaster firmware\n");
69b1214c
JW
735 }
736}
737
738/* unload "firmware" for the IR TX device */
739static void fw_unload(void)
740{
741 mutex_lock(&tx_data_lock);
742 fw_unload_locked();
743 mutex_unlock(&tx_data_lock);
744}
745
746/* load "firmware" for the IR TX device */
06da95a3 747static int fw_load(struct IR_tx *tx)
69b1214c
JW
748{
749 int ret;
750 unsigned int i;
751 unsigned char *data, version, num_global_fixed;
752 const struct firmware *fw_entry;
753
754 /* Already loaded? */
755 mutex_lock(&tx_data_lock);
756 if (tx_data) {
757 ret = 0;
758 goto out;
759 }
760
761 /* Request codeset data file */
5bd6b046 762 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", tx->ir->l.dev);
69b1214c 763 if (ret != 0) {
be4aa815 764 dev_err(tx->ir->l.dev, "firmware haup-ir-blaster.bin not available (%d)\n",
70a51d28 765 ret);
69b1214c
JW
766 ret = ret < 0 ? ret : -EFAULT;
767 goto out;
768 }
be4aa815 769 dev_dbg(tx->ir->l.dev, "firmware of size %zu loaded\n", fw_entry->size);
69b1214c
JW
770
771 /* Parse the file */
772 tx_data = vmalloc(sizeof(*tx_data));
773 if (tx_data == NULL) {
69b1214c
JW
774 release_firmware(fw_entry);
775 ret = -ENOMEM;
776 goto out;
777 }
778 tx_data->code_sets = NULL;
779
780 /* Copy the data so hotplug doesn't get confused and timeout */
781 tx_data->datap = vmalloc(fw_entry->size);
782 if (tx_data->datap == NULL) {
69b1214c
JW
783 release_firmware(fw_entry);
784 vfree(tx_data);
785 ret = -ENOMEM;
786 goto out;
787 }
788 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
789 tx_data->endp = tx_data->datap + fw_entry->size;
790 release_firmware(fw_entry); fw_entry = NULL;
791
792 /* Check version */
793 data = tx_data->datap;
794 if (!read_uint8(&data, tx_data->endp, &version))
795 goto corrupt;
796 if (version != 1) {
221ca912
LB
797 dev_err(tx->ir->l.dev,
798 "unsupported code set file version (%u, expected 1) -- please upgrade to a newer driver",
799 version);
69b1214c
JW
800 fw_unload_locked();
801 ret = -EFAULT;
802 goto out;
803 }
804
805 /* Save boot block for later */
806 tx_data->boot_data = data;
807 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
808 goto corrupt;
809
810 if (!read_uint32(&data, tx_data->endp,
811 &tx_data->num_code_sets))
812 goto corrupt;
813
be4aa815
AM
814 dev_dbg(tx->ir->l.dev, "%u IR blaster codesets loaded\n",
815 tx_data->num_code_sets);
69b1214c
JW
816
817 tx_data->code_sets = vmalloc(
818 tx_data->num_code_sets * sizeof(char *));
819 if (tx_data->code_sets == NULL) {
820 fw_unload_locked();
821 ret = -ENOMEM;
822 goto out;
823 }
824
825 for (i = 0; i < TX_BLOCK_SIZE; ++i)
826 tx_data->fixed[i] = -1;
827
828 /* Read global fixed data template */
829 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
830 num_global_fixed > TX_BLOCK_SIZE)
831 goto corrupt;
832 for (i = 0; i < num_global_fixed; ++i) {
833 unsigned char pos, val;
fd8392f3 834
69b1214c
JW
835 if (!read_uint8(&data, tx_data->endp, &pos) ||
836 !read_uint8(&data, tx_data->endp, &val) ||
837 pos >= TX_BLOCK_SIZE)
838 goto corrupt;
839 tx_data->fixed[pos] = (int)val;
840 }
841
842 /* Filch out the position of each code set */
843 for (i = 0; i < tx_data->num_code_sets; ++i) {
844 unsigned int id;
845 unsigned char keys;
846 unsigned char ndiffs;
847
848 /* Save the codeset position */
849 tx_data->code_sets[i] = data;
850
851 /* Read header */
852 if (!read_uint32(&data, tx_data->endp, &id) ||
853 !read_uint8(&data, tx_data->endp, &keys) ||
854 !read_uint8(&data, tx_data->endp, &ndiffs) ||
855 ndiffs > TX_BLOCK_SIZE || keys == 0)
856 goto corrupt;
857
858 /* skip diff positions */
859 if (!skip(&data, tx_data->endp, ndiffs))
860 goto corrupt;
861
862 /*
863 * After the diffs we have the first key id + data -
864 * global fixed
865 */
866 if (!skip(&data, tx_data->endp,
867 1 + TX_BLOCK_SIZE - num_global_fixed))
868 goto corrupt;
869
870 /* Then we have keys-1 blocks of key id+diffs */
871 if (!skip(&data, tx_data->endp,
872 (ndiffs + 1) * (keys - 1)))
873 goto corrupt;
874 }
875 ret = 0;
876 goto out;
877
878corrupt:
be4aa815 879 dev_err(tx->ir->l.dev, "firmware is corrupt\n");
69b1214c
JW
880 fw_unload_locked();
881 ret = -EFAULT;
882
883out:
884 mutex_unlock(&tx_data_lock);
885 return ret;
886}
887
69b1214c 888/* copied from lirc_dev */
12a72083
TT
889static ssize_t read(struct file *filep, char __user *outbuf, size_t n,
890 loff_t *ppos)
69b1214c 891{
e0ac7da0 892 struct IR *ir = filep->private_data;
5bd6b046 893 struct IR_rx *rx;
bcbd1655 894 struct lirc_buffer *rbuf = ir->l.rbuf;
cc664ae0 895 int ret = 0, written = 0, retries = 0;
8152b760 896 unsigned int m;
69b1214c
JW
897 DECLARE_WAITQUEUE(wait, current);
898
be4aa815 899 dev_dbg(ir->l.dev, "read called\n");
bcbd1655 900 if (n % rbuf->chunk_size) {
be4aa815 901 dev_dbg(ir->l.dev, "read result = -EINVAL\n");
69b1214c
JW
902 return -EINVAL;
903 }
904
5bd6b046
AW
905 rx = get_ir_rx(ir);
906 if (rx == NULL)
907 return -ENXIO;
908
69b1214c
JW
909 /*
910 * we add ourselves to the task queue before buffer check
911 * to avoid losing scan code (in case when queue is awaken somewhere
912 * between while condition checking and scheduling)
913 */
bcbd1655 914 add_wait_queue(&rbuf->wait_poll, &wait);
69b1214c
JW
915 set_current_state(TASK_INTERRUPTIBLE);
916
917 /*
918 * while we didn't provide 'length' bytes, device is opened in blocking
919 * mode and 'copy_to_user' is happy, wait for data.
920 */
921 while (written < n && ret == 0) {
bcbd1655 922 if (lirc_buffer_empty(rbuf)) {
69b1214c
JW
923 /*
924 * According to the read(2) man page, 'written' can be
925 * returned as less than 'n', instead of blocking
926 * again, returning -EWOULDBLOCK, or returning
927 * -ERESTARTSYS
928 */
929 if (written)
930 break;
931 if (filep->f_flags & O_NONBLOCK) {
932 ret = -EWOULDBLOCK;
933 break;
934 }
935 if (signal_pending(current)) {
936 ret = -ERESTARTSYS;
937 break;
938 }
939 schedule();
940 set_current_state(TASK_INTERRUPTIBLE);
941 } else {
ac5b4b6b
MCC
942 unsigned char buf[MAX_XFER_SIZE];
943
944 if (rbuf->chunk_size > sizeof(buf)) {
be4aa815 945 dev_err(ir->l.dev, "chunk_size is too big (%d)!\n",
ac5b4b6b
MCC
946 rbuf->chunk_size);
947 ret = -EINVAL;
948 break;
949 }
bcbd1655
AW
950 m = lirc_buffer_read(rbuf, buf);
951 if (m == rbuf->chunk_size) {
12a72083 952 ret = copy_to_user(outbuf + written, buf,
bcbd1655
AW
953 rbuf->chunk_size);
954 written += rbuf->chunk_size;
cc664ae0
JW
955 } else {
956 retries++;
957 }
958 if (retries >= 5) {
be4aa815 959 dev_err(ir->l.dev, "Buffer read failed!\n");
cc664ae0 960 ret = -EIO;
8152b760 961 }
69b1214c
JW
962 }
963 }
964
bcbd1655 965 remove_wait_queue(&rbuf->wait_poll, &wait);
5bd6b046 966 put_ir_rx(rx, false);
69b1214c 967 set_current_state(TASK_RUNNING);
69b1214c 968
be4aa815
AM
969 dev_dbg(ir->l.dev, "read result = %d (%s)\n",
970 ret, ret ? "Error" : "OK");
69b1214c
JW
971
972 return ret ? ret : written;
973}
974
975/* send a keypress to the IR TX device */
06da95a3 976static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
69b1214c
JW
977{
978 unsigned char data_block[TX_BLOCK_SIZE];
979 unsigned char buf[2];
980 int i, ret;
981
982 /* Get data for the codeset/key */
983 ret = get_key_data(data_block, code, key);
984
985 if (ret == -EPROTO) {
221ca912
LB
986 dev_err(tx->ir->l.dev,
987 "failed to get data for code %u, key %u -- check lircd.conf entries\n",
988 code, key);
69b1214c
JW
989 return ret;
990 } else if (ret != 0)
991 return ret;
992
993 /* Send the data block */
06da95a3 994 ret = send_data_block(tx, data_block);
69b1214c
JW
995 if (ret != 0)
996 return ret;
997
998 /* Send data block length? */
999 buf[0] = 0x00;
1000 buf[1] = 0x40;
e9b351f6 1001 ret = i2c_master_send(tx->c, buf, 2);
69b1214c 1002 if (ret != 2) {
be4aa815 1003 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
69b1214c
JW
1004 return ret < 0 ? ret : -EFAULT;
1005 }
5766d204
JW
1006
1007 /* Give the z8 a moment to process data block */
1008 for (i = 0; i < 10; i++) {
1009 ret = i2c_master_send(tx->c, buf, 1);
1010 if (ret == 1)
1011 break;
1012 udelay(100);
1013 }
1014
69b1214c 1015 if (ret != 1) {
be4aa815 1016 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
69b1214c
JW
1017 return ret < 0 ? ret : -EFAULT;
1018 }
1019
1020 /* Send finished download? */
e9b351f6 1021 ret = i2c_master_recv(tx->c, buf, 1);
69b1214c 1022 if (ret != 1) {
be4aa815 1023 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
69b1214c
JW
1024 return ret < 0 ? ret : -EFAULT;
1025 }
1026 if (buf[0] != 0xA0) {
be4aa815 1027 dev_err(tx->ir->l.dev, "unexpected IR TX response #1: %02x\n",
69b1214c
JW
1028 buf[0]);
1029 return -EFAULT;
1030 }
1031
1032 /* Send prepare command? */
1033 buf[0] = 0x00;
1034 buf[1] = 0x80;
e9b351f6 1035 ret = i2c_master_send(tx->c, buf, 2);
69b1214c 1036 if (ret != 2) {
be4aa815 1037 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
69b1214c
JW
1038 return ret < 0 ? ret : -EFAULT;
1039 }
1040
69b1214c
JW
1041 /*
1042 * The sleep bits aren't necessary on the HD PVR, and in fact, the
1043 * last i2c_master_recv always fails with a -5, so for now, we're
1044 * going to skip this whole mess and say we're done on the HD PVR
1045 */
06da95a3 1046 if (!tx->post_tx_ready_poll) {
be4aa815 1047 dev_dbg(tx->ir->l.dev, "sent code %u, key %u\n", code, key);
d7c72356
AW
1048 return 0;
1049 }
69b1214c
JW
1050
1051 /*
1052 * This bit NAKs until the device is ready, so we retry it
1053 * sleeping a bit each time. This seems to be what the windows
1054 * driver does, approximately.
1055 * Try for up to 1s.
1056 */
1057 for (i = 0; i < 20; ++i) {
1058 set_current_state(TASK_UNINTERRUPTIBLE);
1059 schedule_timeout((50 * HZ + 999) / 1000);
e9b351f6 1060 ret = i2c_master_send(tx->c, buf, 1);
69b1214c
JW
1061 if (ret == 1)
1062 break;
221ca912
LB
1063 dev_dbg(tx->ir->l.dev,
1064 "NAK expected: i2c_master_send failed with %d (try %d)\n",
1065 ret, i+1);
69b1214c
JW
1066 }
1067 if (ret != 1) {
221ca912
LB
1068 dev_err(tx->ir->l.dev,
1069 "IR TX chip never got ready: last i2c_master_send failed with %d\n",
1070 ret);
69b1214c
JW
1071 return ret < 0 ? ret : -EFAULT;
1072 }
1073
1074 /* Seems to be an 'ok' response */
e9b351f6 1075 i = i2c_master_recv(tx->c, buf, 1);
69b1214c 1076 if (i != 1) {
be4aa815 1077 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
69b1214c
JW
1078 return -EFAULT;
1079 }
1080 if (buf[0] != 0x80) {
be4aa815
AM
1081 dev_err(tx->ir->l.dev, "unexpected IR TX response #2: %02x\n",
1082 buf[0]);
69b1214c
JW
1083 return -EFAULT;
1084 }
1085
69b1214c 1086 /* Oh good, it worked */
be4aa815 1087 dev_dbg(tx->ir->l.dev, "sent code %u, key %u\n", code, key);
69b1214c
JW
1088 return 0;
1089}
1090
1091/*
1092 * Write a code to the device. We take in a 32-bit number (an int) and then
1093 * decode this to a codeset/key index. The key data is then decompressed and
1094 * sent to the device. We have a spin lock as per i2c documentation to prevent
1095 * multiple concurrent sends which would probably cause the device to explode.
1096 */
12a72083
TT
1097static ssize_t write(struct file *filep, const char __user *buf, size_t n,
1098 loff_t *ppos)
69b1214c 1099{
e0ac7da0 1100 struct IR *ir = filep->private_data;
5bd6b046 1101 struct IR_tx *tx;
69b1214c
JW
1102 size_t i;
1103 int failures = 0;
1104
69b1214c
JW
1105 /* Validate user parameters */
1106 if (n % sizeof(int))
1107 return -EINVAL;
1108
5bd6b046
AW
1109 /* Get a struct IR_tx reference */
1110 tx = get_ir_tx(ir);
1111 if (tx == NULL)
1112 return -ENXIO;
1113
d6dbd939
AW
1114 /* Ensure our tx->c i2c_client remains valid for the duration */
1115 mutex_lock(&tx->client_lock);
1116 if (tx->c == NULL) {
1117 mutex_unlock(&tx->client_lock);
1118 put_ir_tx(tx, false);
1119 return -ENXIO;
1120 }
1121
69b1214c
JW
1122 /* Lock i2c bus for the duration */
1123 mutex_lock(&ir->ir_lock);
1124
1125 /* Send each keypress */
1126 for (i = 0; i < n;) {
1127 int ret = 0;
1128 int command;
1129
1130 if (copy_from_user(&command, buf + i, sizeof(command))) {
1131 mutex_unlock(&ir->ir_lock);
d6dbd939 1132 mutex_unlock(&tx->client_lock);
5bd6b046 1133 put_ir_tx(tx, false);
69b1214c
JW
1134 return -EFAULT;
1135 }
1136
1137 /* Send boot data first if required */
06da95a3 1138 if (tx->need_boot == 1) {
5bd6b046
AW
1139 /* Make sure we have the 'firmware' loaded, first */
1140 ret = fw_load(tx);
1141 if (ret != 0) {
1142 mutex_unlock(&ir->ir_lock);
d6dbd939 1143 mutex_unlock(&tx->client_lock);
5bd6b046
AW
1144 put_ir_tx(tx, false);
1145 if (ret != -ENOMEM)
1146 ret = -EIO;
1147 return ret;
1148 }
1149 /* Prep the chip for transmitting codes */
06da95a3 1150 ret = send_boot_data(tx);
69b1214c 1151 if (ret == 0)
06da95a3 1152 tx->need_boot = 0;
69b1214c
JW
1153 }
1154
1155 /* Send the code */
1156 if (ret == 0) {
06da95a3 1157 ret = send_code(tx, (unsigned)command >> 16,
69b1214c
JW
1158 (unsigned)command & 0xFFFF);
1159 if (ret == -EPROTO) {
1160 mutex_unlock(&ir->ir_lock);
d6dbd939 1161 mutex_unlock(&tx->client_lock);
5bd6b046 1162 put_ir_tx(tx, false);
69b1214c
JW
1163 return ret;
1164 }
1165 }
1166
1167 /*
1168 * Hmm, a failure. If we've had a few then give up, otherwise
1169 * try a reset
1170 */
1171 if (ret != 0) {
1172 /* Looks like the chip crashed, reset it */
221ca912
LB
1173 dev_err(tx->ir->l.dev,
1174 "sending to the IR transmitter chip failed, trying reset\n");
69b1214c
JW
1175
1176 if (failures >= 3) {
221ca912
LB
1177 dev_err(tx->ir->l.dev,
1178 "unable to send to the IR chip after 3 resets, giving up\n");
69b1214c 1179 mutex_unlock(&ir->ir_lock);
d6dbd939 1180 mutex_unlock(&tx->client_lock);
5bd6b046 1181 put_ir_tx(tx, false);
69b1214c
JW
1182 return ret;
1183 }
1184 set_current_state(TASK_UNINTERRUPTIBLE);
1185 schedule_timeout((100 * HZ + 999) / 1000);
06da95a3 1186 tx->need_boot = 1;
69b1214c
JW
1187 ++failures;
1188 } else
1189 i += sizeof(int);
1190 }
1191
1192 /* Release i2c bus */
1193 mutex_unlock(&ir->ir_lock);
1194
d6dbd939
AW
1195 mutex_unlock(&tx->client_lock);
1196
5bd6b046
AW
1197 /* Give back our struct IR_tx reference */
1198 put_ir_tx(tx, false);
1199
69b1214c
JW
1200 /* All looks good */
1201 return n;
1202}
1203
1204/* copied from lirc_dev */
1205static unsigned int poll(struct file *filep, poll_table *wait)
1206{
e0ac7da0 1207 struct IR *ir = filep->private_data;
5bd6b046 1208 struct IR_rx *rx;
bcbd1655 1209 struct lirc_buffer *rbuf = ir->l.rbuf;
69b1214c
JW
1210 unsigned int ret;
1211
be4aa815 1212 dev_dbg(ir->l.dev, "poll called\n");
69b1214c 1213
5bd6b046 1214 rx = get_ir_rx(ir);
915e5473
AW
1215 if (rx == NULL) {
1216 /*
1217 * Revisit this, if our poll function ever reports writeable
1218 * status for Tx
1219 */
be4aa815 1220 dev_dbg(ir->l.dev, "poll result = POLLERR\n");
915e5473
AW
1221 return POLLERR;
1222 }
69b1214c 1223
915e5473
AW
1224 /*
1225 * Add our lirc_buffer's wait_queue to the poll_table. A wake up on
1226 * that buffer's wait queue indicates we may have a new poll status.
1227 */
bcbd1655 1228 poll_wait(filep, &rbuf->wait_poll, wait);
69b1214c 1229
915e5473 1230 /* Indicate what ops could happen immediately without blocking */
bcbd1655 1231 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN|POLLRDNORM);
69b1214c 1232
be4aa815
AM
1233 dev_dbg(ir->l.dev, "poll result = %s\n",
1234 ret ? "POLLIN|POLLRDNORM" : "none");
69b1214c
JW
1235 return ret;
1236}
1237
1238static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1239{
e0ac7da0 1240 struct IR *ir = filep->private_data;
12a72083 1241 unsigned long __user *uptr = (unsigned long __user *)arg;
69b1214c 1242 int result;
5bd6b046 1243 unsigned long mode, features;
69b1214c 1244
5bd6b046 1245 features = ir->l.features;
69b1214c
JW
1246
1247 switch (cmd) {
1248 case LIRC_GET_LENGTH:
12a72083 1249 result = put_user(13UL, uptr);
69b1214c
JW
1250 break;
1251 case LIRC_GET_FEATURES:
12a72083 1252 result = put_user(features, uptr);
69b1214c
JW
1253 break;
1254 case LIRC_GET_REC_MODE:
1255 if (!(features&LIRC_CAN_REC_MASK))
1256 return -ENOSYS;
1257
1258 result = put_user(LIRC_REC2MODE
1259 (features&LIRC_CAN_REC_MASK),
12a72083 1260 uptr);
69b1214c
JW
1261 break;
1262 case LIRC_SET_REC_MODE:
1263 if (!(features&LIRC_CAN_REC_MASK))
1264 return -ENOSYS;
1265
12a72083 1266 result = get_user(mode, uptr);
69b1214c
JW
1267 if (!result && !(LIRC_MODE2REC(mode) & features))
1268 result = -EINVAL;
1269 break;
1270 case LIRC_GET_SEND_MODE:
12d896e1
AW
1271 if (!(features&LIRC_CAN_SEND_MASK))
1272 return -ENOSYS;
1273
12a72083 1274 result = put_user(LIRC_MODE_PULSE, uptr);
69b1214c
JW
1275 break;
1276 case LIRC_SET_SEND_MODE:
12d896e1
AW
1277 if (!(features&LIRC_CAN_SEND_MASK))
1278 return -ENOSYS;
1279
12a72083 1280 result = get_user(mode, uptr);
69b1214c
JW
1281 if (!result && mode != LIRC_MODE_PULSE)
1282 return -EINVAL;
1283 break;
1284 default:
1285 return -EINVAL;
1286 }
1287 return result;
1288}
1289
5bd6b046 1290static struct IR *get_ir_device_by_minor(unsigned int minor)
a68a9b73 1291{
5c07134f 1292 struct IR *ir;
5bd6b046 1293 struct IR *ret = NULL;
5c07134f 1294
5bd6b046 1295 mutex_lock(&ir_devices_lock);
a68a9b73 1296
5bd6b046
AW
1297 if (!list_empty(&ir_devices_list)) {
1298 list_for_each_entry(ir, &ir_devices_list, list) {
1299 if (ir->l.minor == minor) {
1300 ret = get_ir_device(ir, true);
1301 break;
1302 }
1303 }
1304 }
5c07134f 1305
5bd6b046
AW
1306 mutex_unlock(&ir_devices_lock);
1307 return ret;
a68a9b73
AW
1308}
1309
69b1214c
JW
1310/*
1311 * Open the IR device. Get hold of our IR structure and
1312 * stash it in private_data for the file
1313 */
1314static int open(struct inode *node, struct file *filep)
1315{
1316 struct IR *ir;
a68a9b73 1317 unsigned int minor = MINOR(node->i_rdev);
69b1214c
JW
1318
1319 /* find our IR struct */
5bd6b046 1320 ir = get_ir_device_by_minor(minor);
a68a9b73
AW
1321
1322 if (ir == NULL)
69b1214c 1323 return -ENODEV;
69b1214c 1324
a3064855 1325 atomic_inc(&ir->open_count);
69b1214c
JW
1326
1327 /* stash our IR struct */
1328 filep->private_data = ir;
1329
9a55a2b3 1330 nonseekable_open(node, filep);
69b1214c
JW
1331 return 0;
1332}
1333
1334/* Close the IR device */
1335static int close(struct inode *node, struct file *filep)
1336{
1337 /* find our IR struct */
e0ac7da0 1338 struct IR *ir = filep->private_data;
fd8392f3 1339
69b1214c 1340 if (ir == NULL) {
be4aa815 1341 dev_err(ir->l.dev, "close: no private_data attached to the file!\n");
69b1214c
JW
1342 return -ENODEV;
1343 }
1344
a3064855 1345 atomic_dec(&ir->open_count);
69b1214c 1346
5bd6b046 1347 put_ir_device(ir, false);
69b1214c
JW
1348 return 0;
1349}
1350
69b1214c
JW
1351static int ir_remove(struct i2c_client *client);
1352static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
69b1214c 1353
d7c72356
AW
1354#define ID_FLAG_TX 0x01
1355#define ID_FLAG_HDPVR 0x02
1356
69b1214c 1357static const struct i2c_device_id ir_transceiver_id[] = {
d7c72356
AW
1358 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1359 { "ir_rx_z8f0811_haup", 0 },
1360 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1361 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
69b1214c
JW
1362 { }
1363};
1364
1365static struct i2c_driver driver = {
1366 .driver = {
1367 .owner = THIS_MODULE,
1368 .name = "Zilog/Hauppauge i2c IR",
1369 },
1370 .probe = ir_probe,
1371 .remove = ir_remove,
69b1214c
JW
1372 .id_table = ir_transceiver_id,
1373};
1374
0f9313ad 1375static const struct file_operations lirc_fops = {
69b1214c 1376 .owner = THIS_MODULE,
9a55a2b3 1377 .llseek = no_llseek,
69b1214c
JW
1378 .read = read,
1379 .write = write,
1380 .poll = poll,
1381 .unlocked_ioctl = ioctl,
8be292cc
JW
1382#ifdef CONFIG_COMPAT
1383 .compat_ioctl = ioctl,
1384#endif
69b1214c
JW
1385 .open = open,
1386 .release = close
1387};
1388
534c1eab
AW
1389static struct lirc_driver lirc_template = {
1390 .name = "lirc_zilog",
1391 .minor = -1,
1392 .code_length = 13,
1393 .buffer_size = BUFLEN / 2,
1394 .sample_rate = 0, /* tell lirc_dev to not start its own kthread */
1395 .chunk_size = 2,
1396 .set_use_inc = set_use_inc,
1397 .set_use_dec = set_use_dec,
1398 .fops = &lirc_fops,
1399 .owner = THIS_MODULE,
1400};
1401
a68a9b73
AW
1402static int ir_remove(struct i2c_client *client)
1403{
5bd6b046
AW
1404 if (strncmp("ir_tx_z8", client->name, 8) == 0) {
1405 struct IR_tx *tx = i2c_get_clientdata(client);
fd8392f3 1406
d6dbd939
AW
1407 if (tx != NULL) {
1408 mutex_lock(&tx->client_lock);
1409 tx->c = NULL;
1410 mutex_unlock(&tx->client_lock);
5bd6b046 1411 put_ir_tx(tx, false);
d6dbd939 1412 }
5bd6b046
AW
1413 } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
1414 struct IR_rx *rx = i2c_get_clientdata(client);
fd8392f3 1415
d6dbd939
AW
1416 if (rx != NULL) {
1417 mutex_lock(&rx->client_lock);
1418 rx->c = NULL;
1419 mutex_unlock(&rx->client_lock);
5bd6b046 1420 put_ir_rx(rx, false);
d6dbd939 1421 }
69b1214c 1422 }
69b1214c
JW
1423 return 0;
1424}
1425
a68a9b73
AW
1426
1427/* ir_devices_lock must be held */
5bd6b046 1428static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
69b1214c 1429{
5c07134f 1430 struct IR *ir;
a68a9b73 1431
5c07134f
AW
1432 if (list_empty(&ir_devices_list))
1433 return NULL;
1434
1435 list_for_each_entry(ir, &ir_devices_list, list)
5bd6b046
AW
1436 if (ir->adapter == adapter) {
1437 get_ir_device(ir, true);
5c07134f 1438 return ir;
5bd6b046 1439 }
a68a9b73 1440
5c07134f 1441 return NULL;
a68a9b73
AW
1442}
1443
1444static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1445{
1446 struct IR *ir;
5bd6b046
AW
1447 struct IR_tx *tx;
1448 struct IR_rx *rx;
69b1214c 1449 struct i2c_adapter *adap = client->adapter;
69b1214c 1450 int ret;
a68a9b73 1451 bool tx_probe = false;
69b1214c 1452
f80bdc28 1453 dev_dbg(&client->dev, "%s: %s on i2c-%d (%s), client addr=0x%02x\n",
86e52428 1454 __func__, id->name, adap->nr, adap->name, client->addr);
69b1214c 1455
d7c72356 1456 /*
a68a9b73
AW
1457 * The IR receiver is at i2c address 0x71.
1458 * The IR transmitter is at i2c address 0x70.
69b1214c 1459 */
69b1214c 1460
a68a9b73
AW
1461 if (id->driver_data & ID_FLAG_TX)
1462 tx_probe = true;
1463 else if (tx_only) /* module option */
e9b351f6 1464 return -ENXIO;
69b1214c 1465
be4aa815 1466 pr_info("probing IR %s on %s (i2c-%d)\n",
b757730b 1467 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
69b1214c 1468
a68a9b73 1469 mutex_lock(&ir_devices_lock);
69b1214c 1470
a68a9b73 1471 /* Use a single struct IR instance for both the Rx and Tx functions */
5bd6b046 1472 ir = get_ir_device_by_adapter(adap);
a68a9b73
AW
1473 if (ir == NULL) {
1474 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1475 if (ir == NULL) {
1476 ret = -ENOMEM;
1477 goto out_no_ir;
06da95a3 1478 }
5bd6b046
AW
1479 kref_init(&ir->ref);
1480
a68a9b73 1481 /* store for use in ir_probe() again, and open() later on */
5c07134f 1482 INIT_LIST_HEAD(&ir->list);
5bd6b046 1483 list_add_tail(&ir->list, &ir_devices_list);
a68a9b73
AW
1484
1485 ir->adapter = adap;
1486 mutex_init(&ir->ir_lock);
a3064855 1487 atomic_set(&ir->open_count, 0);
5bd6b046
AW
1488 spin_lock_init(&ir->tx_ref_lock);
1489 spin_lock_init(&ir->rx_ref_lock);
a68a9b73
AW
1490
1491 /* set lirc_dev stuff */
1492 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
5bd6b046
AW
1493 /*
1494 * FIXME this is a pointer reference to us, but no refcount.
1495 *
1496 * This OK for now, since lirc_dev currently won't touch this
1497 * buffer as we provide our own lirc_fops.
1498 *
1499 * Currently our own lirc_fops rely on this ir->l.rbuf pointer
1500 */
1501 ir->l.rbuf = &ir->rbuf;
1502 ir->l.dev = &adap->dev;
bcbd1655
AW
1503 ret = lirc_buffer_init(ir->l.rbuf,
1504 ir->l.chunk_size, ir->l.buffer_size);
1505 if (ret)
5bd6b046 1506 goto out_put_ir;
06da95a3
AW
1507 }
1508
a68a9b73 1509 if (tx_probe) {
5bd6b046
AW
1510 /* Get the IR_rx instance for later, if already allocated */
1511 rx = get_ir_rx(ir);
1512
a68a9b73 1513 /* Set up a struct IR_tx instance */
5bd6b046
AW
1514 tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1515 if (tx == NULL) {
a68a9b73 1516 ret = -ENOMEM;
5bd6b046 1517 goto out_put_xx;
a68a9b73 1518 }
5bd6b046
AW
1519 kref_init(&tx->ref);
1520 ir->tx = tx;
06da95a3 1521
534c1eab 1522 ir->l.features |= LIRC_CAN_SEND_PULSE;
d6dbd939 1523 mutex_init(&tx->client_lock);
5bd6b046
AW
1524 tx->c = client;
1525 tx->need_boot = 1;
1526 tx->post_tx_ready_poll =
a68a9b73 1527 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
5bd6b046
AW
1528
1529 /* An ir ref goes to the struct IR_tx instance */
1530 tx->ir = get_ir_device(ir, true);
1531
1532 /* A tx ref goes to the i2c_client */
1533 i2c_set_clientdata(client, get_ir_tx(ir));
1534
1535 /*
1536 * Load the 'firmware'. We do this before registering with
1537 * lirc_dev, so the first firmware load attempt does not happen
1538 * after a open() or write() call on the device.
1539 *
1540 * Failure here is not deemed catastrophic, so the receiver will
1541 * still be usable. Firmware load will be retried in write(),
1542 * if it is needed.
1543 */
1544 fw_load(tx);
1545
1546 /* Proceed only if the Rx client is also ready or not needed */
1547 if (rx == NULL && !tx_only) {
221ca912
LB
1548 dev_info(tx->ir->l.dev,
1549 "probe of IR Tx on %s (i2c-%d) done. Waiting on IR Rx.\n",
1550 adap->name, adap->nr);
5bd6b046
AW
1551 goto out_ok;
1552 }
a68a9b73 1553 } else {
5bd6b046
AW
1554 /* Get the IR_tx instance for later, if already allocated */
1555 tx = get_ir_tx(ir);
1556
a68a9b73 1557 /* Set up a struct IR_rx instance */
5bd6b046
AW
1558 rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1559 if (rx == NULL) {
06da95a3 1560 ret = -ENOMEM;
5bd6b046 1561 goto out_put_xx;
06da95a3 1562 }
5bd6b046
AW
1563 kref_init(&rx->ref);
1564 ir->rx = rx;
06da95a3 1565
534c1eab 1566 ir->l.features |= LIRC_CAN_REC_LIRCCODE;
d6dbd939 1567 mutex_init(&rx->client_lock);
5bd6b046
AW
1568 rx->c = client;
1569 rx->hdpvr_data_fmt =
a68a9b73 1570 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
69b1214c 1571
5bd6b046
AW
1572 /* An ir ref goes to the struct IR_rx instance */
1573 rx->ir = get_ir_device(ir, true);
69b1214c 1574
5bd6b046
AW
1575 /* An rx ref goes to the i2c_client */
1576 i2c_set_clientdata(client, get_ir_rx(ir));
a68a9b73 1577
5bd6b046
AW
1578 /*
1579 * Start the polling thread.
1580 * It will only perform an empty loop around schedule_timeout()
1581 * until we register with lirc_dev and the first user open()
1582 */
1583 /* An ir ref goes to the new rx polling kthread */
1584 rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
1585 "zilog-rx-i2c-%d", adap->nr);
1586 if (IS_ERR(rx->task)) {
1587 ret = PTR_ERR(rx->task);
221ca912
LB
1588 dev_err(tx->ir->l.dev,
1589 "%s: could not start IR Rx polling thread\n",
1590 __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) {
221ca912
LB
1602 pr_info("probe of IR Rx on %s (i2c-%d) done. Waiting on IR Tx.\n",
1603 adap->name, adap->nr);
5bd6b046 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) {
be4aa815 1612 dev_err(tx->ir->l.dev, "%s: \"minor\" must be between 0 and %d (%d)!\n",
a68a9b73 1613 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
69b1214c 1614 ret = -EBADRQC;
5bd6b046 1615 goto out_put_xx;
12d896e1 1616 }
be4aa815 1617 dev_info(ir->l.dev, "IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
4933fc9d 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);
be4aa815 1626 dev_info(ir->l.dev, "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 1638out_no_ir:
f80bdc28 1639 dev_err(&client->dev, "%s: probing IR %s on %s (i2c-%d) failed with %d\n",
a68a9b73
AW
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
be4aa815 1650 pr_notice("Zilog/Hauppauge IR driver initializing\n");
69b1214c
JW
1651
1652 mutex_init(&tx_data_lock);
1653
1654 request_module("firmware_class");
1655
1656 ret = i2c_add_driver(&driver);
1657 if (ret)
be4aa815 1658 pr_err("initialization failed\n");
69b1214c 1659 else
be4aa815 1660 pr_notice("initialization complete\n");
69b1214c
JW
1661
1662 return ret;
1663}
1664
1665static void __exit zilog_exit(void)
1666{
1667 i2c_del_driver(&driver);
1668 /* if loaded */
1669 fw_unload();
be4aa815 1670 pr_notice("Zilog/Hauppauge IR driver unloaded\n");
69b1214c
JW
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");