2 * Flashlight applet (widget) for Maemo.
3 * Copyright (C) 2009 Roman Moravcik
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <sys/ioctl.h>
30 #include <asm/types.h>
31 #include <linux/videodev2.h>
33 #include "flashlight_lib.h"
35 int flashlight_get_status (FlashlightContext_t *flashlight, int *status)
37 struct v4l2_control ctrl;
39 if (flashlight == NULL) {
40 printf ("flashlight_get_status: flashlight context is not valid\n");
44 if (flashlight->fd == -1) {
45 printf ("flashlight_get_status: device not openned\n");
51 /* check short circuit fault */
52 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_SCP;
53 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
54 printf ("flashlight_set_intensity: cannot get circuit fault status (%s)\n", strerror (errno));
59 *status |= FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
61 *status &= ~FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
63 /* check overtemperature fault */
64 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OT;
65 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
66 printf ("flashlight_set_intensity: cannot get overtemperature fault status (%s)\n", strerror (errno));
71 *status |= FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
73 *status &= ~FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
75 /* check timeout fault */
76 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_TMR;
77 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
78 printf ("flashlight_set_intensity: cannot get timeout fault status (%s)\n", strerror (errno));
83 *status |= FLASHLIGHT_STATUS_TIMEOUT_FAULT;
85 *status &= ~FLASHLIGHT_STATUS_TIMEOUT_FAULT;
87 /* check overtemperature fault */
88 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OV;
89 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
90 printf ("flashlight_set_intensity: cannot get overvoltage fault status (%s)\n", strerror (errno));
95 *status |= FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
97 *status &= ~FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
102 int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
104 struct v4l2_control ctrl;
106 if (flashlight == NULL) {
107 printf ("flashlight_set_intensity: flashlight context is not valid\n");
111 if (flashlight->fd == -1) {
112 printf ("flashlight_set_intensity: device not openned\n");
116 if (intensity > flashlight->max_intensity)
117 intensity = flashlight->max_intensity;
119 ctrl.id = V4L2_CID_TORCH_INTENSITY;
120 ctrl.value = intensity;
122 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
123 printf ("flashlight_set_intensity: cannot set intensity (%s)\n", strerror (errno));
130 int flashlight_get_intensity (FlashlightContext_t *flashlight, int *intensity)
132 struct v4l2_control ctrl;
134 if (flashlight == NULL) {
135 printf ("flashlight_get_intensity: flashlight context is not valid\n");
139 if (flashlight->fd == -1) {
140 printf ("flashlight_get_intensity: device not openned\n");
144 ctrl.id = V4L2_CID_TORCH_INTENSITY;
146 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
147 printf ("flashlight_get_intensity: cannot get intensity (%s)\n", strerror (errno));
151 *intensity = ctrl.value;
155 int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
157 struct v4l2_queryctrl ctrl;
160 if (flashlight == NULL) {
161 printf ("flashlight_open: flashlight context is not valid\n");
165 if (device_name == NULL) {
166 printf ("flashlight_open: device name not specified\n");
170 memcpy (flashlight->device_name, device_name, sizeof(flashlight->device_name));
172 if (stat (flashlight->device_name, &st) == -1) {
173 printf ("flashlight_open: cannot identify '%s' (%s)\n", flashlight->device_name, strerror (errno));
177 /* check it device_name is real device */
178 if (!S_ISCHR (st.st_mode)) {
179 printf ("flashlight_open: %s is no device\n", flashlight->device_name);
183 flashlight->fd = open (flashlight->device_name, O_RDWR /* required */ | O_NONBLOCK, 0);
185 if (flashlight->fd == -1) {
186 printf ("flashlight_open: cannot open '%s' (%s)\n", flashlight->device_name, strerror (errno));
190 /* query from driver minimal and maximal flashlight intensity */
191 ctrl.id = V4L2_CID_TORCH_INTENSITY;
192 if (ioctl (flashlight->fd, VIDIOC_QUERYCTRL, &ctrl) == -1) {
193 printf ("flashlight_open: cannot get minimal and maximal flashlight intensity (%s)\n", strerror (errno));
197 flashlight->min_intensity = ctrl.minimum;
198 flashlight->max_intensity = ctrl.maximum;
203 int flashlight_close (FlashlightContext_t *flashlight)
205 if (flashlight == NULL) {
206 printf ("flashlight_close: flashlight context is not valid\n");
210 if (flashlight->fd != -1) {
211 if (close (flashlight->fd) == -1) {
212 printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
221 int flashlight_init (FlashlightContext_t **pRefContext)
223 FlashlightContext_t *flashlight = NULL;
225 if (*pRefContext != NULL) {
226 printf("flashlight_init: expecting zero pointer context '*pRefContext'\n");
230 /* allocate memory for context structure */
231 flashlight = malloc (sizeof (FlashlightContext_t));
232 if (flashlight == NULL) {
233 printf ("flashlight_init: unable to allocate memory for context\n");
237 *pRefContext = flashlight;
239 /* initialize default values */
240 memset (flashlight, 0x00, sizeof (FlashlightContext_t));
244 flashlight->min_intensity = 0;
245 flashlight->max_intensity = 11;
250 int flashlight_deinit (FlashlightContext_t *flashlight)
254 if (flashlight == NULL) {
255 printf ("flashlight_deinit: flashlight context is not valid\n");
259 if (flashlight->fd != -1) {
260 /* check if flashlight isn't enabled before closing device */
261 if (flashlight_get_intensity (flashlight, &intensity) == -1)
265 if (flashlight_set_intensity (flashlight, 0) == -1)
269 if (flashlight_close(flashlight))
273 /* free allocated memory */