Staging: Merge branch 'tidspbridge-for-2.6.39' of git://dev.omapzoom.org/pub/scm...
[deliverable/linux.git] / drivers / staging / lirc / lirc_zilog.c
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 *
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 *
26 * This program is free software; you can redistribute it and/or modify
27 * it under the terms of the GNU General Public License as published by
28 * the Free Software Foundation; either version 2 of the License, or
29 * (at your option) any later version.
30 *
31 * This program is distributed in the hope that it will be useful,
32 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
34 * GNU General Public License for more details.
35 *
36 * You should have received a copy of the GNU General Public License
37 * along with this program; if not, write to the Free Software
38 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 *
40 */
41
42
43 #include <linux/version.h>
44 #include <linux/module.h>
45 #include <linux/kmod.h>
46 #include <linux/kernel.h>
47 #include <linux/sched.h>
48 #include <linux/fs.h>
49 #include <linux/poll.h>
50 #include <linux/string.h>
51 #include <linux/timer.h>
52 #include <linux/delay.h>
53 #include <linux/completion.h>
54 #include <linux/errno.h>
55 #include <linux/slab.h>
56 #include <linux/i2c.h>
57 #include <linux/firmware.h>
58 #include <linux/vmalloc.h>
59
60 #include <linux/mutex.h>
61 #include <linux/kthread.h>
62
63 #include <media/lirc_dev.h>
64 #include <media/lirc.h>
65
66 struct IR_rx {
67 /* RX device */
68 struct i2c_client *c;
69
70 /* RX device buffer & lock */
71 struct lirc_buffer buf;
72 struct mutex buf_lock;
73
74 /* RX polling thread data */
75 struct task_struct *task;
76
77 /* RX read data */
78 unsigned char b[3];
79 bool hdpvr_data_fmt;
80 };
81
82 struct IR_tx {
83 /* TX device */
84 struct i2c_client *c;
85
86 /* TX additional actions needed */
87 int need_boot;
88 bool post_tx_ready_poll;
89 };
90
91 struct IR {
92 struct lirc_driver l;
93
94 struct mutex ir_lock;
95 int open;
96
97 struct i2c_adapter *adapter;
98 struct IR_rx *rx;
99 struct IR_tx *tx;
100 };
101
102 /* Minor -> data mapping */
103 static struct mutex ir_devices_lock;
104 static struct IR *ir_devices[MAX_IRCTL_DEVICES];
105
106 /* Block size for IR transmitter */
107 #define TX_BLOCK_SIZE 99
108
109 /* Hauppauge IR transmitter data */
110 struct tx_data_struct {
111 /* Boot block */
112 unsigned char *boot_data;
113
114 /* Start of binary data block */
115 unsigned char *datap;
116
117 /* End of binary data block */
118 unsigned char *endp;
119
120 /* Number of installed codesets */
121 unsigned int num_code_sets;
122
123 /* Pointers to codesets */
124 unsigned char **code_sets;
125
126 /* Global fixed data template */
127 int fixed[TX_BLOCK_SIZE];
128 };
129
130 static struct tx_data_struct *tx_data;
131 static struct mutex tx_data_lock;
132
133 #define zilog_notify(s, args...) printk(KERN_NOTICE KBUILD_MODNAME ": " s, \
134 ## args)
135 #define zilog_error(s, args...) printk(KERN_ERR KBUILD_MODNAME ": " s, ## args)
136 #define zilog_info(s, args...) printk(KERN_INFO KBUILD_MODNAME ": " s, ## args)
137
138 /* module parameters */
139 static int debug; /* debug output */
140 static int tx_only; /* only handle the IR Tx function */
141 static int minor = -1; /* minor number */
142
143 #define dprintk(fmt, args...) \
144 do { \
145 if (debug) \
146 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
147 ## args); \
148 } while (0)
149
150 static int add_to_buf(struct IR *ir)
151 {
152 __u16 code;
153 unsigned char codes[2];
154 unsigned char keybuf[6];
155 int got_data = 0;
156 int ret;
157 int failures = 0;
158 unsigned char sendbuf[1] = { 0 };
159 struct IR_rx *rx = ir->rx;
160
161 if (rx == NULL)
162 return -ENXIO;
163
164 if (lirc_buffer_full(&rx->buf)) {
165 dprintk("buffer overflow\n");
166 return -EOVERFLOW;
167 }
168
169 /*
170 * service the device as long as it is returning
171 * data and we have space
172 */
173 do {
174 if (kthread_should_stop())
175 return -ENODATA;
176
177 /*
178 * Lock i2c bus for the duration. RX/TX chips interfere so
179 * this is worth it
180 */
181 mutex_lock(&ir->ir_lock);
182
183 if (kthread_should_stop()) {
184 mutex_unlock(&ir->ir_lock);
185 return -ENODATA;
186 }
187
188 /*
189 * Send random "poll command" (?) Windows driver does this
190 * and it is a good point to detect chip failure.
191 */
192 ret = i2c_master_send(rx->c, sendbuf, 1);
193 if (ret != 1) {
194 zilog_error("i2c_master_send failed with %d\n", ret);
195 if (failures >= 3) {
196 mutex_unlock(&ir->ir_lock);
197 zilog_error("unable to read from the IR chip "
198 "after 3 resets, giving up\n");
199 return ret;
200 }
201
202 /* Looks like the chip crashed, reset it */
203 zilog_error("polling the IR receiver chip failed, "
204 "trying reset\n");
205
206 set_current_state(TASK_UNINTERRUPTIBLE);
207 if (kthread_should_stop()) {
208 mutex_unlock(&ir->ir_lock);
209 return -ENODATA;
210 }
211 schedule_timeout((100 * HZ + 999) / 1000);
212 ir->tx->need_boot = 1;
213
214 ++failures;
215 mutex_unlock(&ir->ir_lock);
216 continue;
217 }
218
219 if (kthread_should_stop()) {
220 mutex_unlock(&ir->ir_lock);
221 return -ENODATA;
222 }
223 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
224 mutex_unlock(&ir->ir_lock);
225 if (ret != sizeof(keybuf)) {
226 zilog_error("i2c_master_recv failed with %d -- "
227 "keeping last read buffer\n", ret);
228 } else {
229 rx->b[0] = keybuf[3];
230 rx->b[1] = keybuf[4];
231 rx->b[2] = keybuf[5];
232 dprintk("key (0x%02x/0x%02x)\n", rx->b[0], rx->b[1]);
233 }
234
235 /* key pressed ? */
236 if (rx->hdpvr_data_fmt) {
237 if (got_data && (keybuf[0] == 0x80))
238 return 0;
239 else if (got_data && (keybuf[0] == 0x00))
240 return -ENODATA;
241 } else if ((rx->b[0] & 0x80) == 0)
242 return got_data ? 0 : -ENODATA;
243
244 /* look what we have */
245 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
246
247 codes[0] = (code >> 8) & 0xff;
248 codes[1] = code & 0xff;
249
250 /* return it */
251 lirc_buffer_write(&rx->buf, codes);
252 ++got_data;
253 } while (!lirc_buffer_full(&rx->buf));
254
255 return 0;
256 }
257
258 /*
259 * Main function of the polling thread -- from lirc_dev.
260 * We don't fit the LIRC model at all anymore. This is horrible, but
261 * basically we have a single RX/TX device with a nasty failure mode
262 * that needs to be accounted for across the pair. lirc lets us provide
263 * fops, but prevents us from using the internal polling, etc. if we do
264 * so. Hence the replication. Might be neater to extend the LIRC model
265 * to account for this but I'd think it's a very special case of seriously
266 * messed up hardware.
267 */
268 static int lirc_thread(void *arg)
269 {
270 struct IR *ir = arg;
271 struct IR_rx *rx = ir->rx;
272
273 dprintk("poll thread started\n");
274
275 while (!kthread_should_stop()) {
276 set_current_state(TASK_INTERRUPTIBLE);
277
278 /* if device not opened, we can sleep half a second */
279 if (!ir->open) {
280 schedule_timeout(HZ/2);
281 continue;
282 }
283
284 /*
285 * This is ~113*2 + 24 + jitter (2*repeat gap + code length).
286 * We use this interval as the chip resets every time you poll
287 * it (bad!). This is therefore just sufficient to catch all
288 * of the button presses. It makes the remote much more
289 * responsive. You can see the difference by running irw and
290 * holding down a button. With 100ms, the old polling
291 * interval, you'll notice breaks in the repeat sequence
292 * corresponding to lost keypresses.
293 */
294 schedule_timeout((260 * HZ) / 1000);
295 if (kthread_should_stop())
296 break;
297 if (!add_to_buf(ir))
298 wake_up_interruptible(&rx->buf.wait_poll);
299 }
300
301 dprintk("poll thread ended\n");
302 return 0;
303 }
304
305 static int set_use_inc(void *data)
306 {
307 struct IR *ir = data;
308
309 if (ir->l.owner == NULL || try_module_get(ir->l.owner) == 0)
310 return -ENODEV;
311
312 /* lock bttv in memory while /dev/lirc is in use */
313 /*
314 * this is completely broken code. lirc_unregister_driver()
315 * must be possible even when the device is open
316 */
317 if (ir->rx != NULL)
318 i2c_use_client(ir->rx->c);
319 if (ir->tx != NULL)
320 i2c_use_client(ir->tx->c);
321
322 return 0;
323 }
324
325 static void set_use_dec(void *data)
326 {
327 struct IR *ir = data;
328
329 if (ir->rx)
330 i2c_release_client(ir->rx->c);
331 if (ir->tx)
332 i2c_release_client(ir->tx->c);
333 if (ir->l.owner != NULL)
334 module_put(ir->l.owner);
335 }
336
337 /* safe read of a uint32 (always network byte order) */
338 static int read_uint32(unsigned char **data,
339 unsigned char *endp, unsigned int *val)
340 {
341 if (*data + 4 > endp)
342 return 0;
343 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
344 ((*data)[2] << 8) | (*data)[3];
345 *data += 4;
346 return 1;
347 }
348
349 /* safe read of a uint8 */
350 static int read_uint8(unsigned char **data,
351 unsigned char *endp, unsigned char *val)
352 {
353 if (*data + 1 > endp)
354 return 0;
355 *val = *((*data)++);
356 return 1;
357 }
358
359 /* safe skipping of N bytes */
360 static int skip(unsigned char **data,
361 unsigned char *endp, unsigned int distance)
362 {
363 if (*data + distance > endp)
364 return 0;
365 *data += distance;
366 return 1;
367 }
368
369 /* decompress key data into the given buffer */
370 static int get_key_data(unsigned char *buf,
371 unsigned int codeset, unsigned int key)
372 {
373 unsigned char *data, *endp, *diffs, *key_block;
374 unsigned char keys, ndiffs, id;
375 unsigned int base, lim, pos, i;
376
377 /* Binary search for the codeset */
378 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
379 pos = base + (lim >> 1);
380 data = tx_data->code_sets[pos];
381
382 if (!read_uint32(&data, tx_data->endp, &i))
383 goto corrupt;
384
385 if (i == codeset)
386 break;
387 else if (codeset > i) {
388 base = pos + 1;
389 --lim;
390 }
391 }
392 /* Not found? */
393 if (!lim)
394 return -EPROTO;
395
396 /* Set end of data block */
397 endp = pos < tx_data->num_code_sets - 1 ?
398 tx_data->code_sets[pos + 1] : tx_data->endp;
399
400 /* Read the block header */
401 if (!read_uint8(&data, endp, &keys) ||
402 !read_uint8(&data, endp, &ndiffs) ||
403 ndiffs > TX_BLOCK_SIZE || keys == 0)
404 goto corrupt;
405
406 /* Save diffs & skip */
407 diffs = data;
408 if (!skip(&data, endp, ndiffs))
409 goto corrupt;
410
411 /* Read the id of the first key */
412 if (!read_uint8(&data, endp, &id))
413 goto corrupt;
414
415 /* Unpack the first key's data */
416 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
417 if (tx_data->fixed[i] == -1) {
418 if (!read_uint8(&data, endp, &buf[i]))
419 goto corrupt;
420 } else {
421 buf[i] = (unsigned char)tx_data->fixed[i];
422 }
423 }
424
425 /* Early out key found/not found */
426 if (key == id)
427 return 0;
428 if (keys == 1)
429 return -EPROTO;
430
431 /* Sanity check */
432 key_block = data;
433 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
434 goto corrupt;
435
436 /* Binary search for the key */
437 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
438 /* Seek to block */
439 unsigned char *key_data;
440 pos = base + (lim >> 1);
441 key_data = key_block + (ndiffs + 1) * pos;
442
443 if (*key_data == key) {
444 /* skip key id */
445 ++key_data;
446
447 /* found, so unpack the diffs */
448 for (i = 0; i < ndiffs; ++i) {
449 unsigned char val;
450 if (!read_uint8(&key_data, endp, &val) ||
451 diffs[i] >= TX_BLOCK_SIZE)
452 goto corrupt;
453 buf[diffs[i]] = val;
454 }
455
456 return 0;
457 } else if (key > *key_data) {
458 base = pos + 1;
459 --lim;
460 }
461 }
462 /* Key not found */
463 return -EPROTO;
464
465 corrupt:
466 zilog_error("firmware is corrupt\n");
467 return -EFAULT;
468 }
469
470 /* send a block of data to the IR TX device */
471 static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
472 {
473 int i, j, ret;
474 unsigned char buf[5];
475
476 for (i = 0; i < TX_BLOCK_SIZE;) {
477 int tosend = TX_BLOCK_SIZE - i;
478 if (tosend > 4)
479 tosend = 4;
480 buf[0] = (unsigned char)(i + 1);
481 for (j = 0; j < tosend; ++j)
482 buf[1 + j] = data_block[i + j];
483 dprintk("%02x %02x %02x %02x %02x",
484 buf[0], buf[1], buf[2], buf[3], buf[4]);
485 ret = i2c_master_send(tx->c, buf, tosend + 1);
486 if (ret != tosend + 1) {
487 zilog_error("i2c_master_send failed with %d\n", ret);
488 return ret < 0 ? ret : -EFAULT;
489 }
490 i += tosend;
491 }
492 return 0;
493 }
494
495 /* send boot data to the IR TX device */
496 static int send_boot_data(struct IR_tx *tx)
497 {
498 int ret;
499 unsigned char buf[4];
500
501 /* send the boot block */
502 ret = send_data_block(tx, tx_data->boot_data);
503 if (ret != 0)
504 return ret;
505
506 /* kick it off? */
507 buf[0] = 0x00;
508 buf[1] = 0x20;
509 ret = i2c_master_send(tx->c, buf, 2);
510 if (ret != 2) {
511 zilog_error("i2c_master_send failed with %d\n", ret);
512 return ret < 0 ? ret : -EFAULT;
513 }
514 ret = i2c_master_send(tx->c, buf, 1);
515 if (ret != 1) {
516 zilog_error("i2c_master_send failed with %d\n", ret);
517 return ret < 0 ? ret : -EFAULT;
518 }
519
520 /* Here comes the firmware version... (hopefully) */
521 ret = i2c_master_recv(tx->c, buf, 4);
522 if (ret != 4) {
523 zilog_error("i2c_master_recv failed with %d\n", ret);
524 return 0;
525 }
526 if (buf[0] != 0x80) {
527 zilog_error("unexpected IR TX response: %02x\n", buf[0]);
528 return 0;
529 }
530 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
531 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
532
533 return 0;
534 }
535
536 /* unload "firmware", lock held */
537 static void fw_unload_locked(void)
538 {
539 if (tx_data) {
540 if (tx_data->code_sets)
541 vfree(tx_data->code_sets);
542
543 if (tx_data->datap)
544 vfree(tx_data->datap);
545
546 vfree(tx_data);
547 tx_data = NULL;
548 dprintk("successfully unloaded IR blaster firmware\n");
549 }
550 }
551
552 /* unload "firmware" for the IR TX device */
553 static void fw_unload(void)
554 {
555 mutex_lock(&tx_data_lock);
556 fw_unload_locked();
557 mutex_unlock(&tx_data_lock);
558 }
559
560 /* load "firmware" for the IR TX device */
561 static int fw_load(struct IR_tx *tx)
562 {
563 int ret;
564 unsigned int i;
565 unsigned char *data, version, num_global_fixed;
566 const struct firmware *fw_entry;
567
568 /* Already loaded? */
569 mutex_lock(&tx_data_lock);
570 if (tx_data) {
571 ret = 0;
572 goto out;
573 }
574
575 /* Request codeset data file */
576 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
577 if (ret != 0) {
578 zilog_error("firmware haup-ir-blaster.bin not available "
579 "(%d)\n", ret);
580 ret = ret < 0 ? ret : -EFAULT;
581 goto out;
582 }
583 dprintk("firmware of size %zu loaded\n", fw_entry->size);
584
585 /* Parse the file */
586 tx_data = vmalloc(sizeof(*tx_data));
587 if (tx_data == NULL) {
588 zilog_error("out of memory\n");
589 release_firmware(fw_entry);
590 ret = -ENOMEM;
591 goto out;
592 }
593 tx_data->code_sets = NULL;
594
595 /* Copy the data so hotplug doesn't get confused and timeout */
596 tx_data->datap = vmalloc(fw_entry->size);
597 if (tx_data->datap == NULL) {
598 zilog_error("out of memory\n");
599 release_firmware(fw_entry);
600 vfree(tx_data);
601 ret = -ENOMEM;
602 goto out;
603 }
604 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
605 tx_data->endp = tx_data->datap + fw_entry->size;
606 release_firmware(fw_entry); fw_entry = NULL;
607
608 /* Check version */
609 data = tx_data->datap;
610 if (!read_uint8(&data, tx_data->endp, &version))
611 goto corrupt;
612 if (version != 1) {
613 zilog_error("unsupported code set file version (%u, expected"
614 "1) -- please upgrade to a newer driver",
615 version);
616 fw_unload_locked();
617 ret = -EFAULT;
618 goto out;
619 }
620
621 /* Save boot block for later */
622 tx_data->boot_data = data;
623 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
624 goto corrupt;
625
626 if (!read_uint32(&data, tx_data->endp,
627 &tx_data->num_code_sets))
628 goto corrupt;
629
630 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
631
632 tx_data->code_sets = vmalloc(
633 tx_data->num_code_sets * sizeof(char *));
634 if (tx_data->code_sets == NULL) {
635 fw_unload_locked();
636 ret = -ENOMEM;
637 goto out;
638 }
639
640 for (i = 0; i < TX_BLOCK_SIZE; ++i)
641 tx_data->fixed[i] = -1;
642
643 /* Read global fixed data template */
644 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
645 num_global_fixed > TX_BLOCK_SIZE)
646 goto corrupt;
647 for (i = 0; i < num_global_fixed; ++i) {
648 unsigned char pos, val;
649 if (!read_uint8(&data, tx_data->endp, &pos) ||
650 !read_uint8(&data, tx_data->endp, &val) ||
651 pos >= TX_BLOCK_SIZE)
652 goto corrupt;
653 tx_data->fixed[pos] = (int)val;
654 }
655
656 /* Filch out the position of each code set */
657 for (i = 0; i < tx_data->num_code_sets; ++i) {
658 unsigned int id;
659 unsigned char keys;
660 unsigned char ndiffs;
661
662 /* Save the codeset position */
663 tx_data->code_sets[i] = data;
664
665 /* Read header */
666 if (!read_uint32(&data, tx_data->endp, &id) ||
667 !read_uint8(&data, tx_data->endp, &keys) ||
668 !read_uint8(&data, tx_data->endp, &ndiffs) ||
669 ndiffs > TX_BLOCK_SIZE || keys == 0)
670 goto corrupt;
671
672 /* skip diff positions */
673 if (!skip(&data, tx_data->endp, ndiffs))
674 goto corrupt;
675
676 /*
677 * After the diffs we have the first key id + data -
678 * global fixed
679 */
680 if (!skip(&data, tx_data->endp,
681 1 + TX_BLOCK_SIZE - num_global_fixed))
682 goto corrupt;
683
684 /* Then we have keys-1 blocks of key id+diffs */
685 if (!skip(&data, tx_data->endp,
686 (ndiffs + 1) * (keys - 1)))
687 goto corrupt;
688 }
689 ret = 0;
690 goto out;
691
692 corrupt:
693 zilog_error("firmware is corrupt\n");
694 fw_unload_locked();
695 ret = -EFAULT;
696
697 out:
698 mutex_unlock(&tx_data_lock);
699 return ret;
700 }
701
702 /* initialise the IR TX device */
703 static int tx_init(struct IR_tx *tx)
704 {
705 int ret;
706
707 /* Load 'firmware' */
708 ret = fw_load(tx);
709 if (ret != 0)
710 return ret;
711
712 /* Send boot block */
713 ret = send_boot_data(tx);
714 if (ret != 0)
715 return ret;
716 tx->need_boot = 0;
717
718 /* Looks good */
719 return 0;
720 }
721
722 /* do nothing stub to make LIRC happy */
723 static loff_t lseek(struct file *filep, loff_t offset, int orig)
724 {
725 return -ESPIPE;
726 }
727
728 /* copied from lirc_dev */
729 static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
730 {
731 struct IR *ir = filep->private_data;
732 struct IR_rx *rx = ir->rx;
733 int ret = 0, written = 0;
734 DECLARE_WAITQUEUE(wait, current);
735
736 dprintk("read called\n");
737 if (rx == NULL)
738 return -ENODEV;
739
740 if (mutex_lock_interruptible(&rx->buf_lock))
741 return -ERESTARTSYS;
742
743 if (n % rx->buf.chunk_size) {
744 dprintk("read result = -EINVAL\n");
745 mutex_unlock(&rx->buf_lock);
746 return -EINVAL;
747 }
748
749 /*
750 * we add ourselves to the task queue before buffer check
751 * to avoid losing scan code (in case when queue is awaken somewhere
752 * between while condition checking and scheduling)
753 */
754 add_wait_queue(&rx->buf.wait_poll, &wait);
755 set_current_state(TASK_INTERRUPTIBLE);
756
757 /*
758 * while we didn't provide 'length' bytes, device is opened in blocking
759 * mode and 'copy_to_user' is happy, wait for data.
760 */
761 while (written < n && ret == 0) {
762 if (lirc_buffer_empty(&rx->buf)) {
763 /*
764 * According to the read(2) man page, 'written' can be
765 * returned as less than 'n', instead of blocking
766 * again, returning -EWOULDBLOCK, or returning
767 * -ERESTARTSYS
768 */
769 if (written)
770 break;
771 if (filep->f_flags & O_NONBLOCK) {
772 ret = -EWOULDBLOCK;
773 break;
774 }
775 if (signal_pending(current)) {
776 ret = -ERESTARTSYS;
777 break;
778 }
779 schedule();
780 set_current_state(TASK_INTERRUPTIBLE);
781 } else {
782 unsigned char buf[rx->buf.chunk_size];
783 lirc_buffer_read(&rx->buf, buf);
784 ret = copy_to_user((void *)outbuf+written, buf,
785 rx->buf.chunk_size);
786 written += rx->buf.chunk_size;
787 }
788 }
789
790 remove_wait_queue(&rx->buf.wait_poll, &wait);
791 set_current_state(TASK_RUNNING);
792 mutex_unlock(&rx->buf_lock);
793
794 dprintk("read result = %s (%d)\n",
795 ret ? "-EFAULT" : "OK", ret);
796
797 return ret ? ret : written;
798 }
799
800 /* send a keypress to the IR TX device */
801 static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
802 {
803 unsigned char data_block[TX_BLOCK_SIZE];
804 unsigned char buf[2];
805 int i, ret;
806
807 /* Get data for the codeset/key */
808 ret = get_key_data(data_block, code, key);
809
810 if (ret == -EPROTO) {
811 zilog_error("failed to get data for code %u, key %u -- check "
812 "lircd.conf entries\n", code, key);
813 return ret;
814 } else if (ret != 0)
815 return ret;
816
817 /* Send the data block */
818 ret = send_data_block(tx, data_block);
819 if (ret != 0)
820 return ret;
821
822 /* Send data block length? */
823 buf[0] = 0x00;
824 buf[1] = 0x40;
825 ret = i2c_master_send(tx->c, buf, 2);
826 if (ret != 2) {
827 zilog_error("i2c_master_send failed with %d\n", ret);
828 return ret < 0 ? ret : -EFAULT;
829 }
830 ret = i2c_master_send(tx->c, buf, 1);
831 if (ret != 1) {
832 zilog_error("i2c_master_send failed with %d\n", ret);
833 return ret < 0 ? ret : -EFAULT;
834 }
835
836 /* Send finished download? */
837 ret = i2c_master_recv(tx->c, buf, 1);
838 if (ret != 1) {
839 zilog_error("i2c_master_recv failed with %d\n", ret);
840 return ret < 0 ? ret : -EFAULT;
841 }
842 if (buf[0] != 0xA0) {
843 zilog_error("unexpected IR TX response #1: %02x\n",
844 buf[0]);
845 return -EFAULT;
846 }
847
848 /* Send prepare command? */
849 buf[0] = 0x00;
850 buf[1] = 0x80;
851 ret = i2c_master_send(tx->c, buf, 2);
852 if (ret != 2) {
853 zilog_error("i2c_master_send failed with %d\n", ret);
854 return ret < 0 ? ret : -EFAULT;
855 }
856
857 /*
858 * The sleep bits aren't necessary on the HD PVR, and in fact, the
859 * last i2c_master_recv always fails with a -5, so for now, we're
860 * going to skip this whole mess and say we're done on the HD PVR
861 */
862 if (!tx->post_tx_ready_poll) {
863 dprintk("sent code %u, key %u\n", code, key);
864 return 0;
865 }
866
867 /*
868 * This bit NAKs until the device is ready, so we retry it
869 * sleeping a bit each time. This seems to be what the windows
870 * driver does, approximately.
871 * Try for up to 1s.
872 */
873 for (i = 0; i < 20; ++i) {
874 set_current_state(TASK_UNINTERRUPTIBLE);
875 schedule_timeout((50 * HZ + 999) / 1000);
876 ret = i2c_master_send(tx->c, buf, 1);
877 if (ret == 1)
878 break;
879 dprintk("NAK expected: i2c_master_send "
880 "failed with %d (try %d)\n", ret, i+1);
881 }
882 if (ret != 1) {
883 zilog_error("IR TX chip never got ready: last i2c_master_send "
884 "failed with %d\n", ret);
885 return ret < 0 ? ret : -EFAULT;
886 }
887
888 /* Seems to be an 'ok' response */
889 i = i2c_master_recv(tx->c, buf, 1);
890 if (i != 1) {
891 zilog_error("i2c_master_recv failed with %d\n", ret);
892 return -EFAULT;
893 }
894 if (buf[0] != 0x80) {
895 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
896 return -EFAULT;
897 }
898
899 /* Oh good, it worked */
900 dprintk("sent code %u, key %u\n", code, key);
901 return 0;
902 }
903
904 /*
905 * Write a code to the device. We take in a 32-bit number (an int) and then
906 * decode this to a codeset/key index. The key data is then decompressed and
907 * sent to the device. We have a spin lock as per i2c documentation to prevent
908 * multiple concurrent sends which would probably cause the device to explode.
909 */
910 static ssize_t write(struct file *filep, const char *buf, size_t n,
911 loff_t *ppos)
912 {
913 struct IR *ir = filep->private_data;
914 struct IR_tx *tx = ir->tx;
915 size_t i;
916 int failures = 0;
917
918 if (tx == NULL)
919 return -ENODEV;
920
921 /* Validate user parameters */
922 if (n % sizeof(int))
923 return -EINVAL;
924
925 /* Lock i2c bus for the duration */
926 mutex_lock(&ir->ir_lock);
927
928 /* Send each keypress */
929 for (i = 0; i < n;) {
930 int ret = 0;
931 int command;
932
933 if (copy_from_user(&command, buf + i, sizeof(command))) {
934 mutex_unlock(&ir->ir_lock);
935 return -EFAULT;
936 }
937
938 /* Send boot data first if required */
939 if (tx->need_boot == 1) {
940 ret = send_boot_data(tx);
941 if (ret == 0)
942 tx->need_boot = 0;
943 }
944
945 /* Send the code */
946 if (ret == 0) {
947 ret = send_code(tx, (unsigned)command >> 16,
948 (unsigned)command & 0xFFFF);
949 if (ret == -EPROTO) {
950 mutex_unlock(&ir->ir_lock);
951 return ret;
952 }
953 }
954
955 /*
956 * Hmm, a failure. If we've had a few then give up, otherwise
957 * try a reset
958 */
959 if (ret != 0) {
960 /* Looks like the chip crashed, reset it */
961 zilog_error("sending to the IR transmitter chip "
962 "failed, trying reset\n");
963
964 if (failures >= 3) {
965 zilog_error("unable to send to the IR chip "
966 "after 3 resets, giving up\n");
967 mutex_unlock(&ir->ir_lock);
968 return ret;
969 }
970 set_current_state(TASK_UNINTERRUPTIBLE);
971 schedule_timeout((100 * HZ + 999) / 1000);
972 tx->need_boot = 1;
973 ++failures;
974 } else
975 i += sizeof(int);
976 }
977
978 /* Release i2c bus */
979 mutex_unlock(&ir->ir_lock);
980
981 /* All looks good */
982 return n;
983 }
984
985 /* copied from lirc_dev */
986 static unsigned int poll(struct file *filep, poll_table *wait)
987 {
988 struct IR *ir = filep->private_data;
989 struct IR_rx *rx = ir->rx;
990 unsigned int ret;
991
992 dprintk("poll called\n");
993 if (rx == NULL)
994 return -ENODEV;
995
996 mutex_lock(&rx->buf_lock);
997
998 poll_wait(filep, &rx->buf.wait_poll, wait);
999
1000 dprintk("poll result = %s\n",
1001 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1002
1003 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1004
1005 mutex_unlock(&rx->buf_lock);
1006 return ret;
1007 }
1008
1009 static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1010 {
1011 struct IR *ir = filep->private_data;
1012 int result;
1013 unsigned long mode, features = 0;
1014
1015 features |= LIRC_CAN_SEND_PULSE;
1016 if (ir->rx != NULL)
1017 features |= LIRC_CAN_REC_LIRCCODE;
1018
1019 switch (cmd) {
1020 case LIRC_GET_LENGTH:
1021 result = put_user((unsigned long)13,
1022 (unsigned long *)arg);
1023 break;
1024 case LIRC_GET_FEATURES:
1025 result = put_user(features, (unsigned long *) arg);
1026 break;
1027 case LIRC_GET_REC_MODE:
1028 if (!(features&LIRC_CAN_REC_MASK))
1029 return -ENOSYS;
1030
1031 result = put_user(LIRC_REC2MODE
1032 (features&LIRC_CAN_REC_MASK),
1033 (unsigned long *)arg);
1034 break;
1035 case LIRC_SET_REC_MODE:
1036 if (!(features&LIRC_CAN_REC_MASK))
1037 return -ENOSYS;
1038
1039 result = get_user(mode, (unsigned long *)arg);
1040 if (!result && !(LIRC_MODE2REC(mode) & features))
1041 result = -EINVAL;
1042 break;
1043 case LIRC_GET_SEND_MODE:
1044 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1045 break;
1046 case LIRC_SET_SEND_MODE:
1047 result = get_user(mode, (unsigned long *) arg);
1048 if (!result && mode != LIRC_MODE_PULSE)
1049 return -EINVAL;
1050 break;
1051 default:
1052 return -EINVAL;
1053 }
1054 return result;
1055 }
1056
1057 /* ir_devices_lock must be held */
1058 static struct IR *find_ir_device_by_minor(unsigned int minor)
1059 {
1060 if (minor >= MAX_IRCTL_DEVICES)
1061 return NULL;
1062
1063 return ir_devices[minor];
1064 }
1065
1066 /*
1067 * Open the IR device. Get hold of our IR structure and
1068 * stash it in private_data for the file
1069 */
1070 static int open(struct inode *node, struct file *filep)
1071 {
1072 struct IR *ir;
1073 int ret;
1074 unsigned int minor = MINOR(node->i_rdev);
1075
1076 /* find our IR struct */
1077 mutex_lock(&ir_devices_lock);
1078 ir = find_ir_device_by_minor(minor);
1079 mutex_unlock(&ir_devices_lock);
1080
1081 if (ir == NULL)
1082 return -ENODEV;
1083
1084 /* increment in use count */
1085 mutex_lock(&ir->ir_lock);
1086 ++ir->open;
1087 ret = set_use_inc(ir);
1088 if (ret != 0) {
1089 --ir->open;
1090 mutex_unlock(&ir->ir_lock);
1091 return ret;
1092 }
1093 mutex_unlock(&ir->ir_lock);
1094
1095 /* stash our IR struct */
1096 filep->private_data = ir;
1097
1098 return 0;
1099 }
1100
1101 /* Close the IR device */
1102 static int close(struct inode *node, struct file *filep)
1103 {
1104 /* find our IR struct */
1105 struct IR *ir = filep->private_data;
1106 if (ir == NULL) {
1107 zilog_error("close: no private_data attached to the file!\n");
1108 return -ENODEV;
1109 }
1110
1111 /* decrement in use count */
1112 mutex_lock(&ir->ir_lock);
1113 --ir->open;
1114 set_use_dec(ir);
1115 mutex_unlock(&ir->ir_lock);
1116
1117 return 0;
1118 }
1119
1120 static struct lirc_driver lirc_template = {
1121 .name = "lirc_zilog",
1122 .set_use_inc = set_use_inc,
1123 .set_use_dec = set_use_dec,
1124 .owner = THIS_MODULE
1125 };
1126
1127 static int ir_remove(struct i2c_client *client);
1128 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1129
1130 #define ID_FLAG_TX 0x01
1131 #define ID_FLAG_HDPVR 0x02
1132
1133 static const struct i2c_device_id ir_transceiver_id[] = {
1134 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1135 { "ir_rx_z8f0811_haup", 0 },
1136 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1137 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1138 { }
1139 };
1140
1141 static struct i2c_driver driver = {
1142 .driver = {
1143 .owner = THIS_MODULE,
1144 .name = "Zilog/Hauppauge i2c IR",
1145 },
1146 .probe = ir_probe,
1147 .remove = ir_remove,
1148 .id_table = ir_transceiver_id,
1149 };
1150
1151 static const struct file_operations lirc_fops = {
1152 .owner = THIS_MODULE,
1153 .llseek = lseek,
1154 .read = read,
1155 .write = write,
1156 .poll = poll,
1157 .unlocked_ioctl = ioctl,
1158 #ifdef CONFIG_COMPAT
1159 .compat_ioctl = ioctl,
1160 #endif
1161 .open = open,
1162 .release = close
1163 };
1164
1165 static void destroy_rx_kthread(struct IR_rx *rx)
1166 {
1167 /* end up polling thread */
1168 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1169 kthread_stop(rx->task);
1170 rx->task = NULL;
1171 }
1172 }
1173
1174 /* ir_devices_lock must be held */
1175 static int add_ir_device(struct IR *ir)
1176 {
1177 int i;
1178
1179 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1180 if (ir_devices[i] == NULL) {
1181 ir_devices[i] = ir;
1182 break;
1183 }
1184
1185 return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1186 }
1187
1188 /* ir_devices_lock must be held */
1189 static void del_ir_device(struct IR *ir)
1190 {
1191 int i;
1192
1193 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1194 if (ir_devices[i] == ir) {
1195 ir_devices[i] = NULL;
1196 break;
1197 }
1198 }
1199
1200 static int ir_remove(struct i2c_client *client)
1201 {
1202 struct IR *ir = i2c_get_clientdata(client);
1203
1204 mutex_lock(&ir_devices_lock);
1205
1206 if (ir == NULL) {
1207 /* We destroyed everything when the first client came through */
1208 mutex_unlock(&ir_devices_lock);
1209 return 0;
1210 }
1211
1212 /* Good-bye LIRC */
1213 lirc_unregister_driver(ir->l.minor);
1214
1215 /* Good-bye Rx */
1216 destroy_rx_kthread(ir->rx);
1217 if (ir->rx != NULL) {
1218 if (ir->rx->buf.fifo_initialized)
1219 lirc_buffer_free(&ir->rx->buf);
1220 i2c_set_clientdata(ir->rx->c, NULL);
1221 kfree(ir->rx);
1222 }
1223
1224 /* Good-bye Tx */
1225 i2c_set_clientdata(ir->tx->c, NULL);
1226 kfree(ir->tx);
1227
1228 /* Good-bye IR */
1229 del_ir_device(ir);
1230 kfree(ir);
1231
1232 mutex_unlock(&ir_devices_lock);
1233 return 0;
1234 }
1235
1236
1237 /* ir_devices_lock must be held */
1238 static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1239 {
1240 int i;
1241 struct IR *ir = NULL;
1242
1243 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1244 if (ir_devices[i] != NULL &&
1245 ir_devices[i]->adapter == adapter) {
1246 ir = ir_devices[i];
1247 break;
1248 }
1249
1250 return ir;
1251 }
1252
1253 static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1254 {
1255 struct IR *ir;
1256 struct i2c_adapter *adap = client->adapter;
1257 int ret;
1258 bool tx_probe = false;
1259
1260 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1261 __func__, id->name, adap->nr, adap->name, client->addr);
1262
1263 /*
1264 * The IR receiver is at i2c address 0x71.
1265 * The IR transmitter is at i2c address 0x70.
1266 */
1267
1268 if (id->driver_data & ID_FLAG_TX)
1269 tx_probe = true;
1270 else if (tx_only) /* module option */
1271 return -ENXIO;
1272
1273 zilog_info("probing IR %s on %s (i2c-%d)\n",
1274 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1275
1276 mutex_lock(&ir_devices_lock);
1277
1278 /* Use a single struct IR instance for both the Rx and Tx functions */
1279 ir = find_ir_device_by_adapter(adap);
1280 if (ir == NULL) {
1281 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1282 if (ir == NULL) {
1283 ret = -ENOMEM;
1284 goto out_no_ir;
1285 }
1286 /* store for use in ir_probe() again, and open() later on */
1287 ret = add_ir_device(ir);
1288 if (ret)
1289 goto out_free_ir;
1290
1291 ir->adapter = adap;
1292 mutex_init(&ir->ir_lock);
1293
1294 /* set lirc_dev stuff */
1295 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1296 ir->l.minor = minor; /* module option */
1297 ir->l.code_length = 13;
1298 ir->l.rbuf = NULL;
1299 ir->l.fops = &lirc_fops;
1300 ir->l.data = ir;
1301 ir->l.dev = &adap->dev;
1302 ir->l.sample_rate = 0;
1303 }
1304
1305 if (tx_probe) {
1306 /* Set up a struct IR_tx instance */
1307 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1308 if (ir->tx == NULL) {
1309 ret = -ENOMEM;
1310 goto out_free_xx;
1311 }
1312
1313 ir->tx->c = client;
1314 ir->tx->need_boot = 1;
1315 ir->tx->post_tx_ready_poll =
1316 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1317 } else {
1318 /* Set up a struct IR_rx instance */
1319 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1320 if (ir->rx == NULL) {
1321 ret = -ENOMEM;
1322 goto out_free_xx;
1323 }
1324
1325 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1326 if (ret)
1327 goto out_free_xx;
1328
1329 mutex_init(&ir->rx->buf_lock);
1330 ir->rx->c = client;
1331 ir->rx->hdpvr_data_fmt =
1332 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1333
1334 /* set lirc_dev stuff */
1335 ir->l.rbuf = &ir->rx->buf;
1336 }
1337
1338 i2c_set_clientdata(client, ir);
1339
1340 /* Proceed only if we have the required Tx and Rx clients ready to go */
1341 if (ir->tx == NULL ||
1342 (ir->rx == NULL && !tx_only)) {
1343 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1344 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1345 adap->nr, tx_probe ? "Rx" : "Tx");
1346 goto out_ok;
1347 }
1348
1349 /* initialise RX device */
1350 if (ir->rx != NULL) {
1351 /* try to fire up polling thread */
1352 ir->rx->task = kthread_run(lirc_thread, ir,
1353 "zilog-rx-i2c-%d", adap->nr);
1354 if (IS_ERR(ir->rx->task)) {
1355 ret = PTR_ERR(ir->rx->task);
1356 zilog_error("%s: could not start IR Rx polling thread"
1357 "\n", __func__);
1358 goto out_free_xx;
1359 }
1360 }
1361
1362 /* register with lirc */
1363 ir->l.minor = lirc_register_driver(&ir->l);
1364 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1365 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1366 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1367 ret = -EBADRQC;
1368 goto out_free_thread;
1369 }
1370
1371 /*
1372 * if we have the tx device, load the 'firmware'. We do this
1373 * after registering with lirc as otherwise hotplug seems to take
1374 * 10s to create the lirc device.
1375 */
1376 ret = tx_init(ir->tx);
1377 if (ret != 0)
1378 goto out_unregister;
1379
1380 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1381 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1382 out_ok:
1383 mutex_unlock(&ir_devices_lock);
1384 return 0;
1385
1386 out_unregister:
1387 lirc_unregister_driver(ir->l.minor);
1388 out_free_thread:
1389 destroy_rx_kthread(ir->rx);
1390 out_free_xx:
1391 if (ir->rx != NULL) {
1392 if (ir->rx->buf.fifo_initialized)
1393 lirc_buffer_free(&ir->rx->buf);
1394 if (ir->rx->c != NULL)
1395 i2c_set_clientdata(ir->rx->c, NULL);
1396 kfree(ir->rx);
1397 }
1398 if (ir->tx != NULL) {
1399 if (ir->tx->c != NULL)
1400 i2c_set_clientdata(ir->tx->c, NULL);
1401 kfree(ir->tx);
1402 }
1403 out_free_ir:
1404 del_ir_device(ir);
1405 kfree(ir);
1406 out_no_ir:
1407 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1408 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1409 ret);
1410 mutex_unlock(&ir_devices_lock);
1411 return ret;
1412 }
1413
1414 static int __init zilog_init(void)
1415 {
1416 int ret;
1417
1418 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1419
1420 mutex_init(&tx_data_lock);
1421 mutex_init(&ir_devices_lock);
1422
1423 request_module("firmware_class");
1424
1425 ret = i2c_add_driver(&driver);
1426 if (ret)
1427 zilog_error("initialization failed\n");
1428 else
1429 zilog_notify("initialization complete\n");
1430
1431 return ret;
1432 }
1433
1434 static void __exit zilog_exit(void)
1435 {
1436 i2c_del_driver(&driver);
1437 /* if loaded */
1438 fw_unload();
1439 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1440 }
1441
1442 module_init(zilog_init);
1443 module_exit(zilog_exit);
1444
1445 MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1446 MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1447 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1448 "Andy Walls");
1449 MODULE_LICENSE("GPL");
1450 /* for compat with old name, which isn't all that accurate anymore */
1451 MODULE_ALIAS("lirc_pvr150");
1452
1453 module_param(minor, int, 0444);
1454 MODULE_PARM_DESC(minor, "Preferred minor device number");
1455
1456 module_param(debug, bool, 0644);
1457 MODULE_PARM_DESC(debug, "Enable debugging messages");
1458
1459 module_param(tx_only, bool, 0644);
1460 MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");
This page took 0.249069 seconds and 5 git commands to generate.