vicam.c 21 KB

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