2 #include <linux/vmalloc.h>
3 #include <linux/videodev2.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"
17 static int pm_video_suspend(struct poseidon *pd);
18 static int pm_video_resume(struct poseidon *pd);
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 {
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);
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;
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)
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;
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;
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;
158 spin_lock(&front->queue_lock);
159 if (!list_empty(&front->active)) {
160 vb = list_entry(front->active.next,
161 struct videobuf_buffer, queue);
164 front->curr_frame = vb;
165 list_del_init(&vb->queue);
167 spin_unlock(&front->queue_lock);
172 /* check if the video's buffer is ready */
173 static bool get_video_frame(struct front_face *front, struct video_data *video)
178 ret = get_frame(front, &need_init);
179 if (ret && need_init)
184 static void submit_frame(struct front_face *front)
186 struct videobuf_buffer *vb = front->curr_frame;
191 front->curr_frame = NULL;
192 vb->state = VIDEOBUF_DONE;
194 v4l2_get_timestamp(&vb->ts);
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);
211 static void copy_video_data(struct video_data *video, char *src,
214 #define copy_data(len) \
216 if (++video->lines_copied > video->lines_per_field) \
218 memcpy(video->dst, src, len);\
219 video->dst += len + video->lines_size; \
224 while (count && count >= video->lines_size) {
225 if (video->prev_left) {
226 copy_data(video->prev_left);
227 video->prev_left = 0;
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;
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 */
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)
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);
268 copy_video_data(video, src, offset);
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);
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;
322 if (!video->is_streaming || urb->status) {
323 if (urb->status == -EPROTO)
327 if (!get_video_frame(front, video))
330 if (count == urb->transfer_buffer_length)
331 copy_vbi_video_data(video, src, count);
333 check_trailer(video, src, count);
336 ret = usb_submit_urb(urb, GFP_ATOMIC);
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)
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;
362 /* This is the gap */
363 if (pkt->status || pkt->actual_length <= 0
364 || pkt->actual_length > ISO_PKT_SIZE) {
370 /* a good isochronous packet */
371 if (pkt->actual_length == ISO_PKT_SIZE) {
378 /* trailer is here */
379 if (pkt->actual_length < ISO_PKT_SIZE) {
389 if (*head == -1 && *tail == -1)
395 * |__|------|___|-----|_______|
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;
408 if (!video->is_streaming)
412 if (!get_video_frame(front, video))
415 switch (get_chunk(head, urb, &head, &tail, &bubble_err)) {
417 copy_vbi_video_data(video, src + (head * ISO_PKT_SIZE),
418 (tail - head + 1) * ISO_PKT_SIZE);
421 check_trailer(video, src + (head * ISO_PKT_SIZE),
426 case GET_TOO_MUCH_BUBBLE:
427 log("\t We got too much bubble");
428 schedule_work(&video->bubble_work);
431 } while (head = tail + 1, head < urb->number_of_packets);
434 ret = usb_submit_urb(urb, GFP_ATOMIC);
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;
445 if (video->urb_array[0])
448 for (i = 0; i < SBUF_NUM; i++) {
453 urb = usb_alloc_urb(PK_PER_URB, GFP_KERNEL);
457 video->urb_array[i] = urb;
458 mem = usb_alloc_coherent(udev,
459 ISO_PKT_SIZE * PK_PER_URB,
463 urb->complete = urb_complete_iso; /* handler */
465 urb->context = video->front;
466 urb->pipe = usb_rcvisocpipe(udev,
467 video->endpoint_addr);
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;
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)
494 for (; i < num; i++) {
496 struct urb *urb = usb_alloc_urb(0, gfp_flags);
500 mem = usb_alloc_coherent(udev, buf_size, gfp_flags,
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;
515 void free_all_urb_generic(struct urb **urb_array, int num)
520 for (i = 0; i < num; i++) {
523 usb_free_coherent(urb->dev,
524 urb->transfer_buffer_length,
525 urb->transfer_buffer,
533 static int prepare_bulk_urb(struct video_data *video)
535 if (video->urb_array[0])
538 alloc_bulk_urbs_generic(video->urb_array, SBUF_NUM,
539 video->pd->udev, video->endpoint_addr,
541 urb_complete_bulk, video->front);
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;
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);
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);
596 vb->state = VIDEOBUF_PREPARED;
600 static int fire_all_urb(struct video_data *video)
604 video->is_streaming = 1;
606 for (i = 0; i < SBUF_NUM; i++) {
607 ret = usb_submit_urb(video->urb_array[i], GFP_KERNEL);
609 log("(%d) failed: error %d", i, ret);
614 static int start_video_stream(struct poseidon *pd)
616 struct video_data *video = &pd->video_data;
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);
626 /* The bulk mode does not need a bubble handler */
627 prepare_bulk_urb(video);
633 static int pd_buf_setup(struct videobuf_queue *q, unsigned int *count,
636 struct front_face *front = q->priv_data;
637 struct poseidon *pd = front->pd;
639 switch (front->type) {
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 */
650 /* same in different altersetting */
651 video->endpoint_addr = 0x82;
652 video->vbi = &pd->vbi_data;
653 video->vbi->video = video;
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);
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)
687 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
689 f->pixelformat = poseidon_formats[f->index].fourcc;
690 strcpy(f->description, poseidon_formats[f->index].name);
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;
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;
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) {
728 vid_resol = TLG_TUNE_VID_RES_704;
735 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
736 vid_resol, &cmd_status);
737 if (ret || cmd_status)
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;
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;
762 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
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);
773 pd_vidioc_s_fmt(pd, &f->fmt.pix);
774 mutex_unlock(&pd->lock);
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;
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;
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;
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);
822 mutex_lock(&pd->lock);
823 ret = send_set_req(pd, VIDEO_STD_SEL, param, &cmd_status);
824 if (ret || cmd_status)
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;
834 vbi->vbi_size = V4L_PAL_VBI_FRAMESIZE;
839 if (pix->height != height) {
840 pix->height = height;
841 pix->sizeimage = pix->width * pix->height * 2;
845 mutex_unlock(&pd->lock);
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;
864 static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *in)
866 if (in->index >= POSEIDON_INPUTS)
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"
877 in->std = V4L2_STD_ALL;
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;
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;
899 if (i >= POSEIDON_INPUTS)
901 ret = send_set_req(pd, SGNL_SRC_SEL,
902 pd_inputs[i].tlg_src, &cmd_status);
906 pd->video_data.context.sig_index = i;
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;
918 case V4L2_CID_BRIGHTNESS:
919 param.param_id = CUST_PARM_ID_BRIGHTNESS_CTRL;
921 case V4L2_CID_CONTRAST:
922 param.param_id = CUST_PARM_ID_CONTRAST_CTRL;
925 param.param_id = CUST_PARM_ID_HUE_CTRL;
927 case V4L2_CID_SATURATION:
928 param.param_id = CUST_PARM_ID_SATURATION_CTRL;
931 param.param_value = c->val;
932 params = *(s32 *)¶m; /* 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);
945 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
949 a->capability = V4L2_AUDCAP_STEREO;
950 strcpy(a->name, "USB audio in");
951 /*Poseidon have no AVL function.*/
956 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
959 a->capability = V4L2_AUDCAP_STEREO;
960 strcpy(a->name, "USB audio in");
965 static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
967 return (0 == a->index) ? 0 : -EINVAL;
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;
979 if (0 != tuner->index)
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);
996 log("P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
998 if (ret || cmd_status)
1000 else if (atv_stat.sig_present && !atv_stat.sig_strength)
1001 tuner->signal = 0xFFFF;
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;
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,
1029 pd->video_data.context.audio_idx = index;
1030 mutex_unlock(&pd->lock);
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;
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);
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)
1057 freq->frequency = context->freq;
1058 freq->type = V4L2_TUNER_ANALOG_TV;
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);
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;
1091 pd->pm_suspend = pm_video_suspend;
1092 pd->pm_resume = pm_video_resume;
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) {
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,
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;
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 */
1162 videobuf_stop(&front->q);
1163 videobuf_mmap_free(&front->q);
1166 mutex_unlock(&pd->lock);
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);
1182 mutex_lock(&pd->lock);
1183 usb_transfer_stop(video);
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))
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))
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));
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 */
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);
1251 static int pm_video_suspend(struct poseidon *pd)
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);
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);
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);
1294 start_video_stream(pd);
1296 /* resume the audio */
1302 void set_debug_mode(struct video_device *vfd, int debug_mode)
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) {
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,
1327 static int pd_video_open(struct file *file)
1329 struct video_device *vfd = video_devdata(file);
1330 struct poseidon *pd = video_get_drvdata(vfd);
1331 struct front_face *front = NULL;
1334 mutex_lock(&pd->lock);
1335 usb_autopm_get_interface(pd->interface);
1337 if (pd->state && !(pd->state & POSEIDON_STATE_ANALOG)) {
1341 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1344 if (vfd->vfl_type == VFL_TYPE_GRABBER) {
1345 pd->cur_transfer_mode = usb_transfer_mode;/* bulk or iso */
1346 init_video_context(&pd->video_data.context);
1348 ret = pd_video_checkmode(pd);
1355 front->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1356 pd->video_data.users++;
1357 set_debug_mode(vfd, debug_mode);
1359 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1360 NULL, &front->queue_lock,
1361 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1362 V4L2_FIELD_INTERLACED,/* video is interlacd */
1363 sizeof(struct videobuf_buffer),/*it's enough*/
1366 front->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1367 pd->vbi_data.front = front;
1368 pd->vbi_data.users++;
1370 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1371 NULL, &front->queue_lock,
1372 V4L2_BUF_TYPE_VBI_CAPTURE,
1373 V4L2_FIELD_NONE, /* vbi is NONE mode */
1374 sizeof(struct videobuf_buffer),
1378 pd->state |= POSEIDON_STATE_ANALOG;
1380 front->curr_frame = NULL;
1381 INIT_LIST_HEAD(&front->active);
1382 spin_lock_init(&front->queue_lock);
1384 file->private_data = front;
1385 kref_get(&pd->kref);
1387 mutex_unlock(&pd->lock);
1390 usb_autopm_put_interface(pd->interface);
1391 mutex_unlock(&pd->lock);
1395 static int pd_video_release(struct file *file)
1397 struct front_face *front = file->private_data;
1398 struct poseidon *pd = front->pd;
1401 mutex_lock(&pd->lock);
1403 if (front->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1404 /* stop the device, and free the URBs */
1405 usb_transfer_stop(&pd->video_data);
1406 free_all_urb(&pd->video_data);
1408 /* stop the firmware */
1409 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1412 pd->file_for_stream = NULL;
1413 pd->video_data.users--;
1414 } else if (front->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1415 pd->vbi_data.front = NULL;
1416 pd->vbi_data.users--;
1418 if (!pd->vbi_data.users && !pd->video_data.users)
1419 pd->state &= ~POSEIDON_STATE_ANALOG;
1420 videobuf_stop(&front->q);
1421 videobuf_mmap_free(&front->q);
1423 usb_autopm_put_interface(pd->interface);
1424 mutex_unlock(&pd->lock);
1427 file->private_data = NULL;
1428 kref_put(&pd->kref, poseidon_delete);
1432 static int pd_video_mmap(struct file *file, struct vm_area_struct *vma)
1434 struct front_face *front = file->private_data;
1435 return videobuf_mmap_mapper(&front->q, vma);
1438 static unsigned int pd_video_poll(struct file *file, poll_table *table)
1440 struct front_face *front = file->private_data;
1441 return videobuf_poll_stream(file, &front->q, table);
1444 static ssize_t pd_video_read(struct file *file, char __user *buffer,
1445 size_t count, loff_t *ppos)
1447 struct front_face *front = file->private_data;
1448 return videobuf_read_stream(&front->q, buffer, count, ppos,
1449 0, file->f_flags & O_NONBLOCK);
1452 /* This struct works for both VIDEO and VBI */
1453 static const struct v4l2_file_operations pd_video_fops = {
1454 .owner = THIS_MODULE,
1455 .open = pd_video_open,
1456 .release = pd_video_release,
1457 .read = pd_video_read,
1458 .poll = pd_video_poll,
1459 .mmap = pd_video_mmap,
1460 .ioctl = video_ioctl2, /* maybe changed in future */
1463 static const struct v4l2_ioctl_ops pd_video_ioctl_ops = {
1464 .vidioc_querycap = vidioc_querycap,
1467 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
1468 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1469 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
1470 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi, /* VBI */
1473 .vidioc_g_input = vidioc_g_input,
1474 .vidioc_s_input = vidioc_s_input,
1475 .vidioc_enum_input = vidioc_enum_input,
1478 .vidioc_enumaudio = vidioc_enumaudio,
1479 .vidioc_g_audio = vidioc_g_audio,
1480 .vidioc_s_audio = vidioc_s_audio,
1483 .vidioc_g_tuner = vidioc_g_tuner,
1484 .vidioc_s_tuner = vidioc_s_tuner,
1485 .vidioc_g_std = vidioc_g_std,
1486 .vidioc_s_std = vidioc_s_std,
1487 .vidioc_g_frequency = vidioc_g_frequency,
1488 .vidioc_s_frequency = vidioc_s_frequency,
1490 /* Buffer handlers */
1491 .vidioc_reqbufs = vidioc_reqbufs,
1492 .vidioc_querybuf = vidioc_querybuf,
1493 .vidioc_qbuf = vidioc_qbuf,
1494 .vidioc_dqbuf = vidioc_dqbuf,
1497 .vidioc_streamon = vidioc_streamon,
1498 .vidioc_streamoff = vidioc_streamoff,
1501 static struct video_device pd_video_template = {
1502 .name = "Telegent-Video",
1503 .fops = &pd_video_fops,
1505 .release = video_device_release_empty,
1506 .tvnorms = V4L2_STD_ALL,
1507 .ioctl_ops = &pd_video_ioctl_ops,
1510 static const struct v4l2_ctrl_ops tlg_ctrl_ops = {
1511 .s_ctrl = tlg_s_ctrl,
1514 void pd_video_exit(struct poseidon *pd)
1516 struct video_data *video = &pd->video_data;
1517 struct vbi_data *vbi = &pd->vbi_data;
1519 video_unregister_device(&video->v_dev);
1520 video_unregister_device(&vbi->v_dev);
1521 v4l2_ctrl_handler_free(&video->ctrl_handler);
1525 int pd_video_init(struct poseidon *pd)
1527 struct video_data *video = &pd->video_data;
1528 struct vbi_data *vbi = &pd->vbi_data;
1529 struct v4l2_ctrl_handler *hdl = &video->ctrl_handler;
1530 u32 freq = TUNER_FREQ_MIN / 62500;
1533 v4l2_ctrl_handler_init(hdl, 4);
1534 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_BRIGHTNESS,
1536 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_CONTRAST,
1538 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_HUE,
1540 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_SATURATION,
1543 v4l2_ctrl_handler_free(hdl);
1546 set_frequency(pd, &freq);
1547 video->v_dev = pd_video_template;
1548 video->v_dev.v4l2_dev = &pd->v4l2_dev;
1549 video->v_dev.ctrl_handler = hdl;
1550 video_set_drvdata(&video->v_dev, pd);
1552 ret = video_register_device(&video->v_dev, VFL_TYPE_GRABBER, -1);
1556 /* VBI uses the same template as video */
1557 vbi->v_dev = pd_video_template;
1558 vbi->v_dev.v4l2_dev = &pd->v4l2_dev;
1559 vbi->v_dev.ctrl_handler = hdl;
1560 video_set_drvdata(&vbi->v_dev, pd);
1561 ret = video_register_device(&vbi->v_dev, VFL_TYPE_VBI, -1);
1564 log("register VIDEO/VBI devices");
1567 log("VIDEO/VBI devices register failed, : %d", ret);