pms.c 26 KB

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