[media] vino/saa7191: move to staging in preparation for removal
[deliverable/linux.git] / drivers / media / usb / tlg2300 / pd-video.c
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>
8
9 #include <media/v4l2-ioctl.h>
10 #include <media/v4l2-dev.h>
11 #include <media/v4l2-ctrls.h>
12
13 #include "pd-common.h"
14 #include "vendorcmds.h"
15
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);
21
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");
25
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},
29 };
30
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 },
50 };
51 static const unsigned int POSEIDON_TVNORMS = ARRAY_SIZE(poseidon_tvnorms);
52
53 struct pd_audio_mode {
54 u32 tlg_audio_mode;
55 u32 v4l2_audio_sub;
56 u32 v4l2_audio_mode;
57 };
58
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 }
70 };
71 static const unsigned int POSEIDON_AUDIOMODS = ARRAY_SIZE(pd_audio_modes);
72
73 struct pd_input {
74 char *name;
75 uint32_t tlg_src;
76 };
77
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 }
83 };
84 static const unsigned int POSEIDON_INPUTS = ARRAY_SIZE(pd_inputs);
85
86 struct video_std_to_audio_std {
87 v4l2_std_id video_std;
88 int audio_std;
89 };
90
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 },
96
97 /* country : { 1, 52, 54, 55, 886 } */
98 {V4L2_STD_NTSC_M | V4L2_STD_PAL_N | V4L2_STD_PAL_M, TLG_TUNE_ASTD_BTSC},
99
100 /* country : { 81 } */
101 { V4L2_STD_NTSC_M_JP, TLG_TUNE_ASTD_EIAJ },
102
103 /* other country : TLG_TUNE_ASTD_A2 */
104 };
105 static const unsigned int map_size = ARRAY_SIZE(video_to_audio_map);
106
107 static int get_audio_std(v4l2_std_id v4l2_std)
108 {
109 int i = 0;
110
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;
114 }
115 return TLG_TUNE_ASTD_A2;
116 }
117
118 static int vidioc_querycap(struct file *file, void *fh,
119 struct v4l2_capability *cap)
120 {
121 struct video_device *vdev = video_devdata(file);
122 struct poseidon *p = video_get_drvdata(vdev);
123
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;
136 }
137
138 /*====================================================================*/
139 static void init_copy(struct video_data *video, bool index)
140 {
141 struct front_face *front = video->front;
142
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 */
149 }
150
151 static bool get_frame(struct front_face *front, int *need_init)
152 {
153 struct videobuf_buffer *vb = front->curr_frame;
154
155 if (vb)
156 return true;
157
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);
166 }
167 spin_unlock(&front->queue_lock);
168
169 return !!vb;
170 }
171
172 /* check if the video's buffer is ready */
173 static bool get_video_frame(struct front_face *front, struct video_data *video)
174 {
175 int need_init = 0;
176 bool ret = true;
177
178 ret = get_frame(front, &need_init);
179 if (ret && need_init)
180 init_copy(video, 0);
181 return ret;
182 }
183
184 static void submit_frame(struct front_face *front)
185 {
186 struct videobuf_buffer *vb = front->curr_frame;
187
188 if (vb == NULL)
189 return;
190
191 front->curr_frame = NULL;
192 vb->state = VIDEOBUF_DONE;
193 vb->field_count++;
194 v4l2_get_timestamp(&vb->ts);
195
196 wake_up(&vb->done);
197 }
198
199 /*
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.
202 */
203 static void end_field(struct video_data *video)
204 {
205 if (1 == video->field_count)
206 submit_frame(video->front);
207 else
208 init_copy(video, 1);
209 }
210
211 static void copy_video_data(struct video_data *video, char *src,
212 unsigned int count)
213 {
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)
223
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;
229 }
230 copy_data(video->lines_size);
231 }
232 if (count && count < video->lines_size) {
233 memcpy(video->dst, src, count);
234
235 video->prev_left = video->lines_size - count;
236 video->dst += count;
237 }
238 return;
239
240 overflow:
241 end_field(video);
242 }
243
244 static void check_trailer(struct video_data *video, char *src, int count)
245 {
246 struct vbi_data *vbi = video->vbi;
247 int offset; /* trailer's offset */
248 char *buf;
249
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);
254
255 if (offset > count || offset <= 0)
256 goto short_package;
257
258 buf = src + offset;
259
260 /* trailer : (VFHS) + U32 + U32 + field_num */
261 if (!strncmp(buf, "VFHS", 4)) {
262 int field_num = *((u32 *)(buf + 12));
263
264 if ((field_num & 1) ^ video->field_count) {
265 init_copy(video, video->field_count);
266 return;
267 }
268 copy_video_data(video, src, offset);
269 }
270 short_package:
271 end_field(video);
272 }
273
274 /* ========== Check this more carefully! =========== */
275 static inline void copy_vbi_data(struct vbi_data *vbi,
276 char *src, unsigned int count)
277 {
278 struct front_face *front = vbi->front;
279
280 if (front && get_frame(front, NULL)) {
281 char *buf = videobuf_to_vmalloc(front->curr_frame);
282
283 if (vbi->video->field_count)
284 buf += (vbi->vbi_size / 2);
285 memcpy(buf + vbi->copied, src, count);
286 }
287 vbi->copied += count;
288 }
289
290 /*
291 * Copy the normal data (VBI or VIDEO) without the trailer.
292 * VBI is not interlaced, while VIDEO is interlaced.
293 */
294 static inline void copy_vbi_video_data(struct video_data *video,
295 char *src, unsigned int count)
296 {
297 struct vbi_data *vbi = video->vbi;
298 unsigned int vbi_delta = (vbi->vbi_size / 2) - vbi->copied;
299
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);
305
306 /* we receive the two fields of the VBI*/
307 if (vbi->front && video->field_count)
308 submit_frame(vbi->front);
309 }
310 copy_video_data(video, src + vbi_delta, count - vbi_delta);
311 }
312 }
313
314 static void urb_complete_bulk(struct urb *urb)
315 {
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;
321
322 if (!video->is_streaming || urb->status) {
323 if (urb->status == -EPROTO)
324 goto resend_it;
325 return;
326 }
327 if (!get_video_frame(front, video))
328 goto resend_it;
329
330 if (count == urb->transfer_buffer_length)
331 copy_vbi_video_data(video, src, count);
332 else
333 check_trailer(video, src, count);
334
335 resend_it:
336 ret = usb_submit_urb(urb, GFP_ATOMIC);
337 if (ret)
338 log(" submit failed: error %d", ret);
339 }
340
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)
348 {
349 struct usb_iso_packet_descriptor *pkt = NULL;
350 int ret = GET_SUCCESS;
351
352 for (*head = *tail = -1; start < urb->number_of_packets; start++) {
353 pkt = &urb->iso_frame_desc[start];
354
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;
360 }
361
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;
368 }
369
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;
376 }
377
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;
384 }
385 break;
386 }
387 }
388
389 if (*head == -1 && *tail == -1)
390 ret = GET_NONE;
391 return ret;
392 }
393
394 /*
395 * |__|------|___|-----|_______|
396 * ^ ^
397 * | |
398 * gap gap
399 */
400 static void urb_complete_iso(struct urb *urb)
401 {
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;
407
408 if (!video->is_streaming)
409 return;
410
411 do {
412 if (!get_video_frame(front, video))
413 goto out;
414
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;
430 }
431 } while (head = tail + 1, head < urb->number_of_packets);
432
433 out:
434 ret = usb_submit_urb(urb, GFP_ATOMIC);
435 if (ret)
436 log("usb_submit_urb err : %d", ret);
437 }
438 /*============================= [ end ] =====================*/
439
440 static int prepare_iso_urb(struct video_data *video)
441 {
442 struct usb_device *udev = video->pd->udev;
443 int i;
444
445 if (video->urb_array[0])
446 return 0;
447
448 for (i = 0; i < SBUF_NUM; i++) {
449 struct urb *urb;
450 void *mem;
451 int j;
452
453 urb = usb_alloc_urb(PK_PER_URB, GFP_KERNEL);
454 if (urb == NULL)
455 goto out;
456
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);
462
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;
473
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;
477 }
478 }
479 return 0;
480 out:
481 for (; i > 0; i--)
482 ;
483 return -ENOMEM;
484 }
485
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)
491 {
492 int i = 0;
493
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;
499
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;
505 }
506
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;
511 }
512 return i;
513 }
514
515 void free_all_urb_generic(struct urb **urb_array, int num)
516 {
517 int i;
518 struct urb *urb;
519
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;
529 }
530 }
531 }
532
533 static int prepare_bulk_urb(struct video_data *video)
534 {
535 if (video->urb_array[0])
536 return 0;
537
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;
543 }
544
545 /* free the URBs */
546 static void free_all_urb(struct video_data *video)
547 {
548 free_all_urb_generic(video->urb_array, SBUF_NUM);
549 }
550
551 static void pd_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
552 {
553 videobuf_vmalloc_free(vb);
554 vb->state = VIDEOBUF_NEEDS_INIT;
555 }
556
557 static void pd_buf_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
558 {
559 struct front_face *front = q->priv_data;
560 vb->state = VIDEOBUF_QUEUED;
561 list_add_tail(&vb->queue, &front->active);
562 }
563
564 static int pd_buf_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
565 enum v4l2_field field)
566 {
567 struct front_face *front = q->priv_data;
568 int rc;
569
570 switch (front->type) {
571 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
572 if (VIDEOBUF_NEEDS_INIT == vb->state) {
573 struct v4l2_pix_format *pix;
574
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;
582 }
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;
590 }
591 break;
592 default:
593 return -EINVAL;
594 }
595 vb->field = field;
596 vb->state = VIDEOBUF_PREPARED;
597 return 0;
598 }
599
600 static int fire_all_urb(struct video_data *video)
601 {
602 int i, ret;
603
604 video->is_streaming = 1;
605
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);
610 }
611 return ret;
612 }
613
614 static int start_video_stream(struct poseidon *pd)
615 {
616 struct video_data *video = &pd->video_data;
617 s32 cmd_status;
618
619 send_set_req(pd, TAKE_REQUEST, 0, &cmd_status);
620 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_START, &cmd_status);
621
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);
628 }
629 fire_all_urb(video);
630 return 0;
631 }
632
633 static int pd_buf_setup(struct videobuf_queue *q, unsigned int *count,
634 unsigned int *size)
635 {
636 struct front_face *front = q->priv_data;
637 struct poseidon *pd = front->pd;
638
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;
645
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;
658 }
659 return start_video_stream(pd);
660 }
661
662 case V4L2_BUF_TYPE_VBI_CAPTURE: {
663 struct vbi_data *vbi = &pd->vbi_data;
664
665 *size = PAGE_ALIGN(vbi->vbi_size);
666 log("size : %d", *size);
667 if (*count == 0)
668 *count = 4;
669 }
670 break;
671 }
672 return 0;
673 }
674
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,
680 };
681
682 static int vidioc_enum_fmt(struct file *file, void *fh,
683 struct v4l2_fmtdesc *f)
684 {
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;
692 }
693
694 static int vidioc_g_fmt(struct file *file, void *fh, struct v4l2_format *f)
695 {
696 struct front_face *front = fh;
697 struct poseidon *pd = front->pd;
698
699 f->fmt.pix = pd->video_data.context.pix;
700 return 0;
701 }
702
703 /*
704 * VLC calls VIDIOC_S_STD before VIDIOC_S_FMT, while
705 * Mplayer calls them in the reverse order.
706 */
707 static int pd_vidioc_s_fmt(struct poseidon *pd, struct v4l2_pix_format *pix)
708 {
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;
713
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;
720 }
721 ret = send_set_req(pd, VIDEO_STREAM_FMT_SEL,
722 vid_resol, &cmd_status);
723
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;
734 }
735 ret |= send_set_req(pd, VIDEO_ROSOLU_SEL,
736 vid_resol, &cmd_status);
737 if (ret || cmd_status)
738 return -EBUSY;
739
740 pix_def->pixelformat = pix->pixelformat; /* save it */
741 pix->height = (context->tvnormid & V4L2_STD_525_60) ? 480 : 576;
742
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;
750 }
751 *pix = *pix_def;
752
753 return 0;
754 }
755
756 static int vidioc_s_fmt(struct file *file, void *fh, struct v4l2_format *f)
757 {
758 struct front_face *front = fh;
759 struct poseidon *pd = front->pd;
760
761 /* stop VBI here */
762 if (V4L2_BUF_TYPE_VIDEO_CAPTURE != f->type)
763 return -EINVAL;
764
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;
771 }
772
773 pd_vidioc_s_fmt(pd, &f->fmt.pix);
774 mutex_unlock(&pd->lock);
775 return 0;
776 }
777
778 static int vidioc_g_fmt_vbi(struct file *file, void *fh,
779 struct v4l2_format *v4l2_f)
780 {
781 struct front_face *front = fh;
782 struct poseidon *pd = front->pd;
783 struct v4l2_vbi_format *vbi_fmt = &v4l2_f->fmt.vbi;
784
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;
799 }
800 vbi_fmt->flags = V4L2_VBI_UNSYNC;
801 return 0;
802 }
803
804 static int set_std(struct poseidon *pd, v4l2_std_id norm)
805 {
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;
812
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;
818 }
819 }
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;
826
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;
836 }
837
838 pix = &context->pix;
839 if (pix->height != height) {
840 pix->height = height;
841 pix->sizeimage = pix->width * pix->height * 2;
842 }
843
844 out:
845 mutex_unlock(&pd->lock);
846 return ret;
847 }
848
849 static int vidioc_s_std(struct file *file, void *fh, v4l2_std_id norm)
850 {
851 struct front_face *front = fh;
852
853 return set_std(front->pd, norm);
854 }
855
856 static int vidioc_g_std(struct file *file, void *fh, v4l2_std_id *norm)
857 {
858 struct front_face *front = fh;
859
860 *norm = front->pd->video_data.context.tvnormid;
861 return 0;
862 }
863
864 static int vidioc_enum_input(struct file *file, void *fh, struct v4l2_input *in)
865 {
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;
870
871 /*
872 * the audio input index mixed with this video input,
873 * Poseidon only have one audio/video, set to "0"
874 */
875 in->audioset = 1;
876 in->tuner = 0;
877 in->std = V4L2_STD_ALL;
878 in->status = 0;
879 return 0;
880 }
881
882 static int vidioc_g_input(struct file *file, void *fh, unsigned int *i)
883 {
884 struct front_face *front = fh;
885 struct poseidon *pd = front->pd;
886 struct running_context *context = &pd->video_data.context;
887
888 *i = context->sig_index;
889 return 0;
890 }
891
892 /* We can support several inputs */
893 static int vidioc_s_input(struct file *file, void *fh, unsigned int i)
894 {
895 struct front_face *front = fh;
896 struct poseidon *pd = front->pd;
897 s32 ret, cmd_status;
898
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;
905
906 pd->video_data.context.sig_index = i;
907 return 0;
908 }
909
910 static int tlg_s_ctrl(struct v4l2_ctrl *c)
911 {
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;
916
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;
930 }
931 param.param_value = c->val;
932 params = *(s32 *)&param; /* temp code */
933
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);
938
939 set_current_state(TASK_INTERRUPTIBLE);
940 schedule_timeout(HZ/4);
941 return ret;
942 }
943
944 /* Audio ioctls */
945 static int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *a)
946 {
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;
954 }
955
956 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
957 {
958 a->index = 0;
959 a->capability = V4L2_AUDCAP_STEREO;
960 strcpy(a->name, "USB audio in");
961 a->mode = 0;
962 return 0;
963 }
964
965 static int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *a)
966 {
967 return (0 == a->index) ? 0 : -EINVAL;
968 }
969
970 /* Tuner ioctls */
971 static int vidioc_g_tuner(struct file *file, void *fh, struct v4l2_tuner *tuner)
972 {
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;
978
979 if (0 != tuner->index)
980 return -EINVAL;
981
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));
985
986 while (atv_stat.sig_lock_busy && count-- && !ret) {
987 set_current_state(TASK_INTERRUPTIBLE);
988 schedule_timeout(HZ);
989
990 ret = send_get_req(pd, TUNER_STATUS, TLG_MODE_ANALOG_TV,
991 &atv_stat, &cmd_status, sizeof(atv_stat));
992 }
993 mutex_unlock(&pd->lock);
994
995 if (debug_mode)
996 log("P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
997
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;
1004
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;
1016 }
1017
1018 static int pd_vidioc_s_tuner(struct poseidon *pd, int index)
1019 {
1020 s32 ret = 0, cmd_status, param, audiomode;
1021
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;
1032 }
1033
1034 static int vidioc_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *a)
1035 {
1036 struct front_face *front = fh;
1037 struct poseidon *pd = front->pd;
1038 int index;
1039
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;
1046 }
1047
1048 static int vidioc_g_frequency(struct file *file, void *fh,
1049 struct v4l2_frequency *freq)
1050 {
1051 struct front_face *front = fh;
1052 struct poseidon *pd = front->pd;
1053 struct running_context *context = &pd->video_data.context;
1054
1055 if (0 != freq->tuner)
1056 return -EINVAL;
1057 freq->frequency = context->freq;
1058 freq->type = V4L2_TUNER_ANALOG_TV;
1059 return 0;
1060 }
1061
1062 static int set_frequency(struct poseidon *pd, u32 *frequency)
1063 {
1064 s32 ret = 0, param, cmd_status;
1065 struct running_context *context = &pd->video_data.context;
1066
1067 *frequency = clamp(*frequency,
1068 TUNER_FREQ_MIN / 62500, TUNER_FREQ_MAX / 62500);
1069 param = (*frequency) * 62500 / 1000;
1070
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);
1074
1075 msleep(250); /* wait for a while until the hardware is ready. */
1076 context->freq = *frequency;
1077 mutex_unlock(&pd->lock);
1078 return ret;
1079 }
1080
1081 static int vidioc_s_frequency(struct file *file, void *fh,
1082 const struct v4l2_frequency *freq)
1083 {
1084 struct front_face *front = fh;
1085 struct poseidon *pd = front->pd;
1086 u32 frequency = freq->frequency;
1087
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);
1095 }
1096
1097 static int vidioc_reqbufs(struct file *file, void *fh,
1098 struct v4l2_requestbuffers *b)
1099 {
1100 struct front_face *front = file->private_data;
1101 return videobuf_reqbufs(&front->q, b);
1102 }
1103
1104 static int vidioc_querybuf(struct file *file, void *fh, struct v4l2_buffer *b)
1105 {
1106 struct front_face *front = file->private_data;
1107 return videobuf_querybuf(&front->q, b);
1108 }
1109
1110 static int vidioc_qbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1111 {
1112 struct front_face *front = file->private_data;
1113 return videobuf_qbuf(&front->q, b);
1114 }
1115
1116 static int vidioc_dqbuf(struct file *file, void *fh, struct v4l2_buffer *b)
1117 {
1118 struct front_face *front = file->private_data;
1119 return videobuf_dqbuf(&front->q, b, file->f_flags & O_NONBLOCK);
1120 }
1121
1122 /* Just stop the URBs, do not free the URBs */
1123 static int usb_transfer_stop(struct video_data *video)
1124 {
1125 if (video->is_streaming) {
1126 int i;
1127 s32 cmd_status;
1128 struct poseidon *pd = video->pd;
1129
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]);
1134 }
1135
1136 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1137 &cmd_status);
1138 }
1139 return 0;
1140 }
1141
1142 int stop_all_video_stream(struct poseidon *pd)
1143 {
1144 struct video_data *video = &pd->video_data;
1145 struct vbi_data *vbi = &pd->vbi_data;
1146
1147 mutex_lock(&pd->lock);
1148 if (video->is_streaming) {
1149 struct front_face *front = video->front;
1150
1151 /* stop the URBs */
1152 usb_transfer_stop(video);
1153 free_all_urb(video);
1154
1155 /* stop the host side of VIDEO */
1156 videobuf_stop(&front->q);
1157 videobuf_mmap_free(&front->q);
1158
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);
1164 }
1165 }
1166 mutex_unlock(&pd->lock);
1167 return 0;
1168 }
1169
1170 /*
1171 * The bubbles can seriously damage the video's quality,
1172 * though it occurs in very rare situation.
1173 */
1174 static void iso_bubble_handler(struct work_struct *w)
1175 {
1176 struct video_data *video;
1177 struct poseidon *pd;
1178
1179 video = container_of(w, struct video_data, bubble_work);
1180 pd = video->pd;
1181
1182 mutex_lock(&pd->lock);
1183 usb_transfer_stop(video);
1184 msleep(500);
1185 start_video_stream(pd);
1186 mutex_unlock(&pd->lock);
1187 }
1188
1189
1190 static int vidioc_streamon(struct file *file, void *fh,
1191 enum v4l2_buf_type type)
1192 {
1193 struct front_face *front = fh;
1194
1195 if (unlikely(type != front->type))
1196 return -EINVAL;
1197 return videobuf_streamon(&front->q);
1198 }
1199
1200 static int vidioc_streamoff(struct file *file, void *fh,
1201 enum v4l2_buf_type type)
1202 {
1203 struct front_face *front = file->private_data;
1204
1205 if (unlikely(type != front->type))
1206 return -EINVAL;
1207 return videobuf_streamoff(&front->q);
1208 }
1209
1210 /* Set the firmware's default values : need altersetting */
1211 static int pd_video_checkmode(struct poseidon *pd)
1212 {
1213 s32 ret = 0, cmd_status, audiomode;
1214
1215 set_current_state(TASK_INTERRUPTIBLE);
1216 schedule_timeout(HZ/2);
1217
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;
1225
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 */
1238
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;
1248 }
1249
1250 #ifdef CONFIG_PM
1251 static int pm_video_suspend(struct poseidon *pd)
1252 {
1253 /* stop audio */
1254 pm_alsa_suspend(pd);
1255
1256 /* stop and free all the URBs */
1257 usb_transfer_stop(&pd->video_data);
1258 free_all_urb(&pd->video_data);
1259
1260 /* reset the interface */
1261 usb_set_interface(pd->udev, 0, 0);
1262 msleep(300);
1263 return 0;
1264 }
1265
1266 static int restore_v4l2_context(struct poseidon *pd,
1267 struct running_context *context)
1268 {
1269 struct front_face *front = pd->video_data.front;
1270
1271 pd_video_checkmode(pd);
1272
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;
1279 }
1280
1281 static int pm_video_resume(struct poseidon *pd)
1282 {
1283 struct video_data *video = &pd->video_data;
1284
1285 /* resume the video */
1286 /* [1] restore the origin V4L2 parameters */
1287 restore_v4l2_context(pd, &video->context);
1288
1289 /* [2] initiate video copy variables */
1290 if (video->front->curr_frame)
1291 init_copy(video, 0);
1292
1293 /* [3] fire urbs */
1294 start_video_stream(pd);
1295
1296 /* resume the audio */
1297 pm_alsa_resume(pd);
1298 return 0;
1299 }
1300 #endif
1301
1302 void set_debug_mode(struct video_device *vfd, int debug_mode)
1303 {
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;
1309 }
1310
1311 static void init_video_context(struct running_context *context)
1312 {
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 };
1325 }
1326
1327 static int pd_video_open(struct file *file)
1328 {
1329 struct video_device *vfd = video_devdata(file);
1330 struct poseidon *pd = video_get_drvdata(vfd);
1331 struct front_face *front = NULL;
1332 int ret = -ENOMEM;
1333
1334 mutex_lock(&pd->lock);
1335 usb_autopm_get_interface(pd->interface);
1336
1337 if (pd->state && !(pd->state & POSEIDON_STATE_ANALOG)) {
1338 ret = -EBUSY;
1339 goto out;
1340 }
1341 front = kzalloc(sizeof(struct front_face), GFP_KERNEL);
1342 if (!front)
1343 goto out;
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);
1347
1348 ret = pd_video_checkmode(pd);
1349 if (ret < 0) {
1350 kfree(front);
1351 ret = -1;
1352 goto out;
1353 }
1354
1355 front->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1356 pd->video_data.users++;
1357 set_debug_mode(vfd, debug_mode);
1358
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*/
1364 front, NULL);
1365 } else {
1366 front->type = V4L2_BUF_TYPE_VBI_CAPTURE;
1367 pd->vbi_data.front = front;
1368 pd->vbi_data.users++;
1369
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),
1375 front, NULL);
1376 }
1377
1378 pd->state |= POSEIDON_STATE_ANALOG;
1379 front->pd = pd;
1380 front->curr_frame = NULL;
1381 INIT_LIST_HEAD(&front->active);
1382 spin_lock_init(&front->queue_lock);
1383
1384 file->private_data = front;
1385 kref_get(&pd->kref);
1386
1387 mutex_unlock(&pd->lock);
1388 return 0;
1389 out:
1390 usb_autopm_put_interface(pd->interface);
1391 mutex_unlock(&pd->lock);
1392 return ret;
1393 }
1394
1395 static int pd_video_release(struct file *file)
1396 {
1397 struct front_face *front = file->private_data;
1398 struct poseidon *pd = front->pd;
1399 s32 cmd_status = 0;
1400
1401 mutex_lock(&pd->lock);
1402
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);
1407
1408 /* stop the firmware */
1409 send_set_req(pd, PLAY_SERVICE, TLG_TUNE_PLAY_SVC_STOP,
1410 &cmd_status);
1411
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--;
1417 }
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);
1422
1423 usb_autopm_put_interface(pd->interface);
1424 mutex_unlock(&pd->lock);
1425
1426 kfree(front);
1427 file->private_data = NULL;
1428 kref_put(&pd->kref, poseidon_delete);
1429 return 0;
1430 }
1431
1432 static int pd_video_mmap(struct file *file, struct vm_area_struct *vma)
1433 {
1434 struct front_face *front = file->private_data;
1435 return videobuf_mmap_mapper(&front->q, vma);
1436 }
1437
1438 static unsigned int pd_video_poll(struct file *file, poll_table *table)
1439 {
1440 struct front_face *front = file->private_data;
1441 return videobuf_poll_stream(file, &front->q, table);
1442 }
1443
1444 static ssize_t pd_video_read(struct file *file, char __user *buffer,
1445 size_t count, loff_t *ppos)
1446 {
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);
1450 }
1451
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 */
1461 };
1462
1463 static const struct v4l2_ioctl_ops pd_video_ioctl_ops = {
1464 .vidioc_querycap = vidioc_querycap,
1465
1466 /* Video format */
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 */
1471
1472 /* Input */
1473 .vidioc_g_input = vidioc_g_input,
1474 .vidioc_s_input = vidioc_s_input,
1475 .vidioc_enum_input = vidioc_enum_input,
1476
1477 /* Audio ioctls */
1478 .vidioc_enumaudio = vidioc_enumaudio,
1479 .vidioc_g_audio = vidioc_g_audio,
1480 .vidioc_s_audio = vidioc_s_audio,
1481
1482 /* Tuner ioctls */
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,
1489
1490 /* Buffer handlers */
1491 .vidioc_reqbufs = vidioc_reqbufs,
1492 .vidioc_querybuf = vidioc_querybuf,
1493 .vidioc_qbuf = vidioc_qbuf,
1494 .vidioc_dqbuf = vidioc_dqbuf,
1495
1496 /* Stream on/off */
1497 .vidioc_streamon = vidioc_streamon,
1498 .vidioc_streamoff = vidioc_streamoff,
1499 };
1500
1501 static struct video_device pd_video_template = {
1502 .name = "Telegent-Video",
1503 .fops = &pd_video_fops,
1504 .minor = -1,
1505 .release = video_device_release_empty,
1506 .tvnorms = V4L2_STD_ALL,
1507 .ioctl_ops = &pd_video_ioctl_ops,
1508 };
1509
1510 static const struct v4l2_ctrl_ops tlg_ctrl_ops = {
1511 .s_ctrl = tlg_s_ctrl,
1512 };
1513
1514 void pd_video_exit(struct poseidon *pd)
1515 {
1516 struct video_data *video = &pd->video_data;
1517 struct vbi_data *vbi = &pd->vbi_data;
1518
1519 video_unregister_device(&video->v_dev);
1520 video_unregister_device(&vbi->v_dev);
1521 v4l2_ctrl_handler_free(&video->ctrl_handler);
1522 log();
1523 }
1524
1525 int pd_video_init(struct poseidon *pd)
1526 {
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;
1531 int ret = -ENOMEM;
1532
1533 v4l2_ctrl_handler_init(hdl, 4);
1534 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_BRIGHTNESS,
1535 0, 10000, 1, 100);
1536 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_CONTRAST,
1537 0, 10000, 1, 100);
1538 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_HUE,
1539 0, 10000, 1, 100);
1540 v4l2_ctrl_new_std(hdl, &tlg_ctrl_ops, V4L2_CID_SATURATION,
1541 0, 10000, 1, 100);
1542 if (hdl->error) {
1543 v4l2_ctrl_handler_free(hdl);
1544 return hdl->error;
1545 }
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);
1551
1552 ret = video_register_device(&video->v_dev, VFL_TYPE_GRABBER, -1);
1553 if (ret != 0)
1554 goto out;
1555
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);
1562 if (ret != 0)
1563 goto out;
1564 log("register VIDEO/VBI devices");
1565 return 0;
1566 out:
1567 log("VIDEO/VBI devices register failed, : %d", ret);
1568 pd_video_exit(pd);
1569 return ret;
1570 }
This page took 0.228495 seconds and 5 git commands to generate.