Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1 | /* |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 2 | * tm6000-video.c - driver for TM5600/TM6000/TM6010 USB video capture devices |
| 3 | * |
| 4 | * Copyright (C) 2006-2007 Mauro Carvalho Chehab <mchehab@infradead.org> |
| 5 | * |
| 6 | * Copyright (C) 2007 Michel Ludwig <michel.ludwig@gmail.com> |
| 7 | * - Fixed module load/unload |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation version 2 |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 21 | */ |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 22 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 23 | #include <linux/module.h> |
| 24 | #include <linux/delay.h> |
| 25 | #include <linux/errno.h> |
| 26 | #include <linux/fs.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/mm.h> |
| 30 | #include <linux/ioport.h> |
| 31 | #include <linux/init.h> |
| 32 | #include <linux/sched.h> |
| 33 | #include <linux/random.h> |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 34 | #include <linux/usb.h> |
| 35 | #include <linux/videodev2.h> |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 36 | #include <media/v4l2-ioctl.h> |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 37 | #include <media/v4l2-event.h> |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 38 | #include <media/tuner.h> |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 39 | #include <linux/interrupt.h> |
| 40 | #include <linux/kthread.h> |
| 41 | #include <linux/highmem.h> |
| 42 | #include <linux/freezer.h> |
| 43 | |
| 44 | #include "tm6000-regs.h" |
| 45 | #include "tm6000.h" |
| 46 | |
| 47 | #define BUFFER_TIMEOUT msecs_to_jiffies(2000) /* 2 seconds */ |
| 48 | |
Michel Ludwig | 95a8382 | 2007-07-24 08:06:45 -0300 | [diff] [blame] | 49 | /* Limits minimum and default number of buffers */ |
| 50 | #define TM6000_MIN_BUF 4 |
| 51 | #define TM6000_DEF_BUF 8 |
| 52 | |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 53 | #define TM6000_NUM_URB_BUF 8 |
| 54 | |
Stefan Ringel | 5a4b55e | 2010-05-19 13:58:27 -0300 | [diff] [blame] | 55 | #define TM6000_MAX_ISO_PACKETS 46 /* Max number of ISO packets */ |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 56 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 57 | /* Declare static vars that will be used as parameters */ |
| 58 | static unsigned int vid_limit = 16; /* Video memory limit, in Mb */ |
| 59 | static int video_nr = -1; /* /dev/videoN, -1 for autodetect */ |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 60 | static int radio_nr = -1; /* /dev/radioN, -1 for autodetect */ |
Mauro Carvalho Chehab | b391b0e | 2012-12-20 15:21:23 -0200 | [diff] [blame] | 61 | static bool keep_urb; /* keep urb buffers allocated */ |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 62 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 63 | /* Debug level */ |
| 64 | int tm6000_debug; |
Mauro Carvalho Chehab | faa7c13 | 2010-06-04 21:08:25 -0300 | [diff] [blame] | 65 | EXPORT_SYMBOL_GPL(tm6000_debug); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 66 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 67 | static struct tm6000_fmt format[] = { |
| 68 | { |
| 69 | .name = "4:2:2, packed, YVY2", |
| 70 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 71 | .depth = 16, |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 72 | }, { |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 73 | .name = "4:2:2, packed, UYVY", |
| 74 | .fourcc = V4L2_PIX_FMT_UYVY, |
| 75 | .depth = 16, |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 76 | }, { |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 77 | .name = "A/V + VBI mux packet", |
| 78 | .fourcc = V4L2_PIX_FMT_TM6000, |
| 79 | .depth = 16, |
| 80 | } |
| 81 | }; |
| 82 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 83 | /* ------------------------------------------------------------------ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 84 | * DMA and thread functions |
| 85 | * ------------------------------------------------------------------ |
| 86 | */ |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 87 | |
| 88 | #define norm_maxw(a) 720 |
Mauro Carvalho Chehab | 4475c04 | 2007-09-03 21:51:45 -0300 | [diff] [blame] | 89 | #define norm_maxh(a) 576 |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 90 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 91 | #define norm_minw(a) norm_maxw(a) |
| 92 | #define norm_minh(a) norm_maxh(a) |
| 93 | |
| 94 | /* |
| 95 | * video-buf generic routine to get the next available buffer |
| 96 | */ |
Mauro Carvalho Chehab | 721f507 | 2008-10-26 09:18:53 -0300 | [diff] [blame] | 97 | static inline void get_next_buf(struct tm6000_dmaqueue *dma_q, |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 98 | struct tm6000_buffer **buf) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 99 | { |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 100 | struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 101 | |
| 102 | if (list_empty(&dma_q->active)) { |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 103 | dprintk(dev, V4L2_DEBUG_QUEUE, "No active queue to serve\n"); |
Mauro Carvalho Chehab | 1f9305b | 2008-11-28 06:44:06 -0300 | [diff] [blame] | 104 | *buf = NULL; |
Mauro Carvalho Chehab | 721f507 | 2008-10-26 09:18:53 -0300 | [diff] [blame] | 105 | return; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | *buf = list_entry(dma_q->active.next, |
| 109 | struct tm6000_buffer, vb.queue); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Announces that a buffer were filled and request the next |
| 114 | */ |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 115 | static inline void buffer_filled(struct tm6000_core *dev, |
| 116 | struct tm6000_dmaqueue *dma_q, |
| 117 | struct tm6000_buffer *buf) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 118 | { |
| 119 | /* Advice that buffer was filled */ |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 120 | dprintk(dev, V4L2_DEBUG_ISOC, "[%p/%d] wakeup\n", buf, buf->vb.i); |
Mauro Carvalho Chehab | 47878f1 | 2007-11-15 16:37:35 -0300 | [diff] [blame] | 121 | buf->vb.state = VIDEOBUF_DONE; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 122 | buf->vb.field_count++; |
Sakari Ailus | 8e6057b | 2012-09-15 15:14:42 -0300 | [diff] [blame] | 123 | v4l2_get_timestamp(&buf->vb.ts); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 124 | |
| 125 | list_del(&buf->vb.queue); |
| 126 | wake_up(&buf->vb.done); |
| 127 | } |
| 128 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 129 | /* |
| 130 | * Identify the tm5600/6000 buffer header type and properly handles |
| 131 | */ |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 132 | static int copy_streams(u8 *data, unsigned long len, |
| 133 | struct urb *urb) |
Mauro Carvalho Chehab | e2c9500 | 2007-09-19 15:39:22 -0300 | [diff] [blame] | 134 | { |
| 135 | struct tm6000_dmaqueue *dma_q = urb->context; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 136 | struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq); |
Hans Verkuil | 8173090 | 2012-04-20 07:30:48 -0300 | [diff] [blame] | 137 | u8 *ptr = data, *endp = data+len; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 138 | unsigned long header = 0; |
| 139 | int rc = 0; |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 140 | unsigned int cmd, cpysize, pktsize, size, field, block, line, pos = 0; |
Jarod Wilson | cc73b4b | 2011-04-11 18:49:24 -0300 | [diff] [blame] | 141 | struct tm6000_buffer *vbuf = NULL; |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 142 | char *voutp = NULL; |
| 143 | unsigned int linewidth; |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 144 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 145 | if (!dev->radio) { |
| 146 | /* get video buffer */ |
| 147 | get_next_buf(dma_q, &vbuf); |
| 148 | |
| 149 | if (!vbuf) |
| 150 | return rc; |
| 151 | voutp = videobuf_to_vmalloc(&vbuf->vb); |
| 152 | |
| 153 | if (!voutp) |
| 154 | return 0; |
| 155 | } |
Mauro Carvalho Chehab | e2c9500 | 2007-09-19 15:39:22 -0300 | [diff] [blame] | 156 | |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 157 | for (ptr = data; ptr < endp;) { |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 158 | if (!dev->isoc_ctl.cmd) { |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 159 | /* Header */ |
| 160 | if (dev->isoc_ctl.tmp_buf_len > 0) { |
| 161 | /* from last urb or packet */ |
| 162 | header = dev->isoc_ctl.tmp_buf; |
| 163 | if (4 - dev->isoc_ctl.tmp_buf_len > 0) { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 164 | memcpy((u8 *)&header + |
Mauro Carvalho Chehab | 801dd3b | 2010-05-02 17:14:33 -0300 | [diff] [blame] | 165 | dev->isoc_ctl.tmp_buf_len, |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 166 | ptr, |
Mauro Carvalho Chehab | 801dd3b | 2010-05-02 17:14:33 -0300 | [diff] [blame] | 167 | 4 - dev->isoc_ctl.tmp_buf_len); |
| 168 | ptr += 4 - dev->isoc_ctl.tmp_buf_len; |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 169 | } |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 170 | dev->isoc_ctl.tmp_buf_len = 0; |
| 171 | } else { |
| 172 | if (ptr + 3 >= endp) { |
| 173 | /* have incomplete header */ |
| 174 | dev->isoc_ctl.tmp_buf_len = endp - ptr; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 175 | memcpy(&dev->isoc_ctl.tmp_buf, ptr, |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 176 | dev->isoc_ctl.tmp_buf_len); |
| 177 | return rc; |
| 178 | } |
| 179 | /* Seek for sync */ |
| 180 | for (; ptr < endp - 3; ptr++) { |
| 181 | if (*(ptr + 3) == 0x47) |
| 182 | break; |
| 183 | } |
| 184 | /* Get message header */ |
| 185 | header = *(unsigned long *)ptr; |
| 186 | ptr += 4; |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 187 | } |
Mauro Carvalho Chehab | 23ba946 | 2010-06-07 11:57:28 -0300 | [diff] [blame] | 188 | |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 189 | /* split the header fields */ |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 190 | size = ((header & 0x7e) << 1); |
| 191 | if (size > 0) |
| 192 | size -= 4; |
| 193 | block = (header >> 7) & 0xf; |
| 194 | field = (header >> 11) & 0x1; |
| 195 | line = (header >> 12) & 0x1ff; |
| 196 | cmd = (header >> 21) & 0x7; |
| 197 | /* Validates haeder fields */ |
| 198 | if (size > TM6000_URB_MSG_LEN) |
| 199 | size = TM6000_URB_MSG_LEN; |
| 200 | pktsize = TM6000_URB_MSG_LEN; |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 201 | /* |
| 202 | * calculate position in buffer and change the buffer |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 203 | */ |
| 204 | switch (cmd) { |
| 205 | case TM6000_URB_MSG_VIDEO: |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 206 | if (!dev->radio) { |
| 207 | if ((dev->isoc_ctl.vfield != field) && |
| 208 | (field == 1)) { |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 209 | /* |
| 210 | * Announces that a new buffer |
| 211 | * were filled |
| 212 | */ |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 213 | buffer_filled(dev, dma_q, vbuf); |
| 214 | dprintk(dev, V4L2_DEBUG_ISOC, |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 215 | "new buffer filled\n"); |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 216 | get_next_buf(dma_q, &vbuf); |
| 217 | if (!vbuf) |
| 218 | return rc; |
| 219 | voutp = videobuf_to_vmalloc(&vbuf->vb); |
| 220 | if (!voutp) |
| 221 | return rc; |
| 222 | memset(voutp, 0, vbuf->vb.size); |
| 223 | } |
| 224 | linewidth = vbuf->vb.width << 1; |
| 225 | pos = ((line << 1) - field - 1) * |
| 226 | linewidth + block * TM6000_URB_MSG_LEN; |
| 227 | /* Don't allow to write out of the buffer */ |
| 228 | if (pos + size > vbuf->vb.size) |
| 229 | cmd = TM6000_URB_MSG_ERR; |
| 230 | dev->isoc_ctl.vfield = field; |
Jarod Wilson | cc73b4b | 2011-04-11 18:49:24 -0300 | [diff] [blame] | 231 | } |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 232 | break; |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 233 | case TM6000_URB_MSG_VBI: |
Mauro Carvalho Chehab | 23ba946 | 2010-06-07 11:57:28 -0300 | [diff] [blame] | 234 | break; |
| 235 | case TM6000_URB_MSG_AUDIO: |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 236 | case TM6000_URB_MSG_PTS: |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 237 | size = pktsize; /* Size is always 180 bytes */ |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 238 | break; |
Mauro Carvalho Chehab | cc6c60d | 2007-09-19 16:24:05 -0300 | [diff] [blame] | 239 | } |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 240 | } else { |
| 241 | /* Continue the last copy */ |
| 242 | cmd = dev->isoc_ctl.cmd; |
| 243 | size = dev->isoc_ctl.size; |
| 244 | pos = dev->isoc_ctl.pos; |
| 245 | pktsize = dev->isoc_ctl.pktsize; |
Stefan Ringel | 423c79e | 2011-05-21 03:05:38 -0300 | [diff] [blame] | 246 | field = dev->isoc_ctl.field; |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 247 | } |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 248 | cpysize = (endp - ptr > size) ? size : endp - ptr; |
| 249 | if (cpysize) { |
| 250 | /* copy data in different buffers */ |
| 251 | switch (cmd) { |
| 252 | case TM6000_URB_MSG_VIDEO: |
| 253 | /* Fills video buffer */ |
| 254 | if (vbuf) |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 255 | memcpy(&voutp[pos], ptr, cpysize); |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 256 | break; |
Stefan Ringel | 7ecff8c | 2011-05-09 16:54:00 -0300 | [diff] [blame] | 257 | case TM6000_URB_MSG_AUDIO: { |
| 258 | int i; |
| 259 | for (i = 0; i < cpysize; i += 2) |
| 260 | swab16s((u16 *)(ptr + i)); |
Dmitri Belimov | 73f4d26 | 2010-09-20 17:07:15 -0300 | [diff] [blame] | 261 | |
Mauro Carvalho Chehab | b17b869 | 2010-06-03 17:16:28 -0300 | [diff] [blame] | 262 | tm6000_call_fillbuf(dev, TM6000_AUDIO, ptr, cpysize); |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 263 | break; |
Stefan Ringel | 7ecff8c | 2011-05-09 16:54:00 -0300 | [diff] [blame] | 264 | } |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 265 | case TM6000_URB_MSG_VBI: |
| 266 | /* Need some code to copy vbi buffer */ |
| 267 | break; |
Stefan Ringel | 2f349da | 2011-05-09 16:54:02 -0300 | [diff] [blame] | 268 | case TM6000_URB_MSG_PTS: { |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 269 | /* Need some code to copy pts */ |
Stefan Ringel | 2f349da | 2011-05-09 16:54:02 -0300 | [diff] [blame] | 270 | u32 pts; |
| 271 | pts = *(u32 *)ptr; |
Stefan Ringel | 423c79e | 2011-05-21 03:05:38 -0300 | [diff] [blame] | 272 | dprintk(dev, V4L2_DEBUG_ISOC, "field %d, PTS %x", |
| 273 | field, pts); |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 274 | break; |
| 275 | } |
Stefan Ringel | 2f349da | 2011-05-09 16:54:02 -0300 | [diff] [blame] | 276 | } |
Mauro Carvalho Chehab | a228618 | 2007-09-22 02:06:25 -0300 | [diff] [blame] | 277 | } |
Mauro Carvalho Chehab | ccfb302 | 2010-06-30 17:24:28 -0300 | [diff] [blame] | 278 | if (ptr + pktsize > endp) { |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 279 | /* |
| 280 | * End of URB packet, but cmd processing is not |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 281 | * complete. Preserve the state for a next packet |
| 282 | */ |
| 283 | dev->isoc_ctl.pos = pos + cpysize; |
| 284 | dev->isoc_ctl.size = size - cpysize; |
| 285 | dev->isoc_ctl.cmd = cmd; |
Stefan Ringel | 423c79e | 2011-05-21 03:05:38 -0300 | [diff] [blame] | 286 | dev->isoc_ctl.field = field; |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 287 | dev->isoc_ctl.pktsize = pktsize - (endp - ptr); |
Mauro Carvalho Chehab | ccfb302 | 2010-06-30 17:24:28 -0300 | [diff] [blame] | 288 | ptr += endp - ptr; |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 289 | } else { |
| 290 | dev->isoc_ctl.cmd = 0; |
| 291 | ptr += pktsize; |
| 292 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 293 | } |
Mauro Carvalho Chehab | a228618 | 2007-09-22 02:06:25 -0300 | [diff] [blame] | 294 | return 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 295 | } |
Stefan Ringel | 83cb9a5 | 2010-06-01 12:47:42 -0300 | [diff] [blame] | 296 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 297 | /* |
| 298 | * Identify the tm5600/6000 buffer header type and properly handles |
| 299 | */ |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 300 | static int copy_multiplexed(u8 *ptr, unsigned long len, |
| 301 | struct urb *urb) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 302 | { |
| 303 | struct tm6000_dmaqueue *dma_q = urb->context; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 304 | struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq); |
| 305 | unsigned int pos = dev->isoc_ctl.pos, cpysize; |
| 306 | int rc = 1; |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 307 | struct tm6000_buffer *buf; |
| 308 | char *outp = NULL; |
| 309 | |
| 310 | get_next_buf(dma_q, &buf); |
| 311 | if (buf) |
| 312 | outp = videobuf_to_vmalloc(&buf->vb); |
| 313 | |
| 314 | if (!outp) |
| 315 | return 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 316 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 317 | while (len > 0) { |
| 318 | cpysize = min(len, buf->vb.size-pos); |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 319 | memcpy(&outp[pos], ptr, cpysize); |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 320 | pos += cpysize; |
| 321 | ptr += cpysize; |
| 322 | len -= cpysize; |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 323 | if (pos >= buf->vb.size) { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 324 | pos = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 325 | /* Announces that a new buffer were filled */ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 326 | buffer_filled(dev, dma_q, buf); |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 327 | dprintk(dev, V4L2_DEBUG_ISOC, "new buffer filled\n"); |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 328 | get_next_buf(dma_q, &buf); |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 329 | if (!buf) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 330 | break; |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 331 | outp = videobuf_to_vmalloc(&(buf->vb)); |
| 332 | if (!outp) |
Mauro Carvalho Chehab | e8a4845 | 2008-11-28 07:39:00 -0300 | [diff] [blame] | 333 | return rc; |
| 334 | pos = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 338 | dev->isoc_ctl.pos = pos; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 339 | return rc; |
| 340 | } |
| 341 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 342 | static inline void print_err_status(struct tm6000_core *dev, |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 343 | int packet, int status) |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 344 | { |
| 345 | char *errmsg = "Unknown"; |
| 346 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 347 | switch (status) { |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 348 | case -ENOENT: |
| 349 | errmsg = "unlinked synchronuously"; |
| 350 | break; |
| 351 | case -ECONNRESET: |
| 352 | errmsg = "unlinked asynchronuously"; |
| 353 | break; |
| 354 | case -ENOSR: |
| 355 | errmsg = "Buffer error (overrun)"; |
| 356 | break; |
| 357 | case -EPIPE: |
| 358 | errmsg = "Stalled (device not responding)"; |
| 359 | break; |
| 360 | case -EOVERFLOW: |
| 361 | errmsg = "Babble (bad cable?)"; |
| 362 | break; |
| 363 | case -EPROTO: |
| 364 | errmsg = "Bit-stuff error (bad cable?)"; |
| 365 | break; |
| 366 | case -EILSEQ: |
| 367 | errmsg = "CRC/Timeout (could be anything)"; |
| 368 | break; |
| 369 | case -ETIME: |
| 370 | errmsg = "Device does not respond"; |
| 371 | break; |
| 372 | } |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 373 | if (packet < 0) { |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 374 | dprintk(dev, V4L2_DEBUG_QUEUE, "URB status %d [%s].\n", |
| 375 | status, errmsg); |
| 376 | } else { |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 377 | dprintk(dev, V4L2_DEBUG_QUEUE, "URB packet %d, status %d [%s].\n", |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 378 | packet, status, errmsg); |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 383 | /* |
| 384 | * Controls the isoc copy of each urb packet |
| 385 | */ |
Mauro Carvalho Chehab | e8a4845 | 2008-11-28 07:39:00 -0300 | [diff] [blame] | 386 | static inline int tm6000_isoc_copy(struct urb *urb) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 387 | { |
| 388 | struct tm6000_dmaqueue *dma_q = urb->context; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 389 | struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq); |
| 390 | int i, len = 0, rc = 1, status; |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 391 | char *p; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 392 | |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 393 | if (urb->status < 0) { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 394 | print_err_status(dev, -1, urb->status); |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 395 | return 0; |
| 396 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 397 | |
| 398 | for (i = 0; i < urb->number_of_packets; i++) { |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 399 | status = urb->iso_frame_desc[i].status; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 400 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 401 | if (status < 0) { |
| 402 | print_err_status(dev, i, status); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 403 | continue; |
Mauro Carvalho Chehab | 5f79675 | 2007-08-27 07:55:05 -0300 | [diff] [blame] | 404 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 405 | |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 406 | len = urb->iso_frame_desc[i].actual_length; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 407 | |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 408 | if (len > 0) { |
| 409 | p = urb->transfer_buffer + urb->iso_frame_desc[i].offset; |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 410 | if (!urb->iso_frame_desc[i].status) { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 411 | if ((dev->fourcc) == V4L2_PIX_FMT_TM6000) { |
| 412 | rc = copy_multiplexed(p, len, urb); |
| 413 | if (rc <= 0) |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 414 | return rc; |
| 415 | } else { |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 416 | copy_streams(p, len, urb); |
Mauro Carvalho Chehab | ed0236a | 2008-04-09 08:07:20 -0300 | [diff] [blame] | 417 | } |
| 418 | } |
Stefan Ringel | 4b6ed9f | 2010-05-19 13:58:26 -0300 | [diff] [blame] | 419 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 420 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 421 | return rc; |
| 422 | } |
| 423 | |
| 424 | /* ------------------------------------------------------------------ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 425 | * URB control |
| 426 | * ------------------------------------------------------------------ |
| 427 | */ |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 428 | |
| 429 | /* |
| 430 | * IRQ callback, called by URB callback |
| 431 | */ |
| 432 | static void tm6000_irq_callback(struct urb *urb) |
| 433 | { |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 434 | struct tm6000_dmaqueue *dma_q = urb->context; |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 435 | struct tm6000_core *dev = container_of(dma_q, struct tm6000_core, vidq); |
Mauro Carvalho Chehab | e8a4845 | 2008-11-28 07:39:00 -0300 | [diff] [blame] | 436 | int i; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 437 | |
Thierry Reding | 14f0915 | 2011-08-04 04:14:06 -0300 | [diff] [blame] | 438 | switch (urb->status) { |
| 439 | case 0: |
| 440 | case -ETIMEDOUT: |
| 441 | break; |
| 442 | |
| 443 | case -ECONNRESET: |
| 444 | case -ENOENT: |
| 445 | case -ESHUTDOWN: |
Mauro Carvalho Chehab | 721f507 | 2008-10-26 09:18:53 -0300 | [diff] [blame] | 446 | return; |
| 447 | |
Thierry Reding | 14f0915 | 2011-08-04 04:14:06 -0300 | [diff] [blame] | 448 | default: |
| 449 | tm6000_err("urb completion error %d.\n", urb->status); |
| 450 | break; |
| 451 | } |
| 452 | |
Mauro Carvalho Chehab | e8a4845 | 2008-11-28 07:39:00 -0300 | [diff] [blame] | 453 | spin_lock(&dev->slock); |
| 454 | tm6000_isoc_copy(urb); |
| 455 | spin_unlock(&dev->slock); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 456 | |
Mauro Carvalho Chehab | e8a4845 | 2008-11-28 07:39:00 -0300 | [diff] [blame] | 457 | /* Reset urb buffers */ |
| 458 | for (i = 0; i < urb->number_of_packets; i++) { |
| 459 | urb->iso_frame_desc[i].status = 0; |
| 460 | urb->iso_frame_desc[i].actual_length = 0; |
| 461 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 462 | |
Mauro Carvalho Chehab | c4acf48 | 2008-01-09 22:44:51 -0300 | [diff] [blame] | 463 | urb->status = usb_submit_urb(urb, GFP_ATOMIC); |
| 464 | if (urb->status) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 465 | tm6000_err("urb resubmit failed (error=%i)\n", |
| 466 | urb->status); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 470 | * Allocate URB buffers |
| 471 | */ |
| 472 | static int tm6000_alloc_urb_buffers(struct tm6000_core *dev) |
| 473 | { |
| 474 | int num_bufs = TM6000_NUM_URB_BUF; |
| 475 | int i; |
| 476 | |
| 477 | if (dev->urb_buffer != NULL) |
| 478 | return 0; |
| 479 | |
| 480 | dev->urb_buffer = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL); |
| 481 | if (!dev->urb_buffer) { |
| 482 | tm6000_err("cannot allocate memory for urb buffers\n"); |
| 483 | return -ENOMEM; |
| 484 | } |
| 485 | |
| 486 | dev->urb_dma = kmalloc(sizeof(dma_addr_t *)*num_bufs, GFP_KERNEL); |
| 487 | if (!dev->urb_dma) { |
| 488 | tm6000_err("cannot allocate memory for urb dma pointers\n"); |
| 489 | return -ENOMEM; |
| 490 | } |
| 491 | |
| 492 | for (i = 0; i < num_bufs; i++) { |
| 493 | dev->urb_buffer[i] = usb_alloc_coherent( |
| 494 | dev->udev, dev->urb_size, |
| 495 | GFP_KERNEL, &dev->urb_dma[i]); |
| 496 | if (!dev->urb_buffer[i]) { |
| 497 | tm6000_err("unable to allocate %i bytes for transfer buffer %i\n", |
| 498 | dev->urb_size, i); |
| 499 | return -ENOMEM; |
| 500 | } |
| 501 | memset(dev->urb_buffer[i], 0, dev->urb_size); |
| 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | /* |
| 508 | * Free URB buffers |
| 509 | */ |
| 510 | static int tm6000_free_urb_buffers(struct tm6000_core *dev) |
| 511 | { |
| 512 | int i; |
| 513 | |
| 514 | if (dev->urb_buffer == NULL) |
| 515 | return 0; |
| 516 | |
| 517 | for (i = 0; i < TM6000_NUM_URB_BUF; i++) { |
| 518 | if (dev->urb_buffer[i]) { |
| 519 | usb_free_coherent(dev->udev, |
| 520 | dev->urb_size, |
| 521 | dev->urb_buffer[i], |
| 522 | dev->urb_dma[i]); |
| 523 | dev->urb_buffer[i] = NULL; |
| 524 | } |
| 525 | } |
| 526 | kfree(dev->urb_buffer); |
| 527 | kfree(dev->urb_dma); |
| 528 | dev->urb_buffer = NULL; |
| 529 | dev->urb_dma = NULL; |
| 530 | |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 535 | * Stop and Deallocate URBs |
| 536 | */ |
| 537 | static void tm6000_uninit_isoc(struct tm6000_core *dev) |
| 538 | { |
| 539 | struct urb *urb; |
| 540 | int i; |
| 541 | |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 542 | dev->isoc_ctl.buf = NULL; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 543 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 544 | urb = dev->isoc_ctl.urb[i]; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 545 | if (urb) { |
| 546 | usb_kill_urb(urb); |
| 547 | usb_unlink_urb(urb); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 548 | usb_free_urb(urb); |
| 549 | dev->isoc_ctl.urb[i] = NULL; |
| 550 | } |
| 551 | dev->isoc_ctl.transfer_buffer[i] = NULL; |
| 552 | } |
| 553 | |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 554 | if (!keep_urb) |
| 555 | tm6000_free_urb_buffers(dev); |
| 556 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 557 | kfree(dev->isoc_ctl.urb); |
| 558 | kfree(dev->isoc_ctl.transfer_buffer); |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 559 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 560 | dev->isoc_ctl.urb = NULL; |
| 561 | dev->isoc_ctl.transfer_buffer = NULL; |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 562 | dev->isoc_ctl.num_bufs = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 566 | * Assign URBs and start IRQ |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 567 | */ |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 568 | static int tm6000_prepare_isoc(struct tm6000_core *dev) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 569 | { |
| 570 | struct tm6000_dmaqueue *dma_q = &dev->vidq; |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 571 | int i, j, sb_size, pipe, size, max_packets; |
| 572 | int num_bufs = TM6000_NUM_URB_BUF; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 573 | struct urb *urb; |
Mauro Carvalho Chehab | 204193d | 2008-01-09 18:12:39 -0300 | [diff] [blame] | 574 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 575 | /* De-allocates all pending stuff */ |
| 576 | tm6000_uninit_isoc(dev); |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 577 | /* Stop interrupt USB pipe */ |
| 578 | tm6000_ir_int_stop(dev); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 579 | |
Mauro Carvalho Chehab | 6ae635c | 2010-04-26 11:24:18 -0300 | [diff] [blame] | 580 | usb_set_interface(dev->udev, |
| 581 | dev->isoc_in.bInterfaceNumber, |
| 582 | dev->isoc_in.bAlternateSetting); |
| 583 | |
Dmitri Belimov | 641d211 | 2010-12-22 05:57:46 -0300 | [diff] [blame] | 584 | /* Start interrupt USB pipe */ |
| 585 | tm6000_ir_int_start(dev); |
| 586 | |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 587 | pipe = usb_rcvisocpipe(dev->udev, |
Mauro Carvalho Chehab | 6ae635c | 2010-04-26 11:24:18 -0300 | [diff] [blame] | 588 | dev->isoc_in.endp->desc.bEndpointAddress & |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 589 | USB_ENDPOINT_NUMBER_MASK); |
| 590 | |
| 591 | size = usb_maxpacket(dev->udev, pipe, usb_pipeout(pipe)); |
| 592 | |
Mauro Carvalho Chehab | 6ae635c | 2010-04-26 11:24:18 -0300 | [diff] [blame] | 593 | if (size > dev->isoc_in.maxsize) |
| 594 | size = dev->isoc_in.maxsize; |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 595 | |
| 596 | dev->isoc_ctl.max_pkt_size = size; |
| 597 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 598 | max_packets = TM6000_MAX_ISO_PACKETS; |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 599 | sb_size = max_packets * size; |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 600 | dev->urb_size = sb_size; |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 601 | |
| 602 | dev->isoc_ctl.num_bufs = num_bufs; |
| 603 | |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 604 | dev->isoc_ctl.urb = kmalloc(sizeof(void *)*num_bufs, GFP_KERNEL); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 605 | if (!dev->isoc_ctl.urb) { |
| 606 | tm6000_err("cannot alloc memory for usb buffers\n"); |
| 607 | return -ENOMEM; |
| 608 | } |
| 609 | |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 610 | dev->isoc_ctl.transfer_buffer = kmalloc(sizeof(void *)*num_bufs, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 611 | GFP_KERNEL); |
Julia Lawall | f8960ee | 2010-02-11 03:30:30 -0300 | [diff] [blame] | 612 | if (!dev->isoc_ctl.transfer_buffer) { |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 613 | tm6000_err("cannot allocate memory for usbtransfer\n"); |
| 614 | kfree(dev->isoc_ctl.urb); |
| 615 | return -ENOMEM; |
| 616 | } |
| 617 | |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 618 | dprintk(dev, V4L2_DEBUG_QUEUE, "Allocating %d x %d packets" |
| 619 | " (%d bytes) of %d bytes each to handle %u size\n", |
| 620 | max_packets, num_bufs, sb_size, |
Mauro Carvalho Chehab | 6ae635c | 2010-04-26 11:24:18 -0300 | [diff] [blame] | 621 | dev->isoc_in.maxsize, size); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 622 | |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 623 | |
| 624 | if (!dev->urb_buffer && tm6000_alloc_urb_buffers(dev) < 0) { |
| 625 | tm6000_err("cannot allocate memory for urb buffers\n"); |
| 626 | |
| 627 | /* call free, as some buffers might have been allocated */ |
| 628 | tm6000_free_urb_buffers(dev); |
| 629 | kfree(dev->isoc_ctl.urb); |
| 630 | kfree(dev->isoc_ctl.transfer_buffer); |
| 631 | return -ENOMEM; |
| 632 | } |
| 633 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 634 | /* allocate urbs and transfer buffers */ |
| 635 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { |
| 636 | urb = usb_alloc_urb(max_packets, GFP_KERNEL); |
| 637 | if (!urb) { |
| 638 | tm6000_err("cannot alloc isoc_ctl.urb %i\n", i); |
| 639 | tm6000_uninit_isoc(dev); |
Mauro Carvalho Chehab | c1a1641 | 2007-10-15 15:43:50 -0300 | [diff] [blame] | 640 | usb_free_urb(urb); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 641 | return -ENOMEM; |
| 642 | } |
| 643 | dev->isoc_ctl.urb[i] = urb; |
| 644 | |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 645 | urb->transfer_dma = dev->urb_dma[i]; |
| 646 | dev->isoc_ctl.transfer_buffer[i] = dev->urb_buffer[i]; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 647 | |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 648 | usb_fill_bulk_urb(urb, dev->udev, pipe, |
| 649 | dev->isoc_ctl.transfer_buffer[i], sb_size, |
| 650 | tm6000_irq_callback, dma_q); |
Mauro Carvalho Chehab | 6ae635c | 2010-04-26 11:24:18 -0300 | [diff] [blame] | 651 | urb->interval = dev->isoc_in.endp->desc.bInterval; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 652 | urb->number_of_packets = max_packets; |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 653 | urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 654 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 655 | for (j = 0; j < max_packets; j++) { |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 656 | urb->iso_frame_desc[j].offset = size * j; |
| 657 | urb->iso_frame_desc[j].length = size; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
| 661 | return 0; |
| 662 | } |
| 663 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 664 | static int tm6000_start_thread(struct tm6000_core *dev) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 665 | { |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 666 | struct tm6000_dmaqueue *dma_q = &dev->vidq; |
| 667 | int i; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 668 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 669 | dma_q->frame = 0; |
| 670 | dma_q->ini_jiffies = jiffies; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 671 | |
| 672 | init_waitqueue_head(&dma_q->wq); |
| 673 | |
| 674 | /* submit urbs and enables IRQ */ |
| 675 | for (i = 0; i < dev->isoc_ctl.num_bufs; i++) { |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 676 | int rc = usb_submit_urb(dev->isoc_ctl.urb[i], GFP_ATOMIC); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 677 | if (rc) { |
| 678 | tm6000_err("submit of urb %i failed (error=%i)\n", i, |
| 679 | rc); |
| 680 | tm6000_uninit_isoc(dev); |
| 681 | return rc; |
| 682 | } |
| 683 | } |
| 684 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 685 | return 0; |
| 686 | } |
| 687 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 688 | /* ------------------------------------------------------------------ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 689 | * Videobuf operations |
| 690 | * ------------------------------------------------------------------ |
| 691 | */ |
Michel Ludwig | 95a8382 | 2007-07-24 08:06:45 -0300 | [diff] [blame] | 692 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 693 | static int |
| 694 | buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size) |
| 695 | { |
| 696 | struct tm6000_fh *fh = vq->priv_data; |
| 697 | |
| 698 | *size = fh->fmt->depth * fh->width * fh->height >> 3; |
| 699 | if (0 == *count) |
Michel Ludwig | 95a8382 | 2007-07-24 08:06:45 -0300 | [diff] [blame] | 700 | *count = TM6000_DEF_BUF; |
| 701 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 702 | if (*count < TM6000_MIN_BUF) |
| 703 | *count = TM6000_MIN_BUF; |
Michel Ludwig | 95a8382 | 2007-07-24 08:06:45 -0300 | [diff] [blame] | 704 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 705 | while (*size * *count > vid_limit * 1024 * 1024) |
| 706 | (*count)--; |
Michel Ludwig | 95a8382 | 2007-07-24 08:06:45 -0300 | [diff] [blame] | 707 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 708 | return 0; |
| 709 | } |
| 710 | |
| 711 | static void free_buffer(struct videobuf_queue *vq, struct tm6000_buffer *buf) |
| 712 | { |
Mauro Carvalho Chehab | 721f507 | 2008-10-26 09:18:53 -0300 | [diff] [blame] | 713 | struct tm6000_fh *fh = vq->priv_data; |
| 714 | struct tm6000_core *dev = fh->dev; |
| 715 | unsigned long flags; |
| 716 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 717 | if (in_interrupt()) |
| 718 | BUG(); |
| 719 | |
Mauro Carvalho Chehab | 721f507 | 2008-10-26 09:18:53 -0300 | [diff] [blame] | 720 | /* We used to wait for the buffer to finish here, but this didn't work |
| 721 | because, as we were keeping the state as VIDEOBUF_QUEUED, |
| 722 | videobuf_queue_cancel marked it as finished for us. |
| 723 | (Also, it could wedge forever if the hardware was misconfigured.) |
| 724 | |
| 725 | This should be safe; by the time we get here, the buffer isn't |
| 726 | queued anymore. If we ever start marking the buffers as |
| 727 | VIDEOBUF_ACTIVE, it won't be, though. |
| 728 | */ |
| 729 | spin_lock_irqsave(&dev->slock, flags); |
| 730 | if (dev->isoc_ctl.buf == buf) |
| 731 | dev->isoc_ctl.buf = NULL; |
| 732 | spin_unlock_irqrestore(&dev->slock, flags); |
| 733 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 734 | videobuf_vmalloc_free(&buf->vb); |
Mauro Carvalho Chehab | 47878f1 | 2007-11-15 16:37:35 -0300 | [diff] [blame] | 735 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static int |
| 739 | buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb, |
| 740 | enum v4l2_field field) |
| 741 | { |
| 742 | struct tm6000_fh *fh = vq->priv_data; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 743 | struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 744 | struct tm6000_core *dev = fh->dev; |
Thierry Reding | 88e834a | 2011-08-04 04:14:12 -0300 | [diff] [blame] | 745 | int rc = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 746 | |
| 747 | BUG_ON(NULL == fh->fmt); |
| 748 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 749 | |
| 750 | /* FIXME: It assumes depth=2 */ |
| 751 | /* The only currently supported format is 16 bits/pixel */ |
| 752 | buf->vb.size = fh->fmt->depth*fh->width*fh->height >> 3; |
| 753 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) |
| 754 | return -EINVAL; |
| 755 | |
| 756 | if (buf->fmt != fh->fmt || |
| 757 | buf->vb.width != fh->width || |
| 758 | buf->vb.height != fh->height || |
| 759 | buf->vb.field != field) { |
| 760 | buf->fmt = fh->fmt; |
| 761 | buf->vb.width = fh->width; |
| 762 | buf->vb.height = fh->height; |
| 763 | buf->vb.field = field; |
Mauro Carvalho Chehab | 47878f1 | 2007-11-15 16:37:35 -0300 | [diff] [blame] | 764 | buf->vb.state = VIDEOBUF_NEEDS_INIT; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 765 | } |
| 766 | |
Mauro Carvalho Chehab | 47878f1 | 2007-11-15 16:37:35 -0300 | [diff] [blame] | 767 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 768 | rc = videobuf_iolock(vq, &buf->vb, NULL); |
| 769 | if (rc != 0) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 770 | goto fail; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 771 | } |
| 772 | |
Thierry Reding | 88e834a | 2011-08-04 04:14:12 -0300 | [diff] [blame] | 773 | if (!dev->isoc_ctl.num_bufs) { |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 774 | rc = tm6000_prepare_isoc(dev); |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 775 | if (rc < 0) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 776 | goto fail; |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 777 | |
| 778 | rc = tm6000_start_thread(dev); |
| 779 | if (rc < 0) |
| 780 | goto fail; |
| 781 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 782 | } |
| 783 | |
Mauro Carvalho Chehab | 47878f1 | 2007-11-15 16:37:35 -0300 | [diff] [blame] | 784 | buf->vb.state = VIDEOBUF_PREPARED; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 785 | return 0; |
| 786 | |
| 787 | fail: |
Mauro Carvalho Chehab | ee1fc07 | 2008-01-10 17:27:26 -0300 | [diff] [blame] | 788 | free_buffer(vq, buf); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 789 | return rc; |
| 790 | } |
| 791 | |
| 792 | static void |
| 793 | buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 794 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 795 | struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 796 | struct tm6000_fh *fh = vq->priv_data; |
| 797 | struct tm6000_core *dev = fh->dev; |
| 798 | struct tm6000_dmaqueue *vidq = &dev->vidq; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 799 | |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 800 | buf->vb.state = VIDEOBUF_QUEUED; |
| 801 | list_add_tail(&buf->vb.queue, &vidq->active); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | static void buffer_release(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 805 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 806 | struct tm6000_buffer *buf = container_of(vb, struct tm6000_buffer, vb); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 807 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 808 | free_buffer(vq, buf); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | static struct videobuf_queue_ops tm6000_video_qops = { |
| 812 | .buf_setup = buffer_setup, |
| 813 | .buf_prepare = buffer_prepare, |
| 814 | .buf_queue = buffer_queue, |
| 815 | .buf_release = buffer_release, |
| 816 | }; |
| 817 | |
| 818 | /* ------------------------------------------------------------------ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 819 | * IOCTL handling |
| 820 | * ------------------------------------------------------------------ |
| 821 | */ |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 822 | |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 823 | static bool is_res_read(struct tm6000_core *dev, struct tm6000_fh *fh) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 824 | { |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 825 | /* Is the current fh handling it? if so, that's OK */ |
| 826 | if (dev->resources == fh && dev->is_res_read) |
| 827 | return true; |
| 828 | |
| 829 | return false; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 830 | } |
| 831 | |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 832 | static bool is_res_streaming(struct tm6000_core *dev, struct tm6000_fh *fh) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 833 | { |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 834 | /* Is the current fh handling it? if so, that's OK */ |
| 835 | if (dev->resources == fh) |
| 836 | return true; |
| 837 | |
| 838 | return false; |
| 839 | } |
| 840 | |
| 841 | static bool res_get(struct tm6000_core *dev, struct tm6000_fh *fh, |
| 842 | bool is_res_read) |
| 843 | { |
| 844 | /* Is the current fh handling it? if so, that's OK */ |
| 845 | if (dev->resources == fh && dev->is_res_read == is_res_read) |
| 846 | return true; |
| 847 | |
| 848 | /* is it free? */ |
| 849 | if (dev->resources) |
| 850 | return false; |
| 851 | |
| 852 | /* grab it */ |
| 853 | dev->resources = fh; |
| 854 | dev->is_res_read = is_res_read; |
| 855 | dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: get\n"); |
| 856 | return true; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | static void res_free(struct tm6000_core *dev, struct tm6000_fh *fh) |
| 860 | { |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 861 | /* Is the current fh handling it? if so, that's OK */ |
| 862 | if (dev->resources != fh) |
| 863 | return; |
| 864 | |
| 865 | dev->resources = NULL; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 866 | dprintk(dev, V4L2_DEBUG_RES_LOCK, "res: put\n"); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 867 | } |
| 868 | |
| 869 | /* ------------------------------------------------------------------ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 870 | * IOCTL vidioc handling |
| 871 | * ------------------------------------------------------------------ |
| 872 | */ |
| 873 | static int vidioc_querycap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 874 | struct v4l2_capability *cap) |
| 875 | { |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 876 | struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev; |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 877 | struct video_device *vdev = video_devdata(file); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 878 | |
| 879 | strlcpy(cap->driver, "tm6000", sizeof(cap->driver)); |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 880 | strlcpy(cap->card, "Trident TVMaster TM5600/6000/6010", sizeof(cap->card)); |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 881 | usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info)); |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 882 | if (dev->tuner_type != TUNER_ABSENT) |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 883 | cap->device_caps |= V4L2_CAP_TUNER; |
| 884 | if (vdev->vfl_type == VFL_TYPE_GRABBER) |
| 885 | cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE | |
| 886 | V4L2_CAP_STREAMING | |
| 887 | V4L2_CAP_READWRITE; |
| 888 | else |
| 889 | cap->device_caps |= V4L2_CAP_RADIO; |
| 890 | cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS | |
| 891 | V4L2_CAP_RADIO | V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE; |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 892 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 893 | return 0; |
| 894 | } |
| 895 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 896 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 897 | struct v4l2_fmtdesc *f) |
| 898 | { |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 899 | if (f->index >= ARRAY_SIZE(format)) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 900 | return -EINVAL; |
| 901 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 902 | strlcpy(f->description, format[f->index].name, sizeof(f->description)); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 903 | f->pixelformat = format[f->index].fourcc; |
| 904 | return 0; |
| 905 | } |
| 906 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 907 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 908 | struct v4l2_format *f) |
| 909 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 910 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 911 | |
| 912 | f->fmt.pix.width = fh->width; |
| 913 | f->fmt.pix.height = fh->height; |
| 914 | f->fmt.pix.field = fh->vb_vidq.field; |
| 915 | f->fmt.pix.pixelformat = fh->fmt->fourcc; |
Hans Verkuil | e618578 | 2012-09-11 11:51:02 -0300 | [diff] [blame] | 916 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 917 | f->fmt.pix.bytesperline = |
| 918 | (f->fmt.pix.width * fh->fmt->depth) >> 3; |
| 919 | f->fmt.pix.sizeimage = |
| 920 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
Hans Verkuil | ed57256 | 2013-02-01 09:08:46 -0300 | [diff] [blame] | 921 | f->fmt.pix.priv = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 922 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 923 | return 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 924 | } |
| 925 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 926 | static struct tm6000_fmt *format_by_fourcc(unsigned int fourcc) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 927 | { |
| 928 | unsigned int i; |
| 929 | |
| 930 | for (i = 0; i < ARRAY_SIZE(format); i++) |
| 931 | if (format[i].fourcc == fourcc) |
| 932 | return format+i; |
| 933 | return NULL; |
| 934 | } |
| 935 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 936 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 937 | struct v4l2_format *f) |
| 938 | { |
| 939 | struct tm6000_core *dev = ((struct tm6000_fh *)priv)->dev; |
| 940 | struct tm6000_fmt *fmt; |
| 941 | enum v4l2_field field; |
| 942 | |
| 943 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 944 | if (NULL == fmt) { |
| 945 | dprintk(dev, V4L2_DEBUG_IOCTL_ARG, "Fourcc format (0x%08x)" |
| 946 | " invalid.\n", f->fmt.pix.pixelformat); |
| 947 | return -EINVAL; |
| 948 | } |
| 949 | |
| 950 | field = f->fmt.pix.field; |
| 951 | |
Hans Verkuil | ed57256 | 2013-02-01 09:08:46 -0300 | [diff] [blame] | 952 | field = V4L2_FIELD_INTERLACED; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 953 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 954 | tm6000_get_std_res(dev); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 955 | |
Mauro Carvalho Chehab | 4475c04 | 2007-09-03 21:51:45 -0300 | [diff] [blame] | 956 | f->fmt.pix.width = dev->width; |
| 957 | f->fmt.pix.height = dev->height; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 958 | |
| 959 | f->fmt.pix.width &= ~0x01; |
| 960 | |
| 961 | f->fmt.pix.field = field; |
Hans Verkuil | ed57256 | 2013-02-01 09:08:46 -0300 | [diff] [blame] | 962 | f->fmt.pix.priv = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 963 | |
| 964 | f->fmt.pix.bytesperline = |
| 965 | (f->fmt.pix.width * fmt->depth) >> 3; |
| 966 | f->fmt.pix.sizeimage = |
| 967 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
Hans Verkuil | e618578 | 2012-09-11 11:51:02 -0300 | [diff] [blame] | 968 | f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 969 | |
| 970 | return 0; |
| 971 | } |
| 972 | |
| 973 | /*FIXME: This seems to be generic enough to be at videodev2 */ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 974 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 975 | struct v4l2_format *f) |
| 976 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 977 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 978 | struct tm6000_core *dev = fh->dev; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 979 | int ret = vidioc_try_fmt_vid_cap(file, fh, f); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 980 | if (ret < 0) |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 981 | return ret; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 982 | |
| 983 | fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 984 | fh->width = f->fmt.pix.width; |
| 985 | fh->height = f->fmt.pix.height; |
| 986 | fh->vb_vidq.field = f->fmt.pix.field; |
| 987 | fh->type = f->type; |
| 988 | |
| 989 | dev->fourcc = f->fmt.pix.pixelformat; |
| 990 | |
| 991 | tm6000_set_fourcc_format(dev); |
| 992 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 993 | return 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 994 | } |
| 995 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 996 | static int vidioc_reqbufs(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 997 | struct v4l2_requestbuffers *p) |
| 998 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 999 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1000 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1001 | return videobuf_reqbufs(&fh->vb_vidq, p); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1002 | } |
| 1003 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1004 | static int vidioc_querybuf(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1005 | struct v4l2_buffer *p) |
| 1006 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1007 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1008 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1009 | return videobuf_querybuf(&fh->vb_vidq, p); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1010 | } |
| 1011 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1012 | static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1013 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1014 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1015 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1016 | return videobuf_qbuf(&fh->vb_vidq, p); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1017 | } |
| 1018 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1019 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1020 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1021 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1022 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1023 | return videobuf_dqbuf(&fh->vb_vidq, p, |
| 1024 | file->f_flags & O_NONBLOCK); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1025 | } |
| 1026 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1027 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
| 1028 | { |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1029 | struct tm6000_fh *fh = priv; |
| 1030 | struct tm6000_core *dev = fh->dev; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1031 | |
| 1032 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1033 | return -EINVAL; |
| 1034 | if (i != fh->type) |
| 1035 | return -EINVAL; |
| 1036 | |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 1037 | if (!res_get(dev, fh, false)) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1038 | return -EBUSY; |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1039 | return videobuf_streamon(&fh->vb_vidq); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
| 1043 | { |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1044 | struct tm6000_fh *fh = priv; |
| 1045 | struct tm6000_core *dev = fh->dev; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1046 | |
| 1047 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) |
| 1048 | return -EINVAL; |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1049 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1050 | if (i != fh->type) |
| 1051 | return -EINVAL; |
| 1052 | |
| 1053 | videobuf_streamoff(&fh->vb_vidq); |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1054 | res_free(dev, fh); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1055 | |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1056 | return 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1057 | } |
| 1058 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 1059 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1060 | { |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1061 | int rc = 0; |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1062 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1063 | struct tm6000_core *dev = fh->dev; |
| 1064 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 1065 | dev->norm = norm; |
Mauro Carvalho Chehab | 709944e | 2010-10-07 02:28:24 -0300 | [diff] [blame] | 1066 | rc = tm6000_init_analog_mode(dev); |
Mauro Carvalho Chehab | 71e7cfa | 2007-09-06 20:12:10 -0300 | [diff] [blame] | 1067 | |
| 1068 | fh->width = dev->width; |
| 1069 | fh->height = dev->height; |
| 1070 | |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1071 | if (rc < 0) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1072 | return rc; |
| 1073 | |
Mauro Carvalho Chehab | 427f7fa | 2009-09-14 16:37:13 -0300 | [diff] [blame] | 1074 | v4l2_device_call_all(&dev->v4l2_dev, 0, core, s_std, dev->norm); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1079 | static const char *iname[] = { |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1080 | [TM6000_INPUT_TV] = "Television", |
| 1081 | [TM6000_INPUT_COMPOSITE1] = "Composite 1", |
| 1082 | [TM6000_INPUT_COMPOSITE2] = "Composite 2", |
| 1083 | [TM6000_INPUT_SVIDEO] = "S-Video", |
| 1084 | }; |
| 1085 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1086 | static int vidioc_enum_input(struct file *file, void *priv, |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1087 | struct v4l2_input *i) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1088 | { |
Dmitri Belimov | 2aefbc1 | 2011-03-17 21:08:55 -0300 | [diff] [blame] | 1089 | struct tm6000_fh *fh = priv; |
| 1090 | struct tm6000_core *dev = fh->dev; |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1091 | unsigned int n; |
Dmitri Belimov | 2aefbc1 | 2011-03-17 21:08:55 -0300 | [diff] [blame] | 1092 | |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1093 | n = i->index; |
| 1094 | if (n >= 3) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1095 | return -EINVAL; |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1096 | |
| 1097 | if (!dev->vinput[n].type) |
| 1098 | return -EINVAL; |
| 1099 | |
| 1100 | i->index = n; |
| 1101 | |
| 1102 | if (dev->vinput[n].type == TM6000_INPUT_TV) |
| 1103 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 1104 | else |
| 1105 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 1106 | |
| 1107 | strcpy(i->name, iname[dev->vinput[n].type]); |
| 1108 | |
| 1109 | i->std = TM6000_STD; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1110 | |
| 1111 | return 0; |
| 1112 | } |
| 1113 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1114 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1115 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1116 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1117 | struct tm6000_core *dev = fh->dev; |
| 1118 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1119 | *i = dev->input; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1120 | |
| 1121 | return 0; |
| 1122 | } |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1123 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1124 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1125 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1126 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1127 | struct tm6000_core *dev = fh->dev; |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1128 | int rc = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1129 | |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1130 | if (i >= 3) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1131 | return -EINVAL; |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1132 | if (!dev->vinput[i].type) |
| 1133 | return -EINVAL; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1134 | |
Stefan Ringel | b8f7bd8 | 2011-05-09 16:53:52 -0300 | [diff] [blame] | 1135 | dev->input = i; |
| 1136 | |
Hans Verkuil | 314527a | 2013-03-15 06:10:40 -0300 | [diff] [blame] | 1137 | rc = vidioc_s_std(file, priv, dev->vfd->current_norm); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1138 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1139 | return rc; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1140 | } |
| 1141 | |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 1142 | /* --- controls ---------------------------------------------- */ |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1143 | |
| 1144 | static int tm6000_s_ctrl(struct v4l2_ctrl *ctrl) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1145 | { |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1146 | struct tm6000_core *dev = container_of(ctrl->handler, struct tm6000_core, ctrl_handler); |
| 1147 | u8 val = ctrl->val; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1148 | |
| 1149 | switch (ctrl->id) { |
| 1150 | case V4L2_CID_CONTRAST: |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1151 | tm6000_set_reg(dev, TM6010_REQ07_R08_LUMA_CONTRAST_ADJ, val); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1152 | return 0; |
| 1153 | case V4L2_CID_BRIGHTNESS: |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1154 | tm6000_set_reg(dev, TM6010_REQ07_R09_LUMA_BRIGHTNESS_ADJ, val); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1155 | return 0; |
| 1156 | case V4L2_CID_SATURATION: |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1157 | tm6000_set_reg(dev, TM6010_REQ07_R0A_CHROMA_SATURATION_ADJ, val); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1158 | return 0; |
| 1159 | case V4L2_CID_HUE: |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1160 | tm6000_set_reg(dev, TM6010_REQ07_R0B_CHROMA_HUE_PHASE_ADJ, val); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1161 | return 0; |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1162 | } |
| 1163 | return -EINVAL; |
| 1164 | } |
| 1165 | |
| 1166 | static const struct v4l2_ctrl_ops tm6000_ctrl_ops = { |
| 1167 | .s_ctrl = tm6000_s_ctrl, |
| 1168 | }; |
| 1169 | |
| 1170 | static int tm6000_radio_s_ctrl(struct v4l2_ctrl *ctrl) |
| 1171 | { |
| 1172 | struct tm6000_core *dev = container_of(ctrl->handler, |
| 1173 | struct tm6000_core, radio_ctrl_handler); |
| 1174 | u8 val = ctrl->val; |
| 1175 | |
| 1176 | switch (ctrl->id) { |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1177 | case V4L2_CID_AUDIO_MUTE: |
| 1178 | dev->ctl_mute = val; |
| 1179 | tm6000_tvaudio_set_mute(dev, val); |
| 1180 | return 0; |
| 1181 | case V4L2_CID_AUDIO_VOLUME: |
| 1182 | dev->ctl_volume = val; |
| 1183 | tm6000_set_volume(dev, val); |
| 1184 | return 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1185 | } |
| 1186 | return -EINVAL; |
| 1187 | } |
| 1188 | |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1189 | static const struct v4l2_ctrl_ops tm6000_radio_ctrl_ops = { |
| 1190 | .s_ctrl = tm6000_radio_s_ctrl, |
| 1191 | }; |
| 1192 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1193 | static int vidioc_g_tuner(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1194 | struct v4l2_tuner *t) |
| 1195 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1196 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1197 | struct tm6000_core *dev = fh->dev; |
| 1198 | |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1199 | if (UNSET == dev->tuner_type) |
| 1200 | return -ENOTTY; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1201 | if (0 != t->index) |
| 1202 | return -EINVAL; |
| 1203 | |
| 1204 | strcpy(t->name, "Television"); |
| 1205 | t->type = V4L2_TUNER_ANALOG_TV; |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1206 | t->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1207 | t->rangehigh = 0xffffffffUL; |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 1208 | t->rxsubchans = V4L2_TUNER_SUB_STEREO; |
| 1209 | |
| 1210 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); |
| 1211 | |
| 1212 | t->audmode = dev->amode; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1213 | |
| 1214 | return 0; |
| 1215 | } |
| 1216 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1217 | static int vidioc_s_tuner(struct file *file, void *priv, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame] | 1218 | const struct v4l2_tuner *t) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1219 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1220 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1221 | struct tm6000_core *dev = fh->dev; |
| 1222 | |
| 1223 | if (UNSET == dev->tuner_type) |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1224 | return -ENOTTY; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1225 | if (0 != t->index) |
| 1226 | return -EINVAL; |
| 1227 | |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1228 | if (t->audmode > V4L2_TUNER_MODE_STEREO) |
| 1229 | dev->amode = V4L2_TUNER_MODE_STEREO; |
| 1230 | else |
| 1231 | dev->amode = t->audmode; |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 1232 | dprintk(dev, 3, "audio mode: %x\n", t->audmode); |
| 1233 | |
| 1234 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); |
Stefan Ringel | 0f6040e | 2011-05-09 16:53:53 -0300 | [diff] [blame] | 1235 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1236 | return 0; |
| 1237 | } |
| 1238 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1239 | static int vidioc_g_frequency(struct file *file, void *priv, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1240 | struct v4l2_frequency *f) |
| 1241 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1242 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1243 | struct tm6000_core *dev = fh->dev; |
| 1244 | |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1245 | if (UNSET == dev->tuner_type) |
| 1246 | return -ENOTTY; |
| 1247 | if (f->tuner) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1248 | return -EINVAL; |
| 1249 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1250 | f->frequency = dev->freq; |
| 1251 | |
Mauro Carvalho Chehab | 427f7fa | 2009-09-14 16:37:13 -0300 | [diff] [blame] | 1252 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_frequency, f); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1253 | |
| 1254 | return 0; |
| 1255 | } |
| 1256 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1257 | static int vidioc_s_frequency(struct file *file, void *priv, |
Hans Verkuil | b530a44 | 2013-03-19 04:09:26 -0300 | [diff] [blame] | 1258 | const struct v4l2_frequency *f) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1259 | { |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1260 | struct tm6000_fh *fh = priv; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1261 | struct tm6000_core *dev = fh->dev; |
| 1262 | |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1263 | if (UNSET == dev->tuner_type) |
| 1264 | return -ENOTTY; |
| 1265 | if (f->tuner != 0) |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1266 | return -EINVAL; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1267 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1268 | dev->freq = f->frequency; |
Mauro Carvalho Chehab | 64d339d | 2009-09-14 17:16:32 -0300 | [diff] [blame] | 1269 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_frequency, f); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1270 | |
| 1271 | return 0; |
| 1272 | } |
| 1273 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1274 | static int radio_g_tuner(struct file *file, void *priv, |
| 1275 | struct v4l2_tuner *t) |
| 1276 | { |
| 1277 | struct tm6000_fh *fh = file->private_data; |
| 1278 | struct tm6000_core *dev = fh->dev; |
| 1279 | |
| 1280 | if (0 != t->index) |
| 1281 | return -EINVAL; |
| 1282 | |
| 1283 | memset(t, 0, sizeof(*t)); |
| 1284 | strcpy(t->name, "Radio"); |
| 1285 | t->type = V4L2_TUNER_RADIO; |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1286 | t->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO; |
Stefan Ringel | 886a3c0 | 2011-05-09 16:53:50 -0300 | [diff] [blame] | 1287 | t->rxsubchans = V4L2_TUNER_SUB_STEREO; |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1288 | t->audmode = V4L2_TUNER_MODE_STEREO; |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1289 | |
| 1290 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, g_tuner, t); |
| 1291 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1292 | return 0; |
| 1293 | } |
| 1294 | |
| 1295 | static int radio_s_tuner(struct file *file, void *priv, |
Hans Verkuil | 2f73c7c | 2013-03-15 06:10:06 -0300 | [diff] [blame] | 1296 | const struct v4l2_tuner *t) |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1297 | { |
| 1298 | struct tm6000_fh *fh = file->private_data; |
| 1299 | struct tm6000_core *dev = fh->dev; |
| 1300 | |
| 1301 | if (0 != t->index) |
| 1302 | return -EINVAL; |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1303 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_tuner, t); |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1304 | return 0; |
| 1305 | } |
| 1306 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1307 | /* ------------------------------------------------------------------ |
| 1308 | File operations for the device |
| 1309 | ------------------------------------------------------------------*/ |
| 1310 | |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1311 | static int __tm6000_open(struct file *file) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1312 | { |
Mauro Carvalho Chehab | 0a34df5 | 2010-05-18 00:43:18 -0300 | [diff] [blame] | 1313 | struct video_device *vdev = video_devdata(file); |
| 1314 | struct tm6000_core *dev = video_drvdata(file); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1315 | struct tm6000_fh *fh; |
Mauro Carvalho Chehab | e8d0416 | 2010-05-18 04:27:27 -0300 | [diff] [blame] | 1316 | enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1317 | int rc; |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1318 | int radio = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1319 | |
Mauro Carvalho Chehab | 0a34df5 | 2010-05-18 00:43:18 -0300 | [diff] [blame] | 1320 | dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: open called (dev=%s)\n", |
| 1321 | video_device_node_name(vdev)); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1322 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1323 | switch (vdev->vfl_type) { |
| 1324 | case VFL_TYPE_GRABBER: |
| 1325 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 1326 | break; |
| 1327 | case VFL_TYPE_VBI: |
| 1328 | type = V4L2_BUF_TYPE_VBI_CAPTURE; |
| 1329 | break; |
| 1330 | case VFL_TYPE_RADIO: |
| 1331 | radio = 1; |
| 1332 | break; |
| 1333 | } |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1334 | |
| 1335 | /* If more than one user, mutex should be added */ |
| 1336 | dev->users++; |
| 1337 | |
Mauro Carvalho Chehab | 0a34df5 | 2010-05-18 00:43:18 -0300 | [diff] [blame] | 1338 | dprintk(dev, V4L2_DEBUG_OPEN, "open dev=%s type=%s users=%d\n", |
| 1339 | video_device_node_name(vdev), v4l2_type_names[type], |
| 1340 | dev->users); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1341 | |
| 1342 | /* allocate + initialize per filehandle data */ |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1343 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1344 | if (NULL == fh) { |
| 1345 | dev->users--; |
| 1346 | return -ENOMEM; |
| 1347 | } |
| 1348 | |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1349 | v4l2_fh_init(&fh->fh, vdev); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1350 | file->private_data = fh; |
| 1351 | fh->dev = dev; |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1352 | fh->radio = radio; |
| 1353 | dev->radio = radio; |
| 1354 | fh->type = type; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1355 | dev->fourcc = format[0].fourcc; |
| 1356 | |
| 1357 | fh->fmt = format_by_fourcc(dev->fourcc); |
Mauro Carvalho Chehab | 4475c04 | 2007-09-03 21:51:45 -0300 | [diff] [blame] | 1358 | |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1359 | tm6000_get_std_res(dev); |
Mauro Carvalho Chehab | 4475c04 | 2007-09-03 21:51:45 -0300 | [diff] [blame] | 1360 | |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1361 | fh->width = dev->width; |
| 1362 | fh->height = dev->height; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1363 | |
| 1364 | dprintk(dev, V4L2_DEBUG_OPEN, "Open: fh=0x%08lx, dev=0x%08lx, " |
| 1365 | "dev->vidq=0x%08lx\n", |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1366 | (unsigned long)fh, (unsigned long)dev, |
| 1367 | (unsigned long)&dev->vidq); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1368 | dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty " |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1369 | "queued=%d\n", list_empty(&dev->vidq.queued)); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1370 | dprintk(dev, V4L2_DEBUG_OPEN, "Open: list_empty " |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1371 | "active=%d\n", list_empty(&dev->vidq.active)); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1372 | |
| 1373 | /* initialize hardware on analog mode */ |
Mauro Carvalho Chehab | 589851d | 2010-10-12 12:11:55 -0300 | [diff] [blame] | 1374 | rc = tm6000_init_analog_mode(dev); |
| 1375 | if (rc < 0) |
| 1376 | return rc; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1377 | |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1378 | dev->mode = TM6000_MODE_ANALOG; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1379 | |
Thierry Reding | aa4a583 | 2011-08-04 04:14:15 -0300 | [diff] [blame] | 1380 | if (!fh->radio) { |
| 1381 | videobuf_queue_vmalloc_init(&fh->vb_vidq, &tm6000_video_qops, |
| 1382 | NULL, &dev->slock, |
| 1383 | fh->type, |
| 1384 | V4L2_FIELD_INTERLACED, |
| 1385 | sizeof(struct tm6000_buffer), fh, &dev->lock); |
| 1386 | } else { |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1387 | dprintk(dev, V4L2_DEBUG_OPEN, "video_open: setting radio device\n"); |
Stefan Ringel | 0f6040e | 2011-05-09 16:53:53 -0300 | [diff] [blame] | 1388 | tm6000_set_audio_rinput(dev); |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1389 | v4l2_device_call_all(&dev->v4l2_dev, 0, tuner, s_radio); |
| 1390 | tm6000_prepare_isoc(dev); |
| 1391 | tm6000_start_thread(dev); |
| 1392 | } |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1393 | v4l2_fh_add(&fh->fh); |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1394 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
| 1397 | |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1398 | static int tm6000_open(struct file *file) |
| 1399 | { |
| 1400 | struct video_device *vdev = video_devdata(file); |
| 1401 | int res; |
| 1402 | |
| 1403 | mutex_lock(vdev->lock); |
| 1404 | res = __tm6000_open(file); |
| 1405 | mutex_unlock(vdev->lock); |
| 1406 | return res; |
| 1407 | } |
| 1408 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1409 | static ssize_t |
| 1410 | tm6000_read(struct file *file, char __user *data, size_t count, loff_t *pos) |
| 1411 | { |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1412 | struct tm6000_fh *fh = file->private_data; |
| 1413 | struct tm6000_core *dev = fh->dev; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1414 | |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1415 | if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) { |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1416 | int res; |
| 1417 | |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 1418 | if (!res_get(fh->dev, fh, true)) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1419 | return -EBUSY; |
| 1420 | |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1421 | if (mutex_lock_interruptible(&dev->lock)) |
| 1422 | return -ERESTARTSYS; |
| 1423 | res = videobuf_read_stream(&fh->vb_vidq, data, count, pos, 0, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1424 | file->f_flags & O_NONBLOCK); |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1425 | mutex_unlock(&dev->lock); |
| 1426 | return res; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1427 | } |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
| 1431 | static unsigned int |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1432 | __tm6000_poll(struct file *file, struct poll_table_struct *wait) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1433 | { |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1434 | unsigned long req_events = poll_requested_events(wait); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1435 | struct tm6000_fh *fh = file->private_data; |
| 1436 | struct tm6000_buffer *buf; |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1437 | int res = 0; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1438 | |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1439 | if (v4l2_event_pending(&fh->fh)) |
| 1440 | res = POLLPRI; |
| 1441 | else if (req_events & POLLPRI) |
| 1442 | poll_wait(file, &fh->fh.wait, wait); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1443 | if (V4L2_BUF_TYPE_VIDEO_CAPTURE != fh->type) |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1444 | return res | POLLERR; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1445 | |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 1446 | if (!!is_res_streaming(fh->dev, fh)) |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1447 | return res | POLLERR; |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 1448 | |
| 1449 | if (!is_res_read(fh->dev, fh)) { |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1450 | /* streaming capture */ |
| 1451 | if (list_empty(&fh->vb_vidq.stream)) |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1452 | return res | POLLERR; |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1453 | buf = list_entry(fh->vb_vidq.stream.next, struct tm6000_buffer, vb.stream); |
Hans Verkuil | 82f0efb | 2013-02-07 07:05:43 -0300 | [diff] [blame] | 1454 | poll_wait(file, &buf->vb.done, wait); |
| 1455 | if (buf->vb.state == VIDEOBUF_DONE || |
| 1456 | buf->vb.state == VIDEOBUF_ERROR) |
| 1457 | return res | POLLIN | POLLRDNORM; |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1458 | } else if (req_events & (POLLIN | POLLRDNORM)) { |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1459 | /* read() capture */ |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1460 | return res | videobuf_poll_stream(file, &fh->vb_vidq, wait); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1461 | } |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1462 | return res; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1463 | } |
| 1464 | |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1465 | static unsigned int tm6000_poll(struct file *file, struct poll_table_struct *wait) |
| 1466 | { |
| 1467 | struct tm6000_fh *fh = file->private_data; |
| 1468 | struct tm6000_core *dev = fh->dev; |
| 1469 | unsigned int res; |
| 1470 | |
| 1471 | mutex_lock(&dev->lock); |
| 1472 | res = __tm6000_poll(file, wait); |
| 1473 | mutex_unlock(&dev->lock); |
| 1474 | return res; |
| 1475 | } |
| 1476 | |
Mauro Carvalho Chehab | 64d339d | 2009-09-14 17:16:32 -0300 | [diff] [blame] | 1477 | static int tm6000_release(struct file *file) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1478 | { |
| 1479 | struct tm6000_fh *fh = file->private_data; |
| 1480 | struct tm6000_core *dev = fh->dev; |
Mauro Carvalho Chehab | 0a34df5 | 2010-05-18 00:43:18 -0300 | [diff] [blame] | 1481 | struct video_device *vdev = video_devdata(file); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1482 | |
Mauro Carvalho Chehab | 0a34df5 | 2010-05-18 00:43:18 -0300 | [diff] [blame] | 1483 | dprintk(dev, V4L2_DEBUG_OPEN, "tm6000: close called (dev=%s, users=%d)\n", |
| 1484 | video_device_node_name(vdev), dev->users); |
Michel Ludwig | 7c3f53e | 2007-06-16 23:21:48 -0300 | [diff] [blame] | 1485 | |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1486 | mutex_lock(&dev->lock); |
Mauro Carvalho Chehab | a58d35c | 2007-06-17 17:14:12 -0300 | [diff] [blame] | 1487 | dev->users--; |
| 1488 | |
Mauro Carvalho Chehab | 9efd85d | 2010-10-11 10:48:11 -0300 | [diff] [blame] | 1489 | res_free(dev, fh); |
Thierry Reding | dd0c8ab | 2011-08-04 04:14:13 -0300 | [diff] [blame] | 1490 | |
Mauro Carvalho Chehab | a58d35c | 2007-06-17 17:14:12 -0300 | [diff] [blame] | 1491 | if (!dev->users) { |
Mauro Carvalho Chehab | c144c03 | 2008-04-09 01:49:19 -0300 | [diff] [blame] | 1492 | tm6000_uninit_isoc(dev); |
Thierry Reding | aa4a583 | 2011-08-04 04:14:15 -0300 | [diff] [blame] | 1493 | |
Stefan Ringel | 8159c18 | 2011-11-28 15:46:18 -0300 | [diff] [blame] | 1494 | /* Stop interrupt USB pipe */ |
| 1495 | tm6000_ir_int_stop(dev); |
| 1496 | |
| 1497 | usb_reset_configuration(dev->udev); |
| 1498 | |
Thierry Reding | 4be9c8f | 2011-12-06 09:39:35 -0300 | [diff] [blame] | 1499 | if (dev->int_in.endp) |
Stefan Ringel | 8159c18 | 2011-11-28 15:46:18 -0300 | [diff] [blame] | 1500 | usb_set_interface(dev->udev, |
Thierry Reding | 875f0e3 | 2011-12-06 09:39:36 -0300 | [diff] [blame] | 1501 | dev->isoc_in.bInterfaceNumber, 2); |
Stefan Ringel | 8159c18 | 2011-11-28 15:46:18 -0300 | [diff] [blame] | 1502 | else |
| 1503 | usb_set_interface(dev->udev, |
Thierry Reding | 875f0e3 | 2011-12-06 09:39:36 -0300 | [diff] [blame] | 1504 | dev->isoc_in.bInterfaceNumber, 0); |
Stefan Ringel | 8159c18 | 2011-11-28 15:46:18 -0300 | [diff] [blame] | 1505 | |
| 1506 | /* Start interrupt USB pipe */ |
| 1507 | tm6000_ir_int_start(dev); |
| 1508 | |
Thierry Reding | aa4a583 | 2011-08-04 04:14:15 -0300 | [diff] [blame] | 1509 | if (!fh->radio) |
| 1510 | videobuf_mmap_free(&fh->vb_vidq); |
Mauro Carvalho Chehab | a58d35c | 2007-06-17 17:14:12 -0300 | [diff] [blame] | 1511 | } |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1512 | v4l2_fh_del(&fh->fh); |
| 1513 | v4l2_fh_exit(&fh->fh); |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1514 | kfree(fh); |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1515 | mutex_unlock(&dev->lock); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1516 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1517 | return 0; |
| 1518 | } |
| 1519 | |
| 1520 | static int tm6000_mmap(struct file *file, struct vm_area_struct * vma) |
| 1521 | { |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1522 | struct tm6000_fh *fh = file->private_data; |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1523 | struct tm6000_core *dev = fh->dev; |
| 1524 | int res; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1525 | |
Hans Verkuil | 14a09da | 2012-06-24 07:26:46 -0300 | [diff] [blame] | 1526 | if (mutex_lock_interruptible(&dev->lock)) |
| 1527 | return -ERESTARTSYS; |
| 1528 | res = videobuf_mmap_mapper(&fh->vb_vidq, vma); |
| 1529 | mutex_unlock(&dev->lock); |
| 1530 | return res; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1531 | } |
| 1532 | |
Mauro Carvalho Chehab | 64d339d | 2009-09-14 17:16:32 -0300 | [diff] [blame] | 1533 | static struct v4l2_file_operations tm6000_fops = { |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1534 | .owner = THIS_MODULE, |
| 1535 | .open = tm6000_open, |
| 1536 | .release = tm6000_release, |
| 1537 | .unlocked_ioctl = video_ioctl2, /* V4L2 ioctl handler */ |
| 1538 | .read = tm6000_read, |
| 1539 | .poll = tm6000_poll, |
| 1540 | .mmap = tm6000_mmap, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1541 | }; |
| 1542 | |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 1543 | static const struct v4l2_ioctl_ops video_ioctl_ops = { |
| 1544 | .vidioc_querycap = vidioc_querycap, |
| 1545 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1546 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1547 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1548 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 1549 | .vidioc_s_std = vidioc_s_std, |
| 1550 | .vidioc_enum_input = vidioc_enum_input, |
| 1551 | .vidioc_g_input = vidioc_g_input, |
| 1552 | .vidioc_s_input = vidioc_s_input, |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 1553 | .vidioc_g_tuner = vidioc_g_tuner, |
| 1554 | .vidioc_s_tuner = vidioc_s_tuner, |
| 1555 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1556 | .vidioc_s_frequency = vidioc_s_frequency, |
| 1557 | .vidioc_streamon = vidioc_streamon, |
| 1558 | .vidioc_streamoff = vidioc_streamoff, |
| 1559 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1560 | .vidioc_querybuf = vidioc_querybuf, |
| 1561 | .vidioc_qbuf = vidioc_qbuf, |
| 1562 | .vidioc_dqbuf = vidioc_dqbuf, |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1563 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1564 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 1565 | }; |
| 1566 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1567 | static struct video_device tm6000_template = { |
| 1568 | .name = "tm6000", |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1569 | .fops = &tm6000_fops, |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 1570 | .ioctl_ops = &video_ioctl_ops, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1571 | .release = video_device_release, |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 1572 | .tvnorms = TM6000_STD, |
| 1573 | .current_norm = V4L2_STD_NTSC_M, |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1574 | }; |
Mauro Carvalho Chehab | 2a8145d | 2008-10-25 10:43:04 -0300 | [diff] [blame] | 1575 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1576 | static const struct v4l2_file_operations radio_fops = { |
Stefan Ringel | 1f38571 | 2011-05-09 16:54:01 -0300 | [diff] [blame] | 1577 | .owner = THIS_MODULE, |
| 1578 | .open = tm6000_open, |
Hans Verkuil | 52dec54 | 2012-09-11 11:53:51 -0300 | [diff] [blame] | 1579 | .poll = v4l2_ctrl_poll, |
Stefan Ringel | 1f38571 | 2011-05-09 16:54:01 -0300 | [diff] [blame] | 1580 | .release = tm6000_release, |
| 1581 | .unlocked_ioctl = video_ioctl2, |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1582 | }; |
| 1583 | |
| 1584 | static const struct v4l2_ioctl_ops radio_ioctl_ops = { |
Hans Verkuil | 2c2a053 | 2012-09-11 07:35:30 -0300 | [diff] [blame] | 1585 | .vidioc_querycap = vidioc_querycap, |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1586 | .vidioc_g_tuner = radio_g_tuner, |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1587 | .vidioc_s_tuner = radio_s_tuner, |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1588 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1589 | .vidioc_s_frequency = vidioc_s_frequency, |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1590 | .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, |
| 1591 | .vidioc_unsubscribe_event = v4l2_event_unsubscribe, |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1592 | }; |
| 1593 | |
Thierry Reding | 3d1a51d | 2011-08-04 04:14:01 -0300 | [diff] [blame] | 1594 | static struct video_device tm6000_radio_template = { |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1595 | .name = "tm6000", |
| 1596 | .fops = &radio_fops, |
Curtis McEnroe | ed7c221 | 2011-06-01 22:21:56 -0400 | [diff] [blame] | 1597 | .ioctl_ops = &radio_ioctl_ops, |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1598 | }; |
| 1599 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1600 | /* ----------------------------------------------------------------- |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1601 | * Initialization and module stuff |
| 1602 | * ------------------------------------------------------------------ |
| 1603 | */ |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1604 | |
Dmitri Belimov | 3c61be4 | 2011-01-13 00:46:07 -0300 | [diff] [blame] | 1605 | static struct video_device *vdev_init(struct tm6000_core *dev, |
| 1606 | const struct video_device |
| 1607 | *template, const char *type_name) |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1608 | { |
Michel Ludwig | 7c3f53e | 2007-06-16 23:21:48 -0300 | [diff] [blame] | 1609 | struct video_device *vfd; |
| 1610 | |
| 1611 | vfd = video_device_alloc(); |
Dmitri Belimov | 3c61be4 | 2011-01-13 00:46:07 -0300 | [diff] [blame] | 1612 | if (NULL == vfd) |
| 1613 | return NULL; |
| 1614 | |
| 1615 | *vfd = *template; |
| 1616 | vfd->v4l2_dev = &dev->v4l2_dev; |
| 1617 | vfd->release = video_device_release; |
| 1618 | vfd->debug = tm6000_debug; |
| 1619 | vfd->lock = &dev->lock; |
Hans Verkuil | 770056c | 2012-09-11 11:50:37 -0300 | [diff] [blame] | 1620 | set_bit(V4L2_FL_USE_FH_PRIO, &vfd->flags); |
Dmitri Belimov | 3c61be4 | 2011-01-13 00:46:07 -0300 | [diff] [blame] | 1621 | |
| 1622 | snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name); |
| 1623 | |
| 1624 | video_set_drvdata(vfd, dev); |
| 1625 | return vfd; |
| 1626 | } |
| 1627 | |
| 1628 | int tm6000_v4l2_register(struct tm6000_core *dev) |
| 1629 | { |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1630 | int ret = 0; |
| 1631 | |
| 1632 | v4l2_ctrl_handler_init(&dev->ctrl_handler, 6); |
| 1633 | v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 2); |
| 1634 | v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops, |
| 1635 | V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0); |
| 1636 | v4l2_ctrl_new_std(&dev->radio_ctrl_handler, &tm6000_radio_ctrl_ops, |
| 1637 | V4L2_CID_AUDIO_VOLUME, -15, 15, 1, 0); |
| 1638 | v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops, |
| 1639 | V4L2_CID_BRIGHTNESS, 0, 255, 1, 54); |
| 1640 | v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops, |
| 1641 | V4L2_CID_CONTRAST, 0, 255, 1, 119); |
| 1642 | v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops, |
| 1643 | V4L2_CID_SATURATION, 0, 255, 1, 112); |
| 1644 | v4l2_ctrl_new_std(&dev->ctrl_handler, &tm6000_ctrl_ops, |
| 1645 | V4L2_CID_HUE, -128, 127, 1, 0); |
| 1646 | v4l2_ctrl_add_handler(&dev->ctrl_handler, |
| 1647 | &dev->radio_ctrl_handler, NULL); |
| 1648 | |
| 1649 | if (dev->radio_ctrl_handler.error) |
| 1650 | ret = dev->radio_ctrl_handler.error; |
| 1651 | if (!ret && dev->ctrl_handler.error) |
| 1652 | ret = dev->ctrl_handler.error; |
| 1653 | if (ret) |
| 1654 | goto free_ctrl; |
Dmitri Belimov | 3c61be4 | 2011-01-13 00:46:07 -0300 | [diff] [blame] | 1655 | |
| 1656 | dev->vfd = vdev_init(dev, &tm6000_template, "video"); |
| 1657 | |
| 1658 | if (!dev->vfd) { |
| 1659 | printk(KERN_INFO "%s: can't register video device\n", |
| 1660 | dev->name); |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1661 | ret = -ENOMEM; |
| 1662 | goto free_ctrl; |
Michel Ludwig | 7c3f53e | 2007-06-16 23:21:48 -0300 | [diff] [blame] | 1663 | } |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1664 | dev->vfd->ctrl_handler = &dev->ctrl_handler; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1665 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1666 | /* init video dma queues */ |
| 1667 | INIT_LIST_HEAD(&dev->vidq.active); |
| 1668 | INIT_LIST_HEAD(&dev->vidq.queued); |
| 1669 | |
Michel Ludwig | 7c3f53e | 2007-06-16 23:21:48 -0300 | [diff] [blame] | 1670 | ret = video_register_device(dev->vfd, VFL_TYPE_GRABBER, video_nr); |
Dmitri Belimov | 3c61be4 | 2011-01-13 00:46:07 -0300 | [diff] [blame] | 1671 | |
| 1672 | if (ret < 0) { |
| 1673 | printk(KERN_INFO "%s: can't register video device\n", |
| 1674 | dev->name); |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1675 | video_device_release(dev->vfd); |
| 1676 | dev->vfd = NULL; |
| 1677 | goto free_ctrl; |
Dmitri Belimov | 3c61be4 | 2011-01-13 00:46:07 -0300 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | printk(KERN_INFO "%s: registered device %s\n", |
| 1681 | dev->name, video_device_node_name(dev->vfd)); |
| 1682 | |
Stefan Ringel | 1416910 | 2011-05-09 16:53:49 -0300 | [diff] [blame] | 1683 | if (dev->caps.has_radio) { |
| 1684 | dev->radio_dev = vdev_init(dev, &tm6000_radio_template, |
| 1685 | "radio"); |
| 1686 | if (!dev->radio_dev) { |
| 1687 | printk(KERN_INFO "%s: can't register radio device\n", |
| 1688 | dev->name); |
Peter Senna Tschudin | 97b55f6 | 2012-09-06 11:23:49 -0300 | [diff] [blame] | 1689 | ret = -ENXIO; |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1690 | goto unreg_video; |
Stefan Ringel | 1416910 | 2011-05-09 16:53:49 -0300 | [diff] [blame] | 1691 | } |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1692 | |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1693 | dev->radio_dev->ctrl_handler = &dev->radio_ctrl_handler; |
Stefan Ringel | 1416910 | 2011-05-09 16:53:49 -0300 | [diff] [blame] | 1694 | ret = video_register_device(dev->radio_dev, VFL_TYPE_RADIO, |
| 1695 | radio_nr); |
| 1696 | if (ret < 0) { |
| 1697 | printk(KERN_INFO "%s: can't register radio device\n", |
| 1698 | dev->name); |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1699 | video_device_release(dev->radio_dev); |
| 1700 | goto unreg_video; |
Stefan Ringel | 1416910 | 2011-05-09 16:53:49 -0300 | [diff] [blame] | 1701 | } |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1702 | |
Stefan Ringel | 1416910 | 2011-05-09 16:53:49 -0300 | [diff] [blame] | 1703 | printk(KERN_INFO "%s: registered device %s\n", |
| 1704 | dev->name, video_device_node_name(dev->radio_dev)); |
| 1705 | } |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1706 | |
Dmitri Belimov | e28f49b | 2010-02-22 06:32:15 -0300 | [diff] [blame] | 1707 | printk(KERN_INFO "Trident TVMaster TM5600/TM6000/TM6010 USB2 board (Load status: %d)\n", ret); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1708 | return ret; |
Hans Verkuil | 9f74735 | 2013-01-31 08:23:01 -0300 | [diff] [blame] | 1709 | |
| 1710 | unreg_video: |
| 1711 | video_unregister_device(dev->vfd); |
| 1712 | free_ctrl: |
| 1713 | v4l2_ctrl_handler_free(&dev->ctrl_handler); |
| 1714 | v4l2_ctrl_handler_free(&dev->radio_ctrl_handler); |
| 1715 | return ret; |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | int tm6000_v4l2_unregister(struct tm6000_core *dev) |
| 1719 | { |
Michel Ludwig | 7c3f53e | 2007-06-16 23:21:48 -0300 | [diff] [blame] | 1720 | video_unregister_device(dev->vfd); |
Michel Ludwig | 22927e8 | 2007-06-14 17:19:59 -0300 | [diff] [blame] | 1721 | |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 1722 | /* if URB buffers are still allocated free them now */ |
| 1723 | tm6000_free_urb_buffers(dev); |
| 1724 | |
Dmitri Belimov | 8aff8ba | 2011-02-17 22:11:05 -0300 | [diff] [blame] | 1725 | if (dev->radio_dev) { |
| 1726 | if (video_is_registered(dev->radio_dev)) |
| 1727 | video_unregister_device(dev->radio_dev); |
| 1728 | else |
| 1729 | video_device_release(dev->radio_dev); |
| 1730 | dev->radio_dev = NULL; |
| 1731 | } |
| 1732 | |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1733 | return 0; |
| 1734 | } |
| 1735 | |
| 1736 | int tm6000_v4l2_exit(void) |
| 1737 | { |
| 1738 | return 0; |
| 1739 | } |
| 1740 | |
| 1741 | module_param(video_nr, int, 0); |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1742 | MODULE_PARM_DESC(video_nr, "Allow changing video device number"); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1743 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1744 | module_param_named(debug, tm6000_debug, int, 0444); |
| 1745 | MODULE_PARM_DESC(debug, "activates debug info"); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1746 | |
Ruslan Pisarev | 60fbfdf | 2010-10-20 06:35:12 -0300 | [diff] [blame] | 1747 | module_param(vid_limit, int, 0644); |
| 1748 | MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes"); |
Mauro Carvalho Chehab | 9701dc9 | 2009-09-14 09:42:41 -0300 | [diff] [blame] | 1749 | |
Julian Scheel | 16427fa | 2012-10-04 10:04:28 -0300 | [diff] [blame] | 1750 | module_param(keep_urb, bool, 0); |
| 1751 | MODULE_PARM_DESC(keep_urb, "Keep urb buffers allocated even when the device is closed by the user"); |