w9966.c 24 KB

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