ffe55177add4255d68f04c9ed3cd809aaf53b08a
[flashlight-appl] / src / flashlight_lib.c
1 /*
2  *  Flashlight applet (widget) for Maemo.
3  *  Copyright (C) 2009 Roman Moravcik
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include <fcntl.h>
25 #include <unistd.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28 #include <sys/ioctl.h>
29 #include <sys/mman.h>
30
31 #include <asm/types.h>
32 #include <linux/videodev2.h>
33
34 #include "flashlight_lib.h"
35
36
37 int flashlight_get_status (FlashlightContext_t *flashlight, int *status)
38 {
39         struct v4l2_control ctrl;
40
41         printf ("flashlight_get_status()\n");
42
43         if (flashlight == NULL) {
44                 printf ("flashlight_get_status: flashlight context is not valid\n");
45                 return ENOCONTEXT;
46         }
47
48         if (flashlight->fd == -1) {
49                 printf ("flashlight_get_status: device not openned\n");
50                 return ENODEVICE;
51         }
52
53         *status = 0;
54
55         /* check short circuit fault */
56         ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_SCP;
57         if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
58                 printf ("flashlight_set_intensity: cannot get circuit fault status (%s)\n", strerror (errno));
59                 return EGENERROR;
60         }
61
62         if (ctrl.value)
63                 *status |= FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
64         else
65                 *status &= ~FLASHLIGHT_STATUS_SHORT_CIRCUT_FAULT;
66
67         /* check overtemperature fault */
68         ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OT;
69         if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
70                 printf ("flashlight_set_intensity: cannot get overtemperature fault status (%s)\n", strerror (errno));
71                 return EGENERROR;
72         }
73
74         if (ctrl.value)
75                 *status |= FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
76         else
77                 *status &= ~FLASHLIGHT_STATUS_OVERTEMPERATURE_FAULT;
78
79         /* check timeout fault */
80         ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_TMR;
81         if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
82                 printf ("flashlight_set_intensity: cannot get timeout fault status (%s)\n", strerror (errno));
83                 return EGENERROR;
84         }
85
86         if (ctrl.value)
87                 *status |= FLASHLIGHT_STATUS_TIMEOUT_FAULT;
88         else
89                 *status &= ~FLASHLIGHT_STATUS_TIMEOUT_FAULT;
90
91         /* check overtemperature fault */
92         ctrl.id = V4L2_CID_FLASH_ADP1653_FAULT_OV;
93         if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
94                 printf ("flashlight_set_intensity: cannot get overvoltage fault status (%s)\n", strerror (errno));
95                 return EGENERROR;
96         }
97
98         if (ctrl.value)
99                 *status |= FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
100         else
101                 *status &= ~FLASHLIGHT_STATUS_OVERVOLTAGE_FAULT;
102
103         return ENOERROR;
104 }
105
106 int flashlight_set_intensity (FlashlightContext_t *flashlight, int intensity)
107 {
108         struct v4l2_control ctrl;
109         enum v4l2_buf_type type;
110
111         printf ("flashlight_set_intensity(%d)\n", intensity);
112
113         if (flashlight == NULL) {
114                 printf ("flashlight_set_intensity: flashlight context is not valid\n");
115                 return ENOCONTEXT;
116         }
117
118         if (flashlight->fd == -1) {
119                 printf ("flashlight_set_intensity: device not openned\n");
120                 return ENODEVICE;
121         }
122
123         if (intensity > flashlight->max_intensity)
124                 intensity = flashlight->max_intensity;
125
126         ctrl.id = V4L2_CID_TORCH_INTENSITY;
127         ctrl.value = intensity;
128
129         if (ioctl (flashlight->fd, VIDIOC_S_CTRL, &ctrl) == -1) {
130                 printf ("flashlight_set_intensity: cannot set intensity (%s)\n", strerror (errno));
131                 return EGENERROR;
132         }
133
134         /*
135            WORKAROUND: start/stop i/o streaming to block camera application
136          */
137         if (intensity > 0) {
138                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
139                 if (ioctl (flashlight->fd, VIDIOC_STREAMON, &type)) {
140                         printf ("flashlight_set_intensity: unable to start i/o streaming (%s)\n", strerror (errno));
141                         return EGENERROR;
142                 }
143         } else {
144                 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
145                 if (ioctl (flashlight->fd, VIDIOC_STREAMOFF, &type) == -1) {
146                         printf ("flashlight_set_intensity: unable to stop i/o streaming (%s)\n", strerror (errno));
147                         return EGENERROR;
148                 }
149         }
150
151         return ENOERROR;
152 }
153
154 int flashlight_get_intensity (FlashlightContext_t *flashlight, int *intensity)
155 {
156         struct v4l2_control ctrl;
157
158         printf ("flashlight_get_intensity()\n");
159
160         if (flashlight == NULL) {
161                 printf ("flashlight_get_intensity: flashlight context is not valid\n");
162                 return ENOCONTEXT;
163         }
164
165         if (flashlight->fd == -1) {
166                 printf ("flashlight_get_intensity: device not openned\n");
167                 return ENODEVICE;
168         }
169
170         ctrl.id = V4L2_CID_TORCH_INTENSITY;
171
172         if (ioctl (flashlight->fd, VIDIOC_G_CTRL, &ctrl) == -1) {
173                 printf ("flashlight_get_intensity: cannot get intensity (%s)\n", strerror (errno));
174                 return EGENERROR;
175         }
176
177         *intensity = ctrl.value;
178         return ENOERROR;
179 }
180
181 int flashlight_open (FlashlightContext_t *flashlight, const char *device_name)
182 {
183         struct v4l2_queryctrl ctrl;
184         struct v4l2_cropcap cropcap;
185         struct v4l2_crop crop;
186         struct v4l2_format fmt;
187         struct v4l2_requestbuffers req;
188         struct stat st;
189
190         printf ("flashlight_open(%s)\n", device_name);
191
192         if (flashlight == NULL) {
193                 printf ("flashlight_open: flashlight context is not valid\n");
194                 return ENOCONTEXT;
195         }
196
197         if (device_name == NULL) {
198                 printf ("flashlight_open: device name not specified\n");
199                 return EGENERROR;
200         }
201
202         memcpy (flashlight->device_name, device_name, sizeof(flashlight->device_name));
203
204         if (stat (flashlight->device_name, &st) == -1) {
205                 printf ("flashlight_open: cannot identify '%s' (%s)\n", flashlight->device_name, strerror (errno));
206                 return EGENERROR;
207         }
208
209         /* check it device_name is real device */
210         if (!S_ISCHR (st.st_mode)) {
211                 printf ("flashlight_open: %s is no device\n", flashlight->device_name);
212                 return EGENERROR;
213         }
214
215         flashlight->fd = open (flashlight->device_name, O_RDWR /* required */ | O_NONBLOCK, 0);
216
217         if (flashlight->fd == -1) {
218                 printf ("flashlight_open: cannot open '%s' (%s)\n", flashlight->device_name, strerror (errno));
219                 return ENODEVICE;
220         }
221
222         /* query from driver minimal and maximal flashlight intensity */
223         ctrl.id = V4L2_CID_TORCH_INTENSITY;
224         if (ioctl (flashlight->fd, VIDIOC_QUERYCTRL, &ctrl) == -1) {
225                 printf ("flashlight_open: cannot get minimal and maximal flashlight intensity (%s)\n", strerror (errno));
226                 return EGENERROR;
227         }
228
229         flashlight->min_intensity = ctrl.minimum;
230         flashlight->max_intensity = ctrl.maximum;
231
232         /*
233            WORKAROUND: Initialization of camera extracted from v4l2_example.
234            http://v4l2spec.bytesex.org/spec/capture-example.html
235
236            We need to initialize camera in other to block camera application.
237            (bug 4949: Applet breaks the camera application)
238          */
239         /* get crop capabilities */
240         cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
241         if (ioctl (flashlight->fd, VIDIOC_CROPCAP, &cropcap) == -1) {
242                 printf ("flashlight_open: unable to get crop capabilities (%s)\n", strerror (errno));
243                 return EGENERROR;
244         }
245
246         /* set crop capabilities */
247         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
248         crop.c = cropcap.defrect; /* reset to default */
249         if (ioctl (flashlight->fd, VIDIOC_S_CROP, &crop) == -1) {
250                 printf ("flashlight_open: unable to set cropping rectangle (%s)\n", strerror (errno));
251                 return EGENERROR;
252         }
253
254         /* set data format */
255         fmt.type                = V4L2_BUF_TYPE_VIDEO_CAPTURE;
256         fmt.fmt.pix.width       = 640;
257         fmt.fmt.pix.height      = 480;
258         fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
259         fmt.fmt.pix.field       = V4L2_FIELD_INTERLACED;
260         if (ioctl (flashlight->fd, VIDIOC_S_FMT, &fmt) == -1) {
261                 printf ("flashlight_open: unable to set data format (%s)\n", strerror (errno));
262                 return EGENERROR;
263         }
264
265         req.count       = 4;
266         req.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
267         req.memory      = V4L2_MEMORY_MMAP;
268         if (ioctl (flashlight->fd, VIDIOC_REQBUFS, &req) == -1) {
269                 printf ("flashlight_open: unable to initiate memory mapping (%s)\n", strerror (errno));
270                 return EGENERROR;
271         }
272
273         if (req.count < 2) {
274                 printf ("flashlight_open: insufficient buffer memory on %s\n", device_name);
275                 return EGENERROR;
276         }
277
278         flashlight->buffers = calloc (req.count, sizeof (*flashlight->buffers));
279         if (!flashlight->buffers) {
280                 printf ("flashlight_open: unable to allocate memory\n");
281                 return EGENERROR;
282         }
283
284         for (flashlight->n_buffers = 0; flashlight->n_buffers < req.count; ++flashlight->n_buffers) {
285                 struct v4l2_buffer buf;
286
287                 buf.type        = V4L2_BUF_TYPE_VIDEO_CAPTURE;
288                 buf.memory      = V4L2_MEMORY_MMAP;
289                 buf.index       = flashlight->n_buffers;
290
291                 if (ioctl (flashlight->fd, VIDIOC_QUERYBUF, &buf) == -1) {
292                         printf ("flashlight_open: unable to query the status of a buffer %d (%s)\n",
293                                 flashlight->n_buffers, strerror (errno));
294                         return EGENERROR;
295                 }
296
297                 flashlight->buffers[flashlight->n_buffers].length = buf.length;
298                 flashlight->buffers[flashlight->n_buffers].start = mmap (NULL /* start anywhere */,
299                                                                          buf.length,
300                                                                          PROT_READ | PROT_WRITE /* required */,
301                                                                          MAP_SHARED /* recommended */,
302                                                                          flashlight->fd,
303                                                                          buf.m.offset);
304
305                 if (flashlight->buffers[flashlight->n_buffers].start == MAP_FAILED) {
306                         printf ("flashlight_open: unable to map memory (%s)\n", strerror (errno));
307                         return EGENERROR;
308                 }
309         }
310
311         return ENOERROR;
312 }
313
314 int flashlight_close (FlashlightContext_t *flashlight)
315 {
316         unsigned int i;
317
318         printf ("flashlight_close()\n");
319
320         if (flashlight == NULL) {
321                 printf ("flashlight_close: flashlight context is not valid\n");
322                 return ENOCONTEXT;
323         }
324
325         /* unmap memory mapped buffers */
326         for (i = 0; i < flashlight->n_buffers; ++i) {
327                 if (flashlight->buffers[flashlight->n_buffers].start != MAP_FAILED) {
328                         if (munmap (flashlight->buffers[i].start, flashlight->buffers[i].length) == -1) {
329                                 printf ("flashlight_close: unable to unmap memory (%s)\n", strerror (errno));
330                                 return EGENERROR;
331                         }
332                 }
333         }
334         flashlight->n_buffers = 0;
335
336         /* free buffers */
337         if (flashlight->buffers)
338                 free (flashlight->buffers);
339         flashlight->buffers = NULL;
340
341         /* close camera device */
342         if (flashlight->fd != -1) {
343                 if (close (flashlight->fd) == -1) {
344                         printf ("flashlight_close: cannot close device '%s' (%s)\n", flashlight->device_name, strerror (errno));
345                         return ENODEVICE;
346                 }
347         }
348         flashlight->fd = -1;
349
350         return ENOERROR;
351 }
352
353 int flashlight_init (FlashlightContext_t **pRefContext)
354 {
355         FlashlightContext_t *flashlight = NULL;
356
357         printf ("flashlight_init()\n");
358
359         if (*pRefContext != NULL) {
360                 printf("flashlight_init: expecting zero pointer context '*pRefContext'\n");
361                 return EGENERROR;
362         }
363
364         /* allocate memory for context structure */
365         flashlight = malloc (sizeof (FlashlightContext_t));
366         if (flashlight == NULL) {
367                 printf ("flashlight_init: unable to allocate memory for context\n");
368                 return ENOCONTEXT;
369         }
370
371         *pRefContext = flashlight;
372
373         /* initialize default values */
374         memset (flashlight, 0x00, sizeof (FlashlightContext_t));
375         flashlight->fd = -1;
376
377         flashlight->n_buffers = 0;
378         flashlight->buffers = NULL;
379
380         /* from adp1653.c */
381         flashlight->min_intensity = 0;
382         flashlight->max_intensity = 11;
383
384         return ENOERROR;
385 }
386
387 int flashlight_deinit (FlashlightContext_t *flashlight)
388 {
389         int intensity = 0;
390
391         printf ("flashlight_deinit()\n");
392
393         if (flashlight == NULL) {
394                 printf ("flashlight_deinit: flashlight context is not valid\n");
395                 return ENOCONTEXT;
396         }
397
398         if (flashlight->fd != -1) {
399                 /* check if flashlight isn't enabled before closing device */
400                 if (flashlight_get_intensity (flashlight, &intensity) == -1)
401                         return EGENERROR;
402
403                 if (intensity > 0) {
404                         if (flashlight_set_intensity (flashlight, 0) == -1)
405                                 return EGENERROR;
406                 }
407
408                 if (flashlight_close (flashlight))
409                         return EGENERROR;
410         }
411
412         /* free allocated memory */
413         free (flashlight);
414
415         return ENOERROR;
416 }