1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18#include <linux/dma-mapping.h>
19#include <linux/slab.h>
20#include <linux/uaccess.h>
21
22#include "isp.h"
23
24#define ISP_STAT_USES_DMAENGINE(stat) ((stat)->dma_ch != NULL)
25
26
27
28
29
30#define MAGIC_SIZE 16
31#define MAGIC_NUM 0x55
32
33
34#define AF_EXTRA_DATA OMAP3ISP_AF_PAXEL_SIZE
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52#define NUM_H3A_RECOVER_BUFS 10
53
54
55
56
57
58#define IS_H3A_AF(stat) ((stat) == &(stat)->isp->isp_af)
59#define IS_H3A_AEWB(stat) ((stat) == &(stat)->isp->isp_aewb)
60#define IS_H3A(stat) (IS_H3A_AF(stat) || IS_H3A_AEWB(stat))
61
62static void __isp_stat_buf_sync_magic(struct ispstat *stat,
63 struct ispstat_buffer *buf,
64 u32 buf_size, enum dma_data_direction dir,
65 void (*dma_sync)(struct device *,
66 dma_addr_t, unsigned long, size_t,
67 enum dma_data_direction))
68{
69
70 dma_sync(stat->isp->dev, buf->dma_addr, 0, MAGIC_SIZE, dir);
71 dma_sync(stat->isp->dev, buf->dma_addr + (buf_size & PAGE_MASK),
72 buf_size & ~PAGE_MASK, MAGIC_SIZE, dir);
73}
74
75static void isp_stat_buf_sync_magic_for_device(struct ispstat *stat,
76 struct ispstat_buffer *buf,
77 u32 buf_size,
78 enum dma_data_direction dir)
79{
80 if (ISP_STAT_USES_DMAENGINE(stat))
81 return;
82
83 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
84 dma_sync_single_range_for_device);
85}
86
87static void isp_stat_buf_sync_magic_for_cpu(struct ispstat *stat,
88 struct ispstat_buffer *buf,
89 u32 buf_size,
90 enum dma_data_direction dir)
91{
92 if (ISP_STAT_USES_DMAENGINE(stat))
93 return;
94
95 __isp_stat_buf_sync_magic(stat, buf, buf_size, dir,
96 dma_sync_single_range_for_cpu);
97}
98
99static int isp_stat_buf_check_magic(struct ispstat *stat,
100 struct ispstat_buffer *buf)
101{
102 const u32 buf_size = IS_H3A_AF(stat) ?
103 buf->buf_size + AF_EXTRA_DATA : buf->buf_size;
104 u8 *w;
105 u8 *end;
106 int ret = -EINVAL;
107
108 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
109
110
111 for (w = buf->virt_addr, end = w + MAGIC_SIZE; w < end; w++)
112 if (likely(*w != MAGIC_NUM))
113 ret = 0;
114
115 if (ret) {
116 dev_dbg(stat->isp->dev, "%s: beginning magic check does not "
117 "match.\n", stat->subdev.name);
118 return ret;
119 }
120
121
122 for (w = buf->virt_addr + buf_size, end = w + MAGIC_SIZE;
123 w < end; w++) {
124 if (unlikely(*w != MAGIC_NUM)) {
125 dev_dbg(stat->isp->dev, "%s: ending magic check does "
126 "not match.\n", stat->subdev.name);
127 return -EINVAL;
128 }
129 }
130
131 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
132 DMA_FROM_DEVICE);
133
134 return 0;
135}
136
137static void isp_stat_buf_insert_magic(struct ispstat *stat,
138 struct ispstat_buffer *buf)
139{
140 const u32 buf_size = IS_H3A_AF(stat) ?
141 stat->buf_size + AF_EXTRA_DATA : stat->buf_size;
142
143 isp_stat_buf_sync_magic_for_cpu(stat, buf, buf_size, DMA_FROM_DEVICE);
144
145
146
147
148
149
150
151 memset(buf->virt_addr, MAGIC_NUM, MAGIC_SIZE);
152 memset(buf->virt_addr + buf_size, MAGIC_NUM, MAGIC_SIZE);
153
154 isp_stat_buf_sync_magic_for_device(stat, buf, buf_size,
155 DMA_BIDIRECTIONAL);
156}
157
158static void isp_stat_buf_sync_for_device(struct ispstat *stat,
159 struct ispstat_buffer *buf)
160{
161 if (ISP_STAT_USES_DMAENGINE(stat))
162 return;
163
164 dma_sync_sg_for_device(stat->isp->dev, buf->sgt.sgl,
165 buf->sgt.nents, DMA_FROM_DEVICE);
166}
167
168static void isp_stat_buf_sync_for_cpu(struct ispstat *stat,
169 struct ispstat_buffer *buf)
170{
171 if (ISP_STAT_USES_DMAENGINE(stat))
172 return;
173
174 dma_sync_sg_for_cpu(stat->isp->dev, buf->sgt.sgl,
175 buf->sgt.nents, DMA_FROM_DEVICE);
176}
177
178static void isp_stat_buf_clear(struct ispstat *stat)
179{
180 int i;
181
182 for (i = 0; i < STAT_MAX_BUFS; i++)
183 stat->buf[i].empty = 1;
184}
185
186static struct ispstat_buffer *
187__isp_stat_buf_find(struct ispstat *stat, int look_empty)
188{
189 struct ispstat_buffer *found = NULL;
190 int i;
191
192 for (i = 0; i < STAT_MAX_BUFS; i++) {
193 struct ispstat_buffer *curr = &stat->buf[i];
194
195
196
197
198
199 if (curr == stat->locked_buf || curr == stat->active_buf)
200 continue;
201
202
203 if (!look_empty && curr->empty)
204 continue;
205
206
207 if (curr->empty) {
208 found = curr;
209 break;
210 }
211
212
213 if (!found ||
214 (s32)curr->frame_number - (s32)found->frame_number < 0)
215 found = curr;
216 }
217
218 return found;
219}
220
221static inline struct ispstat_buffer *
222isp_stat_buf_find_oldest(struct ispstat *stat)
223{
224 return __isp_stat_buf_find(stat, 0);
225}
226
227static inline struct ispstat_buffer *
228isp_stat_buf_find_oldest_or_empty(struct ispstat *stat)
229{
230 return __isp_stat_buf_find(stat, 1);
231}
232
233static int isp_stat_buf_queue(struct ispstat *stat)
234{
235 if (!stat->active_buf)
236 return STAT_NO_BUF;
237
238 v4l2_get_timestamp(&stat->active_buf->ts);
239
240 stat->active_buf->buf_size = stat->buf_size;
241 if (isp_stat_buf_check_magic(stat, stat->active_buf)) {
242 dev_dbg(stat->isp->dev, "%s: data wasn't properly written.\n",
243 stat->subdev.name);
244 return STAT_NO_BUF;
245 }
246 stat->active_buf->config_counter = stat->config_counter;
247 stat->active_buf->frame_number = stat->frame_number;
248 stat->active_buf->empty = 0;
249 stat->active_buf = NULL;
250
251 return STAT_BUF_DONE;
252}
253
254
255static void isp_stat_buf_next(struct ispstat *stat)
256{
257 if (unlikely(stat->active_buf))
258
259 dev_dbg(stat->isp->dev, "%s: new buffer requested without "
260 "queuing active one.\n",
261 stat->subdev.name);
262 else
263 stat->active_buf = isp_stat_buf_find_oldest_or_empty(stat);
264}
265
266static void isp_stat_buf_release(struct ispstat *stat)
267{
268 unsigned long flags;
269
270 isp_stat_buf_sync_for_device(stat, stat->locked_buf);
271 spin_lock_irqsave(&stat->isp->stat_lock, flags);
272 stat->locked_buf = NULL;
273 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
274}
275
276
277static struct ispstat_buffer *isp_stat_buf_get(struct ispstat *stat,
278 struct omap3isp_stat_data *data)
279{
280 int rval = 0;
281 unsigned long flags;
282 struct ispstat_buffer *buf;
283
284 spin_lock_irqsave(&stat->isp->stat_lock, flags);
285
286 while (1) {
287 buf = isp_stat_buf_find_oldest(stat);
288 if (!buf) {
289 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
290 dev_dbg(stat->isp->dev, "%s: cannot find a buffer.\n",
291 stat->subdev.name);
292 return ERR_PTR(-EBUSY);
293 }
294 if (isp_stat_buf_check_magic(stat, buf)) {
295 dev_dbg(stat->isp->dev, "%s: current buffer has "
296 "corrupted data\n.", stat->subdev.name);
297
298 buf->empty = 1;
299 } else {
300
301 break;
302 }
303 }
304
305 stat->locked_buf = buf;
306
307 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
308
309 if (buf->buf_size > data->buf_size) {
310 dev_warn(stat->isp->dev, "%s: userspace's buffer size is "
311 "not enough.\n", stat->subdev.name);
312 isp_stat_buf_release(stat);
313 return ERR_PTR(-EINVAL);
314 }
315
316 isp_stat_buf_sync_for_cpu(stat, buf);
317
318 rval = copy_to_user(data->buf,
319 buf->virt_addr,
320 buf->buf_size);
321
322 if (rval) {
323 dev_info(stat->isp->dev,
324 "%s: failed copying %d bytes of stat data\n",
325 stat->subdev.name, rval);
326 buf = ERR_PTR(-EFAULT);
327 isp_stat_buf_release(stat);
328 }
329
330 return buf;
331}
332
333static void isp_stat_bufs_free(struct ispstat *stat)
334{
335 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
336 ? NULL : stat->isp->dev;
337 unsigned int i;
338
339 for (i = 0; i < STAT_MAX_BUFS; i++) {
340 struct ispstat_buffer *buf = &stat->buf[i];
341
342 if (!buf->virt_addr)
343 continue;
344
345 sg_free_table(&buf->sgt);
346
347 dma_free_coherent(dev, stat->buf_alloc_size, buf->virt_addr,
348 buf->dma_addr);
349
350 buf->dma_addr = 0;
351 buf->virt_addr = NULL;
352 buf->empty = 1;
353 }
354
355 dev_dbg(stat->isp->dev, "%s: all buffers were freed.\n",
356 stat->subdev.name);
357
358 stat->buf_alloc_size = 0;
359 stat->active_buf = NULL;
360}
361
362static int isp_stat_bufs_alloc_one(struct device *dev,
363 struct ispstat_buffer *buf,
364 unsigned int size)
365{
366 int ret;
367
368 buf->virt_addr = dma_alloc_coherent(dev, size, &buf->dma_addr,
369 GFP_KERNEL | GFP_DMA);
370 if (!buf->virt_addr)
371 return -ENOMEM;
372
373 ret = dma_get_sgtable(dev, &buf->sgt, buf->virt_addr, buf->dma_addr,
374 size);
375 if (ret < 0) {
376 dma_free_coherent(dev, size, buf->virt_addr, buf->dma_addr);
377 buf->virt_addr = NULL;
378 buf->dma_addr = 0;
379 return ret;
380 }
381
382 return 0;
383}
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401static int isp_stat_bufs_alloc(struct ispstat *stat, u32 size)
402{
403 struct device *dev = ISP_STAT_USES_DMAENGINE(stat)
404 ? NULL : stat->isp->dev;
405 unsigned long flags;
406 unsigned int i;
407
408 spin_lock_irqsave(&stat->isp->stat_lock, flags);
409
410 BUG_ON(stat->locked_buf != NULL);
411
412
413 if (stat->buf_alloc_size >= size) {
414 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
415 return 0;
416 }
417
418 if (stat->state != ISPSTAT_DISABLED || stat->buf_processing) {
419 dev_info(stat->isp->dev,
420 "%s: trying to allocate memory when busy\n",
421 stat->subdev.name);
422 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
423 return -EBUSY;
424 }
425
426 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
427
428 isp_stat_bufs_free(stat);
429
430 stat->buf_alloc_size = size;
431
432 for (i = 0; i < STAT_MAX_BUFS; i++) {
433 struct ispstat_buffer *buf = &stat->buf[i];
434 int ret;
435
436 ret = isp_stat_bufs_alloc_one(dev, buf, size);
437 if (ret < 0) {
438 dev_err(stat->isp->dev,
439 "%s: Failed to allocate DMA buffer %u\n",
440 stat->subdev.name, i);
441 isp_stat_bufs_free(stat);
442 return ret;
443 }
444
445 buf->empty = 1;
446
447 dev_dbg(stat->isp->dev,
448 "%s: buffer[%u] allocated. dma=0x%08lx virt=0x%08lx",
449 stat->subdev.name, i,
450 (unsigned long)buf->dma_addr,
451 (unsigned long)buf->virt_addr);
452 }
453
454 return 0;
455}
456
457static void isp_stat_queue_event(struct ispstat *stat, int err)
458{
459 struct video_device *vdev = stat->subdev.devnode;
460 struct v4l2_event event;
461 struct omap3isp_stat_event_status *status = (void *)event.u.data;
462
463 memset(&event, 0, sizeof(event));
464 if (!err) {
465 status->frame_number = stat->frame_number;
466 status->config_counter = stat->config_counter;
467 } else {
468 status->buf_err = 1;
469 }
470 event.type = stat->event_type;
471 v4l2_event_queue(vdev, &event);
472}
473
474
475
476
477
478
479
480
481int omap3isp_stat_request_statistics(struct ispstat *stat,
482 struct omap3isp_stat_data *data)
483{
484 struct ispstat_buffer *buf;
485
486 if (stat->state != ISPSTAT_ENABLED) {
487 dev_dbg(stat->isp->dev, "%s: engine not enabled.\n",
488 stat->subdev.name);
489 return -EINVAL;
490 }
491
492 mutex_lock(&stat->ioctl_lock);
493 buf = isp_stat_buf_get(stat, data);
494 if (IS_ERR(buf)) {
495 mutex_unlock(&stat->ioctl_lock);
496 return PTR_ERR(buf);
497 }
498
499 data->ts = buf->ts;
500 data->config_counter = buf->config_counter;
501 data->frame_number = buf->frame_number;
502 data->buf_size = buf->buf_size;
503
504 buf->empty = 1;
505 isp_stat_buf_release(stat);
506 mutex_unlock(&stat->ioctl_lock);
507
508 return 0;
509}
510
511
512
513
514
515
516
517
518
519int omap3isp_stat_config(struct ispstat *stat, void *new_conf)
520{
521 int ret;
522 unsigned long irqflags;
523 struct ispstat_generic_config *user_cfg = new_conf;
524 u32 buf_size = user_cfg->buf_size;
525
526 if (!new_conf) {
527 dev_dbg(stat->isp->dev, "%s: configuration is NULL\n",
528 stat->subdev.name);
529 return -EINVAL;
530 }
531
532 mutex_lock(&stat->ioctl_lock);
533
534 dev_dbg(stat->isp->dev, "%s: configuring module with buffer "
535 "size=0x%08lx\n", stat->subdev.name, (unsigned long)buf_size);
536
537 ret = stat->ops->validate_params(stat, new_conf);
538 if (ret) {
539 mutex_unlock(&stat->ioctl_lock);
540 dev_dbg(stat->isp->dev, "%s: configuration values are "
541 "invalid.\n", stat->subdev.name);
542 return ret;
543 }
544
545 if (buf_size != user_cfg->buf_size)
546 dev_dbg(stat->isp->dev, "%s: driver has corrected buffer size "
547 "request to 0x%08lx\n", stat->subdev.name,
548 (unsigned long)user_cfg->buf_size);
549
550
551
552
553
554
555
556
557
558
559
560
561 if (IS_H3A(stat)) {
562 buf_size = user_cfg->buf_size * 2 + MAGIC_SIZE;
563 if (IS_H3A_AF(stat))
564
565
566
567
568 buf_size += AF_EXTRA_DATA * (NUM_H3A_RECOVER_BUFS + 2);
569 if (stat->recover_priv) {
570 struct ispstat_generic_config *recover_cfg =
571 stat->recover_priv;
572 buf_size += recover_cfg->buf_size *
573 NUM_H3A_RECOVER_BUFS;
574 }
575 buf_size = PAGE_ALIGN(buf_size);
576 } else {
577 buf_size = PAGE_ALIGN(user_cfg->buf_size + MAGIC_SIZE);
578 }
579
580 ret = isp_stat_bufs_alloc(stat, buf_size);
581 if (ret) {
582 mutex_unlock(&stat->ioctl_lock);
583 return ret;
584 }
585
586 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
587 stat->ops->set_params(stat, new_conf);
588 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
589
590
591
592
593
594 user_cfg->config_counter = stat->config_counter + stat->inc_config;
595
596
597 stat->configured = 1;
598 dev_dbg(stat->isp->dev, "%s: module has been successfully "
599 "configured.\n", stat->subdev.name);
600
601 mutex_unlock(&stat->ioctl_lock);
602
603 return 0;
604}
605
606
607
608
609
610
611
612static int isp_stat_buf_process(struct ispstat *stat, int buf_state)
613{
614 int ret = STAT_NO_BUF;
615
616 if (!atomic_add_unless(&stat->buf_err, -1, 0) &&
617 buf_state == STAT_BUF_DONE && stat->state == ISPSTAT_ENABLED) {
618 ret = isp_stat_buf_queue(stat);
619 isp_stat_buf_next(stat);
620 }
621
622 return ret;
623}
624
625int omap3isp_stat_pcr_busy(struct ispstat *stat)
626{
627 return stat->ops->busy(stat);
628}
629
630int omap3isp_stat_busy(struct ispstat *stat)
631{
632 return omap3isp_stat_pcr_busy(stat) | stat->buf_processing |
633 (stat->state != ISPSTAT_DISABLED);
634}
635
636
637
638
639
640
641
642
643static void isp_stat_pcr_enable(struct ispstat *stat, u8 pcr_enable)
644{
645 if ((stat->state != ISPSTAT_ENABLING &&
646 stat->state != ISPSTAT_ENABLED) && pcr_enable)
647
648 return;
649
650 stat->ops->enable(stat, pcr_enable);
651 if (stat->state == ISPSTAT_DISABLING && !pcr_enable)
652 stat->state = ISPSTAT_DISABLED;
653 else if (stat->state == ISPSTAT_ENABLING && pcr_enable)
654 stat->state = ISPSTAT_ENABLED;
655}
656
657void omap3isp_stat_suspend(struct ispstat *stat)
658{
659 unsigned long flags;
660
661 spin_lock_irqsave(&stat->isp->stat_lock, flags);
662
663 if (stat->state != ISPSTAT_DISABLED)
664 stat->ops->enable(stat, 0);
665 if (stat->state == ISPSTAT_ENABLED)
666 stat->state = ISPSTAT_SUSPENDED;
667
668 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
669}
670
671void omap3isp_stat_resume(struct ispstat *stat)
672{
673
674 if (stat->state == ISPSTAT_SUSPENDED)
675 stat->state = ISPSTAT_ENABLING;
676}
677
678static void isp_stat_try_enable(struct ispstat *stat)
679{
680 unsigned long irqflags;
681
682 if (stat->priv == NULL)
683
684 return;
685
686 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
687 if (stat->state == ISPSTAT_ENABLING && !stat->buf_processing &&
688 stat->buf_alloc_size) {
689
690
691
692
693 stat->update = 1;
694 isp_stat_buf_next(stat);
695 stat->ops->setup_regs(stat, stat->priv);
696 isp_stat_buf_insert_magic(stat, stat->active_buf);
697
698
699
700
701
702
703
704 if (!IS_H3A(stat))
705 atomic_set(&stat->buf_err, 0);
706
707 isp_stat_pcr_enable(stat, 1);
708 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
709 dev_dbg(stat->isp->dev, "%s: module is enabled.\n",
710 stat->subdev.name);
711 } else {
712 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
713 }
714}
715
716void omap3isp_stat_isr_frame_sync(struct ispstat *stat)
717{
718 isp_stat_try_enable(stat);
719}
720
721void omap3isp_stat_sbl_overflow(struct ispstat *stat)
722{
723 unsigned long irqflags;
724
725 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
726
727
728
729
730 atomic_set(&stat->buf_err, 2);
731
732
733
734
735
736
737
738 if (stat->recover_priv)
739 stat->sbl_ovl_recover = 1;
740 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
741}
742
743
744
745
746
747
748
749
750int omap3isp_stat_enable(struct ispstat *stat, u8 enable)
751{
752 unsigned long irqflags;
753
754 dev_dbg(stat->isp->dev, "%s: user wants to %s module.\n",
755 stat->subdev.name, enable ? "enable" : "disable");
756
757
758 mutex_lock(&stat->ioctl_lock);
759
760 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
761
762 if (!stat->configured && enable) {
763 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
764 mutex_unlock(&stat->ioctl_lock);
765 dev_dbg(stat->isp->dev, "%s: cannot enable module as it's "
766 "never been successfully configured so far.\n",
767 stat->subdev.name);
768 return -EINVAL;
769 }
770
771 if (enable) {
772 if (stat->state == ISPSTAT_DISABLING)
773
774 stat->state = ISPSTAT_ENABLED;
775 else if (stat->state == ISPSTAT_DISABLED)
776
777 stat->state = ISPSTAT_ENABLING;
778 } else {
779 if (stat->state == ISPSTAT_ENABLING) {
780
781 stat->state = ISPSTAT_DISABLED;
782 } else if (stat->state == ISPSTAT_ENABLED) {
783
784 stat->state = ISPSTAT_DISABLING;
785 isp_stat_buf_clear(stat);
786 }
787 }
788
789 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
790 mutex_unlock(&stat->ioctl_lock);
791
792 return 0;
793}
794
795int omap3isp_stat_s_stream(struct v4l2_subdev *subdev, int enable)
796{
797 struct ispstat *stat = v4l2_get_subdevdata(subdev);
798
799 if (enable) {
800
801
802
803
804 isp_stat_try_enable(stat);
805 } else {
806 unsigned long flags;
807
808 omap3isp_stat_enable(stat, 0);
809 spin_lock_irqsave(&stat->isp->stat_lock, flags);
810 stat->ops->enable(stat, 0);
811 spin_unlock_irqrestore(&stat->isp->stat_lock, flags);
812
813
814
815
816
817
818
819
820
821
822
823
824 if (!omap3isp_stat_pcr_busy(stat))
825 omap3isp_stat_isr(stat);
826
827 dev_dbg(stat->isp->dev, "%s: module is being disabled\n",
828 stat->subdev.name);
829 }
830
831 return 0;
832}
833
834
835
836
837static void __stat_isr(struct ispstat *stat, int from_dma)
838{
839 int ret = STAT_BUF_DONE;
840 int buf_processing;
841 unsigned long irqflags;
842 struct isp_pipeline *pipe;
843
844
845
846
847
848
849 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
850 if (stat->state == ISPSTAT_DISABLED) {
851 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
852 return;
853 }
854 buf_processing = stat->buf_processing;
855 stat->buf_processing = 1;
856 stat->ops->enable(stat, 0);
857
858 if (buf_processing && !from_dma) {
859 if (stat->state == ISPSTAT_ENABLED) {
860 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
861 dev_err(stat->isp->dev,
862 "%s: interrupt occurred when module was still "
863 "processing a buffer.\n", stat->subdev.name);
864 ret = STAT_NO_BUF;
865 goto out;
866 } else {
867
868
869
870
871
872
873
874 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
875 return;
876 }
877 }
878 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
879
880
881 if (!omap3isp_stat_pcr_busy(stat)) {
882 if (!from_dma && stat->ops->buf_process)
883
884 ret = stat->ops->buf_process(stat);
885 if (ret == STAT_BUF_WAITING_DMA)
886
887 return;
888
889 spin_lock_irqsave(&stat->isp->stat_lock, irqflags);
890
891
892
893
894
895
896 if (stat->state == ISPSTAT_DISABLING) {
897 stat->state = ISPSTAT_DISABLED;
898 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
899 stat->buf_processing = 0;
900 return;
901 }
902 pipe = to_isp_pipeline(&stat->subdev.entity);
903 stat->frame_number = atomic_read(&pipe->frame_number);
904
905
906
907
908
909
910 ret = isp_stat_buf_process(stat, ret);
911
912 if (likely(!stat->sbl_ovl_recover)) {
913 stat->ops->setup_regs(stat, stat->priv);
914 } else {
915
916
917
918
919
920 stat->update = 1;
921 stat->ops->setup_regs(stat, stat->recover_priv);
922 stat->sbl_ovl_recover = 0;
923
924
925
926
927
928 stat->update = 1;
929 }
930
931 isp_stat_buf_insert_magic(stat, stat->active_buf);
932
933
934
935
936
937
938
939
940
941
942
943
944 isp_stat_pcr_enable(stat, 1);
945 spin_unlock_irqrestore(&stat->isp->stat_lock, irqflags);
946 } else {
947
948
949
950
951
952
953
954
955
956
957 if (stat->ops->buf_process)
958
959
960
961
962
963
964 atomic_set(&stat->buf_err, 1);
965
966 ret = STAT_NO_BUF;
967 dev_dbg(stat->isp->dev, "%s: cannot process buffer, "
968 "device is busy.\n", stat->subdev.name);
969 }
970
971out:
972 stat->buf_processing = 0;
973 isp_stat_queue_event(stat, ret != STAT_BUF_DONE);
974}
975
976void omap3isp_stat_isr(struct ispstat *stat)
977{
978 __stat_isr(stat, 0);
979}
980
981void omap3isp_stat_dma_isr(struct ispstat *stat)
982{
983 __stat_isr(stat, 1);
984}
985
986int omap3isp_stat_subscribe_event(struct v4l2_subdev *subdev,
987 struct v4l2_fh *fh,
988 struct v4l2_event_subscription *sub)
989{
990 struct ispstat *stat = v4l2_get_subdevdata(subdev);
991
992 if (sub->type != stat->event_type)
993 return -EINVAL;
994
995 return v4l2_event_subscribe(fh, sub, STAT_NEVENTS, NULL);
996}
997
998int omap3isp_stat_unsubscribe_event(struct v4l2_subdev *subdev,
999 struct v4l2_fh *fh,
1000 struct v4l2_event_subscription *sub)
1001{
1002 return v4l2_event_unsubscribe(fh, sub);
1003}
1004
1005void omap3isp_stat_unregister_entities(struct ispstat *stat)
1006{
1007 v4l2_device_unregister_subdev(&stat->subdev);
1008}
1009
1010int omap3isp_stat_register_entities(struct ispstat *stat,
1011 struct v4l2_device *vdev)
1012{
1013 return v4l2_device_register_subdev(vdev, &stat->subdev);
1014}
1015
1016static int isp_stat_init_entities(struct ispstat *stat, const char *name,
1017 const struct v4l2_subdev_ops *sd_ops)
1018{
1019 struct v4l2_subdev *subdev = &stat->subdev;
1020 struct media_entity *me = &subdev->entity;
1021
1022 v4l2_subdev_init(subdev, sd_ops);
1023 snprintf(subdev->name, V4L2_SUBDEV_NAME_SIZE, "OMAP3 ISP %s", name);
1024 subdev->grp_id = 1 << 16;
1025 subdev->flags |= V4L2_SUBDEV_FL_HAS_EVENTS | V4L2_SUBDEV_FL_HAS_DEVNODE;
1026 v4l2_set_subdevdata(subdev, stat);
1027
1028 stat->pad.flags = MEDIA_PAD_FL_SINK | MEDIA_PAD_FL_MUST_CONNECT;
1029 me->ops = NULL;
1030
1031 return media_entity_pads_init(me, 1, &stat->pad);
1032}
1033
1034int omap3isp_stat_init(struct ispstat *stat, const char *name,
1035 const struct v4l2_subdev_ops *sd_ops)
1036{
1037 int ret;
1038
1039 stat->buf = kcalloc(STAT_MAX_BUFS, sizeof(*stat->buf), GFP_KERNEL);
1040 if (!stat->buf)
1041 return -ENOMEM;
1042
1043 isp_stat_buf_clear(stat);
1044 mutex_init(&stat->ioctl_lock);
1045 atomic_set(&stat->buf_err, 0);
1046
1047 ret = isp_stat_init_entities(stat, name, sd_ops);
1048 if (ret < 0) {
1049 mutex_destroy(&stat->ioctl_lock);
1050 kfree(stat->buf);
1051 }
1052
1053 return ret;
1054}
1055
1056void omap3isp_stat_cleanup(struct ispstat *stat)
1057{
1058 media_entity_cleanup(&stat->subdev.entity);
1059 mutex_destroy(&stat->ioctl_lock);
1060 isp_stat_bufs_free(stat);
1061 kfree(stat->buf);
1062}
1063