]> Pileus Git - ~andy/linux/blob - drivers/media/video/em28xx/em28xx-dvb.c
[media] em28xx: support for 2304:0242 PCTV QuatroStick (510e)
[~andy/linux] / drivers / media / video / em28xx / em28xx-dvb.c
1 /*
2  DVB device driver for em28xx
3
4  (c) 2008-2011 Mauro Carvalho Chehab <mchehab@infradead.org>
5
6  (c) 2008 Devin Heitmueller <devin.heitmueller@gmail.com>
7         - Fixes for the driver to properly work with HVR-950
8         - Fixes for the driver to properly work with Pinnacle PCTV HD Pro Stick
9         - Fixes for the driver to properly work with AMD ATI TV Wonder HD 600
10
11  (c) 2008 Aidan Thornton <makosoft@googlemail.com>
12
13  Based on cx88-dvb, saa7134-dvb and videobuf-dvb originally written by:
14         (c) 2004, 2005 Chris Pascoe <c.pascoe@itee.uq.edu.au>
15         (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
16
17  This program is free software; you can redistribute it and/or modify
18  it under the terms of the GNU General Public License as published by
19  the Free Software Foundation; either version 2 of the License.
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/usb.h>
25
26 #include "em28xx.h"
27 #include <media/v4l2-common.h>
28 #include <media/videobuf-vmalloc.h>
29 #include <media/tuner.h>
30 #include "tuner-simple.h"
31
32 #include "lgdt330x.h"
33 #include "lgdt3305.h"
34 #include "zl10353.h"
35 #include "s5h1409.h"
36 #include "mt352.h"
37 #include "mt352_priv.h" /* FIXME */
38 #include "tda1002x.h"
39 #include "tda18271.h"
40 #include "s921.h"
41 #include "drxd.h"
42 #include "cxd2820r.h"
43 #include "tda18271c2dd.h"
44 #include "drxk.h"
45 #include "tda10071.h"
46 #include "a8293.h"
47 #include "qt1010.h"
48
49 MODULE_DESCRIPTION("driver for em28xx based DVB cards");
50 MODULE_AUTHOR("Mauro Carvalho Chehab <mchehab@infradead.org>");
51 MODULE_LICENSE("GPL");
52
53 static unsigned int debug;
54 module_param(debug, int, 0644);
55 MODULE_PARM_DESC(debug, "enable debug messages [dvb]");
56
57 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
58
59 #define dprintk(level, fmt, arg...) do {                        \
60 if (debug >= level)                                             \
61         printk(KERN_DEBUG "%s/2-dvb: " fmt, dev->name, ## arg); \
62 } while (0)
63
64 struct em28xx_dvb {
65         struct dvb_frontend        *fe[2];
66
67         /* feed count management */
68         struct mutex               lock;
69         int                        nfeeds;
70
71         /* general boilerplate stuff */
72         struct dvb_adapter         adapter;
73         struct dvb_demux           demux;
74         struct dmxdev              dmxdev;
75         struct dmx_frontend        fe_hw;
76         struct dmx_frontend        fe_mem;
77         struct dvb_net             net;
78
79         /* Due to DRX-K - probably need changes */
80         int (*gate_ctrl)(struct dvb_frontend *, int);
81         struct semaphore      pll_mutex;
82         bool                    dont_attach_fe1;
83 };
84
85
86 static inline void print_err_status(struct em28xx *dev,
87                                      int packet, int status)
88 {
89         char *errmsg = "Unknown";
90
91         switch (status) {
92         case -ENOENT:
93                 errmsg = "unlinked synchronuously";
94                 break;
95         case -ECONNRESET:
96                 errmsg = "unlinked asynchronuously";
97                 break;
98         case -ENOSR:
99                 errmsg = "Buffer error (overrun)";
100                 break;
101         case -EPIPE:
102                 errmsg = "Stalled (device not responding)";
103                 break;
104         case -EOVERFLOW:
105                 errmsg = "Babble (bad cable?)";
106                 break;
107         case -EPROTO:
108                 errmsg = "Bit-stuff error (bad cable?)";
109                 break;
110         case -EILSEQ:
111                 errmsg = "CRC/Timeout (could be anything)";
112                 break;
113         case -ETIME:
114                 errmsg = "Device does not respond";
115                 break;
116         }
117         if (packet < 0) {
118                 dprintk(1, "URB status %d [%s].\n", status, errmsg);
119         } else {
120                 dprintk(1, "URB packet %d, status %d [%s].\n",
121                         packet, status, errmsg);
122         }
123 }
124
125 static inline int em28xx_dvb_isoc_copy(struct em28xx *dev, struct urb *urb)
126 {
127         int i;
128
129         if (!dev)
130                 return 0;
131
132         if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))
133                 return 0;
134
135         if (urb->status < 0) {
136                 print_err_status(dev, -1, urb->status);
137                 if (urb->status == -ENOENT)
138                         return 0;
139         }
140
141         for (i = 0; i < urb->number_of_packets; i++) {
142                 int status = urb->iso_frame_desc[i].status;
143
144                 if (status < 0) {
145                         print_err_status(dev, i, status);
146                         if (urb->iso_frame_desc[i].status != -EPROTO)
147                                 continue;
148                 }
149
150                 dvb_dmx_swfilter(&dev->dvb->demux, urb->transfer_buffer +
151                                  urb->iso_frame_desc[i].offset,
152                                  urb->iso_frame_desc[i].actual_length);
153         }
154
155         return 0;
156 }
157
158 static int em28xx_start_streaming(struct em28xx_dvb *dvb)
159 {
160         int rc;
161         struct em28xx *dev = dvb->adapter.priv;
162         int max_dvb_packet_size;
163
164         usb_set_interface(dev->udev, 0, dev->dvb_alt);
165         rc = em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
166         if (rc < 0)
167                 return rc;
168
169         max_dvb_packet_size = dev->dvb_max_pkt_size;
170         if (max_dvb_packet_size < 0)
171                 return max_dvb_packet_size;
172         dprintk(1, "Using %d buffers each with %d x %d bytes\n",
173                 EM28XX_DVB_NUM_BUFS,
174                 EM28XX_DVB_MAX_PACKETS,
175                 max_dvb_packet_size);
176
177         return em28xx_init_isoc(dev, EM28XX_DIGITAL_MODE,
178                                 EM28XX_DVB_MAX_PACKETS, EM28XX_DVB_NUM_BUFS,
179                                 max_dvb_packet_size, em28xx_dvb_isoc_copy);
180 }
181
182 static int em28xx_stop_streaming(struct em28xx_dvb *dvb)
183 {
184         struct em28xx *dev = dvb->adapter.priv;
185
186         em28xx_capture_start(dev, 0);
187
188         em28xx_set_mode(dev, EM28XX_SUSPEND);
189
190         return 0;
191 }
192
193 static int em28xx_start_feed(struct dvb_demux_feed *feed)
194 {
195         struct dvb_demux *demux  = feed->demux;
196         struct em28xx_dvb *dvb = demux->priv;
197         int rc, ret;
198
199         if (!demux->dmx.frontend)
200                 return -EINVAL;
201
202         mutex_lock(&dvb->lock);
203         dvb->nfeeds++;
204         rc = dvb->nfeeds;
205
206         if (dvb->nfeeds == 1) {
207                 ret = em28xx_start_streaming(dvb);
208                 if (ret < 0)
209                         rc = ret;
210         }
211
212         mutex_unlock(&dvb->lock);
213         return rc;
214 }
215
216 static int em28xx_stop_feed(struct dvb_demux_feed *feed)
217 {
218         struct dvb_demux *demux  = feed->demux;
219         struct em28xx_dvb *dvb = demux->priv;
220         int err = 0;
221
222         mutex_lock(&dvb->lock);
223         dvb->nfeeds--;
224
225         if (0 == dvb->nfeeds)
226                 err = em28xx_stop_streaming(dvb);
227
228         mutex_unlock(&dvb->lock);
229         return err;
230 }
231
232
233
234 /* ------------------------------------------------------------------ */
235 static int em28xx_dvb_bus_ctrl(struct dvb_frontend *fe, int acquire)
236 {
237         struct em28xx *dev = fe->dvb->priv;
238
239         if (acquire)
240                 return em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
241         else
242                 return em28xx_set_mode(dev, EM28XX_SUSPEND);
243 }
244
245 /* ------------------------------------------------------------------ */
246
247 static struct lgdt330x_config em2880_lgdt3303_dev = {
248         .demod_address = 0x0e,
249         .demod_chip = LGDT3303,
250 };
251
252 static struct lgdt3305_config em2870_lgdt3304_dev = {
253         .i2c_addr           = 0x0e,
254         .demod_chip         = LGDT3304,
255         .spectral_inversion = 1,
256         .deny_i2c_rptr      = 1,
257         .mpeg_mode          = LGDT3305_MPEG_PARALLEL,
258         .tpclk_edge         = LGDT3305_TPCLK_FALLING_EDGE,
259         .tpvalid_polarity   = LGDT3305_TP_VALID_HIGH,
260         .vsb_if_khz         = 3250,
261         .qam_if_khz         = 4000,
262 };
263
264 static struct s921_config sharp_isdbt = {
265         .demod_address = 0x30 >> 1
266 };
267
268 static struct zl10353_config em28xx_zl10353_with_xc3028 = {
269         .demod_address = (0x1e >> 1),
270         .no_tuner = 1,
271         .parallel_ts = 1,
272         .if2 = 45600,
273 };
274
275 static struct s5h1409_config em28xx_s5h1409_with_xc3028 = {
276         .demod_address = 0x32 >> 1,
277         .output_mode   = S5H1409_PARALLEL_OUTPUT,
278         .gpio          = S5H1409_GPIO_OFF,
279         .inversion     = S5H1409_INVERSION_OFF,
280         .status_mode   = S5H1409_DEMODLOCKING,
281         .mpeg_timing   = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
282 };
283
284 static struct tda18271_std_map kworld_a340_std_map = {
285         .atsc_6   = { .if_freq = 3250, .agc_mode = 3, .std = 0,
286                       .if_lvl = 1, .rfagc_top = 0x37, },
287         .qam_6    = { .if_freq = 4000, .agc_mode = 3, .std = 1,
288                       .if_lvl = 1, .rfagc_top = 0x37, },
289 };
290
291 static struct tda18271_config kworld_a340_config = {
292         .std_map           = &kworld_a340_std_map,
293 };
294
295 static struct zl10353_config em28xx_zl10353_xc3028_no_i2c_gate = {
296         .demod_address = (0x1e >> 1),
297         .no_tuner = 1,
298         .disable_i2c_gate_ctrl = 1,
299         .parallel_ts = 1,
300         .if2 = 45600,
301 };
302
303 static struct drxd_config em28xx_drxd = {
304         .demod_address = 0x70,
305         .demod_revision = 0xa2,
306         .pll_type = DRXD_PLL_NONE,
307         .clock = 12000,
308         .insert_rs_byte = 1,
309         .IF = 42800000,
310         .disable_i2c_gate_ctrl = 1,
311 };
312
313 struct drxk_config terratec_h5_drxk = {
314         .adr = 0x29,
315         .single_master = 1,
316         .no_i2c_bridge = 1,
317         .microcode_name = "dvb-usb-terratec-h5-drxk.fw",
318 };
319
320 struct drxk_config hauppauge_930c_drxk = {
321         .adr = 0x29,
322         .single_master = 1,
323         .no_i2c_bridge = 1,
324         .microcode_name = "dvb-usb-hauppauge-hvr930c-drxk.fw",
325         .chunk_size = 56,
326 };
327
328 struct drxk_config maxmedia_ub425_tc_drxk = {
329         .adr = 0x29,
330         .single_master = 1,
331         .no_i2c_bridge = 1,
332 };
333
334 struct drxk_config pctv_520e_drxk = {
335         .adr = 0x29,
336         .single_master = 1,
337         .microcode_name = "dvb-demod-drxk-pctv.fw",
338         .chunk_size = 58,
339 };
340
341 struct drxk_config pctv_520e_drxk = {
342         .adr = 0x29,
343         .single_master = 1,
344         .microcode_name = "dvb-demod-drxk-pctv.fw",
345         .chunk_size = 58,
346 };
347
348 static int drxk_gate_ctrl(struct dvb_frontend *fe, int enable)
349 {
350         struct em28xx_dvb *dvb = fe->sec_priv;
351         int status;
352
353         if (!dvb)
354                 return -EINVAL;
355
356         if (enable) {
357                 down(&dvb->pll_mutex);
358                 status = dvb->gate_ctrl(fe, 1);
359         } else {
360                 status = dvb->gate_ctrl(fe, 0);
361                 up(&dvb->pll_mutex);
362         }
363         return status;
364 }
365
366 static void hauppauge_hvr930c_init(struct em28xx *dev)
367 {
368         int i;
369
370         struct em28xx_reg_seq hauppauge_hvr930c_init[] = {
371                 {EM2874_R80_GPIO,       0xff,   0xff,   0x65},
372                 {EM2874_R80_GPIO,       0xfb,   0xff,   0x32},
373                 {EM2874_R80_GPIO,       0xff,   0xff,   0xb8},
374                 { -1,                   -1,     -1,     -1},
375         };
376         struct em28xx_reg_seq hauppauge_hvr930c_end[] = {
377                 {EM2874_R80_GPIO,       0xef,   0xff,   0x01},
378                 {EM2874_R80_GPIO,       0xaf,   0xff,   0x65},
379                 {EM2874_R80_GPIO,       0xef,   0xff,   0x76},
380                 {EM2874_R80_GPIO,       0xef,   0xff,   0x01},
381                 {EM2874_R80_GPIO,       0xcf,   0xff,   0x0b},
382                 {EM2874_R80_GPIO,       0xef,   0xff,   0x40},
383
384                 {EM2874_R80_GPIO,       0xcf,   0xff,   0x65},
385                 {EM2874_R80_GPIO,       0xef,   0xff,   0x65},
386                 {EM2874_R80_GPIO,       0xcf,   0xff,   0x0b},
387                 {EM2874_R80_GPIO,       0xef,   0xff,   0x65},
388
389                 { -1,                   -1,     -1,     -1},
390         };
391
392         struct {
393                 unsigned char r[4];
394                 int len;
395         } regs[] = {
396                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
397                 {{ 0x01, 0x02 }, 2},
398                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
399                 {{ 0x01, 0x00 }, 2},
400                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
401                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
402                 {{ 0x01, 0x00 }, 2},
403                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
404                 {{ 0x04, 0x00 }, 2},
405                 {{ 0x00, 0x04 }, 2},
406                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
407                 {{ 0x04, 0x14 }, 2},
408                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
409         };
410
411         em28xx_gpio_set(dev, hauppauge_hvr930c_init);
412         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
413         msleep(10);
414         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
415         msleep(10);
416
417         dev->i2c_client.addr = 0x82 >> 1;
418
419         for (i = 0; i < ARRAY_SIZE(regs); i++)
420                 i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
421         em28xx_gpio_set(dev, hauppauge_hvr930c_end);
422
423         msleep(100);
424
425         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x44);
426         msleep(30);
427
428         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
429         msleep(10);
430
431 }
432
433 static void terratec_h5_init(struct em28xx *dev)
434 {
435         int i;
436         struct em28xx_reg_seq terratec_h5_init[] = {
437                 {EM28XX_R08_GPIO,       0xff,   0xff,   10},
438                 {EM2874_R80_GPIO,       0xf6,   0xff,   100},
439                 {EM2874_R80_GPIO,       0xf2,   0xff,   50},
440                 {EM2874_R80_GPIO,       0xf6,   0xff,   100},
441                 { -1,                   -1,     -1,     -1},
442         };
443         struct em28xx_reg_seq terratec_h5_end[] = {
444                 {EM2874_R80_GPIO,       0xe6,   0xff,   100},
445                 {EM2874_R80_GPIO,       0xa6,   0xff,   50},
446                 {EM2874_R80_GPIO,       0xe6,   0xff,   100},
447                 { -1,                   -1,     -1,     -1},
448         };
449         struct {
450                 unsigned char r[4];
451                 int len;
452         } regs[] = {
453                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
454                 {{ 0x01, 0x02 }, 2},
455                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
456                 {{ 0x01, 0x00 }, 2},
457                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
458                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
459                 {{ 0x01, 0x00 }, 2},
460                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
461                 {{ 0x04, 0x00 }, 2},
462                 {{ 0x00, 0x04 }, 2},
463                 {{ 0x00, 0x04, 0x00, 0x0a }, 4},
464                 {{ 0x04, 0x14 }, 2},
465                 {{ 0x04, 0x14, 0x00, 0x00 }, 4},
466         };
467
468         em28xx_gpio_set(dev, terratec_h5_init);
469         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x40);
470         msleep(10);
471         em28xx_write_reg(dev, EM28XX_R06_I2C_CLK, 0x45);
472         msleep(10);
473
474         dev->i2c_client.addr = 0x82 >> 1;
475
476         for (i = 0; i < ARRAY_SIZE(regs); i++)
477                 i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
478         em28xx_gpio_set(dev, terratec_h5_end);
479 };
480
481 static void pctv_520e_init(struct em28xx *dev)
482 {
483         /*
484          * Init TDA8295(?) analog demodulator. Looks like I2C traffic to
485          * digital demodulator and tuner are routed via TDA8295.
486          */
487         int i;
488         struct {
489                 unsigned char r[4];
490                 int len;
491         } regs[] = {
492                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
493                 {{ 0x01, 0x02 }, 2},
494                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
495                 {{ 0x01, 0x00 }, 2},
496                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
497                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
498                 {{ 0x01, 0x00 }, 2},
499                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
500         };
501
502         dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
503
504         for (i = 0; i < ARRAY_SIZE(regs); i++)
505                 i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
506 };
507
508 static void pctv_520e_init(struct em28xx *dev)
509 {
510         /*
511          * Init TDA8295(?) analog demodulator. Looks like I2C traffic to
512          * digital demodulator and tuner are routed via TDA8295.
513          */
514         int i;
515         struct {
516                 unsigned char r[4];
517                 int len;
518         } regs[] = {
519                 {{ 0x06, 0x02, 0x00, 0x31 }, 4},
520                 {{ 0x01, 0x02 }, 2},
521                 {{ 0x01, 0x02, 0x00, 0xc6 }, 4},
522                 {{ 0x01, 0x00 }, 2},
523                 {{ 0x01, 0x00, 0xff, 0xaf }, 4},
524                 {{ 0x01, 0x00, 0x03, 0xa0 }, 4},
525                 {{ 0x01, 0x00 }, 2},
526                 {{ 0x01, 0x00, 0x73, 0xaf }, 4},
527         };
528
529         dev->i2c_client.addr = 0x82 >> 1; /* 0x41 */
530
531         for (i = 0; i < ARRAY_SIZE(regs); i++)
532                 i2c_master_send(&dev->i2c_client, regs[i].r, regs[i].len);
533 };
534
535 static int em28xx_mt352_terratec_xs_init(struct dvb_frontend *fe)
536 {
537         /* Values extracted from a USB trace of the Terratec Windows driver */
538         static u8 clock_config[]   = { CLOCK_CTL,  0x38, 0x2c };
539         static u8 reset[]          = { RESET,      0x80 };
540         static u8 adc_ctl_1_cfg[]  = { ADC_CTL_1,  0x40 };
541         static u8 agc_cfg[]        = { AGC_TARGET, 0x28, 0xa0 };
542         static u8 input_freq_cfg[] = { INPUT_FREQ_1, 0x31, 0xb8 };
543         static u8 rs_err_cfg[]     = { RS_ERR_PER_1, 0x00, 0x4d };
544         static u8 capt_range_cfg[] = { CAPT_RANGE, 0x32 };
545         static u8 trl_nom_cfg[]    = { TRL_NOMINAL_RATE_1, 0x64, 0x00 };
546         static u8 tps_given_cfg[]  = { TPS_GIVEN_1, 0x40, 0x80, 0x50 };
547         static u8 tuner_go[]       = { TUNER_GO, 0x01};
548
549         mt352_write(fe, clock_config,   sizeof(clock_config));
550         udelay(200);
551         mt352_write(fe, reset,          sizeof(reset));
552         mt352_write(fe, adc_ctl_1_cfg,  sizeof(adc_ctl_1_cfg));
553         mt352_write(fe, agc_cfg,        sizeof(agc_cfg));
554         mt352_write(fe, input_freq_cfg, sizeof(input_freq_cfg));
555         mt352_write(fe, rs_err_cfg,     sizeof(rs_err_cfg));
556         mt352_write(fe, capt_range_cfg, sizeof(capt_range_cfg));
557         mt352_write(fe, trl_nom_cfg,    sizeof(trl_nom_cfg));
558         mt352_write(fe, tps_given_cfg,  sizeof(tps_given_cfg));
559         mt352_write(fe, tuner_go,       sizeof(tuner_go));
560         return 0;
561 }
562
563 static struct mt352_config terratec_xs_mt352_cfg = {
564         .demod_address = (0x1e >> 1),
565         .no_tuner = 1,
566         .if2 = 45600,
567         .demod_init = em28xx_mt352_terratec_xs_init,
568 };
569
570 static struct tda10023_config em28xx_tda10023_config = {
571         .demod_address = 0x0c,
572         .invert = 1,
573 };
574
575 static struct cxd2820r_config em28xx_cxd2820r_config = {
576         .i2c_address = (0xd8 >> 1),
577         .ts_mode = CXD2820R_TS_SERIAL,
578
579         /* enable LNA for DVB-T2 and DVB-C */
580         .gpio_dvbt2[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
581         .gpio_dvbc[0] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | CXD2820R_GPIO_L,
582 };
583
584 static struct tda18271_config em28xx_cxd2820r_tda18271_config = {
585         .output_opt = TDA18271_OUTPUT_LT_OFF,
586         .gate = TDA18271_GATE_DIGITAL,
587 };
588
589 static const struct tda10071_config em28xx_tda10071_config = {
590         .i2c_address = 0x55, /* (0xaa >> 1) */
591         .i2c_wr_max = 64,
592         .ts_mode = TDA10071_TS_SERIAL,
593         .spec_inv = 0,
594         .xtal = 40444000, /* 40.444 MHz */
595         .pll_multiplier = 20,
596 };
597
598 static const struct a8293_config em28xx_a8293_config = {
599         .i2c_addr = 0x08, /* (0x10 >> 1) */
600 };
601
602 static struct zl10353_config em28xx_zl10353_no_i2c_gate_dev = {
603         .demod_address = (0x1e >> 1),
604         .disable_i2c_gate_ctrl = 1,
605         .no_tuner = 1,
606         .parallel_ts = 1,
607 };
608 static struct qt1010_config em28xx_qt1010_config = {
609         .i2c_address = 0x62
610
611 };
612
613 /* ------------------------------------------------------------------ */
614
615 static int em28xx_attach_xc3028(u8 addr, struct em28xx *dev)
616 {
617         struct dvb_frontend *fe;
618         struct xc2028_config cfg;
619
620         memset(&cfg, 0, sizeof(cfg));
621         cfg.i2c_adap  = &dev->i2c_adap;
622         cfg.i2c_addr  = addr;
623
624         if (!dev->dvb->fe[0]) {
625                 em28xx_errdev("/2: dvb frontend not attached. "
626                                 "Can't attach xc3028\n");
627                 return -EINVAL;
628         }
629
630         fe = dvb_attach(xc2028_attach, dev->dvb->fe[0], &cfg);
631         if (!fe) {
632                 em28xx_errdev("/2: xc3028 attach failed\n");
633                 dvb_frontend_detach(dev->dvb->fe[0]);
634                 dev->dvb->fe[0] = NULL;
635                 return -EINVAL;
636         }
637
638         em28xx_info("%s/2: xc3028 attached\n", dev->name);
639
640         return 0;
641 }
642
643 /* ------------------------------------------------------------------ */
644
645 static int em28xx_register_dvb(struct em28xx_dvb *dvb, struct module *module,
646                                struct em28xx *dev, struct device *device)
647 {
648         int result;
649
650         mutex_init(&dvb->lock);
651
652         /* register adapter */
653         result = dvb_register_adapter(&dvb->adapter, dev->name, module, device,
654                                       adapter_nr);
655         if (result < 0) {
656                 printk(KERN_WARNING "%s: dvb_register_adapter failed (errno = %d)\n",
657                        dev->name, result);
658                 goto fail_adapter;
659         }
660
661         /* Ensure all frontends negotiate bus access */
662         dvb->fe[0]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
663         if (dvb->fe[1])
664                 dvb->fe[1]->ops.ts_bus_ctrl = em28xx_dvb_bus_ctrl;
665
666         dvb->adapter.priv = dev;
667
668         /* register frontend */
669         result = dvb_register_frontend(&dvb->adapter, dvb->fe[0]);
670         if (result < 0) {
671                 printk(KERN_WARNING "%s: dvb_register_frontend failed (errno = %d)\n",
672                        dev->name, result);
673                 goto fail_frontend0;
674         }
675
676         /* register 2nd frontend */
677         if (dvb->fe[1]) {
678                 result = dvb_register_frontend(&dvb->adapter, dvb->fe[1]);
679                 if (result < 0) {
680                         printk(KERN_WARNING "%s: 2nd dvb_register_frontend failed (errno = %d)\n",
681                                 dev->name, result);
682                         goto fail_frontend1;
683                 }
684         }
685
686         /* register demux stuff */
687         dvb->demux.dmx.capabilities =
688                 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
689                 DMX_MEMORY_BASED_FILTERING;
690         dvb->demux.priv       = dvb;
691         dvb->demux.filternum  = 256;
692         dvb->demux.feednum    = 256;
693         dvb->demux.start_feed = em28xx_start_feed;
694         dvb->demux.stop_feed  = em28xx_stop_feed;
695
696         result = dvb_dmx_init(&dvb->demux);
697         if (result < 0) {
698                 printk(KERN_WARNING "%s: dvb_dmx_init failed (errno = %d)\n",
699                        dev->name, result);
700                 goto fail_dmx;
701         }
702
703         dvb->dmxdev.filternum    = 256;
704         dvb->dmxdev.demux        = &dvb->demux.dmx;
705         dvb->dmxdev.capabilities = 0;
706         result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
707         if (result < 0) {
708                 printk(KERN_WARNING "%s: dvb_dmxdev_init failed (errno = %d)\n",
709                        dev->name, result);
710                 goto fail_dmxdev;
711         }
712
713         dvb->fe_hw.source = DMX_FRONTEND_0;
714         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
715         if (result < 0) {
716                 printk(KERN_WARNING "%s: add_frontend failed (DMX_FRONTEND_0, errno = %d)\n",
717                        dev->name, result);
718                 goto fail_fe_hw;
719         }
720
721         dvb->fe_mem.source = DMX_MEMORY_FE;
722         result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
723         if (result < 0) {
724                 printk(KERN_WARNING "%s: add_frontend failed (DMX_MEMORY_FE, errno = %d)\n",
725                        dev->name, result);
726                 goto fail_fe_mem;
727         }
728
729         result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
730         if (result < 0) {
731                 printk(KERN_WARNING "%s: connect_frontend failed (errno = %d)\n",
732                        dev->name, result);
733                 goto fail_fe_conn;
734         }
735
736         /* register network adapter */
737         dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
738         return 0;
739
740 fail_fe_conn:
741         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
742 fail_fe_mem:
743         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
744 fail_fe_hw:
745         dvb_dmxdev_release(&dvb->dmxdev);
746 fail_dmxdev:
747         dvb_dmx_release(&dvb->demux);
748 fail_dmx:
749         if (dvb->fe[1])
750                 dvb_unregister_frontend(dvb->fe[1]);
751         dvb_unregister_frontend(dvb->fe[0]);
752 fail_frontend1:
753         if (dvb->fe[1])
754                 dvb_frontend_detach(dvb->fe[1]);
755 fail_frontend0:
756         dvb_frontend_detach(dvb->fe[0]);
757         dvb_unregister_adapter(&dvb->adapter);
758 fail_adapter:
759         return result;
760 }
761
762 static void em28xx_unregister_dvb(struct em28xx_dvb *dvb)
763 {
764         dvb_net_release(&dvb->net);
765         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
766         dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
767         dvb_dmxdev_release(&dvb->dmxdev);
768         dvb_dmx_release(&dvb->demux);
769         if (dvb->fe[1])
770                 dvb_unregister_frontend(dvb->fe[1]);
771         dvb_unregister_frontend(dvb->fe[0]);
772         if (dvb->fe[1] && !dvb->dont_attach_fe1)
773                 dvb_frontend_detach(dvb->fe[1]);
774         dvb_frontend_detach(dvb->fe[0]);
775         dvb_unregister_adapter(&dvb->adapter);
776 }
777
778 static int em28xx_dvb_init(struct em28xx *dev)
779 {
780         int result = 0, mfe_shared = 0;
781         struct em28xx_dvb *dvb;
782
783         if (!dev->board.has_dvb) {
784                 /* This device does not support the extension */
785                 printk(KERN_INFO "em28xx_dvb: This device does not support the extension\n");
786                 return 0;
787         }
788
789         dvb = kzalloc(sizeof(struct em28xx_dvb), GFP_KERNEL);
790
791         if (dvb == NULL) {
792                 em28xx_info("em28xx_dvb: memory allocation failed\n");
793                 return -ENOMEM;
794         }
795         dev->dvb = dvb;
796         dvb->fe[0] = dvb->fe[1] = NULL;
797
798         mutex_lock(&dev->lock);
799         em28xx_set_mode(dev, EM28XX_DIGITAL_MODE);
800         /* init frontend */
801         switch (dev->model) {
802         case EM2874_BOARD_LEADERSHIP_ISDBT:
803                 dvb->fe[0] = dvb_attach(s921_attach,
804                                 &sharp_isdbt, &dev->i2c_adap);
805
806                 if (!dvb->fe[0]) {
807                         result = -EINVAL;
808                         goto out_free;
809                 }
810
811                 break;
812         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_850:
813         case EM2883_BOARD_HAUPPAUGE_WINTV_HVR_950:
814         case EM2880_BOARD_PINNACLE_PCTV_HD_PRO:
815         case EM2880_BOARD_AMD_ATI_TV_WONDER_HD_600:
816                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
817                                            &em2880_lgdt3303_dev,
818                                            &dev->i2c_adap);
819                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
820                         result = -EINVAL;
821                         goto out_free;
822                 }
823                 break;
824         case EM2880_BOARD_KWORLD_DVB_310U:
825                 dvb->fe[0] = dvb_attach(zl10353_attach,
826                                            &em28xx_zl10353_with_xc3028,
827                                            &dev->i2c_adap);
828                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
829                         result = -EINVAL;
830                         goto out_free;
831                 }
832                 break;
833         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900:
834         case EM2882_BOARD_TERRATEC_HYBRID_XS:
835         case EM2880_BOARD_EMPIRE_DUAL_TV:
836                 dvb->fe[0] = dvb_attach(zl10353_attach,
837                                            &em28xx_zl10353_xc3028_no_i2c_gate,
838                                            &dev->i2c_adap);
839                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
840                         result = -EINVAL;
841                         goto out_free;
842                 }
843                 break;
844         case EM2880_BOARD_TERRATEC_HYBRID_XS:
845         case EM2880_BOARD_TERRATEC_HYBRID_XS_FR:
846         case EM2881_BOARD_PINNACLE_HYBRID_PRO:
847         case EM2882_BOARD_DIKOM_DK300:
848         case EM2882_BOARD_KWORLD_VS_DVBT:
849                 dvb->fe[0] = dvb_attach(zl10353_attach,
850                                            &em28xx_zl10353_xc3028_no_i2c_gate,
851                                            &dev->i2c_adap);
852                 if (dvb->fe[0] == NULL) {
853                         /* This board could have either a zl10353 or a mt352.
854                            If the chip id isn't for zl10353, try mt352 */
855                         dvb->fe[0] = dvb_attach(mt352_attach,
856                                                    &terratec_xs_mt352_cfg,
857                                                    &dev->i2c_adap);
858                 }
859
860                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
861                         result = -EINVAL;
862                         goto out_free;
863                 }
864                 break;
865         case EM2870_BOARD_KWORLD_355U:
866                 dvb->fe[0] = dvb_attach(zl10353_attach,
867                                            &em28xx_zl10353_no_i2c_gate_dev,
868                                            &dev->i2c_adap);
869                 if (dvb->fe[0] != NULL)
870                         dvb_attach(qt1010_attach, dvb->fe[0],
871                                    &dev->i2c_adap, &em28xx_qt1010_config);
872                 break;
873         case EM2883_BOARD_KWORLD_HYBRID_330U:
874         case EM2882_BOARD_EVGA_INDTUBE:
875                 dvb->fe[0] = dvb_attach(s5h1409_attach,
876                                            &em28xx_s5h1409_with_xc3028,
877                                            &dev->i2c_adap);
878                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
879                         result = -EINVAL;
880                         goto out_free;
881                 }
882                 break;
883         case EM2882_BOARD_KWORLD_ATSC_315U:
884                 dvb->fe[0] = dvb_attach(lgdt330x_attach,
885                                            &em2880_lgdt3303_dev,
886                                            &dev->i2c_adap);
887                 if (dvb->fe[0] != NULL) {
888                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
889                                 &dev->i2c_adap, 0x61, TUNER_THOMSON_DTT761X)) {
890                                 result = -EINVAL;
891                                 goto out_free;
892                         }
893                 }
894                 break;
895         case EM2880_BOARD_HAUPPAUGE_WINTV_HVR_900_R2:
896         case EM2882_BOARD_PINNACLE_HYBRID_PRO_330E:
897                 dvb->fe[0] = dvb_attach(drxd_attach, &em28xx_drxd, NULL,
898                                            &dev->i2c_adap, &dev->udev->dev);
899                 if (em28xx_attach_xc3028(0x61, dev) < 0) {
900                         result = -EINVAL;
901                         goto out_free;
902                 }
903                 break;
904         case EM2870_BOARD_REDDO_DVB_C_USB_BOX:
905                 /* Philips CU1216L NIM (Philips TDA10023 + Infineon TUA6034) */
906                 dvb->fe[0] = dvb_attach(tda10023_attach,
907                         &em28xx_tda10023_config,
908                         &dev->i2c_adap, 0x48);
909                 if (dvb->fe[0]) {
910                         if (!dvb_attach(simple_tuner_attach, dvb->fe[0],
911                                 &dev->i2c_adap, 0x60, TUNER_PHILIPS_CU1216L)) {
912                                 result = -EINVAL;
913                                 goto out_free;
914                         }
915                 }
916                 break;
917         case EM2870_BOARD_KWORLD_A340:
918                 dvb->fe[0] = dvb_attach(lgdt3305_attach,
919                                            &em2870_lgdt3304_dev,
920                                            &dev->i2c_adap);
921                 if (dvb->fe[0] != NULL)
922                         dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
923                                    &dev->i2c_adap, &kworld_a340_config);
924                 break;
925         case EM28174_BOARD_PCTV_290E:
926                 dvb->fe[0] = dvb_attach(cxd2820r_attach,
927                                         &em28xx_cxd2820r_config,
928                                         &dev->i2c_adap);
929                 if (dvb->fe[0]) {
930                         /* FE 0 attach tuner */
931                         if (!dvb_attach(tda18271_attach,
932                                         dvb->fe[0],
933                                         0x60,
934                                         &dev->i2c_adap,
935                                         &em28xx_cxd2820r_tda18271_config)) {
936
937                                 dvb_frontend_detach(dvb->fe[0]);
938                                 result = -EINVAL;
939                                 goto out_free;
940                         }
941                 }
942                 break;
943         case EM2884_BOARD_HAUPPAUGE_WINTV_HVR_930C:
944         {
945                 struct xc5000_config cfg;
946                 hauppauge_hvr930c_init(dev);
947
948                 dvb->fe[0] = dvb_attach(drxk_attach,
949                                         &hauppauge_930c_drxk, &dev->i2c_adap);
950                 if (!dvb->fe[0]) {
951                         result = -EINVAL;
952                         goto out_free;
953                 }
954                 /* FIXME: do we need a pll semaphore? */
955                 dvb->fe[0]->sec_priv = dvb;
956                 sema_init(&dvb->pll_mutex, 1);
957                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
958                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
959
960                 /* Attach xc5000 */
961                 memset(&cfg, 0, sizeof(cfg));
962                 cfg.i2c_address  = 0x61;
963                 cfg.if_khz = 4000;
964
965                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
966                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
967                 if (!dvb_attach(xc5000_attach, dvb->fe[0], &dev->i2c_adap,
968                                 &cfg)) {
969                         result = -EINVAL;
970                         goto out_free;
971                 }
972                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
973                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
974
975                 break;
976         }
977         case EM2884_BOARD_TERRATEC_H5:
978         case EM2884_BOARD_CINERGY_HTC_STICK:
979                 terratec_h5_init(dev);
980
981                 dvb->fe[0] = dvb_attach(drxk_attach, &terratec_h5_drxk, &dev->i2c_adap);
982                 if (!dvb->fe[0]) {
983                         result = -EINVAL;
984                         goto out_free;
985                 }
986                 /* FIXME: do we need a pll semaphore? */
987                 dvb->fe[0]->sec_priv = dvb;
988                 sema_init(&dvb->pll_mutex, 1);
989                 dvb->gate_ctrl = dvb->fe[0]->ops.i2c_gate_ctrl;
990                 dvb->fe[0]->ops.i2c_gate_ctrl = drxk_gate_ctrl;
991
992                 /* Attach tda18271 to DVB-C frontend */
993                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
994                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 1);
995                 if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0], &dev->i2c_adap, 0x60)) {
996                         result = -EINVAL;
997                         goto out_free;
998                 }
999                 if (dvb->fe[0]->ops.i2c_gate_ctrl)
1000                         dvb->fe[0]->ops.i2c_gate_ctrl(dvb->fe[0], 0);
1001
1002                 break;
1003         case EM28174_BOARD_PCTV_460E:
1004                 /* attach demod */
1005                 dvb->fe[0] = dvb_attach(tda10071_attach,
1006                         &em28xx_tda10071_config, &dev->i2c_adap);
1007
1008                 /* attach SEC */
1009                 if (dvb->fe[0])
1010                         dvb_attach(a8293_attach, dvb->fe[0], &dev->i2c_adap,
1011                                 &em28xx_a8293_config);
1012                 break;
1013         case EM2874_BOARD_MAXMEDIA_UB425_TC:
1014                 /* attach demodulator */
1015                 dvb->fe[0] = dvb_attach(drxk_attach, &maxmedia_ub425_tc_drxk,
1016                                 &dev->i2c_adap);
1017
1018                 if (dvb->fe[0]) {
1019                         /* disable I2C-gate */
1020                         dvb->fe[0]->ops.i2c_gate_ctrl = NULL;
1021
1022                         /* attach tuner */
1023                         if (!dvb_attach(tda18271c2dd_attach, dvb->fe[0],
1024                                         &dev->i2c_adap, 0x60)) {
1025                                 dvb_frontend_detach(dvb->fe[0]);
1026                                 result = -EINVAL;
1027                                 goto out_free;
1028                         }
1029                 }
1030
1031                 /* TODO: we need drx-3913k firmware in order to support DVB-T */
1032                 em28xx_info("MaxMedia UB425-TC: only DVB-C supported by that " \
1033                                 "driver version\n");
1034
1035                 break;
1036         case EM2884_BOARD_PCTV_520E:
1037                 pctv_520e_init(dev);
1038
1039                 /* attach demodulator */
1040                 dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1041                                 &dev->i2c_adap);
1042
1043                 if (dvb->fe[0]) {
1044                         /* attach tuner */
1045                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1046                                         &dev->i2c_adap,
1047                                         &em28xx_cxd2820r_tda18271_config)) {
1048                                 dvb_frontend_detach(dvb->fe[0]);
1049                                 result = -EINVAL;
1050                                 goto out_free;
1051                         }
1052                 }
1053                 break;
1054         case EM2884_BOARD_PCTV_510E:
1055         case EM2884_BOARD_PCTV_520E:
1056                 pctv_520e_init(dev);
1057
1058                 /* attach demodulator */
1059                 dvb->fe[0] = dvb_attach(drxk_attach, &pctv_520e_drxk,
1060                                 &dev->i2c_adap);
1061
1062                 if (dvb->fe[0]) {
1063                         /* attach tuner */
1064                         if (!dvb_attach(tda18271_attach, dvb->fe[0], 0x60,
1065                                         &dev->i2c_adap,
1066                                         &em28xx_cxd2820r_tda18271_config)) {
1067                                 dvb_frontend_detach(dvb->fe[0]);
1068                                 result = -EINVAL;
1069                                 goto out_free;
1070                         }
1071                 }
1072                 break;
1073         default:
1074                 em28xx_errdev("/2: The frontend of your DVB/ATSC card"
1075                                 " isn't supported yet\n");
1076                 break;
1077         }
1078         if (NULL == dvb->fe[0]) {
1079                 em28xx_errdev("/2: frontend initialization failed\n");
1080                 result = -EINVAL;
1081                 goto out_free;
1082         }
1083         /* define general-purpose callback pointer */
1084         dvb->fe[0]->callback = em28xx_tuner_callback;
1085         if (dvb->fe[1])
1086                 dvb->fe[1]->callback = em28xx_tuner_callback;
1087
1088         /* register everything */
1089         result = em28xx_register_dvb(dvb, THIS_MODULE, dev, &dev->udev->dev);
1090
1091         if (result < 0)
1092                 goto out_free;
1093
1094         /* MFE lock */
1095         dvb->adapter.mfe_shared = mfe_shared;
1096
1097         em28xx_info("Successfully loaded em28xx-dvb\n");
1098 ret:
1099         em28xx_set_mode(dev, EM28XX_SUSPEND);
1100         mutex_unlock(&dev->lock);
1101         return result;
1102
1103 out_free:
1104         kfree(dvb);
1105         dev->dvb = NULL;
1106         goto ret;
1107 }
1108
1109 static inline void prevent_sleep(struct dvb_frontend_ops *ops)
1110 {
1111         ops->set_voltage = NULL;
1112         ops->sleep = NULL;
1113         ops->tuner_ops.sleep = NULL;
1114 }
1115
1116 static int em28xx_dvb_fini(struct em28xx *dev)
1117 {
1118         if (!dev->board.has_dvb) {
1119                 /* This device does not support the extension */
1120                 return 0;
1121         }
1122
1123         if (dev->dvb) {
1124                 struct em28xx_dvb *dvb = dev->dvb;
1125
1126                 if (dev->state & DEV_DISCONNECTED) {
1127                         /* We cannot tell the device to sleep
1128                          * once it has been unplugged. */
1129                         if (dvb->fe[0])
1130                                 prevent_sleep(&dvb->fe[0]->ops);
1131                         if (dvb->fe[1])
1132                                 prevent_sleep(&dvb->fe[1]->ops);
1133                 }
1134
1135                 em28xx_unregister_dvb(dvb);
1136                 kfree(dvb);
1137                 dev->dvb = NULL;
1138         }
1139
1140         return 0;
1141 }
1142
1143 static struct em28xx_ops dvb_ops = {
1144         .id   = EM28XX_DVB,
1145         .name = "Em28xx dvb Extension",
1146         .init = em28xx_dvb_init,
1147         .fini = em28xx_dvb_fini,
1148 };
1149
1150 static int __init em28xx_dvb_register(void)
1151 {
1152         return em28xx_register_extension(&dvb_ops);
1153 }
1154
1155 static void __exit em28xx_dvb_unregister(void)
1156 {
1157         em28xx_unregister_extension(&dvb_ops);
1158 }
1159
1160 module_init(em28xx_dvb_register);
1161 module_exit(em28xx_dvb_unregister);