pms.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145
  1. /*
  2. * Media Vision Pro Movie Studio
  3. * or
  4. * "all you need is an I2C bus some RAM and a prayer"
  5. *
  6. * This draws heavily on code
  7. *
  8. * (c) Wolfgang Koehler, wolf@first.gmd.de, Dec. 1994
  9. * Kiefernring 15
  10. * 14478 Potsdam, Germany
  11. *
  12. * Most of this code is directly derived from his userspace driver.
  13. * His driver works so send any reports to alan@lxorguk.ukuu.org.uk
  14. * unless the userspace driver also doesn't work for you...
  15. *
  16. * Changes:
  17. * 25-11-2009 Hans Verkuil <hverkuil@xs4all.nl>
  18. * - converted to version 2 of the V4L API.
  19. * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
  20. * - pms_capture: report back -EFAULT
  21. */
  22. #include <linux/module.h>
  23. #include <linux/delay.h>
  24. #include <linux/errno.h>
  25. #include <linux/fs.h>
  26. #include <linux/kernel.h>
  27. #include <linux/mm.h>
  28. #include <linux/ioport.h>
  29. #include <linux/init.h>
  30. #include <linux/mutex.h>
  31. #include <linux/uaccess.h>
  32. #include <asm/io.h>
  33. #include <linux/videodev2.h>
  34. #include <media/v4l2-common.h>
  35. #include <media/v4l2-ioctl.h>
  36. #include <media/v4l2-device.h>
  37. MODULE_LICENSE("GPL");
  38. MODULE_VERSION("0.0.4");
  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->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  576. return 0;
  577. }
  578. static int pms_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  579. {
  580. static const char *inputs[4] = {
  581. "Composite",
  582. "S-Video",
  583. "Composite (VCR)",
  584. "S-Video (VCR)"
  585. };
  586. if (vin->index > 3)
  587. return -EINVAL;
  588. strlcpy(vin->name, inputs[vin->index], sizeof(vin->name));
  589. vin->type = V4L2_INPUT_TYPE_CAMERA;
  590. vin->audioset = 0;
  591. vin->tuner = 0;
  592. vin->std = V4L2_STD_ALL;
  593. vin->status = 0;
  594. return 0;
  595. }
  596. static int pms_g_input(struct file *file, void *fh, unsigned int *inp)
  597. {
  598. struct pms *dev = video_drvdata(file);
  599. *inp = dev->input;
  600. return 0;
  601. }
  602. static int pms_s_input(struct file *file, void *fh, unsigned int inp)
  603. {
  604. struct pms *dev = video_drvdata(file);
  605. if (inp > 3)
  606. return -EINVAL;
  607. mutex_lock(&dev->lock);
  608. dev->input = inp;
  609. pms_videosource(dev, inp & 1);
  610. pms_vcrinput(dev, inp >> 1);
  611. mutex_unlock(&dev->lock);
  612. return 0;
  613. }
  614. static int pms_g_std(struct file *file, void *fh, v4l2_std_id *std)
  615. {
  616. struct pms *dev = video_drvdata(file);
  617. *std = dev->std;
  618. return 0;
  619. }
  620. static int pms_s_std(struct file *file, void *fh, v4l2_std_id *std)
  621. {
  622. struct pms *dev = video_drvdata(file);
  623. int ret = 0;
  624. dev->std = *std;
  625. mutex_lock(&dev->lock);
  626. if (dev->std & V4L2_STD_NTSC) {
  627. pms_framerate(dev, 30);
  628. pms_secamcross(dev, 0);
  629. pms_format(dev, 1);
  630. } else if (dev->std & V4L2_STD_PAL) {
  631. pms_framerate(dev, 25);
  632. pms_secamcross(dev, 0);
  633. pms_format(dev, 2);
  634. } else if (dev->std & V4L2_STD_SECAM) {
  635. pms_framerate(dev, 25);
  636. pms_secamcross(dev, 1);
  637. pms_format(dev, 2);
  638. } else {
  639. ret = -EINVAL;
  640. }
  641. /*
  642. switch (v->mode) {
  643. case VIDEO_MODE_AUTO:
  644. pms_framerate(dev, 25);
  645. pms_secamcross(dev, 0);
  646. pms_format(dev, 0);
  647. break;
  648. }*/
  649. mutex_unlock(&dev->lock);
  650. return 0;
  651. }
  652. static int pms_queryctrl(struct file *file, void *priv,
  653. struct v4l2_queryctrl *qc)
  654. {
  655. switch (qc->id) {
  656. case V4L2_CID_BRIGHTNESS:
  657. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 139);
  658. case V4L2_CID_CONTRAST:
  659. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 70);
  660. case V4L2_CID_SATURATION:
  661. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 64);
  662. case V4L2_CID_HUE:
  663. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 0);
  664. }
  665. return -EINVAL;
  666. }
  667. static int pms_g_ctrl(struct file *file, void *priv,
  668. struct v4l2_control *ctrl)
  669. {
  670. struct pms *dev = video_drvdata(file);
  671. int ret = 0;
  672. switch (ctrl->id) {
  673. case V4L2_CID_BRIGHTNESS:
  674. ctrl->value = dev->brightness;
  675. break;
  676. case V4L2_CID_CONTRAST:
  677. ctrl->value = dev->contrast;
  678. break;
  679. case V4L2_CID_SATURATION:
  680. ctrl->value = dev->saturation;
  681. break;
  682. case V4L2_CID_HUE:
  683. ctrl->value = dev->hue;
  684. break;
  685. default:
  686. ret = -EINVAL;
  687. break;
  688. }
  689. return ret;
  690. }
  691. static int pms_s_ctrl(struct file *file, void *priv,
  692. struct v4l2_control *ctrl)
  693. {
  694. struct pms *dev = video_drvdata(file);
  695. int ret = 0;
  696. mutex_lock(&dev->lock);
  697. switch (ctrl->id) {
  698. case V4L2_CID_BRIGHTNESS:
  699. dev->brightness = ctrl->value;
  700. pms_brightness(dev, dev->brightness);
  701. break;
  702. case V4L2_CID_CONTRAST:
  703. dev->contrast = ctrl->value;
  704. pms_contrast(dev, dev->contrast);
  705. break;
  706. case V4L2_CID_SATURATION:
  707. dev->saturation = ctrl->value;
  708. pms_saturation(dev, dev->saturation);
  709. break;
  710. case V4L2_CID_HUE:
  711. dev->hue = ctrl->value;
  712. pms_hue(dev, dev->hue);
  713. break;
  714. default:
  715. ret = -EINVAL;
  716. break;
  717. }
  718. mutex_unlock(&dev->lock);
  719. return ret;
  720. }
  721. static int pms_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  722. {
  723. struct pms *dev = video_drvdata(file);
  724. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  725. pix->width = dev->width;
  726. pix->height = dev->height;
  727. pix->pixelformat = dev->width == 15 ?
  728. V4L2_PIX_FMT_RGB555 : V4L2_PIX_FMT_RGB565;
  729. pix->field = V4L2_FIELD_NONE;
  730. pix->bytesperline = 2 * dev->width;
  731. pix->sizeimage = 2 * dev->width * dev->height;
  732. /* Just a guess */
  733. pix->colorspace = V4L2_COLORSPACE_SRGB;
  734. return 0;
  735. }
  736. static int pms_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  737. {
  738. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  739. if (pix->height < 16 || pix->height > 480)
  740. return -EINVAL;
  741. if (pix->width < 16 || pix->width > 640)
  742. return -EINVAL;
  743. if (pix->pixelformat != V4L2_PIX_FMT_RGB555 &&
  744. pix->pixelformat != V4L2_PIX_FMT_RGB565)
  745. return -EINVAL;
  746. pix->field = V4L2_FIELD_NONE;
  747. pix->bytesperline = 2 * pix->width;
  748. pix->sizeimage = 2 * pix->width * pix->height;
  749. /* Just a guess */
  750. pix->colorspace = V4L2_COLORSPACE_SRGB;
  751. return 0;
  752. }
  753. static int pms_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  754. {
  755. struct pms *dev = video_drvdata(file);
  756. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  757. int ret = pms_try_fmt_vid_cap(file, fh, fmt);
  758. if (ret)
  759. return ret;
  760. mutex_lock(&dev->lock);
  761. dev->width = pix->width;
  762. dev->height = pix->height;
  763. dev->depth = (pix->pixelformat == V4L2_PIX_FMT_RGB555) ? 15 : 16;
  764. pms_resolution(dev, dev->width, dev->height);
  765. /* Ok we figured out what to use from our wide choice */
  766. mutex_unlock(&dev->lock);
  767. return 0;
  768. }
  769. static int pms_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  770. {
  771. static struct v4l2_fmtdesc formats[] = {
  772. { 0, 0, 0,
  773. "RGB 5:5:5", V4L2_PIX_FMT_RGB555,
  774. { 0, 0, 0, 0 }
  775. },
  776. { 0, 0, 0,
  777. "RGB 5:6:5", V4L2_PIX_FMT_RGB565,
  778. { 0, 0, 0, 0 }
  779. },
  780. };
  781. enum v4l2_buf_type type = fmt->type;
  782. if (fmt->index > 1)
  783. return -EINVAL;
  784. *fmt = formats[fmt->index];
  785. fmt->type = type;
  786. return 0;
  787. }
  788. static ssize_t pms_read(struct file *file, char __user *buf,
  789. size_t count, loff_t *ppos)
  790. {
  791. struct pms *dev = video_drvdata(file);
  792. int len;
  793. mutex_lock(&dev->lock);
  794. len = pms_capture(dev, buf, (dev->depth == 15), count);
  795. mutex_unlock(&dev->lock);
  796. return len;
  797. }
  798. static const struct v4l2_file_operations pms_fops = {
  799. .owner = THIS_MODULE,
  800. .unlocked_ioctl = video_ioctl2,
  801. .read = pms_read,
  802. };
  803. static const struct v4l2_ioctl_ops pms_ioctl_ops = {
  804. .vidioc_querycap = pms_querycap,
  805. .vidioc_g_input = pms_g_input,
  806. .vidioc_s_input = pms_s_input,
  807. .vidioc_enum_input = pms_enum_input,
  808. .vidioc_g_std = pms_g_std,
  809. .vidioc_s_std = pms_s_std,
  810. .vidioc_queryctrl = pms_queryctrl,
  811. .vidioc_g_ctrl = pms_g_ctrl,
  812. .vidioc_s_ctrl = pms_s_ctrl,
  813. .vidioc_enum_fmt_vid_cap = pms_enum_fmt_vid_cap,
  814. .vidioc_g_fmt_vid_cap = pms_g_fmt_vid_cap,
  815. .vidioc_s_fmt_vid_cap = pms_s_fmt_vid_cap,
  816. .vidioc_try_fmt_vid_cap = pms_try_fmt_vid_cap,
  817. };
  818. /*
  819. * Probe for and initialise the Mediavision PMS
  820. */
  821. static int init_mediavision(struct pms *dev)
  822. {
  823. int id;
  824. int idec, decst;
  825. int i;
  826. static const unsigned char i2c_defs[] = {
  827. 0x4c, 0x30, 0x00, 0xe8,
  828. 0xb6, 0xe2, 0x00, 0x00,
  829. 0xff, 0xff, 0x00, 0x00,
  830. 0x00, 0x00, 0x78, 0x98,
  831. 0x00, 0x00, 0x00, 0x00,
  832. 0x34, 0x0a, 0xf4, 0xce,
  833. 0xe4
  834. };
  835. dev->mem = ioremap(mem_base, 0x800);
  836. if (!dev->mem)
  837. return -ENOMEM;
  838. if (!request_region(0x9a01, 1, "Mediavision PMS config")) {
  839. printk(KERN_WARNING "mediavision: unable to detect: 0x9a01 in use.\n");
  840. iounmap(dev->mem);
  841. return -EBUSY;
  842. }
  843. if (!request_region(dev->io, 3, "Mediavision PMS")) {
  844. printk(KERN_WARNING "mediavision: I/O port %d in use.\n", dev->io);
  845. release_region(0x9a01, 1);
  846. iounmap(dev->mem);
  847. return -EBUSY;
  848. }
  849. outb(0xb8, 0x9a01); /* Unlock */
  850. outb(dev->io >> 4, 0x9a01); /* Set IO port */
  851. id = mvv_read(dev, 3);
  852. decst = pms_i2c_stat(dev, 0x43);
  853. if (decst != -1)
  854. idec = 2;
  855. else if (pms_i2c_stat(dev, 0xb9) != -1)
  856. idec = 3;
  857. else if (pms_i2c_stat(dev, 0x8b) != -1)
  858. idec = 1;
  859. else
  860. idec = 0;
  861. printk(KERN_INFO "PMS type is %d\n", idec);
  862. if (idec == 0) {
  863. release_region(dev->io, 3);
  864. release_region(0x9a01, 1);
  865. iounmap(dev->mem);
  866. return -ENODEV;
  867. }
  868. /*
  869. * Ok we have a PMS of some sort
  870. */
  871. mvv_write(dev, 0x04, mem_base >> 12); /* Set the memory area */
  872. /* Ok now load the defaults */
  873. for (i = 0; i < 0x19; i++) {
  874. if (i2c_defs[i] == 0xff)
  875. pms_i2c_andor(dev, 0x8a, i, 0x07, 0x00);
  876. else
  877. pms_i2c_write(dev, 0x8a, i, i2c_defs[i]);
  878. }
  879. pms_i2c_write(dev, 0xb8, 0x00, 0x12);
  880. pms_i2c_write(dev, 0xb8, 0x04, 0x00);
  881. pms_i2c_write(dev, 0xb8, 0x07, 0x00);
  882. pms_i2c_write(dev, 0xb8, 0x08, 0x00);
  883. pms_i2c_write(dev, 0xb8, 0x09, 0xff);
  884. pms_i2c_write(dev, 0xb8, 0x0a, 0x00);
  885. pms_i2c_write(dev, 0xb8, 0x0b, 0x10);
  886. pms_i2c_write(dev, 0xb8, 0x10, 0x03);
  887. mvv_write(dev, 0x01, 0x00);
  888. mvv_write(dev, 0x05, 0xa0);
  889. mvv_write(dev, 0x08, 0x25);
  890. mvv_write(dev, 0x09, 0x00);
  891. mvv_write(dev, 0x0a, 0x20 | MVVMEMORYWIDTH);
  892. mvv_write(dev, 0x10, 0x02);
  893. mvv_write(dev, 0x1e, 0x0c);
  894. mvv_write(dev, 0x1f, 0x03);
  895. mvv_write(dev, 0x26, 0x06);
  896. mvv_write(dev, 0x2b, 0x00);
  897. mvv_write(dev, 0x2c, 0x20);
  898. mvv_write(dev, 0x2d, 0x00);
  899. mvv_write(dev, 0x2f, 0x70);
  900. mvv_write(dev, 0x32, 0x00);
  901. mvv_write(dev, 0x33, MVVMEMORYWIDTH);
  902. mvv_write(dev, 0x34, 0x00);
  903. mvv_write(dev, 0x35, 0x00);
  904. mvv_write(dev, 0x3a, 0x80);
  905. mvv_write(dev, 0x3b, 0x10);
  906. mvv_write(dev, 0x20, 0x00);
  907. mvv_write(dev, 0x21, 0x00);
  908. mvv_write(dev, 0x30, 0x22);
  909. return 0;
  910. }
  911. /*
  912. * Initialization and module stuff
  913. */
  914. #ifndef MODULE
  915. static int enable;
  916. module_param(enable, int, 0);
  917. #endif
  918. static int __init pms_init(void)
  919. {
  920. struct pms *dev = &pms_card;
  921. struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
  922. int res;
  923. strlcpy(v4l2_dev->name, "pms", sizeof(v4l2_dev->name));
  924. v4l2_info(v4l2_dev, "Mediavision Pro Movie Studio driver 0.03\n");
  925. #ifndef MODULE
  926. if (!enable) {
  927. v4l2_err(v4l2_dev,
  928. "PMS: not enabled, use pms.enable=1 to probe\n");
  929. return -ENODEV;
  930. }
  931. #endif
  932. dev->decoder = PHILIPS2;
  933. dev->io = io_port;
  934. dev->data = io_port + 1;
  935. if (init_mediavision(dev)) {
  936. v4l2_err(v4l2_dev, "Board not found.\n");
  937. return -ENODEV;
  938. }
  939. res = v4l2_device_register(NULL, v4l2_dev);
  940. if (res < 0) {
  941. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  942. return res;
  943. }
  944. strlcpy(dev->vdev.name, v4l2_dev->name, sizeof(dev->vdev.name));
  945. dev->vdev.v4l2_dev = v4l2_dev;
  946. dev->vdev.fops = &pms_fops;
  947. dev->vdev.ioctl_ops = &pms_ioctl_ops;
  948. dev->vdev.release = video_device_release_empty;
  949. video_set_drvdata(&dev->vdev, dev);
  950. mutex_init(&dev->lock);
  951. dev->std = V4L2_STD_NTSC_M;
  952. dev->height = 240;
  953. dev->width = 320;
  954. dev->depth = 15;
  955. dev->brightness = 139;
  956. dev->contrast = 70;
  957. dev->hue = 0;
  958. dev->saturation = 64;
  959. pms_swsense(dev, 75);
  960. pms_resolution(dev, 320, 240);
  961. pms_videosource(dev, 0);
  962. pms_vcrinput(dev, 0);
  963. if (video_register_device(&dev->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  964. v4l2_device_unregister(&dev->v4l2_dev);
  965. release_region(dev->io, 3);
  966. release_region(0x9a01, 1);
  967. iounmap(dev->mem);
  968. return -EINVAL;
  969. }
  970. return 0;
  971. }
  972. static void __exit pms_exit(void)
  973. {
  974. struct pms *dev = &pms_card;
  975. video_unregister_device(&dev->vdev);
  976. release_region(dev->io, 3);
  977. release_region(0x9a01, 1);
  978. iounmap(dev->mem);
  979. }
  980. module_init(pms_init);
  981. module_exit(pms_exit);