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