2 * cx18 functions for DVB support
4 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
5 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "cx18-version.h"
26 #include "cx18-streams.h"
27 #include "cx18-cards.h"
31 DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33 #define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
35 static struct mxl5005s_config hauppauge_hvr1600_tuner = {
36 .i2c_address = 0xC6 >> 1,
37 .if_freq = IF_FREQ_5380000HZ,
38 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
39 .agc_mode = MXL_SINGLE_AGC,
40 .tracking_filter = MXL_TF_C_H,
41 .rssi_enable = MXL_RSSI_ENABLE,
42 .cap_select = MXL_CAP_SEL_ENABLE,
43 .div_out = MXL_DIV_OUT_4,
44 .clock_out = MXL_CLOCK_OUT_DISABLE,
45 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
46 .top = MXL5005S_TOP_25P2,
47 .mod_mode = MXL_DIGITAL_MODE,
48 .if_mode = MXL_ZERO_IF,
49 .AgcMasterByte = 0x00,
52 static struct s5h1409_config hauppauge_hvr1600_config = {
53 .demod_address = 0x32 >> 1,
54 .output_mode = S5H1409_SERIAL_OUTPUT,
55 .gpio = S5H1409_GPIO_ON,
57 .inversion = S5H1409_INVERSION_OFF,
58 .status_mode = S5H1409_DEMODLOCKING,
59 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
63 static int dvb_register(struct cx18_stream *stream);
65 /* Kernel DVB framework calls this when the feed needs to start.
66 * The CX18 framework should enable the transport DMA handling
67 * and queue processing.
69 static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
71 struct dvb_demux *demux = feed->demux;
72 struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
73 struct cx18 *cx = stream->cx;
77 CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
78 feed->pid, feed->index);
80 mutex_lock(&cx->serialize_lock);
81 ret = cx18_init_on_first_open(cx);
82 mutex_unlock(&cx->serialize_lock);
84 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
89 switch (cx->card->type) {
90 case CX18_CARD_HVR_1600_ESMT:
91 case CX18_CARD_HVR_1600_SAMSUNG:
92 v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
93 v |= 0x00400000; /* Serial Mode */
94 v |= 0x00002000; /* Data Length - Byte */
95 v |= 0x00010000; /* Error - Polarity */
96 v |= 0x00020000; /* Error - Passthru */
97 v |= 0x000c0000; /* Error - Ignore */
98 cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
102 /* Assumption - Parallel transport - Signalling
103 * undefined or default.
108 if (!demux->dmx.frontend)
114 mutex_lock(&stream->dvb.feedlock);
115 if (stream->dvb.feeding++ == 0) {
116 CX18_DEBUG_INFO("Starting Transport DMA\n");
117 set_bit(CX18_F_S_STREAMING, &stream->s_flags);
118 ret = cx18_start_v4l2_encode_stream(stream);
120 CX18_DEBUG_INFO("Failed to start Transport DMA\n");
121 stream->dvb.feeding--;
122 if (stream->dvb.feeding == 0)
123 clear_bit(CX18_F_S_STREAMING, &stream->s_flags);
127 mutex_unlock(&stream->dvb.feedlock);
132 /* Kernel DVB framework calls this when the feed needs to stop. */
133 static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
135 struct dvb_demux *demux = feed->demux;
136 struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
137 struct cx18 *cx = stream->cx;
140 CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
141 feed->pid, feed->index);
144 mutex_lock(&stream->dvb.feedlock);
145 if (--stream->dvb.feeding == 0) {
146 CX18_DEBUG_INFO("Stopping Transport DMA\n");
147 ret = cx18_stop_v4l2_encode_stream(stream, 0);
150 mutex_unlock(&stream->dvb.feedlock);
156 int cx18_dvb_register(struct cx18_stream *stream)
158 struct cx18 *cx = stream->cx;
159 struct cx18_dvb *dvb = &stream->dvb;
160 struct dvb_adapter *dvb_adapter;
161 struct dvb_demux *dvbdemux;
162 struct dmx_demux *dmx;
168 ret = dvb_register_adapter(&dvb->dvb_adapter,
170 THIS_MODULE, &cx->pci_dev->dev, adapter_nr);
174 dvb_adapter = &dvb->dvb_adapter;
176 dvbdemux = &dvb->demux;
178 dvbdemux->priv = (void *)stream;
180 dvbdemux->filternum = 256;
181 dvbdemux->feednum = 256;
182 dvbdemux->start_feed = cx18_dvb_start_feed;
183 dvbdemux->stop_feed = cx18_dvb_stop_feed;
184 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
185 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
186 ret = dvb_dmx_init(dvbdemux);
188 goto err_dvb_unregister_adapter;
190 dmx = &dvbdemux->dmx;
192 dvb->hw_frontend.source = DMX_FRONTEND_0;
193 dvb->mem_frontend.source = DMX_MEMORY_FE;
194 dvb->dmxdev.filternum = 256;
195 dvb->dmxdev.demux = dmx;
197 ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
199 goto err_dvb_dmx_release;
201 ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
203 goto err_dvb_dmxdev_release;
205 ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
207 goto err_remove_hw_frontend;
209 ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
211 goto err_remove_mem_frontend;
213 ret = dvb_register(stream);
215 goto err_disconnect_frontend;
217 dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
219 CX18_INFO("DVB Frontend registered\n");
220 CX18_INFO("Registered DVB adapter%d for %s (%d x %d kB)\n",
221 stream->dvb.dvb_adapter.num, stream->name,
222 stream->buffers, stream->buf_size/1024);
224 mutex_init(&dvb->feedlock);
228 err_disconnect_frontend:
229 dmx->disconnect_frontend(dmx);
230 err_remove_mem_frontend:
231 dmx->remove_frontend(dmx, &dvb->mem_frontend);
232 err_remove_hw_frontend:
233 dmx->remove_frontend(dmx, &dvb->hw_frontend);
234 err_dvb_dmxdev_release:
235 dvb_dmxdev_release(&dvb->dmxdev);
237 dvb_dmx_release(dvbdemux);
238 err_dvb_unregister_adapter:
239 dvb_unregister_adapter(dvb_adapter);
244 void cx18_dvb_unregister(struct cx18_stream *stream)
246 struct cx18 *cx = stream->cx;
247 struct cx18_dvb *dvb = &stream->dvb;
248 struct dvb_adapter *dvb_adapter;
249 struct dvb_demux *dvbdemux;
250 struct dmx_demux *dmx;
252 CX18_INFO("unregister DVB\n");
254 dvb_adapter = &dvb->dvb_adapter;
255 dvbdemux = &dvb->demux;
256 dmx = &dvbdemux->dmx;
259 dvb_net_release(&dvb->dvbnet);
260 dmx->remove_frontend(dmx, &dvb->mem_frontend);
261 dmx->remove_frontend(dmx, &dvb->hw_frontend);
262 dvb_dmxdev_release(&dvb->dmxdev);
263 dvb_dmx_release(dvbdemux);
264 dvb_unregister_frontend(dvb->fe);
265 dvb_frontend_detach(dvb->fe);
266 dvb_unregister_adapter(dvb_adapter);
269 /* All the DVB attach calls go here, this function get's modified
270 * for each new card. No other function in this file needs
273 static int dvb_register(struct cx18_stream *stream)
275 struct cx18_dvb *dvb = &stream->dvb;
276 struct cx18 *cx = stream->cx;
279 switch (cx->card->type) {
280 case CX18_CARD_HVR_1600_ESMT:
281 case CX18_CARD_HVR_1600_SAMSUNG:
282 dvb->fe = dvb_attach(s5h1409_attach,
283 &hauppauge_hvr1600_config,
285 if (dvb->fe != NULL) {
286 dvb_attach(mxl5005s_attach, dvb->fe,
288 &hauppauge_hvr1600_tuner);
293 /* No Digital Tv Support */
297 if (dvb->fe == NULL) {
298 CX18_ERR("frontend initialization failed\n");
302 ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
304 if (dvb->fe->ops.release)
305 dvb->fe->ops.release(dvb->fe);