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