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 void ts_reset_encoder(struct saa7134_dev* dev) 52 { 53 if (!dev->empress_started) 54 return; 55 56 saa_writeb(SAA7134_SPECIAL_MODE, 0x00); 57 msleep(10); 58 saa_writeb(SAA7134_SPECIAL_MODE, 0x01); 59 msleep(100); 60 dev->empress_started = 0; 61 } 62 63 static int ts_init_encoder(struct saa7134_dev* dev) 64 { 65 u32 leading_null_bytes = 0; 66 67 /* If more cards start to need this, then this 68 should probably be added to the card definitions. */ 69 switch (dev->board) { 70 case SAA7134_BOARD_BEHOLD_M6: 71 case SAA7134_BOARD_BEHOLD_M63: 72 case SAA7134_BOARD_BEHOLD_M6_EXTRA: 73 leading_null_bytes = 1; 74 break; 75 } 76 ts_reset_encoder(dev); 77 saa_call_all(dev, core, init, leading_null_bytes); 78 dev->empress_started = 1; 79 return 0; 80 } 81 82 /* ------------------------------------------------------------------ */ 83 84 static int ts_open(struct file *file) 85 { 86 struct video_device *vdev = video_devdata(file); 87 struct saa7134_dev *dev = video_drvdata(file); 88 struct saa7134_fh *fh; 89 90 /* allocate + initialize per filehandle data */ 91 fh = kzalloc(sizeof(*fh), GFP_KERNEL); 92 if (NULL == fh) 93 return -ENOMEM; 94 95 v4l2_fh_init(&fh->fh, vdev); 96 file->private_data = fh; 97 fh->is_empress = true; 98 v4l2_fh_add(&fh->fh); 99 100 /* Unmute audio */ 101 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, 102 saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6)); 103 104 return 0; 105 } 106 107 static int ts_release(struct file *file) 108 { 109 struct saa7134_dev *dev = video_drvdata(file); 110 struct saa7134_fh *fh = file->private_data; 111 112 if (res_check(fh, RESOURCE_EMPRESS)) { 113 videobuf_stop(&dev->empress_tsq); 114 videobuf_mmap_free(&dev->empress_tsq); 115 116 /* stop the encoder */ 117 ts_reset_encoder(dev); 118 119 /* Mute audio */ 120 saa_writeb(SAA7134_AUDIO_MUTE_CTRL, 121 saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6)); 122 } 123 124 v4l2_fh_del(&fh->fh); 125 v4l2_fh_exit(&fh->fh); 126 return 0; 127 } 128 129 static ssize_t 130 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos) 131 { 132 struct saa7134_dev *dev = video_drvdata(file); 133 134 if (res_locked(dev, RESOURCE_EMPRESS)) 135 return -EBUSY; 136 if (!dev->empress_started) 137 ts_init_encoder(dev); 138 139 return videobuf_read_stream(&dev->empress_tsq, 140 data, count, ppos, 0, 141 file->f_flags & O_NONBLOCK); 142 } 143 144 static unsigned int 145 ts_poll(struct file *file, struct poll_table_struct *wait) 146 { 147 unsigned long req_events = poll_requested_events(wait); 148 struct saa7134_dev *dev = video_drvdata(file); 149 struct saa7134_fh *fh = file->private_data; 150 unsigned int rc = 0; 151 152 if (v4l2_event_pending(&fh->fh)) 153 rc = POLLPRI; 154 else if (req_events & POLLPRI) 155 poll_wait(file, &fh->fh.wait, wait); 156 return rc | videobuf_poll_stream(file, &dev->empress_tsq, wait); 157 } 158 159 160 static int 161 ts_mmap(struct file *file, struct vm_area_struct * vma) 162 { 163 struct saa7134_dev *dev = video_drvdata(file); 164 165 return videobuf_mmap_mapper(&dev->empress_tsq, vma); 166 } 167 168 static int empress_enum_fmt_vid_cap(struct file *file, void *priv, 169 struct v4l2_fmtdesc *f) 170 { 171 if (f->index != 0) 172 return -EINVAL; 173 174 strlcpy(f->description, "MPEG TS", sizeof(f->description)); 175 f->pixelformat = V4L2_PIX_FMT_MPEG; 176 f->flags = V4L2_FMT_FLAG_COMPRESSED; 177 return 0; 178 } 179 180 static int empress_g_fmt_vid_cap(struct file *file, void *priv, 181 struct v4l2_format *f) 182 { 183 struct saa7134_dev *dev = video_drvdata(file); 184 struct v4l2_mbus_framefmt mbus_fmt; 185 186 saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt); 187 188 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); 189 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 190 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 191 f->fmt.pix.bytesperline = 0; 192 f->fmt.pix.priv = 0; 193 194 return 0; 195 } 196 197 static int empress_s_fmt_vid_cap(struct file *file, void *priv, 198 struct v4l2_format *f) 199 { 200 struct saa7134_dev *dev = video_drvdata(file); 201 struct v4l2_mbus_framefmt mbus_fmt; 202 203 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED); 204 saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt); 205 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); 206 207 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 208 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 209 f->fmt.pix.bytesperline = 0; 210 f->fmt.pix.priv = 0; 211 212 return 0; 213 } 214 215 static int empress_try_fmt_vid_cap(struct file *file, void *priv, 216 struct v4l2_format *f) 217 { 218 struct saa7134_dev *dev = video_drvdata(file); 219 struct v4l2_mbus_framefmt mbus_fmt; 220 221 v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED); 222 saa_call_all(dev, video, try_mbus_fmt, &mbus_fmt); 223 v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt); 224 225 f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG; 226 f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets; 227 f->fmt.pix.bytesperline = 0; 228 f->fmt.pix.priv = 0; 229 230 return 0; 231 } 232 233 static const struct v4l2_file_operations ts_fops = 234 { 235 .owner = THIS_MODULE, 236 .open = ts_open, 237 .release = ts_release, 238 .read = ts_read, 239 .poll = ts_poll, 240 .mmap = ts_mmap, 241 .ioctl = video_ioctl2, 242 }; 243 244 static const struct v4l2_ioctl_ops ts_ioctl_ops = { 245 .vidioc_querycap = saa7134_querycap, 246 .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap, 247 .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap, 248 .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap, 249 .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap, 250 .vidioc_reqbufs = saa7134_reqbufs, 251 .vidioc_querybuf = saa7134_querybuf, 252 .vidioc_qbuf = saa7134_qbuf, 253 .vidioc_dqbuf = saa7134_dqbuf, 254 .vidioc_streamon = saa7134_streamon, 255 .vidioc_streamoff = saa7134_streamoff, 256 .vidioc_g_frequency = saa7134_g_frequency, 257 .vidioc_s_frequency = saa7134_s_frequency, 258 .vidioc_g_tuner = saa7134_g_tuner, 259 .vidioc_s_tuner = saa7134_s_tuner, 260 .vidioc_enum_input = saa7134_enum_input, 261 .vidioc_g_input = saa7134_g_input, 262 .vidioc_s_input = saa7134_s_input, 263 .vidioc_s_std = saa7134_s_std, 264 .vidioc_g_std = saa7134_g_std, 265 .vidioc_log_status = v4l2_ctrl_log_status, 266 .vidioc_subscribe_event = v4l2_ctrl_subscribe_event, 267 .vidioc_unsubscribe_event = v4l2_event_unsubscribe, 268 }; 269 270 /* ----------------------------------------------------------- */ 271 272 static struct video_device saa7134_empress_template = { 273 .name = "saa7134-empress", 274 .fops = &ts_fops, 275 .ioctl_ops = &ts_ioctl_ops, 276 277 .tvnorms = SAA7134_NORMS, 278 }; 279 280 static void empress_signal_update(struct work_struct *work) 281 { 282 struct saa7134_dev* dev = 283 container_of(work, struct saa7134_dev, empress_workqueue); 284 285 if (dev->nosignal) { 286 dprintk("no video signal\n"); 287 } else { 288 dprintk("video signal acquired\n"); 289 } 290 } 291 292 static void empress_signal_change(struct saa7134_dev *dev) 293 { 294 schedule_work(&dev->empress_workqueue); 295 } 296 297 static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl) 298 { 299 switch (ctrl->id) { 300 case V4L2_CID_BRIGHTNESS: 301 case V4L2_CID_HUE: 302 case V4L2_CID_CONTRAST: 303 case V4L2_CID_SATURATION: 304 case V4L2_CID_AUDIO_MUTE: 305 case V4L2_CID_AUDIO_VOLUME: 306 case V4L2_CID_PRIVATE_INVERT: 307 case V4L2_CID_PRIVATE_AUTOMUTE: 308 return true; 309 default: 310 return false; 311 } 312 } 313 314 static int empress_init(struct saa7134_dev *dev) 315 { 316 struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler; 317 int err; 318 319 dprintk("%s: %s\n",dev->name,__func__); 320 dev->empress_dev = video_device_alloc(); 321 if (NULL == dev->empress_dev) 322 return -ENOMEM; 323 *(dev->empress_dev) = saa7134_empress_template; 324 dev->empress_dev->v4l2_dev = &dev->v4l2_dev; 325 dev->empress_dev->release = video_device_release; 326 snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name), 327 "%s empress (%s)", dev->name, 328 saa7134_boards[dev->board].name); 329 set_bit(V4L2_FL_USE_FH_PRIO, &dev->empress_dev->flags); 330 v4l2_ctrl_handler_init(hdl, 21); 331 v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter); 332 if (dev->empress_sd) 333 v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL); 334 if (hdl->error) { 335 video_device_release(dev->empress_dev); 336 return hdl->error; 337 } 338 dev->empress_dev->ctrl_handler = hdl; 339 340 INIT_WORK(&dev->empress_workqueue, empress_signal_update); 341 342 video_set_drvdata(dev->empress_dev, dev); 343 err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER, 344 empress_nr[dev->nr]); 345 if (err < 0) { 346 printk(KERN_INFO "%s: can't register video device\n", 347 dev->name); 348 video_device_release(dev->empress_dev); 349 dev->empress_dev = NULL; 350 return err; 351 } 352 printk(KERN_INFO "%s: registered device %s [mpeg]\n", 353 dev->name, video_device_node_name(dev->empress_dev)); 354 355 videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops, 356 &dev->pci->dev, &dev->slock, 357 V4L2_BUF_TYPE_VIDEO_CAPTURE, 358 V4L2_FIELD_ALTERNATE, 359 sizeof(struct saa7134_buf), 360 dev, NULL); 361 362 empress_signal_update(&dev->empress_workqueue); 363 return 0; 364 } 365 366 static int empress_fini(struct saa7134_dev *dev) 367 { 368 dprintk("%s: %s\n",dev->name,__func__); 369 370 if (NULL == dev->empress_dev) 371 return 0; 372 flush_work(&dev->empress_workqueue); 373 video_unregister_device(dev->empress_dev); 374 v4l2_ctrl_handler_free(&dev->empress_ctrl_handler); 375 dev->empress_dev = NULL; 376 return 0; 377 } 378 379 static struct saa7134_mpeg_ops empress_ops = { 380 .type = SAA7134_MPEG_EMPRESS, 381 .init = empress_init, 382 .fini = empress_fini, 383 .signal_change = empress_signal_change, 384 }; 385 386 static int __init empress_register(void) 387 { 388 return saa7134_ts_register(&empress_ops); 389 } 390 391 static void __exit empress_unregister(void) 392 { 393 saa7134_ts_unregister(&empress_ops); 394 } 395 396 module_init(empress_register); 397 module_exit(empress_unregister); 398 399 /* ----------------------------------------------------------- */ 400 /* 401 * Local variables: 402 * c-basic-offset: 8 403 * End: 404 */ 405