w9966.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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/version.h>
  43. #include <linux/videodev2.h>
  44. #include <linux/slab.h>
  45. #include <media/v4l2-common.h>
  46. #include <media/v4l2-ioctl.h>
  47. #include <media/v4l2-device.h>
  48. #include <linux/parport.h>
  49. /*#define DEBUG*/ /* Undef me for production */
  50. #ifdef DEBUG
  51. #define DPRINTF(x, a...) printk(KERN_DEBUG "W9966: %s(): "x, __func__ , ##a)
  52. #else
  53. #define DPRINTF(x...)
  54. #endif
  55. /*
  56. * Defines, simple typedefs etc.
  57. */
  58. #define W9966_DRIVERNAME "W9966CF Webcam"
  59. #define W9966_MAXCAMS 4 /* Maximum number of cameras */
  60. #define W9966_RBUFFER 2048 /* Read buffer (must be an even number) */
  61. #define W9966_SRAMSIZE 131072 /* 128kb */
  62. #define W9966_SRAMID 0x02 /* check w9966cf.pdf */
  63. /* Empirically determined window limits */
  64. #define W9966_WND_MIN_X 16
  65. #define W9966_WND_MIN_Y 14
  66. #define W9966_WND_MAX_X 705
  67. #define W9966_WND_MAX_Y 253
  68. #define W9966_WND_MAX_W (W9966_WND_MAX_X - W9966_WND_MIN_X)
  69. #define W9966_WND_MAX_H (W9966_WND_MAX_Y - W9966_WND_MIN_Y)
  70. /* Keep track of our current state */
  71. #define W9966_STATE_PDEV 0x01
  72. #define W9966_STATE_CLAIMED 0x02
  73. #define W9966_STATE_VDEV 0x04
  74. #define W9966_I2C_W_ID 0x48
  75. #define W9966_I2C_R_ID 0x49
  76. #define W9966_I2C_R_DATA 0x08
  77. #define W9966_I2C_R_CLOCK 0x04
  78. #define W9966_I2C_W_DATA 0x02
  79. #define W9966_I2C_W_CLOCK 0x01
  80. struct w9966 {
  81. struct v4l2_device v4l2_dev;
  82. unsigned char dev_state;
  83. unsigned char i2c_state;
  84. unsigned short ppmode;
  85. struct parport *pport;
  86. struct pardevice *pdev;
  87. struct video_device vdev;
  88. unsigned short width;
  89. unsigned short height;
  90. unsigned char brightness;
  91. signed char contrast;
  92. signed char color;
  93. signed char hue;
  94. struct mutex lock;
  95. };
  96. /*
  97. * Module specific properties
  98. */
  99. MODULE_AUTHOR("Jakob Kemi <jakob.kemi@post.utfors.se>");
  100. MODULE_DESCRIPTION("Winbond w9966cf WebCam driver (0.32)");
  101. MODULE_LICENSE("GPL");
  102. #ifdef MODULE
  103. static const char *pardev[] = {[0 ... W9966_MAXCAMS] = ""};
  104. #else
  105. static const 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->version = KERNEL_VERSION(0, 33, 0);
  462. vcap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_READWRITE;
  463. return 0;
  464. }
  465. static int cam_enum_input(struct file *file, void *fh, struct v4l2_input *vin)
  466. {
  467. if (vin->index > 0)
  468. return -EINVAL;
  469. strlcpy(vin->name, "Camera", sizeof(vin->name));
  470. vin->type = V4L2_INPUT_TYPE_CAMERA;
  471. vin->audioset = 0;
  472. vin->tuner = 0;
  473. vin->std = 0;
  474. vin->status = 0;
  475. return 0;
  476. }
  477. static int cam_g_input(struct file *file, void *fh, unsigned int *inp)
  478. {
  479. *inp = 0;
  480. return 0;
  481. }
  482. static int cam_s_input(struct file *file, void *fh, unsigned int inp)
  483. {
  484. return (inp > 0) ? -EINVAL : 0;
  485. }
  486. static int cam_queryctrl(struct file *file, void *priv,
  487. struct v4l2_queryctrl *qc)
  488. {
  489. switch (qc->id) {
  490. case V4L2_CID_BRIGHTNESS:
  491. return v4l2_ctrl_query_fill(qc, 0, 255, 1, 128);
  492. case V4L2_CID_CONTRAST:
  493. return v4l2_ctrl_query_fill(qc, -64, 64, 1, 64);
  494. case V4L2_CID_SATURATION:
  495. return v4l2_ctrl_query_fill(qc, -64, 64, 1, 64);
  496. case V4L2_CID_HUE:
  497. return v4l2_ctrl_query_fill(qc, -128, 127, 1, 0);
  498. }
  499. return -EINVAL;
  500. }
  501. static int cam_g_ctrl(struct file *file, void *priv,
  502. struct v4l2_control *ctrl)
  503. {
  504. struct w9966 *cam = video_drvdata(file);
  505. int ret = 0;
  506. switch (ctrl->id) {
  507. case V4L2_CID_BRIGHTNESS:
  508. ctrl->value = cam->brightness;
  509. break;
  510. case V4L2_CID_CONTRAST:
  511. ctrl->value = cam->contrast;
  512. break;
  513. case V4L2_CID_SATURATION:
  514. ctrl->value = cam->color;
  515. break;
  516. case V4L2_CID_HUE:
  517. ctrl->value = cam->hue;
  518. break;
  519. default:
  520. ret = -EINVAL;
  521. break;
  522. }
  523. return ret;
  524. }
  525. static int cam_s_ctrl(struct file *file, void *priv,
  526. struct v4l2_control *ctrl)
  527. {
  528. struct w9966 *cam = video_drvdata(file);
  529. int ret = 0;
  530. mutex_lock(&cam->lock);
  531. switch (ctrl->id) {
  532. case V4L2_CID_BRIGHTNESS:
  533. cam->brightness = ctrl->value;
  534. break;
  535. case V4L2_CID_CONTRAST:
  536. cam->contrast = ctrl->value;
  537. break;
  538. case V4L2_CID_SATURATION:
  539. cam->color = ctrl->value;
  540. break;
  541. case V4L2_CID_HUE:
  542. cam->hue = ctrl->value;
  543. break;
  544. default:
  545. ret = -EINVAL;
  546. break;
  547. }
  548. if (ret == 0) {
  549. w9966_pdev_claim(cam);
  550. if (w9966_write_reg_i2c(cam, 0x0a, cam->brightness) == -1 ||
  551. w9966_write_reg_i2c(cam, 0x0b, cam->contrast) == -1 ||
  552. w9966_write_reg_i2c(cam, 0x0c, cam->color) == -1 ||
  553. w9966_write_reg_i2c(cam, 0x0d, cam->hue) == -1) {
  554. ret = -EIO;
  555. }
  556. w9966_pdev_release(cam);
  557. }
  558. mutex_unlock(&cam->lock);
  559. return ret;
  560. }
  561. static int cam_g_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  562. {
  563. struct w9966 *cam = video_drvdata(file);
  564. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  565. pix->width = cam->width;
  566. pix->height = cam->height;
  567. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  568. pix->field = V4L2_FIELD_NONE;
  569. pix->bytesperline = 2 * cam->width;
  570. pix->sizeimage = 2 * cam->width * cam->height;
  571. /* Just a guess */
  572. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  573. return 0;
  574. }
  575. static int cam_try_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  576. {
  577. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  578. if (pix->width < 2)
  579. pix->width = 2;
  580. if (pix->height < 1)
  581. pix->height = 1;
  582. if (pix->width > W9966_WND_MAX_W)
  583. pix->width = W9966_WND_MAX_W;
  584. if (pix->height > W9966_WND_MAX_H)
  585. pix->height = W9966_WND_MAX_H;
  586. pix->pixelformat = V4L2_PIX_FMT_YUYV;
  587. pix->field = V4L2_FIELD_NONE;
  588. pix->bytesperline = 2 * pix->width;
  589. pix->sizeimage = 2 * pix->width * pix->height;
  590. /* Just a guess */
  591. pix->colorspace = V4L2_COLORSPACE_SMPTE170M;
  592. return 0;
  593. }
  594. static int cam_s_fmt_vid_cap(struct file *file, void *fh, struct v4l2_format *fmt)
  595. {
  596. struct w9966 *cam = video_drvdata(file);
  597. struct v4l2_pix_format *pix = &fmt->fmt.pix;
  598. int ret = cam_try_fmt_vid_cap(file, fh, fmt);
  599. if (ret)
  600. return ret;
  601. mutex_lock(&cam->lock);
  602. /* Update camera regs */
  603. w9966_pdev_claim(cam);
  604. ret = w9966_setup(cam, 0, 0, 1023, 1023, pix->width, pix->height);
  605. w9966_pdev_release(cam);
  606. mutex_unlock(&cam->lock);
  607. return ret;
  608. }
  609. static int cam_enum_fmt_vid_cap(struct file *file, void *fh, struct v4l2_fmtdesc *fmt)
  610. {
  611. static struct v4l2_fmtdesc formats[] = {
  612. { 0, 0, 0,
  613. "YUV 4:2:2", V4L2_PIX_FMT_YUYV,
  614. { 0, 0, 0, 0 }
  615. },
  616. };
  617. enum v4l2_buf_type type = fmt->type;
  618. if (fmt->index > 0)
  619. return -EINVAL;
  620. *fmt = formats[fmt->index];
  621. fmt->type = type;
  622. return 0;
  623. }
  624. /* Capture data */
  625. static ssize_t w9966_v4l_read(struct file *file, char __user *buf,
  626. size_t count, loff_t *ppos)
  627. {
  628. struct w9966 *cam = video_drvdata(file);
  629. unsigned char addr = 0xa0; /* ECP, read, CCD-transfer, 00000 */
  630. unsigned char __user *dest = (unsigned char __user *)buf;
  631. unsigned long dleft = count;
  632. unsigned char *tbuf;
  633. /* Why would anyone want more than this?? */
  634. if (count > cam->width * cam->height * 2)
  635. return -EINVAL;
  636. mutex_lock(&cam->lock);
  637. w9966_pdev_claim(cam);
  638. w9966_write_reg(cam, 0x00, 0x02); /* Reset ECP-FIFO buffer */
  639. w9966_write_reg(cam, 0x00, 0x00); /* Return to normal operation */
  640. w9966_write_reg(cam, 0x01, 0x98); /* Enable capture */
  641. /* write special capture-addr and negotiate into data transfer */
  642. if ((parport_negotiate(cam->pport, cam->ppmode|IEEE1284_ADDR) != 0) ||
  643. (parport_write(cam->pport, &addr, 1) != 1) ||
  644. (parport_negotiate(cam->pport, cam->ppmode|IEEE1284_DATA) != 0)) {
  645. w9966_pdev_release(cam);
  646. mutex_unlock(&cam->lock);
  647. return -EFAULT;
  648. }
  649. tbuf = kmalloc(W9966_RBUFFER, GFP_KERNEL);
  650. if (tbuf == NULL) {
  651. count = -ENOMEM;
  652. goto out;
  653. }
  654. while (dleft > 0) {
  655. unsigned long tsize = (dleft > W9966_RBUFFER) ? W9966_RBUFFER : dleft;
  656. if (parport_read(cam->pport, tbuf, tsize) < tsize) {
  657. count = -EFAULT;
  658. goto out;
  659. }
  660. if (copy_to_user(dest, tbuf, tsize) != 0) {
  661. count = -EFAULT;
  662. goto out;
  663. }
  664. dest += tsize;
  665. dleft -= tsize;
  666. }
  667. w9966_write_reg(cam, 0x01, 0x18); /* Disable capture */
  668. out:
  669. kfree(tbuf);
  670. w9966_pdev_release(cam);
  671. mutex_unlock(&cam->lock);
  672. return count;
  673. }
  674. static const struct v4l2_file_operations w9966_fops = {
  675. .owner = THIS_MODULE,
  676. .unlocked_ioctl = video_ioctl2,
  677. .read = w9966_v4l_read,
  678. };
  679. static const struct v4l2_ioctl_ops w9966_ioctl_ops = {
  680. .vidioc_querycap = cam_querycap,
  681. .vidioc_g_input = cam_g_input,
  682. .vidioc_s_input = cam_s_input,
  683. .vidioc_enum_input = cam_enum_input,
  684. .vidioc_queryctrl = cam_queryctrl,
  685. .vidioc_g_ctrl = cam_g_ctrl,
  686. .vidioc_s_ctrl = cam_s_ctrl,
  687. .vidioc_enum_fmt_vid_cap = cam_enum_fmt_vid_cap,
  688. .vidioc_g_fmt_vid_cap = cam_g_fmt_vid_cap,
  689. .vidioc_s_fmt_vid_cap = cam_s_fmt_vid_cap,
  690. .vidioc_try_fmt_vid_cap = cam_try_fmt_vid_cap,
  691. };
  692. /* Initialize camera device. Setup all internal flags, set a
  693. default video mode, setup ccd-chip, register v4l device etc..
  694. Also used for 'probing' of hardware.
  695. -1 on error */
  696. static int w9966_init(struct w9966 *cam, struct parport *port)
  697. {
  698. struct v4l2_device *v4l2_dev = &cam->v4l2_dev;
  699. if (cam->dev_state != 0)
  700. return -1;
  701. strlcpy(v4l2_dev->name, "w9966", sizeof(v4l2_dev->name));
  702. if (v4l2_device_register(NULL, v4l2_dev) < 0) {
  703. v4l2_err(v4l2_dev, "Could not register v4l2_device\n");
  704. return -1;
  705. }
  706. cam->pport = port;
  707. cam->brightness = 128;
  708. cam->contrast = 64;
  709. cam->color = 64;
  710. cam->hue = 0;
  711. /* Select requested transfer mode */
  712. switch (parmode) {
  713. default: /* Auto-detect (priority: hw-ecp, hw-epp, sw-ecp) */
  714. case 0:
  715. if (port->modes & PARPORT_MODE_ECP)
  716. cam->ppmode = IEEE1284_MODE_ECP;
  717. else if (port->modes & PARPORT_MODE_EPP)
  718. cam->ppmode = IEEE1284_MODE_EPP;
  719. else
  720. cam->ppmode = IEEE1284_MODE_ECP;
  721. break;
  722. case 1: /* hw- or sw-ecp */
  723. cam->ppmode = IEEE1284_MODE_ECP;
  724. break;
  725. case 2: /* hw- or sw-epp */
  726. cam->ppmode = IEEE1284_MODE_EPP;
  727. break;
  728. }
  729. /* Tell the parport driver that we exists */
  730. cam->pdev = parport_register_device(port, "w9966", NULL, NULL, NULL, 0, NULL);
  731. if (cam->pdev == NULL) {
  732. DPRINTF("parport_register_device() failed\n");
  733. return -1;
  734. }
  735. w9966_set_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV);
  736. w9966_pdev_claim(cam);
  737. /* Setup a default capture mode */
  738. if (w9966_setup(cam, 0, 0, 1023, 1023, 200, 160) != 0) {
  739. DPRINTF("w9966_setup() failed.\n");
  740. return -1;
  741. }
  742. w9966_pdev_release(cam);
  743. /* Fill in the video_device struct and register us to v4l */
  744. strlcpy(cam->vdev.name, W9966_DRIVERNAME, sizeof(cam->vdev.name));
  745. cam->vdev.v4l2_dev = v4l2_dev;
  746. cam->vdev.fops = &w9966_fops;
  747. cam->vdev.ioctl_ops = &w9966_ioctl_ops;
  748. cam->vdev.release = video_device_release_empty;
  749. video_set_drvdata(&cam->vdev, cam);
  750. mutex_init(&cam->lock);
  751. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, video_nr) < 0)
  752. return -1;
  753. w9966_set_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV);
  754. /* All ok */
  755. v4l2_info(v4l2_dev, "Found and initialized a webcam on %s.\n",
  756. cam->pport->name);
  757. return 0;
  758. }
  759. /* Terminate everything gracefully */
  760. static void w9966_term(struct w9966 *cam)
  761. {
  762. /* Unregister from v4l */
  763. if (w9966_get_state(cam, W9966_STATE_VDEV, W9966_STATE_VDEV)) {
  764. video_unregister_device(&cam->vdev);
  765. w9966_set_state(cam, W9966_STATE_VDEV, 0);
  766. }
  767. /* Terminate from IEEE1284 mode and release pdev block */
  768. if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
  769. w9966_pdev_claim(cam);
  770. parport_negotiate(cam->pport, IEEE1284_MODE_COMPAT);
  771. w9966_pdev_release(cam);
  772. }
  773. /* Unregister from parport */
  774. if (w9966_get_state(cam, W9966_STATE_PDEV, W9966_STATE_PDEV)) {
  775. parport_unregister_device(cam->pdev);
  776. w9966_set_state(cam, W9966_STATE_PDEV, 0);
  777. }
  778. memset(cam, 0, sizeof(*cam));
  779. }
  780. /* Called once for every parport on init */
  781. static void w9966_attach(struct parport *port)
  782. {
  783. int i;
  784. for (i = 0; i < W9966_MAXCAMS; i++) {
  785. if (w9966_cams[i].dev_state != 0) /* Cam is already assigned */
  786. continue;
  787. if (strcmp(pardev[i], "aggressive") == 0 || strcmp(pardev[i], port->name) == 0) {
  788. if (w9966_init(&w9966_cams[i], port) != 0)
  789. w9966_term(&w9966_cams[i]);
  790. break; /* return */
  791. }
  792. }
  793. }
  794. /* Called once for every parport on termination */
  795. static void w9966_detach(struct parport *port)
  796. {
  797. int i;
  798. for (i = 0; i < W9966_MAXCAMS; i++)
  799. if (w9966_cams[i].dev_state != 0 && w9966_cams[i].pport == port)
  800. w9966_term(&w9966_cams[i]);
  801. }
  802. static struct parport_driver w9966_ppd = {
  803. .name = W9966_DRIVERNAME,
  804. .attach = w9966_attach,
  805. .detach = w9966_detach,
  806. };
  807. /* Module entry point */
  808. static int __init w9966_mod_init(void)
  809. {
  810. int i;
  811. for (i = 0; i < W9966_MAXCAMS; i++)
  812. w9966_cams[i].dev_state = 0;
  813. return parport_register_driver(&w9966_ppd);
  814. }
  815. /* Module cleanup */
  816. static void __exit w9966_mod_term(void)
  817. {
  818. parport_unregister_driver(&w9966_ppd);
  819. }
  820. module_init(w9966_mod_init);
  821. module_exit(w9966_mod_term);