cx231xx-vbi.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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/version.h>
  25. #include <linux/mm.h>
  26. #include <linux/mutex.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) || (dev->state & DEV_MISCONFIGURED))
  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.isoc_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(p_buffer + bytes_parsed, /* p_buffer */
  122. buffer_size - bytes_parsed, /* buffer size */
  123. &bytes_used); /* Receives bytes used to get SAV/EAV */
  124. bytes_parsed += bytes_used;
  125. sav_eav &= 0xF0;
  126. if (sav_eav && (bytes_parsed < buffer_size)) {
  127. bytes_parsed += cx231xx_get_vbi_line(dev,
  128. dma_q, sav_eav, /* SAV/EAV */
  129. p_buffer + bytes_parsed, /* p_buffer */
  130. buffer_size - bytes_parsed); /* buffer size */
  131. }
  132. }
  133. /* Save the last four bytes of the buffer so we can check the buffer boundary
  134. condition next time */
  135. memcpy(dma_q->partial_buf, p_buffer + buffer_size - 4, 4);
  136. bytes_parsed = 0;
  137. }
  138. return rc;
  139. }
  140. /* ------------------------------------------------------------------
  141. Vbi buf operations
  142. ------------------------------------------------------------------*/
  143. static int
  144. vbi_buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  145. unsigned int *size)
  146. {
  147. struct cx231xx_fh *fh = vq->priv_data;
  148. struct cx231xx *dev = fh->dev;
  149. u32 height = 0;
  150. height = ((dev->norm & V4L2_STD_625_50) ?
  151. PAL_VBI_LINES : NTSC_VBI_LINES);
  152. *size = (dev->width * height * 2);
  153. if (0 == *count)
  154. *count = CX231XX_DEF_VBI_BUF;
  155. if (*count < CX231XX_MIN_BUF)
  156. *count = CX231XX_MIN_BUF;
  157. /* call VBI setup if required */
  158. /* cx231xx_i2c_call_clients(&dev->i2c_bus[1], VIDIOC_S_FREQUENCY, &f);
  159. */
  160. return 0;
  161. }
  162. /* This is called *without* dev->slock held; please keep it that way */
  163. static void free_buffer(struct videobuf_queue *vq, struct cx231xx_buffer *buf)
  164. {
  165. struct cx231xx_fh *fh = vq->priv_data;
  166. struct cx231xx *dev = fh->dev;
  167. unsigned long flags = 0;
  168. if (in_interrupt())
  169. BUG();
  170. /* We used to wait for the buffer to finish here, but this didn't work
  171. because, as we were keeping the state as VIDEOBUF_QUEUED,
  172. videobuf_queue_cancel marked it as finished for us.
  173. (Also, it could wedge forever if the hardware was misconfigured.)
  174. This should be safe; by the time we get here, the buffer isn't
  175. queued anymore. If we ever start marking the buffers as
  176. VIDEOBUF_ACTIVE, it won't be, though.
  177. */
  178. spin_lock_irqsave(&dev->vbi_mode.slock, flags);
  179. if (dev->vbi_mode.isoc_ctl.buf == buf)
  180. dev->vbi_mode.isoc_ctl.buf = NULL;
  181. spin_unlock_irqrestore(&dev->vbi_mode.slock, flags);
  182. videobuf_vmalloc_free(&buf->vb);
  183. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  184. }
  185. static int
  186. vbi_buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  187. enum v4l2_field field)
  188. {
  189. struct cx231xx_fh *fh = vq->priv_data;
  190. struct cx231xx_buffer *buf =
  191. container_of(vb, struct cx231xx_buffer, vb);
  192. struct cx231xx *dev = fh->dev;
  193. int rc = 0, urb_init = 0;
  194. u32 height = 0;
  195. height = ((dev->norm & V4L2_STD_625_50) ?
  196. PAL_VBI_LINES : NTSC_VBI_LINES);
  197. buf->vb.size = ((dev->width << 1) * height);
  198. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  199. return -EINVAL;
  200. buf->vb.width = dev->width;
  201. buf->vb.height = height;
  202. buf->vb.field = field;
  203. buf->vb.field = V4L2_FIELD_SEQ_TB;
  204. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  205. rc = videobuf_iolock(vq, &buf->vb, NULL);
  206. if (rc < 0)
  207. goto fail;
  208. }
  209. if (!dev->vbi_mode.isoc_ctl.num_bufs)
  210. urb_init = 1;
  211. if (urb_init) {
  212. rc = cx231xx_init_vbi_isoc(dev, CX231XX_NUM_VBI_PACKETS,
  213. CX231XX_NUM_VBI_BUFS,
  214. dev->vbi_mode.alt_max_pkt_size[0],
  215. cx231xx_isoc_vbi_copy);
  216. if (rc < 0)
  217. goto fail;
  218. }
  219. buf->vb.state = VIDEOBUF_PREPARED;
  220. return 0;
  221. fail:
  222. free_buffer(vq, buf);
  223. return rc;
  224. }
  225. static void
  226. vbi_buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  227. {
  228. struct cx231xx_buffer *buf =
  229. container_of(vb, struct cx231xx_buffer, vb);
  230. struct cx231xx_fh *fh = vq->priv_data;
  231. struct cx231xx *dev = fh->dev;
  232. struct cx231xx_dmaqueue *vidq = &dev->vbi_mode.vidq;
  233. buf->vb.state = VIDEOBUF_QUEUED;
  234. list_add_tail(&buf->vb.queue, &vidq->active);
  235. }
  236. static void vbi_buffer_release(struct videobuf_queue *vq,
  237. struct videobuf_buffer *vb)
  238. {
  239. struct cx231xx_buffer *buf =
  240. container_of(vb, struct cx231xx_buffer, vb);
  241. free_buffer(vq, buf);
  242. }
  243. struct videobuf_queue_ops cx231xx_vbi_qops = {
  244. .buf_setup = vbi_buffer_setup,
  245. .buf_prepare = vbi_buffer_prepare,
  246. .buf_queue = vbi_buffer_queue,
  247. .buf_release = vbi_buffer_release,
  248. };
  249. /* ------------------------------------------------------------------
  250. URB control
  251. ------------------------------------------------------------------*/
  252. /*
  253. * IRQ callback, called by URB callback
  254. */
  255. static void cx231xx_irq_vbi_callback(struct urb *urb)
  256. {
  257. struct cx231xx_dmaqueue *dma_q = urb->context;
  258. struct cx231xx_video_mode *vmode =
  259. container_of(dma_q, struct cx231xx_video_mode, vidq);
  260. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  261. int rc;
  262. switch (urb->status) {
  263. case 0: /* success */
  264. case -ETIMEDOUT: /* NAK */
  265. break;
  266. case -ECONNRESET: /* kill */
  267. case -ENOENT:
  268. case -ESHUTDOWN:
  269. return;
  270. default: /* error */
  271. cx231xx_err(DRIVER_NAME "urb completition error %d.\n",
  272. urb->status);
  273. break;
  274. }
  275. /* Copy data from URB */
  276. spin_lock(&dev->vbi_mode.slock);
  277. rc = dev->vbi_mode.isoc_ctl.isoc_copy(dev, urb);
  278. spin_unlock(&dev->vbi_mode.slock);
  279. /* Reset status */
  280. urb->status = 0;
  281. urb->status = usb_submit_urb(urb, GFP_ATOMIC);
  282. if (urb->status) {
  283. cx231xx_err(DRIVER_NAME "urb resubmit failed (error=%i)\n",
  284. urb->status);
  285. }
  286. }
  287. /*
  288. * Stop and Deallocate URBs
  289. */
  290. void cx231xx_uninit_vbi_isoc(struct cx231xx *dev)
  291. {
  292. struct urb *urb;
  293. int i;
  294. cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_uninit_vbi_isoc\n");
  295. dev->vbi_mode.isoc_ctl.nfields = -1;
  296. for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
  297. urb = dev->vbi_mode.isoc_ctl.urb[i];
  298. if (urb) {
  299. if (!irqs_disabled())
  300. usb_kill_urb(urb);
  301. else
  302. usb_unlink_urb(urb);
  303. if (dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
  304. kfree(dev->vbi_mode.isoc_ctl.
  305. transfer_buffer[i]);
  306. dev->vbi_mode.isoc_ctl.transfer_buffer[i] =
  307. NULL;
  308. }
  309. usb_free_urb(urb);
  310. dev->vbi_mode.isoc_ctl.urb[i] = NULL;
  311. }
  312. dev->vbi_mode.isoc_ctl.transfer_buffer[i] = NULL;
  313. }
  314. kfree(dev->vbi_mode.isoc_ctl.urb);
  315. kfree(dev->vbi_mode.isoc_ctl.transfer_buffer);
  316. dev->vbi_mode.isoc_ctl.urb = NULL;
  317. dev->vbi_mode.isoc_ctl.transfer_buffer = NULL;
  318. dev->vbi_mode.isoc_ctl.num_bufs = 0;
  319. cx231xx_capture_start(dev, 0, Vbi);
  320. }
  321. EXPORT_SYMBOL_GPL(cx231xx_uninit_vbi_isoc);
  322. /*
  323. * Allocate URBs and start IRQ
  324. */
  325. int cx231xx_init_vbi_isoc(struct cx231xx *dev, int max_packets,
  326. int num_bufs, int max_pkt_size,
  327. int (*isoc_copy) (struct cx231xx *dev,
  328. struct urb *urb))
  329. {
  330. struct cx231xx_dmaqueue *dma_q = &dev->vbi_mode.vidq;
  331. int i;
  332. int sb_size, pipe;
  333. struct urb *urb;
  334. int rc;
  335. cx231xx_info(DRIVER_NAME "cx231xx: called cx231xx_prepare_isoc\n");
  336. /* De-allocates all pending stuff */
  337. cx231xx_uninit_vbi_isoc(dev);
  338. /* clear if any halt */
  339. usb_clear_halt(dev->udev,
  340. usb_rcvbulkpipe(dev->udev,
  341. dev->vbi_mode.end_point_addr));
  342. dev->vbi_mode.isoc_ctl.isoc_copy = isoc_copy;
  343. dev->vbi_mode.isoc_ctl.num_bufs = num_bufs;
  344. dma_q->pos = 0;
  345. dma_q->is_partial_line = 0;
  346. dma_q->last_sav = 0;
  347. dma_q->current_field = -1;
  348. dma_q->bytes_left_in_line = dev->width << 1;
  349. dma_q->lines_per_field = ((dev->norm & V4L2_STD_625_50) ?
  350. PAL_VBI_LINES : NTSC_VBI_LINES);
  351. dma_q->lines_completed = 0;
  352. for (i = 0; i < 8; i++)
  353. dma_q->partial_buf[i] = 0;
  354. dev->vbi_mode.isoc_ctl.urb = kzalloc(sizeof(void *) * num_bufs,
  355. GFP_KERNEL);
  356. if (!dev->vbi_mode.isoc_ctl.urb) {
  357. cx231xx_errdev("cannot alloc memory for usb buffers\n");
  358. return -ENOMEM;
  359. }
  360. dev->vbi_mode.isoc_ctl.transfer_buffer =
  361. kzalloc(sizeof(void *) * num_bufs, GFP_KERNEL);
  362. if (!dev->vbi_mode.isoc_ctl.transfer_buffer) {
  363. cx231xx_errdev("cannot allocate memory for usbtransfer\n");
  364. kfree(dev->vbi_mode.isoc_ctl.urb);
  365. return -ENOMEM;
  366. }
  367. dev->vbi_mode.isoc_ctl.max_pkt_size = max_pkt_size;
  368. dev->vbi_mode.isoc_ctl.buf = NULL;
  369. sb_size = max_packets * dev->vbi_mode.isoc_ctl.max_pkt_size;
  370. /* allocate urbs and transfer buffers */
  371. for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
  372. urb = usb_alloc_urb(0, GFP_KERNEL);
  373. if (!urb) {
  374. cx231xx_err(DRIVER_NAME
  375. ": cannot alloc isoc_ctl.urb %i\n", i);
  376. cx231xx_uninit_vbi_isoc(dev);
  377. return -ENOMEM;
  378. }
  379. dev->vbi_mode.isoc_ctl.urb[i] = urb;
  380. urb->transfer_flags = 0;
  381. dev->vbi_mode.isoc_ctl.transfer_buffer[i] =
  382. kzalloc(sb_size, GFP_KERNEL);
  383. if (!dev->vbi_mode.isoc_ctl.transfer_buffer[i]) {
  384. cx231xx_err(DRIVER_NAME
  385. ": unable to allocate %i bytes for transfer"
  386. " buffer %i%s\n", sb_size, i,
  387. in_interrupt() ? " while in int" : "");
  388. cx231xx_uninit_vbi_isoc(dev);
  389. return -ENOMEM;
  390. }
  391. pipe = usb_rcvbulkpipe(dev->udev, dev->vbi_mode.end_point_addr);
  392. usb_fill_bulk_urb(urb, dev->udev, pipe,
  393. dev->vbi_mode.isoc_ctl.transfer_buffer[i],
  394. sb_size, cx231xx_irq_vbi_callback, dma_q);
  395. }
  396. init_waitqueue_head(&dma_q->wq);
  397. /* submit urbs and enables IRQ */
  398. for (i = 0; i < dev->vbi_mode.isoc_ctl.num_bufs; i++) {
  399. rc = usb_submit_urb(dev->vbi_mode.isoc_ctl.urb[i], GFP_ATOMIC);
  400. if (rc) {
  401. cx231xx_err(DRIVER_NAME
  402. ": submit of urb %i failed (error=%i)\n", i,
  403. rc);
  404. cx231xx_uninit_vbi_isoc(dev);
  405. return rc;
  406. }
  407. }
  408. cx231xx_capture_start(dev, 1, Vbi);
  409. return 0;
  410. }
  411. EXPORT_SYMBOL_GPL(cx231xx_init_vbi_isoc);
  412. u32 cx231xx_get_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  413. u8 sav_eav, u8 *p_buffer, u32 buffer_size)
  414. {
  415. u32 bytes_copied = 0;
  416. int current_field = -1;
  417. switch (sav_eav) {
  418. case SAV_VBI_FIELD1:
  419. current_field = 1;
  420. break;
  421. case SAV_VBI_FIELD2:
  422. current_field = 2;
  423. break;
  424. default:
  425. break;
  426. }
  427. if (current_field < 0)
  428. return bytes_copied;
  429. dma_q->last_sav = sav_eav;
  430. bytes_copied =
  431. cx231xx_copy_vbi_line(dev, dma_q, p_buffer, buffer_size,
  432. current_field);
  433. return bytes_copied;
  434. }
  435. /*
  436. * Announces that a buffer were filled and request the next
  437. */
  438. static inline void vbi_buffer_filled(struct cx231xx *dev,
  439. struct cx231xx_dmaqueue *dma_q,
  440. struct cx231xx_buffer *buf)
  441. {
  442. /* Advice that buffer was filled */
  443. /* cx231xx_info(DRIVER_NAME "[%p/%d] wakeup\n", buf, buf->vb.i); */
  444. buf->vb.state = VIDEOBUF_DONE;
  445. buf->vb.field_count++;
  446. do_gettimeofday(&buf->vb.ts);
  447. dev->vbi_mode.isoc_ctl.buf = NULL;
  448. list_del(&buf->vb.queue);
  449. wake_up(&buf->vb.done);
  450. }
  451. u32 cx231xx_copy_vbi_line(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  452. u8 *p_line, u32 length, int field_number)
  453. {
  454. u32 bytes_to_copy;
  455. struct cx231xx_buffer *buf;
  456. u32 _line_size = dev->width * 2;
  457. if (dma_q->current_field != field_number)
  458. cx231xx_reset_vbi_buffer(dev, dma_q);
  459. /* get the buffer pointer */
  460. buf = dev->vbi_mode.isoc_ctl.buf;
  461. /* Remember the field number for next time */
  462. dma_q->current_field = field_number;
  463. bytes_to_copy = dma_q->bytes_left_in_line;
  464. if (bytes_to_copy > length)
  465. bytes_to_copy = length;
  466. if (dma_q->lines_completed >= dma_q->lines_per_field) {
  467. dma_q->bytes_left_in_line -= bytes_to_copy;
  468. dma_q->is_partial_line =
  469. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  470. return 0;
  471. }
  472. dma_q->is_partial_line = 1;
  473. /* If we don't have a buffer, just return the number of bytes we would
  474. have copied if we had a buffer. */
  475. if (!buf) {
  476. dma_q->bytes_left_in_line -= bytes_to_copy;
  477. dma_q->is_partial_line =
  478. (dma_q->bytes_left_in_line == 0) ? 0 : 1;
  479. return bytes_to_copy;
  480. }
  481. /* copy the data to video buffer */
  482. cx231xx_do_vbi_copy(dev, dma_q, p_line, bytes_to_copy);
  483. dma_q->pos += bytes_to_copy;
  484. dma_q->bytes_left_in_line -= bytes_to_copy;
  485. if (dma_q->bytes_left_in_line == 0) {
  486. dma_q->bytes_left_in_line = _line_size;
  487. dma_q->lines_completed++;
  488. dma_q->is_partial_line = 0;
  489. if (cx231xx_is_vbi_buffer_done(dev, dma_q) && buf) {
  490. vbi_buffer_filled(dev, dma_q, buf);
  491. dma_q->pos = 0;
  492. buf = NULL;
  493. dma_q->lines_completed = 0;
  494. }
  495. }
  496. return bytes_to_copy;
  497. }
  498. /*
  499. * video-buf generic routine to get the next available buffer
  500. */
  501. static inline void get_next_vbi_buf(struct cx231xx_dmaqueue *dma_q,
  502. struct cx231xx_buffer **buf)
  503. {
  504. struct cx231xx_video_mode *vmode =
  505. container_of(dma_q, struct cx231xx_video_mode, vidq);
  506. struct cx231xx *dev = container_of(vmode, struct cx231xx, vbi_mode);
  507. char *outp;
  508. if (list_empty(&dma_q->active)) {
  509. cx231xx_err(DRIVER_NAME ": No active queue to serve\n");
  510. dev->vbi_mode.isoc_ctl.buf = NULL;
  511. *buf = NULL;
  512. return;
  513. }
  514. /* Get the next buffer */
  515. *buf = list_entry(dma_q->active.next, struct cx231xx_buffer, vb.queue);
  516. /* Cleans up buffer - Usefull for testing for frame/URB loss */
  517. outp = videobuf_to_vmalloc(&(*buf)->vb);
  518. memset(outp, 0, (*buf)->vb.size);
  519. dev->vbi_mode.isoc_ctl.buf = *buf;
  520. return;
  521. }
  522. void cx231xx_reset_vbi_buffer(struct cx231xx *dev,
  523. struct cx231xx_dmaqueue *dma_q)
  524. {
  525. struct cx231xx_buffer *buf;
  526. buf = dev->vbi_mode.isoc_ctl.buf;
  527. if (buf == NULL) {
  528. /* first try to get the buffer */
  529. get_next_vbi_buf(dma_q, &buf);
  530. dma_q->pos = 0;
  531. dma_q->current_field = -1;
  532. }
  533. dma_q->bytes_left_in_line = dev->width << 1;
  534. dma_q->lines_completed = 0;
  535. }
  536. int cx231xx_do_vbi_copy(struct cx231xx *dev, struct cx231xx_dmaqueue *dma_q,
  537. u8 *p_buffer, u32 bytes_to_copy)
  538. {
  539. u8 *p_out_buffer = NULL;
  540. u32 current_line_bytes_copied = 0;
  541. struct cx231xx_buffer *buf;
  542. u32 _line_size = dev->width << 1;
  543. void *startwrite;
  544. int offset, lencopy;
  545. buf = dev->vbi_mode.isoc_ctl.buf;
  546. if (buf == NULL)
  547. return -EINVAL;
  548. p_out_buffer = videobuf_to_vmalloc(&buf->vb);
  549. if (dma_q->bytes_left_in_line != _line_size) {
  550. current_line_bytes_copied =
  551. _line_size - dma_q->bytes_left_in_line;
  552. }
  553. offset = (dma_q->lines_completed * _line_size) +
  554. current_line_bytes_copied;
  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. return (dma_q->lines_completed == height) ? 1 : 0;
  569. }