Path: blob/master/drivers/media/video/cx231xx/cx231xx-vbi.c
17988 views
/*1cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices23Copyright (C) 2008 <srinivasa.deevi at conexant dot com>4Based on cx88 driver56This program is free software; you can redistribute it and/or modify7it under the terms of the GNU General Public License as published by8the Free Software Foundation; either version 2 of the License, or9(at your option) any later version.1011This program is distributed in the hope that it will be useful,12but WITHOUT ANY WARRANTY; without even the implied warranty of13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the14GNU General Public License for more details.1516You should have received a copy of the GNU General Public License17along with this program; if not, write to the Free Software18Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.19*/2021#include <linux/init.h>22#include <linux/list.h>23#include <linux/module.h>24#include <linux/kernel.h>25#include <linux/bitmap.h>26#include <linux/usb.h>27#include <linux/i2c.h>28#include <linux/mm.h>29#include <linux/mutex.h>30#include <linux/slab.h>3132#include <media/v4l2-common.h>33#include <media/v4l2-ioctl.h>34#include <media/v4l2-chip-ident.h>35#include <media/msp3400.h>36#include <media/tuner.h>3738#include "cx231xx.h"39#include "cx231xx-vbi.h"4041static inline void print_err_status(struct cx231xx *dev, int packet, int status)42{43char *errmsg = "Unknown";4445switch (status) {46case -ENOENT:47errmsg = "unlinked synchronuously";48break;49case -ECONNRESET:50errmsg = "unlinked asynchronuously";51break;52case -ENOSR:53errmsg = "Buffer error (overrun)";54break;55case -EPIPE:56errmsg = "Stalled (device not responding)";57break;58case -EOVERFLOW:59errmsg = "Babble (bad cable?)";60break;61case -EPROTO:62errmsg = "Bit-stuff error (bad cable?)";63break;64case -EILSEQ:65errmsg = "CRC/Timeout (could be anything)";66break;67case -ETIME:68errmsg = "Device does not respond";69break;70}71if (packet < 0) {72cx231xx_err(DRIVER_NAME "URB status %d [%s].\n", status,73errmsg);74} else {75cx231xx_err(DRIVER_NAME "URB packet %d, status %d [%s].\n",76packet, status, errmsg);77}78}7980/*81* Controls the isoc copy of each urb packet82*/83static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)84{85struct cx231xx_buffer *buf;86struct cx231xx_dmaqueue *dma_q = urb->context;87int rc = 1;88unsigned char *p_buffer;89u32 bytes_parsed = 0, buffer_size = 0;90u8 sav_eav = 0;9192if (!dev)93return 0;9495if ((dev->state & DEV_DISCONNECTED) || (dev->state & DEV_MISCONFIGURED))96return 0;9798if (urb->status < 0) {99print_err_status(dev, -1, urb->status);100if (urb->status == -ENOENT)101return 0;102}103104buf = dev->vbi_mode.bulk_ctl.buf;105106/* get buffer pointer and length */107p_buffer = urb->transfer_buffer;108buffer_size = urb->actual_length;109110if (buffer_size > 0) {111bytes_parsed = 0;112113if (dma_q->is_partial_line) {114/* Handle the case where we were working on a partial115line */116sav_eav = dma_q->last_sav;117} else {118/* Check for a SAV/EAV overlapping the119buffer boundary */120121sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,122dma_q->partial_buf,123&bytes_parsed);124}125126sav_eav &= 0xF0;127/* Get the first line if we have some portion of an SAV/EAV from128the last buffer or a partial line */129if (sav_eav) {130bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,131sav_eav, /* SAV/EAV */132p_buffer + bytes_parsed, /* p_buffer */133buffer_size - bytes_parsed); /* buffer size */134}135136/* Now parse data that is completely in this buffer */137dma_q->is_partial_line = 0;138139while (bytes_parsed < buffer_size) {140u32 bytes_used = 0;141142sav_eav = cx231xx_find_next_SAV_EAV(143p_buffer + bytes_parsed, /* p_buffer */144buffer_size - bytes_parsed, /* buffer size */145&bytes_used); /* bytes used to get SAV/EAV */146147bytes_parsed += bytes_used;148149sav_eav &= 0xF0;150if (sav_eav && (bytes_parsed < buffer_size)) {151bytes_parsed += cx231xx_get_vbi_line(dev,152dma_q, sav_eav, /* SAV/EAV */153p_buffer+bytes_parsed, /* p_buffer */154buffer_size-bytes_parsed);/*buf size*/155}156}157158/* Save the last four bytes of the buffer so we can159check the buffer boundary condition next time */160memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);161bytes_parsed = 0;162}163164return rc;165}166167/* ------------------------------------------------------------------168Vbi buf operations169------------------------------------------------------------------*/170171static int172vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,173unsigned int *size)174{175struct cx231xx_fh *fh = vq->priv_data;176struct cx231xx *dev = fh->dev;177u32 height = 0;178179height = ((dev->norm & V4L2_STD_625_50) ?180PAL_VBI_LINES : NTSC_VBI_LINES);181182*size = (dev->width * height * 2 * 2);183if (0 == *count)184*count = CX231XX_DEF_VBI_BUF;185186if (*count < CX231XX_MIN_BUF)187*count = CX231XX_MIN_BUF;188189return 0;190}191192/* This is called *without* dev->slock held; please keep it that way */193static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)194{195struct cx231xx_fh *fh = vq->priv_data;196struct cx231xx *dev = fh->dev;197unsigned long flags = 0;198if (in_interrupt())199BUG();200201/* We used to wait for the buffer to finish here, but this didn't work202because, as we were keeping the state as VIDEOBUF_QUEUED,203videobuf_queue_cancel marked it as finished for us.204(Also, it could wedge forever if the hardware was misconfigured.)205206This should be safe; by the time we get here, the buffer isn't207queued anymore. If we ever start marking the buffers as208VIDEOBUF_ACTIVE, it won't be, though.209*/210spin_lock_irqsave(&dev->vbi_mode.slock, flags);211if (dev->vbi_mode.bulk_ctl.buf == buf)212dev->vbi_mode.bulk_ctl.buf = NULL;213spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);214215videobuf_vmalloc_free(&buf->vb);216buf->vb.state = VIDEOBUF_NEEDS_INIT;217}218219static int220vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,221enum v4l2_field field)222{223struct cx231xx_fh *fh = vq->priv_data;224struct cx231xx_buffer *buf =225container_of(vb, struct cx231xx_buffer, vb);226struct cx231xx *dev = fh->dev;227int rc = 0, urb_init = 0;228u32 height = 0;229230height = ((dev->norm & V4L2_STD_625_50) ?231PAL_VBI_LINES : NTSC_VBI_LINES);232buf->vb.size = ((dev->width << 1) * height * 2);233234if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)235return -EINVAL;236237buf->vb.width = dev->width;238buf->vb.height = height;239buf->vb.field = field;240buf->vb.field = V4L2_FIELD_SEQ_TB;241242if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {243rc = videobuf_iolock(vq, &buf->vb, NULL);244if (rc < 0)245goto fail;246}247248if (!dev->vbi_mode.bulk_ctl.num_bufs)249urb_init = 1;250251if (urb_init) {252rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,253CX231XX_NUM_VBI_BUFS,254dev->vbi_mode.alt_max_pkt_size[0],255cx231xx_isoc_vbi_copy);256if (rc < 0)257goto fail;258}259260buf->vb.state = VIDEOBUF_PREPARED;261return 0;262263fail:264free_buffer(vq, buf);265return rc;266}267268static void269vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)270{271struct cx231xx_buffer *buf =272container_of(vb, struct cx231xx_buffer, vb);273struct cx231xx_fh *fh = vq->priv_data;274struct cx231xx *dev = fh->dev;275struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;276277buf->vb.state = VIDEOBUF_QUEUED;278list_add_tail(&buf->vb.queue, &vidq->active);279280}281282static void vbi_buffer_release(struct videobuf_queue *vq,283struct videobuf_buffer *vb)284{285struct cx231xx_buffer *buf =286container_of(vb, struct cx231xx_buffer, vb);287288289free_buffer(vq, buf);290}291292struct videobuf_queue_ops cx231xx_vbi_qops = {293.buf_setup = vbi_buffer_setup,294.buf_prepare = vbi_buffer_prepare,295.buf_queue = vbi_buffer_queue,296.buf_release = vbi_buffer_release,297};298299/* ------------------------------------------------------------------300URB control301------------------------------------------------------------------*/302303/*304* IRQ callback, called by URB callback305*/306static void cx231xx_irq_vbi_callback(struct urb *urb)307{308struct cx231xx_dmaqueue *dma_q = urb->context;309struct cx231xx_video_mode *vmode =310container_of(dma_q, struct cx231xx_video_mode, vidq);311struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);312int rc;313314switch (urb->status) {315case 0: /* success */316case -ETIMEDOUT: /* NAK */317break;318case -ECONNRESET: /* kill */319case -ENOENT:320case -ESHUTDOWN:321return;322default: /* error */323cx231xx_err(DRIVER_NAME "urb completition error %d.\n",324urb->status);325break;326}327328/* Copy data from URB */329spin_lock(&dev->vbi_mode.slock);330rc = dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);331spin_unlock(&dev->vbi_mode.slock);332333/* Reset status */334urb->status = 0;335336urb->status = usb_submit_urb(urb, GFP_ATOMIC);337if (urb->status) {338cx231xx_err(DRIVER_NAME "urb resubmit failed (error=%i)\n",339urb->status);340}341}342343/*344* Stop and Deallocate URBs345*/346void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)347{348struct urb *urb;349int i;350351cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_uninit_vbi_isoc\n");352353dev->vbi_mode.bulk_ctl.nfields = -1;354for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {355urb = dev->vbi_mode.bulk_ctl.urb[i];356if (urb) {357if (!irqs_disabled())358usb_kill_urb(urb);359else360usb_unlink_urb(urb);361362if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {363364kfree(dev->vbi_mode.bulk_ctl.365transfer_buffer[i]);366dev->vbi_mode.bulk_ctl.transfer_buffer[i] =367NULL;368}369usb_free_urb(urb);370dev->vbi_mode.bulk_ctl.urb[i] = NULL;371}372dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;373}374375kfree(dev->vbi_mode.bulk_ctl.urb);376kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);377378dev->vbi_mode.bulk_ctl.urb = NULL;379dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;380dev->vbi_mode.bulk_ctl.num_bufs = 0;381382cx231xx_capture_start(dev, 0, Vbi);383}384EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);385386/*387* Allocate URBs and start IRQ388*/389int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,390int num_bufs, int max_pkt_size,391int (*bulk_copy) (struct cx231xx *dev,392struct urb *urb))393{394struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;395int i;396int sb_size, pipe;397struct urb *urb;398int rc;399400cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_prepare_isoc\n");401402/* De-allocates all pending stuff */403cx231xx_uninit_vbi_isoc(dev);404405/* clear if any halt */406usb_clear_halt(dev->udev,407usb_rcvbulkpipe(dev->udev,408dev->vbi_mode.end_point_addr));409410dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;411dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;412dma_q->pos = 0;413dma_q->is_partial_line = 0;414dma_q->last_sav = 0;415dma_q->current_field = -1;416dma_q->bytes_left_in_line = dev->width << 1;417dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?418PAL_VBI_LINES : NTSC_VBI_LINES);419dma_q->lines_completed = 0;420for (i = 0; i < 8; i++)421dma_q->partial_buf[i] = 0;422423dev->vbi_mode.bulk_ctl.urb = kzalloc(sizeof(void *) * num_bufs,424GFP_KERNEL);425if (!dev->vbi_mode.bulk_ctl.urb) {426cx231xx_errdev("cannot alloc memory for usb buffers\n");427return -ENOMEM;428}429430dev->vbi_mode.bulk_ctl.transfer_buffer =431kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);432if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {433cx231xx_errdev("cannot allocate memory for usbtransfer\n");434kfree(dev->vbi_mode.bulk_ctl.urb);435return -ENOMEM;436}437438dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;439dev->vbi_mode.bulk_ctl.buf = NULL;440441sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;442443/* allocate urbs and transfer buffers */444for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {445446urb = usb_alloc_urb(0, GFP_KERNEL);447if (!urb) {448cx231xx_err(DRIVER_NAME449": cannot alloc bulk_ctl.urb %i\n", i);450cx231xx_uninit_vbi_isoc(dev);451return -ENOMEM;452}453dev->vbi_mode.bulk_ctl.urb[i] = urb;454urb->transfer_flags = 0;455456dev->vbi_mode.bulk_ctl.transfer_buffer[i] =457kzalloc(sb_size, GFP_KERNEL);458if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {459cx231xx_err(DRIVER_NAME460": unable to allocate %i bytes for transfer"461" buffer %i%s\n", sb_size, i,462in_interrupt() ? " while in int" : "");463cx231xx_uninit_vbi_isoc(dev);464return -ENOMEM;465}466467pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);468usb_fill_bulk_urb(urb, dev->udev, pipe,469dev->vbi_mode.bulk_ctl.transfer_buffer[i],470sb_size, cx231xx_irq_vbi_callback, dma_q);471}472473init_waitqueue_head(&dma_q->wq);474475/* submit urbs and enables IRQ */476for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {477rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);478if (rc) {479cx231xx_err(DRIVER_NAME480": submit of urb %i failed (error=%i)\n", i,481rc);482cx231xx_uninit_vbi_isoc(dev);483return rc;484}485}486487cx231xx_capture_start(dev, 1, Vbi);488489return 0;490}491EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);492493u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,494u8 sav_eav, u8 *p_buffer, u32 buffer_size)495{496u32 bytes_copied = 0;497int current_field = -1;498499switch (sav_eav) {500501case SAV_VBI_FIELD1:502current_field = 1;503break;504505case SAV_VBI_FIELD2:506current_field = 2;507break;508default:509break;510}511512if (current_field < 0)513return bytes_copied;514515dma_q->last_sav = sav_eav;516517bytes_copied =518cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,519current_field);520521return bytes_copied;522}523524/*525* Announces that a buffer were filled and request the next526*/527static inline void vbi_buffer_filled(struct cx231xx *dev,528struct cx231xx_dmaqueue *dma_q,529struct cx231xx_buffer *buf)530{531/* Advice that buffer was filled */532/* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */533534buf->vb.state = VIDEOBUF_DONE;535buf->vb.field_count++;536do_gettimeofday(&buf->vb.ts);537538dev->vbi_mode.bulk_ctl.buf = NULL;539540list_del(&buf->vb.queue);541wake_up(&buf->vb.done);542}543544u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,545u8 *p_line, u32 length, int field_number)546{547u32 bytes_to_copy;548struct cx231xx_buffer *buf;549u32 _line_size = dev->width * 2;550551if (dma_q->current_field == -1) {552/* Just starting up */553cx231xx_reset_vbi_buffer(dev, dma_q);554}555556if (dma_q->current_field != field_number)557dma_q->lines_completed = 0;558559/* get the buffer pointer */560buf = dev->vbi_mode.bulk_ctl.buf;561562/* Remember the field number for next time */563dma_q->current_field = field_number;564565bytes_to_copy = dma_q->bytes_left_in_line;566if (bytes_to_copy > length)567bytes_to_copy = length;568569if (dma_q->lines_completed >= dma_q->lines_per_field) {570dma_q->bytes_left_in_line -= bytes_to_copy;571dma_q->is_partial_line =572(dma_q->bytes_left_in_line == 0) ? 0 : 1;573return 0;574}575576dma_q->is_partial_line = 1;577578/* If we don't have a buffer, just return the number of bytes we would579have copied if we had a buffer. */580if (!buf) {581dma_q->bytes_left_in_line -= bytes_to_copy;582dma_q->is_partial_line =583(dma_q->bytes_left_in_line == 0) ? 0 : 1;584return bytes_to_copy;585}586587/* copy the data to video buffer */588cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);589590dma_q->pos += bytes_to_copy;591dma_q->bytes_left_in_line -= bytes_to_copy;592593if (dma_q->bytes_left_in_line == 0) {594595dma_q->bytes_left_in_line = _line_size;596dma_q->lines_completed++;597dma_q->is_partial_line = 0;598599if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {600601vbi_buffer_filled(dev, dma_q, buf);602603dma_q->pos = 0;604dma_q->lines_completed = 0;605cx231xx_reset_vbi_buffer(dev, dma_q);606}607}608609return bytes_to_copy;610}611612/*613* video-buf generic routine to get the next available buffer614*/615static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,616struct cx231xx_buffer **buf)617{618struct cx231xx_video_mode *vmode =619container_of(dma_q, struct cx231xx_video_mode, vidq);620struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);621char *outp;622623if (list_empty(&dma_q->active)) {624cx231xx_err(DRIVER_NAME ": No active queue to serve\n");625dev->vbi_mode.bulk_ctl.buf = NULL;626*buf = NULL;627return;628}629630/* Get the next buffer */631*buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);632633/* Cleans up buffer - Useful for testing for frame/URB loss */634outp = videobuf_to_vmalloc(&(*buf)->vb);635memset(outp, 0, (*buf)->vb.size);636637dev->vbi_mode.bulk_ctl.buf = *buf;638639return;640}641642void cx231xx_reset_vbi_buffer(struct cx231xx *dev,643struct cx231xx_dmaqueue *dma_q)644{645struct cx231xx_buffer *buf;646647buf = dev->vbi_mode.bulk_ctl.buf;648649if (buf == NULL) {650/* first try to get the buffer */651get_next_vbi_buf(dma_q, &buf);652653dma_q->pos = 0;654dma_q->current_field = -1;655}656657dma_q->bytes_left_in_line = dev->width << 1;658dma_q->lines_completed = 0;659}660661int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,662u8 *p_buffer, u32 bytes_to_copy)663{664u8 *p_out_buffer = NULL;665u32 current_line_bytes_copied = 0;666struct cx231xx_buffer *buf;667u32 _line_size = dev->width << 1;668void *startwrite;669int offset, lencopy;670671buf = dev->vbi_mode.bulk_ctl.buf;672673if (buf == NULL)674return -EINVAL;675676p_out_buffer = videobuf_to_vmalloc(&buf->vb);677678if (dma_q->bytes_left_in_line != _line_size) {679current_line_bytes_copied =680_line_size - dma_q->bytes_left_in_line;681}682683offset = (dma_q->lines_completed * _line_size) +684current_line_bytes_copied;685686if (dma_q->current_field == 2) {687/* Populate the second half of the frame */688offset += (dev->width * 2 * dma_q->lines_per_field);689}690691/* prepare destination address */692startwrite = p_out_buffer + offset;693694lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?695bytes_to_copy : dma_q->bytes_left_in_line;696697memcpy(startwrite, p_buffer, lencopy);698699return 0;700}701702u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,703struct cx231xx_dmaqueue *dma_q)704{705u32 height = 0;706707height = ((dev->norm & V4L2_STD_625_50) ?708PAL_VBI_LINES : NTSC_VBI_LINES);709if (dma_q->lines_completed == height && dma_q->current_field == 2)710return 1;711else712return 0;713}714715716