saa7164-vbi.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. /*
  2. * Driver for the NXP SAA7164 PCIe bridge
  3. *
  4. * Copyright (c) 2010-2015 Steven Toth <stoth@kernellabs.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. *
  15. * GNU General Public License for more details.
  16. */
  17. #include "saa7164.h"
  18. /* Take the encoder configuration from the port struct and
  19. * flush it to the hardware.
  20. */
  21. static void saa7164_vbi_configure(struct saa7164_port *port)
  22. {
  23. struct saa7164_dev *dev = port->dev;
  24. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  25. port->vbi_params.width = port->enc_port->width;
  26. port->vbi_params.height = port->enc_port->height;
  27. port->vbi_params.is_50hz =
  28. (port->enc_port->encodernorm.id & V4L2_STD_625_50) != 0;
  29. /* Set up the DIF (enable it) for analog mode by default */
  30. saa7164_api_initialize_dif(port);
  31. dprintk(DBGLVL_VBI, "%s() ends\n", __func__);
  32. }
  33. static int saa7164_vbi_buffers_dealloc(struct saa7164_port *port)
  34. {
  35. struct list_head *c, *n, *p, *q, *l, *v;
  36. struct saa7164_dev *dev = port->dev;
  37. struct saa7164_buffer *buf;
  38. struct saa7164_user_buffer *ubuf;
  39. /* Remove any allocated buffers */
  40. mutex_lock(&port->dmaqueue_lock);
  41. dprintk(DBGLVL_VBI, "%s(port=%d) dmaqueue\n", __func__, port->nr);
  42. list_for_each_safe(c, n, &port->dmaqueue.list) {
  43. buf = list_entry(c, struct saa7164_buffer, list);
  44. list_del(c);
  45. saa7164_buffer_dealloc(buf);
  46. }
  47. dprintk(DBGLVL_VBI, "%s(port=%d) used\n", __func__, port->nr);
  48. list_for_each_safe(p, q, &port->list_buf_used.list) {
  49. ubuf = list_entry(p, struct saa7164_user_buffer, list);
  50. list_del(p);
  51. saa7164_buffer_dealloc_user(ubuf);
  52. }
  53. dprintk(DBGLVL_VBI, "%s(port=%d) free\n", __func__, port->nr);
  54. list_for_each_safe(l, v, &port->list_buf_free.list) {
  55. ubuf = list_entry(l, struct saa7164_user_buffer, list);
  56. list_del(l);
  57. saa7164_buffer_dealloc_user(ubuf);
  58. }
  59. mutex_unlock(&port->dmaqueue_lock);
  60. dprintk(DBGLVL_VBI, "%s(port=%d) done\n", __func__, port->nr);
  61. return 0;
  62. }
  63. /* Dynamic buffer switch at vbi start time */
  64. static int saa7164_vbi_buffers_alloc(struct saa7164_port *port)
  65. {
  66. struct saa7164_dev *dev = port->dev;
  67. struct saa7164_buffer *buf;
  68. struct saa7164_user_buffer *ubuf;
  69. struct tmHWStreamParameters *params = &port->hw_streamingparams;
  70. int result = -ENODEV, i;
  71. int len = 0;
  72. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  73. /* TODO: NTSC SPECIFIC */
  74. /* Init and establish defaults */
  75. params->samplesperline = 1440;
  76. params->numberoflines = 12;
  77. params->numberoflines = 18;
  78. params->pitch = 1600;
  79. params->pitch = 1440;
  80. params->numpagetables = 2 +
  81. ((params->numberoflines * params->pitch) / PAGE_SIZE);
  82. params->bitspersample = 8;
  83. params->linethreshold = 0;
  84. params->pagetablelistvirt = NULL;
  85. params->pagetablelistphys = NULL;
  86. params->numpagetableentries = port->hwcfg.buffercount;
  87. /* Allocate the PCI resources, buffers (hard) */
  88. for (i = 0; i < port->hwcfg.buffercount; i++) {
  89. buf = saa7164_buffer_alloc(port,
  90. params->numberoflines *
  91. params->pitch);
  92. if (!buf) {
  93. printk(KERN_ERR "%s() failed (errno = %d), unable to allocate buffer\n",
  94. __func__, result);
  95. result = -ENOMEM;
  96. goto failed;
  97. } else {
  98. mutex_lock(&port->dmaqueue_lock);
  99. list_add_tail(&buf->list, &port->dmaqueue.list);
  100. mutex_unlock(&port->dmaqueue_lock);
  101. }
  102. }
  103. /* Allocate some kernel buffers for copying
  104. * to userpsace.
  105. */
  106. len = params->numberoflines * params->pitch;
  107. if (vbi_buffers < 16)
  108. vbi_buffers = 16;
  109. if (vbi_buffers > 512)
  110. vbi_buffers = 512;
  111. for (i = 0; i < vbi_buffers; i++) {
  112. ubuf = saa7164_buffer_alloc_user(dev, len);
  113. if (ubuf) {
  114. mutex_lock(&port->dmaqueue_lock);
  115. list_add_tail(&ubuf->list, &port->list_buf_free.list);
  116. mutex_unlock(&port->dmaqueue_lock);
  117. }
  118. }
  119. result = 0;
  120. failed:
  121. return result;
  122. }
  123. static int saa7164_vbi_initialize(struct saa7164_port *port)
  124. {
  125. saa7164_vbi_configure(port);
  126. return 0;
  127. }
  128. /* -- V4L2 --------------------------------------------------------- */
  129. static int vidioc_s_std(struct file *file, void *priv, v4l2_std_id id)
  130. {
  131. struct saa7164_vbi_fh *fh = file->private_data;
  132. return saa7164_s_std(fh->port->enc_port, id);
  133. }
  134. static int vidioc_g_std(struct file *file, void *priv, v4l2_std_id *id)
  135. {
  136. struct saa7164_encoder_fh *fh = file->private_data;
  137. return saa7164_g_std(fh->port->enc_port, id);
  138. }
  139. static int vidioc_g_input(struct file *file, void *priv, unsigned int *i)
  140. {
  141. struct saa7164_vbi_fh *fh = file->private_data;
  142. return saa7164_g_input(fh->port->enc_port, i);
  143. }
  144. static int vidioc_s_input(struct file *file, void *priv, unsigned int i)
  145. {
  146. struct saa7164_vbi_fh *fh = file->private_data;
  147. return saa7164_s_input(fh->port->enc_port, i);
  148. }
  149. static int vidioc_g_frequency(struct file *file, void *priv,
  150. struct v4l2_frequency *f)
  151. {
  152. struct saa7164_vbi_fh *fh = file->private_data;
  153. return saa7164_g_frequency(fh->port->enc_port, f);
  154. }
  155. static int vidioc_s_frequency(struct file *file, void *priv,
  156. const struct v4l2_frequency *f)
  157. {
  158. struct saa7164_vbi_fh *fh = file->private_data;
  159. int ret = saa7164_s_frequency(fh->port->enc_port, f);
  160. if (ret == 0)
  161. saa7164_vbi_initialize(fh->port);
  162. return ret;
  163. }
  164. static int vidioc_querycap(struct file *file, void *priv,
  165. struct v4l2_capability *cap)
  166. {
  167. struct saa7164_vbi_fh *fh = file->private_data;
  168. struct saa7164_port *port = fh->port;
  169. struct saa7164_dev *dev = port->dev;
  170. strcpy(cap->driver, dev->name);
  171. strlcpy(cap->card, saa7164_boards[dev->board].name,
  172. sizeof(cap->card));
  173. sprintf(cap->bus_info, "PCI:%s", pci_name(dev->pci));
  174. cap->device_caps =
  175. V4L2_CAP_VBI_CAPTURE |
  176. V4L2_CAP_READWRITE |
  177. V4L2_CAP_TUNER;
  178. cap->capabilities = cap->device_caps |
  179. V4L2_CAP_VIDEO_CAPTURE |
  180. V4L2_CAP_DEVICE_CAPS;
  181. return 0;
  182. }
  183. static int saa7164_vbi_stop_port(struct saa7164_port *port)
  184. {
  185. struct saa7164_dev *dev = port->dev;
  186. int ret;
  187. ret = saa7164_api_transition_port(port, SAA_DMASTATE_STOP);
  188. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  189. printk(KERN_ERR "%s() stop transition failed, ret = 0x%x\n",
  190. __func__, ret);
  191. ret = -EIO;
  192. } else {
  193. dprintk(DBGLVL_VBI, "%s() Stopped\n", __func__);
  194. ret = 0;
  195. }
  196. return ret;
  197. }
  198. static int saa7164_vbi_acquire_port(struct saa7164_port *port)
  199. {
  200. struct saa7164_dev *dev = port->dev;
  201. int ret;
  202. ret = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  203. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  204. printk(KERN_ERR "%s() acquire transition failed, ret = 0x%x\n",
  205. __func__, ret);
  206. ret = -EIO;
  207. } else {
  208. dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
  209. ret = 0;
  210. }
  211. return ret;
  212. }
  213. static int saa7164_vbi_pause_port(struct saa7164_port *port)
  214. {
  215. struct saa7164_dev *dev = port->dev;
  216. int ret;
  217. ret = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  218. if ((ret != SAA_OK) && (ret != SAA_ERR_ALREADY_STOPPED)) {
  219. printk(KERN_ERR "%s() pause transition failed, ret = 0x%x\n",
  220. __func__, ret);
  221. ret = -EIO;
  222. } else {
  223. dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
  224. ret = 0;
  225. }
  226. return ret;
  227. }
  228. /* Firmware is very windows centric, meaning you have to transition
  229. * the part through AVStream / KS Windows stages, forwards or backwards.
  230. * States are: stopped, acquired (h/w), paused, started.
  231. * We have to leave here will all of the soft buffers on the free list,
  232. * else the cfg_post() func won't have soft buffers to correctly configure.
  233. */
  234. static int saa7164_vbi_stop_streaming(struct saa7164_port *port)
  235. {
  236. struct saa7164_dev *dev = port->dev;
  237. struct saa7164_buffer *buf;
  238. struct saa7164_user_buffer *ubuf;
  239. struct list_head *c, *n;
  240. int ret;
  241. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  242. ret = saa7164_vbi_pause_port(port);
  243. ret = saa7164_vbi_acquire_port(port);
  244. ret = saa7164_vbi_stop_port(port);
  245. dprintk(DBGLVL_VBI, "%s(port=%d) Hardware stopped\n", __func__,
  246. port->nr);
  247. /* Reset the state of any allocated buffer resources */
  248. mutex_lock(&port->dmaqueue_lock);
  249. /* Reset the hard and soft buffer state */
  250. list_for_each_safe(c, n, &port->dmaqueue.list) {
  251. buf = list_entry(c, struct saa7164_buffer, list);
  252. buf->flags = SAA7164_BUFFER_FREE;
  253. buf->pos = 0;
  254. }
  255. list_for_each_safe(c, n, &port->list_buf_used.list) {
  256. ubuf = list_entry(c, struct saa7164_user_buffer, list);
  257. ubuf->pos = 0;
  258. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  259. }
  260. mutex_unlock(&port->dmaqueue_lock);
  261. /* Free any allocated resources */
  262. saa7164_vbi_buffers_dealloc(port);
  263. dprintk(DBGLVL_VBI, "%s(port=%d) Released\n", __func__, port->nr);
  264. return ret;
  265. }
  266. static int saa7164_vbi_start_streaming(struct saa7164_port *port)
  267. {
  268. struct saa7164_dev *dev = port->dev;
  269. int result, ret = 0;
  270. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  271. port->done_first_interrupt = 0;
  272. /* allocate all of the PCIe DMA buffer resources on the fly,
  273. * allowing switching between TS and PS payloads without
  274. * requiring a complete driver reload.
  275. */
  276. saa7164_vbi_buffers_alloc(port);
  277. /* Configure the encoder with any cache values */
  278. #if 0
  279. saa7164_api_set_encoder(port);
  280. saa7164_api_get_encoder(port);
  281. #endif
  282. /* Place the empty buffers on the hardware */
  283. saa7164_buffer_cfg_port(port);
  284. /* Negotiate format */
  285. if (saa7164_api_set_vbi_format(port) != SAA_OK) {
  286. printk(KERN_ERR "%s() No supported VBI format\n", __func__);
  287. ret = -EIO;
  288. goto out;
  289. }
  290. /* Acquire the hardware */
  291. result = saa7164_api_transition_port(port, SAA_DMASTATE_ACQUIRE);
  292. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  293. printk(KERN_ERR "%s() acquire transition failed, res = 0x%x\n",
  294. __func__, result);
  295. ret = -EIO;
  296. goto out;
  297. } else
  298. dprintk(DBGLVL_VBI, "%s() Acquired\n", __func__);
  299. /* Pause the hardware */
  300. result = saa7164_api_transition_port(port, SAA_DMASTATE_PAUSE);
  301. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  302. printk(KERN_ERR "%s() pause transition failed, res = 0x%x\n",
  303. __func__, result);
  304. /* Stop the hardware, regardless */
  305. result = saa7164_vbi_stop_port(port);
  306. if (result != SAA_OK) {
  307. printk(KERN_ERR "%s() pause/forced stop transition failed, res = 0x%x\n",
  308. __func__, result);
  309. }
  310. ret = -EIO;
  311. goto out;
  312. } else
  313. dprintk(DBGLVL_VBI, "%s() Paused\n", __func__);
  314. /* Start the hardware */
  315. result = saa7164_api_transition_port(port, SAA_DMASTATE_RUN);
  316. if ((result != SAA_OK) && (result != SAA_ERR_ALREADY_STOPPED)) {
  317. printk(KERN_ERR "%s() run transition failed, result = 0x%x\n",
  318. __func__, result);
  319. /* Stop the hardware, regardless */
  320. result = saa7164_vbi_acquire_port(port);
  321. result = saa7164_vbi_stop_port(port);
  322. if (result != SAA_OK) {
  323. printk(KERN_ERR "%s() run/forced stop transition failed, res = 0x%x\n",
  324. __func__, result);
  325. }
  326. ret = -EIO;
  327. } else
  328. dprintk(DBGLVL_VBI, "%s() Running\n", __func__);
  329. out:
  330. return ret;
  331. }
  332. static int saa7164_vbi_fmt(struct file *file, void *priv,
  333. struct v4l2_format *f)
  334. {
  335. /* ntsc */
  336. f->fmt.vbi.samples_per_line = 1440;
  337. f->fmt.vbi.sampling_rate = 27000000;
  338. f->fmt.vbi.sample_format = V4L2_PIX_FMT_GREY;
  339. f->fmt.vbi.offset = 0;
  340. f->fmt.vbi.flags = 0;
  341. f->fmt.vbi.start[0] = 10;
  342. f->fmt.vbi.count[0] = 18;
  343. f->fmt.vbi.start[1] = 263 + 10 + 1;
  344. f->fmt.vbi.count[1] = 18;
  345. memset(f->fmt.vbi.reserved, 0, sizeof(f->fmt.vbi.reserved));
  346. return 0;
  347. }
  348. static int fops_open(struct file *file)
  349. {
  350. struct saa7164_dev *dev;
  351. struct saa7164_port *port;
  352. struct saa7164_vbi_fh *fh;
  353. port = (struct saa7164_port *)video_get_drvdata(video_devdata(file));
  354. if (!port)
  355. return -ENODEV;
  356. dev = port->dev;
  357. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  358. /* allocate + initialize per filehandle data */
  359. fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  360. if (NULL == fh)
  361. return -ENOMEM;
  362. fh->port = port;
  363. v4l2_fh_init(&fh->fh, video_devdata(file));
  364. v4l2_fh_add(&fh->fh);
  365. file->private_data = fh;
  366. return 0;
  367. }
  368. static int fops_release(struct file *file)
  369. {
  370. struct saa7164_vbi_fh *fh = file->private_data;
  371. struct saa7164_port *port = fh->port;
  372. struct saa7164_dev *dev = port->dev;
  373. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  374. /* Shut device down on last close */
  375. if (atomic_cmpxchg(&fh->v4l_reading, 1, 0) == 1) {
  376. if (atomic_dec_return(&port->v4l_reader_count) == 0) {
  377. /* stop vbi capture then cancel buffers */
  378. saa7164_vbi_stop_streaming(port);
  379. }
  380. }
  381. v4l2_fh_del(&fh->fh);
  382. v4l2_fh_exit(&fh->fh);
  383. kfree(fh);
  384. return 0;
  385. }
  386. static struct
  387. saa7164_user_buffer *saa7164_vbi_next_buf(struct saa7164_port *port)
  388. {
  389. struct saa7164_user_buffer *ubuf = NULL;
  390. struct saa7164_dev *dev = port->dev;
  391. u32 crc;
  392. mutex_lock(&port->dmaqueue_lock);
  393. if (!list_empty(&port->list_buf_used.list)) {
  394. ubuf = list_first_entry(&port->list_buf_used.list,
  395. struct saa7164_user_buffer, list);
  396. if (crc_checking) {
  397. crc = crc32(0, ubuf->data, ubuf->actual_size);
  398. if (crc != ubuf->crc) {
  399. printk(KERN_ERR "%s() ubuf %p crc became invalid, was 0x%x became 0x%x\n",
  400. __func__,
  401. ubuf, ubuf->crc, crc);
  402. }
  403. }
  404. }
  405. mutex_unlock(&port->dmaqueue_lock);
  406. dprintk(DBGLVL_VBI, "%s() returns %p\n", __func__, ubuf);
  407. return ubuf;
  408. }
  409. static ssize_t fops_read(struct file *file, char __user *buffer,
  410. size_t count, loff_t *pos)
  411. {
  412. struct saa7164_vbi_fh *fh = file->private_data;
  413. struct saa7164_port *port = fh->port;
  414. struct saa7164_user_buffer *ubuf = NULL;
  415. struct saa7164_dev *dev = port->dev;
  416. int ret = 0;
  417. int rem, cnt;
  418. u8 *p;
  419. port->last_read_msecs_diff = port->last_read_msecs;
  420. port->last_read_msecs = jiffies_to_msecs(jiffies);
  421. port->last_read_msecs_diff = port->last_read_msecs -
  422. port->last_read_msecs_diff;
  423. saa7164_histogram_update(&port->read_interval,
  424. port->last_read_msecs_diff);
  425. if (*pos) {
  426. printk(KERN_ERR "%s() ESPIPE\n", __func__);
  427. return -ESPIPE;
  428. }
  429. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  430. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  431. if (saa7164_vbi_initialize(port) < 0) {
  432. printk(KERN_ERR "%s() EINVAL\n", __func__);
  433. return -EINVAL;
  434. }
  435. saa7164_vbi_start_streaming(port);
  436. msleep(200);
  437. }
  438. }
  439. /* blocking wait for buffer */
  440. if ((file->f_flags & O_NONBLOCK) == 0) {
  441. if (wait_event_interruptible(port->wait_read,
  442. saa7164_vbi_next_buf(port))) {
  443. printk(KERN_ERR "%s() ERESTARTSYS\n", __func__);
  444. return -ERESTARTSYS;
  445. }
  446. }
  447. /* Pull the first buffer from the used list */
  448. ubuf = saa7164_vbi_next_buf(port);
  449. while ((count > 0) && ubuf) {
  450. /* set remaining bytes to copy */
  451. rem = ubuf->actual_size - ubuf->pos;
  452. cnt = rem > count ? count : rem;
  453. p = ubuf->data + ubuf->pos;
  454. dprintk(DBGLVL_VBI,
  455. "%s() count=%d cnt=%d rem=%d buf=%p buf->pos=%d\n",
  456. __func__, (int)count, cnt, rem, ubuf, ubuf->pos);
  457. if (copy_to_user(buffer, p, cnt)) {
  458. printk(KERN_ERR "%s() copy_to_user failed\n", __func__);
  459. if (!ret) {
  460. printk(KERN_ERR "%s() EFAULT\n", __func__);
  461. ret = -EFAULT;
  462. }
  463. goto err;
  464. }
  465. ubuf->pos += cnt;
  466. count -= cnt;
  467. buffer += cnt;
  468. ret += cnt;
  469. if (ubuf->pos > ubuf->actual_size)
  470. printk(KERN_ERR "read() pos > actual, huh?\n");
  471. if (ubuf->pos == ubuf->actual_size) {
  472. /* finished with current buffer, take next buffer */
  473. /* Requeue the buffer on the free list */
  474. ubuf->pos = 0;
  475. mutex_lock(&port->dmaqueue_lock);
  476. list_move_tail(&ubuf->list, &port->list_buf_free.list);
  477. mutex_unlock(&port->dmaqueue_lock);
  478. /* Dequeue next */
  479. if ((file->f_flags & O_NONBLOCK) == 0) {
  480. if (wait_event_interruptible(port->wait_read,
  481. saa7164_vbi_next_buf(port))) {
  482. break;
  483. }
  484. }
  485. ubuf = saa7164_vbi_next_buf(port);
  486. }
  487. }
  488. err:
  489. if (!ret && !ubuf) {
  490. printk(KERN_ERR "%s() EAGAIN\n", __func__);
  491. ret = -EAGAIN;
  492. }
  493. return ret;
  494. }
  495. static unsigned int fops_poll(struct file *file, poll_table *wait)
  496. {
  497. struct saa7164_vbi_fh *fh = (struct saa7164_vbi_fh *)file->private_data;
  498. struct saa7164_port *port = fh->port;
  499. unsigned int mask = 0;
  500. port->last_poll_msecs_diff = port->last_poll_msecs;
  501. port->last_poll_msecs = jiffies_to_msecs(jiffies);
  502. port->last_poll_msecs_diff = port->last_poll_msecs -
  503. port->last_poll_msecs_diff;
  504. saa7164_histogram_update(&port->poll_interval,
  505. port->last_poll_msecs_diff);
  506. if (!video_is_registered(port->v4l_device))
  507. return -EIO;
  508. if (atomic_cmpxchg(&fh->v4l_reading, 0, 1) == 0) {
  509. if (atomic_inc_return(&port->v4l_reader_count) == 1) {
  510. if (saa7164_vbi_initialize(port) < 0)
  511. return -EINVAL;
  512. saa7164_vbi_start_streaming(port);
  513. msleep(200);
  514. }
  515. }
  516. /* blocking wait for buffer */
  517. if ((file->f_flags & O_NONBLOCK) == 0) {
  518. if (wait_event_interruptible(port->wait_read,
  519. saa7164_vbi_next_buf(port))) {
  520. return -ERESTARTSYS;
  521. }
  522. }
  523. /* Pull the first buffer from the used list */
  524. if (!list_empty(&port->list_buf_used.list))
  525. mask |= POLLIN | POLLRDNORM;
  526. return mask;
  527. }
  528. static const struct v4l2_file_operations vbi_fops = {
  529. .owner = THIS_MODULE,
  530. .open = fops_open,
  531. .release = fops_release,
  532. .read = fops_read,
  533. .poll = fops_poll,
  534. .unlocked_ioctl = video_ioctl2,
  535. };
  536. static const struct v4l2_ioctl_ops vbi_ioctl_ops = {
  537. .vidioc_s_std = vidioc_s_std,
  538. .vidioc_g_std = vidioc_g_std,
  539. .vidioc_enum_input = saa7164_enum_input,
  540. .vidioc_g_input = vidioc_g_input,
  541. .vidioc_s_input = vidioc_s_input,
  542. .vidioc_g_tuner = saa7164_g_tuner,
  543. .vidioc_s_tuner = saa7164_s_tuner,
  544. .vidioc_g_frequency = vidioc_g_frequency,
  545. .vidioc_s_frequency = vidioc_s_frequency,
  546. .vidioc_querycap = vidioc_querycap,
  547. .vidioc_g_fmt_vbi_cap = saa7164_vbi_fmt,
  548. .vidioc_try_fmt_vbi_cap = saa7164_vbi_fmt,
  549. .vidioc_s_fmt_vbi_cap = saa7164_vbi_fmt,
  550. };
  551. static struct video_device saa7164_vbi_template = {
  552. .name = "saa7164",
  553. .fops = &vbi_fops,
  554. .ioctl_ops = &vbi_ioctl_ops,
  555. .minor = -1,
  556. .tvnorms = SAA7164_NORMS,
  557. };
  558. static struct video_device *saa7164_vbi_alloc(
  559. struct saa7164_port *port,
  560. struct pci_dev *pci,
  561. struct video_device *template,
  562. char *type)
  563. {
  564. struct video_device *vfd;
  565. struct saa7164_dev *dev = port->dev;
  566. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  567. vfd = video_device_alloc();
  568. if (NULL == vfd)
  569. return NULL;
  570. *vfd = *template;
  571. snprintf(vfd->name, sizeof(vfd->name), "%s %s (%s)", dev->name,
  572. type, saa7164_boards[dev->board].name);
  573. vfd->v4l2_dev = &dev->v4l2_dev;
  574. vfd->release = video_device_release;
  575. return vfd;
  576. }
  577. int saa7164_vbi_register(struct saa7164_port *port)
  578. {
  579. struct saa7164_dev *dev = port->dev;
  580. int result = -ENODEV;
  581. dprintk(DBGLVL_VBI, "%s()\n", __func__);
  582. if (port->type != SAA7164_MPEG_VBI)
  583. BUG();
  584. /* Sanity check that the PCI configuration space is active */
  585. if (port->hwcfg.BARLocation == 0) {
  586. printk(KERN_ERR "%s() failed (errno = %d), NO PCI configuration\n",
  587. __func__, result);
  588. result = -ENOMEM;
  589. goto failed;
  590. }
  591. /* Establish VBI defaults here */
  592. /* Allocate and register the video device node */
  593. port->v4l_device = saa7164_vbi_alloc(port,
  594. dev->pci, &saa7164_vbi_template, "vbi");
  595. if (!port->v4l_device) {
  596. printk(KERN_INFO "%s: can't allocate vbi device\n",
  597. dev->name);
  598. result = -ENOMEM;
  599. goto failed;
  600. }
  601. port->enc_port = &dev->ports[port->nr - 2];
  602. video_set_drvdata(port->v4l_device, port);
  603. result = video_register_device(port->v4l_device,
  604. VFL_TYPE_VBI, -1);
  605. if (result < 0) {
  606. printk(KERN_INFO "%s: can't register vbi device\n",
  607. dev->name);
  608. /* TODO: We're going to leak here if we don't dealloc
  609. The buffers above. The unreg function can't deal wit it.
  610. */
  611. goto failed;
  612. }
  613. printk(KERN_INFO "%s: registered device vbi%d [vbi]\n",
  614. dev->name, port->v4l_device->num);
  615. /* Configure the hardware defaults */
  616. result = 0;
  617. failed:
  618. return result;
  619. }
  620. void saa7164_vbi_unregister(struct saa7164_port *port)
  621. {
  622. struct saa7164_dev *dev = port->dev;
  623. dprintk(DBGLVL_VBI, "%s(port=%d)\n", __func__, port->nr);
  624. if (port->type != SAA7164_MPEG_VBI)
  625. BUG();
  626. if (port->v4l_device) {
  627. if (port->v4l_device->minor != -1)
  628. video_unregister_device(port->v4l_device);
  629. else
  630. video_device_release(port->v4l_device);
  631. port->v4l_device = NULL;
  632. }
  633. }