]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/media/dvb/dvb-usb/ec168.c
[media] ec168: convert to new DVB USB
[mirror_ubuntu-artful-kernel.git] / drivers / media / dvb / dvb-usb / ec168.c
CommitLineData
2bf290be
AP
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 * 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.
19 *
20 */
21
22#include "ec168.h"
23#include "ec100.h"
24#include "mxl5005s.h"
25
26/* debug */
27static int dvb_usb_ec168_debug;
28module_param_named(debug, dvb_usb_ec168_debug, int, 0644);
29MODULE_PARM_DESC(debug, "set debugging level" DVB_USB_DEBUG_STATUS);
30DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
31
3eee0472 32static int ec168_ctrl_msg(struct dvb_usb_device *d, struct ec168_req *req)
2bf290be
AP
33{
34 int ret;
35 unsigned int pipe;
36 u8 request, requesttype;
26b72c6e
FM
37 u8 *buf;
38
2bf290be
AP
39 switch (req->cmd) {
40 case DOWNLOAD_FIRMWARE:
41 case GPIO:
42 case WRITE_I2C:
43 case STREAMING_CTRL:
44 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
45 request = req->cmd;
46 break;
47 case READ_I2C:
48 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
49 request = req->cmd;
50 break;
51 case GET_CONFIG:
52 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
53 request = CONFIG;
54 break;
55 case SET_CONFIG:
56 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
57 request = CONFIG;
58 break;
59 case WRITE_DEMOD:
60 requesttype = (USB_TYPE_VENDOR | USB_DIR_OUT);
61 request = DEMOD_RW;
62 break;
63 case READ_DEMOD:
64 requesttype = (USB_TYPE_VENDOR | USB_DIR_IN);
65 request = DEMOD_RW;
66 break;
67 default:
68 err("unknown command:%02x", req->cmd);
69 ret = -EPERM;
70 goto error;
71 }
72
26b72c6e
FM
73 buf = kmalloc(req->size, GFP_KERNEL);
74 if (!buf) {
75 ret = -ENOMEM;
76 goto error;
77 }
78
2bf290be
AP
79 if (requesttype == (USB_TYPE_VENDOR | USB_DIR_OUT)) {
80 /* write */
81 memcpy(buf, req->data, req->size);
3eee0472 82 pipe = usb_sndctrlpipe(d->udev, 0);
2bf290be
AP
83 } else {
84 /* read */
3eee0472 85 pipe = usb_rcvctrlpipe(d->udev, 0);
2bf290be
AP
86 }
87
88 msleep(1); /* avoid I2C errors */
89
3eee0472 90 ret = usb_control_msg(d->udev, pipe, request, requesttype, req->value,
26b72c6e 91 req->index, buf, req->size, EC168_USB_TIMEOUT);
2bf290be
AP
92
93 ec168_debug_dump(request, requesttype, req->value, req->index, buf,
94 req->size, deb_xfer);
95
96 if (ret < 0)
26b72c6e 97 goto err_dealloc;
2bf290be
AP
98 else
99 ret = 0;
100
101 /* read request, copy returned data to return buf */
102 if (!ret && requesttype == (USB_TYPE_VENDOR | USB_DIR_IN))
103 memcpy(req->data, buf, req->size);
104
26b72c6e 105 kfree(buf);
2bf290be 106 return ret;
26b72c6e
FM
107
108err_dealloc:
109 kfree(buf);
2bf290be
AP
110error:
111 deb_info("%s: failed:%d\n", __func__, ret);
112 return ret;
113}
114
2bf290be 115/* I2C */
3eee0472
AP
116static struct ec100_config ec168_ec100_config;
117
2bf290be
AP
118static int ec168_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msg[],
119 int num)
120{
121 struct dvb_usb_device *d = i2c_get_adapdata(adap);
122 struct ec168_req req;
123 int i = 0;
124 int ret;
125
126 if (num > 2)
127 return -EINVAL;
128
129 if (mutex_lock_interruptible(&d->i2c_mutex) < 0)
130 return -EAGAIN;
131
132 while (i < num) {
133 if (num > i + 1 && (msg[i+1].flags & I2C_M_RD)) {
134 if (msg[i].addr == ec168_ec100_config.demod_address) {
135 req.cmd = READ_DEMOD;
136 req.value = 0;
137 req.index = 0xff00 + msg[i].buf[0]; /* reg */
138 req.size = msg[i+1].len; /* bytes to read */
139 req.data = &msg[i+1].buf[0];
140 ret = ec168_ctrl_msg(d, &req);
141 i += 2;
142 } else {
143 err("I2C read not implemented");
144 ret = -ENOSYS;
145 i += 2;
146 }
147 } else {
148 if (msg[i].addr == ec168_ec100_config.demod_address) {
149 req.cmd = WRITE_DEMOD;
150 req.value = msg[i].buf[1]; /* val */
151 req.index = 0xff00 + msg[i].buf[0]; /* reg */
152 req.size = 0;
153 req.data = NULL;
154 ret = ec168_ctrl_msg(d, &req);
155 i += 1;
156 } else {
157 req.cmd = WRITE_I2C;
158 req.value = msg[i].buf[0]; /* val */
159 req.index = 0x0100 + msg[i].addr; /* I2C addr */
160 req.size = msg[i].len-1;
161 req.data = &msg[i].buf[1];
162 ret = ec168_ctrl_msg(d, &req);
163 i += 1;
164 }
165 }
166 if (ret)
167 goto error;
168
169 }
170 ret = i;
171
172error:
173 mutex_unlock(&d->i2c_mutex);
174 return i;
175}
176
2bf290be
AP
177static u32 ec168_i2c_func(struct i2c_adapter *adapter)
178{
179 return I2C_FUNC_I2C;
180}
181
182static struct i2c_algorithm ec168_i2c_algo = {
183 .master_xfer = ec168_i2c_xfer,
184 .functionality = ec168_i2c_func,
185};
186
187/* Callbacks for DVB USB */
3eee0472 188static int ec168_identify_state(struct dvb_usb_device *d)
2bf290be 189{
3eee0472
AP
190 int ret;
191 u8 reply;
192 struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
2bf290be 193 deb_info("%s:\n", __func__);
2bf290be 194
3eee0472
AP
195 ret = ec168_ctrl_msg(d, &req);
196 if (ret)
197 goto error;
2bf290be 198
3eee0472 199 deb_info("%s: reply:%02x\n", __func__, reply);
2bf290be 200
3eee0472
AP
201 if (reply == 0x01)
202 ret = WARM;
203 else
204 ret = COLD;
2bf290be 205
3eee0472
AP
206 return ret;
207error:
208 deb_info("%s: failed:%d\n", __func__, ret);
209 return ret;
2bf290be
AP
210}
211
3eee0472
AP
212static int ec168_download_firmware(struct dvb_usb_device *d,
213 const struct firmware *fw)
2bf290be
AP
214{
215 int i, len, packets, remainder, ret;
216 u16 addr = 0x0000; /* firmware start address */
217 struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
218 deb_info("%s:\n", __func__);
219
220 #define FW_PACKET_MAX_DATA 2048
221 packets = fw->size / FW_PACKET_MAX_DATA;
222 remainder = fw->size % FW_PACKET_MAX_DATA;
223 len = FW_PACKET_MAX_DATA;
224 for (i = 0; i <= packets; i++) {
225 if (i == packets) /* set size of the last packet */
226 len = remainder;
227
228 req.size = len;
229 req.data = (u8 *)(fw->data + i * FW_PACKET_MAX_DATA);
230 req.index = addr;
231 addr += FW_PACKET_MAX_DATA;
232
3eee0472 233 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
234 if (ret) {
235 err("firmware download failed:%d packet:%d", ret, i);
236 goto error;
237 }
238 }
239 req.size = 0;
240
241 /* set "warm"? */
242 req.cmd = SET_CONFIG;
243 req.value = 0;
244 req.index = 0x0001;
3eee0472 245 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
246 if (ret)
247 goto error;
248
249 /* really needed - no idea what does */
250 req.cmd = GPIO;
251 req.value = 0;
252 req.index = 0x0206;
3eee0472 253 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
254 if (ret)
255 goto error;
256
257 /* activate tuner I2C? */
258 req.cmd = WRITE_I2C;
259 req.value = 0;
260 req.index = 0x00c6;
3eee0472 261 ret = ec168_ctrl_msg(d, &req);
2bf290be
AP
262 if (ret)
263 goto error;
264
265 return ret;
266error:
267 deb_info("%s: failed:%d\n", __func__, ret);
268 return ret;
269}
270
3eee0472
AP
271static struct ec100_config ec168_ec100_config = {
272 .demod_address = 0xff, /* not real address, demod is integrated */
273};
274
275static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
2bf290be 276{
2bf290be 277 deb_info("%s:\n", __func__);
3eee0472
AP
278 adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
279 &adap->dev->i2c_adap);
280 if (adap->fe[0] == NULL)
281 return -ENODEV;
2bf290be 282
3eee0472 283 return 0;
2bf290be
AP
284}
285
3eee0472
AP
286static 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,
301};
2bf290be 302
3eee0472 303static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
2bf290be 304{
3eee0472
AP
305 deb_info("%s:\n", __func__);
306 return dvb_attach(mxl5005s_attach, adap->fe[0],
307 &adap->dev->i2c_adap,
308 &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
2bf290be
AP
309}
310
3eee0472
AP
311static int ec168_streaming_ctrl(struct dvb_usb_adapter *adap, int onoff)
312{
313 struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
314 deb_info("%s: onoff:%d\n", __func__, onoff);
315 if (onoff)
316 req.index = 0x0102;
317 return ec168_ctrl_msg(adap->dev, &req);
318}
2bf290be 319
3eee0472
AP
320/* DVB USB Driver stuff */
321/* bInterfaceNumber 0 is HID
322 * bInterfaceNumber 1 is DVB-T */
323static struct dvb_usb_device_properties ec168_props = {
324 .driver_name = KBUILD_MODNAME,
325 .owner = THIS_MODULE,
326 .adapter_nr = adapter_nr,
327 .bInterfaceNumber = 1,
2bf290be 328
3eee0472 329 .identify_state = ec168_identify_state,
2bf290be 330 .firmware = "dvb-usb-ec168.fw",
3eee0472 331 .download_firmware = ec168_download_firmware,
2bf290be 332
3eee0472
AP
333 .i2c_algo = &ec168_i2c_algo,
334 .frontend_attach = ec168_ec100_frontend_attach,
335 .tuner_attach = ec168_mxl5003s_tuner_attach,
336 .streaming_ctrl = ec168_streaming_ctrl,
2bf290be
AP
337
338 .num_adapters = 1,
339 .adapter = {
340 {
2bf290be
AP
341 .stream = {
342 .type = USB_BULK,
343 .count = 6,
344 .endpoint = 0x82,
345 .u = {
346 .bulk = {
347 .buffersize = (32*512),
348 }
349 }
350 },
351 }
352 },
3eee0472 353};
2bf290be 354
3eee0472
AP
355static const struct dvb_usb_driver_info ec168_driver_info = {
356 .name = "E3C EC168 reference design",
357 .props = &ec168_props,
358};
2bf290be 359
3eee0472
AP
360static const struct usb_device_id ec168_id[] = {
361 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
362 .driver_info = (kernel_ulong_t) &ec168_driver_info },
363 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
364 .driver_info = (kernel_ulong_t) &ec168_driver_info },
365 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
366 .driver_info = (kernel_ulong_t) &ec168_driver_info },
367 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
368 .driver_info = (kernel_ulong_t) &ec168_driver_info },
369 { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
370 .driver_info = (kernel_ulong_t) &ec168_driver_info },
371 {}
2bf290be 372};
3eee0472 373MODULE_DEVICE_TABLE(usb, ec168_id);
2bf290be
AP
374
375static struct usb_driver ec168_driver = {
3eee0472
AP
376 .name = KBUILD_MODNAME,
377 .id_table = ec168_id,
378 .probe = dvb_usbv2_probe,
379 .disconnect = dvb_usbv2_disconnect,
380 .suspend = dvb_usbv2_suspend,
381 .resume = dvb_usbv2_resume,
382 .no_dynamic_id = 1,
383 .soft_unbind = 1,
2bf290be
AP
384};
385
ecb3b2b3 386module_usb_driver(ec168_driver);
2bf290be
AP
387
388MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
389MODULE_DESCRIPTION("E3C EC168 DVB-T USB2.0 driver");
390MODULE_LICENSE("GPL");