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