saa7146_fops.c 15 KB

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