]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/media/video/omap/camera_core.c
Merge with ../linux-2.6
[linux-2.6-omap-h63xx.git] / drivers / media / video / omap / camera_core.c
1 /*
2  * drivers/media/video/omap/camera_core.c
3  *
4  * Copyright (C) 2004 Texas Instruments, Inc. 
5  *
6  * Video-for-Linux (Version 2) camera capture driver for
7  * the OMAP H2 and H3 camera controller.
8  *
9  * Adapted from omap24xx driver written by Andy Lowe (source@mvista.com)
10  * Copyright (C) 2003-2004 MontaVista Software, Inc.
11  * 
12  * This package is free software; you can redistribute it and/or modify 
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation. 
15  * 
16  * THIS PACKAGE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
19  *
20  * History:
21  *   27/03/05   Vladimir Barinov - Added support for power management
22  */
23  
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
27 #include <linux/init.h>
28 #include <linux/fs.h>
29 #include <linux/vmalloc.h>
30 #include <linux/slab.h>
31 #include <linux/proc_fs.h>
32 #include <linux/ctype.h>
33 #include <linux/pagemap.h>
34 #include <linux/mm.h>
35 #include <linux/device.h>
36 #include <linux/delay.h>
37 #include <linux/interrupt.h>
38 #include <linux/videodev.h>
39 #include <linux/pci.h>
40 #include <asm/semaphore.h>
41 #include <asm/processor.h>
42 #include <linux/dma-mapping.h>
43 #include <linux/fb.h>
44
45 #include <asm/io.h>
46 #include <asm/byteorder.h>
47 #include <asm/irq.h>
48
49 #include "sensor_if.h"
50 #include "camera_hw_if.h"
51 #include "camera_core.h"
52  
53 struct camera_device *camera_dev;
54 extern struct camera_sensor camera_sensor_if;
55 extern struct camera_hardware camera_hardware_if;
56  
57 static void camera_core_sgdma_process(struct camera_device *cam);
58
59 /* module parameters */
60 static int video_nr = -1;       /* video device minor (-1 ==> auto assign) */
61
62 /* Maximum amount of memory to use for capture buffers.
63  * Default is 4800KB, enough to double-buffer SXGA.
64  */
65 static int capture_mem = 1280*960*2*2;
66
67 /*Size of video overlay framebuffer. This determines the maximum image size
68  *that can be previewed. Default is 600KB, enough for sxga.
69  */
70 static int overlay_mem = 640*480*2;
71
72  
73 /* DMA completion routine for the scatter-gather DMA fragments. */
74 /* This function is called when a scatter DMA fragment is completed */
75 static void
76 camera_core_callback_sgdma(void *arg1, void *arg2)
77 {
78         struct camera_device *cam = (struct camera_device *)arg1;
79         int sgslot = (int)arg2;
80
81         struct sgdma_state *sgdma;
82
83         spin_lock(&cam->sg_lock);
84         sgdma = cam->sgdma + sgslot;
85         if (!sgdma->queued_sglist)
86         {
87                 spin_unlock(&cam->sg_lock);
88                 printk(KERN_ERR CAM_NAME ": SGDMA completed when none queued\n");
89                 return;
90         }
91         if (!--sgdma->queued_sglist) {
92                 /* queue for this sglist is empty so check whether transfer
93                 ** of the frame has been completed */
94                 if (sgdma->next_sglist == sgdma->sglen) {
95                         dma_callback_t callback = sgdma->callback;
96                         void *arg = sgdma->arg;
97                         /* all done with this sglist */
98                         cam->free_sgdma++;
99                         if (callback) {
100                                 spin_unlock(&cam->sg_lock);
101                                 (*callback)(cam, arg);
102                                 camera_core_sgdma_process(cam);
103                                 return;
104                         }
105                 }
106         }
107         spin_unlock(&cam->sg_lock);
108         camera_core_sgdma_process(cam);
109
110         return;
111 }
112
113 static void
114 camera_core_sgdma_init(struct camera_device *cam)
115 {
116         int sg;
117
118         /* Initialize the underlying camera DMA */
119         cam->cam_hardware->init_dma(cam->hardware_data);
120         spin_lock_init(&cam->sg_lock);
121         
122         cam->free_sgdma = NUM_SG_DMA;
123         cam->next_sgdma = 0;
124         for (sg = 0; sg < NUM_SG_DMA; sg++) {
125                 cam->sgdma[sg].sglen = 0;
126                 cam->sgdma[sg].next_sglist = 0;
127                 cam->sgdma[sg].queued_sglist = 0;
128                 cam->sgdma[sg].csr = 0;
129                 cam->sgdma[sg].callback = NULL;
130                 cam->sgdma[sg].arg = NULL;
131         }
132 }
133
134 /*
135  * Process the scatter-gather DMA queue by starting queued transfers
136  * This function is called to program the dma to start the transfer of an image.
137  */
138 static void
139 camera_core_sgdma_process(struct camera_device *cam)
140 {
141         unsigned long irqflags;
142         int queued_sgdma, sgslot;
143         struct sgdma_state *sgdma;
144         const struct scatterlist *sglist;
145         
146         spin_lock_irqsave(&cam->sg_lock, irqflags);
147         if (1 == cam->in_use) {
148                 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
149                 return;
150         }
151         cam->in_use = 1;
152         spin_unlock_irqrestore(&cam->sg_lock, irqflags);
153
154         queued_sgdma = NUM_SG_DMA - cam->free_sgdma;
155         sgslot = (cam->next_sgdma + cam->free_sgdma) % (NUM_SG_DMA);
156         while (queued_sgdma > 0) {
157                 sgdma = cam->sgdma + sgslot;
158                 while (sgdma->next_sglist < sgdma->sglen) {
159                         sglist = sgdma->sglist + sgdma->next_sglist;
160                         if (cam->cam_hardware->start_dma(sgdma, camera_core_callback_sgdma,
161                                 (void *)cam, (void *)sgslot, cam->hardware_data)) {
162                                         /* dma start failed */
163                                         cam->in_use = 0;
164                                         return;
165                         }
166                         else {
167                                 /* dma start successful */
168                                 sgdma->next_sglist ++;
169                                 sgdma->queued_sglist ++;
170                         }
171                 }
172                 queued_sgdma-- ;
173                 sgslot = (sgslot + 1) % (NUM_SG_DMA);
174         }
175
176         cam->in_use = 0;
177 }
178
179 /* Queue a scatter-gather DMA transfer from the camera to memory.
180  * Returns zero if the transfer was successfully queued, or
181  * non-zero if all of the scatter-gather slots are already in use.
182  */
183 static int
184 camera_core_sgdma_queue(struct camera_device *cam,
185         const struct scatterlist *sglist, int sglen, dma_callback_t callback,
186         void *arg)
187 {
188         unsigned long irqflags;
189         struct sgdma_state *sgdma;
190
191         if ((sglen < 0) || ((sglen > 0) & !sglist))
192                 return -EINVAL;
193
194         spin_lock_irqsave(&cam->sg_lock, irqflags);
195
196         if (!cam->free_sgdma) {
197                 spin_unlock_irqrestore(&cam->sg_lock, irqflags);
198                 return -EBUSY;
199         }
200
201         sgdma = cam->sgdma + cam->next_sgdma;
202
203         sgdma->sglist = sglist;
204         sgdma->sglen = sglen;
205         sgdma->next_sglist = 0;
206         sgdma->queued_sglist = 0;
207         sgdma->csr = 0;
208         sgdma->callback = callback;
209         sgdma->arg = arg;
210
211         cam->next_sgdma = (cam->next_sgdma + 1) % (NUM_SG_DMA); 
212         cam->free_sgdma--;
213
214         spin_unlock_irqrestore(&cam->sg_lock, irqflags);
215
216         camera_core_sgdma_process(cam);
217
218         return 0;
219 }
220
221
222 /* -------------------overlay routines ------------------------------*/
223 /* callback routine for overlay DMA completion. We just start another DMA
224  * transfer unless overlay has been turned off
225  */
226
227 static void
228 camera_core_overlay_callback(void *arg1, void *arg)
229 {
230         struct camera_device *cam = (struct camera_device *)arg1;
231         int err;
232         unsigned long irqflags;
233         int i, j;
234         int count, index;
235         unsigned char *fb_buf = phys_to_virt((unsigned long)camera_dev->fbuf.base);
236
237         spin_lock_irqsave(&cam->overlay_lock, irqflags);
238
239         if (!cam->previewing || cam->overlay_cnt == 0) {
240                 spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
241                 return;
242         }
243
244         --cam->overlay_cnt;
245         sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
246         sg_dma_len(&cam->overlay_sglist) = cam->pix.sizeimage;
247
248         count = 0;
249         j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline);
250         for (i = 0 ; i < cam->pix.sizeimage; i += cam->pix.bytesperline) {
251                 for (index = 0; index < cam->pix.bytesperline; index++) {
252                         fb_buf[j] = *(((unsigned char *) cam->overlay_base) +
253                                                                  i + index);
254                         index++;
255                         fb_buf[j + 1] = *(((unsigned char *) cam->overlay_base) + i + index);
256                         j = j - cam->fbuf.fmt.bytesperline;
257                 }
258                 count += 2;
259                 j = ((cam->pix.width - 1) * cam->fbuf.fmt.bytesperline) + count;
260         }
261
262         while (cam->overlay_cnt < 2) {
263                 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
264                         camera_core_overlay_callback, NULL);
265                 if (err)
266                         break;
267                 ++cam->overlay_cnt;
268         }
269
270         spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
271
272 }
273
274  
275 static void
276 camera_core_start_overlay(struct camera_device *cam)
277 {
278         int err;
279         unsigned long irqflags;
280
281         if (!cam->previewing) 
282                 return;
283
284         spin_lock_irqsave(&cam->overlay_lock, irqflags);
285
286         sg_dma_address(&cam->overlay_sglist) = cam->overlay_base_phys;
287         sg_dma_len(&cam->overlay_sglist)= cam->pix.sizeimage;
288         while (cam->overlay_cnt < 2) {
289                 err = camera_core_sgdma_queue(cam, &cam->overlay_sglist, 1,
290                                 camera_core_overlay_callback, NULL);
291                 if (err)
292                         break;
293                 ++cam->overlay_cnt;
294         }
295
296         spin_unlock_irqrestore(&cam->overlay_lock, irqflags);
297 }
298
299 /* ------------------ videobuf_queue_ops ---------------------------------------- */
300
301 /* This routine is called from interrupt context when a scatter-gather DMA
302  * transfer of a videobuf_buffer completes.
303  */
304 static void
305 camera_core_vbq_complete(void *arg1, void *arg)
306 {
307         struct camera_device *cam = (struct camera_device *)arg1;
308         struct videobuf_buffer *vb = (struct videobuf_buffer *)arg;
309
310         spin_lock(&cam->vbq_lock);
311
312         do_gettimeofday(&vb->ts);
313         vb->field_count = cam->field_count;
314         cam->field_count += 2;
315         vb->state = STATE_DONE;
316
317         wake_up(&vb->done);
318         
319         spin_unlock(&cam->vbq_lock);
320 }
321
322 static void
323 camera_core_vbq_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
324 {
325         videobuf_waiton(vb, 0, 0);
326         videobuf_dma_pci_unmap(NULL, &vb->dma);
327         videobuf_dma_free(&vb->dma);
328
329         vb->state = STATE_NEEDS_INIT;
330 }
331
332 /* Limit the number of available kernel image capture buffers based on the
333  * number requested, the currently selected image size, and the maximum
334  * amount of memory permitted for kernel capture buffers.
335  */
336 static int
337 camera_core_vbq_setup(struct videobuf_queue *q, unsigned int *cnt, unsigned int *size)
338 {
339         struct camera_device *cam = q->priv_data;
340
341         if (*cnt <= 0)
342                 *cnt = VIDEO_MAX_FRAME; /* supply a default number of buffers */
343
344         if (*cnt > VIDEO_MAX_FRAME)
345                 *cnt = VIDEO_MAX_FRAME;
346
347         spin_lock(&cam->img_lock);
348         *size = cam->pix.sizeimage;
349         spin_unlock(&cam->img_lock);
350
351         while (*size * *cnt > capture_mem)
352                 (*cnt)--;
353
354         return 0;
355 }
356
357 static int
358 camera_core_vbq_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
359         enum v4l2_field field)
360 {
361         struct camera_device *cam = q->priv_data;
362         int err = 0;
363
364         spin_lock(&cam->img_lock);
365         if (cam->pix.sizeimage > vb->bsize) {
366                 spin_unlock(&cam->img_lock);
367                 return -EINVAL;
368         }
369         vb->size = cam->pix.sizeimage; 
370         vb->width = cam->pix.width;
371         vb->height = cam->pix.height;
372         vb->field = field;
373         spin_unlock(&cam->img_lock);
374
375         if (vb->state == STATE_NEEDS_INIT)
376                 err = videobuf_iolock(NULL, vb, NULL);
377
378         if (!err)
379                 vb->state = STATE_PREPARED;
380         else
381                 camera_core_vbq_release (q, vb);
382
383         return err;
384 }
385
386 static void
387 camera_core_vbq_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
388 {
389         struct camera_device *cam = q->priv_data;
390         enum videobuf_state state = vb->state;
391         int err;
392
393         vb->state = STATE_QUEUED;
394         err = camera_core_sgdma_queue(cam, vb->dma.sglist, vb->dma.sglen,
395                 camera_core_vbq_complete, vb);
396         if (err) {
397                 /* Oops.  We're not supposed to get any errors here.  The only
398                 * way we could get an error is if we ran out of scatter-gather
399                 * DMA slots, but we are supposed to have at least as many
400                 * scatter-gather DMA slots as video buffers so that can't
401                 * happen.
402                 */
403                 printk(KERN_DEBUG CAM_NAME
404                         ": Failed to queue a video buffer for SGDMA\n");
405                 vb->state = state;
406         }
407 }
408
409 /* ------------------ videobuf_queue_ops ---------------------------------------- */
410
411 static int
412 camera_core_do_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 
413                      void *arg)
414 {
415         struct camera_fh *fh  = file->private_data;
416         struct camera_device *cam = fh->cam;
417         int err;
418
419         switch (cmd) {
420                 case VIDIOC_ENUMINPUT:
421                 {
422                         /* default handler assumes 1 video input (the camera) */
423                         struct v4l2_input *input = (struct v4l2_input *)arg;
424                         int index = input->index;
425
426                         memset(input, 0, sizeof(*input));
427                         input->index = index;
428
429                         if (index > 0)
430                                 return -EINVAL;
431
432                         strlcpy(input->name, "camera", sizeof(input->name));
433                         input->type = V4L2_INPUT_TYPE_CAMERA;
434
435                         return 0;
436                 }
437
438                 case VIDIOC_G_INPUT:
439                 {
440                         unsigned int *input = arg;
441                         *input = 0;
442
443                         return 0;
444                 }
445
446                 case VIDIOC_S_INPUT:
447                 {
448                         unsigned int *input = arg;
449
450                         if (*input > 0)
451                                 return -EINVAL;
452
453                         return 0;
454                 }
455
456                 case VIDIOC_ENUM_FMT:
457                 {
458                         struct v4l2_fmtdesc *fmt = arg;
459                         return cam->cam_sensor->enum_pixformat(fmt, cam->sensor_data);
460                 }       
461
462                 case VIDIOC_TRY_FMT:
463                 {
464                         struct v4l2_format *fmt = arg;
465                         return cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
466
467                 }
468
469                 case VIDIOC_G_FMT:
470                 {
471                         struct v4l2_format *fmt = arg;
472
473                         /* get the current format */
474                         memset(&fmt->fmt.pix, 0, sizeof (fmt->fmt.pix));
475                         fmt->fmt.pix = cam->pix;
476                         
477                         return 0;
478                 }
479
480                 case VIDIOC_S_FMT:
481                 {
482                         struct v4l2_format *fmt = arg;
483                         unsigned int temp_sizeimage = 0;
484
485                         temp_sizeimage = cam->pix.sizeimage;
486                         cam->cam_sensor->try_format(&fmt->fmt.pix, cam->sensor_data);
487                         cam->pix = fmt->fmt.pix;
488
489                         cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
490                                 &cam->nominal_timeperframe, cam->sensor_data);
491                         cam->cparm.timeperframe = cam->nominal_timeperframe;
492                         cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
493                         return cam->cam_sensor->configure(&cam->pix, cam->xclk, 
494                                                 &cam->cparm.timeperframe, cam->sensor_data);
495                 }
496
497                 case VIDIOC_QUERYCTRL:
498                 {
499                         struct v4l2_queryctrl *qc = arg;
500                         return cam->cam_sensor->query_control(qc, cam->sensor_data);
501                 }
502
503                 case VIDIOC_G_CTRL:
504                 {
505                         struct v4l2_control *vc = arg;
506                         return cam->cam_sensor->get_control(vc, cam->sensor_data);
507                 }
508
509                 case VIDIOC_S_CTRL:
510                 {
511                         struct v4l2_control *vc = arg;
512                         return cam->cam_sensor->set_control(vc, cam->sensor_data);
513                 }
514                 
515                 case VIDIOC_QUERYCAP:
516                 {
517                         struct v4l2_capability *cap = 
518                                 (struct v4l2_capability *) arg;
519
520                         memset(cap, 0, sizeof(*cap));
521                         strlcpy(cap->driver, CAM_NAME, sizeof(cap->driver));
522                         strlcpy(cap->card, cam->vfd->name, sizeof(cap->card));
523                         cap->bus_info[0] = '\0';
524                         cap->version = KERNEL_VERSION(0, 0, 0);
525                         cap->capabilities =
526                                 V4L2_CAP_VIDEO_CAPTURE |
527                                 V4L2_CAP_VIDEO_OVERLAY |
528                                 V4L2_CAP_READWRITE | 
529                                 V4L2_CAP_STREAMING;
530                         return 0;
531                 }
532
533                 case VIDIOC_G_FBUF: /* Get the frame buffer parameters */
534                 {
535                         struct v4l2_framebuffer *fbuf =
536                                 (struct v4l2_framebuffer *) arg;
537
538                         spin_lock(&cam->img_lock);
539                         *fbuf = cam->fbuf;
540                         spin_unlock(&cam->img_lock);
541                         return 0;
542                 }
543
544                 case VIDIOC_S_FBUF: /* set the frame buffer parameters */
545                 {
546                         struct v4l2_framebuffer *fbuf =
547                                 (struct v4l2_framebuffer *) arg;
548
549                         spin_lock(&cam->img_lock);
550                         if (cam->previewing) {
551                                 spin_unlock(&cam->img_lock);
552                                 return -EBUSY;
553                         }
554                         cam->fbuf.base = fbuf->base;
555                         cam->fbuf.fmt = fbuf->fmt;      
556                         
557                         spin_unlock(&cam->img_lock);
558                         return 0;
559                 }
560
561                 case VIDIOC_OVERLAY:
562                 {
563                         int enable = *((int *) arg);
564
565                         /* 
566                          * check whether the capture format and 
567                          ** the display format matches 
568                          * return failure if they are different
569                          */
570                         if (cam->pix.pixelformat != cam->fbuf.fmt.pixelformat)
571                         {
572                                 return -EINVAL;
573                         }
574
575                         /* If the camera image size is greater 
576                         ** than LCD size return failure */
577                         if ((cam->pix.width > cam->fbuf.fmt.height) || 
578                                 (cam->pix.height > cam->fbuf.fmt.width))
579                         {
580                                 return -EINVAL;
581                         }
582                         
583                         if (!cam->previewing && enable)
584                         {
585                                 cam->previewing = fh;
586                                 cam->overlay_cnt = 0;
587                                 camera_core_start_overlay(cam);
588                         }
589                         else if (!enable)
590                         {
591                                 cam->previewing = NULL;
592                         }
593         
594                         return 0;
595                 }
596
597                 case VIDIOC_REQBUFS:
598                         return videobuf_reqbufs(&fh->vbq, arg);
599
600                 case VIDIOC_QUERYBUF:
601                         return videobuf_querybuf(&fh->vbq, arg);
602
603                 case VIDIOC_QBUF:
604                         return videobuf_qbuf(&fh->vbq, arg);
605
606                 case VIDIOC_DQBUF:
607                         return videobuf_dqbuf(&fh->vbq, arg,
608            file->f_flags & O_NONBLOCK);
609
610                 case VIDIOC_STREAMON:
611                 {
612                         spin_lock(&cam->img_lock);
613
614                         if (cam->streaming || cam->reading) {
615                                 spin_unlock(&cam->img_lock);
616                                 return -EBUSY;
617                         }
618                         else {
619                                 cam->streaming = fh;
620                                 /* FIXME: start camera interface */
621                         }
622
623                         spin_unlock(&cam->img_lock);
624
625                         return videobuf_streamon(&fh->vbq);
626                 }
627                 case VIDIOC_STREAMOFF:
628                 {
629                         err = videobuf_streamoff(&fh->vbq);
630                         if (err < 0)
631                                 return err;
632
633                         spin_lock(&cam->img_lock);
634                         if (cam->streaming == fh) {
635                                 cam->streaming = NULL;
636                                 /* FIXME: stop camera interface */
637                         }
638                         spin_unlock(&cam->img_lock);
639                         return 0;
640                 }
641                 case VIDIOC_ENUMSTD:
642                 case VIDIOC_G_STD:
643                 case VIDIOC_S_STD:
644                 case VIDIOC_QUERYSTD:
645                 {
646                         /* Digital cameras don't have an analog video standard, 
647                          * so we don't need to implement these ioctls.
648                          */
649                          return -EINVAL;
650                 }
651                 case VIDIOC_G_AUDIO:
652                 case VIDIOC_S_AUDIO:
653                 case VIDIOC_G_AUDOUT:
654                 case VIDIOC_S_AUDOUT:
655                 {
656                         /* we don't have any audio inputs or outputs */
657                         return -EINVAL;
658                 }
659
660                 case VIDIOC_G_JPEGCOMP:
661                 case VIDIOC_S_JPEGCOMP:
662                 {
663                         /* JPEG compression is not supported */
664                         return -EINVAL;
665                 }
666
667                 case VIDIOC_G_TUNER:
668                 case VIDIOC_S_TUNER:
669                 case VIDIOC_G_MODULATOR:
670                 case VIDIOC_S_MODULATOR:
671                 case VIDIOC_G_FREQUENCY:
672                 case VIDIOC_S_FREQUENCY:
673                 {
674                         /* we don't have a tuner or modulator */
675                         return -EINVAL;
676                 }
677
678                 case VIDIOC_ENUMOUTPUT:
679                 case VIDIOC_G_OUTPUT:
680                 case VIDIOC_S_OUTPUT:
681                 {
682                         /* we don't have any video outputs */
683                         return -EINVAL;
684                 }
685
686                 default:
687                 {
688                         /* unrecognized ioctl */
689                         return -ENOIOCTLCMD;
690                 }
691         }
692         return 0;
693 }
694
695 /*
696  *  file operations
697  */
698
699 static unsigned
700 int camera_core_poll(struct file *file, struct poll_table_struct *wait)
701 {
702         return -EINVAL;
703 }
704
705 /* ------------------------------------------------------------ */
706 /* callback routine for read DMA completion. We just start another DMA
707  * transfer unless overlay has been turned off
708  */
709 static void
710 camera_core_capture_callback(void *arg1, void *arg)
711 {
712         struct camera_device *cam = (struct camera_device *)arg1;
713         int err;
714         unsigned long irqflags;
715         static int done = 0;
716
717         spin_lock_irqsave(&cam->capture_lock, irqflags);
718         if (!cam->reading)
719         {
720                 done = 0;
721                 cam->capture_started = 0;
722                 spin_unlock_irqrestore(&cam->capture_lock, irqflags);
723                 return;
724         }
725
726         if (done < 14) {
727                 ++done;
728                 sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
729                 sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
730                 err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
731                         camera_core_capture_callback, NULL);            
732         } else {
733                 cam->capture_completed = 1;
734                 if (cam->reading)
735                 {
736                         /* Wake up any process which are waiting for the 
737                         ** DMA to complete */
738                         wake_up_interruptible(&camera_dev->new_video_frame);
739                         sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
740                         sg_dma_len(&cam->capture_sglist) = cam->pix.sizeimage;
741                         err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
742                                 camera_core_capture_callback, NULL);
743                 }
744         }
745
746         spin_unlock_irqrestore(&cam->capture_lock, irqflags);
747 }
748
749  
750 static ssize_t
751 camera_core_read(struct file *file, char *data, size_t count, loff_t *ppos)
752 {
753         struct camera_fh *fh = file->private_data;
754         struct camera_device *cam = fh->cam;
755         int err;
756         unsigned long irqflags;
757         long timeout;
758 #if 0   /* use video_buf to do capture */
759         int i;
760         for (i = 0; i < 14; i++)
761                 videobuf_read_one(file, &fh->vbq, data, count, ppos);
762         i = videobuf_read_one(file, &fh->vbq, data, count, ppos);
763         return i;
764 #endif
765         
766         if (!cam->capture_base) {
767                 cam->capture_base = (unsigned long)dma_alloc_coherent(NULL,
768                                 cam->pix.sizeimage,
769                                 (dma_addr_t *) &cam->capture_base_phys,
770                                 GFP_KERNEL | GFP_DMA);
771         }
772         if (!cam->capture_base) {
773                 printk(KERN_ERR CAM_NAME
774                         ": cannot allocate capture buffer\n");
775                 return 0;
776         }
777
778         spin_lock_irqsave(&cam->capture_lock, irqflags);
779         cam->reading = fh;
780         cam->capture_started = 1;
781         sg_dma_address(&cam->capture_sglist) = cam->capture_base_phys;
782         sg_dma_len(&cam->capture_sglist)= cam->pix.sizeimage;
783         spin_unlock_irqrestore(&cam->capture_lock, irqflags);
784
785         err = camera_core_sgdma_queue(cam, &cam->capture_sglist, 1,
786                         camera_core_capture_callback, NULL);
787
788         /* Wait till DMA is completed */
789         timeout = HZ * 10;
790         cam->capture_completed = 0;
791         while (cam->capture_completed == 0) {
792                 timeout = interruptible_sleep_on_timeout 
793                                 (&cam->new_video_frame, timeout);
794                 if (timeout == 0) {
795                         printk(KERN_ERR CAM_NAME ": timeout waiting video frame\n");    
796                         return -EIO; /* time out */
797                 }
798         }
799         /* copy the data to the user buffer */
800         err = copy_to_user(data, (void *)cam->capture_base, cam->pix.sizeimage);
801         return (cam->pix.sizeimage - err);
802         
803 }
804
805 static int
806 camera_core_mmap(struct file *file, struct vm_area_struct *vma)
807 {
808         struct camera_fh *fh = file->private_data;
809
810         return videobuf_mmap_mapper(&fh->vbq, vma);
811 }
812
813 static int
814 camera_core_ioctl(struct inode *inode, struct file *file, unsigned int cmd, 
815                   unsigned long arg)
816 {
817
818         return video_usercopy(inode, file, cmd, arg, camera_core_do_ioctl);
819 }
820
821 static int
822 camera_core_release(struct inode *inode, struct file *file)
823 {
824         struct camera_fh *fh = file->private_data;
825         struct camera_device *cam = fh->cam;
826         
827         file->private_data = NULL;
828         kfree(fh);
829
830         spin_lock(&cam->img_lock);
831         if (cam->previewing == fh) {
832                 cam->previewing = NULL;
833         }
834         if (cam->streaming == fh) {
835                 cam->streaming = NULL;
836         }
837         if (cam->reading == fh) {
838                 cam->reading = NULL;
839         }
840         spin_unlock(&cam->img_lock);
841
842         camera_dev->cam_hardware->finish_dma(cam->hardware_data);
843
844         if (cam->capture_base) {
845                 dma_free_coherent(NULL, cam->pix.sizeimage, 
846                                         (void *)cam->capture_base, 
847                                         cam->capture_base_phys);
848                 cam->capture_base = 0;
849                 cam->capture_base_phys = 0;
850         }
851         if (fh->vbq.read_buf) {
852                 camera_core_vbq_release(&fh->vbq, fh->vbq.read_buf);
853                 kfree(fh->vbq.read_buf);
854         }
855
856         cam->cam_hardware->close(cam->hardware_data);
857         cam->active = 0;
858         return 0;
859 }
860
861 static int
862 camera_core_open(struct inode *inode, struct file *file)
863 {
864         int minor = iminor(inode);
865         struct camera_device *cam = camera_dev;
866         struct camera_fh *fh;
867
868         if (!cam || !cam->vfd || (cam->vfd->minor != minor))
869                 return -ENODEV;
870
871         /* allocate per-filehandle data */
872         fh = kmalloc(sizeof(*fh), GFP_KERNEL);
873         if (NULL == fh)
874                 return -ENOMEM;
875         file->private_data = fh;
876         fh->cam = cam;
877         fh->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
878
879         spin_lock(&cam->img_lock);
880         if (cam->active == 1) {
881                 printk (KERN_ERR CAM_NAME ": Camera device Active\n");
882                 spin_unlock(&cam->img_lock);
883                 return -EPERM;
884         }
885         cam->active = 1;
886         spin_unlock(&cam->img_lock);
887
888         videobuf_queue_init(&fh->vbq, &cam->vbq_ops, NULL, &cam->vbq_lock,
889                 fh->type, V4L2_FIELD_NONE, sizeof(struct videobuf_buffer), fh);
890
891         cam->capture_completed = 0;
892         cam->capture_started = 0;
893
894         if (cam->cam_hardware->open(cam->hardware_data))
895         {
896                 printk (KERN_ERR CAM_NAME ": Camera IF configuration failed\n");
897                 cam->active = 0;
898                 return -ENODEV;
899         }
900         
901         cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
902         /* program the sensor for the capture format and rate */
903         if (cam->cam_sensor->configure(&cam->pix, cam->xclk, 
904                                 &cam->cparm.timeperframe, cam->sensor_data))
905         {
906                 printk (KERN_ERR CAM_NAME ": Camera sensor configuration failed\n");
907                 cam->cam_hardware->close(cam->hardware_data);
908                 cam->active = 0;
909                 return -ENODEV;
910         }
911
912         return 0;
913 }
914
915 #ifdef CONFIG_PM
916 static int camera_core_suspend(struct device *dev, u32 state, u32 level)
917 {
918         struct camera_device *cam = dev_get_drvdata(dev);
919         int ret = 0;
920
921         spin_lock(&cam->img_lock);
922         switch (level) {
923         case SUSPEND_POWER_DOWN:
924                 if (cam->active) {
925                         cam->cam_hardware->close(cam->hardware_data);
926                 }
927                 cam->cam_sensor->power_off(cam->sensor_data);
928                 break;
929         }
930
931         spin_unlock(&cam->img_lock);
932         return ret;
933 }
934
935 static int camera_core_resume(struct device *dev, u32 level)
936 {
937         struct camera_device *cam = dev_get_drvdata(dev);
938         int ret = 0;
939
940         spin_lock(&cam->img_lock);
941         switch (level) {
942         case RESUME_POWER_ON:
943                 cam->cam_sensor->power_on(cam->sensor_data);
944                 if (cam->active) {
945                         cam->capture_completed = 1;
946                         cam->cam_hardware->open(cam->hardware_data);
947                         cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
948
949                         cam->cam_sensor->configure(&cam->pix, cam->xclk,
950                                 &cam->cparm.timeperframe, cam->sensor_data);
951
952                         camera_core_sgdma_process(cam);
953                 }
954                 break;
955         }
956
957         spin_unlock(&cam->img_lock);
958         return ret;
959 }
960 #endif  /* CONFIG_PM */
961
962 static struct file_operations camera_core_fops = 
963 {
964         .owner                  = THIS_MODULE,
965         .llseek                 = no_llseek,
966         .read                   = camera_core_read,
967         .poll                   = camera_core_poll,
968         .ioctl                  = camera_core_ioctl,
969         .mmap                   = camera_core_mmap,
970         .open                   = camera_core_open,
971         .release                = camera_core_release,
972 };
973
974 static struct device_driver camera_core_driver = {
975         .name                   = CAM_NAME,
976         .bus                    = &platform_bus_type,
977         .probe                  = NULL,
978         .remove                 = NULL,
979 #ifdef CONFIG_PM
980         .suspend                = camera_core_suspend,
981         .resume                 = camera_core_resume,
982 #endif
983         .shutdown               = NULL,
984 };
985
986 static struct platform_device camera_core_device = {
987         .name   = CAM_NAME,
988         .dev    = {
989                         .release        = NULL,
990                   },
991         .id     = 0,
992 };
993
994 void
995 camera_core_cleanup(void)
996 {
997         struct camera_device *cam = camera_dev;
998         struct video_device *vfd;
999
1000         if (!cam)
1001                 return;
1002
1003         vfd = cam->vfd;
1004         if (vfd) {
1005                 if (vfd->minor == -1) {
1006                         /* The device never got registered, so release the 
1007                         ** video_device struct directly
1008                         */
1009                         video_device_release(vfd);
1010                 } else {
1011                         /* The unregister function will release the video_device
1012                         ** struct as well as unregistering it.
1013                         */
1014                         video_unregister_device(vfd);
1015                         driver_unregister(&camera_core_driver);
1016                         platform_device_unregister(&camera_core_device);
1017                 }
1018                 cam->vfd = NULL;
1019         }
1020         if (cam->overlay_base) {
1021                 dma_free_coherent(NULL, cam->overlay_size,
1022                                         (void *)cam->overlay_base, 
1023                                         cam->overlay_base_phys);
1024                 cam->overlay_base = 0;
1025         }       
1026         cam->overlay_base_phys = 0;
1027
1028         cam->cam_sensor->cleanup(cam->sensor_data);
1029         cam->cam_hardware->cleanup(cam->hardware_data);
1030         kfree(cam);
1031         camera_dev = NULL;
1032
1033         return;
1034 }
1035
1036
1037 int __init 
1038 camera_core_init(void)
1039 {
1040         struct camera_device *cam;
1041         struct video_device *vfd;
1042
1043         cam = kmalloc(sizeof(struct camera_device), GFP_KERNEL);
1044         if (!cam) {
1045                 printk(KERN_ERR CAM_NAME ": could not allocate memory\n");
1046                 goto init_error;
1047         }
1048         memset(cam, 0, sizeof(struct camera_device));
1049         
1050         /* Save the pointer to camera device in a global variable */
1051         camera_dev = cam;
1052
1053         /* initialize the video_device struct */
1054         vfd = cam->vfd = video_device_alloc();
1055         if (!vfd) {
1056                 printk(KERN_ERR CAM_NAME 
1057                         ": could not allocate video device struct\n");
1058                 goto init_error;
1059         }
1060         
1061         vfd->release = video_device_release;
1062
1063         strlcpy(vfd->name, CAM_NAME, sizeof(vfd->name));
1064         vfd->type = VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CHROMAKEY;
1065         
1066         /* need to register for a VID_HARDWARE_* ID in videodev.h */
1067         vfd->hardware = 0;
1068         vfd->fops = &camera_core_fops;
1069         video_set_drvdata(vfd, cam);
1070         vfd->minor = -1;
1071
1072         /* initialize the videobuf queue ops */
1073         cam->vbq_ops.buf_setup = camera_core_vbq_setup;
1074         cam->vbq_ops.buf_prepare = camera_core_vbq_prepare;
1075         cam->vbq_ops.buf_queue = camera_core_vbq_queue;
1076         cam->vbq_ops.buf_release = camera_core_vbq_release;
1077
1078         /* initilize the overlay interface */
1079         cam->overlay_size = overlay_mem;
1080         if (cam->overlay_size > 0)
1081         {
1082                 cam->overlay_base = (unsigned long) dma_alloc_coherent(NULL,
1083                                         cam->overlay_size,
1084                                         (dma_addr_t *) &cam->overlay_base_phys,
1085                                         GFP_KERNEL | GFP_DMA);
1086                 if (!cam->overlay_base) {
1087                         printk(KERN_ERR CAM_NAME
1088                                 ": cannot allocate overlay framebuffer\n");
1089                         goto init_error;
1090                 }
1091         }
1092         memset((void*)cam->overlay_base, 0, cam->overlay_size);
1093         spin_lock_init(&cam->overlay_lock);
1094         spin_lock_init(&cam->capture_lock);
1095
1096         /*Initialise the pointer to the sensor interface and camera interface */
1097         cam->cam_sensor = &camera_sensor_if;
1098         cam->cam_hardware = &camera_hardware_if;
1099
1100         /* initialize the camera interface */
1101         cam->hardware_data = cam->cam_hardware->init();
1102         if (!cam->hardware_data) {
1103                 printk(KERN_ERR CAM_NAME ": cannot initialize interface hardware\n");
1104                 goto init_error;
1105         }
1106          
1107         /* initialize the spinlock used to serialize access to the image 
1108          * parameters
1109          */
1110         spin_lock_init(&cam->img_lock);
1111
1112         /* initialize the streaming capture parameters */
1113         cam->cparm.capability = V4L2_CAP_TIMEPERFRAME;
1114         cam->cparm.readbuffers = 1;
1115
1116         /* Enable the xclk output.  The sensor may (and does, in the case of 
1117          * the OV9640) require an xclk input in order for its initialization 
1118          * routine to work.
1119          */
1120         cam->xclk = 21000000;   /* choose an arbitrary xclk frequency */
1121         cam->xclk = cam->cam_hardware->set_xclk(cam->xclk, cam->hardware_data);
1122
1123         /* initialize the sensor and define a default capture format cam->pix */
1124         cam->sensor_data = cam->cam_sensor->init(&cam->pix);
1125         if (!cam->sensor_data) {
1126                 cam->cam_hardware->disable(cam->hardware_data);
1127                 printk(KERN_ERR CAM_NAME ": cannot initialize sensor\n");
1128                 goto init_error;
1129         }
1130
1131         printk(KERN_INFO CAM_NAME ": %s interface with %s sensor\n",
1132                 cam->cam_hardware->name, cam->cam_sensor->name);
1133
1134         /* select an arbitrary default capture frame rate of 15fps */
1135         cam->nominal_timeperframe.numerator = 1;
1136         cam->nominal_timeperframe.denominator = 15;
1137
1138         /* calculate xclk based on the default capture format and default 
1139          * frame rate
1140          */
1141         cam->xclk = cam->cam_sensor->calc_xclk(&cam->pix,
1142                 &cam->nominal_timeperframe, cam->sensor_data);
1143         cam->cparm.timeperframe = cam->nominal_timeperframe;
1144
1145         /* initialise the wait queue */
1146         init_waitqueue_head(&cam->new_video_frame);
1147
1148         /* Initialise the DMA structures */
1149         camera_core_sgdma_init(cam);
1150
1151         /* Disable the Camera after detection */
1152         cam->cam_hardware->disable(cam->hardware_data);
1153
1154         dev_set_drvdata(&camera_core_device.dev, (void *)cam);
1155         if (platform_device_register(&camera_core_device) < 0) {
1156                 printk(KERN_ERR CAM_NAME
1157                         ": could not register platform_device\n");
1158                 goto init_error;
1159         }
1160
1161         if (driver_register(&camera_core_driver) < 0) {
1162                 printk(KERN_ERR CAM_NAME
1163                         ": could not register driver\n");
1164                 platform_device_unregister(&camera_core_device);
1165                 goto init_error;
1166         }
1167         if (video_register_device(vfd, VFL_TYPE_GRABBER, video_nr) < 0) {
1168                 printk(KERN_ERR CAM_NAME 
1169                         ": could not register Video for Linux device\n");
1170                 platform_device_unregister(&camera_core_device);
1171                 driver_unregister(&camera_core_driver);
1172                 goto init_error;
1173         }
1174
1175         printk(KERN_INFO CAM_NAME 
1176                 ": registered device video%d [v4l2]\n", vfd->minor);
1177         return 0;
1178
1179 init_error:
1180         camera_core_cleanup();
1181         return -ENODEV;
1182 }
1183
1184 MODULE_AUTHOR("Texas Instruments.");
1185 MODULE_DESCRIPTION("OMAP Video for Linux camera driver");
1186 MODULE_LICENSE("GPL");
1187 module_param(video_nr, int, 0);
1188 MODULE_PARM_DESC(video_nr, 
1189                 "Minor number for video device (-1 ==> auto assign)");
1190 module_param(capture_mem, int, 0);
1191 MODULE_PARM_DESC(capture_mem,
1192         "Maximum amount of memory for capture buffers (default 4800KB)");
1193
1194 module_init(camera_core_init);
1195 module_exit(camera_core_cleanup);
1196
1197