blob: 70ec80d8be7110efecd920ba2878cef41f9fb028 [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 Chehabc3707352012-08-05 09:35:02 -030010 * Copyright (c) 2010-2012 Mauro Carvalho Chehab <mchehab@redhat.com>
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -030011 * Driver modified by in order to work with upstream drxk driver, and
Mauro Carvalho Chehabc3707352012-08-05 09:35:02 -030012 * tons of bugs got fixed, and converted to use dvb-usb-v2.
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
Antti Palosaari37b44a02013-01-04 15:21:26 -0300192#if IS_ENABLED(CONFIG_RC_CORE)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300193/* remote control stuff (does not work with my box) */
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300194static int az6007_rc_query(struct dvb_usb_device *d)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300195{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300196 struct az6007_device_state *st = d_to_priv(d);
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300197 unsigned code = 0;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300198
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300199 az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300200
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300201 if (st->data[1] == 0x44)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300202 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300203
Mauro Carvalho Chehab91651442012-01-21 12:14:13 -0300204 if ((st->data[1] ^ st->data[2]) == 0xff)
205 code = st->data[1];
206 else
207 code = st->data[1] << 8 | st->data[2];
208
209 if ((st->data[3] ^ st->data[4]) == 0xff)
210 code = code << 8 | st->data[3];
211 else
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200212 code = code << 16 | st->data[3] << 8 | st->data[4];
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300213
Mauro Carvalho Chehabd3d076a2012-01-21 12:20:30 -0300214 rc_keydown(d->rc_dev, code, st->data[5]);
Mauro Carvalho Chehab2d5161b2011-07-29 11:31:13 -0300215
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300216 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300217}
218
Antti Palosaari7b753222012-12-09 20:23:22 -0300219static int az6007_get_rc_config(struct dvb_usb_device *d, struct dvb_usb_rc *rc)
220{
221 pr_debug("Getting az6007 Remote Control properties\n");
222
223 rc->allowed_protos = RC_BIT_NEC;
224 rc->query = az6007_rc_query;
225 rc->interval = 400;
226
227 return 0;
228}
229#else
230 #define az6007_get_rc_config NULL
231#endif
232
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300233static int az6007_ci_read_attribute_mem(struct dvb_ca_en50221 *ca,
234 int slot,
235 int address)
236{
237 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300238 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300239
240 int ret;
241 u8 req;
242 u16 value;
243 u16 index;
244 int blen;
245 u8 *b;
246
247 if (slot != 0)
248 return -EINVAL;
249
250 b = kmalloc(12, GFP_KERNEL);
251 if (!b)
252 return -ENOMEM;
253
254 mutex_lock(&state->ca_mutex);
255
256 req = 0xC1;
257 value = address;
258 index = 0;
259 blen = 1;
260
261 ret = az6007_read(d, req, value, index, b, blen);
262 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300263 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300264 ret = -EINVAL;
265 } else {
266 ret = b[0];
267 }
268
269 mutex_unlock(&state->ca_mutex);
270 kfree(b);
271 return ret;
272}
273
274static int az6007_ci_write_attribute_mem(struct dvb_ca_en50221 *ca,
275 int slot,
276 int address,
277 u8 value)
278{
279 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300280 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300281
282 int ret;
283 u8 req;
284 u16 value1;
285 u16 index;
286 int blen;
287
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300288 pr_debug("%s(), slot %d\n", __func__, slot);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300289 if (slot != 0)
290 return -EINVAL;
291
292 mutex_lock(&state->ca_mutex);
293 req = 0xC2;
294 value1 = address;
295 index = value;
296 blen = 0;
297
298 ret = az6007_write(d, req, value1, index, NULL, blen);
299 if (ret != 0)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300300 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300301
302 mutex_unlock(&state->ca_mutex);
303 return ret;
304}
305
306static int az6007_ci_read_cam_control(struct dvb_ca_en50221 *ca,
307 int slot,
308 u8 address)
309{
310 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300311 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300312
313 int ret;
314 u8 req;
315 u16 value;
316 u16 index;
317 int blen;
318 u8 *b;
319
320 if (slot != 0)
321 return -EINVAL;
322
323 b = kmalloc(12, GFP_KERNEL);
324 if (!b)
325 return -ENOMEM;
326
327 mutex_lock(&state->ca_mutex);
328
329 req = 0xC3;
330 value = address;
331 index = 0;
332 blen = 2;
333
334 ret = az6007_read(d, req, value, index, b, blen);
335 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300336 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300337 ret = -EINVAL;
338 } else {
339 if (b[0] == 0)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300340 pr_warn("Read CI IO error\n");
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300341
342 ret = b[1];
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300343 pr_debug("read cam data = %x from 0x%x\n", b[1], value);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300344 }
345
346 mutex_unlock(&state->ca_mutex);
347 kfree(b);
348 return ret;
349}
350
351static int az6007_ci_write_cam_control(struct dvb_ca_en50221 *ca,
352 int slot,
353 u8 address,
354 u8 value)
355{
356 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300357 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300358
359 int ret;
360 u8 req;
361 u16 value1;
362 u16 index;
363 int blen;
364
365 if (slot != 0)
366 return -EINVAL;
367
368 mutex_lock(&state->ca_mutex);
369 req = 0xC4;
370 value1 = address;
371 index = value;
372 blen = 0;
373
374 ret = az6007_write(d, req, value1, index, NULL, blen);
375 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300376 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300377 goto failed;
378 }
379
380failed:
381 mutex_unlock(&state->ca_mutex);
382 return ret;
383}
384
385static int CI_CamReady(struct dvb_ca_en50221 *ca, int slot)
386{
387 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
388
389 int ret;
390 u8 req;
391 u16 value;
392 u16 index;
393 int blen;
394 u8 *b;
395
396 b = kmalloc(12, GFP_KERNEL);
397 if (!b)
398 return -ENOMEM;
399
400 req = 0xC8;
401 value = 0;
402 index = 0;
403 blen = 1;
404
405 ret = az6007_read(d, req, value, index, b, blen);
406 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300407 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300408 ret = -EIO;
409 } else{
410 ret = b[0];
411 }
412 kfree(b);
413 return ret;
414}
415
416static int az6007_ci_slot_reset(struct dvb_ca_en50221 *ca, int slot)
417{
418 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300419 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300420
421 int ret, i;
422 u8 req;
423 u16 value;
424 u16 index;
425 int blen;
426
427 mutex_lock(&state->ca_mutex);
428
429 req = 0xC6;
430 value = 1;
431 index = 0;
432 blen = 0;
433
434 ret = az6007_write(d, req, value, index, NULL, blen);
435 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300436 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300437 goto failed;
438 }
439
440 msleep(500);
441 req = 0xC6;
442 value = 0;
443 index = 0;
444 blen = 0;
445
446 ret = az6007_write(d, req, value, index, NULL, blen);
447 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300448 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300449 goto failed;
450 }
451
452 for (i = 0; i < 15; i++) {
453 msleep(100);
454
455 if (CI_CamReady(ca, slot)) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300456 pr_debug("CAM Ready\n");
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300457 break;
458 }
459 }
460 msleep(5000);
461
462failed:
463 mutex_unlock(&state->ca_mutex);
464 return ret;
465}
466
467static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
468{
469 return 0;
470}
471
472static int az6007_ci_slot_ts_enable(struct dvb_ca_en50221 *ca, int slot)
473{
474 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300475 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300476
477 int ret;
478 u8 req;
479 u16 value;
480 u16 index;
481 int blen;
482
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300483 pr_debug("%s()\n", __func__);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300484 mutex_lock(&state->ca_mutex);
485 req = 0xC7;
486 value = 1;
487 index = 0;
488 blen = 0;
489
490 ret = az6007_write(d, req, value, index, NULL, blen);
491 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300492 pr_warn("usb out operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300493 goto failed;
494 }
495
496failed:
497 mutex_unlock(&state->ca_mutex);
498 return ret;
499}
500
501static int az6007_ci_poll_slot_status(struct dvb_ca_en50221 *ca, int slot, int open)
502{
503 struct dvb_usb_device *d = (struct dvb_usb_device *)ca->data;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300504 struct az6007_device_state *state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300505 int ret;
506 u8 req;
507 u16 value;
508 u16 index;
509 int blen;
510 u8 *b;
511
512 b = kmalloc(12, GFP_KERNEL);
513 if (!b)
514 return -ENOMEM;
515 mutex_lock(&state->ca_mutex);
516
517 req = 0xC5;
518 value = 0;
519 index = 0;
520 blen = 1;
521
522 ret = az6007_read(d, req, value, index, b, blen);
523 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300524 pr_warn("usb in operation failed. (%d)\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300525 ret = -EIO;
526 } else
527 ret = 0;
528
529 if (!ret && b[0] == 1) {
530 ret = DVB_CA_EN50221_POLL_CAM_PRESENT |
531 DVB_CA_EN50221_POLL_CAM_READY;
532 }
533
534 mutex_unlock(&state->ca_mutex);
535 kfree(b);
536 return ret;
537}
538
539
540static void az6007_ci_uninit(struct dvb_usb_device *d)
541{
542 struct az6007_device_state *state;
543
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300544 pr_debug("%s()\n", __func__);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300545
546 if (NULL == d)
547 return;
548
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300549 state = d_to_priv(d);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300550 if (NULL == state)
551 return;
552
553 if (NULL == state->ca.data)
554 return;
555
556 dvb_ca_en50221_release(&state->ca);
557
558 memset(&state->ca, 0, sizeof(state->ca));
559}
560
561
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300562static int az6007_ci_init(struct dvb_usb_adapter *adap)
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300563{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300564 struct dvb_usb_device *d = adap_to_d(adap);
565 struct az6007_device_state *state = adap_to_priv(adap);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300566 int ret;
567
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300568 pr_debug("%s()\n", __func__);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300569
570 mutex_init(&state->ca_mutex);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300571 state->ca.owner = THIS_MODULE;
572 state->ca.read_attribute_mem = az6007_ci_read_attribute_mem;
573 state->ca.write_attribute_mem = az6007_ci_write_attribute_mem;
574 state->ca.read_cam_control = az6007_ci_read_cam_control;
575 state->ca.write_cam_control = az6007_ci_write_cam_control;
576 state->ca.slot_reset = az6007_ci_slot_reset;
577 state->ca.slot_shutdown = az6007_ci_slot_shutdown;
578 state->ca.slot_ts_enable = az6007_ci_slot_ts_enable;
579 state->ca.poll_slot_status = az6007_ci_poll_slot_status;
580 state->ca.data = d;
581
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300582 ret = dvb_ca_en50221_init(&adap->dvb_adap,
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300583 &state->ca,
584 0, /* flags */
585 1);/* n_slots */
586 if (ret != 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300587 pr_err("Cannot initialize CI: Error %d.\n", ret);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300588 memset(&state->ca, 0, sizeof(state->ca));
589 return ret;
590 }
591
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300592 pr_debug("CI initialized.\n");
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300593
594 return 0;
595}
596
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300597static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300598{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300599 struct dvb_usb_device *d = adap_to_d(adap);
600 struct az6007_device_state *st = adap_to_priv(adap);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300601 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300602
603 ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
Alan Cox5a7a5702012-07-24 12:02:46 -0300604 memcpy(mac, st->data, 6);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300605
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300606 if (ret > 0)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300607 pr_debug("%s: mac is %pM\n", __func__, mac);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300608
609 return ret;
610}
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300611
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300612static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
613{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300614 struct az6007_device_state *st = adap_to_priv(adap);
615 struct dvb_usb_device *d = adap_to_d(adap);
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300616
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300617 pr_debug("attaching demod drxk\n");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300618
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300619 adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
620 &d->i2c_adap);
621 if (!adap->fe[0])
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300622 return -EINVAL;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300623
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300624 adap->fe[0]->sec_priv = adap;
625 st->gate_ctrl = adap->fe[0]->ops.i2c_gate_ctrl;
626 adap->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
Mauro Carvalho Chehabda989e02011-07-24 09:25:39 -0300627
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300628 az6007_ci_init(adap);
629
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300630 return 0;
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300631}
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300632
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300633static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
634{
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300635 struct dvb_usb_device *d = adap_to_d(adap);
636
637 pr_debug("attaching tuner mt2063\n");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300638
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300639 /* Attach mt2063 to DVB-C frontend */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300640 if (adap->fe[0]->ops.i2c_gate_ctrl)
641 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 1);
642 if (!dvb_attach(mt2063_attach, adap->fe[0],
Mauro Carvalho Chehab781dacc2012-01-16 18:57:51 -0300643 &az6007_mt2063_config,
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300644 &d->i2c_adap))
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300645 return -EINVAL;
646
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300647 if (adap->fe[0]->ops.i2c_gate_ctrl)
648 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
Mauro Carvalho Chehab1c9a2842011-07-31 10:11:32 -0300649
650 return 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300651}
652
Mauro Carvalho Chehab0ca477e2012-08-05 09:23:17 -0300653static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300654{
Mauro Carvalho Chehab3cb9f4e2012-08-05 09:22:23 -0300655 struct az6007_device_state *state = d_to_priv(d);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300656 int ret;
657
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300658 pr_debug("%s()\n", __func__);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300659
Mauro Carvalho Chehab3cb9f4e2012-08-05 09:22:23 -0300660 if (!state->warm) {
661 mutex_init(&state->mutex);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300662
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300663 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300664 if (ret < 0)
665 return ret;
666 msleep(60);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300667 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300668 if (ret < 0)
669 return ret;
670 msleep(100);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300671 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300672 if (ret < 0)
673 return ret;
674 msleep(20);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300675 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300676 if (ret < 0)
677 return ret;
678
679 msleep(400);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300680 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300681 if (ret < 0)
682 return ret;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200683 msleep(150);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300684 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300685 if (ret < 0)
686 return ret;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200687 msleep(430);
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300688 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300689 if (ret < 0)
690 return ret;
691
Mauro Carvalho Chehab3cb9f4e2012-08-05 09:22:23 -0300692 state->warm = true;
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300693
694 return 0;
695 }
696
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300697 if (!onoff)
698 return 0;
699
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300700 az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
701 az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300702
Mauro Carvalho Chehab81091142011-07-25 11:07:20 -0300703 return 0;
704}
705
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300706/* I2C */
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300707static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
708 int num)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300709{
710 struct dvb_usb_device *d = i2c_get_adapdata(adap);
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300711 struct az6007_device_state *st = d_to_priv(d);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300712 int i, j, len;
713 int ret = 0;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300714 u16 index;
715 u16 value;
716 int length;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300717 u8 req, addr;
Mauro Carvalho Chehab6da34702011-07-21 18:31:14 -0300718
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300719 if (mutex_lock_interruptible(&st->mutex) < 0)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300720 return -EAGAIN;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300721
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300722 for (i = 0; i < num; i++) {
723 addr = msgs[i].addr << 1;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300724 if (((i + 1) < num)
725 && (msgs[i].len == 1)
Mauro Carvalho Chehab916c8142012-08-04 21:39:41 -0300726 && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD)
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300727 && (msgs[i + 1].flags & I2C_M_RD)
728 && (msgs[i].addr == msgs[i + 1].addr)) {
729 /*
730 * A write + read xfer for the same address, where
731 * the first xfer has just 1 byte length.
732 * Need to join both into one operation
733 */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300734 if (az6007_xfer_debug)
735 printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300736 addr, msgs[i].len, msgs[i + 1].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300737 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300738 index = msgs[i].buf[0];
739 value = addr | (1 << 8);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300740 length = 6 + msgs[i + 1].len;
741 len = msgs[i + 1].len;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200742 ret = __az6007_read(d->udev, req, value, index,
743 st->data, length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300744 if (ret >= len) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300745 for (j = 0; j < len; j++)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300746 msgs[i + 1].buf[j] = st->data[j + 5];
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300747 } else
748 ret = -EIO;
749 i++;
750 } else if (!(msgs[i].flags & I2C_M_RD)) {
751 /* write bytes */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300752 if (az6007_xfer_debug)
753 printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n",
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300754 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300755 req = AZ6007_I2C_WR;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300756 index = msgs[i].buf[0];
757 value = addr | (1 << 8);
758 length = msgs[i].len - 1;
759 len = msgs[i].len - 1;
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300760 for (j = 0; j < len; j++)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300761 st->data[j] = msgs[i].buf[j + 1];
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200762 ret = __az6007_write(d->udev, req, value, index,
763 st->data, length);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300764 } else {
765 /* read bytes */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300766 if (az6007_xfer_debug)
767 printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n",
Mauro Carvalho Chehabd20a7f72011-07-23 09:51:12 -0300768 addr, msgs[i].len);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300769 req = AZ6007_I2C_RD;
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300770 index = msgs[i].buf[0];
771 value = addr;
772 length = msgs[i].len + 6;
773 len = msgs[i].len;
Mauro Carvalho Chehab9e5e3092012-01-21 13:52:39 -0200774 ret = __az6007_read(d->udev, req, value, index,
775 st->data, length);
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300776 for (j = 0; j < len; j++)
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300777 msgs[i].buf[j] = st->data[j + 5];
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300778 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300779 if (ret < 0)
780 goto err;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300781 }
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300782err:
Mauro Carvalho Chehaba2c35d32012-01-21 11:19:16 -0300783 mutex_unlock(&st->mutex);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300784
785 if (ret < 0) {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300786 pr_info("%s ERROR: %i\n", __func__, ret);
Mauro Carvalho Chehabcaa1a702011-07-22 10:31:25 -0300787 return ret;
788 }
789 return num;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300790}
791
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300792static u32 az6007_i2c_func(struct i2c_adapter *adapter)
793{
794 return I2C_FUNC_I2C;
795}
796
797static struct i2c_algorithm az6007_i2c_algo = {
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300798 .master_xfer = az6007_i2c_xfer,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300799 .functionality = az6007_i2c_func,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300800};
801
Mauro Carvalho Chehab0ca477e2012-08-05 09:23:17 -0300802static int az6007_identify_state(struct dvb_usb_device *d, const char **name)
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300803{
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300804 int ret;
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300805 u8 *mac;
806
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300807 pr_debug("Identifying az6007 state\n");
808
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300809 mac = kmalloc(6, GFP_ATOMIC);
810 if (!mac)
811 return -ENOMEM;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300812
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300813 /* Try to read the mac address */
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300814 ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300815 if (ret == 6)
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300816 ret = WARM;
Mauro Carvalho Chehab3aecf2c2011-07-25 12:45:16 -0300817 else
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300818 ret = COLD;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300819
Mauro Carvalho Chehab711e1392012-01-21 11:53:18 -0300820 kfree(mac);
821
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300822 if (ret == COLD) {
823 __az6007_write(d->udev, 0x09, 1, 0, NULL, 0);
824 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
825 __az6007_write(d->udev, 0x00, 0, 0, NULL, 0);
Mauro Carvalho Chehabb19280c2012-01-20 18:37:01 -0300826 }
827
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300828 pr_debug("Device is on %s state\n",
829 ret == WARM ? "warm" : "cold");
830 return ret;
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300831}
832
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300833static void az6007_usb_disconnect(struct usb_interface *intf)
834{
835 struct dvb_usb_device *d = usb_get_intfdata(intf);
836 az6007_ci_uninit(d);
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300837 dvb_usbv2_disconnect(intf);
Jose Alberto Reguero962f8f62012-03-04 19:22:05 -0300838}
839
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300840static int az6007_download_firmware(struct dvb_usb_device *d,
841 const struct firmware *fw)
842{
843 pr_debug("Loading az6007 firmware\n");
844
845 return usbv2_cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
846}
847
848/* DVB USB Driver stuff */
849static struct dvb_usb_device_properties az6007_props = {
850 .driver_name = KBUILD_MODNAME,
851 .owner = THIS_MODULE,
852 .firmware = AZ6007_FIRMWARE,
853
854 .adapter_nr = adapter_nr,
855 .size_of_priv = sizeof(struct az6007_device_state),
856 .i2c_algo = &az6007_i2c_algo,
857 .tuner_attach = az6007_tuner_attach,
858 .frontend_attach = az6007_frontend_attach,
859 .streaming_ctrl = az6007_streaming_ctrl,
860 .get_rc_config = az6007_get_rc_config,
861 .read_mac_address = az6007_read_mac_addr,
862 .download_firmware = az6007_download_firmware,
863 .identify_state = az6007_identify_state,
864 .power_ctrl = az6007_power_ctrl,
865 .num_adapters = 1,
866 .adapter = {
867 { .stream = DVB_USB_STREAM_BULK(0x02, 10, 4096), }
868 }
869};
870
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300871static struct usb_device_id az6007_usb_table[] = {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300872 {DVB_USB_DEVICE(USB_VID_AZUREWAVE, USB_PID_AZUREWAVE_6007,
873 &az6007_props, "Azurewave 6007", RC_MAP_EMPTY)},
874 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7,
875 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
876 {DVB_USB_DEVICE(USB_VID_TERRATEC, USB_PID_TERRATEC_H7_2,
877 &az6007_props, "Terratec H7", RC_MAP_NEC_TERRATEC_CINERGY_XS)},
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300878 {0},
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300879};
880
881MODULE_DEVICE_TABLE(usb, az6007_usb_table);
882
Mauro Carvalho Chehab6da74b12012-08-05 09:25:34 -0300883static int az6007_suspend(struct usb_interface *intf, pm_message_t msg)
884{
885 struct dvb_usb_device *d = usb_get_intfdata(intf);
886
887 az6007_ci_uninit(d);
888 return dvb_usbv2_suspend(intf, msg);
889}
890
891static int az6007_resume(struct usb_interface *intf)
892{
893 struct dvb_usb_device *d = usb_get_intfdata(intf);
894 struct dvb_usb_adapter *adap = &d->adapter[0];
895
896 az6007_ci_init(adap);
897 return dvb_usbv2_resume(intf);
898}
899
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300900/* usb specific object needed to register this driver with the usb subsystem */
901static struct usb_driver az6007_usb_driver = {
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300902 .name = KBUILD_MODNAME,
Mauro Carvalho Chehab93b32122011-07-23 10:40:08 -0300903 .id_table = az6007_usb_table,
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300904 .probe = dvb_usbv2_probe,
905 .disconnect = az6007_usb_disconnect,
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300906 .no_dynamic_id = 1,
907 .soft_unbind = 1,
Mauro Carvalho Chehab6da74b12012-08-05 09:25:34 -0300908 /*
909 * FIXME: need to implement reset_resume, likely with
910 * dvb-usb-v2 core support
911 */
912 .suspend = az6007_suspend,
913 .resume = az6007_resume,
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300914};
915
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300916module_usb_driver(az6007_usb_driver);
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300917
918MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
Mauro Carvalho Chehab04e3ece2012-01-21 10:35:12 -0300919MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300920MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300921MODULE_VERSION("2.0");
Mauro Carvalho Chehab71d67632011-07-21 17:46:41 -0300922MODULE_LICENSE("GPL");
Mauro Carvalho Chehab255d52f2012-08-04 16:05:34 -0300923MODULE_FIRMWARE(AZ6007_FIRMWARE);