cx231xx-vbi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 synchronously";
  38. break;
  39. case -ECONNRESET:
  40. errmsg = "unlinked asynchronously";
  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. const 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. unsigned long flags;
  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. dev_err(dev->dev,
  266. "urb completion error %d.\n", urb->status);
  267. break;
  268. }
  269. /* Copy data from URB */
  270. spin_lock_irqsave(&dev->vbi_mode.slock, flags);
  271. dev->vbi_mode.bulk_ctl.bulk_copy(dev, urb);
  272. spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
  273. /* Reset status */
  274. urb->status = 0;
  275. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  276. if (urb->status) {
  277. dev_err(dev->dev, "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. dev_dbg(dev->dev, "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. dev_dbg(dev->dev, "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 = kcalloc(num_bufs, sizeof(void *),
  349. GFP_KERNEL);
  350. if (!dev->vbi_mode.bulk_ctl.urb) {
  351. dev_err(dev->dev,
  352. "cannot alloc memory for usb buffers\n");
  353. return -ENOMEM;
  354. }
  355. dev->vbi_mode.bulk_ctl.transfer_buffer =
  356. kcalloc(num_bufs, sizeof(void *), GFP_KERNEL);
  357. if (!dev->vbi_mode.bulk_ctl.transfer_buffer) {
  358. dev_err(dev->dev,
  359. "cannot allocate memory for usbtransfer\n");
  360. kfree(dev->vbi_mode.bulk_ctl.urb);
  361. return -ENOMEM;
  362. }
  363. dev->vbi_mode.bulk_ctl.max_pkt_size = max_pkt_size;
  364. dev->vbi_mode.bulk_ctl.buf = NULL;
  365. sb_size = max_packets * dev->vbi_mode.bulk_ctl.max_pkt_size;
  366. /* allocate urbs and transfer buffers */
  367. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  368. urb = usb_alloc_urb(0, GFP_KERNEL);
  369. if (!urb) {
  370. cx231xx_uninit_vbi_isoc(dev);
  371. return -ENOMEM;
  372. }
  373. dev->vbi_mode.bulk_ctl.urb[i] = urb;
  374. urb->transfer_flags = 0;
  375. dev->vbi_mode.bulk_ctl.transfer_buffer[i] =
  376. kzalloc(sb_size, GFP_KERNEL);
  377. if (!dev->vbi_mode.bulk_ctl.transfer_buffer[i]) {
  378. dev_err(dev->dev,
  379. "unable to allocate %i bytes for transfer buffer %i%s\n",
  380. sb_size, i,
  381. in_interrupt() ? " while in int" : "");
  382. cx231xx_uninit_vbi_isoc(dev);
  383. return -ENOMEM;
  384. }
  385. pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
  386. usb_fill_bulk_urb(urb, dev->udev, pipe,
  387. dev->vbi_mode.bulk_ctl.transfer_buffer[i],
  388. sb_size, cx231xx_irq_vbi_callback, dma_q);
  389. }
  390. init_waitqueue_head(&dma_q->wq);
  391. /* submit urbs and enables IRQ */
  392. for (i = 0; i < dev->vbi_mode.bulk_ctl.num_bufs; i++) {
  393. rc = usb_submit_urb(dev->vbi_mode.bulk_ctl.urb[i], GFP_ATOMIC);
  394. if (rc) {
  395. dev_err(dev->dev,
  396. "submit of urb %i failed (error=%i)\n", i, rc);
  397. cx231xx_uninit_vbi_isoc(dev);
  398. return rc;
  399. }
  400. }
  401. cx231xx_capture_start(dev, 1, Vbi);
  402. return 0;
  403. }
  404. EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
  405. u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  406. u8 sav_eav, u8 *p_buffer, u32 buffer_size)
  407. {
  408. u32 bytes_copied = 0;
  409. int current_field = -1;
  410. switch (sav_eav) {
  411. case SAV_VBI_FIELD1:
  412. current_field = 1;
  413. break;
  414. case SAV_VBI_FIELD2:
  415. current_field = 2;
  416. break;
  417. default:
  418. break;
  419. }
  420. if (current_field < 0)
  421. return bytes_copied;
  422. dma_q->last_sav = sav_eav;
  423. bytes_copied =
  424. cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
  425. current_field);
  426. return bytes_copied;
  427. }
  428. /*
  429. * Announces that a buffer were filled and request the next
  430. */
  431. static inline void vbi_buffer_filled(struct cx231xx *dev,
  432. struct cx231xx_dmaqueue *dma_q,
  433. struct cx231xx_buffer *buf)
  434. {
  435. /* Advice that buffer was filled */
  436. /* dev_dbg(dev->dev, "[%p/%d] wakeup\n", buf, buf->vb.i); */
  437. buf->vb.state = VIDEOBUF_DONE;
  438. buf->vb.field_count++;
  439. v4l2_get_timestamp(&buf->vb.ts);
  440. dev->vbi_mode.bulk_ctl.buf = NULL;
  441. list_del(&buf->vb.queue);
  442. wake_up(&buf->vb.done);
  443. }
  444. u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  445. u8 *p_line, u32 length, int field_number)
  446. {
  447. u32 bytes_to_copy;
  448. struct cx231xx_buffer *buf;
  449. u32 _line_size = dev->width * 2;
  450. if (dma_q->current_field == -1) {
  451. /* Just starting up */
  452. cx231xx_reset_vbi_buffer(dev, dma_q);
  453. }
  454. if (dma_q->current_field != field_number)
  455. dma_q->lines_completed = 0;
  456. /* get the buffer pointer */
  457. buf = dev->vbi_mode.bulk_ctl.buf;
  458. /* Remember the field number for next time */
  459. dma_q->current_field = field_number;
  460. bytes_to_copy = dma_q->bytes_left_in_line;
  461. if (bytes_to_copy > length)
  462. bytes_to_copy = length;
  463. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  464. dma_q->bytes_left_in_line -= bytes_to_copy;
  465. dma_q->is_partial_line =
  466. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  467. return 0;
  468. }
  469. dma_q->is_partial_line = 1;
  470. /* If we don't have a buffer, just return the number of bytes we would
  471. have copied if we had a buffer. */
  472. if (!buf) {
  473. dma_q->bytes_left_in_line -= bytes_to_copy;
  474. dma_q->is_partial_line =
  475. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  476. return bytes_to_copy;
  477. }
  478. /* copy the data to video buffer */
  479. cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
  480. dma_q->pos += bytes_to_copy;
  481. dma_q->bytes_left_in_line -= bytes_to_copy;
  482. if (dma_q->bytes_left_in_line == 0) {
  483. dma_q->bytes_left_in_line = _line_size;
  484. dma_q->lines_completed++;
  485. dma_q->is_partial_line = 0;
  486. if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
  487. vbi_buffer_filled(dev, dma_q, buf);
  488. dma_q->pos = 0;
  489. dma_q->lines_completed = 0;
  490. cx231xx_reset_vbi_buffer(dev, dma_q);
  491. }
  492. }
  493. return bytes_to_copy;
  494. }
  495. /*
  496. * video-buf generic routine to get the next available buffer
  497. */
  498. static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
  499. struct cx231xx_buffer **buf)
  500. {
  501. struct cx231xx_video_mode *vmode =
  502. container_of(dma_q, struct cx231xx_video_mode, vidq);
  503. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  504. char *outp;
  505. if (list_empty(&dma_q->active)) {
  506. dev_err(dev->dev, "No active queue to serve\n");
  507. dev->vbi_mode.bulk_ctl.buf = NULL;
  508. *buf = NULL;
  509. return;
  510. }
  511. /* Get the next buffer */
  512. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  513. /* Cleans up buffer - Useful for testing for frame/URB loss */
  514. outp = videobuf_to_vmalloc(&(*buf)->vb);
  515. memset(outp, 0, (*buf)->vb.size);
  516. dev->vbi_mode.bulk_ctl.buf = *buf;
  517. return;
  518. }
  519. void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
  520. struct cx231xx_dmaqueue *dma_q)
  521. {
  522. struct cx231xx_buffer *buf;
  523. buf = dev->vbi_mode.bulk_ctl.buf;
  524. if (buf == NULL) {
  525. /* first try to get the buffer */
  526. get_next_vbi_buf(dma_q, &buf);
  527. dma_q->pos = 0;
  528. dma_q->current_field = -1;
  529. }
  530. dma_q->bytes_left_in_line = dev->width << 1;
  531. dma_q->lines_completed = 0;
  532. }
  533. int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  534. u8 *p_buffer, u32 bytes_to_copy)
  535. {
  536. u8 *p_out_buffer = NULL;
  537. u32 current_line_bytes_copied = 0;
  538. struct cx231xx_buffer *buf;
  539. u32 _line_size = dev->width << 1;
  540. void *startwrite;
  541. int offset, lencopy;
  542. buf = dev->vbi_mode.bulk_ctl.buf;
  543. if (buf == NULL)
  544. return -EINVAL;
  545. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  546. if (dma_q->bytes_left_in_line != _line_size) {
  547. current_line_bytes_copied =
  548. _line_size - dma_q->bytes_left_in_line;
  549. }
  550. offset = (dma_q->lines_completed * _line_size) +
  551. current_line_bytes_copied;
  552. if (dma_q->current_field == 2) {
  553. /* Populate the second half of the frame */
  554. offset += (dev->width * 2 * dma_q->lines_per_field);
  555. }
  556. /* prepare destination address */
  557. startwrite = p_out_buffer + offset;
  558. lencopy = dma_q->bytes_left_in_line > bytes_to_copy ?
  559. bytes_to_copy : dma_q->bytes_left_in_line;
  560. memcpy(startwrite, p_buffer, lencopy);
  561. return 0;
  562. }
  563. u8 cx231xx_is_vbi_buffer_done(struct cx231xx *dev,
  564. struct cx231xx_dmaqueue *dma_q)
  565. {
  566. u32 height = 0;
  567. height = ((dev->norm & V4L2_STD_625_50) ?
  568. PAL_VBI_LINES : NTSC_VBI_LINES);
  569. if (dma_q->lines_completed == height && dma_q->current_field == 2)
  570. return 1;
  571. else
  572. return 0;
  573. }