pms.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  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[dw + 32]; /* 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. if (rgb555)
  542. r8 |= 0x20; /* else use untranslated rgb = 565 */
  543. mvv_write(dev, 0x08, r8); /* capture rgb555/565, init DRAM, PC enable */
  544. /* printf("%d %d %d %d %d %x %x\n",width,height,voff,nom,den,mvv_buf); */
  545. for (y = 0; y < dev->height; y++) {
  546. writeb(0, dev->mem); /* synchronisiert neue Zeile */
  547. /*
  548. * This is in truth a fifo, be very careful as if you
  549. * forgot this odd things will occur 8)
  550. */
  551. memcpy_fromio(tmp, dev->mem, dw + 32); /* discard 16 word */
  552. cnt -= dev->height;
  553. while (cnt <= 0) {
  554. /*
  555. * Don't copy too far
  556. */
  557. int dt = dw;
  558. if (dt + len > count)
  559. dt = count - len;
  560. cnt += dev->height;
  561. if (copy_to_user(buf, tmp + 32, dt))
  562. return len ? len : -EFAULT;
  563. buf += dt;
  564. len += dt;
  565. }
  566. }
  567. return len;
  568. }
  569. /*
  570. * Video4linux interfacing
  571. */
  572. static int pms_querycap(struct file *file, void *priv,
  573. struct v4l2_capability *vcap)
  574. {
  575. struct pms *dev = video_drvdata(file);
  576. strlcpy(vcap->driver, dev->v4l2_dev.name, sizeof(vcap->driver));
  577. strlcpy(vcap->card, "Mediavision PMS", sizeof(vcap->card));
  578. snprintf(vcap->bus_info, sizeof(vcap->bus_info),
  579. "ISA:%s", dev->v4l2_dev.name);
  580. vcap->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  581. vcap->capabilities = vcap->device_caps | V4L2_CAP_DEVICE_CAPS;
  582. return 0;
  583. }
  584. static int pms_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  585. {
  586. static const char *inputs[4] = {
  587. "Composite",
  588. "S-Video",
  589. "Composite (VCR)",
  590. "S-Video (VCR)"
  591. };
  592. if (vin->index > 3)
  593. return -EINVAL;
  594. strlcpy(vin->name, inputs[vin->index], sizeof(vin->name));
  595. vin->type = V4L2_INPUT_TYPE_CAMERA;
  596. vin->audioset = 0;
  597. vin->tuner = 0;
  598. vin->std = V4L2_STD_ALL;
  599. vin->status = 0;
  600. return 0;
  601. }
  602. static int pms_g_input(struct file *file, void *fh, unsigned int *inp)
  603. {
  604. struct pms *dev = video_drvdata(file);
  605. *inp = dev->input;
  606. return 0;
  607. }
  608. static int pms_s_input(struct file *file, void *fh, unsigned int inp)
  609. {
  610. struct pms *dev = video_drvdata(file);
  611. if (inp > 3)
  612. return -EINVAL;
  613. dev->input = inp;
  614. pms_videosource(dev, inp & 1);
  615. pms_vcrinput(dev, inp >> 1);
  616. return 0;
  617. }
  618. static int pms_g_std(struct file *file, void *fh, v4l2_std_id *std)
  619. {
  620. struct pms *dev = video_drvdata(file);
  621. *std = dev->std;
  622. return 0;
  623. }
  624. static int pms_s_std(struct file *file, void *fh, v4l2_std_id *std)
  625. {
  626. struct pms *dev = video_drvdata(file);
  627. int ret = 0;
  628. dev->std = *std;
  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. return ret;
  653. }
  654. static int pms_s_ctrl(struct v4l2_ctrl *ctrl)
  655. {
  656. struct pms *dev = container_of(ctrl->handler, struct pms, hdl);
  657. int ret = 0;
  658. switch (ctrl->id) {
  659. case V4L2_CID_BRIGHTNESS:
  660. pms_brightness(dev, ctrl->val);
  661. break;
  662. case V4L2_CID_CONTRAST:
  663. pms_contrast(dev, ctrl->val);
  664. break;
  665. case V4L2_CID_SATURATION:
  666. pms_saturation(dev, ctrl->val);
  667. break;
  668. case V4L2_CID_HUE:
  669. pms_hue(dev, ctrl->val);
  670. break;
  671. default:
  672. ret = -EINVAL;
  673. break;
  674. }
  675. return ret;
  676. }
  677. static int pms_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  678. {
  679. struct pms *dev = video_drvdata(file);
  680. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  681. pix->width = dev->width;
  682. pix->height = dev->height;
  683. pix->pixelformat = dev->width == 15 ?
  684. V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB565;
  685. pix->field = V4L2_FIELD_NONE;
  686. pix->bytesperline = 2 * dev->width;
  687. pix->sizeimage = 2 * dev->width * dev->height;
  688. /* Just a guess */
  689. pix->colorspace = V4L2_COLORSPACE_SRGB;
  690. return 0;
  691. }
  692. static int pms_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  693. {
  694. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  695. if (pix->height < 16 || pix->height > 480)
  696. return -EINVAL;
  697. if (pix->width < 16 || pix->width > 640)
  698. return -EINVAL;
  699. if (pix->pixelformat != V4L2_PIX_FMT_RGB555 &&
  700. pix->pixelformat != V4L2_PIX_FMT_RGB565)
  701. return -EINVAL;
  702. pix->field = V4L2_FIELD_NONE;
  703. pix->bytesperline = 2 * pix->width;
  704. pix->sizeimage = 2 * pix->width * pix->height;
  705. /* Just a guess */
  706. pix->colorspace = V4L2_COLORSPACE_SRGB;
  707. return 0;
  708. }
  709. static int pms_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  710. {
  711. struct pms *dev = video_drvdata(file);
  712. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  713. int ret = pms_try_fmt_vid_cap(file, fh, fmt);
  714. if (ret)
  715. return ret;
  716. dev->width = pix->width;
  717. dev->height = pix->height;
  718. dev->depth = (pix->pixelformat == V4L2_PIX_FMT_RGB555) ? 15 : 16;
  719. pms_resolution(dev, dev->width, dev->height);
  720. /* Ok we figured out what to use from our wide choice */
  721. return 0;
  722. }
  723. static int pms_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  724. {
  725. static struct v4l2_fmtdesc formats[] = {
  726. { 0, 0, 0,
  727. "RGB 5:5:5", V4L2_PIX_FMT_RGB555,
  728. { 0, 0, 0, 0 }
  729. },
  730. { 1, 0, 0,
  731. "RGB 5:6:5", V4L2_PIX_FMT_RGB565,
  732. { 0, 0, 0, 0 }
  733. },
  734. };
  735. enum v4l2_buf_type type = fmt->type;
  736. if (fmt->index > 1)
  737. return -EINVAL;
  738. *fmt = formats[fmt->index];
  739. fmt->type = type;
  740. return 0;
  741. }
  742. static ssize_t pms_read(struct file *file, char __user *buf,
  743. size_t count, loff_t *ppos)
  744. {
  745. struct pms *dev = video_drvdata(file);
  746. int len;
  747. len = pms_capture(dev, buf, (dev->depth == 15), count);
  748. return len;
  749. }
  750. static unsigned int pms_poll(struct file *file, struct poll_table_struct *wait)
  751. {
  752. struct v4l2_fh *fh = file->private_data;
  753. unsigned int res = POLLIN | POLLRDNORM;
  754. if (v4l2_event_pending(fh))
  755. res |= POLLPRI;
  756. poll_wait(file, &fh->wait, wait);
  757. return res;
  758. }
  759. static const struct v4l2_file_operations pms_fops = {
  760. .owner = THIS_MODULE,
  761. .open = v4l2_fh_open,
  762. .release = v4l2_fh_release,
  763. .poll = pms_poll,
  764. .unlocked_ioctl = video_ioctl2,
  765. .read = pms_read,
  766. };
  767. static const struct v4l2_ioctl_ops pms_ioctl_ops = {
  768. .vidioc_querycap = pms_querycap,
  769. .vidioc_g_input = pms_g_input,
  770. .vidioc_s_input = pms_s_input,
  771. .vidioc_enum_input = pms_enum_input,
  772. .vidioc_g_std = pms_g_std,
  773. .vidioc_s_std = pms_s_std,
  774. .vidioc_enum_fmt_vid_cap = pms_enum_fmt_vid_cap,
  775. .vidioc_g_fmt_vid_cap = pms_g_fmt_vid_cap,
  776. .vidioc_s_fmt_vid_cap = pms_s_fmt_vid_cap,
  777. .vidioc_try_fmt_vid_cap = pms_try_fmt_vid_cap,
  778. .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
  779. .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
  780. };
  781. /*
  782. * Probe for and initialise the Mediavision PMS
  783. */
  784. static int init_mediavision(struct pms *dev)
  785. {
  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. decst = pms_i2c_stat(dev, 0x43);
  814. if (decst != -1)
  815. idec = 2;
  816. else if (pms_i2c_stat(dev, 0xb9) != -1)
  817. idec = 3;
  818. else if (pms_i2c_stat(dev, 0x8b) != -1)
  819. idec = 1;
  820. else
  821. idec = 0;
  822. printk(KERN_INFO "PMS type is %d\n", idec);
  823. if (idec == 0) {
  824. release_region(dev->io, 3);
  825. release_region(0x9a01, 1);
  826. iounmap(dev->mem);
  827. return -ENODEV;
  828. }
  829. /*
  830. * Ok we have a PMS of some sort
  831. */
  832. mvv_write(dev, 0x04, mem_base >> 12); /* Set the memory area */
  833. /* Ok now load the defaults */
  834. for (i = 0; i < 0x19; i++) {
  835. if (i2c_defs[i] == 0xff)
  836. pms_i2c_andor(dev, 0x8a, i, 0x07, 0x00);
  837. else
  838. pms_i2c_write(dev, 0x8a, i, i2c_defs[i]);
  839. }
  840. pms_i2c_write(dev, 0xb8, 0x00, 0x12);
  841. pms_i2c_write(dev, 0xb8, 0x04, 0x00);
  842. pms_i2c_write(dev, 0xb8, 0x07, 0x00);
  843. pms_i2c_write(dev, 0xb8, 0x08, 0x00);
  844. pms_i2c_write(dev, 0xb8, 0x09, 0xff);
  845. pms_i2c_write(dev, 0xb8, 0x0a, 0x00);
  846. pms_i2c_write(dev, 0xb8, 0x0b, 0x10);
  847. pms_i2c_write(dev, 0xb8, 0x10, 0x03);
  848. mvv_write(dev, 0x01, 0x00);
  849. mvv_write(dev, 0x05, 0xa0);
  850. mvv_write(dev, 0x08, 0x25);
  851. mvv_write(dev, 0x09, 0x00);
  852. mvv_write(dev, 0x0a, 0x20 | MVVMEMORYWIDTH);
  853. mvv_write(dev, 0x10, 0x02);
  854. mvv_write(dev, 0x1e, 0x0c);
  855. mvv_write(dev, 0x1f, 0x03);
  856. mvv_write(dev, 0x26, 0x06);
  857. mvv_write(dev, 0x2b, 0x00);
  858. mvv_write(dev, 0x2c, 0x20);
  859. mvv_write(dev, 0x2d, 0x00);
  860. mvv_write(dev, 0x2f, 0x70);
  861. mvv_write(dev, 0x32, 0x00);
  862. mvv_write(dev, 0x33, MVVMEMORYWIDTH);
  863. mvv_write(dev, 0x34, 0x00);
  864. mvv_write(dev, 0x35, 0x00);
  865. mvv_write(dev, 0x3a, 0x80);
  866. mvv_write(dev, 0x3b, 0x10);
  867. mvv_write(dev, 0x20, 0x00);
  868. mvv_write(dev, 0x21, 0x00);
  869. mvv_write(dev, 0x30, 0x22);
  870. return 0;
  871. }
  872. /*
  873. * Initialization and module stuff
  874. */
  875. #ifndef MODULE
  876. static int enable;
  877. module_param(enable, int, 0);
  878. #endif
  879. static const struct v4l2_ctrl_ops pms_ctrl_ops = {
  880. .s_ctrl = pms_s_ctrl,
  881. };
  882. static int pms_probe(struct device *pdev, unsigned int card)
  883. {
  884. struct pms *dev;
  885. struct v4l2_device *v4l2_dev;
  886. struct v4l2_ctrl_handler *hdl;
  887. int res;
  888. #ifndef MODULE
  889. if (!enable) {
  890. pr_err("PMS: not enabled, use pms.enable=1 to probe\n");
  891. return -ENODEV;
  892. }
  893. #endif
  894. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  895. if (dev == NULL)
  896. return -ENOMEM;
  897. dev->decoder = PHILIPS2;
  898. dev->io = io_port;
  899. dev->data = io_port + 1;
  900. v4l2_dev = &dev->v4l2_dev;
  901. hdl = &dev->hdl;
  902. res = v4l2_device_register(pdev, v4l2_dev);
  903. if (res < 0) {
  904. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  905. goto free_dev;
  906. }
  907. v4l2_info(v4l2_dev, "Mediavision Pro Movie Studio driver 0.05\n");
  908. res = init_mediavision(dev);
  909. if (res) {
  910. v4l2_err(v4l2_dev, "Board not found.\n");
  911. goto free_io;
  912. }
  913. v4l2_ctrl_handler_init(hdl, 4);
  914. v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
  915. V4L2_CID_BRIGHTNESS, 0, 255, 1, 139);
  916. v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
  917. V4L2_CID_CONTRAST, 0, 255, 1, 70);
  918. v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
  919. V4L2_CID_SATURATION, 0, 255, 1, 64);
  920. v4l2_ctrl_new_std(hdl, &pms_ctrl_ops,
  921. V4L2_CID_HUE, 0, 255, 1, 0);
  922. if (hdl->error) {
  923. res = hdl->error;
  924. goto free_hdl;
  925. }
  926. mutex_init(&dev->lock);
  927. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  928. dev->vdev.v4l2_dev = v4l2_dev;
  929. dev->vdev.ctrl_handler = hdl;
  930. dev->vdev.fops = &pms_fops;
  931. dev->vdev.ioctl_ops = &pms_ioctl_ops;
  932. dev->vdev.release = video_device_release_empty;
  933. dev->vdev.lock = &dev->lock;
  934. dev->vdev.tvnorms = V4L2_STD_NTSC | V4L2_STD_PAL | V4L2_STD_SECAM;
  935. set_bit(V4L2_FL_USE_FH_PRIO, &dev->vdev.flags);
  936. video_set_drvdata(&dev->vdev, dev);
  937. dev->std = V4L2_STD_NTSC_M;
  938. dev->height = 240;
  939. dev->width = 320;
  940. dev->depth = 16;
  941. pms_swsense(dev, 75);
  942. pms_resolution(dev, 320, 240);
  943. pms_videosource(dev, 0);
  944. pms_vcrinput(dev, 0);
  945. v4l2_ctrl_handler_setup(hdl);
  946. res = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr);
  947. if (res >= 0)
  948. return 0;
  949. free_hdl:
  950. v4l2_ctrl_handler_free(hdl);
  951. v4l2_device_unregister(&dev->v4l2_dev);
  952. free_io:
  953. release_region(dev->io, 3);
  954. release_region(0x9a01, 1);
  955. iounmap(dev->mem);
  956. free_dev:
  957. kfree(dev);
  958. return res;
  959. }
  960. static int pms_remove(struct device *pdev, unsigned int card)
  961. {
  962. struct pms *dev = dev_get_drvdata(pdev);
  963. video_unregister_device(&dev->vdev);
  964. v4l2_ctrl_handler_free(&dev->hdl);
  965. release_region(dev->io, 3);
  966. release_region(0x9a01, 1);
  967. iounmap(dev->mem);
  968. return 0;
  969. }
  970. static struct isa_driver pms_driver = {
  971. .probe = pms_probe,
  972. .remove = pms_remove,
  973. .driver = {
  974. .name = "pms",
  975. },
  976. };
  977. static int __init pms_init(void)
  978. {
  979. return isa_register_driver(&pms_driver, 1);
  980. }
  981. static void __exit pms_exit(void)
  982. {
  983. isa_unregister_driver(&pms_driver);
  984. }
  985. module_init(pms_init);
  986. module_exit(pms_exit);