--- zzzz-none-000/linux-3.10.107/drivers/media/usb/au0828/au0828-video.c 2017-06-27 09:49:32.000000000 +0000 +++ scorpion-7490-727/linux-3.10.107/drivers/media/usb/au0828/au0828-video.c 2021-02-04 17:41:59.000000000 +0000 @@ -22,23 +22,21 @@ /* Developer Notes: * - * VBI support is not yet working * The hardware scaler supported is unimplemented * AC97 audio support is unimplemented (only i2s audio mode) * */ +#include "au0828.h" + #include #include #include #include -#include #include #include #include -#include #include -#include "au0828.h" #include "au0828-reg.h" static DEFINE_MUTEX(au0828_sysfs_lock); @@ -54,7 +52,7 @@ #define au0828_isocdbg(fmt, arg...) \ do {\ if (isoc_debug) { \ - printk(KERN_INFO "au0828 %s :"fmt, \ + pr_info("au0828 %s :"fmt, \ __func__ , ##arg); \ } \ } while (0) @@ -106,14 +104,13 @@ static int check_dev(struct au0828_dev *dev) { - if (dev->dev_state & DEV_DISCONNECTED) { - printk(KERN_INFO "v4l2 ioctl: device not present\n"); + if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) { + pr_info("v4l2 ioctl: device not present\n"); return -ENODEV; } - if (dev->dev_state & DEV_MISCONFIGURED) { - printk(KERN_INFO "v4l2 ioctl: device is misconfigured; " - "close and open it again\n"); + if (test_bit(DEV_MISCONFIGURED, &dev->dev_state)) { + pr_info("v4l2 ioctl: device is misconfigured; close and open it again\n"); return -EIO; } return 0; @@ -160,6 +157,7 @@ au0828_isocdbg("urb resubmit failed (error=%i)\n", urb->status); } + dev->stream_state = STREAM_ON; } /* @@ -199,6 +197,8 @@ dev->isoc_ctl.urb = NULL; dev->isoc_ctl.transfer_buffer = NULL; dev->isoc_ctl.num_bufs = 0; + + dev->stream_state = STREAM_OFF; } /* @@ -217,9 +217,6 @@ au0828_isocdbg("au0828: called au0828_prepare_isoc\n"); - /* De-allocates all pending stuff */ - au0828_uninit_isoc(dev); - dev->isoc_ctl.isoc_copy = isoc_copy; dev->isoc_ctl.num_bufs = num_bufs; @@ -283,8 +280,6 @@ } } - init_waitqueue_head(&dma_q->wq); - /* submit urbs and enables IRQ */ for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); @@ -303,37 +298,23 @@ * Announces that a buffer were filled and request the next */ static inline void buffer_filled(struct au0828_dev *dev, - struct au0828_dmaqueue *dma_q, - struct au0828_buffer *buf) + struct au0828_dmaqueue *dma_q, + struct au0828_buffer *buf) { - /* Advice that buffer was filled */ - au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); - - buf->vb.state = VIDEOBUF_DONE; - buf->vb.field_count++; - v4l2_get_timestamp(&buf->vb.ts); - - dev->isoc_ctl.buf = NULL; - - list_del(&buf->vb.queue); - wake_up(&buf->vb.done); -} + struct vb2_v4l2_buffer *vb = &buf->vb; + struct vb2_queue *q = vb->vb2_buf.vb2_queue; -static inline void vbi_buffer_filled(struct au0828_dev *dev, - struct au0828_dmaqueue *dma_q, - struct au0828_buffer *buf) -{ /* Advice that buffer was filled */ - au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i); - - buf->vb.state = VIDEOBUF_DONE; - buf->vb.field_count++; - v4l2_get_timestamp(&buf->vb.ts); + au0828_isocdbg("[%p/%d] wakeup\n", buf, buf->top_field); - dev->isoc_ctl.vbi_buf = NULL; + if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) + vb->sequence = dev->frame_count++; + else + vb->sequence = dev->vbi_frame_count++; - list_del(&buf->vb.queue); - wake_up(&buf->vb.done); + vb->field = V4L2_FIELD_INTERLACED; + v4l2_get_timestamp(&vb->timestamp); + vb2_buffer_done(&vb->vb2_buf, VB2_BUF_STATE_DONE); } /* @@ -352,8 +333,8 @@ if (len == 0) return; - if (dma_q->pos + len > buf->vb.size) - len = buf->vb.size - dma_q->pos; + if (dma_q->pos + len > buf->length) + len = buf->length - dma_q->pos; startread = p; remain = len; @@ -371,11 +352,11 @@ lencopy = bytesperline - currlinedone; lencopy = lencopy > remain ? remain : lencopy; - if ((char *)startwrite + lencopy > (char *)outp + buf->vb.size) { + if ((char *)startwrite + lencopy > (char *)outp + buf->length) { au0828_isocdbg("Overflow of %zi bytes past buffer end (1)\n", ((char *)startwrite + lencopy) - - ((char *)outp + buf->vb.size)); - remain = (char *)outp + buf->vb.size - (char *)startwrite; + ((char *)outp + buf->length)); + remain = (char *)outp + buf->length - (char *)startwrite; lencopy = remain; } if (lencopy <= 0) @@ -393,11 +374,11 @@ lencopy = bytesperline; if ((char *)startwrite + lencopy > (char *)outp + - buf->vb.size) { + buf->length) { au0828_isocdbg("Overflow %zi bytes past buf end (2)\n", ((char *)startwrite + lencopy) - - ((char *)outp + buf->vb.size)); - lencopy = remain = (char *)outp + buf->vb.size - + ((char *)outp + buf->length)); + lencopy = remain = (char *)outp + buf->length - (char *)startwrite; } if (lencopy <= 0) @@ -433,7 +414,11 @@ } /* Get the next buffer */ - *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue); + *buf = list_entry(dma_q->active.next, struct au0828_buffer, list); + /* Cleans up buffer - Useful for testing for frame/URB loss */ + list_del(&(*buf)->list); + dma_q->pos = 0; + (*buf)->vb_buf = (*buf)->mem; dev->isoc_ctl.buf = *buf; return; @@ -471,8 +456,8 @@ bytesperline = dev->vbi_width; - if (dma_q->pos + len > buf->vb.size) - len = buf->vb.size - dma_q->pos; + if (dma_q->pos + len > buf->length) + len = buf->length - dma_q->pos; startread = p; startwrite = outp + (dma_q->pos / 2); @@ -495,7 +480,6 @@ struct au0828_buffer **buf) { struct au0828_dev *dev = container_of(dma_q, struct au0828_dev, vbiq); - char *outp; if (list_empty(&dma_q->active)) { au0828_isocdbg("No active queue to serve\n"); @@ -505,13 +489,12 @@ } /* Get the next buffer */ - *buf = list_entry(dma_q->active.next, struct au0828_buffer, vb.queue); + *buf = list_entry(dma_q->active.next, struct au0828_buffer, list); /* Cleans up buffer - Useful for testing for frame/URB loss */ - outp = videobuf_to_vmalloc(&(*buf)->vb); - memset(outp, 0x00, (*buf)->vb.size); - + list_del(&(*buf)->list); + dma_q->pos = 0; + (*buf)->vb_buf = (*buf)->mem; dev->isoc_ctl.vbi_buf = *buf; - return; } @@ -535,8 +518,8 @@ if (!dev) return 0; - if ((dev->dev_state & DEV_DISCONNECTED) || - (dev->dev_state & DEV_MISCONFIGURED)) + if (test_bit(DEV_DISCONNECTED, &dev->dev_state) || + test_bit(DEV_MISCONFIGURED, &dev->dev_state)) return 0; if (urb->status < 0) { @@ -547,11 +530,11 @@ buf = dev->isoc_ctl.buf; if (buf != NULL) - outp = videobuf_to_vmalloc(&buf->vb); + outp = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); vbi_buf = dev->isoc_ctl.vbi_buf; if (vbi_buf != NULL) - vbioutp = videobuf_to_vmalloc(&vbi_buf->vb); + vbioutp = vb2_plane_vaddr(&vbi_buf->vb.vb2_buf, 0); for (i = 0; i < urb->number_of_packets; i++) { int status = urb->iso_frame_desc[i].status; @@ -584,15 +567,13 @@ if (fbyte & 0x40) { /* VBI */ if (vbi_buf != NULL) - vbi_buffer_filled(dev, - vbi_dma_q, - vbi_buf); + buffer_filled(dev, vbi_dma_q, vbi_buf); vbi_get_next_buf(vbi_dma_q, &vbi_buf); if (vbi_buf == NULL) vbioutp = NULL; else - vbioutp = videobuf_to_vmalloc( - &vbi_buf->vb); + vbioutp = vb2_plane_vaddr( + &vbi_buf->vb.vb2_buf, 0); /* Video */ if (buf != NULL) @@ -601,7 +582,8 @@ if (buf == NULL) outp = NULL; else - outp = videobuf_to_vmalloc(&buf->vb); + outp = vb2_plane_vaddr( + &buf->vb.vb2_buf, 0); /* As long as isoc traffic is arriving, keep resetting the timer */ @@ -655,130 +637,63 @@ return rc; } -static int -buffer_setup(struct videobuf_queue *vq, unsigned int *count, - unsigned int *size) -{ - struct au0828_fh *fh = vq->priv_data; - *size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3; - - if (0 == *count) - *count = AU0828_DEF_BUF; - - if (*count < AU0828_MIN_BUF) - *count = AU0828_MIN_BUF; - return 0; -} +static int queue_setup(struct vb2_queue *vq, const void *parg, + unsigned int *nbuffers, unsigned int *nplanes, + unsigned int sizes[], void *alloc_ctxs[]) +{ + const struct v4l2_format *fmt = parg; + struct au0828_dev *dev = vb2_get_drv_priv(vq); + unsigned long img_size = dev->height * dev->bytesperline; + unsigned long size; -/* This is called *without* dev->slock held; please keep it that way */ -static void free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf) -{ - struct au0828_fh *fh = vq->priv_data; - struct au0828_dev *dev = fh->dev; - unsigned long flags = 0; - if (in_interrupt()) - BUG(); + size = fmt ? fmt->fmt.pix.sizeimage : img_size; + if (size < img_size) + return -EINVAL; - /* We used to wait for the buffer to finish here, but this didn't work - because, as we were keeping the state as VIDEOBUF_QUEUED, - videobuf_queue_cancel marked it as finished for us. - (Also, it could wedge forever if the hardware was misconfigured.) - - This should be safe; by the time we get here, the buffer isn't - queued anymore. If we ever start marking the buffers as - VIDEOBUF_ACTIVE, it won't be, though. - */ - spin_lock_irqsave(&dev->slock, flags); - if (dev->isoc_ctl.buf == buf) - dev->isoc_ctl.buf = NULL; - spin_unlock_irqrestore(&dev->slock, flags); + *nplanes = 1; + sizes[0] = size; - videobuf_vmalloc_free(&buf->vb); - buf->vb.state = VIDEOBUF_NEEDS_INIT; + return 0; } static int -buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, - enum v4l2_field field) +buffer_prepare(struct vb2_buffer *vb) { - struct au0828_fh *fh = vq->priv_data; - struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb); - struct au0828_dev *dev = fh->dev; - int rc = 0, urb_init = 0; - - buf->vb.size = (fh->dev->width * fh->dev->height * 16 + 7) >> 3; - - if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct au0828_buffer *buf = container_of(vbuf, + struct au0828_buffer, vb); + struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue); + + buf->length = dev->height * dev->bytesperline; + + if (vb2_plane_size(vb, 0) < buf->length) { + pr_err("%s data will not fit into plane (%lu < %lu)\n", + __func__, vb2_plane_size(vb, 0), buf->length); return -EINVAL; - - buf->vb.width = dev->width; - buf->vb.height = dev->height; - buf->vb.field = field; - - if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { - rc = videobuf_iolock(vq, &buf->vb, NULL); - if (rc < 0) { - printk(KERN_INFO "videobuf_iolock failed\n"); - goto fail; - } } - - if (!dev->isoc_ctl.num_bufs) - urb_init = 1; - - if (urb_init) { - rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB, - AU0828_MAX_ISO_BUFS, dev->max_pkt_size, - au0828_isoc_copy); - if (rc < 0) { - printk(KERN_INFO "au0828_init_isoc failed\n"); - goto fail; - } - } - - buf->vb.state = VIDEOBUF_PREPARED; + vb2_set_plane_payload(&buf->vb.vb2_buf, 0, buf->length); return 0; - -fail: - free_buffer(vq, buf); - return rc; } static void -buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) +buffer_queue(struct vb2_buffer *vb) { - struct au0828_buffer *buf = container_of(vb, + struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb); + struct au0828_buffer *buf = container_of(vbuf, struct au0828_buffer, vb); - struct au0828_fh *fh = vq->priv_data; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = vb2_get_drv_priv(vb->vb2_queue); struct au0828_dmaqueue *vidq = &dev->vidq; + unsigned long flags = 0; - buf->vb.state = VIDEOBUF_QUEUED; - list_add_tail(&buf->vb.queue, &vidq->active); -} - -static void buffer_release(struct videobuf_queue *vq, - struct videobuf_buffer *vb) -{ - struct au0828_buffer *buf = container_of(vb, - struct au0828_buffer, - vb); + buf->mem = vb2_plane_vaddr(vb, 0); + buf->length = vb2_plane_size(vb, 0); - free_buffer(vq, buf); + spin_lock_irqsave(&dev->slock, flags); + list_add_tail(&buf->list, &vidq->active); + spin_unlock_irqrestore(&dev->slock, flags); } -static struct videobuf_queue_ops au0828_video_qops = { - .buf_setup = buffer_setup, - .buf_prepare = buffer_prepare, - .buf_queue = buffer_queue, - .buf_release = buffer_release, -}; - -/* ------------------------------------------------------------------ - V4L2 interface - ------------------------------------------------------------------*/ - static int au0828_i2s_init(struct au0828_dev *dev) { /* Enable i2s mode */ @@ -792,7 +707,7 @@ static int au0828_analog_stream_enable(struct au0828_dev *d) { struct usb_interface *iface; - int ret; + int ret, h, w; dprintk(1, "au0828_analog_stream_enable called\n"); @@ -802,31 +717,32 @@ /* set au0828 interface0 to AS5 here again */ ret = usb_set_interface(d->usbdev, 0, 5); if (ret < 0) { - printk(KERN_INFO "Au0828 can't set alt setting to 5!\n"); + pr_info("Au0828 can't set alt setting to 5!\n"); return -EBUSY; } } - /* FIXME: size should be calculated using d->width, d->height */ + h = d->height / 2 + 2; + w = d->width * 2; au0828_writereg(d, AU0828_SENSORCTRL_VBI_103, 0x00); au0828_writereg(d, 0x106, 0x00); /* set x position */ au0828_writereg(d, 0x110, 0x00); au0828_writereg(d, 0x111, 0x00); - au0828_writereg(d, 0x114, 0xa0); - au0828_writereg(d, 0x115, 0x05); + au0828_writereg(d, 0x114, w & 0xff); + au0828_writereg(d, 0x115, w >> 8); /* set y position */ au0828_writereg(d, 0x112, 0x00); au0828_writereg(d, 0x113, 0x00); - au0828_writereg(d, 0x116, 0xf2); - au0828_writereg(d, 0x117, 0x00); + au0828_writereg(d, 0x116, h & 0xff); + au0828_writereg(d, 0x117, h >> 8); au0828_writereg(d, AU0828_SENSORCTRL_100, 0xb3); return 0; } -int au0828_analog_stream_disable(struct au0828_dev *d) +static int au0828_analog_stream_disable(struct au0828_dev *d) { dprintk(1, "au0828_analog_stream_disable called\n"); au0828_writereg(d, AU0828_SENSORCTRL_100, 0x0); @@ -849,88 +765,140 @@ int ret = 0; dev->stream_state = STREAM_INTERRUPT; - if (dev->dev_state == DEV_DISCONNECTED) + if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) return -ENODEV; else if (ret) { - dev->dev_state = DEV_MISCONFIGURED; + set_bit(DEV_MISCONFIGURED, &dev->dev_state); dprintk(1, "%s device is misconfigured!\n", __func__); return ret; } return 0; } -/* - * au0828_release_resources - * unregister v4l2 devices - */ -void au0828_analog_unregister(struct au0828_dev *dev) +int au0828_start_analog_streaming(struct vb2_queue *vq, unsigned int count) { - dprintk(1, "au0828_release_resources called\n"); - mutex_lock(&au0828_sysfs_lock); + struct au0828_dev *dev = vb2_get_drv_priv(vq); + int rc = 0; - if (dev->vdev) - video_unregister_device(dev->vdev); - if (dev->vbi_dev) - video_unregister_device(dev->vbi_dev); + dprintk(1, "au0828_start_analog_streaming called %d\n", + dev->streaming_users); - mutex_unlock(&au0828_sysfs_lock); -} + if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) + dev->frame_count = 0; + else + dev->vbi_frame_count = 0; + + if (dev->streaming_users == 0) { + /* If we were doing ac97 instead of i2s, it would go here...*/ + au0828_i2s_init(dev); + rc = au0828_init_isoc(dev, AU0828_ISO_PACKETS_PER_URB, + AU0828_MAX_ISO_BUFS, dev->max_pkt_size, + au0828_isoc_copy); + if (rc < 0) { + pr_info("au0828_init_isoc failed\n"); + return rc; + } + if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { + v4l2_device_call_all(&dev->v4l2_dev, 0, video, + s_stream, 1); + dev->vid_timeout_running = 1; + mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); + } else if (vq->type == V4L2_BUF_TYPE_VBI_CAPTURE) { + dev->vbi_timeout_running = 1; + mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); + } + } + dev->streaming_users++; + return rc; +} -/* Usage lock check functions */ -static int res_get(struct au0828_fh *fh, unsigned int bit) +static void au0828_stop_streaming(struct vb2_queue *vq) { - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = vb2_get_drv_priv(vq); + struct au0828_dmaqueue *vidq = &dev->vidq; + unsigned long flags = 0; - if (fh->resources & bit) - /* have it already allocated */ - return 1; - - /* is it free? */ - if (dev->resources & bit) { - /* no, someone else uses it */ - return 0; + dprintk(1, "au0828_stop_streaming called %d\n", dev->streaming_users); + + if (dev->streaming_users-- == 1) + au0828_uninit_isoc(dev); + + v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); + dev->vid_timeout_running = 0; + del_timer_sync(&dev->vid_timeout); + + spin_lock_irqsave(&dev->slock, flags); + if (dev->isoc_ctl.buf != NULL) { + vb2_buffer_done(&dev->isoc_ctl.buf->vb.vb2_buf, + VB2_BUF_STATE_ERROR); + dev->isoc_ctl.buf = NULL; } - /* it's free, grab it */ - fh->resources |= bit; - dev->resources |= bit; - dprintk(1, "res: get %d\n", bit); + while (!list_empty(&vidq->active)) { + struct au0828_buffer *buf; - return 1; + buf = list_entry(vidq->active.next, struct au0828_buffer, list); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); + list_del(&buf->list); + } + spin_unlock_irqrestore(&dev->slock, flags); } -static int res_check(struct au0828_fh *fh, unsigned int bit) +void au0828_stop_vbi_streaming(struct vb2_queue *vq) { - return fh->resources & bit; -} + struct au0828_dev *dev = vb2_get_drv_priv(vq); + struct au0828_dmaqueue *vbiq = &dev->vbiq; + unsigned long flags = 0; -static int res_locked(struct au0828_dev *dev, unsigned int bit) -{ - return dev->resources & bit; -} + dprintk(1, "au0828_stop_vbi_streaming called %d\n", + dev->streaming_users); -static void res_free(struct au0828_fh *fh, unsigned int bits) -{ - struct au0828_dev *dev = fh->dev; + if (dev->streaming_users-- == 1) + au0828_uninit_isoc(dev); + + spin_lock_irqsave(&dev->slock, flags); + if (dev->isoc_ctl.vbi_buf != NULL) { + vb2_buffer_done(&dev->isoc_ctl.vbi_buf->vb.vb2_buf, + VB2_BUF_STATE_ERROR); + dev->isoc_ctl.vbi_buf = NULL; + } + while (!list_empty(&vbiq->active)) { + struct au0828_buffer *buf; - BUG_ON((fh->resources & bits) != bits); + buf = list_entry(vbiq->active.next, struct au0828_buffer, list); + list_del(&buf->list); + vb2_buffer_done(&buf->vb.vb2_buf, VB2_BUF_STATE_ERROR); + } + spin_unlock_irqrestore(&dev->slock, flags); - fh->resources &= ~bits; - dev->resources &= ~bits; - dprintk(1, "res: put %d\n", bits); + dev->vbi_timeout_running = 0; + del_timer_sync(&dev->vbi_timeout); } -static int get_ressource(struct au0828_fh *fh) +static struct vb2_ops au0828_video_qops = { + .queue_setup = queue_setup, + .buf_prepare = buffer_prepare, + .buf_queue = buffer_queue, + .start_streaming = au0828_start_analog_streaming, + .stop_streaming = au0828_stop_streaming, + .wait_prepare = vb2_ops_wait_prepare, + .wait_finish = vb2_ops_wait_finish, +}; + +/* ------------------------------------------------------------------ + V4L2 interface + ------------------------------------------------------------------*/ +/* + * au0828_analog_unregister + * unregister v4l2 devices + */ +void au0828_analog_unregister(struct au0828_dev *dev) { - switch (fh->type) { - case V4L2_BUF_TYPE_VIDEO_CAPTURE: - return AU0828_RESOURCE_VIDEO; - case V4L2_BUF_TYPE_VBI_CAPTURE: - return AU0828_RESOURCE_VBI; - default: - BUG(); - return 0; - } + dprintk(1, "au0828_analog_unregister called\n"); + mutex_lock(&au0828_sysfs_lock); + video_unregister_device(&dev->vdev); + video_unregister_device(&dev->vbi_dev); + mutex_unlock(&au0828_sysfs_lock); } /* This function ensures that video frames continue to be delivered even if @@ -948,8 +916,8 @@ buf = dev->isoc_ctl.buf; if (buf != NULL) { - vid_data = videobuf_to_vmalloc(&buf->vb); - memset(vid_data, 0x00, buf->vb.size); /* Blank green frame */ + vid_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); + memset(vid_data, 0x00, buf->length); /* Blank green frame */ buffer_filled(dev, dma_q, buf); } get_next_buf(dma_q, &buf); @@ -972,9 +940,9 @@ buf = dev->isoc_ctl.vbi_buf; if (buf != NULL) { - vbi_data = videobuf_to_vmalloc(&buf->vb); - memset(vbi_data, 0x00, buf->vb.size); - vbi_buffer_filled(dev, dma_q, buf); + vbi_data = vb2_plane_vaddr(&buf->vb.vb2_buf, 0); + memset(vbi_data, 0x00, buf->length); + buffer_filled(dev, dma_q, buf); } vbi_get_next_buf(dma_q, &buf); @@ -983,105 +951,65 @@ spin_unlock_irqrestore(&dev->slock, flags); } - static int au0828_v4l2_open(struct file *filp) { - int ret = 0; - struct video_device *vdev = video_devdata(filp); struct au0828_dev *dev = video_drvdata(filp); - struct au0828_fh *fh; - int type; - - switch (vdev->vfl_type) { - case VFL_TYPE_GRABBER: - type = V4L2_BUF_TYPE_VIDEO_CAPTURE; - break; - case VFL_TYPE_VBI: - type = V4L2_BUF_TYPE_VBI_CAPTURE; - break; - default: - return -EINVAL; - } - - fh = kzalloc(sizeof(struct au0828_fh), GFP_KERNEL); - if (NULL == fh) { - dprintk(1, "Failed allocate au0828_fh struct!\n"); - return -ENOMEM; - } + int ret; - fh->type = type; - fh->dev = dev; - v4l2_fh_init(&fh->fh, vdev); - filp->private_data = fh; + dprintk(1, + "%s called std_set %d dev_state %ld stream users %d users %d\n", + __func__, dev->std_set_in_tuner_core, dev->dev_state, + dev->streaming_users, dev->users); - if (mutex_lock_interruptible(&dev->lock)) { - kfree(fh); + if (mutex_lock_interruptible(&dev->lock)) return -ERESTARTSYS; + + ret = v4l2_fh_open(filp); + if (ret) { + au0828_isocdbg("%s: v4l2_fh_open() returned error %d\n", + __func__, ret); + mutex_unlock(&dev->lock); + return ret; } + if (dev->users == 0) { au0828_analog_stream_enable(dev); au0828_analog_stream_reset(dev); - - /* If we were doing ac97 instead of i2s, it would go here...*/ - au0828_i2s_init(dev); - dev->stream_state = STREAM_OFF; - dev->dev_state |= DEV_INITIALIZED; + set_bit(DEV_INITIALIZED, &dev->dev_state); } - dev->users++; mutex_unlock(&dev->lock); - - videobuf_queue_vmalloc_init(&fh->vb_vidq, &au0828_video_qops, - NULL, &dev->slock, - V4L2_BUF_TYPE_VIDEO_CAPTURE, - V4L2_FIELD_INTERLACED, - sizeof(struct au0828_buffer), fh, - &dev->lock); - - /* VBI Setup */ - videobuf_queue_vmalloc_init(&fh->vb_vbiq, &au0828_vbi_qops, - NULL, &dev->slock, - V4L2_BUF_TYPE_VBI_CAPTURE, - V4L2_FIELD_SEQ_TB, - sizeof(struct au0828_buffer), fh, - &dev->lock); - v4l2_fh_add(&fh->fh); return ret; } static int au0828_v4l2_close(struct file *filp) { int ret; - struct au0828_fh *fh = filp->private_data; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(filp); + struct video_device *vdev = video_devdata(filp); + + dprintk(1, + "%s called std_set %d dev_state %ld stream users %d users %d\n", + __func__, dev->std_set_in_tuner_core, dev->dev_state, + dev->streaming_users, dev->users); - v4l2_fh_del(&fh->fh); - v4l2_fh_exit(&fh->fh); mutex_lock(&dev->lock); - if (res_check(fh, AU0828_RESOURCE_VIDEO)) { + if (vdev->vfl_type == VFL_TYPE_GRABBER && dev->vid_timeout_running) { /* Cancel timeout thread in case they didn't call streamoff */ dev->vid_timeout_running = 0; del_timer_sync(&dev->vid_timeout); - - videobuf_stop(&fh->vb_vidq); - res_free(fh, AU0828_RESOURCE_VIDEO); - } - - if (res_check(fh, AU0828_RESOURCE_VBI)) { + } else if (vdev->vfl_type == VFL_TYPE_VBI && + dev->vbi_timeout_running) { /* Cancel timeout thread in case they didn't call streamoff */ dev->vbi_timeout_running = 0; del_timer_sync(&dev->vbi_timeout); - - videobuf_stop(&fh->vb_vbiq); - res_free(fh, AU0828_RESOURCE_VBI); } - if (dev->users == 1 && video_is_registered(video_devdata(filp))) { - au0828_analog_stream_disable(dev); - - au0828_uninit_isoc(dev); + if (test_bit(DEV_DISCONNECTED, &dev->dev_state)) + goto end; + if (dev->users == 1) { /* Save some power by putting tuner to sleep */ v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_power, 0); dev->std_set_in_tuner_core = 0; @@ -1090,15 +1018,12 @@ USB bandwidth */ ret = usb_set_interface(dev->usbdev, 0, 0); if (ret < 0) - printk(KERN_INFO "Au0828 can't set alternate to 0!\n"); + pr_info("Au0828 can't set alternate to 0!\n"); } - mutex_unlock(&dev->lock); - - videobuf_mmap_free(&fh->vb_vidq); - videobuf_mmap_free(&fh->vb_vbiq); - kfree(fh); +end: + _vb2_fop_release(filp, NULL); dev->users--; - wake_up_interruptible_nr(&dev->open, 1); + mutex_unlock(&dev->lock); return 0; } @@ -1110,6 +1035,9 @@ .type = V4L2_TUNER_ANALOG_TV, }; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + if (dev->std_set_in_tuner_core) return; dev->std_set_in_tuner_core = 1; @@ -1117,103 +1045,11 @@ /* If we've never sent the standard in tuner core, do so now. We don't do this at device probe because we don't want to incur the cost of a firmware load */ - v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->std); + v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, dev->std); v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, &f); i2c_gate_ctrl(dev, 0); } -static ssize_t au0828_v4l2_read(struct file *filp, char __user *buf, - size_t count, loff_t *pos) -{ - struct au0828_fh *fh = filp->private_data; - struct au0828_dev *dev = fh->dev; - int rc; - - rc = check_dev(dev); - if (rc < 0) - return rc; - - if (mutex_lock_interruptible(&dev->lock)) - return -ERESTARTSYS; - au0828_init_tuner(dev); - mutex_unlock(&dev->lock); - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { - if (res_locked(dev, AU0828_RESOURCE_VIDEO)) - return -EBUSY; - - return videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0, - filp->f_flags & O_NONBLOCK); - } - - if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { - if (!res_get(fh, AU0828_RESOURCE_VBI)) - return -EBUSY; - - if (dev->vbi_timeout_running == 0) { - /* Handle case where caller tries to read without - calling streamon first */ - dev->vbi_timeout_running = 1; - mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); - } - - return videobuf_read_stream(&fh->vb_vbiq, buf, count, pos, 0, - filp->f_flags & O_NONBLOCK); - } - - return 0; -} - -static unsigned int au0828_v4l2_poll(struct file *filp, poll_table *wait) -{ - struct au0828_fh *fh = filp->private_data; - struct au0828_dev *dev = fh->dev; - unsigned long req_events = poll_requested_events(wait); - unsigned int res; - - if (check_dev(dev) < 0) - return POLLERR; - - res = v4l2_ctrl_poll(filp, wait); - if (!(req_events & (POLLIN | POLLRDNORM))) - return res; - - if (mutex_lock_interruptible(&dev->lock)) - return -ERESTARTSYS; - au0828_init_tuner(dev); - mutex_unlock(&dev->lock); - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { - if (!res_get(fh, AU0828_RESOURCE_VIDEO)) - return POLLERR; - return res | videobuf_poll_stream(filp, &fh->vb_vidq, wait); - } - if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { - if (!res_get(fh, AU0828_RESOURCE_VBI)) - return POLLERR; - return res | videobuf_poll_stream(filp, &fh->vb_vbiq, wait); - } - return POLLERR; -} - -static int au0828_v4l2_mmap(struct file *filp, struct vm_area_struct *vma) -{ - struct au0828_fh *fh = filp->private_data; - struct au0828_dev *dev = fh->dev; - int rc; - - rc = check_dev(dev); - if (rc < 0) - return rc; - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) - rc = videobuf_mmap_mapper(&fh->vb_vidq, vma); - else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) - rc = videobuf_mmap_mapper(&fh->vb_vbiq, vma); - - return rc; -} - static int au0828_set_format(struct au0828_dev *dev, unsigned int cmd, struct v4l2_format *format) { @@ -1265,13 +1101,14 @@ return 0; } - static int vidioc_querycap(struct file *file, void *priv, struct v4l2_capability *cap) { struct video_device *vdev = video_devdata(file); - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); strlcpy(cap->driver, "au0828", sizeof(cap->driver)); strlcpy(cap->card, dev->board.name, sizeof(cap->card)); @@ -1297,6 +1134,8 @@ if (f->index) return -EINVAL; + dprintk(1, "%s called\n", __func__); + f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; strcpy(f->description, "Packed YUV2"); @@ -1309,8 +1148,10 @@ static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); f->fmt.pix.width = dev->width; f->fmt.pix.height = dev->height; @@ -1326,8 +1167,10 @@ static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); return au0828_set_format(dev, VIDIOC_TRY_FMT, f); } @@ -1335,16 +1178,18 @@ static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, struct v4l2_format *f) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); int rc; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + rc = check_dev(dev); if (rc < 0) return rc; - if (videobuf_queue_is_busy(&fh->vb_vidq)) { - printk(KERN_INFO "%s queue busy\n", __func__); + if (vb2_is_busy(&dev->vb_vidq)) { + pr_info("%s queue busy\n", __func__); rc = -EBUSY; goto out; } @@ -1356,8 +1201,16 @@ static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + + if (norm == dev->std) + return 0; + + if (dev->streaming_users > 0) + return -EBUSY; dev->std = norm; @@ -1365,11 +1218,13 @@ i2c_gate_ctrl(dev, 1); - /* FIXME: when we support something other than NTSC, we are going to - have to make the au0828 bridge adjust the size of its capture - buffer, which is currently hardcoded at 720x480 */ + /* + * FIXME: when we support something other than 60Hz standards, + * we are going to have to make the au0828 bridge adjust the size + * of its capture buffer, which is currently hardcoded at 720x480 + */ - v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, norm); + v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_std, norm); i2c_gate_ctrl(dev, 0); @@ -1378,8 +1233,10 @@ static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *norm) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); *norm = dev->std; return 0; @@ -1388,8 +1245,7 @@ static int vidioc_enum_input(struct file *file, void *priv, struct v4l2_input *input) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); unsigned int tmp; static const char *inames[] = { @@ -1402,6 +1258,9 @@ [AU0828_VMUX_DEBUG] = "tv debug" }; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + tmp = input->index; if (tmp >= AU0828_MAX_INPUT) @@ -1420,15 +1279,18 @@ input->audioset = 2; } - input->std = dev->vdev->tvnorms; + input->std = dev->vdev.tvnorms; return 0; } static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + *i = dev->ctrl_input; return 0; } @@ -1437,6 +1299,9 @@ { int i; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + switch (AUVI_INPUT(index).type) { case AU0828_VMUX_SVIDEO: dev->input_type = AU0828_VMUX_SVIDEO; @@ -1486,8 +1351,7 @@ static int vidioc_s_input(struct file *file, void *priv, unsigned int index) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); dprintk(1, "VIDIOC_S_INPUT in function %s, input=%d\n", __func__, index); @@ -1505,6 +1369,8 @@ if (a->index > 1) return -EINVAL; + dprintk(1, "%s called\n", __func__); + if (a->index == 0) strcpy(a->name, "Television"); else @@ -1516,8 +1382,10 @@ static int vidioc_g_audio(struct file *file, void *priv, struct v4l2_audio *a) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); a->index = dev->ctrl_ainput; if (a->index == 0) @@ -1531,22 +1399,26 @@ static int vidioc_s_audio(struct file *file, void *priv, const struct v4l2_audio *a) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); if (a->index != dev->ctrl_ainput) return -EINVAL; + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); return 0; } static int vidioc_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); if (t->index != 0) return -EINVAL; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + strcpy(t->name, "Auvitek tuner"); au0828_init_tuner(dev); @@ -1559,12 +1431,14 @@ static int vidioc_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); if (t->index != 0) return -EINVAL; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + au0828_init_tuner(dev); i2c_gate_ctrl(dev, 1); v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); @@ -1580,11 +1454,12 @@ static int vidioc_g_frequency(struct file *file, void *priv, struct v4l2_frequency *freq) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); if (freq->tuner != 0) return -EINVAL; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); freq->frequency = dev->ctrl_freq; return 0; } @@ -1592,13 +1467,15 @@ static int vidioc_s_frequency(struct file *file, void *priv, const struct v4l2_frequency *freq) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); struct v4l2_frequency new_freq = *freq; if (freq->tuner != 0) return -EINVAL; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + au0828_init_tuner(dev); i2c_gate_ctrl(dev, 1); @@ -1620,8 +1497,10 @@ static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv, struct v4l2_format *format) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); format->fmt.vbi.samples_per_line = dev->vbi_width; format->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY; @@ -1638,35 +1517,17 @@ return 0; } -static int vidioc_g_chip_ident(struct file *file, void *priv, - struct v4l2_dbg_chip_ident *chip) -{ - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - chip->ident = V4L2_IDENT_NONE; - chip->revision = 0; - - if (v4l2_chip_match_host(&chip->match)) { - chip->ident = V4L2_IDENT_AU0828; - return 0; - } - - v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_chip_ident, chip); - if (chip->ident == V4L2_IDENT_NONE) - return -EINVAL; - - return 0; -} - static int vidioc_cropcap(struct file *file, void *priv, struct v4l2_cropcap *cc) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) return -EINVAL; + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); + cc->bounds.left = 0; cc->bounds.top = 0; cc->bounds.width = dev->width; @@ -1680,132 +1541,28 @@ return 0; } -static int vidioc_streamon(struct file *file, void *priv, - enum v4l2_buf_type type) -{ - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - int rc = -EINVAL; - - rc = check_dev(dev); - if (rc < 0) - return rc; - - if (unlikely(type != fh->type)) - return -EINVAL; - - dprintk(1, "vidioc_streamon fh=%p t=%d fh->res=%d dev->res=%d\n", - fh, type, fh->resources, dev->resources); - - if (unlikely(!res_get(fh, get_ressource(fh)))) - return -EBUSY; - - au0828_init_tuner(dev); - if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { - au0828_analog_stream_enable(dev); - v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 1); - } - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { - rc = videobuf_streamon(&fh->vb_vidq); - dev->vid_timeout_running = 1; - mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); - } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { - rc = videobuf_streamon(&fh->vb_vbiq); - dev->vbi_timeout_running = 1; - mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); - } - - return rc; -} - -static int vidioc_streamoff(struct file *file, void *priv, - enum v4l2_buf_type type) -{ - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - int rc; - int i; - - rc = check_dev(dev); - if (rc < 0) - return rc; - - if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE && - fh->type != V4L2_BUF_TYPE_VBI_CAPTURE) - return -EINVAL; - if (type != fh->type) - return -EINVAL; - - dprintk(1, "vidioc_streamoff fh=%p t=%d fh->res=%d dev->res=%d\n", - fh, type, fh->resources, dev->resources); - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { - dev->vid_timeout_running = 0; - del_timer_sync(&dev->vid_timeout); - - v4l2_device_call_all(&dev->v4l2_dev, 0, video, s_stream, 0); - rc = au0828_stream_interrupt(dev); - if (rc != 0) - return rc; - - for (i = 0; i < AU0828_MAX_INPUT; i++) { - if (AUVI_INPUT(i).audio_setup == NULL) - continue; - (AUVI_INPUT(i).audio_setup)(dev, 0); - } - - if (res_check(fh, AU0828_RESOURCE_VIDEO)) { - videobuf_streamoff(&fh->vb_vidq); - res_free(fh, AU0828_RESOURCE_VIDEO); - } - } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) { - dev->vbi_timeout_running = 0; - del_timer_sync(&dev->vbi_timeout); - - if (res_check(fh, AU0828_RESOURCE_VBI)) { - videobuf_streamoff(&fh->vb_vbiq); - res_free(fh, AU0828_RESOURCE_VBI); - } - } - - return 0; -} - #ifdef CONFIG_VIDEO_ADV_DEBUG static int vidioc_g_register(struct file *file, void *priv, struct v4l2_dbg_register *reg) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); - switch (reg->match.type) { - case V4L2_CHIP_MATCH_I2C_DRIVER: - v4l2_device_call_all(&dev->v4l2_dev, 0, core, g_register, reg); - return 0; - default: - if (!v4l2_chip_match_host(®->match)) - return -EINVAL; - } + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); reg->val = au0828_read(dev, reg->reg); + reg->size = 1; return 0; } static int vidioc_s_register(struct file *file, void *priv, const struct v4l2_dbg_register *reg) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; + struct au0828_dev *dev = video_drvdata(file); + + dprintk(1, "%s called std_set %d dev_state %ld\n", __func__, + dev->std_set_in_tuner_core, dev->dev_state); - switch (reg->match.type) { - case V4L2_CHIP_MATCH_I2C_DRIVER: - v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_register, reg); - return 0; - default: - if (!v4l2_chip_match_host(®->match)) - return -EINVAL; - } return au0828_writereg(dev, reg->reg, reg->val); } #endif @@ -1814,100 +1571,83 @@ { struct video_device *vdev = video_devdata(file); + dprintk(1, "%s called\n", __func__); + v4l2_ctrl_log_status(file, fh); v4l2_device_call_all(vdev->v4l2_dev, 0, core, log_status); return 0; } -static int vidioc_reqbufs(struct file *file, void *priv, - struct v4l2_requestbuffers *rb) -{ - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - int rc; - - rc = check_dev(dev); - if (rc < 0) - return rc; - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) - rc = videobuf_reqbufs(&fh->vb_vidq, rb); - else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) - rc = videobuf_reqbufs(&fh->vb_vbiq, rb); - - return rc; -} - -static int vidioc_querybuf(struct file *file, void *priv, - struct v4l2_buffer *b) +void au0828_v4l2_suspend(struct au0828_dev *dev) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - int rc; + struct urb *urb; + int i; - rc = check_dev(dev); - if (rc < 0) - return rc; + pr_info("stopping V4L2\n"); - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) - rc = videobuf_querybuf(&fh->vb_vidq, b); - else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) - rc = videobuf_querybuf(&fh->vb_vbiq, b); + if (dev->stream_state == STREAM_ON) { + pr_info("stopping V4L2 active URBs\n"); + au0828_analog_stream_disable(dev); + /* stop urbs */ + for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { + urb = dev->isoc_ctl.urb[i]; + if (urb) { + if (!irqs_disabled()) + usb_kill_urb(urb); + else + usb_unlink_urb(urb); + } + } + } - return rc; + if (dev->vid_timeout_running) + del_timer_sync(&dev->vid_timeout); + if (dev->vbi_timeout_running) + del_timer_sync(&dev->vbi_timeout); } -static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b) +void au0828_v4l2_resume(struct au0828_dev *dev) { - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - int rc; + int i, rc; - rc = check_dev(dev); - if (rc < 0) - return rc; + pr_info("restarting V4L2\n"); - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) - rc = videobuf_qbuf(&fh->vb_vidq, b); - else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) - rc = videobuf_qbuf(&fh->vb_vbiq, b); + if (dev->stream_state == STREAM_ON) { + au0828_stream_interrupt(dev); + au0828_init_tuner(dev); + } - return rc; -} + if (dev->vid_timeout_running) + mod_timer(&dev->vid_timeout, jiffies + (HZ / 10)); + if (dev->vbi_timeout_running) + mod_timer(&dev->vbi_timeout, jiffies + (HZ / 10)); -static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b) -{ - struct au0828_fh *fh = priv; - struct au0828_dev *dev = fh->dev; - int rc; + /* If we were doing ac97 instead of i2s, it would go here...*/ + au0828_i2s_init(dev); - rc = check_dev(dev); - if (rc < 0) - return rc; + au0828_analog_stream_enable(dev); - /* Workaround for a bug in the au0828 hardware design that sometimes - results in the colorspace being inverted */ - if (dev->greenscreen_detected == 1) { - dprintk(1, "Detected green frame. Resetting stream...\n"); + if (!(dev->stream_state == STREAM_ON)) { au0828_analog_stream_reset(dev); - dev->greenscreen_detected = 0; + /* submit urbs */ + for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { + rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); + if (rc) { + au0828_isocdbg("submit of urb %i failed (error=%i)\n", + i, rc); + au0828_uninit_isoc(dev); + } + } } - - if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) - rc = videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK); - else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) - rc = videobuf_dqbuf(&fh->vb_vbiq, b, file->f_flags & O_NONBLOCK); - - return rc; } static struct v4l2_file_operations au0828_v4l_fops = { .owner = THIS_MODULE, .open = au0828_v4l2_open, .release = au0828_v4l2_close, - .read = au0828_v4l2_read, - .poll = au0828_v4l2_poll, - .mmap = au0828_v4l2_mmap, + .read = vb2_fop_read, + .poll = vb2_fop_poll, + .mmap = vb2_fop_mmap, .unlocked_ioctl = video_ioctl2, }; @@ -1924,17 +1664,24 @@ .vidioc_g_audio = vidioc_g_audio, .vidioc_s_audio = vidioc_s_audio, .vidioc_cropcap = vidioc_cropcap, - .vidioc_reqbufs = vidioc_reqbufs, - .vidioc_querybuf = vidioc_querybuf, - .vidioc_qbuf = vidioc_qbuf, - .vidioc_dqbuf = vidioc_dqbuf, + + .vidioc_reqbufs = vb2_ioctl_reqbufs, + .vidioc_create_bufs = vb2_ioctl_create_bufs, + .vidioc_prepare_buf = vb2_ioctl_prepare_buf, + .vidioc_querybuf = vb2_ioctl_querybuf, + .vidioc_qbuf = vb2_ioctl_qbuf, + .vidioc_dqbuf = vb2_ioctl_dqbuf, + .vidioc_expbuf = vb2_ioctl_expbuf, + .vidioc_s_std = vidioc_s_std, .vidioc_g_std = vidioc_g_std, .vidioc_enum_input = vidioc_enum_input, .vidioc_g_input = vidioc_g_input, .vidioc_s_input = vidioc_s_input, - .vidioc_streamon = vidioc_streamon, - .vidioc_streamoff = vidioc_streamoff, + + .vidioc_streamon = vb2_ioctl_streamon, + .vidioc_streamoff = vb2_ioctl_streamoff, + .vidioc_g_tuner = vidioc_g_tuner, .vidioc_s_tuner = vidioc_s_tuner, .vidioc_g_frequency = vidioc_g_frequency, @@ -1943,7 +1690,6 @@ .vidioc_g_register = vidioc_g_register, .vidioc_s_register = vidioc_s_register, #endif - .vidioc_g_chip_ident = vidioc_g_chip_ident, .vidioc_log_status = vidioc_log_status, .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, .vidioc_unsubscribe_event = v4l2_event_unsubscribe, @@ -1951,11 +1697,47 @@ static const struct video_device au0828_video_template = { .fops = &au0828_v4l_fops, - .release = video_device_release, + .release = video_device_release_empty, .ioctl_ops = &video_ioctl_ops, - .tvnorms = V4L2_STD_NTSC_M, + .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_M, }; +static int au0828_vb2_setup(struct au0828_dev *dev) +{ + int rc; + struct vb2_queue *q; + + /* Setup Videobuf2 for Video capture */ + q = &dev->vb_vidq; + q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; + q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + q->drv_priv = dev; + q->buf_struct_size = sizeof(struct au0828_buffer); + q->ops = &au0828_video_qops; + q->mem_ops = &vb2_vmalloc_memops; + + rc = vb2_queue_init(q); + if (rc < 0) + return rc; + + /* Setup Videobuf2 for VBI capture */ + q = &dev->vb_vbiq; + q->type = V4L2_BUF_TYPE_VBI_CAPTURE; + q->io_modes = VB2_READ | VB2_MMAP | VB2_USERPTR | VB2_DMABUF; + q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; + q->drv_priv = dev; + q->buf_struct_size = sizeof(struct au0828_buffer); + q->ops = &au0828_vbi_qops; + q->mem_ops = &vb2_vmalloc_memops; + + rc = vb2_queue_init(q); + if (rc < 0) + return rc; + + return 0; +} + /**************************************************************************/ int au0828_analog_register(struct au0828_dev *dev, @@ -1966,13 +1748,14 @@ struct usb_endpoint_descriptor *endpoint; int i, ret; - dprintk(1, "au0828_analog_register called!\n"); + dprintk(1, "au0828_analog_register called for intf#%d!\n", + interface->cur_altsetting->desc.bInterfaceNumber); /* set au0828 usb interface0 to as5 */ retval = usb_set_interface(dev->usbdev, interface->cur_altsetting->desc.bInterfaceNumber, 5); if (retval != 0) { - printk(KERN_INFO "Failure setting usb interface0 to as5\n"); + pr_info("Failure setting usb interface0 to as5\n"); return retval; } @@ -1990,11 +1773,13 @@ dev->max_pkt_size = (tmp & 0x07ff) * (((tmp & 0x1800) >> 11) + 1); dev->isoc_in_endpointaddr = endpoint->bEndpointAddress; + dprintk(1, + "Found isoc endpoint 0x%02x, max size = %d\n", + dev->isoc_in_endpointaddr, dev->max_pkt_size); } } if (!(dev->isoc_in_endpointaddr)) { - printk(KERN_INFO "Could not locate isoc endpoint\n"); - kfree(dev); + pr_info("Could not locate isoc endpoint\n"); return -ENODEV; } @@ -2003,17 +1788,12 @@ /* init video dma queues */ INIT_LIST_HEAD(&dev->vidq.active); - INIT_LIST_HEAD(&dev->vidq.queued); INIT_LIST_HEAD(&dev->vbiq.active); - INIT_LIST_HEAD(&dev->vbiq.queued); - dev->vid_timeout.function = au0828_vid_buffer_timeout; - dev->vid_timeout.data = (unsigned long) dev; - init_timer(&dev->vid_timeout); - - dev->vbi_timeout.function = au0828_vbi_buffer_timeout; - dev->vbi_timeout.data = (unsigned long) dev; - init_timer(&dev->vbi_timeout); + setup_timer(&dev->vid_timeout, au0828_vid_buffer_timeout, + (unsigned long)dev); + setup_timer(&dev->vbi_timeout, au0828_vbi_buffer_timeout, + (unsigned long)dev); dev->width = NTSC_STD_W; dev->height = NTSC_STD_H; @@ -2027,63 +1807,62 @@ dev->std = V4L2_STD_NTSC_M; au0828_s_input(dev, 0); - /* allocate and fill v4l2 video struct */ - dev->vdev = video_device_alloc(); - if (NULL == dev->vdev) { - dprintk(1, "Can't allocate video_device.\n"); - return -ENOMEM; - } - - /* allocate the VBI struct */ - dev->vbi_dev = video_device_alloc(); - if (NULL == dev->vbi_dev) { - dprintk(1, "Can't allocate vbi_device.\n"); - ret = -ENOMEM; - goto err_vdev; - } + mutex_init(&dev->vb_queue_lock); + mutex_init(&dev->vb_vbi_queue_lock); /* Fill the video capture device struct */ - *dev->vdev = au0828_video_template; - dev->vdev->v4l2_dev = &dev->v4l2_dev; - dev->vdev->lock = &dev->lock; - set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev->flags); - strcpy(dev->vdev->name, "au0828a video"); + dev->vdev = au0828_video_template; + dev->vdev.v4l2_dev = &dev->v4l2_dev; + dev->vdev.lock = &dev->lock; + dev->vdev.queue = &dev->vb_vidq; + dev->vdev.queue->lock = &dev->vb_queue_lock; + strcpy(dev->vdev.name, "au0828a video"); /* Setup the VBI device */ - *dev->vbi_dev = au0828_video_template; - dev->vbi_dev->v4l2_dev = &dev->v4l2_dev; - dev->vbi_dev->lock = &dev->lock; - set_bit(V4L2_FL_USE_FH_PRIO, &dev->vbi_dev->flags); - strcpy(dev->vbi_dev->name, "au0828a vbi"); + dev->vbi_dev = au0828_video_template; + dev->vbi_dev.v4l2_dev = &dev->v4l2_dev; + dev->vbi_dev.lock = &dev->lock; + dev->vbi_dev.queue = &dev->vb_vbiq; + dev->vbi_dev.queue->lock = &dev->vb_vbi_queue_lock; + strcpy(dev->vbi_dev.name, "au0828a vbi"); + + /* initialize videobuf2 stuff */ + retval = au0828_vb2_setup(dev); + if (retval != 0) { + dprintk(1, "unable to setup videobuf2 queues (error = %d).\n", + retval); + return -ENODEV; + } /* Register the v4l2 device */ - video_set_drvdata(dev->vdev, dev); - retval = video_register_device(dev->vdev, VFL_TYPE_GRABBER, -1); + video_set_drvdata(&dev->vdev, dev); + retval = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1); if (retval != 0) { dprintk(1, "unable to register video device (error = %d).\n", retval); ret = -ENODEV; - goto err_vbi_dev; + goto err_reg_vdev; } /* Register the vbi device */ - video_set_drvdata(dev->vbi_dev, dev); - retval = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, -1); + video_set_drvdata(&dev->vbi_dev, dev); + retval = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI, -1); if (retval != 0) { dprintk(1, "unable to register vbi device (error = %d).\n", retval); ret = -ENODEV; - goto err_vbi_dev; + goto err_reg_vbi_dev; } dprintk(1, "%s completed!\n", __func__); return 0; -err_vbi_dev: - video_device_release(dev->vbi_dev); -err_vdev: - video_device_release(dev->vdev); +err_reg_vbi_dev: + video_unregister_device(&dev->vdev); +err_reg_vdev: + vb2_queue_release(&dev->vb_vidq); + vb2_queue_release(&dev->vb_vbiq); return ret; }