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