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