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