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