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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
/*
    video4linux-parts of the saa7146 device driver
    
    Copyright (C) 1998,1999 Michael Hunold <michael@mihu.de>

    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/module.h>	/* for module-version */
#include <linux/string.h>
#include <linux/slab.h>		/* for kmalloc/kfree */
#include <linux/delay.h>	/* for delay-stuff */
#include <asm/uaccess.h>	/* for copy_to/from_user */
#include <linux/wrapper.h>	/* for mem_map_reserve */
#include <linux/vmalloc.h>
#include <linux/videodev.h>

#include "saa7146_defs.h"
#include "saa7146_core.h"
#include "saa7146_v4l.h"


static int saa7146_v4l_debug = 0;

#define dprintk	if (saa7146_v4l_debug) printk
#define hprintk	if (saa7146_v4l_debug >= 2) printk
#define gprintk	if (saa7146_v4l_debug >= 3) printk

#define __COMPILE_SAA7146__
#include "saa7146.c"

/* transform video4linux-cliplist to plain arrays -- we assume that the arrays
   are big enough -- if not: your fault! */	
int	saa7146_v4lclip2plain(struct video_clip *clips, u16 clipcount, int x[], int y[], int width[], int height[])
{
	int	i = 0;
	struct	video_clip* vc = NULL;

	dprintk("saa7146_v4l.o: ==> saa7146_v4lclip2plain, cc:%d\n",clipcount);

	/* anything to do here? */	
	if( 0 == clipcount )
		return 0;

	/* copy to kernel-space */
	vc = vmalloc(sizeof(struct video_clip)*(clipcount));
	if( NULL == vc	) {
		printk("saa7146_v4l.o: ==> v4lclip2saa7146_v4l.o: no memory #2!\n");
		return -ENOMEM;
	}
	if(copy_from_user(vc,clips,sizeof(struct video_clip)*clipcount)) {
		printk("saa7146_v4l.o: ==> v4lclip2saa7146_v4l.o: could not copy from user-space!\n");
		return -EFAULT;
	}

	/* copy the clip-list to the arrays
	   note: the video_clip-struct may contain negative values to indicate that a window
		 doesn't lay completly over the video window. Thus, we correct the values right here */
	for(i = 0; i < clipcount; i++) {

		if( vc[i].width < 0) {
			vc[i].x += vc[i].width; vc[i].width = -vc[i].width;
		}
		if( vc[i].height < 0) {
			vc[i].y += vc[i].height; vc[i].height = -vc[i].height;
		}			

		if( vc[i].x < 0) {
			vc[i].width += vc[i].x; vc[i].x = 0;
		}
		if( vc[i].y < 0) {
			vc[i].height += vc[i].y; vc[i].y = 0;
		}		

		if(vc[i].width <= 0 || vc[i].height <= 0) {
			vfree(vc);
			return -EINVAL;
		}
		
		x[i]		= vc[i].x;
		y[i]		= vc[i].y;
		width[i]	= vc[i].width;
		height[i]	= vc[i].height;
	}		

	/* free memory used for temporary clips */
	vfree(vc);

	return 0;	
}
	
struct saa7146_v4l_struct {
	struct video_buffer	buffer;
	struct video_mbuf	mbuf;
	struct video_window	window;
	struct video_picture	picture;
}; 

static int saa7146_v4l_command(struct saa7146* saa, void *p, unsigned int cmd, void *arg)
{
	struct saa7146_v4l_struct* data = (struct saa7146_v4l_struct*)p;

	hprintk("saa7146_v4l.o: ==> saa7146_v4l_command\n");

	if( NULL == saa)
		return -EINVAL;

	switch(cmd) {
		case SAA7146_V4L_GPICT:
		{
			struct video_picture *p = arg;

			hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_GPICT\n");

			memcpy(p, &data->picture, sizeof(struct video_picture));

		}
		break;

		case SAA7146_V4L_SPICT:
		{
			struct video_picture *p = arg;
			
			hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_SPICT\n");

			memcpy(&data->picture, p, sizeof(struct video_picture));
			set_picture_prop(saa, (u32)(data->picture.brightness>>8),(u32)(data->picture.contrast>>9),(u32)(data->picture.colour>>9));

		}
		break;

		case SAA7146_V4L_SWIN:
		{
			struct video_window *vw = arg;
			int	*x = NULL, *y = NULL, *w = NULL, *h = NULL;

			u32 palette = 0;
			
			hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_SWIN\n");
			
			video_setmode(saa, 0);
			saa7146_write(saa->mem, MC1, (MASK_21));

			set_window(saa, vw->width, vw->height,0,0,0);
			//saa->port, saa->sync);
			if (move_to(saa, vw->x, vw->y, vw->height, data->buffer.width,
				    data->buffer.depth, data->buffer.bytesperline,
				    (u32)data->buffer.base, 0)<0)
			  return -1;

			switch( data->picture.palette ) {

			case VIDEO_PALETTE_RGB555:
				palette = RGB15_COMPOSED;
				break;

			case VIDEO_PALETTE_RGB24:
				palette = RGB24_COMPOSED;
				break;

			case VIDEO_PALETTE_RGB32:
				palette = RGB32_COMPOSED;
				break;

			case VIDEO_PALETTE_UYVY:
				palette = YUV422_COMPOSED;
				break;

			case VIDEO_PALETTE_YUV422P:
				palette = YUV422_DECOMPOSED;
				break;

			case VIDEO_PALETTE_YUV420P:
				palette = YUV420_DECOMPOSED;
				break;

			case VIDEO_PALETTE_YUV411P:
				palette = YUV411_DECOMPOSED;
				break;

			default:
			/*case VIDEO_PALETTE_RGB565:*/
				palette = RGB16_COMPOSED;
				break;
			}

			set_output_format(saa, palette);

			if (vw->flags==VIDEO_CLIP_BITMAP) {
			  clip_windows(saa, SAA7146_CLIPPING_MASK, vw->width, vw->height,
				       (u32 *) vw->clips, 1, 0, 0, 0, 0);
			} else {
			  
			  
			  /* this is tricky, but helps us saving kmalloc/kfree-calls 
			     and boring if/else-constructs ... */
			  x = (int*)kmalloc(sizeof(int)*vw->clipcount*4,GFP_KERNEL);
			  if( NULL == x ) {
			    hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_SWIN: out of kernel-memory.\n");
			    return -ENOMEM;
			  }
			  y = x+(1*vw->clipcount);
			  w = x+(2*vw->clipcount);
			  h = x+(3*vw->clipcount);
			  
			  /* transform clipping-windows */
			  if (0 != saa7146_v4lclip2plain(vw->clips, vw->clipcount,x,y,w,h))
			    break;
			  clip_windows(saa, SAA7146_CLIPPING_RECT, vw->width, vw->height,
				       NULL, vw->clipcount, x, y, w, h);
			  kfree(x);
			  
			  memcpy(&data->window, arg, sizeof(struct video_window));
			}
			video_setmode(saa, 1);
			break;
		}

		case SAA7146_V4L_CCAPTURE:
		{
			int* i = arg;

			hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_CCAPTURE\n");

			if ( 0 == *i ) {
				video_setmode(saa, 0);
			}
			else {
				video_setmode(saa, 1);
			}
						
			break;
		}

		case SAA7146_V4L_GFBUF:
		{
			struct video_buffer *b = arg;

			hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_GFBUF\n");

			memcpy(b, &data->buffer, sizeof(struct video_buffer));

			break;
		}

		case SAA7146_V4L_SFBUF:
		{
			struct video_buffer *b = arg;
		
			memcpy(&data->buffer, b, sizeof(struct video_buffer));
			hprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_SFBUF: b:0x%08x, h:%d, w:%d, d:%d\n", (u32)data->buffer.base, data->buffer.height, data->buffer.width, data->buffer.depth);			

			break;
		}


		case SAA7146_V4L_CSYNC:
		{
			int i = *((int*)arg);

			int count = 0, k = 0;
			unsigned char* grabbfr;
			unsigned char y, uv;

			/* sanity checks */
                        if ( i >= saa->buffers || i < 0) {
				gprintk("saa7146_v4l.o: SAA7146_V4L_CSYNC, invalid buffer %d\n",i);
				return -EINVAL;
			}

			/* get the state */
			switch ( saa->frame_stat[i] ) {
				case GBUFFER_UNUSED:
				{
					/* there was no grab to this buffer */
					gprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_CSYNC, invalid frame (fr:%d)\n",i);
					return -EINVAL;
				}
				case GBUFFER_GRABBING:
				{		
					/* wait to be woken up by the irq-handler */
					interruptible_sleep_on(&saa->rps0_wq);
					break;
				}
				case GBUFFER_DONE:
				{
					gprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_CSYNC, frame done! (fr:%d)\n",i);
					break;
				}
			}

			/* all saa7146´s below chip-revision 3 are not capable of
			   doing byte-swaps with video-dma1. for rgb-grabbing this
			   does not matter, but yuv422-grabbing has the wrong
			   byte-order, so we have to swap in software */
			if ( ( saa->revision<3) && 
			     (saa->grab_format[i] == YUV422_COMPOSED)) {
				/* swap UYVY to YUYV */
				count = saa->grab_height[i]*saa->grab_width[i]*2;
				grabbfr = ((unsigned char*)(saa->grabbing))+i*GRABBING_MEM_SIZE;
				for (k=0; k<count; k=k+2) {
					y = grabbfr[k+1];
					uv = grabbfr[k];
					grabbfr[k] = y;
					grabbfr[k+1] = uv;
				}
			}
						
			/* set corresponding buffer to ´unused´ */
			saa->frame_stat[i] = GBUFFER_UNUSED;
printk ("saa7146_v4l.o: SAA7146_V4L_CSYNC, frame %i done.\n", i);

			break;
		}
		case SAA7146_V4L_CMCAPTURE:
		{
                        struct video_mmap *vm = arg;

			gprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_CMCAPTURE, trying buffer:%d\n", vm->frame);

			/* check status for wanted frame */
			if ( GBUFFER_GRABBING == saa->frame_stat[vm->frame] ) {
				gprintk("saa7146_v4l.o: frame #%d still grabbing!\n",vm->frame);
				return -EBUSY;
			}

			/* do necessary transformations from the videodev-structure to our own format. */		

			/* sanity checks */
			if ( vm->width <= 0 || vm->height <= 0 ) {
				gprintk("saa7146_v4l.o: set_up_grabbing, invalid dimension for wanted buffer %d\n",vm->frame);
				return -EINVAL;
			}

			/* set corresponding buffer to ´grabbing´ */
			saa->frame_stat[vm->frame] = GBUFFER_GRABBING;

			/* copy grabbing informtaion for the buffer */
			saa->grab_height[vm->frame] = vm->height;
			saa->grab_width[vm->frame]  = vm->width;
			/* fixme: setting of grabbing port ?!*/
			saa->grab_port[vm->frame]   = 0;

			switch( vm->format ) {

				case VIDEO_PALETTE_RGB555:
					saa->grab_format[vm->frame] = RGB15_COMPOSED;
					break;

				case VIDEO_PALETTE_RGB24:
					saa->grab_format[vm->frame] = RGB24_COMPOSED;
					break;

				case VIDEO_PALETTE_RGB32:
					saa->grab_format[vm->frame] = RGB32_COMPOSED;
					break;

				case VIDEO_PALETTE_YUV420P:
					return -EINVAL;

				case VIDEO_PALETTE_YUV422:
					saa->grab_format[vm->frame] = YUV422_COMPOSED;
					break;

				case VIDEO_PALETTE_YUV422P:
					saa->grab_format[vm->frame] = YUV422_DECOMPOSED;
					break;

				case VIDEO_PALETTE_YUV411P:
					saa->grab_format[vm->frame] = YUV411_DECOMPOSED;
					break;

				default:
					/*case VIDEO_PALETTE_RGB565:*/
					saa->grab_format[vm->frame] = RGB16_COMPOSED;
					break;
				}

			set_up_grabbing(saa,vm->frame);
			break;
		}
		case SAA7146_V4L_GMBUF:
		{
                        struct video_mbuf *m = arg;
			int i = 0;

			m->size = saa->buffers * GRABBING_MEM_SIZE;
			m->frames = saa->buffers;

			for(i = 0; i < saa->buffers; i++)
				m->offsets[i]=(i*GRABBING_MEM_SIZE);

			gprintk(KERN_ERR "saa7146_v4l.o: SAA7146_V4L_GMBUF, providing %d buffers.\n", saa->buffers);
			
			break;
		}

		default:
			return -ENOIOCTLCMD;
	}
	
	return 0;
}

int saa7146_v4l_attach(struct saa7146* adap, void** p)
{
	struct saa7146_v4l_struct* data;

	hprintk("saa7146_v4l.o: ==> saa7146_v4l_inc_use_attach\n");
					
	if (!(data = kmalloc(sizeof(struct saa7146_v4l_struct), GFP_KERNEL))) {
	    	printk (KERN_ERR "%s: out of memory!\n", __FUNCTION__);
		return -ENOMEM;
	}
	*(struct saa7146_v4l_struct**)p = data;

	memset(&data->buffer, 0x0, sizeof(struct video_buffer));
	memset(&data->mbuf,   0x0, sizeof(struct video_mbuf));
	memset(&data->window, 0x0, sizeof(struct video_window));
	memset(&data->picture,0x0, sizeof(struct video_picture));

	data->picture.brightness = 32768;
	data->picture.contrast = 32768;
	data->picture.colour = 32768;	/* saturation */
	data->picture.depth = 16;
	data->picture.palette = VIDEO_PALETTE_RGB565;

	return 0;
}


void saa7146_v4l_inc_use(struct saa7146* adap)
{
	MOD_INC_USE_COUNT;
}


int saa7146_v4l_detach(struct saa7146* adap, void** p)
{
	struct saa7146_v4l_struct** data = (struct saa7146_v4l_struct**)p;

	kfree(*data);
	*data = NULL;

	return 0;
}


void saa7146_v4l_dec_use(struct saa7146* adap)
{
	MOD_DEC_USE_COUNT;
}


static struct saa7146_extension saa7146_v4l_extension = {
	"v4l extension\0",
	MASK_27,                            /* handles rps0 irqs */
	saa7146_std_grab_irq_callback_rps0,
	saa7146_v4l_command,
	saa7146_v4l_attach,
	saa7146_v4l_detach,
	saa7146_v4l_inc_use,
	saa7146_v4l_dec_use
};	


int saa7146_v4l_init (void) 
{
	int res = 0;

	if((res = saa7146_add_extension(&saa7146_v4l_extension))) {
		printk("saa7146_v4l.o: extension registration failed, module not inserted.\n");
		return res;
	}
	
	return 0;
}


void saa7146_v4l_exit (void) 
{
	int res = 0;
	if ((res = saa7146_del_extension(&saa7146_v4l_extension))) {
		printk("saa7146_v4l.o: extension deregistration failed, module not removed.\n");
	}
}

MODULE_PARM(saa7146_v4l_debug, "i");
MODULE_PARM_DESC(saa7146_v4l_debug, "set saa7146_v4l.c in debug mode");