1 /*
2  * E3C EC168 DVB USB driver
3  *
4  * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5  *
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.
10  *
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.
15  *
16  */
17 
18 #include "ec168.h"
19 #include "ec100.h"
20 #include "mxl5005s.h"
21 
22 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
23 
24 static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
25 {
26 	int ret;
27 	unsigned int pipe;
28 	u8 request, requesttype;
29 	u8 *buf;
30 
31 	switch (req->cmd) {
32 	case DOWNLOAD_FIRMWARE:
33 	case GPIO:
34 	case WRITE_I2C:
35 	case STREAMING_CTRL:
36 		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
37 		request = req->cmd;
38 		break;
39 	case READ_I2C:
40 		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
41 		request = req->cmd;
42 		break;
43 	case GET_CONFIG:
44 		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
45 		request = CONFIG;
46 		break;
47 	case SET_CONFIG:
48 		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
49 		request = CONFIG;
50 		break;
51 	case WRITE_DEMOD:
52 		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
53 		request = DEMOD_RW;
54 		break;
55 	case READ_DEMOD:
56 		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
57 		request = DEMOD_RW;
58 		break;
59 	default:
60 		dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
61 				KBUILD_MODNAME, req->cmd);
62 		ret = -EINVAL;
63 		goto error;
64 	}
65 
66 	buf = kmalloc(req->size, GFP_KERNEL);
67 	if (!buf) {
68 		ret = -ENOMEM;
69 		goto error;
70 	}
71 
72 	if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
73 		/* write */
74 		memcpy(buf, req->data, req->size);
75 		pipe = usb_sndctrlpipe(d->udev, 0);
76 	} else {
77 		/* read */
78 		pipe = usb_rcvctrlpipe(d->udev, 0);
79 	}
80 
81 	msleep(1); /* avoid I2C errors */
82 
83 	ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
84 		req->index, buf, req->size, EC168_USB_TIMEOUT);
85 
86 	dvb_usb_dbg_usb_control_msg(d->udev, request, requesttype, req->value,
87 			req->index, buf, req->size);
88 
89 	if (ret < 0)
90 		goto err_dealloc;
91 	else
92 		ret = 0;
93 
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);
97 
98 	kfree(buf);
99 	return ret;
100 
101 err_dealloc:
102 	kfree(buf);
103 error:
104 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
105 	return ret;
106 }
107 
108 /* I2C */
109 static struct ec100_config ec168_ec100_config;
110 
111 static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
112 	int num)
113 {
114 	struct dvb_usb_device *d = i2c_get_adapdata(adap);
115 	struct ec168_req req;
116 	int i = 0;
117 	int ret;
118 
119 	if (num > 2)
120 		return -EINVAL;
121 
122 	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
123 		return -EAGAIN;
124 
125 	while (i < num) {
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;
129 				req.value = 0;
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);
134 				i += 2;
135 			} else {
136 				dev_err(&d->udev->dev, "%s: I2C read not " \
137 						"implemented\n",
138 						KBUILD_MODNAME);
139 				ret = -EOPNOTSUPP;
140 				i += 2;
141 			}
142 		} else {
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 */
147 				req.size = 0;
148 				req.data = NULL;
149 				ret = ec168_ctrl_msg(d, &req);
150 				i += 1;
151 			} else {
152 				req.cmd = WRITE_I2C;
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);
158 				i += 1;
159 			}
160 		}
161 		if (ret)
162 			goto error;
163 
164 	}
165 	ret = i;
166 
167 error:
168 	mutex_unlock(&d->i2c_mutex);
169 	return ret;
170 }
171 
172 static u32 ec168_i2c_func(struct i2c_adapter *adapter)
173 {
174 	return I2C_FUNC_I2C;
175 }
176 
177 static struct i2c_algorithm ec168_i2c_algo = {
178 	.master_xfer   = ec168_i2c_xfer,
179 	.functionality = ec168_i2c_func,
180 };
181 
182 /* Callbacks for DVB USB */
183 static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
184 {
185 	int ret;
186 	u8 reply;
187 	struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
188 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
189 
190 	ret = ec168_ctrl_msg(d, &req);
191 	if (ret)
192 		goto error;
193 
194 	dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
195 
196 	if (reply == 0x01)
197 		ret = WARM;
198 	else
199 		ret = COLD;
200 
201 	return ret;
202 error:
203 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
204 	return ret;
205 }
206 
207 static int ec168_download_firmware(struct dvb_usb_device *d,
208 		const struct firmware *fw)
209 {
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__);
213 
214 	#define LEN_MAX 2048 /* max packet size */
215 	for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
216 		len = remaining;
217 		if (len > LEN_MAX)
218 			len = LEN_MAX;
219 
220 		req.size = len;
221 		req.data = (u8 *) &fw->data[fw->size - remaining];
222 		req.index = fw->size - remaining;
223 
224 		ret = ec168_ctrl_msg(d, &req);
225 		if (ret) {
226 			dev_err(&d->udev->dev,
227 					"%s: firmware download failed=%d\n",
228 					KBUILD_MODNAME, ret);
229 			goto error;
230 		}
231 	}
232 
233 	req.size = 0;
234 
235 	/* set "warm"? */
236 	req.cmd = SET_CONFIG;
237 	req.value = 0;
238 	req.index = 0x0001;
239 	ret = ec168_ctrl_msg(d, &req);
240 	if (ret)
241 		goto error;
242 
243 	/* really needed - no idea what does */
244 	req.cmd = GPIO;
245 	req.value = 0;
246 	req.index = 0x0206;
247 	ret = ec168_ctrl_msg(d, &req);
248 	if (ret)
249 		goto error;
250 
251 	/* activate tuner I2C? */
252 	req.cmd = WRITE_I2C;
253 	req.value = 0;
254 	req.index = 0x00c6;
255 	ret = ec168_ctrl_msg(d, &req);
256 	if (ret)
257 		goto error;
258 
259 	return ret;
260 error:
261 	dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
262 	return ret;
263 }
264 
265 static struct ec100_config ec168_ec100_config = {
266 	.demod_address = 0xff, /* not real address, demod is integrated */
267 };
268 
269 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
270 {
271 	struct dvb_usb_device *d = adap_to_d(adap);
272 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
273 
274 	adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
275 			&d->i2c_adap);
276 	if (adap->fe[0] == NULL)
277 		return -ENODEV;
278 
279 	return 0;
280 }
281 
282 static struct mxl5005s_config ec168_mxl5003s_config = {
283 	.i2c_address     = 0xc6,
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,
297 };
298 
299 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
300 {
301 	struct dvb_usb_device *d = adap_to_d(adap);
302 	dev_dbg(&d->udev->dev, "%s:\n", __func__);
303 
304 	return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
305 			&ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
306 }
307 
308 static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
309 {
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);
313 
314 	if (onoff)
315 		req.index = 0x0102;
316 	return ec168_ctrl_msg(d, &req);
317 }
318 
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,
327 
328 	.identify_state = ec168_identify_state,
329 	.firmware = EC168_FIRMWARE,
330 	.download_firmware = ec168_download_firmware,
331 
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,
336 
337 	.num_adapters = 1,
338 	.adapter = {
339 		{
340 			.stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
341 		}
342 	},
343 };
344 
345 static const struct dvb_usb_driver_info ec168_driver_info = {
346 	.name = "E3C EC168 reference design",
347 	.props = &ec168_props,
348 };
349 
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 },
361 	{}
362 };
363 MODULE_DEVICE_TABLE(usb, ec168_id);
364 
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,
372 	.no_dynamic_id = 1,
373 	.soft_unbind = 1,
374 };
375 
376 module_usb_driver(ec168_driver);
377 
378 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
379 MODULE_DESCRIPTION("E3C EC168 driver");
380 MODULE_LICENSE("GPL");
381 MODULE_FIRMWARE(EC168_FIRMWARE);
382