w9966.c 24 KB

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