PageRenderTime 73ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/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
Possible License(s): GPL-2.0
  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;
  1597. return 0;
  1598. fail:
  1599. bttv_dma_free(q,btv,buf);
  1600. return rc;
  1601. }
  1602. static int
  1603. buffer_setup(struct videobuf_queue *q, unsigned int *count, unsigned int *size)
  1604. {
  1605. struct bttv_fh *fh = q->priv_data;
  1606. *size = fh->fmt->depth*fh->width*fh->height >> 3;
  1607. if (0 == *count)
  1608. *count = gbuffers;
  1609. if (*size * *count > gbuffers * gbufsize)
  1610. *count = (gbuffers * gbufsize) / *size;
  1611. return 0;
  1612. }
  1613. static int
  1614. buffer_prepare(struct videobuf_queue *q, struct videobuf_buffer *vb,
  1615. enum v4l2_field field)
  1616. {
  1617. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1618. struct bttv_fh *fh = q->priv_data;
  1619. return bttv_prepare_buffer(q,fh->btv, buf, fh->fmt,
  1620. fh->width, fh->height, field);
  1621. }
  1622. static void
  1623. buffer_queue(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1624. {
  1625. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1626. struct bttv_fh *fh = q->priv_data;
  1627. struct bttv *btv = fh->btv;
  1628. buf->vb.state = VIDEOBUF_QUEUED;
  1629. list_add_tail(&buf->vb.queue,&btv->capture);
  1630. if (!btv->curr.frame_irq) {
  1631. btv->loop_irq |= 1;
  1632. bttv_set_dma(btv, 0x03);
  1633. }
  1634. }
  1635. static void buffer_release(struct videobuf_queue *q, struct videobuf_buffer *vb)
  1636. {
  1637. struct bttv_buffer *buf = container_of(vb,struct bttv_buffer,vb);
  1638. struct bttv_fh *fh = q->priv_data;
  1639. bttv_dma_free(q,fh->btv,buf);
  1640. }
  1641. static struct videobuf_queue_ops bttv_video_qops = {
  1642. .buf_setup = buffer_setup,
  1643. .buf_prepare = buffer_prepare,
  1644. .buf_queue = buffer_queue,
  1645. .buf_release = buffer_release,
  1646. };
  1647. static int bttv_s_std(struct file *file, void *priv, v4l2_std_id *id)
  1648. {
  1649. struct bttv_fh *fh = priv;
  1650. struct bttv *btv = fh->btv;
  1651. unsigned int i;
  1652. int err;
  1653. err = v4l2_prio_check(&btv->prio, fh->prio);
  1654. if (0 != err)
  1655. return err;
  1656. for (i = 0; i < BTTV_TVNORMS; i++)
  1657. if (*id & bttv_tvnorms[i].v4l2_id)
  1658. break;
  1659. if (i == BTTV_TVNORMS)
  1660. return -EINVAL;
  1661. mutex_lock(&btv->lock);
  1662. set_tvnorm(btv, i);
  1663. mutex_unlock(&btv->lock);
  1664. return 0;
  1665. }
  1666. static int bttv_querystd(struct file *file, void *f, v4l2_std_id *id)
  1667. {
  1668. struct bttv_fh *fh = f;
  1669. struct bttv *btv = fh->btv;
  1670. if (btread(BT848_DSTATUS) & BT848_DSTATUS_NUML)
  1671. *id = V4L2_STD_625_50;
  1672. else
  1673. *id = V4L2_STD_525_60;
  1674. return 0;
  1675. }
  1676. static int bttv_enum_input(struct file *file, void *priv,
  1677. struct v4l2_input *i)
  1678. {
  1679. struct bttv_fh *fh = priv;
  1680. struct bttv *btv = fh->btv;
  1681. int n;
  1682. if (i->index >= bttv_tvcards[btv->c.type].video_inputs)
  1683. return -EINVAL;
  1684. i->type = V4L2_INPUT_TYPE_CAMERA;
  1685. i->audioset = 1;
  1686. if (btv->tuner_type != TUNER_ABSENT && i->index == 0) {
  1687. sprintf(i->name, "Television");
  1688. i->type = V4L2_INPUT_TYPE_TUNER;
  1689. i->tuner = 0;
  1690. } else if (i->index == btv->svhs) {
  1691. sprintf(i->name, "S-Video");
  1692. } else {
  1693. sprintf(i->name, "Composite%d", i->index);
  1694. }
  1695. if (i->index == btv->input) {
  1696. __u32 dstatus = btread(BT848_DSTATUS);
  1697. if (0 == (dstatus & BT848_DSTATUS_PRES))
  1698. i->status |= V4L2_IN_ST_NO_SIGNAL;
  1699. if (0 == (dstatus & BT848_DSTATUS_HLOC))
  1700. i->status |= V4L2_IN_ST_NO_H_LOCK;
  1701. }
  1702. for (n = 0; n < BTTV_TVNORMS; n++)
  1703. i->std |= bttv_tvnorms[n].v4l2_id;
  1704. return 0;
  1705. }
  1706. static int bttv_g_input(struct file *file, void *priv, unsigned int *i)
  1707. {
  1708. struct bttv_fh *fh = priv;
  1709. struct bttv *btv = fh->btv;
  1710. *i = btv->input;
  1711. return 0;
  1712. }
  1713. static int bttv_s_input(struct file *file, void *priv, unsigned int i)
  1714. {
  1715. struct bttv_fh *fh = priv;
  1716. struct bttv *btv = fh->btv;
  1717. int err;
  1718. err = v4l2_prio_check(&btv->prio, fh->prio);
  1719. if (0 != err)
  1720. return err;
  1721. if (i > bttv_tvcards[btv->c.type].video_inputs)
  1722. return -EINVAL;
  1723. mutex_lock(&btv->lock);
  1724. set_input(btv, i, btv->tvnorm);
  1725. mutex_unlock(&btv->lock);
  1726. return 0;
  1727. }
  1728. static int bttv_s_tuner(struct file *file, void *priv,
  1729. struct v4l2_tuner *t)
  1730. {
  1731. struct bttv_fh *fh = priv;
  1732. struct bttv *btv = fh->btv;
  1733. int err;
  1734. err = v4l2_prio_check(&btv->prio, fh->prio);
  1735. if (0 != err)
  1736. return err;
  1737. if (btv->tuner_type == TUNER_ABSENT)
  1738. return -EINVAL;
  1739. if (0 != t->index)
  1740. return -EINVAL;
  1741. mutex_lock(&btv->lock);
  1742. bttv_call_all(btv, tuner, s_tuner, t);
  1743. if (btv->audio_mode_gpio)
  1744. btv->audio_mode_gpio(btv, t, 1);
  1745. mutex_unlock(&btv->lock);
  1746. return 0;
  1747. }
  1748. static int bttv_g_frequency(struct file *file, void *priv,
  1749. struct v4l2_frequency *f)
  1750. {
  1751. struct bttv_fh *fh = priv;
  1752. struct bttv *btv = fh->btv;
  1753. f->type = btv->radio_user ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV;
  1754. f->frequency = btv->freq;
  1755. return 0;
  1756. }
  1757. static int bttv_s_frequency(struct file *file, void *priv,
  1758. struct v4l2_frequency *f)
  1759. {
  1760. struct bttv_fh *fh = priv;
  1761. struct bttv *btv = fh->btv;
  1762. int err;
  1763. err = v4l2_prio_check(&btv->prio, fh->prio);
  1764. if (0 != err)
  1765. return err;
  1766. if (unlikely(f->tuner != 0))
  1767. return -EINVAL;
  1768. if (unlikely(f->type != (btv->radio_user
  1769. ? V4L2_TUNER_RADIO : V4L2_TUNER_ANALOG_TV)))
  1770. return -EINVAL;
  1771. mutex_lock(&btv->lock);
  1772. btv->freq = f->frequency;
  1773. bttv_call_all(btv, tuner, s_frequency, f);
  1774. if (btv->has_matchbox && btv->radio_user)
  1775. tea5757_set_freq(btv, btv->freq);
  1776. mutex_unlock(&btv->lock);
  1777. return 0;
  1778. }
  1779. static int bttv_log_status(struct file *file, void *f)
  1780. {
  1781. struct bttv_fh *fh = f;
  1782. struct bttv *btv = fh->btv;
  1783. printk(KERN_INFO "bttv%d: ======== START STATUS CARD #%d ========\n",
  1784. btv->c.nr, btv->c.nr);
  1785. bttv_call_all(btv, core, log_status);
  1786. printk(KERN_INFO "bttv%d: ======== END STATUS CARD #%d ========\n",
  1787. btv->c.nr, btv->c.nr);
  1788. return 0;
  1789. }
  1790. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1791. static int bttv_g_register(struct file *file, void *f,
  1792. struct v4l2_dbg_register *reg)
  1793. {
  1794. struct bttv_fh *fh = f;
  1795. struct bttv *btv = fh->btv;
  1796. if (!capable(CAP_SYS_ADMIN))
  1797. return -EPERM;
  1798. if (!v4l2_chip_match_host(&reg->match))
  1799. return -EINVAL;
  1800. /* bt848 has a 12-bit register space */
  1801. reg->reg &= 0xfff;
  1802. reg->val = btread(reg->reg);
  1803. reg->size = 1;
  1804. return 0;
  1805. }
  1806. static int bttv_s_register(struct file *file, void *f,
  1807. struct v4l2_dbg_register *reg)
  1808. {
  1809. struct bttv_fh *fh = f;
  1810. struct bttv *btv = fh->btv;
  1811. if (!capable(CAP_SYS_ADMIN))
  1812. return -EPERM;
  1813. if (!v4l2_chip_match_host(&reg->match))
  1814. return -EINVAL;
  1815. /* bt848 has a 12-bit register space */
  1816. reg->reg &= 0xfff;
  1817. btwrite(reg->val, reg->reg);
  1818. return 0;
  1819. }
  1820. #endif
  1821. /* Given cropping boundaries b and the scaled width and height of a
  1822. single field or frame, which must not exceed hardware limits, this
  1823. function adjusts the cropping parameters c. */
  1824. static void
  1825. bttv_crop_adjust (struct bttv_crop * c,
  1826. const struct v4l2_rect * b,
  1827. __s32 width,
  1828. __s32 height,
  1829. enum v4l2_field field)
  1830. {
  1831. __s32 frame_height = height << !V4L2_FIELD_HAS_BOTH(field);
  1832. __s32 max_left;
  1833. __s32 max_top;
  1834. if (width < c->min_scaled_width) {
  1835. /* Max. hor. scale factor 16:1. */
  1836. c->rect.width = width * 16;
  1837. } else if (width > c->max_scaled_width) {
  1838. /* Min. hor. scale factor 1:1. */
  1839. c->rect.width = width;
  1840. max_left = b->left + b->width - width;
  1841. max_left = min(max_left, (__s32) MAX_HDELAY);
  1842. if (c->rect.left > max_left)
  1843. c->rect.left = max_left;
  1844. }
  1845. if (height < c->min_scaled_height) {
  1846. /* Max. vert. scale factor 16:1, single fields 8:1. */
  1847. c->rect.height = height * 16;
  1848. } else if (frame_height > c->max_scaled_height) {
  1849. /* Min. vert. scale factor 1:1.
  1850. Top and height count field lines times two. */
  1851. c->rect.height = (frame_height + 1) & ~1;
  1852. max_top = b->top + b->height - c->rect.height;
  1853. if (c->rect.top > max_top)
  1854. c->rect.top = max_top;
  1855. }
  1856. bttv_crop_calc_limits(c);
  1857. }
  1858. /* Returns an error if scaling to a frame or single field with the given
  1859. width and height is not possible with the current cropping parameters
  1860. and width aligned according to width_mask. If adjust_size is TRUE the
  1861. function may adjust the width and/or height instead, rounding width
  1862. to (width + width_bias) & width_mask. If adjust_crop is TRUE it may
  1863. also adjust the current cropping parameters to get closer to the
  1864. desired image size. */
  1865. static int
  1866. limit_scaled_size (struct bttv_fh * fh,
  1867. __s32 * width,
  1868. __s32 * height,
  1869. enum v4l2_field field,
  1870. unsigned int width_mask,
  1871. unsigned int width_bias,
  1872. int adjust_size,
  1873. int adjust_crop)
  1874. {
  1875. struct bttv *btv = fh->btv;
  1876. const struct v4l2_rect *b;
  1877. struct bttv_crop *c;
  1878. __s32 min_width;
  1879. __s32 min_height;
  1880. __s32 max_width;
  1881. __s32 max_height;
  1882. int rc;
  1883. BUG_ON((int) width_mask >= 0 ||
  1884. width_bias >= (unsigned int) -width_mask);
  1885. /* Make sure tvnorm, vbi_end and the current cropping parameters
  1886. remain consistent until we're done. */
  1887. mutex_lock(&btv->lock);
  1888. b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  1889. /* Do crop - use current, don't - use default parameters. */
  1890. c = &btv->crop[!!fh->do_crop];
  1891. if (fh->do_crop
  1892. && adjust_size
  1893. && adjust_crop
  1894. && !locked_btres(btv, VIDEO_RESOURCES)) {
  1895. min_width = 48;
  1896. min_height = 32;
  1897. /* We cannot scale up. When the scaled image is larger
  1898. than crop.rect we adjust the crop.rect as required
  1899. by the V4L2 spec, hence cropcap.bounds are our limit. */
  1900. max_width = min(b->width, (__s32) MAX_HACTIVE);
  1901. max_height = b->height;
  1902. /* We cannot capture the same line as video and VBI data.
  1903. Note btv->vbi_end is really a minimum, see
  1904. bttv_vbi_try_fmt(). */
  1905. if (btv->vbi_end > b->top) {
  1906. max_height -= btv->vbi_end - b->top;
  1907. rc = -EBUSY;
  1908. if (min_height > max_height)
  1909. goto fail;
  1910. }
  1911. } else {
  1912. rc = -EBUSY;
  1913. if (btv->vbi_end > c->rect.top)
  1914. goto fail;
  1915. min_width = c->min_scaled_width;
  1916. min_height = c->min_scaled_height;
  1917. max_width = c->max_scaled_width;
  1918. max_height = c->max_scaled_height;
  1919. adjust_crop = 0;
  1920. }
  1921. min_width = (min_width - width_mask - 1) & width_mask;
  1922. max_width = max_width & width_mask;
  1923. /* Max. scale factor is 16:1 for frames, 8:1 for fields. */
  1924. min_height = min_height;
  1925. /* Min. scale factor is 1:1. */
  1926. max_height >>= !V4L2_FIELD_HAS_BOTH(field);
  1927. if (adjust_size) {
  1928. *width = clamp(*width, min_width, max_width);
  1929. *height = clamp(*height, min_height, max_height);
  1930. /* Round after clamping to avoid overflow. */
  1931. *width = (*width + width_bias) & width_mask;
  1932. if (adjust_crop) {
  1933. bttv_crop_adjust(c, b, *width, *height, field);
  1934. if (btv->vbi_end > c->rect.top) {
  1935. /* Move the crop window out of the way. */
  1936. c->rect.top = btv->vbi_end;
  1937. }
  1938. }
  1939. } else {
  1940. rc = -EINVAL;
  1941. if (*width < min_width ||
  1942. *height < min_height ||
  1943. *width > max_width ||
  1944. *height > max_height ||
  1945. 0 != (*width & ~width_mask))
  1946. goto fail;
  1947. }
  1948. rc = 0; /* success */
  1949. fail:
  1950. mutex_unlock(&btv->lock);
  1951. return rc;
  1952. }
  1953. /* Returns an error if the given overlay window dimensions are not
  1954. possible with the current cropping parameters. If adjust_size is
  1955. TRUE the function may adjust the window width and/or height
  1956. instead, however it always rounds the horizontal position and
  1957. width as btcx_align() does. If adjust_crop is TRUE the function
  1958. may also adjust the current cropping parameters to get closer
  1959. to the desired window size. */
  1960. static int
  1961. verify_window (struct bttv_fh * fh,
  1962. struct v4l2_window * win,
  1963. int adjust_size,
  1964. int adjust_crop)
  1965. {
  1966. enum v4l2_field field;
  1967. unsigned int width_mask;
  1968. int rc;
  1969. if (win->w.width < 48 || win->w.height < 32)
  1970. return -EINVAL;
  1971. if (win->clipcount > 2048)
  1972. return -EINVAL;
  1973. field = win->field;
  1974. if (V4L2_FIELD_ANY == field) {
  1975. __s32 height2;
  1976. height2 = fh->btv->crop[!!fh->do_crop].rect.height >> 1;
  1977. field = (win->w.height > height2)
  1978. ? V4L2_FIELD_INTERLACED
  1979. : V4L2_FIELD_TOP;
  1980. }
  1981. switch (field) {
  1982. case V4L2_FIELD_TOP:
  1983. case V4L2_FIELD_BOTTOM:
  1984. case V4L2_FIELD_INTERLACED:
  1985. break;
  1986. default:
  1987. return -EINVAL;
  1988. }
  1989. /* 4-byte alignment. */
  1990. if (NULL == fh->ovfmt)
  1991. return -EINVAL;
  1992. width_mask = ~0;
  1993. switch (fh->ovfmt->depth) {
  1994. case 8:
  1995. case 24:
  1996. width_mask = ~3;
  1997. break;
  1998. case 16:
  1999. width_mask = ~1;
  2000. break;
  2001. case 32:
  2002. break;
  2003. default:
  2004. BUG();
  2005. }
  2006. win->w.width -= win->w.left & ~width_mask;
  2007. win->w.left = (win->w.left - width_mask - 1) & width_mask;
  2008. rc = limit_scaled_size(fh, &win->w.width, &win->w.height,
  2009. field, width_mask,
  2010. /* width_bias: round down */ 0,
  2011. adjust_size, adjust_crop);
  2012. if (0 != rc)
  2013. return rc;
  2014. win->field = field;
  2015. return 0;
  2016. }
  2017. static int setup_window(struct bttv_fh *fh, struct bttv *btv,
  2018. struct v4l2_window *win, int fixup)
  2019. {
  2020. struct v4l2_clip *clips = NULL;
  2021. int n,size,retval = 0;
  2022. if (NULL == fh->ovfmt)
  2023. return -EINVAL;
  2024. if (!(fh->ovfmt->flags & FORMAT_FLAGS_PACKED))
  2025. return -EINVAL;
  2026. retval = verify_window(fh, win,
  2027. /* adjust_size */ fixup,
  2028. /* adjust_crop */ fixup);
  2029. if (0 != retval)
  2030. return retval;
  2031. /* copy clips -- luckily v4l1 + v4l2 are binary
  2032. compatible here ...*/
  2033. n = win->clipcount;
  2034. size = sizeof(*clips)*(n+4);
  2035. clips = kmalloc(size,GFP_KERNEL);
  2036. if (NULL == clips)
  2037. return -ENOMEM;
  2038. if (n > 0) {
  2039. if (copy_from_user(clips,win->clips,sizeof(struct v4l2_clip)*n)) {
  2040. kfree(clips);
  2041. return -EFAULT;
  2042. }
  2043. }
  2044. /* clip against screen */
  2045. if (NULL != btv->fbuf.base)
  2046. n = btcx_screen_clips(btv->fbuf.fmt.width, btv->fbuf.fmt.height,
  2047. &win->w, clips, n);
  2048. btcx_sort_clips(clips,n);
  2049. /* 4-byte alignments */
  2050. switch (fh->ovfmt->depth) {
  2051. case 8:
  2052. case 24:
  2053. btcx_align(&win->w, clips, n, 3);
  2054. break;
  2055. case 16:
  2056. btcx_align(&win->w, clips, n, 1);
  2057. break;
  2058. case 32:
  2059. /* no alignment fixups needed */
  2060. break;
  2061. default:
  2062. BUG();
  2063. }
  2064. mutex_lock(&fh->cap.vb_lock);
  2065. kfree(fh->ov.clips);
  2066. fh->ov.clips = clips;
  2067. fh->ov.nclips = n;
  2068. fh->ov.w = win->w;
  2069. fh->ov.field = win->field;
  2070. fh->ov.setup_ok = 1;
  2071. btv->init.ov.w.width = win->w.width;
  2072. btv->init.ov.w.height = win->w.height;
  2073. btv->init.ov.field = win->field;
  2074. /* update overlay if needed */
  2075. retval = 0;
  2076. if (check_btres(fh, RESOURCE_OVERLAY)) {
  2077. struct bttv_buffer *new;
  2078. new = videobuf_sg_alloc(sizeof(*new));
  2079. new->crop = btv->crop[!!fh->do_crop].rect;
  2080. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2081. retval = bttv_switch_overlay(btv,fh,new);
  2082. }
  2083. mutex_unlock(&fh->cap.vb_lock);
  2084. return retval;
  2085. }
  2086. /* ----------------------------------------------------------------------- */
  2087. static struct videobuf_queue* bttv_queue(struct bttv_fh *fh)
  2088. {
  2089. struct videobuf_queue* q = NULL;
  2090. switch (fh->type) {
  2091. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2092. q = &fh->cap;
  2093. break;
  2094. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2095. q = &fh->vbi;
  2096. break;
  2097. default:
  2098. BUG();
  2099. }
  2100. return q;
  2101. }
  2102. static int bttv_resource(struct bttv_fh *fh)
  2103. {
  2104. int res = 0;
  2105. switch (fh->type) {
  2106. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2107. res = RESOURCE_VIDEO_STREAM;
  2108. break;
  2109. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2110. res = RESOURCE_VBI;
  2111. break;
  2112. default:
  2113. BUG();
  2114. }
  2115. return res;
  2116. }
  2117. static int bttv_switch_type(struct bttv_fh *fh, enum v4l2_buf_type type)
  2118. {
  2119. struct videobuf_queue *q = bttv_queue(fh);
  2120. int res = bttv_resource(fh);
  2121. if (check_btres(fh,res))
  2122. return -EBUSY;
  2123. if (videobuf_queue_is_busy(q))
  2124. return -EBUSY;
  2125. fh->type = type;
  2126. return 0;
  2127. }
  2128. static void
  2129. pix_format_set_size (struct v4l2_pix_format * f,
  2130. const struct bttv_format * fmt,
  2131. unsigned int width,
  2132. unsigned int height)
  2133. {
  2134. f->width = width;
  2135. f->height = height;
  2136. if (fmt->flags & FORMAT_FLAGS_PLANAR) {
  2137. f->bytesperline = width; /* Y plane */
  2138. f->sizeimage = (width * height * fmt->depth) >> 3;
  2139. } else {
  2140. f->bytesperline = (width * fmt->depth) >> 3;
  2141. f->sizeimage = height * f->bytesperline;
  2142. }
  2143. }
  2144. static int bttv_g_fmt_vid_cap(struct file *file, void *priv,
  2145. struct v4l2_format *f)
  2146. {
  2147. struct bttv_fh *fh = priv;
  2148. pix_format_set_size(&f->fmt.pix, fh->fmt,
  2149. fh->width, fh->height);
  2150. f->fmt.pix.field = fh->cap.field;
  2151. f->fmt.pix.pixelformat = fh->fmt->fourcc;
  2152. return 0;
  2153. }
  2154. static int bttv_g_fmt_vid_overlay(struct file *file, void *priv,
  2155. struct v4l2_format *f)
  2156. {
  2157. struct bttv_fh *fh = priv;
  2158. f->fmt.win.w = fh->ov.w;
  2159. f->fmt.win.field = fh->ov.field;
  2160. return 0;
  2161. }
  2162. static int bttv_try_fmt_vid_cap(struct file *file, void *priv,
  2163. struct v4l2_format *f)
  2164. {
  2165. const struct bttv_format *fmt;
  2166. struct bttv_fh *fh = priv;
  2167. struct bttv *btv = fh->btv;
  2168. enum v4l2_field field;
  2169. __s32 width, height;
  2170. int rc;
  2171. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  2172. if (NULL == fmt)
  2173. return -EINVAL;
  2174. field = f->fmt.pix.field;
  2175. if (V4L2_FIELD_ANY == field) {
  2176. __s32 height2;
  2177. height2 = btv->crop[!!fh->do_crop].rect.height >> 1;
  2178. field = (f->fmt.pix.height > height2)
  2179. ? V4L2_FIELD_INTERLACED
  2180. : V4L2_FIELD_BOTTOM;
  2181. }
  2182. if (V4L2_FIELD_SEQ_BT == field)
  2183. field = V4L2_FIELD_SEQ_TB;
  2184. switch (field) {
  2185. case V4L2_FIELD_TOP:
  2186. case V4L2_FIELD_BOTTOM:
  2187. case V4L2_FIELD_ALTERNATE:
  2188. case V4L2_FIELD_INTERLACED:
  2189. break;
  2190. case V4L2_FIELD_SEQ_TB:
  2191. if (fmt->flags & FORMAT_FLAGS_PLANAR)
  2192. return -EINVAL;
  2193. break;
  2194. default:
  2195. return -EINVAL;
  2196. }
  2197. width = f->fmt.pix.width;
  2198. height = f->fmt.pix.height;
  2199. rc = limit_scaled_size(fh, &width, &height, field,
  2200. /* width_mask: 4 pixels */ ~3,
  2201. /* width_bias: nearest */ 2,
  2202. /* adjust_size */ 1,
  2203. /* adjust_crop */ 0);
  2204. if (0 != rc)
  2205. return rc;
  2206. /* update data for the application */
  2207. f->fmt.pix.field = field;
  2208. pix_format_set_size(&f->fmt.pix, fmt, width, height);
  2209. return 0;
  2210. }
  2211. static int bttv_try_fmt_vid_overlay(struct file *file, void *priv,
  2212. struct v4l2_format *f)
  2213. {
  2214. struct bttv_fh *fh = priv;
  2215. return verify_window(fh, &f->fmt.win,
  2216. /* adjust_size */ 1,
  2217. /* adjust_crop */ 0);
  2218. }
  2219. static int bttv_s_fmt_vid_cap(struct file *file, void *priv,
  2220. struct v4l2_format *f)
  2221. {
  2222. int retval;
  2223. const struct bttv_format *fmt;
  2224. struct bttv_fh *fh = priv;
  2225. struct bttv *btv = fh->btv;
  2226. __s32 width, height;
  2227. enum v4l2_field field;
  2228. retval = bttv_switch_type(fh, f->type);
  2229. if (0 != retval)
  2230. return retval;
  2231. retval = bttv_try_fmt_vid_cap(file, priv, f);
  2232. if (0 != retval)
  2233. return retval;
  2234. width = f->fmt.pix.width;
  2235. height = f->fmt.pix.height;
  2236. field = f->fmt.pix.field;
  2237. retval = limit_scaled_size(fh, &width, &height, f->fmt.pix.field,
  2238. /* width_mask: 4 pixels */ ~3,
  2239. /* width_bias: nearest */ 2,
  2240. /* adjust_size */ 1,
  2241. /* adjust_crop */ 1);
  2242. if (0 != retval)
  2243. return retval;
  2244. f->fmt.pix.field = field;
  2245. fmt = format_by_fourcc(f->fmt.pix.pixelformat);
  2246. /* update our state informations */
  2247. mutex_lock(&fh->cap.vb_lock);
  2248. fh->fmt = fmt;
  2249. fh->cap.field = f->fmt.pix.field;
  2250. fh->cap.last = V4L2_FIELD_NONE;
  2251. fh->width = f->fmt.pix.width;
  2252. fh->height = f->fmt.pix.height;
  2253. btv->init.fmt = fmt;
  2254. btv->init.width = f->fmt.pix.width;
  2255. btv->init.height = f->fmt.pix.height;
  2256. mutex_unlock(&fh->cap.vb_lock);
  2257. return 0;
  2258. }
  2259. static int bttv_s_fmt_vid_overlay(struct file *file, void *priv,
  2260. struct v4l2_format *f)
  2261. {
  2262. struct bttv_fh *fh = priv;
  2263. struct bttv *btv = fh->btv;
  2264. if (no_overlay > 0) {
  2265. printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  2266. return -EINVAL;
  2267. }
  2268. return setup_window(fh, btv, &f->fmt.win, 1);
  2269. }
  2270. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2271. static int vidiocgmbuf(struct file *file, void *priv, struct video_mbuf *mbuf)
  2272. {
  2273. int retval;
  2274. unsigned int i;
  2275. struct bttv_fh *fh = priv;
  2276. mutex_lock(&fh->cap.vb_lock);
  2277. retval = __videobuf_mmap_setup(&fh->cap, gbuffers, gbufsize,
  2278. V4L2_MEMORY_MMAP);
  2279. if (retval < 0) {
  2280. mutex_unlock(&fh->cap.vb_lock);
  2281. return retval;
  2282. }
  2283. gbuffers = retval;
  2284. memset(mbuf, 0, sizeof(*mbuf));
  2285. mbuf->frames = gbuffers;
  2286. mbuf->size = gbuffers * gbufsize;
  2287. for (i = 0; i < gbuffers; i++)
  2288. mbuf->offsets[i] = i * gbufsize;
  2289. mutex_unlock(&fh->cap.vb_lock);
  2290. return 0;
  2291. }
  2292. #endif
  2293. static int bttv_querycap(struct file *file, void *priv,
  2294. struct v4l2_capability *cap)
  2295. {
  2296. struct bttv_fh *fh = priv;
  2297. struct bttv *btv = fh->btv;
  2298. if (0 == v4l2)
  2299. return -EINVAL;
  2300. strlcpy(cap->driver, "bttv", sizeof(cap->driver));
  2301. strlcpy(cap->card, btv->video_dev->name, sizeof(cap->card));
  2302. snprintf(cap->bus_info, sizeof(cap->bus_info),
  2303. "PCI:%s", pci_name(btv->c.pci));
  2304. cap->version = BTTV_VERSION_CODE;
  2305. cap->capabilities =
  2306. V4L2_CAP_VIDEO_CAPTURE |
  2307. V4L2_CAP_VBI_CAPTURE |
  2308. V4L2_CAP_READWRITE |
  2309. V4L2_CAP_STREAMING;
  2310. if (btv->has_saa6588)
  2311. cap->capabilities |= V4L2_CAP_RDS_CAPTURE;
  2312. if (no_overlay <= 0)
  2313. cap->capabilities |= V4L2_CAP_VIDEO_OVERLAY;
  2314. if (btv->tuner_type != TUNER_ABSENT)
  2315. cap->capabilities |= V4L2_CAP_TUNER;
  2316. return 0;
  2317. }
  2318. static int bttv_enum_fmt_cap_ovr(struct v4l2_fmtdesc *f)
  2319. {
  2320. int index = -1, i;
  2321. for (i = 0; i < FORMATS; i++) {
  2322. if (formats[i].fourcc != -1)
  2323. index++;
  2324. if ((unsigned int)index == f->index)
  2325. break;
  2326. }
  2327. if (FORMATS == i)
  2328. return -EINVAL;
  2329. f->pixelformat = formats[i].fourcc;
  2330. strlcpy(f->description, formats[i].name, sizeof(f->description));
  2331. return i;
  2332. }
  2333. static int bttv_enum_fmt_vid_cap(struct file *file, void *priv,
  2334. struct v4l2_fmtdesc *f)
  2335. {
  2336. int rc = bttv_enum_fmt_cap_ovr(f);
  2337. if (rc < 0)
  2338. return rc;
  2339. return 0;
  2340. }
  2341. static int bttv_enum_fmt_vid_overlay(struct file *file, void *priv,
  2342. struct v4l2_fmtdesc *f)
  2343. {
  2344. int rc;
  2345. if (no_overlay > 0) {
  2346. printk(KERN_ERR "V4L2_BUF_TYPE_VIDEO_OVERLAY: no_overlay\n");
  2347. return -EINVAL;
  2348. }
  2349. rc = bttv_enum_fmt_cap_ovr(f);
  2350. if (rc < 0)
  2351. return rc;
  2352. if (!(formats[rc].flags & FORMAT_FLAGS_PACKED))
  2353. return -EINVAL;
  2354. return 0;
  2355. }
  2356. static int bttv_g_fbuf(struct file *file, void *f,
  2357. struct v4l2_framebuffer *fb)
  2358. {
  2359. struct bttv_fh *fh = f;
  2360. struct bttv *btv = fh->btv;
  2361. *fb = btv->fbuf;
  2362. fb->capability = V4L2_FBUF_CAP_LIST_CLIPPING;
  2363. if (fh->ovfmt)
  2364. fb->fmt.pixelformat = fh->ovfmt->fourcc;
  2365. return 0;
  2366. }
  2367. static int bttv_overlay(struct file *file, void *f, unsigned int on)
  2368. {
  2369. struct bttv_fh *fh = f;
  2370. struct bttv *btv = fh->btv;
  2371. struct bttv_buffer *new;
  2372. int retval;
  2373. if (on) {
  2374. /* verify args */
  2375. if (NULL == btv->fbuf.base)
  2376. return -EINVAL;
  2377. if (!fh->ov.setup_ok) {
  2378. dprintk("bttv%d: overlay: !setup_ok\n", btv->c.nr);
  2379. return -EINVAL;
  2380. }
  2381. }
  2382. if (!check_alloc_btres(btv, fh, RESOURCE_OVERLAY))
  2383. return -EBUSY;
  2384. mutex_lock(&fh->cap.vb_lock);
  2385. if (on) {
  2386. fh->ov.tvnorm = btv->tvnorm;
  2387. new = videobuf_sg_alloc(sizeof(*new));
  2388. new->crop = btv->crop[!!fh->do_crop].rect;
  2389. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2390. } else {
  2391. new = NULL;
  2392. }
  2393. /* switch over */
  2394. retval = bttv_switch_overlay(btv, fh, new);
  2395. mutex_unlock(&fh->cap.vb_lock);
  2396. return retval;
  2397. }
  2398. static int bttv_s_fbuf(struct file *file, void *f,
  2399. struct v4l2_framebuffer *fb)
  2400. {
  2401. struct bttv_fh *fh = f;
  2402. struct bttv *btv = fh->btv;
  2403. const struct bttv_format *fmt;
  2404. int retval;
  2405. if (!capable(CAP_SYS_ADMIN) &&
  2406. !capable(CAP_SYS_RAWIO))
  2407. return -EPERM;
  2408. /* check args */
  2409. fmt = format_by_fourcc(fb->fmt.pixelformat);
  2410. if (NULL == fmt)
  2411. return -EINVAL;
  2412. if (0 == (fmt->flags & FORMAT_FLAGS_PACKED))
  2413. return -EINVAL;
  2414. retval = -EINVAL;
  2415. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2416. __s32 width = fb->fmt.width;
  2417. __s32 height = fb->fmt.height;
  2418. retval = limit_scaled_size(fh, &width, &height,
  2419. V4L2_FIELD_INTERLACED,
  2420. /* width_mask */ ~3,
  2421. /* width_bias */ 2,
  2422. /* adjust_size */ 0,
  2423. /* adjust_crop */ 0);
  2424. if (0 != retval)
  2425. return retval;
  2426. }
  2427. /* ok, accept it */
  2428. mutex_lock(&fh->cap.vb_lock);
  2429. btv->fbuf.base = fb->base;
  2430. btv->fbuf.fmt.width = fb->fmt.width;
  2431. btv->fbuf.fmt.height = fb->fmt.height;
  2432. if (0 != fb->fmt.bytesperline)
  2433. btv->fbuf.fmt.bytesperline = fb->fmt.bytesperline;
  2434. else
  2435. btv->fbuf.fmt.bytesperline = btv->fbuf.fmt.width*fmt->depth/8;
  2436. retval = 0;
  2437. fh->ovfmt = fmt;
  2438. btv->init.ovfmt = fmt;
  2439. if (fb->flags & V4L2_FBUF_FLAG_OVERLAY) {
  2440. fh->ov.w.left = 0;
  2441. fh->ov.w.top = 0;
  2442. fh->ov.w.width = fb->fmt.width;
  2443. fh->ov.w.height = fb->fmt.height;
  2444. btv->init.ov.w.width = fb->fmt.width;
  2445. btv->init.ov.w.height = fb->fmt.height;
  2446. kfree(fh->ov.clips);
  2447. fh->ov.clips = NULL;
  2448. fh->ov.nclips = 0;
  2449. if (check_btres(fh, RESOURCE_OVERLAY)) {
  2450. struct bttv_buffer *new;
  2451. new = videobuf_sg_alloc(sizeof(*new));
  2452. new->crop = btv->crop[!!fh->do_crop].rect;
  2453. bttv_overlay_risc(btv, &fh->ov, fh->ovfmt, new);
  2454. retval = bttv_switch_overlay(btv, fh, new);
  2455. }
  2456. }
  2457. mutex_unlock(&fh->cap.vb_lock);
  2458. return retval;
  2459. }
  2460. static int bttv_reqbufs(struct file *file, void *priv,
  2461. struct v4l2_requestbuffers *p)
  2462. {
  2463. struct bttv_fh *fh = priv;
  2464. return videobuf_reqbufs(bttv_queue(fh), p);
  2465. }
  2466. static int bttv_querybuf(struct file *file, void *priv,
  2467. struct v4l2_buffer *b)
  2468. {
  2469. struct bttv_fh *fh = priv;
  2470. return videobuf_querybuf(bttv_queue(fh), b);
  2471. }
  2472. static int bttv_qbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  2473. {
  2474. struct bttv_fh *fh = priv;
  2475. struct bttv *btv = fh->btv;
  2476. int res = bttv_resource(fh);
  2477. if (!check_alloc_btres(btv, fh, res))
  2478. return -EBUSY;
  2479. return videobuf_qbuf(bttv_queue(fh), b);
  2480. }
  2481. static int bttv_dqbuf(struct file *file, void *priv, struct v4l2_buffer *b)
  2482. {
  2483. struct bttv_fh *fh = priv;
  2484. return videobuf_dqbuf(bttv_queue(fh), b,
  2485. file->f_flags & O_NONBLOCK);
  2486. }
  2487. static int bttv_streamon(struct file *file, void *priv,
  2488. enum v4l2_buf_type type)
  2489. {
  2490. struct bttv_fh *fh = priv;
  2491. struct bttv *btv = fh->btv;
  2492. int res = bttv_resource(fh);
  2493. if (!check_alloc_btres(btv, fh, res))
  2494. return -EBUSY;
  2495. return videobuf_streamon(bttv_queue(fh));
  2496. }
  2497. static int bttv_streamoff(struct file *file, void *priv,
  2498. enum v4l2_buf_type type)
  2499. {
  2500. struct bttv_fh *fh = priv;
  2501. struct bttv *btv = fh->btv;
  2502. int retval;
  2503. int res = bttv_resource(fh);
  2504. retval = videobuf_streamoff(bttv_queue(fh));
  2505. if (retval < 0)
  2506. return retval;
  2507. free_btres(btv, fh, res);
  2508. return 0;
  2509. }
  2510. static int bttv_queryctrl(struct file *file, void *priv,
  2511. struct v4l2_queryctrl *c)
  2512. {
  2513. struct bttv_fh *fh = priv;
  2514. struct bttv *btv = fh->btv;
  2515. const struct v4l2_queryctrl *ctrl;
  2516. if ((c->id < V4L2_CID_BASE ||
  2517. c->id >= V4L2_CID_LASTP1) &&
  2518. (c->id < V4L2_CID_PRIVATE_BASE ||
  2519. c->id >= V4L2_CID_PRIVATE_LASTP1))
  2520. return -EINVAL;
  2521. if (!btv->volume_gpio && (c->id == V4L2_CID_AUDIO_VOLUME))
  2522. *c = no_ctl;
  2523. else {
  2524. ctrl = ctrl_by_id(c->id);
  2525. *c = (NULL != ctrl) ? *ctrl : no_ctl;
  2526. }
  2527. return 0;
  2528. }
  2529. static int bttv_g_parm(struct file *file, void *f,
  2530. struct v4l2_streamparm *parm)
  2531. {
  2532. struct bttv_fh *fh = f;
  2533. struct bttv *btv = fh->btv;
  2534. v4l2_video_std_frame_period(bttv_tvnorms[btv->tvnorm].v4l2_id,
  2535. &parm->parm.capture.timeperframe);
  2536. return 0;
  2537. }
  2538. static int bttv_g_tuner(struct file *file, void *priv,
  2539. struct v4l2_tuner *t)
  2540. {
  2541. struct bttv_fh *fh = priv;
  2542. struct bttv *btv = fh->btv;
  2543. if (btv->tuner_type == TUNER_ABSENT)
  2544. return -EINVAL;
  2545. if (0 != t->index)
  2546. return -EINVAL;
  2547. mutex_lock(&btv->lock);
  2548. t->rxsubchans = V4L2_TUNER_SUB_MONO;
  2549. bttv_call_all(btv, tuner, g_tuner, t);
  2550. strcpy(t->name, "Television");
  2551. t->capability = V4L2_TUNER_CAP_NORM;
  2552. t->type = V4L2_TUNER_ANALOG_TV;
  2553. if (btread(BT848_DSTATUS)&BT848_DSTATUS_HLOC)
  2554. t->signal = 0xffff;
  2555. if (btv->audio_mode_gpio)
  2556. btv->audio_mode_gpio(btv, t, 0);
  2557. mutex_unlock(&btv->lock);
  2558. return 0;
  2559. }
  2560. static int bttv_g_priority(struct file *file, void *f, enum v4l2_priority *p)
  2561. {
  2562. struct bttv_fh *fh = f;
  2563. struct bttv *btv = fh->btv;
  2564. *p = v4l2_prio_max(&btv->prio);
  2565. return 0;
  2566. }
  2567. static int bttv_s_priority(struct file *file, void *f,
  2568. enum v4l2_priority prio)
  2569. {
  2570. struct bttv_fh *fh = f;
  2571. struct bttv *btv = fh->btv;
  2572. return v4l2_prio_change(&btv->prio, &fh->prio, prio);
  2573. }
  2574. static int bttv_cropcap(struct file *file, void *priv,
  2575. struct v4l2_cropcap *cap)
  2576. {
  2577. struct bttv_fh *fh = priv;
  2578. struct bttv *btv = fh->btv;
  2579. if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2580. cap->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2581. return -EINVAL;
  2582. *cap = bttv_tvnorms[btv->tvnorm].cropcap;
  2583. return 0;
  2584. }
  2585. static int bttv_g_crop(struct file *file, void *f, struct v4l2_crop *crop)
  2586. {
  2587. struct bttv_fh *fh = f;
  2588. struct bttv *btv = fh->btv;
  2589. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2590. crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2591. return -EINVAL;
  2592. /* No fh->do_crop = 1; because btv->crop[1] may be
  2593. inconsistent with fh->width or fh->height and apps
  2594. do not expect a change here. */
  2595. crop->c = btv->crop[!!fh->do_crop].rect;
  2596. return 0;
  2597. }
  2598. static int bttv_s_crop(struct file *file, void *f, struct v4l2_crop *crop)
  2599. {
  2600. struct bttv_fh *fh = f;
  2601. struct bttv *btv = fh->btv;
  2602. const struct v4l2_rect *b;
  2603. int retval;
  2604. struct bttv_crop c;
  2605. __s32 b_left;
  2606. __s32 b_top;
  2607. __s32 b_right;
  2608. __s32 b_bottom;
  2609. if (crop->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  2610. crop->type != V4L2_BUF_TYPE_VIDEO_OVERLAY)
  2611. return -EINVAL;
  2612. retval = v4l2_prio_check(&btv->prio, fh->prio);
  2613. if (0 != retval)
  2614. return retval;
  2615. /* Make sure tvnorm, vbi_end and the current cropping
  2616. parameters remain consistent until we're done. Note
  2617. read() may change vbi_end in check_alloc_btres(). */
  2618. mutex_lock(&btv->lock);
  2619. retval = -EBUSY;
  2620. if (locked_btres(fh->btv, VIDEO_RESOURCES)) {
  2621. mutex_unlock(&btv->lock);
  2622. return retval;
  2623. }
  2624. b = &bttv_tvnorms[btv->tvnorm].cropcap.bounds;
  2625. b_left = b->left;
  2626. b_right = b_left + b->width;
  2627. b_bottom = b->top + b->height;
  2628. b_top = max(b->top, btv->vbi_end);
  2629. if (b_top + 32 >= b_bottom) {
  2630. mutex_unlock(&btv->lock);
  2631. return retval;
  2632. }
  2633. /* Min. scaled size 48 x 32. */
  2634. c.rect.left = clamp(crop->c.left, b_left, b_right - 48);
  2635. c.rect.left = min(c.rect.left, (__s32) MAX_HDELAY);
  2636. c.rect.width = clamp(crop->c.width,
  2637. 48, b_right - c.rect.left);
  2638. c.rect.top = clamp(crop->c.top, b_top, b_bottom - 32);
  2639. /* Top and height must be a multiple of two. */
  2640. c.rect.top = (c.rect.top + 1) & ~1;
  2641. c.rect.height = clamp(crop->c.height,
  2642. 32, b_bottom - c.rect.top);
  2643. c.rect.height = (c.rect.height + 1) & ~1;
  2644. bttv_crop_calc_limits(&c);
  2645. btv->crop[1] = c;
  2646. mutex_unlock(&btv->lock);
  2647. fh->do_crop = 1;
  2648. mutex_lock(&fh->cap.vb_lock);
  2649. if (fh->width < c.min_scaled_width) {
  2650. fh->width = c.min_scaled_width;
  2651. btv->init.width = c.min_scaled_width;
  2652. } else if (fh->width > c.max_scaled_width) {
  2653. fh->width = c.max_scaled_width;
  2654. btv->init.width = c.max_scaled_width;
  2655. }
  2656. if (fh->height < c.min_scaled_height) {
  2657. fh->height = c.min_scaled_height;
  2658. btv->init.height = c.min_scaled_height;
  2659. } else if (fh->height > c.max_scaled_height) {
  2660. fh->height = c.max_scaled_height;
  2661. btv->init.height = c.max_scaled_height;
  2662. }
  2663. mutex_unlock(&fh->cap.vb_lock);
  2664. return 0;
  2665. }
  2666. static int bttv_g_audio(struct file *file, void *priv, struct v4l2_audio *a)
  2667. {
  2668. if (unlikely(a->index))
  2669. return -EINVAL;
  2670. strcpy(a->name, "audio");
  2671. return 0;
  2672. }
  2673. static int bttv_s_audio(struct file *file, void *priv, struct v4l2_audio *a)
  2674. {
  2675. if (unlikely(a->index))
  2676. return -EINVAL;
  2677. return 0;
  2678. }
  2679. static ssize_t bttv_read(struct file *file, char __user *data,
  2680. size_t count, loff_t *ppos)
  2681. {
  2682. struct bttv_fh *fh = file->private_data;
  2683. int retval = 0;
  2684. if (fh->btv->errors)
  2685. bttv_reinit_bt848(fh->btv);
  2686. dprintk("bttv%d: read count=%d type=%s\n",
  2687. fh->btv->c.nr,(int)count,v4l2_type_names[fh->type]);
  2688. switch (fh->type) {
  2689. case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  2690. if (!check_alloc_btres(fh->btv, fh, RESOURCE_VIDEO_READ)) {
  2691. /* VIDEO_READ in use by another fh,
  2692. or VIDEO_STREAM by any fh. */
  2693. return -EBUSY;
  2694. }
  2695. retval = videobuf_read_one(&fh->cap, data, count, ppos,
  2696. file->f_flags & O_NONBLOCK);
  2697. free_btres(fh->btv, fh, RESOURCE_VIDEO_READ);
  2698. break;
  2699. case V4L2_BUF_TYPE_VBI_CAPTURE:
  2700. if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
  2701. return -EBUSY;
  2702. retval = videobuf_read_stream(&fh->vbi, data, count, ppos, 1,
  2703. file->f_flags & O_NONBLOCK);
  2704. break;
  2705. default:
  2706. BUG();
  2707. }
  2708. return retval;
  2709. }
  2710. static unsigned int bttv_poll(struct file *file, poll_table *wait)
  2711. {
  2712. struct bttv_fh *fh = file->private_data;
  2713. struct bttv_buffer *buf;
  2714. enum v4l2_field field;
  2715. unsigned int rc = POLLERR;
  2716. if (V4L2_BUF_TYPE_VBI_CAPTURE == fh->type) {
  2717. if (!check_alloc_btres(fh->btv,fh,RESOURCE_VBI))
  2718. return POLLERR;
  2719. return videobuf_poll_stream(file, &fh->vbi, wait);
  2720. }
  2721. if (check_btres(fh,RESOURCE_VIDEO_STREAM)) {
  2722. mutex_lock(&fh->cap.vb_lock);
  2723. /* streaming capture */
  2724. if (list_empty(&fh->cap.stream))
  2725. goto err;
  2726. buf = list_entry(fh->cap.stream.next,struct bttv_buffer,vb.stream);
  2727. } else {
  2728. /* read() capture */
  2729. mutex_lock(&fh->cap.vb_lock);
  2730. if (NULL == fh->cap.read_buf) {
  2731. /* need to capture a new frame */
  2732. if (locked_btres(fh->btv,RESOURCE_VIDEO_STREAM))
  2733. goto err;
  2734. fh->cap.read_buf = videobuf_sg_alloc(fh->cap.msize);
  2735. if (NULL == fh->cap.read_buf)
  2736. goto err;
  2737. fh->cap.read_buf->memory = V4L2_MEMORY_USERPTR;
  2738. field = videobuf_next_field(&fh->cap);
  2739. if (0 != fh->cap.ops->buf_prepare(&fh->cap,fh->cap.read_buf,field)) {
  2740. kfree (fh->cap.read_buf);
  2741. fh->cap.read_buf = NULL;
  2742. goto err;
  2743. }
  2744. fh->cap.ops->buf_queue(&fh->cap,fh->cap.read_buf);
  2745. fh->cap.read_off = 0;
  2746. }
  2747. mutex_unlock(&fh->cap.vb_lock);
  2748. buf = (struct bttv_buffer*)fh->cap.read_buf;
  2749. }
  2750. poll_wait(file, &buf->vb.done, wait);
  2751. if (buf->vb.state == VIDEOBUF_DONE ||
  2752. buf->vb.state == VIDEOBUF_ERROR)
  2753. rc = POLLIN|POLLRDNORM;
  2754. else
  2755. rc = 0;
  2756. err:
  2757. mutex_unlock(&fh->cap.vb_lock);
  2758. return rc;
  2759. }
  2760. static int bttv_open(struct file *file)
  2761. {
  2762. struct video_device *vdev = video_devdata(file);
  2763. struct bttv *btv = video_drvdata(file);
  2764. struct bttv_fh *fh;
  2765. enum v4l2_buf_type type = 0;
  2766. dprintk(KERN_DEBUG "bttv: open dev=%s\n", video_device_node_name(vdev));
  2767. if (vdev->vfl_type == VFL_TYPE_GRABBER) {
  2768. type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2769. } else if (vdev->vfl_type == VFL_TYPE_VBI) {
  2770. type = V4L2_BUF_TYPE_VBI_CAPTURE;
  2771. } else {
  2772. WARN_ON(1);
  2773. return -ENODEV;
  2774. }
  2775. lock_kernel();
  2776. dprintk(KERN_DEBUG "bttv%d: open called (type=%s)\n",
  2777. btv->c.nr,v4l2_type_names[type]);
  2778. /* allocate per filehandle data */
  2779. fh = kmalloc(sizeof(*fh),GFP_KERNEL);
  2780. if (NULL == fh) {
  2781. unlock_kernel();
  2782. return -ENOMEM;
  2783. }
  2784. file->private_data = fh;
  2785. *fh = btv->init;
  2786. fh->type = type;
  2787. fh->ov.setup_ok = 0;
  2788. v4l2_prio_open(&btv->prio, &fh->prio);
  2789. videobuf_queue_sg_init(&fh->cap, &bttv_video_qops,
  2790. &btv->c.pci->dev, &btv->s_lock,
  2791. V4L2_BUF_TYPE_VIDEO_CAPTURE,
  2792. V4L2_FIELD_INTERLACED,
  2793. sizeof(struct bttv_buffer),
  2794. fh);
  2795. videobuf_queue_sg_init(&fh->vbi, &bttv_vbi_qops,
  2796. &btv->c.pci->dev, &btv->s_lock,
  2797. V4L2_BUF_TYPE_VBI_CAPTURE,
  2798. V4L2_FIELD_SEQ_TB,
  2799. sizeof(struct bttv_buffer),
  2800. fh);
  2801. set_tvnorm(btv,btv->tvnorm);
  2802. set_input(btv, btv->input, btv->tvnorm);
  2803. btv->users++;
  2804. /* The V4L2 spec requires one global set of cropping parameters
  2805. which only change on request. These are stored in btv->crop[1].
  2806. However for compatibility with V4L apps and cropping unaware
  2807. V4L2 apps we now reset the cropping parameters as seen through
  2808. this fh, which is to say VIDIOC_G_CROP and scaling limit checks
  2809. will use btv->crop[0], the default cropping parameters for the
  2810. current video standard, and VIDIOC_S_FMT will not implicitely
  2811. change the cropping parameters until VIDIOC_S_CROP has been
  2812. called. */
  2813. fh->do_crop = !reset_crop; /* module parameter */
  2814. /* Likewise there should be one global set of VBI capture
  2815. parameters, but for compatibility with V4L apps and earlier
  2816. driver versions each fh has its own parameters. */
  2817. bttv_vbi_fmt_reset(&fh->vbi_fmt, btv->tvnorm);
  2818. bttv_field_count(btv);
  2819. unlock_kernel();
  2820. return 0;
  2821. }
  2822. static int bttv_release(struct file *file)
  2823. {
  2824. struct bttv_fh *fh = file->private_data;
  2825. struct bttv *btv = fh->btv;
  2826. /* turn off overlay */
  2827. if (check_btres(fh, RESOURCE_OVERLAY))
  2828. bttv_switch_overlay(btv,fh,NULL);
  2829. /* stop video capture */
  2830. if (check_btres(fh, RESOURCE_VIDEO_STREAM)) {
  2831. videobuf_streamoff(&fh->cap);
  2832. free_btres(btv,fh,RESOURCE_VIDEO_STREAM);
  2833. }
  2834. if (fh->cap.read_buf) {
  2835. buffer_release(&fh->cap,fh->cap.read_buf);
  2836. kfree(fh->cap.read_buf);
  2837. }
  2838. if (check_btres(fh, RESOURCE_VIDEO_READ)) {
  2839. free_btres(btv, fh, RESOURCE_VIDEO_READ);
  2840. }
  2841. /* stop vbi capture */
  2842. if (check_btres(fh, RESOURCE_VBI)) {
  2843. videobuf_stop(&fh->vbi);
  2844. free_btres(btv,fh,RESOURCE_VBI);
  2845. }
  2846. /* free stuff */
  2847. videobuf_mmap_free(&fh->cap);
  2848. videobuf_mmap_free(&fh->vbi);
  2849. v4l2_prio_close(&btv->prio, fh->prio);
  2850. file->private_data = NULL;
  2851. kfree(fh);
  2852. btv->users--;
  2853. bttv_field_count(btv);
  2854. if (!btv->users)
  2855. audio_mute(btv, 1);
  2856. return 0;
  2857. }
  2858. static int
  2859. bttv_mmap(struct file *file, struct vm_area_struct *vma)
  2860. {
  2861. struct bttv_fh *fh = file->private_data;
  2862. dprintk("bttv%d: mmap type=%s 0x%lx+%ld\n",
  2863. fh->btv->c.nr, v4l2_type_names[fh->type],
  2864. vma->vm_start, vma->vm_end - vma->vm_start);
  2865. return videobuf_mmap_mapper(bttv_queue(fh),vma);
  2866. }
  2867. static const struct v4l2_file_operations bttv_fops =
  2868. {
  2869. .owner = THIS_MODULE,
  2870. .open = bttv_open,
  2871. .release = bttv_release,
  2872. .ioctl = video_ioctl2,
  2873. .read = bttv_read,
  2874. .mmap = bttv_mmap,
  2875. .poll = bttv_poll,
  2876. };
  2877. static const struct v4l2_ioctl_ops bttv_ioctl_ops = {
  2878. .vidioc_querycap = bttv_querycap,
  2879. .vidioc_enum_fmt_vid_cap = bttv_enum_fmt_vid_cap,
  2880. .vidioc_g_fmt_vid_cap = bttv_g_fmt_vid_cap,
  2881. .vidioc_try_fmt_vid_cap = bttv_try_fmt_vid_cap,
  2882. .vidioc_s_fmt_vid_cap = bttv_s_fmt_vid_cap,
  2883. .vidioc_enum_fmt_vid_overlay = bttv_enum_fmt_vid_overlay,
  2884. .vidioc_g_fmt_vid_overlay = bttv_g_fmt_vid_overlay,
  2885. .vidioc_try_fmt_vid_overlay = bttv_try_fmt_vid_overlay,
  2886. .vidioc_s_fmt_vid_overlay = bttv_s_fmt_vid_overlay,
  2887. .vidioc_g_fmt_vbi_cap = bttv_g_fmt_vbi_cap,
  2888. .vidioc_try_fmt_vbi_cap = bttv_try_fmt_vbi_cap,
  2889. .vidioc_s_fmt_vbi_cap = bttv_s_fmt_vbi_cap,
  2890. .vidioc_g_audio = bttv_g_audio,
  2891. .vidioc_s_audio = bttv_s_audio,
  2892. .vidioc_cropcap = bttv_cropcap,
  2893. .vidioc_reqbufs = bttv_reqbufs,
  2894. .vidioc_querybuf = bttv_querybuf,
  2895. .vidioc_qbuf = bttv_qbuf,
  2896. .vidioc_dqbuf = bttv_dqbuf,
  2897. .vidioc_s_std = bttv_s_std,
  2898. .vidioc_enum_input = bttv_enum_input,
  2899. .vidioc_g_input = bttv_g_input,
  2900. .vidioc_s_input = bttv_s_input,
  2901. .vidioc_queryctrl = bttv_queryctrl,
  2902. .vidioc_g_ctrl = bttv_g_ctrl,
  2903. .vidioc_s_ctrl = bttv_s_ctrl,
  2904. .vidioc_streamon = bttv_streamon,
  2905. .vidioc_streamoff = bttv_streamoff,
  2906. .vidioc_g_tuner = bttv_g_tuner,
  2907. .vidioc_s_tuner = bttv_s_tuner,
  2908. #ifdef CONFIG_VIDEO_V4L1_COMPAT
  2909. .vidiocgmbuf = vidiocgmbuf,
  2910. #endif
  2911. .vidioc_g_crop = bttv_g_crop,
  2912. .vidioc_s_crop = bttv_s_crop,
  2913. .vidioc_g_fbuf = bttv_g_fbuf,
  2914. .vidioc_s_fbuf = bttv_s_fbuf,
  2915. .vidioc_overlay = bttv_overlay,
  2916. .vidioc_g_priority = bttv_g_priority,
  2917. .vidioc_s_priority = bttv_s_priority,
  2918. .vidioc_g_parm = bttv_g_parm,
  2919. .vidioc_g_frequency = bttv_g_frequency,
  2920. .vidioc_s_frequency = bttv_s_frequency,
  2921. .vidioc_log_status = bttv_log_status,
  2922. .vidioc_querystd = bttv_querystd,
  2923. #ifdef CONFIG_VIDEO_ADV_DEBUG
  2924. .vidioc_g_register = bttv_g_register,
  2925. .vidioc_s_register = bttv_s_register,
  2926. #endif
  2927. };
  2928. static struct video_device bttv_video_template = {
  2929. .fops = &bttv_fops,
  2930. .ioctl_ops = &bttv_ioctl_ops,
  2931. .tvnorms = BTTV_NORMS,
  2932. .current_norm = V4L2_STD_PAL,
  2933. };
  2934. /* ----------------------------------------------------------------------- */
  2935. /* radio interface */
  2936. static int radio_open(struct file *file)
  2937. {
  2938. struct video_device *vdev = video_devdata(file);
  2939. struct bttv *btv = video_drvdata(file);
  2940. struct bttv_fh *fh;
  2941. dprintk("bttv: open dev=%s\n", video_device_node_name(vdev));
  2942. lock_kernel();
  2943. dprintk("bttv%d: open called (radio)\n",btv->c.nr);
  2944. /* allocate per filehandle data */
  2945. fh = kmalloc(sizeof(*fh), GFP_KERNEL);
  2946. if (NULL == fh) {
  2947. unlock_kernel();
  2948. return -ENOMEM;
  2949. }
  2950. file->private_data = fh;
  2951. *fh = btv->init;
  2952. v4l2_prio_open(&btv->prio, &fh->prio);
  2953. mutex_lock(&btv->lock);
  2954. btv->radio_user++;
  2955. bttv_call_all(btv, tuner, s_radio);
  2956. audio_input(btv,TVAUDIO_INPUT_RADIO);
  2957. mutex_unlock(&btv->lock);
  2958. unlock_kernel();
  2959. return 0;
  2960. }
  2961. static int radio_release(struct file *file)
  2962. {
  2963. struct bttv_fh *fh = file->private_data;
  2964. struct bttv *btv = fh->btv;
  2965. struct rds_command cmd;
  2966. v4l2_prio_close(&btv->prio, fh->prio);
  2967. file->private_data = NULL;
  2968. kfree(fh);
  2969. btv->radio_user--;
  2970. bttv_call_all(btv, core, ioctl, RDS_CMD_CLOSE, &cmd);
  2971. return 0;
  2972. }
  2973. static int radio_querycap(struct file *file, void *priv,
  2974. struct v4l2_capability *cap)
  2975. {
  2976. struct bttv_fh *fh = priv;
  2977. struct bttv *btv = fh->btv;
  2978. strcpy(cap->driver, "bttv");
  2979. strlcpy(cap->card, btv->radio_dev->name, sizeof(cap->card));
  2980. sprintf(cap->bus_info, "PCI:%s", pci_name(btv->c.pci));
  2981. cap->version = BTTV_VERSION_CODE;
  2982. cap->capabilities = V4L2_CAP_TUNER;
  2983. return 0;
  2984. }
  2985. static int radio_g_tuner(struct file *file, void *priv, struct v4l2_tuner *t)
  2986. {
  2987. struct bttv_fh *fh = priv;
  2988. struct bttv *btv = fh->btv;
  2989. if (btv->tuner_type == TUNER_ABSENT)
  2990. return -EINVAL;
  2991. if (0 != t->index)
  2992. return -EINVAL;
  2993. mutex_lock(&btv->lock);
  2994. strcpy(t->name, "Radio");
  2995. t->type = V4L2_TUNER_RADIO;
  2996. bttv_call_all(btv, tuner, g_tuner, t);
  2997. if (btv->audio_mode_gpio)
  2998. btv->audio_mode_gpio(btv, t, 0);
  2999. mutex_unlock(&btv->lock);
  3000. return 0;
  3001. }
  3002. static int radio_enum_input(struct file *file, void *priv,
  3003. struct v4l2_input *i)
  3004. {
  3005. if (i->index != 0)
  3006. return -EINVAL;
  3007. strcpy(i->name, "Radio");
  3008. i->type = V4L2_INPUT_TYPE_TUNER;
  3009. return 0;
  3010. }
  3011. static int radio_g_audio(struct file *file, void *priv,
  3012. struct v4l2_audio *a)
  3013. {
  3014. if (unlikely(a->index))
  3015. return -EINVAL;
  3016. strcpy(a->name, "Radio");
  3017. return 0;
  3018. }
  3019. static int radio_s_tuner(struct file *file, void *priv,
  3020. struct v4l2_tuner *t)
  3021. {
  3022. struct bttv_fh *fh = priv;
  3023. struct bttv *btv = fh->btv;
  3024. if (0 != t->index)
  3025. return -EINVAL;
  3026. bttv_call_all(btv, tuner, g_tuner, t);
  3027. return 0;
  3028. }
  3029. static int radio_s_audio(struct file *file, void *priv,
  3030. struct v4l2_audio *a)
  3031. {
  3032. if (unlikely(a->index))
  3033. return -EINVAL;
  3034. return 0;
  3035. }
  3036. static int radio_s_input(struct file *filp, void *priv, unsigned int i)
  3037. {
  3038. if (unlikely(i))
  3039. return -EINVAL;
  3040. return 0;
  3041. }
  3042. static int radio_s_std(struct file *file, void *fh, v4l2_std_id *norm)
  3043. {
  3044. return 0;
  3045. }
  3046. static int radio_queryctrl(struct file *file, void *priv,
  3047. struct v4l2_queryctrl *c)
  3048. {
  3049. const struct v4l2_queryctrl *ctrl;
  3050. if (c->id < V4L2_CID_BASE ||
  3051. c->id >= V4L2_CID_LASTP1)
  3052. return -EINVAL;
  3053. if (c->id == V4L2_CID_AUDIO_MUTE) {
  3054. ctrl = ctrl_by_id(c->id);
  3055. *c = *ctrl;
  3056. } else
  3057. *c = no_ctl;
  3058. return 0;
  3059. }
  3060. static int radio_g_input(struct file *filp, void *priv, unsigned int *i)
  3061. {
  3062. *i = 0;
  3063. return 0;
  3064. }
  3065. static ssize_t radio_read(struct file *file, char __user *data,
  3066. size_t count, loff_t *ppos)
  3067. {
  3068. struct bttv_fh *fh = file->private_data;
  3069. struct bttv *btv = fh->btv;
  3070. struct rds_command cmd;
  3071. cmd.block_count = count/3;
  3072. cmd.buffer = data;
  3073. cmd.instance = file;
  3074. cmd.result = -ENODEV;
  3075. bttv_call_all(btv, core, ioctl, RDS_CMD_READ, &cmd);
  3076. return cmd.result;
  3077. }
  3078. static unsigned int radio_poll(struct file *file, poll_table *wait)
  3079. {
  3080. struct bttv_fh *fh = file->private_data;
  3081. struct bttv *btv = fh->btv;
  3082. struct rds_command cmd;
  3083. cmd.instance = file;
  3084. cmd.event_list = wait;
  3085. cmd.result = -ENODEV;
  3086. bttv_call_all(btv, core, ioctl, RDS_CMD_POLL, &cmd);
  3087. return cmd.result;
  3088. }
  3089. static const struct v4l2_file_operations radio_fops =
  3090. {
  3091. .owner = THIS_MODULE,
  3092. .open = radio_open,
  3093. .read = radio_read,
  3094. .release = radio_release,
  3095. .ioctl = video_ioctl2,
  3096. .poll = radio_poll,
  3097. };
  3098. static const struct v4l2_ioctl_ops radio_ioctl_ops = {
  3099. .vidioc_querycap = radio_querycap,
  3100. .vidioc_g_tuner = radio_g_tuner,
  3101. .vidioc_enum_input = radio_enum_input,
  3102. .vidioc_g_audio = radio_g_audio,
  3103. .vidioc_s_tuner = radio_s_tuner,
  3104. .vidioc_s_audio = radio_s_audio,
  3105. .vidioc_s_input = radio_s_input,
  3106. .vidioc_s_std = radio_s_std,
  3107. .vidioc_queryctrl = radio_queryctrl,
  3108. .vidioc_g_input = radio_g_input,
  3109. .vidioc_g_ctrl = bttv_g_ctrl,
  3110. .vidioc_s_ctrl = bttv_s_ctrl,
  3111. .vidioc_g_frequency = bttv_g_frequency,
  3112. .vidioc_s_frequency = bttv_s_frequency,
  3113. };
  3114. static struct video_device radio_template = {
  3115. .fops = &radio_fops,
  3116. .ioctl_ops = &radio_ioctl_ops,
  3117. };
  3118. /* ----------------------------------------------------------------------- */
  3119. /* some debug code */
  3120. static int bttv_risc_decode(u32 risc)
  3121. {
  3122. static char *instr[16] = {
  3123. [ BT848_RISC_WRITE >> 28 ] = "write",
  3124. [ BT848_RISC_SKIP >> 28 ] = "skip",
  3125. [ BT848_RISC_WRITEC >> 28 ] = "writec",
  3126. [ BT848_RISC_JUMP >> 28 ] = "jump",
  3127. [ BT848_RISC_SYNC >> 28 ] = "sync",
  3128. [ BT848_RISC_WRITE123 >> 28 ] = "write123",
  3129. [ BT848_RISC_SKIP123 >> 28 ] = "skip123",
  3130. [ BT848_RISC_WRITE1S23 >> 28 ] = "write1s23",
  3131. };
  3132. static int incr[16] = {
  3133. [ BT848_RISC_WRITE >> 28 ] = 2,
  3134. [ BT848_RISC_JUMP >> 28 ] = 2,
  3135. [ BT848_RISC_SYNC >> 28 ] = 2,
  3136. [ BT848_RISC_WRITE123 >> 28 ] = 5,
  3137. [ BT848_RISC_SKIP123 >> 28 ] = 2,
  3138. [ BT848_RISC_WRITE1S23 >> 28 ] = 3,
  3139. };
  3140. static char *bits[] = {
  3141. "be0", "be1", "be2", "be3/resync",
  3142. "set0", "set1", "set2", "set3",
  3143. "clr0", "clr1", "clr2", "clr3",
  3144. "irq", "res", "eol", "sol",
  3145. };
  3146. int i;
  3147. printk("0x%08x [ %s", risc,
  3148. instr[risc >> 28] ? instr[risc >> 28] : "INVALID");
  3149. for (i = ARRAY_SIZE(bits)-1; i >= 0; i--)
  3150. if (risc & (1 << (i + 12)))
  3151. printk(" %s",bits[i]);
  3152. printk(" count=%d ]\n", risc & 0xfff);
  3153. return incr[risc >> 28] ? incr[risc >> 28] : 1;
  3154. }
  3155. static void bttv_risc_disasm(struct bttv *btv,
  3156. struct btcx_riscmem *risc)
  3157. {
  3158. unsigned int i,j,n;
  3159. printk("%s: risc disasm: %p [dma=0x%08lx]\n",
  3160. btv->c.v4l2_dev.name, risc->cpu, (unsigned long)risc->dma);
  3161. for (i = 0; i < (risc->size >> 2); i += n) {
  3162. printk("%s: 0x%lx: ", btv->c.v4l2_dev.name,
  3163. (unsigned long)(risc->dma + (i<<2)));
  3164. n = bttv_risc_decode(le32_to_cpu(risc->cpu[i]));
  3165. for (j = 1; j < n; j++)
  3166. printk("%s: 0x%lx: 0x%08x [ arg #%d ]\n",
  3167. btv->c.v4l2_dev.name, (unsigned long)(risc->dma + ((i+j)<<2)),
  3168. risc->cpu[i+j], j);
  3169. if (0 == risc->cpu[i])
  3170. break;
  3171. }
  3172. }
  3173. static void bttv_print_riscaddr(struct bttv *btv)
  3174. {
  3175. printk(" main: %08Lx\n",
  3176. (unsigned long long)btv->main.dma);
  3177. printk(" vbi : o=%08Lx e=%08Lx\n",
  3178. btv->cvbi ? (unsigned long long)btv->cvbi->top.dma : 0,
  3179. btv->cvbi ? (unsigned long long)btv->cvbi->bottom.dma : 0);
  3180. printk(" cap : o=%08Lx e=%08Lx\n",
  3181. btv->curr.top ? (unsigned long long)btv->curr.top->top.dma : 0,
  3182. btv->curr.bottom ? (unsigned long long)btv->curr.bottom->bottom.dma : 0);
  3183. printk(" scr : o=%08Lx e=%08Lx\n",
  3184. btv->screen ? (unsigned long long)btv->screen->top.dma : 0,
  3185. btv->screen ? (unsigned long long)btv->screen->bottom.dma : 0);
  3186. bttv_risc_disasm(btv, &btv->main);
  3187. }
  3188. /* ----------------------------------------------------------------------- */
  3189. /* irq handler */
  3190. static char *irq_name[] = {
  3191. "FMTCHG", // format change detected (525 vs. 625)
  3192. "VSYNC", // vertical sync (new field)
  3193. "HSYNC", // horizontal sync
  3194. "OFLOW", // chroma/luma AGC overflow
  3195. "HLOCK", // horizontal lock changed
  3196. "VPRES", // video presence changed
  3197. "6", "7",
  3198. "I2CDONE", // hw irc operation finished
  3199. "GPINT", // gpio port triggered irq
  3200. "10",
  3201. "RISCI", // risc instruction triggered irq
  3202. "FBUS", // pixel data fifo dropped data (high pci bus latencies)
  3203. "FTRGT", // pixel data fifo overrun
  3204. "FDSR", // fifo data stream resyncronisation
  3205. "PPERR", // parity error (data transfer)
  3206. "RIPERR", // parity error (read risc instructions)
  3207. "PABORT", // pci abort
  3208. "OCERR", // risc instruction error
  3209. "SCERR", // syncronisation error
  3210. };
  3211. static void bttv_print_irqbits(u32 print, u32 mark)
  3212. {
  3213. unsigned int i;
  3214. printk("bits:");
  3215. for (i = 0; i < ARRAY_SIZE(irq_name); i++) {
  3216. if (print & (1 << i))
  3217. printk(" %s",irq_name[i]);
  3218. if (mark & (1 << i))
  3219. printk("*");
  3220. }
  3221. }
  3222. static void bttv_irq_debug_low_latency(struct bttv *btv, u32 rc)
  3223. {
  3224. printk("bttv%d: irq: skipped frame [main=%lx,o_vbi=%lx,o_field=%lx,rc=%lx]\n",
  3225. btv->c.nr,
  3226. (unsigned long)btv->main.dma,
  3227. (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_VBI+1]),
  3228. (unsigned long)le32_to_cpu(btv->main.cpu[RISC_SLOT_O_FIELD+1]),
  3229. (unsigned long)rc);
  3230. if (0 == (btread(BT848_DSTATUS) & BT848_DSTATUS_HLOC)) {
  3231. printk("bttv%d: Oh, there (temporarely?) is no input signal. "
  3232. "Ok, then this is harmless, don't worry ;)\n",
  3233. btv->c.nr);
  3234. return;
  3235. }
  3236. printk("bttv%d: Uhm. Looks like we have unusual high IRQ latencies.\n",
  3237. btv->c.nr);
  3238. printk("bttv%d: Lets try to catch the culpit red-handed ...\n",
  3239. btv->c.nr);
  3240. dump_stack();
  3241. }
  3242. static int
  3243. bttv_irq_next_video(struct bttv *btv, struct bttv_buffer_set *set)
  3244. {
  3245. struct bttv_buffer *item;
  3246. memset(set,0,sizeof(*set));
  3247. /* capture request ? */
  3248. if (!list_empty(&btv->capture)) {
  3249. set->frame_irq = 1;
  3250. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3251. if (V4L2_FIELD_HAS_TOP(item->vb.field))
  3252. set->top = item;
  3253. if (V4L2_FIELD_HAS_BOTTOM(item->vb.field))
  3254. set->bottom = item;
  3255. /* capture request for other field ? */
  3256. if (!V4L2_FIELD_HAS_BOTH(item->vb.field) &&
  3257. (item->vb.queue.next != &btv->capture)) {
  3258. item = list_entry(item->vb.queue.next, struct bttv_buffer, vb.queue);
  3259. /* Mike Isely <isely@pobox.com> - Only check
  3260. * and set up the bottom field in the logic
  3261. * below. Don't ever do the top field. This
  3262. * of course means that if we set up the
  3263. * bottom field in the above code that we'll
  3264. * actually skip a field. But that's OK.
  3265. * Having processed only a single buffer this
  3266. * time, then the next time around the first
  3267. * available buffer should be for a top field.
  3268. * That will then cause us here to set up a
  3269. * top then a bottom field in the normal way.
  3270. * The alternative to this understanding is
  3271. * that we set up the second available buffer
  3272. * as a top field, but that's out of order
  3273. * since this driver always processes the top
  3274. * field first - the effect will be the two
  3275. * buffers being returned in the wrong order,
  3276. * with the second buffer also being delayed
  3277. * by one field time (owing to the fifo nature
  3278. * of videobuf). Worse still, we'll be stuck
  3279. * doing fields out of order now every time
  3280. * until something else causes a field to be
  3281. * dropped. By effectively forcing a field to
  3282. * drop this way then we always get back into
  3283. * sync within a single frame time. (Out of
  3284. * order fields can screw up deinterlacing
  3285. * algorithms.) */
  3286. if (!V4L2_FIELD_HAS_BOTH(item->vb.field)) {
  3287. if (NULL == set->bottom &&
  3288. V4L2_FIELD_BOTTOM == item->vb.field) {
  3289. set->bottom = item;
  3290. }
  3291. if (NULL != set->top && NULL != set->bottom)
  3292. set->top_irq = 2;
  3293. }
  3294. }
  3295. }
  3296. /* screen overlay ? */
  3297. if (NULL != btv->screen) {
  3298. if (V4L2_FIELD_HAS_BOTH(btv->screen->vb.field)) {
  3299. if (NULL == set->top && NULL == set->bottom) {
  3300. set->top = btv->screen;
  3301. set->bottom = btv->screen;
  3302. }
  3303. } else {
  3304. if (V4L2_FIELD_TOP == btv->screen->vb.field &&
  3305. NULL == set->top) {
  3306. set->top = btv->screen;
  3307. }
  3308. if (V4L2_FIELD_BOTTOM == btv->screen->vb.field &&
  3309. NULL == set->bottom) {
  3310. set->bottom = btv->screen;
  3311. }
  3312. }
  3313. }
  3314. dprintk("bttv%d: next set: top=%p bottom=%p [screen=%p,irq=%d,%d]\n",
  3315. btv->c.nr,set->top, set->bottom,
  3316. btv->screen,set->frame_irq,set->top_irq);
  3317. return 0;
  3318. }
  3319. static void
  3320. bttv_irq_wakeup_video(struct bttv *btv, struct bttv_buffer_set *wakeup,
  3321. struct bttv_buffer_set *curr, unsigned int state)
  3322. {
  3323. struct timeval ts;
  3324. do_gettimeofday(&ts);
  3325. if (wakeup->top == wakeup->bottom) {
  3326. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3327. if (irq_debug > 1)
  3328. printk("bttv%d: wakeup: both=%p\n",btv->c.nr,wakeup->top);
  3329. wakeup->top->vb.ts = ts;
  3330. wakeup->top->vb.field_count = btv->field_count;
  3331. wakeup->top->vb.state = state;
  3332. wake_up(&wakeup->top->vb.done);
  3333. }
  3334. } else {
  3335. if (NULL != wakeup->top && curr->top != wakeup->top) {
  3336. if (irq_debug > 1)
  3337. printk("bttv%d: wakeup: top=%p\n",btv->c.nr,wakeup->top);
  3338. wakeup->top->vb.ts = ts;
  3339. wakeup->top->vb.field_count = btv->field_count;
  3340. wakeup->top->vb.state = state;
  3341. wake_up(&wakeup->top->vb.done);
  3342. }
  3343. if (NULL != wakeup->bottom && curr->bottom != wakeup->bottom) {
  3344. if (irq_debug > 1)
  3345. printk("bttv%d: wakeup: bottom=%p\n",btv->c.nr,wakeup->bottom);
  3346. wakeup->bottom->vb.ts = ts;
  3347. wakeup->bottom->vb.field_count = btv->field_count;
  3348. wakeup->bottom->vb.state = state;
  3349. wake_up(&wakeup->bottom->vb.done);
  3350. }
  3351. }
  3352. }
  3353. static void
  3354. bttv_irq_wakeup_vbi(struct bttv *btv, struct bttv_buffer *wakeup,
  3355. unsigned int state)
  3356. {
  3357. struct timeval ts;
  3358. if (NULL == wakeup)
  3359. return;
  3360. do_gettimeofday(&ts);
  3361. wakeup->vb.ts = ts;
  3362. wakeup->vb.field_count = btv->field_count;
  3363. wakeup->vb.state = state;
  3364. wake_up(&wakeup->vb.done);
  3365. }
  3366. static void bttv_irq_timeout(unsigned long data)
  3367. {
  3368. struct bttv *btv = (struct bttv *)data;
  3369. struct bttv_buffer_set old,new;
  3370. struct bttv_buffer *ovbi;
  3371. struct bttv_buffer *item;
  3372. unsigned long flags;
  3373. if (bttv_verbose) {
  3374. printk(KERN_INFO "bttv%d: timeout: drop=%d irq=%d/%d, risc=%08x, ",
  3375. btv->c.nr, btv->framedrop, btv->irq_me, btv->irq_total,
  3376. btread(BT848_RISC_COUNT));
  3377. bttv_print_irqbits(btread(BT848_INT_STAT),0);
  3378. printk("\n");
  3379. }
  3380. spin_lock_irqsave(&btv->s_lock,flags);
  3381. /* deactivate stuff */
  3382. memset(&new,0,sizeof(new));
  3383. old = btv->curr;
  3384. ovbi = btv->cvbi;
  3385. btv->curr = new;
  3386. btv->cvbi = NULL;
  3387. btv->loop_irq = 0;
  3388. bttv_buffer_activate_video(btv, &new);
  3389. bttv_buffer_activate_vbi(btv, NULL);
  3390. bttv_set_dma(btv, 0);
  3391. /* wake up */
  3392. bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_ERROR);
  3393. bttv_irq_wakeup_vbi(btv, ovbi, VIDEOBUF_ERROR);
  3394. /* cancel all outstanding capture / vbi requests */
  3395. while (!list_empty(&btv->capture)) {
  3396. item = list_entry(btv->capture.next, struct bttv_buffer, vb.queue);
  3397. list_del(&item->vb.queue);
  3398. item->vb.state = VIDEOBUF_ERROR;
  3399. wake_up(&item->vb.done);
  3400. }
  3401. while (!list_empty(&btv->vcapture)) {
  3402. item = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3403. list_del(&item->vb.queue);
  3404. item->vb.state = VIDEOBUF_ERROR;
  3405. wake_up(&item->vb.done);
  3406. }
  3407. btv->errors++;
  3408. spin_unlock_irqrestore(&btv->s_lock,flags);
  3409. }
  3410. static void
  3411. bttv_irq_wakeup_top(struct bttv *btv)
  3412. {
  3413. struct bttv_buffer *wakeup = btv->curr.top;
  3414. if (NULL == wakeup)
  3415. return;
  3416. spin_lock(&btv->s_lock);
  3417. btv->curr.top_irq = 0;
  3418. btv->curr.top = NULL;
  3419. bttv_risc_hook(btv, RISC_SLOT_O_FIELD, NULL, 0);
  3420. do_gettimeofday(&wakeup->vb.ts);
  3421. wakeup->vb.field_count = btv->field_count;
  3422. wakeup->vb.state = VIDEOBUF_DONE;
  3423. wake_up(&wakeup->vb.done);
  3424. spin_unlock(&btv->s_lock);
  3425. }
  3426. static inline int is_active(struct btcx_riscmem *risc, u32 rc)
  3427. {
  3428. if (rc < risc->dma)
  3429. return 0;
  3430. if (rc > risc->dma + risc->size)
  3431. return 0;
  3432. return 1;
  3433. }
  3434. static void
  3435. bttv_irq_switch_video(struct bttv *btv)
  3436. {
  3437. struct bttv_buffer_set new;
  3438. struct bttv_buffer_set old;
  3439. dma_addr_t rc;
  3440. spin_lock(&btv->s_lock);
  3441. /* new buffer set */
  3442. bttv_irq_next_video(btv, &new);
  3443. rc = btread(BT848_RISC_COUNT);
  3444. if ((btv->curr.top && is_active(&btv->curr.top->top, rc)) ||
  3445. (btv->curr.bottom && is_active(&btv->curr.bottom->bottom, rc))) {
  3446. btv->framedrop++;
  3447. if (debug_latency)
  3448. bttv_irq_debug_low_latency(btv, rc);
  3449. spin_unlock(&btv->s_lock);
  3450. return;
  3451. }
  3452. /* switch over */
  3453. old = btv->curr;
  3454. btv->curr = new;
  3455. btv->loop_irq &= ~1;
  3456. bttv_buffer_activate_video(btv, &new);
  3457. bttv_set_dma(btv, 0);
  3458. /* switch input */
  3459. if (UNSET != btv->new_input) {
  3460. video_mux(btv,btv->new_input);
  3461. btv->new_input = UNSET;
  3462. }
  3463. /* wake up finished buffers */
  3464. bttv_irq_wakeup_video(btv, &old, &new, VIDEOBUF_DONE);
  3465. spin_unlock(&btv->s_lock);
  3466. }
  3467. static void
  3468. bttv_irq_switch_vbi(struct bttv *btv)
  3469. {
  3470. struct bttv_buffer *new = NULL;
  3471. struct bttv_buffer *old;
  3472. u32 rc;
  3473. spin_lock(&btv->s_lock);
  3474. if (!list_empty(&btv->vcapture))
  3475. new = list_entry(btv->vcapture.next, struct bttv_buffer, vb.queue);
  3476. old = btv->cvbi;
  3477. rc = btread(BT848_RISC_COUNT);
  3478. if (NULL != old && (is_active(&old->top, rc) ||
  3479. is_active(&old->bottom, rc))) {
  3480. btv->framedrop++;
  3481. if (debug_latency)
  3482. bttv_irq_debug_low_latency(btv, rc);
  3483. spin_unlock(&btv->s_lock);
  3484. return;
  3485. }
  3486. /* switch */
  3487. btv->cvbi = new;
  3488. btv->loop_irq &= ~4;
  3489. bttv_buffer_activate_vbi(btv, new);
  3490. bttv_set_dma(btv, 0);
  3491. bttv_irq_wakeup_vbi(btv, old, VIDEOBUF_DONE);
  3492. spin_unlock(&btv->s_lock);
  3493. }
  3494. static irqreturn_t bttv_irq(int irq, void *dev_id)
  3495. {
  3496. u32 stat,astat;
  3497. u32 dstat;
  3498. int count;
  3499. struct bttv *btv;
  3500. int handled = 0;
  3501. btv=(struct bttv *)dev_id;
  3502. if (btv->custom_irq)
  3503. handled = btv->custom_irq(btv);
  3504. count=0;
  3505. while (1) {
  3506. /* get/clear interrupt status bits */
  3507. stat=btread(BT848_INT_STAT);
  3508. astat=stat&btread(BT848_INT_MASK);
  3509. if (!astat)
  3510. break;
  3511. handled = 1;
  3512. btwrite(stat,BT848_INT_STAT);
  3513. /* get device status bits */
  3514. dstat=btread(BT848_DSTATUS);
  3515. if (irq_debug) {
  3516. printk(KERN_DEBUG "bttv%d: irq loop=%d fc=%d "
  3517. "riscs=%x, riscc=%08x, ",
  3518. btv->c.nr, count, btv->field_count,
  3519. stat>>28, btread(BT848_RISC_COUNT));
  3520. bttv_print_irqbits(stat,astat);
  3521. if (stat & BT848_INT_HLOCK)
  3522. printk(" HLOC => %s", (dstat & BT848_DSTATUS_HLOC)
  3523. ? "yes" : "no");
  3524. if (stat & BT848_INT_VPRES)
  3525. printk(" PRES => %s", (dstat & BT848_DSTATUS_PRES)
  3526. ? "yes" : "no");
  3527. if (stat & BT848_INT_FMTCHG)
  3528. printk(" NUML => %s", (dstat & BT848_DSTATUS_NUML)
  3529. ? "625" : "525");
  3530. printk("\n");
  3531. }
  3532. if (astat&BT848_INT_VSYNC)
  3533. btv->field_count++;
  3534. if ((astat & BT848_INT_GPINT) && btv->remote) {
  3535. wake_up(&btv->gpioq);
  3536. bttv_input_irq(btv);
  3537. }
  3538. if (astat & BT848_INT_I2CDONE) {
  3539. btv->i2c_done = stat;
  3540. wake_up(&btv->i2c_queue);
  3541. }
  3542. if ((astat & BT848_INT_RISCI) && (stat & (4<<28)))
  3543. bttv_irq_switch_vbi(btv);
  3544. if ((astat & BT848_INT_RISCI) && (stat & (2<<28)))
  3545. bttv_irq_wakeup_top(btv);
  3546. if ((astat & BT848_INT_RISCI) && (stat & (1<<28)))
  3547. bttv_irq_switch_video(btv);
  3548. if ((astat & BT848_INT_HLOCK) && btv->opt_automute)
  3549. audio_mute(btv, btv->mute); /* trigger automute */
  3550. if (astat & (BT848_INT_SCERR|BT848_INT_OCERR)) {
  3551. printk(KERN_INFO "bttv%d: %s%s @ %08x,",btv->c.nr,
  3552. (astat & BT848_INT_SCERR) ? "SCERR" : "",
  3553. (astat & BT848_INT_OCERR) ? "OCERR" : "",
  3554. btread(BT848_RISC_COUNT));
  3555. bttv_print_irqbits(stat,astat);
  3556. printk("\n");
  3557. if (bttv_debug)
  3558. bttv_print_riscaddr(btv);
  3559. }
  3560. if (fdsr && astat & BT848_INT_FDSR) {
  3561. printk(KERN_INFO "bttv%d: FDSR @ %08x\n",
  3562. btv->c.nr,btread(BT848_RISC_COUNT));
  3563. if (bttv_debug)
  3564. bttv_print_riscaddr(btv);
  3565. }
  3566. count++;
  3567. if (count > 4) {
  3568. if (count > 8 || !(astat & BT848_INT_GPINT)) {
  3569. btwrite(0, BT848_INT_MASK);
  3570. printk(KERN_ERR
  3571. "bttv%d: IRQ lockup, cleared int mask [", btv->c.nr);
  3572. } else {
  3573. printk(KERN_ERR
  3574. "bttv%d: IRQ lockup, clearing GPINT from int mask [", btv->c.nr);
  3575. btwrite(btread(BT848_INT_MASK) & (-1 ^ BT848_INT_GPINT),
  3576. BT848_INT_MASK);
  3577. };
  3578. bttv_print_irqbits(stat,astat);
  3579. printk("]\n");
  3580. }
  3581. }
  3582. btv->irq_total++;
  3583. if (handled)
  3584. btv->irq_me++;
  3585. return IRQ_RETVAL(handled);
  3586. }
  3587. /* ----------------------------------------------------------------------- */
  3588. /* initialitation */
  3589. static struct video_device *vdev_init(struct bttv *btv,
  3590. const struct video_device *template,
  3591. const char *type_name)
  3592. {
  3593. struct video_device *vfd;
  3594. vfd = video_device_alloc();
  3595. if (NULL == vfd)
  3596. return NULL;
  3597. *vfd = *template;
  3598. vfd->v4l2_dev = &btv->c.v4l2_dev;
  3599. vfd->release = video_device_release;
  3600. vfd->debug = bttv_debug;
  3601. video_set_drvdata(vfd, btv);
  3602. snprintf(vfd->name, sizeof(vfd->name), "BT%d%s %s (%s)",
  3603. btv->id, (btv->id==848 && btv->revision==0x12) ? "A" : "",
  3604. type_name, bttv_tvcards[btv->c.type].name);
  3605. return vfd;
  3606. }
  3607. static void bttv_unregister_video(struct bttv *btv)
  3608. {
  3609. if (btv->video_dev) {
  3610. if (video_is_registered(btv->video_dev))
  3611. video_unregister_device(btv->video_dev);
  3612. else
  3613. video_device_release(btv->video_dev);
  3614. btv->video_dev = NULL;
  3615. }
  3616. if (btv->vbi_dev) {
  3617. if (video_is_registered(btv->vbi_dev))
  3618. video_unregister_device(btv->vbi_dev);
  3619. else
  3620. video_device_release(btv->vbi_dev);
  3621. btv->vbi_dev = NULL;
  3622. }
  3623. if (btv->radio_dev) {
  3624. if (video_is_registered(btv->radio_dev))
  3625. video_unregister_device(btv->radio_dev);
  3626. else
  3627. video_device_release(btv->radio_dev);
  3628. btv->radio_dev = NULL;
  3629. }
  3630. }
  3631. /* register video4linux devices */
  3632. static int __devinit bttv_register_video(struct bttv *btv)
  3633. {
  3634. if (no_overlay > 0)
  3635. printk("bttv: Overlay support disabled.\n");
  3636. /* video */
  3637. btv->video_dev = vdev_init(btv, &bttv_video_template, "video");
  3638. if (NULL == btv->video_dev)
  3639. goto err;
  3640. if (video_register_device(btv->video_dev, VFL_TYPE_GRABBER,
  3641. video_nr[btv->c.nr]) < 0)
  3642. goto err;
  3643. printk(KERN_INFO "bttv%d: registered device %s\n",
  3644. btv->c.nr, video_device_node_name(btv->video_dev));
  3645. if (device_create_file(&btv->video_dev->dev,
  3646. &dev_attr_card)<0) {
  3647. printk(KERN_ERR "bttv%d: device_create_file 'card' "
  3648. "failed\n", btv->c.nr);
  3649. goto err;
  3650. }
  3651. /* vbi */
  3652. btv->vbi_dev = vdev_init(btv, &bttv_video_template, "vbi");
  3653. if (NULL == btv->vbi_dev)
  3654. goto err;
  3655. if (video_register_device(btv->vbi_dev, VFL_TYPE_VBI,
  3656. vbi_nr[btv->c.nr]) < 0)
  3657. goto err;
  3658. printk(KERN_INFO "bttv%d: registered device %s\n",
  3659. btv->c.nr, video_device_node_name(btv->vbi_dev));
  3660. if (!btv->has_radio)
  3661. return 0;
  3662. /* radio */
  3663. btv->radio_dev = vdev_init(btv, &radio_template, "radio");
  3664. if (NULL == btv->radio_dev)
  3665. goto err;
  3666. if (video_register_device(btv->radio_dev, VFL_TYPE_RADIO,
  3667. radio_nr[btv->c.nr]) < 0)
  3668. goto err;
  3669. printk(KERN_INFO "bttv%d: registered device %s\n",
  3670. btv->c.nr, video_device_node_name(btv->radio_dev));
  3671. /* all done */
  3672. return 0;
  3673. err:
  3674. bttv_unregister_video(btv);
  3675. return -1;
  3676. }
  3677. /* on OpenFirmware machines (PowerMac at least), PCI memory cycle */
  3678. /* response on cards with no firmware is not enabled by OF */
  3679. static void pci_set_command(struct pci_dev *dev)
  3680. {
  3681. #if defined(__powerpc__)
  3682. unsigned int cmd;
  3683. pci_read_config_dword(dev, PCI_COMMAND, &cmd);
  3684. cmd = (cmd | PCI_COMMAND_MEMORY );
  3685. pci_write_config_dword(dev, PCI_COMMAND, cmd);
  3686. #endif
  3687. }
  3688. static int __devinit bttv_probe(struct pci_dev *dev,
  3689. const struct pci_device_id *pci_id)
  3690. {
  3691. int result;
  3692. unsigned char lat;
  3693. struct bttv *btv;
  3694. if (bttv_num == BTTV_MAX)
  3695. return -ENOMEM;
  3696. printk(KERN_INFO "bttv: Bt8xx card found (%d).\n", bttv_num);
  3697. bttvs[bttv_num] = btv = kzalloc(sizeof(*btv), GFP_KERNEL);
  3698. if (btv == NULL) {
  3699. printk(KERN_ERR "bttv: out of memory.\n");
  3700. return -ENOMEM;
  3701. }
  3702. btv->c.nr = bttv_num;
  3703. snprintf(btv->c.v4l2_dev.name, sizeof(btv->c.v4l2_dev.name),
  3704. "bttv%d", btv->c.nr);
  3705. /* initialize structs / fill in defaults */
  3706. mutex_init(&btv->lock);
  3707. spin_lock_init(&btv->s_lock);
  3708. spin_lock_init(&btv->gpio_lock);
  3709. init_waitqueue_head(&btv->gpioq);
  3710. init_waitqueue_head(&btv->i2c_queue);
  3711. INIT_LIST_HEAD(&btv->c.subs);
  3712. INIT_LIST_HEAD(&btv->capture);
  3713. INIT_LIST_HEAD(&btv->vcapture);
  3714. v4l2_prio_init(&btv->prio);
  3715. init_timer(&btv->timeout);
  3716. btv->timeout.function = bttv_irq_timeout;
  3717. btv->timeout.data = (unsigned long)btv;
  3718. btv->i2c_rc = -1;
  3719. btv->tuner_type = UNSET;
  3720. btv->new_input = UNSET;
  3721. btv->has_radio=radio[btv->c.nr];
  3722. /* pci stuff (init, get irq/mmio, ... */
  3723. btv->c.pci = dev;
  3724. btv->id = dev->device;
  3725. if (pci_enable_device(dev)) {
  3726. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3727. btv->c.nr);
  3728. return -EIO;
  3729. }
  3730. if (pci_set_dma_mask(dev, DMA_BIT_MASK(32))) {
  3731. printk(KERN_WARNING "bttv%d: No suitable DMA available.\n",
  3732. btv->c.nr);
  3733. return -EIO;
  3734. }
  3735. if (!request_mem_region(pci_resource_start(dev,0),
  3736. pci_resource_len(dev,0),
  3737. btv->c.v4l2_dev.name)) {
  3738. printk(KERN_WARNING "bttv%d: can't request iomem (0x%llx).\n",
  3739. btv->c.nr,
  3740. (unsigned long long)pci_resource_start(dev,0));
  3741. return -EBUSY;
  3742. }
  3743. pci_set_master(dev);
  3744. pci_set_command(dev);
  3745. result = v4l2_device_register(&dev->dev, &btv->c.v4l2_dev);
  3746. if (result < 0) {
  3747. printk(KERN_WARNING "bttv%d: v4l2_device_register() failed\n", btv->c.nr);
  3748. goto fail0;
  3749. }
  3750. pci_read_config_byte(dev, PCI_CLASS_REVISION, &btv->revision);
  3751. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  3752. printk(KERN_INFO "bttv%d: Bt%d (rev %d) at %s, ",
  3753. bttv_num,btv->id, btv->revision, pci_name(dev));
  3754. printk("irq: %d, latency: %d, mmio: 0x%llx\n",
  3755. btv->c.pci->irq, lat,
  3756. (unsigned long long)pci_resource_start(dev,0));
  3757. schedule();
  3758. btv->bt848_mmio = ioremap(pci_resource_start(dev, 0), 0x1000);
  3759. if (NULL == btv->bt848_mmio) {
  3760. printk("bttv%d: ioremap() failed\n", btv->c.nr);
  3761. result = -EIO;
  3762. goto fail1;
  3763. }
  3764. /* identify card */
  3765. bttv_idcard(btv);
  3766. /* disable irqs, register irq handler */
  3767. btwrite(0, BT848_INT_MASK);
  3768. result = request_irq(btv->c.pci->irq, bttv_irq,
  3769. IRQF_SHARED | IRQF_DISABLED, btv->c.v4l2_dev.name, (void *)btv);
  3770. if (result < 0) {
  3771. printk(KERN_ERR "bttv%d: can't get IRQ %d\n",
  3772. bttv_num,btv->c.pci->irq);
  3773. goto fail1;
  3774. }
  3775. if (0 != bttv_handle_chipset(btv)) {
  3776. result = -EIO;
  3777. goto fail2;
  3778. }
  3779. /* init options from insmod args */
  3780. btv->opt_combfilter = combfilter;
  3781. btv->opt_lumafilter = lumafilter;
  3782. btv->opt_automute = automute;
  3783. btv->opt_chroma_agc = chroma_agc;
  3784. btv->opt_adc_crush = adc_crush;
  3785. btv->opt_vcr_hack = vcr_hack;
  3786. btv->opt_whitecrush_upper = whitecrush_upper;
  3787. btv->opt_whitecrush_lower = whitecrush_lower;
  3788. btv->opt_uv_ratio = uv_ratio;
  3789. btv->opt_full_luma_range = full_luma_range;
  3790. btv->opt_coring = coring;
  3791. /* fill struct bttv with some useful defaults */
  3792. btv->init.btv = btv;
  3793. btv->init.ov.w.width = 320;
  3794. btv->init.ov.w.height = 240;
  3795. btv->init.fmt = format_by_fourcc(V4L2_PIX_FMT_BGR24);
  3796. btv->init.width = 320;
  3797. btv->init.height = 240;
  3798. btv->input = 0;
  3799. /* initialize hardware */
  3800. if (bttv_gpio)
  3801. bttv_gpio_tracking(btv,"pre-init");
  3802. bttv_risc_init_main(btv);
  3803. init_bt848(btv);
  3804. /* gpio */
  3805. btwrite(0x00, BT848_GPIO_REG_INP);
  3806. btwrite(0x00, BT848_GPIO_OUT_EN);
  3807. if (bttv_verbose)
  3808. bttv_gpio_tracking(btv,"init");
  3809. /* needs to be done before i2c is registered */
  3810. bttv_init_card1(btv);
  3811. /* register i2c + gpio */
  3812. init_bttv_i2c(btv);
  3813. /* some card-specific stuff (needs working i2c) */
  3814. bttv_init_card2(btv);
  3815. bttv_init_tuner(btv);
  3816. init_irqreg(btv);
  3817. /* register video4linux + input */
  3818. if (!bttv_tvcards[btv->c.type].no_video) {
  3819. bttv_register_video(btv);
  3820. bt848_bright(btv,32768);
  3821. bt848_contrast(btv,32768);
  3822. bt848_hue(btv,32768);
  3823. bt848_sat(btv,32768);
  3824. audio_mute(btv, 1);
  3825. set_input(btv, 0, btv->tvnorm);
  3826. bttv_crop_reset(&btv->crop[0], btv->tvnorm);
  3827. btv->crop[1] = btv->crop[0]; /* current = default */
  3828. disclaim_vbi_lines(btv);
  3829. disclaim_video_lines(btv);
  3830. }
  3831. /* add subdevices and autoload dvb-bt8xx if needed */
  3832. if (bttv_tvcards[btv->c.type].has_dvb) {
  3833. bttv_sub_add_device(&btv->c, "dvb");
  3834. request_modules(btv);
  3835. }
  3836. if (!disable_ir) {
  3837. init_bttv_i2c_ir(btv);
  3838. bttv_input_init(btv);
  3839. }
  3840. /* everything is fine */
  3841. bttv_num++;
  3842. return 0;
  3843. fail2:
  3844. free_irq(btv->c.pci->irq,btv);
  3845. fail1:
  3846. v4l2_device_unregister(&btv->c.v4l2_dev);
  3847. fail0:
  3848. if (btv->bt848_mmio)
  3849. iounmap(btv->bt848_mmio);
  3850. release_mem_region(pci_resource_start(btv->c.pci,0),
  3851. pci_resource_len(btv->c.pci,0));
  3852. return result;
  3853. }
  3854. static void __devexit bttv_remove(struct pci_dev *pci_dev)
  3855. {
  3856. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3857. struct bttv *btv = to_bttv(v4l2_dev);
  3858. if (bttv_verbose)
  3859. printk("bttv%d: unloading\n",btv->c.nr);
  3860. /* shutdown everything (DMA+IRQs) */
  3861. btand(~15, BT848_GPIO_DMA_CTL);
  3862. btwrite(0, BT848_INT_MASK);
  3863. btwrite(~0x0, BT848_INT_STAT);
  3864. btwrite(0x0, BT848_GPIO_OUT_EN);
  3865. if (bttv_gpio)
  3866. bttv_gpio_tracking(btv,"cleanup");
  3867. /* tell gpio modules we are leaving ... */
  3868. btv->shutdown=1;
  3869. wake_up(&btv->gpioq);
  3870. bttv_input_fini(btv);
  3871. bttv_sub_del_devices(&btv->c);
  3872. /* unregister i2c_bus + input */
  3873. fini_bttv_i2c(btv);
  3874. /* unregister video4linux */
  3875. bttv_unregister_video(btv);
  3876. /* free allocated memory */
  3877. btcx_riscmem_free(btv->c.pci,&btv->main);
  3878. /* free ressources */
  3879. free_irq(btv->c.pci->irq,btv);
  3880. iounmap(btv->bt848_mmio);
  3881. release_mem_region(pci_resource_start(btv->c.pci,0),
  3882. pci_resource_len(btv->c.pci,0));
  3883. v4l2_device_unregister(&btv->c.v4l2_dev);
  3884. bttvs[btv->c.nr] = NULL;
  3885. kfree(btv);
  3886. return;
  3887. }
  3888. #ifdef CONFIG_PM
  3889. static int bttv_suspend(struct pci_dev *pci_dev, pm_message_t state)
  3890. {
  3891. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3892. struct bttv *btv = to_bttv(v4l2_dev);
  3893. struct bttv_buffer_set idle;
  3894. unsigned long flags;
  3895. dprintk("bttv%d: suspend %d\n", btv->c.nr, state.event);
  3896. /* stop dma + irqs */
  3897. spin_lock_irqsave(&btv->s_lock,flags);
  3898. memset(&idle, 0, sizeof(idle));
  3899. btv->state.video = btv->curr;
  3900. btv->state.vbi = btv->cvbi;
  3901. btv->state.loop_irq = btv->loop_irq;
  3902. btv->curr = idle;
  3903. btv->loop_irq = 0;
  3904. bttv_buffer_activate_video(btv, &idle);
  3905. bttv_buffer_activate_vbi(btv, NULL);
  3906. bttv_set_dma(btv, 0);
  3907. btwrite(0, BT848_INT_MASK);
  3908. spin_unlock_irqrestore(&btv->s_lock,flags);
  3909. /* save bt878 state */
  3910. btv->state.gpio_enable = btread(BT848_GPIO_OUT_EN);
  3911. btv->state.gpio_data = gpio_read();
  3912. /* save pci state */
  3913. pci_save_state(pci_dev);
  3914. if (0 != pci_set_power_state(pci_dev, pci_choose_state(pci_dev, state))) {
  3915. pci_disable_device(pci_dev);
  3916. btv->state.disabled = 1;
  3917. }
  3918. return 0;
  3919. }
  3920. static int bttv_resume(struct pci_dev *pci_dev)
  3921. {
  3922. struct v4l2_device *v4l2_dev = pci_get_drvdata(pci_dev);
  3923. struct bttv *btv = to_bttv(v4l2_dev);
  3924. unsigned long flags;
  3925. int err;
  3926. dprintk("bttv%d: resume\n", btv->c.nr);
  3927. /* restore pci state */
  3928. if (btv->state.disabled) {
  3929. err=pci_enable_device(pci_dev);
  3930. if (err) {
  3931. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3932. btv->c.nr);
  3933. return err;
  3934. }
  3935. btv->state.disabled = 0;
  3936. }
  3937. err=pci_set_power_state(pci_dev, PCI_D0);
  3938. if (err) {
  3939. pci_disable_device(pci_dev);
  3940. printk(KERN_WARNING "bttv%d: Can't enable device.\n",
  3941. btv->c.nr);
  3942. btv->state.disabled = 1;
  3943. return err;
  3944. }
  3945. pci_restore_state(pci_dev);
  3946. /* restore bt878 state */
  3947. bttv_reinit_bt848(btv);
  3948. gpio_inout(0xffffff, btv->state.gpio_enable);
  3949. gpio_write(btv->state.gpio_data);
  3950. /* restart dma */
  3951. spin_lock_irqsave(&btv->s_lock,flags);
  3952. btv->curr = btv->state.video;
  3953. btv->cvbi = btv->state.vbi;
  3954. btv->loop_irq = btv->state.loop_irq;
  3955. bttv_buffer_activate_video(btv, &btv->curr);
  3956. bttv_buffer_activate_vbi(btv, btv->cvbi);
  3957. bttv_set_dma(btv, 0);
  3958. spin_unlock_irqrestore(&btv->s_lock,flags);
  3959. return 0;
  3960. }
  3961. #endif
  3962. static struct pci_device_id bttv_pci_tbl[] = {
  3963. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT848), 0},
  3964. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT849), 0},
  3965. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT878), 0},
  3966. {PCI_VDEVICE(BROOKTREE, PCI_DEVICE_ID_BT879), 0},
  3967. {0,}
  3968. };
  3969. MODULE_DEVICE_TABLE(pci, bttv_pci_tbl);
  3970. static struct pci_driver bttv_pci_driver = {
  3971. .name = "bttv",
  3972. .id_table = bttv_pci_tbl,
  3973. .probe = bttv_probe,
  3974. .remove = __devexit_p(bttv_remove),
  3975. #ifdef CONFIG_PM
  3976. .suspend = bttv_suspend,
  3977. .resume = bttv_resume,
  3978. #endif
  3979. };
  3980. static int __init bttv_init_module(void)
  3981. {
  3982. int ret;
  3983. bttv_num = 0;
  3984. printk(KERN_INFO "bttv: driver version %d.%d.%d loaded\n",
  3985. (BTTV_VERSION_CODE >> 16) & 0xff,
  3986. (BTTV_VERSION_CODE >> 8) & 0xff,
  3987. BTTV_VERSION_CODE & 0xff);
  3988. #ifdef SNAPSHOT
  3989. printk(KERN_INFO "bttv: snapshot date %04d-%02d-%02d\n",
  3990. SNAPSHOT/10000, (SNAPSHOT/100)%100, SNAPSHOT%100);
  3991. #endif
  3992. if (gbuffers < 2 || gbuffers > VIDEO_MAX_FRAME)
  3993. gbuffers = 2;
  3994. if (gbufsize > BTTV_MAX_FBUF)
  3995. gbufsize = BTTV_MAX_FBUF;
  3996. gbufsize = (gbufsize + PAGE_SIZE - 1) & PAGE_MASK;
  3997. if (bttv_verbose)
  3998. printk(KERN_INFO "bttv: using %d buffers with %dk (%d pages) each for capture\n",
  3999. gbuffers, gbufsize >> 10, gbufsize >> PAGE_SHIFT);
  4000. bttv_check_chipset();
  4001. ret = bus_register(&bttv_sub_bus_type);
  4002. if (ret < 0) {
  4003. printk(KERN_WARNING "bttv: bus_register error: %d\n", ret);
  4004. return ret;
  4005. }
  4006. ret = pci_register_driver(&bttv_pci_driver);
  4007. if (ret < 0)
  4008. bus_unregister(&bttv_sub_bus_type);
  4009. return ret;
  4010. }
  4011. static void __exit bttv_cleanup_module(void)
  4012. {
  4013. pci_unregister_driver(&bttv_pci_driver);
  4014. bus_unregister(&bttv_sub_bus_type);
  4015. }
  4016. module_init(bttv_init_module);
  4017. module_exit(bttv_cleanup_module);
  4018. /*
  4019. * Local variables:
  4020. * c-basic-offset: 8
  4021. * End:
  4022. */