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