Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

media: saa7146: convert to vb2

Convert this driver from the old videobuf framework to the vb2
frame.

Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab@kernel.org>

authored by

Hans Verkuil and committed by
Mauro Carvalho Chehab
0b6e30bd b3b2dd37

+350 -856
+1 -1
drivers/media/common/saa7146/Kconfig
··· 6 6 config VIDEO_SAA7146_VV 7 7 tristate 8 8 depends on VIDEO_DEV 9 - select VIDEOBUF_DMA_SG 9 + select VIDEOBUF2_DMA_SG 10 10 select VIDEO_SAA7146
+57 -261
drivers/media/common/saa7146/saa7146_fops.c
··· 43 43 44 44 45 45 /********************************************************************************/ 46 - /* common dma functions */ 47 - 48 - void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q, 49 - struct saa7146_buf *buf) 50 - { 51 - struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 52 - DEB_EE("dev:%p, buf:%p\n", dev, buf); 53 - 54 - videobuf_waiton(q, &buf->vb, 0, 0); 55 - videobuf_dma_unmap(q->dev, dma); 56 - videobuf_dma_free(dma); 57 - buf->vb.state = VIDEOBUF_NEEDS_INIT; 58 - } 59 - 60 - 61 - /********************************************************************************/ 62 46 /* common buffer functions */ 63 47 64 48 int saa7146_buffer_queue(struct saa7146_dev *dev, ··· 60 76 DEB_D("immediately activating buffer %p\n", buf); 61 77 buf->activate(dev,buf,NULL); 62 78 } else { 63 - list_add_tail(&buf->vb.queue,&q->queue); 64 - buf->vb.state = VIDEOBUF_QUEUED; 79 + list_add_tail(&buf->list, &q->queue); 65 80 DEB_D("adding buffer %p to queue. (active buffer present)\n", 66 81 buf); 67 82 } ··· 71 88 struct saa7146_dmaqueue *q, 72 89 int state) 73 90 { 91 + struct saa7146_vv *vv = dev->vv_data; 92 + struct saa7146_buf *buf = q->curr; 93 + 74 94 assert_spin_locked(&dev->slock); 75 95 DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev, q, state); 76 96 DEB_EE("q->curr:%p\n", q->curr); 77 97 78 98 /* finish current buffer */ 79 - if (NULL == q->curr) { 99 + if (!buf) { 80 100 DEB_D("aiii. no current buffer\n"); 81 101 return; 82 102 } 83 103 84 - q->curr->vb.state = state; 85 - q->curr->vb.ts = ktime_get_ns(); 86 - wake_up(&q->curr->vb.done); 87 - 88 104 q->curr = NULL; 105 + buf->vb.vb2_buf.timestamp = ktime_get_ns(); 106 + if (vv->video_fmt.field == V4L2_FIELD_ALTERNATE) 107 + buf->vb.field = vv->last_field; 108 + else if (vv->video_fmt.field == V4L2_FIELD_ANY) 109 + buf->vb.field = (vv->video_fmt.height > vv->standard->v_max_out / 2) 110 + ? V4L2_FIELD_INTERLACED 111 + : V4L2_FIELD_BOTTOM; 112 + else 113 + buf->vb.field = vv->video_fmt.field; 114 + buf->vb.sequence = vv->seqnr++; 115 + vb2_buffer_done(&buf->vb.vb2_buf, state); 89 116 } 90 117 91 118 void saa7146_buffer_next(struct saa7146_dev *dev, ··· 111 118 assert_spin_locked(&dev->slock); 112 119 if (!list_empty(&q->queue)) { 113 120 /* activate next one from queue */ 114 - buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue); 115 - list_del(&buf->vb.queue); 121 + buf = list_entry(q->queue.next, struct saa7146_buf, list); 122 + list_del(&buf->list); 116 123 if (!list_empty(&q->queue)) 117 - next = list_entry(q->queue.next,struct saa7146_buf, vb.queue); 124 + next = list_entry(q->queue.next, struct saa7146_buf, list); 118 125 q->curr = buf; 119 126 DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n", 120 127 buf, q->queue.prev, q->queue.next); ··· 162 169 spin_lock_irqsave(&dev->slock,flags); 163 170 if (q->curr) { 164 171 DEB_D("timeout on %p\n", q->curr); 165 - saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR); 172 + saa7146_buffer_finish(dev, q, VB2_BUF_STATE_ERROR); 166 173 } 167 174 168 175 /* we don't restart the transfer here like other drivers do. when ··· 171 178 we mess up our capture logic. if a timeout occurs on another buffer, 172 179 then something is seriously broken before, so no need to buffer the 173 180 next capture IMHO... */ 174 - /* 175 - saa7146_buffer_next(dev,q); 176 - */ 181 + 182 + saa7146_buffer_next(dev, q, 0); 183 + 177 184 spin_unlock_irqrestore(&dev->slock,flags); 178 185 } 179 186 180 187 /********************************************************************************/ 181 188 /* file operations */ 182 - 183 - static int fops_open(struct file *file) 184 - { 185 - struct video_device *vdev = video_devdata(file); 186 - struct saa7146_dev *dev = video_drvdata(file); 187 - struct saa7146_fh *fh = NULL; 188 - int result = 0; 189 - 190 - DEB_EE("file:%p, dev:%s\n", file, video_device_node_name(vdev)); 191 - 192 - if (mutex_lock_interruptible(vdev->lock)) 193 - return -ERESTARTSYS; 194 - 195 - DEB_D("using: %p\n", dev); 196 - 197 - /* check if an extension is registered */ 198 - if( NULL == dev->ext ) { 199 - DEB_S("no extension registered for this device\n"); 200 - result = -ENODEV; 201 - goto out; 202 - } 203 - 204 - /* allocate per open data */ 205 - fh = kzalloc(sizeof(*fh),GFP_KERNEL); 206 - if (NULL == fh) { 207 - DEB_S("cannot allocate memory for per open data\n"); 208 - result = -ENOMEM; 209 - goto out; 210 - } 211 - 212 - v4l2_fh_init(&fh->fh, vdev); 213 - 214 - file->private_data = &fh->fh; 215 - 216 - if (vdev->vfl_type == VFL_TYPE_VBI) { 217 - DEB_S("initializing vbi...\n"); 218 - if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) 219 - result = saa7146_vbi_uops.open(dev,file); 220 - if (dev->ext_vv_data->vbi_fops.open) 221 - dev->ext_vv_data->vbi_fops.open(file); 222 - } else { 223 - DEB_S("initializing video...\n"); 224 - result = saa7146_video_uops.open(dev,file); 225 - } 226 - 227 - if (0 != result) { 228 - goto out; 229 - } 230 - 231 - if( 0 == try_module_get(dev->ext->module)) { 232 - result = -EINVAL; 233 - goto out; 234 - } 235 - 236 - result = 0; 237 - v4l2_fh_add(&fh->fh); 238 - out: 239 - if (fh && result != 0) { 240 - kfree(fh); 241 - file->private_data = NULL; 242 - } 243 - mutex_unlock(vdev->lock); 244 - return result; 245 - } 246 - 247 - static int fops_release(struct file *file) 248 - { 249 - struct video_device *vdev = video_devdata(file); 250 - struct saa7146_dev *dev = video_drvdata(file); 251 - struct saa7146_fh *fh = file->private_data; 252 - 253 - DEB_EE("file:%p\n", file); 254 - 255 - mutex_lock(vdev->lock); 256 - 257 - if (vdev->vfl_type == VFL_TYPE_VBI) { 258 - if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) 259 - saa7146_vbi_uops.release(dev,file); 260 - if (dev->ext_vv_data->vbi_fops.release) 261 - dev->ext_vv_data->vbi_fops.release(file); 262 - } else { 263 - saa7146_video_uops.release(dev,file); 264 - } 265 - 266 - v4l2_fh_del(&fh->fh); 267 - v4l2_fh_exit(&fh->fh); 268 - module_put(dev->ext->module); 269 - file->private_data = NULL; 270 - kfree(fh); 271 - 272 - mutex_unlock(vdev->lock); 273 - 274 - return 0; 275 - } 276 - 277 - static int fops_mmap(struct file *file, struct vm_area_struct * vma) 278 - { 279 - struct video_device *vdev = video_devdata(file); 280 - struct saa7146_dev *dev = video_drvdata(file); 281 - struct saa7146_fh *fh = file->private_data; 282 - struct videobuf_queue *q; 283 - int res; 284 - 285 - switch (vdev->vfl_type) { 286 - case VFL_TYPE_VIDEO: { 287 - DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n", 288 - file, vma); 289 - q = &fh->video_q; 290 - break; 291 - } 292 - case VFL_TYPE_VBI: { 293 - DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n", 294 - file, vma); 295 - if (dev->ext_vv_data->capabilities & V4L2_CAP_SLICED_VBI_OUTPUT) 296 - return -ENODEV; 297 - q = &fh->vbi_q; 298 - break; 299 - } 300 - default: 301 - BUG(); 302 - } 303 - 304 - if (mutex_lock_interruptible(vdev->lock)) 305 - return -ERESTARTSYS; 306 - res = videobuf_mmap_mapper(q, vma); 307 - mutex_unlock(vdev->lock); 308 - return res; 309 - } 310 - 311 - static __poll_t __fops_poll(struct file *file, struct poll_table_struct *wait) 312 - { 313 - struct video_device *vdev = video_devdata(file); 314 - struct saa7146_dev *dev = video_drvdata(file); 315 - struct saa7146_fh *fh = file->private_data; 316 - struct videobuf_buffer *buf = NULL; 317 - struct videobuf_queue *q; 318 - __poll_t res = v4l2_ctrl_poll(file, wait); 319 - 320 - DEB_EE("file:%p, poll:%p\n", file, wait); 321 - 322 - if (vdev->vfl_type == VFL_TYPE_VBI) { 323 - if (dev->ext_vv_data->capabilities & V4L2_CAP_SLICED_VBI_OUTPUT) 324 - return res | EPOLLOUT | EPOLLWRNORM; 325 - if( 0 == fh->vbi_q.streaming ) 326 - return res | 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 res | EPOLLERR; 339 - } 340 - 341 - poll_wait(file, &buf->done, wait); 342 - if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) { 343 - DEB_D("poll succeeded!\n"); 344 - return res | EPOLLIN | EPOLLRDNORM; 345 - } 346 - 347 - DEB_D("nothing to poll for, buf->state:%d\n", buf->state); 348 - return res; 349 - } 350 - 351 - static __poll_t fops_poll(struct file *file, struct poll_table_struct *wait) 352 - { 353 - struct video_device *vdev = video_devdata(file); 354 - __poll_t res; 355 - 356 - mutex_lock(vdev->lock); 357 - res = __fops_poll(file, wait); 358 - mutex_unlock(vdev->lock); 359 - return res; 360 - } 361 - 362 - static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 363 - { 364 - struct video_device *vdev = video_devdata(file); 365 - struct saa7146_dev *dev = video_drvdata(file); 366 - int ret; 367 - 368 - switch (vdev->vfl_type) { 369 - case VFL_TYPE_VIDEO: 370 - /* 371 - DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun", 372 - file, data, (unsigned long)count); 373 - */ 374 - return saa7146_video_uops.read(file,data,count,ppos); 375 - case VFL_TYPE_VBI: 376 - /* 377 - DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n", 378 - file, data, (unsigned long)count); 379 - */ 380 - if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE) { 381 - if (mutex_lock_interruptible(vdev->lock)) 382 - return -ERESTARTSYS; 383 - ret = saa7146_vbi_uops.read(file, data, count, ppos); 384 - mutex_unlock(vdev->lock); 385 - return ret; 386 - } 387 - return -EINVAL; 388 - default: 389 - BUG(); 390 - } 391 - } 392 189 393 190 static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos) 394 191 { ··· 186 403 struct saa7146_dev *dev = video_drvdata(file); 187 404 int ret; 188 405 189 - switch (vdev->vfl_type) { 190 - case VFL_TYPE_VIDEO: 406 + if (vdev->vfl_type != VFL_TYPE_VBI || !dev->ext_vv_data->vbi_fops.write) 191 407 return -EINVAL; 192 - case VFL_TYPE_VBI: 193 - if (dev->ext_vv_data->vbi_fops.write) { 194 - if (mutex_lock_interruptible(vdev->lock)) 195 - return -ERESTARTSYS; 196 - ret = dev->ext_vv_data->vbi_fops.write(file, data, count, ppos); 197 - mutex_unlock(vdev->lock); 198 - return ret; 199 - } 200 - return -EINVAL; 201 - default: 202 - BUG(); 203 - } 408 + if (mutex_lock_interruptible(vdev->lock)) 409 + return -ERESTARTSYS; 410 + ret = dev->ext_vv_data->vbi_fops.write(file, data, count, ppos); 411 + mutex_unlock(vdev->lock); 412 + return ret; 204 413 } 205 414 206 415 static const struct v4l2_file_operations video_fops = 207 416 { 208 417 .owner = THIS_MODULE, 209 - .open = fops_open, 210 - .release = fops_release, 211 - .read = fops_read, 418 + .open = v4l2_fh_open, 419 + .release = vb2_fop_release, 420 + .read = vb2_fop_read, 212 421 .write = fops_write, 213 - .poll = fops_poll, 214 - .mmap = fops_mmap, 422 + .poll = vb2_fop_poll, 423 + .mmap = vb2_fop_mmap, 215 424 .unlocked_ioctl = video_ioctl2, 216 425 }; 217 426 ··· 343 568 int saa7146_register_device(struct video_device *vfd, struct saa7146_dev *dev, 344 569 char *name, int type) 345 570 { 571 + struct vb2_queue *q; 346 572 int err; 347 573 int i; 348 574 349 575 DEB_EE("dev:%p, name:'%s', type:%d\n", dev, name, type); 350 576 351 577 vfd->fops = &video_fops; 352 - if (type == VFL_TYPE_VIDEO) 578 + if (type == VFL_TYPE_VIDEO) { 353 579 vfd->ioctl_ops = &dev->ext_vv_data->vid_ops; 354 - else 580 + q = &dev->vv_data->video_dmaq.q; 581 + } else { 355 582 vfd->ioctl_ops = &dev->ext_vv_data->vbi_ops; 583 + q = &dev->vv_data->vbi_dmaq.q; 584 + } 356 585 vfd->release = video_device_release_empty; 357 586 vfd->lock = &dev->v4l2_lock; 358 587 vfd->v4l2_dev = &dev->v4l2_dev; ··· 377 598 } else { 378 599 vfd->device_caps &= ~V4L2_CAP_VIDEO_CAPTURE; 379 600 } 601 + 602 + q->type = type == VFL_TYPE_VIDEO ? V4L2_BUF_TYPE_VIDEO_CAPTURE : V4L2_BUF_TYPE_VBI_CAPTURE; 603 + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 604 + q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF; 605 + q->ops = type == VFL_TYPE_VIDEO ? &video_qops : &vbi_qops; 606 + q->mem_ops = &vb2_dma_sg_memops; 607 + q->drv_priv = dev; 608 + q->gfp_flags = __GFP_DMA32; 609 + q->buf_struct_size = sizeof(struct saa7146_buf); 610 + q->lock = &dev->v4l2_lock; 611 + q->min_buffers_needed = 2; 612 + q->dev = &dev->pci->dev; 613 + err = vb2_queue_init(q); 614 + if (err) 615 + return err; 616 + vfd->queue = q; 617 + 380 618 video_set_drvdata(vfd, dev); 381 619 382 620 err = video_register_device(vfd, type, -1);
+138 -186
drivers/media/common/saa7146/saa7146_vbi.c
··· 207 207 struct saa7146_buf *next) 208 208 { 209 209 struct saa7146_vv *vv = dev->vv_data; 210 - buf->vb.state = VIDEOBUF_ACTIVE; 211 210 212 211 DEB_VBI("dev:%p, buf:%p, next:%p\n", dev, buf, next); 213 212 saa7146_set_vbi_capture(dev,buf,next); ··· 215 216 return 0; 216 217 } 217 218 218 - static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,enum v4l2_field field) 219 - { 220 - struct file *file = q->priv_data; 221 - struct saa7146_dev *dev = video_drvdata(file); 222 - struct saa7146_buf *buf = (struct saa7146_buf *)vb; 223 - 224 - int err = 0; 225 - int lines, llength, size; 226 - 227 - lines = 16 * 2 ; /* 2 fields */ 228 - llength = vbi_pixel_to_capture; 229 - size = lines * llength; 230 - 231 - DEB_VBI("vb:%p\n", vb); 232 - 233 - if (0 != buf->vb.baddr && buf->vb.bsize < size) { 234 - DEB_VBI("size mismatch\n"); 235 - return -EINVAL; 236 - } 237 - 238 - if (buf->vb.size != size) 239 - saa7146_dma_free(dev,q,buf); 240 - 241 - if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 242 - struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 243 - 244 - buf->vb.width = llength; 245 - buf->vb.height = lines; 246 - buf->vb.size = size; 247 - buf->vb.field = field; // FIXME: check this 248 - 249 - saa7146_pgtable_free(dev->pci, &buf->pt[2]); 250 - saa7146_pgtable_alloc(dev->pci, &buf->pt[2]); 251 - 252 - err = videobuf_iolock(q,&buf->vb, NULL); 253 - if (err) 254 - goto oops; 255 - err = saa7146_pgtable_build_single(dev->pci, &buf->pt[2], 256 - dma->sglist, dma->sglen); 257 - if (0 != err) 258 - return err; 259 - } 260 - buf->vb.state = VIDEOBUF_PREPARED; 261 - buf->activate = buffer_activate; 262 - 263 - return 0; 264 - 265 - oops: 266 - DEB_VBI("error out\n"); 267 - saa7146_dma_free(dev,q,buf); 268 - 269 - return err; 270 - } 271 - 272 - static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 273 - { 274 - int llength,lines; 275 - 276 - lines = 16 * 2 ; /* 2 fields */ 277 - llength = vbi_pixel_to_capture; 278 - 279 - *size = lines * llength; 280 - *count = 2; 281 - 282 - DEB_VBI("count:%d, size:%d\n", *count, *size); 283 - 284 - return 0; 285 - } 286 - 287 - static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) 288 - { 289 - struct file *file = q->priv_data; 290 - struct saa7146_dev *dev = video_drvdata(file); 291 - struct saa7146_vv *vv = dev->vv_data; 292 - struct saa7146_buf *buf = (struct saa7146_buf *)vb; 293 - 294 - DEB_VBI("vb:%p\n", vb); 295 - saa7146_buffer_queue(dev, &vv->vbi_dmaq, buf); 296 - } 297 - 298 - static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 299 - { 300 - struct file *file = q->priv_data; 301 - struct saa7146_dev *dev = video_drvdata(file); 302 - struct saa7146_buf *buf = (struct saa7146_buf *)vb; 303 - 304 - DEB_VBI("vb:%p\n", vb); 305 - saa7146_dma_free(dev,q,buf); 306 - } 307 - 308 - static const struct videobuf_queue_ops vbi_qops = { 309 - .buf_setup = buffer_setup, 310 - .buf_prepare = buffer_prepare, 311 - .buf_queue = buffer_queue, 312 - .buf_release = buffer_release, 313 - }; 314 - 315 219 /* ------------------------------------------------------------------ */ 316 220 317 - static void vbi_stop(struct saa7146_fh *fh, struct file *file) 221 + static int queue_setup(struct vb2_queue *q, 222 + unsigned int *num_buffers, unsigned int *num_planes, 223 + unsigned int sizes[], struct device *alloc_devs[]) 318 224 { 319 - struct saa7146_dev *dev = video_drvdata(file); 225 + unsigned int size = 16 * 2 * vbi_pixel_to_capture; 226 + 227 + if (*num_planes) 228 + return sizes[0] < size ? -EINVAL : 0; 229 + *num_planes = 1; 230 + sizes[0] = size; 231 + 232 + return 0; 233 + } 234 + 235 + static void buf_queue(struct vb2_buffer *vb) 236 + { 237 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 238 + struct vb2_queue *vq = vb->vb2_queue; 239 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 240 + struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb); 241 + unsigned long flags; 242 + 243 + spin_lock_irqsave(&dev->slock, flags); 244 + 245 + saa7146_buffer_queue(dev, &dev->vv_data->vbi_dmaq, buf); 246 + spin_unlock_irqrestore(&dev->slock, flags); 247 + } 248 + 249 + static int buf_init(struct vb2_buffer *vb) 250 + { 251 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 252 + struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb); 253 + struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0); 254 + struct scatterlist *list = sgt->sgl; 255 + int length = sgt->nents; 256 + struct vb2_queue *vq = vb->vb2_queue; 257 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 258 + int ret; 259 + 260 + buf->activate = buffer_activate; 261 + 262 + saa7146_pgtable_alloc(dev->pci, &buf->pt[2]); 263 + 264 + ret = saa7146_pgtable_build_single(dev->pci, &buf->pt[2], 265 + list, length); 266 + if (ret) 267 + saa7146_pgtable_free(dev->pci, &buf->pt[2]); 268 + return ret; 269 + } 270 + 271 + static int buf_prepare(struct vb2_buffer *vb) 272 + { 273 + unsigned int size = 16 * 2 * vbi_pixel_to_capture; 274 + 275 + if (vb2_plane_size(vb, 0) < size) 276 + return -EINVAL; 277 + vb2_set_plane_payload(vb, 0, size); 278 + return 0; 279 + } 280 + 281 + static void buf_cleanup(struct vb2_buffer *vb) 282 + { 283 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 284 + struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb); 285 + struct vb2_queue *vq = vb->vb2_queue; 286 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 287 + 288 + saa7146_pgtable_free(dev->pci, &buf->pt[2]); 289 + } 290 + 291 + static void return_buffers(struct vb2_queue *q, int state) 292 + { 293 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 294 + struct saa7146_dmaqueue *dq = &dev->vv_data->vbi_dmaq; 295 + struct saa7146_buf *buf; 296 + 297 + if (dq->curr) { 298 + buf = dq->curr; 299 + dq->curr = NULL; 300 + vb2_buffer_done(&buf->vb.vb2_buf, state); 301 + } 302 + while (!list_empty(&dq->queue)) { 303 + buf = list_entry(dq->queue.next, struct saa7146_buf, list); 304 + list_del(&buf->list); 305 + vb2_buffer_done(&buf->vb.vb2_buf, state); 306 + } 307 + } 308 + 309 + static void vbi_stop(struct saa7146_dev *dev) 310 + { 320 311 struct saa7146_vv *vv = dev->vv_data; 321 312 unsigned long flags; 322 - DEB_VBI("dev:%p, fh:%p\n", dev, fh); 313 + DEB_VBI("dev:%p\n", dev); 323 314 324 315 spin_lock_irqsave(&dev->slock,flags); 325 316 ··· 322 333 /* shut down dma 3 transfers */ 323 334 saa7146_write(dev, MC1, MASK_20); 324 335 325 - if (vv->vbi_dmaq.curr) 326 - saa7146_buffer_finish(dev, &vv->vbi_dmaq, VIDEOBUF_DONE); 327 - 328 - videobuf_queue_cancel(&fh->vbi_q); 329 - 330 - vv->vbi_streaming = NULL; 331 - 332 336 del_timer(&vv->vbi_dmaq.timeout); 333 337 del_timer(&vv->vbi_read_timeout); 334 338 ··· 331 349 static void vbi_read_timeout(struct timer_list *t) 332 350 { 333 351 struct saa7146_vv *vv = from_timer(vv, t, vbi_read_timeout); 334 - struct file *file = vv->vbi_read_timeout_file; 335 - struct saa7146_dev *dev = video_drvdata(file); 336 - struct saa7146_fh *fh = file->private_data; 352 + struct saa7146_dev *dev = vv->vbi_dmaq.dev; 337 353 338 - DEB_VBI("dev:%p, fh:%p\n", dev, fh); 339 - 340 - vbi_stop(fh, file); 341 - } 342 - 343 - static void vbi_init(struct saa7146_dev *dev, struct saa7146_vv *vv) 344 - { 345 354 DEB_VBI("dev:%p\n", dev); 346 355 347 - INIT_LIST_HEAD(&vv->vbi_dmaq.queue); 348 - 349 - timer_setup(&vv->vbi_dmaq.timeout, saa7146_buffer_timeout, 0); 350 - vv->vbi_dmaq.dev = dev; 351 - 352 - init_waitqueue_head(&vv->vbi_wq); 356 + vbi_stop(dev); 353 357 } 354 358 355 - static int vbi_open(struct saa7146_dev *dev, struct file *file) 359 + static int vbi_begin(struct saa7146_dev *dev) 356 360 { 357 - struct saa7146_fh *fh = file->private_data; 358 361 struct saa7146_vv *vv = dev->vv_data; 359 - 360 362 u32 arbtr_ctrl = saa7146_read(dev, PCI_BT_V1); 361 363 int ret = 0; 362 364 363 - DEB_VBI("dev:%p, fh:%p\n", dev, fh); 365 + DEB_VBI("dev:%p\n", dev); 364 366 365 367 ret = saa7146_res_get(dev, RESOURCE_DMA3_BRS); 366 368 if (0 == ret) { ··· 358 392 saa7146_write(dev, PCI_BT_V1, arbtr_ctrl); 359 393 saa7146_write(dev, MC2, (MASK_04|MASK_20)); 360 394 361 - videobuf_queue_sg_init(&fh->vbi_q, &vbi_qops, 362 - &dev->pci->dev, &dev->slock, 363 - V4L2_BUF_TYPE_VBI_CAPTURE, 364 - V4L2_FIELD_SEQ_TB, // FIXME: does this really work? 365 - sizeof(struct saa7146_buf), 366 - file, &dev->v4l2_lock); 367 - 368 395 vv->vbi_read_timeout.function = vbi_read_timeout; 369 - vv->vbi_read_timeout_file = file; 370 396 371 397 /* initialize the brs */ 372 398 if ( 0 != (SAA7146_USE_PORT_B_FOR_VBI & dev->ext_vv_data->flags)) { ··· 377 419 return 0; 378 420 } 379 421 380 - static void vbi_close(struct saa7146_dev *dev, struct file *file) 422 + static int start_streaming(struct vb2_queue *q, unsigned int count) 381 423 { 382 - struct saa7146_fh *fh = file->private_data; 383 - struct saa7146_vv *vv = dev->vv_data; 384 - DEB_VBI("dev:%p, fh:%p\n", dev, fh); 424 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 425 + int ret; 385 426 386 - if( fh == vv->vbi_streaming ) { 387 - vbi_stop(fh, file); 388 - } 427 + if (!vb2_is_streaming(&dev->vv_data->vbi_dmaq.q)) 428 + dev->vv_data->seqnr = 0; 429 + ret = vbi_begin(dev); 430 + if (ret) 431 + return_buffers(q, VB2_BUF_STATE_QUEUED); 432 + return ret; 433 + } 434 + 435 + static void stop_streaming(struct vb2_queue *q) 436 + { 437 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 438 + 439 + vbi_stop(dev); 440 + return_buffers(q, VB2_BUF_STATE_ERROR); 389 441 saa7146_res_free(dev, RESOURCE_DMA3_BRS); 442 + } 443 + 444 + const struct vb2_ops vbi_qops = { 445 + .queue_setup = queue_setup, 446 + .buf_queue = buf_queue, 447 + .buf_init = buf_init, 448 + .buf_prepare = buf_prepare, 449 + .buf_cleanup = buf_cleanup, 450 + .start_streaming = start_streaming, 451 + .stop_streaming = stop_streaming, 452 + .wait_prepare = vb2_ops_wait_prepare, 453 + .wait_finish = vb2_ops_wait_finish, 454 + }; 455 + 456 + /* ------------------------------------------------------------------ */ 457 + 458 + static void vbi_init(struct saa7146_dev *dev, struct saa7146_vv *vv) 459 + { 460 + DEB_VBI("dev:%p\n", dev); 461 + 462 + INIT_LIST_HEAD(&vv->vbi_dmaq.queue); 463 + 464 + timer_setup(&vv->vbi_dmaq.timeout, saa7146_buffer_timeout, 0); 465 + vv->vbi_dmaq.dev = dev; 466 + 467 + init_waitqueue_head(&vv->vbi_wq); 390 468 } 391 469 392 470 static void vbi_irq_done(struct saa7146_dev *dev, unsigned long status) ··· 432 438 433 439 if (vv->vbi_dmaq.curr) { 434 440 DEB_VBI("dev:%p, curr:%p\n", dev, vv->vbi_dmaq.curr); 435 - /* this must be += 2, one count for each field */ 436 - vv->vbi_fieldcount+=2; 437 - vv->vbi_dmaq.curr->vb.field_count = vv->vbi_fieldcount; 438 - saa7146_buffer_finish(dev, &vv->vbi_dmaq, VIDEOBUF_DONE); 441 + saa7146_buffer_finish(dev, &vv->vbi_dmaq, VB2_BUF_STATE_DONE); 439 442 } else { 440 443 DEB_VBI("dev:%p\n", dev); 441 444 } ··· 441 450 spin_unlock(&dev->slock); 442 451 } 443 452 444 - static ssize_t vbi_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 445 - { 446 - struct saa7146_fh *fh = file->private_data; 447 - struct saa7146_dev *dev = video_drvdata(file); 448 - struct saa7146_vv *vv = dev->vv_data; 449 - ssize_t ret = 0; 450 - 451 - DEB_VBI("dev:%p, fh:%p\n", dev, fh); 452 - 453 - if( NULL == vv->vbi_streaming ) { 454 - // fixme: check if dma3 is available 455 - // fixme: activate vbi engine here if necessary. (really?) 456 - vv->vbi_streaming = fh; 457 - } 458 - 459 - if( fh != vv->vbi_streaming ) { 460 - DEB_VBI("open %p is already using vbi capture\n", 461 - vv->vbi_streaming); 462 - return -EBUSY; 463 - } 464 - 465 - mod_timer(&vv->vbi_read_timeout, jiffies+BUFFER_TIMEOUT); 466 - ret = videobuf_read_stream(&fh->vbi_q, data, count, ppos, 1, 467 - file->f_flags & O_NONBLOCK); 468 - /* 469 - printk("BASE_ODD3: 0x%08x\n", saa7146_read(dev, BASE_ODD3)); 470 - printk("BASE_EVEN3: 0x%08x\n", saa7146_read(dev, BASE_EVEN3)); 471 - printk("PROT_ADDR3: 0x%08x\n", saa7146_read(dev, PROT_ADDR3)); 472 - printk("PITCH3: 0x%08x\n", saa7146_read(dev, PITCH3)); 473 - printk("BASE_PAGE3: 0x%08x\n", saa7146_read(dev, BASE_PAGE3)); 474 - printk("NUM_LINE_BYTE3: 0x%08x\n", saa7146_read(dev, NUM_LINE_BYTE3)); 475 - printk("BRS_CTRL: 0x%08x\n", saa7146_read(dev, BRS_CTRL)); 476 - */ 477 - return ret; 478 - } 479 - 480 453 const struct saa7146_use_ops saa7146_vbi_uops = { 481 454 .init = vbi_init, 482 - .open = vbi_open, 483 - .release = vbi_close, 484 455 .irq_done = vbi_irq_done, 485 - .read = vbi_read, 486 456 };
+146 -370
drivers/media/common/saa7146/saa7146_video.c
··· 6 6 #include <linux/module.h> 7 7 #include <linux/kernel.h> 8 8 9 - static int max_memory = 32; 10 - 11 - module_param(max_memory, int, 0644); 12 - MODULE_PARM_DESC(max_memory, "maximum memory usage for capture buffers (default: 32Mb)"); 13 - 14 - #define IS_CAPTURE_ACTIVE(fh) \ 15 - (((vv->video_status & STATUS_CAPTURE) != 0) && (vv->video_fh == fh)) 16 - 17 9 /* format descriptions for capture and preview */ 18 10 static struct saa7146_format formats[] = { 19 11 { ··· 87 95 { 88 96 struct saa7146_vv *vv = dev->vv_data; 89 97 struct pci_dev *pci = dev->pci; 90 - struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb); 91 - struct scatterlist *list = dma->sglist; 92 - int length = dma->sglen; 98 + struct sg_table *sgt = vb2_dma_sg_plane_desc(&buf->vb.vb2_buf, 0); 99 + struct scatterlist *list = sgt->sgl; 100 + int length = sgt->nents; 93 101 struct v4l2_pix_format *pix = &vv->video_fmt; 94 102 struct saa7146_format *sfmt = saa7146_format_by_fourcc(dev, pix->pixelformat); 95 103 ··· 143 151 144 152 /* if we have a user buffer, the first page may not be 145 153 aligned to a page boundary. */ 146 - pt1->offset = dma->sglist->offset; 154 + pt1->offset = sgt->sgl->offset; 147 155 pt2->offset = pt1->offset + o1; 148 156 pt3->offset = pt1->offset + o2; 149 157 ··· 179 187 /********************************************************************************/ 180 188 /* file operations */ 181 189 182 - static int video_begin(struct saa7146_dev *dev, struct saa7146_fh *fh) 190 + static int video_begin(struct saa7146_dev *dev) 183 191 { 184 192 struct saa7146_vv *vv = dev->vv_data; 185 193 struct saa7146_format *fmt = NULL; 186 194 unsigned int resource; 187 195 int ret = 0; 188 196 189 - DEB_EE("dev:%p, fh:%p\n", dev, fh); 190 - 191 - if ((vv->video_status & STATUS_CAPTURE) != 0) { 192 - if (vv->video_fh == fh) { 193 - DEB_S("already capturing\n"); 194 - return 0; 195 - } 196 - DEB_S("already capturing in another open\n"); 197 - return -EBUSY; 198 - } 197 + DEB_EE("dev:%p\n", dev); 199 198 200 199 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat); 201 200 /* we need to have a valid format set here */ ··· 211 228 /* enable rps0 irqs */ 212 229 SAA7146_IER_ENABLE(dev, MASK_27); 213 230 214 - vv->video_fh = fh; 215 - vv->video_status = STATUS_CAPTURE; 216 - 217 231 return 0; 218 232 } 219 233 220 - static int video_end(struct saa7146_dev *dev, struct saa7146_fh *fh) 234 + static void video_end(struct saa7146_dev *dev) 221 235 { 222 236 struct saa7146_vv *vv = dev->vv_data; 223 - struct saa7146_dmaqueue *q = &vv->video_dmaq; 224 237 struct saa7146_format *fmt = NULL; 225 238 unsigned long flags; 226 239 unsigned int resource; 227 240 u32 dmas = 0; 228 - DEB_EE("dev:%p, fh:%p\n", dev, fh); 229 - 230 - if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) { 231 - DEB_S("not capturing\n"); 232 - return 0; 233 - } 234 - 235 - if (vv->video_fh != fh) { 236 - DEB_S("capturing, but in another open\n"); 237 - return -EBUSY; 238 - } 241 + DEB_EE("dev:%p\n", dev); 239 242 240 243 fmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat); 241 244 /* we need to have a valid format set here */ 242 245 if (!fmt) 243 - return -EINVAL; 246 + return; 244 247 245 248 if (0 != (fmt->flags & FORMAT_IS_PLANAR)) { 246 249 resource = RESOURCE_DMA1_HPS|RESOURCE_DMA2_CLP|RESOURCE_DMA3_BRS; ··· 246 277 /* shut down all used video dma transfers */ 247 278 saa7146_write(dev, MC1, dmas); 248 279 249 - if (q->curr) 250 - saa7146_buffer_finish(dev, q, VIDEOBUF_DONE); 251 - 252 280 spin_unlock_irqrestore(&dev->slock, flags); 253 281 254 - vv->video_fh = NULL; 255 - vv->video_status = 0; 256 - 257 282 saa7146_res_free(dev, resource); 258 - 259 - return 0; 260 283 } 261 284 262 285 static int vidioc_querycap(struct file *file, void *fh, struct v4l2_capability *cap) ··· 306 345 307 346 case V4L2_CID_HFLIP: 308 347 /* fixme: we can support changing VFLIP and HFLIP here... */ 309 - if ((vv->video_status & STATUS_CAPTURE)) 348 + if (vb2_is_busy(&vv->video_dmaq.q)) 310 349 return -EBUSY; 311 350 vv->hflip = ctrl->val; 312 351 break; 313 352 314 353 case V4L2_CID_VFLIP: 315 - if ((vv->video_status & STATUS_CAPTURE)) 354 + if (vb2_is_busy(&vv->video_dmaq.q)) 316 355 return -EBUSY; 317 356 vv->vflip = ctrl->val; 318 357 break; ··· 420 459 return 0; 421 460 } 422 461 423 - static int vidioc_s_fmt_vid_cap(struct file *file, void *__fh, struct v4l2_format *f) 462 + static int vidioc_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *f) 424 463 { 425 464 struct saa7146_dev *dev = video_drvdata(file); 426 - struct saa7146_fh *fh = __fh; 427 465 struct saa7146_vv *vv = dev->vv_data; 428 466 int err; 429 467 430 - DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p, fh:%p\n", dev, fh); 431 - if (IS_CAPTURE_ACTIVE(fh) != 0) { 468 + DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: dev:%p\n", dev); 469 + if (vb2_is_busy(&vv->video_dmaq.q)) { 432 470 DEB_EE("streaming capture is active\n"); 433 471 return -EBUSY; 434 472 } ··· 449 489 return 0; 450 490 } 451 491 452 - /* the saa7146 supfhrts (used in conjunction with the saa7111a for example) 453 - PAL / NTSC / SECAM. if your hardware does not (or does more) 454 - -- override this function in your extension */ 455 - /* 456 - case VIDIOC_ENUMSTD: 457 - { 458 - struct v4l2_standard *e = arg; 459 - if (e->index < 0 ) 460 - return -EINVAL; 461 - if( e->index < dev->ext_vv_data->num_stds ) { 462 - DEB_EE("VIDIOC_ENUMSTD: index:%d\n", e->index); 463 - v4l2_video_std_construct(e, dev->ext_vv_data->stds[e->index].id, dev->ext_vv_data->stds[e->index].name); 464 - return 0; 465 - } 466 - return -EINVAL; 467 - } 468 - */ 469 - 470 492 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id id) 471 493 { 472 494 struct saa7146_dev *dev = video_drvdata(file); ··· 458 516 459 517 DEB_EE("VIDIOC_S_STD\n"); 460 518 461 - if ((vv->video_status & STATUS_CAPTURE) == STATUS_CAPTURE) { 519 + if (vb2_is_busy(&vv->video_dmaq.q) || vb2_is_busy(&vv->vbi_dmaq.q)) { 462 520 DEB_D("cannot change video standard while streaming capture is active\n"); 463 521 return -EBUSY; 464 522 } ··· 482 540 return 0; 483 541 } 484 542 485 - static int vidioc_reqbufs(struct file *file, void *__fh, struct v4l2_requestbuffers *b) 486 - { 487 - struct saa7146_fh *fh = __fh; 488 - 489 - if (b->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 490 - return videobuf_reqbufs(&fh->video_q, b); 491 - if (b->type == V4L2_BUF_TYPE_VBI_CAPTURE) 492 - return videobuf_reqbufs(&fh->vbi_q, b); 493 - return -EINVAL; 494 - } 495 - 496 - static int vidioc_querybuf(struct file *file, void *__fh, struct v4l2_buffer *buf) 497 - { 498 - struct saa7146_fh *fh = __fh; 499 - 500 - if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 501 - return videobuf_querybuf(&fh->video_q, buf); 502 - if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE) 503 - return videobuf_querybuf(&fh->vbi_q, buf); 504 - return -EINVAL; 505 - } 506 - 507 - static int vidioc_qbuf(struct file *file, void *__fh, struct v4l2_buffer *buf) 508 - { 509 - struct saa7146_fh *fh = __fh; 510 - 511 - if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 512 - return videobuf_qbuf(&fh->video_q, buf); 513 - if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE) 514 - return videobuf_qbuf(&fh->vbi_q, buf); 515 - return -EINVAL; 516 - } 517 - 518 - static int vidioc_dqbuf(struct file *file, void *__fh, struct v4l2_buffer *buf) 519 - { 520 - struct saa7146_fh *fh = __fh; 521 - 522 - if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 523 - return videobuf_dqbuf(&fh->video_q, buf, file->f_flags & O_NONBLOCK); 524 - if (buf->type == V4L2_BUF_TYPE_VBI_CAPTURE) 525 - return videobuf_dqbuf(&fh->vbi_q, buf, file->f_flags & O_NONBLOCK); 526 - return -EINVAL; 527 - } 528 - 529 - static int vidioc_streamon(struct file *file, void *__fh, enum v4l2_buf_type type) 530 - { 531 - struct saa7146_dev *dev = video_drvdata(file); 532 - struct saa7146_fh *fh = __fh; 533 - int err; 534 - 535 - DEB_D("VIDIOC_STREAMON, type:%d\n", type); 536 - 537 - err = video_begin(dev, fh); 538 - if (err) 539 - return err; 540 - if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 541 - return videobuf_streamon(&fh->video_q); 542 - if (type == V4L2_BUF_TYPE_VBI_CAPTURE) 543 - return videobuf_streamon(&fh->vbi_q); 544 - return -EINVAL; 545 - } 546 - 547 - static int vidioc_streamoff(struct file *file, void *__fh, enum v4l2_buf_type type) 548 - { 549 - struct saa7146_dev *dev = video_drvdata(file); 550 - struct saa7146_fh *fh = __fh; 551 - struct saa7146_vv *vv = dev->vv_data; 552 - int err; 553 - 554 - DEB_D("VIDIOC_STREAMOFF, type:%d\n", type); 555 - 556 - /* ugly: we need to copy some checks from video_end(), 557 - because videobuf_streamoff() relies on the capture running. 558 - check and fix this */ 559 - if ((vv->video_status & STATUS_CAPTURE) != STATUS_CAPTURE) { 560 - DEB_S("not capturing\n"); 561 - return 0; 562 - } 563 - 564 - if (vv->video_fh != fh) { 565 - DEB_S("capturing, but in another open\n"); 566 - return -EBUSY; 567 - } 568 - 569 - err = -EINVAL; 570 - if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) 571 - err = videobuf_streamoff(&fh->video_q); 572 - else if (type == V4L2_BUF_TYPE_VBI_CAPTURE) 573 - err = videobuf_streamoff(&fh->vbi_q); 574 - if (0 != err) { 575 - DEB_D("warning: videobuf_streamoff() failed\n"); 576 - video_end(dev, fh); 577 - } else { 578 - err = video_end(dev, fh); 579 - } 580 - return err; 581 - } 582 - 583 543 const struct v4l2_ioctl_ops saa7146_video_ioctl_ops = { 584 544 .vidioc_querycap = vidioc_querycap, 585 545 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, 586 546 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, 587 547 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, 588 548 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, 589 - 590 - .vidioc_reqbufs = vidioc_reqbufs, 591 - .vidioc_querybuf = vidioc_querybuf, 592 - .vidioc_qbuf = vidioc_qbuf, 593 - .vidioc_dqbuf = vidioc_dqbuf, 594 549 .vidioc_g_std = vidioc_g_std, 595 550 .vidioc_s_std = vidioc_s_std, 596 - .vidioc_streamon = vidioc_streamon, 597 - .vidioc_streamoff = vidioc_streamoff, 598 551 .vidioc_g_parm = vidioc_g_parm, 552 + .vidioc_reqbufs = vb2_ioctl_reqbufs, 553 + .vidioc_create_bufs = vb2_ioctl_create_bufs, 554 + .vidioc_querybuf = vb2_ioctl_querybuf, 555 + .vidioc_qbuf = vb2_ioctl_qbuf, 556 + .vidioc_dqbuf = vb2_ioctl_dqbuf, 557 + .vidioc_streamon = vb2_ioctl_streamon, 558 + .vidioc_streamoff = vb2_ioctl_streamoff, 599 559 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 600 560 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 601 561 }; ··· 505 661 const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops = { 506 662 .vidioc_querycap = vidioc_querycap, 507 663 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 508 - 509 - .vidioc_reqbufs = vidioc_reqbufs, 510 - .vidioc_querybuf = vidioc_querybuf, 511 - .vidioc_qbuf = vidioc_qbuf, 512 - .vidioc_dqbuf = vidioc_dqbuf, 664 + .vidioc_s_fmt_vbi_cap = vidioc_g_fmt_vbi_cap, 513 665 .vidioc_g_std = vidioc_g_std, 514 666 .vidioc_s_std = vidioc_s_std, 515 - .vidioc_streamon = vidioc_streamon, 516 - .vidioc_streamoff = vidioc_streamoff, 517 667 .vidioc_g_parm = vidioc_g_parm, 668 + .vidioc_reqbufs = vb2_ioctl_reqbufs, 669 + .vidioc_create_bufs = vb2_ioctl_create_bufs, 670 + .vidioc_querybuf = vb2_ioctl_querybuf, 671 + .vidioc_qbuf = vb2_ioctl_qbuf, 672 + .vidioc_dqbuf = vb2_ioctl_dqbuf, 673 + .vidioc_streamon = vb2_ioctl_streamon, 674 + .vidioc_streamoff = vb2_ioctl_streamoff, 518 675 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 519 676 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 520 677 }; ··· 529 684 { 530 685 struct saa7146_vv *vv = dev->vv_data; 531 686 532 - buf->vb.state = VIDEOBUF_ACTIVE; 533 687 saa7146_set_capture(dev,buf,next); 534 688 535 689 mod_timer(&vv->video_dmaq.timeout, jiffies+BUFFER_TIMEOUT); ··· 542 698 saa7146_pgtable_free(dev->pci, &buf->pt[2]); 543 699 } 544 700 545 - static int buffer_prepare(struct videobuf_queue *q, 546 - struct videobuf_buffer *vb, enum v4l2_field field) 701 + static int queue_setup(struct vb2_queue *q, 702 + unsigned int *num_buffers, unsigned int *num_planes, 703 + unsigned int sizes[], struct device *alloc_devs[]) 547 704 { 548 - struct file *file = q->priv_data; 549 - struct saa7146_dev *dev = video_drvdata(file); 705 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 706 + unsigned int size = dev->vv_data->video_fmt.sizeimage; 707 + 708 + if (*num_planes) 709 + return sizes[0] < size ? -EINVAL : 0; 710 + *num_planes = 1; 711 + sizes[0] = size; 712 + 713 + return 0; 714 + } 715 + 716 + static void buf_queue(struct vb2_buffer *vb) 717 + { 718 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 719 + struct vb2_queue *vq = vb->vb2_queue; 720 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 721 + struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb); 722 + unsigned long flags; 723 + 724 + spin_lock_irqsave(&dev->slock, flags); 725 + 726 + saa7146_buffer_queue(dev, &dev->vv_data->video_dmaq, buf); 727 + spin_unlock_irqrestore(&dev->slock, flags); 728 + } 729 + 730 + static int buf_init(struct vb2_buffer *vb) 731 + { 732 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 733 + struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb); 734 + struct vb2_queue *vq = vb->vb2_queue; 735 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 550 736 struct saa7146_vv *vv = dev->vv_data; 551 - struct saa7146_buf *buf = (struct saa7146_buf *)vb; 552 - int size,err = 0; 737 + struct saa7146_format *sfmt; 738 + int ret; 553 739 554 - DEB_CAP("vbuf:%p\n", vb); 555 - 556 - /* sanity checks */ 557 - if (vv->video_fmt.width < 48 || 558 - vv->video_fmt.height < 32 || 559 - vv->video_fmt.width > vv->standard->h_max_out || 560 - vv->video_fmt.height > vv->standard->v_max_out) { 561 - DEB_D("w (%d) / h (%d) out of bounds\n", 562 - vv->video_fmt.width, vv->video_fmt.height); 563 - return -EINVAL; 564 - } 565 - 566 - size = vv->video_fmt.sizeimage; 567 - if (0 != buf->vb.baddr && buf->vb.bsize < size) { 568 - DEB_D("size mismatch\n"); 569 - return -EINVAL; 570 - } 571 - 572 - DEB_CAP("buffer_prepare [size=%dx%d,bytes=%d,fields=%s]\n", 573 - vv->video_fmt.width, vv->video_fmt.height, 574 - size, v4l2_field_names[vv->video_fmt.field]); 575 - if (buf->vb.width != vv->video_fmt.width || 576 - buf->vb.bytesperline != vv->video_fmt.bytesperline || 577 - buf->vb.height != vv->video_fmt.height || 578 - buf->vb.size != size || 579 - buf->vb.field != field || 580 - buf->vb.field != vv->video_fmt.field) { 581 - saa7146_dma_free(dev,q,buf); 582 - } 583 - 584 - if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 585 - struct saa7146_format *sfmt; 586 - 587 - buf->vb.bytesperline = vv->video_fmt.bytesperline; 588 - buf->vb.width = vv->video_fmt.width; 589 - buf->vb.height = vv->video_fmt.height; 590 - buf->vb.size = size; 591 - buf->vb.field = field; 592 - buf->vb.field = vv->video_fmt.field; 593 - 594 - sfmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat); 595 - 596 - release_all_pagetables(dev, buf); 597 - if( 0 != IS_PLANAR(sfmt->trans)) { 598 - saa7146_pgtable_alloc(dev->pci, &buf->pt[0]); 599 - saa7146_pgtable_alloc(dev->pci, &buf->pt[1]); 600 - saa7146_pgtable_alloc(dev->pci, &buf->pt[2]); 601 - } else { 602 - saa7146_pgtable_alloc(dev->pci, &buf->pt[0]); 603 - } 604 - 605 - err = videobuf_iolock(q, &buf->vb, NULL); 606 - if (err) 607 - goto oops; 608 - err = saa7146_pgtable_build(dev,buf); 609 - if (err) 610 - goto oops; 611 - } 612 - buf->vb.state = VIDEOBUF_PREPARED; 613 740 buf->activate = buffer_activate; 741 + sfmt = saa7146_format_by_fourcc(dev, vv->video_fmt.pixelformat); 614 742 615 - return 0; 616 - 617 - oops: 618 - DEB_D("error out\n"); 619 - saa7146_dma_free(dev,q,buf); 620 - 621 - return err; 622 - } 623 - 624 - static int buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 625 - { 626 - struct file *file = q->priv_data; 627 - struct saa7146_dev *dev = video_drvdata(file); 628 - struct saa7146_vv *vv = dev->vv_data; 629 - 630 - if (0 == *count || *count > MAX_SAA7146_CAPTURE_BUFFERS) 631 - *count = MAX_SAA7146_CAPTURE_BUFFERS; 632 - 633 - *size = vv->video_fmt.sizeimage; 634 - 635 - /* check if we exceed the "max_memory" parameter */ 636 - if( (*count * *size) > (max_memory*1048576) ) { 637 - *count = (max_memory*1048576) / *size; 743 + if (IS_PLANAR(sfmt->trans)) { 744 + saa7146_pgtable_alloc(dev->pci, &buf->pt[0]); 745 + saa7146_pgtable_alloc(dev->pci, &buf->pt[1]); 746 + saa7146_pgtable_alloc(dev->pci, &buf->pt[2]); 747 + } else { 748 + saa7146_pgtable_alloc(dev->pci, &buf->pt[0]); 638 749 } 639 750 640 - DEB_CAP("%d buffers, %d bytes each\n", *count, *size); 751 + ret = saa7146_pgtable_build(dev, buf); 752 + if (ret) 753 + release_all_pagetables(dev, buf); 754 + return ret; 755 + } 641 756 757 + static int buf_prepare(struct vb2_buffer *vb) 758 + { 759 + struct vb2_queue *vq = vb->vb2_queue; 760 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 761 + struct saa7146_vv *vv = dev->vv_data; 762 + unsigned int size = vv->video_fmt.sizeimage; 763 + 764 + if (vb2_plane_size(vb, 0) < size) 765 + return -EINVAL; 766 + vb2_set_plane_payload(vb, 0, size); 642 767 return 0; 643 768 } 644 769 645 - static void buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb) 770 + static void buf_cleanup(struct vb2_buffer *vb) 646 771 { 647 - struct file *file = q->priv_data; 648 - struct saa7146_dev *dev = video_drvdata(file); 649 - struct saa7146_vv *vv = dev->vv_data; 650 - struct saa7146_buf *buf = (struct saa7146_buf *)vb; 651 - 652 - DEB_CAP("vbuf:%p\n", vb); 653 - saa7146_buffer_queue(dev, &vv->video_dmaq, buf); 654 - } 655 - 656 - static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 657 - { 658 - struct file *file = q->priv_data; 659 - struct saa7146_dev *dev = video_drvdata(file); 660 - struct saa7146_buf *buf = (struct saa7146_buf *)vb; 661 - 662 - DEB_CAP("vbuf:%p\n", vb); 663 - 664 - saa7146_dma_free(dev,q,buf); 772 + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); 773 + struct saa7146_buf *buf = container_of(vbuf, struct saa7146_buf, vb); 774 + struct vb2_queue *vq = vb->vb2_queue; 775 + struct saa7146_dev *dev = vb2_get_drv_priv(vq); 665 776 666 777 release_all_pagetables(dev, buf); 667 778 } 668 779 669 - static const struct videobuf_queue_ops video_qops = { 670 - .buf_setup = buffer_setup, 671 - .buf_prepare = buffer_prepare, 672 - .buf_queue = buffer_queue, 673 - .buf_release = buffer_release, 780 + static void return_buffers(struct vb2_queue *q, int state) 781 + { 782 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 783 + struct saa7146_dmaqueue *dq = &dev->vv_data->video_dmaq; 784 + struct saa7146_buf *buf; 785 + 786 + if (dq->curr) { 787 + buf = dq->curr; 788 + dq->curr = NULL; 789 + vb2_buffer_done(&buf->vb.vb2_buf, state); 790 + } 791 + while (!list_empty(&dq->queue)) { 792 + buf = list_entry(dq->queue.next, struct saa7146_buf, list); 793 + list_del(&buf->list); 794 + vb2_buffer_done(&buf->vb.vb2_buf, state); 795 + } 796 + } 797 + 798 + static int start_streaming(struct vb2_queue *q, unsigned int count) 799 + { 800 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 801 + int ret; 802 + 803 + if (!vb2_is_streaming(&dev->vv_data->video_dmaq.q)) 804 + dev->vv_data->seqnr = 0; 805 + ret = video_begin(dev); 806 + if (ret) 807 + return_buffers(q, VB2_BUF_STATE_QUEUED); 808 + return ret; 809 + } 810 + 811 + static void stop_streaming(struct vb2_queue *q) 812 + { 813 + struct saa7146_dev *dev = vb2_get_drv_priv(q); 814 + struct saa7146_dmaqueue *dq = &dev->vv_data->video_dmaq; 815 + 816 + del_timer(&dq->timeout); 817 + video_end(dev); 818 + return_buffers(q, VB2_BUF_STATE_ERROR); 819 + } 820 + 821 + const struct vb2_ops video_qops = { 822 + .queue_setup = queue_setup, 823 + .buf_queue = buf_queue, 824 + .buf_init = buf_init, 825 + .buf_prepare = buf_prepare, 826 + .buf_cleanup = buf_cleanup, 827 + .start_streaming = start_streaming, 828 + .stop_streaming = stop_streaming, 829 + .wait_prepare = vb2_ops_wait_prepare, 830 + .wait_finish = vb2_ops_wait_finish, 674 831 }; 675 832 676 833 /********************************************************************************/ ··· 692 847 vv->current_hps_sync = SAA7146_HPS_SYNC_PORT_A; 693 848 } 694 849 695 - 696 - static int video_open(struct saa7146_dev *dev, struct file *file) 697 - { 698 - struct saa7146_fh *fh = file->private_data; 699 - 700 - videobuf_queue_sg_init(&fh->video_q, &video_qops, 701 - &dev->pci->dev, &dev->slock, 702 - V4L2_BUF_TYPE_VIDEO_CAPTURE, 703 - V4L2_FIELD_INTERLACED, 704 - sizeof(struct saa7146_buf), 705 - file, &dev->v4l2_lock); 706 - 707 - return 0; 708 - } 709 - 710 - 711 - static void video_close(struct saa7146_dev *dev, struct file *file) 712 - { 713 - struct saa7146_fh *fh = file->private_data; 714 - struct saa7146_vv *vv = dev->vv_data; 715 - struct videobuf_queue *q = &fh->video_q; 716 - 717 - if (IS_CAPTURE_ACTIVE(fh) != 0) 718 - video_end(dev, fh); 719 - 720 - videobuf_stop(q); 721 - /* hmm, why is this function declared void? */ 722 - } 723 - 724 - 725 850 static void video_irq_done(struct saa7146_dev *dev, unsigned long st) 726 851 { 727 852 struct saa7146_vv *vv = dev->vv_data; ··· 701 886 DEB_CAP("called\n"); 702 887 703 888 /* only finish the buffer if we have one... */ 704 - if( NULL != q->curr ) { 705 - saa7146_buffer_finish(dev,q,VIDEOBUF_DONE); 706 - } 889 + if (q->curr) 890 + saa7146_buffer_finish(dev, q, VB2_BUF_STATE_DONE); 707 891 saa7146_buffer_next(dev,q,0); 708 892 709 893 spin_unlock(&dev->slock); 710 894 } 711 895 712 - static ssize_t video_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 713 - { 714 - struct saa7146_dev *dev = video_drvdata(file); 715 - struct saa7146_fh *fh = file->private_data; 716 - struct saa7146_vv *vv = dev->vv_data; 717 - ssize_t ret = 0; 718 - 719 - DEB_EE("called\n"); 720 - 721 - if ((vv->video_status & STATUS_CAPTURE) != 0) { 722 - /* fixme: should we allow read() captures while streaming capture? */ 723 - if (vv->video_fh == fh) { 724 - DEB_S("already capturing\n"); 725 - return -EBUSY; 726 - } 727 - DEB_S("already capturing in another open\n"); 728 - return -EBUSY; 729 - } 730 - 731 - ret = video_begin(dev, fh); 732 - if( 0 != ret) { 733 - goto out; 734 - } 735 - 736 - ret = videobuf_read_one(&fh->video_q , data, count, ppos, 737 - file->f_flags & O_NONBLOCK); 738 - if (ret != 0) { 739 - video_end(dev, fh); 740 - } else { 741 - ret = video_end(dev, fh); 742 - } 743 - out: 744 - return ret; 745 - } 746 - 747 896 const struct saa7146_use_ops saa7146_video_uops = { 748 897 .init = video_init, 749 - .open = video_open, 750 - .release = video_close, 751 898 .irq_done = video_irq_done, 752 - .read = video_read, 753 899 };
-10
drivers/media/pci/saa7146/mxb.c
··· 587 587 { 588 588 struct saa7146_dev *dev = video_drvdata(file); 589 589 struct mxb *mxb = (struct mxb *)dev->ext_priv; 590 - struct saa7146_vv *vv = dev->vv_data; 591 590 592 591 if (f->tuner) 593 592 return -EINVAL; ··· 603 604 tuner_call(mxb, tuner, g_frequency, &mxb->cur_freq); 604 605 if (mxb->cur_audinput == 0) 605 606 mxb_update_audmode(mxb); 606 - 607 - if (mxb->cur_input) 608 - return 0; 609 - 610 - /* hack: changing the frequency should invalidate the vbi-counter (=> alevt) */ 611 - spin_lock(&dev->slock); 612 - vv->vbi_fieldcount = 0; 613 - spin_unlock(&dev->slock); 614 - 615 607 return 0; 616 608 } 617 609
+8 -28
include/media/drv-intf/saa7146_vv.h
··· 6 6 #include <media/v4l2-ioctl.h> 7 7 #include <media/v4l2-fh.h> 8 8 #include <media/drv-intf/saa7146.h> 9 - #include <media/videobuf-dma-sg.h> 9 + #include <media/videobuf2-dma-sg.h> 10 10 11 11 #define MAX_SAA7146_CAPTURE_BUFFERS 32 /* arbitrary */ 12 12 #define BUFFER_TIMEOUT (HZ/2) /* 0.5 seconds */ ··· 57 57 /* buffer for one video/vbi frame */ 58 58 struct saa7146_buf { 59 59 /* common v4l buffer stuff -- must be first */ 60 - struct videobuf_buffer vb; 60 + struct vb2_v4l2_buffer vb; 61 + struct list_head list; 61 62 62 63 /* saa7146 specific */ 63 64 int (*activate)(struct saa7146_dev *dev, ··· 74 73 struct saa7146_buf *curr; 75 74 struct list_head queue; 76 75 struct timer_list timeout; 76 + struct vb2_queue q; 77 77 }; 78 - 79 - /* per open data */ 80 - struct saa7146_fh { 81 - /* Must be the first field! */ 82 - struct v4l2_fh fh; 83 - 84 - /* video capture */ 85 - struct videobuf_queue video_q; 86 - 87 - /* vbi capture */ 88 - struct videobuf_queue vbi_q; 89 - }; 90 - 91 - #define STATUS_CAPTURE 0x02 92 78 93 79 struct saa7146_vv 94 80 { ··· 83 95 struct saa7146_dmaqueue vbi_dmaq; 84 96 struct v4l2_vbi_format vbi_fmt; 85 97 struct timer_list vbi_read_timeout; 86 - struct file *vbi_read_timeout_file; 87 98 /* vbi workaround interrupt queue */ 88 99 wait_queue_head_t vbi_wq; 89 - int vbi_fieldcount; 90 - struct saa7146_fh *vbi_streaming; 91 - 92 - int video_status; 93 - struct saa7146_fh *video_fh; 94 100 95 101 /* video capture */ 96 102 struct saa7146_dmaqueue video_dmaq; 97 103 struct v4l2_pix_format video_fmt; 98 104 enum v4l2_field last_field; 105 + u32 seqnr; 99 106 100 107 /* common: fixme? shouldn't this be in saa7146_fh? 101 108 (this leads to a more complicated question: shall the driver ··· 105 122 int current_hps_source; 106 123 int current_hps_sync; 107 124 108 - unsigned int resources; /* resource management for device */ 125 + unsigned int resources; /* resource management for device */ 109 126 }; 110 127 111 128 /* flags */ ··· 135 152 136 153 struct saa7146_use_ops { 137 154 void (*init)(struct saa7146_dev *, struct saa7146_vv *); 138 - int(*open)(struct saa7146_dev *, struct file *); 139 - void (*release)(struct saa7146_dev *, struct file *); 140 155 void (*irq_done)(struct saa7146_dev *, unsigned long status); 141 - ssize_t (*read)(struct file *, char __user *, size_t, loff_t *); 142 156 }; 143 157 144 158 /* from saa7146_fops.c */ ··· 145 165 void saa7146_buffer_next(struct saa7146_dev *dev, struct saa7146_dmaqueue *q,int vbi); 146 166 int saa7146_buffer_queue(struct saa7146_dev *dev, struct saa7146_dmaqueue *q, struct saa7146_buf *buf); 147 167 void saa7146_buffer_timeout(struct timer_list *t); 148 - void saa7146_dma_free(struct saa7146_dev* dev,struct videobuf_queue *q, 149 - struct saa7146_buf *buf); 150 168 151 169 int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv); 152 170 int saa7146_vv_release(struct saa7146_dev* dev); ··· 159 181 extern const struct v4l2_ioctl_ops saa7146_video_ioctl_ops; 160 182 extern const struct v4l2_ioctl_ops saa7146_vbi_ioctl_ops; 161 183 extern const struct saa7146_use_ops saa7146_video_uops; 184 + extern const struct vb2_ops video_qops; 162 185 long saa7146_video_do_ioctl(struct file *file, unsigned int cmd, void *arg); 163 186 int saa7146_s_ctrl(struct v4l2_ctrl *ctrl); 164 187 165 188 /* from saa7146_vbi.c */ 166 189 extern const struct saa7146_use_ops saa7146_vbi_uops; 190 + extern const struct vb2_ops vbi_qops; 167 191 168 192 /* resource management functions */ 169 193 int saa7146_res_get(struct saa7146_dev *dev, unsigned int bit);