blob: e7285a1096f27dc317bdde7508189beaffaeb15f [file] [log] [blame]
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03001/*
2 * cx18 functions for DVB support
3 *
Steven Toth6d897612008-09-03 17:12:12 -03004 * Copyright (c) 2008 Steven Toth <stoth@linuxtv.org>
Andy Walls1ed9dcc2008-11-22 01:37:34 -03005 * Copyright (C) 2008 Andy Walls <awalls@radix.net>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03006 *
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.
11 *
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
15 *
16 * GNU General Public License for more details.
17 *
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.
21 */
22
23#include "cx18-version.h"
24#include "cx18-dvb.h"
Andy Wallsb1526422008-08-30 16:03:44 -030025#include "cx18-io.h"
Andy Walls21a278b2009-04-15 20:45:10 -030026#include "cx18-queue.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030027#include "cx18-streams.h"
28#include "cx18-cards.h"
29#include "s5h1409.h"
Steven Toth67129472008-05-01 07:23:23 -030030#include "mxl5005s.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030031
32DVB_DEFINE_MOD_OPT_ADAPTER_NR(adapter_nr);
33
34#define CX18_REG_DMUX_NUM_PORT_0_CONTROL 0xd5a000
35
Steven Toth67129472008-05-01 07:23:23 -030036static struct mxl5005s_config hauppauge_hvr1600_tuner = {
37 .i2c_address = 0xC6 >> 1,
38 .if_freq = IF_FREQ_5380000HZ,
39 .xtal_freq = CRYSTAL_FREQ_16000000HZ,
40 .agc_mode = MXL_SINGLE_AGC,
41 .tracking_filter = MXL_TF_C_H,
42 .rssi_enable = MXL_RSSI_ENABLE,
43 .cap_select = MXL_CAP_SEL_ENABLE,
44 .div_out = MXL_DIV_OUT_4,
45 .clock_out = MXL_CLOCK_OUT_DISABLE,
46 .output_load = MXL5005S_IF_OUTPUT_LOAD_200_OHM,
47 .top = MXL5005S_TOP_25P2,
48 .mod_mode = MXL_DIGITAL_MODE,
49 .if_mode = MXL_ZERO_IF,
50 .AgcMasterByte = 0x00,
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030051};
52
53static struct s5h1409_config hauppauge_hvr1600_config = {
54 .demod_address = 0x32 >> 1,
55 .output_mode = S5H1409_SERIAL_OUTPUT,
56 .gpio = S5H1409_GPIO_ON,
57 .qam_if = 44000,
58 .inversion = S5H1409_INVERSION_OFF,
59 .status_mode = S5H1409_DEMODLOCKING,
60 .mpeg_timing = S5H1409_MPEGTIMING_CONTINOUS_NONINVERTING_CLOCK
61
62};
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030063
64static int dvb_register(struct cx18_stream *stream);
65
66/* Kernel DVB framework calls this when the feed needs to start.
67 * The CX18 framework should enable the transport DMA handling
68 * and queue processing.
69 */
70static int cx18_dvb_start_feed(struct dvb_demux_feed *feed)
71{
72 struct dvb_demux *demux = feed->demux;
73 struct cx18_stream *stream = (struct cx18_stream *) demux->priv;
74 struct cx18 *cx = stream->cx;
Andy Walls08cf7b22008-06-22 00:04:21 -030075 int ret;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030076 u32 v;
77
78 CX18_DEBUG_INFO("Start feed: pid = 0x%x index = %d\n",
79 feed->pid, feed->index);
Andy Walls08cf7b22008-06-22 00:04:21 -030080
81 mutex_lock(&cx->serialize_lock);
82 ret = cx18_init_on_first_open(cx);
83 mutex_unlock(&cx->serialize_lock);
84 if (ret) {
85 CX18_ERR("Failed to initialize firmware starting DVB feed\n");
86 return ret;
87 }
88 ret = -EINVAL;
89
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030090 switch (cx->card->type) {
91 case CX18_CARD_HVR_1600_ESMT:
92 case CX18_CARD_HVR_1600_SAMSUNG:
Andy Wallsb1526422008-08-30 16:03:44 -030093 v = cx18_read_reg(cx, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030094 v |= 0x00400000; /* Serial Mode */
95 v |= 0x00002000; /* Data Length - Byte */
96 v |= 0x00010000; /* Error - Polarity */
97 v |= 0x00020000; /* Error - Passthru */
98 v |= 0x000c0000; /* Error - Ignore */
Andy Wallsb1526422008-08-30 16:03:44 -030099 cx18_write_reg(cx, v, CX18_REG_DMUX_NUM_PORT_0_CONTROL);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300100 break;
101
102 default:
103 /* Assumption - Parallel transport - Signalling
104 * undefined or default.
105 */
106 break;
107 }
108
109 if (!demux->dmx.frontend)
110 return -EINVAL;
111
Andy Wallsf68d0cf2008-11-05 21:19:15 -0300112 if (!stream)
113 return -EINVAL;
114
115 mutex_lock(&stream->dvb.feedlock);
116 if (stream->dvb.feeding++ == 0) {
117 CX18_DEBUG_INFO("Starting Transport DMA\n");
118 set_bit(CX18_F_S_STREAMING, &stream->s_flags);
119 ret = cx18_start_v4l2_encode_stream(stream);
120 if (ret < 0) {
121 CX18_DEBUG_INFO("Failed to start Transport DMA\n");
122 stream->dvb.feeding--;
123 if (stream->dvb.feeding == 0)
124 clear_bit(CX18_F_S_STREAMING, &stream->s_flags);
125 }
126 } else
127 ret = 0;
128 mutex_unlock(&stream->dvb.feedlock);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300129
130 return ret;
131}
132
133/* Kernel DVB framework calls this when the feed needs to stop. */
134static int cx18_dvb_stop_feed(struct dvb_demux_feed *feed)
135{
136 struct dvb_demux *demux = feed->demux;
137 struct cx18_stream *stream = (struct cx18_stream *)demux->priv;
138 struct cx18 *cx = stream->cx;
139 int ret = -EINVAL;
140
141 CX18_DEBUG_INFO("Stop feed: pid = 0x%x index = %d\n",
142 feed->pid, feed->index);
143
144 if (stream) {
145 mutex_lock(&stream->dvb.feedlock);
146 if (--stream->dvb.feeding == 0) {
147 CX18_DEBUG_INFO("Stopping Transport DMA\n");
148 ret = cx18_stop_v4l2_encode_stream(stream, 0);
149 } else
150 ret = 0;
151 mutex_unlock(&stream->dvb.feedlock);
152 }
153
154 return ret;
155}
156
157int cx18_dvb_register(struct cx18_stream *stream)
158{
159 struct cx18 *cx = stream->cx;
160 struct cx18_dvb *dvb = &stream->dvb;
161 struct dvb_adapter *dvb_adapter;
162 struct dvb_demux *dvbdemux;
163 struct dmx_demux *dmx;
164 int ret;
165
166 if (!dvb)
167 return -EINVAL;
168
169 ret = dvb_register_adapter(&dvb->dvb_adapter,
170 CX18_DRIVER_NAME,
Andy Walls3d059132009-01-10 21:54:39 -0300171 THIS_MODULE, &cx->pci_dev->dev, adapter_nr);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300172 if (ret < 0)
173 goto err_out;
174
175 dvb_adapter = &dvb->dvb_adapter;
176
177 dvbdemux = &dvb->demux;
178
179 dvbdemux->priv = (void *)stream;
180
181 dvbdemux->filternum = 256;
182 dvbdemux->feednum = 256;
183 dvbdemux->start_feed = cx18_dvb_start_feed;
184 dvbdemux->stop_feed = cx18_dvb_stop_feed;
185 dvbdemux->dmx.capabilities = (DMX_TS_FILTERING |
186 DMX_SECTION_FILTERING | DMX_MEMORY_BASED_FILTERING);
187 ret = dvb_dmx_init(dvbdemux);
188 if (ret < 0)
189 goto err_dvb_unregister_adapter;
190
191 dmx = &dvbdemux->dmx;
192
193 dvb->hw_frontend.source = DMX_FRONTEND_0;
194 dvb->mem_frontend.source = DMX_MEMORY_FE;
195 dvb->dmxdev.filternum = 256;
196 dvb->dmxdev.demux = dmx;
197
198 ret = dvb_dmxdev_init(&dvb->dmxdev, dvb_adapter);
199 if (ret < 0)
200 goto err_dvb_dmx_release;
201
202 ret = dmx->add_frontend(dmx, &dvb->hw_frontend);
203 if (ret < 0)
204 goto err_dvb_dmxdev_release;
205
206 ret = dmx->add_frontend(dmx, &dvb->mem_frontend);
207 if (ret < 0)
208 goto err_remove_hw_frontend;
209
210 ret = dmx->connect_frontend(dmx, &dvb->hw_frontend);
211 if (ret < 0)
212 goto err_remove_mem_frontend;
213
214 ret = dvb_register(stream);
215 if (ret < 0)
216 goto err_disconnect_frontend;
217
218 dvb_net_init(dvb_adapter, &dvb->dvbnet, dmx);
219
220 CX18_INFO("DVB Frontend registered\n");
Andy Walls6ecd86d2008-12-07 23:30:17 -0300221 CX18_INFO("Registered DVB adapter%d for %s (%d x %d kB)\n",
222 stream->dvb.dvb_adapter.num, stream->name,
223 stream->buffers, stream->buf_size/1024);
224
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300225 mutex_init(&dvb->feedlock);
226 dvb->enabled = 1;
227 return ret;
228
229err_disconnect_frontend:
230 dmx->disconnect_frontend(dmx);
231err_remove_mem_frontend:
232 dmx->remove_frontend(dmx, &dvb->mem_frontend);
233err_remove_hw_frontend:
234 dmx->remove_frontend(dmx, &dvb->hw_frontend);
235err_dvb_dmxdev_release:
236 dvb_dmxdev_release(&dvb->dmxdev);
237err_dvb_dmx_release:
238 dvb_dmx_release(dvbdemux);
239err_dvb_unregister_adapter:
240 dvb_unregister_adapter(dvb_adapter);
241err_out:
242 return ret;
243}
244
245void cx18_dvb_unregister(struct cx18_stream *stream)
246{
247 struct cx18 *cx = stream->cx;
248 struct cx18_dvb *dvb = &stream->dvb;
249 struct dvb_adapter *dvb_adapter;
250 struct dvb_demux *dvbdemux;
251 struct dmx_demux *dmx;
252
253 CX18_INFO("unregister DVB\n");
254
255 dvb_adapter = &dvb->dvb_adapter;
256 dvbdemux = &dvb->demux;
257 dmx = &dvbdemux->dmx;
258
259 dmx->close(dmx);
260 dvb_net_release(&dvb->dvbnet);
261 dmx->remove_frontend(dmx, &dvb->mem_frontend);
262 dmx->remove_frontend(dmx, &dvb->hw_frontend);
263 dvb_dmxdev_release(&dvb->dmxdev);
264 dvb_dmx_release(dvbdemux);
265 dvb_unregister_frontend(dvb->fe);
266 dvb_frontend_detach(dvb->fe);
267 dvb_unregister_adapter(dvb_adapter);
268}
269
270/* All the DVB attach calls go here, this function get's modified
271 * for each new card. No other function in this file needs
272 * to change.
273 */
274static int dvb_register(struct cx18_stream *stream)
275{
276 struct cx18_dvb *dvb = &stream->dvb;
277 struct cx18 *cx = stream->cx;
278 int ret = 0;
279
280 switch (cx->card->type) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300281 case CX18_CARD_HVR_1600_ESMT:
282 case CX18_CARD_HVR_1600_SAMSUNG:
283 dvb->fe = dvb_attach(s5h1409_attach,
284 &hauppauge_hvr1600_config,
285 &cx->i2c_adap[0]);
286 if (dvb->fe != NULL) {
Steven Toth67129472008-05-01 07:23:23 -0300287 dvb_attach(mxl5005s_attach, dvb->fe,
288 &cx->i2c_adap[0],
289 &hauppauge_hvr1600_tuner);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300290 ret = 0;
291 }
292 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300293 default:
294 /* No Digital Tv Support */
295 break;
296 }
297
298 if (dvb->fe == NULL) {
299 CX18_ERR("frontend initialization failed\n");
300 return -1;
301 }
302
303 ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
304 if (ret < 0) {
305 if (dvb->fe->ops.release)
306 dvb->fe->ops.release(dvb->fe);
307 return ret;
308 }
309
310 return ret;
311}