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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr
);
28 static int ec168_ctrl_msg(struct dvb_usb_device
*d
, struct ec168_req
*req
)
32 u8 request
, requesttype
;
36 case DOWNLOAD_FIRMWARE
:
40 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
44 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
48 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
52 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
56 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_OUT
);
60 requesttype
= (USB_TYPE_VENDOR
| USB_DIR_IN
);
64 dev_err(&d
->udev
->dev
, "%s: unknown command=%02x\n",
65 KBUILD_MODNAME
, req
->cmd
);
70 buf
= kmalloc(req
->size
, GFP_KERNEL
);
76 if (requesttype
== (USB_TYPE_VENDOR
| USB_DIR_OUT
)) {
78 memcpy(buf
, req
->data
, req
->size
);
79 pipe
= usb_sndctrlpipe(d
->udev
, 0);
82 pipe
= usb_rcvctrlpipe(d
->udev
, 0);
85 msleep(1); /* avoid I2C errors */
87 ret
= usb_control_msg(d
->udev
, pipe
, request
, requesttype
, req
->value
,
88 req
->index
, buf
, req
->size
, EC168_USB_TIMEOUT
);
90 dvb_usb_dbg_usb_control_msg(d
->udev
, request
, requesttype
, req
->value
,
91 req
->index
, buf
, req
->size
);
98 /* read request, copy returned data to return buf */
99 if (!ret
&& requesttype
== (USB_TYPE_VENDOR
| USB_DIR_IN
))
100 memcpy(req
->data
, buf
, req
->size
);
108 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
113 static struct ec100_config ec168_ec100_config
;
115 static int ec168_i2c_xfer(struct i2c_adapter
*adap
, struct i2c_msg msg
[],
118 struct dvb_usb_device
*d
= i2c_get_adapdata(adap
);
119 struct ec168_req req
;
126 if (mutex_lock_interruptible(&d
->i2c_mutex
) < 0)
130 if (num
> i
+ 1 && (msg
[i
+1].flags
& I2C_M_RD
)) {
131 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
132 req
.cmd
= READ_DEMOD
;
134 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
135 req
.size
= msg
[i
+1].len
; /* bytes to read */
136 req
.data
= &msg
[i
+1].buf
[0];
137 ret
= ec168_ctrl_msg(d
, &req
);
140 dev_err(&d
->udev
->dev
, "%s: I2C read not " \
147 if (msg
[i
].addr
== ec168_ec100_config
.demod_address
) {
148 req
.cmd
= WRITE_DEMOD
;
149 req
.value
= msg
[i
].buf
[1]; /* val */
150 req
.index
= 0xff00 + msg
[i
].buf
[0]; /* reg */
153 ret
= ec168_ctrl_msg(d
, &req
);
157 req
.value
= msg
[i
].buf
[0]; /* val */
158 req
.index
= 0x0100 + msg
[i
].addr
; /* I2C addr */
159 req
.size
= msg
[i
].len
-1;
160 req
.data
= &msg
[i
].buf
[1];
161 ret
= ec168_ctrl_msg(d
, &req
);
172 mutex_unlock(&d
->i2c_mutex
);
176 static u32
ec168_i2c_func(struct i2c_adapter
*adapter
)
181 static struct i2c_algorithm ec168_i2c_algo
= {
182 .master_xfer
= ec168_i2c_xfer
,
183 .functionality
= ec168_i2c_func
,
186 /* Callbacks for DVB USB */
187 static int ec168_identify_state(struct dvb_usb_device
*d
, const char **name
)
191 struct ec168_req req
= {GET_CONFIG
, 0, 1, sizeof(reply
), &reply
};
192 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
194 ret
= ec168_ctrl_msg(d
, &req
);
198 dev_dbg(&d
->udev
->dev
, "%s: reply=%02x\n", __func__
, reply
);
207 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
211 static int ec168_download_firmware(struct dvb_usb_device
*d
,
212 const struct firmware
*fw
)
214 int ret
, len
, remaining
;
215 struct ec168_req req
= {DOWNLOAD_FIRMWARE
, 0, 0, 0, NULL
};
216 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
218 #define LEN_MAX 2048 /* max packet size */
219 for (remaining
= fw
->size
; remaining
> 0; remaining
-= LEN_MAX
) {
225 req
.data
= (u8
*) &fw
->data
[fw
->size
- remaining
];
226 req
.index
= fw
->size
- remaining
;
228 ret
= ec168_ctrl_msg(d
, &req
);
230 dev_err(&d
->udev
->dev
,
231 "%s: firmware download failed=%d\n",
232 KBUILD_MODNAME
, ret
);
240 req
.cmd
= SET_CONFIG
;
243 ret
= ec168_ctrl_msg(d
, &req
);
247 /* really needed - no idea what does */
251 ret
= ec168_ctrl_msg(d
, &req
);
255 /* activate tuner I2C? */
259 ret
= ec168_ctrl_msg(d
, &req
);
265 dev_dbg(&d
->udev
->dev
, "%s: failed=%d\n", __func__
, ret
);
269 static struct ec100_config ec168_ec100_config
= {
270 .demod_address
= 0xff, /* not real address, demod is integrated */
273 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter
*adap
)
275 struct dvb_usb_device
*d
= adap_to_d(adap
);
276 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
278 adap
->fe
[0] = dvb_attach(ec100_attach
, &ec168_ec100_config
,
280 if (adap
->fe
[0] == NULL
)
286 static struct mxl5005s_config ec168_mxl5003s_config
= {
288 .if_freq
= IF_FREQ_4570000HZ
,
289 .xtal_freq
= CRYSTAL_FREQ_16000000HZ
,
290 .agc_mode
= MXL_SINGLE_AGC
,
291 .tracking_filter
= MXL_TF_OFF
,
292 .rssi_enable
= MXL_RSSI_ENABLE
,
293 .cap_select
= MXL_CAP_SEL_ENABLE
,
294 .div_out
= MXL_DIV_OUT_4
,
295 .clock_out
= MXL_CLOCK_OUT_DISABLE
,
296 .output_load
= MXL5005S_IF_OUTPUT_LOAD_200_OHM
,
297 .top
= MXL5005S_TOP_25P2
,
298 .mod_mode
= MXL_DIGITAL_MODE
,
299 .if_mode
= MXL_ZERO_IF
,
300 .AgcMasterByte
= 0x00,
303 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter
*adap
)
305 struct dvb_usb_device
*d
= adap_to_d(adap
);
306 dev_dbg(&d
->udev
->dev
, "%s:\n", __func__
);
308 return dvb_attach(mxl5005s_attach
, adap
->fe
[0], &d
->i2c_adap
,
309 &ec168_mxl5003s_config
) == NULL
? -ENODEV
: 0;
312 static int ec168_streaming_ctrl(struct dvb_frontend
*fe
, int onoff
)
314 struct dvb_usb_device
*d
= fe_to_d(fe
);
315 struct ec168_req req
= {STREAMING_CTRL
, 0x7f01, 0x0202, 0, NULL
};
316 dev_dbg(&d
->udev
->dev
, "%s: onoff=%d\n", __func__
, onoff
);
320 return ec168_ctrl_msg(d
, &req
);
323 /* DVB USB Driver stuff */
324 /* bInterfaceNumber 0 is HID
325 * bInterfaceNumber 1 is DVB-T */
326 static struct dvb_usb_device_properties ec168_props
= {
327 .driver_name
= KBUILD_MODNAME
,
328 .owner
= THIS_MODULE
,
329 .adapter_nr
= adapter_nr
,
330 .bInterfaceNumber
= 1,
332 .identify_state
= ec168_identify_state
,
333 .firmware
= EC168_FIRMWARE
,
334 .download_firmware
= ec168_download_firmware
,
336 .i2c_algo
= &ec168_i2c_algo
,
337 .frontend_attach
= ec168_ec100_frontend_attach
,
338 .tuner_attach
= ec168_mxl5003s_tuner_attach
,
339 .streaming_ctrl
= ec168_streaming_ctrl
,
344 .stream
= DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
349 static const struct dvb_usb_driver_info ec168_driver_info
= {
350 .name
= "E3C EC168 reference design",
351 .props
= &ec168_props
,
354 static const struct usb_device_id ec168_id
[] = {
355 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168
),
356 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
357 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_2
),
358 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
359 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_3
),
360 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
361 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_4
),
362 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
363 { USB_DEVICE(USB_VID_E3C
, USB_PID_E3C_EC168_5
),
364 .driver_info
= (kernel_ulong_t
) &ec168_driver_info
},
367 MODULE_DEVICE_TABLE(usb
, ec168_id
);
369 static struct usb_driver ec168_driver
= {
370 .name
= KBUILD_MODNAME
,
371 .id_table
= ec168_id
,
372 .probe
= dvb_usbv2_probe
,
373 .disconnect
= dvb_usbv2_disconnect
,
374 .suspend
= dvb_usbv2_suspend
,
375 .resume
= dvb_usbv2_resume
,
380 module_usb_driver(ec168_driver
);
382 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
383 MODULE_DESCRIPTION("E3C EC168 driver");
384 MODULE_LICENSE("GPL");
385 MODULE_FIRMWARE(EC168_FIRMWARE
);