GNU Linux-libre 4.19.286-gnu1
[releases.git] / drivers / media / usb / dvb-usb-v2 / ec168.c
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                                 if (msg[i].len < 1) {
129                                         i = -EOPNOTSUPP;
130                                         break;
131                                 }
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;
145                         }
146                 } else {
147                         if (msg[i].addr == ec168_ec100_config.demod_address) {
148                                 if (msg[i].len < 1) {
149                                         i = -EOPNOTSUPP;
150                                         break;
151                                 }
152                                 req.cmd = WRITE_DEMOD;
153                                 req.value = msg[i].buf[1]; /* val */
154                                 req.index = 0xff00 + msg[i].buf[0]; /* reg */
155                                 req.size = 0;
156                                 req.data = NULL;
157                                 ret = ec168_ctrl_msg(d, &req);
158                                 i += 1;
159                         } else {
160                                 if (msg[i].len < 1) {
161                                         i = -EOPNOTSUPP;
162                                         break;
163                                 }
164                                 req.cmd = WRITE_I2C;
165                                 req.value = msg[i].buf[0]; /* val */
166                                 req.index = 0x0100 + msg[i].addr; /* I2C addr */
167                                 req.size = msg[i].len-1;
168                                 req.data = &msg[i].buf[1];
169                                 ret = ec168_ctrl_msg(d, &req);
170                                 i += 1;
171                         }
172                 }
173                 if (ret)
174                         goto error;
175
176         }
177         ret = i;
178
179 error:
180         mutex_unlock(&d->i2c_mutex);
181         return ret;
182 }
183
184 static u32 ec168_i2c_func(struct i2c_adapter *adapter)
185 {
186         return I2C_FUNC_I2C;
187 }
188
189 static struct i2c_algorithm ec168_i2c_algo = {
190         .master_xfer   = ec168_i2c_xfer,
191         .functionality = ec168_i2c_func,
192 };
193
194 /* Callbacks for DVB USB */
195 static int ec168_identify_state(struct dvb_usb_device *d, const char **name)
196 {
197         int ret;
198         u8 reply;
199         struct ec168_req req = {GET_CONFIG, 0, 1, sizeof(reply), &reply};
200         dev_dbg(&d->udev->dev, "%s:\n", __func__);
201
202         ret = ec168_ctrl_msg(d, &req);
203         if (ret)
204                 goto error;
205
206         dev_dbg(&d->udev->dev, "%s: reply=%02x\n", __func__, reply);
207
208         if (reply == 0x01)
209                 ret = WARM;
210         else
211                 ret = COLD;
212
213         return ret;
214 error:
215         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
216         return ret;
217 }
218
219 static int ec168_download_firmware(struct dvb_usb_device *d,
220                 const struct firmware *fw)
221 {
222         int ret, len, remaining;
223         struct ec168_req req = {DOWNLOAD_FIRMWARE, 0, 0, 0, NULL};
224         dev_dbg(&d->udev->dev, "%s:\n", __func__);
225
226         #define LEN_MAX 2048 /* max packet size */
227         for (remaining = fw->size; remaining > 0; remaining -= LEN_MAX) {
228                 len = remaining;
229                 if (len > LEN_MAX)
230                         len = LEN_MAX;
231
232                 req.size = len;
233                 req.data = (u8 *) &fw->data[fw->size - remaining];
234                 req.index = fw->size - remaining;
235
236                 ret = ec168_ctrl_msg(d, &req);
237                 if (ret) {
238                         dev_err(&d->udev->dev,
239                                         "%s: firmware download failed=%d\n",
240                                         KBUILD_MODNAME, ret);
241                         goto error;
242                 }
243         }
244
245         req.size = 0;
246
247         /* set "warm"? */
248         req.cmd = SET_CONFIG;
249         req.value = 0;
250         req.index = 0x0001;
251         ret = ec168_ctrl_msg(d, &req);
252         if (ret)
253                 goto error;
254
255         /* really needed - no idea what does */
256         req.cmd = GPIO;
257         req.value = 0;
258         req.index = 0x0206;
259         ret = ec168_ctrl_msg(d, &req);
260         if (ret)
261                 goto error;
262
263         /* activate tuner I2C? */
264         req.cmd = WRITE_I2C;
265         req.value = 0;
266         req.index = 0x00c6;
267         ret = ec168_ctrl_msg(d, &req);
268         if (ret)
269                 goto error;
270
271         return ret;
272 error:
273         dev_dbg(&d->udev->dev, "%s: failed=%d\n", __func__, ret);
274         return ret;
275 }
276
277 static struct ec100_config ec168_ec100_config = {
278         .demod_address = 0xff, /* not real address, demod is integrated */
279 };
280
281 static int ec168_ec100_frontend_attach(struct dvb_usb_adapter *adap)
282 {
283         struct dvb_usb_device *d = adap_to_d(adap);
284         dev_dbg(&d->udev->dev, "%s:\n", __func__);
285
286         adap->fe[0] = dvb_attach(ec100_attach, &ec168_ec100_config,
287                         &d->i2c_adap);
288         if (adap->fe[0] == NULL)
289                 return -ENODEV;
290
291         return 0;
292 }
293
294 static struct mxl5005s_config ec168_mxl5003s_config = {
295         .i2c_address     = 0xc6,
296         .if_freq         = IF_FREQ_4570000HZ,
297         .xtal_freq       = CRYSTAL_FREQ_16000000HZ,
298         .agc_mode        = MXL_SINGLE_AGC,
299         .tracking_filter = MXL_TF_OFF,
300         .rssi_enable     = MXL_RSSI_ENABLE,
301         .cap_select      = MXL_CAP_SEL_ENABLE,
302         .div_out         = MXL_DIV_OUT_4,
303         .clock_out       = MXL_CLOCK_OUT_DISABLE,
304         .output_load     = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
305         .top             = MXL5005S_TOP_25P2,
306         .mod_mode        = MXL_DIGITAL_MODE,
307         .if_mode         = MXL_ZERO_IF,
308         .AgcMasterByte   = 0x00,
309 };
310
311 static int ec168_mxl5003s_tuner_attach(struct dvb_usb_adapter *adap)
312 {
313         struct dvb_usb_device *d = adap_to_d(adap);
314         dev_dbg(&d->udev->dev, "%s:\n", __func__);
315
316         return dvb_attach(mxl5005s_attach, adap->fe[0], &d->i2c_adap,
317                         &ec168_mxl5003s_config) == NULL ? -ENODEV : 0;
318 }
319
320 static int ec168_streaming_ctrl(struct dvb_frontend *fe, int onoff)
321 {
322         struct dvb_usb_device *d = fe_to_d(fe);
323         struct ec168_req req = {STREAMING_CTRL, 0x7f01, 0x0202, 0, NULL};
324         dev_dbg(&d->udev->dev, "%s: onoff=%d\n", __func__, onoff);
325
326         if (onoff)
327                 req.index = 0x0102;
328         return ec168_ctrl_msg(d, &req);
329 }
330
331 /* DVB USB Driver stuff */
332 /* bInterfaceNumber 0 is HID
333  * bInterfaceNumber 1 is DVB-T */
334 static struct dvb_usb_device_properties ec168_props = {
335         .driver_name = KBUILD_MODNAME,
336         .owner = THIS_MODULE,
337         .adapter_nr = adapter_nr,
338         .bInterfaceNumber = 1,
339
340         .identify_state = ec168_identify_state,
341         .firmware = EC168_FIRMWARE,
342         .download_firmware = ec168_download_firmware,
343
344         .i2c_algo = &ec168_i2c_algo,
345         .frontend_attach = ec168_ec100_frontend_attach,
346         .tuner_attach = ec168_mxl5003s_tuner_attach,
347         .streaming_ctrl = ec168_streaming_ctrl,
348
349         .num_adapters = 1,
350         .adapter = {
351                 {
352                         .stream = DVB_USB_STREAM_BULK(0x82, 6, 32 * 512),
353                 }
354         },
355 };
356
357 static const struct dvb_usb_driver_info ec168_driver_info = {
358         .name = "E3C EC168 reference design",
359         .props = &ec168_props,
360 };
361
362 static const struct usb_device_id ec168_id[] = {
363         { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168),
364                 .driver_info = (kernel_ulong_t) &ec168_driver_info },
365         { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_2),
366                 .driver_info = (kernel_ulong_t) &ec168_driver_info },
367         { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_3),
368                 .driver_info = (kernel_ulong_t) &ec168_driver_info },
369         { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_4),
370                 .driver_info = (kernel_ulong_t) &ec168_driver_info },
371         { USB_DEVICE(USB_VID_E3C, USB_PID_E3C_EC168_5),
372                 .driver_info = (kernel_ulong_t) &ec168_driver_info },
373         {}
374 };
375 MODULE_DEVICE_TABLE(usb, ec168_id);
376
377 static struct usb_driver ec168_driver = {
378         .name = KBUILD_MODNAME,
379         .id_table = ec168_id,
380         .probe = dvb_usbv2_probe,
381         .disconnect = dvb_usbv2_disconnect,
382         .suspend = dvb_usbv2_suspend,
383         .resume = dvb_usbv2_resume,
384         .no_dynamic_id = 1,
385         .soft_unbind = 1,
386 };
387
388 module_usb_driver(ec168_driver);
389
390 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
391 MODULE_DESCRIPTION("E3C EC168 driver");
392 MODULE_LICENSE("GPL");
393 /*(DEBLOBBED)*/