vicam.c 21 KB

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