zr36067.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:119k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. #define DEBUGLEVEL 0
  2. #define MAX_KMALLOC_MEM (128*1024)
  3. /*
  4.    Miro/Pinnacle Systems Inc. DC10/DC10plus and
  5.    Linux Media Labs LML33 video capture boards driver
  6.    now with IOMega BUZ support!
  7.    
  8.    Copyright (C) 2000 Serguei Miridonov <mirsev@cicese.mx>
  9.    Changes for BUZ by Wolfgang Scherr <scherr@net4you.net>
  10.    
  11.    Based on
  12.    
  13.     Miro DC10 driver
  14.     Copyright (C) 1999 Wolfgang Scherr <scherr@net4you.net>
  15.    
  16.     Iomega Buz driver version 1.0
  17.     Copyright (C) 1999 Rainer Johanni <Rainer@Johanni.de>
  18.     buz.0.0.3
  19.     Copyright (C) 1998 Dave Perks <dperks@ibm.net>
  20.     bttv - Bt848 frame grabber driver
  21.     Copyright (C) 1996,97,98 Ralph  Metzler (rjkm@thp.uni-koeln.de)
  22.                            & Marcus Metzler (mocm@thp.uni-koeln.de)
  23.     
  24.     This program is free software; you can redistribute it and/or modify
  25.     it under the terms of the GNU General Public License as published by
  26.     the Free Software Foundation; either version 2 of the License, or
  27.     (at your option) any later version.
  28.     This program is distributed in the hope that it will be useful,
  29.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  30.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  31.     GNU General Public License for more details.
  32.     You should have received a copy of the GNU General Public License
  33.     along with this program; if not, write to the Free Software
  34.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35. */
  36. #include <linux/config.h>
  37. #include <linux/version.h>
  38. #include <linux/init.h>
  39. #include <linux/module.h>
  40. #include <linux/delay.h>
  41. #include <linux/errno.h>
  42. #include <linux/fs.h>
  43. #include <linux/kernel.h>
  44. #include <linux/major.h>
  45. #include <linux/slab.h>
  46. #include <linux/mm.h>
  47. #include <linux/pci.h>
  48. #include <linux/signal.h>
  49. #include <asm/io.h>
  50. #include <asm/pgtable.h>
  51. #include <asm/page.h>
  52. #include <linux/sched.h>
  53. #include <asm/segment.h>
  54. #include <linux/types.h>
  55. #include <linux/wrapper.h>
  56. #include <linux/spinlock.h>
  57. #include <linux/vmalloc.h>
  58. #include <linux/i2c-old.h>
  59. #define     MAP_NR(x)       virt_to_page(x)
  60. #define     ZORAN_HARDWARE  VID_HARDWARE_ZR36067
  61. #include <linux/videodev.h>
  62. #include <asm/uaccess.h>
  63. #include <linux/proc_fs.h>
  64. #include "zoran.h"
  65. #include <linux/video_decoder.h>
  66. #include <linux/video_encoder.h>
  67. // RJ: Test only - want to test BUZ_USE_HIMEM even when CONFIG_BIGPHYS_AREA is defined
  68. #if !defined(CONFIG_BIGPHYS_AREA)
  69. //#undef CONFIG_BIGPHYS_AREA
  70. #define BUZ_USE_HIMEM
  71. #endif
  72. #if defined(CONFIG_BIGPHYS_AREA)
  73. #   include <linux/bigphysarea.h>
  74. #endif
  75. #define IRQ_MASK ( ZR36057_ISR_GIRQ0 | ZR36057_ISR_GIRQ1 | /* ZR36057_ISR_CodRepIRQ | */  ZR36057_ISR_JPEGRepIRQ ) // SW
  76. //#define GPIO_MASK 0xcd
  77. //#define GPIO_MASK 0x8d
  78. /*
  79. DC10:
  80. GPIO0 = /RESET ZR 36060
  81. GPIO1 = VIDEO BUS DIRECTION (0: CAPTURE, 1: DISPLAY)
  82. GPIO2 = VIDEO BUS ENABLE    (0: ON, 1: OFF)
  83. GPIO3 = /SLEEP ZR 36060
  84. GPIO4 = ADC7175 (video out) FREQUENCY (0: LCC/SAA7110, 1: 27 MHz (quarz))
  85. GPIO5 = ZORAN FREQUENCY  (0: LCC and LCC2 from SAA7110,
  86.                           1: 27 and 13.5 MHz (quarz))
  87. GPIO6 = /FRAME ZR 36060
  88. GPIO7 = /RESET ADV7175 (video out)
  89. (I think they lost the SAA7110 reset.....)
  90. GIRQ0 signals that ZR36060's DATERR# line is asserted.
  91. GIRQ1 signals a vertical sync of the video signal (VS SAA7110)
  92.   SAA7110A:
  93.    mode 0 - Composite
  94.    mode 1 - 
  95.    mode 2 - 
  96.    mode 3 - 
  97.    mode 4 - 
  98.    mode 5 - internal Composite (from PCTV)
  99.    mode 6 - 
  100.    mode 7 - S-Video
  101. BUZ:
  102. GPIO0 = 1, take board out of reset
  103. GPIO1 = 1, take JPEG codec out of sleep mode
  104. GPIO3 = 1, deassert FRAME# to 36060
  105. GIRQ0 signals a vertical sync of the video signal
  106. GIRQ1 signals that ZR36060's DATERR# line is asserted.
  107. SAA7111A
  108.    In their infinite wisdom, the Iomega engineers decided to
  109.    use the same input line for composite and S-Video Color,
  110.    although there are two entries not connected at all!
  111.    Through this ingenious strike, it is not possible to
  112.    keep two running video sources connected at the same time
  113.    to Composite and S-VHS input!
  114.    mode 0 - N/C
  115.    mode 1 - S-Video Y
  116.    mode 2 - noise or something I don't know
  117.    mode 3 - Composite and S-Video C
  118.    mode 4 - N/C
  119.    mode 5 - S-Video (gain C independently selectable of gain Y)
  120.    mode 6 - N/C
  121.    mode 7 - S-Video (gain C adapted to gain Y)
  122. */
  123. #define MAJOR_VERSION 0 /* driver major version */
  124. #define MINOR_VERSION 7 /* driver minor version */
  125. #define ZORAN_NAME    "zr36067" /* name of the device */
  126. #define BUZ_ERR       KERN_ERR     ZORAN_NAME
  127. #define BUZ_DEBUG     KERN_INFO    ZORAN_NAME
  128. #define BUZ_INFO      KERN_INFO    ZORAN_NAME
  129. #define BUZ_WARNING   KERN_WARNING ZORAN_NAME
  130. #if(DEBUGLEVEL>0)
  131. #define DEBUG1(x...)    x
  132. #else
  133. #define DEBUG1(x...)
  134. #endif
  135. #if(DEBUGLEVEL>1)
  136. #define DEBUG2(x...)    x
  137. #else
  138. #define DEBUG2(x...)
  139. #endif
  140. #if(DEBUGLEVEL>2)
  141. #define DEBUG3(x...)    x
  142. #else
  143. #define DEBUG3(x...)
  144. #endif
  145. #if(DEBUGLEVEL>3)
  146. #define DEBUG4(x...)    x
  147. #else
  148. #define DEBUG4(x...)
  149. #endif
  150. /* The parameters for this driver */
  151. /*
  152.    The video mem address of the video card.
  153.    The driver has a little database for some videocards
  154.    to determine it from there. If your video card is not in there
  155.    you have either to give it to the driver as a parameter
  156.    or set in in a VIDIOCSFBUF ioctl
  157.  */
  158. static unsigned long vidmem = 0; /* Video memory base address */
  159. /* Special purposes only: */
  160. static int triton = 0; /* 0=no, 1=yes */
  161. static int natoma = 0; /* 0=no, 1=yes */
  162. /*
  163.    Number and size of grab buffers for Video 4 Linux
  164.    The vast majority of applications should not need more than 2,
  165.    the very popular BTTV driver actually does ONLY have 2.
  166.    Time sensitive applications might need more, the maximum
  167.    is VIDEO_MAX_FRAME (defined in <linux/videodev.h>).
  168.    The size is set so that the maximum possible request
  169.    can be satisfied. Decrease  it, if bigphys_area alloc'd
  170.    memory is low. If you don't have the bigphys_area patch,
  171.    set it to 128 KB. Will you allow only to grab small
  172.    images with V4L, but that's better than nothing.
  173.    v4l_bufsize has to be given in KB !
  174. */
  175. static int v4l_nbufs = 2;
  176. static int v4l_bufsize = 128; /* Everybody should be able to work with this setting */
  177. /*
  178.    Default input and video norm at startup of the driver.
  179. */
  180. static int default_input = 0; /* 0=Composite, 1=S-VHS */
  181. static int default_norm = 0; /* 0=PAL, 1=NTSC 2=SECAM */
  182. static int lock_norm = 0; /* 1=Don't change TV standard (norm) */
  183. static int pass_through = 0; /* 1=Pass through TV signal when device is not used */
  184. /* 0=Show color bar when device is not used (LML33: only if lml33dpath=1) */
  185. static int lml33dpath = 0; /* 1 will use digital path in capture mode instead of analog.
  186.    It can be used for picture adjustments using tool like xawtv
  187.    while watching image on TV monitor connected to the output.
  188.    However, due to absence of 75 Ohm load on Bt819 input, there
  189.    will be some image imperfections */
  190. static int video_nr = -1;
  191. MODULE_PARM(vidmem, "i");
  192. MODULE_PARM(triton, "i");
  193. MODULE_PARM(natoma, "i");
  194. MODULE_PARM(v4l_nbufs, "i");
  195. MODULE_PARM(v4l_bufsize, "i");
  196. MODULE_PARM(default_input, "i");
  197. MODULE_PARM(default_norm, "i");
  198. MODULE_PARM(lock_norm, "i");
  199. MODULE_PARM(pass_through, "i");
  200. MODULE_PARM(lml33dpath, "i");
  201. MODULE_PARM(video_nr, "i");
  202. static struct pci_device_id zr36067_pci_tbl[] = {
  203. { PCI_VENDOR_ID_ZORAN, PCI_DEVICE_ID_ZORAN_36057, 
  204.   PCI_ANY_ID, PCI_ANY_ID,  0, 0, 0 },
  205. { 0 }
  206. };
  207. MODULE_DEVICE_TABLE(pci, zr36067_pci_tbl);
  208. /* Anybody who uses more than four? */
  209. #define BUZ_MAX 4
  210. static int zoran_num; /* number of Buzs in use */
  211. static struct zoran zoran[BUZ_MAX];
  212. /* forward references */
  213. static void v4l_fbuffer_free(struct zoran *zr);
  214. static void jpg_fbuffer_free(struct zoran *zr);
  215. static void zoran_feed_stat_com(struct zoran *zr);
  216. /*
  217.  *   Allocate the V4L grab buffers
  218.  *
  219.  *   These have to be pysically contiguous.
  220.  *   If v4l_bufsize <= MAX_KMALLOC_MEM we use kmalloc
  221.  *   else we try to allocate them with bigphysarea_alloc_pages
  222.  *   if the bigphysarea patch is present in the kernel,
  223.  *   else we try to use high memory (if the user has bootet
  224.  *   Linux with the necessary memory left over).
  225.  */
  226. static int v4l_fbuffer_alloc(struct zoran *zr)
  227. {
  228. int i, off;
  229. unsigned char *mem;
  230. for (i = 0; i < v4l_nbufs; i++) {
  231. if (zr->v4l_gbuf[i].fbuffer)
  232. printk(KERN_WARNING
  233.        "%s: v4l_fbuffer_alloc: buffer %d allready allocated ???n",
  234.        zr->name, i);
  235. //udelay(20);
  236. if (v4l_bufsize <= MAX_KMALLOC_MEM) {
  237. /* Use kmalloc */
  238. mem =
  239.     (unsigned char *) kmalloc(v4l_bufsize,
  240.       GFP_KERNEL);
  241. if (mem == 0) {
  242. printk(KERN_ERR
  243.        "%s: kmalloc for V4L bufs failedn",
  244.        zr->name);
  245. v4l_fbuffer_free(zr);
  246. return -ENOBUFS;
  247. }
  248. zr->v4l_gbuf[i].fbuffer = mem;
  249. zr->v4l_gbuf[i].fbuffer_phys = virt_to_phys(mem);
  250. zr->v4l_gbuf[i].fbuffer_bus = virt_to_bus(mem);
  251. for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
  252. mem_map_reserve(MAP_NR(mem + off));
  253. DEBUG1(printk
  254.        (KERN_INFO
  255. "%s: V4L frame %d mem 0x%lx (bus: 0x%lx)n",
  256. zr->name, i, (unsigned long) mem,
  257. virt_to_bus(mem)));
  258. } else {
  259. #if defined(CONFIG_BIGPHYS_AREA)
  260. /* Use bigphysarea_alloc_pages */
  261. int n = (v4l_bufsize + PAGE_SIZE - 1) / PAGE_SIZE;
  262. mem =
  263.     (unsigned char *) bigphysarea_alloc_pages(n, 0,
  264.       GFP_KERNEL);
  265. if (mem == 0) {
  266. printk(KERN_ERR
  267.        "%s: bigphysarea_alloc_pages for V4L bufs failedn",
  268.        zr->name);
  269. v4l_fbuffer_free(zr);
  270. return -ENOBUFS;
  271. }
  272. zr->v4l_gbuf[i].fbuffer = mem;
  273. zr->v4l_gbuf[i].fbuffer_phys = virt_to_phys(mem);
  274. zr->v4l_gbuf[i].fbuffer_bus = virt_to_bus(mem);
  275. DEBUG1(printk
  276.        (KERN_INFO
  277. "%s: Bigphysarea frame %d mem 0x%x (bus: 0x%x)n",
  278. zr->name, i, (unsigned) mem,
  279. (unsigned) virt_to_bus(mem)));
  280. /* Zero out the allocated memory */
  281. memset(zr->v4l_gbuf[i].fbuffer, 0, v4l_bufsize);
  282. #else
  283. /* No bigphysarea present, usage of high memory disabled,
  284.    but user wants buffers of more than MAX_KMALLOC_MEM */
  285. printk(KERN_ERR
  286.        "%s: No bigphysarea_patch present, usage of high memory disabled,n",
  287.        zr->name);
  288. printk(KERN_ERR
  289.        "%s: sorry, could not allocate V4L buffers of size %d KB.n",
  290.        zr->name, v4l_bufsize >> 10);
  291. return -ENOBUFS;
  292. #endif
  293. }
  294. }
  295. return 0;
  296. }
  297. /* free the V4L grab buffers */
  298. static void v4l_fbuffer_free(struct zoran *zr)
  299. {
  300. int i, off;
  301. unsigned char *mem;
  302. for (i = 0; i < v4l_nbufs; i++) {
  303. if (!zr->v4l_gbuf[i].fbuffer)
  304. continue;
  305. if (v4l_bufsize <= MAX_KMALLOC_MEM) {
  306. mem = zr->v4l_gbuf[i].fbuffer;
  307. for (off = 0; off < v4l_bufsize; off += PAGE_SIZE)
  308. mem_map_unreserve(MAP_NR(mem + off));
  309. kfree((void *) zr->v4l_gbuf[i].fbuffer);
  310. }
  311. #if defined(CONFIG_BIGPHYS_AREA)
  312. else
  313. bigphysarea_free_pages((void *) zr->v4l_gbuf[i].
  314.        fbuffer);
  315. #endif
  316. zr->v4l_gbuf[i].fbuffer = NULL;
  317. }
  318. }
  319. /*
  320.  *   Allocate the MJPEG grab buffers.
  321.  *
  322.  *   If the requested buffer size is smaller than MAX_KMALLOC_MEM,
  323.  *   kmalloc is used to request a physically contiguous area,
  324.  *   else we allocate the memory in framgents with get_free_page.
  325.  *
  326.  *   If a Natoma chipset is present and this is a revision 1 zr36057,
  327.  *   each MJPEG buffer needs to be physically contiguous.
  328.  *   (RJ: This statement is from Dave Perks' original driver,
  329.  *   I could never check it because I have a zr36067)
  330.  *   The driver cares about this because it reduces the buffer
  331.  *   size to MAX_KMALLOC_MEM in that case (which forces contiguous allocation).
  332.  *
  333.  *   RJ: The contents grab buffers needs never be accessed in the driver.
  334.  *       Therefore there is no need to allocate them with vmalloc in order
  335.  *       to get a contiguous virtual memory space.
  336.  *       I don't understand why many other drivers first allocate them with
  337.  *       vmalloc (which uses internally also get_free_page, but delivers you
  338.  *       virtual addresses) and then again have to make a lot of efforts
  339.  *       to get the physical address.
  340.  *
  341.  */
  342. static int jpg_fbuffer_alloc(struct zoran *zr)
  343. {
  344. int i, j, off; //alloc_contig;
  345. unsigned long mem;
  346. /* Decide if we should alloc contiguous or fragmented memory */
  347. /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
  348. //alloc_contig = (zr->jpg_bufsize <= MAX_KMALLOC_MEM);
  349. for (i = 0; i < zr->jpg_nbufs; i++) {
  350. if (zr->jpg_gbuf[i].frag_tab)
  351. printk(KERN_WARNING
  352.        "%s: jpg_fbuffer_alloc: buffer %d allready allocated ???n",
  353.        zr->name, i);
  354. /* Allocate fragment table for this buffer */
  355. mem = get_free_page(GFP_KERNEL);
  356. if (mem == 0) {
  357. printk(KERN_ERR
  358.        "%s: jpg_fbuffer_alloc: get_free_page (frag_tab) failed for buffer %dn",
  359.        zr->name, i);
  360. jpg_fbuffer_free(zr);
  361. return -ENOBUFS;
  362. }
  363. memset((void *) mem, 0, PAGE_SIZE);
  364. zr->jpg_gbuf[i].frag_tab = (u32 *) mem;
  365. zr->jpg_gbuf[i].frag_tab_bus = virt_to_bus((void *) mem);
  366. //if (alloc_contig) {
  367. if (zr->need_contiguous) {
  368. mem = (unsigned long) kmalloc(zr->jpg_bufsize, GFP_KERNEL);
  369. if (mem == 0) {
  370. printk(KERN_ERR "%s: jpg_fbuffer_alloc: kmalloc failed for buffer %dn",
  371.        zr->name, i);
  372. jpg_fbuffer_free(zr);
  373. return -ENOBUFS;
  374. }
  375. zr->jpg_gbuf[i].frag_tab[0] = virt_to_bus((void *) mem);
  376. zr->jpg_gbuf[i].frag_tab[1] =
  377.     ((zr->jpg_bufsize / 4) << 1) | 1;
  378. for (off = 0; off < zr->jpg_bufsize; off += PAGE_SIZE)
  379. mem_map_reserve(MAP_NR(mem + off));
  380. } else {
  381. /* jpg_bufsize is allreay page aligned */
  382. for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) 
  383. {
  384. mem = get_free_page(GFP_KERNEL);
  385. if (mem == 0) {
  386. printk(KERN_ERR
  387.        "%s: jpg_fbuffer_alloc: get_free_page failed for buffer %dn",
  388.        zr->name, i);
  389. jpg_fbuffer_free(zr);
  390. return -ENOBUFS;
  391. }
  392. zr->jpg_gbuf[i].frag_tab[2 * j] =
  393.     virt_to_bus((void *) mem);
  394. zr->jpg_gbuf[i].frag_tab[2 * j + 1] =
  395.     (PAGE_SIZE / 4) << 1;
  396. mem_map_reserve(MAP_NR(mem));
  397. }
  398. zr->jpg_gbuf[i].frag_tab[2 * j - 1] |= 1;
  399. }
  400. }
  401. DEBUG1(printk
  402.        ("%s: jpg_fbuffer_alloc: %ld KB allocatedn", zr->name,
  403. (zr->jpg_nbufs * zr->jpg_bufsize) >> 10));
  404. zr->jpg_buffers_allocated = 1;
  405. return 0;
  406. }
  407. /* free the MJPEG grab buffers */
  408. static void jpg_fbuffer_free(struct zoran *zr)
  409. {
  410. int i, j, off; // alloc_contig;
  411. unsigned char *mem;
  412. /* Decide if we should alloc contiguous or fragmented memory */
  413. /* This has to be identical in jpg_fbuffer_alloc and jpg_fbuffer_free */
  414. //alloc_contig = (zr->jpg_bufsize <= MAX_KMALLOC_MEM);
  415. for (i = 0; i < zr->jpg_nbufs; i++) {
  416. if (!zr->jpg_gbuf[i].frag_tab)
  417. continue;
  418. //if (alloc_contig) {
  419. if (zr->need_contiguous) {
  420. if (zr->jpg_gbuf[i].frag_tab[0]) {
  421. mem =
  422.     (unsigned char *) bus_to_virt(zr->
  423.   jpg_gbuf
  424.   [i].
  425.   frag_tab
  426.   [0]);
  427. for (off = 0; off < zr->jpg_bufsize;
  428.      off += PAGE_SIZE)
  429. mem_map_unreserve(MAP_NR
  430.   (mem + off));
  431. kfree((void *) mem);
  432. zr->jpg_gbuf[i].frag_tab[0] = 0;
  433. zr->jpg_gbuf[i].frag_tab[1] = 0;
  434. }
  435. } else {
  436. for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
  437. if (!zr->jpg_gbuf[i].frag_tab[2 * j])
  438. break;
  439. mem_map_unreserve(MAP_NR
  440.   (bus_to_virt
  441.    (zr->jpg_gbuf[i].
  442.     frag_tab[2 * j])));
  443. free_page((unsigned long)
  444.   bus_to_virt(zr->jpg_gbuf[i].
  445.       frag_tab[2 * j]));
  446. zr->jpg_gbuf[i].frag_tab[2 * j] = 0;
  447. zr->jpg_gbuf[i].frag_tab[2 * j + 1] = 0;
  448. }
  449. }
  450. free_page((unsigned long) zr->jpg_gbuf[i].frag_tab);
  451. zr->jpg_gbuf[i].frag_tab = NULL;
  452. }
  453. zr->jpg_buffers_allocated = 0;
  454. }
  455. /* ----------------------------------------------------------------------- */
  456. /* I2C functions                                                           */
  457. #define I2C_DELAY   10
  458. /* software I2C functions */
  459. static void i2c_setlines(struct i2c_bus *bus, int ctrl, int data)
  460. {
  461. struct zoran *zr = (struct zoran *) bus->data;
  462. btwrite((data << 1) | ctrl, ZR36057_I2CBR);
  463. udelay(I2C_DELAY);
  464. }
  465. static int i2c_getdataline(struct i2c_bus *bus)
  466. {
  467. struct zoran *zr = (struct zoran *) bus->data;
  468. return (btread(ZR36057_I2CBR) >> 1) & 1;
  469. }
  470. static void attach_inform(struct i2c_bus *bus, int id)
  471. {
  472. int i;
  473. struct zoran *zr = (struct zoran *) bus->data;
  474. DEBUG1(printk(KERN_DEBUG "%s: i2c attach %02xn", zr->name, id));
  475. for (i = 0; i < bus->devcount; i++) {
  476. if (strcmp(bus->devices[i]->name, "saa7110") == 0) {
  477. if (zr->revision < 2) {
  478. zr->card = DC10;
  479. sprintf(zr->name, "DC10[%u]", zr->id);
  480. } else {
  481. zr->card = DC10plus;
  482. sprintf(zr->name, "DC10plus[%u]", zr->id);
  483. }
  484. break;
  485. }
  486. if (strcmp(bus->devices[i]->name, "bt819") == 0) {
  487. zr->card = LML33;
  488. sprintf(zr->name, "LML33[%u]", zr->id);
  489. break;
  490. }
  491. if (strcmp(bus->devices[i]->name, "saa7111") == 0) {
  492. zr->card = BUZ;
  493. sprintf(zr->name, "Buz[%u]", zr->id);
  494. break;
  495. }
  496. }
  497. }
  498. static void detach_inform(struct i2c_bus *bus, int id)
  499. {
  500. DEBUG1(struct zoran *zr = (struct zoran *) bus->data);
  501. DEBUG1(printk(KERN_DEBUG "%s: i2c detach %02xn", zr->name, id));
  502. }
  503. static struct i2c_bus zoran_i2c_bus_template = {
  504. "zr36057",
  505. I2C_BUSID_BT848,
  506. NULL,
  507. SPIN_LOCK_UNLOCKED,
  508. attach_inform,
  509. detach_inform,
  510. i2c_setlines,
  511. i2c_getdataline,
  512. NULL,
  513. NULL,
  514. };
  515. /*
  516.  *   Set the registers for the size we have specified. Don't bother
  517.  *   trying to understand this without the ZR36057 manual in front of
  518.  *   you [AC].
  519.  *
  520.  *   PS: The manual is free for download in .pdf format from
  521.  *   www.zoran.com - nicely done those folks.
  522.  */
  523. static struct tvnorm f50sqpixel = { 944, 768, 83, 880, 625, 576, 16 };
  524. static struct tvnorm f60sqpixel = { 780, 640, 51, 716, 525, 480, 12 };
  525. static struct tvnorm f50ccir601 = { 864, 720, 75, 804, 625, 576, 18 };
  526. static struct tvnorm f60ccir601 = { 858, 720, 57, 788, 525, 480, 16 };
  527. static struct tvnorm *dc10norms[] = {
  528. &f50sqpixel, /* PAL-BDGHI */
  529. &f60sqpixel, /* NTSC */
  530. &f50sqpixel, /* SECAM */
  531. };
  532. static struct tvnorm *lml33norms[] = {
  533. &f50ccir601, /* PAL-BDGHI */
  534. &f60ccir601, /* NTSC      */
  535. NULL, /* SECAM (not supported in LML33) */
  536. };
  537. static struct tvnorm *buznorms[] = {
  538. &f50ccir601, /* PAL-BDGHI */
  539. &f60ccir601, /* NTSC      */
  540. NULL, /* SECAM     */
  541. };
  542. static struct tvnorm *unsupported[] = {
  543. NULL, /* PAL-BDGHI */
  544. NULL, /* NTSC      */
  545. NULL, /* SECAM     */
  546. };
  547. static struct tvnorm **cardnorms[] = {
  548. unsupported, /* UNKNOWN  */
  549. dc10norms, /* DC10     */
  550. dc10norms, /* DC10plus */
  551. lml33norms, /* LML33    */
  552. buznorms, /* Buz      */
  553. };
  554. static u32 cardvsync[] = {
  555. 0, /* UNKNOWN  */
  556. ZR36057_ISR_GIRQ1, /* DC10     */
  557. ZR36057_ISR_GIRQ1, /* DC10plus */
  558. ZR36057_ISR_GIRQ0, /* LML33    */
  559. ZR36057_ISR_GIRQ0, /* Buz      */
  560. };
  561. static u32 cardjpegint[] = {
  562. 0, /* UNKNOWN  */
  563. ZR36057_ISR_GIRQ0, /* DC10     */
  564. ZR36057_ISR_GIRQ0, /* DC10plus */
  565. ZR36057_ISR_GIRQ1, /* LML33    */
  566. ZR36057_ISR_GIRQ1, /* Buz      */
  567. };
  568. static int format2bpp(int format)
  569. {
  570. int bpp;
  571. /* Determine the number of bytes per pixel for the video format requested */
  572. switch (format) {
  573. case VIDEO_PALETTE_YUV422:
  574. bpp = 2;
  575. break;
  576. case VIDEO_PALETTE_RGB555:
  577. bpp = 2;
  578. break;
  579. case VIDEO_PALETTE_RGB565:
  580. bpp = 2;
  581. break;
  582. case VIDEO_PALETTE_RGB24:
  583. bpp = 3;
  584. break;
  585. case VIDEO_PALETTE_RGB32:
  586. bpp = 4;
  587. break;
  588. default:
  589. bpp = 0;
  590. }
  591. return bpp;
  592. }
  593. static void zr36057_adjust_vfe(struct zoran *zr,
  594.        enum zoran_codec_mode mode)
  595. {
  596. u32 reg;
  597. switch (mode) {
  598. case BUZ_MODE_MOTION_DECOMPRESS:
  599. btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
  600. reg = btread(ZR36057_VFEHCR);
  601. if (reg & (1 << 10)) {
  602. reg += ((1 << 10) | 1);
  603. }
  604. btwrite(reg, ZR36057_VFEHCR);
  605. break;
  606. case BUZ_MODE_MOTION_COMPRESS:
  607. case BUZ_MODE_IDLE:
  608. default:
  609. if (zr->params.norm == VIDEO_MODE_NTSC)
  610. btand(~ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
  611. else
  612. btor(ZR36057_VFESPFR_ExtFl, ZR36057_VFESPFR);
  613. reg = btread(ZR36057_VFEHCR);
  614. if (!(reg & (1 << 10))) {
  615. reg -= ((1 << 10) | 1);
  616. }
  617. btwrite(reg, ZR36057_VFEHCR);
  618. break;
  619. }
  620. }
  621. /*
  622.  * set geometry
  623.  */
  624. static void zr36057_set_vfe(struct zoran *zr, int video_width,
  625.     int video_height, unsigned int video_format)
  626. {
  627. struct tvnorm *tvn;
  628. unsigned HStart, HEnd, VStart, VEnd;
  629. unsigned DispMode;
  630. unsigned VidWinWid, VidWinHt;
  631. unsigned hcrop1, hcrop2, vcrop1, vcrop2;
  632. unsigned Wa, We, Ha, He;
  633. unsigned X, Y, HorDcm, VerDcm;
  634. u32 reg;
  635. unsigned mask_line_size;
  636. tvn = zr->timing;
  637. Wa = tvn->Wa;
  638. Ha = tvn->Ha;
  639. DEBUG1(printk (BUZ_INFO ": width = %d, height = %dn", video_width, video_height));
  640. if (zr->params.norm != VIDEO_MODE_PAL
  641.     && zr->params.norm != VIDEO_MODE_NTSC
  642.     && zr->params.norm != VIDEO_MODE_SECAM) {
  643. printk(KERN_ERR "%s: set_vfe: video_norm = %d not validn",
  644.        zr->name, zr->params.norm);
  645. return;
  646. }
  647. if (video_width < BUZ_MIN_WIDTH || video_height < BUZ_MIN_HEIGHT
  648.     || video_width > Wa || video_height > Ha) {
  649. printk(KERN_ERR "%s: set_vfe: w=%d h=%d not validn",
  650.        zr->name, video_width, video_height);
  651. return;
  652. }
  653. /* if window has more than half of active height,
  654.    switch on interlacing - we want the full information */
  655. zr->video_interlace = (video_height > Ha / 2);
  656. /**** zr36057 ****/
  657. /* horizontal */
  658. VidWinWid = video_width;
  659. X = (VidWinWid * 64 + tvn->Wa - 1) / tvn->Wa;
  660. We = (VidWinWid * 64) / X;
  661. HorDcm = 64 - X;
  662. hcrop1 = 2 * ((tvn->Wa - We) / 4);
  663. hcrop2 = tvn->Wa - We - hcrop1;
  664. HStart = tvn->HStart | 1;
  665. if (zr->card == LML33)
  666. HStart += 62;
  667. if (zr->card == BUZ) { //HStart += 67;
  668. HStart += 44;
  669. }
  670. HEnd = HStart + tvn->Wa - 1;
  671. HStart += hcrop1;
  672. HEnd -= hcrop2;
  673. reg = ((HStart & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HStart)
  674.     | ((HEnd & ZR36057_VFEHCR_Hmask) << ZR36057_VFEHCR_HEnd);
  675. if (zr->card != BUZ)
  676. reg |= ZR36057_VFEHCR_HSPol;
  677. btwrite(reg, ZR36057_VFEHCR);
  678. /* Vertical */
  679. DispMode = !zr->video_interlace;
  680. VidWinHt = DispMode ? video_height : video_height / 2;
  681. Y = (VidWinHt * 64 * 2 + tvn->Ha - 1) / tvn->Ha;
  682. He = (VidWinHt * 64) / Y;
  683. VerDcm = 64 - Y;
  684. vcrop1 = (tvn->Ha / 2 - He) / 2;
  685. vcrop2 = tvn->Ha / 2 - He - vcrop1;
  686. VStart = tvn->VStart;
  687. VEnd = VStart + tvn->Ha / 2 - 1;
  688. VStart += vcrop1;
  689. VEnd -= vcrop2;
  690. reg = ((VStart & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VStart)
  691.     | ((VEnd & ZR36057_VFEVCR_Vmask) << ZR36057_VFEVCR_VEnd);
  692. reg |= ZR36057_VFEVCR_VSPol;
  693. btwrite(reg, ZR36057_VFEVCR);
  694. /* scaler and pixel format */
  695. reg = 0;
  696. reg |= (HorDcm << ZR36057_VFESPFR_HorDcm);
  697. reg |= (VerDcm << ZR36057_VFESPFR_VerDcm);
  698. reg |= (DispMode << ZR36057_VFESPFR_DispMode);
  699. reg |= ZR36057_VFESPFR_LittleEndian;
  700. /* RJ: I don't know, why the following has to be the opposite
  701.    of the corresponding ZR36060 setting, but only this way
  702.    we get the correct colors when uncompressing to the screen  */
  703. //reg |= ZR36057_VFESPFR_VCLKPol; /**/
  704. /* RJ: Don't know if that is needed for NTSC also */
  705. if (zr->params.norm != VIDEO_MODE_NTSC)
  706. reg |= ZR36057_VFESPFR_ExtFl; // NEEDED!!!!!!! Wolfgang
  707. reg |= ZR36057_VFESPFR_TopField;
  708. switch (video_format) {
  709. case VIDEO_PALETTE_YUV422:
  710. reg |= ZR36057_VFESPFR_YUV422;
  711. break;
  712. case VIDEO_PALETTE_RGB555:
  713. reg |= ZR36057_VFESPFR_RGB555 | ZR36057_VFESPFR_ErrDif;
  714. break;
  715. case VIDEO_PALETTE_RGB565:
  716. reg |= ZR36057_VFESPFR_RGB565 | ZR36057_VFESPFR_ErrDif;
  717. break;
  718. case VIDEO_PALETTE_RGB24:
  719. reg |= ZR36057_VFESPFR_RGB888 | ZR36057_VFESPFR_Pack24;
  720. break;
  721. case VIDEO_PALETTE_RGB32:
  722. reg |= ZR36057_VFESPFR_RGB888;
  723. break;
  724. default:
  725. printk(KERN_INFO "%s: Unknown color_fmt=%xn", zr->name,
  726.        video_format);
  727. return;
  728. }
  729. if (HorDcm >= 48) {
  730. reg |= 3 << ZR36057_VFESPFR_HFilter; /* 5 tap filter */
  731. } else if (HorDcm >= 32) {
  732. reg |= 2 << ZR36057_VFESPFR_HFilter; /* 4 tap filter */
  733. } else if (HorDcm >= 16) {
  734. reg |= 1 << ZR36057_VFESPFR_HFilter; /* 3 tap filter */
  735. }
  736. btwrite(reg, ZR36057_VFESPFR);
  737. /* display configuration */
  738. reg = (16 << ZR36057_VDCR_MinPix)
  739.     | (VidWinHt << ZR36057_VDCR_VidWinHt)
  740.     | (VidWinWid << ZR36057_VDCR_VidWinWid);
  741. if (triton || zr->revision <= 1)
  742. reg &= ~ZR36057_VDCR_Triton;
  743. else
  744. reg |= ZR36057_VDCR_Triton;
  745. btwrite(reg, ZR36057_VDCR);
  746. /* Write overlay clipping mask data, but don't enable overlay clipping */
  747. /* RJ: since this makes only sense on the screen, we use 
  748.    zr->window.width instead of video_width */
  749. mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
  750. reg = virt_to_bus(zr->overlay_mask);
  751. btwrite(reg, ZR36057_MMTR);
  752. reg = virt_to_bus(zr->overlay_mask + mask_line_size);
  753. btwrite(reg, ZR36057_MMBR);
  754. reg = mask_line_size - (zr->window.width + 31) / 32;
  755. if (DispMode == 0)
  756. reg += mask_line_size;
  757. reg <<= ZR36057_OCR_MaskStride;
  758. btwrite(reg, ZR36057_OCR);
  759. zr36057_adjust_vfe(zr, zr->codec_mode);
  760. }
  761. /*
  762.  * Switch overlay on or off
  763.  */
  764. static void zr36057_overlay(struct zoran *zr, int on)
  765. {
  766. int fmt, bpp;
  767. u32 reg;
  768. if (on) {
  769. /* do the necessary settings ... */
  770. btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR); /* switch it off first */
  771. switch (zr->buffer.depth) {
  772. case 15:
  773. fmt = VIDEO_PALETTE_RGB555;
  774. bpp = 2;
  775. break;
  776. case 16:
  777. fmt = VIDEO_PALETTE_RGB565;
  778. bpp = 2;
  779. break;
  780. case 24:
  781. fmt = VIDEO_PALETTE_RGB24;
  782. bpp = 3;
  783. break;
  784. case 32:
  785. fmt = VIDEO_PALETTE_RGB32;
  786. bpp = 4;
  787. break;
  788. default:
  789. fmt = 0;
  790. bpp = 0;
  791. }
  792. zr36057_set_vfe(zr, zr->window.width, zr->window.height,
  793. fmt);
  794. /* Start and length of each line MUST be 4-byte aligned.
  795.    This should be allready checked before the call to this routine.
  796.    All error messages are internal driver checking only! */
  797. /* video display top and bottom registers */
  798. reg =
  799.     (u32) zr->buffer.base + zr->window.x * bpp +
  800.     zr->window.y * zr->buffer.bytesperline;
  801. btwrite(reg, ZR36057_VDTR);
  802. if (reg & 3)
  803. printk(KERN_ERR
  804.        "%s: zr36057_overlay: video_address not alignedn",
  805.        zr->name);
  806. if (zr->video_interlace)
  807. reg += zr->buffer.bytesperline;
  808. btwrite(reg, ZR36057_VDBR);
  809. /* video stride, status, and frame grab register */
  810. reg = zr->buffer.bytesperline - zr->window.width * bpp;
  811. if (zr->video_interlace)
  812. reg += zr->buffer.bytesperline;
  813. if (reg & 3)
  814. printk(KERN_ERR
  815.        "%s: zr36057_overlay: video_stride not alignedn",
  816.        zr->name);
  817. reg = (reg << ZR36057_VSSFGR_DispStride);
  818. reg |= ZR36057_VSSFGR_VidOvf; /* clear overflow status */
  819. btwrite(reg, ZR36057_VSSFGR);
  820. /* Set overlay clipping */
  821. if (zr->window.clipcount)
  822. btor(ZR36057_OCR_OvlEnable, ZR36057_OCR);
  823. /* ... and switch it on */
  824. btor(ZR36057_VDCR_VidEn, ZR36057_VDCR);
  825. } else {
  826. /* Switch it off */
  827. btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
  828. }
  829. }
  830. /*
  831.  * The overlay mask has one bit for each pixel on a scan line,
  832.  *  and the maximum window size is BUZ_MAX_WIDTH * BUZ_MAX_HEIGHT pixels.
  833.  */
  834. static void write_overlay_mask(struct zoran *zr, struct video_clip *vp,
  835.        int count)
  836. {
  837. unsigned mask_line_size = (BUZ_MAX_WIDTH + 31) / 32;
  838. u32 *mask;
  839. int x, y, width, height;
  840. unsigned i, j, k;
  841. u32 reg;
  842. /* fill mask with one bits */
  843. memset(zr->overlay_mask, ~0, mask_line_size * 4 * BUZ_MAX_HEIGHT);
  844. reg = 0;
  845. for (i = 0; i < count; ++i) {
  846. /* pick up local copy of clip */
  847. x = vp[i].x;
  848. y = vp[i].y;
  849. width = vp[i].width;
  850. height = vp[i].height;
  851. /* trim clips that extend beyond the window */
  852. if (x < 0) {
  853. width += x;
  854. x = 0;
  855. }
  856. if (y < 0) {
  857. height += y;
  858. y = 0;
  859. }
  860. if (x + width > zr->window.width) {
  861. width = zr->window.width - x;
  862. }
  863. if (y + height > zr->window.height) {
  864. height = zr->window.height - y;
  865. }
  866. /* ignore degenerate clips */
  867. if (height <= 0) {
  868. continue;
  869. }
  870. if (width <= 0) {
  871. continue;
  872. }
  873. /* apply clip for each scan line */
  874. for (j = 0; j < height; ++j) {
  875. /* reset bit for each pixel */
  876. /* this can be optimized later if need be */
  877. mask = zr->overlay_mask + (y + j) * mask_line_size;
  878. for (k = 0; k < width; ++k) {
  879. mask[(x + k) / 32] &=
  880.     ~((u32) 1 << (x + k) % 32);
  881. }
  882. }
  883. }
  884. }
  885. /* Enable/Disable uncompressed memory grabbing of the 36057 */
  886. static void zr36057_set_memgrab(struct zoran *zr, int mode)
  887. {
  888. if (mode) {
  889. if (btread(ZR36057_VSSFGR) &
  890.     (ZR36057_VSSFGR_SnapShot | ZR36057_VSSFGR_FrameGrab))
  891. printk(KERN_WARNING
  892.        "%s: zr36057_set_memgrab_on with SnapShot or FrameGrab on ???n",
  893.        zr->name);
  894. /* switch on VSync interrupts */
  895. btwrite(IRQ_MASK, ZR36057_ISR); // Clear Interrupts
  896. btor(cardvsync[zr->card], ZR36057_ICR); // SW
  897. /* enable SnapShot */
  898. btor(ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
  899. /* Set zr36057 video front end  and enable video */
  900. zr36057_set_vfe(zr, zr->gwidth, zr->gheight, zr->gformat);
  901. zr->v4l_memgrab_active = 1;
  902. } else {
  903. zr->v4l_memgrab_active = 0;
  904. /* switch off VSync interrupts */
  905. //btand(~ZR36057_ICR_GIRQ1, ZR36057_ICR); // SW
  906. /* reenable grabbing to screen if it was running */
  907. if (zr->v4l_overlay_active) {
  908. zr36057_overlay(zr, 1);
  909. } else {
  910. btand(~ZR36057_VDCR_VidEn, ZR36057_VDCR);
  911. btand(~ZR36057_VSSFGR_SnapShot, ZR36057_VSSFGR);
  912. }
  913. }
  914. }
  915. static int wait_grab_pending(struct zoran *zr)
  916. {
  917. unsigned long flags;
  918. /* wait until all pending grabs are finished */
  919. if (!zr->v4l_memgrab_active)
  920. return 0;
  921. while (zr->v4l_pend_tail != zr->v4l_pend_head) {
  922. interruptible_sleep_on(&zr->v4l_capq);
  923. if (signal_pending(current))
  924. return -ERESTARTSYS;
  925. }
  926. spin_lock_irqsave(&zr->lock, flags);
  927. zr36057_set_memgrab(zr, 0);
  928. spin_unlock_irqrestore(&zr->lock, flags);
  929. return 0;
  930. }
  931. /*
  932.  *   V4L Buffer grabbing
  933.  */
  934. static int v4l_grab(struct zoran *zr, struct video_mmap *mp)
  935. {
  936. unsigned long flags;
  937. int res, bpp;
  938. /*
  939.  * There is a long list of limitations to what is allowed to be grabbed
  940.  * We don't output error messages here, since some programs (e.g. xawtv)
  941.  * just try several settings to find out what is valid or not.
  942.  */
  943. /* No grabbing outside the buffer range! */
  944. if (mp->frame >= v4l_nbufs || mp->frame < 0) {
  945. DEBUG2(printk
  946.        (KERN_ERR "%s: Can not grab frame %dn", zr->name,
  947. mp->frame));
  948. return -EINVAL;
  949. }
  950. /* Check size and format of the grab wanted */
  951. if (mp->height < BUZ_MIN_HEIGHT || mp->width < BUZ_MIN_WIDTH
  952.     || mp->height > BUZ_MAX_HEIGHT || mp->width > BUZ_MAX_WIDTH) {
  953. DEBUG2(printk
  954.        (KERN_ERR "%s: Wrong frame size.n", zr->name));
  955. return -EINVAL;
  956. }
  957. bpp = format2bpp(mp->format);
  958. if (bpp == 0) {
  959. DEBUG2(printk
  960.        (KERN_ERR "%s: Wrong bytes-per-pixel formatn",
  961. zr->name));
  962. return -EINVAL;
  963. }
  964. /* Check against available buffer size */
  965. if (mp->height * mp->width * bpp > v4l_bufsize) {
  966. DEBUG2(printk
  967.        (KERN_ERR "%s: Video buffer size is too smalln",
  968. zr->name));
  969. return -EINVAL;
  970. }
  971. /* The video front end needs 4-byte alinged line sizes */
  972. if ((bpp == 2 && (mp->width & 1)) || (bpp == 3 && (mp->width & 3))) {
  973. DEBUG2(printk
  974.        (KERN_ERR "%s: Wrong frame alingmentn", zr->name));
  975. return -EINVAL;
  976. }
  977. /*
  978.  * To minimize the time spent in the IRQ routine, we avoid setting up
  979.  * the video front end there.
  980.  * If this grab has different parameters from a running streaming capture
  981.  * we stop the streaming capture and start it over again.
  982.  */
  983. if (zr->v4l_memgrab_active
  984.     && (zr->gwidth != mp->width || zr->gheight != mp->height
  985. || zr->gformat != mp->format)) {
  986. res = wait_grab_pending(zr);
  987. if (res)
  988. return res;
  989. }
  990. zr->gwidth = mp->width;
  991. zr->gheight = mp->height;
  992. zr->gformat = mp->format;
  993. zr->gbpl = bpp * zr->gwidth;
  994. spin_lock_irqsave(&zr->lock, flags);
  995. /* make sure a grab isn't going on currently with this buffer */
  996. switch (zr->v4l_gbuf[mp->frame].state) {
  997. default:
  998. case BUZ_STATE_PEND:
  999. res = -EBUSY; /* what are you doing? */
  1000. break;
  1001. case BUZ_STATE_USER:
  1002. case BUZ_STATE_DONE:
  1003. /* since there is at least one unused buffer there's room for at least one more pend[] entry */
  1004. zr->v4l_pend[zr->v4l_pend_head++ & V4L_MASK_FRAME] =
  1005.     mp->frame;
  1006. zr->v4l_gbuf[mp->frame].state = BUZ_STATE_PEND;
  1007. res = 0;
  1008. break;
  1009. }
  1010. /* put the 36057 into frame grabbing mode */
  1011. if (!res && !zr->v4l_memgrab_active)
  1012. zr36057_set_memgrab(zr, 1);
  1013. spin_unlock_irqrestore(&zr->lock, flags);
  1014. //DEBUG2(printk(KERN_INFO "%s: Frame grab 3...n", zr->name));
  1015. return res;
  1016. }
  1017. /*
  1018.  * Sync on a V4L buffer
  1019.  */
  1020. static int v4l_sync(struct zoran *zr, int frame)
  1021. {
  1022. unsigned long flags;
  1023. /* check passed-in frame number */
  1024. if (frame >= v4l_nbufs || frame < 0) {
  1025. DEBUG1(printk(KERN_ERR "%s: v4l_sync: frame %d is invalidn",
  1026.        zr->name, frame));
  1027. return -EINVAL;
  1028. }
  1029. /* Check if is buffer was queued at all */
  1030. if (zr->v4l_gbuf[frame].state == BUZ_STATE_USER) {
  1031. DEBUG1(printk(KERN_ERR
  1032.        "%s: v4l_sync: Attempt to sync on a buffer which was not queued?n",
  1033.        zr->name));
  1034. return -EPROTO;
  1035. }
  1036. /* wait on this buffer to get ready */
  1037. while (zr->v4l_gbuf[frame].state == BUZ_STATE_PEND) {
  1038. interruptible_sleep_on(&zr->v4l_capq);
  1039. if (signal_pending(current))
  1040. return -ERESTARTSYS;
  1041. }
  1042. /* buffer should now be in BUZ_STATE_DONE */
  1043. if (zr->v4l_gbuf[frame].state != BUZ_STATE_DONE)
  1044. printk(KERN_ERR "%s: v4l_sync - internal errorn",
  1045.        zr->name);
  1046. /* Check if streaming capture has finished */
  1047. spin_lock_irqsave(&zr->lock, flags);
  1048. if (zr->v4l_pend_tail == zr->v4l_pend_head)
  1049. zr36057_set_memgrab(zr, 0);
  1050. spin_unlock_irqrestore(&zr->lock, flags);
  1051. return 0;
  1052. }
  1053. /*****************************************************************************
  1054.  *                                                                           *
  1055.  *  Set up the Buz-specific MJPEG part                                       *
  1056.  *                                                                           *
  1057.  *****************************************************************************/
  1058. /*
  1059. Wait til post office is no longer busy */
  1060. static int post_office_wait(struct zoran *zr)
  1061. {
  1062. u32 por;
  1063. //      while (((por = btread(ZR36057_POR)) & (ZR36057_POR_POPen | ZR36057_POR_POTime)) == ZR36057_POR_POPen) {
  1064. while ((por = btread(ZR36057_POR)) & ZR36057_POR_POPen) {
  1065. /* wait for something to happen */
  1066. }
  1067. if ((por & ZR36057_POR_POTime) && zr->card != LML33
  1068.     && zr->card != BUZ) {
  1069. /* In LML33/BUZ GWS line is not connected, so it has always timeout set */
  1070. printk(KERN_WARNING "%s: pop timeout %08xn", zr->name, por);
  1071. return -1;
  1072. }
  1073. return 0;
  1074. }
  1075. static int post_office_write(struct zoran *zr, unsigned guest,
  1076.      unsigned reg, unsigned value)
  1077. {
  1078. u32 por;
  1079. por =
  1080.     ZR36057_POR_PODir | ZR36057_POR_POTime | ((guest & 7) << 20) |
  1081.     ((reg & 7) << 16) | (value & 0xFF);
  1082. btwrite(por, ZR36057_POR);
  1083. return post_office_wait(zr);
  1084. }
  1085. static int post_office_read(struct zoran *zr, unsigned guest, unsigned reg)
  1086. {
  1087. u32 por;
  1088. por = ZR36057_POR_POTime | ((guest & 7) << 20) | ((reg & 7) << 16);
  1089. btwrite(por, ZR36057_POR);
  1090. if (post_office_wait(zr) < 0) {
  1091. return -1;
  1092. }
  1093. return btread(ZR36057_POR) & 0xFF;
  1094. }
  1095. static int zr36060_write_8(struct zoran *zr, unsigned reg, unsigned val)
  1096. {
  1097. if (post_office_wait(zr)
  1098.     || post_office_write(zr, 0, 1, reg >> 8)
  1099.     || post_office_write(zr, 0, 2, reg)) {
  1100. return -1;
  1101. }
  1102. return post_office_write(zr, 0, 3, val);
  1103. }
  1104. static int zr36060_write_16(struct zoran *zr, unsigned reg, unsigned val)
  1105. {
  1106. if (zr36060_write_8(zr, reg + 0, val >> 8)) {
  1107. return -1;
  1108. }
  1109. return zr36060_write_8(zr, reg + 1, val >> 0);
  1110. }
  1111. static int zr36060_write_24(struct zoran *zr, unsigned reg, unsigned val)
  1112. {
  1113. if (zr36060_write_8(zr, reg + 0, val >> 16)) {
  1114. return -1;
  1115. }
  1116. return zr36060_write_16(zr, reg + 1, val >> 0);
  1117. }
  1118. static int zr36060_write_32(struct zoran *zr, unsigned reg, unsigned val)
  1119. {
  1120. if (zr36060_write_16(zr, reg + 0, val >> 16)) {
  1121. return -1;
  1122. }
  1123. return zr36060_write_16(zr, reg + 2, val >> 0);
  1124. }
  1125. static u32 zr36060_read_8(struct zoran *zr, unsigned reg)
  1126. {
  1127. if (post_office_wait(zr)
  1128.     || post_office_write(zr, 0, 1, reg >> 8)
  1129.     || post_office_write(zr, 0, 2, reg)) {
  1130. return -1;
  1131. }
  1132. return post_office_read(zr, 0, 3) & 0xFF;
  1133. }
  1134. /* ----------------------------------------------------------------------- */
  1135. static void GPIO(struct zoran *zr, unsigned bit, unsigned value)
  1136. {
  1137. u32 reg;
  1138. u32 mask;
  1139. mask = 1 << (24 + bit);
  1140. reg = btread(ZR36057_GPPGCR1) & ~mask;
  1141. if (value) {
  1142. reg |= mask;
  1143. }
  1144. btwrite(reg, ZR36057_GPPGCR1);
  1145. udelay(1);
  1146. }
  1147. static void zr36060_sleep(struct zoran *zr, int sleep)
  1148. {
  1149. switch (zr->card) {
  1150. case DC10:
  1151. case DC10plus:
  1152. GPIO(zr, 3, !sleep);
  1153. break;
  1154. case BUZ:
  1155. case LML33:
  1156. GPIO(zr, 1, !sleep);
  1157. break;
  1158. default:
  1159. break;
  1160. }
  1161. if (!sleep)
  1162. udelay(500);
  1163. else
  1164. udelay(2);
  1165. }
  1166. static int zr36060_reset(struct zoran *zr)
  1167. {
  1168. switch (zr->card) {
  1169. case DC10:
  1170. case DC10plus:
  1171. zr36060_sleep(zr, 0);
  1172. GPIO(zr, 0, 0);
  1173. udelay(2);
  1174. GPIO(zr, 0, 1);
  1175. udelay(2);
  1176. break;
  1177. case LML33:
  1178. case BUZ:
  1179. zr36060_sleep(zr, 0);
  1180. post_office_write(zr, 3, 0, 0);
  1181. udelay(2);
  1182. default:
  1183. }
  1184. return 0;
  1185. }
  1186. static void set_frame(struct zoran *zr, int val)
  1187. {
  1188. switch (zr->card) {
  1189. case DC10:
  1190. case DC10plus:
  1191. GPIO(zr, 6, val);
  1192. break;
  1193. case LML33:
  1194. case BUZ:
  1195. GPIO(zr, 3, val);
  1196. break;
  1197. default:
  1198. break;
  1199. }
  1200. }
  1201. static void set_videobus_dir(struct zoran *zr, int val)
  1202. {
  1203. switch (zr->card) {
  1204. case DC10:
  1205. case DC10plus:
  1206. GPIO(zr, 1, val);
  1207. break;
  1208. case LML33:
  1209. if (lml33dpath == 0)
  1210. GPIO(zr, 5, val);
  1211. else
  1212. GPIO(zr, 5, 1);
  1213. break;
  1214. case BUZ:
  1215. default:
  1216. break;
  1217. }
  1218. }
  1219. static void set_videobus_enable(struct zoran *zr, int val)
  1220. {
  1221. switch (zr->card) {
  1222. case LML33:
  1223. GPIO(zr, 7, val);
  1224. break;
  1225. case DC10:
  1226. case DC10plus:
  1227. case BUZ:
  1228. default:
  1229. break;
  1230. }
  1231. }
  1232. static void zr36060_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
  1233. {
  1234. struct tvnorm *tvn;
  1235. u32 reg;
  1236. int size, blocks;
  1237. reg = (1 << 0) /* CodeMstr */
  1238.     |(0 << 2) /* CFIS=0 */
  1239.     |(0 << 6) /* Endian=0 */
  1240.     |(0 << 7); /* Code16=0 */
  1241. zr36060_write_8(zr, 0x002, reg);
  1242. switch (mode) {
  1243. case BUZ_MODE_MOTION_DECOMPRESS:
  1244. case BUZ_MODE_STILL_DECOMPRESS:
  1245. reg = 0x00; /* Codec mode = decompression */
  1246. break;
  1247. case BUZ_MODE_MOTION_COMPRESS:
  1248. case BUZ_MODE_STILL_COMPRESS:
  1249. default:
  1250. reg = 0xa4; /* Codec mode = compression with variable scale factor */
  1251. break;
  1252. }
  1253. zr36060_write_8(zr, 0x003, reg);
  1254. reg = 0x00; /* reserved, mbz */
  1255. zr36060_write_8(zr, 0x004, reg);
  1256. /* code volume */
  1257. /* Target field size in pixels: */
  1258. tvn = zr->timing;
  1259. size =
  1260.     (tvn->Ha / 2) * (tvn->Wa) / (zr->params.HorDcm) /
  1261.     (zr->params.VerDcm);
  1262. blocks = size / 64;
  1263. /* Target compressed field size in bits: */
  1264. size = size * 16; /* uncompressed size in bits */
  1265. size = size * zr->params.quality / 400; /* quality = 100 is a compression ratio 1:4 */
  1266. /* Lower limit (arbitrary, 1 KB) */
  1267. if (size < 8192)
  1268. size = 8192;
  1269. /* Upper limit: 6/8 of the code buffers */
  1270. if (size * zr->params.field_per_buff > zr->jpg_bufsize * 6)
  1271. size = zr->jpg_bufsize * 6 / zr->params.field_per_buff;
  1272. reg = size * 4 / blocks;
  1273. if (reg > 0xf0)
  1274. reg = 0xf0; /* 480 bits/block, does 0xff represents unlimited? */
  1275. zr36060_write_8(zr, 0x005, reg);
  1276. /* JPEG markers */
  1277. reg = (zr->params.jpeg_markers) & 0x38; /* DRI, DQT, DHT */
  1278. if (zr->params.COM_len)
  1279. reg |= JPEG_MARKER_COM;
  1280. if (zr->params.APP_len)
  1281. reg |= JPEG_MARKER_APP;
  1282. zr36060_write_8(zr, 0x006, reg);
  1283. if (zr->card != LML33 && zr->card != BUZ) {
  1284. reg = (0 << 3) /* EOAV=0 */
  1285.     |(0 << 2) /* EOI=0 */
  1286.     |(0 << 1) /* END=0 */
  1287.     |(1 << 0); /* DATERR=1 */
  1288. } else {
  1289. reg = (0 << 3) /* EOAV=0 */
  1290.     |(0 << 2) /* EOI=0 */
  1291.     |(0 << 1) /* END=0 */
  1292.     |(0 << 0); /* DATERR=0 */
  1293. }
  1294. zr36060_write_8(zr, 0x007, reg);
  1295. reg = size;
  1296. zr36060_write_32(zr, 0x009, reg);
  1297. reg = (size * 10) / 11;
  1298. zr36060_write_32(zr, 0x00d, reg); // Not needed for compr. with variable scale factor, just in case ...
  1299. /* how do we set initial SF as a function of quality parameter? */
  1300. reg = 0x0100; /* SF=1.0 */
  1301. zr36060_write_16(zr, 0x011, reg);
  1302. reg = 0x00ffffff; /* AF=max */
  1303. zr36060_write_24(zr, 0x013, reg);
  1304. reg = 0x0000; /* test */
  1305. zr36060_write_16(zr, 0x024, reg);
  1306. //post_office_read(zr,1,0);
  1307. }
  1308. static void zr36060_set_video(struct zoran *zr, enum zoran_codec_mode mode)
  1309. {
  1310. struct tvnorm *tvn;
  1311. u32 reg;
  1312. unsigned HStart;
  1313. tvn = zr->timing;
  1314. reg = (0 << 7) /* Video8     */
  1315.     |(0 << 6) /* Range      */
  1316.     |(0 << 3) /* FlDet      */
  1317.     |(1 << 2) /* FlVedge    */
  1318.     |(0 << 1) /* FlExt      */
  1319.     |(0 << 0); /* SyncMstr   */
  1320. if (mode != BUZ_MODE_STILL_DECOMPRESS) {
  1321. /* limit pixels to range 16..235 as per CCIR-601 */
  1322. reg |= (1 << 6); /* Range=1 */
  1323. }
  1324. zr36060_write_8(zr, 0x030, reg);
  1325. switch (zr->card) {
  1326. case DC10:
  1327. case DC10plus:
  1328. reg = (0 << 7) /* VCLKPol   */
  1329.     |(0 << 6) /* PValPol   */
  1330.     |(0 << 5) /* PoePol    */
  1331.     |(0 << 4) /* SImgPol   */
  1332.     |(1 << 3) /* BLPol     */
  1333.     |(1 << 2) /* FlPol     */
  1334.     |(1 << 1) /* HSPol     */
  1335.     |(1 << 0); /* VSPol     */
  1336. break;
  1337. case LML33:
  1338. reg = (0 << 7) /* VCLKPol=0 */
  1339.     |(0 << 6) /* PValPol=0 */
  1340.     |(1 << 5) /* PoePol=1 */
  1341.     |(0 << 4) /* SImgPol=0 */
  1342.     |(0 << 3) /* BLPol=0 */
  1343.     |(0 << 2) /* FlPol=0 */
  1344.     |(0 << 1) /* HSPol=0, sync on falling edge */
  1345.     |(1 << 0); /* VSPol=1 */
  1346. break;
  1347. case BUZ:
  1348. default:
  1349. reg = (0 << 7) /* VCLKPol=0 */
  1350.     |(0 << 6) /* PValPol=0 */
  1351.     |(1 << 5) /* PoePol=1 */
  1352.     |(0 << 4) /* SImgPol=0 */
  1353.     |(0 << 3) /* BLPol=0 */
  1354.     |(0 << 2) /* FlPol=0 */
  1355.     |(1 << 1) /* HSPol=0, sync on falling edge */
  1356.     |(1 << 0); /* VSPol=1 */
  1357. break;
  1358. }
  1359. zr36060_write_8(zr, 0x031, reg);
  1360. switch (zr->params.HorDcm) {
  1361. default:
  1362. case 1:
  1363. reg = (0 << 0);
  1364. break; /* HScale = 0 */
  1365. case 2:
  1366. reg = (1 << 0);
  1367. break; /* HScale = 1 */
  1368. case 4:
  1369. reg = (2 << 0);
  1370. break; /* HScale = 2 */
  1371. }
  1372. if (zr->params.VerDcm == 2)
  1373. reg |= (1 << 2);
  1374. zr36060_write_8(zr, 0x032, reg);
  1375. reg = 0x00; /* BackY */
  1376. zr36060_write_8(zr, 0x033, reg);
  1377. reg = 0x80; /* BackU */
  1378. zr36060_write_8(zr, 0x034, reg);
  1379. reg = 0x80; /* BackV */
  1380. zr36060_write_8(zr, 0x035, reg);
  1381. /* sync generator */
  1382. reg = tvn->Ht - 1; /* Vtotal */
  1383. zr36060_write_16(zr, 0x036, reg);
  1384. reg = tvn->Wt - 1; /* Htotal */
  1385. zr36060_write_16(zr, 0x038, reg);
  1386. reg = 6 - 1; /* VsyncSize */
  1387. zr36060_write_8(zr, 0x03a, reg);
  1388. //reg   = 30 - 1;               /* HsyncSize */
  1389. reg = (zr->params.norm == 1 ? 57 : 68);
  1390. zr36060_write_8(zr, 0x03b, reg);
  1391. reg = tvn->VStart - 1; /* BVstart */
  1392. zr36060_write_8(zr, 0x03c, reg);
  1393. reg += tvn->Ha / 2; /* BVend */
  1394. zr36060_write_16(zr, 0x03e, reg);
  1395. reg = tvn->HStart + 64 - 1; /* BHstart */
  1396. zr36060_write_8(zr, 0x03d, reg);
  1397. reg += tvn->Wa; /* BHend */
  1398. zr36060_write_16(zr, 0x040, reg);
  1399. /* active area */
  1400. reg = zr->params.img_y + tvn->VStart; /* Vstart */
  1401. zr36060_write_16(zr, 0x042, reg);
  1402. reg += zr->params.img_height; /* Vend */
  1403. zr36060_write_16(zr, 0x044, reg);
  1404. HStart = tvn->HStart;
  1405. if (zr->card == BUZ) {
  1406. HStart += 44;
  1407. } else {
  1408. HStart += 64;
  1409. }
  1410. reg = zr->params.img_x + HStart; /* Hstart */
  1411. zr36060_write_16(zr, 0x046, reg);
  1412. reg += zr->params.img_width; /* Hend */
  1413. zr36060_write_16(zr, 0x048, reg);
  1414. /* subimage area */
  1415. reg = tvn->VStart - 4; /* SVstart */
  1416. zr36060_write_16(zr, 0x04a, reg);
  1417. reg += tvn->Ha / 2 + 8; /* SVend */
  1418. zr36060_write_16(zr, 0x04c, reg);
  1419. reg = tvn->HStart + 64 - 4; /* SHstart */
  1420. zr36060_write_16(zr, 0x04e, reg);
  1421. reg += tvn->Wa + 8; /* SHend */
  1422. zr36060_write_16(zr, 0x050, reg);
  1423. }
  1424. static void zr36060_set_jpg_SOF(struct zoran *zr)
  1425. {
  1426. u32 reg;
  1427. reg = 0xffc0; /* SOF marker */
  1428. zr36060_write_16(zr, 0x060, reg);
  1429. reg = 17; /* SOF length */
  1430. zr36060_write_16(zr, 0x062, reg);
  1431. reg = 8; /* precision 8 bits */
  1432. zr36060_write_8(zr, 0x064, reg);
  1433. reg = zr->params.img_height / zr->params.VerDcm; /* image height */
  1434. zr36060_write_16(zr, 0x065, reg);
  1435. reg = zr->params.img_width / zr->params.HorDcm; /* image width */
  1436. zr36060_write_16(zr, 0x067, reg);
  1437. reg = 3; /* 3 color components */
  1438. zr36060_write_8(zr, 0x069, reg);
  1439. reg = 0x002100; /* Y component */
  1440. zr36060_write_24(zr, 0x06a, reg);
  1441. reg = 0x011101; /* U component */
  1442. zr36060_write_24(zr, 0x06d, reg);
  1443. reg = 0x021101; /* V component */
  1444. zr36060_write_24(zr, 0x070, reg);
  1445. }
  1446. static void zr36060_set_jpg_SOS(struct zoran *zr)
  1447. {
  1448. u32 reg;
  1449. reg = 0xffda; /* SOS marker */
  1450. zr36060_write_16(zr, 0x07a, reg);
  1451. reg = 12; /* SOS length */
  1452. zr36060_write_16(zr, 0x07c, reg);
  1453. reg = 3; /* 3 color components */
  1454. zr36060_write_8(zr, 0x07e, reg);
  1455. reg = 0x0000; /* Y component */
  1456. zr36060_write_16(zr, 0x07f, reg);
  1457. reg = 0x0111; /* U component */
  1458. zr36060_write_16(zr, 0x081, reg);
  1459. reg = 0x0211; /* V component */
  1460. zr36060_write_16(zr, 0x083, reg);
  1461. reg = 0x003f00; /* Start, end spectral scans */
  1462. zr36060_write_24(zr, 0x085, reg);
  1463. }
  1464. static void zr36060_set_jpg_DRI(struct zoran *zr)
  1465. {
  1466. u32 reg;
  1467. reg = 0xffdd; /* DRI marker */
  1468. zr36060_write_16(zr, 0x0c0, reg);
  1469. reg = 4; /* DRI length */
  1470. zr36060_write_16(zr, 0x0c2, reg);
  1471. reg = 8; /* length in MCUs */
  1472. zr36060_write_16(zr, 0x0c4, reg);
  1473. }
  1474. static void zr36060_set_jpg_DQT(struct zoran *zr)
  1475. {
  1476. unsigned i;
  1477. unsigned adr;
  1478. static const u8 dqt[] = {
  1479. 0xff, 0xdb, /* DHT marker */
  1480. 0x00, 0x84, /* DHT length */
  1481. 0x00, /* table ID 0 */
  1482. 0x10, 0x0b, 0x0c, 0x0e, 0x0c, 0x0a, 0x10, 0x0e,
  1483. 0x0d, 0x0e, 0x12, 0x11, 0x10, 0x13, 0x18, 0x28,
  1484. 0x1a, 0x18, 0x16, 0x16, 0x18, 0x31, 0x23, 0x25,
  1485. 0x1d, 0x28, 0x3a, 0x33, 0x3d, 0x3c, 0x39, 0x33,
  1486. 0x38, 0x37, 0x40, 0x48, 0x5c, 0x4e, 0x40, 0x44,
  1487. 0x57, 0x45, 0x37, 0x38, 0x50, 0x6d, 0x51, 0x57,
  1488. 0x5f, 0x62, 0x67, 0x68, 0x67, 0x3e, 0x4d, 0x71,
  1489. 0x79, 0x70, 0x64, 0x78, 0x5c, 0x65, 0x67, 0x63,
  1490. 0x01, /* table ID 1 */
  1491. 0x11, 0x12, 0x12, 0x18, 0x15, 0x18, 0x2f, 0x1a,
  1492. 0x1a, 0x2f, 0x63, 0x42, 0x38, 0x42, 0x63, 0x63,
  1493. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1494. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1495. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1496. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1497. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63,
  1498. 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63, 0x63
  1499. };
  1500. /* write fixed quantitization tables */
  1501. adr = 0x0cc;
  1502. for (i = 0; i < sizeof(dqt); ++i) {
  1503. zr36060_write_8(zr, adr++, dqt[i]);
  1504. }
  1505. }
  1506. static void zr36060_set_jpg_DHT(struct zoran *zr)
  1507. {
  1508. unsigned i;
  1509. unsigned adr;
  1510. static const u8 dht[] = {
  1511. 0xff, 0xc4, /* DHT marker */
  1512. 0x01, 0xa2, /* DHT length */
  1513. 0x00, /* table class 0, ID 0 */
  1514. 0x00, 0x01, 0x05, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
  1515. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 8..16 */
  1516. 0x00, /* values for codes of length 2 */
  1517. 0x01, 0x02, 0x03, 0x04, 0x05, /* values for codes of length 3 */
  1518. 0x06, /* values for codes of length 4 */
  1519. 0x07, /* values for codes of length 5 */
  1520. 0x08, /* values for codes of length 6 */
  1521. 0x09, /* values for codes of length 7 */
  1522. 0x0a, /* values for codes of length 8 */
  1523. 0x0b, /* values for codes of length 9 */
  1524. 0x01, /* table class 0, ID 1 */
  1525. 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, /* # codes of length 1..8 */
  1526. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, /* # codes of length 9..16 */
  1527. 0x00, 0x01, 0x02, /* values for codes of length 2 */
  1528. 0x03, /* values for codes of length 3 */
  1529. 0x04, /* values for codes of length 4 */
  1530. 0x05, /* values for codes of length 5 */
  1531. 0x06, /* values for codes of length 6 */
  1532. 0x07, /* values for codes of length 7 */
  1533. 0x08, /* values for codes of length 8 */
  1534. 0x09, /* values for codes of length 9 */
  1535. 0x0a, /* values for codes of length 10 */
  1536. 0x0b, /* values for codes of length 11 */
  1537. 0x10,
  1538. 0x00, 0x02, 0x01, 0x03, 0x03, 0x02, 0x04, 0x03,
  1539. 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7d,
  1540. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  1541. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  1542. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  1543. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  1544. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  1545. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  1546. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  1547. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  1548. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  1549. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  1550. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  1551. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  1552. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  1553. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  1554. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  1555. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  1556. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  1557. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  1558. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  1559. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  1560. 0xf9, 0xfa, 0x11, 0x00, 0x02, 0x01, 0x02, 0x04,
  1561. 0x04, 0x03, 0x04, 0x07, 0x05, 0x04, 0x04, 0x00,
  1562. 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
  1563. 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51,
  1564. 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08,
  1565. 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23,
  1566. 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a,
  1567. 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18,
  1568. 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35,
  1569. 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45,
  1570. 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55,
  1571. 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65,
  1572. 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75,
  1573. 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84,
  1574. 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93,
  1575. 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2,
  1576. 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
  1577. 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
  1578. 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8,
  1579. 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7,
  1580. 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6,
  1581. 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5,
  1582. 0xf6, 0xf7, 0xf8, 0xf9, 0xfa
  1583. };
  1584. /* write fixed Huffman tables */
  1585. adr = 0x1d4;
  1586. for (i = 0; i < sizeof(dht); ++i) {
  1587. zr36060_write_8(zr, adr++, dht[i]);
  1588. }
  1589. }
  1590. static void zr36060_set_jpg_APP(struct zoran *zr)
  1591. {
  1592. unsigned adr;
  1593. int len, i;
  1594. u32 reg;
  1595. len = zr->params.APP_len;
  1596. if (len < 0)
  1597. len = 0;
  1598. if (len > 60)
  1599. len = 60;
  1600. i = zr->params.APPn;
  1601. if (i < 0)
  1602. i = 0;
  1603. if (i > 15)
  1604. i = 15;
  1605. reg = 0xffe0 + i; /* APPn marker */
  1606. zr36060_write_16(zr, 0x380, reg);
  1607. reg = len + 2; /* APPn len */
  1608. zr36060_write_16(zr, 0x382, reg);
  1609. /* write APPn data */
  1610. adr = 0x384;
  1611. for (i = 0; i < 60; i++) {
  1612. zr36060_write_8(zr, adr++,
  1613. (i < len ? zr->params.APP_data[i] : 0));
  1614. }
  1615. }
  1616. static void zr36060_set_jpg_COM(struct zoran *zr)
  1617. {
  1618. unsigned adr;
  1619. int len, i;
  1620. u32 reg;
  1621. len = zr->params.COM_len;
  1622. if (len < 0)
  1623. len = 0;
  1624. if (len > 60)
  1625. len = 60;
  1626. reg = 0xfffe; /* COM marker */
  1627. zr36060_write_16(zr, 0x3c0, reg);
  1628. reg = len + 2; /* COM len */
  1629. zr36060_write_16(zr, 0x3c2, reg);
  1630. /* write COM data */
  1631. adr = 0x3c4;
  1632. for (i = 0; i < 60; i++) {
  1633. zr36060_write_8(zr, adr++,
  1634. (i < len ? zr->params.COM_data[i] : 0));
  1635. }
  1636. }
  1637. static void zr36060_set_cap(struct zoran *zr, enum zoran_codec_mode mode)
  1638. {
  1639. unsigned i;
  1640. u32 reg;
  1641. zr36060_reset(zr);
  1642. mdelay(10);
  1643. reg = (0 << 7) /* Load=0 */
  1644.     |(1 << 0); /* SynRst=1 */
  1645. zr36060_write_8(zr, 0x000, reg);
  1646. zr36060_set_jpg(zr, mode);
  1647. zr36060_set_video(zr, mode);
  1648. zr36060_set_jpg_SOF(zr);
  1649. zr36060_set_jpg_SOS(zr);
  1650. zr36060_set_jpg_DRI(zr);
  1651. zr36060_set_jpg_DQT(zr);
  1652. zr36060_set_jpg_DHT(zr);
  1653. zr36060_set_jpg_APP(zr);
  1654. zr36060_set_jpg_COM(zr);
  1655. reg = (1 << 7) /* Load=1 */
  1656.     |(1 << 0); /* SynRst=0 */
  1657. zr36060_write_8(zr, 0x000, reg);
  1658. /* wait for codec to unbusy */
  1659. for (i = 0; i < 100000; ++i) {
  1660. reg = zr36060_read_8(zr, 0x001);
  1661. if ((reg & (1 << 7)) == 0) {
  1662. return;
  1663. }
  1664. //udelay(100);
  1665. }
  1666. printk(KERN_ERR "%sZR36060: stuck busy, statux=%02xn", zr->name,
  1667.        reg);
  1668. }
  1669. static void init_jpeg_queue(struct zoran *zr)
  1670. {
  1671. int i;
  1672. /* re-initialize DMA ring stuff */
  1673. zr->jpg_que_head = 0;
  1674. zr->jpg_dma_head = 0;
  1675. zr->jpg_dma_tail = 0;
  1676. zr->jpg_que_tail = 0;
  1677. zr->jpg_seq_num = 0;
  1678. zr->JPEG_error = 0;
  1679. zr->num_errors = 0;
  1680. zr->jpg_err_seq = 0;
  1681. zr->jpg_err_shift = 0;
  1682. zr->jpg_queued_num = 0;
  1683. for (i = 0; i < zr->jpg_nbufs; i++) {
  1684. zr->jpg_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
  1685. }
  1686. for (i = 0; i < BUZ_NUM_STAT_COM; i++) {
  1687. zr->stat_com[i] = 1; /* mark as unavailable to zr36057 */
  1688. }
  1689. }
  1690. static void zr36057_set_jpg(struct zoran *zr, enum zoran_codec_mode mode)
  1691. {
  1692. struct tvnorm *tvn;
  1693. u32 reg;
  1694. tvn = zr->timing;
  1695. /* assert P_Reset */
  1696. btwrite(0, ZR36057_JPC);
  1697. /* MJPEG compression mode */
  1698. switch (mode) {
  1699. case BUZ_MODE_MOTION_COMPRESS:
  1700. default:
  1701. reg = ZR36057_JMC_MJPGCmpMode;
  1702. break;
  1703. case BUZ_MODE_MOTION_DECOMPRESS:
  1704. reg = ZR36057_JMC_MJPGExpMode;
  1705. reg |= ZR36057_JMC_SyncMstr;
  1706. /* RJ: The following is experimental - improves the output to screen */
  1707. //if(zr->params.VFIFO_FB) reg |= ZR36057_JMC_VFIFO_FB; // No, it doesn't. SM
  1708. break;
  1709. case BUZ_MODE_STILL_COMPRESS:
  1710. reg = ZR36057_JMC_JPGCmpMode;
  1711. break;
  1712. case BUZ_MODE_STILL_DECOMPRESS:
  1713. reg = ZR36057_JMC_JPGExpMode;
  1714. break;
  1715. }
  1716. reg |= ZR36057_JMC_JPG;
  1717. if (zr->params.field_per_buff == 1)
  1718. reg |= ZR36057_JMC_Fld_per_buff;
  1719. btwrite(reg, ZR36057_JMC);
  1720. /* vertical */
  1721. btor(ZR36057_VFEVCR_VSPol, ZR36057_VFEVCR);
  1722. reg =
  1723.     (6 << ZR36057_VSP_VsyncSize) | (tvn->Ht << ZR36057_VSP_FrmTot);
  1724. btwrite(reg, ZR36057_VSP);
  1725. reg = ((zr->params.img_y + tvn->VStart) << ZR36057_FVAP_NAY)
  1726.     | (zr->params.img_height << ZR36057_FVAP_PAY);
  1727. btwrite(reg, ZR36057_FVAP);
  1728. /* horizontal */
  1729. btor(ZR36057_VFEHCR_HSPol, ZR36057_VFEHCR);
  1730. reg =
  1731.     ((tvn->HSyncStart) << ZR36057_HSP_HsyncStart) | (tvn->
  1732.      Wt <<
  1733.      ZR36057_HSP_LineTot);
  1734. btwrite(reg, ZR36057_HSP);
  1735. reg = ((zr->params.img_x + tvn->HStart + 4) << ZR36057_FHAP_NAX)
  1736.     | (zr->params.img_width << ZR36057_FHAP_PAX);
  1737. btwrite(reg, ZR36057_FHAP);
  1738. /* field process parameters */
  1739. if (zr->params.odd_even)
  1740. reg = ZR36057_FPP_Odd_Even;
  1741. else
  1742. reg = 0;
  1743. if (mode == BUZ_MODE_MOTION_DECOMPRESS && zr->card != LML33
  1744.     && zr->card != BUZ)
  1745. reg ^= ZR36057_FPP_Odd_Even;
  1746. btwrite(reg, ZR36057_FPP);
  1747. /* Set proper VCLK Polarity, else colors will be wrong during playback */
  1748. //btor(ZR36057_VFESPFR_VCLKPol, ZR36057_VFESPFR);
  1749. /* code base address */
  1750. reg = virt_to_bus(zr->stat_com);
  1751. btwrite(reg, ZR36057_JCBA);
  1752. /* FIFO threshold (FIFO is 160. double words) */
  1753. /* NOTE: decimal values here */
  1754. switch (mode) {
  1755. case BUZ_MODE_STILL_COMPRESS:
  1756. case BUZ_MODE_MOTION_COMPRESS:
  1757. reg = 140;
  1758. break;
  1759. case BUZ_MODE_STILL_DECOMPRESS:
  1760. case BUZ_MODE_MOTION_DECOMPRESS:
  1761. reg = 20;
  1762. break;
  1763. default:
  1764. reg = 80;
  1765. break;
  1766. }
  1767. btwrite(reg, ZR36057_JCFT);
  1768. zr36057_adjust_vfe(zr, mode);
  1769. }
  1770. #if (DEBUGLEVEL > 2)
  1771. static void dump_guests(struct zoran *zr)
  1772. {
  1773. int i, guest[8];
  1774. for (i = 1; i < 8; i++) { // Don't read zr36060 here
  1775. guest[i] = post_office_read(zr, i, 0);
  1776. }
  1777. printk(KERN_INFO "%s: Guests:", zr->name);
  1778. for (i = 1; i < 8; i++) {
  1779. printk(" 0x%02x", guest[i]);
  1780. }
  1781. printk("n");
  1782. }
  1783. static unsigned long get_time(void)
  1784. {
  1785. struct timeval tv;
  1786. do_gettimeofday(&tv);
  1787. return (1000000 * tv.tv_sec + tv.tv_usec);
  1788. }
  1789. static void detect_guest_activity(struct zoran *zr)
  1790. {
  1791. int timeout, i, j, res, guest[8], guest0[8], change[8][3];
  1792. unsigned long t0, t1;
  1793. dump_guests(zr);
  1794. printk(KERN_INFO "%s: Detecting guests activity, please wait...n",
  1795.        zr->name);
  1796. for (i = 1; i < 8; i++) { // Don't read zr36060 here
  1797. guest0[i] = guest[i] = post_office_read(zr, i, 0);
  1798. }
  1799. timeout = 0;
  1800. j = 0;
  1801. t0 = get_time();
  1802. while (timeout < 10000) {
  1803. udelay(10);
  1804. timeout++;
  1805. for (i = 1; (i < 8) && (j < 8); i++) {
  1806. res = post_office_read(zr, i, 0);
  1807. if (res != guest[i]) {
  1808. t1 = get_time();
  1809. change[j][0] = (t1 - t0);
  1810. t0 = t1;
  1811. change[j][1] = i;
  1812. change[j][2] = res;
  1813. j++;
  1814. guest[i] = res;
  1815. }
  1816. }
  1817. if (j >= 8)
  1818. break;
  1819. }
  1820. printk(KERN_INFO "%s: Guests:", zr->name);
  1821. for (i = 1; i < 8; i++) {
  1822. printk(" 0x%02x", guest0[i]);
  1823. }
  1824. printk("n");
  1825. if (j == 0) {
  1826. printk(KERN_INFO "%s: No activity detected.n", zr->name);
  1827. return;
  1828. }
  1829. for (i = 0; i < j; i++) {
  1830. printk(KERN_INFO "%s: %6d: %d => 0x%02xn", zr->name,
  1831.        change[i][0], change[i][1], change[i][2]);
  1832. }
  1833. }
  1834. #endif
  1835. static void print_interrupts(struct zoran *zr)
  1836. {
  1837. int res, noerr;
  1838. noerr = 0;
  1839. printk(KERN_INFO "%s: interrupts received:", zr->name);
  1840. if ((res = zr->field_counter) < -1 || res > 1) {
  1841. printk(" FD:%d", res);
  1842. }
  1843. if ((res = zr->intr_counter_GIRQ1) != 0) {
  1844. printk(" GIRQ1:%d", res);
  1845. noerr++;
  1846. }
  1847. if ((res = zr->intr_counter_GIRQ0) != 0) {
  1848. printk(" GIRQ0:%d", res);
  1849. noerr++;
  1850. }
  1851. if ((res = zr->intr_counter_CodRepIRQ) != 0) {
  1852. printk(" CodRepIRQ:%d", res);
  1853. noerr++;
  1854. }
  1855. if ((res = zr->intr_counter_JPEGRepIRQ) != 0) {
  1856. printk(" JPEGRepIRQ:%d", res);
  1857. noerr++;
  1858. }
  1859. if (zr->JPEG_max_missed) {
  1860. printk(" JPEG delays: max=%d min=%d", zr->JPEG_max_missed,
  1861.        zr->JPEG_min_missed);
  1862. }
  1863. if (zr->END_event_missed) {
  1864. printk(" ENDs missed: %d", zr->END_event_missed);
  1865. }
  1866. //if (zr->jpg_queued_num) {
  1867. printk(" queue_state=%ld/%ld/%ld/%ld", zr->jpg_que_tail,
  1868.        zr->jpg_dma_tail, zr->jpg_dma_head, zr->jpg_que_head);
  1869. //}
  1870. if (!noerr) {
  1871. printk(": no interrupts detected.");
  1872. }
  1873. printk("n");
  1874. }
  1875. static void clear_interrupt_counters(struct zoran *zr)
  1876. {
  1877. zr->intr_counter_GIRQ1 = 0;
  1878. zr->intr_counter_GIRQ0 = 0;
  1879. zr->intr_counter_CodRepIRQ = 0;
  1880. zr->intr_counter_JPEGRepIRQ = 0;
  1881. zr->field_counter = 0;
  1882. zr->IRQ1_in = 0;
  1883. zr->IRQ1_out = 0;
  1884. zr->JPEG_in = 0;
  1885. zr->JPEG_out = 0;
  1886. zr->JPEG_0 = 0;
  1887. zr->JPEG_1 = 0;
  1888. zr->END_event_missed = 0;
  1889. zr->JPEG_missed = 0;
  1890. zr->JPEG_max_missed = 0;
  1891. zr->JPEG_min_missed = 0x7fffffff;
  1892. }
  1893. static u32 count_reset_interrupt(struct zoran *zr)
  1894. {
  1895. u32 isr;
  1896. if ((isr = btread(ZR36057_ISR) & 0x78000000)) {
  1897. if (isr & ZR36057_ISR_GIRQ1) {
  1898. btwrite(ZR36057_ISR_GIRQ1, ZR36057_ISR);
  1899. zr->intr_counter_GIRQ1++;
  1900. }
  1901. if (isr & ZR36057_ISR_GIRQ0) {
  1902. btwrite(ZR36057_ISR_GIRQ0, ZR36057_ISR);
  1903. zr->intr_counter_GIRQ0++;
  1904. }
  1905. if (isr & ZR36057_ISR_CodRepIRQ) {
  1906. btwrite(ZR36057_ISR_CodRepIRQ, ZR36057_ISR);
  1907. zr->intr_counter_CodRepIRQ++;
  1908. }
  1909. if (isr & ZR36057_ISR_JPEGRepIRQ) {
  1910. btwrite(ZR36057_ISR_JPEGRepIRQ, ZR36057_ISR);
  1911. zr->intr_counter_JPEGRepIRQ++;
  1912. }
  1913. }
  1914. return isr;
  1915. }
  1916. static void jpeg_start(struct zoran *zr)
  1917. {
  1918. int reg;
  1919. zr->frame_num = 0;
  1920. btwrite(ZR36057_JPC_P_Reset, ZR36057_JPC); // /P_Reset
  1921. btand(~ZR36057_MCTCR_CFlush, ZR36057_MCTCR); // CFlush
  1922. btor(ZR36057_JPC_CodTrnsEn, ZR36057_JPC); // /CodTrnsEn
  1923. btwrite(IRQ_MASK, ZR36057_ISR); // Clear IRQs
  1924. btwrite(IRQ_MASK | ZR36057_ICR_IntPinEn, ZR36057_ICR); // Enable IRQs
  1925. set_frame(zr, 0); // FRAME
  1926. /* JPEG codec guest ID */
  1927. reg =
  1928.     (1 << ZR36057_JCGI_JPEGuestID) | (0 <<
  1929.       ZR36057_JCGI_JPEGuestReg);
  1930. btwrite(reg, ZR36057_JCGI);
  1931. btor(ZR36057_JPC_Active, ZR36057_JPC); // /Active
  1932. btor(ZR36057_JMC_Go_en, ZR36057_JMC); // /Go_en
  1933. udelay(30);
  1934. set_frame(zr, 1); // /FRAME
  1935. }
  1936. static void zr36057_enable_jpg(struct zoran *zr,
  1937.        enum zoran_codec_mode mode)
  1938. {
  1939. static int zero = 0;
  1940. static int one = 1;
  1941. zr->codec_mode = mode;
  1942. switch (mode) {
  1943. case BUZ_MODE_MOTION_COMPRESS:
  1944. set_videobus_enable(zr, 0);
  1945. set_videobus_dir(zr, 0); // GPIO(zr, 1, 0);
  1946. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
  1947.    DECODER_ENABLE_OUTPUT, &one);
  1948. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
  1949.    ENCODER_SET_INPUT, &zero);
  1950. set_videobus_enable(zr, 1);
  1951. zr36060_sleep(zr, 0);
  1952. zr36060_set_cap(zr, mode); // Load ZR36060
  1953. init_jpeg_queue(zr);
  1954. zr36057_set_jpg(zr, mode); // P_Reset, ... Video param, FIFO
  1955. clear_interrupt_counters(zr);
  1956. DEBUG1(printk
  1957.        (KERN_INFO "%s: enable_jpg MOTION_COMPRESSn",
  1958. zr->name));
  1959. break;
  1960. case BUZ_MODE_MOTION_DECOMPRESS:
  1961. set_videobus_enable(zr, 0);
  1962. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
  1963.    DECODER_ENABLE_OUTPUT, &zero);
  1964. set_videobus_dir(zr, 1); // GPIO(zr, 1, 1);
  1965. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
  1966.    ENCODER_SET_INPUT, &one);
  1967. set_videobus_enable(zr, 1);
  1968. zr36060_sleep(zr, 0);
  1969. zr36060_set_cap(zr, mode); // Load ZR36060
  1970. init_jpeg_queue(zr);
  1971. zr36057_set_jpg(zr, mode); // P_Reset, ... Video param, FIFO
  1972. clear_interrupt_counters(zr);
  1973. DEBUG1(printk
  1974.        (KERN_INFO "%s: enable_jpg MOTION_DECOMPRESSn",
  1975. zr->name));
  1976. break;
  1977. case BUZ_MODE_IDLE:
  1978. default:
  1979. /* shut down processing */
  1980. btand(~(cardjpegint[zr->card] | ZR36057_ICR_JPEGRepIRQ),
  1981.       ZR36057_ICR);
  1982. btwrite(cardjpegint[zr->card] | ZR36057_ICR_JPEGRepIRQ,
  1983. ZR36057_ISR);
  1984. btand(~ZR36057_JMC_Go_en, ZR36057_JMC); // Go_en
  1985. set_current_state(TASK_UNINTERRUPTIBLE);
  1986. schedule_timeout(HZ/20);
  1987. set_videobus_dir(zr, 0); // GPIO(zr, 1, 0);
  1988. set_frame(zr, 1); //GPIO(zr, 6, 1);     // /FRAME
  1989. btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR); // /CFlush
  1990. btwrite(0, ZR36057_JPC); // P_Reset,CodTrnsEn,Active
  1991. btand(~ZR36057_JMC_VFIFO_FB, ZR36057_JMC);
  1992. btand(~ZR36057_JMC_SyncMstr, ZR36057_JMC);
  1993. zr36060_reset(zr);
  1994. zr36060_sleep(zr, 1);
  1995. zr36057_adjust_vfe(zr, mode);
  1996. set_videobus_enable(zr, 0);
  1997. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
  1998.    DECODER_ENABLE_OUTPUT, &one);
  1999. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
  2000.    ENCODER_SET_INPUT, &zero);
  2001. set_videobus_enable(zr, 1);
  2002. DEBUG1(printk
  2003.        (KERN_INFO "%s: enable_jpg IDLEn", zr->name));
  2004. break;
  2005. }
  2006. }
  2007. /*
  2008.  *   Queue a MJPEG buffer for capture/playback
  2009.  */
  2010. static int jpg_qbuf(struct zoran *zr, int frame,
  2011.     enum zoran_codec_mode mode)
  2012. {
  2013. unsigned long flags;
  2014. int res;
  2015. /* Check if buffers are allocated */
  2016. if (!zr->jpg_buffers_allocated) {
  2017. printk(KERN_ERR
  2018.        "%s: jpg_qbuf: buffers not yet allocatedn",
  2019.        zr->name);
  2020. return -ENOMEM;
  2021. }
  2022. /* Does the user want to stop streaming? */
  2023. if (frame < 0) {
  2024. if (zr->codec_mode == mode) {
  2025. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  2026. return 0;
  2027. } else {
  2028. printk(KERN_ERR
  2029.        "%s: jpg_qbuf - stop streaming but not in streaming moden",
  2030.        zr->name);
  2031. return -EINVAL;
  2032. }
  2033. }
  2034. /* No grabbing outside the buffer range! */
  2035. if (frame >= zr->jpg_nbufs) {
  2036. printk(KERN_ERR "%s: jpg_qbuf: buffer %d out of rangen",
  2037.        zr->name, frame);
  2038. return -EINVAL;
  2039. }
  2040. /* what is the codec mode right now? */
  2041. if (zr->codec_mode == BUZ_MODE_IDLE) {
  2042. /* Ok load up the zr36060 */
  2043. zr36057_enable_jpg(zr, mode);
  2044. } else if (zr->codec_mode != mode) {
  2045. /* wrong codec mode active - invalid */
  2046. printk(KERN_ERR "%s: jpg_qbuf - codec in wrong moden",
  2047.        zr->name);
  2048. return -EINVAL;
  2049. }
  2050. spin_lock_irqsave(&zr->lock, flags);
  2051. /* make sure a grab isn't going on currently with this buffer */
  2052. switch (zr->jpg_gbuf[frame].state) {
  2053. case BUZ_STATE_DONE:
  2054. DEBUG1(printk
  2055.        (KERN_WARNING
  2056. "%s: Warning: queing frame in BUZ_STATE_DONE staten",
  2057. zr->name));
  2058. case BUZ_STATE_USER:
  2059. /* since there is at least one unused buffer there's room for at least one more pend[] entry */
  2060. zr->jpg_pend[zr->jpg_que_head++ & BUZ_MASK_FRAME] = frame;
  2061. zr->jpg_gbuf[frame].state = BUZ_STATE_PEND;
  2062. zoran_feed_stat_com(zr);
  2063. res = 0;
  2064. break;
  2065. default:
  2066. case BUZ_STATE_DMA:
  2067. case BUZ_STATE_PEND:
  2068. res = -EBUSY; /* what are you doing? */
  2069. break;
  2070. }
  2071. spin_unlock_irqrestore(&zr->lock, flags);
  2072. /* Start the zr36060 when the first frame is queued  */
  2073. if (zr->jpg_que_head == 1)
  2074. jpeg_start(zr);
  2075. return res;
  2076. }
  2077. /*
  2078.  *   Sync on a MJPEG buffer
  2079.  */
  2080. static int jpg_sync(struct zoran *zr, struct zoran_sync *bs)
  2081. {
  2082. unsigned long flags;
  2083. int frame, timeout;
  2084. if (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS
  2085.     && zr->codec_mode != BUZ_MODE_MOTION_COMPRESS) {
  2086. DEBUG1(printk(KERN_ERR
  2087.        "%s: BUZIOCSYNC: - codec not in streaming moden",
  2088.        zr->name));
  2089. return -EINVAL;
  2090. }
  2091. while (zr->jpg_que_tail == zr->jpg_dma_tail) {
  2092. if (zr->jpg_dma_tail == zr->jpg_dma_head)
  2093. break;
  2094. timeout =
  2095.     interruptible_sleep_on_timeout(&zr->jpg_capq, 10 * HZ);
  2096. if (!timeout) {
  2097. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  2098. udelay(1);
  2099. printk(KERN_ERR
  2100.        "%s: timeout: codec isr=0x%02x, csr=0x%02xn",
  2101.        zr->name, zr36060_read_8(zr, 0x008),
  2102.        zr36060_read_8(zr, 0x001));
  2103. return -ETIME;
  2104. } else if (signal_pending(current))
  2105. return -ERESTARTSYS;
  2106. }
  2107. spin_lock_irqsave(&zr->lock, flags);
  2108. if (zr->jpg_dma_tail != zr->jpg_dma_head)
  2109. frame = zr->jpg_pend[zr->jpg_que_tail++ & BUZ_MASK_FRAME];
  2110. else
  2111. frame = zr->jpg_pend[zr->jpg_que_tail & BUZ_MASK_FRAME];
  2112. /* buffer should now be in BUZ_STATE_DONE */
  2113. #if(DEBUGLEVEL > 0)
  2114. if (zr->jpg_gbuf[frame].state != BUZ_STATE_DONE)
  2115. printk(KERN_ERR "%s: jpg_sync - internal errorn",
  2116.        zr->name);
  2117. #endif
  2118. *bs = zr->jpg_gbuf[frame].bs;
  2119. zr->jpg_gbuf[frame].state = BUZ_STATE_USER;
  2120. spin_unlock_irqrestore(&zr->lock, flags);
  2121. return 0;
  2122. }
  2123. /* when this is called the spinlock must be held */
  2124. static void zoran_feed_stat_com(struct zoran *zr)
  2125. {
  2126. /* move frames from pending queue to DMA */
  2127. int frame, i, max_stat_com;
  2128. max_stat_com =
  2129.     (zr->params.TmpDcm ==
  2130.      1) ? BUZ_NUM_STAT_COM : (BUZ_NUM_STAT_COM >> 1);
  2131. while ((zr->jpg_dma_head - zr->jpg_dma_tail) < max_stat_com
  2132.        && zr->jpg_dma_head < zr->jpg_que_head) {
  2133. frame = zr->jpg_pend[zr->jpg_dma_head & BUZ_MASK_FRAME];
  2134. if (zr->params.TmpDcm == 1) {
  2135. /* fill 1 stat_com entry */
  2136. i = (zr->jpg_dma_head -
  2137.      zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
  2138. if (!(zr->stat_com[i] & 1))
  2139. break;
  2140. zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
  2141. } else {
  2142. /* fill 2 stat_com entries */
  2143. i = ((zr->jpg_dma_head -
  2144.       zr->jpg_err_shift) & 1) * 2;
  2145. if (!(zr->stat_com[i] & 1))
  2146. break;
  2147. zr->stat_com[i] = zr->jpg_gbuf[frame].frag_tab_bus;
  2148. zr->stat_com[i + 1] =
  2149.     zr->jpg_gbuf[frame].frag_tab_bus;
  2150. }
  2151. zr->jpg_gbuf[frame].state = BUZ_STATE_DMA;
  2152. zr->jpg_dma_head++;
  2153. }
  2154. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
  2155. zr->jpg_queued_num++;
  2156. }
  2157. /* when this is called the spinlock must be held */
  2158. static void zoran_reap_stat_com(struct zoran *zr)
  2159. {
  2160. /* move frames from DMA queue to done queue */
  2161. int i;
  2162. u32 stat_com;
  2163. unsigned int seq;
  2164. unsigned int dif;
  2165. int frame;
  2166. struct zoran_gbuffer *gbuf;
  2167. /* In motion decompress we don't have a hardware frame counter,
  2168.    we just count the interrupts here */
  2169. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
  2170. zr->jpg_seq_num++;
  2171. }
  2172. while (zr->jpg_dma_tail < zr->jpg_dma_head) {
  2173. if (zr->params.TmpDcm == 1)
  2174. i = (zr->jpg_dma_tail -
  2175.      zr->jpg_err_shift) & BUZ_MASK_STAT_COM;
  2176. else
  2177. i = ((zr->jpg_dma_tail -
  2178.       zr->jpg_err_shift) & 1) * 2 + 1;
  2179. stat_com = zr->stat_com[i];
  2180. if ((stat_com & 1) == 0) {
  2181. return;
  2182. }
  2183. frame = zr->jpg_pend[zr->jpg_dma_tail & BUZ_MASK_FRAME];
  2184. gbuf = &zr->jpg_gbuf[frame];
  2185. do_gettimeofday(&gbuf->bs.timestamp);
  2186. if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS) {
  2187. gbuf->bs.length = (stat_com & 0x7fffff) >> 1;
  2188. /* update sequence number with the help of the counter in stat_com */
  2189. seq = ((stat_com >> 24) + zr->jpg_err_seq) & 0xff;
  2190. dif = (seq - zr->jpg_seq_num) & 0xff;
  2191. zr->jpg_seq_num += dif;
  2192. } else {
  2193. gbuf->bs.length = 0;
  2194. }
  2195. gbuf->bs.seq =
  2196.     zr->params.TmpDcm ==
  2197.     2 ? (zr->jpg_seq_num >> 1) : zr->jpg_seq_num;
  2198. gbuf->state = BUZ_STATE_DONE;
  2199. zr->jpg_dma_tail++;
  2200. }
  2201. }
  2202. static void error_handler(struct zoran *zr, u32 astat, u32 stat)
  2203. {
  2204. /* This is JPEG error handling part */
  2205. if ((zr->codec_mode != BUZ_MODE_MOTION_COMPRESS)
  2206.     && (zr->codec_mode != BUZ_MODE_MOTION_DECOMPRESS)) {
  2207. //printk(KERN_ERR "%s: Internal error: error handling request in mode %dn", zr->name, zr->codec_mode);
  2208. return;
  2209. }
  2210. if ((stat & 1) == 0
  2211.     && zr->codec_mode == BUZ_MODE_MOTION_COMPRESS
  2212.     && zr->jpg_dma_tail - zr->jpg_que_tail >= zr->jpg_nbufs) {
  2213. /* No free buffers... */
  2214. zoran_reap_stat_com(zr);
  2215. zoran_feed_stat_com(zr);
  2216. wake_up_interruptible(&zr->jpg_capq);
  2217. zr->JPEG_missed = 0;
  2218. return;
  2219. }
  2220. if (zr->JPEG_error != 1) {
  2221. /*
  2222.  * First entry: error just happened during normal operation
  2223.  * 
  2224.  * In BUZ_MODE_MOTION_COMPRESS:
  2225.  * 
  2226.  * Possible glitch in TV signal. In this case we should
  2227.  * stop the codec and wait for good quality signal before
  2228.  * restarting it to avoid further problems
  2229.  * 
  2230.  * In BUZ_MODE_MOTION_DECOMPRESS:
  2231.  * 
  2232.  * Bad JPEG frame: we have to mark it as processed (codec crashed
  2233.  * and was not able to do it itself), and to remove it from queue.
  2234.  */
  2235. btand(~ZR36057_JMC_Go_en, ZR36057_JMC);
  2236. udelay(1);
  2237. stat =
  2238.     stat | (post_office_read(zr, 7, 0) & 3) << 8 |
  2239.     zr36060_read_8(zr, 0x008);
  2240. btwrite(0, ZR36057_JPC);
  2241. btor(ZR36057_MCTCR_CFlush, ZR36057_MCTCR);
  2242. zr36060_reset(zr);
  2243. zr36060_sleep(zr, 1);
  2244. zr->JPEG_error = 1;
  2245. zr->num_errors++;
  2246. /* Report error */
  2247. #if(DEBUGLEVEL > 1)
  2248. if (zr->num_errors <= 8) {
  2249. long frame;
  2250. frame =
  2251.     zr->jpg_pend[zr->
  2252.  jpg_dma_tail & BUZ_MASK_FRAME];
  2253. printk(KERN_ERR
  2254.        "%s: JPEG error stat=0x%08x(0x%08x) queue_state=%ld/%ld/%ld/%ld seq=%ld frame=%ld. Codec stopped. ",
  2255.        zr->name, stat, zr->last_isr,
  2256.        zr->jpg_que_tail, zr->jpg_dma_tail,
  2257.        zr->jpg_dma_head, zr->jpg_que_head,
  2258.        zr->jpg_seq_num, frame);
  2259. printk("stat_com frames:");
  2260. {
  2261. int i, j;
  2262. for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
  2263. for (i = 0; i < zr->jpg_nbufs; i++) {
  2264. if (zr->stat_com[j] ==
  2265.     zr->jpg_gbuf[i].
  2266.     frag_tab_bus) {
  2267. printk("% d->%d",
  2268.        j, i);
  2269. }
  2270. }
  2271. }
  2272. printk("n");
  2273. }
  2274. }
  2275. #endif
  2276. /* Find an entry in stat_com and rotate contents */
  2277. {
  2278. int i;
  2279. if (zr->params.TmpDcm == 1)
  2280. i = (zr->jpg_dma_tail -
  2281.      zr->
  2282.      jpg_err_shift) & BUZ_MASK_STAT_COM;
  2283. else
  2284. i = ((zr->jpg_dma_tail -
  2285.       zr->jpg_err_shift) & 1) * 2;
  2286. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS) {
  2287. /* Mimic zr36067 operation */
  2288. zr->stat_com[i] |= 1;
  2289. if (zr->params.TmpDcm != 1)
  2290. zr->stat_com[i + 1] |= 1;
  2291. /* Refill */
  2292. zoran_reap_stat_com(zr);
  2293. zoran_feed_stat_com(zr);
  2294. wake_up_interruptible(&zr->jpg_capq);
  2295. /* Find an entry in stat_com again after refill */
  2296. if (zr->params.TmpDcm == 1)
  2297. i = (zr->jpg_dma_tail -
  2298.      zr->
  2299.      jpg_err_shift) &
  2300.     BUZ_MASK_STAT_COM;
  2301. else
  2302. i = ((zr->jpg_dma_tail -
  2303.       zr->jpg_err_shift) & 1) * 2;
  2304. }
  2305. if (i) {
  2306. /* Rotate stat_comm entries to make current entry first */
  2307. int j;
  2308. u32 bus_addr[BUZ_NUM_STAT_COM];
  2309. memcpy(bus_addr, zr->stat_com,
  2310.        sizeof(bus_addr));
  2311. for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
  2312. zr->stat_com[j] =
  2313.     bus_addr[(i +
  2314.       j) &
  2315.      BUZ_MASK_STAT_COM];
  2316. }
  2317. zr->jpg_err_shift += i;
  2318. zr->jpg_err_shift &= BUZ_MASK_STAT_COM;
  2319. }
  2320. if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
  2321. zr->jpg_err_seq = zr->jpg_seq_num; /* + 1; */
  2322. }
  2323. }
  2324. /* Now the stat_comm buffer is ready for restart */
  2325. {
  2326. int status;
  2327. status = 0;
  2328. if (zr->codec_mode == BUZ_MODE_MOTION_COMPRESS)
  2329. i2c_control_device(&zr->i2c,
  2330.    I2C_DRIVERID_VIDEODECODER,
  2331.    DECODER_GET_STATUS, &status);
  2332. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS
  2333.     || (status & DECODER_STATUS_GOOD)) {
  2334.     /********** RESTART code *************/
  2335. zr36060_reset(zr);
  2336. zr36060_set_cap(zr, zr->codec_mode);
  2337. zr36057_set_jpg(zr, zr->codec_mode);
  2338. jpeg_start(zr);
  2339. #if(DEBUGLEVEL > 1)
  2340. if (zr->num_errors <= 8)
  2341. printk(KERN_INFO "%s: Restartn",
  2342.        zr->name);
  2343. #endif
  2344. zr->JPEG_missed = 0;
  2345. zr->JPEG_error = 2;
  2346.     /********** End RESTART code ***********/
  2347. }
  2348. }
  2349. }
  2350. static void zoran_irq(int irq, void *dev_id, struct pt_regs *regs)
  2351. {
  2352. u32 stat, astat;
  2353. int count;
  2354. struct zoran *zr;
  2355. unsigned long flags;
  2356. zr = (struct zoran *) dev_id;
  2357. count = 0;
  2358. if (zr->testing) {
  2359. /* Testing interrupts */
  2360. spin_lock_irqsave(&zr->lock, flags);
  2361. while ((stat = count_reset_interrupt(zr))) {
  2362. if (count++ > 100) {
  2363. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  2364. printk(KERN_ERR
  2365.        "%s: IRQ lockup while testing, isr=0x%08x, cleared int maskn",
  2366.        zr->name, stat);
  2367. wake_up_interruptible(&zr->test_q);
  2368. }
  2369. }
  2370. zr->last_isr = stat;
  2371. spin_unlock_irqrestore(&zr->lock, flags);
  2372. return;
  2373. }
  2374. spin_lock_irqsave(&zr->lock, flags);
  2375. while (1) {
  2376. /* get/clear interrupt status bits */
  2377. stat = count_reset_interrupt(zr);
  2378. astat = stat & IRQ_MASK;
  2379. if (!astat) {
  2380. break;
  2381. }
  2382. if (astat & cardvsync[zr->card]) { // SW
  2383. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS
  2384.     || zr->codec_mode ==
  2385.     BUZ_MODE_MOTION_COMPRESS) {
  2386. /* count missed interrupts */
  2387. zr->JPEG_missed++;
  2388. }
  2389. //post_office_read(zr,1,0);
  2390. /* Interrupts may still happen when zr->v4l_memgrab_active is switched off.
  2391.    We simply ignore them */
  2392. if (zr->v4l_memgrab_active) {
  2393. /* A lot more checks should be here ... */
  2394. if ((btread(ZR36057_VSSFGR) &
  2395.      ZR36057_VSSFGR_SnapShot) == 0)
  2396. printk(KERN_WARNING
  2397.        "%s: BuzIRQ with SnapShot off ???n",
  2398.        zr->name);
  2399. if (zr->v4l_grab_frame != NO_GRAB_ACTIVE) {
  2400. /* There is a grab on a frame going on, check if it has finished */
  2401. if ((btread(ZR36057_VSSFGR) &
  2402.      ZR36057_VSSFGR_FrameGrab) ==
  2403.     0) {
  2404. /* it is finished, notify the user */
  2405. zr->v4l_gbuf[zr->
  2406.      v4l_grab_frame].
  2407.     state = BUZ_STATE_DONE;
  2408. zr->v4l_grab_frame =
  2409.     NO_GRAB_ACTIVE;
  2410. zr->v4l_grab_seq++;
  2411. zr->v4l_pend_tail++;
  2412. }
  2413. }
  2414. if (zr->v4l_grab_frame == NO_GRAB_ACTIVE)
  2415. wake_up_interruptible(&zr->
  2416.       v4l_capq);
  2417. /* Check if there is another grab queued */
  2418. if (zr->v4l_grab_frame == NO_GRAB_ACTIVE
  2419.     && zr->v4l_pend_tail !=
  2420.     zr->v4l_pend_head) {
  2421. int frame =
  2422.     zr->v4l_pend[zr->
  2423.  v4l_pend_tail &
  2424.  V4L_MASK_FRAME];
  2425. u32 reg;
  2426. zr->v4l_grab_frame = frame;
  2427. /* Set zr36057 video front end and enable video */
  2428. /* Buffer address */
  2429. reg =
  2430.     zr->v4l_gbuf[frame].
  2431.     fbuffer_bus;
  2432. btwrite(reg, ZR36057_VDTR);
  2433. if (zr->video_interlace)
  2434. reg += zr->gbpl;
  2435. btwrite(reg, ZR36057_VDBR);
  2436. /* video stride, status, and frame grab register */
  2437. reg = 0;
  2438. if (zr->video_interlace)
  2439. reg += zr->gbpl;
  2440. reg =
  2441.     (reg <<
  2442.      ZR36057_VSSFGR_DispStride);
  2443. reg |= ZR36057_VSSFGR_VidOvf;
  2444. reg |= ZR36057_VSSFGR_SnapShot;
  2445. reg |= ZR36057_VSSFGR_FrameGrab;
  2446. btwrite(reg, ZR36057_VSSFGR);
  2447. btor(ZR36057_VDCR_VidEn,
  2448.      ZR36057_VDCR);
  2449. }
  2450. }
  2451. }
  2452. #if (IRQ_MASK & ZR36057_ISR_CodRepIRQ)
  2453. if (astat & ZR36057_ISR_CodRepIRQ) {
  2454. zr->intr_counter_CodRepIRQ++;
  2455. IDEBUG(printk
  2456.        (KERN_DEBUG "%s: ZR36057_ISR_CodRepIRQn",
  2457. zr->name));
  2458. btand(~ZR36057_ICR_CodRepIRQ, ZR36057_ICR);
  2459. }
  2460. #endif /* (IRQ_MASK & ZR36057_ISR_CodRepIRQ) */
  2461. #if (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ)
  2462. if (astat & ZR36057_ISR_JPEGRepIRQ) {
  2463. if (zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS
  2464.     || zr->codec_mode ==
  2465.     BUZ_MODE_MOTION_COMPRESS) {
  2466. #if(DEBUGLEVEL > 1)
  2467. if (!zr->frame_num || zr->JPEG_error) {
  2468. printk(KERN_INFO
  2469.        "%s: first frame ready: state=0x%08x odd_even=%d field_per_buff=%d delay=%dn",
  2470.        zr->name, stat,
  2471.        zr->params.odd_even,
  2472.        zr->params.field_per_buff,
  2473.        zr->JPEG_missed);
  2474. {
  2475. char sc[] = "0000";
  2476. char sv[5];
  2477. int i;
  2478. strcpy(sv, sc);
  2479. for (i = 0; i < 4; i++) {
  2480. if (zr->
  2481.     stat_com[i] &
  2482.     1)
  2483. sv[i] =
  2484.     '1';
  2485. }
  2486. sv[4] = 0;
  2487. printk(KERN_INFO
  2488.        "%s: stat_com=%s queue_state=%ld/%ld/%ld/%ldn",
  2489.        zr->name, sv,
  2490.        zr->jpg_que_tail,
  2491.        zr->jpg_dma_tail,
  2492.        zr->jpg_dma_head,
  2493.        zr->jpg_que_head);
  2494. }
  2495. } else {
  2496. if (zr->JPEG_missed > zr->JPEG_max_missed) // Get statistics
  2497. zr->JPEG_max_missed =
  2498.     zr->JPEG_missed;
  2499. if (zr->JPEG_missed <
  2500.     zr->JPEG_min_missed)
  2501. zr->JPEG_min_missed =
  2502.     zr->JPEG_missed;
  2503. }
  2504. #endif
  2505. #if(DEBUGLEVEL > 2)
  2506. if (zr->frame_num < 6) {
  2507. int i;
  2508. printk("%s: seq=%ld stat_com:",
  2509.        zr->name, zr->jpg_seq_num);
  2510. for (i = 0; i < 4; i++) {
  2511. printk(" %08x",
  2512.        zr->stat_com[i]);
  2513. }
  2514. printk("n");
  2515. }
  2516. #endif
  2517. zr->frame_num++;
  2518. zr->JPEG_missed = 0;
  2519. zr->JPEG_error = 0;
  2520. zoran_reap_stat_com(zr);
  2521. zoran_feed_stat_com(zr);
  2522. wake_up_interruptible(&zr->jpg_capq);
  2523. } //else {
  2524. //      printk(KERN_ERR "%s: JPEG interrupt while not in motion (de)compress mode!n", zr->name);
  2525. //}
  2526. }
  2527. #endif /* (IRQ_MASK & ZR36057_ISR_JPEGRepIRQ) */
  2528. if ((astat & cardjpegint[zr->card]) /* DATERR interrupt received                 */
  2529.     ||zr->JPEG_missed > 25 /* Too many fields missed without processing */
  2530.     || zr->JPEG_error == 1 /* We are already in error processing        */
  2531.     || ((zr->codec_mode == BUZ_MODE_MOTION_DECOMPRESS)
  2532. && (zr->
  2533.     frame_num & (zr->JPEG_missed >
  2534.  zr->params.field_per_buff)))
  2535.     /* fields missed during decompression */
  2536.     ) {
  2537. error_handler(zr, astat, stat);
  2538. }
  2539. count++;
  2540. if (count > 10) {
  2541. printk(KERN_WARNING "%s: irq loop %dn", zr->name,
  2542.        count);
  2543. if (count > 20) {
  2544. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  2545. printk(KERN_ERR
  2546.        "%s: IRQ lockup, cleared int maskn",
  2547.        zr->name);
  2548. break;
  2549. }
  2550. }
  2551. zr->last_isr = stat;
  2552. }
  2553. spin_unlock_irqrestore(&zr->lock, flags);
  2554. }
  2555. /* Check a zoran_params struct for correctness, insert default params */
  2556. static int zoran_check_params(struct zoran *zr,
  2557.       struct zoran_params *params)
  2558. {
  2559. int err = 0, err0 = 0;
  2560. /* insert constant params */
  2561. params->major_version = MAJOR_VERSION;
  2562. params->minor_version = MINOR_VERSION;
  2563. /* Check input and norm: must be set by calling VIDIOCSCHAN only! */
  2564. params->norm = zr->params.norm;
  2565. params->input = zr->params.input;
  2566. /* Check decimation, set default values for decimation = 1, 2, 4 */
  2567. switch (params->decimation) {
  2568. case 1:
  2569. params->HorDcm = 1;
  2570. params->VerDcm = 1;
  2571. params->TmpDcm = 1;
  2572. params->field_per_buff = 2;
  2573. params->img_x = 0;
  2574. params->img_y = 0;
  2575. params->img_width = zr->timing->Wa;
  2576. params->img_height = zr->timing->Ha / 2;
  2577. break;
  2578. case 2:
  2579. params->HorDcm = 2;
  2580. params->VerDcm = 1;
  2581. params->TmpDcm = 2;
  2582. params->field_per_buff = 1;
  2583. params->img_x = 8;
  2584. params->img_y = 0;
  2585. params->img_width = zr->timing->Wa;
  2586. params->img_height = zr->timing->Ha / 2;
  2587. break;
  2588. case 4:
  2589. params->HorDcm = 4;
  2590. params->VerDcm = 2;
  2591. params->TmpDcm = 2;
  2592. params->field_per_buff = 1;
  2593. params->img_x = 8;
  2594. params->img_y = 0;
  2595. params->img_width = zr->timing->Wa;
  2596. params->img_height = zr->timing->Ha / 2;
  2597. break;
  2598. case 0:
  2599. /* We have to check the data the user has set */
  2600. if (params->HorDcm != 1 && params->HorDcm != 2
  2601.     && params->HorDcm != 4)
  2602. err0++;
  2603. if (params->VerDcm != 1 && params->VerDcm != 2)
  2604. err0++;
  2605. if (params->TmpDcm != 1 && params->TmpDcm != 2)
  2606. err0++;
  2607. if (params->field_per_buff != 1
  2608.     && params->field_per_buff != 2)
  2609. err0++;
  2610. if (params->img_x < 0)
  2611. err0++;
  2612. if (params->img_y < 0)
  2613. err0++;
  2614. if (params->img_width < 0)
  2615. err0++;
  2616. if (params->img_height < 0)
  2617. err0++;
  2618. if (params->img_x + params->img_width > zr->timing->Wa)
  2619. err0++;
  2620. if (params->img_y + params->img_height >
  2621.     zr->timing->Ha / 2)
  2622. err0++;
  2623. if (params->HorDcm) {
  2624. if (params->img_width % (16 * params->HorDcm) != 0)
  2625. err0++;
  2626. if (params->img_height % (8 * params->VerDcm) != 0)
  2627. err0++;
  2628. }
  2629. if (err0) {
  2630. DEBUG1(printk(KERN_ERR
  2631.        "%s: SET PARAMS: error in params for decimation = 0n",
  2632.        zr->name));
  2633. err++;
  2634. }
  2635. break;
  2636. default:
  2637. DEBUG1(printk(KERN_ERR
  2638.        "%s: SET PARAMS: decimation = %d, must be 0, 1, 2 or 4n",
  2639.        zr->name, params->decimation));
  2640. err++;
  2641. break;
  2642. }
  2643. if (params->quality > 100)
  2644. params->quality = 100;
  2645. if (params->quality < 5)
  2646. params->quality = 5;
  2647. if (params->APPn < 0)
  2648. params->APPn = 0;
  2649. if (params->APPn > 15)
  2650. params->APPn = 15;
  2651. if (params->APP_len < 0)
  2652. params->APP_len = 0;
  2653. if (params->APP_len > 60)
  2654. params->APP_len = 60;
  2655. if (params->COM_len < 0)
  2656. params->COM_len = 0;
  2657. if (params->COM_len > 60)
  2658. params->COM_len = 60;
  2659. if (err)
  2660. return -EINVAL;
  2661. return 0;
  2662. }
  2663. static void zoran_open_init_params(struct zoran *zr)
  2664. {
  2665. int i;
  2666. /* Per default, map the V4L Buffers */
  2667. zr->map_mjpeg_buffers = 0;
  2668. /* User must explicitly set a window */
  2669. zr->window_set = 0;
  2670. zr->window.x = 0;
  2671. zr->window.y = 0;
  2672. zr->window.width = 0;
  2673. zr->window.height = 0;
  2674. zr->window.chromakey = 0;
  2675. zr->window.flags = 0;
  2676. zr->window.clips = NULL;
  2677. zr->window.clipcount = 0;
  2678. zr->video_interlace = 0;
  2679. zr->v4l_memgrab_active = 0;
  2680. zr->v4l_overlay_active = 0;
  2681. zr->v4l_grab_frame = NO_GRAB_ACTIVE;
  2682. zr->v4l_grab_seq = 0;
  2683. zr->gwidth = 0;
  2684. zr->gheight = 0;
  2685. zr->gformat = 0;
  2686. zr->gbpl = 0;
  2687. /* DMA ring stuff for V4L */
  2688. zr->v4l_pend_tail = 0;
  2689. zr->v4l_pend_head = 0;
  2690. for (i = 0; i < v4l_nbufs; i++) {
  2691. zr->v4l_gbuf[i].state = BUZ_STATE_USER; /* nothing going on */
  2692. }
  2693. /* Set necessary params and call zoran_check_params to set the defaults */
  2694. zr->params.decimation = 1;
  2695. zr->params.quality = 50; /* default compression factor 8 */
  2696. if (zr->card != BUZ)
  2697. zr->params.odd_even = 1;
  2698. else
  2699. zr->params.odd_even = 0;
  2700. zr->params.APPn = 0;
  2701. zr->params.APP_len = 0; /* No APPn marker */
  2702. for (i = 0; i < 60; i++)
  2703. zr->params.APP_data[i] = 0;
  2704. zr->params.COM_len = 0; /* No COM marker */
  2705. for (i = 0; i < 60; i++)
  2706. zr->params.COM_data[i] = 0;
  2707. zr->params.VFIFO_FB = 0;
  2708. memset(zr->params.reserved, 0, sizeof(zr->params.reserved));
  2709. zr->params.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
  2710. i = zoran_check_params(zr, &zr->params);
  2711. if (i)
  2712. printk(KERN_ERR
  2713.        "%s: zoran_open_init_params internal errorn",
  2714.        zr->name);
  2715. clear_interrupt_counters(zr);
  2716. zr->testing = 0;
  2717. }
  2718. /*
  2719.  *   Open a zoran card. Right now the flags stuff is just playing
  2720.  */
  2721. static int zoran_open(struct video_device *dev, int flags)
  2722. {
  2723. struct zoran *zr = (struct zoran *) dev;
  2724. //int one = 1;
  2725. DEBUG1(printk
  2726.        (KERN_INFO "%s: zoran_open, %s pid=[%d]n", zr->name,
  2727. current->comm, current->pid));
  2728. switch (flags) {
  2729. case 0:
  2730. if (zr->user > 1) {
  2731. DEBUG1(printk(KERN_WARNING
  2732.        "%s: zoran_open: Buz is allready in usen",
  2733.        zr->name));
  2734. return -EBUSY;
  2735. }
  2736. zr->user++;
  2737. if (zr->user == 1 && v4l_fbuffer_alloc(zr) < 0) {
  2738. zr->user--;
  2739. printk(KERN_ERR
  2740.        "%s: zoran_open: v4l_fbuffer_alloc failedn",
  2741.        zr->name);
  2742. return -ENOMEM;
  2743. }
  2744. /* default setup */
  2745. if (zr->user == 1) { /* First device open */
  2746. zoran_open_init_params(zr);
  2747. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  2748. btwrite(IRQ_MASK, ZR36057_ISR); // Clears interrupts
  2749. btor(ZR36057_ICR_IntPinEn, ZR36057_ICR);
  2750. dev->busy = 0; /* Allow second open */
  2751. }
  2752. break;
  2753. default:
  2754. DEBUG1(printk(KERN_WARNING
  2755.        "%s: zoran_open: flags = 0x%x not yet supportedn",
  2756.        zr->name, flags));
  2757. return -EBUSY;
  2758. break;
  2759. }
  2760. MOD_INC_USE_COUNT;
  2761. return 0;
  2762. }
  2763. static void zoran_close(struct video_device *dev)
  2764. {
  2765. struct zoran *zr = (struct zoran *) dev;
  2766. int zero = 0, two = 2;
  2767. DEBUG1(printk
  2768.        (KERN_INFO "%s: zoran_close, %s pid=[%d]n", zr->name,
  2769. current->comm, current->pid));
  2770. /* Clean up JPEG process */
  2771. wake_up_interruptible(&zr->jpg_capq);
  2772. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  2773. jpg_fbuffer_free(zr);
  2774. zr->jpg_nbufs = 0;
  2775. if (zr->user == 1) { /* Last process */
  2776. /* disable interrupts */
  2777. btand(~ZR36057_ICR_IntPinEn, ZR36057_ICR);
  2778. #if(DEBUGLEVEL > 1)
  2779. print_interrupts(zr);
  2780. #endif
  2781. /* Overlay off */
  2782. wake_up_interruptible(&zr->v4l_capq);
  2783. zr36057_set_memgrab(zr, 0);
  2784. if (zr->v4l_overlay_active)
  2785. zr36057_overlay(zr, 0);
  2786. v4l_fbuffer_free(zr);
  2787. if (!pass_through) { /* Switch to color bar */
  2788. set_videobus_enable(zr, 0);
  2789. i2c_control_device(&zr->i2c,
  2790.    I2C_DRIVERID_VIDEODECODER,
  2791.    DECODER_ENABLE_OUTPUT, &zero);
  2792. i2c_control_device(&zr->i2c,
  2793.    I2C_DRIVERID_VIDEOENCODER,
  2794.    ENCODER_SET_INPUT, &two);
  2795. set_videobus_enable(zr, 1);
  2796. }
  2797. }
  2798. zr->user--;
  2799. MOD_DEC_USE_COUNT;
  2800. DEBUG2(printk(KERN_INFO "%s: zoran_close donen", zr->name));
  2801. }
  2802. static long zoran_read(struct video_device *dev, char *buf,
  2803.        unsigned long count, int nonblock)
  2804. {
  2805. return -EINVAL;
  2806. }
  2807. static long zoran_write(struct video_device *dev, const char *buf,
  2808. unsigned long count, int nonblock)
  2809. {
  2810. return -EINVAL;
  2811. }
  2812. /*
  2813.  *   ioctl routine
  2814.  */
  2815. static int do_zoran_ioctl(struct zoran *zr, unsigned int cmd,
  2816.        void *arg)
  2817. {
  2818. switch (cmd) {
  2819. case VIDIOCGCAP:
  2820. {
  2821. struct video_capability b;
  2822. DEBUG2(printk("%s: ioctl VIDIOCGCAPn", zr->name));
  2823. strncpy(b.name, zr->video_dev.name,
  2824. sizeof(b.name));
  2825. b.type =
  2826.     VID_TYPE_CAPTURE | VID_TYPE_OVERLAY |
  2827.     VID_TYPE_CLIPPING | VID_TYPE_FRAMERAM |
  2828.     VID_TYPE_SCALES;
  2829. /* theoretically we could also flag VID_TYPE_SUBCAPTURE
  2830.    but this is not even implemented in the BTTV driver */
  2831. if (zr->card == DC10 || zr->card == DC10plus) {
  2832. b.channels = 3; /* composite, svhs, internal */
  2833. } else {
  2834. b.channels = 2; /* composite, svhs */
  2835. }
  2836. b.audios = 0;
  2837. b.maxwidth = BUZ_MAX_WIDTH;
  2838. b.maxheight = BUZ_MAX_HEIGHT;
  2839. b.minwidth = BUZ_MIN_WIDTH;
  2840. b.minheight = BUZ_MIN_HEIGHT;
  2841. if (copy_to_user(arg, &b, sizeof(b))) {
  2842. return -EFAULT;
  2843. }
  2844. return 0;
  2845. }
  2846. break;
  2847. case VIDIOCGCHAN:
  2848. {
  2849. struct video_channel v;
  2850. if (copy_from_user(&v, arg, sizeof(v))) {
  2851. return -EFAULT;
  2852. }
  2853. DEBUG2(printk
  2854.        ("%s: ioctl VIDIOCGCHAN for channel %dn",
  2855. zr->name, v.channel));
  2856. switch (v.channel) {
  2857. case 0:
  2858. strcpy(v.name, "Composite");
  2859. break;
  2860. case 1:
  2861. strcpy(v.name, "SVHS");
  2862. break;
  2863. case 2:
  2864. if (zr->card == DC10
  2865.     || zr->card == DC10plus) {
  2866. strcpy(v.name, "Internal/comp");
  2867. break;
  2868. }
  2869. default:
  2870. DEBUG1(printk(KERN_ERR
  2871.        "%s: VIDIOCGCHAN on not existing channel %dn",
  2872.        zr->name, v.channel));
  2873. return -EINVAL;
  2874. }
  2875. v.tuners = 0;
  2876. v.flags = 0;
  2877. v.type = VIDEO_TYPE_CAMERA;
  2878. v.norm = zr->params.norm;
  2879. if (copy_to_user(arg, &v, sizeof(v))) {
  2880. return -EFAULT;
  2881. }
  2882. return 0;
  2883. }
  2884. break;
  2885. /* RJ: the documentation at http://roadrunner.swansea.linux.org.uk/v4lapi.shtml says:
  2886.  * "The VIDIOCSCHAN ioctl takes an integer argument and switches the capture to this input."
  2887.  *                                 ^^^^^^^
  2888.  * The famos BTTV driver has it implemented with a struct video_channel argument
  2889.  * and we follow it for compatibility reasons
  2890.  *
  2891.  * BTW: this is the only way the user can set the norm!
  2892.  */
  2893. case VIDIOCSCHAN:
  2894. {
  2895. struct video_channel v;
  2896. int input;
  2897. int on, res;
  2898. int encoder_norm;
  2899. if (copy_from_user(&v, arg, sizeof(v))) {
  2900. return -EFAULT;
  2901. }
  2902. if (zr->codec_mode != BUZ_MODE_IDLE) {
  2903. if (v.norm != zr->params.norm
  2904.     || v.channel != zr->params.input) {
  2905. DEBUG1(printk(KERN_ERR
  2906.        "%s: VIDIOCSCHAN called while the card in capture/playback moden",
  2907.        zr->name));
  2908. return -EINVAL;
  2909. } else {
  2910. DEBUG1(printk(BUZ_WARNING
  2911.        "%s: Warning: VIDIOCSCHAN called while the card in capture/playback moden",
  2912.        zr->name));
  2913. }
  2914. }
  2915. DEBUG2(printk
  2916.        ("%s: ioctl VIDIOCSCHAN: channel=%d, norm=%dn",
  2917. zr->name, v.channel, v.norm));
  2918. switch (v.channel) {
  2919. case 0:
  2920. if (zr->card == BUZ)
  2921. input = 3;
  2922. else
  2923. input = 0;
  2924. break;
  2925. case 1:
  2926. input = 7;
  2927. break;
  2928. case 2:
  2929. if (zr->card == DC10
  2930.     || zr->card == DC10plus) {
  2931. input = 5;
  2932. break;
  2933. }
  2934. default:
  2935. DEBUG1(printk(KERN_ERR
  2936.        "%s: VIDIOCSCHAN on not existing channel %dn",
  2937.        zr->name, v.channel));
  2938. return -EINVAL;
  2939. break;
  2940. }
  2941. if (lock_norm && v.norm != zr->params.norm) {
  2942. if (lock_norm > 1) {
  2943. DEBUG1(printk(KERN_WARNING
  2944.        "%s: VIDIOCSCHAN: TV standard is locked, can not switch norm.n",
  2945.        zr->name));
  2946. return -EINVAL;
  2947. } else {
  2948. DEBUG1(printk(KERN_WARNING
  2949.        "%s: VIDIOCSCHAN: TV standard is locked, norm was not changed.n",
  2950.        zr->name));
  2951. v.norm = zr->params.norm;
  2952. }
  2953. }
  2954. if(v.norm >= 2)
  2955. return -EINVAL;
  2956. if (!cardnorms[zr->card][v.norm]) {
  2957. DEBUG1(printk(KERN_ERR
  2958.        "%s: VIDIOCSCHAN with not supported norm %dn",
  2959.        zr->name, v.norm));
  2960. return -EOPNOTSUPP;
  2961. break;
  2962. }
  2963. encoder_norm = v.norm;
  2964. zr->params.norm = v.norm;
  2965. zr->params.input = v.channel;
  2966. zr->timing = cardnorms[zr->card][zr->params.norm];
  2967. /* We switch overlay off and on since a change in the norm
  2968.    needs different VFE settings */
  2969. on = zr->v4l_overlay_active
  2970.     && !zr->v4l_memgrab_active;
  2971. if (on)
  2972. zr36057_overlay(zr, 0);
  2973. set_videobus_enable(zr, 0);
  2974. i2c_control_device(&zr->i2c,
  2975.    I2C_DRIVERID_VIDEODECODER,
  2976.    DECODER_SET_INPUT, &input);
  2977. i2c_control_device(&zr->i2c,
  2978.    I2C_DRIVERID_VIDEODECODER,
  2979.    DECODER_SET_NORM,
  2980.    &zr->params.norm);
  2981. i2c_control_device(&zr->i2c,
  2982.    I2C_DRIVERID_VIDEOENCODER,
  2983.    ENCODER_SET_NORM,
  2984.    &encoder_norm);
  2985. set_videobus_enable(zr, 1);
  2986. if (on)
  2987. zr36057_overlay(zr, 1);
  2988. /* Make sure the changes come into effect */
  2989. res = wait_grab_pending(zr);
  2990. if (res)
  2991. return res;
  2992. return 0;
  2993. }
  2994. break;
  2995. case VIDIOCGTUNER:
  2996. {
  2997. DEBUG1(printk(KERN_ERR
  2998.        "%s: ioctl VIDIOCGTUNER not supportedn",
  2999.        zr->name));
  3000. return -EINVAL;
  3001. }
  3002. break;
  3003. case VIDIOCSTUNER:
  3004. {
  3005. DEBUG1(printk(KERN_ERR
  3006.        "%s: ioctl VIDIOCSTUNER not supportedn",
  3007.        zr->name));
  3008. return -EINVAL;
  3009. }
  3010. break;
  3011. case VIDIOCGPICT:
  3012. {
  3013. struct video_picture p = zr->picture;
  3014. DEBUG2(printk
  3015.        ("%s: ioctl VIDIOCGPICTn", zr->name));
  3016. p.depth = zr->buffer.depth;
  3017. switch (zr->buffer.depth) {
  3018. case 15:
  3019. p.palette = VIDEO_PALETTE_RGB555;
  3020. break;
  3021. case 16:
  3022. p.palette = VIDEO_PALETTE_RGB565;
  3023. break;
  3024. case 24:
  3025. p.palette = VIDEO_PALETTE_RGB24;
  3026. break;
  3027. case 32:
  3028. p.palette = VIDEO_PALETTE_RGB32;
  3029. break;
  3030. }
  3031. if (copy_to_user(arg, &p, sizeof(p))) {
  3032. return -EFAULT;
  3033. }
  3034. return 0;
  3035. }
  3036. break;
  3037. case VIDIOCSPICT:
  3038. {
  3039. struct video_picture p;
  3040. if (copy_from_user(&p, arg, sizeof(p))) {
  3041. return -EFAULT;
  3042. }
  3043. i2c_control_device(&zr->i2c,
  3044.    I2C_DRIVERID_VIDEODECODER,
  3045.    DECODER_SET_PICTURE, &p);
  3046. DEBUG2(printk
  3047.        ("%s: ioctl VIDIOCSPICT bri=%d hue=%d col=%d con=%d dep=%d pal=%dn",
  3048. zr->name, p.brightness, p.hue, p.colour,
  3049. p.contrast, p.depth, p.palette));
  3050. /* The depth and palette values have no meaning to us,
  3051.    should we return  -EINVAL if they don't fit ? */
  3052. zr->picture = p;
  3053. return 0;
  3054. }
  3055. break;
  3056. case VIDIOCCAPTURE:
  3057. {
  3058. int v, res;
  3059. if (copy_from_user(&v, arg, sizeof(v))) {
  3060. return -EFAULT;
  3061. }
  3062. DEBUG2(printk
  3063.        ("%s: ioctl VIDIOCCAPTURE: %dn", zr->name,
  3064. v));
  3065. /* If there is nothing to do, return immediatly */
  3066. if ((v && zr->v4l_overlay_active)
  3067.     || (!v && !zr->v4l_overlay_active))
  3068. return 0;
  3069. if (v == 0) {
  3070. zr->v4l_overlay_active = 0;
  3071. if (!zr->v4l_memgrab_active)
  3072. zr36057_overlay(zr, 0);
  3073. /* When a grab is running, the video simply won't be switched on any more */
  3074. } else {
  3075. if (!zr->buffer_set || !zr->window_set) {
  3076. DEBUG1(printk(KERN_ERR
  3077.        "%s: VIDIOCCAPTURE: buffer or window not setn",
  3078.        zr->name));
  3079. return -EINVAL;
  3080. }
  3081. zr->v4l_overlay_active = 1;
  3082. if (!zr->v4l_memgrab_active)
  3083. zr36057_overlay(zr, 1);
  3084. /* When a grab is running, the video will be switched on when grab is finished */
  3085. }
  3086. /* Make sure the changes come into effect */
  3087. res = wait_grab_pending(zr);
  3088. if (res)
  3089. return res;
  3090. return 0;
  3091. }
  3092. break;
  3093. case VIDIOCGWIN:
  3094. {
  3095. DEBUG2(printk("%s: ioctl VIDIOCGWINn", zr->name));
  3096. if (copy_to_user
  3097.     (arg, &zr->window, sizeof(zr->window))) {
  3098. return -EFAULT;
  3099. }
  3100. return 0;
  3101. }
  3102. break;
  3103. case VIDIOCSWIN:
  3104. {
  3105. struct video_clip *vcp;
  3106. struct video_window vw;
  3107. struct tvnorm *tvn;
  3108. int on, end, res, Wa, Ha;
  3109. tvn = zr->timing;
  3110. Wa = tvn->Wa;
  3111. Ha = tvn->Ha;
  3112. if (copy_from_user(&vw, arg, sizeof(vw))) {
  3113. return -EFAULT;
  3114. }
  3115. DEBUG2(printk
  3116.        ("%s: ioctl VIDIOCSWIN: x=%d y=%d w=%d h=%d clipcount=%dn",
  3117. zr->name, vw.x, vw.y, vw.width, vw.height,
  3118. vw.clipcount));
  3119. if (!zr->buffer_set) {
  3120. DEBUG1(printk(KERN_ERR
  3121.        "%s: VIDIOCSWIN: frame buffer has to be set firstn",
  3122.        zr->name));
  3123. return -EINVAL;
  3124. }
  3125. /*
  3126.  * The video front end needs 4-byte alinged line sizes, we correct that
  3127.  * silently here if necessary
  3128.  */
  3129. if (zr->buffer.depth == 15
  3130.     || zr->buffer.depth == 16) {
  3131. end = (vw.x + vw.width) & ~1; /* round down */
  3132. vw.x = (vw.x + 1) & ~1; /* round up */
  3133. vw.width = end - vw.x;
  3134. }
  3135. if (zr->buffer.depth == 24) {
  3136. end = (vw.x + vw.width) & ~3; /* round down */
  3137. vw.x = (vw.x + 3) & ~3; /* round up */
  3138. vw.width = end - vw.x;
  3139. }
  3140. if (vw.width > Wa)
  3141. vw.width = Wa;
  3142. if (vw.height > Ha)
  3143. vw.height = Ha;
  3144. /* Check for vaild parameters */
  3145. if (vw.width < BUZ_MIN_WIDTH
  3146.     || vw.height < BUZ_MIN_HEIGHT
  3147.     || vw.width > BUZ_MAX_WIDTH
  3148.     || vw.height > BUZ_MAX_HEIGHT) {
  3149. DEBUG1(printk(KERN_ERR
  3150.        "%s: VIDIOCSWIN: width = %d or height = %d invalidn",
  3151.        zr->name, vw.width, vw.height));
  3152. return -EINVAL;
  3153. }
  3154. zr->window.x = vw.x;
  3155. zr->window.y = vw.y;
  3156. zr->window.width = vw.width;
  3157. zr->window.height = vw.height;
  3158. zr->window.chromakey = 0;
  3159. zr->window.flags = 0; // RJ: Is this intended for interlace on/off ?
  3160. zr->window.clips = NULL;
  3161. /*
  3162.  * If an overlay is running, we have to switch it off
  3163.  * and switch it on again in order to get the new settings in effect.
  3164.  *
  3165.  * We also want to avoid that the overlay mask is written
  3166.  * when an overlay is running.
  3167.  */
  3168. on = zr->v4l_overlay_active
  3169.     && !zr->v4l_memgrab_active;
  3170. if (on)
  3171. zr36057_overlay(zr, 0);
  3172. /*
  3173.  *   Write the overlay mask if clips are wanted.
  3174.  */
  3175.  
  3176. if (vw.clipcount > 2048)
  3177. return -EINVAL;
  3178. if (vw.clipcount) {
  3179. vcp =
  3180.     vmalloc(sizeof(struct video_clip) *
  3181.     (vw.clipcount + 4));
  3182. if (vcp == NULL) {
  3183. printk(KERN_ERR
  3184.        "%s: zoran_ioctl: Alloc of clip mask failedn",
  3185.        zr->name);
  3186. return -ENOMEM;
  3187. }
  3188. if (copy_from_user
  3189.     (vcp, vw.clips,
  3190.      sizeof(struct video_clip) *
  3191.      vw.clipcount)) {
  3192. vfree(vcp);
  3193. return -EFAULT;
  3194. }
  3195. write_overlay_mask(zr, vcp, vw.clipcount);
  3196. vfree(vcp);
  3197. }
  3198. zr->window.clipcount = vw.clipcount;
  3199. if (on)
  3200. zr36057_overlay(zr, 1);
  3201. zr->window_set = 1;
  3202. /* Make sure the changes come into effect */
  3203. res = wait_grab_pending(zr);
  3204. if (res)
  3205. return res;
  3206. return 0;
  3207. }
  3208. break;
  3209. case VIDIOCGFBUF:
  3210. {
  3211. DEBUG2(printk
  3212.        ("%s: ioctl VIDIOCGFBUFn", zr->name));
  3213. if (copy_to_user
  3214.     (arg, &zr->buffer, sizeof(zr->buffer))) {
  3215. return -EFAULT;
  3216. }
  3217. return 0;
  3218. }
  3219. break;
  3220. case VIDIOCSFBUF:
  3221. {
  3222. struct video_buffer v;
  3223. /* RJ: Isn't this too restrictive? As long as the user doesn't set
  3224.    the base address it shouldn't be too dangerous */
  3225. if (!capable(CAP_SYS_ADMIN)) {
  3226. DEBUG1(printk(KERN_ERR
  3227.        "%s: Only the superuser may issue VIDIOCSFBUF ioctln",
  3228.        zr->name));
  3229. return -EPERM;
  3230. }
  3231. if (copy_from_user(&v, arg, sizeof(v))) {
  3232. return -EFAULT;
  3233. }
  3234. DEBUG2(printk
  3235.        ("%s: ioctl VIDIOCSFBUF: base=0x%x w=%d h=%d depth=%d bpl=%dn",
  3236. zr->name, (u32) v.base, v.width, v.height,
  3237. v.depth, v.bytesperline));
  3238. if (zr->v4l_overlay_active) {
  3239. /* Has the user gotten crazy ... ? */
  3240. DEBUG1(printk(KERN_ERR
  3241.        "%s: VIDIOCSFBUF not allowed when overlay activen",
  3242.        zr->name));
  3243. return -EINVAL;
  3244. }
  3245. if (v.depth != 15 && v.depth != 16 && v.depth != 24
  3246.     && v.depth != 32) {
  3247. DEBUG1(printk(KERN_ERR
  3248.        "%s: VIDIOCSFBUF: depth=%d not supportedn",
  3249.        zr->name, v.depth));
  3250. return -EINVAL;
  3251. }
  3252. if (v.height <= 0 || v.width <= 0
  3253.     || v.bytesperline <= 0) {
  3254. DEBUG1(printk(KERN_ERR
  3255.        "%s: VIDIOCSFBUF: invalid height/width/bpl valuen",
  3256.        zr->name));
  3257. return -EINVAL;
  3258. }
  3259. if (v.bytesperline & 3) {
  3260. DEBUG1(printk(KERN_ERR
  3261.        "%s: VIDIOCSFBUF: bytesperline must be 4-byte alignedn",
  3262.        zr->name));
  3263. return -EINVAL;
  3264. }
  3265. if (v.base) {
  3266. zr->buffer.base =
  3267.     (void *) ((unsigned long) v.base & ~3);
  3268. }
  3269. zr->buffer.height = v.height;
  3270. zr->buffer.width = v.width;
  3271. zr->buffer.depth = v.depth;
  3272. zr->buffer.bytesperline = v.bytesperline;
  3273. if (zr->buffer.base)
  3274. zr->buffer_set = 1;
  3275. zr->window_set = 0; /* The user should set new window parameters */
  3276. return 0;
  3277. }
  3278. break;
  3279. /* RJ: what is VIDIOCKEY intended to do ??? */
  3280. case VIDIOCKEY:
  3281. {
  3282. /* Will be handled higher up .. */
  3283. DEBUG2(printk("%s: ioctl VIDIOCKEYn", zr->name));
  3284. return 0;
  3285. }
  3286. break;
  3287. case VIDIOCGFREQ:
  3288. {
  3289. DEBUG1(printk(KERN_ERR
  3290.        "%s: ioctl VIDIOCGFREQ not supportedn",
  3291.        zr->name));
  3292. return -EINVAL;
  3293. }
  3294. break;
  3295. case VIDIOCSFREQ:
  3296. {
  3297. DEBUG1(printk(KERN_ERR
  3298.        "%s: ioctl VIDIOCSFREQ not supportedn",
  3299.        zr->name));
  3300. return -EINVAL;
  3301. }
  3302. break;
  3303. case VIDIOCGAUDIO:
  3304. {
  3305. DEBUG1(printk(KERN_ERR
  3306.        "%s: ioctl VIDIOCGAUDIO not supportedn",
  3307.        zr->name));
  3308. return -EINVAL;
  3309. }
  3310. break;
  3311. case VIDIOCSAUDIO:
  3312. {
  3313. DEBUG1(printk(KERN_ERR
  3314.        "%s: ioctl VIDIOCSAUDIO not supportedn",
  3315.        zr->name));
  3316. return -EINVAL;
  3317. }
  3318. break;
  3319. case VIDIOCSYNC:
  3320. {
  3321. int v;
  3322. if (copy_from_user(&v, arg, sizeof(v))) {
  3323. return -EFAULT;
  3324. }
  3325. DEBUG3(printk
  3326.        ("%s: ioctl VIDIOCSYNC %dn", zr->name, v));
  3327. return v4l_sync(zr, v);
  3328. }
  3329. break;
  3330. case VIDIOCMCAPTURE:
  3331. {
  3332. struct video_mmap vm;
  3333. if (copy_from_user
  3334.     ((void *) &vm, (void *) arg, sizeof(vm))) {
  3335. return -EFAULT;
  3336. }
  3337. DEBUG2(printk
  3338.        ("%s: ioctl VIDIOCMCAPTURE frame=%d geom=%dx%d fmt=%dn",
  3339. zr->name, vm.frame, vm.width, vm.height,
  3340. vm.format));
  3341. return v4l_grab(zr, &vm);
  3342. }
  3343. break;
  3344. case VIDIOCGMBUF:
  3345. {
  3346. struct video_mbuf vm;
  3347. int i;
  3348. DEBUG2(printk
  3349.        ("%s: ioctl VIDIOCGMBUFn", zr->name));
  3350. vm.size = v4l_nbufs * v4l_bufsize;
  3351. vm.frames = v4l_nbufs;
  3352. for (i = 0; i < v4l_nbufs; i++) {
  3353. vm.offsets[i] = i * v4l_bufsize;
  3354. }
  3355. /* The next mmap will map the V4L buffers */
  3356. zr->map_mjpeg_buffers = 0;
  3357. if (copy_to_user(arg, &vm, sizeof(vm))) {
  3358. return -EFAULT;
  3359. }
  3360. return 0;
  3361. }
  3362. break;
  3363. case VIDIOCGUNIT:
  3364. {
  3365. struct video_unit vu;
  3366. DEBUG2(printk
  3367.        ("%s: ioctl VIDIOCGUNITn", zr->name));
  3368. vu.video = zr->video_dev.minor;
  3369. vu.vbi = VIDEO_NO_UNIT;
  3370. vu.radio = VIDEO_NO_UNIT;
  3371. vu.audio = VIDEO_NO_UNIT;
  3372. vu.teletext = VIDEO_NO_UNIT;
  3373. if (copy_to_user(arg, &vu, sizeof(vu))) {
  3374. return -EFAULT;
  3375. }
  3376. return 0;
  3377. }
  3378. break;
  3379. /*
  3380.  * RJ: In principal we could support subcaptures for V4L grabbing.
  3381.  *     Not even the famous BTTV driver has them, however.
  3382.  *     If there should be a strong demand, one could consider
  3383.  *     to implement them.
  3384.  */
  3385. case VIDIOCGCAPTURE:
  3386. {
  3387. DEBUG1(printk(KERN_ERR
  3388.        "%s: ioctl VIDIOCGCAPTURE not supportedn",
  3389.        zr->name));
  3390. return -EINVAL;
  3391. }
  3392. break;
  3393. case VIDIOCSCAPTURE:
  3394. {
  3395. DEBUG1(printk(KERN_ERR
  3396.        "%s: ioctl VIDIOCSCAPTURE not supportedn",
  3397.        zr->name));
  3398. return -EINVAL;
  3399. }
  3400. break;
  3401. case BUZIOC_G_PARAMS:
  3402. {
  3403. DEBUG2(printk
  3404.        ("%s: ioctl BUZIOC_G_PARAMSn", zr->name));
  3405. if (copy_to_user
  3406.     (arg, &(zr->params), sizeof(zr->params))) {
  3407. return -EFAULT;
  3408. }
  3409. return 0;
  3410. }
  3411. break;
  3412. case BUZIOC_S_PARAMS:
  3413. {
  3414. struct zoran_params bp;
  3415. /* int input, on; */
  3416. if (zr->codec_mode != BUZ_MODE_IDLE) {
  3417. DEBUG1(printk(KERN_ERR
  3418.        "%s: BUZIOC_S_PARAMS called but Buz in capture/playback moden",
  3419.        zr->name));
  3420. return -EINVAL;
  3421. }
  3422. if (copy_from_user(&bp, arg, sizeof(bp))) {
  3423. return -EFAULT;
  3424. }
  3425. DEBUG2(printk
  3426.        ("%s: ioctl BUZIOC_S_PARAMSn", zr->name));
  3427. /* Check the params first before overwriting our internal values */
  3428. if (zoran_check_params(zr, &bp))
  3429. return -EINVAL;
  3430. zr->params = bp;
  3431. /* Make changes of input and norm go into effect immediatly */
  3432. /* We switch overlay off and on since a change in the norm
  3433.    needs different VFE settings */
  3434. if (copy_to_user(arg, &bp, sizeof(bp))) {
  3435. return -EFAULT;
  3436. }
  3437. return 0;
  3438. }
  3439. break;
  3440. case BUZIOC_REQBUFS:
  3441. {
  3442. struct zoran_requestbuffers br;
  3443. if (zr->jpg_buffers_allocated) {
  3444. DEBUG1(printk(KERN_ERR
  3445.        "%s: BUZIOC_REQBUFS: buffers allready allocatedn",
  3446.        zr->name));
  3447. return -EINVAL;
  3448. }
  3449. if (copy_from_user(&br, arg, sizeof(br))) {
  3450. return -EFAULT;
  3451. }
  3452. DEBUG2(printk
  3453.        ("%s: ioctl BUZIOC_REQBUFS count = %lu size=%lun",
  3454. zr->name, br.count, br.size));
  3455. /* Enforce reasonable lower and upper limits */
  3456. if (br.count < 4)
  3457. br.count = 4; /* Could be choosen smaller */
  3458. if (br.count > BUZ_MAX_FRAME)
  3459. br.count = BUZ_MAX_FRAME;
  3460. br.size = PAGE_ALIGN(br.size);
  3461. if (br.size < 8192)
  3462. br.size = 8192; /* Arbitrary */
  3463. /* br.size is limited by 1 page for the stat_com tables to a Maximum of 2 MB */
  3464. if (br.size > (512 * 1024))
  3465. br.size = (512 * 1024); /* 512 K should be enough */
  3466. if (zr->need_contiguous
  3467.     && br.size > MAX_KMALLOC_MEM)
  3468. br.size = MAX_KMALLOC_MEM;
  3469. zr->jpg_nbufs = br.count;
  3470. zr->jpg_bufsize = br.size;
  3471. if (jpg_fbuffer_alloc(zr))
  3472. return -ENOMEM;
  3473. /* The next mmap will map the MJPEG buffers */
  3474. zr->map_mjpeg_buffers = 1;
  3475. if (copy_to_user(arg, &br, sizeof(br))) {
  3476. return -EFAULT;
  3477. }
  3478. return 0;
  3479. }
  3480. break;
  3481. case BUZIOC_QBUF_CAPT:
  3482. {
  3483. int nb;
  3484. if (copy_from_user
  3485.     ((void *) &nb, (void *) arg, sizeof(int))) {
  3486. return -EFAULT;
  3487. }
  3488. DEBUG4(printk
  3489.        ("%s: ioctl BUZIOC_QBUF_CAPT %dn",
  3490. zr->name, nb));
  3491. return jpg_qbuf(zr, nb, BUZ_MODE_MOTION_COMPRESS);
  3492. }
  3493. break;
  3494. case BUZIOC_QBUF_PLAY:
  3495. {
  3496. int nb;
  3497. if (copy_from_user
  3498.     ((void *) &nb, (void *) arg, sizeof(int))) {
  3499. return -EFAULT;
  3500. }
  3501. DEBUG4(printk
  3502.        ("%s: ioctl BUZIOC_QBUF_PLAY %dn",
  3503. zr->name, nb));
  3504. return jpg_qbuf(zr, nb,
  3505. BUZ_MODE_MOTION_DECOMPRESS);
  3506. }
  3507. break;
  3508. case BUZIOC_SYNC:
  3509. {
  3510. struct zoran_sync bs;
  3511. int res;
  3512. DEBUG4(printk
  3513.        ("%s: ioctl BUZIOC_SYNCn", zr->name));
  3514. res = jpg_sync(zr, &bs);
  3515. if (copy_to_user(arg, &bs, sizeof(bs))) {
  3516. return -EFAULT;
  3517. }
  3518. return res;
  3519. }
  3520. break;
  3521. case BUZIOC_G_STATUS:
  3522. {
  3523. struct zoran_status bs;
  3524. int norm, input, status;
  3525. unsigned long timeout;
  3526. if (zr->codec_mode != BUZ_MODE_IDLE) {
  3527. DEBUG1(printk(KERN_ERR
  3528.        "%s: BUZIOC_G_STATUS called but Buz in capture/playback moden",
  3529.        zr->name));
  3530. return -EINVAL;
  3531. }
  3532. if (copy_from_user(&bs, arg, sizeof(bs))) {
  3533. return -EFAULT;
  3534. }
  3535. DEBUG2(printk
  3536.        ("%s: ioctl BUZIOC_G_STATUSn", zr->name));
  3537. switch (bs.input) {
  3538. case 0:
  3539. if (zr->card == BUZ)
  3540. input = 3;
  3541. else
  3542. input = 0;
  3543. break;
  3544. case 1:
  3545. input = 7;
  3546. break;
  3547. default:
  3548. DEBUG1(printk(KERN_ERR
  3549.        "%s: BUZIOC_G_STATUS on not existing input %dn",
  3550.        zr->name, bs.input));
  3551. return -EINVAL;
  3552. }
  3553. /* Set video norm to VIDEO_MODE_AUTO */
  3554. norm = VIDEO_MODE_AUTO;
  3555. set_videobus_enable(zr, 0);
  3556. i2c_control_device(&zr->i2c,
  3557.    I2C_DRIVERID_VIDEODECODER,
  3558.    DECODER_SET_INPUT, &input);
  3559. i2c_control_device(&zr->i2c,
  3560.    I2C_DRIVERID_VIDEODECODER,
  3561.    DECODER_SET_NORM, &norm);
  3562. set_videobus_enable(zr, 1);
  3563. /* sleep 1 second */
  3564. timeout = jiffies + 1 * HZ;
  3565. while (jiffies < timeout)
  3566. schedule();
  3567. /* Get status of video decoder */
  3568. i2c_control_device(&zr->i2c,
  3569.    I2C_DRIVERID_VIDEODECODER,
  3570.    DECODER_GET_STATUS, &status);
  3571. bs.signal = (status & DECODER_STATUS_GOOD) ? 1 : 0;
  3572. if (status & DECODER_STATUS_NTSC)
  3573. bs.norm = VIDEO_MODE_NTSC;
  3574. else if (status & DECODER_STATUS_SECAM)
  3575. bs.norm = VIDEO_MODE_SECAM;
  3576. else
  3577. bs.norm = VIDEO_MODE_PAL;
  3578. bs.color = (status & DECODER_STATUS_COLOR) ? 1 : 0;
  3579. /* restore previous input and norm */
  3580. if (zr->card == BUZ)
  3581. input = zr->params.input == 0 ? 3 : 7;
  3582. else
  3583. input = zr->params.input == 0 ? 0 : 7;
  3584. set_videobus_enable(zr, 0);
  3585. i2c_control_device(&zr->i2c,
  3586.    I2C_DRIVERID_VIDEODECODER,
  3587.    DECODER_SET_INPUT, &input);
  3588. i2c_control_device(&zr->i2c,
  3589.    I2C_DRIVERID_VIDEODECODER,
  3590.    DECODER_SET_NORM,
  3591.    &zr->params.norm);
  3592. set_videobus_enable(zr, 1);
  3593. if (copy_to_user(arg, &bs, sizeof(bs))) {
  3594. return -EFAULT;
  3595. }
  3596. return 0;
  3597. }
  3598. break;
  3599. default:
  3600. DEBUG1(printk
  3601.        ("%s: UNKNOWN ioctl cmd: 0x%xn", zr->name, cmd));
  3602. return -ENOIOCTLCMD;
  3603. }
  3604. return 0;
  3605. }
  3606. static int zoran_ioctl(struct video_device *dev, unsigned int cmd, void *arg)
  3607. {
  3608. struct zoran *zr = (struct zoran *) dev;
  3609. int err;
  3610. down(&zr->sem);
  3611. err = do_zoran_ioctl(zr, cmd, arg);
  3612. up(&zr->sem);
  3613. return err;
  3614. }
  3615. /*
  3616.  *   This maps the buffers to user space.
  3617.  *
  3618.  *   Depending on the state of zr->map_mjpeg_buffers
  3619.  *   the V4L or the MJPEG buffers are mapped
  3620.  *
  3621.  */
  3622. static int do_zoran_mmap(struct zoran *zr, const char *adr,
  3623.       unsigned long size)
  3624. {
  3625. unsigned long start = (unsigned long) adr;
  3626. unsigned long page, pos, todo, fraglen;
  3627. int i, j;
  3628. DEBUG2(printk
  3629.        (KERN_INFO "%s: mmap at 0x%08lx, size %lun", zr->name,
  3630. start, size));
  3631. if (zr->map_mjpeg_buffers) {
  3632. /* Map the MJPEG buffers */
  3633. if (!zr->jpg_buffers_allocated) {
  3634. DEBUG1(printk(KERN_ERR
  3635.        "%s: zoran_mmap(MJPEG): buffers not yet allocatedn",
  3636.        zr->name));
  3637. return -ENOMEM;
  3638. }
  3639. if (size > zr->jpg_nbufs * zr->jpg_bufsize) {
  3640. DEBUG1(printk(KERN_ERR
  3641.        "%s: zoran_mmap(MJPEG): Max size is %lu - you wanted %lun",
  3642.        zr->name, zr->jpg_nbufs * zr->jpg_bufsize,
  3643.        size));
  3644. return -EINVAL;
  3645. }
  3646. if (size != zr->jpg_nbufs * zr->jpg_bufsize)
  3647. DEBUG1(printk(KERN_WARNING
  3648.        "%s: zoran_mmap(MJPEG): Expected %lu - you wanted %lun",
  3649.        zr->name, zr->jpg_nbufs * zr->jpg_bufsize,
  3650.        size));
  3651. for (i = 0; i < zr->jpg_nbufs; i++) {
  3652. for (j = 0; j < zr->jpg_bufsize / PAGE_SIZE; j++) {
  3653. fraglen =
  3654.     (zr->jpg_gbuf[i].
  3655.      frag_tab[2 * j + 1] & ~1) << 1;
  3656. todo = size;
  3657. if (todo > fraglen)
  3658. todo = fraglen;
  3659. pos =
  3660.     (unsigned long) zr->jpg_gbuf[i].
  3661.     frag_tab[2 * j];
  3662. page = virt_to_phys(bus_to_virt(pos)); /* should just be pos on i386 */
  3663. if (remap_page_range
  3664.     (start, page, todo, PAGE_SHARED)) {
  3665. printk(KERN_ERR
  3666.        "%s: zoran_mmap(V4L): remap_page_range failedn",
  3667.        zr->name);
  3668. return -EAGAIN;
  3669. }
  3670. size -= todo;
  3671. start += todo;
  3672. if (size == 0)
  3673. break;
  3674. if (zr->jpg_gbuf[i].
  3675.     frag_tab[2 * j + 1] & 1)
  3676. break; /* was last fragment */
  3677. }
  3678. if (size == 0)
  3679. break;
  3680. }
  3681. } else {
  3682. /* Map the V4L buffers */
  3683. if (size > v4l_nbufs * v4l_bufsize) {
  3684. DEBUG1(printk(KERN_ERR
  3685.        "%s: zoran_mmap(V4L): Max size is %d - you wanted %ldn",
  3686.        zr->name, v4l_nbufs * v4l_bufsize, size));
  3687. return -EINVAL;
  3688. }
  3689. if (size != v4l_nbufs * v4l_bufsize)
  3690. DEBUG1(printk(KERN_WARNING
  3691.        "%s: zoran_mmap(V4L): Expected %d - you wanted %ldn",
  3692.        zr->name, v4l_nbufs * v4l_bufsize, size));
  3693. for (i = 0; i < v4l_nbufs; i++) {
  3694. todo = size;
  3695. if (todo > v4l_bufsize)
  3696. todo = v4l_bufsize;
  3697. page = zr->v4l_gbuf[i].fbuffer_phys;
  3698. DEBUG2(printk
  3699.        ("V4L remap page range %d 0x%lx %ld to 0x%lxn",
  3700. i, page, todo, start));
  3701. if (remap_page_range
  3702.     (start, page, todo, PAGE_SHARED)) {
  3703. printk(KERN_ERR
  3704.        "%s: zoran_mmap(V4L): remap_page_range failedn",
  3705.        zr->name);
  3706. return -EAGAIN;
  3707. }
  3708. size -= todo;
  3709. start += todo;
  3710. if (size == 0)
  3711. break;
  3712. }
  3713. }
  3714. return 0;
  3715. }
  3716. static int zoran_mmap(struct video_device *dev, const char *adr,
  3717.       unsigned long size)
  3718. {
  3719. int err;
  3720. struct zoran *zr = (struct zoran *) dev;
  3721. down(&zr->sem);
  3722. err = do_zoran_mmap(zr, adr, size);
  3723. up(&zr->sem);
  3724. return err;
  3725. }
  3726. static int zoran_init_done(struct video_device *dev)
  3727. {
  3728. return 0;
  3729. }
  3730. static struct video_device zoran_template = {
  3731. THIS_MODULE,
  3732. ZORAN_NAME,
  3733. VID_TYPE_CAPTURE | VID_TYPE_OVERLAY | VID_TYPE_CLIPPING |
  3734.     VID_TYPE_FRAMERAM | VID_TYPE_SCALES | VID_TYPE_SUBCAPTURE,
  3735. ZORAN_HARDWARE,
  3736. zoran_open,
  3737. zoran_close,
  3738. zoran_read,
  3739. zoran_write,
  3740. NULL,
  3741. zoran_ioctl,
  3742. zoran_mmap,
  3743. zoran_init_done,
  3744. NULL,
  3745. 0,
  3746. 0
  3747. };
  3748. /*
  3749.  * initialize video front end
  3750.  */
  3751. static void zr36057_init_vfe(struct zoran *zr)
  3752. {
  3753. u32 reg;
  3754. reg = btread(ZR36057_VFESPFR);
  3755. reg |= ZR36057_VFESPFR_LittleEndian;
  3756. reg &= ~ZR36057_VFESPFR_VCLKPol;
  3757. reg |= ZR36057_VFESPFR_ExtFl;
  3758. reg |= ZR36057_VFESPFR_TopField;
  3759. btwrite(reg, ZR36057_VFESPFR);
  3760. reg = btread(ZR36057_VDCR);
  3761. if (triton || zr->revision <= 1)
  3762. reg &= ~ZR36057_VDCR_Triton;
  3763. else
  3764. reg |= ZR36057_VDCR_Triton;
  3765. btwrite(reg, ZR36057_VDCR);
  3766. }
  3767. static void test_interrupts(struct zoran *zr)
  3768. {
  3769. int timeout, icr;
  3770. clear_interrupt_counters(zr);
  3771. zr->testing = 1;
  3772. icr = btread(ZR36057_ICR);
  3773. btwrite(0x78000000 | ZR36057_ICR_IntPinEn, ZR36057_ICR);
  3774. timeout = interruptible_sleep_on_timeout(&zr->test_q, 1 * HZ);
  3775. btwrite(0, ZR36057_ICR);
  3776. btwrite(0x78000000, ZR36057_ISR);
  3777. zr->testing = 0;
  3778. printk(KERN_INFO "%s: Testing interrupts...n", zr->name);
  3779. if (timeout) {
  3780. printk(": time spent: %dn", 1 * HZ - timeout);
  3781. }
  3782. print_interrupts(zr);
  3783. btwrite(icr, ZR36057_ICR);
  3784. }
  3785. static int zr36057_init(int i)
  3786. {
  3787. struct zoran *zr = &zoran[i];
  3788. unsigned long mem;
  3789. unsigned mem_needed;
  3790. int j;
  3791. int two = 2;
  3792. int zero = 0;
  3793. printk(KERN_INFO "%s: Initializing card[%d], zr=%xn", zr->name, i, (int) zr);
  3794. /* default setup of all parameters which will persist beetween opens */
  3795. zr->user = 0;
  3796. init_waitqueue_head(&zr->v4l_capq);
  3797. init_waitqueue_head(&zr->jpg_capq);
  3798. init_waitqueue_head(&zr->test_q);
  3799. zr->map_mjpeg_buffers = 0; /* Map V4L buffers by default */
  3800. zr->jpg_nbufs = 0;
  3801. zr->jpg_bufsize = 0;
  3802. zr->jpg_buffers_allocated = 0;
  3803. zr->buffer_set = 0; /* Flag if frame buffer has been set */
  3804. zr->buffer.base = (void *) vidmem;
  3805. zr->buffer.width = 0;
  3806. zr->buffer.height = 0;
  3807. zr->buffer.depth = 0;
  3808. zr->buffer.bytesperline = 0;
  3809. zr->params.norm = default_norm = (default_norm < 3 ? default_norm : VIDEO_MODE_PAL); /* Avoid nonsense settings from user */
  3810. if (!(zr->timing = cardnorms[zr->card][zr->params.norm])) {
  3811. printk(KERN_WARNING
  3812.        "%s: default TV statdard not supported by hardware. PAL will be used.n",
  3813.        zr->name);
  3814. zr->params.norm = VIDEO_MODE_PAL;
  3815. zr->timing = cardnorms[zr->card][zr->params.norm];
  3816. }
  3817. zr->params.input = default_input = (default_input ? 1 : 0); /* Avoid nonsense settings from user */
  3818. zr->video_interlace = 0;
  3819. /* Should the following be reset at every open ? */
  3820. zr->picture.colour = 32768;
  3821. zr->picture.brightness = 32768;
  3822. zr->picture.hue = 32768;
  3823. zr->picture.contrast = 32768;
  3824. zr->picture.whiteness = 0;
  3825. zr->picture.depth = 0;
  3826. zr->picture.palette = 0;
  3827. for (j = 0; j < VIDEO_MAX_FRAME; j++) {
  3828. zr->v4l_gbuf[i].fbuffer = 0;
  3829. zr->v4l_gbuf[i].fbuffer_phys = 0;
  3830. zr->v4l_gbuf[i].fbuffer_bus = 0;
  3831. }
  3832. zr->stat_com = 0;
  3833. /* default setup (will be repeated at every open) */
  3834. zoran_open_init_params(zr);
  3835. /* allocate memory *before* doing anything to the hardware in case allocation fails */
  3836. /* STAT_COM table and overlay mask */
  3837. mem_needed = (BUZ_NUM_STAT_COM + ((BUZ_MAX_WIDTH + 31) / 32) * BUZ_MAX_HEIGHT) * 4;
  3838. mem = (unsigned long) kmalloc(mem_needed, GFP_KERNEL);
  3839. if (!mem) {
  3840. printk(KERN_ERR "%s: zr36057_init: kmalloc (STAT_COM + ovl.mask) failedn", zr->name);
  3841. return -ENOMEM;
  3842. }
  3843. memset((void *) mem, 0, mem_needed);
  3844. zr->stat_com = (u32 *) mem;
  3845. for (j = 0; j < BUZ_NUM_STAT_COM; j++) {
  3846. zr->stat_com[j] = 1; /* mark as unavailable to zr36057 */
  3847. }
  3848. zr->overlay_mask = (u32 *) (mem + BUZ_NUM_STAT_COM * 4);
  3849. /* Initialize zr->jpg_gbuf */
  3850. for (j = 0; j < BUZ_MAX_FRAME; j++) {
  3851. zr->jpg_gbuf[j].frag_tab = 0;
  3852. zr->jpg_gbuf[j].frag_tab_bus = 0;
  3853. zr->jpg_gbuf[j].state = BUZ_STATE_USER;
  3854. zr->jpg_gbuf[j].bs.frame = j;
  3855. }
  3856. /*
  3857.  *   Now add the template and register the device unit.
  3858.  */
  3859. memcpy(&zr->video_dev, &zoran_template, sizeof(zoran_template));
  3860. strcpy(zr->video_dev.name, zr->name);
  3861. if (video_register_device(&zr->video_dev, VFL_TYPE_GRABBER, video_nr) < 0) {
  3862. i2c_unregister_bus(&zr->i2c);
  3863. kfree((void *) zr->stat_com);
  3864. return -1;
  3865. }
  3866. /* Enable bus-mastering */
  3867. pci_set_master(zr->pci_dev);
  3868. if (zr->card == BUZ)
  3869. j = zr->params.input == 0 ? 3 : 7;
  3870. else
  3871. j = zr->params.input == 0 ? 0 : 7;
  3872. set_videobus_enable(zr, 0);
  3873. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
  3874.    DECODER_SET_INPUT, &j);
  3875. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
  3876.    DECODER_SET_NORM, &zr->params.norm);
  3877. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
  3878.    ENCODER_SET_NORM, &zr->params.norm);
  3879. set_videobus_enable(zr, 1);
  3880. /* toggle JPEG codec sleep to sync PLL */
  3881. zr36060_sleep(zr, 1);
  3882. zr36060_sleep(zr, 0);
  3883. /* set individual interrupt enables (without GIRQ1)
  3884.    but don't global enable until zoran_open() */
  3885. //btwrite(IRQ_MASK & ~ZR36057_ISR_GIRQ1, ZR36057_ICR);  // SW
  3886. // It looks like using only JPEGRepIRQEn is not always reliable,
  3887. // may be when JPEG codec crashes it won't generate IRQ? So,
  3888. btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM
  3889. zr36057_init_vfe(zr);
  3890. zr->zoran_proc = NULL;
  3891. zr->initialized = 1;
  3892. zr36057_enable_jpg(zr, BUZ_MODE_IDLE);
  3893. #if (DEBUGLEVEL > 2)
  3894. detect_guest_activity(zr);
  3895. #endif
  3896. test_interrupts(zr);
  3897. btwrite(IRQ_MASK, ZR36057_ICR); // Enable Vsync interrupts too. SM
  3898. if (!pass_through) {
  3899. set_videobus_enable(zr, 0);
  3900. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEODECODER,
  3901.    DECODER_ENABLE_OUTPUT, &zero);
  3902. i2c_control_device(&zr->i2c, I2C_DRIVERID_VIDEOENCODER,
  3903.    ENCODER_SET_INPUT, &two);
  3904. set_videobus_enable(zr, 1);
  3905. }
  3906. return 0;
  3907. }
  3908. #include "zoran_procfs.c"
  3909. static void release_dc10(void)
  3910. {
  3911. u8 command;
  3912. int i;
  3913. struct zoran *zr;
  3914. for (i = 0; i < zoran_num; i++) {
  3915. zr = &zoran[i];
  3916. if (!zr->initialized)
  3917. continue;
  3918. /* unregister i2c_bus */
  3919. i2c_unregister_bus((&zr->i2c));
  3920. /* disable PCI bus-mastering */
  3921. pci_read_config_byte(zr->pci_dev, PCI_COMMAND, &command);
  3922. command &= ~PCI_COMMAND_MASTER;
  3923. pci_write_config_byte(zr->pci_dev, PCI_COMMAND, command);
  3924. /* put chip into reset */
  3925. btwrite(0, ZR36057_SPGPPCR);
  3926. free_irq(zr->pci_dev->irq, zr);
  3927. /* unmap and free memory */
  3928. kfree((void *) zr->stat_com);
  3929. zoran_proc_cleanup(i);
  3930. iounmap(zr->zr36057_mem);
  3931. video_unregister_device(&zr->video_dev);
  3932. }
  3933. }
  3934. /*
  3935.  *   Scan for a Buz card (actually for the PCI contoler ZR36057),
  3936.  *   request the irq and map the io memory
  3937.  */
  3938. static int find_zr36057(void)
  3939. {
  3940. unsigned char latency, need_latency;
  3941. struct zoran *zr;
  3942. struct pci_dev *dev = NULL;
  3943. int result;
  3944. zoran_num = 0;
  3945. while (zoran_num < BUZ_MAX
  3946.        && (dev =
  3947.    pci_find_device(PCI_VENDOR_ID_ZORAN,
  3948.    PCI_DEVICE_ID_ZORAN_36057,
  3949.    dev)) != NULL) {
  3950. zr = &zoran[zoran_num];
  3951. zr->pci_dev = dev;
  3952. zr->zr36057_mem = NULL;
  3953. zr->id = zoran_num;
  3954. sprintf(zr->name, "MJPEG[%u]", zr->id);
  3955. spin_lock_init(&zr->lock);
  3956. init_MUTEX(&zr->sem);
  3957. if (pci_enable_device(dev))
  3958. continue;
  3959. zr->zr36057_adr = pci_resource_start(zr->pci_dev, 0);
  3960. pci_read_config_byte(zr->pci_dev, PCI_CLASS_REVISION,
  3961.      &zr->revision);
  3962. if (zr->revision < 2) {
  3963. printk(KERN_INFO
  3964.        "%s: Zoran ZR36057 (rev %d) irq: %d, memory: 0x%08x.n",
  3965.        zr->name, zr->revision, zr->pci_dev->irq,
  3966.        zr->zr36057_adr);
  3967. } else {
  3968. unsigned short ss_vendor_id, ss_id;
  3969. ss_vendor_id = zr->pci_dev->subsystem_vendor;
  3970. ss_id = zr->pci_dev->subsystem_device;
  3971. printk(KERN_INFO
  3972.        "%s: Zoran ZR36067 (rev %d) irq: %d, memory: 0x%08xn",
  3973.        zr->name, zr->revision, zr->pci_dev->irq,
  3974.        zr->zr36057_adr);
  3975. printk(KERN_INFO
  3976.        "%s: subsystem vendor=0x%04x id=0x%04xn",
  3977.        zr->name, ss_vendor_id, ss_id);
  3978. }
  3979. zr->zr36057_mem = ioremap_nocache(zr->zr36057_adr, 0x1000);
  3980. if (!zr->zr36057_mem) {
  3981. printk(KERN_ERR "%s: ioremap failedn", zr->name);
  3982. /* XXX handle error */
  3983. }
  3984. result = request_irq(zr->pci_dev->irq, zoran_irq, SA_SHIRQ | SA_INTERRUPT, zr->name, (void *) zr);
  3985. if (result < 0) {
  3986. if (result == -EINVAL) {
  3987. printk(KERN_ERR
  3988.        "%s: Bad irq number or handlern",
  3989.        zr->name);
  3990. } else if (result == -EBUSY) {
  3991. printk(KERN_ERR
  3992.        "%s: IRQ %d busy, change your PnP config in BIOSn",
  3993.        zr->name, zr->pci_dev->irq);
  3994. } else {
  3995. printk(KERN_ERR
  3996.        "%s: Can't assign irq, error code %dn",
  3997.        zr->name, result);
  3998. }
  3999. iounmap(zr->zr36057_mem);
  4000. continue;
  4001. }
  4002. /* set PCI latency timer */
  4003. pci_read_config_byte(zr->pci_dev, PCI_LATENCY_TIMER, &latency);
  4004. need_latency = zr->revision > 1 ? 32 : 48;
  4005. if (latency != need_latency) {
  4006. printk(KERN_INFO "%s: Changing PCI latency from %d to %d.n",  zr->name, latency, need_latency);
  4007. pci_write_config_byte(zr->pci_dev, PCI_LATENCY_TIMER,  need_latency);
  4008. }
  4009. btwrite(0, ZR36057_SPGPPCR);
  4010. mdelay(1);
  4011. btwrite(ZR36057_SPGPPCR_SoftReset, ZR36057_SPGPPCR);
  4012. mdelay(1);
  4013. /* assert P_Reset */
  4014. btwrite(0, ZR36057_JPC);
  4015. /* set up GPIO direction - all output */
  4016. btwrite(ZR36057_SPGPPCR_SoftReset | 0, ZR36057_SPGPPCR);
  4017. btwrite((0x81 << 24) | 0x8888, ZR36057_GPPGCR1);
  4018. /* i2c */
  4019. memcpy(&zr->i2c, &zoran_i2c_bus_template,
  4020.        sizeof(struct i2c_bus));
  4021. strcpy(zr->i2c.name, zr->name);
  4022. zr->i2c.data = zr;
  4023. printk(KERN_INFO "%s: Initializing i2c bus...n", zr->name);
  4024. if (i2c_register_bus(&zr->i2c) < 0) {
  4025. /* put chip into reset */
  4026. btwrite(0, ZR36057_SPGPPCR);
  4027. free_irq(zr->pci_dev->irq, zr);
  4028. iounmap(zr->zr36057_mem);
  4029. printk(KERN_ERR "%s: Can't initialize i2c busn", zr->name);
  4030. continue;
  4031. }
  4032. if (zr->card != DC10 && zr->card != DC10plus
  4033.     && zr->card != LML33 && zr->card != BUZ) {
  4034. /* unregister i2c_bus */
  4035. i2c_unregister_bus((&zr->i2c));
  4036. /* put chip into reset */
  4037. btwrite(0, ZR36057_SPGPPCR);
  4038. free_irq(zr->pci_dev->irq, zr);
  4039. iounmap(zr->zr36057_mem);
  4040. printk(KERN_ERR "%s: Card not supportedn",
  4041.        zr->name);
  4042. continue;
  4043. }
  4044. printk(KERN_INFO "%s card detectedn", zr->name);
  4045. if (zr->card == LML33) {
  4046. GPIO(zr, 2, 1); // Set Composite input/output
  4047. }
  4048. /* reset JPEG codec */
  4049. zr36060_sleep(zr, 1);
  4050. zr36060_reset(zr);
  4051. /* video bus enabled */
  4052. /* display codec revision */
  4053. if (zr36060_read_8(zr, 0x022) == 0x33) {
  4054. printk(KERN_INFO "%s: Zoran ZR36060 (rev %d)n",
  4055.        zr->name, zr36060_read_8(zr, 0x023));
  4056. } else {
  4057. /* unregister i2c_bus */
  4058. i2c_unregister_bus((&zr->i2c));
  4059. /* put chip into reset */
  4060. btwrite(0, ZR36057_SPGPPCR);
  4061. free_irq(zr->pci_dev->irq, zr);
  4062. iounmap(zr->zr36057_mem);
  4063. printk(KERN_ERR "%s: Zoran ZR36060 not foundn",
  4064.        zr->name);
  4065. continue;
  4066. }
  4067. zoran_num++;
  4068. }
  4069. if (zoran_num == 0) {
  4070. printk(KERN_INFO "No known MJPEG cards found.n");
  4071. }
  4072. return zoran_num;
  4073. }
  4074. static void handle_chipset(void)
  4075. {
  4076. if(pci_pci_problems & PCIPCI_FAIL)
  4077. printk(KERN_WARNING "Chipset may not support reliable PCI-PCI DMA.n");
  4078. if(pci_pci_problems & PCIPCI_TRITON)
  4079. {
  4080. printk(KERN_WARNING "Enabling Triton support.n");
  4081. triton = 1;
  4082. }
  4083. if(pci_pci_problems & PCIPCI_NATOMA)
  4084. {
  4085. printk(KERN_WARNING "Enabling Natoma workaround.n");
  4086. natoma = 1;
  4087. }
  4088. }
  4089. static int init_dc10_cards(void)
  4090. {
  4091. int i;
  4092. memset(zoran, 0, sizeof(zoran));
  4093. printk(KERN_INFO
  4094.        "Zoran ZR36060 + ZR36057/67 MJPEG board driver version %d.%dn",
  4095.        MAJOR_VERSION, MINOR_VERSION);
  4096. /* Look for cards */
  4097. if (find_zr36057() < 0) {
  4098. return -EIO;
  4099. }
  4100. if (zoran_num == 0)
  4101. return 0; //-ENXIO;
  4102. printk(KERN_INFO "MJPEG: %d card(s) foundn", zoran_num);
  4103. /* check the parameters we have been given, adjust if necessary */
  4104. if (v4l_nbufs < 0)
  4105. v4l_nbufs = 0;
  4106. if (v4l_nbufs > VIDEO_MAX_FRAME)
  4107. v4l_nbufs = VIDEO_MAX_FRAME;
  4108. /* The user specfies the in KB, we want them in byte (and page aligned) */
  4109. v4l_bufsize = PAGE_ALIGN(v4l_bufsize * 1024);
  4110. if (v4l_bufsize < 32768)
  4111. v4l_bufsize = 32768;
  4112. /* 2 MB is arbitrary but sufficient for the maximum possible images */
  4113. if (v4l_bufsize > 2048 * 1024)
  4114. v4l_bufsize = 2048 * 1024;
  4115. printk(KERN_INFO "MJPEG: using %d V4L buffers of size %d KBn",
  4116.        v4l_nbufs, v4l_bufsize >> 10);
  4117. /* Use parameter for vidmem or try to find a video card */
  4118. if (vidmem) {
  4119. printk(KERN_INFO
  4120.        "MJPEG: Using supplied video memory base address @ 0x%lxn",
  4121.        vidmem);
  4122. }
  4123. /* check if we have a Triton or Natome chipset */
  4124. handle_chipset();
  4125. /* take care of Natoma chipset and a revision 1 zr36057 */
  4126. for (i = 0; i < zoran_num; i++) {
  4127. if (natoma && zoran[i].revision <= 1) {
  4128. zoran[i].need_contiguous = 1;
  4129. printk(KERN_INFO
  4130.        "%s: ZR36057/Natoma bug, max. buffer size is 128Kn",
  4131.        zoran[i].name);
  4132. } else {
  4133. zoran[i].need_contiguous = 0;
  4134. }
  4135. }
  4136. /* initialize the Buzs */
  4137. /* We have to know which ones must be released if an error occurs */
  4138. for (i = 0; i < zoran_num; i++)
  4139. zoran[i].initialized = 0;
  4140. for (i = 0; i < zoran_num; i++) {
  4141. if (zr36057_init(i) < 0) {
  4142. release_dc10();
  4143. return -EIO;
  4144. }
  4145. zoran_proc_init(i);
  4146. }
  4147. return 0;
  4148. }
  4149. static void unload_dc10_cards(void)
  4150. {
  4151. release_dc10();
  4152. }
  4153. module_init(init_dc10_cards);
  4154. module_exit(unload_dc10_cards);
  4155. MODULE_LICENSE("GPL");