2 * E3C EC168 DVB USB driver
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
22 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
24 static int ec168_ctrl_msg(struct dvb_usb_device
*d
, struct ec168_req
*req
)
28 u8 request
, requesttype
;
32 case DOWNLOAD_FIRMWARE
:
36 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
40 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
44 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
48 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
52 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
56 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
60 dev_err(&d
->udev
->dev
, "%s: unknown command=%02x\n",
61 KBUILD_MODNAME
, req
->cmd
);
66 buf
= kmalloc(req
->size
, GFP_KERNEL
);
72 if (requesttype
== (USB_TYPE_VENDOR
| USB_DIR_OUT
)) {
74 memcpy(buf
, req
->data
, req
->size
);
75 pipe
= usb_sndctrlpipe(d
->udev
, 0);
78 pipe
= usb_rcvctrlpipe(d
->udev
, 0);
81 msleep(1); /* avoid I2C errors */
83 ret
= usb_control_msg(d
->udev
, pipe
, request
, requesttype
, req
->value
,
84 req
->index
, buf
, req
->size
, EC168_USB_TIMEOUT
);
86 dvb_usb_dbg_usb_control_msg(d
->udev
, request
, requesttype
, req
->value
,
87 req
->index
, buf
, req
->size
);
94 /* read request, copy returned data to return buf */
95 if (!ret
&& requesttype
== (USB_TYPE_VENDOR
| USB_DIR_IN
))
96 memcpy(req
->data
, buf
, req
->size
);
104 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
109 static struct ec100_config ec168_ec100_config
;
111 static int ec168_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
114 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
115 struct ec168_req req
;
122 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
126 if (num
> i
+ 1 && (msg
[i
+1].flags
& I2C_M_RD
)) {
127 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
128 req
.cmd
= READ_DEMOD
;
130 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
131 req
.size
= msg
[i
+1].len
; /* bytes to read */
132 req
.data
= &msg
[i
+1].buf
[0];
133 ret
= ec168_ctrl_msg(d
, &req
);
136 dev_err(&d
->udev
->dev
, "%s: I2C read not " \
143 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
144 req
.cmd
= WRITE_DEMOD
;
145 req
.value
= msg
[i
].buf
[1]; /* val */
146 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
149 ret
= ec168_ctrl_msg(d
, &req
);
153 req
.value
= msg
[i
].buf
[0]; /* val */
154 req
.index
= 0x0100 + msg
[i
].addr
; /* I2C addr */
155 req
.size
= msg
[i
].len
-1;
156 req
.data
= &msg
[i
].buf
[1];
157 ret
= ec168_ctrl_msg(d
, &req
);
168 mutex_unlock(&d
->i2c_mutex
);
172 static u32
ec168_i2c_func(struct i2c_adapter
*adapter
)
177 static struct i2c_algorithm ec168_i2c_algo
= {
178 .master_xfer
= ec168_i2c_xfer
,
179 .functionality
= ec168_i2c_func
,
182 /* Callbacks for DVB USB */
183 static int ec168_identify_state(struct dvb_usb_device
*d
, const char **name
)
187 struct ec168_req req
= {GET_CONFIG
, 0, 1, sizeof(reply
), &reply
};
188 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
190 ret
= ec168_ctrl_msg(d
, &req
);
194 dev_dbg(&d
->udev
->dev
, "%s: reply=%02x\n", __func__
, reply
);
203 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
207 static int ec168_download_firmware(struct dvb_usb_device
*d
,
208 const struct firmware
*fw
)
210 int ret
, len
, remaining
;
211 struct ec168_req req
= {DOWNLOAD_FIRMWARE
, 0, 0, 0, NULL
};
212 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
214 #define LEN_MAX 2048 /* max packet size */
215 for (remaining
= fw
->size
; remaining
> 0; remaining
-= LEN_MAX
) {
221 req
.data
= (u8
*) &fw
->data
[fw
->size
- remaining
];
222 req
.index
= fw
->size
- remaining
;
224 ret
= ec168_ctrl_msg(d
, &req
);
226 dev_err(&d
->udev
->dev
,
227 "%s: firmware download failed=%d\n",
228 KBUILD_MODNAME
, ret
);
236 req
.cmd
= SET_CONFIG
;
239 ret
= ec168_ctrl_msg(d
, &req
);
243 /* really needed - no idea what does */
247 ret
= ec168_ctrl_msg(d
, &req
);
251 /* activate tuner I2C? */
255 ret
= ec168_ctrl_msg(d
, &req
);
261 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
265 static struct ec100_config ec168_ec100_config
= {
266 .demod_address
= 0xff, /* not real address, demod is integrated */
269 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter
*adap
)
271 struct dvb_usb_device
*d
= adap_to_d(adap
);
272 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
274 adap
->fe
[0] = dvb_attach(ec100_attach
, &ec168_ec100_config
,
276 if (adap
->fe
[0] == NULL
)
282 static struct mxl5005s_config ec168_mxl5003s_config
= {
284 .if_freq
= IF_FREQ_4570000HZ
,
285 .xtal_freq
= CRYSTAL_FREQ_16000000HZ
,
286 .agc_mode
= MXL_SINGLE_AGC
,
287 .tracking_filter
= MXL_TF_OFF
,
288 .rssi_enable
= MXL_RSSI_ENABLE
,
289 .cap_select
= MXL_CAP_SEL_ENABLE
,
290 .div_out
= MXL_DIV_OUT_4
,
291 .clock_out
= MXL_CLOCK_OUT_DISABLE
,
292 .output_load
= MXL5005S_IF_OUTPUT_LOAD_200_OHM
,
293 .top
= MXL5005S_TOP_25P2
,
294 .mod_mode
= MXL_DIGITAL_MODE
,
295 .if_mode
= MXL_ZERO_IF
,
296 .AgcMasterByte
= 0x00,
299 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter
*adap
)
301 struct dvb_usb_device
*d
= adap_to_d(adap
);
302 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
304 return dvb_attach(mxl5005s_attach
, adap
->fe
[0], &d
->i2c_adap
,
305 &ec168_mxl5003s_config
) == NULL
? -ENODEV
: 0;
308 static int ec168_streaming_ctrl(struct dvb_frontend
*fe
, int onoff
)
310 struct dvb_usb_device
*d
= fe_to_d(fe
);
311 struct ec168_req req
= {STREAMING_CTRL
, 0x7f01, 0x0202, 0, NULL
};
312 dev_dbg(&d
->udev
->dev
, "%s: onoff=%d\n", __func__
, onoff
);
316 return ec168_ctrl_msg(d
, &req
);
319 /* DVB USB Driver stuff */
320 /* bInterfaceNumber 0 is HID
321 * bInterfaceNumber 1 is DVB-T */
322 static struct dvb_usb_device_properties ec168_props
= {
323 .driver_name
= KBUILD_MODNAME
,
324 .owner
= THIS_MODULE
,
325 .adapter_nr
= adapter_nr
,
326 .bInterfaceNumber
= 1,
328 .identify_state
= ec168_identify_state
,
329 .firmware
= EC168_FIRMWARE
,
330 .download_firmware
= ec168_download_firmware
,
332 .i2c_algo
= &ec168_i2c_algo
,
333 .frontend_attach
= ec168_ec100_frontend_attach
,
334 .tuner_attach
= ec168_mxl5003s_tuner_attach
,
335 .streaming_ctrl
= ec168_streaming_ctrl
,
340 .stream
= DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
345 static const struct dvb_usb_driver_info ec168_driver_info
= {
346 .name
= "E3C EC168 reference design",
347 .props
= &ec168_props
,
350 static const struct usb_device_id ec168_id
[] = {
351 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168
),
352 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
353 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_2
),
354 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
355 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_3
),
356 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
357 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_4
),
358 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
359 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_5
),
360 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
363 MODULE_DEVICE_TABLE(usb
, ec168_id
);
365 static struct usb_driver ec168_driver
= {
366 .name
= KBUILD_MODNAME
,
367 .id_table
= ec168_id
,
368 .probe
= dvb_usbv2_probe
,
369 .disconnect
= dvb_usbv2_disconnect
,
370 .suspend
= dvb_usbv2_suspend
,
371 .resume
= dvb_usbv2_resume
,
376 module_usb_driver(ec168_driver
);
378 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
379 MODULE_DESCRIPTION("E3C EC168 driver");
380 MODULE_LICENSE("GPL");
381 MODULE_FIRMWARE(EC168_FIRMWARE
);