ec168.c 9.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/*
 * E3C EC168 DVB USB driver
 *
 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "ec168.h"
#include "ec100.h"
#include "mxl5005s.h"

DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);

28
static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
29 30 31 32
{
	int ret;
	unsigned int pipe;
	u8 request, requesttype;
33 34
	u8 *buf;

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
	switch (req->cmd) {
	case DOWNLOAD_FIRMWARE:
	case GPIO:
	case WRITE_I2C:
	case STREAMING_CTRL:
		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
		request = req->cmd;
		break;
	case READ_I2C:
		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
		request = req->cmd;
		break;
	case GET_CONFIG:
		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
		request = CONFIG;
		break;
	case SET_CONFIG:
		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
		request = CONFIG;
		break;
	case WRITE_DEMOD:
		requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
		request = DEMOD_RW;
		break;
	case READ_DEMOD:
		requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
		request = DEMOD_RW;
		break;
	default:
64 65
		pr_err("%s: unknown command=%02x\n", KBUILD_MODNAME, req->cmd);
		ret = -EINVAL;
66 67 68
		goto error;
	}

69 70 71 72 73 74
	buf = kmalloc(req->size, GFP_KERNEL);
	if (!buf) {
		ret = -ENOMEM;
		goto error;
	}

75 76 77
	if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
		/* write */
		memcpy(buf, req->data, req->size);
78
		pipe = usb_sndctrlpipe(d->udev, 0);
79 80
	} else {
		/* read */
81
		pipe = usb_rcvctrlpipe(d->udev, 0);
82 83 84 85
	}

	msleep(1); /* avoid I2C errors */

86
	ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
87
		req->index, buf, req->size, EC168_USB_TIMEOUT);
88 89

	ec168_debug_dump(request, requesttype, req->value, req->index, buf,
90
		req->size);
91 92

	if (ret < 0)
93
		goto err_dealloc;
94 95 96 97 98 99 100
	else
		ret = 0;

	/* read request, copy returned data to return buf */
	if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
		memcpy(req->data, buf, req->size);

101
	kfree(buf);
102
	return ret;
103 104 105

err_dealloc:
	kfree(buf);
106
error:
107
	pr_debug("%s: failed=%d\n", __func__, ret);
108 109 110 111
	return ret;
}

/* I2C */
112 113
static struct ec100_config ec168_ec100_config;

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
	int num)
{
	struct dvb_usb_device *d = i2c_get_adapdata(adap);
	struct ec168_req req;
	int i = 0;
	int ret;

	if (num > 2)
		return -EINVAL;

	if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
		return -EAGAIN;

	while (i < num) {
		if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
			if (msg[i].addr == ec168_ec100_config.demod_address) {
				req.cmd = READ_DEMOD;
				req.value = 0;
				req.index = 0xff00 + msg[i].buf[0]; /* reg */
				req.size = msg[i+1].len; /* bytes to read */
				req.data = &msg[i+1].buf[0];
				ret = ec168_ctrl_msg(d, &req);
				i += 2;
			} else {
139 140 141
				pr_err("%s: I2C read not implemented\n",
						KBUILD_MODNAME);
				ret = -EOPNOTSUPP;
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
				i += 2;
			}
		} else {
			if (msg[i].addr == ec168_ec100_config.demod_address) {
				req.cmd = WRITE_DEMOD;
				req.value = msg[i].buf[1]; /* val */
				req.index = 0xff00 + msg[i].buf[0]; /* reg */
				req.size = 0;
				req.data = NULL;
				ret = ec168_ctrl_msg(d, &req);
				i += 1;
			} else {
				req.cmd = WRITE_I2C;
				req.value = msg[i].buf[0]; /* val */
				req.index = 0x0100 + msg[i].addr; /* I2C addr */
				req.size = msg[i].len-1;
				req.data = &msg[i].buf[1];
				ret = ec168_ctrl_msg(d, &req);
				i += 1;
			}
		}
		if (ret)
			goto error;

	}
	ret = i;

error:
	mutex_unlock(&d->i2c_mutex);
	return i;
}

static u32 ec168_i2c_func(struct i2c_adapter *adapter)
{
	return I2C_FUNC_I2C;
}

static struct i2c_algorithm ec168_i2c_algo = {
	.master_xfer   = ec168_i2c_xfer,
	.functionality = ec168_i2c_func,
};

/* Callbacks for DVB USB */
185
static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
186
{
187 188 189
	int ret;
	u8 reply;
	struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
190
	pr_debug("%s:\n", __func__);
191

192 193 194
	ret = ec168_ctrl_msg(d, &req);
	if (ret)
		goto error;
195

196
	pr_debug("%s: reply=%02x\n", __func__, reply);
197

198 199 200 201
	if (reply == 0x01)
		ret = WARM;
	else
		ret = COLD;
202

203 204
	return ret;
error:
205
	pr_debug("%s: failed=%d\n", __func__, ret);
206
	return ret;
207 208
}

209 210
static int ec168_download_firmware(struct dvb_usb_device *d,
		const struct firmware *fw)
211
{
212
	int ret, len, remaining;
213
	struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
214
	pr_debug("%s:\n", __func__);
215

216 217 218 219 220
	#define LEN_MAX 2048 /* max packet size */
	for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
		len = remaining;
		if (len > LEN_MAX)
			len = LEN_MAX;
221 222

		req.size = len;
223 224
		req.data = (u8 *) &fw->data[fw->size - remaining];
		req.index = fw->size - remaining;
225

226
		ret = ec168_ctrl_msg(d, &req);
227
		if (ret) {
228 229
			pr_err("%s: firmware download failed=%d\n",
					KBUILD_MODNAME, ret);
230 231 232
			goto error;
		}
	}
233

234 235 236 237 238 239
	req.size = 0;

	/* set "warm"? */
	req.cmd = SET_CONFIG;
	req.value = 0;
	req.index = 0x0001;
240
	ret = ec168_ctrl_msg(d, &req);
241 242 243 244 245 246 247
	if (ret)
		goto error;

	/* really needed - no idea what does */
	req.cmd = GPIO;
	req.value = 0;
	req.index = 0x0206;
248
	ret = ec168_ctrl_msg(d, &req);
249 250 251 252 253 254 255
	if (ret)
		goto error;

	/* activate tuner I2C? */
	req.cmd = WRITE_I2C;
	req.value = 0;
	req.index = 0x00c6;
256
	ret = ec168_ctrl_msg(d, &req);
257 258 259 260 261
	if (ret)
		goto error;

	return ret;
error:
262
	pr_debug("%s: failed=%d\n", __func__, ret);
263 264 265
	return ret;
}

266 267 268 269 270
static struct ec100_config ec168_ec100_config = {
	.demod_address = 0xff, /* not real address, demod is integrated */
};

static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
271
{
272
	pr_debug("%s:\n", __func__);
273
	adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
274
			&adap_to_d(adap)->i2c_adap);
275 276
	if (adap->fe[0] == NULL)
		return -ENODEV;
277

278
	return 0;
279 280
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
static struct mxl5005s_config ec168_mxl5003s_config = {
	.i2c_address     = 0xc6,
	.if_freq         = IF_FREQ_4570000HZ,
	.xtal_freq       = CRYSTAL_FREQ_16000000HZ,
	.agc_mode        = MXL_SINGLE_AGC,
	.tracking_filter = MXL_TF_OFF,
	.rssi_enable     = MXL_RSSI_ENABLE,
	.cap_select      = MXL_CAP_SEL_ENABLE,
	.div_out         = MXL_DIV_OUT_4,
	.clock_out       = MXL_CLOCK_OUT_DISABLE,
	.output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
	.top		 = MXL5005S_TOP_25P2,
	.mod_mode        = MXL_DIGITAL_MODE,
	.if_mode         = MXL_ZERO_IF,
	.AgcMasterByte   = 0x00,
};
297

298
static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
299
{
300
	pr_debug("%s:\n", __func__);
301
	return dvb_attach(mxl5005s_attach, adap->fe[0],
302
			&adap_to_d(adap)->i2c_adap,
303
			&ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
304 305
}

306 307 308
static int ec168_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
{
	struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
309
	pr_debug("%s: onoff=%d\n", __func__, onoff);
310 311
	if (onoff)
		req.index = 0x0102;
312
	return ec168_ctrl_msg(adap_to_d(adap), &req);
313
}
314

315 316 317 318 319 320 321 322
/* DVB USB Driver stuff */
/* bInterfaceNumber 0 is HID
 * bInterfaceNumber 1 is DVB-T */
static struct dvb_usb_device_properties ec168_props = {
	.driver_name = KBUILD_MODNAME,
	.owner = THIS_MODULE,
	.adapter_nr = adapter_nr,
	.bInterfaceNumber = 1,
323

324
	.identify_state = ec168_identify_state,
325
	.firmware = "dvb-usb-ec168.fw",
326
	.download_firmware = ec168_download_firmware,
327

328 329 330 331
	.i2c_algo = &ec168_i2c_algo,
	.frontend_attach = ec168_ec100_frontend_attach,
	.tuner_attach = ec168_mxl5003s_tuner_attach,
	.streaming_ctrl = ec168_streaming_ctrl,
332 333 334 335

	.num_adapters = 1,
	.adapter = {
		{
336
			.stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
337 338
		}
	},
339
};
340

341 342 343 344
static const struct dvb_usb_driver_info ec168_driver_info = {
	.name = "E3C EC168 reference design",
	.props = &ec168_props,
};
345

346 347 348 349 350 351 352 353 354 355 356 357
static const struct usb_device_id ec168_id[] = {
	{ USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
		.driver_info = (kernel_ulong_t) &ec168_driver_info },
	{ USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
		.driver_info = (kernel_ulong_t) &ec168_driver_info },
	{ USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
		.driver_info = (kernel_ulong_t) &ec168_driver_info },
	{ USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
		.driver_info = (kernel_ulong_t) &ec168_driver_info },
	{ USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
		.driver_info = (kernel_ulong_t) &ec168_driver_info },
	{}
358
};
359
MODULE_DEVICE_TABLE(usb, ec168_id);
360 361

static struct usb_driver ec168_driver = {
362 363 364 365 366 367 368 369
	.name = KBUILD_MODNAME,
	.id_table = ec168_id,
	.probe = dvb_usbv2_probe,
	.disconnect = dvb_usbv2_disconnect,
	.suspend = dvb_usbv2_suspend,
	.resume = dvb_usbv2_resume,
	.no_dynamic_id = 1,
	.soft_unbind = 1,
370 371
};

372
module_usb_driver(ec168_driver);
373 374

MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
375
MODULE_DESCRIPTION("E3C EC168 driver");
376
MODULE_LICENSE("GPL");