c-qcam.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. /*
  2. * Video4Linux Colour QuickCam driver
  3. * Copyright 1997-2000 Philip Blundell <philb@gnu.org>
  4. *
  5. * Module parameters:
  6. *
  7. * parport=auto -- probe all parports (default)
  8. * parport=0 -- parport0 becomes qcam1
  9. * parport=2,0,1 -- parports 2,0,1 are tried in that order
  10. *
  11. * probe=0 -- do no probing, assume camera is present
  12. * probe=1 -- use IEEE-1284 autoprobe data only (default)
  13. * probe=2 -- probe aggressively for cameras
  14. *
  15. * force_rgb=1 -- force data format to RGB (default is BGR)
  16. *
  17. * The parport parameter controls which parports will be scanned.
  18. * Scanning all parports causes some printers to print a garbage page.
  19. * -- March 14, 1999 Billy Donahue <billy@escape.com>
  20. *
  21. * Fixed data format to BGR, added force_rgb parameter. Added missing
  22. * parport_unregister_driver() on module removal.
  23. * -- May 28, 2000 Claudio Matsuoka <claudio@conectiva.com>
  24. */
  25. #include <linux/module.h>
  26. #include <linux/delay.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/init.h>
  30. #include <linux/kernel.h>
  31. #include <linux/slab.h>
  32. #include <linux/mm.h>
  33. #include <linux/parport.h>
  34. #include <linux/sched.h>
  35. #include <linux/videodev.h>
  36. #include <media/v4l2-common.h>
  37. #include <media/v4l2-ioctl.h>
  38. #include <linux/mutex.h>
  39. #include <linux/jiffies.h>
  40. #include <asm/uaccess.h>
  41. struct qcam_device {
  42. struct video_device vdev;
  43. struct pardevice *pdev;
  44. struct parport *pport;
  45. int width, height;
  46. int ccd_width, ccd_height;
  47. int mode;
  48. int contrast, brightness, whitebal;
  49. int top, left;
  50. unsigned int bidirectional;
  51. unsigned long in_use;
  52. struct mutex lock;
  53. };
  54. /* cameras maximum */
  55. #define MAX_CAMS 4
  56. /* The three possible QuickCam modes */
  57. #define QC_MILLIONS 0x18
  58. #define QC_BILLIONS 0x10
  59. #define QC_THOUSANDS 0x08 /* with VIDEC compression (not supported) */
  60. /* The three possible decimations */
  61. #define QC_DECIMATION_1 0
  62. #define QC_DECIMATION_2 2
  63. #define QC_DECIMATION_4 4
  64. #define BANNER "Colour QuickCam for Video4Linux v0.05"
  65. static int parport[MAX_CAMS] = { [1 ... MAX_CAMS-1] = -1 };
  66. static int probe = 2;
  67. static int force_rgb;
  68. static int video_nr = -1;
  69. static inline void qcam_set_ack(struct qcam_device *qcam, unsigned int i)
  70. {
  71. /* note: the QC specs refer to the PCAck pin by voltage, not
  72. software level. PC ports have builtin inverters. */
  73. parport_frob_control(qcam->pport, 8, i?8:0);
  74. }
  75. static inline unsigned int qcam_ready1(struct qcam_device *qcam)
  76. {
  77. return (parport_read_status(qcam->pport) & 0x8)?1:0;
  78. }
  79. static inline unsigned int qcam_ready2(struct qcam_device *qcam)
  80. {
  81. return (parport_read_data(qcam->pport) & 0x1)?1:0;
  82. }
  83. static unsigned int qcam_await_ready1(struct qcam_device *qcam,
  84. int value)
  85. {
  86. unsigned long oldjiffies = jiffies;
  87. unsigned int i;
  88. for (oldjiffies = jiffies;
  89. time_before(jiffies, oldjiffies + msecs_to_jiffies(40)); )
  90. if (qcam_ready1(qcam) == value)
  91. return 0;
  92. /* If the camera didn't respond within 1/25 second, poll slowly
  93. for a while. */
  94. for (i = 0; i < 50; i++)
  95. {
  96. if (qcam_ready1(qcam) == value)
  97. return 0;
  98. msleep_interruptible(100);
  99. }
  100. /* Probably somebody pulled the plug out. Not much we can do. */
  101. printk(KERN_ERR "c-qcam: ready1 timeout (%d) %x %x\n", value,
  102. parport_read_status(qcam->pport),
  103. parport_read_control(qcam->pport));
  104. return 1;
  105. }
  106. static unsigned int qcam_await_ready2(struct qcam_device *qcam, int value)
  107. {
  108. unsigned long oldjiffies = jiffies;
  109. unsigned int i;
  110. for (oldjiffies = jiffies;
  111. time_before(jiffies, oldjiffies + msecs_to_jiffies(40)); )
  112. if (qcam_ready2(qcam) == value)
  113. return 0;
  114. /* If the camera didn't respond within 1/25 second, poll slowly
  115. for a while. */
  116. for (i = 0; i < 50; i++)
  117. {
  118. if (qcam_ready2(qcam) == value)
  119. return 0;
  120. msleep_interruptible(100);
  121. }
  122. /* Probably somebody pulled the plug out. Not much we can do. */
  123. printk(KERN_ERR "c-qcam: ready2 timeout (%d) %x %x %x\n", value,
  124. parport_read_status(qcam->pport),
  125. parport_read_control(qcam->pport),
  126. parport_read_data(qcam->pport));
  127. return 1;
  128. }
  129. static int qcam_read_data(struct qcam_device *qcam)
  130. {
  131. unsigned int idata;
  132. qcam_set_ack(qcam, 0);
  133. if (qcam_await_ready1(qcam, 1)) return -1;
  134. idata = parport_read_status(qcam->pport) & 0xf0;
  135. qcam_set_ack(qcam, 1);
  136. if (qcam_await_ready1(qcam, 0)) return -1;
  137. idata |= (parport_read_status(qcam->pport) >> 4);
  138. return idata;
  139. }
  140. static int qcam_write_data(struct qcam_device *qcam, unsigned int data)
  141. {
  142. unsigned int idata;
  143. parport_write_data(qcam->pport, data);
  144. idata = qcam_read_data(qcam);
  145. if (data != idata)
  146. {
  147. printk(KERN_WARNING "cqcam: sent %x but received %x\n", data,
  148. idata);
  149. return 1;
  150. }
  151. return 0;
  152. }
  153. static inline int qcam_set(struct qcam_device *qcam, unsigned int cmd, unsigned int data)
  154. {
  155. if (qcam_write_data(qcam, cmd))
  156. return -1;
  157. if (qcam_write_data(qcam, data))
  158. return -1;
  159. return 0;
  160. }
  161. static inline int qcam_get(struct qcam_device *qcam, unsigned int cmd)
  162. {
  163. if (qcam_write_data(qcam, cmd))
  164. return -1;
  165. return qcam_read_data(qcam);
  166. }
  167. static int qc_detect(struct qcam_device *qcam)
  168. {
  169. unsigned int stat, ostat, i, count = 0;
  170. /* The probe routine below is not very reliable. The IEEE-1284
  171. probe takes precedence. */
  172. /* XXX Currently parport provides no way to distinguish between
  173. "the IEEE probe was not done" and "the probe was done, but
  174. no device was found". Fix this one day. */
  175. if (qcam->pport->probe_info[0].class == PARPORT_CLASS_MEDIA
  176. && qcam->pport->probe_info[0].model
  177. && !strcmp(qcam->pdev->port->probe_info[0].model,
  178. "Color QuickCam 2.0")) {
  179. printk(KERN_DEBUG "QuickCam: Found by IEEE1284 probe.\n");
  180. return 1;
  181. }
  182. if (probe < 2)
  183. return 0;
  184. parport_write_control(qcam->pport, 0xc);
  185. /* look for a heartbeat */
  186. ostat = stat = parport_read_status(qcam->pport);
  187. for (i=0; i<250; i++)
  188. {
  189. mdelay(1);
  190. stat = parport_read_status(qcam->pport);
  191. if (ostat != stat)
  192. {
  193. if (++count >= 3) return 1;
  194. ostat = stat;
  195. }
  196. }
  197. /* Reset the camera and try again */
  198. parport_write_control(qcam->pport, 0xc);
  199. parport_write_control(qcam->pport, 0x8);
  200. mdelay(1);
  201. parport_write_control(qcam->pport, 0xc);
  202. mdelay(1);
  203. count = 0;
  204. ostat = stat = parport_read_status(qcam->pport);
  205. for (i=0; i<250; i++)
  206. {
  207. mdelay(1);
  208. stat = parport_read_status(qcam->pport);
  209. if (ostat != stat)
  210. {
  211. if (++count >= 3) return 1;
  212. ostat = stat;
  213. }
  214. }
  215. /* no (or flatline) camera, give up */
  216. return 0;
  217. }
  218. static void qc_reset(struct qcam_device *qcam)
  219. {
  220. parport_write_control(qcam->pport, 0xc);
  221. parport_write_control(qcam->pport, 0x8);
  222. mdelay(1);
  223. parport_write_control(qcam->pport, 0xc);
  224. mdelay(1);
  225. }
  226. /* Reset the QuickCam and program for brightness, contrast,
  227. * white-balance, and resolution. */
  228. static void qc_setup(struct qcam_device *q)
  229. {
  230. qc_reset(q);
  231. /* Set the brightness. */
  232. qcam_set(q, 11, q->brightness);
  233. /* Set the height and width. These refer to the actual
  234. CCD area *before* applying the selected decimation. */
  235. qcam_set(q, 17, q->ccd_height);
  236. qcam_set(q, 19, q->ccd_width / 2);
  237. /* Set top and left. */
  238. qcam_set(q, 0xd, q->top);
  239. qcam_set(q, 0xf, q->left);
  240. /* Set contrast and white balance. */
  241. qcam_set(q, 0x19, q->contrast);
  242. qcam_set(q, 0x1f, q->whitebal);
  243. /* Set the speed. */
  244. qcam_set(q, 45, 2);
  245. }
  246. /* Read some bytes from the camera and put them in the buffer.
  247. nbytes should be a multiple of 3, because bidirectional mode gives
  248. us three bytes at a time. */
  249. static unsigned int qcam_read_bytes(struct qcam_device *q, unsigned char *buf, unsigned int nbytes)
  250. {
  251. unsigned int bytes = 0;
  252. qcam_set_ack(q, 0);
  253. if (q->bidirectional)
  254. {
  255. /* It's a bidirectional port */
  256. while (bytes < nbytes)
  257. {
  258. unsigned int lo1, hi1, lo2, hi2;
  259. unsigned char r, g, b;
  260. if (qcam_await_ready2(q, 1)) return bytes;
  261. lo1 = parport_read_data(q->pport) >> 1;
  262. hi1 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
  263. qcam_set_ack(q, 1);
  264. if (qcam_await_ready2(q, 0)) return bytes;
  265. lo2 = parport_read_data(q->pport) >> 1;
  266. hi2 = ((parport_read_status(q->pport) >> 3) & 0x1f) ^ 0x10;
  267. qcam_set_ack(q, 0);
  268. r = (lo1 | ((hi1 & 1)<<7));
  269. g = ((hi1 & 0x1e)<<3) | ((hi2 & 0x1e)>>1);
  270. b = (lo2 | ((hi2 & 1)<<7));
  271. if (force_rgb) {
  272. buf[bytes++] = r;
  273. buf[bytes++] = g;
  274. buf[bytes++] = b;
  275. } else {
  276. buf[bytes++] = b;
  277. buf[bytes++] = g;
  278. buf[bytes++] = r;
  279. }
  280. }
  281. }
  282. else
  283. {
  284. /* It's a unidirectional port */
  285. int i = 0, n = bytes;
  286. unsigned char rgb[3];
  287. while (bytes < nbytes)
  288. {
  289. unsigned int hi, lo;
  290. if (qcam_await_ready1(q, 1)) return bytes;
  291. hi = (parport_read_status(q->pport) & 0xf0);
  292. qcam_set_ack(q, 1);
  293. if (qcam_await_ready1(q, 0)) return bytes;
  294. lo = (parport_read_status(q->pport) & 0xf0);
  295. qcam_set_ack(q, 0);
  296. /* flip some bits */
  297. rgb[(i = bytes++ % 3)] = (hi | (lo >> 4)) ^ 0x88;
  298. if (i >= 2) {
  299. get_fragment:
  300. if (force_rgb) {
  301. buf[n++] = rgb[0];
  302. buf[n++] = rgb[1];
  303. buf[n++] = rgb[2];
  304. } else {
  305. buf[n++] = rgb[2];
  306. buf[n++] = rgb[1];
  307. buf[n++] = rgb[0];
  308. }
  309. }
  310. }
  311. if (i) {
  312. i = 0;
  313. goto get_fragment;
  314. }
  315. }
  316. return bytes;
  317. }
  318. #define BUFSZ 150
  319. static long qc_capture(struct qcam_device *q, char __user *buf, unsigned long len)
  320. {
  321. unsigned lines, pixelsperline, bitsperxfer;
  322. unsigned int is_bi_dir = q->bidirectional;
  323. size_t wantlen, outptr = 0;
  324. char tmpbuf[BUFSZ];
  325. if (!access_ok(VERIFY_WRITE, buf, len))
  326. return -EFAULT;
  327. /* Wait for camera to become ready */
  328. for (;;)
  329. {
  330. int i = qcam_get(q, 41);
  331. if (i == -1) {
  332. qc_setup(q);
  333. return -EIO;
  334. }
  335. if ((i & 0x80) == 0)
  336. break;
  337. else
  338. schedule();
  339. }
  340. if (qcam_set(q, 7, (q->mode | (is_bi_dir?1:0)) + 1))
  341. return -EIO;
  342. lines = q->height;
  343. pixelsperline = q->width;
  344. bitsperxfer = (is_bi_dir) ? 24 : 8;
  345. if (is_bi_dir)
  346. {
  347. /* Turn the port around */
  348. parport_data_reverse(q->pport);
  349. mdelay(3);
  350. qcam_set_ack(q, 0);
  351. if (qcam_await_ready1(q, 1)) {
  352. qc_setup(q);
  353. return -EIO;
  354. }
  355. qcam_set_ack(q, 1);
  356. if (qcam_await_ready1(q, 0)) {
  357. qc_setup(q);
  358. return -EIO;
  359. }
  360. }
  361. wantlen = lines * pixelsperline * 24 / 8;
  362. while (wantlen)
  363. {
  364. size_t t, s;
  365. s = (wantlen > BUFSZ)?BUFSZ:wantlen;
  366. t = qcam_read_bytes(q, tmpbuf, s);
  367. if (outptr < len)
  368. {
  369. size_t sz = len - outptr;
  370. if (sz > t) sz = t;
  371. if (__copy_to_user(buf+outptr, tmpbuf, sz))
  372. break;
  373. outptr += sz;
  374. }
  375. wantlen -= t;
  376. if (t < s)
  377. break;
  378. cond_resched();
  379. }
  380. len = outptr;
  381. if (wantlen)
  382. {
  383. printk("qcam: short read.\n");
  384. if (is_bi_dir)
  385. parport_data_forward(q->pport);
  386. qc_setup(q);
  387. return len;
  388. }
  389. if (is_bi_dir)
  390. {
  391. int l;
  392. do {
  393. l = qcam_read_bytes(q, tmpbuf, 3);
  394. cond_resched();
  395. } while (l && (tmpbuf[0] == 0x7e || tmpbuf[1] == 0x7e || tmpbuf[2] == 0x7e));
  396. if (force_rgb) {
  397. if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
  398. printk("qcam: bad EOF\n");
  399. } else {
  400. if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xe)
  401. printk("qcam: bad EOF\n");
  402. }
  403. qcam_set_ack(q, 0);
  404. if (qcam_await_ready1(q, 1))
  405. {
  406. printk("qcam: no ack after EOF\n");
  407. parport_data_forward(q->pport);
  408. qc_setup(q);
  409. return len;
  410. }
  411. parport_data_forward(q->pport);
  412. mdelay(3);
  413. qcam_set_ack(q, 1);
  414. if (qcam_await_ready1(q, 0))
  415. {
  416. printk("qcam: no ack to port turnaround\n");
  417. qc_setup(q);
  418. return len;
  419. }
  420. }
  421. else
  422. {
  423. int l;
  424. do {
  425. l = qcam_read_bytes(q, tmpbuf, 1);
  426. cond_resched();
  427. } while (l && tmpbuf[0] == 0x7e);
  428. l = qcam_read_bytes(q, tmpbuf+1, 2);
  429. if (force_rgb) {
  430. if (tmpbuf[0] != 0xe || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xf)
  431. printk("qcam: bad EOF\n");
  432. } else {
  433. if (tmpbuf[0] != 0xf || tmpbuf[1] != 0x0 || tmpbuf[2] != 0xe)
  434. printk("qcam: bad EOF\n");
  435. }
  436. }
  437. qcam_write_data(q, 0);
  438. return len;
  439. }
  440. /*
  441. * Video4linux interfacing
  442. */
  443. static long qcam_do_ioctl(struct file *file, unsigned int cmd, void *arg)
  444. {
  445. struct video_device *dev = video_devdata(file);
  446. struct qcam_device *qcam=(struct qcam_device *)dev;
  447. switch(cmd)
  448. {
  449. case VIDIOCGCAP:
  450. {
  451. struct video_capability *b = arg;
  452. strcpy(b->name, "Quickcam");
  453. b->type = VID_TYPE_CAPTURE|VID_TYPE_SCALES;
  454. b->channels = 1;
  455. b->audios = 0;
  456. b->maxwidth = 320;
  457. b->maxheight = 240;
  458. b->minwidth = 80;
  459. b->minheight = 60;
  460. return 0;
  461. }
  462. case VIDIOCGCHAN:
  463. {
  464. struct video_channel *v = arg;
  465. if(v->channel!=0)
  466. return -EINVAL;
  467. v->flags=0;
  468. v->tuners=0;
  469. /* Good question.. its composite or SVHS so.. */
  470. v->type = VIDEO_TYPE_CAMERA;
  471. strcpy(v->name, "Camera");
  472. return 0;
  473. }
  474. case VIDIOCSCHAN:
  475. {
  476. struct video_channel *v = arg;
  477. if(v->channel!=0)
  478. return -EINVAL;
  479. return 0;
  480. }
  481. case VIDIOCGTUNER:
  482. {
  483. struct video_tuner *v = arg;
  484. if(v->tuner)
  485. return -EINVAL;
  486. memset(v,0,sizeof(*v));
  487. strcpy(v->name, "Format");
  488. v->mode = VIDEO_MODE_AUTO;
  489. return 0;
  490. }
  491. case VIDIOCSTUNER:
  492. {
  493. struct video_tuner *v = arg;
  494. if(v->tuner)
  495. return -EINVAL;
  496. if(v->mode!=VIDEO_MODE_AUTO)
  497. return -EINVAL;
  498. return 0;
  499. }
  500. case VIDIOCGPICT:
  501. {
  502. struct video_picture *p = arg;
  503. p->colour=0x8000;
  504. p->hue=0x8000;
  505. p->brightness=qcam->brightness<<8;
  506. p->contrast=qcam->contrast<<8;
  507. p->whiteness=qcam->whitebal<<8;
  508. p->depth=24;
  509. p->palette=VIDEO_PALETTE_RGB24;
  510. return 0;
  511. }
  512. case VIDIOCSPICT:
  513. {
  514. struct video_picture *p = arg;
  515. /*
  516. * Sanity check args
  517. */
  518. if (p->depth != 24 || p->palette != VIDEO_PALETTE_RGB24)
  519. return -EINVAL;
  520. /*
  521. * Now load the camera.
  522. */
  523. qcam->brightness = p->brightness>>8;
  524. qcam->contrast = p->contrast>>8;
  525. qcam->whitebal = p->whiteness>>8;
  526. mutex_lock(&qcam->lock);
  527. parport_claim_or_block(qcam->pdev);
  528. qc_setup(qcam);
  529. parport_release(qcam->pdev);
  530. mutex_unlock(&qcam->lock);
  531. return 0;
  532. }
  533. case VIDIOCSWIN:
  534. {
  535. struct video_window *vw = arg;
  536. if(vw->flags)
  537. return -EINVAL;
  538. if(vw->clipcount)
  539. return -EINVAL;
  540. if(vw->height<60||vw->height>240)
  541. return -EINVAL;
  542. if(vw->width<80||vw->width>320)
  543. return -EINVAL;
  544. qcam->width = 80;
  545. qcam->height = 60;
  546. qcam->mode = QC_DECIMATION_4;
  547. if(vw->width>=160 && vw->height>=120)
  548. {
  549. qcam->width = 160;
  550. qcam->height = 120;
  551. qcam->mode = QC_DECIMATION_2;
  552. }
  553. if(vw->width>=320 && vw->height>=240)
  554. {
  555. qcam->width = 320;
  556. qcam->height = 240;
  557. qcam->mode = QC_DECIMATION_1;
  558. }
  559. qcam->mode |= QC_MILLIONS;
  560. #if 0
  561. if(vw->width>=640 && vw->height>=480)
  562. {
  563. qcam->width = 640;
  564. qcam->height = 480;
  565. qcam->mode = QC_BILLIONS | QC_DECIMATION_1;
  566. }
  567. #endif
  568. /* Ok we figured out what to use from our
  569. wide choice */
  570. mutex_lock(&qcam->lock);
  571. parport_claim_or_block(qcam->pdev);
  572. qc_setup(qcam);
  573. parport_release(qcam->pdev);
  574. mutex_unlock(&qcam->lock);
  575. return 0;
  576. }
  577. case VIDIOCGWIN:
  578. {
  579. struct video_window *vw = arg;
  580. memset(vw, 0, sizeof(*vw));
  581. vw->width=qcam->width;
  582. vw->height=qcam->height;
  583. return 0;
  584. }
  585. case VIDIOCKEY:
  586. return 0;
  587. case VIDIOCCAPTURE:
  588. case VIDIOCGFBUF:
  589. case VIDIOCSFBUF:
  590. case VIDIOCGFREQ:
  591. case VIDIOCSFREQ:
  592. case VIDIOCGAUDIO:
  593. case VIDIOCSAUDIO:
  594. return -EINVAL;
  595. default:
  596. return -ENOIOCTLCMD;
  597. }
  598. return 0;
  599. }
  600. static long qcam_ioctl(struct file *file,
  601. unsigned int cmd, unsigned long arg)
  602. {
  603. return video_usercopy(file, cmd, arg, qcam_do_ioctl);
  604. }
  605. static ssize_t qcam_read(struct file *file, char __user *buf,
  606. size_t count, loff_t *ppos)
  607. {
  608. struct video_device *v = video_devdata(file);
  609. struct qcam_device *qcam=(struct qcam_device *)v;
  610. int len;
  611. mutex_lock(&qcam->lock);
  612. parport_claim_or_block(qcam->pdev);
  613. /* Probably should have a semaphore against multiple users */
  614. len = qc_capture(qcam, buf,count);
  615. parport_release(qcam->pdev);
  616. mutex_unlock(&qcam->lock);
  617. return len;
  618. }
  619. static int qcam_exclusive_open(struct file *file)
  620. {
  621. struct video_device *dev = video_devdata(file);
  622. struct qcam_device *qcam = (struct qcam_device *)dev;
  623. return test_and_set_bit(0, &qcam->in_use) ? -EBUSY : 0;
  624. }
  625. static int qcam_exclusive_release(struct file *file)
  626. {
  627. struct video_device *dev = video_devdata(file);
  628. struct qcam_device *qcam = (struct qcam_device *)dev;
  629. clear_bit(0, &qcam->in_use);
  630. return 0;
  631. }
  632. /* video device template */
  633. static const struct v4l2_file_operations qcam_fops = {
  634. .owner = THIS_MODULE,
  635. .open = qcam_exclusive_open,
  636. .release = qcam_exclusive_release,
  637. .ioctl = qcam_ioctl,
  638. .read = qcam_read,
  639. };
  640. static struct video_device qcam_template=
  641. {
  642. .name = "Colour QuickCam",
  643. .fops = &qcam_fops,
  644. .release = video_device_release_empty,
  645. };
  646. /* Initialize the QuickCam driver control structure. */
  647. static struct qcam_device *qcam_init(struct parport *port)
  648. {
  649. struct qcam_device *q;
  650. q = kmalloc(sizeof(struct qcam_device), GFP_KERNEL);
  651. if(q==NULL)
  652. return NULL;
  653. q->pport = port;
  654. q->pdev = parport_register_device(port, "c-qcam", NULL, NULL,
  655. NULL, 0, NULL);
  656. q->bidirectional = (q->pport->modes & PARPORT_MODE_TRISTATE)?1:0;
  657. if (q->pdev == NULL)
  658. {
  659. printk(KERN_ERR "c-qcam: couldn't register for %s.\n",
  660. port->name);
  661. kfree(q);
  662. return NULL;
  663. }
  664. memcpy(&q->vdev, &qcam_template, sizeof(qcam_template));
  665. mutex_init(&q->lock);
  666. q->width = q->ccd_width = 320;
  667. q->height = q->ccd_height = 240;
  668. q->mode = QC_MILLIONS | QC_DECIMATION_1;
  669. q->contrast = 192;
  670. q->brightness = 240;
  671. q->whitebal = 128;
  672. q->top = 1;
  673. q->left = 14;
  674. return q;
  675. }
  676. static struct qcam_device *qcams[MAX_CAMS];
  677. static unsigned int num_cams;
  678. static int init_cqcam(struct parport *port)
  679. {
  680. struct qcam_device *qcam;
  681. if (parport[0] != -1)
  682. {
  683. /* The user gave specific instructions */
  684. int i, found = 0;
  685. for (i = 0; i < MAX_CAMS && parport[i] != -1; i++)
  686. {
  687. if (parport[0] == port->number)
  688. found = 1;
  689. }
  690. if (!found)
  691. return -ENODEV;
  692. }
  693. if (num_cams == MAX_CAMS)
  694. return -ENOSPC;
  695. qcam = qcam_init(port);
  696. if (qcam==NULL)
  697. return -ENODEV;
  698. parport_claim_or_block(qcam->pdev);
  699. qc_reset(qcam);
  700. if (probe && qc_detect(qcam)==0)
  701. {
  702. parport_release(qcam->pdev);
  703. parport_unregister_device(qcam->pdev);
  704. kfree(qcam);
  705. return -ENODEV;
  706. }
  707. qc_setup(qcam);
  708. parport_release(qcam->pdev);
  709. if (video_register_device(&qcam->vdev, VFL_TYPE_GRABBER, video_nr) < 0) {
  710. printk(KERN_ERR "Unable to register Colour QuickCam on %s\n",
  711. qcam->pport->name);
  712. parport_unregister_device(qcam->pdev);
  713. kfree(qcam);
  714. return -ENODEV;
  715. }
  716. printk(KERN_INFO "video%d: Colour QuickCam found on %s\n",
  717. qcam->vdev.num, qcam->pport->name);
  718. qcams[num_cams++] = qcam;
  719. return 0;
  720. }
  721. static void close_cqcam(struct qcam_device *qcam)
  722. {
  723. video_unregister_device(&qcam->vdev);
  724. parport_unregister_device(qcam->pdev);
  725. kfree(qcam);
  726. }
  727. static void cq_attach(struct parport *port)
  728. {
  729. init_cqcam(port);
  730. }
  731. static void cq_detach(struct parport *port)
  732. {
  733. /* Write this some day. */
  734. }
  735. static struct parport_driver cqcam_driver = {
  736. .name = "cqcam",
  737. .attach = cq_attach,
  738. .detach = cq_detach,
  739. };
  740. static int __init cqcam_init (void)
  741. {
  742. printk(BANNER "\n");
  743. return parport_register_driver(&cqcam_driver);
  744. }
  745. static void __exit cqcam_cleanup (void)
  746. {
  747. unsigned int i;
  748. for (i = 0; i < num_cams; i++)
  749. close_cqcam(qcams[i]);
  750. parport_unregister_driver(&cqcam_driver);
  751. }
  752. MODULE_AUTHOR("Philip Blundell <philb@gnu.org>");
  753. MODULE_DESCRIPTION(BANNER);
  754. MODULE_LICENSE("GPL");
  755. /* FIXME: parport=auto would never have worked, surely? --RR */
  756. MODULE_PARM_DESC(parport ,"parport=<auto|n[,n]...> for port detection method\n\
  757. probe=<0|1|2> for camera detection method\n\
  758. force_rgb=<0|1> for RGB data format (default BGR)");
  759. module_param_array(parport, int, NULL, 0);
  760. module_param(probe, int, 0);
  761. module_param(force_rgb, bool, 0);
  762. module_param(video_nr, int, 0);
  763. module_init(cqcam_init);
  764. module_exit(cqcam_cleanup);