Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
 * device driver for philips saa7134 based TV cards
 * video4linux video interface
 *
 * (c) 2001,02 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/list.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/slab.h>

#include "saa7134-reg.h"
#include "saa7134.h"

/* ------------------------------------------------------------------ */

static unsigned int ts_debug  = 0;
MODULE_PARM(ts_debug,"i");
MODULE_PARM_DESC(ts_debug,"enable debug messages [ts]");

static unsigned int tsbufs = 4;
MODULE_PARM(tsbufs,"i");
MODULE_PARM_DESC(tsbufs,"number of ts buffers, range 2-32");

#define TS_PACKET_SIZE 188 /* TS packets 188 bytes */
#define TS_NR_PACKETS 312

#define dprintk(fmt, arg...)	if (ts_debug) \
	printk(KERN_DEBUG "%s/ts: " fmt, dev->name, ## arg)

/* ------------------------------------------------------------------ */

static int buffer_activate(struct saa7134_dev *dev,
			   struct saa7134_buf *buf,
			   struct saa7134_buf *next)
{
	u32 control;
	
	dprintk("buffer_activate [%p]\n",buf);
	buf->vb.state = STATE_ACTIVE;
	
        /* dma: setup channel 5 (= TS) */
        control = SAA7134_RS_CONTROL_BURST_16 |
                SAA7134_RS_CONTROL_ME |
                (buf->pt->dma >> 12);

	if (NULL == next)
		next = buf;
	if (V4L2_FIELD_TOP == buf->vb.field) {
		dprintk("[top]     buf=%p next=%p",buf,next);
		saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(buf));
		saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(next));
	} else {
		dprintk("[bottom]  buf=%p next=%p",buf,next);
		saa_writel(SAA7134_RS_BA1(5),saa7134_buffer_base(next));
		saa_writel(SAA7134_RS_BA2(5),saa7134_buffer_base(buf));
	}
	saa_writel(SAA7134_RS_PITCH(5),TS_PACKET_SIZE);
	saa_writel(SAA7134_RS_CONTROL(5),control);

	/* start DMA */
	saa7134_set_dmabits(dev);
	
	mod_timer(&dev->ts_q.timeout, jiffies+BUFFER_TIMEOUT);
	return 0;
}

static int buffer_prepare(struct file *file, struct videobuf_buffer *vb,
			  enum v4l2_field field)
{
	struct saa7134_dev *dev = file->private_data;
	struct saa7134_buf *buf = (struct saa7134_buf *)vb;
	int lines, llength, size, err;
	
	llength = TS_PACKET_SIZE;
	lines = TS_NR_PACKETS;
	
	size = lines * llength;
	if (0 != buf->vb.baddr  &&  buf->vb.bsize < size)
		return -EINVAL;

	if (buf->vb.size != size) {
		saa7134_dma_free(dev,buf);
	}

	if (STATE_NEEDS_INIT == buf->vb.state) {
		buf->vb.width  = llength;
		buf->vb.height = lines;
		buf->vb.size   = size;
		buf->pt        = &dev->ts.pt_ts;

		err = videobuf_iolock(dev->pci,&buf->vb);
		if (err)
			goto oops;
		err = saa7134_pgtable_build(dev->pci,buf->pt,
					    buf->vb.dma.sglist,
					    buf->vb.dma.sglen,
					    saa7134_buffer_startpage(buf));
		if (err)
			goto oops;
	}
	buf->vb.state = STATE_PREPARED;
	buf->top_seen = 0;
	buf->activate = buffer_activate;
	buf->vb.field = field;
	return 0;

 oops:
	saa7134_dma_free(dev,buf);
	return err;
}

static int
buffer_setup(struct file *file, int *count, int *size)
{
	*size = TS_PACKET_SIZE * TS_NR_PACKETS;
	if (0 == *count)
		*count = tsbufs;
	*count = saa7134_buffer_count(*size,*count);
	return 0;
}

static void buffer_queue(struct file *file, struct videobuf_buffer *vb)
{
	struct saa7134_dev *dev = file->private_data;
	struct saa7134_buf *buf = (struct saa7134_buf *)vb;
	
	saa7134_buffer_queue(dev,&dev->ts_q,buf);
}

static void buffer_release(struct file *file, struct videobuf_buffer *vb)
{
	struct saa7134_dev *dev = file->private_data;
	struct saa7134_buf *buf = (struct saa7134_buf *)vb;
	
	saa7134_dma_free(dev,buf);
}

static struct videobuf_queue_ops ts_qops = {
	.buf_setup    = buffer_setup,
	.buf_prepare  = buffer_prepare,
	.buf_queue    = buffer_queue,
	.buf_release  = buffer_release,
};

/* ------------------------------------------------------------------ */

static int ts_open(struct inode *inode, struct file *file)
{
	unsigned int minor = minor(inode->i_rdev);
	struct saa7134_dev *h,*dev = NULL;
	struct list_head *list;
	int err;
	
	list_for_each(list,&saa7134_devlist) {
		h = list_entry(list, struct saa7134_dev, devlist);
		if (h->ts_dev.minor == minor)
			dev = h;
	}
	if (NULL == dev)
		return -ENODEV;

	dprintk("open minor=%d\n",minor);
	down(&dev->ts.ts.lock);
	err = -EBUSY;
	if (dev->ts.users)
		goto done;

	dev->ts.users++;
	file->private_data = dev;
	err = 0;
 done:
	up(&dev->ts.ts.lock);
	return err;
}

static int ts_release(struct inode *inode, struct file *file)
{
	struct saa7134_dev *dev = file->private_data;

	if (dev->ts.ts.streaming)
		videobuf_streamoff(file,&dev->ts.ts);
	down(&dev->ts.ts.lock);
	if (dev->ts.ts.reading)
		videobuf_read_stop(file,&dev->ts.ts);
	dev->ts.users--;
	up(&dev->ts.ts.lock);
	return 0;
}

static ssize_t
ts_read(struct file *file, char *data, size_t count, loff_t *ppos)
{
	struct saa7134_dev *dev = file->private_data;

	return videobuf_read_stream(file, &dev->ts.ts, data, count, ppos, 0);
}

static unsigned int
ts_poll(struct file *file, struct poll_table_struct *wait)
{
	struct saa7134_dev *dev = file->private_data;

	return videobuf_poll_stream(file, &dev->ts.ts, wait);
}


static int
ts_mmap(struct file *file, struct vm_area_struct * vma)
{
	struct saa7134_dev *dev = file->private_data;

	return videobuf_mmap_mapper(vma, &dev->ts.ts);
}

/*
 * This function is _not_ called directly, but from
 * video_generic_ioctl (and maybe others).  userspace
 * copying is done already, arg is a kernel pointer.
 */
static int ts_do_ioctl(struct inode *inode, struct file *file,
		       unsigned int cmd, void *arg)
{
	struct saa7134_dev *dev = file->private_data;

	if (ts_debug > 1)
		saa7134_print_ioctl(dev->name,cmd);
	switch (cmd) {
	case VIDIOC_QUERYCAP:
	{
		struct v4l2_capability *cap = arg;

		memset(cap,0,sizeof(*cap));
                strcpy(cap->driver, "saa7134");
		strncpy(cap->card, saa7134_boards[dev->board].name,
			sizeof(cap->card));
		sprintf(cap->bus_info,"PCI:%s",dev->pci->slot_name);
		cap->version = SAA7134_VERSION_CODE;
		cap->capabilities =
			V4L2_CAP_VIDEO_CAPTURE |
			V4L2_CAP_READWRITE |
			V4L2_CAP_STREAMING;
		return 0;
	}

	/* --- input switching --------------------------------------- */
	case VIDIOC_ENUMINPUT:
	{
		struct v4l2_input *i = arg;
		
		if (i->index != 0)
			return -EINVAL;
		i->type = V4L2_INPUT_TYPE_CAMERA;
		strcpy(i->name,"CCIR656");
		return 0;
	}
	case VIDIOC_G_INPUT:
	{
		int *i = arg;
		*i = 0;
		return 0;
	}
	case VIDIOC_S_INPUT:
	{
		int *i = arg;
		
		if (*i != 0)
			return -EINVAL;
		return 0;
	}
	/* --- capture ioctls ---------------------------------------- */
	
	case VIDIOC_ENUM_FMT:
	{
		struct v4l2_fmtdesc *f = arg;
		int index;

		index = f->index;
		if (index != 0)
			return -EINVAL;
		
		memset(f,0,sizeof(*f));
		f->index = index;
		strncpy(f->description, "MPEG TS", 31);
		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		f->pixelformat = V4L2_PIX_FMT_MPEG;
		return 0;
	}

	case VIDIOC_G_FMT:
	{
		struct v4l2_format *f = arg;

		memset(f,0,sizeof(*f));
		f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
		
		/* FIXME: translate subsampling type EMPRESS into
		 *        width/height: */
		f->fmt.pix.width        = 720; /* D1 */
		f->fmt.pix.height       = 576;
		f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
		f->fmt.pix.sizeimage    = TS_PACKET_SIZE*TS_NR_PACKETS;
		return 0;
	}
	
	case VIDIOC_S_FMT:
	{
		struct v4l2_format *f = arg;

		if (f->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
		    return -EINVAL;

		/* 
		  FIXME: translate and round width/height into EMPRESS
		  subsample type:
		 
		          type  |   PAL   |  NTSC 
			---------------------------
			  SIF   | 352x288 | 352x240
			 1/2 D1 | 352x576 | 352x480
			 2/3 D1 | 480x576 | 480x480
			  D1    | 720x576 | 720x480
		*/

		f->fmt.pix.width        = 720; /* D1 */
		f->fmt.pix.height       = 576;
		f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
		f->fmt.pix.sizeimage    = TS_PACKET_SIZE*TS_NR_PACKETS;
	}

	case VIDIOC_REQBUFS:
		return videobuf_reqbufs(file,&dev->ts.ts,arg);

	case VIDIOC_QUERYBUF:
		return videobuf_querybuf(&dev->ts.ts,arg);

	case VIDIOC_QBUF:
		return videobuf_qbuf(file,&dev->ts.ts,arg);

	case VIDIOC_DQBUF:
		return videobuf_dqbuf(file,&dev->ts.ts,arg);

	case VIDIOC_STREAMON:
		return videobuf_streamon(file,&dev->ts.ts);

	case VIDIOC_STREAMOFF:
		return videobuf_streamoff(file,&dev->ts.ts);

	default:
		return -ENOIOCTLCMD;
	}
	return 0;
}

static int ts_ioctl(struct inode *inode, struct file *file,
		     unsigned int cmd, unsigned long arg)
{
	return video_usercopy(inode, file, cmd, arg, ts_do_ioctl);
}


static struct file_operations ts_fops =
{
	.owner	  = THIS_MODULE,
	.open	  = ts_open,
	.release  = ts_release,
	.read	  = ts_read,
	.poll	  = ts_poll,
	.mmap	  = ts_mmap,
	.ioctl	  = ts_ioctl,
	.llseek   = no_llseek,
};


/* ----------------------------------------------------------- */
/* exported stuff                                              */

struct video_device saa7134_ts_template =
{
	.name          = "saa7134-ts",
	.type          = 0 /* FIXME */,
	.type2         = 0 /* FIXME */,
	.hardware      = 0,
	.fops          = &ts_fops,
	.minor	       = -1,
};

int saa7134_ts_init(struct saa7134_dev *dev)
{
	/* sanitycheck insmod options */
	if (tsbufs < 2)
		tsbufs = 2;
	if (tsbufs > VIDEO_MAX_FRAME)
		tsbufs = VIDEO_MAX_FRAME;

	INIT_LIST_HEAD(&dev->ts_q.queue);
	init_timer(&dev->ts_q.timeout);
	dev->ts_q.timeout.function = saa7134_buffer_timeout;
	dev->ts_q.timeout.data     = (unsigned long)(&dev->ts_q);
	dev->ts_q.dev              = dev;
	videobuf_queue_init(&dev->ts.ts, &ts_qops, dev->pci, &dev->slock,
			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
			    V4L2_FIELD_ALTERNATE,
			    sizeof(struct saa7134_buf));
	saa7134_pgtable_alloc(dev->pci,&dev->ts.pt_ts);

	/* init TS hw */
	saa_writeb(SAA7134_TS_SERIAL1, 0x00);  /* deactivate TS softreset */
	saa_writeb(SAA7134_TS_PARALLEL, 0xec); /* TSSOP high active, TSVAL high active, TSLOCK ignored */
	saa_writeb(SAA7134_TS_PARALLEL_SERIAL, (TS_PACKET_SIZE-1));
	saa_writeb(SAA7134_TS_DMA0, ((TS_NR_PACKETS-1)&0xff));
	saa_writeb(SAA7134_TS_DMA1, (((TS_NR_PACKETS-1)>>8)&0xff));
	saa_writeb(SAA7134_TS_DMA2, ((((TS_NR_PACKETS-1)>>16)&0x3f) | 0x00)); /* TSNOPIT=0, TSCOLAP=0 */	 
 
	return 0;
}

int saa7134_ts_fini(struct saa7134_dev *dev)
{
	/* nothing */
	saa7134_pgtable_free(dev->pci,&dev->ts.pt_ts);
	return 0;
}


void saa7134_irq_ts_done(struct saa7134_dev *dev, unsigned long status)
{
	enum v4l2_field field;

	spin_lock(&dev->slock);
	if (dev->ts_q.curr) {
		field = dev->video_q.curr->vb.field;
		if (field == V4L2_FIELD_TOP) {
			if ((status & 0x100000) != 0x100000)
				goto done;
		} else {
			if ((status & 0x100000) != 0x000000)
				goto done;
		}
		saa7134_buffer_finish(dev,&dev->ts_q,STATE_DONE);
	}
	saa7134_buffer_next(dev,&dev->ts_q);

 done:
	spin_unlock(&dev->slock);
}

/* ----------------------------------------------------------- */
/*
 * Local variables:
 * c-basic-offset: 8
 * End:
 */