]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/media/usb/dvb-usb-v2/az6007.c
arm: imx6: defconfig: update tx6 defconfigs
[karo-tx-linux.git] / drivers / media / usb / dvb-usb-v2 / az6007.c
1 /*
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  *
10  * Copyright (c) 2010-2012 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, and converted to use dvb-usb-v2.
13  *
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.
22  */
23
24 #include "drxk.h"
25 #include "mt2063.h"
26 #include "dvb_ca_en50221.h"
27 #include "dvb_usb.h"
28 #include "cypress_firmware.h"
29
30 #define AZ6007_FIRMWARE "dvb-usb-terratec-h7-az6007.fw"
31
32 static int az6007_xfer_debug;
33 module_param_named(xfer_debug, az6007_xfer_debug, int, 0644);
34 MODULE_PARM_DESC(xfer_debug, "Enable xfer debug");
35
36 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
37
38 /* 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
47 #define AZ6007_READ_IR          0xb4
48
49 struct az6007_device_state {
50         struct mutex            mutex;
51         struct mutex            ca_mutex;
52         struct dvb_ca_en50221   ca;
53         unsigned                warm:1;
54         int                     (*gate_ctrl) (struct dvb_frontend *, int);
55         unsigned char           data[4096];
56 };
57
58 static struct drxk_config terratec_h7_drxk = {
59         .adr = 0x29,
60         .parallel_ts = true,
61         .dynamic_clk = true,
62         .single_master = true,
63         .enable_merr_cfg = true,
64         .no_i2c_bridge = false,
65         .chunk_size = 64,
66         .mpeg_out_clk_strength = 0x02,
67         .qam_demod_parameter_count = 2,
68         .microcode_name = "dvb-usb-terratec-h7-drxk.fw",
69 };
70
71 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
72 {
73         struct az6007_device_state *st = fe_to_priv(fe);
74         struct dvb_usb_adapter *adap = fe->sec_priv;
75         int status = 0;
76
77         pr_debug("%s: %s\n", __func__, enable ? "enable" : "disable");
78
79         if (!adap || !st)
80                 return -EINVAL;
81
82         if (enable)
83                 status = st->gate_ctrl(fe, 1);
84         else
85                 status = st->gate_ctrl(fe, 0);
86
87         return status;
88 }
89
90 static struct mt2063_config az6007_mt2063_config = {
91         .tuner_address = 0x60,
92         .refclock = 36125000,
93 };
94
95 static int __az6007_read(struct usb_device *udev, u8 req, u16 value,
96                             u16 index, u8 *b, int blen)
97 {
98         int ret;
99
100         ret = usb_control_msg(udev,
101                               usb_rcvctrlpipe(udev, 0),
102                               req,
103                               USB_TYPE_VENDOR | USB_DIR_IN,
104                               value, index, b, blen, 5000);
105         if (ret < 0) {
106                 pr_warn("usb read operation failed. (%d)\n", ret);
107                 return -EIO;
108         }
109
110         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         }
116
117         return ret;
118 }
119
120 static 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
136 static int __az6007_write(struct usb_device *udev, u8 req, u16 value,
137                              u16 index, u8 *b, int blen)
138 {
139         int ret;
140
141         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         }
147
148         if (blen > 64) {
149                 pr_err("az6007: tried to write %d bytes, but I2C max size is 64 bytes\n",
150                        blen);
151                 return -EOPNOTSUPP;
152         }
153
154         ret = usb_control_msg(udev,
155                               usb_sndctrlpipe(udev, 0),
156                               req,
157                               USB_TYPE_VENDOR | USB_DIR_OUT,
158                               value, index, b, blen, 5000);
159         if (ret != blen) {
160                 pr_err("usb write operation failed. (%d)\n", ret);
161                 return -EIO;
162         }
163
164         return 0;
165 }
166
167 static 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
183 static int az6007_streaming_ctrl(struct dvb_frontend *fe, int onoff)
184 {
185         struct dvb_usb_device *d = fe_to_d(fe);
186
187         pr_debug("%s: %s\n", __func__, onoff ? "enable" : "disable");
188
189         return az6007_write(d, 0xbc, onoff, 0, NULL, 0);
190 }
191
192 #if IS_ENABLED(CONFIG_RC_CORE)
193 /* remote control stuff (does not work with my box) */
194 static int az6007_rc_query(struct dvb_usb_device *d)
195 {
196         struct az6007_device_state *st = d_to_priv(d);
197         unsigned code = 0;
198
199         az6007_read(d, AZ6007_READ_IR, 0, 0, st->data, 10);
200
201         if (st->data[1] == 0x44)
202                 return 0;
203
204         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
212                 code = code << 16 | st->data[3] << 8 | st->data[4];
213
214         rc_keydown(d->rc_dev, code, st->data[5]);
215
216         return 0;
217 }
218
219 static 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
233 static 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;
238         struct az6007_device_state *state = d_to_priv(d);
239
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) {
263                 pr_warn("usb in operation failed. (%d)\n", ret);
264                 ret = -EINVAL;
265         } else {
266                 ret = b[0];
267         }
268
269         mutex_unlock(&state->ca_mutex);
270         kfree(b);
271         return ret;
272 }
273
274 static 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;
280         struct az6007_device_state *state = d_to_priv(d);
281
282         int ret;
283         u8 req;
284         u16 value1;
285         u16 index;
286         int blen;
287
288         pr_debug("%s(), slot %d\n", __func__, slot);
289         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)
300                 pr_warn("usb out operation failed. (%d)\n", ret);
301
302         mutex_unlock(&state->ca_mutex);
303         return ret;
304 }
305
306 static 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;
311         struct az6007_device_state *state = d_to_priv(d);
312
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) {
336                 pr_warn("usb in operation failed. (%d)\n", ret);
337                 ret = -EINVAL;
338         } else {
339                 if (b[0] == 0)
340                         pr_warn("Read CI IO error\n");
341
342                 ret = b[1];
343                 pr_debug("read cam data = %x from 0x%x\n", b[1], value);
344         }
345
346         mutex_unlock(&state->ca_mutex);
347         kfree(b);
348         return ret;
349 }
350
351 static 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;
357         struct az6007_device_state *state = d_to_priv(d);
358
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) {
376                 pr_warn("usb out operation failed. (%d)\n", ret);
377                 goto failed;
378         }
379
380 failed:
381         mutex_unlock(&state->ca_mutex);
382         return ret;
383 }
384
385 static 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) {
407                 pr_warn("usb in operation failed. (%d)\n", ret);
408                 ret = -EIO;
409         } else{
410                 ret = b[0];
411         }
412         kfree(b);
413         return ret;
414 }
415
416 static 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;
419         struct az6007_device_state *state = d_to_priv(d);
420
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) {
436                 pr_warn("usb out operation failed. (%d)\n", ret);
437                 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) {
448                 pr_warn("usb out operation failed. (%d)\n", ret);
449                 goto failed;
450         }
451
452         for (i = 0; i < 15; i++) {
453                 msleep(100);
454
455                 if (CI_CamReady(ca, slot)) {
456                         pr_debug("CAM Ready\n");
457                         break;
458                 }
459         }
460         msleep(5000);
461
462 failed:
463         mutex_unlock(&state->ca_mutex);
464         return ret;
465 }
466
467 static int az6007_ci_slot_shutdown(struct dvb_ca_en50221 *ca, int slot)
468 {
469         return 0;
470 }
471
472 static 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;
475         struct az6007_device_state *state = d_to_priv(d);
476
477         int ret;
478         u8 req;
479         u16 value;
480         u16 index;
481         int blen;
482
483         pr_debug("%s()\n", __func__);
484         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) {
492                 pr_warn("usb out operation failed. (%d)\n", ret);
493                 goto failed;
494         }
495
496 failed:
497         mutex_unlock(&state->ca_mutex);
498         return ret;
499 }
500
501 static 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;
504         struct az6007_device_state *state = d_to_priv(d);
505         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) {
524                 pr_warn("usb in operation failed. (%d)\n", ret);
525                 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
540 static void az6007_ci_uninit(struct dvb_usb_device *d)
541 {
542         struct az6007_device_state *state;
543
544         pr_debug("%s()\n", __func__);
545
546         if (NULL == d)
547                 return;
548
549         state = d_to_priv(d);
550         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
562 static int az6007_ci_init(struct dvb_usb_adapter *adap)
563 {
564         struct dvb_usb_device *d = adap_to_d(adap);
565         struct az6007_device_state *state = adap_to_priv(adap);
566         int ret;
567
568         pr_debug("%s()\n", __func__);
569
570         mutex_init(&state->ca_mutex);
571         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
582         ret = dvb_ca_en50221_init(&adap->dvb_adap,
583                                   &state->ca,
584                                   0, /* flags */
585                                   1);/* n_slots */
586         if (ret != 0) {
587                 pr_err("Cannot initialize CI: Error %d.\n", ret);
588                 memset(&state->ca, 0, sizeof(state->ca));
589                 return ret;
590         }
591
592         pr_debug("CI initialized.\n");
593
594         return 0;
595 }
596
597 static int az6007_read_mac_addr(struct dvb_usb_adapter *adap, u8 mac[6])
598 {
599         struct dvb_usb_device *d = adap_to_d(adap);
600         struct az6007_device_state *st = adap_to_priv(adap);
601         int ret;
602
603         ret = az6007_read(d, AZ6007_READ_DATA, 6, 0, st->data, 6);
604         memcpy(mac, st->data, 6);
605
606         if (ret > 0)
607                 pr_debug("%s: mac is %pM\n", __func__, mac);
608
609         return ret;
610 }
611
612 static int az6007_frontend_attach(struct dvb_usb_adapter *adap)
613 {
614         struct az6007_device_state *st = adap_to_priv(adap);
615         struct dvb_usb_device *d = adap_to_d(adap);
616
617         pr_debug("attaching demod drxk\n");
618
619         adap->fe[0] = dvb_attach(drxk_attach, &terratec_h7_drxk,
620                                  &d->i2c_adap);
621         if (!adap->fe[0])
622                 return -EINVAL;
623
624         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;
627
628         az6007_ci_init(adap);
629
630         return 0;
631 }
632
633 static int az6007_tuner_attach(struct dvb_usb_adapter *adap)
634 {
635         struct dvb_usb_device *d = adap_to_d(adap);
636
637         pr_debug("attaching tuner mt2063\n");
638
639         /* Attach mt2063 to DVB-C frontend */
640         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],
643                         &az6007_mt2063_config,
644                         &d->i2c_adap))
645                 return -EINVAL;
646
647         if (adap->fe[0]->ops.i2c_gate_ctrl)
648                 adap->fe[0]->ops.i2c_gate_ctrl(adap->fe[0], 0);
649
650         return 0;
651 }
652
653 static int az6007_power_ctrl(struct dvb_usb_device *d, int onoff)
654 {
655         struct az6007_device_state *state = d_to_priv(d);
656         int ret;
657
658         pr_debug("%s()\n", __func__);
659
660         if (!state->warm) {
661                 mutex_init(&state->mutex);
662
663                 ret = az6007_write(d, AZ6007_POWER, 0, 2, NULL, 0);
664                 if (ret < 0)
665                         return ret;
666                 msleep(60);
667                 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
668                 if (ret < 0)
669                         return ret;
670                 msleep(100);
671                 ret = az6007_write(d, AZ6007_POWER, 1, 3, NULL, 0);
672                 if (ret < 0)
673                         return ret;
674                 msleep(20);
675                 ret = az6007_write(d, AZ6007_POWER, 1, 4, NULL, 0);
676                 if (ret < 0)
677                         return ret;
678
679                 msleep(400);
680                 ret = az6007_write(d, FX2_SCON1, 0, 3, NULL, 0);
681                 if (ret < 0)
682                         return ret;
683                 msleep(150);
684                 ret = az6007_write(d, FX2_SCON1, 1, 3, NULL, 0);
685                 if (ret < 0)
686                         return ret;
687                 msleep(430);
688                 ret = az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
689                 if (ret < 0)
690                         return ret;
691
692                 state->warm = true;
693
694                 return 0;
695         }
696
697         if (!onoff)
698                 return 0;
699
700         az6007_write(d, AZ6007_POWER, 0, 0, NULL, 0);
701         az6007_write(d, AZ6007_TS_THROUGH, 0, 0, NULL, 0);
702
703         return 0;
704 }
705
706 /* I2C */
707 static int az6007_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg msgs[],
708                            int num)
709 {
710         struct dvb_usb_device *d = i2c_get_adapdata(adap);
711         struct az6007_device_state *st = d_to_priv(d);
712         int i, j, len;
713         int ret = 0;
714         u16 index;
715         u16 value;
716         int length;
717         u8 req, addr;
718
719         if (mutex_lock_interruptible(&st->mutex) < 0)
720                 return -EAGAIN;
721
722         for (i = 0; i < num; i++) {
723                 addr = msgs[i].addr << 1;
724                 if (((i + 1) < num)
725                     && (msgs[i].len == 1)
726                     && ((msgs[i].flags & I2C_M_RD) != I2C_M_RD)
727                     && (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                          */
734                         if (az6007_xfer_debug)
735                                 printk(KERN_DEBUG "az6007: I2C W/R addr=0x%x len=%d/%d\n",
736                                        addr, msgs[i].len, msgs[i + 1].len);
737                         req = AZ6007_I2C_RD;
738                         index = msgs[i].buf[0];
739                         value = addr | (1 << 8);
740                         length = 6 + msgs[i + 1].len;
741                         len = msgs[i + 1].len;
742                         ret = __az6007_read(d->udev, req, value, index,
743                                             st->data, length);
744                         if (ret >= len) {
745                                 for (j = 0; j < len; j++)
746                                         msgs[i + 1].buf[j] = st->data[j + 5];
747                         } else
748                                 ret = -EIO;
749                         i++;
750                 } else if (!(msgs[i].flags & I2C_M_RD)) {
751                         /* write bytes */
752                         if (az6007_xfer_debug)
753                                 printk(KERN_DEBUG "az6007: I2C W addr=0x%x len=%d\n",
754                                        addr, msgs[i].len);
755                         req = AZ6007_I2C_WR;
756                         index = msgs[i].buf[0];
757                         value = addr | (1 << 8);
758                         length = msgs[i].len - 1;
759                         len = msgs[i].len - 1;
760                         for (j = 0; j < len; j++)
761                                 st->data[j] = msgs[i].buf[j + 1];
762                         ret =  __az6007_write(d->udev, req, value, index,
763                                               st->data, length);
764                 } else {
765                         /* read bytes */
766                         if (az6007_xfer_debug)
767                                 printk(KERN_DEBUG "az6007: I2C R addr=0x%x len=%d\n",
768                                        addr, msgs[i].len);
769                         req = AZ6007_I2C_RD;
770                         index = msgs[i].buf[0];
771                         value = addr;
772                         length = msgs[i].len + 6;
773                         len = msgs[i].len;
774                         ret = __az6007_read(d->udev, req, value, index,
775                                             st->data, length);
776                         for (j = 0; j < len; j++)
777                                 msgs[i].buf[j] = st->data[j + 5];
778                 }
779                 if (ret < 0)
780                         goto err;
781         }
782 err:
783         mutex_unlock(&st->mutex);
784
785         if (ret < 0) {
786                 pr_info("%s ERROR: %i\n", __func__, ret);
787                 return ret;
788         }
789         return num;
790 }
791
792 static u32 az6007_i2c_func(struct i2c_adapter *adapter)
793 {
794         return I2C_FUNC_I2C;
795 }
796
797 static struct i2c_algorithm az6007_i2c_algo = {
798         .master_xfer = az6007_i2c_xfer,
799         .functionality = az6007_i2c_func,
800 };
801
802 static int az6007_identify_state(struct dvb_usb_device *d, const char **name)
803 {
804         int ret;
805         u8 *mac;
806
807         pr_debug("Identifying az6007 state\n");
808
809         mac = kmalloc(6, GFP_ATOMIC);
810         if (!mac)
811                 return -ENOMEM;
812
813         /* Try to read the mac address */
814         ret = __az6007_read(d->udev, AZ6007_READ_DATA, 6, 0, mac, 6);
815         if (ret == 6)
816                 ret = WARM;
817         else
818                 ret = COLD;
819
820         kfree(mac);
821
822         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);
826         }
827
828         pr_debug("Device is on %s state\n",
829                  ret == WARM ? "warm" : "cold");
830         return ret;
831 }
832
833 static void az6007_usb_disconnect(struct usb_interface *intf)
834 {
835         struct dvb_usb_device *d = usb_get_intfdata(intf);
836         az6007_ci_uninit(d);
837         dvb_usbv2_disconnect(intf);
838 }
839
840 static 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 cypress_load_firmware(d->udev, fw, CYPRESS_FX2);
846 }
847
848 /* DVB USB Driver stuff */
849 static 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
871 static struct usb_device_id az6007_usb_table[] = {
872         {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)},
878         {0},
879 };
880
881 MODULE_DEVICE_TABLE(usb, az6007_usb_table);
882
883 static 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
891 static 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
900 /* usb specific object needed to register this driver with the usb subsystem */
901 static struct usb_driver az6007_usb_driver = {
902         .name           = KBUILD_MODNAME,
903         .id_table       = az6007_usb_table,
904         .probe          = dvb_usbv2_probe,
905         .disconnect     = az6007_usb_disconnect,
906         .no_dynamic_id  = 1,
907         .soft_unbind    = 1,
908         /*
909          * FIXME: need to implement reset_resume, likely with
910          * dvb-usb-v2 core support
911          */
912         .suspend        = az6007_suspend,
913         .resume         = az6007_resume,
914 };
915
916 module_usb_driver(az6007_usb_driver);
917
918 MODULE_AUTHOR("Henry Wang <Henry.wang@AzureWave.com>");
919 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@redhat.com>");
920 MODULE_DESCRIPTION("Driver for AzureWave 6007 DVB-C/T USB2.0 and clones");
921 MODULE_VERSION("2.0");
922 MODULE_LICENSE("GPL");
923 MODULE_FIRMWARE(AZ6007_FIRMWARE);