Pull esi-support into release branch
[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 #define TS_WIDTH                (2 * TS_SIZE)
43 #define TS_WIDTH_ACTIVY         TS_SIZE
44 #define TS_HEIGHT_MASK          0xf00
45 #define TS_HEIGHT_MASK_ACTIVY   0xc00
46 #define TS_MIN_BUFSIZE_K        188
47 #define TS_MAX_BUFSIZE_K        1410
48 #define TS_MAX_BUFSIZE_K_ACTIVY 564
49 #define BUFFER_WARNING_WAIT     (30*HZ)
50
51 int budget_debug;
52 static int dma_buffer_size = TS_MIN_BUFSIZE_K;
53 module_param_named(debug, budget_debug, int, 0644);
54 module_param_named(bufsize, dma_buffer_size, int, 0444);
55 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
56 MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
57
58 /****************************************************************************
59  * TT budget / WinTV Nova
60  ****************************************************************************/
61
62 static int stop_ts_capture(struct budget *budget)
63 {
64         dprintk(2, "budget: %p\n", budget);
65
66         saa7146_write(budget->dev, MC1, MASK_20);       // DMA3 off
67         SAA7146_IER_DISABLE(budget->dev, MASK_10);
68         return 0;
69 }
70
71 static int start_ts_capture(struct budget *budget)
72 {
73         struct saa7146_dev *dev = budget->dev;
74
75         dprintk(2, "budget: %p\n", budget);
76
77         if (!budget->feeding || !budget->fe_synced)
78                 return 0;
79
80         saa7146_write(dev, MC1, MASK_20);       // DMA3 off
81
82         memset(budget->grabbing, 0x00, budget->buffer_size);
83
84         saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
85
86         budget->ttbp = 0;
87
88         /*
89          *  Signal path on the Activy:
90          *
91          *  tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
92          *
93          *  Since the tuner feeds 204 bytes packets into the SAA7146,
94          *  DMA3 is configured to strip the trailing 16 FEC bytes:
95          *      Pitch: 188, NumBytes3: 188, NumLines3: 1024
96          */
97
98         switch(budget->card->type) {
99         case BUDGET_FS_ACTIVY:
100                 saa7146_write(dev, DD1_INIT, 0x04000000);
101                 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
102                 saa7146_write(dev, BRS_CTRL, 0x00000000);
103                 break;
104         case BUDGET_PATCH:
105                 saa7146_write(dev, DD1_INIT, 0x00000200);
106                 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
107                 saa7146_write(dev, BRS_CTRL, 0x60000000);
108                 break;
109         default:
110                 if (budget->video_port == BUDGET_VIDEO_PORTA) {
111                         saa7146_write(dev, DD1_INIT, 0x06000200);
112                         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
113                         saa7146_write(dev, BRS_CTRL, 0x00000000);
114                 } else {
115                         saa7146_write(dev, DD1_INIT, 0x02000600);
116                         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
117                         saa7146_write(dev, BRS_CTRL, 0x60000000);
118                 }
119         }
120
121         saa7146_write(dev, MC2, (MASK_08 | MASK_24));
122         mdelay(10);
123
124         saa7146_write(dev, BASE_ODD3, 0);
125         saa7146_write(dev, BASE_EVEN3, 0);
126         saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
127         saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
128
129         saa7146_write(dev, PITCH3, budget->buffer_width);
130         saa7146_write(dev, NUM_LINE_BYTE3,
131                         (budget->buffer_height << 16) | budget->buffer_width);
132
133         saa7146_write(dev, MC2, (MASK_04 | MASK_20));
134
135         SAA7146_ISR_CLEAR(budget->dev, MASK_10);        /* VPE */
136         SAA7146_IER_ENABLE(budget->dev, MASK_10);       /* VPE */
137         saa7146_write(dev, MC1, (MASK_04 | MASK_20));   /* DMA3 on */
138
139         return 0;
140 }
141
142 static int budget_read_fe_status(struct dvb_frontend *fe, fe_status_t *status)
143 {
144         struct budget *budget = (struct budget *) fe->dvb->priv;
145         int synced;
146         int ret;
147
148         if (budget->read_fe_status)
149                 ret = budget->read_fe_status(fe, status);
150         else
151                 ret = -EINVAL;
152
153         if (!ret) {
154                 synced = (*status & FE_HAS_LOCK);
155                 if (synced != budget->fe_synced) {
156                         budget->fe_synced = synced;
157                         spin_lock(&budget->feedlock);
158                         if (synced)
159                                 start_ts_capture(budget);
160                         else
161                                 stop_ts_capture(budget);
162                         spin_unlock(&budget->feedlock);
163                 }
164         }
165         return ret;
166 }
167
168 static void vpeirq(unsigned long data)
169 {
170         struct budget *budget = (struct budget *) data;
171         u8 *mem = (u8 *) (budget->grabbing);
172         u32 olddma = budget->ttbp;
173         u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
174         u32 count;
175
176         /* nearest lower position divisible by 188 */
177         newdma -= newdma % 188;
178
179         if (newdma >= budget->buffer_size)
180                 return;
181
182         budget->ttbp = newdma;
183
184         if (budget->feeding == 0 || newdma == olddma)
185                 return;
186
187         if (newdma > olddma) {  /* no wraparound, dump olddma..newdma */
188                 count = newdma - olddma;
189                 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
190         } else {                /* wraparound, dump olddma..buflen and 0..newdma */
191                 count = budget->buffer_size - olddma;
192                 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
193                 count += newdma;
194                 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
195         }
196
197         if (count > budget->buffer_warning_threshold)
198                 budget->buffer_warnings++;
199
200         if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
201                 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
202                         budget->dev->name, __FUNCTION__, budget->buffer_warnings, count);
203                 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
204                 budget->buffer_warnings = 0;
205         }
206 }
207
208
209 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
210                           int uselocks, int nobusyloop)
211 {
212         struct saa7146_dev *saa = budget->dev;
213         int result = 0;
214         unsigned long flags = 0;
215
216         if (count > 4 || count <= 0)
217                 return 0;
218
219         if (uselocks)
220                 spin_lock_irqsave(&budget->debilock, flags);
221
222         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
223                 if (uselocks)
224                         spin_unlock_irqrestore(&budget->debilock, flags);
225                 return result;
226         }
227
228         saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
229         saa7146_write(saa, DEBI_CONFIG, config);
230         saa7146_write(saa, DEBI_PAGE, 0);
231         saa7146_write(saa, MC2, (2 << 16) | 2);
232
233         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
234                 if (uselocks)
235                         spin_unlock_irqrestore(&budget->debilock, flags);
236                 return result;
237         }
238
239         result = saa7146_read(saa, DEBI_AD);
240         result &= (0xffffffffUL >> ((4 - count) * 8));
241
242         if (uselocks)
243                 spin_unlock_irqrestore(&budget->debilock, flags);
244
245         return result;
246 }
247
248 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
249                            int count, u32 value, int uselocks, int nobusyloop)
250 {
251         struct saa7146_dev *saa = budget->dev;
252         unsigned long flags = 0;
253         int result;
254
255         if (count > 4 || count <= 0)
256                 return 0;
257
258         if (uselocks)
259                 spin_lock_irqsave(&budget->debilock, flags);
260
261         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
262                 if (uselocks)
263                         spin_unlock_irqrestore(&budget->debilock, flags);
264                 return result;
265         }
266
267         saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
268         saa7146_write(saa, DEBI_CONFIG, config);
269         saa7146_write(saa, DEBI_PAGE, 0);
270         saa7146_write(saa, DEBI_AD, value);
271         saa7146_write(saa, MC2, (2 << 16) | 2);
272
273         if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
274                 if (uselocks)
275                         spin_unlock_irqrestore(&budget->debilock, flags);
276                 return result;
277         }
278
279         if (uselocks)
280                 spin_unlock_irqrestore(&budget->debilock, flags);
281         return 0;
282 }
283
284
285 /****************************************************************************
286  * DVB API SECTION
287  ****************************************************************************/
288
289 static int budget_start_feed(struct dvb_demux_feed *feed)
290 {
291         struct dvb_demux *demux = feed->demux;
292         struct budget *budget = (struct budget *) demux->priv;
293         int status = 0;
294
295         dprintk(2, "budget: %p\n", budget);
296
297         if (!demux->dmx.frontend)
298                 return -EINVAL;
299
300         spin_lock(&budget->feedlock);
301         feed->pusi_seen = 0; /* have a clean section start */
302         if (budget->feeding++ == 0)
303                 status = start_ts_capture(budget);
304         spin_unlock(&budget->feedlock);
305         return status;
306 }
307
308 static int budget_stop_feed(struct dvb_demux_feed *feed)
309 {
310         struct dvb_demux *demux = feed->demux;
311         struct budget *budget = (struct budget *) demux->priv;
312         int status = 0;
313
314         dprintk(2, "budget: %p\n", budget);
315
316         spin_lock(&budget->feedlock);
317         if (--budget->feeding == 0)
318                 status = stop_ts_capture(budget);
319         spin_unlock(&budget->feedlock);
320         return status;
321 }
322
323 static int budget_register(struct budget *budget)
324 {
325         struct dvb_demux *dvbdemux = &budget->demux;
326         int ret;
327
328         dprintk(2, "budget: %p\n", budget);
329
330         dvbdemux->priv = (void *) budget;
331
332         dvbdemux->filternum = 256;
333         dvbdemux->feednum = 256;
334         dvbdemux->start_feed = budget_start_feed;
335         dvbdemux->stop_feed = budget_stop_feed;
336         dvbdemux->write_to_decoder = NULL;
337
338         dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
339                                       DMX_MEMORY_BASED_FILTERING);
340
341         dvb_dmx_init(&budget->demux);
342
343         budget->dmxdev.filternum = 256;
344         budget->dmxdev.demux = &dvbdemux->dmx;
345         budget->dmxdev.capabilities = 0;
346
347         dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
348
349         budget->hw_frontend.source = DMX_FRONTEND_0;
350
351         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
352
353         if (ret < 0)
354                 return ret;
355
356         budget->mem_frontend.source = DMX_MEMORY_FE;
357         ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
358         if (ret < 0)
359                 return ret;
360
361         ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
362         if (ret < 0)
363                 return ret;
364
365         dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
366
367         return 0;
368 }
369
370 static void budget_unregister(struct budget *budget)
371 {
372         struct dvb_demux *dvbdemux = &budget->demux;
373
374         dprintk(2, "budget: %p\n", budget);
375
376         dvb_net_release(&budget->dvb_net);
377
378         dvbdemux->dmx.close(&dvbdemux->dmx);
379         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
380         dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
381
382         dvb_dmxdev_release(&budget->dmxdev);
383         dvb_dmx_release(&budget->demux);
384 }
385
386 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
387                       struct saa7146_pci_extension_data *info,
388                       struct module *owner)
389 {
390         int ret = 0;
391         struct budget_info *bi = info->ext_priv;
392         int max_bufsize;
393         int height_mask;
394
395         memset(budget, 0, sizeof(struct budget));
396
397         dprintk(2, "dev: %p, budget: %p\n", dev, budget);
398
399         budget->card = bi;
400         budget->dev = (struct saa7146_dev *) dev;
401
402         if (budget->card->type == BUDGET_FS_ACTIVY) {
403                 budget->buffer_width = TS_WIDTH_ACTIVY;
404                 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
405                 height_mask = TS_HEIGHT_MASK_ACTIVY;
406         } else {
407                 budget->buffer_width = TS_WIDTH;
408                 max_bufsize = TS_MAX_BUFSIZE_K;
409                 height_mask = TS_HEIGHT_MASK;
410         }
411
412         if (dma_buffer_size < TS_MIN_BUFSIZE_K)
413                 dma_buffer_size = TS_MIN_BUFSIZE_K;
414         else if (dma_buffer_size > max_bufsize)
415                 dma_buffer_size = max_bufsize;
416
417         budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
418         budget->buffer_height &= height_mask;
419         budget->buffer_size = budget->buffer_height * budget->buffer_width;
420         budget->buffer_warning_threshold = budget->buffer_size * 80/100;
421         budget->buffer_warnings = 0;
422         budget->buffer_warning_time = jiffies;
423
424         dprintk(2, "%s: width = %d, height = %d\n",
425                 budget->dev->name, budget->buffer_width, budget->buffer_height);
426         printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
427
428         if ((ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name, owner, &budget->dev->pci->dev)) < 0) {
429                 return ret;
430         }
431
432         /* set dd1 stream a & b */
433         saa7146_write(dev, DD1_STREAM_B, 0x00000000);
434         saa7146_write(dev, MC2, (MASK_09 | MASK_25));
435         saa7146_write(dev, MC2, (MASK_10 | MASK_26));
436         saa7146_write(dev, DD1_INIT, 0x02000000);
437         saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
438
439         if (bi->type != BUDGET_FS_ACTIVY)
440                 budget->video_port = BUDGET_VIDEO_PORTB;
441         else
442                 budget->video_port = BUDGET_VIDEO_PORTA;
443         spin_lock_init(&budget->feedlock);
444         spin_lock_init(&budget->debilock);
445
446         /* the Siemens DVB needs this if you want to have the i2c chips
447            get recognized before the main driver is loaded */
448         if (bi->type != BUDGET_FS_ACTIVY)
449                 saa7146_write(dev, GPIO_CTRL, 0x500000);        /* GPIO 3 = 1 */
450
451 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
452         budget->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL;
453 #else
454         budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
455 #endif
456
457         strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
458
459         saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
460         strcpy(budget->i2c_adap.name, budget->card->name);
461
462         if (i2c_add_adapter(&budget->i2c_adap) < 0) {
463                 dvb_unregister_adapter(&budget->dvb_adapter);
464                 return -ENOMEM;
465         }
466
467         ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
468
469         if (NULL ==
470             (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt))) {
471                 ret = -ENOMEM;
472                 goto err;
473         }
474
475         saa7146_write(dev, PCI_BT_V1, 0x001c0000);
476         /* upload all */
477         saa7146_write(dev, GPIO_CTRL, 0x000000);
478
479         tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
480
481         /* frontend power on */
482         if (bi->type != BUDGET_FS_ACTIVY)
483                 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
484
485         if (budget_register(budget) == 0) {
486                 return 0;
487         }
488 err:
489         i2c_del_adapter(&budget->i2c_adap);
490
491         vfree(budget->grabbing);
492
493         dvb_unregister_adapter(&budget->dvb_adapter);
494
495         return ret;
496 }
497
498 void ttpci_budget_init_hooks(struct budget *budget)
499 {
500         if (budget->dvb_frontend && !budget->read_fe_status) {
501                 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
502                 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
503         }
504 }
505
506 int ttpci_budget_deinit(struct budget *budget)
507 {
508         struct saa7146_dev *dev = budget->dev;
509
510         dprintk(2, "budget: %p\n", budget);
511
512         budget_unregister(budget);
513
514         i2c_del_adapter(&budget->i2c_adap);
515
516         dvb_unregister_adapter(&budget->dvb_adapter);
517
518         tasklet_kill(&budget->vpe_tasklet);
519
520         saa7146_pgtable_free(dev->pci, &budget->pt);
521
522         vfree(budget->grabbing);
523
524         return 0;
525 }
526
527 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
528 {
529         struct budget *budget = (struct budget *) dev->ext_priv;
530
531         dprintk(8, "dev: %p, budget: %p\n", dev, budget);
532
533         if (*isr & MASK_10)
534                 tasklet_schedule(&budget->vpe_tasklet);
535 }
536
537 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
538 {
539         struct budget *budget = (struct budget *) dev->ext_priv;
540
541         spin_lock(&budget->feedlock);
542         budget->video_port = video_port;
543         if (budget->feeding) {
544                 stop_ts_capture(budget);
545                 start_ts_capture(budget);
546         }
547         spin_unlock(&budget->feedlock);
548 }
549
550 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
551 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
552 EXPORT_SYMBOL_GPL(ttpci_budget_init);
553 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
554 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
555 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
556 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
557 EXPORT_SYMBOL_GPL(budget_debug);
558
559 MODULE_LICENSE("GPL");