blob: 5f0300ac465cd2853cb461664d4b6f3b41ceaca3 [file] [log] [blame]
Huang Shijie5b3f03f2010-02-02 04:07:47 -03001#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
8#include <media/v4l2-ioctl.h>
9#include <media/v4l2-dev.h>
10
11#include "pd-common.h"
12#include "vendorcmds.h"
13
14static int pm_video_suspend(struct poseidon *pd);
15static int pm_video_resume(struct poseidon *pd);
16static void iso_bubble_handler(struct work_struct *w);
17
18int country_code = 86;
19module_param(country_code, int, 0644);
20MODULE_PARM_DESC(country_code, "country code (e.g China is 86)");
21
22int usb_transfer_mode;
23module_param(usb_transfer_mode, int, 0644);
24MODULE_PARM_DESC(usb_transfer_mode, "0 = Bulk, 1 = Isochronous");
25
26static const struct poseidon_format poseidon_formats[] = {
27 { "YUV 422", V4L2_PIX_FMT_YUYV, 16, 0},
28 { "RGB565", V4L2_PIX_FMT_RGB565, 16, 0},
29};
30
31static 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 },
50};
51static const unsigned int POSEIDON_TVNORMS = ARRAY_SIZE(poseidon_tvnorms);
52
53struct pd_audio_mode {
54 u32 tlg_audio_mode;
55 u32 v4l2_audio_sub;
56 u32 v4l2_audio_mode;
57};
58
59static 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 }
70};
71static const unsigned int POSEIDON_AUDIOMODS = ARRAY_SIZE(pd_audio_modes);
72
73struct pd_input {
74 char *name;
75 uint32_t tlg_src;
76};
77
78static 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 }
83};
84static const unsigned int POSEIDON_INPUTS = ARRAY_SIZE(pd_inputs);
85
86struct poseidon_control {
87 struct v4l2_queryctrl v4l2_ctrl;
88 enum cmd_custom_param_id vc_id;
89};
90
91static struct poseidon_control controls[] = {
92 {
93 { V4L2_CID_BRIGHTNESS, V4L2_CTRL_TYPE_INTEGER,
94 "brightness", 0, 10000, 1, 100, 0, },
95 CUST_PARM_ID_BRIGHTNESS_CTRL
96 },
97
98 {
99 { V4L2_CID_CONTRAST, V4L2_CTRL_TYPE_INTEGER,
100 "contrast", 0, 10000, 1, 100, 0, },
101 CUST_PARM_ID_CONTRAST_CTRL,
102 },
103
104 {
105 { V4L2_CID_HUE, V4L2_CTRL_TYPE_INTEGER,
106 "hue", 0, 10000, 1, 100, 0, },
107 CUST_PARM_ID_HUE_CTRL,
108 },
109
110 {
111 { V4L2_CID_SATURATION, V4L2_CTRL_TYPE_INTEGER,
112 "saturation", 0, 10000, 1, 100, 0, },
113 CUST_PARM_ID_SATURATION_CTRL,
114 },
115};
116
117static int vidioc_querycap(struct file *file, void *fh,
118 struct v4l2_capability *cap)
119{
120 struct front_face *front = fh;
121 struct poseidon *p = front->pd;
122
123 logs(front);
124
125 strcpy(cap->driver, "tele-video");
126 strcpy(cap->card, "Telegent Poseidon");
127 usb_make_path(p->udev, cap->bus_info, sizeof(cap->bus_info));
128 cap->version = KERNEL_VERSION(0, 0, 1);
129 cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_TUNER |
130 V4L2_CAP_AUDIO | V4L2_CAP_STREAMING |
131 V4L2_CAP_READWRITE | V4L2_CAP_VBI_CAPTURE;
132 return 0;
133}
134
135/*====================================================================*/
136static void init_copy(struct video_data *video, bool index)
137{
138 struct front_face *front = video->front;
139
140 video->field_count = index;
141 video->lines_copied = 0;
142 video->prev_left = 0 ;
143 video->dst = (char *)videobuf_to_vmalloc(front->curr_frame)
144 + index * video->lines_size;
145 video->vbi->copied = 0; /* set it here */
146}
147
148static bool get_frame(struct front_face *front, int *need_init)
149{
150 struct videobuf_buffer *vb = front->curr_frame;
151
152 if (vb)
153 return true;
154
155 spin_lock(&front->queue_lock);
156 if (!list_empty(&front->active)) {
157 vb = list_entry(front->active.next,
158 struct videobuf_buffer, queue);
159 if (need_init)
160 *need_init = 1;
161 front->curr_frame = vb;
162 list_del_init(&vb->queue);
163 }
164 spin_unlock(&front->queue_lock);
165
166 return !!vb;
167}
168
169/* check if the video's buffer is ready */
170static bool get_video_frame(struct front_face *front, struct video_data *video)
171{
172 int need_init = 0;
173 bool ret = true;
174
175 ret = get_frame(front, &need_init);
176 if (ret && need_init)
177 init_copy(video, 0);
178 return ret;
179}
180
181static void submit_frame(struct front_face *front)
182{
183 struct videobuf_buffer *vb = front->curr_frame;
184
185 if (vb == NULL)
186 return;
187
188 front->curr_frame = NULL;
189 vb->state = VIDEOBUF_DONE;
190 vb->field_count++;
191 do_gettimeofday(&vb->ts);
192
193 wake_up(&vb->done);
194}
195
196/*
197 * A frame is composed of two fields. If we receive all the two fields,
198 * call the submit_frame() to submit the whole frame to applications.
199 */
200static void end_field(struct video_data *video)
201{
202 /* logs(video->front); */
203 if (1 == video->field_count)
204 submit_frame(video->front);
205 else
206 init_copy(video, 1);
207}
208
209static void copy_video_data(struct video_data *video, char *src,
210 unsigned int count)
211{
212#define copy_data(len) \
213 do { \
214 if (++video->lines_copied > video->lines_per_field) \
215 goto overflow; \
216 memcpy(video->dst, src, len);\
217 video->dst += len + video->lines_size; \
218 src += len; \
219 count -= len; \
220 } while (0)
221
222 while (count && count >= video->lines_size) {
223 if (video->prev_left) {
224 copy_data(video->prev_left);
225 video->prev_left = 0;
226 continue;
227 }
228 copy_data(video->lines_size);
229 }
230 if (count && count < video->lines_size) {
231 memcpy(video->dst, src, count);
232
233 video->prev_left = video->lines_size - count;
234 video->dst += count;
235 }
236 return;
237
238overflow:
239 end_field(video);
240}
241
242static void check_trailer(struct video_data *video, char *src, int count)
243{
244 struct vbi_data *vbi = video->vbi;
245 int offset; /* trailer's offset */
246 char *buf;
247
248 offset = (video->context.pix.sizeimage / 2 + vbi->vbi_size / 2)
249 - (vbi->copied + video->lines_size * video->lines_copied);
250 if (video->prev_left)
251 offset -= (video->lines_size - video->prev_left);
252
253 if (offset > count || offset <= 0)
254 goto short_package;
255
256 buf = src + offset;
257
258 /* trailer : (VFHS) + U32 + U32 + field_num */
259 if (!strncmp(buf, "VFHS", 4)) {
260 int field_num = *((u32 *)(buf + 12));
261
262 if ((field_num & 1) ^ video->field_count) {
263 init_copy(video, video->field_count);
264 return;
265 }
266 copy_video_data(video, src, offset);
267 }
268short_package:
269 end_field(video);
270}
271
272/* ========== Check this more carefully! =========== */
273static inline void copy_vbi_data(struct vbi_data *vbi,
274 char *src, unsigned int count)
275{
276 struct front_face *front = vbi->front;
277
278 if (front && get_frame(front, NULL)) {
279 char *buf = videobuf_to_vmalloc(front->curr_frame);
280
281 if (vbi->video->field_count)
282 buf += (vbi->vbi_size / 2);
283 memcpy(buf + vbi->copied, src, count);
284 }
285 vbi->copied += count;
286}
287
288/*
289 * Copy the normal data (VBI or VIDEO) without the trailer.
290 * VBI is not interlaced, while VIDEO is interlaced.
291 */
292static inline void copy_vbi_video_data(struct video_data *video,
293 char *src, unsigned int count)
294{
295 struct vbi_data *vbi = video->vbi;
296 unsigned int vbi_delta = (vbi->vbi_size / 2) - vbi->copied;
297
298 if (vbi_delta >= count) {
299 copy_vbi_data(vbi, src, count);
300 } else {
301 if (vbi_delta) {
302 copy_vbi_data(vbi, src, vbi_delta);
303
304 /* we receive the two fields of the VBI*/
305 if (vbi->front && video->field_count)
306 submit_frame(vbi->front);
307 }
308 copy_video_data(video, src + vbi_delta, count - vbi_delta);
309 }
310}
311
312static void urb_complete_bulk(struct urb *urb)
313{
314 struct front_face *front = urb->context;
315 struct video_data *video = &front->pd->video_data;
316 char *src = (char *)urb->transfer_buffer;
317 int count = urb->actual_length;
318 int ret = 0;
319
320 if (!video->is_streaming || urb->status) {
321 if (urb->status == -EPROTO)
322 goto resend_it;
323 return;
324 }
325 if (!get_video_frame(front, video))
326 goto resend_it;
327
328 if (count == urb->transfer_buffer_length)
329 copy_vbi_video_data(video, src, count);
330 else
331 check_trailer(video, src, count);
332
333resend_it:
334 ret = usb_submit_urb(urb, GFP_ATOMIC);
335 if (ret)
336 log(" submit failed: error %d", ret);
337}
338
339/************************* for ISO *********************/
340#define GET_SUCCESS (0)
341#define GET_TRAILER (1)
342#define GET_TOO_MUCH_BUBBLE (2)
343#define GET_NONE (3)
344static int get_chunk(int start, struct urb *urb,
345 int *head, int *tail, int *bubble_err)
346{
347 struct usb_iso_packet_descriptor *pkt = NULL;
348 int ret = GET_SUCCESS;
349
350 for (*head = *tail = -1; start < urb->number_of_packets; start++) {
351 pkt = &urb->iso_frame_desc[start];
352
353 /* handle the bubble of the Hub */
354 if (-EOVERFLOW == pkt->status) {
355 if (++*bubble_err > urb->number_of_packets / 3)
356 return GET_TOO_MUCH_BUBBLE;
357 continue;
358 }
359
360 /* This is the gap */
361 if (pkt->status || pkt->actual_length <= 0
362 || pkt->actual_length > ISO_PKT_SIZE) {
363 if (*head != -1)
364 break;
365 continue;
366 }
367
368 /* a good isochronous packet */
369 if (pkt->actual_length == ISO_PKT_SIZE) {
370 if (*head == -1)
371 *head = start;
372 *tail = start;
373 continue;
374 }
375
376 /* trailer is here */
377 if (pkt->actual_length < ISO_PKT_SIZE) {
378 if (*head == -1) {
379 *head = start;
380 *tail = start;
381 return GET_TRAILER;
382 }
383 break;
384 }
385 }
386
387 if (*head == -1 && *tail == -1)
388 ret = GET_NONE;
389 return ret;
390}
391
392/*
393 * |__|------|___|-----|_______|
394 * ^ ^
395 * | |
396 * gap gap
397 */
398static void urb_complete_iso(struct urb *urb)
399{
400 struct front_face *front = urb->context;
401 struct video_data *video = &front->pd->video_data;
402 int bubble_err = 0, head = 0, tail = 0;
403 char *src = (char *)urb->transfer_buffer;
404 int ret = 0;
405
406 if (!video->is_streaming)
407 return;
408
409 do {
410 if (!get_video_frame(front, video))
411 goto out;
412
413 switch (get_chunk(head, urb, &head, &tail, &bubble_err)) {
414 case GET_SUCCESS:
415 copy_vbi_video_data(video, src + (head * ISO_PKT_SIZE),
416 (tail - head + 1) * ISO_PKT_SIZE);
417 break;
418 case GET_TRAILER:
419 check_trailer(video, src + (head * ISO_PKT_SIZE),
420 ISO_PKT_SIZE);
421 break;
422 case GET_NONE:
423 goto out;
424 case GET_TOO_MUCH_BUBBLE:
425 log("\t We got too much bubble");
426 schedule_work(&video->bubble_work);
427 return;
428 }
429 } while (head = tail + 1, head < urb->number_of_packets);
430
431out:
432 ret = usb_submit_urb(urb, GFP_ATOMIC);
433 if (ret)
434 log("usb_submit_urb err : %d", ret);
435}
436/*============================= [ end ] =====================*/
437
438static int prepare_iso_urb(struct video_data *video)
439{
440 struct usb_device *udev = video->pd->udev;
441 int i;
442
443 if (video->urb_array[0])
444 return 0;
445
446 for (i = 0; i < SBUF_NUM; i++) {
447 struct urb *urb;
448 void *mem;
449 int j;
450
451 urb = usb_alloc_urb(PK_PER_URB, GFP_KERNEL);
452 if (urb == NULL)
453 goto out;
454
455 video->urb_array[i] = urb;
456 mem = usb_buffer_alloc(udev,
457 ISO_PKT_SIZE * PK_PER_URB,
458 GFP_KERNEL,
459 &urb->transfer_dma);
460
461 urb->complete = urb_complete_iso; /* handler */
462 urb->dev = udev;
463 urb->context = video->front;
464 urb->pipe = usb_rcvisocpipe(udev,
465 video->endpoint_addr);
466 urb->interval = 1;
467 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
468 urb->number_of_packets = PK_PER_URB;
469 urb->transfer_buffer = mem;
470 urb->transfer_buffer_length = PK_PER_URB * ISO_PKT_SIZE;
471
472 for (j = 0; j < PK_PER_URB; j++) {
473 urb->iso_frame_desc[j].offset = ISO_PKT_SIZE * j;
474 urb->iso_frame_desc[j].length = ISO_PKT_SIZE;
475 }
476 }
477 return 0;
478out:
479 for (; i > 0; i--)
480 ;
481 return -ENOMEM;
482}
483
484/* return the succeeded number of the allocation */
485int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
486 struct usb_device *udev, u8 ep_addr,
487 int buf_size, gfp_t gfp_flags,
488 usb_complete_t complete_fn, void *context)
489{
490 struct urb *urb;
491 void *mem;
492 int i;
493
494 for (i = 0; i < num; i++) {
495 urb = usb_alloc_urb(0, gfp_flags);
496 if (urb == NULL)
497 return i;
498
499 mem = usb_buffer_alloc(udev, buf_size, gfp_flags,
500 &urb->transfer_dma);
501 if (mem == NULL)
502 return i;
503
504 usb_fill_bulk_urb(urb, udev, usb_rcvbulkpipe(udev, ep_addr),
505 mem, buf_size, complete_fn, context);
506 urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
507 urb_array[i] = urb;
508 }
509 return i;
510}
511
512void free_all_urb_generic(struct urb **urb_array, int num)
513{
514 int i;
515 struct urb *urb;
516
517 for (i = 0; i < num; i++) {
518 urb = urb_array[i];
519 if (urb) {
520 usb_buffer_free(urb->dev,
521 urb->transfer_buffer_length,
522 urb->transfer_buffer,
523 urb->transfer_dma);
524 usb_free_urb(urb);
525 urb_array[i] = NULL;
526 }
527 }
528}
529
530static int prepare_bulk_urb(struct video_data *video)
531{
532 if (video->urb_array[0])
533 return 0;
534
535 alloc_bulk_urbs_generic(video->urb_array, SBUF_NUM,
536 video->pd->udev, video->endpoint_addr,
537 0x2000, GFP_KERNEL,
538 urb_complete_bulk, video->front);
539 return 0;
540}
541
542/* free the URBs */
543static void free_all_urb(struct video_data *video)
544{
545 free_all_urb_generic(video->urb_array, SBUF_NUM);
546}
547
548static void pd_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
549{
550 videobuf_vmalloc_free(vb);
551 vb->state = VIDEOBUF_NEEDS_INIT;
552}
553
554static void pd_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
555{
556 struct front_face *front = q->priv_data;
557 vb->state = VIDEOBUF_QUEUED;
558 list_add_tail(&vb->queue, &front->active);
559}
560
561static int pd_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
562 enum v4l2_field field)
563{
564 struct front_face *front = q->priv_data;
565 int rc;
566
567 switch (front->type) {
568 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
569 if (VIDEOBUF_NEEDS_INIT == vb->state) {
570 struct v4l2_pix_format *pix;
571
572 pix = &front->pd->video_data.context.pix;
573 vb->size = pix->sizeimage; /* real frame size */
574 vb->width = pix->width;
575 vb->height = pix->height;
576 rc = videobuf_iolock(q, vb, NULL);
577 if (rc < 0)
578 return rc;
579 }
580 break;
581 case V4L2_BUF_TYPE_VBI_CAPTURE:
582 if (VIDEOBUF_NEEDS_INIT == vb->state) {
583 vb->size = front->pd->vbi_data.vbi_size;
584 rc = videobuf_iolock(q, vb, NULL);
585 if (rc < 0)
586 return rc;
587 }
588 break;
589 default:
590 return -EINVAL;
591 }
592 vb->field = field;
593 vb->state = VIDEOBUF_PREPARED;
594 return 0;
595}
596
597int fire_all_urb(struct video_data *video)
598{
599 int i, ret;
600
601 video->is_streaming = 1;
602
603 for (i = 0; i < SBUF_NUM; i++) {
604 ret = usb_submit_urb(video->urb_array[i], GFP_KERNEL);
605 if (ret)
606 log("(%d) failed: error %d", i, ret);
607 }
608 return ret;
609}
610
611static int start_video_stream(struct poseidon *pd)
612{
613 struct video_data *video = &pd->video_data;
614 s32 cmd_status;
615
616 send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
617 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_START, &cmd_status);
618
619 if (pd->cur_transfer_mode) {
620 prepare_iso_urb(video);
621 INIT_WORK(&video->bubble_work, iso_bubble_handler);
622 } else {
623 /* The bulk mode does not need a bubble handler */
624 prepare_bulk_urb(video);
625 }
626 fire_all_urb(video);
627 return 0;
628}
629
630static int pd_buf_setup(struct videobuf_queue *q, unsigned int *count,
631 unsigned int *size)
632{
633 struct front_face *front = q->priv_data;
634 struct poseidon *pd = front->pd;
635
636 switch (front->type) {
637 default:
638 return -EINVAL;
639 case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
640 struct video_data *video = &pd->video_data;
641 struct v4l2_pix_format *pix = &video->context.pix;
642
643 *size = PAGE_ALIGN(pix->sizeimage);/* page aligned frame size */
644 if (*count < 4)
645 *count = 4;
646 if (1) {
647 /* same in different altersetting */
648 video->endpoint_addr = 0x82;
649 video->vbi = &pd->vbi_data;
650 video->vbi->video = video;
651 video->pd = pd;
652 video->lines_per_field = pix->height / 2;
653 video->lines_size = pix->width * 2;
654 video->front = front;
655 }
656 return start_video_stream(pd);
657 }
658
659 case V4L2_BUF_TYPE_VBI_CAPTURE: {
660 struct vbi_data *vbi = &pd->vbi_data;
661
662 *size = PAGE_ALIGN(vbi->vbi_size);
663 log("size : %d", *size);
664 if (*count == 0)
665 *count = 4;
666 }
667 break;
668 }
669 return 0;
670}
671
672static struct videobuf_queue_ops pd_video_qops = {
673 .buf_setup = pd_buf_setup,
674 .buf_prepare = pd_buf_prepare,
675 .buf_queue = pd_buf_queue,
676 .buf_release = pd_buf_release,
677};
678
679static int vidioc_enum_fmt(struct file *file, void *fh,
680 struct v4l2_fmtdesc *f)
681{
682 if (ARRAY_SIZE(poseidon_formats) <= f->index)
683 return -EINVAL;
684 f->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
685 f->flags = 0;
686 f->pixelformat = poseidon_formats[f->index].fourcc;
687 strcpy(f->description, poseidon_formats[f->index].name);
688 return 0;
689}
690
691static int vidioc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
692{
693 struct front_face *front = fh;
694 struct poseidon *pd = front->pd;
695
696 logs(front);
697 f->fmt.pix = pd->video_data.context.pix;
698 return 0;
699}
700
701static int vidioc_try_fmt(struct file *file, void *fh,
702 struct v4l2_format *f)
703{
704 return 0;
705}
706
707/*
708 * VLC calls VIDIOC_S_STD before VIDIOC_S_FMT, while
709 * Mplayer calls them in the reverse order.
710 */
711static int pd_vidioc_s_fmt(struct poseidon *pd, struct v4l2_pix_format *pix)
712{
713 struct video_data *video = &pd->video_data;
714 struct running_context *context = &video->context;
715 struct v4l2_pix_format *pix_def = &context->pix;
716 s32 ret = 0, cmd_status = 0, vid_resol;
717
718 /* set the pixel format to firmware */
719 if (pix->pixelformat == V4L2_PIX_FMT_RGB565) {
720 vid_resol = TLG_TUNER_VID_FORMAT_RGB_565;
721 } else {
722 pix->pixelformat = V4L2_PIX_FMT_YUYV;
723 vid_resol = TLG_TUNER_VID_FORMAT_YUV;
724 }
725 ret = send_set_req(pd, VIDEO_STREAM_FMT_SEL,
726 vid_resol, &cmd_status);
727
728 /* set the resolution to firmware */
729 vid_resol = TLG_TUNE_VID_RES_720;
730 switch (pix->width) {
731 case 704:
732 vid_resol = TLG_TUNE_VID_RES_704;
733 break;
734 default:
735 pix->width = 720;
736 case 720:
737 break;
738 }
739 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
740 vid_resol, &cmd_status);
741 if (ret || cmd_status) {
742 mutex_unlock(&pd->lock);
743 return -EBUSY;
744 }
745
746 pix_def->pixelformat = pix->pixelformat; /* save it */
747 pix->height = (context->tvnormid & V4L2_STD_525_60) ? 480 : 576;
748
749 /* Compare with the default setting */
750 if ((pix_def->width != pix->width)
751 || (pix_def->height != pix->height)) {
752 pix_def->width = pix->width;
753 pix_def->height = pix->height;
754 pix_def->bytesperline = pix->width * 2;
755 pix_def->sizeimage = pix->width * pix->height * 2;
756 }
757 *pix = *pix_def;
758
759 return 0;
760}
761
762static int vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
763{
764 struct front_face *front = fh;
765 struct poseidon *pd = front->pd;
766
767 logs(front);
768 /* stop VBI here */
769 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
770 return -EINVAL;
771
772 mutex_lock(&pd->lock);
773 if (pd->file_for_stream == NULL)
774 pd->file_for_stream = file;
775 else if (file != pd->file_for_stream) {
776 mutex_unlock(&pd->lock);
777 return -EINVAL;
778 }
779
780 pd_vidioc_s_fmt(pd, &f->fmt.pix);
781 mutex_unlock(&pd->lock);
782 return 0;
783}
784
785static int vidioc_g_fmt_vbi(struct file *file, void *fh,
786 struct v4l2_format *v4l2_f)
787{
788 struct front_face *front = fh;
789 struct poseidon *pd = front->pd;
790 struct v4l2_vbi_format *vbi_fmt = &v4l2_f->fmt.vbi;
791
792 vbi_fmt->samples_per_line = 720 * 2;
793 vbi_fmt->sampling_rate = 6750000 * 4;
794 vbi_fmt->sample_format = V4L2_PIX_FMT_GREY;
795 vbi_fmt->offset = 64 * 4; /*FIXME: why offset */
796 if (pd->video_data.context.tvnormid & V4L2_STD_525_60) {
797 vbi_fmt->start[0] = 10;
798 vbi_fmt->start[1] = 264;
799 vbi_fmt->count[0] = V4L_NTSC_VBI_LINES;
800 vbi_fmt->count[1] = V4L_NTSC_VBI_LINES;
801 } else {
802 vbi_fmt->start[0] = 6;
803 vbi_fmt->start[1] = 314;
804 vbi_fmt->count[0] = V4L_PAL_VBI_LINES;
805 vbi_fmt->count[1] = V4L_PAL_VBI_LINES;
806 }
807 vbi_fmt->flags = V4L2_VBI_UNSYNC;
808 logs(front);
809 return 0;
810}
811
812static int set_std(struct poseidon *pd, v4l2_std_id *norm)
813{
814 struct video_data *video = &pd->video_data;
815 struct vbi_data *vbi = &pd->vbi_data;
816 struct running_context *context;
817 struct v4l2_pix_format *pix;
818 s32 i, ret = 0, cmd_status, param;
819 int height;
820
821 for (i = 0; i < POSEIDON_TVNORMS; i++) {
822 if (*norm & poseidon_tvnorms[i].v4l2_id) {
823 param = poseidon_tvnorms[i].tlg_tvnorm;
824 log("name : %s", poseidon_tvnorms[i].name);
825 goto found;
826 }
827 }
828 return -EINVAL;
829found:
830 mutex_lock(&pd->lock);
831 ret = send_set_req(pd, VIDEO_STD_SEL, param, &cmd_status);
832 if (ret || cmd_status)
833 goto out;
834
835 /* Set vbi size and check the height of the frame */
836 context = &video->context;
837 context->tvnormid = poseidon_tvnorms[i].v4l2_id;
838 if (context->tvnormid & V4L2_STD_525_60) {
839 vbi->vbi_size = V4L_NTSC_VBI_FRAMESIZE;
840 height = 480;
841 } else {
842 vbi->vbi_size = V4L_PAL_VBI_FRAMESIZE;
843 height = 576;
844 }
845
846 pix = &context->pix;
847 if (pix->height != height) {
848 pix->height = height;
849 pix->sizeimage = pix->width * pix->height * 2;
850 }
851
852out:
853 mutex_unlock(&pd->lock);
854 return ret;
855}
856
857int vidioc_s_std(struct file *file, void *fh, v4l2_std_id *norm)
858{
859 struct front_face *front = fh;
860 logs(front);
861 return set_std(front->pd, norm);
862}
863
864static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *in)
865{
866 struct front_face *front = fh;
867
868 if (in->index < 0 || in->index >= POSEIDON_INPUTS)
869 return -EINVAL;
870 strcpy(in->name, pd_inputs[in->index].name);
871 in->type = V4L2_INPUT_TYPE_TUNER;
872
873 /*
874 * the audio input index mixed with this video input,
875 * Poseidon only have one audio/video, set to "0"
876 */
877 in->audioset = 0;
878 in->tuner = 0;
879 in->std = V4L2_STD_ALL;
880 in->status = 0;
881 logs(front);
882 return 0;
883}
884
885static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
886{
887 struct front_face *front = fh;
888 struct poseidon *pd = front->pd;
889 struct running_context *context = &pd->video_data.context;
890
891 logs(front);
892 *i = context->sig_index;
893 return 0;
894}
895
896/* We can support several inputs */
897static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
898{
899 struct front_face *front = fh;
900 struct poseidon *pd = front->pd;
901 s32 ret, cmd_status;
902
903 if (i < 0 || i >= POSEIDON_INPUTS)
904 return -EINVAL;
905 ret = send_set_req(pd, SGNL_SRC_SEL,
906 pd_inputs[i].tlg_src, &cmd_status);
907 if (ret)
908 return ret;
909
910 pd->video_data.context.sig_index = i;
911 return 0;
912}
913
914static struct poseidon_control *check_control_id(__u32 id)
915{
916 struct poseidon_control *control = &controls[0];
917 int array_size = ARRAY_SIZE(controls);
918
919 for (; control < &controls[array_size]; control++)
920 if (control->v4l2_ctrl.id == id)
921 return control;
922 return NULL;
923}
924
925static int vidioc_queryctrl(struct file *file, void *fh,
926 struct v4l2_queryctrl *a)
927{
928 struct poseidon_control *control = NULL;
929
930 control = check_control_id(a->id);
931 if (!control)
932 return -EINVAL;
933
934 *a = control->v4l2_ctrl;
935 return 0;
936}
937
938static int vidioc_g_ctrl(struct file *file, void *fh, struct v4l2_control *ctrl)
939{
940 struct front_face *front = fh;
941 struct poseidon *pd = front->pd;
942 struct poseidon_control *control = NULL;
943 struct tuner_custom_parameter_s tuner_param;
944 s32 ret = 0, cmd_status;
945
946 control = check_control_id(ctrl->id);
947 if (!control)
948 return -EINVAL;
949
950 mutex_lock(&pd->lock);
951 ret = send_get_req(pd, TUNER_CUSTOM_PARAMETER, control->vc_id,
952 &tuner_param, &cmd_status, sizeof(tuner_param));
953 mutex_unlock(&pd->lock);
954
955 if (ret || cmd_status)
956 return -1;
957
958 ctrl->value = tuner_param.param_value;
959 return 0;
960}
961
962static int vidioc_s_ctrl(struct file *file, void *fh, struct v4l2_control *a)
963{
964 struct tuner_custom_parameter_s param = {0};
965 struct poseidon_control *control = NULL;
966 struct front_face *front = fh;
967 struct poseidon *pd = front->pd;
968 s32 ret = 0, cmd_status, params;
969
970 control = check_control_id(a->id);
971 if (!control)
972 return -EINVAL;
973
974 param.param_value = a->value;
975 param.param_id = control->vc_id;
976 params = *(s32 *)&param; /* temp code */
977
978 mutex_lock(&pd->lock);
979 ret = send_set_req(pd, TUNER_CUSTOM_PARAMETER, params, &cmd_status);
980 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
981 mutex_unlock(&pd->lock);
982
983 set_current_state(TASK_INTERRUPTIBLE);
984 schedule_timeout(HZ/4);
985 return ret;
986}
987
988/* Audio ioctls */
989static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
990{
991 if (0 != a->index)
992 return -EINVAL;
993 a->capability = V4L2_AUDCAP_STEREO;
994 strcpy(a->name, "USB audio in");
995 /*Poseidon have no AVL function.*/
996 a->mode = 0;
997 return 0;
998}
999
1000int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
1001{
1002 a->index = 0;
1003 a->capability = V4L2_AUDCAP_STEREO;
1004 strcpy(a->name, "USB audio in");
1005 a->mode = 0;
1006 return 0;
1007}
1008
1009int vidioc_s_audio(struct file *file, void *fh, struct v4l2_audio *a)
1010{
1011 return (0 == a->index) ? 0 : -EINVAL;
1012}
1013
1014/* Tuner ioctls */
1015static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *tuner)
1016{
1017 struct front_face *front = fh;
1018 struct poseidon *pd = front->pd;
1019 struct tuner_atv_sig_stat_s atv_stat;
1020 s32 count = 5, ret, cmd_status;
1021 int index;
1022
1023 if (0 != tuner->index)
1024 return -EINVAL;
1025
1026 mutex_lock(&pd->lock);
1027 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
1028 &atv_stat, &cmd_status, sizeof(atv_stat));
1029
1030 while (atv_stat.sig_lock_busy && count-- && !ret) {
1031 set_current_state(TASK_INTERRUPTIBLE);
1032 schedule_timeout(HZ);
1033
1034 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
1035 &atv_stat, &cmd_status, sizeof(atv_stat));
1036 }
1037 mutex_unlock(&pd->lock);
1038
1039 if (debug_mode)
1040 log("P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
1041
1042 if (ret || cmd_status)
1043 tuner->signal = 0;
1044 else if (atv_stat.sig_present && !atv_stat.sig_strength)
1045 tuner->signal = 0xFFFF;
1046 else
1047 tuner->signal = (atv_stat.sig_strength * 255 / 10) << 8;
1048
1049 strcpy(tuner->name, "Telegent Systems");
1050 tuner->type = V4L2_TUNER_ANALOG_TV;
1051 tuner->rangelow = TUNER_FREQ_MIN / 62500;
1052 tuner->rangehigh = TUNER_FREQ_MAX / 62500;
1053 tuner->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1054 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1055 index = pd->video_data.context.audio_idx;
1056 tuner->rxsubchans = pd_audio_modes[index].v4l2_audio_sub;
1057 tuner->audmode = pd_audio_modes[index].v4l2_audio_mode;
1058 tuner->afc = 0;
1059 logs(front);
1060 return 0;
1061}
1062
1063static int pd_vidioc_s_tuner(struct poseidon *pd, int index)
1064{
1065 s32 ret = 0, cmd_status, param, audiomode;
1066
1067 mutex_lock(&pd->lock);
1068 param = pd_audio_modes[index].tlg_audio_mode;
1069 ret = send_set_req(pd, TUNER_AUD_MODE, param, &cmd_status);
1070 audiomode = get_audio_std(TLG_MODE_ANALOG_TV, pd->country_code);
1071 ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode,
1072 &cmd_status);
1073 if (!ret)
1074 pd->video_data.context.audio_idx = index;
1075 mutex_unlock(&pd->lock);
1076 return ret;
1077}
1078
1079static int vidioc_s_tuner(struct file *file, void *fh, struct v4l2_tuner *a)
1080{
1081 struct front_face *front = fh;
1082 struct poseidon *pd = front->pd;
1083 int index;
1084
1085 if (0 != a->index)
1086 return -EINVAL;
1087 logs(front);
1088 for (index = 0; index < POSEIDON_AUDIOMODS; index++)
1089 if (a->audmode == pd_audio_modes[index].v4l2_audio_mode)
1090 return pd_vidioc_s_tuner(pd, index);
1091 return -EINVAL;
1092}
1093
1094static int vidioc_g_frequency(struct file *file, void *fh,
1095 struct v4l2_frequency *freq)
1096{
1097 struct front_face *front = fh;
1098 struct poseidon *pd = front->pd;
1099 struct running_context *context = &pd->video_data.context;
1100
1101 if (0 != freq->tuner)
1102 return -EINVAL;
1103 freq->frequency = context->freq;
1104 freq->type = V4L2_TUNER_ANALOG_TV;
1105 return 0;
1106}
1107
1108static int set_frequency(struct poseidon *pd, __u32 frequency)
1109{
1110 s32 ret = 0, param, cmd_status;
1111 struct running_context *context = &pd->video_data.context;
1112
1113 param = frequency * 62500 / 1000;
1114 if (param < TUNER_FREQ_MIN/1000 || param > TUNER_FREQ_MAX / 1000)
1115 return -EINVAL;
1116
1117 mutex_lock(&pd->lock);
1118 ret = send_set_req(pd, TUNE_FREQ_SELECT, param, &cmd_status);
1119 ret = send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
1120
1121 msleep(250); /* wait for a while until the hardware is ready. */
1122 context->freq = frequency;
1123 mutex_unlock(&pd->lock);
1124 return ret;
1125}
1126
1127static int vidioc_s_frequency(struct file *file, void *fh,
1128 struct v4l2_frequency *freq)
1129{
1130 struct front_face *front = fh;
1131 struct poseidon *pd = front->pd;
1132
1133 logs(front);
1134#ifdef CONFIG_PM
1135 pd->pm_suspend = pm_video_suspend;
1136 pd->pm_resume = pm_video_resume;
1137#endif
1138 return set_frequency(pd, freq->frequency);
1139}
1140
1141static int vidioc_reqbufs(struct file *file, void *fh,
1142 struct v4l2_requestbuffers *b)
1143{
1144 struct front_face *front = file->private_data;
1145 logs(front);
1146 return videobuf_reqbufs(&front->q, b);
1147}
1148
1149static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
1150{
1151 struct front_face *front = file->private_data;
1152 logs(front);
1153 return videobuf_querybuf(&front->q, b);
1154}
1155
1156static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1157{
1158 struct front_face *front = file->private_data;
1159 return videobuf_qbuf(&front->q, b);
1160}
1161
1162static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1163{
1164 struct front_face *front = file->private_data;
1165 return videobuf_dqbuf(&front->q, b, file->f_flags & O_NONBLOCK);
1166}
1167
1168/* Just stop the URBs, do not free the URBs */
1169int usb_transfer_stop(struct video_data *video)
1170{
1171 if (video->is_streaming) {
1172 int i;
1173 s32 cmd_status;
1174 struct poseidon *pd = video->pd;
1175
1176 video->is_streaming = 0;
1177 for (i = 0; i < SBUF_NUM; ++i) {
1178 if (video->urb_array[i])
1179 usb_kill_urb(video->urb_array[i]);
1180 }
1181
1182 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1183 &cmd_status);
1184 }
1185 return 0;
1186}
1187
1188int stop_all_video_stream(struct poseidon *pd)
1189{
1190 struct video_data *video = &pd->video_data;
1191 struct vbi_data *vbi = &pd->vbi_data;
1192
1193 mutex_lock(&pd->lock);
1194 if (video->is_streaming) {
1195 struct front_face *front = video->front;
1196
1197 /* stop the URBs */
1198 usb_transfer_stop(video);
1199 free_all_urb(video);
1200
1201 /* stop the host side of VIDEO */
1202 videobuf_stop(&front->q);
1203 videobuf_mmap_free(&front->q);
1204
1205 /* stop the host side of VBI */
1206 front = vbi->front;
1207 if (front) {
1208 videobuf_stop(&front->q);
1209 videobuf_mmap_free(&front->q);
1210 }
1211 }
1212 mutex_unlock(&pd->lock);
1213 return 0;
1214}
1215
1216/*
1217 * The bubbles can seriously damage the video's quality,
1218 * though it occurs in very rare situation.
1219 */
1220static void iso_bubble_handler(struct work_struct *w)
1221{
1222 struct video_data *video;
1223 struct poseidon *pd;
1224
1225 video = container_of(w, struct video_data, bubble_work);
1226 pd = video->pd;
1227
1228 mutex_lock(&pd->lock);
1229 usb_transfer_stop(video);
1230 msleep(500);
1231 start_video_stream(pd);
1232 mutex_unlock(&pd->lock);
1233}
1234
1235
1236static int vidioc_streamon(struct file *file, void *fh,
1237 enum v4l2_buf_type type)
1238{
1239 struct front_face *front = fh;
1240
1241 logs(front);
1242 if (unlikely(type != front->type))
1243 return -EINVAL;
1244 return videobuf_streamon(&front->q);
1245}
1246
1247static int vidioc_streamoff(struct file *file, void *fh,
1248 enum v4l2_buf_type type)
1249{
1250 struct front_face *front = file->private_data;
1251
1252 logs(front);
1253 if (unlikely(type != front->type))
1254 return -EINVAL;
1255 return videobuf_streamoff(&front->q);
1256}
1257
1258/*
1259 * Set the firmware' default values : need altersetting and country code
1260 */
1261static int pd_video_checkmode(struct poseidon *pd)
1262{
1263 s32 ret = 0, cmd_status, audiomode;
1264
1265 set_current_state(TASK_INTERRUPTIBLE);
1266 schedule_timeout(HZ/2);
1267
1268 /* choose the altersetting */
1269 ret = usb_set_interface(pd->udev, 0,
1270 (pd->cur_transfer_mode ?
1271 ISO_3K_BULK_ALTERNATE_IFACE :
1272 BULK_ALTERNATE_IFACE));
1273 if (ret < 0)
1274 goto error;
1275
1276 /* set default parameters for PAL-D , with the VBI enabled*/
1277 ret = set_tuner_mode(pd, TLG_MODE_ANALOG_TV);
1278 ret |= send_set_req(pd, SGNL_SRC_SEL,
1279 TLG_SIG_SRC_ANTENNA, &cmd_status);
1280 ret |= send_set_req(pd, VIDEO_STD_SEL,
1281 TLG_TUNE_VSTD_PAL_D, &cmd_status);
1282 ret |= send_set_req(pd, VIDEO_STREAM_FMT_SEL,
1283 TLG_TUNER_VID_FORMAT_YUV, &cmd_status);
1284 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
1285 TLG_TUNE_VID_RES_720, &cmd_status);
1286 ret |= send_set_req(pd, TUNE_FREQ_SELECT, TUNER_FREQ_MIN, &cmd_status);
1287 ret |= send_set_req(pd, VBI_DATA_SEL, 1, &cmd_status);/* enable vbi */
1288
1289 /* need country code to set the audio */
1290 audiomode = get_audio_std(TLG_MODE_ANALOG_TV, pd->country_code);
1291 ret |= send_set_req(pd, TUNER_AUD_ANA_STD, audiomode, &cmd_status);
1292 ret |= send_set_req(pd, TUNER_AUD_MODE,
1293 TLG_TUNE_TVAUDIO_MODE_STEREO, &cmd_status);
1294 ret |= send_set_req(pd, AUDIO_SAMPLE_RATE_SEL,
1295 ATV_AUDIO_RATE_48K, &cmd_status);
1296error:
1297 return ret;
1298}
1299
1300#ifdef CONFIG_PM
1301static int pm_video_suspend(struct poseidon *pd)
1302{
1303 /* stop audio */
1304 pm_alsa_suspend(pd);
1305
1306 /* stop and free all the URBs */
1307 usb_transfer_stop(&pd->video_data);
1308 free_all_urb(&pd->video_data);
1309
1310 /* reset the interface */
1311 usb_set_interface(pd->udev, 0, 0);
1312 msleep(300);
1313 return 0;
1314}
1315
1316static int restore_v4l2_context(struct poseidon *pd,
1317 struct running_context *context)
1318{
1319 struct front_face *front = pd->video_data.front;
1320
1321 pd_video_checkmode(pd);
1322
1323 set_std(pd, &context->tvnormid);
1324 vidioc_s_input(NULL, front, context->sig_index);
1325 pd_vidioc_s_tuner(pd, context->audio_idx);
1326 pd_vidioc_s_fmt(pd, &context->pix);
1327 set_frequency(pd, context->freq);
1328 return 0;
1329}
1330
1331static int pm_video_resume(struct poseidon *pd)
1332{
1333 struct video_data *video = &pd->video_data;
1334
1335 /* resume the video */
1336 /* [1] restore the origin V4L2 parameters */
1337 restore_v4l2_context(pd, &video->context);
1338
1339 /* [2] initiate video copy variables */
1340 if (video->front->curr_frame)
1341 init_copy(video, 0);
1342
1343 /* [3] fire urbs */
1344 start_video_stream(pd);
1345
1346 /* resume the audio */
1347 pm_alsa_resume(pd);
1348 return 0;
1349}
1350#endif
1351
1352void set_debug_mode(struct video_device *vfd, int debug_mode)
1353{
1354 vfd->debug = 0;
1355 if (debug_mode & 0x1)
1356 vfd->debug = V4L2_DEBUG_IOCTL;
1357 if (debug_mode & 0x2)
1358 vfd->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
1359}
1360
1361static void init_video_context(struct running_context *context)
1362{
1363 context->sig_index = 0;
1364 context->audio_idx = 1; /* stereo */
1365 context->tvnormid = V4L2_STD_PAL_D;
1366 context->pix = (struct v4l2_pix_format) {
1367 .width = 720,
1368 .height = 576,
1369 .pixelformat = V4L2_PIX_FMT_YUYV,
1370 .field = V4L2_FIELD_INTERLACED,
1371 .bytesperline = 720 * 2,
1372 .sizeimage = 720 * 576 * 2,
1373 .colorspace = V4L2_COLORSPACE_SMPTE170M,
1374 .priv = 0
1375 };
1376}
1377
1378static int pd_video_open(struct file *file)
1379{
1380 struct video_device *vfd = video_devdata(file);
1381 struct poseidon *pd = video_get_drvdata(vfd);
1382 struct front_face *front = NULL;
1383 int ret = -ENOMEM;
1384
1385 mutex_lock(&pd->lock);
1386 usb_autopm_get_interface(pd->interface);
1387
1388 if (vfd->vfl_type == VFL_TYPE_GRABBER
1389 && !(pd->state & POSEIDON_STATE_ANALOG)) {
1390 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1391 if (!front)
1392 goto out;
1393
1394 pd->cur_transfer_mode = usb_transfer_mode;/* bulk or iso */
1395 pd->country_code = country_code;
1396 init_video_context(&pd->video_data.context);
1397
1398 ret = pd_video_checkmode(pd);
1399 if (ret < 0) {
1400 kfree(front);
1401 ret = -1;
1402 goto out;
1403 }
1404
1405 pd->state |= POSEIDON_STATE_ANALOG;
1406 front->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1407 pd->video_data.users++;
1408 set_debug_mode(vfd, debug_mode);
1409
1410 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1411 NULL, &front->queue_lock,
1412 V4L2_BUF_TYPE_VIDEO_CAPTURE,
1413 V4L2_FIELD_INTERLACED,/* video is interlacd */
1414 sizeof(struct videobuf_buffer),/*it's enough*/
1415 front);
1416 } else if (vfd->vfl_type == VFL_TYPE_VBI
1417 && !(pd->state & POSEIDON_STATE_VBI)) {
1418 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1419 if (!front)
1420 goto out;
1421
1422 pd->state |= POSEIDON_STATE_VBI;
1423 front->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1424 pd->vbi_data.front = front;
1425 pd->vbi_data.users++;
1426
1427 videobuf_queue_vmalloc_init(&front->q, &pd_video_qops,
1428 NULL, &front->queue_lock,
1429 V4L2_BUF_TYPE_VBI_CAPTURE,
1430 V4L2_FIELD_NONE, /* vbi is NONE mode */
1431 sizeof(struct videobuf_buffer),
1432 front);
1433 } else {
1434 /* maybe add FM support here */
1435 log("other ");
1436 ret = -EINVAL;
1437 goto out;
1438 }
1439
1440 front->pd = pd;
1441 front->curr_frame = NULL;
1442 INIT_LIST_HEAD(&front->active);
1443 spin_lock_init(&front->queue_lock);
1444
1445 file->private_data = front;
1446 kref_get(&pd->kref);
1447
1448 mutex_unlock(&pd->lock);
1449 return 0;
1450out:
1451 usb_autopm_put_interface(pd->interface);
1452 mutex_unlock(&pd->lock);
1453 return ret;
1454}
1455
1456static int pd_video_release(struct file *file)
1457{
1458 struct front_face *front = file->private_data;
1459 struct poseidon *pd = front->pd;
1460 s32 cmd_status = 0;
1461
1462 logs(front);
1463 mutex_lock(&pd->lock);
1464
1465 if (front->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1466 pd->state &= ~POSEIDON_STATE_ANALOG;
1467
1468 /* stop the device, and free the URBs */
1469 usb_transfer_stop(&pd->video_data);
1470 free_all_urb(&pd->video_data);
1471
1472 /* stop the firmware */
1473 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1474 &cmd_status);
1475
1476 pd->file_for_stream = NULL;
1477 pd->video_data.users--;
1478 } else if (front->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
1479 pd->state &= ~POSEIDON_STATE_VBI;
1480 pd->vbi_data.front = NULL;
1481 pd->vbi_data.users--;
1482 }
1483 videobuf_stop(&front->q);
1484 videobuf_mmap_free(&front->q);
1485
1486 usb_autopm_put_interface(pd->interface);
1487 mutex_unlock(&pd->lock);
1488
1489 kfree(front);
1490 file->private_data = NULL;
1491 kref_put(&pd->kref, poseidon_delete);
1492 return 0;
1493}
1494
1495static int pd_video_mmap(struct file *file, struct vm_area_struct *vma)
1496{
1497 struct front_face *front = file->private_data;
1498 return videobuf_mmap_mapper(&front->q, vma);
1499}
1500
1501unsigned int pd_video_poll(struct file *file, poll_table *table)
1502{
1503 struct front_face *front = file->private_data;
1504 return videobuf_poll_stream(file, &front->q, table);
1505}
1506
1507ssize_t pd_video_read(struct file *file, char __user *buffer,
1508 size_t count, loff_t *ppos)
1509{
1510 struct front_face *front = file->private_data;
1511 return videobuf_read_stream(&front->q, buffer, count, ppos,
1512 0, file->f_flags & O_NONBLOCK);
1513}
1514
1515/* This struct works for both VIDEO and VBI */
1516static const struct v4l2_file_operations pd_video_fops = {
1517 .owner = THIS_MODULE,
1518 .open = pd_video_open,
1519 .release = pd_video_release,
1520 .read = pd_video_read,
1521 .poll = pd_video_poll,
1522 .mmap = pd_video_mmap,
1523 .ioctl = video_ioctl2, /* maybe changed in future */
1524};
1525
1526static const struct v4l2_ioctl_ops pd_video_ioctl_ops = {
1527 .vidioc_querycap = vidioc_querycap,
1528
1529 /* Video format */
1530 .vidioc_g_fmt_vid_cap = vidioc_g_fmt,
1531 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt,
1532 .vidioc_s_fmt_vid_cap = vidioc_s_fmt,
1533 .vidioc_g_fmt_vbi_cap = vidioc_g_fmt_vbi, /* VBI */
1534 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
1535
1536 /* Input */
1537 .vidioc_g_input = vidioc_g_input,
1538 .vidioc_s_input = vidioc_s_input,
1539 .vidioc_enum_input = vidioc_enum_input,
1540
1541 /* Audio ioctls */
1542 .vidioc_enumaudio = vidioc_enumaudio,
1543 .vidioc_g_audio = vidioc_g_audio,
1544 .vidioc_s_audio = vidioc_s_audio,
1545
1546 /* Tuner ioctls */
1547 .vidioc_g_tuner = vidioc_g_tuner,
1548 .vidioc_s_tuner = vidioc_s_tuner,
1549 .vidioc_s_std = vidioc_s_std,
1550 .vidioc_g_frequency = vidioc_g_frequency,
1551 .vidioc_s_frequency = vidioc_s_frequency,
1552
1553 /* Buffer handlers */
1554 .vidioc_reqbufs = vidioc_reqbufs,
1555 .vidioc_querybuf = vidioc_querybuf,
1556 .vidioc_qbuf = vidioc_qbuf,
1557 .vidioc_dqbuf = vidioc_dqbuf,
1558
1559 /* Stream on/off */
1560 .vidioc_streamon = vidioc_streamon,
1561 .vidioc_streamoff = vidioc_streamoff,
1562
1563 /* Control handling */
1564 .vidioc_queryctrl = vidioc_queryctrl,
1565 .vidioc_g_ctrl = vidioc_g_ctrl,
1566 .vidioc_s_ctrl = vidioc_s_ctrl,
1567};
1568
1569static struct video_device pd_video_template = {
1570 .name = "Telegent-Video",
1571 .fops = &pd_video_fops,
1572 .minor = -1,
1573 .release = video_device_release,
1574 .tvnorms = V4L2_STD_ALL,
1575 .ioctl_ops = &pd_video_ioctl_ops,
1576};
1577
1578struct video_device *vdev_init(struct poseidon *pd, struct video_device *tmp)
1579{
1580 struct video_device *vfd;
1581
1582 vfd = video_device_alloc();
1583 if (vfd == NULL)
1584 return NULL;
1585 *vfd = *tmp;
1586 vfd->minor = -1;
1587 vfd->v4l2_dev = &pd->v4l2_dev;
1588 /*vfd->parent = &(pd->udev->dev); */
1589 vfd->release = video_device_release;
1590 video_set_drvdata(vfd, pd);
1591 return vfd;
1592}
1593
1594void destroy_video_device(struct video_device **v_dev)
1595{
1596 struct video_device *dev = *v_dev;
1597
1598 if (dev == NULL)
1599 return;
1600
1601 if (video_is_registered(dev))
1602 video_unregister_device(dev);
1603 else
1604 video_device_release(dev);
1605 *v_dev = NULL;
1606}
1607
1608void pd_video_exit(struct poseidon *pd)
1609{
1610 struct video_data *video = &pd->video_data;
1611 struct vbi_data *vbi = &pd->vbi_data;
1612
1613 destroy_video_device(&video->v_dev);
1614 destroy_video_device(&vbi->v_dev);
1615 log();
1616}
1617
1618int pd_video_init(struct poseidon *pd)
1619{
1620 struct video_data *video = &pd->video_data;
1621 struct vbi_data *vbi = &pd->vbi_data;
1622 int ret = -ENOMEM;
1623
1624 video->v_dev = vdev_init(pd, &pd_video_template);
1625 if (video->v_dev == NULL)
1626 goto out;
1627
1628 ret = video_register_device(video->v_dev, VFL_TYPE_GRABBER, -1);
1629 if (ret != 0)
1630 goto out;
1631
1632 /* VBI uses the same template as video */
1633 vbi->v_dev = vdev_init(pd, &pd_video_template);
1634 if (vbi->v_dev == NULL) {
1635 ret = -ENOMEM;
1636 goto out;
1637 }
1638 ret = video_register_device(vbi->v_dev, VFL_TYPE_VBI, -1);
1639 if (ret != 0)
1640 goto out;
1641 log("register VIDEO/VBI devices");
1642 return 0;
1643out:
1644 log("VIDEO/VBI devices register failed, : %d", ret);
1645 pd_video_exit(pd);
1646 return ret;
1647}
1648