blob: 3c11cff00475fa2f0df8caac2ae504b2a7039361 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <media/saa7146_vv.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07002
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -08003#define BOARD_CAN_DO_VBI(dev) (dev->revision != 0 && dev->vv_data->vbi_minor != -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5/****************************************************************************/
6/* resource management functions, shamelessly stolen from saa7134 driver */
7
8int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
9{
10 struct saa7146_dev *dev = fh->dev;
11 struct saa7146_vv *vv = dev->vv_data;
12
13 if (fh->resources & bit) {
14 DEB_D(("already allocated! want: 0x%02x, cur:0x%02x\n",bit,vv->resources));
15 /* have it already allocated */
16 return 1;
17 }
18
19 /* is it free? */
Ingo Molnar3593cab2006-02-07 06:49:14 -020020 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 if (vv->resources & bit) {
22 DEB_D(("locked! vv->resources:0x%02x, we want:0x%02x\n",vv->resources,bit));
23 /* no, someone else uses it */
Ingo Molnar3593cab2006-02-07 06:49:14 -020024 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 return 0;
26 }
27 /* it's free, grab it */
28 fh->resources |= bit;
29 vv->resources |= bit;
30 DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit,vv->resources));
Ingo Molnar3593cab2006-02-07 06:49:14 -020031 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 return 1;
33}
34
35void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits)
36{
37 struct saa7146_dev *dev = fh->dev;
38 struct saa7146_vv *vv = dev->vv_data;
39
Eric Sesterhennae246012006-03-13 13:17:11 -030040 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Ingo Molnar3593cab2006-02-07 06:49:14 -020042 mutex_lock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 fh->resources &= ~bits;
44 vv->resources &= ~bits;
45 DEB_D(("res: put 0x%02x, cur:0x%02x\n",bits,vv->resources));
Ingo Molnar3593cab2006-02-07 06:49:14 -020046 mutex_unlock(&dev->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047}
48
49
50/********************************************************************************/
51/* common dma functions */
52
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030053void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
54 struct saa7146_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -030056 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 DEB_EE(("dev:%p, buf:%p\n",dev,buf));
58
Eric Sesterhennae246012006-03-13 13:17:11 -030059 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61 videobuf_waiton(&buf->vb,0,0);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -030062 videobuf_dma_unmap(q, dma);
63 videobuf_dma_free(dma);
Brandon Philips0fc06862007-11-06 20:02:36 -030064 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065}
66
67
68/********************************************************************************/
69/* common buffer functions */
70
71int saa7146_buffer_queue(struct saa7146_dev *dev,
72 struct saa7146_dmaqueue *q,
73 struct saa7146_buf *buf)
74{
75 assert_spin_locked(&dev->slock);
76 DEB_EE(("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf));
77
78 BUG_ON(!q);
79
80 if (NULL == q->curr) {
81 q->curr = buf;
82 DEB_D(("immediately activating buffer %p\n", buf));
83 buf->activate(dev,buf,NULL);
84 } else {
85 list_add_tail(&buf->vb.queue,&q->queue);
Brandon Philips0fc06862007-11-06 20:02:36 -030086 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf));
88 }
89 return 0;
90}
91
92void saa7146_buffer_finish(struct saa7146_dev *dev,
93 struct saa7146_dmaqueue *q,
94 int state)
95{
96 assert_spin_locked(&dev->slock);
97 DEB_EE(("dev:%p, dmaq:%p, state:%d\n", dev, q, state));
98 DEB_EE(("q->curr:%p\n",q->curr));
99
100 BUG_ON(!q->curr);
101
102 /* finish current buffer */
103 if (NULL == q->curr) {
104 DEB_D(("aiii. no current buffer\n"));
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 q->curr->vb.state = state;
109 do_gettimeofday(&q->curr->vb.ts);
110 wake_up(&q->curr->vb.done);
111
112 q->curr = NULL;
113}
114
115void saa7146_buffer_next(struct saa7146_dev *dev,
116 struct saa7146_dmaqueue *q, int vbi)
117{
118 struct saa7146_buf *buf,*next = NULL;
119
120 BUG_ON(!q);
121
122 DEB_INT(("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi));
123
124 assert_spin_locked(&dev->slock);
125 if (!list_empty(&q->queue)) {
126 /* activate next one from queue */
127 buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue);
128 list_del(&buf->vb.queue);
129 if (!list_empty(&q->queue))
130 next = list_entry(q->queue.next,struct saa7146_buf, vb.queue);
131 q->curr = buf;
132 DEB_INT(("next buffer: buf:%p, prev:%p, next:%p\n", buf, q->queue.prev,q->queue.next));
133 buf->activate(dev,buf,next);
134 } else {
135 DEB_INT(("no next buffer. stopping.\n"));
136 if( 0 != vbi ) {
137 /* turn off video-dma3 */
138 saa7146_write(dev,MC1, MASK_20);
139 } else {
140 /* nothing to do -- just prevent next video-dma1 transfer
141 by lowering the protection address */
142
143 // fixme: fix this for vflip != 0
144
145 saa7146_write(dev, PROT_ADDR1, 0);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800146 saa7146_write(dev, MC2, (MASK_02|MASK_18));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 /* write the address of the rps-program */
149 saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
150 /* turn on rps */
151 saa7146_write(dev, MC1, (MASK_12 | MASK_28));
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/*
154 printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
155 printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
156 printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
157 printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
158 printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
159 printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
160*/
161 }
162 del_timer(&q->timeout);
163 }
164}
165
166void saa7146_buffer_timeout(unsigned long data)
167{
168 struct saa7146_dmaqueue *q = (struct saa7146_dmaqueue*)data;
169 struct saa7146_dev *dev = q->dev;
170 unsigned long flags;
171
172 DEB_EE(("dev:%p, dmaq:%p\n", dev, q));
173
174 spin_lock_irqsave(&dev->slock,flags);
175 if (q->curr) {
176 DEB_D(("timeout on %p\n", q->curr));
Brandon Philips0fc06862007-11-06 20:02:36 -0300177 saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 /* we don't restart the transfer here like other drivers do. when
181 a streaming capture is disabled, the timeout function will be
182 called for the current buffer. if we activate the next buffer now,
183 we mess up our capture logic. if a timeout occurs on another buffer,
184 then something is seriously broken before, so no need to buffer the
185 next capture IMHO... */
186/*
187 saa7146_buffer_next(dev,q);
188*/
189 spin_unlock_irqrestore(&dev->slock,flags);
190}
191
192/********************************************************************************/
193/* file operations */
194
Hans Verkuilbec43662008-12-30 06:58:20 -0300195static int fops_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200197 struct video_device *vdev = video_devdata(file);
198 struct saa7146_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct saa7146_fh *fh = NULL;
200 int result = 0;
201
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200202 enum v4l2_buf_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200204 DEB_EE(("file:%p, dev:%s\n", file, video_device_node_name(vdev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Ingo Molnar3593cab2006-02-07 06:49:14 -0200206 if (mutex_lock_interruptible(&saa7146_devices_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return -ERESTARTSYS;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 DEB_D(("using: %p\n",dev));
210
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200211 type = vdev->vfl_type == VFL_TYPE_GRABBER
212 ? V4L2_BUF_TYPE_VIDEO_CAPTURE
213 : V4L2_BUF_TYPE_VBI_CAPTURE;
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* check if an extension is registered */
216 if( NULL == dev->ext ) {
217 DEB_S(("no extension registered for this device.\n"));
218 result = -ENODEV;
219 goto out;
220 }
221
222 /* allocate per open data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200223 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (NULL == fh) {
225 DEB_S(("cannot allocate memory for per open data.\n"));
226 result = -ENOMEM;
227 goto out;
228 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 file->private_data = fh;
231 fh->dev = dev;
232 fh->type = type;
233
234 if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
235 DEB_S(("initializing vbi...\n"));
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200236 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
237 result = saa7146_vbi_uops.open(dev,file);
238 if (dev->ext_vv_data->vbi_fops.open)
Hans Verkuilbec43662008-12-30 06:58:20 -0300239 dev->ext_vv_data->vbi_fops.open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 } else {
241 DEB_S(("initializing video...\n"));
242 result = saa7146_video_uops.open(dev,file);
243 }
244
245 if (0 != result) {
246 goto out;
247 }
248
249 if( 0 == try_module_get(dev->ext->module)) {
250 result = -EINVAL;
251 goto out;
252 }
253
254 result = 0;
255out:
Al Viro5fa12472008-03-29 03:07:38 +0000256 if (fh && result != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 kfree(fh);
258 file->private_data = NULL;
259 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200260 mutex_unlock(&saa7146_devices_lock);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800261 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Hans Verkuilbec43662008-12-30 06:58:20 -0300264static int fops_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct saa7146_fh *fh = file->private_data;
267 struct saa7146_dev *dev = fh->dev;
268
Hans Verkuilbec43662008-12-30 06:58:20 -0300269 DEB_EE(("file:%p\n", file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Ingo Molnar3593cab2006-02-07 06:49:14 -0200271 if (mutex_lock_interruptible(&saa7146_devices_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return -ERESTARTSYS;
273
274 if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200275 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
276 saa7146_vbi_uops.release(dev,file);
277 if (dev->ext_vv_data->vbi_fops.release)
Hans Verkuilbec43662008-12-30 06:58:20 -0300278 dev->ext_vv_data->vbi_fops.release(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 } else {
280 saa7146_video_uops.release(dev,file);
281 }
282
283 module_put(dev->ext->module);
284 file->private_data = NULL;
285 kfree(fh);
286
Ingo Molnar3593cab2006-02-07 06:49:14 -0200287 mutex_unlock(&saa7146_devices_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289 return 0;
290}
291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292static int fops_mmap(struct file *file, struct vm_area_struct * vma)
293{
294 struct saa7146_fh *fh = file->private_data;
295 struct videobuf_queue *q;
296
297 switch (fh->type) {
298 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
299 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",file, vma));
300 q = &fh->video_q;
301 break;
302 }
303 case V4L2_BUF_TYPE_VBI_CAPTURE: {
304 DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",file, vma));
305 q = &fh->vbi_q;
306 break;
307 }
308 default:
309 BUG();
310 return 0;
311 }
Michael Krufky50c25ff2006-01-09 15:25:34 -0200312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 return videobuf_mmap_mapper(q,vma);
314}
315
316static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
317{
318 struct saa7146_fh *fh = file->private_data;
319 struct videobuf_buffer *buf = NULL;
320 struct videobuf_queue *q;
321
322 DEB_EE(("file:%p, poll:%p\n",file, wait));
323
324 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
325 if( 0 == fh->vbi_q.streaming )
326 return videobuf_poll_stream(file, &fh->vbi_q, wait);
327 q = &fh->vbi_q;
328 } else {
329 DEB_D(("using video queue.\n"));
330 q = &fh->video_q;
331 }
332
333 if (!list_empty(&q->stream))
334 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
335
336 if (!buf) {
337 DEB_D(("buf == NULL!\n"));
338 return POLLERR;
339 }
340
341 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300342 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 DEB_D(("poll succeeded!\n"));
344 return POLLIN|POLLRDNORM;
345 }
346
347 DEB_D(("nothing to poll for, buf->state:%d\n",buf->state));
348 return 0;
349}
350
351static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
352{
353 struct saa7146_fh *fh = file->private_data;
354
355 switch (fh->type) {
356 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
357// DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", file, data, (unsigned long)count));
358 return saa7146_video_uops.read(file,data,count,ppos);
359 }
360 case V4L2_BUF_TYPE_VBI_CAPTURE: {
361// DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", file, data, (unsigned long)count));
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200362 if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
363 return saa7146_vbi_uops.read(file,data,count,ppos);
364 else
365 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367 break;
368 default:
369 BUG();
370 return 0;
371 }
372}
373
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200374static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
375{
376 struct saa7146_fh *fh = file->private_data;
377
378 switch (fh->type) {
379 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
380 return -EINVAL;
381 case V4L2_BUF_TYPE_VBI_CAPTURE:
382 if (fh->dev->ext_vv_data->vbi_fops.write)
383 return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
384 else
385 return -EINVAL;
386 default:
387 BUG();
388 return -EINVAL;
389 }
390}
391
Hans Verkuilbec43662008-12-30 06:58:20 -0300392static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
394 .owner = THIS_MODULE,
395 .open = fops_open,
396 .release = fops_release,
397 .read = fops_read,
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200398 .write = fops_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 .poll = fops_poll,
400 .mmap = fops_mmap,
Hans Verkuilb9600742009-01-18 19:59:11 -0300401 .ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402};
403
Adrian Bunk52c1da32005-06-23 22:05:33 -0700404static void vv_callback(struct saa7146_dev *dev, unsigned long status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 u32 isr = status;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800407
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 DEB_INT(("dev:%p, isr:0x%08x\n",dev,(u32)status));
409
410 if (0 != (isr & (MASK_27))) {
411 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
412 saa7146_video_uops.irq_done(dev,isr);
413 }
414
415 if (0 != (isr & (MASK_28))) {
416 u32 mc2 = saa7146_read(dev, MC2);
417 if( 0 != (mc2 & MASK_15)) {
418 DEB_INT(("irq: RPS1 vbi workaround (0x%08x).\n",isr));
419 wake_up(&dev->vv_data->vbi_wq);
420 saa7146_write(dev,MC2, MASK_31);
421 return;
422 }
423 DEB_INT(("irq: RPS1 (0x%08x).\n",isr));
424 saa7146_vbi_uops.irq_done(dev,isr);
425 }
426}
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
429{
Hans Verkuil230b65f2009-02-07 20:42:33 -0300430 struct saa7146_vv *vv;
431 int err;
Hans Verkuilb9600742009-01-18 19:59:11 -0300432
Hans Verkuil230b65f2009-02-07 20:42:33 -0300433 err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
434 if (err)
435 return err;
436
437 vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
Hans Verkuilb9600742009-01-18 19:59:11 -0300438 if (vv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 ERR(("out of memory. aborting.\n"));
Hans Verkuil230b65f2009-02-07 20:42:33 -0300440 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300442 ext_vv->ops = saa7146_video_ioctl_ops;
443 ext_vv->core_ops = &saa7146_video_ioctl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 DEB_EE(("dev:%p\n",dev));
446
447 /* set default values for video parts of the saa7146 */
448 saa7146_write(dev, BCS_CTRL, 0x80400040);
449
450 /* enable video-port pins */
451 saa7146_write(dev, MC1, (MASK_10 | MASK_26));
452
453 /* save per-device extension data (one extension can
454 handle different devices that might need different
455 configuration data) */
456 dev->ext_vv_data = ext_vv;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 vv->video_minor = -1;
459 vv->vbi_minor = -1;
460
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800461 vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if( NULL == vv->d_clipping.cpu_addr ) {
463 ERR(("out of memory. aborting.\n"));
464 kfree(vv);
465 return -1;
466 }
467 memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
468
469 saa7146_video_uops.init(dev,vv);
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200470 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
471 saa7146_vbi_uops.init(dev,vv);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 dev->vv_data = vv;
474 dev->vv_callback = &vv_callback;
475
476 return 0;
477}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300478EXPORT_SYMBOL_GPL(saa7146_vv_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480int saa7146_vv_release(struct saa7146_dev* dev)
481{
482 struct saa7146_vv *vv = dev->vv_data;
483
484 DEB_EE(("dev:%p\n",dev));
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800485
Hans Verkuil230b65f2009-02-07 20:42:33 -0300486 v4l2_device_unregister(&dev->v4l2_dev);
Marco Schluessler58af0042007-01-31 14:27:55 -0300487 pci_free_consistent(dev->pci, SAA7146_CLIPPING_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800488 kfree(vv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 dev->vv_data = NULL;
490 dev->vv_callback = NULL;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 return 0;
493}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300494EXPORT_SYMBOL_GPL(saa7146_vv_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
496int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
497 char *name, int type)
498{
499 struct saa7146_vv *vv = dev->vv_data;
500 struct video_device *vfd;
Hans Verkuilb9600742009-01-18 19:59:11 -0300501 int err;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300502 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 DEB_EE(("dev:%p, name:'%s', type:%d\n",dev,name,type));
505
506 // released by vfd->release
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800507 vfd = video_device_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 if (vfd == NULL)
509 return -ENOMEM;
510
Hans Verkuilb9600742009-01-18 19:59:11 -0300511 vfd->fops = &video_fops;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300512 vfd->ioctl_ops = &dev->ext_vv_data->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 vfd->release = video_device_release;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300514 vfd->tvnorms = 0;
515 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
516 vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
Hans Verkuilb9600742009-01-18 19:59:11 -0300517 strlcpy(vfd->name, name, sizeof(vfd->name));
Hans Verkuil601e9442008-08-23 07:24:07 -0300518 video_set_drvdata(vfd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Hans Verkuilb9600742009-01-18 19:59:11 -0300520 err = video_register_device(vfd, type, -1);
521 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 ERR(("cannot register v4l2 device. skipping.\n"));
Julia Lawalla2a9b1e2008-01-11 22:03:42 -0300523 video_device_release(vfd);
Hans Verkuilb9600742009-01-18 19:59:11 -0300524 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300527 if (VFL_TYPE_GRABBER == type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 vv->video_minor = vfd->minor;
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300529 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 vv->vbi_minor = vfd->minor;
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300531
532 INFO(("%s: registered device %s [v4l2]\n",
533 dev->name, video_device_node_name(vfd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535 *vid = vfd;
536 return 0;
537}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300538EXPORT_SYMBOL_GPL(saa7146_register_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
540int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
541{
542 struct saa7146_vv *vv = dev->vv_data;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 DEB_EE(("dev:%p\n",dev));
545
Hans Verkuil0ea6bc82008-07-26 08:26:43 -0300546 if ((*vid)->vfl_type == VFL_TYPE_GRABBER) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 vv->video_minor = -1;
548 } else {
549 vv->vbi_minor = -1;
550 }
551
552 video_unregister_device(*vid);
553 *vid = NULL;
554
555 return 0;
556}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300557EXPORT_SYMBOL_GPL(saa7146_unregister_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
559static int __init saa7146_vv_init_module(void)
560{
561 return 0;
562}
563
564
565static void __exit saa7146_vv_cleanup_module(void)
566{
567}
568
569module_init(saa7146_vv_init_module);
570module_exit(saa7146_vv_cleanup_module);
571
572MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
573MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
574MODULE_LICENSE("GPL");