saa7146_fops.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  2. #include <media/saa7146_vv.h>
  3. /****************************************************************************/
  4. /* resource management functions, shamelessly stolen from saa7134 driver */
  5. int saa7146_res_get(struct saa7146_fh *fh, unsigned int bit)
  6. {
  7. struct saa7146_dev *dev = fh->dev;
  8. struct saa7146_vv *vv = dev->vv_data;
  9. if (fh->resources & bit) {
  10. DEB_D("already allocated! want: 0x%02x, cur:0x%02x\n",
  11. bit, vv->resources);
  12. /* have it already allocated */
  13. return 1;
  14. }
  15. /* is it free? */
  16. if (vv->resources & bit) {
  17. DEB_D("locked! vv->resources:0x%02x, we want:0x%02x\n",
  18. vv->resources, bit);
  19. /* no, someone else uses it */
  20. return 0;
  21. }
  22. /* it's free, grab it */
  23. fh->resources |= bit;
  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_fh *fh, unsigned int bits)
  29. {
  30. struct saa7146_dev *dev = fh->dev;
  31. struct saa7146_vv *vv = dev->vv_data;
  32. BUG_ON((fh->resources & bits) != bits);
  33. fh->resources &= ~bits;
  34. vv->resources &= ~bits;
  35. DEB_D("res: put 0x%02x, cur:0x%02x\n", bits, vv->resources);
  36. }
  37. /********************************************************************************/
  38. /* common dma functions */
  39. void saa7146_dma_free(struct saa7146_dev *dev,struct videobuf_queue *q,
  40. struct saa7146_buf *buf)
  41. {
  42. struct videobuf_dmabuf *dma=videobuf_to_dma(&buf->vb);
  43. DEB_EE("dev:%p, buf:%p\n", dev, buf);
  44. BUG_ON(in_interrupt());
  45. videobuf_waiton(q, &buf->vb, 0, 0);
  46. videobuf_dma_unmap(q->dev, dma);
  47. videobuf_dma_free(dma);
  48. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  49. }
  50. /********************************************************************************/
  51. /* common buffer functions */
  52. int saa7146_buffer_queue(struct saa7146_dev *dev,
  53. struct saa7146_dmaqueue *q,
  54. struct saa7146_buf *buf)
  55. {
  56. assert_spin_locked(&dev->slock);
  57. DEB_EE("dev:%p, dmaq:%p, buf:%p\n", dev, q, buf);
  58. BUG_ON(!q);
  59. if (NULL == q->curr) {
  60. q->curr = buf;
  61. DEB_D("immediately activating buffer %p\n", buf);
  62. buf->activate(dev,buf,NULL);
  63. } else {
  64. list_add_tail(&buf->vb.queue,&q->queue);
  65. buf->vb.state = VIDEOBUF_QUEUED;
  66. DEB_D("adding buffer %p to queue. (active buffer present)\n",
  67. buf);
  68. }
  69. return 0;
  70. }
  71. void saa7146_buffer_finish(struct saa7146_dev *dev,
  72. struct saa7146_dmaqueue *q,
  73. int state)
  74. {
  75. assert_spin_locked(&dev->slock);
  76. DEB_EE("dev:%p, dmaq:%p, state:%d\n", dev, q, state);
  77. DEB_EE("q->curr:%p\n", q->curr);
  78. BUG_ON(!q->curr);
  79. /* finish current buffer */
  80. if (NULL == q->curr) {
  81. DEB_D("aiii. no current buffer\n");
  82. return;
  83. }
  84. q->curr->vb.state = state;
  85. do_gettimeofday(&q->curr->vb.ts);
  86. wake_up(&q->curr->vb.done);
  87. q->curr = NULL;
  88. }
  89. void saa7146_buffer_next(struct saa7146_dev *dev,
  90. struct saa7146_dmaqueue *q, int vbi)
  91. {
  92. struct saa7146_buf *buf,*next = NULL;
  93. BUG_ON(!q);
  94. DEB_INT("dev:%p, dmaq:%p, vbi:%d\n", dev, q, vbi);
  95. assert_spin_locked(&dev->slock);
  96. if (!list_empty(&q->queue)) {
  97. /* activate next one from queue */
  98. buf = list_entry(q->queue.next,struct saa7146_buf,vb.queue);
  99. list_del(&buf->vb.queue);
  100. if (!list_empty(&q->queue))
  101. next = list_entry(q->queue.next,struct saa7146_buf, vb.queue);
  102. q->curr = buf;
  103. DEB_INT("next buffer: buf:%p, prev:%p, next:%p\n",
  104. buf, q->queue.prev, q->queue.next);
  105. buf->activate(dev,buf,next);
  106. } else {
  107. DEB_INT("no next buffer. stopping.\n");
  108. if( 0 != vbi ) {
  109. /* turn off video-dma3 */
  110. saa7146_write(dev,MC1, MASK_20);
  111. } else {
  112. /* nothing to do -- just prevent next video-dma1 transfer
  113. by lowering the protection address */
  114. // fixme: fix this for vflip != 0
  115. saa7146_write(dev, PROT_ADDR1, 0);
  116. saa7146_write(dev, MC2, (MASK_02|MASK_18));
  117. /* write the address of the rps-program */
  118. saa7146_write(dev, RPS_ADDR0, dev->d_rps0.dma_handle);
  119. /* turn on rps */
  120. saa7146_write(dev, MC1, (MASK_12 | MASK_28));
  121. /*
  122. printk("vdma%d.base_even: 0x%08x\n", 1,saa7146_read(dev,BASE_EVEN1));
  123. printk("vdma%d.base_odd: 0x%08x\n", 1,saa7146_read(dev,BASE_ODD1));
  124. printk("vdma%d.prot_addr: 0x%08x\n", 1,saa7146_read(dev,PROT_ADDR1));
  125. printk("vdma%d.base_page: 0x%08x\n", 1,saa7146_read(dev,BASE_PAGE1));
  126. printk("vdma%d.pitch: 0x%08x\n", 1,saa7146_read(dev,PITCH1));
  127. printk("vdma%d.num_line_byte: 0x%08x\n", 1,saa7146_read(dev,NUM_LINE_BYTE1));
  128. */
  129. }
  130. del_timer(&q->timeout);
  131. }
  132. }
  133. void saa7146_buffer_timeout(unsigned long data)
  134. {
  135. struct saa7146_dmaqueue *q = (struct saa7146_dmaqueue*)data;
  136. struct saa7146_dev *dev = q->dev;
  137. unsigned long flags;
  138. DEB_EE("dev:%p, dmaq:%p\n", dev, q);
  139. spin_lock_irqsave(&dev->slock,flags);
  140. if (q->curr) {
  141. DEB_D("timeout on %p\n", q->curr);
  142. saa7146_buffer_finish(dev,q,VIDEOBUF_ERROR);
  143. }
  144. /* we don't restart the transfer here like other drivers do. when
  145. a streaming capture is disabled, the timeout function will be
  146. called for the current buffer. if we activate the next buffer now,
  147. we mess up our capture logic. if a timeout occurs on another buffer,
  148. then something is seriously broken before, so no need to buffer the
  149. next capture IMHO... */
  150. /*
  151. saa7146_buffer_next(dev,q);
  152. */
  153. spin_unlock_irqrestore(&dev->slock,flags);
  154. }
  155. /********************************************************************************/
  156. /* file operations */
  157. static int fops_open(struct file *file)
  158. {
  159. struct video_device *vdev = video_devdata(file);
  160. struct saa7146_dev *dev = video_drvdata(file);
  161. struct saa7146_fh *fh = NULL;
  162. int result = 0;
  163. enum v4l2_buf_type type;
  164. DEB_EE("file:%p, dev:%s\n", file, video_device_node_name(vdev));
  165. if (mutex_lock_interruptible(&saa7146_devices_lock))
  166. return -ERESTARTSYS;
  167. DEB_D("using: %p\n", dev);
  168. type = vdev->vfl_type == VFL_TYPE_GRABBER
  169. ? V4L2_BUF_TYPE_VIDEO_CAPTURE
  170. : V4L2_BUF_TYPE_VBI_CAPTURE;
  171. /* check if an extension is registered */
  172. if( NULL == dev->ext ) {
  173. DEB_S("no extension registered for this device\n");
  174. result = -ENODEV;
  175. goto out;
  176. }
  177. /* allocate per open data */
  178. fh = kzalloc(sizeof(*fh),GFP_KERNEL);
  179. if (NULL == fh) {
  180. DEB_S("cannot allocate memory for per open data\n");
  181. result = -ENOMEM;
  182. goto out;
  183. }
  184. file->private_data = fh;
  185. fh->dev = dev;
  186. fh->type = type;
  187. if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  188. DEB_S("initializing vbi...\n");
  189. if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
  190. result = saa7146_vbi_uops.open(dev,file);
  191. if (dev->ext_vv_data->vbi_fops.open)
  192. dev->ext_vv_data->vbi_fops.open(file);
  193. } else {
  194. DEB_S("initializing video...\n");
  195. result = saa7146_video_uops.open(dev,file);
  196. }
  197. if (0 != result) {
  198. goto out;
  199. }
  200. if( 0 == try_module_get(dev->ext->module)) {
  201. result = -EINVAL;
  202. goto out;
  203. }
  204. result = 0;
  205. out:
  206. if (fh && result != 0) {
  207. kfree(fh);
  208. file->private_data = NULL;
  209. }
  210. mutex_unlock(&saa7146_devices_lock);
  211. return result;
  212. }
  213. static int fops_release(struct file *file)
  214. {
  215. struct saa7146_fh *fh = file->private_data;
  216. struct saa7146_dev *dev = fh->dev;
  217. DEB_EE("file:%p\n", file);
  218. if (mutex_lock_interruptible(&saa7146_devices_lock))
  219. return -ERESTARTSYS;
  220. if( fh->type == V4L2_BUF_TYPE_VBI_CAPTURE) {
  221. if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
  222. saa7146_vbi_uops.release(dev,file);
  223. if (dev->ext_vv_data->vbi_fops.release)
  224. dev->ext_vv_data->vbi_fops.release(file);
  225. } else {
  226. saa7146_video_uops.release(dev,file);
  227. }
  228. module_put(dev->ext->module);
  229. file->private_data = NULL;
  230. kfree(fh);
  231. mutex_unlock(&saa7146_devices_lock);
  232. return 0;
  233. }
  234. static int fops_mmap(struct file *file, struct vm_area_struct * vma)
  235. {
  236. struct saa7146_fh *fh = file->private_data;
  237. struct videobuf_queue *q;
  238. switch (fh->type) {
  239. case V4L2_BUF_TYPE_VIDEO_CAPTURE: {
  240. DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, vma:%p\n",
  241. file, vma);
  242. q = &fh->video_q;
  243. break;
  244. }
  245. case V4L2_BUF_TYPE_VBI_CAPTURE: {
  246. DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, vma:%p\n",
  247. file, vma);
  248. q = &fh->vbi_q;
  249. break;
  250. }
  251. default:
  252. BUG();
  253. return 0;
  254. }
  255. return videobuf_mmap_mapper(q,vma);
  256. }
  257. static unsigned int fops_poll(struct file *file, struct poll_table_struct *wait)
  258. {
  259. struct saa7146_fh *fh = file->private_data;
  260. struct videobuf_buffer *buf = NULL;
  261. struct videobuf_queue *q;
  262. DEB_EE("file:%p, poll:%p\n", file, wait);
  263. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  264. if( 0 == fh->vbi_q.streaming )
  265. return videobuf_poll_stream(file, &fh->vbi_q, wait);
  266. q = &fh->vbi_q;
  267. } else {
  268. DEB_D("using video queue\n");
  269. q = &fh->video_q;
  270. }
  271. if (!list_empty(&q->stream))
  272. buf = list_entry(q->stream.next, struct videobuf_buffer, stream);
  273. if (!buf) {
  274. DEB_D("buf == NULL!\n");
  275. return POLLERR;
  276. }
  277. poll_wait(file, &buf->done, wait);
  278. if (buf->state == VIDEOBUF_DONE || buf->state == VIDEOBUF_ERROR) {
  279. DEB_D("poll succeeded!\n");
  280. return POLLIN|POLLRDNORM;
  281. }
  282. DEB_D("nothing to poll for, buf->state:%d\n", buf->state);
  283. return 0;
  284. }
  285. static ssize_t fops_read(struct file *file, char __user *data, size_t count, loff_t *ppos)
  286. {
  287. struct saa7146_fh *fh = file->private_data;
  288. switch (fh->type) {
  289. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  290. /*
  291. DEB_EE("V4L2_BUF_TYPE_VIDEO_CAPTURE: file:%p, data:%p, count:%lun",
  292. file, data, (unsigned long)count);
  293. */
  294. return saa7146_video_uops.read(file,data,count,ppos);
  295. case V4L2_BUF_TYPE_VBI_CAPTURE:
  296. /*
  297. DEB_EE("V4L2_BUF_TYPE_VBI_CAPTURE: file:%p, data:%p, count:%lu\n",
  298. file, data, (unsigned long)count);
  299. */
  300. if (fh->dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
  301. return saa7146_vbi_uops.read(file,data,count,ppos);
  302. return -EINVAL;
  303. default:
  304. BUG();
  305. return 0;
  306. }
  307. }
  308. static ssize_t fops_write(struct file *file, const char __user *data, size_t count, loff_t *ppos)
  309. {
  310. struct saa7146_fh *fh = file->private_data;
  311. switch (fh->type) {
  312. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  313. return -EINVAL;
  314. case V4L2_BUF_TYPE_VBI_CAPTURE:
  315. if (fh->dev->ext_vv_data->vbi_fops.write)
  316. return fh->dev->ext_vv_data->vbi_fops.write(file, data, count, ppos);
  317. else
  318. return -EINVAL;
  319. default:
  320. BUG();
  321. return -EINVAL;
  322. }
  323. }
  324. static const struct v4l2_file_operations video_fops =
  325. {
  326. .owner = THIS_MODULE,
  327. .open = fops_open,
  328. .release = fops_release,
  329. .read = fops_read,
  330. .write = fops_write,
  331. .poll = fops_poll,
  332. .mmap = fops_mmap,
  333. .unlocked_ioctl = video_ioctl2,
  334. };
  335. static void vv_callback(struct saa7146_dev *dev, unsigned long status)
  336. {
  337. u32 isr = status;
  338. DEB_INT("dev:%p, isr:0x%08x\n", dev, (u32)status);
  339. if (0 != (isr & (MASK_27))) {
  340. DEB_INT("irq: RPS0 (0x%08x)\n", isr);
  341. saa7146_video_uops.irq_done(dev,isr);
  342. }
  343. if (0 != (isr & (MASK_28))) {
  344. u32 mc2 = saa7146_read(dev, MC2);
  345. if( 0 != (mc2 & MASK_15)) {
  346. DEB_INT("irq: RPS1 vbi workaround (0x%08x)\n", isr);
  347. wake_up(&dev->vv_data->vbi_wq);
  348. saa7146_write(dev,MC2, MASK_31);
  349. return;
  350. }
  351. DEB_INT("irq: RPS1 (0x%08x)\n", isr);
  352. saa7146_vbi_uops.irq_done(dev,isr);
  353. }
  354. }
  355. int saa7146_vv_init(struct saa7146_dev* dev, struct saa7146_ext_vv *ext_vv)
  356. {
  357. struct saa7146_vv *vv;
  358. int err;
  359. err = v4l2_device_register(&dev->pci->dev, &dev->v4l2_dev);
  360. if (err)
  361. return err;
  362. vv = kzalloc(sizeof(struct saa7146_vv), GFP_KERNEL);
  363. if (vv == NULL) {
  364. ERR("out of memory. aborting.\n");
  365. return -ENOMEM;
  366. }
  367. ext_vv->ops = saa7146_video_ioctl_ops;
  368. ext_vv->core_ops = &saa7146_video_ioctl_ops;
  369. DEB_EE("dev:%p\n", dev);
  370. /* set default values for video parts of the saa7146 */
  371. saa7146_write(dev, BCS_CTRL, 0x80400040);
  372. /* enable video-port pins */
  373. saa7146_write(dev, MC1, (MASK_10 | MASK_26));
  374. /* save per-device extension data (one extension can
  375. handle different devices that might need different
  376. configuration data) */
  377. dev->ext_vv_data = ext_vv;
  378. vv->d_clipping.cpu_addr = pci_alloc_consistent(dev->pci, SAA7146_CLIPPING_MEM, &vv->d_clipping.dma_handle);
  379. if( NULL == vv->d_clipping.cpu_addr ) {
  380. ERR("out of memory. aborting.\n");
  381. kfree(vv);
  382. return -1;
  383. }
  384. memset(vv->d_clipping.cpu_addr, 0x0, SAA7146_CLIPPING_MEM);
  385. saa7146_video_uops.init(dev,vv);
  386. if (dev->ext_vv_data->capabilities & V4L2_CAP_VBI_CAPTURE)
  387. saa7146_vbi_uops.init(dev,vv);
  388. dev->vv_data = vv;
  389. dev->vv_callback = &vv_callback;
  390. return 0;
  391. }
  392. EXPORT_SYMBOL_GPL(saa7146_vv_init);
  393. int saa7146_vv_release(struct saa7146_dev* dev)
  394. {
  395. struct saa7146_vv *vv = dev->vv_data;
  396. DEB_EE("dev:%p\n", dev);
  397. v4l2_device_unregister(&dev->v4l2_dev);
  398. pci_free_consistent(dev->pci, SAA7146_CLIPPING_MEM, vv->d_clipping.cpu_addr, vv->d_clipping.dma_handle);
  399. kfree(vv);
  400. dev->vv_data = NULL;
  401. dev->vv_callback = NULL;
  402. return 0;
  403. }
  404. EXPORT_SYMBOL_GPL(saa7146_vv_release);
  405. int saa7146_register_device(struct video_device **vid, struct saa7146_dev* dev,
  406. char *name, int type)
  407. {
  408. struct video_device *vfd;
  409. int err;
  410. int i;
  411. DEB_EE("dev:%p, name:'%s', type:%d\n", dev, name, type);
  412. // released by vfd->release
  413. vfd = video_device_alloc();
  414. if (vfd == NULL)
  415. return -ENOMEM;
  416. vfd->fops = &video_fops;
  417. vfd->ioctl_ops = &dev->ext_vv_data->ops;
  418. vfd->release = video_device_release;
  419. vfd->lock = &dev->v4l2_lock;
  420. vfd->tvnorms = 0;
  421. for (i = 0; i < dev->ext_vv_data->num_stds; i++)
  422. vfd->tvnorms |= dev->ext_vv_data->stds[i].id;
  423. strlcpy(vfd->name, name, sizeof(vfd->name));
  424. video_set_drvdata(vfd, dev);
  425. err = video_register_device(vfd, type, -1);
  426. if (err < 0) {
  427. ERR("cannot register v4l2 device. skipping.\n");
  428. video_device_release(vfd);
  429. return err;
  430. }
  431. pr_info("%s: registered device %s [v4l2]\n",
  432. dev->name, video_device_node_name(vfd));
  433. *vid = vfd;
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(saa7146_register_device);
  437. int saa7146_unregister_device(struct video_device **vid, struct saa7146_dev* dev)
  438. {
  439. DEB_EE("dev:%p\n", dev);
  440. video_unregister_device(*vid);
  441. *vid = NULL;
  442. return 0;
  443. }
  444. EXPORT_SYMBOL_GPL(saa7146_unregister_device);
  445. static int __init saa7146_vv_init_module(void)
  446. {
  447. return 0;
  448. }
  449. static void __exit saa7146_vv_cleanup_module(void)
  450. {
  451. }
  452. module_init(saa7146_vv_init_module);
  453. module_exit(saa7146_vv_cleanup_module);
  454. MODULE_AUTHOR("Michael Hunold <michael@mihu.de>");
  455. MODULE_DESCRIPTION("video4linux driver for saa7146-based hardware");
  456. MODULE_LICENSE("GPL");