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