pms.c 26 KB

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