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#include <linux/module.h>
43#include <linux/kmod.h>
44#include <linux/kernel.h>
45#include <linux/sched/signal.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
64
65#define MAX_XFER_SIZE 64
66
67struct IR;
68
69struct IR_rx {
70 struct kref ref;
71 struct IR *ir;
72
73
74 struct mutex client_lock;
75 struct i2c_client *c;
76
77
78 struct task_struct *task;
79
80
81 unsigned char b[3];
82 bool hdpvr_data_fmt;
83};
84
85struct IR_tx {
86 struct kref ref;
87 struct IR *ir;
88
89
90 struct mutex client_lock;
91 struct i2c_client *c;
92
93
94 int need_boot;
95 bool post_tx_ready_poll;
96};
97
98struct IR {
99 struct kref ref;
100 struct list_head list;
101
102
103 struct lirc_driver l;
104 struct lirc_buffer rbuf;
105
106 struct mutex ir_lock;
107 atomic_t open_count;
108
109 struct i2c_adapter *adapter;
110
111 spinlock_t rx_ref_lock;
112 struct IR_rx *rx;
113
114 spinlock_t tx_ref_lock;
115 struct IR_tx *tx;
116};
117
118
119
120
121
122
123
124
125static DEFINE_MUTEX(ir_devices_lock);
126static LIST_HEAD(ir_devices_list);
127
128
129#define TX_BLOCK_SIZE 99
130
131
132struct tx_data_struct {
133
134 unsigned char *boot_data;
135
136
137 unsigned char *datap;
138
139
140 unsigned char *endp;
141
142
143 unsigned int num_code_sets;
144
145
146 unsigned char **code_sets;
147
148
149 int fixed[TX_BLOCK_SIZE];
150};
151
152static struct tx_data_struct *tx_data;
153static struct mutex tx_data_lock;
154
155
156
157static bool debug;
158static bool tx_only;
159
160
161
162static struct IR *get_ir_device(struct IR *ir, bool ir_devices_lock_held)
163{
164 if (ir_devices_lock_held) {
165 kref_get(&ir->ref);
166 } else {
167 mutex_lock(&ir_devices_lock);
168 kref_get(&ir->ref);
169 mutex_unlock(&ir_devices_lock);
170 }
171 return ir;
172}
173
174static void release_ir_device(struct kref *ref)
175{
176 struct IR *ir = container_of(ref, struct IR, ref);
177
178
179
180
181
182
183
184
185
186 if (ir->l.minor >= 0) {
187 lirc_unregister_driver(ir->l.minor);
188 ir->l.minor = -1;
189 }
190
191 if (kfifo_initialized(&ir->rbuf.fifo))
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
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)
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
227 if (!IS_ERR_OR_NULL(rx->task)) {
228 kthread_stop(rx->task);
229 rx->task = NULL;
230
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
242
243
244
245
246 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
247
248 ir->rx = NULL;
249
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
261 if (released) {
262 destroy_rx_kthread(rx, ir_devices_lock_held);
263 kfree(rx);
264
265 wake_up_interruptible(&ir->rbuf.wait_poll);
266 }
267
268 if (released)
269 put_ir_device(ir, ir_devices_lock_held);
270 return released;
271}
272
273
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)
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_LIRCCODE;
292
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
306 if (released)
307 put_ir_device(ir, ir_devices_lock_held);
308 return released;
309}
310
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 };
320 struct lirc_buffer *rbuf = ir->l.rbuf;
321 struct IR_rx *rx;
322 struct IR_tx *tx;
323
324 if (lirc_buffer_full(rbuf)) {
325 dev_dbg(ir->l.dev, "buffer overflow\n");
326 return -EOVERFLOW;
327 }
328
329 rx = get_ir_rx(ir);
330 if (!rx)
331 return -ENXIO;
332
333
334 mutex_lock(&rx->client_lock);
335 if (!rx->c) {
336 mutex_unlock(&rx->client_lock);
337 put_ir_rx(rx, false);
338 return -ENXIO;
339 }
340
341 tx = get_ir_tx(ir);
342
343
344
345
346
347 do {
348 if (kthread_should_stop()) {
349 ret = -ENODATA;
350 break;
351 }
352
353
354
355
356
357 mutex_lock(&ir->ir_lock);
358
359 if (kthread_should_stop()) {
360 mutex_unlock(&ir->ir_lock);
361 ret = -ENODATA;
362 break;
363 }
364
365
366
367
368
369 ret = i2c_master_send(rx->c, sendbuf, 1);
370 if (ret != 1) {
371 dev_err(ir->l.dev, "i2c_master_send failed with %d\n",
372 ret);
373 if (failures >= 3) {
374 mutex_unlock(&ir->ir_lock);
375 dev_err(ir->l.dev,
376 "unable to read from the IR chip after 3 resets, giving up\n");
377 break;
378 }
379
380
381 dev_err(ir->l.dev,
382 "polling the IR receiver chip failed, trying reset\n");
383
384 set_current_state(TASK_UNINTERRUPTIBLE);
385 if (kthread_should_stop()) {
386 mutex_unlock(&ir->ir_lock);
387 ret = -ENODATA;
388 break;
389 }
390 schedule_timeout((100 * HZ + 999) / 1000);
391 if (tx)
392 tx->need_boot = 1;
393
394 ++failures;
395 mutex_unlock(&ir->ir_lock);
396 ret = 0;
397 continue;
398 }
399
400 if (kthread_should_stop()) {
401 mutex_unlock(&ir->ir_lock);
402 ret = -ENODATA;
403 break;
404 }
405 ret = i2c_master_recv(rx->c, keybuf, sizeof(keybuf));
406 mutex_unlock(&ir->ir_lock);
407 if (ret != sizeof(keybuf)) {
408 dev_err(ir->l.dev,
409 "i2c_master_recv failed with %d -- keeping last read buffer\n",
410 ret);
411 } else {
412 rx->b[0] = keybuf[3];
413 rx->b[1] = keybuf[4];
414 rx->b[2] = keybuf[5];
415 dev_dbg(ir->l.dev,
416 "key (0x%02x/0x%02x)\n",
417 rx->b[0], rx->b[1]);
418 }
419
420
421 if (rx->hdpvr_data_fmt) {
422 if (got_data && (keybuf[0] == 0x80)) {
423 ret = 0;
424 break;
425 } else if (got_data && (keybuf[0] == 0x00)) {
426 ret = -ENODATA;
427 break;
428 }
429 } else if ((rx->b[0] & 0x80) == 0) {
430 ret = got_data ? 0 : -ENODATA;
431 break;
432 }
433
434
435 code = (((__u16)rx->b[0] & 0x7f) << 6) | (rx->b[1] >> 2);
436
437 codes[0] = (code >> 8) & 0xff;
438 codes[1] = code & 0xff;
439
440
441 lirc_buffer_write(rbuf, codes);
442 ++got_data;
443 ret = 0;
444 } while (!lirc_buffer_full(rbuf));
445
446 mutex_unlock(&rx->client_lock);
447 if (tx)
448 put_ir_tx(tx, false);
449 put_ir_rx(rx, false);
450 return ret;
451}
452
453
454
455
456
457
458
459
460
461
462
463static int lirc_thread(void *arg)
464{
465 struct IR *ir = arg;
466 struct lirc_buffer *rbuf = ir->l.rbuf;
467
468 dev_dbg(ir->l.dev, "poll thread started\n");
469
470 while (!kthread_should_stop()) {
471 set_current_state(TASK_INTERRUPTIBLE);
472
473
474 if (atomic_read(&ir->open_count) == 0) {
475 schedule_timeout(HZ / 2);
476 continue;
477 }
478
479
480
481
482
483
484
485
486
487
488
489 schedule_timeout((260 * HZ) / 1000);
490 if (kthread_should_stop())
491 break;
492 if (!add_to_buf(ir))
493 wake_up_interruptible(&rbuf->wait_poll);
494 }
495
496 dev_dbg(ir->l.dev, "poll thread ended\n");
497 return 0;
498}
499
500
501static int read_uint32(unsigned char **data,
502 unsigned char *endp, unsigned int *val)
503{
504 if (*data + 4 > endp)
505 return 0;
506 *val = ((*data)[0] << 24) | ((*data)[1] << 16) |
507 ((*data)[2] << 8) | (*data)[3];
508 *data += 4;
509 return 1;
510}
511
512
513static int read_uint8(unsigned char **data,
514 unsigned char *endp, unsigned char *val)
515{
516 if (*data + 1 > endp)
517 return 0;
518 *val = *((*data)++);
519 return 1;
520}
521
522
523static int skip(unsigned char **data,
524 unsigned char *endp, unsigned int distance)
525{
526 if (*data + distance > endp)
527 return 0;
528 *data += distance;
529 return 1;
530}
531
532
533static int get_key_data(unsigned char *buf,
534 unsigned int codeset, unsigned int key)
535{
536 unsigned char *data, *endp, *diffs, *key_block;
537 unsigned char keys, ndiffs, id;
538 unsigned int base, lim, pos, i;
539
540
541 for (base = 0, lim = tx_data->num_code_sets; lim; lim >>= 1) {
542 pos = base + (lim >> 1);
543 data = tx_data->code_sets[pos];
544
545 if (!read_uint32(&data, tx_data->endp, &i))
546 goto corrupt;
547
548 if (i == codeset) {
549 break;
550 } else if (codeset > i) {
551 base = pos + 1;
552 --lim;
553 }
554 }
555
556 if (!lim)
557 return -EPROTO;
558
559
560 endp = pos < tx_data->num_code_sets - 1 ?
561 tx_data->code_sets[pos + 1] : tx_data->endp;
562
563
564 if (!read_uint8(&data, endp, &keys) ||
565 !read_uint8(&data, endp, &ndiffs) ||
566 ndiffs > TX_BLOCK_SIZE || keys == 0)
567 goto corrupt;
568
569
570 diffs = data;
571 if (!skip(&data, endp, ndiffs))
572 goto corrupt;
573
574
575 if (!read_uint8(&data, endp, &id))
576 goto corrupt;
577
578
579 for (i = 0; i < TX_BLOCK_SIZE; ++i) {
580 if (tx_data->fixed[i] == -1) {
581 if (!read_uint8(&data, endp, &buf[i]))
582 goto corrupt;
583 } else {
584 buf[i] = (unsigned char)tx_data->fixed[i];
585 }
586 }
587
588
589 if (key == id)
590 return 0;
591 if (keys == 1)
592 return -EPROTO;
593
594
595 key_block = data;
596 if (!skip(&data, endp, (keys - 1) * (ndiffs + 1)))
597 goto corrupt;
598
599
600 for (base = 0, lim = keys - 1; lim; lim >>= 1) {
601
602 unsigned char *key_data;
603
604 pos = base + (lim >> 1);
605 key_data = key_block + (ndiffs + 1) * pos;
606
607 if (*key_data == key) {
608
609 ++key_data;
610
611
612 for (i = 0; i < ndiffs; ++i) {
613 unsigned char val;
614
615 if (!read_uint8(&key_data, endp, &val) ||
616 diffs[i] >= TX_BLOCK_SIZE)
617 goto corrupt;
618 buf[diffs[i]] = val;
619 }
620
621 return 0;
622 } else if (key > *key_data) {
623 base = pos + 1;
624 --lim;
625 }
626 }
627
628 return -EPROTO;
629
630corrupt:
631 pr_err("firmware is corrupt\n");
632 return -EFAULT;
633}
634
635
636static int send_data_block(struct IR_tx *tx, unsigned char *data_block)
637{
638 int i, j, ret;
639 unsigned char buf[5];
640
641 for (i = 0; i < TX_BLOCK_SIZE;) {
642 int tosend = TX_BLOCK_SIZE - i;
643
644 if (tosend > 4)
645 tosend = 4;
646 buf[0] = (unsigned char)(i + 1);
647 for (j = 0; j < tosend; ++j)
648 buf[1 + j] = data_block[i + j];
649 dev_dbg(tx->ir->l.dev, "%*ph", 5, buf);
650 ret = i2c_master_send(tx->c, buf, tosend + 1);
651 if (ret != tosend + 1) {
652 dev_err(tx->ir->l.dev,
653 "i2c_master_send failed with %d\n", ret);
654 return ret < 0 ? ret : -EFAULT;
655 }
656 i += tosend;
657 }
658 return 0;
659}
660
661
662static int send_boot_data(struct IR_tx *tx)
663{
664 int ret, i;
665 unsigned char buf[4];
666
667
668 ret = send_data_block(tx, tx_data->boot_data);
669 if (ret != 0)
670 return ret;
671
672
673 buf[0] = 0x00;
674 buf[1] = 0x20;
675 ret = i2c_master_send(tx->c, buf, 2);
676 if (ret != 2) {
677 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
678 return ret < 0 ? ret : -EFAULT;
679 }
680
681
682
683
684
685
686 for (i = 0; i < 10; i++) {
687 ret = i2c_master_send(tx->c, buf, 1);
688 if (ret == 1)
689 break;
690 udelay(100);
691 }
692
693 if (ret != 1) {
694 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
695 return ret < 0 ? ret : -EFAULT;
696 }
697
698
699 ret = i2c_master_recv(tx->c, buf, 4);
700 if (ret != 4) {
701 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
702 return 0;
703 }
704 if ((buf[0] != 0x80) && (buf[0] != 0xa0)) {
705 dev_err(tx->ir->l.dev, "unexpected IR TX init response: %02x\n",
706 buf[0]);
707 return 0;
708 }
709 dev_notice(tx->ir->l.dev,
710 "Zilog/Hauppauge IR blaster firmware version %d.%d.%d loaded\n",
711 buf[1], buf[2], buf[3]);
712
713 return 0;
714}
715
716
717static void fw_unload_locked(void)
718{
719 if (tx_data) {
720 vfree(tx_data->code_sets);
721
722 vfree(tx_data->datap);
723
724 vfree(tx_data);
725 tx_data = NULL;
726 pr_debug("successfully unloaded IR blaster firmware\n");
727 }
728}
729
730
731static void fw_unload(void)
732{
733 mutex_lock(&tx_data_lock);
734 fw_unload_locked();
735 mutex_unlock(&tx_data_lock);
736}
737
738
739static int fw_load(struct IR_tx *tx)
740{
741 int ret;
742 unsigned int i;
743 unsigned char *data, version, num_global_fixed;
744 const struct firmware *fw_entry;
745
746
747 mutex_lock(&tx_data_lock);
748 if (tx_data) {
749 ret = 0;
750 goto out;
751 }
752
753
754 ret = request_firmware(&fw_entry, "haup-ir-blaster.bin", tx->ir->l.dev);
755 if (ret != 0) {
756 dev_err(tx->ir->l.dev,
757 "firmware haup-ir-blaster.bin not available (%d)\n",
758 ret);
759 ret = ret < 0 ? ret : -EFAULT;
760 goto out;
761 }
762 dev_dbg(tx->ir->l.dev, "firmware of size %zu loaded\n", fw_entry->size);
763
764
765 tx_data = vmalloc(sizeof(*tx_data));
766 if (!tx_data) {
767 release_firmware(fw_entry);
768 ret = -ENOMEM;
769 goto out;
770 }
771 tx_data->code_sets = NULL;
772
773
774 tx_data->datap = vmalloc(fw_entry->size);
775 if (!tx_data->datap) {
776 release_firmware(fw_entry);
777 vfree(tx_data);
778 ret = -ENOMEM;
779 goto out;
780 }
781 memcpy(tx_data->datap, fw_entry->data, fw_entry->size);
782 tx_data->endp = tx_data->datap + fw_entry->size;
783 release_firmware(fw_entry); fw_entry = NULL;
784
785
786 data = tx_data->datap;
787 if (!read_uint8(&data, tx_data->endp, &version))
788 goto corrupt;
789 if (version != 1) {
790 dev_err(tx->ir->l.dev,
791 "unsupported code set file version (%u, expected 1) -- please upgrade to a newer driver\n",
792 version);
793 fw_unload_locked();
794 ret = -EFAULT;
795 goto out;
796 }
797
798
799 tx_data->boot_data = data;
800 if (!skip(&data, tx_data->endp, TX_BLOCK_SIZE))
801 goto corrupt;
802
803 if (!read_uint32(&data, tx_data->endp,
804 &tx_data->num_code_sets))
805 goto corrupt;
806
807 dev_dbg(tx->ir->l.dev, "%u IR blaster codesets loaded\n",
808 tx_data->num_code_sets);
809
810 tx_data->code_sets = vmalloc(
811 tx_data->num_code_sets * sizeof(char *));
812 if (!tx_data->code_sets) {
813 fw_unload_locked();
814 ret = -ENOMEM;
815 goto out;
816 }
817
818 for (i = 0; i < TX_BLOCK_SIZE; ++i)
819 tx_data->fixed[i] = -1;
820
821
822 if (!read_uint8(&data, tx_data->endp, &num_global_fixed) ||
823 num_global_fixed > TX_BLOCK_SIZE)
824 goto corrupt;
825 for (i = 0; i < num_global_fixed; ++i) {
826 unsigned char pos, val;
827
828 if (!read_uint8(&data, tx_data->endp, &pos) ||
829 !read_uint8(&data, tx_data->endp, &val) ||
830 pos >= TX_BLOCK_SIZE)
831 goto corrupt;
832 tx_data->fixed[pos] = (int)val;
833 }
834
835
836 for (i = 0; i < tx_data->num_code_sets; ++i) {
837 unsigned int id;
838 unsigned char keys;
839 unsigned char ndiffs;
840
841
842 tx_data->code_sets[i] = data;
843
844
845 if (!read_uint32(&data, tx_data->endp, &id) ||
846 !read_uint8(&data, tx_data->endp, &keys) ||
847 !read_uint8(&data, tx_data->endp, &ndiffs) ||
848 ndiffs > TX_BLOCK_SIZE || keys == 0)
849 goto corrupt;
850
851
852 if (!skip(&data, tx_data->endp, ndiffs))
853 goto corrupt;
854
855
856
857
858
859 if (!skip(&data, tx_data->endp,
860 1 + TX_BLOCK_SIZE - num_global_fixed))
861 goto corrupt;
862
863
864 if (!skip(&data, tx_data->endp,
865 (ndiffs + 1) * (keys - 1)))
866 goto corrupt;
867 }
868 ret = 0;
869 goto out;
870
871corrupt:
872 dev_err(tx->ir->l.dev, "firmware is corrupt\n");
873 fw_unload_locked();
874 ret = -EFAULT;
875
876out:
877 mutex_unlock(&tx_data_lock);
878 return ret;
879}
880
881
882static ssize_t read(struct file *filep, char __user *outbuf, size_t n,
883 loff_t *ppos)
884{
885 struct IR *ir = filep->private_data;
886 struct IR_rx *rx;
887 struct lirc_buffer *rbuf = ir->l.rbuf;
888 int ret = 0, written = 0, retries = 0;
889 unsigned int m;
890 DECLARE_WAITQUEUE(wait, current);
891
892 dev_dbg(ir->l.dev, "read called\n");
893 if (n % rbuf->chunk_size) {
894 dev_dbg(ir->l.dev, "read result = -EINVAL\n");
895 return -EINVAL;
896 }
897
898 rx = get_ir_rx(ir);
899 if (!rx)
900 return -ENXIO;
901
902
903
904
905
906
907 add_wait_queue(&rbuf->wait_poll, &wait);
908 set_current_state(TASK_INTERRUPTIBLE);
909
910
911
912
913
914 while (written < n && ret == 0) {
915 if (lirc_buffer_empty(rbuf)) {
916
917
918
919
920
921
922 if (written)
923 break;
924 if (filep->f_flags & O_NONBLOCK) {
925 ret = -EWOULDBLOCK;
926 break;
927 }
928 if (signal_pending(current)) {
929 ret = -ERESTARTSYS;
930 break;
931 }
932 schedule();
933 set_current_state(TASK_INTERRUPTIBLE);
934 } else {
935 unsigned char buf[MAX_XFER_SIZE];
936
937 if (rbuf->chunk_size > sizeof(buf)) {
938 dev_err(ir->l.dev,
939 "chunk_size is too big (%d)!\n",
940 rbuf->chunk_size);
941 ret = -EINVAL;
942 break;
943 }
944 m = lirc_buffer_read(rbuf, buf);
945 if (m == rbuf->chunk_size) {
946 ret = copy_to_user(outbuf + written, buf,
947 rbuf->chunk_size);
948 written += rbuf->chunk_size;
949 } else {
950 retries++;
951 }
952 if (retries >= 5) {
953 dev_err(ir->l.dev, "Buffer read failed!\n");
954 ret = -EIO;
955 }
956 }
957 }
958
959 remove_wait_queue(&rbuf->wait_poll, &wait);
960 put_ir_rx(rx, false);
961 set_current_state(TASK_RUNNING);
962
963 dev_dbg(ir->l.dev, "read result = %d (%s)\n", ret,
964 ret ? "Error" : "OK");
965
966 return ret ? ret : written;
967}
968
969
970static int send_code(struct IR_tx *tx, unsigned int code, unsigned int key)
971{
972 unsigned char data_block[TX_BLOCK_SIZE];
973 unsigned char buf[2];
974 int i, ret;
975
976
977 ret = get_key_data(data_block, code, key);
978
979 if (ret == -EPROTO) {
980 dev_err(tx->ir->l.dev,
981 "failed to get data for code %u, key %u -- check lircd.conf entries\n",
982 code, key);
983 return ret;
984 } else if (ret != 0) {
985 return ret;
986 }
987
988
989 ret = send_data_block(tx, data_block);
990 if (ret != 0)
991 return ret;
992
993
994 buf[0] = 0x00;
995 buf[1] = 0x40;
996 ret = i2c_master_send(tx->c, buf, 2);
997 if (ret != 2) {
998 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
999 return ret < 0 ? ret : -EFAULT;
1000 }
1001
1002
1003 for (i = 0; i < 10; i++) {
1004 ret = i2c_master_send(tx->c, buf, 1);
1005 if (ret == 1)
1006 break;
1007 udelay(100);
1008 }
1009
1010 if (ret != 1) {
1011 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
1012 return ret < 0 ? ret : -EFAULT;
1013 }
1014
1015
1016 ret = i2c_master_recv(tx->c, buf, 1);
1017 if (ret != 1) {
1018 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
1019 return ret < 0 ? ret : -EFAULT;
1020 }
1021 if (buf[0] != 0xA0) {
1022 dev_err(tx->ir->l.dev, "unexpected IR TX response #1: %02x\n",
1023 buf[0]);
1024 return -EFAULT;
1025 }
1026
1027
1028 buf[0] = 0x00;
1029 buf[1] = 0x80;
1030 ret = i2c_master_send(tx->c, buf, 2);
1031 if (ret != 2) {
1032 dev_err(tx->ir->l.dev, "i2c_master_send failed with %d\n", ret);
1033 return ret < 0 ? ret : -EFAULT;
1034 }
1035
1036
1037
1038
1039
1040
1041 if (!tx->post_tx_ready_poll) {
1042 dev_dbg(tx->ir->l.dev, "sent code %u, key %u\n", code, key);
1043 return 0;
1044 }
1045
1046
1047
1048
1049
1050
1051
1052 for (i = 0; i < 20; ++i) {
1053 set_current_state(TASK_UNINTERRUPTIBLE);
1054 schedule_timeout((50 * HZ + 999) / 1000);
1055 ret = i2c_master_send(tx->c, buf, 1);
1056 if (ret == 1)
1057 break;
1058 dev_dbg(tx->ir->l.dev,
1059 "NAK expected: i2c_master_send failed with %d (try %d)\n",
1060 ret, i + 1);
1061 }
1062 if (ret != 1) {
1063 dev_err(tx->ir->l.dev,
1064 "IR TX chip never got ready: last i2c_master_send failed with %d\n",
1065 ret);
1066 return ret < 0 ? ret : -EFAULT;
1067 }
1068
1069
1070 i = i2c_master_recv(tx->c, buf, 1);
1071 if (i != 1) {
1072 dev_err(tx->ir->l.dev, "i2c_master_recv failed with %d\n", ret);
1073 return -EFAULT;
1074 }
1075 if (buf[0] != 0x80) {
1076 dev_err(tx->ir->l.dev, "unexpected IR TX response #2: %02x\n",
1077 buf[0]);
1078 return -EFAULT;
1079 }
1080
1081
1082 dev_dbg(tx->ir->l.dev, "sent code %u, key %u\n", code, key);
1083 return 0;
1084}
1085
1086
1087
1088
1089
1090
1091
1092static ssize_t write(struct file *filep, const char __user *buf, size_t n,
1093 loff_t *ppos)
1094{
1095 struct IR *ir = filep->private_data;
1096 struct IR_tx *tx;
1097 size_t i;
1098 int failures = 0;
1099
1100
1101 if (n % sizeof(int))
1102 return -EINVAL;
1103
1104
1105 tx = get_ir_tx(ir);
1106 if (!tx)
1107 return -ENXIO;
1108
1109
1110 mutex_lock(&tx->client_lock);
1111 if (!tx->c) {
1112 mutex_unlock(&tx->client_lock);
1113 put_ir_tx(tx, false);
1114 return -ENXIO;
1115 }
1116
1117
1118 mutex_lock(&ir->ir_lock);
1119
1120
1121 for (i = 0; i < n;) {
1122 int ret = 0;
1123 int command;
1124
1125 if (copy_from_user(&command, buf + i, sizeof(command))) {
1126 mutex_unlock(&ir->ir_lock);
1127 mutex_unlock(&tx->client_lock);
1128 put_ir_tx(tx, false);
1129 return -EFAULT;
1130 }
1131
1132
1133 if (tx->need_boot == 1) {
1134
1135 ret = fw_load(tx);
1136 if (ret != 0) {
1137 mutex_unlock(&ir->ir_lock);
1138 mutex_unlock(&tx->client_lock);
1139 put_ir_tx(tx, false);
1140 if (ret != -ENOMEM)
1141 ret = -EIO;
1142 return ret;
1143 }
1144
1145 ret = send_boot_data(tx);
1146 if (ret == 0)
1147 tx->need_boot = 0;
1148 }
1149
1150
1151 if (ret == 0) {
1152 ret = send_code(tx, (unsigned int)command >> 16,
1153 (unsigned int)command & 0xFFFF);
1154 if (ret == -EPROTO) {
1155 mutex_unlock(&ir->ir_lock);
1156 mutex_unlock(&tx->client_lock);
1157 put_ir_tx(tx, false);
1158 return ret;
1159 }
1160 }
1161
1162
1163
1164
1165
1166 if (ret != 0) {
1167
1168 dev_err(tx->ir->l.dev,
1169 "sending to the IR transmitter chip failed, trying reset\n");
1170
1171 if (failures >= 3) {
1172 dev_err(tx->ir->l.dev,
1173 "unable to send to the IR chip after 3 resets, giving up\n");
1174 mutex_unlock(&ir->ir_lock);
1175 mutex_unlock(&tx->client_lock);
1176 put_ir_tx(tx, false);
1177 return ret;
1178 }
1179 set_current_state(TASK_UNINTERRUPTIBLE);
1180 schedule_timeout((100 * HZ + 999) / 1000);
1181 tx->need_boot = 1;
1182 ++failures;
1183 } else {
1184 i += sizeof(int);
1185 }
1186 }
1187
1188
1189 mutex_unlock(&ir->ir_lock);
1190
1191 mutex_unlock(&tx->client_lock);
1192
1193
1194 put_ir_tx(tx, false);
1195
1196
1197 return n;
1198}
1199
1200
1201static unsigned int poll(struct file *filep, poll_table *wait)
1202{
1203 struct IR *ir = filep->private_data;
1204 struct IR_rx *rx;
1205 struct lirc_buffer *rbuf = ir->l.rbuf;
1206 unsigned int ret;
1207
1208 dev_dbg(ir->l.dev, "%s called\n", __func__);
1209
1210 rx = get_ir_rx(ir);
1211 if (!rx) {
1212
1213
1214
1215
1216 dev_dbg(ir->l.dev, "%s result = POLLERR\n", __func__);
1217 return POLLERR;
1218 }
1219
1220
1221
1222
1223
1224 poll_wait(filep, &rbuf->wait_poll, wait);
1225
1226
1227 ret = lirc_buffer_empty(rbuf) ? 0 : (POLLIN | POLLRDNORM);
1228
1229 dev_dbg(ir->l.dev, "%s result = %s\n", __func__,
1230 ret ? "POLLIN|POLLRDNORM" : "none");
1231 return ret;
1232}
1233
1234static long ioctl(struct file *filep, unsigned int cmd, unsigned long arg)
1235{
1236 struct IR *ir = filep->private_data;
1237 unsigned long __user *uptr = (unsigned long __user *)arg;
1238 int result;
1239 unsigned long mode, features;
1240
1241 features = ir->l.features;
1242
1243 switch (cmd) {
1244 case LIRC_GET_LENGTH:
1245 result = put_user(13UL, uptr);
1246 break;
1247 case LIRC_GET_FEATURES:
1248 result = put_user(features, uptr);
1249 break;
1250 case LIRC_GET_REC_MODE:
1251 if (!(features & LIRC_CAN_REC_MASK))
1252 return -ENOTTY;
1253
1254 result = put_user(LIRC_REC2MODE
1255 (features & LIRC_CAN_REC_MASK),
1256 uptr);
1257 break;
1258 case LIRC_SET_REC_MODE:
1259 if (!(features & LIRC_CAN_REC_MASK))
1260 return -ENOTTY;
1261
1262 result = get_user(mode, uptr);
1263 if (!result && !(LIRC_MODE2REC(mode) & features))
1264 result = -ENOTTY;
1265 break;
1266 case LIRC_GET_SEND_MODE:
1267 if (!(features & LIRC_CAN_SEND_MASK))
1268 return -ENOTTY;
1269
1270 result = put_user(LIRC_MODE_LIRCCODE, uptr);
1271 break;
1272 case LIRC_SET_SEND_MODE:
1273 if (!(features & LIRC_CAN_SEND_MASK))
1274 return -ENOTTY;
1275
1276 result = get_user(mode, uptr);
1277 if (!result && mode != LIRC_MODE_LIRCCODE)
1278 return -EINVAL;
1279 break;
1280 default:
1281 return -EINVAL;
1282 }
1283 return result;
1284}
1285
1286static struct IR *get_ir_device_by_minor(unsigned int minor)
1287{
1288 struct IR *ir;
1289 struct IR *ret = NULL;
1290
1291 mutex_lock(&ir_devices_lock);
1292
1293 if (!list_empty(&ir_devices_list)) {
1294 list_for_each_entry(ir, &ir_devices_list, list) {
1295 if (ir->l.minor == minor) {
1296 ret = get_ir_device(ir, true);
1297 break;
1298 }
1299 }
1300 }
1301
1302 mutex_unlock(&ir_devices_lock);
1303 return ret;
1304}
1305
1306
1307
1308
1309
1310static int open(struct inode *node, struct file *filep)
1311{
1312 struct IR *ir;
1313 unsigned int minor = MINOR(node->i_rdev);
1314
1315
1316 ir = get_ir_device_by_minor(minor);
1317
1318 if (!ir)
1319 return -ENODEV;
1320
1321 atomic_inc(&ir->open_count);
1322
1323
1324 filep->private_data = ir;
1325
1326 nonseekable_open(node, filep);
1327 return 0;
1328}
1329
1330
1331static int close(struct inode *node, struct file *filep)
1332{
1333
1334 struct IR *ir = filep->private_data;
1335
1336 if (!ir) {
1337 pr_err("ir: %s: no private_data attached to the file!\n",
1338 __func__);
1339 return -ENODEV;
1340 }
1341
1342 atomic_dec(&ir->open_count);
1343
1344 put_ir_device(ir, false);
1345 return 0;
1346}
1347
1348static int ir_remove(struct i2c_client *client);
1349static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id);
1350
1351#define ID_FLAG_TX 0x01
1352#define ID_FLAG_HDPVR 0x02
1353
1354static const struct i2c_device_id ir_transceiver_id[] = {
1355 { "ir_tx_z8f0811_haup", ID_FLAG_TX },
1356 { "ir_rx_z8f0811_haup", 0 },
1357 { "ir_tx_z8f0811_hdpvr", ID_FLAG_HDPVR | ID_FLAG_TX },
1358 { "ir_rx_z8f0811_hdpvr", ID_FLAG_HDPVR },
1359 { }
1360};
1361MODULE_DEVICE_TABLE(i2c, ir_transceiver_id);
1362
1363static struct i2c_driver driver = {
1364 .driver = {
1365 .name = "Zilog/Hauppauge i2c IR",
1366 },
1367 .probe = ir_probe,
1368 .remove = ir_remove,
1369 .id_table = ir_transceiver_id,
1370};
1371
1372static const struct file_operations lirc_fops = {
1373 .owner = THIS_MODULE,
1374 .llseek = no_llseek,
1375 .read = read,
1376 .write = write,
1377 .poll = poll,
1378 .unlocked_ioctl = ioctl,
1379#ifdef CONFIG_COMPAT
1380 .compat_ioctl = ioctl,
1381#endif
1382 .open = open,
1383 .release = close
1384};
1385
1386static struct lirc_driver lirc_template = {
1387 .name = "lirc_zilog",
1388 .minor = -1,
1389 .code_length = 13,
1390 .buffer_size = BUFLEN / 2,
1391 .chunk_size = 2,
1392 .fops = &lirc_fops,
1393 .owner = THIS_MODULE,
1394};
1395
1396static int ir_remove(struct i2c_client *client)
1397{
1398 if (strncmp("ir_tx_z8", client->name, 8) == 0) {
1399 struct IR_tx *tx = i2c_get_clientdata(client);
1400
1401 if (tx) {
1402 mutex_lock(&tx->client_lock);
1403 tx->c = NULL;
1404 mutex_unlock(&tx->client_lock);
1405 put_ir_tx(tx, false);
1406 }
1407 } else if (strncmp("ir_rx_z8", client->name, 8) == 0) {
1408 struct IR_rx *rx = i2c_get_clientdata(client);
1409
1410 if (rx) {
1411 mutex_lock(&rx->client_lock);
1412 rx->c = NULL;
1413 mutex_unlock(&rx->client_lock);
1414 put_ir_rx(rx, false);
1415 }
1416 }
1417 return 0;
1418}
1419
1420
1421static struct IR *get_ir_device_by_adapter(struct i2c_adapter *adapter)
1422{
1423 struct IR *ir;
1424
1425 if (list_empty(&ir_devices_list))
1426 return NULL;
1427
1428 list_for_each_entry(ir, &ir_devices_list, list)
1429 if (ir->adapter == adapter) {
1430 get_ir_device(ir, true);
1431 return ir;
1432 }
1433
1434 return NULL;
1435}
1436
1437static int ir_probe(struct i2c_client *client, const struct i2c_device_id *id)
1438{
1439 struct IR *ir;
1440 struct IR_tx *tx;
1441 struct IR_rx *rx;
1442 struct i2c_adapter *adap = client->adapter;
1443 int ret;
1444 bool tx_probe = false;
1445
1446 dev_dbg(&client->dev, "%s: %s on i2c-%d (%s), client addr=0x%02x\n",
1447 __func__, id->name, adap->nr, adap->name, client->addr);
1448
1449
1450
1451
1452
1453
1454 if (id->driver_data & ID_FLAG_TX)
1455 tx_probe = true;
1456 else if (tx_only)
1457 return -ENXIO;
1458
1459 pr_info("probing IR %s on %s (i2c-%d)\n",
1460 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1461
1462 mutex_lock(&ir_devices_lock);
1463
1464
1465 ir = get_ir_device_by_adapter(adap);
1466 if (!ir) {
1467 ir = kzalloc(sizeof(*ir), GFP_KERNEL);
1468 if (!ir) {
1469 ret = -ENOMEM;
1470 goto out_no_ir;
1471 }
1472 kref_init(&ir->ref);
1473
1474
1475 INIT_LIST_HEAD(&ir->list);
1476 list_add_tail(&ir->list, &ir_devices_list);
1477
1478 ir->adapter = adap;
1479 mutex_init(&ir->ir_lock);
1480 atomic_set(&ir->open_count, 0);
1481 spin_lock_init(&ir->tx_ref_lock);
1482 spin_lock_init(&ir->rx_ref_lock);
1483
1484
1485 memcpy(&ir->l, &lirc_template, sizeof(struct lirc_driver));
1486
1487
1488
1489
1490
1491
1492
1493
1494 ir->l.rbuf = &ir->rbuf;
1495 ir->l.dev = &adap->dev;
1496 ret = lirc_buffer_init(ir->l.rbuf,
1497 ir->l.chunk_size, ir->l.buffer_size);
1498 if (ret)
1499 goto out_put_ir;
1500 }
1501
1502 if (tx_probe) {
1503
1504 rx = get_ir_rx(ir);
1505
1506
1507 tx = kzalloc(sizeof(*tx), GFP_KERNEL);
1508 if (!tx) {
1509 ret = -ENOMEM;
1510 goto out_put_xx;
1511 }
1512 kref_init(&tx->ref);
1513 ir->tx = tx;
1514
1515 ir->l.features |= LIRC_CAN_SEND_LIRCCODE;
1516 mutex_init(&tx->client_lock);
1517 tx->c = client;
1518 tx->need_boot = 1;
1519 tx->post_tx_ready_poll =
1520 (id->driver_data & ID_FLAG_HDPVR) ? false : true;
1521
1522
1523 tx->ir = get_ir_device(ir, true);
1524
1525
1526 i2c_set_clientdata(client, get_ir_tx(ir));
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537 fw_load(tx);
1538
1539
1540 if (!rx && !tx_only) {
1541 dev_info(tx->ir->l.dev,
1542 "probe of IR Tx on %s (i2c-%d) done. Waiting on IR Rx.\n",
1543 adap->name, adap->nr);
1544 goto out_ok;
1545 }
1546 } else {
1547
1548 tx = get_ir_tx(ir);
1549
1550
1551 rx = kzalloc(sizeof(*rx), GFP_KERNEL);
1552 if (!rx) {
1553 ret = -ENOMEM;
1554 goto out_put_xx;
1555 }
1556 kref_init(&rx->ref);
1557 ir->rx = rx;
1558
1559 ir->l.features |= LIRC_CAN_REC_LIRCCODE;
1560 mutex_init(&rx->client_lock);
1561 rx->c = client;
1562 rx->hdpvr_data_fmt =
1563 (id->driver_data & ID_FLAG_HDPVR) ? true : false;
1564
1565
1566 rx->ir = get_ir_device(ir, true);
1567
1568
1569 i2c_set_clientdata(client, get_ir_rx(ir));
1570
1571
1572
1573
1574
1575
1576
1577 rx->task = kthread_run(lirc_thread, get_ir_device(ir, true),
1578 "zilog-rx-i2c-%d", adap->nr);
1579 if (IS_ERR(rx->task)) {
1580 ret = PTR_ERR(rx->task);
1581 dev_err(tx->ir->l.dev,
1582 "%s: could not start IR Rx polling thread\n",
1583 __func__);
1584
1585 put_ir_device(ir, true);
1586
1587 i2c_set_clientdata(client, NULL);
1588 put_ir_rx(rx, true);
1589 ir->l.features &= ~LIRC_CAN_REC_LIRCCODE;
1590 goto out_put_tx;
1591 }
1592
1593
1594 if (!tx) {
1595 pr_info("probe of IR Rx on %s (i2c-%d) done. Waiting on IR Tx.\n",
1596 adap->name, adap->nr);
1597 goto out_ok;
1598 }
1599 }
1600
1601
1602 ir->l.minor = lirc_register_driver(&ir->l);
1603 if (ir->l.minor < 0) {
1604 dev_err(tx->ir->l.dev,
1605 "%s: lirc_register_driver() failed: %i\n",
1606 __func__, ir->l.minor);
1607 ret = -EBADRQC;
1608 goto out_put_xx;
1609 }
1610 dev_info(ir->l.dev,
1611 "IR unit on %s (i2c-%d) registered as lirc%d and ready\n",
1612 adap->name, adap->nr, ir->l.minor);
1613
1614out_ok:
1615 if (rx)
1616 put_ir_rx(rx, true);
1617 if (tx)
1618 put_ir_tx(tx, true);
1619 put_ir_device(ir, true);
1620 dev_info(ir->l.dev,
1621 "probe of IR %s on %s (i2c-%d) done\n",
1622 tx_probe ? "Tx" : "Rx", adap->name, adap->nr);
1623 mutex_unlock(&ir_devices_lock);
1624 return 0;
1625
1626out_put_xx:
1627 if (rx)
1628 put_ir_rx(rx, true);
1629out_put_tx:
1630 if (tx)
1631 put_ir_tx(tx, true);
1632out_put_ir:
1633 put_ir_device(ir, true);
1634out_no_ir:
1635 dev_err(&client->dev,
1636 "%s: probing IR %s on %s (i2c-%d) failed with %d\n",
1637 __func__, tx_probe ? "Tx" : "Rx", adap->name, adap->nr, ret);
1638 mutex_unlock(&ir_devices_lock);
1639 return ret;
1640}
1641
1642static int __init zilog_init(void)
1643{
1644 int ret;
1645
1646 pr_notice("Zilog/Hauppauge IR driver initializing\n");
1647
1648 mutex_init(&tx_data_lock);
1649
1650 request_module("firmware_class");
1651
1652 ret = i2c_add_driver(&driver);
1653 if (ret)
1654 pr_err("initialization failed\n");
1655 else
1656 pr_notice("initialization complete\n");
1657
1658 return ret;
1659}
1660
1661static void __exit zilog_exit(void)
1662{
1663 i2c_del_driver(&driver);
1664
1665 fw_unload();
1666 pr_notice("Zilog/Hauppauge IR driver unloaded\n");
1667}
1668
1669module_init(zilog_init);
1670module_exit(zilog_exit);
1671
1672MODULE_DESCRIPTION("Zilog/Hauppauge infrared transmitter driver (i2c stack)");
1673MODULE_AUTHOR("Gerd Knorr, Michal Kochanowicz, Christoph Bartelmus, Ulrich Mueller, Stefan Jahn, Jerome Brock, Mark Weaver, Andy Walls");
1674MODULE_LICENSE("GPL");
1675
1676MODULE_ALIAS("lirc_pvr150");
1677
1678module_param(debug, bool, 0644);
1679MODULE_PARM_DESC(debug, "Enable debugging messages");
1680
1681module_param(tx_only, bool, 0644);
1682MODULE_PARM_DESC(tx_only, "Only handle the IR transmit function");
1683