w9966.c 24 KB

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