pms.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  1. /*
  2. * Media Vision Pro Movie Studio
  3. * or
  4. * "all you need is an I2C bus some RAM and a prayer"
  5. *
  6. * This draws heavily on code
  7. *
  8. * (c) Wolfgang Koehler, wolf@first.gmd.de, Dec. 1994
  9. * Kiefernring 15
  10. * 14478 Potsdam, Germany
  11. *
  12. * Most of this code is directly derived from his userspace driver.
  13. * His driver works so send any reports to alan@lxorguk.ukuu.org.uk
  14. * unless the userspace driver also doesn't work for you...
  15. *
  16. * Changes:
  17. * 25-11-2009 Hans Verkuil <hverkuil@xs4all.nl>
  18. * - converted to version 2 of the V4L API.
  19. * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
  20. * - pms_capture: report back -EFAULT
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/kernel.h>
  27. #include <linux/slab.h>
  28. #include <linux/mm.h>
  29. #include <linux/ioport.h>
  30. #include <linux/init.h>
  31. #include <linux/version.h>
  32. #include <linux/mutex.h>
  33. #include <asm/uaccess.h>
  34. #include <asm/io.h>
  35. #include <linux/videodev2.h>
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <media/v4l2-device.h>
  39. MODULE_LICENSE("GPL");
  40. #define MOTOROLA 1
  41. #define PHILIPS2 2 /* SAA7191 */
  42. #define PHILIPS1 3
  43. #define MVVMEMORYWIDTH 0x40 /* 512 bytes */
  44. struct i2c_info {
  45. u8 slave;
  46. u8 sub;
  47. u8 data;
  48. u8 hits;
  49. };
  50. struct pms {
  51. struct v4l2_device v4l2_dev;
  52. struct video_device vdev;
  53. int height;
  54. int width;
  55. int depth;
  56. int input;
  57. s32 brightness, saturation, hue, contrast;
  58. unsigned long in_use;
  59. struct mutex lock;
  60. int i2c_count;
  61. struct i2c_info i2cinfo[64];
  62. int decoder;
  63. int standard; /* 0 - auto 1 - ntsc 2 - pal 3 - secam */
  64. v4l2_std_id std;
  65. int io;
  66. int data;
  67. void __iomem *mem;
  68. };
  69. static struct pms pms_card;
  70. /*
  71. * I/O ports and Shared Memory
  72. */
  73. static int io_port = 0x250;
  74. module_param(io_port, int, 0);
  75. static int mem_base = 0xc8000;
  76. module_param(mem_base, int, 0);
  77. static int video_nr = -1;
  78. module_param(video_nr, int, 0);
  79. static inline void mvv_write(struct pms *dev, u8 index, u8 value)
  80. {
  81. outw(index | (value << 8), dev->io);
  82. }
  83. static inline u8 mvv_read(struct pms *dev, u8 index)
  84. {
  85. outb(index, dev->io);
  86. return inb(dev->data);
  87. }
  88. static int pms_i2c_stat(struct pms *dev, u8 slave)
  89. {
  90. int counter = 0;
  91. int i;
  92. outb(0x28, dev->io);
  93. while ((inb(dev->data) & 0x01) == 0)
  94. if (counter++ == 256)
  95. break;
  96. while ((inb(dev->data) & 0x01) != 0)
  97. if (counter++ == 256)
  98. break;
  99. outb(slave, dev->io);
  100. counter = 0;
  101. while ((inb(dev->data) & 0x01) == 0)
  102. if (counter++ == 256)
  103. break;
  104. while ((inb(dev->data) & 0x01) != 0)
  105. if (counter++ == 256)
  106. break;
  107. for (i = 0; i < 12; i++) {
  108. char st = inb(dev->data);
  109. if ((st & 2) != 0)
  110. return -1;
  111. if ((st & 1) == 0)
  112. break;
  113. }
  114. outb(0x29, dev->io);
  115. return inb(dev->data);
  116. }
  117. static int pms_i2c_write(struct pms *dev, u16 slave, u16 sub, u16 data)
  118. {
  119. int skip = 0;
  120. int count;
  121. int i;
  122. for (i = 0; i < dev->i2c_count; i++) {
  123. if ((dev->i2cinfo[i].slave == slave) &&
  124. (dev->i2cinfo[i].sub == sub)) {
  125. if (dev->i2cinfo[i].data == data)
  126. skip = 1;
  127. dev->i2cinfo[i].data = data;
  128. i = dev->i2c_count + 1;
  129. }
  130. }
  131. if (i == dev->i2c_count && dev->i2c_count < 64) {
  132. dev->i2cinfo[dev->i2c_count].slave = slave;
  133. dev->i2cinfo[dev->i2c_count].sub = sub;
  134. dev->i2cinfo[dev->i2c_count].data = data;
  135. dev->i2c_count++;
  136. }
  137. if (skip)
  138. return 0;
  139. mvv_write(dev, 0x29, sub);
  140. mvv_write(dev, 0x2A, data);
  141. mvv_write(dev, 0x28, slave);
  142. outb(0x28, dev->io);
  143. count = 0;
  144. while ((inb(dev->data) & 1) == 0)
  145. if (count > 255)
  146. break;
  147. while ((inb(dev->data) & 1) != 0)
  148. if (count > 255)
  149. break;
  150. count = inb(dev->data);
  151. if (count & 2)
  152. return -1;
  153. return count;
  154. }
  155. static int pms_i2c_read(struct pms *dev, int slave, int sub)
  156. {
  157. int i;
  158. for (i = 0; i < dev->i2c_count; i++) {
  159. if (dev->i2cinfo[i].slave == slave && dev->i2cinfo[i].sub == sub)
  160. return dev->i2cinfo[i].data;
  161. }
  162. return 0;
  163. }
  164. static void pms_i2c_andor(struct pms *dev, int slave, int sub, int and, int or)
  165. {
  166. u8 tmp;
  167. tmp = pms_i2c_read(dev, slave, sub);
  168. tmp = (tmp & and) | or;
  169. pms_i2c_write(dev, slave, sub, tmp);
  170. }
  171. /*
  172. * Control functions
  173. */
  174. static void pms_videosource(struct pms *dev, short source)
  175. {
  176. switch (dev->decoder) {
  177. case MOTOROLA:
  178. break;
  179. case PHILIPS2:
  180. pms_i2c_andor(dev, 0x8a, 0x06, 0x7f, source ? 0x80 : 0);
  181. break;
  182. case PHILIPS1:
  183. break;
  184. }
  185. mvv_write(dev, 0x2E, 0x31);
  186. /* Was: mvv_write(dev, 0x2E, source ? 0x31 : 0x30);
  187. But could not make this work correctly. Only Composite input
  188. worked for me. */
  189. }
  190. static void pms_hue(struct pms *dev, short hue)
  191. {
  192. switch (dev->decoder) {
  193. case MOTOROLA:
  194. pms_i2c_write(dev, 0x8a, 0x00, hue);
  195. break;
  196. case PHILIPS2:
  197. pms_i2c_write(dev, 0x8a, 0x07, hue);
  198. break;
  199. case PHILIPS1:
  200. pms_i2c_write(dev, 0x42, 0x07, hue);
  201. break;
  202. }
  203. }
  204. static void pms_saturation(struct pms *dev, short sat)
  205. {
  206. switch (dev->decoder) {
  207. case MOTOROLA:
  208. pms_i2c_write(dev, 0x8a, 0x00, sat);
  209. break;
  210. case PHILIPS1:
  211. pms_i2c_write(dev, 0x42, 0x12, sat);
  212. break;
  213. }
  214. }
  215. static void pms_contrast(struct pms *dev, short contrast)
  216. {
  217. switch (dev->decoder) {
  218. case MOTOROLA:
  219. pms_i2c_write(dev, 0x8a, 0x00, contrast);
  220. break;
  221. case PHILIPS1:
  222. pms_i2c_write(dev, 0x42, 0x13, contrast);
  223. break;
  224. }
  225. }
  226. static void pms_brightness(struct pms *dev, short brightness)
  227. {
  228. switch (dev->decoder) {
  229. case MOTOROLA:
  230. pms_i2c_write(dev, 0x8a, 0x00, brightness);
  231. pms_i2c_write(dev, 0x8a, 0x00, brightness);
  232. pms_i2c_write(dev, 0x8a, 0x00, brightness);
  233. break;
  234. case PHILIPS1:
  235. pms_i2c_write(dev, 0x42, 0x19, brightness);
  236. break;
  237. }
  238. }
  239. static void pms_format(struct pms *dev, short format)
  240. {
  241. int target;
  242. dev->standard = format;
  243. if (dev->decoder == PHILIPS1)
  244. target = 0x42;
  245. else if (dev->decoder == PHILIPS2)
  246. target = 0x8a;
  247. else
  248. return;
  249. switch (format) {
  250. case 0: /* Auto */
  251. pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
  252. pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x80);
  253. break;
  254. case 1: /* NTSC */
  255. pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
  256. pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x40);
  257. break;
  258. case 2: /* PAL */
  259. pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x00);
  260. pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x00);
  261. break;
  262. case 3: /* SECAM */
  263. pms_i2c_andor(dev, target, 0x0d, 0xfe, 0x01);
  264. pms_i2c_andor(dev, target, 0x0f, 0x3f, 0x00);
  265. break;
  266. }
  267. }
  268. #ifdef FOR_FUTURE_EXPANSION
  269. /*
  270. * These features of the PMS card are not currently exposes. They
  271. * could become a private v4l ioctl for PMSCONFIG or somesuch if
  272. * people need it. We also don't yet use the PMS interrupt.
  273. */
  274. static void pms_hstart(struct pms *dev, short start)
  275. {
  276. switch (dev->decoder) {
  277. case PHILIPS1:
  278. pms_i2c_write(dev, 0x8a, 0x05, start);
  279. pms_i2c_write(dev, 0x8a, 0x18, start);
  280. break;
  281. case PHILIPS2:
  282. pms_i2c_write(dev, 0x42, 0x05, start);
  283. pms_i2c_write(dev, 0x42, 0x18, start);
  284. break;
  285. }
  286. }
  287. /*
  288. * Bandpass filters
  289. */
  290. static void pms_bandpass(struct pms *dev, short pass)
  291. {
  292. if (dev->decoder == PHILIPS2)
  293. pms_i2c_andor(dev, 0x8a, 0x06, 0xcf, (pass & 0x03) << 4);
  294. else if (dev->decoder == PHILIPS1)
  295. pms_i2c_andor(dev, 0x42, 0x06, 0xcf, (pass & 0x03) << 4);
  296. }
  297. static void pms_antisnow(struct pms *dev, short snow)
  298. {
  299. if (dev->decoder == PHILIPS2)
  300. pms_i2c_andor(dev, 0x8a, 0x06, 0xf3, (snow & 0x03) << 2);
  301. else if (dev->decoder == PHILIPS1)
  302. pms_i2c_andor(dev, 0x42, 0x06, 0xf3, (snow & 0x03) << 2);
  303. }
  304. static void pms_sharpness(struct pms *dev, short sharp)
  305. {
  306. if (dev->decoder == PHILIPS2)
  307. pms_i2c_andor(dev, 0x8a, 0x06, 0xfc, sharp & 0x03);
  308. else if (dev->decoder == PHILIPS1)
  309. pms_i2c_andor(dev, 0x42, 0x06, 0xfc, sharp & 0x03);
  310. }
  311. static void pms_chromaagc(struct pms *dev, short agc)
  312. {
  313. if (dev->decoder == PHILIPS2)
  314. pms_i2c_andor(dev, 0x8a, 0x0c, 0x9f, (agc & 0x03) << 5);
  315. else if (dev->decoder == PHILIPS1)
  316. pms_i2c_andor(dev, 0x42, 0x0c, 0x9f, (agc & 0x03) << 5);
  317. }
  318. static void pms_vertnoise(struct pms *dev, short noise)
  319. {
  320. if (dev->decoder == PHILIPS2)
  321. pms_i2c_andor(dev, 0x8a, 0x10, 0xfc, noise & 3);
  322. else if (dev->decoder == PHILIPS1)
  323. pms_i2c_andor(dev, 0x42, 0x10, 0xfc, noise & 3);
  324. }
  325. static void pms_forcecolour(struct pms *dev, short colour)
  326. {
  327. if (dev->decoder == PHILIPS2)
  328. pms_i2c_andor(dev, 0x8a, 0x0c, 0x7f, (colour & 1) << 7);
  329. else if (dev->decoder == PHILIPS1)
  330. pms_i2c_andor(dev, 0x42, 0x0c, 0x7, (colour & 1) << 7);
  331. }
  332. static void pms_antigamma(struct pms *dev, short gamma)
  333. {
  334. if (dev->decoder == PHILIPS2)
  335. pms_i2c_andor(dev, 0xb8, 0x00, 0x7f, (gamma & 1) << 7);
  336. else if (dev->decoder == PHILIPS1)
  337. pms_i2c_andor(dev, 0x42, 0x20, 0x7, (gamma & 1) << 7);
  338. }
  339. static void pms_prefilter(struct pms *dev, short filter)
  340. {
  341. if (dev->decoder == PHILIPS2)
  342. pms_i2c_andor(dev, 0x8a, 0x06, 0xbf, (filter & 1) << 6);
  343. else if (dev->decoder == PHILIPS1)
  344. pms_i2c_andor(dev, 0x42, 0x06, 0xbf, (filter & 1) << 6);
  345. }
  346. static void pms_hfilter(struct pms *dev, short filter)
  347. {
  348. if (dev->decoder == PHILIPS2)
  349. pms_i2c_andor(dev, 0xb8, 0x04, 0x1f, (filter & 7) << 5);
  350. else if (dev->decoder == PHILIPS1)
  351. pms_i2c_andor(dev, 0x42, 0x24, 0x1f, (filter & 7) << 5);
  352. }
  353. static void pms_vfilter(struct pms *dev, short filter)
  354. {
  355. if (dev->decoder == PHILIPS2)
  356. pms_i2c_andor(dev, 0xb8, 0x08, 0x9f, (filter & 3) << 5);
  357. else if (dev->decoder == PHILIPS1)
  358. pms_i2c_andor(dev, 0x42, 0x28, 0x9f, (filter & 3) << 5);
  359. }
  360. static void pms_killcolour(struct pms *dev, short colour)
  361. {
  362. if (dev->decoder == PHILIPS2) {
  363. pms_i2c_andor(dev, 0x8a, 0x08, 0x07, (colour & 0x1f) << 3);
  364. pms_i2c_andor(dev, 0x8a, 0x09, 0x07, (colour & 0x1f) << 3);
  365. } else if (dev->decoder == PHILIPS1) {
  366. pms_i2c_andor(dev, 0x42, 0x08, 0x07, (colour & 0x1f) << 3);
  367. pms_i2c_andor(dev, 0x42, 0x09, 0x07, (colour & 0x1f) << 3);
  368. }
  369. }
  370. static void pms_chromagain(struct pms *dev, short chroma)
  371. {
  372. if (dev->decoder == PHILIPS2)
  373. pms_i2c_write(dev, 0x8a, 0x11, chroma);
  374. else if (dev->decoder == PHILIPS1)
  375. pms_i2c_write(dev, 0x42, 0x11, chroma);
  376. }
  377. static void pms_spacialcompl(struct pms *dev, short data)
  378. {
  379. mvv_write(dev, 0x3b, data);
  380. }
  381. static void pms_spacialcomph(struct pms *dev, short data)
  382. {
  383. mvv_write(dev, 0x3a, data);
  384. }
  385. static void pms_vstart(struct pms *dev, short start)
  386. {
  387. mvv_write(dev, 0x16, start);
  388. mvv_write(dev, 0x17, (start >> 8) & 0x01);
  389. }
  390. #endif
  391. static void pms_secamcross(struct pms *dev, short cross)
  392. {
  393. if (dev->decoder == PHILIPS2)
  394. pms_i2c_andor(dev, 0x8a, 0x0f, 0xdf, (cross & 1) << 5);
  395. else if (dev->decoder == PHILIPS1)
  396. pms_i2c_andor(dev, 0x42, 0x0f, 0xdf, (cross & 1) << 5);
  397. }
  398. static void pms_swsense(struct pms *dev, short sense)
  399. {
  400. if (dev->decoder == PHILIPS2) {
  401. pms_i2c_write(dev, 0x8a, 0x0a, sense);
  402. pms_i2c_write(dev, 0x8a, 0x0b, sense);
  403. } else if (dev->decoder == PHILIPS1) {
  404. pms_i2c_write(dev, 0x42, 0x0a, sense);
  405. pms_i2c_write(dev, 0x42, 0x0b, sense);
  406. }
  407. }
  408. static void pms_framerate(struct pms *dev, short frr)
  409. {
  410. int fps = (dev->std & V4L2_STD_525_60) ? 30 : 25;
  411. if (frr == 0)
  412. return;
  413. fps = fps/frr;
  414. mvv_write(dev, 0x14, 0x80 | fps);
  415. mvv_write(dev, 0x15, 1);
  416. }
  417. static void pms_vert(struct pms *dev, u8 deciden, u8 decinum)
  418. {
  419. mvv_write(dev, 0x1c, deciden); /* Denominator */
  420. mvv_write(dev, 0x1d, decinum); /* Numerator */
  421. }
  422. /*
  423. * Turn 16bit ratios into best small ratio the chipset can grok
  424. */
  425. static void pms_vertdeci(struct pms *dev, unsigned short decinum, unsigned short deciden)
  426. {
  427. /* Knock it down by / 5 once */
  428. if (decinum % 5 == 0) {
  429. deciden /= 5;
  430. decinum /= 5;
  431. }
  432. /*
  433. * 3's
  434. */
  435. while (decinum % 3 == 0 && deciden % 3 == 0) {
  436. deciden /= 3;
  437. decinum /= 3;
  438. }
  439. /*
  440. * 2's
  441. */
  442. while (decinum % 2 == 0 && deciden % 2 == 0) {
  443. decinum /= 2;
  444. deciden /= 2;
  445. }
  446. /*
  447. * Fudgyify
  448. */
  449. while (deciden > 32) {
  450. deciden /= 2;
  451. decinum = (decinum + 1) / 2;
  452. }
  453. if (deciden == 32)
  454. deciden--;
  455. pms_vert(dev, deciden, decinum);
  456. }
  457. static void pms_horzdeci(struct pms *dev, short decinum, short deciden)
  458. {
  459. if (decinum <= 512) {
  460. if (decinum % 5 == 0) {
  461. decinum /= 5;
  462. deciden /= 5;
  463. }
  464. } else {
  465. decinum = 512;
  466. deciden = 640; /* 768 would be ideal */
  467. }
  468. while (((decinum | deciden) & 1) == 0) {
  469. decinum >>= 1;
  470. deciden >>= 1;
  471. }
  472. while (deciden > 32) {
  473. deciden >>= 1;
  474. decinum = (decinum + 1) >> 1;
  475. }
  476. if (deciden == 32)
  477. deciden--;
  478. mvv_write(dev, 0x24, 0x80 | deciden);
  479. mvv_write(dev, 0x25, decinum);
  480. }
  481. static void pms_resolution(struct pms *dev, short width, short height)
  482. {
  483. int fg_height;
  484. fg_height = height;
  485. if (fg_height > 280)
  486. fg_height = 280;
  487. mvv_write(dev, 0x18, fg_height);
  488. mvv_write(dev, 0x19, fg_height >> 8);
  489. if (dev->std & V4L2_STD_525_60) {
  490. mvv_write(dev, 0x1a, 0xfc);
  491. mvv_write(dev, 0x1b, 0x00);
  492. if (height > fg_height)
  493. pms_vertdeci(dev, 240, 240);
  494. else
  495. pms_vertdeci(dev, fg_height, 240);
  496. } else {
  497. mvv_write(dev, 0x1a, 0x1a);
  498. mvv_write(dev, 0x1b, 0x01);
  499. if (fg_height > 256)
  500. pms_vertdeci(dev, 270, 270);
  501. else
  502. pms_vertdeci(dev, fg_height, 270);
  503. }
  504. mvv_write(dev, 0x12, 0);
  505. mvv_write(dev, 0x13, MVVMEMORYWIDTH);
  506. mvv_write(dev, 0x42, 0x00);
  507. mvv_write(dev, 0x43, 0x00);
  508. mvv_write(dev, 0x44, MVVMEMORYWIDTH);
  509. mvv_write(dev, 0x22, width + 8);
  510. mvv_write(dev, 0x23, (width + 8) >> 8);
  511. if (dev->std & V4L2_STD_525_60)
  512. pms_horzdeci(dev, width, 640);
  513. else
  514. pms_horzdeci(dev, width + 8, 768);
  515. mvv_write(dev, 0x30, mvv_read(dev, 0x30) & 0xfe);
  516. mvv_write(dev, 0x08, mvv_read(dev, 0x08) | 0x01);
  517. mvv_write(dev, 0x01, mvv_read(dev, 0x01) & 0xfd);
  518. mvv_write(dev, 0x32, 0x00);
  519. mvv_write(dev, 0x33, MVVMEMORYWIDTH);
  520. }
  521. /*
  522. * Set Input
  523. */
  524. static void pms_vcrinput(struct pms *dev, short input)
  525. {
  526. if (dev->decoder == PHILIPS2)
  527. pms_i2c_andor(dev, 0x8a, 0x0d, 0x7f, (input & 1) << 7);
  528. else if (dev->decoder == PHILIPS1)
  529. pms_i2c_andor(dev, 0x42, 0x0d, 0x7f, (input & 1) << 7);
  530. }
  531. static int pms_capture(struct pms *dev, char __user *buf, int rgb555, int count)
  532. {
  533. int y;
  534. int dw = 2 * dev->width;
  535. char tmp[dw + 32]; /* using a temp buffer is faster than direct */
  536. int cnt = 0;
  537. int len = 0;
  538. unsigned char r8 = 0x5; /* value for reg8 */
  539. if (rgb555)
  540. r8 |= 0x20; /* else use untranslated rgb = 565 */
  541. mvv_write(dev, 0x08, r8); /* capture rgb555/565, init DRAM, PC enable */
  542. /* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
  543. for (y = 0; y < dev->height; y++) {
  544. writeb(0, dev->mem); /* synchronisiert neue Zeile */
  545. /*
  546. * This is in truth a fifo, be very careful as if you
  547. * forgot this odd things will occur 8)
  548. */
  549. memcpy_fromio(tmp, dev->mem, dw + 32); /* discard 16 word */
  550. cnt -= dev->height;
  551. while (cnt <= 0) {
  552. /*
  553. * Don't copy too far
  554. */
  555. int dt = dw;
  556. if (dt + len > count)
  557. dt = count - len;
  558. cnt += dev->height;
  559. if (copy_to_user(buf, tmp + 32, dt))
  560. return len ? len : -EFAULT;
  561. buf += dt;
  562. len += dt;
  563. }
  564. }
  565. return len;
  566. }
  567. /*
  568. * Video4linux interfacing
  569. */
  570. static int pms_querycap(struct file *file, void *priv,
  571. struct v4l2_capability *vcap)
  572. {
  573. struct pms *dev = video_drvdata(file);
  574. strlcpy(vcap->driver, dev->v4l2_dev.name, sizeof(vcap->driver));
  575. strlcpy(vcap->card, "Mediavision PMS", sizeof(vcap->card));
  576. strlcpy(vcap->bus_info, "ISA", sizeof(vcap->bus_info));
  577. vcap->version = KERNEL_VERSION(0, 0, 3);
  578. vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  579. return 0;
  580. }
  581. static int pms_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  582. {
  583. static const char *inputs[4] = {
  584. "Composite",
  585. "S-Video",
  586. "Composite (VCR)",
  587. "S-Video (VCR)"
  588. };
  589. if (vin->index > 3)
  590. return -EINVAL;
  591. strlcpy(vin->name, inputs[vin->index], sizeof(vin->name));
  592. vin->type = V4L2_INPUT_TYPE_CAMERA;
  593. vin->audioset = 0;
  594. vin->tuner = 0;
  595. vin->std = V4L2_STD_ALL;
  596. vin->status = 0;
  597. return 0;
  598. }
  599. static int pms_g_input(struct file *file, void *fh, unsigned int *inp)
  600. {
  601. struct pms *dev = video_drvdata(file);
  602. *inp = dev->input;
  603. return 0;
  604. }
  605. static int pms_s_input(struct file *file, void *fh, unsigned int inp)
  606. {
  607. struct pms *dev = video_drvdata(file);
  608. if (inp > 3)
  609. return -EINVAL;
  610. mutex_lock(&dev->lock);
  611. dev->input = inp;
  612. pms_videosource(dev, inp & 1);
  613. pms_vcrinput(dev, inp >> 1);
  614. mutex_unlock(&dev->lock);
  615. return 0;
  616. }
  617. static int pms_g_std(struct file *file, void *fh, v4l2_std_id *std)
  618. {
  619. struct pms *dev = video_drvdata(file);
  620. *std = dev->std;
  621. return 0;
  622. }
  623. static int pms_s_std(struct file *file, void *fh, v4l2_std_id *std)
  624. {
  625. struct pms *dev = video_drvdata(file);
  626. int ret = 0;
  627. dev->std = *std;
  628. mutex_lock(&dev->lock);
  629. if (dev->std & V4L2_STD_NTSC) {
  630. pms_framerate(dev, 30);
  631. pms_secamcross(dev, 0);
  632. pms_format(dev, 1);
  633. } else if (dev->std & V4L2_STD_PAL) {
  634. pms_framerate(dev, 25);
  635. pms_secamcross(dev, 0);
  636. pms_format(dev, 2);
  637. } else if (dev->std & V4L2_STD_SECAM) {
  638. pms_framerate(dev, 25);
  639. pms_secamcross(dev, 1);
  640. pms_format(dev, 2);
  641. } else {
  642. ret = -EINVAL;
  643. }
  644. /*
  645. switch (v->mode) {
  646. case VIDEO_MODE_AUTO:
  647. pms_framerate(dev, 25);
  648. pms_secamcross(dev, 0);
  649. pms_format(dev, 0);
  650. break;
  651. }*/
  652. mutex_unlock(&dev->lock);
  653. return 0;
  654. }
  655. static int pms_queryctrl(struct file *file, void *priv,
  656. struct v4l2_queryctrl *qc)
  657. {
  658. switch (qc->id) {
  659. case V4L2_CID_BRIGHTNESS:
  660. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 139);
  661. case V4L2_CID_CONTRAST:
  662. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 70);
  663. case V4L2_CID_SATURATION:
  664. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 64);
  665. case V4L2_CID_HUE:
  666. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 0);
  667. }
  668. return -EINVAL;
  669. }
  670. static int pms_g_ctrl(struct file *file, void *priv,
  671. struct v4l2_control *ctrl)
  672. {
  673. struct pms *dev = video_drvdata(file);
  674. int ret = 0;
  675. switch (ctrl->id) {
  676. case V4L2_CID_BRIGHTNESS:
  677. ctrl->value = dev->brightness;
  678. break;
  679. case V4L2_CID_CONTRAST:
  680. ctrl->value = dev->contrast;
  681. break;
  682. case V4L2_CID_SATURATION:
  683. ctrl->value = dev->saturation;
  684. break;
  685. case V4L2_CID_HUE:
  686. ctrl->value = dev->hue;
  687. break;
  688. default:
  689. ret = -EINVAL;
  690. break;
  691. }
  692. return ret;
  693. }
  694. static int pms_s_ctrl(struct file *file, void *priv,
  695. struct v4l2_control *ctrl)
  696. {
  697. struct pms *dev = video_drvdata(file);
  698. int ret = 0;
  699. mutex_lock(&dev->lock);
  700. switch (ctrl->id) {
  701. case V4L2_CID_BRIGHTNESS:
  702. dev->brightness = ctrl->value;
  703. pms_brightness(dev, dev->brightness);
  704. break;
  705. case V4L2_CID_CONTRAST:
  706. dev->contrast = ctrl->value;
  707. pms_contrast(dev, dev->contrast);
  708. break;
  709. case V4L2_CID_SATURATION:
  710. dev->saturation = ctrl->value;
  711. pms_saturation(dev, dev->saturation);
  712. break;
  713. case V4L2_CID_HUE:
  714. dev->hue = ctrl->value;
  715. pms_hue(dev, dev->hue);
  716. break;
  717. default:
  718. ret = -EINVAL;
  719. break;
  720. }
  721. mutex_unlock(&dev->lock);
  722. return ret;
  723. }
  724. static int pms_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  725. {
  726. struct pms *dev = video_drvdata(file);
  727. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  728. pix->width = dev->width;
  729. pix->height = dev->height;
  730. pix->pixelformat = dev->width == 15 ?
  731. V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB565;
  732. pix->field = V4L2_FIELD_NONE;
  733. pix->bytesperline = 2 * dev->width;
  734. pix->sizeimage = 2 * dev->width * dev->height;
  735. /* Just a guess */
  736. pix->colorspace = V4L2_COLORSPACE_SRGB;
  737. return 0;
  738. }
  739. static int pms_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  740. {
  741. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  742. if (pix->height < 16 || pix->height > 480)
  743. return -EINVAL;
  744. if (pix->width < 16 || pix->width > 640)
  745. return -EINVAL;
  746. if (pix->pixelformat != V4L2_PIX_FMT_RGB555 &&
  747. pix->pixelformat != V4L2_PIX_FMT_RGB565)
  748. return -EINVAL;
  749. pix->field = V4L2_FIELD_NONE;
  750. pix->bytesperline = 2 * pix->width;
  751. pix->sizeimage = 2 * pix->width * pix->height;
  752. /* Just a guess */
  753. pix->colorspace = V4L2_COLORSPACE_SRGB;
  754. return 0;
  755. }
  756. static int pms_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  757. {
  758. struct pms *dev = video_drvdata(file);
  759. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  760. int ret = pms_try_fmt_vid_cap(file, fh, fmt);
  761. if (ret)
  762. return ret;
  763. mutex_lock(&dev->lock);
  764. dev->width = pix->width;
  765. dev->height = pix->height;
  766. dev->depth = (pix->pixelformat == V4L2_PIX_FMT_RGB555) ? 15 : 16;
  767. pms_resolution(dev, dev->width, dev->height);
  768. /* Ok we figured out what to use from our wide choice */
  769. mutex_unlock(&dev->lock);
  770. return 0;
  771. }
  772. static int pms_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  773. {
  774. static struct v4l2_fmtdesc formats[] = {
  775. { 0, 0, 0,
  776. "RGB 5:5:5", V4L2_PIX_FMT_RGB555,
  777. { 0, 0, 0, 0 }
  778. },
  779. { 0, 0, 0,
  780. "RGB 5:6:5", V4L2_PIX_FMT_RGB565,
  781. { 0, 0, 0, 0 }
  782. },
  783. };
  784. enum v4l2_buf_type type = fmt->type;
  785. if (fmt->index > 1)
  786. return -EINVAL;
  787. *fmt = formats[fmt->index];
  788. fmt->type = type;
  789. return 0;
  790. }
  791. static ssize_t pms_read(struct file *file, char __user *buf,
  792. size_t count, loff_t *ppos)
  793. {
  794. struct pms *dev = video_drvdata(file);
  795. int len;
  796. mutex_lock(&dev->lock);
  797. len = pms_capture(dev, buf, (dev->depth == 15), count);
  798. mutex_unlock(&dev->lock);
  799. return len;
  800. }
  801. static int pms_exclusive_open(struct file *file)
  802. {
  803. struct pms *dev = video_drvdata(file);
  804. return test_and_set_bit(0, &dev->in_use) ? -EBUSY : 0;
  805. }
  806. static int pms_exclusive_release(struct file *file)
  807. {
  808. struct pms *dev = video_drvdata(file);
  809. clear_bit(0, &dev->in_use);
  810. return 0;
  811. }
  812. static const struct v4l2_file_operations pms_fops = {
  813. .owner = THIS_MODULE,
  814. .open = pms_exclusive_open,
  815. .release = pms_exclusive_release,
  816. .ioctl = video_ioctl2,
  817. .read = pms_read,
  818. };
  819. static const struct v4l2_ioctl_ops pms_ioctl_ops = {
  820. .vidioc_querycap = pms_querycap,
  821. .vidioc_g_input = pms_g_input,
  822. .vidioc_s_input = pms_s_input,
  823. .vidioc_enum_input = pms_enum_input,
  824. .vidioc_g_std = pms_g_std,
  825. .vidioc_s_std = pms_s_std,
  826. .vidioc_queryctrl = pms_queryctrl,
  827. .vidioc_g_ctrl = pms_g_ctrl,
  828. .vidioc_s_ctrl = pms_s_ctrl,
  829. .vidioc_enum_fmt_vid_cap = pms_enum_fmt_vid_cap,
  830. .vidioc_g_fmt_vid_cap = pms_g_fmt_vid_cap,
  831. .vidioc_s_fmt_vid_cap = pms_s_fmt_vid_cap,
  832. .vidioc_try_fmt_vid_cap = pms_try_fmt_vid_cap,
  833. };
  834. /*
  835. * Probe for and initialise the Mediavision PMS
  836. */
  837. static int init_mediavision(struct pms *dev)
  838. {
  839. int id;
  840. int idec, decst;
  841. int i;
  842. static const unsigned char i2c_defs[] = {
  843. 0x4c, 0x30, 0x00, 0xe8,
  844. 0xb6, 0xe2, 0x00, 0x00,
  845. 0xff, 0xff, 0x00, 0x00,
  846. 0x00, 0x00, 0x78, 0x98,
  847. 0x00, 0x00, 0x00, 0x00,
  848. 0x34, 0x0a, 0xf4, 0xce,
  849. 0xe4
  850. };
  851. dev->mem = ioremap(mem_base, 0x800);
  852. if (!dev->mem)
  853. return -ENOMEM;
  854. if (!request_region(0x9a01, 1, "Mediavision PMS config")) {
  855. printk(KERN_WARNING "mediavision: unable to detect: 0x9a01 in use.\n");
  856. iounmap(dev->mem);
  857. return -EBUSY;
  858. }
  859. if (!request_region(dev->io, 3, "Mediavision PMS")) {
  860. printk(KERN_WARNING "mediavision: I/O port %d in use.\n", dev->io);
  861. release_region(0x9a01, 1);
  862. iounmap(dev->mem);
  863. return -EBUSY;
  864. }
  865. outb(0xb8, 0x9a01); /* Unlock */
  866. outb(dev->io >> 4, 0x9a01); /* Set IO port */
  867. id = mvv_read(dev, 3);
  868. decst = pms_i2c_stat(dev, 0x43);
  869. if (decst != -1)
  870. idec = 2;
  871. else if (pms_i2c_stat(dev, 0xb9) != -1)
  872. idec = 3;
  873. else if (pms_i2c_stat(dev, 0x8b) != -1)
  874. idec = 1;
  875. else
  876. idec = 0;
  877. printk(KERN_INFO "PMS type is %d\n", idec);
  878. if (idec == 0) {
  879. release_region(dev->io, 3);
  880. release_region(0x9a01, 1);
  881. iounmap(dev->mem);
  882. return -ENODEV;
  883. }
  884. /*
  885. * Ok we have a PMS of some sort
  886. */
  887. mvv_write(dev, 0x04, mem_base >> 12); /* Set the memory area */
  888. /* Ok now load the defaults */
  889. for (i = 0; i < 0x19; i++) {
  890. if (i2c_defs[i] == 0xff)
  891. pms_i2c_andor(dev, 0x8a, i, 0x07, 0x00);
  892. else
  893. pms_i2c_write(dev, 0x8a, i, i2c_defs[i]);
  894. }
  895. pms_i2c_write(dev, 0xb8, 0x00, 0x12);
  896. pms_i2c_write(dev, 0xb8, 0x04, 0x00);
  897. pms_i2c_write(dev, 0xb8, 0x07, 0x00);
  898. pms_i2c_write(dev, 0xb8, 0x08, 0x00);
  899. pms_i2c_write(dev, 0xb8, 0x09, 0xff);
  900. pms_i2c_write(dev, 0xb8, 0x0a, 0x00);
  901. pms_i2c_write(dev, 0xb8, 0x0b, 0x10);
  902. pms_i2c_write(dev, 0xb8, 0x10, 0x03);
  903. mvv_write(dev, 0x01, 0x00);
  904. mvv_write(dev, 0x05, 0xa0);
  905. mvv_write(dev, 0x08, 0x25);
  906. mvv_write(dev, 0x09, 0x00);
  907. mvv_write(dev, 0x0a, 0x20 | MVVMEMORYWIDTH);
  908. mvv_write(dev, 0x10, 0x02);
  909. mvv_write(dev, 0x1e, 0x0c);
  910. mvv_write(dev, 0x1f, 0x03);
  911. mvv_write(dev, 0x26, 0x06);
  912. mvv_write(dev, 0x2b, 0x00);
  913. mvv_write(dev, 0x2c, 0x20);
  914. mvv_write(dev, 0x2d, 0x00);
  915. mvv_write(dev, 0x2f, 0x70);
  916. mvv_write(dev, 0x32, 0x00);
  917. mvv_write(dev, 0x33, MVVMEMORYWIDTH);
  918. mvv_write(dev, 0x34, 0x00);
  919. mvv_write(dev, 0x35, 0x00);
  920. mvv_write(dev, 0x3a, 0x80);
  921. mvv_write(dev, 0x3b, 0x10);
  922. mvv_write(dev, 0x20, 0x00);
  923. mvv_write(dev, 0x21, 0x00);
  924. mvv_write(dev, 0x30, 0x22);
  925. return 0;
  926. }
  927. /*
  928. * Initialization and module stuff
  929. */
  930. #ifndef MODULE
  931. static int enable;
  932. module_param(enable, int, 0);
  933. #endif
  934. static int __init pms_init(void)
  935. {
  936. struct pms *dev = &pms_card;
  937. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  938. int res;
  939. strlcpy(v4l2_dev->name, "pms", sizeof(v4l2_dev->name));
  940. v4l2_info(v4l2_dev, "Mediavision Pro Movie Studio driver 0.03\n");
  941. #ifndef MODULE
  942. if (!enable) {
  943. v4l2_err(v4l2_dev,
  944. "PMS: not enabled, use pms.enable=1 to probe\n");
  945. return -ENODEV;
  946. }
  947. #endif
  948. dev->decoder = PHILIPS2;
  949. dev->io = io_port;
  950. dev->data = io_port + 1;
  951. if (init_mediavision(dev)) {
  952. v4l2_err(v4l2_dev, "Board not found.\n");
  953. return -ENODEV;
  954. }
  955. res = v4l2_device_register(NULL, v4l2_dev);
  956. if (res < 0) {
  957. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  958. return res;
  959. }
  960. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  961. dev->vdev.v4l2_dev = v4l2_dev;
  962. dev->vdev.fops = &pms_fops;
  963. dev->vdev.ioctl_ops = &pms_ioctl_ops;
  964. dev->vdev.release = video_device_release_empty;
  965. video_set_drvdata(&dev->vdev, dev);
  966. mutex_init(&dev->lock);
  967. dev->std = V4L2_STD_NTSC_M;
  968. dev->height = 240;
  969. dev->width = 320;
  970. dev->depth = 15;
  971. dev->brightness = 139;
  972. dev->contrast = 70;
  973. dev->hue = 0;
  974. dev->saturation = 64;
  975. pms_swsense(dev, 75);
  976. pms_resolution(dev, 320, 240);
  977. pms_videosource(dev, 0);
  978. pms_vcrinput(dev, 0);
  979. if (video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  980. v4l2_device_unregister(&dev->v4l2_dev);
  981. release_region(dev->io, 3);
  982. release_region(0x9a01, 1);
  983. iounmap(dev->mem);
  984. return -EINVAL;
  985. }
  986. return 0;
  987. }
  988. static void __exit pms_exit(void)
  989. {
  990. struct pms *dev = &pms_card;
  991. video_unregister_device(&dev->vdev);
  992. release_region(dev->io, 3);
  993. release_region(0x9a01, 1);
  994. iounmap(dev->mem);
  995. }
  996. module_init(pms_init);
  997. module_exit(pms_exit);