Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Driver for the Conexant CX23885 PCIe bridge |
| 3 | * |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 4 | * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org> |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <linux/init.h> |
| 23 | #include <linux/list.h> |
| 24 | #include <linux/module.h> |
| 25 | #include <linux/moduleparam.h> |
| 26 | #include <linux/kmod.h> |
| 27 | #include <linux/kernel.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/delay.h> |
| 31 | #include <linux/kthread.h> |
| 32 | #include <asm/div64.h> |
| 33 | |
| 34 | #include "cx23885.h" |
| 35 | #include <media/v4l2-common.h> |
Hans Verkuil | 35ea11f | 2008-07-20 08:12:02 -0300 | [diff] [blame] | 36 | #include <media/v4l2-ioctl.h> |
Andy Walls | 7461824 | 2009-09-26 22:50:44 -0300 | [diff] [blame] | 37 | #include "cx23885-ioctl.h" |
Kusanagi Kouichi | 0b32d65 | 2010-01-22 04:55:28 -0300 | [diff] [blame] | 38 | #include "tuner-xc2028.h" |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 39 | |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 40 | #include <media/cx25840.h> |
| 41 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 42 | MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards"); |
Steven Toth | 6d89761 | 2008-09-03 17:12:12 -0300 | [diff] [blame] | 43 | MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>"); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 44 | MODULE_LICENSE("GPL"); |
| 45 | |
| 46 | /* ------------------------------------------------------------------ */ |
| 47 | |
| 48 | static unsigned int video_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET }; |
| 49 | static unsigned int vbi_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET }; |
| 50 | static unsigned int radio_nr[] = {[0 ... (CX23885_MAXBOARDS - 1)] = UNSET }; |
| 51 | |
| 52 | module_param_array(video_nr, int, NULL, 0444); |
| 53 | module_param_array(vbi_nr, int, NULL, 0444); |
| 54 | module_param_array(radio_nr, int, NULL, 0444); |
| 55 | |
| 56 | MODULE_PARM_DESC(video_nr, "video device numbers"); |
| 57 | MODULE_PARM_DESC(vbi_nr, "vbi device numbers"); |
| 58 | MODULE_PARM_DESC(radio_nr, "radio device numbers"); |
| 59 | |
Steven Toth | 4513fc69 | 2008-01-12 11:36:36 -0300 | [diff] [blame] | 60 | static unsigned int video_debug; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 61 | module_param(video_debug, int, 0644); |
| 62 | MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); |
| 63 | |
Steven Toth | 4513fc69 | 2008-01-12 11:36:36 -0300 | [diff] [blame] | 64 | static unsigned int irq_debug; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 65 | module_param(irq_debug, int, 0644); |
| 66 | MODULE_PARM_DESC(irq_debug, "enable debug messages [IRQ handler]"); |
| 67 | |
| 68 | static unsigned int vid_limit = 16; |
| 69 | module_param(vid_limit, int, 0644); |
| 70 | MODULE_PARM_DESC(vid_limit, "capture memory limit in megabytes"); |
| 71 | |
| 72 | #define dprintk(level, fmt, arg...)\ |
Steven Toth | 4513fc69 | 2008-01-12 11:36:36 -0300 | [diff] [blame] | 73 | do { if (video_debug >= level)\ |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 74 | printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\ |
Steven Toth | 4513fc69 | 2008-01-12 11:36:36 -0300 | [diff] [blame] | 75 | } while (0) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 76 | |
| 77 | /* ------------------------------------------------------------------- */ |
| 78 | /* static data */ |
| 79 | |
| 80 | #define FORMAT_FLAGS_PACKED 0x01 |
Steven Toth | af76e9f | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 81 | #if 0 |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 82 | static struct cx23885_fmt formats[] = { |
| 83 | { |
| 84 | .name = "8 bpp, gray", |
| 85 | .fourcc = V4L2_PIX_FMT_GREY, |
| 86 | .depth = 8, |
| 87 | .flags = FORMAT_FLAGS_PACKED, |
| 88 | }, { |
| 89 | .name = "15 bpp RGB, le", |
| 90 | .fourcc = V4L2_PIX_FMT_RGB555, |
| 91 | .depth = 16, |
| 92 | .flags = FORMAT_FLAGS_PACKED, |
| 93 | }, { |
| 94 | .name = "15 bpp RGB, be", |
| 95 | .fourcc = V4L2_PIX_FMT_RGB555X, |
| 96 | .depth = 16, |
| 97 | .flags = FORMAT_FLAGS_PACKED, |
| 98 | }, { |
| 99 | .name = "16 bpp RGB, le", |
| 100 | .fourcc = V4L2_PIX_FMT_RGB565, |
| 101 | .depth = 16, |
| 102 | .flags = FORMAT_FLAGS_PACKED, |
| 103 | }, { |
| 104 | .name = "16 bpp RGB, be", |
| 105 | .fourcc = V4L2_PIX_FMT_RGB565X, |
| 106 | .depth = 16, |
| 107 | .flags = FORMAT_FLAGS_PACKED, |
| 108 | }, { |
| 109 | .name = "24 bpp RGB, le", |
| 110 | .fourcc = V4L2_PIX_FMT_BGR24, |
| 111 | .depth = 24, |
| 112 | .flags = FORMAT_FLAGS_PACKED, |
| 113 | }, { |
| 114 | .name = "32 bpp RGB, le", |
| 115 | .fourcc = V4L2_PIX_FMT_BGR32, |
| 116 | .depth = 32, |
| 117 | .flags = FORMAT_FLAGS_PACKED, |
| 118 | }, { |
| 119 | .name = "32 bpp RGB, be", |
| 120 | .fourcc = V4L2_PIX_FMT_RGB32, |
| 121 | .depth = 32, |
| 122 | .flags = FORMAT_FLAGS_PACKED, |
| 123 | }, { |
| 124 | .name = "4:2:2, packed, YUYV", |
| 125 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 126 | .depth = 16, |
| 127 | .flags = FORMAT_FLAGS_PACKED, |
| 128 | }, { |
| 129 | .name = "4:2:2, packed, UYVY", |
| 130 | .fourcc = V4L2_PIX_FMT_UYVY, |
| 131 | .depth = 16, |
| 132 | .flags = FORMAT_FLAGS_PACKED, |
| 133 | }, |
| 134 | }; |
Steven Toth | af76e9f | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 135 | #else |
| 136 | static struct cx23885_fmt formats[] = { |
| 137 | { |
| 138 | #if 0 |
| 139 | .name = "4:2:2, packed, UYVY", |
| 140 | .fourcc = V4L2_PIX_FMT_UYVY, |
| 141 | .depth = 16, |
| 142 | .flags = FORMAT_FLAGS_PACKED, |
| 143 | }, { |
| 144 | #endif |
| 145 | .name = "4:2:2, packed, YUYV", |
| 146 | .fourcc = V4L2_PIX_FMT_YUYV, |
| 147 | .depth = 16, |
| 148 | .flags = FORMAT_FLAGS_PACKED, |
| 149 | } |
| 150 | }; |
| 151 | #endif |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 152 | |
| 153 | static struct cx23885_fmt *format_by_fourcc(unsigned int fourcc) |
| 154 | { |
| 155 | unsigned int i; |
| 156 | |
| 157 | for (i = 0; i < ARRAY_SIZE(formats); i++) |
| 158 | if (formats[i].fourcc == fourcc) |
| 159 | return formats+i; |
| 160 | |
Steven Toth | af76e9f | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 161 | printk(KERN_ERR "%s(%c%c%c%c) NOT FOUND\n", __func__, |
| 162 | (fourcc & 0xff), |
| 163 | ((fourcc >> 8) & 0xff), |
| 164 | ((fourcc >> 16) & 0xff), |
| 165 | ((fourcc >> 24) & 0xff) |
| 166 | ); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | /* ------------------------------------------------------------------- */ |
| 171 | |
| 172 | static const struct v4l2_queryctrl no_ctl = { |
| 173 | .name = "42", |
| 174 | .flags = V4L2_CTRL_FLAG_DISABLED, |
| 175 | }; |
| 176 | |
| 177 | static struct cx23885_ctrl cx23885_ctls[] = { |
| 178 | /* --- video --- */ |
| 179 | { |
| 180 | .v = { |
| 181 | .id = V4L2_CID_BRIGHTNESS, |
| 182 | .name = "Brightness", |
| 183 | .minimum = 0x00, |
| 184 | .maximum = 0xff, |
| 185 | .step = 1, |
| 186 | .default_value = 0x7f, |
| 187 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 188 | }, |
| 189 | .off = 128, |
| 190 | .reg = LUMA_CTRL, |
| 191 | .mask = 0x00ff, |
| 192 | .shift = 0, |
| 193 | }, { |
| 194 | .v = { |
| 195 | .id = V4L2_CID_CONTRAST, |
| 196 | .name = "Contrast", |
| 197 | .minimum = 0, |
Mijhail Moreyra | b12ab3f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 198 | .maximum = 0x7f, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 199 | .step = 1, |
| 200 | .default_value = 0x3f, |
| 201 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 202 | }, |
| 203 | .off = 0, |
| 204 | .reg = LUMA_CTRL, |
| 205 | .mask = 0xff00, |
| 206 | .shift = 8, |
| 207 | }, { |
| 208 | .v = { |
| 209 | .id = V4L2_CID_HUE, |
| 210 | .name = "Hue", |
Mijhail Moreyra | b12ab3f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 211 | .minimum = -127, |
| 212 | .maximum = 128, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 213 | .step = 1, |
Mijhail Moreyra | b12ab3f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 214 | .default_value = 0x0, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 215 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 216 | }, |
| 217 | .off = 128, |
| 218 | .reg = CHROMA_CTRL, |
| 219 | .mask = 0xff0000, |
| 220 | .shift = 16, |
| 221 | }, { |
| 222 | /* strictly, this only describes only U saturation. |
| 223 | * V saturation is handled specially through code. |
| 224 | */ |
| 225 | .v = { |
| 226 | .id = V4L2_CID_SATURATION, |
| 227 | .name = "Saturation", |
| 228 | .minimum = 0, |
Mijhail Moreyra | b12ab3f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 229 | .maximum = 0x7f, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 230 | .step = 1, |
Mijhail Moreyra | b12ab3f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 231 | .default_value = 0x3f, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 232 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 233 | }, |
| 234 | .off = 0, |
| 235 | .reg = CHROMA_CTRL, |
| 236 | .mask = 0x00ff, |
| 237 | .shift = 0, |
| 238 | }, { |
| 239 | /* --- audio --- */ |
| 240 | .v = { |
| 241 | .id = V4L2_CID_AUDIO_MUTE, |
| 242 | .name = "Mute", |
| 243 | .minimum = 0, |
| 244 | .maximum = 1, |
| 245 | .default_value = 1, |
| 246 | .type = V4L2_CTRL_TYPE_BOOLEAN, |
| 247 | }, |
| 248 | .reg = PATH1_CTL1, |
| 249 | .mask = (0x1f << 24), |
| 250 | .shift = 24, |
| 251 | }, { |
| 252 | .v = { |
| 253 | .id = V4L2_CID_AUDIO_VOLUME, |
| 254 | .name = "Volume", |
| 255 | .minimum = 0, |
| 256 | .maximum = 0x3f, |
| 257 | .step = 1, |
| 258 | .default_value = 0x3f, |
| 259 | .type = V4L2_CTRL_TYPE_INTEGER, |
| 260 | }, |
| 261 | .reg = PATH1_VOL_CTL, |
| 262 | .mask = 0xff, |
| 263 | .shift = 0, |
| 264 | } |
| 265 | }; |
| 266 | static const int CX23885_CTLS = ARRAY_SIZE(cx23885_ctls); |
| 267 | |
Hans Verkuil | 2ba5889 | 2009-02-13 10:57:48 -0300 | [diff] [blame] | 268 | /* Must be sorted from low to high control ID! */ |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 269 | static const u32 cx23885_user_ctrls[] = { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 270 | V4L2_CID_USER_CLASS, |
| 271 | V4L2_CID_BRIGHTNESS, |
| 272 | V4L2_CID_CONTRAST, |
| 273 | V4L2_CID_SATURATION, |
| 274 | V4L2_CID_HUE, |
| 275 | V4L2_CID_AUDIO_VOLUME, |
| 276 | V4L2_CID_AUDIO_MUTE, |
| 277 | 0 |
| 278 | }; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 279 | |
| 280 | static const u32 *ctrl_classes[] = { |
| 281 | cx23885_user_ctrls, |
| 282 | NULL |
| 283 | }; |
| 284 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 285 | void cx23885_video_wakeup(struct cx23885_dev *dev, |
| 286 | struct cx23885_dmaqueue *q, u32 count) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 287 | { |
| 288 | struct cx23885_buffer *buf; |
| 289 | int bc; |
| 290 | |
| 291 | for (bc = 0;; bc++) { |
| 292 | if (list_empty(&q->active)) |
| 293 | break; |
| 294 | buf = list_entry(q->active.next, |
| 295 | struct cx23885_buffer, vb.queue); |
Steven Toth | a19602f2 | 2008-01-10 11:43:18 -0300 | [diff] [blame] | 296 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 297 | /* count comes from the hw and is is 16bit wide -- |
| 298 | * this trick handles wrap-arounds correctly for |
| 299 | * up to 32767 buffers in flight... */ |
| 300 | if ((s16) (count - buf->count) < 0) |
| 301 | break; |
Steven Toth | a19602f2 | 2008-01-10 11:43:18 -0300 | [diff] [blame] | 302 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 303 | do_gettimeofday(&buf->vb.ts); |
| 304 | dprintk(2, "[%p/%d] wakeup reg=%d buf=%d\n", buf, buf->vb.i, |
| 305 | count, buf->count); |
| 306 | buf->vb.state = VIDEOBUF_DONE; |
| 307 | list_del(&buf->vb.queue); |
| 308 | wake_up(&buf->vb.done); |
| 309 | } |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 310 | if (list_empty(&q->active)) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 311 | del_timer(&q->timeout); |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 312 | else |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 313 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 314 | if (bc != 1) |
Steven Toth | 21a7809 | 2008-01-10 04:38:59 -0300 | [diff] [blame] | 315 | printk(KERN_ERR "%s: %d buffers handled (should be 1)\n", |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 316 | __func__, bc); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 317 | } |
| 318 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 319 | static int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 320 | { |
| 321 | dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 322 | __func__, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 323 | (unsigned int)norm, |
| 324 | v4l2_norm_to_name(norm)); |
| 325 | |
| 326 | dev->tvnorm = norm; |
| 327 | |
Hans Verkuil | f41737e | 2009-04-01 03:52:39 -0300 | [diff] [blame] | 328 | call_all(dev, core, s_std, norm); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 333 | static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 334 | struct pci_dev *pci, |
| 335 | struct video_device *template, |
| 336 | char *type) |
| 337 | { |
| 338 | struct video_device *vfd; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 339 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 340 | |
| 341 | vfd = video_device_alloc(); |
| 342 | if (NULL == vfd) |
| 343 | return NULL; |
| 344 | *vfd = *template; |
Hans Verkuil | c0714f6 | 2009-03-13 08:02:43 -0300 | [diff] [blame] | 345 | vfd->v4l2_dev = &dev->v4l2_dev; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 346 | vfd->release = video_device_release; |
| 347 | snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", |
| 348 | dev->name, type, cx23885_boards[dev->board].name); |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 349 | video_set_drvdata(vfd, dev); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 350 | return vfd; |
| 351 | } |
| 352 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 353 | static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 354 | { |
| 355 | int i; |
| 356 | |
| 357 | if (qctrl->id < V4L2_CID_BASE || |
| 358 | qctrl->id >= V4L2_CID_LASTP1) |
| 359 | return -EINVAL; |
| 360 | for (i = 0; i < CX23885_CTLS; i++) |
| 361 | if (cx23885_ctls[i].v.id == qctrl->id) |
| 362 | break; |
| 363 | if (i == CX23885_CTLS) { |
| 364 | *qctrl = no_ctl; |
| 365 | return 0; |
| 366 | } |
| 367 | *qctrl = cx23885_ctls[i].v; |
| 368 | return 0; |
| 369 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 370 | |
| 371 | /* ------------------------------------------------------------------- */ |
| 372 | /* resource management */ |
| 373 | |
| 374 | static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh, |
| 375 | unsigned int bit) |
| 376 | { |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 377 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 378 | if (fh->resources & bit) |
| 379 | /* have it already allocated */ |
| 380 | return 1; |
| 381 | |
| 382 | /* is it free? */ |
| 383 | mutex_lock(&dev->lock); |
| 384 | if (dev->resources & bit) { |
| 385 | /* no, someone else uses it */ |
| 386 | mutex_unlock(&dev->lock); |
| 387 | return 0; |
| 388 | } |
| 389 | /* it's free, grab it */ |
| 390 | fh->resources |= bit; |
| 391 | dev->resources |= bit; |
| 392 | dprintk(1, "res: get %d\n", bit); |
| 393 | mutex_unlock(&dev->lock); |
| 394 | return 1; |
| 395 | } |
| 396 | |
| 397 | static int res_check(struct cx23885_fh *fh, unsigned int bit) |
| 398 | { |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 399 | return fh->resources & bit; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | static int res_locked(struct cx23885_dev *dev, unsigned int bit) |
| 403 | { |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 404 | return dev->resources & bit; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static void res_free(struct cx23885_dev *dev, struct cx23885_fh *fh, |
| 408 | unsigned int bits) |
| 409 | { |
| 410 | BUG_ON((fh->resources & bits) != bits); |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 411 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 412 | |
| 413 | mutex_lock(&dev->lock); |
| 414 | fh->resources &= ~bits; |
| 415 | dev->resources &= ~bits; |
| 416 | dprintk(1, "res: put %d\n", bits); |
| 417 | mutex_unlock(&dev->lock); |
| 418 | } |
| 419 | |
Steven Toth | 33cdeb3 | 2011-10-10 11:09:55 -0300 | [diff] [blame] | 420 | static int cx23885_flatiron_write(struct cx23885_dev *dev, u8 reg, u8 data) |
| 421 | { |
| 422 | /* 8 bit registers, 8 bit values */ |
| 423 | u8 buf[] = { reg, data }; |
| 424 | |
| 425 | struct i2c_msg msg = { .addr = 0x98 >> 1, |
| 426 | .flags = 0, .buf = buf, .len = 2 }; |
| 427 | |
| 428 | return i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg, 1); |
| 429 | } |
| 430 | |
| 431 | static u8 cx23885_flatiron_read(struct cx23885_dev *dev, u8 reg) |
| 432 | { |
| 433 | /* 8 bit registers, 8 bit values */ |
| 434 | int ret; |
| 435 | u8 b0[] = { reg }; |
| 436 | u8 b1[] = { 0 }; |
| 437 | |
| 438 | struct i2c_msg msg[] = { |
| 439 | { .addr = 0x98 >> 1, .flags = 0, .buf = b0, .len = 1 }, |
| 440 | { .addr = 0x98 >> 1, .flags = I2C_M_RD, .buf = b1, .len = 1 } |
| 441 | }; |
| 442 | |
| 443 | ret = i2c_transfer(&dev->i2c_bus[2].i2c_adap, &msg[0], 2); |
| 444 | if (ret != 2) |
| 445 | printk(KERN_ERR "%s() error\n", __func__); |
| 446 | |
| 447 | return b1[0]; |
| 448 | } |
| 449 | |
| 450 | static void cx23885_flatiron_dump(struct cx23885_dev *dev) |
| 451 | { |
| 452 | int i; |
| 453 | dprintk(1, "Flatiron dump\n"); |
| 454 | for (i = 0; i < 0x24; i++) { |
| 455 | dprintk(1, "FI[%02x] = %02x\n", i, |
| 456 | cx23885_flatiron_read(dev, i)); |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | static int cx23885_flatiron_mux(struct cx23885_dev *dev, int input) |
| 461 | { |
| 462 | u8 val; |
| 463 | dprintk(1, "%s(input = %d)\n", __func__, input); |
| 464 | |
| 465 | if (input == 1) |
| 466 | val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) & ~FLD_CH_SEL; |
| 467 | else if (input == 2) |
| 468 | val = cx23885_flatiron_read(dev, CH_PWR_CTRL1) | FLD_CH_SEL; |
| 469 | else |
| 470 | return -EINVAL; |
| 471 | |
| 472 | val |= 0x20; /* Enable clock to delta-sigma and dec filter */ |
| 473 | |
| 474 | cx23885_flatiron_write(dev, CH_PWR_CTRL1, val); |
| 475 | |
| 476 | /* Wake up */ |
| 477 | cx23885_flatiron_write(dev, CH_PWR_CTRL2, 0); |
| 478 | |
| 479 | if (video_debug) |
| 480 | cx23885_flatiron_dump(dev); |
| 481 | |
| 482 | return 0; |
| 483 | } |
| 484 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 485 | static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 486 | { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 487 | dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n", |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 488 | __func__, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 489 | input, INPUT(input)->vmux, |
| 490 | INPUT(input)->gpio0, INPUT(input)->gpio1, |
| 491 | INPUT(input)->gpio2, INPUT(input)->gpio3); |
| 492 | dev->input = input; |
| 493 | |
David T.L. Wong | 6f0d8c0 | 2009-10-21 13:15:30 -0300 | [diff] [blame] | 494 | if (dev->board == CX23885_BOARD_MYGICA_X8506 || |
| 495 | dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2) { |
| 496 | /* Select Analog TV */ |
| 497 | if (INPUT(input)->type == CX23885_VMUX_TELEVISION) |
| 498 | cx23885_gpio_clear(dev, GPIO_0); |
| 499 | } |
| 500 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 501 | /* Tell the internal A/V decoder */ |
Hans Verkuil | 5325b42 | 2009-04-02 11:26:22 -0300 | [diff] [blame] | 502 | v4l2_subdev_call(dev->sd_cx25840, video, s_routing, |
| 503 | INPUT(input)->vmux, 0, 0); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 504 | |
Steven Toth | 2cb9ccd | 2011-10-10 11:09:55 -0300 | [diff] [blame^] | 505 | if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) || |
| 506 | (dev->board == CX23885_BOARD_MPX885)) { |
Steven Toth | 33cdeb3 | 2011-10-10 11:09:55 -0300 | [diff] [blame] | 507 | /* Configure audio routing */ |
| 508 | v4l2_subdev_call(dev->sd_cx25840, audio, s_routing, |
| 509 | INPUT(input)->amux, 0, 0); |
| 510 | |
| 511 | if (INPUT(input)->amux == CX25840_AUDIO7) |
| 512 | cx23885_flatiron_mux(dev, 1); |
| 513 | else if (INPUT(input)->amux == CX25840_AUDIO6) |
| 514 | cx23885_flatiron_mux(dev, 2); |
| 515 | } |
| 516 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 517 | return 0; |
| 518 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 519 | |
| 520 | /* ------------------------------------------------------------------ */ |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 521 | static int cx23885_set_scale(struct cx23885_dev *dev, unsigned int width, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 522 | unsigned int height, enum v4l2_field field) |
| 523 | { |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 524 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | static int cx23885_start_video_dma(struct cx23885_dev *dev, |
| 529 | struct cx23885_dmaqueue *q, |
| 530 | struct cx23885_buffer *buf) |
| 531 | { |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 532 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 533 | |
| 534 | /* setup fifo + format */ |
| 535 | cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01], |
| 536 | buf->bpl, buf->risc.dma); |
| 537 | cx23885_set_scale(dev, buf->vb.width, buf->vb.height, buf->vb.field); |
| 538 | |
| 539 | /* reset counter */ |
| 540 | cx_write(VID_A_GPCNT_CTL, 3); |
| 541 | q->count = 1; |
| 542 | |
| 543 | /* enable irq */ |
Andy Walls | dbe83a3 | 2010-07-19 01:19:43 -0300 | [diff] [blame] | 544 | cx23885_irq_add_enable(dev, 0x01); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 545 | cx_set(VID_A_INT_MSK, 0x000011); |
| 546 | |
| 547 | /* start dma */ |
| 548 | cx_set(DEV_CNTRL2, (1<<5)); |
| 549 | cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */ |
| 550 | |
| 551 | return 0; |
| 552 | } |
| 553 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 554 | |
| 555 | static int cx23885_restart_video_queue(struct cx23885_dev *dev, |
| 556 | struct cx23885_dmaqueue *q) |
| 557 | { |
| 558 | struct cx23885_buffer *buf, *prev; |
| 559 | struct list_head *item; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 560 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 561 | |
| 562 | if (!list_empty(&q->active)) { |
| 563 | buf = list_entry(q->active.next, struct cx23885_buffer, |
| 564 | vb.queue); |
| 565 | dprintk(2, "restart_queue [%p/%d]: restart dma\n", |
| 566 | buf, buf->vb.i); |
| 567 | cx23885_start_video_dma(dev, q, buf); |
| 568 | list_for_each(item, &q->active) { |
| 569 | buf = list_entry(item, struct cx23885_buffer, |
| 570 | vb.queue); |
| 571 | buf->count = q->count++; |
| 572 | } |
| 573 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
| 574 | return 0; |
| 575 | } |
| 576 | |
| 577 | prev = NULL; |
| 578 | for (;;) { |
| 579 | if (list_empty(&q->queued)) |
| 580 | return 0; |
| 581 | buf = list_entry(q->queued.next, struct cx23885_buffer, |
| 582 | vb.queue); |
| 583 | if (NULL == prev) { |
| 584 | list_move_tail(&buf->vb.queue, &q->active); |
| 585 | cx23885_start_video_dma(dev, q, buf); |
| 586 | buf->vb.state = VIDEOBUF_ACTIVE; |
| 587 | buf->count = q->count++; |
| 588 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
| 589 | dprintk(2, "[%p/%d] restart_queue - first active\n", |
| 590 | buf, buf->vb.i); |
| 591 | |
| 592 | } else if (prev->vb.width == buf->vb.width && |
| 593 | prev->vb.height == buf->vb.height && |
| 594 | prev->fmt == buf->fmt) { |
| 595 | list_move_tail(&buf->vb.queue, &q->active); |
| 596 | buf->vb.state = VIDEOBUF_ACTIVE; |
| 597 | buf->count = q->count++; |
| 598 | prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); |
| 599 | prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */ |
| 600 | dprintk(2, "[%p/%d] restart_queue - move to active\n", |
| 601 | buf, buf->vb.i); |
| 602 | } else { |
| 603 | return 0; |
| 604 | } |
| 605 | prev = buf; |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | static int buffer_setup(struct videobuf_queue *q, unsigned int *count, |
| 610 | unsigned int *size) |
| 611 | { |
| 612 | struct cx23885_fh *fh = q->priv_data; |
| 613 | |
| 614 | *size = fh->fmt->depth*fh->width*fh->height >> 3; |
| 615 | if (0 == *count) |
| 616 | *count = 32; |
Andreas Bombe | dab7e31 | 2010-03-21 16:02:45 -0300 | [diff] [blame] | 617 | if (*size * *count > vid_limit * 1024 * 1024) |
| 618 | *count = (vid_limit * 1024 * 1024) / *size; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 619 | return 0; |
| 620 | } |
| 621 | |
| 622 | static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, |
| 623 | enum v4l2_field field) |
| 624 | { |
| 625 | struct cx23885_fh *fh = q->priv_data; |
| 626 | struct cx23885_dev *dev = fh->dev; |
| 627 | struct cx23885_buffer *buf = |
| 628 | container_of(vb, struct cx23885_buffer, vb); |
| 629 | int rc, init_buffer = 0; |
| 630 | u32 line0_offset, line1_offset; |
| 631 | struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); |
| 632 | |
| 633 | BUG_ON(NULL == fh->fmt); |
| 634 | if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) || |
| 635 | fh->height < 32 || fh->height > norm_maxh(dev->tvnorm)) |
| 636 | return -EINVAL; |
| 637 | buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3; |
| 638 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) |
| 639 | return -EINVAL; |
| 640 | |
| 641 | if (buf->fmt != fh->fmt || |
| 642 | buf->vb.width != fh->width || |
| 643 | buf->vb.height != fh->height || |
| 644 | buf->vb.field != field) { |
| 645 | buf->fmt = fh->fmt; |
| 646 | buf->vb.width = fh->width; |
| 647 | buf->vb.height = fh->height; |
| 648 | buf->vb.field = field; |
| 649 | init_buffer = 1; |
| 650 | } |
| 651 | |
| 652 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { |
| 653 | init_buffer = 1; |
| 654 | rc = videobuf_iolock(q, &buf->vb, NULL); |
| 655 | if (0 != rc) |
| 656 | goto fail; |
| 657 | } |
| 658 | |
| 659 | if (init_buffer) { |
| 660 | buf->bpl = buf->vb.width * buf->fmt->depth >> 3; |
| 661 | switch (buf->vb.field) { |
| 662 | case V4L2_FIELD_TOP: |
| 663 | cx23885_risc_buffer(dev->pci, &buf->risc, |
| 664 | dma->sglist, 0, UNSET, |
| 665 | buf->bpl, 0, buf->vb.height); |
| 666 | break; |
| 667 | case V4L2_FIELD_BOTTOM: |
| 668 | cx23885_risc_buffer(dev->pci, &buf->risc, |
| 669 | dma->sglist, UNSET, 0, |
| 670 | buf->bpl, 0, buf->vb.height); |
| 671 | break; |
| 672 | case V4L2_FIELD_INTERLACED: |
| 673 | if (dev->tvnorm & V4L2_STD_NTSC) { |
| 674 | /* cx25840 transmits NTSC bottom field first */ |
| 675 | dprintk(1, "%s() Creating NTSC risc\n", |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 676 | __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 677 | line0_offset = buf->bpl; |
| 678 | line1_offset = 0; |
| 679 | } else { |
| 680 | /* All other formats are top field first */ |
| 681 | dprintk(1, "%s() Creating PAL/SECAM risc\n", |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 682 | __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 683 | line0_offset = 0; |
| 684 | line1_offset = buf->bpl; |
| 685 | } |
| 686 | cx23885_risc_buffer(dev->pci, &buf->risc, |
| 687 | dma->sglist, line0_offset, |
| 688 | line1_offset, |
| 689 | buf->bpl, buf->bpl, |
| 690 | buf->vb.height >> 1); |
| 691 | break; |
| 692 | case V4L2_FIELD_SEQ_TB: |
| 693 | cx23885_risc_buffer(dev->pci, &buf->risc, |
| 694 | dma->sglist, |
| 695 | 0, buf->bpl * (buf->vb.height >> 1), |
| 696 | buf->bpl, 0, |
| 697 | buf->vb.height >> 1); |
| 698 | break; |
| 699 | case V4L2_FIELD_SEQ_BT: |
| 700 | cx23885_risc_buffer(dev->pci, &buf->risc, |
| 701 | dma->sglist, |
| 702 | buf->bpl * (buf->vb.height >> 1), 0, |
| 703 | buf->bpl, 0, |
| 704 | buf->vb.height >> 1); |
| 705 | break; |
| 706 | default: |
| 707 | BUG(); |
| 708 | } |
| 709 | } |
| 710 | dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n", |
| 711 | buf, buf->vb.i, |
| 712 | fh->width, fh->height, fh->fmt->depth, fh->fmt->name, |
| 713 | (unsigned long)buf->risc.dma); |
| 714 | |
| 715 | buf->vb.state = VIDEOBUF_PREPARED; |
| 716 | return 0; |
| 717 | |
| 718 | fail: |
| 719 | cx23885_free_buffer(q, buf); |
| 720 | return rc; |
| 721 | } |
| 722 | |
| 723 | static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
| 724 | { |
| 725 | struct cx23885_buffer *buf = container_of(vb, |
| 726 | struct cx23885_buffer, vb); |
| 727 | struct cx23885_buffer *prev; |
| 728 | struct cx23885_fh *fh = vq->priv_data; |
| 729 | struct cx23885_dev *dev = fh->dev; |
| 730 | struct cx23885_dmaqueue *q = &dev->vidq; |
| 731 | |
| 732 | /* add jump to stopper */ |
| 733 | buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); |
| 734 | buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma); |
| 735 | buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ |
| 736 | |
| 737 | if (!list_empty(&q->queued)) { |
| 738 | list_add_tail(&buf->vb.queue, &q->queued); |
| 739 | buf->vb.state = VIDEOBUF_QUEUED; |
| 740 | dprintk(2, "[%p/%d] buffer_queue - append to queued\n", |
| 741 | buf, buf->vb.i); |
| 742 | |
| 743 | } else if (list_empty(&q->active)) { |
| 744 | list_add_tail(&buf->vb.queue, &q->active); |
| 745 | cx23885_start_video_dma(dev, q, buf); |
| 746 | buf->vb.state = VIDEOBUF_ACTIVE; |
| 747 | buf->count = q->count++; |
| 748 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
| 749 | dprintk(2, "[%p/%d] buffer_queue - first active\n", |
| 750 | buf, buf->vb.i); |
| 751 | |
| 752 | } else { |
| 753 | prev = list_entry(q->active.prev, struct cx23885_buffer, |
| 754 | vb.queue); |
| 755 | if (prev->vb.width == buf->vb.width && |
| 756 | prev->vb.height == buf->vb.height && |
| 757 | prev->fmt == buf->fmt) { |
| 758 | list_add_tail(&buf->vb.queue, &q->active); |
| 759 | buf->vb.state = VIDEOBUF_ACTIVE; |
| 760 | buf->count = q->count++; |
| 761 | prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); |
| 762 | /* 64 bit bits 63-32 */ |
| 763 | prev->risc.jmp[2] = cpu_to_le32(0); |
| 764 | dprintk(2, "[%p/%d] buffer_queue - append to active\n", |
| 765 | buf, buf->vb.i); |
| 766 | |
| 767 | } else { |
| 768 | list_add_tail(&buf->vb.queue, &q->queued); |
| 769 | buf->vb.state = VIDEOBUF_QUEUED; |
| 770 | dprintk(2, "[%p/%d] buffer_queue - first queued\n", |
| 771 | buf, buf->vb.i); |
| 772 | } |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | static void buffer_release(struct videobuf_queue *q, |
| 777 | struct videobuf_buffer *vb) |
| 778 | { |
| 779 | struct cx23885_buffer *buf = container_of(vb, |
| 780 | struct cx23885_buffer, vb); |
| 781 | |
| 782 | cx23885_free_buffer(q, buf); |
| 783 | } |
| 784 | |
| 785 | static struct videobuf_queue_ops cx23885_video_qops = { |
| 786 | .buf_setup = buffer_setup, |
| 787 | .buf_prepare = buffer_prepare, |
| 788 | .buf_queue = buffer_queue, |
| 789 | .buf_release = buffer_release, |
| 790 | }; |
| 791 | |
| 792 | static struct videobuf_queue *get_queue(struct cx23885_fh *fh) |
| 793 | { |
| 794 | switch (fh->type) { |
| 795 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 796 | return &fh->vidq; |
| 797 | case V4L2_BUF_TYPE_VBI_CAPTURE: |
| 798 | return &fh->vbiq; |
| 799 | default: |
| 800 | BUG(); |
| 801 | return NULL; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | static int get_resource(struct cx23885_fh *fh) |
| 806 | { |
| 807 | switch (fh->type) { |
| 808 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 809 | return RESOURCE_VIDEO; |
| 810 | case V4L2_BUF_TYPE_VBI_CAPTURE: |
| 811 | return RESOURCE_VBI; |
| 812 | default: |
| 813 | BUG(); |
| 814 | return 0; |
| 815 | } |
| 816 | } |
| 817 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 818 | static int video_open(struct file *file) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 819 | { |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 820 | struct video_device *vdev = video_devdata(file); |
| 821 | struct cx23885_dev *dev = video_drvdata(file); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 822 | struct cx23885_fh *fh; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 823 | enum v4l2_buf_type type = 0; |
| 824 | int radio = 0; |
| 825 | |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 826 | switch (vdev->vfl_type) { |
| 827 | case VFL_TYPE_GRABBER: |
| 828 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
| 829 | break; |
| 830 | case VFL_TYPE_VBI: |
| 831 | type = V4L2_BUF_TYPE_VBI_CAPTURE; |
| 832 | break; |
| 833 | case VFL_TYPE_RADIO: |
| 834 | radio = 1; |
| 835 | break; |
Hans Verkuil | d56dc61 | 2008-07-30 08:43:36 -0300 | [diff] [blame] | 836 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 837 | |
Laurent Pinchart | 50462eb | 2009-12-10 11:47:13 -0200 | [diff] [blame] | 838 | dprintk(1, "open dev=%s radio=%d type=%s\n", |
| 839 | video_device_node_name(vdev), radio, v4l2_type_names[type]); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 840 | |
| 841 | /* allocate + initialize per filehandle data */ |
| 842 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 843 | if (NULL == fh) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 844 | return -ENOMEM; |
Laurent Pinchart | 63b0d5a | 2009-12-10 11:44:04 -0200 | [diff] [blame] | 845 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 846 | file->private_data = fh; |
| 847 | fh->dev = dev; |
| 848 | fh->radio = radio; |
| 849 | fh->type = type; |
| 850 | fh->width = 320; |
| 851 | fh->height = 240; |
Steven Toth | af76e9f | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 852 | fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 853 | |
Guennadi Liakhovetski | 0705135 | 2008-04-22 14:42:13 -0300 | [diff] [blame] | 854 | videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops, |
| 855 | &dev->pci->dev, &dev->slock, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 856 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
| 857 | V4L2_FIELD_INTERLACED, |
| 858 | sizeof(struct cx23885_buffer), |
Hans Verkuil | 08bff03 | 2010-09-20 17:39:46 -0300 | [diff] [blame] | 859 | fh, NULL); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 860 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 861 | videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops, |
| 862 | &dev->pci->dev, &dev->slock, |
| 863 | V4L2_BUF_TYPE_VBI_CAPTURE, |
| 864 | V4L2_FIELD_SEQ_TB, |
| 865 | sizeof(struct cx23885_buffer), |
| 866 | fh, NULL); |
| 867 | |
| 868 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 869 | dprintk(1, "post videobuf_queue_init()\n"); |
| 870 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | static ssize_t video_read(struct file *file, char __user *data, |
| 875 | size_t count, loff_t *ppos) |
| 876 | { |
| 877 | struct cx23885_fh *fh = file->private_data; |
| 878 | |
| 879 | switch (fh->type) { |
| 880 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 881 | if (res_locked(fh->dev, RESOURCE_VIDEO)) |
| 882 | return -EBUSY; |
| 883 | return videobuf_read_one(&fh->vidq, data, count, ppos, |
| 884 | file->f_flags & O_NONBLOCK); |
| 885 | case V4L2_BUF_TYPE_VBI_CAPTURE: |
| 886 | if (!res_get(fh->dev, fh, RESOURCE_VBI)) |
| 887 | return -EBUSY; |
| 888 | return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1, |
| 889 | file->f_flags & O_NONBLOCK); |
| 890 | default: |
| 891 | BUG(); |
| 892 | return 0; |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | static unsigned int video_poll(struct file *file, |
| 897 | struct poll_table_struct *wait) |
| 898 | { |
| 899 | struct cx23885_fh *fh = file->private_data; |
| 900 | struct cx23885_buffer *buf; |
Figo.zhang | 9fd6418 | 2009-06-16 13:31:29 -0300 | [diff] [blame] | 901 | unsigned int rc = POLLERR; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 902 | |
| 903 | if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { |
| 904 | if (!res_get(fh->dev, fh, RESOURCE_VBI)) |
| 905 | return POLLERR; |
| 906 | return videobuf_poll_stream(file, &fh->vbiq, wait); |
| 907 | } |
| 908 | |
Figo.zhang | 9fd6418 | 2009-06-16 13:31:29 -0300 | [diff] [blame] | 909 | mutex_lock(&fh->vidq.vb_lock); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 910 | if (res_check(fh, RESOURCE_VIDEO)) { |
| 911 | /* streaming capture */ |
| 912 | if (list_empty(&fh->vidq.stream)) |
Figo.zhang | 9fd6418 | 2009-06-16 13:31:29 -0300 | [diff] [blame] | 913 | goto done; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 914 | buf = list_entry(fh->vidq.stream.next, |
| 915 | struct cx23885_buffer, vb.stream); |
| 916 | } else { |
| 917 | /* read() capture */ |
| 918 | buf = (struct cx23885_buffer *)fh->vidq.read_buf; |
| 919 | if (NULL == buf) |
Figo.zhang | 9fd6418 | 2009-06-16 13:31:29 -0300 | [diff] [blame] | 920 | goto done; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 921 | } |
| 922 | poll_wait(file, &buf->vb.done, wait); |
| 923 | if (buf->vb.state == VIDEOBUF_DONE || |
| 924 | buf->vb.state == VIDEOBUF_ERROR) |
Figo.zhang | 9fd6418 | 2009-06-16 13:31:29 -0300 | [diff] [blame] | 925 | rc = POLLIN|POLLRDNORM; |
| 926 | else |
| 927 | rc = 0; |
| 928 | done: |
| 929 | mutex_unlock(&fh->vidq.vb_lock); |
| 930 | return rc; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 931 | } |
| 932 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 933 | static int video_release(struct file *file) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 934 | { |
| 935 | struct cx23885_fh *fh = file->private_data; |
| 936 | struct cx23885_dev *dev = fh->dev; |
| 937 | |
| 938 | /* turn off overlay */ |
| 939 | if (res_check(fh, RESOURCE_OVERLAY)) { |
| 940 | /* FIXME */ |
| 941 | res_free(dev, fh, RESOURCE_OVERLAY); |
| 942 | } |
| 943 | |
| 944 | /* stop video capture */ |
| 945 | if (res_check(fh, RESOURCE_VIDEO)) { |
| 946 | videobuf_queue_cancel(&fh->vidq); |
| 947 | res_free(dev, fh, RESOURCE_VIDEO); |
| 948 | } |
| 949 | if (fh->vidq.read_buf) { |
| 950 | buffer_release(&fh->vidq, fh->vidq.read_buf); |
| 951 | kfree(fh->vidq.read_buf); |
| 952 | } |
| 953 | |
| 954 | /* stop vbi capture */ |
| 955 | if (res_check(fh, RESOURCE_VBI)) { |
| 956 | if (fh->vbiq.streaming) |
| 957 | videobuf_streamoff(&fh->vbiq); |
| 958 | if (fh->vbiq.reading) |
| 959 | videobuf_read_stop(&fh->vbiq); |
| 960 | res_free(dev, fh, RESOURCE_VBI); |
| 961 | } |
| 962 | |
| 963 | videobuf_mmap_free(&fh->vidq); |
| 964 | file->private_data = NULL; |
| 965 | kfree(fh); |
| 966 | |
Steven Toth | a19602f2 | 2008-01-10 11:43:18 -0300 | [diff] [blame] | 967 | /* We are not putting the tuner to sleep here on exit, because |
| 968 | * we want to use the mpeg encoder in another session to capture |
| 969 | * tuner video. Closing this will result in no video to the encoder. |
| 970 | */ |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static int video_mmap(struct file *file, struct vm_area_struct *vma) |
| 976 | { |
| 977 | struct cx23885_fh *fh = file->private_data; |
| 978 | |
| 979 | return videobuf_mmap_mapper(get_queue(fh), vma); |
| 980 | } |
| 981 | |
| 982 | /* ------------------------------------------------------------------ */ |
| 983 | /* VIDEO CTRL IOCTLS */ |
| 984 | |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 985 | static int cx23885_get_control(struct cx23885_dev *dev, |
| 986 | struct v4l2_control *ctl) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 987 | { |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 988 | dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__); |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 989 | call_all(dev, core, g_ctrl, ctl); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 990 | return 0; |
| 991 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 992 | |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 993 | static int cx23885_set_control(struct cx23885_dev *dev, |
| 994 | struct v4l2_control *ctl) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 995 | { |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 996 | dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__); |
| 997 | call_all(dev, core, s_ctrl, ctl); |
| 998 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 999 | return 0; |
| 1000 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1001 | |
| 1002 | static void init_controls(struct cx23885_dev *dev) |
| 1003 | { |
| 1004 | struct v4l2_control ctrl; |
| 1005 | int i; |
| 1006 | |
| 1007 | for (i = 0; i < CX23885_CTLS; i++) { |
| 1008 | ctrl.id = cx23885_ctls[i].v.id; |
| 1009 | ctrl.value = cx23885_ctls[i].v.default_value; |
| 1010 | |
| 1011 | cx23885_set_control(dev, &ctrl); |
| 1012 | } |
| 1013 | } |
| 1014 | |
| 1015 | /* ------------------------------------------------------------------ */ |
| 1016 | /* VIDEO IOCTLS */ |
| 1017 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1018 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1019 | struct v4l2_format *f) |
| 1020 | { |
| 1021 | struct cx23885_fh *fh = priv; |
| 1022 | |
| 1023 | f->fmt.pix.width = fh->width; |
| 1024 | f->fmt.pix.height = fh->height; |
| 1025 | f->fmt.pix.field = fh->vidq.field; |
| 1026 | f->fmt.pix.pixelformat = fh->fmt->fourcc; |
| 1027 | f->fmt.pix.bytesperline = |
| 1028 | (f->fmt.pix.width * fh->fmt->depth) >> 3; |
| 1029 | f->fmt.pix.sizeimage = |
| 1030 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1035 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1036 | struct v4l2_format *f) |
| 1037 | { |
| 1038 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1039 | struct cx23885_fmt *fmt; |
| 1040 | enum v4l2_field field; |
| 1041 | unsigned int maxw, maxh; |
| 1042 | |
| 1043 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 1044 | if (NULL == fmt) |
| 1045 | return -EINVAL; |
| 1046 | |
| 1047 | field = f->fmt.pix.field; |
| 1048 | maxw = norm_maxw(dev->tvnorm); |
| 1049 | maxh = norm_maxh(dev->tvnorm); |
| 1050 | |
| 1051 | if (V4L2_FIELD_ANY == field) { |
| 1052 | field = (f->fmt.pix.height > maxh/2) |
| 1053 | ? V4L2_FIELD_INTERLACED |
| 1054 | : V4L2_FIELD_BOTTOM; |
| 1055 | } |
| 1056 | |
| 1057 | switch (field) { |
| 1058 | case V4L2_FIELD_TOP: |
| 1059 | case V4L2_FIELD_BOTTOM: |
| 1060 | maxh = maxh / 2; |
| 1061 | break; |
| 1062 | case V4L2_FIELD_INTERLACED: |
| 1063 | break; |
| 1064 | default: |
| 1065 | return -EINVAL; |
| 1066 | } |
| 1067 | |
| 1068 | f->fmt.pix.field = field; |
Trent Piepho | 2449afc | 2009-05-30 21:45:46 -0300 | [diff] [blame] | 1069 | v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2, |
| 1070 | &f->fmt.pix.height, 32, maxh, 0, 0); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1071 | f->fmt.pix.bytesperline = |
| 1072 | (f->fmt.pix.width * fmt->depth) >> 3; |
| 1073 | f->fmt.pix.sizeimage = |
| 1074 | f->fmt.pix.height * f->fmt.pix.bytesperline; |
| 1075 | |
| 1076 | return 0; |
| 1077 | } |
| 1078 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1079 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1080 | struct v4l2_format *f) |
| 1081 | { |
| 1082 | struct cx23885_fh *fh = priv; |
| 1083 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
Hans Verkuil | cc99113 | 2010-05-09 10:09:28 -0300 | [diff] [blame] | 1084 | struct v4l2_mbus_framefmt mbus_fmt; |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1085 | int err; |
| 1086 | |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1087 | dprintk(2, "%s()\n", __func__); |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1088 | err = vidioc_try_fmt_vid_cap(file, priv, f); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1089 | |
| 1090 | if (0 != err) |
| 1091 | return err; |
| 1092 | fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat); |
| 1093 | fh->width = f->fmt.pix.width; |
| 1094 | fh->height = f->fmt.pix.height; |
| 1095 | fh->vidq.field = f->fmt.pix.field; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1096 | dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1097 | fh->width, fh->height, fh->vidq.field); |
Hans Verkuil | cc99113 | 2010-05-09 10:09:28 -0300 | [diff] [blame] | 1098 | v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED); |
| 1099 | call_all(dev, video, s_mbus_fmt, &mbus_fmt); |
| 1100 | v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1101 | return 0; |
| 1102 | } |
| 1103 | |
| 1104 | static int vidioc_querycap(struct file *file, void *priv, |
| 1105 | struct v4l2_capability *cap) |
| 1106 | { |
| 1107 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1108 | |
| 1109 | strcpy(cap->driver, "cx23885"); |
| 1110 | strlcpy(cap->card, cx23885_boards[dev->board].name, |
| 1111 | sizeof(cap->card)); |
| 1112 | sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci)); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1113 | cap->capabilities = |
| 1114 | V4L2_CAP_VIDEO_CAPTURE | |
| 1115 | V4L2_CAP_READWRITE | |
| 1116 | V4L2_CAP_STREAMING | |
| 1117 | V4L2_CAP_VBI_CAPTURE; |
| 1118 | if (UNSET != dev->tuner_type) |
| 1119 | cap->capabilities |= V4L2_CAP_TUNER; |
| 1120 | return 0; |
| 1121 | } |
| 1122 | |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1123 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1124 | struct v4l2_fmtdesc *f) |
| 1125 | { |
| 1126 | if (unlikely(f->index >= ARRAY_SIZE(formats))) |
| 1127 | return -EINVAL; |
| 1128 | |
| 1129 | strlcpy(f->description, formats[f->index].name, |
| 1130 | sizeof(f->description)); |
| 1131 | f->pixelformat = formats[f->index].fourcc; |
| 1132 | |
| 1133 | return 0; |
| 1134 | } |
| 1135 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1136 | static int vidioc_reqbufs(struct file *file, void *priv, |
| 1137 | struct v4l2_requestbuffers *p) |
| 1138 | { |
| 1139 | struct cx23885_fh *fh = priv; |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 1140 | return videobuf_reqbufs(get_queue(fh), p); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | static int vidioc_querybuf(struct file *file, void *priv, |
| 1144 | struct v4l2_buffer *p) |
| 1145 | { |
| 1146 | struct cx23885_fh *fh = priv; |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 1147 | return videobuf_querybuf(get_queue(fh), p); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1148 | } |
| 1149 | |
| 1150 | static int vidioc_qbuf(struct file *file, void *priv, |
| 1151 | struct v4l2_buffer *p) |
| 1152 | { |
| 1153 | struct cx23885_fh *fh = priv; |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 1154 | return videobuf_qbuf(get_queue(fh), p); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1155 | } |
| 1156 | |
| 1157 | static int vidioc_dqbuf(struct file *file, void *priv, |
| 1158 | struct v4l2_buffer *p) |
| 1159 | { |
| 1160 | struct cx23885_fh *fh = priv; |
Steven Toth | 9c8ced5 | 2008-10-16 20:18:44 -0300 | [diff] [blame] | 1161 | return videobuf_dqbuf(get_queue(fh), p, |
| 1162 | file->f_flags & O_NONBLOCK); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1163 | } |
| 1164 | |
| 1165 | static int vidioc_streamon(struct file *file, void *priv, |
| 1166 | enum v4l2_buf_type i) |
| 1167 | { |
| 1168 | struct cx23885_fh *fh = priv; |
| 1169 | struct cx23885_dev *dev = fh->dev; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1170 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1171 | |
Steven Toth | 5ab27e6 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1172 | if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
| 1173 | (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1174 | return -EINVAL; |
| 1175 | if (unlikely(i != fh->type)) |
| 1176 | return -EINVAL; |
| 1177 | |
| 1178 | if (unlikely(!res_get(dev, fh, get_resource(fh)))) |
| 1179 | return -EBUSY; |
Steven Toth | 24465b4 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1180 | |
| 1181 | /* Don't start VBI streaming unless vida streaming |
| 1182 | * has already started. |
| 1183 | */ |
| 1184 | if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) && |
| 1185 | ((cx_read(VID_A_DMA_CTL) & 0x11) == 0)) |
| 1186 | return -EINVAL; |
| 1187 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1188 | return videobuf_streamon(get_queue(fh)); |
| 1189 | } |
| 1190 | |
| 1191 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) |
| 1192 | { |
| 1193 | struct cx23885_fh *fh = priv; |
| 1194 | struct cx23885_dev *dev = fh->dev; |
| 1195 | int err, res; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1196 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1197 | |
Steven Toth | 5ab27e6 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1198 | if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
| 1199 | (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1200 | return -EINVAL; |
| 1201 | if (i != fh->type) |
| 1202 | return -EINVAL; |
| 1203 | |
| 1204 | res = get_resource(fh); |
| 1205 | err = videobuf_streamoff(get_queue(fh)); |
| 1206 | if (err < 0) |
| 1207 | return err; |
| 1208 | res_free(dev, fh, res); |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms) |
| 1213 | { |
| 1214 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1215 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1216 | |
| 1217 | mutex_lock(&dev->lock); |
| 1218 | cx23885_set_tvnorm(dev, *tvnorms); |
| 1219 | mutex_unlock(&dev->lock); |
| 1220 | |
| 1221 | return 0; |
| 1222 | } |
| 1223 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 1224 | static int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1225 | { |
| 1226 | static const char *iname[] = { |
| 1227 | [CX23885_VMUX_COMPOSITE1] = "Composite1", |
| 1228 | [CX23885_VMUX_COMPOSITE2] = "Composite2", |
| 1229 | [CX23885_VMUX_COMPOSITE3] = "Composite3", |
| 1230 | [CX23885_VMUX_COMPOSITE4] = "Composite4", |
| 1231 | [CX23885_VMUX_SVIDEO] = "S-Video", |
David T.L. Wong | dac65fa | 2009-10-21 11:07:24 -0300 | [diff] [blame] | 1232 | [CX23885_VMUX_COMPONENT] = "Component", |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1233 | [CX23885_VMUX_TELEVISION] = "Television", |
| 1234 | [CX23885_VMUX_CABLE] = "Cable TV", |
| 1235 | [CX23885_VMUX_DVB] = "DVB", |
| 1236 | [CX23885_VMUX_DEBUG] = "for debug only", |
| 1237 | }; |
| 1238 | unsigned int n; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1239 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1240 | |
| 1241 | n = i->index; |
| 1242 | if (n >= 4) |
| 1243 | return -EINVAL; |
| 1244 | |
| 1245 | if (0 == INPUT(n)->type) |
| 1246 | return -EINVAL; |
| 1247 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1248 | i->index = n; |
| 1249 | i->type = V4L2_INPUT_TYPE_CAMERA; |
| 1250 | strcpy(i->name, iname[INPUT(n)->type]); |
| 1251 | if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) || |
Julia Lawall | 473d802 | 2010-08-05 17:22:44 -0300 | [diff] [blame] | 1252 | (CX23885_VMUX_CABLE == INPUT(n)->type)) { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1253 | i->type = V4L2_INPUT_TYPE_TUNER; |
| 1254 | i->std = CX23885_NORMS; |
Julia Lawall | 473d802 | 2010-08-05 17:22:44 -0300 | [diff] [blame] | 1255 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1258 | |
| 1259 | static int vidioc_enum_input(struct file *file, void *priv, |
| 1260 | struct v4l2_input *i) |
| 1261 | { |
| 1262 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1263 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1264 | return cx23885_enum_input(dev, i); |
| 1265 | } |
| 1266 | |
| 1267 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
| 1268 | { |
| 1269 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1270 | |
| 1271 | *i = dev->input; |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1272 | dprintk(1, "%s() returns %d\n", __func__, *i); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1273 | return 0; |
| 1274 | } |
| 1275 | |
| 1276 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
| 1277 | { |
| 1278 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1279 | |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1280 | dprintk(1, "%s(%d)\n", __func__, i); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1281 | |
| 1282 | if (i >= 4) { |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1283 | dprintk(1, "%s() -EINVAL\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1284 | return -EINVAL; |
| 1285 | } |
| 1286 | |
| 1287 | mutex_lock(&dev->lock); |
| 1288 | cx23885_video_mux(dev, i); |
| 1289 | mutex_unlock(&dev->lock); |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
Andy Walls | e9e5cf4 | 2010-07-18 18:18:06 -0300 | [diff] [blame] | 1293 | static int vidioc_log_status(struct file *file, void *priv) |
| 1294 | { |
| 1295 | struct cx23885_fh *fh = priv; |
| 1296 | struct cx23885_dev *dev = fh->dev; |
| 1297 | |
| 1298 | printk(KERN_INFO |
| 1299 | "%s/0: ============ START LOG STATUS ============\n", |
| 1300 | dev->name); |
| 1301 | call_all(dev, core, log_status); |
| 1302 | printk(KERN_INFO |
| 1303 | "%s/0: ============= END LOG STATUS =============\n", |
| 1304 | dev->name); |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1308 | static int vidioc_queryctrl(struct file *file, void *priv, |
| 1309 | struct v4l2_queryctrl *qctrl) |
| 1310 | { |
| 1311 | qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id); |
| 1312 | if (unlikely(qctrl->id == 0)) |
| 1313 | return -EINVAL; |
| 1314 | return cx23885_ctrl_query(qctrl); |
| 1315 | } |
| 1316 | |
| 1317 | static int vidioc_g_ctrl(struct file *file, void *priv, |
| 1318 | struct v4l2_control *ctl) |
| 1319 | { |
| 1320 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1321 | |
| 1322 | return cx23885_get_control(dev, ctl); |
| 1323 | } |
| 1324 | |
| 1325 | static int vidioc_s_ctrl(struct file *file, void *priv, |
| 1326 | struct v4l2_control *ctl) |
| 1327 | { |
| 1328 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1329 | |
| 1330 | return cx23885_set_control(dev, ctl); |
| 1331 | } |
| 1332 | |
| 1333 | static int vidioc_g_tuner(struct file *file, void *priv, |
| 1334 | struct v4l2_tuner *t) |
| 1335 | { |
| 1336 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1337 | |
| 1338 | if (unlikely(UNSET == dev->tuner_type)) |
| 1339 | return -EINVAL; |
| 1340 | if (0 != t->index) |
| 1341 | return -EINVAL; |
| 1342 | |
| 1343 | strcpy(t->name, "Television"); |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1344 | |
Steven Toth | 80f1e08 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1345 | call_all(dev, tuner, g_tuner, t); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1346 | return 0; |
| 1347 | } |
| 1348 | |
| 1349 | static int vidioc_s_tuner(struct file *file, void *priv, |
| 1350 | struct v4l2_tuner *t) |
| 1351 | { |
| 1352 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; |
| 1353 | |
| 1354 | if (UNSET == dev->tuner_type) |
| 1355 | return -EINVAL; |
| 1356 | if (0 != t->index) |
| 1357 | return -EINVAL; |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1358 | /* Update the A/V core */ |
Steven Toth | 80f1e08 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1359 | call_all(dev, tuner, s_tuner, t); |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1360 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1361 | return 0; |
| 1362 | } |
| 1363 | |
| 1364 | static int vidioc_g_frequency(struct file *file, void *priv, |
| 1365 | struct v4l2_frequency *f) |
| 1366 | { |
| 1367 | struct cx23885_fh *fh = priv; |
| 1368 | struct cx23885_dev *dev = fh->dev; |
| 1369 | |
| 1370 | if (unlikely(UNSET == dev->tuner_type)) |
| 1371 | return -EINVAL; |
| 1372 | |
| 1373 | /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */ |
| 1374 | f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; |
| 1375 | f->frequency = dev->freq; |
| 1376 | |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1377 | call_all(dev, tuner, g_frequency, f); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1378 | |
| 1379 | return 0; |
| 1380 | } |
| 1381 | |
Hans Verkuil | d45b9b8 | 2008-09-04 03:33:43 -0300 | [diff] [blame] | 1382 | static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1383 | { |
| 1384 | if (unlikely(UNSET == dev->tuner_type)) |
| 1385 | return -EINVAL; |
| 1386 | if (unlikely(f->tuner != 0)) |
| 1387 | return -EINVAL; |
| 1388 | |
| 1389 | mutex_lock(&dev->lock); |
| 1390 | dev->freq = f->frequency; |
| 1391 | |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1392 | call_all(dev, tuner, s_frequency, f); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1393 | |
| 1394 | /* When changing channels it is required to reset TVAUDIO */ |
| 1395 | msleep(10); |
| 1396 | |
| 1397 | mutex_unlock(&dev->lock); |
| 1398 | |
| 1399 | return 0; |
| 1400 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1401 | |
| 1402 | static int vidioc_s_frequency(struct file *file, void *priv, |
| 1403 | struct v4l2_frequency *f) |
| 1404 | { |
| 1405 | struct cx23885_fh *fh = priv; |
| 1406 | struct cx23885_dev *dev = fh->dev; |
| 1407 | |
| 1408 | if (unlikely(0 == fh->radio && f->type != V4L2_TUNER_ANALOG_TV)) |
| 1409 | return -EINVAL; |
| 1410 | if (unlikely(1 == fh->radio && f->type != V4L2_TUNER_RADIO)) |
| 1411 | return -EINVAL; |
| 1412 | |
| 1413 | return |
| 1414 | cx23885_set_freq(dev, f); |
| 1415 | } |
| 1416 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1417 | /* ----------------------------------------------------------- */ |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1418 | |
| 1419 | static void cx23885_vid_timeout(unsigned long data) |
| 1420 | { |
| 1421 | struct cx23885_dev *dev = (struct cx23885_dev *)data; |
| 1422 | struct cx23885_dmaqueue *q = &dev->vidq; |
| 1423 | struct cx23885_buffer *buf; |
| 1424 | unsigned long flags; |
| 1425 | |
| 1426 | cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]); |
| 1427 | |
| 1428 | cx_clear(VID_A_DMA_CTL, 0x11); |
| 1429 | |
| 1430 | spin_lock_irqsave(&dev->slock, flags); |
| 1431 | while (!list_empty(&q->active)) { |
| 1432 | buf = list_entry(q->active.next, |
| 1433 | struct cx23885_buffer, vb.queue); |
| 1434 | list_del(&buf->vb.queue); |
| 1435 | buf->vb.state = VIDEOBUF_ERROR; |
| 1436 | wake_up(&buf->vb.done); |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1437 | printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n", |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1438 | dev->name, buf, buf->vb.i, |
| 1439 | (unsigned long)buf->risc.dma); |
| 1440 | } |
| 1441 | cx23885_restart_video_queue(dev, q); |
| 1442 | spin_unlock_irqrestore(&dev->slock, flags); |
| 1443 | } |
| 1444 | |
| 1445 | int cx23885_video_irq(struct cx23885_dev *dev, u32 status) |
| 1446 | { |
| 1447 | u32 mask, count; |
| 1448 | int handled = 0; |
| 1449 | |
| 1450 | mask = cx_read(VID_A_INT_MSK); |
| 1451 | if (0 == (status & mask)) |
| 1452 | return handled; |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1453 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1454 | cx_write(VID_A_INT_STAT, status); |
| 1455 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1456 | /* risc op code error */ |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1457 | if ((status & VID_BC_MSK_OPC_ERR) || |
| 1458 | (status & VID_BC_MSK_SYNC) || |
| 1459 | (status & VID_BC_MSK_OF)) { |
| 1460 | |
| 1461 | if (status & VID_BC_MSK_OPC_ERR) |
| 1462 | dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n", |
| 1463 | VID_BC_MSK_OPC_ERR); |
| 1464 | |
| 1465 | if (status & VID_BC_MSK_SYNC) |
| 1466 | dprintk(7, " (VID_BC_MSK_SYNC 0x%08x)\n", |
| 1467 | VID_BC_MSK_SYNC); |
| 1468 | |
| 1469 | if (status & VID_BC_MSK_OF) |
| 1470 | dprintk(7, " (VID_BC_MSK_OF 0x%08x)\n", |
| 1471 | VID_BC_MSK_OF); |
| 1472 | |
| 1473 | printk(KERN_WARNING "%s: video risc op code error\n", |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1474 | dev->name); |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1475 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1476 | cx_clear(VID_A_DMA_CTL, 0x11); |
| 1477 | cx23885_sram_channel_dump(dev, &dev->sram_channels[SRAM_CH01]); |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1478 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1479 | } |
| 1480 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1481 | /* Video */ |
| 1482 | if (status & VID_BC_MSK_RISCI1) { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1483 | spin_lock(&dev->slock); |
| 1484 | count = cx_read(VID_A_GPCNT); |
| 1485 | cx23885_video_wakeup(dev, &dev->vidq, count); |
| 1486 | spin_unlock(&dev->slock); |
| 1487 | handled++; |
| 1488 | } |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1489 | if (status & VID_BC_MSK_RISCI2) { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1490 | dprintk(2, "stopper video\n"); |
| 1491 | spin_lock(&dev->slock); |
| 1492 | cx23885_restart_video_queue(dev, &dev->vidq); |
| 1493 | spin_unlock(&dev->slock); |
| 1494 | handled++; |
| 1495 | } |
| 1496 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1497 | /* Allow the VBI framework to process it's payload */ |
| 1498 | handled += cx23885_vbi_irq(dev, status); |
| 1499 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1500 | return handled; |
| 1501 | } |
| 1502 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1503 | /* ----------------------------------------------------------- */ |
| 1504 | /* exported stuff */ |
| 1505 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1506 | static const struct v4l2_file_operations video_fops = { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1507 | .owner = THIS_MODULE, |
| 1508 | .open = video_open, |
| 1509 | .release = video_release, |
| 1510 | .read = video_read, |
| 1511 | .poll = video_poll, |
| 1512 | .mmap = video_mmap, |
| 1513 | .ioctl = video_ioctl2, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1514 | }; |
| 1515 | |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1516 | static const struct v4l2_ioctl_ops video_ioctl_ops = { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1517 | .vidioc_querycap = vidioc_querycap, |
Hans Verkuil | 78b526a | 2008-05-28 12:16:41 -0300 | [diff] [blame] | 1518 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
| 1519 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, |
| 1520 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, |
| 1521 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
| 1522 | .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt, |
| 1523 | .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt, |
| 1524 | .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1525 | .vidioc_reqbufs = vidioc_reqbufs, |
| 1526 | .vidioc_querybuf = vidioc_querybuf, |
| 1527 | .vidioc_qbuf = vidioc_qbuf, |
| 1528 | .vidioc_dqbuf = vidioc_dqbuf, |
| 1529 | .vidioc_s_std = vidioc_s_std, |
| 1530 | .vidioc_enum_input = vidioc_enum_input, |
| 1531 | .vidioc_g_input = vidioc_g_input, |
| 1532 | .vidioc_s_input = vidioc_s_input, |
Andy Walls | e9e5cf4 | 2010-07-18 18:18:06 -0300 | [diff] [blame] | 1533 | .vidioc_log_status = vidioc_log_status, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1534 | .vidioc_queryctrl = vidioc_queryctrl, |
| 1535 | .vidioc_g_ctrl = vidioc_g_ctrl, |
| 1536 | .vidioc_s_ctrl = vidioc_s_ctrl, |
| 1537 | .vidioc_streamon = vidioc_streamon, |
| 1538 | .vidioc_streamoff = vidioc_streamoff, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1539 | .vidioc_g_tuner = vidioc_g_tuner, |
| 1540 | .vidioc_s_tuner = vidioc_s_tuner, |
| 1541 | .vidioc_g_frequency = vidioc_g_frequency, |
| 1542 | .vidioc_s_frequency = vidioc_s_frequency, |
Andy Walls | 7461824 | 2009-09-26 22:50:44 -0300 | [diff] [blame] | 1543 | .vidioc_g_chip_ident = cx23885_g_chip_ident, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1544 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
Andy Walls | 7461824 | 2009-09-26 22:50:44 -0300 | [diff] [blame] | 1545 | .vidioc_g_register = cx23885_g_register, |
| 1546 | .vidioc_s_register = cx23885_s_register, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1547 | #endif |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1548 | }; |
| 1549 | |
| 1550 | static struct video_device cx23885_vbi_template; |
| 1551 | static struct video_device cx23885_video_template = { |
| 1552 | .name = "cx23885-video", |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1553 | .fops = &video_fops, |
Hans Verkuil | a399810 | 2008-07-21 02:57:38 -0300 | [diff] [blame] | 1554 | .ioctl_ops = &video_ioctl_ops, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1555 | .tvnorms = CX23885_NORMS, |
| 1556 | .current_norm = V4L2_STD_NTSC_M, |
| 1557 | }; |
| 1558 | |
Hans Verkuil | bec4366 | 2008-12-30 06:58:20 -0300 | [diff] [blame] | 1559 | static const struct v4l2_file_operations radio_fops = { |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1560 | .owner = THIS_MODULE, |
| 1561 | .open = video_open, |
| 1562 | .release = video_release, |
| 1563 | .ioctl = video_ioctl2, |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1564 | }; |
| 1565 | |
| 1566 | |
| 1567 | void cx23885_video_unregister(struct cx23885_dev *dev) |
| 1568 | { |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1569 | dprintk(1, "%s()\n", __func__); |
Andy Walls | dbe83a3 | 2010-07-19 01:19:43 -0300 | [diff] [blame] | 1570 | cx23885_irq_remove(dev, 0x01); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1571 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1572 | if (dev->vbi_dev) { |
| 1573 | if (video_is_registered(dev->vbi_dev)) |
| 1574 | video_unregister_device(dev->vbi_dev); |
| 1575 | else |
| 1576 | video_device_release(dev->vbi_dev); |
| 1577 | dev->vbi_dev = NULL; |
| 1578 | btcx_riscmem_free(dev->pci, &dev->vbiq.stopper); |
| 1579 | } |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1580 | if (dev->video_dev) { |
Laurent Pinchart | f0813b4 | 2009-11-27 13:57:30 -0300 | [diff] [blame] | 1581 | if (video_is_registered(dev->video_dev)) |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1582 | video_unregister_device(dev->video_dev); |
| 1583 | else |
| 1584 | video_device_release(dev->video_dev); |
| 1585 | dev->video_dev = NULL; |
| 1586 | |
| 1587 | btcx_riscmem_free(dev->pci, &dev->vidq.stopper); |
| 1588 | } |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1589 | |
| 1590 | if (dev->audio_dev) |
Steven Toth | efa762f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1591 | cx23885_audio_unregister(dev); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | int cx23885_video_register(struct cx23885_dev *dev) |
| 1595 | { |
| 1596 | int err; |
| 1597 | |
Harvey Harrison | 22b4e64 | 2008-04-08 23:20:00 -0300 | [diff] [blame] | 1598 | dprintk(1, "%s()\n", __func__); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1599 | spin_lock_init(&dev->slock); |
| 1600 | |
| 1601 | /* Initialize VBI template */ |
| 1602 | memcpy(&cx23885_vbi_template, &cx23885_video_template, |
| 1603 | sizeof(cx23885_vbi_template)); |
| 1604 | strcpy(cx23885_vbi_template.name, "cx23885-vbi"); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1605 | |
| 1606 | dev->tvnorm = cx23885_video_template.current_norm; |
| 1607 | |
| 1608 | /* init video dma queues */ |
| 1609 | INIT_LIST_HEAD(&dev->vidq.active); |
| 1610 | INIT_LIST_HEAD(&dev->vidq.queued); |
| 1611 | dev->vidq.timeout.function = cx23885_vid_timeout; |
| 1612 | dev->vidq.timeout.data = (unsigned long)dev; |
| 1613 | init_timer(&dev->vidq.timeout); |
| 1614 | cx23885_risc_stopper(dev->pci, &dev->vidq.stopper, |
| 1615 | VID_A_DMA_CTL, 0x11, 0x00); |
| 1616 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1617 | /* init vbi dma queues */ |
| 1618 | INIT_LIST_HEAD(&dev->vbiq.active); |
| 1619 | INIT_LIST_HEAD(&dev->vbiq.queued); |
| 1620 | dev->vbiq.timeout.function = cx23885_vbi_timeout; |
| 1621 | dev->vbiq.timeout.data = (unsigned long)dev; |
| 1622 | init_timer(&dev->vbiq.timeout); |
| 1623 | cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper, |
| 1624 | VID_A_DMA_CTL, 0x22, 0x00); |
Andy Walls | dbe83a3 | 2010-07-19 01:19:43 -0300 | [diff] [blame] | 1625 | |
| 1626 | cx23885_irq_add_enable(dev, 0x01); |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1627 | |
Igor M. Liplianin | 557f48d | 2011-01-25 17:05:00 -0300 | [diff] [blame] | 1628 | if ((TUNER_ABSENT != dev->tuner_type) && |
| 1629 | ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) { |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1630 | struct v4l2_subdev *sd = NULL; |
| 1631 | |
| 1632 | if (dev->tuner_addr) |
Hans Verkuil | e6574f2 | 2009-04-01 03:57:53 -0300 | [diff] [blame] | 1633 | sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, |
Igor M. Liplianin | 557f48d | 2011-01-25 17:05:00 -0300 | [diff] [blame] | 1634 | &dev->i2c_bus[dev->tuner_bus].i2c_adap, |
Laurent Pinchart | 9a1f8b3 | 2010-09-24 10:16:44 -0300 | [diff] [blame] | 1635 | "tuner", dev->tuner_addr, NULL); |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1636 | else |
Hans Verkuil | 53dacb1 | 2009-08-10 02:49:08 -0300 | [diff] [blame] | 1637 | sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, |
Igor M. Liplianin | 557f48d | 2011-01-25 17:05:00 -0300 | [diff] [blame] | 1638 | &dev->i2c_bus[dev->tuner_bus].i2c_adap, |
Laurent Pinchart | 1532a07 | 2010-09-24 07:58:29 -0300 | [diff] [blame] | 1639 | "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV)); |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1640 | if (sd) { |
| 1641 | struct tuner_setup tun_setup; |
| 1642 | |
David T.L. Wong | fd705e7 | 2009-10-21 11:08:30 -0300 | [diff] [blame] | 1643 | memset(&tun_setup, 0, sizeof(tun_setup)); |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1644 | tun_setup.mode_mask = T_ANALOG_TV; |
| 1645 | tun_setup.type = dev->tuner_type; |
| 1646 | tun_setup.addr = v4l2_i2c_subdev_addr(sd); |
David T.L. Wong | 6f0d8c0 | 2009-10-21 13:15:30 -0300 | [diff] [blame] | 1647 | tun_setup.tuner_callback = cx23885_tuner_callback; |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1648 | |
| 1649 | v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup); |
Kusanagi Kouichi | 0b32d65 | 2010-01-22 04:55:28 -0300 | [diff] [blame] | 1650 | |
| 1651 | if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) { |
| 1652 | struct xc2028_ctrl ctrl = { |
| 1653 | .fname = XC2028_DEFAULT_FIRMWARE, |
| 1654 | .max_len = 64 |
| 1655 | }; |
| 1656 | struct v4l2_priv_tun_config cfg = { |
| 1657 | .tuner = dev->tuner_type, |
| 1658 | .priv = &ctrl |
| 1659 | }; |
| 1660 | v4l2_subdev_call(sd, tuner, s_config, &cfg); |
| 1661 | } |
Hans Verkuil | 0d5a19f | 2009-03-29 06:53:29 -0300 | [diff] [blame] | 1662 | } |
| 1663 | } |
| 1664 | |
Steven Toth | 79776c8 | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1665 | /* register Video device */ |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1666 | dev->video_dev = cx23885_vdev_init(dev, dev->pci, |
| 1667 | &cx23885_video_template, "video"); |
| 1668 | err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, |
| 1669 | video_nr[dev->nr]); |
| 1670 | if (err < 0) { |
| 1671 | printk(KERN_INFO "%s: can't register video device\n", |
| 1672 | dev->name); |
| 1673 | goto fail_unreg; |
| 1674 | } |
Steven Toth | f88fb8e | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1675 | printk(KERN_INFO "%s: registered device %s [v4l2]\n", |
Laurent Pinchart | 38c7c03 | 2009-11-27 13:57:15 -0300 | [diff] [blame] | 1676 | dev->name, video_device_node_name(dev->video_dev)); |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1677 | |
Steven Toth | f88fb8e | 2011-10-10 11:09:54 -0300 | [diff] [blame] | 1678 | /* register VBI device */ |
| 1679 | dev->vbi_dev = cx23885_vdev_init(dev, dev->pci, |
| 1680 | &cx23885_vbi_template, "vbi"); |
| 1681 | err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, |
| 1682 | vbi_nr[dev->nr]); |
| 1683 | if (err < 0) { |
| 1684 | printk(KERN_INFO "%s: can't register vbi device\n", |
| 1685 | dev->name); |
| 1686 | goto fail_unreg; |
| 1687 | } |
| 1688 | printk(KERN_INFO "%s: registered device %s\n", |
| 1689 | dev->name, video_device_node_name(dev->vbi_dev)); |
| 1690 | |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1691 | /* Register ALSA audio device */ |
Steven Toth | efa762f | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1692 | dev->audio_dev = cx23885_audio_register(dev); |
Mijhail Moreyra | 97ce567 | 2011-10-10 11:09:53 -0300 | [diff] [blame] | 1693 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1694 | /* initial device configuration */ |
| 1695 | mutex_lock(&dev->lock); |
| 1696 | cx23885_set_tvnorm(dev, dev->tvnorm); |
| 1697 | init_controls(dev); |
| 1698 | cx23885_video_mux(dev, 0); |
| 1699 | mutex_unlock(&dev->lock); |
| 1700 | |
Steven Toth | e47f30b | 2008-01-10 04:25:59 -0300 | [diff] [blame] | 1701 | return 0; |
| 1702 | |
| 1703 | fail_unreg: |
| 1704 | cx23885_video_unregister(dev); |
| 1705 | return err; |
| 1706 | } |
| 1707 | |