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; |
Hartmut Birr | aa323ac | 2007-04-21 19:37:17 -0300 | [diff] [blame] | 112 | case BUDGET_CIN1200C_MK3: |
| 113 | case BUDGET_KNC1C_MK3: |
| 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); |
| 123 | } |
| 124 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | saa7146_write(dev, MC2, (MASK_08 | MASK_24)); |
| 138 | mdelay(10); |
| 139 | |
| 140 | saa7146_write(dev, BASE_ODD3, 0); |
Hartmut Birr | fd9c66e | 2007-04-21 19:17:49 -0300 | [diff] [blame] | 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); |
| 147 | } |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 148 | saa7146_write(dev, PROT_ADDR3, budget->buffer_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | saa7146_write(dev, BASE_PAGE3, budget->pt.dma | ME1 | 0x90); |
| 150 | |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 151 | saa7146_write(dev, PITCH3, budget->buffer_width); |
| 152 | saa7146_write(dev, NUM_LINE_BYTE3, |
| 153 | (budget->buffer_height << 16) | budget->buffer_width); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
| 155 | saa7146_write(dev, MC2, (MASK_04 | MASK_20)); |
| 156 | |
| 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 */ |
| 160 | |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | static int budget_read_fe_status(struct dvb_frontend *fe, fe_status_t *status) |
| 165 | { |
| 166 | struct budget *budget = (struct budget *) fe->dvb->priv; |
| 167 | int synced; |
| 168 | int ret; |
| 169 | |
| 170 | if (budget->read_fe_status) |
| 171 | ret = budget->read_fe_status(fe, status); |
| 172 | else |
| 173 | ret = -EINVAL; |
| 174 | |
| 175 | if (!ret) { |
| 176 | synced = (*status & FE_HAS_LOCK); |
| 177 | if (synced != budget->fe_synced) { |
| 178 | budget->fe_synced = synced; |
| 179 | spin_lock(&budget->feedlock); |
| 180 | if (synced) |
| 181 | start_ts_capture(budget); |
| 182 | else |
| 183 | stop_ts_capture(budget); |
| 184 | spin_unlock(&budget->feedlock); |
| 185 | } |
| 186 | } |
| 187 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | static void vpeirq(unsigned long data) |
| 191 | { |
| 192 | struct budget *budget = (struct budget *) data; |
| 193 | u8 *mem = (u8 *) (budget->grabbing); |
| 194 | u32 olddma = budget->ttbp; |
| 195 | u32 newdma = saa7146_read(budget->dev, PCI_VDP3); |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 196 | u32 count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 198 | /* Ensure streamed PCI data is synced to CPU */ |
| 199 | pci_dma_sync_sg_for_cpu(budget->dev->pci, budget->pt.slist, budget->pt.nents, PCI_DMA_FROMDEVICE); |
| 200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | /* nearest lower position divisible by 188 */ |
| 202 | newdma -= newdma % 188; |
| 203 | |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 204 | if (newdma >= budget->buffer_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | return; |
| 206 | |
| 207 | budget->ttbp = newdma; |
| 208 | |
| 209 | if (budget->feeding == 0 || newdma == olddma) |
| 210 | return; |
| 211 | |
| 212 | if (newdma > olddma) { /* no wraparound, dump olddma..newdma */ |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 213 | count = newdma - olddma; |
| 214 | dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } else { /* wraparound, dump olddma..buflen and 0..newdma */ |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 216 | count = budget->buffer_size - olddma; |
| 217 | dvb_dmx_swfilter_packets(&budget->demux, mem + olddma, count / 188); |
| 218 | count += newdma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | dvb_dmx_swfilter_packets(&budget->demux, mem, newdma / 188); |
| 220 | } |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 221 | |
| 222 | if (count > budget->buffer_warning_threshold) |
| 223 | budget->buffer_warnings++; |
| 224 | |
| 225 | if (budget->buffer_warnings && time_after(jiffies, budget->buffer_warning_time)) { |
| 226 | printk("%s %s: used %d times >80%% of buffer (%u bytes now)\n", |
| 227 | budget->dev->name, __FUNCTION__, budget->buffer_warnings, count); |
| 228 | budget->buffer_warning_time = jiffies + BUFFER_WARNING_WAIT; |
| 229 | budget->buffer_warnings = 0; |
| 230 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
| 234 | int ttpci_budget_debiread(struct budget *budget, u32 config, int addr, int count, |
| 235 | int uselocks, int nobusyloop) |
| 236 | { |
| 237 | struct saa7146_dev *saa = budget->dev; |
| 238 | int result = 0; |
| 239 | unsigned long flags = 0; |
| 240 | |
| 241 | if (count > 4 || count <= 0) |
| 242 | return 0; |
| 243 | |
| 244 | if (uselocks) |
| 245 | spin_lock_irqsave(&budget->debilock, flags); |
| 246 | |
| 247 | if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) { |
| 248 | if (uselocks) |
| 249 | spin_unlock_irqrestore(&budget->debilock, flags); |
| 250 | return result; |
| 251 | } |
| 252 | |
| 253 | saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x10000 | (addr & 0xffff)); |
| 254 | saa7146_write(saa, DEBI_CONFIG, config); |
| 255 | saa7146_write(saa, DEBI_PAGE, 0); |
| 256 | saa7146_write(saa, MC2, (2 << 16) | 2); |
| 257 | |
| 258 | if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) { |
| 259 | if (uselocks) |
| 260 | spin_unlock_irqrestore(&budget->debilock, flags); |
| 261 | return result; |
| 262 | } |
| 263 | |
| 264 | result = saa7146_read(saa, DEBI_AD); |
| 265 | result &= (0xffffffffUL >> ((4 - count) * 8)); |
| 266 | |
| 267 | if (uselocks) |
| 268 | spin_unlock_irqrestore(&budget->debilock, flags); |
| 269 | |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | int ttpci_budget_debiwrite(struct budget *budget, u32 config, int addr, |
| 274 | int count, u32 value, int uselocks, int nobusyloop) |
| 275 | { |
| 276 | struct saa7146_dev *saa = budget->dev; |
| 277 | unsigned long flags = 0; |
| 278 | int result; |
| 279 | |
| 280 | if (count > 4 || count <= 0) |
| 281 | return 0; |
| 282 | |
| 283 | if (uselocks) |
| 284 | spin_lock_irqsave(&budget->debilock, flags); |
| 285 | |
| 286 | if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) { |
| 287 | if (uselocks) |
| 288 | spin_unlock_irqrestore(&budget->debilock, flags); |
| 289 | return result; |
| 290 | } |
| 291 | |
| 292 | saa7146_write(saa, DEBI_COMMAND, (count << 17) | 0x00000 | (addr & 0xffff)); |
| 293 | saa7146_write(saa, DEBI_CONFIG, config); |
| 294 | saa7146_write(saa, DEBI_PAGE, 0); |
| 295 | saa7146_write(saa, DEBI_AD, value); |
| 296 | saa7146_write(saa, MC2, (2 << 16) | 2); |
| 297 | |
| 298 | if ((result = saa7146_wait_for_debi_done(saa, nobusyloop)) < 0) { |
| 299 | if (uselocks) |
| 300 | spin_unlock_irqrestore(&budget->debilock, flags); |
| 301 | return result; |
| 302 | } |
| 303 | |
| 304 | if (uselocks) |
| 305 | spin_unlock_irqrestore(&budget->debilock, flags); |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | |
| 310 | /**************************************************************************** |
| 311 | * DVB API SECTION |
| 312 | ****************************************************************************/ |
| 313 | |
| 314 | static int budget_start_feed(struct dvb_demux_feed *feed) |
| 315 | { |
| 316 | struct dvb_demux *demux = feed->demux; |
| 317 | struct budget *budget = (struct budget *) demux->priv; |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 318 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | |
| 320 | dprintk(2, "budget: %p\n", budget); |
| 321 | |
| 322 | if (!demux->dmx.frontend) |
| 323 | return -EINVAL; |
| 324 | |
| 325 | spin_lock(&budget->feedlock); |
| 326 | feed->pusi_seen = 0; /* have a clean section start */ |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 327 | if (budget->feeding++ == 0) |
| 328 | status = start_ts_capture(budget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | spin_unlock(&budget->feedlock); |
| 330 | return status; |
| 331 | } |
| 332 | |
| 333 | static int budget_stop_feed(struct dvb_demux_feed *feed) |
| 334 | { |
| 335 | struct dvb_demux *demux = feed->demux; |
| 336 | struct budget *budget = (struct budget *) demux->priv; |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 337 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | |
| 339 | dprintk(2, "budget: %p\n", budget); |
| 340 | |
| 341 | spin_lock(&budget->feedlock); |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 342 | if (--budget->feeding == 0) |
| 343 | status = stop_ts_capture(budget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | spin_unlock(&budget->feedlock); |
| 345 | return status; |
| 346 | } |
| 347 | |
| 348 | static int budget_register(struct budget *budget) |
| 349 | { |
| 350 | struct dvb_demux *dvbdemux = &budget->demux; |
| 351 | int ret; |
| 352 | |
| 353 | dprintk(2, "budget: %p\n", budget); |
| 354 | |
| 355 | dvbdemux->priv = (void *) budget; |
| 356 | |
| 357 | dvbdemux->filternum = 256; |
| 358 | dvbdemux->feednum = 256; |
| 359 | dvbdemux->start_feed = budget_start_feed; |
| 360 | dvbdemux->stop_feed = budget_stop_feed; |
| 361 | dvbdemux->write_to_decoder = NULL; |
| 362 | |
| 363 | dvbdemux->dmx.capabilities = (DMX_TS_FILTERING | DMX_SECTION_FILTERING | |
| 364 | DMX_MEMORY_BASED_FILTERING); |
| 365 | |
| 366 | dvb_dmx_init(&budget->demux); |
| 367 | |
| 368 | budget->dmxdev.filternum = 256; |
| 369 | budget->dmxdev.demux = &dvbdemux->dmx; |
| 370 | budget->dmxdev.capabilities = 0; |
| 371 | |
Johannes Stezenbach | fdc53a6 | 2005-05-16 21:54:39 -0700 | [diff] [blame] | 372 | dvb_dmxdev_init(&budget->dmxdev, &budget->dvb_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | |
| 374 | budget->hw_frontend.source = DMX_FRONTEND_0; |
| 375 | |
| 376 | ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->hw_frontend); |
| 377 | |
| 378 | if (ret < 0) |
| 379 | return ret; |
| 380 | |
| 381 | budget->mem_frontend.source = DMX_MEMORY_FE; |
| 382 | ret = dvbdemux->dmx.add_frontend(&dvbdemux->dmx, &budget->mem_frontend); |
| 383 | if (ret < 0) |
| 384 | return ret; |
| 385 | |
| 386 | ret = dvbdemux->dmx.connect_frontend(&dvbdemux->dmx, &budget->hw_frontend); |
| 387 | if (ret < 0) |
| 388 | return ret; |
| 389 | |
Johannes Stezenbach | fdc53a6 | 2005-05-16 21:54:39 -0700 | [diff] [blame] | 390 | dvb_net_init(&budget->dvb_adapter, &budget->dvb_net, &dvbdemux->dmx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | static void budget_unregister(struct budget *budget) |
| 396 | { |
| 397 | struct dvb_demux *dvbdemux = &budget->demux; |
| 398 | |
| 399 | dprintk(2, "budget: %p\n", budget); |
| 400 | |
| 401 | dvb_net_release(&budget->dvb_net); |
| 402 | |
| 403 | dvbdemux->dmx.close(&dvbdemux->dmx); |
| 404 | dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->hw_frontend); |
| 405 | dvbdemux->dmx.remove_frontend(&dvbdemux->dmx, &budget->mem_frontend); |
| 406 | |
| 407 | dvb_dmxdev_release(&budget->dmxdev); |
| 408 | dvb_dmx_release(&budget->demux); |
| 409 | } |
| 410 | |
| 411 | int ttpci_budget_init(struct budget *budget, struct saa7146_dev *dev, |
| 412 | struct saa7146_pci_extension_data *info, |
| 413 | struct module *owner) |
| 414 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | int ret = 0; |
| 416 | struct budget_info *bi = info->ext_priv; |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 417 | int max_bufsize; |
| 418 | int height_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | |
| 420 | memset(budget, 0, sizeof(struct budget)); |
| 421 | |
| 422 | dprintk(2, "dev: %p, budget: %p\n", dev, budget); |
| 423 | |
| 424 | budget->card = bi; |
| 425 | budget->dev = (struct saa7146_dev *) dev; |
| 426 | |
Hartmut Birr | fd9c66e | 2007-04-21 19:17:49 -0300 | [diff] [blame] | 427 | switch(budget->card->type) { |
| 428 | case BUDGET_FS_ACTIVY: |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 429 | budget->buffer_width = TS_WIDTH_ACTIVY; |
| 430 | max_bufsize = TS_MAX_BUFSIZE_K_ACTIVY; |
| 431 | height_mask = TS_HEIGHT_MASK_ACTIVY; |
Hartmut Birr | fd9c66e | 2007-04-21 19:17:49 -0300 | [diff] [blame] | 432 | break; |
| 433 | |
| 434 | case BUDGET_KNC1C: |
| 435 | case BUDGET_KNC1CP: |
| 436 | case BUDGET_CIN1200C: |
Hartmut Birr | aa323ac | 2007-04-21 19:37:17 -0300 | [diff] [blame] | 437 | case BUDGET_KNC1C_MK3: |
| 438 | case BUDGET_KNC1CP_MK3: |
| 439 | case BUDGET_CIN1200C_MK3: |
Hartmut Birr | fd9c66e | 2007-04-21 19:17:49 -0300 | [diff] [blame] | 440 | budget->buffer_width = TS_WIDTH_DVBC; |
| 441 | max_bufsize = TS_MAX_BUFSIZE_K_DVBC; |
| 442 | height_mask = TS_HEIGHT_MASK_DVBC; |
| 443 | break; |
| 444 | |
| 445 | default: |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 446 | budget->buffer_width = TS_WIDTH; |
| 447 | max_bufsize = TS_MAX_BUFSIZE_K; |
| 448 | height_mask = TS_HEIGHT_MASK; |
| 449 | } |
| 450 | |
| 451 | if (dma_buffer_size < TS_MIN_BUFSIZE_K) |
| 452 | dma_buffer_size = TS_MIN_BUFSIZE_K; |
| 453 | else if (dma_buffer_size > max_bufsize) |
| 454 | dma_buffer_size = max_bufsize; |
| 455 | |
| 456 | budget->buffer_height = dma_buffer_size * 1024 / budget->buffer_width; |
Hartmut Birr | fd9c66e | 2007-04-21 19:17:49 -0300 | [diff] [blame] | 457 | if (budget->buffer_height > 0xfff) { |
| 458 | budget->buffer_height /= 2; |
| 459 | budget->buffer_height &= height_mask; |
| 460 | budget->buffer_size = 2 * budget->buffer_height * budget->buffer_width; |
| 461 | } else { |
| 462 | budget->buffer_height &= height_mask; |
| 463 | budget->buffer_size = budget->buffer_height * budget->buffer_width; |
| 464 | } |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 465 | budget->buffer_warning_threshold = budget->buffer_size * 80/100; |
| 466 | budget->buffer_warnings = 0; |
| 467 | budget->buffer_warning_time = jiffies; |
| 468 | |
Hartmut Birr | fd9c66e | 2007-04-21 19:17:49 -0300 | [diff] [blame] | 469 | dprintk(2, "%s: buffer type = %s, width = %d, height = %d\n", |
| 470 | budget->dev->name, |
| 471 | budget->buffer_size > budget->buffer_width * budget->buffer_height ? "odd/even" : "single", |
| 472 | budget->buffer_width, budget->buffer_height); |
Ingo Schneider | afa47ab | 2006-03-29 22:05:16 -0300 | [diff] [blame] | 473 | printk("%s: dma buffer size %u\n", budget->dev->name, budget->buffer_size); |
| 474 | |
Andrew de Quincey | d09dbf9 | 2006-04-10 09:27:37 -0300 | [diff] [blame] | 475 | 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] | 476 | return ret; |
| 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
| 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)); |
| 485 | |
| 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); |
| 492 | |
| 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 */ |
| 497 | |
| 498 | #ifdef I2C_ADAP_CLASS_TV_DIGITAL |
| 499 | budget->i2c_adap.class = I2C_ADAP_CLASS_TV_DIGITAL; |
| 500 | #else |
| 501 | budget->i2c_adap.class = I2C_CLASS_TV_DIGITAL; |
| 502 | #endif |
| 503 | |
| 504 | strlcpy(budget->i2c_adap.name, budget->card->name, sizeof(budget->i2c_adap.name)); |
| 505 | |
| 506 | saa7146_i2c_adapter_prepare(dev, &budget->i2c_adap, SAA7146_I2C_BUS_BIT_RATE_120); |
| 507 | strcpy(budget->i2c_adap.name, budget->card->name); |
| 508 | |
| 509 | if (i2c_add_adapter(&budget->i2c_adap) < 0) { |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 510 | ret = -ENOMEM; |
| 511 | goto err_dvb_unregister; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | } |
| 513 | |
Johannes Stezenbach | fdc53a6 | 2005-05-16 21:54:39 -0700 | [diff] [blame] | 514 | ttpci_eeprom_parse_mac(&budget->i2c_adap, budget->dvb_adapter.proposed_mac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 516 | budget->grabbing = saa7146_vmalloc_build_pgtable(dev->pci, budget->buffer_size, &budget->pt); |
| 517 | if (NULL == budget->grabbing) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | ret = -ENOMEM; |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 519 | goto err_del_i2c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | saa7146_write(dev, PCI_BT_V1, 0x001c0000); |
| 523 | /* upload all */ |
| 524 | saa7146_write(dev, GPIO_CTRL, 0x000000); |
| 525 | |
| 526 | tasklet_init(&budget->vpe_tasklet, vpeirq, (unsigned long) budget); |
| 527 | |
| 528 | /* frontend power on */ |
Oliver Endriss | f49cc15 | 2006-01-09 15:25:06 -0200 | [diff] [blame] | 529 | if (bi->type != BUDGET_FS_ACTIVY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | saa7146_setgpio(dev, 2, SAA7146_GPIO_OUTHI); |
| 531 | |
Hartmut Birr | 1fb4a17 | 2007-07-02 13:39:41 -0300 | [diff] [blame^] | 532 | if ((ret = budget_register(budget)) == 0) |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 533 | return 0; /* Everything OK */ |
| 534 | |
| 535 | /* An error occurred, cleanup resources */ |
| 536 | saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt); |
| 537 | |
| 538 | err_del_i2c: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | i2c_del_adapter(&budget->i2c_adap); |
| 540 | |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 541 | err_dvb_unregister: |
Johannes Stezenbach | fdc53a6 | 2005-05-16 21:54:39 -0700 | [diff] [blame] | 542 | dvb_unregister_adapter(&budget->dvb_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | |
| 544 | return ret; |
| 545 | } |
| 546 | |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 547 | void ttpci_budget_init_hooks(struct budget *budget) |
| 548 | { |
| 549 | if (budget->dvb_frontend && !budget->read_fe_status) { |
| 550 | budget->read_fe_status = budget->dvb_frontend->ops.read_status; |
| 551 | budget->dvb_frontend->ops.read_status = budget_read_fe_status; |
| 552 | } |
| 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | int ttpci_budget_deinit(struct budget *budget) |
| 556 | { |
| 557 | struct saa7146_dev *dev = budget->dev; |
| 558 | |
| 559 | dprintk(2, "budget: %p\n", budget); |
| 560 | |
| 561 | budget_unregister(budget); |
| 562 | |
Jon Burgess | 87c3019 | 2007-05-03 12:23:44 -0300 | [diff] [blame] | 563 | tasklet_kill(&budget->vpe_tasklet); |
| 564 | |
| 565 | saa7146_vfree_destroy_pgtable(dev->pci, budget->grabbing, &budget->pt); |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | i2c_del_adapter(&budget->i2c_adap); |
| 568 | |
Johannes Stezenbach | fdc53a6 | 2005-05-16 21:54:39 -0700 | [diff] [blame] | 569 | dvb_unregister_adapter(&budget->dvb_adapter); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | void ttpci_budget_irq10_handler(struct saa7146_dev *dev, u32 * isr) |
| 575 | { |
| 576 | struct budget *budget = (struct budget *) dev->ext_priv; |
| 577 | |
| 578 | dprintk(8, "dev: %p, budget: %p\n", dev, budget); |
| 579 | |
| 580 | if (*isr & MASK_10) |
| 581 | tasklet_schedule(&budget->vpe_tasklet); |
| 582 | } |
| 583 | |
| 584 | void ttpci_budget_set_video_port(struct saa7146_dev *dev, int video_port) |
| 585 | { |
| 586 | struct budget *budget = (struct budget *) dev->ext_priv; |
| 587 | |
| 588 | spin_lock(&budget->feedlock); |
| 589 | budget->video_port = video_port; |
| 590 | if (budget->feeding) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | stop_ts_capture(budget); |
| 592 | start_ts_capture(budget); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | } |
| 594 | spin_unlock(&budget->feedlock); |
| 595 | } |
| 596 | |
| 597 | EXPORT_SYMBOL_GPL(ttpci_budget_debiread); |
| 598 | EXPORT_SYMBOL_GPL(ttpci_budget_debiwrite); |
| 599 | EXPORT_SYMBOL_GPL(ttpci_budget_init); |
Oliver Endriss | 32e4c3a | 2006-07-18 22:55:23 -0300 | [diff] [blame] | 600 | EXPORT_SYMBOL_GPL(ttpci_budget_init_hooks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | EXPORT_SYMBOL_GPL(ttpci_budget_deinit); |
| 602 | EXPORT_SYMBOL_GPL(ttpci_budget_irq10_handler); |
| 603 | EXPORT_SYMBOL_GPL(ttpci_budget_set_video_port); |
| 604 | EXPORT_SYMBOL_GPL(budget_debug); |
| 605 | |
| 606 | MODULE_LICENSE("GPL"); |