s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / media / pci / ttpci / budget-core.c
blobe9674b40007c17098b3607282017bea2ef1aba3f
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/
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_KNC1C_TDA10024:
114 case BUDGET_KNC1CP_MK3:
115 if (budget->video_port == BUDGET_VIDEO_PORTA) {
116 saa7146_write(dev, DD1_INIT, 0x06000200);
117 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
118 saa7146_write(dev, BRS_CTRL, 0x00000000);
119 } else {
120 saa7146_write(dev, DD1_INIT, 0x00000600);
121 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
122 saa7146_write(dev, BRS_CTRL, 0x60000000);
124 break;
125 default:
126 if (budget->video_port == BUDGET_VIDEO_PORTA) {
127 saa7146_write(dev, DD1_INIT, 0x06000200);
128 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
129 saa7146_write(dev, BRS_CTRL, 0x00000000);
130 } else {
131 saa7146_write(dev, DD1_INIT, 0x02000600);
132 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
133 saa7146_write(dev, BRS_CTRL, 0x60000000);
137 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
138 mdelay(10);
140 saa7146_write(dev, BASE_ODD3, 0);
141 if (budget->buffer_size > budget->buffer_height * budget->buffer_width) {
142 // using odd/even buffers
143 saa7146_write(dev, BASE_EVEN3, budget->buffer_height * budget->buffer_width);
144 } else {
145 // using a single buffer
146 saa7146_write(dev, BASE_EVEN3, 0);
148 saa7146_write(dev, PROT_ADDR3, budget->buffer_size);
149 saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90);
151 saa7146_write(dev, PITCH3, budget->buffer_width);
152 saa7146_write(dev, NUM_LINE_BYTE3,
153 (budget->buffer_height << 16) | budget->buffer_width);
155 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
157 SAA7146_ISR_CLEAR(budget->dev, MASK_10); /* VPE */
158 SAA7146_IER_ENABLE(budget->dev, MASK_10); /* VPE */
159 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); /* DMA3 on */
161 return 0;
164 static int budget_read_fe_status(struct dvb_frontend *fe,
165 enum fe_status *status)
167 struct budget *budget = (struct budget *) fe->dvb->priv;
168 int synced;
169 int ret;
171 if (budget->read_fe_status)
172 ret = budget->read_fe_status(fe, status);
173 else
174 ret = -EINVAL;
176 if (!ret) {
177 synced = (*status & FE_HAS_LOCK);
178 if (synced != budget->fe_synced) {
179 budget->fe_synced = synced;
180 spin_lock(&budget->feedlock);
181 if (synced)
182 start_ts_capture(budget);
183 else
184 stop_ts_capture(budget);
185 spin_unlock(&budget->feedlock);
188 return ret;
191 static void vpeirq(unsigned long data)
193 struct budget *budget = (struct budget *) data;
194 u8 *mem = (u8 *) (budget->grabbing);
195 u32 olddma = budget->ttbp;
196 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
197 u32 count;
199 /* Ensure streamed PCI data is synced to CPU */
200 pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE);
202 /* nearest lower position divisible by 188 */
203 newdma -= newdma % 188;
205 if (newdma >= budget->buffer_size)
206 return;
208 budget->ttbp = newdma;
210 if (budget->feeding == 0 || newdma == olddma)
211 return;
213 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
214 count = newdma - olddma;
215 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
216 } else { /* wraparound, dump olddma..buflen and 0..newdma */
217 count = budget->buffer_size - olddma;
218 dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188);
219 count += newdma;
220 dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188);
223 if (count > budget->buffer_warning_threshold)
224 budget->buffer_warnings++;
226 if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) {
227 printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n",
228 budget->dev->name, __func__, budget->buffer_warnings, count);
229 budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT;
230 budget->buffer_warnings = 0;
235 static int ttpci_budget_debiread_nolock(struct budget *budget, u32 config,
236 int addr, int count, int nobusyloop)
238 struct saa7146_dev *saa = budget->dev;
239 int result;
241 result = saa7146_wait_for_debi_done(saa, nobusyloop);
242 if (result < 0)
243 return result;
245 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff));
246 saa7146_write(saa, DEBI_CONFIG, config);
247 saa7146_write(saa, DEBI_PAGE, 0);
248 saa7146_write(saa, MC2, (2 << 16) | 2);
250 result = saa7146_wait_for_debi_done(saa, nobusyloop);
251 if (result < 0)
252 return result;
254 result = saa7146_read(saa, DEBI_AD);
255 result &= (0xffffffffUL >> ((4 - count) * 8));
256 return result;
259 int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count,
260 int uselocks, int nobusyloop)
262 if (count > 4 || count <= 0)
263 return 0;
265 if (uselocks) {
266 unsigned long flags;
267 int result;
269 spin_lock_irqsave(&budget->debilock, flags);
270 result = ttpci_budget_debiread_nolock(budget, config, addr,
271 count, nobusyloop);
272 spin_unlock_irqrestore(&budget->debilock, flags);
273 return result;
275 return ttpci_budget_debiread_nolock(budget, config, addr,
276 count, nobusyloop);
279 static int ttpci_budget_debiwrite_nolock(struct budget *budget, u32 config,
280 int addr, int count, u32 value, int nobusyloop)
282 struct saa7146_dev *saa = budget->dev;
283 int result;
285 result = saa7146_wait_for_debi_done(saa, nobusyloop);
286 if (result < 0)
287 return result;
289 saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff));
290 saa7146_write(saa, DEBI_CONFIG, config);
291 saa7146_write(saa, DEBI_PAGE, 0);
292 saa7146_write(saa, DEBI_AD, value);
293 saa7146_write(saa, MC2, (2 << 16) | 2);
295 result = saa7146_wait_for_debi_done(saa, nobusyloop);
296 return result < 0 ? result : 0;
299 int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr,
300 int count, u32 value, int uselocks, int nobusyloop)
302 if (count > 4 || count <= 0)
303 return 0;
305 if (uselocks) {
306 unsigned long flags;
307 int result;
309 spin_lock_irqsave(&budget->debilock, flags);
310 result = ttpci_budget_debiwrite_nolock(budget, config, addr,
311 count, value, nobusyloop);
312 spin_unlock_irqrestore(&budget->debilock, flags);
313 return result;
315 return ttpci_budget_debiwrite_nolock(budget, config, addr,
316 count, value, nobusyloop);
320 /****************************************************************************
321 * DVB API SECTION
322 ****************************************************************************/
324 static int budget_start_feed(struct dvb_demux_feed *feed)
326 struct dvb_demux *demux = feed->demux;
327 struct budget *budget = (struct budget *) demux->priv;
328 int status = 0;
330 dprintk(2, "budget: %p\n", budget);
332 if (!demux->dmx.frontend)
333 return -EINVAL;
335 spin_lock(&budget->feedlock);
336 feed->pusi_seen = 0; /* have a clean section start */
337 if (budget->feeding++ == 0)
338 status = start_ts_capture(budget);
339 spin_unlock(&budget->feedlock);
340 return status;
343 static int budget_stop_feed(struct dvb_demux_feed *feed)
345 struct dvb_demux *demux = feed->demux;
346 struct budget *budget = (struct budget *) demux->priv;
347 int status = 0;
349 dprintk(2, "budget: %p\n", budget);
351 spin_lock(&budget->feedlock);
352 if (--budget->feeding == 0)
353 status = stop_ts_capture(budget);
354 spin_unlock(&budget->feedlock);
355 return status;
358 static int budget_register(struct budget *budget)
360 struct dvb_demux *dvbdemux = &budget->demux;
361 int ret;
363 dprintk(2, "budget: %p\n", budget);
365 dvbdemux->priv = (void *) budget;
367 dvbdemux->filternum = 256;
368 dvbdemux->feednum = 256;
369 dvbdemux->start_feed = budget_start_feed;
370 dvbdemux->stop_feed = budget_stop_feed;
371 dvbdemux->write_to_decoder = NULL;
373 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
374 DMX_MEMORY_BASED_FILTERING);
376 dvb_dmx_init(&budget->demux);
378 budget->dmxdev.filternum = 256;
379 budget->dmxdev.demux = &dvbdemux->dmx;
380 budget->dmxdev.capabilities = 0;
382 dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter);
384 budget->hw_frontend.source = DMX_FRONTEND_0;
386 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
388 if (ret < 0)
389 return ret;
391 budget->mem_frontend.source = DMX_MEMORY_FE;
392 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend);
393 if (ret < 0)
394 return ret;
396 ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend);
397 if (ret < 0)
398 return ret;
400 dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
402 return 0;
405 static void budget_unregister(struct budget *budget)
407 struct dvb_demux *dvbdemux = &budget->demux;
409 dprintk(2, "budget: %p\n", budget);
411 dvb_net_release(&budget->dvb_net);
413 dvbdemux->dmx.close(&dvbdemux->dmx);
414 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
415 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
417 dvb_dmxdev_release(&budget->dmxdev);
418 dvb_dmx_release(&budget->demux);
421 int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev,
422 struct saa7146_pci_extension_data *info,
423 struct module *owner, short *adapter_nums)
425 int ret = 0;
426 struct budget_info *bi = info->ext_priv;
427 int max_bufsize;
428 int height_mask;
430 memset(budget, 0, sizeof(struct budget));
432 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
434 budget->card = bi;
435 budget->dev = (struct saa7146_dev *) dev;
437 switch(budget->card->type) {
438 case BUDGET_FS_ACTIVY:
439 budget->buffer_width = TS_WIDTH_ACTIVY;
440 max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY;
441 height_mask = TS_HEIGHT_MASK_ACTIVY;
442 break;
444 case BUDGET_KNC1C:
445 case BUDGET_KNC1CP:
446 case BUDGET_CIN1200C:
447 case BUDGET_KNC1C_MK3:
448 case BUDGET_KNC1C_TDA10024:
449 case BUDGET_KNC1CP_MK3:
450 case BUDGET_CIN1200C_MK3:
451 budget->buffer_width = TS_WIDTH_DVBC;
452 max_bufsize = TS_MAX_BUFSIZE_K_DVBC;
453 height_mask = TS_HEIGHT_MASK_DVBC;
454 break;
456 default:
457 budget->buffer_width = TS_WIDTH;
458 max_bufsize = TS_MAX_BUFSIZE_K;
459 height_mask = TS_HEIGHT_MASK;
462 if (dma_buffer_size < TS_MIN_BUFSIZE_K)
463 dma_buffer_size = TS_MIN_BUFSIZE_K;
464 else if (dma_buffer_size > max_bufsize)
465 dma_buffer_size = max_bufsize;
467 budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width;
468 if (budget->buffer_height > 0xfff) {
469 budget->buffer_height /= 2;
470 budget->buffer_height &= height_mask;
471 budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width;
472 } else {
473 budget->buffer_height &= height_mask;
474 budget->buffer_size = budget->buffer_height * budget->buffer_width;
476 budget->buffer_warning_threshold = budget->buffer_size * 80/100;
477 budget->buffer_warnings = 0;
478 budget->buffer_warning_time = jiffies;
480 dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n",
481 budget->dev->name,
482 budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single",
483 budget->buffer_width, budget->buffer_height);
484 printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size);
486 ret = dvb_register_adapter(&budget->dvb_adapter, budget->card->name,
487 owner, &budget->dev->pci->dev, adapter_nums);
488 if (ret < 0)
489 return ret;
491 /* set dd1 stream a & b */
492 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
493 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
494 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
495 saa7146_write(dev, DD1_INIT, 0x02000000);
496 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
498 if (bi->type != BUDGET_FS_ACTIVY)
499 budget->video_port = BUDGET_VIDEO_PORTB;
500 else
501 budget->video_port = BUDGET_VIDEO_PORTA;
502 spin_lock_init(&budget->feedlock);
503 spin_lock_init(&budget->debilock);
505 /* the Siemens DVB needs this if you want to have the i2c chips
506 get recognized before the main driver is loaded */
507 if (bi->type != BUDGET_FS_ACTIVY)
508 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
510 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
512 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
513 strcpy(budget->i2c_adap.name, budget->card->name);
515 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
516 ret = -ENOMEM;
517 goto err_dvb_unregister;
520 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac);
522 budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt);
523 if (NULL == budget->grabbing) {
524 ret = -ENOMEM;
525 goto err_del_i2c;
528 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
529 /* upload all */
530 saa7146_write(dev, GPIO_CTRL, 0x000000);
532 tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
534 /* frontend power on */
535 if (bi->type != BUDGET_FS_ACTIVY)
536 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
538 if ((ret = budget_register(budget)) == 0)
539 return 0; /* Everything OK */
541 /* An error occurred, cleanup resources */
542 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
544 err_del_i2c:
545 i2c_del_adapter(&budget->i2c_adap);
547 err_dvb_unregister:
548 dvb_unregister_adapter(&budget->dvb_adapter);
550 return ret;
553 void ttpci_budget_init_hooks(struct budget *budget)
555 if (budget->dvb_frontend && !budget->read_fe_status) {
556 budget->read_fe_status = budget->dvb_frontend->ops.read_status;
557 budget->dvb_frontend->ops.read_status = budget_read_fe_status;
561 int ttpci_budget_deinit(struct budget *budget)
563 struct saa7146_dev *dev = budget->dev;
565 dprintk(2, "budget: %p\n", budget);
567 budget_unregister(budget);
569 tasklet_kill(&budget->vpe_tasklet);
571 saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt);
573 i2c_del_adapter(&budget->i2c_adap);
575 dvb_unregister_adapter(&budget->dvb_adapter);
577 return 0;
580 void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr)
582 struct budget *budget = (struct budget *) dev->ext_priv;
584 dprintk(8, "dev: %p, budget: %p\n", dev, budget);
586 if (*isr & MASK_10)
587 tasklet_schedule(&budget->vpe_tasklet);
590 void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port)
592 struct budget *budget = (struct budget *) dev->ext_priv;
594 spin_lock(&budget->feedlock);
595 budget->video_port = video_port;
596 if (budget->feeding) {
597 stop_ts_capture(budget);
598 start_ts_capture(budget);
600 spin_unlock(&budget->feedlock);
603 EXPORT_SYMBOL_GPL(ttpci_budget_debiread);
604 EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite);
605 EXPORT_SYMBOL_GPL(ttpci_budget_init);
606 EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks);
607 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
608 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
609 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
610 EXPORT_SYMBOL_GPL(budget_debug);
612 MODULE_LICENSE("GPL");