blob: e4547afcfa8809e41c9488b380c58939188ca719 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#include <media/saa7146_vv.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04002#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07003
Linus Torvalds1da177e2005-04-16 15:20:36 -07004/****************************************************************************/
5/* resource management functions, shamelessly stolen from saa7134 driver */
6
7int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
8{
9 struct saa7146_dev *dev = fh->dev;
10 struct saa7146_vv *vv = dev->vv_data;
11
12 if (fh->resources & bit) {
13 DEB_D(("already allocated! want: 0x%02x, cur:0x%02x\n",bit,vv->resources));
14 /* have it already allocated */
15 return 1;
16 }
17
18 /* is it free? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070019 if (vv->resources & bit) {
20 DEB_D(("locked! vv->resources:0x%02x, we want:0x%02x\n",vv->resources,bit));
21 /* no, someone else uses it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 return 0;
23 }
24 /* it's free, grab it */
25 fh->resources |= bit;
26 vv->resources |= bit;
27 DEB_D(("res: get 0x%02x, cur:0x%02x\n",bit,vv->resources));
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return 1;
29}
30
31void saa7146_res_free(struct saa7146_fh *fh, unsigned int bits)
32{
33 struct saa7146_dev *dev = fh->dev;
34 struct saa7146_vv *vv = dev->vv_data;
35
Eric Sesterhennae246012006-03-13 13:17:11 -030036 BUG_ON((fh->resources & bits) != bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 fh->resources &= ~bits;
39 vv->resources &= ~bits;
40 DEB_D(("res: put 0x%02x, cur:0x%02x\n",bits,vv->resources));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
43
44/********************************************************************************/
45/* common dma functions */
46
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030047void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
48 struct saa7146_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -030050 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 DEB_EE(("dev:%p, buf:%p\n",dev,buf));
52
Eric Sesterhennae246012006-03-13 13:17:11 -030053 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Hans Verkuil0e0809a2010-09-26 09:01:26 -030055 videobuf_waiton(q, &buf->vb, 0, 0);
Laurent Pinchart95268402010-05-11 10:36:30 -030056 videobuf_dma_unmap(q->dev, dma);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -030057 videobuf_dma_free(dma);
Brandon Philips0fc06862007-11-06 20:02:36 -030058 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059}
60
61
62/********************************************************************************/
63/* common buffer functions */
64
65int saa7146_buffer_queue(struct saa7146_dev *dev,
66 struct saa7146_dmaqueue *q,
67 struct saa7146_buf *buf)
68{
69 assert_spin_locked(&dev->slock);
70 DEB_EE(("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf));
71
72 BUG_ON(!q);
73
74 if (NULL == q->curr) {
75 q->curr = buf;
76 DEB_D(("immediately activating buffer %p\n", buf));
77 buf->activate(dev,buf,NULL);
78 } else {
79 list_add_tail(&buf->vb.queue,&q->queue);
Brandon Philips0fc06862007-11-06 20:02:36 -030080 buf->vb.state = VIDEOBUF_QUEUED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 DEB_D(("adding buffer %p to queue. (active buffer present)\n", buf));
82 }
83 return 0;
84}
85
86void saa7146_buffer_finish(struct saa7146_dev *dev,
87 struct saa7146_dmaqueue *q,
88 int state)
89{
90 assert_spin_locked(&dev->slock);
91 DEB_EE(("dev:%p, dmaq:%p, state:%d\n", dev, q, state));
92 DEB_EE(("q->curr:%p\n",q->curr));
93
94 BUG_ON(!q->curr);
95
96 /* finish current buffer */
97 if (NULL == q->curr) {
98 DEB_D(("aiii. no current buffer\n"));
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -080099 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 q->curr->vb.state = state;
103 do_gettimeofday(&q->curr->vb.ts);
104 wake_up(&q->curr->vb.done);
105
106 q->curr = NULL;
107}
108
109void saa7146_buffer_next(struct saa7146_dev *dev,
110 struct saa7146_dmaqueue *q, int vbi)
111{
112 struct saa7146_buf *buf,*next = NULL;
113
114 BUG_ON(!q);
115
116 DEB_INT(("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi));
117
118 assert_spin_locked(&dev->slock);
119 if (!list_empty(&q->queue)) {
120 /* activate next one from queue */
121 buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue);
122 list_del(&buf->vb.queue);
123 if (!list_empty(&q->queue))
124 next = list_entry(q->queue.next,struct saa7146_buf, vb.queue);
125 q->curr = buf;
126 DEB_INT(("next buffer: buf:%p, prev:%p, next:%p\n", buf, q->queue.prev,q->queue.next));
127 buf->activate(dev,buf,next);
128 } else {
129 DEB_INT(("no next buffer. stopping.\n"));
130 if( 0 != vbi ) {
131 /* turn off video-dma3 */
132 saa7146_write(dev,MC1, MASK_20);
133 } else {
134 /* nothing to do -- just prevent next video-dma1 transfer
135 by lowering the protection address */
136
137 // fixme: fix this for vflip != 0
138
139 saa7146_write(dev, PROT_ADDR1, 0);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800140 saa7146_write(dev, MC2, (MASK_02|MASK_18));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
142 /* write the address of the rps-program */
143 saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
144 /* turn on rps */
145 saa7146_write(dev, MC1, (MASK_12 | MASK_28));
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
148 printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
149 printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
150 printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
151 printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
152 printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
153 printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
154*/
155 }
156 del_timer(&q->timeout);
157 }
158}
159
160void saa7146_buffer_timeout(unsigned long data)
161{
162 struct saa7146_dmaqueue *q = (struct saa7146_dmaqueue*)data;
163 struct saa7146_dev *dev = q->dev;
164 unsigned long flags;
165
166 DEB_EE(("dev:%p, dmaq:%p\n", dev, q));
167
168 spin_lock_irqsave(&dev->slock,flags);
169 if (q->curr) {
170 DEB_D(("timeout on %p\n", q->curr));
Brandon Philips0fc06862007-11-06 20:02:36 -0300171 saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 /* we don't restart the transfer here like other drivers do. when
175 a streaming capture is disabled, the timeout function will be
176 called for the current buffer. if we activate the next buffer now,
177 we mess up our capture logic. if a timeout occurs on another buffer,
178 then something is seriously broken before, so no need to buffer the
179 next capture IMHO... */
180/*
181 saa7146_buffer_next(dev,q);
182*/
183 spin_unlock_irqrestore(&dev->slock,flags);
184}
185
186/********************************************************************************/
187/* file operations */
188
Hans Verkuilbec43662008-12-30 06:58:20 -0300189static int fops_open(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200191 struct video_device *vdev = video_devdata(file);
192 struct saa7146_dev *dev = video_drvdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 struct saa7146_fh *fh = NULL;
194 int result = 0;
195
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200196 enum v4l2_buf_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Laurent Pinchart50462eb2009-12-10 11:47:13 -0200198 DEB_EE(("file:%p, dev:%s\n", file, video_device_node_name(vdev)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Ingo Molnar3593cab2006-02-07 06:49:14 -0200200 if (mutex_lock_interruptible(&saa7146_devices_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return -ERESTARTSYS;
202
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 DEB_D(("using: %p\n",dev));
204
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200205 type = vdev->vfl_type == VFL_TYPE_GRABBER
206 ? V4L2_BUF_TYPE_VIDEO_CAPTURE
207 : V4L2_BUF_TYPE_VBI_CAPTURE;
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 /* check if an extension is registered */
210 if( NULL == dev->ext ) {
211 DEB_S(("no extension registered for this device.\n"));
212 result = -ENODEV;
213 goto out;
214 }
215
216 /* allocate per open data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200217 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 if (NULL == fh) {
219 DEB_S(("cannot allocate memory for per open data.\n"));
220 result = -ENOMEM;
221 goto out;
222 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 file->private_data = fh;
225 fh->dev = dev;
226 fh->type = type;
227
228 if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
229 DEB_S(("initializing vbi...\n"));
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200230 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
231 result = saa7146_vbi_uops.open(dev,file);
232 if (dev->ext_vv_data->vbi_fops.open)
Hans Verkuilbec43662008-12-30 06:58:20 -0300233 dev->ext_vv_data->vbi_fops.open(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 } else {
235 DEB_S(("initializing video...\n"));
236 result = saa7146_video_uops.open(dev,file);
237 }
238
239 if (0 != result) {
240 goto out;
241 }
242
243 if( 0 == try_module_get(dev->ext->module)) {
244 result = -EINVAL;
245 goto out;
246 }
247
248 result = 0;
249out:
Al Viro5fa12472008-03-29 03:07:38 +0000250 if (fh && result != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 kfree(fh);
252 file->private_data = NULL;
253 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200254 mutex_unlock(&saa7146_devices_lock);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800255 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Hans Verkuilbec43662008-12-30 06:58:20 -0300258static int fops_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
260 struct saa7146_fh *fh = file->private_data;
261 struct saa7146_dev *dev = fh->dev;
262
Hans Verkuilbec43662008-12-30 06:58:20 -0300263 DEB_EE(("file:%p\n", file));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Ingo Molnar3593cab2006-02-07 06:49:14 -0200265 if (mutex_lock_interruptible(&saa7146_devices_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return -ERESTARTSYS;
267
268 if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200269 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
270 saa7146_vbi_uops.release(dev,file);
271 if (dev->ext_vv_data->vbi_fops.release)
Hans Verkuilbec43662008-12-30 06:58:20 -0300272 dev->ext_vv_data->vbi_fops.release(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 } else {
274 saa7146_video_uops.release(dev,file);
275 }
276
277 module_put(dev->ext->module);
278 file->private_data = NULL;
279 kfree(fh);
280
Ingo Molnar3593cab2006-02-07 06:49:14 -0200281 mutex_unlock(&saa7146_devices_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 return 0;
284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286static int fops_mmap(struct file *file, struct vm_area_struct * vma)
287{
288 struct saa7146_fh *fh = file->private_data;
289 struct videobuf_queue *q;
290
291 switch (fh->type) {
292 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
293 DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",file, vma));
294 q = &fh->video_q;
295 break;
296 }
297 case V4L2_BUF_TYPE_VBI_CAPTURE: {
298 DEB_EE(("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",file, vma));
299 q = &fh->vbi_q;
300 break;
301 }
302 default:
303 BUG();
304 return 0;
305 }
Michael Krufky50c25ff2006-01-09 15:25:34 -0200306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return videobuf_mmap_mapper(q,vma);
308}
309
310static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
311{
312 struct saa7146_fh *fh = file->private_data;
313 struct videobuf_buffer *buf = NULL;
314 struct videobuf_queue *q;
315
316 DEB_EE(("file:%p, poll:%p\n",file, wait));
317
318 if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
319 if( 0 == fh->vbi_q.streaming )
320 return videobuf_poll_stream(file, &fh->vbi_q, wait);
321 q = &fh->vbi_q;
322 } else {
323 DEB_D(("using video queue.\n"));
324 q = &fh->video_q;
325 }
326
327 if (!list_empty(&q->stream))
328 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
329
330 if (!buf) {
331 DEB_D(("buf == NULL!\n"));
332 return POLLERR;
333 }
334
335 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300336 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 DEB_D(("poll succeeded!\n"));
338 return POLLIN|POLLRDNORM;
339 }
340
341 DEB_D(("nothing to poll for, buf->state:%d\n",buf->state));
342 return 0;
343}
344
345static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
346{
347 struct saa7146_fh *fh = file->private_data;
348
349 switch (fh->type) {
350 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
351// DEB_EE(("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", file, data, (unsigned long)count));
352 return saa7146_video_uops.read(file,data,count,ppos);
353 }
354 case V4L2_BUF_TYPE_VBI_CAPTURE: {
355// 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 -0200356 if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
357 return saa7146_vbi_uops.read(file,data,count,ppos);
358 else
359 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 }
361 break;
362 default:
363 BUG();
364 return 0;
365 }
366}
367
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200368static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
369{
370 struct saa7146_fh *fh = file->private_data;
371
372 switch (fh->type) {
373 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
374 return -EINVAL;
375 case V4L2_BUF_TYPE_VBI_CAPTURE:
376 if (fh->dev->ext_vv_data->vbi_fops.write)
377 return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
378 else
379 return -EINVAL;
380 default:
381 BUG();
382 return -EINVAL;
383 }
384}
385
Hans Verkuilbec43662008-12-30 06:58:20 -0300386static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 .owner = THIS_MODULE,
389 .open = fops_open,
390 .release = fops_release,
391 .read = fops_read,
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200392 .write = fops_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 .poll = fops_poll,
394 .mmap = fops_mmap,
Hans Verkuil9af39712010-12-18 09:20:59 -0300395 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396};
397
Adrian Bunk52c1da32005-06-23 22:05:33 -0700398static void vv_callback(struct saa7146_dev *dev, unsigned long status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
400 u32 isr = status;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 DEB_INT(("dev:%p, isr:0x%08x\n",dev,(u32)status));
403
404 if (0 != (isr & (MASK_27))) {
405 DEB_INT(("irq: RPS0 (0x%08x).\n",isr));
406 saa7146_video_uops.irq_done(dev,isr);
407 }
408
409 if (0 != (isr & (MASK_28))) {
410 u32 mc2 = saa7146_read(dev, MC2);
411 if( 0 != (mc2 & MASK_15)) {
412 DEB_INT(("irq: RPS1 vbi workaround (0x%08x).\n",isr));
413 wake_up(&dev->vv_data->vbi_wq);
414 saa7146_write(dev,MC2, MASK_31);
415 return;
416 }
417 DEB_INT(("irq: RPS1 (0x%08x).\n",isr));
418 saa7146_vbi_uops.irq_done(dev,isr);
419 }
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
423{
Hans Verkuil230b65f2009-02-07 20:42:33 -0300424 struct saa7146_vv *vv;
Hans Verkuil03b19302010-03-24 19:09:55 -0300425 int err;
426
427 err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
428 if (err)
429 return err;
Hans Verkuil230b65f2009-02-07 20:42:33 -0300430
431 vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
Hans Verkuilb9600742009-01-18 19:59:11 -0300432 if (vv == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 ERR(("out of memory. aborting.\n"));
Hans Verkuil230b65f2009-02-07 20:42:33 -0300434 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300436 ext_vv->ops = saa7146_video_ioctl_ops;
437 ext_vv->core_ops = &saa7146_video_ioctl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 DEB_EE(("dev:%p\n",dev));
440
441 /* set default values for video parts of the saa7146 */
442 saa7146_write(dev, BCS_CTRL, 0x80400040);
443
444 /* enable video-port pins */
445 saa7146_write(dev, MC1, (MASK_10 | MASK_26));
446
447 /* save per-device extension data (one extension can
448 handle different devices that might need different
449 configuration data) */
450 dev->ext_vv_data = ext_vv;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800451
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800452 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 -0700453 if( NULL == vv->d_clipping.cpu_addr ) {
454 ERR(("out of memory. aborting.\n"));
455 kfree(vv);
456 return -1;
457 }
458 memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
459
460 saa7146_video_uops.init(dev,vv);
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200461 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
462 saa7146_vbi_uops.init(dev,vv);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 dev->vv_data = vv;
465 dev->vv_callback = &vv_callback;
466
467 return 0;
468}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300469EXPORT_SYMBOL_GPL(saa7146_vv_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471int saa7146_vv_release(struct saa7146_dev* dev)
472{
473 struct saa7146_vv *vv = dev->vv_data;
474
475 DEB_EE(("dev:%p\n",dev));
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800476
Hans Verkuil230b65f2009-02-07 20:42:33 -0300477 v4l2_device_unregister(&dev->v4l2_dev);
Marco Schluessler58af0042007-01-31 14:27:55 -0300478 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 -0800479 kfree(vv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 dev->vv_data = NULL;
481 dev->vv_callback = NULL;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300485EXPORT_SYMBOL_GPL(saa7146_vv_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
488 char *name, int type)
489{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 struct video_device *vfd;
Hans Verkuilb9600742009-01-18 19:59:11 -0300491 int err;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300492 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 DEB_EE(("dev:%p, name:'%s', type:%d\n",dev,name,type));
495
496 // released by vfd->release
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800497 vfd = video_device_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (vfd == NULL)
499 return -ENOMEM;
500
Hans Verkuilb9600742009-01-18 19:59:11 -0300501 vfd->fops = &video_fops;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300502 vfd->ioctl_ops = &dev->ext_vv_data->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 vfd->release = video_device_release;
Hans Verkuil9af39712010-12-18 09:20:59 -0300504 vfd->lock = &dev->v4l2_lock;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300505 vfd->tvnorms = 0;
506 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
507 vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
Hans Verkuilb9600742009-01-18 19:59:11 -0300508 strlcpy(vfd->name, name, sizeof(vfd->name));
Hans Verkuil601e9442008-08-23 07:24:07 -0300509 video_set_drvdata(vfd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Hans Verkuilb9600742009-01-18 19:59:11 -0300511 err = video_register_device(vfd, type, -1);
512 if (err < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 ERR(("cannot register v4l2 device. skipping.\n"));
Julia Lawalla2a9b1e2008-01-11 22:03:42 -0300514 video_device_release(vfd);
Hans Verkuilb9600742009-01-18 19:59:11 -0300515 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517
Laurent Pinchart38c7c032009-11-27 13:57:15 -0300518 INFO(("%s: registered device %s [v4l2]\n",
519 dev->name, video_device_node_name(vfd)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
521 *vid = vfd;
522 return 0;
523}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300524EXPORT_SYMBOL_GPL(saa7146_register_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
526int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
527{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 DEB_EE(("dev:%p\n",dev));
529
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 video_unregister_device(*vid);
531 *vid = NULL;
532
533 return 0;
534}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300535EXPORT_SYMBOL_GPL(saa7146_unregister_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
537static int __init saa7146_vv_init_module(void)
538{
539 return 0;
540}
541
542
543static void __exit saa7146_vv_cleanup_module(void)
544{
545}
546
547module_init(saa7146_vv_init_module);
548module_exit(saa7146_vv_cleanup_module);
549
550MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
551MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
552MODULE_LICENSE("GPL");