blob: 4845f732ef486a12cd249506f754e4af594eaa27 [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>
Hans Verkuil1c1e45d2008-04-28 20:24:33 -03005 *
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.
10 *
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
14 *
15 * GNU General Public License for more details.
16 *
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.
20 */
21
22#include "cx18-version.h"
23#include "cx18-dvb.h"
Andy Wallsb1526422008-08-30 16:03:44 -030024#include "cx18-io.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030025#include "cx18-streams.h"
Andy Walls1d6782b2008-11-05 00:49:14 -030026#include "cx18-queue.h"
27#include "cx18-scb.h"
Hans Verkuil1c1e45d2008-04-28 20:24:33 -030028#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,
171 THIS_MODULE, &cx->dev->dev, adapter_nr);
172 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");
221 mutex_init(&dvb->feedlock);
222 dvb->enabled = 1;
223 return ret;
224
225err_disconnect_frontend:
226 dmx->disconnect_frontend(dmx);
227err_remove_mem_frontend:
228 dmx->remove_frontend(dmx, &dvb->mem_frontend);
229err_remove_hw_frontend:
230 dmx->remove_frontend(dmx, &dvb->hw_frontend);
231err_dvb_dmxdev_release:
232 dvb_dmxdev_release(&dvb->dmxdev);
233err_dvb_dmx_release:
234 dvb_dmx_release(dvbdemux);
235err_dvb_unregister_adapter:
236 dvb_unregister_adapter(dvb_adapter);
237err_out:
238 return ret;
239}
240
241void cx18_dvb_unregister(struct cx18_stream *stream)
242{
243 struct cx18 *cx = stream->cx;
244 struct cx18_dvb *dvb = &stream->dvb;
245 struct dvb_adapter *dvb_adapter;
246 struct dvb_demux *dvbdemux;
247 struct dmx_demux *dmx;
248
249 CX18_INFO("unregister DVB\n");
250
251 dvb_adapter = &dvb->dvb_adapter;
252 dvbdemux = &dvb->demux;
253 dmx = &dvbdemux->dmx;
254
255 dmx->close(dmx);
256 dvb_net_release(&dvb->dvbnet);
257 dmx->remove_frontend(dmx, &dvb->mem_frontend);
258 dmx->remove_frontend(dmx, &dvb->hw_frontend);
259 dvb_dmxdev_release(&dvb->dmxdev);
260 dvb_dmx_release(dvbdemux);
261 dvb_unregister_frontend(dvb->fe);
262 dvb_frontend_detach(dvb->fe);
263 dvb_unregister_adapter(dvb_adapter);
264}
265
266/* All the DVB attach calls go here, this function get's modified
267 * for each new card. No other function in this file needs
268 * to change.
269 */
270static int dvb_register(struct cx18_stream *stream)
271{
272 struct cx18_dvb *dvb = &stream->dvb;
273 struct cx18 *cx = stream->cx;
274 int ret = 0;
275
276 switch (cx->card->type) {
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300277 case CX18_CARD_HVR_1600_ESMT:
278 case CX18_CARD_HVR_1600_SAMSUNG:
279 dvb->fe = dvb_attach(s5h1409_attach,
280 &hauppauge_hvr1600_config,
281 &cx->i2c_adap[0]);
282 if (dvb->fe != NULL) {
Steven Toth67129472008-05-01 07:23:23 -0300283 dvb_attach(mxl5005s_attach, dvb->fe,
284 &cx->i2c_adap[0],
285 &hauppauge_hvr1600_tuner);
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300286 ret = 0;
287 }
288 break;
Hans Verkuil1c1e45d2008-04-28 20:24:33 -0300289 default:
290 /* No Digital Tv Support */
291 break;
292 }
293
294 if (dvb->fe == NULL) {
295 CX18_ERR("frontend initialization failed\n");
296 return -1;
297 }
298
299 ret = dvb_register_frontend(&dvb->dvb_adapter, dvb->fe);
300 if (ret < 0) {
301 if (dvb->fe->ops.release)
302 dvb->fe->ops.release(dvb->fe);
303 return ret;
304 }
305
306 return ret;
307}
Andy Walls1d6782b2008-11-05 00:49:14 -0300308
309void cx18_dvb_work_handler(struct cx18 *cx)
310{
311 struct cx18_buffer *buf;
312 struct cx18_stream *s = &cx->streams[CX18_ENC_STREAM_TYPE_TS];
313
314 while ((buf = cx18_dequeue(s, &s->q_full)) != NULL) {
315 if (s->dvb.enabled)
316 dvb_dmx_swfilter(&s->dvb.demux, buf->buf,
317 buf->bytesused);
318
Andy Walls1d6782b2008-11-05 00:49:14 -0300319 cx18_buf_sync_for_device(s, buf);
Andy Wallsf68d0cf2008-11-05 21:19:15 -0300320 cx18_enqueue(s, buf, &s->q_free);
321
322 if (s->handle == CX18_INVALID_TASK_HANDLE ||
323 !test_bit(CX18_F_S_STREAMING, &s->s_flags))
Andy Walls1d6782b2008-11-05 00:49:14 -0300324 continue;
325
326 cx18_vapi(cx, CX18_CPU_DE_SET_MDL, 5, s->handle,
327 (void __iomem *)&cx->scb->cpu_mdl[buf->id] - cx->enc_mem,
328 1, buf->id, s->buf_size);
329 }
330}