1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include "cx25821-video.h"
25
26static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
27{
28 struct cx25821_buffer *buf =
29 container_of(vb, struct cx25821_buffer, vb);
30 struct cx25821_buffer *prev;
31 struct cx25821_fh *fh = vq->priv_data;
32 struct cx25821_dev *dev = fh->dev;
33 struct cx25821_dmaqueue *q = &dev->vidq[SRAM_CH07];
34
35
36 buf->risc.jmp[0] = cpu_to_le32(RISC_JUMP | RISC_IRQ1 | RISC_CNT_INC);
37 buf->risc.jmp[1] = cpu_to_le32(q->stopper.dma);
38 buf->risc.jmp[2] = cpu_to_le32(0);
39
40 dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]);
41 if (!list_empty(&q->queued)) {
42 list_add_tail(&buf->vb.queue, &q->queued);
43 buf->vb.state = VIDEOBUF_QUEUED;
44 dprintk(2, "[%p/%d] buffer_queue - append to queued\n", buf,
45 buf->vb.i);
46
47 } else if (list_empty(&q->active)) {
48 list_add_tail(&buf->vb.queue, &q->active);
49 cx25821_start_video_dma(dev, q, buf,
50 &dev->sram_channels[SRAM_CH07]);
51 buf->vb.state = VIDEOBUF_ACTIVE;
52 buf->count = q->count++;
53 mod_timer(&q->timeout, jiffies + BUFFER_TIMEOUT);
54 dprintk(2,
55 "[%p/%d] buffer_queue - first active, buf cnt = %d, q->count = %d\n",
56 buf, buf->vb.i, buf->count, q->count);
57 } else {
58 prev =
59 list_entry(q->active.prev, struct cx25821_buffer, vb.queue);
60 if (prev->vb.width == buf->vb.width
61 && prev->vb.height == buf->vb.height
62 && prev->fmt == buf->fmt) {
63 list_add_tail(&buf->vb.queue, &q->active);
64 buf->vb.state = VIDEOBUF_ACTIVE;
65 buf->count = q->count++;
66 prev->risc.jmp[1] = cpu_to_le32(buf->risc.dma);
67
68
69 prev->risc.jmp[2] = cpu_to_le32(0);
70 dprintk(2,
71 "[%p/%d] buffer_queue - append to active, buf->count=%d\n",
72 buf, buf->vb.i, buf->count);
73
74 } else {
75 list_add_tail(&buf->vb.queue, &q->queued);
76 buf->vb.state = VIDEOBUF_QUEUED;
77 dprintk(2, "[%p/%d] buffer_queue - first queued\n", buf,
78 buf->vb.i);
79 }
80 }
81
82 if (list_empty(&q->active)) {
83 dprintk(2, "active queue empty!\n");
84 }
85}
86
87static struct videobuf_queue_ops cx25821_video_qops = {
88 .buf_setup = buffer_setup,
89 .buf_prepare = buffer_prepare,
90 .buf_queue = buffer_queue,
91 .buf_release = buffer_release,
92};
93
94static int video_open(struct file *file)
95{
96 int minor = video_devdata(file)->minor;
97 struct cx25821_dev *h, *dev = NULL;
98 struct cx25821_fh *fh;
99 struct list_head *list;
100 enum v4l2_buf_type type = 0;
101 u32 pix_format;
102
103 lock_kernel();
104 list_for_each(list, &cx25821_devlist) {
105 h = list_entry(list, struct cx25821_dev, devlist);
106
107 if (h->video_dev[SRAM_CH07]
108 && h->video_dev[SRAM_CH07]->minor == minor) {
109 dev = h;
110 type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
111 }
112 }
113
114 if (NULL == dev) {
115 unlock_kernel();
116 return -ENODEV;
117 }
118
119 printk("open minor=%d type=%s\n", minor, v4l2_type_names[type]);
120
121
122 fh = kzalloc(sizeof(*fh), GFP_KERNEL);
123 if (NULL == fh) {
124 unlock_kernel();
125 return -ENOMEM;
126 }
127 file->private_data = fh;
128 fh->dev = dev;
129 fh->type = type;
130 fh->width = 720;
131
132 if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK)
133 fh->height = 576;
134 else
135 fh->height = 480;
136
137 dev->channel_opened = SRAM_CH07;
138 pix_format =
139 (dev->pixel_formats[dev->channel_opened] ==
140 PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV;
141 fh->fmt = format_by_fourcc(pix_format);
142
143 v4l2_prio_open(&dev->prio, &fh->prio);
144
145 videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops,
146 &dev->pci->dev, &dev->slock,
147 V4L2_BUF_TYPE_VIDEO_CAPTURE,
148 V4L2_FIELD_INTERLACED,
149 sizeof(struct cx25821_buffer), fh);
150
151 dprintk(1, "post videobuf_queue_init()\n");
152 unlock_kernel();
153
154 return 0;
155}
156
157static ssize_t video_read(struct file *file, char __user * data, size_t count,
158 loff_t * ppos)
159{
160 struct cx25821_fh *fh = file->private_data;
161
162 switch (fh->type) {
163 case V4L2_BUF_TYPE_VIDEO_CAPTURE:
164 if (res_locked(fh->dev, RESOURCE_VIDEO7))
165 return -EBUSY;
166
167 return videobuf_read_one(&fh->vidq, data, count, ppos,
168 file->f_flags & O_NONBLOCK);
169
170 default:
171 BUG();
172 return 0;
173 }
174}
175
176static unsigned int video_poll(struct file *file,
177 struct poll_table_struct *wait)
178{
179 struct cx25821_fh *fh = file->private_data;
180 struct cx25821_buffer *buf;
181
182 if (res_check(fh, RESOURCE_VIDEO7)) {
183
184 if (list_empty(&fh->vidq.stream))
185 return POLLERR;
186 buf = list_entry(fh->vidq.stream.next,
187 struct cx25821_buffer, vb.stream);
188 } else {
189
190 buf = (struct cx25821_buffer *)fh->vidq.read_buf;
191 if (NULL == buf)
192 return POLLERR;
193 }
194
195 poll_wait(file, &buf->vb.done, wait);
196 if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) {
197 if (buf->vb.state == VIDEOBUF_DONE) {
198 struct cx25821_dev *dev = fh->dev;
199
200 if (dev && dev->use_cif_resolution[SRAM_CH07]) {
201 u8 cam_id = *((char *)buf->vb.baddr + 3);
202 memcpy((char *)buf->vb.baddr,
203 (char *)buf->vb.baddr + (fh->width * 2),
204 (fh->width * 2));
205 *((char *)buf->vb.baddr + 3) = cam_id;
206 }
207 }
208
209 return POLLIN | POLLRDNORM;
210 }
211
212 return 0;
213}
214
215static int video_release(struct file *file)
216{
217 struct cx25821_fh *fh = file->private_data;
218 struct cx25821_dev *dev = fh->dev;
219
220
221 cx_write(channel7->dma_ctl, 0);
222
223
224 if (res_check(fh, RESOURCE_VIDEO7)) {
225 videobuf_queue_cancel(&fh->vidq);
226 res_free(dev, fh, RESOURCE_VIDEO7);
227 }
228
229 if (fh->vidq.read_buf) {
230 buffer_release(&fh->vidq, fh->vidq.read_buf);
231 kfree(fh->vidq.read_buf);
232 }
233
234 videobuf_mmap_free(&fh->vidq);
235
236 v4l2_prio_close(&dev->prio, &fh->prio);
237 file->private_data = NULL;
238 kfree(fh);
239
240 return 0;
241}
242
243static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
244{
245 struct cx25821_fh *fh = priv;
246 struct cx25821_dev *dev = fh->dev;
247
248 if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) {
249 return -EINVAL;
250 }
251
252 if (unlikely(i != fh->type)) {
253 return -EINVAL;
254 }
255
256 if (unlikely(!res_get(dev, fh, get_resource(fh, RESOURCE_VIDEO7)))) {
257 return -EBUSY;
258 }
259
260 return videobuf_streamon(get_queue(fh));
261}
262
263static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
264{
265 struct cx25821_fh *fh = priv;
266 struct cx25821_dev *dev = fh->dev;
267 int err, res;
268
269 if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
270 return -EINVAL;
271 if (i != fh->type)
272 return -EINVAL;
273
274 res = get_resource(fh, RESOURCE_VIDEO7);
275 err = videobuf_streamoff(get_queue(fh));
276 if (err < 0)
277 return err;
278 res_free(dev, fh, res);
279 return 0;
280}
281
282static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
283 struct v4l2_format *f)
284{
285 struct cx25821_fh *fh = priv;
286 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
287 int err;
288 int pix_format = 0;
289
290 if (fh) {
291 err = v4l2_prio_check(&dev->prio, &fh->prio);
292 if (0 != err)
293 return err;
294 }
295
296 dprintk(2, "%s()\n", __func__);
297 err = vidioc_try_fmt_vid_cap(file, priv, f);
298
299 if (0 != err)
300 return err;
301
302 fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat);
303 fh->vidq.field = f->fmt.pix.field;
304
305
306 if (is_valid_width(f->fmt.pix.width, dev->tvnorm)) {
307 fh->width = f->fmt.pix.width;
308 }
309
310 if (is_valid_height(f->fmt.pix.height, dev->tvnorm)) {
311 fh->height = f->fmt.pix.height;
312 }
313
314 if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P)
315 pix_format = PIXEL_FRMT_411;
316 else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV)
317 pix_format = PIXEL_FRMT_422;
318 else
319 return -EINVAL;
320
321 cx25821_set_pixel_format(dev, SRAM_CH07, pix_format);
322
323
324 if (fh->width == 320 || fh->width == 352) {
325 dev->use_cif_resolution[SRAM_CH07] = 1;
326 } else {
327 dev->use_cif_resolution[SRAM_CH07] = 0;
328 }
329 dev->cif_width[SRAM_CH07] = fh->width;
330 medusa_set_resolution(dev, fh->width, SRAM_CH07);
331
332 dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width,
333 fh->height, fh->vidq.field);
334 cx25821_call_all(dev, video, s_fmt, f);
335
336 return 0;
337}
338
339static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
340{
341 int ret_val = 0;
342 struct cx25821_fh *fh = priv;
343 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
344
345 ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK);
346
347 p->sequence = dev->vidq[SRAM_CH07].count;
348
349 return ret_val;
350}
351static int vidioc_log_status(struct file *file, void *priv)
352{
353 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
354 char name[32 + 2];
355
356 struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH07];
357 u32 tmp = 0;
358
359 snprintf(name, sizeof(name), "%s/2", dev->name);
360 printk(KERN_INFO "%s/2: ============ START LOG STATUS ============\n",
361 dev->name);
362 cx25821_call_all(dev, core, log_status);
363
364 tmp = cx_read(sram_ch->dma_ctl);
365 printk(KERN_INFO "Video input 7 is %s\n",
366 (tmp & 0x11) ? "streaming" : "stopped");
367 printk(KERN_INFO "%s/2: ============= END LOG STATUS =============\n",
368 dev->name);
369 return 0;
370}
371
372static int vidioc_s_ctrl(struct file *file, void *priv,
373 struct v4l2_control *ctl)
374{
375 struct cx25821_fh *fh = priv;
376 struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev;
377 int err;
378
379 if (fh) {
380 err = v4l2_prio_check(&dev->prio, &fh->prio);
381 if (0 != err)
382 return err;
383 }
384
385 return cx25821_set_control(dev, ctl, SRAM_CH07);
386}
387
388
389static const struct v4l2_file_operations video_fops = {
390 .owner = THIS_MODULE,
391 .open = video_open,
392 .release = video_release,
393 .read = video_read,
394 .poll = video_poll,
395 .mmap = video_mmap,
396 .ioctl = video_ioctl2,
397};
398
399static const struct v4l2_ioctl_ops video_ioctl_ops = {
400 .vidioc_querycap = vidioc_querycap,
401 .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
402 .vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
403 .vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,
404 .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
405 .vidioc_reqbufs = vidioc_reqbufs,
406 .vidioc_querybuf = vidioc_querybuf,
407 .vidioc_qbuf = vidioc_qbuf,
408 .vidioc_dqbuf = vidioc_dqbuf,
409#ifdef TUNER_FLAG
410 .vidioc_s_std = vidioc_s_std,
411 .vidioc_querystd = vidioc_querystd,
412#endif
413 .vidioc_cropcap = vidioc_cropcap,
414 .vidioc_s_crop = vidioc_s_crop,
415 .vidioc_g_crop = vidioc_g_crop,
416 .vidioc_enum_input = vidioc_enum_input,
417 .vidioc_g_input = vidioc_g_input,
418 .vidioc_s_input = vidioc_s_input,
419 .vidioc_g_ctrl = vidioc_g_ctrl,
420 .vidioc_s_ctrl = vidioc_s_ctrl,
421 .vidioc_queryctrl = vidioc_queryctrl,
422 .vidioc_streamon = vidioc_streamon,
423 .vidioc_streamoff = vidioc_streamoff,
424 .vidioc_log_status = vidioc_log_status,
425 .vidioc_g_priority = vidioc_g_priority,
426 .vidioc_s_priority = vidioc_s_priority,
427#ifdef CONFIG_VIDEO_V4L1_COMPAT
428 .vidiocgmbuf = vidiocgmbuf,
429#endif
430#ifdef TUNER_FLAG
431 .vidioc_g_tuner = vidioc_g_tuner,
432 .vidioc_s_tuner = vidioc_s_tuner,
433 .vidioc_g_frequency = vidioc_g_frequency,
434 .vidioc_s_frequency = vidioc_s_frequency,
435#endif
436#ifdef CONFIG_VIDEO_ADV_DEBUG
437 .vidioc_g_register = vidioc_g_register,
438 .vidioc_s_register = vidioc_s_register,
439#endif
440};
441
442struct video_device cx25821_video_template7 = {
443 .name = "cx25821-video",
444 .fops = &video_fops,
445 .minor = -1,
446 .ioctl_ops = &video_ioctl_ops,
447 .tvnorms = CX25821_NORMS,
448 .current_norm = V4L2_STD_NTSC_M,
449};
450