cx231xx-vbi.c 18 KB

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