PageRenderTime 56ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/usb/media/usbvideo.c

https://bitbucket.org/abioy/linux
C | 1919 lines | 1337 code | 220 blank | 362 comment | 356 complexity | a643f0b087268ca868e6867df19a0892 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * This program is free software; you can redistribute it and/or modify
  3. * it under the terms of the GNU General Public License as published by
  4. * the Free Software Foundation; either version 2, or (at your option)
  5. * any later version.
  6. *
  7. * This program is distributed in the hope that it will be useful,
  8. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. * GNU General Public License for more details.
  11. *
  12. * You should have received a copy of the GNU General Public License
  13. * along with this program; if not, write to the Free Software
  14. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/list.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. #include <linux/mm.h>
  22. #include <linux/smp_lock.h>
  23. #include <linux/vmalloc.h>
  24. #include <linux/init.h>
  25. #include <linux/spinlock.h>
  26. #include <asm/io.h>
  27. #include "usbvideo.h"
  28. #if defined(MAP_NR)
  29. #define virt_to_page(v) MAP_NR(v) /* Kernels 2.2.x */
  30. #endif
  31. static int video_nr = -1;
  32. MODULE_PARM(video_nr, "i");
  33. /*
  34. * Local prototypes.
  35. */
  36. static void usbvideo_Disconnect(struct usb_interface *intf);
  37. static void usbvideo_CameraRelease(struct uvd *uvd);
  38. static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file,
  39. unsigned int cmd, unsigned long arg);
  40. static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma);
  41. static int usbvideo_v4l_open(struct inode *inode, struct file *file);
  42. static ssize_t usbvideo_v4l_read(struct file *file, char *buf,
  43. size_t count, loff_t *ppos);
  44. static int usbvideo_v4l_close(struct inode *inode, struct file *file);
  45. static int usbvideo_StartDataPump(struct uvd *uvd);
  46. static void usbvideo_StopDataPump(struct uvd *uvd);
  47. static int usbvideo_GetFrame(struct uvd *uvd, int frameNum);
  48. static int usbvideo_NewFrame(struct uvd *uvd, int framenum);
  49. static void usbvideo_SoftwareContrastAdjustment(struct uvd *uvd,
  50. struct usbvideo_frame *frame);
  51. /*******************************/
  52. /* Memory management functions */
  53. /*******************************/
  54. /*
  55. * Here we want the physical address of the memory.
  56. * This is used when initializing the contents of the area.
  57. */
  58. unsigned long usbvideo_kvirt_to_pa(unsigned long adr)
  59. {
  60. unsigned long kva, ret;
  61. kva = (unsigned long) page_address(vmalloc_to_page((void *)adr));
  62. kva |= adr & (PAGE_SIZE-1); /* restore the offset */
  63. ret = __pa(kva);
  64. return ret;
  65. }
  66. static void *usbvideo_rvmalloc(unsigned long size)
  67. {
  68. void *mem;
  69. unsigned long adr;
  70. size = PAGE_ALIGN(size);
  71. mem = vmalloc_32(size);
  72. if (!mem)
  73. return NULL;
  74. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  75. adr = (unsigned long) mem;
  76. while (size > 0) {
  77. SetPageReserved(vmalloc_to_page((void *)adr));
  78. adr += PAGE_SIZE;
  79. size -= PAGE_SIZE;
  80. }
  81. return mem;
  82. }
  83. static void usbvideo_rvfree(void *mem, unsigned long size)
  84. {
  85. unsigned long adr;
  86. if (!mem)
  87. return;
  88. adr = (unsigned long) mem;
  89. while ((long) size > 0) {
  90. ClearPageReserved(vmalloc_to_page((void *)adr));
  91. adr += PAGE_SIZE;
  92. size -= PAGE_SIZE;
  93. }
  94. vfree(mem);
  95. }
  96. static void RingQueue_Initialize(struct RingQueue *rq)
  97. {
  98. assert(rq != NULL);
  99. init_waitqueue_head(&rq->wqh);
  100. }
  101. static void RingQueue_Allocate(struct RingQueue *rq, int rqLen)
  102. {
  103. /* Make sure the requested size is a power of 2 and
  104. round up if necessary. This allows index wrapping
  105. using masks rather than modulo */
  106. int i = 1;
  107. assert(rq != NULL);
  108. assert(rqLen > 0);
  109. while(rqLen >> i)
  110. i++;
  111. if(rqLen != 1 << (i-1))
  112. rqLen = 1 << i;
  113. rq->length = rqLen;
  114. rq->ri = rq->wi = 0;
  115. rq->queue = usbvideo_rvmalloc(rq->length);
  116. assert(rq->queue != NULL);
  117. }
  118. static int RingQueue_IsAllocated(const struct RingQueue *rq)
  119. {
  120. if (rq == NULL)
  121. return 0;
  122. return (rq->queue != NULL) && (rq->length > 0);
  123. }
  124. static void RingQueue_Free(struct RingQueue *rq)
  125. {
  126. assert(rq != NULL);
  127. if (RingQueue_IsAllocated(rq)) {
  128. usbvideo_rvfree(rq->queue, rq->length);
  129. rq->queue = NULL;
  130. rq->length = 0;
  131. }
  132. }
  133. int RingQueue_Dequeue(struct RingQueue *rq, unsigned char *dst, int len)
  134. {
  135. int rql, toread;
  136. assert(rq != NULL);
  137. assert(dst != NULL);
  138. rql = RingQueue_GetLength(rq);
  139. if(!rql)
  140. return 0;
  141. /* Clip requested length to available data */
  142. if(len > rql)
  143. len = rql;
  144. toread = len;
  145. if(rq->ri > rq->wi) {
  146. /* Read data from tail */
  147. int read = (toread < (rq->length - rq->ri)) ? toread : rq->length - rq->ri;
  148. memcpy(dst, rq->queue + rq->ri, read);
  149. toread -= read;
  150. dst += read;
  151. rq->ri = (rq->ri + read) & (rq->length-1);
  152. }
  153. if(toread) {
  154. /* Read data from head */
  155. memcpy(dst, rq->queue + rq->ri, toread);
  156. rq->ri = (rq->ri + toread) & (rq->length-1);
  157. }
  158. return len;
  159. }
  160. EXPORT_SYMBOL(RingQueue_Dequeue);
  161. int RingQueue_Enqueue(struct RingQueue *rq, const unsigned char *cdata, int n)
  162. {
  163. int enqueued = 0;
  164. assert(rq != NULL);
  165. assert(cdata != NULL);
  166. assert(rq->length > 0);
  167. while (n > 0) {
  168. int m, q_avail;
  169. /* Calculate the largest chunk that fits the tail of the ring */
  170. q_avail = rq->length - rq->wi;
  171. if (q_avail <= 0) {
  172. rq->wi = 0;
  173. q_avail = rq->length;
  174. }
  175. m = n;
  176. assert(q_avail > 0);
  177. if (m > q_avail)
  178. m = q_avail;
  179. memcpy(rq->queue + rq->wi, cdata, m);
  180. RING_QUEUE_ADVANCE_INDEX(rq, wi, m);
  181. cdata += m;
  182. enqueued += m;
  183. n -= m;
  184. }
  185. return enqueued;
  186. }
  187. EXPORT_SYMBOL(RingQueue_Enqueue);
  188. static void RingQueue_InterruptibleSleepOn(struct RingQueue *rq)
  189. {
  190. assert(rq != NULL);
  191. interruptible_sleep_on(&rq->wqh);
  192. }
  193. void RingQueue_WakeUpInterruptible(struct RingQueue *rq)
  194. {
  195. assert(rq != NULL);
  196. if (waitqueue_active(&rq->wqh))
  197. wake_up_interruptible(&rq->wqh);
  198. }
  199. EXPORT_SYMBOL(RingQueue_WakeUpInterruptible);
  200. void RingQueue_Flush(struct RingQueue *rq)
  201. {
  202. assert(rq != NULL);
  203. rq->ri = 0;
  204. rq->wi = 0;
  205. }
  206. EXPORT_SYMBOL(RingQueue_Flush);
  207. /*
  208. * usbvideo_VideosizeToString()
  209. *
  210. * This procedure converts given videosize value to readable string.
  211. *
  212. * History:
  213. * 07-Aug-2000 Created.
  214. * 19-Oct-2000 Reworked for usbvideo module.
  215. */
  216. static void usbvideo_VideosizeToString(char *buf, int bufLen, videosize_t vs)
  217. {
  218. char tmp[40];
  219. int n;
  220. n = 1 + sprintf(tmp, "%ldx%ld", VIDEOSIZE_X(vs), VIDEOSIZE_Y(vs));
  221. assert(n < sizeof(tmp));
  222. if ((buf == NULL) || (bufLen < n))
  223. err("usbvideo_VideosizeToString: buffer is too small.");
  224. else
  225. memmove(buf, tmp, n);
  226. }
  227. /*
  228. * usbvideo_OverlayChar()
  229. *
  230. * History:
  231. * 01-Feb-2000 Created.
  232. */
  233. static void usbvideo_OverlayChar(struct uvd *uvd, struct usbvideo_frame *frame,
  234. int x, int y, int ch)
  235. {
  236. static const unsigned short digits[16] = {
  237. 0xF6DE, /* 0 */
  238. 0x2492, /* 1 */
  239. 0xE7CE, /* 2 */
  240. 0xE79E, /* 3 */
  241. 0xB792, /* 4 */
  242. 0xF39E, /* 5 */
  243. 0xF3DE, /* 6 */
  244. 0xF492, /* 7 */
  245. 0xF7DE, /* 8 */
  246. 0xF79E, /* 9 */
  247. 0x77DA, /* a */
  248. 0xD75C, /* b */
  249. 0xF24E, /* c */
  250. 0xD6DC, /* d */
  251. 0xF34E, /* e */
  252. 0xF348 /* f */
  253. };
  254. unsigned short digit;
  255. int ix, iy;
  256. if ((uvd == NULL) || (frame == NULL))
  257. return;
  258. if (ch >= '0' && ch <= '9')
  259. ch -= '0';
  260. else if (ch >= 'A' && ch <= 'F')
  261. ch = 10 + (ch - 'A');
  262. else if (ch >= 'a' && ch <= 'f')
  263. ch = 10 + (ch - 'a');
  264. else
  265. return;
  266. digit = digits[ch];
  267. for (iy=0; iy < 5; iy++) {
  268. for (ix=0; ix < 3; ix++) {
  269. if (digit & 0x8000) {
  270. if (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24)) {
  271. /* TODO */ RGB24_PUTPIXEL(frame, x+ix, y+iy, 0xFF, 0xFF, 0xFF);
  272. }
  273. }
  274. digit = digit << 1;
  275. }
  276. }
  277. }
  278. /*
  279. * usbvideo_OverlayString()
  280. *
  281. * History:
  282. * 01-Feb-2000 Created.
  283. */
  284. static void usbvideo_OverlayString(struct uvd *uvd, struct usbvideo_frame *frame,
  285. int x, int y, const char *str)
  286. {
  287. while (*str) {
  288. usbvideo_OverlayChar(uvd, frame, x, y, *str);
  289. str++;
  290. x += 4; /* 3 pixels character + 1 space */
  291. }
  292. }
  293. /*
  294. * usbvideo_OverlayStats()
  295. *
  296. * Overlays important debugging information.
  297. *
  298. * History:
  299. * 01-Feb-2000 Created.
  300. */
  301. static void usbvideo_OverlayStats(struct uvd *uvd, struct usbvideo_frame *frame)
  302. {
  303. const int y_diff = 8;
  304. char tmp[16];
  305. int x = 10, y=10;
  306. long i, j, barLength;
  307. const int qi_x1 = 60, qi_y1 = 10;
  308. const int qi_x2 = VIDEOSIZE_X(frame->request) - 10, qi_h = 10;
  309. /* Call the user callback, see if we may proceed after that */
  310. if (VALID_CALLBACK(uvd, overlayHook)) {
  311. if (GET_CALLBACK(uvd, overlayHook)(uvd, frame) < 0)
  312. return;
  313. }
  314. /*
  315. * We draw a (mostly) hollow rectangle with qi_xxx coordinates.
  316. * Left edge symbolizes the queue index 0; right edge symbolizes
  317. * the full capacity of the queue.
  318. */
  319. barLength = qi_x2 - qi_x1 - 2;
  320. if ((barLength > 10) && (uvd->paletteBits & (1L << VIDEO_PALETTE_RGB24))) {
  321. /* TODO */ long u_lo, u_hi, q_used;
  322. long m_ri, m_wi, m_lo, m_hi;
  323. /*
  324. * Determine fill zones (used areas of the queue):
  325. * 0 xxxxxxx u_lo ...... uvd->dp.ri xxxxxxxx u_hi ..... uvd->dp.length
  326. *
  327. * if u_lo < 0 then there is no first filler.
  328. */
  329. q_used = RingQueue_GetLength(&uvd->dp);
  330. if ((uvd->dp.ri + q_used) >= uvd->dp.length) {
  331. u_hi = uvd->dp.length;
  332. u_lo = (q_used + uvd->dp.ri) & (uvd->dp.length-1);
  333. } else {
  334. u_hi = (q_used + uvd->dp.ri);
  335. u_lo = -1;
  336. }
  337. /* Convert byte indices into screen units */
  338. m_ri = qi_x1 + ((barLength * uvd->dp.ri) / uvd->dp.length);
  339. m_wi = qi_x1 + ((barLength * uvd->dp.wi) / uvd->dp.length);
  340. m_lo = (u_lo > 0) ? (qi_x1 + ((barLength * u_lo) / uvd->dp.length)) : -1;
  341. m_hi = qi_x1 + ((barLength * u_hi) / uvd->dp.length);
  342. for (j=qi_y1; j < (qi_y1 + qi_h); j++) {
  343. for (i=qi_x1; i < qi_x2; i++) {
  344. /* Draw border lines */
  345. if ((j == qi_y1) || (j == (qi_y1 + qi_h - 1)) ||
  346. (i == qi_x1) || (i == (qi_x2 - 1))) {
  347. RGB24_PUTPIXEL(frame, i, j, 0xFF, 0xFF, 0xFF);
  348. continue;
  349. }
  350. /* For all other points the Y coordinate does not matter */
  351. if ((i >= m_ri) && (i <= (m_ri + 3))) {
  352. RGB24_PUTPIXEL(frame, i, j, 0x00, 0xFF, 0x00);
  353. } else if ((i >= m_wi) && (i <= (m_wi + 3))) {
  354. RGB24_PUTPIXEL(frame, i, j, 0xFF, 0x00, 0x00);
  355. } else if ((i < m_lo) || ((i > m_ri) && (i < m_hi)))
  356. RGB24_PUTPIXEL(frame, i, j, 0x00, 0x00, 0xFF);
  357. }
  358. }
  359. }
  360. sprintf(tmp, "%8lx", uvd->stats.frame_num);
  361. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  362. y += y_diff;
  363. sprintf(tmp, "%8lx", uvd->stats.urb_count);
  364. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  365. y += y_diff;
  366. sprintf(tmp, "%8lx", uvd->stats.urb_length);
  367. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  368. y += y_diff;
  369. sprintf(tmp, "%8lx", uvd->stats.data_count);
  370. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  371. y += y_diff;
  372. sprintf(tmp, "%8lx", uvd->stats.header_count);
  373. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  374. y += y_diff;
  375. sprintf(tmp, "%8lx", uvd->stats.iso_skip_count);
  376. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  377. y += y_diff;
  378. sprintf(tmp, "%8lx", uvd->stats.iso_err_count);
  379. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  380. y += y_diff;
  381. sprintf(tmp, "%8x", uvd->vpic.colour);
  382. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  383. y += y_diff;
  384. sprintf(tmp, "%8x", uvd->vpic.hue);
  385. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  386. y += y_diff;
  387. sprintf(tmp, "%8x", uvd->vpic.brightness >> 8);
  388. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  389. y += y_diff;
  390. sprintf(tmp, "%8x", uvd->vpic.contrast >> 12);
  391. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  392. y += y_diff;
  393. sprintf(tmp, "%8d", uvd->vpic.whiteness >> 8);
  394. usbvideo_OverlayString(uvd, frame, x, y, tmp);
  395. y += y_diff;
  396. }
  397. /*
  398. * usbvideo_ReportStatistics()
  399. *
  400. * This procedure prints packet and transfer statistics.
  401. *
  402. * History:
  403. * 14-Jan-2000 Corrected default multiplier.
  404. */
  405. static void usbvideo_ReportStatistics(const struct uvd *uvd)
  406. {
  407. if ((uvd != NULL) && (uvd->stats.urb_count > 0)) {
  408. unsigned long allPackets, badPackets, goodPackets, percent;
  409. allPackets = uvd->stats.urb_count * CAMERA_URB_FRAMES;
  410. badPackets = uvd->stats.iso_skip_count + uvd->stats.iso_err_count;
  411. goodPackets = allPackets - badPackets;
  412. /* Calculate percentage wisely, remember integer limits */
  413. assert(allPackets != 0);
  414. if (goodPackets < (((unsigned long)-1)/100))
  415. percent = (100 * goodPackets) / allPackets;
  416. else
  417. percent = goodPackets / (allPackets / 100);
  418. info("Packet Statistics: Total=%lu. Empty=%lu. Usage=%lu%%",
  419. allPackets, badPackets, percent);
  420. if (uvd->iso_packet_len > 0) {
  421. unsigned long allBytes, xferBytes;
  422. char multiplier = ' ';
  423. allBytes = allPackets * uvd->iso_packet_len;
  424. xferBytes = uvd->stats.data_count;
  425. assert(allBytes != 0);
  426. if (xferBytes < (((unsigned long)-1)/100))
  427. percent = (100 * xferBytes) / allBytes;
  428. else
  429. percent = xferBytes / (allBytes / 100);
  430. /* Scale xferBytes for easy reading */
  431. if (xferBytes > 10*1024) {
  432. xferBytes /= 1024;
  433. multiplier = 'K';
  434. if (xferBytes > 10*1024) {
  435. xferBytes /= 1024;
  436. multiplier = 'M';
  437. if (xferBytes > 10*1024) {
  438. xferBytes /= 1024;
  439. multiplier = 'G';
  440. if (xferBytes > 10*1024) {
  441. xferBytes /= 1024;
  442. multiplier = 'T';
  443. }
  444. }
  445. }
  446. }
  447. info("Transfer Statistics: Transferred=%lu%cB Usage=%lu%%",
  448. xferBytes, multiplier, percent);
  449. }
  450. }
  451. }
  452. /*
  453. * usbvideo_DrawLine()
  454. *
  455. * A standard implementation of Bresenham's line drawing algorithm.
  456. * This procedure is provided primarily for debugging or demo
  457. * purposes.
  458. */
  459. void usbvideo_DrawLine(
  460. struct usbvideo_frame *frame,
  461. int x1, int y1,
  462. int x2, int y2,
  463. unsigned char cr, unsigned char cg, unsigned char cb)
  464. {
  465. int i, dx, dy, np, d;
  466. int dinc1, dinc2, x, xinc1, xinc2, y, yinc1, yinc2;
  467. if ((dx = x2 - x1) < 0)
  468. dx = -dx;
  469. if ((dy = y2 - y1) < 0)
  470. dy = -dy;
  471. if (dx >= dy) {
  472. np = dx + 1;
  473. d = (2 * dy) - dx;
  474. dinc1 = dy << 1;
  475. dinc2 = (dy - dx) << 1;
  476. xinc1 = 1;
  477. xinc2 = 1;
  478. yinc1 = 0;
  479. yinc2 = 1;
  480. } else {
  481. np = dy + 1;
  482. d = (2 * dx) - dy;
  483. dinc1 = dx << 1;
  484. dinc2 = (dx - dy) << 1;
  485. xinc1 = 0;
  486. xinc2 = 1;
  487. yinc1 = 1;
  488. yinc2 = 1;
  489. }
  490. /* Make sure x and y move in the right directions */
  491. if (x1 > x2) {
  492. xinc1 = -xinc1;
  493. xinc2 = -xinc2;
  494. }
  495. if (y1 > y2) {
  496. yinc1 = -yinc1;
  497. yinc2 = -yinc2;
  498. }
  499. for (i=0, x=x1, y=y1; i < np; i++) {
  500. if (frame->palette == VIDEO_PALETTE_RGB24) {
  501. /* TODO */ RGB24_PUTPIXEL(frame, x, y, cr, cg, cb);
  502. }
  503. if (d < 0) {
  504. d += dinc1;
  505. x += xinc1;
  506. y += yinc1;
  507. } else {
  508. d += dinc2;
  509. x += xinc2;
  510. y += yinc2;
  511. }
  512. }
  513. }
  514. EXPORT_SYMBOL(usbvideo_DrawLine);
  515. /*
  516. * usbvideo_TestPattern()
  517. *
  518. * Procedure forms a test pattern (yellow grid on blue background).
  519. *
  520. * Parameters:
  521. * fullframe: if TRUE then entire frame is filled, otherwise the procedure
  522. * continues from the current scanline.
  523. * pmode 0: fill the frame with solid blue color (like on VCR or TV)
  524. * 1: Draw a colored grid
  525. *
  526. * History:
  527. * 01-Feb-2000 Created.
  528. */
  529. void usbvideo_TestPattern(struct uvd *uvd, int fullframe, int pmode)
  530. {
  531. struct usbvideo_frame *frame;
  532. int num_cell = 0;
  533. int scan_length = 0;
  534. static int num_pass = 0;
  535. if (uvd == NULL) {
  536. err("%s: uvd == NULL", __FUNCTION__);
  537. return;
  538. }
  539. if ((uvd->curframe < 0) || (uvd->curframe >= USBVIDEO_NUMFRAMES)) {
  540. err("%s: uvd->curframe=%d.", __FUNCTION__, uvd->curframe);
  541. return;
  542. }
  543. /* Grab the current frame */
  544. frame = &uvd->frame[uvd->curframe];
  545. /* Optionally start at the beginning */
  546. if (fullframe) {
  547. frame->curline = 0;
  548. frame->seqRead_Length = 0;
  549. }
  550. #if 0
  551. { /* For debugging purposes only */
  552. char tmp[20];
  553. usbvideo_VideosizeToString(tmp, sizeof(tmp), frame->request);
  554. info("testpattern: frame=%s", tmp);
  555. }
  556. #endif
  557. /* Form every scan line */
  558. for (; frame->curline < VIDEOSIZE_Y(frame->request); frame->curline++) {
  559. int i;
  560. unsigned char *f = frame->data +
  561. (VIDEOSIZE_X(frame->request) * V4L_BYTES_PER_PIXEL * frame->curline);
  562. for (i=0; i < VIDEOSIZE_X(frame->request); i++) {
  563. unsigned char cb=0x80;
  564. unsigned char cg = 0;
  565. unsigned char cr = 0;
  566. if (pmode == 1) {
  567. if (frame->curline % 32 == 0)
  568. cb = 0, cg = cr = 0xFF;
  569. else if (i % 32 == 0) {
  570. if (frame->curline % 32 == 1)
  571. num_cell++;
  572. cb = 0, cg = cr = 0xFF;
  573. } else {
  574. cb = ((num_cell*7) + num_pass) & 0xFF;
  575. cg = ((num_cell*5) + num_pass*2) & 0xFF;
  576. cr = ((num_cell*3) + num_pass*3) & 0xFF;
  577. }
  578. } else {
  579. /* Just the blue screen */
  580. }
  581. *f++ = cb;
  582. *f++ = cg;
  583. *f++ = cr;
  584. scan_length += 3;
  585. }
  586. }
  587. frame->frameState = FrameState_Done;
  588. frame->seqRead_Length += scan_length;
  589. ++num_pass;
  590. /* We do this unconditionally, regardless of FLAGS_OVERLAY_STATS */
  591. usbvideo_OverlayStats(uvd, frame);
  592. }
  593. EXPORT_SYMBOL(usbvideo_TestPattern);
  594. /*
  595. * usbvideo_HexDump()
  596. *
  597. * A debugging tool. Prints hex dumps.
  598. *
  599. * History:
  600. * 29-Jul-2000 Added printing of offsets.
  601. */
  602. void usbvideo_HexDump(const unsigned char *data, int len)
  603. {
  604. const int bytes_per_line = 32;
  605. char tmp[128]; /* 32*3 + 5 */
  606. int i, k;
  607. for (i=k=0; len > 0; i++, len--) {
  608. if (i > 0 && ((i % bytes_per_line) == 0)) {
  609. printk("%s\n", tmp);
  610. k=0;
  611. }
  612. if ((i % bytes_per_line) == 0)
  613. k += sprintf(&tmp[k], "%04x: ", i);
  614. k += sprintf(&tmp[k], "%02x ", data[i]);
  615. }
  616. if (k > 0)
  617. printk("%s\n", tmp);
  618. }
  619. EXPORT_SYMBOL(usbvideo_HexDump);
  620. /* Debugging aid */
  621. void usbvideo_SayAndWait(const char *what)
  622. {
  623. wait_queue_head_t wq;
  624. init_waitqueue_head(&wq);
  625. info("Say: %s", what);
  626. interruptible_sleep_on_timeout (&wq, HZ*3); /* Timeout */
  627. }
  628. EXPORT_SYMBOL(usbvideo_SayAndWait);
  629. /* ******************************************************************** */
  630. /* XXX: this piece of crap really wants some error handling.. */
  631. static void usbvideo_ClientIncModCount(struct uvd *uvd)
  632. {
  633. if (uvd == NULL) {
  634. err("%s: uvd == NULL", __FUNCTION__);
  635. return;
  636. }
  637. if (uvd->handle == NULL) {
  638. err("%s: uvd->handle == NULL", __FUNCTION__);
  639. return;
  640. }
  641. if (uvd->handle->md_module == NULL) {
  642. err("%s: uvd->handle->md_module == NULL", __FUNCTION__);
  643. return;
  644. }
  645. if (!try_module_get(uvd->handle->md_module)) {
  646. err("%s: try_module_get() == 0", __FUNCTION__);
  647. return;
  648. }
  649. }
  650. static void usbvideo_ClientDecModCount(struct uvd *uvd)
  651. {
  652. if (uvd == NULL) {
  653. err("%s: uvd == NULL", __FUNCTION__);
  654. return;
  655. }
  656. if (uvd->handle == NULL) {
  657. err("%s: uvd->handle == NULL", __FUNCTION__);
  658. return;
  659. }
  660. if (uvd->handle->md_module == NULL) {
  661. err("%s: uvd->handle->md_module == NULL", __FUNCTION__);
  662. return;
  663. }
  664. module_put(uvd->handle->md_module);
  665. }
  666. int usbvideo_register(
  667. struct usbvideo **pCams,
  668. const int num_cams,
  669. const int num_extra,
  670. const char *driverName,
  671. const struct usbvideo_cb *cbTbl,
  672. struct module *md,
  673. const struct usb_device_id *id_table)
  674. {
  675. struct usbvideo *cams;
  676. int i, base_size, result;
  677. /* Check parameters for sanity */
  678. if ((num_cams <= 0) || (pCams == NULL) || (cbTbl == NULL)) {
  679. err("%s: Illegal call", __FUNCTION__);
  680. return -EINVAL;
  681. }
  682. /* Check registration callback - must be set! */
  683. if (cbTbl->probe == NULL) {
  684. err("%s: probe() is required!", __FUNCTION__);
  685. return -EINVAL;
  686. }
  687. base_size = num_cams * sizeof(struct uvd) + sizeof(struct usbvideo);
  688. cams = (struct usbvideo *) kmalloc(base_size, GFP_KERNEL);
  689. if (cams == NULL) {
  690. err("Failed to allocate %d. bytes for usbvideo struct", base_size);
  691. return -ENOMEM;
  692. }
  693. dbg("%s: Allocated $%p (%d. bytes) for %d. cameras",
  694. __FUNCTION__, cams, base_size, num_cams);
  695. memset(cams, 0, base_size);
  696. /* Copy callbacks, apply defaults for those that are not set */
  697. memmove(&cams->cb, cbTbl, sizeof(cams->cb));
  698. if (cams->cb.getFrame == NULL)
  699. cams->cb.getFrame = usbvideo_GetFrame;
  700. if (cams->cb.disconnect == NULL)
  701. cams->cb.disconnect = usbvideo_Disconnect;
  702. if (cams->cb.startDataPump == NULL)
  703. cams->cb.startDataPump = usbvideo_StartDataPump;
  704. if (cams->cb.stopDataPump == NULL)
  705. cams->cb.stopDataPump = usbvideo_StopDataPump;
  706. cams->num_cameras = num_cams;
  707. cams->cam = (struct uvd *) &cams[1];
  708. cams->md_module = md;
  709. if (cams->md_module == NULL)
  710. warn("%s: module == NULL!", __FUNCTION__);
  711. init_MUTEX(&cams->lock); /* to 1 == available */
  712. for (i = 0; i < num_cams; i++) {
  713. struct uvd *up = &cams->cam[i];
  714. up->handle = cams;
  715. /* Allocate user_data separately because of kmalloc's limits */
  716. if (num_extra > 0) {
  717. up->user_size = num_cams * num_extra;
  718. up->user_data = (char *) kmalloc(up->user_size, GFP_KERNEL);
  719. if (up->user_data == NULL) {
  720. err("%s: Failed to allocate user_data (%d. bytes)",
  721. __FUNCTION__, up->user_size);
  722. while (i) {
  723. up = &cams->cam[--i];
  724. kfree(up->user_data);
  725. }
  726. kfree(cams);
  727. return -ENOMEM;
  728. }
  729. dbg("%s: Allocated cams[%d].user_data=$%p (%d. bytes)",
  730. __FUNCTION__, i, up->user_data, up->user_size);
  731. }
  732. }
  733. /*
  734. * Register ourselves with USB stack.
  735. */
  736. strcpy(cams->drvName, (driverName != NULL) ? driverName : "Unknown");
  737. cams->usbdrv.name = cams->drvName;
  738. cams->usbdrv.probe = cams->cb.probe;
  739. cams->usbdrv.disconnect = cams->cb.disconnect;
  740. cams->usbdrv.id_table = id_table;
  741. /*
  742. * Update global handle to usbvideo. This is very important
  743. * because probe() can be called before usb_register() returns.
  744. * If the handle is not yet updated then the probe() will fail.
  745. */
  746. *pCams = cams;
  747. result = usb_register(&cams->usbdrv);
  748. if (result) {
  749. for (i = 0; i < num_cams; i++) {
  750. struct uvd *up = &cams->cam[i];
  751. kfree(up->user_data);
  752. }
  753. kfree(cams);
  754. }
  755. return result;
  756. }
  757. EXPORT_SYMBOL(usbvideo_register);
  758. /*
  759. * usbvideo_Deregister()
  760. *
  761. * Procedure frees all usbvideo and user data structures. Be warned that
  762. * if you had some dynamically allocated components in ->user field then
  763. * you should free them before calling here.
  764. */
  765. void usbvideo_Deregister(struct usbvideo **pCams)
  766. {
  767. struct usbvideo *cams;
  768. int i;
  769. if (pCams == NULL) {
  770. err("%s: pCams == NULL", __FUNCTION__);
  771. return;
  772. }
  773. cams = *pCams;
  774. if (cams == NULL) {
  775. err("%s: cams == NULL", __FUNCTION__);
  776. return;
  777. }
  778. dbg("%s: Deregistering %s driver.", __FUNCTION__, cams->drvName);
  779. usb_deregister(&cams->usbdrv);
  780. dbg("%s: Deallocating cams=$%p (%d. cameras)", __FUNCTION__, cams, cams->num_cameras);
  781. for (i=0; i < cams->num_cameras; i++) {
  782. struct uvd *up = &cams->cam[i];
  783. int warning = 0;
  784. if (up->user_data != NULL) {
  785. if (up->user_size <= 0)
  786. ++warning;
  787. } else {
  788. if (up->user_size > 0)
  789. ++warning;
  790. }
  791. if (warning) {
  792. err("%s: Warning: user_data=$%p user_size=%d.",
  793. __FUNCTION__, up->user_data, up->user_size);
  794. } else {
  795. dbg("%s: Freeing %d. $%p->user_data=$%p",
  796. __FUNCTION__, i, up, up->user_data);
  797. kfree(up->user_data);
  798. }
  799. }
  800. /* Whole array was allocated in one chunk */
  801. dbg("%s: Freed %d uvd structures",
  802. __FUNCTION__, cams->num_cameras);
  803. kfree(cams);
  804. *pCams = NULL;
  805. }
  806. EXPORT_SYMBOL(usbvideo_Deregister);
  807. /*
  808. * usbvideo_Disconnect()
  809. *
  810. * This procedure stops all driver activity. Deallocation of
  811. * the interface-private structure (pointed by 'ptr') is done now
  812. * (if we don't have any open files) or later, when those files
  813. * are closed. After that driver should be removable.
  814. *
  815. * This code handles surprise removal. The uvd->user is a counter which
  816. * increments on open() and decrements on close(). If we see here that
  817. * this counter is not 0 then we have a client who still has us opened.
  818. * We set uvd->remove_pending flag as early as possible, and after that
  819. * all access to the camera will gracefully fail. These failures should
  820. * prompt client to (eventually) close the video device, and then - in
  821. * usbvideo_v4l_close() - we decrement uvd->uvd_used and usage counter.
  822. *
  823. * History:
  824. * 22-Jan-2000 Added polling of MOD_IN_USE to delay removal until all users gone.
  825. * 27-Jan-2000 Reworked to allow pending disconnects; see xxx_close()
  826. * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
  827. * 19-Oct-2000 Moved to usbvideo module.
  828. */
  829. static void usbvideo_Disconnect(struct usb_interface *intf)
  830. {
  831. struct uvd *uvd = usb_get_intfdata (intf);
  832. int i;
  833. if (uvd == NULL) {
  834. err("%s($%p): Illegal call.", __FUNCTION__, intf);
  835. return;
  836. }
  837. usb_set_intfdata (intf, NULL);
  838. usbvideo_ClientIncModCount(uvd);
  839. if (uvd->debug > 0)
  840. info("%s(%p.)", __FUNCTION__, intf);
  841. down(&uvd->lock);
  842. uvd->remove_pending = 1; /* Now all ISO data will be ignored */
  843. /* At this time we ask to cancel outstanding URBs */
  844. GET_CALLBACK(uvd, stopDataPump)(uvd);
  845. for (i=0; i < USBVIDEO_NUMSBUF; i++)
  846. usb_free_urb(uvd->sbuf[i].urb);
  847. usb_put_dev(uvd->dev);
  848. uvd->dev = NULL; /* USB device is no more */
  849. video_unregister_device(&uvd->vdev);
  850. if (uvd->debug > 0)
  851. info("%s: Video unregistered.", __FUNCTION__);
  852. if (uvd->user)
  853. info("%s: In use, disconnect pending.", __FUNCTION__);
  854. else
  855. usbvideo_CameraRelease(uvd);
  856. up(&uvd->lock);
  857. info("USB camera disconnected.");
  858. usbvideo_ClientDecModCount(uvd);
  859. }
  860. /*
  861. * usbvideo_CameraRelease()
  862. *
  863. * This code does final release of uvd. This happens
  864. * after the device is disconnected -and- all clients
  865. * closed their files.
  866. *
  867. * History:
  868. * 27-Jan-2000 Created.
  869. */
  870. static void usbvideo_CameraRelease(struct uvd *uvd)
  871. {
  872. if (uvd == NULL) {
  873. err("%s: Illegal call", __FUNCTION__);
  874. return;
  875. }
  876. RingQueue_Free(&uvd->dp);
  877. if (VALID_CALLBACK(uvd, userFree))
  878. GET_CALLBACK(uvd, userFree)(uvd);
  879. uvd->uvd_used = 0; /* This is atomic, no need to take mutex */
  880. }
  881. /*
  882. * usbvideo_find_struct()
  883. *
  884. * This code searches the array of preallocated (static) structures
  885. * and returns index of the first one that isn't in use. Returns -1
  886. * if there are no free structures.
  887. *
  888. * History:
  889. * 27-Jan-2000 Created.
  890. */
  891. static int usbvideo_find_struct(struct usbvideo *cams)
  892. {
  893. int u, rv = -1;
  894. if (cams == NULL) {
  895. err("No usbvideo handle?");
  896. return -1;
  897. }
  898. down(&cams->lock);
  899. for (u = 0; u < cams->num_cameras; u++) {
  900. struct uvd *uvd = &cams->cam[u];
  901. if (!uvd->uvd_used) /* This one is free */
  902. {
  903. uvd->uvd_used = 1; /* In use now */
  904. init_MUTEX(&uvd->lock); /* to 1 == available */
  905. uvd->dev = NULL;
  906. rv = u;
  907. break;
  908. }
  909. }
  910. up(&cams->lock);
  911. return rv;
  912. }
  913. static struct file_operations usbvideo_fops = {
  914. .owner = THIS_MODULE,
  915. .open = usbvideo_v4l_open,
  916. .release =usbvideo_v4l_close,
  917. .read = usbvideo_v4l_read,
  918. .mmap = usbvideo_v4l_mmap,
  919. .ioctl = usbvideo_v4l_ioctl,
  920. .llseek = no_llseek,
  921. };
  922. static struct video_device usbvideo_template = {
  923. .owner = THIS_MODULE,
  924. .type = VID_TYPE_CAPTURE,
  925. .hardware = VID_HARDWARE_CPIA,
  926. .fops = &usbvideo_fops,
  927. };
  928. struct uvd *usbvideo_AllocateDevice(struct usbvideo *cams)
  929. {
  930. int i, devnum;
  931. struct uvd *uvd = NULL;
  932. if (cams == NULL) {
  933. err("No usbvideo handle?");
  934. return NULL;
  935. }
  936. devnum = usbvideo_find_struct(cams);
  937. if (devnum == -1) {
  938. err("IBM USB camera driver: Too many devices!");
  939. return NULL;
  940. }
  941. uvd = &cams->cam[devnum];
  942. dbg("Device entry #%d. at $%p", devnum, uvd);
  943. /* Not relying upon caller we increase module counter ourselves */
  944. usbvideo_ClientIncModCount(uvd);
  945. down(&uvd->lock);
  946. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  947. uvd->sbuf[i].urb = usb_alloc_urb(FRAMES_PER_DESC, GFP_KERNEL);
  948. if (uvd->sbuf[i].urb == NULL) {
  949. err("usb_alloc_urb(%d.) failed.", FRAMES_PER_DESC);
  950. uvd->uvd_used = 0;
  951. uvd = NULL;
  952. goto allocate_done;
  953. }
  954. }
  955. uvd->user=0;
  956. uvd->remove_pending = 0;
  957. uvd->last_error = 0;
  958. RingQueue_Initialize(&uvd->dp);
  959. /* Initialize video device structure */
  960. uvd->vdev = usbvideo_template;
  961. sprintf(uvd->vdev.name, "%.20s USB Camera", cams->drvName);
  962. /*
  963. * The client is free to overwrite those because we
  964. * return control to the client's probe function right now.
  965. */
  966. allocate_done:
  967. up (&uvd->lock);
  968. usbvideo_ClientDecModCount(uvd);
  969. return uvd;
  970. }
  971. EXPORT_SYMBOL(usbvideo_AllocateDevice);
  972. int usbvideo_RegisterVideoDevice(struct uvd *uvd)
  973. {
  974. char tmp1[20], tmp2[20]; /* Buffers for printing */
  975. if (uvd == NULL) {
  976. err("%s: Illegal call.", __FUNCTION__);
  977. return -EINVAL;
  978. }
  979. if (uvd->video_endp == 0) {
  980. info("%s: No video endpoint specified; data pump disabled.", __FUNCTION__);
  981. }
  982. if (uvd->paletteBits == 0) {
  983. err("%s: No palettes specified!", __FUNCTION__);
  984. return -EINVAL;
  985. }
  986. if (uvd->defaultPalette == 0) {
  987. info("%s: No default palette!", __FUNCTION__);
  988. }
  989. uvd->max_frame_size = VIDEOSIZE_X(uvd->canvas) *
  990. VIDEOSIZE_Y(uvd->canvas) * V4L_BYTES_PER_PIXEL;
  991. usbvideo_VideosizeToString(tmp1, sizeof(tmp1), uvd->videosize);
  992. usbvideo_VideosizeToString(tmp2, sizeof(tmp2), uvd->canvas);
  993. if (uvd->debug > 0) {
  994. info("%s: iface=%d. endpoint=$%02x paletteBits=$%08lx",
  995. __FUNCTION__, uvd->iface, uvd->video_endp, uvd->paletteBits);
  996. }
  997. if (video_register_device(&uvd->vdev, VFL_TYPE_GRABBER, video_nr) == -1) {
  998. err("%s: video_register_device failed", __FUNCTION__);
  999. return -EPIPE;
  1000. }
  1001. if (uvd->debug > 1) {
  1002. info("%s: video_register_device() successful", __FUNCTION__);
  1003. }
  1004. if (uvd->dev == NULL) {
  1005. err("%s: uvd->dev == NULL", __FUNCTION__);
  1006. return -EINVAL;
  1007. }
  1008. info("%s on /dev/video%d: canvas=%s videosize=%s",
  1009. (uvd->handle != NULL) ? uvd->handle->drvName : "???",
  1010. uvd->vdev.minor, tmp2, tmp1);
  1011. usb_get_dev(uvd->dev);
  1012. return 0;
  1013. }
  1014. EXPORT_SYMBOL(usbvideo_RegisterVideoDevice);
  1015. /* ******************************************************************** */
  1016. static int usbvideo_v4l_mmap(struct file *file, struct vm_area_struct *vma)
  1017. {
  1018. struct uvd *uvd = file->private_data;
  1019. unsigned long start = vma->vm_start;
  1020. unsigned long size = vma->vm_end-vma->vm_start;
  1021. unsigned long page, pos;
  1022. if (!CAMERA_IS_OPERATIONAL(uvd))
  1023. return -EFAULT;
  1024. if (size > (((USBVIDEO_NUMFRAMES * uvd->max_frame_size) + PAGE_SIZE - 1) & ~(PAGE_SIZE - 1)))
  1025. return -EINVAL;
  1026. pos = (unsigned long) uvd->fbuf;
  1027. while (size > 0) {
  1028. page = usbvideo_kvirt_to_pa(pos);
  1029. if (remap_page_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  1030. return -EAGAIN;
  1031. start += PAGE_SIZE;
  1032. pos += PAGE_SIZE;
  1033. if (size > PAGE_SIZE)
  1034. size -= PAGE_SIZE;
  1035. else
  1036. size = 0;
  1037. }
  1038. return 0;
  1039. }
  1040. /*
  1041. * usbvideo_v4l_open()
  1042. *
  1043. * This is part of Video 4 Linux API. The driver can be opened by one
  1044. * client only (checks internal counter 'uvdser'). The procedure
  1045. * then allocates buffers needed for video processing.
  1046. *
  1047. * History:
  1048. * 22-Jan-2000 Rewrote, moved scratch buffer allocation here. Now the
  1049. * camera is also initialized here (once per connect), at
  1050. * expense of V4L client (it waits on open() call).
  1051. * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers.
  1052. * 24-May-2000 Corrected to prevent race condition (MOD_xxx_USE_COUNT).
  1053. */
  1054. static int usbvideo_v4l_open(struct inode *inode, struct file *file)
  1055. {
  1056. struct video_device *dev = video_devdata(file);
  1057. struct uvd *uvd = (struct uvd *) dev;
  1058. const int sb_size = FRAMES_PER_DESC * uvd->iso_packet_len;
  1059. int i, errCode = 0;
  1060. if (uvd->debug > 1)
  1061. info("%s($%p)", __FUNCTION__, dev);
  1062. usbvideo_ClientIncModCount(uvd);
  1063. down(&uvd->lock);
  1064. if (uvd->user) {
  1065. err("%s: Someone tried to open an already opened device!", __FUNCTION__);
  1066. errCode = -EBUSY;
  1067. } else {
  1068. /* Clear statistics */
  1069. memset(&uvd->stats, 0, sizeof(uvd->stats));
  1070. /* Clean pointers so we know if we allocated something */
  1071. for (i=0; i < USBVIDEO_NUMSBUF; i++)
  1072. uvd->sbuf[i].data = NULL;
  1073. /* Allocate memory for the frame buffers */
  1074. uvd->fbuf_size = USBVIDEO_NUMFRAMES * uvd->max_frame_size;
  1075. uvd->fbuf = usbvideo_rvmalloc(uvd->fbuf_size);
  1076. RingQueue_Allocate(&uvd->dp, RING_QUEUE_SIZE);
  1077. if ((uvd->fbuf == NULL) ||
  1078. (!RingQueue_IsAllocated(&uvd->dp))) {
  1079. err("%s: Failed to allocate fbuf or dp", __FUNCTION__);
  1080. errCode = -ENOMEM;
  1081. } else {
  1082. /* Allocate all buffers */
  1083. for (i=0; i < USBVIDEO_NUMFRAMES; i++) {
  1084. uvd->frame[i].frameState = FrameState_Unused;
  1085. uvd->frame[i].data = uvd->fbuf + i*(uvd->max_frame_size);
  1086. /*
  1087. * Set default sizes in case IOCTL (VIDIOCMCAPTURE)
  1088. * is not used (using read() instead).
  1089. */
  1090. uvd->frame[i].canvas = uvd->canvas;
  1091. uvd->frame[i].seqRead_Index = 0;
  1092. }
  1093. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  1094. uvd->sbuf[i].data = kmalloc(sb_size, GFP_KERNEL);
  1095. if (uvd->sbuf[i].data == NULL) {
  1096. errCode = -ENOMEM;
  1097. break;
  1098. }
  1099. }
  1100. }
  1101. if (errCode != 0) {
  1102. /* Have to free all that memory */
  1103. if (uvd->fbuf != NULL) {
  1104. usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
  1105. uvd->fbuf = NULL;
  1106. }
  1107. RingQueue_Free(&uvd->dp);
  1108. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  1109. if (uvd->sbuf[i].data != NULL) {
  1110. kfree (uvd->sbuf[i].data);
  1111. uvd->sbuf[i].data = NULL;
  1112. }
  1113. }
  1114. }
  1115. }
  1116. /* If so far no errors then we shall start the camera */
  1117. if (errCode == 0) {
  1118. /* Start data pump if we have valid endpoint */
  1119. if (uvd->video_endp != 0)
  1120. errCode = GET_CALLBACK(uvd, startDataPump)(uvd);
  1121. if (errCode == 0) {
  1122. if (VALID_CALLBACK(uvd, setupOnOpen)) {
  1123. if (uvd->debug > 1)
  1124. info("%s: setupOnOpen callback", __FUNCTION__);
  1125. errCode = GET_CALLBACK(uvd, setupOnOpen)(uvd);
  1126. if (errCode < 0) {
  1127. err("%s: setupOnOpen callback failed (%d.).",
  1128. __FUNCTION__, errCode);
  1129. } else if (uvd->debug > 1) {
  1130. info("%s: setupOnOpen callback successful", __FUNCTION__);
  1131. }
  1132. }
  1133. if (errCode == 0) {
  1134. uvd->settingsAdjusted = 0;
  1135. if (uvd->debug > 1)
  1136. info("%s: Open succeeded.", __FUNCTION__);
  1137. uvd->user++;
  1138. file->private_data = uvd;
  1139. }
  1140. }
  1141. }
  1142. up(&uvd->lock);
  1143. if (errCode != 0)
  1144. usbvideo_ClientDecModCount(uvd);
  1145. if (uvd->debug > 0)
  1146. info("%s: Returning %d.", __FUNCTION__, errCode);
  1147. return errCode;
  1148. }
  1149. /*
  1150. * usbvideo_v4l_close()
  1151. *
  1152. * This is part of Video 4 Linux API. The procedure
  1153. * stops streaming and deallocates all buffers that were earlier
  1154. * allocated in usbvideo_v4l_open().
  1155. *
  1156. * History:
  1157. * 22-Jan-2000 Moved scratch buffer deallocation here.
  1158. * 27-Jan-2000 Used USBVIDEO_NUMSBUF as number of URB buffers.
  1159. * 24-May-2000 Moved MOD_DEC_USE_COUNT outside of code that can sleep.
  1160. */
  1161. static int usbvideo_v4l_close(struct inode *inode, struct file *file)
  1162. {
  1163. struct video_device *dev = file->private_data;
  1164. struct uvd *uvd = (struct uvd *) dev;
  1165. int i;
  1166. if (uvd->debug > 1)
  1167. info("%s($%p)", __FUNCTION__, dev);
  1168. down(&uvd->lock);
  1169. GET_CALLBACK(uvd, stopDataPump)(uvd);
  1170. usbvideo_rvfree(uvd->fbuf, uvd->fbuf_size);
  1171. uvd->fbuf = NULL;
  1172. RingQueue_Free(&uvd->dp);
  1173. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  1174. kfree(uvd->sbuf[i].data);
  1175. uvd->sbuf[i].data = NULL;
  1176. }
  1177. #if USBVIDEO_REPORT_STATS
  1178. usbvideo_ReportStatistics(uvd);
  1179. #endif
  1180. uvd->user--;
  1181. if (uvd->remove_pending) {
  1182. if (uvd->debug > 0)
  1183. info("usbvideo_v4l_close: Final disconnect.");
  1184. usbvideo_CameraRelease(uvd);
  1185. }
  1186. up(&uvd->lock);
  1187. usbvideo_ClientDecModCount(uvd);
  1188. if (uvd->debug > 1)
  1189. info("%s: Completed.", __FUNCTION__);
  1190. file->private_data = NULL;
  1191. return 0;
  1192. }
  1193. /*
  1194. * usbvideo_v4l_ioctl()
  1195. *
  1196. * This is part of Video 4 Linux API. The procedure handles ioctl() calls.
  1197. *
  1198. * History:
  1199. * 22-Jan-2000 Corrected VIDIOCSPICT to reject unsupported settings.
  1200. */
  1201. static int usbvideo_v4l_do_ioctl(struct inode *inode, struct file *file,
  1202. unsigned int cmd, void *arg)
  1203. {
  1204. struct uvd *uvd = file->private_data;
  1205. if (!CAMERA_IS_OPERATIONAL(uvd))
  1206. return -EIO;
  1207. switch (cmd) {
  1208. case VIDIOCGCAP:
  1209. {
  1210. struct video_capability *b = arg;
  1211. *b = uvd->vcap;
  1212. return 0;
  1213. }
  1214. case VIDIOCGCHAN:
  1215. {
  1216. struct video_channel *v = arg;
  1217. *v = uvd->vchan;
  1218. return 0;
  1219. }
  1220. case VIDIOCSCHAN:
  1221. {
  1222. struct video_channel *v = arg;
  1223. if (v->channel != 0)
  1224. return -EINVAL;
  1225. return 0;
  1226. }
  1227. case VIDIOCGPICT:
  1228. {
  1229. struct video_picture *pic = arg;
  1230. *pic = uvd->vpic;
  1231. return 0;
  1232. }
  1233. case VIDIOCSPICT:
  1234. {
  1235. struct video_picture *pic = arg;
  1236. /*
  1237. * Use temporary 'video_picture' structure to preserve our
  1238. * own settings (such as color depth, palette) that we
  1239. * aren't allowing everyone (V4L client) to change.
  1240. */
  1241. uvd->vpic.brightness = pic->brightness;
  1242. uvd->vpic.hue = pic->hue;
  1243. uvd->vpic.colour = pic->colour;
  1244. uvd->vpic.contrast = pic->contrast;
  1245. uvd->settingsAdjusted = 0; /* Will force new settings */
  1246. return 0;
  1247. }
  1248. case VIDIOCSWIN:
  1249. {
  1250. struct video_window *vw = arg;
  1251. if(VALID_CALLBACK(uvd, setVideoMode)) {
  1252. return GET_CALLBACK(uvd, setVideoMode)(uvd, vw);
  1253. }
  1254. if (vw->flags)
  1255. return -EINVAL;
  1256. if (vw->clipcount)
  1257. return -EINVAL;
  1258. if (vw->width != VIDEOSIZE_X(uvd->canvas))
  1259. return -EINVAL;
  1260. if (vw->height != VIDEOSIZE_Y(uvd->canvas))
  1261. return -EINVAL;
  1262. return 0;
  1263. }
  1264. case VIDIOCGWIN:
  1265. {
  1266. struct video_window *vw = arg;
  1267. vw->x = 0;
  1268. vw->y = 0;
  1269. vw->width = VIDEOSIZE_X(uvd->videosize);
  1270. vw->height = VIDEOSIZE_Y(uvd->videosize);
  1271. vw->chromakey = 0;
  1272. if (VALID_CALLBACK(uvd, getFPS))
  1273. vw->flags = GET_CALLBACK(uvd, getFPS)(uvd);
  1274. else
  1275. vw->flags = 10; /* FIXME: do better! */
  1276. return 0;
  1277. }
  1278. case VIDIOCGMBUF:
  1279. {
  1280. struct video_mbuf *vm = arg;
  1281. int i;
  1282. memset(vm, 0, sizeof(*vm));
  1283. vm->size = uvd->max_frame_size * USBVIDEO_NUMFRAMES;
  1284. vm->frames = USBVIDEO_NUMFRAMES;
  1285. for(i = 0; i < USBVIDEO_NUMFRAMES; i++)
  1286. vm->offsets[i] = i * uvd->max_frame_size;
  1287. return 0;
  1288. }
  1289. case VIDIOCMCAPTURE:
  1290. {
  1291. struct video_mmap *vm = arg;
  1292. if (uvd->debug >= 1) {
  1293. info("VIDIOCMCAPTURE: frame=%d. size=%dx%d, format=%d.",
  1294. vm->frame, vm->width, vm->height, vm->format);
  1295. }
  1296. /*
  1297. * Check if the requested size is supported. If the requestor
  1298. * requests too big a frame then we may be tricked into accessing
  1299. * outside of own preallocated frame buffer (in uvd->frame).
  1300. * This will cause oops or a security hole. Theoretically, we
  1301. * could only clamp the size down to acceptable bounds, but then
  1302. * we'd need to figure out how to insert our smaller buffer into
  1303. * larger caller's buffer... this is not an easy question. So we
  1304. * here just flatly reject too large requests, assuming that the
  1305. * caller will resubmit with smaller size. Callers should know
  1306. * what size we support (returned by VIDIOCGCAP). However vidcat,
  1307. * for one, does not care and allows to ask for any size.
  1308. */
  1309. if ((vm->width > VIDEOSIZE_X(uvd->canvas)) ||
  1310. (vm->height > VIDEOSIZE_Y(uvd->canvas))) {
  1311. if (uvd->debug > 0) {
  1312. info("VIDIOCMCAPTURE: Size=%dx%d too large; "
  1313. "allowed only up to %ldx%ld", vm->width, vm->height,
  1314. VIDEOSIZE_X(uvd->canvas), VIDEOSIZE_Y(uvd->canvas));
  1315. }
  1316. return -EINVAL;
  1317. }
  1318. /* Check if the palette is supported */
  1319. if (((1L << vm->format) & uvd->paletteBits) == 0) {
  1320. if (uvd->debug > 0) {
  1321. info("VIDIOCMCAPTURE: format=%d. not supported"
  1322. " (paletteBits=$%08lx)",
  1323. vm->format, uvd->paletteBits);
  1324. }
  1325. return -EINVAL;
  1326. }
  1327. if ((vm->frame < 0) || (vm->frame >= USBVIDEO_NUMFRAMES)) {
  1328. err("VIDIOCMCAPTURE: vm.frame=%d. !E [0-%d]", vm->frame, USBVIDEO_NUMFRAMES-1);
  1329. return -EINVAL;
  1330. }
  1331. if (uvd->frame[vm->frame].frameState == FrameState_Grabbing) {
  1332. /* Not an error - can happen */
  1333. }
  1334. uvd->frame[vm->frame].request = VIDEOSIZE(vm->width, vm->height);
  1335. uvd->frame[vm->frame].palette = vm->format;
  1336. /* Mark it as ready */
  1337. uvd->frame[vm->frame].frameState = FrameState_Ready;
  1338. return usbvideo_NewFrame(uvd, vm->frame);
  1339. }
  1340. case VIDIOCSYNC:
  1341. {
  1342. int *frameNum = arg;
  1343. int ret;
  1344. if (*frameNum < 0 || *frameNum >= USBVIDEO_NUMFRAMES)
  1345. return -EINVAL;
  1346. if (uvd->debug >= 1)
  1347. info("VIDIOCSYNC: syncing to frame %d.", *frameNum);
  1348. if (uvd->flags & FLAGS_NO_DECODING)
  1349. ret = usbvideo_GetFrame(uvd, *frameNum);
  1350. else if (VALID_CALLBACK(uvd, getFrame)) {
  1351. ret = GET_CALLBACK(uvd, getFrame)(uvd, *frameNum);
  1352. if ((ret < 0) && (uvd->debug >= 1)) {
  1353. err("VIDIOCSYNC: getFrame() returned %d.", ret);
  1354. }
  1355. } else {
  1356. err("VIDIOCSYNC: getFrame is not set");
  1357. ret = -EFAULT;
  1358. }
  1359. /*
  1360. * The frame is in FrameState_Done_Hold state. Release it
  1361. * right now because its data is already mapped into
  1362. * the user space and it's up to the application to
  1363. * make use of it until it asks for another frame.
  1364. */
  1365. uvd->frame[*frameNum].frameState = FrameState_Unused;
  1366. return ret;
  1367. }
  1368. case VIDIOCGFBUF:
  1369. {
  1370. struct video_buffer *vb = arg;
  1371. memset(vb, 0, sizeof(*vb));
  1372. return 0;
  1373. }
  1374. case VIDIOCKEY:
  1375. return 0;
  1376. case VIDIOCCAPTURE:
  1377. return -EINVAL;
  1378. case VIDIOCSFBUF:
  1379. case VIDIOCGTUNER:
  1380. case VIDIOCSTUNER:
  1381. case VIDIOCGFREQ:
  1382. case VIDIOCSFREQ:
  1383. case VIDIOCGAUDIO:
  1384. case VIDIOCSAUDIO:
  1385. return -EINVAL;
  1386. default:
  1387. return -ENOIOCTLCMD;
  1388. }
  1389. return 0;
  1390. }
  1391. static int usbvideo_v4l_ioctl(struct inode *inode, struct file *file,
  1392. unsigned int cmd, unsigned long arg)
  1393. {
  1394. return video_usercopy(inode, file, cmd, arg, usbvideo_v4l_do_ioctl);
  1395. }
  1396. /*
  1397. * usbvideo_v4l_read()
  1398. *
  1399. * This is mostly boring stuff. We simply ask for a frame and when it
  1400. * arrives copy all the video data from it into user space. There is
  1401. * no obvious need to override this method.
  1402. *
  1403. * History:
  1404. * 20-Oct-2000 Created.
  1405. * 01-Nov-2000 Added mutex (uvd->lock).
  1406. */
  1407. static ssize_t usbvideo_v4l_read(struct file *file, char *buf,
  1408. size_t count, loff_t *ppos)
  1409. {
  1410. struct uvd *uvd = file->private_data;
  1411. int noblock = file->f_flags & O_NONBLOCK;
  1412. int frmx = -1, i;
  1413. struct usbvideo_frame *frame;
  1414. if (!CAMERA_IS_OPERATIONAL(uvd) || (buf == NULL))
  1415. return -EFAULT;
  1416. if (uvd->debug >= 1)
  1417. info("%s: %Zd. bytes, noblock=%d.", __FUNCTION__, count, noblock);
  1418. down(&uvd->lock);
  1419. /* See if a frame is completed, then use it. */
  1420. for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
  1421. if ((uvd->frame[i].frameState == FrameState_Done) ||
  1422. (uvd->frame[i].frameState == FrameState_Done_Hold) ||
  1423. (uvd->frame[i].frameState == FrameState_Error)) {
  1424. frmx = i;
  1425. break;
  1426. }
  1427. }
  1428. /* FIXME: If we don't start a frame here then who ever does? */
  1429. if (noblock && (frmx == -1)) {
  1430. count = -EAGAIN;
  1431. goto read_done;
  1432. }
  1433. /*
  1434. * If no FrameState_Done, look for a FrameState_Grabbing state.
  1435. * See if a frame is in process (grabbing), then use it.
  1436. * We will need to wait until it becomes cooked, of course.
  1437. */
  1438. if (frmx == -1) {
  1439. for(i = 0; i < USBVIDEO_NUMFRAMES; i++) {
  1440. if (uvd->frame[i].frameState == FrameState_Grabbing) {
  1441. frmx = i;
  1442. break;
  1443. }
  1444. }
  1445. }
  1446. /*
  1447. * If no frame is active, start one. We don't care which one
  1448. * it will be, so #0 is as good as any.
  1449. * In read access mode we don't have convenience of VIDIOCMCAPTURE
  1450. * to specify the requested palette (video format) on per-frame
  1451. * basis. This means that we have to return data in -some- format
  1452. * and just hope that the client knows what to do with it.
  1453. * The default format is configured in uvd->defaultPalette field
  1454. * as one of VIDEO_PALETTE_xxx values. We stuff it into the new
  1455. * frame and initiate the frame filling process.
  1456. */
  1457. if (frmx == -1) {
  1458. if (uvd->defaultPalette == 0) {
  1459. err("%s: No default palette; don't know what to do!", __FUNCTION__);
  1460. count = -EFAULT;
  1461. goto read_done;
  1462. }
  1463. frmx = 0;
  1464. /*
  1465. * We have no per-frame control over video size.
  1466. * Therefore we only can use whatever size was
  1467. * specified as default.
  1468. */
  1469. uvd->frame[frmx].request = uvd->videosize;
  1470. uvd->frame[frmx].palette = uvd->defaultPalette;
  1471. uvd->frame[frmx].frameState = FrameState_Ready;
  1472. usbvideo_NewFrame(uvd, frmx);
  1473. /* Now frame 0 is supposed to start filling... */
  1474. }
  1475. /*
  1476. * Get a pointer to the active frame. It is either previously
  1477. * completed frame or frame in progress but not completed yet.
  1478. */
  1479. frame = &uvd->frame[frmx];
  1480. /*
  1481. * Sit back & wait until the frame gets filled and postprocessed.
  1482. * If we fail to get the picture [in time] then return the error.
  1483. * In this call we specify that we want the frame to be waited for,
  1484. * postprocessed and switched into FrameState_Done_Hold state. This
  1485. * state is used to hold the frame as "fully completed" between
  1486. * subsequent partial reads of the same frame.
  1487. */
  1488. if (frame->frameState != FrameState_Done_Hold) {
  1489. long rv = -EFAULT;
  1490. if (uvd->flags & FLAGS_NO_DECODING)
  1491. rv = usbvideo_GetFrame(uvd, frmx);
  1492. else if (VALID_CALLBACK(uvd, getFrame))
  1493. rv = GET_CALLBACK(uvd, getFrame)(uvd, frmx);
  1494. else
  1495. err("getFrame is not set");
  1496. if ((rv != 0) || (frame->frameState != FrameState_Done_Hold)) {
  1497. count = rv;
  1498. goto read_done;
  1499. }
  1500. }
  1501. /*
  1502. * Copy bytes to user space. We allow for partial reads, which
  1503. * means that the user application can request read less than
  1504. * the full frame size. It is up to the application to issue
  1505. * subsequent calls until entire frame is read.
  1506. *
  1507. * First things first, make sure we don't copy more than we
  1508. * have - even if the application wants more. That would be
  1509. * a big security embarassment!
  1510. */
  1511. if ((count + frame->seqRead_Index) > frame->seqRead_Length)
  1512. count = frame->seqRead_Length - frame->seqRead_Index;
  1513. /*
  1514. * Copy requested amount of data to user space. We start
  1515. * copying from the position where we last left it, which
  1516. * will be zero for a new frame (not read before).
  1517. */
  1518. if (copy_to_user(buf, frame->data + frame->seqRead_Index, count)) {
  1519. count = -EFAULT;
  1520. goto read_done;
  1521. }
  1522. /* Update last read position */
  1523. frame->seqRead_Index += count;
  1524. if (uvd->debug >= 1) {
  1525. err("%s: {copy} count used=%Zd, new seqRead_Index=%ld",
  1526. __FUNCTION__, count, frame->seqRead_Index);
  1527. }
  1528. /* Finally check if the frame is done with and "release" it */
  1529. if (frame->seqRead_Index >= frame->seqRead_Length) {
  1530. /* All data has been read */
  1531. frame->seqRead_Index = 0;
  1532. /* Mark it as available to be used again. */
  1533. uvd->frame[frmx].frameState = FrameState_Unused;
  1534. if (usbvideo_NewFrame(uvd, (frmx + 1) % USBVIDEO_NUMFRAMES)) {
  1535. err("%s: usbvideo_NewFrame failed.", __FUNCTION__);
  1536. }
  1537. }
  1538. read_done:
  1539. up(&uvd->lock);
  1540. return count;
  1541. }
  1542. /*
  1543. * Make all of the blocks of data contiguous
  1544. */
  1545. static int usbvideo_CompressIsochronous(struct uvd *uvd, struct urb *urb)
  1546. {
  1547. char *cdata;
  1548. int i, totlen = 0;
  1549. for (i = 0; i < urb->number_of_packets; i++) {
  1550. int n = urb->iso_frame_desc[i].actual_length;
  1551. int st = urb->iso_frame_desc[i].status;
  1552. cdata = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  1553. /* Detect and ignore errored packets */
  1554. if (st < 0) {
  1555. if (uvd->debug >= 1)
  1556. err("Data error: packet=%d. len=%d. status=%d.", i, n, st);
  1557. uvd->stats.iso_err_count++;
  1558. continue;
  1559. }
  1560. /* Detect and ignore empty packets */
  1561. if (n <= 0) {
  1562. uvd->stats.iso_skip_count++;
  1563. continue;
  1564. }
  1565. totlen += n; /* Little local accounting */
  1566. RingQueue_Enqueue(&uvd->dp, cdata, n);
  1567. }
  1568. return totlen;
  1569. }
  1570. static void usbvideo_IsocIrq(struct urb *urb, struct pt_regs *regs)
  1571. {
  1572. int i, ret, len;
  1573. struct uvd *uvd = urb->context;
  1574. /* We don't want to do anything if we are about to be removed! */
  1575. if (!CAMERA_IS_OPERATIONAL(uvd))
  1576. return;
  1577. #if 0
  1578. if (urb->actual_length > 0) {
  1579. info("urb=$%p status=%d. errcount=%d. length=%d.",
  1580. urb, urb->status, urb->error_count, urb->actual_length);
  1581. } else {
  1582. static int c = 0;
  1583. if (c++ % 100 == 0)
  1584. info("No Isoc data");
  1585. }
  1586. #endif
  1587. if (!uvd->streaming) {
  1588. if (uvd->debug >= 1)
  1589. info("Not streaming, but interrupt!");
  1590. return;
  1591. }
  1592. uvd->stats.urb_count++;
  1593. if (urb->actual_length <= 0)
  1594. goto urb_done_with;
  1595. /* Copy the data received into ring queue */
  1596. len = usbvideo_CompressIsochronous(uvd, urb);
  1597. uvd->stats.urb_length = len;
  1598. if (len <= 0)
  1599. goto urb_done_with;
  1600. /* Here we got some data */
  1601. uvd->stats.data_count += len;
  1602. RingQueue_WakeUpInterruptible(&uvd->dp);
  1603. urb_done_with:
  1604. for (i = 0; i < FRAMES_PER_DESC; i++) {
  1605. urb->iso_frame_desc[i].status = 0;
  1606. urb->iso_frame_desc[i].actual_length = 0;
  1607. }
  1608. urb->status = 0;
  1609. urb->dev = uvd->dev;
  1610. ret = usb_submit_urb (urb, GFP_KERNEL);
  1611. if(ret)
  1612. err("usb_submit_urb error (%d)", ret);
  1613. return;
  1614. }
  1615. /*
  1616. * usbvideo_StartDataPump()
  1617. *
  1618. * History:
  1619. * 27-Jan-2000 Used ibmcam->iface, ibmcam->ifaceAltActive instead
  1620. * of hardcoded values. Simplified by using for loop,
  1621. * allowed any number of URBs.
  1622. */
  1623. static int usbvideo_StartDataPump(struct uvd *uvd)
  1624. {
  1625. struct usb_device *dev = uvd->dev;
  1626. int i, errFlag;
  1627. if (uvd->debug > 1)
  1628. info("%s($%p)", __FUNCTION__, uvd);
  1629. if (!CAMERA_IS_OPERATIONAL(uvd)) {
  1630. err("%s: Camera is not operational", __FUNCTION__);
  1631. return -EFAULT;
  1632. }
  1633. uvd->curframe = -1;
  1634. /* Alternate interface 1 is is the biggest frame size */
  1635. i = usb_set_interface(dev, uvd->iface, uvd->ifaceAltActive);
  1636. if (i < 0) {
  1637. err("%s: usb_set_interface error", __FUNCTION__);
  1638. uvd->last_error = i;
  1639. return -EBUSY;
  1640. }
  1641. if (VALID_CALLBACK(uvd, videoStart))
  1642. GET_CALLBACK(uvd, videoStart)(uvd);
  1643. else
  1644. err("%s: videoStart not set", __FUNCTION__);
  1645. /* We double buffer the Iso lists */
  1646. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  1647. int j, k;
  1648. struct urb *urb = uvd->sbuf[i].urb;
  1649. urb->dev = dev;
  1650. urb->context = uvd;
  1651. urb->pipe = usb_rcvisocpipe(dev, uvd->video_endp);
  1652. urb->interval = 1;
  1653. urb->transfer_flags = URB_ISO_ASAP;
  1654. urb->transfer_buffer = uvd->sbuf[i].data;
  1655. urb->complete = usbvideo_IsocIrq;
  1656. urb->number_of_packets = FRAMES_PER_DESC;
  1657. urb->transfer_buffer_length = uvd->iso_packet_len * FRAMES_PER_DESC;
  1658. for (j=k=0; j < FRAMES_PER_DESC; j++, k += uvd->iso_packet_len) {
  1659. urb->iso_frame_desc[j].offset = k;
  1660. urb->iso_frame_desc[j].length = uvd->iso_packet_len;
  1661. }
  1662. }
  1663. /* Submit all URBs */
  1664. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  1665. errFlag = usb_submit_urb(uvd->sbuf[i].urb, GFP_KERNEL);
  1666. if (errFlag)
  1667. err("%s: usb_submit_isoc(%d) ret %d", __FUNCTION__, i, errFlag);
  1668. }
  1669. uvd->streaming = 1;
  1670. if (uvd->debug > 1)
  1671. info("%s: streaming=1 video_endp=$%02x", __FUNCTION__, uvd->video_endp);
  1672. return 0;
  1673. }
  1674. /*
  1675. * usbvideo_StopDataPump()
  1676. *
  1677. * This procedure stops streaming and deallocates URBs. Then it
  1678. * activates zero-bandwidth alt. setting of the video interface.
  1679. *
  1680. * History:
  1681. * 22-Jan-2000 Corrected order of actions to work after surprise removal.
  1682. * 27-Jan-2000 Used uvd->iface, uvd->ifaceAltInactive instead of hardcoded values.
  1683. */
  1684. static void usbvideo_StopDataPump(struct uvd *uvd)
  1685. {
  1686. int i, j;
  1687. if (uvd->debug > 1)
  1688. info("%s($%p)", __FUNCTION__, uvd);
  1689. if ((uvd == NULL) || (!uvd->streaming) || (uvd->dev == NULL))
  1690. return;
  1691. /* Unschedule all of the iso td's */
  1692. for (i=0; i < USBVIDEO_NUMSBUF; i++) {
  1693. j = usb_unlink_urb(uvd->sbuf[i].urb);
  1694. if (j < 0)
  1695. err("%s: usb_unlink_urb() error %d.", __FUNCTION__, j);
  1696. }
  1697. if (uvd->debug > 1)
  1698. info("%s: streaming=0", __FUNCTION__);
  1699. uvd->streaming