powerpc: use consistent types in mktree
[zen-stable.git] / drivers / media / dvb / ttpci / budget-core.c
blobba18e56d5f11295bb8bb79b9ce7d4e5d4a608b4b
1 /*
2 * budget-core.c: driver for the SAA7146 based Budget DVB cards
4 * Compiled from various sources by Michael Hunold <michael@mihu.de>
6 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
8 * Copyright (C) 1999-2002 Ralph Metzler
9 * & Marcus Metzler for convergence integrated media GmbH
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
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.
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.
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
34 * the project's page is at http://www.linuxtv.org/dvb/
38 #include "budget.h"
39 #include "ttpci-eeprom.h"
41 #define TS_WIDTH (2 * TS_SIZE)
42 #define TS_WIDTH_ACTIVY TS_SIZE
43 #define TS_WIDTH_DVBC TS_SIZE
44 #define TS_HEIGHT_MASK 0xf00
45 #define TS_HEIGHT_MASK_ACTIVY 0xc00
46 #define TS_HEIGHT_MASK_DVBC 0xe00
47 #define TS_MIN_BUFSIZE_K 188
48 #define TS_MAX_BUFSIZE_K 1410
49 #define TS_MAX_BUFSIZE_K_ACTIVY 564
50 #define TS_MAX_BUFSIZE_K_DVBC 1316
51 #define BUFFER_WARNING_WAIT (30*HZ)
53 int budget_debug;
54 static int dma_buffer_size = TS_MIN_BUFSIZE_K;
55 module_param_named(debug, budget_debug, int, 0644);
56 module_param_named(bufsize, dma_buffer_size, int, 0444);
57 MODULE_PARM_DESC(debug, "Turn on/off budget debugging (default:off).");
58 MODULE_PARM_DESC(bufsize, "DMA buffer size in KB, default: 188, min: 188, max: 1410 (Activy: 564)");
60 /****************************************************************************
61 * TT budget / WinTV Nova
62 ****************************************************************************/
64 static int stop_ts_capture(struct budget *budget)
66 dprintk(2, "budget: %p\n", budget);
68 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
69 SAA7146_IER_DISABLE(budget->dev, MASK_10);
70 return 0;
73 static int start_ts_capture(struct budget *budget)
75 struct saa7146_dev *dev = budget->dev;
77 dprintk(2, "budget: %p\n", budget);
79 if (!budget->feeding || !budget->fe_synced)
80 return 0;
82 saa7146_write(dev, MC1, MASK_20); // DMA3 off
84 memset(budget->grabbing, 0x00, budget->buffer_size);
86 saa7146_write(dev, PCI_BT_V1, 0x001c0000 | (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
88 budget->ttbp = 0;
91 * Signal path on the Activy:
93 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
95 * Since the tuner feeds 204 bytes packets into the SAA7146,
96 * DMA3 is configured to strip the trailing 16 FEC bytes:
97 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
100 switch(budget->card->type) {
101 case BUDGET_FS_ACTIVY:
102 saa7146_write(dev, DD1_INIT, 0x04000000);
103 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
104 saa7146_write(dev, BRS_CTRL, 0x00000000);
105 break;
106 case BUDGET_PATCH:
107 saa7146_write(dev, DD1_INIT, 0x00000200);
108 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
109 saa7146_write(dev, BRS_CTRL, 0x60000000);
110 break;
111 case BUDGET_CIN1200C_MK3:
112 case BUDGET_KNC1C_MK3:
113 case BUDGET_KNC1CP_MK3:
114 if (budget->video_port == BUDGET_VIDEO_PORTA) {
115 saa7146_write(dev, DD1_INIT, 0x06000200);
116 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
117 saa7146_write(dev, BRS_CTRL, 0x00000000);
118 } else {
119 saa7146_write(dev, DD1_INIT, 0x00000600);
120 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
121 saa7146_write(dev, BRS_CTRL, 0x60000000);
123 break;
124 default:
125 if (budget->video_port == BUDGET_VIDEO_PORTA) {
126 saa7146_write(dev, DD1_INIT, 0x06000200);
127 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
128 saa7146_write(dev, BRS_CTRL, 0x00000000);
129 } else {
130 saa7146_write(dev, DD1_INIT, 0x02000600);
131 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
132 saa7146_write(dev, BRS_CTRL, 0x60000000);
136 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
137 mdelay(10);
139 saa7146_write(dev, BASE_ODD3, 0);
140 if (budget->buffer_size > budget->buffer_height * budget->buffer_width) {
141 // using odd/even buffers
142 saa7146_write(dev, BASE_EVEN3, budget->buffer_height * budget->buffer_width);
143 } else {
144 // using a single buffer
145 saa7146_write(dev, BASE_EVEN3, 0);
147 saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
148 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
150 saa7146_write(dev, PITCH3, budget->buffer_width);
151 saa7146_write(dev, NUM_LINE_BYTE3,
152 (budget->buffer_height << 16) | budget->buffer_width);
154 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
156 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
157 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
158 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
160 return 0;
163 static int budget_read_fe_status(struct dvb_frontend *fe, fe_status_t *status)
165 struct budget *budget = (struct budget *) fe->dvb->priv;
166 int synced;
167 int ret;
169 if (budget->read_fe_status)
170 ret = budget->read_fe_status(fe, status);
171 else
172 ret = -EINVAL;
174 if (!ret) {
175 synced = (*status & FE_HAS_LOCK);
176 if (synced != budget->fe_synced) {
177 budget->fe_synced = synced;
178 spin_lock(&budget->feedlock);
179 if (synced)
180 start_ts_capture(budget);
181 else
182 stop_ts_capture(budget);
183 spin_unlock(&budget->feedlock);
186 return ret;
189 static void vpeirq(unsigned long data)
191 struct budget *budget = (struct budget *) data;
192 u8 *mem = (u8 *) (budget->grabbing);
193 u32 olddma = budget->ttbp;
194 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
195 u32 count;
197 /* Ensure streamed PCI data is synced to CPU */
198 pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
200 /* nearest lower position divisible by 188 */
201 newdma -= newdma % 188;
203 if (newdma >= budget->buffer_size)
204 return;
206 budget->ttbp = newdma;
208 if (budget->feeding == 0 || newdma == olddma)
209 return;
211 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
212 count = newdma - olddma;
213 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
214 } else { /* wraparound, dump olddma..buflen and 0..newdma */
215 count = budget->buffer_size - olddma;
216 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
217 count += newdma;
218 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
221 if (count > budget->buffer_warning_threshold)
222 budget->buffer_warnings++;
224 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
225 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
226 budget->dev->name, __func__, budget->buffer_warnings, count);
227 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
228 budget->buffer_warnings = 0;
233 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
234 int uselocks, int nobusyloop)
236 struct saa7146_dev *saa = budget->dev;
237 int result = 0;
238 unsigned long flags = 0;
240 if (count > 4 || count <= 0)
241 return 0;
243 if (uselocks)
244 spin_lock_irqsave(&budget->debilock, flags);
246 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
247 if (uselocks)
248 spin_unlock_irqrestore(&budget->debilock, flags);
249 return result;
252 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
253 saa7146_write(saa, DEBI_CONFIG, config);
254 saa7146_write(saa, DEBI_PAGE, 0);
255 saa7146_write(saa, MC2, (2 << 16) | 2);
257 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
258 if (uselocks)
259 spin_unlock_irqrestore(&budget->debilock, flags);
260 return result;
263 result = saa7146_read(saa, DEBI_AD);
264 result &= (0xffffffffUL >> ((4 - count) * 8));
266 if (uselocks)
267 spin_unlock_irqrestore(&budget->debilock, flags);
269 return result;
272 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
273 int count, u32 value, int uselocks, int nobusyloop)
275 struct saa7146_dev *saa = budget->dev;
276 unsigned long flags = 0;
277 int result;
279 if (count > 4 || count <= 0)
280 return 0;
282 if (uselocks)
283 spin_lock_irqsave(&budget->debilock, flags);
285 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
286 if (uselocks)
287 spin_unlock_irqrestore(&budget->debilock, flags);
288 return result;
291 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
292 saa7146_write(saa, DEBI_CONFIG, config);
293 saa7146_write(saa, DEBI_PAGE, 0);
294 saa7146_write(saa, DEBI_AD, value);
295 saa7146_write(saa, MC2, (2 << 16) | 2);
297 if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) {
298 if (uselocks)
299 spin_unlock_irqrestore(&budget->debilock, flags);
300 return result;
303 if (uselocks)
304 spin_unlock_irqrestore(&budget->debilock, flags);
305 return 0;
309 /****************************************************************************
310 * DVB API SECTION
311 ****************************************************************************/
313 static int budget_start_feed(struct dvb_demux_feed *feed)
315 struct dvb_demux *demux = feed->demux;
316 struct budget *budget = (struct budget *) demux->priv;
317 int status = 0;
319 dprintk(2, "budget: %p\n", budget);
321 if (!demux->dmx.frontend)
322 return -EINVAL;
324 spin_lock(&budget->feedlock);
325 feed->pusi_seen = 0; /* have a clean section start */
326 if (budget->feeding++ == 0)
327 status = start_ts_capture(budget);
328 spin_unlock(&budget->feedlock);
329 return status;
332 static int budget_stop_feed(struct dvb_demux_feed *feed)
334 struct dvb_demux *demux = feed->demux;
335 struct budget *budget = (struct budget *) demux->priv;
336 int status = 0;
338 dprintk(2, "budget: %p\n", budget);
340 spin_lock(&budget->feedlock);
341 if (--budget->feeding == 0)
342 status = stop_ts_capture(budget);
343 spin_unlock(&budget->feedlock);
344 return status;
347 static int budget_register(struct budget *budget)
349 struct dvb_demux *dvbdemux = &budget->demux;
350 int ret;
352 dprintk(2, "budget: %p\n", budget);
354 dvbdemux->priv = (void *) budget;
356 dvbdemux->filternum = 256;
357 dvbdemux->feednum = 256;
358 dvbdemux->start_feed = budget_start_feed;
359 dvbdemux->stop_feed = budget_stop_feed;
360 dvbdemux->write_to_decoder = NULL;
362 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
363 DMX_MEMORY_BASED_FILTERING);
365 dvb_dmx_init(&budget->demux);
367 budget->dmxdev.filternum = 256;
368 budget->dmxdev.demux = &dvbdemux->dmx;
369 budget->dmxdev.capabilities = 0;
371 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
373 budget->hw_frontend.source = DMX_FRONTEND_0;
375 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
377 if (ret < 0)
378 return ret;
380 budget->mem_frontend.source = DMX_MEMORY_FE;
381 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
382 if (ret < 0)
383 return ret;
385 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
386 if (ret < 0)
387 return ret;
389 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
391 return 0;
394 static void budget_unregister(struct budget *budget)
396 struct dvb_demux *dvbdemux = &budget->demux;
398 dprintk(2, "budget: %p\n", budget);
400 dvb_net_release(&budget->dvb_net);
402 dvbdemux->dmx.close(&dvbdemux->dmx);
403 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
404 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
406 dvb_dmxdev_release(&budget->dmxdev);
407 dvb_dmx_release(&budget->demux);
410 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
411 struct saa7146_pci_extension_data *info,
412 struct module *owner, short *adapter_nums)
414 int ret = 0;
415 struct budget_info *bi = info->ext_priv;
416 int max_bufsize;
417 int height_mask;
419 memset(budget, 0, sizeof(struct budget));
421 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
423 budget->card = bi;
424 budget->dev = (struct saa7146_dev *) dev;
426 switch(budget->card->type) {
427 case BUDGET_FS_ACTIVY:
428 budget->buffer_width = TS_WIDTH_ACTIVY;
429 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
430 height_mask = TS_HEIGHT_MASK_ACTIVY;
431 break;
433 case BUDGET_KNC1C:
434 case BUDGET_KNC1CP:
435 case BUDGET_CIN1200C:
436 case BUDGET_KNC1C_MK3:
437 case BUDGET_KNC1CP_MK3:
438 case BUDGET_CIN1200C_MK3:
439 budget->buffer_width = TS_WIDTH_DVBC;
440 max_bufsize = TS_MAX_BUFSIZE_K_DVBC;
441 height_mask = TS_HEIGHT_MASK_DVBC;
442 break;
444 default:
445 budget->buffer_width = TS_WIDTH;
446 max_bufsize = TS_MAX_BUFSIZE_K;
447 height_mask = TS_HEIGHT_MASK;
450 if (dma_buffer_size < TS_MIN_BUFSIZE_K)
451 dma_buffer_size = TS_MIN_BUFSIZE_K;
452 else if (dma_buffer_size > max_bufsize)
453 dma_buffer_size = max_bufsize;
455 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
456 if (budget->buffer_height > 0xfff) {
457 budget->buffer_height /= 2;
458 budget->buffer_height &= height_mask;
459 budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width;
460 } else {
461 budget->buffer_height &= height_mask;
462 budget->buffer_size = budget->buffer_height * budget->buffer_width;
464 budget->buffer_warning_threshold = budget->buffer_size * 80/100;
465 budget->buffer_warnings = 0;
466 budget->buffer_warning_time = jiffies;
468 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n",
469 budget->dev->name,
470 budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single",
471 budget->buffer_width, budget->buffer_height);
472 printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
474 ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name,
475 owner, &budget->dev->pci->dev, adapter_nums);
476 if (ret < 0)
477 return ret;
479 /* set dd1 stream a & b */
480 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
481 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
482 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
483 saa7146_write(dev, DD1_INIT, 0x02000000);
484 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
486 if (bi->type != BUDGET_FS_ACTIVY)
487 budget->video_port = BUDGET_VIDEO_PORTB;
488 else
489 budget->video_port = BUDGET_VIDEO_PORTA;
490 spin_lock_init(&budget->feedlock);
491 spin_lock_init(&budget->debilock);
493 /* the Siemens DVB needs this if you want to have the i2c chips
494 get recognized before the main driver is loaded */
495 if (bi->type != BUDGET_FS_ACTIVY)
496 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
498 budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL;
500 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
502 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
503 strcpy(budget->i2c_adap.name, budget->card->name);
505 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
506 ret = -ENOMEM;
507 goto err_dvb_unregister;
510 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
512 budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt);
513 if (NULL == budget->grabbing) {
514 ret = -ENOMEM;
515 goto err_del_i2c;
518 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
519 /* upload all */
520 saa7146_write(dev, GPIO_CTRL, 0x000000);
522 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
524 /* frontend power on */
525 if (bi->type != BUDGET_FS_ACTIVY)
526 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
528 if ((ret = budget_register(budget)) == 0)
529 return 0; /* Everything OK */
531 /* An error occurred, cleanup resources */
532 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
534 err_del_i2c:
535 i2c_del_adapter(&budget->i2c_adap);
537 err_dvb_unregister:
538 dvb_unregister_adapter(&budget->dvb_adapter);
540 return ret;
543 void ttpci_budget_init_hooks(struct budget *budget)
545 if (budget->dvb_frontend && !budget->read_fe_status) {
546 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
547 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
551 int ttpci_budget_deinit(struct budget *budget)
553 struct saa7146_dev *dev = budget->dev;
555 dprintk(2, "budget: %p\n", budget);
557 budget_unregister(budget);
559 tasklet_kill(&budget->vpe_tasklet);
561 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
563 i2c_del_adapter(&budget->i2c_adap);
565 dvb_unregister_adapter(&budget->dvb_adapter);
567 return 0;
570 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
572 struct budget *budget = (struct budget *) dev->ext_priv;
574 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
576 if (*isr & MASK_10)
577 tasklet_schedule(&budget->vpe_tasklet);
580 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
582 struct budget *budget = (struct budget *) dev->ext_priv;
584 spin_lock(&budget->feedlock);
585 budget->video_port = video_port;
586 if (budget->feeding) {
587 stop_ts_capture(budget);
588 start_ts_capture(budget);
590 spin_unlock(&budget->feedlock);
593 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
594 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
595 EXPORT_SYMBOL_GPL(ttpci_budget_init);
596 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
597 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
598 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
599 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
600 EXPORT_SYMBOL_GPL(budget_debug);
602 MODULE_LICENSE("GPL");