blob: dfb396568ab691a325bac003bad516fa03b02e5b [file] [log] [blame]
Joe Perches44d0b802011-08-21 19:56:44 -03001#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
2
Linus Torvalds1da177e2005-04-16 15:20:36 -07003#include <media/saa7146_vv.h>
Paul Gortmaker7a707b82011-07-03 14:03:12 -04004#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006/****************************************************************************/
7/* resource management functions, shamelessly stolen from saa7134 driver */
8
9int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
10{
11 struct saa7146_dev *dev = fh->dev;
12 struct saa7146_vv *vv = dev->vv_data;
13
14 if (fh->resources & bit) {
Joe Perches44d0b802011-08-21 19:56:44 -030015 DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n",
16 bit, vv->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 /* have it already allocated */
18 return 1;
19 }
20
21 /* is it free? */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 if (vv->resources & bit) {
Joe Perches44d0b802011-08-21 19:56:44 -030023 DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n",
24 vv->resources, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 /* no, someone else uses it */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 return 0;
27 }
28 /* it's free, grab it */
Joe Perches44d0b802011-08-21 19:56:44 -030029 fh->resources |= bit;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 vv->resources |= bit;
Joe Perches44d0b802011-08-21 19:56:44 -030031 DEB_D("res: get 0x%02x, cur:0x%02x\n", bit, vv->resources);
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
Joe Perches44d0b802011-08-21 19:56:44 -030042 fh->resources &= ~bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 vv->resources &= ~bits;
Joe Perches44d0b802011-08-21 19:56:44 -030044 DEB_D("res: put 0x%02x, cur:0x%02x\n", bits, vv->resources);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
47
48/********************************************************************************/
49/* common dma functions */
50
Mauro Carvalho Chehabc7b0ac02006-03-10 12:29:15 -030051void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
52 struct saa7146_buf *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -030054 struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
Joe Perches44d0b802011-08-21 19:56:44 -030055 DEB_EE("dev:%p, buf:%p\n", dev, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Eric Sesterhennae246012006-03-13 13:17:11 -030057 BUG_ON(in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Hans Verkuil0e0809a2010-09-26 09:01:26 -030059 videobuf_waiton(q, &buf->vb, 0, 0);
Laurent Pinchart95268402010-05-11 10:36:30 -030060 videobuf_dma_unmap(q->dev, dma);
Mauro Carvalho Chehabc1accaa2007-08-23 16:37:49 -030061 videobuf_dma_free(dma);
Brandon Philips0fc06862007-11-06 20:02:36 -030062 buf->vb.state = VIDEOBUF_NEEDS_INIT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063}
64
65
66/********************************************************************************/
67/* common buffer functions */
68
69int saa7146_buffer_queue(struct saa7146_dev *dev,
70 struct saa7146_dmaqueue *q,
71 struct saa7146_buf *buf)
72{
73 assert_spin_locked(&dev->slock);
Joe Perches44d0b802011-08-21 19:56:44 -030074 DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 BUG_ON(!q);
77
78 if (NULL == q->curr) {
79 q->curr = buf;
Joe Perches44d0b802011-08-21 19:56:44 -030080 DEB_D("immediately activating buffer %p\n", buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 buf->activate(dev,buf,NULL);
82 } else {
83 list_add_tail(&buf->vb.queue,&q->queue);
Brandon Philips0fc06862007-11-06 20:02:36 -030084 buf->vb.state = VIDEOBUF_QUEUED;
Joe Perches44d0b802011-08-21 19:56:44 -030085 DEB_D("adding buffer %p to queue. (active buffer present)\n",
86 buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 }
88 return 0;
89}
90
91void saa7146_buffer_finish(struct saa7146_dev *dev,
92 struct saa7146_dmaqueue *q,
93 int state)
94{
95 assert_spin_locked(&dev->slock);
Joe Perches44d0b802011-08-21 19:56:44 -030096 DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev, q, state);
97 DEB_EE("q->curr:%p\n", q->curr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99 BUG_ON(!q->curr);
100
101 /* finish current buffer */
102 if (NULL == q->curr) {
Joe Perches44d0b802011-08-21 19:56:44 -0300103 DEB_D("aiii. no current buffer\n");
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800104 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 q->curr->vb.state = state;
108 do_gettimeofday(&q->curr->vb.ts);
109 wake_up(&q->curr->vb.done);
110
111 q->curr = NULL;
112}
113
114void saa7146_buffer_next(struct saa7146_dev *dev,
115 struct saa7146_dmaqueue *q, int vbi)
116{
117 struct saa7146_buf *buf,*next = NULL;
118
119 BUG_ON(!q);
120
Joe Perches44d0b802011-08-21 19:56:44 -0300121 DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 assert_spin_locked(&dev->slock);
124 if (!list_empty(&q->queue)) {
125 /* activate next one from queue */
126 buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue);
127 list_del(&buf->vb.queue);
128 if (!list_empty(&q->queue))
129 next = list_entry(q->queue.next,struct saa7146_buf, vb.queue);
130 q->curr = buf;
Joe Perches44d0b802011-08-21 19:56:44 -0300131 DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n",
132 buf, q->queue.prev, q->queue.next);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 buf->activate(dev,buf,next);
134 } else {
Joe Perches44d0b802011-08-21 19:56:44 -0300135 DEB_INT("no next buffer. stopping.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 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
Joe Perches44d0b802011-08-21 19:56:44 -0300172 DEB_EE("dev:%p, dmaq:%p\n", dev, q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 spin_lock_irqsave(&dev->slock,flags);
175 if (q->curr) {
Joe Perches44d0b802011-08-21 19:56:44 -0300176 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;
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200201 enum v4l2_buf_type type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Joe Perches44d0b802011-08-21 19:56:44 -0300203 DEB_EE("file:%p, dev:%s\n", file, video_device_node_name(vdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Ingo Molnar3593cab2006-02-07 06:49:14 -0200205 if (mutex_lock_interruptible(&saa7146_devices_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return -ERESTARTSYS;
207
Joe Perches44d0b802011-08-21 19:56:44 -0300208 DEB_D("using: %p\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Laurent Pinchart63b0d5a2009-12-10 11:44:04 -0200210 type = vdev->vfl_type == VFL_TYPE_GRABBER
211 ? V4L2_BUF_TYPE_VIDEO_CAPTURE
212 : V4L2_BUF_TYPE_VBI_CAPTURE;
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 /* check if an extension is registered */
215 if( NULL == dev->ext ) {
Joe Perches44d0b802011-08-21 19:56:44 -0300216 DEB_S("no extension registered for this device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 result = -ENODEV;
218 goto out;
219 }
220
221 /* allocate per open data */
Panagiotis Issaris74081872006-01-11 19:40:56 -0200222 fh = kzalloc(sizeof(*fh),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (NULL == fh) {
Joe Perches44d0b802011-08-21 19:56:44 -0300224 DEB_S("cannot allocate memory for per open data\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 result = -ENOMEM;
226 goto out;
227 }
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800228
Hans Verkuil537fa492012-05-01 12:04:52 -0300229 v4l2_fh_init(&fh->fh, vdev);
230
231 file->private_data = &fh->fh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 fh->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Hans Verkuil6694a562012-05-01 11:39:08 -0300234 if (vdev->vfl_type == VFL_TYPE_VBI) {
Joe Perches44d0b802011-08-21 19:56:44 -0300235 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 {
Joe Perches44d0b802011-08-21 19:56:44 -0300241 DEB_S("initializing video...\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 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;
Hans Verkuil537fa492012-05-01 12:04:52 -0300255 v4l2_fh_add(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256out:
Al Viro5fa12472008-03-29 03:07:38 +0000257 if (fh && result != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 kfree(fh);
259 file->private_data = NULL;
260 }
Ingo Molnar3593cab2006-02-07 06:49:14 -0200261 mutex_unlock(&saa7146_devices_lock);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800262 return result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263}
264
Hans Verkuilbec43662008-12-30 06:58:20 -0300265static int fops_release(struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Hans Verkuil6694a562012-05-01 11:39:08 -0300267 struct video_device *vdev = video_devdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct saa7146_fh *fh = file->private_data;
269 struct saa7146_dev *dev = fh->dev;
270
Joe Perches44d0b802011-08-21 19:56:44 -0300271 DEB_EE("file:%p\n", file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Ingo Molnar3593cab2006-02-07 06:49:14 -0200273 if (mutex_lock_interruptible(&saa7146_devices_lock))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return -ERESTARTSYS;
275
Hans Verkuil6694a562012-05-01 11:39:08 -0300276 if (vdev->vfl_type == VFL_TYPE_VBI) {
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200277 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
278 saa7146_vbi_uops.release(dev,file);
279 if (dev->ext_vv_data->vbi_fops.release)
Hans Verkuilbec43662008-12-30 06:58:20 -0300280 dev->ext_vv_data->vbi_fops.release(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 } else {
282 saa7146_video_uops.release(dev,file);
283 }
284
Hans Verkuil537fa492012-05-01 12:04:52 -0300285 v4l2_fh_del(&fh->fh);
286 v4l2_fh_exit(&fh->fh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 module_put(dev->ext->module);
288 file->private_data = NULL;
289 kfree(fh);
290
Ingo Molnar3593cab2006-02-07 06:49:14 -0200291 mutex_unlock(&saa7146_devices_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 return 0;
294}
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296static int fops_mmap(struct file *file, struct vm_area_struct * vma)
297{
Hans Verkuil6694a562012-05-01 11:39:08 -0300298 struct video_device *vdev = video_devdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 struct saa7146_fh *fh = file->private_data;
300 struct videobuf_queue *q;
301
Hans Verkuil6694a562012-05-01 11:39:08 -0300302 switch (vdev->vfl_type) {
303 case VFL_TYPE_GRABBER: {
Joe Perches44d0b802011-08-21 19:56:44 -0300304 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",
305 file, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 q = &fh->video_q;
307 break;
308 }
Hans Verkuil6694a562012-05-01 11:39:08 -0300309 case VFL_TYPE_VBI: {
Joe Perches44d0b802011-08-21 19:56:44 -0300310 DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",
311 file, vma);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 q = &fh->vbi_q;
313 break;
314 }
315 default:
316 BUG();
317 return 0;
318 }
Michael Krufky50c25ff2006-01-09 15:25:34 -0200319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return videobuf_mmap_mapper(q,vma);
321}
322
323static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
324{
Hans Verkuil6694a562012-05-01 11:39:08 -0300325 struct video_device *vdev = video_devdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 struct saa7146_fh *fh = file->private_data;
327 struct videobuf_buffer *buf = NULL;
328 struct videobuf_queue *q;
Hans Verkuil537fa492012-05-01 12:04:52 -0300329 unsigned int res = v4l2_ctrl_poll(file, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
Joe Perches44d0b802011-08-21 19:56:44 -0300331 DEB_EE("file:%p, poll:%p\n", file, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Hans Verkuil6694a562012-05-01 11:39:08 -0300333 if (vdev->vfl_type == VFL_TYPE_VBI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 if( 0 == fh->vbi_q.streaming )
Hans Verkuil537fa492012-05-01 12:04:52 -0300335 return res | videobuf_poll_stream(file, &fh->vbi_q, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 q = &fh->vbi_q;
337 } else {
Joe Perches44d0b802011-08-21 19:56:44 -0300338 DEB_D("using video queue\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 q = &fh->video_q;
340 }
341
342 if (!list_empty(&q->stream))
343 buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
344
345 if (!buf) {
Joe Perches44d0b802011-08-21 19:56:44 -0300346 DEB_D("buf == NULL!\n");
Hans Verkuil537fa492012-05-01 12:04:52 -0300347 return res | POLLERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 }
349
350 poll_wait(file, &buf->done, wait);
Brandon Philips0fc06862007-11-06 20:02:36 -0300351 if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
Joe Perches44d0b802011-08-21 19:56:44 -0300352 DEB_D("poll succeeded!\n");
Hans Verkuil537fa492012-05-01 12:04:52 -0300353 return res | POLLIN | POLLRDNORM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
355
Joe Perches44d0b802011-08-21 19:56:44 -0300356 DEB_D("nothing to poll for, buf->state:%d\n", buf->state);
Hans Verkuil537fa492012-05-01 12:04:52 -0300357 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
360static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
361{
Hans Verkuil6694a562012-05-01 11:39:08 -0300362 struct video_device *vdev = video_devdata(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct saa7146_fh *fh = file->private_data;
364
Hans Verkuil6694a562012-05-01 11:39:08 -0300365 switch (vdev->vfl_type) {
366 case VFL_TYPE_GRABBER:
Joe Perches44d0b802011-08-21 19:56:44 -0300367/*
368 DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun",
369 file, data, (unsigned long)count);
370*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 return saa7146_video_uops.read(file,data,count,ppos);
Hans Verkuil6694a562012-05-01 11:39:08 -0300372 case VFL_TYPE_VBI:
Joe Perches44d0b802011-08-21 19:56:44 -0300373/*
374 DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n",
375 file, data, (unsigned long)count);
376*/
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200377 if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
378 return saa7146_vbi_uops.read(file,data,count,ppos);
Joe Perches44d0b802011-08-21 19:56:44 -0300379 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 default:
381 BUG();
382 return 0;
383 }
384}
385
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200386static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
387{
Hans Verkuil6694a562012-05-01 11:39:08 -0300388 struct video_device *vdev = video_devdata(file);
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200389 struct saa7146_fh *fh = file->private_data;
390
Hans Verkuil6694a562012-05-01 11:39:08 -0300391 switch (vdev->vfl_type) {
392 case VFL_TYPE_GRABBER:
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200393 return -EINVAL;
Hans Verkuil6694a562012-05-01 11:39:08 -0300394 case VFL_TYPE_VBI:
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200395 if (fh->dev->ext_vv_data->vbi_fops.write)
396 return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
397 else
398 return -EINVAL;
399 default:
400 BUG();
401 return -EINVAL;
402 }
403}
404
Hans Verkuilbec43662008-12-30 06:58:20 -0300405static const struct v4l2_file_operations video_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 .owner = THIS_MODULE,
408 .open = fops_open,
409 .release = fops_release,
410 .read = fops_read,
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200411 .write = fops_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 .poll = fops_poll,
413 .mmap = fops_mmap,
Hans Verkuil9af39712010-12-18 09:20:59 -0300414 .unlocked_ioctl = video_ioctl2,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415};
416
Adrian Bunk52c1da32005-06-23 22:05:33 -0700417static void vv_callback(struct saa7146_dev *dev, unsigned long status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
419 u32 isr = status;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800420
Joe Perches44d0b802011-08-21 19:56:44 -0300421 DEB_INT("dev:%p, isr:0x%08x\n", dev, (u32)status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
423 if (0 != (isr & (MASK_27))) {
Joe Perches44d0b802011-08-21 19:56:44 -0300424 DEB_INT("irq: RPS0 (0x%08x)\n", isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 saa7146_video_uops.irq_done(dev,isr);
426 }
427
428 if (0 != (isr & (MASK_28))) {
429 u32 mc2 = saa7146_read(dev, MC2);
430 if( 0 != (mc2 & MASK_15)) {
Joe Perches44d0b802011-08-21 19:56:44 -0300431 DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 wake_up(&dev->vv_data->vbi_wq);
433 saa7146_write(dev,MC2, MASK_31);
434 return;
435 }
Joe Perches44d0b802011-08-21 19:56:44 -0300436 DEB_INT("irq: RPS1 (0x%08x)\n", isr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 saa7146_vbi_uops.irq_done(dev,isr);
438 }
439}
440
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300441static const struct v4l2_ctrl_ops saa7146_ctrl_ops = {
442 .s_ctrl = saa7146_s_ctrl,
443};
444
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
446{
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300447 struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
Hans Verkuil5da545a2012-05-01 11:06:44 -0300448 struct v4l2_pix_format *fmt;
Hans Verkuilfca34692012-05-01 11:28:20 -0300449 struct v4l2_vbi_format *vbi;
Hans Verkuil230b65f2009-02-07 20:42:33 -0300450 struct saa7146_vv *vv;
Hans Verkuil03b19302010-03-24 19:09:55 -0300451 int err;
452
453 err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
454 if (err)
455 return err;
Hans Verkuil230b65f2009-02-07 20:42:33 -0300456
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300457 v4l2_ctrl_handler_init(hdl, 6);
458 v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
459 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
460 v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
461 V4L2_CID_CONTRAST, 0, 127, 1, 64);
462 v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
463 V4L2_CID_SATURATION, 0, 127, 1, 64);
464 v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
465 V4L2_CID_VFLIP, 0, 1, 1, 0);
466 v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
467 V4L2_CID_HFLIP, 0, 1, 1, 0);
468 if (hdl->error) {
469 err = hdl->error;
470 v4l2_ctrl_handler_free(hdl);
471 return err;
472 }
473 dev->v4l2_dev.ctrl_handler = hdl;
474
Hans Verkuil230b65f2009-02-07 20:42:33 -0300475 vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
Hans Verkuilb9600742009-01-18 19:59:11 -0300476 if (vv == NULL) {
Joe Perches44d0b802011-08-21 19:56:44 -0300477 ERR("out of memory. aborting.\n");
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300478 v4l2_ctrl_handler_free(hdl);
Hans Verkuil230b65f2009-02-07 20:42:33 -0300479 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Hans Verkuilb9600742009-01-18 19:59:11 -0300481 ext_vv->ops = saa7146_video_ioctl_ops;
482 ext_vv->core_ops = &saa7146_video_ioctl_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Joe Perches44d0b802011-08-21 19:56:44 -0300484 DEB_EE("dev:%p\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 /* set default values for video parts of the saa7146 */
487 saa7146_write(dev, BCS_CTRL, 0x80400040);
488
489 /* enable video-port pins */
490 saa7146_write(dev, MC1, (MASK_10 | MASK_26));
491
492 /* save per-device extension data (one extension can
493 handle different devices that might need different
494 configuration data) */
495 dev->ext_vv_data = ext_vv;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800496
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800497 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 -0700498 if( NULL == vv->d_clipping.cpu_addr ) {
Joe Perches44d0b802011-08-21 19:56:44 -0300499 ERR("out of memory. aborting.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 kfree(vv);
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300501 v4l2_ctrl_handler_free(hdl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 return -1;
503 }
504 memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
505
506 saa7146_video_uops.init(dev,vv);
Oliver Endriss5b0fa4f2006-01-09 18:21:37 -0200507 if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
508 saa7146_vbi_uops.init(dev,vv);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800509
Hans Verkuil5da545a2012-05-01 11:06:44 -0300510 fmt = &vv->ov_fb.fmt;
511 fmt->width = vv->standard->h_max_out;
512 fmt->height = vv->standard->v_max_out;
513 fmt->pixelformat = V4L2_PIX_FMT_RGB565;
514 fmt->bytesperline = 2 * fmt->width;
515 fmt->sizeimage = fmt->bytesperline * fmt->height;
516 fmt->colorspace = V4L2_COLORSPACE_SRGB;
Hans Verkuilfd74d6e2012-05-01 11:17:35 -0300517
518 fmt = &vv->video_fmt;
519 fmt->width = 384;
520 fmt->height = 288;
521 fmt->pixelformat = V4L2_PIX_FMT_BGR24;
522 fmt->field = V4L2_FIELD_ANY;
523 fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
524 fmt->bytesperline = 3 * fmt->width;
525 fmt->sizeimage = fmt->bytesperline * fmt->height;
526
Hans Verkuilfca34692012-05-01 11:28:20 -0300527 vbi = &vv->vbi_fmt;
528 vbi->sampling_rate = 27000000;
529 vbi->offset = 248; /* todo */
530 vbi->samples_per_line = 720 * 2;
531 vbi->sample_format = V4L2_PIX_FMT_GREY;
532
533 /* fixme: this only works for PAL */
534 vbi->start[0] = 5;
535 vbi->count[0] = 16;
536 vbi->start[1] = 312;
537 vbi->count[1] = 16;
538
539 init_timer(&vv->vbi_read_timeout);
540
Hans Verkuil5da545a2012-05-01 11:06:44 -0300541 vv->ov_fb.capability = V4L2_FBUF_CAP_LIST_CLIPPING;
542 vv->ov_fb.flags = V4L2_FBUF_FLAG_PRIMARY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 dev->vv_data = vv;
544 dev->vv_callback = &vv_callback;
545
546 return 0;
547}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300548EXPORT_SYMBOL_GPL(saa7146_vv_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550int saa7146_vv_release(struct saa7146_dev* dev)
551{
552 struct saa7146_vv *vv = dev->vv_data;
553
Joe Perches44d0b802011-08-21 19:56:44 -0300554 DEB_EE("dev:%p\n", dev);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800555
Hans Verkuil230b65f2009-02-07 20:42:33 -0300556 v4l2_device_unregister(&dev->v4l2_dev);
Marco Schluessler58af0042007-01-31 14:27:55 -0300557 pci_free_consistent(dev->pci, SAA7146_CLIPPING_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300558 v4l2_ctrl_handler_free(&dev->ctrl_handler);
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800559 kfree(vv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dev->vv_data = NULL;
561 dev->vv_callback = NULL;
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 return 0;
564}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300565EXPORT_SYMBOL_GPL(saa7146_vv_release);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
568 char *name, int type)
569{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 struct video_device *vfd;
Hans Verkuilb9600742009-01-18 19:59:11 -0300571 int err;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300572 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Joe Perches44d0b802011-08-21 19:56:44 -0300574 DEB_EE("dev:%p, name:'%s', type:%d\n", dev, name, type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 // released by vfd->release
Mauro Carvalho Chehabafd1a0c2005-12-12 00:37:27 -0800577 vfd = video_device_alloc();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 if (vfd == NULL)
579 return -ENOMEM;
580
Hans Verkuilb9600742009-01-18 19:59:11 -0300581 vfd->fops = &video_fops;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300582 vfd->ioctl_ops = &dev->ext_vv_data->ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 vfd->release = video_device_release;
Hans Verkuil5126f252012-05-10 04:57:22 -0300584 /* Locking in file operations other than ioctl should be done by
585 the driver, not the V4L2 core.
586 This driver needs auditing so that this flag can be removed. */
587 set_bit(V4L2_FL_LOCK_ALL_FOPS, &vfd->flags);
Hans Verkuil9af39712010-12-18 09:20:59 -0300588 vfd->lock = &dev->v4l2_lock;
Hans Verkuil6e65ca92012-04-29 16:47:47 -0300589 vfd->v4l2_dev = &dev->v4l2_dev;
Hans Verkuild0852ed2009-01-26 19:13:05 -0300590 vfd->tvnorms = 0;
Hans Verkuil537fa492012-05-01 12:04:52 -0300591 set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags);
Hans Verkuild0852ed2009-01-26 19:13:05 -0300592 for (i = 0; i < dev->ext_vv_data->num_stds; i++)
593 vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
Hans Verkuilb9600742009-01-18 19:59:11 -0300594 strlcpy(vfd->name, name, sizeof(vfd->name));
Hans Verkuil601e9442008-08-23 07:24:07 -0300595 video_set_drvdata(vfd, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Hans Verkuilb9600742009-01-18 19:59:11 -0300597 err = video_register_device(vfd, type, -1);
598 if (err < 0) {
Joe Perches44d0b802011-08-21 19:56:44 -0300599 ERR("cannot register v4l2 device. skipping.\n");
Julia Lawalla2a9b1e2008-01-11 22:03:42 -0300600 video_device_release(vfd);
Hans Verkuilb9600742009-01-18 19:59:11 -0300601 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 }
603
Joe Perches44d0b802011-08-21 19:56:44 -0300604 pr_info("%s: registered device %s [v4l2]\n",
605 dev->name, video_device_node_name(vfd));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
607 *vid = vfd;
608 return 0;
609}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300610EXPORT_SYMBOL_GPL(saa7146_register_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
612int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
613{
Joe Perches44d0b802011-08-21 19:56:44 -0300614 DEB_EE("dev:%p\n", dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 video_unregister_device(*vid);
617 *vid = NULL;
618
619 return 0;
620}
Adrian Bunk5d7dc8c2006-04-11 10:26:57 -0300621EXPORT_SYMBOL_GPL(saa7146_unregister_device);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623static int __init saa7146_vv_init_module(void)
624{
625 return 0;
626}
627
628
629static void __exit saa7146_vv_cleanup_module(void)
630{
631}
632
633module_init(saa7146_vv_init_module);
634module_exit(saa7146_vv_cleanup_module);
635
636MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
637MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
638MODULE_LICENSE("GPL");