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->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 mutex_init(&dvb->feedlock);
224 err_disconnect_frontend:
225 dmx->disconnect_frontend(dmx);
226 err_remove_mem_frontend:
227 dmx->remove_frontend(dmx, &dvb->mem_frontend);
228 err_remove_hw_frontend:
229 dmx->remove_frontend(dmx, &dvb->hw_frontend);
230 err_dvb_dmxdev_release:
231 dvb_dmxdev_release(&dvb->dmxdev);
233 dvb_dmx_release(dvbdemux);
234 err_dvb_unregister_adapter:
235 dvb_unregister_adapter(dvb_adapter);
240 void cx18_dvb_unregister(struct cx18_stream *stream)
242 struct cx18 *cx = stream->cx;
243 struct cx18_dvb *dvb = &stream->dvb;
244 struct dvb_adapter *dvb_adapter;
245 struct dvb_demux *dvbdemux;
246 struct dmx_demux *dmx;
248 CX18_INFO("unregister DVB\n");
250 dvb_adapter = &dvb->dvb_adapter;
251 dvbdemux = &dvb->demux;
252 dmx = &dvbdemux->dmx;
255 dvb_net_release(&dvb->dvbnet);
256 dmx->remove_frontend(dmx, &dvb->mem_frontend);
257 dmx->remove_frontend(dmx, &dvb->hw_frontend);
258 dvb_dmxdev_release(&dvb->dmxdev);
259 dvb_dmx_release(dvbdemux);
260 dvb_unregister_frontend(dvb->fe);
261 dvb_frontend_detach(dvb->fe);
262 dvb_unregister_adapter(dvb_adapter);
265 /* All the DVB attach calls go here, this function get's modified
266 * for each new card. No other function in this file needs
269 static int dvb_register(struct cx18_stream *stream)
271 struct cx18_dvb *dvb = &stream->dvb;
272 struct cx18 *cx = stream->cx;
275 switch (cx->card->type) {
276 case CX18_CARD_HVR_1600_ESMT:
277 case CX18_CARD_HVR_1600_SAMSUNG:
278 dvb->fe = dvb_attach(s5h1409_attach,
279 &hauppauge_hvr1600_config,
281 if (dvb->fe != NULL) {
282 dvb_attach(mxl5005s_attach, dvb->fe,
284 &hauppauge_hvr1600_tuner);
289 /* No Digital Tv Support */
293 if (dvb->fe == NULL) {
294 CX18_ERR("frontend initialization failed\n");
298 ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
300 if (dvb->fe->ops.release)
301 dvb->fe->ops.release(dvb->fe);