Commit | Line | Data |
---|---|---|
e47f30b1 ST |
1 | /* |
2 | * Driver for the Conexant CX23885 PCIe bridge | |
3 | * | |
6d897616 | 4 | * Copyright (c) 2007 Steven Toth <stoth@linuxtv.org> |
e47f30b1 ST |
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> | |
35ea11ff | 36 | #include <media/v4l2-ioctl.h> |
74618244 | 37 | #include "cx23885-ioctl.h" |
0b32d65c | 38 | #include "tuner-xc2028.h" |
e47f30b1 | 39 | |
97ce5670 MM |
40 | #include <media/cx25840.h> |
41 | ||
e47f30b1 | 42 | MODULE_DESCRIPTION("v4l2 driver module for cx23885 based TV cards"); |
6d897616 | 43 | MODULE_AUTHOR("Steven Toth <stoth@linuxtv.org>"); |
e47f30b1 ST |
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 | ||
4513fc69 | 60 | static unsigned int video_debug; |
e47f30b1 ST |
61 | module_param(video_debug, int, 0644); |
62 | MODULE_PARM_DESC(video_debug, "enable debug messages [video]"); | |
63 | ||
4513fc69 | 64 | static unsigned int irq_debug; |
e47f30b1 ST |
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...)\ | |
4513fc69 | 73 | do { if (video_debug >= level)\ |
79776c89 | 74 | printk(KERN_DEBUG "%s: " fmt, dev->name, ## arg);\ |
4513fc69 | 75 | } while (0) |
e47f30b1 ST |
76 | |
77 | /* ------------------------------------------------------------------- */ | |
78 | /* static data */ | |
79 | ||
80 | #define FORMAT_FLAGS_PACKED 0x01 | |
af76e9f6 | 81 | #if 0 |
e47f30b1 ST |
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 | }; | |
af76e9f6 ST |
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 | |
e47f30b1 ST |
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 | ||
af76e9f6 ST |
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 | ); | |
e47f30b1 ST |
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, | |
b12ab3f8 | 198 | .maximum = 0x7f, |
e47f30b1 ST |
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", | |
b12ab3f8 MM |
211 | .minimum = -127, |
212 | .maximum = 128, | |
e47f30b1 | 213 | .step = 1, |
b12ab3f8 | 214 | .default_value = 0x0, |
e47f30b1 ST |
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, | |
b12ab3f8 | 229 | .maximum = 0x7f, |
e47f30b1 | 230 | .step = 1, |
b12ab3f8 | 231 | .default_value = 0x3f, |
e47f30b1 ST |
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, | |
4c3764d1 ST |
256 | .maximum = 65535, |
257 | .step = 65535 / 100, | |
258 | .default_value = 65535, | |
e47f30b1 ST |
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 | ||
2ba58894 | 268 | /* Must be sorted from low to high control ID! */ |
d45b9b8a | 269 | static const u32 cx23885_user_ctrls[] = { |
e47f30b1 ST |
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 | }; | |
e47f30b1 ST |
279 | |
280 | static const u32 *ctrl_classes[] = { | |
281 | cx23885_user_ctrls, | |
282 | NULL | |
283 | }; | |
284 | ||
79776c89 ST |
285 | void cx23885_video_wakeup(struct cx23885_dev *dev, |
286 | struct cx23885_dmaqueue *q, u32 count) | |
e47f30b1 ST |
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); | |
a19602f2 | 296 | |
e47f30b1 ST |
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; | |
a19602f2 | 302 | |
e47f30b1 ST |
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 | } | |
9c8ced51 | 310 | if (list_empty(&q->active)) |
e47f30b1 | 311 | del_timer(&q->timeout); |
9c8ced51 | 312 | else |
e47f30b1 | 313 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); |
e47f30b1 | 314 | if (bc != 1) |
21a78091 | 315 | printk(KERN_ERR "%s: %d buffers handled (should be 1)\n", |
22b4e64f | 316 | __func__, bc); |
e47f30b1 ST |
317 | } |
318 | ||
35045137 | 319 | int cx23885_set_tvnorm(struct cx23885_dev *dev, v4l2_std_id norm) |
e47f30b1 ST |
320 | { |
321 | dprintk(1, "%s(norm = 0x%08x) name: [%s]\n", | |
22b4e64f | 322 | __func__, |
e47f30b1 ST |
323 | (unsigned int)norm, |
324 | v4l2_norm_to_name(norm)); | |
325 | ||
326 | dev->tvnorm = norm; | |
327 | ||
f41737ec | 328 | call_all(dev, core, s_std, norm); |
e47f30b1 ST |
329 | |
330 | return 0; | |
331 | } | |
332 | ||
d45b9b8a | 333 | static struct video_device *cx23885_vdev_init(struct cx23885_dev *dev, |
e47f30b1 ST |
334 | struct pci_dev *pci, |
335 | struct video_device *template, | |
336 | char *type) | |
337 | { | |
338 | struct video_device *vfd; | |
22b4e64f | 339 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
340 | |
341 | vfd = video_device_alloc(); | |
342 | if (NULL == vfd) | |
343 | return NULL; | |
344 | *vfd = *template; | |
c0714f6c | 345 | vfd->v4l2_dev = &dev->v4l2_dev; |
e47f30b1 | 346 | vfd->release = video_device_release; |
9def5ed7 ST |
347 | snprintf(vfd->name, sizeof(vfd->name), "%s (%s)", |
348 | cx23885_boards[dev->board].name, type); | |
63b0d5ad | 349 | video_set_drvdata(vfd, dev); |
e47f30b1 ST |
350 | return vfd; |
351 | } | |
352 | ||
d45b9b8a | 353 | static int cx23885_ctrl_query(struct v4l2_queryctrl *qctrl) |
e47f30b1 ST |
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 | } | |
e47f30b1 ST |
370 | |
371 | /* ------------------------------------------------------------------- */ | |
372 | /* resource management */ | |
373 | ||
374 | static int res_get(struct cx23885_dev *dev, struct cx23885_fh *fh, | |
375 | unsigned int bit) | |
376 | { | |
22b4e64f | 377 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
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 | { | |
9c8ced51 | 399 | return fh->resources & bit; |
e47f30b1 ST |
400 | } |
401 | ||
402 | static int res_locked(struct cx23885_dev *dev, unsigned int bit) | |
403 | { | |
9c8ced51 | 404 | return dev->resources & bit; |
e47f30b1 ST |
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); | |
22b4e64f | 411 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
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 | ||
33cdeb35 ST |
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 | ||
d45b9b8a | 485 | static int cx23885_video_mux(struct cx23885_dev *dev, unsigned int input) |
e47f30b1 | 486 | { |
e47f30b1 | 487 | dprintk(1, "%s() video_mux: %d [vmux=%d, gpio=0x%x,0x%x,0x%x,0x%x]\n", |
22b4e64f | 488 | __func__, |
e47f30b1 ST |
489 | input, INPUT(input)->vmux, |
490 | INPUT(input)->gpio0, INPUT(input)->gpio1, | |
491 | INPUT(input)->gpio2, INPUT(input)->gpio3); | |
492 | dev->input = input; | |
493 | ||
6f0d8c02 | 494 | if (dev->board == CX23885_BOARD_MYGICA_X8506 || |
87988753 AJD |
495 | dev->board == CX23885_BOARD_MAGICPRO_PROHDTVE2 || |
496 | dev->board == CX23885_BOARD_MYGICA_X8507) { | |
6f0d8c02 DW |
497 | /* Select Analog TV */ |
498 | if (INPUT(input)->type == CX23885_VMUX_TELEVISION) | |
499 | cx23885_gpio_clear(dev, GPIO_0); | |
500 | } | |
501 | ||
e47f30b1 | 502 | /* Tell the internal A/V decoder */ |
5325b427 HV |
503 | v4l2_subdev_call(dev->sd_cx25840, video, s_routing, |
504 | INPUT(input)->vmux, 0, 0); | |
e47f30b1 | 505 | |
2cb9ccd4 | 506 | if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1800) || |
35045137 | 507 | (dev->board == CX23885_BOARD_MPX885) || |
d214ddc8 | 508 | (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1250) || |
0ac60acb DH |
509 | (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) || |
510 | (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111) || | |
082c0576 AJD |
511 | (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) || |
512 | (dev->board == CX23885_BOARD_MYGICA_X8507)) { | |
33cdeb35 ST |
513 | /* Configure audio routing */ |
514 | v4l2_subdev_call(dev->sd_cx25840, audio, s_routing, | |
515 | INPUT(input)->amux, 0, 0); | |
516 | ||
517 | if (INPUT(input)->amux == CX25840_AUDIO7) | |
518 | cx23885_flatiron_mux(dev, 1); | |
519 | else if (INPUT(input)->amux == CX25840_AUDIO6) | |
520 | cx23885_flatiron_mux(dev, 2); | |
521 | } | |
522 | ||
e47f30b1 ST |
523 | return 0; |
524 | } | |
e47f30b1 | 525 | |
fc1a889d ST |
526 | static int cx23885_audio_mux(struct cx23885_dev *dev, unsigned int input) |
527 | { | |
528 | dprintk(1, "%s(input=%d)\n", __func__, input); | |
529 | ||
fa1e0fd3 ST |
530 | /* The baseband video core of the cx23885 has two audio inputs. |
531 | * LR1 and LR2. In almost every single case so far only HVR1xxx | |
532 | * cards we've only ever supported LR1. Time to support LR2, | |
533 | * which is available via the optional white breakout header on | |
534 | * the board. | |
535 | * We'll use a could of existing enums in the card struct to allow | |
536 | * devs to specify which baseband input they need, or just default | |
537 | * to what we've always used. | |
538 | */ | |
539 | if (INPUT(input)->amux == CX25840_AUDIO7) | |
540 | cx23885_flatiron_mux(dev, 1); | |
541 | else if (INPUT(input)->amux == CX25840_AUDIO6) | |
542 | cx23885_flatiron_mux(dev, 2); | |
543 | else { | |
544 | /* Not specifically defined, assume the default. */ | |
545 | cx23885_flatiron_mux(dev, 1); | |
fc1a889d ST |
546 | } |
547 | ||
548 | return 0; | |
549 | } | |
550 | ||
e47f30b1 | 551 | /* ------------------------------------------------------------------ */ |
e47f30b1 ST |
552 | static int cx23885_start_video_dma(struct cx23885_dev *dev, |
553 | struct cx23885_dmaqueue *q, | |
554 | struct cx23885_buffer *buf) | |
555 | { | |
22b4e64f | 556 | dprintk(1, "%s()\n", __func__); |
e47f30b1 | 557 | |
a461e0ad ST |
558 | /* Stop the dma/fifo before we tamper with it's risc programs */ |
559 | cx_clear(VID_A_DMA_CTL, 0x11); | |
560 | ||
e47f30b1 ST |
561 | /* setup fifo + format */ |
562 | cx23885_sram_channel_setup(dev, &dev->sram_channels[SRAM_CH01], | |
563 | buf->bpl, buf->risc.dma); | |
e47f30b1 ST |
564 | |
565 | /* reset counter */ | |
566 | cx_write(VID_A_GPCNT_CTL, 3); | |
567 | q->count = 1; | |
568 | ||
569 | /* enable irq */ | |
dbe83a3b | 570 | cx23885_irq_add_enable(dev, 0x01); |
e47f30b1 ST |
571 | cx_set(VID_A_INT_MSK, 0x000011); |
572 | ||
573 | /* start dma */ | |
574 | cx_set(DEV_CNTRL2, (1<<5)); | |
575 | cx_set(VID_A_DMA_CTL, 0x11); /* FIFO and RISC enable */ | |
576 | ||
577 | return 0; | |
578 | } | |
579 | ||
e47f30b1 ST |
580 | |
581 | static int cx23885_restart_video_queue(struct cx23885_dev *dev, | |
582 | struct cx23885_dmaqueue *q) | |
583 | { | |
584 | struct cx23885_buffer *buf, *prev; | |
585 | struct list_head *item; | |
22b4e64f | 586 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
587 | |
588 | if (!list_empty(&q->active)) { | |
589 | buf = list_entry(q->active.next, struct cx23885_buffer, | |
590 | vb.queue); | |
591 | dprintk(2, "restart_queue [%p/%d]: restart dma\n", | |
592 | buf, buf->vb.i); | |
593 | cx23885_start_video_dma(dev, q, buf); | |
594 | list_for_each(item, &q->active) { | |
595 | buf = list_entry(item, struct cx23885_buffer, | |
596 | vb.queue); | |
597 | buf->count = q->count++; | |
598 | } | |
599 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); | |
600 | return 0; | |
601 | } | |
602 | ||
603 | prev = NULL; | |
604 | for (;;) { | |
605 | if (list_empty(&q->queued)) | |
606 | return 0; | |
607 | buf = list_entry(q->queued.next, struct cx23885_buffer, | |
608 | vb.queue); | |
609 | if (NULL == prev) { | |
610 | list_move_tail(&buf->vb.queue, &q->active); | |
611 | cx23885_start_video_dma(dev, q, buf); | |
612 | buf->vb.state = VIDEOBUF_ACTIVE; | |
613 | buf->count = q->count++; | |
614 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); | |
615 | dprintk(2, "[%p/%d] restart_queue - first active\n", | |
616 | buf, buf->vb.i); | |
617 | ||
618 | } else if (prev->vb.width == buf->vb.width && | |
619 | prev->vb.height == buf->vb.height && | |
620 | prev->fmt == buf->fmt) { | |
621 | list_move_tail(&buf->vb.queue, &q->active); | |
622 | buf->vb.state = VIDEOBUF_ACTIVE; | |
623 | buf->count = q->count++; | |
624 | prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); | |
625 | prev->risc.jmp[2] = cpu_to_le32(0); /* Bits 63 - 32 */ | |
626 | dprintk(2, "[%p/%d] restart_queue - move to active\n", | |
627 | buf, buf->vb.i); | |
628 | } else { | |
629 | return 0; | |
630 | } | |
631 | prev = buf; | |
632 | } | |
633 | } | |
634 | ||
635 | static int buffer_setup(struct videobuf_queue *q, unsigned int *count, | |
636 | unsigned int *size) | |
637 | { | |
638 | struct cx23885_fh *fh = q->priv_data; | |
639 | ||
640 | *size = fh->fmt->depth*fh->width*fh->height >> 3; | |
641 | if (0 == *count) | |
642 | *count = 32; | |
dab7e310 AB |
643 | if (*size * *count > vid_limit * 1024 * 1024) |
644 | *count = (vid_limit * 1024 * 1024) / *size; | |
e47f30b1 ST |
645 | return 0; |
646 | } | |
647 | ||
648 | static int buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb, | |
649 | enum v4l2_field field) | |
650 | { | |
651 | struct cx23885_fh *fh = q->priv_data; | |
652 | struct cx23885_dev *dev = fh->dev; | |
653 | struct cx23885_buffer *buf = | |
654 | container_of(vb, struct cx23885_buffer, vb); | |
655 | int rc, init_buffer = 0; | |
656 | u32 line0_offset, line1_offset; | |
657 | struct videobuf_dmabuf *dma = videobuf_to_dma(&buf->vb); | |
35045137 | 658 | int field_tff; |
e47f30b1 ST |
659 | |
660 | BUG_ON(NULL == fh->fmt); | |
661 | if (fh->width < 48 || fh->width > norm_maxw(dev->tvnorm) || | |
662 | fh->height < 32 || fh->height > norm_maxh(dev->tvnorm)) | |
663 | return -EINVAL; | |
664 | buf->vb.size = (fh->width * fh->height * fh->fmt->depth) >> 3; | |
665 | if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size) | |
666 | return -EINVAL; | |
667 | ||
668 | if (buf->fmt != fh->fmt || | |
669 | buf->vb.width != fh->width || | |
670 | buf->vb.height != fh->height || | |
671 | buf->vb.field != field) { | |
672 | buf->fmt = fh->fmt; | |
673 | buf->vb.width = fh->width; | |
674 | buf->vb.height = fh->height; | |
675 | buf->vb.field = field; | |
676 | init_buffer = 1; | |
677 | } | |
678 | ||
679 | if (VIDEOBUF_NEEDS_INIT == buf->vb.state) { | |
680 | init_buffer = 1; | |
681 | rc = videobuf_iolock(q, &buf->vb, NULL); | |
682 | if (0 != rc) | |
683 | goto fail; | |
684 | } | |
685 | ||
686 | if (init_buffer) { | |
687 | buf->bpl = buf->vb.width * buf->fmt->depth >> 3; | |
688 | switch (buf->vb.field) { | |
689 | case V4L2_FIELD_TOP: | |
690 | cx23885_risc_buffer(dev->pci, &buf->risc, | |
691 | dma->sglist, 0, UNSET, | |
692 | buf->bpl, 0, buf->vb.height); | |
693 | break; | |
694 | case V4L2_FIELD_BOTTOM: | |
695 | cx23885_risc_buffer(dev->pci, &buf->risc, | |
696 | dma->sglist, UNSET, 0, | |
697 | buf->bpl, 0, buf->vb.height); | |
698 | break; | |
699 | case V4L2_FIELD_INTERLACED: | |
35045137 ST |
700 | if (dev->tvnorm & V4L2_STD_NTSC) |
701 | /* NTSC or */ | |
702 | field_tff = 1; | |
703 | else | |
704 | field_tff = 0; | |
705 | ||
706 | if (cx23885_boards[dev->board].force_bff) | |
707 | /* PAL / SECAM OR 888 in NTSC MODE */ | |
708 | field_tff = 0; | |
709 | ||
710 | if (field_tff) { | |
e47f30b1 | 711 | /* cx25840 transmits NTSC bottom field first */ |
35045137 | 712 | dprintk(1, "%s() Creating TFF/NTSC risc\n", |
22b4e64f | 713 | __func__); |
e47f30b1 ST |
714 | line0_offset = buf->bpl; |
715 | line1_offset = 0; | |
716 | } else { | |
717 | /* All other formats are top field first */ | |
35045137 | 718 | dprintk(1, "%s() Creating BFF/PAL/SECAM risc\n", |
22b4e64f | 719 | __func__); |
e47f30b1 ST |
720 | line0_offset = 0; |
721 | line1_offset = buf->bpl; | |
722 | } | |
723 | cx23885_risc_buffer(dev->pci, &buf->risc, | |
724 | dma->sglist, line0_offset, | |
725 | line1_offset, | |
726 | buf->bpl, buf->bpl, | |
727 | buf->vb.height >> 1); | |
728 | break; | |
729 | case V4L2_FIELD_SEQ_TB: | |
730 | cx23885_risc_buffer(dev->pci, &buf->risc, | |
731 | dma->sglist, | |
732 | 0, buf->bpl * (buf->vb.height >> 1), | |
733 | buf->bpl, 0, | |
734 | buf->vb.height >> 1); | |
735 | break; | |
736 | case V4L2_FIELD_SEQ_BT: | |
737 | cx23885_risc_buffer(dev->pci, &buf->risc, | |
738 | dma->sglist, | |
739 | buf->bpl * (buf->vb.height >> 1), 0, | |
740 | buf->bpl, 0, | |
741 | buf->vb.height >> 1); | |
742 | break; | |
743 | default: | |
744 | BUG(); | |
745 | } | |
746 | } | |
747 | dprintk(2, "[%p/%d] buffer_prep - %dx%d %dbpp \"%s\" - dma=0x%08lx\n", | |
748 | buf, buf->vb.i, | |
749 | fh->width, fh->height, fh->fmt->depth, fh->fmt->name, | |
750 | (unsigned long)buf->risc.dma); | |
751 | ||
752 | buf->vb.state = VIDEOBUF_PREPARED; | |
753 | return 0; | |
754 | ||
755 | fail: | |
756 | cx23885_free_buffer(q, buf); | |
757 | return rc; | |
758 | } | |
759 | ||
760 | static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) | |
761 | { | |
762 | struct cx23885_buffer *buf = container_of(vb, | |
763 | struct cx23885_buffer, vb); | |
764 | struct cx23885_buffer *prev; | |
765 | struct cx23885_fh *fh = vq->priv_data; | |
766 | struct cx23885_dev *dev = fh->dev; | |
767 | struct cx23885_dmaqueue *q = &dev->vidq; | |
768 | ||
769 | /* add jump to stopper */ | |
770 | buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC); | |
771 | buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma); | |
772 | buf->risc.jmp[2] = cpu_to_le32(0); /* bits 63-32 */ | |
773 | ||
774 | if (!list_empty(&q->queued)) { | |
775 | list_add_tail(&buf->vb.queue, &q->queued); | |
776 | buf->vb.state = VIDEOBUF_QUEUED; | |
777 | dprintk(2, "[%p/%d] buffer_queue - append to queued\n", | |
778 | buf, buf->vb.i); | |
779 | ||
780 | } else if (list_empty(&q->active)) { | |
781 | list_add_tail(&buf->vb.queue, &q->active); | |
782 | cx23885_start_video_dma(dev, q, buf); | |
783 | buf->vb.state = VIDEOBUF_ACTIVE; | |
784 | buf->count = q->count++; | |
785 | mod_timer(&q->timeout, jiffies+BUFFER_TIMEOUT); | |
786 | dprintk(2, "[%p/%d] buffer_queue - first active\n", | |
787 | buf, buf->vb.i); | |
788 | ||
789 | } else { | |
790 | prev = list_entry(q->active.prev, struct cx23885_buffer, | |
791 | vb.queue); | |
792 | if (prev->vb.width == buf->vb.width && | |
793 | prev->vb.height == buf->vb.height && | |
794 | prev->fmt == buf->fmt) { | |
795 | list_add_tail(&buf->vb.queue, &q->active); | |
796 | buf->vb.state = VIDEOBUF_ACTIVE; | |
797 | buf->count = q->count++; | |
798 | prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma); | |
799 | /* 64 bit bits 63-32 */ | |
800 | prev->risc.jmp[2] = cpu_to_le32(0); | |
801 | dprintk(2, "[%p/%d] buffer_queue - append to active\n", | |
802 | buf, buf->vb.i); | |
803 | ||
804 | } else { | |
805 | list_add_tail(&buf->vb.queue, &q->queued); | |
806 | buf->vb.state = VIDEOBUF_QUEUED; | |
807 | dprintk(2, "[%p/%d] buffer_queue - first queued\n", | |
808 | buf, buf->vb.i); | |
809 | } | |
810 | } | |
811 | } | |
812 | ||
813 | static void buffer_release(struct videobuf_queue *q, | |
814 | struct videobuf_buffer *vb) | |
815 | { | |
816 | struct cx23885_buffer *buf = container_of(vb, | |
817 | struct cx23885_buffer, vb); | |
818 | ||
819 | cx23885_free_buffer(q, buf); | |
820 | } | |
821 | ||
822 | static struct videobuf_queue_ops cx23885_video_qops = { | |
823 | .buf_setup = buffer_setup, | |
824 | .buf_prepare = buffer_prepare, | |
825 | .buf_queue = buffer_queue, | |
826 | .buf_release = buffer_release, | |
827 | }; | |
828 | ||
829 | static struct videobuf_queue *get_queue(struct cx23885_fh *fh) | |
830 | { | |
831 | switch (fh->type) { | |
832 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
833 | return &fh->vidq; | |
834 | case V4L2_BUF_TYPE_VBI_CAPTURE: | |
835 | return &fh->vbiq; | |
836 | default: | |
837 | BUG(); | |
838 | return NULL; | |
839 | } | |
840 | } | |
841 | ||
842 | static int get_resource(struct cx23885_fh *fh) | |
843 | { | |
844 | switch (fh->type) { | |
845 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
846 | return RESOURCE_VIDEO; | |
847 | case V4L2_BUF_TYPE_VBI_CAPTURE: | |
848 | return RESOURCE_VBI; | |
849 | default: | |
850 | BUG(); | |
851 | return 0; | |
852 | } | |
853 | } | |
854 | ||
bec43661 | 855 | static int video_open(struct file *file) |
e47f30b1 | 856 | { |
63b0d5ad LP |
857 | struct video_device *vdev = video_devdata(file); |
858 | struct cx23885_dev *dev = video_drvdata(file); | |
e47f30b1 | 859 | struct cx23885_fh *fh; |
e47f30b1 ST |
860 | enum v4l2_buf_type type = 0; |
861 | int radio = 0; | |
862 | ||
63b0d5ad LP |
863 | switch (vdev->vfl_type) { |
864 | case VFL_TYPE_GRABBER: | |
865 | type = V4L2_BUF_TYPE_VIDEO_CAPTURE; | |
866 | break; | |
867 | case VFL_TYPE_VBI: | |
868 | type = V4L2_BUF_TYPE_VBI_CAPTURE; | |
869 | break; | |
870 | case VFL_TYPE_RADIO: | |
871 | radio = 1; | |
872 | break; | |
d56dc612 | 873 | } |
e47f30b1 | 874 | |
50462eb0 LP |
875 | dprintk(1, "open dev=%s radio=%d type=%s\n", |
876 | video_device_node_name(vdev), radio, v4l2_type_names[type]); | |
e47f30b1 ST |
877 | |
878 | /* allocate + initialize per filehandle data */ | |
879 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); | |
63b0d5ad | 880 | if (NULL == fh) |
e47f30b1 | 881 | return -ENOMEM; |
63b0d5ad | 882 | |
e47f30b1 ST |
883 | file->private_data = fh; |
884 | fh->dev = dev; | |
885 | fh->radio = radio; | |
886 | fh->type = type; | |
887 | fh->width = 320; | |
888 | fh->height = 240; | |
af76e9f6 | 889 | fh->fmt = format_by_fourcc(V4L2_PIX_FMT_YUYV); |
e47f30b1 | 890 | |
0705135e GL |
891 | videobuf_queue_sg_init(&fh->vidq, &cx23885_video_qops, |
892 | &dev->pci->dev, &dev->slock, | |
e47f30b1 ST |
893 | V4L2_BUF_TYPE_VIDEO_CAPTURE, |
894 | V4L2_FIELD_INTERLACED, | |
895 | sizeof(struct cx23885_buffer), | |
08bff03e | 896 | fh, NULL); |
e47f30b1 | 897 | |
79776c89 ST |
898 | videobuf_queue_sg_init(&fh->vbiq, &cx23885_vbi_qops, |
899 | &dev->pci->dev, &dev->slock, | |
900 | V4L2_BUF_TYPE_VBI_CAPTURE, | |
901 | V4L2_FIELD_SEQ_TB, | |
902 | sizeof(struct cx23885_buffer), | |
903 | fh, NULL); | |
904 | ||
905 | ||
e47f30b1 ST |
906 | dprintk(1, "post videobuf_queue_init()\n"); |
907 | ||
e47f30b1 ST |
908 | return 0; |
909 | } | |
910 | ||
911 | static ssize_t video_read(struct file *file, char __user *data, | |
912 | size_t count, loff_t *ppos) | |
913 | { | |
914 | struct cx23885_fh *fh = file->private_data; | |
915 | ||
916 | switch (fh->type) { | |
917 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: | |
918 | if (res_locked(fh->dev, RESOURCE_VIDEO)) | |
919 | return -EBUSY; | |
920 | return videobuf_read_one(&fh->vidq, data, count, ppos, | |
921 | file->f_flags & O_NONBLOCK); | |
922 | case V4L2_BUF_TYPE_VBI_CAPTURE: | |
923 | if (!res_get(fh->dev, fh, RESOURCE_VBI)) | |
924 | return -EBUSY; | |
925 | return videobuf_read_stream(&fh->vbiq, data, count, ppos, 1, | |
926 | file->f_flags & O_NONBLOCK); | |
927 | default: | |
928 | BUG(); | |
929 | return 0; | |
930 | } | |
931 | } | |
932 | ||
933 | static unsigned int video_poll(struct file *file, | |
934 | struct poll_table_struct *wait) | |
935 | { | |
936 | struct cx23885_fh *fh = file->private_data; | |
937 | struct cx23885_buffer *buf; | |
9fd6418a | 938 | unsigned int rc = POLLERR; |
e47f30b1 ST |
939 | |
940 | if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) { | |
941 | if (!res_get(fh->dev, fh, RESOURCE_VBI)) | |
942 | return POLLERR; | |
943 | return videobuf_poll_stream(file, &fh->vbiq, wait); | |
944 | } | |
945 | ||
9fd6418a | 946 | mutex_lock(&fh->vidq.vb_lock); |
e47f30b1 ST |
947 | if (res_check(fh, RESOURCE_VIDEO)) { |
948 | /* streaming capture */ | |
949 | if (list_empty(&fh->vidq.stream)) | |
9fd6418a | 950 | goto done; |
e47f30b1 ST |
951 | buf = list_entry(fh->vidq.stream.next, |
952 | struct cx23885_buffer, vb.stream); | |
953 | } else { | |
954 | /* read() capture */ | |
955 | buf = (struct cx23885_buffer *)fh->vidq.read_buf; | |
956 | if (NULL == buf) | |
9fd6418a | 957 | goto done; |
e47f30b1 ST |
958 | } |
959 | poll_wait(file, &buf->vb.done, wait); | |
960 | if (buf->vb.state == VIDEOBUF_DONE || | |
961 | buf->vb.state == VIDEOBUF_ERROR) | |
9fd6418a F |
962 | rc = POLLIN|POLLRDNORM; |
963 | else | |
964 | rc = 0; | |
965 | done: | |
966 | mutex_unlock(&fh->vidq.vb_lock); | |
967 | return rc; | |
e47f30b1 ST |
968 | } |
969 | ||
bec43661 | 970 | static int video_release(struct file *file) |
e47f30b1 ST |
971 | { |
972 | struct cx23885_fh *fh = file->private_data; | |
973 | struct cx23885_dev *dev = fh->dev; | |
974 | ||
975 | /* turn off overlay */ | |
976 | if (res_check(fh, RESOURCE_OVERLAY)) { | |
977 | /* FIXME */ | |
978 | res_free(dev, fh, RESOURCE_OVERLAY); | |
979 | } | |
980 | ||
981 | /* stop video capture */ | |
982 | if (res_check(fh, RESOURCE_VIDEO)) { | |
983 | videobuf_queue_cancel(&fh->vidq); | |
984 | res_free(dev, fh, RESOURCE_VIDEO); | |
985 | } | |
986 | if (fh->vidq.read_buf) { | |
987 | buffer_release(&fh->vidq, fh->vidq.read_buf); | |
988 | kfree(fh->vidq.read_buf); | |
989 | } | |
990 | ||
991 | /* stop vbi capture */ | |
992 | if (res_check(fh, RESOURCE_VBI)) { | |
993 | if (fh->vbiq.streaming) | |
994 | videobuf_streamoff(&fh->vbiq); | |
995 | if (fh->vbiq.reading) | |
996 | videobuf_read_stop(&fh->vbiq); | |
997 | res_free(dev, fh, RESOURCE_VBI); | |
998 | } | |
999 | ||
1000 | videobuf_mmap_free(&fh->vidq); | |
35045137 ST |
1001 | videobuf_mmap_free(&fh->vbiq); |
1002 | ||
e47f30b1 ST |
1003 | file->private_data = NULL; |
1004 | kfree(fh); | |
1005 | ||
a19602f2 ST |
1006 | /* We are not putting the tuner to sleep here on exit, because |
1007 | * we want to use the mpeg encoder in another session to capture | |
1008 | * tuner video. Closing this will result in no video to the encoder. | |
1009 | */ | |
e47f30b1 ST |
1010 | |
1011 | return 0; | |
1012 | } | |
1013 | ||
1014 | static int video_mmap(struct file *file, struct vm_area_struct *vma) | |
1015 | { | |
1016 | struct cx23885_fh *fh = file->private_data; | |
1017 | ||
1018 | return videobuf_mmap_mapper(get_queue(fh), vma); | |
1019 | } | |
1020 | ||
1021 | /* ------------------------------------------------------------------ */ | |
1022 | /* VIDEO CTRL IOCTLS */ | |
1023 | ||
35045137 | 1024 | int cx23885_get_control(struct cx23885_dev *dev, |
9c8ced51 | 1025 | struct v4l2_control *ctl) |
e47f30b1 | 1026 | { |
22b4e64f | 1027 | dprintk(1, "%s() calling cx25840(VIDIOC_G_CTRL)\n", __func__); |
0d5a19f1 | 1028 | call_all(dev, core, g_ctrl, ctl); |
e47f30b1 ST |
1029 | return 0; |
1030 | } | |
e47f30b1 | 1031 | |
35045137 | 1032 | int cx23885_set_control(struct cx23885_dev *dev, |
9c8ced51 | 1033 | struct v4l2_control *ctl) |
e47f30b1 | 1034 | { |
97ce5670 MM |
1035 | dprintk(1, "%s() calling cx25840(VIDIOC_S_CTRL)\n", __func__); |
1036 | call_all(dev, core, s_ctrl, ctl); | |
1037 | ||
e47f30b1 ST |
1038 | return 0; |
1039 | } | |
e47f30b1 ST |
1040 | |
1041 | static void init_controls(struct cx23885_dev *dev) | |
1042 | { | |
1043 | struct v4l2_control ctrl; | |
1044 | int i; | |
1045 | ||
1046 | for (i = 0; i < CX23885_CTLS; i++) { | |
1047 | ctrl.id = cx23885_ctls[i].v.id; | |
1048 | ctrl.value = cx23885_ctls[i].v.default_value; | |
1049 | ||
1050 | cx23885_set_control(dev, &ctrl); | |
1051 | } | |
1052 | } | |
1053 | ||
1054 | /* ------------------------------------------------------------------ */ | |
1055 | /* VIDEO IOCTLS */ | |
1056 | ||
78b526a4 | 1057 | static int vidioc_g_fmt_vid_cap(struct file *file, void *priv, |
e47f30b1 ST |
1058 | struct v4l2_format *f) |
1059 | { | |
1060 | struct cx23885_fh *fh = priv; | |
1061 | ||
1062 | f->fmt.pix.width = fh->width; | |
1063 | f->fmt.pix.height = fh->height; | |
1064 | f->fmt.pix.field = fh->vidq.field; | |
1065 | f->fmt.pix.pixelformat = fh->fmt->fourcc; | |
1066 | f->fmt.pix.bytesperline = | |
1067 | (f->fmt.pix.width * fh->fmt->depth) >> 3; | |
1068 | f->fmt.pix.sizeimage = | |
1069 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
1070 | ||
1071 | return 0; | |
1072 | } | |
1073 | ||
78b526a4 | 1074 | static int vidioc_try_fmt_vid_cap(struct file *file, void *priv, |
e47f30b1 ST |
1075 | struct v4l2_format *f) |
1076 | { | |
1077 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1078 | struct cx23885_fmt *fmt; | |
1079 | enum v4l2_field field; | |
1080 | unsigned int maxw, maxh; | |
1081 | ||
1082 | fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
1083 | if (NULL == fmt) | |
1084 | return -EINVAL; | |
1085 | ||
1086 | field = f->fmt.pix.field; | |
1087 | maxw = norm_maxw(dev->tvnorm); | |
1088 | maxh = norm_maxh(dev->tvnorm); | |
1089 | ||
1090 | if (V4L2_FIELD_ANY == field) { | |
1091 | field = (f->fmt.pix.height > maxh/2) | |
1092 | ? V4L2_FIELD_INTERLACED | |
1093 | : V4L2_FIELD_BOTTOM; | |
1094 | } | |
1095 | ||
1096 | switch (field) { | |
1097 | case V4L2_FIELD_TOP: | |
1098 | case V4L2_FIELD_BOTTOM: | |
1099 | maxh = maxh / 2; | |
1100 | break; | |
1101 | case V4L2_FIELD_INTERLACED: | |
1102 | break; | |
1103 | default: | |
1104 | return -EINVAL; | |
1105 | } | |
1106 | ||
1107 | f->fmt.pix.field = field; | |
2449afcb TP |
1108 | v4l_bound_align_image(&f->fmt.pix.width, 48, maxw, 2, |
1109 | &f->fmt.pix.height, 32, maxh, 0, 0); | |
e47f30b1 ST |
1110 | f->fmt.pix.bytesperline = |
1111 | (f->fmt.pix.width * fmt->depth) >> 3; | |
1112 | f->fmt.pix.sizeimage = | |
1113 | f->fmt.pix.height * f->fmt.pix.bytesperline; | |
1114 | ||
1115 | return 0; | |
1116 | } | |
1117 | ||
78b526a4 | 1118 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
e47f30b1 ST |
1119 | struct v4l2_format *f) |
1120 | { | |
1121 | struct cx23885_fh *fh = priv; | |
1122 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
cc99113b | 1123 | struct v4l2_mbus_framefmt mbus_fmt; |
e47f30b1 ST |
1124 | int err; |
1125 | ||
22b4e64f | 1126 | dprintk(2, "%s()\n", __func__); |
78b526a4 | 1127 | err = vidioc_try_fmt_vid_cap(file, priv, f); |
e47f30b1 ST |
1128 | |
1129 | if (0 != err) | |
1130 | return err; | |
1131 | fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
1132 | fh->width = f->fmt.pix.width; | |
1133 | fh->height = f->fmt.pix.height; | |
1134 | fh->vidq.field = f->fmt.pix.field; | |
22b4e64f | 1135 | dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, |
e47f30b1 | 1136 | fh->width, fh->height, fh->vidq.field); |
cc99113b HV |
1137 | v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED); |
1138 | call_all(dev, video, s_mbus_fmt, &mbus_fmt); | |
1139 | v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); | |
e47f30b1 ST |
1140 | return 0; |
1141 | } | |
1142 | ||
1143 | static int vidioc_querycap(struct file *file, void *priv, | |
1144 | struct v4l2_capability *cap) | |
1145 | { | |
1146 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1147 | ||
1148 | strcpy(cap->driver, "cx23885"); | |
1149 | strlcpy(cap->card, cx23885_boards[dev->board].name, | |
1150 | sizeof(cap->card)); | |
1151 | sprintf(cap->bus_info, "PCIe:%s", pci_name(dev->pci)); | |
e47f30b1 ST |
1152 | cap->capabilities = |
1153 | V4L2_CAP_VIDEO_CAPTURE | | |
1154 | V4L2_CAP_READWRITE | | |
1155 | V4L2_CAP_STREAMING | | |
1156 | V4L2_CAP_VBI_CAPTURE; | |
1157 | if (UNSET != dev->tuner_type) | |
1158 | cap->capabilities |= V4L2_CAP_TUNER; | |
1159 | return 0; | |
1160 | } | |
1161 | ||
78b526a4 | 1162 | static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv, |
e47f30b1 ST |
1163 | struct v4l2_fmtdesc *f) |
1164 | { | |
1165 | if (unlikely(f->index >= ARRAY_SIZE(formats))) | |
1166 | return -EINVAL; | |
1167 | ||
1168 | strlcpy(f->description, formats[f->index].name, | |
1169 | sizeof(f->description)); | |
1170 | f->pixelformat = formats[f->index].fourcc; | |
1171 | ||
1172 | return 0; | |
1173 | } | |
1174 | ||
e47f30b1 ST |
1175 | static int vidioc_reqbufs(struct file *file, void *priv, |
1176 | struct v4l2_requestbuffers *p) | |
1177 | { | |
1178 | struct cx23885_fh *fh = priv; | |
9c8ced51 | 1179 | return videobuf_reqbufs(get_queue(fh), p); |
e47f30b1 ST |
1180 | } |
1181 | ||
1182 | static int vidioc_querybuf(struct file *file, void *priv, | |
1183 | struct v4l2_buffer *p) | |
1184 | { | |
1185 | struct cx23885_fh *fh = priv; | |
9c8ced51 | 1186 | return videobuf_querybuf(get_queue(fh), p); |
e47f30b1 ST |
1187 | } |
1188 | ||
1189 | static int vidioc_qbuf(struct file *file, void *priv, | |
1190 | struct v4l2_buffer *p) | |
1191 | { | |
1192 | struct cx23885_fh *fh = priv; | |
9c8ced51 | 1193 | return videobuf_qbuf(get_queue(fh), p); |
e47f30b1 ST |
1194 | } |
1195 | ||
1196 | static int vidioc_dqbuf(struct file *file, void *priv, | |
1197 | struct v4l2_buffer *p) | |
1198 | { | |
1199 | struct cx23885_fh *fh = priv; | |
9c8ced51 ST |
1200 | return videobuf_dqbuf(get_queue(fh), p, |
1201 | file->f_flags & O_NONBLOCK); | |
e47f30b1 ST |
1202 | } |
1203 | ||
1204 | static int vidioc_streamon(struct file *file, void *priv, | |
1205 | enum v4l2_buf_type i) | |
1206 | { | |
1207 | struct cx23885_fh *fh = priv; | |
1208 | struct cx23885_dev *dev = fh->dev; | |
22b4e64f | 1209 | dprintk(1, "%s()\n", __func__); |
e47f30b1 | 1210 | |
5ab27e6d ST |
1211 | if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
1212 | (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) | |
e47f30b1 ST |
1213 | return -EINVAL; |
1214 | if (unlikely(i != fh->type)) | |
1215 | return -EINVAL; | |
1216 | ||
1217 | if (unlikely(!res_get(dev, fh, get_resource(fh)))) | |
1218 | return -EBUSY; | |
24465b44 ST |
1219 | |
1220 | /* Don't start VBI streaming unless vida streaming | |
1221 | * has already started. | |
1222 | */ | |
1223 | if ((fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) && | |
1224 | ((cx_read(VID_A_DMA_CTL) & 0x11) == 0)) | |
1225 | return -EINVAL; | |
1226 | ||
e47f30b1 ST |
1227 | return videobuf_streamon(get_queue(fh)); |
1228 | } | |
1229 | ||
1230 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) | |
1231 | { | |
1232 | struct cx23885_fh *fh = priv; | |
1233 | struct cx23885_dev *dev = fh->dev; | |
1234 | int err, res; | |
22b4e64f | 1235 | dprintk(1, "%s()\n", __func__); |
e47f30b1 | 1236 | |
5ab27e6d ST |
1237 | if ((fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) && |
1238 | (fh->type != V4L2_BUF_TYPE_VBI_CAPTURE)) | |
e47f30b1 ST |
1239 | return -EINVAL; |
1240 | if (i != fh->type) | |
1241 | return -EINVAL; | |
1242 | ||
1243 | res = get_resource(fh); | |
1244 | err = videobuf_streamoff(get_queue(fh)); | |
1245 | if (err < 0) | |
1246 | return err; | |
1247 | res_free(dev, fh, res); | |
1248 | return 0; | |
1249 | } | |
1250 | ||
35045137 ST |
1251 | static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id) |
1252 | { | |
1253 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1254 | dprintk(1, "%s()\n", __func__); | |
1255 | ||
1256 | call_all(dev, core, g_std, id); | |
1257 | ||
1258 | return 0; | |
1259 | } | |
1260 | ||
e47f30b1 ST |
1261 | static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id *tvnorms) |
1262 | { | |
1263 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
22b4e64f | 1264 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
1265 | |
1266 | mutex_lock(&dev->lock); | |
1267 | cx23885_set_tvnorm(dev, *tvnorms); | |
1268 | mutex_unlock(&dev->lock); | |
1269 | ||
1270 | return 0; | |
1271 | } | |
1272 | ||
35045137 | 1273 | int cx23885_enum_input(struct cx23885_dev *dev, struct v4l2_input *i) |
e47f30b1 ST |
1274 | { |
1275 | static const char *iname[] = { | |
1276 | [CX23885_VMUX_COMPOSITE1] = "Composite1", | |
1277 | [CX23885_VMUX_COMPOSITE2] = "Composite2", | |
1278 | [CX23885_VMUX_COMPOSITE3] = "Composite3", | |
1279 | [CX23885_VMUX_COMPOSITE4] = "Composite4", | |
1280 | [CX23885_VMUX_SVIDEO] = "S-Video", | |
dac65fa1 | 1281 | [CX23885_VMUX_COMPONENT] = "Component", |
e47f30b1 ST |
1282 | [CX23885_VMUX_TELEVISION] = "Television", |
1283 | [CX23885_VMUX_CABLE] = "Cable TV", | |
1284 | [CX23885_VMUX_DVB] = "DVB", | |
1285 | [CX23885_VMUX_DEBUG] = "for debug only", | |
1286 | }; | |
1287 | unsigned int n; | |
22b4e64f | 1288 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
1289 | |
1290 | n = i->index; | |
e92bcf8d | 1291 | if (n >= MAX_CX23885_INPUT) |
e47f30b1 ST |
1292 | return -EINVAL; |
1293 | ||
1294 | if (0 == INPUT(n)->type) | |
1295 | return -EINVAL; | |
1296 | ||
e47f30b1 ST |
1297 | i->index = n; |
1298 | i->type = V4L2_INPUT_TYPE_CAMERA; | |
1299 | strcpy(i->name, iname[INPUT(n)->type]); | |
1300 | if ((CX23885_VMUX_TELEVISION == INPUT(n)->type) || | |
473d8024 | 1301 | (CX23885_VMUX_CABLE == INPUT(n)->type)) { |
e47f30b1 ST |
1302 | i->type = V4L2_INPUT_TYPE_TUNER; |
1303 | i->std = CX23885_NORMS; | |
473d8024 | 1304 | } |
6c6f52fd ST |
1305 | |
1306 | /* Two selectable audio inputs for non-tv inputs */ | |
1307 | if (INPUT(n)->type != CX23885_VMUX_TELEVISION) | |
1308 | i->audioset = 0x3; | |
1309 | ||
c147f610 ST |
1310 | if (dev->input == n) { |
1311 | /* enum'd input matches our configured input. | |
1312 | * Ask the video decoder to process the call | |
1313 | * and give it an oppertunity to update the | |
1314 | * status field. | |
1315 | */ | |
1316 | call_all(dev, video, g_input_status, &i->status); | |
1317 | } | |
1318 | ||
e47f30b1 ST |
1319 | return 0; |
1320 | } | |
e47f30b1 ST |
1321 | |
1322 | static int vidioc_enum_input(struct file *file, void *priv, | |
1323 | struct v4l2_input *i) | |
1324 | { | |
1325 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
22b4e64f | 1326 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
1327 | return cx23885_enum_input(dev, i); |
1328 | } | |
1329 | ||
35045137 | 1330 | int cx23885_get_input(struct file *file, void *priv, unsigned int *i) |
e47f30b1 ST |
1331 | { |
1332 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1333 | ||
1334 | *i = dev->input; | |
22b4e64f | 1335 | dprintk(1, "%s() returns %d\n", __func__, *i); |
e47f30b1 ST |
1336 | return 0; |
1337 | } | |
1338 | ||
35045137 ST |
1339 | static int vidioc_g_input(struct file *file, void *priv, unsigned int *i) |
1340 | { | |
1341 | return cx23885_get_input(file, priv, i); | |
1342 | } | |
1343 | ||
1344 | int cx23885_set_input(struct file *file, void *priv, unsigned int i) | |
e47f30b1 ST |
1345 | { |
1346 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1347 | ||
22b4e64f | 1348 | dprintk(1, "%s(%d)\n", __func__, i); |
e47f30b1 | 1349 | |
e92bcf8d | 1350 | if (i >= MAX_CX23885_INPUT) { |
22b4e64f | 1351 | dprintk(1, "%s() -EINVAL\n", __func__); |
e47f30b1 ST |
1352 | return -EINVAL; |
1353 | } | |
1354 | ||
e92bcf8d ST |
1355 | if (INPUT(i)->type == 0) |
1356 | return -EINVAL; | |
1357 | ||
e47f30b1 ST |
1358 | mutex_lock(&dev->lock); |
1359 | cx23885_video_mux(dev, i); | |
fa1e0fd3 ST |
1360 | |
1361 | /* By default establish the default audio input for the card also */ | |
1362 | /* Caller is free to use VIDIOC_S_AUDIO to override afterwards */ | |
1363 | cx23885_audio_mux(dev, i); | |
e47f30b1 ST |
1364 | mutex_unlock(&dev->lock); |
1365 | return 0; | |
1366 | } | |
1367 | ||
35045137 ST |
1368 | static int vidioc_s_input(struct file *file, void *priv, unsigned int i) |
1369 | { | |
1370 | return cx23885_set_input(file, priv, i); | |
1371 | } | |
1372 | ||
e9e5cf47 AW |
1373 | static int vidioc_log_status(struct file *file, void *priv) |
1374 | { | |
1375 | struct cx23885_fh *fh = priv; | |
1376 | struct cx23885_dev *dev = fh->dev; | |
1377 | ||
1378 | printk(KERN_INFO | |
1379 | "%s/0: ============ START LOG STATUS ============\n", | |
35045137 | 1380 | dev->name); |
e9e5cf47 AW |
1381 | call_all(dev, core, log_status); |
1382 | printk(KERN_INFO | |
1383 | "%s/0: ============= END LOG STATUS =============\n", | |
35045137 | 1384 | dev->name); |
e9e5cf47 AW |
1385 | return 0; |
1386 | } | |
1387 | ||
fc1a889d ST |
1388 | static int cx23885_query_audinput(struct file *file, void *priv, |
1389 | struct v4l2_audio *i) | |
1390 | { | |
1391 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1392 | static const char *iname[] = { | |
1393 | [0] = "Baseband L/R 1", | |
1394 | [1] = "Baseband L/R 2", | |
1395 | }; | |
1396 | unsigned int n; | |
1397 | dprintk(1, "%s()\n", __func__); | |
1398 | ||
1399 | n = i->index; | |
1400 | if (n >= 2) | |
1401 | return -EINVAL; | |
1402 | ||
1403 | memset(i, 0, sizeof(*i)); | |
1404 | i->index = n; | |
1405 | strcpy(i->name, iname[n]); | |
1406 | i->capability = V4L2_AUDCAP_STEREO; | |
1407 | i->mode = V4L2_AUDMODE_AVL; | |
1408 | return 0; | |
1409 | ||
1410 | } | |
1411 | ||
1412 | static int vidioc_enum_audinput(struct file *file, void *priv, | |
1413 | struct v4l2_audio *i) | |
1414 | { | |
1415 | return cx23885_query_audinput(file, priv, i); | |
1416 | } | |
1417 | ||
1418 | static int vidioc_g_audinput(struct file *file, void *priv, | |
1419 | struct v4l2_audio *i) | |
1420 | { | |
1421 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1422 | ||
1423 | i->index = dev->audinput; | |
1424 | dprintk(1, "%s(input=%d)\n", __func__, i->index); | |
1425 | ||
1426 | return cx23885_query_audinput(file, priv, i); | |
1427 | } | |
1428 | ||
1429 | static int vidioc_s_audinput(struct file *file, void *priv, | |
0e8025b9 | 1430 | const struct v4l2_audio *i) |
fc1a889d ST |
1431 | { |
1432 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
fc1a889d ST |
1433 | if (i->index >= 2) |
1434 | return -EINVAL; | |
1435 | ||
1436 | dprintk(1, "%s(%d)\n", __func__, i->index); | |
1437 | ||
fc1a889d | 1438 | dev->audinput = i->index; |
fa1e0fd3 ST |
1439 | |
1440 | /* Skip the audio defaults from the cards struct, caller wants | |
1441 | * directly touch the audio mux hardware. */ | |
1442 | cx23885_flatiron_mux(dev, dev->audinput + 1); | |
fc1a889d ST |
1443 | return 0; |
1444 | } | |
1445 | ||
e47f30b1 ST |
1446 | static int vidioc_queryctrl(struct file *file, void *priv, |
1447 | struct v4l2_queryctrl *qctrl) | |
1448 | { | |
1449 | qctrl->id = v4l2_ctrl_next(ctrl_classes, qctrl->id); | |
1450 | if (unlikely(qctrl->id == 0)) | |
1451 | return -EINVAL; | |
1452 | return cx23885_ctrl_query(qctrl); | |
1453 | } | |
1454 | ||
1455 | static int vidioc_g_ctrl(struct file *file, void *priv, | |
1456 | struct v4l2_control *ctl) | |
1457 | { | |
1458 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1459 | ||
1460 | return cx23885_get_control(dev, ctl); | |
1461 | } | |
1462 | ||
1463 | static int vidioc_s_ctrl(struct file *file, void *priv, | |
1464 | struct v4l2_control *ctl) | |
1465 | { | |
1466 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1467 | ||
1468 | return cx23885_set_control(dev, ctl); | |
1469 | } | |
1470 | ||
1471 | static int vidioc_g_tuner(struct file *file, void *priv, | |
1472 | struct v4l2_tuner *t) | |
1473 | { | |
1474 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1475 | ||
1476 | if (unlikely(UNSET == dev->tuner_type)) | |
1477 | return -EINVAL; | |
1478 | if (0 != t->index) | |
1479 | return -EINVAL; | |
1480 | ||
1481 | strcpy(t->name, "Television"); | |
97ce5670 | 1482 | |
80f1e086 | 1483 | call_all(dev, tuner, g_tuner, t); |
e47f30b1 ST |
1484 | return 0; |
1485 | } | |
1486 | ||
1487 | static int vidioc_s_tuner(struct file *file, void *priv, | |
1488 | struct v4l2_tuner *t) | |
1489 | { | |
1490 | struct cx23885_dev *dev = ((struct cx23885_fh *)priv)->dev; | |
1491 | ||
1492 | if (UNSET == dev->tuner_type) | |
1493 | return -EINVAL; | |
1494 | if (0 != t->index) | |
1495 | return -EINVAL; | |
97ce5670 | 1496 | /* Update the A/V core */ |
80f1e086 | 1497 | call_all(dev, tuner, s_tuner, t); |
97ce5670 | 1498 | |
e47f30b1 ST |
1499 | return 0; |
1500 | } | |
1501 | ||
1502 | static int vidioc_g_frequency(struct file *file, void *priv, | |
1503 | struct v4l2_frequency *f) | |
1504 | { | |
1505 | struct cx23885_fh *fh = priv; | |
1506 | struct cx23885_dev *dev = fh->dev; | |
1507 | ||
1508 | if (unlikely(UNSET == dev->tuner_type)) | |
1509 | return -EINVAL; | |
1510 | ||
1511 | /* f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; */ | |
1512 | f->type = fh->radio ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV; | |
1513 | f->frequency = dev->freq; | |
1514 | ||
0d5a19f1 | 1515 | call_all(dev, tuner, g_frequency, f); |
e47f30b1 ST |
1516 | |
1517 | return 0; | |
1518 | } | |
1519 | ||
d45b9b8a | 1520 | static int cx23885_set_freq(struct cx23885_dev *dev, struct v4l2_frequency *f) |
e47f30b1 | 1521 | { |
35045137 ST |
1522 | struct v4l2_control ctrl; |
1523 | ||
e47f30b1 ST |
1524 | if (unlikely(UNSET == dev->tuner_type)) |
1525 | return -EINVAL; | |
1526 | if (unlikely(f->tuner != 0)) | |
1527 | return -EINVAL; | |
1528 | ||
1529 | mutex_lock(&dev->lock); | |
1530 | dev->freq = f->frequency; | |
1531 | ||
35045137 ST |
1532 | /* I need to mute audio here */ |
1533 | ctrl.id = V4L2_CID_AUDIO_MUTE; | |
1534 | ctrl.value = 1; | |
1535 | cx23885_set_control(dev, &ctrl); | |
1536 | ||
0d5a19f1 | 1537 | call_all(dev, tuner, s_frequency, f); |
e47f30b1 ST |
1538 | |
1539 | /* When changing channels it is required to reset TVAUDIO */ | |
35045137 ST |
1540 | msleep(100); |
1541 | ||
1542 | /* I need to unmute audio here */ | |
1543 | ctrl.value = 0; | |
1544 | cx23885_set_control(dev, &ctrl); | |
e47f30b1 ST |
1545 | |
1546 | mutex_unlock(&dev->lock); | |
1547 | ||
1548 | return 0; | |
1549 | } | |
e47f30b1 | 1550 | |
35045137 ST |
1551 | static int cx23885_set_freq_via_ops(struct cx23885_dev *dev, |
1552 | struct v4l2_frequency *f) | |
1553 | { | |
1554 | struct v4l2_control ctrl; | |
1555 | struct videobuf_dvb_frontend *vfe; | |
1556 | struct dvb_frontend *fe; | |
35045137 ST |
1557 | |
1558 | struct analog_parameters params = { | |
1559 | .mode = V4L2_TUNER_ANALOG_TV, | |
1560 | .audmode = V4L2_TUNER_MODE_STEREO, | |
1561 | .std = dev->tvnorm, | |
1562 | .frequency = f->frequency | |
1563 | }; | |
1564 | ||
1565 | mutex_lock(&dev->lock); | |
1566 | dev->freq = f->frequency; | |
1567 | ||
1568 | /* I need to mute audio here */ | |
1569 | ctrl.id = V4L2_CID_AUDIO_MUTE; | |
1570 | ctrl.value = 1; | |
1571 | cx23885_set_control(dev, &ctrl); | |
1572 | ||
1573 | /* If HVR1850 */ | |
1574 | dprintk(1, "%s() frequency=%d tuner=%d std=0x%llx\n", __func__, | |
1575 | params.frequency, f->tuner, params.std); | |
1576 | ||
1577 | vfe = videobuf_dvb_get_frontend(&dev->ts2.frontends, 1); | |
0cb64f02 DC |
1578 | if (!vfe) { |
1579 | mutex_unlock(&dev->lock); | |
1580 | return -EINVAL; | |
1581 | } | |
35045137 ST |
1582 | |
1583 | fe = vfe->dvb.frontend; | |
1584 | ||
0ac60acb DH |
1585 | if ((dev->board == CX23885_BOARD_HAUPPAUGE_HVR1850) || |
1586 | (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255) || | |
1587 | (dev->board == CX23885_BOARD_HAUPPAUGE_HVR1255_22111)) | |
35045137 ST |
1588 | fe = &dev->ts1.analog_fe; |
1589 | ||
1590 | if (fe && fe->ops.tuner_ops.set_analog_params) { | |
1591 | call_all(dev, core, s_std, dev->tvnorm); | |
1592 | fe->ops.tuner_ops.set_analog_params(fe, ¶ms); | |
1593 | } | |
1594 | else | |
1595 | printk(KERN_ERR "%s() No analog tuner, aborting\n", __func__); | |
1596 | ||
1597 | /* When changing channels it is required to reset TVAUDIO */ | |
1598 | msleep(100); | |
1599 | ||
1600 | /* I need to unmute audio here */ | |
1601 | ctrl.value = 0; | |
1602 | cx23885_set_control(dev, &ctrl); | |
1603 | ||
1604 | mutex_unlock(&dev->lock); | |
1605 | ||
1606 | return 0; | |
1607 | } | |
1608 | ||
1609 | int cx23885_set_frequency(struct file *file, void *priv, | |
1610 | struct v4l2_frequency *f) | |
e47f30b1 ST |
1611 | { |
1612 | struct cx23885_fh *fh = priv; | |
1613 | struct cx23885_dev *dev = fh->dev; | |
35045137 | 1614 | int ret; |
e47f30b1 | 1615 | |
35045137 | 1616 | switch (dev->board) { |
0ac60acb DH |
1617 | case CX23885_BOARD_HAUPPAUGE_HVR1255: |
1618 | case CX23885_BOARD_HAUPPAUGE_HVR1255_22111: | |
35045137 ST |
1619 | case CX23885_BOARD_HAUPPAUGE_HVR1850: |
1620 | ret = cx23885_set_freq_via_ops(dev, f); | |
1621 | break; | |
1622 | default: | |
1623 | ret = cx23885_set_freq(dev, f); | |
1624 | } | |
e47f30b1 | 1625 | |
35045137 ST |
1626 | return ret; |
1627 | } | |
1628 | ||
1629 | static int vidioc_s_frequency(struct file *file, void *priv, | |
1630 | struct v4l2_frequency *f) | |
1631 | { | |
1632 | return cx23885_set_frequency(file, priv, f); | |
e47f30b1 ST |
1633 | } |
1634 | ||
e47f30b1 ST |
1635 | /* ----------------------------------------------------------- */ |
1636 | ||
1637 | static void cx23885_vid_timeout(unsigned long data) | |
1638 | { | |
1639 | struct cx23885_dev *dev = (struct cx23885_dev *)data; | |
1640 | struct cx23885_dmaqueue *q = &dev->vidq; | |
1641 | struct cx23885_buffer *buf; | |
1642 | unsigned long flags; | |
1643 | ||
e47f30b1 ST |
1644 | spin_lock_irqsave(&dev->slock, flags); |
1645 | while (!list_empty(&q->active)) { | |
1646 | buf = list_entry(q->active.next, | |
1647 | struct cx23885_buffer, vb.queue); | |
1648 | list_del(&buf->vb.queue); | |
1649 | buf->vb.state = VIDEOBUF_ERROR; | |
1650 | wake_up(&buf->vb.done); | |
79776c89 | 1651 | printk(KERN_ERR "%s: [%p/%d] timeout - dma=0x%08lx\n", |
e47f30b1 ST |
1652 | dev->name, buf, buf->vb.i, |
1653 | (unsigned long)buf->risc.dma); | |
1654 | } | |
1655 | cx23885_restart_video_queue(dev, q); | |
1656 | spin_unlock_irqrestore(&dev->slock, flags); | |
1657 | } | |
1658 | ||
1659 | int cx23885_video_irq(struct cx23885_dev *dev, u32 status) | |
1660 | { | |
1661 | u32 mask, count; | |
1662 | int handled = 0; | |
1663 | ||
1664 | mask = cx_read(VID_A_INT_MSK); | |
1665 | if (0 == (status & mask)) | |
1666 | return handled; | |
79776c89 | 1667 | |
e47f30b1 ST |
1668 | cx_write(VID_A_INT_STAT, status); |
1669 | ||
19696f09 | 1670 | /* risc op code error, fifo overflow or line sync detection error */ |
79776c89 ST |
1671 | if ((status & VID_BC_MSK_OPC_ERR) || |
1672 | (status & VID_BC_MSK_SYNC) || | |
1673 | (status & VID_BC_MSK_OF)) { | |
1674 | ||
19696f09 | 1675 | if (status & VID_BC_MSK_OPC_ERR) { |
79776c89 ST |
1676 | dprintk(7, " (VID_BC_MSK_OPC_ERR 0x%08x)\n", |
1677 | VID_BC_MSK_OPC_ERR); | |
19696f09 ST |
1678 | printk(KERN_WARNING "%s: video risc op code error\n", |
1679 | dev->name); | |
1680 | cx23885_sram_channel_dump(dev, | |
1681 | &dev->sram_channels[SRAM_CH01]); | |
1682 | } | |
79776c89 ST |
1683 | |
1684 | if (status & VID_BC_MSK_SYNC) | |
19696f09 ST |
1685 | dprintk(7, " (VID_BC_MSK_SYNC 0x%08x) " |
1686 | "video lines miss-match\n", | |
79776c89 ST |
1687 | VID_BC_MSK_SYNC); |
1688 | ||
1689 | if (status & VID_BC_MSK_OF) | |
19696f09 | 1690 | dprintk(7, " (VID_BC_MSK_OF 0x%08x) fifo overflow\n", |
79776c89 ST |
1691 | VID_BC_MSK_OF); |
1692 | ||
e47f30b1 ST |
1693 | } |
1694 | ||
79776c89 ST |
1695 | /* Video */ |
1696 | if (status & VID_BC_MSK_RISCI1) { | |
e47f30b1 ST |
1697 | spin_lock(&dev->slock); |
1698 | count = cx_read(VID_A_GPCNT); | |
1699 | cx23885_video_wakeup(dev, &dev->vidq, count); | |
1700 | spin_unlock(&dev->slock); | |
1701 | handled++; | |
1702 | } | |
79776c89 | 1703 | if (status & VID_BC_MSK_RISCI2) { |
e47f30b1 ST |
1704 | dprintk(2, "stopper video\n"); |
1705 | spin_lock(&dev->slock); | |
1706 | cx23885_restart_video_queue(dev, &dev->vidq); | |
1707 | spin_unlock(&dev->slock); | |
1708 | handled++; | |
1709 | } | |
1710 | ||
79776c89 ST |
1711 | /* Allow the VBI framework to process it's payload */ |
1712 | handled += cx23885_vbi_irq(dev, status); | |
1713 | ||
e47f30b1 ST |
1714 | return handled; |
1715 | } | |
1716 | ||
e47f30b1 ST |
1717 | /* ----------------------------------------------------------- */ |
1718 | /* exported stuff */ | |
1719 | ||
bec43661 | 1720 | static const struct v4l2_file_operations video_fops = { |
e47f30b1 ST |
1721 | .owner = THIS_MODULE, |
1722 | .open = video_open, | |
1723 | .release = video_release, | |
1724 | .read = video_read, | |
1725 | .poll = video_poll, | |
1726 | .mmap = video_mmap, | |
1727 | .ioctl = video_ioctl2, | |
e47f30b1 ST |
1728 | }; |
1729 | ||
a399810c | 1730 | static const struct v4l2_ioctl_ops video_ioctl_ops = { |
e47f30b1 | 1731 | .vidioc_querycap = vidioc_querycap, |
78b526a4 HV |
1732 | .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap, |
1733 | .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap, | |
1734 | .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap, | |
1735 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, | |
1736 | .vidioc_g_fmt_vbi_cap = cx23885_vbi_fmt, | |
1737 | .vidioc_try_fmt_vbi_cap = cx23885_vbi_fmt, | |
1738 | .vidioc_s_fmt_vbi_cap = cx23885_vbi_fmt, | |
e47f30b1 ST |
1739 | .vidioc_reqbufs = vidioc_reqbufs, |
1740 | .vidioc_querybuf = vidioc_querybuf, | |
1741 | .vidioc_qbuf = vidioc_qbuf, | |
1742 | .vidioc_dqbuf = vidioc_dqbuf, | |
1743 | .vidioc_s_std = vidioc_s_std, | |
35045137 ST |
1744 | .vidioc_g_std = vidioc_g_std, |
1745 | .vidioc_querystd = vidioc_g_std, | |
e47f30b1 ST |
1746 | .vidioc_enum_input = vidioc_enum_input, |
1747 | .vidioc_g_input = vidioc_g_input, | |
1748 | .vidioc_s_input = vidioc_s_input, | |
e9e5cf47 | 1749 | .vidioc_log_status = vidioc_log_status, |
e47f30b1 ST |
1750 | .vidioc_queryctrl = vidioc_queryctrl, |
1751 | .vidioc_g_ctrl = vidioc_g_ctrl, | |
1752 | .vidioc_s_ctrl = vidioc_s_ctrl, | |
1753 | .vidioc_streamon = vidioc_streamon, | |
1754 | .vidioc_streamoff = vidioc_streamoff, | |
e47f30b1 ST |
1755 | .vidioc_g_tuner = vidioc_g_tuner, |
1756 | .vidioc_s_tuner = vidioc_s_tuner, | |
1757 | .vidioc_g_frequency = vidioc_g_frequency, | |
1758 | .vidioc_s_frequency = vidioc_s_frequency, | |
74618244 | 1759 | .vidioc_g_chip_ident = cx23885_g_chip_ident, |
e47f30b1 | 1760 | #ifdef CONFIG_VIDEO_ADV_DEBUG |
74618244 AW |
1761 | .vidioc_g_register = cx23885_g_register, |
1762 | .vidioc_s_register = cx23885_s_register, | |
e47f30b1 | 1763 | #endif |
fc1a889d ST |
1764 | .vidioc_enumaudio = vidioc_enum_audinput, |
1765 | .vidioc_g_audio = vidioc_g_audinput, | |
1766 | .vidioc_s_audio = vidioc_s_audinput, | |
a399810c HV |
1767 | }; |
1768 | ||
1769 | static struct video_device cx23885_vbi_template; | |
1770 | static struct video_device cx23885_video_template = { | |
1771 | .name = "cx23885-video", | |
a399810c | 1772 | .fops = &video_fops, |
a399810c | 1773 | .ioctl_ops = &video_ioctl_ops, |
e47f30b1 ST |
1774 | .tvnorms = CX23885_NORMS, |
1775 | .current_norm = V4L2_STD_NTSC_M, | |
1776 | }; | |
1777 | ||
bec43661 | 1778 | static const struct v4l2_file_operations radio_fops = { |
e47f30b1 ST |
1779 | .owner = THIS_MODULE, |
1780 | .open = video_open, | |
1781 | .release = video_release, | |
1782 | .ioctl = video_ioctl2, | |
e47f30b1 ST |
1783 | }; |
1784 | ||
1785 | ||
1786 | void cx23885_video_unregister(struct cx23885_dev *dev) | |
1787 | { | |
22b4e64f | 1788 | dprintk(1, "%s()\n", __func__); |
dbe83a3b | 1789 | cx23885_irq_remove(dev, 0x01); |
e47f30b1 | 1790 | |
79776c89 ST |
1791 | if (dev->vbi_dev) { |
1792 | if (video_is_registered(dev->vbi_dev)) | |
1793 | video_unregister_device(dev->vbi_dev); | |
1794 | else | |
1795 | video_device_release(dev->vbi_dev); | |
1796 | dev->vbi_dev = NULL; | |
1797 | btcx_riscmem_free(dev->pci, &dev->vbiq.stopper); | |
1798 | } | |
e47f30b1 | 1799 | if (dev->video_dev) { |
f0813b4c | 1800 | if (video_is_registered(dev->video_dev)) |
e47f30b1 ST |
1801 | video_unregister_device(dev->video_dev); |
1802 | else | |
1803 | video_device_release(dev->video_dev); | |
1804 | dev->video_dev = NULL; | |
1805 | ||
1806 | btcx_riscmem_free(dev->pci, &dev->vidq.stopper); | |
1807 | } | |
97ce5670 MM |
1808 | |
1809 | if (dev->audio_dev) | |
efa762f5 | 1810 | cx23885_audio_unregister(dev); |
e47f30b1 ST |
1811 | } |
1812 | ||
1813 | int cx23885_video_register(struct cx23885_dev *dev) | |
1814 | { | |
1815 | int err; | |
1816 | ||
22b4e64f | 1817 | dprintk(1, "%s()\n", __func__); |
e47f30b1 ST |
1818 | spin_lock_init(&dev->slock); |
1819 | ||
1820 | /* Initialize VBI template */ | |
1821 | memcpy(&cx23885_vbi_template, &cx23885_video_template, | |
1822 | sizeof(cx23885_vbi_template)); | |
1823 | strcpy(cx23885_vbi_template.name, "cx23885-vbi"); | |
e47f30b1 ST |
1824 | |
1825 | dev->tvnorm = cx23885_video_template.current_norm; | |
1826 | ||
1827 | /* init video dma queues */ | |
1828 | INIT_LIST_HEAD(&dev->vidq.active); | |
1829 | INIT_LIST_HEAD(&dev->vidq.queued); | |
1830 | dev->vidq.timeout.function = cx23885_vid_timeout; | |
1831 | dev->vidq.timeout.data = (unsigned long)dev; | |
1832 | init_timer(&dev->vidq.timeout); | |
1833 | cx23885_risc_stopper(dev->pci, &dev->vidq.stopper, | |
1834 | VID_A_DMA_CTL, 0x11, 0x00); | |
1835 | ||
79776c89 ST |
1836 | /* init vbi dma queues */ |
1837 | INIT_LIST_HEAD(&dev->vbiq.active); | |
1838 | INIT_LIST_HEAD(&dev->vbiq.queued); | |
1839 | dev->vbiq.timeout.function = cx23885_vbi_timeout; | |
1840 | dev->vbiq.timeout.data = (unsigned long)dev; | |
1841 | init_timer(&dev->vbiq.timeout); | |
1842 | cx23885_risc_stopper(dev->pci, &dev->vbiq.stopper, | |
1843 | VID_A_DMA_CTL, 0x22, 0x00); | |
dbe83a3b AW |
1844 | |
1845 | cx23885_irq_add_enable(dev, 0x01); | |
e47f30b1 | 1846 | |
557f48d5 IL |
1847 | if ((TUNER_ABSENT != dev->tuner_type) && |
1848 | ((dev->tuner_bus == 0) || (dev->tuner_bus == 1))) { | |
0d5a19f1 HV |
1849 | struct v4l2_subdev *sd = NULL; |
1850 | ||
1851 | if (dev->tuner_addr) | |
e6574f2f | 1852 | sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, |
557f48d5 | 1853 | &dev->i2c_bus[dev->tuner_bus].i2c_adap, |
9a1f8b34 | 1854 | "tuner", dev->tuner_addr, NULL); |
0d5a19f1 | 1855 | else |
53dacb15 | 1856 | sd = v4l2_i2c_new_subdev(&dev->v4l2_dev, |
557f48d5 | 1857 | &dev->i2c_bus[dev->tuner_bus].i2c_adap, |
1532a070 | 1858 | "tuner", 0, v4l2_i2c_tuner_addrs(ADDRS_TV)); |
0d5a19f1 HV |
1859 | if (sd) { |
1860 | struct tuner_setup tun_setup; | |
1861 | ||
fd705e7c | 1862 | memset(&tun_setup, 0, sizeof(tun_setup)); |
0d5a19f1 HV |
1863 | tun_setup.mode_mask = T_ANALOG_TV; |
1864 | tun_setup.type = dev->tuner_type; | |
1865 | tun_setup.addr = v4l2_i2c_subdev_addr(sd); | |
6f0d8c02 | 1866 | tun_setup.tuner_callback = cx23885_tuner_callback; |
0d5a19f1 HV |
1867 | |
1868 | v4l2_subdev_call(sd, tuner, s_type_addr, &tun_setup); | |
0b32d65c KK |
1869 | |
1870 | if (dev->board == CX23885_BOARD_LEADTEK_WINFAST_PXTV1200) { | |
1871 | struct xc2028_ctrl ctrl = { | |
1872 | .fname = XC2028_DEFAULT_FIRMWARE, | |
1873 | .max_len = 64 | |
1874 | }; | |
1875 | struct v4l2_priv_tun_config cfg = { | |
1876 | .tuner = dev->tuner_type, | |
1877 | .priv = &ctrl | |
1878 | }; | |
1879 | v4l2_subdev_call(sd, tuner, s_config, &cfg); | |
1880 | } | |
0d5a19f1 HV |
1881 | } |
1882 | } | |
1883 | ||
79776c89 | 1884 | /* register Video device */ |
e47f30b1 ST |
1885 | dev->video_dev = cx23885_vdev_init(dev, dev->pci, |
1886 | &cx23885_video_template, "video"); | |
1887 | err = video_register_device(dev->video_dev, VFL_TYPE_GRABBER, | |
1888 | video_nr[dev->nr]); | |
1889 | if (err < 0) { | |
1890 | printk(KERN_INFO "%s: can't register video device\n", | |
1891 | dev->name); | |
1892 | goto fail_unreg; | |
1893 | } | |
f88fb8e9 | 1894 | printk(KERN_INFO "%s: registered device %s [v4l2]\n", |
38c7c036 | 1895 | dev->name, video_device_node_name(dev->video_dev)); |
97ce5670 | 1896 | |
f88fb8e9 ST |
1897 | /* register VBI device */ |
1898 | dev->vbi_dev = cx23885_vdev_init(dev, dev->pci, | |
1899 | &cx23885_vbi_template, "vbi"); | |
1900 | err = video_register_device(dev->vbi_dev, VFL_TYPE_VBI, | |
1901 | vbi_nr[dev->nr]); | |
1902 | if (err < 0) { | |
1903 | printk(KERN_INFO "%s: can't register vbi device\n", | |
1904 | dev->name); | |
1905 | goto fail_unreg; | |
1906 | } | |
1907 | printk(KERN_INFO "%s: registered device %s\n", | |
1908 | dev->name, video_device_node_name(dev->vbi_dev)); | |
1909 | ||
97ce5670 | 1910 | /* Register ALSA audio device */ |
efa762f5 | 1911 | dev->audio_dev = cx23885_audio_register(dev); |
97ce5670 | 1912 | |
e47f30b1 ST |
1913 | /* initial device configuration */ |
1914 | mutex_lock(&dev->lock); | |
1915 | cx23885_set_tvnorm(dev, dev->tvnorm); | |
1916 | init_controls(dev); | |
1917 | cx23885_video_mux(dev, 0); | |
fa1e0fd3 | 1918 | cx23885_audio_mux(dev, 0); |
e47f30b1 ST |
1919 | mutex_unlock(&dev->lock); |
1920 | ||
e47f30b1 ST |
1921 | return 0; |
1922 | ||
1923 | fail_unreg: | |
1924 | cx23885_video_unregister(dev); | |
1925 | return err; | |
1926 | } | |
1927 |