w9966.c 25 KB

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