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#include "anysee.h"
31#include "dvb-pll.h"
32#include "tda1002x.h"
33#include "mt352.h"
34#include "mt352_priv.h"
35#include "zl10353.h"
36#include "tda18212.h"
37#include "cx24116.h"
38#include "stv0900.h"
39#include "stv6110.h"
40#include "isl6423.h"
41#include "cxd2820r.h"
42
43DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
44
45static int anysee_ctrl_msg(struct dvb_usb_device *d,
46 u8 *sbuf, u8 slen, u8 *rbuf, u8 rlen)
47{
48 struct anysee_state *state = d_to_priv(d);
49 int act_len, ret, i;
50
51 mutex_lock(&d->usb_mutex);
52
53 memcpy(&state->buf[0], sbuf, slen);
54 state->buf[60] = state->seq++;
55
56 dev_dbg(&d->udev->dev, "%s: >>> %*ph\n", __func__, slen, state->buf);
57
58
59
60 ret = dvb_usbv2_generic_rw_locked(d, state->buf, sizeof(state->buf),
61 state->buf, sizeof(state->buf));
62 if (ret)
63 goto error_unlock;
64
65
66
67
68
69
70
71
72
73
74
75
76
77 for (i = 0; i < 3; i++) {
78
79 ret = usb_bulk_msg(d->udev, usb_rcvbulkpipe(d->udev,
80 d->props->generic_bulk_ctrl_endpoint),
81 state->buf, sizeof(state->buf), &act_len, 2000);
82 if (ret) {
83 dev_dbg(&d->udev->dev,
84 "%s: recv bulk message failed=%d\n",
85 __func__, ret);
86 } else {
87 dev_dbg(&d->udev->dev, "%s: <<< %*ph\n", __func__,
88 rlen, state->buf);
89
90 if (state->buf[63] != 0x4f)
91 dev_dbg(&d->udev->dev,
92 "%s: cmd failed\n", __func__);
93 break;
94 }
95 }
96
97 if (ret) {
98
99 dev_err(&d->udev->dev, "%s: recv bulk message failed=%d\n",
100 KBUILD_MODNAME, ret);
101 goto error_unlock;
102 }
103
104
105 if (rbuf && rlen)
106 memcpy(rbuf, state->buf, rlen);
107
108error_unlock:
109 mutex_unlock(&d->usb_mutex);
110 return ret;
111}
112
113static int anysee_read_reg(struct dvb_usb_device *d, u16 reg, u8 *val)
114{
115 u8 buf[] = {CMD_REG_READ, reg >> 8, reg & 0xff, 0x01};
116 int ret;
117 ret = anysee_ctrl_msg(d, buf, sizeof(buf), val, 1);
118 dev_dbg(&d->udev->dev, "%s: reg=%04x val=%02x\n", __func__, reg, *val);
119 return ret;
120}
121
122static int anysee_write_reg(struct dvb_usb_device *d, u16 reg, u8 val)
123{
124 u8 buf[] = {CMD_REG_WRITE, reg >> 8, reg & 0xff, 0x01, val};
125 dev_dbg(&d->udev->dev, "%s: reg=%04x val=%02x\n", __func__, reg, val);
126 return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
127}
128
129
130static int anysee_wr_reg_mask(struct dvb_usb_device *d, u16 reg, u8 val,
131 u8 mask)
132{
133 int ret;
134 u8 tmp;
135
136
137 if (mask != 0xff) {
138 ret = anysee_read_reg(d, reg, &tmp);
139 if (ret)
140 return ret;
141
142 val &= mask;
143 tmp &= ~mask;
144 val |= tmp;
145 }
146
147 return anysee_write_reg(d, reg, val);
148}
149
150
151static int anysee_rd_reg_mask(struct dvb_usb_device *d, u16 reg, u8 *val,
152 u8 mask)
153{
154 int ret, i;
155 u8 tmp;
156
157 ret = anysee_read_reg(d, reg, &tmp);
158 if (ret)
159 return ret;
160
161 tmp &= mask;
162
163
164 for (i = 0; i < 8; i++) {
165 if ((mask >> i) & 0x01)
166 break;
167 }
168 *val = tmp >> i;
169
170 return 0;
171}
172
173static int anysee_get_hw_info(struct dvb_usb_device *d, u8 *id)
174{
175 u8 buf[] = {CMD_GET_HW_INFO};
176 return anysee_ctrl_msg(d, buf, sizeof(buf), id, 3);
177}
178
179static int anysee_streaming_ctrl(struct dvb_frontend *fe, int onoff)
180{
181 u8 buf[] = {CMD_STREAMING_CTRL, (u8)onoff, 0x00};
182 dev_dbg(&fe_to_d(fe)->udev->dev, "%s: onoff=%d\n", __func__, onoff);
183 return anysee_ctrl_msg(fe_to_d(fe), buf, sizeof(buf), NULL, 0);
184}
185
186static int anysee_led_ctrl(struct dvb_usb_device *d, u8 mode, u8 interval)
187{
188 u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x01, mode, interval};
189 dev_dbg(&d->udev->dev, "%s: state=%d interval=%d\n", __func__,
190 mode, interval);
191 return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
192}
193
194static int anysee_ir_ctrl(struct dvb_usb_device *d, u8 onoff)
195{
196 u8 buf[] = {CMD_LED_AND_IR_CTRL, 0x02, onoff};
197 dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
198 return anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
199}
200
201
202static int anysee_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msg,
203 int num)
204{
205 struct dvb_usb_device *d = i2c_get_adapdata(adap);
206 int ret = 0, inc, i = 0;
207 u8 buf[52];
208
209 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
210 return -EAGAIN;
211
212 while (i < num) {
213 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
214 if (msg[i].len > 2 || msg[i+1].len > 60) {
215 ret = -EOPNOTSUPP;
216 break;
217 }
218 buf[0] = CMD_I2C_READ;
219 buf[1] = (msg[i].addr << 1) | 0x01;
220 buf[2] = msg[i].buf[0];
221 buf[3] = msg[i].buf[1];
222 buf[4] = msg[i].len-1;
223 buf[5] = msg[i+1].len;
224 ret = anysee_ctrl_msg(d, buf, 6, msg[i+1].buf,
225 msg[i+1].len);
226 inc = 2;
227 } else {
228 if (msg[i].len > 48) {
229 ret = -EOPNOTSUPP;
230 break;
231 }
232 buf[0] = CMD_I2C_WRITE;
233 buf[1] = (msg[i].addr << 1);
234 buf[2] = msg[i].len;
235 buf[3] = 0x01;
236 memcpy(&buf[4], msg[i].buf, msg[i].len);
237 ret = anysee_ctrl_msg(d, buf, 4 + msg[i].len, NULL, 0);
238 inc = 1;
239 }
240 if (ret)
241 break;
242
243 i += inc;
244 }
245
246 mutex_unlock(&d->i2c_mutex);
247
248 return ret ? ret : i;
249}
250
251static u32 anysee_i2c_func(struct i2c_adapter *adapter)
252{
253 return I2C_FUNC_I2C;
254}
255
256static struct i2c_algorithm anysee_i2c_algo = {
257 .master_xfer = anysee_master_xfer,
258 .functionality = anysee_i2c_func,
259};
260
261static int anysee_mt352_demod_init(struct dvb_frontend *fe)
262{
263 static u8 clock_config[] = { CLOCK_CTL, 0x38, 0x28 };
264 static u8 reset[] = { RESET, 0x80 };
265 static u8 adc_ctl_1_cfg[] = { ADC_CTL_1, 0x40 };
266 static u8 agc_cfg[] = { AGC_TARGET, 0x28, 0x20 };
267 static u8 gpp_ctl_cfg[] = { GPP_CTL, 0x33 };
268 static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
269
270 mt352_write(fe, clock_config, sizeof(clock_config));
271 udelay(200);
272 mt352_write(fe, reset, sizeof(reset));
273 mt352_write(fe, adc_ctl_1_cfg, sizeof(adc_ctl_1_cfg));
274
275 mt352_write(fe, agc_cfg, sizeof(agc_cfg));
276 mt352_write(fe, gpp_ctl_cfg, sizeof(gpp_ctl_cfg));
277 mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
278
279 return 0;
280}
281
282
283static struct tda10023_config anysee_tda10023_config = {
284 .demod_address = (0x1a >> 1),
285 .invert = 0,
286 .xtal = 16000000,
287 .pll_m = 11,
288 .pll_p = 3,
289 .pll_n = 1,
290 .output_mode = TDA10023_OUTPUT_MODE_PARALLEL_C,
291 .deltaf = 0xfeeb,
292};
293
294static struct mt352_config anysee_mt352_config = {
295 .demod_address = (0x1e >> 1),
296 .demod_init = anysee_mt352_demod_init,
297};
298
299static struct zl10353_config anysee_zl10353_config = {
300 .demod_address = (0x1e >> 1),
301 .parallel_ts = 1,
302};
303
304static struct zl10353_config anysee_zl10353_tda18212_config2 = {
305 .demod_address = (0x1e >> 1),
306 .parallel_ts = 1,
307 .disable_i2c_gate_ctrl = 1,
308 .no_tuner = 1,
309 .if2 = 41500,
310};
311
312static struct zl10353_config anysee_zl10353_tda18212_config = {
313 .demod_address = (0x18 >> 1),
314 .parallel_ts = 1,
315 .disable_i2c_gate_ctrl = 1,
316 .no_tuner = 1,
317 .if2 = 41500,
318};
319
320static struct tda10023_config anysee_tda10023_tda18212_config = {
321 .demod_address = (0x1a >> 1),
322 .xtal = 16000000,
323 .pll_m = 12,
324 .pll_p = 3,
325 .pll_n = 1,
326 .output_mode = TDA10023_OUTPUT_MODE_PARALLEL_B,
327 .deltaf = 0xba02,
328};
329
330static struct tda18212_config anysee_tda18212_config = {
331 .if_dvbt_6 = 4150,
332 .if_dvbt_7 = 4150,
333 .if_dvbt_8 = 4150,
334 .if_dvbc = 5000,
335};
336
337static struct tda18212_config anysee_tda18212_config2 = {
338 .if_dvbt_6 = 3550,
339 .if_dvbt_7 = 3700,
340 .if_dvbt_8 = 4150,
341 .if_dvbt2_6 = 3250,
342 .if_dvbt2_7 = 4000,
343 .if_dvbt2_8 = 4000,
344 .if_dvbc = 5000,
345};
346
347static struct cx24116_config anysee_cx24116_config = {
348 .demod_address = (0xaa >> 1),
349 .mpg_clk_pos_pol = 0x00,
350 .i2c_wr_max = 48,
351};
352
353static struct stv0900_config anysee_stv0900_config = {
354 .demod_address = (0xd0 >> 1),
355 .demod_mode = 0,
356 .xtal = 8000000,
357 .clkmode = 3,
358 .diseqc_mode = 2,
359 .tun1_maddress = 0,
360 .tun1_adc = 1,
361 .path1_mode = 3,
362};
363
364static struct stv6110_config anysee_stv6110_config = {
365 .i2c_address = (0xc0 >> 1),
366 .mclk = 16000000,
367 .clk_div = 1,
368};
369
370static struct isl6423_config anysee_isl6423_config = {
371 .current_max = SEC_CURRENT_800m,
372 .curlim = SEC_CURRENT_LIM_OFF,
373 .mod_extern = 1,
374 .addr = (0x10 >> 1),
375};
376
377static struct cxd2820r_config anysee_cxd2820r_config = {
378 .i2c_address = 0x6d,
379 .ts_mode = 0x38,
380};
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498static int anysee_read_config(struct dvb_usb_device *d)
499{
500 struct anysee_state *state = d_to_priv(d);
501 int ret;
502 u8 hw_info[3];
503
504
505
506
507
508 ret = anysee_get_hw_info(d, hw_info);
509 if (ret)
510 goto error;
511
512 ret = anysee_get_hw_info(d, hw_info);
513 if (ret)
514 goto error;
515
516
517
518
519 dev_info(&d->udev->dev, "%s: firmware version %d.%d hardware id %d\n",
520 KBUILD_MODNAME, hw_info[1], hw_info[2], hw_info[0]);
521
522 state->hw = hw_info[0];
523error:
524 return ret;
525}
526
527
528static int anysee_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
529{
530
531 return anysee_wr_reg_mask(fe_to_d(fe), REG_IOE, (enable << 4), 0x10);
532}
533
534static int anysee_frontend_ctrl(struct dvb_frontend *fe, int onoff)
535{
536 struct anysee_state *state = fe_to_priv(fe);
537 struct dvb_usb_device *d = fe_to_d(fe);
538 int ret;
539 dev_dbg(&d->udev->dev, "%s: fe=%d onoff=%d\n", __func__, fe->id, onoff);
540
541
542 if (onoff == 0)
543 return 0;
544
545 switch (state->hw) {
546 case ANYSEE_HW_507FA:
547
548
549
550 if (fe->id == 0) {
551
552 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
553 if (ret)
554 goto error;
555
556
557 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
558 if (ret)
559 goto error;
560
561
562 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
563 if (ret)
564 goto error;
565 } else {
566
567 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
568 if (ret)
569 goto error;
570
571
572 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
573 if (ret)
574 goto error;
575
576
577 ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
578 if (ret)
579 goto error;
580 }
581
582 break;
583 case ANYSEE_HW_508TC:
584 case ANYSEE_HW_508PTC:
585
586
587
588 if (fe->id == 0) {
589
590 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
591 if (ret)
592 goto error;
593
594
595 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
596 if (ret)
597 goto error;
598
599
600 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 0), 0x01);
601 if (ret)
602 goto error;
603 } else {
604
605 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
606 if (ret)
607 goto error;
608
609
610 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
611 if (ret)
612 goto error;
613
614
615 ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 0), 0x01);
616 if (ret)
617 goto error;
618 }
619
620 break;
621 default:
622 ret = 0;
623 }
624
625error:
626 return ret;
627}
628
629static int anysee_add_i2c_dev(struct dvb_usb_device *d, const char *type,
630 u8 addr, void *platform_data)
631{
632 int ret, num;
633 struct anysee_state *state = d_to_priv(d);
634 struct i2c_client *client;
635 struct i2c_adapter *adapter = &d->i2c_adap;
636 struct i2c_board_info board_info = {
637 .addr = addr,
638 .platform_data = platform_data,
639 };
640
641 strlcpy(board_info.type, type, I2C_NAME_SIZE);
642
643
644 for (num = 0; num < ANYSEE_I2C_CLIENT_MAX; num++) {
645 if (state->i2c_client[num] == NULL)
646 break;
647 }
648
649 dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
650
651 if (num == ANYSEE_I2C_CLIENT_MAX) {
652 dev_err(&d->udev->dev, "%s: I2C client out of index\n",
653 KBUILD_MODNAME);
654 ret = -ENODEV;
655 goto err;
656 }
657
658 request_module("%s", board_info.type);
659
660
661 client = i2c_new_device(adapter, &board_info);
662 if (client == NULL || client->dev.driver == NULL) {
663 ret = -ENODEV;
664 goto err;
665 }
666
667
668 if (!try_module_get(client->dev.driver->owner)) {
669 i2c_unregister_device(client);
670 ret = -ENODEV;
671 goto err;
672 }
673
674 state->i2c_client[num] = client;
675 return 0;
676err:
677 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
678 return ret;
679}
680
681static void anysee_del_i2c_dev(struct dvb_usb_device *d)
682{
683 int num;
684 struct anysee_state *state = d_to_priv(d);
685 struct i2c_client *client;
686
687
688 num = ANYSEE_I2C_CLIENT_MAX;
689 while (num--) {
690 if (state->i2c_client[num] != NULL)
691 break;
692 }
693
694 dev_dbg(&d->udev->dev, "%s: num=%d\n", __func__, num);
695
696 if (num == -1) {
697 dev_err(&d->udev->dev, "%s: I2C client out of index\n",
698 KBUILD_MODNAME);
699 goto err;
700 }
701
702 client = state->i2c_client[num];
703
704
705 module_put(client->dev.driver->owner);
706
707
708 i2c_unregister_device(client);
709
710 state->i2c_client[num] = NULL;
711err:
712 dev_dbg(&d->udev->dev, "%s: failed\n", __func__);
713}
714
715static int anysee_frontend_attach(struct dvb_usb_adapter *adap)
716{
717 struct anysee_state *state = adap_to_priv(adap);
718 struct dvb_usb_device *d = adap_to_d(adap);
719 int ret = 0;
720 u8 tmp;
721 struct i2c_msg msg[2] = {
722 {
723 .addr = 0x60,
724 .flags = 0,
725 .len = 1,
726 .buf = "\x00",
727 }, {
728 .addr = 0x60,
729 .flags = I2C_M_RD,
730 .len = 1,
731 .buf = &tmp,
732 }
733 };
734
735 switch (state->hw) {
736 case ANYSEE_HW_507T:
737
738
739
740 adap->fe[0] = dvb_attach(mt352_attach, &anysee_mt352_config,
741 &d->i2c_adap);
742 if (adap->fe[0])
743 break;
744
745
746 adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
747 &d->i2c_adap);
748
749 break;
750 case ANYSEE_HW_507CD:
751
752
753
754 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
755 if (ret)
756 goto error;
757
758
759 ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
760 if (ret)
761 goto error;
762
763
764 adap->fe[0] = dvb_attach(zl10353_attach, &anysee_zl10353_config,
765 &d->i2c_adap);
766
767 break;
768 case ANYSEE_HW_507DC:
769
770
771
772 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
773 if (ret)
774 goto error;
775
776
777 adap->fe[0] = dvb_attach(tda10023_attach,
778 &anysee_tda10023_config, &d->i2c_adap, 0x48);
779
780 break;
781 case ANYSEE_HW_507SI:
782
783
784
785 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
786 if (ret)
787 goto error;
788
789
790 adap->fe[0] = dvb_attach(cx24116_attach, &anysee_cx24116_config,
791 &d->i2c_adap);
792
793 break;
794 case ANYSEE_HW_507FA:
795
796
797
798
799 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 4), 0x10);
800 if (ret)
801 goto error;
802
803
804 tmp = 0;
805 ret = i2c_transfer(&d->i2c_adap, msg, 2);
806 if (ret == 2 && tmp == 0xc7) {
807 dev_dbg(&d->udev->dev, "%s: TDA18212 found\n",
808 __func__);
809 state->has_tda18212 = true;
810 }
811 else
812 tmp = 0;
813
814
815 ret = anysee_wr_reg_mask(d, REG_IOE, (0 << 4), 0x10);
816 if (ret)
817 goto error;
818
819
820 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 0), 0x01);
821 if (ret)
822 goto error;
823
824
825 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
826 if (ret)
827 goto error;
828
829
830 if (tmp == 0xc7) {
831
832 adap->fe[0] = dvb_attach(tda10023_attach,
833 &anysee_tda10023_tda18212_config,
834 &d->i2c_adap, 0x48);
835
836
837 if (adap->fe[0])
838 adap->fe[0]->ops.i2c_gate_ctrl =
839 anysee_i2c_gate_ctrl;
840 } else {
841
842 adap->fe[0] = dvb_attach(tda10023_attach,
843 &anysee_tda10023_config,
844 &d->i2c_adap, 0x48);
845 }
846
847
848 if (!adap->fe[0])
849 break;
850
851
852 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
853 if (ret)
854 goto error;
855
856
857 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 0), 0x01);
858 if (ret)
859 goto error;
860
861
862 if (tmp == 0xc7) {
863
864 adap->fe[1] = dvb_attach(zl10353_attach,
865 &anysee_zl10353_tda18212_config2,
866 &d->i2c_adap);
867
868
869 if (adap->fe[1])
870 adap->fe[1]->ops.i2c_gate_ctrl =
871 anysee_i2c_gate_ctrl;
872 } else {
873
874 adap->fe[1] = dvb_attach(zl10353_attach,
875 &anysee_zl10353_config,
876 &d->i2c_adap);
877 }
878
879 break;
880 case ANYSEE_HW_508TC:
881 case ANYSEE_HW_508PTC:
882
883
884
885
886 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 6), 0x40);
887 if (ret)
888 goto error;
889
890
891 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 5), 0x20);
892 if (ret)
893 goto error;
894
895
896 adap->fe[0] = dvb_attach(tda10023_attach,
897 &anysee_tda10023_tda18212_config,
898 &d->i2c_adap, 0x48);
899
900
901 if (adap->fe[0])
902 adap->fe[0]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
903
904
905 if (!adap->fe[0])
906 break;
907
908
909 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 5), 0x20);
910 if (ret)
911 goto error;
912
913
914 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 6), 0x40);
915 if (ret)
916 goto error;
917
918
919 adap->fe[1] = dvb_attach(zl10353_attach,
920 &anysee_zl10353_tda18212_config,
921 &d->i2c_adap);
922
923
924 if (adap->fe[1])
925 adap->fe[1]->ops.i2c_gate_ctrl = anysee_i2c_gate_ctrl;
926
927 state->has_ci = true;
928
929 break;
930 case ANYSEE_HW_508S2:
931 case ANYSEE_HW_508PS2:
932
933
934
935
936 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
937 if (ret)
938 goto error;
939
940
941 adap->fe[0] = dvb_attach(stv0900_attach,
942 &anysee_stv0900_config, &d->i2c_adap, 0);
943
944 state->has_ci = true;
945
946 break;
947 case ANYSEE_HW_508T2C:
948
949
950
951 ret = anysee_wr_reg_mask(d, REG_IOE, (1 << 5), 0x20);
952 if (ret)
953 goto error;
954
955
956 adap->fe[0] = dvb_attach(cxd2820r_attach,
957 &anysee_cxd2820r_config, &d->i2c_adap, NULL);
958
959 state->has_ci = true;
960
961 break;
962 }
963
964 if (!adap->fe[0]) {
965
966 ret = -ENODEV;
967 dev_err(&d->udev->dev,
968 "%s: Unsupported Anysee version. Please report to <linux-media@vger.kernel.org>.\n",
969 KBUILD_MODNAME);
970 }
971error:
972 return ret;
973}
974
975static int anysee_tuner_attach(struct dvb_usb_adapter *adap)
976{
977 struct anysee_state *state = adap_to_priv(adap);
978 struct dvb_usb_device *d = adap_to_d(adap);
979 struct dvb_frontend *fe;
980 int ret;
981 dev_dbg(&d->udev->dev, "%s:\n", __func__);
982
983 switch (state->hw) {
984 case ANYSEE_HW_507T:
985
986
987
988 fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1), NULL,
989 DVB_PLL_THOMSON_DTT7579);
990
991 break;
992 case ANYSEE_HW_507CD:
993
994
995
996 fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc2 >> 1),
997 &d->i2c_adap, DVB_PLL_THOMSON_DTT7579);
998
999 break;
1000 case ANYSEE_HW_507DC:
1001
1002
1003
1004 fe = dvb_attach(dvb_pll_attach, adap->fe[0], (0xc0 >> 1),
1005 &d->i2c_adap, DVB_PLL_SAMSUNG_DTOS403IH102A);
1006
1007 break;
1008 case ANYSEE_HW_507SI:
1009
1010
1011
1012 fe = dvb_attach(isl6423_attach, adap->fe[0], &d->i2c_adap,
1013 &anysee_isl6423_config);
1014
1015 break;
1016 case ANYSEE_HW_507FA:
1017
1018
1019
1020
1021
1022
1023
1024 if (state->has_tda18212) {
1025 struct tda18212_config tda18212_config =
1026 anysee_tda18212_config;
1027
1028 tda18212_config.fe = adap->fe[0];
1029 ret = anysee_add_i2c_dev(d, "tda18212", 0x60,
1030 &tda18212_config);
1031 if (ret)
1032 goto err;
1033
1034
1035 if (adap->fe[1]) {
1036 adap->fe[1]->tuner_priv =
1037 adap->fe[0]->tuner_priv;
1038 memcpy(&adap->fe[1]->ops.tuner_ops,
1039 &adap->fe[0]->ops.tuner_ops,
1040 sizeof(struct dvb_tuner_ops));
1041 }
1042
1043 return 0;
1044 } else {
1045
1046 fe = dvb_attach(dvb_pll_attach, adap->fe[0],
1047 (0xc0 >> 1), &d->i2c_adap,
1048 DVB_PLL_SAMSUNG_DTOS403IH102A);
1049
1050 if (fe && adap->fe[1]) {
1051
1052 fe = dvb_attach(dvb_pll_attach, adap->fe[1],
1053 (0xc0 >> 1), &d->i2c_adap,
1054 DVB_PLL_SAMSUNG_DTOS403IH102A);
1055 }
1056 }
1057
1058 break;
1059 case ANYSEE_HW_508TC:
1060 case ANYSEE_HW_508PTC:
1061 {
1062
1063
1064 struct tda18212_config tda18212_config = anysee_tda18212_config;
1065
1066 tda18212_config.fe = adap->fe[0];
1067 ret = anysee_add_i2c_dev(d, "tda18212", 0x60, &tda18212_config);
1068 if (ret)
1069 goto err;
1070
1071
1072 if (adap->fe[1]) {
1073 adap->fe[1]->tuner_priv = adap->fe[0]->tuner_priv;
1074 memcpy(&adap->fe[1]->ops.tuner_ops,
1075 &adap->fe[0]->ops.tuner_ops,
1076 sizeof(struct dvb_tuner_ops));
1077 }
1078
1079 return 0;
1080 }
1081 case ANYSEE_HW_508S2:
1082 case ANYSEE_HW_508PS2:
1083
1084
1085
1086
1087 fe = dvb_attach(stv6110_attach, adap->fe[0],
1088 &anysee_stv6110_config, &d->i2c_adap);
1089
1090 if (fe) {
1091
1092 fe = dvb_attach(isl6423_attach, adap->fe[0],
1093 &d->i2c_adap, &anysee_isl6423_config);
1094 }
1095
1096 break;
1097
1098 case ANYSEE_HW_508T2C:
1099 {
1100
1101 struct tda18212_config tda18212_config =
1102 anysee_tda18212_config2;
1103
1104 tda18212_config.fe = adap->fe[0];
1105 ret = anysee_add_i2c_dev(d, "tda18212", 0x60, &tda18212_config);
1106 if (ret)
1107 goto err;
1108
1109 return 0;
1110 }
1111 default:
1112 fe = NULL;
1113 }
1114
1115 if (fe)
1116 ret = 0;
1117 else
1118 ret = -ENODEV;
1119err:
1120 return ret;
1121}
1122
1123#if IS_ENABLED(CONFIG_RC_CORE)
1124static int anysee_rc_query(struct dvb_usb_device *d)
1125{
1126 u8 buf[] = {CMD_GET_IR_CODE};
1127 u8 ircode[2];
1128 int ret;
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138 ret = anysee_ctrl_msg(d, buf, sizeof(buf), ircode, sizeof(ircode));
1139 if (ret)
1140 return ret;
1141
1142 if (ircode[0]) {
1143 dev_dbg(&d->udev->dev, "%s: key pressed %02x\n", __func__,
1144 ircode[1]);
1145 rc_keydown(d->rc_dev, RC_PROTO_NEC,
1146 RC_SCANCODE_NEC(0x08, ircode[1]), 0);
1147 }
1148
1149 return 0;
1150}
1151
1152static int anysee_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
1153{
1154 rc->allowed_protos = RC_PROTO_BIT_NEC;
1155 rc->query = anysee_rc_query;
1156 rc->interval = 250;
1157
1158 return 0;
1159}
1160#else
1161 #define anysee_get_rc_config NULL
1162#endif
1163
1164static int anysee_ci_read_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1165 int addr)
1166{
1167 struct dvb_usb_device *d = ci->data;
1168 int ret;
1169 u8 buf[] = {CMD_CI, 0x02, 0x40 | addr >> 8, addr & 0xff, 0x00, 1};
1170 u8 val;
1171
1172 ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1173 if (ret)
1174 return ret;
1175
1176 return val;
1177}
1178
1179static int anysee_ci_write_attribute_mem(struct dvb_ca_en50221 *ci, int slot,
1180 int addr, u8 val)
1181{
1182 struct dvb_usb_device *d = ci->data;
1183 int ret;
1184 u8 buf[] = {CMD_CI, 0x03, 0x40 | addr >> 8, addr & 0xff, 0x00, 1, val};
1185
1186 ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1187 if (ret)
1188 return ret;
1189
1190 return 0;
1191}
1192
1193static int anysee_ci_read_cam_control(struct dvb_ca_en50221 *ci, int slot,
1194 u8 addr)
1195{
1196 struct dvb_usb_device *d = ci->data;
1197 int ret;
1198 u8 buf[] = {CMD_CI, 0x04, 0x40, addr, 0x00, 1};
1199 u8 val;
1200
1201 ret = anysee_ctrl_msg(d, buf, sizeof(buf), &val, 1);
1202 if (ret)
1203 return ret;
1204
1205 return val;
1206}
1207
1208static int anysee_ci_write_cam_control(struct dvb_ca_en50221 *ci, int slot,
1209 u8 addr, u8 val)
1210{
1211 struct dvb_usb_device *d = ci->data;
1212 int ret;
1213 u8 buf[] = {CMD_CI, 0x05, 0x40, addr, 0x00, 1, val};
1214
1215 ret = anysee_ctrl_msg(d, buf, sizeof(buf), NULL, 0);
1216 if (ret)
1217 return ret;
1218
1219 return 0;
1220}
1221
1222static int anysee_ci_slot_reset(struct dvb_ca_en50221 *ci, int slot)
1223{
1224 struct dvb_usb_device *d = ci->data;
1225 int ret;
1226 struct anysee_state *state = d_to_priv(d);
1227
1228 state->ci_cam_ready = jiffies + msecs_to_jiffies(1000);
1229
1230 ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1231 if (ret)
1232 return ret;
1233
1234 msleep(300);
1235
1236 ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1237 if (ret)
1238 return ret;
1239
1240 return 0;
1241}
1242
1243static int anysee_ci_slot_shutdown(struct dvb_ca_en50221 *ci, int slot)
1244{
1245 struct dvb_usb_device *d = ci->data;
1246 int ret;
1247
1248 ret = anysee_wr_reg_mask(d, REG_IOA, (0 << 7), 0x80);
1249 if (ret)
1250 return ret;
1251
1252 msleep(30);
1253
1254 ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1255 if (ret)
1256 return ret;
1257
1258 return 0;
1259}
1260
1261static int anysee_ci_slot_ts_enable(struct dvb_ca_en50221 *ci, int slot)
1262{
1263 struct dvb_usb_device *d = ci->data;
1264 int ret;
1265
1266 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 1), 0x02);
1267 if (ret)
1268 return ret;
1269
1270 return 0;
1271}
1272
1273static int anysee_ci_poll_slot_status(struct dvb_ca_en50221 *ci, int slot,
1274 int open)
1275{
1276 struct dvb_usb_device *d = ci->data;
1277 struct anysee_state *state = d_to_priv(d);
1278 int ret;
1279 u8 tmp = 0;
1280
1281 ret = anysee_rd_reg_mask(d, REG_IOC, &tmp, 0x40);
1282 if (ret)
1283 return ret;
1284
1285 if (tmp == 0) {
1286 ret = DVB_CA_EN50221_POLL_CAM_PRESENT;
1287 if (time_after(jiffies, state->ci_cam_ready))
1288 ret |= DVB_CA_EN50221_POLL_CAM_READY;
1289 }
1290
1291 return ret;
1292}
1293
1294static int anysee_ci_init(struct dvb_usb_device *d)
1295{
1296 struct anysee_state *state = d_to_priv(d);
1297 int ret;
1298
1299 state->ci.owner = THIS_MODULE;
1300 state->ci.read_attribute_mem = anysee_ci_read_attribute_mem;
1301 state->ci.write_attribute_mem = anysee_ci_write_attribute_mem;
1302 state->ci.read_cam_control = anysee_ci_read_cam_control;
1303 state->ci.write_cam_control = anysee_ci_write_cam_control;
1304 state->ci.slot_reset = anysee_ci_slot_reset;
1305 state->ci.slot_shutdown = anysee_ci_slot_shutdown;
1306 state->ci.slot_ts_enable = anysee_ci_slot_ts_enable;
1307 state->ci.poll_slot_status = anysee_ci_poll_slot_status;
1308 state->ci.data = d;
1309
1310 ret = anysee_wr_reg_mask(d, REG_IOA, (1 << 7), 0x80);
1311 if (ret)
1312 return ret;
1313
1314 ret = anysee_wr_reg_mask(d, REG_IOD, (0 << 2)|(0 << 1)|(0 << 0), 0x07);
1315 if (ret)
1316 return ret;
1317
1318 ret = anysee_wr_reg_mask(d, REG_IOD, (1 << 2)|(1 << 1)|(1 << 0), 0x07);
1319 if (ret)
1320 return ret;
1321
1322 ret = dvb_ca_en50221_init(&d->adapter[0].dvb_adap, &state->ci, 0, 1);
1323 if (ret)
1324 return ret;
1325
1326 state->ci_attached = true;
1327
1328 return 0;
1329}
1330
1331static void anysee_ci_release(struct dvb_usb_device *d)
1332{
1333 struct anysee_state *state = d_to_priv(d);
1334
1335
1336 if (state->ci_attached)
1337 dvb_ca_en50221_release(&state->ci);
1338
1339 return;
1340}
1341
1342static int anysee_init(struct dvb_usb_device *d)
1343{
1344 struct anysee_state *state = d_to_priv(d);
1345 int ret;
1346
1347
1348
1349
1350
1351 ret = usb_set_interface(d->udev, 0, 0);
1352 if (ret)
1353 return ret;
1354
1355
1356 ret = anysee_led_ctrl(d, 0x01, 0x03);
1357 if (ret)
1358 return ret;
1359
1360
1361 ret = anysee_ir_ctrl(d, 1);
1362 if (ret)
1363 return ret;
1364
1365
1366 if (state->has_ci) {
1367 ret = anysee_ci_init(d);
1368 if (ret)
1369 return ret;
1370 }
1371
1372 return 0;
1373}
1374
1375static void anysee_exit(struct dvb_usb_device *d)
1376{
1377 struct anysee_state *state = d_to_priv(d);
1378
1379 if (state->i2c_client[0])
1380 anysee_del_i2c_dev(d);
1381
1382 return anysee_ci_release(d);
1383}
1384
1385
1386static struct dvb_usb_device_properties anysee_props = {
1387 .driver_name = KBUILD_MODNAME,
1388 .owner = THIS_MODULE,
1389 .adapter_nr = adapter_nr,
1390 .size_of_priv = sizeof(struct anysee_state),
1391
1392 .generic_bulk_ctrl_endpoint = 0x01,
1393 .generic_bulk_ctrl_endpoint_response = 0x81,
1394
1395 .i2c_algo = &anysee_i2c_algo,
1396 .read_config = anysee_read_config,
1397 .frontend_attach = anysee_frontend_attach,
1398 .tuner_attach = anysee_tuner_attach,
1399 .init = anysee_init,
1400 .get_rc_config = anysee_get_rc_config,
1401 .frontend_ctrl = anysee_frontend_ctrl,
1402 .streaming_ctrl = anysee_streaming_ctrl,
1403 .exit = anysee_exit,
1404
1405 .num_adapters = 1,
1406 .adapter = {
1407 {
1408 .stream = DVB_USB_STREAM_BULK(0x82, 8, 16 * 512),
1409 }
1410 }
1411};
1412
1413static const struct usb_device_id anysee_id_table[] = {
1414 { DVB_USB_DEVICE(USB_VID_CYPRESS, USB_PID_ANYSEE,
1415 &anysee_props, "Anysee", RC_MAP_ANYSEE) },
1416 { DVB_USB_DEVICE(USB_VID_AMT, USB_PID_ANYSEE,
1417 &anysee_props, "Anysee", RC_MAP_ANYSEE) },
1418 { }
1419};
1420MODULE_DEVICE_TABLE(usb, anysee_id_table);
1421
1422static struct usb_driver anysee_usb_driver = {
1423 .name = KBUILD_MODNAME,
1424 .id_table = anysee_id_table,
1425 .probe = dvb_usbv2_probe,
1426 .disconnect = dvb_usbv2_disconnect,
1427 .suspend = dvb_usbv2_suspend,
1428 .resume = dvb_usbv2_resume,
1429 .reset_resume = dvb_usbv2_reset_resume,
1430 .no_dynamic_id = 1,
1431 .soft_unbind = 1,
1432};
1433
1434module_usb_driver(anysee_usb_driver);
1435
1436MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1437MODULE_DESCRIPTION("Driver Anysee E30 DVB-C & DVB-T USB2.0");
1438MODULE_LICENSE("GPL");
1439