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 f->pixelformat = V4L2_PIX_FMT_MPEG; 95 return 0; 96 } 97 98 static int empress_g_fmt_vid_cap(struct file *file, void *priv, 99 struct v4l2_format *f) 100 { 101 struct saa7134_dev *dev = video_drvdata(file); 102 struct v4l2_subdev_format fmt = { 103 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 104 }; 105 struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format; 106 107 saa_call_all(dev, pad, get_fmt, NULL, &fmt); 108 109 v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt); 110 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 111 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 112 f->fmt.pix.bytesperline = 0; 113 114 return 0; 115 } 116 117 static int empress_s_fmt_vid_cap(struct file *file, void *priv, 118 struct v4l2_format *f) 119 { 120 struct saa7134_dev *dev = video_drvdata(file); 121 struct v4l2_subdev_format format = { 122 .which = V4L2_SUBDEV_FORMAT_ACTIVE, 123 }; 124 125 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED); 126 saa_call_all(dev, pad, set_fmt, NULL, &format); 127 v4l2_fill_pix_format(&f->fmt.pix, &format.format); 128 129 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 130 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 131 f->fmt.pix.bytesperline = 0; 132 133 return 0; 134 } 135 136 static int empress_try_fmt_vid_cap(struct file *file, void *priv, 137 struct v4l2_format *f) 138 { 139 struct saa7134_dev *dev = video_drvdata(file); 140 struct v4l2_subdev_pad_config pad_cfg; 141 struct v4l2_subdev_format format = { 142 .which = V4L2_SUBDEV_FORMAT_TRY, 143 }; 144 145 v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED); 146 saa_call_all(dev, pad, set_fmt, &pad_cfg, &format); 147 v4l2_fill_pix_format(&f->fmt.pix, &format.format); 148 149 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 150 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 151 f->fmt.pix.bytesperline = 0; 152 153 return 0; 154 } 155 156 static const struct v4l2_file_operations ts_fops = 157 { 158 .owner = THIS_MODULE, 159 .open = v4l2_fh_open, 160 .release = vb2_fop_release, 161 .read = vb2_fop_read, 162 .poll = vb2_fop_poll, 163 .mmap = vb2_fop_mmap, 164 .unlocked_ioctl = video_ioctl2, 165 }; 166 167 static const struct v4l2_ioctl_ops ts_ioctl_ops = { 168 .vidioc_querycap = saa7134_querycap, 169 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap, 170 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap, 171 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap, 172 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap, 173 .vidioc_reqbufs = vb2_ioctl_reqbufs, 174 .vidioc_querybuf = vb2_ioctl_querybuf, 175 .vidioc_qbuf = vb2_ioctl_qbuf, 176 .vidioc_dqbuf = vb2_ioctl_dqbuf, 177 .vidioc_expbuf = vb2_ioctl_expbuf, 178 .vidioc_streamon = vb2_ioctl_streamon, 179 .vidioc_streamoff = vb2_ioctl_streamoff, 180 .vidioc_g_frequency = saa7134_g_frequency, 181 .vidioc_s_frequency = saa7134_s_frequency, 182 .vidioc_g_tuner = saa7134_g_tuner, 183 .vidioc_s_tuner = saa7134_s_tuner, 184 .vidioc_enum_input = saa7134_enum_input, 185 .vidioc_g_input = saa7134_g_input, 186 .vidioc_s_input = saa7134_s_input, 187 .vidioc_s_std = saa7134_s_std, 188 .vidioc_g_std = saa7134_g_std, 189 .vidioc_querystd = saa7134_querystd, 190 .vidioc_log_status = v4l2_ctrl_log_status, 191 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 192 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 193 }; 194 195 /* ----------------------------------------------------------- */ 196 197 static const struct video_device saa7134_empress_template = { 198 .name = "saa7134-empress", 199 .fops = &ts_fops, 200 .ioctl_ops = &ts_ioctl_ops, 201 202 .tvnorms = SAA7134_NORMS, 203 }; 204 205 static void empress_signal_update(struct work_struct *work) 206 { 207 struct saa7134_dev* dev = 208 container_of(work, struct saa7134_dev, empress_workqueue); 209 210 if (dev->nosignal) { 211 pr_debug("no video signal\n"); 212 } else { 213 pr_debug("video signal acquired\n"); 214 } 215 } 216 217 static void empress_signal_change(struct saa7134_dev *dev) 218 { 219 schedule_work(&dev->empress_workqueue); 220 } 221 222 static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl) 223 { 224 switch (ctrl->id) { 225 case V4L2_CID_BRIGHTNESS: 226 case V4L2_CID_HUE: 227 case V4L2_CID_CONTRAST: 228 case V4L2_CID_SATURATION: 229 case V4L2_CID_AUDIO_MUTE: 230 case V4L2_CID_AUDIO_VOLUME: 231 case V4L2_CID_PRIVATE_INVERT: 232 case V4L2_CID_PRIVATE_AUTOMUTE: 233 return true; 234 default: 235 return false; 236 } 237 } 238 239 static int empress_init(struct saa7134_dev *dev) 240 { 241 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler; 242 struct vb2_queue *q; 243 int err; 244 245 pr_debug("%s: %s\n", dev->name, __func__); 246 dev->empress_dev = video_device_alloc(); 247 if (NULL == dev->empress_dev) 248 return -ENOMEM; 249 *(dev->empress_dev) = saa7134_empress_template; 250 dev->empress_dev->v4l2_dev = &dev->v4l2_dev; 251 dev->empress_dev->release = video_device_release; 252 dev->empress_dev->lock = &dev->lock; 253 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name), 254 "%s empress (%s)", dev->name, 255 saa7134_boards[dev->board].name); 256 v4l2_ctrl_handler_init(hdl, 21); 257 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter, false); 258 if (dev->empress_sd) 259 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL, true); 260 if (hdl->error) { 261 video_device_release(dev->empress_dev); 262 return hdl->error; 263 } 264 dev->empress_dev->ctrl_handler = hdl; 265 266 INIT_WORK(&dev->empress_workqueue, empress_signal_update); 267 268 q = &dev->empress_vbq; 269 q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE; 270 /* 271 * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle 272 * transfers that do not start at the beginning of a page. A USERPTR 273 * can start anywhere in a page, so USERPTR support is a no-go. 274 */ 275 q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ; 276 q->drv_priv = &dev->ts_q; 277 q->ops = &saa7134_empress_qops; 278 q->gfp_flags = GFP_DMA32; 279 q->mem_ops = &vb2_dma_sg_memops; 280 q->buf_struct_size = sizeof(struct saa7134_buf); 281 q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; 282 q->lock = &dev->lock; 283 q->dev = &dev->pci->dev; 284 err = vb2_queue_init(q); 285 if (err) 286 return err; 287 dev->empress_dev->queue = q; 288 dev->empress_dev->device_caps = V4L2_CAP_READWRITE | V4L2_CAP_STREAMING | 289 V4L2_CAP_VIDEO_CAPTURE; 290 if (dev->tuner_type != TUNER_ABSENT && dev->tuner_type != UNSET) 291 dev->empress_dev->device_caps |= V4L2_CAP_TUNER; 292 293 video_set_drvdata(dev->empress_dev, dev); 294 err = video_register_device(dev->empress_dev,VFL_TYPE_VIDEO, 295 empress_nr[dev->nr]); 296 if (err < 0) { 297 pr_info("%s: can't register video device\n", 298 dev->name); 299 video_device_release(dev->empress_dev); 300 dev->empress_dev = NULL; 301 return err; 302 } 303 pr_info("%s: registered device %s [mpeg]\n", 304 dev->name, video_device_node_name(dev->empress_dev)); 305 306 empress_signal_update(&dev->empress_workqueue); 307 return 0; 308 } 309 310 static int empress_fini(struct saa7134_dev *dev) 311 { 312 pr_debug("%s: %s\n", dev->name, __func__); 313 314 if (NULL == dev->empress_dev) 315 return 0; 316 flush_work(&dev->empress_workqueue); 317 video_unregister_device(dev->empress_dev); 318 vb2_queue_release(&dev->empress_vbq); 319 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler); 320 dev->empress_dev = NULL; 321 return 0; 322 } 323 324 static struct saa7134_mpeg_ops empress_ops = { 325 .type = SAA7134_MPEG_EMPRESS, 326 .init = empress_init, 327 .fini = empress_fini, 328 .signal_change = empress_signal_change, 329 }; 330 331 static int __init empress_register(void) 332 { 333 return saa7134_ts_register(&empress_ops); 334 } 335 336 static void __exit empress_unregister(void) 337 { 338 saa7134_ts_unregister(&empress_ops); 339 } 340 341 module_init(empress_register); 342 module_exit(empress_unregister); 343