Commit | Line | Data |
---|---|---|
faa4fd2a BC |
1 | /* |
2 | * Copyright (C) 2010 Bluecherry, LLC www.bluecherrydvr.com | |
3 | * Copyright (C) 2010 Ben Collins <bcollins@bluecherry.net> | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
18 | */ | |
19 | ||
20 | #include <linux/kernel.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/kthread.h> | |
23 | #include <linux/freezer.h> | |
24 | ||
25 | #include <media/v4l2-ioctl.h> | |
26 | #include <media/v4l2-common.h> | |
f62de9be | 27 | #include <media/videobuf-dma-sg.h> |
faa4fd2a BC |
28 | |
29 | #include "solo6010.h" | |
30 | #include "solo6010-tw28.h" | |
31 | ||
32 | #define SOLO_HW_BPL 2048 | |
33 | #define SOLO_DISP_PIX_FIELD V4L2_FIELD_INTERLACED | |
faa4fd2a BC |
34 | |
35 | /* Image size is two fields, SOLO_HW_BPL is one horizontal line */ | |
36 | #define solo_vlines(__solo) (__solo->video_vsize * 2) | |
37 | #define solo_image_size(__solo) (solo_bytesperline(__solo) * \ | |
38 | solo_vlines(__solo)) | |
39 | #define solo_bytesperline(__solo) (__solo->video_hsize * 2) | |
40 | ||
41 | #define MIN_VID_BUFFERS 4 | |
42 | ||
43 | /* Simple file handle */ | |
44 | struct solo_filehandle { | |
45 | struct solo6010_dev *solo_dev; | |
46 | struct videobuf_queue vidq; | |
47 | struct task_struct *kthread; | |
48 | spinlock_t slock; | |
49 | int old_write; | |
50 | struct list_head vidq_active; | |
f62de9be BC |
51 | struct p2m_desc desc[SOLO_NR_P2M_DESC]; |
52 | int desc_idx; | |
faa4fd2a BC |
53 | }; |
54 | ||
55 | unsigned video_nr = -1; | |
56 | module_param(video_nr, uint, 0644); | |
57 | MODULE_PARM_DESC(video_nr, "videoX start number, -1 is autodetect (default)"); | |
58 | ||
59 | static void erase_on(struct solo6010_dev *solo_dev) | |
60 | { | |
61 | solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, SOLO_VO_DISP_ERASE_ON); | |
62 | solo_dev->erasing = 1; | |
63 | solo_dev->frame_blank = 0; | |
64 | } | |
65 | ||
66 | static int erase_off(struct solo6010_dev *solo_dev) | |
67 | { | |
68 | if (!solo_dev->erasing) | |
69 | return 0; | |
70 | ||
71 | /* First time around, assert erase off */ | |
72 | if (!solo_dev->frame_blank) | |
73 | solo_reg_write(solo_dev, SOLO_VO_DISP_ERASE, 0); | |
74 | /* Keep the erasing flag on for 8 frames minimum */ | |
75 | if (solo_dev->frame_blank++ >= 8) | |
76 | solo_dev->erasing = 0; | |
77 | ||
78 | return 1; | |
79 | } | |
80 | ||
81 | void solo_video_in_isr(struct solo6010_dev *solo_dev) | |
82 | { | |
83 | solo_reg_write(solo_dev, SOLO_IRQ_STAT, SOLO_IRQ_VIDEO_IN); | |
84 | wake_up_interruptible(&solo_dev->disp_thread_wait); | |
85 | } | |
86 | ||
87 | static void solo_win_setup(struct solo6010_dev *solo_dev, u8 ch, | |
88 | int sx, int sy, int ex, int ey, int scale) | |
89 | { | |
90 | if (ch >= solo_dev->nr_chans) | |
91 | return; | |
92 | ||
93 | /* Here, we just keep window/channel the same */ | |
94 | solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL0(ch), | |
95 | SOLO_VI_WIN_CHANNEL(ch) | | |
96 | SOLO_VI_WIN_SX(sx) | | |
97 | SOLO_VI_WIN_EX(ex) | | |
98 | SOLO_VI_WIN_SCALE(scale)); | |
99 | ||
f62de9be | 100 | solo_reg_write(solo_dev, SOLO_VI_WIN_CTRL1(ch), |
faa4fd2a BC |
101 | SOLO_VI_WIN_SY(sy) | |
102 | SOLO_VI_WIN_EY(ey)); | |
103 | } | |
104 | ||
105 | static int solo_v4l2_ch_ext_4up(struct solo6010_dev *solo_dev, u8 idx, int on) | |
106 | { | |
107 | u8 ch = idx * 4; | |
108 | ||
109 | if (ch >= solo_dev->nr_chans) | |
110 | return -EINVAL; | |
111 | ||
112 | if (!on) { | |
113 | u8 i; | |
114 | for (i = ch; i < ch + 4; i++) | |
115 | solo_win_setup(solo_dev, i, solo_dev->video_hsize, | |
116 | solo_vlines(solo_dev), | |
117 | solo_dev->video_hsize, | |
118 | solo_vlines(solo_dev), 0); | |
119 | return 0; | |
120 | } | |
121 | ||
122 | /* Row 1 */ | |
123 | solo_win_setup(solo_dev, ch, 0, 0, solo_dev->video_hsize / 2, | |
124 | solo_vlines(solo_dev) / 2, 3); | |
125 | solo_win_setup(solo_dev, ch + 1, solo_dev->video_hsize / 2, 0, | |
126 | solo_dev->video_hsize, solo_vlines(solo_dev) / 2, 3); | |
127 | /* Row 2 */ | |
128 | solo_win_setup(solo_dev, ch + 2, 0, solo_vlines(solo_dev) / 2, | |
129 | solo_dev->video_hsize / 2, solo_vlines(solo_dev), 3); | |
130 | solo_win_setup(solo_dev, ch + 3, solo_dev->video_hsize / 2, | |
131 | solo_vlines(solo_dev) / 2, solo_dev->video_hsize, | |
132 | solo_vlines(solo_dev), 3); | |
133 | ||
134 | return 0; | |
135 | } | |
136 | ||
137 | static int solo_v4l2_ch_ext_16up(struct solo6010_dev *solo_dev, int on) | |
138 | { | |
139 | int sy, ysize, hsize, i; | |
140 | ||
141 | if (!on) { | |
142 | for (i = 0; i < 16; i++) | |
143 | solo_win_setup(solo_dev, i, solo_dev->video_hsize, | |
144 | solo_vlines(solo_dev), | |
145 | solo_dev->video_hsize, | |
146 | solo_vlines(solo_dev), 0); | |
147 | return 0; | |
148 | } | |
149 | ||
150 | ysize = solo_vlines(solo_dev) / 4; | |
151 | hsize = solo_dev->video_hsize / 4; | |
152 | ||
153 | for (sy = 0, i = 0; i < 4; i++, sy += ysize) { | |
154 | solo_win_setup(solo_dev, i * 4, 0, sy, hsize, | |
155 | sy + ysize, 5); | |
156 | solo_win_setup(solo_dev, (i * 4) + 1, hsize, sy, | |
157 | hsize * 2, sy + ysize, 5); | |
158 | solo_win_setup(solo_dev, (i * 4) + 2, hsize * 2, sy, | |
159 | hsize * 3, sy + ysize, 5); | |
160 | solo_win_setup(solo_dev, (i * 4) + 3, hsize * 3, sy, | |
161 | solo_dev->video_hsize, sy + ysize, 5); | |
162 | } | |
163 | ||
164 | return 0; | |
165 | } | |
166 | ||
167 | static int solo_v4l2_ch(struct solo6010_dev *solo_dev, u8 ch, int on) | |
168 | { | |
169 | u8 ext_ch; | |
170 | ||
171 | if (ch < solo_dev->nr_chans) { | |
172 | solo_win_setup(solo_dev, ch, on ? 0 : solo_dev->video_hsize, | |
173 | on ? 0 : solo_vlines(solo_dev), | |
174 | solo_dev->video_hsize, solo_vlines(solo_dev), | |
175 | on ? 1 : 0); | |
176 | return 0; | |
177 | } | |
178 | ||
179 | if (ch >= solo_dev->nr_chans + solo_dev->nr_ext) | |
180 | return -EINVAL; | |
181 | ||
182 | ext_ch = ch - solo_dev->nr_chans; | |
183 | ||
184 | /* 4up's first */ | |
185 | if (ext_ch < 4) | |
186 | return solo_v4l2_ch_ext_4up(solo_dev, ext_ch, on); | |
187 | ||
188 | /* Remaining case is 16up for 16-port */ | |
189 | return solo_v4l2_ch_ext_16up(solo_dev, on); | |
190 | } | |
191 | ||
192 | static int solo_v4l2_set_ch(struct solo6010_dev *solo_dev, u8 ch) | |
193 | { | |
194 | if (ch >= solo_dev->nr_chans + solo_dev->nr_ext) | |
195 | return -EINVAL; | |
196 | ||
197 | erase_on(solo_dev); | |
198 | ||
199 | solo_v4l2_ch(solo_dev, solo_dev->cur_disp_ch, 0); | |
200 | solo_v4l2_ch(solo_dev, ch, 1); | |
201 | ||
202 | solo_dev->cur_disp_ch = ch; | |
203 | ||
204 | return 0; | |
205 | } | |
206 | ||
f62de9be BC |
207 | static void disp_reset_desc(struct solo_filehandle *fh) |
208 | { | |
209 | fh->desc_idx = 0; | |
210 | } | |
211 | ||
212 | static int disp_flush_descs(struct solo_filehandle *fh) | |
213 | { | |
214 | int ret; | |
215 | ||
216 | if (!fh->desc_idx) | |
217 | return 0; | |
218 | ||
219 | ret = solo_p2m_dma_desc(fh->solo_dev, SOLO_P2M_DMA_ID_DISP, | |
220 | fh->desc, fh->desc_idx); | |
221 | disp_reset_desc(fh); | |
222 | ||
223 | return ret; | |
224 | } | |
225 | ||
226 | static int disp_push_desc(struct solo_filehandle *fh, dma_addr_t dma_addr, | |
227 | u32 ext_addr, int size, int repeat, int ext_size) | |
228 | { | |
229 | if (fh->desc_idx >= SOLO_NR_P2M_DESC) { | |
230 | int ret = disp_flush_descs(fh); | |
231 | if (ret) | |
232 | return ret; | |
233 | } | |
234 | ||
235 | solo_p2m_push_desc(&fh->desc[fh->desc_idx], 0, dma_addr, ext_addr, | |
236 | size, repeat, ext_size); | |
237 | fh->desc_idx++; | |
238 | ||
239 | return 0; | |
240 | } | |
241 | ||
faa4fd2a BC |
242 | static void solo_fillbuf(struct solo_filehandle *fh, |
243 | struct videobuf_buffer *vb) | |
244 | { | |
245 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
f62de9be | 246 | struct videobuf_dmabuf* vbuf; |
faa4fd2a | 247 | unsigned int fdma_addr; |
faa4fd2a BC |
248 | int error = 1; |
249 | int i; | |
f62de9be BC |
250 | struct scatterlist* sg; |
251 | dma_addr_t sg_dma; | |
252 | int sg_size_left; | |
faa4fd2a | 253 | |
f62de9be | 254 | if (!(vbuf = videobuf_to_dma(vb))) |
faa4fd2a BC |
255 | goto finish_buf; |
256 | ||
257 | if (erase_off(solo_dev)) { | |
f62de9be BC |
258 | int i; |
259 | ||
260 | /* Just blit to the entire sg list, ignoring size */ | |
261 | for_each_sg(vbuf->sglist, sg, vbuf->sglen, i) { | |
262 | void *p = sg_virt(sg); | |
263 | size_t len = sg_dma_len(sg); | |
264 | ||
265 | for (i = 0; i < len; i += 2) { | |
266 | ((u8 *)p)[i] = 0x80; | |
267 | ((u8 *)p)[i + 1] = 0x00; | |
268 | } | |
faa4fd2a | 269 | } |
f62de9be | 270 | |
faa4fd2a BC |
271 | error = 0; |
272 | goto finish_buf; | |
273 | } | |
274 | ||
f62de9be BC |
275 | disp_reset_desc(fh); |
276 | sg = vbuf->sglist; | |
277 | sg_dma = sg_dma_address(sg); | |
278 | sg_size_left = sg_dma_len(sg); | |
279 | ||
280 | fdma_addr = SOLO_DISP_EXT_ADDR(solo_dev) + (fh->old_write * | |
281 | (SOLO_HW_BPL * solo_vlines(solo_dev))); | |
faa4fd2a | 282 | |
f62de9be BC |
283 | for (i = 0; i < solo_vlines(solo_dev); i++) { |
284 | int line_len = solo_bytesperline(solo_dev); | |
285 | int lines; | |
286 | ||
287 | if (!sg_size_left) { | |
288 | sg = sg_next(sg); | |
289 | if (sg == NULL) | |
faa4fd2a | 290 | goto finish_buf; |
f62de9be BC |
291 | sg_dma = sg_dma_address(sg); |
292 | sg_size_left = sg_dma_len(sg); | |
faa4fd2a | 293 | } |
f62de9be BC |
294 | |
295 | /* No room for an entire line, so chunk it up */ | |
296 | if (sg_size_left < line_len) { | |
297 | int this_addr = fdma_addr; | |
298 | ||
299 | while (line_len > 0) { | |
300 | int this_write; | |
301 | ||
302 | if (!sg_size_left) { | |
303 | sg = sg_next(sg); | |
304 | if (sg == NULL) | |
305 | goto finish_buf; | |
306 | sg_dma = sg_dma_address(sg); | |
307 | sg_size_left = sg_dma_len(sg); | |
308 | } | |
309 | ||
310 | this_write = min(sg_size_left, line_len); | |
311 | ||
312 | if (disp_push_desc(fh, sg_dma, this_addr, | |
313 | this_write, 0, 0)) | |
314 | goto finish_buf; | |
315 | ||
316 | line_len -= this_write; | |
317 | sg_size_left -= this_write; | |
318 | sg_dma += this_write; | |
319 | this_addr += this_write; | |
320 | } | |
321 | ||
322 | fdma_addr += SOLO_HW_BPL; | |
323 | continue; | |
324 | } | |
325 | ||
326 | /* Shove as many lines into a repeating descriptor as possible */ | |
327 | lines = min(sg_size_left / line_len, | |
328 | solo_vlines(solo_dev) - i); | |
329 | ||
330 | if (disp_push_desc(fh, sg_dma, fdma_addr, line_len, | |
331 | lines - 1, SOLO_HW_BPL)) | |
332 | goto finish_buf; | |
333 | ||
334 | i += lines - 1; | |
335 | fdma_addr += SOLO_HW_BPL * lines; | |
336 | sg_dma += lines * line_len; | |
337 | sg_size_left -= lines * line_len; | |
faa4fd2a | 338 | } |
f62de9be BC |
339 | |
340 | error = disp_flush_descs(fh); | |
faa4fd2a BC |
341 | |
342 | finish_buf: | |
343 | if (error) { | |
344 | vb->state = VIDEOBUF_ERROR; | |
345 | } else { | |
f62de9be | 346 | vb->size = solo_vlines(solo_dev) * solo_bytesperline(solo_dev); |
faa4fd2a BC |
347 | vb->state = VIDEOBUF_DONE; |
348 | vb->field_count++; | |
349 | do_gettimeofday(&vb->ts); | |
350 | } | |
351 | ||
352 | wake_up(&vb->done); | |
353 | ||
354 | return; | |
355 | } | |
356 | ||
357 | static void solo_thread_try(struct solo_filehandle *fh) | |
358 | { | |
359 | struct videobuf_buffer *vb; | |
360 | unsigned int cur_write; | |
361 | ||
362 | for (;;) { | |
363 | spin_lock(&fh->slock); | |
364 | ||
365 | if (list_empty(&fh->vidq_active)) | |
366 | break; | |
367 | ||
368 | vb = list_first_entry(&fh->vidq_active, struct videobuf_buffer, | |
369 | queue); | |
370 | ||
371 | if (!waitqueue_active(&vb->done)) | |
372 | break; | |
373 | ||
374 | cur_write = SOLO_VI_STATUS0_PAGE(solo_reg_read(fh->solo_dev, | |
f62de9be | 375 | SOLO_VI_STATUS0)); |
faa4fd2a BC |
376 | if (cur_write == fh->old_write) |
377 | break; | |
378 | ||
379 | fh->old_write = cur_write; | |
380 | list_del(&vb->queue); | |
381 | ||
382 | spin_unlock(&fh->slock); | |
383 | ||
384 | solo_fillbuf(fh, vb); | |
385 | } | |
386 | ||
387 | assert_spin_locked(&fh->slock); | |
388 | spin_unlock(&fh->slock); | |
389 | } | |
390 | ||
391 | static int solo_thread(void *data) | |
392 | { | |
393 | struct solo_filehandle *fh = data; | |
394 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
395 | DECLARE_WAITQUEUE(wait, current); | |
396 | ||
397 | set_freezable(); | |
398 | add_wait_queue(&solo_dev->disp_thread_wait, &wait); | |
399 | ||
400 | for (;;) { | |
401 | long timeout = schedule_timeout_interruptible(HZ); | |
402 | if (timeout == -ERESTARTSYS || kthread_should_stop()) | |
403 | break; | |
404 | solo_thread_try(fh); | |
405 | try_to_freeze(); | |
406 | } | |
407 | ||
408 | remove_wait_queue(&solo_dev->disp_thread_wait, &wait); | |
409 | ||
f62de9be | 410 | return 0; |
faa4fd2a BC |
411 | } |
412 | ||
413 | static int solo_start_thread(struct solo_filehandle *fh) | |
414 | { | |
415 | fh->kthread = kthread_run(solo_thread, fh, SOLO6010_NAME "_disp"); | |
416 | ||
417 | if (IS_ERR(fh->kthread)) | |
418 | return PTR_ERR(fh->kthread); | |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
423 | static void solo_stop_thread(struct solo_filehandle *fh) | |
424 | { | |
425 | if (fh->kthread) { | |
426 | kthread_stop(fh->kthread); | |
427 | fh->kthread = NULL; | |
428 | } | |
429 | } | |
430 | ||
431 | static int solo_buf_setup(struct videobuf_queue *vq, unsigned int *count, | |
432 | unsigned int *size) | |
433 | { | |
434 | struct solo_filehandle *fh = vq->priv_data; | |
435 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
436 | ||
f62de9be | 437 | *size = solo_image_size(solo_dev); |
faa4fd2a | 438 | |
f62de9be | 439 | if (*count < MIN_VID_BUFFERS) |
faa4fd2a BC |
440 | *count = MIN_VID_BUFFERS; |
441 | ||
f62de9be | 442 | return 0; |
faa4fd2a BC |
443 | } |
444 | ||
445 | static int solo_buf_prepare(struct videobuf_queue *vq, | |
446 | struct videobuf_buffer *vb, enum v4l2_field field) | |
447 | { | |
448 | struct solo_filehandle *fh = vq->priv_data; | |
449 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
450 | ||
451 | vb->size = solo_image_size(solo_dev); | |
452 | if (vb->baddr != 0 && vb->bsize < vb->size) | |
453 | return -EINVAL; | |
454 | ||
455 | /* XXX: These properties only change when queue is idle */ | |
456 | vb->width = solo_dev->video_hsize; | |
457 | vb->height = solo_vlines(solo_dev); | |
458 | vb->bytesperline = solo_bytesperline(solo_dev); | |
459 | vb->field = field; | |
460 | ||
461 | if (vb->state == VIDEOBUF_NEEDS_INIT) { | |
462 | int rc = videobuf_iolock(vq, vb, NULL); | |
463 | if (rc < 0) { | |
f62de9be BC |
464 | struct videobuf_dmabuf *dma = videobuf_to_dma(vb); |
465 | videobuf_dma_unmap(vq, dma); | |
466 | videobuf_dma_free(dma); | |
faa4fd2a BC |
467 | vb->state = VIDEOBUF_NEEDS_INIT; |
468 | return rc; | |
469 | } | |
470 | } | |
471 | vb->state = VIDEOBUF_PREPARED; | |
472 | ||
473 | return 0; | |
474 | } | |
475 | ||
476 | static void solo_buf_queue(struct videobuf_queue *vq, | |
477 | struct videobuf_buffer *vb) | |
478 | { | |
479 | struct solo_filehandle *fh = vq->priv_data; | |
480 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
481 | ||
482 | vb->state = VIDEOBUF_QUEUED; | |
483 | list_add_tail(&vb->queue, &fh->vidq_active); | |
484 | wake_up_interruptible(&solo_dev->disp_thread_wait); | |
485 | } | |
486 | ||
487 | static void solo_buf_release(struct videobuf_queue *vq, | |
488 | struct videobuf_buffer *vb) | |
489 | { | |
f62de9be BC |
490 | struct videobuf_dmabuf *dma = videobuf_to_dma(vb); |
491 | ||
492 | videobuf_dma_unmap(vq, dma); | |
493 | videobuf_dma_free(dma); | |
faa4fd2a BC |
494 | vb->state = VIDEOBUF_NEEDS_INIT; |
495 | } | |
496 | ||
497 | static struct videobuf_queue_ops solo_video_qops = { | |
498 | .buf_setup = solo_buf_setup, | |
499 | .buf_prepare = solo_buf_prepare, | |
500 | .buf_queue = solo_buf_queue, | |
501 | .buf_release = solo_buf_release, | |
502 | }; | |
503 | ||
504 | static unsigned int solo_v4l2_poll(struct file *file, | |
505 | struct poll_table_struct *wait) | |
506 | { | |
507 | struct solo_filehandle *fh = file->private_data; | |
508 | ||
f62de9be | 509 | return videobuf_poll_stream(file, &fh->vidq, wait); |
faa4fd2a BC |
510 | } |
511 | ||
512 | static int solo_v4l2_mmap(struct file *file, struct vm_area_struct *vma) | |
513 | { | |
514 | struct solo_filehandle *fh = file->private_data; | |
515 | ||
516 | return videobuf_mmap_mapper(&fh->vidq, vma); | |
517 | } | |
518 | ||
519 | static int solo_v4l2_open(struct file *file) | |
520 | { | |
521 | struct solo6010_dev *solo_dev = video_drvdata(file); | |
522 | struct solo_filehandle *fh; | |
523 | int ret; | |
524 | ||
525 | if ((fh = kzalloc(sizeof(*fh), GFP_KERNEL)) == NULL) | |
526 | return -ENOMEM; | |
527 | ||
528 | spin_lock_init(&fh->slock); | |
529 | INIT_LIST_HEAD(&fh->vidq_active); | |
530 | fh->solo_dev = solo_dev; | |
531 | file->private_data = fh; | |
532 | ||
533 | if ((ret = solo_start_thread(fh))) { | |
534 | kfree(fh); | |
535 | return ret; | |
536 | } | |
537 | ||
f62de9be BC |
538 | videobuf_queue_sg_init(&fh->vidq, &solo_video_qops, |
539 | &solo_dev->pdev->dev, &fh->slock, | |
540 | V4L2_BUF_TYPE_VIDEO_CAPTURE, | |
541 | SOLO_DISP_PIX_FIELD, | |
542 | sizeof(struct videobuf_buffer), fh); | |
faa4fd2a BC |
543 | |
544 | return 0; | |
545 | } | |
546 | ||
547 | static ssize_t solo_v4l2_read(struct file *file, char __user *data, | |
548 | size_t count, loff_t *ppos) | |
549 | { | |
550 | struct solo_filehandle *fh = file->private_data; | |
551 | ||
552 | return videobuf_read_stream(&fh->vidq, data, count, ppos, 0, | |
553 | file->f_flags & O_NONBLOCK); | |
554 | } | |
555 | ||
556 | static int solo_v4l2_release(struct file *file) | |
557 | { | |
558 | struct solo_filehandle *fh = file->private_data; | |
559 | ||
560 | videobuf_stop(&fh->vidq); | |
561 | videobuf_mmap_free(&fh->vidq); | |
562 | solo_stop_thread(fh); | |
563 | kfree(fh); | |
564 | ||
565 | return 0; | |
566 | } | |
567 | ||
568 | static int solo_querycap(struct file *file, void *priv, | |
569 | struct v4l2_capability *cap) | |
570 | { | |
571 | struct solo_filehandle *fh = priv; | |
572 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
573 | ||
574 | strcpy(cap->driver, SOLO6010_NAME); | |
575 | strcpy(cap->card, "Softlogic 6010"); | |
576 | snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI %s", | |
577 | pci_name(solo_dev->pdev)); | |
578 | cap->version = SOLO6010_VER_NUM; | |
579 | cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | | |
580 | V4L2_CAP_READWRITE | | |
581 | V4L2_CAP_STREAMING; | |
582 | return 0; | |
583 | } | |
584 | ||
585 | static int solo_enum_ext_input(struct solo6010_dev *solo_dev, | |
586 | struct v4l2_input *input) | |
587 | { | |
588 | static const char *dispnames_1[] = { "4UP" }; | |
589 | static const char *dispnames_2[] = { "4UP-1", "4UP-2" }; | |
590 | static const char *dispnames_5[] = { | |
591 | "4UP-1", "4UP-2", "4UP-3", "4UP-4", "16UP" | |
592 | }; | |
593 | const char **dispnames; | |
594 | ||
595 | if (input->index >= (solo_dev->nr_chans + solo_dev->nr_ext)) | |
596 | return -EINVAL; | |
597 | ||
598 | if (solo_dev->nr_ext == 5) | |
599 | dispnames = dispnames_5; | |
600 | else if (solo_dev->nr_ext == 2) | |
601 | dispnames = dispnames_2; | |
602 | else | |
603 | dispnames = dispnames_1; | |
604 | ||
605 | snprintf(input->name, sizeof(input->name), "Multi %s", | |
606 | dispnames[input->index - solo_dev->nr_chans]); | |
607 | ||
608 | return 0; | |
609 | } | |
610 | ||
611 | static int solo_enum_input(struct file *file, void *priv, | |
612 | struct v4l2_input *input) | |
613 | { | |
614 | struct solo_filehandle *fh = priv; | |
615 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
616 | ||
617 | if (input->index >= solo_dev->nr_chans) { | |
618 | int ret = solo_enum_ext_input(solo_dev, input); | |
619 | if (ret < 0) | |
620 | return ret; | |
621 | } else { | |
622 | snprintf(input->name, sizeof(input->name), "Camera %d", | |
623 | input->index + 1); | |
624 | ||
625 | /* We can only check this for normal inputs */ | |
626 | if (!tw28_get_video_status(solo_dev, input->index)) | |
627 | input->status = V4L2_IN_ST_NO_SIGNAL; | |
628 | } | |
629 | ||
630 | input->type = V4L2_INPUT_TYPE_CAMERA; | |
631 | ||
632 | if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) | |
633 | input->std = V4L2_STD_NTSC_M; | |
634 | else | |
f62de9be | 635 | input->std = V4L2_STD_PAL_B; |
faa4fd2a BC |
636 | |
637 | return 0; | |
638 | } | |
639 | ||
640 | static int solo_set_input(struct file *file, void *priv, unsigned int index) | |
641 | { | |
642 | struct solo_filehandle *fh = priv; | |
643 | ||
644 | return solo_v4l2_set_ch(fh->solo_dev, index); | |
645 | } | |
646 | ||
647 | static int solo_get_input(struct file *file, void *priv, unsigned int *index) | |
648 | { | |
649 | struct solo_filehandle *fh = priv; | |
650 | ||
651 | *index = fh->solo_dev->cur_disp_ch; | |
652 | ||
653 | return 0; | |
654 | } | |
655 | ||
656 | static int solo_enum_fmt_cap(struct file *file, void *priv, | |
657 | struct v4l2_fmtdesc *f) | |
658 | { | |
659 | if (f->index) | |
660 | return -EINVAL; | |
661 | ||
662 | f->pixelformat = V4L2_PIX_FMT_UYVY; | |
663 | strlcpy(f->description, "UYUV 4:2:2 Packed", sizeof(f->description)); | |
664 | ||
665 | return 0; | |
666 | } | |
667 | ||
668 | static int solo_try_fmt_cap(struct file *file, void *priv, | |
669 | struct v4l2_format *f) | |
670 | { | |
671 | struct solo_filehandle *fh = priv; | |
672 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
673 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
674 | int image_size = solo_image_size(solo_dev); | |
675 | ||
676 | /* Check supported sizes */ | |
677 | if (pix->width != solo_dev->video_hsize) | |
678 | pix->width = solo_dev->video_hsize; | |
679 | if (pix->height != solo_vlines(solo_dev)) | |
680 | pix->height = solo_vlines(solo_dev); | |
681 | if (pix->sizeimage != image_size) | |
682 | pix->sizeimage = image_size; | |
683 | ||
684 | /* Check formats */ | |
685 | if (pix->field == V4L2_FIELD_ANY) | |
686 | pix->field = SOLO_DISP_PIX_FIELD; | |
687 | ||
688 | if (pix->pixelformat != V4L2_PIX_FMT_UYVY || | |
689 | pix->field != SOLO_DISP_PIX_FIELD || | |
690 | pix->colorspace != V4L2_COLORSPACE_SMPTE170M) | |
691 | return -EINVAL; | |
692 | ||
693 | return 0; | |
694 | } | |
695 | ||
696 | static int solo_set_fmt_cap(struct file *file, void *priv, | |
697 | struct v4l2_format *f) | |
698 | { | |
699 | struct solo_filehandle *fh = priv; | |
700 | ||
701 | if (videobuf_queue_is_busy(&fh->vidq)) | |
702 | return -EBUSY; | |
703 | ||
704 | /* For right now, if it doesn't match our running config, | |
705 | * then fail */ | |
706 | return solo_try_fmt_cap(file, priv, f); | |
707 | } | |
708 | ||
709 | static int solo_get_fmt_cap(struct file *file, void *priv, | |
710 | struct v4l2_format *f) | |
711 | { | |
712 | struct solo_filehandle *fh = priv; | |
713 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
714 | struct v4l2_pix_format *pix = &f->fmt.pix; | |
715 | ||
716 | pix->width = solo_dev->video_hsize; | |
717 | pix->height = solo_vlines(solo_dev); | |
718 | pix->pixelformat = V4L2_PIX_FMT_UYVY; | |
719 | pix->field = SOLO_DISP_PIX_FIELD; | |
720 | pix->sizeimage = solo_image_size(solo_dev); | |
721 | pix->colorspace = V4L2_COLORSPACE_SMPTE170M; | |
722 | pix->bytesperline = solo_bytesperline(solo_dev); | |
723 | ||
724 | return 0; | |
725 | } | |
726 | ||
727 | static int solo_reqbufs(struct file *file, void *priv, | |
728 | struct v4l2_requestbuffers *req) | |
729 | { | |
730 | struct solo_filehandle *fh = priv; | |
731 | ||
732 | return videobuf_reqbufs(&fh->vidq, req); | |
733 | } | |
734 | ||
735 | static int solo_querybuf(struct file *file, void *priv, struct v4l2_buffer *buf) | |
736 | { | |
737 | struct solo_filehandle *fh = priv; | |
738 | ||
739 | return videobuf_querybuf(&fh->vidq, buf); | |
740 | } | |
741 | ||
742 | static int solo_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf) | |
743 | { | |
744 | struct solo_filehandle *fh = priv; | |
745 | ||
746 | return videobuf_qbuf(&fh->vidq, buf); | |
747 | } | |
748 | ||
749 | static int solo_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf) | |
750 | { | |
751 | struct solo_filehandle *fh = priv; | |
752 | ||
753 | return videobuf_dqbuf(&fh->vidq, buf, file->f_flags & O_NONBLOCK); | |
754 | } | |
755 | ||
756 | static int solo_streamon(struct file *file, void *priv, enum v4l2_buf_type i) | |
757 | { | |
758 | struct solo_filehandle *fh = priv; | |
759 | ||
760 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
761 | return -EINVAL; | |
762 | ||
763 | return videobuf_streamon(&fh->vidq); | |
764 | } | |
765 | ||
766 | static int solo_streamoff(struct file *file, void *priv, enum v4l2_buf_type i) | |
767 | { | |
768 | struct solo_filehandle *fh = priv; | |
769 | ||
770 | if (i != V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
771 | return -EINVAL; | |
772 | ||
773 | return videobuf_streamoff(&fh->vidq); | |
774 | } | |
775 | ||
776 | static int solo_s_std(struct file *file, void *priv, v4l2_std_id *i) | |
777 | { | |
778 | return 0; | |
779 | } | |
780 | ||
781 | static const u32 solo_motion_ctrls[] = { | |
782 | V4L2_CID_MOTION_TRACE, | |
783 | 0 | |
784 | }; | |
785 | ||
786 | static const u32 *solo_ctrl_classes[] = { | |
787 | solo_motion_ctrls, | |
788 | NULL | |
789 | }; | |
790 | ||
791 | static int solo_disp_queryctrl(struct file *file, void *priv, | |
792 | struct v4l2_queryctrl *qc) | |
793 | { | |
794 | qc->id = v4l2_ctrl_next(solo_ctrl_classes, qc->id); | |
795 | if (!qc->id) | |
796 | return -EINVAL; | |
797 | ||
798 | switch (qc->id) { | |
799 | #ifdef PRIVATE_CIDS | |
800 | case V4L2_CID_MOTION_TRACE: | |
801 | qc->type = V4L2_CTRL_TYPE_BOOLEAN; | |
802 | qc->minimum = 0; | |
803 | qc->maximum = qc->step = 1; | |
804 | qc->default_value = 0; | |
805 | strlcpy(qc->name, "Motion Detection Trace", sizeof(qc->name)); | |
806 | return 0; | |
807 | #else | |
808 | case V4L2_CID_MOTION_TRACE: | |
809 | return v4l2_ctrl_query_fill(qc, 0, 1, 1, 0); | |
810 | #endif | |
811 | } | |
812 | return -EINVAL; | |
813 | } | |
814 | ||
815 | static int solo_disp_g_ctrl(struct file *file, void *priv, | |
816 | struct v4l2_control *ctrl) | |
817 | { | |
818 | struct solo_filehandle *fh = priv; | |
819 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
820 | ||
821 | switch (ctrl->id) { | |
822 | case V4L2_CID_MOTION_TRACE: | |
823 | ctrl->value = solo_reg_read(solo_dev, SOLO_VI_MOTION_BAR) | |
824 | ? 1 : 0; | |
825 | return 0; | |
826 | } | |
827 | return -EINVAL; | |
828 | } | |
829 | ||
830 | static int solo_disp_s_ctrl(struct file *file, void *priv, | |
831 | struct v4l2_control *ctrl) | |
832 | { | |
833 | struct solo_filehandle *fh = priv; | |
834 | struct solo6010_dev *solo_dev = fh->solo_dev; | |
835 | ||
836 | switch (ctrl->id) { | |
837 | case V4L2_CID_MOTION_TRACE: | |
838 | if (ctrl->value) { | |
839 | solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, | |
840 | SOLO_VI_MOTION_Y_ADD | | |
841 | SOLO_VI_MOTION_Y_VALUE(0x20) | | |
842 | SOLO_VI_MOTION_CB_VALUE(0x10) | | |
843 | SOLO_VI_MOTION_CR_VALUE(0x10)); | |
844 | solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, | |
845 | SOLO_VI_MOTION_CR_ADD | | |
846 | SOLO_VI_MOTION_Y_VALUE(0x10) | | |
847 | SOLO_VI_MOTION_CB_VALUE(0x80) | | |
848 | SOLO_VI_MOTION_CR_VALUE(0x10)); | |
849 | } else { | |
850 | solo_reg_write(solo_dev, SOLO_VI_MOTION_BORDER, 0); | |
851 | solo_reg_write(solo_dev, SOLO_VI_MOTION_BAR, 0); | |
852 | } | |
853 | return 0; | |
854 | } | |
855 | return -EINVAL; | |
856 | } | |
857 | ||
858 | static const struct v4l2_file_operations solo_v4l2_fops = { | |
859 | .owner = THIS_MODULE, | |
860 | .open = solo_v4l2_open, | |
861 | .release = solo_v4l2_release, | |
862 | .read = solo_v4l2_read, | |
863 | .poll = solo_v4l2_poll, | |
864 | .mmap = solo_v4l2_mmap, | |
865 | .ioctl = video_ioctl2, | |
866 | }; | |
867 | ||
868 | static const struct v4l2_ioctl_ops solo_v4l2_ioctl_ops = { | |
869 | .vidioc_querycap = solo_querycap, | |
870 | .vidioc_s_std = solo_s_std, | |
871 | /* Input callbacks */ | |
872 | .vidioc_enum_input = solo_enum_input, | |
873 | .vidioc_s_input = solo_set_input, | |
874 | .vidioc_g_input = solo_get_input, | |
875 | /* Video capture format callbacks */ | |
876 | .vidioc_enum_fmt_vid_cap = solo_enum_fmt_cap, | |
877 | .vidioc_try_fmt_vid_cap = solo_try_fmt_cap, | |
878 | .vidioc_s_fmt_vid_cap = solo_set_fmt_cap, | |
879 | .vidioc_g_fmt_vid_cap = solo_get_fmt_cap, | |
880 | /* Streaming I/O */ | |
881 | .vidioc_reqbufs = solo_reqbufs, | |
882 | .vidioc_querybuf = solo_querybuf, | |
883 | .vidioc_qbuf = solo_qbuf, | |
884 | .vidioc_dqbuf = solo_dqbuf, | |
885 | .vidioc_streamon = solo_streamon, | |
f62de9be | 886 | .vidioc_streamoff = solo_streamoff, |
faa4fd2a BC |
887 | /* Controls */ |
888 | .vidioc_queryctrl = solo_disp_queryctrl, | |
f62de9be BC |
889 | .vidioc_g_ctrl = solo_disp_g_ctrl, |
890 | .vidioc_s_ctrl = solo_disp_s_ctrl, | |
faa4fd2a BC |
891 | }; |
892 | ||
893 | static struct video_device solo_v4l2_template = { | |
894 | .name = SOLO6010_NAME, | |
895 | .fops = &solo_v4l2_fops, | |
896 | .ioctl_ops = &solo_v4l2_ioctl_ops, | |
897 | .minor = -1, | |
898 | .release = video_device_release, | |
899 | ||
f62de9be | 900 | .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_B, |
faa4fd2a BC |
901 | .current_norm = V4L2_STD_NTSC_M, |
902 | }; | |
903 | ||
904 | int solo_v4l2_init(struct solo6010_dev *solo_dev) | |
905 | { | |
906 | int ret; | |
907 | int i; | |
908 | ||
909 | init_waitqueue_head(&solo_dev->disp_thread_wait); | |
910 | ||
911 | solo_dev->vfd = video_device_alloc(); | |
912 | if (!solo_dev->vfd) | |
913 | return -ENOMEM; | |
914 | ||
915 | *solo_dev->vfd = solo_v4l2_template; | |
916 | solo_dev->vfd->parent = &solo_dev->pdev->dev; | |
917 | ||
918 | ret = video_register_device(solo_dev->vfd, VFL_TYPE_GRABBER, video_nr); | |
919 | if (ret < 0) { | |
920 | video_device_release(solo_dev->vfd); | |
921 | solo_dev->vfd = NULL; | |
922 | return ret; | |
923 | } | |
924 | ||
925 | video_set_drvdata(solo_dev->vfd, solo_dev); | |
926 | ||
927 | snprintf(solo_dev->vfd->name, sizeof(solo_dev->vfd->name), "%s (%i)", | |
928 | SOLO6010_NAME, solo_dev->vfd->num); | |
929 | ||
3850a8ae | 930 | if (video_nr != -1) |
faa4fd2a BC |
931 | video_nr++; |
932 | ||
933 | dev_info(&solo_dev->pdev->dev, "Display as /dev/video%d with " | |
934 | "%d inputs (%d extended)\n", solo_dev->vfd->num, | |
935 | solo_dev->nr_chans, solo_dev->nr_ext); | |
936 | ||
937 | /* Cycle all the channels and clear */ | |
938 | for (i = 0; i < solo_dev->nr_chans; i++) { | |
939 | solo_v4l2_set_ch(solo_dev, i); | |
940 | while (erase_off(solo_dev)) | |
941 | ;// Do nothing | |
942 | } | |
943 | ||
944 | /* Set the default display channel */ | |
945 | solo_v4l2_set_ch(solo_dev, 0); | |
946 | while (erase_off(solo_dev)) | |
947 | ;// Do nothing | |
948 | ||
949 | solo6010_irq_on(solo_dev, SOLO_IRQ_VIDEO_IN); | |
950 | ||
951 | return 0; | |
952 | } | |
953 | ||
954 | void solo_v4l2_exit(struct solo6010_dev *solo_dev) | |
955 | { | |
956 | solo6010_irq_off(solo_dev, SOLO_IRQ_VIDEO_IN); | |
957 | if (solo_dev->vfd) { | |
958 | video_unregister_device(solo_dev->vfd); | |
959 | solo_dev->vfd = NULL; | |
960 | } | |
961 | } |