1 // SPDX-License-Identifier: GPL-2.0-only
2 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 
4 #include <media/drv-intf/saa7146_vv.h>
5 #include <linux/module.h>
6 
7 /****************************************************************************/
8 /* resource management functions, shamelessly stolen from saa7134 driver */
9 
saa7146_res_get(struct saa7146_fh * fh,unsigned int bit)10 int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
11 {
12 	struct saa7146_dev *dev = fh->dev;
13 	struct saa7146_vv *vv = dev->vv_data;
14 
15 	if (fh->resources & bit) {
16 		DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n",
17 		      bit, vv->resources);
18 		/* have it already allocated */
19 		return 1;
20 	}
21 
22 	/* is it free? */
23 	if (vv->resources & bit) {
24 		DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n",
25 		      vv->resources, bit);
26 		/* no, someone else uses it */
27 		return 0;
28 	}
29 	/* it's free, grab it */
30 	fh->resources |= bit;
31 	vv->resources |= bit;
32 	DEB_D("res: get 0x%02x, cur:0x%02x\n", bit, vv->resources);
33 	return 1;
34 }
35 
saa7146_res_free(struct saa7146_fh * fh,unsigned int bits)36 void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits)
37 {
38 	struct saa7146_dev *dev = fh->dev;
39 	struct saa7146_vv *vv = dev->vv_data;
40 
41 	BUG_ON((fh->resources & bits) != bits);
42 
43 	fh->resources &= ~bits;
44 	vv->resources &= ~bits;
45 	DEB_D("res: put 0x%02x, cur:0x%02x\n", bits, vv->resources);
46 }
47 
48 
49 /********************************************************************************/
50 /* common dma functions */
51 
saa7146_dma_free(struct saa7146_dev * dev,struct videobuf_queue * q,struct saa7146_buf * buf)52 void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
53 						struct saa7146_buf *buf)
54 {
55 	struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
56 	DEB_EE("dev:%p, buf:%p\n", dev, buf);
57 
58 	videobuf_waiton(q, &buf->vb, 0, 0);
59 	videobuf_dma_unmap(q->dev, dma);
60 	videobuf_dma_free(dma);
61 	buf->vb.state = VIDEOBUF_NEEDS_INIT;
62 }
63 
64 
65 /********************************************************************************/
66 /* common buffer functions */
67 
saa7146_buffer_queue(struct saa7146_dev * dev,struct saa7146_dmaqueue * q,struct saa7146_buf * buf)68 int saa7146_buffer_queue(struct saa7146_dev *dev,
69 			 struct saa7146_dmaqueue *q,
70 			 struct saa7146_buf *buf)
71 {
72 	assert_spin_locked(&dev->slock);
73 	DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf);
74 
75 	BUG_ON(!q);
76 
77 	if (NULL == q->curr) {
78 		q->curr = buf;
79 		DEB_D("immediately activating buffer %p\n", buf);
80 		buf->activate(dev,buf,NULL);
81 	} else {
82 		list_add_tail(&buf->vb.queue,&q->queue);
83 		buf->vb.state = VIDEOBUF_QUEUED;
84 		DEB_D("adding buffer %p to queue. (active buffer present)\n",
85 		      buf);
86 	}
87 	return 0;
88 }
89 
saa7146_buffer_finish(struct saa7146_dev * dev,struct saa7146_dmaqueue * q,int state)90 void saa7146_buffer_finish(struct saa7146_dev *dev,
91 			   struct saa7146_dmaqueue *q,
92 			   int state)
93 {
94 	assert_spin_locked(&dev->slock);
95 	DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev, q, state);
96 	DEB_EE("q->curr:%p\n", q->curr);
97 
98 	/* finish current buffer */
99 	if (NULL == q->curr) {
100 		DEB_D("aiii. no current buffer\n");
101 		return;
102 	}
103 
104 	q->curr->vb.state = state;
105 	q->curr->vb.ts = ktime_get_ns();
106 	wake_up(&q->curr->vb.done);
107 
108 	q->curr = NULL;
109 }
110 
saa7146_buffer_next(struct saa7146_dev * dev,struct saa7146_dmaqueue * q,int vbi)111 void saa7146_buffer_next(struct saa7146_dev *dev,
112 			 struct saa7146_dmaqueue *q, int vbi)
113 {
114 	struct saa7146_buf *buf,*next = NULL;
115 
116 	BUG_ON(!q);
117 
118 	DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi);
119 
120 	assert_spin_locked(&dev->slock);
121 	if (!list_empty(&q->queue)) {
122 		/* activate next one from queue */
123 		buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue);
124 		list_del(&buf->vb.queue);
125 		if (!list_empty(&q->queue))
126 			next = list_entry(q->queue.next,struct saa7146_buf, vb.queue);
127 		q->curr = buf;
128 		DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n",
129 			buf, q->queue.prev, q->queue.next);
130 		buf->activate(dev,buf,next);
131 	} else {
132 		DEB_INT("no next buffer. stopping.\n");
133 		if( 0 != vbi ) {
134 			/* turn off video-dma3 */
135 			saa7146_write(dev,MC1, MASK_20);
136 		} else {
137 			/* nothing to do -- just prevent next video-dma1 transfer
138 			   by lowering the protection address */
139 
140 			// fixme: fix this for vflip != 0
141 
142 			saa7146_write(dev, PROT_ADDR1, 0);
143 			saa7146_write(dev, MC2, (MASK_02|MASK_18));
144 
145 			/* write the address of the rps-program */
146 			saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
147 			/* turn on rps */
148 			saa7146_write(dev, MC1, (MASK_12 | MASK_28));
149 
150 /*
151 			printk("vdma%d.base_even:     0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
152 			printk("vdma%d.base_odd:      0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
153 			printk("vdma%d.prot_addr:     0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
154 			printk("vdma%d.base_page:     0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
155 			printk("vdma%d.pitch:         0x%08x\n", 1,saa7146_read(dev,PITCH1));
156 			printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
157 */
158 		}
159 		del_timer(&q->timeout);
160 	}
161 }
162 
saa7146_buffer_timeout(struct timer_list * t)163 void saa7146_buffer_timeout(struct timer_list *t)
164 {
165 	struct saa7146_dmaqueue *q = from_timer(q, t, timeout);
166 	struct saa7146_dev *dev = q->dev;
167 	unsigned long flags;
168 
169 	DEB_EE("dev:%p, dmaq:%p\n", dev, q);
170 
171 	spin_lock_irqsave(&dev->slock,flags);
172 	if (q->curr) {
173 		DEB_D("timeout on %p\n", q->curr);
174 		saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR);
175 	}
176 
177 	/* we don't restart the transfer here like other drivers do. when
178 	   a streaming capture is disabled, the timeout function will be
179 	   called for the current buffer. if we activate the next buffer now,
180 	   we mess up our capture logic. if a timeout occurs on another buffer,
181 	   then something is seriously broken before, so no need to buffer the
182 	   next capture IMHO... */
183 /*
184 	saa7146_buffer_next(dev,q);
185 */
186 	spin_unlock_irqrestore(&dev->slock,flags);
187 }
188 
189 /********************************************************************************/
190 /* file operations */
191 
fops_open(struct file * file)192 static int fops_open(struct file *file)
193 {
194 	struct video_device *vdev = video_devdata(file);
195 	struct saa7146_dev *dev = video_drvdata(file);
196 	struct saa7146_fh *fh = NULL;
197 	int result = 0;
198 
199 	DEB_EE("file:%p, dev:%s\n", file, video_device_node_name(vdev));
200 
201 	if (mutex_lock_interruptible(vdev->lock))
202 		return -ERESTARTSYS;
203 
204 	DEB_D("using: %p\n", dev);
205 
206 	/* check if an extension is registered */
207 	if( NULL == dev->ext ) {
208 		DEB_S("no extension registered for this device\n");
209 		result = -ENODEV;
210 		goto out;
211 	}
212 
213 	/* allocate per open data */
214 	fh = kzalloc(sizeof(*fh),GFP_KERNEL);
215 	if (NULL == fh) {
216 		DEB_S("cannot allocate memory for per open data\n");
217 		result = -ENOMEM;
218 		goto out;
219 	}
220 
221 	v4l2_fh_init(&fh->fh, vdev);
222 
223 	file->private_data = &fh->fh;
224 	fh->dev = dev;
225 
226 	if (vdev->vfl_type == VFL_TYPE_VBI) {
227 		DEB_S("initializing vbi...\n");
228 		if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
229 			result = saa7146_vbi_uops.open(dev,file);
230 		if (dev->ext_vv_data->vbi_fops.open)
231 			dev->ext_vv_data->vbi_fops.open(file);
232 	} else {
233 		DEB_S("initializing video...\n");
234 		result = saa7146_video_uops.open(dev,file);
235 	}
236 
237 	if (0 != result) {
238 		goto out;
239 	}
240 
241 	if( 0 == try_module_get(dev->ext->module)) {
242 		result = -EINVAL;
243 		goto out;
244 	}
245 
246 	result = 0;
247 	v4l2_fh_add(&fh->fh);
248 out:
249 	if (fh && result != 0) {
250 		kfree(fh);
251 		file->private_data = NULL;
252 	}
253 	mutex_unlock(vdev->lock);
254 	return result;
255 }
256 
fops_release(struct file * file)257 static int fops_release(struct file *file)
258 {
259 	struct video_device *vdev = video_devdata(file);
260 	struct saa7146_fh  *fh  = file->private_data;
261 	struct saa7146_dev *dev = fh->dev;
262 
263 	DEB_EE("file:%p\n", file);
264 
265 	mutex_lock(vdev->lock);
266 
267 	if (vdev->vfl_type == VFL_TYPE_VBI) {
268 		if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
269 			saa7146_vbi_uops.release(dev,file);
270 		if (dev->ext_vv_data->vbi_fops.release)
271 			dev->ext_vv_data->vbi_fops.release(file);
272 	} else {
273 		saa7146_video_uops.release(dev,file);
274 	}
275 
276 	v4l2_fh_del(&fh->fh);
277 	v4l2_fh_exit(&fh->fh);
278 	module_put(dev->ext->module);
279 	file->private_data = NULL;
280 	kfree(fh);
281 
282 	mutex_unlock(vdev->lock);
283 
284 	return 0;
285 }
286 
fops_mmap(struct file * file,struct vm_area_struct * vma)287 static int fops_mmap(struct file *file, struct vm_area_struct * vma)
288 {
289 	struct video_device *vdev = video_devdata(file);
290 	struct saa7146_fh *fh = file->private_data;
291 	struct videobuf_queue *q;
292 	int res;
293 
294 	switch (vdev->vfl_type) {
295 	case VFL_TYPE_VIDEO: {
296 		DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",
297 		       file, vma);
298 		q = &fh->video_q;
299 		break;
300 		}
301 	case VFL_TYPE_VBI: {
302 		DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",
303 		       file, vma);
304 		if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_SLICED_VBI_OUTPUT)
305 			return -ENODEV;
306 		q = &fh->vbi_q;
307 		break;
308 		}
309 	default:
310 		BUG();
311 	}
312 
313 	if (mutex_lock_interruptible(vdev->lock))
314 		return -ERESTARTSYS;
315 	res = videobuf_mmap_mapper(q, vma);
316 	mutex_unlock(vdev->lock);
317 	return res;
318 }
319 
__fops_poll(struct file * file,struct poll_table_struct * wait)320 static __poll_t __fops_poll(struct file *file, struct poll_table_struct *wait)
321 {
322 	struct video_device *vdev = video_devdata(file);
323 	struct saa7146_fh *fh = file->private_data;
324 	struct videobuf_buffer *buf = NULL;
325 	struct videobuf_queue *q;
326 	__poll_t res = v4l2_ctrl_poll(file, wait);
327 
328 	DEB_EE("file:%p, poll:%p\n", file, wait);
329 
330 	if (vdev->vfl_type == VFL_TYPE_VBI) {
331 		if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_SLICED_VBI_OUTPUT)
332 			return res | EPOLLOUT | EPOLLWRNORM;
333 		if( 0 == fh->vbi_q.streaming )
334 			return res | videobuf_poll_stream(file, &fh->vbi_q, wait);
335 		q = &fh->vbi_q;
336 	} else {
337 		DEB_D("using video queue\n");
338 		q = &fh->video_q;
339 	}
340 
341 	if (!list_empty(&q->stream))
342 		buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
343 
344 	if (!buf) {
345 		DEB_D("buf == NULL!\n");
346 		return res | EPOLLERR;
347 	}
348 
349 	poll_wait(file, &buf->done, wait);
350 	if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
351 		DEB_D("poll succeeded!\n");
352 		return res | EPOLLIN | EPOLLRDNORM;
353 	}
354 
355 	DEB_D("nothing to poll for, buf->state:%d\n", buf->state);
356 	return res;
357 }
358 
fops_poll(struct file * file,struct poll_table_struct * wait)359 static __poll_t fops_poll(struct file *file, struct poll_table_struct *wait)
360 {
361 	struct video_device *vdev = video_devdata(file);
362 	__poll_t res;
363 
364 	mutex_lock(vdev->lock);
365 	res = __fops_poll(file, wait);
366 	mutex_unlock(vdev->lock);
367 	return res;
368 }
369 
fops_read(struct file * file,char __user * data,size_t count,loff_t * ppos)370 static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
371 {
372 	struct video_device *vdev = video_devdata(file);
373 	struct saa7146_fh *fh = file->private_data;
374 	int ret;
375 
376 	switch (vdev->vfl_type) {
377 	case VFL_TYPE_VIDEO:
378 /*
379 		DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun",
380 		       file, data, (unsigned long)count);
381 */
382 		return saa7146_video_uops.read(file,data,count,ppos);
383 	case VFL_TYPE_VBI:
384 /*
385 		DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n",
386 		       file, data, (unsigned long)count);
387 */
388 		if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) {
389 			if (mutex_lock_interruptible(vdev->lock))
390 				return -ERESTARTSYS;
391 			ret = saa7146_vbi_uops.read(file, data, count, ppos);
392 			mutex_unlock(vdev->lock);
393 			return ret;
394 		}
395 		return -EINVAL;
396 	default:
397 		BUG();
398 	}
399 }
400 
fops_write(struct file * file,const char __user * data,size_t count,loff_t * ppos)401 static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
402 {
403 	struct video_device *vdev = video_devdata(file);
404 	struct saa7146_fh *fh = file->private_data;
405 	int ret;
406 
407 	switch (vdev->vfl_type) {
408 	case VFL_TYPE_VIDEO:
409 		return -EINVAL;
410 	case VFL_TYPE_VBI:
411 		if (fh->dev->ext_vv_data->vbi_fops.write) {
412 			if (mutex_lock_interruptible(vdev->lock))
413 				return -ERESTARTSYS;
414 			ret = fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
415 			mutex_unlock(vdev->lock);
416 			return ret;
417 		}
418 		return -EINVAL;
419 	default:
420 		BUG();
421 	}
422 }
423 
424 static const struct v4l2_file_operations video_fops =
425 {
426 	.owner		= THIS_MODULE,
427 	.open		= fops_open,
428 	.release	= fops_release,
429 	.read		= fops_read,
430 	.write		= fops_write,
431 	.poll		= fops_poll,
432 	.mmap		= fops_mmap,
433 	.unlocked_ioctl	= video_ioctl2,
434 };
435 
vv_callback(struct saa7146_dev * dev,unsigned long status)436 static void vv_callback(struct saa7146_dev *dev, unsigned long status)
437 {
438 	u32 isr = status;
439 
440 	DEB_INT("dev:%p, isr:0x%08x\n", dev, (u32)status);
441 
442 	if (0 != (isr & (MASK_27))) {
443 		DEB_INT("irq: RPS0 (0x%08x)\n", isr);
444 		saa7146_video_uops.irq_done(dev,isr);
445 	}
446 
447 	if (0 != (isr & (MASK_28))) {
448 		u32 mc2 = saa7146_read(dev, MC2);
449 		if( 0 != (mc2 & MASK_15)) {
450 			DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr);
451 			wake_up(&dev->vv_data->vbi_wq);
452 			saa7146_write(dev,MC2, MASK_31);
453 			return;
454 		}
455 		DEB_INT("irq: RPS1 (0x%08x)\n", isr);
456 		saa7146_vbi_uops.irq_done(dev,isr);
457 	}
458 }
459 
460 static const struct v4l2_ctrl_ops saa7146_ctrl_ops = {
461 	.s_ctrl = saa7146_s_ctrl,
462 };
463 
saa7146_vv_init(struct saa7146_dev * dev,struct saa7146_ext_vv * ext_vv)464 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
465 {
466 	struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
467 	struct v4l2_pix_format *fmt;
468 	struct v4l2_vbi_format *vbi;
469 	struct saa7146_vv *vv;
470 	int err;
471 
472 	err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
473 	if (err)
474 		return err;
475 
476 	v4l2_ctrl_handler_init(hdl, 6);
477 	v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
478 		V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
479 	v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
480 		V4L2_CID_CONTRAST, 0, 127, 1, 64);
481 	v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
482 		V4L2_CID_SATURATION, 0, 127, 1, 64);
483 	v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
484 		V4L2_CID_VFLIP, 0, 1, 1, 0);
485 	v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
486 		V4L2_CID_HFLIP, 0, 1, 1, 0);
487 	if (hdl->error) {
488 		err = hdl->error;
489 		v4l2_ctrl_handler_free(hdl);
490 		v4l2_device_unregister(&dev->v4l2_dev);
491 		return err;
492 	}
493 	dev->v4l2_dev.ctrl_handler = hdl;
494 
495 	vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
496 	if (vv == NULL) {
497 		ERR("out of memory. aborting.\n");
498 		v4l2_ctrl_handler_free(hdl);
499 		v4l2_device_unregister(&dev->v4l2_dev);
500 		return -ENOMEM;
501 	}
502 	ext_vv->vid_ops = saa7146_video_ioctl_ops;
503 	ext_vv->vbi_ops = saa7146_vbi_ioctl_ops;
504 	ext_vv->core_ops = &saa7146_video_ioctl_ops;
505 
506 	DEB_EE("dev:%p\n", dev);
507 
508 	/* set default values for video parts of the saa7146 */
509 	saa7146_write(dev, BCS_CTRL, 0x80400040);
510 
511 	/* enable video-port pins */
512 	saa7146_write(dev, MC1, (MASK_10 | MASK_26));
513 
514 	/* save per-device extension data (one extension can
515 	   handle different devices that might need different
516 	   configuration data) */
517 	dev->ext_vv_data = ext_vv;
518 
519 	vv->d_clipping.cpu_addr =
520 		dma_alloc_coherent(&dev->pci->dev, SAA7146_CLIPPING_MEM,
521 				   &vv->d_clipping.dma_handle, GFP_KERNEL);
522 	if( NULL == vv->d_clipping.cpu_addr ) {
523 		ERR("out of memory. aborting.\n");
524 		kfree(vv);
525 		v4l2_ctrl_handler_free(hdl);
526 		v4l2_device_unregister(&dev->v4l2_dev);
527 		return -ENOMEM;
528 	}
529 
530 	saa7146_video_uops.init(dev,vv);
531 	if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
532 		saa7146_vbi_uops.init(dev,vv);
533 
534 	vv->ov_fb.fmt.width = vv->standard->h_max_out;
535 	vv->ov_fb.fmt.height = vv->standard->v_max_out;
536 	vv->ov_fb.fmt.pixelformat = V4L2_PIX_FMT_RGB565;
537 	vv->ov_fb.fmt.bytesperline = 2 * vv->ov_fb.fmt.width;
538 	vv->ov_fb.fmt.sizeimage = vv->ov_fb.fmt.bytesperline * vv->ov_fb.fmt.height;
539 	vv->ov_fb.fmt.colorspace = V4L2_COLORSPACE_SRGB;
540 
541 	fmt = &vv->video_fmt;
542 	fmt->width = 384;
543 	fmt->height = 288;
544 	fmt->pixelformat = V4L2_PIX_FMT_BGR24;
545 	fmt->field = V4L2_FIELD_ANY;
546 	fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
547 	fmt->bytesperline = 3 * fmt->width;
548 	fmt->sizeimage = fmt->bytesperline * fmt->height;
549 
550 	vbi = &vv->vbi_fmt;
551 	vbi->sampling_rate	= 27000000;
552 	vbi->offset		= 248; /* todo */
553 	vbi->samples_per_line	= 720 * 2;
554 	vbi->sample_format	= V4L2_PIX_FMT_GREY;
555 
556 	/* fixme: this only works for PAL */
557 	vbi->start[0] = 5;
558 	vbi->count[0] = 16;
559 	vbi->start[1] = 312;
560 	vbi->count[1] = 16;
561 
562 	timer_setup(&vv->vbi_read_timeout, NULL, 0);
563 
564 	vv->ov_fb.capability = V4L2_FBUF_CAP_LIST_CLIPPING;
565 	vv->ov_fb.flags = V4L2_FBUF_FLAG_PRIMARY;
566 	dev->vv_data = vv;
567 	dev->vv_callback = &vv_callback;
568 
569 	return 0;
570 }
571 EXPORT_SYMBOL_GPL(saa7146_vv_init);
572 
saa7146_vv_release(struct saa7146_dev * dev)573 int saa7146_vv_release(struct saa7146_dev* dev)
574 {
575 	struct saa7146_vv *vv = dev->vv_data;
576 
577 	DEB_EE("dev:%p\n", dev);
578 
579 	v4l2_device_unregister(&dev->v4l2_dev);
580 	dma_free_coherent(&dev->pci->dev, SAA7146_CLIPPING_MEM,
581 			  vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
582 	v4l2_ctrl_handler_free(&dev->ctrl_handler);
583 	kfree(vv);
584 	dev->vv_data = NULL;
585 	dev->vv_callback = NULL;
586 
587 	return 0;
588 }
589 EXPORT_SYMBOL_GPL(saa7146_vv_release);
590 
saa7146_register_device(struct video_device * vfd,struct saa7146_dev * dev,char * name,int type)591 int saa7146_register_device(struct video_device *vfd, struct saa7146_dev *dev,
592 			    char *name, int type)
593 {
594 	int err;
595 	int i;
596 
597 	DEB_EE("dev:%p, name:'%s', type:%d\n", dev, name, type);
598 
599 	vfd->fops = &video_fops;
600 	if (type == VFL_TYPE_VIDEO)
601 		vfd->ioctl_ops = &dev->ext_vv_data->vid_ops;
602 	else
603 		vfd->ioctl_ops = &dev->ext_vv_data->vbi_ops;
604 	vfd->release = video_device_release_empty;
605 	vfd->lock = &dev->v4l2_lock;
606 	vfd->v4l2_dev = &dev->v4l2_dev;
607 	vfd->tvnorms = 0;
608 	for (i = 0; i < dev->ext_vv_data->num_stds; i++)
609 		vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
610 	strscpy(vfd->name, name, sizeof(vfd->name));
611 	vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY |
612 			   V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
613 	vfd->device_caps |= dev->ext_vv_data->capabilities;
614 	if (type == VFL_TYPE_VIDEO)
615 		vfd->device_caps &=
616 			~(V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_OUTPUT);
617 	else
618 		vfd->device_caps &=
619 			~(V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OVERLAY | V4L2_CAP_AUDIO);
620 	video_set_drvdata(vfd, dev);
621 
622 	err = video_register_device(vfd, type, -1);
623 	if (err < 0) {
624 		ERR("cannot register v4l2 device. skipping.\n");
625 		return err;
626 	}
627 
628 	pr_info("%s: registered device %s [v4l2]\n",
629 		dev->name, video_device_node_name(vfd));
630 	return 0;
631 }
632 EXPORT_SYMBOL_GPL(saa7146_register_device);
633 
saa7146_unregister_device(struct video_device * vfd,struct saa7146_dev * dev)634 int saa7146_unregister_device(struct video_device *vfd, struct saa7146_dev *dev)
635 {
636 	DEB_EE("dev:%p\n", dev);
637 
638 	video_unregister_device(vfd);
639 	return 0;
640 }
641 EXPORT_SYMBOL_GPL(saa7146_unregister_device);
642 
saa7146_vv_init_module(void)643 static int __init saa7146_vv_init_module(void)
644 {
645 	return 0;
646 }
647 
648 
saa7146_vv_cleanup_module(void)649 static void __exit saa7146_vv_cleanup_module(void)
650 {
651 }
652 
653 module_init(saa7146_vv_init_module);
654 module_exit(saa7146_vv_cleanup_module);
655 
656 MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
657 MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
658 MODULE_LICENSE("GPL");
659