saa7134-empress.c 9.9 KB

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