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