1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * E3C EC168 DVB USB driver
5 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
12 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
14 static int ec168_ctrl_msg(struct dvb_usb_device
*d
, struct ec168_req
*req
)
18 u8 request
, requesttype
;
22 case DOWNLOAD_FIRMWARE
:
26 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
30 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
34 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
38 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
42 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
46 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
50 dev_err(&d
->udev
->dev
, "%s: unknown command=%02x\n",
51 KBUILD_MODNAME
, req
->cmd
);
56 buf
= kmalloc(req
->size
, GFP_KERNEL
);
62 if (requesttype
== (USB_TYPE_VENDOR
| USB_DIR_OUT
)) {
64 memcpy(buf
, req
->data
, req
->size
);
65 pipe
= usb_sndctrlpipe(d
->udev
, 0);
68 pipe
= usb_rcvctrlpipe(d
->udev
, 0);
71 msleep(1); /* avoid I2C errors */
73 ret
= usb_control_msg(d
->udev
, pipe
, request
, requesttype
, req
->value
,
74 req
->index
, buf
, req
->size
, EC168_USB_TIMEOUT
);
76 dvb_usb_dbg_usb_control_msg(d
->udev
, request
, requesttype
, req
->value
,
77 req
->index
, buf
, req
->size
);
84 /* read request, copy returned data to return buf */
85 if (!ret
&& requesttype
== (USB_TYPE_VENDOR
| USB_DIR_IN
))
86 memcpy(req
->data
, buf
, req
->size
);
94 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
99 static struct ec100_config ec168_ec100_config
;
101 static int ec168_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
104 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
105 struct ec168_req req
;
112 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
116 if (num
> i
+ 1 && (msg
[i
+1].flags
& I2C_M_RD
)) {
117 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
118 req
.cmd
= READ_DEMOD
;
120 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
121 req
.size
= msg
[i
+1].len
; /* bytes to read */
122 req
.data
= &msg
[i
+1].buf
[0];
123 ret
= ec168_ctrl_msg(d
, &req
);
126 dev_err(&d
->udev
->dev
, "%s: I2C read not " \
133 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
134 req
.cmd
= WRITE_DEMOD
;
135 req
.value
= msg
[i
].buf
[1]; /* val */
136 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
139 ret
= ec168_ctrl_msg(d
, &req
);
143 req
.value
= msg
[i
].buf
[0]; /* val */
144 req
.index
= 0x0100 + msg
[i
].addr
; /* I2C addr */
145 req
.size
= msg
[i
].len
-1;
146 req
.data
= &msg
[i
].buf
[1];
147 ret
= ec168_ctrl_msg(d
, &req
);
158 mutex_unlock(&d
->i2c_mutex
);
162 static u32
ec168_i2c_func(struct i2c_adapter
*adapter
)
167 static struct i2c_algorithm ec168_i2c_algo
= {
168 .master_xfer
= ec168_i2c_xfer
,
169 .functionality
= ec168_i2c_func
,
172 /* Callbacks for DVB USB */
173 static int ec168_identify_state(struct dvb_usb_device
*d
, const char **name
)
177 struct ec168_req req
= {GET_CONFIG
, 0, 1, sizeof(reply
), &reply
};
178 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
180 ret
= ec168_ctrl_msg(d
, &req
);
184 dev_dbg(&d
->udev
->dev
, "%s: reply=%02x\n", __func__
, reply
);
193 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
197 static int ec168_download_firmware(struct dvb_usb_device
*d
,
198 const struct firmware
*fw
)
200 int ret
, len
, remaining
;
201 struct ec168_req req
= {DOWNLOAD_FIRMWARE
, 0, 0, 0, NULL
};
202 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
204 #define LEN_MAX 2048 /* max packet size */
205 for (remaining
= fw
->size
; remaining
> 0; remaining
-= LEN_MAX
) {
211 req
.data
= (u8
*) &fw
->data
[fw
->size
- remaining
];
212 req
.index
= fw
->size
- remaining
;
214 ret
= ec168_ctrl_msg(d
, &req
);
216 dev_err(&d
->udev
->dev
,
217 "%s: firmware download failed=%d\n",
218 KBUILD_MODNAME
, ret
);
226 req
.cmd
= SET_CONFIG
;
229 ret
= ec168_ctrl_msg(d
, &req
);
233 /* really needed - no idea what does */
237 ret
= ec168_ctrl_msg(d
, &req
);
241 /* activate tuner I2C? */
245 ret
= ec168_ctrl_msg(d
, &req
);
251 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
255 static struct ec100_config ec168_ec100_config
= {
256 .demod_address
= 0xff, /* not real address, demod is integrated */
259 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter
*adap
)
261 struct dvb_usb_device
*d
= adap_to_d(adap
);
262 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
264 adap
->fe
[0] = dvb_attach(ec100_attach
, &ec168_ec100_config
,
266 if (adap
->fe
[0] == NULL
)
272 static struct mxl5005s_config ec168_mxl5003s_config
= {
274 .if_freq
= IF_FREQ_4570000HZ
,
275 .xtal_freq
= CRYSTAL_FREQ_16000000HZ
,
276 .agc_mode
= MXL_SINGLE_AGC
,
277 .tracking_filter
= MXL_TF_OFF
,
278 .rssi_enable
= MXL_RSSI_ENABLE
,
279 .cap_select
= MXL_CAP_SEL_ENABLE
,
280 .div_out
= MXL_DIV_OUT_4
,
281 .clock_out
= MXL_CLOCK_OUT_DISABLE
,
282 .output_load
= MXL5005S_IF_OUTPUT_LOAD_200_OHM
,
283 .top
= MXL5005S_TOP_25P2
,
284 .mod_mode
= MXL_DIGITAL_MODE
,
285 .if_mode
= MXL_ZERO_IF
,
286 .AgcMasterByte
= 0x00,
289 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter
*adap
)
291 struct dvb_usb_device
*d
= adap_to_d(adap
);
292 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
294 return dvb_attach(mxl5005s_attach
, adap
->fe
[0], &d
->i2c_adap
,
295 &ec168_mxl5003s_config
) == NULL
? -ENODEV
: 0;
298 static int ec168_streaming_ctrl(struct dvb_frontend
*fe
, int onoff
)
300 struct dvb_usb_device
*d
= fe_to_d(fe
);
301 struct ec168_req req
= {STREAMING_CTRL
, 0x7f01, 0x0202, 0, NULL
};
302 dev_dbg(&d
->udev
->dev
, "%s: onoff=%d\n", __func__
, onoff
);
306 return ec168_ctrl_msg(d
, &req
);
309 /* DVB USB Driver stuff */
310 /* bInterfaceNumber 0 is HID
311 * bInterfaceNumber 1 is DVB-T */
312 static const struct dvb_usb_device_properties ec168_props
= {
313 .driver_name
= KBUILD_MODNAME
,
314 .owner
= THIS_MODULE
,
315 .adapter_nr
= adapter_nr
,
316 .bInterfaceNumber
= 1,
318 .identify_state
= ec168_identify_state
,
319 .firmware
= EC168_FIRMWARE
,
320 .download_firmware
= ec168_download_firmware
,
322 .i2c_algo
= &ec168_i2c_algo
,
323 .frontend_attach
= ec168_ec100_frontend_attach
,
324 .tuner_attach
= ec168_mxl5003s_tuner_attach
,
325 .streaming_ctrl
= ec168_streaming_ctrl
,
330 .stream
= DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
335 static const struct usb_device_id ec168_id
[] = {
336 { DVB_USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168
,
337 &ec168_props
, "E3C EC168 reference design", NULL
)},
338 { DVB_USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_2
,
339 &ec168_props
, "E3C EC168 reference design", NULL
)},
340 { DVB_USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_3
,
341 &ec168_props
, "E3C EC168 reference design", NULL
)},
342 { DVB_USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_4
,
343 &ec168_props
, "E3C EC168 reference design", NULL
)},
344 { DVB_USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_5
,
345 &ec168_props
, "E3C EC168 reference design", NULL
)},
348 MODULE_DEVICE_TABLE(usb
, ec168_id
);
350 static struct usb_driver ec168_driver
= {
351 .name
= KBUILD_MODNAME
,
352 .id_table
= ec168_id
,
353 .probe
= dvb_usbv2_probe
,
354 .disconnect
= dvb_usbv2_disconnect
,
355 .suspend
= dvb_usbv2_suspend
,
356 .resume
= dvb_usbv2_resume
,
361 module_usb_driver(ec168_driver
);
363 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
364 MODULE_DESCRIPTION("E3C EC168 driver");
365 MODULE_LICENSE("GPL");
366 MODULE_FIRMWARE(EC168_FIRMWARE
);