Automatic merge of /spare/repo/netdev-2.6 branch we18-ieee80211
[linux-2.6] / drivers / media / dvb / bt8xx / dvb-bt8xx.c
1 /*
2  * Bt8xx based DVB adapter driver
3  *
4  * Copyright (C) 2002,2003 Florian Schirmer <jolt@tuxbox.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  */
21
22 #include <linux/bitops.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
25 #include <linux/init.h>
26 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/slab.h>
29 #include <linux/i2c.h>
30
31 #include "dmxdev.h"
32 #include "dvbdev.h"
33 #include "dvb_demux.h"
34 #include "dvb_frontend.h"
35
36 #include "dvb-bt8xx.h"
37
38 #include "bt878.h"
39
40 static int debug;
41
42 module_param(debug, int, 0644);
43 MODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
44
45 #define dprintk( args... ) \
46         do { \
47                 if (debug) printk(KERN_DEBUG args); \
48         } while (0)
49
50 static void dvb_bt8xx_task(unsigned long data)
51 {
52         struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *)data;
53
54         //printk("%d ", card->bt->finished_block);
55
56         while (card->bt->last_block != card->bt->finished_block) {
57                 (card->bt->TS_Size ? dvb_dmx_swfilter_204 : dvb_dmx_swfilter)
58                         (&card->demux,
59                          &card->bt->buf_cpu[card->bt->last_block *
60                                             card->bt->block_bytes],
61                          card->bt->block_bytes);
62                 card->bt->last_block = (card->bt->last_block + 1) %
63                                         card->bt->block_count;
64         }
65 }
66
67 static int dvb_bt8xx_start_feed(struct dvb_demux_feed *dvbdmxfeed)
68 {
69         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
70         struct dvb_bt8xx_card *card = dvbdmx->priv;
71         int rc;
72
73         dprintk("dvb_bt8xx: start_feed\n");
74
75         if (!dvbdmx->dmx.frontend)
76                 return -EINVAL;
77
78         down(&card->lock);
79         card->nfeeds++;
80         rc = card->nfeeds;
81         if (card->nfeeds == 1)
82                 bt878_start(card->bt, card->gpio_mode,
83                             card->op_sync_orin, card->irq_err_ignore);
84         up(&card->lock);
85         return rc;
86 }
87
88 static int dvb_bt8xx_stop_feed(struct dvb_demux_feed *dvbdmxfeed)
89 {
90         struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
91         struct dvb_bt8xx_card *card = dvbdmx->priv;
92
93         dprintk("dvb_bt8xx: stop_feed\n");
94
95         if (!dvbdmx->dmx.frontend)
96                 return -EINVAL;
97
98         down(&card->lock);
99         card->nfeeds--;
100         if (card->nfeeds == 0)
101                 bt878_stop(card->bt);
102         up(&card->lock);
103
104         return 0;
105 }
106
107 static int is_pci_slot_eq(struct pci_dev* adev, struct pci_dev* bdev)
108 {
109         if ((adev->subsystem_vendor == bdev->subsystem_vendor) &&
110                 (adev->subsystem_device == bdev->subsystem_device) &&
111                 (adev->bus->number == bdev->bus->number) &&
112                 (PCI_SLOT(adev->devfn) == PCI_SLOT(bdev->devfn)))
113                 return 1;
114         return 0;
115 }
116
117 static struct bt878 __init *dvb_bt8xx_878_match(unsigned int bttv_nr, struct pci_dev* bttv_pci_dev)
118 {
119         unsigned int card_nr;
120
121         /* Hmm, n squared. Hope n is small */
122         for (card_nr = 0; card_nr < bt878_num; card_nr++) {
123                 if (is_pci_slot_eq(bt878[card_nr].dev, bttv_pci_dev))
124                         return &bt878[card_nr];
125         }
126         return NULL;
127 }
128
129
130 static int thomson_dtt7579_demod_init(struct dvb_frontend* fe)
131 {
132         static u8 mt352_clock_config [] = { 0x89, 0x38, 0x38 };
133         static u8 mt352_reset [] = { 0x50, 0x80 };
134         static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
135         static u8 mt352_agc_cfg [] = { 0x67, 0x28, 0x20 };
136         static u8 mt352_gpp_ctl_cfg [] = { 0x8C, 0x33 };
137         static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
138
139         mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
140         udelay(2000);
141         mt352_write(fe, mt352_reset, sizeof(mt352_reset));
142         mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
143
144         mt352_write(fe, mt352_agc_cfg, sizeof(mt352_agc_cfg));
145         mt352_write(fe, mt352_gpp_ctl_cfg, sizeof(mt352_gpp_ctl_cfg));
146         mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
147
148         return 0;
149 }
150
151 static int thomson_dtt7579_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params, u8* pllbuf)
152 {
153         u32 div;
154         unsigned char bs = 0;
155         unsigned char cp = 0;
156
157         #define IF_FREQUENCYx6 217    /* 6 * 36.16666666667MHz */
158         div = (((params->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
159
160         if (params->frequency < 542000000) cp = 0xb4;
161         else if (params->frequency < 771000000) cp = 0xbc;
162         else cp = 0xf4;
163
164         if (params->frequency == 0) bs = 0x03;
165         else if (params->frequency < 443250000) bs = 0x02;
166         else bs = 0x08;
167
168         pllbuf[0] = 0xc0; // Note: non-linux standard PLL i2c address
169         pllbuf[1] = div >> 8;
170         pllbuf[2] = div & 0xff;
171         pllbuf[3] = cp;
172         pllbuf[4] = bs;
173
174         return 0;
175 }
176
177 static struct mt352_config thomson_dtt7579_config = {
178
179         .demod_address = 0x0f,
180         .demod_init = thomson_dtt7579_demod_init,
181         .pll_set = thomson_dtt7579_pll_set,
182 };
183
184 static int cx24108_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
185 {
186    u32 freq = params->frequency;
187
188    int i, a, n, pump;
189    u32 band, pll;
190
191
192    u32 osci[]={950000,1019000,1075000,1178000,1296000,1432000,
193                1576000,1718000,1856000,2036000,2150000};
194    u32 bandsel[]={0,0x00020000,0x00040000,0x00100800,0x00101000,
195                0x00102000,0x00104000,0x00108000,0x00110000,
196                0x00120000,0x00140000};
197
198 #define XTAL 1011100 /* Hz, really 1.0111 MHz and a /10 prescaler */
199         printk("cx24108 debug: entering SetTunerFreq, freq=%d\n",freq);
200
201         /* This is really the bit driving the tuner chip cx24108 */
202
203         if(freq<950000) freq=950000; /* kHz */
204         if(freq>2150000) freq=2150000; /* satellite IF is 950..2150MHz */
205
206         /* decide which VCO to use for the input frequency */
207         for(i=1;(i<sizeof(osci)/sizeof(osci[0]))&&(osci[i]<freq);i++);
208         printk("cx24108 debug: select vco #%d (f=%d)\n",i,freq);
209         band=bandsel[i];
210         /* the gain values must be set by SetSymbolrate */
211         /* compute the pll divider needed, from Conexant data sheet,
212            resolved for (n*32+a), remember f(vco) is f(receive) *2 or *4,
213            depending on the divider bit. It is set to /4 on the 2 lowest
214            bands  */
215         n=((i<=2?2:1)*freq*10L)/(XTAL/100);
216         a=n%32; n/=32; if(a==0) n--;
217         pump=(freq<(osci[i-1]+osci[i])/2);
218         pll=0xf8000000|
219             ((pump?1:2)<<(14+11))|
220             ((n&0x1ff)<<(5+11))|
221             ((a&0x1f)<<11);
222         /* everything is shifted left 11 bits to left-align the bits in the
223            32bit word. Output to the tuner goes MSB-aligned, after all */
224         printk("cx24108 debug: pump=%d, n=%d, a=%d\n",pump,n,a);
225         cx24110_pll_write(fe,band);
226         /* set vga and vca to their widest-band settings, as a precaution.
227            SetSymbolrate might not be called to set this up */
228         cx24110_pll_write(fe,0x500c0000);
229         cx24110_pll_write(fe,0x83f1f800);
230         cx24110_pll_write(fe,pll);
231 /*        writereg(client,0x56,0x7f);*/
232
233         return 0;
234 }
235
236 static int pinnsat_pll_init(struct dvb_frontend* fe)
237 {
238    return 0;
239 }
240
241
242 static struct cx24110_config pctvsat_config = {
243
244         .demod_address = 0x55,
245         .pll_init = pinnsat_pll_init,
246         .pll_set = cx24108_pll_set,
247 };
248
249
250 static int microtune_mt7202dtf_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
251 {
252         struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
253         u8 cfg, cpump, band_select;
254         u8 data[4];
255         u32 div;
256         struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = data, .len = sizeof(data) };
257
258         div = (36000000 + params->frequency + 83333) / 166666;
259         cfg = 0x88;
260
261         if (params->frequency < 175000000) cpump = 2;
262         else if (params->frequency < 390000000) cpump = 1;
263         else if (params->frequency < 470000000) cpump = 2;
264         else if (params->frequency < 750000000) cpump = 2;
265         else cpump = 3;
266
267         if (params->frequency < 175000000) band_select = 0x0e;
268         else if (params->frequency < 470000000) band_select = 0x05;
269         else band_select = 0x03;
270
271         data[0] = (div >> 8) & 0x7f;
272         data[1] = div & 0xff;
273         data[2] = ((div >> 10) & 0x60) | cfg;
274         data[3] = cpump | band_select;
275
276         i2c_transfer(card->i2c_adapter, &msg, 1);
277         return (div * 166666 - 36000000);
278 }
279
280 static int microtune_mt7202dtf_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
281 {
282         struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
283
284         return request_firmware(fw, name, &bt->bt->dev->dev);
285 }
286
287 static struct sp887x_config microtune_mt7202dtf_config = {
288
289         .demod_address = 0x70,
290         .pll_set = microtune_mt7202dtf_pll_set,
291         .request_firmware = microtune_mt7202dtf_request_firmware,
292 };
293
294
295
296 static int advbt771_samsung_tdtc9251dh0_demod_init(struct dvb_frontend* fe)
297 {
298         static u8 mt352_clock_config [] = { 0x89, 0x38, 0x2d };
299         static u8 mt352_reset [] = { 0x50, 0x80 };
300         static u8 mt352_adc_ctl_1_cfg [] = { 0x8E, 0x40 };
301         static u8 mt352_agc_cfg [] = { 0x67, 0x10, 0x23, 0x00, 0xFF, 0xFF,
302                                        0x00, 0xFF, 0x00, 0x40, 0x40 };
303         static u8 mt352_av771_extra[] = { 0xB5, 0x7A };
304         static u8 mt352_capt_range_cfg[] = { 0x75, 0x32 };
305
306
307         mt352_write(fe, mt352_clock_config, sizeof(mt352_clock_config));
308         udelay(2000);
309         mt352_write(fe, mt352_reset, sizeof(mt352_reset));
310         mt352_write(fe, mt352_adc_ctl_1_cfg, sizeof(mt352_adc_ctl_1_cfg));
311
312         mt352_write(fe, mt352_agc_cfg,sizeof(mt352_agc_cfg));
313         udelay(2000);
314         mt352_write(fe, mt352_av771_extra,sizeof(mt352_av771_extra));
315         mt352_write(fe, mt352_capt_range_cfg, sizeof(mt352_capt_range_cfg));
316
317         return 0;
318 }
319
320 static int advbt771_samsung_tdtc9251dh0_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params, u8* pllbuf)
321 {
322         u32 div;
323         unsigned char bs = 0;
324         unsigned char cp = 0;
325
326         #define IF_FREQUENCYx6 217    /* 6 * 36.16666666667MHz */
327         div = (((params->frequency + 83333) * 3) / 500000) + IF_FREQUENCYx6;
328
329         if (params->frequency < 150000000) cp = 0xB4;
330         else if (params->frequency < 173000000) cp = 0xBC;
331         else if (params->frequency < 250000000) cp = 0xB4;
332         else if (params->frequency < 400000000) cp = 0xBC;
333         else if (params->frequency < 420000000) cp = 0xF4;
334         else if (params->frequency < 470000000) cp = 0xFC;
335         else if (params->frequency < 600000000) cp = 0xBC;
336         else if (params->frequency < 730000000) cp = 0xF4;
337         else cp = 0xFC;
338
339         if (params->frequency < 150000000) bs = 0x01;
340         else if (params->frequency < 173000000) bs = 0x01;
341         else if (params->frequency < 250000000) bs = 0x02;
342         else if (params->frequency < 400000000) bs = 0x02;
343         else if (params->frequency < 420000000) bs = 0x02;
344         else if (params->frequency < 470000000) bs = 0x02;
345         else if (params->frequency < 600000000) bs = 0x08;
346         else if (params->frequency < 730000000) bs = 0x08;
347         else bs = 0x08;
348
349         pllbuf[0] = 0xc2; // Note: non-linux standard PLL i2c address
350         pllbuf[1] = div >> 8;
351         pllbuf[2] = div & 0xff;
352         pllbuf[3] = cp;
353         pllbuf[4] = bs;
354
355         return 0;
356 }
357
358 static struct mt352_config advbt771_samsung_tdtc9251dh0_config = {
359
360         .demod_address = 0x0f,
361         .demod_init = advbt771_samsung_tdtc9251dh0_demod_init,
362         .pll_set = advbt771_samsung_tdtc9251dh0_pll_set,
363 };
364
365
366 static struct dst_config dst_config = {
367
368         .demod_address = 0x55,
369 };
370
371
372 static int or51211_request_firmware(struct dvb_frontend* fe, const struct firmware **fw, char* name)
373 {
374         struct dvb_bt8xx_card* bt = (struct dvb_bt8xx_card*) fe->dvb->priv;
375
376         return request_firmware(fw, name, &bt->bt->dev->dev);
377 }
378
379 static void or51211_setmode(struct dvb_frontend * fe, int mode)
380 {
381         struct dvb_bt8xx_card *bt = fe->dvb->priv;
382         bttv_write_gpio(bt->bttv_nr, 0x0002, mode);   /* Reset */
383         msleep(20);
384 }
385
386 static void or51211_reset(struct dvb_frontend * fe)
387 {
388         struct dvb_bt8xx_card *bt = fe->dvb->priv;
389
390         /* RESET DEVICE
391          * reset is controled by GPIO-0
392          * when set to 0 causes reset and when to 1 for normal op
393          * must remain reset for 128 clock cycles on a 50Mhz clock
394          * also PRM1 PRM2 & PRM4 are controled by GPIO-1,GPIO-2 & GPIO-4
395          * We assume that the reset has be held low long enough or we
396          * have been reset by a power on.  When the driver is unloaded
397          * reset set to 0 so if reloaded we have been reset.
398          */
399         /* reset & PRM1,2&4 are outputs */
400         int ret = bttv_gpio_enable(bt->bttv_nr, 0x001F, 0x001F);
401         if (ret != 0) {
402                 printk(KERN_WARNING "or51211: Init Error - Can't Reset DVR "
403                        "(%i)\n", ret);
404         }
405         bttv_write_gpio(bt->bttv_nr, 0x001F, 0x0000);   /* Reset */
406         msleep(20);
407         /* Now set for normal operation */
408         bttv_write_gpio(bt->bttv_nr, 0x0001F, 0x0001);
409         /* wait for operation to begin */
410         msleep(500);
411 }
412
413 static void or51211_sleep(struct dvb_frontend * fe)
414 {
415         struct dvb_bt8xx_card *bt = fe->dvb->priv;
416         bttv_write_gpio(bt->bttv_nr, 0x0001, 0x0000);
417 }
418
419 static struct or51211_config or51211_config = {
420
421         .demod_address = 0x15,
422         .request_firmware = or51211_request_firmware,
423         .setmode = or51211_setmode,
424         .reset = or51211_reset,
425         .sleep = or51211_sleep,
426 };
427
428
429 static int vp3021_alps_tded4_pll_set(struct dvb_frontend* fe, struct dvb_frontend_parameters* params)
430 {
431         struct dvb_bt8xx_card *card = (struct dvb_bt8xx_card *) fe->dvb->priv;
432         u8 buf[4];
433         u32 div;
434         struct i2c_msg msg = { .addr = 0x60, .flags = 0, .buf = buf, .len = sizeof(buf) };
435
436         div = (params->frequency + 36166667) / 166667;
437
438         buf[0] = (div >> 8) & 0x7F;
439         buf[1] = div & 0xFF;
440         buf[2] = 0x85;
441         if ((params->frequency >= 47000000) && (params->frequency < 153000000))
442                 buf[3] = 0x01;
443         else if ((params->frequency >= 153000000) && (params->frequency < 430000000))
444                 buf[3] = 0x02;
445         else if ((params->frequency >= 430000000) && (params->frequency < 824000000))
446                 buf[3] = 0x0C;
447         else if ((params->frequency >= 824000000) && (params->frequency < 863000000))
448                 buf[3] = 0x8C;
449         else
450                 return -EINVAL;
451
452         i2c_transfer(card->i2c_adapter, &msg, 1);
453         return 0;
454 }
455
456 static struct nxt6000_config vp3021_alps_tded4_config = {
457
458         .demod_address = 0x0a,
459         .clock_inversion = 1,
460         .pll_set = vp3021_alps_tded4_pll_set,
461 };
462
463
464 static void frontend_init(struct dvb_bt8xx_card *card, u32 type)
465 {
466         int ret;
467         struct dst_state* state = NULL;
468
469         switch(type) {
470 #ifdef BTTV_DVICO_DVBT_LITE
471         case BTTV_DVICO_DVBT_LITE:
472                 card->fe = mt352_attach(&thomson_dtt7579_config, card->i2c_adapter);
473                 if (card->fe != NULL) {
474                         card->fe->ops->info.frequency_min = 174000000;
475                         card->fe->ops->info.frequency_max = 862000000;
476                         break;
477                 }
478                 break;
479 #endif
480
481 #ifdef BTTV_TWINHAN_VP3021
482         case BTTV_TWINHAN_VP3021:
483 #else
484         case BTTV_NEBULA_DIGITV:
485 #endif
486                 card->fe = nxt6000_attach(&vp3021_alps_tded4_config, card->i2c_adapter);
487                 if (card->fe != NULL) {
488                         break;
489                 }
490                 break;
491
492         case BTTV_AVDVBT_761:
493                 card->fe = sp887x_attach(&microtune_mt7202dtf_config, card->i2c_adapter);
494                 if (card->fe != NULL) {
495                         break;
496                 }
497                 break;
498
499         case BTTV_AVDVBT_771:
500                 card->fe = mt352_attach(&advbt771_samsung_tdtc9251dh0_config, card->i2c_adapter);
501                 if (card->fe != NULL) {
502                         card->fe->ops->info.frequency_min = 174000000;
503                         card->fe->ops->info.frequency_max = 862000000;
504                         break;
505                 }
506                 break;
507
508         case BTTV_TWINHAN_DST:
509                 /*      DST is not a frontend driver !!!                */
510                 state = (struct dst_state *) kmalloc(sizeof (struct dst_state), GFP_KERNEL);
511                 /*      Setup the Card                                  */
512                 state->config = &dst_config;
513                 state->i2c = card->i2c_adapter;
514                 state->bt = card->bt;
515
516                 /*      DST is not a frontend, attaching the ASIC       */
517                 if ((dst_attach(state, &card->dvb_adapter)) == NULL) {
518                         printk("%s: Could not find a Twinhan DST.\n", __FUNCTION__);
519                         break;
520                 }
521                 card->fe = &state->frontend;
522
523                 /*      Attach other DST peripherals if any             */
524                 /*      Conditional Access device                       */
525                 if (state->dst_hw_cap & DST_TYPE_HAS_CA) {
526                         ret = dst_ca_attach(state, &card->dvb_adapter);
527                 }
528                 if (card->fe != NULL) {
529                         break;
530                 }
531                 break;
532
533         case BTTV_PINNACLESAT:
534                 card->fe = cx24110_attach(&pctvsat_config, card->i2c_adapter);
535                 if (card->fe != NULL) {
536                         break;
537                 }
538                 break;
539
540         case BTTV_PC_HDTV:
541                 card->fe = or51211_attach(&or51211_config, card->i2c_adapter);
542                 if (card->fe != NULL) {
543                         break;
544                 }
545                 break;
546         }
547
548         if (card->fe == NULL) {
549                 printk("dvb-bt8xx: A frontend driver was not found for device %04x/%04x subsystem %04x/%04x\n",
550                        card->bt->dev->vendor,
551                        card->bt->dev->device,
552                        card->bt->dev->subsystem_vendor,
553                        card->bt->dev->subsystem_device);
554         } else {
555                 if (dvb_register_frontend(&card->dvb_adapter, card->fe)) {
556                         printk("dvb-bt8xx: Frontend registration failed!\n");
557                         if (card->fe->ops->release)
558                                 card->fe->ops->release(card->fe);
559                         card->fe = NULL;
560                 }
561         }
562 }
563
564 static int __init dvb_bt8xx_load_card(struct dvb_bt8xx_card *card, u32 type)
565 {
566         int result;
567
568         if ((result = dvb_register_adapter(&card->dvb_adapter, card->card_name,
569                                            THIS_MODULE)) < 0) {
570                 printk("dvb_bt8xx: dvb_register_adapter failed (errno = %d)\n", result);
571                 return result;
572
573         }
574         card->dvb_adapter.priv = card;
575
576         card->bt->adapter = card->i2c_adapter;
577
578         memset(&card->demux, 0, sizeof(struct dvb_demux));
579
580         card->demux.dmx.capabilities = DMX_TS_FILTERING | DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING;
581
582         card->demux.priv = card;
583         card->demux.filternum = 256;
584         card->demux.feednum = 256;
585         card->demux.start_feed = dvb_bt8xx_start_feed;
586         card->demux.stop_feed = dvb_bt8xx_stop_feed;
587         card->demux.write_to_decoder = NULL;
588
589         if ((result = dvb_dmx_init(&card->demux)) < 0) {
590                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
591
592                 dvb_unregister_adapter(&card->dvb_adapter);
593                 return result;
594         }
595
596         card->dmxdev.filternum = 256;
597         card->dmxdev.demux = &card->demux.dmx;
598         card->dmxdev.capabilities = 0;
599
600         if ((result = dvb_dmxdev_init(&card->dmxdev, &card->dvb_adapter)) < 0) {
601                 printk("dvb_bt8xx: dvb_dmxdev_init failed (errno = %d)\n", result);
602
603                 dvb_dmx_release(&card->demux);
604                 dvb_unregister_adapter(&card->dvb_adapter);
605                 return result;
606         }
607
608         card->fe_hw.source = DMX_FRONTEND_0;
609
610         if ((result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_hw)) < 0) {
611                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
612
613                 dvb_dmxdev_release(&card->dmxdev);
614                 dvb_dmx_release(&card->demux);
615                 dvb_unregister_adapter(&card->dvb_adapter);
616                 return result;
617         }
618
619         card->fe_mem.source = DMX_MEMORY_FE;
620
621         if ((result = card->demux.dmx.add_frontend(&card->demux.dmx, &card->fe_mem)) < 0) {
622                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
623
624                 card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
625                 dvb_dmxdev_release(&card->dmxdev);
626                 dvb_dmx_release(&card->demux);
627                 dvb_unregister_adapter(&card->dvb_adapter);
628                 return result;
629         }
630
631         if ((result = card->demux.dmx.connect_frontend(&card->demux.dmx, &card->fe_hw)) < 0) {
632                 printk("dvb_bt8xx: dvb_dmx_init failed (errno = %d)\n", result);
633
634                 card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
635                 card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
636                 dvb_dmxdev_release(&card->dmxdev);
637                 dvb_dmx_release(&card->demux);
638                 dvb_unregister_adapter(&card->dvb_adapter);
639                 return result;
640         }
641
642         dvb_net_init(&card->dvb_adapter, &card->dvbnet, &card->demux.dmx);
643
644         tasklet_init(&card->bt->tasklet, dvb_bt8xx_task, (unsigned long) card);
645
646         frontend_init(card, type);
647
648         return 0;
649 }
650
651 static int dvb_bt8xx_probe(struct device *dev)
652 {
653         struct bttv_sub_device *sub = to_bttv_sub_dev(dev);
654         struct dvb_bt8xx_card *card;
655         struct pci_dev* bttv_pci_dev;
656         int ret;
657
658         if (!(card = kmalloc(sizeof(struct dvb_bt8xx_card), GFP_KERNEL)))
659                 return -ENOMEM;
660
661         memset(card, 0, sizeof(*card));
662         init_MUTEX(&card->lock);
663         card->bttv_nr = sub->core->nr;
664         strncpy(card->card_name, sub->core->name, sizeof(sub->core->name));
665         card->i2c_adapter = &sub->core->i2c_adap;
666
667         switch(sub->core->type)
668         {
669         case BTTV_PINNACLESAT:
670                 card->gpio_mode = 0x0400c060;
671                 /* should be: BT878_A_GAIN=0,BT878_A_PWRDN,BT878_DA_DPM,BT878_DA_SBR,
672                               BT878_DA_IOM=1,BT878_DA_APP to enable serial highspeed mode. */
673                 card->op_sync_orin = 0;
674                 card->irq_err_ignore = 0;
675                 break;
676
677 #ifdef BTTV_DVICO_DVBT_LITE
678         case BTTV_DVICO_DVBT_LITE:
679 #endif
680                 card->gpio_mode = 0x0400C060;
681                 card->op_sync_orin = 0;
682                 card->irq_err_ignore = 0;
683                 /* 26, 15, 14, 6, 5
684                  * A_PWRDN  DA_DPM DA_SBR DA_IOM_DA
685                  * DA_APP(parallel) */
686                 break;
687
688 #ifdef BTTV_TWINHAN_VP3021
689         case BTTV_TWINHAN_VP3021:
690 #else
691         case BTTV_NEBULA_DIGITV:
692 #endif
693         case BTTV_AVDVBT_761:
694                 card->gpio_mode = (1 << 26) | (1 << 14) | (1 << 5);
695                 card->op_sync_orin = 0;
696                 card->irq_err_ignore = 0;
697                 /* A_PWRDN DA_SBR DA_APP (high speed serial) */
698                 break;
699
700         case BTTV_AVDVBT_771: //case 0x07711461:
701                 card->gpio_mode = 0x0400402B;
702                 card->op_sync_orin = BT878_RISC_SYNC_MASK;
703                 card->irq_err_ignore = 0;
704                 /* A_PWRDN DA_SBR  DA_APP[0] PKTP=10 RISC_ENABLE FIFO_ENABLE*/
705                 break;
706
707         case BTTV_TWINHAN_DST:
708                 card->gpio_mode = 0x2204f2c;
709                 card->op_sync_orin = BT878_RISC_SYNC_MASK;
710                 card->irq_err_ignore = BT878_APABORT | BT878_ARIPERR |
711                                        BT878_APPERR | BT878_AFBUS;
712                 /* 25,21,14,11,10,9,8,3,2 then
713                  * 0x33 = 5,4,1,0
714                  * A_SEL=SML, DA_MLB, DA_SBR,
715                  * DA_SDR=f, fifo trigger = 32 DWORDS
716                  * IOM = 0 == audio A/D
717                  * DPM = 0 == digital audio mode
718                  * == async data parallel port
719                  * then 0x33 (13 is set by start_capture)
720                  * DA_APP = async data parallel port,
721                  * ACAP_EN = 1,
722                  * RISC+FIFO ENABLE */
723                 break;
724
725         case BTTV_PC_HDTV:
726                 card->gpio_mode = 0x0100EC7B;
727                 card->op_sync_orin = 0;
728                 card->irq_err_ignore = 0;
729                 break;
730
731         default:
732                 printk(KERN_WARNING "dvb_bt8xx: Unknown bttv card type: %d.\n",
733                                 sub->core->type);
734                 kfree(card);
735                 return -ENODEV;
736         }
737
738         dprintk("dvb_bt8xx: identified card%d as %s\n", card->bttv_nr, card->card_name);
739
740         if (!(bttv_pci_dev = bttv_get_pcidev(card->bttv_nr))) {
741                 printk("dvb_bt8xx: no pci device for card %d\n", card->bttv_nr);
742                 kfree(card);
743                 return -EFAULT;
744         }
745
746         if (!(card->bt = dvb_bt8xx_878_match(card->bttv_nr, bttv_pci_dev))) {
747                 printk("dvb_bt8xx: unable to determine DMA core of card %d,\n",
748                        card->bttv_nr);
749                 printk("dvb_bt8xx: if you have the ALSA bt87x audio driver "
750                        "installed, try removing it.\n");
751
752                 kfree(card);
753                 return -EFAULT;
754
755         }
756
757         init_MUTEX(&card->bt->gpio_lock);
758         card->bt->bttv_nr = sub->core->nr;
759
760         if ( (ret = dvb_bt8xx_load_card(card, sub->core->type)) ) {
761                 kfree(card);
762                 return ret;
763         }
764
765         dev_set_drvdata(dev, card);
766         return 0;
767 }
768
769 static int dvb_bt8xx_remove(struct device *dev)
770 {
771         struct dvb_bt8xx_card *card = dev_get_drvdata(dev);
772
773         dprintk("dvb_bt8xx: unloading card%d\n", card->bttv_nr);
774
775         bt878_stop(card->bt);
776         tasklet_kill(&card->bt->tasklet);
777         dvb_net_release(&card->dvbnet);
778         card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_mem);
779         card->demux.dmx.remove_frontend(&card->demux.dmx, &card->fe_hw);
780         dvb_dmxdev_release(&card->dmxdev);
781         dvb_dmx_release(&card->demux);
782         if (card->fe) dvb_unregister_frontend(card->fe);
783         dvb_unregister_adapter(&card->dvb_adapter);
784
785         kfree(card);
786
787         return 0;
788 }
789
790 static struct bttv_sub_driver driver = {
791         .drv = {
792                 .name           = "dvb-bt8xx",
793                 .probe          = dvb_bt8xx_probe,
794                 .remove         = dvb_bt8xx_remove,
795                 /* FIXME:
796                  * .shutdown    = dvb_bt8xx_shutdown,
797                  * .suspend     = dvb_bt8xx_suspend,
798                  * .resume      = dvb_bt8xx_resume,
799                  */
800         },
801 };
802
803 static int __init dvb_bt8xx_init(void)
804 {
805         return bttv_sub_register(&driver, "dvb");
806 }
807
808 static void __exit dvb_bt8xx_exit(void)
809 {
810         bttv_sub_unregister(&driver);
811 }
812
813 module_init(dvb_bt8xx_init);
814 module_exit(dvb_bt8xx_exit);
815
816 MODULE_DESCRIPTION("Bt8xx based DVB adapter driver");
817 MODULE_AUTHOR("Florian Schirmer <jolt@tuxbox.org>");
818 MODULE_LICENSE("GPL");