saa7164-vbi.c 20 KB

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