Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 1 | /* |
| 2 | cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices |
| 3 | |
| 4 | Copyright (C) 2008 <srinivasa.deevi at conexant dot com> |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 5 | Based on cx88 driver |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 2 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/kernel.h> |
| 26 | #include <linux/bitmap.h> |
| 27 | #include <linux/usb.h> |
| 28 | #include <linux/i2c.h> |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 29 | #include <linux/mm.h> |
| 30 | #include <linux/mutex.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 31 | #include <linux/slab.h> |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 32 | |
| 33 | #include <media/v4l2-common.h> |
| 34 | #include <media/v4l2-ioctl.h> |
| 35 | #include <media/v4l2-chip-ident.h> |
| 36 | #include <media/msp3400.h> |
| 37 | #include <media/tuner.h> |
| 38 | |
| 39 | #include "cx231xx.h" |
| 40 | #include "cx231xx-vbi.h" |
| 41 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 42 | static inline void print_err_status(struct cx231xx *dev, int packet, int status) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 43 | { |
| 44 | char *errmsg = "Unknown"; |
| 45 | |
| 46 | switch (status) { |
| 47 | case -ENOENT: |
| 48 | errmsg = "unlinked synchronuously"; |
| 49 | break; |
| 50 | case -ECONNRESET: |
| 51 | errmsg = "unlinked asynchronuously"; |
| 52 | break; |
| 53 | case -ENOSR: |
| 54 | errmsg = "Buffer error (overrun)"; |
| 55 | break; |
| 56 | case -EPIPE: |
| 57 | errmsg = "Stalled (device not responding)"; |
| 58 | break; |
| 59 | case -EOVERFLOW: |
| 60 | errmsg = "Babble (bad cable?)"; |
| 61 | break; |
| 62 | case -EPROTO: |
| 63 | errmsg = "Bit-stuff error (bad cable?)"; |
| 64 | break; |
| 65 | case -EILSEQ: |
| 66 | errmsg = "CRC/Timeout (could be anything)"; |
| 67 | break; |
| 68 | case -ETIME: |
| 69 | errmsg = "Device does not respond"; |
| 70 | break; |
| 71 | } |
| 72 | if (packet < 0) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 73 | cx231xx_err(DRIVER_NAME "URB status %d [%s].\n", status, |
| 74 | errmsg); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 75 | } else { |
| 76 | cx231xx_err(DRIVER_NAME "URB packet %d, status %d [%s].\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 77 | packet, status, errmsg); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | |
| 81 | /* |
| 82 | * Controls the isoc copy of each urb packet |
| 83 | */ |
| 84 | static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb) |
| 85 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 86 | struct cx231xx_dmaqueue *dma_q = urb->context; |
| 87 | int rc = 1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 88 | unsigned char *p_buffer; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 89 | u32 bytes_parsed = 0, buffer_size = 0; |
| 90 | u8 sav_eav = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 91 | |
| 92 | if (!dev) |
| 93 | return 0; |
| 94 | |
Mauro Carvalho Chehab | 990862a | 2012-01-10 09:48:50 -0200 | [diff] [blame] | 95 | if (dev->state & DEV_DISCONNECTED) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 96 | return 0; |
| 97 | |
| 98 | if (urb->status < 0) { |
| 99 | print_err_status(dev, -1, urb->status); |
| 100 | if (urb->status == -ENOENT) |
| 101 | return 0; |
| 102 | } |
| 103 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 104 | /* get buffer pointer and length */ |
| 105 | p_buffer = urb->transfer_buffer; |
| 106 | buffer_size = urb->actual_length; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 107 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 108 | if (buffer_size > 0) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 109 | bytes_parsed = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 110 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 111 | if (dma_q->is_partial_line) { |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 112 | /* Handle the case where we were working on a partial |
| 113 | line */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 114 | sav_eav = dma_q->last_sav; |
| 115 | } else { |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 116 | /* Check for a SAV/EAV overlapping the |
| 117 | buffer boundary */ |
| 118 | |
| 119 | sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 120 | dma_q->partial_buf, |
| 121 | &bytes_parsed); |
| 122 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 123 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 124 | sav_eav &= 0xF0; |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 125 | /* Get the first line if we have some portion of an SAV/EAV from |
| 126 | the last buffer or a partial line */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 127 | if (sav_eav) { |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 128 | bytes_parsed += cx231xx_get_vbi_line(dev, dma_q, |
| 129 | sav_eav, /* SAV/EAV */ |
| 130 | p_buffer + bytes_parsed, /* p_buffer */ |
| 131 | buffer_size - bytes_parsed); /* buffer size */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 132 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 133 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 134 | /* Now parse data that is completely in this buffer */ |
| 135 | dma_q->is_partial_line = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 136 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 137 | while (bytes_parsed < buffer_size) { |
| 138 | u32 bytes_used = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 139 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 140 | sav_eav = cx231xx_find_next_SAV_EAV( |
| 141 | p_buffer + bytes_parsed, /* p_buffer */ |
| 142 | buffer_size - bytes_parsed, /* buffer size */ |
| 143 | &bytes_used); /* bytes used to get SAV/EAV */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 144 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 145 | bytes_parsed += bytes_used; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 146 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 147 | sav_eav &= 0xF0; |
| 148 | if (sav_eav && (bytes_parsed < buffer_size)) { |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 149 | bytes_parsed += cx231xx_get_vbi_line(dev, |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 150 | dma_q, sav_eav, /* SAV/EAV */ |
| 151 | p_buffer+bytes_parsed, /* p_buffer */ |
| 152 | buffer_size-bytes_parsed);/*buf size*/ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 153 | } |
| 154 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 155 | |
Sri Deevi | b925517 | 2009-03-04 17:49:01 -0300 | [diff] [blame] | 156 | /* Save the last four bytes of the buffer so we can |
| 157 | check the buffer boundary condition next time */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 158 | memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4); |
| 159 | bytes_parsed = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | return rc; |
| 163 | } |
| 164 | |
| 165 | /* ------------------------------------------------------------------ |
| 166 | Vbi buf operations |
| 167 | ------------------------------------------------------------------*/ |
| 168 | |
| 169 | static int |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 170 | vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count, |
| 171 | unsigned int *size) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 172 | { |
| 173 | struct cx231xx_fh *fh = vq->priv_data; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 174 | struct cx231xx *dev = fh->dev; |
| 175 | u32 height = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 176 | |
| 177 | height = ((dev->norm & V4L2_STD_625_50) ? |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 178 | PAL_VBI_LINES : NTSC_VBI_LINES); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 179 | |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 180 | *size = (dev->width * height * 2 * 2); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 181 | if (0 == *count) |
| 182 | *count = CX231XX_DEF_VBI_BUF; |
| 183 | |
| 184 | if (*count < CX231XX_MIN_BUF) |
| 185 | *count = CX231XX_MIN_BUF; |
| 186 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* This is called *without* dev->slock held; please keep it that way */ |
| 191 | static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf) |
| 192 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 193 | struct cx231xx_fh *fh = vq->priv_data; |
| 194 | struct cx231xx *dev = fh->dev; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 195 | unsigned long flags = 0; |
| 196 | if (in_interrupt()) |
| 197 | BUG(); |
| 198 | |
| 199 | /* We used to wait for the buffer to finish here, but this didn't work |
| 200 | because, as we were keeping the state as VIDEOBUF_QUEUED, |
| 201 | videobuf_queue_cancel marked it as finished for us. |
| 202 | (Also, it could wedge forever if the hardware was misconfigured.) |
| 203 | |
| 204 | This should be safe; by the time we get here, the buffer isn't |
| 205 | queued anymore. If we ever start marking the buffers as |
| 206 | VIDEOBUF_ACTIVE, it won't be, though. |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 207 | */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 208 | spin_lock_irqsave(&dev->vbi_mode.slock, flags); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 209 | if (dev->vbi_mode.bulk_ctl.buf == buf) |
| 210 | dev->vbi_mode.bulk_ctl.buf = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 211 | spin_unlock_irqrestore(&dev->vbi_mode.slock, flags); |
| 212 | |
| 213 | videobuf_vmalloc_free(&buf->vb); |
| 214 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
| 215 | } |
| 216 | |
| 217 | static int |
| 218 | vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 219 | enum v4l2_field field) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 220 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 221 | struct cx231xx_fh *fh = vq->priv_data; |
| 222 | struct cx231xx_buffer *buf = |
| 223 | container_of(vb, struct cx231xx_buffer, vb); |
| 224 | struct cx231xx *dev = fh->dev; |
| 225 | int rc = 0, urb_init = 0; |
| 226 | u32 height = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 227 | |
| 228 | height = ((dev->norm & V4L2_STD_625_50) ? |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 229 | PAL_VBI_LINES : NTSC_VBI_LINES); |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 230 | buf->vb.size = ((dev->width << 1) * height * 2); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 231 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 232 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 233 | return -EINVAL; |
| 234 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 235 | buf->vb.width = dev->width; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 236 | buf->vb.height = height; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 237 | buf->vb.field = field; |
| 238 | buf->vb.field = V4L2_FIELD_SEQ_TB; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 239 | |
| 240 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
| 241 | rc = videobuf_iolock(vq, &buf->vb, NULL); |
| 242 | if (rc < 0) |
| 243 | goto fail; |
| 244 | } |
| 245 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 246 | if (!dev->vbi_mode.bulk_ctl.num_bufs) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 247 | urb_init = 1; |
| 248 | |
| 249 | if (urb_init) { |
| 250 | rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 251 | CX231XX_NUM_VBI_BUFS, |
| 252 | dev->vbi_mode.alt_max_pkt_size[0], |
| 253 | cx231xx_isoc_vbi_copy); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 254 | if (rc < 0) |
| 255 | goto fail; |
| 256 | } |
| 257 | |
| 258 | buf->vb.state = VIDEOBUF_PREPARED; |
| 259 | return 0; |
| 260 | |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 261 | fail: |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 262 | free_buffer(vq, buf); |
| 263 | return rc; |
| 264 | } |
| 265 | |
| 266 | static void |
| 267 | vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 268 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 269 | struct cx231xx_buffer *buf = |
| 270 | container_of(vb, struct cx231xx_buffer, vb); |
| 271 | struct cx231xx_fh *fh = vq->priv_data; |
| 272 | struct cx231xx *dev = fh->dev; |
| 273 | struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 274 | |
| 275 | buf->vb.state = VIDEOBUF_QUEUED; |
| 276 | list_add_tail(&buf->vb.queue, &vidq->active); |
| 277 | |
| 278 | } |
| 279 | |
| 280 | static void vbi_buffer_release(struct videobuf_queue *vq, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 281 | struct videobuf_buffer *vb) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 282 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 283 | struct cx231xx_buffer *buf = |
| 284 | container_of(vb, struct cx231xx_buffer, vb); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 285 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 286 | |
| 287 | free_buffer(vq, buf); |
| 288 | } |
| 289 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 290 | struct videobuf_queue_ops cx231xx_vbi_qops = { |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 291 | .buf_setup = vbi_buffer_setup, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 292 | .buf_prepare = vbi_buffer_prepare, |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 293 | .buf_queue = vbi_buffer_queue, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 294 | .buf_release = vbi_buffer_release, |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 295 | }; |
| 296 | |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 297 | /* ------------------------------------------------------------------ |
| 298 | URB control |
| 299 | ------------------------------------------------------------------*/ |
| 300 | |
| 301 | /* |
| 302 | * IRQ callback, called by URB callback |
| 303 | */ |
| 304 | static void cx231xx_irq_vbi_callback(struct urb *urb) |
| 305 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 306 | struct cx231xx_dmaqueue *dma_q = urb->context; |
| 307 | struct cx231xx_video_mode *vmode = |
| 308 | container_of(dma_q, struct cx231xx_video_mode, vidq); |
| 309 | struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 310 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 311 | switch (urb->status) { |
| 312 | case 0: /* success */ |
| 313 | case -ETIMEDOUT: /* NAK */ |
| 314 | break; |
| 315 | case -ECONNRESET: /* kill */ |
| 316 | case -ENOENT: |
| 317 | case -ESHUTDOWN: |
| 318 | return; |
| 319 | default: /* error */ |
| 320 | cx231xx_err(DRIVER_NAME "urb completition error %d.\n", |
| 321 | urb->status); |
| 322 | break; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* Copy data from URB */ |
| 326 | spin_lock(&dev->vbi_mode.slock); |
Hans Verkuil | da98350 | 2012-05-14 10:14:53 -0300 | [diff] [blame] | 327 | dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 328 | spin_unlock(&dev->vbi_mode.slock); |
| 329 | |
| 330 | /* Reset status */ |
| 331 | urb->status = 0; |
| 332 | |
| 333 | urb->status = usb_submit_urb(urb, GFP_ATOMIC); |
| 334 | if (urb->status) { |
| 335 | cx231xx_err(DRIVER_NAME "urb resubmit failed (error=%i)\n", |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 336 | urb->status); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 337 | } |
| 338 | } |
| 339 | |
| 340 | /* |
| 341 | * Stop and Deallocate URBs |
| 342 | */ |
| 343 | void cx231xx_uninit_vbi_isoc(struct cx231xx *dev) |
| 344 | { |
| 345 | struct urb *urb; |
| 346 | int i; |
| 347 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 348 | cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_uninit_vbi_isoc\n"); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 349 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 350 | dev->vbi_mode.bulk_ctl.nfields = -1; |
| 351 | for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) { |
| 352 | urb = dev->vbi_mode.bulk_ctl.urb[i]; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 353 | if (urb) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 354 | if (!irqs_disabled()) |
| 355 | usb_kill_urb(urb); |
| 356 | else |
| 357 | usb_unlink_urb(urb); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 358 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 359 | if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 360 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 361 | kfree(dev->vbi_mode.bulk_ctl. |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 362 | transfer_buffer[i]); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 363 | dev->vbi_mode.bulk_ctl.transfer_buffer[i] = |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 364 | NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 365 | } |
| 366 | usb_free_urb(urb); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 367 | dev->vbi_mode.bulk_ctl.urb[i] = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 368 | } |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 369 | dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 370 | } |
| 371 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 372 | kfree(dev->vbi_mode.bulk_ctl.urb); |
| 373 | kfree(dev->vbi_mode.bulk_ctl.transfer_buffer); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 374 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 375 | dev->vbi_mode.bulk_ctl.urb = NULL; |
| 376 | dev->vbi_mode.bulk_ctl.transfer_buffer = NULL; |
| 377 | dev->vbi_mode.bulk_ctl.num_bufs = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 378 | |
| 379 | cx231xx_capture_start(dev, 0, Vbi); |
| 380 | } |
| 381 | EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc); |
| 382 | |
| 383 | /* |
| 384 | * Allocate URBs and start IRQ |
| 385 | */ |
| 386 | int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 387 | int num_bufs, int max_pkt_size, |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 388 | int (*bulk_copy) (struct cx231xx *dev, |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 389 | struct urb *urb)) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 390 | { |
| 391 | struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq; |
| 392 | int i; |
| 393 | int sb_size, pipe; |
| 394 | struct urb *urb; |
| 395 | int rc; |
| 396 | |
| 397 | cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_prepare_isoc\n"); |
| 398 | |
| 399 | /* De-allocates all pending stuff */ |
| 400 | cx231xx_uninit_vbi_isoc(dev); |
| 401 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 402 | /* clear if any halt */ |
| 403 | usb_clear_halt(dev->udev, |
| 404 | usb_rcvbulkpipe(dev->udev, |
| 405 | dev->vbi_mode.end_point_addr)); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 406 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 407 | dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy; |
| 408 | dev->vbi_mode.bulk_ctl.num_bufs = num_bufs; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 409 | dma_q->pos = 0; |
| 410 | dma_q->is_partial_line = 0; |
| 411 | dma_q->last_sav = 0; |
| 412 | dma_q->current_field = -1; |
| 413 | dma_q->bytes_left_in_line = dev->width << 1; |
| 414 | dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ? |
| 415 | PAL_VBI_LINES : NTSC_VBI_LINES); |
| 416 | dma_q->lines_completed = 0; |
| 417 | for (i = 0; i < 8; i++) |
| 418 | dma_q->partial_buf[i] = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 419 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 420 | dev->vbi_mode.bulk_ctl.urb = kzalloc(sizeof(void *) * num_bufs, |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 421 | GFP_KERNEL); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 422 | if (!dev->vbi_mode.bulk_ctl.urb) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 423 | cx231xx_errdev("cannot alloc memory for usb buffers\n"); |
| 424 | return -ENOMEM; |
| 425 | } |
| 426 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 427 | dev->vbi_mode.bulk_ctl.transfer_buffer = |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 428 | kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 429 | if (!dev->vbi_mode.bulk_ctl.transfer_buffer) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 430 | cx231xx_errdev("cannot allocate memory for usbtransfer\n"); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 431 | kfree(dev->vbi_mode.bulk_ctl.urb); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 432 | return -ENOMEM; |
| 433 | } |
| 434 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 435 | dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size; |
| 436 | dev->vbi_mode.bulk_ctl.buf = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 437 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 438 | sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 439 | |
| 440 | /* allocate urbs and transfer buffers */ |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 441 | for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 442 | |
| 443 | urb = usb_alloc_urb(0, GFP_KERNEL); |
| 444 | if (!urb) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 445 | cx231xx_err(DRIVER_NAME |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 446 | ": cannot alloc bulk_ctl.urb %i\n", i); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 447 | cx231xx_uninit_vbi_isoc(dev); |
| 448 | return -ENOMEM; |
| 449 | } |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 450 | dev->vbi_mode.bulk_ctl.urb[i] = urb; |
Thomas Petazzoni | 7a6f6c2 | 2012-01-07 09:52:37 -0300 | [diff] [blame] | 451 | urb->transfer_flags = URB_NO_TRANSFER_DMA_MAP; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 452 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 453 | dev->vbi_mode.bulk_ctl.transfer_buffer[i] = |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 454 | kzalloc(sb_size, GFP_KERNEL); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 455 | if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 456 | cx231xx_err(DRIVER_NAME |
| 457 | ": unable to allocate %i bytes for transfer" |
| 458 | " buffer %i%s\n", sb_size, i, |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 459 | in_interrupt() ? " while in int" : ""); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 460 | cx231xx_uninit_vbi_isoc(dev); |
| 461 | return -ENOMEM; |
| 462 | } |
| 463 | |
| 464 | pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 465 | usb_fill_bulk_urb(urb, dev->udev, pipe, |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 466 | dev->vbi_mode.bulk_ctl.transfer_buffer[i], |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 467 | sb_size, cx231xx_irq_vbi_callback, dma_q); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | init_waitqueue_head(&dma_q->wq); |
| 471 | |
| 472 | /* submit urbs and enables IRQ */ |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 473 | for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) { |
| 474 | rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 475 | if (rc) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 476 | cx231xx_err(DRIVER_NAME |
| 477 | ": submit of urb %i failed (error=%i)\n", i, |
| 478 | rc); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 479 | cx231xx_uninit_vbi_isoc(dev); |
| 480 | return rc; |
| 481 | } |
| 482 | } |
| 483 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 484 | cx231xx_capture_start(dev, 1, Vbi); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc); |
| 489 | |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 490 | u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q, |
| 491 | u8 sav_eav, u8 *p_buffer, u32 buffer_size) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 492 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 493 | u32 bytes_copied = 0; |
| 494 | int current_field = -1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 495 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 496 | switch (sav_eav) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 497 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 498 | case SAV_VBI_FIELD1: |
| 499 | current_field = 1; |
| 500 | break; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 501 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 502 | case SAV_VBI_FIELD2: |
| 503 | current_field = 2; |
| 504 | break; |
| 505 | default: |
| 506 | break; |
| 507 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 508 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 509 | if (current_field < 0) |
| 510 | return bytes_copied; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 511 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 512 | dma_q->last_sav = sav_eav; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 513 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 514 | bytes_copied = |
| 515 | cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size, |
| 516 | current_field); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 517 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 518 | return bytes_copied; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Announces that a buffer were filled and request the next |
| 523 | */ |
| 524 | static inline void vbi_buffer_filled(struct cx231xx *dev, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 525 | struct cx231xx_dmaqueue *dma_q, |
| 526 | struct cx231xx_buffer *buf) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 527 | { |
| 528 | /* Advice that buffer was filled */ |
| 529 | /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */ |
| 530 | |
| 531 | buf->vb.state = VIDEOBUF_DONE; |
| 532 | buf->vb.field_count++; |
| 533 | do_gettimeofday(&buf->vb.ts); |
| 534 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 535 | dev->vbi_mode.bulk_ctl.buf = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 536 | |
| 537 | list_del(&buf->vb.queue); |
| 538 | wake_up(&buf->vb.done); |
| 539 | } |
| 540 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 541 | u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q, |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 542 | u8 *p_line, u32 length, int field_number) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 543 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 544 | u32 bytes_to_copy; |
| 545 | struct cx231xx_buffer *buf; |
| 546 | u32 _line_size = dev->width * 2; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 547 | |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 548 | if (dma_q->current_field == -1) { |
| 549 | /* Just starting up */ |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 550 | cx231xx_reset_vbi_buffer(dev, dma_q); |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 551 | } |
| 552 | |
| 553 | if (dma_q->current_field != field_number) |
| 554 | dma_q->lines_completed = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 555 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 556 | /* get the buffer pointer */ |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 557 | buf = dev->vbi_mode.bulk_ctl.buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 558 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 559 | /* Remember the field number for next time */ |
| 560 | dma_q->current_field = field_number; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 561 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 562 | bytes_to_copy = dma_q->bytes_left_in_line; |
| 563 | if (bytes_to_copy > length) |
| 564 | bytes_to_copy = length; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 565 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 566 | if (dma_q->lines_completed >= dma_q->lines_per_field) { |
| 567 | dma_q->bytes_left_in_line -= bytes_to_copy; |
| 568 | dma_q->is_partial_line = |
| 569 | (dma_q->bytes_left_in_line == 0) ? 0 : 1; |
| 570 | return 0; |
| 571 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 572 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 573 | dma_q->is_partial_line = 1; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 574 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 575 | /* If we don't have a buffer, just return the number of bytes we would |
| 576 | have copied if we had a buffer. */ |
| 577 | if (!buf) { |
| 578 | dma_q->bytes_left_in_line -= bytes_to_copy; |
| 579 | dma_q->is_partial_line = |
| 580 | (dma_q->bytes_left_in_line == 0) ? 0 : 1; |
| 581 | return bytes_to_copy; |
| 582 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 583 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 584 | /* copy the data to video buffer */ |
| 585 | cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 586 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 587 | dma_q->pos += bytes_to_copy; |
| 588 | dma_q->bytes_left_in_line -= bytes_to_copy; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 589 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 590 | if (dma_q->bytes_left_in_line == 0) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 591 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 592 | dma_q->bytes_left_in_line = _line_size; |
| 593 | dma_q->lines_completed++; |
| 594 | dma_q->is_partial_line = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 595 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 596 | if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) { |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 597 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 598 | vbi_buffer_filled(dev, dma_q, buf); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 599 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 600 | dma_q->pos = 0; |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 601 | dma_q->lines_completed = 0; |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 602 | cx231xx_reset_vbi_buffer(dev, dma_q); |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 603 | } |
| 604 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 605 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 606 | return bytes_to_copy; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | /* |
| 610 | * video-buf generic routine to get the next available buffer |
| 611 | */ |
| 612 | static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q, |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 613 | struct cx231xx_buffer **buf) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 614 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 615 | struct cx231xx_video_mode *vmode = |
| 616 | container_of(dma_q, struct cx231xx_video_mode, vidq); |
| 617 | struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 618 | char *outp; |
| 619 | |
| 620 | if (list_empty(&dma_q->active)) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 621 | cx231xx_err(DRIVER_NAME ": No active queue to serve\n"); |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 622 | dev->vbi_mode.bulk_ctl.buf = NULL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 623 | *buf = NULL; |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | /* Get the next buffer */ |
| 628 | *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue); |
| 629 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 630 | /* Cleans up buffer - Useful for testing for frame/URB loss */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 631 | outp = videobuf_to_vmalloc(&(*buf)->vb); |
| 632 | memset(outp, 0, (*buf)->vb.size); |
| 633 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 634 | dev->vbi_mode.bulk_ctl.buf = *buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 635 | |
| 636 | return; |
| 637 | } |
| 638 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 639 | void cx231xx_reset_vbi_buffer(struct cx231xx *dev, |
| 640 | struct cx231xx_dmaqueue *dma_q) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 641 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 642 | struct cx231xx_buffer *buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 643 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 644 | buf = dev->vbi_mode.bulk_ctl.buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 645 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 646 | if (buf == NULL) { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 647 | /* first try to get the buffer */ |
| 648 | get_next_vbi_buf(dma_q, &buf); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 649 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 650 | dma_q->pos = 0; |
| 651 | dma_q->current_field = -1; |
| 652 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 653 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 654 | dma_q->bytes_left_in_line = dev->width << 1; |
| 655 | dma_q->lines_completed = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q, |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 659 | u8 *p_buffer, u32 bytes_to_copy) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 660 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 661 | u8 *p_out_buffer = NULL; |
| 662 | u32 current_line_bytes_copied = 0; |
| 663 | struct cx231xx_buffer *buf; |
| 664 | u32 _line_size = dev->width << 1; |
| 665 | void *startwrite; |
| 666 | int offset, lencopy; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 667 | |
Palash Bandyopadhyay | 64fbf44 | 2010-07-06 18:12:25 -0300 | [diff] [blame] | 668 | buf = dev->vbi_mode.bulk_ctl.buf; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 669 | |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 670 | if (buf == NULL) |
| 671 | return -EINVAL; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 672 | |
| 673 | p_out_buffer = videobuf_to_vmalloc(&buf->vb); |
| 674 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 675 | if (dma_q->bytes_left_in_line != _line_size) { |
| 676 | current_line_bytes_copied = |
| 677 | _line_size - dma_q->bytes_left_in_line; |
| 678 | } |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 679 | |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 680 | offset = (dma_q->lines_completed * _line_size) + |
| 681 | current_line_bytes_copied; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 682 | |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 683 | if (dma_q->current_field == 2) { |
| 684 | /* Populate the second half of the frame */ |
| 685 | offset += (dev->width * 2 * dma_q->lines_per_field); |
| 686 | } |
| 687 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 688 | /* prepare destination address */ |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 689 | startwrite = p_out_buffer + offset; |
| 690 | |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 691 | lencopy = dma_q->bytes_left_in_line > bytes_to_copy ? |
| 692 | bytes_to_copy : dma_q->bytes_left_in_line; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 693 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 694 | memcpy(startwrite, p_buffer, lencopy); |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 695 | |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 696 | return 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 697 | } |
| 698 | |
Mauro Carvalho Chehab | cde4362 | 2009-03-03 13:31:36 -0300 | [diff] [blame] | 699 | u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev, |
| 700 | struct cx231xx_dmaqueue *dma_q) |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 701 | { |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 702 | u32 height = 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 703 | |
| 704 | height = ((dev->norm & V4L2_STD_625_50) ? |
Mauro Carvalho Chehab | 84b5dbf | 2009-03-03 06:14:34 -0300 | [diff] [blame] | 705 | PAL_VBI_LINES : NTSC_VBI_LINES); |
Devin Heitmueller | 99d35a0 | 2010-07-08 15:45:13 -0300 | [diff] [blame] | 706 | if (dma_q->lines_completed == height && dma_q->current_field == 2) |
| 707 | return 1; |
| 708 | else |
| 709 | return 0; |
Sri Deevi | e0d3baf | 2009-03-03 14:37:50 -0300 | [diff] [blame] | 710 | } |