blob: 53884814161c2a064853de36a8557b14b04aad75 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * av7110_av.c: audio and video MPEG decoder stuff
3 *
4 * Copyright (C) 1999-2002 Ralph Metzler
5 * & Marcus Metzler for convergence integrated media GmbH
6 *
7 * originally based on code by:
8 * Copyright (C) 1998,1999 Christian Theiss <mistert@rz.fh-augsburg.de>
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 * Or, point your browser to http://www.gnu.org/copyleft/gpl.html
26 *
27 *
28 * the project's page is at http://www.linuxtv.org/dvb/
29 */
30
31#include <linux/types.h>
32#include <linux/kernel.h>
33#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/fs.h>
36
37#include "av7110.h"
38#include "av7110_hw.h"
39#include "av7110_av.h"
40#include "av7110_ipack.h"
41
42/* MPEG-2 (ISO 13818 / H.222.0) stream types */
43#define PROG_STREAM_MAP 0xBC
44#define PRIVATE_STREAM1 0xBD
45#define PADDING_STREAM 0xBE
46#define PRIVATE_STREAM2 0xBF
47#define AUDIO_STREAM_S 0xC0
48#define AUDIO_STREAM_E 0xDF
49#define VIDEO_STREAM_S 0xE0
50#define VIDEO_STREAM_E 0xEF
51#define ECM_STREAM 0xF0
52#define EMM_STREAM 0xF1
53#define DSM_CC_STREAM 0xF2
54#define ISO13522_STREAM 0xF3
55#define PROG_STREAM_DIR 0xFF
56
57#define PTS_DTS_FLAGS 0xC0
58
59//pts_dts flags
60#define PTS_ONLY 0x80
61#define PTS_DTS 0xC0
62#define TS_SIZE 188
63#define TRANS_ERROR 0x80
64#define PAY_START 0x40
65#define TRANS_PRIO 0x20
66#define PID_MASK_HI 0x1F
67//flags
68#define TRANS_SCRMBL1 0x80
69#define TRANS_SCRMBL2 0x40
70#define ADAPT_FIELD 0x20
71#define PAYLOAD 0x10
72#define COUNT_MASK 0x0F
73
74// adaptation flags
75#define DISCON_IND 0x80
76#define RAND_ACC_IND 0x40
77#define ES_PRI_IND 0x20
78#define PCR_FLAG 0x10
79#define OPCR_FLAG 0x08
80#define SPLICE_FLAG 0x04
81#define TRANS_PRIV 0x02
82#define ADAP_EXT_FLAG 0x01
83
84// adaptation extension flags
85#define LTW_FLAG 0x80
86#define PIECE_RATE 0x40
87#define SEAM_SPLICE 0x20
88
89
90static void p_to_t(u8 const *buf, long int length, u16 pid,
91 u8 *counter, struct dvb_demux_feed *feed);
Oliver Endrissfd46d162009-04-10 10:20:00 -030092static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94
95int av7110_record_cb(struct dvb_filter_pes2ts *p2t, u8 *buf, size_t len)
96{
97 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) p2t->priv;
98
99 if (!(dvbdmxfeed->ts_type & TS_PACKET))
100 return 0;
101 if (buf[3] == 0xe0) // video PES do not have a length in TS
102 buf[4] = buf[5] = 0;
103 if (dvbdmxfeed->ts_type & TS_PAYLOAD_ONLY)
104 return dvbdmxfeed->cb.ts(buf, len, NULL, 0,
105 &dvbdmxfeed->feed.ts, DMX_OK);
106 else
107 return dvb_filter_pes2ts(p2t, buf, len, 1);
108}
109
110static int dvb_filter_pes2ts_cb(void *priv, unsigned char *data)
111{
112 struct dvb_demux_feed *dvbdmxfeed = (struct dvb_demux_feed *) priv;
113
114 dvbdmxfeed->cb.ts(data, 188, NULL, 0,
115 &dvbdmxfeed->feed.ts, DMX_OK);
116 return 0;
117}
118
119int av7110_av_start_record(struct av7110 *av7110, int av,
120 struct dvb_demux_feed *dvbdmxfeed)
121{
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700122 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 struct dvb_demux *dvbdmx = dvbdmxfeed->demux;
124
125 dprintk(2, "av7110:%p, , dvb_demux_feed:%p\n", av7110, dvbdmxfeed);
126
127 if (av7110->playing || (av7110->rec_mode & av))
128 return -EBUSY;
129 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
130 dvbdmx->recording = 1;
131 av7110->rec_mode |= av;
132
133 switch (av7110->rec_mode) {
134 case RP_AUDIO:
135 dvb_filter_pes2ts_init(&av7110->p2t[0],
136 dvbdmx->pesfilter[0]->pid,
137 dvb_filter_pes2ts_cb,
138 (void *) dvbdmx->pesfilter[0]);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700139 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 break;
141
142 case RP_VIDEO:
143 dvb_filter_pes2ts_init(&av7110->p2t[1],
144 dvbdmx->pesfilter[1]->pid,
145 dvb_filter_pes2ts_cb,
146 (void *) dvbdmx->pesfilter[1]);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700147 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 break;
149
150 case RP_AV:
151 dvb_filter_pes2ts_init(&av7110->p2t[0],
152 dvbdmx->pesfilter[0]->pid,
153 dvb_filter_pes2ts_cb,
154 (void *) dvbdmx->pesfilter[0]);
155 dvb_filter_pes2ts_init(&av7110->p2t[1],
156 dvbdmx->pesfilter[1]->pid,
157 dvb_filter_pes2ts_cb,
158 (void *) dvbdmx->pesfilter[1]);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700159 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AV_PES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 break;
161 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700162 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
165int av7110_av_start_play(struct av7110 *av7110, int av)
166{
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700167 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 dprintk(2, "av7110:%p, \n", av7110);
169
170 if (av7110->rec_mode)
171 return -EBUSY;
172 if (av7110->playing & av)
173 return -EBUSY;
174
175 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
176
177 if (av7110->playing == RP_NONE) {
178 av7110_ipack_reset(&av7110->ipack[0]);
179 av7110_ipack_reset(&av7110->ipack[1]);
180 }
181
182 av7110->playing |= av;
183 switch (av7110->playing) {
184 case RP_AUDIO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700185 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 break;
187 case RP_VIDEO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700188 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 av7110->sinfo = 0;
190 break;
191 case RP_AV:
192 av7110->sinfo = 0;
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700193 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AV_PES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 break;
195 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700196 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700199int av7110_av_stop(struct av7110 *av7110, int av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700201 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 dprintk(2, "av7110:%p, \n", av7110);
203
204 if (!(av7110->playing & av) && !(av7110->rec_mode & av))
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700205 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
207 if (av7110->playing) {
208 av7110->playing &= ~av;
209 switch (av7110->playing) {
210 case RP_AUDIO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700211 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, AudioPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 break;
213 case RP_VIDEO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700214 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Play, 2, VideoPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 break;
216 case RP_NONE:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700217 ret = av7110_set_vidmode(av7110, av7110->vidmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 break;
219 }
220 } else {
221 av7110->rec_mode &= ~av;
222 switch (av7110->rec_mode) {
223 case RP_AUDIO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700224 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, AudioPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 break;
226 case RP_VIDEO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700227 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Record, 2, VideoPES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 break;
229 case RP_NONE:
230 break;
231 }
232 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700233 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234}
235
236
237int av7110_pes_play(void *dest, struct dvb_ringbuffer *buf, int dlen)
238{
239 int len;
240 u32 sync;
241 u16 blen;
242
243 if (!dlen) {
244 wake_up(&buf->queue);
245 return -1;
246 }
247 while (1) {
248 if ((len = dvb_ringbuffer_avail(buf)) < 6)
249 return -1;
250 sync = DVB_RINGBUFFER_PEEK(buf, 0) << 24;
251 sync |= DVB_RINGBUFFER_PEEK(buf, 1) << 16;
252 sync |= DVB_RINGBUFFER_PEEK(buf, 2) << 8;
253 sync |= DVB_RINGBUFFER_PEEK(buf, 3);
254
255 if (((sync &~ 0x0f) == 0x000001e0) ||
256 ((sync &~ 0x1f) == 0x000001c0) ||
257 (sync == 0x000001bd))
258 break;
259 printk("resync\n");
260 DVB_RINGBUFFER_SKIP(buf, 1);
261 }
262 blen = DVB_RINGBUFFER_PEEK(buf, 4) << 8;
263 blen |= DVB_RINGBUFFER_PEEK(buf, 5);
264 blen += 6;
265 if (len < blen || blen > dlen) {
266 //printk("buffer empty - avail %d blen %u dlen %d\n", len, blen, dlen);
267 wake_up(&buf->queue);
268 return -1;
269 }
270
Al Virob0ba0e32008-06-22 14:20:29 -0300271 dvb_ringbuffer_read(buf, dest, (size_t) blen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
273 dprintk(2, "pread=0x%08lx, pwrite=0x%08lx\n",
274 (unsigned long) buf->pread, (unsigned long) buf->pwrite);
275 wake_up(&buf->queue);
276 return blen;
277}
278
279
280int av7110_set_volume(struct av7110 *av7110, int volleft, int volright)
281{
282 int err, vol, val, balance = 0;
283
284 dprintk(2, "av7110:%p, \n", av7110);
285
286 av7110->mixer.volume_left = volleft;
287 av7110->mixer.volume_right = volright;
288
289 switch (av7110->adac_type) {
290 case DVB_ADAC_TI:
291 volleft = (volleft * 256) / 1036;
292 volright = (volright * 256) / 1036;
293 if (volleft > 0x3f)
294 volleft = 0x3f;
295 if (volright > 0x3f)
296 volright = 0x3f;
297 if ((err = SendDAC(av7110, 3, 0x80 + volleft)))
298 return err;
299 return SendDAC(av7110, 4, volright);
300
301 case DVB_ADAC_CRYSTAL:
302 volleft = 127 - volleft / 2;
303 volright = 127 - volright / 2;
304 i2c_writereg(av7110, 0x20, 0x03, volleft);
305 i2c_writereg(av7110, 0x20, 0x04, volright);
306 return 0;
307
Marco Schluessler1c13b952006-01-09 15:25:06 -0200308 case DVB_ADAC_MSP34x0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 vol = (volleft > volright) ? volleft : volright;
310 val = (vol * 0x73 / 255) << 8;
311 if (vol > 0)
312 balance = ((volright - volleft) * 127) / vol;
313 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
314 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
315 msp_writereg(av7110, MSP_WR_DSP, 0x0006, val); /* headphonesr */
316 return 0;
Tim Kaiser61391e02006-06-25 09:14:07 -0300317
318 case DVB_ADAC_MSP34x5:
319 vol = (volleft > volright) ? volleft : volright;
320 val = (vol * 0x73 / 255) << 8;
321 if (vol > 0)
322 balance = ((volright - volleft) * 127) / vol;
323 msp_writereg(av7110, MSP_WR_DSP, 0x0001, balance << 8);
324 msp_writereg(av7110, MSP_WR_DSP, 0x0000, val); /* loudspeaker */
325 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
Tim Kaiser61391e02006-06-25 09:14:07 -0300327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return 0;
329}
330
Marco Schluessler58a44042007-10-31 01:20:42 -0300331int av7110_set_vidmode(struct av7110 *av7110, enum av7110_video_mode mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332{
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700333 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 dprintk(2, "av7110:%p, \n", av7110);
335
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700336 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, LoadVidCode, 1, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700338 if (!ret && !av7110->playing) {
339 ret = ChangePIDs(av7110, av7110->pids[DMX_PES_VIDEO],
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 av7110->pids[DMX_PES_AUDIO],
341 av7110->pids[DMX_PES_TELETEXT],
342 0, av7110->pids[DMX_PES_PCR]);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700343 if (!ret)
344 ret = av7110_fw_cmd(av7110, COMTYPE_PIDFILTER, Scan, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700346 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349
Marco Schluessler58a44042007-10-31 01:20:42 -0300350static enum av7110_video_mode sw2mode[16] = {
351 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
352 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_PAL,
353 AV7110_VIDEO_MODE_NTSC, AV7110_VIDEO_MODE_NTSC,
354 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_NTSC,
355 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
356 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
357 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
358 AV7110_VIDEO_MODE_PAL, AV7110_VIDEO_MODE_PAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359};
360
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700361static int get_video_format(struct av7110 *av7110, u8 *buf, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int i;
364 int hsize, vsize;
365 int sw;
366 u8 *p;
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700367 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 dprintk(2, "av7110:%p, \n", av7110);
370
371 if (av7110->sinfo)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700372 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 for (i = 7; i < count - 10; i++) {
374 p = buf + i;
375 if (p[0] || p[1] || p[2] != 0x01 || p[3] != 0xb3)
376 continue;
377 p += 4;
378 hsize = ((p[1] &0xF0) >> 4) | (p[0] << 4);
379 vsize = ((p[1] &0x0F) << 8) | (p[2]);
380 sw = (p[3] & 0x0F);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700381 ret = av7110_set_vidmode(av7110, sw2mode[sw]);
382 if (!ret) {
383 dprintk(2, "playback %dx%d fr=%d\n", hsize, vsize, sw);
384 av7110->sinfo = 1;
385 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -0700388 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389}
390
391
392/****************************************************************************
393 * I/O buffer management and control
394 ****************************************************************************/
395
396static inline long aux_ring_buffer_write(struct dvb_ringbuffer *rbuf,
Oliver Endriss804b4452007-07-12 20:37:50 -0300397 const u8 *buf, unsigned long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 unsigned long todo = count;
400 int free;
401
402 while (todo > 0) {
403 if (dvb_ringbuffer_free(rbuf) < 2048) {
404 if (wait_event_interruptible(rbuf->queue,
405 (dvb_ringbuffer_free(rbuf) >= 2048)))
406 return count - todo;
407 }
408 free = dvb_ringbuffer_free(rbuf);
409 if (free > todo)
410 free = todo;
411 dvb_ringbuffer_write(rbuf, buf, free);
412 todo -= free;
413 buf += free;
414 }
415
416 return count - todo;
417}
418
419static void play_video_cb(u8 *buf, int count, void *priv)
420{
421 struct av7110 *av7110 = (struct av7110 *) priv;
422 dprintk(2, "av7110:%p, \n", av7110);
423
424 if ((buf[3] & 0xe0) == 0xe0) {
425 get_video_format(av7110, buf, count);
426 aux_ring_buffer_write(&av7110->avout, buf, count);
427 } else
428 aux_ring_buffer_write(&av7110->aout, buf, count);
429}
430
431static void play_audio_cb(u8 *buf, int count, void *priv)
432{
433 struct av7110 *av7110 = (struct av7110 *) priv;
434 dprintk(2, "av7110:%p, \n", av7110);
435
436 aux_ring_buffer_write(&av7110->aout, buf, count);
437}
438
Oliver Endrissfd46d162009-04-10 10:20:00 -0300439
440#define FREE_COND_TS (dvb_ringbuffer_free(rb) >= 4096)
441
442static ssize_t ts_play(struct av7110 *av7110, const char __user *buf,
443 unsigned long count, int nonblock, int type)
444{
445 struct dvb_ringbuffer *rb;
446 u8 *kb;
447 unsigned long todo = count;
448
449 dprintk(2, "%s: type %d cnt %lu\n", __func__, type, count);
450
451 rb = (type) ? &av7110->avout : &av7110->aout;
452 kb = av7110->kbuf[type];
453
454 if (!kb)
455 return -ENOBUFS;
456
457 if (nonblock && !FREE_COND_TS)
458 return -EWOULDBLOCK;
459
460 while (todo >= TS_SIZE) {
461 if (!FREE_COND_TS) {
462 if (nonblock)
463 return count - todo;
464 if (wait_event_interruptible(rb->queue, FREE_COND_TS))
465 return count - todo;
466 }
467 if (copy_from_user(kb, buf, TS_SIZE))
468 return -EFAULT;
469 write_ts_to_decoder(av7110, type, kb, TS_SIZE);
470 todo -= TS_SIZE;
471 buf += TS_SIZE;
472 }
473
474 return count - todo;
475}
476
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478#define FREE_COND (dvb_ringbuffer_free(&av7110->avout) >= 20 * 1024 && \
479 dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
480
Oliver Endriss804b4452007-07-12 20:37:50 -0300481static ssize_t dvb_play(struct av7110 *av7110, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unsigned long count, int nonblock, int type)
483{
484 unsigned long todo = count, n;
485 dprintk(2, "av7110:%p, \n", av7110);
486
487 if (!av7110->kbuf[type])
488 return -ENOBUFS;
489
490 if (nonblock && !FREE_COND)
491 return -EWOULDBLOCK;
492
493 while (todo > 0) {
494 if (!FREE_COND) {
495 if (nonblock)
496 return count - todo;
497 if (wait_event_interruptible(av7110->avout.queue,
498 FREE_COND))
499 return count - todo;
500 }
501 n = todo;
502 if (n > IPACKS * 2)
503 n = IPACKS * 2;
504 if (copy_from_user(av7110->kbuf[type], buf, n))
505 return -EFAULT;
506 av7110_ipack_instant_repack(av7110->kbuf[type], n,
507 &av7110->ipack[type]);
508 todo -= n;
509 buf += n;
510 }
511 return count - todo;
512}
513
514static ssize_t dvb_play_kernel(struct av7110 *av7110, const u8 *buf,
515 unsigned long count, int nonblock, int type)
516{
517 unsigned long todo = count, n;
518 dprintk(2, "av7110:%p, \n", av7110);
519
520 if (!av7110->kbuf[type])
521 return -ENOBUFS;
522
523 if (nonblock && !FREE_COND)
524 return -EWOULDBLOCK;
525
526 while (todo > 0) {
527 if (!FREE_COND) {
528 if (nonblock)
529 return count - todo;
530 if (wait_event_interruptible(av7110->avout.queue,
531 FREE_COND))
532 return count - todo;
533 }
534 n = todo;
535 if (n > IPACKS * 2)
536 n = IPACKS * 2;
537 av7110_ipack_instant_repack(buf, n, &av7110->ipack[type]);
538 todo -= n;
539 buf += n;
540 }
541 return count - todo;
542}
543
Oliver Endriss804b4452007-07-12 20:37:50 -0300544static ssize_t dvb_aplay(struct av7110 *av7110, const char __user *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 unsigned long count, int nonblock, int type)
546{
547 unsigned long todo = count, n;
548 dprintk(2, "av7110:%p, \n", av7110);
549
550 if (!av7110->kbuf[type])
551 return -ENOBUFS;
552 if (nonblock && dvb_ringbuffer_free(&av7110->aout) < 20 * 1024)
553 return -EWOULDBLOCK;
554
555 while (todo > 0) {
556 if (dvb_ringbuffer_free(&av7110->aout) < 20 * 1024) {
557 if (nonblock)
558 return count - todo;
559 if (wait_event_interruptible(av7110->aout.queue,
560 (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)))
561 return count-todo;
562 }
563 n = todo;
564 if (n > IPACKS * 2)
565 n = IPACKS * 2;
566 if (copy_from_user(av7110->kbuf[type], buf, n))
567 return -EFAULT;
568 av7110_ipack_instant_repack(av7110->kbuf[type], n,
569 &av7110->ipack[type]);
570 todo -= n;
571 buf += n;
572 }
573 return count - todo;
574}
575
576void av7110_p2t_init(struct av7110_p2t *p, struct dvb_demux_feed *feed)
577{
578 memset(p->pes, 0, TS_SIZE);
579 p->counter = 0;
580 p->pos = 0;
581 p->frags = 0;
582 if (feed)
583 p->feed = feed;
584}
585
586static void clear_p2t(struct av7110_p2t *p)
587{
588 memset(p->pes, 0, TS_SIZE);
589// p->counter = 0;
590 p->pos = 0;
591 p->frags = 0;
592}
593
594
595static int find_pes_header(u8 const *buf, long int length, int *frags)
596{
597 int c = 0;
598 int found = 0;
599
600 *frags = 0;
601
602 while (c < length - 3 && !found) {
603 if (buf[c] == 0x00 && buf[c + 1] == 0x00 &&
604 buf[c + 2] == 0x01) {
605 switch ( buf[c + 3] ) {
606 case PROG_STREAM_MAP:
607 case PRIVATE_STREAM2:
608 case PROG_STREAM_DIR:
609 case ECM_STREAM :
610 case EMM_STREAM :
611 case PADDING_STREAM :
612 case DSM_CC_STREAM :
613 case ISO13522_STREAM:
614 case PRIVATE_STREAM1:
615 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
616 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
617 found = 1;
618 break;
619
620 default:
621 c++;
622 break;
623 }
624 } else
625 c++;
626 }
627 if (c == length - 3 && !found) {
628 if (buf[length - 1] == 0x00)
629 *frags = 1;
630 if (buf[length - 2] == 0x00 &&
631 buf[length - 1] == 0x00)
632 *frags = 2;
633 if (buf[length - 3] == 0x00 &&
634 buf[length - 2] == 0x00 &&
635 buf[length - 1] == 0x01)
636 *frags = 3;
637 return -1;
638 }
639
640 return c;
641}
642
643void av7110_p2t_write(u8 const *buf, long int length, u16 pid, struct av7110_p2t *p)
644{
645 int c, c2, l, add;
646 int check, rest;
647
648 c = 0;
649 c2 = 0;
650 if (p->frags){
651 check = 0;
652 switch(p->frags) {
653 case 1:
654 if (buf[c] == 0x00 && buf[c + 1] == 0x01) {
655 check = 1;
656 c += 2;
657 }
658 break;
659 case 2:
660 if (buf[c] == 0x01) {
661 check = 1;
662 c++;
663 }
664 break;
665 case 3:
666 check = 1;
667 }
668 if (check) {
669 switch (buf[c]) {
670 case PROG_STREAM_MAP:
671 case PRIVATE_STREAM2:
672 case PROG_STREAM_DIR:
673 case ECM_STREAM :
674 case EMM_STREAM :
675 case PADDING_STREAM :
676 case DSM_CC_STREAM :
677 case ISO13522_STREAM:
678 case PRIVATE_STREAM1:
679 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
680 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
681 p->pes[0] = 0x00;
682 p->pes[1] = 0x00;
683 p->pes[2] = 0x01;
684 p->pes[3] = buf[c];
685 p->pos = 4;
686 memcpy(p->pes + p->pos, buf + c, (TS_SIZE - 4) - p->pos);
687 c += (TS_SIZE - 4) - p->pos;
688 p_to_t(p->pes, (TS_SIZE - 4), pid, &p->counter, p->feed);
689 clear_p2t(p);
690 break;
691
692 default:
693 c = 0;
694 break;
695 }
696 }
697 p->frags = 0;
698 }
699
700 if (p->pos) {
701 c2 = find_pes_header(buf + c, length - c, &p->frags);
702 if (c2 >= 0 && c2 < (TS_SIZE - 4) - p->pos)
703 l = c2+c;
704 else
705 l = (TS_SIZE - 4) - p->pos;
706 memcpy(p->pes + p->pos, buf, l);
707 c += l;
708 p->pos += l;
709 p_to_t(p->pes, p->pos, pid, &p->counter, p->feed);
710 clear_p2t(p);
711 }
712
713 add = 0;
714 while (c < length) {
715 c2 = find_pes_header(buf + c + add, length - c - add, &p->frags);
716 if (c2 >= 0) {
717 c2 += c + add;
718 if (c2 > c){
719 p_to_t(buf + c, c2 - c, pid, &p->counter, p->feed);
720 c = c2;
721 clear_p2t(p);
722 add = 0;
723 } else
724 add = 1;
725 } else {
726 l = length - c;
727 rest = l % (TS_SIZE - 4);
728 l -= rest;
729 p_to_t(buf + c, l, pid, &p->counter, p->feed);
730 memcpy(p->pes, buf + c + l, rest);
731 p->pos = rest;
732 c = length;
733 }
734 }
735}
736
737
738static int write_ts_header2(u16 pid, u8 *counter, int pes_start, u8 *buf, u8 length)
739{
740 int i;
741 int c = 0;
742 int fill;
743 u8 tshead[4] = { 0x47, 0x00, 0x00, 0x10 };
744
745 fill = (TS_SIZE - 4) - length;
746 if (pes_start)
747 tshead[1] = 0x40;
748 if (fill)
749 tshead[3] = 0x30;
750 tshead[1] |= (u8)((pid & 0x1F00) >> 8);
751 tshead[2] |= (u8)(pid & 0x00FF);
752 tshead[3] |= ((*counter)++ & 0x0F);
753 memcpy(buf, tshead, 4);
754 c += 4;
755
756 if (fill) {
757 buf[4] = fill - 1;
758 c++;
759 if (fill > 1) {
760 buf[5] = 0x00;
761 c++;
762 }
763 for (i = 6; i < fill + 4; i++) {
764 buf[i] = 0xFF;
765 c++;
766 }
767 }
768
769 return c;
770}
771
772
773static void p_to_t(u8 const *buf, long int length, u16 pid, u8 *counter,
774 struct dvb_demux_feed *feed)
775{
776 int l, pes_start;
777 u8 obuf[TS_SIZE];
778 long c = 0;
779
780 pes_start = 0;
781 if (length > 3 &&
782 buf[0] == 0x00 && buf[1] == 0x00 && buf[2] == 0x01)
783 switch (buf[3]) {
784 case PROG_STREAM_MAP:
785 case PRIVATE_STREAM2:
786 case PROG_STREAM_DIR:
787 case ECM_STREAM :
788 case EMM_STREAM :
789 case PADDING_STREAM :
790 case DSM_CC_STREAM :
791 case ISO13522_STREAM:
792 case PRIVATE_STREAM1:
793 case AUDIO_STREAM_S ... AUDIO_STREAM_E:
794 case VIDEO_STREAM_S ... VIDEO_STREAM_E:
795 pes_start = 1;
796 break;
797
798 default:
799 break;
800 }
801
802 while (c < length) {
803 memset(obuf, 0, TS_SIZE);
804 if (length - c >= (TS_SIZE - 4)){
805 l = write_ts_header2(pid, counter, pes_start,
806 obuf, (TS_SIZE - 4));
807 memcpy(obuf + l, buf + c, TS_SIZE - l);
808 c += TS_SIZE - l;
809 } else {
810 l = write_ts_header2(pid, counter, pes_start,
811 obuf, length - c);
812 memcpy(obuf + l, buf + c, TS_SIZE - l);
813 c = length;
814 }
815 feed->cb.ts(obuf, 188, NULL, 0, &feed->feed.ts, DMX_OK);
816 pes_start = 0;
817 }
818}
819
820
Oliver Endrissfd46d162009-04-10 10:20:00 -0300821static int write_ts_to_decoder(struct av7110 *av7110, int type, const u8 *buf, size_t len)
822{
823 struct ipack *ipack = &av7110->ipack[type];
824
Oliver Endrissef226d02009-04-19 14:47:01 -0300825 if (buf[1] & TRANS_ERROR) {
826 av7110_ipack_reset(ipack);
827 return -1;
828 }
829
Oliver Endrissfd46d162009-04-10 10:20:00 -0300830 if (!(buf[3] & PAYLOAD))
831 return -1;
832
833 if (buf[1] & PAY_START)
834 av7110_ipack_flush(ipack);
835
836 if (buf[3] & ADAPT_FIELD) {
837 len -= buf[4] + 1;
838 buf += buf[4] + 1;
839 if (!len)
840 return 0;
841 }
842
843 av7110_ipack_instant_repack(buf + 4, len - 4, ipack);
844 return 0;
845}
846
847
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848int av7110_write_to_decoder(struct dvb_demux_feed *feed, const u8 *buf, size_t len)
849{
850 struct dvb_demux *demux = feed->demux;
851 struct av7110 *av7110 = (struct av7110 *) demux->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853 dprintk(2, "av7110:%p, \n", av7110);
854
Oliver Endriss9e615ea2008-09-03 19:15:27 -0300855 if (av7110->full_ts && demux->dmx.frontend->source != DMX_MEMORY_FE)
856 return 0;
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 switch (feed->pes_type) {
859 case 0:
860 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
861 return -EINVAL;
862 break;
863 case 1:
864 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
865 return -EINVAL;
866 break;
867 default:
868 return -1;
869 }
870
Oliver Endrissfd46d162009-04-10 10:20:00 -0300871 return write_ts_to_decoder(av7110, feed->pes_type, buf, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874
875
876/******************************************************************************
877 * Video MPEG decoder events
878 ******************************************************************************/
879void dvb_video_add_event(struct av7110 *av7110, struct video_event *event)
880{
881 struct dvb_video_events *events = &av7110->video_events;
882 int wp;
883
884 spin_lock_bh(&events->lock);
885
886 wp = (events->eventw + 1) % MAX_VIDEO_EVENT;
887 if (wp == events->eventr) {
888 events->overflow = 1;
889 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
890 }
891
892 //FIXME: timestamp?
893 memcpy(&events->events[events->eventw], event, sizeof(struct video_event));
894 events->eventw = wp;
895
896 spin_unlock_bh(&events->lock);
897
898 wake_up_interruptible(&events->wait_queue);
899}
900
901
902static int dvb_video_get_event (struct av7110 *av7110, struct video_event *event, int flags)
903{
904 struct dvb_video_events *events = &av7110->video_events;
905
906 if (events->overflow) {
907 events->overflow = 0;
908 return -EOVERFLOW;
909 }
910 if (events->eventw == events->eventr) {
911 int ret;
912
913 if (flags & O_NONBLOCK)
914 return -EWOULDBLOCK;
915
916 ret = wait_event_interruptible(events->wait_queue,
917 events->eventw != events->eventr);
918 if (ret < 0)
919 return ret;
920 }
921
922 spin_lock_bh(&events->lock);
923
924 memcpy(event, &events->events[events->eventr],
925 sizeof(struct video_event));
926 events->eventr = (events->eventr + 1) % MAX_VIDEO_EVENT;
927
928 spin_unlock_bh(&events->lock);
929
930 return 0;
931}
932
933
934/******************************************************************************
935 * DVB device file operations
936 ******************************************************************************/
937
938static unsigned int dvb_video_poll(struct file *file, poll_table *wait)
939{
Tobias Klauserd9bdf772006-12-26 07:33:48 -0300940 struct dvb_device *dvbdev = file->private_data;
941 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 unsigned int mask = 0;
943
944 dprintk(2, "av7110:%p, \n", av7110);
945
946 if ((file->f_flags & O_ACCMODE) != O_RDONLY)
947 poll_wait(file, &av7110->avout.queue, wait);
948
949 poll_wait(file, &av7110->video_events.wait_queue, wait);
950
951 if (av7110->video_events.eventw != av7110->video_events.eventr)
952 mask = POLLPRI;
953
954 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
955 if (av7110->playing) {
956 if (FREE_COND)
957 mask |= (POLLOUT | POLLWRNORM);
958 } else /* if not playing: may play if asked for */
959 mask |= (POLLOUT | POLLWRNORM);
960 }
961
962 return mask;
963}
964
965static ssize_t dvb_video_write(struct file *file, const char __user *buf,
966 size_t count, loff_t *ppos)
967{
Tobias Klauserd9bdf772006-12-26 07:33:48 -0300968 struct dvb_device *dvbdev = file->private_data;
969 struct av7110 *av7110 = dvbdev->priv;
Oliver Endrissfd46d162009-04-10 10:20:00 -0300970 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 dprintk(2, "av7110:%p, \n", av7110);
973
974 if ((file->f_flags & O_ACCMODE) == O_RDONLY)
975 return -EPERM;
976
977 if (av7110->videostate.stream_source != VIDEO_SOURCE_MEMORY)
978 return -EPERM;
979
Oliver Endrissfd46d162009-04-10 10:20:00 -0300980 if (get_user(c, buf))
981 return -EFAULT;
982 if (c == 0x47 && count % TS_SIZE == 0)
983 return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
984 else
985 return dvb_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986}
987
988static unsigned int dvb_audio_poll(struct file *file, poll_table *wait)
989{
Tobias Klauserd9bdf772006-12-26 07:33:48 -0300990 struct dvb_device *dvbdev = file->private_data;
991 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 unsigned int mask = 0;
993
994 dprintk(2, "av7110:%p, \n", av7110);
995
996 poll_wait(file, &av7110->aout.queue, wait);
997
998 if (av7110->playing) {
999 if (dvb_ringbuffer_free(&av7110->aout) >= 20 * 1024)
1000 mask |= (POLLOUT | POLLWRNORM);
1001 } else /* if not playing: may play if asked for */
1002 mask = (POLLOUT | POLLWRNORM);
1003
1004 return mask;
1005}
1006
1007static ssize_t dvb_audio_write(struct file *file, const char __user *buf,
1008 size_t count, loff_t *ppos)
1009{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001010 struct dvb_device *dvbdev = file->private_data;
1011 struct av7110 *av7110 = dvbdev->priv;
Oliver Endrissfd46d162009-04-10 10:20:00 -03001012 unsigned char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 dprintk(2, "av7110:%p, \n", av7110);
1015
1016 if (av7110->audiostate.stream_source != AUDIO_SOURCE_MEMORY) {
1017 printk(KERN_ERR "not audio source memory\n");
1018 return -EPERM;
1019 }
Oliver Endrissfd46d162009-04-10 10:20:00 -03001020
1021 if (get_user(c, buf))
1022 return -EFAULT;
1023 if (c == 0x47 && count % TS_SIZE == 0)
1024 return ts_play(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
1025 else
1026 return dvb_aplay(av7110, buf, count, file->f_flags & O_NONBLOCK, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static u8 iframe_header[] = { 0x00, 0x00, 0x01, 0xe0, 0x00, 0x00, 0x80, 0x00, 0x00 };
1030
1031#define MIN_IFRAME 400000
1032
Oliver Endriss804b4452007-07-12 20:37:50 -03001033static int play_iframe(struct av7110 *av7110, char __user *buf, unsigned int len, int nonblock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Al Viroa230e552008-05-21 00:30:41 -03001035 unsigned i, n;
Oliver Endriss0ed4a6e2008-01-30 01:07:20 -03001036 int progressive = 0;
Al Viroa230e552008-05-21 00:30:41 -03001037 int match = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 dprintk(2, "av7110:%p, \n", av7110);
1040
1041 if (!(av7110->playing & RP_VIDEO)) {
1042 if (av7110_av_start_play(av7110, RP_VIDEO) < 0)
1043 return -EBUSY;
1044 }
1045
Al Viroa230e552008-05-21 00:30:41 -03001046 /* search in buf for instances of 00 00 01 b5 1? */
1047 for (i = 0; i < len; i++) {
1048 unsigned char c;
1049 if (get_user(c, buf + i))
1050 return -EFAULT;
1051 if (match == 5) {
1052 progressive = c & 0x08;
1053 match = 0;
1054 }
1055 if (c == 0x00) {
1056 match = (match == 1 || match == 2) ? 2 : 1;
1057 continue;
1058 }
1059 switch (match++) {
1060 case 2: if (c == 0x01)
1061 continue;
1062 break;
1063 case 3: if (c == 0xb5)
1064 continue;
1065 break;
1066 case 4: if ((c & 0xf0) == 0x10)
1067 continue;
1068 break;
1069 }
1070 match = 0;
Oliver Endriss0ed4a6e2008-01-30 01:07:20 -03001071 }
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 /* setting n always > 1, fixes problems when playing stillframes
1074 consisting of I- and P-Frames */
1075 n = MIN_IFRAME / len + 1;
1076
1077 /* FIXME: nonblock? */
1078 dvb_play_kernel(av7110, iframe_header, sizeof(iframe_header), 0, 1);
1079
1080 for (i = 0; i < n; i++)
1081 dvb_play(av7110, buf, len, 0, 1);
1082
1083 av7110_ipack_flush(&av7110->ipack[1]);
Oliver Endriss0ed4a6e2008-01-30 01:07:20 -03001084
1085 if (progressive)
1086 return vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
1087 else
1088 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089}
1090
1091
1092static int dvb_video_ioctl(struct inode *inode, struct file *file,
1093 unsigned int cmd, void *parg)
1094{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001095 struct dvb_device *dvbdev = file->private_data;
1096 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 unsigned long arg = (unsigned long) parg;
1098 int ret = 0;
1099
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001100 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 if ((file->f_flags & O_ACCMODE) == O_RDONLY) {
1103 if ( cmd != VIDEO_GET_STATUS && cmd != VIDEO_GET_EVENT &&
1104 cmd != VIDEO_GET_SIZE ) {
1105 return -EPERM;
1106 }
1107 }
1108
1109 switch (cmd) {
1110 case VIDEO_STOP:
1111 av7110->videostate.play_state = VIDEO_STOPPED;
1112 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001113 ret = av7110_av_stop(av7110, RP_VIDEO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 else
Hans Verkuil2435be12007-04-27 12:31:09 -03001115 ret = vidcom(av7110, AV_VIDEO_CMD_STOP,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 av7110->videostate.video_blank ? 0 : 1);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001117 if (!ret)
1118 av7110->trickmode = TRICK_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 break;
1120
1121 case VIDEO_PLAY:
1122 av7110->trickmode = TRICK_NONE;
1123 if (av7110->videostate.play_state == VIDEO_FREEZED) {
1124 av7110->videostate.play_state = VIDEO_PLAYING;
Hans Verkuil2435be12007-04-27 12:31:09 -03001125 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001126 if (ret)
1127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (av7110->videostate.stream_source == VIDEO_SOURCE_MEMORY) {
1130 if (av7110->playing == RP_AV) {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001131 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Stop, 0);
1132 if (ret)
1133 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 av7110->playing &= ~RP_VIDEO;
1135 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001136 ret = av7110_av_start_play(av7110, RP_VIDEO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001138 if (!ret)
Hans Verkuil2435be12007-04-27 12:31:09 -03001139 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001140 if (!ret)
1141 av7110->videostate.play_state = VIDEO_PLAYING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 break;
1143
1144 case VIDEO_FREEZE:
1145 av7110->videostate.play_state = VIDEO_FREEZED;
1146 if (av7110->playing & RP_VIDEO)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001147 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Pause, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 else
Hans Verkuil2435be12007-04-27 12:31:09 -03001149 ret = vidcom(av7110, AV_VIDEO_CMD_FREEZE, 1);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001150 if (!ret)
1151 av7110->trickmode = TRICK_FREEZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 break;
1153
1154 case VIDEO_CONTINUE:
1155 if (av7110->playing & RP_VIDEO)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001156 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Continue, 0);
1157 if (!ret)
Hans Verkuil2435be12007-04-27 12:31:09 -03001158 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001159 if (!ret) {
1160 av7110->videostate.play_state = VIDEO_PLAYING;
1161 av7110->trickmode = TRICK_NONE;
1162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
1164
1165 case VIDEO_SELECT_SOURCE:
1166 av7110->videostate.stream_source = (video_stream_source_t) arg;
1167 break;
1168
1169 case VIDEO_SET_BLANK:
1170 av7110->videostate.video_blank = (int) arg;
1171 break;
1172
1173 case VIDEO_GET_STATUS:
1174 memcpy(parg, &av7110->videostate, sizeof(struct video_status));
1175 break;
1176
1177 case VIDEO_GET_EVENT:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001178 ret = dvb_video_get_event(av7110, parg, file->f_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 break;
1180
1181 case VIDEO_GET_SIZE:
1182 memcpy(parg, &av7110->video_size, sizeof(video_size_t));
1183 break;
1184
1185 case VIDEO_SET_DISPLAY_FORMAT:
1186 {
1187 video_displayformat_t format = (video_displayformat_t) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 switch (format) {
1189 case VIDEO_PAN_SCAN:
Oliver Endrissdefd574e2007-07-12 23:08:07 -03001190 av7110->display_panscan = VID_PAN_SCAN_PREF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 case VIDEO_LETTER_BOX:
Oliver Endrissdefd574e2007-07-12 23:08:07 -03001193 av7110->display_panscan = VID_VC_AND_PS_PREF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 case VIDEO_CENTER_CUT_OUT:
Oliver Endrissdefd574e2007-07-12 23:08:07 -03001196 av7110->display_panscan = VID_CENTRE_CUT_PREF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 default:
1199 ret = -EINVAL;
1200 }
1201 if (ret < 0)
1202 break;
Johannes Stezenbacha65d3bb2005-05-16 21:54:26 -07001203 av7110->videostate.display_format = format;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetPanScanType,
Oliver Endrissdefd574e2007-07-12 23:08:07 -03001205 1, av7110->display_panscan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
1207 }
1208
1209 case VIDEO_SET_FORMAT:
1210 if (arg > 1) {
1211 ret = -EINVAL;
1212 break;
1213 }
1214 av7110->display_ar = arg;
1215 ret = av7110_fw_cmd(av7110, COMTYPE_ENCODER, SetMonitorType,
1216 1, (u16) arg);
1217 break;
1218
1219 case VIDEO_STILLPICTURE:
1220 {
1221 struct video_still_picture *pic =
1222 (struct video_still_picture *) parg;
1223 av7110->videostate.stream_source = VIDEO_SOURCE_MEMORY;
1224 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1225 ret = play_iframe(av7110, pic->iFrame, pic->size,
1226 file->f_flags & O_NONBLOCK);
1227 break;
1228 }
1229
1230 case VIDEO_FAST_FORWARD:
1231 //note: arg is ignored by firmware
1232 if (av7110->playing & RP_VIDEO)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001233 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
Johannes Stezenbach7a2fa902005-07-07 17:58:01 -07001234 __Scan_I, 2, AV_PES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 else
Hans Verkuil2435be12007-04-27 12:31:09 -03001236 ret = vidcom(av7110, AV_VIDEO_CMD_FFWD, arg);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001237 if (!ret) {
1238 av7110->trickmode = TRICK_FAST;
1239 av7110->videostate.play_state = VIDEO_PLAYING;
1240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 break;
1242
1243 case VIDEO_SLOWMOTION:
1244 if (av7110->playing&RP_VIDEO) {
Oliver Endriss452dac42009-04-23 23:24:33 -03001245 if (av7110->trickmode != TRICK_SLOW)
1246 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY, __Slow, 2, 0, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001247 if (!ret)
Hans Verkuil2435be12007-04-27 12:31:09 -03001248 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 } else {
Hans Verkuil2435be12007-04-27 12:31:09 -03001250 ret = vidcom(av7110, AV_VIDEO_CMD_PLAY, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001251 if (!ret)
Hans Verkuil2435be12007-04-27 12:31:09 -03001252 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001253 if (!ret)
Hans Verkuil2435be12007-04-27 12:31:09 -03001254 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001256 if (!ret) {
1257 av7110->trickmode = TRICK_SLOW;
1258 av7110->videostate.play_state = VIDEO_PLAYING;
1259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 break;
1261
1262 case VIDEO_GET_CAPABILITIES:
1263 *(int *)parg = VIDEO_CAP_MPEG1 | VIDEO_CAP_MPEG2 |
1264 VIDEO_CAP_SYS | VIDEO_CAP_PROG;
1265 break;
1266
1267 case VIDEO_CLEAR_BUFFER:
1268 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1269 av7110_ipack_reset(&av7110->ipack[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 if (av7110->playing == RP_AV) {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001271 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
Johannes Stezenbach7a2fa902005-07-07 17:58:01 -07001272 __Play, 2, AV_PES, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001273 if (ret)
1274 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 if (av7110->trickmode == TRICK_FAST)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001276 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
Johannes Stezenbach7a2fa902005-07-07 17:58:01 -07001277 __Scan_I, 2, AV_PES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 if (av7110->trickmode == TRICK_SLOW) {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001279 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
Johannes Stezenbach7a2fa902005-07-07 17:58:01 -07001280 __Slow, 2, 0, 0);
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001281 if (!ret)
Hans Verkuil2435be12007-04-27 12:31:09 -03001282 ret = vidcom(av7110, AV_VIDEO_CMD_SLOW, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284 if (av7110->trickmode == TRICK_FREEZE)
Hans Verkuil2435be12007-04-27 12:31:09 -03001285 ret = vidcom(av7110, AV_VIDEO_CMD_STOP, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 }
1287 break;
1288
1289 case VIDEO_SET_STREAMTYPE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 break;
1291
1292 default:
1293 ret = -ENOIOCTLCMD;
1294 break;
1295 }
Oliver Endrissea3b73d2009-04-29 23:25:07 -03001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 return ret;
1298}
1299
1300static int dvb_audio_ioctl(struct inode *inode, struct file *file,
1301 unsigned int cmd, void *parg)
1302{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001303 struct dvb_device *dvbdev = file->private_data;
1304 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 unsigned long arg = (unsigned long) parg;
1306 int ret = 0;
1307
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001308 dprintk(1, "av7110:%p, cmd=%04x\n", av7110,cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 if (((file->f_flags & O_ACCMODE) == O_RDONLY) &&
1311 (cmd != AUDIO_GET_STATUS))
1312 return -EPERM;
1313
1314 switch (cmd) {
1315 case AUDIO_STOP:
1316 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001317 ret = av7110_av_stop(av7110, RP_AUDIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 else
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001319 ret = audcom(av7110, AUDIO_CMD_MUTE);
1320 if (!ret)
1321 av7110->audiostate.play_state = AUDIO_STOPPED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 break;
1323
1324 case AUDIO_PLAY:
1325 if (av7110->audiostate.stream_source == AUDIO_SOURCE_MEMORY)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001326 ret = av7110_av_start_play(av7110, RP_AUDIO);
1327 if (!ret)
1328 ret = audcom(av7110, AUDIO_CMD_UNMUTE);
1329 if (!ret)
1330 av7110->audiostate.play_state = AUDIO_PLAYING;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 break;
1332
1333 case AUDIO_PAUSE:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001334 ret = audcom(av7110, AUDIO_CMD_MUTE);
1335 if (!ret)
1336 av7110->audiostate.play_state = AUDIO_PAUSED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 break;
1338
1339 case AUDIO_CONTINUE:
1340 if (av7110->audiostate.play_state == AUDIO_PAUSED) {
1341 av7110->audiostate.play_state = AUDIO_PLAYING;
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001342 ret = audcom(av7110, AUDIO_CMD_UNMUTE | AUDIO_CMD_PCM16);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
1344 break;
1345
1346 case AUDIO_SELECT_SOURCE:
1347 av7110->audiostate.stream_source = (audio_stream_source_t) arg;
1348 break;
1349
1350 case AUDIO_SET_MUTE:
1351 {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001352 ret = audcom(av7110, arg ? AUDIO_CMD_MUTE : AUDIO_CMD_UNMUTE);
1353 if (!ret)
1354 av7110->audiostate.mute_state = (int) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 break;
1356 }
1357
1358 case AUDIO_SET_AV_SYNC:
1359 av7110->audiostate.AV_sync_state = (int) arg;
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001360 ret = audcom(av7110, arg ? AUDIO_CMD_SYNC_ON : AUDIO_CMD_SYNC_OFF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 break;
1362
1363 case AUDIO_SET_BYPASS_MODE:
Dr. Werner Fink47f36922006-01-09 15:25:07 -02001364 if (FW_VERSION(av7110->arm_app) < 0x2621)
1365 ret = -EINVAL;
1366 av7110->audiostate.bypass_mode = (int)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 break;
1368
1369 case AUDIO_CHANNEL_SELECT:
1370 av7110->audiostate.channel_select = (audio_channel_select_t) arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 switch(av7110->audiostate.channel_select) {
1372 case AUDIO_STEREO:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001373 ret = audcom(av7110, AUDIO_CMD_STEREO);
Tim Kaiser61391e02006-06-25 09:14:07 -03001374 if (!ret) {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001375 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1376 i2c_writereg(av7110, 0x20, 0x02, 0x49);
Tim Kaiser61391e02006-06-25 09:14:07 -03001377 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1378 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0220);
1379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 case AUDIO_MONO_LEFT:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001382 ret = audcom(av7110, AUDIO_CMD_MONO_L);
Tim Kaiser61391e02006-06-25 09:14:07 -03001383 if (!ret) {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001384 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1385 i2c_writereg(av7110, 0x20, 0x02, 0x4a);
Tim Kaiser61391e02006-06-25 09:14:07 -03001386 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1387 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0200);
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 case AUDIO_MONO_RIGHT:
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001391 ret = audcom(av7110, AUDIO_CMD_MONO_R);
Tim Kaiser61391e02006-06-25 09:14:07 -03001392 if (!ret) {
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001393 if (av7110->adac_type == DVB_ADAC_CRYSTAL)
1394 i2c_writereg(av7110, 0x20, 0x02, 0x45);
Tim Kaiser61391e02006-06-25 09:14:07 -03001395 else if (av7110->adac_type == DVB_ADAC_MSP34x5)
1396 msp_writereg(av7110, MSP_WR_DSP, 0x0008, 0x0210);
1397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 default:
1400 ret = -EINVAL;
1401 break;
1402 }
1403 break;
1404
1405 case AUDIO_GET_STATUS:
1406 memcpy(parg, &av7110->audiostate, sizeof(struct audio_status));
1407 break;
1408
1409 case AUDIO_GET_CAPABILITIES:
Dr. Werner Fink47f36922006-01-09 15:25:07 -02001410 if (FW_VERSION(av7110->arm_app) < 0x2621)
1411 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
1412 else
1413 *(unsigned int *)parg = AUDIO_CAP_LPCM | AUDIO_CAP_DTS | AUDIO_CAP_AC3 |
1414 AUDIO_CAP_MP1 | AUDIO_CAP_MP2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 break;
1416
1417 case AUDIO_CLEAR_BUFFER:
1418 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1419 av7110_ipack_reset(&av7110->ipack[0]);
1420 if (av7110->playing == RP_AV)
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001421 ret = av7110_fw_cmd(av7110, COMTYPE_REC_PLAY,
Johannes Stezenbach7a2fa902005-07-07 17:58:01 -07001422 __Play, 2, AV_PES, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
Oliver Endrissea3b73d2009-04-29 23:25:07 -03001425 case AUDIO_SET_ID:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 break;
Oliver Endrissea3b73d2009-04-29 23:25:07 -03001427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 case AUDIO_SET_MIXER:
1429 {
1430 struct audio_mixer *amix = (struct audio_mixer *)parg;
Wolfgang Rohdewaldce18a222005-07-07 17:57:59 -07001431 ret = av7110_set_volume(av7110, amix->volume_left, amix->volume_right);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 break;
1433 }
Oliver Endrissea3b73d2009-04-29 23:25:07 -03001434
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 case AUDIO_SET_STREAMTYPE:
1436 break;
Oliver Endrissea3b73d2009-04-29 23:25:07 -03001437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 default:
1439 ret = -ENOIOCTLCMD;
1440 }
Oliver Endrissea3b73d2009-04-29 23:25:07 -03001441
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 return ret;
1443}
1444
1445
1446static int dvb_video_open(struct inode *inode, struct file *file)
1447{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001448 struct dvb_device *dvbdev = file->private_data;
1449 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 int err;
1451
1452 dprintk(2, "av7110:%p, \n", av7110);
1453
1454 if ((err = dvb_generic_open(inode, file)) < 0)
1455 return err;
1456
1457 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1458 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1459 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->avout);
1460 av7110->video_blank = 1;
1461 av7110->audiostate.AV_sync_state = 1;
1462 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1463
1464 /* empty event queue */
1465 av7110->video_events.eventr = av7110->video_events.eventw = 0;
1466 }
1467
1468 return 0;
1469}
1470
1471static int dvb_video_release(struct inode *inode, struct file *file)
1472{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001473 struct dvb_device *dvbdev = file->private_data;
1474 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475
1476 dprintk(2, "av7110:%p, \n", av7110);
1477
1478 if ((file->f_flags & O_ACCMODE) != O_RDONLY) {
1479 av7110_av_stop(av7110, RP_VIDEO);
1480 }
1481
1482 return dvb_generic_release(inode, file);
1483}
1484
1485static int dvb_audio_open(struct inode *inode, struct file *file)
1486{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001487 struct dvb_device *dvbdev = file->private_data;
1488 struct av7110 *av7110 = dvbdev->priv;
1489 int err = dvb_generic_open(inode, file);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
1491 dprintk(2, "av7110:%p, \n", av7110);
1492
1493 if (err < 0)
1494 return err;
1495 dvb_ringbuffer_flush_spinlock_wakeup(&av7110->aout);
1496 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1497 return 0;
1498}
1499
1500static int dvb_audio_release(struct inode *inode, struct file *file)
1501{
Tobias Klauserd9bdf772006-12-26 07:33:48 -03001502 struct dvb_device *dvbdev = file->private_data;
1503 struct av7110 *av7110 = dvbdev->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 dprintk(2, "av7110:%p, \n", av7110);
1506
1507 av7110_av_stop(av7110, RP_AUDIO);
1508 return dvb_generic_release(inode, file);
1509}
1510
1511
1512
1513/******************************************************************************
1514 * driver registration
1515 ******************************************************************************/
1516
Jan Engelhardt784e29d2009-01-11 06:12:43 -03001517static const struct file_operations dvb_video_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 .owner = THIS_MODULE,
1519 .write = dvb_video_write,
1520 .ioctl = dvb_generic_ioctl,
1521 .open = dvb_video_open,
1522 .release = dvb_video_release,
1523 .poll = dvb_video_poll,
1524};
1525
1526static struct dvb_device dvbdev_video = {
1527 .priv = NULL,
1528 .users = 6,
1529 .readers = 5, /* arbitrary */
1530 .writers = 1,
1531 .fops = &dvb_video_fops,
1532 .kernel_ioctl = dvb_video_ioctl,
1533};
1534
Jan Engelhardt784e29d2009-01-11 06:12:43 -03001535static const struct file_operations dvb_audio_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 .owner = THIS_MODULE,
1537 .write = dvb_audio_write,
1538 .ioctl = dvb_generic_ioctl,
1539 .open = dvb_audio_open,
1540 .release = dvb_audio_release,
1541 .poll = dvb_audio_poll,
1542};
1543
1544static struct dvb_device dvbdev_audio = {
1545 .priv = NULL,
1546 .users = 1,
1547 .writers = 1,
1548 .fops = &dvb_audio_fops,
1549 .kernel_ioctl = dvb_audio_ioctl,
1550};
1551
1552
1553int av7110_av_register(struct av7110 *av7110)
1554{
1555 av7110->audiostate.AV_sync_state = 0;
1556 av7110->audiostate.mute_state = 0;
1557 av7110->audiostate.play_state = AUDIO_STOPPED;
1558 av7110->audiostate.stream_source = AUDIO_SOURCE_DEMUX;
1559 av7110->audiostate.channel_select = AUDIO_STEREO;
1560 av7110->audiostate.bypass_mode = 0;
1561
1562 av7110->videostate.video_blank = 0;
1563 av7110->videostate.play_state = VIDEO_STOPPED;
1564 av7110->videostate.stream_source = VIDEO_SOURCE_DEMUX;
1565 av7110->videostate.video_format = VIDEO_FORMAT_4_3;
Oliver Endrissdefd574e2007-07-12 23:08:07 -03001566 av7110->videostate.display_format = VIDEO_LETTER_BOX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 av7110->display_ar = VIDEO_FORMAT_4_3;
Oliver Endrissdefd574e2007-07-12 23:08:07 -03001568 av7110->display_panscan = VID_VC_AND_PS_PREF;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
1570 init_waitqueue_head(&av7110->video_events.wait_queue);
1571 spin_lock_init(&av7110->video_events.lock);
1572 av7110->video_events.eventw = av7110->video_events.eventr = 0;
1573 av7110->video_events.overflow = 0;
1574 memset(&av7110->video_size, 0, sizeof (video_size_t));
1575
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001576 dvb_register_device(&av7110->dvb_adapter, &av7110->video_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 &dvbdev_video, av7110, DVB_DEVICE_VIDEO);
1578
Johannes Stezenbachfdc53a62005-05-16 21:54:39 -07001579 dvb_register_device(&av7110->dvb_adapter, &av7110->audio_dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 &dvbdev_audio, av7110, DVB_DEVICE_AUDIO);
1581
1582 return 0;
1583}
1584
1585void av7110_av_unregister(struct av7110 *av7110)
1586{
1587 dvb_unregister_device(av7110->audio_dev);
1588 dvb_unregister_device(av7110->video_dev);
1589}
1590
1591int av7110_av_init(struct av7110 *av7110)
1592{
1593 void (*play[])(u8 *, int, void *) = { play_audio_cb, play_video_cb };
1594 int i, ret;
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 for (i = 0; i < 2; i++) {
1597 struct ipack *ipack = av7110->ipack + i;
1598
1599 ret = av7110_ipack_init(ipack, IPACKS, play[i]);
1600 if (ret < 0) {
1601 if (i)
1602 av7110_ipack_free(--ipack);
1603 goto out;
1604 }
1605 ipack->data = av7110;
1606 }
1607
1608 dvb_ringbuffer_init(&av7110->avout, av7110->iobuf, AVOUTLEN);
1609 dvb_ringbuffer_init(&av7110->aout, av7110->iobuf + AVOUTLEN, AOUTLEN);
1610
1611 av7110->kbuf[0] = (u8 *)(av7110->iobuf + AVOUTLEN + AOUTLEN + BMPLEN);
1612 av7110->kbuf[1] = av7110->kbuf[0] + 2 * IPACKS;
1613out:
1614 return ret;
1615}
1616
1617void av7110_av_exit(struct av7110 *av7110)
1618{
1619 av7110_ipack_free(&av7110->ipack[0]);
1620 av7110_ipack_free(&av7110->ipack[1]);
1621}