pms.c 25 KB

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