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