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 "saa7134-reg.h"
27 #include "saa7134.h"
28 
29 #include <media/saa6752hs.h>
30 #include <media/v4l2-common.h>
31 #include <media/v4l2-chip-ident.h>
32 
33 /* ------------------------------------------------------------------ */
34 
35 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
36 MODULE_LICENSE("GPL");
37 
38 static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
39 
40 module_param_array(empress_nr, int, NULL, 0444);
41 MODULE_PARM_DESC(empress_nr,"ts device number");
42 
43 static unsigned int debug;
44 module_param(debug, int, 0644);
45 MODULE_PARM_DESC(debug,"enable debug messages");
46 
47 #define dprintk(fmt, arg...)	if (debug)			\
48 	printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
49 
50 /* ------------------------------------------------------------------ */
51 
52 static void ts_reset_encoder(struct saa7134_dev* dev)
53 {
54 	if (!dev->empress_started)
55 		return;
56 
57 	saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
58 	msleep(10);
59 	saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
60 	msleep(100);
61 	dev->empress_started = 0;
62 }
63 
64 static int ts_init_encoder(struct saa7134_dev* dev)
65 {
66 	u32 leading_null_bytes = 0;
67 
68 	/* If more cards start to need this, then this
69 	   should probably be added to the card definitions. */
70 	switch (dev->board) {
71 	case SAA7134_BOARD_BEHOLD_M6:
72 	case SAA7134_BOARD_BEHOLD_M63:
73 	case SAA7134_BOARD_BEHOLD_M6_EXTRA:
74 		leading_null_bytes = 1;
75 		break;
76 	}
77 	ts_reset_encoder(dev);
78 	saa_call_all(dev, core, init, leading_null_bytes);
79 	dev->empress_started = 1;
80 	return 0;
81 }
82 
83 /* ------------------------------------------------------------------ */
84 
85 static int ts_open(struct file *file)
86 {
87 	struct video_device *vdev = video_devdata(file);
88 	struct saa7134_dev *dev = video_drvdata(file);
89 	int err;
90 
91 	dprintk("open dev=%s\n", video_device_node_name(vdev));
92 	err = -EBUSY;
93 	if (!mutex_trylock(&dev->empress_tsq.vb_lock))
94 		return err;
95 	if (atomic_read(&dev->empress_users))
96 		goto done;
97 
98 	/* Unmute audio */
99 	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
100 		saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
101 
102 	atomic_inc(&dev->empress_users);
103 	file->private_data = dev;
104 	err = 0;
105 
106 done:
107 	mutex_unlock(&dev->empress_tsq.vb_lock);
108 	return err;
109 }
110 
111 static int ts_release(struct file *file)
112 {
113 	struct saa7134_dev *dev = file->private_data;
114 
115 	videobuf_stop(&dev->empress_tsq);
116 	videobuf_mmap_free(&dev->empress_tsq);
117 
118 	/* stop the encoder */
119 	ts_reset_encoder(dev);
120 
121 	/* Mute audio */
122 	saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
123 		saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
124 
125 	atomic_dec(&dev->empress_users);
126 
127 	return 0;
128 }
129 
130 static ssize_t
131 ts_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
132 {
133 	struct saa7134_dev *dev = file->private_data;
134 
135 	if (!dev->empress_started)
136 		ts_init_encoder(dev);
137 
138 	return videobuf_read_stream(&dev->empress_tsq,
139 				    data, count, ppos, 0,
140 				    file->f_flags & O_NONBLOCK);
141 }
142 
143 static unsigned int
144 ts_poll(struct file *file, struct poll_table_struct *wait)
145 {
146 	struct saa7134_dev *dev = file->private_data;
147 
148 	return videobuf_poll_stream(file, &dev->empress_tsq, wait);
149 }
150 
151 
152 static int
153 ts_mmap(struct file *file, struct vm_area_struct * vma)
154 {
155 	struct saa7134_dev *dev = file->private_data;
156 
157 	return videobuf_mmap_mapper(&dev->empress_tsq, vma);
158 }
159 
160 /*
161  * This function is _not_ called directly, but from
162  * video_generic_ioctl (and maybe others).  userspace
163  * copying is done already, arg is a kernel pointer.
164  */
165 
166 static int empress_querycap(struct file *file, void  *priv,
167 					struct v4l2_capability *cap)
168 {
169 	struct saa7134_dev *dev = file->private_data;
170 
171 	strcpy(cap->driver, "saa7134");
172 	strlcpy(cap->card, saa7134_boards[dev->board].name,
173 		sizeof(cap->card));
174 	sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
175 	cap->capabilities =
176 		V4L2_CAP_VIDEO_CAPTURE |
177 		V4L2_CAP_READWRITE |
178 		V4L2_CAP_STREAMING;
179 	return 0;
180 }
181 
182 static int empress_enum_input(struct file *file, void *priv,
183 					struct v4l2_input *i)
184 {
185 	if (i->index != 0)
186 		return -EINVAL;
187 
188 	i->type = V4L2_INPUT_TYPE_CAMERA;
189 	strcpy(i->name, "CCIR656");
190 
191 	return 0;
192 }
193 
194 static int empress_g_input(struct file *file, void *priv, unsigned int *i)
195 {
196 	*i = 0;
197 	return 0;
198 }
199 
200 static int empress_s_input(struct file *file, void *priv, unsigned int i)
201 {
202 	if (i != 0)
203 		return -EINVAL;
204 
205 	return 0;
206 }
207 
208 static int empress_enum_fmt_vid_cap(struct file *file, void  *priv,
209 					struct v4l2_fmtdesc *f)
210 {
211 	if (f->index != 0)
212 		return -EINVAL;
213 
214 	strlcpy(f->description, "MPEG TS", sizeof(f->description));
215 	f->pixelformat = V4L2_PIX_FMT_MPEG;
216 
217 	return 0;
218 }
219 
220 static int empress_g_fmt_vid_cap(struct file *file, void *priv,
221 				struct v4l2_format *f)
222 {
223 	struct saa7134_dev *dev = file->private_data;
224 	struct v4l2_mbus_framefmt mbus_fmt;
225 
226 	saa_call_all(dev, video, g_mbus_fmt, &mbus_fmt);
227 
228 	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
229 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
230 	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
231 
232 	return 0;
233 }
234 
235 static int empress_s_fmt_vid_cap(struct file *file, void *priv,
236 				struct v4l2_format *f)
237 {
238 	struct saa7134_dev *dev = file->private_data;
239 	struct v4l2_mbus_framefmt mbus_fmt;
240 
241 	v4l2_fill_mbus_format(&mbus_fmt, &f->fmt.pix, V4L2_MBUS_FMT_FIXED);
242 	saa_call_all(dev, video, s_mbus_fmt, &mbus_fmt);
243 	v4l2_fill_pix_format(&f->fmt.pix, &mbus_fmt);
244 
245 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
246 	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
247 
248 	return 0;
249 }
250 
251 static int empress_try_fmt_vid_cap(struct file *file, void *priv,
252 				struct v4l2_format *f)
253 {
254 	struct saa7134_dev *dev = file->private_data;
255 
256 	f->fmt.pix.pixelformat  = V4L2_PIX_FMT_MPEG;
257 	f->fmt.pix.sizeimage    = TS_PACKET_SIZE * dev->ts.nr_packets;
258 
259 	return 0;
260 }
261 
262 static int empress_reqbufs(struct file *file, void *priv,
263 					struct v4l2_requestbuffers *p)
264 {
265 	struct saa7134_dev *dev = file->private_data;
266 
267 	return videobuf_reqbufs(&dev->empress_tsq, p);
268 }
269 
270 static int empress_querybuf(struct file *file, void *priv,
271 					struct v4l2_buffer *b)
272 {
273 	struct saa7134_dev *dev = file->private_data;
274 
275 	return videobuf_querybuf(&dev->empress_tsq, b);
276 }
277 
278 static int empress_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
279 {
280 	struct saa7134_dev *dev = file->private_data;
281 
282 	return videobuf_qbuf(&dev->empress_tsq, b);
283 }
284 
285 static int empress_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
286 {
287 	struct saa7134_dev *dev = file->private_data;
288 
289 	return videobuf_dqbuf(&dev->empress_tsq, b,
290 				file->f_flags & O_NONBLOCK);
291 }
292 
293 static int empress_streamon(struct file *file, void *priv,
294 					enum v4l2_buf_type type)
295 {
296 	struct saa7134_dev *dev = file->private_data;
297 
298 	return videobuf_streamon(&dev->empress_tsq);
299 }
300 
301 static int empress_streamoff(struct file *file, void *priv,
302 					enum v4l2_buf_type type)
303 {
304 	struct saa7134_dev *dev = file->private_data;
305 
306 	return videobuf_streamoff(&dev->empress_tsq);
307 }
308 
309 static int empress_s_ext_ctrls(struct file *file, void *priv,
310 			       struct v4l2_ext_controls *ctrls)
311 {
312 	struct saa7134_dev *dev = file->private_data;
313 	int err;
314 
315 	/* count == 0 is abused in saa6752hs.c, so that special
316 		case is handled here explicitly. */
317 	if (ctrls->count == 0)
318 		return 0;
319 
320 	if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
321 		return -EINVAL;
322 
323 	err = saa_call_empress(dev, core, s_ext_ctrls, ctrls);
324 	ts_init_encoder(dev);
325 
326 	return err;
327 }
328 
329 static int empress_g_ext_ctrls(struct file *file, void *priv,
330 			       struct v4l2_ext_controls *ctrls)
331 {
332 	struct saa7134_dev *dev = file->private_data;
333 
334 	if (ctrls->ctrl_class != V4L2_CTRL_CLASS_MPEG)
335 		return -EINVAL;
336 	return saa_call_empress(dev, core, g_ext_ctrls, ctrls);
337 }
338 
339 static int empress_g_ctrl(struct file *file, void *priv,
340 					struct v4l2_control *c)
341 {
342 	struct saa7134_dev *dev = file->private_data;
343 
344 	return saa7134_g_ctrl_internal(dev, NULL, c);
345 }
346 
347 static int empress_s_ctrl(struct file *file, void *priv,
348 					struct v4l2_control *c)
349 {
350 	struct saa7134_dev *dev = file->private_data;
351 
352 	return saa7134_s_ctrl_internal(dev, NULL, c);
353 }
354 
355 static int empress_queryctrl(struct file *file, void *priv,
356 					struct v4l2_queryctrl *c)
357 {
358 	/* Must be sorted from low to high control ID! */
359 	static const u32 user_ctrls[] = {
360 		V4L2_CID_USER_CLASS,
361 		V4L2_CID_BRIGHTNESS,
362 		V4L2_CID_CONTRAST,
363 		V4L2_CID_SATURATION,
364 		V4L2_CID_HUE,
365 		V4L2_CID_AUDIO_VOLUME,
366 		V4L2_CID_AUDIO_MUTE,
367 		V4L2_CID_HFLIP,
368 		0
369 	};
370 
371 	/* Must be sorted from low to high control ID! */
372 	static const u32 mpeg_ctrls[] = {
373 		V4L2_CID_MPEG_CLASS,
374 		V4L2_CID_MPEG_STREAM_TYPE,
375 		V4L2_CID_MPEG_STREAM_PID_PMT,
376 		V4L2_CID_MPEG_STREAM_PID_AUDIO,
377 		V4L2_CID_MPEG_STREAM_PID_VIDEO,
378 		V4L2_CID_MPEG_STREAM_PID_PCR,
379 		V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
380 		V4L2_CID_MPEG_AUDIO_ENCODING,
381 		V4L2_CID_MPEG_AUDIO_L2_BITRATE,
382 		V4L2_CID_MPEG_VIDEO_ENCODING,
383 		V4L2_CID_MPEG_VIDEO_ASPECT,
384 		V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
385 		V4L2_CID_MPEG_VIDEO_BITRATE,
386 		V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
387 		0
388 	};
389 	static const u32 *ctrl_classes[] = {
390 		user_ctrls,
391 		mpeg_ctrls,
392 		NULL
393 	};
394 	struct saa7134_dev *dev = file->private_data;
395 
396 	c->id = v4l2_ctrl_next(ctrl_classes, c->id);
397 	if (c->id == 0)
398 		return -EINVAL;
399 	if (c->id == V4L2_CID_USER_CLASS || c->id == V4L2_CID_MPEG_CLASS)
400 		return v4l2_ctrl_query_fill(c, 0, 0, 0, 0);
401 	if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
402 		return saa7134_queryctrl(file, priv, c);
403 	return saa_call_empress(dev, core, queryctrl, c);
404 }
405 
406 static int empress_querymenu(struct file *file, void *priv,
407 					struct v4l2_querymenu *c)
408 {
409 	struct saa7134_dev *dev = file->private_data;
410 
411 	if (V4L2_CTRL_ID2CLASS(c->id) != V4L2_CTRL_CLASS_MPEG)
412 		return -EINVAL;
413 	return saa_call_empress(dev, core, querymenu, c);
414 }
415 
416 static int empress_g_chip_ident(struct file *file, void *fh,
417 	       struct v4l2_dbg_chip_ident *chip)
418 {
419 	struct saa7134_dev *dev = file->private_data;
420 
421 	chip->ident = V4L2_IDENT_NONE;
422 	chip->revision = 0;
423 	if (chip->match.type == V4L2_CHIP_MATCH_I2C_DRIVER &&
424 	    !strcmp(chip->match.name, "saa6752hs"))
425 		return saa_call_empress(dev, core, g_chip_ident, chip);
426 	if (chip->match.type == V4L2_CHIP_MATCH_I2C_ADDR)
427 		return saa_call_empress(dev, core, g_chip_ident, chip);
428 	return -EINVAL;
429 }
430 
431 static int empress_s_std(struct file *file, void *priv, v4l2_std_id *id)
432 {
433 	struct saa7134_dev *dev = file->private_data;
434 
435 	return saa7134_s_std_internal(dev, NULL, id);
436 }
437 
438 static int empress_g_std(struct file *file, void *priv, v4l2_std_id *id)
439 {
440 	struct saa7134_dev *dev = file->private_data;
441 
442 	*id = dev->tvnorm->id;
443 	return 0;
444 }
445 
446 static const struct v4l2_file_operations ts_fops =
447 {
448 	.owner	  = THIS_MODULE,
449 	.open	  = ts_open,
450 	.release  = ts_release,
451 	.read	  = ts_read,
452 	.poll	  = ts_poll,
453 	.mmap	  = ts_mmap,
454 	.ioctl	  = video_ioctl2,
455 };
456 
457 static const struct v4l2_ioctl_ops ts_ioctl_ops = {
458 	.vidioc_querycap		= empress_querycap,
459 	.vidioc_enum_fmt_vid_cap	= empress_enum_fmt_vid_cap,
460 	.vidioc_try_fmt_vid_cap		= empress_try_fmt_vid_cap,
461 	.vidioc_s_fmt_vid_cap		= empress_s_fmt_vid_cap,
462 	.vidioc_g_fmt_vid_cap		= empress_g_fmt_vid_cap,
463 	.vidioc_reqbufs			= empress_reqbufs,
464 	.vidioc_querybuf		= empress_querybuf,
465 	.vidioc_qbuf			= empress_qbuf,
466 	.vidioc_dqbuf			= empress_dqbuf,
467 	.vidioc_streamon		= empress_streamon,
468 	.vidioc_streamoff		= empress_streamoff,
469 	.vidioc_s_ext_ctrls		= empress_s_ext_ctrls,
470 	.vidioc_g_ext_ctrls		= empress_g_ext_ctrls,
471 	.vidioc_enum_input		= empress_enum_input,
472 	.vidioc_g_input			= empress_g_input,
473 	.vidioc_s_input			= empress_s_input,
474 	.vidioc_queryctrl		= empress_queryctrl,
475 	.vidioc_querymenu		= empress_querymenu,
476 	.vidioc_g_ctrl			= empress_g_ctrl,
477 	.vidioc_s_ctrl			= empress_s_ctrl,
478 	.vidioc_g_chip_ident 		= empress_g_chip_ident,
479 	.vidioc_s_std			= empress_s_std,
480 	.vidioc_g_std			= empress_g_std,
481 };
482 
483 /* ----------------------------------------------------------- */
484 
485 static struct video_device saa7134_empress_template = {
486 	.name          = "saa7134-empress",
487 	.fops          = &ts_fops,
488 	.ioctl_ops     = &ts_ioctl_ops,
489 
490 	.tvnorms			= SAA7134_NORMS,
491 	.current_norm			= V4L2_STD_PAL,
492 };
493 
494 static void empress_signal_update(struct work_struct *work)
495 {
496 	struct saa7134_dev* dev =
497 		container_of(work, struct saa7134_dev, empress_workqueue);
498 
499 	if (dev->nosignal) {
500 		dprintk("no video signal\n");
501 	} else {
502 		dprintk("video signal acquired\n");
503 	}
504 }
505 
506 static void empress_signal_change(struct saa7134_dev *dev)
507 {
508 	schedule_work(&dev->empress_workqueue);
509 }
510 
511 
512 static int empress_init(struct saa7134_dev *dev)
513 {
514 	int err;
515 
516 	dprintk("%s: %s\n",dev->name,__func__);
517 	dev->empress_dev = video_device_alloc();
518 	if (NULL == dev->empress_dev)
519 		return -ENOMEM;
520 	*(dev->empress_dev) = saa7134_empress_template;
521 	dev->empress_dev->parent  = &dev->pci->dev;
522 	dev->empress_dev->release = video_device_release;
523 	snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
524 		 "%s empress (%s)", dev->name,
525 		 saa7134_boards[dev->board].name);
526 
527 	INIT_WORK(&dev->empress_workqueue, empress_signal_update);
528 
529 	video_set_drvdata(dev->empress_dev, dev);
530 	err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
531 				    empress_nr[dev->nr]);
532 	if (err < 0) {
533 		printk(KERN_INFO "%s: can't register video device\n",
534 		       dev->name);
535 		video_device_release(dev->empress_dev);
536 		dev->empress_dev = NULL;
537 		return err;
538 	}
539 	printk(KERN_INFO "%s: registered device %s [mpeg]\n",
540 	       dev->name, video_device_node_name(dev->empress_dev));
541 
542 	videobuf_queue_sg_init(&dev->empress_tsq, &saa7134_ts_qops,
543 			    &dev->pci->dev, &dev->slock,
544 			    V4L2_BUF_TYPE_VIDEO_CAPTURE,
545 			    V4L2_FIELD_ALTERNATE,
546 			    sizeof(struct saa7134_buf),
547 			    dev, NULL);
548 
549 	empress_signal_update(&dev->empress_workqueue);
550 	return 0;
551 }
552 
553 static int empress_fini(struct saa7134_dev *dev)
554 {
555 	dprintk("%s: %s\n",dev->name,__func__);
556 
557 	if (NULL == dev->empress_dev)
558 		return 0;
559 	flush_work(&dev->empress_workqueue);
560 	video_unregister_device(dev->empress_dev);
561 	dev->empress_dev = NULL;
562 	return 0;
563 }
564 
565 static struct saa7134_mpeg_ops empress_ops = {
566 	.type          = SAA7134_MPEG_EMPRESS,
567 	.init          = empress_init,
568 	.fini          = empress_fini,
569 	.signal_change = empress_signal_change,
570 };
571 
572 static int __init empress_register(void)
573 {
574 	return saa7134_ts_register(&empress_ops);
575 }
576 
577 static void __exit empress_unregister(void)
578 {
579 	saa7134_ts_unregister(&empress_ops);
580 }
581 
582 module_init(empress_register);
583 module_exit(empress_unregister);
584 
585 /* ----------------------------------------------------------- */
586 /*
587  * Local variables:
588  * c-basic-offset: 8
589  * End:
590  */
591