Commit | Line | Data |
---|---|---|
02b20b0b MCC |
1 | /* |
2 | * Driver for the Conexant CX25821 PCIe bridge | |
3 | * | |
bb4c9a74 | 4 | * Copyright (C) 2009 Conexant Systems Inc. |
02b20b0b MCC |
5 | * Authors <shu.lin@conexant.com>, <hiep.huynh@conexant.com> |
6 | * Based on Steven Toth <stoth@linuxtv.org> cx23885 driver | |
7 | * | |
8 | * This program is free software; you can redistribute it and/or modify | |
9 | * it under the terms of the GNU General Public License as published by | |
10 | * the Free Software Foundation; either version 2 of the License, or | |
11 | * (at your option) any later version. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * | |
17 | * GNU General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU General Public License | |
20 | * along with this program; if not, write to the Free Software | |
21 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | */ | |
23 | ||
24 | #include "cx25821-video.h" | |
25 | ||
02b20b0b MCC |
26 | static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb) |
27 | { | |
1a9fc855 MCC |
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 | /* add jump to stopper */ | |
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); /* bits 63-32 */ | |
02b20b0b | 39 | |
bb4c9a74 | 40 | dprintk(2, "jmp to stopper (0x%x)\n", buf->risc.jmp[1]); |
1a9fc855 MCC |
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); | |
bb4c9a74 | 57 | } else { |
1a9fc855 MCC |
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 | /* 64 bit bits 63-32 */ | |
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 | } | |
bb4c9a74 | 80 | } |
02b20b0b | 81 | |
1a9fc855 MCC |
82 | if (list_empty(&q->active)) { |
83 | dprintk(2, "active queue empty!\n"); | |
84 | } | |
02b20b0b MCC |
85 | } |
86 | ||
02b20b0b | 87 | static struct videobuf_queue_ops cx25821_video_qops = { |
f2466d63 MCC |
88 | .buf_setup = cx25821_buffer_setup, |
89 | .buf_prepare = cx25821_buffer_prepare, | |
1a9fc855 | 90 | .buf_queue = buffer_queue, |
f2466d63 | 91 | .buf_release = cx25821_buffer_release, |
02b20b0b MCC |
92 | }; |
93 | ||
02b20b0b MCC |
94 | static int video_open(struct file *file) |
95 | { | |
50462eb0 | 96 | struct video_device *vdev = video_devdata(file); |
63b0d5ad | 97 | struct cx25821_dev *dev = video_drvdata(file); |
1a9fc855 | 98 | struct cx25821_fh *fh; |
63b0d5ad | 99 | enum v4l2_buf_type type = V4L2_BUF_TYPE_VIDEO_CAPTURE; |
1a9fc855 MCC |
100 | u32 pix_format; |
101 | ||
50462eb0 LP |
102 | printk("open dev=%s type=%s\n", video_device_node_name(vdev), |
103 | v4l2_type_names[type]); | |
02b20b0b | 104 | |
1a9fc855 MCC |
105 | /* allocate + initialize per filehandle data */ |
106 | fh = kzalloc(sizeof(*fh), GFP_KERNEL); | |
63b0d5ad | 107 | if (NULL == fh) |
1a9fc855 | 108 | return -ENOMEM; |
63b0d5ad LP |
109 | |
110 | lock_kernel(); | |
111 | ||
1a9fc855 MCC |
112 | file->private_data = fh; |
113 | fh->dev = dev; | |
114 | fh->type = type; | |
115 | fh->width = 720; | |
116 | ||
117 | if (dev->tvnorm & V4L2_STD_PAL_BG || dev->tvnorm & V4L2_STD_PAL_DK) | |
118 | fh->height = 576; | |
119 | else | |
120 | fh->height = 480; | |
121 | ||
122 | dev->channel_opened = SRAM_CH07; | |
123 | pix_format = | |
124 | (dev->pixel_formats[dev->channel_opened] == | |
125 | PIXEL_FRMT_411) ? V4L2_PIX_FMT_Y41P : V4L2_PIX_FMT_YUYV; | |
126 | fh->fmt = format_by_fourcc(pix_format); | |
127 | ||
128 | v4l2_prio_open(&dev->prio, &fh->prio); | |
129 | ||
130 | videobuf_queue_sg_init(&fh->vidq, &cx25821_video_qops, | |
131 | &dev->pci->dev, &dev->slock, | |
132 | V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
133 | V4L2_FIELD_INTERLACED, | |
134 | sizeof(struct cx25821_buffer), fh); | |
135 | ||
136 | dprintk(1, "post videobuf_queue_init()\n"); | |
bb4c9a74 | 137 | unlock_kernel(); |
1a9fc855 MCC |
138 | |
139 | return 0; | |
02b20b0b MCC |
140 | } |
141 | ||
1a9fc855 MCC |
142 | static ssize_t video_read(struct file *file, char __user * data, size_t count, |
143 | loff_t * ppos) | |
02b20b0b | 144 | { |
1a9fc855 | 145 | struct cx25821_fh *fh = file->private_data; |
02b20b0b | 146 | |
1a9fc855 | 147 | switch (fh->type) { |
bb4c9a74 | 148 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
f2466d63 | 149 | if (cx25821_res_locked(fh->dev, RESOURCE_VIDEO7)) |
1a9fc855 | 150 | return -EBUSY; |
02b20b0b | 151 | |
1a9fc855 MCC |
152 | return videobuf_read_one(&fh->vidq, data, count, ppos, |
153 | file->f_flags & O_NONBLOCK); | |
02b20b0b | 154 | |
bb4c9a74 | 155 | default: |
1a9fc855 MCC |
156 | BUG(); |
157 | return 0; | |
158 | } | |
02b20b0b MCC |
159 | } |
160 | ||
1a9fc855 MCC |
161 | static unsigned int video_poll(struct file *file, |
162 | struct poll_table_struct *wait) | |
02b20b0b | 163 | { |
1a9fc855 MCC |
164 | struct cx25821_fh *fh = file->private_data; |
165 | struct cx25821_buffer *buf; | |
166 | ||
f2466d63 | 167 | if (cx25821_res_check(fh, RESOURCE_VIDEO7)) { |
1a9fc855 MCC |
168 | /* streaming capture */ |
169 | if (list_empty(&fh->vidq.stream)) | |
170 | return POLLERR; | |
171 | buf = list_entry(fh->vidq.stream.next, | |
172 | struct cx25821_buffer, vb.stream); | |
173 | } else { | |
174 | /* read() capture */ | |
175 | buf = (struct cx25821_buffer *)fh->vidq.read_buf; | |
176 | if (NULL == buf) | |
177 | return POLLERR; | |
bb4c9a74 MCC |
178 | } |
179 | ||
1a9fc855 MCC |
180 | poll_wait(file, &buf->vb.done, wait); |
181 | if (buf->vb.state == VIDEOBUF_DONE || buf->vb.state == VIDEOBUF_ERROR) { | |
182 | if (buf->vb.state == VIDEOBUF_DONE) { | |
183 | struct cx25821_dev *dev = fh->dev; | |
184 | ||
185 | if (dev && dev->use_cif_resolution[SRAM_CH07]) { | |
186 | u8 cam_id = *((char *)buf->vb.baddr + 3); | |
187 | memcpy((char *)buf->vb.baddr, | |
188 | (char *)buf->vb.baddr + (fh->width * 2), | |
189 | (fh->width * 2)); | |
190 | *((char *)buf->vb.baddr + 3) = cam_id; | |
191 | } | |
192 | } | |
193 | ||
194 | return POLLIN | POLLRDNORM; | |
195 | } | |
02b20b0b | 196 | |
1a9fc855 | 197 | return 0; |
02b20b0b MCC |
198 | } |
199 | ||
02b20b0b MCC |
200 | static int video_release(struct file *file) |
201 | { | |
1a9fc855 MCC |
202 | struct cx25821_fh *fh = file->private_data; |
203 | struct cx25821_dev *dev = fh->dev; | |
02b20b0b | 204 | |
1a9fc855 MCC |
205 | //stop the risc engine and fifo |
206 | cx_write(channel7->dma_ctl, 0); /* FIFO and RISC disable */ | |
02b20b0b | 207 | |
1a9fc855 | 208 | /* stop video capture */ |
f2466d63 | 209 | if (cx25821_res_check(fh, RESOURCE_VIDEO7)) { |
1a9fc855 | 210 | videobuf_queue_cancel(&fh->vidq); |
f2466d63 | 211 | cx25821_res_free(dev, fh, RESOURCE_VIDEO7); |
1a9fc855 | 212 | } |
02b20b0b | 213 | |
1a9fc855 | 214 | if (fh->vidq.read_buf) { |
f2466d63 | 215 | cx25821_buffer_release(&fh->vidq, fh->vidq.read_buf); |
1a9fc855 MCC |
216 | kfree(fh->vidq.read_buf); |
217 | } | |
02b20b0b | 218 | |
1a9fc855 | 219 | videobuf_mmap_free(&fh->vidq); |
02b20b0b | 220 | |
ffb4877b | 221 | v4l2_prio_close(&dev->prio, fh->prio); |
1a9fc855 MCC |
222 | file->private_data = NULL; |
223 | kfree(fh); | |
02b20b0b | 224 | |
1a9fc855 | 225 | return 0; |
02b20b0b MCC |
226 | } |
227 | ||
02b20b0b MCC |
228 | static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i) |
229 | { | |
1a9fc855 MCC |
230 | struct cx25821_fh *fh = priv; |
231 | struct cx25821_dev *dev = fh->dev; | |
02b20b0b | 232 | |
1a9fc855 MCC |
233 | if (unlikely(fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)) { |
234 | return -EINVAL; | |
235 | } | |
02b20b0b | 236 | |
1a9fc855 MCC |
237 | if (unlikely(i != fh->type)) { |
238 | return -EINVAL; | |
239 | } | |
02b20b0b | 240 | |
f2466d63 | 241 | if (unlikely(!cx25821_res_get(dev, fh, cx25821_get_resource(fh, RESOURCE_VIDEO7)))) { |
1a9fc855 MCC |
242 | return -EBUSY; |
243 | } | |
02b20b0b | 244 | |
1a9fc855 | 245 | return videobuf_streamon(get_queue(fh)); |
02b20b0b MCC |
246 | } |
247 | ||
248 | static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) | |
249 | { | |
1a9fc855 MCC |
250 | struct cx25821_fh *fh = priv; |
251 | struct cx25821_dev *dev = fh->dev; | |
252 | int err, res; | |
253 | ||
254 | if (fh->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
255 | return -EINVAL; | |
256 | if (i != fh->type) | |
257 | return -EINVAL; | |
258 | ||
f2466d63 | 259 | res = cx25821_get_resource(fh, RESOURCE_VIDEO7); |
1a9fc855 MCC |
260 | err = videobuf_streamoff(get_queue(fh)); |
261 | if (err < 0) | |
262 | return err; | |
f2466d63 | 263 | cx25821_res_free(dev, fh, res); |
1a9fc855 | 264 | return 0; |
02b20b0b MCC |
265 | } |
266 | ||
1a9fc855 MCC |
267 | static int vidioc_s_fmt_vid_cap(struct file *file, void *priv, |
268 | struct v4l2_format *f) | |
02b20b0b | 269 | { |
1a9fc855 MCC |
270 | struct cx25821_fh *fh = priv; |
271 | struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; | |
272 | int err; | |
273 | int pix_format = 0; | |
274 | ||
275 | if (fh) { | |
ffb4877b | 276 | err = v4l2_prio_check(&dev->prio, fh->prio); |
1a9fc855 MCC |
277 | if (0 != err) |
278 | return err; | |
279 | } | |
280 | ||
281 | dprintk(2, "%s()\n", __func__); | |
f2466d63 | 282 | err = cx25821_vidioc_try_fmt_vid_cap(file, priv, f); |
1a9fc855 | 283 | |
bb4c9a74 | 284 | if (0 != err) |
1a9fc855 MCC |
285 | return err; |
286 | ||
287 | fh->fmt = format_by_fourcc(f->fmt.pix.pixelformat); | |
288 | fh->vidq.field = f->fmt.pix.field; | |
289 | ||
290 | // check if width and height is valid based on set standard | |
f2466d63 | 291 | if (cx25821_is_valid_width(f->fmt.pix.width, dev->tvnorm)) { |
1a9fc855 MCC |
292 | fh->width = f->fmt.pix.width; |
293 | } | |
294 | ||
f2466d63 | 295 | if (cx25821_is_valid_height(f->fmt.pix.height, dev->tvnorm)) { |
1a9fc855 MCC |
296 | fh->height = f->fmt.pix.height; |
297 | } | |
298 | ||
299 | if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_Y41P) | |
300 | pix_format = PIXEL_FRMT_411; | |
301 | else if (f->fmt.pix.pixelformat == V4L2_PIX_FMT_YUYV) | |
302 | pix_format = PIXEL_FRMT_422; | |
303 | else | |
304 | return -EINVAL; | |
305 | ||
306 | cx25821_set_pixel_format(dev, SRAM_CH07, pix_format); | |
307 | ||
308 | // check if cif resolution | |
309 | if (fh->width == 320 || fh->width == 352) { | |
310 | dev->use_cif_resolution[SRAM_CH07] = 1; | |
311 | } else { | |
312 | dev->use_cif_resolution[SRAM_CH07] = 0; | |
313 | } | |
314 | dev->cif_width[SRAM_CH07] = fh->width; | |
315 | medusa_set_resolution(dev, fh->width, SRAM_CH07); | |
316 | ||
317 | dprintk(2, "%s() width=%d height=%d field=%d\n", __func__, fh->width, | |
318 | fh->height, fh->vidq.field); | |
319 | cx25821_call_all(dev, video, s_fmt, f); | |
320 | ||
321 | return 0; | |
02b20b0b MCC |
322 | } |
323 | ||
324 | static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p) | |
325 | { | |
1a9fc855 MCC |
326 | int ret_val = 0; |
327 | struct cx25821_fh *fh = priv; | |
328 | struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; | |
02b20b0b | 329 | |
1a9fc855 | 330 | ret_val = videobuf_dqbuf(get_queue(fh), p, file->f_flags & O_NONBLOCK); |
02b20b0b | 331 | |
1a9fc855 | 332 | p->sequence = dev->vidq[SRAM_CH07].count; |
02b20b0b | 333 | |
1a9fc855 | 334 | return ret_val; |
02b20b0b | 335 | } |
1a9fc855 | 336 | static int vidioc_log_status(struct file *file, void *priv) |
02b20b0b | 337 | { |
1a9fc855 MCC |
338 | struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; |
339 | char name[32 + 2]; | |
02b20b0b | 340 | |
1a9fc855 MCC |
341 | struct sram_channel *sram_ch = &dev->sram_channels[SRAM_CH07]; |
342 | u32 tmp = 0; | |
02b20b0b | 343 | |
1a9fc855 MCC |
344 | snprintf(name, sizeof(name), "%s/2", dev->name); |
345 | printk(KERN_INFO "%s/2: ============ START LOG STATUS ============\n", | |
02b20b0b | 346 | dev->name); |
1a9fc855 | 347 | cx25821_call_all(dev, core, log_status); |
bb4c9a74 | 348 | |
1a9fc855 MCC |
349 | tmp = cx_read(sram_ch->dma_ctl); |
350 | printk(KERN_INFO "Video input 7 is %s\n", | |
351 | (tmp & 0x11) ? "streaming" : "stopped"); | |
352 | printk(KERN_INFO "%s/2: ============= END LOG STATUS =============\n", | |
02b20b0b | 353 | dev->name); |
1a9fc855 | 354 | return 0; |
02b20b0b MCC |
355 | } |
356 | ||
357 | static int vidioc_s_ctrl(struct file *file, void *priv, | |
1a9fc855 | 358 | struct v4l2_control *ctl) |
02b20b0b MCC |
359 | { |
360 | struct cx25821_fh *fh = priv; | |
bb4c9a74 MCC |
361 | struct cx25821_dev *dev = ((struct cx25821_fh *)priv)->dev; |
362 | int err; | |
02b20b0b | 363 | |
bb4c9a74 | 364 | if (fh) { |
ffb4877b | 365 | err = v4l2_prio_check(&dev->prio, fh->prio); |
02b20b0b MCC |
366 | if (0 != err) |
367 | return err; | |
368 | } | |
369 | ||
370 | return cx25821_set_control(dev, ctl, SRAM_CH07); | |
371 | } | |
372 | ||
373 | // exported stuff | |
374 | static const struct v4l2_file_operations video_fops = { | |
1a9fc855 MCC |
375 | .owner = THIS_MODULE, |
376 | .open = video_open, | |
377 | .release = video_release, | |
378 | .read = video_read, | |
379 | .poll = video_poll, | |
f2466d63 | 380 | .mmap = cx25821_video_mmap, |
1a9fc855 | 381 | .ioctl = video_ioctl2, |
02b20b0b MCC |
382 | }; |
383 | ||
384 | static const struct v4l2_ioctl_ops video_ioctl_ops = { | |
f2466d63 MCC |
385 | .vidioc_querycap = cx25821_vidioc_querycap, |
386 | .vidioc_enum_fmt_vid_cap = cx25821_vidioc_enum_fmt_vid_cap, | |
387 | .vidioc_g_fmt_vid_cap = cx25821_vidioc_g_fmt_vid_cap, | |
388 | .vidioc_try_fmt_vid_cap = cx25821_vidioc_try_fmt_vid_cap, | |
1a9fc855 | 389 | .vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap, |
f2466d63 MCC |
390 | .vidioc_reqbufs = cx25821_vidioc_reqbufs, |
391 | .vidioc_querybuf = cx25821_vidioc_querybuf, | |
392 | .vidioc_qbuf = cx25821_vidioc_qbuf, | |
1a9fc855 | 393 | .vidioc_dqbuf = vidioc_dqbuf, |
02b20b0b | 394 | #ifdef TUNER_FLAG |
f2466d63 MCC |
395 | .vidioc_s_std = cx25821_vidioc_s_std, |
396 | .vidioc_querystd = cx25821_vidioc_querystd, | |
02b20b0b | 397 | #endif |
f2466d63 MCC |
398 | .vidioc_cropcap = cx25821_vidioc_cropcap, |
399 | .vidioc_s_crop = cx25821_vidioc_s_crop, | |
400 | .vidioc_g_crop = cx25821_vidioc_g_crop, | |
401 | .vidioc_enum_input = cx25821_vidioc_enum_input, | |
402 | .vidioc_g_input = cx25821_vidioc_g_input, | |
403 | .vidioc_s_input = cx25821_vidioc_s_input, | |
404 | .vidioc_g_ctrl = cx25821_vidioc_g_ctrl, | |
1a9fc855 | 405 | .vidioc_s_ctrl = vidioc_s_ctrl, |
f2466d63 | 406 | .vidioc_queryctrl = cx25821_vidioc_queryctrl, |
1a9fc855 MCC |
407 | .vidioc_streamon = vidioc_streamon, |
408 | .vidioc_streamoff = vidioc_streamoff, | |
409 | .vidioc_log_status = vidioc_log_status, | |
f2466d63 MCC |
410 | .vidioc_g_priority = cx25821_vidioc_g_priority, |
411 | .vidioc_s_priority = cx25821_vidioc_s_priority, | |
02b20b0b | 412 | #ifdef CONFIG_VIDEO_V4L1_COMPAT |
f2466d63 | 413 | .vidiocgmbuf = cx25821_vidiocgmbuf, |
02b20b0b MCC |
414 | #endif |
415 | #ifdef TUNER_FLAG | |
f2466d63 MCC |
416 | .vidioc_g_tuner = cx25821_vidioc_g_tuner, |
417 | .vidioc_s_tuner = cx25821_vidioc_s_tuner, | |
418 | .vidioc_g_frequency = cx25821_vidioc_g_frequency, | |
419 | .vidioc_s_frequency = cx25821_vidioc_s_frequency, | |
02b20b0b MCC |
420 | #endif |
421 | #ifdef CONFIG_VIDEO_ADV_DEBUG | |
f2466d63 MCC |
422 | .vidioc_g_register = cx25821_vidioc_g_register, |
423 | .vidioc_s_register = cx25821_vidioc_s_register, | |
02b20b0b MCC |
424 | #endif |
425 | }; | |
426 | ||
427 | struct video_device cx25821_video_template7 = { | |
1a9fc855 MCC |
428 | .name = "cx25821-video", |
429 | .fops = &video_fops, | |
1a9fc855 MCC |
430 | .ioctl_ops = &video_ioctl_ops, |
431 | .tvnorms = CX25821_NORMS, | |
432 | .current_norm = V4L2_STD_NTSC_M, | |
02b20b0b | 433 | }; |