Staging: dream: camera: msm_camera: fix code style issues
[pandora-kernel.git] / drivers / staging / dream / camera / msm_camera.c
1 /*
2  * Copyright (C) 2008-2009 QUALCOMM Incorporated.
3  */
4
5 /* FIXME: most allocations need not be GFP_ATOMIC */
6 /* FIXME: management of mutexes */
7 /* FIXME: msm_pmem_region_lookup return values */
8 /* FIXME: way too many copy to/from user */
9 /* FIXME: does region->active mean free */
10 /* FIXME: check limits on command lenghts passed from userspace */
11 /* FIXME: __msm_release: which queues should we flush when opencnt != 0 */
12
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/init.h>
16 #include <linux/sched.h>
17 #include <mach/board.h>
18
19 #include <linux/fs.h>
20 #include <linux/list.h>
21 #include <linux/uaccess.h>
22 #include <linux/android_pmem.h>
23 #include <linux/poll.h>
24 #include <media/msm_camera.h>
25 #include <mach/camera.h>
26
27 #define MSM_MAX_CAMERA_SENSORS 5
28
29 #define ERR_USER_COPY(to) pr_err("%s(%d): copy %s user\n", \
30                                 __func__, __LINE__, ((to) ? "to" : "from"))
31 #define ERR_COPY_FROM_USER() ERR_USER_COPY(0)
32 #define ERR_COPY_TO_USER() ERR_USER_COPY(1)
33
34 static struct class *msm_class;
35 static dev_t msm_devno;
36 static LIST_HEAD(msm_sensors);
37
38 #define __CONTAINS(r, v, l, field) ({                           \
39         typeof(r) __r = r;                                      \
40         typeof(v) __v = v;                                      \
41         typeof(v) __e = __v + l;                                \
42         int res = __v >= __r->field &&                          \
43                 __e <= __r->field + __r->len;                   \
44         res;                                                    \
45 })
46
47 #define CONTAINS(r1, r2, field) ({                              \
48         typeof(r2) __r2 = r2;                                   \
49         __CONTAINS(r1, __r2->field, __r2->len, field);          \
50 })
51
52 #define IN_RANGE(r, v, field) ({                                \
53         typeof(r) __r = r;                                      \
54         typeof(v) __vv = v;                                     \
55         int res = ((__vv >= __r->field) &&                      \
56                 (__vv < (__r->field + __r->len)));              \
57         res;                                                    \
58 })
59
60 #define OVERLAPS(r1, r2, field) ({                              \
61         typeof(r1) __r1 = r1;                                   \
62         typeof(r2) __r2 = r2;                                   \
63         typeof(__r2->field) __v = __r2->field;                  \
64         typeof(__v) __e = __v + __r2->len - 1;                  \
65         int res = (IN_RANGE(__r1, __v, field) ||                \
66                    IN_RANGE(__r1, __e, field));                 \
67         res;                                                    \
68 })
69
70 #define MSM_DRAIN_QUEUE_NOSYNC(sync, name) do {                 \
71         struct msm_queue_cmd *qcmd = NULL;                      \
72         CDBG("%s: draining queue "#name"\n", __func__);         \
73         while (!list_empty(&(sync)->name)) {                    \
74                 qcmd = list_first_entry(&(sync)->name,          \
75                         struct msm_queue_cmd, list);            \
76                 list_del_init(&qcmd->list);                     \
77                 kfree(qcmd);                                    \
78         };                                                      \
79 } while (0)
80
81 #define MSM_DRAIN_QUEUE(sync, name) do {                        \
82         unsigned long flags;                                    \
83         spin_lock_irqsave(&(sync)->name##_lock, flags);         \
84         MSM_DRAIN_QUEUE_NOSYNC(sync, name);                     \
85         spin_unlock_irqrestore(&(sync)->name##_lock, flags);    \
86 } while (0)
87
88 static int check_overlap(struct hlist_head *ptype,
89                         unsigned long paddr,
90                         unsigned long len)
91 {
92         struct msm_pmem_region *region;
93         struct msm_pmem_region t = { .paddr = paddr, .len = len };
94         struct hlist_node *node;
95
96         hlist_for_each_entry(region, node, ptype, list) {
97                 if (CONTAINS(region, &t, paddr) ||
98                                 CONTAINS(&t, region, paddr) ||
99                                 OVERLAPS(region, &t, paddr)) {
100                         printk(KERN_ERR
101                                 " region (PHYS %p len %ld)"
102                                 " clashes with registered region"
103                                 " (paddr %p len %ld)\n",
104                                 (void *)t.paddr, t.len,
105                                 (void *)region->paddr, region->len);
106                         return -1;
107                 }
108         }
109
110         return 0;
111 }
112
113 static int msm_pmem_table_add(struct hlist_head *ptype,
114         struct msm_pmem_info *info)
115 {
116         struct file *file;
117         unsigned long paddr;
118         unsigned long vstart;
119         unsigned long len;
120         int rc;
121         struct msm_pmem_region *region;
122
123         rc = get_pmem_file(info->fd, &paddr, &vstart, &len, &file);
124         if (rc < 0) {
125                 pr_err("msm_pmem_table_add: get_pmem_file fd %d error %d\n",
126                         info->fd, rc);
127                 return rc;
128         }
129
130         if (check_overlap(ptype, paddr, len) < 0)
131                 return -EINVAL;
132
133         CDBG("%s: type = %d, paddr = 0x%lx, vaddr = 0x%lx\n",
134                 __func__,
135                 info->type, paddr, (unsigned long)info->vaddr);
136
137         region = kmalloc(sizeof(*region), GFP_KERNEL);
138         if (!region)
139                 return -ENOMEM;
140
141         INIT_HLIST_NODE(&region->list);
142
143         region->type = info->type;
144         region->vaddr = info->vaddr;
145         region->paddr = paddr;
146         region->len = len;
147         region->file = file;
148         region->y_off = info->y_off;
149         region->cbcr_off = info->cbcr_off;
150         region->fd = info->fd;
151         region->active = info->active;
152
153         hlist_add_head(&(region->list), ptype);
154
155         return 0;
156 }
157
158 /* return of 0 means failure */
159 static uint8_t msm_pmem_region_lookup(struct hlist_head *ptype,
160         int pmem_type, struct msm_pmem_region *reg, uint8_t maxcount)
161 {
162         struct msm_pmem_region *region;
163         struct msm_pmem_region *regptr;
164         struct hlist_node *node, *n;
165
166         uint8_t rc = 0;
167
168         regptr = reg;
169
170         hlist_for_each_entry_safe(region, node, n, ptype, list) {
171                 if (region->type == pmem_type && region->active) {
172                         *regptr = *region;
173                         rc += 1;
174                         if (rc >= maxcount)
175                                 break;
176                         regptr++;
177                 }
178         }
179
180         return rc;
181 }
182
183 static unsigned long msm_pmem_frame_ptov_lookup(struct msm_sync *sync,
184                 unsigned long pyaddr,
185                 unsigned long pcbcraddr,
186                 uint32_t *yoff, uint32_t *cbcroff, int *fd)
187 {
188         struct msm_pmem_region *region;
189         struct hlist_node *node, *n;
190
191         hlist_for_each_entry_safe(region, node, n, &sync->frame, list) {
192                 if (pyaddr == (region->paddr + region->y_off) &&
193                                 pcbcraddr == (region->paddr +
194                                                 region->cbcr_off) &&
195                                 region->active) {
196                         /* offset since we could pass vaddr inside
197                          * a registerd pmem buffer
198                          */
199                         *yoff = region->y_off;
200                         *cbcroff = region->cbcr_off;
201                         *fd = region->fd;
202                         region->active = 0;
203                         return (unsigned long)(region->vaddr);
204                 }
205         }
206
207         return 0;
208 }
209
210 static unsigned long msm_pmem_stats_ptov_lookup(struct msm_sync *sync,
211                 unsigned long addr, int *fd)
212 {
213         struct msm_pmem_region *region;
214         struct hlist_node *node, *n;
215
216         hlist_for_each_entry_safe(region, node, n, &sync->stats, list) {
217                 if (addr == region->paddr && region->active) {
218                         /* offset since we could pass vaddr inside a
219                          * registered pmem buffer */
220                         *fd = region->fd;
221                         region->active = 0;
222                         return (unsigned long)(region->vaddr);
223                 }
224         }
225
226         return 0;
227 }
228
229 static unsigned long msm_pmem_frame_vtop_lookup(struct msm_sync *sync,
230                 unsigned long buffer,
231                 uint32_t yoff, uint32_t cbcroff, int fd)
232 {
233         struct msm_pmem_region *region;
234         struct hlist_node *node, *n;
235
236         hlist_for_each_entry_safe(region,
237                 node, n, &sync->frame, list) {
238                 if (((unsigned long)(region->vaddr) == buffer) &&
239                                 (region->y_off == yoff) &&
240                                 (region->cbcr_off == cbcroff) &&
241                                 (region->fd == fd) &&
242                                 (region->active == 0)) {
243
244                         region->active = 1;
245                         return region->paddr;
246                 }
247         }
248
249         return 0;
250 }
251
252 static unsigned long msm_pmem_stats_vtop_lookup(
253                 struct msm_sync *sync,
254                 unsigned long buffer,
255                 int fd)
256 {
257         struct msm_pmem_region *region;
258         struct hlist_node *node, *n;
259
260         hlist_for_each_entry_safe(region, node, n, &sync->stats, list) {
261                 if (((unsigned long)(region->vaddr) == buffer) &&
262                                 (region->fd == fd) && region->active == 0) {
263                         region->active = 1;
264                         return region->paddr;
265                 }
266         }
267
268         return 0;
269 }
270
271 static int __msm_pmem_table_del(struct msm_sync *sync,
272                 struct msm_pmem_info *pinfo)
273 {
274         int rc = 0;
275         struct msm_pmem_region *region;
276         struct hlist_node *node, *n;
277
278         switch (pinfo->type) {
279         case MSM_PMEM_OUTPUT1:
280         case MSM_PMEM_OUTPUT2:
281         case MSM_PMEM_THUMBAIL:
282         case MSM_PMEM_MAINIMG:
283         case MSM_PMEM_RAW_MAINIMG:
284                 hlist_for_each_entry_safe(region, node, n,
285                         &sync->frame, list) {
286
287                         if (pinfo->type == region->type &&
288                                         pinfo->vaddr == region->vaddr &&
289                                         pinfo->fd == region->fd) {
290                                 hlist_del(node);
291                                 put_pmem_file(region->file);
292                                 kfree(region);
293                         }
294                 }
295                 break;
296
297         case MSM_PMEM_AEC_AWB:
298         case MSM_PMEM_AF:
299                 hlist_for_each_entry_safe(region, node, n,
300                         &sync->stats, list) {
301
302                         if (pinfo->type == region->type &&
303                                         pinfo->vaddr == region->vaddr &&
304                                         pinfo->fd == region->fd) {
305                                 hlist_del(node);
306                                 put_pmem_file(region->file);
307                                 kfree(region);
308                         }
309                 }
310                 break;
311
312         default:
313                 rc = -EINVAL;
314                 break;
315         }
316
317         return rc;
318 }
319
320 static int msm_pmem_table_del(struct msm_sync *sync, void __user *arg)
321 {
322         struct msm_pmem_info info;
323
324         if (copy_from_user(&info, arg, sizeof(info))) {
325                 ERR_COPY_FROM_USER();
326                 return -EFAULT;
327         }
328
329         return __msm_pmem_table_del(sync, &info);
330 }
331
332 static int __msm_get_frame(struct msm_sync *sync,
333                 struct msm_frame *frame)
334 {
335         unsigned long flags;
336         int rc = 0;
337
338         struct msm_queue_cmd *qcmd = NULL;
339         struct msm_vfe_phy_info *pphy;
340
341         spin_lock_irqsave(&sync->prev_frame_q_lock, flags);
342         if (!list_empty(&sync->prev_frame_q)) {
343                 qcmd = list_first_entry(&sync->prev_frame_q,
344                         struct msm_queue_cmd, list);
345                 list_del_init(&qcmd->list);
346         }
347         spin_unlock_irqrestore(&sync->prev_frame_q_lock, flags);
348
349         if (!qcmd) {
350                 pr_err("%s: no preview frame.\n", __func__);
351                 return -EAGAIN;
352         }
353
354         pphy = (struct msm_vfe_phy_info *)(qcmd->command);
355
356         frame->buffer =
357                 msm_pmem_frame_ptov_lookup(sync,
358                         pphy->y_phy,
359                         pphy->cbcr_phy, &(frame->y_off),
360                         &(frame->cbcr_off), &(frame->fd));
361         if (!frame->buffer) {
362                 pr_err("%s: cannot get frame, invalid lookup address "
363                         "y=%x cbcr=%x offset=%d\n",
364                         __func__,
365                         pphy->y_phy,
366                         pphy->cbcr_phy,
367                         frame->y_off);
368                 rc = -EINVAL;
369         }
370
371         CDBG("__msm_get_frame: y=0x%x, cbcr=0x%x, qcmd=0x%x, virt_addr=0x%x\n",
372                 pphy->y_phy, pphy->cbcr_phy, (int) qcmd, (int) frame->buffer);
373
374         kfree(qcmd);
375         return rc;
376 }
377
378 static int msm_get_frame(struct msm_sync *sync, void __user *arg)
379 {
380         int rc = 0;
381         struct msm_frame frame;
382
383         if (copy_from_user(&frame,
384                                 arg,
385                                 sizeof(struct msm_frame))) {
386                 ERR_COPY_FROM_USER();
387                 return -EFAULT;
388         }
389
390         rc = __msm_get_frame(sync, &frame);
391         if (rc < 0)
392                 return rc;
393
394         if (sync->croplen) {
395                 if (frame.croplen > sync->croplen) {
396                         pr_err("msm_get_frame: invalid frame croplen %d\n",
397                                 frame.croplen);
398                         return -EINVAL;
399                 }
400
401                 if (copy_to_user((void *)frame.cropinfo,
402                                 sync->cropinfo,
403                                 sync->croplen)) {
404                         ERR_COPY_TO_USER();
405                         return -EFAULT;
406                 }
407         }
408
409         if (copy_to_user((void *)arg,
410                                 &frame, sizeof(struct msm_frame))) {
411                 ERR_COPY_TO_USER();
412                 rc = -EFAULT;
413         }
414
415         CDBG("Got frame!!!\n");
416
417         return rc;
418 }
419
420 static int msm_enable_vfe(struct msm_sync *sync, void __user *arg)
421 {
422         int rc = -EIO;
423         struct camera_enable_cmd cfg;
424
425         if (copy_from_user(&cfg,
426                         arg,
427                         sizeof(struct camera_enable_cmd))) {
428                 ERR_COPY_FROM_USER();
429                 return -EFAULT;
430         }
431
432         if (sync->vfefn.vfe_enable)
433                 rc = sync->vfefn.vfe_enable(&cfg);
434
435         CDBG("msm_enable_vfe: returned rc = %d\n", rc);
436         return rc;
437 }
438
439 static int msm_disable_vfe(struct msm_sync *sync, void __user *arg)
440 {
441         int rc = -EIO;
442         struct camera_enable_cmd cfg;
443
444         if (copy_from_user(&cfg,
445                         arg,
446                         sizeof(struct camera_enable_cmd))) {
447                 ERR_COPY_FROM_USER();
448                 return -EFAULT;
449         }
450
451         if (sync->vfefn.vfe_disable)
452                 rc = sync->vfefn.vfe_disable(&cfg, NULL);
453
454         CDBG("msm_disable_vfe: returned rc = %d\n", rc);
455         return rc;
456 }
457
458 static struct msm_queue_cmd *__msm_control(struct msm_sync *sync,
459                 struct msm_control_device_queue *queue,
460                 struct msm_queue_cmd *qcmd,
461                 int timeout)
462 {
463         unsigned long flags;
464         int rc;
465
466         spin_lock_irqsave(&sync->msg_event_q_lock, flags);
467         list_add_tail(&qcmd->list, &sync->msg_event_q);
468         /* wake up config thread */
469         wake_up(&sync->msg_event_wait);
470         spin_unlock_irqrestore(&sync->msg_event_q_lock, flags);
471
472         if (!queue)
473                 return NULL;
474
475         /* wait for config status */
476         rc = wait_event_interruptible_timeout(
477                         queue->ctrl_status_wait,
478                         !list_empty_careful(&queue->ctrl_status_q),
479                         timeout);
480         if (list_empty_careful(&queue->ctrl_status_q)) {
481                 if (!rc)
482                         rc = -ETIMEDOUT;
483                 if (rc < 0) {
484                         pr_err("msm_control: wait_event error %d\n", rc);
485 #if 0
486                         /* This is a bit scary.  If we time out too early, we
487                          * will free qcmd at the end of this function, and the
488                          * dsp may do the same when it does respond, so we
489                          * remove the message from the source queue.
490                          */
491                         pr_err("%s: error waiting for ctrl_status_q: %d\n",
492                                 __func__, rc);
493                         spin_lock_irqsave(&sync->msg_event_q_lock, flags);
494                         list_del_init(&qcmd->list);
495                         spin_unlock_irqrestore(&sync->msg_event_q_lock, flags);
496 #endif
497                         return ERR_PTR(rc);
498                 }
499         }
500
501         /* control command status is ready */
502         spin_lock_irqsave(&queue->ctrl_status_q_lock, flags);
503         BUG_ON(list_empty(&queue->ctrl_status_q));
504         qcmd = list_first_entry(&queue->ctrl_status_q,
505                         struct msm_queue_cmd, list);
506         list_del_init(&qcmd->list);
507         spin_unlock_irqrestore(&queue->ctrl_status_q_lock, flags);
508
509         return qcmd;
510 }
511
512 static int msm_control(struct msm_control_device *ctrl_pmsm,
513                         int block,
514                         void __user *arg)
515 {
516         int rc = 0;
517
518         struct msm_sync *sync = ctrl_pmsm->pmsm->sync;
519         struct msm_ctrl_cmd udata, *ctrlcmd;
520         struct msm_queue_cmd *qcmd = NULL, *qcmd_temp;
521
522         if (copy_from_user(&udata, arg, sizeof(struct msm_ctrl_cmd))) {
523                 ERR_COPY_FROM_USER();
524                 rc = -EFAULT;
525                 goto end;
526         }
527
528         qcmd = kmalloc(sizeof(struct msm_queue_cmd) +
529                                 sizeof(struct msm_ctrl_cmd) + udata.length,
530                                 GFP_KERNEL);
531         if (!qcmd) {
532                 pr_err("msm_control: cannot allocate buffer\n");
533                 rc = -ENOMEM;
534                 goto end;
535         }
536
537         qcmd->type = MSM_CAM_Q_CTRL;
538         qcmd->command = ctrlcmd = (struct msm_ctrl_cmd *)(qcmd + 1);
539         *ctrlcmd = udata;
540         ctrlcmd->value = ctrlcmd + 1;
541
542         if (udata.length) {
543                 if (copy_from_user(ctrlcmd->value,
544                                 udata.value, udata.length)) {
545                         ERR_COPY_FROM_USER();
546                         rc = -EFAULT;
547                         goto end;
548                 }
549         }
550
551         if (!block) {
552                 /* qcmd will be set to NULL */
553                 qcmd = __msm_control(sync, NULL, qcmd, 0);
554                 goto end;
555         }
556
557         qcmd_temp = __msm_control(sync,
558                                   &ctrl_pmsm->ctrl_q,
559                                   qcmd, MAX_SCHEDULE_TIMEOUT);
560
561         if (IS_ERR(qcmd_temp)) {
562                 rc = PTR_ERR(qcmd_temp);
563                 goto end;
564         }
565         qcmd = qcmd_temp;
566
567         if (qcmd->command) {
568                 void __user *to = udata.value;
569                 udata = *(struct msm_ctrl_cmd *)qcmd->command;
570                 if (udata.length > 0) {
571                         if (copy_to_user(to,
572                                          udata.value,
573                                          udata.length)) {
574                                 ERR_COPY_TO_USER();
575                                 rc = -EFAULT;
576                                 goto end;
577                         }
578                 }
579                 udata.value = to;
580
581                 if (copy_to_user((void *)arg, &udata,
582                                 sizeof(struct msm_ctrl_cmd))) {
583                         ERR_COPY_TO_USER();
584                         rc = -EFAULT;
585                         goto end;
586                 }
587         }
588
589 end:
590         /* Note: if we get here as a result of an error, we will free the
591          * qcmd that we kmalloc() in this function.  When we come here as
592          * a result of a successful completion, we are freeing the qcmd that
593          * we dequeued from queue->ctrl_status_q.
594          */
595         if (qcmd)
596                 kfree(qcmd);
597
598         CDBG("msm_control: end rc = %d\n", rc);
599         return rc;
600 }
601
602 static int msm_get_stats(struct msm_sync *sync, void __user *arg)
603 {
604         unsigned long flags;
605         int timeout;
606         int rc = 0;
607
608         struct msm_stats_event_ctrl se;
609
610         struct msm_queue_cmd *qcmd = NULL;
611         struct msm_ctrl_cmd  *ctrl = NULL;
612         struct msm_vfe_resp  *data = NULL;
613         struct msm_stats_buf stats;
614
615         if (copy_from_user(&se, arg,
616                         sizeof(struct msm_stats_event_ctrl))) {
617                 ERR_COPY_FROM_USER();
618                 return -EFAULT;
619         }
620
621         timeout = (int)se.timeout_ms;
622
623         CDBG("msm_get_stats timeout %d\n", timeout);
624         rc = wait_event_interruptible_timeout(
625                         sync->msg_event_wait,
626                         !list_empty_careful(&sync->msg_event_q),
627                         msecs_to_jiffies(timeout));
628         if (list_empty_careful(&sync->msg_event_q)) {
629                 if (rc == 0)
630                         rc = -ETIMEDOUT;
631                 if (rc < 0) {
632                         pr_err("msm_get_stats error %d\n", rc);
633                         return rc;
634                 }
635         }
636         CDBG("msm_get_stats returned from wait: %d\n", rc);
637
638         spin_lock_irqsave(&sync->msg_event_q_lock, flags);
639         BUG_ON(list_empty(&sync->msg_event_q));
640         qcmd = list_first_entry(&sync->msg_event_q,
641                         struct msm_queue_cmd, list);
642         list_del_init(&qcmd->list);
643         spin_unlock_irqrestore(&sync->msg_event_q_lock, flags);
644
645         CDBG("=== received from DSP === %d\n", qcmd->type);
646
647         switch (qcmd->type) {
648         case MSM_CAM_Q_VFE_EVT:
649         case MSM_CAM_Q_VFE_MSG:
650                 data = (struct msm_vfe_resp *)(qcmd->command);
651
652                 /* adsp event and message */
653                 se.resptype = MSM_CAM_RESP_STAT_EVT_MSG;
654
655                 /* 0 - msg from aDSP, 1 - event from mARM */
656                 se.stats_event.type   = data->evt_msg.type;
657                 se.stats_event.msg_id = data->evt_msg.msg_id;
658                 se.stats_event.len    = data->evt_msg.len;
659
660                 CDBG("msm_get_stats, qcmd->type = %d\n", qcmd->type);
661                 CDBG("length = %d\n", se.stats_event.len);
662                 CDBG("msg_id = %d\n", se.stats_event.msg_id);
663
664                 if ((data->type == VFE_MSG_STATS_AF) ||
665                                 (data->type == VFE_MSG_STATS_WE)) {
666
667                         stats.buffer =
668                         msm_pmem_stats_ptov_lookup(sync,
669                                         data->phy.sbuf_phy,
670                                         &(stats.fd));
671                         if (!stats.buffer) {
672                                 pr_err("%s: msm_pmem_stats_ptov_lookup error\n",
673                                         __func__);
674                                 rc = -EINVAL;
675                                 goto failure;
676                         }
677
678                         if (copy_to_user((void *)(se.stats_event.data),
679                                         &stats,
680                                         sizeof(struct msm_stats_buf))) {
681                                 ERR_COPY_TO_USER();
682                                 rc = -EFAULT;
683                                 goto failure;
684                         }
685                 } else if ((data->evt_msg.len > 0) &&
686                                 (data->type == VFE_MSG_GENERAL)) {
687                         if (copy_to_user((void *)(se.stats_event.data),
688                                         data->evt_msg.data,
689                                         data->evt_msg.len)) {
690                                 ERR_COPY_TO_USER();
691                                 rc = -EFAULT;
692                         }
693                 } else if (data->type == VFE_MSG_OUTPUT1 ||
694                         data->type == VFE_MSG_OUTPUT2) {
695                         if (copy_to_user((void *)(se.stats_event.data),
696                                         data->extdata,
697                                         data->extlen)) {
698                                 ERR_COPY_TO_USER();
699                                 rc = -EFAULT;
700                         }
701                 } else if (data->type == VFE_MSG_SNAPSHOT && sync->pict_pp) {
702                         struct msm_postproc buf;
703                         struct msm_pmem_region region;
704                         buf.fmnum = msm_pmem_region_lookup(&sync->frame,
705                                         MSM_PMEM_MAINIMG,
706                                         &region, 1);
707                         if (buf.fmnum == 1) {
708                                 buf.fmain.buffer = (unsigned long)region.vaddr;
709                                 buf.fmain.y_off  = region.y_off;
710                                 buf.fmain.cbcr_off = region.cbcr_off;
711                                 buf.fmain.fd = region.fd;
712                         } else {
713                                 buf.fmnum = msm_pmem_region_lookup(&sync->frame,
714                                                 MSM_PMEM_RAW_MAINIMG,
715                                                 &region, 1);
716                                 if (buf.fmnum == 1) {
717                                         buf.fmain.path = MSM_FRAME_PREV_2;
718                                         buf.fmain.buffer =
719                                                 (unsigned long)region.vaddr;
720                                         buf.fmain.fd = region.fd;
721                                 } else {
722                                         pr_err("%s: pmem lookup failed\n",
723                                                 __func__);
724                                         rc = -EINVAL;
725                                 }
726                         }
727
728                         if (copy_to_user((void *)(se.stats_event.data), &buf,
729                                         sizeof(buf))) {
730                                 ERR_COPY_TO_USER();
731                                 rc = -EFAULT;
732                                 goto failure;
733                         }
734                         CDBG("snapshot copy_to_user!\n");
735                 }
736                 break;
737
738         case MSM_CAM_Q_CTRL:
739                 /* control command from control thread */
740                 ctrl = (struct msm_ctrl_cmd *)(qcmd->command);
741
742                 CDBG("msm_get_stats, qcmd->type = %d\n", qcmd->type);
743                 CDBG("length = %d\n", ctrl->length);
744
745                 if (ctrl->length > 0) {
746                         if (copy_to_user((void *)(se.ctrl_cmd.value),
747                                                 ctrl->value,
748                                                 ctrl->length)) {
749                                 ERR_COPY_TO_USER();
750                                 rc = -EFAULT;
751                                 goto failure;
752                         }
753                 }
754
755                 se.resptype = MSM_CAM_RESP_CTRL;
756
757                 /* what to control */
758                 se.ctrl_cmd.type = ctrl->type;
759                 se.ctrl_cmd.length = ctrl->length;
760                 se.ctrl_cmd.resp_fd = ctrl->resp_fd;
761                 break;
762
763         case MSM_CAM_Q_V4L2_REQ:
764                 /* control command from v4l2 client */
765                 ctrl = (struct msm_ctrl_cmd *)(qcmd->command);
766
767                 CDBG("msm_get_stats, qcmd->type = %d\n", qcmd->type);
768                 CDBG("length = %d\n", ctrl->length);
769
770                 if (ctrl->length > 0) {
771                         if (copy_to_user((void *)(se.ctrl_cmd.value),
772                                         ctrl->value, ctrl->length)) {
773                                 ERR_COPY_TO_USER();
774                                 rc = -EFAULT;
775                                 goto failure;
776                         }
777                 }
778
779                 /* 2 tells config thread this is v4l2 request */
780                 se.resptype = MSM_CAM_RESP_V4L2;
781
782                 /* what to control */
783                 se.ctrl_cmd.type   = ctrl->type;
784                 se.ctrl_cmd.length = ctrl->length;
785                 break;
786
787         default:
788                 rc = -EFAULT;
789                 goto failure;
790         } /* switch qcmd->type */
791
792         if (copy_to_user((void *)arg, &se, sizeof(se))) {
793                 ERR_COPY_TO_USER();
794                 rc = -EFAULT;
795         }
796
797 failure:
798         if (qcmd)
799                 kfree(qcmd);
800
801         CDBG("msm_get_stats: %d\n", rc);
802         return rc;
803 }
804
805 static int msm_ctrl_cmd_done(struct msm_control_device *ctrl_pmsm,
806                 void __user *arg)
807 {
808         unsigned long flags;
809         int rc = 0;
810
811         struct msm_ctrl_cmd udata, *ctrlcmd;
812         struct msm_queue_cmd *qcmd = NULL;
813
814         if (copy_from_user(&udata, arg, sizeof(struct msm_ctrl_cmd))) {
815                 ERR_COPY_FROM_USER();
816                 rc = -EFAULT;
817                 goto end;
818         }
819
820         qcmd = kmalloc(sizeof(struct msm_queue_cmd) +
821                         sizeof(struct msm_ctrl_cmd) + udata.length,
822                         GFP_KERNEL);
823         if (!qcmd) {
824                 rc = -ENOMEM;
825                 goto end;
826         }
827
828         qcmd->command = ctrlcmd = (struct msm_ctrl_cmd *)(qcmd + 1);
829         *ctrlcmd = udata;
830         if (udata.length > 0) {
831                 ctrlcmd->value = ctrlcmd + 1;
832                 if (copy_from_user(ctrlcmd->value,
833                                         (void *)udata.value,
834                                         udata.length)) {
835                         ERR_COPY_FROM_USER();
836                         rc = -EFAULT;
837                         kfree(qcmd);
838                         goto end;
839                 }
840         } else
841                 ctrlcmd->value = NULL;
842
843 end:
844         CDBG("msm_ctrl_cmd_done: end rc = %d\n", rc);
845         if (rc == 0) {
846                 /* wake up control thread */
847                 spin_lock_irqsave(&ctrl_pmsm->ctrl_q.ctrl_status_q_lock, flags);
848                 list_add_tail(&qcmd->list, &ctrl_pmsm->ctrl_q.ctrl_status_q);
849                 wake_up(&ctrl_pmsm->ctrl_q.ctrl_status_wait);
850                 spin_unlock_irqrestore(&ctrl_pmsm->ctrl_q.ctrl_status_q_lock, flags);
851         }
852
853         return rc;
854 }
855
856 static int msm_config_vfe(struct msm_sync *sync, void __user *arg)
857 {
858         struct msm_vfe_cfg_cmd cfgcmd;
859         struct msm_pmem_region region[8];
860         struct axidata axi_data;
861         void *data = NULL;
862         int rc = -EIO;
863
864         memset(&axi_data, 0, sizeof(axi_data));
865
866         if (copy_from_user(&cfgcmd, arg, sizeof(cfgcmd))) {
867                 ERR_COPY_FROM_USER();
868                 return -EFAULT;
869         }
870
871         switch (cfgcmd.cmd_type) {
872         case CMD_STATS_ENABLE:
873                 axi_data.bufnum1 =
874                         msm_pmem_region_lookup(&sync->stats,
875                                         MSM_PMEM_AEC_AWB, &region[0],
876                                         NUM_WB_EXP_STAT_OUTPUT_BUFFERS);
877                 if (!axi_data.bufnum1) {
878                         pr_err("%s: pmem region lookup error\n", __func__);
879                         return -EINVAL;
880                 }
881                 axi_data.region = &region[0];
882                 data = &axi_data;
883                 break;
884         case CMD_STATS_AF_ENABLE:
885                 axi_data.bufnum1 =
886                         msm_pmem_region_lookup(&sync->stats,
887                                         MSM_PMEM_AF, &region[0],
888                                         NUM_AF_STAT_OUTPUT_BUFFERS);
889                 if (!axi_data.bufnum1) {
890                         pr_err("%s: pmem region lookup error\n", __func__);
891                         return -EINVAL;
892                 }
893                 axi_data.region = &region[0];
894                 data = &axi_data;
895                 break;
896         case CMD_GENERAL:
897         case CMD_STATS_DISABLE:
898                 break;
899         default:
900                 pr_err("%s: unknown command type %d\n",
901                         __func__, cfgcmd.cmd_type);
902                 return -EINVAL;
903         }
904
905
906         if (sync->vfefn.vfe_config)
907                 rc = sync->vfefn.vfe_config(&cfgcmd, data);
908
909         return rc;
910 }
911
912 static int msm_frame_axi_cfg(struct msm_sync *sync,
913                 struct msm_vfe_cfg_cmd *cfgcmd)
914 {
915         int rc = -EIO;
916         struct axidata axi_data;
917         void *data = &axi_data;
918         struct msm_pmem_region region[8];
919         int pmem_type;
920
921         memset(&axi_data, 0, sizeof(axi_data));
922
923         switch (cfgcmd->cmd_type) {
924         case CMD_AXI_CFG_OUT1:
925                 pmem_type = MSM_PMEM_OUTPUT1;
926                 axi_data.bufnum1 =
927                         msm_pmem_region_lookup(&sync->frame, pmem_type,
928                                 &region[0], 8);
929                 if (!axi_data.bufnum1) {
930                         pr_err("%s: pmem region lookup error\n", __func__);
931                         return -EINVAL;
932                 }
933                 break;
934
935         case CMD_AXI_CFG_OUT2:
936                 pmem_type = MSM_PMEM_OUTPUT2;
937                 axi_data.bufnum2 =
938                         msm_pmem_region_lookup(&sync->frame, pmem_type,
939                                 &region[0], 8);
940                 if (!axi_data.bufnum2) {
941                         pr_err("%s: pmem region lookup error\n", __func__);
942                         return -EINVAL;
943                 }
944                 break;
945
946         case CMD_AXI_CFG_SNAP_O1_AND_O2:
947                 pmem_type = MSM_PMEM_THUMBAIL;
948                 axi_data.bufnum1 =
949                         msm_pmem_region_lookup(&sync->frame, pmem_type,
950                                 &region[0], 8);
951                 if (!axi_data.bufnum1) {
952                         pr_err("%s: pmem region lookup error\n", __func__);
953                         return -EINVAL;
954                 }
955
956                 pmem_type = MSM_PMEM_MAINIMG;
957                 axi_data.bufnum2 =
958                         msm_pmem_region_lookup(&sync->frame, pmem_type,
959                                 &region[axi_data.bufnum1], 8);
960                 if (!axi_data.bufnum2) {
961                         pr_err("%s: pmem region lookup error\n", __func__);
962                         return -EINVAL;
963                 }
964                 break;
965
966         case CMD_RAW_PICT_AXI_CFG:
967                 pmem_type = MSM_PMEM_RAW_MAINIMG;
968                 axi_data.bufnum2 =
969                         msm_pmem_region_lookup(&sync->frame, pmem_type,
970                                 &region[0], 8);
971                 if (!axi_data.bufnum2) {
972                         pr_err("%s: pmem region lookup error\n", __func__);
973                         return -EINVAL;
974                 }
975                 break;
976
977         case CMD_GENERAL:
978                 data = NULL;
979                 break;
980
981         default:
982                 pr_err("%s: unknown command type %d\n",
983                         __func__, cfgcmd->cmd_type);
984                 return -EINVAL;
985         }
986
987         axi_data.region = &region[0];
988
989         /* send the AXI configuration command to driver */
990         if (sync->vfefn.vfe_config)
991                 rc = sync->vfefn.vfe_config(cfgcmd, data);
992
993         return rc;
994 }
995
996 static int msm_get_sensor_info(struct msm_sync *sync, void __user *arg)
997 {
998         int rc = 0;
999         struct msm_camsensor_info info;
1000         struct msm_camera_sensor_info *sdata;
1001
1002         if (copy_from_user(&info,
1003                         arg,
1004                         sizeof(struct msm_camsensor_info))) {
1005                 ERR_COPY_FROM_USER();
1006                 return -EFAULT;
1007         }
1008
1009         sdata = sync->pdev->dev.platform_data;
1010         CDBG("sensor_name %s\n", sdata->sensor_name);
1011
1012         memcpy(&info.name[0],
1013                 sdata->sensor_name,
1014                 MAX_SENSOR_NAME);
1015         info.flash_enabled = sdata->flash_type != MSM_CAMERA_FLASH_NONE;
1016
1017         /* copy back to user space */
1018         if (copy_to_user((void *)arg,
1019                         &info,
1020                         sizeof(struct msm_camsensor_info))) {
1021                 ERR_COPY_TO_USER();
1022                 rc = -EFAULT;
1023         }
1024
1025         return rc;
1026 }
1027
1028 static int __msm_put_frame_buf(struct msm_sync *sync,
1029                 struct msm_frame *pb)
1030 {
1031         unsigned long pphy;
1032         struct msm_vfe_cfg_cmd cfgcmd;
1033
1034         int rc = -EIO;
1035
1036         pphy = msm_pmem_frame_vtop_lookup(sync,
1037                 pb->buffer,
1038                 pb->y_off, pb->cbcr_off, pb->fd);
1039
1040         if (pphy != 0) {
1041                 CDBG("rel: vaddr = 0x%lx, paddr = 0x%lx\n",
1042                         pb->buffer, pphy);
1043                 cfgcmd.cmd_type = CMD_FRAME_BUF_RELEASE;
1044                 cfgcmd.value    = (void *)pb;
1045                 if (sync->vfefn.vfe_config)
1046                         rc = sync->vfefn.vfe_config(&cfgcmd, &pphy);
1047         } else {
1048                 pr_err("%s: msm_pmem_frame_vtop_lookup failed\n",
1049                         __func__);
1050                 rc = -EINVAL;
1051         }
1052
1053         return rc;
1054 }
1055
1056 static int msm_put_frame_buffer(struct msm_sync *sync, void __user *arg)
1057 {
1058         struct msm_frame buf_t;
1059
1060         if (copy_from_user(&buf_t,
1061                                 arg,
1062                                 sizeof(struct msm_frame))) {
1063                 ERR_COPY_FROM_USER();
1064                 return -EFAULT;
1065         }
1066
1067         return __msm_put_frame_buf(sync, &buf_t);
1068 }
1069
1070 static int __msm_register_pmem(struct msm_sync *sync,
1071                 struct msm_pmem_info *pinfo)
1072 {
1073         int rc = 0;
1074
1075         switch (pinfo->type) {
1076         case MSM_PMEM_OUTPUT1:
1077         case MSM_PMEM_OUTPUT2:
1078         case MSM_PMEM_THUMBAIL:
1079         case MSM_PMEM_MAINIMG:
1080         case MSM_PMEM_RAW_MAINIMG:
1081                 rc = msm_pmem_table_add(&sync->frame, pinfo);
1082                 break;
1083
1084         case MSM_PMEM_AEC_AWB:
1085         case MSM_PMEM_AF:
1086                 rc = msm_pmem_table_add(&sync->stats, pinfo);
1087                 break;
1088
1089         default:
1090                 rc = -EINVAL;
1091                 break;
1092         }
1093
1094         return rc;
1095 }
1096
1097 static int msm_register_pmem(struct msm_sync *sync, void __user *arg)
1098 {
1099         struct msm_pmem_info info;
1100
1101         if (copy_from_user(&info, arg, sizeof(info))) {
1102                 ERR_COPY_FROM_USER();
1103                 return -EFAULT;
1104         }
1105
1106         return __msm_register_pmem(sync, &info);
1107 }
1108
1109 static int msm_stats_axi_cfg(struct msm_sync *sync,
1110                 struct msm_vfe_cfg_cmd *cfgcmd)
1111 {
1112         int rc = -EIO;
1113         struct axidata axi_data;
1114         void *data = &axi_data;
1115
1116         struct msm_pmem_region region[3];
1117         int pmem_type = MSM_PMEM_MAX;
1118
1119         memset(&axi_data, 0, sizeof(axi_data));
1120
1121         switch (cfgcmd->cmd_type) {
1122         case CMD_STATS_AXI_CFG:
1123                 pmem_type = MSM_PMEM_AEC_AWB;
1124                 break;
1125         case CMD_STATS_AF_AXI_CFG:
1126                 pmem_type = MSM_PMEM_AF;
1127                 break;
1128         case CMD_GENERAL:
1129                 data = NULL;
1130                 break;
1131         default:
1132                 pr_err("%s: unknown command type %d\n",
1133                         __func__, cfgcmd->cmd_type);
1134                 return -EINVAL;
1135         }
1136
1137         if (cfgcmd->cmd_type != CMD_GENERAL) {
1138                 axi_data.bufnum1 =
1139                         msm_pmem_region_lookup(&sync->stats, pmem_type,
1140                                 &region[0], NUM_WB_EXP_STAT_OUTPUT_BUFFERS);
1141                 if (!axi_data.bufnum1) {
1142                         pr_err("%s: pmem region lookup error\n", __func__);
1143                         return -EINVAL;
1144                 }
1145                 axi_data.region = &region[0];
1146         }
1147
1148         /* send the AEC/AWB STATS configuration command to driver */
1149         if (sync->vfefn.vfe_config)
1150                 rc = sync->vfefn.vfe_config(cfgcmd, &axi_data);
1151
1152         return rc;
1153 }
1154
1155 static int msm_put_stats_buffer(struct msm_sync *sync, void __user *arg)
1156 {
1157         int rc = -EIO;
1158
1159         struct msm_stats_buf buf;
1160         unsigned long pphy;
1161         struct msm_vfe_cfg_cmd cfgcmd;
1162
1163         if (copy_from_user(&buf, arg,
1164                                 sizeof(struct msm_stats_buf))) {
1165                 ERR_COPY_FROM_USER();
1166                 return -EFAULT;
1167         }
1168
1169         CDBG("msm_put_stats_buffer\n");
1170         pphy = msm_pmem_stats_vtop_lookup(sync, buf.buffer, buf.fd);
1171
1172         if (pphy != 0) {
1173                 if (buf.type == STAT_AEAW)
1174                         cfgcmd.cmd_type = CMD_STATS_BUF_RELEASE;
1175                 else if (buf.type == STAT_AF)
1176                         cfgcmd.cmd_type = CMD_STATS_AF_BUF_RELEASE;
1177                 else {
1178                         pr_err("%s: invalid buf type %d\n",
1179                                 __func__,
1180                                 buf.type);
1181                         rc = -EINVAL;
1182                         goto put_done;
1183                 }
1184
1185                 cfgcmd.value = (void *)&buf;
1186
1187                 if (sync->vfefn.vfe_config) {
1188                         rc = sync->vfefn.vfe_config(&cfgcmd, &pphy);
1189                         if (rc < 0)
1190                                 pr_err("msm_put_stats_buffer: "\
1191                                         "vfe_config err %d\n", rc);
1192                 } else
1193                         pr_err("msm_put_stats_buffer: vfe_config is NULL\n");
1194         } else {
1195                 pr_err("msm_put_stats_buffer: NULL physical address\n");
1196                 rc = -EINVAL;
1197         }
1198
1199 put_done:
1200         return rc;
1201 }
1202
1203 static int msm_axi_config(struct msm_sync *sync, void __user *arg)
1204 {
1205         struct msm_vfe_cfg_cmd cfgcmd;
1206
1207         if (copy_from_user(&cfgcmd, arg, sizeof(cfgcmd))) {
1208                 ERR_COPY_FROM_USER();
1209                 return -EFAULT;
1210         }
1211
1212         switch (cfgcmd.cmd_type) {
1213         case CMD_AXI_CFG_OUT1:
1214         case CMD_AXI_CFG_OUT2:
1215         case CMD_AXI_CFG_SNAP_O1_AND_O2:
1216         case CMD_RAW_PICT_AXI_CFG:
1217                 return msm_frame_axi_cfg(sync, &cfgcmd);
1218
1219         case CMD_STATS_AXI_CFG:
1220         case CMD_STATS_AF_AXI_CFG:
1221                 return msm_stats_axi_cfg(sync, &cfgcmd);
1222
1223         default:
1224                 pr_err("%s: unknown command type %d\n",
1225                         __func__,
1226                         cfgcmd.cmd_type);
1227                 return -EINVAL;
1228         }
1229
1230         return 0;
1231 }
1232
1233 static int __msm_get_pic(struct msm_sync *sync, struct msm_ctrl_cmd *ctrl)
1234 {
1235         unsigned long flags;
1236         int rc = 0;
1237         int tm;
1238
1239         struct msm_queue_cmd *qcmd = NULL;
1240
1241         tm = (int)ctrl->timeout_ms;
1242
1243         rc = wait_event_interruptible_timeout(
1244                         sync->pict_frame_wait,
1245                         !list_empty_careful(&sync->pict_frame_q),
1246                         msecs_to_jiffies(tm));
1247         if (list_empty_careful(&sync->pict_frame_q)) {
1248                 if (rc == 0)
1249                         return -ETIMEDOUT;
1250                 if (rc < 0) {
1251                         pr_err("msm_camera_get_picture, rc = %d\n", rc);
1252                         return rc;
1253                 }
1254         }
1255
1256         spin_lock_irqsave(&sync->pict_frame_q_lock, flags);
1257         BUG_ON(list_empty(&sync->pict_frame_q));
1258         qcmd = list_first_entry(&sync->pict_frame_q,
1259                         struct msm_queue_cmd, list);
1260         list_del_init(&qcmd->list);
1261         spin_unlock_irqrestore(&sync->pict_frame_q_lock, flags);
1262
1263         if (qcmd->command != NULL) {
1264                 struct msm_ctrl_cmd *q =
1265                         (struct msm_ctrl_cmd *)qcmd->command;
1266                 ctrl->type = q->type;
1267                 ctrl->status = q->status;
1268         } else {
1269                 ctrl->type = -1;
1270                 ctrl->status = -1;
1271         }
1272
1273         kfree(qcmd);
1274         return rc;
1275 }
1276
1277 static int msm_get_pic(struct msm_sync *sync, void __user *arg)
1278 {
1279         struct msm_ctrl_cmd ctrlcmd_t;
1280         int rc;
1281
1282         if (copy_from_user(&ctrlcmd_t,
1283                                 arg,
1284                                 sizeof(struct msm_ctrl_cmd))) {
1285                 ERR_COPY_FROM_USER();
1286                 return -EFAULT;
1287         }
1288
1289         rc = __msm_get_pic(sync, &ctrlcmd_t);
1290         if (rc < 0)
1291                 return rc;
1292
1293         if (sync->croplen) {
1294                 if (ctrlcmd_t.length < sync->croplen) {
1295                         pr_err("msm_get_pic: invalid len %d\n",
1296                                 ctrlcmd_t.length);
1297                         return -EINVAL;
1298                 }
1299                 if (copy_to_user(ctrlcmd_t.value,
1300                                 sync->cropinfo,
1301                                 sync->croplen)) {
1302                         ERR_COPY_TO_USER();
1303                         return -EFAULT;
1304                 }
1305         }
1306
1307         if (copy_to_user((void *)arg,
1308                 &ctrlcmd_t,
1309                 sizeof(struct msm_ctrl_cmd))) {
1310                 ERR_COPY_TO_USER();
1311                 return -EFAULT;
1312         }
1313         return 0;
1314 }
1315
1316 static int msm_set_crop(struct msm_sync *sync, void __user *arg)
1317 {
1318         struct crop_info crop;
1319
1320         if (copy_from_user(&crop,
1321                                 arg,
1322                                 sizeof(struct crop_info))) {
1323                 ERR_COPY_FROM_USER();
1324                 return -EFAULT;
1325         }
1326
1327         if (!sync->croplen) {
1328                 sync->cropinfo = kmalloc(crop.len, GFP_KERNEL);
1329                 if (!sync->cropinfo)
1330                         return -ENOMEM;
1331         } else if (sync->croplen < crop.len)
1332                 return -EINVAL;
1333
1334         if (copy_from_user(sync->cropinfo,
1335                                 crop.info,
1336                                 crop.len)) {
1337                 ERR_COPY_FROM_USER();
1338                 kfree(sync->cropinfo);
1339                 return -EFAULT;
1340         }
1341
1342         sync->croplen = crop.len;
1343
1344         return 0;
1345 }
1346
1347 static int msm_pict_pp_done(struct msm_sync *sync, void __user *arg)
1348 {
1349         struct msm_ctrl_cmd udata;
1350         struct msm_ctrl_cmd *ctrlcmd = NULL;
1351         struct msm_queue_cmd *qcmd = NULL;
1352         unsigned long flags;
1353         int rc = 0;
1354
1355         if (!sync->pict_pp)
1356                 return -EINVAL;
1357
1358         if (copy_from_user(&udata, arg, sizeof(struct msm_ctrl_cmd))) {
1359                 ERR_COPY_FROM_USER();
1360                 rc = -EFAULT;
1361                 goto pp_fail;
1362         }
1363
1364         qcmd = kmalloc(sizeof(struct msm_queue_cmd) +
1365                         sizeof(struct msm_ctrl_cmd),
1366                         GFP_KERNEL);
1367         if (!qcmd) {
1368                 rc = -ENOMEM;
1369                 goto pp_fail;
1370         }
1371
1372         qcmd->type = MSM_CAM_Q_VFE_MSG;
1373         qcmd->command = ctrlcmd = (struct msm_ctrl_cmd *)(qcmd + 1);
1374         memset(ctrlcmd, 0, sizeof(struct msm_ctrl_cmd));
1375         ctrlcmd->type = udata.type;
1376         ctrlcmd->status = udata.status;
1377
1378         spin_lock_irqsave(&sync->pict_frame_q_lock, flags);
1379         list_add_tail(&qcmd->list, &sync->pict_frame_q);
1380         spin_unlock_irqrestore(&sync->pict_frame_q_lock, flags);
1381         wake_up(&sync->pict_frame_wait);
1382
1383 pp_fail:
1384         return rc;
1385 }
1386
1387 static long msm_ioctl_common(struct msm_device *pmsm,
1388                 unsigned int cmd,
1389                 void __user *argp)
1390 {
1391         CDBG("msm_ioctl_common\n");
1392         switch (cmd) {
1393         case MSM_CAM_IOCTL_REGISTER_PMEM:
1394                 return msm_register_pmem(pmsm->sync, argp);
1395         case MSM_CAM_IOCTL_UNREGISTER_PMEM:
1396                 return msm_pmem_table_del(pmsm->sync, argp);
1397         default:
1398                 return -EINVAL;
1399         }
1400 }
1401
1402 static long msm_ioctl_config(struct file *filep, unsigned int cmd,
1403         unsigned long arg)
1404 {
1405         int rc = -EINVAL;
1406         void __user *argp = (void __user *)arg;
1407         struct msm_device *pmsm = filep->private_data;
1408
1409         CDBG("msm_ioctl_config cmd = %d\n", _IOC_NR(cmd));
1410
1411         switch (cmd) {
1412         case MSM_CAM_IOCTL_GET_SENSOR_INFO:
1413                 rc = msm_get_sensor_info(pmsm->sync, argp);
1414                 break;
1415
1416         case MSM_CAM_IOCTL_CONFIG_VFE:
1417                 /* Coming from config thread for update */
1418                 rc = msm_config_vfe(pmsm->sync, argp);
1419                 break;
1420
1421         case MSM_CAM_IOCTL_GET_STATS:
1422                 /* Coming from config thread wait
1423                  * for vfe statistics and control requests */
1424                 rc = msm_get_stats(pmsm->sync, argp);
1425                 break;
1426
1427         case MSM_CAM_IOCTL_ENABLE_VFE:
1428                 /* This request comes from control thread:
1429                  * enable either QCAMTASK or VFETASK */
1430                 rc = msm_enable_vfe(pmsm->sync, argp);
1431                 break;
1432
1433         case MSM_CAM_IOCTL_DISABLE_VFE:
1434                 /* This request comes from control thread:
1435                  * disable either QCAMTASK or VFETASK */
1436                 rc = msm_disable_vfe(pmsm->sync, argp);
1437                 break;
1438
1439         case MSM_CAM_IOCTL_VFE_APPS_RESET:
1440                 msm_camio_vfe_blk_reset();
1441                 rc = 0;
1442                 break;
1443
1444         case MSM_CAM_IOCTL_RELEASE_STATS_BUFFER:
1445                 rc = msm_put_stats_buffer(pmsm->sync, argp);
1446                 break;
1447
1448         case MSM_CAM_IOCTL_AXI_CONFIG:
1449                 rc = msm_axi_config(pmsm->sync, argp);
1450                 break;
1451
1452         case MSM_CAM_IOCTL_SET_CROP:
1453                 rc = msm_set_crop(pmsm->sync, argp);
1454                 break;
1455
1456         case MSM_CAM_IOCTL_PICT_PP: {
1457                 uint8_t enable;
1458                 if (copy_from_user(&enable, argp, sizeof(enable))) {
1459                         ERR_COPY_FROM_USER();
1460                         rc = -EFAULT;
1461                 } else {
1462                         pmsm->sync->pict_pp = enable;
1463                         rc = 0;
1464                 }
1465                 break;
1466         }
1467
1468         case MSM_CAM_IOCTL_PICT_PP_DONE:
1469                 rc = msm_pict_pp_done(pmsm->sync, argp);
1470                 break;
1471
1472         case MSM_CAM_IOCTL_SENSOR_IO_CFG:
1473                 rc = pmsm->sync->sctrl.s_config(argp);
1474                 break;
1475
1476         case MSM_CAM_IOCTL_FLASH_LED_CFG: {
1477                 uint32_t led_state;
1478                 if (copy_from_user(&led_state, argp, sizeof(led_state))) {
1479                         ERR_COPY_FROM_USER();
1480                         rc = -EFAULT;
1481                 } else
1482                         rc = msm_camera_flash_set_led_state(led_state);
1483                 break;
1484         }
1485
1486         default:
1487                 rc = msm_ioctl_common(pmsm, cmd, argp);
1488                 break;
1489         }
1490
1491         CDBG("msm_ioctl_config cmd = %d DONE\n", _IOC_NR(cmd));
1492         return rc;
1493 }
1494
1495 static int msm_unblock_poll_frame(struct msm_sync *);
1496
1497 static long msm_ioctl_frame(struct file *filep, unsigned int cmd,
1498         unsigned long arg)
1499 {
1500         int rc = -EINVAL;
1501         void __user *argp = (void __user *)arg;
1502         struct msm_device *pmsm = filep->private_data;
1503
1504
1505         switch (cmd) {
1506         case MSM_CAM_IOCTL_GETFRAME:
1507                 /* Coming from frame thread to get frame
1508                  * after SELECT is done */
1509                 rc = msm_get_frame(pmsm->sync, argp);
1510                 break;
1511         case MSM_CAM_IOCTL_RELEASE_FRAME_BUFFER:
1512                 rc = msm_put_frame_buffer(pmsm->sync, argp);
1513                 break;
1514         case MSM_CAM_IOCTL_UNBLOCK_POLL_FRAME:
1515                 rc = msm_unblock_poll_frame(pmsm->sync);
1516                 break;
1517         default:
1518                 break;
1519         }
1520
1521         return rc;
1522 }
1523
1524
1525 static long msm_ioctl_control(struct file *filep, unsigned int cmd,
1526         unsigned long arg)
1527 {
1528         int rc = -EINVAL;
1529         void __user *argp = (void __user *)arg;
1530         struct msm_control_device *ctrl_pmsm = filep->private_data;
1531         struct msm_device *pmsm = ctrl_pmsm->pmsm;
1532
1533         switch (cmd) {
1534         case MSM_CAM_IOCTL_CTRL_COMMAND:
1535                 /* Coming from control thread, may need to wait for
1536                  * command status */
1537                 rc = msm_control(ctrl_pmsm, 1, argp);
1538                 break;
1539         case MSM_CAM_IOCTL_CTRL_COMMAND_2:
1540                 /* Sends a message, returns immediately */
1541                 rc = msm_control(ctrl_pmsm, 0, argp);
1542                 break;
1543         case MSM_CAM_IOCTL_CTRL_CMD_DONE:
1544                 /* Config thread calls the control thread to notify it
1545                  * of the result of a MSM_CAM_IOCTL_CTRL_COMMAND.
1546                  */
1547                 rc = msm_ctrl_cmd_done(ctrl_pmsm, argp);
1548                 break;
1549         case MSM_CAM_IOCTL_GET_PICTURE:
1550                 rc = msm_get_pic(pmsm->sync, argp);
1551                 break;
1552         default:
1553                 rc = msm_ioctl_common(pmsm, cmd, argp);
1554                 break;
1555         }
1556
1557         return rc;
1558 }
1559
1560 static int __msm_release(struct msm_sync *sync)
1561 {
1562         struct msm_pmem_region *region;
1563         struct hlist_node *hnode;
1564         struct hlist_node *n;
1565
1566         mutex_lock(&sync->lock);
1567         if (sync->opencnt)
1568                 sync->opencnt--;
1569
1570         if (!sync->opencnt) {
1571                 /* need to clean up system resource */
1572                 if (sync->vfefn.vfe_release)
1573                         sync->vfefn.vfe_release(sync->pdev);
1574
1575                 if (sync->cropinfo) {
1576                         kfree(sync->cropinfo);
1577                         sync->cropinfo = NULL;
1578                         sync->croplen = 0;
1579                 }
1580
1581                 hlist_for_each_entry_safe(region, hnode, n,
1582                                 &sync->frame, list) {
1583                         hlist_del(hnode);
1584                         put_pmem_file(region->file);
1585                         kfree(region);
1586                 }
1587
1588                 hlist_for_each_entry_safe(region, hnode, n,
1589                                 &sync->stats, list) {
1590                         hlist_del(hnode);
1591                         put_pmem_file(region->file);
1592                         kfree(region);
1593                 }
1594
1595                 MSM_DRAIN_QUEUE(sync, msg_event_q);
1596                 MSM_DRAIN_QUEUE(sync, prev_frame_q);
1597                 MSM_DRAIN_QUEUE(sync, pict_frame_q);
1598
1599                 sync->sctrl.s_release();
1600
1601                 sync->apps_id = NULL;
1602                 CDBG("msm_release completed!\n");
1603         }
1604         mutex_unlock(&sync->lock);
1605
1606         return 0;
1607 }
1608
1609 static int msm_release_config(struct inode *node, struct file *filep)
1610 {
1611         int rc;
1612         struct msm_device *pmsm = filep->private_data;
1613         printk("msm_camera: RELEASE %s\n", filep->f_path.dentry->d_name.name);
1614         rc = __msm_release(pmsm->sync);
1615         atomic_set(&pmsm->opened, 0);
1616         return rc;
1617 }
1618
1619 static int msm_release_control(struct inode *node, struct file *filep)
1620 {
1621         int rc;
1622         struct msm_control_device *ctrl_pmsm = filep->private_data;
1623         struct msm_device *pmsm = ctrl_pmsm->pmsm;
1624         printk(KERN_INFO "msm_camera: RELEASE %s\n",
1625                                         filep->f_path.dentry->d_name.name);
1626         rc = __msm_release(pmsm->sync);
1627         if (!rc) {
1628                 MSM_DRAIN_QUEUE(&ctrl_pmsm->ctrl_q, ctrl_status_q);
1629                 MSM_DRAIN_QUEUE(pmsm->sync, pict_frame_q);
1630         }
1631         kfree(ctrl_pmsm);
1632         return rc;
1633 }
1634
1635 static int msm_release_frame(struct inode *node, struct file *filep)
1636 {
1637         int rc;
1638         struct msm_device *pmsm = filep->private_data;
1639         printk(KERN_INFO "msm_camera: RELEASE %s\n",
1640                                         filep->f_path.dentry->d_name.name);
1641         rc = __msm_release(pmsm->sync);
1642         if (!rc) {
1643                 MSM_DRAIN_QUEUE(pmsm->sync, prev_frame_q);
1644                 atomic_set(&pmsm->opened, 0);
1645         }
1646         return rc;
1647 }
1648
1649 static int msm_unblock_poll_frame(struct msm_sync *sync)
1650 {
1651         unsigned long flags;
1652         CDBG("msm_unblock_poll_frame\n");
1653         spin_lock_irqsave(&sync->prev_frame_q_lock, flags);
1654         sync->unblock_poll_frame = 1;
1655         wake_up(&sync->prev_frame_wait);
1656         spin_unlock_irqrestore(&sync->prev_frame_q_lock, flags);
1657         return 0;
1658 }
1659
1660 static unsigned int __msm_poll_frame(struct msm_sync *sync,
1661                 struct file *filep,
1662                 struct poll_table_struct *pll_table)
1663 {
1664         int rc = 0;
1665         unsigned long flags;
1666
1667         poll_wait(filep, &sync->prev_frame_wait, pll_table);
1668
1669         spin_lock_irqsave(&sync->prev_frame_q_lock, flags);
1670         if (!list_empty_careful(&sync->prev_frame_q))
1671                 /* frame ready */
1672                 rc = POLLIN | POLLRDNORM;
1673         if (sync->unblock_poll_frame) {
1674                 CDBG("%s: sync->unblock_poll_frame is true\n", __func__);
1675                 rc |= POLLPRI;
1676                 sync->unblock_poll_frame = 0;
1677         }
1678         spin_unlock_irqrestore(&sync->prev_frame_q_lock, flags);
1679
1680         return rc;
1681 }
1682
1683 static unsigned int msm_poll_frame(struct file *filep,
1684         struct poll_table_struct *pll_table)
1685 {
1686         struct msm_device *pmsm = filep->private_data;
1687         return __msm_poll_frame(pmsm->sync, filep, pll_table);
1688 }
1689
1690 /*
1691  * This function executes in interrupt context.
1692  */
1693
1694 static void *msm_vfe_sync_alloc(int size,
1695                         void *syncdata __attribute__((unused)))
1696 {
1697         struct msm_queue_cmd *qcmd =
1698                 kmalloc(sizeof(struct msm_queue_cmd) + size, GFP_ATOMIC);
1699         return qcmd ? qcmd + 1 : NULL;
1700 }
1701
1702 /*
1703  * This function executes in interrupt context.
1704  */
1705
1706 static void msm_vfe_sync(struct msm_vfe_resp *vdata,
1707                 enum msm_queue qtype, void *syncdata)
1708 {
1709         struct msm_queue_cmd *qcmd = NULL;
1710         struct msm_queue_cmd *qcmd_frame = NULL;
1711         struct msm_vfe_phy_info *fphy;
1712
1713         unsigned long flags;
1714         struct msm_sync *sync = (struct msm_sync *)syncdata;
1715         if (!sync) {
1716                 pr_err("msm_camera: no context in dsp callback.\n");
1717                 return;
1718         }
1719
1720         qcmd = ((struct msm_queue_cmd *)vdata) - 1;
1721         qcmd->type = qtype;
1722
1723         if (qtype == MSM_CAM_Q_VFE_MSG) {
1724                 switch (vdata->type) {
1725                 case VFE_MSG_OUTPUT1:
1726                 case VFE_MSG_OUTPUT2:
1727                         qcmd_frame =
1728                                 kmalloc(sizeof(struct msm_queue_cmd) +
1729                                         sizeof(struct msm_vfe_phy_info),
1730                                         GFP_ATOMIC);
1731                         if (!qcmd_frame)
1732                                 goto mem_fail;
1733                         fphy = (struct msm_vfe_phy_info *)(qcmd_frame + 1);
1734                         *fphy = vdata->phy;
1735
1736                         qcmd_frame->type = MSM_CAM_Q_VFE_MSG;
1737                         qcmd_frame->command = fphy;
1738
1739                         CDBG("qcmd_frame= 0x%x phy_y= 0x%x, phy_cbcr= 0x%x\n",
1740                                 (int) qcmd_frame, fphy->y_phy, fphy->cbcr_phy);
1741
1742                         spin_lock_irqsave(&sync->prev_frame_q_lock, flags);
1743                         list_add_tail(&qcmd_frame->list, &sync->prev_frame_q);
1744                         wake_up(&sync->prev_frame_wait);
1745                         spin_unlock_irqrestore(&sync->prev_frame_q_lock, flags);
1746                         CDBG("woke up frame thread\n");
1747                         break;
1748                 case VFE_MSG_SNAPSHOT:
1749                         if (sync->pict_pp)
1750                                 break;
1751
1752                         CDBG("snapshot pp = %d\n", sync->pict_pp);
1753                         qcmd_frame =
1754                                 kmalloc(sizeof(struct msm_queue_cmd),
1755                                         GFP_ATOMIC);
1756                         if (!qcmd_frame)
1757                                 goto mem_fail;
1758                         qcmd_frame->type = MSM_CAM_Q_VFE_MSG;
1759                         qcmd_frame->command = NULL;
1760                                 spin_lock_irqsave(&sync->pict_frame_q_lock,
1761                                 flags);
1762                         list_add_tail(&qcmd_frame->list, &sync->pict_frame_q);
1763                         wake_up(&sync->pict_frame_wait);
1764                         spin_unlock_irqrestore(&sync->pict_frame_q_lock, flags);
1765                         CDBG("woke up picture thread\n");
1766                         break;
1767                 default:
1768                         CDBG("%s: qtype = %d not handled\n",
1769                                 __func__, vdata->type);
1770                         break;
1771                 }
1772         }
1773
1774         qcmd->command = (void *)vdata;
1775         CDBG("vdata->type = %d\n", vdata->type);
1776
1777         spin_lock_irqsave(&sync->msg_event_q_lock, flags);
1778         list_add_tail(&qcmd->list, &sync->msg_event_q);
1779         wake_up(&sync->msg_event_wait);
1780         spin_unlock_irqrestore(&sync->msg_event_q_lock, flags);
1781         CDBG("woke up config thread\n");
1782         return;
1783
1784 mem_fail:
1785         kfree(qcmd);
1786 }
1787
1788 static struct msm_vfe_callback msm_vfe_s = {
1789         .vfe_resp = msm_vfe_sync,
1790         .vfe_alloc = msm_vfe_sync_alloc,
1791 };
1792
1793 static int __msm_open(struct msm_sync *sync, const char *const apps_id)
1794 {
1795         int rc = 0;
1796
1797         mutex_lock(&sync->lock);
1798         if (sync->apps_id && strcmp(sync->apps_id, apps_id)) {
1799                 pr_err("msm_camera(%s): sensor %s is already opened for %s\n",
1800                         apps_id,
1801                         sync->sdata->sensor_name,
1802                         sync->apps_id);
1803                 rc = -EBUSY;
1804                 goto msm_open_done;
1805         }
1806
1807         sync->apps_id = apps_id;
1808
1809         if (!sync->opencnt) {
1810
1811                 msm_camvfe_fn_init(&sync->vfefn, sync);
1812                 if (sync->vfefn.vfe_init) {
1813                         rc = sync->vfefn.vfe_init(&msm_vfe_s,
1814                                 sync->pdev);
1815                         if (rc < 0) {
1816                                 pr_err("vfe_init failed at %d\n", rc);
1817                                 goto msm_open_done;
1818                         }
1819                         rc = sync->sctrl.s_init(sync->sdata);
1820                         if (rc < 0) {
1821                                 pr_err("sensor init failed: %d\n", rc);
1822                                 goto msm_open_done;
1823                         }
1824                 } else {
1825                         pr_err("no sensor init func\n");
1826                         rc = -ENODEV;
1827                         goto msm_open_done;
1828                 }
1829
1830                 if (rc >= 0) {
1831                         INIT_HLIST_HEAD(&sync->frame);
1832                         INIT_HLIST_HEAD(&sync->stats);
1833                         sync->unblock_poll_frame = 0;
1834                 }
1835         }
1836         sync->opencnt++;
1837
1838 msm_open_done:
1839         mutex_unlock(&sync->lock);
1840         return rc;
1841 }
1842
1843 static int msm_open_common(struct inode *inode, struct file *filep,
1844                            int once)
1845 {
1846         int rc;
1847         struct msm_device *pmsm =
1848                 container_of(inode->i_cdev, struct msm_device, cdev);
1849
1850         CDBG("msm_camera: open %s\n", filep->f_path.dentry->d_name.name);
1851
1852         if (atomic_cmpxchg(&pmsm->opened, 0, 1) && once) {
1853                 pr_err("msm_camera: %s is already opened.\n",
1854                         filep->f_path.dentry->d_name.name);
1855                 return -EBUSY;
1856         }
1857
1858         rc = nonseekable_open(inode, filep);
1859         if (rc < 0) {
1860                 pr_err("msm_open: nonseekable_open error %d\n", rc);
1861                 return rc;
1862         }
1863
1864         rc = __msm_open(pmsm->sync, MSM_APPS_ID_PROP);
1865         if (rc < 0)
1866                 return rc;
1867
1868         filep->private_data = pmsm;
1869
1870         CDBG("msm_open() open: rc = %d\n", rc);
1871         return rc;
1872 }
1873
1874 static int msm_open(struct inode *inode, struct file *filep)
1875 {
1876         return msm_open_common(inode, filep, 1);
1877 }
1878
1879 static int msm_open_control(struct inode *inode, struct file *filep)
1880 {
1881         int rc;
1882
1883         struct msm_control_device *ctrl_pmsm =
1884                 kmalloc(sizeof(struct msm_control_device), GFP_KERNEL);
1885         if (!ctrl_pmsm)
1886                 return -ENOMEM;
1887
1888         rc = msm_open_common(inode, filep, 0);
1889         if (rc < 0)
1890                 return rc;
1891
1892         ctrl_pmsm->pmsm = filep->private_data;
1893         filep->private_data = ctrl_pmsm;
1894         spin_lock_init(&ctrl_pmsm->ctrl_q.ctrl_status_q_lock);
1895         INIT_LIST_HEAD(&ctrl_pmsm->ctrl_q.ctrl_status_q);
1896         init_waitqueue_head(&ctrl_pmsm->ctrl_q.ctrl_status_wait);
1897
1898         CDBG("msm_open() open: rc = %d\n", rc);
1899         return rc;
1900 }
1901
1902 static int __msm_v4l2_control(struct msm_sync *sync,
1903                 struct msm_ctrl_cmd *out)
1904 {
1905         int rc = 0;
1906
1907         struct msm_queue_cmd *qcmd = NULL, *rcmd = NULL;
1908         struct msm_ctrl_cmd *ctrl;
1909         struct msm_control_device_queue FIXME;
1910
1911         /* wake up config thread, 4 is for V4L2 application */
1912         qcmd = kmalloc(sizeof(struct msm_queue_cmd), GFP_KERNEL);
1913         if (!qcmd) {
1914                 pr_err("msm_control: cannot allocate buffer\n");
1915                 rc = -ENOMEM;
1916                 goto end;
1917         }
1918         qcmd->type = MSM_CAM_Q_V4L2_REQ;
1919         qcmd->command = out;
1920
1921         rcmd = __msm_control(sync, &FIXME, qcmd, out->timeout_ms);
1922         if (IS_ERR(rcmd)) {
1923                 rc = PTR_ERR(rcmd);
1924                 goto end;
1925         }
1926
1927         ctrl = (struct msm_ctrl_cmd *)(rcmd->command);
1928         /* FIXME: we should just set out->length = ctrl->length; */
1929         BUG_ON(out->length < ctrl->length);
1930         memcpy(out->value, ctrl->value, ctrl->length);
1931
1932 end:
1933         if (rcmd)
1934                 kfree(rcmd);
1935         CDBG("__msm_v4l2_control: end rc = %d\n", rc);
1936         return rc;
1937 }
1938
1939 static const struct file_operations msm_fops_config = {
1940         .owner = THIS_MODULE,
1941         .open = msm_open,
1942         .unlocked_ioctl = msm_ioctl_config,
1943         .release = msm_release_config,
1944 };
1945
1946 static const struct file_operations msm_fops_control = {
1947         .owner = THIS_MODULE,
1948         .open = msm_open_control,
1949         .unlocked_ioctl = msm_ioctl_control,
1950         .release = msm_release_control,
1951 };
1952
1953 static const struct file_operations msm_fops_frame = {
1954         .owner = THIS_MODULE,
1955         .open = msm_open,
1956         .unlocked_ioctl = msm_ioctl_frame,
1957         .release = msm_release_frame,
1958         .poll = msm_poll_frame,
1959 };
1960
1961 static int msm_setup_cdev(struct msm_device *msm,
1962                         int node,
1963                         dev_t devno,
1964                         const char *suffix,
1965                         const struct file_operations *fops)
1966 {
1967         int rc = -ENODEV;
1968
1969         struct device *device =
1970                 device_create(msm_class, NULL,
1971                         devno, NULL,
1972                         "%s%d", suffix, node);
1973
1974         if (IS_ERR(device)) {
1975                 rc = PTR_ERR(device);
1976                 pr_err("msm_camera: error creating device: %d\n", rc);
1977                 return rc;
1978         }
1979
1980         cdev_init(&msm->cdev, fops);
1981         msm->cdev.owner = THIS_MODULE;
1982
1983         rc = cdev_add(&msm->cdev, devno, 1);
1984         if (rc < 0) {
1985                 pr_err("msm_camera: error adding cdev: %d\n", rc);
1986                 device_destroy(msm_class, devno);
1987                 return rc;
1988         }
1989
1990         return rc;
1991 }
1992
1993 static int msm_tear_down_cdev(struct msm_device *msm, dev_t devno)
1994 {
1995         cdev_del(&msm->cdev);
1996         device_destroy(msm_class, devno);
1997         return 0;
1998 }
1999
2000 int msm_v4l2_register(struct msm_v4l2_driver *drv)
2001 {
2002         /* FIXME: support multiple sensors */
2003         if (list_empty(&msm_sensors))
2004                 return -ENODEV;
2005
2006         drv->sync = list_first_entry(&msm_sensors, struct msm_sync, list);
2007         drv->open      = __msm_open;
2008         drv->release   = __msm_release;
2009         drv->ctrl      = __msm_v4l2_control;
2010         drv->reg_pmem  = __msm_register_pmem;
2011         drv->get_frame = __msm_get_frame;
2012         drv->put_frame = __msm_put_frame_buf;
2013         drv->get_pict  = __msm_get_pic;
2014         drv->drv_poll  = __msm_poll_frame;
2015
2016         return 0;
2017 }
2018 EXPORT_SYMBOL(msm_v4l2_register);
2019
2020 int msm_v4l2_unregister(struct msm_v4l2_driver *drv)
2021 {
2022         drv->sync = NULL;
2023         return 0;
2024 }
2025 EXPORT_SYMBOL(msm_v4l2_unregister);
2026
2027 static int msm_sync_init(struct msm_sync *sync,
2028                 struct platform_device *pdev,
2029                 int (*sensor_probe)(const struct msm_camera_sensor_info *,
2030                                 struct msm_sensor_ctrl *))
2031 {
2032         int rc = 0;
2033         struct msm_sensor_ctrl sctrl;
2034         sync->sdata = pdev->dev.platform_data;
2035
2036         spin_lock_init(&sync->msg_event_q_lock);
2037         INIT_LIST_HEAD(&sync->msg_event_q);
2038         init_waitqueue_head(&sync->msg_event_wait);
2039
2040         spin_lock_init(&sync->prev_frame_q_lock);
2041         INIT_LIST_HEAD(&sync->prev_frame_q);
2042         init_waitqueue_head(&sync->prev_frame_wait);
2043
2044         spin_lock_init(&sync->pict_frame_q_lock);
2045         INIT_LIST_HEAD(&sync->pict_frame_q);
2046         init_waitqueue_head(&sync->pict_frame_wait);
2047
2048         rc = msm_camio_probe_on(pdev);
2049         if (rc < 0)
2050                 return rc;
2051         rc = sensor_probe(sync->sdata, &sctrl);
2052         if (rc >= 0) {
2053                 sync->pdev = pdev;
2054                 sync->sctrl = sctrl;
2055         }
2056         msm_camio_probe_off(pdev);
2057         if (rc < 0) {
2058                 pr_err("msm_camera: failed to initialize %s\n",
2059                         sync->sdata->sensor_name);
2060                 return rc;
2061         }
2062
2063         sync->opencnt = 0;
2064         mutex_init(&sync->lock);
2065         CDBG("initialized %s\n", sync->sdata->sensor_name);
2066         return rc;
2067 }
2068
2069 static int msm_sync_destroy(struct msm_sync *sync)
2070 {
2071         return 0;
2072 }
2073
2074 static int msm_device_init(struct msm_device *pmsm,
2075                 struct msm_sync *sync,
2076                 int node)
2077 {
2078         int dev_num = 3 * node;
2079         int rc = msm_setup_cdev(pmsm, node,
2080                 MKDEV(MAJOR(msm_devno), dev_num),
2081                 "control", &msm_fops_control);
2082         if (rc < 0) {
2083                 pr_err("error creating control node: %d\n", rc);
2084                 return rc;
2085         }
2086
2087         rc = msm_setup_cdev(pmsm + 1, node,
2088                 MKDEV(MAJOR(msm_devno), dev_num + 1),
2089                 "config", &msm_fops_config);
2090         if (rc < 0) {
2091                 pr_err("error creating config node: %d\n", rc);
2092                 msm_tear_down_cdev(pmsm, MKDEV(MAJOR(msm_devno),
2093                                 dev_num));
2094                 return rc;
2095         }
2096
2097         rc = msm_setup_cdev(pmsm + 2, node,
2098                 MKDEV(MAJOR(msm_devno), dev_num + 2),
2099                 "frame", &msm_fops_frame);
2100         if (rc < 0) {
2101                 pr_err("error creating frame node: %d\n", rc);
2102                 msm_tear_down_cdev(pmsm,
2103                         MKDEV(MAJOR(msm_devno), dev_num));
2104                 msm_tear_down_cdev(pmsm + 1,
2105                         MKDEV(MAJOR(msm_devno), dev_num + 1));
2106                 return rc;
2107         }
2108
2109         atomic_set(&pmsm[0].opened, 0);
2110         atomic_set(&pmsm[1].opened, 0);
2111         atomic_set(&pmsm[2].opened, 0);
2112
2113         pmsm[0].sync = sync;
2114         pmsm[1].sync = sync;
2115         pmsm[2].sync = sync;
2116
2117         return rc;
2118 }
2119
2120 int msm_camera_drv_start(struct platform_device *dev,
2121                 int (*sensor_probe)(const struct msm_camera_sensor_info *,
2122                         struct msm_sensor_ctrl *))
2123 {
2124         struct msm_device *pmsm = NULL;
2125         struct msm_sync *sync;
2126         int rc = -ENODEV;
2127         static int camera_node;
2128
2129         if (camera_node >= MSM_MAX_CAMERA_SENSORS) {
2130                 pr_err("msm_camera: too many camera sensors\n");
2131                 return rc;
2132         }
2133
2134         if (!msm_class) {
2135                 /* There are three device nodes per sensor */
2136                 rc = alloc_chrdev_region(&msm_devno, 0,
2137                                 3 * MSM_MAX_CAMERA_SENSORS,
2138                                 "msm_camera");
2139                 if (rc < 0) {
2140                         pr_err("msm_camera: failed to allocate chrdev: %d\n",
2141                                 rc);
2142                         return rc;
2143                 }
2144
2145                 msm_class = class_create(THIS_MODULE, "msm_camera");
2146                 if (IS_ERR(msm_class)) {
2147                         rc = PTR_ERR(msm_class);
2148                         pr_err("msm_camera: create device class failed: %d\n",
2149                                 rc);
2150                         return rc;
2151                 }
2152         }
2153
2154         pmsm = kzalloc(sizeof(struct msm_device) * 3 +
2155                         sizeof(struct msm_sync), GFP_ATOMIC);
2156         if (!pmsm)
2157                 return -ENOMEM;
2158         sync = (struct msm_sync *)(pmsm + 3);
2159
2160         rc = msm_sync_init(sync, dev, sensor_probe);
2161         if (rc < 0) {
2162                 kfree(pmsm);
2163                 return rc;
2164         }
2165
2166         CDBG("setting camera node %d\n", camera_node);
2167         rc = msm_device_init(pmsm, sync, camera_node);
2168         if (rc < 0) {
2169                 msm_sync_destroy(sync);
2170                 kfree(pmsm);
2171                 return rc;
2172         }
2173
2174         camera_node++;
2175         list_add(&sync->list, &msm_sensors);
2176         return rc;
2177 }
2178 EXPORT_SYMBOL(msm_camera_drv_start);