saa7146_fops.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <media/drv-intf/saa7146_vv.h>
  4. #include <linux/module.h>
  5. /****************************************************************************/
  6. /* resource management functions, shamelessly stolen from saa7134 driver */
  7. int saa7146_res_get(struct saa7146_dev *dev, unsigned int bit)
  8. {
  9. struct saa7146_vv *vv = dev->vv_data;
  10. if (vv->resources & bit) {
  11. DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n",
  12. bit, vv->resources);
  13. /* have it already allocated */
  14. return 1;
  15. }
  16. /* is it free? */
  17. if (vv->resources & bit) {
  18. DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n",
  19. vv->resources, bit);
  20. /* no, someone else uses it */
  21. return 0;
  22. }
  23. /* it's free, grab it */
  24. vv->resources |= bit;
  25. DEB_D("res: get 0x%02x, cur:0x%02x\n", bit, vv->resources);
  26. return 1;
  27. }
  28. void saa7146_res_free(struct saa7146_dev *dev, unsigned int bits)
  29. {
  30. struct saa7146_vv *vv = dev->vv_data;
  31. WARN_ON((vv->resources & bits) != bits);
  32. vv->resources &= ~bits;
  33. DEB_D("res: put 0x%02x, cur:0x%02x\n", bits, vv->resources);
  34. }
  35. /********************************************************************************/
  36. /* common buffer functions */
  37. int saa7146_buffer_queue(struct saa7146_dev *dev,
  38. struct saa7146_dmaqueue *q,
  39. struct saa7146_buf *buf)
  40. {
  41. assert_spin_locked(&dev->slock);
  42. DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf);
  43. if (WARN_ON(!q))
  44. return -EIO;
  45. if (NULL == q->curr) {
  46. q->curr = buf;
  47. DEB_D("immediately activating buffer %p\n", buf);
  48. buf->activate(dev,buf,NULL);
  49. } else {
  50. list_add_tail(&buf->list, &q->queue);
  51. DEB_D("adding buffer %p to queue. (active buffer present)\n",
  52. buf);
  53. }
  54. return 0;
  55. }
  56. void saa7146_buffer_finish(struct saa7146_dev *dev,
  57. struct saa7146_dmaqueue *q,
  58. int state)
  59. {
  60. struct saa7146_vv *vv = dev->vv_data;
  61. struct saa7146_buf *buf = q->curr;
  62. assert_spin_locked(&dev->slock);
  63. DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev, q, state);
  64. DEB_EE("q->curr:%p\n", q->curr);
  65. /* finish current buffer */
  66. if (!buf) {
  67. DEB_D("aiii. no current buffer\n");
  68. return;
  69. }
  70. q->curr = NULL;
  71. buf->vb.vb2_buf.timestamp = ktime_get_ns();
  72. if (vv->video_fmt.field == V4L2_FIELD_ALTERNATE)
  73. buf->vb.field = vv->last_field;
  74. else if (vv->video_fmt.field == V4L2_FIELD_ANY)
  75. buf->vb.field = (vv->video_fmt.height > vv->standard->v_max_out / 2)
  76. ? V4L2_FIELD_INTERLACED
  77. : V4L2_FIELD_BOTTOM;
  78. else
  79. buf->vb.field = vv->video_fmt.field;
  80. buf->vb.sequence = vv->seqnr++;
  81. vb2_buffer_done(&buf->vb.vb2_buf, state);
  82. }
  83. void saa7146_buffer_next(struct saa7146_dev *dev,
  84. struct saa7146_dmaqueue *q, int vbi)
  85. {
  86. struct saa7146_buf *buf,*next = NULL;
  87. if (WARN_ON(!q))
  88. return;
  89. DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi);
  90. assert_spin_locked(&dev->slock);
  91. if (!list_empty(&q->queue)) {
  92. /* activate next one from queue */
  93. buf = list_entry(q->queue.next, struct saa7146_buf, list);
  94. list_del(&buf->list);
  95. if (!list_empty(&q->queue))
  96. next = list_entry(q->queue.next, struct saa7146_buf, list);
  97. q->curr = buf;
  98. DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n",
  99. buf, q->queue.prev, q->queue.next);
  100. buf->activate(dev,buf,next);
  101. } else {
  102. DEB_INT("no next buffer. stopping.\n");
  103. if( 0 != vbi ) {
  104. /* turn off video-dma3 */
  105. saa7146_write(dev,MC1, MASK_20);
  106. } else {
  107. /* nothing to do -- just prevent next video-dma1 transfer
  108. by lowering the protection address */
  109. // fixme: fix this for vflip != 0
  110. saa7146_write(dev, PROT_ADDR1, 0);
  111. saa7146_write(dev, MC2, (MASK_02|MASK_18));
  112. /* write the address of the rps-program */
  113. saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
  114. /* turn on rps */
  115. saa7146_write(dev, MC1, (MASK_12 | MASK_28));
  116. /*
  117. printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
  118. printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
  119. printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
  120. printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
  121. printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
  122. printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
  123. */
  124. }
  125. del_timer(&q->timeout);
  126. }
  127. }
  128. void saa7146_buffer_timeout(struct timer_list *t)
  129. {
  130. struct saa7146_dmaqueue *q = from_timer(q, t, timeout);
  131. struct saa7146_dev *dev = q->dev;
  132. unsigned long flags;
  133. DEB_EE("dev:%p, dmaq:%p\n", dev, q);
  134. spin_lock_irqsave(&dev->slock,flags);
  135. if (q->curr) {
  136. DEB_D("timeout on %p\n", q->curr);
  137. saa7146_buffer_finish(dev, q, VB2_BUF_STATE_ERROR);
  138. }
  139. /* we don't restart the transfer here like other drivers do. when
  140. a streaming capture is disabled, the timeout function will be
  141. called for the current buffer. if we activate the next buffer now,
  142. we mess up our capture logic. if a timeout occurs on another buffer,
  143. then something is seriously broken before, so no need to buffer the
  144. next capture IMHO... */
  145. saa7146_buffer_next(dev, q, 0);
  146. spin_unlock_irqrestore(&dev->slock,flags);
  147. }
  148. /********************************************************************************/
  149. /* file operations */
  150. static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
  151. {
  152. struct video_device *vdev = video_devdata(file);
  153. struct saa7146_dev *dev = video_drvdata(file);
  154. int ret;
  155. if (vdev->vfl_type != VFL_TYPE_VBI || !dev->ext_vv_data->vbi_fops.write)
  156. return -EINVAL;
  157. if (mutex_lock_interruptible(vdev->lock))
  158. return -ERESTARTSYS;
  159. ret = dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
  160. mutex_unlock(vdev->lock);
  161. return ret;
  162. }
  163. static const struct v4l2_file_operations video_fops =
  164. {
  165. .owner = THIS_MODULE,
  166. .open = v4l2_fh_open,
  167. .release = vb2_fop_release,
  168. .read = vb2_fop_read,
  169. .write = fops_write,
  170. .poll = vb2_fop_poll,
  171. .mmap = vb2_fop_mmap,
  172. .unlocked_ioctl = video_ioctl2,
  173. };
  174. static void vv_callback(struct saa7146_dev *dev, unsigned long status)
  175. {
  176. u32 isr = status;
  177. DEB_INT("dev:%p, isr:0x%08x\n", dev, (u32)status);
  178. if (0 != (isr & (MASK_27))) {
  179. DEB_INT("irq: RPS0 (0x%08x)\n", isr);
  180. saa7146_video_uops.irq_done(dev,isr);
  181. }
  182. if (0 != (isr & (MASK_28))) {
  183. u32 mc2 = saa7146_read(dev, MC2);
  184. if( 0 != (mc2 & MASK_15)) {
  185. DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr);
  186. wake_up(&dev->vv_data->vbi_wq);
  187. saa7146_write(dev,MC2, MASK_31);
  188. return;
  189. }
  190. DEB_INT("irq: RPS1 (0x%08x)\n", isr);
  191. saa7146_vbi_uops.irq_done(dev,isr);
  192. }
  193. }
  194. static const struct v4l2_ctrl_ops saa7146_ctrl_ops = {
  195. .s_ctrl = saa7146_s_ctrl,
  196. };
  197. int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
  198. {
  199. struct v4l2_ctrl_handler *hdl = &dev->ctrl_handler;
  200. struct v4l2_pix_format *fmt;
  201. struct v4l2_vbi_format *vbi;
  202. struct saa7146_vv *vv;
  203. int err;
  204. err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
  205. if (err)
  206. return err;
  207. v4l2_ctrl_handler_init(hdl, 6);
  208. v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
  209. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  210. v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
  211. V4L2_CID_CONTRAST, 0, 127, 1, 64);
  212. v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
  213. V4L2_CID_SATURATION, 0, 127, 1, 64);
  214. v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
  215. V4L2_CID_VFLIP, 0, 1, 1, 0);
  216. v4l2_ctrl_new_std(hdl, &saa7146_ctrl_ops,
  217. V4L2_CID_HFLIP, 0, 1, 1, 0);
  218. if (hdl->error) {
  219. err = hdl->error;
  220. v4l2_ctrl_handler_free(hdl);
  221. v4l2_device_unregister(&dev->v4l2_dev);
  222. return err;
  223. }
  224. dev->v4l2_dev.ctrl_handler = hdl;
  225. vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
  226. if (vv == NULL) {
  227. ERR("out of memory. aborting.\n");
  228. v4l2_ctrl_handler_free(hdl);
  229. v4l2_device_unregister(&dev->v4l2_dev);
  230. return -ENOMEM;
  231. }
  232. ext_vv->vid_ops = saa7146_video_ioctl_ops;
  233. ext_vv->vbi_ops = saa7146_vbi_ioctl_ops;
  234. ext_vv->core_ops = &saa7146_video_ioctl_ops;
  235. DEB_EE("dev:%p\n", dev);
  236. /* set default values for video parts of the saa7146 */
  237. saa7146_write(dev, BCS_CTRL, 0x80400040);
  238. /* enable video-port pins */
  239. saa7146_write(dev, MC1, (MASK_10 | MASK_26));
  240. /* save per-device extension data (one extension can
  241. handle different devices that might need different
  242. configuration data) */
  243. dev->ext_vv_data = ext_vv;
  244. saa7146_video_uops.init(dev,vv);
  245. if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
  246. saa7146_vbi_uops.init(dev,vv);
  247. fmt = &vv->video_fmt;
  248. fmt->width = 384;
  249. fmt->height = 288;
  250. fmt->pixelformat = V4L2_PIX_FMT_BGR24;
  251. fmt->field = V4L2_FIELD_INTERLACED;
  252. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  253. fmt->bytesperline = 3 * fmt->width;
  254. fmt->sizeimage = fmt->bytesperline * fmt->height;
  255. vbi = &vv->vbi_fmt;
  256. vbi->sampling_rate = 27000000;
  257. vbi->offset = 248; /* todo */
  258. vbi->samples_per_line = 720 * 2;
  259. vbi->sample_format = V4L2_PIX_FMT_GREY;
  260. /* fixme: this only works for PAL */
  261. vbi->start[0] = 5;
  262. vbi->count[0] = 16;
  263. vbi->start[1] = 312;
  264. vbi->count[1] = 16;
  265. timer_setup(&vv->vbi_read_timeout, NULL, 0);
  266. dev->vv_data = vv;
  267. dev->vv_callback = &vv_callback;
  268. return 0;
  269. }
  270. EXPORT_SYMBOL_GPL(saa7146_vv_init);
  271. int saa7146_vv_release(struct saa7146_dev* dev)
  272. {
  273. struct saa7146_vv *vv = dev->vv_data;
  274. DEB_EE("dev:%p\n", dev);
  275. v4l2_device_unregister(&dev->v4l2_dev);
  276. v4l2_ctrl_handler_free(&dev->ctrl_handler);
  277. kfree(vv);
  278. dev->vv_data = NULL;
  279. dev->vv_callback = NULL;
  280. return 0;
  281. }
  282. EXPORT_SYMBOL_GPL(saa7146_vv_release);
  283. int saa7146_register_device(struct video_device *vfd, struct saa7146_dev *dev,
  284. char *name, int type)
  285. {
  286. struct vb2_queue *q;
  287. int err;
  288. int i;
  289. DEB_EE("dev:%p, name:'%s', type:%d\n", dev, name, type);
  290. vfd->fops = &video_fops;
  291. if (type == VFL_TYPE_VIDEO) {
  292. vfd->ioctl_ops = &dev->ext_vv_data->vid_ops;
  293. q = &dev->vv_data->video_dmaq.q;
  294. } else {
  295. vfd->ioctl_ops = &dev->ext_vv_data->vbi_ops;
  296. q = &dev->vv_data->vbi_dmaq.q;
  297. }
  298. vfd->release = video_device_release_empty;
  299. vfd->lock = &dev->v4l2_lock;
  300. vfd->v4l2_dev = &dev->v4l2_dev;
  301. vfd->tvnorms = 0;
  302. for (i = 0; i < dev->ext_vv_data->num_stds; i++)
  303. vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
  304. strscpy(vfd->name, name, sizeof(vfd->name));
  305. vfd->device_caps = V4L2_CAP_VIDEO_CAPTURE |
  306. V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  307. vfd->device_caps |= dev->ext_vv_data->capabilities;
  308. if (type == VFL_TYPE_VIDEO) {
  309. vfd->device_caps &=
  310. ~(V4L2_CAP_VBI_CAPTURE | V4L2_CAP_SLICED_VBI_OUTPUT);
  311. } else if (vfd->device_caps & V4L2_CAP_SLICED_VBI_OUTPUT) {
  312. vfd->vfl_dir = VFL_DIR_TX;
  313. vfd->device_caps &= ~(V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING |
  314. V4L2_CAP_AUDIO | V4L2_CAP_TUNER);
  315. } else {
  316. vfd->device_caps &= ~V4L2_CAP_VIDEO_CAPTURE;
  317. }
  318. q->type = type == VFL_TYPE_VIDEO ? V4L2_BUF_TYPE_VIDEO_CAPTURE : V4L2_BUF_TYPE_VBI_CAPTURE;
  319. q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
  320. q->io_modes = VB2_MMAP | VB2_READ | VB2_DMABUF;
  321. q->ops = type == VFL_TYPE_VIDEO ? &video_qops : &vbi_qops;
  322. q->mem_ops = &vb2_dma_sg_memops;
  323. q->drv_priv = dev;
  324. q->gfp_flags = __GFP_DMA32;
  325. q->buf_struct_size = sizeof(struct saa7146_buf);
  326. q->lock = &dev->v4l2_lock;
  327. q->min_queued_buffers = 2;
  328. q->dev = &dev->pci->dev;
  329. err = vb2_queue_init(q);
  330. if (err)
  331. return err;
  332. vfd->queue = q;
  333. video_set_drvdata(vfd, dev);
  334. err = video_register_device(vfd, type, -1);
  335. if (err < 0) {
  336. ERR("cannot register v4l2 device. skipping.\n");
  337. return err;
  338. }
  339. pr_info("%s: registered device %s [v4l2]\n",
  340. dev->name, video_device_node_name(vfd));
  341. return 0;
  342. }
  343. EXPORT_SYMBOL_GPL(saa7146_register_device);
  344. int saa7146_unregister_device(struct video_device *vfd, struct saa7146_dev *dev)
  345. {
  346. DEB_EE("dev:%p\n", dev);
  347. video_unregister_device(vfd);
  348. return 0;
  349. }
  350. EXPORT_SYMBOL_GPL(saa7146_unregister_device);
  351. static int __init saa7146_vv_init_module(void)
  352. {
  353. return 0;
  354. }
  355. static void __exit saa7146_vv_cleanup_module(void)
  356. {
  357. }
  358. module_init(saa7146_vv_init_module);
  359. module_exit(saa7146_vv_cleanup_module);
  360. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  361. MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
  362. MODULE_LICENSE("GPL");