saa7164-vbi.c 20 KB

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