2 * Copyright (C) 2010-2013 Bluecherry, LLC <http://www.bluecherrydvr.com>
5 * Ben Collins <bcollins@ubuntu.com>
8 * John Brooks <john.brooks@bluecherry.net>
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.
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.
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.
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/kthread.h>
28 #include <linux/freezer.h>
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>
36 #include "solo6x10-tw28.h"
37 #include "solo6x10-jpeg.h"
39 #define MIN_VID_BUFFERS 2
40 #define FRAME_BUF_SIZE (196 * 1024)
42 #define DMA_ALIGN 4096
45 static unsigned char vop_6010_ntsc_d1[] = {
46 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
47 0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
48 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
49 0x1f, 0x4c, 0x58, 0x10, 0xf0, 0x71, 0x18, 0x3f,
52 static unsigned char vop_6010_ntsc_cif[] = {
53 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
54 0x02, 0x48, 0x1d, 0xc0, 0x00, 0x40, 0x00, 0x40,
55 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
56 0x1f, 0x4c, 0x2c, 0x10, 0x78, 0x51, 0x18, 0x3f,
59 static unsigned char vop_6010_pal_d1[] = {
60 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
61 0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
62 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
63 0x1f, 0x4c, 0x58, 0x11, 0x20, 0x71, 0x18, 0x3f,
66 static unsigned char vop_6010_pal_cif[] = {
67 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x20,
68 0x02, 0x48, 0x15, 0xc0, 0x00, 0x40, 0x00, 0x40,
69 0x00, 0x40, 0x00, 0x80, 0x00, 0x97, 0x53, 0x04,
70 0x1f, 0x4c, 0x2c, 0x10, 0x90, 0x51, 0x18, 0x3f,
74 static unsigned char vop_6110_ntsc_d1[] = {
75 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
76 0x9a, 0x74, 0x05, 0x81, 0xec, 0x80, 0x00, 0x00,
77 0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
80 static unsigned char vop_6110_ntsc_cif[] = {
81 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
82 0x9a, 0x74, 0x0b, 0x0f, 0xc8, 0x00, 0x00, 0x00,
83 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
86 static unsigned char vop_6110_pal_d1[] = {
87 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
88 0x9a, 0x74, 0x05, 0x80, 0x93, 0x20, 0x00, 0x00,
89 0x00, 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00,
92 static unsigned char vop_6110_pal_cif[] = {
93 0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e,
94 0x9a, 0x74, 0x0b, 0x04, 0xb2, 0x00, 0x00, 0x00,
95 0x01, 0x68, 0xce, 0x32, 0x28, 0x00, 0x00, 0x00,
98 typedef __le32 vop_header[16];
100 struct solo_enc_buf {
101 enum solo_enc_types type;
102 const vop_header *vh;
106 static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
108 struct solo_dev *solo_dev = solo_enc->solo_dev;
110 return (solo_dev->motion_mask >> solo_enc->ch) & 1;
113 static int solo_motion_detected(struct solo_enc_dev *solo_enc)
115 struct solo_dev *solo_dev = solo_enc->solo_dev;
117 u32 ch_mask = 1 << solo_enc->ch;
120 spin_lock_irqsave(&solo_enc->motion_lock, flags);
121 if (solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS) & ch_mask) {
122 solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, ch_mask);
125 spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
130 static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
132 struct solo_dev *solo_dev = solo_enc->solo_dev;
133 u32 mask = 1 << solo_enc->ch;
136 spin_lock_irqsave(&solo_enc->motion_lock, flags);
139 solo_dev->motion_mask |= mask;
141 solo_dev->motion_mask &= ~mask;
143 solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, mask);
145 solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
146 SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
147 (SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
149 spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
152 void solo_update_mode(struct solo_enc_dev *solo_enc)
154 struct solo_dev *solo_dev = solo_enc->solo_dev;
158 solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
159 solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
161 if (solo_enc->mode == SOLO_ENC_MODE_CIF) {
162 solo_enc->width = solo_dev->video_hsize >> 1;
163 solo_enc->height = solo_dev->video_vsize;
164 if (solo_dev->type == SOLO_DEV_6110) {
165 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
166 vop = vop_6110_ntsc_cif;
167 vop_len = sizeof(vop_6110_ntsc_cif);
169 vop = vop_6110_pal_cif;
170 vop_len = sizeof(vop_6110_pal_cif);
173 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
174 vop = vop_6010_ntsc_cif;
175 vop_len = sizeof(vop_6010_ntsc_cif);
177 vop = vop_6010_pal_cif;
178 vop_len = sizeof(vop_6010_pal_cif);
182 solo_enc->width = solo_dev->video_hsize;
183 solo_enc->height = solo_dev->video_vsize << 1;
184 solo_enc->bw_weight <<= 2;
185 if (solo_dev->type == SOLO_DEV_6110) {
186 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
187 vop = vop_6110_ntsc_d1;
188 vop_len = sizeof(vop_6110_ntsc_d1);
190 vop = vop_6110_pal_d1;
191 vop_len = sizeof(vop_6110_pal_d1);
194 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
195 vop = vop_6010_ntsc_d1;
196 vop_len = sizeof(vop_6010_ntsc_d1);
198 vop = vop_6010_pal_d1;
199 vop_len = sizeof(vop_6010_pal_d1);
204 memcpy(solo_enc->vop, vop, vop_len);
206 /* Some fixups for 6010/M4V */
207 if (solo_dev->type == SOLO_DEV_6010) {
208 u16 fps = solo_dev->fps * 1000;
209 u16 interval = solo_enc->interval * 1000;
213 /* Frame rate and interval */
215 vop[23] = ((fps << 4) & 0xf0) | 0x0c
216 | ((interval >> 13) & 0x3);
217 vop[24] = (interval >> 5) & 0xff;
218 vop[25] = ((interval << 3) & 0xf8) | 0x04;
221 solo_enc->vop_len = vop_len;
223 /* Now handle the jpeg header */
224 vop = solo_enc->jpeg_header;
225 vop[SOF0_START + 5] = 0xff & (solo_enc->height >> 8);
226 vop[SOF0_START + 6] = 0xff & solo_enc->height;
227 vop[SOF0_START + 7] = 0xff & (solo_enc->width >> 8);
228 vop[SOF0_START + 8] = 0xff & solo_enc->width;
230 memcpy(vop + DQT_START,
231 jpeg_dqt[solo_g_jpeg_qp(solo_dev, solo_enc->ch)], DQT_LEN);
234 static int solo_enc_on(struct solo_enc_dev *solo_enc)
236 u8 ch = solo_enc->ch;
237 struct solo_dev *solo_dev = solo_enc->solo_dev;
240 solo_update_mode(solo_enc);
242 /* Make sure to do a bandwidth check */
243 if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
245 solo_enc->sequence = 0;
246 solo_enc->motion_last_state = false;
247 solo_enc->frames_since_last_motion = 0;
248 solo_dev->enc_bw_remain -= solo_enc->bw_weight;
250 if (solo_enc->type == SOLO_ENC_TYPE_EXT)
251 solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
253 /* Disable all encoding for this channel */
254 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
256 /* Common for both std and ext encoding */
257 solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
258 solo_enc->interlaced ? 1 : 0);
260 if (solo_enc->interlaced)
261 interval = solo_enc->interval - 1;
263 interval = solo_enc->interval;
265 /* Standard encoding only */
266 solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
267 solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
268 solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
270 /* Extended encoding only */
271 solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
272 solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
273 solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
275 /* Enables the standard encoder */
276 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
281 static void solo_enc_off(struct solo_enc_dev *solo_enc)
283 struct solo_dev *solo_dev = solo_enc->solo_dev;
285 solo_dev->enc_bw_remain += solo_enc->bw_weight;
287 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
288 solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
291 static int enc_get_mpeg_dma(struct solo_dev *solo_dev, dma_addr_t dma,
292 unsigned int off, unsigned int size)
296 if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
300 if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
301 return solo_p2m_dma_t(solo_dev, 0, dma,
302 SOLO_MP4E_EXT_ADDR(solo_dev) + off, size,
307 ret = solo_p2m_dma_t(solo_dev, 0, dma,
308 SOLO_MP4E_EXT_ADDR(solo_dev) + off,
309 SOLO_MP4E_EXT_SIZE(solo_dev) - off, 0, 0);
312 ret = solo_p2m_dma_t(solo_dev, 0,
313 dma + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
314 SOLO_MP4E_EXT_ADDR(solo_dev),
315 size + off - SOLO_MP4E_EXT_SIZE(solo_dev), 0, 0);
321 /* Build a descriptor queue out of an SG list and send it to the P2M for
323 static int solo_send_desc(struct solo_enc_dev *solo_enc, int skip,
324 struct sg_table *vbuf, int off, int size,
325 unsigned int base, unsigned int base_size)
327 struct solo_dev *solo_dev = solo_enc->solo_dev;
328 struct scatterlist *sg;
332 if (WARN_ON_ONCE(size > FRAME_BUF_SIZE))
335 solo_enc->desc_count = 1;
337 for_each_sg(vbuf->sgl, sg, vbuf->nents, i) {
338 struct solo_p2m_desc *desc;
341 int left = base_size - off;
343 desc = &solo_enc->desc_items[solo_enc->desc_count++];
344 dma = sg_dma_address(sg);
345 len = sg_dma_len(sg);
347 /* We assume this is smaller than the scatter size */
356 len = min(len, size);
359 /* Single descriptor */
360 solo_p2m_fill_desc(desc, 0, dma, base + off,
364 /* XXX: Do these as separate DMA requests, to avoid
365 timeout errors triggered by awkwardly sized
367 <https://github.com/bluecherrydvr/solo6x10/issues/8>
369 ret = solo_p2m_dma_t(solo_dev, 0, dma, base + off,
374 ret = solo_p2m_dma_t(solo_dev, 0, dma + left, base,
379 solo_enc->desc_count--;
387 if (off >= base_size)
390 /* Because we may use two descriptors per loop */
391 if (solo_enc->desc_count >= (solo_enc->desc_nelts - 1)) {
392 ret = solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
394 solo_enc->desc_count - 1);
397 solo_enc->desc_count = 1;
401 if (solo_enc->desc_count <= 1)
404 return solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
405 solo_enc->desc_dma, solo_enc->desc_count - 1);
408 /* Extract values from VOP header - VE_STATUSxx */
409 static inline int vop_interlaced(const vop_header *vh)
411 return (__le32_to_cpu((*vh)[0]) >> 30) & 1;
414 static inline u8 vop_channel(const vop_header *vh)
416 return (__le32_to_cpu((*vh)[0]) >> 24) & 0x1F;
419 static inline u8 vop_type(const vop_header *vh)
421 return (__le32_to_cpu((*vh)[0]) >> 22) & 3;
424 static inline u32 vop_mpeg_size(const vop_header *vh)
426 return __le32_to_cpu((*vh)[0]) & 0xFFFFF;
429 static inline u8 vop_hsize(const vop_header *vh)
431 return (__le32_to_cpu((*vh)[1]) >> 8) & 0xFF;
434 static inline u8 vop_vsize(const vop_header *vh)
436 return __le32_to_cpu((*vh)[1]) & 0xFF;
439 static inline u32 vop_mpeg_offset(const vop_header *vh)
441 return __le32_to_cpu((*vh)[2]);
444 static inline u32 vop_jpeg_offset(const vop_header *vh)
446 return __le32_to_cpu((*vh)[3]);
449 static inline u32 vop_jpeg_size(const vop_header *vh)
451 return __le32_to_cpu((*vh)[4]) & 0xFFFFF;
454 static inline u32 vop_sec(const vop_header *vh)
456 return __le32_to_cpu((*vh)[5]);
459 static inline u32 vop_usec(const vop_header *vh)
461 return __le32_to_cpu((*vh)[6]);
464 static int solo_fill_jpeg(struct solo_enc_dev *solo_enc,
465 struct vb2_buffer *vb, const vop_header *vh)
467 struct solo_dev *solo_dev = solo_enc->solo_dev;
468 struct sg_table *vbuf = vb2_dma_sg_plane_desc(vb, 0);
472 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
474 if (vb2_plane_size(vb, 0) < vop_jpeg_size(vh) + solo_enc->jpeg_len)
477 frame_size = ALIGN(vop_jpeg_size(vh) + solo_enc->jpeg_len, DMA_ALIGN);
478 vb2_set_plane_payload(vb, 0, vop_jpeg_size(vh) + solo_enc->jpeg_len);
480 /* may discard all previous data in vbuf->sgl */
481 dma_map_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
483 ret = solo_send_desc(solo_enc, solo_enc->jpeg_len, vbuf,
484 vop_jpeg_offset(vh) - SOLO_JPEG_EXT_ADDR(solo_dev),
485 frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
486 SOLO_JPEG_EXT_SIZE(solo_dev));
487 dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
490 /* add the header only after dma_unmap_sg() */
491 sg_copy_from_buffer(vbuf->sgl, vbuf->nents,
492 solo_enc->jpeg_header, solo_enc->jpeg_len);
497 static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
498 struct vb2_buffer *vb, const vop_header *vh)
500 struct solo_dev *solo_dev = solo_enc->solo_dev;
501 struct sg_table *vbuf = vb2_dma_sg_plane_desc(vb, 0);
502 int frame_off, frame_size;
506 if (vb2_plane_size(vb, 0) < vop_mpeg_size(vh))
509 /* If this is a key frame, add extra header */
510 vb->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME |
511 V4L2_BUF_FLAG_BFRAME);
513 skip = solo_enc->vop_len;
514 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
515 vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh) +
518 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
519 vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh));
522 /* Now get the actual mpeg payload */
523 frame_off = (vop_mpeg_offset(vh) - SOLO_MP4E_EXT_ADDR(solo_dev) +
524 sizeof(*vh)) % SOLO_MP4E_EXT_SIZE(solo_dev);
525 frame_size = ALIGN(vop_mpeg_size(vh) + skip, DMA_ALIGN);
527 /* may discard all previous data in vbuf->sgl */
528 dma_map_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
530 ret = solo_send_desc(solo_enc, skip, vbuf, frame_off, frame_size,
531 SOLO_MP4E_EXT_ADDR(solo_dev),
532 SOLO_MP4E_EXT_SIZE(solo_dev));
533 dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
536 /* add the header only after dma_unmap_sg() */
538 sg_copy_from_buffer(vbuf->sgl, vbuf->nents,
539 solo_enc->vop, solo_enc->vop_len);
543 static int solo_enc_fillbuf(struct solo_enc_dev *solo_enc,
544 struct vb2_buffer *vb, struct solo_enc_buf *enc_buf)
546 const vop_header *vh = enc_buf->vh;
549 switch (solo_enc->fmt) {
550 case V4L2_PIX_FMT_MPEG4:
551 case V4L2_PIX_FMT_H264:
552 ret = solo_fill_mpeg(solo_enc, vb, vh);
554 default: /* V4L2_PIX_FMT_MJPEG */
555 ret = solo_fill_jpeg(solo_enc, vb, vh);
560 bool send_event = false;
562 vb->v4l2_buf.sequence = solo_enc->sequence++;
563 vb->v4l2_buf.timestamp.tv_sec = vop_sec(vh);
564 vb->v4l2_buf.timestamp.tv_usec = vop_usec(vh);
566 /* Check for motion flags */
567 if (solo_is_motion_on(solo_enc)) {
568 /* It takes a few frames for the hardware to detect
569 * motion. Once it does it clears the motion detection
570 * register and it takes again a few frames before
571 * motion is seen. This means in practice that when the
572 * motion field is 1, it will go back to 0 for the next
573 * frame. This leads to motion detection event being
574 * sent all the time, which is not what we want.
575 * Instead wait a few frames before deciding that the
576 * motion has halted. After some experimentation it
577 * turns out that waiting for 5 frames works well.
579 if (enc_buf->motion == 0 &&
580 solo_enc->motion_last_state &&
581 solo_enc->frames_since_last_motion++ > 5)
583 else if (enc_buf->motion) {
584 solo_enc->frames_since_last_motion = 0;
585 send_event = !solo_enc->motion_last_state;
590 struct v4l2_event ev = {
591 .type = V4L2_EVENT_MOTION_DET,
593 .flags = V4L2_EVENT_MD_FL_HAVE_FRAME_SEQ,
594 .frame_sequence = vb->v4l2_buf.sequence,
595 .region_mask = enc_buf->motion ? 1 : 0,
599 solo_enc->motion_last_state = enc_buf->motion;
600 solo_enc->frames_since_last_motion = 0;
601 v4l2_event_queue(solo_enc->vfd, &ev);
605 vb2_buffer_done(vb, ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
610 static void solo_enc_handle_one(struct solo_enc_dev *solo_enc,
611 struct solo_enc_buf *enc_buf)
613 struct solo_vb2_buf *vb;
616 mutex_lock(&solo_enc->lock);
617 if (solo_enc->type != enc_buf->type)
620 spin_lock_irqsave(&solo_enc->av_lock, flags);
621 if (list_empty(&solo_enc->vidq_active)) {
622 spin_unlock_irqrestore(&solo_enc->av_lock, flags);
625 vb = list_first_entry(&solo_enc->vidq_active, struct solo_vb2_buf,
628 spin_unlock_irqrestore(&solo_enc->av_lock, flags);
630 solo_enc_fillbuf(solo_enc, &vb->vb, enc_buf);
632 mutex_unlock(&solo_enc->lock);
635 void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
637 wake_up_interruptible_all(&solo_dev->ring_thread_wait);
640 static void solo_handle_ring(struct solo_dev *solo_dev)
643 struct solo_enc_dev *solo_enc;
644 struct solo_enc_buf enc_buf;
645 u32 mpeg_current, off;
649 /* Check if the hardware has any new ones in the queue */
650 cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
651 if (cur_q == solo_dev->enc_idx)
654 mpeg_current = solo_reg_read(solo_dev,
655 SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
656 solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
658 ch = (mpeg_current >> 24) & 0x1f;
659 off = mpeg_current & 0x00ffffff;
661 if (ch >= SOLO_MAX_CHANNELS) {
662 ch -= SOLO_MAX_CHANNELS;
663 enc_buf.type = SOLO_ENC_TYPE_EXT;
665 enc_buf.type = SOLO_ENC_TYPE_STD;
667 solo_enc = solo_dev->v4l2_enc[ch];
668 if (solo_enc == NULL) {
669 dev_err(&solo_dev->pdev->dev,
670 "Got spurious packet for channel %d\n", ch);
675 if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
679 enc_buf.vh = solo_dev->vh_buf;
682 if (vop_mpeg_offset(enc_buf.vh) !=
683 SOLO_MP4E_EXT_ADDR(solo_dev) + off)
686 if (solo_motion_detected(solo_enc))
691 solo_enc_handle_one(solo_enc, &enc_buf);
695 static int solo_ring_thread(void *data)
697 struct solo_dev *solo_dev = data;
698 DECLARE_WAITQUEUE(wait, current);
701 add_wait_queue(&solo_dev->ring_thread_wait, &wait);
704 long timeout = schedule_timeout_interruptible(HZ);
705 if (timeout == -ERESTARTSYS || kthread_should_stop())
707 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
708 solo_handle_ring(solo_dev);
709 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
713 remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
718 static int solo_enc_queue_setup(struct vb2_queue *q,
719 const struct v4l2_format *fmt,
720 unsigned int *num_buffers,
721 unsigned int *num_planes, unsigned int sizes[],
724 sizes[0] = FRAME_BUF_SIZE;
727 if (*num_buffers < MIN_VID_BUFFERS)
728 *num_buffers = MIN_VID_BUFFERS;
733 static void solo_enc_buf_queue(struct vb2_buffer *vb)
735 struct vb2_queue *vq = vb->vb2_queue;
736 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
737 struct solo_vb2_buf *solo_vb =
738 container_of(vb, struct solo_vb2_buf, vb);
740 spin_lock(&solo_enc->av_lock);
741 list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
742 spin_unlock(&solo_enc->av_lock);
745 static int solo_ring_start(struct solo_dev *solo_dev)
747 solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
748 SOLO6X10_NAME "_ring");
749 if (IS_ERR(solo_dev->ring_thread)) {
750 int err = PTR_ERR(solo_dev->ring_thread);
751 solo_dev->ring_thread = NULL;
755 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
760 static void solo_ring_stop(struct solo_dev *solo_dev)
762 if (solo_dev->ring_thread) {
763 kthread_stop(solo_dev->ring_thread);
764 solo_dev->ring_thread = NULL;
767 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
770 static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
772 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
775 ret = solo_enc_on(solo_enc);
778 return solo_ring_start(solo_enc->solo_dev);
781 static void solo_enc_stop_streaming(struct vb2_queue *q)
783 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
785 solo_enc_off(solo_enc);
786 INIT_LIST_HEAD(&solo_enc->vidq_active);
787 solo_ring_stop(solo_enc->solo_dev);
790 static struct vb2_ops solo_enc_video_qops = {
791 .queue_setup = solo_enc_queue_setup,
792 .buf_queue = solo_enc_buf_queue,
793 .start_streaming = solo_enc_start_streaming,
794 .stop_streaming = solo_enc_stop_streaming,
795 .wait_prepare = vb2_ops_wait_prepare,
796 .wait_finish = vb2_ops_wait_finish,
799 static int solo_enc_querycap(struct file *file, void *priv,
800 struct v4l2_capability *cap)
802 struct solo_enc_dev *solo_enc = video_drvdata(file);
803 struct solo_dev *solo_dev = solo_enc->solo_dev;
805 strcpy(cap->driver, SOLO6X10_NAME);
806 snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
808 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
809 pci_name(solo_dev->pdev));
810 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
811 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
812 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
816 static int solo_enc_enum_input(struct file *file, void *priv,
817 struct v4l2_input *input)
819 struct solo_enc_dev *solo_enc = video_drvdata(file);
820 struct solo_dev *solo_dev = solo_enc->solo_dev;
825 snprintf(input->name, sizeof(input->name), "Encoder %d",
827 input->type = V4L2_INPUT_TYPE_CAMERA;
828 input->std = solo_enc->vfd->tvnorms;
830 if (!tw28_get_video_status(solo_dev, solo_enc->ch))
831 input->status = V4L2_IN_ST_NO_SIGNAL;
836 static int solo_enc_set_input(struct file *file, void *priv,
845 static int solo_enc_get_input(struct file *file, void *priv,
853 static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
854 struct v4l2_fmtdesc *f)
856 struct solo_enc_dev *solo_enc = video_drvdata(file);
857 int dev_type = solo_enc->solo_dev->type;
863 f->pixelformat = V4L2_PIX_FMT_MPEG4;
864 strcpy(f->description, "MPEG-4 part 2");
867 f->pixelformat = V4L2_PIX_FMT_H264;
868 strcpy(f->description, "H.264");
873 f->pixelformat = V4L2_PIX_FMT_MJPEG;
874 strcpy(f->description, "MJPEG");
880 f->flags = V4L2_FMT_FLAG_COMPRESSED;
885 static inline int solo_valid_pixfmt(u32 pixfmt, int dev_type)
887 return (pixfmt == V4L2_PIX_FMT_H264 && dev_type == SOLO_DEV_6110)
888 || (pixfmt == V4L2_PIX_FMT_MPEG4 && dev_type == SOLO_DEV_6010)
889 || pixfmt == V4L2_PIX_FMT_MJPEG ? 0 : -EINVAL;
892 static int solo_enc_try_fmt_cap(struct file *file, void *priv,
893 struct v4l2_format *f)
895 struct solo_enc_dev *solo_enc = video_drvdata(file);
896 struct solo_dev *solo_dev = solo_enc->solo_dev;
897 struct v4l2_pix_format *pix = &f->fmt.pix;
899 if (solo_valid_pixfmt(pix->pixelformat, solo_dev->type))
902 if (pix->width < solo_dev->video_hsize ||
903 pix->height < solo_dev->video_vsize << 1) {
904 /* Default to CIF 1/2 size */
905 pix->width = solo_dev->video_hsize >> 1;
906 pix->height = solo_dev->video_vsize;
909 pix->width = solo_dev->video_hsize;
910 pix->height = solo_dev->video_vsize << 1;
913 switch (pix->field) {
914 case V4L2_FIELD_NONE:
915 case V4L2_FIELD_INTERLACED:
919 pix->field = V4L2_FIELD_INTERLACED;
924 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
925 pix->sizeimage = FRAME_BUF_SIZE;
926 pix->bytesperline = 0;
932 static int solo_enc_set_fmt_cap(struct file *file, void *priv,
933 struct v4l2_format *f)
935 struct solo_enc_dev *solo_enc = video_drvdata(file);
936 struct solo_dev *solo_dev = solo_enc->solo_dev;
937 struct v4l2_pix_format *pix = &f->fmt.pix;
940 if (vb2_is_busy(&solo_enc->vidq))
943 ret = solo_enc_try_fmt_cap(file, priv, f);
947 if (pix->width == solo_dev->video_hsize)
948 solo_enc->mode = SOLO_ENC_MODE_D1;
950 solo_enc->mode = SOLO_ENC_MODE_CIF;
952 /* This does not change the encoder at all */
953 solo_enc->fmt = pix->pixelformat;
956 * More information is needed about these 'extended' types. As far
957 * as I can tell these are basically additional video streams with
958 * different MPEG encoding attributes that can run in parallel with
959 * the main stream. If so, then this should be implemented as a
960 * second video node. Abusing priv like this is certainly not the
963 solo_enc->type = SOLO_ENC_TYPE_EXT;
965 solo_update_mode(solo_enc);
969 static int solo_enc_get_fmt_cap(struct file *file, void *priv,
970 struct v4l2_format *f)
972 struct solo_enc_dev *solo_enc = video_drvdata(file);
973 struct v4l2_pix_format *pix = &f->fmt.pix;
975 pix->width = solo_enc->width;
976 pix->height = solo_enc->height;
977 pix->pixelformat = solo_enc->fmt;
978 pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
980 pix->sizeimage = FRAME_BUF_SIZE;
981 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
987 static int solo_enc_g_std(struct file *file, void *priv, v4l2_std_id *i)
989 struct solo_enc_dev *solo_enc = video_drvdata(file);
990 struct solo_dev *solo_dev = solo_enc->solo_dev;
992 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
993 *i = V4L2_STD_NTSC_M;
999 static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id std)
1001 struct solo_enc_dev *solo_enc = video_drvdata(file);
1003 return solo_set_video_type(solo_enc->solo_dev, std & V4L2_STD_625_50);
1006 static int solo_enum_framesizes(struct file *file, void *priv,
1007 struct v4l2_frmsizeenum *fsize)
1009 struct solo_enc_dev *solo_enc = video_drvdata(file);
1010 struct solo_dev *solo_dev = solo_enc->solo_dev;
1012 if (solo_valid_pixfmt(fsize->pixel_format, solo_dev->type))
1015 switch (fsize->index) {
1017 fsize->discrete.width = solo_dev->video_hsize >> 1;
1018 fsize->discrete.height = solo_dev->video_vsize;
1021 fsize->discrete.width = solo_dev->video_hsize;
1022 fsize->discrete.height = solo_dev->video_vsize << 1;
1028 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1033 static int solo_enum_frameintervals(struct file *file, void *priv,
1034 struct v4l2_frmivalenum *fintv)
1036 struct solo_enc_dev *solo_enc = video_drvdata(file);
1037 struct solo_dev *solo_dev = solo_enc->solo_dev;
1039 if (solo_valid_pixfmt(fintv->pixel_format, solo_dev->type))
1043 if ((fintv->width != solo_dev->video_hsize >> 1 ||
1044 fintv->height != solo_dev->video_vsize) &&
1045 (fintv->width != solo_dev->video_hsize ||
1046 fintv->height != solo_dev->video_vsize << 1))
1049 fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1051 fintv->stepwise.min.numerator = 1;
1052 fintv->stepwise.min.denominator = solo_dev->fps;
1054 fintv->stepwise.max.numerator = 15;
1055 fintv->stepwise.max.denominator = solo_dev->fps;
1057 fintv->stepwise.step.numerator = 1;
1058 fintv->stepwise.step.denominator = solo_dev->fps;
1063 static int solo_g_parm(struct file *file, void *priv,
1064 struct v4l2_streamparm *sp)
1066 struct solo_enc_dev *solo_enc = video_drvdata(file);
1067 struct v4l2_captureparm *cp = &sp->parm.capture;
1069 cp->capability = V4L2_CAP_TIMEPERFRAME;
1070 cp->timeperframe.numerator = solo_enc->interval;
1071 cp->timeperframe.denominator = solo_enc->solo_dev->fps;
1072 cp->capturemode = 0;
1073 /* XXX: Shouldn't we be able to get/set this from videobuf? */
1074 cp->readbuffers = 2;
1079 static inline int calc_interval(u8 fps, u32 n, u32 d)
1086 return min(15U, n / d + (n % d >= (fps >> 1)));
1089 static int solo_s_parm(struct file *file, void *priv,
1090 struct v4l2_streamparm *sp)
1092 struct solo_enc_dev *solo_enc = video_drvdata(file);
1093 struct v4l2_fract *t = &sp->parm.capture.timeperframe;
1094 u8 fps = solo_enc->solo_dev->fps;
1096 if (vb2_is_streaming(&solo_enc->vidq))
1099 solo_enc->interval = calc_interval(fps, t->numerator, t->denominator);
1100 solo_update_mode(solo_enc);
1101 return solo_g_parm(file, priv, sp);
1104 static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
1106 struct solo_enc_dev *solo_enc =
1107 container_of(ctrl->handler, struct solo_enc_dev, hdl);
1108 struct solo_dev *solo_dev = solo_enc->solo_dev;
1112 case V4L2_CID_BRIGHTNESS:
1113 case V4L2_CID_CONTRAST:
1114 case V4L2_CID_SATURATION:
1116 case V4L2_CID_SHARPNESS:
1117 return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
1119 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1120 solo_enc->gop = ctrl->val;
1122 case V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD:
1123 solo_enc->motion_thresh = ctrl->val << 8;
1124 if (!solo_enc->motion_global || !solo_enc->motion_enabled)
1126 return solo_set_motion_threshold(solo_dev, solo_enc->ch,
1127 solo_enc->motion_thresh);
1128 case V4L2_CID_DETECT_MD_MODE:
1129 solo_enc->motion_global = ctrl->val == V4L2_DETECT_MD_MODE_GLOBAL;
1130 solo_enc->motion_enabled = ctrl->val > V4L2_DETECT_MD_MODE_DISABLED;
1132 if (solo_enc->motion_global)
1133 solo_set_motion_threshold(solo_dev, solo_enc->ch,
1134 solo_enc->motion_thresh);
1136 solo_set_motion_block(solo_dev, solo_enc->ch,
1137 solo_enc->md_thresholds->p_cur.p_u16);
1139 solo_motion_toggle(solo_enc, ctrl->val);
1141 case V4L2_CID_DETECT_MD_THRESHOLD_GRID:
1142 if (solo_enc->motion_enabled && !solo_enc->motion_global)
1143 return solo_set_motion_block(solo_dev, solo_enc->ch,
1144 solo_enc->md_thresholds->p_new.p_u16);
1146 case V4L2_CID_OSD_TEXT:
1147 strcpy(solo_enc->osd_text, ctrl->p_new.p_char);
1148 err = solo_osd_print(solo_enc);
1157 static int solo_subscribe_event(struct v4l2_fh *fh,
1158 const struct v4l2_event_subscription *sub)
1161 switch (sub->type) {
1162 case V4L2_EVENT_CTRL:
1163 return v4l2_ctrl_subscribe_event(fh, sub);
1164 case V4L2_EVENT_MOTION_DET:
1165 /* Allow for up to 30 events (1 second for NTSC) to be
1167 return v4l2_event_subscribe(fh, sub, 30, NULL);
1172 static const struct v4l2_file_operations solo_enc_fops = {
1173 .owner = THIS_MODULE,
1174 .open = v4l2_fh_open,
1175 .release = vb2_fop_release,
1176 .read = vb2_fop_read,
1177 .poll = vb2_fop_poll,
1178 .mmap = vb2_fop_mmap,
1179 .unlocked_ioctl = video_ioctl2,
1182 static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
1183 .vidioc_querycap = solo_enc_querycap,
1184 .vidioc_s_std = solo_enc_s_std,
1185 .vidioc_g_std = solo_enc_g_std,
1186 /* Input callbacks */
1187 .vidioc_enum_input = solo_enc_enum_input,
1188 .vidioc_s_input = solo_enc_set_input,
1189 .vidioc_g_input = solo_enc_get_input,
1190 /* Video capture format callbacks */
1191 .vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
1192 .vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
1193 .vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
1194 .vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
1196 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1197 .vidioc_querybuf = vb2_ioctl_querybuf,
1198 .vidioc_qbuf = vb2_ioctl_qbuf,
1199 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1200 .vidioc_streamon = vb2_ioctl_streamon,
1201 .vidioc_streamoff = vb2_ioctl_streamoff,
1202 /* Frame size and interval */
1203 .vidioc_enum_framesizes = solo_enum_framesizes,
1204 .vidioc_enum_frameintervals = solo_enum_frameintervals,
1205 /* Video capture parameters */
1206 .vidioc_s_parm = solo_s_parm,
1207 .vidioc_g_parm = solo_g_parm,
1208 /* Logging and events */
1209 .vidioc_log_status = v4l2_ctrl_log_status,
1210 .vidioc_subscribe_event = solo_subscribe_event,
1211 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1214 static const struct video_device solo_enc_template = {
1215 .name = SOLO6X10_NAME,
1216 .fops = &solo_enc_fops,
1217 .ioctl_ops = &solo_enc_ioctl_ops,
1219 .release = video_device_release,
1220 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1223 static const struct v4l2_ctrl_ops solo_ctrl_ops = {
1224 .s_ctrl = solo_s_ctrl,
1227 static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
1228 .ops = &solo_ctrl_ops,
1229 .id = V4L2_CID_OSD_TEXT,
1231 .type = V4L2_CTRL_TYPE_STRING,
1232 .max = OSD_TEXT_MAX,
1236 /* Motion Detection Threshold matrix */
1237 static const struct v4l2_ctrl_config solo_md_thresholds = {
1238 .ops = &solo_ctrl_ops,
1239 .id = V4L2_CID_DETECT_MD_THRESHOLD_GRID,
1240 .dims = { SOLO_MOTION_SZ, SOLO_MOTION_SZ },
1241 .def = SOLO_DEF_MOT_THRESH,
1246 static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1249 struct solo_enc_dev *solo_enc;
1250 struct v4l2_ctrl_handler *hdl;
1253 solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
1255 return ERR_PTR(-ENOMEM);
1257 hdl = &solo_enc->hdl;
1258 v4l2_ctrl_handler_init(hdl, 10);
1259 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1260 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1261 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1262 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1263 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1264 V4L2_CID_SATURATION, 0, 255, 1, 128);
1265 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1266 V4L2_CID_HUE, 0, 255, 1, 128);
1267 if (tw28_has_sharpness(solo_dev, ch))
1268 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1269 V4L2_CID_SHARPNESS, 0, 15, 1, 0);
1270 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1271 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
1272 v4l2_ctrl_new_std_menu(hdl, &solo_ctrl_ops,
1273 V4L2_CID_DETECT_MD_MODE,
1274 V4L2_DETECT_MD_MODE_THRESHOLD_GRID, 0,
1275 V4L2_DETECT_MD_MODE_DISABLED);
1276 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1277 V4L2_CID_DETECT_MD_GLOBAL_THRESHOLD, 0, 0xff, 1,
1278 SOLO_DEF_MOT_THRESH >> 8);
1279 v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
1280 solo_enc->md_thresholds =
1281 v4l2_ctrl_new_custom(hdl, &solo_md_thresholds, NULL);
1287 solo_enc->solo_dev = solo_dev;
1289 mutex_init(&solo_enc->lock);
1290 spin_lock_init(&solo_enc->av_lock);
1291 INIT_LIST_HEAD(&solo_enc->vidq_active);
1292 solo_enc->fmt = (solo_dev->type == SOLO_DEV_6010) ?
1293 V4L2_PIX_FMT_MPEG4 : V4L2_PIX_FMT_H264;
1294 solo_enc->type = SOLO_ENC_TYPE_STD;
1296 solo_enc->qp = SOLO_DEFAULT_QP;
1297 solo_enc->gop = solo_dev->fps;
1298 solo_enc->interval = 1;
1299 solo_enc->mode = SOLO_ENC_MODE_CIF;
1300 solo_enc->motion_global = true;
1301 solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
1302 solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1303 solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1304 solo_enc->vidq.ops = &solo_enc_video_qops;
1305 solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
1306 solo_enc->vidq.drv_priv = solo_enc;
1307 solo_enc->vidq.gfp_flags = __GFP_DMA32;
1308 solo_enc->vidq.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1309 solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
1310 solo_enc->vidq.lock = &solo_enc->lock;
1311 ret = vb2_queue_init(&solo_enc->vidq);
1314 solo_update_mode(solo_enc);
1316 spin_lock_init(&solo_enc->motion_lock);
1318 /* Initialize this per encoder */
1319 solo_enc->jpeg_len = sizeof(jpeg_header);
1320 memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
1322 solo_enc->desc_nelts = 32;
1323 solo_enc->desc_items = pci_alloc_consistent(solo_dev->pdev,
1324 sizeof(struct solo_p2m_desc) *
1325 solo_enc->desc_nelts,
1326 &solo_enc->desc_dma);
1328 if (solo_enc->desc_items == NULL)
1331 solo_enc->vfd = video_device_alloc();
1335 *solo_enc->vfd = solo_enc_template;
1336 solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
1337 solo_enc->vfd->ctrl_handler = hdl;
1338 solo_enc->vfd->queue = &solo_enc->vidq;
1339 solo_enc->vfd->lock = &solo_enc->lock;
1340 video_set_drvdata(solo_enc->vfd, solo_enc);
1341 ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER, nr);
1345 snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
1346 "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
1347 solo_enc->vfd->num);
1352 video_device_release(solo_enc->vfd);
1354 pci_free_consistent(solo_enc->solo_dev->pdev,
1355 sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
1356 solo_enc->desc_items, solo_enc->desc_dma);
1358 v4l2_ctrl_handler_free(hdl);
1360 return ERR_PTR(ret);
1363 static void solo_enc_free(struct solo_enc_dev *solo_enc)
1365 if (solo_enc == NULL)
1368 video_unregister_device(solo_enc->vfd);
1369 v4l2_ctrl_handler_free(&solo_enc->hdl);
1373 int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
1377 init_waitqueue_head(&solo_dev->ring_thread_wait);
1379 solo_dev->vh_size = sizeof(vop_header);
1380 solo_dev->vh_buf = pci_alloc_consistent(solo_dev->pdev,
1383 if (solo_dev->vh_buf == NULL)
1386 for (i = 0; i < solo_dev->nr_chans; i++) {
1387 solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
1388 if (IS_ERR(solo_dev->v4l2_enc[i]))
1392 if (i != solo_dev->nr_chans) {
1393 int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
1395 solo_enc_free(solo_dev->v4l2_enc[i]);
1396 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1397 solo_dev->vh_buf, solo_dev->vh_dma);
1398 solo_dev->vh_buf = NULL;
1402 if (solo_dev->type == SOLO_DEV_6010)
1403 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
1405 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
1407 dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
1408 solo_dev->v4l2_enc[0]->vfd->num,
1409 solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
1414 void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
1418 for (i = 0; i < solo_dev->nr_chans; i++)
1419 solo_enc_free(solo_dev->v4l2_enc[i]);
1421 if (solo_dev->vh_buf)
1422 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1423 solo_dev->vh_buf, solo_dev->vh_dma);