1 // SPDX-License-Identifier: GPL-2.0-only
2 /* DVB USB compliant linux driver for Technotrend DVB USB boxes and clones
3 * (e.g. Pinnacle 400e DVB-S USB2.0).
5 * The Pinnacle 400e uses the same protocol as the Technotrend USB1.1 boxes.
10 * 0x08 - LNBP21PD - LNB power supply
11 * 0x0e - TDA10086 - Demodulator
13 * 0x60 - TDA8263 - Tuner
16 * Copyright (c) 2002 Holger Waechtler <holger@convergence.de>
17 * Copyright (c) 2003 Felix Domke <tmbinc@elitedvb.net>
18 * Copyright (C) 2005-6 Patrick Boettcher <pb@linuxtv.org>
20 * see Documentation/media/dvb-drivers/dvb-usb.rst for more information
22 #define DVB_USB_LOG_PREFIX "ttusb2"
34 #include <media/dvb_ca_en50221.h>
37 static int dvb_usb_ttusb2_debug
;
38 #define deb_info(args...) dprintk(dvb_usb_ttusb2_debug,0x01,args)
39 module_param_named(debug
,dvb_usb_ttusb2_debug
, int, 0644);
40 MODULE_PARM_DESC(debug
, "set debugging level (1=info (or-able))." DVB_USB_DEBUG_STATUS
);
41 static int dvb_usb_ttusb2_debug_ci
;
42 module_param_named(debug_ci
,dvb_usb_ttusb2_debug_ci
, int, 0644);
43 MODULE_PARM_DESC(debug_ci
, "set debugging ci." DVB_USB_DEBUG_STATUS
);
45 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
47 #define ci_dbg(format, arg...) \
49 if (dvb_usb_ttusb2_debug_ci) \
50 printk(KERN_DEBUG DVB_USB_LOG_PREFIX \
51 ": %s " format "\n" , __func__, ## arg); \
55 TT3650_CMD_CI_TEST
= 0x40,
56 TT3650_CMD_CI_RD_CTRL
,
57 TT3650_CMD_CI_WR_CTRL
,
58 TT3650_CMD_CI_RD_ATTR
,
59 TT3650_CMD_CI_WR_ATTR
,
61 TT3650_CMD_CI_SET_VIDEO_PORT
65 struct dvb_ca_en50221 ca
;
66 struct mutex ca_mutex
;
71 static int ttusb2_msg(struct dvb_usb_device
*d
, u8 cmd
,
72 u8
*wbuf
, int wlen
, u8
*rbuf
, int rlen
)
74 struct ttusb2_state
*st
= d
->priv
;
81 s
= kzalloc(wlen
+4, GFP_KERNEL
);
85 r
= kzalloc(64, GFP_KERNEL
);
95 memcpy(&s
[4],wbuf
,wlen
);
97 ret
= dvb_usb_generic_rw(d
, s
, wlen
+4, r
, 64, 0);
103 (rlen
> 0 && r
[3] != rlen
)) {
104 warn("there might have been an error during control message transfer. (rlen = %d, was %d)",rlen
,r
[3]);
111 memcpy(rbuf
, &r
[4], rlen
);
120 static int tt3650_ci_msg(struct dvb_usb_device
*d
, u8 cmd
, u8
*data
, unsigned int write_len
, unsigned int read_len
)
123 u8 rx
[60];/* (64 -4) */
124 ret
= ttusb2_msg(d
, cmd
, data
, write_len
, rx
, read_len
);
126 memcpy(data
, rx
, read_len
);
130 static int tt3650_ci_msg_locked(struct dvb_ca_en50221
*ca
, u8 cmd
, u8
*data
, unsigned int write_len
, unsigned int read_len
)
132 struct dvb_usb_device
*d
= ca
->data
;
133 struct ttusb2_state
*state
= d
->priv
;
136 mutex_lock(&state
->ca_mutex
);
137 ret
= tt3650_ci_msg(d
, cmd
, data
, write_len
, read_len
);
138 mutex_unlock(&state
->ca_mutex
);
143 static int tt3650_ci_read_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
, int address
)
151 buf
[0] = (address
>> 8) & 0x0F;
155 ret
= tt3650_ci_msg_locked(ca
, TT3650_CMD_CI_RD_ATTR
, buf
, 2, 3);
157 ci_dbg("%04x -> %d 0x%02x", address
, ret
, buf
[2]);
165 static int tt3650_ci_write_attribute_mem(struct dvb_ca_en50221
*ca
, int slot
, int address
, u8 value
)
169 ci_dbg("%d 0x%04x 0x%02x", slot
, address
, value
);
174 buf
[0] = (address
>> 8) & 0x0F;
178 return tt3650_ci_msg_locked(ca
, TT3650_CMD_CI_WR_ATTR
, buf
, 3, 3);
181 static int tt3650_ci_read_cam_control(struct dvb_ca_en50221
*ca
, int slot
, u8 address
)
189 buf
[0] = address
& 3;
191 ret
= tt3650_ci_msg_locked(ca
, TT3650_CMD_CI_RD_CTRL
, buf
, 1, 2);
193 ci_dbg("0x%02x -> %d 0x%02x", address
, ret
, buf
[1]);
201 static int tt3650_ci_write_cam_control(struct dvb_ca_en50221
*ca
, int slot
, u8 address
, u8 value
)
205 ci_dbg("%d 0x%02x 0x%02x", slot
, address
, value
);
213 return tt3650_ci_msg_locked(ca
, TT3650_CMD_CI_WR_CTRL
, buf
, 2, 2);
216 static int tt3650_ci_set_video_port(struct dvb_ca_en50221
*ca
, int slot
, int enable
)
221 ci_dbg("%d %d", slot
, enable
);
228 ret
= tt3650_ci_msg_locked(ca
, TT3650_CMD_CI_SET_VIDEO_PORT
, buf
, 1, 1);
232 if (enable
!= buf
[0]) {
233 err("CI not %sabled.", enable
? "en" : "dis");
240 static int tt3650_ci_slot_shutdown(struct dvb_ca_en50221
*ca
, int slot
)
242 return tt3650_ci_set_video_port(ca
, slot
, 0);
245 static int tt3650_ci_slot_ts_enable(struct dvb_ca_en50221
*ca
, int slot
)
247 return tt3650_ci_set_video_port(ca
, slot
, 1);
250 static int tt3650_ci_slot_reset(struct dvb_ca_en50221
*ca
, int slot
)
252 struct dvb_usb_device
*d
= ca
->data
;
253 struct ttusb2_state
*state
= d
->priv
;
264 mutex_lock(&state
->ca_mutex
);
266 ret
= tt3650_ci_msg(d
, TT3650_CMD_CI_RESET
, buf
, 1, 1);
274 ret
= tt3650_ci_msg(d
, TT3650_CMD_CI_RESET
, buf
, 1, 1);
280 buf
[0] = 0; /* FTA */
282 ret
= tt3650_ci_msg(d
, TT3650_CMD_CI_SET_VIDEO_PORT
, buf
, 1, 1);
287 mutex_unlock(&state
->ca_mutex
);
292 static int tt3650_ci_poll_slot_status(struct dvb_ca_en50221
*ca
, int slot
, int open
)
300 ret
= tt3650_ci_msg_locked(ca
, TT3650_CMD_CI_TEST
, buf
, 0, 1);
305 return DVB_CA_EN50221_POLL_CAM_PRESENT
|
306 DVB_CA_EN50221_POLL_CAM_READY
;
311 static void tt3650_ci_uninit(struct dvb_usb_device
*d
)
313 struct ttusb2_state
*state
;
324 if (NULL
== state
->ca
.data
)
327 dvb_ca_en50221_release(&state
->ca
);
329 memset(&state
->ca
, 0, sizeof(state
->ca
));
332 static int tt3650_ci_init(struct dvb_usb_adapter
*a
)
334 struct dvb_usb_device
*d
= a
->dev
;
335 struct ttusb2_state
*state
= d
->priv
;
340 mutex_init(&state
->ca_mutex
);
342 state
->ca
.owner
= THIS_MODULE
;
343 state
->ca
.read_attribute_mem
= tt3650_ci_read_attribute_mem
;
344 state
->ca
.write_attribute_mem
= tt3650_ci_write_attribute_mem
;
345 state
->ca
.read_cam_control
= tt3650_ci_read_cam_control
;
346 state
->ca
.write_cam_control
= tt3650_ci_write_cam_control
;
347 state
->ca
.slot_reset
= tt3650_ci_slot_reset
;
348 state
->ca
.slot_shutdown
= tt3650_ci_slot_shutdown
;
349 state
->ca
.slot_ts_enable
= tt3650_ci_slot_ts_enable
;
350 state
->ca
.poll_slot_status
= tt3650_ci_poll_slot_status
;
353 ret
= dvb_ca_en50221_init(&a
->dvb_adap
,
358 err("Cannot initialize CI: Error %d.", ret
);
359 memset(&state
->ca
, 0, sizeof(state
->ca
));
363 info("CI initialized.");
368 static int ttusb2_i2c_xfer(struct i2c_adapter
*adap
,struct i2c_msg msg
[],int num
)
370 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
371 static u8 obuf
[60], ibuf
[60];
372 int i
, write_read
, read
;
374 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
378 warn("more than 2 i2c messages at a time is not handled yet. TODO.");
380 for (i
= 0; i
< num
; i
++) {
381 write_read
= i
+1 < num
&& (msg
[i
+1].flags
& I2C_M_RD
);
382 read
= msg
[i
].flags
& I2C_M_RD
;
384 if (3 + msg
[i
].len
> sizeof(obuf
)) {
385 err("i2c wr len=%d too high", msg
[i
].len
);
389 if (3 + msg
[i
+1].len
> sizeof(ibuf
)) {
390 err("i2c rd len=%d too high", msg
[i
+1].len
);
394 if (3 + msg
[i
].len
> sizeof(ibuf
)) {
395 err("i2c rd len=%d too high", msg
[i
].len
);
400 obuf
[0] = (msg
[i
].addr
<< 1) | (write_read
| read
);
404 obuf
[1] = msg
[i
].len
;
408 obuf
[2] = msg
[i
+1].len
;
410 obuf
[2] = msg
[i
].len
;
414 memcpy(&obuf
[3], msg
[i
].buf
, msg
[i
].len
);
416 if (ttusb2_msg(d
, CMD_I2C_XFER
, obuf
, obuf
[1]+3, ibuf
, obuf
[2] + 3) < 0) {
417 err("i2c transfer failed.");
422 memcpy(msg
[i
+1].buf
, &ibuf
[3], msg
[i
+1].len
);
425 memcpy(msg
[i
].buf
, &ibuf
[3], msg
[i
].len
);
428 mutex_unlock(&d
->i2c_mutex
);
432 static u32
ttusb2_i2c_func(struct i2c_adapter
*adapter
)
437 static struct i2c_algorithm ttusb2_i2c_algo
= {
438 .master_xfer
= ttusb2_i2c_xfer
,
439 .functionality
= ttusb2_i2c_func
,
442 /* command to poll IR receiver (copied from pctv452e.c) */
443 #define CMD_GET_IR_CODE 0x1b
446 static int tt3650_rc_query(struct dvb_usb_device
*d
)
449 u8 rx
[9]; /* A CMD_GET_IR_CODE reply is 9 bytes long */
450 struct ttusb2_state
*st
= d
->priv
;
451 ret
= ttusb2_msg(d
, CMD_GET_IR_CODE
, NULL
, 0, rx
, sizeof(rx
));
456 /* got a "press" event */
457 st
->last_rc_key
= RC_SCANCODE_RC5(rx
[3], rx
[2]);
458 deb_info("%s: cmd=0x%02x sys=0x%02x\n", __func__
, rx
[2], rx
[3]);
459 rc_keydown(d
->rc_dev
, RC_PROTO_RC5
, st
->last_rc_key
, rx
[1]);
460 } else if (st
->last_rc_key
) {
469 /* Callbacks for DVB USB */
470 static int ttusb2_identify_state (struct usb_device
*udev
, struct
471 dvb_usb_device_properties
*props
, struct dvb_usb_device_description
**desc
,
474 *cold
= udev
->descriptor
.iManufacturer
== 0 && udev
->descriptor
.iProduct
== 0;
478 static int ttusb2_power_ctrl(struct dvb_usb_device
*d
, int onoff
)
481 ttusb2_msg(d
, CMD_POWER
, &b
, 0, NULL
, 0);
482 return ttusb2_msg(d
, CMD_POWER
, &b
, 1, NULL
, 0);
486 static struct tda10086_config tda10086_config
= {
487 .demod_address
= 0x0e,
490 .xtal_freq
= TDA10086_XTAL_16M
,
493 static struct tda10023_config tda10023_config
= {
494 .demod_address
= 0x0c,
503 static struct tda10048_config tda10048_config
= {
504 .demod_address
= 0x10 >> 1,
505 .output_mode
= TDA10048_PARALLEL_OUTPUT
,
506 .inversion
= TDA10048_INVERSION_ON
,
507 .dtv6_if_freq_khz
= TDA10048_IF_4000
,
508 .dtv7_if_freq_khz
= TDA10048_IF_4500
,
509 .dtv8_if_freq_khz
= TDA10048_IF_5000
,
510 .clk_freq_khz
= TDA10048_CLK_16000
,
518 static struct tda827x_config tda827x_config
= {
522 static int ttusb2_frontend_tda10086_attach(struct dvb_usb_adapter
*adap
)
524 if (usb_set_interface(adap
->dev
->udev
,0,3) < 0)
525 err("set interface to alts=3 failed");
527 if ((adap
->fe_adap
[0].fe
= dvb_attach(tda10086_attach
, &tda10086_config
, &adap
->dev
->i2c_adap
)) == NULL
) {
528 deb_info("TDA10086 attach failed\n");
535 static int ttusb2_ct3650_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
537 struct dvb_usb_adapter
*adap
= fe
->dvb
->priv
;
539 return adap
->fe_adap
[0].fe
->ops
.i2c_gate_ctrl(adap
->fe_adap
[0].fe
, enable
);
542 static int ttusb2_frontend_tda10023_attach(struct dvb_usb_adapter
*adap
)
544 if (usb_set_interface(adap
->dev
->udev
, 0, 3) < 0)
545 err("set interface to alts=3 failed");
547 if (adap
->fe_adap
[0].fe
== NULL
) {
549 adap
->fe_adap
[0].fe
= dvb_attach(tda10023_attach
,
550 &tda10023_config
, &adap
->dev
->i2c_adap
, 0x48);
552 if (adap
->fe_adap
[0].fe
== NULL
) {
553 deb_info("TDA10023 attach failed\n");
556 tt3650_ci_init(adap
);
558 adap
->fe_adap
[1].fe
= dvb_attach(tda10048_attach
,
559 &tda10048_config
, &adap
->dev
->i2c_adap
);
561 if (adap
->fe_adap
[1].fe
== NULL
) {
562 deb_info("TDA10048 attach failed\n");
566 /* tuner is behind TDA10023 I2C-gate */
567 adap
->fe_adap
[1].fe
->ops
.i2c_gate_ctrl
= ttusb2_ct3650_i2c_gate_ctrl
;
574 static int ttusb2_tuner_tda827x_attach(struct dvb_usb_adapter
*adap
)
576 struct dvb_frontend
*fe
;
578 /* MFE: select correct FE to attach tuner since that's called twice */
579 if (adap
->fe_adap
[1].fe
== NULL
)
580 fe
= adap
->fe_adap
[0].fe
;
582 fe
= adap
->fe_adap
[1].fe
;
585 if (dvb_attach(tda827x_attach
, fe
, 0x61, &adap
->dev
->i2c_adap
, &tda827x_config
) == NULL
) {
586 printk(KERN_ERR
"%s: No tda827x found!\n", __func__
);
592 static int ttusb2_tuner_tda826x_attach(struct dvb_usb_adapter
*adap
)
594 if (dvb_attach(tda826x_attach
, adap
->fe_adap
[0].fe
, 0x60, &adap
->dev
->i2c_adap
, 0) == NULL
) {
595 deb_info("TDA8263 attach failed\n");
599 if (dvb_attach(lnbp21_attach
, adap
->fe_adap
[0].fe
, &adap
->dev
->i2c_adap
, 0, 0) == NULL
) {
600 deb_info("LNBP21 attach failed\n");
606 /* DVB USB Driver stuff */
607 static struct dvb_usb_device_properties ttusb2_properties
;
608 static struct dvb_usb_device_properties ttusb2_properties_s2400
;
609 static struct dvb_usb_device_properties ttusb2_properties_ct3650
;
611 static void ttusb2_usb_disconnect(struct usb_interface
*intf
)
613 struct dvb_usb_device
*d
= usb_get_intfdata(intf
);
616 dvb_usb_device_exit(intf
);
619 static int ttusb2_probe(struct usb_interface
*intf
,
620 const struct usb_device_id
*id
)
622 if (0 == dvb_usb_device_init(intf
, &ttusb2_properties
,
623 THIS_MODULE
, NULL
, adapter_nr
) ||
624 0 == dvb_usb_device_init(intf
, &ttusb2_properties_s2400
,
625 THIS_MODULE
, NULL
, adapter_nr
) ||
626 0 == dvb_usb_device_init(intf
, &ttusb2_properties_ct3650
,
627 THIS_MODULE
, NULL
, adapter_nr
))
632 static struct usb_device_id ttusb2_table
[] = {
633 { USB_DEVICE(USB_VID_PINNACLE
, USB_PID_PCTV_400E
) },
634 { USB_DEVICE(USB_VID_PINNACLE
, USB_PID_PCTV_450E
) },
635 { USB_DEVICE(USB_VID_TECHNOTREND
,
636 USB_PID_TECHNOTREND_CONNECT_S2400
) },
637 { USB_DEVICE(USB_VID_TECHNOTREND
,
638 USB_PID_TECHNOTREND_CONNECT_CT3650
) },
639 { USB_DEVICE(USB_VID_TECHNOTREND
,
640 USB_PID_TECHNOTREND_CONNECT_S2400_8KEEPROM
) },
641 {} /* Terminating entry */
643 MODULE_DEVICE_TABLE (usb
, ttusb2_table
);
645 static struct dvb_usb_device_properties ttusb2_properties
= {
646 .caps
= DVB_USB_IS_AN_I2C_ADAPTER
,
648 .usb_ctrl
= CYPRESS_FX2
,
649 .firmware
= "dvb-usb-pctv-400e-01.fw",
651 .size_of_priv
= sizeof(struct ttusb2_state
),
658 .streaming_ctrl
= NULL
, // ttusb2_streaming_ctrl,
660 .frontend_attach
= ttusb2_frontend_tda10086_attach
,
661 .tuner_attach
= ttusb2_tuner_tda826x_attach
,
663 /* parameter for the MPEG2-data transfer */
680 .power_ctrl
= ttusb2_power_ctrl
,
681 .identify_state
= ttusb2_identify_state
,
683 .i2c_algo
= &ttusb2_i2c_algo
,
685 .generic_bulk_ctrl_endpoint
= 0x01,
687 .num_device_descs
= 2,
689 { "Pinnacle 400e DVB-S USB2.0",
690 { &ttusb2_table
[0], NULL
},
693 { "Pinnacle 450e DVB-S USB2.0",
694 { &ttusb2_table
[1], NULL
},
700 static struct dvb_usb_device_properties ttusb2_properties_s2400
= {
701 .caps
= DVB_USB_IS_AN_I2C_ADAPTER
,
703 .usb_ctrl
= CYPRESS_FX2
,
704 .firmware
= "dvb-usb-tt-s2400-01.fw",
706 .size_of_priv
= sizeof(struct ttusb2_state
),
713 .streaming_ctrl
= NULL
,
715 .frontend_attach
= ttusb2_frontend_tda10086_attach
,
716 .tuner_attach
= ttusb2_tuner_tda826x_attach
,
718 /* parameter for the MPEG2-data transfer */
735 .power_ctrl
= ttusb2_power_ctrl
,
736 .identify_state
= ttusb2_identify_state
,
738 .i2c_algo
= &ttusb2_i2c_algo
,
740 .generic_bulk_ctrl_endpoint
= 0x01,
742 .num_device_descs
= 2,
744 { "Technotrend TT-connect S-2400",
745 { &ttusb2_table
[2], NULL
},
748 { "Technotrend TT-connect S-2400 (8kB EEPROM)",
749 { &ttusb2_table
[4], NULL
},
755 static struct dvb_usb_device_properties ttusb2_properties_ct3650
= {
756 .caps
= DVB_USB_IS_AN_I2C_ADAPTER
,
758 .usb_ctrl
= CYPRESS_FX2
,
760 .size_of_priv
= sizeof(struct ttusb2_state
),
763 .rc_interval
= 150, /* Less than IR_KEYPRESS_TIMEOUT */
764 .rc_codes
= RC_MAP_TT_1500
,
765 .rc_query
= tt3650_rc_query
,
766 .allowed_protos
= RC_PROTO_BIT_RC5
,
774 .streaming_ctrl
= NULL
,
776 .frontend_attach
= ttusb2_frontend_tda10023_attach
,
777 .tuner_attach
= ttusb2_tuner_tda827x_attach
,
779 /* parameter for the MPEG2-data transfer */
793 .streaming_ctrl
= NULL
,
795 .frontend_attach
= ttusb2_frontend_tda10023_attach
,
796 .tuner_attach
= ttusb2_tuner_tda827x_attach
,
798 /* parameter for the MPEG2-data transfer */
815 .power_ctrl
= ttusb2_power_ctrl
,
816 .identify_state
= ttusb2_identify_state
,
818 .i2c_algo
= &ttusb2_i2c_algo
,
820 .generic_bulk_ctrl_endpoint
= 0x01,
822 .num_device_descs
= 1,
824 { "Technotrend TT-connect CT-3650",
825 .warm_ids
= { &ttusb2_table
[3], NULL
},
830 static struct usb_driver ttusb2_driver
= {
831 .name
= "dvb_usb_ttusb2",
832 .probe
= ttusb2_probe
,
833 .disconnect
= ttusb2_usb_disconnect
,
834 .id_table
= ttusb2_table
,
837 module_usb_driver(ttusb2_driver
);
839 MODULE_AUTHOR("Patrick Boettcher <patrick.boettcher@posteo.de>");
840 MODULE_DESCRIPTION("Driver for Pinnacle PCTV 400e DVB-S USB2.0");
841 MODULE_VERSION("1.0");
842 MODULE_LICENSE("GPL");