blob: 90a0645c60618da086e60fad5143ff8dcc21fa3b [file] [log] [blame]
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001/*
Hans Verkuildcae5da2013-03-25 05:35:17 -03002 * 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>
Ben Collinsfaa4fd22010-06-17 13:27:26 -04009 *
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>
Hans Verkuildcae5da2013-03-25 05:35:17 -030029
Ben Collinsfaa4fd22010-06-17 13:27:26 -040030#include <media/v4l2-ioctl.h>
31#include <media/v4l2-common.h>
Hans Verkuil94160492013-03-12 18:47:03 -030032#include <media/v4l2-event.h>
Hans Verkuil382c31a2013-03-15 12:04:14 -030033#include <media/videobuf2-dma-sg.h>
Hans Verkuildcae5da2013-03-25 05:35:17 -030034
Krzysztof Hałasaae69b222011-02-11 13:36:27 +010035#include "solo6x10.h"
Hans Verkuildad7fab2013-03-25 05:42:46 -030036#include "solo6x10-tw28.h"
Hans Verkuilb3c7d452011-11-03 06:57:08 -030037#include "solo6x10-jpeg.h"
Ben Collinsfaa4fd22010-06-17 13:27:26 -040038
Hans Verkuildcae5da2013-03-25 05:35:17 -030039#define MIN_VID_BUFFERS 2
40#define FRAME_BUF_SIZE (196 * 1024)
Ben Collinsfaa4fd22010-06-17 13:27:26 -040041#define MP4_QS 16
Hans Verkuildcae5da2013-03-25 05:35:17 -030042#define DMA_ALIGN 4096
Ben Collinsfaa4fd22010-06-17 13:27:26 -040043
Hans Verkuildcae5da2013-03-25 05:35:17 -030044/* 6010 M4V */
45static 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,
50};
51
52static 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,
57};
58
59static 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,
64};
65
66static 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,
71};
72
73/* 6110 h.264 */
74static 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,
78};
79
80static 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,
84};
85
86static 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,
90};
91
92static 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,
96};
97
Hans Verkuildcae5da2013-03-25 05:35:17 -030098struct vop_header {
99 /* VE_STATUS0 */
100 u32 mpeg_size:20, sad_motion_flag:1, video_motion_flag:1, vop_type:2,
101 channel:5, source_fl:1, interlace:1, progressive:1;
102
103 /* VE_STATUS1 */
104 u32 vsize:8, hsize:8, last_queue:4, nop0:8, scale:4;
105
106 /* VE_STATUS2 */
107 u32 mpeg_off;
108
109 /* VE_STATUS3 */
110 u32 jpeg_off;
111
112 /* VE_STATUS4 */
113 u32 jpeg_size:20, interval:10, nop1:2;
114
115 /* VE_STATUS5/6 */
116 u32 sec, usec;
117
118 /* VE_STATUS7/8/9 */
119 u32 nop2[3];
120
121 /* VE_STATUS10 */
122 u32 mpeg_size_alt:20, nop3:12;
123
124 u32 end_nops[5];
125} __packed;
126
127struct solo_enc_buf {
128 enum solo_enc_types type;
129 struct vop_header *vh;
130 int motion;
131};
132
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400133static int solo_is_motion_on(struct solo_enc_dev *solo_enc)
134{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100135 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400136
Hans Verkuildcae5da2013-03-25 05:35:17 -0300137 return (solo_dev->motion_mask >> solo_enc->ch) & 1;
138}
139
140static int solo_motion_detected(struct solo_enc_dev *solo_enc)
141{
142 struct solo_dev *solo_dev = solo_enc->solo_dev;
143 unsigned long flags;
144 u32 ch_mask = 1 << solo_enc->ch;
145 int ret = 0;
146
147 spin_lock_irqsave(&solo_enc->motion_lock, flags);
148 if (solo_reg_read(solo_dev, SOLO_VI_MOT_STATUS) & ch_mask) {
149 solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, ch_mask);
150 ret = 1;
151 }
152 spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
153
154 return ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400155}
156
157static void solo_motion_toggle(struct solo_enc_dev *solo_enc, int on)
158{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100159 struct solo_dev *solo_dev = solo_enc->solo_dev;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300160 u32 mask = 1 << solo_enc->ch;
161 unsigned long flags;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400162
Hans Verkuildcae5da2013-03-25 05:35:17 -0300163 spin_lock_irqsave(&solo_enc->motion_lock, flags);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400164
165 if (on)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300166 solo_dev->motion_mask |= mask;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400167 else
Hans Verkuildcae5da2013-03-25 05:35:17 -0300168 solo_dev->motion_mask &= ~mask;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400169
Hans Verkuildcae5da2013-03-25 05:35:17 -0300170 solo_reg_write(solo_dev, SOLO_VI_MOT_CLEAR, mask);
Ben Collinsf62de9b2010-11-04 22:51:17 -0400171
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400172 solo_reg_write(solo_dev, SOLO_VI_MOT_ADR,
173 SOLO_VI_MOTION_EN(solo_dev->motion_mask) |
174 (SOLO_MOTION_EXT_ADDR(solo_dev) >> 16));
175
Hans Verkuildcae5da2013-03-25 05:35:17 -0300176 spin_unlock_irqrestore(&solo_enc->motion_lock, flags);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400177}
178
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300179void solo_update_mode(struct solo_enc_dev *solo_enc)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400180{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100181 struct solo_dev *solo_dev = solo_enc->solo_dev;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300182 int vop_len;
183 unsigned char *vop;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400184
185 solo_enc->interlaced = (solo_enc->mode & 0x08) ? 1 : 0;
186 solo_enc->bw_weight = max(solo_dev->fps / solo_enc->interval, 1);
187
Hans Verkuildcae5da2013-03-25 05:35:17 -0300188 if (solo_enc->mode == SOLO_ENC_MODE_CIF) {
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400189 solo_enc->width = solo_dev->video_hsize >> 1;
190 solo_enc->height = solo_dev->video_vsize;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300191 if (solo_dev->type == SOLO_DEV_6110) {
192 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
193 vop = vop_6110_ntsc_cif;
194 vop_len = sizeof(vop_6110_ntsc_cif);
195 } else {
196 vop = vop_6110_pal_cif;
197 vop_len = sizeof(vop_6110_pal_cif);
198 }
199 } else {
200 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
201 vop = vop_6010_ntsc_cif;
202 vop_len = sizeof(vop_6010_ntsc_cif);
203 } else {
204 vop = vop_6010_pal_cif;
205 vop_len = sizeof(vop_6010_pal_cif);
206 }
207 }
208 } else {
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400209 solo_enc->width = solo_dev->video_hsize;
210 solo_enc->height = solo_dev->video_vsize << 1;
211 solo_enc->bw_weight <<= 2;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300212 if (solo_dev->type == SOLO_DEV_6110) {
213 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
214 vop = vop_6110_ntsc_d1;
215 vop_len = sizeof(vop_6110_ntsc_d1);
216 } else {
217 vop = vop_6110_pal_d1;
218 vop_len = sizeof(vop_6110_pal_d1);
219 }
220 } else {
221 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC) {
222 vop = vop_6010_ntsc_d1;
223 vop_len = sizeof(vop_6010_ntsc_d1);
224 } else {
225 vop = vop_6010_pal_d1;
226 vop_len = sizeof(vop_6010_pal_d1);
227 }
228 }
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400229 }
Hans Verkuildcae5da2013-03-25 05:35:17 -0300230
231 memcpy(solo_enc->vop, vop, vop_len);
232
233 /* Some fixups for 6010/M4V */
234 if (solo_dev->type == SOLO_DEV_6010) {
235 u16 fps = solo_dev->fps * 1000;
236 u16 interval = solo_enc->interval * 1000;
237
238 vop = solo_enc->vop;
239
240 /* Frame rate and interval */
241 vop[22] = fps >> 4;
242 vop[23] = ((fps << 4) & 0xf0) | 0x0c
243 | ((interval >> 13) & 0x3);
244 vop[24] = (interval >> 5) & 0xff;
245 vop[25] = ((interval << 3) & 0xf8) | 0x04;
246 }
247
248 solo_enc->vop_len = vop_len;
249
250 /* Now handle the jpeg header */
251 vop = solo_enc->jpeg_header;
252 vop[SOF0_START + 5] = 0xff & (solo_enc->height >> 8);
253 vop[SOF0_START + 6] = 0xff & solo_enc->height;
254 vop[SOF0_START + 7] = 0xff & (solo_enc->width >> 8);
255 vop[SOF0_START + 8] = 0xff & solo_enc->width;
256
257 memcpy(vop + DQT_START,
258 jpeg_dqt[solo_g_jpeg_qp(solo_dev, solo_enc->ch)], DQT_LEN);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400259}
260
Hans Verkuil382c31a2013-03-15 12:04:14 -0300261static int solo_enc_on(struct solo_enc_dev *solo_enc)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400262{
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400263 u8 ch = solo_enc->ch;
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100264 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400265 u8 interval;
266
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400267 solo_update_mode(solo_enc);
268
Hans Verkuil382c31a2013-03-15 12:04:14 -0300269 /* Make sure to do a bandwidth check */
270 if (solo_enc->bw_weight > solo_dev->enc_bw_remain)
271 return -EBUSY;
Hans Verkuil15513c12013-03-15 13:16:49 -0300272 solo_enc->sequence = 0;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300273 solo_dev->enc_bw_remain -= solo_enc->bw_weight;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400274
Hans Verkuila7eb9312013-03-18 08:41:13 -0300275 if (solo_enc->type == SOLO_ENC_TYPE_EXT)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400276 solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(ch), 1);
277
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400278 /* Disable all encoding for this channel */
279 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), 0);
280
281 /* Common for both std and ext encoding */
282 solo_reg_write(solo_dev, SOLO_VE_CH_INTL(ch),
283 solo_enc->interlaced ? 1 : 0);
284
285 if (solo_enc->interlaced)
286 interval = solo_enc->interval - 1;
287 else
288 interval = solo_enc->interval;
289
290 /* Standard encoding only */
291 solo_reg_write(solo_dev, SOLO_VE_CH_GOP(ch), solo_enc->gop);
292 solo_reg_write(solo_dev, SOLO_VE_CH_QP(ch), solo_enc->qp);
293 solo_reg_write(solo_dev, SOLO_CAP_CH_INTV(ch), interval);
294
295 /* Extended encoding only */
296 solo_reg_write(solo_dev, SOLO_VE_CH_GOP_E(ch), solo_enc->gop);
297 solo_reg_write(solo_dev, SOLO_VE_CH_QP_E(ch), solo_enc->qp);
298 solo_reg_write(solo_dev, SOLO_CAP_CH_INTV_E(ch), interval);
299
300 /* Enables the standard encoder */
301 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(ch), solo_enc->mode);
302
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400303 return 0;
304}
305
Hans Verkuil382c31a2013-03-15 12:04:14 -0300306static void solo_enc_off(struct solo_enc_dev *solo_enc)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400307{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100308 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400309
Hans Verkuildcae5da2013-03-25 05:35:17 -0300310 solo_dev->enc_bw_remain += solo_enc->bw_weight;
311
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400312 solo_reg_write(solo_dev, SOLO_CAP_CH_SCALE(solo_enc->ch), 0);
313 solo_reg_write(solo_dev, SOLO_CAP_CH_COMP_ENA_E(solo_enc->ch), 0);
314}
315
Hans Verkuildcae5da2013-03-25 05:35:17 -0300316static int enc_get_mpeg_dma(struct solo_dev *solo_dev, dma_addr_t dma,
317 unsigned int off, unsigned int size)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400318{
319 int ret;
320
321 if (off > SOLO_MP4E_EXT_SIZE(solo_dev))
322 return -EINVAL;
323
Hans Verkuildcae5da2013-03-25 05:35:17 -0300324 /* Single shot */
Ben Collinsf62de9b2010-11-04 22:51:17 -0400325 if (off + size <= SOLO_MP4E_EXT_SIZE(solo_dev)) {
Hans Verkuildcae5da2013-03-25 05:35:17 -0300326 return solo_p2m_dma_t(solo_dev, 0, dma,
327 SOLO_MP4E_EXT_ADDR(solo_dev) + off, size,
328 0, 0);
Ben Collinsf62de9b2010-11-04 22:51:17 -0400329 }
330
331 /* Buffer wrap */
Hans Verkuildcae5da2013-03-25 05:35:17 -0300332 ret = solo_p2m_dma_t(solo_dev, 0, dma,
Ben Collinsf62de9b2010-11-04 22:51:17 -0400333 SOLO_MP4E_EXT_ADDR(solo_dev) + off,
Hans Verkuildcae5da2013-03-25 05:35:17 -0300334 SOLO_MP4E_EXT_SIZE(solo_dev) - off, 0, 0);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400335
Hans Verkuildcae5da2013-03-25 05:35:17 -0300336 if (!ret) {
337 ret = solo_p2m_dma_t(solo_dev, 0,
338 dma + SOLO_MP4E_EXT_SIZE(solo_dev) - off,
339 SOLO_MP4E_EXT_ADDR(solo_dev),
340 size + off - SOLO_MP4E_EXT_SIZE(solo_dev), 0, 0);
Ben Collinsf62de9b2010-11-04 22:51:17 -0400341 }
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400342
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400343 return ret;
344}
345
Hans Verkuildcae5da2013-03-25 05:35:17 -0300346/* Build a descriptor queue out of an SG list and send it to the P2M for
347 * processing. */
Hans Verkuila7eb9312013-03-18 08:41:13 -0300348static int solo_send_desc(struct solo_enc_dev *solo_enc, int skip,
Hans Verkuil382c31a2013-03-15 12:04:14 -0300349 struct vb2_dma_sg_desc *vbuf, int off, int size,
Hans Verkuildcae5da2013-03-25 05:35:17 -0300350 unsigned int base, unsigned int base_size)
Ben Collinsf62de9b2010-11-04 22:51:17 -0400351{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300352 struct solo_dev *solo_dev = solo_enc->solo_dev;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300353 struct scatterlist *sg;
354 int i;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400355 int ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400356
Hans Verkuildcae5da2013-03-25 05:35:17 -0300357 if (WARN_ON_ONCE(size > FRAME_BUF_SIZE))
Ben Collinsf62de9b2010-11-04 22:51:17 -0400358 return -EINVAL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400359
Hans Verkuila7eb9312013-03-18 08:41:13 -0300360 solo_enc->desc_count = 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400361
Hans Verkuil382c31a2013-03-15 12:04:14 -0300362 for_each_sg(vbuf->sglist, sg, vbuf->num_pages, i) {
Hans Verkuildcae5da2013-03-25 05:35:17 -0300363 struct solo_p2m_desc *desc;
364 dma_addr_t dma;
365 int len;
366 int left = base_size - off;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400367
Hans Verkuila7eb9312013-03-18 08:41:13 -0300368 desc = &solo_enc->desc_items[solo_enc->desc_count++];
Hans Verkuildcae5da2013-03-25 05:35:17 -0300369 dma = sg_dma_address(sg);
370 len = sg_dma_len(sg);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400371
Hans Verkuildcae5da2013-03-25 05:35:17 -0300372 /* We assume this is smaller than the scatter size */
373 BUG_ON(skip >= len);
374 if (skip) {
375 len -= skip;
376 dma += skip;
377 size -= skip;
378 skip = 0;
379 }
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400380
Hans Verkuildcae5da2013-03-25 05:35:17 -0300381 len = min(len, size);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400382
Hans Verkuildcae5da2013-03-25 05:35:17 -0300383 if (len <= left) {
384 /* Single descriptor */
385 solo_p2m_fill_desc(desc, 0, dma, base + off,
386 len, 0, 0);
Ben Collinsf62de9b2010-11-04 22:51:17 -0400387 } else {
Hans Verkuildcae5da2013-03-25 05:35:17 -0300388 /* Buffer wrap */
389 /* XXX: Do these as separate DMA requests, to avoid
390 timeout errors triggered by awkwardly sized
391 descriptors. See
392 <https://github.com/bluecherrydvr/solo6x10/issues/8>
393 */
394 ret = solo_p2m_dma_t(solo_dev, 0, dma, base + off,
395 left, 0, 0);
396 if (ret)
397 return ret;
398
399 ret = solo_p2m_dma_t(solo_dev, 0, dma + left, base,
400 len - left, 0, 0);
401 if (ret)
402 return ret;
403
Hans Verkuila7eb9312013-03-18 08:41:13 -0300404 solo_enc->desc_count--;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300405 }
406
407 size -= len;
408 if (size <= 0)
409 break;
410
411 off += len;
412 if (off >= base_size)
413 off -= base_size;
414
415 /* Because we may use two descriptors per loop */
Hans Verkuila7eb9312013-03-18 08:41:13 -0300416 if (solo_enc->desc_count >= (solo_enc->desc_nelts - 1)) {
417 ret = solo_p2m_dma_desc(solo_dev, solo_enc->desc_items,
418 solo_enc->desc_dma,
419 solo_enc->desc_count - 1);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300420 if (ret)
421 return ret;
Hans Verkuila7eb9312013-03-18 08:41:13 -0300422 solo_enc->desc_count = 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400423 }
424 }
425
Hans Verkuila7eb9312013-03-18 08:41:13 -0300426 if (solo_enc->desc_count <= 1)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300427 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400428
Hans Verkuila7eb9312013-03-18 08:41:13 -0300429 return solo_p2m_dma_desc(solo_dev, solo_enc->desc_items, solo_enc->desc_dma,
430 solo_enc->desc_count - 1);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400431}
432
Hans Verkuil382c31a2013-03-15 12:04:14 -0300433static int solo_fill_jpeg(struct solo_enc_dev *solo_enc,
434 struct vb2_buffer *vb, struct vop_header *vh)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400435{
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100436 struct solo_dev *solo_dev = solo_enc->solo_dev;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300437 struct vb2_dma_sg_desc *vbuf = vb2_dma_sg_plane_desc(vb, 0);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300438 int frame_size;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300439 int ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400440
Hans Verkuil382c31a2013-03-15 12:04:14 -0300441 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400442
Hans Verkuil382c31a2013-03-15 12:04:14 -0300443 if (vb2_plane_size(vb, 0) < vh->jpeg_size + solo_enc->jpeg_len)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300444 return -EIO;
445
Hans Verkuil382c31a2013-03-15 12:04:14 -0300446 sg_copy_from_buffer(vbuf->sglist, vbuf->num_pages,
447 solo_enc->jpeg_header,
448 solo_enc->jpeg_len);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300449
450 frame_size = (vh->jpeg_size + solo_enc->jpeg_len + (DMA_ALIGN - 1))
451 & ~(DMA_ALIGN - 1);
Hans Verkuil382c31a2013-03-15 12:04:14 -0300452 vb2_set_plane_payload(vb, 0, vh->jpeg_size + solo_enc->jpeg_len);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300453
Hans Verkuil382c31a2013-03-15 12:04:14 -0300454 dma_map_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
455 DMA_FROM_DEVICE);
456 ret = solo_send_desc(solo_enc, solo_enc->jpeg_len, vbuf, vh->jpeg_off,
457 frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
458 SOLO_JPEG_EXT_SIZE(solo_dev));
459 dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
460 DMA_FROM_DEVICE);
461 return ret;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300462}
463
Hans Verkuil382c31a2013-03-15 12:04:14 -0300464static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
465 struct vb2_buffer *vb, struct vop_header *vh)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300466{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300467 struct solo_dev *solo_dev = solo_enc->solo_dev;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300468 struct vb2_dma_sg_desc *vbuf = vb2_dma_sg_plane_desc(vb, 0);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300469 int frame_off, frame_size;
470 int skip = 0;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300471 int ret;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300472
Hans Verkuil382c31a2013-03-15 12:04:14 -0300473 if (vb2_plane_size(vb, 0) < vh->mpeg_size)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300474 return -EIO;
475
Hans Verkuildcae5da2013-03-25 05:35:17 -0300476 /* If this is a key frame, add extra header */
477 if (!vh->vop_type) {
Hans Verkuil382c31a2013-03-15 12:04:14 -0300478 sg_copy_from_buffer(vbuf->sglist, vbuf->num_pages,
479 solo_enc->vop,
480 solo_enc->vop_len);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300481
482 skip = solo_enc->vop_len;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300483
Hans Verkuil382c31a2013-03-15 12:04:14 -0300484 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_KEYFRAME;
485 vb2_set_plane_payload(vb, 0, vh->mpeg_size + solo_enc->vop_len);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300486 } else {
Hans Verkuil382c31a2013-03-15 12:04:14 -0300487 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
488 vb2_set_plane_payload(vb, 0, vh->mpeg_size);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300489 }
490
491 /* Now get the actual mpeg payload */
492 frame_off = (vh->mpeg_off + sizeof(*vh))
493 % SOLO_MP4E_EXT_SIZE(solo_dev);
494 frame_size = (vh->mpeg_size + skip + (DMA_ALIGN - 1))
495 & ~(DMA_ALIGN - 1);
496
Hans Verkuil382c31a2013-03-15 12:04:14 -0300497 dma_map_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
498 DMA_FROM_DEVICE);
499 ret = solo_send_desc(solo_enc, skip, vbuf, frame_off, frame_size,
500 SOLO_MP4E_EXT_ADDR(solo_dev),
501 SOLO_MP4E_EXT_SIZE(solo_dev));
502 dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sglist, vbuf->num_pages,
503 DMA_FROM_DEVICE);
504 return ret;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300505}
506
Hans Verkuila7eb9312013-03-18 08:41:13 -0300507static int solo_enc_fillbuf(struct solo_enc_dev *solo_enc,
Hans Verkuil382c31a2013-03-15 12:04:14 -0300508 struct vb2_buffer *vb, struct solo_enc_buf *enc_buf)
Hans Verkuildcae5da2013-03-25 05:35:17 -0300509{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300510 struct vop_header *vh = enc_buf->vh;
511 int ret;
512
Hans Verkuildcae5da2013-03-25 05:35:17 -0300513 /* Check for motion flags */
Hans Verkuilf5df0b72013-03-18 08:43:14 -0300514 vb->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_MOTION_ON |
515 V4L2_BUF_FLAG_MOTION_DETECTED);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300516 if (solo_is_motion_on(solo_enc)) {
Hans Verkuil382c31a2013-03-15 12:04:14 -0300517 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_MOTION_ON;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300518 if (enc_buf->motion)
Hans Verkuil382c31a2013-03-15 12:04:14 -0300519 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_MOTION_DETECTED;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300520 }
521
Hans Verkuil9a8bfe92013-03-15 12:53:59 -0300522 if (solo_enc->fmt == V4L2_PIX_FMT_MPEG4)
Hans Verkuil382c31a2013-03-15 12:04:14 -0300523 ret = solo_fill_mpeg(solo_enc, vb, vh);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300524 else
Hans Verkuil382c31a2013-03-15 12:04:14 -0300525 ret = solo_fill_jpeg(solo_enc, vb, vh);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300526
Hans Verkuil382c31a2013-03-15 12:04:14 -0300527 if (!ret) {
Hans Verkuil15513c12013-03-15 13:16:49 -0300528 vb->v4l2_buf.sequence = solo_enc->sequence++;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300529 vb->v4l2_buf.timestamp.tv_sec = vh->sec;
530 vb->v4l2_buf.timestamp.tv_usec = vh->usec;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300531 }
532
Hans Verkuil382c31a2013-03-15 12:04:14 -0300533 vb2_buffer_done(vb, ret ? VB2_BUF_STATE_ERROR : VB2_BUF_STATE_DONE);
534
Hans Verkuildcae5da2013-03-25 05:35:17 -0300535 return ret;
536}
537
538static void solo_enc_handle_one(struct solo_enc_dev *solo_enc,
539 struct solo_enc_buf *enc_buf)
540{
Hans Verkuil382c31a2013-03-15 12:04:14 -0300541 struct solo_vb2_buf *vb;
Hans Verkuila7eb9312013-03-18 08:41:13 -0300542 unsigned long flags;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300543
Hans Verkuil382c31a2013-03-15 12:04:14 -0300544 mutex_lock(&solo_enc->lock);
Hans Verkuila7eb9312013-03-18 08:41:13 -0300545 if (solo_enc->type != enc_buf->type)
546 goto unlock;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300547
Hans Verkuila7eb9312013-03-18 08:41:13 -0300548 spin_lock_irqsave(&solo_enc->av_lock, flags);
Hans Verkuil382c31a2013-03-15 12:04:14 -0300549 if (list_empty(&solo_enc->vidq_active)) {
550 spin_unlock_irqrestore(&solo_enc->av_lock, flags);
551 goto unlock;
552 }
553 vb = list_first_entry(&solo_enc->vidq_active, struct solo_vb2_buf, list);
554 list_del(&vb->list);
Hans Verkuila7eb9312013-03-18 08:41:13 -0300555 spin_unlock_irqrestore(&solo_enc->av_lock, flags);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400556
Hans Verkuil382c31a2013-03-15 12:04:14 -0300557 solo_enc_fillbuf(solo_enc, &vb->vb, enc_buf);
Hans Verkuila7eb9312013-03-18 08:41:13 -0300558unlock:
Hans Verkuil382c31a2013-03-15 12:04:14 -0300559 mutex_unlock(&solo_enc->lock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400560}
561
Hans Verkuildcae5da2013-03-25 05:35:17 -0300562void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400563{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300564 wake_up_interruptible_all(&solo_dev->ring_thread_wait);
565}
566
567static void solo_handle_ring(struct solo_dev *solo_dev)
568{
569 for (;;) {
570 struct solo_enc_dev *solo_enc;
571 struct solo_enc_buf enc_buf;
572 u32 mpeg_current, off;
573 u8 ch;
574 u8 cur_q;
575
576 /* Check if the hardware has any new ones in the queue */
577 cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
578 if (cur_q == solo_dev->enc_idx)
579 break;
580
581 mpeg_current = solo_reg_read(solo_dev,
582 SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
583 solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
584
585 ch = (mpeg_current >> 24) & 0x1f;
586 off = mpeg_current & 0x00ffffff;
587
588 if (ch >= SOLO_MAX_CHANNELS) {
589 ch -= SOLO_MAX_CHANNELS;
590 enc_buf.type = SOLO_ENC_TYPE_EXT;
591 } else
592 enc_buf.type = SOLO_ENC_TYPE_STD;
593
594 solo_enc = solo_dev->v4l2_enc[ch];
595 if (solo_enc == NULL) {
596 dev_err(&solo_dev->pdev->dev,
597 "Got spurious packet for channel %d\n", ch);
598 continue;
599 }
600
601 /* FAIL... */
602 if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
603 sizeof(struct vop_header)))
604 continue;
605
606 enc_buf.vh = (struct vop_header *)solo_dev->vh_buf;
607 enc_buf.vh->mpeg_off -= SOLO_MP4E_EXT_ADDR(solo_dev);
608 enc_buf.vh->jpeg_off -= SOLO_JPEG_EXT_ADDR(solo_dev);
609
610 /* Sanity check */
611 if (enc_buf.vh->mpeg_off != off)
612 continue;
613
614 if (solo_motion_detected(solo_enc))
615 enc_buf.motion = 1;
616 else
617 enc_buf.motion = 0;
618
619 solo_enc_handle_one(solo_enc, &enc_buf);
620 }
621}
622
623static int solo_ring_thread(void *data)
624{
625 struct solo_dev *solo_dev = data;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400626 DECLARE_WAITQUEUE(wait, current);
627
628 set_freezable();
Hans Verkuildcae5da2013-03-25 05:35:17 -0300629 add_wait_queue(&solo_dev->ring_thread_wait, &wait);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400630
631 for (;;) {
632 long timeout = schedule_timeout_interruptible(HZ);
633 if (timeout == -ERESTARTSYS || kthread_should_stop())
634 break;
Hans Verkuildcae5da2013-03-25 05:35:17 -0300635 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
636 solo_handle_ring(solo_dev);
637 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400638 try_to_freeze();
639 }
640
Hans Verkuildcae5da2013-03-25 05:35:17 -0300641 remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400642
Ben Collinsf62de9b2010-11-04 22:51:17 -0400643 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400644}
645
Hans Verkuil382c31a2013-03-15 12:04:14 -0300646static int solo_enc_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
647 unsigned int *num_buffers, unsigned int *num_planes,
648 unsigned int sizes[], void *alloc_ctxs[])
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400649{
Hans Verkuil382c31a2013-03-15 12:04:14 -0300650 sizes[0] = FRAME_BUF_SIZE;
651 *num_planes = 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400652
Hans Verkuil382c31a2013-03-15 12:04:14 -0300653 if (*num_buffers < MIN_VID_BUFFERS)
654 *num_buffers = MIN_VID_BUFFERS;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400655
Ben Collinsf62de9b2010-11-04 22:51:17 -0400656 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400657}
658
Hans Verkuil382c31a2013-03-15 12:04:14 -0300659static void solo_enc_buf_queue(struct vb2_buffer *vb)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400660{
Hans Verkuil382c31a2013-03-15 12:04:14 -0300661 struct vb2_queue *vq = vb->vb2_queue;
662 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
663 struct solo_vb2_buf *solo_vb =
664 container_of(vb, struct solo_vb2_buf, vb);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400665
Hans Verkuil382c31a2013-03-15 12:04:14 -0300666 spin_lock(&solo_enc->av_lock);
667 list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
668 spin_unlock(&solo_enc->av_lock);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400669}
670
Hans Verkuildcae5da2013-03-25 05:35:17 -0300671static int solo_ring_start(struct solo_dev *solo_dev)
672{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300673 solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
674 SOLO6X10_NAME "_ring");
675 if (IS_ERR(solo_dev->ring_thread)) {
676 int err = PTR_ERR(solo_dev->ring_thread);
677 solo_dev->ring_thread = NULL;
678 return err;
679 }
680
681 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
682
683 return 0;
684}
685
686static void solo_ring_stop(struct solo_dev *solo_dev)
687{
Hans Verkuildcae5da2013-03-25 05:35:17 -0300688 if (solo_dev->ring_thread) {
689 kthread_stop(solo_dev->ring_thread);
690 solo_dev->ring_thread = NULL;
691 }
692
693 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
694}
695
Hans Verkuil382c31a2013-03-15 12:04:14 -0300696static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400697{
Hans Verkuil382c31a2013-03-15 12:04:14 -0300698 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300699 int ret;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400700
Hans Verkuila7eb9312013-03-18 08:41:13 -0300701 ret = solo_enc_on(solo_enc);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300702 if (ret)
703 return ret;
Hans Verkuil382c31a2013-03-15 12:04:14 -0300704 return solo_ring_start(solo_enc->solo_dev);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400705}
706
Hans Verkuil382c31a2013-03-15 12:04:14 -0300707static int solo_enc_stop_streaming(struct vb2_queue *q)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400708{
Hans Verkuil382c31a2013-03-15 12:04:14 -0300709 struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300710
Hans Verkuila7eb9312013-03-18 08:41:13 -0300711 solo_enc_off(solo_enc);
Hans Verkuil382c31a2013-03-15 12:04:14 -0300712 INIT_LIST_HEAD(&solo_enc->vidq_active);
713 solo_ring_stop(solo_enc->solo_dev);
714 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400715}
716
Hans Verkuil382c31a2013-03-15 12:04:14 -0300717static struct vb2_ops solo_enc_video_qops = {
718 .queue_setup = solo_enc_queue_setup,
719 .buf_queue = solo_enc_buf_queue,
720 .start_streaming = solo_enc_start_streaming,
721 .stop_streaming = solo_enc_stop_streaming,
722 .wait_prepare = vb2_ops_wait_prepare,
723 .wait_finish = vb2_ops_wait_finish,
724};
725
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400726static int solo_enc_querycap(struct file *file, void *priv,
727 struct v4l2_capability *cap)
728{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300729 struct solo_enc_dev *solo_enc = video_drvdata(file);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100730 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400731
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100732 strcpy(cap->driver, SOLO6X10_NAME);
733 snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400734 solo_enc->ch);
Hans Verkuil20c5f492013-03-12 18:03:20 -0300735 snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400736 pci_name(solo_dev->pdev));
Hans Verkuil20c5f492013-03-12 18:03:20 -0300737 cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
738 V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
739 cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400740 return 0;
741}
742
743static int solo_enc_enum_input(struct file *file, void *priv,
744 struct v4l2_input *input)
745{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300746 struct solo_enc_dev *solo_enc = video_drvdata(file);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100747 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400748
749 if (input->index)
750 return -EINVAL;
751
752 snprintf(input->name, sizeof(input->name), "Encoder %d",
753 solo_enc->ch + 1);
754 input->type = V4L2_INPUT_TYPE_CAMERA;
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300755 input->std = solo_enc->vfd->tvnorms;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400756
757 if (!tw28_get_video_status(solo_dev, solo_enc->ch))
758 input->status = V4L2_IN_ST_NO_SIGNAL;
759
760 return 0;
761}
762
Hans Verkuildcae5da2013-03-25 05:35:17 -0300763static int solo_enc_set_input(struct file *file, void *priv,
764 unsigned int index)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400765{
766 if (index)
767 return -EINVAL;
768
769 return 0;
770}
771
772static int solo_enc_get_input(struct file *file, void *priv,
773 unsigned int *index)
774{
775 *index = 0;
776
777 return 0;
778}
779
780static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
781 struct v4l2_fmtdesc *f)
782{
783 switch (f->index) {
784 case 0:
Hans Verkuil9a8bfe92013-03-15 12:53:59 -0300785 f->pixelformat = V4L2_PIX_FMT_MPEG4;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400786 strcpy(f->description, "MPEG-4 AVC");
787 break;
788 case 1:
789 f->pixelformat = V4L2_PIX_FMT_MJPEG;
790 strcpy(f->description, "MJPEG");
791 break;
792 default:
793 return -EINVAL;
794 }
795
796 f->flags = V4L2_FMT_FLAG_COMPRESSED;
797
798 return 0;
799}
800
801static int solo_enc_try_fmt_cap(struct file *file, void *priv,
802 struct v4l2_format *f)
803{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300804 struct solo_enc_dev *solo_enc = video_drvdata(file);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100805 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400806 struct v4l2_pix_format *pix = &f->fmt.pix;
807
Hans Verkuil9a8bfe92013-03-15 12:53:59 -0300808 if (pix->pixelformat != V4L2_PIX_FMT_MPEG4 &&
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400809 pix->pixelformat != V4L2_PIX_FMT_MJPEG)
810 return -EINVAL;
811
Krzysztof Hałasa98ab1c92011-02-11 13:10:30 +0100812 if (pix->width < solo_dev->video_hsize ||
813 pix->height < solo_dev->video_vsize << 1) {
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400814 /* Default to CIF 1/2 size */
815 pix->width = solo_dev->video_hsize >> 1;
816 pix->height = solo_dev->video_vsize;
Krzysztof Hałasa98ab1c92011-02-11 13:10:30 +0100817 } else {
818 /* Full frame */
819 pix->width = solo_dev->video_hsize;
820 pix->height = solo_dev->video_vsize << 1;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400821 }
822
Hans Verkuil016afda2013-03-12 18:43:21 -0300823 switch (pix->field) {
824 case V4L2_FIELD_NONE:
825 case V4L2_FIELD_INTERLACED:
826 break;
827 case V4L2_FIELD_ANY:
828 default:
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400829 pix->field = V4L2_FIELD_INTERLACED;
Hans Verkuil016afda2013-03-12 18:43:21 -0300830 break;
831 }
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400832
833 /* Just set these */
834 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
835 pix->sizeimage = FRAME_BUF_SIZE;
Hans Verkuil94160492013-03-12 18:47:03 -0300836 pix->bytesperline = 0;
Hans Verkuil016afda2013-03-12 18:43:21 -0300837 pix->priv = 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400838
839 return 0;
840}
841
842static int solo_enc_set_fmt_cap(struct file *file, void *priv,
843 struct v4l2_format *f)
844{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300845 struct solo_enc_dev *solo_enc = video_drvdata(file);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100846 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400847 struct v4l2_pix_format *pix = &f->fmt.pix;
848 int ret;
849
Hans Verkuil382c31a2013-03-15 12:04:14 -0300850 if (vb2_is_busy(&solo_enc->vidq))
851 return -EBUSY;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400852
facugaichafabbe62010-11-10 10:39:33 -0300853 ret = solo_enc_try_fmt_cap(file, priv, f);
Hans Verkuil016afda2013-03-12 18:43:21 -0300854 if (ret)
855 return ret;
856
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400857 if (pix->width == solo_dev->video_hsize)
858 solo_enc->mode = SOLO_ENC_MODE_D1;
859 else
860 solo_enc->mode = SOLO_ENC_MODE_CIF;
861
862 /* This does not change the encoder at all */
Hans Verkuila7eb9312013-03-18 08:41:13 -0300863 solo_enc->fmt = pix->pixelformat;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400864
Hans Verkuil69996872013-03-15 16:35:28 -0300865 /*
866 * More information is needed about these 'extended' types. As far
867 * as I can tell these are basically additional video streams with
868 * different MPEG encoding attributes that can run in parallel with
869 * the main stream. If so, then this should be implemented as a
870 * second video node. Abusing priv like this is certainly not the
871 * right approach.
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400872 if (pix->priv)
Hans Verkuila7eb9312013-03-18 08:41:13 -0300873 solo_enc->type = SOLO_ENC_TYPE_EXT;
Hans Verkuil69996872013-03-15 16:35:28 -0300874 */
Ismael Lucenocdcfe402013-04-12 18:28:33 -0300875 solo_update_mode(solo_enc);
Hans Verkuildcae5da2013-03-25 05:35:17 -0300876 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400877}
878
879static int solo_enc_get_fmt_cap(struct file *file, void *priv,
880 struct v4l2_format *f)
881{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300882 struct solo_enc_dev *solo_enc = video_drvdata(file);
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400883 struct v4l2_pix_format *pix = &f->fmt.pix;
884
885 pix->width = solo_enc->width;
886 pix->height = solo_enc->height;
Hans Verkuila7eb9312013-03-18 08:41:13 -0300887 pix->pixelformat = solo_enc->fmt;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400888 pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
889 V4L2_FIELD_NONE;
890 pix->sizeimage = FRAME_BUF_SIZE;
891 pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil016afda2013-03-12 18:43:21 -0300892 pix->priv = 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400893
894 return 0;
895}
896
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300897static int solo_enc_g_std(struct file *file, void *priv, v4l2_std_id *i)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400898{
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300899 struct solo_enc_dev *solo_enc = video_drvdata(file);
900 struct solo_dev *solo_dev = solo_enc->solo_dev;
901
902 if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
903 *i = V4L2_STD_NTSC_M;
904 else
905 *i = V4L2_STD_PAL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400906 return 0;
907}
908
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300909static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id std)
910{
911 struct solo_enc_dev *solo_enc = video_drvdata(file);
912
913 return solo_set_video_type(solo_enc->solo_dev, std & V4L2_STD_PAL);
914}
915
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400916static int solo_enum_framesizes(struct file *file, void *priv,
917 struct v4l2_frmsizeenum *fsize)
918{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300919 struct solo_enc_dev *solo_enc = video_drvdata(file);
920 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400921
Hans Verkuil9a8bfe92013-03-15 12:53:59 -0300922 if (fsize->pixel_format != V4L2_PIX_FMT_MPEG4 &&
Hans Verkuil016afda2013-03-12 18:43:21 -0300923 fsize->pixel_format != V4L2_PIX_FMT_MJPEG)
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400924 return -EINVAL;
925
926 switch (fsize->index) {
927 case 0:
928 fsize->discrete.width = solo_dev->video_hsize >> 1;
929 fsize->discrete.height = solo_dev->video_vsize;
930 break;
931 case 1:
932 fsize->discrete.width = solo_dev->video_hsize;
933 fsize->discrete.height = solo_dev->video_vsize << 1;
934 break;
935 default:
936 return -EINVAL;
937 }
938
939 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
940
941 return 0;
942}
943
944static int solo_enum_frameintervals(struct file *file, void *priv,
945 struct v4l2_frmivalenum *fintv)
946{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300947 struct solo_enc_dev *solo_enc = video_drvdata(file);
948 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400949
Hans Verkuil9a8bfe92013-03-15 12:53:59 -0300950 if (fintv->pixel_format != V4L2_PIX_FMT_MPEG4 &&
Hans Verkuil016afda2013-03-12 18:43:21 -0300951 fintv->pixel_format != V4L2_PIX_FMT_MJPEG)
952 return -EINVAL;
953 if (fintv->index)
954 return -EINVAL;
955 if ((fintv->width != solo_dev->video_hsize >> 1 ||
956 fintv->height != solo_dev->video_vsize) &&
957 (fintv->width != solo_dev->video_hsize ||
958 fintv->height != solo_dev->video_vsize << 1))
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400959 return -EINVAL;
960
961 fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
962
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300963 fintv->stepwise.min.numerator = 1;
Hans Verkuil016afda2013-03-12 18:43:21 -0300964 fintv->stepwise.min.denominator = solo_dev->fps;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400965
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300966 fintv->stepwise.max.numerator = 15;
Hans Verkuil016afda2013-03-12 18:43:21 -0300967 fintv->stepwise.max.denominator = solo_dev->fps;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400968
969 fintv->stepwise.step.numerator = 1;
Hans Verkuil4c211ed2013-03-15 12:53:17 -0300970 fintv->stepwise.step.denominator = solo_dev->fps;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400971
972 return 0;
973}
974
975static int solo_g_parm(struct file *file, void *priv,
976 struct v4l2_streamparm *sp)
977{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300978 struct solo_enc_dev *solo_enc = video_drvdata(file);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100979 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400980 struct v4l2_captureparm *cp = &sp->parm.capture;
981
982 cp->capability = V4L2_CAP_TIMEPERFRAME;
983 cp->timeperframe.numerator = solo_enc->interval;
984 cp->timeperframe.denominator = solo_dev->fps;
985 cp->capturemode = 0;
986 /* XXX: Shouldn't we be able to get/set this from videobuf? */
987 cp->readbuffers = 2;
988
Ben Collinsf62de9b2010-11-04 22:51:17 -0400989 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400990}
991
992static int solo_s_parm(struct file *file, void *priv,
993 struct v4l2_streamparm *sp)
994{
Hans Verkuila7eb9312013-03-18 08:41:13 -0300995 struct solo_enc_dev *solo_enc = video_drvdata(file);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +0100996 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -0400997 struct v4l2_captureparm *cp = &sp->parm.capture;
998
Hans Verkuil382c31a2013-03-15 12:04:14 -0300999 if (vb2_is_streaming(&solo_enc->vidq))
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001000 return -EBUSY;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001001
1002 if ((cp->timeperframe.numerator == 0) ||
1003 (cp->timeperframe.denominator == 0)) {
1004 /* reset framerate */
1005 cp->timeperframe.numerator = 1;
1006 cp->timeperframe.denominator = solo_dev->fps;
1007 }
1008
1009 if (cp->timeperframe.denominator != solo_dev->fps)
1010 cp->timeperframe.denominator = solo_dev->fps;
1011
1012 if (cp->timeperframe.numerator > 15)
1013 cp->timeperframe.numerator = 15;
1014
1015 solo_enc->interval = cp->timeperframe.numerator;
1016
1017 cp->capability = V4L2_CAP_TIMEPERFRAME;
Hans Verkuil016afda2013-03-12 18:43:21 -03001018 cp->readbuffers = 2;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001019
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001020 solo_update_mode(solo_enc);
Ben Collinsf62de9b2010-11-04 22:51:17 -04001021 return 0;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001022}
1023
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001024static long solo_enc_default(struct file *file, void *fh,
Mauro Carvalho Chehab6d43be72013-03-26 08:04:52 -03001025 bool valid_prio, unsigned int cmd, void *arg)
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001026{
1027 struct solo_enc_dev *solo_enc = video_drvdata(file);
1028 struct solo_dev *solo_dev = solo_enc->solo_dev;
1029 struct solo_motion_thresholds *thresholds = arg;
1030
1031 switch (cmd) {
1032 case SOLO_IOC_G_MOTION_THRESHOLDS:
1033 *thresholds = solo_enc->motion_thresholds;
1034 return 0;
1035
1036 case SOLO_IOC_S_MOTION_THRESHOLDS:
1037 if (!valid_prio)
1038 return -EBUSY;
1039 solo_enc->motion_thresholds = *thresholds;
1040 if (solo_enc->motion_enabled && !solo_enc->motion_global)
1041 return solo_set_motion_block(solo_dev, solo_enc->ch,
1042 &solo_enc->motion_thresholds);
1043 return 0;
1044 default:
1045 return -ENOTTY;
1046 }
1047}
1048
Hans Verkuilc813bd32013-03-25 05:38:14 -03001049static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001050{
Hans Verkuilc813bd32013-03-25 05:38:14 -03001051 struct solo_enc_dev *solo_enc =
1052 container_of(ctrl->handler, struct solo_enc_dev, hdl);
Krzysztof Hałasadecebab2011-02-11 13:38:20 +01001053 struct solo_dev *solo_dev = solo_enc->solo_dev;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001054 int err;
1055
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001056 switch (ctrl->id) {
1057 case V4L2_CID_BRIGHTNESS:
1058 case V4L2_CID_CONTRAST:
1059 case V4L2_CID_SATURATION:
1060 case V4L2_CID_HUE:
1061 case V4L2_CID_SHARPNESS:
1062 return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
Hans Verkuilc813bd32013-03-25 05:38:14 -03001063 ctrl->val);
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001064 case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
Hans Verkuilc813bd32013-03-25 05:38:14 -03001065 solo_enc->gop = ctrl->val;
1066 return 0;
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001067 case V4L2_CID_MOTION_THRESHOLD:
1068 solo_enc->motion_thresh = ctrl->val;
1069 if (!solo_enc->motion_global || !solo_enc->motion_enabled)
1070 return 0;
1071 return solo_set_motion_threshold(solo_dev, solo_enc->ch, ctrl->val);
1072 case V4L2_CID_MOTION_MODE:
1073 solo_enc->motion_global = ctrl->val == 1;
1074 solo_enc->motion_enabled = ctrl->val > 0;
1075 if (ctrl->val) {
1076 if (solo_enc->motion_global)
1077 solo_set_motion_threshold(solo_dev, solo_enc->ch,
1078 solo_enc->motion_thresh);
1079 else
1080 solo_set_motion_block(solo_dev, solo_enc->ch,
1081 &solo_enc->motion_thresholds);
Hans Verkuildcae5da2013-03-25 05:35:17 -03001082 }
Hans Verkuilc813bd32013-03-25 05:38:14 -03001083 solo_motion_toggle(solo_enc, ctrl->val);
1084 return 0;
1085 case V4L2_CID_OSD_TEXT:
Hans Verkuilc813bd32013-03-25 05:38:14 -03001086 strcpy(solo_enc->osd_text, ctrl->string);
1087 err = solo_osd_print(solo_enc);
Hans Verkuilc813bd32013-03-25 05:38:14 -03001088 return err;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001089 default:
1090 return -EINVAL;
1091 }
1092
1093 return 0;
1094}
1095
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001096static const struct v4l2_file_operations solo_enc_fops = {
1097 .owner = THIS_MODULE,
Hans Verkuil382c31a2013-03-15 12:04:14 -03001098 .open = v4l2_fh_open,
1099 .release = vb2_fop_release,
1100 .read = vb2_fop_read,
1101 .poll = vb2_fop_poll,
1102 .mmap = vb2_fop_mmap,
1103 .unlocked_ioctl = video_ioctl2,
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001104};
1105
1106static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
1107 .vidioc_querycap = solo_enc_querycap,
1108 .vidioc_s_std = solo_enc_s_std,
Hans Verkuil4c211ed2013-03-15 12:53:17 -03001109 .vidioc_g_std = solo_enc_g_std,
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001110 /* Input callbacks */
1111 .vidioc_enum_input = solo_enc_enum_input,
1112 .vidioc_s_input = solo_enc_set_input,
1113 .vidioc_g_input = solo_enc_get_input,
1114 /* Video capture format callbacks */
1115 .vidioc_enum_fmt_vid_cap = solo_enc_enum_fmt_cap,
1116 .vidioc_try_fmt_vid_cap = solo_enc_try_fmt_cap,
1117 .vidioc_s_fmt_vid_cap = solo_enc_set_fmt_cap,
1118 .vidioc_g_fmt_vid_cap = solo_enc_get_fmt_cap,
1119 /* Streaming I/O */
Hans Verkuil382c31a2013-03-15 12:04:14 -03001120 .vidioc_reqbufs = vb2_ioctl_reqbufs,
1121 .vidioc_querybuf = vb2_ioctl_querybuf,
1122 .vidioc_qbuf = vb2_ioctl_qbuf,
1123 .vidioc_dqbuf = vb2_ioctl_dqbuf,
1124 .vidioc_streamon = vb2_ioctl_streamon,
1125 .vidioc_streamoff = vb2_ioctl_streamoff,
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001126 /* Frame size and interval */
1127 .vidioc_enum_framesizes = solo_enum_framesizes,
1128 .vidioc_enum_frameintervals = solo_enum_frameintervals,
1129 /* Video capture parameters */
1130 .vidioc_s_parm = solo_s_parm,
1131 .vidioc_g_parm = solo_g_parm,
Hans Verkuil94160492013-03-12 18:47:03 -03001132 /* Logging and events */
1133 .vidioc_log_status = v4l2_ctrl_log_status,
1134 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
1135 .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001136 .vidioc_default = solo_enc_default,
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001137};
1138
Hans Verkuildcae5da2013-03-25 05:35:17 -03001139static const struct video_device solo_enc_template = {
Krzysztof Hałasadecebab2011-02-11 13:38:20 +01001140 .name = SOLO6X10_NAME,
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001141 .fops = &solo_enc_fops,
1142 .ioctl_ops = &solo_enc_ioctl_ops,
1143 .minor = -1,
1144 .release = video_device_release,
Hans Verkuil4c211ed2013-03-15 12:53:17 -03001145 .tvnorms = V4L2_STD_NTSC_M | V4L2_STD_PAL,
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001146};
1147
Hans Verkuilc813bd32013-03-25 05:38:14 -03001148static const struct v4l2_ctrl_ops solo_ctrl_ops = {
1149 .s_ctrl = solo_s_ctrl,
1150};
1151
1152static const struct v4l2_ctrl_config solo_motion_threshold_ctrl = {
1153 .ops = &solo_ctrl_ops,
1154 .id = V4L2_CID_MOTION_THRESHOLD,
1155 .name = "Motion Detection Threshold",
1156 .type = V4L2_CTRL_TYPE_INTEGER,
1157 .max = 0xffff,
1158 .def = SOLO_DEF_MOT_THRESH,
1159 .step = 1,
1160 .flags = V4L2_CTRL_FLAG_SLIDER,
1161};
1162
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001163static const char * const solo_motion_mode_menu[] = {
1164 "Disabled",
1165 "Global Threshold",
1166 "Regional Threshold",
1167 NULL
1168};
1169
Hans Verkuilc813bd32013-03-25 05:38:14 -03001170static const struct v4l2_ctrl_config solo_motion_enable_ctrl = {
1171 .ops = &solo_ctrl_ops,
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001172 .id = V4L2_CID_MOTION_MODE,
1173 .name = "Motion Detection Mode",
1174 .type = V4L2_CTRL_TYPE_MENU,
1175 .qmenu = solo_motion_mode_menu,
1176 .max = 2,
Hans Verkuilc813bd32013-03-25 05:38:14 -03001177};
1178
1179static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
1180 .ops = &solo_ctrl_ops,
1181 .id = V4L2_CID_OSD_TEXT,
1182 .name = "OSD Text",
1183 .type = V4L2_CTRL_TYPE_STRING,
1184 .max = OSD_TEXT_MAX,
1185 .step = 1,
1186};
1187
Hans Verkuildcae5da2013-03-25 05:35:17 -03001188static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1189 u8 ch, unsigned nr)
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001190{
1191 struct solo_enc_dev *solo_enc;
Hans Verkuilc813bd32013-03-25 05:38:14 -03001192 struct v4l2_ctrl_handler *hdl;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001193 int ret;
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001194 int x, y;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001195
1196 solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
1197 if (!solo_enc)
1198 return ERR_PTR(-ENOMEM);
1199
Hans Verkuilc813bd32013-03-25 05:38:14 -03001200 hdl = &solo_enc->hdl;
1201 v4l2_ctrl_handler_init(hdl, 10);
1202 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1203 V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1204 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1205 V4L2_CID_CONTRAST, 0, 255, 1, 128);
1206 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1207 V4L2_CID_SATURATION, 0, 255, 1, 128);
1208 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1209 V4L2_CID_HUE, 0, 255, 1, 128);
1210 if (tw28_has_sharpness(solo_dev, ch))
1211 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1212 V4L2_CID_SHARPNESS, 0, 15, 1, 0);
Hans Verkuilc813bd32013-03-25 05:38:14 -03001213 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1214 V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
1215 v4l2_ctrl_new_custom(hdl, &solo_motion_threshold_ctrl, NULL);
1216 v4l2_ctrl_new_custom(hdl, &solo_motion_enable_ctrl, NULL);
1217 v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
1218 if (hdl->error) {
1219 ret = hdl->error;
Hans Verkuila7eb9312013-03-18 08:41:13 -03001220 goto hdl_free;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001221 }
1222
1223 solo_enc->solo_dev = solo_dev;
1224 solo_enc->ch = ch;
Hans Verkuil382c31a2013-03-15 12:04:14 -03001225 mutex_init(&solo_enc->lock);
Hans Verkuila7eb9312013-03-18 08:41:13 -03001226 spin_lock_init(&solo_enc->av_lock);
1227 INIT_LIST_HEAD(&solo_enc->vidq_active);
Hans Verkuil9a8bfe92013-03-15 12:53:59 -03001228 solo_enc->fmt = V4L2_PIX_FMT_MPEG4;
Hans Verkuila7eb9312013-03-18 08:41:13 -03001229 solo_enc->type = SOLO_ENC_TYPE_STD;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001230
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001231 solo_enc->qp = SOLO_DEFAULT_QP;
Ben Collinsf62de9b2010-11-04 22:51:17 -04001232 solo_enc->gop = solo_dev->fps;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001233 solo_enc->interval = 1;
1234 solo_enc->mode = SOLO_ENC_MODE_CIF;
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001235 solo_enc->motion_global = true;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001236 solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
Hans Verkuilf5df0b72013-03-18 08:43:14 -03001237 for (y = 0; y < SOLO_MOTION_SZ; y++)
1238 for (x = 0; x < SOLO_MOTION_SZ; x++)
1239 solo_enc->motion_thresholds.thresholds[y][x] =
1240 SOLO_DEF_MOT_THRESH;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001241
Hans Verkuil382c31a2013-03-15 12:04:14 -03001242 solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1243 solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1244 solo_enc->vidq.ops = &solo_enc_video_qops;
1245 solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
1246 solo_enc->vidq.drv_priv = solo_enc;
1247 solo_enc->vidq.gfp_flags = __GFP_DMA32;
1248 solo_enc->vidq.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1249 solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
1250 solo_enc->vidq.lock = &solo_enc->lock;
1251 ret = vb2_queue_init(&solo_enc->vidq);
1252 if (ret)
1253 goto hdl_free;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001254 solo_update_mode(solo_enc);
Hans Verkuila7eb9312013-03-18 08:41:13 -03001255
Hans Verkuila7eb9312013-03-18 08:41:13 -03001256 spin_lock_init(&solo_enc->motion_lock);
1257
Hans Verkuildcae5da2013-03-25 05:35:17 -03001258 /* Initialize this per encoder */
1259 solo_enc->jpeg_len = sizeof(jpeg_header);
1260 memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001261
Hans Verkuila7eb9312013-03-18 08:41:13 -03001262 solo_enc->desc_nelts = 32;
1263 solo_enc->desc_items = pci_alloc_consistent(solo_dev->pdev,
1264 sizeof(struct solo_p2m_desc) *
1265 solo_enc->desc_nelts, &solo_enc->desc_dma);
1266 ret = -ENOMEM;
1267 if (solo_enc->desc_items == NULL)
1268 goto hdl_free;
1269
Hans Verkuila7eb9312013-03-18 08:41:13 -03001270 solo_enc->vfd = video_device_alloc();
1271 if (!solo_enc->vfd)
1272 goto pci_free;
1273
1274 *solo_enc->vfd = solo_enc_template;
1275 solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
1276 solo_enc->vfd->ctrl_handler = hdl;
Hans Verkuil382c31a2013-03-15 12:04:14 -03001277 solo_enc->vfd->queue = &solo_enc->vidq;
1278 solo_enc->vfd->lock = &solo_enc->lock;
Hans Verkuila7eb9312013-03-18 08:41:13 -03001279 set_bit(V4L2_FL_USE_FH_PRIO, &solo_enc->vfd->flags);
1280 video_set_drvdata(solo_enc->vfd, solo_enc);
1281 ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER, nr);
1282 if (ret < 0)
1283 goto vdev_release;
1284
1285 snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
1286 "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
1287 solo_enc->vfd->num);
1288
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001289 return solo_enc;
Hans Verkuila7eb9312013-03-18 08:41:13 -03001290
1291vdev_release:
1292 video_device_release(solo_enc->vfd);
1293pci_free:
1294 pci_free_consistent(solo_enc->solo_dev->pdev,
1295 sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
1296 solo_enc->desc_items, solo_enc->desc_dma);
1297hdl_free:
1298 v4l2_ctrl_handler_free(hdl);
1299 kfree(solo_enc);
1300 return ERR_PTR(ret);
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001301}
1302
1303static void solo_enc_free(struct solo_enc_dev *solo_enc)
1304{
1305 if (solo_enc == NULL)
1306 return;
1307
1308 video_unregister_device(solo_enc->vfd);
Hans Verkuilc813bd32013-03-25 05:38:14 -03001309 v4l2_ctrl_handler_free(&solo_enc->hdl);
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001310 kfree(solo_enc);
1311}
1312
Hans Verkuildcae5da2013-03-25 05:35:17 -03001313int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001314{
1315 int i;
1316
Hans Verkuildcae5da2013-03-25 05:35:17 -03001317 init_waitqueue_head(&solo_dev->ring_thread_wait);
1318
1319 solo_dev->vh_size = sizeof(struct vop_header);
1320 solo_dev->vh_buf = pci_alloc_consistent(solo_dev->pdev,
1321 solo_dev->vh_size,
1322 &solo_dev->vh_dma);
1323 if (solo_dev->vh_buf == NULL)
1324 return -ENOMEM;
1325
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001326 for (i = 0; i < solo_dev->nr_chans; i++) {
Hans Verkuildcae5da2013-03-25 05:35:17 -03001327 solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001328 if (IS_ERR(solo_dev->v4l2_enc[i]))
1329 break;
1330 }
1331
1332 if (i != solo_dev->nr_chans) {
1333 int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
1334 while (i--)
1335 solo_enc_free(solo_dev->v4l2_enc[i]);
Hans Verkuildcae5da2013-03-25 05:35:17 -03001336 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1337 solo_dev->vh_buf, solo_dev->vh_dma);
Hans Verkuila7eb9312013-03-18 08:41:13 -03001338 solo_dev->vh_buf = NULL;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001339 return ret;
1340 }
1341
Hans Verkuildcae5da2013-03-25 05:35:17 -03001342 if (solo_dev->type == SOLO_DEV_6010)
1343 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
1344 else
1345 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001346
1347 dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
1348 solo_dev->v4l2_enc[0]->vfd->num,
1349 solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
1350
1351 return 0;
1352}
1353
Krzysztof Hałasadecebab2011-02-11 13:38:20 +01001354void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001355{
1356 int i;
1357
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001358 for (i = 0; i < solo_dev->nr_chans; i++)
1359 solo_enc_free(solo_dev->v4l2_enc[i]);
Hans Verkuildcae5da2013-03-25 05:35:17 -03001360
Hans Verkuila7eb9312013-03-18 08:41:13 -03001361 if (solo_dev->vh_buf)
1362 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
Hans Verkuildcae5da2013-03-25 05:35:17 -03001363 solo_dev->vh_buf, solo_dev->vh_dma);
Ben Collinsfaa4fd22010-06-17 13:27:26 -04001364}