3 * (c) 2004 Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]
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.
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.
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.
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>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-event.h>
29 #include "saa7134-reg.h"
32 /* ------------------------------------------------------------------ */
34 MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
35 MODULE_LICENSE("GPL");
37 static unsigned int empress_nr
[] = {[0 ... (SAA7134_MAXBOARDS
- 1)] = UNSET
};
39 module_param_array(empress_nr
, int, NULL
, 0444);
40 MODULE_PARM_DESC(empress_nr
,"ts device number");
42 static unsigned int debug
;
43 module_param(debug
, int, 0644);
44 MODULE_PARM_DESC(debug
,"enable debug messages");
46 #define dprintk(fmt, arg...) if (debug) \
47 printk(KERN_DEBUG "%s/empress: " fmt, dev->name , ## arg)
49 /* ------------------------------------------------------------------ */
51 static void ts_reset_encoder(struct saa7134_dev
* dev
)
53 if (!dev
->empress_started
)
56 saa_writeb(SAA7134_SPECIAL_MODE
, 0x00);
58 saa_writeb(SAA7134_SPECIAL_MODE
, 0x01);
60 dev
->empress_started
= 0;
63 static int ts_init_encoder(struct saa7134_dev
* dev
)
65 u32 leading_null_bytes
= 0;
67 /* If more cards start to need this, then this
68 should probably be added to the card definitions. */
70 case SAA7134_BOARD_BEHOLD_M6
:
71 case SAA7134_BOARD_BEHOLD_M63
:
72 case SAA7134_BOARD_BEHOLD_M6_EXTRA
:
73 leading_null_bytes
= 1;
76 ts_reset_encoder(dev
);
77 saa_call_all(dev
, core
, init
, leading_null_bytes
);
78 dev
->empress_started
= 1;
82 /* ------------------------------------------------------------------ */
84 static int ts_open(struct file
*file
)
86 struct video_device
*vdev
= video_devdata(file
);
87 struct saa7134_dev
*dev
= video_drvdata(file
);
88 struct saa7134_fh
*fh
;
90 /* allocate + initialize per filehandle data */
91 fh
= kzalloc(sizeof(*fh
), GFP_KERNEL
);
95 v4l2_fh_init(&fh
->fh
, vdev
);
96 file
->private_data
= fh
;
97 fh
->is_empress
= true;
101 saa_writeb(SAA7134_AUDIO_MUTE_CTRL
,
102 saa_readb(SAA7134_AUDIO_MUTE_CTRL
) & ~(1 << 6));
107 static int ts_release(struct file
*file
)
109 struct saa7134_dev
*dev
= video_drvdata(file
);
110 struct saa7134_fh
*fh
= file
->private_data
;
112 if (res_check(fh
, RESOURCE_EMPRESS
)) {
113 videobuf_stop(&dev
->empress_tsq
);
114 videobuf_mmap_free(&dev
->empress_tsq
);
116 /* stop the encoder */
117 ts_reset_encoder(dev
);
120 saa_writeb(SAA7134_AUDIO_MUTE_CTRL
,
121 saa_readb(SAA7134_AUDIO_MUTE_CTRL
) | (1 << 6));
124 v4l2_fh_del(&fh
->fh
);
125 v4l2_fh_exit(&fh
->fh
);
130 ts_read(struct file
*file
, char __user
*data
, size_t count
, loff_t
*ppos
)
132 struct saa7134_dev
*dev
= video_drvdata(file
);
134 if (res_locked(dev
, RESOURCE_EMPRESS
))
136 if (!dev
->empress_started
)
137 ts_init_encoder(dev
);
139 return videobuf_read_stream(&dev
->empress_tsq
,
140 data
, count
, ppos
, 0,
141 file
->f_flags
& O_NONBLOCK
);
145 ts_poll(struct file
*file
, struct poll_table_struct
*wait
)
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
;
152 if (v4l2_event_pending(&fh
->fh
))
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
);
161 ts_mmap(struct file
*file
, struct vm_area_struct
* vma
)
163 struct saa7134_dev
*dev
= video_drvdata(file
);
165 return videobuf_mmap_mapper(&dev
->empress_tsq
, vma
);
168 static int empress_enum_fmt_vid_cap(struct file
*file
, void *priv
,
169 struct v4l2_fmtdesc
*f
)
174 strlcpy(f
->description
, "MPEG TS", sizeof(f
->description
));
175 f
->pixelformat
= V4L2_PIX_FMT_MPEG
;
176 f
->flags
= V4L2_FMT_FLAG_COMPRESSED
;
180 static int empress_g_fmt_vid_cap(struct file
*file
, void *priv
,
181 struct v4l2_format
*f
)
183 struct saa7134_dev
*dev
= video_drvdata(file
);
184 struct v4l2_mbus_framefmt mbus_fmt
;
186 saa_call_all(dev
, video
, g_mbus_fmt
, &mbus_fmt
);
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;
197 static int empress_s_fmt_vid_cap(struct file
*file
, void *priv
,
198 struct v4l2_format
*f
)
200 struct saa7134_dev
*dev
= video_drvdata(file
);
201 struct v4l2_mbus_framefmt mbus_fmt
;
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
);
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;
215 static int empress_try_fmt_vid_cap(struct file
*file
, void *priv
,
216 struct v4l2_format
*f
)
218 struct saa7134_dev
*dev
= video_drvdata(file
);
219 struct v4l2_mbus_framefmt mbus_fmt
;
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
);
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;
233 static const struct v4l2_file_operations ts_fops
=
235 .owner
= THIS_MODULE
,
237 .release
= ts_release
,
241 .ioctl
= video_ioctl2
,
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
,
270 /* ----------------------------------------------------------- */
272 static struct video_device saa7134_empress_template
= {
273 .name
= "saa7134-empress",
275 .ioctl_ops
= &ts_ioctl_ops
,
277 .tvnorms
= SAA7134_NORMS
,
280 static void empress_signal_update(struct work_struct
*work
)
282 struct saa7134_dev
* dev
=
283 container_of(work
, struct saa7134_dev
, empress_workqueue
);
286 dprintk("no video signal\n");
288 dprintk("video signal acquired\n");
292 static void empress_signal_change(struct saa7134_dev
*dev
)
294 schedule_work(&dev
->empress_workqueue
);
297 static bool empress_ctrl_filter(const struct v4l2_ctrl
*ctrl
)
300 case V4L2_CID_BRIGHTNESS
:
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
:
314 static int empress_init(struct saa7134_dev
*dev
)
316 struct v4l2_ctrl_handler
*hdl
= &dev
->empress_ctrl_handler
;
319 dprintk("%s: %s\n",dev
->name
,__func__
);
320 dev
->empress_dev
= video_device_alloc();
321 if (NULL
== dev
->empress_dev
)
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
);
333 v4l2_ctrl_add_handler(hdl
, dev
->empress_sd
->ctrl_handler
, NULL
);
335 video_device_release(dev
->empress_dev
);
338 dev
->empress_dev
->ctrl_handler
= hdl
;
340 INIT_WORK(&dev
->empress_workqueue
, empress_signal_update
);
342 video_set_drvdata(dev
->empress_dev
, dev
);
343 err
= video_register_device(dev
->empress_dev
,VFL_TYPE_GRABBER
,
344 empress_nr
[dev
->nr
]);
346 printk(KERN_INFO
"%s: can't register video device\n",
348 video_device_release(dev
->empress_dev
);
349 dev
->empress_dev
= NULL
;
352 printk(KERN_INFO
"%s: registered device %s [mpeg]\n",
353 dev
->name
, video_device_node_name(dev
->empress_dev
));
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
),
362 empress_signal_update(&dev
->empress_workqueue
);
366 static int empress_fini(struct saa7134_dev
*dev
)
368 dprintk("%s: %s\n",dev
->name
,__func__
);
370 if (NULL
== dev
->empress_dev
)
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
;
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
,
386 static int __init
empress_register(void)
388 return saa7134_ts_register(&empress_ops
);
391 static void __exit
empress_unregister(void)
393 saa7134_ts_unregister(&empress_ops
);
396 module_init(empress_register
);
397 module_exit(empress_unregister
);
399 /* ----------------------------------------------------------- */