[media] solo6x10: convert encoder nodes to vb2
[deliverable/linux.git] / drivers / staging / media / solo6x10 / v4l2-enc.c
1 /*
2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
3 *
4 * Original author:
5 * Ben Collins <bcollins@ubuntu.com>
6 *
7 * Additional work by:
8 * John Brooks <john.brooks@bluecherry.net>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
29
30 #include <media/v4l2-ioctl.h>
31 #include <media/v4l2-common.h>
32 #include <media/v4l2-event.h>
33 #include <media/videobuf2-dma-sg.h>
34
35 #include "solo6x10.h"
36 #include "tw28.h"
37 #include "solo6x10-jpeg.h"
38
39 #define MIN_VID_BUFFERS 2
40 #define FRAME_BUF_SIZE (196 * 1024)
41 #define MP4_QS 16
42 #define DMA_ALIGN 4096
43
44 struct solo_videobuf {
45 struct videobuf_buffer vb;
46 unsigned int flags;
47 };
48
49 /* 6010 M4V */
50 static unsigned char vop_6010_ntsc_d1[] = {
51 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
52 0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
53 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
54 0x1f, 0x4c, 0x58, 0x10, 0xf0, 0x71, 0x18, 0x3f,
55 };
56
57 static unsigned char vop_6010_ntsc_cif[] = {
58 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
59 0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
60 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
61 0x1f, 0x4c, 0x2c, 0x10, 0x78, 0x51, 0x18, 0x3f,
62 };
63
64 static unsigned char vop_6010_pal_d1[] = {
65 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
66 0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
67 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
68 0x1f, 0x4c, 0x58, 0x11, 0x20, 0x71, 0x18, 0x3f,
69 };
70
71 static unsigned char vop_6010_pal_cif[] = {
72 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
73 0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
74 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
75 0x1f, 0x4c, 0x2c, 0x10, 0x90, 0x51, 0x18, 0x3f,
76 };
77
78 /* 6110 h.264 */
79 static unsigned char vop_6110_ntsc_d1[] = {
80 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
81 0x9a, 0x74, 0x05, 0x81, 0xec, 0x80, 0x00, 0x00,
82 0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
83 };
84
85 static unsigned char vop_6110_ntsc_cif[] = {
86 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
87 0x9a, 0x74, 0x0b, 0x0f, 0xc8, 0x00, 0x00, 0x00,
88 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
89 };
90
91 static unsigned char vop_6110_pal_d1[] = {
92 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
93 0x9a, 0x74, 0x05, 0x80, 0x93, 0x20, 0x00, 0x00,
94 0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
95 };
96
97 static unsigned char vop_6110_pal_cif[] = {
98 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
99 0x9a, 0x74, 0x0b, 0x04, 0xb2, 0x00, 0x00, 0x00,
100 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
101 };
102
103 struct vop_header {
104 /* VE_STATUS0 */
105 u32 mpeg_size:20, sad_motion_flag:1, video_motion_flag:1, vop_type:2,
106 channel:5, source_fl:1, interlace:1, progressive:1;
107
108 /* VE_STATUS1 */
109 u32 vsize:8, hsize:8, last_queue:4, nop0:8, scale:4;
110
111 /* VE_STATUS2 */
112 u32 mpeg_off;
113
114 /* VE_STATUS3 */
115 u32 jpeg_off;
116
117 /* VE_STATUS4 */
118 u32 jpeg_size:20, interval:10, nop1:2;
119
120 /* VE_STATUS5/6 */
121 u32 sec, usec;
122
123 /* VE_STATUS7/8/9 */
124 u32 nop2[3];
125
126 /* VE_STATUS10 */
127 u32 mpeg_size_alt:20, nop3:12;
128
129 u32 end_nops[5];
130 } __packed;
131
132 struct solo_enc_buf {
133 enum solo_enc_types type;
134 struct vop_header *vh;
135 int motion;
136 };
137
138 static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
139 {
140 struct solo_dev *solo_dev = solo_enc->solo_dev;
141
142 return (solo_dev->motion_mask >> solo_enc->ch) & 1;
143 }
144
145 static int solo_motion_detected(struct solo_enc_dev *solo_enc)
146 {
147 struct solo_dev *solo_dev = solo_enc->solo_dev;
148 unsigned long flags;
149 u32 ch_mask = 1 << solo_enc->ch;
150 int ret = 0;
151
152 spin_lock_irqsave(&solo_enc->motion_lock, flags);
153 if (solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS) & ch_mask) {
154 solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, ch_mask);
155 ret = 1;
156 }
157 spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
158
159 return ret;
160 }
161
162 static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
163 {
164 struct solo_dev *solo_dev = solo_enc->solo_dev;
165 u32 mask = 1 << solo_enc->ch;
166 unsigned long flags;
167
168 spin_lock_irqsave(&solo_enc->motion_lock, flags);
169
170 if (on)
171 solo_dev->motion_mask |= mask;
172 else
173 solo_dev->motion_mask &= ~mask;
174
175 solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, mask);
176
177 solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
178 SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
179 (SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
180
181 spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
182 }
183
184 static void solo_update_mode(struct solo_enc_dev *solo_enc)
185 {
186 struct solo_dev *solo_dev = solo_enc->solo_dev;
187 int vop_len;
188 unsigned char *vop;
189
190 solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
191 solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
192
193 if (solo_enc->mode == SOLO_ENC_MODE_CIF) {
194 solo_enc->width = solo_dev->video_hsize >> 1;
195 solo_enc->height = solo_dev->video_vsize;
196 if (solo_dev->type == SOLO_DEV_6110) {
197 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
198 vop = vop_6110_ntsc_cif;
199 vop_len = sizeof(vop_6110_ntsc_cif);
200 } else {
201 vop = vop_6110_pal_cif;
202 vop_len = sizeof(vop_6110_pal_cif);
203 }
204 } else {
205 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
206 vop = vop_6010_ntsc_cif;
207 vop_len = sizeof(vop_6010_ntsc_cif);
208 } else {
209 vop = vop_6010_pal_cif;
210 vop_len = sizeof(vop_6010_pal_cif);
211 }
212 }
213 } else {
214 solo_enc->width = solo_dev->video_hsize;
215 solo_enc->height = solo_dev->video_vsize << 1;
216 solo_enc->bw_weight <<= 2;
217 if (solo_dev->type == SOLO_DEV_6110) {
218 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
219 vop = vop_6110_ntsc_d1;
220 vop_len = sizeof(vop_6110_ntsc_d1);
221 } else {
222 vop = vop_6110_pal_d1;
223 vop_len = sizeof(vop_6110_pal_d1);
224 }
225 } else {
226 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
227 vop = vop_6010_ntsc_d1;
228 vop_len = sizeof(vop_6010_ntsc_d1);
229 } else {
230 vop = vop_6010_pal_d1;
231 vop_len = sizeof(vop_6010_pal_d1);
232 }
233 }
234 }
235
236 memcpy(solo_enc->vop, vop, vop_len);
237
238 /* Some fixups for 6010/M4V */
239 if (solo_dev->type == SOLO_DEV_6010) {
240 u16 fps = solo_dev->fps * 1000;
241 u16 interval = solo_enc->interval * 1000;
242
243 vop = solo_enc->vop;
244
245 /* Frame rate and interval */
246 vop[22] = fps >> 4;
247 vop[23] = ((fps << 4) & 0xf0) | 0x0c
248 | ((interval >> 13) & 0x3);
249 vop[24] = (interval >> 5) & 0xff;
250 vop[25] = ((interval << 3) & 0xf8) | 0x04;
251 }
252
253 solo_enc->vop_len = vop_len;
254
255 /* Now handle the jpeg header */
256 vop = solo_enc->jpeg_header;
257 vop[SOF0_START + 5] = 0xff & (solo_enc->height >> 8);
258 vop[SOF0_START + 6] = 0xff & solo_enc->height;
259 vop[SOF0_START + 7] = 0xff & (solo_enc->width >> 8);
260 vop[SOF0_START + 8] = 0xff & solo_enc->width;
261
262 memcpy(vop + DQT_START,
263 jpeg_dqt[solo_g_jpeg_qp(solo_dev, solo_enc->ch)], DQT_LEN);
264 }
265
266 static int solo_enc_on(struct solo_enc_dev *solo_enc)
267 {
268 u8 ch = solo_enc->ch;
269 struct solo_dev *solo_dev = solo_enc->solo_dev;
270 u8 interval;
271
272 solo_update_mode(solo_enc);
273
274 /* Make sure to do a bandwidth check */
275 if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
276 return -EBUSY;
277 solo_dev->enc_bw_remain -= solo_enc->bw_weight;
278
279 if (solo_enc->type == SOLO_ENC_TYPE_EXT)
280 solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
281
282 /* Disable all encoding for this channel */
283 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
284
285 /* Common for both std and ext encoding */
286 solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
287 solo_enc->interlaced ? 1 : 0);
288
289 if (solo_enc->interlaced)
290 interval = solo_enc->interval - 1;
291 else
292 interval = solo_enc->interval;
293
294 /* Standard encoding only */
295 solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
296 solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
297 solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
298
299 /* Extended encoding only */
300 solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
301 solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
302 solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
303
304 /* Enables the standard encoder */
305 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
306
307 return 0;
308 }
309
310 static void solo_enc_off(struct solo_enc_dev *solo_enc)
311 {
312 struct solo_dev *solo_dev = solo_enc->solo_dev;
313
314 solo_dev->enc_bw_remain += solo_enc->bw_weight;
315
316 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
317 solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
318 }
319
320 static int enc_get_mpeg_dma(struct solo_dev *solo_dev, dma_addr_t dma,
321 unsigned int off, unsigned int size)
322 {
323 int ret;
324
325 if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
326 return -EINVAL;
327
328 /* Single shot */
329 if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
330 return solo_p2m_dma_t(solo_dev, 0, dma,
331 SOLO_MP4E_EXT_ADDR(solo_dev) + off, size,
332 0, 0);
333 }
334
335 /* Buffer wrap */
336 ret = solo_p2m_dma_t(solo_dev, 0, dma,
337 SOLO_MP4E_EXT_ADDR(solo_dev) + off,
338 SOLO_MP4E_EXT_SIZE(solo_dev) - off, 0, 0);
339
340 if (!ret) {
341 ret = solo_p2m_dma_t(solo_dev, 0,
342 dma + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
343 SOLO_MP4E_EXT_ADDR(solo_dev),
344 size + off - SOLO_MP4E_EXT_SIZE(solo_dev), 0, 0);
345 }
346
347 return ret;
348 }
349
350 /* Build a descriptor queue out of an SG list and send it to the P2M for
351 * processing. */
352 static int solo_send_desc(struct solo_enc_dev *solo_enc, int skip,
353 struct vb2_dma_sg_desc *vbuf, int off, int size,
354 unsigned int base, unsigned int base_size)
355 {
356 struct solo_dev *solo_dev = solo_enc->solo_dev;
357 struct scatterlist *sg;
358 int i;
359 int ret;
360
361 if (WARN_ON_ONCE(size > FRAME_BUF_SIZE))
362 return -EINVAL;
363
364 solo_enc->desc_count = 1;
365
366 for_each_sg(vbuf->sglist, sg, vbuf->num_pages, i) {
367 struct solo_p2m_desc *desc;
368 dma_addr_t dma;
369 int len;
370 int left = base_size - off;
371
372 desc = &solo_enc->desc_items[solo_enc->desc_count++];
373 dma = sg_dma_address(sg);
374 len = sg_dma_len(sg);
375
376 /* We assume this is smaller than the scatter size */
377 BUG_ON(skip >= len);
378 if (skip) {
379 len -= skip;
380 dma += skip;
381 size -= skip;
382 skip = 0;
383 }
384
385 len = min(len, size);
386
387 if (len <= left) {
388 /* Single descriptor */
389 solo_p2m_fill_desc(desc, 0, dma, base + off,
390 len, 0, 0);
391 } else {
392 /* Buffer wrap */
393 /* XXX: Do these as separate DMA requests, to avoid
394 timeout errors triggered by awkwardly sized
395 descriptors. See
396 <https://github.com/bluecherrydvr/solo6x10/issues/8>
397 */
398 ret = solo_p2m_dma_t(solo_dev, 0, dma, base + off,
399 left, 0, 0);
400 if (ret)
401 return ret;
402
403 ret = solo_p2m_dma_t(solo_dev, 0, dma + left, base,
404 len - left, 0, 0);
405 if (ret)
406 return ret;
407
408 solo_enc->desc_count--;
409 }
410
411 size -= len;
412 if (size <= 0)
413 break;
414
415 off += len;
416 if (off >= base_size)
417 off -= base_size;
418
419 /* Because we may use two descriptors per loop */
420 if (solo_enc->desc_count >= (solo_enc->desc_nelts - 1)) {
421 ret = solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
422 solo_enc->desc_dma,
423 solo_enc->desc_count - 1);
424 if (ret)
425 return ret;
426 solo_enc->desc_count = 1;
427 }
428 }
429
430 if (solo_enc->desc_count <= 1)
431 return 0;
432
433 return solo_p2m_dma_desc(solo_dev, solo_enc->desc_items, solo_enc->desc_dma,
434 solo_enc->desc_count - 1);
435 }
436
437 static int solo_fill_jpeg(struct solo_enc_dev *solo_enc,
438 struct vb2_buffer *vb, struct vop_header *vh)
439 {
440 struct solo_dev *solo_dev = solo_enc->solo_dev;
441 struct vb2_dma_sg_desc *vbuf = vb2_dma_sg_plane_desc(vb, 0);
442 int frame_size;
443 int ret;
444
445 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
446
447 if (vb2_plane_size(vb, 0) < vh->jpeg_size + solo_enc->jpeg_len)
448 return -EIO;
449
450 sg_copy_from_buffer(vbuf->sglist, vbuf->num_pages,
451 solo_enc->jpeg_header,
452 solo_enc->jpeg_len);
453
454 frame_size = (vh->jpeg_size + solo_enc->jpeg_len + (DMA_ALIGN - 1))
455 & ~(DMA_ALIGN - 1);
456 vb2_set_plane_payload(vb, 0, vh->jpeg_size + solo_enc->jpeg_len);
457
458 dma_map_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
459 DMA_FROM_DEVICE);
460 ret = solo_send_desc(solo_enc, solo_enc->jpeg_len, vbuf, vh->jpeg_off,
461 frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
462 SOLO_JPEG_EXT_SIZE(solo_dev));
463 dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
464 DMA_FROM_DEVICE);
465 return ret;
466 }
467
468 static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
469 struct vb2_buffer *vb, struct vop_header *vh)
470 {
471 struct solo_dev *solo_dev = solo_enc->solo_dev;
472 struct vb2_dma_sg_desc *vbuf = vb2_dma_sg_plane_desc(vb, 0);
473 int frame_off, frame_size;
474 int skip = 0;
475 int ret;
476
477 if (vb2_plane_size(vb, 0) < vh->mpeg_size)
478 return -EIO;
479
480 /* If this is a key frame, add extra header */
481 if (!vh->vop_type) {
482 sg_copy_from_buffer(vbuf->sglist, vbuf->num_pages,
483 solo_enc->vop,
484 solo_enc->vop_len);
485
486 skip = solo_enc->vop_len;
487
488 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
489 vb2_set_plane_payload(vb, 0, vh->mpeg_size + solo_enc->vop_len);
490 } else {
491 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
492 vb2_set_plane_payload(vb, 0, vh->mpeg_size);
493 }
494
495 /* Now get the actual mpeg payload */
496 frame_off = (vh->mpeg_off + sizeof(*vh))
497 % SOLO_MP4E_EXT_SIZE(solo_dev);
498 frame_size = (vh->mpeg_size + skip + (DMA_ALIGN - 1))
499 & ~(DMA_ALIGN - 1);
500
501 dma_map_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
502 DMA_FROM_DEVICE);
503 ret = solo_send_desc(solo_enc, skip, vbuf, frame_off, frame_size,
504 SOLO_MP4E_EXT_ADDR(solo_dev),
505 SOLO_MP4E_EXT_SIZE(solo_dev));
506 dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
507 DMA_FROM_DEVICE);
508 return ret;
509 }
510
511 static int solo_enc_fillbuf(struct solo_enc_dev *solo_enc,
512 struct vb2_buffer *vb, struct solo_enc_buf *enc_buf)
513 {
514 struct vop_header *vh = enc_buf->vh;
515 int ret;
516
517 /* Check for motion flags */
518 if (solo_is_motion_on(solo_enc)) {
519 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_MOTION_ON;
520 if (enc_buf->motion)
521 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_MOTION_DETECTED;
522 }
523
524 if (solo_enc->fmt == V4L2_PIX_FMT_MPEG)
525 ret = solo_fill_mpeg(solo_enc, vb, vh);
526 else
527 ret = solo_fill_jpeg(solo_enc, vb, vh);
528
529 if (!ret) {
530 vb->v4l2_buf.sequence++;
531 vb->v4l2_buf.timestamp.tv_sec = vh->sec;
532 vb->v4l2_buf.timestamp.tv_usec = vh->usec;
533 }
534
535 vb2_buffer_done(vb, ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
536
537 return ret;
538 }
539
540 static void solo_enc_handle_one(struct solo_enc_dev *solo_enc,
541 struct solo_enc_buf *enc_buf)
542 {
543 struct solo_vb2_buf *vb;
544 unsigned long flags;
545
546 mutex_lock(&solo_enc->lock);
547 if (solo_enc->type != enc_buf->type)
548 goto unlock;
549
550 spin_lock_irqsave(&solo_enc->av_lock, flags);
551 if (list_empty(&solo_enc->vidq_active)) {
552 spin_unlock_irqrestore(&solo_enc->av_lock, flags);
553 goto unlock;
554 }
555 vb = list_first_entry(&solo_enc->vidq_active, struct solo_vb2_buf, list);
556 list_del(&vb->list);
557 spin_unlock_irqrestore(&solo_enc->av_lock, flags);
558
559 solo_enc_fillbuf(solo_enc, &vb->vb, enc_buf);
560 unlock:
561 mutex_unlock(&solo_enc->lock);
562 }
563
564 void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
565 {
566 wake_up_interruptible_all(&solo_dev->ring_thread_wait);
567 }
568
569 static void solo_handle_ring(struct solo_dev *solo_dev)
570 {
571 for (;;) {
572 struct solo_enc_dev *solo_enc;
573 struct solo_enc_buf enc_buf;
574 u32 mpeg_current, off;
575 u8 ch;
576 u8 cur_q;
577
578 /* Check if the hardware has any new ones in the queue */
579 cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
580 if (cur_q == solo_dev->enc_idx)
581 break;
582
583 mpeg_current = solo_reg_read(solo_dev,
584 SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
585 solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
586
587 ch = (mpeg_current >> 24) & 0x1f;
588 off = mpeg_current & 0x00ffffff;
589
590 if (ch >= SOLO_MAX_CHANNELS) {
591 ch -= SOLO_MAX_CHANNELS;
592 enc_buf.type = SOLO_ENC_TYPE_EXT;
593 } else
594 enc_buf.type = SOLO_ENC_TYPE_STD;
595
596 solo_enc = solo_dev->v4l2_enc[ch];
597 if (solo_enc == NULL) {
598 dev_err(&solo_dev->pdev->dev,
599 "Got spurious packet for channel %d\n", ch);
600 continue;
601 }
602
603 /* FAIL... */
604 if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
605 sizeof(struct vop_header)))
606 continue;
607
608 enc_buf.vh = (struct vop_header *)solo_dev->vh_buf;
609 enc_buf.vh->mpeg_off -= SOLO_MP4E_EXT_ADDR(solo_dev);
610 enc_buf.vh->jpeg_off -= SOLO_JPEG_EXT_ADDR(solo_dev);
611
612 /* Sanity check */
613 if (enc_buf.vh->mpeg_off != off)
614 continue;
615
616 if (solo_motion_detected(solo_enc))
617 enc_buf.motion = 1;
618 else
619 enc_buf.motion = 0;
620
621 solo_enc_handle_one(solo_enc, &enc_buf);
622 }
623 }
624
625 static int solo_ring_thread(void *data)
626 {
627 struct solo_dev *solo_dev = data;
628 DECLARE_WAITQUEUE(wait, current);
629
630 set_freezable();
631 add_wait_queue(&solo_dev->ring_thread_wait, &wait);
632
633 for (;;) {
634 long timeout = schedule_timeout_interruptible(HZ);
635 if (timeout == -ERESTARTSYS || kthread_should_stop())
636 break;
637 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
638 solo_handle_ring(solo_dev);
639 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
640 try_to_freeze();
641 }
642
643 remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
644
645 return 0;
646 }
647
648 static int solo_enc_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
649 unsigned int *num_buffers, unsigned int *num_planes,
650 unsigned int sizes[], void *alloc_ctxs[])
651 {
652 sizes[0] = FRAME_BUF_SIZE;
653 *num_planes = 1;
654
655 if (*num_buffers < MIN_VID_BUFFERS)
656 *num_buffers = MIN_VID_BUFFERS;
657
658 return 0;
659 }
660
661 static void solo_enc_buf_queue(struct vb2_buffer *vb)
662 {
663 struct vb2_queue *vq = vb->vb2_queue;
664 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
665 struct solo_vb2_buf *solo_vb =
666 container_of(vb, struct solo_vb2_buf, vb);
667
668 spin_lock(&solo_enc->av_lock);
669 list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
670 spin_unlock(&solo_enc->av_lock);
671 }
672
673 static int solo_ring_start(struct solo_dev *solo_dev)
674 {
675 if (atomic_inc_return(&solo_dev->enc_users) > 1)
676 return 0;
677
678 solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
679 SOLO6X10_NAME "_ring");
680 if (IS_ERR(solo_dev->ring_thread)) {
681 int err = PTR_ERR(solo_dev->ring_thread);
682 solo_dev->ring_thread = NULL;
683 return err;
684 }
685
686 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
687
688 return 0;
689 }
690
691 static void solo_ring_stop(struct solo_dev *solo_dev)
692 {
693 if (atomic_dec_return(&solo_dev->enc_users) > 0)
694 return;
695
696 if (solo_dev->ring_thread) {
697 kthread_stop(solo_dev->ring_thread);
698 solo_dev->ring_thread = NULL;
699 }
700
701 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
702 }
703
704 static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
705 {
706 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
707 int ret;
708
709 ret = solo_enc_on(solo_enc);
710 if (ret)
711 return ret;
712 return solo_ring_start(solo_enc->solo_dev);
713 }
714
715 static int solo_enc_stop_streaming(struct vb2_queue *q)
716 {
717 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
718
719 solo_enc_off(solo_enc);
720 INIT_LIST_HEAD(&solo_enc->vidq_active);
721 solo_ring_stop(solo_enc->solo_dev);
722 return 0;
723 }
724
725 static struct vb2_ops solo_enc_video_qops = {
726 .queue_setup = solo_enc_queue_setup,
727 .buf_queue = solo_enc_buf_queue,
728 .start_streaming = solo_enc_start_streaming,
729 .stop_streaming = solo_enc_stop_streaming,
730 .wait_prepare = vb2_ops_wait_prepare,
731 .wait_finish = vb2_ops_wait_finish,
732 };
733
734 static int solo_enc_querycap(struct file *file, void *priv,
735 struct v4l2_capability *cap)
736 {
737 struct solo_enc_dev *solo_enc = video_drvdata(file);
738 struct solo_dev *solo_dev = solo_enc->solo_dev;
739
740 strcpy(cap->driver, SOLO6X10_NAME);
741 snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
742 solo_enc->ch);
743 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
744 pci_name(solo_dev->pdev));
745 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
746 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
747 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
748 return 0;
749 }
750
751 static int solo_enc_enum_input(struct file *file, void *priv,
752 struct v4l2_input *input)
753 {
754 struct solo_enc_dev *solo_enc = video_drvdata(file);
755 struct solo_dev *solo_dev = solo_enc->solo_dev;
756
757 if (input->index)
758 return -EINVAL;
759
760 snprintf(input->name, sizeof(input->name), "Encoder %d",
761 solo_enc->ch + 1);
762 input->type = V4L2_INPUT_TYPE_CAMERA;
763
764 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
765 input->std = V4L2_STD_NTSC_M;
766 else
767 input->std = V4L2_STD_PAL_B;
768
769 if (!tw28_get_video_status(solo_dev, solo_enc->ch))
770 input->status = V4L2_IN_ST_NO_SIGNAL;
771
772 return 0;
773 }
774
775 static int solo_enc_set_input(struct file *file, void *priv,
776 unsigned int index)
777 {
778 if (index)
779 return -EINVAL;
780
781 return 0;
782 }
783
784 static int solo_enc_get_input(struct file *file, void *priv,
785 unsigned int *index)
786 {
787 *index = 0;
788
789 return 0;
790 }
791
792 static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
793 struct v4l2_fmtdesc *f)
794 {
795 switch (f->index) {
796 case 0:
797 f->pixelformat = V4L2_PIX_FMT_MPEG;
798 strcpy(f->description, "MPEG-4 AVC");
799 break;
800 case 1:
801 f->pixelformat = V4L2_PIX_FMT_MJPEG;
802 strcpy(f->description, "MJPEG");
803 break;
804 default:
805 return -EINVAL;
806 }
807
808 f->flags = V4L2_FMT_FLAG_COMPRESSED;
809
810 return 0;
811 }
812
813 static int solo_enc_try_fmt_cap(struct file *file, void *priv,
814 struct v4l2_format *f)
815 {
816 struct solo_enc_dev *solo_enc = video_drvdata(file);
817 struct solo_dev *solo_dev = solo_enc->solo_dev;
818 struct v4l2_pix_format *pix = &f->fmt.pix;
819
820 if (pix->pixelformat != V4L2_PIX_FMT_MPEG &&
821 pix->pixelformat != V4L2_PIX_FMT_MJPEG)
822 return -EINVAL;
823
824 if (pix->width < solo_dev->video_hsize ||
825 pix->height < solo_dev->video_vsize << 1) {
826 /* Default to CIF 1/2 size */
827 pix->width = solo_dev->video_hsize >> 1;
828 pix->height = solo_dev->video_vsize;
829 } else {
830 /* Full frame */
831 pix->width = solo_dev->video_hsize;
832 pix->height = solo_dev->video_vsize << 1;
833 }
834
835 switch (pix->field) {
836 case V4L2_FIELD_NONE:
837 case V4L2_FIELD_INTERLACED:
838 break;
839 case V4L2_FIELD_ANY:
840 default:
841 pix->field = V4L2_FIELD_INTERLACED;
842 break;
843 }
844
845 /* Just set these */
846 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
847 pix->sizeimage = FRAME_BUF_SIZE;
848 pix->bytesperline = 0;
849 pix->priv = 0;
850
851 return 0;
852 }
853
854 static int solo_enc_set_fmt_cap(struct file *file, void *priv,
855 struct v4l2_format *f)
856 {
857 struct solo_enc_dev *solo_enc = video_drvdata(file);
858 struct solo_dev *solo_dev = solo_enc->solo_dev;
859 struct v4l2_pix_format *pix = &f->fmt.pix;
860 int ret;
861
862 if (vb2_is_busy(&solo_enc->vidq))
863 return -EBUSY;
864
865 ret = solo_enc_try_fmt_cap(file, priv, f);
866 if (ret)
867 return ret;
868
869 if (pix->width == solo_dev->video_hsize)
870 solo_enc->mode = SOLO_ENC_MODE_D1;
871 else
872 solo_enc->mode = SOLO_ENC_MODE_CIF;
873
874 /* This does not change the encoder at all */
875 solo_enc->fmt = pix->pixelformat;
876
877 if (pix->priv)
878 solo_enc->type = SOLO_ENC_TYPE_EXT;
879 return 0;
880 }
881
882 static int solo_enc_get_fmt_cap(struct file *file, void *priv,
883 struct v4l2_format *f)
884 {
885 struct solo_enc_dev *solo_enc = video_drvdata(file);
886 struct v4l2_pix_format *pix = &f->fmt.pix;
887
888 pix->width = solo_enc->width;
889 pix->height = solo_enc->height;
890 pix->pixelformat = solo_enc->fmt;
891 pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
892 V4L2_FIELD_NONE;
893 pix->sizeimage = FRAME_BUF_SIZE;
894 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
895 pix->priv = 0;
896
897 return 0;
898 }
899
900 static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id i)
901 {
902 return 0;
903 }
904
905 static int solo_enum_framesizes(struct file *file, void *priv,
906 struct v4l2_frmsizeenum *fsize)
907 {
908 struct solo_enc_dev *solo_enc = video_drvdata(file);
909 struct solo_dev *solo_dev = solo_enc->solo_dev;
910
911 if (fsize->pixel_format != V4L2_PIX_FMT_MPEG &&
912 fsize->pixel_format != V4L2_PIX_FMT_MJPEG)
913 return -EINVAL;
914
915 switch (fsize->index) {
916 case 0:
917 fsize->discrete.width = solo_dev->video_hsize >> 1;
918 fsize->discrete.height = solo_dev->video_vsize;
919 break;
920 case 1:
921 fsize->discrete.width = solo_dev->video_hsize;
922 fsize->discrete.height = solo_dev->video_vsize << 1;
923 break;
924 default:
925 return -EINVAL;
926 }
927
928 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
929
930 return 0;
931 }
932
933 static int solo_enum_frameintervals(struct file *file, void *priv,
934 struct v4l2_frmivalenum *fintv)
935 {
936 struct solo_enc_dev *solo_enc = video_drvdata(file);
937 struct solo_dev *solo_dev = solo_enc->solo_dev;
938
939 if (fintv->pixel_format != V4L2_PIX_FMT_MPEG &&
940 fintv->pixel_format != V4L2_PIX_FMT_MJPEG)
941 return -EINVAL;
942 if (fintv->index)
943 return -EINVAL;
944 if ((fintv->width != solo_dev->video_hsize >> 1 ||
945 fintv->height != solo_dev->video_vsize) &&
946 (fintv->width != solo_dev->video_hsize ||
947 fintv->height != solo_dev->video_vsize << 1))
948 return -EINVAL;
949
950 fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
951
952 fintv->stepwise.min.denominator = solo_dev->fps;
953 fintv->stepwise.min.numerator = 15;
954
955 fintv->stepwise.max.denominator = solo_dev->fps;
956 fintv->stepwise.max.numerator = 1;
957
958 fintv->stepwise.step.numerator = 1;
959 fintv->stepwise.step.denominator = 1;
960
961 return 0;
962 }
963
964 static int solo_g_parm(struct file *file, void *priv,
965 struct v4l2_streamparm *sp)
966 {
967 struct solo_enc_dev *solo_enc = video_drvdata(file);
968 struct solo_dev *solo_dev = solo_enc->solo_dev;
969 struct v4l2_captureparm *cp = &sp->parm.capture;
970
971 cp->capability = V4L2_CAP_TIMEPERFRAME;
972 cp->timeperframe.numerator = solo_enc->interval;
973 cp->timeperframe.denominator = solo_dev->fps;
974 cp->capturemode = 0;
975 /* XXX: Shouldn't we be able to get/set this from videobuf? */
976 cp->readbuffers = 2;
977
978 return 0;
979 }
980
981 static int solo_s_parm(struct file *file, void *priv,
982 struct v4l2_streamparm *sp)
983 {
984 struct solo_enc_dev *solo_enc = video_drvdata(file);
985 struct solo_dev *solo_dev = solo_enc->solo_dev;
986 struct v4l2_captureparm *cp = &sp->parm.capture;
987
988 if (vb2_is_streaming(&solo_enc->vidq))
989 return -EBUSY;
990
991 if ((cp->timeperframe.numerator == 0) ||
992 (cp->timeperframe.denominator == 0)) {
993 /* reset framerate */
994 cp->timeperframe.numerator = 1;
995 cp->timeperframe.denominator = solo_dev->fps;
996 }
997
998 if (cp->timeperframe.denominator != solo_dev->fps)
999 cp->timeperframe.denominator = solo_dev->fps;
1000
1001 if (cp->timeperframe.numerator > 15)
1002 cp->timeperframe.numerator = 15;
1003
1004 solo_enc->interval = cp->timeperframe.numerator;
1005
1006 cp->capability = V4L2_CAP_TIMEPERFRAME;
1007 cp->readbuffers = 2;
1008
1009 solo_update_mode(solo_enc);
1010 return 0;
1011 }
1012
1013 static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
1014 {
1015 struct solo_enc_dev *solo_enc =
1016 container_of(ctrl->handler, struct solo_enc_dev, hdl);
1017 struct solo_dev *solo_dev = solo_enc->solo_dev;
1018 int err;
1019
1020 switch (ctrl->id) {
1021 case V4L2_CID_BRIGHTNESS:
1022 case V4L2_CID_CONTRAST:
1023 case V4L2_CID_SATURATION:
1024 case V4L2_CID_HUE:
1025 case V4L2_CID_SHARPNESS:
1026 return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
1027 ctrl->val);
1028 case V4L2_CID_MPEG_VIDEO_ENCODING:
1029 return 0;
1030 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1031 solo_enc->gop = ctrl->val;
1032 return 0;
1033 case V4L2_CID_MOTION_THRESHOLD: {
1034 u16 block = (ctrl->val >> 16) & 0xffff;
1035 u16 value = ctrl->val & 0xffff;
1036
1037 /* Motion thresholds are in a table of 64x64 samples, with
1038 * each sample representing 16x16 pixels of the source. In
1039 * effect, 44x30 samples are used for NTSC, and 44x36 for PAL.
1040 * The 5th sample on the 10th row is (10*64)+5 = 645.
1041 *
1042 * Block is 0 to set the threshold globally, or any positive
1043 * number under 2049 to set block-1 individually. */
1044 /* Currently we limit support to block 0 only. A later patch
1045 * will add a new ioctl to set all other blocks. */
1046 if (block == 0) {
1047 solo_enc->motion_thresh = value;
1048 return solo_set_motion_threshold(solo_dev,
1049 solo_enc->ch, value);
1050 }
1051 return solo_set_motion_block(solo_dev, solo_enc->ch,
1052 value, block - 1);
1053 }
1054 case V4L2_CID_MOTION_ENABLE:
1055 solo_motion_toggle(solo_enc, ctrl->val);
1056 return 0;
1057 case V4L2_CID_OSD_TEXT:
1058 strcpy(solo_enc->osd_text, ctrl->string);
1059 err = solo_osd_print(solo_enc);
1060 return err;
1061 default:
1062 return -EINVAL;
1063 }
1064
1065 return 0;
1066 }
1067
1068 static const struct v4l2_file_operations solo_enc_fops = {
1069 .owner = THIS_MODULE,
1070 .open = v4l2_fh_open,
1071 .release = vb2_fop_release,
1072 .read = vb2_fop_read,
1073 .poll = vb2_fop_poll,
1074 .mmap = vb2_fop_mmap,
1075 .unlocked_ioctl = video_ioctl2,
1076 };
1077
1078 static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
1079 .vidioc_querycap = solo_enc_querycap,
1080 .vidioc_s_std = solo_enc_s_std,
1081 /* Input callbacks */
1082 .vidioc_enum_input = solo_enc_enum_input,
1083 .vidioc_s_input = solo_enc_set_input,
1084 .vidioc_g_input = solo_enc_get_input,
1085 /* Video capture format callbacks */
1086 .vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
1087 .vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
1088 .vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
1089 .vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
1090 /* Streaming I/O */
1091 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1092 .vidioc_querybuf = vb2_ioctl_querybuf,
1093 .vidioc_qbuf = vb2_ioctl_qbuf,
1094 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1095 .vidioc_streamon = vb2_ioctl_streamon,
1096 .vidioc_streamoff = vb2_ioctl_streamoff,
1097 /* Frame size and interval */
1098 .vidioc_enum_framesizes = solo_enum_framesizes,
1099 .vidioc_enum_frameintervals = solo_enum_frameintervals,
1100 /* Video capture parameters */
1101 .vidioc_s_parm = solo_s_parm,
1102 .vidioc_g_parm = solo_g_parm,
1103 /* Logging and events */
1104 .vidioc_log_status = v4l2_ctrl_log_status,
1105 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1106 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1107 };
1108
1109 static const struct video_device solo_enc_template = {
1110 .name = SOLO6X10_NAME,
1111 .fops = &solo_enc_fops,
1112 .ioctl_ops = &solo_enc_ioctl_ops,
1113 .minor = -1,
1114 .release = video_device_release,
1115
1116 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL_B,
1117 .current_norm = V4L2_STD_NTSC_M,
1118 };
1119
1120 static const struct v4l2_ctrl_ops solo_ctrl_ops = {
1121 .s_ctrl = solo_s_ctrl,
1122 };
1123
1124 static const struct v4l2_ctrl_config solo_motion_threshold_ctrl = {
1125 .ops = &solo_ctrl_ops,
1126 .id = V4L2_CID_MOTION_THRESHOLD,
1127 .name = "Motion Detection Threshold",
1128 .type = V4L2_CTRL_TYPE_INTEGER,
1129 .max = 0xffff,
1130 .def = SOLO_DEF_MOT_THRESH,
1131 .step = 1,
1132 .flags = V4L2_CTRL_FLAG_SLIDER,
1133 };
1134
1135 static const struct v4l2_ctrl_config solo_motion_enable_ctrl = {
1136 .ops = &solo_ctrl_ops,
1137 .id = V4L2_CID_MOTION_ENABLE,
1138 .name = "Motion Detection Enable",
1139 .type = V4L2_CTRL_TYPE_BOOLEAN,
1140 .max = 1,
1141 .step = 1,
1142 };
1143
1144 static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
1145 .ops = &solo_ctrl_ops,
1146 .id = V4L2_CID_OSD_TEXT,
1147 .name = "OSD Text",
1148 .type = V4L2_CTRL_TYPE_STRING,
1149 .max = OSD_TEXT_MAX,
1150 .step = 1,
1151 };
1152
1153 static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1154 u8 ch, unsigned nr)
1155 {
1156 struct solo_enc_dev *solo_enc;
1157 struct v4l2_ctrl_handler *hdl;
1158 int ret;
1159
1160 solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
1161 if (!solo_enc)
1162 return ERR_PTR(-ENOMEM);
1163
1164 hdl = &solo_enc->hdl;
1165 v4l2_ctrl_handler_init(hdl, 10);
1166 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1167 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1168 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1169 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1170 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1171 V4L2_CID_SATURATION, 0, 255, 1, 128);
1172 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1173 V4L2_CID_HUE, 0, 255, 1, 128);
1174 if (tw28_has_sharpness(solo_dev, ch))
1175 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1176 V4L2_CID_SHARPNESS, 0, 15, 1, 0);
1177 v4l2_ctrl_new_std_menu(hdl, &solo_ctrl_ops,
1178 V4L2_CID_MPEG_VIDEO_ENCODING,
1179 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC, 3,
1180 V4L2_MPEG_VIDEO_ENCODING_MPEG_4_AVC);
1181 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1182 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
1183 v4l2_ctrl_new_custom(hdl, &solo_motion_threshold_ctrl, NULL);
1184 v4l2_ctrl_new_custom(hdl, &solo_motion_enable_ctrl, NULL);
1185 v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
1186 if (hdl->error) {
1187 ret = hdl->error;
1188 goto hdl_free;
1189 }
1190
1191 solo_enc->solo_dev = solo_dev;
1192 solo_enc->ch = ch;
1193 mutex_init(&solo_enc->lock);
1194 spin_lock_init(&solo_enc->av_lock);
1195 INIT_LIST_HEAD(&solo_enc->vidq_active);
1196 solo_enc->fmt = V4L2_PIX_FMT_MPEG;
1197 solo_enc->type = SOLO_ENC_TYPE_STD;
1198
1199 solo_enc->qp = SOLO_DEFAULT_QP;
1200 solo_enc->gop = solo_dev->fps;
1201 solo_enc->interval = 1;
1202 solo_enc->mode = SOLO_ENC_MODE_CIF;
1203 solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
1204
1205 solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1206 solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1207 solo_enc->vidq.ops = &solo_enc_video_qops;
1208 solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
1209 solo_enc->vidq.drv_priv = solo_enc;
1210 solo_enc->vidq.gfp_flags = __GFP_DMA32;
1211 solo_enc->vidq.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1212 solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
1213 solo_enc->vidq.lock = &solo_enc->lock;
1214 ret = vb2_queue_init(&solo_enc->vidq);
1215 if (ret)
1216 goto hdl_free;
1217 spin_lock(&solo_enc->av_lock);
1218 solo_update_mode(solo_enc);
1219 spin_unlock(&solo_enc->av_lock);
1220
1221 spin_lock_init(&solo_enc->motion_lock);
1222
1223 /* Initialize this per encoder */
1224 solo_enc->jpeg_len = sizeof(jpeg_header);
1225 memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
1226
1227 solo_enc->desc_nelts = 32;
1228 solo_enc->desc_items = pci_alloc_consistent(solo_dev->pdev,
1229 sizeof(struct solo_p2m_desc) *
1230 solo_enc->desc_nelts, &solo_enc->desc_dma);
1231 ret = -ENOMEM;
1232 if (solo_enc->desc_items == NULL)
1233 goto hdl_free;
1234
1235 solo_enc->vfd = video_device_alloc();
1236 if (!solo_enc->vfd)
1237 goto pci_free;
1238
1239 *solo_enc->vfd = solo_enc_template;
1240 solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
1241 solo_enc->vfd->ctrl_handler = hdl;
1242 solo_enc->vfd->queue = &solo_enc->vidq;
1243 solo_enc->vfd->lock = &solo_enc->lock;
1244 set_bit(V4L2_FL_USE_FH_PRIO, &solo_enc->vfd->flags);
1245 video_set_drvdata(solo_enc->vfd, solo_enc);
1246 ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER, nr);
1247 if (ret < 0)
1248 goto vdev_release;
1249
1250 snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
1251 "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
1252 solo_enc->vfd->num);
1253
1254 return solo_enc;
1255
1256 vdev_release:
1257 video_device_release(solo_enc->vfd);
1258 pci_free:
1259 pci_free_consistent(solo_enc->solo_dev->pdev,
1260 sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
1261 solo_enc->desc_items, solo_enc->desc_dma);
1262 hdl_free:
1263 v4l2_ctrl_handler_free(hdl);
1264 kfree(solo_enc);
1265 return ERR_PTR(ret);
1266 }
1267
1268 static void solo_enc_free(struct solo_enc_dev *solo_enc)
1269 {
1270 if (solo_enc == NULL)
1271 return;
1272
1273 video_unregister_device(solo_enc->vfd);
1274 v4l2_ctrl_handler_free(&solo_enc->hdl);
1275 kfree(solo_enc);
1276 }
1277
1278 int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
1279 {
1280 int i;
1281
1282 atomic_set(&solo_dev->enc_users, 0);
1283 init_waitqueue_head(&solo_dev->ring_thread_wait);
1284
1285 solo_dev->vh_size = sizeof(struct vop_header);
1286 solo_dev->vh_buf = pci_alloc_consistent(solo_dev->pdev,
1287 solo_dev->vh_size,
1288 &solo_dev->vh_dma);
1289 if (solo_dev->vh_buf == NULL)
1290 return -ENOMEM;
1291
1292 for (i = 0; i < solo_dev->nr_chans; i++) {
1293 solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
1294 if (IS_ERR(solo_dev->v4l2_enc[i]))
1295 break;
1296 }
1297
1298 if (i != solo_dev->nr_chans) {
1299 int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
1300 while (i--)
1301 solo_enc_free(solo_dev->v4l2_enc[i]);
1302 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1303 solo_dev->vh_buf, solo_dev->vh_dma);
1304 solo_dev->vh_buf = NULL;
1305 return ret;
1306 }
1307
1308 if (solo_dev->type == SOLO_DEV_6010)
1309 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
1310 else
1311 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
1312
1313 dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
1314 solo_dev->v4l2_enc[0]->vfd->num,
1315 solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
1316
1317 return 0;
1318 }
1319
1320 void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
1321 {
1322 int i;
1323
1324 for (i = 0; i < solo_dev->nr_chans; i++)
1325 solo_enc_free(solo_dev->v4l2_enc[i]);
1326
1327 if (solo_dev->vh_buf)
1328 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1329 solo_dev->vh_buf, solo_dev->vh_dma);
1330 }
This page took 0.143449 seconds and 5 git commands to generate.