Home | History | Annotate | Download | only in src
      1 /*
      2 Copyright (c) 2012, The Linux Foundation. All rights reserved.
      3 
      4 Redistribution and use in source and binary forms, with or without
      5 modification, are permitted provided that the following conditions are
      6 met:
      7     * Redistributions of source code must retain the above copyright
      8       notice, this list of conditions and the following disclaimer.
      9     * Redistributions in binary form must reproduce the above
     10       copyright notice, this list of conditions and the following
     11       disclaimer in the documentation and/or other materials provided
     12       with the distribution.
     13     * Neither the name of The Linux Foundation nor the names of its
     14       contributors may be used to endorse or promote products derived
     15       from this software without specific prior written permission.
     16 
     17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
     18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
     20 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
     21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
     24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28 */
     29 
     30 #include <pthread.h>
     31 #include <errno.h>
     32 #include <sys/ioctl.h>
     33 #include <sys/types.h>
     34 #include <sys/stat.h>
     35 #include <fcntl.h>
     36 #include <camera.h>
     37 #include <semaphore.h>
     38 
     39 #include "mm_camera_dbg.h"
     40 #include "mm_camera_interface.h"
     41 #include "mm_camera.h"
     42 
     43 #if 0
     44 #undef CDBG
     45 #undef LOG_TAG
     46 #define CDBG ALOGE
     47 #define LOG_TAG "NotifyLogs"
     48 #endif
     49 
     50 int32_t mm_camera_queue_init(mm_camera_queue_t* queue)
     51 {
     52     pthread_mutex_init(&queue->lock, NULL);
     53     cam_list_init(&queue->head.list);
     54     queue->size = 0;
     55     return 0;
     56 }
     57 
     58 int32_t mm_camera_queue_enq(mm_camera_queue_t* queue, void* data)
     59 {
     60     mm_camera_q_node_t* node =
     61         (mm_camera_q_node_t *)malloc(sizeof(mm_camera_q_node_t));
     62     if (NULL == node) {
     63         CDBG_ERROR("%s: No memory for mm_camera_q_node_t", __func__);
     64         return -1;
     65     }
     66 
     67     memset(node, 0, sizeof(mm_camera_q_node_t));
     68     node->data = data;
     69 
     70     pthread_mutex_lock(&queue->lock);
     71     cam_list_add_tail_node(&node->list, &queue->head.list);
     72     queue->size++;
     73     pthread_mutex_unlock(&queue->lock);
     74 
     75     return 0;
     76 
     77 }
     78 
     79 void* mm_camera_queue_deq(mm_camera_queue_t* queue)
     80 {
     81     mm_camera_q_node_t* node = NULL;
     82     void* data = NULL;
     83     struct cam_list *head = NULL;
     84     struct cam_list *pos = NULL;
     85 
     86     pthread_mutex_lock(&queue->lock);
     87     head = &queue->head.list;
     88     pos = head->next;
     89     if (pos != head) {
     90         node = member_of(pos, mm_camera_q_node_t, list);
     91         cam_list_del_node(&node->list);
     92         queue->size--;
     93     }
     94     pthread_mutex_unlock(&queue->lock);
     95 
     96     if (NULL != node) {
     97         data = node->data;
     98         free(node);
     99     }
    100 
    101     return data;
    102 }
    103 
    104 int32_t mm_camera_queue_deinit(mm_camera_queue_t* queue)
    105 {
    106     mm_camera_queue_flush(queue);
    107     pthread_mutex_destroy(&queue->lock);
    108     return 0;
    109 }
    110 
    111 int32_t mm_camera_queue_flush(mm_camera_queue_t* queue)
    112 {
    113     mm_camera_q_node_t* node = NULL;
    114     void* data = NULL;
    115     struct cam_list *head = NULL;
    116     struct cam_list *pos = NULL;
    117 
    118     pthread_mutex_lock(&queue->lock);
    119     head = &queue->head.list;
    120     pos = head->next;
    121 
    122     while(pos != head) {
    123         node = member_of(pos, mm_camera_q_node_t, list);
    124         pos = pos->next;
    125         cam_list_del_node(&node->list);
    126         queue->size--;
    127 
    128         /* TODO later to consider ptr inside data */
    129         /* for now we only assume there is no ptr inside data
    130          * so we free data directly */
    131         if (NULL != node->data) {
    132             free(node->data);
    133         }
    134         free(node);
    135 
    136     }
    137     queue->size = 0;
    138     pthread_mutex_unlock(&queue->lock);
    139     return 0;
    140 }
    141 
    142 static void *mm_camera_cmd_thread(void *data)
    143 {
    144     int rc = 0;
    145     int running = 1;
    146     int ret;
    147     mm_camera_cmd_thread_t *cmd_thread =
    148                 (mm_camera_cmd_thread_t *)data;
    149     mm_camera_cmdcb_t* node = NULL;
    150 
    151     do {
    152         do {
    153             ret = sem_wait(&cmd_thread->cmd_sem);
    154             if (ret != 0 && errno != EINVAL) {
    155                 CDBG_ERROR("%s: sem_wait error (%s)",
    156                            __func__, strerror(errno));
    157                 return NULL;
    158             }
    159         } while (ret != 0);
    160 
    161         /* we got notified about new cmd avail in cmd queue */
    162         node = (mm_camera_cmdcb_t*)mm_camera_queue_deq(&cmd_thread->cmd_queue);
    163         while (node != NULL) {
    164             switch (node->cmd_type) {
    165             case MM_CAMERA_CMD_TYPE_EVT_CB:
    166             case MM_CAMERA_CMD_TYPE_DATA_CB:
    167             case MM_CAMERA_CMD_TYPE_ASYNC_CB:
    168             case MM_CAMERA_CMD_TYPE_REQ_DATA_CB:
    169             case MM_CAMERA_CMD_TYPE_SUPER_BUF_DATA_CB:
    170                 if (NULL != cmd_thread->cb) {
    171                     cmd_thread->cb(node, cmd_thread->user_data);
    172                 }
    173                 break;
    174             case MM_CAMERA_CMD_TYPE_EXIT:
    175             default:
    176                 running = 0;
    177                 break;
    178             }
    179             free(node);
    180             node = (mm_camera_cmdcb_t*)mm_camera_queue_deq(&cmd_thread->cmd_queue);
    181         } /* (node != NULL) */
    182     } while (running);
    183     return NULL;
    184 }
    185 
    186 int32_t mm_camera_cmd_thread_launch(mm_camera_cmd_thread_t * cmd_thread,
    187                                     mm_camera_cmd_cb_t cb,
    188                                     void* user_data)
    189 {
    190     int32_t rc = 0;
    191 
    192     sem_init(&cmd_thread->cmd_sem, 0, 0);
    193     mm_camera_queue_init(&cmd_thread->cmd_queue);
    194     cmd_thread->cb = cb;
    195     cmd_thread->user_data = user_data;
    196 
    197     /* launch the thread */
    198     pthread_create(&cmd_thread->cmd_pid,
    199                    NULL,
    200                    mm_camera_cmd_thread,
    201                    (void *)cmd_thread);
    202     return rc;
    203 }
    204 
    205 int32_t mm_camera_cmd_thread_stop(mm_camera_cmd_thread_t * cmd_thread)
    206 {
    207     int32_t rc = 0;
    208     mm_camera_buf_info_t  buf_info;
    209     mm_camera_cmdcb_t* node = (mm_camera_cmdcb_t *)malloc(sizeof(mm_camera_cmdcb_t));
    210     if (NULL == node) {
    211         CDBG_ERROR("%s: No memory for mm_camera_cmdcb_t", __func__);
    212         return -1;
    213     }
    214 
    215     memset(node, 0, sizeof(mm_camera_cmdcb_t));
    216     node->cmd_type = MM_CAMERA_CMD_TYPE_EXIT;
    217 
    218     mm_camera_queue_enq(&cmd_thread->cmd_queue, node);
    219     sem_post(&cmd_thread->cmd_sem);
    220 
    221     /* wait until cmd thread exits */
    222     if (pthread_join(cmd_thread->cmd_pid, NULL) != 0) {
    223         CDBG("%s: pthread dead already\n", __func__);
    224     }
    225     return rc;
    226 }
    227 
    228 int32_t mm_camera_cmd_thread_destroy(mm_camera_cmd_thread_t * cmd_thread)
    229 {
    230     int32_t rc = 0;
    231     mm_camera_queue_deinit(&cmd_thread->cmd_queue);
    232     sem_destroy(&cmd_thread->cmd_sem);
    233     memset(cmd_thread, 0, sizeof(mm_camera_cmd_thread_t));
    234     return rc;
    235 }
    236 
    237 int32_t mm_camera_cmd_thread_release(mm_camera_cmd_thread_t * cmd_thread)
    238 {
    239     int32_t rc = 0;
    240     rc = mm_camera_cmd_thread_stop(cmd_thread);
    241     if (0 == rc) {
    242         rc = mm_camera_cmd_thread_destroy(cmd_thread);
    243     }
    244     return rc;
    245 }
    246 
    247