Merge tag 'hwmon-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/groeck...
[linux-drm-fsl-dcu.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, solo_enc->desc_dma,
403                                  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 = (vop_jpeg_size(vh) + solo_enc->jpeg_len + (DMA_ALIGN - 1))
476                 & ~(DMA_ALIGN - 1);
477         vb2_set_plane_payload(vb, 0, vop_jpeg_size(vh) + solo_enc->jpeg_len);
478
479         /* may discard all previous data in vbuf->sgl */
480         dma_map_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
481                         DMA_FROM_DEVICE);
482         ret = solo_send_desc(solo_enc, solo_enc->jpeg_len, vbuf,
483                              vop_jpeg_offset(vh) - SOLO_JPEG_EXT_ADDR(solo_dev),
484                              frame_size, SOLO_JPEG_EXT_ADDR(solo_dev),
485                              SOLO_JPEG_EXT_SIZE(solo_dev));
486         dma_unmap_sg(&solo_dev->pdev->dev, vbuf->sgl, vbuf->nents,
487                         DMA_FROM_DEVICE);
488
489         /* add the header only after dma_unmap_sg() */
490         sg_copy_from_buffer(vbuf->sgl, vbuf->nents,
491                             solo_enc->jpeg_header, solo_enc->jpeg_len);
492
493         return ret;
494 }
495
496 static int solo_fill_mpeg(struct solo_enc_dev *solo_enc,
497                 struct vb2_buffer *vb, const vop_header *vh)
498 {
499         struct solo_dev *solo_dev = solo_enc->solo_dev;
500         struct sg_table *vbuf = vb2_dma_sg_plane_desc(vb, 0);
501         int frame_off, frame_size;
502         int skip = 0;
503         int ret;
504
505         if (vb2_plane_size(vb, 0) < vop_mpeg_size(vh))
506                 return -EIO;
507
508         /* If this is a key frame, add extra header */
509         vb->v4l2_buf.flags &= ~(V4L2_BUF_FLAG_KEYFRAME | V4L2_BUF_FLAG_PFRAME | 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) + solo_enc->vop_len);
514         } else {
515                 vb->v4l2_buf.flags |= V4L2_BUF_FLAG_PFRAME;
516                 vb2_set_plane_payload(vb, 0, vop_mpeg_size(vh));
517         }
518
519         /* Now get the actual mpeg payload */
520         frame_off = (vop_mpeg_offset(vh) - SOLO_MP4E_EXT_ADDR(solo_dev) + sizeof(*vh))
521                 % SOLO_MP4E_EXT_SIZE(solo_dev);
522         frame_size = (vop_mpeg_size(vh) + skip + (DMA_ALIGN - 1))
523                 & ~(DMA_ALIGN - 1);
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, list);
593         list_del(&vb->list);
594         spin_unlock_irqrestore(&solo_enc->av_lock, flags);
595
596         solo_enc_fillbuf(solo_enc, &vb->vb, enc_buf);
597 unlock:
598         mutex_unlock(&solo_enc->lock);
599 }
600
601 void solo_enc_v4l2_isr(struct solo_dev *solo_dev)
602 {
603         wake_up_interruptible_all(&solo_dev->ring_thread_wait);
604 }
605
606 static void solo_handle_ring(struct solo_dev *solo_dev)
607 {
608         for (;;) {
609                 struct solo_enc_dev *solo_enc;
610                 struct solo_enc_buf enc_buf;
611                 u32 mpeg_current, off;
612                 u8 ch;
613                 u8 cur_q;
614
615                 /* Check if the hardware has any new ones in the queue */
616                 cur_q = solo_reg_read(solo_dev, SOLO_VE_STATE(11)) & 0xff;
617                 if (cur_q == solo_dev->enc_idx)
618                         break;
619
620                 mpeg_current = solo_reg_read(solo_dev,
621                                         SOLO_VE_MPEG4_QUE(solo_dev->enc_idx));
622                 solo_dev->enc_idx = (solo_dev->enc_idx + 1) % MP4_QS;
623
624                 ch = (mpeg_current >> 24) & 0x1f;
625                 off = mpeg_current & 0x00ffffff;
626
627                 if (ch >= SOLO_MAX_CHANNELS) {
628                         ch -= SOLO_MAX_CHANNELS;
629                         enc_buf.type = SOLO_ENC_TYPE_EXT;
630                 } else
631                         enc_buf.type = SOLO_ENC_TYPE_STD;
632
633                 solo_enc = solo_dev->v4l2_enc[ch];
634                 if (solo_enc == NULL) {
635                         dev_err(&solo_dev->pdev->dev,
636                                 "Got spurious packet for channel %d\n", ch);
637                         continue;
638                 }
639
640                 /* FAIL... */
641                 if (enc_get_mpeg_dma(solo_dev, solo_dev->vh_dma, off,
642                                      sizeof(vop_header)))
643                         continue;
644
645                 enc_buf.vh = solo_dev->vh_buf;
646
647                 /* Sanity check */
648                 if (vop_mpeg_offset(enc_buf.vh) != SOLO_MP4E_EXT_ADDR(solo_dev) + off)
649                         continue;
650
651                 if (solo_motion_detected(solo_enc))
652                         enc_buf.motion = 1;
653                 else
654                         enc_buf.motion = 0;
655
656                 solo_enc_handle_one(solo_enc, &enc_buf);
657         }
658 }
659
660 static int solo_ring_thread(void *data)
661 {
662         struct solo_dev *solo_dev = data;
663         DECLARE_WAITQUEUE(wait, current);
664
665         set_freezable();
666         add_wait_queue(&solo_dev->ring_thread_wait, &wait);
667
668         for (;;) {
669                 long timeout = schedule_timeout_interruptible(HZ);
670                 if (timeout == -ERESTARTSYS || kthread_should_stop())
671                         break;
672                 solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
673                 solo_handle_ring(solo_dev);
674                 solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
675                 try_to_freeze();
676         }
677
678         remove_wait_queue(&solo_dev->ring_thread_wait, &wait);
679
680         return 0;
681 }
682
683 static int solo_enc_queue_setup(struct vb2_queue *q, const struct v4l2_format *fmt,
684                            unsigned int *num_buffers, unsigned int *num_planes,
685                            unsigned int sizes[], void *alloc_ctxs[])
686 {
687         sizes[0] = FRAME_BUF_SIZE;
688         *num_planes = 1;
689
690         if (*num_buffers < MIN_VID_BUFFERS)
691                 *num_buffers = MIN_VID_BUFFERS;
692
693         return 0;
694 }
695
696 static void solo_enc_buf_queue(struct vb2_buffer *vb)
697 {
698         struct vb2_queue *vq = vb->vb2_queue;
699         struct solo_enc_dev *solo_enc = vb2_get_drv_priv(vq);
700         struct solo_vb2_buf *solo_vb =
701                 container_of(vb, struct solo_vb2_buf, vb);
702
703         spin_lock(&solo_enc->av_lock);
704         list_add_tail(&solo_vb->list, &solo_enc->vidq_active);
705         spin_unlock(&solo_enc->av_lock);
706 }
707
708 static int solo_ring_start(struct solo_dev *solo_dev)
709 {
710         solo_dev->ring_thread = kthread_run(solo_ring_thread, solo_dev,
711                                             SOLO6X10_NAME "_ring");
712         if (IS_ERR(solo_dev->ring_thread)) {
713                 int err = PTR_ERR(solo_dev->ring_thread);
714                 solo_dev->ring_thread = NULL;
715                 return err;
716         }
717
718         solo_irq_on(solo_dev, SOLO_IRQ_ENCODER);
719
720         return 0;
721 }
722
723 static void solo_ring_stop(struct solo_dev *solo_dev)
724 {
725         if (solo_dev->ring_thread) {
726                 kthread_stop(solo_dev->ring_thread);
727                 solo_dev->ring_thread = NULL;
728         }
729
730         solo_irq_off(solo_dev, SOLO_IRQ_ENCODER);
731 }
732
733 static int solo_enc_start_streaming(struct vb2_queue *q, unsigned int count)
734 {
735         struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
736         int ret;
737
738         ret = solo_enc_on(solo_enc);
739         if (ret)
740                 return ret;
741         return solo_ring_start(solo_enc->solo_dev);
742 }
743
744 static int solo_enc_stop_streaming(struct vb2_queue *q)
745 {
746         struct solo_enc_dev *solo_enc = vb2_get_drv_priv(q);
747
748         solo_enc_off(solo_enc);
749         INIT_LIST_HEAD(&solo_enc->vidq_active);
750         solo_ring_stop(solo_enc->solo_dev);
751         return 0;
752 }
753
754 static struct vb2_ops solo_enc_video_qops = {
755         .queue_setup    = solo_enc_queue_setup,
756         .buf_queue      = solo_enc_buf_queue,
757         .start_streaming = solo_enc_start_streaming,
758         .stop_streaming = solo_enc_stop_streaming,
759         .wait_prepare   = vb2_ops_wait_prepare,
760         .wait_finish    = vb2_ops_wait_finish,
761 };
762
763 static int solo_enc_querycap(struct file *file, void  *priv,
764                              struct v4l2_capability *cap)
765 {
766         struct solo_enc_dev *solo_enc = video_drvdata(file);
767         struct solo_dev *solo_dev = solo_enc->solo_dev;
768
769         strcpy(cap->driver, SOLO6X10_NAME);
770         snprintf(cap->card, sizeof(cap->card), "Softlogic 6x10 Enc %d",
771                  solo_enc->ch);
772         snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
773                  pci_name(solo_dev->pdev));
774         cap->device_caps = V4L2_CAP_VIDEO_CAPTURE |
775                         V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
776         cap->capabilities = cap->device_caps | V4L2_CAP_DEVICE_CAPS;
777         return 0;
778 }
779
780 static int solo_enc_enum_input(struct file *file, void *priv,
781                                struct v4l2_input *input)
782 {
783         struct solo_enc_dev *solo_enc = video_drvdata(file);
784         struct solo_dev *solo_dev = solo_enc->solo_dev;
785
786         if (input->index)
787                 return -EINVAL;
788
789         snprintf(input->name, sizeof(input->name), "Encoder %d",
790                  solo_enc->ch + 1);
791         input->type = V4L2_INPUT_TYPE_CAMERA;
792         input->std = solo_enc->vfd->tvnorms;
793
794         if (!tw28_get_video_status(solo_dev, solo_enc->ch))
795                 input->status = V4L2_IN_ST_NO_SIGNAL;
796
797         return 0;
798 }
799
800 static int solo_enc_set_input(struct file *file, void *priv,
801                               unsigned int index)
802 {
803         if (index)
804                 return -EINVAL;
805
806         return 0;
807 }
808
809 static int solo_enc_get_input(struct file *file, void *priv,
810                               unsigned int *index)
811 {
812         *index = 0;
813
814         return 0;
815 }
816
817 static int solo_enc_enum_fmt_cap(struct file *file, void *priv,
818                                  struct v4l2_fmtdesc *f)
819 {
820         struct solo_enc_dev *solo_enc = video_drvdata(file);
821         int dev_type = solo_enc->solo_dev->type;
822
823         switch (f->index) {
824         case 0:
825                 switch (dev_type) {
826                 case SOLO_DEV_6010:
827                         f->pixelformat = V4L2_PIX_FMT_MPEG4;
828                         strcpy(f->description, "MPEG-4 part 2");
829                         break;
830                 case SOLO_DEV_6110:
831                         f->pixelformat = V4L2_PIX_FMT_H264;
832                         strcpy(f->description, "H.264");
833                         break;
834                 }
835                 break;
836         case 1:
837                 f->pixelformat = V4L2_PIX_FMT_MJPEG;
838                 strcpy(f->description, "MJPEG");
839                 break;
840         default:
841                 return -EINVAL;
842         }
843
844         f->flags = V4L2_FMT_FLAG_COMPRESSED;
845
846         return 0;
847 }
848
849 static inline int solo_valid_pixfmt(u32 pixfmt, int dev_type)
850 {
851         return (pixfmt == V4L2_PIX_FMT_H264 && dev_type == SOLO_DEV_6110)
852                 || (pixfmt == V4L2_PIX_FMT_MPEG4 && dev_type == SOLO_DEV_6010)
853                 || pixfmt == V4L2_PIX_FMT_MJPEG ? 0 : -EINVAL;
854 }
855
856 static int solo_enc_try_fmt_cap(struct file *file, void *priv,
857                             struct v4l2_format *f)
858 {
859         struct solo_enc_dev *solo_enc = video_drvdata(file);
860         struct solo_dev *solo_dev = solo_enc->solo_dev;
861         struct v4l2_pix_format *pix = &f->fmt.pix;
862
863         if (solo_valid_pixfmt(pix->pixelformat, solo_dev->type))
864                 return -EINVAL;
865
866         if (pix->width < solo_dev->video_hsize ||
867             pix->height < solo_dev->video_vsize << 1) {
868                 /* Default to CIF 1/2 size */
869                 pix->width = solo_dev->video_hsize >> 1;
870                 pix->height = solo_dev->video_vsize;
871         } else {
872                 /* Full frame */
873                 pix->width = solo_dev->video_hsize;
874                 pix->height = solo_dev->video_vsize << 1;
875         }
876
877         switch (pix->field) {
878         case V4L2_FIELD_NONE:
879         case V4L2_FIELD_INTERLACED:
880                 break;
881         case V4L2_FIELD_ANY:
882         default:
883                 pix->field = V4L2_FIELD_INTERLACED;
884                 break;
885         }
886
887         /* Just set these */
888         pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
889         pix->sizeimage = FRAME_BUF_SIZE;
890         pix->bytesperline = 0;
891         pix->priv = 0;
892
893         return 0;
894 }
895
896 static int solo_enc_set_fmt_cap(struct file *file, void *priv,
897                                 struct v4l2_format *f)
898 {
899         struct solo_enc_dev *solo_enc = video_drvdata(file);
900         struct solo_dev *solo_dev = solo_enc->solo_dev;
901         struct v4l2_pix_format *pix = &f->fmt.pix;
902         int ret;
903
904         if (vb2_is_busy(&solo_enc->vidq))
905                 return -EBUSY;
906
907         ret = solo_enc_try_fmt_cap(file, priv, f);
908         if (ret)
909                 return ret;
910
911         if (pix->width == solo_dev->video_hsize)
912                 solo_enc->mode = SOLO_ENC_MODE_D1;
913         else
914                 solo_enc->mode = SOLO_ENC_MODE_CIF;
915
916         /* This does not change the encoder at all */
917         solo_enc->fmt = pix->pixelformat;
918
919         /*
920          * More information is needed about these 'extended' types. As far
921          * as I can tell these are basically additional video streams with
922          * different MPEG encoding attributes that can run in parallel with
923          * the main stream. If so, then this should be implemented as a
924          * second video node. Abusing priv like this is certainly not the
925          * right approach.
926         if (pix->priv)
927                 solo_enc->type = SOLO_ENC_TYPE_EXT;
928          */
929         solo_update_mode(solo_enc);
930         return 0;
931 }
932
933 static int solo_enc_get_fmt_cap(struct file *file, void *priv,
934                                 struct v4l2_format *f)
935 {
936         struct solo_enc_dev *solo_enc = video_drvdata(file);
937         struct v4l2_pix_format *pix = &f->fmt.pix;
938
939         pix->width = solo_enc->width;
940         pix->height = solo_enc->height;
941         pix->pixelformat = solo_enc->fmt;
942         pix->field = solo_enc->interlaced ? V4L2_FIELD_INTERLACED :
943                      V4L2_FIELD_NONE;
944         pix->sizeimage = FRAME_BUF_SIZE;
945         pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
946         pix->priv = 0;
947
948         return 0;
949 }
950
951 static int solo_enc_g_std(struct file *file, void *priv, v4l2_std_id *i)
952 {
953         struct solo_enc_dev *solo_enc = video_drvdata(file);
954         struct solo_dev *solo_dev = solo_enc->solo_dev;
955
956         if (solo_dev->video_type == SOLO_VO_FMT_TYPE_NTSC)
957                 *i = V4L2_STD_NTSC_M;
958         else
959                 *i = V4L2_STD_PAL;
960         return 0;
961 }
962
963 static int solo_enc_s_std(struct file *file, void *priv, v4l2_std_id std)
964 {
965         struct solo_enc_dev *solo_enc = video_drvdata(file);
966
967         return solo_set_video_type(solo_enc->solo_dev, std & V4L2_STD_PAL);
968 }
969
970 static int solo_enum_framesizes(struct file *file, void *priv,
971                                 struct v4l2_frmsizeenum *fsize)
972 {
973         struct solo_enc_dev *solo_enc = video_drvdata(file);
974         struct solo_dev *solo_dev = solo_enc->solo_dev;
975
976         if (solo_valid_pixfmt(fsize->pixel_format, solo_dev->type))
977                 return -EINVAL;
978
979         switch (fsize->index) {
980         case 0:
981                 fsize->discrete.width = solo_dev->video_hsize >> 1;
982                 fsize->discrete.height = solo_dev->video_vsize;
983                 break;
984         case 1:
985                 fsize->discrete.width = solo_dev->video_hsize;
986                 fsize->discrete.height = solo_dev->video_vsize << 1;
987                 break;
988         default:
989                 return -EINVAL;
990         }
991
992         fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
993
994         return 0;
995 }
996
997 static int solo_enum_frameintervals(struct file *file, void *priv,
998                                     struct v4l2_frmivalenum *fintv)
999 {
1000         struct solo_enc_dev *solo_enc = video_drvdata(file);
1001         struct solo_dev *solo_dev = solo_enc->solo_dev;
1002
1003         if (solo_valid_pixfmt(fintv->pixel_format, solo_dev->type))
1004                 return -EINVAL;
1005         if (fintv->index)
1006                 return -EINVAL;
1007         if ((fintv->width != solo_dev->video_hsize >> 1 ||
1008              fintv->height != solo_dev->video_vsize) &&
1009             (fintv->width != solo_dev->video_hsize ||
1010              fintv->height != solo_dev->video_vsize << 1))
1011                 return -EINVAL;
1012
1013         fintv->type = V4L2_FRMIVAL_TYPE_STEPWISE;
1014
1015         fintv->stepwise.min.numerator = 1;
1016         fintv->stepwise.min.denominator = solo_dev->fps;
1017
1018         fintv->stepwise.max.numerator = 15;
1019         fintv->stepwise.max.denominator = solo_dev->fps;
1020
1021         fintv->stepwise.step.numerator = 1;
1022         fintv->stepwise.step.denominator = solo_dev->fps;
1023
1024         return 0;
1025 }
1026
1027 static int solo_g_parm(struct file *file, void *priv,
1028                        struct v4l2_streamparm *sp)
1029 {
1030         struct solo_enc_dev *solo_enc = video_drvdata(file);
1031         struct v4l2_captureparm *cp = &sp->parm.capture;
1032
1033         cp->capability = V4L2_CAP_TIMEPERFRAME;
1034         cp->timeperframe.numerator = solo_enc->interval;
1035         cp->timeperframe.denominator = solo_enc->solo_dev->fps;
1036         cp->capturemode = 0;
1037         /* XXX: Shouldn't we be able to get/set this from videobuf? */
1038         cp->readbuffers = 2;
1039
1040         return 0;
1041 }
1042
1043 static inline int calc_interval(u8 fps, u32 n, u32 d)
1044 {
1045         if (!n || !d)
1046                 return 1;
1047         if (d == fps)
1048                 return n;
1049         n *= fps;
1050         return min(15U, n / d + (n % d >= (fps >> 1)));
1051 }
1052
1053 static int solo_s_parm(struct file *file, void *priv,
1054                        struct v4l2_streamparm *sp)
1055 {
1056         struct solo_enc_dev *solo_enc = video_drvdata(file);
1057         struct v4l2_fract *t = &sp->parm.capture.timeperframe;
1058         u8 fps = solo_enc->solo_dev->fps;
1059
1060         if (vb2_is_streaming(&solo_enc->vidq))
1061                 return -EBUSY;
1062
1063         solo_enc->interval = calc_interval(fps, t->numerator, t->denominator);
1064         solo_update_mode(solo_enc);
1065         return solo_g_parm(file, priv, sp);
1066 }
1067
1068 static long solo_enc_default(struct file *file, void *fh,
1069                         bool valid_prio, unsigned int cmd, void *arg)
1070 {
1071         struct solo_enc_dev *solo_enc = video_drvdata(file);
1072         struct solo_dev *solo_dev = solo_enc->solo_dev;
1073         struct solo_motion_thresholds *thresholds = arg;
1074
1075         switch (cmd) {
1076         case SOLO_IOC_G_MOTION_THRESHOLDS:
1077                 *thresholds = solo_enc->motion_thresholds;
1078                 return 0;
1079
1080         case SOLO_IOC_S_MOTION_THRESHOLDS:
1081                 if (!valid_prio)
1082                         return -EBUSY;
1083                 solo_enc->motion_thresholds = *thresholds;
1084                 if (solo_enc->motion_enabled && !solo_enc->motion_global)
1085                         return solo_set_motion_block(solo_dev, solo_enc->ch,
1086                                                 &solo_enc->motion_thresholds);
1087                 return 0;
1088         default:
1089                 return -ENOTTY;
1090         }
1091 }
1092
1093 static int solo_s_ctrl(struct v4l2_ctrl *ctrl)
1094 {
1095         struct solo_enc_dev *solo_enc =
1096                 container_of(ctrl->handler, struct solo_enc_dev, hdl);
1097         struct solo_dev *solo_dev = solo_enc->solo_dev;
1098         int err;
1099
1100         switch (ctrl->id) {
1101         case V4L2_CID_BRIGHTNESS:
1102         case V4L2_CID_CONTRAST:
1103         case V4L2_CID_SATURATION:
1104         case V4L2_CID_HUE:
1105         case V4L2_CID_SHARPNESS:
1106                 return tw28_set_ctrl_val(solo_dev, ctrl->id, solo_enc->ch,
1107                                          ctrl->val);
1108         case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1109                 solo_enc->gop = ctrl->val;
1110                 return 0;
1111         case V4L2_CID_MOTION_THRESHOLD:
1112                 solo_enc->motion_thresh = ctrl->val;
1113                 if (!solo_enc->motion_global || !solo_enc->motion_enabled)
1114                         return 0;
1115                 return solo_set_motion_threshold(solo_dev, solo_enc->ch, ctrl->val);
1116         case V4L2_CID_MOTION_MODE:
1117                 solo_enc->motion_global = ctrl->val == 1;
1118                 solo_enc->motion_enabled = ctrl->val > 0;
1119                 if (ctrl->val) {
1120                         if (solo_enc->motion_global)
1121                                 solo_set_motion_threshold(solo_dev, solo_enc->ch,
1122                                                 solo_enc->motion_thresh);
1123                         else
1124                                 solo_set_motion_block(solo_dev, solo_enc->ch,
1125                                                 &solo_enc->motion_thresholds);
1126                 }
1127                 solo_motion_toggle(solo_enc, ctrl->val);
1128                 return 0;
1129         case V4L2_CID_OSD_TEXT:
1130                 strcpy(solo_enc->osd_text, ctrl->string);
1131                 err = solo_osd_print(solo_enc);
1132                 return err;
1133         default:
1134                 return -EINVAL;
1135         }
1136
1137         return 0;
1138 }
1139
1140 static const struct v4l2_file_operations solo_enc_fops = {
1141         .owner                  = THIS_MODULE,
1142         .open                   = v4l2_fh_open,
1143         .release                = vb2_fop_release,
1144         .read                   = vb2_fop_read,
1145         .poll                   = vb2_fop_poll,
1146         .mmap                   = vb2_fop_mmap,
1147         .unlocked_ioctl         = video_ioctl2,
1148 };
1149
1150 static const struct v4l2_ioctl_ops solo_enc_ioctl_ops = {
1151         .vidioc_querycap                = solo_enc_querycap,
1152         .vidioc_s_std                   = solo_enc_s_std,
1153         .vidioc_g_std                   = solo_enc_g_std,
1154         /* Input callbacks */
1155         .vidioc_enum_input              = solo_enc_enum_input,
1156         .vidioc_s_input                 = solo_enc_set_input,
1157         .vidioc_g_input                 = solo_enc_get_input,
1158         /* Video capture format callbacks */
1159         .vidioc_enum_fmt_vid_cap        = solo_enc_enum_fmt_cap,
1160         .vidioc_try_fmt_vid_cap         = solo_enc_try_fmt_cap,
1161         .vidioc_s_fmt_vid_cap           = solo_enc_set_fmt_cap,
1162         .vidioc_g_fmt_vid_cap           = solo_enc_get_fmt_cap,
1163         /* Streaming I/O */
1164         .vidioc_reqbufs                 = vb2_ioctl_reqbufs,
1165         .vidioc_querybuf                = vb2_ioctl_querybuf,
1166         .vidioc_qbuf                    = vb2_ioctl_qbuf,
1167         .vidioc_dqbuf                   = vb2_ioctl_dqbuf,
1168         .vidioc_streamon                = vb2_ioctl_streamon,
1169         .vidioc_streamoff               = vb2_ioctl_streamoff,
1170         /* Frame size and interval */
1171         .vidioc_enum_framesizes         = solo_enum_framesizes,
1172         .vidioc_enum_frameintervals     = solo_enum_frameintervals,
1173         /* Video capture parameters */
1174         .vidioc_s_parm                  = solo_s_parm,
1175         .vidioc_g_parm                  = solo_g_parm,
1176         /* Logging and events */
1177         .vidioc_log_status              = v4l2_ctrl_log_status,
1178         .vidioc_subscribe_event         = v4l2_ctrl_subscribe_event,
1179         .vidioc_unsubscribe_event       = v4l2_event_unsubscribe,
1180         .vidioc_default                 = solo_enc_default,
1181 };
1182
1183 static const struct video_device solo_enc_template = {
1184         .name                   = SOLO6X10_NAME,
1185         .fops                   = &solo_enc_fops,
1186         .ioctl_ops              = &solo_enc_ioctl_ops,
1187         .minor                  = -1,
1188         .release                = video_device_release,
1189         .tvnorms                = V4L2_STD_NTSC_M | V4L2_STD_PAL,
1190 };
1191
1192 static const struct v4l2_ctrl_ops solo_ctrl_ops = {
1193         .s_ctrl = solo_s_ctrl,
1194 };
1195
1196 static const struct v4l2_ctrl_config solo_motion_threshold_ctrl = {
1197         .ops = &solo_ctrl_ops,
1198         .id = V4L2_CID_MOTION_THRESHOLD,
1199         .name = "Motion Detection Threshold",
1200         .type = V4L2_CTRL_TYPE_INTEGER,
1201         .max = 0xffff,
1202         .def = SOLO_DEF_MOT_THRESH,
1203         .step = 1,
1204         .flags = V4L2_CTRL_FLAG_SLIDER,
1205 };
1206
1207 static const char * const solo_motion_mode_menu[] = {
1208         "Disabled",
1209         "Global Threshold",
1210         "Regional Threshold",
1211         NULL
1212 };
1213
1214 static const struct v4l2_ctrl_config solo_motion_enable_ctrl = {
1215         .ops = &solo_ctrl_ops,
1216         .id = V4L2_CID_MOTION_MODE,
1217         .name = "Motion Detection Mode",
1218         .type = V4L2_CTRL_TYPE_MENU,
1219         .qmenu = solo_motion_mode_menu,
1220         .max = 2,
1221 };
1222
1223 static const struct v4l2_ctrl_config solo_osd_text_ctrl = {
1224         .ops = &solo_ctrl_ops,
1225         .id = V4L2_CID_OSD_TEXT,
1226         .name = "OSD Text",
1227         .type = V4L2_CTRL_TYPE_STRING,
1228         .max = OSD_TEXT_MAX,
1229         .step = 1,
1230 };
1231
1232 static struct solo_enc_dev *solo_enc_alloc(struct solo_dev *solo_dev,
1233                                            u8 ch, unsigned nr)
1234 {
1235         struct solo_enc_dev *solo_enc;
1236         struct v4l2_ctrl_handler *hdl;
1237         int ret;
1238         int x, y;
1239
1240         solo_enc = kzalloc(sizeof(*solo_enc), GFP_KERNEL);
1241         if (!solo_enc)
1242                 return ERR_PTR(-ENOMEM);
1243
1244         hdl = &solo_enc->hdl;
1245         v4l2_ctrl_handler_init(hdl, 10);
1246         v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1247                         V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
1248         v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1249                         V4L2_CID_CONTRAST, 0, 255, 1, 128);
1250         v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1251                         V4L2_CID_SATURATION, 0, 255, 1, 128);
1252         v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1253                         V4L2_CID_HUE, 0, 255, 1, 128);
1254         if (tw28_has_sharpness(solo_dev, ch))
1255                 v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1256                         V4L2_CID_SHARPNESS, 0, 15, 1, 0);
1257         v4l2_ctrl_new_std(hdl, &solo_ctrl_ops,
1258                         V4L2_CID_MPEG_VIDEO_GOP_SIZE, 1, 255, 1, solo_dev->fps);
1259         v4l2_ctrl_new_custom(hdl, &solo_motion_threshold_ctrl, NULL);
1260         v4l2_ctrl_new_custom(hdl, &solo_motion_enable_ctrl, NULL);
1261         v4l2_ctrl_new_custom(hdl, &solo_osd_text_ctrl, NULL);
1262         if (hdl->error) {
1263                 ret = hdl->error;
1264                 goto hdl_free;
1265         }
1266
1267         solo_enc->solo_dev = solo_dev;
1268         solo_enc->ch = ch;
1269         mutex_init(&solo_enc->lock);
1270         spin_lock_init(&solo_enc->av_lock);
1271         INIT_LIST_HEAD(&solo_enc->vidq_active);
1272         solo_enc->fmt = (solo_dev->type == SOLO_DEV_6010) ?
1273                 V4L2_PIX_FMT_MPEG4 : V4L2_PIX_FMT_H264;
1274         solo_enc->type = SOLO_ENC_TYPE_STD;
1275
1276         solo_enc->qp = SOLO_DEFAULT_QP;
1277         solo_enc->gop = solo_dev->fps;
1278         solo_enc->interval = 1;
1279         solo_enc->mode = SOLO_ENC_MODE_CIF;
1280         solo_enc->motion_global = true;
1281         solo_enc->motion_thresh = SOLO_DEF_MOT_THRESH;
1282         for (y = 0; y < SOLO_MOTION_SZ; y++)
1283                 for (x = 0; x < SOLO_MOTION_SZ; x++)
1284                         solo_enc->motion_thresholds.thresholds[y][x] =
1285                                                         SOLO_DEF_MOT_THRESH;
1286
1287         solo_enc->vidq.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1288         solo_enc->vidq.io_modes = VB2_MMAP | VB2_USERPTR | VB2_READ;
1289         solo_enc->vidq.ops = &solo_enc_video_qops;
1290         solo_enc->vidq.mem_ops = &vb2_dma_sg_memops;
1291         solo_enc->vidq.drv_priv = solo_enc;
1292         solo_enc->vidq.gfp_flags = __GFP_DMA32;
1293         solo_enc->vidq.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1294         solo_enc->vidq.buf_struct_size = sizeof(struct solo_vb2_buf);
1295         solo_enc->vidq.lock = &solo_enc->lock;
1296         ret = vb2_queue_init(&solo_enc->vidq);
1297         if (ret)
1298                 goto hdl_free;
1299         solo_update_mode(solo_enc);
1300
1301         spin_lock_init(&solo_enc->motion_lock);
1302
1303         /* Initialize this per encoder */
1304         solo_enc->jpeg_len = sizeof(jpeg_header);
1305         memcpy(solo_enc->jpeg_header, jpeg_header, solo_enc->jpeg_len);
1306
1307         solo_enc->desc_nelts = 32;
1308         solo_enc->desc_items = pci_alloc_consistent(solo_dev->pdev,
1309                                       sizeof(struct solo_p2m_desc) *
1310                                       solo_enc->desc_nelts, &solo_enc->desc_dma);
1311         ret = -ENOMEM;
1312         if (solo_enc->desc_items == NULL)
1313                 goto hdl_free;
1314
1315         solo_enc->vfd = video_device_alloc();
1316         if (!solo_enc->vfd)
1317                 goto pci_free;
1318
1319         *solo_enc->vfd = solo_enc_template;
1320         solo_enc->vfd->v4l2_dev = &solo_dev->v4l2_dev;
1321         solo_enc->vfd->ctrl_handler = hdl;
1322         solo_enc->vfd->queue = &solo_enc->vidq;
1323         solo_enc->vfd->lock = &solo_enc->lock;
1324         set_bit(V4L2_FL_USE_FH_PRIO, &solo_enc->vfd->flags);
1325         video_set_drvdata(solo_enc->vfd, solo_enc);
1326         ret = video_register_device(solo_enc->vfd, VFL_TYPE_GRABBER, nr);
1327         if (ret < 0)
1328                 goto vdev_release;
1329
1330         snprintf(solo_enc->vfd->name, sizeof(solo_enc->vfd->name),
1331                  "%s-enc (%i/%i)", SOLO6X10_NAME, solo_dev->vfd->num,
1332                  solo_enc->vfd->num);
1333
1334         return solo_enc;
1335
1336 vdev_release:
1337         video_device_release(solo_enc->vfd);
1338 pci_free:
1339         pci_free_consistent(solo_enc->solo_dev->pdev,
1340                         sizeof(struct solo_p2m_desc) * solo_enc->desc_nelts,
1341                         solo_enc->desc_items, solo_enc->desc_dma);
1342 hdl_free:
1343         v4l2_ctrl_handler_free(hdl);
1344         kfree(solo_enc);
1345         return ERR_PTR(ret);
1346 }
1347
1348 static void solo_enc_free(struct solo_enc_dev *solo_enc)
1349 {
1350         if (solo_enc == NULL)
1351                 return;
1352
1353         video_unregister_device(solo_enc->vfd);
1354         v4l2_ctrl_handler_free(&solo_enc->hdl);
1355         kfree(solo_enc);
1356 }
1357
1358 int solo_enc_v4l2_init(struct solo_dev *solo_dev, unsigned nr)
1359 {
1360         int i;
1361
1362         init_waitqueue_head(&solo_dev->ring_thread_wait);
1363
1364         solo_dev->vh_size = sizeof(vop_header);
1365         solo_dev->vh_buf = pci_alloc_consistent(solo_dev->pdev,
1366                                                 solo_dev->vh_size,
1367                                                 &solo_dev->vh_dma);
1368         if (solo_dev->vh_buf == NULL)
1369                 return -ENOMEM;
1370
1371         for (i = 0; i < solo_dev->nr_chans; i++) {
1372                 solo_dev->v4l2_enc[i] = solo_enc_alloc(solo_dev, i, nr);
1373                 if (IS_ERR(solo_dev->v4l2_enc[i]))
1374                         break;
1375         }
1376
1377         if (i != solo_dev->nr_chans) {
1378                 int ret = PTR_ERR(solo_dev->v4l2_enc[i]);
1379                 while (i--)
1380                         solo_enc_free(solo_dev->v4l2_enc[i]);
1381                 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1382                                     solo_dev->vh_buf, solo_dev->vh_dma);
1383                 solo_dev->vh_buf = NULL;
1384                 return ret;
1385         }
1386
1387         if (solo_dev->type == SOLO_DEV_6010)
1388                 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 4;
1389         else
1390                 solo_dev->enc_bw_remain = solo_dev->fps * 4 * 5;
1391
1392         dev_info(&solo_dev->pdev->dev, "Encoders as /dev/video%d-%d\n",
1393                  solo_dev->v4l2_enc[0]->vfd->num,
1394                  solo_dev->v4l2_enc[solo_dev->nr_chans - 1]->vfd->num);
1395
1396         return 0;
1397 }
1398
1399 void solo_enc_v4l2_exit(struct solo_dev *solo_dev)
1400 {
1401         int i;
1402
1403         for (i = 0; i < solo_dev->nr_chans; i++)
1404                 solo_enc_free(solo_dev->v4l2_enc[i]);
1405
1406         if (solo_dev->vh_buf)
1407                 pci_free_consistent(solo_dev->pdev, solo_dev->vh_size,
1408                             solo_dev->vh_buf, solo_dev->vh_dma);
1409 }