Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pd-video.c
Go to the documentation of this file.
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 #ifdef CONFIG_PM
16 static int pm_video_suspend(struct poseidon *pd);
17 static int pm_video_resume(struct poseidon *pd);
18 #endif
19 static void iso_bubble_handler(struct work_struct *w);
20 
21 static int usb_transfer_mode;
22 module_param(usb_transfer_mode, int, 0644);
23 MODULE_PARM_DESC(usb_transfer_mode, "0 = Bulk, 1 = Isochronous");
24 
25 static 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 
30 static 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 },
49 };
50 static const unsigned int POSEIDON_TVNORMS = ARRAY_SIZE(poseidon_tvnorms);
51 
52 struct pd_audio_mode {
56 };
57 
58 static const struct pd_audio_mode pd_audio_modes[] = {
69 };
70 static const unsigned int POSEIDON_AUDIOMODS = ARRAY_SIZE(pd_audio_modes);
71 
72 struct pd_input {
73  char *name;
75 };
76 
77 static 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 };
83 static const unsigned int POSEIDON_INPUTS = ARRAY_SIZE(pd_inputs);
84 
88 };
89 
90 static struct poseidon_control controls[] = {
91  {
93  "brightness", 0, 10000, 1, 100, 0, },
95  }, {
97  "contrast", 0, 10000, 1, 100, 0, },
99  }, {
101  "hue", 0, 10000, 1, 100, 0, },
103  }, {
105  "saturation", 0, 10000, 1, 100, 0, },
107  },
108 };
109 
113 };
114 
115 static 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 } */
120 
121  /* country : { 1, 52, 54, 55, 886 } */
123 
124  /* country : { 81 } */
126 
127  /* other country : TLG_TUNE_ASTD_A2 */
128 };
129 static const unsigned int map_size = ARRAY_SIZE(video_to_audio_map);
130 
131 static 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 
142 static 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));
156  return 0;
157 }
158 
159 /*====================================================================*/
160 static void init_copy(struct video_data *video, bool index)
161 {
162  struct front_face *front = video->front;
163 
164  video->field_count = index;
165  video->lines_copied = 0;
166  video->prev_left = 0 ;
167  video->dst = (char *)videobuf_to_vmalloc(front->curr_frame)
168  + index * video->lines_size;
169  video->vbi->copied = 0; /* set it here */
170 }
171 
172 static bool get_frame(struct front_face *front, int *need_init)
173 {
174  struct videobuf_buffer *vb = front->curr_frame;
175 
176  if (vb)
177  return true;
178 
179  spin_lock(&front->queue_lock);
180  if (!list_empty(&front->active)) {
181  vb = list_entry(front->active.next,
182  struct videobuf_buffer, queue);
183  if (need_init)
184  *need_init = 1;
185  front->curr_frame = vb;
186  list_del_init(&vb->queue);
187  }
188  spin_unlock(&front->queue_lock);
189 
190  return !!vb;
191 }
192 
193 /* check if the video's buffer is ready */
194 static bool get_video_frame(struct front_face *front, struct video_data *video)
195 {
196  int need_init = 0;
197  bool ret = true;
198 
199  ret = get_frame(front, &need_init);
200  if (ret && need_init)
201  init_copy(video, 0);
202  return ret;
203 }
204 
205 static void submit_frame(struct front_face *front)
206 {
207  struct videobuf_buffer *vb = front->curr_frame;
208 
209  if (vb == NULL)
210  return;
211 
212  front->curr_frame = NULL;
213  vb->state = VIDEOBUF_DONE;
214  vb->field_count++;
215  do_gettimeofday(&vb->ts);
216 
217  wake_up(&vb->done);
218 }
219 
220 /*
221  * A frame is composed of two fields. If we receive all the two fields,
222  * call the submit_frame() to submit the whole frame to applications.
223  */
224 static void end_field(struct video_data *video)
225 {
226  /* logs(video->front); */
227  if (1 == video->field_count)
228  submit_frame(video->front);
229  else
230  init_copy(video, 1);
231 }
232 
233 static void copy_video_data(struct video_data *video, char *src,
234  unsigned int count)
235 {
236 #define copy_data(len) \
237  do { \
238  if (++video->lines_copied > video->lines_per_field) \
239  goto overflow; \
240  memcpy(video->dst, src, len);\
241  video->dst += len + video->lines_size; \
242  src += len; \
243  count -= len; \
244  } while (0)
245 
246  while (count && count >= video->lines_size) {
247  if (video->prev_left) {
248  copy_data(video->prev_left);
249  video->prev_left = 0;
250  continue;
251  }
252  copy_data(video->lines_size);
253  }
254  if (count && count < video->lines_size) {
255  memcpy(video->dst, src, count);
256 
257  video->prev_left = video->lines_size - count;
258  video->dst += count;
259  }
260  return;
261 
262 overflow:
263  end_field(video);
264 }
265 
266 static void check_trailer(struct video_data *video, char *src, int count)
267 {
268  struct vbi_data *vbi = video->vbi;
269  int offset; /* trailer's offset */
270  char *buf;
271 
272  offset = (video->context.pix.sizeimage / 2 + vbi->vbi_size / 2)
273  - (vbi->copied + video->lines_size * video->lines_copied);
274  if (video->prev_left)
275  offset -= (video->lines_size - video->prev_left);
276 
277  if (offset > count || offset <= 0)
278  goto short_package;
279 
280  buf = src + offset;
281 
282  /* trailer : (VFHS) + U32 + U32 + field_num */
283  if (!strncmp(buf, "VFHS", 4)) {
284  int field_num = *((u32 *)(buf + 12));
285 
286  if ((field_num & 1) ^ video->field_count) {
287  init_copy(video, video->field_count);
288  return;
289  }
290  copy_video_data(video, src, offset);
291  }
292 short_package:
293  end_field(video);
294 }
295 
296 /* ========== Check this more carefully! =========== */
297 static inline void copy_vbi_data(struct vbi_data *vbi,
298  char *src, unsigned int count)
299 {
300  struct front_face *front = vbi->front;
301 
302  if (front && get_frame(front, NULL)) {
303  char *buf = videobuf_to_vmalloc(front->curr_frame);
304 
305  if (vbi->video->field_count)
306  buf += (vbi->vbi_size / 2);
307  memcpy(buf + vbi->copied, src, count);
308  }
309  vbi->copied += count;
310 }
311 
312 /*
313  * Copy the normal data (VBI or VIDEO) without the trailer.
314  * VBI is not interlaced, while VIDEO is interlaced.
315  */
316 static inline void copy_vbi_video_data(struct video_data *video,
317  char *src, unsigned int count)
318 {
319  struct vbi_data *vbi = video->vbi;
320  unsigned int vbi_delta = (vbi->vbi_size / 2) - vbi->copied;
321 
322  if (vbi_delta >= count) {
323  copy_vbi_data(vbi, src, count);
324  } else {
325  if (vbi_delta) {
326  copy_vbi_data(vbi, src, vbi_delta);
327 
328  /* we receive the two fields of the VBI*/
329  if (vbi->front && video->field_count)
330  submit_frame(vbi->front);
331  }
332  copy_video_data(video, src + vbi_delta, count - vbi_delta);
333  }
334 }
335 
336 static void urb_complete_bulk(struct urb *urb)
337 {
338  struct front_face *front = urb->context;
339  struct video_data *video = &front->pd->video_data;
340  char *src = (char *)urb->transfer_buffer;
341  int count = urb->actual_length;
342  int ret = 0;
343 
344  if (!video->is_streaming || urb->status) {
345  if (urb->status == -EPROTO)
346  goto resend_it;
347  return;
348  }
349  if (!get_video_frame(front, video))
350  goto resend_it;
351 
352  if (count == urb->transfer_buffer_length)
353  copy_vbi_video_data(video, src, count);
354  else
355  check_trailer(video, src, count);
356 
357 resend_it:
358  ret = usb_submit_urb(urb, GFP_ATOMIC);
359  if (ret)
360  log(" submit failed: error %d", ret);
361 }
362 
363 /************************* for ISO *********************/
364 #define GET_SUCCESS (0)
365 #define GET_TRAILER (1)
366 #define GET_TOO_MUCH_BUBBLE (2)
367 #define GET_NONE (3)
368 static int get_chunk(int start, struct urb *urb,
369  int *head, int *tail, int *bubble_err)
370 {
371  struct usb_iso_packet_descriptor *pkt = NULL;
372  int ret = GET_SUCCESS;
373 
374  for (*head = *tail = -1; start < urb->number_of_packets; start++) {
375  pkt = &urb->iso_frame_desc[start];
376 
377  /* handle the bubble of the Hub */
378  if (-EOVERFLOW == pkt->status) {
379  if (++*bubble_err > urb->number_of_packets / 3)
380  return GET_TOO_MUCH_BUBBLE;
381  continue;
382  }
383 
384  /* This is the gap */
385  if (pkt->status || pkt->actual_length <= 0
386  || pkt->actual_length > ISO_PKT_SIZE) {
387  if (*head != -1)
388  break;
389  continue;
390  }
391 
392  /* a good isochronous packet */
393  if (pkt->actual_length == ISO_PKT_SIZE) {
394  if (*head == -1)
395  *head = start;
396  *tail = start;
397  continue;
398  }
399 
400  /* trailer is here */
401  if (pkt->actual_length < ISO_PKT_SIZE) {
402  if (*head == -1) {
403  *head = start;
404  *tail = start;
405  return GET_TRAILER;
406  }
407  break;
408  }
409  }
410 
411  if (*head == -1 && *tail == -1)
412  ret = GET_NONE;
413  return ret;
414 }
415 
416 /*
417  * |__|------|___|-----|_______|
418  * ^ ^
419  * | |
420  * gap gap
421  */
422 static void urb_complete_iso(struct urb *urb)
423 {
424  struct front_face *front = urb->context;
425  struct video_data *video = &front->pd->video_data;
426  int bubble_err = 0, head = 0, tail = 0;
427  char *src = (char *)urb->transfer_buffer;
428  int ret = 0;
429 
430  if (!video->is_streaming)
431  return;
432 
433  do {
434  if (!get_video_frame(front, video))
435  goto out;
436 
437  switch (get_chunk(head, urb, &head, &tail, &bubble_err)) {
438  case GET_SUCCESS:
439  copy_vbi_video_data(video, src + (head * ISO_PKT_SIZE),
440  (tail - head + 1) * ISO_PKT_SIZE);
441  break;
442  case GET_TRAILER:
443  check_trailer(video, src + (head * ISO_PKT_SIZE),
444  ISO_PKT_SIZE);
445  break;
446  case GET_NONE:
447  goto out;
448  case GET_TOO_MUCH_BUBBLE:
449  log("\t We got too much bubble");
450  schedule_work(&video->bubble_work);
451  return;
452  }
453  } while (head = tail + 1, head < urb->number_of_packets);
454 
455 out:
456  ret = usb_submit_urb(urb, GFP_ATOMIC);
457  if (ret)
458  log("usb_submit_urb err : %d", ret);
459 }
460 /*============================= [ end ] =====================*/
461 
462 static int prepare_iso_urb(struct video_data *video)
463 {
464  struct usb_device *udev = video->pd->udev;
465  int i;
466 
467  if (video->urb_array[0])
468  return 0;
469 
470  for (i = 0; i < SBUF_NUM; i++) {
471  struct urb *urb;
472  void *mem;
473  int j;
474 
476  if (urb == NULL)
477  goto out;
478 
479  video->urb_array[i] = urb;
480  mem = usb_alloc_coherent(udev,
482  GFP_KERNEL,
483  &urb->transfer_dma);
484 
485  urb->complete = urb_complete_iso; /* handler */
486  urb->dev = udev;
487  urb->context = video->front;
488  urb->pipe = usb_rcvisocpipe(udev,
489  video->endpoint_addr);
490  urb->interval = 1;
491  urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
492  urb->number_of_packets = PK_PER_URB;
493  urb->transfer_buffer = mem;
494  urb->transfer_buffer_length = PK_PER_URB * ISO_PKT_SIZE;
495 
496  for (j = 0; j < PK_PER_URB; j++) {
497  urb->iso_frame_desc[j].offset = ISO_PKT_SIZE * j;
498  urb->iso_frame_desc[j].length = ISO_PKT_SIZE;
499  }
500  }
501  return 0;
502 out:
503  for (; i > 0; i--)
504  ;
505  return -ENOMEM;
506 }
507 
508 /* return the succeeded number of the allocation */
509 int alloc_bulk_urbs_generic(struct urb **urb_array, int num,
510  struct usb_device *udev, u8 ep_addr,
511  int buf_size, gfp_t gfp_flags,
512  usb_complete_t complete_fn, void *context)
513 {
514  int i = 0;
515 
516  for (; i < num; i++) {
517  void *mem;
518  struct urb *urb = usb_alloc_urb(0, gfp_flags);
519  if (urb == NULL)
520  return i;
521 
522  mem = usb_alloc_coherent(udev, buf_size, gfp_flags,
523  &urb->transfer_dma);
524  if (mem == NULL) {
525  usb_free_urb(urb);
526  return i;
527  }
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 
537 void 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) {
545  usb_free_coherent(urb->dev,
546  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 
555 static 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 */
568 static void free_all_urb(struct video_data *video)
569 {
570  free_all_urb_generic(video->urb_array, SBUF_NUM);
571 }
572 
573 static void pd_buf_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
574 {
577 }
578 
579 static 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 
586 static 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) {
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;
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 
622 static int fire_all_urb(struct video_data *video)
623 {
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 
636 static 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);
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 
655 static 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;
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 
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 
697 static 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 
704 static 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;
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 
716 static 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 
726 static 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  */
736 static 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 {
748  vid_resol = TLG_TUNER_VID_FORMAT_YUV;
749  }
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  return -EBUSY;
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 */
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) {
863  height = 480;
864  } else {
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 static 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);
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;
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 
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;
1017  strcpy(a->name, "USB audio in");
1018  /*Poseidon have no AVL function.*/
1019  a->mode = 0;
1020  return 0;
1021 }
1022 
1023 static int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *a)
1024 {
1025  a->index = 0;
1027  strcpy(a->name, "USB audio in");
1028  a->mode = 0;
1029  return 0;
1030 }
1031 
1032 static int vidioc_s_audio(struct file *file, void *fh, const 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);
1051  &atv_stat, &cmd_status, sizeof(atv_stat));
1052 
1053  while (atv_stat.sig_lock_busy && count-- && !ret) {
1056 
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;
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;
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 static 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 
1206  &cmd_status);
1207  }
1208  return 0;
1209 }
1210 
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 
1287  schedule_timeout(HZ/2);
1288 
1289  /* choose the altersetting */
1290  ret = usb_set_interface(pd->udev, 0,
1291  (pd->cur_transfer_mode ?
1294  if (ret < 0)
1295  goto error;
1296 
1297  /* set default parameters for PAL-D , with the VBI enabled*/
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);
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);
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 
1374 {
1375  vfd->debug = 0;
1376  if (debug_mode & 0x1)
1377  vfd->debug = V4L2_DEBUG_IOCTL;
1378  if (debug_mode & 0x2)
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 
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,
1433  V4L2_FIELD_INTERLACED,/* video is interlacd */
1434  sizeof(struct videobuf_buffer),/*it's enough*/
1435  front, NULL);
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;
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,
1450  V4L2_FIELD_NONE, /* vbi is NONE mode */
1451  sizeof(struct videobuf_buffer),
1452  front, NULL);
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 */
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 static 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 static 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,
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); */
1610  video_set_drvdata(vfd, pd);
1611  return vfd;
1612 }
1613 
1615 {
1616  struct video_device *dev = *v_dev;
1617 
1618  if (dev == NULL)
1619  return;
1620 
1621  if (video_is_registered(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