PageRenderTime 29ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/media/video/pwc/pwc-if.c

https://bitbucket.org/abioy/linux
C | 2116 lines | 1552 code | 226 blank | 338 comment | 343 complexity | 7b073174d8068e2bcbed40f8e6be1baa MD5 | raw file
Possible License(s): CC-BY-SA-3.0, GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /* Linux driver for Philips webcam
  2. USB and Video4Linux interface part.
  3. (C) 1999-2004 Nemosoft Unv.
  4. (C) 2004-2006 Luc Saillard (luc@saillard.org)
  5. NOTE: this version of pwc is an unofficial (modified) release of pwc & pcwx
  6. driver and thus may have bugs that are not present in the original version.
  7. Please send bug reports and support requests to <luc@saillard.org>.
  8. The decompression routines have been implemented by reverse-engineering the
  9. Nemosoft binary pwcx module. Caveat emptor.
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /*
  23. This code forms the interface between the USB layers and the Philips
  24. specific stuff. Some adanved stuff of the driver falls under an
  25. NDA, signed between me and Philips B.V., Eindhoven, the Netherlands, and
  26. is thus not distributed in source form. The binary pwcx.o module
  27. contains the code that falls under the NDA.
  28. In case you're wondering: 'pwc' stands for "Philips WebCam", but
  29. I really didn't want to type 'philips_web_cam' every time (I'm lazy as
  30. any Linux kernel hacker, but I don't like uncomprehensible abbreviations
  31. without explanation).
  32. Oh yes, convention: to disctinguish between all the various pointers to
  33. device-structures, I use these names for the pointer variables:
  34. udev: struct usb_device *
  35. vdev: struct video_device *
  36. pdev: struct pwc_devive *
  37. */
  38. /* Contributors:
  39. - Alvarado: adding whitebalance code
  40. - Alistar Moire: QuickCam 3000 Pro device/product ID
  41. - Tony Hoyle: Creative Labs Webcam 5 device/product ID
  42. - Mark Burazin: solving hang in VIDIOCSYNC when camera gets unplugged
  43. - Jk Fang: Sotec Afina Eye ID
  44. - Xavier Roche: QuickCam Pro 4000 ID
  45. - Jens Knudsen: QuickCam Zoom ID
  46. - J. Debert: QuickCam for Notebooks ID
  47. - Pham Thanh Nam: webcam snapshot button as an event input device
  48. */
  49. #include <linux/errno.h>
  50. #include <linux/init.h>
  51. #include <linux/mm.h>
  52. #include <linux/module.h>
  53. #include <linux/poll.h>
  54. #include <linux/slab.h>
  55. #include <linux/smp_lock.h>
  56. #ifdef CONFIG_USB_PWC_INPUT_EVDEV
  57. #include <linux/usb/input.h>
  58. #endif
  59. #include <linux/vmalloc.h>
  60. #include <asm/io.h>
  61. #include <linux/kernel.h> /* simple_strtol() */
  62. #include "pwc.h"
  63. #include "pwc-kiara.h"
  64. #include "pwc-timon.h"
  65. #include "pwc-dec23.h"
  66. #include "pwc-dec1.h"
  67. #include "pwc-uncompress.h"
  68. /* Function prototypes and driver templates */
  69. /* hotplug device table support */
  70. static const struct usb_device_id pwc_device_table [] = {
  71. { USB_DEVICE(0x0471, 0x0302) }, /* Philips models */
  72. { USB_DEVICE(0x0471, 0x0303) },
  73. { USB_DEVICE(0x0471, 0x0304) },
  74. { USB_DEVICE(0x0471, 0x0307) },
  75. { USB_DEVICE(0x0471, 0x0308) },
  76. { USB_DEVICE(0x0471, 0x030C) },
  77. { USB_DEVICE(0x0471, 0x0310) },
  78. { USB_DEVICE(0x0471, 0x0311) }, /* Philips ToUcam PRO II */
  79. { USB_DEVICE(0x0471, 0x0312) },
  80. { USB_DEVICE(0x0471, 0x0313) }, /* the 'new' 720K */
  81. { USB_DEVICE(0x0471, 0x0329) }, /* Philips SPC 900NC PC Camera */
  82. { USB_DEVICE(0x069A, 0x0001) }, /* Askey */
  83. { USB_DEVICE(0x046D, 0x08B0) }, /* Logitech QuickCam Pro 3000 */
  84. { USB_DEVICE(0x046D, 0x08B1) }, /* Logitech QuickCam Notebook Pro */
  85. { USB_DEVICE(0x046D, 0x08B2) }, /* Logitech QuickCam Pro 4000 */
  86. { USB_DEVICE(0x046D, 0x08B3) }, /* Logitech QuickCam Zoom (old model) */
  87. { USB_DEVICE(0x046D, 0x08B4) }, /* Logitech QuickCam Zoom (new model) */
  88. { USB_DEVICE(0x046D, 0x08B5) }, /* Logitech QuickCam Orbit/Sphere */
  89. { USB_DEVICE(0x046D, 0x08B6) }, /* Cisco VT Camera */
  90. { USB_DEVICE(0x046D, 0x08B7) }, /* Logitech ViewPort AV 100 */
  91. { USB_DEVICE(0x046D, 0x08B8) }, /* Logitech (reserved) */
  92. { USB_DEVICE(0x055D, 0x9000) }, /* Samsung MPC-C10 */
  93. { USB_DEVICE(0x055D, 0x9001) }, /* Samsung MPC-C30 */
  94. { USB_DEVICE(0x055D, 0x9002) }, /* Samsung SNC-35E (Ver3.0) */
  95. { USB_DEVICE(0x041E, 0x400C) }, /* Creative Webcam 5 */
  96. { USB_DEVICE(0x041E, 0x4011) }, /* Creative Webcam Pro Ex */
  97. { USB_DEVICE(0x04CC, 0x8116) }, /* Afina Eye */
  98. { USB_DEVICE(0x06BE, 0x8116) }, /* new Afina Eye */
  99. { USB_DEVICE(0x0d81, 0x1910) }, /* Visionite */
  100. { USB_DEVICE(0x0d81, 0x1900) },
  101. { }
  102. };
  103. MODULE_DEVICE_TABLE(usb, pwc_device_table);
  104. static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id);
  105. static void usb_pwc_disconnect(struct usb_interface *intf);
  106. static struct usb_driver pwc_driver = {
  107. .name = "Philips webcam", /* name */
  108. .id_table = pwc_device_table,
  109. .probe = usb_pwc_probe, /* probe() */
  110. .disconnect = usb_pwc_disconnect, /* disconnect() */
  111. };
  112. #define MAX_DEV_HINTS 20
  113. #define MAX_ISOC_ERRORS 20
  114. static int default_size = PSZ_QCIF;
  115. static int default_fps = 10;
  116. static int default_fbufs = 3; /* Default number of frame buffers */
  117. int pwc_mbufs = 2; /* Default number of mmap() buffers */
  118. #ifdef CONFIG_USB_PWC_DEBUG
  119. int pwc_trace = PWC_DEBUG_LEVEL;
  120. #endif
  121. static int power_save;
  122. static int led_on = 100, led_off; /* defaults to LED that is on while in use */
  123. static int pwc_preferred_compression = 1; /* 0..3 = uncompressed..high */
  124. static struct {
  125. int type;
  126. char serial_number[30];
  127. int device_node;
  128. struct pwc_device *pdev;
  129. } device_hint[MAX_DEV_HINTS];
  130. /***/
  131. static int pwc_video_open(struct file *file);
  132. static int pwc_video_close(struct file *file);
  133. static ssize_t pwc_video_read(struct file *file, char __user *buf,
  134. size_t count, loff_t *ppos);
  135. static unsigned int pwc_video_poll(struct file *file, poll_table *wait);
  136. static long pwc_video_ioctl(struct file *file,
  137. unsigned int ioctlnr, unsigned long arg);
  138. static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma);
  139. static const struct v4l2_file_operations pwc_fops = {
  140. .owner = THIS_MODULE,
  141. .open = pwc_video_open,
  142. .release = pwc_video_close,
  143. .read = pwc_video_read,
  144. .poll = pwc_video_poll,
  145. .mmap = pwc_video_mmap,
  146. .ioctl = pwc_video_ioctl,
  147. };
  148. static struct video_device pwc_template = {
  149. .name = "Philips Webcam", /* Filled in later */
  150. .release = video_device_release,
  151. .fops = &pwc_fops,
  152. };
  153. /***************************************************************************/
  154. /* Okay, this is some magic that I worked out and the reasoning behind it...
  155. The biggest problem with any USB device is of course: "what to do
  156. when the user unplugs the device while it is in use by an application?"
  157. We have several options:
  158. 1) Curse them with the 7 plagues when they do (requires divine intervention)
  159. 2) Tell them not to (won't work: they'll do it anyway)
  160. 3) Oops the kernel (this will have a negative effect on a user's uptime)
  161. 4) Do something sensible.
  162. Of course, we go for option 4.
  163. It happens that this device will be linked to two times, once from
  164. usb_device and once from the video_device in their respective 'private'
  165. pointers. This is done when the device is probed() and all initialization
  166. succeeded. The pwc_device struct links back to both structures.
  167. When a device is unplugged while in use it will be removed from the
  168. list of known USB devices; I also de-register it as a V4L device, but
  169. unfortunately I can't free the memory since the struct is still in use
  170. by the file descriptor. This free-ing is then deferend until the first
  171. opportunity. Crude, but it works.
  172. A small 'advantage' is that if a user unplugs the cam and plugs it back
  173. in, it should get assigned the same video device minor, but unfortunately
  174. it's non-trivial to re-link the cam back to the video device... (that
  175. would surely be magic! :))
  176. */
  177. /***************************************************************************/
  178. /* Private functions */
  179. /* Here we want the physical address of the memory.
  180. * This is used when initializing the contents of the area.
  181. */
  182. static void *pwc_rvmalloc(unsigned long size)
  183. {
  184. void * mem;
  185. unsigned long adr;
  186. mem=vmalloc_32(size);
  187. if (!mem)
  188. return NULL;
  189. memset(mem, 0, size); /* Clear the ram out, no junk to the user */
  190. adr=(unsigned long) mem;
  191. while (size > 0)
  192. {
  193. SetPageReserved(vmalloc_to_page((void *)adr));
  194. adr += PAGE_SIZE;
  195. size -= PAGE_SIZE;
  196. }
  197. return mem;
  198. }
  199. static void pwc_rvfree(void * mem, unsigned long size)
  200. {
  201. unsigned long adr;
  202. if (!mem)
  203. return;
  204. adr=(unsigned long) mem;
  205. while ((long) size > 0)
  206. {
  207. ClearPageReserved(vmalloc_to_page((void *)adr));
  208. adr += PAGE_SIZE;
  209. size -= PAGE_SIZE;
  210. }
  211. vfree(mem);
  212. }
  213. static int pwc_allocate_buffers(struct pwc_device *pdev)
  214. {
  215. int i, err;
  216. void *kbuf;
  217. PWC_DEBUG_MEMORY(">> pwc_allocate_buffers(pdev = 0x%p)\n", pdev);
  218. if (pdev == NULL)
  219. return -ENXIO;
  220. /* Allocate Isochronuous pipe buffers */
  221. for (i = 0; i < MAX_ISO_BUFS; i++) {
  222. if (pdev->sbuf[i].data == NULL) {
  223. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  224. if (kbuf == NULL) {
  225. PWC_ERROR("Failed to allocate iso buffer %d.\n", i);
  226. return -ENOMEM;
  227. }
  228. PWC_DEBUG_MEMORY("Allocated iso buffer at %p.\n", kbuf);
  229. pdev->sbuf[i].data = kbuf;
  230. }
  231. }
  232. /* Allocate frame buffer structure */
  233. if (pdev->fbuf == NULL) {
  234. kbuf = kzalloc(default_fbufs * sizeof(struct pwc_frame_buf), GFP_KERNEL);
  235. if (kbuf == NULL) {
  236. PWC_ERROR("Failed to allocate frame buffer structure.\n");
  237. return -ENOMEM;
  238. }
  239. PWC_DEBUG_MEMORY("Allocated frame buffer structure at %p.\n", kbuf);
  240. pdev->fbuf = kbuf;
  241. }
  242. /* create frame buffers, and make circular ring */
  243. for (i = 0; i < default_fbufs; i++) {
  244. if (pdev->fbuf[i].data == NULL) {
  245. kbuf = vmalloc(PWC_FRAME_SIZE); /* need vmalloc since frame buffer > 128K */
  246. if (kbuf == NULL) {
  247. PWC_ERROR("Failed to allocate frame buffer %d.\n", i);
  248. return -ENOMEM;
  249. }
  250. PWC_DEBUG_MEMORY("Allocated frame buffer %d at %p.\n", i, kbuf);
  251. pdev->fbuf[i].data = kbuf;
  252. memset(kbuf, 0, PWC_FRAME_SIZE);
  253. }
  254. }
  255. /* Allocate decompressor table space */
  256. if (DEVICE_USE_CODEC1(pdev->type))
  257. err = pwc_dec1_alloc(pdev);
  258. else
  259. err = pwc_dec23_alloc(pdev);
  260. if (err) {
  261. PWC_ERROR("Failed to allocate decompress table.\n");
  262. return err;
  263. }
  264. /* Allocate image buffer; double buffer for mmap() */
  265. kbuf = pwc_rvmalloc(pwc_mbufs * pdev->len_per_image);
  266. if (kbuf == NULL) {
  267. PWC_ERROR("Failed to allocate image buffer(s). needed (%d)\n",
  268. pwc_mbufs * pdev->len_per_image);
  269. return -ENOMEM;
  270. }
  271. PWC_DEBUG_MEMORY("Allocated image buffer at %p.\n", kbuf);
  272. pdev->image_data = kbuf;
  273. for (i = 0; i < pwc_mbufs; i++) {
  274. pdev->images[i].offset = i * pdev->len_per_image;
  275. pdev->images[i].vma_use_count = 0;
  276. }
  277. for (; i < MAX_IMAGES; i++) {
  278. pdev->images[i].offset = 0;
  279. }
  280. kbuf = NULL;
  281. PWC_DEBUG_MEMORY("<< pwc_allocate_buffers()\n");
  282. return 0;
  283. }
  284. static void pwc_free_buffers(struct pwc_device *pdev)
  285. {
  286. int i;
  287. PWC_DEBUG_MEMORY("Entering free_buffers(%p).\n", pdev);
  288. if (pdev == NULL)
  289. return;
  290. /* Release Iso-pipe buffers */
  291. for (i = 0; i < MAX_ISO_BUFS; i++)
  292. if (pdev->sbuf[i].data != NULL) {
  293. PWC_DEBUG_MEMORY("Freeing ISO buffer at %p.\n", pdev->sbuf[i].data);
  294. kfree(pdev->sbuf[i].data);
  295. pdev->sbuf[i].data = NULL;
  296. }
  297. /* The same for frame buffers */
  298. if (pdev->fbuf != NULL) {
  299. for (i = 0; i < default_fbufs; i++) {
  300. if (pdev->fbuf[i].data != NULL) {
  301. PWC_DEBUG_MEMORY("Freeing frame buffer %d at %p.\n", i, pdev->fbuf[i].data);
  302. vfree(pdev->fbuf[i].data);
  303. pdev->fbuf[i].data = NULL;
  304. }
  305. }
  306. kfree(pdev->fbuf);
  307. pdev->fbuf = NULL;
  308. }
  309. /* Intermediate decompression buffer & tables */
  310. if (pdev->decompress_data != NULL) {
  311. PWC_DEBUG_MEMORY("Freeing decompression buffer at %p.\n", pdev->decompress_data);
  312. kfree(pdev->decompress_data);
  313. pdev->decompress_data = NULL;
  314. }
  315. /* Release image buffers */
  316. if (pdev->image_data != NULL) {
  317. PWC_DEBUG_MEMORY("Freeing image buffer at %p.\n", pdev->image_data);
  318. pwc_rvfree(pdev->image_data, pwc_mbufs * pdev->len_per_image);
  319. }
  320. pdev->image_data = NULL;
  321. PWC_DEBUG_MEMORY("Leaving free_buffers().\n");
  322. }
  323. /* The frame & image buffer mess.
  324. Yes, this is a mess. Well, it used to be simple, but alas... In this
  325. module, 3 buffers schemes are used to get the data from the USB bus to
  326. the user program. The first scheme involves the ISO buffers (called thus
  327. since they transport ISO data from the USB controller), and not really
  328. interesting. Suffices to say the data from this buffer is quickly
  329. gathered in an interrupt handler (pwc_isoc_handler) and placed into the
  330. frame buffer.
  331. The frame buffer is the second scheme, and is the central element here.
  332. It collects the data from a single frame from the camera (hence, the
  333. name). Frames are delimited by the USB camera with a short USB packet,
  334. so that's easy to detect. The frame buffers form a list that is filled
  335. by the camera+USB controller and drained by the user process through
  336. either read() or mmap().
  337. The image buffer is the third scheme, in which frames are decompressed
  338. and converted into planar format. For mmap() there is more than
  339. one image buffer available.
  340. The frame buffers provide the image buffering. In case the user process
  341. is a bit slow, this introduces lag and some undesired side-effects.
  342. The problem arises when the frame buffer is full. I used to drop the last
  343. frame, which makes the data in the queue stale very quickly. But dropping
  344. the frame at the head of the queue proved to be a litte bit more difficult.
  345. I tried a circular linked scheme, but this introduced more problems than
  346. it solved.
  347. Because filling and draining are completely asynchronous processes, this
  348. requires some fiddling with pointers and mutexes.
  349. Eventually, I came up with a system with 2 lists: an 'empty' frame list
  350. and a 'full' frame list:
  351. * Initially, all frame buffers but one are on the 'empty' list; the one
  352. remaining buffer is our initial fill frame.
  353. * If a frame is needed for filling, we try to take it from the 'empty'
  354. list, unless that list is empty, in which case we take the buffer at
  355. the head of the 'full' list.
  356. * When our fill buffer has been filled, it is appended to the 'full'
  357. list.
  358. * If a frame is needed by read() or mmap(), it is taken from the head of
  359. the 'full' list, handled, and then appended to the 'empty' list. If no
  360. buffer is present on the 'full' list, we wait.
  361. The advantage is that the buffer that is currently being decompressed/
  362. converted, is on neither list, and thus not in our way (any other scheme
  363. I tried had the problem of old data lingering in the queue).
  364. Whatever strategy you choose, it always remains a tradeoff: with more
  365. frame buffers the chances of a missed frame are reduced. On the other
  366. hand, on slower machines it introduces lag because the queue will
  367. always be full.
  368. */
  369. /**
  370. \brief Find next frame buffer to fill. Take from empty or full list, whichever comes first.
  371. */
  372. static int pwc_next_fill_frame(struct pwc_device *pdev)
  373. {
  374. int ret;
  375. unsigned long flags;
  376. ret = 0;
  377. spin_lock_irqsave(&pdev->ptrlock, flags);
  378. if (pdev->fill_frame != NULL) {
  379. /* append to 'full' list */
  380. if (pdev->full_frames == NULL) {
  381. pdev->full_frames = pdev->fill_frame;
  382. pdev->full_frames_tail = pdev->full_frames;
  383. }
  384. else {
  385. pdev->full_frames_tail->next = pdev->fill_frame;
  386. pdev->full_frames_tail = pdev->fill_frame;
  387. }
  388. }
  389. if (pdev->empty_frames != NULL) {
  390. /* We have empty frames available. That's easy */
  391. pdev->fill_frame = pdev->empty_frames;
  392. pdev->empty_frames = pdev->empty_frames->next;
  393. }
  394. else {
  395. /* Hmm. Take it from the full list */
  396. /* sanity check */
  397. if (pdev->full_frames == NULL) {
  398. PWC_ERROR("Neither empty or full frames available!\n");
  399. spin_unlock_irqrestore(&pdev->ptrlock, flags);
  400. return -EINVAL;
  401. }
  402. pdev->fill_frame = pdev->full_frames;
  403. pdev->full_frames = pdev->full_frames->next;
  404. ret = 1;
  405. }
  406. pdev->fill_frame->next = NULL;
  407. spin_unlock_irqrestore(&pdev->ptrlock, flags);
  408. return ret;
  409. }
  410. /**
  411. \brief Reset all buffers, pointers and lists, except for the image_used[] buffer.
  412. If the image_used[] buffer is cleared too, mmap()/VIDIOCSYNC will run into trouble.
  413. */
  414. static void pwc_reset_buffers(struct pwc_device *pdev)
  415. {
  416. int i;
  417. unsigned long flags;
  418. PWC_DEBUG_MEMORY(">> %s __enter__\n", __func__);
  419. spin_lock_irqsave(&pdev->ptrlock, flags);
  420. pdev->full_frames = NULL;
  421. pdev->full_frames_tail = NULL;
  422. for (i = 0; i < default_fbufs; i++) {
  423. pdev->fbuf[i].filled = 0;
  424. if (i > 0)
  425. pdev->fbuf[i].next = &pdev->fbuf[i - 1];
  426. else
  427. pdev->fbuf->next = NULL;
  428. }
  429. pdev->empty_frames = &pdev->fbuf[default_fbufs - 1];
  430. pdev->empty_frames_tail = pdev->fbuf;
  431. pdev->read_frame = NULL;
  432. pdev->fill_frame = pdev->empty_frames;
  433. pdev->empty_frames = pdev->empty_frames->next;
  434. pdev->image_read_pos = 0;
  435. pdev->fill_image = 0;
  436. spin_unlock_irqrestore(&pdev->ptrlock, flags);
  437. PWC_DEBUG_MEMORY("<< %s __leaving__\n", __func__);
  438. }
  439. /**
  440. \brief Do all the handling for getting one frame: get pointer, decompress, advance pointers.
  441. */
  442. int pwc_handle_frame(struct pwc_device *pdev)
  443. {
  444. int ret = 0;
  445. unsigned long flags;
  446. spin_lock_irqsave(&pdev->ptrlock, flags);
  447. /* First grab our read_frame; this is removed from all lists, so
  448. we can release the lock after this without problems */
  449. if (pdev->read_frame != NULL) {
  450. /* This can't theoretically happen */
  451. PWC_ERROR("Huh? Read frame still in use?\n");
  452. spin_unlock_irqrestore(&pdev->ptrlock, flags);
  453. return ret;
  454. }
  455. if (pdev->full_frames == NULL) {
  456. PWC_ERROR("Woops. No frames ready.\n");
  457. }
  458. else {
  459. pdev->read_frame = pdev->full_frames;
  460. pdev->full_frames = pdev->full_frames->next;
  461. pdev->read_frame->next = NULL;
  462. }
  463. if (pdev->read_frame != NULL) {
  464. /* Decompression is a lengthy process, so it's outside of the lock.
  465. This gives the isoc_handler the opportunity to fill more frames
  466. in the mean time.
  467. */
  468. spin_unlock_irqrestore(&pdev->ptrlock, flags);
  469. ret = pwc_decompress(pdev);
  470. spin_lock_irqsave(&pdev->ptrlock, flags);
  471. /* We're done with read_buffer, tack it to the end of the empty buffer list */
  472. if (pdev->empty_frames == NULL) {
  473. pdev->empty_frames = pdev->read_frame;
  474. pdev->empty_frames_tail = pdev->empty_frames;
  475. }
  476. else {
  477. pdev->empty_frames_tail->next = pdev->read_frame;
  478. pdev->empty_frames_tail = pdev->read_frame;
  479. }
  480. pdev->read_frame = NULL;
  481. }
  482. spin_unlock_irqrestore(&pdev->ptrlock, flags);
  483. return ret;
  484. }
  485. /**
  486. \brief Advance pointers of image buffer (after each user request)
  487. */
  488. void pwc_next_image(struct pwc_device *pdev)
  489. {
  490. pdev->image_used[pdev->fill_image] = 0;
  491. pdev->fill_image = (pdev->fill_image + 1) % pwc_mbufs;
  492. }
  493. /**
  494. * Print debug information when a frame is discarded because all of our buffer
  495. * is full
  496. */
  497. static void pwc_frame_dumped(struct pwc_device *pdev)
  498. {
  499. pdev->vframes_dumped++;
  500. if (pdev->vframe_count < FRAME_LOWMARK)
  501. return;
  502. if (pdev->vframes_dumped < 20)
  503. PWC_DEBUG_FLOW("Dumping frame %d\n", pdev->vframe_count);
  504. else if (pdev->vframes_dumped == 20)
  505. PWC_DEBUG_FLOW("Dumping frame %d (last message)\n",
  506. pdev->vframe_count);
  507. }
  508. static void pwc_snapshot_button(struct pwc_device *pdev, int down)
  509. {
  510. if (down) {
  511. PWC_TRACE("Snapshot button pressed.\n");
  512. pdev->snapshot_button_status = 1;
  513. } else {
  514. PWC_TRACE("Snapshot button released.\n");
  515. }
  516. #ifdef CONFIG_USB_PWC_INPUT_EVDEV
  517. if (pdev->button_dev) {
  518. input_report_key(pdev->button_dev, KEY_CAMERA, down);
  519. input_sync(pdev->button_dev);
  520. }
  521. #endif
  522. }
  523. static int pwc_rcv_short_packet(struct pwc_device *pdev, const struct pwc_frame_buf *fbuf)
  524. {
  525. int awake = 0;
  526. /* The ToUCam Fun CMOS sensor causes the firmware to send 2 or 3 bogus
  527. frames on the USB wire after an exposure change. This conditition is
  528. however detected in the cam and a bit is set in the header.
  529. */
  530. if (pdev->type == 730) {
  531. unsigned char *ptr = (unsigned char *)fbuf->data;
  532. if (ptr[1] == 1 && ptr[0] & 0x10) {
  533. PWC_TRACE("Hyundai CMOS sensor bug. Dropping frame.\n");
  534. pdev->drop_frames += 2;
  535. pdev->vframes_error++;
  536. }
  537. if ((ptr[0] ^ pdev->vmirror) & 0x01) {
  538. pwc_snapshot_button(pdev, ptr[0] & 0x01);
  539. }
  540. if ((ptr[0] ^ pdev->vmirror) & 0x02) {
  541. if (ptr[0] & 0x02)
  542. PWC_TRACE("Image is mirrored.\n");
  543. else
  544. PWC_TRACE("Image is normal.\n");
  545. }
  546. pdev->vmirror = ptr[0] & 0x03;
  547. /* Sometimes the trailer of the 730 is still sent as a 4 byte packet
  548. after a short frame; this condition is filtered out specifically. A 4 byte
  549. frame doesn't make sense anyway.
  550. So we get either this sequence:
  551. drop_bit set -> 4 byte frame -> short frame -> good frame
  552. Or this one:
  553. drop_bit set -> short frame -> good frame
  554. So we drop either 3 or 2 frames in all!
  555. */
  556. if (fbuf->filled == 4)
  557. pdev->drop_frames++;
  558. }
  559. else if (pdev->type == 740 || pdev->type == 720) {
  560. unsigned char *ptr = (unsigned char *)fbuf->data;
  561. if ((ptr[0] ^ pdev->vmirror) & 0x01) {
  562. pwc_snapshot_button(pdev, ptr[0] & 0x01);
  563. }
  564. pdev->vmirror = ptr[0] & 0x03;
  565. }
  566. /* In case we were instructed to drop the frame, do so silently.
  567. The buffer pointers are not updated either (but the counters are reset below).
  568. */
  569. if (pdev->drop_frames > 0)
  570. pdev->drop_frames--;
  571. else {
  572. /* Check for underflow first */
  573. if (fbuf->filled < pdev->frame_total_size) {
  574. PWC_DEBUG_FLOW("Frame buffer underflow (%d bytes);"
  575. " discarded.\n", fbuf->filled);
  576. pdev->vframes_error++;
  577. }
  578. else {
  579. /* Send only once per EOF */
  580. awake = 1; /* delay wake_ups */
  581. /* Find our next frame to fill. This will always succeed, since we
  582. * nick a frame from either empty or full list, but if we had to
  583. * take it from the full list, it means a frame got dropped.
  584. */
  585. if (pwc_next_fill_frame(pdev))
  586. pwc_frame_dumped(pdev);
  587. }
  588. } /* !drop_frames */
  589. pdev->vframe_count++;
  590. return awake;
  591. }
  592. /* This gets called for the Isochronous pipe (video). This is done in
  593. * interrupt time, so it has to be fast, not crash, and not stall. Neat.
  594. */
  595. static void pwc_isoc_handler(struct urb *urb)
  596. {
  597. struct pwc_device *pdev;
  598. int i, fst, flen;
  599. int awake;
  600. struct pwc_frame_buf *fbuf;
  601. unsigned char *fillptr = NULL, *iso_buf = NULL;
  602. awake = 0;
  603. pdev = (struct pwc_device *)urb->context;
  604. if (pdev == NULL) {
  605. PWC_ERROR("isoc_handler() called with NULL device?!\n");
  606. return;
  607. }
  608. if (urb->status == -ENOENT || urb->status == -ECONNRESET) {
  609. PWC_DEBUG_OPEN("URB (%p) unlinked %ssynchronuously.\n", urb, urb->status == -ENOENT ? "" : "a");
  610. return;
  611. }
  612. if (urb->status != -EINPROGRESS && urb->status != 0) {
  613. const char *errmsg;
  614. errmsg = "Unknown";
  615. switch(urb->status) {
  616. case -ENOSR: errmsg = "Buffer error (overrun)"; break;
  617. case -EPIPE: errmsg = "Stalled (device not responding)"; break;
  618. case -EOVERFLOW: errmsg = "Babble (bad cable?)"; break;
  619. case -EPROTO: errmsg = "Bit-stuff error (bad cable?)"; break;
  620. case -EILSEQ: errmsg = "CRC/Timeout (could be anything)"; break;
  621. case -ETIME: errmsg = "Device does not respond"; break;
  622. }
  623. PWC_DEBUG_FLOW("pwc_isoc_handler() called with status %d [%s].\n", urb->status, errmsg);
  624. /* Give up after a number of contiguous errors on the USB bus.
  625. Appearantly something is wrong so we simulate an unplug event.
  626. */
  627. if (++pdev->visoc_errors > MAX_ISOC_ERRORS)
  628. {
  629. PWC_INFO("Too many ISOC errors, bailing out.\n");
  630. pdev->error_status = EIO;
  631. awake = 1;
  632. wake_up_interruptible(&pdev->frameq);
  633. }
  634. goto handler_end; // ugly, but practical
  635. }
  636. fbuf = pdev->fill_frame;
  637. if (fbuf == NULL) {
  638. PWC_ERROR("pwc_isoc_handler without valid fill frame.\n");
  639. awake = 1;
  640. goto handler_end;
  641. }
  642. else {
  643. fillptr = fbuf->data + fbuf->filled;
  644. }
  645. /* Reset ISOC error counter. We did get here, after all. */
  646. pdev->visoc_errors = 0;
  647. /* vsync: 0 = don't copy data
  648. 1 = sync-hunt
  649. 2 = synched
  650. */
  651. /* Compact data */
  652. for (i = 0; i < urb->number_of_packets; i++) {
  653. fst = urb->iso_frame_desc[i].status;
  654. flen = urb->iso_frame_desc[i].actual_length;
  655. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  656. if (fst == 0) {
  657. if (flen > 0) { /* if valid data... */
  658. if (pdev->vsync > 0) { /* ...and we are not sync-hunting... */
  659. pdev->vsync = 2;
  660. /* ...copy data to frame buffer, if possible */
  661. if (flen + fbuf->filled > pdev->frame_total_size) {
  662. PWC_DEBUG_FLOW("Frame buffer overflow (flen = %d, frame_total_size = %d).\n", flen, pdev->frame_total_size);
  663. pdev->vsync = 0; /* Hmm, let's wait for an EOF (end-of-frame) */
  664. pdev->vframes_error++;
  665. }
  666. else {
  667. memmove(fillptr, iso_buf, flen);
  668. fillptr += flen;
  669. }
  670. }
  671. fbuf->filled += flen;
  672. } /* ..flen > 0 */
  673. if (flen < pdev->vlast_packet_size) {
  674. /* Shorter packet... We probably have the end of an image-frame;
  675. wake up read() process and let select()/poll() do something.
  676. Decompression is done in user time over there.
  677. */
  678. if (pdev->vsync == 2) {
  679. if (pwc_rcv_short_packet(pdev, fbuf)) {
  680. awake = 1;
  681. fbuf = pdev->fill_frame;
  682. }
  683. }
  684. fbuf->filled = 0;
  685. fillptr = fbuf->data;
  686. pdev->vsync = 1;
  687. }
  688. pdev->vlast_packet_size = flen;
  689. } /* ..status == 0 */
  690. else {
  691. /* This is normally not interesting to the user, unless
  692. * you are really debugging something, default = 0 */
  693. static int iso_error;
  694. iso_error++;
  695. if (iso_error < 20)
  696. PWC_DEBUG_FLOW("Iso frame %d of USB has error %d\n", i, fst);
  697. }
  698. }
  699. handler_end:
  700. if (awake)
  701. wake_up_interruptible(&pdev->frameq);
  702. urb->dev = pdev->udev;
  703. i = usb_submit_urb(urb, GFP_ATOMIC);
  704. if (i != 0)
  705. PWC_ERROR("Error (%d) re-submitting urb in pwc_isoc_handler.\n", i);
  706. }
  707. int pwc_isoc_init(struct pwc_device *pdev)
  708. {
  709. struct usb_device *udev;
  710. struct urb *urb;
  711. int i, j, ret;
  712. struct usb_interface *intf;
  713. struct usb_host_interface *idesc = NULL;
  714. if (pdev == NULL)
  715. return -EFAULT;
  716. if (pdev->iso_init)
  717. return 0;
  718. pdev->vsync = 0;
  719. udev = pdev->udev;
  720. /* Get the current alternate interface, adjust packet size */
  721. if (!udev->actconfig)
  722. return -EFAULT;
  723. intf = usb_ifnum_to_if(udev, 0);
  724. if (intf)
  725. idesc = usb_altnum_to_altsetting(intf, pdev->valternate);
  726. if (!idesc)
  727. return -EFAULT;
  728. /* Search video endpoint */
  729. pdev->vmax_packet_size = -1;
  730. for (i = 0; i < idesc->desc.bNumEndpoints; i++) {
  731. if ((idesc->endpoint[i].desc.bEndpointAddress & 0xF) == pdev->vendpoint) {
  732. pdev->vmax_packet_size = le16_to_cpu(idesc->endpoint[i].desc.wMaxPacketSize);
  733. break;
  734. }
  735. }
  736. if (pdev->vmax_packet_size < 0 || pdev->vmax_packet_size > ISO_MAX_FRAME_SIZE) {
  737. PWC_ERROR("Failed to find packet size for video endpoint in current alternate setting.\n");
  738. return -ENFILE; /* Odd error, that should be noticeable */
  739. }
  740. /* Set alternate interface */
  741. ret = 0;
  742. PWC_DEBUG_OPEN("Setting alternate interface %d\n", pdev->valternate);
  743. ret = usb_set_interface(pdev->udev, 0, pdev->valternate);
  744. if (ret < 0)
  745. return ret;
  746. for (i = 0; i < MAX_ISO_BUFS; i++) {
  747. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  748. if (urb == NULL) {
  749. PWC_ERROR("Failed to allocate urb %d\n", i);
  750. ret = -ENOMEM;
  751. break;
  752. }
  753. pdev->sbuf[i].urb = urb;
  754. PWC_DEBUG_MEMORY("Allocated URB at 0x%p\n", urb);
  755. }
  756. if (ret) {
  757. /* De-allocate in reverse order */
  758. while (i--) {
  759. usb_free_urb(pdev->sbuf[i].urb);
  760. pdev->sbuf[i].urb = NULL;
  761. }
  762. return ret;
  763. }
  764. /* init URB structure */
  765. for (i = 0; i < MAX_ISO_BUFS; i++) {
  766. urb = pdev->sbuf[i].urb;
  767. urb->interval = 1; // devik
  768. urb->dev = udev;
  769. urb->pipe = usb_rcvisocpipe(udev, pdev->vendpoint);
  770. urb->transfer_flags = URB_ISO_ASAP;
  771. urb->transfer_buffer = pdev->sbuf[i].data;
  772. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  773. urb->complete = pwc_isoc_handler;
  774. urb->context = pdev;
  775. urb->start_frame = 0;
  776. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  777. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  778. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  779. urb->iso_frame_desc[j].length = pdev->vmax_packet_size;
  780. }
  781. }
  782. /* link */
  783. for (i = 0; i < MAX_ISO_BUFS; i++) {
  784. ret = usb_submit_urb(pdev->sbuf[i].urb, GFP_KERNEL);
  785. if (ret)
  786. PWC_ERROR("isoc_init() submit_urb %d failed with error %d\n", i, ret);
  787. else
  788. PWC_DEBUG_MEMORY("URB 0x%p submitted.\n", pdev->sbuf[i].urb);
  789. }
  790. /* All is done... */
  791. pdev->iso_init = 1;
  792. PWC_DEBUG_OPEN("<< pwc_isoc_init()\n");
  793. return 0;
  794. }
  795. static void pwc_iso_stop(struct pwc_device *pdev)
  796. {
  797. int i;
  798. /* Unlinking ISOC buffers one by one */
  799. for (i = 0; i < MAX_ISO_BUFS; i++) {
  800. struct urb *urb;
  801. urb = pdev->sbuf[i].urb;
  802. if (urb) {
  803. PWC_DEBUG_MEMORY("Unlinking URB %p\n", urb);
  804. usb_kill_urb(urb);
  805. }
  806. }
  807. }
  808. static void pwc_iso_free(struct pwc_device *pdev)
  809. {
  810. int i;
  811. /* Freeing ISOC buffers one by one */
  812. for (i = 0; i < MAX_ISO_BUFS; i++) {
  813. struct urb *urb;
  814. urb = pdev->sbuf[i].urb;
  815. if (urb) {
  816. PWC_DEBUG_MEMORY("Freeing URB\n");
  817. usb_free_urb(urb);
  818. pdev->sbuf[i].urb = NULL;
  819. }
  820. }
  821. }
  822. void pwc_isoc_cleanup(struct pwc_device *pdev)
  823. {
  824. PWC_DEBUG_OPEN(">> pwc_isoc_cleanup()\n");
  825. if (pdev == NULL)
  826. return;
  827. if (pdev->iso_init == 0)
  828. return;
  829. pwc_iso_stop(pdev);
  830. pwc_iso_free(pdev);
  831. /* Stop camera, but only if we are sure the camera is still there (unplug
  832. is signalled by EPIPE)
  833. */
  834. if (pdev->error_status && pdev->error_status != EPIPE) {
  835. PWC_DEBUG_OPEN("Setting alternate interface 0.\n");
  836. usb_set_interface(pdev->udev, 0, 0);
  837. }
  838. pdev->iso_init = 0;
  839. PWC_DEBUG_OPEN("<< pwc_isoc_cleanup()\n");
  840. }
  841. int pwc_try_video_mode(struct pwc_device *pdev, int width, int height, int new_fps, int new_compression, int new_snapshot)
  842. {
  843. int ret, start;
  844. /* Stop isoc stuff */
  845. pwc_isoc_cleanup(pdev);
  846. /* Reset parameters */
  847. pwc_reset_buffers(pdev);
  848. /* Try to set video mode... */
  849. start = ret = pwc_set_video_mode(pdev, width, height, new_fps, new_compression, new_snapshot);
  850. if (ret) {
  851. PWC_DEBUG_FLOW("pwc_set_video_mode attempt 1 failed.\n");
  852. /* That failed... restore old mode (we know that worked) */
  853. start = pwc_set_video_mode(pdev, pdev->view.x, pdev->view.y, pdev->vframes, pdev->vcompression, pdev->vsnapshot);
  854. if (start) {
  855. PWC_DEBUG_FLOW("pwc_set_video_mode attempt 2 failed.\n");
  856. }
  857. }
  858. if (start == 0)
  859. {
  860. if (pwc_isoc_init(pdev) < 0)
  861. {
  862. PWC_WARNING("Failed to restart ISOC transfers in pwc_try_video_mode.\n");
  863. ret = -EAGAIN; /* let's try again, who knows if it works a second time */
  864. }
  865. }
  866. pdev->drop_frames++; /* try to avoid garbage during switch */
  867. return ret; /* Return original error code */
  868. }
  869. /*********
  870. * sysfs
  871. *********/
  872. static struct pwc_device *cd_to_pwc(struct device *cd)
  873. {
  874. struct video_device *vdev = to_video_device(cd);
  875. return video_get_drvdata(vdev);
  876. }
  877. static ssize_t show_pan_tilt(struct device *class_dev,
  878. struct device_attribute *attr, char *buf)
  879. {
  880. struct pwc_device *pdev = cd_to_pwc(class_dev);
  881. return sprintf(buf, "%d %d\n", pdev->pan_angle, pdev->tilt_angle);
  882. }
  883. static ssize_t store_pan_tilt(struct device *class_dev,
  884. struct device_attribute *attr,
  885. const char *buf, size_t count)
  886. {
  887. struct pwc_device *pdev = cd_to_pwc(class_dev);
  888. int pan, tilt;
  889. int ret = -EINVAL;
  890. if (strncmp(buf, "reset", 5) == 0)
  891. ret = pwc_mpt_reset(pdev, 0x3);
  892. else if (sscanf(buf, "%d %d", &pan, &tilt) > 0)
  893. ret = pwc_mpt_set_angle(pdev, pan, tilt);
  894. if (ret < 0)
  895. return ret;
  896. return strlen(buf);
  897. }
  898. static DEVICE_ATTR(pan_tilt, S_IRUGO | S_IWUSR, show_pan_tilt,
  899. store_pan_tilt);
  900. static ssize_t show_snapshot_button_status(struct device *class_dev,
  901. struct device_attribute *attr, char *buf)
  902. {
  903. struct pwc_device *pdev = cd_to_pwc(class_dev);
  904. int status = pdev->snapshot_button_status;
  905. pdev->snapshot_button_status = 0;
  906. return sprintf(buf, "%d\n", status);
  907. }
  908. static DEVICE_ATTR(button, S_IRUGO | S_IWUSR, show_snapshot_button_status,
  909. NULL);
  910. static int pwc_create_sysfs_files(struct video_device *vdev)
  911. {
  912. struct pwc_device *pdev = video_get_drvdata(vdev);
  913. int rc;
  914. rc = device_create_file(&vdev->dev, &dev_attr_button);
  915. if (rc)
  916. goto err;
  917. if (pdev->features & FEATURE_MOTOR_PANTILT) {
  918. rc = device_create_file(&vdev->dev, &dev_attr_pan_tilt);
  919. if (rc)
  920. goto err_button;
  921. }
  922. return 0;
  923. err_button:
  924. device_remove_file(&vdev->dev, &dev_attr_button);
  925. err:
  926. PWC_ERROR("Could not create sysfs files.\n");
  927. return rc;
  928. }
  929. static void pwc_remove_sysfs_files(struct video_device *vdev)
  930. {
  931. struct pwc_device *pdev = video_get_drvdata(vdev);
  932. if (pdev->features & FEATURE_MOTOR_PANTILT)
  933. device_remove_file(&vdev->dev, &dev_attr_pan_tilt);
  934. device_remove_file(&vdev->dev, &dev_attr_button);
  935. }
  936. #ifdef CONFIG_USB_PWC_DEBUG
  937. static const char *pwc_sensor_type_to_string(unsigned int sensor_type)
  938. {
  939. switch(sensor_type) {
  940. case 0x00:
  941. return "Hyundai CMOS sensor";
  942. case 0x20:
  943. return "Sony CCD sensor + TDA8787";
  944. case 0x2E:
  945. return "Sony CCD sensor + Exas 98L59";
  946. case 0x2F:
  947. return "Sony CCD sensor + ADI 9804";
  948. case 0x30:
  949. return "Sharp CCD sensor + TDA8787";
  950. case 0x3E:
  951. return "Sharp CCD sensor + Exas 98L59";
  952. case 0x3F:
  953. return "Sharp CCD sensor + ADI 9804";
  954. case 0x40:
  955. return "UPA 1021 sensor";
  956. case 0x100:
  957. return "VGA sensor";
  958. case 0x101:
  959. return "PAL MR sensor";
  960. default:
  961. return "unknown type of sensor";
  962. }
  963. }
  964. #endif
  965. /***************************************************************************/
  966. /* Video4Linux functions */
  967. static int pwc_video_open(struct file *file)
  968. {
  969. int i, ret;
  970. struct video_device *vdev = video_devdata(file);
  971. struct pwc_device *pdev;
  972. PWC_DEBUG_OPEN(">> video_open called(vdev = 0x%p).\n", vdev);
  973. pdev = video_get_drvdata(vdev);
  974. BUG_ON(!pdev);
  975. if (pdev->vopen) {
  976. PWC_DEBUG_OPEN("I'm busy, someone is using the device.\n");
  977. return -EBUSY;
  978. }
  979. mutex_lock(&pdev->modlock);
  980. pwc_construct(pdev); /* set min/max sizes correct */
  981. if (!pdev->usb_init) {
  982. PWC_DEBUG_OPEN("Doing first time initialization.\n");
  983. pdev->usb_init = 1;
  984. /* Query sensor type */
  985. ret = pwc_get_cmos_sensor(pdev, &i);
  986. if (ret >= 0)
  987. {
  988. PWC_DEBUG_OPEN("This %s camera is equipped with a %s (%d).\n",
  989. pdev->vdev->name,
  990. pwc_sensor_type_to_string(i), i);
  991. }
  992. }
  993. /* Turn on camera */
  994. if (power_save) {
  995. i = pwc_camera_power(pdev, 1);
  996. if (i < 0)
  997. PWC_DEBUG_OPEN("Failed to restore power to the camera! (%d)\n", i);
  998. }
  999. /* Set LED on/off time */
  1000. if (pwc_set_leds(pdev, led_on, led_off) < 0)
  1001. PWC_DEBUG_OPEN("Failed to set LED on/off time.\n");
  1002. /* So far, so good. Allocate memory. */
  1003. i = pwc_allocate_buffers(pdev);
  1004. if (i < 0) {
  1005. PWC_DEBUG_OPEN("Failed to allocate buffers memory.\n");
  1006. pwc_free_buffers(pdev);
  1007. mutex_unlock(&pdev->modlock);
  1008. return i;
  1009. }
  1010. /* Reset buffers & parameters */
  1011. pwc_reset_buffers(pdev);
  1012. for (i = 0; i < pwc_mbufs; i++)
  1013. pdev->image_used[i] = 0;
  1014. pdev->vframe_count = 0;
  1015. pdev->vframes_dumped = 0;
  1016. pdev->vframes_error = 0;
  1017. pdev->visoc_errors = 0;
  1018. pdev->error_status = 0;
  1019. pwc_construct(pdev); /* set min/max sizes correct */
  1020. /* Set some defaults */
  1021. pdev->vsnapshot = 0;
  1022. /* Start iso pipe for video; first try the last used video size
  1023. (or the default one); if that fails try QCIF/10 or QSIF/10;
  1024. it that fails too, give up.
  1025. */
  1026. i = pwc_set_video_mode(pdev, pwc_image_sizes[pdev->vsize].x, pwc_image_sizes[pdev->vsize].y, pdev->vframes, pdev->vcompression, 0);
  1027. if (i) {
  1028. unsigned int default_resolution;
  1029. PWC_DEBUG_OPEN("First attempt at set_video_mode failed.\n");
  1030. if (pdev->type>= 730)
  1031. default_resolution = PSZ_QSIF;
  1032. else
  1033. default_resolution = PSZ_QCIF;
  1034. i = pwc_set_video_mode(pdev,
  1035. pwc_image_sizes[default_resolution].x,
  1036. pwc_image_sizes[default_resolution].y,
  1037. 10,
  1038. pdev->vcompression,
  1039. 0);
  1040. }
  1041. if (i) {
  1042. PWC_DEBUG_OPEN("Second attempt at set_video_mode failed.\n");
  1043. pwc_free_buffers(pdev);
  1044. mutex_unlock(&pdev->modlock);
  1045. return i;
  1046. }
  1047. i = pwc_isoc_init(pdev);
  1048. if (i) {
  1049. PWC_DEBUG_OPEN("Failed to init ISOC stuff = %d.\n", i);
  1050. pwc_isoc_cleanup(pdev);
  1051. pwc_free_buffers(pdev);
  1052. mutex_unlock(&pdev->modlock);
  1053. return i;
  1054. }
  1055. /* Initialize the webcam to sane value */
  1056. pwc_set_brightness(pdev, 0x7fff);
  1057. pwc_set_agc(pdev, 1, 0);
  1058. pdev->vopen++;
  1059. file->private_data = vdev;
  1060. mutex_unlock(&pdev->modlock);
  1061. PWC_DEBUG_OPEN("<< video_open() returns 0.\n");
  1062. return 0;
  1063. }
  1064. static void pwc_cleanup(struct pwc_device *pdev)
  1065. {
  1066. pwc_remove_sysfs_files(pdev->vdev);
  1067. video_unregister_device(pdev->vdev);
  1068. #ifdef CONFIG_USB_PWC_INPUT_EVDEV
  1069. if (pdev->button_dev)
  1070. input_unregister_device(pdev->button_dev);
  1071. #endif
  1072. kfree(pdev);
  1073. }
  1074. /* Note that all cleanup is done in the reverse order as in _open */
  1075. static int pwc_video_close(struct file *file)
  1076. {
  1077. struct video_device *vdev = file->private_data;
  1078. struct pwc_device *pdev;
  1079. int i, hint;
  1080. PWC_DEBUG_OPEN(">> video_close called(vdev = 0x%p).\n", vdev);
  1081. lock_kernel();
  1082. pdev = video_get_drvdata(vdev);
  1083. if (pdev->vopen == 0)
  1084. PWC_DEBUG_MODULE("video_close() called on closed device?\n");
  1085. /* Dump statistics, but only if a reasonable amount of frames were
  1086. processed (to prevent endless log-entries in case of snap-shot
  1087. programs)
  1088. */
  1089. if (pdev->vframe_count > 20)
  1090. PWC_DEBUG_MODULE("Closing video device: %d frames received, dumped %d frames, %d frames with errors.\n", pdev->vframe_count, pdev->vframes_dumped, pdev->vframes_error);
  1091. if (DEVICE_USE_CODEC1(pdev->type))
  1092. pwc_dec1_exit();
  1093. else
  1094. pwc_dec23_exit();
  1095. pwc_isoc_cleanup(pdev);
  1096. pwc_free_buffers(pdev);
  1097. /* Turn off LEDS and power down camera, but only when not unplugged */
  1098. if (!pdev->unplugged) {
  1099. /* Turn LEDs off */
  1100. if (pwc_set_leds(pdev, 0, 0) < 0)
  1101. PWC_DEBUG_MODULE("Failed to set LED on/off time.\n");
  1102. if (power_save) {
  1103. i = pwc_camera_power(pdev, 0);
  1104. if (i < 0)
  1105. PWC_ERROR("Failed to power down camera (%d)\n", i);
  1106. }
  1107. pdev->vopen--;
  1108. PWC_DEBUG_OPEN("<< video_close() vopen=%d\n", pdev->vopen);
  1109. } else {
  1110. pwc_cleanup(pdev);
  1111. /* search device_hint[] table if we occupy a slot, by any chance */
  1112. for (hint = 0; hint < MAX_DEV_HINTS; hint++)
  1113. if (device_hint[hint].pdev == pdev)
  1114. device_hint[hint].pdev = NULL;
  1115. }
  1116. unlock_kernel();
  1117. return 0;
  1118. }
  1119. /*
  1120. * FIXME: what about two parallel reads ????
  1121. * ANSWER: Not supported. You can't open the device more than once,
  1122. despite what the V4L1 interface says. First, I don't see
  1123. the need, second there's no mechanism of alerting the
  1124. 2nd/3rd/... process of events like changing image size.
  1125. And I don't see the point of blocking that for the
  1126. 2nd/3rd/... process.
  1127. In multi-threaded environments reading parallel from any
  1128. device is tricky anyhow.
  1129. */
  1130. static ssize_t pwc_video_read(struct file *file, char __user *buf,
  1131. size_t count, loff_t *ppos)
  1132. {
  1133. struct video_device *vdev = file->private_data;
  1134. struct pwc_device *pdev;
  1135. int noblock = file->f_flags & O_NONBLOCK;
  1136. DECLARE_WAITQUEUE(wait, current);
  1137. int bytes_to_read, rv = 0;
  1138. void *image_buffer_addr;
  1139. PWC_DEBUG_READ("pwc_video_read(vdev=0x%p, buf=%p, count=%zd) called.\n",
  1140. vdev, buf, count);
  1141. if (vdev == NULL)
  1142. return -EFAULT;
  1143. pdev = video_get_drvdata(vdev);
  1144. if (pdev == NULL)
  1145. return -EFAULT;
  1146. mutex_lock(&pdev->modlock);
  1147. if (pdev->error_status) {
  1148. rv = -pdev->error_status; /* Something happened, report what. */
  1149. goto err_out;
  1150. }
  1151. /* In case we're doing partial reads, we don't have to wait for a frame */
  1152. if (pdev->image_read_pos == 0) {
  1153. /* Do wait queueing according to the (doc)book */
  1154. add_wait_queue(&pdev->frameq, &wait);
  1155. while (pdev->full_frames == NULL) {
  1156. /* Check for unplugged/etc. here */
  1157. if (pdev->error_status) {
  1158. remove_wait_queue(&pdev->frameq, &wait);
  1159. set_current_state(TASK_RUNNING);
  1160. rv = -pdev->error_status ;
  1161. goto err_out;
  1162. }
  1163. if (noblock) {
  1164. remove_wait_queue(&pdev->frameq, &wait);
  1165. set_current_state(TASK_RUNNING);
  1166. rv = -EWOULDBLOCK;
  1167. goto err_out;
  1168. }
  1169. if (signal_pending(current)) {
  1170. remove_wait_queue(&pdev->frameq, &wait);
  1171. set_current_state(TASK_RUNNING);
  1172. rv = -ERESTARTSYS;
  1173. goto err_out;
  1174. }
  1175. schedule();
  1176. set_current_state(TASK_INTERRUPTIBLE);
  1177. }
  1178. remove_wait_queue(&pdev->frameq, &wait);
  1179. set_current_state(TASK_RUNNING);
  1180. /* Decompress and release frame */
  1181. if (pwc_handle_frame(pdev)) {
  1182. rv = -EFAULT;
  1183. goto err_out;
  1184. }
  1185. }
  1186. PWC_DEBUG_READ("Copying data to user space.\n");
  1187. if (pdev->vpalette == VIDEO_PALETTE_RAW)
  1188. bytes_to_read = pdev->frame_size + sizeof(struct pwc_raw_frame);
  1189. else
  1190. bytes_to_read = pdev->view.size;
  1191. /* copy bytes to user space; we allow for partial reads */
  1192. if (count + pdev->image_read_pos > bytes_to_read)
  1193. count = bytes_to_read - pdev->image_read_pos;
  1194. image_buffer_addr = pdev->image_data;
  1195. image_buffer_addr += pdev->images[pdev->fill_image].offset;
  1196. image_buffer_addr += pdev->image_read_pos;
  1197. if (copy_to_user(buf, image_buffer_addr, count)) {
  1198. rv = -EFAULT;
  1199. goto err_out;
  1200. }
  1201. pdev->image_read_pos += count;
  1202. if (pdev->image_read_pos >= bytes_to_read) { /* All data has been read */
  1203. pdev->image_read_pos = 0;
  1204. pwc_next_image(pdev);
  1205. }
  1206. mutex_unlock(&pdev->modlock);
  1207. return count;
  1208. err_out:
  1209. mutex_unlock(&pdev->modlock);
  1210. return rv;
  1211. }
  1212. static unsigned int pwc_video_poll(struct file *file, poll_table *wait)
  1213. {
  1214. struct video_device *vdev = file->private_data;
  1215. struct pwc_device *pdev;
  1216. if (vdev == NULL)
  1217. return -EFAULT;
  1218. pdev = video_get_drvdata(vdev);
  1219. if (pdev == NULL)
  1220. return -EFAULT;
  1221. poll_wait(file, &pdev->frameq, wait);
  1222. if (pdev->error_status)
  1223. return POLLERR;
  1224. if (pdev->full_frames != NULL) /* we have frames waiting */
  1225. return (POLLIN | POLLRDNORM);
  1226. return 0;
  1227. }
  1228. static long pwc_video_ioctl(struct file *file,
  1229. unsigned int cmd, unsigned long arg)
  1230. {
  1231. struct video_device *vdev = file->private_data;
  1232. struct pwc_device *pdev;
  1233. long r = -ENODEV;
  1234. if (!vdev)
  1235. goto out;
  1236. pdev = video_get_drvdata(vdev);
  1237. mutex_lock(&pdev->modlock);
  1238. if (!pdev->unplugged)
  1239. r = video_usercopy(file, cmd, arg, pwc_video_do_ioctl);
  1240. mutex_unlock(&pdev->modlock);
  1241. out:
  1242. return r;
  1243. }
  1244. static int pwc_video_mmap(struct file *file, struct vm_area_struct *vma)
  1245. {
  1246. struct video_device *vdev = file->private_data;
  1247. struct pwc_device *pdev;
  1248. unsigned long start;
  1249. unsigned long size;
  1250. unsigned long page, pos = 0;
  1251. int index;
  1252. PWC_DEBUG_MEMORY(">> %s\n", __func__);
  1253. pdev = video_get_drvdata(vdev);
  1254. size = vma->vm_end - vma->vm_start;
  1255. start = vma->vm_start;
  1256. /* Find the idx buffer for this mapping */
  1257. for (index = 0; index < pwc_mbufs; index++) {
  1258. pos = pdev->images[index].offset;
  1259. if ((pos>>PAGE_SHIFT) == vma->vm_pgoff)
  1260. break;
  1261. }
  1262. if (index == MAX_IMAGES)
  1263. return -EINVAL;
  1264. if (index == 0) {
  1265. /*
  1266. * Special case for v4l1. In v4l1, we map only one big buffer,
  1267. * but in v4l2 each buffer is mapped
  1268. */
  1269. unsigned long total_size;
  1270. total_size = pwc_mbufs * pdev->len_per_image;
  1271. if (size != pdev->len_per_image && size != total_size) {
  1272. PWC_ERROR("Wrong size (%lu) needed to be len_per_image=%d or total_size=%lu\n",
  1273. size, pdev->len_per_image, total_size);
  1274. return -EINVAL;
  1275. }
  1276. } else if (size > pdev->len_per_image)
  1277. return -EINVAL;
  1278. vma->vm_flags |= VM_IO; /* from 2.6.9-acX */
  1279. pos += (unsigned long)pdev->image_data;
  1280. while (size > 0) {
  1281. page = vmalloc_to_pfn((void *)pos);
  1282. if (remap_pfn_range(vma, start, page, PAGE_SIZE, PAGE_SHARED))
  1283. return -EAGAIN;
  1284. start += PAGE_SIZE;
  1285. pos += PAGE_SIZE;
  1286. if (size > PAGE_SIZE)
  1287. size -= PAGE_SIZE;
  1288. else
  1289. size = 0;
  1290. }
  1291. return 0;
  1292. }
  1293. /***************************************************************************/
  1294. /* USB functions */
  1295. /* This function gets called when a new device is plugged in or the usb core
  1296. * is loaded.
  1297. */
  1298. static int usb_pwc_probe(struct usb_interface *intf, const struct usb_device_id *id)
  1299. {
  1300. struct usb_device *udev = interface_to_usbdev(intf);
  1301. struct pwc_device *pdev = NULL;
  1302. int vendor_id, product_id, type_id;
  1303. int hint, rc;
  1304. int features = 0;
  1305. int video_nr = -1; /* default: use next available device */
  1306. char serial_number[30], *name;
  1307. vendor_id = le16_to_cpu(udev->descriptor.idVendor);
  1308. product_id = le16_to_cpu(udev->descriptor.idProduct);
  1309. /* Check if we can handle this device */
  1310. PWC_DEBUG_PROBE("probe() called [%04X %04X], if %d\n",
  1311. vendor_id, product_id,
  1312. intf->altsetting->desc.bInterfaceNumber);
  1313. /* the interfaces are probed one by one. We are only interested in the
  1314. video interface (0) now.
  1315. Interface 1 is the Audio Control, and interface 2 Audio itself.
  1316. */
  1317. if (intf->altsetting->desc.bInterfaceNumber > 0)
  1318. return -ENODEV;
  1319. if (vendor_id == 0x0471) {
  1320. switch (product_id) {
  1321. case 0x0302:
  1322. PWC_INFO("Philips PCA645VC USB webcam detected.\n");
  1323. name = "Philips 645 webcam";
  1324. type_id = 645;
  1325. break;
  1326. case 0x0303:
  1327. PWC_INFO("Philips PCA646VC USB webcam detected.\n");
  1328. name = "Philips 646 webcam";
  1329. type_id = 646;
  1330. break;
  1331. case 0x0304:
  1332. PWC_INFO("Askey VC010 type 2 USB webcam detected.\n");
  1333. name = "Askey VC010 webcam";
  1334. type_id = 646;
  1335. break;
  1336. case 0x0307:
  1337. PWC_INFO("Philips PCVC675K (Vesta) USB webcam detected.\n");
  1338. name = "Philips 675 webcam";
  1339. type_id = 675;
  1340. break;
  1341. case 0x0308:
  1342. PWC_INFO("Philips PCVC680K (Vesta Pro) USB webcam detected.\n");
  1343. name = "Philips 680 webcam";
  1344. type_id = 680;
  1345. break;
  1346. case 0x030C:
  1347. PWC_INFO("Philips PCVC690K (Vesta Pro Scan) USB webcam detected.\n");
  1348. name = "Philips 690 webcam";
  1349. type_id = 690;
  1350. break;
  1351. case 0x0310:
  1352. PWC_INFO("Philips PCVC730K (ToUCam Fun)/PCVC830 (ToUCam II) USB webcam detected.\n");
  1353. name = "Philips 730 webcam";
  1354. type_id = 730;
  1355. break;
  1356. case 0x0311:
  1357. PWC_INFO("Philips PCVC740K (ToUCam Pro)/PCVC840 (ToUCam II) USB webcam detected.\n");
  1358. name = "Philips 740 webcam";
  1359. type_id = 740;
  1360. break;
  1361. case 0x0312:
  1362. PWC_INFO("Philips PCVC750K (ToUCam Pro Scan) USB webcam detected.\n");
  1363. name = "Philips 750 webcam";
  1364. type_id = 750;
  1365. break;
  1366. case 0x0313:
  1367. PWC_INFO("Philips PCVC720K/40 (ToUCam XS) USB webcam detected.\n");
  1368. name = "Philips 720K/40 webcam";
  1369. type_id = 720;
  1370. break;
  1371. case 0x0329:
  1372. PWC_INFO("Philips SPC 900NC USB webcam detected.\n");
  1373. name = "Philips SPC 900NC webcam";
  1374. type_id = 740;
  1375. break;
  1376. default:
  1377. return -ENODEV;
  1378. break;
  1379. }
  1380. }
  1381. else if (vendor_id == 0x069A) {
  1382. switch(product_id) {
  1383. case 0x0001:
  1384. PWC_INFO("Askey VC010 type 1 USB webcam detected.\n");
  1385. name = "Askey VC010 webcam";
  1386. type_id = 645;
  1387. break;
  1388. default:
  1389. return -ENODEV;
  1390. break;
  1391. }
  1392. }
  1393. else if (vendor_id == 0x046d) {
  1394. switch(product_id) {
  1395. case 0x08b0:
  1396. PWC_INFO("Logitech QuickCam Pro 3000 USB webcam detected.\n");
  1397. name = "Logitech QuickCam Pro 3000";
  1398. type_id = 740; /* CCD sensor */
  1399. break;
  1400. case 0x08b1:
  1401. PWC_INFO("Logitech QuickCam Notebook Pro USB webcam detected.\n");
  1402. name = "Logitech QuickCam Notebook Pro";
  1403. type_id = 740; /* CCD sensor */
  1404. break;
  1405. case 0x08b2:
  1406. PWC_INFO("Logitech QuickCam 4000 Pro USB webcam detected.\n");
  1407. name = "Logitech QuickCam Pro 4000";
  1408. type_id = 740; /* CCD sensor */
  1409. break;
  1410. case 0x08b3:
  1411. PWC_INFO("Logitech QuickCam Zoom USB webcam detected.\n");
  1412. name = "Logitech QuickCam Zoom";
  1413. type_id = 740; /* CCD sensor */
  1414. break;
  1415. case 0x08B4:
  1416. PWC_INFO("Logitech QuickCam Zoom (new model) USB webcam detected.\n");
  1417. name = "Logitech QuickCam Zoom";
  1418. type_id = 740; /* CCD sensor */
  1419. power_save = 1;
  1420. break;
  1421. case 0x08b5:
  1422. PWC_INFO("Logitech QuickCam Orbit/Sphere USB webcam detected.\n");
  1423. name = "Logitech QuickCam Orbit";
  1424. type_id = 740; /* CCD sensor */
  1425. features |= FEATURE_MOTOR_PANTILT;
  1426. break;
  1427. case 0x08b6:
  1428. PWC_INFO("Logitech/Cisco VT Camera webcam detected.\n");
  1429. name = "Cisco VT Camera";
  1430. type_id = 740; /* CCD sensor */
  1431. break;
  1432. case 0x08b7:
  1433. PWC_INFO("Logitech ViewPort AV 100 webcam detected.\n");
  1434. name = "Logitech ViewPort AV 100";
  1435. type_id = 740; /* CCD sensor */
  1436. break;
  1437. case 0x08b8: /* Where this released? */
  1438. PWC_INFO("Logitech QuickCam detected (reserved ID).\n");
  1439. name = "Logitech QuickCam (res.)";
  1440. type_id = 730; /* Assuming CMOS */
  1441. break;
  1442. default:
  1443. return -ENODEV;
  1444. break;
  1445. }
  1446. }
  1447. else if (vendor_id == 0x055d) {
  1448. /* I don't know the difference between the C10 and the C30;
  1449. I suppose the difference is the sensor, but both cameras
  1450. work equally well with a type_id of 675
  1451. */
  1452. switch(product_id) {
  1453. case 0x9000:
  1454. PWC_INFO("Samsung MPC-C10 USB webcam detected.\n");
  1455. name = "Samsung MPC-C10";
  1456. type_id = 675;
  1457. break;
  1458. case 0x9001:
  1459. PWC_INFO("Samsung MPC-C30 USB webcam detected.\n");
  1460. name = "Samsung MPC-C30";
  1461. type_id = 675;
  1462. break;
  1463. case 0x9002:
  1464. PWC_INFO("Samsung SNC-35E (v3.0) USB webcam detected.\n");
  1465. name = "Samsung MPC-C30";
  1466. type_id = 740;
  1467. break;
  1468. default:
  1469. return -ENODEV;
  1470. break;
  1471. }
  1472. }
  1473. else if (vendor_id == 0x041e) {
  1474. switch(product_id) {
  1475. case 0x400c:
  1476. PWC_INFO("Creative Labs Webcam 5 detected.\n");
  1477. name = "Creative Labs Webcam 5";
  1478. type_id = 730;
  1479. break;
  1480. case 0x4011:
  1481. PWC_INFO("Creative Labs Webcam Pro Ex detected.\n");
  1482. name = "Creative Labs Webcam Pro Ex";
  1483. type_id = 740;
  1484. break;
  1485. default:
  1486. return -ENODEV;
  1487. break;
  1488. }
  1489. }
  1490. else if (vendor_id == 0x04cc) {
  1491. switch(product_id) {
  1492. case 0x8116:
  1493. PWC_INFO("Sotec Afina Eye USB webcam detected.\n");
  1494. name = "Sotec Afina Eye";
  1495. type_id = 730;
  1496. break;
  1497. default:
  1498. return -ENODEV;
  1499. break;
  1500. }
  1501. }
  1502. else if (vendor_id == 0x06be) {
  1503. switch(product_id) {
  1504. case 0x8116:
  1505. /* This is essentially the same cam as the Sotec Afina Eye */
  1506. PWC_INFO("AME Co. Afina Eye USB webcam detected.\n");
  1507. name = "AME Co. Afina Eye";
  1508. type_id = 750;
  1509. break;
  1510. default:
  1511. return -ENODEV;
  1512. break;
  1513. }
  1514. }
  1515. else if (vendor_id == 0x0d81) {
  1516. switch(product_id) {
  1517. case 0x1900:
  1518. PWC_INFO("Visionite VCS-UC300 USB webcam detected.\n");
  1519. name = "Visionite VCS-UC300";
  1520. type_id = 740; /* CCD sensor */
  1521. break;
  1522. case 0x1910:
  1523. PWC_INFO("Visionite VCS-UM100 USB webcam detected.\n");
  1524. name = "Visionite VCS-UM100";
  1525. type_id = 730; /* CMOS sensor */
  1526. break;
  1527. default:
  1528. return -ENODEV;
  1529. break;
  1530. }
  1531. }
  1532. else
  1533. return -ENODEV; /* Not any of the know types; but the list keeps growing. */
  1534. memset(serial_number, 0, 30);
  1535. usb_string(udev, udev->descriptor.iSerialNumber, serial_number, 29);
  1536. PWC_DEBUG_PROBE("Device serial number is %s\n", serial_number);
  1537. if (udev->descriptor.bNumConfigurations > 1)
  1538. PWC_WARNING("Warning: more than 1 configuration available.\n");
  1539. /* Allocate structure, initialize pointers, mutexes, etc. and link it to the usb_device */
  1540. pdev = kzalloc(sizeof(struct pwc_device), GFP_KERNEL);
  1541. if (pdev == NULL) {
  1542. PWC_ERROR("Oops, could not allocate memory for pwc_device.\n");
  1543. return -ENOMEM;
  1544. }
  1545. pdev->type = type_id;
  1546. pdev->vsize = default_size;
  1547. pdev->vframes = default_fps;
  1548. strcpy(pdev->serial, serial_number);
  1549. pdev->features = features;
  1550. if (vendor_id == 0x046D && product_id == 0x08B5) {
  1551. /* Logitech QuickCam Orbit
  1552. The ranges have been determined experimentally; they may differ from cam to cam.
  1553. Also, the exact ranges left-right and up-down are different for my cam
  1554. */
  1555. pdev->angle_range.pan_min = -7000;
  1556. pdev->angle_range.pan_max = 7000;
  1557. pdev->angle_range.tilt_min = -3000;
  1558. pdev->angle_range.tilt_max = 2500;
  1559. }
  1560. mutex_init(&pdev->modlock);
  1561. spin_lock_init(&pdev->ptrlock);
  1562. pdev->udev = udev;
  1563. init_waitqueue_head(&pdev->frameq);
  1564. pdev->vcompression = pwc_preferred_compression;
  1565. /* Allocate video_device structure */
  1566. pdev->vdev = video_device_alloc();
  1567. if (!pdev->vdev) {
  1568. PWC_ERROR("Err, cannot allocate video_device struture. Failing probe.");
  1569. rc = -ENOMEM;
  1570. goto err_free_mem;
  1571. }
  1572. memcpy(pdev->vdev, &pwc_template, sizeof(pwc_template));
  1573. pdev->vdev->parent = &intf->dev;
  1574. strcpy(pdev->vdev->name, name);
  1575. video_set_drvdata(pdev->vdev, pdev);
  1576. pdev->release = le16_to_cpu(udev->descriptor.bcdDevice);
  1577. PWC_DEBUG_PROBE("Release: %04x\n", pdev->release);
  1578. /* Now search device_hint[] table for a match, so we can hint a node number. */
  1579. for (hint = 0; hint < MAX_DEV_HINTS; hint++) {
  1580. if (((device_hint[hint].type == -1) || (device_hint[hint].type == pdev->type)) &&
  1581. (device_hint[hint].pdev == NULL)) {
  1582. /* so far, so good... try serial number */
  1583. if ((device_hint[hint].serial_number[0] == '*') || !strcmp(device_hint[hint].serial_number, serial_number)) {
  1584. /* match! */
  1585. video_nr = device_hint[hint].device_node;
  1586. PWC_DEBUG_PROBE("Found hint, will try to register as /dev/video%d\n", video_nr);
  1587. break;
  1588. }
  1589. }
  1590. }
  1591. pdev->vdev->release = video_device_release;
  1592. rc = video_register_device(pdev->vdev, VFL_TYPE_GRABBER, video_nr);
  1593. if (rc < 0) {
  1594. PWC_ERROR("Failed to register as video device (%d).\n", rc);
  1595. goto err_video_release;
  1596. }
  1597. PWC_INFO("Registered as %s.\n", video_device_node_name(pdev->vdev));
  1598. /* occupy slot */
  1599. if (hint < MAX_DEV_HINTS)
  1600. device_hint[hint].pdev = pdev;
  1601. PWC_DEBUG_PROBE("probe() function returning struct at 0x%p.\n", pdev);
  1602. usb_set_intfdata(intf, pdev);
  1603. rc = pwc_create_sysfs_files(pdev->vdev);
  1604. if (rc)
  1605. goto err_video_unreg;
  1606. /* Set the leds off */
  1607. pwc_set_leds(pdev, 0, 0);
  1608. pwc_camera_power(pdev, 0);
  1609. #ifdef CONFIG_USB_PWC_INPUT_EVDEV
  1610. /* register webcam snapshot button input device */
  1611. pdev->button_dev = input_allocate_device();
  1612. if (!pdev->button_dev) {
  1613. PWC_ERROR("Err, insufficient memory for webcam snapshot button device.");
  1614. rc = -ENOMEM;
  1615. pwc_remove_sysfs_files(pdev->vdev);
  1616. goto err_video_unreg;
  1617. }
  1618. usb_make_path(udev, pdev->button_phys, sizeof(pdev->button_phys));
  1619. strlcat(pdev->button_phys, "/input0", sizeof(pdev->button_phys));
  1620. pdev->button_dev->name = "PWC snapshot button";
  1621. pdev->button_dev->phys = pdev->button_phys;
  1622. usb_to_input_id(pdev->udev, &pdev->button_dev->id);
  1623. pdev->button_dev->dev.parent = &pdev->udev->dev;
  1624. pdev->button_dev->evbit[0] = BIT_MASK(EV_KEY);
  1625. pdev->button_dev->keybit[BIT_WORD(KEY_CAMERA)] = BIT_MASK(KEY_CAMERA);
  1626. rc = input_register_device(pdev->button_dev);
  1627. if (rc) {
  1628. input_free_device(pdev->button_dev);
  1629. pdev->button_dev = NULL;
  1630. pwc_remove_sysfs_files(pdev->vdev);
  1631. goto err_video_unreg;
  1632. }
  1633. #endif
  1634. return 0;
  1635. err_video_unreg:
  1636. if (hint < MAX_DEV_HINTS)
  1637. device_hint[hint].pdev = NULL;
  1638. video_unregister_device(pdev->vdev);
  1639. pdev->vdev = NULL; /* So we don't try to release it below */
  1640. err_video_release:
  1641. video_device_release(pdev->vdev);
  1642. err_free_mem:
  1643. kfree(pdev);
  1644. return rc;
  1645. }
  1646. /* The user yanked out the cable... */
  1647. static void usb_pwc_disconnect(struct usb_interface *intf)
  1648. {
  1649. struct pwc_device *pdev;
  1650. int hint;
  1651. lock_kernel();
  1652. pdev = usb_get_intfdata (intf);
  1653. usb_set_intfdata (intf, NULL);
  1654. if (pdev == NULL) {
  1655. PWC_ERROR("pwc_disconnect() Called without private pointer.\n");
  1656. goto disconnect_out;
  1657. }
  1658. if (pdev->udev == NULL) {
  1659. PWC_ERROR("pwc_disconnect() already called for %p\n", pdev);
  1660. goto disconnect_out;
  1661. }
  1662. if (pdev->udev != interface_to_usbdev(intf)) {
  1663. PWC_ERROR("pwc_disconnect() Woops: pointer mismatch udev/pdev.\n");
  1664. goto disconnect_out;
  1665. }
  1666. /* We got unplugged; this is signalled by an EPIPE error code */
  1667. if (pdev->vopen) {
  1668. PWC_INFO("Disconnected while webcam is in use!\n");
  1669. pdev->error_status = EPIPE;
  1670. }
  1671. /* Alert waiting processes */
  1672. wake_up_interruptible(&pdev->frameq);
  1673. /* Wait until device is closed */
  1674. if (pdev->vopen) {
  1675. mutex_lock(&pdev->modlock);
  1676. pdev->unplugged = 1;
  1677. mutex_unlock(&pdev->modlock);
  1678. pwc_iso_stop(pdev);
  1679. } else {
  1680. /* Device is closed, so we can safely unregister it */
  1681. PWC_DEBUG_PROBE("Unregistering video device in disconnect().\n");
  1682. pwc_cleanup(pdev);
  1683. disconnect_out:
  1684. /* search device_hint[] table if we occupy a slot, by any chance */
  1685. for (hint = 0; hint < MAX_DEV_HINTS; hint++)
  1686. if (device_hint[hint].pdev == pdev)
  1687. device_hint[hint].pdev = NULL;
  1688. }
  1689. unlock_kernel();
  1690. }
  1691. /*
  1692. * Initialization code & module stuff
  1693. */
  1694. static char *size;
  1695. static int fps;
  1696. static int fbufs;
  1697. static int mbufs;
  1698. static int compression = -1;
  1699. static int leds[2] = { -1, -1 };
  1700. static unsigned int leds_nargs;
  1701. static char *dev_hint[MAX_DEV_HINTS];
  1702. static unsigned int dev_hint_nargs;
  1703. module_param(size, charp, 0444);
  1704. module_param(fps, int, 0444);
  1705. module_param(fbufs, int, 0444);
  1706. module_param(mbufs, int, 0444);
  1707. #ifdef CONFIG_USB_PWC_DEBUG
  1708. module_param_named(trace, pwc_trace, int, 0644);
  1709. #endif
  1710. module_param(power_save, int, 0444);
  1711. module_param(compression, int, 0444);
  1712. module_param_array(leds, int, &leds_nargs, 0444);
  1713. module_param_array(dev_hint, charp, &dev_hint_nargs, 0444);
  1714. MODULE_PARM_DESC(size, "Initial image size. One of sqcif, qsif, qcif, sif, cif, vga");
  1715. MODULE_PARM_DESC(fps, "Initial frames per second. Varies with model, useful range 5-30");
  1716. MODULE_PARM_DESC(fbufs, "Number of internal frame buffers to reserve");
  1717. MODULE_PARM_DESC(mbufs, "Number of external (mmap()ed) image buffers");
  1718. #ifdef CONFIG_USB_PWC_DEBUG
  1719. MODULE_PARM_DESC(trace, "For debugging purposes");
  1720. #endif
  1721. MODULE_PARM_DESC(power_save, "Turn power save feature in camera on or off");
  1722. MODULE_PARM_DESC(compression, "Preferred compression quality. Range 0 (uncompressed) to 3 (high compression)");
  1723. MODULE_PARM_DESC(leds, "LED on,off time in milliseconds");
  1724. MODULE_PARM_DESC(dev_hint, "Device node hints");
  1725. MODULE_DESCRIPTION("Philips & OEM USB webcam driver");
  1726. MODULE_AUTHOR("Luc Saillard <luc@saillard.org>");
  1727. MODULE_LICENSE("GPL");
  1728. MODULE_ALIAS("pwcx");
  1729. MODULE_VERSION( PWC_VERSION );
  1730. static int __init usb_pwc_init(void)
  1731. {
  1732. int i, sz;
  1733. char *sizenames[PSZ_MAX] = { "sqcif", "qsif", "qcif", "sif", "cif", "vga" };
  1734. PWC_INFO("Philips webcam module version " PWC_VERSION " loaded.\n");
  1735. PWC_INFO("Supports Philips PCA645/646, PCVC675/680/690, PCVC720[40]/730/740/750 & PCVC830/840.\n");
  1736. PWC_INFO("Also supports the Askey VC010, various Logitech Quickcams, Samsung MPC-C10 and MPC-C30,\n");
  1737. PWC_INFO("the Creative WebCam 5 & Pro Ex, SOTEC Afina Eye and Visionite VCS-UC300 and VCS-UM100.\n");
  1738. if (fps) {
  1739. if (fps < 4 || fps > 30) {
  1740. PWC_ERROR("Framerate out of bounds (4-30).\n");
  1741. return -EINVAL;
  1742. }
  1743. default_fps = fps;
  1744. PWC_DEBUG_MODULE("Default framerate set to %d.\n", default_fps);
  1745. }
  1746. if (size) {
  1747. /* string; try matching with array */
  1748. for (sz = 0; sz < PSZ_MAX; sz++) {
  1749. if (!strcmp(sizenames[sz], size)) { /* Found! */
  1750. default_size = sz;
  1751. break;
  1752. }
  1753. }
  1754. if (sz == PSZ_MAX) {
  1755. PWC_ERROR("Size not recognized; try size=[sqcif | qsif | qcif | sif | cif | vga].\n");
  1756. return -EINVAL;
  1757. }
  1758. PWC_DEBUG_MODULE("Default image size set to %s [%dx%d].\n", sizenames[default_size], pwc_image_sizes[default_size].x, pwc_image_sizes[default_size].y);
  1759. }
  1760. if (mbufs) {
  1761. if (mbufs < 1 || mbufs > MAX_IMAGES) {
  1762. PWC_ERROR("Illegal number of mmap() buffers; use a number between 1 and %d.\n", MAX_IMAGES);
  1763. return -EINVAL;
  1764. }
  1765. pwc_mbufs = mbufs;
  1766. PWC_DEBUG_MODULE("Number of image buffers set to %d.\n", pwc_mbufs);
  1767. }
  1768. if (fbufs) {
  1769. if (fbufs < 2 || fbufs > MAX_FRAMES) {
  1770. PWC_ERROR("Illegal number of frame buffers; use a number between 2 and %d.\n", MAX_FRAMES);
  1771. return -EINVAL;
  1772. }
  1773. default_fbufs = fbufs;
  1774. PWC_DEBUG_MODULE("Number of frame buffers set to %d.\n", default_fbufs);
  1775. }
  1776. #ifdef CONFIG_USB_PWC_DEBUG
  1777. if (pwc_trace >= 0) {
  1778. PWC_DEBUG_MODULE("Trace options: 0x%04x\n", pwc_trace);
  1779. }
  1780. #endif
  1781. if (compression >= 0) {
  1782. if (compression > 3) {
  1783. PWC_ERROR("Invalid compression setting; use a number between 0 (uncompressed) and 3 (high).\n");
  1784. return -EINVAL;
  1785. }
  1786. pwc_preferred_compression = compression;
  1787. PWC_DEBUG_MODULE("Preferred compression set to %d.\n", pwc_preferred_compression);
  1788. }
  1789. if (power_save)
  1790. PWC_DEBUG_MODULE("Enabling power save on open/close.\n");
  1791. if (leds[0] >= 0)
  1792. led_on = leds[0];
  1793. if (leds[1] >= 0)
  1794. led_off = leds[1];
  1795. /* Big device node whoopla. Basically, it allows you to assign a
  1796. device node (/dev/videoX) to a camera, based on its type
  1797. & serial number. The format is [type[.serialnumber]:]node.
  1798. Any camera that isn't matched by these rules gets the next
  1799. available free device node.
  1800. */
  1801. for (i = 0; i < MAX_DEV_HINTS; i++) {
  1802. char *s, *colon, *dot;
  1803. /* This loop also initializes the array */
  1804. device_hint[i].pdev = NULL;
  1805. s = dev_hint[i];
  1806. if (s != NULL && *s != '\0') {
  1807. device_hint[i].type = -1; /* wildcard */
  1808. strcpy(device_hint[i].serial_number, "*");
  1809. /* parse string: chop at ':' & '/' */
  1810. colon = dot = s;
  1811. while (*colon != '\0' && *colon != ':')
  1812. colon++;
  1813. while (*dot != '\0' && *dot != '.')
  1814. dot++;
  1815. /* Few sanity checks */
  1816. if (*dot != '\0' && dot > colon) {
  1817. PWC_ERROR("Malformed camera hint: the colon must be after the dot.\n");
  1818. return -EINVAL;
  1819. }
  1820. if (*colon == '\0') {
  1821. /* No colon */
  1822. if (*dot != '\0') {
  1823. PWC_ERROR("Malformed camera hint: no colon + device node given.\n");
  1824. return -EINVAL;
  1825. }
  1826. else {
  1827. /* No type or serial number specified, just a number. */
  1828. device_hint[i].device_node =
  1829. simple_strtol(s, NULL, 10);
  1830. }
  1831. }
  1832. else {
  1833. /* There's a colon, so we have at least a type and a device node */
  1834. device_hint[i].type =
  1835. simple_strtol(s, NULL, 10);
  1836. device_hint[i].device_node =
  1837. simple_strtol(colon + 1, NULL, 10);
  1838. if (*dot != '\0') {
  1839. /* There's a serial number as well */
  1840. int k;
  1841. dot++;
  1842. k = 0;
  1843. while (*dot != ':' && k < 29) {
  1844. device_hint[i].serial_number[k++] = *dot;
  1845. dot++;
  1846. }
  1847. device_hint[i].serial_number[k] = '\0';
  1848. }
  1849. }
  1850. PWC_TRACE("device_hint[%d]:\n", i);
  1851. PWC_TRACE(" type : %d\n", device_hint[i].type);
  1852. PWC_TRACE(" serial# : %s\n", device_hint[i].serial_number);
  1853. PWC_TRACE(" node : %d\n", device_hint[i].device_node);
  1854. }
  1855. else
  1856. device_hint[i].type = 0; /* not filled */
  1857. } /* ..for MAX_DEV_HINTS */
  1858. PWC_DEBUG_PROBE("Registering driver at address 0x%p.\n", &pwc_driver);
  1859. return usb_register(&pwc_driver);
  1860. }
  1861. static void __exit usb_pwc_exit(void)
  1862. {
  1863. PWC_DEBUG_MODULE("Deregistering driver.\n");
  1864. usb_deregister(&pwc_driver);
  1865. PWC_INFO("Philips webcam module removed.\n");
  1866. }
  1867. module_init(usb_pwc_init);
  1868. module_exit(usb_pwc_exit);
  1869. /* vim: set cino= formatoptions=croql cindent shiftwidth=8 tabstop=8: */