blob: 420cb62fbc7540a96c74345d36cb8dd8130e1bb8 [file] [log] [blame]
Mauro Carvalho Chehab25c16462011-07-23 10:59:25 -03001/*
2 * Driver for AzureWave 6007 DVB-C/T USB2.0 and clones
3 *
4 * Copyright (c) Henry Wang <Henry.wang@AzureWave.com>
5 *
6 * This driver was made publicly available by Terratec, at:
7 * http://linux.terratec.de/files/TERRATEC_H7/20110323_TERRATEC_H7_Linux.tar.gz
8 * The original driver's license is GPL, as declared with MODULE_LICENSE()
9 *
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030010 * Copyright (c) 2010-2011 Mauro Carvalho Chehab <mchehab@redhat.com>
11 * Driver modified by in order to work with upstream drxk driver, and
12 * tons of bugs got fixed.
Mauro Carvalho Chehab25c16462011-07-23 10:59:25 -030013 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation under version 2 of the License.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030022 */
23
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030024#include "drxk.h"
25#include "mt2063.h"
26#include "dvb_ca_en50221.h"
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -030027#include "dvb_usb.h"
28#include "cypress_firmware.h"
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030029
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -030030#define AZ6007_FIRMWARE "dvb-usb-terratec-h7-az6007.fw"
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030031
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -030032static int az6007_xfer_debug;
33module_param_named(xfer_debug, az6007_xfer_debug, int, 0644);
34MODULE_PARM_DESC(xfer_debug, "Enable xfer debug");
Mauro Carvalho Chehab70fa4442011-07-23 10:55:10 -030035
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030036DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
37
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030038/* Known requests (Cypress FX2 firmware + az6007 "private" ones*/
39
40#define FX2_OED 0xb5
41#define AZ6007_READ_DATA 0xb7
42#define AZ6007_I2C_RD 0xb9
43#define AZ6007_POWER 0xbc
44#define AZ6007_I2C_WR 0xbd
45#define FX2_SCON1 0xc0
46#define AZ6007_TS_THROUGH 0xc7
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -030047#define AZ6007_READ_IR 0xb4
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -030048
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030049struct az6007_device_state {
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -030050 struct mutex mutex;
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -030051 struct mutex ca_mutex;
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -030052 struct dvb_ca_en50221 ca;
53 unsigned warm:1;
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030054 int (*gate_ctrl) (struct dvb_frontend *, int);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030055 unsigned char data[4096];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030056};
57
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030058static struct drxk_config terratec_h7_drxk = {
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030059 .adr = 0x29,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030060 .parallel_ts = true,
61 .dynamic_clk = true,
62 .single_master = true,
Mauro Carvalho Chehabd5856812012-01-21 07:57:06 -030063 .enable_merr_cfg = true,
Mauro Carvalho Chehab6e5caf842012-01-20 18:36:26 -030064 .no_i2c_bridge = false,
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -030065 .chunk_size = 64,
Mauro Carvalho Chehab6fb65a62012-01-20 19:13:07 -030066 .mpeg_out_clk_strength = 0x02,
Mauro Carvalho Chehabcaeb5ac2012-08-05 00:10:13 -030067 .qam_demod_parameter_count = 2,
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -030068 .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030069};
70
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030071static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
72{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -030073 struct az6007_device_state *st = fe_to_priv(fe);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030074 struct dvb_usb_adapter *adap = fe->sec_priv;
Mauro Carvalho Chehabf2ba9e52011-07-23 11:54:40 -030075 int status = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030076
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -030077 pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable");
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030078
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -030079 if (!adap || !st)
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030080 return -EINVAL;
81
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030082 if (enable)
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030083 status = st->gate_ctrl(fe, 1);
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030084 else
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030085 status = st->gate_ctrl(fe, 0);
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030086
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -030087 return status;
88}
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030089
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030090static struct mt2063_config az6007_mt2063_config = {
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -030091 .tuner_address = 0x60,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030092 .refclock = 36125000,
93};
94
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -030095static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -030096 u16 index, u8 *b, int blen)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030097{
Mauro Carvalho Chehab9b01f3d2011-07-29 11:40:40 -030098 int ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -030099
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300100 ret = usb_control_msg(udev,
101 usb_rcvctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300102 req,
103 USB_TYPE_VENDOR | USB_DIR_IN,
104 value, index, b, blen, 5000);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300105 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300106 pr_warn("usb read operation failed. (%d)\n", ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300107 return -EIO;
108 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300109
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300110 if (az6007_xfer_debug) {
111 printk(KERN_DEBUG "az6007: IN req: %02x, value: %04x, index: %04x\n",
112 req, value, index);
113 print_hex_dump_bytes("az6007: payload: ",
114 DUMP_PREFIX_NONE, b, blen);
115 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300116
117 return ret;
118}
119
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300120static int az6007_read(struct dvb_usb_device *d, u8 req, u16 value,
121 u16 index, u8 *b, int blen)
122{
123 struct az6007_device_state *st = d->priv;
124 int ret;
125
126 if (mutex_lock_interruptible(&st->mutex) < 0)
127 return -EAGAIN;
128
129 ret = __az6007_read(d->udev, req, value, index, b, blen);
130
131 mutex_unlock(&st->mutex);
132
133 return ret;
134}
135
136static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300137 u16 index, u8 *b, int blen)
138{
139 int ret;
140
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300141 if (az6007_xfer_debug) {
142 printk(KERN_DEBUG "az6007: OUT req: %02x, value: %04x, index: %04x\n",
143 req, value, index);
144 print_hex_dump_bytes("az6007: payload: ",
145 DUMP_PREFIX_NONE, b, blen);
146 }
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300147
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300148 if (blen > 64) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300149 pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
150 blen);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300151 return -EOPNOTSUPP;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300152 }
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300153
Mauro Carvalho Chehab3af2f4f2011-07-25 11:17:41 -0300154 ret = usb_control_msg(udev,
155 usb_sndctrlpipe(udev, 0),
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300156 req,
157 USB_TYPE_VENDOR | USB_DIR_OUT,
158 value, index, b, blen, 5000);
159 if (ret != blen) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300160 pr_err("usb write operation failed. (%d)\n", ret);
Mauro Carvalho Chehab35753582011-07-23 10:12:12 -0300161 return -EIO;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300162 }
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300163
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300164 return 0;
165}
166
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300167static int az6007_write(struct dvb_usb_device *d, u8 req, u16 value,
168 u16 index, u8 *b, int blen)
169{
170 struct az6007_device_state *st = d->priv;
171 int ret;
172
173 if (mutex_lock_interruptible(&st->mutex) < 0)
174 return -EAGAIN;
175
176 ret = __az6007_write(d->udev, req, value, index, b, blen);
177
178 mutex_unlock(&st->mutex);
179
180 return ret;
181}
182
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300183static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300184{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300185 struct dvb_usb_device *d = fe_to_d(fe);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300186
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300187 pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable");
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300188
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300189 return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300190}
191
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300192/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300193static int az6007_rc_query(struct dvb_usb_device *d)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300194{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300195 struct az6007_device_state *st = d_to_priv(d);
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300196 unsigned code = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300197
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300198 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300199
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300200 if (st->data[1] == 0x44)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300201 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300202
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300203 if ((st->data[1] ^ st->data[2]) == 0xff)
204 code = st->data[1];
205 else
206 code = st->data[1] << 8 | st->data[2];
207
208 if ((st->data[3] ^ st->data[4]) == 0xff)
209 code = code << 8 | st->data[3];
210 else
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200211 code = code << 16 | st->data[3] << 8 | st->data[4];
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300212
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300213 rc_keydown(d->rc_dev, code, st->data[5]);
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300214
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300215 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216}
217
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300218static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
219 int slot,
220 int address)
221{
222 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300223 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300224
225 int ret;
226 u8 req;
227 u16 value;
228 u16 index;
229 int blen;
230 u8 *b;
231
232 if (slot != 0)
233 return -EINVAL;
234
235 b = kmalloc(12, GFP_KERNEL);
236 if (!b)
237 return -ENOMEM;
238
239 mutex_lock(&state->ca_mutex);
240
241 req = 0xC1;
242 value = address;
243 index = 0;
244 blen = 1;
245
246 ret = az6007_read(d, req, value, index, b, blen);
247 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300248 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300249 ret = -EINVAL;
250 } else {
251 ret = b[0];
252 }
253
254 mutex_unlock(&state->ca_mutex);
255 kfree(b);
256 return ret;
257}
258
259static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
260 int slot,
261 int address,
262 u8 value)
263{
264 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300265 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300266
267 int ret;
268 u8 req;
269 u16 value1;
270 u16 index;
271 int blen;
272
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300273 pr_debug("%s(), slot %d\n", __func__, slot);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300274 if (slot != 0)
275 return -EINVAL;
276
277 mutex_lock(&state->ca_mutex);
278 req = 0xC2;
279 value1 = address;
280 index = value;
281 blen = 0;
282
283 ret = az6007_write(d, req, value1, index, NULL, blen);
284 if (ret != 0)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300285 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300286
287 mutex_unlock(&state->ca_mutex);
288 return ret;
289}
290
291static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca,
292 int slot,
293 u8 address)
294{
295 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300296 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300297
298 int ret;
299 u8 req;
300 u16 value;
301 u16 index;
302 int blen;
303 u8 *b;
304
305 if (slot != 0)
306 return -EINVAL;
307
308 b = kmalloc(12, GFP_KERNEL);
309 if (!b)
310 return -ENOMEM;
311
312 mutex_lock(&state->ca_mutex);
313
314 req = 0xC3;
315 value = address;
316 index = 0;
317 blen = 2;
318
319 ret = az6007_read(d, req, value, index, b, blen);
320 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300321 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300322 ret = -EINVAL;
323 } else {
324 if (b[0] == 0)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300325 pr_warn("Read CI IO error\n");
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300326
327 ret = b[1];
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300328 pr_debug("read cam data = %x from 0x%x\n", b[1], value);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300329 }
330
331 mutex_unlock(&state->ca_mutex);
332 kfree(b);
333 return ret;
334}
335
336static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca,
337 int slot,
338 u8 address,
339 u8 value)
340{
341 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300342 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300343
344 int ret;
345 u8 req;
346 u16 value1;
347 u16 index;
348 int blen;
349
350 if (slot != 0)
351 return -EINVAL;
352
353 mutex_lock(&state->ca_mutex);
354 req = 0xC4;
355 value1 = address;
356 index = value;
357 blen = 0;
358
359 ret = az6007_write(d, req, value1, index, NULL, blen);
360 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300361 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300362 goto failed;
363 }
364
365failed:
366 mutex_unlock(&state->ca_mutex);
367 return ret;
368}
369
370static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
371{
372 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
373
374 int ret;
375 u8 req;
376 u16 value;
377 u16 index;
378 int blen;
379 u8 *b;
380
381 b = kmalloc(12, GFP_KERNEL);
382 if (!b)
383 return -ENOMEM;
384
385 req = 0xC8;
386 value = 0;
387 index = 0;
388 blen = 1;
389
390 ret = az6007_read(d, req, value, index, b, blen);
391 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300392 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300393 ret = -EIO;
394 } else{
395 ret = b[0];
396 }
397 kfree(b);
398 return ret;
399}
400
401static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
402{
403 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300404 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300405
406 int ret, i;
407 u8 req;
408 u16 value;
409 u16 index;
410 int blen;
411
412 mutex_lock(&state->ca_mutex);
413
414 req = 0xC6;
415 value = 1;
416 index = 0;
417 blen = 0;
418
419 ret = az6007_write(d, req, value, index, NULL, blen);
420 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300421 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300422 goto failed;
423 }
424
425 msleep(500);
426 req = 0xC6;
427 value = 0;
428 index = 0;
429 blen = 0;
430
431 ret = az6007_write(d, req, value, index, NULL, blen);
432 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300433 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300434 goto failed;
435 }
436
437 for (i = 0; i < 15; i++) {
438 msleep(100);
439
440 if (CI_CamReady(ca, slot)) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300441 pr_debug("CAM Ready\n");
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300442 break;
443 }
444 }
445 msleep(5000);
446
447failed:
448 mutex_unlock(&state->ca_mutex);
449 return ret;
450}
451
452static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
453{
454 return 0;
455}
456
457static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
458{
459 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300460 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300461
462 int ret;
463 u8 req;
464 u16 value;
465 u16 index;
466 int blen;
467
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300468 pr_debug("%s()\n", __func__);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300469 mutex_lock(&state->ca_mutex);
470 req = 0xC7;
471 value = 1;
472 index = 0;
473 blen = 0;
474
475 ret = az6007_write(d, req, value, index, NULL, blen);
476 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300477 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300478 goto failed;
479 }
480
481failed:
482 mutex_unlock(&state->ca_mutex);
483 return ret;
484}
485
486static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
487{
488 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300489 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300490 int ret;
491 u8 req;
492 u16 value;
493 u16 index;
494 int blen;
495 u8 *b;
496
497 b = kmalloc(12, GFP_KERNEL);
498 if (!b)
499 return -ENOMEM;
500 mutex_lock(&state->ca_mutex);
501
502 req = 0xC5;
503 value = 0;
504 index = 0;
505 blen = 1;
506
507 ret = az6007_read(d, req, value, index, b, blen);
508 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300509 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300510 ret = -EIO;
511 } else
512 ret = 0;
513
514 if (!ret && b[0] == 1) {
515 ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
516 DVB_CA_EN50221_POLL_CAM_READY;
517 }
518
519 mutex_unlock(&state->ca_mutex);
520 kfree(b);
521 return ret;
522}
523
524
525static void az6007_ci_uninit(struct dvb_usb_device *d)
526{
527 struct az6007_device_state *state;
528
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300529 pr_debug("%s()\n", __func__);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300530
531 if (NULL == d)
532 return;
533
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300534 state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300535 if (NULL == state)
536 return;
537
538 if (NULL == state->ca.data)
539 return;
540
541 dvb_ca_en50221_release(&state->ca);
542
543 memset(&state->ca, 0, sizeof(state->ca));
544}
545
546
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300547static int az6007_ci_init(struct dvb_usb_adapter *adap)
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300548{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300549 struct dvb_usb_device *d = adap_to_d(adap);
550 struct az6007_device_state *state = adap_to_priv(adap);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300551 int ret;
552
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300553 pr_debug("%s()\n", __func__);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300554
555 mutex_init(&state->ca_mutex);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300556 state->ca.owner = THIS_MODULE;
557 state->ca.read_attribute_mem = az6007_ci_read_attribute_mem;
558 state->ca.write_attribute_mem = az6007_ci_write_attribute_mem;
559 state->ca.read_cam_control = az6007_ci_read_cam_control;
560 state->ca.write_cam_control = az6007_ci_write_cam_control;
561 state->ca.slot_reset = az6007_ci_slot_reset;
562 state->ca.slot_shutdown = az6007_ci_slot_shutdown;
563 state->ca.slot_ts_enable = az6007_ci_slot_ts_enable;
564 state->ca.poll_slot_status = az6007_ci_poll_slot_status;
565 state->ca.data = d;
566
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300567 ret = dvb_ca_en50221_init(&adap->dvb_adap,
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300568 &state->ca,
569 0, /* flags */
570 1);/* n_slots */
571 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300572 pr_err("Cannot initialize CI: Error %d.\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300573 memset(&state->ca, 0, sizeof(state->ca));
574 return ret;
575 }
576
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300577 pr_debug("CI initialized.\n");
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300578
579 return 0;
580}
581
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300582static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300583{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300584 struct dvb_usb_device *d = adap_to_d(adap);
585 struct az6007_device_state *st = adap_to_priv(adap);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300586 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300587
588 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
Alan Cox5a7a5702012-07-24 12:02:46 -0300589 memcpy(mac, st->data, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300590
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300591 if (ret > 0)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300592 pr_debug("%s: mac is %pM\n", __func__, mac);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300593
594 return ret;
595}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300596
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300597static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
598{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300599 struct az6007_device_state *st = adap_to_priv(adap);
600 struct dvb_usb_device *d = adap_to_d(adap);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300601
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300602 pr_debug("attaching demod drxk\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300603
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300604 adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
605 &d->i2c_adap);
606 if (!adap->fe[0])
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300607 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300608
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300609 adap->fe[0]->sec_priv = adap;
610 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
611 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300612
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300613 az6007_ci_init(adap);
614
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300615 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300616}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300617
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300618static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
619{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300620 struct dvb_usb_device *d = adap_to_d(adap);
621
622 pr_debug("attaching tuner mt2063\n");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300623
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300624 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300625 if (adap->fe[0]->ops.i2c_gate_ctrl)
626 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1);
627 if (!dvb_attach(mt2063_attach, adap->fe[0],
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300628 &az6007_mt2063_config,
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300629 &d->i2c_adap))
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300630 return -EINVAL;
631
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300632 if (adap->fe[0]->ops.i2c_gate_ctrl)
633 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300634
635 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300636}
637
Mauro Carvalho Chehab0ca477e2012-08-05 09:23:17 -0300638static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300639{
Mauro Carvalho Chehab3cb9f4e2012-08-05 09:22:23 -0300640 struct az6007_device_state *state = d_to_priv(d);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300641 int ret;
642
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300643 pr_debug("%s()\n", __func__);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300644
Mauro Carvalho Chehab3cb9f4e2012-08-05 09:22:23 -0300645 if (!state->warm) {
646 mutex_init(&state->mutex);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300647
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300648 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300649 if (ret < 0)
650 return ret;
651 msleep(60);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300652 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300653 if (ret < 0)
654 return ret;
655 msleep(100);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300656 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300657 if (ret < 0)
658 return ret;
659 msleep(20);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300660 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300661 if (ret < 0)
662 return ret;
663
664 msleep(400);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300665 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300666 if (ret < 0)
667 return ret;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200668 msleep(150);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300669 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300670 if (ret < 0)
671 return ret;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200672 msleep(430);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300673 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300674 if (ret < 0)
675 return ret;
676
Mauro Carvalho Chehab3cb9f4e2012-08-05 09:22:23 -0300677 state->warm = true;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300678
679 return 0;
680 }
681
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300682 if (!onoff)
683 return 0;
684
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300685 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
686 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300687
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300688 return 0;
689}
690
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300691/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300692static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
693 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300694{
695 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300696 struct az6007_device_state *st = d_to_priv(d);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300697 int i, j, len;
698 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300699 u16 index;
700 u16 value;
701 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300702 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300703
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300704 if (mutex_lock_interruptible(&st->mutex) < 0)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300705 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300706
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300707 for (i = 0; i < num; i++) {
708 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300709 if (((i + 1) < num)
710 && (msgs[i].len == 1)
Mauro Carvalho Chehab916c8142012-08-04 21:39:41 -0300711 && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD)
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300712 && (msgs[i + 1].flags & I2C_M_RD)
713 && (msgs[i].addr == msgs[i + 1].addr)) {
714 /*
715 * A write + read xfer for the same address, where
716 * the first xfer has just 1 byte length.
717 * Need to join both into one operation
718 */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300719 if (az6007_xfer_debug)
720 printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300721 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300722 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300723 index = msgs[i].buf[0];
724 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300725 length = 6 + msgs[i + 1].len;
726 len = msgs[i + 1].len;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200727 ret = __az6007_read(d->udev, req, value, index,
728 st->data, length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300729 if (ret >= len) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300730 for (j = 0; j < len; j++)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300731 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300732 } else
733 ret = -EIO;
734 i++;
735 } else if (!(msgs[i].flags & I2C_M_RD)) {
736 /* write bytes */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300737 if (az6007_xfer_debug)
738 printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n",
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300739 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300740 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300741 index = msgs[i].buf[0];
742 value = addr | (1 << 8);
743 length = msgs[i].len - 1;
744 len = msgs[i].len - 1;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300745 for (j = 0; j < len; j++)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300746 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200747 ret = __az6007_write(d->udev, req, value, index,
748 st->data, length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300749 } else {
750 /* read bytes */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300751 if (az6007_xfer_debug)
752 printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n",
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300753 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300754 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300755 index = msgs[i].buf[0];
756 value = addr;
757 length = msgs[i].len + 6;
758 len = msgs[i].len;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200759 ret = __az6007_read(d->udev, req, value, index,
760 st->data, length);
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300761 for (j = 0; j < len; j++)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300762 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300763 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300764 if (ret < 0)
765 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300766 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300767err:
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300768 mutex_unlock(&st->mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300769
770 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300771 pr_info("%s ERROR: %i\n", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300772 return ret;
773 }
774 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300775}
776
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300777static u32 az6007_i2c_func(struct i2c_adapter *adapter)
778{
779 return I2C_FUNC_I2C;
780}
781
782static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300783 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300784 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300785};
786
Mauro Carvalho Chehab0ca477e2012-08-05 09:23:17 -0300787static int az6007_identify_state(struct dvb_usb_device *d, const char **name)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300788{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300789 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300790 u8 *mac;
791
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300792 pr_debug("Identifying az6007 state\n");
793
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300794 mac = kmalloc(6, GFP_ATOMIC);
795 if (!mac)
796 return -ENOMEM;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300797
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300798 /* Try to read the mac address */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300799 ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300800 if (ret == 6)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300801 ret = WARM;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300802 else
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300803 ret = COLD;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300804
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300805 kfree(mac);
806
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300807 if (ret == COLD) {
808 __az6007_write(d->udev, 0x09, 1, 0, NULL, 0);
809 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
810 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300811 }
812
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300813 pr_debug("Device is on %s state\n",
814 ret == WARM ? "warm" : "cold");
815 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300816}
817
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300818static void az6007_usb_disconnect(struct usb_interface *intf)
819{
820 struct dvb_usb_device *d = usb_get_intfdata(intf);
821 az6007_ci_uninit(d);
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300822 dvb_usbv2_disconnect(intf);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300823}
824
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300825static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300826{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300827 pr_debug("Getting az6007 Remote Control properties\n");
828
829 rc->allowed_protos = RC_TYPE_NEC;
830 rc->query = az6007_rc_query;
831 rc->interval = 400;
832
833 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300834}
835
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300836static int az6007_download_firmware(struct dvb_usb_device *d,
837 const struct firmware *fw)
838{
839 pr_debug("Loading az6007 firmware\n");
840
841 return usbv2_cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
842}
843
844/* DVB USB Driver stuff */
845static struct dvb_usb_device_properties az6007_props = {
846 .driver_name = KBUILD_MODNAME,
847 .owner = THIS_MODULE,
848 .firmware = AZ6007_FIRMWARE,
849
850 .adapter_nr = adapter_nr,
851 .size_of_priv = sizeof(struct az6007_device_state),
852 .i2c_algo = &az6007_i2c_algo,
853 .tuner_attach = az6007_tuner_attach,
854 .frontend_attach = az6007_frontend_attach,
855 .streaming_ctrl = az6007_streaming_ctrl,
856 .get_rc_config = az6007_get_rc_config,
857 .read_mac_address = az6007_read_mac_addr,
858 .download_firmware = az6007_download_firmware,
859 .identify_state = az6007_identify_state,
860 .power_ctrl = az6007_power_ctrl,
861 .num_adapters = 1,
862 .adapter = {
863 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
864 }
865};
866
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300867static struct usb_device_id az6007_usb_table[] = {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300868 {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
869 &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
870 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7,
871 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
872 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
873 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300874 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300875};
876
877MODULE_DEVICE_TABLE(usb, az6007_usb_table);
878
Mauro Carvalho Chehab6da74b12012-08-05 09:25:34 -0300879static int az6007_suspend(struct usb_interface *intf, pm_message_t msg)
880{
881 struct dvb_usb_device *d = usb_get_intfdata(intf);
882
883 az6007_ci_uninit(d);
884 return dvb_usbv2_suspend(intf, msg);
885}
886
887static int az6007_resume(struct usb_interface *intf)
888{
889 struct dvb_usb_device *d = usb_get_intfdata(intf);
890 struct dvb_usb_adapter *adap = &d->adapter[0];
891
892 az6007_ci_init(adap);
893 return dvb_usbv2_resume(intf);
894}
895
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300896/* usb specific object needed to register this driver with the usb subsystem */
897static struct usb_driver az6007_usb_driver = {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300898 .name = KBUILD_MODNAME,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300899 .id_table = az6007_usb_table,
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300900 .probe = dvb_usbv2_probe,
901 .disconnect = az6007_usb_disconnect,
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300902 .no_dynamic_id = 1,
903 .soft_unbind = 1,
Mauro Carvalho Chehab6da74b12012-08-05 09:25:34 -0300904 /*
905 * FIXME: need to implement reset_resume, likely with
906 * dvb-usb-v2 core support
907 */
908 .suspend = az6007_suspend,
909 .resume = az6007_resume,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300910};
911
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300912module_usb_driver(az6007_usb_driver);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300913
914MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300915MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300916MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300917MODULE_VERSION("2.0");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300918MODULE_LICENSE("GPL");
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300919MODULE_FIRMWARE(AZ6007_FIRMWARE);