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