Merge remote-tracking branch 'moduleh/module.h-split'
[linux-2.6/next.git] / drivers / media / video / saa7164 / saa7164-dvb.c
blobf65eab63ca871fb634be16d0f17a0457ee1d933c
1 /*
2 * Driver for the NXP SAA7164 PCIe bridge
4 * Copyright (c) 2010 Steven Toth <stoth@kernellabs.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include "saa7164.h"
24 #include "tda10048.h"
25 #include "tda18271.h"
26 #include "s5h1411.h"
28 #define DRIVER_NAME "saa7164"
30 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
32 /* addr is in the card struct, get it from there */
33 static struct tda10048_config hauppauge_hvr2200_1_config = {
34 .demod_address = 0x10 >> 1,
35 .output_mode = TDA10048_SERIAL_OUTPUT,
36 .fwbulkwritelen = TDA10048_BULKWRITE_200,
37 .inversion = TDA10048_INVERSION_ON,
38 .dtv6_if_freq_khz = TDA10048_IF_3300,
39 .dtv7_if_freq_khz = TDA10048_IF_3500,
40 .dtv8_if_freq_khz = TDA10048_IF_4000,
41 .clk_freq_khz = TDA10048_CLK_16000,
43 static struct tda10048_config hauppauge_hvr2200_2_config = {
44 .demod_address = 0x12 >> 1,
45 .output_mode = TDA10048_SERIAL_OUTPUT,
46 .fwbulkwritelen = TDA10048_BULKWRITE_200,
47 .inversion = TDA10048_INVERSION_ON,
48 .dtv6_if_freq_khz = TDA10048_IF_3300,
49 .dtv7_if_freq_khz = TDA10048_IF_3500,
50 .dtv8_if_freq_khz = TDA10048_IF_4000,
51 .clk_freq_khz = TDA10048_CLK_16000,
54 static struct tda18271_std_map hauppauge_tda18271_std_map = {
55 .atsc_6 = { .if_freq = 3250, .agc_mode = 3, .std = 3,
56 .if_lvl = 6, .rfagc_top = 0x37 },
57 .qam_6 = { .if_freq = 4000, .agc_mode = 3, .std = 0,
58 .if_lvl = 6, .rfagc_top = 0x37 },
61 static struct tda18271_config hauppauge_hvr22x0_tuner_config = {
62 .std_map = &hauppauge_tda18271_std_map,
63 .gate = TDA18271_GATE_ANALOG,
64 .role = TDA18271_MASTER,
67 static struct tda18271_config hauppauge_hvr22x0s_tuner_config = {
68 .std_map = &hauppauge_tda18271_std_map,
69 .gate = TDA18271_GATE_ANALOG,
70 .role = TDA18271_SLAVE,
71 .output_opt = TDA18271_OUTPUT_LT_OFF,
72 .rf_cal_on_startup = 1
75 static struct s5h1411_config hauppauge_s5h1411_config = {
76 .output_mode = S5H1411_SERIAL_OUTPUT,
77 .gpio = S5H1411_GPIO_ON,
78 .qam_if = S5H1411_IF_4000,
79 .vsb_if = S5H1411_IF_3250,
80 .inversion = S5H1411_INVERSION_ON,
81 .status_mode = S5H1411_DEMODLOCKING,
82 .mpeg_timing = S5H1411_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK,
85 static int saa7164_dvb_stop_port(struct saa7164_port *port)
87 struct saa7164_dev *dev = port->dev;
88 int ret;
90 ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
91 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
92 printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
93 __func__, ret);
94 ret = -EIO;
95 } else {
96 dprintk(DBGLVL_DVB, "%s() Stopped\n", __func__);
97 ret = 0;
100 return ret;
103 static int saa7164_dvb_acquire_port(struct saa7164_port *port)
105 struct saa7164_dev *dev = port->dev;
106 int ret;
108 ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
109 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
110 printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
111 __func__, ret);
112 ret = -EIO;
113 } else {
114 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
115 ret = 0;
118 return ret;
121 static int saa7164_dvb_pause_port(struct saa7164_port *port)
123 struct saa7164_dev *dev = port->dev;
124 int ret;
126 ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
127 if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
128 printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
129 __func__, ret);
130 ret = -EIO;
131 } else {
132 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
133 ret = 0;
136 return ret;
139 /* Firmware is very windows centric, meaning you have to transition
140 * the part through AVStream / KS Windows stages, forwards or backwards.
141 * States are: stopped, acquired (h/w), paused, started.
143 static int saa7164_dvb_stop_streaming(struct saa7164_port *port)
145 struct saa7164_dev *dev = port->dev;
146 struct saa7164_buffer *buf;
147 struct list_head *p, *q;
148 int ret;
150 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
152 ret = saa7164_dvb_pause_port(port);
153 ret = saa7164_dvb_acquire_port(port);
154 ret = saa7164_dvb_stop_port(port);
156 /* Mark the hardware buffers as free */
157 mutex_lock(&port->dmaqueue_lock);
158 list_for_each_safe(p, q, &port->dmaqueue.list) {
159 buf = list_entry(p, struct saa7164_buffer, list);
160 buf->flags = SAA7164_BUFFER_FREE;
162 mutex_unlock(&port->dmaqueue_lock);
164 return ret;
167 static int saa7164_dvb_start_port(struct saa7164_port *port)
169 struct saa7164_dev *dev = port->dev;
170 int ret = 0, result;
172 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
174 saa7164_buffer_cfg_port(port);
176 /* Acquire the hardware */
177 result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
178 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
179 printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
180 __func__, result);
182 /* Stop the hardware, regardless */
183 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
184 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
185 printk(KERN_ERR "%s() acquire/forced stop transition "
186 "failed, res = 0x%x\n", __func__, result);
188 ret = -EIO;
189 goto out;
190 } else
191 dprintk(DBGLVL_DVB, "%s() Acquired\n", __func__);
193 /* Pause the hardware */
194 result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
195 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
196 printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
197 __func__, result);
199 /* Stop the hardware, regardless */
200 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
201 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
202 printk(KERN_ERR "%s() pause/forced stop transition "
203 "failed, res = 0x%x\n", __func__, result);
206 ret = -EIO;
207 goto out;
208 } else
209 dprintk(DBGLVL_DVB, "%s() Paused\n", __func__);
211 /* Start the hardware */
212 result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
213 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
214 printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
215 __func__, result);
217 /* Stop the hardware, regardless */
218 result = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
219 if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
220 printk(KERN_ERR "%s() run/forced stop transition "
221 "failed, res = 0x%x\n", __func__, result);
224 ret = -EIO;
225 } else
226 dprintk(DBGLVL_DVB, "%s() Running\n", __func__);
228 out:
229 return ret;
232 static int saa7164_dvb_start_feed(struct dvb_demux_feed *feed)
234 struct dvb_demux *demux = feed->demux;
235 struct saa7164_port *port = (struct saa7164_port *) demux->priv;
236 struct saa7164_dvb *dvb = &port->dvb;
237 struct saa7164_dev *dev = port->dev;
238 int ret = 0;
240 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
242 if (!demux->dmx.frontend)
243 return -EINVAL;
245 if (dvb) {
246 mutex_lock(&dvb->lock);
247 if (dvb->feeding++ == 0) {
248 /* Start transport */
249 ret = saa7164_dvb_start_port(port);
251 mutex_unlock(&dvb->lock);
252 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
253 __func__, port->nr, dvb->feeding);
256 return ret;
259 static int saa7164_dvb_stop_feed(struct dvb_demux_feed *feed)
261 struct dvb_demux *demux = feed->demux;
262 struct saa7164_port *port = (struct saa7164_port *) demux->priv;
263 struct saa7164_dvb *dvb = &port->dvb;
264 struct saa7164_dev *dev = port->dev;
265 int ret = 0;
267 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
269 if (dvb) {
270 mutex_lock(&dvb->lock);
271 if (--dvb->feeding == 0) {
272 /* Stop transport */
273 ret = saa7164_dvb_stop_streaming(port);
275 mutex_unlock(&dvb->lock);
276 dprintk(DBGLVL_DVB, "%s(port=%d) now feeding = %d\n",
277 __func__, port->nr, dvb->feeding);
280 return ret;
283 static int dvb_register(struct saa7164_port *port)
285 struct saa7164_dvb *dvb = &port->dvb;
286 struct saa7164_dev *dev = port->dev;
287 struct saa7164_buffer *buf;
288 int result, i;
290 dprintk(DBGLVL_DVB, "%s(port=%d)\n", __func__, port->nr);
292 if (port->type != SAA7164_MPEG_DVB)
293 BUG();
295 /* Sanity check that the PCI configuration space is active */
296 if (port->hwcfg.BARLocation == 0) {
297 result = -ENOMEM;
298 printk(KERN_ERR "%s: dvb_register_adapter failed "
299 "(errno = %d), NO PCI configuration\n",
300 DRIVER_NAME, result);
301 goto fail_adapter;
304 /* Init and establish defaults */
305 port->hw_streamingparams.bitspersample = 8;
306 port->hw_streamingparams.samplesperline = 188;
307 port->hw_streamingparams.numberoflines =
308 (SAA7164_TS_NUMBER_OF_LINES * 188) / 188;
310 port->hw_streamingparams.pitch = 188;
311 port->hw_streamingparams.linethreshold = 0;
312 port->hw_streamingparams.pagetablelistvirt = NULL;
313 port->hw_streamingparams.pagetablelistphys = NULL;
314 port->hw_streamingparams.numpagetables = 2 +
315 ((SAA7164_TS_NUMBER_OF_LINES * 188) / PAGE_SIZE);
317 port->hw_streamingparams.numpagetableentries = port->hwcfg.buffercount;
319 /* Allocate the PCI resources */
320 for (i = 0; i < port->hwcfg.buffercount; i++) {
321 buf = saa7164_buffer_alloc(port,
322 port->hw_streamingparams.numberoflines *
323 port->hw_streamingparams.pitch);
325 if (!buf) {
326 result = -ENOMEM;
327 printk(KERN_ERR "%s: dvb_register_adapter failed "
328 "(errno = %d), unable to allocate buffers\n",
329 DRIVER_NAME, result);
330 goto fail_adapter;
333 mutex_lock(&port->dmaqueue_lock);
334 list_add_tail(&buf->list, &port->dmaqueue.list);
335 mutex_unlock(&port->dmaqueue_lock);
338 /* register adapter */
339 result = dvb_register_adapter(&dvb->adapter, DRIVER_NAME, THIS_MODULE,
340 &dev->pci->dev, adapter_nr);
341 if (result < 0) {
342 printk(KERN_ERR "%s: dvb_register_adapter failed "
343 "(errno = %d)\n", DRIVER_NAME, result);
344 goto fail_adapter;
346 dvb->adapter.priv = port;
348 /* register frontend */
349 result = dvb_register_frontend(&dvb->adapter, dvb->frontend);
350 if (result < 0) {
351 printk(KERN_ERR "%s: dvb_register_frontend failed "
352 "(errno = %d)\n", DRIVER_NAME, result);
353 goto fail_frontend;
356 /* register demux stuff */
357 dvb->demux.dmx.capabilities =
358 DMX_TS_FILTERING | DMX_SECTION_FILTERING |
359 DMX_MEMORY_BASED_FILTERING;
360 dvb->demux.priv = port;
361 dvb->demux.filternum = 256;
362 dvb->demux.feednum = 256;
363 dvb->demux.start_feed = saa7164_dvb_start_feed;
364 dvb->demux.stop_feed = saa7164_dvb_stop_feed;
365 result = dvb_dmx_init(&dvb->demux);
366 if (result < 0) {
367 printk(KERN_ERR "%s: dvb_dmx_init failed (errno = %d)\n",
368 DRIVER_NAME, result);
369 goto fail_dmx;
372 dvb->dmxdev.filternum = 256;
373 dvb->dmxdev.demux = &dvb->demux.dmx;
374 dvb->dmxdev.capabilities = 0;
375 result = dvb_dmxdev_init(&dvb->dmxdev, &dvb->adapter);
376 if (result < 0) {
377 printk(KERN_ERR "%s: dvb_dmxdev_init failed (errno = %d)\n",
378 DRIVER_NAME, result);
379 goto fail_dmxdev;
382 dvb->fe_hw.source = DMX_FRONTEND_0;
383 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_hw);
384 if (result < 0) {
385 printk(KERN_ERR "%s: add_frontend failed "
386 "(DMX_FRONTEND_0, errno = %d)\n", DRIVER_NAME, result);
387 goto fail_fe_hw;
390 dvb->fe_mem.source = DMX_MEMORY_FE;
391 result = dvb->demux.dmx.add_frontend(&dvb->demux.dmx, &dvb->fe_mem);
392 if (result < 0) {
393 printk(KERN_ERR "%s: add_frontend failed "
394 "(DMX_MEMORY_FE, errno = %d)\n", DRIVER_NAME, result);
395 goto fail_fe_mem;
398 result = dvb->demux.dmx.connect_frontend(&dvb->demux.dmx, &dvb->fe_hw);
399 if (result < 0) {
400 printk(KERN_ERR "%s: connect_frontend failed (errno = %d)\n",
401 DRIVER_NAME, result);
402 goto fail_fe_conn;
405 /* register network adapter */
406 dvb_net_init(&dvb->adapter, &dvb->net, &dvb->demux.dmx);
407 return 0;
409 fail_fe_conn:
410 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
411 fail_fe_mem:
412 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
413 fail_fe_hw:
414 dvb_dmxdev_release(&dvb->dmxdev);
415 fail_dmxdev:
416 dvb_dmx_release(&dvb->demux);
417 fail_dmx:
418 dvb_unregister_frontend(dvb->frontend);
419 fail_frontend:
420 dvb_frontend_detach(dvb->frontend);
421 dvb_unregister_adapter(&dvb->adapter);
422 fail_adapter:
423 return result;
426 int saa7164_dvb_unregister(struct saa7164_port *port)
428 struct saa7164_dvb *dvb = &port->dvb;
429 struct saa7164_dev *dev = port->dev;
430 struct saa7164_buffer *b;
431 struct list_head *c, *n;
433 dprintk(DBGLVL_DVB, "%s()\n", __func__);
435 if (port->type != SAA7164_MPEG_DVB)
436 BUG();
438 /* Remove any allocated buffers */
439 mutex_lock(&port->dmaqueue_lock);
440 list_for_each_safe(c, n, &port->dmaqueue.list) {
441 b = list_entry(c, struct saa7164_buffer, list);
442 list_del(c);
443 saa7164_buffer_dealloc(b);
445 mutex_unlock(&port->dmaqueue_lock);
447 if (dvb->frontend == NULL)
448 return 0;
450 dvb_net_release(&dvb->net);
451 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_mem);
452 dvb->demux.dmx.remove_frontend(&dvb->demux.dmx, &dvb->fe_hw);
453 dvb_dmxdev_release(&dvb->dmxdev);
454 dvb_dmx_release(&dvb->demux);
455 dvb_unregister_frontend(dvb->frontend);
456 dvb_frontend_detach(dvb->frontend);
457 dvb_unregister_adapter(&dvb->adapter);
458 return 0;
461 /* All the DVB attach calls go here, this function get's modified
462 * for each new card.
464 int saa7164_dvb_register(struct saa7164_port *port)
466 struct saa7164_dev *dev = port->dev;
467 struct saa7164_dvb *dvb = &port->dvb;
468 struct saa7164_i2c *i2c_bus = NULL;
469 int ret;
471 dprintk(DBGLVL_DVB, "%s()\n", __func__);
473 /* init frontend */
474 switch (dev->board) {
475 case SAA7164_BOARD_HAUPPAUGE_HVR2200:
476 case SAA7164_BOARD_HAUPPAUGE_HVR2200_2:
477 case SAA7164_BOARD_HAUPPAUGE_HVR2200_3:
478 i2c_bus = &dev->i2c_bus[port->nr + 1];
479 switch (port->nr) {
480 case 0:
481 port->dvb.frontend = dvb_attach(tda10048_attach,
482 &hauppauge_hvr2200_1_config,
483 &i2c_bus->i2c_adap);
485 if (port->dvb.frontend != NULL) {
486 /* TODO: addr is in the card struct */
487 dvb_attach(tda18271_attach, port->dvb.frontend,
488 0xc0 >> 1, &i2c_bus->i2c_adap,
489 &hauppauge_hvr22x0_tuner_config);
492 break;
493 case 1:
494 port->dvb.frontend = dvb_attach(tda10048_attach,
495 &hauppauge_hvr2200_2_config,
496 &i2c_bus->i2c_adap);
498 if (port->dvb.frontend != NULL) {
499 /* TODO: addr is in the card struct */
500 dvb_attach(tda18271_attach, port->dvb.frontend,
501 0xc0 >> 1, &i2c_bus->i2c_adap,
502 &hauppauge_hvr22x0s_tuner_config);
505 break;
507 break;
508 case SAA7164_BOARD_HAUPPAUGE_HVR2250:
509 case SAA7164_BOARD_HAUPPAUGE_HVR2250_2:
510 case SAA7164_BOARD_HAUPPAUGE_HVR2250_3:
511 i2c_bus = &dev->i2c_bus[port->nr + 1];
513 port->dvb.frontend = dvb_attach(s5h1411_attach,
514 &hauppauge_s5h1411_config,
515 &i2c_bus->i2c_adap);
517 if (port->dvb.frontend != NULL) {
518 if (port->nr == 0) {
519 /* Master TDA18271 */
520 /* TODO: addr is in the card struct */
521 dvb_attach(tda18271_attach, port->dvb.frontend,
522 0xc0 >> 1, &i2c_bus->i2c_adap,
523 &hauppauge_hvr22x0_tuner_config);
524 } else {
525 /* Slave TDA18271 */
526 dvb_attach(tda18271_attach, port->dvb.frontend,
527 0xc0 >> 1, &i2c_bus->i2c_adap,
528 &hauppauge_hvr22x0s_tuner_config);
532 break;
533 default:
534 printk(KERN_ERR "%s: The frontend isn't supported\n",
535 dev->name);
536 break;
538 if (NULL == dvb->frontend) {
539 printk(KERN_ERR "%s() Frontend initialization failed\n",
540 __func__);
541 return -1;
544 /* register everything */
545 ret = dvb_register(port);
546 if (ret < 0) {
547 if (dvb->frontend->ops.release)
548 dvb->frontend->ops.release(dvb->frontend);
549 return ret;
552 return 0;