3 #include <linux/videodev2.h>
6 #include <linux/sched.h>
7 #include <linux/slab.h>
16 static int pm_video_suspend(
struct poseidon *pd);
17 static int pm_video_resume(
struct poseidon *pd);
21 static int usb_transfer_mode;
50 static const unsigned int POSEIDON_TVNORMS =
ARRAY_SIZE(poseidon_tvnorms);
70 static const unsigned int POSEIDON_AUDIOMODS =
ARRAY_SIZE(pd_audio_modes);
77 static const struct pd_input pd_inputs[] = {
83 static const unsigned int POSEIDON_INPUTS =
ARRAY_SIZE(pd_inputs);
93 "brightness", 0, 10000, 1, 100, 0, },
97 "contrast", 0, 10000, 1, 100, 0, },
101 "hue", 0, 10000, 1, 100, 0, },
105 "saturation", 0, 10000, 1, 100, 0, },
129 static const unsigned int map_size =
ARRAY_SIZE(video_to_audio_map);
135 for (; i < map_size; i++) {
136 if (v4l2_std & video_to_audio_map[i].
video_std)
142 static int vidioc_querycap(
struct file *
file,
void *fh,
169 video->
vbi->copied = 0;
172 static bool get_frame(
struct front_face *front,
int *need_init)
180 if (!list_empty(&front->
active)) {
186 list_del_init(&vb->
queue);
199 ret = get_frame(front, &need_init);
200 if (ret && need_init)
205 static void submit_frame(
struct front_face *front)
224 static void end_field(
struct video_data *video)
228 submit_frame(video->
front);
233 static void copy_video_data(
struct video_data *video,
char *
src,
236 #define copy_data(len) \
238 if (++video->lines_copied > video->lines_per_field) \
240 memcpy(video->dst, src, len);\
241 video->dst += len + video->lines_size; \
254 if (count && count < video->lines_size) {
266 static void check_trailer(
struct video_data *video,
char *src,
int count)
277 if (offset > count || offset <= 0)
283 if (!
strncmp(buf,
"VFHS", 4)) {
284 int field_num = *((
u32 *)(buf + 12));
290 copy_video_data(video, src, offset);
297 static inline void copy_vbi_data(
struct vbi_data *vbi,
298 char *src,
unsigned int count)
302 if (front && get_frame(front,
NULL)) {
305 if (vbi->
video->field_count)
316 static inline void copy_vbi_video_data(
struct video_data *video,
317 char *src,
unsigned int count)
322 if (vbi_delta >= count) {
323 copy_vbi_data(vbi, src, count);
326 copy_vbi_data(vbi, src, vbi_delta);
330 submit_frame(vbi->
front);
332 copy_video_data(video, src + vbi_delta, count - vbi_delta);
336 static void urb_complete_bulk(
struct urb *
urb)
340 char *src = (
char *)urb->transfer_buffer;
341 int count = urb->actual_length;
345 if (urb->status == -
EPROTO)
349 if (!get_video_frame(front, video))
352 if (count == urb->transfer_buffer_length)
353 copy_vbi_video_data(video, src, count);
355 check_trailer(video, src, count);
360 log(
" submit failed: error %d", ret);
364 #define GET_SUCCESS (0)
365 #define GET_TRAILER (1)
366 #define GET_TOO_MUCH_BUBBLE (2)
368 static int get_chunk(
int start,
struct urb *urb,
369 int *
head,
int *
tail,
int *bubble_err)
371 struct usb_iso_packet_descriptor *pkt =
NULL;
374 for (*head = *tail = -1; start < urb->number_of_packets; start++) {
375 pkt = &urb->iso_frame_desc[
start];
379 if (++*bubble_err > urb->number_of_packets / 3)
385 if (pkt->status || pkt->actual_length <= 0
411 if (*head == -1 && *tail == -1)
422 static void urb_complete_iso(
struct urb *urb)
426 int bubble_err = 0,
head = 0,
tail = 0;
427 char *src = (
char *)urb->transfer_buffer;
434 if (!get_video_frame(front, video))
437 switch (get_chunk(
head, urb, &
head, &
tail, &bubble_err)) {
443 check_trailer(video, src + (
head * ISO_PKT_SIZE),
449 log(
"\t We got too much bubble");
458 log(
"usb_submit_urb err : %d", ret);
462 static int prepare_iso_urb(
struct video_data *video)
464 struct usb_device *
udev = video->
pd->udev;
485 urb->complete = urb_complete_iso;
487 urb->context = video->
front;
488 urb->pipe = usb_rcvisocpipe(udev,
491 urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
493 urb->transfer_buffer =
mem;
497 urb->iso_frame_desc[
j].offset = ISO_PKT_SIZE *
j;
510 struct usb_device *udev,
u8 ep_addr,
512 usb_complete_t complete_fn,
void *
context)
516 for (; i < num; i++) {
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;
542 for (i = 0; i < num; i++) {
546 urb->transfer_buffer_length,
547 urb->transfer_buffer,
555 static int prepare_bulk_urb(
struct video_data *video)
563 urb_complete_bulk, video->
front);
568 static void free_all_urb(
struct video_data *video)
592 switch (front->
type) {
597 pix = &front->
pd->video_data.context.pix;
608 vb->
size = front->
pd->vbi_data.vbi_size;
622 static int fire_all_urb(
struct video_data *video)
631 log(
"(%d) failed: error %d", i, ret);
636 static int start_video_stream(
struct poseidon *pd)
645 prepare_iso_urb(video);
649 prepare_bulk_urb(video);
655 static int pd_buf_setup(
struct videobuf_queue *q,
unsigned int *count,
661 switch (front->
type) {
675 video->
vbi->video = video;
679 video->
front = front;
681 return start_video_stream(pd);
688 log(
"size : %d", *size);
698 .buf_setup = pd_buf_setup,
699 .buf_prepare = pd_buf_prepare,
700 .buf_queue = pd_buf_queue,
701 .buf_release = pd_buf_release,
704 static int vidioc_enum_fmt(
struct file *
file,
void *fh,
716 static int vidioc_g_fmt(
struct file *file,
void *fh,
struct v4l2_format *f)
726 static int vidioc_try_fmt(
struct file *file,
void *fh,
741 s32 ret = 0, cmd_status = 0, vid_resol;
751 vid_resol, &cmd_status);
755 switch (pix->
width) {
765 vid_resol, &cmd_status);
766 if (ret || cmd_status)
785 static int vidioc_s_fmt(
struct file *file,
void *fh,
struct v4l2_format *f)
803 pd_vidioc_s_fmt(pd, &f->
fmt.
pix);
808 static int vidioc_g_fmt_vbi(
struct file *file,
void *fh,
820 vbi_fmt->
start[0] = 10;
821 vbi_fmt->
start[1] = 264;
825 vbi_fmt->
start[0] = 6;
826 vbi_fmt->
start[1] = 314;
844 for (i = 0; i < POSEIDON_TVNORMS; i++) {
845 if (*norm & poseidon_tvnorms[i].v4l2_id) {
847 log(
"name : %s", poseidon_tvnorms[i].
name);
855 if (ret || cmd_status)
870 if (pix->
height != height) {
880 static int vidioc_s_std(
struct file *file,
void *fh,
v4l2_std_id *norm)
884 return set_std(front->
pd, norm);
887 static int vidioc_enum_input(
struct file *file,
void *fh,
struct v4l2_input *
in)
891 if (in->
index < 0 || in->
index >= POSEIDON_INPUTS)
908 static int vidioc_g_input(
struct file *file,
void *fh,
unsigned int *i)
920 static int vidioc_s_input(
struct file *file,
void *fh,
unsigned int i)
926 if (i < 0 || i >= POSEIDON_INPUTS)
929 pd_inputs[i].tlg_src, &cmd_status);
942 for (; control < &controls[array_size]; control++)
948 static int vidioc_queryctrl(
struct file *file,
void *fh,
953 control = check_control_id(a->
id);
961 static int vidioc_g_ctrl(
struct file *file,
void *fh,
struct v4l2_control *
ctrl)
969 control = check_control_id(ctrl->
id);
978 if (ret || cmd_status)
985 static int vidioc_s_ctrl(
struct file *file,
void *fh,
struct v4l2_control *a)
993 control = check_control_id(a->
id);
999 params = *(
s32 *)¶m;
1012 static int vidioc_enumaudio(
struct file *file,
void *fh,
struct v4l2_audio *a)
1023 static int vidioc_g_audio(
struct file *file,
void *fh,
struct v4l2_audio *a)
1032 static int vidioc_s_audio(
struct file *file,
void *fh,
const struct v4l2_audio *a)
1038 static int vidioc_g_tuner(
struct file *file,
void *fh,
struct v4l2_tuner *
tuner)
1046 if (0 != tuner->
index)
1051 &atv_stat, &cmd_status,
sizeof(atv_stat));
1053 while (atv_stat.sig_lock_busy && count-- && !ret) {
1058 &atv_stat, &cmd_status,
sizeof(atv_stat));
1063 log(
"P:%d,S:%d", atv_stat.sig_present, atv_stat.sig_strength);
1065 if (ret || cmd_status)
1067 else if (atv_stat.sig_present && !atv_stat.sig_strength)
1070 tuner->
signal = (atv_stat.sig_strength * 255 / 10) << 8;
1086 static int pd_vidioc_s_tuner(
struct poseidon *pd,
int index)
1093 audiomode = get_audio_std(pd->
video_data.context.tvnormid);
1102 static int vidioc_s_tuner(
struct file *file,
void *fh,
struct v4l2_tuner *a)
1111 for (index = 0; index < POSEIDON_AUDIOMODS; index++)
1113 return pd_vidioc_s_tuner(pd, index);
1117 static int vidioc_g_frequency(
struct file *file,
void *fh,
1124 if (0 != freq->
tuner)
1136 param = frequency * 62500 / 1000;
1150 static int vidioc_s_frequency(
struct file *file,
void *fh,
1158 pd->pm_suspend = pm_video_suspend;
1159 pd->pm_resume = pm_video_resume;
1161 return set_frequency(pd, freq->
frequency);
1164 static int vidioc_reqbufs(
struct file *file,
void *fh,
1172 static int vidioc_querybuf(
struct file *file,
void *fh,
struct v4l2_buffer *
b)
1179 static int vidioc_qbuf(
struct file *file,
void *fh,
struct v4l2_buffer *
b)
1185 static int vidioc_dqbuf(
struct file *file,
void *fh,
struct v4l2_buffer *
b)
1192 static int usb_transfer_stop(
struct video_data *video)
1221 usb_transfer_stop(video);
1222 free_all_urb(video);
1252 usb_transfer_stop(video);
1254 start_video_stream(pd);
1259 static int vidioc_streamon(
struct file *file,
void *fh,
1270 static int vidioc_streamoff(
struct file *file,
void *fh,
1282 static int pd_video_checkmode(
struct poseidon *pd)
1311 audiomode = get_audio_std(pd->
video_data.context.tvnormid);
1322 static int pm_video_suspend(
struct poseidon *pd)
1325 pm_alsa_suspend(pd);
1337 static int restore_v4l2_context(
struct poseidon *pd,
1342 pd_video_checkmode(pd);
1346 pd_vidioc_s_tuner(pd, context->
audio_idx);
1347 pd_vidioc_s_fmt(pd, &context->
pix);
1348 set_frequency(pd, context->
freq);
1352 static int pm_video_resume(
struct poseidon *pd)
1358 restore_v4l2_context(pd, &video->
context);
1361 if (video->
front->curr_frame)
1362 init_copy(video, 0);
1365 start_video_stream(pd);
1376 if (debug_mode & 0x1)
1378 if (debug_mode & 0x2)
1392 .bytesperline = 720 * 2,
1393 .sizeimage = 720 * 576 * 2,
1399 static int pd_video_open(
struct file *file)
1402 struct poseidon *pd = video_get_drvdata(vfd);
1407 usb_autopm_get_interface(pd->
interface);
1418 ret = pd_video_checkmode(pd);
1462 INIT_LIST_HEAD(&front->
active);
1466 kref_get(&pd->
kref);
1471 usb_autopm_put_interface(pd->
interface);
1476 static int pd_video_release(
struct file *file)
1506 usb_autopm_put_interface(pd->
interface);
1515 static int pd_video_mmap(
struct file *file,
struct vm_area_struct *vma)
1521 static unsigned int pd_video_poll(
struct file *file,
poll_table *
table)
1527 static ssize_t pd_video_read(
struct file *file,
char __user *
buffer,
1528 size_t count, loff_t *ppos)
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,
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,
1554 .vidioc_try_fmt_vid_cap = vidioc_try_fmt,
1590 .name =
"Telegent-Video",
1591 .fops = &pd_video_fops,
1595 .ioctl_ops = &pd_video_ioctl_ops,
1610 video_set_drvdata(vfd, pd);
1621 if (video_is_registered(dev))
1661 log(
"register VIDEO/VBI devices");
1664 log(
"VIDEO/VBI devices register failed, : %d", ret);