saa7134-empress.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. #include "saa7134.h"
  16. #include "saa7134-reg.h"
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <media/v4l2-common.h>
  23. #include <media/v4l2-event.h>
  24. /* ------------------------------------------------------------------ */
  25. MODULE_AUTHOR("Gerd Knorr <kraxel@bytesex.org> [SuSE Labs]");
  26. MODULE_LICENSE("GPL");
  27. static unsigned int empress_nr[] = {[0 ... (SAA7134_MAXBOARDS - 1)] = UNSET };
  28. module_param_array(empress_nr, int, NULL, 0444);
  29. MODULE_PARM_DESC(empress_nr,"ts device number");
  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. err = saa7134_ts_start_streaming(vq, count);
  38. if (err)
  39. return err;
  40. /* If more cards start to need this, then this
  41. should probably be added to the card definitions. */
  42. switch (dev->board) {
  43. case SAA7134_BOARD_BEHOLD_M6:
  44. case SAA7134_BOARD_BEHOLD_M63:
  45. case SAA7134_BOARD_BEHOLD_M6_EXTRA:
  46. leading_null_bytes = 1;
  47. break;
  48. }
  49. saa_call_all(dev, core, init, leading_null_bytes);
  50. /* Unmute audio */
  51. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  52. saa_readb(SAA7134_AUDIO_MUTE_CTRL) & ~(1 << 6));
  53. dev->empress_started = 1;
  54. return 0;
  55. }
  56. static void stop_streaming(struct vb2_queue *vq)
  57. {
  58. struct saa7134_dmaqueue *dmaq = vq->drv_priv;
  59. struct saa7134_dev *dev = dmaq->dev;
  60. saa7134_ts_stop_streaming(vq);
  61. saa_writeb(SAA7134_SPECIAL_MODE, 0x00);
  62. msleep(20);
  63. saa_writeb(SAA7134_SPECIAL_MODE, 0x01);
  64. msleep(100);
  65. /* Mute audio */
  66. saa_writeb(SAA7134_AUDIO_MUTE_CTRL,
  67. saa_readb(SAA7134_AUDIO_MUTE_CTRL) | (1 << 6));
  68. dev->empress_started = 0;
  69. }
  70. static const struct vb2_ops saa7134_empress_qops = {
  71. .queue_setup = saa7134_ts_queue_setup,
  72. .buf_init = saa7134_ts_buffer_init,
  73. .buf_prepare = saa7134_ts_buffer_prepare,
  74. .buf_queue = saa7134_vb2_buffer_queue,
  75. .wait_prepare = vb2_ops_wait_prepare,
  76. .wait_finish = vb2_ops_wait_finish,
  77. .start_streaming = start_streaming,
  78. .stop_streaming = stop_streaming,
  79. };
  80. /* ------------------------------------------------------------------ */
  81. static int empress_enum_fmt_vid_cap(struct file *file, void *priv,
  82. struct v4l2_fmtdesc *f)
  83. {
  84. if (f->index != 0)
  85. return -EINVAL;
  86. strlcpy(f->description, "MPEG TS", sizeof(f->description));
  87. f->pixelformat = V4L2_PIX_FMT_MPEG;
  88. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  89. return 0;
  90. }
  91. static int empress_g_fmt_vid_cap(struct file *file, void *priv,
  92. struct v4l2_format *f)
  93. {
  94. struct saa7134_dev *dev = video_drvdata(file);
  95. struct v4l2_subdev_format fmt = {
  96. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  97. };
  98. struct v4l2_mbus_framefmt *mbus_fmt = &fmt.format;
  99. saa_call_all(dev, pad, get_fmt, NULL, &fmt);
  100. v4l2_fill_pix_format(&f->fmt.pix, mbus_fmt);
  101. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  102. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  103. f->fmt.pix.bytesperline = 0;
  104. return 0;
  105. }
  106. static int empress_s_fmt_vid_cap(struct file *file, void *priv,
  107. struct v4l2_format *f)
  108. {
  109. struct saa7134_dev *dev = video_drvdata(file);
  110. struct v4l2_subdev_format format = {
  111. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  112. };
  113. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
  114. saa_call_all(dev, pad, set_fmt, NULL, &format);
  115. v4l2_fill_pix_format(&f->fmt.pix, &format.format);
  116. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  117. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  118. f->fmt.pix.bytesperline = 0;
  119. return 0;
  120. }
  121. static int empress_try_fmt_vid_cap(struct file *file, void *priv,
  122. struct v4l2_format *f)
  123. {
  124. struct saa7134_dev *dev = video_drvdata(file);
  125. struct v4l2_subdev_pad_config pad_cfg;
  126. struct v4l2_subdev_format format = {
  127. .which = V4L2_SUBDEV_FORMAT_TRY,
  128. };
  129. v4l2_fill_mbus_format(&format.format, &f->fmt.pix, MEDIA_BUS_FMT_FIXED);
  130. saa_call_all(dev, pad, set_fmt, &pad_cfg, &format);
  131. v4l2_fill_pix_format(&f->fmt.pix, &format.format);
  132. f->fmt.pix.pixelformat = V4L2_PIX_FMT_MPEG;
  133. f->fmt.pix.sizeimage = TS_PACKET_SIZE * dev->ts.nr_packets;
  134. f->fmt.pix.bytesperline = 0;
  135. return 0;
  136. }
  137. static const struct v4l2_file_operations ts_fops =
  138. {
  139. .owner = THIS_MODULE,
  140. .open = v4l2_fh_open,
  141. .release = vb2_fop_release,
  142. .read = vb2_fop_read,
  143. .poll = vb2_fop_poll,
  144. .mmap = vb2_fop_mmap,
  145. .unlocked_ioctl = video_ioctl2,
  146. };
  147. static const struct v4l2_ioctl_ops ts_ioctl_ops = {
  148. .vidioc_querycap = saa7134_querycap,
  149. .vidioc_enum_fmt_vid_cap = empress_enum_fmt_vid_cap,
  150. .vidioc_try_fmt_vid_cap = empress_try_fmt_vid_cap,
  151. .vidioc_s_fmt_vid_cap = empress_s_fmt_vid_cap,
  152. .vidioc_g_fmt_vid_cap = empress_g_fmt_vid_cap,
  153. .vidioc_reqbufs = vb2_ioctl_reqbufs,
  154. .vidioc_querybuf = vb2_ioctl_querybuf,
  155. .vidioc_qbuf = vb2_ioctl_qbuf,
  156. .vidioc_dqbuf = vb2_ioctl_dqbuf,
  157. .vidioc_expbuf = vb2_ioctl_expbuf,
  158. .vidioc_streamon = vb2_ioctl_streamon,
  159. .vidioc_streamoff = vb2_ioctl_streamoff,
  160. .vidioc_g_frequency = saa7134_g_frequency,
  161. .vidioc_s_frequency = saa7134_s_frequency,
  162. .vidioc_g_tuner = saa7134_g_tuner,
  163. .vidioc_s_tuner = saa7134_s_tuner,
  164. .vidioc_enum_input = saa7134_enum_input,
  165. .vidioc_g_input = saa7134_g_input,
  166. .vidioc_s_input = saa7134_s_input,
  167. .vidioc_s_std = saa7134_s_std,
  168. .vidioc_g_std = saa7134_g_std,
  169. .vidioc_querystd = saa7134_querystd,
  170. .vidioc_log_status = v4l2_ctrl_log_status,
  171. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  172. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  173. };
  174. /* ----------------------------------------------------------- */
  175. static const struct video_device saa7134_empress_template = {
  176. .name = "saa7134-empress",
  177. .fops = &ts_fops,
  178. .ioctl_ops = &ts_ioctl_ops,
  179. .tvnorms = SAA7134_NORMS,
  180. };
  181. static void empress_signal_update(struct work_struct *work)
  182. {
  183. struct saa7134_dev* dev =
  184. container_of(work, struct saa7134_dev, empress_workqueue);
  185. if (dev->nosignal) {
  186. pr_debug("no video signal\n");
  187. } else {
  188. pr_debug("video signal acquired\n");
  189. }
  190. }
  191. static void empress_signal_change(struct saa7134_dev *dev)
  192. {
  193. schedule_work(&dev->empress_workqueue);
  194. }
  195. static bool empress_ctrl_filter(const struct v4l2_ctrl *ctrl)
  196. {
  197. switch (ctrl->id) {
  198. case V4L2_CID_BRIGHTNESS:
  199. case V4L2_CID_HUE:
  200. case V4L2_CID_CONTRAST:
  201. case V4L2_CID_SATURATION:
  202. case V4L2_CID_AUDIO_MUTE:
  203. case V4L2_CID_AUDIO_VOLUME:
  204. case V4L2_CID_PRIVATE_INVERT:
  205. case V4L2_CID_PRIVATE_AUTOMUTE:
  206. return true;
  207. default:
  208. return false;
  209. }
  210. }
  211. static int empress_init(struct saa7134_dev *dev)
  212. {
  213. struct v4l2_ctrl_handler *hdl = &dev->empress_ctrl_handler;
  214. struct vb2_queue *q;
  215. int err;
  216. pr_debug("%s: %s\n", dev->name, __func__);
  217. dev->empress_dev = video_device_alloc();
  218. if (NULL == dev->empress_dev)
  219. return -ENOMEM;
  220. *(dev->empress_dev) = saa7134_empress_template;
  221. dev->empress_dev->v4l2_dev = &dev->v4l2_dev;
  222. dev->empress_dev->release = video_device_release;
  223. dev->empress_dev->lock = &dev->lock;
  224. snprintf(dev->empress_dev->name, sizeof(dev->empress_dev->name),
  225. "%s empress (%s)", dev->name,
  226. saa7134_boards[dev->board].name);
  227. v4l2_ctrl_handler_init(hdl, 21);
  228. v4l2_ctrl_add_handler(hdl, &dev->ctrl_handler, empress_ctrl_filter);
  229. if (dev->empress_sd)
  230. v4l2_ctrl_add_handler(hdl, dev->empress_sd->ctrl_handler, NULL);
  231. if (hdl->error) {
  232. video_device_release(dev->empress_dev);
  233. return hdl->error;
  234. }
  235. dev->empress_dev->ctrl_handler = hdl;
  236. INIT_WORK(&dev->empress_workqueue, empress_signal_update);
  237. q = &dev->empress_vbq;
  238. q->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  239. /*
  240. * Do not add VB2_USERPTR: the saa7134 DMA engine cannot handle
  241. * transfers that do not start at the beginning of a page. A USERPTR
  242. * can start anywhere in a page, so USERPTR support is a no-go.
  243. */
  244. q->io_modes = VB2_MMAP | VB2_DMABUF | VB2_READ;
  245. q->drv_priv = &dev->ts_q;
  246. q->ops = &saa7134_empress_qops;
  247. q->gfp_flags = GFP_DMA32;
  248. q->mem_ops = &vb2_dma_sg_memops;
  249. q->buf_struct_size = sizeof(struct saa7134_buf);
  250. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  251. q->lock = &dev->lock;
  252. q->dev = &dev->pci->dev;
  253. err = vb2_queue_init(q);
  254. if (err)
  255. return err;
  256. dev->empress_dev->queue = q;
  257. video_set_drvdata(dev->empress_dev, dev);
  258. err = video_register_device(dev->empress_dev,VFL_TYPE_GRABBER,
  259. empress_nr[dev->nr]);
  260. if (err < 0) {
  261. pr_info("%s: can't register video device\n",
  262. dev->name);
  263. video_device_release(dev->empress_dev);
  264. dev->empress_dev = NULL;
  265. return err;
  266. }
  267. pr_info("%s: registered device %s [mpeg]\n",
  268. dev->name, video_device_node_name(dev->empress_dev));
  269. empress_signal_update(&dev->empress_workqueue);
  270. return 0;
  271. }
  272. static int empress_fini(struct saa7134_dev *dev)
  273. {
  274. pr_debug("%s: %s\n", dev->name, __func__);
  275. if (NULL == dev->empress_dev)
  276. return 0;
  277. flush_work(&dev->empress_workqueue);
  278. video_unregister_device(dev->empress_dev);
  279. vb2_queue_release(&dev->empress_vbq);
  280. v4l2_ctrl_handler_free(&dev->empress_ctrl_handler);
  281. dev->empress_dev = NULL;
  282. return 0;
  283. }
  284. static struct saa7134_mpeg_ops empress_ops = {
  285. .type = SAA7134_MPEG_EMPRESS,
  286. .init = empress_init,
  287. .fini = empress_fini,
  288. .signal_change = empress_signal_change,
  289. };
  290. static int __init empress_register(void)
  291. {
  292. return saa7134_ts_register(&empress_ops);
  293. }
  294. static void __exit empress_unregister(void)
  295. {
  296. saa7134_ts_unregister(&empress_ops);
  297. }
  298. module_init(empress_register);
  299. module_exit(empress_unregister);