cx231xx-vbi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. /*
  2. cx231xx_vbi.c - driver for Conexant Cx23100/101/102 USB video capture devices
  3. Copyright (C) 2008 <srinivasa.deevi at conexant dot com>
  4. Based on cx88 driver
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/init.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/kernel.h>
  21. #include <linux/bitmap.h>
  22. #include <linux/usb.h>
  23. #include <linux/i2c.h>
  24. #include <linux/mm.h>
  25. #include <linux/mutex.h>
  26. #include <linux/slab.h>
  27. #include <media/v4l2-common.h>
  28. #include <media/v4l2-ioctl.h>
  29. #include <media/msp3400.h>
  30. #include <media/tuner.h>
  31. #include "cx231xx.h"
  32. #include "cx231xx-vbi.h"
  33. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  34. {
  35. char *errmsg = "Unknown";
  36. switch (status) {
  37. case -ENOENT:
  38. errmsg = "unlinked synchronuously";
  39. break;
  40. case -ECONNRESET:
  41. errmsg = "unlinked asynchronuously";
  42. break;
  43. case -ENOSR:
  44. errmsg = "Buffer error (overrun)";
  45. break;
  46. case -EPIPE:
  47. errmsg = "Stalled (device not responding)";
  48. break;
  49. case -EOVERFLOW:
  50. errmsg = "Babble (bad cable?)";
  51. break;
  52. case -EPROTO:
  53. errmsg = "Bit-stuff error (bad cable?)";
  54. break;
  55. case -EILSEQ:
  56. errmsg = "CRC/Timeout (could be anything)";
  57. break;
  58. case -ETIME:
  59. errmsg = "Device does not respond";
  60. break;
  61. }
  62. if (packet < 0) {
  63. cx231xx_err("URB status %d [%s].\n", status,
  64. errmsg);
  65. } else {
  66. cx231xx_err("URB packet %d, status %d [%s].\n",
  67. packet, status, errmsg);
  68. }
  69. }
  70. /*
  71. * Controls the isoc copy of each urb packet
  72. */
  73. static inline int cx231xx_isoc_vbi_copy(struct cx231xx *dev, struct urb *urb)
  74. {
  75. struct cx231xx_dmaqueue *dma_q = urb->context;
  76. int rc = 1;
  77. unsigned char *p_buffer;
  78. u32 bytes_parsed = 0, buffer_size = 0;
  79. u8 sav_eav = 0;
  80. if (!dev)
  81. return 0;
  82. if (dev->state & DEV_DISCONNECTED)
  83. return 0;
  84. if (urb->status < 0) {
  85. print_err_status(dev, -1, urb->status);
  86. if (urb->status == -ENOENT)
  87. return 0;
  88. }
  89. /* get buffer pointer and length */
  90. p_buffer = urb->transfer_buffer;
  91. buffer_size = urb->actual_length;
  92. if (buffer_size > 0) {
  93. bytes_parsed = 0;
  94. if (dma_q->is_partial_line) {
  95. /* Handle the case where we were working on a partial
  96. line */
  97. sav_eav = dma_q->last_sav;
  98. } else {
  99. /* Check for a SAV/EAV overlapping the
  100. buffer boundary */
  101. sav_eav = cx231xx_find_boundary_SAV_EAV(p_buffer,
  102. dma_q->partial_buf,
  103. &bytes_parsed);
  104. }
  105. sav_eav &= 0xF0;
  106. /* Get the first line if we have some portion of an SAV/EAV from
  107. the last buffer or a partial line */
  108. if (sav_eav) {
  109. bytes_parsed += cx231xx_get_vbi_line(dev, dma_q,
  110. sav_eav, /* SAV/EAV */
  111. p_buffer + bytes_parsed, /* p_buffer */
  112. buffer_size - bytes_parsed); /* buffer size */
  113. }
  114. /* Now parse data that is completely in this buffer */
  115. dma_q->is_partial_line = 0;
  116. while (bytes_parsed < buffer_size) {
  117. u32 bytes_used = 0;
  118. sav_eav = cx231xx_find_next_SAV_EAV(
  119. p_buffer + bytes_parsed, /* p_buffer */
  120. buffer_size - bytes_parsed, /* buffer size */
  121. &bytes_used); /* bytes used to get SAV/EAV */
  122. bytes_parsed += bytes_used;
  123. sav_eav &= 0xF0;
  124. if (sav_eav && (bytes_parsed < buffer_size)) {
  125. bytes_parsed += cx231xx_get_vbi_line(dev,
  126. dma_q, sav_eav, /* SAV/EAV */
  127. p_buffer+bytes_parsed, /* p_buffer */
  128. buffer_size-bytes_parsed);/*buf size*/
  129. }
  130. }
  131. /* Save the last four bytes of the buffer so we can
  132. check the buffer boundary condition next time */
  133. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  134. bytes_parsed = 0;
  135. }
  136. return rc;
  137. }
  138. /* ------------------------------------------------------------------
  139. Vbi buf operations
  140. ------------------------------------------------------------------*/
  141. static int
  142. vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  143. unsigned int *size)
  144. {
  145. struct cx231xx_fh *fh = vq->priv_data;
  146. struct cx231xx *dev = fh->dev;
  147. u32 height = 0;
  148. height = ((dev->norm & V4L2_STD_625_50) ?
  149. PAL_VBI_LINES : NTSC_VBI_LINES);
  150. *size = (dev->width * height * 2 * 2);
  151. if (0 == *count)
  152. *count = CX231XX_DEF_VBI_BUF;
  153. if (*count < CX231XX_MIN_BUF)
  154. *count = CX231XX_MIN_BUF;
  155. return 0;
  156. }
  157. /* This is called *without* dev->slock held; please keep it that way */
  158. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  159. {
  160. struct cx231xx_fh *fh = vq->priv_data;
  161. struct cx231xx *dev = fh->dev;
  162. unsigned long flags = 0;
  163. if (in_interrupt())
  164. BUG();
  165. /* We used to wait for the buffer to finish here, but this didn't work
  166. because, as we were keeping the state as VIDEOBUF_QUEUED,
  167. videobuf_queue_cancel marked it as finished for us.
  168. (Also, it could wedge forever if the hardware was misconfigured.)
  169. This should be safe; by the time we get here, the buffer isn't
  170. queued anymore. If we ever start marking the buffers as
  171. VIDEOBUF_ACTIVE, it won't be, though.
  172. */
  173. spin_lock_irqsave(&dev->vbi_mode.slock, flags);
  174. if (dev->vbi_mode.bulk_ctl.buf == buf)
  175. dev->vbi_mode.bulk_ctl.buf = NULL;
  176. spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
  177. videobuf_vmalloc_free(&buf->vb);
  178. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  179. }
  180. static int
  181. vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  182. enum v4l2_field field)
  183. {
  184. struct cx231xx_fh *fh = vq->priv_data;
  185. struct cx231xx_buffer *buf =
  186. container_of(vb, struct cx231xx_buffer, vb);
  187. struct cx231xx *dev = fh->dev;
  188. int rc = 0, urb_init = 0;
  189. u32 height = 0;
  190. height = ((dev->norm & V4L2_STD_625_50) ?
  191. PAL_VBI_LINES : NTSC_VBI_LINES);
  192. buf->vb.size = ((dev->width << 1) * height * 2);
  193. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  194. return -EINVAL;
  195. buf->vb.width = dev->width;
  196. buf->vb.height = height;
  197. buf->vb.field = field;
  198. buf->vb.field = V4L2_FIELD_SEQ_TB;
  199. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  200. rc = videobuf_iolock(vq, &buf->vb, NULL);
  201. if (rc < 0)
  202. goto fail;
  203. }
  204. if (!dev->vbi_mode.bulk_ctl.num_bufs)
  205. urb_init = 1;
  206. if (urb_init) {
  207. rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
  208. CX231XX_NUM_VBI_BUFS,
  209. dev->vbi_mode.alt_max_pkt_size[0],
  210. cx231xx_isoc_vbi_copy);
  211. if (rc < 0)
  212. goto fail;
  213. }
  214. buf->vb.state = VIDEOBUF_PREPARED;
  215. return 0;
  216. fail:
  217. free_buffer(vq, buf);
  218. return rc;
  219. }
  220. static void
  221. vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  222. {
  223. struct cx231xx_buffer *buf =
  224. container_of(vb, struct cx231xx_buffer, vb);
  225. struct cx231xx_fh *fh = vq->priv_data;
  226. struct cx231xx *dev = fh->dev;
  227. struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
  228. buf->vb.state = VIDEOBUF_QUEUED;
  229. list_add_tail(&buf->vb.queue, &vidq->active);
  230. }
  231. static void vbi_buffer_release(struct videobuf_queue *vq,
  232. struct videobuf_buffer *vb)
  233. {
  234. struct cx231xx_buffer *buf =
  235. container_of(vb, struct cx231xx_buffer, vb);
  236. free_buffer(vq, buf);
  237. }
  238. struct videobuf_queue_ops cx231xx_vbi_qops = {
  239. .buf_setup = vbi_buffer_setup,
  240. .buf_prepare = vbi_buffer_prepare,
  241. .buf_queue = vbi_buffer_queue,
  242. .buf_release = vbi_buffer_release,
  243. };
  244. /* ------------------------------------------------------------------
  245. URB control
  246. ------------------------------------------------------------------*/
  247. /*
  248. * IRQ callback, called by URB callback
  249. */
  250. static void cx231xx_irq_vbi_callback(struct urb *urb)
  251. {
  252. struct cx231xx_dmaqueue *dma_q = urb->context;
  253. struct cx231xx_video_mode *vmode =
  254. container_of(dma_q, struct cx231xx_video_mode, vidq);
  255. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  256. switch (urb->status) {
  257. case 0: /* success */
  258. case -ETIMEDOUT: /* NAK */
  259. break;
  260. case -ECONNRESET: /* kill */
  261. case -ENOENT:
  262. case -ESHUTDOWN:
  263. return;
  264. default: /* error */
  265. cx231xx_err("urb completition error %d.\n",
  266. urb->status);
  267. break;
  268. }
  269. /* Copy data from URB */
  270. spin_lock(&dev->vbi_mode.slock);
  271. dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
  272. spin_unlock(&dev->vbi_mode.slock);
  273. /* Reset status */
  274. urb->status = 0;
  275. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  276. if (urb->status) {
  277. cx231xx_err("urb resubmit failed (error=%i)\n",
  278. urb->status);
  279. }
  280. }
  281. /*
  282. * Stop and Deallocate URBs
  283. */
  284. void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
  285. {
  286. struct urb *urb;
  287. int i;
  288. cx231xx_info("called cx231xx_uninit_vbi_isoc\n");
  289. dev->vbi_mode.bulk_ctl.nfields = -1;
  290. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  291. urb = dev->vbi_mode.bulk_ctl.urb[i];
  292. if (urb) {
  293. if (!irqs_disabled())
  294. usb_kill_urb(urb);
  295. else
  296. usb_unlink_urb(urb);
  297. if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
  298. kfree(dev->vbi_mode.bulk_ctl.
  299. transfer_buffer[i]);
  300. dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
  301. NULL;
  302. }
  303. usb_free_urb(urb);
  304. dev->vbi_mode.bulk_ctl.urb[i] = NULL;
  305. }
  306. dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
  307. }
  308. kfree(dev->vbi_mode.bulk_ctl.urb);
  309. kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
  310. dev->vbi_mode.bulk_ctl.urb = NULL;
  311. dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
  312. dev->vbi_mode.bulk_ctl.num_bufs = 0;
  313. cx231xx_capture_start(dev, 0, Vbi);
  314. }
  315. EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
  316. /*
  317. * Allocate URBs and start IRQ
  318. */
  319. int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
  320. int num_bufs, int max_pkt_size,
  321. int (*bulk_copy) (struct cx231xx *dev,
  322. struct urb *urb))
  323. {
  324. struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
  325. int i;
  326. int sb_size, pipe;
  327. struct urb *urb;
  328. int rc;
  329. cx231xx_info("called cx231xx_vbi_isoc\n");
  330. /* De-allocates all pending stuff */
  331. cx231xx_uninit_vbi_isoc(dev);
  332. /* clear if any halt */
  333. usb_clear_halt(dev->udev,
  334. usb_rcvbulkpipe(dev->udev,
  335. dev->vbi_mode.end_point_addr));
  336. dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
  337. dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
  338. dma_q->pos = 0;
  339. dma_q->is_partial_line = 0;
  340. dma_q->last_sav = 0;
  341. dma_q->current_field = -1;
  342. dma_q->bytes_left_in_line = dev->width << 1;
  343. dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
  344. PAL_VBI_LINES : NTSC_VBI_LINES);
  345. dma_q->lines_completed = 0;
  346. for (i = 0; i < 8; i++)
  347. dma_q->partial_buf[i] = 0;
  348. dev->vbi_mode.bulk_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
  349. GFP_KERNEL);
  350. if (!dev->vbi_mode.bulk_ctl.urb) {
  351. cx231xx_errdev("cannot alloc memory for usb buffers\n");
  352. return -ENOMEM;
  353. }
  354. dev->vbi_mode.bulk_ctl.transfer_buffer =
  355. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  356. if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
  357. cx231xx_errdev("cannot allocate memory for usbtransfer\n");
  358. kfree(dev->vbi_mode.bulk_ctl.urb);
  359. return -ENOMEM;
  360. }
  361. dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
  362. dev->vbi_mode.bulk_ctl.buf = NULL;
  363. sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
  364. /* allocate urbs and transfer buffers */
  365. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  366. urb = usb_alloc_urb(0, GFP_KERNEL);
  367. if (!urb) {
  368. cx231xx_err("cannot alloc bulk_ctl.urb %i\n", i);
  369. cx231xx_uninit_vbi_isoc(dev);
  370. return -ENOMEM;
  371. }
  372. dev->vbi_mode.bulk_ctl.urb[i] = urb;
  373. urb->transfer_flags = 0;
  374. dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
  375. kzalloc(sb_size, GFP_KERNEL);
  376. if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
  377. cx231xx_err("unable to allocate %i bytes for transfer"
  378. " buffer %i%s\n", sb_size, i,
  379. in_interrupt() ? " while in int" : "");
  380. cx231xx_uninit_vbi_isoc(dev);
  381. return -ENOMEM;
  382. }
  383. pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
  384. usb_fill_bulk_urb(urb, dev->udev, pipe,
  385. dev->vbi_mode.bulk_ctl.transfer_buffer[i],
  386. sb_size, cx231xx_irq_vbi_callback, dma_q);
  387. }
  388. init_waitqueue_head(&dma_q->wq);
  389. /* submit urbs and enables IRQ */
  390. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  391. rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
  392. if (rc) {
  393. cx231xx_err("submit of urb %i failed (error=%i)\n", i,
  394. rc);
  395. cx231xx_uninit_vbi_isoc(dev);
  396. return rc;
  397. }
  398. }
  399. cx231xx_capture_start(dev, 1, Vbi);
  400. return 0;
  401. }
  402. EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
  403. u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  404. u8 sav_eav, u8 *p_buffer, u32 buffer_size)
  405. {
  406. u32 bytes_copied = 0;
  407. int current_field = -1;
  408. switch (sav_eav) {
  409. case SAV_VBI_FIELD1:
  410. current_field = 1;
  411. break;
  412. case SAV_VBI_FIELD2:
  413. current_field = 2;
  414. break;
  415. default:
  416. break;
  417. }
  418. if (current_field < 0)
  419. return bytes_copied;
  420. dma_q->last_sav = sav_eav;
  421. bytes_copied =
  422. cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
  423. current_field);
  424. return bytes_copied;
  425. }
  426. /*
  427. * Announces that a buffer were filled and request the next
  428. */
  429. static inline void vbi_buffer_filled(struct cx231xx *dev,
  430. struct cx231xx_dmaqueue *dma_q,
  431. struct cx231xx_buffer *buf)
  432. {
  433. /* Advice that buffer was filled */
  434. /* cx231xx_info("[%p/%d] wakeup\n", buf, buf->vb.i); */
  435. buf->vb.state = VIDEOBUF_DONE;
  436. buf->vb.field_count++;
  437. v4l2_get_timestamp(&buf->vb.ts);
  438. dev->vbi_mode.bulk_ctl.buf = NULL;
  439. list_del(&buf->vb.queue);
  440. wake_up(&buf->vb.done);
  441. }
  442. u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  443. u8 *p_line, u32 length, int field_number)
  444. {
  445. u32 bytes_to_copy;
  446. struct cx231xx_buffer *buf;
  447. u32 _line_size = dev->width * 2;
  448. if (dma_q->current_field == -1) {
  449. /* Just starting up */
  450. cx231xx_reset_vbi_buffer(dev, dma_q);
  451. }
  452. if (dma_q->current_field != field_number)
  453. dma_q->lines_completed = 0;
  454. /* get the buffer pointer */
  455. buf = dev->vbi_mode.bulk_ctl.buf;
  456. /* Remember the field number for next time */
  457. dma_q->current_field = field_number;
  458. bytes_to_copy = dma_q->bytes_left_in_line;
  459. if (bytes_to_copy > length)
  460. bytes_to_copy = length;
  461. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  462. dma_q->bytes_left_in_line -= bytes_to_copy;
  463. dma_q->is_partial_line =
  464. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  465. return 0;
  466. }
  467. dma_q->is_partial_line = 1;
  468. /* If we don't have a buffer, just return the number of bytes we would
  469. have copied if we had a buffer. */
  470. if (!buf) {
  471. dma_q->bytes_left_in_line -= bytes_to_copy;
  472. dma_q->is_partial_line =
  473. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  474. return bytes_to_copy;
  475. }
  476. /* copy the data to video buffer */
  477. cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
  478. dma_q->pos += bytes_to_copy;
  479. dma_q->bytes_left_in_line -= bytes_to_copy;
  480. if (dma_q->bytes_left_in_line == 0) {
  481. dma_q->bytes_left_in_line = _line_size;
  482. dma_q->lines_completed++;
  483. dma_q->is_partial_line = 0;
  484. if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
  485. vbi_buffer_filled(dev, dma_q, buf);
  486. dma_q->pos = 0;
  487. dma_q->lines_completed = 0;
  488. cx231xx_reset_vbi_buffer(dev, dma_q);
  489. }
  490. }
  491. return bytes_to_copy;
  492. }
  493. /*
  494. * video-buf generic routine to get the next available buffer
  495. */
  496. static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
  497. struct cx231xx_buffer **buf)
  498. {
  499. struct cx231xx_video_mode *vmode =
  500. container_of(dma_q, struct cx231xx_video_mode, vidq);
  501. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  502. char *outp;
  503. if (list_empty(&dma_q->active)) {
  504. cx231xx_err("No active queue to serve\n");
  505. dev->vbi_mode.bulk_ctl.buf = NULL;
  506. *buf = NULL;
  507. return;
  508. }
  509. /* Get the next buffer */
  510. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  511. /* Cleans up buffer - Useful for testing for frame/URB loss */
  512. outp = videobuf_to_vmalloc(&(*buf)->vb);
  513. memset(outp, 0, (*buf)->vb.size);
  514. dev->vbi_mode.bulk_ctl.buf = *buf;
  515. return;
  516. }
  517. void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
  518. struct cx231xx_dmaqueue *dma_q)
  519. {
  520. struct cx231xx_buffer *buf;
  521. buf = dev->vbi_mode.bulk_ctl.buf;
  522. if (buf == NULL) {
  523. /* first try to get the buffer */
  524. get_next_vbi_buf(dma_q, &buf);
  525. dma_q->pos = 0;
  526. dma_q->current_field = -1;
  527. }
  528. dma_q->bytes_left_in_line = dev->width << 1;
  529. dma_q->lines_completed = 0;
  530. }
  531. int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  532. u8 *p_buffer, u32 bytes_to_copy)
  533. {
  534. u8 *p_out_buffer = NULL;
  535. u32 current_line_bytes_copied = 0;
  536. struct cx231xx_buffer *buf;
  537. u32 _line_size = dev->width << 1;
  538. void *startwrite;
  539. int offset, lencopy;
  540. buf = dev->vbi_mode.bulk_ctl.buf;
  541. if (buf == NULL)
  542. return -EINVAL;
  543. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  544. if (dma_q->bytes_left_in_line != _line_size) {
  545. current_line_bytes_copied =
  546. _line_size - dma_q->bytes_left_in_line;
  547. }
  548. offset = (dma_q->lines_completed * _line_size) +
  549. current_line_bytes_copied;
  550. if (dma_q->current_field == 2) {
  551. /* Populate the second half of the frame */
  552. offset += (dev->width * 2 * dma_q->lines_per_field);
  553. }
  554. /* prepare destination address */
  555. startwrite = p_out_buffer + offset;
  556. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  557. bytes_to_copy : dma_q->bytes_left_in_line;
  558. memcpy(startwrite, p_buffer, lencopy);
  559. return 0;
  560. }
  561. u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
  562. struct cx231xx_dmaqueue *dma_q)
  563. {
  564. u32 height = 0;
  565. height = ((dev->norm & V4L2_STD_625_50) ?
  566. PAL_VBI_LINES : NTSC_VBI_LINES);
  567. if (dma_q->lines_completed == height && dma_q->current_field == 2)
  568. return 1;
  569. else
  570. return 0;
  571. }