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 printf ("flashlight_get_status()\n");
41 if (flashlight == NULL) {
42 printf ("flashlight_get_status: flashlight context is not valid\n");
46 if (flashlight->fd == -1) {
47 printf ("flashlight_get_status: device not openned\n");
53 /* check short circuit fault */
54 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_SCP;
55 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
56 printf ("flashlight_set_intensity: cannot get circuit fault status (%s)\n", strerror (errno));
61 *status |= FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
63 *status &= ~FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
65 /* check overtemperature fault */
66 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OT;
67 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
68 printf ("flashlight_set_intensity: cannot get overtemperature fault status (%s)\n", strerror (errno));
73 *status |= FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
75 *status &= ~FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
77 /* check timeout fault */
78 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_TMR;
79 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
80 printf ("flashlight_set_intensity: cannot get timeout fault status (%s)\n", strerror (errno));
85 *status |= FLASHLIGHT_STATUS_TIMEOUT_FAULT;
87 *status &= ~FLASHLIGHT_STATUS_TIMEOUT_FAULT;
89 /* check overtemperature fault */
90 ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OV;
91 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
92 printf ("flashlight_set_intensity: cannot get overvoltage fault status (%s)\n", strerror (errno));
97 *status |= FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
99 *status &= ~FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
104 int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
106 struct v4l2_control ctrl;
108 printf ("flashlight_set_intensity(%d)\n", intensity);
110 if (flashlight == NULL) {
111 printf ("flashlight_set_intensity: flashlight context is not valid\n");
115 if (flashlight->fd == -1) {
116 printf ("flashlight_set_intensity: device not openned\n");
120 if (intensity > flashlight->max_intensity)
121 intensity = flashlight->max_intensity;
123 ctrl.id = V4L2_CID_TORCH_INTENSITY;
124 ctrl.value = intensity;
126 if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
127 printf ("flashlight_set_intensity: cannot set intensity (%s)\n", strerror (errno));
134 int flashlight_get_intensity (FlashlightContext_t *flashlight, int *intensity)
136 struct v4l2_control ctrl;
138 printf ("flashlight_get_intensity()\n");
140 if (flashlight == NULL) {
141 printf ("flashlight_get_intensity: flashlight context is not valid\n");
145 if (flashlight->fd == -1) {
146 printf ("flashlight_get_intensity: device not openned\n");
150 ctrl.id = V4L2_CID_TORCH_INTENSITY;
152 if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
153 printf ("flashlight_get_intensity: cannot get intensity (%s)\n", strerror (errno));
157 *intensity = ctrl.value;
161 int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
163 struct v4l2_queryctrl ctrl;
166 printf ("flashlight_open(%s)\n", device_name);
168 if (flashlight == NULL) {
169 printf ("flashlight_open: flashlight context is not valid\n");
173 if (device_name == NULL) {
174 printf ("flashlight_open: device name not specified\n");
178 memcpy (flashlight->device_name, device_name, sizeof(flashlight->device_name));
180 if (stat (flashlight->device_name, &st) == -1) {
181 printf ("flashlight_open: cannot identify '%s' (%s)\n", flashlight->device_name, strerror (errno));
185 /* check it device_name is real device */
186 if (!S_ISCHR (st.st_mode)) {
187 printf ("flashlight_open: %s is no device\n", flashlight->device_name);
191 flashlight->fd = open (flashlight->device_name, O_RDWR /* required */ | O_NONBLOCK, 0);
193 if (flashlight->fd == -1) {
194 printf ("flashlight_open: cannot open '%s' (%s)\n", flashlight->device_name, strerror (errno));
198 /* query from driver minimal and maximal flashlight intensity */
199 ctrl.id = V4L2_CID_TORCH_INTENSITY;
200 if (ioctl (flashlight->fd, VIDIOC_QUERYCTRL, &ctrl) == -1) {
201 printf ("flashlight_open: cannot get minimal and maximal flashlight intensity (%s)\n", strerror (errno));
205 flashlight->min_intensity = ctrl.minimum;
206 flashlight->max_intensity = ctrl.maximum;
211 int flashlight_close (FlashlightContext_t *flashlight)
213 printf ("flashlight_close()\n");
215 if (flashlight == NULL) {
216 printf ("flashlight_close: flashlight context is not valid\n");
220 if (flashlight->fd != -1) {
221 if (close (flashlight->fd) == -1) {
222 printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
231 int flashlight_init (FlashlightContext_t **pRefContext)
233 FlashlightContext_t *flashlight = NULL;
235 printf ("flashlight_init()\n");
237 if (*pRefContext != NULL) {
238 printf("flashlight_init: expecting zero pointer context '*pRefContext'\n");
242 /* allocate memory for context structure */
243 flashlight = malloc (sizeof (FlashlightContext_t));
244 if (flashlight == NULL) {
245 printf ("flashlight_init: unable to allocate memory for context\n");
249 *pRefContext = flashlight;
251 /* initialize default values */
252 memset (flashlight, 0x00, sizeof (FlashlightContext_t));
256 flashlight->min_intensity = 0;
257 flashlight->max_intensity = 11;
262 int flashlight_deinit (FlashlightContext_t *flashlight)
266 printf ("flashlight_deinit()\n");
268 if (flashlight == NULL) {
269 printf ("flashlight_deinit: flashlight context is not valid\n");
273 if (flashlight->fd != -1) {
274 /* check if flashlight isn't enabled before closing device */
275 if (flashlight_get_intensity (flashlight, &intensity) == -1)
279 if (flashlight_set_intensity (flashlight, 0) == -1)
283 if (flashlight_close(flashlight))
287 /* free allocated memory */