cx231xx-vbi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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 "cx231xx.h"
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/module.h>
  21. #include <linux/kernel.h>
  22. #include <linux/bitmap.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/drv-intf/msp3400.h>
  30. #include <media/tuner.h>
  31. #include "cx231xx-vbi.h"
  32. static inline void print_err_status(struct cx231xx *dev, int packet, int status)
  33. {
  34. char *errmsg = "Unknown";
  35. switch (status) {
  36. case -ENOENT:
  37. errmsg = "unlinked synchronuously";
  38. break;
  39. case -ECONNRESET:
  40. errmsg = "unlinked asynchronuously";
  41. break;
  42. case -ENOSR:
  43. errmsg = "Buffer error (overrun)";
  44. break;
  45. case -EPIPE:
  46. errmsg = "Stalled (device not responding)";
  47. break;
  48. case -EOVERFLOW:
  49. errmsg = "Babble (bad cable?)";
  50. break;
  51. case -EPROTO:
  52. errmsg = "Bit-stuff error (bad cable?)";
  53. break;
  54. case -EILSEQ:
  55. errmsg = "CRC/Timeout (could be anything)";
  56. break;
  57. case -ETIME:
  58. errmsg = "Device does not respond";
  59. break;
  60. }
  61. if (packet < 0) {
  62. dev_err(dev->dev,
  63. "URB status %d [%s].\n", status, errmsg);
  64. } else {
  65. dev_err(dev->dev,
  66. "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. BUG_ON(in_interrupt());
  164. /* We used to wait for the buffer to finish here, but this didn't work
  165. because, as we were keeping the state as VIDEOBUF_QUEUED,
  166. videobuf_queue_cancel marked it as finished for us.
  167. (Also, it could wedge forever if the hardware was misconfigured.)
  168. This should be safe; by the time we get here, the buffer isn't
  169. queued anymore. If we ever start marking the buffers as
  170. VIDEOBUF_ACTIVE, it won't be, though.
  171. */
  172. spin_lock_irqsave(&dev->vbi_mode.slock, flags);
  173. if (dev->vbi_mode.bulk_ctl.buf == buf)
  174. dev->vbi_mode.bulk_ctl.buf = NULL;
  175. spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
  176. videobuf_vmalloc_free(&buf->vb);
  177. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  178. }
  179. static int
  180. vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  181. enum v4l2_field field)
  182. {
  183. struct cx231xx_fh *fh = vq->priv_data;
  184. struct cx231xx_buffer *buf =
  185. container_of(vb, struct cx231xx_buffer, vb);
  186. struct cx231xx *dev = fh->dev;
  187. int rc = 0, urb_init = 0;
  188. u32 height = 0;
  189. height = ((dev->norm & V4L2_STD_625_50) ?
  190. PAL_VBI_LINES : NTSC_VBI_LINES);
  191. buf->vb.size = ((dev->width << 1) * height * 2);
  192. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  193. return -EINVAL;
  194. buf->vb.width = dev->width;
  195. buf->vb.height = height;
  196. buf->vb.field = field;
  197. buf->vb.field = V4L2_FIELD_SEQ_TB;
  198. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  199. rc = videobuf_iolock(vq, &buf->vb, NULL);
  200. if (rc < 0)
  201. goto fail;
  202. }
  203. if (!dev->vbi_mode.bulk_ctl.num_bufs)
  204. urb_init = 1;
  205. if (urb_init) {
  206. rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
  207. CX231XX_NUM_VBI_BUFS,
  208. dev->vbi_mode.alt_max_pkt_size[0],
  209. cx231xx_isoc_vbi_copy);
  210. if (rc < 0)
  211. goto fail;
  212. }
  213. buf->vb.state = VIDEOBUF_PREPARED;
  214. return 0;
  215. fail:
  216. free_buffer(vq, buf);
  217. return rc;
  218. }
  219. static void
  220. vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  221. {
  222. struct cx231xx_buffer *buf =
  223. container_of(vb, struct cx231xx_buffer, vb);
  224. struct cx231xx_fh *fh = vq->priv_data;
  225. struct cx231xx *dev = fh->dev;
  226. struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
  227. buf->vb.state = VIDEOBUF_QUEUED;
  228. list_add_tail(&buf->vb.queue, &vidq->active);
  229. }
  230. static void vbi_buffer_release(struct videobuf_queue *vq,
  231. struct videobuf_buffer *vb)
  232. {
  233. struct cx231xx_buffer *buf =
  234. container_of(vb, struct cx231xx_buffer, vb);
  235. free_buffer(vq, buf);
  236. }
  237. struct videobuf_queue_ops cx231xx_vbi_qops = {
  238. .buf_setup = vbi_buffer_setup,
  239. .buf_prepare = vbi_buffer_prepare,
  240. .buf_queue = vbi_buffer_queue,
  241. .buf_release = vbi_buffer_release,
  242. };
  243. /* ------------------------------------------------------------------
  244. URB control
  245. ------------------------------------------------------------------*/
  246. /*
  247. * IRQ callback, called by URB callback
  248. */
  249. static void cx231xx_irq_vbi_callback(struct urb *urb)
  250. {
  251. struct cx231xx_dmaqueue *dma_q = urb->context;
  252. struct cx231xx_video_mode *vmode =
  253. container_of(dma_q, struct cx231xx_video_mode, vidq);
  254. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  255. switch (urb->status) {
  256. case 0: /* success */
  257. case -ETIMEDOUT: /* NAK */
  258. break;
  259. case -ECONNRESET: /* kill */
  260. case -ENOENT:
  261. case -ESHUTDOWN:
  262. return;
  263. default: /* error */
  264. dev_err(dev->dev,
  265. "urb completition error %d.\n", urb->status);
  266. break;
  267. }
  268. /* Copy data from URB */
  269. spin_lock(&dev->vbi_mode.slock);
  270. dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
  271. spin_unlock(&dev->vbi_mode.slock);
  272. /* Reset status */
  273. urb->status = 0;
  274. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  275. if (urb->status) {
  276. dev_err(dev->dev, "urb resubmit failed (error=%i)\n",
  277. urb->status);
  278. }
  279. }
  280. /*
  281. * Stop and Deallocate URBs
  282. */
  283. void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
  284. {
  285. struct urb *urb;
  286. int i;
  287. dev_dbg(dev->dev, "called cx231xx_uninit_vbi_isoc\n");
  288. dev->vbi_mode.bulk_ctl.nfields = -1;
  289. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  290. urb = dev->vbi_mode.bulk_ctl.urb[i];
  291. if (urb) {
  292. if (!irqs_disabled())
  293. usb_kill_urb(urb);
  294. else
  295. usb_unlink_urb(urb);
  296. if (dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
  297. kfree(dev->vbi_mode.bulk_ctl.
  298. transfer_buffer[i]);
  299. dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
  300. NULL;
  301. }
  302. usb_free_urb(urb);
  303. dev->vbi_mode.bulk_ctl.urb[i] = NULL;
  304. }
  305. dev->vbi_mode.bulk_ctl.transfer_buffer[i] = NULL;
  306. }
  307. kfree(dev->vbi_mode.bulk_ctl.urb);
  308. kfree(dev->vbi_mode.bulk_ctl.transfer_buffer);
  309. dev->vbi_mode.bulk_ctl.urb = NULL;
  310. dev->vbi_mode.bulk_ctl.transfer_buffer = NULL;
  311. dev->vbi_mode.bulk_ctl.num_bufs = 0;
  312. cx231xx_capture_start(dev, 0, Vbi);
  313. }
  314. EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
  315. /*
  316. * Allocate URBs and start IRQ
  317. */
  318. int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
  319. int num_bufs, int max_pkt_size,
  320. int (*bulk_copy) (struct cx231xx *dev,
  321. struct urb *urb))
  322. {
  323. struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
  324. int i;
  325. int sb_size, pipe;
  326. struct urb *urb;
  327. int rc;
  328. dev_dbg(dev->dev, "called cx231xx_vbi_isoc\n");
  329. /* De-allocates all pending stuff */
  330. cx231xx_uninit_vbi_isoc(dev);
  331. /* clear if any halt */
  332. usb_clear_halt(dev->udev,
  333. usb_rcvbulkpipe(dev->udev,
  334. dev->vbi_mode.end_point_addr));
  335. dev->vbi_mode.bulk_ctl.bulk_copy = bulk_copy;
  336. dev->vbi_mode.bulk_ctl.num_bufs = num_bufs;
  337. dma_q->pos = 0;
  338. dma_q->is_partial_line = 0;
  339. dma_q->last_sav = 0;
  340. dma_q->current_field = -1;
  341. dma_q->bytes_left_in_line = dev->width << 1;
  342. dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
  343. PAL_VBI_LINES : NTSC_VBI_LINES);
  344. dma_q->lines_completed = 0;
  345. for (i = 0; i < 8; i++)
  346. dma_q->partial_buf[i] = 0;
  347. dev->vbi_mode.bulk_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
  348. GFP_KERNEL);
  349. if (!dev->vbi_mode.bulk_ctl.urb) {
  350. dev_err(dev->dev,
  351. "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. dev_err(dev->dev,
  358. "cannot allocate memory for usbtransfer\n");
  359. kfree(dev->vbi_mode.bulk_ctl.urb);
  360. return -ENOMEM;
  361. }
  362. dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
  363. dev->vbi_mode.bulk_ctl.buf = NULL;
  364. sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
  365. /* allocate urbs and transfer buffers */
  366. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  367. urb = usb_alloc_urb(0, GFP_KERNEL);
  368. if (!urb) {
  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. dev_err(dev->dev,
  378. "unable to allocate %i bytes for transfer buffer %i%s\n",
  379. sb_size, i,
  380. in_interrupt() ? " while in int" : "");
  381. cx231xx_uninit_vbi_isoc(dev);
  382. return -ENOMEM;
  383. }
  384. pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
  385. usb_fill_bulk_urb(urb, dev->udev, pipe,
  386. dev->vbi_mode.bulk_ctl.transfer_buffer[i],
  387. sb_size, cx231xx_irq_vbi_callback, dma_q);
  388. }
  389. init_waitqueue_head(&dma_q->wq);
  390. /* submit urbs and enables IRQ */
  391. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  392. rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
  393. if (rc) {
  394. dev_err(dev->dev,
  395. "submit of urb %i failed (error=%i)\n", i, rc);
  396. cx231xx_uninit_vbi_isoc(dev);
  397. return rc;
  398. }
  399. }
  400. cx231xx_capture_start(dev, 1, Vbi);
  401. return 0;
  402. }
  403. EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
  404. u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  405. u8 sav_eav, u8 *p_buffer, u32 buffer_size)
  406. {
  407. u32 bytes_copied = 0;
  408. int current_field = -1;
  409. switch (sav_eav) {
  410. case SAV_VBI_FIELD1:
  411. current_field = 1;
  412. break;
  413. case SAV_VBI_FIELD2:
  414. current_field = 2;
  415. break;
  416. default:
  417. break;
  418. }
  419. if (current_field < 0)
  420. return bytes_copied;
  421. dma_q->last_sav = sav_eav;
  422. bytes_copied =
  423. cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
  424. current_field);
  425. return bytes_copied;
  426. }
  427. /*
  428. * Announces that a buffer were filled and request the next
  429. */
  430. static inline void vbi_buffer_filled(struct cx231xx *dev,
  431. struct cx231xx_dmaqueue *dma_q,
  432. struct cx231xx_buffer *buf)
  433. {
  434. /* Advice that buffer was filled */
  435. /* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.i); */
  436. buf->vb.state = VIDEOBUF_DONE;
  437. buf->vb.field_count++;
  438. v4l2_get_timestamp(&buf->vb.ts);
  439. dev->vbi_mode.bulk_ctl.buf = NULL;
  440. list_del(&buf->vb.queue);
  441. wake_up(&buf->vb.done);
  442. }
  443. u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  444. u8 *p_line, u32 length, int field_number)
  445. {
  446. u32 bytes_to_copy;
  447. struct cx231xx_buffer *buf;
  448. u32 _line_size = dev->width * 2;
  449. if (dma_q->current_field == -1) {
  450. /* Just starting up */
  451. cx231xx_reset_vbi_buffer(dev, dma_q);
  452. }
  453. if (dma_q->current_field != field_number)
  454. dma_q->lines_completed = 0;
  455. /* get the buffer pointer */
  456. buf = dev->vbi_mode.bulk_ctl.buf;
  457. /* Remember the field number for next time */
  458. dma_q->current_field = field_number;
  459. bytes_to_copy = dma_q->bytes_left_in_line;
  460. if (bytes_to_copy > length)
  461. bytes_to_copy = length;
  462. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  463. dma_q->bytes_left_in_line -= bytes_to_copy;
  464. dma_q->is_partial_line =
  465. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  466. return 0;
  467. }
  468. dma_q->is_partial_line = 1;
  469. /* If we don't have a buffer, just return the number of bytes we would
  470. have copied if we had a buffer. */
  471. if (!buf) {
  472. dma_q->bytes_left_in_line -= bytes_to_copy;
  473. dma_q->is_partial_line =
  474. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  475. return bytes_to_copy;
  476. }
  477. /* copy the data to video buffer */
  478. cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
  479. dma_q->pos += bytes_to_copy;
  480. dma_q->bytes_left_in_line -= bytes_to_copy;
  481. if (dma_q->bytes_left_in_line == 0) {
  482. dma_q->bytes_left_in_line = _line_size;
  483. dma_q->lines_completed++;
  484. dma_q->is_partial_line = 0;
  485. if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
  486. vbi_buffer_filled(dev, dma_q, buf);
  487. dma_q->pos = 0;
  488. dma_q->lines_completed = 0;
  489. cx231xx_reset_vbi_buffer(dev, dma_q);
  490. }
  491. }
  492. return bytes_to_copy;
  493. }
  494. /*
  495. * video-buf generic routine to get the next available buffer
  496. */
  497. static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
  498. struct cx231xx_buffer **buf)
  499. {
  500. struct cx231xx_video_mode *vmode =
  501. container_of(dma_q, struct cx231xx_video_mode, vidq);
  502. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  503. char *outp;
  504. if (list_empty(&dma_q->active)) {
  505. dev_err(dev->dev, "No active queue to serve\n");
  506. dev->vbi_mode.bulk_ctl.buf = NULL;
  507. *buf = NULL;
  508. return;
  509. }
  510. /* Get the next buffer */
  511. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  512. /* Cleans up buffer - Useful for testing for frame/URB loss */
  513. outp = videobuf_to_vmalloc(&(*buf)->vb);
  514. memset(outp, 0, (*buf)->vb.size);
  515. dev->vbi_mode.bulk_ctl.buf = *buf;
  516. return;
  517. }
  518. void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
  519. struct cx231xx_dmaqueue *dma_q)
  520. {
  521. struct cx231xx_buffer *buf;
  522. buf = dev->vbi_mode.bulk_ctl.buf;
  523. if (buf == NULL) {
  524. /* first try to get the buffer */
  525. get_next_vbi_buf(dma_q, &buf);
  526. dma_q->pos = 0;
  527. dma_q->current_field = -1;
  528. }
  529. dma_q->bytes_left_in_line = dev->width << 1;
  530. dma_q->lines_completed = 0;
  531. }
  532. int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  533. u8 *p_buffer, u32 bytes_to_copy)
  534. {
  535. u8 *p_out_buffer = NULL;
  536. u32 current_line_bytes_copied = 0;
  537. struct cx231xx_buffer *buf;
  538. u32 _line_size = dev->width << 1;
  539. void *startwrite;
  540. int offset, lencopy;
  541. buf = dev->vbi_mode.bulk_ctl.buf;
  542. if (buf == NULL)
  543. return -EINVAL;
  544. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  545. if (dma_q->bytes_left_in_line != _line_size) {
  546. current_line_bytes_copied =
  547. _line_size - dma_q->bytes_left_in_line;
  548. }
  549. offset = (dma_q->lines_completed * _line_size) +
  550. current_line_bytes_copied;
  551. if (dma_q->current_field == 2) {
  552. /* Populate the second half of the frame */
  553. offset += (dev->width * 2 * dma_q->lines_per_field);
  554. }
  555. /* prepare destination address */
  556. startwrite = p_out_buffer + offset;
  557. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  558. bytes_to_copy : dma_q->bytes_left_in_line;
  559. memcpy(startwrite, p_buffer, lencopy);
  560. return 0;
  561. }
  562. u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
  563. struct cx231xx_dmaqueue *dma_q)
  564. {
  565. u32 height = 0;
  566. height = ((dev->norm & V4L2_STD_625_50) ?
  567. PAL_VBI_LINES : NTSC_VBI_LINES);
  568. if (dma_q->lines_completed == height && dma_q->current_field == 2)
  569. return 1;
  570. else
  571. return 0;
  572. }