Merge drm-fixes into drm-next.
[deliverable/linux.git] / drivers / media / usb / cx231xx / cx231xx-video.c
CommitLineData
e0d3bafd 1/*
cde4362f
MCC
2 cx231xx-video.c - driver for Conexant Cx23100/101/102
3 USB video capture devices
e0d3bafd
SD
4
5 Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
84b5dbf3
MCC
6 Based on em28xx driver
7 Based on cx23885 driver
8 Based on cx88 driver
e0d3bafd
SD
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24
589dadf2 25#include "cx231xx.h"
e0d3bafd
SD
26#include <linux/init.h>
27#include <linux/list.h>
28#include <linux/module.h>
29#include <linux/kernel.h>
30#include <linux/bitmap.h>
e0d3bafd 31#include <linux/i2c.h>
e0d3bafd
SD
32#include <linux/mm.h>
33#include <linux/mutex.h>
5a0e3ad6 34#include <linux/slab.h>
e0d3bafd
SD
35
36#include <media/v4l2-common.h>
37#include <media/v4l2-ioctl.h>
1d08a4fa 38#include <media/v4l2-event.h>
d647f0b7 39#include <media/drv-intf/msp3400.h>
e0d3bafd
SD
40#include <media/tuner.h>
41
42#include "dvb_frontend.h"
43
e0d3bafd
SD
44#include "cx231xx-vbi.h"
45
3d28cf3e 46#define CX231XX_VERSION "0.0.3"
818fdf34 47
e0d3bafd
SD
48#define DRIVER_AUTHOR "Srinivasa Deevi <srinivasa.deevi@conexant.com>"
49#define DRIVER_DESC "Conexant cx231xx based USB video device driver"
50
e0d3bafd
SD
51#define cx231xx_videodbg(fmt, arg...) do {\
52 if (video_debug) \
53 printk(KERN_INFO "%s %s :"fmt, \
54 dev->name, __func__ , ##arg); } while (0)
55
56static unsigned int isoc_debug;
57module_param(isoc_debug, int, 0644);
58MODULE_PARM_DESC(isoc_debug, "enable debug messages [isoc transfers]");
59
60#define cx231xx_isocdbg(fmt, arg...) \
61do {\
62 if (isoc_debug) { \
63 printk(KERN_INFO "%s %s :"fmt, \
64 dev->name, __func__ , ##arg); \
65 } \
66 } while (0)
67
68MODULE_AUTHOR(DRIVER_AUTHOR);
69MODULE_DESCRIPTION(DRIVER_DESC);
70MODULE_LICENSE("GPL");
1990d50b 71MODULE_VERSION(CX231XX_VERSION);
e0d3bafd 72
e0d3bafd
SD
73static unsigned int card[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
74static unsigned int video_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
75static unsigned int vbi_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
76static unsigned int radio_nr[] = {[0 ... (CX231XX_MAXBOARDS - 1)] = UNSET };
77
84b5dbf3 78module_param_array(card, int, NULL, 0444);
e0d3bafd
SD
79module_param_array(video_nr, int, NULL, 0444);
80module_param_array(vbi_nr, int, NULL, 0444);
81module_param_array(radio_nr, int, NULL, 0444);
82
84b5dbf3 83MODULE_PARM_DESC(card, "card type");
e0d3bafd 84MODULE_PARM_DESC(video_nr, "video device numbers");
84b5dbf3 85MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
e0d3bafd
SD
86MODULE_PARM_DESC(radio_nr, "radio device numbers");
87
88static unsigned int video_debug;
89module_param(video_debug, int, 0644);
90MODULE_PARM_DESC(video_debug, "enable debug messages [video]");
91
e0d3bafd
SD
92/* supported video standards */
93static struct cx231xx_fmt format[] = {
94 {
84b5dbf3
MCC
95 .name = "16bpp YUY2, 4:2:2, packed",
96 .fourcc = V4L2_PIX_FMT_YUYV,
97 .depth = 16,
98 .reg = 0,
99 },
e0d3bafd
SD
100};
101
e0d3bafd 102
3d263114
MCC
103static int cx231xx_enable_analog_tuner(struct cx231xx *dev)
104{
105#ifdef CONFIG_MEDIA_CONTROLLER
106 struct media_device *mdev = dev->media_dev;
107 struct media_entity *entity, *decoder = NULL, *source;
108 struct media_link *link, *found_link = NULL;
57208e5e 109 int ret, active_links = 0;
3d263114
MCC
110
111 if (!mdev)
112 return 0;
113
114 /*
115 * This will find the tuner that is connected into the decoder.
116 * Technically, this is not 100% correct, as the device may be
117 * using an analog input instead of the tuner. However, as we can't
118 * do DVB streaming while the DMA engine is being used for V4L2,
119 * this should be enough for the actual needs.
120 */
121 media_device_for_each_entity(entity, mdev) {
4ca72efa 122 if (entity->function == MEDIA_ENT_F_ATV_DECODER) {
3d263114
MCC
123 decoder = entity;
124 break;
125 }
126 }
127 if (!decoder)
128 return 0;
129
57208e5e 130 list_for_each_entry(link, &decoder->links, list) {
3d263114
MCC
131 if (link->sink->entity == decoder) {
132 found_link = link;
133 if (link->flags & MEDIA_LNK_FL_ENABLED)
134 active_links++;
135 break;
136 }
137 }
138
139 if (active_links == 1 || !found_link)
140 return 0;
141
142 source = found_link->source->entity;
57208e5e 143 list_for_each_entry(link, &source->links, list) {
3d263114
MCC
144 struct media_entity *sink;
145 int flags = 0;
146
3d263114
MCC
147 sink = link->sink->entity;
148
149 if (sink == entity)
150 flags = MEDIA_LNK_FL_ENABLED;
151
152 ret = media_entity_setup_link(link, flags);
153 if (ret) {
154 dev_err(dev->dev,
155 "Couldn't change link %s->%s to %s. Error %d\n",
156 source->name, sink->name,
157 flags ? "enabled" : "disabled",
158 ret);
159 return ret;
160 } else
161 dev_dbg(dev->dev,
162 "link %s->%s was %s\n",
163 source->name, sink->name,
164 flags ? "ENABLED" : "disabled");
165 }
166#endif
167 return 0;
168}
169
e0d3bafd
SD
170/* ------------------------------------------------------------------
171 Video buffer and parser functions
172 ------------------------------------------------------------------*/
173
174/*
175 * Announces that a buffer were filled and request the next
176 */
177static inline void buffer_filled(struct cx231xx *dev,
84b5dbf3
MCC
178 struct cx231xx_dmaqueue *dma_q,
179 struct cx231xx_buffer *buf)
e0d3bafd
SD
180{
181 /* Advice that buffer was filled */
182 cx231xx_isocdbg("[%p/%d] wakeup\n", buf, buf->vb.i);
183 buf->vb.state = VIDEOBUF_DONE;
184 buf->vb.field_count++;
8e6057b5 185 v4l2_get_timestamp(&buf->vb.ts);
e0d3bafd 186
64fbf444
PB
187 if (dev->USE_ISO)
188 dev->video_mode.isoc_ctl.buf = NULL;
189 else
190 dev->video_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
191
192 list_del(&buf->vb.queue);
193 wake_up(&buf->vb.done);
194}
195
84b5dbf3 196static inline void print_err_status(struct cx231xx *dev, int packet, int status)
e0d3bafd
SD
197{
198 char *errmsg = "Unknown";
199
200 switch (status) {
201 case -ENOENT:
202 errmsg = "unlinked synchronuously";
203 break;
204 case -ECONNRESET:
205 errmsg = "unlinked asynchronuously";
206 break;
207 case -ENOSR:
208 errmsg = "Buffer error (overrun)";
209 break;
210 case -EPIPE:
211 errmsg = "Stalled (device not responding)";
212 break;
213 case -EOVERFLOW:
214 errmsg = "Babble (bad cable?)";
215 break;
216 case -EPROTO:
217 errmsg = "Bit-stuff error (bad cable?)";
218 break;
219 case -EILSEQ:
220 errmsg = "CRC/Timeout (could be anything)";
221 break;
222 case -ETIME:
223 errmsg = "Device does not respond";
224 break;
225 }
226 if (packet < 0) {
84b5dbf3 227 cx231xx_isocdbg("URB status %d [%s].\n", status, errmsg);
e0d3bafd
SD
228 } else {
229 cx231xx_isocdbg("URB packet %d, status %d [%s].\n",
84b5dbf3 230 packet, status, errmsg);
e0d3bafd
SD
231 }
232}
233
234/*
235 * video-buf generic routine to get the next available buffer
236 */
237static inline void get_next_buf(struct cx231xx_dmaqueue *dma_q,
84b5dbf3 238 struct cx231xx_buffer **buf)
e0d3bafd 239{
84b5dbf3
MCC
240 struct cx231xx_video_mode *vmode =
241 container_of(dma_q, struct cx231xx_video_mode, vidq);
242 struct cx231xx *dev = container_of(vmode, struct cx231xx, video_mode);
e0d3bafd
SD
243
244 char *outp;
245
e0d3bafd
SD
246 if (list_empty(&dma_q->active)) {
247 cx231xx_isocdbg("No active queue to serve\n");
64fbf444
PB
248 if (dev->USE_ISO)
249 dev->video_mode.isoc_ctl.buf = NULL;
250 else
251 dev->video_mode.bulk_ctl.buf = NULL;
e0d3bafd
SD
252 *buf = NULL;
253 return;
254 }
255
256 /* Get the next buffer */
257 *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
258
25985edc 259 /* Cleans up buffer - Useful for testing for frame/URB loss */
e0d3bafd
SD
260 outp = videobuf_to_vmalloc(&(*buf)->vb);
261 memset(outp, 0, (*buf)->vb.size);
262
64fbf444
PB
263 if (dev->USE_ISO)
264 dev->video_mode.isoc_ctl.buf = *buf;
265 else
266 dev->video_mode.bulk_ctl.buf = *buf;
e0d3bafd
SD
267
268 return;
269}
270
271/*
272 * Controls the isoc copy of each urb packet
273 */
274static inline int cx231xx_isoc_copy(struct cx231xx *dev, struct urb *urb)
275{
84b5dbf3 276 struct cx231xx_dmaqueue *dma_q = urb->context;
4df16f70 277 int i;
e0d3bafd 278 unsigned char *p_buffer;
84b5dbf3
MCC
279 u32 bytes_parsed = 0, buffer_size = 0;
280 u8 sav_eav = 0;
e0d3bafd
SD
281
282 if (!dev)
283 return 0;
284
990862a2 285 if (dev->state & DEV_DISCONNECTED)
e0d3bafd
SD
286 return 0;
287
288 if (urb->status < 0) {
289 print_err_status(dev, -1, urb->status);
290 if (urb->status == -ENOENT)
291 return 0;
292 }
293
e0d3bafd
SD
294 for (i = 0; i < urb->number_of_packets; i++) {
295 int status = urb->iso_frame_desc[i].status;
296
297 if (status < 0) {
298 print_err_status(dev, i, status);
299 if (urb->iso_frame_desc[i].status != -EPROTO)
300 continue;
301 }
302
84b5dbf3 303 if (urb->iso_frame_desc[i].actual_length <= 0) {
e0d3bafd
SD
304 /* cx231xx_isocdbg("packet %d is empty",i); - spammy */
305 continue;
306 }
307 if (urb->iso_frame_desc[i].actual_length >
84b5dbf3 308 dev->video_mode.max_pkt_size) {
e0d3bafd
SD
309 cx231xx_isocdbg("packet bigger than packet size");
310 continue;
311 }
312
84b5dbf3 313 /* get buffer pointer and length */
e0d3bafd 314 p_buffer = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
84b5dbf3
MCC
315 buffer_size = urb->iso_frame_desc[i].actual_length;
316 bytes_parsed = 0;
317
318 if (dma_q->is_partial_line) {
b9255176 319 /* Handle the case of a partial line */
84b5dbf3
MCC
320 sav_eav = dma_q->last_sav;
321 } else {
b9255176
SD
322 /* Check for a SAV/EAV overlapping
323 the buffer boundary */
84b5dbf3
MCC
324 sav_eav =
325 cx231xx_find_boundary_SAV_EAV(p_buffer,
326 dma_q->partial_buf,
327 &bytes_parsed);
328 }
e0d3bafd 329
84b5dbf3 330 sav_eav &= 0xF0;
cde4362f
MCC
331 /* Get the first line if we have some portion of an SAV/EAV from
332 the last buffer or a partial line */
84b5dbf3 333 if (sav_eav) {
cde4362f 334 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
b9255176
SD
335 sav_eav, /* SAV/EAV */
336 p_buffer + bytes_parsed, /* p_buffer */
337 buffer_size - bytes_parsed);/* buf size */
84b5dbf3 338 }
e0d3bafd 339
84b5dbf3
MCC
340 /* Now parse data that is completely in this buffer */
341 /* dma_q->is_partial_line = 0; */
e0d3bafd 342
84b5dbf3
MCC
343 while (bytes_parsed < buffer_size) {
344 u32 bytes_used = 0;
e0d3bafd 345
b9255176
SD
346 sav_eav = cx231xx_find_next_SAV_EAV(
347 p_buffer + bytes_parsed, /* p_buffer */
348 buffer_size - bytes_parsed, /* buf size */
349 &bytes_used);/* bytes used to get SAV/EAV */
e0d3bafd 350
84b5dbf3 351 bytes_parsed += bytes_used;
e0d3bafd 352
84b5dbf3
MCC
353 sav_eav &= 0xF0;
354 if (sav_eav && (bytes_parsed < buffer_size)) {
cde4362f 355 bytes_parsed += cx231xx_get_video_line(dev,
b9255176
SD
356 dma_q, sav_eav, /* SAV/EAV */
357 p_buffer + bytes_parsed,/* p_buffer */
358 buffer_size - bytes_parsed);/*buf size*/
84b5dbf3
MCC
359 }
360 }
e0d3bafd 361
cde4362f
MCC
362 /* Save the last four bytes of the buffer so we can check the
363 buffer boundary condition next time */
84b5dbf3
MCC
364 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
365 bytes_parsed = 0;
e0d3bafd 366
84b5dbf3 367 }
4df16f70 368 return 1;
e0d3bafd
SD
369}
370
64fbf444
PB
371static inline int cx231xx_bulk_copy(struct cx231xx *dev, struct urb *urb)
372{
64fbf444 373 struct cx231xx_dmaqueue *dma_q = urb->context;
64fbf444
PB
374 unsigned char *p_buffer;
375 u32 bytes_parsed = 0, buffer_size = 0;
376 u8 sav_eav = 0;
377
378 if (!dev)
379 return 0;
380
990862a2 381 if (dev->state & DEV_DISCONNECTED)
64fbf444
PB
382 return 0;
383
384 if (urb->status < 0) {
385 print_err_status(dev, -1, urb->status);
386 if (urb->status == -ENOENT)
387 return 0;
388 }
389
64fbf444
PB
390 if (1) {
391
392 /* get buffer pointer and length */
393 p_buffer = urb->transfer_buffer;
394 buffer_size = urb->actual_length;
395 bytes_parsed = 0;
396
397 if (dma_q->is_partial_line) {
398 /* Handle the case of a partial line */
399 sav_eav = dma_q->last_sav;
400 } else {
401 /* Check for a SAV/EAV overlapping
402 the buffer boundary */
403 sav_eav =
404 cx231xx_find_boundary_SAV_EAV(p_buffer,
405 dma_q->partial_buf,
406 &bytes_parsed);
407 }
408
409 sav_eav &= 0xF0;
410 /* Get the first line if we have some portion of an SAV/EAV from
411 the last buffer or a partial line */
412 if (sav_eav) {
413 bytes_parsed += cx231xx_get_video_line(dev, dma_q,
414 sav_eav, /* SAV/EAV */
415 p_buffer + bytes_parsed, /* p_buffer */
416 buffer_size - bytes_parsed);/* buf size */
417 }
418
419 /* Now parse data that is completely in this buffer */
420 /* dma_q->is_partial_line = 0; */
421
422 while (bytes_parsed < buffer_size) {
423 u32 bytes_used = 0;
424
425 sav_eav = cx231xx_find_next_SAV_EAV(
426 p_buffer + bytes_parsed, /* p_buffer */
427 buffer_size - bytes_parsed, /* buf size */
428 &bytes_used);/* bytes used to get SAV/EAV */
429
430 bytes_parsed += bytes_used;
431
432 sav_eav &= 0xF0;
433 if (sav_eav && (bytes_parsed < buffer_size)) {
434 bytes_parsed += cx231xx_get_video_line(dev,
435 dma_q, sav_eav, /* SAV/EAV */
436 p_buffer + bytes_parsed,/* p_buffer */
437 buffer_size - bytes_parsed);/*buf size*/
438 }
439 }
440
441 /* Save the last four bytes of the buffer so we can check the
442 buffer boundary condition next time */
443 memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
444 bytes_parsed = 0;
445
446 }
4df16f70 447 return 1;
64fbf444
PB
448}
449
450
cde4362f
MCC
451u8 cx231xx_find_boundary_SAV_EAV(u8 *p_buffer, u8 *partial_buf,
452 u32 *p_bytes_used)
e0d3bafd 453{
84b5dbf3
MCC
454 u32 bytes_used;
455 u8 boundary_bytes[8];
456 u8 sav_eav = 0;
457
458 *p_bytes_used = 0;
e0d3bafd 459
84b5dbf3
MCC
460 /* Create an array of the last 4 bytes of the last buffer and the first
461 4 bytes of the current buffer. */
e0d3bafd 462
84b5dbf3
MCC
463 memcpy(boundary_bytes, partial_buf, 4);
464 memcpy(boundary_bytes + 4, p_buffer, 4);
e0d3bafd 465
84b5dbf3 466 /* Check for the SAV/EAV in the boundary buffer */
cde4362f
MCC
467 sav_eav = cx231xx_find_next_SAV_EAV((u8 *)&boundary_bytes, 8,
468 &bytes_used);
e0d3bafd 469
84b5dbf3
MCC
470 if (sav_eav) {
471 /* found a boundary SAV/EAV. Updates the bytes used to reflect
472 only those used in the new buffer */
473 *p_bytes_used = bytes_used - 4;
474 }
e0d3bafd 475
84b5dbf3 476 return sav_eav;
e0d3bafd
SD
477}
478
cde4362f 479u8 cx231xx_find_next_SAV_EAV(u8 *p_buffer, u32 buffer_size, u32 *p_bytes_used)
84b5dbf3
MCC
480{
481 u32 i;
482 u8 sav_eav = 0;
e0d3bafd 483
cde4362f
MCC
484 /*
485 * Don't search if the buffer size is less than 4. It causes a page
486 * fault since buffer_size - 4 evaluates to a large number in that
487 * case.
488 */
84b5dbf3
MCC
489 if (buffer_size < 4) {
490 *p_bytes_used = buffer_size;
491 return 0;
492 }
e0d3bafd 493
84b5dbf3 494 for (i = 0; i < (buffer_size - 3); i++) {
e0d3bafd 495
84b5dbf3
MCC
496 if ((p_buffer[i] == 0xFF) &&
497 (p_buffer[i + 1] == 0x00) && (p_buffer[i + 2] == 0x00)) {
e0d3bafd 498
84b5dbf3
MCC
499 *p_bytes_used = i + 4;
500 sav_eav = p_buffer[i + 3];
501 return sav_eav;
502 }
503 }
e0d3bafd 504
84b5dbf3
MCC
505 *p_bytes_used = buffer_size;
506 return 0;
507}
e0d3bafd 508
cde4362f
MCC
509u32 cx231xx_get_video_line(struct cx231xx *dev,
510 struct cx231xx_dmaqueue *dma_q, u8 sav_eav,
511 u8 *p_buffer, u32 buffer_size)
84b5dbf3
MCC
512{
513 u32 bytes_copied = 0;
514 int current_field = -1;
515
516 switch (sav_eav) {
517 case SAV_ACTIVE_VIDEO_FIELD1:
cde4362f
MCC
518 /* looking for skipped line which occurred in PAL 720x480 mode.
519 In this case, there will be no active data contained
520 between the SAV and EAV */
521 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
522 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
523 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
524 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
525 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
526 (p_buffer[3] == EAV_VBLANK_FIELD2)))
84b5dbf3 527 return bytes_copied;
84b5dbf3
MCC
528 current_field = 1;
529 break;
e0d3bafd 530
84b5dbf3 531 case SAV_ACTIVE_VIDEO_FIELD2:
cde4362f
MCC
532 /* looking for skipped line which occurred in PAL 720x480 mode.
533 In this case, there will be no active data contained between
534 the SAV and EAV */
535 if ((buffer_size > 3) && (p_buffer[0] == 0xFF) &&
536 (p_buffer[1] == 0x00) && (p_buffer[2] == 0x00) &&
537 ((p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD1) ||
538 (p_buffer[3] == EAV_ACTIVE_VIDEO_FIELD2) ||
539 (p_buffer[3] == EAV_VBLANK_FIELD1) ||
540 (p_buffer[3] == EAV_VBLANK_FIELD2)))
84b5dbf3 541 return bytes_copied;
84b5dbf3
MCC
542 current_field = 2;
543 break;
544 }
e0d3bafd 545
84b5dbf3 546 dma_q->last_sav = sav_eav;
e0d3bafd 547
cde4362f
MCC
548 bytes_copied = cx231xx_copy_video_line(dev, dma_q, p_buffer,
549 buffer_size, current_field);
84b5dbf3
MCC
550
551 return bytes_copied;
e0d3bafd
SD
552}
553
cde4362f
MCC
554u32 cx231xx_copy_video_line(struct cx231xx *dev,
555 struct cx231xx_dmaqueue *dma_q, u8 *p_line,
84b5dbf3 556 u32 length, int field_number)
e0d3bafd 557{
84b5dbf3
MCC
558 u32 bytes_to_copy;
559 struct cx231xx_buffer *buf;
560 u32 _line_size = dev->width * 2;
e0d3bafd 561
cde4362f 562 if (dma_q->current_field != field_number)
84b5dbf3 563 cx231xx_reset_video_buffer(dev, dma_q);
e0d3bafd 564
84b5dbf3 565 /* get the buffer pointer */
64fbf444
PB
566 if (dev->USE_ISO)
567 buf = dev->video_mode.isoc_ctl.buf;
568 else
569 buf = dev->video_mode.bulk_ctl.buf;
e0d3bafd 570
84b5dbf3
MCC
571 /* Remember the field number for next time */
572 dma_q->current_field = field_number;
e0d3bafd 573
84b5dbf3
MCC
574 bytes_to_copy = dma_q->bytes_left_in_line;
575 if (bytes_to_copy > length)
576 bytes_to_copy = length;
e0d3bafd 577
84b5dbf3
MCC
578 if (dma_q->lines_completed >= dma_q->lines_per_field) {
579 dma_q->bytes_left_in_line -= bytes_to_copy;
cde4362f
MCC
580 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0) ?
581 0 : 1;
84b5dbf3
MCC
582 return 0;
583 }
e0d3bafd 584
84b5dbf3 585 dma_q->is_partial_line = 1;
e0d3bafd 586
84b5dbf3
MCC
587 /* If we don't have a buffer, just return the number of bytes we would
588 have copied if we had a buffer. */
589 if (!buf) {
590 dma_q->bytes_left_in_line -= bytes_to_copy;
cde4362f
MCC
591 dma_q->is_partial_line = (dma_q->bytes_left_in_line == 0)
592 ? 0 : 1;
84b5dbf3
MCC
593 return bytes_to_copy;
594 }
e0d3bafd 595
84b5dbf3
MCC
596 /* copy the data to video buffer */
597 cx231xx_do_copy(dev, dma_q, p_line, bytes_to_copy);
e0d3bafd 598
84b5dbf3
MCC
599 dma_q->pos += bytes_to_copy;
600 dma_q->bytes_left_in_line -= bytes_to_copy;
e0d3bafd 601
84b5dbf3 602 if (dma_q->bytes_left_in_line == 0) {
84b5dbf3
MCC
603 dma_q->bytes_left_in_line = _line_size;
604 dma_q->lines_completed++;
605 dma_q->is_partial_line = 0;
e0d3bafd 606
84b5dbf3 607 if (cx231xx_is_buffer_done(dev, dma_q) && buf) {
84b5dbf3 608 buffer_filled(dev, dma_q, buf);
e0d3bafd 609
84b5dbf3
MCC
610 dma_q->pos = 0;
611 buf = NULL;
612 dma_q->lines_completed = 0;
613 }
614 }
e0d3bafd 615
84b5dbf3 616 return bytes_to_copy;
e0d3bafd
SD
617}
618
84b5dbf3
MCC
619void cx231xx_reset_video_buffer(struct cx231xx *dev,
620 struct cx231xx_dmaqueue *dma_q)
e0d3bafd 621{
84b5dbf3 622 struct cx231xx_buffer *buf;
e0d3bafd 623
84b5dbf3
MCC
624 /* handle the switch from field 1 to field 2 */
625 if (dma_q->current_field == 1) {
cde4362f 626 if (dma_q->lines_completed >= dma_q->lines_per_field)
84b5dbf3 627 dma_q->field1_done = 1;
cde4362f 628 else
84b5dbf3 629 dma_q->field1_done = 0;
84b5dbf3 630 }
e0d3bafd 631
64fbf444
PB
632 if (dev->USE_ISO)
633 buf = dev->video_mode.isoc_ctl.buf;
634 else
635 buf = dev->video_mode.bulk_ctl.buf;
e0d3bafd 636
84b5dbf3 637 if (buf == NULL) {
84b5dbf3
MCC
638 /* first try to get the buffer */
639 get_next_buf(dma_q, &buf);
e0d3bafd 640
84b5dbf3
MCC
641 dma_q->pos = 0;
642 dma_q->field1_done = 0;
643 dma_q->current_field = -1;
644 }
e0d3bafd 645
84b5dbf3
MCC
646 /* reset the counters */
647 dma_q->bytes_left_in_line = dev->width << 1;
648 dma_q->lines_completed = 0;
e0d3bafd
SD
649}
650
651int cx231xx_do_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
cde4362f 652 u8 *p_buffer, u32 bytes_to_copy)
e0d3bafd 653{
84b5dbf3
MCC
654 u8 *p_out_buffer = NULL;
655 u32 current_line_bytes_copied = 0;
656 struct cx231xx_buffer *buf;
657 u32 _line_size = dev->width << 1;
658 void *startwrite;
659 int offset, lencopy;
e0d3bafd 660
64fbf444
PB
661 if (dev->USE_ISO)
662 buf = dev->video_mode.isoc_ctl.buf;
663 else
664 buf = dev->video_mode.bulk_ctl.buf;
e0d3bafd 665
84b5dbf3
MCC
666 if (buf == NULL)
667 return -1;
e0d3bafd
SD
668
669 p_out_buffer = videobuf_to_vmalloc(&buf->vb);
670
84b5dbf3 671 current_line_bytes_copied = _line_size - dma_q->bytes_left_in_line;
e0d3bafd 672
84b5dbf3
MCC
673 /* Offset field 2 one line from the top of the buffer */
674 offset = (dma_q->current_field == 1) ? 0 : _line_size;
e0d3bafd 675
84b5dbf3
MCC
676 /* Offset for field 2 */
677 startwrite = p_out_buffer + offset;
e0d3bafd 678
84b5dbf3
MCC
679 /* lines already completed in the current field */
680 startwrite += (dma_q->lines_completed * _line_size * 2);
e0d3bafd 681
84b5dbf3
MCC
682 /* bytes already completed in the current line */
683 startwrite += current_line_bytes_copied;
e0d3bafd 684
cde4362f
MCC
685 lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
686 bytes_to_copy : dma_q->bytes_left_in_line;
e0d3bafd 687
cde4362f 688 if ((u8 *)(startwrite + lencopy) > (u8 *)(p_out_buffer + buf->vb.size))
84b5dbf3 689 return 0;
e0d3bafd 690
84b5dbf3
MCC
691 /* The below copies the UYVY data straight into video buffer */
692 cx231xx_swab((u16 *) p_buffer, (u16 *) startwrite, (u16) lencopy);
e0d3bafd 693
84b5dbf3 694 return 0;
e0d3bafd
SD
695}
696
cde4362f 697void cx231xx_swab(u16 *from, u16 *to, u16 len)
e0d3bafd 698{
84b5dbf3 699 u16 i;
e0d3bafd 700
84b5dbf3
MCC
701 if (len <= 0)
702 return;
e0d3bafd 703
cde4362f 704 for (i = 0; i < len / 2; i++)
84b5dbf3 705 to[i] = (from[i] << 8) | (from[i] >> 8);
e0d3bafd
SD
706}
707
84b5dbf3 708u8 cx231xx_is_buffer_done(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q)
e0d3bafd 709{
84b5dbf3 710 u8 buffer_complete = 0;
e0d3bafd 711
84b5dbf3 712 /* Dual field stream */
cde4362f
MCC
713 buffer_complete = ((dma_q->current_field == 2) &&
714 (dma_q->lines_completed >= dma_q->lines_per_field) &&
715 dma_q->field1_done);
e0d3bafd 716
84b5dbf3 717 return buffer_complete;
e0d3bafd
SD
718}
719
e0d3bafd
SD
720/* ------------------------------------------------------------------
721 Videobuf operations
722 ------------------------------------------------------------------*/
723
724static int
725buffer_setup(struct videobuf_queue *vq, unsigned int *count, unsigned int *size)
726{
727 struct cx231xx_fh *fh = vq->priv_data;
84b5dbf3 728 struct cx231xx *dev = fh->dev;
e0d3bafd 729
cde4362f 730 *size = (fh->dev->width * fh->dev->height * dev->format->depth + 7)>>3;
e0d3bafd
SD
731 if (0 == *count)
732 *count = CX231XX_DEF_BUF;
733
734 if (*count < CX231XX_MIN_BUF)
735 *count = CX231XX_MIN_BUF;
736
3d263114
MCC
737
738 cx231xx_enable_analog_tuner(dev);
739
e0d3bafd
SD
740 return 0;
741}
742
743/* This is called *without* dev->slock held; please keep it that way */
744static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
745{
84b5dbf3
MCC
746 struct cx231xx_fh *fh = vq->priv_data;
747 struct cx231xx *dev = fh->dev;
e0d3bafd 748 unsigned long flags = 0;
cde4362f 749
09f2082e 750 BUG_ON(in_interrupt());
e0d3bafd
SD
751
752 /* We used to wait for the buffer to finish here, but this didn't work
753 because, as we were keeping the state as VIDEOBUF_QUEUED,
754 videobuf_queue_cancel marked it as finished for us.
755 (Also, it could wedge forever if the hardware was misconfigured.)
756
757 This should be safe; by the time we get here, the buffer isn't
758 queued anymore. If we ever start marking the buffers as
759 VIDEOBUF_ACTIVE, it won't be, though.
84b5dbf3 760 */
e0d3bafd 761 spin_lock_irqsave(&dev->video_mode.slock, flags);
64fbf444
PB
762 if (dev->USE_ISO) {
763 if (dev->video_mode.isoc_ctl.buf == buf)
764 dev->video_mode.isoc_ctl.buf = NULL;
765 } else {
766 if (dev->video_mode.bulk_ctl.buf == buf)
767 dev->video_mode.bulk_ctl.buf = NULL;
768 }
e0d3bafd
SD
769 spin_unlock_irqrestore(&dev->video_mode.slock, flags);
770
771 videobuf_vmalloc_free(&buf->vb);
772 buf->vb.state = VIDEOBUF_NEEDS_INIT;
773}
774
775static int
776buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
84b5dbf3 777 enum v4l2_field field)
e0d3bafd 778{
84b5dbf3
MCC
779 struct cx231xx_fh *fh = vq->priv_data;
780 struct cx231xx_buffer *buf =
781 container_of(vb, struct cx231xx_buffer, vb);
782 struct cx231xx *dev = fh->dev;
783 int rc = 0, urb_init = 0;
e0d3bafd
SD
784
785 /* The only currently supported format is 16 bits/pixel */
cde4362f
MCC
786 buf->vb.size = (fh->dev->width * fh->dev->height * dev->format->depth
787 + 7) >> 3;
84b5dbf3 788 if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
e0d3bafd
SD
789 return -EINVAL;
790
84b5dbf3 791 buf->vb.width = dev->width;
e0d3bafd 792 buf->vb.height = dev->height;
84b5dbf3 793 buf->vb.field = field;
e0d3bafd
SD
794
795 if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
796 rc = videobuf_iolock(vq, &buf->vb, NULL);
797 if (rc < 0)
798 goto fail;
799 }
800
64fbf444
PB
801 if (dev->USE_ISO) {
802 if (!dev->video_mode.isoc_ctl.num_bufs)
803 urb_init = 1;
804 } else {
805 if (!dev->video_mode.bulk_ctl.num_bufs)
806 urb_init = 1;
807 }
336fea92 808 dev_dbg(dev->dev,
b7085c08
MCC
809 "urb_init=%d dev->video_mode.max_pkt_size=%d\n",
810 urb_init, dev->video_mode.max_pkt_size);
e0d3bafd 811 if (urb_init) {
64fbf444
PB
812 dev->mode_tv = 0;
813 if (dev->USE_ISO)
814 rc = cx231xx_init_isoc(dev, CX231XX_NUM_PACKETS,
84b5dbf3
MCC
815 CX231XX_NUM_BUFS,
816 dev->video_mode.max_pkt_size,
817 cx231xx_isoc_copy);
64fbf444
PB
818 else
819 rc = cx231xx_init_bulk(dev, CX231XX_NUM_PACKETS,
820 CX231XX_NUM_BUFS,
821 dev->video_mode.max_pkt_size,
822 cx231xx_bulk_copy);
e0d3bafd
SD
823 if (rc < 0)
824 goto fail;
825 }
826
827 buf->vb.state = VIDEOBUF_PREPARED;
0f0fa90b 828
e0d3bafd
SD
829 return 0;
830
cde4362f 831fail:
e0d3bafd
SD
832 free_buffer(vq, buf);
833 return rc;
834}
835
84b5dbf3 836static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
e0d3bafd 837{
84b5dbf3
MCC
838 struct cx231xx_buffer *buf =
839 container_of(vb, struct cx231xx_buffer, vb);
840 struct cx231xx_fh *fh = vq->priv_data;
841 struct cx231xx *dev = fh->dev;
842 struct cx231xx_dmaqueue *vidq = &dev->video_mode.vidq;
e0d3bafd
SD
843
844 buf->vb.state = VIDEOBUF_QUEUED;
845 list_add_tail(&buf->vb.queue, &vidq->active);
846
847}
848
849static void buffer_release(struct videobuf_queue *vq,
84b5dbf3 850 struct videobuf_buffer *vb)
e0d3bafd 851{
84b5dbf3
MCC
852 struct cx231xx_buffer *buf =
853 container_of(vb, struct cx231xx_buffer, vb);
854 struct cx231xx_fh *fh = vq->priv_data;
855 struct cx231xx *dev = (struct cx231xx *)fh->dev;
e0d3bafd
SD
856
857 cx231xx_isocdbg("cx231xx: called buffer_release\n");
858
859 free_buffer(vq, buf);
860}
861
862static struct videobuf_queue_ops cx231xx_video_qops = {
84b5dbf3
MCC
863 .buf_setup = buffer_setup,
864 .buf_prepare = buffer_prepare,
865 .buf_queue = buffer_queue,
866 .buf_release = buffer_release,
e0d3bafd
SD
867};
868
869/********************* v4l2 interface **************************************/
870
e0d3bafd
SD
871void video_mux(struct cx231xx *dev, int index)
872{
e0d3bafd
SD
873 dev->video_input = index;
874 dev->ctl_ainput = INPUT(index)->amux;
875
84b5dbf3 876 cx231xx_set_video_input_mux(dev, index);
e0d3bafd 877
5325b427 878 cx25840_call(dev, video, s_routing, INPUT(index)->vmux, 0, 0);
e0d3bafd 879
84b5dbf3 880 cx231xx_set_audio_input(dev, dev->ctl_ainput);
e0d3bafd 881
336fea92 882 dev_dbg(dev->dev, "video_mux : %d\n", index);
e0d3bafd 883
84b5dbf3
MCC
884 /* do mode control overrides if required */
885 cx231xx_do_mode_ctrl_overrides(dev);
e0d3bafd
SD
886}
887
888/* Usage lock check functions */
889static int res_get(struct cx231xx_fh *fh)
890{
84b5dbf3
MCC
891 struct cx231xx *dev = fh->dev;
892 int rc = 0;
e0d3bafd
SD
893
894 /* This instance already has stream_on */
895 if (fh->stream_on)
896 return rc;
897
84b5dbf3
MCC
898 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
899 if (dev->stream_on)
900 return -EBUSY;
901 dev->stream_on = 1;
902 } else if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
903 if (dev->vbi_stream_on)
904 return -EBUSY;
905 dev->vbi_stream_on = 1;
906 } else
907 return -EINVAL;
e0d3bafd 908
84b5dbf3 909 fh->stream_on = 1;
e0d3bafd
SD
910
911 return rc;
912}
913
914static int res_check(struct cx231xx_fh *fh)
915{
cde4362f 916 return fh->stream_on;
e0d3bafd
SD
917}
918
919static void res_free(struct cx231xx_fh *fh)
920{
84b5dbf3 921 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
922
923 fh->stream_on = 0;
924
84b5dbf3
MCC
925 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
926 dev->stream_on = 0;
927 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)
928 dev->vbi_stream_on = 0;
e0d3bafd
SD
929}
930
931static int check_dev(struct cx231xx *dev)
932{
933 if (dev->state & DEV_DISCONNECTED) {
336fea92 934 dev_err(dev->dev, "v4l2 ioctl: device not present\n");
e0d3bafd
SD
935 return -ENODEV;
936 }
e0d3bafd
SD
937 return 0;
938}
939
e0d3bafd
SD
940/* ------------------------------------------------------------------
941 IOCTL vidioc handling
942 ------------------------------------------------------------------*/
943
944static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
84b5dbf3 945 struct v4l2_format *f)
e0d3bafd 946{
84b5dbf3
MCC
947 struct cx231xx_fh *fh = priv;
948 struct cx231xx *dev = fh->dev;
e0d3bafd 949
e0d3bafd
SD
950 f->fmt.pix.width = dev->width;
951 f->fmt.pix.height = dev->height;
1ebcad77
JP
952 f->fmt.pix.pixelformat = dev->format->fourcc;
953 f->fmt.pix.bytesperline = (dev->width * dev->format->depth + 7) >> 3;
84b5dbf3 954 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * dev->height;
e0d3bafd
SD
955 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
956
957 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
958
e0d3bafd
SD
959 return 0;
960}
961
962static struct cx231xx_fmt *format_by_fourcc(unsigned int fourcc)
963{
964 unsigned int i;
965
966 for (i = 0; i < ARRAY_SIZE(format); i++)
967 if (format[i].fourcc == fourcc)
968 return &format[i];
969
970 return NULL;
971}
972
973static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
84b5dbf3 974 struct v4l2_format *f)
e0d3bafd 975{
84b5dbf3
MCC
976 struct cx231xx_fh *fh = priv;
977 struct cx231xx *dev = fh->dev;
9bd0e8d7
TP
978 unsigned int width = f->fmt.pix.width;
979 unsigned int height = f->fmt.pix.height;
84b5dbf3
MCC
980 unsigned int maxw = norm_maxw(dev);
981 unsigned int maxh = norm_maxh(dev);
84b5dbf3 982 struct cx231xx_fmt *fmt;
e0d3bafd
SD
983
984 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
985 if (!fmt) {
986 cx231xx_videodbg("Fourcc format (%08x) invalid.\n",
84b5dbf3 987 f->fmt.pix.pixelformat);
e0d3bafd
SD
988 return -EINVAL;
989 }
990
991 /* width must even because of the YUYV format
992 height must be even because of interlacing */
9bd0e8d7 993 v4l_bound_align_image(&width, 48, maxw, 1, &height, 32, maxh, 1, 0);
e0d3bafd 994
e0d3bafd
SD
995 f->fmt.pix.width = width;
996 f->fmt.pix.height = height;
997 f->fmt.pix.pixelformat = fmt->fourcc;
8b735c13 998 f->fmt.pix.bytesperline = (width * fmt->depth + 7) >> 3;
e0d3bafd
SD
999 f->fmt.pix.sizeimage = f->fmt.pix.bytesperline * height;
1000 f->fmt.pix.colorspace = V4L2_COLORSPACE_SMPTE170M;
1001 f->fmt.pix.field = V4L2_FIELD_INTERLACED;
1002
1003 return 0;
1004}
1005
1006static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
84b5dbf3 1007 struct v4l2_format *f)
e0d3bafd 1008{
84b5dbf3
MCC
1009 struct cx231xx_fh *fh = priv;
1010 struct cx231xx *dev = fh->dev;
1011 int rc;
1012 struct cx231xx_fmt *fmt;
ebf984bb
HV
1013 struct v4l2_subdev_format format = {
1014 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1015 };
e0d3bafd
SD
1016
1017 rc = check_dev(dev);
1018 if (rc < 0)
1019 return rc;
1020
84b5dbf3 1021 vidioc_try_fmt_vid_cap(file, priv, f);
e0d3bafd 1022
84b5dbf3 1023 fmt = format_by_fourcc(f->fmt.pix.pixelformat);
643800d5
MCC
1024 if (!fmt)
1025 return -EINVAL;
e0d3bafd
SD
1026
1027 if (videobuf_queue_is_busy(&fh->vb_vidq)) {
336fea92 1028 dev_err(dev->dev, "%s: queue busy\n", __func__);
643800d5 1029 return -EBUSY;
e0d3bafd
SD
1030 }
1031
1032 if (dev->stream_on && !fh->stream_on) {
336fea92 1033 dev_err(dev->dev,
b7085c08 1034 "%s: device in use by another fh\n", __func__);
643800d5 1035 return -EBUSY;
e0d3bafd
SD
1036 }
1037
1038 /* set new image size */
1039 dev->width = f->fmt.pix.width;
1040 dev->height = f->fmt.pix.height;
84b5dbf3 1041 dev->format = fmt;
e0d3bafd 1042
ebf984bb
HV
1043 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
1044 call_all(dev, pad, set_fmt, NULL, &format);
1045 v4l2_fill_pix_format(&f->fmt.pix, &format.format);
e0d3bafd 1046
e0d3bafd
SD
1047 return rc;
1048}
1049
64fbf444 1050static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
e0d3bafd 1051{
84b5dbf3
MCC
1052 struct cx231xx_fh *fh = priv;
1053 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1054
1055 *id = dev->norm;
1056 return 0;
1057}
1058
314527ac 1059static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id norm)
e0d3bafd 1060{
84b5dbf3
MCC
1061 struct cx231xx_fh *fh = priv;
1062 struct cx231xx *dev = fh->dev;
ebf984bb
HV
1063 struct v4l2_subdev_format format = {
1064 .which = V4L2_SUBDEV_FORMAT_ACTIVE,
1065 };
84b5dbf3 1066 int rc;
e0d3bafd
SD
1067
1068 rc = check_dev(dev);
1069 if (rc < 0)
1070 return rc;
1071
314527ac 1072 if (dev->norm == norm)
d61072a4
HV
1073 return 0;
1074
1075 if (videobuf_queue_is_busy(&fh->vb_vidq))
1076 return -EBUSY;
e0d3bafd 1077
314527ac 1078 dev->norm = norm;
e0d3bafd 1079
e0d3bafd 1080 /* Adjusts width/height, if needed */
d61072a4
HV
1081 dev->width = 720;
1082 dev->height = (dev->norm & V4L2_STD_625_50) ? 576 : 480;
e0d3bafd 1083
8774bed9 1084 call_all(dev, video, s_std, dev->norm);
435b4f78
DH
1085
1086 /* We need to reset basic properties in the decoder related to
1087 resolution (since a standard change effects things like the number
1088 of lines in VACT, etc) */
ebf984bb
HV
1089 format.format.code = MEDIA_BUS_FMT_FIXED;
1090 format.format.width = dev->width;
1091 format.format.height = dev->height;
1092 call_all(dev, pad, set_fmt, NULL, &format);
e0d3bafd 1093
84b5dbf3
MCC
1094 /* do mode control overrides */
1095 cx231xx_do_mode_ctrl_overrides(dev);
e0d3bafd
SD
1096
1097 return 0;
1098}
1099
1100static const char *iname[] = {
1101 [CX231XX_VMUX_COMPOSITE1] = "Composite1",
cde4362f 1102 [CX231XX_VMUX_SVIDEO] = "S-Video",
e0d3bafd 1103 [CX231XX_VMUX_TELEVISION] = "Television",
cde4362f
MCC
1104 [CX231XX_VMUX_CABLE] = "Cable TV",
1105 [CX231XX_VMUX_DVB] = "DVB",
e0d3bafd
SD
1106};
1107
6168309a
MCC
1108void cx231xx_v4l2_create_entities(struct cx231xx *dev)
1109{
1110#if defined(CONFIG_MEDIA_CONTROLLER)
1111 int ret, i;
1112
1113 /* Create entities for each input connector */
1114 for (i = 0; i < MAX_CX231XX_INPUT; i++) {
1115 struct media_entity *ent = &dev->input_ent[i];
1116
1117 if (!INPUT(i)->type)
1118 break;
1119
1120 ent->name = iname[INPUT(i)->type];
1121 ent->flags = MEDIA_ENT_FL_CONNECTOR;
1122 dev->input_pad[i].flags = MEDIA_PAD_FL_SOURCE;
1123
1124 switch (INPUT(i)->type) {
1125 case CX231XX_VMUX_COMPOSITE1:
1126 ent->function = MEDIA_ENT_F_CONN_COMPOSITE;
1127 break;
1128 case CX231XX_VMUX_SVIDEO:
1129 ent->function = MEDIA_ENT_F_CONN_SVIDEO;
1130 break;
1131 case CX231XX_VMUX_TELEVISION:
1132 case CX231XX_VMUX_CABLE:
1133 case CX231XX_VMUX_DVB:
1134 /* The DVB core will handle it */
1135 if (dev->tuner_type == TUNER_ABSENT)
1136 continue;
1137 /* fall though */
22d50e9a 1138 default: /* just to shut up a gcc warning */
6168309a
MCC
1139 ent->function = MEDIA_ENT_F_CONN_RF;
1140 break;
1141 }
1142
1143 ret = media_entity_pads_init(ent, 1, &dev->input_pad[i]);
1144 if (ret < 0)
1145 pr_err("failed to initialize input pad[%d]!\n", i);
1146
1147 ret = media_device_register_entity(dev->media_dev, ent);
1148 if (ret < 0)
1149 pr_err("failed to register input entity %d!\n", i);
1150 }
1151#endif
1152}
1153
b86d1544 1154int cx231xx_enum_input(struct file *file, void *priv,
84b5dbf3 1155 struct v4l2_input *i)
e0d3bafd 1156{
84b5dbf3
MCC
1157 struct cx231xx_fh *fh = priv;
1158 struct cx231xx *dev = fh->dev;
de99d532 1159 u32 gen_stat;
e54560d9
AH
1160 unsigned int n;
1161 int ret;
e0d3bafd
SD
1162
1163 n = i->index;
1164 if (n >= MAX_CX231XX_INPUT)
1165 return -EINVAL;
1166 if (0 == INPUT(n)->type)
1167 return -EINVAL;
1168
1169 i->index = n;
1170 i->type = V4L2_INPUT_TYPE_CAMERA;
1171
1172 strcpy(i->name, iname[INPUT(n)->type]);
1173
1174 if ((CX231XX_VMUX_TELEVISION == INPUT(n)->type) ||
84b5dbf3 1175 (CX231XX_VMUX_CABLE == INPUT(n)->type))
e0d3bafd
SD
1176 i->type = V4L2_INPUT_TYPE_TUNER;
1177
60acf187 1178 i->std = dev->vdev.tvnorms;
e0d3bafd 1179
de99d532
DH
1180 /* If they are asking about the active input, read signal status */
1181 if (n == dev->video_input) {
1182 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1183 GEN_STAT, 2, &gen_stat, 4);
1184 if (ret > 0) {
1185 if ((gen_stat & FLD_VPRES) == 0x00)
1186 i->status |= V4L2_IN_ST_NO_SIGNAL;
1187 if ((gen_stat & FLD_HLOCK) == 0x00)
1188 i->status |= V4L2_IN_ST_NO_H_LOCK;
1189 }
1190 }
1191
e0d3bafd
SD
1192 return 0;
1193}
1194
b86d1544 1195int cx231xx_g_input(struct file *file, void *priv, unsigned int *i)
e0d3bafd 1196{
84b5dbf3
MCC
1197 struct cx231xx_fh *fh = priv;
1198 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1199
1200 *i = dev->video_input;
1201
1202 return 0;
1203}
1204
b86d1544 1205int cx231xx_s_input(struct file *file, void *priv, unsigned int i)
e0d3bafd 1206{
84b5dbf3
MCC
1207 struct cx231xx_fh *fh = priv;
1208 struct cx231xx *dev = fh->dev;
1209 int rc;
e0d3bafd 1210
64fbf444 1211 dev->mode_tv = 0;
e0d3bafd
SD
1212 rc = check_dev(dev);
1213 if (rc < 0)
1214 return rc;
1215
1216 if (i >= MAX_CX231XX_INPUT)
1217 return -EINVAL;
1218 if (0 == INPUT(i)->type)
1219 return -EINVAL;
1220
e0d3bafd
SD
1221 video_mux(dev, i);
1222
c09d6695
DH
1223 if (INPUT(i)->type == CX231XX_VMUX_TELEVISION ||
1224 INPUT(i)->type == CX231XX_VMUX_CABLE) {
1225 /* There's a tuner, so reset the standard and put it on the
1226 last known frequency (since it was probably powered down
1227 until now */
8774bed9 1228 call_all(dev, video, s_std, dev->norm);
c09d6695
DH
1229 }
1230
e0d3bafd
SD
1231 return 0;
1232}
1233
b86d1544 1234int cx231xx_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
e0d3bafd 1235{
84b5dbf3
MCC
1236 struct cx231xx_fh *fh = priv;
1237 struct cx231xx *dev = fh->dev;
1238 int rc;
e0d3bafd
SD
1239
1240 rc = check_dev(dev);
1241 if (rc < 0)
1242 return rc;
1243
1244 if (0 != t->index)
1245 return -EINVAL;
1246
1247 strcpy(t->name, "Tuner");
1248
84b5dbf3 1249 t->type = V4L2_TUNER_ANALOG_TV;
e0d3bafd 1250 t->capability = V4L2_TUNER_CAP_NORM;
84b5dbf3
MCC
1251 t->rangehigh = 0xffffffffUL;
1252 t->signal = 0xffff; /* LOCKED */
b251f957 1253 call_all(dev, tuner, g_tuner, t);
e0d3bafd
SD
1254
1255 return 0;
1256}
1257
2f73c7c5 1258int cx231xx_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
e0d3bafd 1259{
84b5dbf3
MCC
1260 struct cx231xx_fh *fh = priv;
1261 struct cx231xx *dev = fh->dev;
1262 int rc;
e0d3bafd
SD
1263
1264 rc = check_dev(dev);
1265 if (rc < 0)
1266 return rc;
1267
1268 if (0 != t->index)
1269 return -EINVAL;
1270#if 0
b1196126 1271 call_all(dev, tuner, s_tuner, t);
e0d3bafd
SD
1272#endif
1273 return 0;
1274}
1275
b86d1544 1276int cx231xx_g_frequency(struct file *file, void *priv,
84b5dbf3 1277 struct v4l2_frequency *f)
e0d3bafd 1278{
84b5dbf3
MCC
1279 struct cx231xx_fh *fh = priv;
1280 struct cx231xx *dev = fh->dev;
e0d3bafd 1281
b251f957
HV
1282 if (f->tuner)
1283 return -EINVAL;
1284
e0d3bafd
SD
1285 f->frequency = dev->ctl_freq;
1286
e0d3bafd
SD
1287 return 0;
1288}
1289
b86d1544 1290int cx231xx_s_frequency(struct file *file, void *priv,
b530a447 1291 const struct v4l2_frequency *f)
e0d3bafd 1292{
84b5dbf3
MCC
1293 struct cx231xx_fh *fh = priv;
1294 struct cx231xx *dev = fh->dev;
b530a447 1295 struct v4l2_frequency new_freq = *f;
84b5dbf3 1296 int rc;
64fbf444
PB
1297 u32 if_frequency = 5400000;
1298
336fea92 1299 dev_dbg(dev->dev,
b7085c08
MCC
1300 "Enter vidioc_s_frequency()f->frequency=%d;f->type=%d\n",
1301 f->frequency, f->type);
e0d3bafd
SD
1302
1303 rc = check_dev(dev);
1304 if (rc < 0)
1305 return rc;
1306
1307 if (0 != f->tuner)
1308 return -EINVAL;
1309
84b5dbf3
MCC
1310 /* set pre channel change settings in DIF first */
1311 rc = cx231xx_tuner_pre_channel_change(dev);
e0d3bafd 1312
64fbf444 1313 call_all(dev, tuner, s_frequency, f);
b530a447
HV
1314 call_all(dev, tuner, g_frequency, &new_freq);
1315 dev->ctl_freq = new_freq.frequency;
e0d3bafd 1316
84b5dbf3
MCC
1317 /* set post channel change settings in DIF first */
1318 rc = cx231xx_tuner_post_channel_change(dev);
e0d3bafd 1319
64fbf444
PB
1320 if (dev->tuner_type == TUNER_NXP_TDA18271) {
1321 if (dev->norm & (V4L2_STD_MN | V4L2_STD_NTSC_443))
1322 if_frequency = 5400000; /*5.4MHz */
1323 else if (dev->norm & V4L2_STD_B)
1324 if_frequency = 6000000; /*6.0MHz */
1325 else if (dev->norm & (V4L2_STD_PAL_DK | V4L2_STD_SECAM_DK))
1326 if_frequency = 6900000; /*6.9MHz */
1327 else if (dev->norm & V4L2_STD_GH)
1328 if_frequency = 7100000; /*7.1MHz */
1329 else if (dev->norm & V4L2_STD_PAL_I)
1330 if_frequency = 7250000; /*7.25MHz */
1331 else if (dev->norm & V4L2_STD_SECAM_L)
1332 if_frequency = 6900000; /*6.9MHz */
1333 else if (dev->norm & V4L2_STD_SECAM_LC)
1334 if_frequency = 1250000; /*1.25MHz */
1335
336fea92 1336 dev_dbg(dev->dev,
b7085c08 1337 "if_frequency is set to %d\n", if_frequency);
64fbf444
PB
1338 cx231xx_set_Colibri_For_LowIF(dev, if_frequency, 1, 1);
1339
1340 update_HH_register_after_set_DIF(dev);
1341 }
1342
336fea92 1343 dev_dbg(dev->dev, "Set New FREQUENCY to %d\n", f->frequency);
e0d3bafd
SD
1344
1345 return rc;
1346}
1347
08fe9f7d
HV
1348#ifdef CONFIG_VIDEO_ADV_DEBUG
1349
1350int cx231xx_g_chip_info(struct file *file, void *fh,
1351 struct v4l2_dbg_chip_info *chip)
fddd14c8 1352{
08fe9f7d
HV
1353 switch (chip->match.addr) {
1354 case 0: /* Cx231xx - internal registers */
1355 return 0;
1356 case 1: /* AFE - read byte */
1357 strlcpy(chip->name, "AFE (byte)", sizeof(chip->name));
1358 return 0;
1359 case 2: /* Video Block - read byte */
1360 strlcpy(chip->name, "Video (byte)", sizeof(chip->name));
1361 return 0;
1362 case 3: /* I2S block - read byte */
1363 strlcpy(chip->name, "I2S (byte)", sizeof(chip->name));
1364 return 0;
1365 case 4: /* AFE - read dword */
1366 strlcpy(chip->name, "AFE (dword)", sizeof(chip->name));
1367 return 0;
1368 case 5: /* Video Block - read dword */
1369 strlcpy(chip->name, "Video (dword)", sizeof(chip->name));
1370 return 0;
1371 case 6: /* I2S Block - read dword */
1372 strlcpy(chip->name, "I2S (dword)", sizeof(chip->name));
fddd14c8
HV
1373 return 0;
1374 }
1375 return -EINVAL;
1376}
1377
b86d1544 1378int cx231xx_g_register(struct file *file, void *priv,
e0d3bafd
SD
1379 struct v4l2_dbg_register *reg)
1380{
84b5dbf3
MCC
1381 struct cx231xx_fh *fh = priv;
1382 struct cx231xx *dev = fh->dev;
08fe9f7d 1383 int ret;
84b5dbf3
MCC
1384 u8 value[4] = { 0, 0, 0, 0 };
1385 u32 data = 0;
1386
08fe9f7d
HV
1387 switch (reg->match.addr) {
1388 case 0: /* Cx231xx - internal registers */
1389 ret = cx231xx_read_ctrl_reg(dev, VRT_GET_REGISTER,
1390 (u16)reg->reg, value, 4);
1391 reg->val = value[0] | value[1] << 8 |
1392 value[2] << 16 | value[3] << 24;
04ae4cf2 1393 reg->size = 4;
08fe9f7d
HV
1394 break;
1395 case 1: /* AFE - read byte */
1396 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1397 (u16)reg->reg, 2, &data, 1);
1398 reg->val = data;
04ae4cf2 1399 reg->size = 1;
08fe9f7d
HV
1400 break;
1401 case 2: /* Video Block - read byte */
1402 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1403 (u16)reg->reg, 2, &data, 1);
1404 reg->val = data;
04ae4cf2 1405 reg->size = 1;
08fe9f7d
HV
1406 break;
1407 case 3: /* I2S block - read byte */
1408 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1409 (u16)reg->reg, 1, &data, 1);
1410 reg->val = data;
04ae4cf2 1411 reg->size = 1;
08fe9f7d
HV
1412 break;
1413 case 4: /* AFE - read dword */
1414 ret = cx231xx_read_i2c_data(dev, AFE_DEVICE_ADDRESS,
1415 (u16)reg->reg, 2, &data, 4);
1416 reg->val = data;
04ae4cf2 1417 reg->size = 4;
08fe9f7d
HV
1418 break;
1419 case 5: /* Video Block - read dword */
1420 ret = cx231xx_read_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1421 (u16)reg->reg, 2, &data, 4);
1422 reg->val = data;
04ae4cf2 1423 reg->size = 4;
08fe9f7d
HV
1424 break;
1425 case 6: /* I2S Block - read dword */
1426 ret = cx231xx_read_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1427 (u16)reg->reg, 1, &data, 4);
1428 reg->val = data;
04ae4cf2 1429 reg->size = 4;
08fe9f7d 1430 break;
84b5dbf3 1431 default:
08fe9f7d 1432 return -EINVAL;
84b5dbf3 1433 }
08fe9f7d 1434 return ret < 0 ? ret : 0;
e0d3bafd
SD
1435}
1436
b86d1544 1437int cx231xx_s_register(struct file *file, void *priv,
977ba3b1 1438 const struct v4l2_dbg_register *reg)
e0d3bafd 1439{
84b5dbf3
MCC
1440 struct cx231xx_fh *fh = priv;
1441 struct cx231xx *dev = fh->dev;
08fe9f7d 1442 int ret;
84b5dbf3 1443 u8 data[4] = { 0, 0, 0, 0 };
e0d3bafd 1444
08fe9f7d
HV
1445 switch (reg->match.addr) {
1446 case 0: /* cx231xx internal registers */
1447 data[0] = (u8) reg->val;
1448 data[1] = (u8) (reg->val >> 8);
1449 data[2] = (u8) (reg->val >> 16);
1450 data[3] = (u8) (reg->val >> 24);
1451 ret = cx231xx_write_ctrl_reg(dev, VRT_SET_REGISTER,
1452 (u16)reg->reg, data, 4);
1453 break;
1454 case 1: /* AFE - write byte */
1455 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1456 (u16)reg->reg, 2, reg->val, 1);
1457 break;
1458 case 2: /* Video Block - write byte */
1459 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1460 (u16)reg->reg, 2, reg->val, 1);
1461 break;
1462 case 3: /* I2S block - write byte */
1463 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1464 (u16)reg->reg, 1, reg->val, 1);
1465 break;
1466 case 4: /* AFE - write dword */
1467 ret = cx231xx_write_i2c_data(dev, AFE_DEVICE_ADDRESS,
1468 (u16)reg->reg, 2, reg->val, 4);
1469 break;
1470 case 5: /* Video Block - write dword */
1471 ret = cx231xx_write_i2c_data(dev, VID_BLK_I2C_ADDRESS,
1472 (u16)reg->reg, 2, reg->val, 4);
84b5dbf3 1473 break;
08fe9f7d
HV
1474 case 6: /* I2S block - write dword */
1475 ret = cx231xx_write_i2c_data(dev, I2S_BLK_DEVICE_ADDRESS,
1476 (u16)reg->reg, 1, reg->val, 4);
1477 break;
1478 default:
1479 return -EINVAL;
84b5dbf3 1480 }
08fe9f7d 1481 return ret < 0 ? ret : 0;
e0d3bafd
SD
1482}
1483#endif
1484
e0d3bafd 1485static int vidioc_cropcap(struct file *file, void *priv,
84b5dbf3 1486 struct v4l2_cropcap *cc)
e0d3bafd 1487{
84b5dbf3
MCC
1488 struct cx231xx_fh *fh = priv;
1489 struct cx231xx *dev = fh->dev;
e25cb200 1490 bool is_50hz = dev->norm & V4L2_STD_625_50;
e0d3bafd
SD
1491
1492 if (cc->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1493 return -EINVAL;
1494
1495 cc->bounds.left = 0;
1496 cc->bounds.top = 0;
1497 cc->bounds.width = dev->width;
1498 cc->bounds.height = dev->height;
1499 cc->defrect = cc->bounds;
e25cb200
HV
1500 cc->pixelaspect.numerator = is_50hz ? 54 : 11;
1501 cc->pixelaspect.denominator = is_50hz ? 59 : 10;
e0d3bafd
SD
1502
1503 return 0;
1504}
1505
1506static int vidioc_streamon(struct file *file, void *priv,
84b5dbf3 1507 enum v4l2_buf_type type)
e0d3bafd 1508{
84b5dbf3
MCC
1509 struct cx231xx_fh *fh = priv;
1510 struct cx231xx *dev = fh->dev;
1511 int rc;
e0d3bafd
SD
1512
1513 rc = check_dev(dev);
1514 if (rc < 0)
1515 return rc;
1516
e0d3bafd
SD
1517 rc = res_get(fh);
1518
1519 if (likely(rc >= 0))
1520 rc = videobuf_streamon(&fh->vb_vidq);
1521
b1196126
SD
1522 call_all(dev, video, s_stream, 1);
1523
e0d3bafd
SD
1524 return rc;
1525}
1526
1527static int vidioc_streamoff(struct file *file, void *priv,
84b5dbf3 1528 enum v4l2_buf_type type)
e0d3bafd 1529{
84b5dbf3
MCC
1530 struct cx231xx_fh *fh = priv;
1531 struct cx231xx *dev = fh->dev;
1532 int rc;
e0d3bafd
SD
1533
1534 rc = check_dev(dev);
1535 if (rc < 0)
1536 return rc;
1537
e0d3bafd
SD
1538 if (type != fh->type)
1539 return -EINVAL;
1540
b1196126
SD
1541 cx25840_call(dev, video, s_stream, 0);
1542
e0d3bafd
SD
1543 videobuf_streamoff(&fh->vb_vidq);
1544 res_free(fh);
1545
e0d3bafd
SD
1546 return 0;
1547}
1548
bc08734c 1549int cx231xx_querycap(struct file *file, void *priv,
84b5dbf3 1550 struct v4l2_capability *cap)
e0d3bafd 1551{
4bc837d4 1552 struct video_device *vdev = video_devdata(file);
84b5dbf3
MCC
1553 struct cx231xx_fh *fh = priv;
1554 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1555
1556 strlcpy(cap->driver, "cx231xx", sizeof(cap->driver));
1557 strlcpy(cap->card, cx231xx_boards[dev->model].name, sizeof(cap->card));
2c6beca8 1558 usb_make_path(dev->udev, cap->bus_info, sizeof(cap->bus_info));
e0d3bafd 1559
530e01e7
HV
1560 if (vdev->vfl_type == VFL_TYPE_RADIO)
1561 cap->device_caps = V4L2_CAP_RADIO;
1562 else {
06c46003 1563 cap->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
530e01e7
HV
1564 if (vdev->vfl_type == VFL_TYPE_VBI)
1565 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
1566 else
1567 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
1568 }
e0d3bafd 1569 if (dev->tuner_type != TUNER_ABSENT)
4bc837d4 1570 cap->device_caps |= V4L2_CAP_TUNER;
06c46003 1571 cap->capabilities = cap->device_caps | V4L2_CAP_READWRITE |
4bc837d4 1572 V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE |
530e01e7 1573 V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
60acf187 1574 if (video_is_registered(&dev->radio_dev))
530e01e7 1575 cap->capabilities |= V4L2_CAP_RADIO;
e0d3bafd
SD
1576
1577 return 0;
1578}
1579
84b5dbf3
MCC
1580static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
1581 struct v4l2_fmtdesc *f)
e0d3bafd 1582{
84b5dbf3 1583 if (unlikely(f->index >= ARRAY_SIZE(format)))
e0d3bafd
SD
1584 return -EINVAL;
1585
1586 strlcpy(f->description, format[f->index].name, sizeof(f->description));
1587 f->pixelformat = format[f->index].fourcc;
1588
1589 return 0;
1590}
1591
e0d3bafd
SD
1592/* RAW VBI ioctls */
1593
1594static int vidioc_g_fmt_vbi_cap(struct file *file, void *priv,
84b5dbf3 1595 struct v4l2_format *f)
e0d3bafd 1596{
84b5dbf3
MCC
1597 struct cx231xx_fh *fh = priv;
1598 struct cx231xx *dev = fh->dev;
6264722c 1599
adc0356e 1600 f->fmt.vbi.sampling_rate = 6750000 * 4;
e0d3bafd
SD
1601 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1602 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
adc0356e 1603 f->fmt.vbi.offset = 0;
e0d3bafd 1604 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1605 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
e0d3bafd 1606 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1607 PAL_VBI_LINES : NTSC_VBI_LINES;
e0d3bafd 1608 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1609 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
e0d3bafd 1610 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
6264722c 1611 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
e0d3bafd
SD
1612
1613 return 0;
1614
1615}
1616
1617static int vidioc_try_fmt_vbi_cap(struct file *file, void *priv,
84b5dbf3 1618 struct v4l2_format *f)
e0d3bafd 1619{
84b5dbf3
MCC
1620 struct cx231xx_fh *fh = priv;
1621 struct cx231xx *dev = fh->dev;
e0d3bafd 1622
adc0356e 1623 f->fmt.vbi.sampling_rate = 6750000 * 4;
e0d3bafd
SD
1624 f->fmt.vbi.samples_per_line = VBI_LINE_LENGTH;
1625 f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
adc0356e 1626 f->fmt.vbi.offset = 0;
e0d3bafd
SD
1627 f->fmt.vbi.flags = 0;
1628 f->fmt.vbi.start[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1629 PAL_VBI_START_LINE : NTSC_VBI_START_LINE;
e0d3bafd 1630 f->fmt.vbi.count[0] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1631 PAL_VBI_LINES : NTSC_VBI_LINES;
e0d3bafd 1632 f->fmt.vbi.start[1] = (dev->norm & V4L2_STD_625_50) ?
84b5dbf3 1633 PAL_VBI_START_LINE + 312 : NTSC_VBI_START_LINE + 263;
e0d3bafd 1634 f->fmt.vbi.count[1] = f->fmt.vbi.count[0];
6264722c 1635 memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
e0d3bafd
SD
1636
1637 return 0;
1638
1639}
1640
6264722c
HV
1641static int vidioc_s_fmt_vbi_cap(struct file *file, void *priv,
1642 struct v4l2_format *f)
1643{
1644 struct cx231xx_fh *fh = priv;
1645 struct cx231xx *dev = fh->dev;
1646
1647 if (dev->vbi_stream_on && !fh->stream_on) {
336fea92 1648 dev_err(dev->dev,
b7085c08 1649 "%s device in use by another fh\n", __func__);
6264722c
HV
1650 return -EBUSY;
1651 }
1652 return vidioc_try_fmt_vbi_cap(file, priv, f);
1653}
1654
e0d3bafd
SD
1655static int vidioc_reqbufs(struct file *file, void *priv,
1656 struct v4l2_requestbuffers *rb)
1657{
84b5dbf3
MCC
1658 struct cx231xx_fh *fh = priv;
1659 struct cx231xx *dev = fh->dev;
1660 int rc;
e0d3bafd
SD
1661
1662 rc = check_dev(dev);
1663 if (rc < 0)
1664 return rc;
1665
cde4362f 1666 return videobuf_reqbufs(&fh->vb_vidq, rb);
e0d3bafd
SD
1667}
1668
84b5dbf3 1669static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *b)
e0d3bafd 1670{
84b5dbf3
MCC
1671 struct cx231xx_fh *fh = priv;
1672 struct cx231xx *dev = fh->dev;
1673 int rc;
e0d3bafd
SD
1674
1675 rc = check_dev(dev);
1676 if (rc < 0)
1677 return rc;
1678
cde4362f 1679 return videobuf_querybuf(&fh->vb_vidq, b);
e0d3bafd
SD
1680}
1681
1682static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1683{
84b5dbf3
MCC
1684 struct cx231xx_fh *fh = priv;
1685 struct cx231xx *dev = fh->dev;
1686 int rc;
e0d3bafd
SD
1687
1688 rc = check_dev(dev);
1689 if (rc < 0)
1690 return rc;
1691
cde4362f 1692 return videobuf_qbuf(&fh->vb_vidq, b);
e0d3bafd
SD
1693}
1694
1695static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
1696{
84b5dbf3
MCC
1697 struct cx231xx_fh *fh = priv;
1698 struct cx231xx *dev = fh->dev;
1699 int rc;
e0d3bafd
SD
1700
1701 rc = check_dev(dev);
1702 if (rc < 0)
1703 return rc;
1704
cde4362f 1705 return videobuf_dqbuf(&fh->vb_vidq, b, file->f_flags & O_NONBLOCK);
e0d3bafd
SD
1706}
1707
e0d3bafd
SD
1708/* ----------------------------------------------------------- */
1709/* RADIO ESPECIFIC IOCTLS */
1710/* ----------------------------------------------------------- */
1711
84b5dbf3 1712static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
e0d3bafd
SD
1713{
1714 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1715
530e01e7 1716 if (t->index)
e0d3bafd
SD
1717 return -EINVAL;
1718
1719 strcpy(t->name, "Radio");
e0d3bafd 1720
530e01e7 1721 call_all(dev, tuner, g_tuner, t);
e0d3bafd 1722
e0d3bafd
SD
1723 return 0;
1724}
2f73c7c5 1725static int radio_s_tuner(struct file *file, void *priv, const struct v4l2_tuner *t)
e0d3bafd
SD
1726{
1727 struct cx231xx *dev = ((struct cx231xx_fh *)priv)->dev;
1728
b86d1544 1729 if (t->index)
e0d3bafd
SD
1730 return -EINVAL;
1731
b1196126 1732 call_all(dev, tuner, s_tuner, t);
e0d3bafd
SD
1733
1734 return 0;
1735}
1736
e0d3bafd
SD
1737/*
1738 * cx231xx_v4l2_open()
1739 * inits the device and starts isoc transfer
1740 */
1741static int cx231xx_v4l2_open(struct file *filp)
1742{
4df16f70 1743 int radio = 0;
63b0d5ad
LP
1744 struct video_device *vdev = video_devdata(filp);
1745 struct cx231xx *dev = video_drvdata(filp);
e0d3bafd
SD
1746 struct cx231xx_fh *fh;
1747 enum v4l2_buf_type fh_type = 0;
1748
63b0d5ad
LP
1749 switch (vdev->vfl_type) {
1750 case VFL_TYPE_GRABBER:
1751 fh_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1752 break;
1753 case VFL_TYPE_VBI:
1754 fh_type = V4L2_BUF_TYPE_VBI_CAPTURE;
1755 break;
1756 case VFL_TYPE_RADIO:
1757 radio = 1;
1758 break;
1759 }
e0d3bafd 1760
50462eb0
LP
1761 cx231xx_videodbg("open dev=%s type=%s users=%d\n",
1762 video_device_node_name(vdev), v4l2_type_names[fh_type],
1763 dev->users);
e0d3bafd
SD
1764
1765#if 0
1766 errCode = cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1767 if (errCode < 0) {
336fea92 1768 dev_err(dev->dev,
b7085c08 1769 "Device locked on digital mode. Can't open analog\n");
e0d3bafd
SD
1770 return -EBUSY;
1771 }
1772#endif
1773
1774 fh = kzalloc(sizeof(struct cx231xx_fh), GFP_KERNEL);
b7085c08 1775 if (!fh)
e0d3bafd 1776 return -ENOMEM;
1f7e073d
HV
1777 if (mutex_lock_interruptible(&dev->lock)) {
1778 kfree(fh);
1779 return -ERESTARTSYS;
1780 }
e0d3bafd 1781 fh->dev = dev;
e0d3bafd
SD
1782 fh->type = fh_type;
1783 filp->private_data = fh;
1d08a4fa 1784 v4l2_fh_init(&fh->fh, vdev);
e0d3bafd
SD
1785
1786 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE && dev->users == 0) {
84b5dbf3 1787 /* Power up in Analog TV mode */
2f861387 1788 if (dev->board.external_av)
64fbf444
PB
1789 cx231xx_set_power_mode(dev,
1790 POLARIS_AVMODE_ENXTERNAL_AV);
1791 else
1792 cx231xx_set_power_mode(dev, POLARIS_AVMODE_ANALOGT_TV);
e0d3bafd
SD
1793
1794#if 0
1795 cx231xx_set_mode(dev, CX231XX_ANALOG_MODE);
1796#endif
e0d3bafd 1797
84b5dbf3
MCC
1798 /* set video alternate setting */
1799 cx231xx_set_video_alternate(dev);
e0d3bafd
SD
1800
1801 /* Needed, since GPIO might have disabled power of
1802 some i2c device */
1803 cx231xx_config_i2c(dev);
1804
1805 /* device needs to be initialized before isoc transfer */
84b5dbf3 1806 dev->video_input = dev->video_input > 2 ? 2 : dev->video_input;
e0d3bafd
SD
1807
1808 }
71590765 1809 if (radio) {
e0d3bafd
SD
1810 cx231xx_videodbg("video_open: setting radio device\n");
1811
1812 /* cx231xx_start_radio(dev); */
1813
b1196126 1814 call_all(dev, tuner, s_radio);
e0d3bafd
SD
1815 }
1816
1817 dev->users++;
1818
cde4362f
MCC
1819 if (fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1820 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_video_qops,
1821 NULL, &dev->video_mode.slock,
1822 fh->type, V4L2_FIELD_INTERLACED,
08bff03e 1823 sizeof(struct cx231xx_buffer),
643800d5 1824 fh, &dev->lock);
84b5dbf3 1825 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
cde4362f
MCC
1826 /* Set the required alternate setting VBI interface works in
1827 Bulk mode only */
2f861387 1828 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
e0d3bafd 1829
cde4362f
MCC
1830 videobuf_queue_vmalloc_init(&fh->vb_vidq, &cx231xx_vbi_qops,
1831 NULL, &dev->vbi_mode.slock,
1832 fh->type, V4L2_FIELD_SEQ_TB,
08bff03e 1833 sizeof(struct cx231xx_buffer),
643800d5 1834 fh, &dev->lock);
84b5dbf3 1835 }
1f7e073d 1836 mutex_unlock(&dev->lock);
1d08a4fa 1837 v4l2_fh_add(&fh->fh);
e0d3bafd 1838
4df16f70 1839 return 0;
e0d3bafd
SD
1840}
1841
1842/*
1843 * cx231xx_realease_resources()
1844 * unregisters the v4l2,i2c and usb devices
1845 * called when the device gets disconected or at module unload
1846*/
1847void cx231xx_release_analog_resources(struct cx231xx *dev)
1848{
1849
1850 /*FIXME: I2C IR should be disconnected */
1851
60acf187
HV
1852 if (video_is_registered(&dev->radio_dev))
1853 video_unregister_device(&dev->radio_dev);
1854 if (video_is_registered(&dev->vbi_dev)) {
336fea92 1855 dev_info(dev->dev, "V4L2 device %s deregistered\n",
60acf187
HV
1856 video_device_node_name(&dev->vbi_dev));
1857 video_unregister_device(&dev->vbi_dev);
e0d3bafd 1858 }
60acf187 1859 if (video_is_registered(&dev->vdev)) {
336fea92 1860 dev_info(dev->dev, "V4L2 device %s deregistered\n",
60acf187 1861 video_device_node_name(&dev->vdev));
64fbf444 1862
2f861387 1863 if (dev->board.has_417)
64fbf444
PB
1864 cx231xx_417_unregister(dev);
1865
60acf187 1866 video_unregister_device(&dev->vdev);
e0d3bafd 1867 }
d2370f8e
HV
1868 v4l2_ctrl_handler_free(&dev->ctrl_handler);
1869 v4l2_ctrl_handler_free(&dev->radio_ctrl_handler);
e0d3bafd
SD
1870}
1871
1872/*
1f7e073d 1873 * cx231xx_close()
e0d3bafd
SD
1874 * stops streaming and deallocates all resources allocated by the v4l2
1875 * calls and ioctls
1876 */
1f7e073d 1877static int cx231xx_close(struct file *filp)
e0d3bafd 1878{
84b5dbf3
MCC
1879 struct cx231xx_fh *fh = filp->private_data;
1880 struct cx231xx *dev = fh->dev;
e0d3bafd
SD
1881
1882 cx231xx_videodbg("users=%d\n", dev->users);
1883
64fbf444 1884 cx231xx_videodbg("users=%d\n", dev->users);
e0d3bafd
SD
1885 if (res_check(fh))
1886 res_free(fh);
1887
2f861387
MCC
1888 /*
1889 * To workaround error number=-71 on EP0 for VideoGrabber,
1890 * need exclude following.
1891 * FIXME: It is probably safe to remove most of these, as we're
1892 * now avoiding the alternate setting for INDEX_VANC
1893 */
1894 if (!dev->board.no_alt_vanc)
64fbf444
PB
1895 if (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1896 videobuf_stop(&fh->vb_vidq);
1897 videobuf_mmap_free(&fh->vb_vidq);
1898
1899 /* the device is already disconnect,
1900 free the remaining resources */
1901 if (dev->state & DEV_DISCONNECTED) {
1902 if (atomic_read(&dev->devlist_count) > 0) {
1903 cx231xx_release_resources(dev);
266e8ae3 1904 fh->dev = NULL;
64fbf444
PB
1905 return 0;
1906 }
64fbf444
PB
1907 return 0;
1908 }
84b5dbf3 1909
64fbf444
PB
1910 /* do this before setting alternate! */
1911 cx231xx_uninit_vbi_isoc(dev);
1912
1913 /* set alternate 0 */
1914 if (!dev->vbi_or_sliced_cc_mode)
1915 cx231xx_set_alt_setting(dev, INDEX_VANC, 0);
1916 else
1917 cx231xx_set_alt_setting(dev, INDEX_HANC, 0);
1918
1d08a4fa
HV
1919 v4l2_fh_del(&fh->fh);
1920 v4l2_fh_exit(&fh->fh);
64fbf444
PB
1921 kfree(fh);
1922 dev->users--;
543409a2 1923 wake_up_interruptible(&dev->open);
84b5dbf3
MCC
1924 return 0;
1925 }
1926
1d08a4fa 1927 v4l2_fh_del(&fh->fh);
990862a2
MCC
1928 dev->users--;
1929 if (!dev->users) {
e0d3bafd
SD
1930 videobuf_stop(&fh->vb_vidq);
1931 videobuf_mmap_free(&fh->vb_vidq);
1932
1933 /* the device is already disconnect,
1934 free the remaining resources */
1935 if (dev->state & DEV_DISCONNECTED) {
1936 cx231xx_release_resources(dev);
266e8ae3 1937 fh->dev = NULL;
e0d3bafd
SD
1938 return 0;
1939 }
1940
84b5dbf3 1941 /* Save some power by putting tuner to sleep */
622b828a 1942 call_all(dev, core, s_power, 0);
e0d3bafd
SD
1943
1944 /* do this before setting alternate! */
64fbf444
PB
1945 if (dev->USE_ISO)
1946 cx231xx_uninit_isoc(dev);
1947 else
1948 cx231xx_uninit_bulk(dev);
e0d3bafd
SD
1949 cx231xx_set_mode(dev, CX231XX_SUSPEND);
1950
1951 /* set alternate 0 */
1952 cx231xx_set_alt_setting(dev, INDEX_VIDEO, 0);
1953 }
1d08a4fa 1954 v4l2_fh_exit(&fh->fh);
e0d3bafd 1955 kfree(fh);
543409a2 1956 wake_up_interruptible(&dev->open);
e0d3bafd
SD
1957 return 0;
1958}
1959
1f7e073d
HV
1960static int cx231xx_v4l2_close(struct file *filp)
1961{
1962 struct cx231xx_fh *fh = filp->private_data;
1963 struct cx231xx *dev = fh->dev;
1964 int rc;
1965
1966 mutex_lock(&dev->lock);
1967 rc = cx231xx_close(filp);
1968 mutex_unlock(&dev->lock);
1969 return rc;
1970}
1971
e0d3bafd
SD
1972/*
1973 * cx231xx_v4l2_read()
1974 * will allocate buffers when called for the first time
1975 */
1976static ssize_t
cde4362f
MCC
1977cx231xx_v4l2_read(struct file *filp, char __user *buf, size_t count,
1978 loff_t *pos)
e0d3bafd
SD
1979{
1980 struct cx231xx_fh *fh = filp->private_data;
1981 struct cx231xx *dev = fh->dev;
1982 int rc;
1983
1984 rc = check_dev(dev);
1985 if (rc < 0)
1986 return rc;
1987
84b5dbf3
MCC
1988 if ((fh->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) ||
1989 (fh->type == V4L2_BUF_TYPE_VBI_CAPTURE)) {
e0d3bafd 1990 rc = res_get(fh);
e0d3bafd
SD
1991
1992 if (unlikely(rc < 0))
1993 return rc;
1994
1f7e073d
HV
1995 if (mutex_lock_interruptible(&dev->lock))
1996 return -ERESTARTSYS;
1997 rc = videobuf_read_stream(&fh->vb_vidq, buf, count, pos, 0,
84b5dbf3 1998 filp->f_flags & O_NONBLOCK);
1f7e073d
HV
1999 mutex_unlock(&dev->lock);
2000 return rc;
e0d3bafd
SD
2001 }
2002 return 0;
2003}
2004
2005/*
2006 * cx231xx_v4l2_poll()
2007 * will allocate buffers when called for the first time
2008 */
64fbf444 2009static unsigned int cx231xx_v4l2_poll(struct file *filp, poll_table *wait)
e0d3bafd 2010{
1d08a4fa 2011 unsigned long req_events = poll_requested_events(wait);
e0d3bafd
SD
2012 struct cx231xx_fh *fh = filp->private_data;
2013 struct cx231xx *dev = fh->dev;
1d08a4fa 2014 unsigned res = 0;
e0d3bafd
SD
2015 int rc;
2016
2017 rc = check_dev(dev);
2018 if (rc < 0)
1d08a4fa 2019 return POLLERR;
e0d3bafd 2020
e0d3bafd 2021 rc = res_get(fh);
e0d3bafd
SD
2022
2023 if (unlikely(rc < 0))
2024 return POLLERR;
2025
1d08a4fa
HV
2026 if (v4l2_event_pending(&fh->fh))
2027 res |= POLLPRI;
2028 else
2029 poll_wait(filp, &fh->fh.wait, wait);
2030
2031 if (!(req_events & (POLLIN | POLLRDNORM)))
2032 return res;
2033
84b5dbf3 2034 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == fh->type) ||
1f7e073d 2035 (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type)) {
1f7e073d 2036 mutex_lock(&dev->lock);
1d08a4fa 2037 res |= videobuf_poll_stream(filp, &fh->vb_vidq, wait);
1f7e073d
HV
2038 mutex_unlock(&dev->lock);
2039 return res;
2040 }
1d08a4fa 2041 return res | POLLERR;
e0d3bafd
SD
2042}
2043
2044/*
2045 * cx231xx_v4l2_mmap()
2046 */
2047static int cx231xx_v4l2_mmap(struct file *filp, struct vm_area_struct *vma)
2048{
84b5dbf3
MCC
2049 struct cx231xx_fh *fh = filp->private_data;
2050 struct cx231xx *dev = fh->dev;
2051 int rc;
e0d3bafd
SD
2052
2053 rc = check_dev(dev);
2054 if (rc < 0)
2055 return rc;
2056
e0d3bafd 2057 rc = res_get(fh);
e0d3bafd
SD
2058
2059 if (unlikely(rc < 0))
2060 return rc;
2061
1f7e073d
HV
2062 if (mutex_lock_interruptible(&dev->lock))
2063 return -ERESTARTSYS;
e0d3bafd 2064 rc = videobuf_mmap_mapper(&fh->vb_vidq, vma);
1f7e073d 2065 mutex_unlock(&dev->lock);
e0d3bafd
SD
2066
2067 cx231xx_videodbg("vma start=0x%08lx, size=%ld, ret=%d\n",
84b5dbf3
MCC
2068 (unsigned long)vma->vm_start,
2069 (unsigned long)vma->vm_end -
2070 (unsigned long)vma->vm_start, rc);
e0d3bafd
SD
2071
2072 return rc;
2073}
2074
2075static const struct v4l2_file_operations cx231xx_v4l_fops = {
cde4362f
MCC
2076 .owner = THIS_MODULE,
2077 .open = cx231xx_v4l2_open,
84b5dbf3 2078 .release = cx231xx_v4l2_close,
cde4362f
MCC
2079 .read = cx231xx_v4l2_read,
2080 .poll = cx231xx_v4l2_poll,
2081 .mmap = cx231xx_v4l2_mmap,
643800d5 2082 .unlocked_ioctl = video_ioctl2,
e0d3bafd
SD
2083};
2084
2085static const struct v4l2_ioctl_ops video_ioctl_ops = {
bc08734c 2086 .vidioc_querycap = cx231xx_querycap,
cde4362f
MCC
2087 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
2088 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
2089 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
2090 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
2091 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi_cap,
2092 .vidioc_try_fmt_vbi_cap = vidioc_try_fmt_vbi_cap,
6264722c 2093 .vidioc_s_fmt_vbi_cap = vidioc_s_fmt_vbi_cap,
cde4362f 2094 .vidioc_cropcap = vidioc_cropcap,
cde4362f
MCC
2095 .vidioc_reqbufs = vidioc_reqbufs,
2096 .vidioc_querybuf = vidioc_querybuf,
2097 .vidioc_qbuf = vidioc_qbuf,
2098 .vidioc_dqbuf = vidioc_dqbuf,
2099 .vidioc_s_std = vidioc_s_std,
2100 .vidioc_g_std = vidioc_g_std,
b86d1544
HV
2101 .vidioc_enum_input = cx231xx_enum_input,
2102 .vidioc_g_input = cx231xx_g_input,
2103 .vidioc_s_input = cx231xx_s_input,
cde4362f
MCC
2104 .vidioc_streamon = vidioc_streamon,
2105 .vidioc_streamoff = vidioc_streamoff,
b86d1544
HV
2106 .vidioc_g_tuner = cx231xx_g_tuner,
2107 .vidioc_s_tuner = cx231xx_s_tuner,
2108 .vidioc_g_frequency = cx231xx_g_frequency,
2109 .vidioc_s_frequency = cx231xx_s_frequency,
e0d3bafd 2110#ifdef CONFIG_VIDEO_ADV_DEBUG
08fe9f7d 2111 .vidioc_g_chip_info = cx231xx_g_chip_info,
b86d1544
HV
2112 .vidioc_g_register = cx231xx_g_register,
2113 .vidioc_s_register = cx231xx_s_register,
e0d3bafd 2114#endif
1d08a4fa
HV
2115 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2116 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
e0d3bafd
SD
2117};
2118
2119static struct video_device cx231xx_vbi_template;
2120
2121static const struct video_device cx231xx_video_template = {
cde4362f 2122 .fops = &cx231xx_v4l_fops,
60acf187 2123 .release = video_device_release_empty,
cde4362f 2124 .ioctl_ops = &video_ioctl_ops,
cde4362f 2125 .tvnorms = V4L2_STD_ALL,
e0d3bafd
SD
2126};
2127
2128static const struct v4l2_file_operations radio_fops = {
cde4362f
MCC
2129 .owner = THIS_MODULE,
2130 .open = cx231xx_v4l2_open,
84b5dbf3 2131 .release = cx231xx_v4l2_close,
1d08a4fa 2132 .poll = v4l2_ctrl_poll,
1265f080 2133 .unlocked_ioctl = video_ioctl2,
e0d3bafd
SD
2134};
2135
2136static const struct v4l2_ioctl_ops radio_ioctl_ops = {
bc08734c 2137 .vidioc_querycap = cx231xx_querycap,
cde4362f 2138 .vidioc_g_tuner = radio_g_tuner,
cde4362f 2139 .vidioc_s_tuner = radio_s_tuner,
b86d1544
HV
2140 .vidioc_g_frequency = cx231xx_g_frequency,
2141 .vidioc_s_frequency = cx231xx_s_frequency,
e0d3bafd 2142#ifdef CONFIG_VIDEO_ADV_DEBUG
08fe9f7d 2143 .vidioc_g_chip_info = cx231xx_g_chip_info,
b86d1544
HV
2144 .vidioc_g_register = cx231xx_g_register,
2145 .vidioc_s_register = cx231xx_s_register,
e0d3bafd 2146#endif
1d08a4fa
HV
2147 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
2148 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
e0d3bafd
SD
2149};
2150
2151static struct video_device cx231xx_radio_template = {
cde4362f
MCC
2152 .name = "cx231xx-radio",
2153 .fops = &radio_fops,
84b5dbf3 2154 .ioctl_ops = &radio_ioctl_ops,
e0d3bafd
SD
2155};
2156
2157/******************************** usb interface ******************************/
2158
60acf187
HV
2159static void cx231xx_vdev_init(struct cx231xx *dev,
2160 struct video_device *vfd,
2161 const struct video_device *template,
2162 const char *type_name)
e0d3bafd 2163{
e0d3bafd 2164 *vfd = *template;
b1196126 2165 vfd->v4l2_dev = &dev->v4l2_dev;
60acf187 2166 vfd->release = video_device_release_empty;
643800d5 2167 vfd->lock = &dev->lock;
e0d3bafd 2168
84b5dbf3 2169 snprintf(vfd->name, sizeof(vfd->name), "%s %s", dev->name, type_name);
e0d3bafd 2170
63b0d5ad 2171 video_set_drvdata(vfd, dev);
06c46003
HV
2172 if (dev->tuner_type == TUNER_ABSENT) {
2173 v4l2_disable_ioctl(vfd, VIDIOC_G_FREQUENCY);
2174 v4l2_disable_ioctl(vfd, VIDIOC_S_FREQUENCY);
2175 v4l2_disable_ioctl(vfd, VIDIOC_G_TUNER);
2176 v4l2_disable_ioctl(vfd, VIDIOC_S_TUNER);
2177 }
e0d3bafd
SD
2178}
2179
2180int cx231xx_register_analog_devices(struct cx231xx *dev)
2181{
2182 int ret;
2183
336fea92 2184 dev_info(dev->dev, "v4l2 driver version %s\n", CX231XX_VERSION);
e0d3bafd
SD
2185
2186 /* set default norm */
a25a7012 2187 dev->norm = V4L2_STD_PAL;
e0d3bafd
SD
2188 dev->width = norm_maxw(dev);
2189 dev->height = norm_maxh(dev);
2190 dev->interlaced = 0;
e0d3bafd
SD
2191
2192 /* Analog specific initialization */
2193 dev->format = &format[0];
6e6a2ba9
DH
2194
2195 /* Set the initial input */
2196 video_mux(dev, dev->video_input);
e0d3bafd 2197
8774bed9 2198 call_all(dev, video, s_std, dev->norm);
d61072a4 2199
d2370f8e
HV
2200 v4l2_ctrl_handler_init(&dev->ctrl_handler, 10);
2201 v4l2_ctrl_handler_init(&dev->radio_ctrl_handler, 5);
2202
2203 if (dev->sd_cx25840) {
2204 v4l2_ctrl_add_handler(&dev->ctrl_handler,
2205 dev->sd_cx25840->ctrl_handler, NULL);
2206 v4l2_ctrl_add_handler(&dev->radio_ctrl_handler,
2207 dev->sd_cx25840->ctrl_handler,
2208 v4l2_ctrl_radio_filter);
2209 }
2210
2211 if (dev->ctrl_handler.error)
2212 return dev->ctrl_handler.error;
2213 if (dev->radio_ctrl_handler.error)
2214 return dev->radio_ctrl_handler.error;
e0d3bafd
SD
2215
2216 /* enable vbi capturing */
84b5dbf3 2217 /* write code here... */
e0d3bafd
SD
2218
2219 /* allocate and fill video video_device struct */
60acf187 2220 cx231xx_vdev_init(dev, &dev->vdev, &cx231xx_video_template, "video");
b6a40e72
MCC
2221#if defined(CONFIG_MEDIA_CONTROLLER)
2222 dev->video_pad.flags = MEDIA_PAD_FL_SINK;
ab22e77c 2223 ret = media_entity_pads_init(&dev->vdev.entity, 1, &dev->video_pad);
b6a40e72
MCC
2224 if (ret < 0)
2225 dev_err(dev->dev, "failed to initialize video media entity!\n");
2226#endif
60acf187 2227 dev->vdev.ctrl_handler = &dev->ctrl_handler;
e0d3bafd 2228 /* register v4l2 video video_device */
60acf187 2229 ret = video_register_device(&dev->vdev, VFL_TYPE_GRABBER,
84b5dbf3 2230 video_nr[dev->devno]);
e0d3bafd 2231 if (ret) {
336fea92 2232 dev_err(dev->dev,
b7085c08 2233 "unable to register video device (error=%i).\n",
ed0e3729 2234 ret);
e0d3bafd
SD
2235 return ret;
2236 }
2237
336fea92 2238 dev_info(dev->dev, "Registered video device %s [v4l2]\n",
60acf187 2239 video_device_node_name(&dev->vdev));
e0d3bafd 2240
84b5dbf3 2241 /* Initialize VBI template */
3724dde9 2242 cx231xx_vbi_template = cx231xx_video_template;
84b5dbf3 2243 strcpy(cx231xx_vbi_template.name, "cx231xx-vbi");
e0d3bafd
SD
2244
2245 /* Allocate and fill vbi video_device struct */
60acf187 2246 cx231xx_vdev_init(dev, &dev->vbi_dev, &cx231xx_vbi_template, "vbi");
e0d3bafd 2247
b6a40e72
MCC
2248#if defined(CONFIG_MEDIA_CONTROLLER)
2249 dev->vbi_pad.flags = MEDIA_PAD_FL_SINK;
ab22e77c 2250 ret = media_entity_pads_init(&dev->vbi_dev.entity, 1, &dev->vbi_pad);
b6a40e72
MCC
2251 if (ret < 0)
2252 dev_err(dev->dev, "failed to initialize vbi media entity!\n");
2253#endif
60acf187 2254 dev->vbi_dev.ctrl_handler = &dev->ctrl_handler;
e0d3bafd 2255 /* register v4l2 vbi video_device */
60acf187 2256 ret = video_register_device(&dev->vbi_dev, VFL_TYPE_VBI,
84b5dbf3 2257 vbi_nr[dev->devno]);
e0d3bafd 2258 if (ret < 0) {
336fea92 2259 dev_err(dev->dev, "unable to register vbi device\n");
e0d3bafd
SD
2260 return ret;
2261 }
2262
336fea92 2263 dev_info(dev->dev, "Registered VBI device %s\n",
60acf187 2264 video_device_node_name(&dev->vbi_dev));
e0d3bafd
SD
2265
2266 if (cx231xx_boards[dev->model].radio.type == CX231XX_RADIO) {
60acf187
HV
2267 cx231xx_vdev_init(dev, &dev->radio_dev,
2268 &cx231xx_radio_template, "radio");
2269 dev->radio_dev.ctrl_handler = &dev->radio_ctrl_handler;
2270 ret = video_register_device(&dev->radio_dev, VFL_TYPE_RADIO,
e0d3bafd
SD
2271 radio_nr[dev->devno]);
2272 if (ret < 0) {
336fea92 2273 dev_err(dev->dev,
b7085c08 2274 "can't register radio device\n");
e0d3bafd
SD
2275 return ret;
2276 }
336fea92 2277 dev_info(dev->dev, "Registered radio device as %s\n",
60acf187 2278 video_device_node_name(&dev->radio_dev));
e0d3bafd
SD
2279 }
2280
e0d3bafd
SD
2281 return 0;
2282}
This page took 0.64278 seconds and 5 git commands to generate.