[PATCH] DVB: rework debugging in av7110
[linux-2.6/history.git] / drivers / media / dvb / ttpci / budget-core.c
blob0a8c897b494eb92461ea309d6944f61cac687ef6
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/
37 #include <linux/moduleparam.h>
39 #include "budget.h"
40 #include "ttpci-eeprom.h"
42 int budget_debug;
44 module_param_named(debug, budget_debug, int, 0644);
45 MODULE_PARM_DESC(budget_debug, "Turn on/off budget debugging (default:off).");
47 /****************************************************************************
48 * TT budget / WinTV Nova
49 ****************************************************************************/
51 static int stop_ts_capture(struct budget *budget)
53 dprintk(2, "budget: %p\n", budget);
55 if (--budget->feeding)
56 return budget->feeding;
58 saa7146_write(budget->dev, MC1, MASK_20); // DMA3 off
59 IER_DISABLE(budget->dev, MASK_10);
60 return 0;
64 static int start_ts_capture (struct budget *budget)
66 struct saa7146_dev *dev=budget->dev;
68 dprintk(2, "budget: %p\n", budget);
70 if (budget->feeding)
71 return ++budget->feeding;
73 saa7146_write(dev, MC1, MASK_20); // DMA3 off
75 memset(budget->grabbing, 0x00, TS_HEIGHT*TS_WIDTH);
77 saa7146_write(dev, PCI_BT_V1, 0x001c0000 |
78 (saa7146_read(dev, PCI_BT_V1) & ~0x001f0000));
80 budget->tsf=0xff;
81 budget->ttbp=0;
84 * Signal path on the Activy:
86 * tuner -> SAA7146 port A -> SAA7146 BRS -> SAA7146 DMA3 -> memory
88 * Since the tuner feeds 204 bytes packets into the SAA7146,
89 * DMA3 is configured to strip the trailing 16 FEC bytes:
90 * Pitch: 188, NumBytes3: 188, NumLines3: 1024
93 if (budget->card->type == BUDGET_FS_ACTIVY) {
94 saa7146_write(dev, DD1_INIT, 0x04000000);
95 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
96 saa7146_write(dev, BRS_CTRL, 0x00000000);
97 } else {
98 if (budget->video_port == BUDGET_VIDEO_PORTA) {
99 saa7146_write(dev, DD1_INIT, 0x06000200);
100 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
101 saa7146_write(dev, BRS_CTRL, 0x00000000);
102 } else {
103 saa7146_write(dev, DD1_INIT, 0x02000600);
104 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
105 saa7146_write(dev, BRS_CTRL, 0x60000000);
109 saa7146_write(dev, MC2, (MASK_08 | MASK_24));
110 mdelay(10);
112 saa7146_write(dev, BASE_ODD3, 0);
113 saa7146_write(dev, BASE_EVEN3, 0);
114 saa7146_write(dev, PROT_ADDR3, TS_WIDTH*TS_HEIGHT);
115 saa7146_write(dev, BASE_PAGE3, budget->pt.dma |ME1|0x90);
117 if (budget->card->type == BUDGET_FS_ACTIVY) {
118 saa7146_write(dev, PITCH3, TS_WIDTH/2);
119 saa7146_write(dev, NUM_LINE_BYTE3, ((TS_HEIGHT*2)<<16)|(TS_WIDTH/2));
120 } else {
121 saa7146_write(dev, PITCH3, TS_WIDTH);
122 saa7146_write(dev, NUM_LINE_BYTE3, (TS_HEIGHT<<16)|TS_WIDTH);
125 saa7146_write(dev, MC2, (MASK_04 | MASK_20));
126 saa7146_write(dev, MC1, (MASK_04 | MASK_20)); // DMA3 on
128 IER_ENABLE(budget->dev, MASK_10); // VPE
130 return ++budget->feeding;
134 static void vpeirq (unsigned long data)
136 struct budget *budget = (struct budget*) data;
137 u8 *mem = (u8 *)(budget->grabbing);
138 u32 olddma = budget->ttbp;
139 u32 newdma = saa7146_read(budget->dev, PCI_VDP3);
141 /* nearest lower position divisible by 188 */
142 newdma -= newdma % 188;
144 if (newdma >= TS_BUFLEN)
145 return;
147 budget->ttbp = newdma;
149 if(budget->feeding == 0 || newdma == olddma)
150 return;
152 if (newdma > olddma) { /* no wraparound, dump olddma..newdma */
153 dvb_dmx_swfilter_packets(&budget->demux,
154 mem+olddma, (newdma-olddma) / 188);
155 } else { /* wraparound, dump olddma..buflen and 0..newdma */
156 dvb_dmx_swfilter_packets(&budget->demux,
157 mem+olddma, (TS_BUFLEN-olddma) / 188);
158 dvb_dmx_swfilter_packets(&budget->demux,
159 mem, newdma / 188);
164 /****************************************************************************
165 * DVB API SECTION
166 ****************************************************************************/
168 static int budget_start_feed(struct dvb_demux_feed *feed)
170 struct dvb_demux *demux = feed->demux;
171 struct budget *budget = (struct budget*) demux->priv;
172 int status;
174 dprintk(2, "budget: %p\n", budget);
176 if (!demux->dmx.frontend)
177 return -EINVAL;
179 spin_lock(&budget->feedlock);
180 status = start_ts_capture (budget);
181 spin_unlock(&budget->feedlock);
182 return status;
185 static int budget_stop_feed(struct dvb_demux_feed *feed)
187 struct dvb_demux *demux = feed->demux;
188 struct budget *budget = (struct budget *) demux->priv;
189 int status;
191 dprintk(2, "budget: %p\n", budget);
193 spin_lock(&budget->feedlock);
194 status = stop_ts_capture (budget);
195 spin_unlock(&budget->feedlock);
196 return status;
200 static int budget_register(struct budget *budget)
202 struct dvb_demux *dvbdemux=&budget->demux;
203 int ret;
205 dprintk(2, "budget: %p\n", budget);
207 dvbdemux->priv = (void *) budget;
209 dvbdemux->filternum = 256;
210 dvbdemux->feednum = 256;
211 dvbdemux->start_feed = budget_start_feed;
212 dvbdemux->stop_feed = budget_stop_feed;
213 dvbdemux->write_to_decoder = NULL;
215 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING |
216 DMX_MEMORY_BASED_FILTERING);
218 dvb_dmx_init(&budget->demux);
220 budget->dmxdev.filternum = 256;
221 budget->dmxdev.demux = &dvbdemux->dmx;
222 budget->dmxdev.capabilities = 0;
224 dvb_dmxdev_init(&budget->dmxdev, budget->dvb_adapter);
226 budget->hw_frontend.source = DMX_FRONTEND_0;
228 ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend);
230 if (ret < 0)
231 return ret;
233 budget->mem_frontend.source = DMX_MEMORY_FE;
234 ret=dvbdemux->dmx.add_frontend (&dvbdemux->dmx,
235 &budget->mem_frontend);
236 if (ret<0)
237 return ret;
239 ret=dvbdemux->dmx.connect_frontend (&dvbdemux->dmx,
240 &budget->hw_frontend);
241 if (ret < 0)
242 return ret;
244 dvb_net_init(budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx);
246 return 0;
250 static void budget_unregister(struct budget *budget)
252 struct dvb_demux *dvbdemux=&budget->demux;
254 dprintk(2, "budget: %p\n", budget);
256 dvb_net_release(&budget->dvb_net);
258 dvbdemux->dmx.close(&dvbdemux->dmx);
259 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend);
260 dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend);
262 dvb_dmxdev_release(&budget->dmxdev);
263 dvb_dmx_release(&budget->demux);
266 /* fixme: can this be unified among all saa7146 based dvb cards? */
267 static int client_register(struct i2c_client *client)
269 struct saa7146_dev *dev = (struct saa7146_dev*)i2c_get_adapdata(client->adapter);
270 struct budget *budget = (struct budget*)dev->ext_priv;
272 if (client->driver->command)
273 return client->driver->command(client, FE_REGISTER, budget->dvb_adapter);
274 return 0;
277 static int client_unregister(struct i2c_client *client)
279 struct saa7146_dev *dev = (struct saa7146_dev*)i2c_get_adapdata(client->adapter);
280 struct budget *budget = (struct budget*)dev->ext_priv;
282 if (client->driver->command)
283 return client->driver->command(client, FE_UNREGISTER, budget->dvb_adapter);
284 return 0;
287 int ttpci_budget_init (struct budget *budget,
288 struct saa7146_dev* dev,
289 struct saa7146_pci_extension_data *info)
291 int length = TS_WIDTH*TS_HEIGHT;
292 int ret = 0;
293 struct budget_info *bi = info->ext_priv;
295 memset(budget, 0, sizeof(struct budget));
297 dprintk(2, "dev: %p, budget: %p\n", dev, budget);
299 budget->card = bi;
300 budget->dev = (struct saa7146_dev *) dev;
302 dvb_register_adapter(&budget->dvb_adapter, budget->card->name, THIS_MODULE);
304 /* set dd1 stream a & b */
305 saa7146_write(dev, DD1_STREAM_B, 0x00000000);
306 saa7146_write(dev, MC2, (MASK_09 | MASK_25));
307 saa7146_write(dev, MC2, (MASK_10 | MASK_26));
308 saa7146_write(dev, DD1_INIT, 0x02000000);
309 saa7146_write(dev, MC2, (MASK_09 | MASK_25 | MASK_10 | MASK_26));
311 if (bi->type != BUDGET_FS_ACTIVY)
312 budget->video_port = BUDGET_VIDEO_PORTB;
313 else
314 budget->video_port = BUDGET_VIDEO_PORTA;
315 spin_lock_init(&budget->feedlock);
317 /* the Siemens DVB needs this if you want to have the i2c chips
318 get recognized before the main driver is loaded */
319 if (bi->type != BUDGET_FS_ACTIVY)
320 saa7146_write(dev, GPIO_CTRL, 0x500000); /* GPIO 3 = 1 */
322 budget->i2c_adap = (struct i2c_adapter) {
323 .client_register = client_register,
324 .client_unregister = client_unregister,
325 #ifdef I2C_ADAP_CLASS_TV_DIGITAL
326 .class = I2C_ADAP_CLASS_TV_DIGITAL,
327 #else
328 .class = I2C_CLASS_TV_DIGITAL,
329 #endif
332 strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name));
334 saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120);
335 strcpy(budget->i2c_adap.name, budget->card->name);
337 if (i2c_add_adapter(&budget->i2c_adap) < 0) {
338 dvb_unregister_adapter (budget->dvb_adapter);
339 return -ENOMEM;
342 ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter->proposed_mac);
344 if( NULL == (budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci,length,&budget->pt))) {
345 ret = -ENOMEM;
346 goto err;
349 saa7146_write(dev, PCI_BT_V1, 0x001c0000);
350 /* upload all */
351 saa7146_write(dev, GPIO_CTRL, 0x000000);
353 tasklet_init (&budget->vpe_tasklet, vpeirq, (unsigned long) budget);
355 /* frontend power on */
356 if (bi->type == BUDGET_FS_ACTIVY)
357 saa7146_setgpio(dev, 1, SAA7146_GPIO_OUTHI);
358 else
359 saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI);
361 if (budget_register(budget) == 0) {
362 return 0;
364 err:
365 i2c_del_adapter(&budget->i2c_adap);
367 if (budget->grabbing)
368 vfree(budget->grabbing);
370 dvb_unregister_adapter (budget->dvb_adapter);
372 return ret;
376 int ttpci_budget_deinit (struct budget *budget)
378 struct saa7146_dev *dev = budget->dev;
380 dprintk(2, "budget: %p\n", budget);
382 budget_unregister (budget);
384 i2c_del_adapter(&budget->i2c_adap);
386 dvb_unregister_adapter (budget->dvb_adapter);
388 tasklet_kill (&budget->vpe_tasklet);
390 saa7146_pgtable_free (dev->pci, &budget->pt);
392 vfree (budget->grabbing);
394 return 0;
397 void ttpci_budget_irq10_handler (struct saa7146_dev* dev, u32 *isr)
399 struct budget *budget = (struct budget*)dev->ext_priv;
401 dprintk(8, "dev: %p, budget: %p\n",dev,budget);
403 if (*isr & MASK_10)
404 tasklet_schedule (&budget->vpe_tasklet);
407 void ttpci_budget_set_video_port(struct saa7146_dev* dev, int video_port)
409 struct budget *budget = (struct budget*)dev->ext_priv;
411 spin_lock(&budget->feedlock);
412 budget->video_port = video_port;
413 if (budget->feeding) {
414 int oldfeeding = budget->feeding;
415 budget->feeding = 1;
416 stop_ts_capture(budget);
417 start_ts_capture(budget);
418 budget->feeding = oldfeeding;
420 spin_unlock(&budget->feedlock);
425 EXPORT_SYMBOL_GPL(ttpci_budget_init);
426 EXPORT_SYMBOL_GPL(ttpci_budget_deinit);
427 EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler);
428 EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port);
429 EXPORT_SYMBOL_GPL(budget_debug);
431 MODULE_LICENSE("GPL");