vicam.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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/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 *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. /* the videodev_lock held above us protects us from
  398. * simultaneous opens...for now. we probably shouldn't
  399. * rely on this fact forever.
  400. */
  401. lock_kernel();
  402. if (cam->open_count > 0) {
  403. printk(KERN_INFO
  404. "vicam_open called on already opened camera");
  405. unlock_kernel();
  406. return -EBUSY;
  407. }
  408. cam->raw_image = kmalloc(VICAM_MAX_READ_SIZE, GFP_KERNEL);
  409. if (!cam->raw_image) {
  410. unlock_kernel();
  411. return -ENOMEM;
  412. }
  413. cam->framebuf = rvmalloc(VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  414. if (!cam->framebuf) {
  415. kfree(cam->raw_image);
  416. unlock_kernel();
  417. return -ENOMEM;
  418. }
  419. cam->cntrlbuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  420. if (!cam->cntrlbuf) {
  421. kfree(cam->raw_image);
  422. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  423. unlock_kernel();
  424. return -ENOMEM;
  425. }
  426. // First upload firmware, then turn the camera on
  427. if (!cam->is_initialized) {
  428. initialize_camera(cam);
  429. cam->is_initialized = 1;
  430. }
  431. set_camera_power(cam, 1);
  432. cam->needsDummyRead = 1;
  433. cam->open_count++;
  434. file->private_data = cam;
  435. unlock_kernel();
  436. return 0;
  437. }
  438. static int
  439. vicam_close(struct file *file)
  440. {
  441. struct vicam_camera *cam = file->private_data;
  442. int open_count;
  443. struct usb_device *udev;
  444. DBG("close\n");
  445. /* it's not the end of the world if
  446. * we fail to turn the camera off.
  447. */
  448. set_camera_power(cam, 0);
  449. kfree(cam->raw_image);
  450. rvfree(cam->framebuf, VICAM_MAX_FRAME_SIZE * VICAM_FRAMES);
  451. kfree(cam->cntrlbuf);
  452. mutex_lock(&cam->cam_lock);
  453. cam->open_count--;
  454. open_count = cam->open_count;
  455. udev = cam->udev;
  456. mutex_unlock(&cam->cam_lock);
  457. if (!open_count && !udev) {
  458. kfree(cam);
  459. }
  460. return 0;
  461. }
  462. static void vicam_decode_color(const u8 *data, u8 *rgb)
  463. {
  464. /* vicam_decode_color - Convert from Vicam Y-Cr-Cb to RGB
  465. * Copyright (C) 2002 Monroe Williams (monroe@pobox.com)
  466. */
  467. int i, prevY, nextY;
  468. prevY = 512;
  469. nextY = 512;
  470. data += VICAM_HEADER_SIZE;
  471. for( i = 0; i < 240; i++, data += 512 ) {
  472. const int y = ( i * 242 ) / 240;
  473. int j, prevX, nextX;
  474. int Y, Cr, Cb;
  475. if ( y == 242 - 1 ) {
  476. nextY = -512;
  477. }
  478. prevX = 1;
  479. nextX = 1;
  480. for ( j = 0; j < 320; j++, rgb += 3 ) {
  481. const int x = ( j * 512 ) / 320;
  482. const u8 * const src = &data[x];
  483. if ( x == 512 - 1 ) {
  484. nextX = -1;
  485. }
  486. Cr = ( src[prevX] - src[0] ) +
  487. ( src[nextX] - src[0] );
  488. Cr /= 2;
  489. Cb = ( src[prevY] - src[prevX + prevY] ) +
  490. ( src[prevY] - src[nextX + prevY] ) +
  491. ( src[nextY] - src[prevX + nextY] ) +
  492. ( src[nextY] - src[nextX + nextY] );
  493. Cb /= 4;
  494. Y = 1160 * ( src[0] + ( Cr / 2 ) - 16 );
  495. if ( i & 1 ) {
  496. int Ct = Cr;
  497. Cr = Cb;
  498. Cb = Ct;
  499. }
  500. if ( ( x ^ i ) & 1 ) {
  501. Cr = -Cr;
  502. Cb = -Cb;
  503. }
  504. rgb[0] = clamp( ( ( Y + ( 2017 * Cb ) ) +
  505. 500 ) / 900, 0, 255 );
  506. rgb[1] = clamp( ( ( Y - ( 392 * Cb ) -
  507. ( 813 * Cr ) ) +
  508. 500 ) / 1000, 0, 255 );
  509. rgb[2] = clamp( ( ( Y + ( 1594 * Cr ) ) +
  510. 500 ) / 1300, 0, 255 );
  511. prevX = -1;
  512. }
  513. prevY = -512;
  514. }
  515. }
  516. static void
  517. read_frame(struct vicam_camera *cam, int framenum)
  518. {
  519. unsigned char *request = cam->cntrlbuf;
  520. int realShutter;
  521. int n;
  522. int actual_length;
  523. if (cam->needsDummyRead) {
  524. cam->needsDummyRead = 0;
  525. read_frame(cam, framenum);
  526. }
  527. memset(request, 0, 16);
  528. request[0] = cam->gain; // 0 = 0% gain, FF = 100% gain
  529. request[1] = 0; // 512x242 capture
  530. request[2] = 0x90; // the function of these two bytes
  531. request[3] = 0x07; // is not yet understood
  532. if (cam->shutter_speed > 60) {
  533. // Short exposure
  534. realShutter =
  535. ((-15631900 / cam->shutter_speed) + 260533) / 1000;
  536. request[4] = realShutter & 0xFF;
  537. request[5] = (realShutter >> 8) & 0xFF;
  538. request[6] = 0x03;
  539. request[7] = 0x01;
  540. } else {
  541. // Long exposure
  542. realShutter = 15600 / cam->shutter_speed - 1;
  543. request[4] = 0;
  544. request[5] = 0;
  545. request[6] = realShutter & 0xFF;
  546. request[7] = realShutter >> 8;
  547. }
  548. // Per John Markus Bjørndalen, byte at index 8 causes problems if it isn't 0
  549. request[8] = 0;
  550. // bytes 9-15 do not seem to affect exposure or image quality
  551. mutex_lock(&cam->cam_lock);
  552. if (!cam->udev) {
  553. goto done;
  554. }
  555. n = __send_control_msg(cam, 0x51, 0x80, 0, request, 16);
  556. if (n < 0) {
  557. printk(KERN_ERR
  558. " Problem sending frame capture control message");
  559. goto done;
  560. }
  561. n = usb_bulk_msg(cam->udev,
  562. usb_rcvbulkpipe(cam->udev, cam->bulkEndpoint),
  563. cam->raw_image,
  564. 512 * 242 + 128, &actual_length, 10000);
  565. if (n < 0) {
  566. printk(KERN_ERR "Problem during bulk read of frame data: %d\n",
  567. n);
  568. }
  569. done:
  570. mutex_unlock(&cam->cam_lock);
  571. }
  572. static ssize_t
  573. vicam_read( struct file *file, char __user *buf, size_t count, loff_t *ppos )
  574. {
  575. struct vicam_camera *cam = file->private_data;
  576. DBG("read %d bytes.\n", (int) count);
  577. if (*ppos >= VICAM_MAX_FRAME_SIZE) {
  578. *ppos = 0;
  579. return 0;
  580. }
  581. if (*ppos == 0) {
  582. read_frame(cam, 0);
  583. vicam_decode_color(cam->raw_image,
  584. cam->framebuf +
  585. 0 * VICAM_MAX_FRAME_SIZE);
  586. }
  587. count = min_t(size_t, count, VICAM_MAX_FRAME_SIZE - *ppos);
  588. if (copy_to_user(buf, &cam->framebuf[*ppos], count)) {
  589. count = -EFAULT;
  590. } else {
  591. *ppos += count;
  592. }
  593. if (count == VICAM_MAX_FRAME_SIZE) {
  594. *ppos = 0;
  595. }
  596. return count;
  597. }
  598. static int
  599. vicam_mmap(struct file *file, struct vm_area_struct *vma)
  600. {
  601. // TODO: allocate the raw frame buffer if necessary
  602. unsigned long page, pos;
  603. unsigned long start = vma->vm_start;
  604. unsigned long size = vma->vm_end-vma->vm_start;
  605. struct vicam_camera *cam = file->private_data;
  606. if (!cam)
  607. return -ENODEV;
  608. DBG("vicam_mmap: %ld\n", size);
  609. /* We let mmap allocate as much as it wants because Linux was adding 2048 bytes
  610. * to the size the application requested for mmap and it was screwing apps up.
  611. if (size > VICAM_FRAMES*VICAM_MAX_FRAME_SIZE)
  612. return -EINVAL;
  613. */
  614. pos = (unsigned long)cam->framebuf;
  615. while (size > 0) {
  616. page = vmalloc_to_pfn((void *)pos);
  617. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  618. return -EAGAIN;
  619. start += PAGE_SIZE;
  620. pos += PAGE_SIZE;
  621. if (size > PAGE_SIZE)
  622. size -= PAGE_SIZE;
  623. else
  624. size = 0;
  625. }
  626. return 0;
  627. }
  628. static const struct v4l2_file_operations vicam_fops = {
  629. .owner = THIS_MODULE,
  630. .open = vicam_open,
  631. .release = vicam_close,
  632. .read = vicam_read,
  633. .mmap = vicam_mmap,
  634. .ioctl = vicam_ioctl,
  635. };
  636. static struct video_device vicam_template = {
  637. .name = "ViCam-based USB Camera",
  638. .fops = &vicam_fops,
  639. .minor = -1,
  640. .release = video_device_release_empty,
  641. };
  642. /* table of devices that work with this driver */
  643. static struct usb_device_id vicam_table[] = {
  644. {USB_DEVICE(USB_VICAM_VENDOR_ID, USB_VICAM_PRODUCT_ID)},
  645. {USB_DEVICE(USB_COMPRO_VENDOR_ID, USB_COMPRO_PRODUCT_ID)},
  646. {} /* Terminating entry */
  647. };
  648. MODULE_DEVICE_TABLE(usb, vicam_table);
  649. static struct usb_driver vicam_driver = {
  650. .name = "vicam",
  651. .probe = vicam_probe,
  652. .disconnect = vicam_disconnect,
  653. .id_table = vicam_table
  654. };
  655. /**
  656. * vicam_probe
  657. * @intf: the interface
  658. * @id: the device id
  659. *
  660. * Called by the usb core when a new device is connected that it thinks
  661. * this driver might be interested in.
  662. */
  663. static int
  664. vicam_probe( struct usb_interface *intf, const struct usb_device_id *id)
  665. {
  666. struct usb_device *dev = interface_to_usbdev(intf);
  667. int bulkEndpoint = 0;
  668. const struct usb_host_interface *interface;
  669. const struct usb_endpoint_descriptor *endpoint;
  670. struct vicam_camera *cam;
  671. printk(KERN_INFO "ViCam based webcam connected\n");
  672. interface = intf->cur_altsetting;
  673. DBG(KERN_DEBUG "Interface %d. has %u. endpoints!\n",
  674. interface->desc.bInterfaceNumber, (unsigned) (interface->desc.bNumEndpoints));
  675. endpoint = &interface->endpoint[0].desc;
  676. if (usb_endpoint_is_bulk_in(endpoint)) {
  677. /* we found a bulk in endpoint */
  678. bulkEndpoint = endpoint->bEndpointAddress;
  679. } else {
  680. printk(KERN_ERR
  681. "No bulk in endpoint was found ?! (this is bad)\n");
  682. }
  683. if ((cam =
  684. kzalloc(sizeof (struct vicam_camera), GFP_KERNEL)) == NULL) {
  685. printk(KERN_WARNING
  686. "could not allocate kernel memory for vicam_camera struct\n");
  687. return -ENOMEM;
  688. }
  689. cam->shutter_speed = 15;
  690. mutex_init(&cam->cam_lock);
  691. memcpy(&cam->vdev, &vicam_template, sizeof(vicam_template));
  692. video_set_drvdata(&cam->vdev, cam);
  693. cam->udev = dev;
  694. cam->bulkEndpoint = bulkEndpoint;
  695. if (video_register_device(&cam->vdev, VFL_TYPE_GRABBER, -1) < 0) {
  696. kfree(cam);
  697. printk(KERN_WARNING "video_register_device failed\n");
  698. return -EIO;
  699. }
  700. printk(KERN_INFO "ViCam webcam driver now controlling video device %d\n",
  701. cam->vdev.num);
  702. usb_set_intfdata (intf, cam);
  703. return 0;
  704. }
  705. static void
  706. vicam_disconnect(struct usb_interface *intf)
  707. {
  708. int open_count;
  709. struct vicam_camera *cam = usb_get_intfdata (intf);
  710. usb_set_intfdata (intf, NULL);
  711. /* we must unregister the device before taking its
  712. * cam_lock. This is because the video open call
  713. * holds the same lock as video unregister. if we
  714. * unregister inside of the cam_lock and open also
  715. * uses the cam_lock, we get deadlock.
  716. */
  717. video_unregister_device(&cam->vdev);
  718. /* stop the camera from being used */
  719. mutex_lock(&cam->cam_lock);
  720. /* mark the camera as gone */
  721. cam->udev = NULL;
  722. /* the only thing left to do is synchronize with
  723. * our close/release function on who should release
  724. * the camera memory. if there are any users using the
  725. * camera, it's their job. if there are no users,
  726. * it's ours.
  727. */
  728. open_count = cam->open_count;
  729. mutex_unlock(&cam->cam_lock);
  730. if (!open_count) {
  731. kfree(cam);
  732. }
  733. printk(KERN_DEBUG "ViCam-based WebCam disconnected\n");
  734. }
  735. /*
  736. */
  737. static int __init
  738. usb_vicam_init(void)
  739. {
  740. int retval;
  741. DBG(KERN_INFO "ViCam-based WebCam driver startup\n");
  742. retval = usb_register(&vicam_driver);
  743. if (retval)
  744. printk(KERN_WARNING "usb_register failed!\n");
  745. return retval;
  746. }
  747. static void __exit
  748. usb_vicam_exit(void)
  749. {
  750. DBG(KERN_INFO
  751. "ViCam-based WebCam driver shutdown\n");
  752. usb_deregister(&vicam_driver);
  753. }
  754. module_init(usb_vicam_init);
  755. module_exit(usb_vicam_exit);
  756. MODULE_AUTHOR(DRIVER_AUTHOR);
  757. MODULE_DESCRIPTION(DRIVER_DESC);
  758. MODULE_LICENSE("GPL");
  759. MODULE_FIRMWARE("vicam/firmware.fw");