1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * 4 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs] 5 */ 6 7 #include "saa7134.h" 8 #include "saa7134-reg.h" 9 10 #include <linux/init.h> 11 #include <linux/list.h> 12 #include <linux/module.h> 13 #include <linux/kernel.h> 14 #include <linux/delay.h> 15 16 #include <media/v4l2-common.h> 17 #include <media/v4l2-event.h> 18 19 /* ------------------------------------------------------------------ */ 20 21 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]"); 22 MODULE_LICENSE("GPL"); 23 24 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET }; 25 26 module_param_array(empress_nr, int, NULL, 0444); 27 MODULE_PARM_DESC(empress_nr,"ts device number"); 28 29 /* ------------------------------------------------------------------ */ 30 31 static int start_streaming(struct vb2_queue *vq, unsigned int count) 32 { 33 struct saa7134_dmaqueue *dmaq = vq->drv_priv; 34 struct saa7134_dev *dev = dmaq->dev; 35 u32 leading_null_bytes = 0; 36 int err; 37 38 err = saa7134_ts_start_streaming(vq, count); 39 if (err) 40 return err; 41 42 /* If more cards start to need this, then this 43 should probably be added to the card definitions. */ 44 switch (dev->board) { 45 case SAA7134_BOARD_BEHOLD_M6: 46 case SAA7134_BOARD_BEHOLD_M63: 47 case SAA7134_BOARD_BEHOLD_M6_EXTRA: 48 leading_null_bytes = 1; 49 break; 50 } 51 saa_call_all(dev, core, init, leading_null_bytes); 52 /* Unmute audio */ 53 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, 54 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6)); 55 dev->empress_started = 1; 56 return 0; 57 } 58 59 static void stop_streaming(struct vb2_queue *vq) 60 { 61 struct saa7134_dmaqueue *dmaq = vq->drv_priv; 62 struct saa7134_dev *dev = dmaq->dev; 63 64 saa7134_ts_stop_streaming(vq); 65 saa_writeb(SAA7134_SPECIAL_MODE, 0x00); 66 msleep(20); 67 saa_writeb(SAA7134_SPECIAL_MODE, 0x01); 68 msleep(100); 69 /* Mute audio */ 70 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, 71 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6)); 72 dev->empress_started = 0; 73 } 74 75 static const struct vb2_ops saa7134_empress_qops = { 76 .queue_setup = saa7134_ts_queue_setup, 77 .buf_init = saa7134_ts_buffer_init, 78 .buf_prepare = saa7134_ts_buffer_prepare, 79 .buf_queue = saa7134_vb2_buffer_queue, 80 .wait_prepare = vb2_ops_wait_prepare, 81 .wait_finish = vb2_ops_wait_finish, 82 .start_streaming = start_streaming, 83 .stop_streaming = stop_streaming, 84 }; 85 86 /* ------------------------------------------------------------------ */ 87 88 static int empress_enum_fmt_vid_cap(struct file *file, void *priv, 89 struct v4l2_fmtdesc *f) 90 { 91 if (f->index != 0) 92 return -EINVAL; 93 94 strscpy(f->description, "MPEG TS", sizeof(f->description)); 95 f->pixelformat = V4L2_PIX_FMT_MPEG; 96 f->flags = V4L2_FMT_FLAG_COMPRESSED; 97 return 0; 98 } 99 100 static int empress_g_fmt_vid_cap(struct file *file, void *priv, 101 struct v4l2_format *f) 102 { 103 struct saa7134_dev *dev = video_drvdata(file); 104 struct v4l2_subdev_format fmt = { 105 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 106 }; 107 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format; 108 109 saa_call_all(dev, pad, get_fmt, NULL, &fmt); 110 111 v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt); 112 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 113 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 114 f->fmt.pix.bytesperline = 0; 115 116 return 0; 117 } 118 119 static int empress_s_fmt_vid_cap(struct file *file, void *priv, 120 struct v4l2_format *f) 121 { 122 struct saa7134_dev *dev = video_drvdata(file); 123 struct v4l2_subdev_format format = { 124 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 125 }; 126 127 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED); 128 saa_call_all(dev, pad, set_fmt, NULL, &format); 129 v4l2_fill_pix_format(&f->fmt.pix, &format.format); 130 131 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 132 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 133 f->fmt.pix.bytesperline = 0; 134 135 return 0; 136 } 137 138 static int empress_try_fmt_vid_cap(struct file *file, void *priv, 139 struct v4l2_format *f) 140 { 141 struct saa7134_dev *dev = video_drvdata(file); 142 struct v4l2_subdev_pad_config pad_cfg; 143 struct v4l2_subdev_format format = { 144 .which = V4L2_SUBDEV_FORMAT_TRY, 145 }; 146 147 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED); 148 saa_call_all(dev, pad, set_fmt, &pad_cfg, &format); 149 v4l2_fill_pix_format(&f->fmt.pix, &format.format); 150 151 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 152 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 153 f->fmt.pix.bytesperline = 0; 154 155 return 0; 156 } 157 158 static const struct v4l2_file_operations ts_fops = 159 { 160 .owner = THIS_MODULE, 161 .open = v4l2_fh_open, 162 .release = vb2_fop_release, 163 .read = vb2_fop_read, 164 .poll = vb2_fop_poll, 165 .mmap = vb2_fop_mmap, 166 .unlocked_ioctl = video_ioctl2, 167 }; 168 169 static const struct v4l2_ioctl_ops ts_ioctl_ops = { 170 .vidioc_querycap = saa7134_querycap, 171 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap, 172 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap, 173 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap, 174 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap, 175 .vidioc_reqbufs = vb2_ioctl_reqbufs, 176 .vidioc_querybuf = vb2_ioctl_querybuf, 177 .vidioc_qbuf = vb2_ioctl_qbuf, 178 .vidioc_dqbuf = vb2_ioctl_dqbuf, 179 .vidioc_expbuf = vb2_ioctl_expbuf, 180 .vidioc_streamon = vb2_ioctl_streamon, 181 .vidioc_streamoff = vb2_ioctl_streamoff, 182 .vidioc_g_frequency = saa7134_g_frequency, 183 .vidioc_s_frequency = saa7134_s_frequency, 184 .vidioc_g_tuner = saa7134_g_tuner, 185 .vidioc_s_tuner = saa7134_s_tuner, 186 .vidioc_enum_input = saa7134_enum_input, 187 .vidioc_g_input = saa7134_g_input, 188 .vidioc_s_input = saa7134_s_input, 189 .vidioc_s_std = saa7134_s_std, 190 .vidioc_g_std = saa7134_g_std, 191 .vidioc_querystd = saa7134_querystd, 192 .vidioc_log_status = v4l2_ctrl_log_status, 193 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 194 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 195 }; 196 197 /* ----------------------------------------------------------- */ 198 199 static const struct video_device saa7134_empress_template = { 200 .name = "saa7134-empress", 201 .fops = &ts_fops, 202 .ioctl_ops = &ts_ioctl_ops, 203 204 .tvnorms = SAA7134_NORMS, 205 }; 206 207 static void empress_signal_update(struct work_struct *work) 208 { 209 struct saa7134_dev* dev = 210 container_of(work, struct saa7134_dev, empress_workqueue); 211 212 if (dev->nosignal) { 213 pr_debug("no video signal\n"); 214 } else { 215 pr_debug("video signal acquired\n"); 216 } 217 } 218 219 static void empress_signal_change(struct saa7134_dev *dev) 220 { 221 schedule_work(&dev->empress_workqueue); 222 } 223 224 static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl) 225 { 226 switch (ctrl->id) { 227 case V4L2_CID_BRIGHTNESS: 228 case V4L2_CID_HUE: 229 case V4L2_CID_CONTRAST: 230 case V4L2_CID_SATURATION: 231 case V4L2_CID_AUDIO_MUTE: 232 case V4L2_CID_AUDIO_VOLUME: 233 case V4L2_CID_PRIVATE_INVERT: 234 case V4L2_CID_PRIVATE_AUTOMUTE: 235 return true; 236 default: 237 return false; 238 } 239 } 240 241 static int empress_init(struct saa7134_dev *dev) 242 { 243 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler; 244 struct vb2_queue *q; 245 int err; 246 247 pr_debug("%s: %s\n", dev->name, __func__); 248 dev->empress_dev = video_device_alloc(); 249 if (NULL == dev->empress_dev) 250 return -ENOMEM; 251 *(dev->empress_dev) = saa7134_empress_template; 252 dev->empress_dev->v4l2_dev = &dev->v4l2_dev; 253 dev->empress_dev->release = video_device_release; 254 dev->empress_dev->lock = &dev->lock; 255 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name), 256 "%s empress (%s)", dev->name, 257 saa7134_boards[dev->board].name); 258 v4l2_ctrl_handler_init(hdl, 21); 259 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter, false); 260 if (dev->empress_sd) 261 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL, true); 262 if (hdl->error) { 263 video_device_release(dev->empress_dev); 264 return hdl->error; 265 } 266 dev->empress_dev->ctrl_handler = hdl; 267 268 INIT_WORK(&dev->empress_workqueue, empress_signal_update); 269 270 q = &dev->empress_vbq; 271 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 272 /* 273 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle 274 * transfers that do not start at the beginning of a page. A USERPTR 275 * can start anywhere in a page, so USERPTR support is a no-go. 276 */ 277 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ; 278 q->drv_priv = &dev->ts_q; 279 q->ops = &saa7134_empress_qops; 280 q->gfp_flags = GFP_DMA32; 281 q->mem_ops = &vb2_dma_sg_memops; 282 q->buf_struct_size = sizeof(struct saa7134_buf); 283 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 284 q->lock = &dev->lock; 285 q->dev = &dev->pci->dev; 286 err = vb2_queue_init(q); 287 if (err) 288 return err; 289 dev->empress_dev->queue = q; 290 dev->empress_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | 291 V4L2_CAP_VIDEO_CAPTURE; 292 if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET) 293 dev->empress_dev->device_caps |= V4L2_CAP_TUNER; 294 295 video_set_drvdata(dev->empress_dev, dev); 296 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER, 297 empress_nr[dev->nr]); 298 if (err < 0) { 299 pr_info("%s: can't register video device\n", 300 dev->name); 301 video_device_release(dev->empress_dev); 302 dev->empress_dev = NULL; 303 return err; 304 } 305 pr_info("%s: registered device %s [mpeg]\n", 306 dev->name, video_device_node_name(dev->empress_dev)); 307 308 empress_signal_update(&dev->empress_workqueue); 309 return 0; 310 } 311 312 static int empress_fini(struct saa7134_dev *dev) 313 { 314 pr_debug("%s: %s\n", dev->name, __func__); 315 316 if (NULL == dev->empress_dev) 317 return 0; 318 flush_work(&dev->empress_workqueue); 319 video_unregister_device(dev->empress_dev); 320 vb2_queue_release(&dev->empress_vbq); 321 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler); 322 dev->empress_dev = NULL; 323 return 0; 324 } 325 326 static struct saa7134_mpeg_ops empress_ops = { 327 .type = SAA7134_MPEG_EMPRESS, 328 .init = empress_init, 329 .fini = empress_fini, 330 .signal_change = empress_signal_change, 331 }; 332 333 static int __init empress_register(void) 334 { 335 return saa7134_ts_register(&empress_ops); 336 } 337 338 static void __exit empress_unregister(void) 339 { 340 saa7134_ts_unregister(&empress_ops); 341 } 342 343 module_init(empress_register); 344 module_exit(empress_unregister); 345