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