Linux 3.11-rc3
[cris-mirror.git] / drivers / media / usb / dvb-usb-v2 / ec168.c
blob5c68f3918bc81279717131e15a5084e25e10d1d8
1 /*
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.
22 #include "ec168.h"
23 #include "ec100.h"
24 #include "mxl5005s.h"
26 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
28 static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
30 int ret;
31 unsigned int pipe;
32 u8 request, requesttype;
33 u8 *buf;
35 switch (req->cmd) {
36 case DOWNLOAD_FIRMWARE:
37 case GPIO:
38 case WRITE_I2C:
39 case STREAMING_CTRL:
40 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
41 request = req->cmd;
42 break;
43 case READ_I2C:
44 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
45 request = req->cmd;
46 break;
47 case GET_CONFIG:
48 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
49 request = CONFIG;
50 break;
51 case SET_CONFIG:
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
53 request = CONFIG;
54 break;
55 case WRITE_DEMOD:
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
57 request = DEMOD_RW;
58 break;
59 case READ_DEMOD:
60 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
61 request = DEMOD_RW;
62 break;
63 default:
64 dev_err(&d->udev->dev, "%s: unknown command=%02x\n",
65 KBUILD_MODNAME, req->cmd);
66 ret = -EINVAL;
67 goto error;
70 buf = kmalloc(req->size, GFP_KERNEL);
71 if (!buf) {
72 ret = -ENOMEM;
73 goto error;
76 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
77 /* write */
78 memcpy(buf, req->data, req->size);
79 pipe = usb_sndctrlpipe(d->udev, 0);
80 } else {
81 /* read */
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);
93 if (ret < 0)
94 goto err_dealloc;
95 else
96 ret = 0;
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);
102 kfree(buf);
103 return ret;
105 err_dealloc:
106 kfree(buf);
107 error:
108 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
109 return ret;
112 /* I2C */
113 static struct ec100_config ec168_ec100_config;
115 static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
116 int num)
118 struct dvb_usb_device *d = i2c_get_adapdata(adap);
119 struct ec168_req req;
120 int i = 0;
121 int ret;
123 if (num > 2)
124 return -EINVAL;
126 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
127 return -EAGAIN;
129 while (i < num) {
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;
133 req.value = 0;
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);
138 i += 2;
139 } else {
140 dev_err(&d->udev->dev, "%s: I2C read not " \
141 "implemented\n",
142 KBUILD_MODNAME);
143 ret = -EOPNOTSUPP;
144 i += 2;
146 } else {
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 */
151 req.size = 0;
152 req.data = NULL;
153 ret = ec168_ctrl_msg(d, &req);
154 i += 1;
155 } else {
156 req.cmd = WRITE_I2C;
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);
162 i += 1;
165 if (ret)
166 goto error;
169 ret = i;
171 error:
172 mutex_unlock(&d->i2c_mutex);
173 return i;
176 static u32 ec168_i2c_func(struct i2c_adapter *adapter)
178 return I2C_FUNC_I2C;
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)
189 int ret;
190 u8 reply;
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);
195 if (ret)
196 goto error;
198 dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
200 if (reply == 0x01)
201 ret = WARM;
202 else
203 ret = COLD;
205 return ret;
206 error:
207 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
208 return 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) {
220 len = remaining;
221 if (len > LEN_MAX)
222 len = LEN_MAX;
224 req.size = len;
225 req.data = (u8 *) &fw->data[fw->size - remaining];
226 req.index = fw->size - remaining;
228 ret = ec168_ctrl_msg(d, &req);
229 if (ret) {
230 dev_err(&d->udev->dev,
231 "%s: firmware download failed=%d\n",
232 KBUILD_MODNAME, ret);
233 goto error;
237 req.size = 0;
239 /* set "warm"? */
240 req.cmd = SET_CONFIG;
241 req.value = 0;
242 req.index = 0x0001;
243 ret = ec168_ctrl_msg(d, &req);
244 if (ret)
245 goto error;
247 /* really needed - no idea what does */
248 req.cmd = GPIO;
249 req.value = 0;
250 req.index = 0x0206;
251 ret = ec168_ctrl_msg(d, &req);
252 if (ret)
253 goto error;
255 /* activate tuner I2C? */
256 req.cmd = WRITE_I2C;
257 req.value = 0;
258 req.index = 0x00c6;
259 ret = ec168_ctrl_msg(d, &req);
260 if (ret)
261 goto error;
263 return ret;
264 error:
265 dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
266 return 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,
279 &d->i2c_adap);
280 if (adap->fe[0] == NULL)
281 return -ENODEV;
283 return 0;
286 static struct mxl5005s_config ec168_mxl5003s_config = {
287 .i2c_address = 0xc6,
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);
318 if (onoff)
319 req.index = 0x0102;
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,
341 .num_adapters = 1,
342 .adapter = {
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,
376 .no_dynamic_id = 1,
377 .soft_unbind = 1,
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);