Merge branch 'master' of /usr/src/ntfs-2.6/
[linux-2.6] / drivers / media / dvb / ttpci / budget-core.c
1 /*
2  * budget-core.c: driver for the SAA7146 based Budget DVB cards
3  *
4  * Compiled from various sources by Michael Hunold <michael@mihu.de>
5  *
6  * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
7  *
8  * Copyright (C) 1999-2002 Ralph  Metzler
9  *                       & Marcus Metzler for convergence integrated media GmbH
10  *
11  * 26feb2004 Support for FS Activy Card (Grundig tuner) by
12  *           Michael Dreher <michael@5dot1.de>,
13  *           Oliver Endriss <o.endriss@gmx.de>,
14  *           Andreas 'randy' Weinberger
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * as published by the Free Software Foundation; either version 2
19  * of the License, or (at your option) any later version.
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  *
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program; if not, write to the Free Software
30  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31  * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
32  *
33  *
34  * the project's page is at http://www.linuxtv.org/dvb/
35  */
36
37 #include <linux/moduleparam.h>
38
39 #include "budget.h"
40 #include "ttpci-eeprom.h"
41
42 int budget_debug;
43 module_param_named(debug, budget_debug, int, 0644);
44 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
45
46 /****************************************************************************
47  * TT budget / WinTV Nova
48  ****************************************************************************/
49
50 static int stop_ts_capture(struct budget *budget)
51 {
52         dprintk(2, "budget: %p\n", budget);
53
54         if (--budget->feeding)
55                 return budget->feeding;
56
57         saa7146_write(budget->dev, MC1, MASK_20);       // DMA3 off
58         SAA7146_IER_DISABLE(budget->dev, MASK_10);
59         return 0;
60 }
61
62 static int start_ts_capture(struct budget *budget)
63 {
64         struct saa7146_dev *dev = budget->dev;
65
66         dprintk(2, "budget: %p\n", budget);
67
68         if (budget->feeding)
69                 return ++budget->feeding;
70
71         saa7146_write(dev, MC1, MASK_20);       // DMA3 off
72
73         memset(budget->grabbing, 0x00, TS_HEIGHT * TS_WIDTH);
74
75         saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
76
77         budget->tsf = 0xff;
78         budget->ttbp = 0;
79
80         /*
81          *  Signal path on the Activy:
82          *
83          *  tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
84          *
85          *  Since the tuner feeds 204 bytes packets into the SAA7146,
86          *  DMA3 is configured to strip the trailing 16 FEC bytes:
87          *      Pitch: 188, NumBytes3: 188, NumLines3: 1024
88          */
89
90         switch(budget->card->type) {
91         case BUDGET_FS_ACTIVY:
92                 saa7146_write(dev, DD1_INIT, 0x04000000);
93                 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
94                 saa7146_write(dev, BRS_CTRL, 0x00000000);
95                 break;
96         case BUDGET_PATCH:
97                 saa7146_write(dev, DD1_INIT, 0x00000200);
98                 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
99                 saa7146_write(dev, BRS_CTRL, 0x60000000);
100                 break;
101         default:
102                 if (budget->video_port == BUDGET_VIDEO_PORTA) {
103                         saa7146_write(dev, DD1_INIT, 0x06000200);
104                         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
105                         saa7146_write(dev, BRS_CTRL, 0x00000000);
106                 } else {
107                         saa7146_write(dev, DD1_INIT, 0x02000600);
108                         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
109                         saa7146_write(dev, BRS_CTRL, 0x60000000);
110                 }
111         }
112
113         saa7146_write(dev, MC2, (MASK_08 | MASK_24));
114         mdelay(10);
115
116         saa7146_write(dev, BASE_ODD3, 0);
117         saa7146_write(dev, BASE_EVEN3, 0);
118         saa7146_write(dev, PROT_ADDR3, TS_WIDTH * TS_HEIGHT);
119         saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
120
121         if (budget->card->type == BUDGET_FS_ACTIVY) {
122                 saa7146_write(dev, PITCH3, TS_WIDTH / 2);
123                 saa7146_write(dev, NUM_LINE_BYTE3, ((TS_HEIGHT * 2) << 16) | (TS_WIDTH / 2));
124         } else {
125                 saa7146_write(dev, PITCH3, TS_WIDTH);
126                 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT << 16) | TS_WIDTH);
127         }
128
129         saa7146_write(dev, MC2, (MASK_04 | MASK_20));
130
131         SAA7146_ISR_CLEAR(budget->dev, MASK_10);        /* VPE */
132         SAA7146_IER_ENABLE(budget->dev, MASK_10);       /* VPE */
133         saa7146_write(dev, MC1, (MASK_04 | MASK_20));   /* DMA3 on */
134
135         return ++budget->feeding;
136 }
137
138 static void vpeirq(unsigned long data)
139 {
140         struct budget *budget = (struct budget *) data;
141         u8 *mem = (u8 *) (budget->grabbing);
142         u32 olddma = budget->ttbp;
143         u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
144
145         /* nearest lower position divisible by 188 */
146         newdma -= newdma % 188;
147
148         if (newdma >= TS_BUFLEN)
149                 return;
150
151         budget->ttbp = newdma;
152
153         if (budget->feeding == 0 || newdma == olddma)
154                 return;
155
156         if (newdma > olddma) {  /* no wraparound, dump olddma..newdma */
157                 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, (newdma - olddma) / 188);
158         } else {                /* wraparound, dump olddma..buflen and 0..newdma */
159                 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, (TS_BUFLEN - olddma) / 188);
160                 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
161         }
162 }
163
164
165 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
166                           int uselocks, int nobusyloop)
167 {
168         struct saa7146_dev *saa = budget->dev;
169         int result = 0;
170         unsigned long flags = 0;
171
172         if (count > 4 || count <= 0)
173                 return 0;
174
175         if (uselocks)
176                 spin_lock_irqsave(&budget->debilock, flags);
177
178         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
179                 if (uselocks)
180                         spin_unlock_irqrestore(&budget->debilock, flags);
181                 return result;
182         }
183
184         saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
185         saa7146_write(saa, DEBI_CONFIG, config);
186         saa7146_write(saa, DEBI_PAGE, 0);
187         saa7146_write(saa, MC2, (2 << 16) | 2);
188
189         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
190                 if (uselocks)
191                         spin_unlock_irqrestore(&budget->debilock, flags);
192                 return result;
193         }
194
195         result = saa7146_read(saa, DEBI_AD);
196         result &= (0xffffffffUL >> ((4 - count) * 8));
197
198         if (uselocks)
199                 spin_unlock_irqrestore(&budget->debilock, flags);
200
201         return result;
202 }
203
204 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
205                            int count, u32 value, int uselocks, int nobusyloop)
206 {
207         struct saa7146_dev *saa = budget->dev;
208         unsigned long flags = 0;
209         int result;
210
211         if (count > 4 || count <= 0)
212                 return 0;
213
214         if (uselocks)
215                 spin_lock_irqsave(&budget->debilock, flags);
216
217         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
218                 if (uselocks)
219                         spin_unlock_irqrestore(&budget->debilock, flags);
220                 return result;
221         }
222
223         saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
224         saa7146_write(saa, DEBI_CONFIG, config);
225         saa7146_write(saa, DEBI_PAGE, 0);
226         saa7146_write(saa, DEBI_AD, value);
227         saa7146_write(saa, MC2, (2 << 16) | 2);
228
229         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
230                 if (uselocks)
231                         spin_unlock_irqrestore(&budget->debilock, flags);
232                 return result;
233         }
234
235         if (uselocks)
236                 spin_unlock_irqrestore(&budget->debilock, flags);
237         return 0;
238 }
239
240
241 /****************************************************************************
242  * DVB API SECTION
243  ****************************************************************************/
244
245 static int budget_start_feed(struct dvb_demux_feed *feed)
246 {
247         struct dvb_demux *demux = feed->demux;
248         struct budget *budget = (struct budget *) demux->priv;
249         int status;
250
251         dprintk(2, "budget: %p\n", budget);
252
253         if (!demux->dmx.frontend)
254                 return -EINVAL;
255
256         spin_lock(&budget->feedlock);
257         feed->pusi_seen = 0; /* have a clean section start */
258         status = start_ts_capture(budget);
259         spin_unlock(&budget->feedlock);
260         return status;
261 }
262
263 static int budget_stop_feed(struct dvb_demux_feed *feed)
264 {
265         struct dvb_demux *demux = feed->demux;
266         struct budget *budget = (struct budget *) demux->priv;
267         int status;
268
269         dprintk(2, "budget: %p\n", budget);
270
271         spin_lock(&budget->feedlock);
272         status = stop_ts_capture(budget);
273         spin_unlock(&budget->feedlock);
274         return status;
275 }
276
277 static int budget_register(struct budget *budget)
278 {
279         struct dvb_demux *dvbdemux = &budget->demux;
280         int ret;
281
282         dprintk(2, "budget: %p\n", budget);
283
284         dvbdemux->priv = (void *) budget;
285
286         dvbdemux->filternum = 256;
287         dvbdemux->feednum = 256;
288         dvbdemux->start_feed = budget_start_feed;
289         dvbdemux->stop_feed = budget_stop_feed;
290         dvbdemux->write_to_decoder = NULL;
291
292         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
293                                       DMX_MEMORY_BASED_FILTERING);
294
295         dvb_dmx_init(&budget->demux);
296
297         budget->dmxdev.filternum = 256;
298         budget->dmxdev.demux = &dvbdemux->dmx;
299         budget->dmxdev.capabilities = 0;
300
301         dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
302
303         budget->hw_frontend.source = DMX_FRONTEND_0;
304
305         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
306
307         if (ret < 0)
308                 return ret;
309
310         budget->mem_frontend.source = DMX_MEMORY_FE;
311         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
312         if (ret < 0)
313                 return ret;
314
315         ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
316         if (ret < 0)
317                 return ret;
318
319         dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
320
321         return 0;
322 }
323
324 static void budget_unregister(struct budget *budget)
325 {
326         struct dvb_demux *dvbdemux = &budget->demux;
327
328         dprintk(2, "budget: %p\n", budget);
329
330         dvb_net_release(&budget->dvb_net);
331
332         dvbdemux->dmx.close(&dvbdemux->dmx);
333         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
334         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
335
336         dvb_dmxdev_release(&budget->dmxdev);
337         dvb_dmx_release(&budget->demux);
338 }
339
340 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
341                       struct saa7146_pci_extension_data *info,
342                       struct module *owner)
343 {
344         int length = TS_WIDTH * TS_HEIGHT;
345         int ret = 0;
346         struct budget_info *bi = info->ext_priv;
347
348         memset(budget, 0, sizeof(struct budget));
349
350         dprintk(2, "dev: %p, budget: %p\n", dev, budget);
351
352         budget->card = bi;
353         budget->dev = (struct saa7146_dev *) dev;
354
355         dvb_register_adapter(&budget->dvb_adapter, budget->card->name, owner);
356
357         /* set dd1 stream a & b */
358         saa7146_write(dev, DD1_STREAM_B, 0x00000000);
359         saa7146_write(dev, MC2, (MASK_09 | MASK_25));
360         saa7146_write(dev, MC2, (MASK_10 | MASK_26));
361         saa7146_write(dev, DD1_INIT, 0x02000000);
362         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
363
364         if (bi->type != BUDGET_FS_ACTIVY)
365                 budget->video_port = BUDGET_VIDEO_PORTB;
366         else
367                 budget->video_port = BUDGET_VIDEO_PORTA;
368         spin_lock_init(&budget->feedlock);
369         spin_lock_init(&budget->debilock);
370
371         /* the Siemens DVB needs this if you want to have the i2c chips
372            get recognized before the main driver is loaded */
373         if (bi->type != BUDGET_FS_ACTIVY)
374                 saa7146_write(dev, GPIO_CTRL, 0x500000);        /* GPIO 3 = 1 */
375
376 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
377         budget->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
378 #else
379         budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
380 #endif
381
382         strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
383
384         saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
385         strcpy(budget->i2c_adap.name, budget->card->name);
386
387         if (i2c_add_adapter(&budget->i2c_adap) < 0) {
388                 dvb_unregister_adapter(&budget->dvb_adapter);
389                 return -ENOMEM;
390         }
391
392         ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
393
394         if (NULL ==
395             (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, length, &budget->pt))) {
396                 ret = -ENOMEM;
397                 goto err;
398         }
399
400         saa7146_write(dev, PCI_BT_V1, 0x001c0000);
401         /* upload all */
402         saa7146_write(dev, GPIO_CTRL, 0x000000);
403
404         tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
405
406         /* frontend power on */
407         if (bi->type == BUDGET_FS_ACTIVY)
408                 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI);
409         else
410                 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
411
412         if (budget_register(budget) == 0) {
413                 return 0;
414         }
415 err:
416         i2c_del_adapter(&budget->i2c_adap);
417
418         vfree(budget->grabbing);
419
420         dvb_unregister_adapter(&budget->dvb_adapter);
421
422         return ret;
423 }
424
425 int ttpci_budget_deinit(struct budget *budget)
426 {
427         struct saa7146_dev *dev = budget->dev;
428
429         dprintk(2, "budget: %p\n", budget);
430
431         budget_unregister(budget);
432
433         i2c_del_adapter(&budget->i2c_adap);
434
435         dvb_unregister_adapter(&budget->dvb_adapter);
436
437         tasklet_kill(&budget->vpe_tasklet);
438
439         saa7146_pgtable_free(dev->pci, &budget->pt);
440
441         vfree(budget->grabbing);
442
443         return 0;
444 }
445
446 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
447 {
448         struct budget *budget = (struct budget *) dev->ext_priv;
449
450         dprintk(8, "dev: %p, budget: %p\n", dev, budget);
451
452         if (*isr & MASK_10)
453                 tasklet_schedule(&budget->vpe_tasklet);
454 }
455
456 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
457 {
458         struct budget *budget = (struct budget *) dev->ext_priv;
459
460         spin_lock(&budget->feedlock);
461         budget->video_port = video_port;
462         if (budget->feeding) {
463                 int oldfeeding = budget->feeding;
464                 budget->feeding = 1;
465                 stop_ts_capture(budget);
466                 start_ts_capture(budget);
467                 budget->feeding = oldfeeding;
468         }
469         spin_unlock(&budget->feedlock);
470 }
471
472 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
473 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
474 EXPORT_SYMBOL_GPL(ttpci_budget_init);
475 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
476 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
477 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
478 EXPORT_SYMBOL_GPL(budget_debug);
479
480 MODULE_LICENSE("GPL");