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