w9966.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  1. /*
  2. Winbond w9966cf Webcam parport driver.
  3. Version 0.33
  4. Copyright (C) 2001 Jakob Kemi <jakob.kemi@post.utfors.se>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. /*
  18. Supported devices:
  19. *Lifeview FlyCam Supra (using the Philips saa7111a chip)
  20. Does any other model using the w9966 interface chip exist ?
  21. Todo:
  22. *Add a working EPP mode, since DMA ECP read isn't implemented
  23. in the parport drivers. (That's why it's so sloow)
  24. *Add support for other ccd-control chips than the saa7111
  25. please send me feedback on what kind of chips you have.
  26. *Add proper probing. I don't know what's wrong with the IEEE1284
  27. parport drivers but (IEEE1284_MODE_NIBBLE|IEEE1284_DEVICE_ID)
  28. and nibble read seems to be broken for some peripherals.
  29. *Add probing for onboard SRAM, port directions etc. (if possible)
  30. *Add support for the hardware compressed modes (maybe using v4l2)
  31. *Fix better support for the capture window (no skewed images, v4l
  32. interface to capt. window)
  33. *Probably some bugs that I don't know of
  34. Please support me by sending feedback!
  35. Changes:
  36. Alan Cox: Removed RGB mode for kernel merge, added THIS_MODULE
  37. and owner support for newer module locks
  38. */
  39. #include <linux/module.h>
  40. #include <linux/init.h>
  41. #include <linux/delay.h>
  42. #include <linux/videodev2.h>
  43. #include <linux/slab.h>
  44. #include <media/v4l2-common.h>
  45. #include <media/v4l2-ioctl.h>
  46. #include <media/v4l2-device.h>
  47. #include <linux/parport.h>
  48. /*#define DEBUG*/ /* Undef me for production */
  49. #ifdef DEBUG
  50. #define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a)
  51. #else
  52. #define DPRINTF(x...)
  53. #endif
  54. /*
  55. * Defines, simple typedefs etc.
  56. */
  57. #define W9966_DRIVERNAME "W9966CF Webcam"
  58. #define W9966_MAXCAMS 4 /* Maximum number of cameras */
  59. #define W9966_RBUFFER 2048 /* Read buffer (must be an even number) */
  60. #define W9966_SRAMSIZE 131072 /* 128kb */
  61. #define W9966_SRAMID 0x02 /* check w9966cf.pdf */
  62. /* Empirically determined window limits */
  63. #define W9966_WND_MIN_X 16
  64. #define W9966_WND_MIN_Y 14
  65. #define W9966_WND_MAX_X 705
  66. #define W9966_WND_MAX_Y 253
  67. #define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X)
  68. #define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y)
  69. /* Keep track of our current state */
  70. #define W9966_STATE_PDEV 0x01
  71. #define W9966_STATE_CLAIMED 0x02
  72. #define W9966_STATE_VDEV 0x04
  73. #define W9966_I2C_W_ID 0x48
  74. #define W9966_I2C_R_ID 0x49
  75. #define W9966_I2C_R_DATA 0x08
  76. #define W9966_I2C_R_CLOCK 0x04
  77. #define W9966_I2C_W_DATA 0x02
  78. #define W9966_I2C_W_CLOCK 0x01
  79. struct w9966 {
  80. struct v4l2_device v4l2_dev;
  81. unsigned char dev_state;
  82. unsigned char i2c_state;
  83. unsigned short ppmode;
  84. struct parport *pport;
  85. struct pardevice *pdev;
  86. struct video_device vdev;
  87. unsigned short width;
  88. unsigned short height;
  89. unsigned char brightness;
  90. signed char contrast;
  91. signed char color;
  92. signed char hue;
  93. struct mutex lock;
  94. };
  95. /*
  96. * Module specific properties
  97. */
  98. MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>");
  99. MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)");
  100. MODULE_LICENSE("GPL");
  101. MODULE_VERSION("0.33.1");
  102. #ifdef MODULE
  103. static char *pardev[] = {[0 ... W9966_MAXCAMS] = ""};
  104. #else
  105. static char *pardev[] = {[0 ... W9966_MAXCAMS] = "aggressive"};
  106. #endif
  107. module_param_array(pardev, charp, NULL, 0);
  108. MODULE_PARM_DESC(pardev, "pardev: where to search for\n"
  109. "\teach camera. 'aggressive' means brute-force search.\n"
  110. "\tEg: >pardev=parport3,aggressive,parport2,parport1< would assign\n"
  111. "\tcam 1 to parport3 and search every parport for cam 2 etc...");
  112. static int parmode;
  113. module_param(parmode, int, 0);
  114. MODULE_PARM_DESC(parmode, "parmode: transfer mode (0=auto, 1=ecp, 2=epp");
  115. static int video_nr = -1;
  116. module_param(video_nr, int, 0);
  117. static struct w9966 w9966_cams[W9966_MAXCAMS];
  118. /*
  119. * Private function defines
  120. */
  121. /* Set camera phase flags, so we know what to uninit when terminating */
  122. static inline void w9966_set_state(struct w9966 *cam, int mask, int val)
  123. {
  124. cam->dev_state = (cam->dev_state & ~mask) ^ val;
  125. }
  126. /* Get camera phase flags */
  127. static inline int w9966_get_state(struct w9966 *cam, int mask, int val)
  128. {
  129. return ((cam->dev_state & mask) == val);
  130. }
  131. /* Claim parport for ourself */
  132. static void w9966_pdev_claim(struct w9966 *cam)
  133. {
  134. if (w9966_get_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED))
  135. return;
  136. parport_claim_or_block(cam->pdev);
  137. w9966_set_state(cam, W9966_STATE_CLAIMED, W9966_STATE_CLAIMED);
  138. }
  139. /* Release parport for others to use */
  140. static void w9966_pdev_release(struct w9966 *cam)
  141. {
  142. if (w9966_get_state(cam, W9966_STATE_CLAIMED, 0))
  143. return;
  144. parport_release(cam->pdev);
  145. w9966_set_state(cam, W9966_STATE_CLAIMED, 0);
  146. }
  147. /* Read register from W9966 interface-chip
  148. Expects a claimed pdev
  149. -1 on error, else register data (byte) */
  150. static int w9966_read_reg(struct w9966 *cam, int reg)
  151. {
  152. /* ECP, read, regtransfer, REG, REG, REG, REG, REG */
  153. const unsigned char addr = 0x80 | (reg & 0x1f);
  154. unsigned char val;
  155. if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
  156. return -1;
  157. if (parport_write(cam->pport, &addr, 1) != 1)
  158. return -1;
  159. if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
  160. return -1;
  161. if (parport_read(cam->pport, &val, 1) != 1)
  162. return -1;
  163. return val;
  164. }
  165. /* Write register to W9966 interface-chip
  166. Expects a claimed pdev
  167. -1 on error */
  168. static int w9966_write_reg(struct w9966 *cam, int reg, int data)
  169. {
  170. /* ECP, write, regtransfer, REG, REG, REG, REG, REG */
  171. const unsigned char addr = 0xc0 | (reg & 0x1f);
  172. const unsigned char val = data;
  173. if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_ADDR) != 0)
  174. return -1;
  175. if (parport_write(cam->pport, &addr, 1) != 1)
  176. return -1;
  177. if (parport_negotiate(cam->pport, cam->ppmode | IEEE1284_DATA) != 0)
  178. return -1;
  179. if (parport_write(cam->pport, &val, 1) != 1)
  180. return -1;
  181. return 0;
  182. }
  183. /*
  184. * Ugly and primitive i2c protocol functions
  185. */
  186. /* Sets the data line on the i2c bus.
  187. Expects a claimed pdev. */
  188. static void w9966_i2c_setsda(struct w9966 *cam, int state)
  189. {
  190. if (state)
  191. cam->i2c_state |= W9966_I2C_W_DATA;
  192. else
  193. cam->i2c_state &= ~W9966_I2C_W_DATA;
  194. w9966_write_reg(cam, 0x18, cam->i2c_state);
  195. udelay(5);
  196. }
  197. /* Get peripheral clock line
  198. Expects a claimed pdev. */
  199. static int w9966_i2c_getscl(struct w9966 *cam)
  200. {
  201. const unsigned char state = w9966_read_reg(cam, 0x18);
  202. return ((state & W9966_I2C_R_CLOCK) > 0);
  203. }
  204. /* Sets the clock line on the i2c bus.
  205. Expects a claimed pdev. -1 on error */
  206. static int w9966_i2c_setscl(struct w9966 *cam, int state)
  207. {
  208. unsigned long timeout;
  209. if (state)
  210. cam->i2c_state |= W9966_I2C_W_CLOCK;
  211. else
  212. cam->i2c_state &= ~W9966_I2C_W_CLOCK;
  213. w9966_write_reg(cam, 0x18, cam->i2c_state);
  214. udelay(5);
  215. /* we go to high, we also expect the peripheral to ack. */
  216. if (state) {
  217. timeout = jiffies + 100;
  218. while (!w9966_i2c_getscl(cam)) {
  219. if (time_after(jiffies, timeout))
  220. return -1;
  221. }
  222. }
  223. return 0;
  224. }
  225. #if 0
  226. /* Get peripheral data line
  227. Expects a claimed pdev. */
  228. static int w9966_i2c_getsda(struct w9966 *cam)
  229. {
  230. const unsigned char state = w9966_read_reg(cam, 0x18);
  231. return ((state & W9966_I2C_R_DATA) > 0);
  232. }
  233. #endif
  234. /* Write a byte with ack to the i2c bus.
  235. Expects a claimed pdev. -1 on error */
  236. static int w9966_i2c_wbyte(struct w9966 *cam, int data)
  237. {
  238. int i;
  239. for (i = 7; i >= 0; i--) {
  240. w9966_i2c_setsda(cam, (data >> i) & 0x01);
  241. if (w9966_i2c_setscl(cam, 1) == -1)
  242. return -1;
  243. w9966_i2c_setscl(cam, 0);
  244. }
  245. w9966_i2c_setsda(cam, 1);
  246. if (w9966_i2c_setscl(cam, 1) == -1)
  247. return -1;
  248. w9966_i2c_setscl(cam, 0);
  249. return 0;
  250. }
  251. /* Read a data byte with ack from the i2c-bus
  252. Expects a claimed pdev. -1 on error */
  253. #if 0
  254. static int w9966_i2c_rbyte(struct w9966 *cam)
  255. {
  256. unsigned char data = 0x00;
  257. int i;
  258. w9966_i2c_setsda(cam, 1);
  259. for (i = 0; i < 8; i++) {
  260. if (w9966_i2c_setscl(cam, 1) == -1)
  261. return -1;
  262. data = data << 1;
  263. if (w9966_i2c_getsda(cam))
  264. data |= 0x01;
  265. w9966_i2c_setscl(cam, 0);
  266. }
  267. return data;
  268. }
  269. #endif
  270. /* Read a register from the i2c device.
  271. Expects claimed pdev. -1 on error */
  272. #if 0
  273. static int w9966_read_reg_i2c(struct w9966 *cam, int reg)
  274. {
  275. int data;
  276. w9966_i2c_setsda(cam, 0);
  277. w9966_i2c_setscl(cam, 0);
  278. if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
  279. w9966_i2c_wbyte(cam, reg) == -1)
  280. return -1;
  281. w9966_i2c_setsda(cam, 1);
  282. if (w9966_i2c_setscl(cam, 1) == -1)
  283. return -1;
  284. w9966_i2c_setsda(cam, 0);
  285. w9966_i2c_setscl(cam, 0);
  286. if (w9966_i2c_wbyte(cam, W9966_I2C_R_ID) == -1)
  287. return -1;
  288. data = w9966_i2c_rbyte(cam);
  289. if (data == -1)
  290. return -1;
  291. w9966_i2c_setsda(cam, 0);
  292. if (w9966_i2c_setscl(cam, 1) == -1)
  293. return -1;
  294. w9966_i2c_setsda(cam, 1);
  295. return data;
  296. }
  297. #endif
  298. /* Write a register to the i2c device.
  299. Expects claimed pdev. -1 on error */
  300. static int w9966_write_reg_i2c(struct w9966 *cam, int reg, int data)
  301. {
  302. w9966_i2c_setsda(cam, 0);
  303. w9966_i2c_setscl(cam, 0);
  304. if (w9966_i2c_wbyte(cam, W9966_I2C_W_ID) == -1 ||
  305. w9966_i2c_wbyte(cam, reg) == -1 ||
  306. w9966_i2c_wbyte(cam, data) == -1)
  307. return -1;
  308. w9966_i2c_setsda(cam, 0);
  309. if (w9966_i2c_setscl(cam, 1) == -1)
  310. return -1;
  311. w9966_i2c_setsda(cam, 1);
  312. return 0;
  313. }
  314. /* Find a good length for capture window (used both for W and H)
  315. A bit ugly but pretty functional. The capture length
  316. have to match the downscale */
  317. static int w9966_findlen(int near, int size, int maxlen)
  318. {
  319. int bestlen = size;
  320. int besterr = abs(near - bestlen);
  321. int len;
  322. for (len = size + 1; len < maxlen; len++) {
  323. int err;
  324. if (((64 * size) % len) != 0)
  325. continue;
  326. err = abs(near - len);
  327. /* Only continue as long as we keep getting better values */
  328. if (err > besterr)
  329. break;
  330. besterr = err;
  331. bestlen = len;
  332. }
  333. return bestlen;
  334. }
  335. /* Modify capture window (if necessary)
  336. and calculate downscaling
  337. Return -1 on error */
  338. static int w9966_calcscale(int size, int min, int max, int *beg, int *end, unsigned char *factor)
  339. {
  340. int maxlen = max - min;
  341. int len = *end - *beg + 1;
  342. int newlen = w9966_findlen(len, size, maxlen);
  343. int err = newlen - len;
  344. /* Check for bad format */
  345. if (newlen > maxlen || newlen < size)
  346. return -1;
  347. /* Set factor (6 bit fixed) */
  348. *factor = (64 * size) / newlen;
  349. if (*factor == 64)
  350. *factor = 0x00; /* downscale is disabled */
  351. else
  352. *factor |= 0x80; /* set downscale-enable bit */
  353. /* Modify old beginning and end */
  354. *beg -= err / 2;
  355. *end += err - (err / 2);
  356. /* Move window if outside borders */
  357. if (*beg < min) {
  358. *end += min - *beg;
  359. *beg += min - *beg;
  360. }
  361. if (*end > max) {
  362. *beg -= *end - max;
  363. *end -= *end - max;
  364. }
  365. return 0;
  366. }
  367. /* Setup the cameras capture window etc.
  368. Expects a claimed pdev
  369. return -1 on error */
  370. static int w9966_setup(struct w9966 *cam, int x1, int y1, int x2, int y2, int w, int h)
  371. {
  372. unsigned int i;
  373. unsigned int enh_s, enh_e;
  374. unsigned char scale_x, scale_y;
  375. unsigned char regs[0x1c];
  376. unsigned char saa7111_regs[] = {
  377. 0x21, 0x00, 0xd8, 0x23, 0x00, 0x80, 0x80, 0x00,
  378. 0x88, 0x10, 0x80, 0x40, 0x40, 0x00, 0x01, 0x00,
  379. 0x48, 0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  380. 0x00, 0x00, 0x00, 0x71, 0xe7, 0x00, 0x00, 0xc0
  381. };
  382. if (w * h * 2 > W9966_SRAMSIZE) {
  383. DPRINTF("capture window exceeds SRAM size!.\n");
  384. w = 200; h = 160; /* Pick default values */
  385. }
  386. w &= ~0x1;
  387. if (w < 2)
  388. w = 2;
  389. if (h < 1)
  390. h = 1;
  391. if (w > W9966_WND_MAX_W)
  392. w = W9966_WND_MAX_W;
  393. if (h > W9966_WND_MAX_H)
  394. h = W9966_WND_MAX_H;
  395. cam->width = w;
  396. cam->height = h;
  397. enh_s = 0;
  398. enh_e = w * h * 2;
  399. /* Modify capture window if necessary and calculate downscaling */
  400. if (w9966_calcscale(w, W9966_WND_MIN_X, W9966_WND_MAX_X, &x1, &x2, &scale_x) != 0 ||
  401. w9966_calcscale(h, W9966_WND_MIN_Y, W9966_WND_MAX_Y, &y1, &y2, &scale_y) != 0)
  402. return -1;
  403. DPRINTF("%dx%d, x: %d<->%d, y: %d<->%d, sx: %d/64, sy: %d/64.\n",
  404. w, h, x1, x2, y1, y2, scale_x & ~0x80, scale_y & ~0x80);
  405. /* Setup registers */
  406. regs[0x00] = 0x00; /* Set normal operation */
  407. regs[0x01] = 0x18; /* Capture mode */
  408. regs[0x02] = scale_y; /* V-scaling */
  409. regs[0x03] = scale_x; /* H-scaling */
  410. /* Capture window */
  411. regs[0x04] = (x1 & 0x0ff); /* X-start (8 low bits) */
  412. regs[0x05] = (x1 & 0x300)>>8; /* X-start (2 high bits) */
  413. regs[0x06] = (y1 & 0x0ff); /* Y-start (8 low bits) */
  414. regs[0x07] = (y1 & 0x300)>>8; /* Y-start (2 high bits) */
  415. regs[0x08] = (x2 & 0x0ff); /* X-end (8 low bits) */
  416. regs[0x09] = (x2 & 0x300)>>8; /* X-end (2 high bits) */
  417. regs[0x0a] = (y2 & 0x0ff); /* Y-end (8 low bits) */
  418. regs[0x0c] = W9966_SRAMID; /* SRAM-banks (1x 128kb) */
  419. /* Enhancement layer */
  420. regs[0x0d] = (enh_s & 0x000ff); /* Enh. start (0-7) */
  421. regs[0x0e] = (enh_s & 0x0ff00) >> 8; /* Enh. start (8-15) */
  422. regs[0x0f] = (enh_s & 0x70000) >> 16; /* Enh. start (16-17/18??) */
  423. regs[0x10] = (enh_e & 0x000ff); /* Enh. end (0-7) */
  424. regs[0x11] = (enh_e & 0x0ff00) >> 8; /* Enh. end (8-15) */
  425. regs[0x12] = (enh_e & 0x70000) >> 16; /* Enh. end (16-17/18??) */
  426. /* Misc */
  427. regs[0x13] = 0x40; /* VEE control (raw 4:2:2) */
  428. regs[0x17] = 0x00; /* ??? */
  429. regs[0x18] = cam->i2c_state = 0x00; /* Serial bus */
  430. regs[0x19] = 0xff; /* I/O port direction control */
  431. regs[0x1a] = 0xff; /* I/O port data register */
  432. regs[0x1b] = 0x10; /* ??? */
  433. /* SAA7111 chip settings */
  434. saa7111_regs[0x0a] = cam->brightness;
  435. saa7111_regs[0x0b] = cam->contrast;
  436. saa7111_regs[0x0c] = cam->color;
  437. saa7111_regs[0x0d] = cam->hue;
  438. /* Reset (ECP-fifo & serial-bus) */
  439. if (w9966_write_reg(cam, 0x00, 0x03) == -1)
  440. return -1;
  441. /* Write regs to w9966cf chip */
  442. for (i = 0; i < 0x1c; i++)
  443. if (w9966_write_reg(cam, i, regs[i]) == -1)
  444. return -1;
  445. /* Write regs to saa7111 chip */
  446. for (i = 0; i < 0x20; i++)
  447. if (w9966_write_reg_i2c(cam, i, saa7111_regs[i]) == -1)
  448. return -1;
  449. return 0;
  450. }
  451. /*
  452. * Video4linux interfacing
  453. */
  454. static int cam_querycap(struct file *file, void *priv,
  455. struct v4l2_capability *vcap)
  456. {
  457. struct w9966 *cam = video_drvdata(file);
  458. strlcpy(vcap->driver, cam->v4l2_dev.name, sizeof(vcap->driver));
  459. strlcpy(vcap->card, W9966_DRIVERNAME, sizeof(vcap->card));
  460. strlcpy(vcap->bus_info, "parport", sizeof(vcap->bus_info));
  461. vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  462. return 0;
  463. }
  464. static int cam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  465. {
  466. if (vin->index > 0)
  467. return -EINVAL;
  468. strlcpy(vin->name, "Camera", sizeof(vin->name));
  469. vin->type = V4L2_INPUT_TYPE_CAMERA;
  470. vin->audioset = 0;
  471. vin->tuner = 0;
  472. vin->std = 0;
  473. vin->status = 0;
  474. return 0;
  475. }
  476. static int cam_g_input(struct file *file, void *fh, unsigned int *inp)
  477. {
  478. *inp = 0;
  479. return 0;
  480. }
  481. static int cam_s_input(struct file *file, void *fh, unsigned int inp)
  482. {
  483. return (inp > 0) ? -EINVAL : 0;
  484. }
  485. static int cam_queryctrl(struct file *file, void *priv,
  486. struct v4l2_queryctrl *qc)
  487. {
  488. switch (qc->id) {
  489. case V4L2_CID_BRIGHTNESS:
  490. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  491. case V4L2_CID_CONTRAST:
  492. return v4l2_ctrl_query_fill(qc, -64, 64, 1, 64);
  493. case V4L2_CID_SATURATION:
  494. return v4l2_ctrl_query_fill(qc, -64, 64, 1, 64);
  495. case V4L2_CID_HUE:
  496. return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  497. }
  498. return -EINVAL;
  499. }
  500. static int cam_g_ctrl(struct file *file, void *priv,
  501. struct v4l2_control *ctrl)
  502. {
  503. struct w9966 *cam = video_drvdata(file);
  504. int ret = 0;
  505. switch (ctrl->id) {
  506. case V4L2_CID_BRIGHTNESS:
  507. ctrl->value = cam->brightness;
  508. break;
  509. case V4L2_CID_CONTRAST:
  510. ctrl->value = cam->contrast;
  511. break;
  512. case V4L2_CID_SATURATION:
  513. ctrl->value = cam->color;
  514. break;
  515. case V4L2_CID_HUE:
  516. ctrl->value = cam->hue;
  517. break;
  518. default:
  519. ret = -EINVAL;
  520. break;
  521. }
  522. return ret;
  523. }
  524. static int cam_s_ctrl(struct file *file, void *priv,
  525. struct v4l2_control *ctrl)
  526. {
  527. struct w9966 *cam = video_drvdata(file);
  528. int ret = 0;
  529. mutex_lock(&cam->lock);
  530. switch (ctrl->id) {
  531. case V4L2_CID_BRIGHTNESS:
  532. cam->brightness = ctrl->value;
  533. break;
  534. case V4L2_CID_CONTRAST:
  535. cam->contrast = ctrl->value;
  536. break;
  537. case V4L2_CID_SATURATION:
  538. cam->color = ctrl->value;
  539. break;
  540. case V4L2_CID_HUE:
  541. cam->hue = ctrl->value;
  542. break;
  543. default:
  544. ret = -EINVAL;
  545. break;
  546. }
  547. if (ret == 0) {
  548. w9966_pdev_claim(cam);
  549. if (w9966_write_reg_i2c(cam, 0x0a, cam->brightness) == -1 ||
  550. w9966_write_reg_i2c(cam, 0x0b, cam->contrast) == -1 ||
  551. w9966_write_reg_i2c(cam, 0x0c, cam->color) == -1 ||
  552. w9966_write_reg_i2c(cam, 0x0d, cam->hue) == -1) {
  553. ret = -EIO;
  554. }
  555. w9966_pdev_release(cam);
  556. }
  557. mutex_unlock(&cam->lock);
  558. return ret;
  559. }
  560. static int cam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  561. {
  562. struct w9966 *cam = video_drvdata(file);
  563. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  564. pix->width = cam->width;
  565. pix->height = cam->height;
  566. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  567. pix->field = V4L2_FIELD_NONE;
  568. pix->bytesperline = 2 * cam->width;
  569. pix->sizeimage = 2 * cam->width * cam->height;
  570. /* Just a guess */
  571. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  572. return 0;
  573. }
  574. static int cam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  575. {
  576. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  577. if (pix->width < 2)
  578. pix->width = 2;
  579. if (pix->height < 1)
  580. pix->height = 1;
  581. if (pix->width > W9966_WND_MAX_W)
  582. pix->width = W9966_WND_MAX_W;
  583. if (pix->height > W9966_WND_MAX_H)
  584. pix->height = W9966_WND_MAX_H;
  585. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  586. pix->field = V4L2_FIELD_NONE;
  587. pix->bytesperline = 2 * pix->width;
  588. pix->sizeimage = 2 * pix->width * pix->height;
  589. /* Just a guess */
  590. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  591. return 0;
  592. }
  593. static int cam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  594. {
  595. struct w9966 *cam = video_drvdata(file);
  596. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  597. int ret = cam_try_fmt_vid_cap(file, fh, fmt);
  598. if (ret)
  599. return ret;
  600. mutex_lock(&cam->lock);
  601. /* Update camera regs */
  602. w9966_pdev_claim(cam);
  603. ret = w9966_setup(cam, 0, 0, 1023, 1023, pix->width, pix->height);
  604. w9966_pdev_release(cam);
  605. mutex_unlock(&cam->lock);
  606. return ret;
  607. }
  608. static int cam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  609. {
  610. static struct v4l2_fmtdesc formats[] = {
  611. { 0, 0, 0,
  612. "YUV 4:2:2", V4L2_PIX_FMT_YUYV,
  613. { 0, 0, 0, 0 }
  614. },
  615. };
  616. enum v4l2_buf_type type = fmt->type;
  617. if (fmt->index > 0)
  618. return -EINVAL;
  619. *fmt = formats[fmt->index];
  620. fmt->type = type;
  621. return 0;
  622. }
  623. /* Capture data */
  624. static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
  625. size_t count, loff_t *ppos)
  626. {
  627. struct w9966 *cam = video_drvdata(file);
  628. unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */
  629. unsigned char __user *dest = (unsigned char __user *)buf;
  630. unsigned long dleft = count;
  631. unsigned char *tbuf;
  632. /* Why would anyone want more than this?? */
  633. if (count > cam->width * cam->height * 2)
  634. return -EINVAL;
  635. mutex_lock(&cam->lock);
  636. w9966_pdev_claim(cam);
  637. w9966_write_reg(cam, 0x00, 0x02); /* Reset ECP-FIFO buffer */
  638. w9966_write_reg(cam, 0x00, 0x00); /* Return to normal operation */
  639. w9966_write_reg(cam, 0x01, 0x98); /* Enable capture */
  640. /* write special capture-addr and negotiate into data transfer */
  641. if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) ||
  642. (parport_write(cam->pport, &addr, 1) != 1) ||
  643. (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) {
  644. w9966_pdev_release(cam);
  645. mutex_unlock(&cam->lock);
  646. return -EFAULT;
  647. }
  648. tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL);
  649. if (tbuf == NULL) {
  650. count = -ENOMEM;
  651. goto out;
  652. }
  653. while (dleft > 0) {
  654. unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
  655. if (parport_read(cam->pport, tbuf, tsize) < tsize) {
  656. count = -EFAULT;
  657. goto out;
  658. }
  659. if (copy_to_user(dest, tbuf, tsize) != 0) {
  660. count = -EFAULT;
  661. goto out;
  662. }
  663. dest += tsize;
  664. dleft -= tsize;
  665. }
  666. w9966_write_reg(cam, 0x01, 0x18); /* Disable capture */
  667. out:
  668. kfree(tbuf);
  669. w9966_pdev_release(cam);
  670. mutex_unlock(&cam->lock);
  671. return count;
  672. }
  673. static const struct v4l2_file_operations w9966_fops = {
  674. .owner = THIS_MODULE,
  675. .unlocked_ioctl = video_ioctl2,
  676. .read = w9966_v4l_read,
  677. };
  678. static const struct v4l2_ioctl_ops w9966_ioctl_ops = {
  679. .vidioc_querycap = cam_querycap,
  680. .vidioc_g_input = cam_g_input,
  681. .vidioc_s_input = cam_s_input,
  682. .vidioc_enum_input = cam_enum_input,
  683. .vidioc_queryctrl = cam_queryctrl,
  684. .vidioc_g_ctrl = cam_g_ctrl,
  685. .vidioc_s_ctrl = cam_s_ctrl,
  686. .vidioc_enum_fmt_vid_cap = cam_enum_fmt_vid_cap,
  687. .vidioc_g_fmt_vid_cap = cam_g_fmt_vid_cap,
  688. .vidioc_s_fmt_vid_cap = cam_s_fmt_vid_cap,
  689. .vidioc_try_fmt_vid_cap = cam_try_fmt_vid_cap,
  690. };
  691. /* Initialize camera device. Setup all internal flags, set a
  692. default video mode, setup ccd-chip, register v4l device etc..
  693. Also used for 'probing' of hardware.
  694. -1 on error */
  695. static int w9966_init(struct w9966 *cam, struct parport *port)
  696. {
  697. struct v4l2_device *v4l2_dev = &cam->v4l2_dev;
  698. if (cam->dev_state != 0)
  699. return -1;
  700. strlcpy(v4l2_dev->name, "w9966", sizeof(v4l2_dev->name));
  701. if (v4l2_device_register(NULL, v4l2_dev) < 0) {
  702. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  703. return -1;
  704. }
  705. cam->pport = port;
  706. cam->brightness = 128;
  707. cam->contrast = 64;
  708. cam->color = 64;
  709. cam->hue = 0;
  710. /* Select requested transfer mode */
  711. switch (parmode) {
  712. default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */
  713. case 0:
  714. if (port->modes & PARPORT_MODE_ECP)
  715. cam->ppmode = IEEE1284_MODE_ECP;
  716. else if (port->modes & PARPORT_MODE_EPP)
  717. cam->ppmode = IEEE1284_MODE_EPP;
  718. else
  719. cam->ppmode = IEEE1284_MODE_ECP;
  720. break;
  721. case 1: /* hw- or sw-ecp */
  722. cam->ppmode = IEEE1284_MODE_ECP;
  723. break;
  724. case 2: /* hw- or sw-epp */
  725. cam->ppmode = IEEE1284_MODE_EPP;
  726. break;
  727. }
  728. /* Tell the parport driver that we exists */
  729. cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL);
  730. if (cam->pdev == NULL) {
  731. DPRINTF("parport_register_device() failed\n");
  732. return -1;
  733. }
  734. w9966_set_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV);
  735. w9966_pdev_claim(cam);
  736. /* Setup a default capture mode */
  737. if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) {
  738. DPRINTF("w9966_setup() failed.\n");
  739. return -1;
  740. }
  741. w9966_pdev_release(cam);
  742. /* Fill in the video_device struct and register us to v4l */
  743. strlcpy(cam->vdev.name, W9966_DRIVERNAME, sizeof(cam->vdev.name));
  744. cam->vdev.v4l2_dev = v4l2_dev;
  745. cam->vdev.fops = &w9966_fops;
  746. cam->vdev.ioctl_ops = &w9966_ioctl_ops;
  747. cam->vdev.release = video_device_release_empty;
  748. video_set_drvdata(&cam->vdev, cam);
  749. mutex_init(&cam->lock);
  750. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
  751. return -1;
  752. w9966_set_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
  753. /* All ok */
  754. v4l2_info(v4l2_dev, "Found and initialized a webcam on %s.\n",
  755. cam->pport->name);
  756. return 0;
  757. }
  758. /* Terminate everything gracefully */
  759. static void w9966_term(struct w9966 *cam)
  760. {
  761. /* Unregister from v4l */
  762. if (w9966_get_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) {
  763. video_unregister_device(&cam->vdev);
  764. w9966_set_state(cam, W9966_STATE_VDEV, 0);
  765. }
  766. /* Terminate from IEEE1284 mode and release pdev block */
  767. if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
  768. w9966_pdev_claim(cam);
  769. parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT);
  770. w9966_pdev_release(cam);
  771. }
  772. /* Unregister from parport */
  773. if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
  774. parport_unregister_device(cam->pdev);
  775. w9966_set_state(cam, W9966_STATE_PDEV, 0);
  776. }
  777. memset(cam, 0, sizeof(*cam));
  778. }
  779. /* Called once for every parport on init */
  780. static void w9966_attach(struct parport *port)
  781. {
  782. int i;
  783. for (i = 0; i < W9966_MAXCAMS; i++) {
  784. if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */
  785. continue;
  786. if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) {
  787. if (w9966_init(&w9966_cams[i], port) != 0)
  788. w9966_term(&w9966_cams[i]);
  789. break; /* return */
  790. }
  791. }
  792. }
  793. /* Called once for every parport on termination */
  794. static void w9966_detach(struct parport *port)
  795. {
  796. int i;
  797. for (i = 0; i < W9966_MAXCAMS; i++)
  798. if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
  799. w9966_term(&w9966_cams[i]);
  800. }
  801. static struct parport_driver w9966_ppd = {
  802. .name = W9966_DRIVERNAME,
  803. .attach = w9966_attach,
  804. .detach = w9966_detach,
  805. };
  806. /* Module entry point */
  807. static int __init w9966_mod_init(void)
  808. {
  809. int i;
  810. for (i = 0; i < W9966_MAXCAMS; i++)
  811. w9966_cams[i].dev_state = 0;
  812. return parport_register_driver(&w9966_ppd);
  813. }
  814. /* Module cleanup */
  815. static void __exit w9966_mod_term(void)
  816. {
  817. parport_unregister_driver(&w9966_ppd);
  818. }
  819. module_init(w9966_mod_init);
  820. module_exit(w9966_mod_term);