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