[media] davinci: vpif: make request_irq flags as shared
[deliverable/linux.git] / drivers / media / video / davinci / vpif_capture.c
1 /*
2 * Copyright (C) 2009 Texas Instruments Inc
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * TODO : add support for VBI & HBI data service
19 * add static buffer allocation
20 */
21 #include <linux/kernel.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
24 #include <linux/errno.h>
25 #include <linux/fs.h>
26 #include <linux/mm.h>
27 #include <linux/interrupt.h>
28 #include <linux/workqueue.h>
29 #include <linux/string.h>
30 #include <linux/videodev2.h>
31 #include <linux/wait.h>
32 #include <linux/time.h>
33 #include <linux/i2c.h>
34 #include <linux/platform_device.h>
35 #include <linux/io.h>
36 #include <linux/slab.h>
37 #include <media/v4l2-device.h>
38 #include <media/v4l2-ioctl.h>
39 #include <media/v4l2-chip-ident.h>
40
41 #include "vpif_capture.h"
42 #include "vpif.h"
43
44 MODULE_DESCRIPTION("TI DaVinci VPIF Capture driver");
45 MODULE_LICENSE("GPL");
46 MODULE_VERSION(VPIF_CAPTURE_VERSION);
47
48 #define vpif_err(fmt, arg...) v4l2_err(&vpif_obj.v4l2_dev, fmt, ## arg)
49 #define vpif_dbg(level, debug, fmt, arg...) \
50 v4l2_dbg(level, debug, &vpif_obj.v4l2_dev, fmt, ## arg)
51
52 static int debug = 1;
53 static u32 ch0_numbuffers = 3;
54 static u32 ch1_numbuffers = 3;
55 static u32 ch0_bufsize = 1920 * 1080 * 2;
56 static u32 ch1_bufsize = 720 * 576 * 2;
57
58 module_param(debug, int, 0644);
59 module_param(ch0_numbuffers, uint, S_IRUGO);
60 module_param(ch1_numbuffers, uint, S_IRUGO);
61 module_param(ch0_bufsize, uint, S_IRUGO);
62 module_param(ch1_bufsize, uint, S_IRUGO);
63
64 MODULE_PARM_DESC(debug, "Debug level 0-1");
65 MODULE_PARM_DESC(ch2_numbuffers, "Channel0 buffer count (default:3)");
66 MODULE_PARM_DESC(ch3_numbuffers, "Channel1 buffer count (default:3)");
67 MODULE_PARM_DESC(ch2_bufsize, "Channel0 buffer size (default:1920 x 1080 x 2)");
68 MODULE_PARM_DESC(ch3_bufsize, "Channel1 buffer size (default:720 x 576 x 2)");
69
70 static struct vpif_config_params config_params = {
71 .min_numbuffers = 3,
72 .numbuffers[0] = 3,
73 .numbuffers[1] = 3,
74 .min_bufsize[0] = 720 * 480 * 2,
75 .min_bufsize[1] = 720 * 480 * 2,
76 .channel_bufsize[0] = 1920 * 1080 * 2,
77 .channel_bufsize[1] = 720 * 576 * 2,
78 };
79
80 /* global variables */
81 static struct vpif_device vpif_obj = { {NULL} };
82 static struct device *vpif_dev;
83
84 /**
85 * vpif_uservirt_to_phys : translate user/virtual address to phy address
86 * @virtp: user/virtual address
87 *
88 * This inline function is used to convert user space virtual address to
89 * physical address.
90 */
91 static inline u32 vpif_uservirt_to_phys(u32 virtp)
92 {
93 unsigned long physp = 0;
94 struct mm_struct *mm = current->mm;
95 struct vm_area_struct *vma;
96
97 vma = find_vma(mm, virtp);
98
99 /* For kernel direct-mapped memory, take the easy way */
100 if (virtp >= PAGE_OFFSET)
101 physp = virt_to_phys((void *)virtp);
102 else if (vma && (vma->vm_flags & VM_IO) && (vma->vm_pgoff))
103 /**
104 * this will catch, kernel-allocated, mmaped-to-usermode
105 * addresses
106 */
107 physp = (vma->vm_pgoff << PAGE_SHIFT) + (virtp - vma->vm_start);
108 else {
109 /* otherwise, use get_user_pages() for general userland pages */
110 int res, nr_pages = 1;
111 struct page *pages;
112
113 down_read(&current->mm->mmap_sem);
114
115 res = get_user_pages(current, current->mm,
116 virtp, nr_pages, 1, 0, &pages, NULL);
117 up_read(&current->mm->mmap_sem);
118
119 if (res == nr_pages)
120 physp = __pa(page_address(&pages[0]) +
121 (virtp & ~PAGE_MASK));
122 else {
123 vpif_err("get_user_pages failed\n");
124 return 0;
125 }
126 }
127 return physp;
128 }
129
130 /**
131 * buffer_prepare : callback function for buffer prepare
132 * @q : buffer queue ptr
133 * @vb: ptr to video buffer
134 * @field: field info
135 *
136 * This is the callback function for buffer prepare when videobuf_qbuf()
137 * function is called. The buffer is prepared and user space virtual address
138 * or user address is converted into physical address
139 */
140 static int vpif_buffer_prepare(struct videobuf_queue *q,
141 struct videobuf_buffer *vb,
142 enum v4l2_field field)
143 {
144 /* Get the file handle object and channel object */
145 struct vpif_fh *fh = q->priv_data;
146 struct channel_obj *ch = fh->channel;
147 struct common_obj *common;
148 unsigned long addr;
149
150
151 vpif_dbg(2, debug, "vpif_buffer_prepare\n");
152
153 common = &ch->common[VPIF_VIDEO_INDEX];
154
155 /* If buffer is not initialized, initialize it */
156 if (VIDEOBUF_NEEDS_INIT == vb->state) {
157 vb->width = common->width;
158 vb->height = common->height;
159 vb->size = vb->width * vb->height;
160 vb->field = field;
161 }
162 vb->state = VIDEOBUF_PREPARED;
163 /**
164 * if user pointer memory mechanism is used, get the physical
165 * address of the buffer
166 */
167 if (V4L2_MEMORY_USERPTR == common->memory) {
168 if (0 == vb->baddr) {
169 vpif_dbg(1, debug, "buffer address is 0\n");
170 return -EINVAL;
171
172 }
173 vb->boff = vpif_uservirt_to_phys(vb->baddr);
174 if (!IS_ALIGNED(vb->boff, 8))
175 goto exit;
176 }
177
178 addr = vb->boff;
179 if (q->streaming) {
180 if (!IS_ALIGNED((addr + common->ytop_off), 8) ||
181 !IS_ALIGNED((addr + common->ybtm_off), 8) ||
182 !IS_ALIGNED((addr + common->ctop_off), 8) ||
183 !IS_ALIGNED((addr + common->cbtm_off), 8))
184 goto exit;
185 }
186 return 0;
187 exit:
188 vpif_dbg(1, debug, "buffer_prepare:offset is not aligned to 8 bytes\n");
189 return -EINVAL;
190 }
191
192 /**
193 * vpif_buffer_setup : Callback function for buffer setup.
194 * @q: buffer queue ptr
195 * @count: number of buffers
196 * @size: size of the buffer
197 *
198 * This callback function is called when reqbuf() is called to adjust
199 * the buffer count and buffer size
200 */
201 static int vpif_buffer_setup(struct videobuf_queue *q, unsigned int *count,
202 unsigned int *size)
203 {
204 /* Get the file handle object and channel object */
205 struct vpif_fh *fh = q->priv_data;
206 struct channel_obj *ch = fh->channel;
207 struct common_obj *common;
208
209 common = &ch->common[VPIF_VIDEO_INDEX];
210
211 vpif_dbg(2, debug, "vpif_buffer_setup\n");
212
213 /* If memory type is not mmap, return */
214 if (V4L2_MEMORY_MMAP != common->memory)
215 return 0;
216
217 /* Calculate the size of the buffer */
218 *size = config_params.channel_bufsize[ch->channel_id];
219
220 if (*count < config_params.min_numbuffers)
221 *count = config_params.min_numbuffers;
222 return 0;
223 }
224
225 /**
226 * vpif_buffer_queue : Callback function to add buffer to DMA queue
227 * @q: ptr to videobuf_queue
228 * @vb: ptr to videobuf_buffer
229 */
230 static void vpif_buffer_queue(struct videobuf_queue *q,
231 struct videobuf_buffer *vb)
232 {
233 /* Get the file handle object and channel object */
234 struct vpif_fh *fh = q->priv_data;
235 struct channel_obj *ch = fh->channel;
236 struct common_obj *common;
237
238 common = &ch->common[VPIF_VIDEO_INDEX];
239
240 vpif_dbg(2, debug, "vpif_buffer_queue\n");
241
242 /* add the buffer to the DMA queue */
243 list_add_tail(&vb->queue, &common->dma_queue);
244 /* Change state of the buffer */
245 vb->state = VIDEOBUF_QUEUED;
246 }
247
248 /**
249 * vpif_buffer_release : Callback function to free buffer
250 * @q: buffer queue ptr
251 * @vb: ptr to video buffer
252 *
253 * This function is called from the videobuf layer to free memory
254 * allocated to the buffers
255 */
256 static void vpif_buffer_release(struct videobuf_queue *q,
257 struct videobuf_buffer *vb)
258 {
259 /* Get the file handle object and channel object */
260 struct vpif_fh *fh = q->priv_data;
261 struct channel_obj *ch = fh->channel;
262 struct common_obj *common;
263
264 common = &ch->common[VPIF_VIDEO_INDEX];
265
266 videobuf_dma_contig_free(q, vb);
267 vb->state = VIDEOBUF_NEEDS_INIT;
268 }
269
270 static struct videobuf_queue_ops video_qops = {
271 .buf_setup = vpif_buffer_setup,
272 .buf_prepare = vpif_buffer_prepare,
273 .buf_queue = vpif_buffer_queue,
274 .buf_release = vpif_buffer_release,
275 };
276
277 static u8 channel_first_int[VPIF_NUMBER_OF_OBJECTS][2] =
278 { {1, 1} };
279
280 /**
281 * vpif_process_buffer_complete: process a completed buffer
282 * @common: ptr to common channel object
283 *
284 * This function time stamp the buffer and mark it as DONE. It also
285 * wake up any process waiting on the QUEUE and set the next buffer
286 * as current
287 */
288 static void vpif_process_buffer_complete(struct common_obj *common)
289 {
290 do_gettimeofday(&common->cur_frm->ts);
291 common->cur_frm->state = VIDEOBUF_DONE;
292 wake_up_interruptible(&common->cur_frm->done);
293 /* Make curFrm pointing to nextFrm */
294 common->cur_frm = common->next_frm;
295 }
296
297 /**
298 * vpif_schedule_next_buffer: set next buffer address for capture
299 * @common : ptr to common channel object
300 *
301 * This function will get next buffer from the dma queue and
302 * set the buffer address in the vpif register for capture.
303 * the buffer is marked active
304 */
305 static void vpif_schedule_next_buffer(struct common_obj *common)
306 {
307 unsigned long addr = 0;
308
309 common->next_frm = list_entry(common->dma_queue.next,
310 struct videobuf_buffer, queue);
311 /* Remove that buffer from the buffer queue */
312 list_del(&common->next_frm->queue);
313 common->next_frm->state = VIDEOBUF_ACTIVE;
314 if (V4L2_MEMORY_USERPTR == common->memory)
315 addr = common->next_frm->boff;
316 else
317 addr = videobuf_to_dma_contig(common->next_frm);
318
319 /* Set top and bottom field addresses in VPIF registers */
320 common->set_addr(addr + common->ytop_off,
321 addr + common->ybtm_off,
322 addr + common->ctop_off,
323 addr + common->cbtm_off);
324 }
325
326 /**
327 * vpif_channel_isr : ISR handler for vpif capture
328 * @irq: irq number
329 * @dev_id: dev_id ptr
330 *
331 * It changes status of the captured buffer, takes next buffer from the queue
332 * and sets its address in VPIF registers
333 */
334 static irqreturn_t vpif_channel_isr(int irq, void *dev_id)
335 {
336 struct vpif_device *dev = &vpif_obj;
337 struct common_obj *common;
338 struct channel_obj *ch;
339 enum v4l2_field field;
340 int channel_id = 0;
341 int fid = -1, i;
342
343 channel_id = *(int *)(dev_id);
344 if (!vpif_intr_status(channel_id))
345 return IRQ_NONE;
346
347 ch = dev->dev[channel_id];
348
349 field = ch->common[VPIF_VIDEO_INDEX].fmt.fmt.pix.field;
350
351 for (i = 0; i < VPIF_NUMBER_OF_OBJECTS; i++) {
352 common = &ch->common[i];
353 /* skip If streaming is not started in this channel */
354 if (0 == common->started)
355 continue;
356
357 /* Check the field format */
358 if (1 == ch->vpifparams.std_info.frm_fmt) {
359 /* Progressive mode */
360 if (list_empty(&common->dma_queue))
361 continue;
362
363 if (!channel_first_int[i][channel_id])
364 vpif_process_buffer_complete(common);
365
366 channel_first_int[i][channel_id] = 0;
367
368 vpif_schedule_next_buffer(common);
369
370
371 channel_first_int[i][channel_id] = 0;
372 } else {
373 /**
374 * Interlaced mode. If it is first interrupt, ignore
375 * it
376 */
377 if (channel_first_int[i][channel_id]) {
378 channel_first_int[i][channel_id] = 0;
379 continue;
380 }
381 if (0 == i) {
382 ch->field_id ^= 1;
383 /* Get field id from VPIF registers */
384 fid = vpif_channel_getfid(ch->channel_id);
385 if (fid != ch->field_id) {
386 /**
387 * If field id does not match stored
388 * field id, make them in sync
389 */
390 if (0 == fid)
391 ch->field_id = fid;
392 return IRQ_HANDLED;
393 }
394 }
395 /* device field id and local field id are in sync */
396 if (0 == fid) {
397 /* this is even field */
398 if (common->cur_frm == common->next_frm)
399 continue;
400
401 /* mark the current buffer as done */
402 vpif_process_buffer_complete(common);
403 } else if (1 == fid) {
404 /* odd field */
405 if (list_empty(&common->dma_queue) ||
406 (common->cur_frm != common->next_frm))
407 continue;
408
409 vpif_schedule_next_buffer(common);
410 }
411 }
412 }
413 return IRQ_HANDLED;
414 }
415
416 /**
417 * vpif_update_std_info() - update standard related info
418 * @ch: ptr to channel object
419 *
420 * For a given standard selected by application, update values
421 * in the device data structures
422 */
423 static int vpif_update_std_info(struct channel_obj *ch)
424 {
425 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
426 struct vpif_params *vpifparams = &ch->vpifparams;
427 const struct vpif_channel_config_params *config;
428 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
429 struct video_obj *vid_ch = &ch->video;
430 int index;
431
432 vpif_dbg(2, debug, "vpif_update_std_info\n");
433
434 for (index = 0; index < vpif_ch_params_count; index++) {
435 config = &ch_params[index];
436 if (config->hd_sd == 0) {
437 vpif_dbg(2, debug, "SD format\n");
438 if (config->stdid & vid_ch->stdid) {
439 memcpy(std_info, config, sizeof(*config));
440 break;
441 }
442 } else {
443 vpif_dbg(2, debug, "HD format\n");
444 if (config->dv_preset == vid_ch->dv_preset) {
445 memcpy(std_info, config, sizeof(*config));
446 break;
447 }
448 }
449 }
450
451 /* standard not found */
452 if (index == vpif_ch_params_count)
453 return -EINVAL;
454
455 common->fmt.fmt.pix.width = std_info->width;
456 common->width = std_info->width;
457 common->fmt.fmt.pix.height = std_info->height;
458 common->height = std_info->height;
459 common->fmt.fmt.pix.bytesperline = std_info->width;
460 vpifparams->video_params.hpitch = std_info->width;
461 vpifparams->video_params.storage_mode = std_info->frm_fmt;
462
463 return 0;
464 }
465
466 /**
467 * vpif_calculate_offsets : This function calculates buffers offsets
468 * @ch : ptr to channel object
469 *
470 * This function calculates buffer offsets for Y and C in the top and
471 * bottom field
472 */
473 static void vpif_calculate_offsets(struct channel_obj *ch)
474 {
475 unsigned int hpitch, vpitch, sizeimage;
476 struct video_obj *vid_ch = &(ch->video);
477 struct vpif_params *vpifparams = &ch->vpifparams;
478 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
479 enum v4l2_field field = common->fmt.fmt.pix.field;
480
481 vpif_dbg(2, debug, "vpif_calculate_offsets\n");
482
483 if (V4L2_FIELD_ANY == field) {
484 if (vpifparams->std_info.frm_fmt)
485 vid_ch->buf_field = V4L2_FIELD_NONE;
486 else
487 vid_ch->buf_field = V4L2_FIELD_INTERLACED;
488 } else
489 vid_ch->buf_field = common->fmt.fmt.pix.field;
490
491 if (V4L2_MEMORY_USERPTR == common->memory)
492 sizeimage = common->fmt.fmt.pix.sizeimage;
493 else
494 sizeimage = config_params.channel_bufsize[ch->channel_id];
495
496 hpitch = common->fmt.fmt.pix.bytesperline;
497 vpitch = sizeimage / (hpitch * 2);
498
499 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
500 (V4L2_FIELD_INTERLACED == vid_ch->buf_field)) {
501 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
502 common->ytop_off = 0;
503 common->ybtm_off = hpitch;
504 common->ctop_off = sizeimage / 2;
505 common->cbtm_off = sizeimage / 2 + hpitch;
506 } else if (V4L2_FIELD_SEQ_TB == vid_ch->buf_field) {
507 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
508 common->ytop_off = 0;
509 common->ybtm_off = sizeimage / 4;
510 common->ctop_off = sizeimage / 2;
511 common->cbtm_off = common->ctop_off + sizeimage / 4;
512 } else if (V4L2_FIELD_SEQ_BT == vid_ch->buf_field) {
513 /* Calculate offsets for Y top, Y Bottom, C top and C Bottom */
514 common->ybtm_off = 0;
515 common->ytop_off = sizeimage / 4;
516 common->cbtm_off = sizeimage / 2;
517 common->ctop_off = common->cbtm_off + sizeimage / 4;
518 }
519 if ((V4L2_FIELD_NONE == vid_ch->buf_field) ||
520 (V4L2_FIELD_INTERLACED == vid_ch->buf_field))
521 vpifparams->video_params.storage_mode = 1;
522 else
523 vpifparams->video_params.storage_mode = 0;
524
525 if (1 == vpifparams->std_info.frm_fmt)
526 vpifparams->video_params.hpitch =
527 common->fmt.fmt.pix.bytesperline;
528 else {
529 if ((field == V4L2_FIELD_ANY)
530 || (field == V4L2_FIELD_INTERLACED))
531 vpifparams->video_params.hpitch =
532 common->fmt.fmt.pix.bytesperline * 2;
533 else
534 vpifparams->video_params.hpitch =
535 common->fmt.fmt.pix.bytesperline;
536 }
537
538 ch->vpifparams.video_params.stdid = vpifparams->std_info.stdid;
539 }
540
541 /**
542 * vpif_config_format: configure default frame format in the device
543 * ch : ptr to channel object
544 */
545 static void vpif_config_format(struct channel_obj *ch)
546 {
547 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
548
549 vpif_dbg(2, debug, "vpif_config_format\n");
550
551 common->fmt.fmt.pix.field = V4L2_FIELD_ANY;
552 if (config_params.numbuffers[ch->channel_id] == 0)
553 common->memory = V4L2_MEMORY_USERPTR;
554 else
555 common->memory = V4L2_MEMORY_MMAP;
556
557 common->fmt.fmt.pix.sizeimage
558 = config_params.channel_bufsize[ch->channel_id];
559
560 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER)
561 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_SBGGR8;
562 else
563 common->fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV422P;
564 common->fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
565 }
566
567 /**
568 * vpif_get_default_field() - Get default field type based on interface
569 * @vpif_params - ptr to vpif params
570 */
571 static inline enum v4l2_field vpif_get_default_field(
572 struct vpif_interface *iface)
573 {
574 return (iface->if_type == VPIF_IF_RAW_BAYER) ? V4L2_FIELD_NONE :
575 V4L2_FIELD_INTERLACED;
576 }
577
578 /**
579 * vpif_check_format() - check given pixel format for compatibility
580 * @ch - channel ptr
581 * @pixfmt - Given pixel format
582 * @update - update the values as per hardware requirement
583 *
584 * Check the application pixel format for S_FMT and update the input
585 * values as per hardware limits for TRY_FMT. The default pixel and
586 * field format is selected based on interface type.
587 */
588 static int vpif_check_format(struct channel_obj *ch,
589 struct v4l2_pix_format *pixfmt,
590 int update)
591 {
592 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
593 struct vpif_params *vpif_params = &ch->vpifparams;
594 enum v4l2_field field = pixfmt->field;
595 u32 sizeimage, hpitch, vpitch;
596 int ret = -EINVAL;
597
598 vpif_dbg(2, debug, "vpif_check_format\n");
599 /**
600 * first check for the pixel format. If if_type is Raw bayer,
601 * only V4L2_PIX_FMT_SBGGR8 format is supported. Otherwise only
602 * V4L2_PIX_FMT_YUV422P is supported
603 */
604 if (vpif_params->iface.if_type == VPIF_IF_RAW_BAYER) {
605 if (pixfmt->pixelformat != V4L2_PIX_FMT_SBGGR8) {
606 if (!update) {
607 vpif_dbg(2, debug, "invalid pix format\n");
608 goto exit;
609 }
610 pixfmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
611 }
612 } else {
613 if (pixfmt->pixelformat != V4L2_PIX_FMT_YUV422P) {
614 if (!update) {
615 vpif_dbg(2, debug, "invalid pixel format\n");
616 goto exit;
617 }
618 pixfmt->pixelformat = V4L2_PIX_FMT_YUV422P;
619 }
620 }
621
622 if (!(VPIF_VALID_FIELD(field))) {
623 if (!update) {
624 vpif_dbg(2, debug, "invalid field format\n");
625 goto exit;
626 }
627 /**
628 * By default use FIELD_NONE for RAW Bayer capture
629 * and FIELD_INTERLACED for other interfaces
630 */
631 field = vpif_get_default_field(&vpif_params->iface);
632 } else if (field == V4L2_FIELD_ANY)
633 /* unsupported field. Use default */
634 field = vpif_get_default_field(&vpif_params->iface);
635
636 /* validate the hpitch */
637 hpitch = pixfmt->bytesperline;
638 if (hpitch < vpif_params->std_info.width) {
639 if (!update) {
640 vpif_dbg(2, debug, "invalid hpitch\n");
641 goto exit;
642 }
643 hpitch = vpif_params->std_info.width;
644 }
645
646 if (V4L2_MEMORY_USERPTR == common->memory)
647 sizeimage = pixfmt->sizeimage;
648 else
649 sizeimage = config_params.channel_bufsize[ch->channel_id];
650
651 vpitch = sizeimage / (hpitch * 2);
652
653 /* validate the vpitch */
654 if (vpitch < vpif_params->std_info.height) {
655 if (!update) {
656 vpif_dbg(2, debug, "Invalid vpitch\n");
657 goto exit;
658 }
659 vpitch = vpif_params->std_info.height;
660 }
661
662 /* Check for 8 byte alignment */
663 if (!ALIGN(hpitch, 8)) {
664 if (!update) {
665 vpif_dbg(2, debug, "invalid pitch alignment\n");
666 goto exit;
667 }
668 /* adjust to next 8 byte boundary */
669 hpitch = (((hpitch + 7) / 8) * 8);
670 }
671 /* if update is set, modify the bytesperline and sizeimage */
672 if (update) {
673 pixfmt->bytesperline = hpitch;
674 pixfmt->sizeimage = hpitch * vpitch * 2;
675 }
676 /**
677 * Image width and height is always based on current standard width and
678 * height
679 */
680 pixfmt->width = common->fmt.fmt.pix.width;
681 pixfmt->height = common->fmt.fmt.pix.height;
682 return 0;
683 exit:
684 return ret;
685 }
686
687 /**
688 * vpif_config_addr() - function to configure buffer address in vpif
689 * @ch - channel ptr
690 * @muxmode - channel mux mode
691 */
692 static void vpif_config_addr(struct channel_obj *ch, int muxmode)
693 {
694 struct common_obj *common;
695
696 vpif_dbg(2, debug, "vpif_config_addr\n");
697
698 common = &(ch->common[VPIF_VIDEO_INDEX]);
699
700 if (VPIF_CHANNEL1_VIDEO == ch->channel_id)
701 common->set_addr = ch1_set_videobuf_addr;
702 else if (2 == muxmode)
703 common->set_addr = ch0_set_videobuf_addr_yc_nmux;
704 else
705 common->set_addr = ch0_set_videobuf_addr;
706 }
707
708 /**
709 * vpfe_mmap : It is used to map kernel space buffers into user spaces
710 * @filep: file pointer
711 * @vma: ptr to vm_area_struct
712 */
713 static int vpif_mmap(struct file *filep, struct vm_area_struct *vma)
714 {
715 /* Get the channel object and file handle object */
716 struct vpif_fh *fh = filep->private_data;
717 struct channel_obj *ch = fh->channel;
718 struct common_obj *common = &(ch->common[VPIF_VIDEO_INDEX]);
719
720 vpif_dbg(2, debug, "vpif_mmap\n");
721
722 return videobuf_mmap_mapper(&common->buffer_queue, vma);
723 }
724
725 /**
726 * vpif_poll: It is used for select/poll system call
727 * @filep: file pointer
728 * @wait: poll table to wait
729 */
730 static unsigned int vpif_poll(struct file *filep, poll_table * wait)
731 {
732 struct vpif_fh *fh = filep->private_data;
733 struct channel_obj *channel = fh->channel;
734 struct common_obj *common = &(channel->common[VPIF_VIDEO_INDEX]);
735
736 vpif_dbg(2, debug, "vpif_poll\n");
737
738 if (common->started)
739 return videobuf_poll_stream(filep, &common->buffer_queue, wait);
740 return 0;
741 }
742
743 /**
744 * vpif_open : vpif open handler
745 * @filep: file ptr
746 *
747 * It creates object of file handle structure and stores it in private_data
748 * member of filepointer
749 */
750 static int vpif_open(struct file *filep)
751 {
752 struct vpif_capture_config *config = vpif_dev->platform_data;
753 struct video_device *vdev = video_devdata(filep);
754 struct common_obj *common;
755 struct video_obj *vid_ch;
756 struct channel_obj *ch;
757 struct vpif_fh *fh;
758 int i;
759
760 vpif_dbg(2, debug, "vpif_open\n");
761
762 ch = video_get_drvdata(vdev);
763
764 vid_ch = &ch->video;
765 common = &ch->common[VPIF_VIDEO_INDEX];
766
767 if (NULL == ch->curr_subdev_info) {
768 /**
769 * search through the sub device to see a registered
770 * sub device and make it as current sub device
771 */
772 for (i = 0; i < config->subdev_count; i++) {
773 if (vpif_obj.sd[i]) {
774 /* the sub device is registered */
775 ch->curr_subdev_info = &config->subdev_info[i];
776 /* make first input as the current input */
777 vid_ch->input_idx = 0;
778 break;
779 }
780 }
781 if (i == config->subdev_count) {
782 vpif_err("No sub device registered\n");
783 return -ENOENT;
784 }
785 }
786
787 /* Allocate memory for the file handle object */
788 fh = kzalloc(sizeof(struct vpif_fh), GFP_KERNEL);
789 if (NULL == fh) {
790 vpif_err("unable to allocate memory for file handle object\n");
791 return -ENOMEM;
792 }
793
794 /* store pointer to fh in private_data member of filep */
795 filep->private_data = fh;
796 fh->channel = ch;
797 fh->initialized = 0;
798 /* If decoder is not initialized. initialize it */
799 if (!ch->initialized) {
800 fh->initialized = 1;
801 ch->initialized = 1;
802 memset(&(ch->vpifparams), 0, sizeof(struct vpif_params));
803 }
804 /* Increment channel usrs counter */
805 ch->usrs++;
806 /* Set io_allowed member to false */
807 fh->io_allowed[VPIF_VIDEO_INDEX] = 0;
808 /* Initialize priority of this instance to default priority */
809 fh->prio = V4L2_PRIORITY_UNSET;
810 v4l2_prio_open(&ch->prio, &fh->prio);
811 return 0;
812 }
813
814 /**
815 * vpif_release : function to clean up file close
816 * @filep: file pointer
817 *
818 * This function deletes buffer queue, frees the buffers and the vpfe file
819 * handle
820 */
821 static int vpif_release(struct file *filep)
822 {
823 struct vpif_fh *fh = filep->private_data;
824 struct channel_obj *ch = fh->channel;
825 struct common_obj *common;
826
827 vpif_dbg(2, debug, "vpif_release\n");
828
829 common = &ch->common[VPIF_VIDEO_INDEX];
830
831 /* if this instance is doing IO */
832 if (fh->io_allowed[VPIF_VIDEO_INDEX]) {
833 /* Reset io_usrs member of channel object */
834 common->io_usrs = 0;
835 /* Disable channel as per its device type and channel id */
836 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
837 enable_channel0(0);
838 channel0_intr_enable(0);
839 }
840 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
841 (2 == common->started)) {
842 enable_channel1(0);
843 channel1_intr_enable(0);
844 }
845 common->started = 0;
846 /* Free buffers allocated */
847 videobuf_queue_cancel(&common->buffer_queue);
848 videobuf_mmap_free(&common->buffer_queue);
849 }
850
851 /* Decrement channel usrs counter */
852 ch->usrs--;
853
854 /* Close the priority */
855 v4l2_prio_close(&ch->prio, fh->prio);
856
857 if (fh->initialized)
858 ch->initialized = 0;
859
860 filep->private_data = NULL;
861 kfree(fh);
862 return 0;
863 }
864
865 /**
866 * vpif_reqbufs() - request buffer handler
867 * @file: file ptr
868 * @priv: file handle
869 * @reqbuf: request buffer structure ptr
870 */
871 static int vpif_reqbufs(struct file *file, void *priv,
872 struct v4l2_requestbuffers *reqbuf)
873 {
874 struct vpif_fh *fh = priv;
875 struct channel_obj *ch = fh->channel;
876 struct common_obj *common;
877 u8 index = 0;
878
879 vpif_dbg(2, debug, "vpif_reqbufs\n");
880
881 /**
882 * This file handle has not initialized the channel,
883 * It is not allowed to do settings
884 */
885 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)
886 || (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
887 if (!fh->initialized) {
888 vpif_dbg(1, debug, "Channel Busy\n");
889 return -EBUSY;
890 }
891 }
892
893 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != reqbuf->type)
894 return -EINVAL;
895
896 index = VPIF_VIDEO_INDEX;
897
898 common = &ch->common[index];
899
900 if (0 != common->io_usrs)
901 return -EBUSY;
902
903 /* Initialize videobuf queue as per the buffer type */
904 videobuf_queue_dma_contig_init(&common->buffer_queue,
905 &video_qops, NULL,
906 &common->irqlock,
907 reqbuf->type,
908 common->fmt.fmt.pix.field,
909 sizeof(struct videobuf_buffer), fh,
910 &common->lock);
911
912 /* Set io allowed member of file handle to TRUE */
913 fh->io_allowed[index] = 1;
914 /* Increment io usrs member of channel object to 1 */
915 common->io_usrs = 1;
916 /* Store type of memory requested in channel object */
917 common->memory = reqbuf->memory;
918 INIT_LIST_HEAD(&common->dma_queue);
919
920 /* Allocate buffers */
921 return videobuf_reqbufs(&common->buffer_queue, reqbuf);
922 }
923
924 /**
925 * vpif_querybuf() - query buffer handler
926 * @file: file ptr
927 * @priv: file handle
928 * @buf: v4l2 buffer structure ptr
929 */
930 static int vpif_querybuf(struct file *file, void *priv,
931 struct v4l2_buffer *buf)
932 {
933 struct vpif_fh *fh = priv;
934 struct channel_obj *ch = fh->channel;
935 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
936
937 vpif_dbg(2, debug, "vpif_querybuf\n");
938
939 if (common->fmt.type != buf->type)
940 return -EINVAL;
941
942 if (common->memory != V4L2_MEMORY_MMAP) {
943 vpif_dbg(1, debug, "Invalid memory\n");
944 return -EINVAL;
945 }
946
947 return videobuf_querybuf(&common->buffer_queue, buf);
948 }
949
950 /**
951 * vpif_qbuf() - query buffer handler
952 * @file: file ptr
953 * @priv: file handle
954 * @buf: v4l2 buffer structure ptr
955 */
956 static int vpif_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
957 {
958
959 struct vpif_fh *fh = priv;
960 struct channel_obj *ch = fh->channel;
961 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
962 struct v4l2_buffer tbuf = *buf;
963 struct videobuf_buffer *buf1;
964 unsigned long addr = 0;
965 unsigned long flags;
966 int ret = 0;
967
968 vpif_dbg(2, debug, "vpif_qbuf\n");
969
970 if (common->fmt.type != tbuf.type) {
971 vpif_err("invalid buffer type\n");
972 return -EINVAL;
973 }
974
975 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
976 vpif_err("fh io not allowed \n");
977 return -EACCES;
978 }
979
980 if (!(list_empty(&common->dma_queue)) ||
981 (common->cur_frm != common->next_frm) ||
982 !common->started ||
983 (common->started && (0 == ch->field_id)))
984 return videobuf_qbuf(&common->buffer_queue, buf);
985
986 /* bufferqueue is empty store buffer address in VPIF registers */
987 mutex_lock(&common->buffer_queue.vb_lock);
988 buf1 = common->buffer_queue.bufs[tbuf.index];
989
990 if ((buf1->state == VIDEOBUF_QUEUED) ||
991 (buf1->state == VIDEOBUF_ACTIVE)) {
992 vpif_err("invalid state\n");
993 goto qbuf_exit;
994 }
995
996 switch (buf1->memory) {
997 case V4L2_MEMORY_MMAP:
998 if (buf1->baddr == 0)
999 goto qbuf_exit;
1000 break;
1001
1002 case V4L2_MEMORY_USERPTR:
1003 if (tbuf.length < buf1->bsize)
1004 goto qbuf_exit;
1005
1006 if ((VIDEOBUF_NEEDS_INIT != buf1->state)
1007 && (buf1->baddr != tbuf.m.userptr)) {
1008 vpif_buffer_release(&common->buffer_queue, buf1);
1009 buf1->baddr = tbuf.m.userptr;
1010 }
1011 break;
1012
1013 default:
1014 goto qbuf_exit;
1015 }
1016
1017 local_irq_save(flags);
1018 ret = vpif_buffer_prepare(&common->buffer_queue, buf1,
1019 common->buffer_queue.field);
1020 if (ret < 0) {
1021 local_irq_restore(flags);
1022 goto qbuf_exit;
1023 }
1024
1025 buf1->state = VIDEOBUF_ACTIVE;
1026
1027 if (V4L2_MEMORY_USERPTR == common->memory)
1028 addr = buf1->boff;
1029 else
1030 addr = videobuf_to_dma_contig(buf1);
1031
1032 common->next_frm = buf1;
1033 common->set_addr(addr + common->ytop_off,
1034 addr + common->ybtm_off,
1035 addr + common->ctop_off,
1036 addr + common->cbtm_off);
1037
1038 local_irq_restore(flags);
1039 list_add_tail(&buf1->stream, &common->buffer_queue.stream);
1040 mutex_unlock(&common->buffer_queue.vb_lock);
1041 return 0;
1042
1043 qbuf_exit:
1044 mutex_unlock(&common->buffer_queue.vb_lock);
1045 return -EINVAL;
1046 }
1047
1048 /**
1049 * vpif_dqbuf() - query buffer handler
1050 * @file: file ptr
1051 * @priv: file handle
1052 * @buf: v4l2 buffer structure ptr
1053 */
1054 static int vpif_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
1055 {
1056 struct vpif_fh *fh = priv;
1057 struct channel_obj *ch = fh->channel;
1058 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1059
1060 vpif_dbg(2, debug, "vpif_dqbuf\n");
1061
1062 return videobuf_dqbuf(&common->buffer_queue, buf,
1063 file->f_flags & O_NONBLOCK);
1064 }
1065
1066 /**
1067 * vpif_streamon() - streamon handler
1068 * @file: file ptr
1069 * @priv: file handle
1070 * @buftype: v4l2 buffer type
1071 */
1072 static int vpif_streamon(struct file *file, void *priv,
1073 enum v4l2_buf_type buftype)
1074 {
1075
1076 struct vpif_capture_config *config = vpif_dev->platform_data;
1077 struct vpif_fh *fh = priv;
1078 struct channel_obj *ch = fh->channel;
1079 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1080 struct channel_obj *oth_ch = vpif_obj.dev[!ch->channel_id];
1081 struct vpif_params *vpif;
1082 unsigned long addr = 0;
1083 int ret = 0;
1084
1085 vpif_dbg(2, debug, "vpif_streamon\n");
1086
1087 vpif = &ch->vpifparams;
1088
1089 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1090 vpif_dbg(1, debug, "buffer type not supported\n");
1091 return -EINVAL;
1092 }
1093
1094 /* If file handle is not allowed IO, return error */
1095 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1096 vpif_dbg(1, debug, "io not allowed\n");
1097 return -EACCES;
1098 }
1099
1100 /* If Streaming is already started, return error */
1101 if (common->started) {
1102 vpif_dbg(1, debug, "channel->started\n");
1103 return -EBUSY;
1104 }
1105
1106 if ((ch->channel_id == VPIF_CHANNEL0_VIDEO &&
1107 oth_ch->common[VPIF_VIDEO_INDEX].started &&
1108 vpif->std_info.ycmux_mode == 0) ||
1109 ((ch->channel_id == VPIF_CHANNEL1_VIDEO) &&
1110 (2 == oth_ch->common[VPIF_VIDEO_INDEX].started))) {
1111 vpif_dbg(1, debug, "other channel is being used\n");
1112 return -EBUSY;
1113 }
1114
1115 ret = vpif_check_format(ch, &common->fmt.fmt.pix, 0);
1116 if (ret)
1117 return ret;
1118
1119 /* Enable streamon on the sub device */
1120 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1121 s_stream, 1);
1122
1123 if (ret && (ret != -ENOIOCTLCMD)) {
1124 vpif_dbg(1, debug, "stream on failed in subdev\n");
1125 return ret;
1126 }
1127
1128 /* Call videobuf_streamon to start streaming in videobuf */
1129 ret = videobuf_streamon(&common->buffer_queue);
1130 if (ret) {
1131 vpif_dbg(1, debug, "videobuf_streamon\n");
1132 return ret;
1133 }
1134
1135 /* If buffer queue is empty, return error */
1136 if (list_empty(&common->dma_queue)) {
1137 vpif_dbg(1, debug, "buffer queue is empty\n");
1138 ret = -EIO;
1139 goto exit;
1140 }
1141
1142 /* Get the next frame from the buffer queue */
1143 common->cur_frm = list_entry(common->dma_queue.next,
1144 struct videobuf_buffer, queue);
1145 common->next_frm = common->cur_frm;
1146
1147 /* Remove buffer from the buffer queue */
1148 list_del(&common->cur_frm->queue);
1149 /* Mark state of the current frame to active */
1150 common->cur_frm->state = VIDEOBUF_ACTIVE;
1151 /* Initialize field_id and started member */
1152 ch->field_id = 0;
1153 common->started = 1;
1154
1155 if (V4L2_MEMORY_USERPTR == common->memory)
1156 addr = common->cur_frm->boff;
1157 else
1158 addr = videobuf_to_dma_contig(common->cur_frm);
1159
1160 /* Calculate the offset for Y and C data in the buffer */
1161 vpif_calculate_offsets(ch);
1162
1163 if ((vpif->std_info.frm_fmt &&
1164 ((common->fmt.fmt.pix.field != V4L2_FIELD_NONE) &&
1165 (common->fmt.fmt.pix.field != V4L2_FIELD_ANY))) ||
1166 (!vpif->std_info.frm_fmt &&
1167 (common->fmt.fmt.pix.field == V4L2_FIELD_NONE))) {
1168 vpif_dbg(1, debug, "conflict in field format and std format\n");
1169 ret = -EINVAL;
1170 goto exit;
1171 }
1172
1173 /* configure 1 or 2 channel mode */
1174 ret = config->setup_input_channel_mode(vpif->std_info.ycmux_mode);
1175
1176 if (ret < 0) {
1177 vpif_dbg(1, debug, "can't set vpif channel mode\n");
1178 goto exit;
1179 }
1180
1181 /* Call vpif_set_params function to set the parameters and addresses */
1182 ret = vpif_set_video_params(vpif, ch->channel_id);
1183
1184 if (ret < 0) {
1185 vpif_dbg(1, debug, "can't set video params\n");
1186 goto exit;
1187 }
1188
1189 common->started = ret;
1190 vpif_config_addr(ch, ret);
1191
1192 common->set_addr(addr + common->ytop_off,
1193 addr + common->ybtm_off,
1194 addr + common->ctop_off,
1195 addr + common->cbtm_off);
1196
1197 /**
1198 * Set interrupt for both the fields in VPIF Register enable channel in
1199 * VPIF register
1200 */
1201 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id)) {
1202 channel0_intr_assert();
1203 channel0_intr_enable(1);
1204 enable_channel0(1);
1205 }
1206 if ((VPIF_CHANNEL1_VIDEO == ch->channel_id) ||
1207 (common->started == 2)) {
1208 channel1_intr_assert();
1209 channel1_intr_enable(1);
1210 enable_channel1(1);
1211 }
1212 channel_first_int[VPIF_VIDEO_INDEX][ch->channel_id] = 1;
1213 return ret;
1214
1215 exit:
1216 videobuf_streamoff(&common->buffer_queue);
1217 return ret;
1218 }
1219
1220 /**
1221 * vpif_streamoff() - streamoff handler
1222 * @file: file ptr
1223 * @priv: file handle
1224 * @buftype: v4l2 buffer type
1225 */
1226 static int vpif_streamoff(struct file *file, void *priv,
1227 enum v4l2_buf_type buftype)
1228 {
1229
1230 struct vpif_fh *fh = priv;
1231 struct channel_obj *ch = fh->channel;
1232 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1233 int ret;
1234
1235 vpif_dbg(2, debug, "vpif_streamoff\n");
1236
1237 if (buftype != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1238 vpif_dbg(1, debug, "buffer type not supported\n");
1239 return -EINVAL;
1240 }
1241
1242 /* If io is allowed for this file handle, return error */
1243 if (!fh->io_allowed[VPIF_VIDEO_INDEX]) {
1244 vpif_dbg(1, debug, "io not allowed\n");
1245 return -EACCES;
1246 }
1247
1248 /* If streaming is not started, return error */
1249 if (!common->started) {
1250 vpif_dbg(1, debug, "channel->started\n");
1251 return -EINVAL;
1252 }
1253
1254 /* disable channel */
1255 if (VPIF_CHANNEL0_VIDEO == ch->channel_id) {
1256 enable_channel0(0);
1257 channel0_intr_enable(0);
1258 } else {
1259 enable_channel1(0);
1260 channel1_intr_enable(0);
1261 }
1262
1263 common->started = 0;
1264
1265 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1266 s_stream, 0);
1267
1268 if (ret && (ret != -ENOIOCTLCMD))
1269 vpif_dbg(1, debug, "stream off failed in subdev\n");
1270
1271 return videobuf_streamoff(&common->buffer_queue);
1272 }
1273
1274 /**
1275 * vpif_map_sub_device_to_input() - Maps sub device to input
1276 * @ch - ptr to channel
1277 * @config - ptr to capture configuration
1278 * @input_index - Given input index from application
1279 * @sub_device_index - index into sd table
1280 *
1281 * lookup the sub device information for a given input index.
1282 * we report all the inputs to application. inputs table also
1283 * has sub device name for the each input
1284 */
1285 static struct vpif_subdev_info *vpif_map_sub_device_to_input(
1286 struct channel_obj *ch,
1287 struct vpif_capture_config *vpif_cfg,
1288 int input_index,
1289 int *sub_device_index)
1290 {
1291 struct vpif_capture_chan_config *chan_cfg;
1292 struct vpif_subdev_info *subdev_info = NULL;
1293 const char *subdev_name = NULL;
1294 int i;
1295
1296 vpif_dbg(2, debug, "vpif_map_sub_device_to_input\n");
1297
1298 chan_cfg = &vpif_cfg->chan_config[ch->channel_id];
1299
1300 /**
1301 * search through the inputs to find the sub device supporting
1302 * the input
1303 */
1304 for (i = 0; i < chan_cfg->input_count; i++) {
1305 /* For each sub device, loop through input */
1306 if (i == input_index) {
1307 subdev_name = chan_cfg->inputs[i].subdev_name;
1308 break;
1309 }
1310 }
1311
1312 /* if reached maximum. return null */
1313 if (i == chan_cfg->input_count || (NULL == subdev_name))
1314 return subdev_info;
1315
1316 /* loop through the sub device list to get the sub device info */
1317 for (i = 0; i < vpif_cfg->subdev_count; i++) {
1318 subdev_info = &vpif_cfg->subdev_info[i];
1319 if (!strcmp(subdev_info->name, subdev_name))
1320 break;
1321 }
1322
1323 if (i == vpif_cfg->subdev_count)
1324 return subdev_info;
1325
1326 /* check if the sub device is registered */
1327 if (NULL == vpif_obj.sd[i])
1328 return NULL;
1329
1330 *sub_device_index = i;
1331 return subdev_info;
1332 }
1333
1334 /**
1335 * vpif_querystd() - querystd handler
1336 * @file: file ptr
1337 * @priv: file handle
1338 * @std_id: ptr to std id
1339 *
1340 * This function is called to detect standard at the selected input
1341 */
1342 static int vpif_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
1343 {
1344 struct vpif_fh *fh = priv;
1345 struct channel_obj *ch = fh->channel;
1346 int ret = 0;
1347
1348 vpif_dbg(2, debug, "vpif_querystd\n");
1349
1350 /* Call querystd function of decoder device */
1351 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], video,
1352 querystd, std_id);
1353 if (ret < 0)
1354 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1355
1356 return ret;
1357 }
1358
1359 /**
1360 * vpif_g_std() - get STD handler
1361 * @file: file ptr
1362 * @priv: file handle
1363 * @std_id: ptr to std id
1364 */
1365 static int vpif_g_std(struct file *file, void *priv, v4l2_std_id *std)
1366 {
1367 struct vpif_fh *fh = priv;
1368 struct channel_obj *ch = fh->channel;
1369
1370 vpif_dbg(2, debug, "vpif_g_std\n");
1371
1372 *std = ch->video.stdid;
1373 return 0;
1374 }
1375
1376 /**
1377 * vpif_s_std() - set STD handler
1378 * @file: file ptr
1379 * @priv: file handle
1380 * @std_id: ptr to std id
1381 */
1382 static int vpif_s_std(struct file *file, void *priv, v4l2_std_id *std_id)
1383 {
1384 struct vpif_fh *fh = priv;
1385 struct channel_obj *ch = fh->channel;
1386 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1387 int ret = 0;
1388
1389 vpif_dbg(2, debug, "vpif_s_std\n");
1390
1391 if (common->started) {
1392 vpif_err("streaming in progress\n");
1393 return -EBUSY;
1394 }
1395
1396 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1397 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1398 if (!fh->initialized) {
1399 vpif_dbg(1, debug, "Channel Busy\n");
1400 return -EBUSY;
1401 }
1402 }
1403
1404 ret = v4l2_prio_check(&ch->prio, fh->prio);
1405 if (0 != ret)
1406 return ret;
1407
1408 fh->initialized = 1;
1409
1410 /* Call encoder subdevice function to set the standard */
1411 ch->video.stdid = *std_id;
1412 ch->video.dv_preset = V4L2_DV_INVALID;
1413 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1414
1415 /* Get the information about the standard */
1416 if (vpif_update_std_info(ch)) {
1417 vpif_err("Error getting the standard info\n");
1418 return -EINVAL;
1419 }
1420
1421 /* Configure the default format information */
1422 vpif_config_format(ch);
1423
1424 /* set standard in the sub device */
1425 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
1426 s_std, *std_id);
1427 if (ret < 0)
1428 vpif_dbg(1, debug, "Failed to set standard for sub devices\n");
1429 return ret;
1430 }
1431
1432 /**
1433 * vpif_enum_input() - ENUMINPUT handler
1434 * @file: file ptr
1435 * @priv: file handle
1436 * @input: ptr to input structure
1437 */
1438 static int vpif_enum_input(struct file *file, void *priv,
1439 struct v4l2_input *input)
1440 {
1441
1442 struct vpif_capture_config *config = vpif_dev->platform_data;
1443 struct vpif_capture_chan_config *chan_cfg;
1444 struct vpif_fh *fh = priv;
1445 struct channel_obj *ch = fh->channel;
1446
1447 chan_cfg = &config->chan_config[ch->channel_id];
1448
1449 if (input->index >= chan_cfg->input_count) {
1450 vpif_dbg(1, debug, "Invalid input index\n");
1451 return -EINVAL;
1452 }
1453
1454 memcpy(input, &chan_cfg->inputs[input->index].input,
1455 sizeof(*input));
1456 return 0;
1457 }
1458
1459 /**
1460 * vpif_g_input() - Get INPUT handler
1461 * @file: file ptr
1462 * @priv: file handle
1463 * @index: ptr to input index
1464 */
1465 static int vpif_g_input(struct file *file, void *priv, unsigned int *index)
1466 {
1467 struct vpif_fh *fh = priv;
1468 struct channel_obj *ch = fh->channel;
1469 struct video_obj *vid_ch = &ch->video;
1470
1471 *index = vid_ch->input_idx;
1472
1473 return 0;
1474 }
1475
1476 /**
1477 * vpif_s_input() - Set INPUT handler
1478 * @file: file ptr
1479 * @priv: file handle
1480 * @index: input index
1481 */
1482 static int vpif_s_input(struct file *file, void *priv, unsigned int index)
1483 {
1484 struct vpif_capture_config *config = vpif_dev->platform_data;
1485 struct vpif_capture_chan_config *chan_cfg;
1486 struct vpif_fh *fh = priv;
1487 struct channel_obj *ch = fh->channel;
1488 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1489 struct video_obj *vid_ch = &ch->video;
1490 struct vpif_subdev_info *subdev_info;
1491 int ret = 0, sd_index = 0;
1492 u32 input = 0, output = 0;
1493
1494 chan_cfg = &config->chan_config[ch->channel_id];
1495
1496 if (common->started) {
1497 vpif_err("Streaming in progress\n");
1498 return -EBUSY;
1499 }
1500
1501 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1502 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1503 if (!fh->initialized) {
1504 vpif_dbg(1, debug, "Channel Busy\n");
1505 return -EBUSY;
1506 }
1507 }
1508
1509 ret = v4l2_prio_check(&ch->prio, fh->prio);
1510 if (0 != ret)
1511 return ret;
1512
1513 fh->initialized = 1;
1514 subdev_info = vpif_map_sub_device_to_input(ch, config, index,
1515 &sd_index);
1516 if (NULL == subdev_info) {
1517 vpif_dbg(1, debug,
1518 "couldn't lookup sub device for the input index\n");
1519 return -EINVAL;
1520 }
1521
1522 /* first setup input path from sub device to vpif */
1523 if (config->setup_input_path) {
1524 ret = config->setup_input_path(ch->channel_id,
1525 subdev_info->name);
1526 if (ret < 0) {
1527 vpif_dbg(1, debug, "couldn't setup input path for the"
1528 " sub device %s, for input index %d\n",
1529 subdev_info->name, index);
1530 return ret;
1531 }
1532 }
1533
1534 if (subdev_info->can_route) {
1535 input = subdev_info->input;
1536 output = subdev_info->output;
1537 ret = v4l2_subdev_call(vpif_obj.sd[sd_index], video, s_routing,
1538 input, output, 0);
1539 if (ret < 0) {
1540 vpif_dbg(1, debug, "Failed to set input\n");
1541 return ret;
1542 }
1543 }
1544 vid_ch->input_idx = index;
1545 ch->curr_subdev_info = subdev_info;
1546 ch->curr_sd_index = sd_index;
1547 /* copy interface parameters to vpif */
1548 ch->vpifparams.iface = subdev_info->vpif_if;
1549
1550 /* update tvnorms from the sub device input info */
1551 ch->video_dev->tvnorms = chan_cfg->inputs[index].input.std;
1552 return ret;
1553 }
1554
1555 /**
1556 * vpif_enum_fmt_vid_cap() - ENUM_FMT handler
1557 * @file: file ptr
1558 * @priv: file handle
1559 * @index: input index
1560 */
1561 static int vpif_enum_fmt_vid_cap(struct file *file, void *priv,
1562 struct v4l2_fmtdesc *fmt)
1563 {
1564 struct vpif_fh *fh = priv;
1565 struct channel_obj *ch = fh->channel;
1566
1567 if (fmt->index != 0) {
1568 vpif_dbg(1, debug, "Invalid format index\n");
1569 return -EINVAL;
1570 }
1571
1572 /* Fill in the information about format */
1573 if (ch->vpifparams.iface.if_type == VPIF_IF_RAW_BAYER) {
1574 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1575 strcpy(fmt->description, "Raw Mode -Bayer Pattern GrRBGb");
1576 fmt->pixelformat = V4L2_PIX_FMT_SBGGR8;
1577 } else {
1578 fmt->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1579 strcpy(fmt->description, "YCbCr4:2:2 YC Planar");
1580 fmt->pixelformat = V4L2_PIX_FMT_YUV422P;
1581 }
1582 return 0;
1583 }
1584
1585 /**
1586 * vpif_try_fmt_vid_cap() - TRY_FMT handler
1587 * @file: file ptr
1588 * @priv: file handle
1589 * @fmt: ptr to v4l2 format structure
1590 */
1591 static int vpif_try_fmt_vid_cap(struct file *file, void *priv,
1592 struct v4l2_format *fmt)
1593 {
1594 struct vpif_fh *fh = priv;
1595 struct channel_obj *ch = fh->channel;
1596 struct v4l2_pix_format *pixfmt = &fmt->fmt.pix;
1597
1598 return vpif_check_format(ch, pixfmt, 1);
1599 }
1600
1601
1602 /**
1603 * vpif_g_fmt_vid_cap() - Set INPUT handler
1604 * @file: file ptr
1605 * @priv: file handle
1606 * @fmt: ptr to v4l2 format structure
1607 */
1608 static int vpif_g_fmt_vid_cap(struct file *file, void *priv,
1609 struct v4l2_format *fmt)
1610 {
1611 struct vpif_fh *fh = priv;
1612 struct channel_obj *ch = fh->channel;
1613 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1614
1615 /* Check the validity of the buffer type */
1616 if (common->fmt.type != fmt->type)
1617 return -EINVAL;
1618
1619 /* Fill in the information about format */
1620 *fmt = common->fmt;
1621 return 0;
1622 }
1623
1624 /**
1625 * vpif_s_fmt_vid_cap() - Set FMT handler
1626 * @file: file ptr
1627 * @priv: file handle
1628 * @fmt: ptr to v4l2 format structure
1629 */
1630 static int vpif_s_fmt_vid_cap(struct file *file, void *priv,
1631 struct v4l2_format *fmt)
1632 {
1633 struct vpif_fh *fh = priv;
1634 struct channel_obj *ch = fh->channel;
1635 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1636 struct v4l2_pix_format *pixfmt;
1637 int ret = 0;
1638
1639 vpif_dbg(2, debug, "%s\n", __func__);
1640
1641 /* If streaming is started, return error */
1642 if (common->started) {
1643 vpif_dbg(1, debug, "Streaming is started\n");
1644 return -EBUSY;
1645 }
1646
1647 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1648 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1649 if (!fh->initialized) {
1650 vpif_dbg(1, debug, "Channel Busy\n");
1651 return -EBUSY;
1652 }
1653 }
1654
1655 ret = v4l2_prio_check(&ch->prio, fh->prio);
1656 if (0 != ret)
1657 return ret;
1658
1659 fh->initialized = 1;
1660
1661 pixfmt = &fmt->fmt.pix;
1662 /* Check for valid field format */
1663 ret = vpif_check_format(ch, pixfmt, 0);
1664
1665 if (ret)
1666 return ret;
1667 /* store the format in the channel object */
1668 common->fmt = *fmt;
1669 return 0;
1670 }
1671
1672 /**
1673 * vpif_querycap() - QUERYCAP handler
1674 * @file: file ptr
1675 * @priv: file handle
1676 * @cap: ptr to v4l2_capability structure
1677 */
1678 static int vpif_querycap(struct file *file, void *priv,
1679 struct v4l2_capability *cap)
1680 {
1681 struct vpif_capture_config *config = vpif_dev->platform_data;
1682
1683 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
1684 strlcpy(cap->driver, "vpif capture", sizeof(cap->driver));
1685 strlcpy(cap->bus_info, "VPIF Platform", sizeof(cap->bus_info));
1686 strlcpy(cap->card, config->card_name, sizeof(cap->card));
1687
1688 return 0;
1689 }
1690
1691 /**
1692 * vpif_g_priority() - get priority handler
1693 * @file: file ptr
1694 * @priv: file handle
1695 * @prio: ptr to v4l2_priority structure
1696 */
1697 static int vpif_g_priority(struct file *file, void *priv,
1698 enum v4l2_priority *prio)
1699 {
1700 struct vpif_fh *fh = priv;
1701 struct channel_obj *ch = fh->channel;
1702
1703 *prio = v4l2_prio_max(&ch->prio);
1704
1705 return 0;
1706 }
1707
1708 /**
1709 * vpif_s_priority() - set priority handler
1710 * @file: file ptr
1711 * @priv: file handle
1712 * @prio: ptr to v4l2_priority structure
1713 */
1714 static int vpif_s_priority(struct file *file, void *priv, enum v4l2_priority p)
1715 {
1716 struct vpif_fh *fh = priv;
1717 struct channel_obj *ch = fh->channel;
1718
1719 return v4l2_prio_change(&ch->prio, &fh->prio, p);
1720 }
1721
1722 /**
1723 * vpif_cropcap() - cropcap handler
1724 * @file: file ptr
1725 * @priv: file handle
1726 * @crop: ptr to v4l2_cropcap structure
1727 */
1728 static int vpif_cropcap(struct file *file, void *priv,
1729 struct v4l2_cropcap *crop)
1730 {
1731 struct vpif_fh *fh = priv;
1732 struct channel_obj *ch = fh->channel;
1733 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1734
1735 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != crop->type)
1736 return -EINVAL;
1737
1738 crop->bounds.left = 0;
1739 crop->bounds.top = 0;
1740 crop->bounds.height = common->height;
1741 crop->bounds.width = common->width;
1742 crop->defrect = crop->bounds;
1743 return 0;
1744 }
1745
1746 /**
1747 * vpif_enum_dv_presets() - ENUM_DV_PRESETS handler
1748 * @file: file ptr
1749 * @priv: file handle
1750 * @preset: input preset
1751 */
1752 static int vpif_enum_dv_presets(struct file *file, void *priv,
1753 struct v4l2_dv_enum_preset *preset)
1754 {
1755 struct vpif_fh *fh = priv;
1756 struct channel_obj *ch = fh->channel;
1757
1758 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1759 video, enum_dv_presets, preset);
1760 }
1761
1762 /**
1763 * vpif_query_dv_presets() - QUERY_DV_PRESET handler
1764 * @file: file ptr
1765 * @priv: file handle
1766 * @preset: input preset
1767 */
1768 static int vpif_query_dv_preset(struct file *file, void *priv,
1769 struct v4l2_dv_preset *preset)
1770 {
1771 struct vpif_fh *fh = priv;
1772 struct channel_obj *ch = fh->channel;
1773
1774 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1775 video, query_dv_preset, preset);
1776 }
1777 /**
1778 * vpif_s_dv_presets() - S_DV_PRESETS handler
1779 * @file: file ptr
1780 * @priv: file handle
1781 * @preset: input preset
1782 */
1783 static int vpif_s_dv_preset(struct file *file, void *priv,
1784 struct v4l2_dv_preset *preset)
1785 {
1786 struct vpif_fh *fh = priv;
1787 struct channel_obj *ch = fh->channel;
1788 struct common_obj *common = &ch->common[VPIF_VIDEO_INDEX];
1789 int ret = 0;
1790
1791 if (common->started) {
1792 vpif_dbg(1, debug, "streaming in progress\n");
1793 return -EBUSY;
1794 }
1795
1796 if ((VPIF_CHANNEL0_VIDEO == ch->channel_id) ||
1797 (VPIF_CHANNEL1_VIDEO == ch->channel_id)) {
1798 if (!fh->initialized) {
1799 vpif_dbg(1, debug, "Channel Busy\n");
1800 return -EBUSY;
1801 }
1802 }
1803
1804 ret = v4l2_prio_check(&ch->prio, fh->prio);
1805 if (ret)
1806 return ret;
1807
1808 fh->initialized = 1;
1809
1810 /* Call encoder subdevice function to set the standard */
1811 if (mutex_lock_interruptible(&common->lock))
1812 return -ERESTARTSYS;
1813
1814 ch->video.dv_preset = preset->preset;
1815 ch->video.stdid = V4L2_STD_UNKNOWN;
1816 memset(&ch->video.bt_timings, 0, sizeof(ch->video.bt_timings));
1817
1818 /* Get the information about the standard */
1819 if (vpif_update_std_info(ch)) {
1820 vpif_dbg(1, debug, "Error getting the standard info\n");
1821 ret = -EINVAL;
1822 } else {
1823 /* Configure the default format information */
1824 vpif_config_format(ch);
1825
1826 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1827 video, s_dv_preset, preset);
1828 }
1829
1830 mutex_unlock(&common->lock);
1831
1832 return ret;
1833 }
1834 /**
1835 * vpif_g_dv_presets() - G_DV_PRESETS handler
1836 * @file: file ptr
1837 * @priv: file handle
1838 * @preset: input preset
1839 */
1840 static int vpif_g_dv_preset(struct file *file, void *priv,
1841 struct v4l2_dv_preset *preset)
1842 {
1843 struct vpif_fh *fh = priv;
1844 struct channel_obj *ch = fh->channel;
1845
1846 preset->preset = ch->video.dv_preset;
1847
1848 return 0;
1849 }
1850
1851 /**
1852 * vpif_s_dv_timings() - S_DV_TIMINGS handler
1853 * @file: file ptr
1854 * @priv: file handle
1855 * @timings: digital video timings
1856 */
1857 static int vpif_s_dv_timings(struct file *file, void *priv,
1858 struct v4l2_dv_timings *timings)
1859 {
1860 struct vpif_fh *fh = priv;
1861 struct channel_obj *ch = fh->channel;
1862 struct vpif_params *vpifparams = &ch->vpifparams;
1863 struct vpif_channel_config_params *std_info = &vpifparams->std_info;
1864 struct video_obj *vid_ch = &ch->video;
1865 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1866 int ret;
1867
1868 if (timings->type != V4L2_DV_BT_656_1120) {
1869 vpif_dbg(2, debug, "Timing type not defined\n");
1870 return -EINVAL;
1871 }
1872
1873 /* Configure subdevice timings, if any */
1874 ret = v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index],
1875 video, s_dv_timings, timings);
1876 if (ret == -ENOIOCTLCMD) {
1877 vpif_dbg(2, debug, "Custom DV timings not supported by "
1878 "subdevice\n");
1879 return -EINVAL;
1880 }
1881 if (ret < 0) {
1882 vpif_dbg(2, debug, "Error setting custom DV timings\n");
1883 return ret;
1884 }
1885
1886 if (!(timings->bt.width && timings->bt.height &&
1887 (timings->bt.hbackporch ||
1888 timings->bt.hfrontporch ||
1889 timings->bt.hsync) &&
1890 timings->bt.vfrontporch &&
1891 (timings->bt.vbackporch ||
1892 timings->bt.vsync))) {
1893 vpif_dbg(2, debug, "Timings for width, height, "
1894 "horizontal back porch, horizontal sync, "
1895 "horizontal front porch, vertical back porch, "
1896 "vertical sync and vertical back porch "
1897 "must be defined\n");
1898 return -EINVAL;
1899 }
1900
1901 *bt = timings->bt;
1902
1903 /* Configure video port timings */
1904
1905 std_info->eav2sav = bt->hbackporch + bt->hfrontporch +
1906 bt->hsync - 8;
1907 std_info->sav2eav = bt->width;
1908
1909 std_info->l1 = 1;
1910 std_info->l3 = bt->vsync + bt->vbackporch + 1;
1911
1912 if (bt->interlaced) {
1913 if (bt->il_vbackporch || bt->il_vfrontporch || bt->il_vsync) {
1914 std_info->vsize = bt->height * 2 +
1915 bt->vfrontporch + bt->vsync + bt->vbackporch +
1916 bt->il_vfrontporch + bt->il_vsync +
1917 bt->il_vbackporch;
1918 std_info->l5 = std_info->vsize/2 -
1919 (bt->vfrontporch - 1);
1920 std_info->l7 = std_info->vsize/2 + 1;
1921 std_info->l9 = std_info->l7 + bt->il_vsync +
1922 bt->il_vbackporch + 1;
1923 std_info->l11 = std_info->vsize -
1924 (bt->il_vfrontporch - 1);
1925 } else {
1926 vpif_dbg(2, debug, "Required timing values for "
1927 "interlaced BT format missing\n");
1928 return -EINVAL;
1929 }
1930 } else {
1931 std_info->vsize = bt->height + bt->vfrontporch +
1932 bt->vsync + bt->vbackporch;
1933 std_info->l5 = std_info->vsize - (bt->vfrontporch - 1);
1934 }
1935 strncpy(std_info->name, "Custom timings BT656/1120", VPIF_MAX_NAME);
1936 std_info->width = bt->width;
1937 std_info->height = bt->height;
1938 std_info->frm_fmt = bt->interlaced ? 0 : 1;
1939 std_info->ycmux_mode = 0;
1940 std_info->capture_format = 0;
1941 std_info->vbi_supported = 0;
1942 std_info->hd_sd = 1;
1943 std_info->stdid = 0;
1944 std_info->dv_preset = V4L2_DV_INVALID;
1945
1946 vid_ch->stdid = 0;
1947 vid_ch->dv_preset = V4L2_DV_INVALID;
1948 return 0;
1949 }
1950
1951 /**
1952 * vpif_g_dv_timings() - G_DV_TIMINGS handler
1953 * @file: file ptr
1954 * @priv: file handle
1955 * @timings: digital video timings
1956 */
1957 static int vpif_g_dv_timings(struct file *file, void *priv,
1958 struct v4l2_dv_timings *timings)
1959 {
1960 struct vpif_fh *fh = priv;
1961 struct channel_obj *ch = fh->channel;
1962 struct video_obj *vid_ch = &ch->video;
1963 struct v4l2_bt_timings *bt = &vid_ch->bt_timings;
1964
1965 timings->bt = *bt;
1966
1967 return 0;
1968 }
1969
1970 /*
1971 * vpif_g_chip_ident() - Identify the chip
1972 * @file: file ptr
1973 * @priv: file handle
1974 * @chip: chip identity
1975 *
1976 * Returns zero or -EINVAL if read operations fails.
1977 */
1978 static int vpif_g_chip_ident(struct file *file, void *priv,
1979 struct v4l2_dbg_chip_ident *chip)
1980 {
1981 chip->ident = V4L2_IDENT_NONE;
1982 chip->revision = 0;
1983 if (chip->match.type != V4L2_CHIP_MATCH_I2C_DRIVER &&
1984 chip->match.type != V4L2_CHIP_MATCH_I2C_ADDR) {
1985 vpif_dbg(2, debug, "match_type is invalid.\n");
1986 return -EINVAL;
1987 }
1988
1989 return v4l2_device_call_until_err(&vpif_obj.v4l2_dev, 0, core,
1990 g_chip_ident, chip);
1991 }
1992
1993 #ifdef CONFIG_VIDEO_ADV_DEBUG
1994 /*
1995 * vpif_dbg_g_register() - Read register
1996 * @file: file ptr
1997 * @priv: file handle
1998 * @reg: register to be read
1999 *
2000 * Debugging only
2001 * Returns zero or -EINVAL if read operations fails.
2002 */
2003 static int vpif_dbg_g_register(struct file *file, void *priv,
2004 struct v4l2_dbg_register *reg){
2005 struct vpif_fh *fh = priv;
2006 struct channel_obj *ch = fh->channel;
2007
2008 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2009 g_register, reg);
2010 }
2011
2012 /*
2013 * vpif_dbg_s_register() - Write to register
2014 * @file: file ptr
2015 * @priv: file handle
2016 * @reg: register to be modified
2017 *
2018 * Debugging only
2019 * Returns zero or -EINVAL if write operations fails.
2020 */
2021 static int vpif_dbg_s_register(struct file *file, void *priv,
2022 struct v4l2_dbg_register *reg){
2023 struct vpif_fh *fh = priv;
2024 struct channel_obj *ch = fh->channel;
2025
2026 return v4l2_subdev_call(vpif_obj.sd[ch->curr_sd_index], core,
2027 s_register, reg);
2028 }
2029 #endif
2030
2031 /*
2032 * vpif_log_status() - Status information
2033 * @file: file ptr
2034 * @priv: file handle
2035 *
2036 * Returns zero.
2037 */
2038 static int vpif_log_status(struct file *filep, void *priv)
2039 {
2040 /* status for sub devices */
2041 v4l2_device_call_all(&vpif_obj.v4l2_dev, 0, core, log_status);
2042
2043 return 0;
2044 }
2045
2046 /* vpif capture ioctl operations */
2047 static const struct v4l2_ioctl_ops vpif_ioctl_ops = {
2048 .vidioc_querycap = vpif_querycap,
2049 .vidioc_g_priority = vpif_g_priority,
2050 .vidioc_s_priority = vpif_s_priority,
2051 .vidioc_enum_fmt_vid_cap = vpif_enum_fmt_vid_cap,
2052 .vidioc_g_fmt_vid_cap = vpif_g_fmt_vid_cap,
2053 .vidioc_s_fmt_vid_cap = vpif_s_fmt_vid_cap,
2054 .vidioc_try_fmt_vid_cap = vpif_try_fmt_vid_cap,
2055 .vidioc_enum_input = vpif_enum_input,
2056 .vidioc_s_input = vpif_s_input,
2057 .vidioc_g_input = vpif_g_input,
2058 .vidioc_reqbufs = vpif_reqbufs,
2059 .vidioc_querybuf = vpif_querybuf,
2060 .vidioc_querystd = vpif_querystd,
2061 .vidioc_s_std = vpif_s_std,
2062 .vidioc_g_std = vpif_g_std,
2063 .vidioc_qbuf = vpif_qbuf,
2064 .vidioc_dqbuf = vpif_dqbuf,
2065 .vidioc_streamon = vpif_streamon,
2066 .vidioc_streamoff = vpif_streamoff,
2067 .vidioc_cropcap = vpif_cropcap,
2068 .vidioc_enum_dv_presets = vpif_enum_dv_presets,
2069 .vidioc_s_dv_preset = vpif_s_dv_preset,
2070 .vidioc_g_dv_preset = vpif_g_dv_preset,
2071 .vidioc_query_dv_preset = vpif_query_dv_preset,
2072 .vidioc_s_dv_timings = vpif_s_dv_timings,
2073 .vidioc_g_dv_timings = vpif_g_dv_timings,
2074 .vidioc_g_chip_ident = vpif_g_chip_ident,
2075 #ifdef CONFIG_VIDEO_ADV_DEBUG
2076 .vidioc_g_register = vpif_dbg_g_register,
2077 .vidioc_s_register = vpif_dbg_s_register,
2078 #endif
2079 .vidioc_log_status = vpif_log_status,
2080 };
2081
2082 /* vpif file operations */
2083 static struct v4l2_file_operations vpif_fops = {
2084 .owner = THIS_MODULE,
2085 .open = vpif_open,
2086 .release = vpif_release,
2087 .unlocked_ioctl = video_ioctl2,
2088 .mmap = vpif_mmap,
2089 .poll = vpif_poll
2090 };
2091
2092 /* vpif video template */
2093 static struct video_device vpif_video_template = {
2094 .name = "vpif",
2095 .fops = &vpif_fops,
2096 .minor = -1,
2097 .ioctl_ops = &vpif_ioctl_ops,
2098 };
2099
2100 /**
2101 * initialize_vpif() - Initialize vpif data structures
2102 *
2103 * Allocate memory for data structures and initialize them
2104 */
2105 static int initialize_vpif(void)
2106 {
2107 int err = 0, i, j;
2108 int free_channel_objects_index;
2109
2110 /* Default number of buffers should be 3 */
2111 if ((ch0_numbuffers > 0) &&
2112 (ch0_numbuffers < config_params.min_numbuffers))
2113 ch0_numbuffers = config_params.min_numbuffers;
2114 if ((ch1_numbuffers > 0) &&
2115 (ch1_numbuffers < config_params.min_numbuffers))
2116 ch1_numbuffers = config_params.min_numbuffers;
2117
2118 /* Set buffer size to min buffers size if it is invalid */
2119 if (ch0_bufsize < config_params.min_bufsize[VPIF_CHANNEL0_VIDEO])
2120 ch0_bufsize =
2121 config_params.min_bufsize[VPIF_CHANNEL0_VIDEO];
2122 if (ch1_bufsize < config_params.min_bufsize[VPIF_CHANNEL1_VIDEO])
2123 ch1_bufsize =
2124 config_params.min_bufsize[VPIF_CHANNEL1_VIDEO];
2125
2126 config_params.numbuffers[VPIF_CHANNEL0_VIDEO] = ch0_numbuffers;
2127 config_params.numbuffers[VPIF_CHANNEL1_VIDEO] = ch1_numbuffers;
2128 if (ch0_numbuffers) {
2129 config_params.channel_bufsize[VPIF_CHANNEL0_VIDEO]
2130 = ch0_bufsize;
2131 }
2132 if (ch1_numbuffers) {
2133 config_params.channel_bufsize[VPIF_CHANNEL1_VIDEO]
2134 = ch1_bufsize;
2135 }
2136
2137 /* Allocate memory for six channel objects */
2138 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2139 vpif_obj.dev[i] =
2140 kzalloc(sizeof(*vpif_obj.dev[i]), GFP_KERNEL);
2141 /* If memory allocation fails, return error */
2142 if (!vpif_obj.dev[i]) {
2143 free_channel_objects_index = i;
2144 err = -ENOMEM;
2145 goto vpif_init_free_channel_objects;
2146 }
2147 }
2148 return 0;
2149
2150 vpif_init_free_channel_objects:
2151 for (j = 0; j < free_channel_objects_index; j++)
2152 kfree(vpif_obj.dev[j]);
2153 return err;
2154 }
2155
2156 /**
2157 * vpif_probe : This function probes the vpif capture driver
2158 * @pdev: platform device pointer
2159 *
2160 * This creates device entries by register itself to the V4L2 driver and
2161 * initializes fields of each channel objects
2162 */
2163 static __init int vpif_probe(struct platform_device *pdev)
2164 {
2165 struct vpif_subdev_info *subdevdata;
2166 struct vpif_capture_config *config;
2167 int i, j, k, m, q, err;
2168 struct i2c_adapter *i2c_adap;
2169 struct channel_obj *ch;
2170 struct common_obj *common;
2171 struct video_device *vfd;
2172 struct resource *res;
2173 int subdev_count;
2174
2175 vpif_dev = &pdev->dev;
2176
2177 err = initialize_vpif();
2178 if (err) {
2179 v4l2_err(vpif_dev->driver, "Error initializing vpif\n");
2180 return err;
2181 }
2182
2183 err = v4l2_device_register(vpif_dev, &vpif_obj.v4l2_dev);
2184 if (err) {
2185 v4l2_err(vpif_dev->driver, "Error registering v4l2 device\n");
2186 return err;
2187 }
2188
2189 k = 0;
2190 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, k))) {
2191 for (i = res->start; i <= res->end; i++) {
2192 if (request_irq(i, vpif_channel_isr, IRQF_SHARED,
2193 "VPIF_Capture",
2194 (void *)(&vpif_obj.dev[k]->channel_id))) {
2195 err = -EBUSY;
2196 i--;
2197 goto vpif_int_err;
2198 }
2199 }
2200 k++;
2201 }
2202
2203 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2204 /* Get the pointer to the channel object */
2205 ch = vpif_obj.dev[i];
2206 /* Allocate memory for video device */
2207 vfd = video_device_alloc();
2208 if (NULL == vfd) {
2209 for (j = 0; j < i; j++) {
2210 ch = vpif_obj.dev[j];
2211 video_device_release(ch->video_dev);
2212 }
2213 err = -ENOMEM;
2214 goto vpif_dev_alloc_err;
2215 }
2216
2217 /* Initialize field of video device */
2218 *vfd = vpif_video_template;
2219 vfd->v4l2_dev = &vpif_obj.v4l2_dev;
2220 vfd->release = video_device_release;
2221 snprintf(vfd->name, sizeof(vfd->name),
2222 "VPIF_Capture_DRIVER_V%s",
2223 VPIF_CAPTURE_VERSION);
2224 /* Set video_dev to the video device */
2225 ch->video_dev = vfd;
2226 }
2227
2228 for (j = 0; j < VPIF_CAPTURE_MAX_DEVICES; j++) {
2229 ch = vpif_obj.dev[j];
2230 ch->channel_id = j;
2231 common = &(ch->common[VPIF_VIDEO_INDEX]);
2232 spin_lock_init(&common->irqlock);
2233 mutex_init(&common->lock);
2234 /* Locking in file operations other than ioctl should be done
2235 by the driver, not the V4L2 core.
2236 This driver needs auditing so that this flag can be removed. */
2237 set_bit(V4L2_FL_LOCK_ALL_FOPS, &ch->video_dev->flags);
2238 ch->video_dev->lock = &common->lock;
2239 /* Initialize prio member of channel object */
2240 v4l2_prio_init(&ch->prio);
2241 err = video_register_device(ch->video_dev,
2242 VFL_TYPE_GRABBER, (j ? 1 : 0));
2243 if (err)
2244 goto probe_out;
2245
2246 video_set_drvdata(ch->video_dev, ch);
2247
2248 }
2249
2250 i2c_adap = i2c_get_adapter(1);
2251 config = pdev->dev.platform_data;
2252
2253 subdev_count = config->subdev_count;
2254 vpif_obj.sd = kzalloc(sizeof(struct v4l2_subdev *) * subdev_count,
2255 GFP_KERNEL);
2256 if (vpif_obj.sd == NULL) {
2257 vpif_err("unable to allocate memory for subdevice pointers\n");
2258 err = -ENOMEM;
2259 goto probe_out;
2260 }
2261
2262 for (i = 0; i < subdev_count; i++) {
2263 subdevdata = &config->subdev_info[i];
2264 vpif_obj.sd[i] =
2265 v4l2_i2c_new_subdev_board(&vpif_obj.v4l2_dev,
2266 i2c_adap,
2267 &subdevdata->board_info,
2268 NULL);
2269
2270 if (!vpif_obj.sd[i]) {
2271 vpif_err("Error registering v4l2 subdevice\n");
2272 goto probe_subdev_out;
2273 }
2274 v4l2_info(&vpif_obj.v4l2_dev, "registered sub device %s\n",
2275 subdevdata->name);
2276
2277 if (vpif_obj.sd[i])
2278 vpif_obj.sd[i]->grp_id = 1 << i;
2279 }
2280
2281 v4l2_info(&vpif_obj.v4l2_dev, "VPIF capture driver initialized\n");
2282 return 0;
2283
2284 probe_subdev_out:
2285 /* free sub devices memory */
2286 kfree(vpif_obj.sd);
2287
2288 j = VPIF_CAPTURE_MAX_DEVICES;
2289 probe_out:
2290 for (k = 0; k < j; k++) {
2291 /* Get the pointer to the channel object */
2292 ch = vpif_obj.dev[k];
2293 /* Unregister video device */
2294 video_unregister_device(ch->video_dev);
2295 }
2296
2297 vpif_dev_alloc_err:
2298 k = VPIF_CAPTURE_MAX_DEVICES-1;
2299 res = platform_get_resource(pdev, IORESOURCE_IRQ, k);
2300 i = res->end;
2301
2302 vpif_int_err:
2303 for (q = k; q >= 0; q--) {
2304 for (m = i; m >= (int)res->start; m--)
2305 free_irq(m, (void *)(&vpif_obj.dev[q]->channel_id));
2306
2307 res = platform_get_resource(pdev, IORESOURCE_IRQ, q-1);
2308 if (res)
2309 i = res->end;
2310 }
2311 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2312 return err;
2313 }
2314
2315 /**
2316 * vpif_remove() - driver remove handler
2317 * @device: ptr to platform device structure
2318 *
2319 * The vidoe device is unregistered
2320 */
2321 static int vpif_remove(struct platform_device *device)
2322 {
2323 int i;
2324 struct channel_obj *ch;
2325
2326 v4l2_device_unregister(&vpif_obj.v4l2_dev);
2327
2328 /* un-register device */
2329 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++) {
2330 /* Get the pointer to the channel object */
2331 ch = vpif_obj.dev[i];
2332 /* Unregister video device */
2333 video_unregister_device(ch->video_dev);
2334 }
2335 return 0;
2336 }
2337
2338 /**
2339 * vpif_suspend: vpif device suspend
2340 *
2341 * TODO: Add suspend code here
2342 */
2343 static int
2344 vpif_suspend(struct device *dev)
2345 {
2346 return -1;
2347 }
2348
2349 /**
2350 * vpif_resume: vpif device suspend
2351 *
2352 * TODO: Add resume code here
2353 */
2354 static int
2355 vpif_resume(struct device *dev)
2356 {
2357 return -1;
2358 }
2359
2360 static const struct dev_pm_ops vpif_dev_pm_ops = {
2361 .suspend = vpif_suspend,
2362 .resume = vpif_resume,
2363 };
2364
2365 static __refdata struct platform_driver vpif_driver = {
2366 .driver = {
2367 .name = "vpif_capture",
2368 .owner = THIS_MODULE,
2369 .pm = &vpif_dev_pm_ops,
2370 },
2371 .probe = vpif_probe,
2372 .remove = vpif_remove,
2373 };
2374
2375 /**
2376 * vpif_init: initialize the vpif driver
2377 *
2378 * This function registers device and driver to the kernel, requests irq
2379 * handler and allocates memory
2380 * for channel objects
2381 */
2382 static __init int vpif_init(void)
2383 {
2384 return platform_driver_register(&vpif_driver);
2385 }
2386
2387 /**
2388 * vpif_cleanup : This function clean up the vpif capture resources
2389 *
2390 * This will un-registers device and driver to the kernel, frees
2391 * requested irq handler and de-allocates memory allocated for channel
2392 * objects.
2393 */
2394 static void vpif_cleanup(void)
2395 {
2396 struct platform_device *pdev;
2397 struct resource *res;
2398 int irq_num;
2399 int i = 0;
2400
2401 pdev = container_of(vpif_dev, struct platform_device, dev);
2402 while ((res = platform_get_resource(pdev, IORESOURCE_IRQ, i))) {
2403 for (irq_num = res->start; irq_num <= res->end; irq_num++)
2404 free_irq(irq_num,
2405 (void *)(&vpif_obj.dev[i]->channel_id));
2406 i++;
2407 }
2408
2409 platform_driver_unregister(&vpif_driver);
2410
2411 kfree(vpif_obj.sd);
2412 for (i = 0; i < VPIF_CAPTURE_MAX_DEVICES; i++)
2413 kfree(vpif_obj.dev[i]);
2414 }
2415
2416 /* Function for module initialization and cleanup */
2417 module_init(vpif_init);
2418 module_exit(vpif_cleanup);
This page took 0.077267 seconds and 6 git commands to generate.