1*0c0d06caSMauro Carvalho Chehab /* 2*0c0d06caSMauro Carvalho Chehab au0828-vbi.c - VBI driver for au0828 3*0c0d06caSMauro Carvalho Chehab 4*0c0d06caSMauro Carvalho Chehab Copyright (C) 2010 Devin Heitmueller <dheitmueller@kernellabs.com> 5*0c0d06caSMauro Carvalho Chehab 6*0c0d06caSMauro Carvalho Chehab This work was sponsored by GetWellNetwork Inc. 7*0c0d06caSMauro Carvalho Chehab 8*0c0d06caSMauro Carvalho Chehab This program is free software; you can redistribute it and/or modify 9*0c0d06caSMauro Carvalho Chehab it under the terms of the GNU General Public License as published by 10*0c0d06caSMauro Carvalho Chehab the Free Software Foundation; either version 2 of the License, or 11*0c0d06caSMauro Carvalho Chehab (at your option) any later version. 12*0c0d06caSMauro Carvalho Chehab 13*0c0d06caSMauro Carvalho Chehab This program is distributed in the hope that it will be useful, 14*0c0d06caSMauro Carvalho Chehab but WITHOUT ANY WARRANTY; without even the implied warranty of 15*0c0d06caSMauro Carvalho Chehab MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16*0c0d06caSMauro Carvalho Chehab GNU General Public License for more details. 17*0c0d06caSMauro Carvalho Chehab 18*0c0d06caSMauro Carvalho Chehab You should have received a copy of the GNU General Public License 19*0c0d06caSMauro Carvalho Chehab along with this program; if not, write to the Free Software 20*0c0d06caSMauro Carvalho Chehab Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 21*0c0d06caSMauro Carvalho Chehab 02110-1301, USA. 22*0c0d06caSMauro Carvalho Chehab */ 23*0c0d06caSMauro Carvalho Chehab 24*0c0d06caSMauro Carvalho Chehab #include <linux/kernel.h> 25*0c0d06caSMauro Carvalho Chehab #include <linux/module.h> 26*0c0d06caSMauro Carvalho Chehab #include <linux/init.h> 27*0c0d06caSMauro Carvalho Chehab #include <linux/slab.h> 28*0c0d06caSMauro Carvalho Chehab 29*0c0d06caSMauro Carvalho Chehab #include "au0828.h" 30*0c0d06caSMauro Carvalho Chehab 31*0c0d06caSMauro Carvalho Chehab static unsigned int vbibufs = 5; 32*0c0d06caSMauro Carvalho Chehab module_param(vbibufs, int, 0644); 33*0c0d06caSMauro Carvalho Chehab MODULE_PARM_DESC(vbibufs, "number of vbi buffers, range 2-32"); 34*0c0d06caSMauro Carvalho Chehab 35*0c0d06caSMauro Carvalho Chehab /* ------------------------------------------------------------------ */ 36*0c0d06caSMauro Carvalho Chehab 37*0c0d06caSMauro Carvalho Chehab static void 38*0c0d06caSMauro Carvalho Chehab free_buffer(struct videobuf_queue *vq, struct au0828_buffer *buf) 39*0c0d06caSMauro Carvalho Chehab { 40*0c0d06caSMauro Carvalho Chehab struct au0828_fh *fh = vq->priv_data; 41*0c0d06caSMauro Carvalho Chehab struct au0828_dev *dev = fh->dev; 42*0c0d06caSMauro Carvalho Chehab unsigned long flags = 0; 43*0c0d06caSMauro Carvalho Chehab if (in_interrupt()) 44*0c0d06caSMauro Carvalho Chehab BUG(); 45*0c0d06caSMauro Carvalho Chehab 46*0c0d06caSMauro Carvalho Chehab /* We used to wait for the buffer to finish here, but this didn't work 47*0c0d06caSMauro Carvalho Chehab because, as we were keeping the state as VIDEOBUF_QUEUED, 48*0c0d06caSMauro Carvalho Chehab videobuf_queue_cancel marked it as finished for us. 49*0c0d06caSMauro Carvalho Chehab (Also, it could wedge forever if the hardware was misconfigured.) 50*0c0d06caSMauro Carvalho Chehab 51*0c0d06caSMauro Carvalho Chehab This should be safe; by the time we get here, the buffer isn't 52*0c0d06caSMauro Carvalho Chehab queued anymore. If we ever start marking the buffers as 53*0c0d06caSMauro Carvalho Chehab VIDEOBUF_ACTIVE, it won't be, though. 54*0c0d06caSMauro Carvalho Chehab */ 55*0c0d06caSMauro Carvalho Chehab spin_lock_irqsave(&dev->slock, flags); 56*0c0d06caSMauro Carvalho Chehab if (dev->isoc_ctl.vbi_buf == buf) 57*0c0d06caSMauro Carvalho Chehab dev->isoc_ctl.vbi_buf = NULL; 58*0c0d06caSMauro Carvalho Chehab spin_unlock_irqrestore(&dev->slock, flags); 59*0c0d06caSMauro Carvalho Chehab 60*0c0d06caSMauro Carvalho Chehab videobuf_vmalloc_free(&buf->vb); 61*0c0d06caSMauro Carvalho Chehab buf->vb.state = VIDEOBUF_NEEDS_INIT; 62*0c0d06caSMauro Carvalho Chehab } 63*0c0d06caSMauro Carvalho Chehab 64*0c0d06caSMauro Carvalho Chehab static int 65*0c0d06caSMauro Carvalho Chehab vbi_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size) 66*0c0d06caSMauro Carvalho Chehab { 67*0c0d06caSMauro Carvalho Chehab struct au0828_fh *fh = q->priv_data; 68*0c0d06caSMauro Carvalho Chehab struct au0828_dev *dev = fh->dev; 69*0c0d06caSMauro Carvalho Chehab 70*0c0d06caSMauro Carvalho Chehab *size = dev->vbi_width * dev->vbi_height * 2; 71*0c0d06caSMauro Carvalho Chehab 72*0c0d06caSMauro Carvalho Chehab if (0 == *count) 73*0c0d06caSMauro Carvalho Chehab *count = vbibufs; 74*0c0d06caSMauro Carvalho Chehab if (*count < 2) 75*0c0d06caSMauro Carvalho Chehab *count = 2; 76*0c0d06caSMauro Carvalho Chehab if (*count > 32) 77*0c0d06caSMauro Carvalho Chehab *count = 32; 78*0c0d06caSMauro Carvalho Chehab return 0; 79*0c0d06caSMauro Carvalho Chehab } 80*0c0d06caSMauro Carvalho Chehab 81*0c0d06caSMauro Carvalho Chehab static int 82*0c0d06caSMauro Carvalho Chehab vbi_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, 83*0c0d06caSMauro Carvalho Chehab enum v4l2_field field) 84*0c0d06caSMauro Carvalho Chehab { 85*0c0d06caSMauro Carvalho Chehab struct au0828_fh *fh = q->priv_data; 86*0c0d06caSMauro Carvalho Chehab struct au0828_dev *dev = fh->dev; 87*0c0d06caSMauro Carvalho Chehab struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb); 88*0c0d06caSMauro Carvalho Chehab int rc = 0; 89*0c0d06caSMauro Carvalho Chehab 90*0c0d06caSMauro Carvalho Chehab buf->vb.size = dev->vbi_width * dev->vbi_height * 2; 91*0c0d06caSMauro Carvalho Chehab 92*0c0d06caSMauro Carvalho Chehab if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) 93*0c0d06caSMauro Carvalho Chehab return -EINVAL; 94*0c0d06caSMauro Carvalho Chehab 95*0c0d06caSMauro Carvalho Chehab buf->vb.width = dev->vbi_width; 96*0c0d06caSMauro Carvalho Chehab buf->vb.height = dev->vbi_height; 97*0c0d06caSMauro Carvalho Chehab buf->vb.field = field; 98*0c0d06caSMauro Carvalho Chehab 99*0c0d06caSMauro Carvalho Chehab if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { 100*0c0d06caSMauro Carvalho Chehab rc = videobuf_iolock(q, &buf->vb, NULL); 101*0c0d06caSMauro Carvalho Chehab if (rc < 0) 102*0c0d06caSMauro Carvalho Chehab goto fail; 103*0c0d06caSMauro Carvalho Chehab } 104*0c0d06caSMauro Carvalho Chehab 105*0c0d06caSMauro Carvalho Chehab buf->vb.state = VIDEOBUF_PREPARED; 106*0c0d06caSMauro Carvalho Chehab return 0; 107*0c0d06caSMauro Carvalho Chehab 108*0c0d06caSMauro Carvalho Chehab fail: 109*0c0d06caSMauro Carvalho Chehab free_buffer(q, buf); 110*0c0d06caSMauro Carvalho Chehab return rc; 111*0c0d06caSMauro Carvalho Chehab } 112*0c0d06caSMauro Carvalho Chehab 113*0c0d06caSMauro Carvalho Chehab static void 114*0c0d06caSMauro Carvalho Chehab vbi_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) 115*0c0d06caSMauro Carvalho Chehab { 116*0c0d06caSMauro Carvalho Chehab struct au0828_buffer *buf = container_of(vb, 117*0c0d06caSMauro Carvalho Chehab struct au0828_buffer, 118*0c0d06caSMauro Carvalho Chehab vb); 119*0c0d06caSMauro Carvalho Chehab struct au0828_fh *fh = vq->priv_data; 120*0c0d06caSMauro Carvalho Chehab struct au0828_dev *dev = fh->dev; 121*0c0d06caSMauro Carvalho Chehab struct au0828_dmaqueue *vbiq = &dev->vbiq; 122*0c0d06caSMauro Carvalho Chehab 123*0c0d06caSMauro Carvalho Chehab buf->vb.state = VIDEOBUF_QUEUED; 124*0c0d06caSMauro Carvalho Chehab list_add_tail(&buf->vb.queue, &vbiq->active); 125*0c0d06caSMauro Carvalho Chehab } 126*0c0d06caSMauro Carvalho Chehab 127*0c0d06caSMauro Carvalho Chehab static void vbi_release(struct videobuf_queue *q, struct videobuf_buffer *vb) 128*0c0d06caSMauro Carvalho Chehab { 129*0c0d06caSMauro Carvalho Chehab struct au0828_buffer *buf = container_of(vb, struct au0828_buffer, vb); 130*0c0d06caSMauro Carvalho Chehab free_buffer(q, buf); 131*0c0d06caSMauro Carvalho Chehab } 132*0c0d06caSMauro Carvalho Chehab 133*0c0d06caSMauro Carvalho Chehab struct videobuf_queue_ops au0828_vbi_qops = { 134*0c0d06caSMauro Carvalho Chehab .buf_setup = vbi_setup, 135*0c0d06caSMauro Carvalho Chehab .buf_prepare = vbi_prepare, 136*0c0d06caSMauro Carvalho Chehab .buf_queue = vbi_queue, 137*0c0d06caSMauro Carvalho Chehab .buf_release = vbi_release, 138*0c0d06caSMauro Carvalho Chehab }; 139