/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

Large files are truncated click here to view the full file

  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…