PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / media / usb / tlg2300 / pd-video.c
blob8df668d06552bb4323ab0213889d12105e09422f
1 #include <linux/fs.h>
2 #include <linux/vmalloc.h>
3 #include <linux/videodev2.h>
4 #include <linux/usb.h>
5 #include <linux/mm.h>
6 #include <linux/sched.h>
7 #include <linux/slab.h>
9 #include <media/v4l2-ioctl.h>
10 #include <media/v4l2-dev.h>
11 #include <media/v4l2-ctrls.h>
13 #include "pd-common.h"
14 #include "vendorcmds.h"
16 #ifdef CONFIG_PM
17 static int pm_video_suspend(struct poseidon *pd);
18 static int pm_video_resume(struct poseidon *pd);
19 #endif
20 static void iso_bubble_handler(struct work_struct *w);
22 static int usb_transfer_mode;
23 module_param(usb_transfer_mode, int, 0644);
24 MODULE_PARM_DESC(usb_transfer_mode, "0 = Bulk, 1 = Isochronous");
26 static const struct poseidon_format poseidon_formats[] = {
27 { "YUV 422", V4L2_PIX_FMT_YUYV, 16, 0},
28 { "RGB565", V4L2_PIX_FMT_RGB565, 16, 0},
31 static const struct poseidon_tvnorm poseidon_tvnorms[] = {
32 { V4L2_STD_PAL_D, "PAL-D", TLG_TUNE_VSTD_PAL_D },
33 { V4L2_STD_PAL_B, "PAL-B", TLG_TUNE_VSTD_PAL_B },
34 { V4L2_STD_PAL_G, "PAL-G", TLG_TUNE_VSTD_PAL_G },
35 { V4L2_STD_PAL_H, "PAL-H", TLG_TUNE_VSTD_PAL_H },
36 { V4L2_STD_PAL_I, "PAL-I", TLG_TUNE_VSTD_PAL_I },
37 { V4L2_STD_PAL_M, "PAL-M", TLG_TUNE_VSTD_PAL_M },
38 { V4L2_STD_PAL_N, "PAL-N", TLG_TUNE_VSTD_PAL_N_COMBO },
39 { V4L2_STD_PAL_Nc, "PAL-Nc", TLG_TUNE_VSTD_PAL_N_COMBO },
40 { V4L2_STD_NTSC_M, "NTSC-M", TLG_TUNE_VSTD_NTSC_M },
41 { V4L2_STD_NTSC_M_JP, "NTSC-JP", TLG_TUNE_VSTD_NTSC_M_J },
42 { V4L2_STD_SECAM_B, "SECAM-B", TLG_TUNE_VSTD_SECAM_B },
43 { V4L2_STD_SECAM_D, "SECAM-D", TLG_TUNE_VSTD_SECAM_D },
44 { V4L2_STD_SECAM_G, "SECAM-G", TLG_TUNE_VSTD_SECAM_G },
45 { V4L2_STD_SECAM_H, "SECAM-H", TLG_TUNE_VSTD_SECAM_H },
46 { V4L2_STD_SECAM_K, "SECAM-K", TLG_TUNE_VSTD_SECAM_K },
47 { V4L2_STD_SECAM_K1, "SECAM-K1", TLG_TUNE_VSTD_SECAM_K1 },
48 { V4L2_STD_SECAM_L, "SECAM-L", TLG_TUNE_VSTD_SECAM_L },
49 { V4L2_STD_SECAM_LC, "SECAM-LC", TLG_TUNE_VSTD_SECAM_L1 },
51 static const unsigned int POSEIDON_TVNORMS = ARRAY_SIZE(poseidon_tvnorms);
53 struct pd_audio_mode {
54 u32 tlg_audio_mode;
55 u32 v4l2_audio_sub;
56 u32 v4l2_audio_mode;
59 static const struct pd_audio_mode pd_audio_modes[] = {
60 { TLG_TUNE_TVAUDIO_MODE_MONO, V4L2_TUNER_SUB_MONO,
61 V4L2_TUNER_MODE_MONO },
62 { TLG_TUNE_TVAUDIO_MODE_STEREO, V4L2_TUNER_SUB_STEREO,
63 V4L2_TUNER_MODE_STEREO },
64 { TLG_TUNE_TVAUDIO_MODE_LANG_A, V4L2_TUNER_SUB_LANG1,
65 V4L2_TUNER_MODE_LANG1 },
66 { TLG_TUNE_TVAUDIO_MODE_LANG_B, V4L2_TUNER_SUB_LANG2,
67 V4L2_TUNER_MODE_LANG2 },
68 { TLG_TUNE_TVAUDIO_MODE_LANG_C, V4L2_TUNER_SUB_LANG1,
69 V4L2_TUNER_MODE_LANG1_LANG2 }
71 static const unsigned int POSEIDON_AUDIOMODS = ARRAY_SIZE(pd_audio_modes);
73 struct pd_input {
74 char *name;
75 uint32_t tlg_src;
78 static const struct pd_input pd_inputs[] = {
79 { "TV Antenna", TLG_SIG_SRC_ANTENNA },
80 { "TV Cable", TLG_SIG_SRC_CABLE },
81 { "TV SVideo", TLG_SIG_SRC_SVIDEO },
82 { "TV Composite", TLG_SIG_SRC_COMPOSITE }
84 static const unsigned int POSEIDON_INPUTS = ARRAY_SIZE(pd_inputs);
86 struct video_std_to_audio_std {
87 v4l2_std_id video_std;
88 int audio_std;
91 static const struct video_std_to_audio_std video_to_audio_map[] = {
92 /* country : { 27, 32, 33, 34, 36, 44, 45, 46, 47, 48, 64,
93 65, 86, 351, 352, 353, 354, 358, 372, 852, 972 } */
94 { (V4L2_STD_PAL_I | V4L2_STD_PAL_B | V4L2_STD_PAL_D |
95 V4L2_STD_SECAM_L | V4L2_STD_SECAM_D), TLG_TUNE_ASTD_NICAM },
97 /* country : { 1, 52, 54, 55, 886 } */
98 {V4L2_STD_NTSC_M | V4L2_STD_PAL_N | V4L2_STD_PAL_M, TLG_TUNE_ASTD_BTSC},
100 /* country : { 81 } */
101 { V4L2_STD_NTSC_M_JP, TLG_TUNE_ASTD_EIAJ },
103 /* other country : TLG_TUNE_ASTD_A2 */
105 static const unsigned int map_size = ARRAY_SIZE(video_to_audio_map);
107 static int get_audio_std(v4l2_std_id v4l2_std)
109 int i = 0;
111 for (; i < map_size; i++) {
112 if (v4l2_std & video_to_audio_map[i].video_std)
113 return video_to_audio_map[i].audio_std;
115 return TLG_TUNE_ASTD_A2;
118 static int vidioc_querycap(struct file *file, void *fh,
119 struct v4l2_capability *cap)
121 struct video_device *vdev = video_devdata(file);
122 struct poseidon *p = video_get_drvdata(vdev);
124 strcpy(cap->driver, "tele-video");
125 strcpy(cap->card, "Telegent Poseidon");
126 usb_make_path(p->udev, cap->bus_info, sizeof(cap->bus_info));
127 cap->device_caps = V4L2_CAP_TUNER | V4L2_CAP_AUDIO |
128 V4L2_CAP_STREAMING | V4L2_CAP_READWRITE;
129 if (vdev->vfl_type == VFL_TYPE_VBI)
130 cap->device_caps |= V4L2_CAP_VBI_CAPTURE;
131 else
132 cap->device_caps |= V4L2_CAP_VIDEO_CAPTURE;
133 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS |
134 V4L2_CAP_RADIO | V4L2_CAP_VBI_CAPTURE | V4L2_CAP_VIDEO_CAPTURE;
135 return 0;
138 /*====================================================================*/
139 static void init_copy(struct video_data *video, bool index)
141 struct front_face *front = video->front;
143 video->field_count = index;
144 video->lines_copied = 0;
145 video->prev_left = 0 ;
146 video->dst = (char *)videobuf_to_vmalloc(front->curr_frame)
147 + index * video->lines_size;
148 video->vbi->copied = 0; /* set it here */
151 static bool get_frame(struct front_face *front, int *need_init)
153 struct videobuf_buffer *vb = front->curr_frame;
155 if (vb)
156 return true;
158 spin_lock(&front->queue_lock);
159 if (!list_empty(&front->active)) {
160 vb = list_entry(front->active.next,
161 struct videobuf_buffer, queue);
162 if (need_init)
163 *need_init = 1;
164 front->curr_frame = vb;
165 list_del_init(&vb->queue);
167 spin_unlock(&front->queue_lock);
169 return !!vb;
172 /* check if the video's buffer is ready */
173 static bool get_video_frame(struct front_face *front, struct video_data *video)
175 int need_init = 0;
176 bool ret = true;
178 ret = get_frame(front, &need_init);
179 if (ret && need_init)
180 init_copy(video, 0);
181 return ret;
184 static void submit_frame(struct front_face *front)
186 struct videobuf_buffer *vb = front->curr_frame;
188 if (vb == NULL)
189 return;
191 front->curr_frame = NULL;
192 vb->state = VIDEOBUF_DONE;
193 vb->field_count++;
194 v4l2_get_timestamp(&vb->ts);
196 wake_up(&vb->done);
200 * A frame is composed of two fields. If we receive all the two fields,
201 * call the submit_frame() to submit the whole frame to applications.
203 static void end_field(struct video_data *video)
205 if (1 == video->field_count)
206 submit_frame(video->front);
207 else
208 init_copy(video, 1);
211 static void copy_video_data(struct video_data *video, char *src,
212 unsigned int count)
214 #define copy_data(len) \
215 do { \
216 if (++video->lines_copied > video->lines_per_field) \
217 goto overflow; \
218 memcpy(video->dst, src, len);\
219 video->dst += len + video->lines_size; \
220 src += len; \
221 count -= len; \
222 } while (0)
224 while (count && count >= video->lines_size) {
225 if (video->prev_left) {
226 copy_data(video->prev_left);
227 video->prev_left = 0;
228 continue;
230 copy_data(video->lines_size);
232 if (count && count < video->lines_size) {
233 memcpy(video->dst, src, count);
235 video->prev_left = video->lines_size - count;
236 video->dst += count;
238 return;
240 overflow:
241 end_field(video);
244 static void check_trailer(struct video_data *video, char *src, int count)
246 struct vbi_data *vbi = video->vbi;
247 int offset; /* trailer's offset */
248 char *buf;
250 offset = (video->context.pix.sizeimage / 2 + vbi->vbi_size / 2)
251 - (vbi->copied + video->lines_size * video->lines_copied);
252 if (video->prev_left)
253 offset -= (video->lines_size - video->prev_left);
255 if (offset > count || offset <= 0)
256 goto short_package;
258 buf = src + offset;
260 /* trailer : (VFHS) + U32 + U32 + field_num */
261 if (!strncmp(buf, "VFHS", 4)) {
262 int field_num = *((u32 *)(buf + 12));
264 if ((field_num & 1) ^ video->field_count) {
265 init_copy(video, video->field_count);
266 return;
268 copy_video_data(video, src, offset);
270 short_package:
271 end_field(video);
274 /* ========== Check this more carefully! =========== */
275 static inline void copy_vbi_data(struct vbi_data *vbi,
276 char *src, unsigned int count)
278 struct front_face *front = vbi->front;
280 if (front && get_frame(front, NULL)) {
281 char *buf = videobuf_to_vmalloc(front->curr_frame);
283 if (vbi->video->field_count)
284 buf += (vbi->vbi_size / 2);
285 memcpy(buf + vbi->copied, src, count);
287 vbi->copied += count;
291 * Copy the normal data (VBI or VIDEO) without the trailer.
292 * VBI is not interlaced, while VIDEO is interlaced.
294 static inline void copy_vbi_video_data(struct video_data *video,
295 char *src, unsigned int count)
297 struct vbi_data *vbi = video->vbi;
298 unsigned int vbi_delta = (vbi->vbi_size / 2) - vbi->copied;
300 if (vbi_delta >= count) {
301 copy_vbi_data(vbi, src, count);
302 } else {
303 if (vbi_delta) {
304 copy_vbi_data(vbi, src, vbi_delta);
306 /* we receive the two fields of the VBI*/
307 if (vbi->front && video->field_count)
308 submit_frame(vbi->front);
310 copy_video_data(video, src + vbi_delta, count - vbi_delta);
314 static void urb_complete_bulk(struct urb *urb)
316 struct front_face *front = urb->context;
317 struct video_data *video = &front->pd->video_data;
318 char *src = (char *)urb->transfer_buffer;
319 int count = urb->actual_length;
320 int ret = 0;
322 if (!video->is_streaming || urb->status) {
323 if (urb->status == -EPROTO)
324 goto resend_it;
325 return;
327 if (!get_video_frame(front, video))
328 goto resend_it;
330 if (count == urb->transfer_buffer_length)
331 copy_vbi_video_data(video, src, count);
332 else
333 check_trailer(video, src, count);
335 resend_it:
336 ret = usb_submit_urb(urb, GFP_ATOMIC);
337 if (ret)
338 log(" submit failed: error %d", ret);
341 /************************* for ISO *********************/
342 #define GET_SUCCESS (0)
343 #define GET_TRAILER (1)
344 #define GET_TOO_MUCH_BUBBLE (2)
345 #define GET_NONE (3)
346 static int get_chunk(int start, struct urb *urb,
347 int *head, int *tail, int *bubble_err)
349 struct usb_iso_packet_descriptor *pkt = NULL;
350 int ret = GET_SUCCESS;
352 for (*head = *tail = -1; start < urb->number_of_packets; start++) {
353 pkt = &urb->iso_frame_desc[start];
355 /* handle the bubble of the Hub */
356 if (-EOVERFLOW == pkt->status) {
357 if (++*bubble_err > urb->number_of_packets / 3)
358 return GET_TOO_MUCH_BUBBLE;
359 continue;
362 /* This is the gap */
363 if (pkt->status || pkt->actual_length <= 0
364 || pkt->actual_length > ISO_PKT_SIZE) {
365 if (*head != -1)
366 break;
367 continue;
370 /* a good isochronous packet */
371 if (pkt->actual_length == ISO_PKT_SIZE) {
372 if (*head == -1)
373 *head = start;
374 *tail = start;
375 continue;
378 /* trailer is here */
379 if (pkt->actual_length < ISO_PKT_SIZE) {
380 if (*head == -1) {
381 *head = start;
382 *tail = start;
383 return GET_TRAILER;
385 break;
389 if (*head == -1 && *tail == -1)
390 ret = GET_NONE;
391 return ret;
395 * |__|------|___|-----|_______|
396 * ^ ^
397 * | |
398 * gap gap
400 static void urb_complete_iso(struct urb *urb)
402 struct front_face *front = urb->context;
403 struct video_data *video = &front->pd->video_data;
404 int bubble_err = 0, head = 0, tail = 0;
405 char *src = (char *)urb->transfer_buffer;
406 int ret = 0;
408 if (!video->is_streaming)
409 return;
411 do {
412 if (!get_video_frame(front, video))
413 goto out;
415 switch (get_chunk(head, urb, &head, &tail, &bubble_err)) {
416 case GET_SUCCESS:
417 copy_vbi_video_data(video, src + (head * ISO_PKT_SIZE),
418 (tail - head + 1) * ISO_PKT_SIZE);
419 break;
420 case GET_TRAILER:
421 check_trailer(video, src + (head * ISO_PKT_SIZE),
422 ISO_PKT_SIZE);
423 break;
424 case GET_NONE:
425 goto out;
426 case GET_TOO_MUCH_BUBBLE:
427 log("\t We got too much bubble");
428 schedule_work(&video->bubble_work);
429 return;
431 } while (head = tail + 1, head < urb->number_of_packets);
433 out:
434 ret = usb_submit_urb(urb, GFP_ATOMIC);
435 if (ret)
436 log("usb_submit_urb err : %d", ret);
438 /*============================= [ end ] =====================*/
440 static int prepare_iso_urb(struct video_data *video)
442 struct usb_device *udev = video->pd->udev;
443 int i;
445 if (video->urb_array[0])
446 return 0;
448 for (i = 0; i < SBUF_NUM; i++) {
449 struct urb *urb;
450 void *mem;
451 int j;
453 urb = usb_alloc_urb(PK_PER_URB, GFP_KERNEL);
454 if (urb == NULL)
455 goto out;
457 video->urb_array[i] = urb;
458 mem = usb_alloc_coherent(udev,
459 ISO_PKT_SIZE * PK_PER_URB,
460 GFP_KERNEL,
461 &urb->transfer_dma);
463 urb->complete = urb_complete_iso; /* handler */
464 urb->dev = udev;
465 urb->context = video->front;
466 urb->pipe = usb_rcvisocpipe(udev,
467 video->endpoint_addr);
468 urb->interval = 1;
469 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
470 urb->number_of_packets = PK_PER_URB;
471 urb->transfer_buffer = mem;
472 urb->transfer_buffer_length = PK_PER_URB * ISO_PKT_SIZE;
474 for (j = 0; j < PK_PER_URB; j++) {
475 urb->iso_frame_desc[j].offset = ISO_PKT_SIZE * j;
476 urb->iso_frame_desc[j].length = ISO_PKT_SIZE;
479 return 0;
480 out:
481 for (; i > 0; i--)
483 return -ENOMEM;
486 /* return the succeeded number of the allocation */
487 int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
488 struct usb_device *udev, u8 ep_addr,
489 int buf_size, gfp_t gfp_flags,
490 usb_complete_t complete_fn, void *context)
492 int i = 0;
494 for (; i < num; i++) {
495 void *mem;
496 struct urb *urb = usb_alloc_urb(0, gfp_flags);
497 if (urb == NULL)
498 return i;
500 mem = usb_alloc_coherent(udev, buf_size, gfp_flags,
501 &urb->transfer_dma);
502 if (mem == NULL) {
503 usb_free_urb(urb);
504 return i;
507 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, ep_addr),
508 mem, buf_size, complete_fn, context);
509 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
510 urb_array[i] = urb;
512 return i;
515 void free_all_urb_generic(struct urb **urb_array, int num)
517 int i;
518 struct urb *urb;
520 for (i = 0; i < num; i++) {
521 urb = urb_array[i];
522 if (urb) {
523 usb_free_coherent(urb->dev,
524 urb->transfer_buffer_length,
525 urb->transfer_buffer,
526 urb->transfer_dma);
527 usb_free_urb(urb);
528 urb_array[i] = NULL;
533 static int prepare_bulk_urb(struct video_data *video)
535 if (video->urb_array[0])
536 return 0;
538 alloc_bulk_urbs_generic(video->urb_array, SBUF_NUM,
539 video->pd->udev, video->endpoint_addr,
540 0x2000, GFP_KERNEL,
541 urb_complete_bulk, video->front);
542 return 0;
545 /* free the URBs */
546 static void free_all_urb(struct video_data *video)
548 free_all_urb_generic(video->urb_array, SBUF_NUM);
551 static void pd_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
553 videobuf_vmalloc_free(vb);
554 vb->state = VIDEOBUF_NEEDS_INIT;
557 static void pd_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
559 struct front_face *front = q->priv_data;
560 vb->state = VIDEOBUF_QUEUED;
561 list_add_tail(&vb->queue, &front->active);
564 static int pd_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
565 enum v4l2_field field)
567 struct front_face *front = q->priv_data;
568 int rc;
570 switch (front->type) {
571 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
572 if (VIDEOBUF_NEEDS_INIT == vb->state) {
573 struct v4l2_pix_format *pix;
575 pix = &front->pd->video_data.context.pix;
576 vb->size = pix->sizeimage; /* real frame size */
577 vb->width = pix->width;
578 vb->height = pix->height;
579 rc = videobuf_iolock(q, vb, NULL);
580 if (rc < 0)
581 return rc;
583 break;
584 case V4L2_BUF_TYPE_VBI_CAPTURE:
585 if (VIDEOBUF_NEEDS_INIT == vb->state) {
586 vb->size = front->pd->vbi_data.vbi_size;
587 rc = videobuf_iolock(q, vb, NULL);
588 if (rc < 0)
589 return rc;
591 break;
592 default:
593 return -EINVAL;
595 vb->field = field;
596 vb->state = VIDEOBUF_PREPARED;
597 return 0;
600 static int fire_all_urb(struct video_data *video)
602 int i, ret;
604 video->is_streaming = 1;
606 for (i = 0; i < SBUF_NUM; i++) {
607 ret = usb_submit_urb(video->urb_array[i], GFP_KERNEL);
608 if (ret)
609 log("(%d) failed: error %d", i, ret);
611 return ret;
614 static int start_video_stream(struct poseidon *pd)
616 struct video_data *video = &pd->video_data;
617 s32 cmd_status;
619 send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
620 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_START, &cmd_status);
622 if (pd->cur_transfer_mode) {
623 prepare_iso_urb(video);
624 INIT_WORK(&video->bubble_work, iso_bubble_handler);
625 } else {
626 /* The bulk mode does not need a bubble handler */
627 prepare_bulk_urb(video);
629 fire_all_urb(video);
630 return 0;
633 static int pd_buf_setup(struct videobuf_queue *q, unsigned int *count,
634 unsigned int *size)
636 struct front_face *front = q->priv_data;
637 struct poseidon *pd = front->pd;
639 switch (front->type) {
640 default:
641 return -EINVAL;
642 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
643 struct video_data *video = &pd->video_data;
644 struct v4l2_pix_format *pix = &video->context.pix;
646 *size = PAGE_ALIGN(pix->sizeimage);/* page aligned frame size */
647 if (*count < 4)
648 *count = 4;
649 if (1) {
650 /* same in different altersetting */
651 video->endpoint_addr = 0x82;
652 video->vbi = &pd->vbi_data;
653 video->vbi->video = video;
654 video->pd = pd;
655 video->lines_per_field = pix->height / 2;
656 video->lines_size = pix->width * 2;
657 video->front = front;
659 return start_video_stream(pd);
662 case V4L2_BUF_TYPE_VBI_CAPTURE: {
663 struct vbi_data *vbi = &pd->vbi_data;
665 *size = PAGE_ALIGN(vbi->vbi_size);
666 log("size : %d", *size);
667 if (*count == 0)
668 *count = 4;
670 break;
672 return 0;
675 static struct videobuf_queue_ops pd_video_qops = {
676 .buf_setup = pd_buf_setup,
677 .buf_prepare = pd_buf_prepare,
678 .buf_queue = pd_buf_queue,
679 .buf_release = pd_buf_release,
682 static int vidioc_enum_fmt(struct file *file, void *fh,
683 struct v4l2_fmtdesc *f)
685 if (ARRAY_SIZE(poseidon_formats) <= f->index)
686 return -EINVAL;
687 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
688 f->flags = 0;
689 f->pixelformat = poseidon_formats[f->index].fourcc;
690 strcpy(f->description, poseidon_formats[f->index].name);
691 return 0;
694 static int vidioc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
696 struct front_face *front = fh;
697 struct poseidon *pd = front->pd;
699 f->fmt.pix = pd->video_data.context.pix;
700 return 0;
704 * VLC calls VIDIOC_S_STD before VIDIOC_S_FMT, while
705 * Mplayer calls them in the reverse order.
707 static int pd_vidioc_s_fmt(struct poseidon *pd, struct v4l2_pix_format *pix)
709 struct video_data *video = &pd->video_data;
710 struct running_context *context = &video->context;
711 struct v4l2_pix_format *pix_def = &context->pix;
712 s32 ret = 0, cmd_status = 0, vid_resol;
714 /* set the pixel format to firmware */
715 if (pix->pixelformat == V4L2_PIX_FMT_RGB565) {
716 vid_resol = TLG_TUNER_VID_FORMAT_RGB_565;
717 } else {
718 pix->pixelformat = V4L2_PIX_FMT_YUYV;
719 vid_resol = TLG_TUNER_VID_FORMAT_YUV;
721 ret = send_set_req(pd, VIDEO_STREAM_FMT_SEL,
722 vid_resol, &cmd_status);
724 /* set the resolution to firmware */
725 vid_resol = TLG_TUNE_VID_RES_720;
726 switch (pix->width) {
727 case 704:
728 vid_resol = TLG_TUNE_VID_RES_704;
729 break;
730 default:
731 pix->width = 720;
732 case 720:
733 break;
735 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
736 vid_resol, &cmd_status);
737 if (ret || cmd_status)
738 return -EBUSY;
740 pix_def->pixelformat = pix->pixelformat; /* save it */
741 pix->height = (context->tvnormid & V4L2_STD_525_60) ? 480 : 576;
743 /* Compare with the default setting */
744 if ((pix_def->width != pix->width)
745 || (pix_def->height != pix->height)) {
746 pix_def->width = pix->width;
747 pix_def->height = pix->height;
748 pix_def->bytesperline = pix->width * 2;
749 pix_def->sizeimage = pix->width * pix->height * 2;
751 *pix = *pix_def;
753 return 0;
756 static int vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
758 struct front_face *front = fh;
759 struct poseidon *pd = front->pd;
761 /* stop VBI here */
762 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
763 return -EINVAL;
765 mutex_lock(&pd->lock);
766 if (pd->file_for_stream == NULL)
767 pd->file_for_stream = file;
768 else if (file != pd->file_for_stream) {
769 mutex_unlock(&pd->lock);
770 return -EINVAL;
773 pd_vidioc_s_fmt(pd, &f->fmt.pix);
774 mutex_unlock(&pd->lock);
775 return 0;
778 static int vidioc_g_fmt_vbi(struct file *file, void *fh,
779 struct v4l2_format *v4l2_f)
781 struct front_face *front = fh;
782 struct poseidon *pd = front->pd;
783 struct v4l2_vbi_format *vbi_fmt = &v4l2_f->fmt.vbi;
785 vbi_fmt->samples_per_line = 720 * 2;
786 vbi_fmt->sampling_rate = 6750000 * 4;
787 vbi_fmt->sample_format = V4L2_PIX_FMT_GREY;
788 vbi_fmt->offset = 64 * 4; /*FIXME: why offset */
789 if (pd->video_data.context.tvnormid & V4L2_STD_525_60) {
790 vbi_fmt->start[0] = 10;
791 vbi_fmt->start[1] = 264;
792 vbi_fmt->count[0] = V4L_NTSC_VBI_LINES;
793 vbi_fmt->count[1] = V4L_NTSC_VBI_LINES;
794 } else {
795 vbi_fmt->start[0] = 6;
796 vbi_fmt->start[1] = 314;
797 vbi_fmt->count[0] = V4L_PAL_VBI_LINES;
798 vbi_fmt->count[1] = V4L_PAL_VBI_LINES;
800 vbi_fmt->flags = V4L2_VBI_UNSYNC;
801 return 0;
804 static int set_std(struct poseidon *pd, v4l2_std_id norm)
806 struct video_data *video = &pd->video_data;
807 struct vbi_data *vbi = &pd->vbi_data;
808 struct running_context *context;
809 struct v4l2_pix_format *pix;
810 s32 i, ret = 0, cmd_status, param;
811 int height;
813 for (i = 0; i < POSEIDON_TVNORMS; i++) {
814 if (norm & poseidon_tvnorms[i].v4l2_id) {
815 param = poseidon_tvnorms[i].tlg_tvnorm;
816 log("name : %s", poseidon_tvnorms[i].name);
817 goto found;
820 return -EINVAL;
821 found:
822 mutex_lock(&pd->lock);
823 ret = send_set_req(pd, VIDEO_STD_SEL, param, &cmd_status);
824 if (ret || cmd_status)
825 goto out;
827 /* Set vbi size and check the height of the frame */
828 context = &video->context;
829 context->tvnormid = poseidon_tvnorms[i].v4l2_id;
830 if (context->tvnormid & V4L2_STD_525_60) {
831 vbi->vbi_size = V4L_NTSC_VBI_FRAMESIZE;
832 height = 480;
833 } else {
834 vbi->vbi_size = V4L_PAL_VBI_FRAMESIZE;
835 height = 576;
838 pix = &context->pix;
839 if (pix->height != height) {
840 pix->height = height;
841 pix->sizeimage = pix->width * pix->height * 2;
844 out:
845 mutex_unlock(&pd->lock);
846 return ret;
849 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id norm)
851 struct front_face *front = fh;
853 return set_std(front->pd, norm);
856 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
858 struct front_face *front = fh;
860 *norm = front->pd->video_data.context.tvnormid;
861 return 0;
864 static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *in)
866 if (in->index >= POSEIDON_INPUTS)
867 return -EINVAL;
868 strcpy(in->name, pd_inputs[in->index].name);
869 in->type = V4L2_INPUT_TYPE_TUNER;
872 * the audio input index mixed with this video input,
873 * Poseidon only have one audio/video, set to "0"
875 in->audioset = 1;
876 in->tuner = 0;
877 in->std = V4L2_STD_ALL;
878 in->status = 0;
879 return 0;
882 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
884 struct front_face *front = fh;
885 struct poseidon *pd = front->pd;
886 struct running_context *context = &pd->video_data.context;
888 *i = context->sig_index;
889 return 0;
892 /* We can support several inputs */
893 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
895 struct front_face *front = fh;
896 struct poseidon *pd = front->pd;
897 s32 ret, cmd_status;
899 if (i >= POSEIDON_INPUTS)
900 return -EINVAL;
901 ret = send_set_req(pd, SGNL_SRC_SEL,
902 pd_inputs[i].tlg_src, &cmd_status);
903 if (ret)
904 return ret;
906 pd->video_data.context.sig_index = i;
907 return 0;
910 static int tlg_s_ctrl(struct v4l2_ctrl *c)
912 struct poseidon *pd = container_of(c->handler, struct poseidon,
913 video_data.ctrl_handler);
914 struct tuner_custom_parameter_s param = {0};
915 s32 ret = 0, cmd_status, params;
917 switch (c->id) {
918 case V4L2_CID_BRIGHTNESS:
919 param.param_id = CUST_PARM_ID_BRIGHTNESS_CTRL;
920 break;
921 case V4L2_CID_CONTRAST:
922 param.param_id = CUST_PARM_ID_CONTRAST_CTRL;
923 break;
924 case V4L2_CID_HUE:
925 param.param_id = CUST_PARM_ID_HUE_CTRL;
926 break;
927 case V4L2_CID_SATURATION:
928 param.param_id = CUST_PARM_ID_SATURATION_CTRL;
929 break;
931 param.param_value = c->val;
932 params = *(s32 *)&param; /* temp code */
934 mutex_lock(&pd->lock);
935 ret = send_set_req(pd, TUNER_CUSTOM_PARAMETER, params, &cmd_status);
936 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
937 mutex_unlock(&pd->lock);
939 set_current_state(TASK_INTERRUPTIBLE);
940 schedule_timeout(HZ/4);
941 return ret;
944 /* Audio ioctls */
945 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
947 if (0 != a->index)
948 return -EINVAL;
949 a->capability = V4L2_AUDCAP_STEREO;
950 strcpy(a->name, "USB audio in");
951 /*Poseidon have no AVL function.*/
952 a->mode = 0;
953 return 0;
956 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
958 a->index = 0;
959 a->capability = V4L2_AUDCAP_STEREO;
960 strcpy(a->name, "USB audio in");
961 a->mode = 0;
962 return 0;
965 static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
967 return (0 == a->index) ? 0 : -EINVAL;
970 /* Tuner ioctls */
971 static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *tuner)
973 struct front_face *front = fh;
974 struct poseidon *pd = front->pd;
975 struct tuner_atv_sig_stat_s atv_stat;
976 s32 count = 5, ret, cmd_status;
977 int index;
979 if (0 != tuner->index)
980 return -EINVAL;
982 mutex_lock(&pd->lock);
983 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
984 &atv_stat, &cmd_status, sizeof(atv_stat));
986 while (atv_stat.sig_lock_busy && count-- && !ret) {
987 set_current_state(TASK_INTERRUPTIBLE);
988 schedule_timeout(HZ);
990 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
991 &atv_stat, &cmd_status, sizeof(atv_stat));
993 mutex_unlock(&pd->lock);
995 if (debug_mode)
996 log("P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
998 if (ret || cmd_status)
999 tuner->signal = 0;
1000 else if (atv_stat.sig_present && !atv_stat.sig_strength)
1001 tuner->signal = 0xFFFF;
1002 else
1003 tuner->signal = (atv_stat.sig_strength * 255 / 10) << 8;
1005 strcpy(tuner->name, "Telegent Systems");
1006 tuner->type = V4L2_TUNER_ANALOG_TV;
1007 tuner->rangelow = TUNER_FREQ_MIN / 62500;
1008 tuner->rangehigh = TUNER_FREQ_MAX / 62500;
1009 tuner->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1010 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1011 index = pd->video_data.context.audio_idx;
1012 tuner->rxsubchans = pd_audio_modes[index].v4l2_audio_sub;
1013 tuner->audmode = pd_audio_modes[index].v4l2_audio_mode;
1014 tuner->afc = 0;
1015 return 0;
1018 static int pd_vidioc_s_tuner(struct poseidon *pd, int index)
1020 s32 ret = 0, cmd_status, param, audiomode;
1022 mutex_lock(&pd->lock);
1023 param = pd_audio_modes[index].tlg_audio_mode;
1024 ret = send_set_req(pd, TUNER_AUD_MODE, param, &cmd_status);
1025 audiomode = get_audio_std(pd->video_data.context.tvnormid);
1026 ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode,
1027 &cmd_status);
1028 if (!ret)
1029 pd->video_data.context.audio_idx = index;
1030 mutex_unlock(&pd->lock);
1031 return ret;
1034 static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *a)
1036 struct front_face *front = fh;
1037 struct poseidon *pd = front->pd;
1038 int index;
1040 if (0 != a->index)
1041 return -EINVAL;
1042 for (index = 0; index < POSEIDON_AUDIOMODS; index++)
1043 if (a->audmode == pd_audio_modes[index].v4l2_audio_mode)
1044 return pd_vidioc_s_tuner(pd, index);
1045 return -EINVAL;
1048 static int vidioc_g_frequency(struct file *file, void *fh,
1049 struct v4l2_frequency *freq)
1051 struct front_face *front = fh;
1052 struct poseidon *pd = front->pd;
1053 struct running_context *context = &pd->video_data.context;
1055 if (0 != freq->tuner)
1056 return -EINVAL;
1057 freq->frequency = context->freq;
1058 freq->type = V4L2_TUNER_ANALOG_TV;
1059 return 0;
1062 static int set_frequency(struct poseidon *pd, u32 *frequency)
1064 s32 ret = 0, param, cmd_status;
1065 struct running_context *context = &pd->video_data.context;
1067 *frequency = clamp(*frequency,
1068 TUNER_FREQ_MIN / 62500, TUNER_FREQ_MAX / 62500);
1069 param = (*frequency) * 62500 / 1000;
1071 mutex_lock(&pd->lock);
1072 ret = send_set_req(pd, TUNE_FREQ_SELECT, param, &cmd_status);
1073 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
1075 msleep(250); /* wait for a while until the hardware is ready. */
1076 context->freq = *frequency;
1077 mutex_unlock(&pd->lock);
1078 return ret;
1081 static int vidioc_s_frequency(struct file *file, void *fh,
1082 const struct v4l2_frequency *freq)
1084 struct front_face *front = fh;
1085 struct poseidon *pd = front->pd;
1086 u32 frequency = freq->frequency;
1088 if (freq->tuner)
1089 return -EINVAL;
1090 #ifdef CONFIG_PM
1091 pd->pm_suspend = pm_video_suspend;
1092 pd->pm_resume = pm_video_resume;
1093 #endif
1094 return set_frequency(pd, &frequency);
1097 static int vidioc_reqbufs(struct file *file, void *fh,
1098 struct v4l2_requestbuffers *b)
1100 struct front_face *front = file->private_data;
1101 return videobuf_reqbufs(&front->q, b);
1104 static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
1106 struct front_face *front = file->private_data;
1107 return videobuf_querybuf(&front->q, b);
1110 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1112 struct front_face *front = file->private_data;
1113 return videobuf_qbuf(&front->q, b);
1116 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1118 struct front_face *front = file->private_data;
1119 return videobuf_dqbuf(&front->q, b, file->f_flags & O_NONBLOCK);
1122 /* Just stop the URBs, do not free the URBs */
1123 static int usb_transfer_stop(struct video_data *video)
1125 if (video->is_streaming) {
1126 int i;
1127 s32 cmd_status;
1128 struct poseidon *pd = video->pd;
1130 video->is_streaming = 0;
1131 for (i = 0; i < SBUF_NUM; ++i) {
1132 if (video->urb_array[i])
1133 usb_kill_urb(video->urb_array[i]);
1136 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1137 &cmd_status);
1139 return 0;
1142 int stop_all_video_stream(struct poseidon *pd)
1144 struct video_data *video = &pd->video_data;
1145 struct vbi_data *vbi = &pd->vbi_data;
1147 mutex_lock(&pd->lock);
1148 if (video->is_streaming) {
1149 struct front_face *front = video->front;
1151 /* stop the URBs */
1152 usb_transfer_stop(video);
1153 free_all_urb(video);
1155 /* stop the host side of VIDEO */
1156 videobuf_stop(&front->q);
1157 videobuf_mmap_free(&front->q);
1159 /* stop the host side of VBI */
1160 front = vbi->front;
1161 if (front) {
1162 videobuf_stop(&front->q);
1163 videobuf_mmap_free(&front->q);
1166 mutex_unlock(&pd->lock);
1167 return 0;
1171 * The bubbles can seriously damage the video's quality,
1172 * though it occurs in very rare situation.
1174 static void iso_bubble_handler(struct work_struct *w)
1176 struct video_data *video;
1177 struct poseidon *pd;
1179 video = container_of(w, struct video_data, bubble_work);
1180 pd = video->pd;
1182 mutex_lock(&pd->lock);
1183 usb_transfer_stop(video);
1184 msleep(500);
1185 start_video_stream(pd);
1186 mutex_unlock(&pd->lock);
1190 static int vidioc_streamon(struct file *file, void *fh,
1191 enum v4l2_buf_type type)
1193 struct front_face *front = fh;
1195 if (unlikely(type != front->type))
1196 return -EINVAL;
1197 return videobuf_streamon(&front->q);
1200 static int vidioc_streamoff(struct file *file, void *fh,
1201 enum v4l2_buf_type type)
1203 struct front_face *front = file->private_data;
1205 if (unlikely(type != front->type))
1206 return -EINVAL;
1207 return videobuf_streamoff(&front->q);
1210 /* Set the firmware's default values : need altersetting */
1211 static int pd_video_checkmode(struct poseidon *pd)
1213 s32 ret = 0, cmd_status, audiomode;
1215 set_current_state(TASK_INTERRUPTIBLE);
1216 schedule_timeout(HZ/2);
1218 /* choose the altersetting */
1219 ret = usb_set_interface(pd->udev, 0,
1220 (pd->cur_transfer_mode ?
1221 ISO_3K_BULK_ALTERNATE_IFACE :
1222 BULK_ALTERNATE_IFACE));
1223 if (ret < 0)
1224 goto error;
1226 /* set default parameters for PAL-D , with the VBI enabled*/
1227 ret = set_tuner_mode(pd, TLG_MODE_ANALOG_TV);
1228 ret |= send_set_req(pd, SGNL_SRC_SEL,
1229 TLG_SIG_SRC_ANTENNA, &cmd_status);
1230 ret |= send_set_req(pd, VIDEO_STD_SEL,
1231 TLG_TUNE_VSTD_PAL_D, &cmd_status);
1232 ret |= send_set_req(pd, VIDEO_STREAM_FMT_SEL,
1233 TLG_TUNER_VID_FORMAT_YUV, &cmd_status);
1234 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
1235 TLG_TUNE_VID_RES_720, &cmd_status);
1236 ret |= send_set_req(pd, TUNE_FREQ_SELECT, TUNER_FREQ_MIN, &cmd_status);
1237 ret |= send_set_req(pd, VBI_DATA_SEL, 1, &cmd_status);/* enable vbi */
1239 /* set the audio */
1240 audiomode = get_audio_std(pd->video_data.context.tvnormid);
1241 ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode, &cmd_status);
1242 ret |= send_set_req(pd, TUNER_AUD_MODE,
1243 TLG_TUNE_TVAUDIO_MODE_STEREO, &cmd_status);
1244 ret |= send_set_req(pd, AUDIO_SAMPLE_RATE_SEL,
1245 ATV_AUDIO_RATE_48K, &cmd_status);
1246 error:
1247 return ret;
1250 #ifdef CONFIG_PM
1251 static int pm_video_suspend(struct poseidon *pd)
1253 /* stop audio */
1254 pm_alsa_suspend(pd);
1256 /* stop and free all the URBs */
1257 usb_transfer_stop(&pd->video_data);
1258 free_all_urb(&pd->video_data);
1260 /* reset the interface */
1261 usb_set_interface(pd->udev, 0, 0);
1262 msleep(300);
1263 return 0;
1266 static int restore_v4l2_context(struct poseidon *pd,
1267 struct running_context *context)
1269 struct front_face *front = pd->video_data.front;
1271 pd_video_checkmode(pd);
1273 set_std(pd, context->tvnormid);
1274 vidioc_s_input(NULL, front, context->sig_index);
1275 pd_vidioc_s_tuner(pd, context->audio_idx);
1276 pd_vidioc_s_fmt(pd, &context->pix);
1277 set_frequency(pd, &context->freq);
1278 return 0;
1281 static int pm_video_resume(struct poseidon *pd)
1283 struct video_data *video = &pd->video_data;
1285 /* resume the video */
1286 /* [1] restore the origin V4L2 parameters */
1287 restore_v4l2_context(pd, &video->context);
1289 /* [2] initiate video copy variables */
1290 if (video->front->curr_frame)
1291 init_copy(video, 0);
1293 /* [3] fire urbs */
1294 start_video_stream(pd);
1296 /* resume the audio */
1297 pm_alsa_resume(pd);
1298 return 0;
1300 #endif
1302 void set_debug_mode(struct video_device *vfd, int debug_mode)
1304 vfd->debug = 0;
1305 if (debug_mode & 0x1)
1306 vfd->debug = V4L2_DEBUG_IOCTL;
1307 if (debug_mode & 0x2)
1308 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1311 static void init_video_context(struct running_context *context)
1313 context->sig_index = 0;
1314 context->audio_idx = 1; /* stereo */
1315 context->tvnormid = V4L2_STD_PAL_D;
1316 context->pix = (struct v4l2_pix_format) {
1317 .width = 720,
1318 .height = 576,
1319 .pixelformat = V4L2_PIX_FMT_YUYV,
1320 .field = V4L2_FIELD_INTERLACED,
1321 .bytesperline = 720 * 2,
1322 .sizeimage = 720 * 576 * 2,
1323 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1324 .priv = 0
1328 static int pd_video_open(struct file *file)
1330 struct video_device *vfd = video_devdata(file);
1331 struct poseidon *pd = video_get_drvdata(vfd);
1332 struct front_face *front = NULL;
1333 int ret = -ENOMEM;
1335 mutex_lock(&pd->lock);
1336 usb_autopm_get_interface(pd->interface);
1338 if (pd->state && !(pd->state & POSEIDON_STATE_ANALOG)) {
1339 ret = -EBUSY;
1340 goto out;
1342 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1343 if (!front)
1344 goto out;
1345 if (vfd->vfl_type == VFL_TYPE_GRABBER) {
1346 pd->cur_transfer_mode = usb_transfer_mode;/* bulk or iso */
1347 init_video_context(&pd->video_data.context);
1349 ret = pd_video_checkmode(pd);
1350 if (ret < 0) {
1351 kfree(front);
1352 ret = -1;
1353 goto out;
1356 front->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1357 pd->video_data.users++;
1358 set_debug_mode(vfd, debug_mode);
1360 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1361 NULL, &front->queue_lock,
1362 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1363 V4L2_FIELD_INTERLACED,/* video is interlacd */
1364 sizeof(struct videobuf_buffer),/*it's enough*/
1365 front, NULL);
1366 } else {
1367 front->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1368 pd->vbi_data.front = front;
1369 pd->vbi_data.users++;
1371 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1372 NULL, &front->queue_lock,
1373 V4L2_BUF_TYPE_VBI_CAPTURE,
1374 V4L2_FIELD_NONE, /* vbi is NONE mode */
1375 sizeof(struct videobuf_buffer),
1376 front, NULL);
1379 pd->state |= POSEIDON_STATE_ANALOG;
1380 front->pd = pd;
1381 front->curr_frame = NULL;
1382 INIT_LIST_HEAD(&front->active);
1383 spin_lock_init(&front->queue_lock);
1385 file->private_data = front;
1386 kref_get(&pd->kref);
1388 mutex_unlock(&pd->lock);
1389 return 0;
1390 out:
1391 usb_autopm_put_interface(pd->interface);
1392 mutex_unlock(&pd->lock);
1393 return ret;
1396 static int pd_video_release(struct file *file)
1398 struct front_face *front = file->private_data;
1399 struct poseidon *pd = front->pd;
1400 s32 cmd_status = 0;
1402 mutex_lock(&pd->lock);
1404 if (front->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1405 /* stop the device, and free the URBs */
1406 usb_transfer_stop(&pd->video_data);
1407 free_all_urb(&pd->video_data);
1409 /* stop the firmware */
1410 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1411 &cmd_status);
1413 pd->file_for_stream = NULL;
1414 pd->video_data.users--;
1415 } else if (front->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1416 pd->vbi_data.front = NULL;
1417 pd->vbi_data.users--;
1419 if (!pd->vbi_data.users && !pd->video_data.users)
1420 pd->state &= ~POSEIDON_STATE_ANALOG;
1421 videobuf_stop(&front->q);
1422 videobuf_mmap_free(&front->q);
1424 usb_autopm_put_interface(pd->interface);
1425 mutex_unlock(&pd->lock);
1427 kfree(front);
1428 file->private_data = NULL;
1429 kref_put(&pd->kref, poseidon_delete);
1430 return 0;
1433 static int pd_video_mmap(struct file *file, struct vm_area_struct *vma)
1435 struct front_face *front = file->private_data;
1436 return videobuf_mmap_mapper(&front->q, vma);
1439 static unsigned int pd_video_poll(struct file *file, poll_table *table)
1441 struct front_face *front = file->private_data;
1442 return videobuf_poll_stream(file, &front->q, table);
1445 static ssize_t pd_video_read(struct file *file, char __user *buffer,
1446 size_t count, loff_t *ppos)
1448 struct front_face *front = file->private_data;
1449 return videobuf_read_stream(&front->q, buffer, count, ppos,
1450 0, file->f_flags & O_NONBLOCK);
1453 /* This struct works for both VIDEO and VBI */
1454 static const struct v4l2_file_operations pd_video_fops = {
1455 .owner = THIS_MODULE,
1456 .open = pd_video_open,
1457 .release = pd_video_release,
1458 .read = pd_video_read,
1459 .poll = pd_video_poll,
1460 .mmap = pd_video_mmap,
1461 .ioctl = video_ioctl2, /* maybe changed in future */
1464 static const struct v4l2_ioctl_ops pd_video_ioctl_ops = {
1465 .vidioc_querycap = vidioc_querycap,
1467 /* Video format */
1468 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
1469 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1470 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
1471 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi, /* VBI */
1473 /* Input */
1474 .vidioc_g_input = vidioc_g_input,
1475 .vidioc_s_input = vidioc_s_input,
1476 .vidioc_enum_input = vidioc_enum_input,
1478 /* Audio ioctls */
1479 .vidioc_enumaudio = vidioc_enumaudio,
1480 .vidioc_g_audio = vidioc_g_audio,
1481 .vidioc_s_audio = vidioc_s_audio,
1483 /* Tuner ioctls */
1484 .vidioc_g_tuner = vidioc_g_tuner,
1485 .vidioc_s_tuner = vidioc_s_tuner,
1486 .vidioc_g_std = vidioc_g_std,
1487 .vidioc_s_std = vidioc_s_std,
1488 .vidioc_g_frequency = vidioc_g_frequency,
1489 .vidioc_s_frequency = vidioc_s_frequency,
1491 /* Buffer handlers */
1492 .vidioc_reqbufs = vidioc_reqbufs,
1493 .vidioc_querybuf = vidioc_querybuf,
1494 .vidioc_qbuf = vidioc_qbuf,
1495 .vidioc_dqbuf = vidioc_dqbuf,
1497 /* Stream on/off */
1498 .vidioc_streamon = vidioc_streamon,
1499 .vidioc_streamoff = vidioc_streamoff,
1502 static struct video_device pd_video_template = {
1503 .name = "Telegent-Video",
1504 .fops = &pd_video_fops,
1505 .minor = -1,
1506 .release = video_device_release_empty,
1507 .tvnorms = V4L2_STD_ALL,
1508 .ioctl_ops = &pd_video_ioctl_ops,
1511 static const struct v4l2_ctrl_ops tlg_ctrl_ops = {
1512 .s_ctrl = tlg_s_ctrl,
1515 void pd_video_exit(struct poseidon *pd)
1517 struct video_data *video = &pd->video_data;
1518 struct vbi_data *vbi = &pd->vbi_data;
1520 video_unregister_device(&video->v_dev);
1521 video_unregister_device(&vbi->v_dev);
1522 v4l2_ctrl_handler_free(&video->ctrl_handler);
1523 log();
1526 int pd_video_init(struct poseidon *pd)
1528 struct video_data *video = &pd->video_data;
1529 struct vbi_data *vbi = &pd->vbi_data;
1530 struct v4l2_ctrl_handler *hdl = &video->ctrl_handler;
1531 u32 freq = TUNER_FREQ_MIN / 62500;
1532 int ret = -ENOMEM;
1534 v4l2_ctrl_handler_init(hdl, 4);
1535 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_BRIGHTNESS,
1536 0, 10000, 1, 100);
1537 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_CONTRAST,
1538 0, 10000, 1, 100);
1539 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_HUE,
1540 0, 10000, 1, 100);
1541 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_SATURATION,
1542 0, 10000, 1, 100);
1543 if (hdl->error) {
1544 v4l2_ctrl_handler_free(hdl);
1545 return hdl->error;
1547 set_frequency(pd, &freq);
1548 video->v_dev = pd_video_template;
1549 video->v_dev.v4l2_dev = &pd->v4l2_dev;
1550 video->v_dev.ctrl_handler = hdl;
1551 video_set_drvdata(&video->v_dev, pd);
1553 ret = video_register_device(&video->v_dev, VFL_TYPE_GRABBER, -1);
1554 if (ret != 0)
1555 goto out;
1557 /* VBI uses the same template as video */
1558 vbi->v_dev = pd_video_template;
1559 vbi->v_dev.v4l2_dev = &pd->v4l2_dev;
1560 vbi->v_dev.ctrl_handler = hdl;
1561 video_set_drvdata(&vbi->v_dev, pd);
1562 ret = video_register_device(&vbi->v_dev, VFL_TYPE_VBI, -1);
1563 if (ret != 0)
1564 goto out;
1565 log("register VIDEO/VBI devices");
1566 return 0;
1567 out:
1568 log("VIDEO/VBI devices register failed, : %d", ret);
1569 pd_video_exit(pd);
1570 return ret;