cx231xx-vbi.c 17 KB

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