PageRenderTime 81ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/media/video/zoran_driver.c

https://bitbucket.org/abioy/linux
C | 4699 lines | 3647 code | 696 blank | 356 comment | 637 complexity | fd87df4f679a777ad9eb248f9464fc3f MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * Zoran zr36057/zr36067 PCI controller driver, for the
  3. * Pinnacle/Miro DC10/DC10+/DC30/DC30+, Iomega Buz, Linux
  4. * Media Labs LML33/LML33R10.
  5. *
  6. * Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  7. *
  8. * Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
  9. *
  10. * Changes for DC10/DC30 by Laurent Pinchart <laurent.pinchart@skynet.be>
  11. *
  12. * Changes for LML33R10 by Maxim Yevtyushkin <max@linuxmedialabs.com>
  13. *
  14. * Changes for videodev2/v4l2 by Ronald Bultje <rbultje@ronald.bitfreak.net>
  15. *
  16. * Based on
  17. *
  18. * Miro DC10 driver
  19. * Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  20. *
  21. * Iomega Buz driver version 1.0
  22. * Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  23. *
  24. * buz.0.0.3
  25. * Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  26. *
  27. * bttv - Bt848 frame grabber driver
  28. * Copyright (C) 1996,97,98 Ralph Metzler (rjkm@thp.uni-koeln.de)
  29. * & Marcus Metzler (mocm@thp.uni-koeln.de)
  30. *
  31. *
  32. * This program is free software; you can redistribute it and/or modify
  33. * it under the terms of the GNU General Public License as published by
  34. * the Free Software Foundation; either version 2 of the License, or
  35. * (at your option) any later version.
  36. *
  37. * This program is distributed in the hope that it will be useful,
  38. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. * GNU General Public License for more details.
  41. *
  42. * You should have received a copy of the GNU General Public License
  43. * along with this program; if not, write to the Free Software
  44. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  45. */
  46. #include <linux/config.h>
  47. #include <linux/version.h>
  48. #include <linux/init.h>
  49. #include <linux/module.h>
  50. #include <linux/delay.h>
  51. #include <linux/slab.h>
  52. #include <linux/pci.h>
  53. #include <linux/vmalloc.h>
  54. #include <linux/interrupt.h>
  55. #include <linux/i2c.h>
  56. #include <linux/i2c-algo-bit.h>
  57. #include <linux/spinlock.h>
  58. #define MAP_NR(x) virt_to_page(x)
  59. #define ZORAN_HARDWARE VID_HARDWARE_ZR36067
  60. #define ZORAN_VID_TYPE ( \
  61. VID_TYPE_CAPTURE | \
  62. VID_TYPE_OVERLAY | \
  63. VID_TYPE_CLIPPING | \
  64. VID_TYPE_FRAMERAM | \
  65. VID_TYPE_SCALES | \
  66. VID_TYPE_MJPEG_DECODER | \
  67. VID_TYPE_MJPEG_ENCODER \
  68. )
  69. #include <linux/videodev.h>
  70. #include "videocodec.h"
  71. #include <asm/io.h>
  72. #include <asm/uaccess.h>
  73. #include <linux/proc_fs.h>
  74. #include <linux/video_decoder.h>
  75. #include <linux/video_encoder.h>
  76. #include "zoran.h"
  77. #include "zoran_device.h"
  78. #include "zoran_card.h"
  79. #ifdef HAVE_V4L2
  80. /* we declare some card type definitions here, they mean
  81. * the same as the v4l1 ZORAN_VID_TYPE above, except it's v4l2 */
  82. #define ZORAN_V4L2_VID_FLAGS ( \
  83. V4L2_CAP_STREAMING |\
  84. V4L2_CAP_VIDEO_CAPTURE |\
  85. V4L2_CAP_VIDEO_OUTPUT |\
  86. V4L2_CAP_VIDEO_OVERLAY \
  87. )
  88. #endif
  89. #include <asm/byteorder.h>
  90. const struct zoran_format zoran_formats[] = {
  91. {
  92. .name = "15-bit RGB",
  93. .palette = VIDEO_PALETTE_RGB555,
  94. #ifdef HAVE_V4L2
  95. #ifdef __LITTLE_ENDIAN
  96. .fourcc = V4L2_PIX_FMT_RGB555,
  97. #else
  98. .fourcc = V4L2_PIX_FMT_RGB555X,
  99. #endif
  100. .colorspace = V4L2_COLORSPACE_SRGB,
  101. #endif
  102. .depth = 15,
  103. .flags = ZORAN_FORMAT_CAPTURE |
  104. ZORAN_FORMAT_OVERLAY,
  105. }, {
  106. .name = "16-bit RGB",
  107. .palette = VIDEO_PALETTE_RGB565,
  108. #ifdef HAVE_V4L2
  109. #ifdef __LITTLE_ENDIAN
  110. .fourcc = V4L2_PIX_FMT_RGB565,
  111. #else
  112. .fourcc = V4L2_PIX_FMT_RGB565X,
  113. #endif
  114. .colorspace = V4L2_COLORSPACE_SRGB,
  115. #endif
  116. .depth = 16,
  117. .flags = ZORAN_FORMAT_CAPTURE |
  118. ZORAN_FORMAT_OVERLAY,
  119. }, {
  120. .name = "24-bit RGB",
  121. .palette = VIDEO_PALETTE_RGB24,
  122. #ifdef HAVE_V4L2
  123. #ifdef __LITTLE_ENDIAN
  124. .fourcc = V4L2_PIX_FMT_BGR24,
  125. #else
  126. .fourcc = V4L2_PIX_FMT_RGB24,
  127. #endif
  128. .colorspace = V4L2_COLORSPACE_SRGB,
  129. #endif
  130. .depth = 24,
  131. .flags = ZORAN_FORMAT_CAPTURE |
  132. ZORAN_FORMAT_OVERLAY,
  133. }, {
  134. .name = "32-bit RGB",
  135. .palette = VIDEO_PALETTE_RGB32,
  136. #ifdef HAVE_V4L2
  137. #ifdef __LITTLE_ENDIAN
  138. .fourcc = V4L2_PIX_FMT_BGR32,
  139. #else
  140. .fourcc = V4L2_PIX_FMT_RGB32,
  141. #endif
  142. .colorspace = V4L2_COLORSPACE_SRGB,
  143. #endif
  144. .depth = 32,
  145. .flags = ZORAN_FORMAT_CAPTURE |
  146. ZORAN_FORMAT_OVERLAY,
  147. }, {
  148. .name = "4:2:2, packed, YUYV",
  149. .palette = VIDEO_PALETTE_YUV422,
  150. #ifdef HAVE_V4L2
  151. .fourcc = V4L2_PIX_FMT_YUYV,
  152. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  153. #endif
  154. .depth = 16,
  155. .flags = ZORAN_FORMAT_CAPTURE |
  156. ZORAN_FORMAT_OVERLAY,
  157. }, {
  158. .name = "Hardware-encoded Motion-JPEG",
  159. .palette = -1,
  160. #ifdef HAVE_V4L2
  161. .fourcc = V4L2_PIX_FMT_MJPEG,
  162. .colorspace = V4L2_COLORSPACE_SMPTE170M,
  163. #endif
  164. .depth = 0,
  165. .flags = ZORAN_FORMAT_CAPTURE |
  166. ZORAN_FORMAT_PLAYBACK |
  167. ZORAN_FORMAT_COMPRESSED,
  168. }
  169. };
  170. const int zoran_num_formats =
  171. (sizeof(zoran_formats) / sizeof(struct zoran_format));
  172. // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
  173. #if !defined(CONFIG_BIGPHYS_AREA)
  174. //#undef CONFIG_BIGPHYS_AREA
  175. #define BUZ_USE_HIMEM
  176. #endif
  177. #if defined(CONFIG_BIGPHYS_AREA)
  178. # include <linux/bigphysarea.h>
  179. #endif
  180. extern int *zr_debug;
  181. #define dprintk(num, format, args...) \
  182. do { \
  183. if (*zr_debug >= num) \
  184. printk(format, ##args); \
  185. } while (0)
  186. extern int v4l_nbufs;
  187. extern int v4l_bufsize;
  188. extern int jpg_nbufs;
  189. extern int jpg_bufsize;
  190. extern int pass_through;
  191. static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
  192. MODULE_PARM(lock_norm, "i");
  193. MODULE_PARM_DESC(lock_norm, "Users can't change norm");
  194. #ifdef HAVE_V4L2
  195. /* small helper function for calculating buffersizes for v4l2
  196. * we calculate the nearest higher power-of-two, which
  197. * will be the recommended buffersize */
  198. static __u32
  199. zoran_v4l2_calc_bufsize (struct zoran_jpg_settings *settings)
  200. {
  201. __u8 div = settings->VerDcm * settings->HorDcm * settings->TmpDcm;
  202. __u32 num = (1024 * 512) / (div);
  203. __u32 result = 2;
  204. num--;
  205. while (num) {
  206. num >>= 1;
  207. result <<= 1;
  208. }
  209. if (result > jpg_bufsize)
  210. return jpg_bufsize;
  211. if (result < 8192)
  212. return 8192;
  213. return result;
  214. }
  215. #endif
  216. /* forward references */
  217. static void v4l_fbuffer_free(struct file *file);
  218. static void jpg_fbuffer_free(struct file *file);
  219. /*
  220. * Allocate the V4L grab buffers
  221. *
  222. * These have to be pysically contiguous.
  223. * If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
  224. * else we try to allocate them with bigphysarea_alloc_pages
  225. * if the bigphysarea patch is present in the kernel,
  226. * else we try to use high memory (if the user has bootet
  227. * Linux with the necessary memory left over).
  228. */
  229. #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
  230. static unsigned long
  231. get_high_mem (unsigned long size)
  232. {
  233. /*
  234. * Check if there is usable memory at the end of Linux memory
  235. * of at least size. Return the physical address of this memory,
  236. * return 0 on failure.
  237. *
  238. * The idea is from Alexandro Rubini's book "Linux device drivers".
  239. * The driver from him which is downloadable from O'Reilly's
  240. * web site misses the "virt_to_phys(high_memory)" part
  241. * (and therefore doesn't work at all - at least with 2.2.x kernels).
  242. *
  243. * It should be unnecessary to mention that THIS IS DANGEROUS,
  244. * if more than one driver at a time has the idea to use this memory!!!!
  245. */
  246. volatile unsigned char *mem;
  247. unsigned char c;
  248. unsigned long hi_mem_ph;
  249. unsigned long i;
  250. /* Map the high memory to user space */
  251. hi_mem_ph = virt_to_phys(high_memory);
  252. mem = ioremap(hi_mem_ph, size);
  253. if (!mem) {
  254. dprintk(1,
  255. KERN_ERR "%s: get_high_mem() - ioremap failed\n",
  256. ZORAN_NAME);
  257. return 0;
  258. }
  259. for (i = 0; i < size; i++) {
  260. /* Check if it is memory */
  261. c = i & 0xff;
  262. mem[i] = c;
  263. if (mem[i] != c)
  264. break;
  265. c = 255 - c;
  266. mem[i] = c;
  267. if (mem[i] != c)
  268. break;
  269. mem[i] = 0; /* zero out memory */
  270. /* give the kernel air to breath */
  271. if ((i & 0x3ffff) == 0x3ffff)
  272. schedule();
  273. }
  274. iounmap((void *) mem);
  275. if (i != size) {
  276. dprintk(1,
  277. KERN_ERR
  278. "%s: get_high_mem() - requested %lu, avail %lu\n",
  279. ZORAN_NAME, size, i);
  280. return 0;
  281. }
  282. return hi_mem_ph;
  283. }
  284. #endif
  285. static int
  286. v4l_fbuffer_alloc (struct file *file)
  287. {
  288. struct zoran_fh *fh = file->private_data;
  289. struct zoran *zr = fh->zr;
  290. int i, off;
  291. unsigned char *mem;
  292. #if defined(BUZ_USE_HIMEM) && !defined(CONFIG_BIGPHYS_AREA)
  293. unsigned long pmem = 0;
  294. #endif
  295. /* we might have old buffers lying around... */
  296. if (fh->v4l_buffers.ready_to_be_freed) {
  297. v4l_fbuffer_free(file);
  298. }
  299. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  300. if (fh->v4l_buffers.buffer[i].fbuffer)
  301. dprintk(2,
  302. KERN_WARNING
  303. "%s: v4l_fbuffer_alloc() - buffer %d allready allocated!?\n",
  304. ZR_DEVNAME(zr), i);
  305. //udelay(20);
  306. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  307. /* Use kmalloc */
  308. mem =
  309. (unsigned char *) kmalloc(fh->v4l_buffers.
  310. buffer_size,
  311. GFP_KERNEL);
  312. if (mem == 0) {
  313. dprintk(1,
  314. KERN_ERR
  315. "%s: v4l_fbuffer_alloc() - kmalloc for V4L buf %d failed\n",
  316. ZR_DEVNAME(zr), i);
  317. v4l_fbuffer_free(file);
  318. return -ENOBUFS;
  319. }
  320. fh->v4l_buffers.buffer[i].fbuffer = mem;
  321. fh->v4l_buffers.buffer[i].fbuffer_phys =
  322. virt_to_phys(mem);
  323. fh->v4l_buffers.buffer[i].fbuffer_bus =
  324. virt_to_bus(mem);
  325. for (off = 0; off < fh->v4l_buffers.buffer_size;
  326. off += PAGE_SIZE)
  327. SetPageReserved(MAP_NR(mem + off));
  328. dprintk(4,
  329. KERN_INFO
  330. "%s: v4l_fbuffer_alloc() - V4L frame %d mem 0x%lx (bus: 0x%lx)\n",
  331. ZR_DEVNAME(zr), i, (unsigned long) mem,
  332. virt_to_bus(mem));
  333. } else {
  334. #if defined(CONFIG_BIGPHYS_AREA)
  335. /* Use bigphysarea_alloc_pages */
  336. int n =
  337. (fh->v4l_buffers.buffer_size + PAGE_SIZE -
  338. 1) / PAGE_SIZE;
  339. mem =
  340. (unsigned char *) bigphysarea_alloc_pages(n, 0,
  341. GFP_KERNEL);
  342. if (mem == 0) {
  343. dprintk(1,
  344. KERN_ERR
  345. "%s: v4l_fbuffer_alloc() - bigphysarea_alloc_pages for V4L buf %d failed\n",
  346. ZR_DEVNAME(zr), i);
  347. v4l_fbuffer_free(file);
  348. return -ENOBUFS;
  349. }
  350. fh->v4l_buffers.buffer[i].fbuffer = mem;
  351. fh->v4l_buffers.buffer[i].fbuffer_phys =
  352. virt_to_phys(mem);
  353. fh->v4l_buffers.buffer[i].fbuffer_bus =
  354. virt_to_bus(mem);
  355. dprintk(4,
  356. KERN_INFO
  357. "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)\n",
  358. ZR_DEVNAME(zr), i, (unsigned) mem,
  359. (unsigned) virt_to_bus(mem));
  360. /* Zero out the allocated memory */
  361. memset(fh->v4l_buffers.buffer[i].fbuffer, 0,
  362. fh->v4l_buffers.buffer_size);
  363. #elif defined(BUZ_USE_HIMEM)
  364. /* Use high memory which has been left at boot time */
  365. /* Ok., Ok. this is an evil hack - we make
  366. * the assumption that physical addresses are
  367. * the same as bus addresses (true at least
  368. * for Intel processors). The whole method of
  369. * obtaining and using this memory is not very
  370. * nice - but I hope it saves some poor users
  371. * from kernel hacking, which might have even
  372. * more evil results */
  373. if (i == 0) {
  374. int size =
  375. fh->v4l_buffers.num_buffers *
  376. fh->v4l_buffers.buffer_size;
  377. pmem = get_high_mem(size);
  378. if (pmem == 0) {
  379. dprintk(1,
  380. KERN_ERR
  381. "%s: v4l_fbuffer_alloc() - get_high_mem (size = %d KB) for V4L bufs failed\n",
  382. ZR_DEVNAME(zr), size >> 10);
  383. return -ENOBUFS;
  384. }
  385. fh->v4l_buffers.buffer[0].fbuffer = 0;
  386. fh->v4l_buffers.buffer[0].fbuffer_phys =
  387. pmem;
  388. fh->v4l_buffers.buffer[0].fbuffer_bus =
  389. pmem;
  390. dprintk(4,
  391. KERN_INFO
  392. "%s: v4l_fbuffer_alloc() - using %d KB high memory\n",
  393. ZR_DEVNAME(zr), size >> 10);
  394. } else {
  395. fh->v4l_buffers.buffer[i].fbuffer = 0;
  396. fh->v4l_buffers.buffer[i].fbuffer_phys =
  397. pmem + i * fh->v4l_buffers.buffer_size;
  398. fh->v4l_buffers.buffer[i].fbuffer_bus =
  399. pmem + i * fh->v4l_buffers.buffer_size;
  400. }
  401. #else
  402. /* No bigphysarea present, usage of high memory disabled,
  403. * but user wants buffers of more than MAX_KMALLOC_MEM */
  404. dprintk(1,
  405. KERN_ERR
  406. "%s: v4l_fbuffer_alloc() - no bigphysarea_patch present, usage of high memory disabled,\n",
  407. ZR_DEVNAME(zr));
  408. dprintk(1,
  409. KERN_ERR
  410. "%s: v4l_fbuffer_alloc() - sorry, could not allocate %d V4L buffers of size %d KB.\n",
  411. ZR_DEVNAME(zr), fh->v4l_buffers.num_buffers,
  412. fh->v4l_buffers.buffer_size >> 10);
  413. return -ENOBUFS;
  414. #endif
  415. }
  416. }
  417. fh->v4l_buffers.allocated = 1;
  418. return 0;
  419. }
  420. /* free the V4L grab buffers */
  421. static void
  422. v4l_fbuffer_free (struct file *file)
  423. {
  424. struct zoran_fh *fh = file->private_data;
  425. struct zoran *zr = fh->zr;
  426. int i, off;
  427. unsigned char *mem;
  428. dprintk(4, KERN_INFO "%s: v4l_fbuffer_free()\n", ZR_DEVNAME(zr));
  429. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  430. if (!fh->v4l_buffers.buffer[i].fbuffer)
  431. continue;
  432. if (fh->v4l_buffers.buffer_size <= MAX_KMALLOC_MEM) {
  433. mem = fh->v4l_buffers.buffer[i].fbuffer;
  434. for (off = 0; off < fh->v4l_buffers.buffer_size;
  435. off += PAGE_SIZE)
  436. ClearPageReserved(MAP_NR(mem + off));
  437. kfree((void *) fh->v4l_buffers.buffer[i].fbuffer);
  438. }
  439. #if defined(CONFIG_BIGPHYS_AREA)
  440. else
  441. bigphysarea_free_pages((void *) fh->v4l_buffers.
  442. buffer[i].fbuffer);
  443. #endif
  444. fh->v4l_buffers.buffer[i].fbuffer = NULL;
  445. }
  446. fh->v4l_buffers.allocated = 0;
  447. fh->v4l_buffers.ready_to_be_freed = 0;
  448. }
  449. /*
  450. * Allocate the MJPEG grab buffers.
  451. *
  452. * If the requested buffer size is smaller than MAX_KMALLOC_MEM,
  453. * kmalloc is used to request a physically contiguous area,
  454. * else we allocate the memory in framgents with get_zeroed_page.
  455. *
  456. * If a Natoma chipset is present and this is a revision 1 zr36057,
  457. * each MJPEG buffer needs to be physically contiguous.
  458. * (RJ: This statement is from Dave Perks' original driver,
  459. * I could never check it because I have a zr36067)
  460. * The driver cares about this because it reduces the buffer
  461. * size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
  462. *
  463. * RJ: The contents grab buffers needs never be accessed in the driver.
  464. * Therefore there is no need to allocate them with vmalloc in order
  465. * to get a contiguous virtual memory space.
  466. * I don't understand why many other drivers first allocate them with
  467. * vmalloc (which uses internally also get_zeroed_page, but delivers you
  468. * virtual addresses) and then again have to make a lot of efforts
  469. * to get the physical address.
  470. *
  471. */
  472. static int
  473. jpg_fbuffer_alloc (struct file *file)
  474. {
  475. struct zoran_fh *fh = file->private_data;
  476. struct zoran *zr = fh->zr;
  477. int i, j, off;
  478. unsigned long mem;
  479. /* we might have old buffers lying around */
  480. if (fh->jpg_buffers.ready_to_be_freed) {
  481. jpg_fbuffer_free(file);
  482. }
  483. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  484. if (fh->jpg_buffers.buffer[i].frag_tab)
  485. dprintk(2,
  486. KERN_WARNING
  487. "%s: jpg_fbuffer_alloc() - buffer %d allready allocated!?\n",
  488. ZR_DEVNAME(zr), i);
  489. /* Allocate fragment table for this buffer */
  490. mem = get_zeroed_page(GFP_KERNEL);
  491. if (mem == 0) {
  492. dprintk(1,
  493. KERN_ERR
  494. "%s: jpg_fbuffer_alloc() - get_zeroed_page (frag_tab) failed for buffer %d\n",
  495. ZR_DEVNAME(zr), i);
  496. jpg_fbuffer_free(file);
  497. return -ENOBUFS;
  498. }
  499. memset((void *) mem, 0, PAGE_SIZE);
  500. fh->jpg_buffers.buffer[i].frag_tab = (u32 *) mem;
  501. fh->jpg_buffers.buffer[i].frag_tab_bus =
  502. virt_to_bus((void *) mem);
  503. //if (alloc_contig) {
  504. if (fh->jpg_buffers.need_contiguous) {
  505. mem =
  506. (unsigned long) kmalloc(fh->jpg_buffers.
  507. buffer_size,
  508. GFP_KERNEL);
  509. if (mem == 0) {
  510. dprintk(1,
  511. KERN_ERR
  512. "%s: jpg_fbuffer_alloc() - kmalloc failed for buffer %d\n",
  513. ZR_DEVNAME(zr), i);
  514. jpg_fbuffer_free(file);
  515. return -ENOBUFS;
  516. }
  517. fh->jpg_buffers.buffer[i].frag_tab[0] =
  518. virt_to_bus((void *) mem);
  519. fh->jpg_buffers.buffer[i].frag_tab[1] =
  520. ((fh->jpg_buffers.buffer_size / 4) << 1) | 1;
  521. for (off = 0; off < fh->jpg_buffers.buffer_size;
  522. off += PAGE_SIZE)
  523. SetPageReserved(MAP_NR(mem + off));
  524. } else {
  525. /* jpg_bufsize is allreay page aligned */
  526. for (j = 0;
  527. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  528. j++) {
  529. mem = get_zeroed_page(GFP_KERNEL);
  530. if (mem == 0) {
  531. dprintk(1,
  532. KERN_ERR
  533. "%s: jpg_fbuffer_alloc() - get_zeroed_page failed for buffer %d\n",
  534. ZR_DEVNAME(zr), i);
  535. jpg_fbuffer_free(file);
  536. return -ENOBUFS;
  537. }
  538. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  539. virt_to_bus((void *) mem);
  540. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  541. 1] =
  542. (PAGE_SIZE / 4) << 1;
  543. SetPageReserved(MAP_NR(mem));
  544. }
  545. fh->jpg_buffers.buffer[i].frag_tab[2 * j - 1] |= 1;
  546. }
  547. }
  548. dprintk(4,
  549. KERN_DEBUG "%s: jpg_fbuffer_alloc() - %d KB allocated\n",
  550. ZR_DEVNAME(zr),
  551. (fh->jpg_buffers.num_buffers *
  552. fh->jpg_buffers.buffer_size) >> 10);
  553. fh->jpg_buffers.allocated = 1;
  554. return 0;
  555. }
  556. /* free the MJPEG grab buffers */
  557. static void
  558. jpg_fbuffer_free (struct file *file)
  559. {
  560. struct zoran_fh *fh = file->private_data;
  561. struct zoran *zr = fh->zr;
  562. int i, j, off;
  563. unsigned char *mem;
  564. dprintk(4, KERN_DEBUG "%s: jpg_fbuffer_free()\n", ZR_DEVNAME(zr));
  565. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  566. if (!fh->jpg_buffers.buffer[i].frag_tab)
  567. continue;
  568. //if (alloc_contig) {
  569. if (fh->jpg_buffers.need_contiguous) {
  570. if (fh->jpg_buffers.buffer[i].frag_tab[0]) {
  571. mem =
  572. (unsigned char *) bus_to_virt(fh->
  573. jpg_buffers.
  574. buffer
  575. [i].
  576. frag_tab
  577. [0]);
  578. for (off = 0;
  579. off < fh->jpg_buffers.buffer_size;
  580. off += PAGE_SIZE)
  581. ClearPageReserved(MAP_NR
  582. (mem + off));
  583. kfree((void *) mem);
  584. fh->jpg_buffers.buffer[i].frag_tab[0] = 0;
  585. fh->jpg_buffers.buffer[i].frag_tab[1] = 0;
  586. }
  587. } else {
  588. for (j = 0;
  589. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  590. j++) {
  591. if (!fh->jpg_buffers.buffer[i].
  592. frag_tab[2 * j])
  593. break;
  594. ClearPageReserved(MAP_NR
  595. (bus_to_virt
  596. (fh->jpg_buffers.
  597. buffer[i].frag_tab[2 *
  598. j])));
  599. free_page((unsigned long)
  600. bus_to_virt(fh->jpg_buffers.
  601. buffer[i].
  602. frag_tab[2 * j]));
  603. fh->jpg_buffers.buffer[i].frag_tab[2 * j] =
  604. 0;
  605. fh->jpg_buffers.buffer[i].frag_tab[2 * j +
  606. 1] = 0;
  607. }
  608. }
  609. free_page((unsigned long) fh->jpg_buffers.buffer[i].
  610. frag_tab);
  611. fh->jpg_buffers.buffer[i].frag_tab = NULL;
  612. }
  613. fh->jpg_buffers.allocated = 0;
  614. fh->jpg_buffers.ready_to_be_freed = 0;
  615. }
  616. /*
  617. * V4L Buffer grabbing
  618. */
  619. static int
  620. zoran_v4l_set_format (struct file *file,
  621. int width,
  622. int height,
  623. const struct zoran_format *format)
  624. {
  625. struct zoran_fh *fh = file->private_data;
  626. struct zoran *zr = fh->zr;
  627. int bpp;
  628. /* Check size and format of the grab wanted */
  629. if (height < BUZ_MIN_HEIGHT || width < BUZ_MIN_WIDTH ||
  630. height > BUZ_MAX_HEIGHT || width > BUZ_MAX_WIDTH) {
  631. dprintk(1,
  632. KERN_ERR
  633. "%s: v4l_set_format() - wrong frame size (%dx%d)\n",
  634. ZR_DEVNAME(zr), width, height);
  635. return -EINVAL;
  636. }
  637. bpp = (format->depth + 7) / 8;
  638. /* Check against available buffer size */
  639. if (height * width * bpp > fh->v4l_buffers.buffer_size) {
  640. dprintk(1,
  641. KERN_ERR
  642. "%s: v4l_set_format() - video buffer size (%d kB) is too small\n",
  643. ZR_DEVNAME(zr), fh->v4l_buffers.buffer_size >> 10);
  644. return -EINVAL;
  645. }
  646. /* The video front end needs 4-byte alinged line sizes */
  647. if ((bpp == 2 && (width & 1)) || (bpp == 3 && (width & 3))) {
  648. dprintk(1,
  649. KERN_ERR
  650. "%s: v4l_set_format() - wrong frame alingment\n",
  651. ZR_DEVNAME(zr));
  652. return -EINVAL;
  653. }
  654. fh->v4l_settings.width = width;
  655. fh->v4l_settings.height = height;
  656. fh->v4l_settings.format = format;
  657. fh->v4l_settings.bytesperline = bpp * fh->v4l_settings.width;
  658. return 0;
  659. }
  660. static int
  661. zoran_v4l_queue_frame (struct file *file,
  662. int num)
  663. {
  664. struct zoran_fh *fh = file->private_data;
  665. struct zoran *zr = fh->zr;
  666. unsigned long flags;
  667. int res = 0;
  668. if (!fh->v4l_buffers.allocated) {
  669. dprintk(1,
  670. KERN_ERR
  671. "%s: v4l_queue_frame() - buffers not yet allocated\n",
  672. ZR_DEVNAME(zr));
  673. res = -ENOMEM;
  674. }
  675. /* No grabbing outside the buffer range! */
  676. if (num >= fh->v4l_buffers.num_buffers || num < 0) {
  677. dprintk(1,
  678. KERN_ERR
  679. "%s: v4l_queue_frame() - buffer %d is out of range\n",
  680. ZR_DEVNAME(zr), num);
  681. res = -EINVAL;
  682. }
  683. spin_lock_irqsave(&zr->spinlock, flags);
  684. if (fh->v4l_buffers.active == ZORAN_FREE) {
  685. if (zr->v4l_buffers.active == ZORAN_FREE) {
  686. zr->v4l_buffers = fh->v4l_buffers;
  687. fh->v4l_buffers.active = ZORAN_ACTIVE;
  688. } else {
  689. dprintk(1,
  690. KERN_ERR
  691. "%s: v4l_queue_frame() - another session is already capturing\n",
  692. ZR_DEVNAME(zr));
  693. res = -EBUSY;
  694. }
  695. }
  696. /* make sure a grab isn't going on currently with this buffer */
  697. if (!res) {
  698. switch (zr->v4l_buffers.buffer[num].state) {
  699. default:
  700. case BUZ_STATE_PEND:
  701. if (zr->v4l_buffers.active == ZORAN_FREE) {
  702. fh->v4l_buffers.active = ZORAN_FREE;
  703. zr->v4l_buffers.allocated = 0;
  704. }
  705. res = -EBUSY; /* what are you doing? */
  706. break;
  707. case BUZ_STATE_DONE:
  708. dprintk(2,
  709. KERN_WARNING
  710. "%s: v4l_queue_frame() - queueing buffer %d in state DONE!?\n",
  711. ZR_DEVNAME(zr), num);
  712. case BUZ_STATE_USER:
  713. /* since there is at least one unused buffer there's room for at least
  714. * one more pend[] entry */
  715. zr->v4l_pend[zr->v4l_pend_head++ &
  716. V4L_MASK_FRAME] = num;
  717. zr->v4l_buffers.buffer[num].state = BUZ_STATE_PEND;
  718. zr->v4l_buffers.buffer[num].bs.length =
  719. fh->v4l_settings.bytesperline *
  720. zr->v4l_settings.height;
  721. fh->v4l_buffers.buffer[num] =
  722. zr->v4l_buffers.buffer[num];
  723. break;
  724. }
  725. }
  726. spin_unlock_irqrestore(&zr->spinlock, flags);
  727. if (!res && zr->v4l_buffers.active == ZORAN_FREE)
  728. zr->v4l_buffers.active = fh->v4l_buffers.active;
  729. return res;
  730. }
  731. static int
  732. v4l_grab (struct file *file,
  733. struct video_mmap *mp)
  734. {
  735. struct zoran_fh *fh = file->private_data;
  736. struct zoran *zr = fh->zr;
  737. int res = 0, i;
  738. for (i = 0; i < zoran_num_formats; i++) {
  739. if (zoran_formats[i].palette == mp->format &&
  740. zoran_formats[i].flags & ZORAN_FORMAT_CAPTURE &&
  741. !(zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED))
  742. break;
  743. }
  744. if (i == zoran_num_formats || zoran_formats[i].depth == 0) {
  745. dprintk(1,
  746. KERN_ERR
  747. "%s: v4l_grab() - wrong bytes-per-pixel format\n",
  748. ZR_DEVNAME(zr));
  749. return -EINVAL;
  750. }
  751. /*
  752. * To minimize the time spent in the IRQ routine, we avoid setting up
  753. * the video front end there.
  754. * If this grab has different parameters from a running streaming capture
  755. * we stop the streaming capture and start it over again.
  756. */
  757. if (zr->v4l_memgrab_active &&
  758. (zr->v4l_settings.width != mp->width ||
  759. zr->v4l_settings.height != mp->height ||
  760. zr->v4l_settings.format->palette != mp->format)) {
  761. res = wait_grab_pending(zr);
  762. if (res)
  763. return res;
  764. }
  765. if ((res = zoran_v4l_set_format(file,
  766. mp->width,
  767. mp->height,
  768. &zoran_formats[i])))
  769. return res;
  770. zr->v4l_settings = fh->v4l_settings;
  771. /* queue the frame in the pending queue */
  772. if ((res = zoran_v4l_queue_frame(file, mp->frame))) {
  773. fh->v4l_buffers.active = ZORAN_FREE;
  774. return res;
  775. }
  776. /* put the 36057 into frame grabbing mode */
  777. if (!res && !zr->v4l_memgrab_active)
  778. zr36057_set_memgrab(zr, 1);
  779. //dprintk(4, KERN_INFO "%s: Frame grab 3...\n", ZR_DEVNAME(zr));
  780. return res;
  781. }
  782. /*
  783. * Sync on a V4L buffer
  784. */
  785. static int
  786. v4l_sync (struct file *file,
  787. int frame)
  788. {
  789. struct zoran_fh *fh = file->private_data;
  790. struct zoran *zr = fh->zr;
  791. unsigned long flags;
  792. if (fh->v4l_buffers.active == ZORAN_FREE) {
  793. dprintk(1,
  794. KERN_ERR
  795. "%s: v4l_sync() - no grab active for this session\n",
  796. ZR_DEVNAME(zr));
  797. return -EINVAL;
  798. }
  799. /* check passed-in frame number */
  800. if (frame >= fh->v4l_buffers.num_buffers || frame < 0) {
  801. dprintk(1,
  802. KERN_ERR "%s: v4l_sync() - frame %d is invalid\n",
  803. ZR_DEVNAME(zr), frame);
  804. return -EINVAL;
  805. }
  806. /* Check if is buffer was queued at all */
  807. if (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_USER) {
  808. dprintk(1,
  809. KERN_ERR
  810. "%s: v4l_sync() - attempt to sync on a buffer which was not queued?\n",
  811. ZR_DEVNAME(zr));
  812. return -EPROTO;
  813. }
  814. /* wait on this buffer to get ready */
  815. while (zr->v4l_buffers.buffer[frame].state == BUZ_STATE_PEND) {
  816. if (!interruptible_sleep_on_timeout(&zr->v4l_capq, 10 * HZ))
  817. return -ETIME;
  818. else if (signal_pending(current))
  819. return -ERESTARTSYS;
  820. }
  821. /* buffer should now be in BUZ_STATE_DONE */
  822. if (zr->v4l_buffers.buffer[frame].state != BUZ_STATE_DONE)
  823. dprintk(2,
  824. KERN_ERR "%s: v4l_sync() - internal state error\n",
  825. ZR_DEVNAME(zr));
  826. zr->v4l_buffers.buffer[frame].state = BUZ_STATE_USER;
  827. fh->v4l_buffers.buffer[frame] = zr->v4l_buffers.buffer[frame];
  828. spin_lock_irqsave(&zr->spinlock, flags);
  829. /* Check if streaming capture has finished */
  830. if (zr->v4l_pend_tail == zr->v4l_pend_head) {
  831. zr36057_set_memgrab(zr, 0);
  832. if (zr->v4l_buffers.active == ZORAN_ACTIVE) {
  833. fh->v4l_buffers.active = zr->v4l_buffers.active =
  834. ZORAN_FREE;
  835. zr->v4l_buffers.allocated = 0;
  836. }
  837. }
  838. spin_unlock_irqrestore(&zr->spinlock, flags);
  839. return 0;
  840. }
  841. /*
  842. * Queue a MJPEG buffer for capture/playback
  843. */
  844. static int
  845. zoran_jpg_queue_frame (struct file *file,
  846. int num,
  847. enum zoran_codec_mode mode)
  848. {
  849. struct zoran_fh *fh = file->private_data;
  850. struct zoran *zr = fh->zr;
  851. unsigned long flags;
  852. int res = 0;
  853. /* Check if buffers are allocated */
  854. if (!fh->jpg_buffers.allocated) {
  855. dprintk(1,
  856. KERN_ERR
  857. "%s: jpg_queue_frame() - buffers not yet allocated\n",
  858. ZR_DEVNAME(zr));
  859. return -ENOMEM;
  860. }
  861. /* No grabbing outside the buffer range! */
  862. if (num >= fh->jpg_buffers.num_buffers || num < 0) {
  863. dprintk(1,
  864. KERN_ERR
  865. "%s: jpg_queue_frame() - buffer %d out of range\n",
  866. ZR_DEVNAME(zr), num);
  867. return -EINVAL;
  868. }
  869. /* what is the codec mode right now? */
  870. if (zr->codec_mode == BUZ_MODE_IDLE) {
  871. zr->jpg_settings = fh->jpg_settings;
  872. } else if (zr->codec_mode != mode) {
  873. /* wrong codec mode active - invalid */
  874. dprintk(1,
  875. KERN_ERR
  876. "%s: jpg_queue_frame() - codec in wrong mode\n",
  877. ZR_DEVNAME(zr));
  878. return -EINVAL;
  879. }
  880. spin_lock_irqsave(&zr->spinlock, flags);
  881. if (fh->jpg_buffers.active == ZORAN_FREE) {
  882. if (zr->jpg_buffers.active == ZORAN_FREE) {
  883. zr->jpg_buffers = fh->jpg_buffers;
  884. fh->jpg_buffers.active = ZORAN_ACTIVE;
  885. } else {
  886. dprintk(1,
  887. KERN_ERR
  888. "%s: jpg_queue_frame() - another session is already capturing\n",
  889. ZR_DEVNAME(zr));
  890. res = -EBUSY;
  891. }
  892. }
  893. if (!res && zr->codec_mode == BUZ_MODE_IDLE) {
  894. /* Ok load up the jpeg codec */
  895. zr36057_enable_jpg(zr, mode);
  896. }
  897. if (!res) {
  898. switch (zr->jpg_buffers.buffer[num].state) {
  899. case BUZ_STATE_DONE:
  900. dprintk(2,
  901. KERN_WARNING
  902. "%s: jpg_queue_frame() - queing frame in BUZ_STATE_DONE state!?\n",
  903. ZR_DEVNAME(zr));
  904. case BUZ_STATE_USER:
  905. /* since there is at least one unused buffer there's room for at
  906. *least one more pend[] entry */
  907. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] =
  908. num;
  909. zr->jpg_buffers.buffer[num].state = BUZ_STATE_PEND;
  910. fh->jpg_buffers.buffer[num] =
  911. zr->jpg_buffers.buffer[num];
  912. zoran_feed_stat_com(zr);
  913. break;
  914. default:
  915. case BUZ_STATE_DMA:
  916. case BUZ_STATE_PEND:
  917. if (zr->jpg_buffers.active == ZORAN_FREE) {
  918. fh->jpg_buffers.active = ZORAN_FREE;
  919. zr->jpg_buffers.allocated = 0;
  920. }
  921. res = -EBUSY; /* what are you doing? */
  922. break;
  923. }
  924. }
  925. spin_unlock_irqrestore(&zr->spinlock, flags);
  926. if (!res && zr->jpg_buffers.active == ZORAN_FREE) {
  927. zr->jpg_buffers.active = fh->jpg_buffers.active;
  928. }
  929. return res;
  930. }
  931. static int
  932. jpg_qbuf (struct file *file,
  933. int frame,
  934. enum zoran_codec_mode mode)
  935. {
  936. struct zoran_fh *fh = file->private_data;
  937. struct zoran *zr = fh->zr;
  938. int res = 0;
  939. /* Does the user want to stop streaming? */
  940. if (frame < 0) {
  941. if (zr->codec_mode == mode) {
  942. if (fh->jpg_buffers.active == ZORAN_FREE) {
  943. dprintk(1,
  944. KERN_ERR
  945. "%s: jpg_qbuf(-1) - session not active\n",
  946. ZR_DEVNAME(zr));
  947. return -EINVAL;
  948. }
  949. fh->jpg_buffers.active = zr->jpg_buffers.active =
  950. ZORAN_FREE;
  951. zr->jpg_buffers.allocated = 0;
  952. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  953. return 0;
  954. } else {
  955. dprintk(1,
  956. KERN_ERR
  957. "%s: jpg_qbuf() - stop streaming but not in streaming mode\n",
  958. ZR_DEVNAME(zr));
  959. return -EINVAL;
  960. }
  961. }
  962. if ((res = zoran_jpg_queue_frame(file, frame, mode)))
  963. return res;
  964. /* Start the jpeg codec when the first frame is queued */
  965. if (!res && zr->jpg_que_head == 1)
  966. jpeg_start(zr);
  967. return res;
  968. }
  969. /*
  970. * Sync on a MJPEG buffer
  971. */
  972. static int
  973. jpg_sync (struct file *file,
  974. struct zoran_sync *bs)
  975. {
  976. struct zoran_fh *fh = file->private_data;
  977. struct zoran *zr = fh->zr;
  978. unsigned long flags;
  979. int frame, timeout;
  980. if (fh->jpg_buffers.active == ZORAN_FREE) {
  981. dprintk(1,
  982. KERN_ERR
  983. "%s: jpg_sync() - capture is not currently active\n",
  984. ZR_DEVNAME(zr));
  985. return -EINVAL;
  986. }
  987. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS &&
  988. zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  989. dprintk(1,
  990. KERN_ERR
  991. "%s: jpg_sync() - codec not in streaming mode\n",
  992. ZR_DEVNAME(zr));
  993. return -EINVAL;
  994. }
  995. while (zr->jpg_que_tail == zr->jpg_dma_tail) {
  996. if (zr->jpg_dma_tail == zr->jpg_dma_head)
  997. break;
  998. timeout =
  999. interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
  1000. if (!timeout) {
  1001. int isr;
  1002. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  1003. udelay(1);
  1004. zr->codec->control(zr->codec, CODEC_G_STATUS,
  1005. sizeof(isr), &isr);
  1006. dprintk(1,
  1007. KERN_ERR
  1008. "%s: jpg_sync() - timeout: codec isr=0x%02x\n",
  1009. ZR_DEVNAME(zr), isr);
  1010. return -ETIME;
  1011. } else if (signal_pending(current))
  1012. return -ERESTARTSYS;
  1013. }
  1014. spin_lock_irqsave(&zr->spinlock, flags);
  1015. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  1016. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  1017. else
  1018. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  1019. /* buffer should now be in BUZ_STATE_DONE */
  1020. if (*zr_debug > 0)
  1021. if (zr->jpg_buffers.buffer[frame].state != BUZ_STATE_DONE)
  1022. dprintk(2,
  1023. KERN_ERR
  1024. "%s: jpg_sync() - internal state error\n",
  1025. ZR_DEVNAME(zr));
  1026. *bs = zr->jpg_buffers.buffer[frame].bs;
  1027. bs->frame = frame;
  1028. zr->jpg_buffers.buffer[frame].state = BUZ_STATE_USER;
  1029. fh->jpg_buffers.buffer[frame] = zr->jpg_buffers.buffer[frame];
  1030. spin_unlock_irqrestore(&zr->spinlock, flags);
  1031. return 0;
  1032. }
  1033. static void
  1034. zoran_open_init_session (struct file *file)
  1035. {
  1036. int i;
  1037. struct zoran_fh *fh = file->private_data;
  1038. struct zoran *zr = fh->zr;
  1039. /* Per default, map the V4L Buffers */
  1040. fh->map_mode = ZORAN_MAP_MODE_RAW;
  1041. /* take over the card's current settings */
  1042. fh->overlay_settings = zr->overlay_settings;
  1043. fh->overlay_settings.is_set = 0;
  1044. fh->overlay_settings.format = zr->overlay_settings.format;
  1045. fh->overlay_active = ZORAN_FREE;
  1046. /* v4l settings */
  1047. fh->v4l_settings = zr->v4l_settings;
  1048. /* v4l_buffers */
  1049. memset(&fh->v4l_buffers, 0, sizeof(struct zoran_v4l_struct));
  1050. for (i = 0; i < VIDEO_MAX_FRAME; i++) {
  1051. fh->v4l_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1052. fh->v4l_buffers.buffer[i].bs.frame = i;
  1053. }
  1054. fh->v4l_buffers.allocated = 0;
  1055. fh->v4l_buffers.ready_to_be_freed = 0;
  1056. fh->v4l_buffers.active = ZORAN_FREE;
  1057. fh->v4l_buffers.buffer_size = v4l_bufsize;
  1058. fh->v4l_buffers.num_buffers = v4l_nbufs;
  1059. /* jpg settings */
  1060. fh->jpg_settings = zr->jpg_settings;
  1061. /* jpg_buffers */
  1062. memset(&fh->jpg_buffers, 0, sizeof(struct zoran_jpg_struct));
  1063. for (i = 0; i < BUZ_MAX_FRAME; i++) {
  1064. fh->jpg_buffers.buffer[i].state = BUZ_STATE_USER; /* nothing going on */
  1065. fh->jpg_buffers.buffer[i].bs.frame = i;
  1066. }
  1067. fh->jpg_buffers.need_contiguous = zr->jpg_buffers.need_contiguous;
  1068. fh->jpg_buffers.allocated = 0;
  1069. fh->jpg_buffers.ready_to_be_freed = 0;
  1070. fh->jpg_buffers.active = ZORAN_FREE;
  1071. fh->jpg_buffers.buffer_size = jpg_bufsize;
  1072. fh->jpg_buffers.num_buffers = jpg_nbufs;
  1073. }
  1074. static void
  1075. zoran_close_end_session (struct file *file)
  1076. {
  1077. struct zoran_fh *fh = file->private_data;
  1078. struct zoran *zr = fh->zr;
  1079. /* overlay */
  1080. if (fh->overlay_active != ZORAN_FREE) {
  1081. fh->overlay_active = zr->overlay_active = ZORAN_FREE;
  1082. zr->v4l_overlay_active = 0;
  1083. if (!zr->v4l_memgrab_active)
  1084. zr36057_overlay(zr, 0);
  1085. zr->overlay_mask = NULL;
  1086. }
  1087. /* v4l capture */
  1088. if (fh->v4l_buffers.active != ZORAN_FREE) {
  1089. zr36057_set_memgrab(zr, 0);
  1090. zr->v4l_buffers.allocated = 0;
  1091. zr->v4l_buffers.active = fh->v4l_buffers.active =
  1092. ZORAN_FREE;
  1093. }
  1094. /* v4l buffers */
  1095. if (fh->v4l_buffers.allocated ||
  1096. fh->v4l_buffers.ready_to_be_freed) {
  1097. v4l_fbuffer_free(file);
  1098. }
  1099. /* jpg capture */
  1100. if (fh->jpg_buffers.active != ZORAN_FREE) {
  1101. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1102. zr->jpg_buffers.allocated = 0;
  1103. zr->jpg_buffers.active = fh->jpg_buffers.active =
  1104. ZORAN_FREE;
  1105. }
  1106. /* jpg buffers */
  1107. if (fh->jpg_buffers.allocated ||
  1108. fh->jpg_buffers.ready_to_be_freed) {
  1109. jpg_fbuffer_free(file);
  1110. }
  1111. }
  1112. /*
  1113. * Open a zoran card. Right now the flags stuff is just playing
  1114. */
  1115. static int
  1116. zoran_open (struct inode *inode,
  1117. struct file *file)
  1118. {
  1119. unsigned int minor = iminor(inode);
  1120. struct zoran *zr = NULL;
  1121. struct zoran_fh *fh;
  1122. int i, res, first_open = 0, have_module_locks = 0;
  1123. /* find the device */
  1124. for (i = 0; i < zoran_num; i++) {
  1125. if (zoran[i].video_dev->minor == minor) {
  1126. zr = &zoran[i];
  1127. break;
  1128. }
  1129. }
  1130. if (!zr) {
  1131. dprintk(1, KERN_ERR "%s: device not found!\n", ZORAN_NAME);
  1132. res = -ENODEV;
  1133. goto open_unlock_and_return;
  1134. }
  1135. /* see fs/device.c - the kernel already locks during open(),
  1136. * so locking ourselves only causes deadlocks */
  1137. /*down(&zr->resource_lock);*/
  1138. if (!zr->decoder) {
  1139. dprintk(1,
  1140. KERN_ERR "%s: no TV decoder loaded for device!\n",
  1141. ZR_DEVNAME(zr));
  1142. res = -EIO;
  1143. goto open_unlock_and_return;
  1144. }
  1145. /* try to grab a module lock */
  1146. if (!try_module_get(THIS_MODULE)) {
  1147. dprintk(1,
  1148. KERN_ERR
  1149. "%s: failed to acquire my own lock! PANIC!\n",
  1150. ZR_DEVNAME(zr));
  1151. res = -ENODEV;
  1152. goto open_unlock_and_return;
  1153. }
  1154. if (!try_module_get(zr->decoder->driver->owner)) {
  1155. dprintk(1,
  1156. KERN_ERR
  1157. "%s: failed to grab ownership of i2c decoder\n",
  1158. ZR_DEVNAME(zr));
  1159. res = -EIO;
  1160. module_put(THIS_MODULE);
  1161. goto open_unlock_and_return;
  1162. }
  1163. if (zr->encoder &&
  1164. !try_module_get(zr->encoder->driver->owner)) {
  1165. dprintk(1,
  1166. KERN_ERR
  1167. "%s: failed to grab ownership of i2c encoder\n",
  1168. ZR_DEVNAME(zr));
  1169. res = -EIO;
  1170. module_put(zr->decoder->driver->owner);
  1171. module_put(THIS_MODULE);
  1172. goto open_unlock_and_return;
  1173. }
  1174. have_module_locks = 1;
  1175. if (zr->user >= 2048) {
  1176. dprintk(1, KERN_ERR "%s: too many users (%d) on device\n",
  1177. ZR_DEVNAME(zr), zr->user);
  1178. res = -EBUSY;
  1179. goto open_unlock_and_return;
  1180. }
  1181. dprintk(1, KERN_INFO "%s: zoran_open(%s, pid=[%d]), users(-)=%d\n",
  1182. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1183. /* now, create the open()-specific file_ops struct */
  1184. fh = kmalloc(sizeof(struct zoran_fh), GFP_KERNEL);
  1185. if (!fh) {
  1186. dprintk(1,
  1187. KERN_ERR
  1188. "%s: zoran_open() - allocation of zoran_fh failed\n",
  1189. ZR_DEVNAME(zr));
  1190. res = -ENOMEM;
  1191. goto open_unlock_and_return;
  1192. }
  1193. memset(fh, 0, sizeof(struct zoran_fh));
  1194. /* used to be BUZ_MAX_WIDTH/HEIGHT, but that gives overflows
  1195. * on norm-change! */
  1196. fh->overlay_mask =
  1197. kmalloc(((768 + 31) / 32) * 576 * 4, GFP_KERNEL);
  1198. if (!fh->overlay_mask) {
  1199. dprintk(1,
  1200. KERN_ERR
  1201. "%s: zoran_open() - allocation of overlay_mask failed\n",
  1202. ZR_DEVNAME(zr));
  1203. kfree(fh);
  1204. res = -ENOMEM;
  1205. goto open_unlock_and_return;
  1206. }
  1207. if (zr->user++ == 0)
  1208. first_open = 1;
  1209. /*up(&zr->resource_lock);*/
  1210. /* default setup - TODO: look at flags */
  1211. if (first_open) { /* First device open */
  1212. zr36057_restart(zr);
  1213. zoran_open_init_params(zr);
  1214. zoran_init_hardware(zr);
  1215. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1216. }
  1217. /* set file_ops stuff */
  1218. file->private_data = fh;
  1219. fh->zr = zr;
  1220. zoran_open_init_session(file);
  1221. return 0;
  1222. open_unlock_and_return:
  1223. /* if we grabbed locks, release them accordingly */
  1224. if (have_module_locks) {
  1225. module_put(zr->decoder->driver->owner);
  1226. if (zr->encoder) {
  1227. module_put(zr->encoder->driver->owner);
  1228. }
  1229. module_put(THIS_MODULE);
  1230. }
  1231. /* if there's no device found, we didn't obtain the lock either */
  1232. if (zr) {
  1233. /*up(&zr->resource_lock);*/
  1234. }
  1235. return res;
  1236. }
  1237. static int
  1238. zoran_close (struct inode *inode,
  1239. struct file *file)
  1240. {
  1241. struct zoran_fh *fh = file->private_data;
  1242. struct zoran *zr = fh->zr;
  1243. dprintk(1, KERN_INFO "%s: zoran_close(%s, pid=[%d]), users(+)=%d\n",
  1244. ZR_DEVNAME(zr), current->comm, current->pid, zr->user);
  1245. /* kernel locks (fs/device.c), so don't do that ourselves
  1246. * (prevents deadlocks) */
  1247. /*down(&zr->resource_lock);*/
  1248. zoran_close_end_session(file);
  1249. if (zr->user-- == 1) { /* Last process */
  1250. /* Clean up JPEG process */
  1251. wake_up_interruptible(&zr->jpg_capq);
  1252. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  1253. zr->jpg_buffers.allocated = 0;
  1254. zr->jpg_buffers.active = ZORAN_FREE;
  1255. /* disable interrupts */
  1256. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  1257. if (*zr_debug > 1)
  1258. print_interrupts(zr);
  1259. /* Overlay off */
  1260. zr->v4l_overlay_active = 0;
  1261. zr36057_overlay(zr, 0);
  1262. zr->overlay_mask = NULL;
  1263. /* capture off */
  1264. wake_up_interruptible(&zr->v4l_capq);
  1265. zr36057_set_memgrab(zr, 0);
  1266. zr->v4l_buffers.allocated = 0;
  1267. zr->v4l_buffers.active = ZORAN_FREE;
  1268. zoran_set_pci_master(zr, 0);
  1269. if (!pass_through) { /* Switch to color bar */
  1270. int zero = 0, two = 2;
  1271. decoder_command(zr, DECODER_ENABLE_OUTPUT, &zero);
  1272. encoder_command(zr, ENCODER_SET_INPUT, &two);
  1273. }
  1274. }
  1275. file->private_data = NULL;
  1276. kfree(fh->overlay_mask);
  1277. kfree(fh);
  1278. /* release locks on the i2c modules */
  1279. module_put(zr->decoder->driver->owner);
  1280. if (zr->encoder) {
  1281. module_put(zr->encoder->driver->owner);
  1282. }
  1283. module_put(THIS_MODULE);
  1284. /*up(&zr->resource_lock);*/
  1285. dprintk(4, KERN_INFO "%s: zoran_close() done\n", ZR_DEVNAME(zr));
  1286. return 0;
  1287. }
  1288. static ssize_t
  1289. zoran_read (struct file *file,
  1290. char *data,
  1291. size_t count,
  1292. loff_t *ppos)
  1293. {
  1294. /* we simply don't support read() (yet)... */
  1295. return -EINVAL;
  1296. }
  1297. static ssize_t
  1298. zoran_write (struct file *file,
  1299. const char *data,
  1300. size_t count,
  1301. loff_t *ppos)
  1302. {
  1303. /* ...and the same goes for write() */
  1304. return -EINVAL;
  1305. }
  1306. static int
  1307. setup_fbuffer (struct file *file,
  1308. void *base,
  1309. const struct zoran_format *fmt,
  1310. int width,
  1311. int height,
  1312. int bytesperline)
  1313. {
  1314. struct zoran_fh *fh = file->private_data;
  1315. struct zoran *zr = fh->zr;
  1316. /* (Ronald) v4l/v4l2 guidelines */
  1317. if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
  1318. return -EPERM;
  1319. /* we need a bytesperline value, even if not given */
  1320. if (!bytesperline)
  1321. bytesperline = width * ((fmt->depth + 7) & ~7) / 8;
  1322. #if 0
  1323. if (zr->overlay_active) {
  1324. /* dzjee... stupid users... don't even bother to turn off
  1325. * overlay before changing the memory location...
  1326. * normally, we would return errors here. However, one of
  1327. * the tools that does this is... xawtv! and since xawtv
  1328. * is used by +/- 99% of the users, we'd rather be user-
  1329. * friendly and silently do as if nothing went wrong */
  1330. dprintk(3,
  1331. KERN_ERR
  1332. "%s: setup_fbuffer() - forced overlay turnoff because framebuffer changed\n",
  1333. ZR_DEVNAME(zr));
  1334. zr36057_overlay(zr, 0);
  1335. }
  1336. #endif
  1337. if (!(fmt->flags & ZORAN_FORMAT_OVERLAY)) {
  1338. dprintk(1,
  1339. KERN_ERR
  1340. "%s: setup_fbuffer() - no valid overlay format given\n",
  1341. ZR_DEVNAME(zr));
  1342. return -EINVAL;
  1343. }
  1344. if (height <= 0 || width <= 0 || bytesperline <= 0) {
  1345. dprintk(1,
  1346. KERN_ERR
  1347. "%s: setup_fbuffer() - invalid height/width/bpl value (%d|%d|%d)\n",
  1348. ZR_DEVNAME(zr), width, height, bytesperline);
  1349. return -EINVAL;
  1350. }
  1351. if (bytesperline & 3) {
  1352. dprintk(1,
  1353. KERN_ERR
  1354. "%s: setup_fbuffer() - bytesperline (%d) must be 4-byte aligned\n",
  1355. ZR_DEVNAME(zr), bytesperline);
  1356. return -EINVAL;
  1357. }
  1358. zr->buffer.base = (void *) ((unsigned long) base & ~3);
  1359. zr->buffer.height = height;
  1360. zr->buffer.width = width;
  1361. zr->buffer.depth = fmt->depth;
  1362. zr->overlay_settings.format = fmt;
  1363. zr->buffer.bytesperline = bytesperline;
  1364. /* The user should set new window parameters */
  1365. zr->overlay_settings.is_set = 0;
  1366. return 0;
  1367. }
  1368. static int
  1369. setup_window (struct file *file,
  1370. int x,
  1371. int y,
  1372. int width,
  1373. int height,
  1374. struct video_clip *clips,
  1375. int clipcount,
  1376. void *bitmap)
  1377. {
  1378. struct zoran_fh *fh = file->private_data;
  1379. struct zoran *zr = fh->zr;
  1380. struct video_clip *vcp = NULL;
  1381. int on, end;
  1382. if (!zr->buffer.base) {
  1383. dprintk(1,
  1384. KERN_ERR
  1385. "%s: setup_window() - frame buffer has to be set first\n",
  1386. ZR_DEVNAME(zr));
  1387. return -EINVAL;
  1388. }
  1389. if (!fh->overlay_settings.format) {
  1390. dprintk(1,
  1391. KERN_ERR
  1392. "%s: setup_window() - no overlay format set\n",
  1393. ZR_DEVNAME(zr));
  1394. return -EINVAL;
  1395. }
  1396. /*
  1397. * The video front end needs 4-byte alinged line sizes, we correct that
  1398. * silently here if necessary
  1399. */
  1400. if (zr->buffer.depth == 15 || zr->buffer.depth == 16) {
  1401. end = (x + width) & ~1; /* round down */
  1402. x = (x + 1) & ~1; /* round up */
  1403. width = end - x;
  1404. }
  1405. if (zr->buffer.depth == 24) {
  1406. end = (x + width) & ~3; /* round down */
  1407. x = (x + 3) & ~3; /* round up */
  1408. width = end - x;
  1409. }
  1410. if (width > BUZ_MAX_WIDTH)
  1411. width = BUZ_MAX_WIDTH;
  1412. if (height > BUZ_MAX_HEIGHT)
  1413. height = BUZ_MAX_HEIGHT;
  1414. /* Check for vaild parameters */
  1415. if (width < BUZ_MIN_WIDTH || height < BUZ_MIN_HEIGHT ||
  1416. width > BUZ_MAX_WIDTH || height > BUZ_MAX_HEIGHT) {
  1417. dprintk(1,
  1418. KERN_ERR
  1419. "%s: setup_window() - width = %d or height = %d invalid\n",
  1420. ZR_DEVNAME(zr), width, height);
  1421. return -EINVAL;
  1422. }
  1423. fh->overlay_settings.x = x;
  1424. fh->overlay_settings.y = y;
  1425. fh->overlay_settings.width = width;
  1426. fh->overlay_settings.height = height;
  1427. fh->overlay_settings.clipcount = clipcount;
  1428. /*
  1429. * If an overlay is running, we have to switch it off
  1430. * and switch it on again in order to get the new settings in effect.
  1431. *
  1432. * We also want to avoid that the overlay mask is written
  1433. * when an overlay is running.
  1434. */
  1435. on = zr->v4l_overlay_active && !zr->v4l_memgrab_active &&
  1436. zr->overlay_active != ZORAN_FREE &&
  1437. fh->overlay_active != ZORAN_FREE;
  1438. if (on)
  1439. zr36057_overlay(zr, 0);
  1440. /*
  1441. * Write the overlay mask if clips are wanted.
  1442. * We prefer a bitmap.
  1443. */
  1444. if (bitmap) {
  1445. /* fake value - it just means we want clips */
  1446. fh->overlay_settings.clipcount = 1;
  1447. if (copy_from_user(fh->overlay_mask, bitmap,
  1448. (width * height + 7) / 8)) {
  1449. return -EFAULT;
  1450. }
  1451. } else if (clipcount > 0) {
  1452. /* write our own bitmap from the clips */
  1453. vcp = vmalloc(sizeof(struct video_clip) * (clipcount + 4));
  1454. if (vcp == NULL) {
  1455. dprintk(1,
  1456. KERN_ERR
  1457. "%s: setup_window() - Alloc of clip mask failed\n",
  1458. ZR_DEVNAME(zr));
  1459. return -ENOMEM;
  1460. }
  1461. if (copy_from_user
  1462. (vcp, clips, sizeof(struct video_clip) * clipcount)) {
  1463. vfree(vcp);
  1464. return -EFAULT;
  1465. }
  1466. write_overlay_mask(file, vcp, clipcount);
  1467. vfree(vcp);
  1468. }
  1469. fh->overlay_settings.is_set = 1;
  1470. if (fh->overlay_active != ZORAN_FREE &&
  1471. zr->overlay_active != ZORAN_FREE)
  1472. zr->overlay_settings = fh->overlay_settings;
  1473. if (on)
  1474. zr36057_overlay(zr, 1);
  1475. /* Make sure the changes come into effect */
  1476. return wait_grab_pending(zr);
  1477. }
  1478. static int
  1479. setup_overlay (struct file *file,
  1480. int on)
  1481. {
  1482. struct zoran_fh *fh = file->private_data;
  1483. struct zoran *zr = fh->zr;
  1484. /* If there is nothing to do, return immediatly */
  1485. if ((on && fh->overlay_active != ZORAN_FREE) ||
  1486. (!on && fh->overlay_active == ZORAN_FREE))
  1487. return 0;
  1488. /* check whether we're touching someone else's overlay */
  1489. if (on && zr->overlay_active != ZORAN_FREE &&
  1490. fh->overlay_active == ZORAN_FREE) {
  1491. dprintk(1,
  1492. KERN_ERR
  1493. "%s: setup_overlay() - overlay is already active for another session\n",
  1494. ZR_DEVNAME(zr));
  1495. return -EBUSY;
  1496. }
  1497. if (!on && zr->overlay_active != ZORAN_FREE &&
  1498. fh->overlay_active == ZORAN_FREE) {
  1499. dprintk(1,
  1500. KERN_ERR
  1501. "%s: setup_overlay() - you cannot cancel someone else's session\n",
  1502. ZR_DEVNAME(zr));
  1503. return -EPERM;
  1504. }
  1505. if (on == 0) {
  1506. zr->overlay_active = fh->overlay_active = ZORAN_FREE;
  1507. zr->v4l_overlay_active = 0;
  1508. /* When a grab is running, the video simply
  1509. * won't be switched on any more */
  1510. if (!zr->v4l_memgrab_active)
  1511. zr36057_overlay(zr, 0);
  1512. zr->overlay_mask = NULL;
  1513. } else {
  1514. if (!zr->buffer.base || !fh->overlay_settings.is_set) {
  1515. dprintk(1,
  1516. KERN_ERR
  1517. "%s: setup_overlay() - buffer or window not set\n",
  1518. ZR_DEVNAME(zr));
  1519. return -EINVAL;
  1520. }
  1521. if (!fh->overlay_settings.format) {
  1522. dprintk(1,
  1523. KERN_ERR
  1524. "%s: setup_overlay() - no overlay format set\n",
  1525. ZR_DEVNAME(zr));
  1526. return -EINVAL;
  1527. }
  1528. zr->overlay_active = fh->overlay_active = ZORAN_LOCKED;
  1529. zr->v4l_overlay_active = 1;
  1530. zr->overlay_mask = fh->overlay_mask;
  1531. zr->overlay_settings = fh->overlay_settings;
  1532. if (!zr->v4l_memgrab_active)
  1533. zr36057_overlay(zr, 1);
  1534. /* When a grab is running, the video will be
  1535. * switched on when grab is finished */
  1536. }
  1537. /* Make sure the changes come into effect */
  1538. return wait_grab_pending(zr);
  1539. }
  1540. #ifdef HAVE_V4L2
  1541. /* get the status of a buffer in the clients buffer queue */
  1542. static int
  1543. zoran_v4l2_buffer_status (struct file *file,
  1544. struct v4l2_buffer *buf,
  1545. int num)
  1546. {
  1547. struct zoran_fh *fh = file->private_data;
  1548. struct zoran *zr = fh->zr;
  1549. buf->flags = V4L2_BUF_FLAG_MAPPED;
  1550. switch (fh->map_mode) {
  1551. case ZORAN_MAP_MODE_RAW:
  1552. /* check range */
  1553. if (num < 0 || num >= fh->v4l_buffers.num_buffers ||
  1554. !fh->v4l_buffers.allocated) {
  1555. dprintk(1,
  1556. KERN_ERR
  1557. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1558. ZR_DEVNAME(zr));
  1559. return -EINVAL;
  1560. }
  1561. buf->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1562. buf->length = fh->v4l_buffers.buffer_size;
  1563. /* get buffer */
  1564. buf->bytesused = fh->v4l_buffers.buffer[num].bs.length;
  1565. if (fh->v4l_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1566. fh->v4l_buffers.buffer[num].state == BUZ_STATE_USER) {
  1567. buf->sequence = fh->v4l_buffers.buffer[num].bs.seq;
  1568. buf->flags |= V4L2_BUF_FLAG_DONE;
  1569. buf->timestamp =
  1570. fh->v4l_buffers.buffer[num].bs.timestamp;
  1571. } else {
  1572. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1573. }
  1574. if (fh->v4l_settings.height <= BUZ_MAX_HEIGHT / 2)
  1575. buf->field = V4L2_FIELD_TOP;
  1576. else
  1577. buf->field = V4L2_FIELD_INTERLACED;
  1578. break;
  1579. case ZORAN_MAP_MODE_JPG_REC:
  1580. case ZORAN_MAP_MODE_JPG_PLAY:
  1581. /* check range */
  1582. if (num < 0 || num >= fh->jpg_buffers.num_buffers ||
  1583. !fh->jpg_buffers.allocated) {
  1584. dprintk(1,
  1585. KERN_ERR
  1586. "%s: v4l2_buffer_status() - wrong number or buffers not allocated\n",
  1587. ZR_DEVNAME(zr));
  1588. return -EINVAL;
  1589. }
  1590. buf->type = (fh->map_mode == ZORAN_MAP_MODE_JPG_REC) ?
  1591. V4L2_BUF_TYPE_VIDEO_CAPTURE :
  1592. V4L2_BUF_TYPE_VIDEO_OUTPUT;
  1593. buf->length = fh->jpg_buffers.buffer_size;
  1594. /* these variables are only written after frame has been captured */
  1595. if (fh->jpg_buffers.buffer[num].state == BUZ_STATE_DONE ||
  1596. fh->jpg_buffers.buffer[num].state == BUZ_STATE_USER) {
  1597. buf->sequence = fh->jpg_buffers.buffer[num].bs.seq;
  1598. buf->timestamp =
  1599. fh->jpg_buffers.buffer[num].bs.timestamp;
  1600. buf->bytesused =
  1601. fh->jpg_buffers.buffer[num].bs.length;
  1602. buf->flags |= V4L2_BUF_FLAG_DONE;
  1603. } else {
  1604. buf->flags |= V4L2_BUF_FLAG_QUEUED;
  1605. }
  1606. /* which fields are these? */
  1607. if (fh->jpg_settings.TmpDcm != 1)
  1608. buf->field =
  1609. fh->jpg_settings.
  1610. odd_even ? V4L2_FIELD_TOP : V4L2_FIELD_BOTTOM;
  1611. else
  1612. buf->field =
  1613. fh->jpg_settings.
  1614. odd_even ? V4L2_FIELD_SEQ_TB :
  1615. V4L2_FIELD_SEQ_BT;
  1616. break;
  1617. default:
  1618. dprintk(5,
  1619. KERN_ERR
  1620. "%s: v4l2_buffer_status() - invalid buffer type|map_mode (%d|%d)\n",
  1621. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  1622. return -EINVAL;
  1623. }
  1624. buf->memory = V4L2_MEMORY_MMAP;
  1625. buf->index = num;
  1626. buf->m.offset = buf->length * num;
  1627. return 0;
  1628. }
  1629. #endif
  1630. static int
  1631. zoran_set_norm (struct zoran *zr,
  1632. int norm) /* VIDEO_MODE_* */
  1633. {
  1634. int norm_encoder, on;
  1635. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1636. zr->jpg_buffers.active != ZORAN_FREE) {
  1637. dprintk(1,
  1638. KERN_WARNING
  1639. "%s: set_norm() called while in playback/capture mode\n",
  1640. ZR_DEVNAME(zr));
  1641. return -EBUSY;
  1642. }
  1643. if (lock_norm && norm != zr->norm) {
  1644. if (lock_norm > 1) {
  1645. dprintk(1,
  1646. KERN_WARNING
  1647. "%s: set_norm() - TV standard is locked, can not switch norm\n",
  1648. ZR_DEVNAME(zr));
  1649. return -EPERM;
  1650. } else {
  1651. dprintk(1,
  1652. KERN_WARNING
  1653. "%s: set_norm() - TV standard is locked, norm was not changed\n",
  1654. ZR_DEVNAME(zr));
  1655. norm = zr->norm;
  1656. }
  1657. }
  1658. if (norm != VIDEO_MODE_AUTO &&
  1659. (norm < 0 || norm >= zr->card.norms ||
  1660. !zr->card.tvn[norm])) {
  1661. dprintk(1,
  1662. KERN_ERR "%s: set_norm() - unsupported norm %d\n",
  1663. ZR_DEVNAME(zr), norm);
  1664. return -EINVAL;
  1665. }
  1666. if (norm == VIDEO_MODE_AUTO) {
  1667. int status;
  1668. /* if we have autodetect, ... */
  1669. struct video_decoder_capability caps;
  1670. decoder_command(zr, DECODER_GET_CAPABILITIES, &caps);
  1671. if (!(caps.flags & VIDEO_DECODER_AUTO)) {
  1672. dprintk(1, KERN_ERR "%s: norm=auto unsupported\n",
  1673. ZR_DEVNAME(zr));
  1674. return -EINVAL;
  1675. }
  1676. decoder_command(zr, DECODER_SET_NORM, &norm);
  1677. /* let changes come into effect */
  1678. current->state = TASK_UNINTERRUPTIBLE;
  1679. schedule_timeout(2 * HZ);
  1680. decoder_command(zr, DECODER_GET_STATUS, &status);
  1681. if (!(status & DECODER_STATUS_GOOD)) {
  1682. dprintk(1,
  1683. KERN_ERR
  1684. "%s: set_norm() - no norm detected\n",
  1685. ZR_DEVNAME(zr));
  1686. /* reset norm */
  1687. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  1688. return -EIO;
  1689. }
  1690. if (status & DECODER_STATUS_NTSC)
  1691. norm = VIDEO_MODE_NTSC;
  1692. else if (status & DECODER_STATUS_SECAM)
  1693. norm = VIDEO_MODE_SECAM;
  1694. else
  1695. norm = VIDEO_MODE_PAL;
  1696. }
  1697. zr->timing = zr->card.tvn[norm];
  1698. norm_encoder = norm;
  1699. /* We switch overlay off and on since a change in the
  1700. * norm needs different VFE settings */
  1701. on = zr->overlay_active && !zr->v4l_memgrab_active;
  1702. if (on)
  1703. zr36057_overlay(zr, 0);
  1704. decoder_command(zr, DECODER_SET_NORM, &norm);
  1705. encoder_command(zr, ENCODER_SET_NORM, &norm_encoder);
  1706. if (on)
  1707. zr36057_overlay(zr, 1);
  1708. /* Make sure the changes come into effect */
  1709. zr->norm = norm;
  1710. return 0;
  1711. }
  1712. static int
  1713. zoran_set_input (struct zoran *zr,
  1714. int input)
  1715. {
  1716. int realinput;
  1717. if (input == zr->input) {
  1718. return 0;
  1719. }
  1720. if (zr->v4l_buffers.active != ZORAN_FREE ||
  1721. zr->jpg_buffers.active != ZORAN_FREE) {
  1722. dprintk(1,
  1723. KERN_WARNING
  1724. "%s: set_input() called while in playback/capture mode\n",
  1725. ZR_DEVNAME(zr));
  1726. return -EBUSY;
  1727. }
  1728. if (input < 0 || input >= zr->card.inputs) {
  1729. dprintk(1,
  1730. KERN_ERR
  1731. "%s: set_input() - unnsupported input %d\n",
  1732. ZR_DEVNAME(zr), input);
  1733. return -EINVAL;
  1734. }
  1735. realinput = zr->card.input[input].muxsel;
  1736. zr->input = input;
  1737. decoder_command(zr, DECODER_SET_INPUT, &realinput);
  1738. return 0;
  1739. }
  1740. /*
  1741. * ioctl routine
  1742. */
  1743. static int
  1744. zoran_do_ioctl (struct inode *inode,
  1745. struct file *file,
  1746. unsigned int cmd,
  1747. void *arg)
  1748. {
  1749. struct zoran_fh *fh = file->private_data;
  1750. struct zoran *zr = fh->zr;
  1751. /* we might have older buffers lying around... We don't want
  1752. * to wait, but we do want to try cleaning them up ASAP. So
  1753. * we try to obtain the lock and free them. If that fails, we
  1754. * don't do anything and wait for the next turn. In the end,
  1755. * zoran_close() or a new allocation will still free them...
  1756. * This is just a 'the sooner the better' extra 'feature'
  1757. *
  1758. * We don't free the buffers right on munmap() because that
  1759. * causes oopses (kfree() inside munmap() oopses for no
  1760. * apparent reason - it's also not reproduceable in any way,
  1761. * but moving the free code outside the munmap() handler fixes
  1762. * all this... If someone knows why, please explain me (Ronald)
  1763. */
  1764. if (!down_trylock(&zr->resource_lock)) {
  1765. /* we obtained it! Let's try to free some things */
  1766. if (fh->jpg_buffers.ready_to_be_freed)
  1767. jpg_fbuffer_free(file);
  1768. if (fh->v4l_buffers.ready_to_be_freed)
  1769. v4l_fbuffer_free(file);
  1770. up(&zr->resource_lock);
  1771. }
  1772. switch (cmd) {
  1773. case VIDIOCGCAP:
  1774. {
  1775. struct video_capability *vcap = arg;
  1776. dprintk(3, KERN_DEBUG "%s: VIDIOCGCAP\n", ZR_DEVNAME(zr));
  1777. memset(vcap, 0, sizeof(struct video_capability));
  1778. strncpy(vcap->name, ZR_DEVNAME(zr), sizeof(vcap->name));
  1779. vcap->type = ZORAN_VID_TYPE;
  1780. vcap->channels = zr->card.inputs;
  1781. vcap->audios = 0;
  1782. down(&zr->resource_lock);
  1783. vcap->maxwidth = BUZ_MAX_WIDTH;
  1784. vcap->maxheight = BUZ_MAX_HEIGHT;
  1785. vcap->minwidth = BUZ_MIN_WIDTH;
  1786. vcap->minheight = BUZ_MIN_HEIGHT;
  1787. up(&zr->resource_lock);
  1788. return 0;
  1789. }
  1790. break;
  1791. case VIDIOCGCHAN:
  1792. {
  1793. struct video_channel *vchan = arg;
  1794. int channel = vchan->channel;
  1795. dprintk(3, KERN_DEBUG "%s: VIDIOCGCHAN - channel=%d\n",
  1796. ZR_DEVNAME(zr), vchan->channel);
  1797. memset(vchan, 0, sizeof(struct video_channel));
  1798. if (channel > zr->card.inputs || channel < 0) {
  1799. dprintk(1,
  1800. KERN_ERR
  1801. "%s: VIDIOCGCHAN on not existing channel %d\n",
  1802. ZR_DEVNAME(zr), channel);
  1803. return -EINVAL;
  1804. }
  1805. strcpy(vchan->name, zr->card.input[channel].name);
  1806. vchan->tuners = 0;
  1807. vchan->flags = 0;
  1808. vchan->type = VIDEO_TYPE_CAMERA;
  1809. down(&zr->resource_lock);
  1810. vchan->norm = zr->norm;
  1811. up(&zr->resource_lock);
  1812. vchan->channel = channel;
  1813. return 0;
  1814. }
  1815. break;
  1816. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  1817. *
  1818. * * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  1819. * * ^^^^^^^
  1820. * * The famos BTTV driver has it implemented with a struct video_channel argument
  1821. * * and we follow it for compatibility reasons
  1822. * *
  1823. * * BTW: this is the only way the user can set the norm!
  1824. */
  1825. case VIDIOCSCHAN:
  1826. {
  1827. struct video_channel *vchan = arg;
  1828. int res;
  1829. dprintk(3,
  1830. KERN_DEBUG
  1831. "%s: VIDIOCSCHAN - channel=%d, norm=%d\n",
  1832. ZR_DEVNAME(zr), vchan->channel, vchan->norm);
  1833. down(&zr->resource_lock);
  1834. if ((res = zoran_set_input(zr, vchan->channel)))
  1835. goto schan_unlock_and_return;
  1836. if ((res = zoran_set_norm(zr, vchan->norm)))
  1837. goto schan_unlock_and_return;
  1838. /* Make sure the changes come into effect */
  1839. res = wait_grab_pending(zr);
  1840. schan_unlock_and_return:
  1841. up(&zr->resource_lock);
  1842. return res;
  1843. }
  1844. break;
  1845. case VIDIOCGPICT:
  1846. {
  1847. struct video_picture *vpict = arg;
  1848. dprintk(3, KERN_DEBUG "%s: VIDIOCGPICT\n", ZR_DEVNAME(zr));
  1849. memset(vpict, 0, sizeof(struct video_picture));
  1850. down(&zr->resource_lock);
  1851. vpict->hue = zr->hue;
  1852. vpict->brightness = zr->brightness;
  1853. vpict->contrast = zr->contrast;
  1854. vpict->colour = zr->saturation;
  1855. if (fh->overlay_settings.format) {
  1856. vpict->depth = fh->overlay_settings.format->depth;
  1857. vpict->palette = fh->overlay_settings.format->palette;
  1858. } else {
  1859. vpict->depth = 0;
  1860. }
  1861. up(&zr->resource_lock);
  1862. return 0;
  1863. }
  1864. break;
  1865. case VIDIOCSPICT:
  1866. {
  1867. struct video_picture *vpict = arg;
  1868. int i;
  1869. dprintk(3,
  1870. KERN_DEBUG
  1871. "%s: VIDIOCSPICT - bri=%d, hue=%d, col=%d, con=%d, dep=%d, pal=%d\n",
  1872. ZR_DEVNAME(zr), vpict->brightness, vpict->hue,
  1873. vpict->colour, vpict->contrast, vpict->depth,
  1874. vpict->palette);
  1875. for (i = 0; i < zoran_num_formats; i++) {
  1876. const struct zoran_format *fmt = &zoran_formats[i];
  1877. if (fmt->palette != -1 &&
  1878. fmt->flags & ZORAN_FORMAT_OVERLAY &&
  1879. fmt->palette == vpict->palette &&
  1880. fmt->depth == vpict->depth)
  1881. break;
  1882. }
  1883. if (i == zoran_num_formats) {
  1884. dprintk(1,
  1885. KERN_ERR
  1886. "%s: VIDIOCSPICT - Invalid palette %d\n",
  1887. ZR_DEVNAME(zr), vpict->palette);
  1888. return -EINVAL;
  1889. }
  1890. down(&zr->resource_lock);
  1891. decoder_command(zr, DECODER_SET_PICTURE, vpict);
  1892. zr->hue = vpict->hue;
  1893. zr->contrast = vpict->contrast;
  1894. zr->saturation = vpict->colour;
  1895. zr->brightness = vpict->brightness;
  1896. fh->overlay_settings.format = &zoran_formats[i];
  1897. up(&zr->resource_lock);
  1898. return 0;
  1899. }
  1900. break;
  1901. case VIDIOCCAPTURE:
  1902. {
  1903. int *on = arg, res;
  1904. dprintk(3, KERN_DEBUG "%s: VIDIOCCAPTURE - on=%d\n",
  1905. ZR_DEVNAME(zr), *on);
  1906. down(&zr->resource_lock);
  1907. res = setup_overlay(file, *on);
  1908. up(&zr->resource_lock);
  1909. return res;
  1910. }
  1911. break;
  1912. case VIDIOCGWIN:
  1913. {
  1914. struct video_window *vwin = arg;
  1915. dprintk(3, KERN_DEBUG "%s: VIDIOCGWIN\n", ZR_DEVNAME(zr));
  1916. memset(vwin, 0, sizeof(struct video_window));
  1917. down(&zr->resource_lock);
  1918. vwin->x = fh->overlay_settings.x;
  1919. vwin->y = fh->overlay_settings.y;
  1920. vwin->width = fh->overlay_settings.width;
  1921. vwin->height = fh->overlay_settings.height;
  1922. up(&zr->resource_lock);
  1923. vwin->clipcount = 0;
  1924. return 0;
  1925. }
  1926. break;
  1927. case VIDIOCSWIN:
  1928. {
  1929. struct video_window *vwin = arg;
  1930. int res;
  1931. dprintk(3,
  1932. KERN_DEBUG
  1933. "%s: VIDIOCSWIN - x=%d, y=%d, w=%d, h=%d, clipcount=%d\n",
  1934. ZR_DEVNAME(zr), vwin->x, vwin->y, vwin->width,
  1935. vwin->height, vwin->clipcount);
  1936. down(&zr->resource_lock);
  1937. res =
  1938. setup_window(file, vwin->x, vwin->y, vwin->width,
  1939. vwin->height, vwin->clips,
  1940. vwin->clipcount, NULL);
  1941. up(&zr->resource_lock);
  1942. return res;
  1943. }
  1944. break;
  1945. case VIDIOCGFBUF:
  1946. {
  1947. struct video_buffer *vbuf = arg;
  1948. dprintk(3, KERN_DEBUG "%s: VIDIOCGFBUF\n", ZR_DEVNAME(zr));
  1949. down(&zr->resource_lock);
  1950. *vbuf = zr->buffer;
  1951. up(&zr->resource_lock);
  1952. return 0;
  1953. }
  1954. break;
  1955. case VIDIOCSFBUF:
  1956. {
  1957. struct video_buffer *vbuf = arg;
  1958. int i, res = 0;
  1959. dprintk(3,
  1960. KERN_DEBUG
  1961. "%s: VIDIOCSFBUF - base=0x%x, w=%d, h=%d, depth=%d, bpl=%d\n",
  1962. ZR_DEVNAME(zr), (u32) vbuf->base, vbuf->width,
  1963. vbuf->height, vbuf->depth, vbuf->bytesperline);
  1964. for (i = 0; i < zoran_num_formats; i++)
  1965. if (zoran_formats[i].depth == vbuf->depth)
  1966. break;
  1967. if (i == zoran_num_formats) {
  1968. dprintk(1,
  1969. KERN_ERR
  1970. "%s: VIDIOCSFBUF - invalid fbuf depth %d\n",
  1971. ZR_DEVNAME(zr), vbuf->depth);
  1972. return -EINVAL;
  1973. }
  1974. down(&zr->resource_lock);
  1975. res =
  1976. setup_fbuffer(file, vbuf->base, &zoran_formats[i],
  1977. vbuf->width, vbuf->height,
  1978. vbuf->bytesperline);
  1979. up(&zr->resource_lock);
  1980. return res;
  1981. }
  1982. break;
  1983. case VIDIOCSYNC:
  1984. {
  1985. int *frame = arg, res;
  1986. dprintk(3, KERN_DEBUG "%s: VIDIOCSYNC - frame=%d\n",
  1987. ZR_DEVNAME(zr), *frame);
  1988. down(&zr->resource_lock);
  1989. res = v4l_sync(file, *frame);
  1990. up(&zr->resource_lock);
  1991. if (!res)
  1992. zr->v4l_sync_tail++;
  1993. return res;
  1994. }
  1995. break;
  1996. case VIDIOCMCAPTURE:
  1997. {
  1998. struct video_mmap *vmap = arg;
  1999. int res;
  2000. dprintk(3,
  2001. KERN_DEBUG
  2002. "%s: VIDIOCMCAPTURE - frame=%d, geom=%dx%d, fmt=%d\n",
  2003. ZR_DEVNAME(zr), vmap->frame, vmap->width, vmap->height,
  2004. vmap->format);
  2005. down(&zr->resource_lock);
  2006. res = v4l_grab(file, vmap);
  2007. up(&zr->resource_lock);
  2008. return res;
  2009. }
  2010. break;
  2011. case VIDIOCGMBUF:
  2012. {
  2013. struct video_mbuf *vmbuf = arg;
  2014. int i, res = 0;
  2015. dprintk(3, KERN_DEBUG "%s: VIDIOCGMBUF\n", ZR_DEVNAME(zr));
  2016. vmbuf->size =
  2017. fh->v4l_buffers.num_buffers *
  2018. fh->v4l_buffers.buffer_size;
  2019. vmbuf->frames = fh->v4l_buffers.num_buffers;
  2020. for (i = 0; i < vmbuf->frames; i++) {
  2021. vmbuf->offsets[i] =
  2022. i * fh->v4l_buffers.buffer_size;
  2023. }
  2024. down(&zr->resource_lock);
  2025. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2026. dprintk(1,
  2027. KERN_ERR
  2028. "%s: VIDIOCGMBUF - buffers already allocated\n",
  2029. ZR_DEVNAME(zr));
  2030. res = -EINVAL;
  2031. goto v4l1reqbuf_unlock_and_return;
  2032. }
  2033. if (v4l_fbuffer_alloc(file)) {
  2034. res = -ENOMEM;
  2035. goto v4l1reqbuf_unlock_and_return;
  2036. }
  2037. /* The next mmap will map the V4L buffers */
  2038. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2039. v4l1reqbuf_unlock_and_return:
  2040. up(&zr->resource_lock);
  2041. return res;
  2042. }
  2043. break;
  2044. case VIDIOCGUNIT:
  2045. {
  2046. struct video_unit *vunit = arg;
  2047. dprintk(3, KERN_DEBUG "%s: VIDIOCGUNIT\n", ZR_DEVNAME(zr));
  2048. vunit->video = zr->video_dev->minor;
  2049. vunit->vbi = VIDEO_NO_UNIT;
  2050. vunit->radio = VIDEO_NO_UNIT;
  2051. vunit->audio = VIDEO_NO_UNIT;
  2052. vunit->teletext = VIDEO_NO_UNIT;
  2053. return 0;
  2054. }
  2055. break;
  2056. /*
  2057. * RJ: In principal we could support subcaptures for V4L grabbing.
  2058. * Not even the famous BTTV driver has them, however.
  2059. * If there should be a strong demand, one could consider
  2060. * to implement them.
  2061. */
  2062. case VIDIOCGCAPTURE:
  2063. {
  2064. dprintk(3, KERN_ERR "%s: VIDIOCGCAPTURE not supported\n",
  2065. ZR_DEVNAME(zr));
  2066. return -EINVAL;
  2067. }
  2068. break;
  2069. case VIDIOCSCAPTURE:
  2070. {
  2071. dprintk(3, KERN_ERR "%s: VIDIOCSCAPTURE not supported\n",
  2072. ZR_DEVNAME(zr));
  2073. return -EINVAL;
  2074. }
  2075. break;
  2076. case BUZIOC_G_PARAMS:
  2077. {
  2078. struct zoran_params *bparams = arg;
  2079. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_PARAMS\n", ZR_DEVNAME(zr));
  2080. memset(bparams, 0, sizeof(struct zoran_params));
  2081. bparams->major_version = MAJOR_VERSION;
  2082. bparams->minor_version = MINOR_VERSION;
  2083. down(&zr->resource_lock);
  2084. bparams->norm = zr->norm;
  2085. bparams->input = zr->input;
  2086. bparams->decimation = fh->jpg_settings.decimation;
  2087. bparams->HorDcm = fh->jpg_settings.HorDcm;
  2088. bparams->VerDcm = fh->jpg_settings.VerDcm;
  2089. bparams->TmpDcm = fh->jpg_settings.TmpDcm;
  2090. bparams->field_per_buff = fh->jpg_settings.field_per_buff;
  2091. bparams->img_x = fh->jpg_settings.img_x;
  2092. bparams->img_y = fh->jpg_settings.img_y;
  2093. bparams->img_width = fh->jpg_settings.img_width;
  2094. bparams->img_height = fh->jpg_settings.img_height;
  2095. bparams->odd_even = fh->jpg_settings.odd_even;
  2096. bparams->quality = fh->jpg_settings.jpg_comp.quality;
  2097. bparams->APPn = fh->jpg_settings.jpg_comp.APPn;
  2098. bparams->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  2099. memcpy(bparams->APP_data,
  2100. fh->jpg_settings.jpg_comp.APP_data,
  2101. sizeof(bparams->APP_data));
  2102. bparams->COM_len = zr->jpg_settings.jpg_comp.COM_len;
  2103. memcpy(bparams->COM_data,
  2104. fh->jpg_settings.jpg_comp.COM_data,
  2105. sizeof(bparams->COM_data));
  2106. bparams->jpeg_markers =
  2107. fh->jpg_settings.jpg_comp.jpeg_markers;
  2108. up(&zr->resource_lock);
  2109. bparams->VFIFO_FB = 0;
  2110. return 0;
  2111. }
  2112. break;
  2113. case BUZIOC_S_PARAMS:
  2114. {
  2115. struct zoran_params *bparams = arg;
  2116. struct zoran_jpg_settings settings;
  2117. int res = 0;
  2118. dprintk(3, KERN_DEBUG "%s: BUZIOC_S_PARAMS\n", ZR_DEVNAME(zr));
  2119. settings.decimation = bparams->decimation;
  2120. settings.HorDcm = bparams->HorDcm;
  2121. settings.VerDcm = bparams->VerDcm;
  2122. settings.TmpDcm = bparams->TmpDcm;
  2123. settings.field_per_buff = bparams->field_per_buff;
  2124. settings.img_x = bparams->img_x;
  2125. settings.img_y = bparams->img_y;
  2126. settings.img_width = bparams->img_width;
  2127. settings.img_height = bparams->img_height;
  2128. settings.odd_even = bparams->odd_even;
  2129. settings.jpg_comp.quality = bparams->quality;
  2130. settings.jpg_comp.APPn = bparams->APPn;
  2131. settings.jpg_comp.APP_len = bparams->APP_len;
  2132. memcpy(settings.jpg_comp.APP_data, bparams->APP_data,
  2133. sizeof(bparams->APP_data));
  2134. settings.jpg_comp.COM_len = bparams->COM_len;
  2135. memcpy(settings.jpg_comp.COM_data, bparams->COM_data,
  2136. sizeof(bparams->COM_data));
  2137. settings.jpg_comp.jpeg_markers = bparams->jpeg_markers;
  2138. down(&zr->resource_lock);
  2139. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2140. dprintk(1,
  2141. KERN_ERR
  2142. "%s: BUZIOC_S_PARAMS called, but Buz in capture/playback mode\n",
  2143. ZR_DEVNAME(zr));
  2144. res = -EINVAL;
  2145. goto sparams_unlock_and_return;
  2146. }
  2147. /* Check the params first before overwriting our
  2148. * nternal values */
  2149. if (zoran_check_jpg_settings(zr, &settings)) {
  2150. res = -EINVAL;
  2151. goto sparams_unlock_and_return;
  2152. }
  2153. fh->jpg_settings = settings;
  2154. sparams_unlock_and_return:
  2155. up(&zr->resource_lock);
  2156. return res;
  2157. }
  2158. break;
  2159. case BUZIOC_REQBUFS:
  2160. {
  2161. struct zoran_requestbuffers *breq = arg;
  2162. int res = 0;
  2163. dprintk(3,
  2164. KERN_DEBUG
  2165. "%s: BUZIOC_REQBUFS - count=%lu, size=%lu\n",
  2166. ZR_DEVNAME(zr), breq->count, breq->size);
  2167. /* Enforce reasonable lower and upper limits */
  2168. if (breq->count < 4)
  2169. breq->count = 4; /* Could be choosen smaller */
  2170. if (breq->count > jpg_nbufs)
  2171. breq->count = jpg_nbufs;
  2172. breq->size = PAGE_ALIGN(breq->size);
  2173. if (breq->size < 8192)
  2174. breq->size = 8192; /* Arbitrary */
  2175. /* breq->size is limited by 1 page for the stat_com
  2176. * tables to a Maximum of 2 MB */
  2177. if (breq->size > jpg_bufsize)
  2178. breq->size = jpg_bufsize;
  2179. if (fh->jpg_buffers.need_contiguous &&
  2180. breq->size > MAX_KMALLOC_MEM)
  2181. breq->size = MAX_KMALLOC_MEM;
  2182. down(&zr->resource_lock);
  2183. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  2184. dprintk(1,
  2185. KERN_ERR
  2186. "%s: BUZIOC_REQBUFS - buffers allready allocated\n",
  2187. ZR_DEVNAME(zr));
  2188. res = -EBUSY;
  2189. goto jpgreqbuf_unlock_and_return;
  2190. }
  2191. fh->jpg_buffers.num_buffers = breq->count;
  2192. fh->jpg_buffers.buffer_size = breq->size;
  2193. if (jpg_fbuffer_alloc(file)) {
  2194. res = -ENOMEM;
  2195. goto jpgreqbuf_unlock_and_return;
  2196. }
  2197. /* The next mmap will map the MJPEG buffers - could
  2198. * also be *_PLAY, but it doesn't matter here */
  2199. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2200. jpgreqbuf_unlock_and_return:
  2201. up(&zr->resource_lock);
  2202. return res;
  2203. }
  2204. break;
  2205. case BUZIOC_QBUF_CAPT:
  2206. {
  2207. int *frame = arg, res;
  2208. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_CAPT - frame=%d\n",
  2209. ZR_DEVNAME(zr), *frame);
  2210. down(&zr->resource_lock);
  2211. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_COMPRESS);
  2212. up(&zr->resource_lock);
  2213. return res;
  2214. }
  2215. break;
  2216. case BUZIOC_QBUF_PLAY:
  2217. {
  2218. int *frame = arg, res;
  2219. dprintk(3, KERN_DEBUG "%s: BUZIOC_QBUF_PLAY - frame=%d\n",
  2220. ZR_DEVNAME(zr), *frame);
  2221. down(&zr->resource_lock);
  2222. res = jpg_qbuf(file, *frame, BUZ_MODE_MOTION_DECOMPRESS);
  2223. up(&zr->resource_lock);
  2224. return res;
  2225. }
  2226. break;
  2227. case BUZIOC_SYNC:
  2228. {
  2229. struct zoran_sync *bsync = arg;
  2230. int res;
  2231. dprintk(3, KERN_DEBUG "%s: BUZIOC_SYNC\n", ZR_DEVNAME(zr));
  2232. down(&zr->resource_lock);
  2233. res = jpg_sync(file, bsync);
  2234. up(&zr->resource_lock);
  2235. return res;
  2236. }
  2237. break;
  2238. case BUZIOC_G_STATUS:
  2239. {
  2240. struct zoran_status *bstat = arg;
  2241. int norm, input, status, res = 0;
  2242. dprintk(3, KERN_DEBUG "%s: BUZIOC_G_STATUS\n", ZR_DEVNAME(zr));
  2243. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2244. dprintk(1,
  2245. KERN_ERR
  2246. "%s: BUZIOC_G_STATUS called but Buz in capture/playback mode\n",
  2247. ZR_DEVNAME(zr));
  2248. return -EINVAL;
  2249. }
  2250. input = zr->card.input[bstat->input].muxsel;
  2251. norm = VIDEO_MODE_AUTO;
  2252. down(&zr->resource_lock);
  2253. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2254. dprintk(1,
  2255. KERN_ERR
  2256. "%s: BUZIOC_G_STATUS called, but Buz in capture/playback mode\n",
  2257. ZR_DEVNAME(zr));
  2258. res = -EINVAL;
  2259. goto gstat_unlock_and_return;
  2260. }
  2261. decoder_command(zr, DECODER_SET_INPUT, &input);
  2262. decoder_command(zr, DECODER_SET_NORM, &norm);
  2263. /* sleep 1 second */
  2264. current->state = TASK_UNINTERRUPTIBLE;
  2265. schedule_timeout(1 * HZ);
  2266. /* Get status of video decoder */
  2267. decoder_command(zr, DECODER_GET_STATUS, &status);
  2268. /* restore previous input and norm */
  2269. input = zr->card.input[zr->input].muxsel;
  2270. decoder_command(zr, DECODER_SET_INPUT, &input);
  2271. decoder_command(zr, DECODER_SET_NORM, &zr->norm);
  2272. gstat_unlock_and_return:
  2273. up(&zr->resource_lock);
  2274. if (!res) {
  2275. bstat->signal =
  2276. (status & DECODER_STATUS_GOOD) ? 1 : 0;
  2277. if (status & DECODER_STATUS_NTSC)
  2278. bstat->norm = VIDEO_MODE_NTSC;
  2279. else if (status & DECODER_STATUS_SECAM)
  2280. bstat->norm = VIDEO_MODE_SECAM;
  2281. else
  2282. bstat->norm = VIDEO_MODE_PAL;
  2283. bstat->color =
  2284. (status & DECODER_STATUS_COLOR) ? 1 : 0;
  2285. }
  2286. return res;
  2287. }
  2288. break;
  2289. #ifdef HAVE_V4L2
  2290. /* The new video4linux2 capture interface - much nicer than video4linux1, since
  2291. * it allows for integrating the JPEG capturing calls inside standard v4l2
  2292. */
  2293. case VIDIOC_QUERYCAP:
  2294. {
  2295. struct v4l2_capability *cap = arg;
  2296. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCAP\n", ZR_DEVNAME(zr));
  2297. memset(cap, 0, sizeof(*cap));
  2298. strncpy(cap->card, ZR_DEVNAME(zr), sizeof(cap->card));
  2299. strncpy(cap->driver, "zoran", sizeof(cap->driver));
  2300. snprintf(cap->bus_info, sizeof(cap->bus_info), "PCI:%s",
  2301. zr->pci_dev->slot_name);
  2302. cap->version =
  2303. KERNEL_VERSION(MAJOR_VERSION, MINOR_VERSION,
  2304. RELEASE_VERSION);
  2305. cap->capabilities = ZORAN_V4L2_VID_FLAGS;
  2306. return 0;
  2307. }
  2308. break;
  2309. case VIDIOC_ENUM_FMT:
  2310. {
  2311. struct v4l2_fmtdesc *fmt = arg;
  2312. int index = fmt->index, num = -1, i, flag = 0, type =
  2313. fmt->type;
  2314. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUM_FMT - index=%d\n",
  2315. ZR_DEVNAME(zr), fmt->index);
  2316. switch (fmt->type) {
  2317. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2318. flag = ZORAN_FORMAT_CAPTURE;
  2319. break;
  2320. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2321. flag = ZORAN_FORMAT_PLAYBACK;
  2322. break;
  2323. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2324. flag = ZORAN_FORMAT_OVERLAY;
  2325. break;
  2326. default:
  2327. dprintk(1,
  2328. KERN_ERR
  2329. "%s: VIDIOC_ENUM_FMT - unknown type %d\n",
  2330. ZR_DEVNAME(zr), fmt->type);
  2331. return -EINVAL;
  2332. }
  2333. for (i = 0; i < zoran_num_formats; i++) {
  2334. if (zoran_formats[i].flags & flag)
  2335. num++;
  2336. if (num == fmt->index)
  2337. break;
  2338. }
  2339. if (fmt->index < 0 /* late, but not too late */ ||
  2340. i == zoran_num_formats)
  2341. return -EINVAL;
  2342. memset(fmt, 0, sizeof(*fmt));
  2343. fmt->index = index;
  2344. fmt->type = type;
  2345. strncpy(fmt->description, zoran_formats[i].name, 31);
  2346. fmt->pixelformat = zoran_formats[i].fourcc;
  2347. if (zoran_formats[i].flags & ZORAN_FORMAT_COMPRESSED)
  2348. fmt->flags |= V4L2_FMT_FLAG_COMPRESSED;
  2349. return 0;
  2350. }
  2351. break;
  2352. case VIDIOC_G_FMT:
  2353. {
  2354. struct v4l2_format *fmt = arg;
  2355. int type = fmt->type;
  2356. dprintk(5, KERN_DEBUG "%s: VIDIOC_G_FMT\n", ZR_DEVNAME(zr));
  2357. memset(fmt, 0, sizeof(*fmt));
  2358. fmt->type = type;
  2359. switch (fmt->type) {
  2360. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2361. down(&zr->resource_lock);
  2362. fmt->fmt.win.w.left = fh->overlay_settings.x;
  2363. fmt->fmt.win.w.top = fh->overlay_settings.y;
  2364. fmt->fmt.win.w.width = fh->overlay_settings.width;
  2365. fmt->fmt.win.w.height =
  2366. fh->overlay_settings.height;
  2367. if (fh->overlay_settings.width * 2 >
  2368. BUZ_MAX_HEIGHT)
  2369. fmt->fmt.win.field = V4L2_FIELD_INTERLACED;
  2370. else
  2371. fmt->fmt.win.field = V4L2_FIELD_TOP;
  2372. up(&zr->resource_lock);
  2373. break;
  2374. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2375. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2376. down(&zr->resource_lock);
  2377. if (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2378. fh->map_mode == ZORAN_MAP_MODE_RAW) {
  2379. fmt->fmt.pix.width =
  2380. fh->v4l_settings.width;
  2381. fmt->fmt.pix.height =
  2382. fh->v4l_settings.height;
  2383. fmt->fmt.pix.sizeimage =
  2384. fh->v4l_buffers.buffer_size;
  2385. fmt->fmt.pix.pixelformat =
  2386. fh->v4l_settings.format->fourcc;
  2387. fmt->fmt.pix.colorspace =
  2388. fh->v4l_settings.format->colorspace;
  2389. fmt->fmt.pix.bytesperline = 0;
  2390. if (BUZ_MAX_HEIGHT <
  2391. (fh->v4l_settings.height * 2))
  2392. fmt->fmt.pix.field =
  2393. V4L2_FIELD_INTERLACED;
  2394. else
  2395. fmt->fmt.pix.field =
  2396. V4L2_FIELD_TOP;
  2397. } else {
  2398. fmt->fmt.pix.width =
  2399. fh->jpg_settings.img_width /
  2400. fh->jpg_settings.HorDcm;
  2401. fmt->fmt.pix.height =
  2402. fh->jpg_settings.img_height /
  2403. (fh->jpg_settings.VerDcm *
  2404. fh->jpg_settings.TmpDcm);
  2405. fmt->fmt.pix.sizeimage =
  2406. zoran_v4l2_calc_bufsize(&fh->
  2407. jpg_settings);;
  2408. fmt->fmt.pix.pixelformat =
  2409. V4L2_PIX_FMT_MJPEG;
  2410. if (fh->jpg_settings.TmpDcm == 1)
  2411. fmt->fmt.pix.field =
  2412. (fh->jpg_settings.
  2413. odd_even ? V4L2_FIELD_SEQ_BT :
  2414. V4L2_FIELD_SEQ_BT);
  2415. else
  2416. fmt->fmt.pix.field =
  2417. (fh->jpg_settings.
  2418. odd_even ? V4L2_FIELD_TOP :
  2419. V4L2_FIELD_BOTTOM);
  2420. fmt->fmt.pix.bytesperline = 0;
  2421. fmt->fmt.pix.colorspace =
  2422. V4L2_COLORSPACE_SMPTE170M;
  2423. }
  2424. up(&zr->resource_lock);
  2425. break;
  2426. default:
  2427. dprintk(1,
  2428. KERN_ERR
  2429. "%s: VIDIOC_G_FMT - unsupported type %d\n",
  2430. ZR_DEVNAME(zr), fmt->type);
  2431. return -EINVAL;
  2432. }
  2433. return 0;
  2434. }
  2435. break;
  2436. case VIDIOC_S_FMT:
  2437. {
  2438. struct v4l2_format *fmt = arg;
  2439. int i, res = 0;
  2440. __u32 printformat;
  2441. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_FMT - type=%d, ",
  2442. ZR_DEVNAME(zr), fmt->type);
  2443. switch (fmt->type) {
  2444. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  2445. dprintk(3, "x=%d, y=%d, w=%d, h=%d, cnt=%d, map=0x%p\n",
  2446. fmt->fmt.win.w.left, fmt->fmt.win.w.top,
  2447. fmt->fmt.win.w.width,
  2448. fmt->fmt.win.w.height,
  2449. fmt->fmt.win.clipcount,
  2450. fmt->fmt.win.bitmap);
  2451. down(&zr->resource_lock);
  2452. res =
  2453. setup_window(file, fmt->fmt.win.w.left,
  2454. fmt->fmt.win.w.top,
  2455. fmt->fmt.win.w.width,
  2456. fmt->fmt.win.w.height,
  2457. (struct video_clip *)
  2458. fmt->fmt.win.clips,
  2459. fmt->fmt.win.clipcount,
  2460. fmt->fmt.win.bitmap);
  2461. up(&zr->resource_lock);
  2462. return res;
  2463. break;
  2464. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2465. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  2466. printformat =
  2467. __cpu_to_le32(fmt->fmt.pix.pixelformat);
  2468. dprintk(3, "size=%dx%d, fmt=0x%x (%4.4s)\n",
  2469. fmt->fmt.pix.width, fmt->fmt.pix.height,
  2470. fmt->fmt.pix.pixelformat,
  2471. (char *) &printformat);
  2472. if (fmt->fmt.pix.bytesperline > 0) {
  2473. dprintk(5,
  2474. KERN_ERR "%s: bpl not supported\n",
  2475. ZR_DEVNAME(zr));
  2476. return -EINVAL;
  2477. }
  2478. /* we can be requested to do JPEG/raw playback/capture */
  2479. if (!
  2480. (fmt->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  2481. (fmt->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  2482. fmt->fmt.pix.pixelformat ==
  2483. V4L2_PIX_FMT_MJPEG))) {
  2484. dprintk(1,
  2485. KERN_ERR
  2486. "%s: VIDIOC_S_FMT - unknown type %d/0x%x(%4.4s) combination\n",
  2487. ZR_DEVNAME(zr), fmt->type,
  2488. fmt->fmt.pix.pixelformat,
  2489. (char *) &printformat);
  2490. return -EINVAL;
  2491. }
  2492. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  2493. struct zoran_jpg_settings settings;
  2494. down(&zr->resource_lock);
  2495. settings = fh->jpg_settings;
  2496. if (fh->v4l_buffers.allocated ||
  2497. fh->jpg_buffers.allocated) {
  2498. dprintk(1,
  2499. KERN_ERR
  2500. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2501. ZR_DEVNAME(zr));
  2502. res = -EBUSY;
  2503. goto sfmtjpg_unlock_and_return;
  2504. }
  2505. /* we actually need to set 'real' parameters now */
  2506. if ((fmt->fmt.pix.height * 2) >
  2507. BUZ_MAX_HEIGHT)
  2508. settings.TmpDcm = 1;
  2509. else
  2510. settings.TmpDcm = 2;
  2511. settings.decimation = 0;
  2512. if (fmt->fmt.pix.height <=
  2513. fh->jpg_settings.img_height / 2)
  2514. settings.VerDcm = 2;
  2515. else
  2516. settings.VerDcm = 1;
  2517. if (fmt->fmt.pix.width <=
  2518. fh->jpg_settings.img_width / 4)
  2519. settings.HorDcm = 4;
  2520. else if (fmt->fmt.pix.width <=
  2521. fh->jpg_settings.img_width / 2)
  2522. settings.HorDcm = 2;
  2523. else
  2524. settings.HorDcm = 1;
  2525. if (settings.TmpDcm == 1)
  2526. settings.field_per_buff = 2;
  2527. else
  2528. settings.field_per_buff = 1;
  2529. /* check */
  2530. if ((res =
  2531. zoran_check_jpg_settings(zr,
  2532. &settings)))
  2533. goto sfmtjpg_unlock_and_return;
  2534. /* it's ok, so set them */
  2535. fh->jpg_settings = settings;
  2536. /* tell the user what we actually did */
  2537. fmt->fmt.pix.width =
  2538. settings.img_width / settings.HorDcm;
  2539. fmt->fmt.pix.height =
  2540. settings.img_height * 2 /
  2541. (settings.TmpDcm * settings.VerDcm);
  2542. if (settings.TmpDcm == 1)
  2543. fmt->fmt.pix.field =
  2544. (fh->jpg_settings.
  2545. odd_even ? V4L2_FIELD_SEQ_TB :
  2546. V4L2_FIELD_SEQ_BT);
  2547. else
  2548. fmt->fmt.pix.field =
  2549. (fh->jpg_settings.
  2550. odd_even ? V4L2_FIELD_TOP :
  2551. V4L2_FIELD_BOTTOM);
  2552. fh->jpg_buffers.buffer_size =
  2553. zoran_v4l2_calc_bufsize(&fh->
  2554. jpg_settings);
  2555. fmt->fmt.pix.sizeimage =
  2556. fh->jpg_buffers.buffer_size;
  2557. /* we hereby abuse this variable to show that
  2558. * we're gonna do mjpeg capture */
  2559. fh->map_mode =
  2560. (fmt->type ==
  2561. V4L2_BUF_TYPE_VIDEO_CAPTURE) ?
  2562. ZORAN_MAP_MODE_JPG_REC :
  2563. ZORAN_MAP_MODE_JPG_PLAY;
  2564. sfmtjpg_unlock_and_return:
  2565. up(&zr->resource_lock);
  2566. } else {
  2567. for (i = 0; i < zoran_num_formats; i++)
  2568. if (fmt->fmt.pix.pixelformat ==
  2569. zoran_formats[i].fourcc)
  2570. break;
  2571. if (i == zoran_num_formats) {
  2572. dprintk(1,
  2573. KERN_ERR
  2574. "%s: VIDIOC_S_FMT - unknown/unsupported format 0x%x (%4.4s)\n",
  2575. ZR_DEVNAME(zr),
  2576. fmt->fmt.pix.pixelformat,
  2577. (char *) &printformat);
  2578. return -EINVAL;
  2579. }
  2580. down(&zr->resource_lock);
  2581. if (fh->jpg_buffers.allocated ||
  2582. (fh->v4l_buffers.allocated &&
  2583. fh->v4l_buffers.active !=
  2584. ZORAN_FREE)) {
  2585. dprintk(1,
  2586. KERN_ERR
  2587. "%s: VIDIOC_S_FMT - cannot change capture mode\n",
  2588. ZR_DEVNAME(zr));
  2589. res = -EBUSY;
  2590. goto sfmtv4l_unlock_and_return;
  2591. }
  2592. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  2593. fmt->fmt.pix.height =
  2594. BUZ_MAX_HEIGHT;
  2595. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  2596. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  2597. if ((res =
  2598. zoran_v4l_set_format(file,
  2599. fmt->fmt.pix.
  2600. width,
  2601. fmt->fmt.pix.
  2602. height,
  2603. &zoran_formats
  2604. [i])))
  2605. goto sfmtv4l_unlock_and_return;
  2606. /* tell the user the
  2607. * results/missing stuff */
  2608. fmt->fmt.pix.sizeimage = fh->v4l_buffers.buffer_size /*zr->gbpl * zr->gheight */
  2609. ;
  2610. if (BUZ_MAX_HEIGHT <
  2611. (fh->v4l_settings.height * 2))
  2612. fmt->fmt.pix.field =
  2613. V4L2_FIELD_INTERLACED;
  2614. else
  2615. fmt->fmt.pix.field =
  2616. V4L2_FIELD_TOP;
  2617. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2618. sfmtv4l_unlock_and_return:
  2619. up(&zr->resource_lock);
  2620. }
  2621. break;
  2622. default:
  2623. dprintk(3, "unsupported\n");
  2624. dprintk(1,
  2625. KERN_ERR
  2626. "%s: VIDIOC_S_FMT - unsupported type %d\n",
  2627. ZR_DEVNAME(zr), fmt->type);
  2628. return -EINVAL;
  2629. }
  2630. return res;
  2631. }
  2632. break;
  2633. case VIDIOC_G_FBUF:
  2634. {
  2635. struct v4l2_framebuffer *fb = arg;
  2636. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_FBUF\n", ZR_DEVNAME(zr));
  2637. memset(fb, 0, sizeof(*fb));
  2638. down(&zr->resource_lock);
  2639. fb->base = zr->buffer.base;
  2640. fb->fmt.width = zr->buffer.width;
  2641. fb->fmt.height = zr->buffer.height;
  2642. if (zr->overlay_settings.format) {
  2643. fb->fmt.pixelformat =
  2644. fh->overlay_settings.format->fourcc;
  2645. }
  2646. fb->fmt.bytesperline = zr->buffer.bytesperline;
  2647. up(&zr->resource_lock);
  2648. fb->fmt.colorspace = V4L2_COLORSPACE_SRGB;
  2649. fb->fmt.field = V4L2_FIELD_INTERLACED;
  2650. fb->flags = V4L2_FBUF_FLAG_OVERLAY;
  2651. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2652. return 0;
  2653. }
  2654. break;
  2655. case VIDIOC_S_FBUF:
  2656. {
  2657. int i, res = 0;
  2658. struct v4l2_framebuffer *fb = arg;
  2659. __u32 printformat = __cpu_to_le32(fb->fmt.pixelformat);
  2660. dprintk(3,
  2661. KERN_DEBUG
  2662. "%s: VIDIOC_S_FBUF - base=0x%p, size=%dx%d, bpl=%d, fmt=0x%x (%4.4s)\n",
  2663. ZR_DEVNAME(zr), fb->base, fb->fmt.width, fb->fmt.height,
  2664. fb->fmt.bytesperline, fb->fmt.pixelformat,
  2665. (char *) &printformat);
  2666. for (i = 0; i < zoran_num_formats; i++)
  2667. if (zoran_formats[i].fourcc == fb->fmt.pixelformat)
  2668. break;
  2669. if (i == zoran_num_formats) {
  2670. dprintk(1,
  2671. KERN_ERR
  2672. "%s: VIDIOC_S_FBUF - format=0x%x (%4.4s) not allowed\n",
  2673. ZR_DEVNAME(zr), fb->fmt.pixelformat,
  2674. (char *) &printformat);
  2675. return -EINVAL;
  2676. }
  2677. down(&zr->resource_lock);
  2678. res =
  2679. setup_fbuffer(file, fb->base, &zoran_formats[i],
  2680. fb->fmt.width, fb->fmt.height,
  2681. fb->fmt.bytesperline);
  2682. up(&zr->resource_lock);
  2683. return res;
  2684. }
  2685. break;
  2686. case VIDIOC_OVERLAY:
  2687. {
  2688. int *on = arg, res;
  2689. dprintk(3, KERN_DEBUG "%s: VIDIOC_PREVIEW - on=%d\n",
  2690. ZR_DEVNAME(zr), *on);
  2691. down(&zr->resource_lock);
  2692. res = setup_overlay(file, *on);
  2693. up(&zr->resource_lock);
  2694. return res;
  2695. }
  2696. break;
  2697. case VIDIOC_REQBUFS:
  2698. {
  2699. struct v4l2_requestbuffers *req = arg;
  2700. int res = 0;
  2701. dprintk(3, KERN_DEBUG "%s: VIDIOC_REQBUFS - type=%d\n",
  2702. ZR_DEVNAME(zr), req->type);
  2703. if (req->memory != V4L2_MEMORY_MMAP) {
  2704. dprintk(1,
  2705. KERN_ERR
  2706. "%s: only MEMORY_MMAP capture is supported, not %d\n",
  2707. ZR_DEVNAME(zr), req->memory);
  2708. return -EINVAL;
  2709. }
  2710. down(&zr->resource_lock);
  2711. if (fh->v4l_buffers.allocated || fh->jpg_buffers.allocated) {
  2712. dprintk(1,
  2713. KERN_ERR
  2714. "%s: VIDIOC_REQBUFS - buffers allready allocated\n",
  2715. ZR_DEVNAME(zr));
  2716. res = -EBUSY;
  2717. goto v4l2reqbuf_unlock_and_return;
  2718. }
  2719. if (fh->map_mode == ZORAN_MAP_MODE_RAW &&
  2720. req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2721. /* control user input */
  2722. if (req->count < 2)
  2723. req->count = 2;
  2724. if (req->count > v4l_nbufs)
  2725. req->count = v4l_nbufs;
  2726. fh->v4l_buffers.num_buffers = req->count;
  2727. if (v4l_fbuffer_alloc(file)) {
  2728. res = -ENOMEM;
  2729. goto v4l2reqbuf_unlock_and_return;
  2730. }
  2731. /* The next mmap will map the V4L buffers */
  2732. fh->map_mode = ZORAN_MAP_MODE_RAW;
  2733. } else if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC ||
  2734. fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2735. /* we need to calculate size ourselves now */
  2736. if (req->count < 4)
  2737. req->count = 4;
  2738. if (req->count > jpg_nbufs)
  2739. req->count = jpg_nbufs;
  2740. fh->jpg_buffers.num_buffers = req->count;
  2741. fh->jpg_buffers.buffer_size =
  2742. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  2743. if (jpg_fbuffer_alloc(file)) {
  2744. res = -ENOMEM;
  2745. goto v4l2reqbuf_unlock_and_return;
  2746. }
  2747. /* The next mmap will map the MJPEG buffers */
  2748. if (req->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  2749. fh->map_mode = ZORAN_MAP_MODE_JPG_REC;
  2750. else
  2751. fh->map_mode = ZORAN_MAP_MODE_JPG_PLAY;
  2752. } else {
  2753. dprintk(1,
  2754. KERN_ERR
  2755. "%s: VIDIOC_REQBUFS - unknown type %d\n",
  2756. ZR_DEVNAME(zr), req->type);
  2757. res = -EINVAL;
  2758. goto v4l2reqbuf_unlock_and_return;
  2759. }
  2760. v4l2reqbuf_unlock_and_return:
  2761. up(&zr->resource_lock);
  2762. return 0;
  2763. }
  2764. break;
  2765. case VIDIOC_QUERYBUF:
  2766. {
  2767. struct v4l2_buffer *buf = arg;
  2768. __u32 type = buf->type;
  2769. int index = buf->index, res;
  2770. dprintk(3,
  2771. KERN_DEBUG
  2772. "%s: VIDIOC_QUERYBUF - index=%d, type=%d\n",
  2773. ZR_DEVNAME(zr), buf->index, buf->type);
  2774. memset(buf, 0, sizeof(buf));
  2775. buf->type = type;
  2776. buf->index = index;
  2777. down(&zr->resource_lock);
  2778. res = zoran_v4l2_buffer_status(file, buf, buf->index);
  2779. up(&zr->resource_lock);
  2780. return res;
  2781. }
  2782. break;
  2783. case VIDIOC_QBUF:
  2784. {
  2785. struct v4l2_buffer *buf = arg;
  2786. int res = 0, codec_mode, buf_type;
  2787. dprintk(3,
  2788. KERN_DEBUG "%s: VIDIOC_QBUF - type=%d, index=%d\n",
  2789. ZR_DEVNAME(zr), buf->type, buf->index);
  2790. down(&zr->resource_lock);
  2791. switch (fh->map_mode) {
  2792. case ZORAN_MAP_MODE_RAW:
  2793. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2794. dprintk(1,
  2795. KERN_ERR
  2796. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2797. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2798. res = -EINVAL;
  2799. goto qbuf_unlock_and_return;
  2800. }
  2801. res = zoran_v4l_queue_frame(file, buf->index);
  2802. if (res)
  2803. goto qbuf_unlock_and_return;
  2804. if (!zr->v4l_memgrab_active &&
  2805. fh->v4l_buffers.active == ZORAN_LOCKED)
  2806. zr36057_set_memgrab(zr, 1);
  2807. break;
  2808. case ZORAN_MAP_MODE_JPG_REC:
  2809. case ZORAN_MAP_MODE_JPG_PLAY:
  2810. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY) {
  2811. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2812. codec_mode = BUZ_MODE_MOTION_DECOMPRESS;
  2813. } else {
  2814. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2815. codec_mode = BUZ_MODE_MOTION_COMPRESS;
  2816. }
  2817. if (buf->type != buf_type) {
  2818. dprintk(1,
  2819. KERN_ERR
  2820. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2821. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2822. res = -EINVAL;
  2823. goto qbuf_unlock_and_return;
  2824. }
  2825. res =
  2826. zoran_jpg_queue_frame(file, buf->index,
  2827. codec_mode);
  2828. if (res != 0)
  2829. goto qbuf_unlock_and_return;
  2830. if (zr->codec_mode == BUZ_MODE_IDLE &&
  2831. fh->jpg_buffers.active == ZORAN_LOCKED) {
  2832. zr36057_enable_jpg(zr, codec_mode);
  2833. }
  2834. break;
  2835. default:
  2836. dprintk(1,
  2837. KERN_ERR
  2838. "%s: VIDIOC_QBUF - unsupported type %d\n",
  2839. ZR_DEVNAME(zr), buf->type);
  2840. res = -EINVAL;
  2841. goto qbuf_unlock_and_return;
  2842. }
  2843. qbuf_unlock_and_return:
  2844. up(&zr->resource_lock);
  2845. return res;
  2846. }
  2847. break;
  2848. case VIDIOC_DQBUF:
  2849. {
  2850. struct v4l2_buffer *buf = arg;
  2851. int res = 0, buf_type, num = -1; /* compiler borks here (?) */
  2852. dprintk(3, KERN_DEBUG "%s: VIDIOC_DQBUF - type=%d\n",
  2853. ZR_DEVNAME(zr), buf->type);
  2854. down(&zr->resource_lock);
  2855. switch (fh->map_mode) {
  2856. case ZORAN_MAP_MODE_RAW:
  2857. if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  2858. dprintk(1,
  2859. KERN_ERR
  2860. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2861. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2862. res = -EINVAL;
  2863. goto dqbuf_unlock_and_return;
  2864. }
  2865. num = zr->v4l_pend[zr->v4l_sync_tail & V4L_MASK_FRAME];
  2866. if (file->f_flags & O_NONBLOCK &&
  2867. zr->v4l_buffers.buffer[num].state !=
  2868. BUZ_STATE_DONE) {
  2869. res = -EAGAIN;
  2870. goto dqbuf_unlock_and_return;
  2871. }
  2872. res = v4l_sync(file, num);
  2873. if (res)
  2874. goto dqbuf_unlock_and_return;
  2875. else
  2876. zr->v4l_sync_tail++;
  2877. res = zoran_v4l2_buffer_status(file, buf, num);
  2878. break;
  2879. case ZORAN_MAP_MODE_JPG_REC:
  2880. case ZORAN_MAP_MODE_JPG_PLAY:
  2881. {
  2882. struct zoran_sync bs;
  2883. if (fh->map_mode == ZORAN_MAP_MODE_JPG_PLAY)
  2884. buf_type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
  2885. else
  2886. buf_type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2887. if (buf->type != buf_type) {
  2888. dprintk(1,
  2889. KERN_ERR
  2890. "%s: VIDIOC_QBUF - invalid buf->type=%d for map_mode=%d\n",
  2891. ZR_DEVNAME(zr), buf->type, fh->map_mode);
  2892. res = -EINVAL;
  2893. goto dqbuf_unlock_and_return;
  2894. }
  2895. num =
  2896. zr->jpg_pend[zr->
  2897. jpg_que_tail & BUZ_MASK_FRAME];
  2898. if (file->f_flags & O_NONBLOCK &&
  2899. zr->jpg_buffers.buffer[num].state !=
  2900. BUZ_STATE_DONE) {
  2901. res = -EAGAIN;
  2902. goto dqbuf_unlock_and_return;
  2903. }
  2904. res = jpg_sync(file, &bs);
  2905. if (res)
  2906. goto dqbuf_unlock_and_return;
  2907. res =
  2908. zoran_v4l2_buffer_status(file, buf, bs.frame);
  2909. break;
  2910. }
  2911. default:
  2912. dprintk(1,
  2913. KERN_ERR
  2914. "%s: VIDIOC_DQBUF - unsupported type %d\n",
  2915. ZR_DEVNAME(zr), buf->type);
  2916. res = -EINVAL;
  2917. goto dqbuf_unlock_and_return;
  2918. }
  2919. dqbuf_unlock_and_return:
  2920. up(&zr->resource_lock);
  2921. return res;
  2922. }
  2923. break;
  2924. case VIDIOC_STREAMON:
  2925. {
  2926. int res = 0;
  2927. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMON\n", ZR_DEVNAME(zr));
  2928. down(&zr->resource_lock);
  2929. switch (fh->map_mode) {
  2930. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2931. if (zr->v4l_buffers.active != ZORAN_ACTIVE ||
  2932. fh->v4l_buffers.active != ZORAN_ACTIVE) {
  2933. res = -EBUSY;
  2934. goto strmon_unlock_and_return;
  2935. }
  2936. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2937. ZORAN_LOCKED;
  2938. zr->v4l_settings = fh->v4l_settings;
  2939. zr->v4l_sync_tail = zr->v4l_pend_tail;
  2940. if (!zr->v4l_memgrab_active &&
  2941. zr->v4l_pend_head != zr->v4l_pend_tail) {
  2942. zr36057_set_memgrab(zr, 1);
  2943. }
  2944. break;
  2945. case ZORAN_MAP_MODE_JPG_REC:
  2946. case ZORAN_MAP_MODE_JPG_PLAY:
  2947. /* what is the codec mode right now? */
  2948. if (zr->jpg_buffers.active != ZORAN_ACTIVE ||
  2949. fh->jpg_buffers.active != ZORAN_ACTIVE) {
  2950. res = -EBUSY;
  2951. goto strmon_unlock_and_return;
  2952. }
  2953. zr->jpg_buffers.active = fh->jpg_buffers.active =
  2954. ZORAN_LOCKED;
  2955. if (zr->jpg_que_head != zr->jpg_que_tail) {
  2956. /* Start the jpeg codec when the first frame is queued */
  2957. jpeg_start(zr);
  2958. }
  2959. break;
  2960. default:
  2961. dprintk(1,
  2962. KERN_ERR
  2963. "%s: VIDIOC_STREAMON - invalid map mode %d\n",
  2964. ZR_DEVNAME(zr), fh->map_mode);
  2965. res = -EINVAL;
  2966. goto strmon_unlock_and_return;
  2967. }
  2968. strmon_unlock_and_return:
  2969. up(&zr->resource_lock);
  2970. return res;
  2971. }
  2972. break;
  2973. case VIDIOC_STREAMOFF:
  2974. {
  2975. int i, res = 0;
  2976. dprintk(3, KERN_DEBUG "%s: VIDIOC_STREAMOFF\n", ZR_DEVNAME(zr));
  2977. down(&zr->resource_lock);
  2978. switch (fh->map_mode) {
  2979. case ZORAN_MAP_MODE_RAW: /* raw capture */
  2980. if (fh->v4l_buffers.active == ZORAN_FREE &&
  2981. zr->v4l_buffers.active != ZORAN_FREE) {
  2982. res = -EPERM; /* stay off other's settings! */
  2983. goto strmoff_unlock_and_return;
  2984. }
  2985. if (zr->v4l_buffers.active == ZORAN_FREE)
  2986. goto strmoff_unlock_and_return;
  2987. /* unload capture */
  2988. if (zr->v4l_memgrab_active)
  2989. zr36057_set_memgrab(zr, 0);
  2990. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  2991. zr->v4l_buffers.buffer[i].state =
  2992. BUZ_STATE_USER;
  2993. fh->v4l_buffers = zr->v4l_buffers;
  2994. zr->v4l_buffers.active = fh->v4l_buffers.active =
  2995. ZORAN_FREE;
  2996. zr->v4l_grab_seq = 0;
  2997. zr->v4l_pend_head = zr->v4l_pend_tail = 0;
  2998. zr->v4l_sync_tail = 0;
  2999. break;
  3000. case ZORAN_MAP_MODE_JPG_REC:
  3001. case ZORAN_MAP_MODE_JPG_PLAY:
  3002. if (fh->jpg_buffers.active == ZORAN_FREE &&
  3003. zr->jpg_buffers.active != ZORAN_FREE) {
  3004. res = -EPERM; /* stay off other's settings! */
  3005. goto strmoff_unlock_and_return;
  3006. }
  3007. if (zr->jpg_buffers.active == ZORAN_FREE)
  3008. goto strmoff_unlock_and_return;
  3009. res =
  3010. jpg_qbuf(file, -1,
  3011. (fh->map_mode ==
  3012. ZORAN_MAP_MODE_JPG_REC) ?
  3013. BUZ_MODE_MOTION_COMPRESS :
  3014. BUZ_MODE_MOTION_DECOMPRESS);
  3015. if (res)
  3016. goto strmoff_unlock_and_return;
  3017. break;
  3018. default:
  3019. dprintk(1,
  3020. KERN_ERR
  3021. "%s: VIDIOC_STREAMOFF - invalid map mode %d\n",
  3022. ZR_DEVNAME(zr), fh->map_mode);
  3023. res = -EINVAL;
  3024. goto strmoff_unlock_and_return;
  3025. }
  3026. strmoff_unlock_and_return:
  3027. up(&zr->resource_lock);
  3028. return res;
  3029. }
  3030. break;
  3031. case VIDIOC_QUERYCTRL:
  3032. {
  3033. struct v4l2_queryctrl *ctrl = arg;
  3034. dprintk(3, KERN_DEBUG "%s: VIDIOC_QUERYCTRL - id=%d\n",
  3035. ZR_DEVNAME(zr), ctrl->id);
  3036. /* we only support hue/saturation/contrast/brightness */
  3037. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3038. ctrl->id > V4L2_CID_HUE)
  3039. return -EINVAL;
  3040. else {
  3041. int id = ctrl->id;
  3042. memset(ctrl, 0, sizeof(*ctrl));
  3043. ctrl->id = id;
  3044. }
  3045. switch (ctrl->id) {
  3046. case V4L2_CID_BRIGHTNESS:
  3047. strncpy(ctrl->name, "Brightness", 31);
  3048. break;
  3049. case V4L2_CID_CONTRAST:
  3050. strncpy(ctrl->name, "Contrast", 31);
  3051. break;
  3052. case V4L2_CID_SATURATION:
  3053. strncpy(ctrl->name, "Saturation", 31);
  3054. break;
  3055. case V4L2_CID_HUE:
  3056. strncpy(ctrl->name, "Hue", 31);
  3057. break;
  3058. }
  3059. ctrl->minimum = 0;
  3060. ctrl->maximum = 65535;
  3061. ctrl->step = 1;
  3062. ctrl->default_value = 32768;
  3063. ctrl->type = V4L2_CTRL_TYPE_INTEGER;
  3064. return 0;
  3065. }
  3066. break;
  3067. case VIDIOC_G_CTRL:
  3068. {
  3069. struct v4l2_control *ctrl = arg;
  3070. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_CTRL - id=%d\n",
  3071. ZR_DEVNAME(zr), ctrl->id);
  3072. /* we only support hue/saturation/contrast/brightness */
  3073. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3074. ctrl->id > V4L2_CID_HUE)
  3075. return -EINVAL;
  3076. down(&zr->resource_lock);
  3077. switch (ctrl->id) {
  3078. case V4L2_CID_BRIGHTNESS:
  3079. ctrl->value = zr->brightness;
  3080. break;
  3081. case V4L2_CID_CONTRAST:
  3082. ctrl->value = zr->contrast;
  3083. break;
  3084. case V4L2_CID_SATURATION:
  3085. ctrl->value = zr->saturation;
  3086. break;
  3087. case V4L2_CID_HUE:
  3088. ctrl->value = zr->hue;
  3089. break;
  3090. }
  3091. up(&zr->resource_lock);
  3092. return 0;
  3093. }
  3094. break;
  3095. case VIDIOC_S_CTRL:
  3096. {
  3097. struct v4l2_control *ctrl = arg;
  3098. struct video_picture pict;
  3099. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_CTRL - id=%d\n",
  3100. ZR_DEVNAME(zr), ctrl->id);
  3101. /* we only support hue/saturation/contrast/brightness */
  3102. if (ctrl->id < V4L2_CID_BRIGHTNESS ||
  3103. ctrl->id > V4L2_CID_HUE)
  3104. return -EINVAL;
  3105. if (ctrl->value < 0 || ctrl->value > 65535) {
  3106. dprintk(1,
  3107. KERN_ERR
  3108. "%s: VIDIOC_S_CTRL - invalid value %d for id=%d\n",
  3109. ZR_DEVNAME(zr), ctrl->value, ctrl->id);
  3110. return -EINVAL;
  3111. }
  3112. down(&zr->resource_lock);
  3113. switch (ctrl->id) {
  3114. case V4L2_CID_BRIGHTNESS:
  3115. zr->brightness = ctrl->value;
  3116. break;
  3117. case V4L2_CID_CONTRAST:
  3118. zr->contrast = ctrl->value;
  3119. break;
  3120. case V4L2_CID_SATURATION:
  3121. zr->saturation = ctrl->value;
  3122. break;
  3123. case V4L2_CID_HUE:
  3124. zr->hue = ctrl->value;
  3125. break;
  3126. }
  3127. pict.brightness = zr->brightness;
  3128. pict.contrast = zr->contrast;
  3129. pict.colour = zr->saturation;
  3130. pict.hue = zr->hue;
  3131. decoder_command(zr, DECODER_SET_PICTURE, &pict);
  3132. up(&zr->resource_lock);
  3133. return 0;
  3134. }
  3135. break;
  3136. case VIDIOC_ENUMSTD:
  3137. {
  3138. struct v4l2_standard *std = arg;
  3139. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMSTD - index=%d\n",
  3140. ZR_DEVNAME(zr), std->index);
  3141. if (std->index < 0 || std->index >= (zr->card.norms + 1))
  3142. return -EINVAL;
  3143. else {
  3144. int id = std->index;
  3145. memset(std, 0, sizeof(*std));
  3146. std->index = id;
  3147. }
  3148. if (std->index == zr->card.norms) {
  3149. /* if we have autodetect, ... */
  3150. struct video_decoder_capability caps;
  3151. decoder_command(zr, DECODER_GET_CAPABILITIES,
  3152. &caps);
  3153. if (caps.flags & VIDEO_DECODER_AUTO) {
  3154. std->id = V4L2_STD_ALL;
  3155. strncpy(std->name, "Autodetect", 31);
  3156. return 0;
  3157. } else
  3158. return -EINVAL;
  3159. }
  3160. switch (std->index) {
  3161. case 0:
  3162. std->id = V4L2_STD_PAL;
  3163. strncpy(std->name, "PAL", 31);
  3164. std->frameperiod.numerator = 1;
  3165. std->frameperiod.denominator = 25;
  3166. std->framelines = zr->card.tvn[0]->Ht;
  3167. break;
  3168. case 1:
  3169. std->id = V4L2_STD_NTSC;
  3170. strncpy(std->name, "NTSC", 31);
  3171. std->frameperiod.numerator = 1001;
  3172. std->frameperiod.denominator = 30000;
  3173. std->framelines = zr->card.tvn[1]->Ht;
  3174. break;
  3175. case 2:
  3176. std->id = V4L2_STD_SECAM;
  3177. strncpy(std->name, "SECAM", 31);
  3178. std->frameperiod.numerator = 1;
  3179. std->frameperiod.denominator = 25;
  3180. std->framelines = zr->card.tvn[2]->Ht;
  3181. break;
  3182. }
  3183. return 0;
  3184. }
  3185. break;
  3186. case VIDIOC_G_STD:
  3187. {
  3188. v4l2_std_id *std = arg;
  3189. int norm;
  3190. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_STD\n", ZR_DEVNAME(zr));
  3191. down(&zr->resource_lock);
  3192. norm = zr->norm;
  3193. up(&zr->resource_lock);
  3194. switch (norm) {
  3195. case VIDEO_MODE_PAL:
  3196. *std = V4L2_STD_PAL;
  3197. break;
  3198. case VIDEO_MODE_NTSC:
  3199. *std = V4L2_STD_NTSC;
  3200. break;
  3201. case VIDEO_MODE_SECAM:
  3202. *std = V4L2_STD_SECAM;
  3203. break;
  3204. }
  3205. return 0;
  3206. }
  3207. break;
  3208. case VIDIOC_S_STD:
  3209. {
  3210. int norm = -1, res = 0;
  3211. v4l2_std_id *std = arg;
  3212. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_STD - norm=0x%llx\n",
  3213. ZR_DEVNAME(zr), *std);
  3214. if (*std == V4L2_STD_PAL)
  3215. norm = VIDEO_MODE_PAL;
  3216. else if (*std == V4L2_STD_NTSC)
  3217. norm = VIDEO_MODE_NTSC;
  3218. else if (*std == V4L2_STD_SECAM)
  3219. norm = VIDEO_MODE_SECAM;
  3220. else if (*std == V4L2_STD_ALL)
  3221. norm = VIDEO_MODE_AUTO;
  3222. else {
  3223. dprintk(1,
  3224. KERN_ERR
  3225. "%s: VIDIOC_S_STD - invalid norm 0x%llx\n",
  3226. ZR_DEVNAME(zr), *std);
  3227. return -EINVAL;
  3228. }
  3229. down(&zr->resource_lock);
  3230. if ((res = zoran_set_norm(zr, norm)))
  3231. goto sstd_unlock_and_return;
  3232. res = wait_grab_pending(zr);
  3233. sstd_unlock_and_return:
  3234. up(&zr->resource_lock);
  3235. return res;
  3236. }
  3237. break;
  3238. case VIDIOC_ENUMINPUT:
  3239. {
  3240. struct v4l2_input *inp = arg;
  3241. int status;
  3242. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMINPUT - index=%d\n",
  3243. ZR_DEVNAME(zr), inp->index);
  3244. if (inp->index < 0 || inp->index >= zr->card.inputs)
  3245. return -EINVAL;
  3246. else {
  3247. int id = inp->index;
  3248. memset(inp, 0, sizeof(*inp));
  3249. inp->index = id;
  3250. }
  3251. strncpy(inp->name, zr->card.input[inp->index].name,
  3252. sizeof(inp->name) - 1);
  3253. inp->type = V4L2_INPUT_TYPE_CAMERA;
  3254. inp->std = V4L2_STD_ALL;
  3255. /* Get status of video decoder */
  3256. down(&zr->resource_lock);
  3257. decoder_command(zr, DECODER_GET_STATUS, &status);
  3258. up(&zr->resource_lock);
  3259. if (!(status & DECODER_STATUS_GOOD)) {
  3260. inp->status |= V4L2_IN_ST_NO_POWER;
  3261. inp->status |= V4L2_IN_ST_NO_SIGNAL;
  3262. }
  3263. if (!(status & DECODER_STATUS_COLOR))
  3264. inp->status |= V4L2_IN_ST_NO_COLOR;
  3265. return 0;
  3266. }
  3267. break;
  3268. case VIDIOC_G_INPUT:
  3269. {
  3270. int *input = arg;
  3271. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_INPUT\n", ZR_DEVNAME(zr));
  3272. down(&zr->resource_lock);
  3273. *input = zr->input;
  3274. up(&zr->resource_lock);
  3275. return 0;
  3276. }
  3277. break;
  3278. case VIDIOC_S_INPUT:
  3279. {
  3280. int *input = arg, res = 0;
  3281. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_INPUT - input=%d\n",
  3282. ZR_DEVNAME(zr), *input);
  3283. down(&zr->resource_lock);
  3284. if ((res = zoran_set_input(zr, *input)))
  3285. goto sinput_unlock_and_return;
  3286. /* Make sure the changes come into effect */
  3287. res = wait_grab_pending(zr);
  3288. sinput_unlock_and_return:
  3289. up(&zr->resource_lock);
  3290. return res;
  3291. }
  3292. break;
  3293. case VIDIOC_ENUMOUTPUT:
  3294. {
  3295. struct v4l2_output *outp = arg;
  3296. dprintk(3, KERN_DEBUG "%s: VIDIOC_ENUMOUTPUT - index=%d\n",
  3297. ZR_DEVNAME(zr), outp->index);
  3298. if (outp->index != 0)
  3299. return -EINVAL;
  3300. memset(outp, 0, sizeof(*outp));
  3301. outp->index = 0;
  3302. outp->type = V4L2_OUTPUT_TYPE_ANALOGVGAOVERLAY;
  3303. strncpy(outp->name, "Autodetect", 31);
  3304. return 0;
  3305. }
  3306. break;
  3307. case VIDIOC_G_OUTPUT:
  3308. {
  3309. int *output = arg;
  3310. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_OUTPUT\n", ZR_DEVNAME(zr));
  3311. *output = 0;
  3312. return 0;
  3313. }
  3314. break;
  3315. case VIDIOC_S_OUTPUT:
  3316. {
  3317. int *output = arg;
  3318. dprintk(3, KERN_DEBUG "%s: VIDIOC_S_OUTPUT - output=%d\n",
  3319. ZR_DEVNAME(zr), *output);
  3320. if (*output != 0)
  3321. return -EINVAL;
  3322. return 0;
  3323. }
  3324. break;
  3325. /* cropping (sub-frame capture) */
  3326. case VIDIOC_CROPCAP:
  3327. {
  3328. struct v4l2_cropcap *cropcap = arg;
  3329. int type = cropcap->type, res = 0;
  3330. dprintk(3, KERN_ERR "%s: VIDIOC_CROPCAP - type=%d\n",
  3331. ZR_DEVNAME(zr), cropcap->type);
  3332. memset(cropcap, 0, sizeof(*cropcap));
  3333. cropcap->type = type;
  3334. down(&zr->resource_lock);
  3335. if (cropcap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3336. (cropcap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3337. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3338. dprintk(1,
  3339. KERN_ERR
  3340. "%s: VIDIOC_CROPCAP - subcapture only supported for compressed capture\n",
  3341. ZR_DEVNAME(zr));
  3342. res = -EINVAL;
  3343. goto cropcap_unlock_and_return;
  3344. }
  3345. cropcap->bounds.top = cropcap->bounds.left = 0;
  3346. cropcap->bounds.width = BUZ_MAX_WIDTH;
  3347. cropcap->bounds.height = BUZ_MAX_HEIGHT;
  3348. cropcap->defrect.top = cropcap->defrect.left = 0;
  3349. cropcap->defrect.width = BUZ_MIN_WIDTH;
  3350. cropcap->defrect.height = BUZ_MIN_HEIGHT;
  3351. cropcap_unlock_and_return:
  3352. up(&zr->resource_lock);
  3353. return res;
  3354. }
  3355. break;
  3356. case VIDIOC_G_CROP:
  3357. {
  3358. struct v4l2_crop *crop = arg;
  3359. int type = crop->type, res = 0;
  3360. dprintk(3, KERN_ERR "%s: VIDIOC_G_CROP - type=%d\n",
  3361. ZR_DEVNAME(zr), crop->type);
  3362. memset(crop, 0, sizeof(*crop));
  3363. crop->type = type;
  3364. down(&zr->resource_lock);
  3365. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3366. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3367. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3368. dprintk(1,
  3369. KERN_ERR
  3370. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3371. ZR_DEVNAME(zr));
  3372. res = -EINVAL;
  3373. goto gcrop_unlock_and_return;
  3374. }
  3375. crop->c.top = fh->jpg_settings.img_y;
  3376. crop->c.left = fh->jpg_settings.img_x;
  3377. crop->c.width = fh->jpg_settings.img_width;
  3378. crop->c.height = fh->jpg_settings.img_height;
  3379. gcrop_unlock_and_return:
  3380. up(&zr->resource_lock);
  3381. return res;
  3382. }
  3383. break;
  3384. case VIDIOC_S_CROP:
  3385. {
  3386. struct v4l2_crop *crop = arg;
  3387. int res = 0;
  3388. struct zoran_jpg_settings settings = fh->jpg_settings;
  3389. dprintk(3,
  3390. KERN_ERR
  3391. "%s: VIDIOC_S_CROP - type=%d, x=%d,y=%d,w=%d,h=%d\n",
  3392. ZR_DEVNAME(zr), crop->type, crop->c.left, crop->c.top,
  3393. crop->c.width, crop->c.height);
  3394. down(&zr->resource_lock);
  3395. if (fh->jpg_buffers.allocated || fh->v4l_buffers.allocated) {
  3396. dprintk(1,
  3397. KERN_ERR
  3398. "%s: VIDIOC_S_CROP - cannot change settings while active\n",
  3399. ZR_DEVNAME(zr));
  3400. res = -EBUSY;
  3401. goto scrop_unlock_and_return;
  3402. }
  3403. if (crop->type != V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  3404. (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  3405. fh->map_mode == ZORAN_MAP_MODE_RAW)) {
  3406. dprintk(1,
  3407. KERN_ERR
  3408. "%s: VIDIOC_G_CROP - subcapture only supported for compressed capture\n",
  3409. ZR_DEVNAME(zr));
  3410. res = -EINVAL;
  3411. goto scrop_unlock_and_return;
  3412. }
  3413. /* move into a form that we understand */
  3414. settings.img_x = crop->c.left;
  3415. settings.img_y = crop->c.top;
  3416. settings.img_width = crop->c.width;
  3417. settings.img_height = crop->c.height;
  3418. /* check validity */
  3419. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3420. goto scrop_unlock_and_return;
  3421. /* accept */
  3422. fh->jpg_settings = settings;
  3423. scrop_unlock_and_return:
  3424. up(&zr->resource_lock);
  3425. return res;
  3426. }
  3427. break;
  3428. case VIDIOC_G_JPEGCOMP:
  3429. {
  3430. struct v4l2_jpegcompression *params = arg;
  3431. dprintk(3, KERN_DEBUG "%s: VIDIOC_G_JPEGCOMP\n",
  3432. ZR_DEVNAME(zr));
  3433. memset(params, 0, sizeof(*params));
  3434. down(&zr->resource_lock);
  3435. params->quality = fh->jpg_settings.jpg_comp.quality;
  3436. params->APPn = fh->jpg_settings.jpg_comp.APPn;
  3437. memcpy(params->APP_data,
  3438. fh->jpg_settings.jpg_comp.APP_data,
  3439. fh->jpg_settings.jpg_comp.APP_len);
  3440. params->APP_len = fh->jpg_settings.jpg_comp.APP_len;
  3441. memcpy(params->COM_data,
  3442. fh->jpg_settings.jpg_comp.COM_data,
  3443. fh->jpg_settings.jpg_comp.COM_len);
  3444. params->COM_len = fh->jpg_settings.jpg_comp.COM_len;
  3445. params->jpeg_markers =
  3446. fh->jpg_settings.jpg_comp.jpeg_markers;
  3447. up(&zr->resource_lock);
  3448. return 0;
  3449. }
  3450. break;
  3451. case VIDIOC_S_JPEGCOMP:
  3452. {
  3453. struct v4l2_jpegcompression *params = arg;
  3454. struct zoran_jpg_settings settings = fh->jpg_settings;
  3455. int res = 0;
  3456. dprintk(3,
  3457. KERN_DEBUG
  3458. "%s: VIDIOC_S_JPEGCOMP - quality=%d, APPN=%d, APP_len=%d, COM_len=%d\n",
  3459. ZR_DEVNAME(zr), params->quality, params->APPn,
  3460. params->APP_len, params->COM_len);
  3461. settings.jpg_comp = *params;
  3462. down(&zr->resource_lock);
  3463. if (fh->v4l_buffers.active != ZORAN_FREE ||
  3464. fh->jpg_buffers.active != ZORAN_FREE) {
  3465. dprintk(1,
  3466. KERN_WARNING
  3467. "%s: VIDIOC_S_JPEGCOMP called while in playback/capture mode\n",
  3468. ZR_DEVNAME(zr));
  3469. res = -EBUSY;
  3470. goto sjpegc_unlock_and_return;
  3471. }
  3472. if ((res = zoran_check_jpg_settings(zr, &settings)))
  3473. goto sjpegc_unlock_and_return;
  3474. if (!fh->jpg_buffers.allocated)
  3475. fh->jpg_buffers.buffer_size =
  3476. zoran_v4l2_calc_bufsize(&fh->jpg_settings);
  3477. fh->jpg_settings.jpg_comp = *params = settings.jpg_comp;
  3478. sjpegc_unlock_and_return:
  3479. up(&zr->resource_lock);
  3480. return 0;
  3481. }
  3482. break;
  3483. case VIDIOC_QUERYSTD: /* why is this useful? */
  3484. {
  3485. v4l2_std_id *std = arg;
  3486. dprintk(3,
  3487. KERN_DEBUG "%s: VIDIOC_QUERY_STD - std=0x%llx\n",
  3488. ZR_DEVNAME(zr), *std);
  3489. if (*std == V4L2_STD_ALL || *std == V4L2_STD_NTSC ||
  3490. *std == V4L2_STD_PAL || (*std == V4L2_STD_SECAM &&
  3491. zr->card.norms == 3)) {
  3492. return 0;
  3493. }
  3494. return -EINVAL;
  3495. }
  3496. break;
  3497. case VIDIOC_TRY_FMT:
  3498. {
  3499. struct v4l2_format *fmt = arg;
  3500. int res = 0;
  3501. dprintk(3, KERN_DEBUG "%s: VIDIOC_TRY_FMT - type=%d\n",
  3502. ZR_DEVNAME(zr), fmt->type);
  3503. switch (fmt->type) {
  3504. case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  3505. down(&zr->resource_lock);
  3506. if (fmt->fmt.win.w.width > BUZ_MAX_WIDTH)
  3507. fmt->fmt.win.w.width = BUZ_MAX_WIDTH;
  3508. if (fmt->fmt.win.w.width < BUZ_MIN_WIDTH)
  3509. fmt->fmt.win.w.width = BUZ_MIN_WIDTH;
  3510. if (fmt->fmt.win.w.height > BUZ_MAX_HEIGHT)
  3511. fmt->fmt.win.w.height = BUZ_MAX_HEIGHT;
  3512. if (fmt->fmt.win.w.height < BUZ_MIN_HEIGHT)
  3513. fmt->fmt.win.w.height = BUZ_MIN_HEIGHT;
  3514. up(&zr->resource_lock);
  3515. break;
  3516. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  3517. case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  3518. if (fmt->fmt.pix.bytesperline > 0)
  3519. return -EINVAL;
  3520. down(&zr->resource_lock);
  3521. if (fmt->fmt.pix.pixelformat == V4L2_PIX_FMT_MJPEG) {
  3522. struct zoran_jpg_settings settings =
  3523. fh->jpg_settings;
  3524. /* we actually need to set 'real' parameters now */
  3525. if ((fmt->fmt.pix.height * 2) >
  3526. BUZ_MAX_HEIGHT)
  3527. settings.TmpDcm = 1;
  3528. else
  3529. settings.TmpDcm = 2;
  3530. settings.decimation = 0;
  3531. if (fmt->fmt.pix.height <=
  3532. fh->jpg_settings.img_height / 2)
  3533. settings.VerDcm = 2;
  3534. else
  3535. settings.VerDcm = 1;
  3536. if (fmt->fmt.pix.width <=
  3537. fh->jpg_settings.img_width / 4)
  3538. settings.HorDcm = 4;
  3539. else if (fmt->fmt.pix.width <=
  3540. fh->jpg_settings.img_width / 2)
  3541. settings.HorDcm = 2;
  3542. else
  3543. settings.HorDcm = 1;
  3544. if (settings.TmpDcm == 1)
  3545. settings.field_per_buff = 2;
  3546. else
  3547. settings.field_per_buff = 1;
  3548. /* check */
  3549. if ((res =
  3550. zoran_check_jpg_settings(zr,
  3551. &settings)))
  3552. goto tryfmt_unlock_and_return;
  3553. /* tell the user what we actually did */
  3554. fmt->fmt.pix.width =
  3555. settings.img_width / settings.HorDcm;
  3556. fmt->fmt.pix.height =
  3557. settings.img_height * 2 /
  3558. (settings.TmpDcm * settings.VerDcm);
  3559. if (settings.TmpDcm == 1)
  3560. fmt->fmt.pix.field =
  3561. (fh->jpg_settings.
  3562. odd_even ? V4L2_FIELD_SEQ_TB :
  3563. V4L2_FIELD_SEQ_BT);
  3564. else
  3565. fmt->fmt.pix.field =
  3566. (fh->jpg_settings.
  3567. odd_even ? V4L2_FIELD_TOP :
  3568. V4L2_FIELD_BOTTOM);
  3569. fmt->fmt.pix.sizeimage =
  3570. zoran_v4l2_calc_bufsize(&settings);
  3571. } else if (fmt->type ==
  3572. V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  3573. int i;
  3574. for (i = 0; i < zoran_num_formats; i++)
  3575. if (zoran_formats[i].fourcc ==
  3576. fmt->fmt.pix.pixelformat)
  3577. break;
  3578. if (i == zoran_num_formats) {
  3579. res = -EINVAL;
  3580. goto tryfmt_unlock_and_return;
  3581. }
  3582. if (fmt->fmt.pix.width > BUZ_MAX_WIDTH)
  3583. fmt->fmt.pix.width = BUZ_MAX_WIDTH;
  3584. if (fmt->fmt.pix.width < BUZ_MIN_WIDTH)
  3585. fmt->fmt.pix.width = BUZ_MIN_WIDTH;
  3586. if (fmt->fmt.pix.height > BUZ_MAX_HEIGHT)
  3587. fmt->fmt.pix.height =
  3588. BUZ_MAX_HEIGHT;
  3589. if (fmt->fmt.pix.height < BUZ_MIN_HEIGHT)
  3590. fmt->fmt.pix.height =
  3591. BUZ_MIN_HEIGHT;
  3592. } else {
  3593. res = -EINVAL;
  3594. goto tryfmt_unlock_and_return;
  3595. }
  3596. tryfmt_unlock_and_return:
  3597. up(&zr->resource_lock);
  3598. return res;
  3599. break;
  3600. default:
  3601. return -EINVAL;
  3602. }
  3603. return 0;
  3604. }
  3605. break;
  3606. #endif
  3607. default:
  3608. dprintk(1, KERN_DEBUG "%s: UNKNOWN ioctl cmd: 0x%x\n",
  3609. ZR_DEVNAME(zr), cmd);
  3610. return -ENOIOCTLCMD;
  3611. break;
  3612. }
  3613. return 0;
  3614. }
  3615. static int
  3616. zoran_ioctl (struct inode *inode,
  3617. struct file *file,
  3618. unsigned int cmd,
  3619. unsigned long arg)
  3620. {
  3621. return video_usercopy(inode, file, cmd, arg, zoran_do_ioctl);
  3622. }
  3623. static unsigned int
  3624. zoran_poll (struct file *file,
  3625. poll_table *wait)
  3626. {
  3627. struct zoran_fh *fh = file->private_data;
  3628. struct zoran *zr = fh->zr;
  3629. wait_queue_head_t *queue = NULL;
  3630. int res = 0, frame;
  3631. /* we should check whether buffers are ready to be synced on
  3632. * (w/o waits - O_NONBLOCK) here
  3633. * if ready for read (sync), return POLLIN|POLLRDNORM,
  3634. * if ready for write (sync), return POLLOUT|POLLWRNORM,
  3635. * if error, return POLLERR,
  3636. * if no buffers queued or so, return POLLNVAL
  3637. */
  3638. down(&zr->resource_lock);
  3639. switch (fh->map_mode) {
  3640. case ZORAN_MAP_MODE_RAW:
  3641. if (fh->v4l_buffers.active == ZORAN_FREE ||
  3642. zr->v4l_pend_head == zr->v4l_pend_tail) {
  3643. dprintk(1,
  3644. "%s: zoran_poll() - no buffers queued\n",
  3645. ZR_DEVNAME(zr));
  3646. res = POLLNVAL;
  3647. goto poll_unlock_and_return;
  3648. }
  3649. queue = &zr->v4l_capq;
  3650. frame = zr->v4l_pend[zr->v4l_pend_tail & V4L_MASK_FRAME];
  3651. poll_wait(file, queue, wait);
  3652. if (fh->v4l_buffers.buffer[frame].state == BUZ_STATE_DONE)
  3653. res = POLLIN | POLLRDNORM;
  3654. break;
  3655. case ZORAN_MAP_MODE_JPG_REC:
  3656. case ZORAN_MAP_MODE_JPG_PLAY:
  3657. if (fh->jpg_buffers.active == ZORAN_FREE ||
  3658. zr->jpg_que_head == zr->jpg_que_tail) {
  3659. dprintk(1,
  3660. "%s: zoran_poll() - no buffers queued\n",
  3661. ZR_DEVNAME(zr));
  3662. res = POLLNVAL;
  3663. goto poll_unlock_and_return;
  3664. }
  3665. queue = &zr->jpg_capq;
  3666. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  3667. poll_wait(file, queue, wait);
  3668. if (fh->jpg_buffers.buffer[frame].state == BUZ_STATE_DONE) {
  3669. if (fh->map_mode == ZORAN_MAP_MODE_JPG_REC)
  3670. res = POLLIN | POLLRDNORM;
  3671. else
  3672. res = POLLOUT | POLLWRNORM;
  3673. }
  3674. break;
  3675. default:
  3676. dprintk(1,
  3677. "%s: zoran_poll() - internal error, unknown map_mode=%d\n",
  3678. ZR_DEVNAME(zr), fh->map_mode);
  3679. res = POLLNVAL;
  3680. goto poll_unlock_and_return;
  3681. }
  3682. poll_unlock_and_return:
  3683. up(&zr->resource_lock);
  3684. return res;
  3685. }
  3686. /*
  3687. * This maps the buffers to user space.
  3688. *
  3689. * Depending on the state of fh->map_mode
  3690. * the V4L or the MJPEG buffers are mapped
  3691. * per buffer or all together
  3692. *
  3693. * Note that we need to connect to some
  3694. * unmap signal event to unmap the de-allocate
  3695. * the buffer accordingly (zoran_vm_close())
  3696. */
  3697. static void
  3698. zoran_vm_open (struct vm_area_struct *vma)
  3699. {
  3700. struct zoran_mapping *map = vma->vm_private_data;
  3701. map->count++;
  3702. }
  3703. static void
  3704. zoran_vm_close (struct vm_area_struct *vma)
  3705. {
  3706. struct zoran_mapping *map = vma->vm_private_data;
  3707. struct file *file = map->file;
  3708. struct zoran_fh *fh = file->private_data;
  3709. struct zoran *zr = fh->zr;
  3710. int i;
  3711. map->count--;
  3712. if (map->count == 0) {
  3713. switch (fh->map_mode) {
  3714. case ZORAN_MAP_MODE_JPG_REC:
  3715. case ZORAN_MAP_MODE_JPG_PLAY:
  3716. dprintk(3, KERN_INFO "%s: munmap(MJPEG)\n",
  3717. ZR_DEVNAME(zr));
  3718. for (i = 0; i < fh->jpg_buffers.num_buffers; i++) {
  3719. if (fh->jpg_buffers.buffer[i].map == map) {
  3720. fh->jpg_buffers.buffer[i].map =
  3721. NULL;
  3722. }
  3723. }
  3724. kfree(map);
  3725. for (i = 0; i < fh->jpg_buffers.num_buffers; i++)
  3726. if (fh->jpg_buffers.buffer[i].map)
  3727. break;
  3728. if (i == fh->jpg_buffers.num_buffers) {
  3729. down(&zr->resource_lock);
  3730. if (fh->jpg_buffers.active != ZORAN_FREE) {
  3731. jpg_qbuf(file, -1, zr->codec_mode);
  3732. zr->jpg_buffers.allocated = 0;
  3733. zr->jpg_buffers.active =
  3734. fh->jpg_buffers.active =
  3735. ZORAN_FREE;
  3736. }
  3737. //jpg_fbuffer_free(file);
  3738. fh->jpg_buffers.allocated = 0;
  3739. fh->jpg_buffers.ready_to_be_freed = 1;
  3740. up(&zr->resource_lock);
  3741. }
  3742. break;
  3743. case ZORAN_MAP_MODE_RAW:
  3744. dprintk(3, KERN_INFO "%s: munmap(V4L)\n",
  3745. ZR_DEVNAME(zr));
  3746. for (i = 0; i < fh->v4l_buffers.num_buffers; i++) {
  3747. if (fh->v4l_buffers.buffer[i].map == map) {
  3748. /* unqueue/unmap */
  3749. fh->v4l_buffers.buffer[i].map =
  3750. NULL;
  3751. }
  3752. }
  3753. kfree(map);
  3754. for (i = 0; i < fh->v4l_buffers.num_buffers; i++)
  3755. if (fh->v4l_buffers.buffer[i].map)
  3756. break;
  3757. if (i == fh->v4l_buffers.num_buffers) {
  3758. down(&zr->resource_lock);
  3759. if (fh->v4l_buffers.active != ZORAN_FREE) {
  3760. zr36057_set_memgrab(zr, 0);
  3761. zr->v4l_buffers.allocated = 0;
  3762. zr->v4l_buffers.active =
  3763. fh->v4l_buffers.active =
  3764. ZORAN_FREE;
  3765. }
  3766. //v4l_fbuffer_free(file);
  3767. fh->v4l_buffers.allocated = 0;
  3768. fh->v4l_buffers.ready_to_be_freed = 1;
  3769. up(&zr->resource_lock);
  3770. }
  3771. break;
  3772. default:
  3773. printk(KERN_ERR
  3774. "%s: munmap() - internal error - unknown map mode %d\n",
  3775. ZR_DEVNAME(zr), fh->map_mode);
  3776. break;
  3777. }
  3778. }
  3779. }
  3780. static struct vm_operations_struct zoran_vm_ops = {
  3781. .open = zoran_vm_open,
  3782. .close = zoran_vm_close,
  3783. };
  3784. #if LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0)
  3785. #define zr_remap_page_range(a,b,c,d,e) remap_page_range(b,c,d,e)
  3786. #else
  3787. #define zr_remap_page_range(a,b,c,d,e) remap_page_range(a,b,c,d,e)
  3788. #endif
  3789. static int
  3790. zoran_mmap (struct file *file,
  3791. struct vm_area_struct *vma)
  3792. {
  3793. struct zoran_fh *fh = file->private_data;
  3794. struct zoran *zr = fh->zr;
  3795. unsigned long size = (vma->vm_end - vma->vm_start);
  3796. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  3797. int i, j;
  3798. unsigned long page, start = vma->vm_start, todo, pos, fraglen;
  3799. int first, last;
  3800. struct zoran_mapping *map;
  3801. int res = 0;
  3802. dprintk(3,
  3803. KERN_INFO "%s: mmap(%s) of 0x%08lx-0x%08lx (size=%lu)\n",
  3804. ZR_DEVNAME(zr),
  3805. fh->map_mode == ZORAN_MAP_MODE_RAW ? "V4L" : "MJPEG",
  3806. vma->vm_start, vma->vm_end, size);
  3807. if (!(vma->vm_flags & VM_SHARED) || !(vma->vm_flags & VM_READ) ||
  3808. !(vma->vm_flags & VM_WRITE)) {
  3809. dprintk(1,
  3810. KERN_ERR
  3811. "%s: mmap() - no MAP_SHARED/PROT_{READ,WRITE} given\n",
  3812. ZR_DEVNAME(zr));
  3813. return -EINVAL;
  3814. }
  3815. switch (fh->map_mode) {
  3816. case ZORAN_MAP_MODE_JPG_REC:
  3817. case ZORAN_MAP_MODE_JPG_PLAY:
  3818. /* lock */
  3819. down(&zr->resource_lock);
  3820. /* Map the MJPEG buffers */
  3821. if (!fh->jpg_buffers.allocated) {
  3822. dprintk(1,
  3823. KERN_ERR
  3824. "%s: zoran_mmap(MJPEG) - buffers not yet allocated\n",
  3825. ZR_DEVNAME(zr));
  3826. res = -ENOMEM;
  3827. goto jpg_mmap_unlock_and_return;
  3828. }
  3829. first = offset / fh->jpg_buffers.buffer_size;
  3830. last = first - 1 + size / fh->jpg_buffers.buffer_size;
  3831. if (offset % fh->jpg_buffers.buffer_size != 0 ||
  3832. size % fh->jpg_buffers.buffer_size != 0 || first < 0 ||
  3833. last < 0 || first >= fh->jpg_buffers.num_buffers ||
  3834. last >= fh->jpg_buffers.num_buffers) {
  3835. dprintk(1,
  3836. KERN_ERR
  3837. "%s: mmap(MJPEG) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3838. ZR_DEVNAME(zr), offset, size,
  3839. fh->jpg_buffers.buffer_size,
  3840. fh->jpg_buffers.num_buffers);
  3841. res = -EINVAL;
  3842. goto jpg_mmap_unlock_and_return;
  3843. }
  3844. for (i = first; i <= last; i++) {
  3845. if (fh->jpg_buffers.buffer[i].map) {
  3846. dprintk(1,
  3847. KERN_ERR
  3848. "%s: mmap(MJPEG) - buffer %d already mapped\n",
  3849. ZR_DEVNAME(zr), i);
  3850. res = -EBUSY;
  3851. goto jpg_mmap_unlock_and_return;
  3852. }
  3853. }
  3854. /* map these buffers (v4l_buffers[i]) */
  3855. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3856. if (!map) {
  3857. res = -ENOMEM;
  3858. goto jpg_mmap_unlock_and_return;
  3859. }
  3860. map->file = file;
  3861. map->count = 1;
  3862. vma->vm_ops = &zoran_vm_ops;
  3863. vma->vm_flags |= VM_DONTEXPAND;
  3864. vma->vm_private_data = map;
  3865. for (i = first; i <= last; i++) {
  3866. for (j = 0;
  3867. j < fh->jpg_buffers.buffer_size / PAGE_SIZE;
  3868. j++) {
  3869. fraglen =
  3870. (fh->jpg_buffers.buffer[i].
  3871. frag_tab[2 * j + 1] & ~1) << 1;
  3872. todo = size;
  3873. if (todo > fraglen)
  3874. todo = fraglen;
  3875. pos =
  3876. (unsigned long) fh->jpg_buffers.
  3877. buffer[i].frag_tab[2 * j];
  3878. page = virt_to_phys(bus_to_virt(pos)); /* should just be pos on i386 */
  3879. if (zr_remap_page_range
  3880. (vma, start, page, todo, PAGE_SHARED)) {
  3881. dprintk(1,
  3882. KERN_ERR
  3883. "%s: zoran_mmap(V4L) - remap_page_range failed\n",
  3884. ZR_DEVNAME(zr));
  3885. res = -EAGAIN;
  3886. goto jpg_mmap_unlock_and_return;
  3887. }
  3888. size -= todo;
  3889. start += todo;
  3890. if (size == 0)
  3891. break;
  3892. if (fh->jpg_buffers.buffer[i].
  3893. frag_tab[2 * j + 1] & 1)
  3894. break; /* was last fragment */
  3895. }
  3896. fh->jpg_buffers.buffer[i].map = map;
  3897. if (size == 0)
  3898. break;
  3899. }
  3900. jpg_mmap_unlock_and_return:
  3901. up(&zr->resource_lock);
  3902. break;
  3903. case ZORAN_MAP_MODE_RAW:
  3904. down(&zr->resource_lock);
  3905. /* Map the V4L buffers */
  3906. if (!fh->v4l_buffers.allocated) {
  3907. dprintk(1,
  3908. KERN_ERR
  3909. "%s: zoran_mmap(V4L) - buffers not yet allocated\n",
  3910. ZR_DEVNAME(zr));
  3911. res = -ENOMEM;
  3912. goto v4l_mmap_unlock_and_return;
  3913. }
  3914. first = offset / fh->v4l_buffers.buffer_size;
  3915. last = first - 1 + size / fh->v4l_buffers.buffer_size;
  3916. if (offset % fh->v4l_buffers.buffer_size != 0 ||
  3917. size % fh->v4l_buffers.buffer_size != 0 || first < 0 ||
  3918. last < 0 || first >= fh->v4l_buffers.num_buffers ||
  3919. last >= fh->v4l_buffers.buffer_size) {
  3920. dprintk(1,
  3921. KERN_ERR
  3922. "%s: mmap(V4L) - offset=%lu or size=%lu invalid for bufsize=%d and numbufs=%d\n",
  3923. ZR_DEVNAME(zr), offset, size,
  3924. fh->v4l_buffers.buffer_size,
  3925. fh->v4l_buffers.num_buffers);
  3926. res = -EINVAL;
  3927. goto v4l_mmap_unlock_and_return;
  3928. }
  3929. for (i = first; i <= last; i++) {
  3930. if (fh->v4l_buffers.buffer[i].map) {
  3931. dprintk(1,
  3932. KERN_ERR
  3933. "%s: mmap(V4L) - buffer %d already mapped\n",
  3934. ZR_DEVNAME(zr), i);
  3935. res = -EBUSY;
  3936. goto v4l_mmap_unlock_and_return;
  3937. }
  3938. }
  3939. /* map these buffers (v4l_buffers[i]) */
  3940. map = kmalloc(sizeof(struct zoran_mapping), GFP_KERNEL);
  3941. if (!map) {
  3942. res = -ENOMEM;
  3943. goto v4l_mmap_unlock_and_return;
  3944. }
  3945. map->file = file;
  3946. map->count = 1;
  3947. vma->vm_ops = &zoran_vm_ops;
  3948. vma->vm_flags |= VM_DONTEXPAND;
  3949. vma->vm_private_data = map;
  3950. for (i = first; i <= last; i++) {
  3951. todo = size;
  3952. if (todo > fh->v4l_buffers.buffer_size)
  3953. todo = fh->v4l_buffers.buffer_size;
  3954. page = fh->v4l_buffers.buffer[i].fbuffer_phys;
  3955. if (zr_remap_page_range
  3956. (vma, start, page, todo, PAGE_SHARED)) {
  3957. dprintk(1,
  3958. KERN_ERR
  3959. "%s: zoran_mmap(V4L)i - remap_page_range failed\n",
  3960. ZR_DEVNAME(zr));
  3961. res = -EAGAIN;
  3962. goto v4l_mmap_unlock_and_return;
  3963. }
  3964. size -= todo;
  3965. start += todo;
  3966. fh->v4l_buffers.buffer[i].map = map;
  3967. if (size == 0)
  3968. break;
  3969. }
  3970. v4l_mmap_unlock_and_return:
  3971. up(&zr->resource_lock);
  3972. break;
  3973. default:
  3974. dprintk(1,
  3975. KERN_ERR
  3976. "%s: zoran_mmap() - internal error - unknown map mode %d\n",
  3977. ZR_DEVNAME(zr), fh->map_mode);
  3978. break;
  3979. }
  3980. return 0;
  3981. }
  3982. static struct file_operations zoran_fops = {
  3983. .owner = THIS_MODULE,
  3984. .open = zoran_open,
  3985. .release = zoran_close,
  3986. .ioctl = zoran_ioctl,
  3987. .llseek = no_llseek,
  3988. .read = zoran_read,
  3989. .write = zoran_write,
  3990. .mmap = zoran_mmap,
  3991. .poll = zoran_poll,
  3992. };
  3993. struct video_device zoran_template __devinitdata = {
  3994. .name = ZORAN_NAME,
  3995. .type = ZORAN_VID_TYPE,
  3996. #ifdef HAVE_V4L2
  3997. .type2 = ZORAN_V4L2_VID_FLAGS,
  3998. #endif
  3999. .hardware = ZORAN_HARDWARE,
  4000. .fops = &zoran_fops,
  4001. .release = &zoran_vdev_release,
  4002. .minor = -1
  4003. };