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