saa7146_fops.c 14 KB

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