1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
66struct IR_rx {
67
68 struct i2c_client *c;
69
70
71 struct lirc_buffer buf;
72 struct mutex buf_lock;
73
74
75 struct task_struct *task;
76
77
78 unsigned char b[3];
79 bool hdpvr_data_fmt;
80};
81
82struct IR_tx {
83
84 struct i2c_client *c;
85
86
87 int need_boot;
88 bool post_tx_ready_poll;
89};
90
91struct 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
103static struct mutex ir_devices_lock;
104static struct IR *ir_devices[MAX_IRCTL_DEVICES];
105
106
107#define TX_BLOCK_SIZE 99
108
109
110struct tx_data_struct {
111
112 unsigned char *boot_data;
113
114
115 unsigned char *datap;
116
117
118 unsigned char *endp;
119
120
121 unsigned int num_code_sets;
122
123
124 unsigned char **code_sets;
125
126
127 int fixed[TX_BLOCK_SIZE];
128};
129
130static struct tx_data_struct *tx_data;
131static 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
139static int debug;
140static int tx_only;
141static int minor = -1;
142
143#define dprintk(fmt, args...) \
144 do { \
145 if (debug) \
146 printk(KERN_DEBUG KBUILD_MODNAME ": " fmt, \
147 ## args); \
148 } while (0)
149
150static 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
171
172
173 do {
174 if (kthread_should_stop())
175 return -ENODATA;
176
177
178
179
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
190
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
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
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
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
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
260
261
262
263
264
265
266
267
268static 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
279 if (!ir->open) {
280 schedule_timeout(HZ/2);
281 continue;
282 }
283
284
285
286
287
288
289
290
291
292
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
305static 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
313
314
315
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
325static 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
338static 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
350static 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
360static 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
370static 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
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
393 if (!lim)
394 return -EPROTO;
395
396
397 endp = pos < tx_data->num_code_sets - 1 ?
398 tx_data->code_sets[pos + 1] : tx_data->endp;
399
400
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
407 diffs = data;
408 if (!skip(&data, endp, ndiffs))
409 goto corrupt;
410
411
412 if (!read_uint8(&data, endp, &id))
413 goto corrupt;
414
415
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
426 if (key == id)
427 return 0;
428 if (keys == 1)
429 return -EPROTO;
430
431
432 key_block = data;
433 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
434 goto corrupt;
435
436
437 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
438
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
445 ++key_data;
446
447
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
463 return -EPROTO;
464
465corrupt:
466 zilog_error("firmware is corrupt\n");
467 return -EFAULT;
468}
469
470
471static 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
496static int send_boot_data(struct IR_tx *tx)
497{
498 int ret, i;
499 unsigned char buf[4];
500
501
502 ret = send_data_block(tx, tx_data->boot_data);
503 if (ret != 0)
504 return ret;
505
506
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
515
516
517
518
519
520 for (i = 0; i < 10; i++) {
521 ret = i2c_master_send(tx->c, buf, 1);
522 if (ret == 1)
523 break;
524 udelay(100);
525 }
526
527 if (ret != 1) {
528 zilog_error("i2c_master_send failed with %d\n", ret);
529 return ret < 0 ? ret : -EFAULT;
530 }
531
532
533 ret = i2c_master_recv(tx->c, buf, 4);
534 if (ret != 4) {
535 zilog_error("i2c_master_recv failed with %d\n", ret);
536 return 0;
537 }
538 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
539 zilog_error("unexpected IR TX init response: %02x\n", buf[0]);
540 return 0;
541 }
542 zilog_notify("Zilog/Hauppauge IR blaster firmware version "
543 "%d.%d.%d loaded\n", buf[1], buf[2], buf[3]);
544
545 return 0;
546}
547
548
549static void fw_unload_locked(void)
550{
551 if (tx_data) {
552 if (tx_data->code_sets)
553 vfree(tx_data->code_sets);
554
555 if (tx_data->datap)
556 vfree(tx_data->datap);
557
558 vfree(tx_data);
559 tx_data = NULL;
560 dprintk("successfully unloaded IR blaster firmware\n");
561 }
562}
563
564
565static void fw_unload(void)
566{
567 mutex_lock(&tx_data_lock);
568 fw_unload_locked();
569 mutex_unlock(&tx_data_lock);
570}
571
572
573static int fw_load(struct IR_tx *tx)
574{
575 int ret;
576 unsigned int i;
577 unsigned char *data, version, num_global_fixed;
578 const struct firmware *fw_entry;
579
580
581 mutex_lock(&tx_data_lock);
582 if (tx_data) {
583 ret = 0;
584 goto out;
585 }
586
587
588 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", &tx->c->dev);
589 if (ret != 0) {
590 zilog_error("firmware haup-ir-blaster.bin not available "
591 "(%d)\n", ret);
592 ret = ret < 0 ? ret : -EFAULT;
593 goto out;
594 }
595 dprintk("firmware of size %zu loaded\n", fw_entry->size);
596
597
598 tx_data = vmalloc(sizeof(*tx_data));
599 if (tx_data == NULL) {
600 zilog_error("out of memory\n");
601 release_firmware(fw_entry);
602 ret = -ENOMEM;
603 goto out;
604 }
605 tx_data->code_sets = NULL;
606
607
608 tx_data->datap = vmalloc(fw_entry->size);
609 if (tx_data->datap == NULL) {
610 zilog_error("out of memory\n");
611 release_firmware(fw_entry);
612 vfree(tx_data);
613 ret = -ENOMEM;
614 goto out;
615 }
616 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
617 tx_data->endp = tx_data->datap + fw_entry->size;
618 release_firmware(fw_entry); fw_entry = NULL;
619
620
621 data = tx_data->datap;
622 if (!read_uint8(&data, tx_data->endp, &version))
623 goto corrupt;
624 if (version != 1) {
625 zilog_error("unsupported code set file version (%u, expected"
626 "1) -- please upgrade to a newer driver",
627 version);
628 fw_unload_locked();
629 ret = -EFAULT;
630 goto out;
631 }
632
633
634 tx_data->boot_data = data;
635 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
636 goto corrupt;
637
638 if (!read_uint32(&data, tx_data->endp,
639 &tx_data->num_code_sets))
640 goto corrupt;
641
642 dprintk("%u IR blaster codesets loaded\n", tx_data->num_code_sets);
643
644 tx_data->code_sets = vmalloc(
645 tx_data->num_code_sets * sizeof(char *));
646 if (tx_data->code_sets == NULL) {
647 fw_unload_locked();
648 ret = -ENOMEM;
649 goto out;
650 }
651
652 for (i = 0; i < TX_BLOCK_SIZE; ++i)
653 tx_data->fixed[i] = -1;
654
655
656 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
657 num_global_fixed > TX_BLOCK_SIZE)
658 goto corrupt;
659 for (i = 0; i < num_global_fixed; ++i) {
660 unsigned char pos, val;
661 if (!read_uint8(&data, tx_data->endp, &pos) ||
662 !read_uint8(&data, tx_data->endp, &val) ||
663 pos >= TX_BLOCK_SIZE)
664 goto corrupt;
665 tx_data->fixed[pos] = (int)val;
666 }
667
668
669 for (i = 0; i < tx_data->num_code_sets; ++i) {
670 unsigned int id;
671 unsigned char keys;
672 unsigned char ndiffs;
673
674
675 tx_data->code_sets[i] = data;
676
677
678 if (!read_uint32(&data, tx_data->endp, &id) ||
679 !read_uint8(&data, tx_data->endp, &keys) ||
680 !read_uint8(&data, tx_data->endp, &ndiffs) ||
681 ndiffs > TX_BLOCK_SIZE || keys == 0)
682 goto corrupt;
683
684
685 if (!skip(&data, tx_data->endp, ndiffs))
686 goto corrupt;
687
688
689
690
691
692 if (!skip(&data, tx_data->endp,
693 1 + TX_BLOCK_SIZE - num_global_fixed))
694 goto corrupt;
695
696
697 if (!skip(&data, tx_data->endp,
698 (ndiffs + 1) * (keys - 1)))
699 goto corrupt;
700 }
701 ret = 0;
702 goto out;
703
704corrupt:
705 zilog_error("firmware is corrupt\n");
706 fw_unload_locked();
707 ret = -EFAULT;
708
709out:
710 mutex_unlock(&tx_data_lock);
711 return ret;
712}
713
714
715static int tx_init(struct IR_tx *tx)
716{
717 int ret;
718
719
720 ret = fw_load(tx);
721 if (ret != 0)
722 return ret;
723
724
725 ret = send_boot_data(tx);
726 if (ret != 0)
727 return ret;
728 tx->need_boot = 0;
729
730
731 return 0;
732}
733
734
735static loff_t lseek(struct file *filep, loff_t offset, int orig)
736{
737 return -ESPIPE;
738}
739
740
741static ssize_t read(struct file *filep, char *outbuf, size_t n, loff_t *ppos)
742{
743 struct IR *ir = filep->private_data;
744 struct IR_rx *rx = ir->rx;
745 int ret = 0, written = 0;
746 DECLARE_WAITQUEUE(wait, current);
747
748 dprintk("read called\n");
749 if (rx == NULL)
750 return -ENODEV;
751
752 if (mutex_lock_interruptible(&rx->buf_lock))
753 return -ERESTARTSYS;
754
755 if (n % rx->buf.chunk_size) {
756 dprintk("read result = -EINVAL\n");
757 mutex_unlock(&rx->buf_lock);
758 return -EINVAL;
759 }
760
761
762
763
764
765
766 add_wait_queue(&rx->buf.wait_poll, &wait);
767 set_current_state(TASK_INTERRUPTIBLE);
768
769
770
771
772
773 while (written < n && ret == 0) {
774 if (lirc_buffer_empty(&rx->buf)) {
775
776
777
778
779
780
781 if (written)
782 break;
783 if (filep->f_flags & O_NONBLOCK) {
784 ret = -EWOULDBLOCK;
785 break;
786 }
787 if (signal_pending(current)) {
788 ret = -ERESTARTSYS;
789 break;
790 }
791 schedule();
792 set_current_state(TASK_INTERRUPTIBLE);
793 } else {
794 unsigned char buf[rx->buf.chunk_size];
795 lirc_buffer_read(&rx->buf, buf);
796 ret = copy_to_user((void *)outbuf+written, buf,
797 rx->buf.chunk_size);
798 written += rx->buf.chunk_size;
799 }
800 }
801
802 remove_wait_queue(&rx->buf.wait_poll, &wait);
803 set_current_state(TASK_RUNNING);
804 mutex_unlock(&rx->buf_lock);
805
806 dprintk("read result = %s (%d)\n",
807 ret ? "-EFAULT" : "OK", ret);
808
809 return ret ? ret : written;
810}
811
812
813static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
814{
815 unsigned char data_block[TX_BLOCK_SIZE];
816 unsigned char buf[2];
817 int i, ret;
818
819
820 ret = get_key_data(data_block, code, key);
821
822 if (ret == -EPROTO) {
823 zilog_error("failed to get data for code %u, key %u -- check "
824 "lircd.conf entries\n", code, key);
825 return ret;
826 } else if (ret != 0)
827 return ret;
828
829
830 ret = send_data_block(tx, data_block);
831 if (ret != 0)
832 return ret;
833
834
835 buf[0] = 0x00;
836 buf[1] = 0x40;
837 ret = i2c_master_send(tx->c, buf, 2);
838 if (ret != 2) {
839 zilog_error("i2c_master_send failed with %d\n", ret);
840 return ret < 0 ? ret : -EFAULT;
841 }
842
843
844 for (i = 0; i < 10; i++) {
845 ret = i2c_master_send(tx->c, buf, 1);
846 if (ret == 1)
847 break;
848 udelay(100);
849 }
850
851 if (ret != 1) {
852 zilog_error("i2c_master_send failed with %d\n", ret);
853 return ret < 0 ? ret : -EFAULT;
854 }
855
856
857 ret = i2c_master_recv(tx->c, buf, 1);
858 if (ret != 1) {
859 zilog_error("i2c_master_recv failed with %d\n", ret);
860 return ret < 0 ? ret : -EFAULT;
861 }
862 if (buf[0] != 0xA0) {
863 zilog_error("unexpected IR TX response #1: %02x\n",
864 buf[0]);
865 return -EFAULT;
866 }
867
868
869 buf[0] = 0x00;
870 buf[1] = 0x80;
871 ret = i2c_master_send(tx->c, buf, 2);
872 if (ret != 2) {
873 zilog_error("i2c_master_send failed with %d\n", ret);
874 return ret < 0 ? ret : -EFAULT;
875 }
876
877
878
879
880
881
882 if (!tx->post_tx_ready_poll) {
883 dprintk("sent code %u, key %u\n", code, key);
884 return 0;
885 }
886
887
888
889
890
891
892
893 for (i = 0; i < 20; ++i) {
894 set_current_state(TASK_UNINTERRUPTIBLE);
895 schedule_timeout((50 * HZ + 999) / 1000);
896 ret = i2c_master_send(tx->c, buf, 1);
897 if (ret == 1)
898 break;
899 dprintk("NAK expected: i2c_master_send "
900 "failed with %d (try %d)\n", ret, i+1);
901 }
902 if (ret != 1) {
903 zilog_error("IR TX chip never got ready: last i2c_master_send "
904 "failed with %d\n", ret);
905 return ret < 0 ? ret : -EFAULT;
906 }
907
908
909 i = i2c_master_recv(tx->c, buf, 1);
910 if (i != 1) {
911 zilog_error("i2c_master_recv failed with %d\n", ret);
912 return -EFAULT;
913 }
914 if (buf[0] != 0x80) {
915 zilog_error("unexpected IR TX response #2: %02x\n", buf[0]);
916 return -EFAULT;
917 }
918
919
920 dprintk("sent code %u, key %u\n", code, key);
921 return 0;
922}
923
924
925
926
927
928
929
930static ssize_t write(struct file *filep, const char *buf, size_t n,
931 loff_t *ppos)
932{
933 struct IR *ir = filep->private_data;
934 struct IR_tx *tx = ir->tx;
935 size_t i;
936 int failures = 0;
937
938 if (tx == NULL)
939 return -ENODEV;
940
941
942 if (n % sizeof(int))
943 return -EINVAL;
944
945
946 mutex_lock(&ir->ir_lock);
947
948
949 for (i = 0; i < n;) {
950 int ret = 0;
951 int command;
952
953 if (copy_from_user(&command, buf + i, sizeof(command))) {
954 mutex_unlock(&ir->ir_lock);
955 return -EFAULT;
956 }
957
958
959 if (tx->need_boot == 1) {
960 ret = send_boot_data(tx);
961 if (ret == 0)
962 tx->need_boot = 0;
963 }
964
965
966 if (ret == 0) {
967 ret = send_code(tx, (unsigned)command >> 16,
968 (unsigned)command & 0xFFFF);
969 if (ret == -EPROTO) {
970 mutex_unlock(&ir->ir_lock);
971 return ret;
972 }
973 }
974
975
976
977
978
979 if (ret != 0) {
980
981 zilog_error("sending to the IR transmitter chip "
982 "failed, trying reset\n");
983
984 if (failures >= 3) {
985 zilog_error("unable to send to the IR chip "
986 "after 3 resets, giving up\n");
987 mutex_unlock(&ir->ir_lock);
988 return ret;
989 }
990 set_current_state(TASK_UNINTERRUPTIBLE);
991 schedule_timeout((100 * HZ + 999) / 1000);
992 tx->need_boot = 1;
993 ++failures;
994 } else
995 i += sizeof(int);
996 }
997
998
999 mutex_unlock(&ir->ir_lock);
1000
1001
1002 return n;
1003}
1004
1005
1006static unsigned int poll(struct file *filep, poll_table *wait)
1007{
1008 struct IR *ir = filep->private_data;
1009 struct IR_rx *rx = ir->rx;
1010 unsigned int ret;
1011
1012 dprintk("poll called\n");
1013 if (rx == NULL)
1014 return -ENODEV;
1015
1016 mutex_lock(&rx->buf_lock);
1017
1018 poll_wait(filep, &rx->buf.wait_poll, wait);
1019
1020 dprintk("poll result = %s\n",
1021 lirc_buffer_empty(&rx->buf) ? "0" : "POLLIN|POLLRDNORM");
1022
1023 ret = lirc_buffer_empty(&rx->buf) ? 0 : (POLLIN|POLLRDNORM);
1024
1025 mutex_unlock(&rx->buf_lock);
1026 return ret;
1027}
1028
1029static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1030{
1031 struct IR *ir = filep->private_data;
1032 int result;
1033 unsigned long mode, features = 0;
1034
1035 features |= LIRC_CAN_SEND_PULSE;
1036 if (ir->rx != NULL)
1037 features |= LIRC_CAN_REC_LIRCCODE;
1038
1039 switch (cmd) {
1040 case LIRC_GET_LENGTH:
1041 result = put_user((unsigned long)13,
1042 (unsigned long *)arg);
1043 break;
1044 case LIRC_GET_FEATURES:
1045 result = put_user(features, (unsigned long *) arg);
1046 break;
1047 case LIRC_GET_REC_MODE:
1048 if (!(features&LIRC_CAN_REC_MASK))
1049 return -ENOSYS;
1050
1051 result = put_user(LIRC_REC2MODE
1052 (features&LIRC_CAN_REC_MASK),
1053 (unsigned long *)arg);
1054 break;
1055 case LIRC_SET_REC_MODE:
1056 if (!(features&LIRC_CAN_REC_MASK))
1057 return -ENOSYS;
1058
1059 result = get_user(mode, (unsigned long *)arg);
1060 if (!result && !(LIRC_MODE2REC(mode) & features))
1061 result = -EINVAL;
1062 break;
1063 case LIRC_GET_SEND_MODE:
1064 result = put_user(LIRC_MODE_PULSE, (unsigned long *) arg);
1065 break;
1066 case LIRC_SET_SEND_MODE:
1067 result = get_user(mode, (unsigned long *) arg);
1068 if (!result && mode != LIRC_MODE_PULSE)
1069 return -EINVAL;
1070 break;
1071 default:
1072 return -EINVAL;
1073 }
1074 return result;
1075}
1076
1077
1078static struct IR *find_ir_device_by_minor(unsigned int minor)
1079{
1080 if (minor >= MAX_IRCTL_DEVICES)
1081 return NULL;
1082
1083 return ir_devices[minor];
1084}
1085
1086
1087
1088
1089
1090static int open(struct inode *node, struct file *filep)
1091{
1092 struct IR *ir;
1093 int ret;
1094 unsigned int minor = MINOR(node->i_rdev);
1095
1096
1097 mutex_lock(&ir_devices_lock);
1098 ir = find_ir_device_by_minor(minor);
1099 mutex_unlock(&ir_devices_lock);
1100
1101 if (ir == NULL)
1102 return -ENODEV;
1103
1104
1105 mutex_lock(&ir->ir_lock);
1106 ++ir->open;
1107 ret = set_use_inc(ir);
1108 if (ret != 0) {
1109 --ir->open;
1110 mutex_unlock(&ir->ir_lock);
1111 return ret;
1112 }
1113 mutex_unlock(&ir->ir_lock);
1114
1115
1116 filep->private_data = ir;
1117
1118 return 0;
1119}
1120
1121
1122static int close(struct inode *node, struct file *filep)
1123{
1124
1125 struct IR *ir = filep->private_data;
1126 if (ir == NULL) {
1127 zilog_error("close: no private_data attached to the file!\n");
1128 return -ENODEV;
1129 }
1130
1131
1132 mutex_lock(&ir->ir_lock);
1133 --ir->open;
1134 set_use_dec(ir);
1135 mutex_unlock(&ir->ir_lock);
1136
1137 return 0;
1138}
1139
1140static struct lirc_driver lirc_template = {
1141 .name = "lirc_zilog",
1142 .set_use_inc = set_use_inc,
1143 .set_use_dec = set_use_dec,
1144 .owner = THIS_MODULE
1145};
1146
1147static int ir_remove(struct i2c_client *client);
1148static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1149
1150#define ID_FLAG_TX 0x01
1151#define ID_FLAG_HDPVR 0x02
1152
1153static const struct i2c_device_id ir_transceiver_id[] = {
1154 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1155 { "ir_rx_z8f0811_haup", 0 },
1156 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1157 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1158 { }
1159};
1160
1161static struct i2c_driver driver = {
1162 .driver = {
1163 .owner = THIS_MODULE,
1164 .name = "Zilog/Hauppauge i2c IR",
1165 },
1166 .probe = ir_probe,
1167 .remove = ir_remove,
1168 .id_table = ir_transceiver_id,
1169};
1170
1171static const struct file_operations lirc_fops = {
1172 .owner = THIS_MODULE,
1173 .llseek = lseek,
1174 .read = read,
1175 .write = write,
1176 .poll = poll,
1177 .unlocked_ioctl = ioctl,
1178#ifdef CONFIG_COMPAT
1179 .compat_ioctl = ioctl,
1180#endif
1181 .open = open,
1182 .release = close
1183};
1184
1185static void destroy_rx_kthread(struct IR_rx *rx)
1186{
1187
1188 if (rx != NULL && !IS_ERR_OR_NULL(rx->task)) {
1189 kthread_stop(rx->task);
1190 rx->task = NULL;
1191 }
1192}
1193
1194
1195static int add_ir_device(struct IR *ir)
1196{
1197 int i;
1198
1199 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1200 if (ir_devices[i] == NULL) {
1201 ir_devices[i] = ir;
1202 break;
1203 }
1204
1205 return i == MAX_IRCTL_DEVICES ? -ENOMEM : i;
1206}
1207
1208
1209static void del_ir_device(struct IR *ir)
1210{
1211 int i;
1212
1213 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1214 if (ir_devices[i] == ir) {
1215 ir_devices[i] = NULL;
1216 break;
1217 }
1218}
1219
1220static int ir_remove(struct i2c_client *client)
1221{
1222 struct IR *ir = i2c_get_clientdata(client);
1223
1224 mutex_lock(&ir_devices_lock);
1225
1226 if (ir == NULL) {
1227
1228 mutex_unlock(&ir_devices_lock);
1229 return 0;
1230 }
1231
1232
1233 lirc_unregister_driver(ir->l.minor);
1234
1235
1236 destroy_rx_kthread(ir->rx);
1237 if (ir->rx != NULL) {
1238 if (ir->rx->buf.fifo_initialized)
1239 lirc_buffer_free(&ir->rx->buf);
1240 i2c_set_clientdata(ir->rx->c, NULL);
1241 kfree(ir->rx);
1242 }
1243
1244
1245 i2c_set_clientdata(ir->tx->c, NULL);
1246 kfree(ir->tx);
1247
1248
1249 del_ir_device(ir);
1250 kfree(ir);
1251
1252 mutex_unlock(&ir_devices_lock);
1253 return 0;
1254}
1255
1256
1257
1258static struct IR *find_ir_device_by_adapter(struct i2c_adapter *adapter)
1259{
1260 int i;
1261 struct IR *ir = NULL;
1262
1263 for (i = 0; i < MAX_IRCTL_DEVICES; i++)
1264 if (ir_devices[i] != NULL &&
1265 ir_devices[i]->adapter == adapter) {
1266 ir = ir_devices[i];
1267 break;
1268 }
1269
1270 return ir;
1271}
1272
1273static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1274{
1275 struct IR *ir;
1276 struct i2c_adapter *adap = client->adapter;
1277 int ret;
1278 bool tx_probe = false;
1279
1280 dprintk("%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1281 __func__, id->name, adap->nr, adap->name, client->addr);
1282
1283
1284
1285
1286
1287
1288 if (id->driver_data & ID_FLAG_TX)
1289 tx_probe = true;
1290 else if (tx_only)
1291 return -ENXIO;
1292
1293 zilog_info("probing IR %s on %s (i2c-%d)\n",
1294 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1295
1296 mutex_lock(&ir_devices_lock);
1297
1298
1299 ir = find_ir_device_by_adapter(adap);
1300 if (ir == NULL) {
1301 ir = kzalloc(sizeof(struct IR), GFP_KERNEL);
1302 if (ir == NULL) {
1303 ret = -ENOMEM;
1304 goto out_no_ir;
1305 }
1306
1307 ret = add_ir_device(ir);
1308 if (ret)
1309 goto out_free_ir;
1310
1311 ir->adapter = adap;
1312 mutex_init(&ir->ir_lock);
1313
1314
1315 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1316 ir->l.minor = minor;
1317 ir->l.code_length = 13;
1318 ir->l.rbuf = NULL;
1319 ir->l.fops = &lirc_fops;
1320 ir->l.data = ir;
1321 ir->l.dev = &adap->dev;
1322 ir->l.sample_rate = 0;
1323 }
1324
1325 if (tx_probe) {
1326
1327 ir->tx = kzalloc(sizeof(struct IR_tx), GFP_KERNEL);
1328 if (ir->tx == NULL) {
1329 ret = -ENOMEM;
1330 goto out_free_xx;
1331 }
1332
1333 ir->tx->c = client;
1334 ir->tx->need_boot = 1;
1335 ir->tx->post_tx_ready_poll =
1336 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1337 } else {
1338
1339 ir->rx = kzalloc(sizeof(struct IR_rx), GFP_KERNEL);
1340 if (ir->rx == NULL) {
1341 ret = -ENOMEM;
1342 goto out_free_xx;
1343 }
1344
1345 ret = lirc_buffer_init(&ir->rx->buf, 2, BUFLEN / 2);
1346 if (ret)
1347 goto out_free_xx;
1348
1349 mutex_init(&ir->rx->buf_lock);
1350 ir->rx->c = client;
1351 ir->rx->hdpvr_data_fmt =
1352 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1353
1354
1355 ir->l.rbuf = &ir->rx->buf;
1356 }
1357
1358 i2c_set_clientdata(client, ir);
1359
1360
1361 if (ir->tx == NULL ||
1362 (ir->rx == NULL && !tx_only)) {
1363 zilog_info("probe of IR %s on %s (i2c-%d) done. Waiting on "
1364 "IR %s.\n", tx_probe ? "Tx" : "Rx", adap->name,
1365 adap->nr, tx_probe ? "Rx" : "Tx");
1366 goto out_ok;
1367 }
1368
1369
1370 if (ir->rx != NULL) {
1371
1372 ir->rx->task = kthread_run(lirc_thread, ir,
1373 "zilog-rx-i2c-%d", adap->nr);
1374 if (IS_ERR(ir->rx->task)) {
1375 ret = PTR_ERR(ir->rx->task);
1376 zilog_error("%s: could not start IR Rx polling thread"
1377 "\n", __func__);
1378 goto out_free_xx;
1379 }
1380 }
1381
1382
1383 ir->l.minor = lirc_register_driver(&ir->l);
1384 if (ir->l.minor < 0 || ir->l.minor >= MAX_IRCTL_DEVICES) {
1385 zilog_error("%s: \"minor\" must be between 0 and %d (%d)!\n",
1386 __func__, MAX_IRCTL_DEVICES-1, ir->l.minor);
1387 ret = -EBADRQC;
1388 goto out_free_thread;
1389 }
1390
1391
1392
1393
1394
1395
1396 ret = tx_init(ir->tx);
1397 if (ret != 0)
1398 goto out_unregister;
1399
1400 zilog_info("probe of IR %s on %s (i2c-%d) done. IR unit ready.\n",
1401 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1402out_ok:
1403 mutex_unlock(&ir_devices_lock);
1404 return 0;
1405
1406out_unregister:
1407 lirc_unregister_driver(ir->l.minor);
1408out_free_thread:
1409 destroy_rx_kthread(ir->rx);
1410out_free_xx:
1411 if (ir->rx != NULL) {
1412 if (ir->rx->buf.fifo_initialized)
1413 lirc_buffer_free(&ir->rx->buf);
1414 if (ir->rx->c != NULL)
1415 i2c_set_clientdata(ir->rx->c, NULL);
1416 kfree(ir->rx);
1417 }
1418 if (ir->tx != NULL) {
1419 if (ir->tx->c != NULL)
1420 i2c_set_clientdata(ir->tx->c, NULL);
1421 kfree(ir->tx);
1422 }
1423out_free_ir:
1424 del_ir_device(ir);
1425 kfree(ir);
1426out_no_ir:
1427 zilog_error("%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1428 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr,
1429 ret);
1430 mutex_unlock(&ir_devices_lock);
1431 return ret;
1432}
1433
1434static int __init zilog_init(void)
1435{
1436 int ret;
1437
1438 zilog_notify("Zilog/Hauppauge IR driver initializing\n");
1439
1440 mutex_init(&tx_data_lock);
1441 mutex_init(&ir_devices_lock);
1442
1443 request_module("firmware_class");
1444
1445 ret = i2c_add_driver(&driver);
1446 if (ret)
1447 zilog_error("initialization failed\n");
1448 else
1449 zilog_notify("initialization complete\n");
1450
1451 return ret;
1452}
1453
1454static void __exit zilog_exit(void)
1455{
1456 i2c_del_driver(&driver);
1457
1458 fw_unload();
1459 zilog_notify("Zilog/Hauppauge IR driver unloaded\n");
1460}
1461
1462module_init(zilog_init);
1463module_exit(zilog_exit);
1464
1465MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1466MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, "
1467 "Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, "
1468 "Andy Walls");
1469MODULE_LICENSE("GPL");
1470
1471MODULE_ALIAS("lirc_pvr150");
1472
1473module_param(minor, int, 0444);
1474MODULE_PARM_DESC(minor, "Preferred minor device number");
1475
1476module_param(debug, bool, 0644);
1477MODULE_PARM_DESC(debug, "Enable debugging messages");
1478
1479module_param(tx_only, bool, 0644);
1480MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");
1481