/drivers/media/video/bt8xx/bttv-driver.c

https://gitlab.com/TeamCarbonXtreme/android_kernel_samsung_msm7x27 · C · 4686 lines · 3673 code · 648 blank · 365 comment · 526 complexity · 3ad34d4704e6da8f65252d4e13f90884 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. bttv - Bt848 frame grabber driver
  3. Copyright (C) 1996,97,98 Ralph Metzler <rjkm@thp.uni-koeln.de>
  4. & Marcus Metzler <mocm@thp.uni-koeln.de>
  5. (c) 1999-2002 Gerd Knorr <kraxel@bytesex.org>
  6. some v4l2 code lines are taken from Justin's bttv2 driver which is
  7. (c) 2000 Justin Schoeman <justin@suntiger.ee.up.ac.za>
  8. V4L1 removal from:
  9. (c) 2005-2006 Nickolay V. Shmyrev <nshmyrev@yandex.ru>
  10. Fixes to be fully V4L2 compliant by
  11. (c) 2006 Mauro Carvalho Chehab <mchehab@infradead.org>
  12. Cropping and overscan support
  13. Copyright (C) 2005, 2006 Michael H. Schimek <mschimek@gmx.at>
  14. Sponsored by OPQ Systems AB
  15. This program is free software; you can redistribute it and/or modify
  16. it under the terms of the GNU General Public License as published by
  17. the Free Software Foundation; either version 2 of the License, or
  18. (at your option) any later version.
  19. This program is distributed in the hope that it will be useful,
  20. but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. GNU General Public License for more details.
  23. You should have received a copy of the GNU General Public License
  24. along with this program; if not, write to the Free Software
  25. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  26. */
  27. #include <linux/init.h>
  28. #include <linux/module.h>
  29. #include <linux/delay.h>
  30. #include <linux/slab.h>
  31. #include <linux/errno.h>
  32. #include <linux/fs.h>
  33. #include <linux/kernel.h>
  34. #include <linux/sched.h>
  35. #include <linux/smp_lock.h>
  36. #include <linux/interrupt.h>
  37. #include <linux/kdev_t.h>
  38. #include "bttvp.h"
  39. #include <media/v4l2-common.h>
  40. #include <media/v4l2-ioctl.h>
  41. #include <media/tvaudio.h>
  42. #include <media/msp3400.h>
  43. #include <linux/dma-mapping.h>
  44. #include <asm/io.h>
  45. #include <asm/byteorder.h>
  46. #include <media/rds.h>
  47. unsigned int bttv_num; /* number of Bt848s in use */
  48. struct bttv *bttvs[BTTV_MAX];
  49. unsigned int bttv_debug;
  50. unsigned int bttv_verbose = 1;
  51. unsigned int bttv_gpio;
  52. /* config variables */
  53. #ifdef __BIG_ENDIAN
  54. static unsigned int bigendian=1;
  55. #else
  56. static unsigned int bigendian;
  57. #endif
  58. static unsigned int radio[BTTV_MAX];
  59. static unsigned int irq_debug;
  60. static unsigned int gbuffers = 8;
  61. static unsigned int gbufsize = 0x208000;
  62. static unsigned int reset_crop = 1;
  63. static int video_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  64. static int radio_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  65. static int vbi_nr[BTTV_MAX] = { [0 ... (BTTV_MAX-1)] = -1 };
  66. static int debug_latency;
  67. static int disable_ir;
  68. static unsigned int fdsr;
  69. /* options */
  70. static unsigned int combfilter;
  71. static unsigned int lumafilter;
  72. static unsigned int automute = 1;
  73. static unsigned int chroma_agc;
  74. static unsigned int adc_crush = 1;
  75. static unsigned int whitecrush_upper = 0xCF;
  76. static unsigned int whitecrush_lower = 0x7F;
  77. static unsigned int vcr_hack;
  78. static unsigned int irq_iswitch;
  79. static unsigned int uv_ratio = 50;
  80. static unsigned int full_luma_range;
  81. static unsigned int coring;
  82. /* API features (turn on/off stuff for testing) */
  83. static unsigned int v4l2 = 1;
  84. /* insmod args */
  85. module_param(bttv_verbose, int, 0644);
  86. module_param(bttv_gpio, int, 0644);
  87. module_param(bttv_debug, int, 0644);
  88. module_param(irq_debug, int, 0644);
  89. module_param(debug_latency, int, 0644);
  90. module_param(disable_ir, int, 0444);
  91. module_param(fdsr, int, 0444);
  92. module_param(gbuffers, int, 0444);
  93. module_param(gbufsize, int, 0444);
  94. module_param(reset_crop, int, 0444);
  95. module_param(v4l2, int, 0644);
  96. module_param(bigendian, int, 0644);
  97. module_param(irq_iswitch, int, 0644);
  98. module_param(combfilter, int, 0444);
  99. module_param(lumafilter, int, 0444);
  100. module_param(automute, int, 0444);
  101. module_param(chroma_agc, int, 0444);
  102. module_param(adc_crush, int, 0444);
  103. module_param(whitecrush_upper, int, 0444);
  104. module_param(whitecrush_lower, int, 0444);
  105. module_param(vcr_hack, int, 0444);
  106. module_param(uv_ratio, int, 0444);
  107. module_param(full_luma_range, int, 0444);
  108. module_param(coring, int, 0444);
  109. module_param_array(radio, int, NULL, 0444);
  110. module_param_array(video_nr, int, NULL, 0444);
  111. module_param_array(radio_nr, int, NULL, 0444);
  112. module_param_array(vbi_nr, int, NULL, 0444);
  113. MODULE_PARM_DESC(radio,"The TV card supports radio, default is 0 (no)");
  114. MODULE_PARM_DESC(bigendian,"byte order of the framebuffer, default is native endian");
  115. MODULE_PARM_DESC(bttv_verbose,"verbose startup messages, default is 1 (yes)");
  116. MODULE_PARM_DESC(bttv_gpio,"log gpio changes, default is 0 (no)");
  117. MODULE_PARM_DESC(bttv_debug,"debug messages, default is 0 (no)");
  118. MODULE_PARM_DESC(irq_debug,"irq handler debug messages, default is 0 (no)");
  119. MODULE_PARM_DESC(disable_ir, "disable infrared remote support");
  120. MODULE_PARM_DESC(gbuffers,"number of capture buffers. range 2-32, default 8");
  121. MODULE_PARM_DESC(gbufsize,"size of the capture buffers, default is 0x208000");
  122. MODULE_PARM_DESC(reset_crop,"reset cropping parameters at open(), default "
  123. "is 1 (yes) for compatibility with older applications");
  124. MODULE_PARM_DESC(automute,"mute audio on bad/missing video signal, default is 1 (yes)");
  125. MODULE_PARM_DESC(chroma_agc,"enables the AGC of chroma signal, default is 0 (no)");
  126. MODULE_PARM_DESC(adc_crush,"enables the luminance ADC crush, default is 1 (yes)");
  127. MODULE_PARM_DESC(whitecrush_upper,"sets the white crush upper value, default is 207");
  128. MODULE_PARM_DESC(whitecrush_lower,"sets the white crush lower value, default is 127");
  129. MODULE_PARM_DESC(vcr_hack,"enables the VCR hack (improves synch on poor VCR tapes), default is 0 (no)");
  130. MODULE_PARM_DESC(irq_iswitch,"switch inputs in irq handler");
  131. MODULE_PARM_DESC(uv_ratio,"ratio between u and v gains, default is 50");
  132. MODULE_PARM_DESC(full_luma_range,"use the full luma range, default is 0 (no)");
  133. MODULE_PARM_DESC(coring,"set the luma coring level, default is 0 (no)");
  134. MODULE_PARM_DESC(video_nr, "video device numbers");
  135. MODULE_PARM_DESC(vbi_nr, "vbi device numbers");
  136. MODULE_PARM_DESC(radio_nr, "radio device numbers");
  137. MODULE_DESCRIPTION("bttv - v4l/v4l2 driver module for bt848/878 based cards");
  138. MODULE_AUTHOR("Ralph Metzler & Marcus Metzler & Gerd Knorr");
  139. MODULE_LICENSE("GPL");
  140. /* ----------------------------------------------------------------------- */
  141. /* sysfs */
  142. static ssize_t show_card(struct device *cd,
  143. struct device_attribute *attr, char *buf)
  144. {
  145. struct video_device *vfd = container_of(cd, struct video_device, dev);
  146. struct bttv *btv = video_get_drvdata(vfd);
  147. return sprintf(buf, "%d\n", btv ? btv->c.type : UNSET);
  148. }
  149. static DEVICE_ATTR(card, S_IRUGO, show_card, NULL);
  150. /* ----------------------------------------------------------------------- */
  151. /* dvb auto-load setup */
  152. #if defined(CONFIG_MODULES) && defined(MODULE)
  153. static void request_module_async(struct work_struct *work)
  154. {
  155. request_module("dvb-bt8xx");
  156. }
  157. static void request_modules(struct bttv *dev)
  158. {
  159. INIT_WORK(&dev->request_module_wk, request_module_async);
  160. schedule_work(&dev->request_module_wk);
  161. }
  162. #else
  163. #define request_modules(dev)
  164. #endif /* CONFIG_MODULES */
  165. /* ----------------------------------------------------------------------- */
  166. /* static data */
  167. /* special timing tables from conexant... */
  168. static u8 SRAM_Table[][60] =
  169. {
  170. /* PAL digital input over GPIO[7:0] */
  171. {
  172. 45, // 45 bytes following
  173. 0x36,0x11,0x01,0x00,0x90,0x02,0x05,0x10,0x04,0x16,
  174. 0x12,0x05,0x11,0x00,0x04,0x12,0xC0,0x00,0x31,0x00,
  175. 0x06,0x51,0x08,0x03,0x89,0x08,0x07,0xC0,0x44,0x00,
  176. 0x81,0x01,0x01,0xA9,0x0D,0x02,0x02,0x50,0x03,0x37,
  177. 0x37,0x00,0xAF,0x21,0x00
  178. },
  179. /* NTSC digital input over GPIO[7:0] */
  180. {
  181. 51, // 51 bytes following
  182. 0x0C,0xC0,0x00,0x00,0x90,0x02,0x03,0x10,0x03,0x06,
  183. 0x10,0x04,0x12,0x12,0x05,0x02,0x13,0x04,0x19,0x00,
  184. 0x04,0x39,0x00,0x06,0x59,0x08,0x03,0x83,0x08,0x07,
  185. 0x03,0x50,0x00,0xC0,0x40,0x00,0x86,0x01,0x01,0xA6,
  186. 0x0D,0x02,0x03,0x11,0x01,0x05,0x37,0x00,0xAC,0x21,
  187. 0x00,
  188. },
  189. // TGB_NTSC392 // quartzsight
  190. // This table has been modified to be used for Fusion Rev D
  191. {
  192. 0x2A, // size of table = 42
  193. 0x06, 0x08, 0x04, 0x0a, 0xc0, 0x00, 0x18, 0x08, 0x03, 0x24,
  194. 0x08, 0x07, 0x02, 0x90, 0x02, 0x08, 0x10, 0x04, 0x0c, 0x10,
  195. 0x05, 0x2c, 0x11, 0x04, 0x55, 0x48, 0x00, 0x05, 0x50, 0x00,
  196. 0xbf, 0x0c, 0x02, 0x2f, 0x3d, 0x00, 0x2f, 0x3f, 0x00, 0xc3,
  197. 0x20, 0x00
  198. }
  199. };
  200. /* minhdelayx1 first video pixel we can capture on a line and
  201. hdelayx1 start of active video, both relative to rising edge of
  202. /HRESET pulse (0H) in 1 / fCLKx1.
  203. swidth width of active video and
  204. totalwidth total line width, both in 1 / fCLKx1.
  205. sqwidth total line width in square pixels.
  206. vdelay start of active video in 2 * field lines relative to
  207. trailing edge of /VRESET pulse (VDELAY register).
  208. sheight height of active video in 2 * field lines.
  209. videostart0 ITU-R frame line number of the line corresponding
  210. to vdelay in the first field. */
  211. #define CROPCAP(minhdelayx1, hdelayx1, swidth, totalwidth, sqwidth, \
  212. vdelay, sheight, videostart0) \
  213. .cropcap.bounds.left = minhdelayx1, \
  214. /* * 2 because vertically we count field lines times two, */ \
  215. /* e.g. 23 * 2 to 23 * 2 + 576 in PAL-BGHI defrect. */ \
  216. .cropcap.bounds.top = (videostart0) * 2 - (vdelay) + MIN_VDELAY, \
  217. /* 4 is a safety margin at the end of the line. */ \
  218. .cropcap.bounds.width = (totalwidth) - (minhdelayx1) - 4, \
  219. .cropcap.bounds.height = (sheight) + (vdelay) - MIN_VDELAY, \
  220. .cropcap.defrect.left = hdelayx1, \
  221. .cropcap.defrect.top = (videostart0) * 2, \
  222. .cropcap.defrect.width = swidth, \
  223. .cropcap.defrect.height = sheight, \
  224. .cropcap.pixelaspect.numerator = totalwidth, \
  225. .cropcap.pixelaspect.denominator = sqwidth,
  226. const struct bttv_tvnorm bttv_tvnorms[] = {
  227. /* PAL-BDGHI */
  228. /* max. active video is actually 922, but 924 is divisible by 4 and 3! */
  229. /* actually, max active PAL with HSCALE=0 is 948, NTSC is 768 - nil */
  230. {
  231. .v4l2_id = V4L2_STD_PAL,
  232. .name = "PAL",
  233. .Fsc = 35468950,
  234. .swidth = 924,
  235. .sheight = 576,
  236. .totalwidth = 1135,
  237. .adelay = 0x7f,
  238. .bdelay = 0x72,
  239. .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
  240. .scaledtwidth = 1135,
  241. .hdelayx1 = 186,
  242. .hactivex1 = 924,
  243. .vdelay = 0x20,
  244. .vbipack = 255, /* min (2048 / 4, 0x1ff) & 0xff */
  245. .sram = 0,
  246. /* ITU-R frame line number of the first VBI line
  247. we can capture, of the first and second field.
  248. The last line is determined by cropcap.bounds. */
  249. .vbistart = { 7, 320 },
  250. CROPCAP(/* minhdelayx1 */ 68,
  251. /* hdelayx1 */ 186,
  252. /* Should be (768 * 1135 + 944 / 2) / 944.
  253. cropcap.defrect is used for image width
  254. checks, so we keep the old value 924. */
  255. /* swidth */ 924,
  256. /* totalwidth */ 1135,
  257. /* sqwidth */ 944,
  258. /* vdelay */ 0x20,
  259. /* sheight */ 576,
  260. /* videostart0 */ 23)
  261. /* bt878 (and bt848?) can capture another
  262. line below active video. */
  263. .cropcap.bounds.height = (576 + 2) + 0x20 - 2,
  264. },{
  265. .v4l2_id = V4L2_STD_NTSC_M | V4L2_STD_NTSC_M_KR,
  266. .name = "NTSC",
  267. .Fsc = 28636363,
  268. .swidth = 768,
  269. .sheight = 480,
  270. .totalwidth = 910,
  271. .adelay = 0x68,
  272. .bdelay = 0x5d,
  273. .iform = (BT848_IFORM_NTSC|BT848_IFORM_XT0),
  274. .scaledtwidth = 910,
  275. .hdelayx1 = 128,
  276. .hactivex1 = 910,
  277. .vdelay = 0x1a,
  278. .vbipack = 144, /* min (1600 / 4, 0x1ff) & 0xff */
  279. .sram = 1,
  280. .vbistart = { 10, 273 },
  281. CROPCAP(/* minhdelayx1 */ 68,
  282. /* hdelayx1 */ 128,
  283. /* Should be (640 * 910 + 780 / 2) / 780? */
  284. /* swidth */ 768,
  285. /* totalwidth */ 910,
  286. /* sqwidth */ 780,
  287. /* vdelay */ 0x1a,
  288. /* sheight */ 480,
  289. /* videostart0 */ 23)
  290. },{
  291. .v4l2_id = V4L2_STD_SECAM,
  292. .name = "SECAM",
  293. .Fsc = 35468950,
  294. .swidth = 924,
  295. .sheight = 576,
  296. .totalwidth = 1135,
  297. .adelay = 0x7f,
  298. .bdelay = 0xb0,
  299. .iform = (BT848_IFORM_SECAM|BT848_IFORM_XT1),
  300. .scaledtwidth = 1135,
  301. .hdelayx1 = 186,
  302. .hactivex1 = 922,
  303. .vdelay = 0x20,
  304. .vbipack = 255,
  305. .sram = 0, /* like PAL, correct? */
  306. .vbistart = { 7, 320 },
  307. CROPCAP(/* minhdelayx1 */ 68,
  308. /* hdelayx1 */ 186,
  309. /* swidth */ 924,
  310. /* totalwidth */ 1135,
  311. /* sqwidth */ 944,
  312. /* vdelay */ 0x20,
  313. /* sheight */ 576,
  314. /* videostart0 */ 23)
  315. },{
  316. .v4l2_id = V4L2_STD_PAL_Nc,
  317. .name = "PAL-Nc",
  318. .Fsc = 28636363,
  319. .swidth = 640,
  320. .sheight = 576,
  321. .totalwidth = 910,
  322. .adelay = 0x68,
  323. .bdelay = 0x5d,
  324. .iform = (BT848_IFORM_PAL_NC|BT848_IFORM_XT0),
  325. .scaledtwidth = 780,
  326. .hdelayx1 = 130,
  327. .hactivex1 = 734,
  328. .vdelay = 0x1a,
  329. .vbipack = 144,
  330. .sram = -1,
  331. .vbistart = { 7, 320 },
  332. CROPCAP(/* minhdelayx1 */ 68,
  333. /* hdelayx1 */ 130,
  334. /* swidth */ (640 * 910 + 780 / 2) / 780,
  335. /* totalwidth */ 910,
  336. /* sqwidth */ 780,
  337. /* vdelay */ 0x1a,
  338. /* sheight */ 576,
  339. /* videostart0 */ 23)
  340. },{
  341. .v4l2_id = V4L2_STD_PAL_M,
  342. .name = "PAL-M",
  343. .Fsc = 28636363,
  344. .swidth = 640,
  345. .sheight = 480,
  346. .totalwidth = 910,
  347. .adelay = 0x68,
  348. .bdelay = 0x5d,
  349. .iform = (BT848_IFORM_PAL_M|BT848_IFORM_XT0),
  350. .scaledtwidth = 780,
  351. .hdelayx1 = 135,
  352. .hactivex1 = 754,
  353. .vdelay = 0x1a,
  354. .vbipack = 144,
  355. .sram = -1,
  356. .vbistart = { 10, 273 },
  357. CROPCAP(/* minhdelayx1 */ 68,
  358. /* hdelayx1 */ 135,
  359. /* swidth */ (640 * 910 + 780 / 2) / 780,
  360. /* totalwidth */ 910,
  361. /* sqwidth */ 780,
  362. /* vdelay */ 0x1a,
  363. /* sheight */ 480,
  364. /* videostart0 */ 23)
  365. },{
  366. .v4l2_id = V4L2_STD_PAL_N,
  367. .name = "PAL-N",
  368. .Fsc = 35468950,
  369. .swidth = 768,
  370. .sheight = 576,
  371. .totalwidth = 1135,
  372. .adelay = 0x7f,
  373. .bdelay = 0x72,
  374. .iform = (BT848_IFORM_PAL_N|BT848_IFORM_XT1),
  375. .scaledtwidth = 944,
  376. .hdelayx1 = 186,
  377. .hactivex1 = 922,
  378. .vdelay = 0x20,
  379. .vbipack = 144,
  380. .sram = -1,
  381. .vbistart = { 7, 320 },
  382. CROPCAP(/* minhdelayx1 */ 68,
  383. /* hdelayx1 */ 186,
  384. /* swidth */ (768 * 1135 + 944 / 2) / 944,
  385. /* totalwidth */ 1135,
  386. /* sqwidth */ 944,
  387. /* vdelay */ 0x20,
  388. /* sheight */ 576,
  389. /* videostart0 */ 23)
  390. },{
  391. .v4l2_id = V4L2_STD_NTSC_M_JP,
  392. .name = "NTSC-JP",
  393. .Fsc = 28636363,
  394. .swidth = 640,
  395. .sheight = 480,
  396. .totalwidth = 910,
  397. .adelay = 0x68,
  398. .bdelay = 0x5d,
  399. .iform = (BT848_IFORM_NTSC_J|BT848_IFORM_XT0),
  400. .scaledtwidth = 780,
  401. .hdelayx1 = 135,
  402. .hactivex1 = 754,
  403. .vdelay = 0x16,
  404. .vbipack = 144,
  405. .sram = -1,
  406. .vbistart = { 10, 273 },
  407. CROPCAP(/* minhdelayx1 */ 68,
  408. /* hdelayx1 */ 135,
  409. /* swidth */ (640 * 910 + 780 / 2) / 780,
  410. /* totalwidth */ 910,
  411. /* sqwidth */ 780,
  412. /* vdelay */ 0x16,
  413. /* sheight */ 480,
  414. /* videostart0 */ 23)
  415. },{
  416. /* that one hopefully works with the strange timing
  417. * which video recorders produce when playing a NTSC
  418. * tape on a PAL TV ... */
  419. .v4l2_id = V4L2_STD_PAL_60,
  420. .name = "PAL-60",
  421. .Fsc = 35468950,
  422. .swidth = 924,
  423. .sheight = 480,
  424. .totalwidth = 1135,
  425. .adelay = 0x7f,
  426. .bdelay = 0x72,
  427. .iform = (BT848_IFORM_PAL_BDGHI|BT848_IFORM_XT1),
  428. .scaledtwidth = 1135,
  429. .hdelayx1 = 186,
  430. .hactivex1 = 924,
  431. .vdelay = 0x1a,
  432. .vbipack = 255,
  433. .vtotal = 524,
  434. .sram = -1,
  435. .vbistart = { 10, 273 },
  436. CROPCAP(/* minhdelayx1 */ 68,
  437. /* hdelayx1 */ 186,
  438. /* swidth */ 924,
  439. /* totalwidth */ 1135,
  440. /* sqwidth */ 944,
  441. /* vdelay */ 0x1a,
  442. /* sheight */ 480,
  443. /* videostart0 */ 23)
  444. }
  445. };
  446. static const unsigned int BTTV_TVNORMS = ARRAY_SIZE(bttv_tvnorms);
  447. /* ----------------------------------------------------------------------- */
  448. /* bttv format list
  449. packed pixel formats must come first */
  450. static const struct bttv_format formats[] = {
  451. {
  452. .name = "8 bpp, gray",
  453. .fourcc = V4L2_PIX_FMT_GREY,
  454. .btformat = BT848_COLOR_FMT_Y8,
  455. .depth = 8,
  456. .flags = FORMAT_FLAGS_PACKED,
  457. },{
  458. .name = "8 bpp, dithered color",
  459. .fourcc = V4L2_PIX_FMT_HI240,
  460. .btformat = BT848_COLOR_FMT_RGB8,
  461. .depth = 8,
  462. .flags = FORMAT_FLAGS_PACKED | FORMAT_FLAGS_DITHER,
  463. },{
  464. .name = "15 bpp RGB, le",
  465. .fourcc = V4L2_PIX_FMT_RGB555,
  466. .btformat = BT848_COLOR_FMT_RGB15,
  467. .depth = 16,
  468. .flags = FORMAT_FLAGS_PACKED,
  469. },{
  470. .name = "15 bpp RGB, be",
  471. .fourcc = V4L2_PIX_FMT_RGB555X,
  472. .btformat = BT848_COLOR_FMT_RGB15,
  473. .btswap = 0x03, /* byteswap */
  474. .depth = 16,
  475. .flags = FORMAT_FLAGS_PACKED,
  476. },{
  477. .name = "16 bpp RGB, le",
  478. .fourcc = V4L2_PIX_FMT_RGB565,
  479. .btformat = BT848_COLOR_FMT_RGB16,
  480. .depth = 16,
  481. .flags = FORMAT_FLAGS_PACKED,
  482. },{
  483. .name = "16 bpp RGB, be",
  484. .fourcc = V4L2_PIX_FMT_RGB565X,
  485. .btformat = BT848_COLOR_FMT_RGB16,
  486. .btswap = 0x03, /* byteswap */
  487. .depth = 16,
  488. .flags = FORMAT_FLAGS_PACKED,
  489. },{
  490. .name = "24 bpp RGB, le",
  491. .fourcc = V4L2_PIX_FMT_BGR24,
  492. .btformat = BT848_COLOR_FMT_RGB24,
  493. .depth = 24,
  494. .flags = FORMAT_FLAGS_PACKED,
  495. },{
  496. .name = "32 bpp RGB, le",
  497. .fourcc = V4L2_PIX_FMT_BGR32,
  498. .btformat = BT848_COLOR_FMT_RGB32,
  499. .depth = 32,
  500. .flags = FORMAT_FLAGS_PACKED,
  501. },{
  502. .name = "32 bpp RGB, be",
  503. .fourcc = V4L2_PIX_FMT_RGB32,
  504. .btformat = BT848_COLOR_FMT_RGB32,
  505. .btswap = 0x0f, /* byte+word swap */
  506. .depth = 32,
  507. .flags = FORMAT_FLAGS_PACKED,
  508. },{
  509. .name = "4:2:2, packed, YUYV",
  510. .fourcc = V4L2_PIX_FMT_YUYV,
  511. .btformat = BT848_COLOR_FMT_YUY2,
  512. .depth = 16,
  513. .flags = FORMAT_FLAGS_PACKED,
  514. },{
  515. .name = "4:2:2, packed, YUYV",
  516. .fourcc = V4L2_PIX_FMT_YUYV,
  517. .btformat = BT848_COLOR_FMT_YUY2,
  518. .depth = 16,
  519. .flags = FORMAT_FLAGS_PACKED,
  520. },{
  521. .name = "4:2:2, packed, UYVY",
  522. .fourcc = V4L2_PIX_FMT_UYVY,
  523. .btformat = BT848_COLOR_FMT_YUY2,
  524. .btswap = 0x03, /* byteswap */
  525. .depth = 16,
  526. .flags = FORMAT_FLAGS_PACKED,
  527. },{
  528. .name = "4:2:2, planar, Y-Cb-Cr",
  529. .fourcc = V4L2_PIX_FMT_YUV422P,
  530. .btformat = BT848_COLOR_FMT_YCrCb422,
  531. .depth = 16,
  532. .flags = FORMAT_FLAGS_PLANAR,
  533. .hshift = 1,
  534. .vshift = 0,
  535. },{
  536. .name = "4:2:0, planar, Y-Cb-Cr",
  537. .fourcc = V4L2_PIX_FMT_YUV420,
  538. .btformat = BT848_COLOR_FMT_YCrCb422,
  539. .depth = 12,
  540. .flags = FORMAT_FLAGS_PLANAR,
  541. .hshift = 1,
  542. .vshift = 1,
  543. },{
  544. .name = "4:2:0, planar, Y-Cr-Cb",
  545. .fourcc = V4L2_PIX_FMT_YVU420,
  546. .btformat = BT848_COLOR_FMT_YCrCb422,
  547. .depth = 12,
  548. .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
  549. .hshift = 1,
  550. .vshift = 1,
  551. },{
  552. .name = "4:1:1, planar, Y-Cb-Cr",
  553. .fourcc = V4L2_PIX_FMT_YUV411P,
  554. .btformat = BT848_COLOR_FMT_YCrCb411,
  555. .depth = 12,
  556. .flags = FORMAT_FLAGS_PLANAR,
  557. .hshift = 2,
  558. .vshift = 0,
  559. },{
  560. .name = "4:1:0, planar, Y-Cb-Cr",
  561. .fourcc = V4L2_PIX_FMT_YUV410,
  562. .btformat = BT848_COLOR_FMT_YCrCb411,
  563. .depth = 9,
  564. .flags = FORMAT_FLAGS_PLANAR,
  565. .hshift = 2,
  566. .vshift = 2,
  567. },{
  568. .name = "4:1:0, planar, Y-Cr-Cb",
  569. .fourcc = V4L2_PIX_FMT_YVU410,
  570. .btformat = BT848_COLOR_FMT_YCrCb411,
  571. .depth = 9,
  572. .flags = FORMAT_FLAGS_PLANAR | FORMAT_FLAGS_CrCb,
  573. .hshift = 2,
  574. .vshift = 2,
  575. },{
  576. .name = "raw scanlines",
  577. .fourcc = -1,
  578. .btformat = BT848_COLOR_FMT_RAW,
  579. .depth = 8,
  580. .flags = FORMAT_FLAGS_RAW,
  581. }
  582. };
  583. static const unsigned int FORMATS = ARRAY_SIZE(formats);
  584. /* ----------------------------------------------------------------------- */
  585. #define V4L2_CID_PRIVATE_CHROMA_AGC (V4L2_CID_PRIVATE_BASE + 0)
  586. #define V4L2_CID_PRIVATE_COMBFILTER (V4L2_CID_PRIVATE_BASE + 1)
  587. #define V4L2_CID_PRIVATE_AUTOMUTE (V4L2_CID_PRIVATE_BASE + 2)
  588. #define V4L2_CID_PRIVATE_LUMAFILTER (V4L2_CID_PRIVATE_BASE + 3)
  589. #define V4L2_CID_PRIVATE_AGC_CRUSH (V4L2_CID_PRIVATE_BASE + 4)
  590. #define V4L2_CID_PRIVATE_VCR_HACK (V4L2_CID_PRIVATE_BASE + 5)
  591. #define V4L2_CID_PRIVATE_WHITECRUSH_UPPER (V4L2_CID_PRIVATE_BASE + 6)
  592. #define V4L2_CID_PRIVATE_WHITECRUSH_LOWER (V4L2_CID_PRIVATE_BASE + 7)
  593. #define V4L2_CID_PRIVATE_UV_RATIO (V4L2_CID_PRIVATE_BASE + 8)
  594. #define V4L2_CID_PRIVATE_FULL_LUMA_RANGE (V4L2_CID_PRIVATE_BASE + 9)
  595. #define V4L2_CID_PRIVATE_CORING (V4L2_CID_PRIVATE_BASE + 10)
  596. #define V4L2_CID_PRIVATE_LASTP1 (V4L2_CID_PRIVATE_BASE + 11)
  597. static const struct v4l2_queryctrl no_ctl = {
  598. .name = "42",
  599. .flags = V4L2_CTRL_FLAG_DISABLED,
  600. };
  601. static const struct v4l2_queryctrl bttv_ctls[] = {
  602. /* --- video --- */
  603. {
  604. .id = V4L2_CID_BRIGHTNESS,
  605. .name = "Brightness",
  606. .minimum = 0,
  607. .maximum = 65535,
  608. .step = 256,
  609. .default_value = 32768,
  610. .type = V4L2_CTRL_TYPE_INTEGER,
  611. },{
  612. .id = V4L2_CID_CONTRAST,
  613. .name = "Contrast",
  614. .minimum = 0,
  615. .maximum = 65535,
  616. .step = 128,
  617. .default_value = 32768,
  618. .type = V4L2_CTRL_TYPE_INTEGER,
  619. },{
  620. .id = V4L2_CID_SATURATION,
  621. .name = "Saturation",
  622. .minimum = 0,
  623. .maximum = 65535,
  624. .step = 128,
  625. .default_value = 32768,
  626. .type = V4L2_CTRL_TYPE_INTEGER,
  627. },{
  628. .id = V4L2_CID_HUE,
  629. .name = "Hue",
  630. .minimum = 0,
  631. .maximum = 65535,
  632. .step = 256,
  633. .default_value = 32768,
  634. .type = V4L2_CTRL_TYPE_INTEGER,
  635. },
  636. /* --- audio --- */
  637. {
  638. .id = V4L2_CID_AUDIO_MUTE,
  639. .name = "Mute",
  640. .minimum = 0,
  641. .maximum = 1,
  642. .type = V4L2_CTRL_TYPE_BOOLEAN,
  643. },{
  644. .id = V4L2_CID_AUDIO_VOLUME,
  645. .name = "Volume",
  646. .minimum = 0,
  647. .maximum = 65535,
  648. .step = 65535/100,
  649. .default_value = 65535,
  650. .type = V4L2_CTRL_TYPE_INTEGER,
  651. },{
  652. .id = V4L2_CID_AUDIO_BALANCE,
  653. .name = "Balance",
  654. .minimum = 0,
  655. .maximum = 65535,
  656. .step = 65535/100,
  657. .default_value = 32768,
  658. .type = V4L2_CTRL_TYPE_INTEGER,
  659. },{
  660. .id = V4L2_CID_AUDIO_BASS,
  661. .name = "Bass",
  662. .minimum = 0,
  663. .maximum = 65535,
  664. .step = 65535/100,
  665. .default_value = 32768,
  666. .type = V4L2_CTRL_TYPE_INTEGER,
  667. },{
  668. .id = V4L2_CID_AUDIO_TREBLE,
  669. .name = "Treble",
  670. .minimum = 0,
  671. .maximum = 65535,
  672. .step = 65535/100,
  673. .default_value = 32768,
  674. .type = V4L2_CTRL_TYPE_INTEGER,
  675. },
  676. /* --- private --- */
  677. {
  678. .id = V4L2_CID_PRIVATE_CHROMA_AGC,
  679. .name = "chroma agc",
  680. .minimum = 0,
  681. .maximum = 1,
  682. .type = V4L2_CTRL_TYPE_BOOLEAN,
  683. },{
  684. .id = V4L2_CID_PRIVATE_COMBFILTER,
  685. .name = "combfilter",
  686. .minimum = 0,
  687. .maximum = 1,
  688. .type = V4L2_CTRL_TYPE_BOOLEAN,
  689. },{
  690. .id = V4L2_CID_PRIVATE_AUTOMUTE,
  691. .name = "automute",
  692. .minimum = 0,
  693. .maximum = 1,
  694. .type = V4L2_CTRL_TYPE_BOOLEAN,
  695. },{
  696. .id = V4L2_CID_PRIVATE_LUMAFILTER,
  697. .name = "luma decimation filter",
  698. .minimum = 0,
  699. .maximum = 1,
  700. .type = V4L2_CTRL_TYPE_BOOLEAN,
  701. },{
  702. .id = V4L2_CID_PRIVATE_AGC_CRUSH,
  703. .name = "agc crush",
  704. .minimum = 0,
  705. .maximum = 1,
  706. .type = V4L2_CTRL_TYPE_BOOLEAN,
  707. },{
  708. .id = V4L2_CID_PRIVATE_VCR_HACK,
  709. .name = "vcr hack",
  710. .minimum = 0,
  711. .maximum = 1,
  712. .type = V4L2_CTRL_TYPE_BOOLEAN,
  713. },{
  714. .id = V4L2_CID_PRIVATE_WHITECRUSH_UPPER,
  715. .name = "whitecrush upper",
  716. .minimum = 0,
  717. .maximum = 255,
  718. .step = 1,
  719. .default_value = 0xCF,
  720. .type = V4L2_CTRL_TYPE_INTEGER,
  721. },{
  722. .id = V4L2_CID_PRIVATE_WHITECRUSH_LOWER,
  723. .name = "whitecrush lower",
  724. .minimum = 0,
  725. .maximum = 255,
  726. .step = 1,
  727. .default_value = 0x7F,
  728. .type = V4L2_CTRL_TYPE_INTEGER,
  729. },{
  730. .id = V4L2_CID_PRIVATE_UV_RATIO,
  731. .name = "uv ratio",
  732. .minimum = 0,
  733. .maximum = 100,
  734. .step = 1,
  735. .default_value = 50,
  736. .type = V4L2_CTRL_TYPE_INTEGER,
  737. },{
  738. .id = V4L2_CID_PRIVATE_FULL_LUMA_RANGE,
  739. .name = "full luma range",
  740. .minimum = 0,
  741. .maximum = 1,
  742. .type = V4L2_CTRL_TYPE_BOOLEAN,
  743. },{
  744. .id = V4L2_CID_PRIVATE_CORING,
  745. .name = "coring",
  746. .minimum = 0,
  747. .maximum = 3,
  748. .step = 1,
  749. .default_value = 0,
  750. .type = V4L2_CTRL_TYPE_INTEGER,
  751. }
  752. };
  753. static const struct v4l2_queryctrl *ctrl_by_id(int id)
  754. {
  755. int i;
  756. for (i = 0; i < ARRAY_SIZE(bttv_ctls); i++)
  757. if (bttv_ctls[i].id == id)
  758. return bttv_ctls+i;
  759. return NULL;
  760. }
  761. /* ----------------------------------------------------------------------- */
  762. /* resource management */
  763. /*
  764. RESOURCE_ allocated by freed by
  765. VIDEO_READ bttv_read 1) bttv_read 2)
  766. VIDEO_STREAM VIDIOC_STREAMON VIDIOC_STREAMOFF
  767. VIDIOC_QBUF 1) bttv_release
  768. VIDIOCMCAPTURE 1)
  769. OVERLAY VIDIOCCAPTURE on VIDIOCCAPTURE off
  770. VIDIOC_OVERLAY on VIDIOC_OVERLAY off
  771. 3) bttv_release
  772. VBI VIDIOC_STREAMON VIDIOC_STREAMOFF
  773. VIDIOC_QBUF 1) bttv_release
  774. bttv_read, bttv_poll 1) 4)
  775. 1) The resource must be allocated when we enter buffer prepare functions
  776. and remain allocated while buffers are in the DMA queue.
  777. 2) This is a single frame read.
  778. 3) VIDIOC_S_FBUF and VIDIOC_S_FMT (OVERLAY) still work when
  779. RESOURCE_OVERLAY is allocated.
  780. 4) This is a continuous read, implies VIDIOC_STREAMON.
  781. Note this driver permits video input and standard changes regardless if
  782. resources are allocated.
  783. */
  784. #define VBI_RESOURCES (RESOURCE_VBI)
  785. #define VIDEO_RESOURCES (RESOURCE_VIDEO_READ | \
  786. RESOURCE_VIDEO_STREAM | \
  787. RESOURCE_OVERLAY)
  788. static
  789. int check_alloc_btres(struct bttv *btv, struct bttv_fh *fh, int bit)
  790. {
  791. int xbits; /* mutual exclusive resources */
  792. if (fh->resources & bit)
  793. /* have it already allocated */
  794. return 1;
  795. xbits = bit;
  796. if (bit & (RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM))
  797. xbits |= RESOURCE_VIDEO_READ | RESOURCE_VIDEO_STREAM;
  798. /* is it free? */
  799. mutex_lock(&btv->lock);
  800. if (btv->resources & xbits) {
  801. /* no, someone else uses it */
  802. goto fail;
  803. }
  804. if ((bit & VIDEO_RESOURCES)
  805. && 0 == (btv->resources & VIDEO_RESOURCES)) {
  806. /* Do crop - use current, don't - use default parameters. */
  807. __s32 top = btv->crop[!!fh->do_crop].rect.top;
  808. if (btv->vbi_end > top)
  809. goto fail;
  810. /* We cannot capture the same line as video and VBI data.
  811. Claim scan lines crop[].rect.top to bottom. */
  812. btv->crop_start = top;
  813. } else if (bit & VBI_RESOURCES) {
  814. __s32 end = fh->vbi_fmt.end;
  815. if (end > btv->crop_start)
  816. goto fail;
  817. /* Claim scan lines above fh->vbi_fmt.end. */
  818. btv->vbi_end = end;
  819. }
  820. /* it's free, grab it */
  821. fh->resources |= bit;
  822. btv->resources |= bit;
  823. mutex_unlock(&btv->lock);
  824. return 1;
  825. fail:
  826. mutex_unlock(&btv->lock);
  827. return 0;
  828. }
  829. static
  830. int check_btres(struct bttv_fh *fh, int bit)
  831. {
  832. return (fh->resources & bit);
  833. }
  834. static
  835. int locked_btres(struct bttv *btv, int bit)
  836. {
  837. return (btv->resources & bit);
  838. }
  839. /* Call with btv->lock down. */
  840. static void
  841. disclaim_vbi_lines(struct bttv *btv)
  842. {
  843. btv->vbi_end = 0;
  844. }
  845. /* Call with btv->lock down. */
  846. static void
  847. disclaim_video_lines(struct bttv *btv)
  848. {
  849. const struct bttv_tvnorm *tvnorm;
  850. u8 crop;
  851. tvnorm = &bttv_tvnorms[btv->tvnorm];
  852. btv->crop_start = tvnorm->cropcap.bounds.top
  853. + tvnorm->cropcap.bounds.height;
  854. /* VBI capturing ends at VDELAY, start of video capturing, no
  855. matter how many lines the VBI RISC program expects. When video
  856. capturing is off, it shall no longer "preempt" VBI capturing,
  857. so we set VDELAY to maximum. */
  858. crop = btread(BT848_E_CROP) | 0xc0;
  859. btwrite(crop, BT848_E_CROP);
  860. btwrite(0xfe, BT848_E_VDELAY_LO);
  861. btwrite(crop, BT848_O_CROP);
  862. btwrite(0xfe, BT848_O_VDELAY_LO);
  863. }
  864. static
  865. void free_btres(struct bttv *btv, struct bttv_fh *fh, int bits)
  866. {
  867. if ((fh->resources & bits) != bits) {
  868. /* trying to free ressources not allocated by us ... */
  869. printk("bttv: BUG! (btres)\n");
  870. }
  871. mutex_lock(&btv->lock);
  872. fh->resources &= ~bits;
  873. btv->resources &= ~bits;
  874. bits = btv->resources;
  875. if (0 == (bits & VIDEO_RESOURCES))
  876. disclaim_video_lines(btv);
  877. if (0 == (bits & VBI_RESOURCES))
  878. disclaim_vbi_lines(btv);
  879. mutex_unlock(&btv->lock);
  880. }
  881. /* ----------------------------------------------------------------------- */
  882. /* If Bt848a or Bt849, use PLL for PAL/SECAM and crystal for NTSC */
  883. /* Frequency = (F_input / PLL_X) * PLL_I.PLL_F/PLL_C
  884. PLL_X = Reference pre-divider (0=1, 1=2)
  885. PLL_C = Post divider (0=6, 1=4)
  886. PLL_I = Integer input
  887. PLL_F = Fractional input
  888. F_input = 28.636363 MHz:
  889. PAL (CLKx2 = 35.46895 MHz): PLL_X = 1, PLL_I = 0x0E, PLL_F = 0xDCF9, PLL_C = 0
  890. */
  891. static void set_pll_freq(struct bttv *btv, unsigned int fin, unsigned int fout)
  892. {
  893. unsigned char fl, fh, fi;
  894. /* prevent overflows */
  895. fin/=4;
  896. fout/=4;
  897. fout*=12;
  898. fi=fout/fin;
  899. fout=(fout%fin)*256;
  900. fh=fout/fin;
  901. fout=(fout%fin)*256;
  902. fl=fout/fin;
  903. btwrite(fl, BT848_PLL_F_LO);
  904. btwrite(fh, BT848_PLL_F_HI);
  905. btwrite(fi|BT848_PLL_X, BT848_PLL_XCI);
  906. }
  907. static void set_pll(struct bttv *btv)
  908. {
  909. int i;
  910. if (!btv->pll.pll_crystal)
  911. return;
  912. if (btv->pll.pll_ofreq == btv->pll.pll_current) {
  913. dprintk("bttv%d: PLL: no change required\n",btv->c.nr);
  914. return;
  915. }
  916. if (btv->pll.pll_ifreq == btv->pll.pll_ofreq) {
  917. /* no PLL needed */
  918. if (btv->pll.pll_current == 0)
  919. return;
  920. bttv_printk(KERN_INFO "bttv%d: PLL can sleep, using XTAL (%d).\n",
  921. btv->c.nr,btv->pll.pll_ifreq);
  922. btwrite(0x00,BT848_TGCTRL);
  923. btwrite(0x00,BT848_PLL_XCI);
  924. btv->pll.pll_current = 0;
  925. return;
  926. }
  927. bttv_printk(KERN_INFO "bttv%d: PLL: %d => %d ",btv->c.nr,
  928. btv->pll.pll_ifreq, btv->pll.pll_ofreq);
  929. set_pll_freq(btv, btv->pll.pll_ifreq, btv->pll.pll_ofreq);
  930. for (i=0; i<10; i++) {
  931. /* Let other people run while the PLL stabilizes */
  932. bttv_printk(".");
  933. msleep(10);
  934. if (btread(BT848_DSTATUS) & BT848_DSTATUS_PLOCK) {
  935. btwrite(0,BT848_DSTATUS);
  936. } else {
  937. btwrite(0x08,BT848_TGCTRL);
  938. btv->pll.pll_current = btv->pll.pll_ofreq;
  939. bttv_printk(" ok\n");
  940. return;
  941. }
  942. }
  943. btv->pll.pll_current = -1;
  944. bttv_printk("failed\n");
  945. return;
  946. }
  947. /* used to switch between the bt848's analog/digital video capture modes */
  948. static void bt848A_set_timing(struct bttv *btv)
  949. {
  950. int i, len;
  951. int table_idx = bttv_tvnorms[btv->tvnorm].sram;
  952. int fsc = bttv_tvnorms[btv->tvnorm].Fsc;
  953. if (btv->input == btv->dig) {
  954. dprintk("bttv%d: load digital timing table (table_idx=%d)\n",
  955. btv->c.nr,table_idx);
  956. /* timing change...reset timing generator address */
  957. btwrite(0x00, BT848_TGCTRL);
  958. btwrite(0x02, BT848_TGCTRL);
  959. btwrite(0x00, BT848_TGCTRL);
  960. len=SRAM_Table[table_idx][0];
  961. for(i = 1; i <= len; i++)
  962. btwrite(SRAM_Table[table_idx][i],BT848_TGLB);
  963. btv->pll.pll_ofreq = 27000000;
  964. set_pll(btv);
  965. btwrite(0x11, BT848_TGCTRL);
  966. btwrite(0x41, BT848_DVSIF);
  967. } else {
  968. btv->pll.pll_ofreq = fsc;
  969. set_pll(btv);
  970. btwrite(0x0, BT848_DVSIF);
  971. }
  972. }
  973. /* ----------------------------------------------------------------------- */
  974. static void bt848_bright(struct bttv *btv, int bright)
  975. {
  976. int value;
  977. // printk("bttv: set bright: %d\n",bright); // DEBUG
  978. btv->bright = bright;
  979. /* We want -128 to 127 we get 0-65535 */
  980. value = (bright >> 8) - 128;
  981. btwrite(value & 0xff, BT848_BRIGHT);
  982. }
  983. static void bt848_hue(struct bttv *btv, int hue)
  984. {
  985. int value;
  986. btv->hue = hue;
  987. /* -128 to 127 */
  988. value = (hue >> 8) - 128;
  989. btwrite(value & 0xff, BT848_HUE);
  990. }
  991. static void bt848_contrast(struct bttv *btv, int cont)
  992. {
  993. int value,hibit;
  994. btv->contrast = cont;
  995. /* 0-511 */
  996. value = (cont >> 7);
  997. hibit = (value >> 6) & 4;
  998. btwrite(value & 0xff, BT848_CONTRAST_LO);
  999. btaor(hibit, ~4, BT848_E_CONTROL);
  1000. btaor(hibit, ~4, BT848_O_CONTROL);
  1001. }
  1002. static void bt848_sat(struct bttv *btv, int color)
  1003. {
  1004. int val_u,val_v,hibits;
  1005. btv->saturation = color;
  1006. /* 0-511 for the color */
  1007. val_u = ((color * btv->opt_uv_ratio) / 50) >> 7;
  1008. val_v = (((color * (100 - btv->opt_uv_ratio) / 50) >>7)*180L)/254;
  1009. hibits = (val_u >> 7) & 2;
  1010. hibits |= (val_v >> 8) & 1;
  1011. btwrite(val_u & 0xff, BT848_SAT_U_LO);
  1012. btwrite(val_v & 0xff, BT848_SAT_V_LO);
  1013. btaor(hibits, ~3, BT848_E_CONTROL);
  1014. btaor(hibits, ~3, BT848_O_CONTROL);
  1015. }
  1016. /* ----------------------------------------------------------------------- */
  1017. static int
  1018. video_mux(struct bttv *btv, unsigned int input)
  1019. {
  1020. int mux,mask2;
  1021. if (input >= bttv_tvcards[btv->c.type].video_inputs)
  1022. return -EINVAL;
  1023. /* needed by RemoteVideo MX */
  1024. mask2 = bttv_tvcards[btv->c.type].gpiomask2;
  1025. if (mask2)
  1026. gpio_inout(mask2,mask2);
  1027. if (input == btv->svhs) {
  1028. btor(BT848_CONTROL_COMP, BT848_E_CONTROL);
  1029. btor(BT848_CONTROL_COMP, BT848_O_CONTROL);
  1030. } else {
  1031. btand(~BT848_CONTROL_COMP, BT848_E_CONTROL);
  1032. btand(~BT848_CONTROL_COMP, BT848_O_CONTROL);
  1033. }
  1034. mux = bttv_muxsel(btv, input);
  1035. btaor(mux<<5, ~(3<<5), BT848_IFORM);
  1036. dprintk(KERN_DEBUG "bttv%d: video mux: input=%d mux=%d\n",
  1037. btv->c.nr,input,mux);
  1038. /* card specific hook */
  1039. if(bttv_tvcards[btv->c.type].muxsel_hook)
  1040. bttv_tvcards[btv->c.type].muxsel_hook (btv, input);
  1041. return 0;
  1042. }
  1043. static char *audio_modes[] = {
  1044. "audio: tuner", "audio: radio", "audio: extern",
  1045. "audio: intern", "audio: mute"
  1046. };
  1047. static int
  1048. audio_mux(struct bttv *btv, int input, int mute)
  1049. {
  1050. int gpio_val, signal;
  1051. struct v4l2_control ctrl;
  1052. gpio_inout(bttv_tvcards[btv->c.type].gpiomask,
  1053. bttv_tvcards[btv->c.type].gpiomask);
  1054. signal = btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC;
  1055. btv->mute = mute;
  1056. btv->audio = input;
  1057. /* automute */
  1058. mute = mute || (btv->opt_automute && !signal && !btv->radio_user);
  1059. if (mute)
  1060. gpio_val = bttv_tvcards[btv->c.type].gpiomute;
  1061. else
  1062. gpio_val = bttv_tvcards[btv->c.type].gpiomux[input];
  1063. switch (btv->c.type) {
  1064. case BTTV_BOARD_VOODOOTV_FM:
  1065. case BTTV_BOARD_VOODOOTV_200:
  1066. gpio_val = bttv_tda9880_setnorm(btv, gpio_val);
  1067. break;
  1068. default:
  1069. gpio_bits(bttv_tvcards[btv->c.type].gpiomask, gpio_val);
  1070. }
  1071. if (bttv_gpio)
  1072. bttv_gpio_tracking(btv, audio_modes[mute ? 4 : input]);
  1073. if (in_interrupt())
  1074. return 0;
  1075. ctrl.id = V4L2_CID_AUDIO_MUTE;
  1076. ctrl.value = btv->mute;
  1077. bttv_call_all(btv, core, s_ctrl, &ctrl);
  1078. if (btv->sd_msp34xx) {
  1079. u32 in;
  1080. /* Note: the inputs tuner/radio/extern/intern are translated
  1081. to msp routings. This assumes common behavior for all msp3400
  1082. based TV cards. When this assumption fails, then the
  1083. specific MSP routing must be added to the card table.
  1084. For now this is sufficient. */
  1085. switch (input) {
  1086. case TVAUDIO_INPUT_RADIO:
  1087. in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  1088. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  1089. break;
  1090. case TVAUDIO_INPUT_EXTERN:
  1091. in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER1,
  1092. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  1093. break;
  1094. case TVAUDIO_INPUT_INTERN:
  1095. /* Yes, this is the same input as for RADIO. I doubt
  1096. if this is ever used. The only board with an INTERN
  1097. input is the BTTV_BOARD_AVERMEDIA98. I wonder how
  1098. that was tested. My guess is that the whole INTERN
  1099. input does not work. */
  1100. in = MSP_INPUT(MSP_IN_SCART2, MSP_IN_TUNER1,
  1101. MSP_DSP_IN_SCART, MSP_DSP_IN_SCART);
  1102. break;
  1103. case TVAUDIO_INPUT_TUNER:
  1104. default:
  1105. /* This is the only card that uses TUNER2, and afaik,
  1106. is the only difference between the VOODOOTV_FM
  1107. and VOODOOTV_200 */
  1108. if (btv->c.type == BTTV_BOARD_VOODOOTV_200)
  1109. in = MSP_INPUT(MSP_IN_SCART1, MSP_IN_TUNER2, \
  1110. MSP_DSP_IN_TUNER, MSP_DSP_IN_TUNER);
  1111. else
  1112. in = MSP_INPUT_DEFAULT;
  1113. break;
  1114. }
  1115. v4l2_subdev_call(btv->sd_msp34xx, audio, s_routing,
  1116. in, MSP_OUTPUT_DEFAULT, 0);
  1117. }
  1118. if (btv->sd_tvaudio) {
  1119. v4l2_subdev_call(btv->sd_tvaudio, audio, s_routing,
  1120. input, 0, 0);
  1121. }
  1122. return 0;
  1123. }
  1124. static inline int
  1125. audio_mute(struct bttv *btv, int mute)
  1126. {
  1127. return audio_mux(btv, btv->audio, mute);
  1128. }
  1129. static inline int
  1130. audio_input(struct bttv *btv, int input)
  1131. {
  1132. return audio_mux(btv, input, btv->mute);
  1133. }
  1134. static void
  1135. bttv_crop_calc_limits(struct bttv_crop *c)
  1136. {
  1137. /* Scale factor min. 1:1, max. 16:1. Min. image size
  1138. 48 x 32. Scaled width must be a multiple of 4. */
  1139. if (1) {
  1140. /* For bug compatibility with VIDIOCGCAP and image
  1141. size checks in earlier driver versions. */
  1142. c->min_scaled_width = 48;
  1143. c->min_scaled_height = 32;
  1144. } else {
  1145. c->min_scaled_width =
  1146. (max(48, c->rect.width >> 4) + 3) & ~3;
  1147. c->min_scaled_height =
  1148. max(32, c->rect.height >> 4);
  1149. }
  1150. c->max_scaled_width = c->rect.width & ~3;
  1151. c->max_scaled_height = c->rect.height;
  1152. }
  1153. static void
  1154. bttv_crop_reset(struct bttv_crop *c, unsigned int norm)
  1155. {
  1156. c->rect = bttv_tvnorms[norm].cropcap.defrect;
  1157. bttv_crop_calc_limits(c);
  1158. }
  1159. /* Call with btv->lock down. */
  1160. static int
  1161. set_tvnorm(struct bttv *btv, unsigned int norm)
  1162. {
  1163. const struct bttv_tvnorm *tvnorm;
  1164. v4l2_std_id id;
  1165. BUG_ON(norm >= BTTV_TVNORMS);
  1166. BUG_ON(btv->tvnorm >= BTTV_TVNORMS);
  1167. tvnorm = &bttv_tvnorms[norm];
  1168. if (memcmp(&bttv_tvnorms[btv->tvnorm].cropcap, &tvnorm->cropcap,
  1169. sizeof (tvnorm->cropcap))) {
  1170. bttv_crop_reset(&btv->crop[0], norm);
  1171. btv->crop[1] = btv->crop[0]; /* current = default */
  1172. if (0 == (btv->resources & VIDEO_RESOURCES)) {
  1173. btv->crop_start = tvnorm->cropcap.bounds.top
  1174. + tvnorm->cropcap.bounds.height;
  1175. }
  1176. }
  1177. btv->tvnorm = norm;
  1178. btwrite(tvnorm->adelay, BT848_ADELAY);
  1179. btwrite(tvnorm->bdelay, BT848_BDELAY);
  1180. btaor(tvnorm->iform,~(BT848_IFORM_NORM|BT848_IFORM_XTBOTH),
  1181. BT848_IFORM);
  1182. btwrite(tvnorm->vbipack, BT848_VBI_PACK_SIZE);
  1183. btwrite(1, BT848_VBI_PACK_DEL);
  1184. bt848A_set_timing(btv);
  1185. switch (btv->c.type) {
  1186. case BTTV_BOARD_VOODOOTV_FM:
  1187. case BTTV_BOARD_VOODOOTV_200:
  1188. bttv_tda9880_setnorm(btv, gpio_read());
  1189. break;
  1190. }
  1191. id = tvnorm->v4l2_id;
  1192. bttv_call_all(btv, core, s_std, id);
  1193. return 0;
  1194. }
  1195. /* Call with btv->lock down. */
  1196. static void
  1197. set_input(struct bttv *btv, unsigned int input, unsigned int norm)
  1198. {
  1199. unsigned long flags;
  1200. btv->input = input;
  1201. if (irq_iswitch) {
  1202. spin_lock_irqsave(&btv->s_lock,flags);
  1203. if (btv->curr.frame_irq) {
  1204. /* active capture -> delayed input switch */
  1205. btv->new_input = input;
  1206. } else {
  1207. video_mux(btv,input);
  1208. }
  1209. spin_unlock_irqrestore(&btv->s_lock,flags);
  1210. } else {
  1211. video_mux(btv,input);
  1212. }
  1213. audio_input(btv, (btv->tuner_type != TUNER_ABSENT && input == 0) ?
  1214. TVAUDIO_INPUT_TUNER : TVAUDIO_INPUT_EXTERN);
  1215. set_tvnorm(btv, norm);
  1216. }
  1217. static void init_irqreg(struct bttv *btv)
  1218. {
  1219. /* clear status */
  1220. btwrite(0xfffffUL, BT848_INT_STAT);
  1221. if (bttv_tvcards[btv->c.type].no_video) {
  1222. /* i2c only */
  1223. btwrite(BT848_INT_I2CDONE,
  1224. BT848_INT_MASK);
  1225. } else {
  1226. /* full video */
  1227. btwrite((btv->triton1) |
  1228. (btv->gpioirq ? BT848_INT_GPINT : 0) |
  1229. BT848_INT_SCERR |
  1230. (fdsr ? BT848_INT_FDSR : 0) |
  1231. BT848_INT_RISCI | BT848_INT_OCERR |
  1232. BT848_INT_FMTCHG|BT848_INT_HLOCK|
  1233. BT848_INT_I2CDONE,
  1234. BT848_INT_MASK);
  1235. }
  1236. }
  1237. static void init_bt848(struct bttv *btv)
  1238. {
  1239. int val;
  1240. if (bttv_tvcards[btv->c.type].no_video) {
  1241. /* very basic init only */
  1242. init_irqreg(btv);
  1243. return;
  1244. }
  1245. btwrite(0x00, BT848_CAP_CTL);
  1246. btwrite(BT848_COLOR_CTL_GAMMA, BT848_COLOR_CTL);
  1247. btwrite(BT848_IFORM_XTAUTO | BT848_IFORM_AUTO, BT848_IFORM);
  1248. /* set planar and packed mode trigger points and */
  1249. /* set rising edge of inverted GPINTR pin as irq trigger */
  1250. btwrite(BT848_GPIO_DMA_CTL_PKTP_32|
  1251. BT848_GPIO_DMA_CTL_PLTP1_16|
  1252. BT848_GPIO_DMA_CTL_PLTP23_16|
  1253. BT848_GPIO_DMA_CTL_GPINTC|
  1254. BT848_GPIO_DMA_CTL_GPINTI,
  1255. BT848_GPIO_DMA_CTL);
  1256. val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
  1257. btwrite(val, BT848_E_SCLOOP);
  1258. btwrite(val, BT848_O_SCLOOP);
  1259. btwrite(0x20, BT848_E_VSCALE_HI);
  1260. btwrite(0x20, BT848_O_VSCALE_HI);
  1261. btwrite(BT848_ADC_RESERVED | (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
  1262. BT848_ADC);
  1263. btwrite(whitecrush_upper, BT848_WC_UP);
  1264. btwrite(whitecrush_lower, BT848_WC_DOWN);
  1265. if (btv->opt_lumafilter) {
  1266. btwrite(0, BT848_E_CONTROL);
  1267. btwrite(0, BT848_O_CONTROL);
  1268. } else {
  1269. btwrite(BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1270. btwrite(BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1271. }
  1272. bt848_bright(btv, btv->bright);
  1273. bt848_hue(btv, btv->hue);
  1274. bt848_contrast(btv, btv->contrast);
  1275. bt848_sat(btv, btv->saturation);
  1276. /* interrupt */
  1277. init_irqreg(btv);
  1278. }
  1279. static void bttv_reinit_bt848(struct bttv *btv)
  1280. {
  1281. unsigned long flags;
  1282. if (bttv_verbose)
  1283. printk(KERN_INFO "bttv%d: reset, reinitialize\n",btv->c.nr);
  1284. spin_lock_irqsave(&btv->s_lock,flags);
  1285. btv->errors=0;
  1286. bttv_set_dma(btv,0);
  1287. spin_unlock_irqrestore(&btv->s_lock,flags);
  1288. init_bt848(btv);
  1289. btv->pll.pll_current = -1;
  1290. set_input(btv, btv->input, btv->tvnorm);
  1291. }
  1292. static int bttv_g_ctrl(struct file *file, void *priv,
  1293. struct v4l2_control *c)
  1294. {
  1295. struct bttv_fh *fh = priv;
  1296. struct bttv *btv = fh->btv;
  1297. switch (c->id) {
  1298. case V4L2_CID_BRIGHTNESS:
  1299. c->value = btv->bright;
  1300. break;
  1301. case V4L2_CID_HUE:
  1302. c->value = btv->hue;
  1303. break;
  1304. case V4L2_CID_CONTRAST:
  1305. c->value = btv->contrast;
  1306. break;
  1307. case V4L2_CID_SATURATION:
  1308. c->value = btv->saturation;
  1309. break;
  1310. case V4L2_CID_AUDIO_MUTE:
  1311. case V4L2_CID_AUDIO_VOLUME:
  1312. case V4L2_CID_AUDIO_BALANCE:
  1313. case V4L2_CID_AUDIO_BASS:
  1314. case V4L2_CID_AUDIO_TREBLE:
  1315. bttv_call_all(btv, core, g_ctrl, c);
  1316. break;
  1317. case V4L2_CID_PRIVATE_CHROMA_AGC:
  1318. c->value = btv->opt_chroma_agc;
  1319. break;
  1320. case V4L2_CID_PRIVATE_COMBFILTER:
  1321. c->value = btv->opt_combfilter;
  1322. break;
  1323. case V4L2_CID_PRIVATE_LUMAFILTER:
  1324. c->value = btv->opt_lumafilter;
  1325. break;
  1326. case V4L2_CID_PRIVATE_AUTOMUTE:
  1327. c->value = btv->opt_automute;
  1328. break;
  1329. case V4L2_CID_PRIVATE_AGC_CRUSH:
  1330. c->value = btv->opt_adc_crush;
  1331. break;
  1332. case V4L2_CID_PRIVATE_VCR_HACK:
  1333. c->value = btv->opt_vcr_hack;
  1334. break;
  1335. case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
  1336. c->value = btv->opt_whitecrush_upper;
  1337. break;
  1338. case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
  1339. c->value = btv->opt_whitecrush_lower;
  1340. break;
  1341. case V4L2_CID_PRIVATE_UV_RATIO:
  1342. c->value = btv->opt_uv_ratio;
  1343. break;
  1344. case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
  1345. c->value = btv->opt_full_luma_range;
  1346. break;
  1347. case V4L2_CID_PRIVATE_CORING:
  1348. c->value = btv->opt_coring;
  1349. break;
  1350. default:
  1351. return -EINVAL;
  1352. }
  1353. return 0;
  1354. }
  1355. static int bttv_s_ctrl(struct file *file, void *f,
  1356. struct v4l2_control *c)
  1357. {
  1358. int err;
  1359. int val;
  1360. struct bttv_fh *fh = f;
  1361. struct bttv *btv = fh->btv;
  1362. err = v4l2_prio_check(&btv->prio, fh->prio);
  1363. if (0 != err)
  1364. return err;
  1365. switch (c->id) {
  1366. case V4L2_CID_BRIGHTNESS:
  1367. bt848_bright(btv, c->value);
  1368. break;
  1369. case V4L2_CID_HUE:
  1370. bt848_hue(btv, c->value);
  1371. break;
  1372. case V4L2_CID_CONTRAST:
  1373. bt848_contrast(btv, c->value);
  1374. break;
  1375. case V4L2_CID_SATURATION:
  1376. bt848_sat(btv, c->value);
  1377. break;
  1378. case V4L2_CID_AUDIO_MUTE:
  1379. audio_mute(btv, c->value);
  1380. /* fall through */
  1381. case V4L2_CID_AUDIO_VOLUME:
  1382. if (btv->volume_gpio)
  1383. btv->volume_gpio(btv, c->value);
  1384. bttv_call_all(btv, core, s_ctrl, c);
  1385. break;
  1386. case V4L2_CID_AUDIO_BALANCE:
  1387. case V4L2_CID_AUDIO_BASS:
  1388. case V4L2_CID_AUDIO_TREBLE:
  1389. bttv_call_all(btv, core, s_ctrl, c);
  1390. break;
  1391. case V4L2_CID_PRIVATE_CHROMA_AGC:
  1392. btv->opt_chroma_agc = c->value;
  1393. val = btv->opt_chroma_agc ? BT848_SCLOOP_CAGC : 0;
  1394. btwrite(val, BT848_E_SCLOOP);
  1395. btwrite(val, BT848_O_SCLOOP);
  1396. break;
  1397. case V4L2_CID_PRIVATE_COMBFILTER:
  1398. btv->opt_combfilter = c->value;
  1399. break;
  1400. case V4L2_CID_PRIVATE_LUMAFILTER:
  1401. btv->opt_lumafilter = c->value;
  1402. if (btv->opt_lumafilter) {
  1403. btand(~BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1404. btand(~BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1405. } else {
  1406. btor(BT848_CONTROL_LDEC, BT848_E_CONTROL);
  1407. btor(BT848_CONTROL_LDEC, BT848_O_CONTROL);
  1408. }
  1409. break;
  1410. case V4L2_CID_PRIVATE_AUTOMUTE:
  1411. btv->opt_automute = c->value;
  1412. break;
  1413. case V4L2_CID_PRIVATE_AGC_CRUSH:
  1414. btv->opt_adc_crush = c->value;
  1415. btwrite(BT848_ADC_RESERVED |
  1416. (btv->opt_adc_crush ? BT848_ADC_CRUSH : 0),
  1417. BT848_ADC);
  1418. break;
  1419. case V4L2_CID_PRIVATE_VCR_HACK:
  1420. btv->opt_vcr_hack = c->value;
  1421. break;
  1422. case V4L2_CID_PRIVATE_WHITECRUSH_UPPER:
  1423. btv->opt_whitecrush_upper = c->value;
  1424. btwrite(c->value, BT848_WC_UP);
  1425. break;
  1426. case V4L2_CID_PRIVATE_WHITECRUSH_LOWER:
  1427. btv->opt_whitecrush_lower = c->value;
  1428. btwrite(c->value, BT848_WC_DOWN);
  1429. break;
  1430. case V4L2_CID_PRIVATE_UV_RATIO:
  1431. btv->opt_uv_ratio = c->value;
  1432. bt848_sat(btv, btv->saturation);
  1433. break;
  1434. case V4L2_CID_PRIVATE_FULL_LUMA_RANGE:
  1435. btv->opt_full_luma_range = c->value;
  1436. btaor((c->value<<7), ~BT848_OFORM_RANGE, BT848_OFORM);
  1437. break;
  1438. case V4L2_CID_PRIVATE_CORING:
  1439. btv->opt_coring = c->value;
  1440. btaor((c->value<<5), ~BT848_OFORM_CORE32, BT848_OFORM);
  1441. break;
  1442. default:
  1443. return -EINVAL;
  1444. }
  1445. return 0;
  1446. }
  1447. /* ----------------------------------------------------------------------- */
  1448. void bttv_gpio_tracking(struct bttv *btv, char *comment)
  1449. {
  1450. unsigned int outbits, data;
  1451. outbits = btread(BT848_GPIO_OUT_EN);
  1452. data = btread(BT848_GPIO_DATA);
  1453. printk(KERN_DEBUG "bttv%d: gpio: en=%08x, out=%08x in=%08x [%s]\n",
  1454. btv->c.nr,outbits,data & outbits, data & ~outbits, comment);
  1455. }
  1456. static void bttv_field_count(struct bttv *btv)
  1457. {
  1458. int need_count = 0;
  1459. if (btv->users)
  1460. need_count++;
  1461. if (need_count) {
  1462. /* start field counter */
  1463. btor(BT848_INT_VSYNC,BT848_INT_MASK);
  1464. } else {
  1465. /* stop field counter */
  1466. btand(~BT848_INT_VSYNC,BT848_INT_MASK);
  1467. btv->field_count = 0;
  1468. }
  1469. }
  1470. static const struct bttv_format*
  1471. format_by_fourcc(int fourcc)
  1472. {
  1473. unsigned int i;
  1474. for (i = 0; i < FORMATS; i++) {
  1475. if (-1 == formats[i].fourcc)
  1476. continue;
  1477. if (formats[i].fourcc == fourcc)
  1478. return formats+i;
  1479. }
  1480. return NULL;
  1481. }
  1482. /* ----------------------------------------------------------------------- */
  1483. /* misc helpers */
  1484. static int
  1485. bttv_switch_overlay(struct bttv *btv, struct bttv_fh *fh,
  1486. struct bttv_buffer *new)
  1487. {
  1488. struct bttv_buffer *old;
  1489. unsigned long flags;
  1490. int retval = 0;
  1491. dprintk("switch_overlay: enter [new=%p]\n",new);
  1492. if (new)
  1493. new->vb.state = VIDEOBUF_DONE;
  1494. spin_lock_irqsave(&btv->s_lock,flags);
  1495. old = btv->screen;
  1496. btv->screen = new;
  1497. btv->loop_irq |= 1;
  1498. bttv_set_dma(btv, 0x03);
  1499. spin_unlock_irqrestore(&btv->s_lock,flags);
  1500. if (NULL != old) {
  1501. dprintk("switch_overlay: old=%p state is %d\n",old,old->vb.state);
  1502. bttv_dma_free(&fh->cap,btv, old);
  1503. kfree(old);
  1504. }
  1505. if (NULL == new)
  1506. free_btres(btv,fh,RESOURCE_OVERLAY);
  1507. dprintk("switch_overlay: done\n");
  1508. return retval;
  1509. }
  1510. /* ----------------------------------------------------------------------- */
  1511. /* video4linux (1) interface */
  1512. static int bttv_prepare_buffer(struct videobuf_queue *q,struct bttv *btv,
  1513. struct bttv_buffer *buf,
  1514. const struct bttv_format *fmt,
  1515. unsigned int width, unsigned int height,
  1516. enum v4l2_field field)
  1517. {
  1518. struct bttv_fh *fh = q->priv_data;
  1519. int redo_dma_risc = 0;
  1520. struct bttv_crop c;
  1521. int norm;
  1522. int rc;
  1523. /* check settings */
  1524. if (NULL == fmt)
  1525. return -EINVAL;
  1526. if (fmt->btformat == BT848_COLOR_FMT_RAW) {
  1527. width = RAW_BPL;
  1528. height = RAW_LINES*2;
  1529. if (width*height > buf->vb.bsize)
  1530. return -EINVAL;
  1531. buf->vb.size = buf->vb.bsize;
  1532. /* Make sure tvnorm and vbi_end remain consistent
  1533. until we're done. */
  1534. mutex_lock(&btv->lock);
  1535. norm = btv->tvnorm;
  1536. /* In this mode capturing always starts at defrect.top
  1537. (default VDELAY), ignoring cropping parameters. */
  1538. if (btv->vbi_end > bttv_tvnorms[norm].cropcap.defrect.top) {
  1539. mutex_unlock(&btv->lock);
  1540. return -EINVAL;
  1541. }
  1542. mutex_unlock(&btv->lock);
  1543. c.rect = bttv_tvnorms[norm].cropcap.defrect;
  1544. } else {
  1545. mutex_lock(&btv->lock);
  1546. norm = btv->tvnorm;
  1547. c = btv->crop[!!fh->do_crop];
  1548. mutex_unlock(&btv->lock);
  1549. if (width < c.min_scaled_width ||
  1550. width > c.max_scaled_width ||
  1551. height < c.min_scaled_height)
  1552. return -EINVAL;
  1553. switch (field) {
  1554. case V4L2_FIELD_TOP:
  1555. case V4L2_FIELD_BOTTOM:
  1556. case V4L2_FIELD_ALTERNATE:
  1557. /* btv->crop counts frame lines. Max. scale
  1558. factor is 16:1 for frames, 8:1 for fields. */
  1559. if (height * 2 > c.max_scaled_height)
  1560. return -EINVAL;
  1561. break;
  1562. default:
  1563. if (height > c.max_scaled_height)
  1564. return -EINVAL;
  1565. break;
  1566. }
  1567. buf->vb.size = (width * height * fmt->depth) >> 3;
  1568. if (0 != buf->vb.baddr && buf->vb.bsize < buf->vb.size)
  1569. return -EINVAL;
  1570. }
  1571. /* alloc + fill struct bttv_buffer (if changed) */
  1572. if (buf->vb.width != width || buf->vb.height != height ||
  1573. buf->vb.field != field ||
  1574. buf->tvnorm != norm || buf->fmt != fmt ||
  1575. buf->crop.top != c.rect.top ||
  1576. buf->crop.left != c.rect.left ||
  1577. buf->crop.width != c.rect.width ||
  1578. buf->crop.height != c.rect.height) {
  1579. buf->vb.width = width;
  1580. buf->vb.height = height;
  1581. buf->vb.field = field;
  1582. buf->tvnorm = norm;
  1583. buf->fmt = fmt;
  1584. buf->crop = c.rect;
  1585. redo_dma_risc = 1;
  1586. }
  1587. /* alloc risc memory */
  1588. if (VIDEOBUF_NEEDS_INIT == buf->vb.state) {
  1589. redo_dma_risc = 1;
  1590. if (0 != (rc = videobuf_iolock(q,&buf->vb,&btv->fbuf)))
  1591. goto fail;
  1592. }
  1593. if (redo_dma_risc)
  1594. if (0 != (rc = bttv_buffer_risc(btv,buf)))
  1595. goto fail;
  1596. buf->vb.state = VIDEOBUF_PREPARED;