saa7164-vbi.c 19 KB

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