18 #include <linux/module.h>
20 #include <linux/version.h>
22 #include <linux/sched.h>
24 #include <linux/slab.h>
27 #include <linux/time.h>
30 #include <linux/videodev2.h>
34 #include <linux/i2c.h>
43 #ifndef V4L2_MPEG_STREAM_TYPE_MPEG_ELEM
44 #define V4L2_MPEG_STREAM_TYPE_MPEG_ELEM 6
46 #ifndef V4L2_MPEG_VIDEO_ENCODING_MPEG_4
47 #define V4L2_MPEG_VIDEO_ENCODING_MPEG_4 3
50 #define call_all(dev, o, f, args...) \
51 v4l2_device_call_until_err(dev, 0, o, f, ##args)
57 if (gobuf->
state != BUF_STATE_IDLE) {
59 gobuf->
state = BUF_STATE_IDLE;
68 static void abort_queued(
struct go7007 *
go)
73 deactivate_buffer(gobuf);
77 static int go7007_streamoff(
struct go7007 *
go)
87 spin_unlock_irqrestore(&go->
spinlock, flags);
94 static int go7007_open(
struct file *
file)
99 if (go->
status != STATUS_ONLINE)
112 static int go7007_release(
struct file *file)
118 go7007_streamoff(go);
167 int sensor_height = 0, sensor_width = 0;
186 sensor_height = go->
board_info->sensor_height;
191 width = sensor_width;
192 height = sensor_height;
194 if (fmt->
fmt.
pix.width > sensor_width)
195 width = sensor_width;
196 else if (fmt->
fmt.
pix.width < 144)
199 width = fmt->
fmt.
pix.width & ~0x0f;
201 if (fmt->
fmt.
pix.height > sensor_height)
202 height = sensor_height;
203 else if (fmt->
fmt.
pix.height < 96)
206 height = fmt->
fmt.
pix.height & ~0x0f;
208 int requested_size = fmt->
fmt.
pix.width * fmt->
fmt.
pix.height;
209 int sensor_size = sensor_width * sensor_height;
211 if (64 * requested_size < 9 * sensor_size) {
212 width = sensor_width / 4;
213 height = sensor_height / 4;
214 }
else if (64 * requested_size < 36 * sensor_size) {
215 width = sensor_width / 2;
216 height = sensor_height / 2;
218 width = sensor_width;
219 height = sensor_height;
228 memset(fmt, 0,
sizeof(*fmt));
234 fmt->
fmt.
pix.bytesperline = 0;
246 for (i = 0; i < 4; ++
i)
247 go->
modet[i].enable = 0;
248 for (i = 0; i < 1624; ++
i)
252 struct v4l2_mbus_framefmt mbus_fmt;
256 mbus_fmt.width = fmt->
fmt.
pix.width;
258 mbus_fmt.width =
width;
260 if (height > sensor_height / 2) {
261 mbus_fmt.height = height / 2;
269 if (width <= sensor_width / 4) {
273 }
else if (width <= sensor_width / 2) {
287 switch (fmt->
fmt.
pix.pixelformat) {
336 static int clip_to_modet_map(
struct go7007 *go,
int region,
344 clip_ptr = clip_list;
348 if (clip.c.left < 0 || (clip.c.left & 0xF) ||
349 clip.c.width <= 0 || (clip.c.width & 0xF))
351 if (clip.c.left + clip.c.width > go->
width)
353 if (clip.c.top < 0 || (clip.c.top & 0xF) ||
354 clip.c.height <= 0 || (clip.c.height & 0xF))
356 if (clip.c.top + clip.c.height > go->
height)
358 for (y = 0; y < clip.c.height; y += 16)
359 for (x = 0; x < clip.c.width; x += 16) {
360 mbnum = (go->
width >> 4) *
361 ((clip.c.top + y) >> 4) +
362 ((clip.c.left + x) >> 4);
367 clip_ptr = clip.
next;
371 for (mbnum = 0; mbnum < 1624; ++mbnum)
376 clip_ptr = clip_list;
380 for (y = 0; y < clip.c.height; y += 16)
381 for (x = 0; x < clip.c.width; x += 16) {
382 mbnum = (go->
width >> 4) *
383 ((clip.c.top + y) >> 4) +
384 ((clip.c.left + x) >> 4);
387 clip_ptr = clip.
next;
395 static const u32 mpeg_ctrls[] = {
405 static const u32 *ctrl_classes[] = {
453 switch (ctrl->
value) {
473 switch (ctrl->
value) {
507 switch (ctrl->
value) {
534 if (ctrl->
value < 64000 || ctrl->
value > 10000000)
598 static int vidioc_querycap(
struct file *file,
void *
priv,
609 cap->
version = KERNEL_VERSION(0, 9, 8);
620 static int vidioc_enum_fmt_vid_cap(
struct file *file,
void *priv,
625 switch (fmt->
index) {
628 desc =
"Motion-JPEG";
632 desc =
"MPEG1/MPEG2/MPEG4";
645 static int vidioc_g_fmt_vid_cap(
struct file *file,
void *priv,
656 fmt->
fmt.
pix.bytesperline = 0;
663 static int vidioc_try_fmt_vid_cap(
struct file *file,
void *priv,
668 return set_capture_size(go, fmt, 1);
671 static int vidioc_s_fmt_vid_cap(
struct file *file,
void *priv,
679 return set_capture_size(go, fmt, 0);
682 static int vidioc_reqbufs(
struct file *file,
void *priv,
699 if (gofh->
bufs[i].mapped > 0)
700 goto unlock_and_return;
705 goto unlock_and_return;
724 goto unlock_and_return;
727 for (i = 0; i <
count; ++
i) {
730 gofh->
bufs[
i].state = BUF_STATE_IDLE;
731 gofh->
bufs[
i].mapped = 0;
743 memset(req, 0,
sizeof(*req));
756 static int vidioc_querybuf(
struct file *file,
void *priv,
770 goto unlock_and_return;
772 memset(buf, 0,
sizeof(*buf));
776 switch (gofh->
bufs[index].state) {
777 case BUF_STATE_QUEUED:
787 if (gofh->
bufs[index].mapped)
801 static int vidioc_qbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
816 goto unlock_and_return;
820 goto unlock_and_return;
824 goto unlock_and_return;
845 for (i = 0; i <
ret; ++
i)
848 goto unlock_and_return;
854 spin_unlock_irqrestore(&go->
spinlock, flags);
865 static int vidioc_dqbuf(
struct file *file,
void *priv,
struct v4l2_buffer *buf)
881 if (list_empty(&go->
stream))
882 goto unlock_and_return;
903 goto unlock_and_return;
906 deactivate_buffer(gobuf);
907 spin_unlock_irqrestore(&go->
spinlock, flags);
908 frame_type_flag = get_frame_type_flag(gobuf, go->
format);
911 memset(buf, 0,
sizeof(*buf));
932 static int vidioc_streamon(
struct file *file,
void *priv,
960 static int vidioc_streamoff(
struct file *file,
void *priv,
969 go7007_streamoff(go);
975 static int vidioc_queryctrl(
struct file *file,
void *priv,
985 return mpeg_query_ctrl(query);
988 static int vidioc_g_ctrl(
struct file *file,
void *priv,
996 return mpeg_g_ctrl(ctrl, go);
999 static int vidioc_s_ctrl(
struct file *file,
void *priv,
1007 return mpeg_s_ctrl(ctrl, go);
1010 static int vidioc_g_parm(
struct file *filp,
void *priv,
1028 static int vidioc_s_parm(
struct file *filp,
void *priv,
1041 d = 1001 * parm->
parm.
capture.timeperframe.denominator;
1042 if (n != 0 && d != 0 && n > d)
1060 static int vidioc_enum_framesizes(
struct file *filp,
void *priv,
1070 if (fsize->
index > 0)
1080 static int vidioc_enum_frameintervals(
struct file *filp,
void *priv,
1090 if (fival->
index > 0)
1100 static int vidioc_g_std(
struct file *file,
void *priv,
v4l2_std_id *
std)
1105 case GO7007_STD_NTSC:
1108 case GO7007_STD_PAL:
1118 static int vidioc_s_std(
struct file *file,
void *priv,
v4l2_std_id *std)
1152 set_capture_size(go,
NULL, 0);
1157 static int vidioc_querystd(
struct file *file,
void *priv,
v4l2_std_id *std)
1174 static int vidioc_enum_input(
struct file *file,
void *priv,
1195 inp->
std = V4L2_STD_NTSC | V4L2_STD_PAL |
1204 static int vidioc_g_input(
struct file *file,
void *priv,
unsigned int *
input)
1213 static int vidioc_s_input(
struct file *file,
void *priv,
unsigned int input)
1227 static int vidioc_g_tuner(
struct file *file,
void *priv,
1242 static int vidioc_s_tuner(
struct file *file,
void *priv,
1266 static int vidioc_g_frequency(
struct file *file,
void *priv,
1281 static int vidioc_s_frequency(
struct file *file,
void *priv,
1294 static int vidioc_cropcap(
struct file *file,
void *priv,
1304 case GO7007_STD_NTSC:
1306 cropcap->
bounds.left = 0;
1307 cropcap->
bounds.width = 720;
1308 cropcap->
bounds.height = 480;
1312 cropcap->
defrect.height = 480;
1314 case GO7007_STD_PAL:
1316 cropcap->
bounds.left = 0;
1317 cropcap->
bounds.width = 720;
1318 cropcap->
bounds.height = 576;
1322 cropcap->
defrect.height = 576;
1324 case GO7007_STD_OTHER:
1326 cropcap->
bounds.left = 0;
1339 static int vidioc_g_crop(
struct file *file,
void *priv,
struct v4l2_crop *crop)
1350 case GO7007_STD_NTSC:
1353 crop->
c.width = 720;
1354 crop->
c.height = 480;
1356 case GO7007_STD_PAL:
1359 crop->
c.width = 720;
1360 crop->
c.height = 576;
1362 case GO7007_STD_OTHER:
1375 static int vidioc_s_crop(
struct file *file,
void *priv,
const struct v4l2_crop *crop)
1383 static int vidioc_g_jpegcomp(
struct file *file,
void *priv,
1386 memset(params, 0,
sizeof(*params));
1394 static int vidioc_s_jpegcomp(
struct file *file,
void *priv,
1421 if (*bitrate < 64000 || *bitrate > 10000000)
1475 memset(comp, 0,
sizeof(*comp));
1480 if (go->
standard == GO7007_STD_NTSC)
1488 if (go->
standard == GO7007_STD_NTSC)
1532 if (mpeg->
pali >> 24 == 2)
1539 if (mpeg->
pali >> 24 == 4)
1562 memset(mpeg, 0,
sizeof(*mpeg));
1608 int region = mdp->
region;
1614 if (!go->
modet[region].enable)
1630 return clip_to_modet_map(go, region->
region, region->
clips);
1634 static ssize_t go7007_read(
struct file *file,
char __user *
data,
1635 size_t count, loff_t *ppos)
1650 unsigned long flags;
1652 if (--gobuf->
mapped == 0) {
1654 deactivate_buffer(gobuf);
1655 spin_unlock_irqrestore(&gobuf->
go->spinlock, flags);
1660 static int go7007_vm_fault(
struct vm_area_struct *vma,
struct vm_fault *vmf)
1666 return VM_FAULT_OOM;
1672 static struct vm_operations_struct go7007_vm_ops = {
1673 .open = go7007_vm_open,
1674 .close = go7007_vm_close,
1675 .fault = go7007_vm_fault,
1678 static int go7007_mmap(
struct file *file,
struct vm_area_struct *vma)
1683 if (gofh->
go->status != STATUS_ONLINE)
1699 if (gofh->
bufs[index].mapped > 0) {
1705 vma->
vm_ops = &go7007_vm_ops;
1713 static unsigned int go7007_poll(
struct file *file,
poll_table *
wait)
1718 if (list_empty(&gofh->
go->stream))
1721 poll_wait(file, &gofh->
go->frame_waitq, wait);
1727 static void go7007_vfl_release(
struct video_device *vfd)
1729 struct go7007 *go = video_get_drvdata(vfd);
1738 .open = go7007_open,
1739 .release = go7007_release,
1741 .read = go7007_read,
1742 .mmap = go7007_mmap,
1743 .poll = go7007_poll,
1784 .fops = &go7007_fops,
1785 .
release = go7007_vfl_release,
1786 .ioctl_ops = &video_ioctl_ops,
1822 unsigned long flags;
1830 spin_unlock_irqrestore(&go->
spinlock, flags);