• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * v4l-test: Test environment for Video For Linux Two API
3  *
4  *  3 Apr 2009  0.2  Added camera enabling through
5  *                   /sys/devices/platform/eeepc/camera
6  * 18 Dec 2008  0.1  First release
7  *
8  * Written by M�rton N�meth <nm127@freemail.hu>
9  * Released under GPL
10  */
11 
12 #include <stdio.h>
13 #include <sys/types.h>
14 #include <sys/stat.h>
15 #include <fcntl.h>
16 #include <unistd.h>
17 #include <sys/ioctl.h>
18 #include <errno.h>
19 #include <string.h>
20 #include <unistd.h>
21 
22 #include "dev_video.h"
23 
24 #define DEV_VIDEO_STATE_NORMAL		0
25 #define DEV_VIDEO_STATE_ASUS		1
26 #define DEV_VIDEO_STATE_EEEPC		2
27 
28 static int f;
29 static int dev_video_state;
30 
try_ASUS_camera(char * path,char * value)31 static int try_ASUS_camera(char *path, char *value)
32 {
33 	int ASUS_camera;
34 	size_t s;
35 	int ret;
36 
37 	ASUS_camera = open(path, O_WRONLY);
38 	if (ASUS_camera < 0) {
39 		fprintf(stderr, "Cannot open %s: %s\n", path, strerror(errno));
40 		return ASUS_camera;
41 	}
42 	s = write(ASUS_camera, value, 1);
43 	ret = close(ASUS_camera);
44 	if (s != 1) {
45 		return -1;
46 	}
47 	if (ret < 0) {
48 		perror("Cannot close ASUS camera");
49 		return ret;
50 	}
51 
52 	sleep(1);
53 
54 	f = open("/dev/video0", O_RDWR);
55 	if (f < 0) {
56 		perror("Cannot open /dev/video0");
57 		return f;
58 	}
59 	dev_video_state = DEV_VIDEO_STATE_ASUS;
60 
61 	return f;
62 }
63 
open_video()64 int open_video()
65 {
66 	int error = 0;
67 
68 	fflush(stdout);
69 
70 	f = open("/dev/video0", O_RDWR);
71 	if (f < 0) {
72 		perror("Cannot open /dev/video0");
73 		fprintf(stderr, "Retrying with ASUS camera...\n");
74 		f = try_ASUS_camera("/proc/acpi/asus/camera", "1");
75 		if (f < 0) {
76 			f = try_ASUS_camera
77 			    ("/sys/devices/platform/eeepc/camera", "1");
78 			if (f < 0) {
79 				error = 1;
80 			} else {
81 				dev_video_state = DEV_VIDEO_STATE_EEEPC;
82 			}
83 		} else {
84 			dev_video_state = DEV_VIDEO_STATE_ASUS;
85 		}
86 	} else {
87 		dev_video_state = DEV_VIDEO_STATE_NORMAL;
88 	}
89 	return error;
90 }
91 
close_video()92 int close_video()
93 {
94 	int ret;
95 
96 	fflush(stdout);
97 
98 	ret = close(f);
99 	f = 0;
100 	if (ret < 0) {
101 		perror("Cannot close");
102 		return 1;
103 	}
104 	switch (dev_video_state) {
105 	case DEV_VIDEO_STATE_NORMAL:
106 		break;
107 	case DEV_VIDEO_STATE_ASUS:
108 		try_ASUS_camera("/proc/acpi/asus/camera", "0");
109 		break;
110 	case DEV_VIDEO_STATE_EEEPC:
111 		try_ASUS_camera("/sys/devices/platform/eeepc/camera", "0");
112 		break;
113 	}
114 
115 	return 0;
116 }
117 
get_video_fd()118 int get_video_fd()
119 {
120 	return f;
121 }
122