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