vicam.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  1. /*
  2. * USB ViCam WebCam driver
  3. * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
  4. * Christopher L Cheney (ccheney@cheney.cx),
  5. * Pavel Machek (pavel@ucw.cz),
  6. * John Tyner (jtyner@cs.ucr.edu),
  7. * Monroe Williams (monroe@pobox.com)
  8. *
  9. * Supports 3COM HomeConnect PC Digital WebCam
  10. * Supports Compro PS39U WebCam
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * This source code is based heavily on the CPiA webcam driver which was
  27. * written by Peter Pregler, Scott J. Bertin and Johannes Erdfelt
  28. *
  29. * Portions of this code were also copied from usbvideo.c
  30. *
  31. * Special thanks to the whole team at Sourceforge for help making
  32. * this driver become a reality. Notably:
  33. * Andy Armstrong who reverse engineered the color encoding and
  34. * Pavel Machek and Chris Cheney who worked on reverse engineering the
  35. * camera controls and wrote the first generation driver.
  36. */
  37. #include <linux/kernel.h>
  38. #include <linux/module.h>
  39. #include <linux/init.h>
  40. #include <linux/videodev.h>
  41. #include <linux/usb.h>
  42. #include <linux/vmalloc.h>
  43. #include <linux/mm.h>
  44. #include <linux/slab.h>
  45. #include <linux/mutex.h>
  46. #include <linux/firmware.h>
  47. #include <linux/ihex.h>
  48. #include "usbvideo.h"
  49. // #define VICAM_DEBUG
  50. #ifdef VICAM_DEBUG
  51. #define ADBG(lineno,fmt,args...) printk(fmt, jiffies, __func__, lineno, ##args)
  52. #define DBG(fmt,args...) ADBG((__LINE__),KERN_DEBUG __FILE__"(%ld):%s (%d):"fmt,##args)
  53. #else
  54. #define DBG(fmn,args...) do {} while(0)
  55. #endif
  56. #define DRIVER_AUTHOR "Joe Burks, jburks@wavicle.org"
  57. #define DRIVER_DESC "ViCam WebCam Driver"
  58. /* Define these values to match your device */
  59. #define USB_VICAM_VENDOR_ID 0x04c1
  60. #define USB_VICAM_PRODUCT_ID 0x009d
  61. #define USB_COMPRO_VENDOR_ID 0x0602
  62. #define USB_COMPRO_PRODUCT_ID 0x1001
  63. #define VICAM_BYTES_PER_PIXEL 3
  64. #define VICAM_MAX_READ_SIZE (512*242+128)
  65. #define VICAM_MAX_FRAME_SIZE (VICAM_BYTES_PER_PIXEL*320*240)
  66. #define VICAM_FRAMES 2
  67. #define VICAM_HEADER_SIZE 64
  68. /* rvmalloc / rvfree copied from usbvideo.c
  69. *
  70. * Not sure why these are not yet non-statics which I can reference through
  71. * usbvideo.h the same as it is in 2.4.20. I bet this will get fixed sometime
  72. * in the future.
  73. *
  74. */
  75. static void *rvmalloc(unsigned long size)
  76. {
  77. void *mem;
  78. unsigned long adr;
  79. size = PAGE_ALIGN(size);
  80. mem = vmalloc_32(size);
  81. if (!mem)
  82. return NULL;
  83. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  84. adr = (unsigned long) mem;
  85. while (size > 0) {
  86. SetPageReserved(vmalloc_to_page((void *)adr));
  87. adr += PAGE_SIZE;
  88. size -= PAGE_SIZE;
  89. }
  90. return mem;
  91. }
  92. static void rvfree(void *mem, unsigned long size)
  93. {
  94. unsigned long adr;
  95. if (!mem)
  96. return;
  97. adr = (unsigned long) mem;
  98. while ((long) size > 0) {
  99. ClearPageReserved(vmalloc_to_page((void *)adr));
  100. adr += PAGE_SIZE;
  101. size -= PAGE_SIZE;
  102. }
  103. vfree(mem);
  104. }
  105. struct vicam_camera {
  106. u16 shutter_speed; // capture shutter speed
  107. u16 gain; // capture gain
  108. u8 *raw_image; // raw data captured from the camera
  109. u8 *framebuf; // processed data in RGB24 format
  110. u8 *cntrlbuf; // area used to send control msgs
  111. struct video_device vdev; // v4l video device
  112. struct usb_device *udev; // usb device
  113. /* guard against simultaneous accesses to the camera */
  114. struct mutex cam_lock;
  115. int is_initialized;
  116. u8 open_count;
  117. u8 bulkEndpoint;
  118. int needsDummyRead;
  119. };
  120. static int vicam_probe( struct usb_interface *intf, const struct usb_device_id *id);
  121. static void vicam_disconnect(struct usb_interface *intf);
  122. static void read_frame(struct vicam_camera *cam, int framenum);
  123. static void vicam_decode_color(const u8 *, u8 *);
  124. static int __send_control_msg(struct vicam_camera *cam,
  125. u8 request,
  126. u16 value,
  127. u16 index,
  128. unsigned char *cp,
  129. u16 size)
  130. {
  131. int status;
  132. /* cp must be memory that has been allocated by kmalloc */
  133. status = usb_control_msg(cam->udev,
  134. usb_sndctrlpipe(cam->udev, 0),
  135. request,
  136. USB_DIR_OUT | USB_TYPE_VENDOR |
  137. USB_RECIP_DEVICE, value, index,
  138. cp, size, 1000);
  139. status = min(status, 0);
  140. if (status < 0) {
  141. printk(KERN_INFO "Failed sending control message, error %d.\n",
  142. status);
  143. }
  144. return status;
  145. }
  146. static int send_control_msg(struct vicam_camera *cam,
  147. u8 request,
  148. u16 value,
  149. u16 index,
  150. unsigned char *cp,
  151. u16 size)
  152. {
  153. int status = -ENODEV;
  154. mutex_lock(&cam->cam_lock);
  155. if (cam->udev) {
  156. status = __send_control_msg(cam, request, value,
  157. index, cp, size);
  158. }
  159. mutex_unlock(&cam->cam_lock);
  160. return status;
  161. }
  162. static int
  163. initialize_camera(struct vicam_camera *cam)
  164. {
  165. int err;
  166. const struct ihex_binrec *rec;
  167. const struct firmware *uninitialized_var(fw);
  168. err = request_ihex_firmware(&fw, "vicam/firmware.fw", &cam->udev->dev);
  169. if (err) {
  170. printk(KERN_ERR "Failed to load \"vicam/firmware.fw\": %d\n",
  171. err);
  172. return err;
  173. }
  174. for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
  175. memcpy(cam->cntrlbuf, rec->data, be16_to_cpu(rec->len));
  176. err = send_control_msg(cam, 0xff, 0, 0,
  177. cam->cntrlbuf, be16_to_cpu(rec->len));
  178. if (err)
  179. break;
  180. }
  181. release_firmware(fw);
  182. return err;
  183. }
  184. static int
  185. set_camera_power(struct vicam_camera *cam, int state)
  186. {
  187. int status;
  188. if ((status = send_control_msg(cam, 0x50, state, 0, NULL, 0)) < 0)
  189. return status;
  190. if (state) {
  191. send_control_msg(cam, 0x55, 1, 0, NULL, 0);
  192. }
  193. return 0;
  194. }
  195. static long
  196. vicam_ioctl(struct file *file, unsigned int ioctlnr, unsigned long arg)
  197. {
  198. void __user *user_arg = (void __user *)arg;
  199. struct vicam_camera *cam = file->private_data;
  200. long retval = 0;
  201. if (!cam)
  202. return -ENODEV;
  203. switch (ioctlnr) {
  204. /* query capabilities */
  205. case VIDIOCGCAP:
  206. {
  207. struct video_capability b;
  208. DBG("VIDIOCGCAP\n");
  209. memset(&b, 0, sizeof(b));
  210. strcpy(b.name, "ViCam-based Camera");
  211. b.type = VID_TYPE_CAPTURE;
  212. b.channels = 1;
  213. b.audios = 0;
  214. b.maxwidth = 320; /* VIDEOSIZE_CIF */
  215. b.maxheight = 240;
  216. b.minwidth = 320; /* VIDEOSIZE_48_48 */
  217. b.minheight = 240;
  218. if (copy_to_user(user_arg, &b, sizeof(b)))
  219. retval = -EFAULT;
  220. break;
  221. }
  222. /* get/set video source - we are a camera and nothing else */
  223. case VIDIOCGCHAN:
  224. {
  225. struct video_channel v;
  226. DBG("VIDIOCGCHAN\n");
  227. if (copy_from_user(&v, user_arg, sizeof(v))) {
  228. retval = -EFAULT;
  229. break;
  230. }
  231. if (v.channel != 0) {
  232. retval = -EINVAL;
  233. break;
  234. }
  235. v.channel = 0;
  236. strcpy(v.name, "Camera");
  237. v.tuners = 0;
  238. v.flags = 0;
  239. v.type = VIDEO_TYPE_CAMERA;
  240. v.norm = 0;
  241. if (copy_to_user(user_arg, &v, sizeof(v)))
  242. retval = -EFAULT;
  243. break;
  244. }
  245. case VIDIOCSCHAN:
  246. {
  247. int v;
  248. if (copy_from_user(&v, user_arg, sizeof(v)))
  249. retval = -EFAULT;
  250. DBG("VIDIOCSCHAN %d\n", v);
  251. if (retval == 0 && v != 0)
  252. retval = -EINVAL;
  253. break;
  254. }
  255. /* image properties */
  256. case VIDIOCGPICT:
  257. {
  258. struct video_picture vp;
  259. DBG("VIDIOCGPICT\n");
  260. memset(&vp, 0, sizeof (struct video_picture));
  261. vp.brightness = cam->gain << 8;
  262. vp.depth = 24;
  263. vp.palette = VIDEO_PALETTE_RGB24;
  264. if (copy_to_user(user_arg, &vp, sizeof (struct video_picture)))
  265. retval = -EFAULT;
  266. break;
  267. }
  268. case VIDIOCSPICT:
  269. {
  270. struct video_picture vp;
  271. if (copy_from_user(&vp, user_arg, sizeof(vp))) {
  272. retval = -EFAULT;
  273. break;
  274. }
  275. DBG("VIDIOCSPICT depth = %d, pal = %d\n", vp.depth,
  276. vp.palette);
  277. cam->gain = vp.brightness >> 8;
  278. if (vp.depth != 24
  279. || vp.palette != VIDEO_PALETTE_RGB24)
  280. retval = -EINVAL;
  281. break;
  282. }
  283. /* get/set capture window */
  284. case VIDIOCGWIN:
  285. {
  286. struct video_window vw;
  287. vw.x = 0;
  288. vw.y = 0;
  289. vw.width = 320;
  290. vw.height = 240;
  291. vw.chromakey = 0;
  292. vw.flags = 0;
  293. vw.clips = NULL;
  294. vw.clipcount = 0;
  295. DBG("VIDIOCGWIN\n");
  296. if (copy_to_user(user_arg, (void *)&vw, sizeof(vw)))
  297. retval = -EFAULT;
  298. // I'm not sure what the deal with a capture window is, it is very poorly described
  299. // in the doc. So I won't support it now.
  300. break;
  301. }
  302. case VIDIOCSWIN:
  303. {
  304. struct video_window vw;
  305. if (copy_from_user(&vw, user_arg, sizeof(vw))) {
  306. retval = -EFAULT;
  307. break;
  308. }
  309. DBG("VIDIOCSWIN %d x %d\n", vw.width, vw.height);
  310. if ( vw.width != 320 || vw.height != 240 )
  311. retval = -EFAULT;
  312. break;
  313. }
  314. /* mmap interface */
  315. case VIDIOCGMBUF:
  316. {
  317. struct video_mbuf vm;
  318. int i;
  319. DBG("VIDIOCGMBUF\n");
  320. memset(&vm, 0, sizeof (vm));
  321. vm.size =
  322. VICAM_MAX_FRAME_SIZE * VICAM_FRAMES;
  323. vm.frames = VICAM_FRAMES;
  324. for (i = 0; i < VICAM_FRAMES; i++)
  325. vm.offsets[i] = VICAM_MAX_FRAME_SIZE * i;
  326. if (copy_to_user(user_arg, (void *)&vm, sizeof(vm)))
  327. retval = -EFAULT;
  328. break;
  329. }
  330. case VIDIOCMCAPTURE:
  331. {
  332. struct video_mmap vm;
  333. // int video_size;
  334. if (copy_from_user((void *)&vm, user_arg, sizeof(vm))) {
  335. retval = -EFAULT;
  336. break;
  337. }
  338. DBG("VIDIOCMCAPTURE frame=%d, height=%d, width=%d, format=%d.\n",vm.frame,vm.width,vm.height,vm.format);
  339. if ( vm.frame >= VICAM_FRAMES || vm.format != VIDEO_PALETTE_RGB24 )
  340. retval = -EINVAL;
  341. // in theory right here we'd start the image capturing
  342. // (fill in a bulk urb and submit it asynchronously)
  343. //
  344. // Instead we're going to do a total hack job for now and
  345. // retrieve the frame in VIDIOCSYNC
  346. break;
  347. }
  348. case VIDIOCSYNC:
  349. {
  350. int frame;
  351. if (copy_from_user((void *)&frame, user_arg, sizeof(int))) {
  352. retval = -EFAULT;
  353. break;
  354. }
  355. DBG("VIDIOCSYNC: %d\n", frame);
  356. read_frame(cam, frame);
  357. vicam_decode_color(cam->raw_image,
  358. cam->framebuf +
  359. frame * VICAM_MAX_FRAME_SIZE );
  360. break;
  361. }
  362. /* pointless to implement overlay with this camera */
  363. case VIDIOCCAPTURE:
  364. case VIDIOCGFBUF:
  365. case VIDIOCSFBUF:
  366. case VIDIOCKEY:
  367. retval = -EINVAL;
  368. break;
  369. /* tuner interface - we have none */
  370. case VIDIOCGTUNER:
  371. case VIDIOCSTUNER:
  372. case VIDIOCGFREQ:
  373. case VIDIOCSFREQ:
  374. retval = -EINVAL;
  375. break;
  376. /* audio interface - we have none */
  377. case VIDIOCGAUDIO:
  378. case VIDIOCSAUDIO:
  379. retval = -EINVAL;
  380. break;
  381. default:
  382. retval = -ENOIOCTLCMD;
  383. break;
  384. }
  385. return retval;
  386. }
  387. static int
  388. vicam_open(struct file *file)
  389. {
  390. struct vicam_camera *cam = video_drvdata(file);
  391. DBG("open\n");
  392. if (!cam) {
  393. printk(KERN_ERR
  394. "vicam video_device improperly initialized");
  395. return -EINVAL;
  396. }
  397. /* cam_lock/open_count protects us from simultaneous opens
  398. * ... for now. we probably shouldn't rely on this fact forever.
  399. */
  400. mutex_lock(&cam->cam_lock);
  401. if (cam->open_count > 0) {
  402. printk(KERN_INFO
  403. "vicam_open called on already opened camera");
  404. mutex_unlock(&cam->cam_lock);
  405. return -EBUSY;
  406. }
  407. cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
  408. if (!cam->raw_image) {
  409. mutex_unlock(&cam->cam_lock);
  410. return -ENOMEM;
  411. }
  412. cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  413. if (!cam->framebuf) {
  414. kfree(cam->raw_image);
  415. mutex_unlock(&cam->cam_lock);
  416. return -ENOMEM;
  417. }
  418. cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  419. if (!cam->cntrlbuf) {
  420. kfree(cam->raw_image);
  421. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  422. mutex_unlock(&cam->cam_lock);
  423. return -ENOMEM;
  424. }
  425. cam->needsDummyRead = 1;
  426. cam->open_count++;
  427. file->private_data = cam;
  428. mutex_unlock(&cam->cam_lock);
  429. // First upload firmware, then turn the camera on
  430. if (!cam->is_initialized) {
  431. initialize_camera(cam);
  432. cam->is_initialized = 1;
  433. }
  434. set_camera_power(cam, 1);
  435. return 0;
  436. }
  437. static int
  438. vicam_close(struct file *file)
  439. {
  440. struct vicam_camera *cam = file->private_data;
  441. int open_count;
  442. struct usb_device *udev;
  443. DBG("close\n");
  444. /* it's not the end of the world if
  445. * we fail to turn the camera off.
  446. */
  447. set_camera_power(cam, 0);
  448. kfree(cam->raw_image);
  449. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  450. kfree(cam->cntrlbuf);
  451. mutex_lock(&cam->cam_lock);
  452. cam->open_count--;
  453. open_count = cam->open_count;
  454. udev = cam->udev;
  455. mutex_unlock(&cam->cam_lock);
  456. if (!open_count && !udev) {
  457. kfree(cam);
  458. }
  459. return 0;
  460. }
  461. static void vicam_decode_color(const u8 *data, u8 *rgb)
  462. {
  463. /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
  464. * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
  465. */
  466. int i, prevY, nextY;
  467. prevY = 512;
  468. nextY = 512;
  469. data += VICAM_HEADER_SIZE;
  470. for( i = 0; i < 240; i++, data += 512 ) {
  471. const int y = ( i * 242 ) / 240;
  472. int j, prevX, nextX;
  473. int Y, Cr, Cb;
  474. if ( y == 242 - 1 ) {
  475. nextY = -512;
  476. }
  477. prevX = 1;
  478. nextX = 1;
  479. for ( j = 0; j < 320; j++, rgb += 3 ) {
  480. const int x = ( j * 512 ) / 320;
  481. const u8 * const src = &data[x];
  482. if ( x == 512 - 1 ) {
  483. nextX = -1;
  484. }
  485. Cr = ( src[prevX] - src[0] ) +
  486. ( src[nextX] - src[0] );
  487. Cr /= 2;
  488. Cb = ( src[prevY] - src[prevX + prevY] ) +
  489. ( src[prevY] - src[nextX + prevY] ) +
  490. ( src[nextY] - src[prevX + nextY] ) +
  491. ( src[nextY] - src[nextX + nextY] );
  492. Cb /= 4;
  493. Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
  494. if ( i & 1 ) {
  495. int Ct = Cr;
  496. Cr = Cb;
  497. Cb = Ct;
  498. }
  499. if ( ( x ^ i ) & 1 ) {
  500. Cr = -Cr;
  501. Cb = -Cb;
  502. }
  503. rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
  504. 500 ) / 900, 0, 255 );
  505. rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
  506. ( 813 * Cr ) ) +
  507. 500 ) / 1000, 0, 255 );
  508. rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
  509. 500 ) / 1300, 0, 255 );
  510. prevX = -1;
  511. }
  512. prevY = -512;
  513. }
  514. }
  515. static void
  516. read_frame(struct vicam_camera *cam, int framenum)
  517. {
  518. unsigned char *request = cam->cntrlbuf;
  519. int realShutter;
  520. int n;
  521. int actual_length;
  522. if (cam->needsDummyRead) {
  523. cam->needsDummyRead = 0;
  524. read_frame(cam, framenum);
  525. }
  526. memset(request, 0, 16);
  527. request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
  528. request[1] = 0; // 512x242 capture
  529. request[2] = 0x90; // the function of these two bytes
  530. request[3] = 0x07; // is not yet understood
  531. if (cam->shutter_speed > 60) {
  532. // Short exposure
  533. realShutter =
  534. ((-15631900 / cam->shutter_speed) + 260533) / 1000;
  535. request[4] = realShutter & 0xFF;
  536. request[5] = (realShutter >> 8) & 0xFF;
  537. request[6] = 0x03;
  538. request[7] = 0x01;
  539. } else {
  540. // Long exposure
  541. realShutter = 15600 / cam->shutter_speed - 1;
  542. request[4] = 0;
  543. request[5] = 0;
  544. request[6] = realShutter & 0xFF;
  545. request[7] = realShutter >> 8;
  546. }
  547. // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
  548. request[8] = 0;
  549. // bytes 9-15 do not seem to affect exposure or image quality
  550. mutex_lock(&cam->cam_lock);
  551. if (!cam->udev) {
  552. goto done;
  553. }
  554. n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
  555. if (n < 0) {
  556. printk(KERN_ERR
  557. " Problem sending frame capture control message");
  558. goto done;
  559. }
  560. n = usb_bulk_msg(cam->udev,
  561. usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
  562. cam->raw_image,
  563. 512 * 242 + 128, &actual_length, 10000);
  564. if (n < 0) {
  565. printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
  566. n);
  567. }
  568. done:
  569. mutex_unlock(&cam->cam_lock);
  570. }
  571. static ssize_t
  572. vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
  573. {
  574. struct vicam_camera *cam = file->private_data;
  575. DBG("read %d bytes.\n", (int) count);
  576. if (*ppos >= VICAM_MAX_FRAME_SIZE) {
  577. *ppos = 0;
  578. return 0;
  579. }
  580. if (*ppos == 0) {
  581. read_frame(cam, 0);
  582. vicam_decode_color(cam->raw_image,
  583. cam->framebuf +
  584. 0 * VICAM_MAX_FRAME_SIZE);
  585. }
  586. count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
  587. if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
  588. count = -EFAULT;
  589. } else {
  590. *ppos += count;
  591. }
  592. if (count == VICAM_MAX_FRAME_SIZE) {
  593. *ppos = 0;
  594. }
  595. return count;
  596. }
  597. static int
  598. vicam_mmap(struct file *file, struct vm_area_struct *vma)
  599. {
  600. // TODO: allocate the raw frame buffer if necessary
  601. unsigned long page, pos;
  602. unsigned long start = vma->vm_start;
  603. unsigned long size = vma->vm_end-vma->vm_start;
  604. struct vicam_camera *cam = file->private_data;
  605. if (!cam)
  606. return -ENODEV;
  607. DBG("vicam_mmap: %ld\n", size);
  608. /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
  609. * to the size the application requested for mmap and it was screwing apps up.
  610. if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
  611. return -EINVAL;
  612. */
  613. pos = (unsigned long)cam->framebuf;
  614. while (size > 0) {
  615. page = vmalloc_to_pfn((void *)pos);
  616. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  617. return -EAGAIN;
  618. start += PAGE_SIZE;
  619. pos += PAGE_SIZE;
  620. if (size > PAGE_SIZE)
  621. size -= PAGE_SIZE;
  622. else
  623. size = 0;
  624. }
  625. return 0;
  626. }
  627. static const struct v4l2_file_operations vicam_fops = {
  628. .owner = THIS_MODULE,
  629. .open = vicam_open,
  630. .release = vicam_close,
  631. .read = vicam_read,
  632. .mmap = vicam_mmap,
  633. .ioctl = vicam_ioctl,
  634. };
  635. static struct video_device vicam_template = {
  636. .name = "ViCam-based USB Camera",
  637. .fops = &vicam_fops,
  638. .release = video_device_release_empty,
  639. };
  640. /* table of devices that work with this driver */
  641. static struct usb_device_id vicam_table[] = {
  642. {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
  643. {USB_DEVICE(USB_COMPRO_VENDOR_ID, USB_COMPRO_PRODUCT_ID)},
  644. {} /* Terminating entry */
  645. };
  646. MODULE_DEVICE_TABLE(usb, vicam_table);
  647. static struct usb_driver vicam_driver = {
  648. .name = "vicam",
  649. .probe = vicam_probe,
  650. .disconnect = vicam_disconnect,
  651. .id_table = vicam_table
  652. };
  653. /**
  654. * vicam_probe
  655. * @intf: the interface
  656. * @id: the device id
  657. *
  658. * Called by the usb core when a new device is connected that it thinks
  659. * this driver might be interested in.
  660. */
  661. static int
  662. vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
  663. {
  664. struct usb_device *dev = interface_to_usbdev(intf);
  665. int bulkEndpoint = 0;
  666. const struct usb_host_interface *interface;
  667. const struct usb_endpoint_descriptor *endpoint;
  668. struct vicam_camera *cam;
  669. printk(KERN_INFO "ViCam based webcam connected\n");
  670. interface = intf->cur_altsetting;
  671. DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
  672. interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
  673. endpoint = &interface->endpoint[0].desc;
  674. if (usb_endpoint_is_bulk_in(endpoint)) {
  675. /* we found a bulk in endpoint */
  676. bulkEndpoint = endpoint->bEndpointAddress;
  677. } else {
  678. printk(KERN_ERR
  679. "No bulk in endpoint was found ?! (this is bad)\n");
  680. }
  681. if ((cam =
  682. kzalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
  683. printk(KERN_WARNING
  684. "could not allocate kernel memory for vicam_camera struct\n");
  685. return -ENOMEM;
  686. }
  687. cam->shutter_speed = 15;
  688. mutex_init(&cam->cam_lock);
  689. memcpy(&cam->vdev, &vicam_template, sizeof(vicam_template));
  690. video_set_drvdata(&cam->vdev, cam);
  691. cam->udev = dev;
  692. cam->bulkEndpoint = bulkEndpoint;
  693. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) < 0) {
  694. kfree(cam);
  695. printk(KERN_WARNING "video_register_device failed\n");
  696. return -EIO;
  697. }
  698. printk(KERN_INFO "ViCam webcam driver now controlling device %s\n",
  699. video_device_node_name(&cam->vdev));
  700. usb_set_intfdata (intf, cam);
  701. return 0;
  702. }
  703. static void
  704. vicam_disconnect(struct usb_interface *intf)
  705. {
  706. int open_count;
  707. struct vicam_camera *cam = usb_get_intfdata (intf);
  708. usb_set_intfdata (intf, NULL);
  709. /* we must unregister the device before taking its
  710. * cam_lock. This is because the video open call
  711. * holds the same lock as video unregister. if we
  712. * unregister inside of the cam_lock and open also
  713. * uses the cam_lock, we get deadlock.
  714. */
  715. video_unregister_device(&cam->vdev);
  716. /* stop the camera from being used */
  717. mutex_lock(&cam->cam_lock);
  718. /* mark the camera as gone */
  719. cam->udev = NULL;
  720. /* the only thing left to do is synchronize with
  721. * our close/release function on who should release
  722. * the camera memory. if there are any users using the
  723. * camera, it's their job. if there are no users,
  724. * it's ours.
  725. */
  726. open_count = cam->open_count;
  727. mutex_unlock(&cam->cam_lock);
  728. if (!open_count) {
  729. kfree(cam);
  730. }
  731. printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
  732. }
  733. /*
  734. */
  735. static int __init
  736. usb_vicam_init(void)
  737. {
  738. int retval;
  739. DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
  740. retval = usb_register(&vicam_driver);
  741. if (retval)
  742. printk(KERN_WARNING "usb_register failed!\n");
  743. return retval;
  744. }
  745. static void __exit
  746. usb_vicam_exit(void)
  747. {
  748. DBG(KERN_INFO
  749. "ViCam-based WebCam driver shutdown\n");
  750. usb_deregister(&vicam_driver);
  751. }
  752. module_init(usb_vicam_init);
  753. module_exit(usb_vicam_exit);
  754. MODULE_AUTHOR(DRIVER_AUTHOR);
  755. MODULE_DESCRIPTION(DRIVER_DESC);
  756. MODULE_LICENSE("GPL");
  757. MODULE_FIRMWARE("vicam/firmware.fw");