PageRenderTime 3215ms CodeModel.GetById 23ms RepoModel.GetById 4ms app.codeStats 0ms

/drivers/media/video/stk-webcam.c

https://bitbucket.org/wisechild/galaxy-nexus
C | 1392 lines | 1141 code | 169 blank | 82 comment | 229 complexity | 669e62584534e4b00e422ec91bcdd769 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  1. /*
  2. * stk-webcam.c : Driver for Syntek 1125 USB webcam controller
  3. *
  4. * Copyright (C) 2006 Nicolas VIVIEN
  5. * Copyright 2007-2008 Jaime Velasco Juan <jsagarribay@gmail.com>
  6. *
  7. * Some parts are inspired from cafe_ccic.c
  8. * Copyright 2006-2007 Jonathan Corbet
  9. *
  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. * any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. #include <linux/module.h>
  25. #include <linux/init.h>
  26. #include <linux/kernel.h>
  27. #include <linux/errno.h>
  28. #include <linux/slab.h>
  29. #include <linux/usb.h>
  30. #include <linux/mm.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/videodev2.h>
  33. #include <media/v4l2-common.h>
  34. #include <media/v4l2-ioctl.h>
  35. #include "stk-webcam.h"
  36. static int hflip = 1;
  37. module_param(hflip, bool, 0444);
  38. MODULE_PARM_DESC(hflip, "Horizontal image flip (mirror). Defaults to 1");
  39. static int vflip = 1;
  40. module_param(vflip, bool, 0444);
  41. MODULE_PARM_DESC(vflip, "Vertical image flip. Defaults to 1");
  42. static int debug;
  43. module_param(debug, int, 0444);
  44. MODULE_PARM_DESC(debug, "Debug v4l ioctls. Defaults to 0");
  45. MODULE_LICENSE("GPL");
  46. MODULE_AUTHOR("Jaime Velasco Juan <jsagarribay@gmail.com> and Nicolas VIVIEN");
  47. MODULE_DESCRIPTION("Syntek DC1125 webcam driver");
  48. /* Some cameras have audio interfaces, we aren't interested in those */
  49. static struct usb_device_id stkwebcam_table[] = {
  50. { USB_DEVICE_AND_INTERFACE_INFO(0x174f, 0xa311, 0xff, 0xff, 0xff) },
  51. { USB_DEVICE_AND_INTERFACE_INFO(0x05e1, 0x0501, 0xff, 0xff, 0xff) },
  52. { }
  53. };
  54. MODULE_DEVICE_TABLE(usb, stkwebcam_table);
  55. /*
  56. * Basic stuff
  57. */
  58. int stk_camera_write_reg(struct stk_camera *dev, u16 index, u8 value)
  59. {
  60. struct usb_device *udev = dev->udev;
  61. int ret;
  62. ret = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  63. 0x01,
  64. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  65. value,
  66. index,
  67. NULL,
  68. 0,
  69. 500);
  70. if (ret < 0)
  71. return ret;
  72. else
  73. return 0;
  74. }
  75. int stk_camera_read_reg(struct stk_camera *dev, u16 index, int *value)
  76. {
  77. struct usb_device *udev = dev->udev;
  78. int ret;
  79. ret = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  80. 0x00,
  81. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  82. 0x00,
  83. index,
  84. (u8 *) value,
  85. sizeof(u8),
  86. 500);
  87. if (ret < 0)
  88. return ret;
  89. else
  90. return 0;
  91. }
  92. static int stk_start_stream(struct stk_camera *dev)
  93. {
  94. int value;
  95. int i, ret;
  96. int value_116, value_117;
  97. if (!is_present(dev))
  98. return -ENODEV;
  99. if (!is_memallocd(dev) || !is_initialised(dev)) {
  100. STK_ERROR("FIXME: Buffers are not allocated\n");
  101. return -EFAULT;
  102. }
  103. ret = usb_set_interface(dev->udev, 0, 5);
  104. if (ret < 0)
  105. STK_ERROR("usb_set_interface failed !\n");
  106. if (stk_sensor_wakeup(dev))
  107. STK_ERROR("error awaking the sensor\n");
  108. stk_camera_read_reg(dev, 0x0116, &value_116);
  109. stk_camera_read_reg(dev, 0x0117, &value_117);
  110. stk_camera_write_reg(dev, 0x0116, 0x0000);
  111. stk_camera_write_reg(dev, 0x0117, 0x0000);
  112. stk_camera_read_reg(dev, 0x0100, &value);
  113. stk_camera_write_reg(dev, 0x0100, value | 0x80);
  114. stk_camera_write_reg(dev, 0x0116, value_116);
  115. stk_camera_write_reg(dev, 0x0117, value_117);
  116. for (i = 0; i < MAX_ISO_BUFS; i++) {
  117. if (dev->isobufs[i].urb) {
  118. ret = usb_submit_urb(dev->isobufs[i].urb, GFP_KERNEL);
  119. atomic_inc(&dev->urbs_used);
  120. if (ret)
  121. return ret;
  122. }
  123. }
  124. set_streaming(dev);
  125. return 0;
  126. }
  127. static int stk_stop_stream(struct stk_camera *dev)
  128. {
  129. int value;
  130. int i;
  131. if (is_present(dev)) {
  132. stk_camera_read_reg(dev, 0x0100, &value);
  133. stk_camera_write_reg(dev, 0x0100, value & ~0x80);
  134. if (dev->isobufs != NULL) {
  135. for (i = 0; i < MAX_ISO_BUFS; i++) {
  136. if (dev->isobufs[i].urb)
  137. usb_kill_urb(dev->isobufs[i].urb);
  138. }
  139. }
  140. unset_streaming(dev);
  141. if (usb_set_interface(dev->udev, 0, 0))
  142. STK_ERROR("usb_set_interface failed !\n");
  143. if (stk_sensor_sleep(dev))
  144. STK_ERROR("error suspending the sensor\n");
  145. }
  146. return 0;
  147. }
  148. /*
  149. * This seems to be the shortest init sequence we
  150. * must do in order to find the sensor
  151. * Bit 5 of reg. 0x0000 here is important, when reset to 0 the sensor
  152. * is also reset. Maybe powers down it?
  153. * Rest of values don't make a difference
  154. */
  155. static struct regval stk1125_initvals[] = {
  156. /*TODO: What means this sequence? */
  157. {0x0000, 0x24},
  158. {0x0100, 0x21},
  159. {0x0002, 0x68},
  160. {0x0003, 0x80},
  161. {0x0005, 0x00},
  162. {0x0007, 0x03},
  163. {0x000d, 0x00},
  164. {0x000f, 0x02},
  165. {0x0300, 0x12},
  166. {0x0350, 0x41},
  167. {0x0351, 0x00},
  168. {0x0352, 0x00},
  169. {0x0353, 0x00},
  170. {0x0018, 0x10},
  171. {0x0019, 0x00},
  172. {0x001b, 0x0e},
  173. {0x001c, 0x46},
  174. {0x0300, 0x80},
  175. {0x001a, 0x04},
  176. {0x0110, 0x00},
  177. {0x0111, 0x00},
  178. {0x0112, 0x00},
  179. {0x0113, 0x00},
  180. {0xffff, 0xff},
  181. };
  182. static int stk_initialise(struct stk_camera *dev)
  183. {
  184. struct regval *rv;
  185. int ret;
  186. if (!is_present(dev))
  187. return -ENODEV;
  188. if (is_initialised(dev))
  189. return 0;
  190. rv = stk1125_initvals;
  191. while (rv->reg != 0xffff) {
  192. ret = stk_camera_write_reg(dev, rv->reg, rv->val);
  193. if (ret)
  194. return ret;
  195. rv++;
  196. }
  197. if (stk_sensor_init(dev) == 0) {
  198. set_initialised(dev);
  199. return 0;
  200. } else
  201. return -1;
  202. }
  203. /* *********************************************** */
  204. /*
  205. * This function is called as an URB transfert is complete (Isochronous pipe).
  206. * So, the traitement is done in interrupt time, so it has be fast, not crash,
  207. * and not stall. Neat.
  208. */
  209. static void stk_isoc_handler(struct urb *urb)
  210. {
  211. int i;
  212. int ret;
  213. int framelen;
  214. unsigned long flags;
  215. unsigned char *fill = NULL;
  216. unsigned char *iso_buf = NULL;
  217. struct stk_camera *dev;
  218. struct stk_sio_buffer *fb;
  219. dev = (struct stk_camera *) urb->context;
  220. if (dev == NULL) {
  221. STK_ERROR("isoc_handler called with NULL device !\n");
  222. return;
  223. }
  224. if (urb->status == -ENOENT || urb->status == -ECONNRESET
  225. || urb->status == -ESHUTDOWN) {
  226. atomic_dec(&dev->urbs_used);
  227. return;
  228. }
  229. spin_lock_irqsave(&dev->spinlock, flags);
  230. if (urb->status != -EINPROGRESS && urb->status != 0) {
  231. STK_ERROR("isoc_handler: urb->status == %d\n", urb->status);
  232. goto resubmit;
  233. }
  234. if (list_empty(&dev->sio_avail)) {
  235. /*FIXME Stop streaming after a while */
  236. (void) (printk_ratelimit() &&
  237. STK_ERROR("isoc_handler without available buffer!\n"));
  238. goto resubmit;
  239. }
  240. fb = list_first_entry(&dev->sio_avail,
  241. struct stk_sio_buffer, list);
  242. fill = fb->buffer + fb->v4lbuf.bytesused;
  243. for (i = 0; i < urb->number_of_packets; i++) {
  244. if (urb->iso_frame_desc[i].status != 0) {
  245. if (urb->iso_frame_desc[i].status != -EXDEV)
  246. STK_ERROR("Frame %d has error %d\n", i,
  247. urb->iso_frame_desc[i].status);
  248. continue;
  249. }
  250. framelen = urb->iso_frame_desc[i].actual_length;
  251. iso_buf = urb->transfer_buffer + urb->iso_frame_desc[i].offset;
  252. if (framelen <= 4)
  253. continue; /* no data */
  254. /*
  255. * we found something informational from there
  256. * the isoc frames have to type of headers
  257. * type1: 00 xx 00 00 or 20 xx 00 00
  258. * type2: 80 xx 00 00 00 00 00 00 or a0 xx 00 00 00 00 00 00
  259. * xx is a sequencer which has never been seen over 0x3f
  260. * imho data written down looks like bayer, i see similarities
  261. * after every 640 bytes
  262. */
  263. if (*iso_buf & 0x80) {
  264. framelen -= 8;
  265. iso_buf += 8;
  266. /* This marks a new frame */
  267. if (fb->v4lbuf.bytesused != 0
  268. && fb->v4lbuf.bytesused != dev->frame_size) {
  269. (void) (printk_ratelimit() &&
  270. STK_ERROR("frame %d, "
  271. "bytesused=%d, skipping\n",
  272. i, fb->v4lbuf.bytesused));
  273. fb->v4lbuf.bytesused = 0;
  274. fill = fb->buffer;
  275. } else if (fb->v4lbuf.bytesused == dev->frame_size) {
  276. if (list_is_singular(&dev->sio_avail)) {
  277. /* Always reuse the last buffer */
  278. fb->v4lbuf.bytesused = 0;
  279. fill = fb->buffer;
  280. } else {
  281. list_move_tail(dev->sio_avail.next,
  282. &dev->sio_full);
  283. wake_up(&dev->wait_frame);
  284. fb = list_first_entry(&dev->sio_avail,
  285. struct stk_sio_buffer, list);
  286. fb->v4lbuf.bytesused = 0;
  287. fill = fb->buffer;
  288. }
  289. }
  290. } else {
  291. framelen -= 4;
  292. iso_buf += 4;
  293. }
  294. /* Our buffer is full !!! */
  295. if (framelen + fb->v4lbuf.bytesused > dev->frame_size) {
  296. (void) (printk_ratelimit() &&
  297. STK_ERROR("Frame buffer overflow, lost sync\n"));
  298. /*FIXME Do something here? */
  299. continue;
  300. }
  301. spin_unlock_irqrestore(&dev->spinlock, flags);
  302. memcpy(fill, iso_buf, framelen);
  303. spin_lock_irqsave(&dev->spinlock, flags);
  304. fill += framelen;
  305. /* New size of our buffer */
  306. fb->v4lbuf.bytesused += framelen;
  307. }
  308. resubmit:
  309. spin_unlock_irqrestore(&dev->spinlock, flags);
  310. urb->dev = dev->udev;
  311. ret = usb_submit_urb(urb, GFP_ATOMIC);
  312. if (ret != 0) {
  313. STK_ERROR("Error (%d) re-submitting urb in stk_isoc_handler.\n",
  314. ret);
  315. }
  316. }
  317. /* -------------------------------------------- */
  318. static int stk_prepare_iso(struct stk_camera *dev)
  319. {
  320. void *kbuf;
  321. int i, j;
  322. struct urb *urb;
  323. struct usb_device *udev;
  324. if (dev == NULL)
  325. return -ENXIO;
  326. udev = dev->udev;
  327. if (dev->isobufs)
  328. STK_ERROR("isobufs already allocated. Bad\n");
  329. else
  330. dev->isobufs = kzalloc(MAX_ISO_BUFS * sizeof(*dev->isobufs),
  331. GFP_KERNEL);
  332. if (dev->isobufs == NULL) {
  333. STK_ERROR("Unable to allocate iso buffers\n");
  334. return -ENOMEM;
  335. }
  336. for (i = 0; i < MAX_ISO_BUFS; i++) {
  337. if (dev->isobufs[i].data == NULL) {
  338. kbuf = kzalloc(ISO_BUFFER_SIZE, GFP_KERNEL);
  339. if (kbuf == NULL) {
  340. STK_ERROR("Failed to allocate iso buffer %d\n",
  341. i);
  342. goto isobufs_out;
  343. }
  344. dev->isobufs[i].data = kbuf;
  345. } else
  346. STK_ERROR("isobuf data already allocated\n");
  347. if (dev->isobufs[i].urb == NULL) {
  348. urb = usb_alloc_urb(ISO_FRAMES_PER_DESC, GFP_KERNEL);
  349. if (urb == NULL) {
  350. STK_ERROR("Failed to allocate URB %d\n", i);
  351. goto isobufs_out;
  352. }
  353. dev->isobufs[i].urb = urb;
  354. } else {
  355. STK_ERROR("Killing URB\n");
  356. usb_kill_urb(dev->isobufs[i].urb);
  357. urb = dev->isobufs[i].urb;
  358. }
  359. urb->interval = 1;
  360. urb->dev = udev;
  361. urb->pipe = usb_rcvisocpipe(udev, dev->isoc_ep);
  362. urb->transfer_flags = URB_ISO_ASAP;
  363. urb->transfer_buffer = dev->isobufs[i].data;
  364. urb->transfer_buffer_length = ISO_BUFFER_SIZE;
  365. urb->complete = stk_isoc_handler;
  366. urb->context = dev;
  367. urb->start_frame = 0;
  368. urb->number_of_packets = ISO_FRAMES_PER_DESC;
  369. for (j = 0; j < ISO_FRAMES_PER_DESC; j++) {
  370. urb->iso_frame_desc[j].offset = j * ISO_MAX_FRAME_SIZE;
  371. urb->iso_frame_desc[j].length = ISO_MAX_FRAME_SIZE;
  372. }
  373. }
  374. set_memallocd(dev);
  375. return 0;
  376. isobufs_out:
  377. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].data; i++)
  378. kfree(dev->isobufs[i].data);
  379. for (i = 0; i < MAX_ISO_BUFS && dev->isobufs[i].urb; i++)
  380. usb_free_urb(dev->isobufs[i].urb);
  381. kfree(dev->isobufs);
  382. dev->isobufs = NULL;
  383. return -ENOMEM;
  384. }
  385. static void stk_clean_iso(struct stk_camera *dev)
  386. {
  387. int i;
  388. if (dev == NULL || dev->isobufs == NULL)
  389. return;
  390. for (i = 0; i < MAX_ISO_BUFS; i++) {
  391. struct urb *urb;
  392. urb = dev->isobufs[i].urb;
  393. if (urb) {
  394. if (atomic_read(&dev->urbs_used) && is_present(dev))
  395. usb_kill_urb(urb);
  396. usb_free_urb(urb);
  397. }
  398. kfree(dev->isobufs[i].data);
  399. }
  400. kfree(dev->isobufs);
  401. dev->isobufs = NULL;
  402. unset_memallocd(dev);
  403. }
  404. static int stk_setup_siobuf(struct stk_camera *dev, int index)
  405. {
  406. struct stk_sio_buffer *buf = dev->sio_bufs + index;
  407. INIT_LIST_HEAD(&buf->list);
  408. buf->v4lbuf.length = PAGE_ALIGN(dev->frame_size);
  409. buf->buffer = vmalloc_user(buf->v4lbuf.length);
  410. if (buf->buffer == NULL)
  411. return -ENOMEM;
  412. buf->mapcount = 0;
  413. buf->dev = dev;
  414. buf->v4lbuf.index = index;
  415. buf->v4lbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  416. buf->v4lbuf.field = V4L2_FIELD_NONE;
  417. buf->v4lbuf.memory = V4L2_MEMORY_MMAP;
  418. buf->v4lbuf.m.offset = 2*index*buf->v4lbuf.length;
  419. return 0;
  420. }
  421. static int stk_free_sio_buffers(struct stk_camera *dev)
  422. {
  423. int i;
  424. int nbufs;
  425. unsigned long flags;
  426. if (dev->n_sbufs == 0 || dev->sio_bufs == NULL)
  427. return 0;
  428. /*
  429. * If any buffers are mapped, we cannot free them at all.
  430. */
  431. for (i = 0; i < dev->n_sbufs; i++) {
  432. if (dev->sio_bufs[i].mapcount > 0)
  433. return -EBUSY;
  434. }
  435. /*
  436. * OK, let's do it.
  437. */
  438. spin_lock_irqsave(&dev->spinlock, flags);
  439. INIT_LIST_HEAD(&dev->sio_avail);
  440. INIT_LIST_HEAD(&dev->sio_full);
  441. nbufs = dev->n_sbufs;
  442. dev->n_sbufs = 0;
  443. spin_unlock_irqrestore(&dev->spinlock, flags);
  444. for (i = 0; i < nbufs; i++) {
  445. if (dev->sio_bufs[i].buffer != NULL)
  446. vfree(dev->sio_bufs[i].buffer);
  447. }
  448. kfree(dev->sio_bufs);
  449. dev->sio_bufs = NULL;
  450. return 0;
  451. }
  452. static int stk_prepare_sio_buffers(struct stk_camera *dev, unsigned n_sbufs)
  453. {
  454. int i;
  455. if (dev->sio_bufs != NULL)
  456. STK_ERROR("sio_bufs already allocated\n");
  457. else {
  458. dev->sio_bufs = kzalloc(n_sbufs * sizeof(struct stk_sio_buffer),
  459. GFP_KERNEL);
  460. if (dev->sio_bufs == NULL)
  461. return -ENOMEM;
  462. for (i = 0; i < n_sbufs; i++) {
  463. if (stk_setup_siobuf(dev, i))
  464. return (dev->n_sbufs > 1)? 0 : -ENOMEM;
  465. dev->n_sbufs = i+1;
  466. }
  467. }
  468. return 0;
  469. }
  470. static int stk_allocate_buffers(struct stk_camera *dev, unsigned n_sbufs)
  471. {
  472. int err;
  473. err = stk_prepare_iso(dev);
  474. if (err) {
  475. stk_clean_iso(dev);
  476. return err;
  477. }
  478. err = stk_prepare_sio_buffers(dev, n_sbufs);
  479. if (err) {
  480. stk_free_sio_buffers(dev);
  481. return err;
  482. }
  483. return 0;
  484. }
  485. static void stk_free_buffers(struct stk_camera *dev)
  486. {
  487. stk_clean_iso(dev);
  488. stk_free_sio_buffers(dev);
  489. }
  490. /* -------------------------------------------- */
  491. /* v4l file operations */
  492. static int v4l_stk_open(struct file *fp)
  493. {
  494. struct stk_camera *dev;
  495. struct video_device *vdev;
  496. vdev = video_devdata(fp);
  497. dev = vdev_to_camera(vdev);
  498. if (dev == NULL || !is_present(dev)) {
  499. return -ENXIO;
  500. }
  501. fp->private_data = dev;
  502. usb_autopm_get_interface(dev->interface);
  503. return 0;
  504. }
  505. static int v4l_stk_release(struct file *fp)
  506. {
  507. struct stk_camera *dev = fp->private_data;
  508. if (dev->owner == fp) {
  509. stk_stop_stream(dev);
  510. stk_free_buffers(dev);
  511. dev->owner = NULL;
  512. }
  513. if(is_present(dev))
  514. usb_autopm_put_interface(dev->interface);
  515. return 0;
  516. }
  517. static ssize_t v4l_stk_read(struct file *fp, char __user *buf,
  518. size_t count, loff_t *f_pos)
  519. {
  520. int i;
  521. int ret;
  522. unsigned long flags;
  523. struct stk_sio_buffer *sbuf;
  524. struct stk_camera *dev = fp->private_data;
  525. if (!is_present(dev))
  526. return -EIO;
  527. if (dev->owner && dev->owner != fp)
  528. return -EBUSY;
  529. dev->owner = fp;
  530. if (!is_streaming(dev)) {
  531. if (stk_initialise(dev)
  532. || stk_allocate_buffers(dev, 3)
  533. || stk_start_stream(dev))
  534. return -ENOMEM;
  535. spin_lock_irqsave(&dev->spinlock, flags);
  536. for (i = 0; i < dev->n_sbufs; i++) {
  537. list_add_tail(&dev->sio_bufs[i].list, &dev->sio_avail);
  538. dev->sio_bufs[i].v4lbuf.flags = V4L2_BUF_FLAG_QUEUED;
  539. }
  540. spin_unlock_irqrestore(&dev->spinlock, flags);
  541. }
  542. if (*f_pos == 0) {
  543. if (fp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  544. return -EWOULDBLOCK;
  545. ret = wait_event_interruptible(dev->wait_frame,
  546. !list_empty(&dev->sio_full) || !is_present(dev));
  547. if (ret)
  548. return ret;
  549. if (!is_present(dev))
  550. return -EIO;
  551. }
  552. if (count + *f_pos > dev->frame_size)
  553. count = dev->frame_size - *f_pos;
  554. spin_lock_irqsave(&dev->spinlock, flags);
  555. if (list_empty(&dev->sio_full)) {
  556. spin_unlock_irqrestore(&dev->spinlock, flags);
  557. STK_ERROR("BUG: No siobufs ready\n");
  558. return 0;
  559. }
  560. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  561. spin_unlock_irqrestore(&dev->spinlock, flags);
  562. if (copy_to_user(buf, sbuf->buffer + *f_pos, count))
  563. return -EFAULT;
  564. *f_pos += count;
  565. if (*f_pos >= dev->frame_size) {
  566. *f_pos = 0;
  567. spin_lock_irqsave(&dev->spinlock, flags);
  568. list_move_tail(&sbuf->list, &dev->sio_avail);
  569. spin_unlock_irqrestore(&dev->spinlock, flags);
  570. }
  571. return count;
  572. }
  573. static unsigned int v4l_stk_poll(struct file *fp, poll_table *wait)
  574. {
  575. struct stk_camera *dev = fp->private_data;
  576. poll_wait(fp, &dev->wait_frame, wait);
  577. if (!is_present(dev))
  578. return POLLERR;
  579. if (!list_empty(&dev->sio_full))
  580. return (POLLIN | POLLRDNORM);
  581. return 0;
  582. }
  583. static void stk_v4l_vm_open(struct vm_area_struct *vma)
  584. {
  585. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  586. sbuf->mapcount++;
  587. }
  588. static void stk_v4l_vm_close(struct vm_area_struct *vma)
  589. {
  590. struct stk_sio_buffer *sbuf = vma->vm_private_data;
  591. sbuf->mapcount--;
  592. if (sbuf->mapcount == 0)
  593. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_MAPPED;
  594. }
  595. static const struct vm_operations_struct stk_v4l_vm_ops = {
  596. .open = stk_v4l_vm_open,
  597. .close = stk_v4l_vm_close
  598. };
  599. static int v4l_stk_mmap(struct file *fp, struct vm_area_struct *vma)
  600. {
  601. unsigned int i;
  602. int ret;
  603. unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
  604. struct stk_camera *dev = fp->private_data;
  605. struct stk_sio_buffer *sbuf = NULL;
  606. if (!(vma->vm_flags & VM_WRITE) || !(vma->vm_flags & VM_SHARED))
  607. return -EINVAL;
  608. for (i = 0; i < dev->n_sbufs; i++) {
  609. if (dev->sio_bufs[i].v4lbuf.m.offset == offset) {
  610. sbuf = dev->sio_bufs + i;
  611. break;
  612. }
  613. }
  614. if (sbuf == NULL)
  615. return -EINVAL;
  616. ret = remap_vmalloc_range(vma, sbuf->buffer, 0);
  617. if (ret)
  618. return ret;
  619. vma->vm_flags |= VM_DONTEXPAND;
  620. vma->vm_private_data = sbuf;
  621. vma->vm_ops = &stk_v4l_vm_ops;
  622. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_MAPPED;
  623. stk_v4l_vm_open(vma);
  624. return 0;
  625. }
  626. /* v4l ioctl handlers */
  627. static int stk_vidioc_querycap(struct file *filp,
  628. void *priv, struct v4l2_capability *cap)
  629. {
  630. strcpy(cap->driver, "stk");
  631. strcpy(cap->card, "stk");
  632. cap->version = DRIVER_VERSION_NUM;
  633. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
  634. | V4L2_CAP_READWRITE | V4L2_CAP_STREAMING;
  635. return 0;
  636. }
  637. static int stk_vidioc_enum_input(struct file *filp,
  638. void *priv, struct v4l2_input *input)
  639. {
  640. if (input->index != 0)
  641. return -EINVAL;
  642. strcpy(input->name, "Syntek USB Camera");
  643. input->type = V4L2_INPUT_TYPE_CAMERA;
  644. return 0;
  645. }
  646. static int stk_vidioc_g_input(struct file *filp, void *priv, unsigned int *i)
  647. {
  648. *i = 0;
  649. return 0;
  650. }
  651. static int stk_vidioc_s_input(struct file *filp, void *priv, unsigned int i)
  652. {
  653. if (i != 0)
  654. return -EINVAL;
  655. else
  656. return 0;
  657. }
  658. /* from vivi.c */
  659. static int stk_vidioc_s_std(struct file *filp, void *priv, v4l2_std_id *a)
  660. {
  661. return 0;
  662. }
  663. /* List of all V4Lv2 controls supported by the driver */
  664. static struct v4l2_queryctrl stk_controls[] = {
  665. {
  666. .id = V4L2_CID_BRIGHTNESS,
  667. .type = V4L2_CTRL_TYPE_INTEGER,
  668. .name = "Brightness",
  669. .minimum = 0,
  670. .maximum = 0xffff,
  671. .step = 0x0100,
  672. .default_value = 0x6000,
  673. },
  674. {
  675. .id = V4L2_CID_HFLIP,
  676. .type = V4L2_CTRL_TYPE_BOOLEAN,
  677. .name = "Horizontal Flip",
  678. .minimum = 0,
  679. .maximum = 1,
  680. .step = 1,
  681. .default_value = 1,
  682. },
  683. {
  684. .id = V4L2_CID_VFLIP,
  685. .type = V4L2_CTRL_TYPE_BOOLEAN,
  686. .name = "Vertical Flip",
  687. .minimum = 0,
  688. .maximum = 1,
  689. .step = 1,
  690. .default_value = 1,
  691. },
  692. };
  693. static int stk_vidioc_queryctrl(struct file *filp,
  694. void *priv, struct v4l2_queryctrl *c)
  695. {
  696. int i;
  697. int nbr;
  698. nbr = ARRAY_SIZE(stk_controls);
  699. for (i = 0; i < nbr; i++) {
  700. if (stk_controls[i].id == c->id) {
  701. memcpy(c, &stk_controls[i],
  702. sizeof(struct v4l2_queryctrl));
  703. return 0;
  704. }
  705. }
  706. return -EINVAL;
  707. }
  708. static int stk_vidioc_g_ctrl(struct file *filp,
  709. void *priv, struct v4l2_control *c)
  710. {
  711. struct stk_camera *dev = priv;
  712. switch (c->id) {
  713. case V4L2_CID_BRIGHTNESS:
  714. c->value = dev->vsettings.brightness;
  715. break;
  716. case V4L2_CID_HFLIP:
  717. c->value = dev->vsettings.hflip;
  718. break;
  719. case V4L2_CID_VFLIP:
  720. c->value = dev->vsettings.vflip;
  721. break;
  722. default:
  723. return -EINVAL;
  724. }
  725. return 0;
  726. }
  727. static int stk_vidioc_s_ctrl(struct file *filp,
  728. void *priv, struct v4l2_control *c)
  729. {
  730. struct stk_camera *dev = priv;
  731. switch (c->id) {
  732. case V4L2_CID_BRIGHTNESS:
  733. dev->vsettings.brightness = c->value;
  734. return stk_sensor_set_brightness(dev, c->value >> 8);
  735. case V4L2_CID_HFLIP:
  736. dev->vsettings.hflip = c->value;
  737. return 0;
  738. case V4L2_CID_VFLIP:
  739. dev->vsettings.vflip = c->value;
  740. return 0;
  741. default:
  742. return -EINVAL;
  743. }
  744. return 0;
  745. }
  746. static int stk_vidioc_enum_fmt_vid_cap(struct file *filp,
  747. void *priv, struct v4l2_fmtdesc *fmtd)
  748. {
  749. switch (fmtd->index) {
  750. case 0:
  751. fmtd->pixelformat = V4L2_PIX_FMT_RGB565;
  752. strcpy(fmtd->description, "r5g6b5");
  753. break;
  754. case 1:
  755. fmtd->pixelformat = V4L2_PIX_FMT_RGB565X;
  756. strcpy(fmtd->description, "r5g6b5BE");
  757. break;
  758. case 2:
  759. fmtd->pixelformat = V4L2_PIX_FMT_UYVY;
  760. strcpy(fmtd->description, "yuv4:2:2");
  761. break;
  762. case 3:
  763. fmtd->pixelformat = V4L2_PIX_FMT_SBGGR8;
  764. strcpy(fmtd->description, "Raw bayer");
  765. break;
  766. case 4:
  767. fmtd->pixelformat = V4L2_PIX_FMT_YUYV;
  768. strcpy(fmtd->description, "yuv4:2:2");
  769. break;
  770. default:
  771. return -EINVAL;
  772. }
  773. return 0;
  774. }
  775. static struct stk_size {
  776. unsigned w;
  777. unsigned h;
  778. enum stk_mode m;
  779. } stk_sizes[] = {
  780. { .w = 1280, .h = 1024, .m = MODE_SXGA, },
  781. { .w = 640, .h = 480, .m = MODE_VGA, },
  782. { .w = 352, .h = 288, .m = MODE_CIF, },
  783. { .w = 320, .h = 240, .m = MODE_QVGA, },
  784. { .w = 176, .h = 144, .m = MODE_QCIF, },
  785. };
  786. static int stk_vidioc_g_fmt_vid_cap(struct file *filp,
  787. void *priv, struct v4l2_format *f)
  788. {
  789. struct v4l2_pix_format *pix_format = &f->fmt.pix;
  790. struct stk_camera *dev = priv;
  791. int i;
  792. for (i = 0; i < ARRAY_SIZE(stk_sizes)
  793. && stk_sizes[i].m != dev->vsettings.mode;
  794. i++);
  795. if (i == ARRAY_SIZE(stk_sizes)) {
  796. STK_ERROR("ERROR: mode invalid\n");
  797. return -EINVAL;
  798. }
  799. pix_format->width = stk_sizes[i].w;
  800. pix_format->height = stk_sizes[i].h;
  801. pix_format->field = V4L2_FIELD_NONE;
  802. pix_format->colorspace = V4L2_COLORSPACE_SRGB;
  803. pix_format->pixelformat = dev->vsettings.palette;
  804. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  805. pix_format->bytesperline = pix_format->width;
  806. else
  807. pix_format->bytesperline = 2 * pix_format->width;
  808. pix_format->sizeimage = pix_format->bytesperline
  809. * pix_format->height;
  810. return 0;
  811. }
  812. static int stk_vidioc_try_fmt_vid_cap(struct file *filp,
  813. void *priv, struct v4l2_format *fmtd)
  814. {
  815. int i;
  816. switch (fmtd->fmt.pix.pixelformat) {
  817. case V4L2_PIX_FMT_RGB565:
  818. case V4L2_PIX_FMT_RGB565X:
  819. case V4L2_PIX_FMT_UYVY:
  820. case V4L2_PIX_FMT_YUYV:
  821. case V4L2_PIX_FMT_SBGGR8:
  822. break;
  823. default:
  824. return -EINVAL;
  825. }
  826. for (i = 1; i < ARRAY_SIZE(stk_sizes); i++) {
  827. if (fmtd->fmt.pix.width > stk_sizes[i].w)
  828. break;
  829. }
  830. if (i == ARRAY_SIZE(stk_sizes)
  831. || (abs(fmtd->fmt.pix.width - stk_sizes[i-1].w)
  832. < abs(fmtd->fmt.pix.width - stk_sizes[i].w))) {
  833. fmtd->fmt.pix.height = stk_sizes[i-1].h;
  834. fmtd->fmt.pix.width = stk_sizes[i-1].w;
  835. fmtd->fmt.pix.priv = i - 1;
  836. } else {
  837. fmtd->fmt.pix.height = stk_sizes[i].h;
  838. fmtd->fmt.pix.width = stk_sizes[i].w;
  839. fmtd->fmt.pix.priv = i;
  840. }
  841. fmtd->fmt.pix.field = V4L2_FIELD_NONE;
  842. fmtd->fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
  843. if (fmtd->fmt.pix.pixelformat == V4L2_PIX_FMT_SBGGR8)
  844. fmtd->fmt.pix.bytesperline = fmtd->fmt.pix.width;
  845. else
  846. fmtd->fmt.pix.bytesperline = 2 * fmtd->fmt.pix.width;
  847. fmtd->fmt.pix.sizeimage = fmtd->fmt.pix.bytesperline
  848. * fmtd->fmt.pix.height;
  849. return 0;
  850. }
  851. static int stk_setup_format(struct stk_camera *dev)
  852. {
  853. int i = 0;
  854. int depth;
  855. if (dev->vsettings.palette == V4L2_PIX_FMT_SBGGR8)
  856. depth = 1;
  857. else
  858. depth = 2;
  859. while (i < ARRAY_SIZE(stk_sizes) &&
  860. stk_sizes[i].m != dev->vsettings.mode)
  861. i++;
  862. if (i == ARRAY_SIZE(stk_sizes)) {
  863. STK_ERROR("Something is broken in %s\n", __func__);
  864. return -EFAULT;
  865. }
  866. /* This registers controls some timings, not sure of what. */
  867. stk_camera_write_reg(dev, 0x001b, 0x0e);
  868. if (dev->vsettings.mode == MODE_SXGA)
  869. stk_camera_write_reg(dev, 0x001c, 0x0e);
  870. else
  871. stk_camera_write_reg(dev, 0x001c, 0x46);
  872. /*
  873. * Registers 0x0115 0x0114 are the size of each line (bytes),
  874. * regs 0x0117 0x0116 are the heigth of the image.
  875. */
  876. stk_camera_write_reg(dev, 0x0115,
  877. ((stk_sizes[i].w * depth) >> 8) & 0xff);
  878. stk_camera_write_reg(dev, 0x0114,
  879. (stk_sizes[i].w * depth) & 0xff);
  880. stk_camera_write_reg(dev, 0x0117,
  881. (stk_sizes[i].h >> 8) & 0xff);
  882. stk_camera_write_reg(dev, 0x0116,
  883. stk_sizes[i].h & 0xff);
  884. return stk_sensor_configure(dev);
  885. }
  886. static int stk_vidioc_s_fmt_vid_cap(struct file *filp,
  887. void *priv, struct v4l2_format *fmtd)
  888. {
  889. int ret;
  890. struct stk_camera *dev = priv;
  891. if (dev == NULL)
  892. return -ENODEV;
  893. if (!is_present(dev))
  894. return -ENODEV;
  895. if (is_streaming(dev))
  896. return -EBUSY;
  897. if (dev->owner && dev->owner != filp)
  898. return -EBUSY;
  899. ret = stk_vidioc_try_fmt_vid_cap(filp, priv, fmtd);
  900. if (ret)
  901. return ret;
  902. dev->owner = filp;
  903. dev->vsettings.palette = fmtd->fmt.pix.pixelformat;
  904. stk_free_buffers(dev);
  905. dev->frame_size = fmtd->fmt.pix.sizeimage;
  906. dev->vsettings.mode = stk_sizes[fmtd->fmt.pix.priv].m;
  907. stk_initialise(dev);
  908. return stk_setup_format(dev);
  909. }
  910. static int stk_vidioc_reqbufs(struct file *filp,
  911. void *priv, struct v4l2_requestbuffers *rb)
  912. {
  913. struct stk_camera *dev = priv;
  914. if (dev == NULL)
  915. return -ENODEV;
  916. if (rb->memory != V4L2_MEMORY_MMAP)
  917. return -EINVAL;
  918. if (is_streaming(dev)
  919. || (dev->owner && dev->owner != filp))
  920. return -EBUSY;
  921. dev->owner = filp;
  922. /*FIXME If they ask for zero, we must stop streaming and free */
  923. if (rb->count < 3)
  924. rb->count = 3;
  925. /* Arbitrary limit */
  926. else if (rb->count > 5)
  927. rb->count = 5;
  928. stk_allocate_buffers(dev, rb->count);
  929. rb->count = dev->n_sbufs;
  930. return 0;
  931. }
  932. static int stk_vidioc_querybuf(struct file *filp,
  933. void *priv, struct v4l2_buffer *buf)
  934. {
  935. struct stk_camera *dev = priv;
  936. struct stk_sio_buffer *sbuf;
  937. if (buf->index >= dev->n_sbufs)
  938. return -EINVAL;
  939. sbuf = dev->sio_bufs + buf->index;
  940. *buf = sbuf->v4lbuf;
  941. return 0;
  942. }
  943. static int stk_vidioc_qbuf(struct file *filp,
  944. void *priv, struct v4l2_buffer *buf)
  945. {
  946. struct stk_camera *dev = priv;
  947. struct stk_sio_buffer *sbuf;
  948. unsigned long flags;
  949. if (buf->memory != V4L2_MEMORY_MMAP)
  950. return -EINVAL;
  951. if (buf->index >= dev->n_sbufs)
  952. return -EINVAL;
  953. sbuf = dev->sio_bufs + buf->index;
  954. if (sbuf->v4lbuf.flags & V4L2_BUF_FLAG_QUEUED)
  955. return 0;
  956. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_QUEUED;
  957. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_DONE;
  958. spin_lock_irqsave(&dev->spinlock, flags);
  959. list_add_tail(&sbuf->list, &dev->sio_avail);
  960. *buf = sbuf->v4lbuf;
  961. spin_unlock_irqrestore(&dev->spinlock, flags);
  962. return 0;
  963. }
  964. static int stk_vidioc_dqbuf(struct file *filp,
  965. void *priv, struct v4l2_buffer *buf)
  966. {
  967. struct stk_camera *dev = priv;
  968. struct stk_sio_buffer *sbuf;
  969. unsigned long flags;
  970. int ret;
  971. if (!is_streaming(dev))
  972. return -EINVAL;
  973. if (filp->f_flags & O_NONBLOCK && list_empty(&dev->sio_full))
  974. return -EWOULDBLOCK;
  975. ret = wait_event_interruptible(dev->wait_frame,
  976. !list_empty(&dev->sio_full) || !is_present(dev));
  977. if (ret)
  978. return ret;
  979. if (!is_present(dev))
  980. return -EIO;
  981. spin_lock_irqsave(&dev->spinlock, flags);
  982. sbuf = list_first_entry(&dev->sio_full, struct stk_sio_buffer, list);
  983. list_del_init(&sbuf->list);
  984. spin_unlock_irqrestore(&dev->spinlock, flags);
  985. sbuf->v4lbuf.flags &= ~V4L2_BUF_FLAG_QUEUED;
  986. sbuf->v4lbuf.flags |= V4L2_BUF_FLAG_DONE;
  987. sbuf->v4lbuf.sequence = ++dev->sequence;
  988. do_gettimeofday(&sbuf->v4lbuf.timestamp);
  989. *buf = sbuf->v4lbuf;
  990. return 0;
  991. }
  992. static int stk_vidioc_streamon(struct file *filp,
  993. void *priv, enum v4l2_buf_type type)
  994. {
  995. struct stk_camera *dev = priv;
  996. if (is_streaming(dev))
  997. return 0;
  998. if (dev->sio_bufs == NULL)
  999. return -EINVAL;
  1000. dev->sequence = 0;
  1001. return stk_start_stream(dev);
  1002. }
  1003. static int stk_vidioc_streamoff(struct file *filp,
  1004. void *priv, enum v4l2_buf_type type)
  1005. {
  1006. struct stk_camera *dev = priv;
  1007. unsigned long flags;
  1008. int i;
  1009. stk_stop_stream(dev);
  1010. spin_lock_irqsave(&dev->spinlock, flags);
  1011. INIT_LIST_HEAD(&dev->sio_avail);
  1012. INIT_LIST_HEAD(&dev->sio_full);
  1013. for (i = 0; i < dev->n_sbufs; i++) {
  1014. INIT_LIST_HEAD(&dev->sio_bufs[i].list);
  1015. dev->sio_bufs[i].v4lbuf.flags = 0;
  1016. }
  1017. spin_unlock_irqrestore(&dev->spinlock, flags);
  1018. return 0;
  1019. }
  1020. static int stk_vidioc_g_parm(struct file *filp,
  1021. void *priv, struct v4l2_streamparm *sp)
  1022. {
  1023. /*FIXME This is not correct */
  1024. sp->parm.capture.timeperframe.numerator = 1;
  1025. sp->parm.capture.timeperframe.denominator = 30;
  1026. sp->parm.capture.readbuffers = 2;
  1027. return 0;
  1028. }
  1029. static int stk_vidioc_enum_framesizes(struct file *filp,
  1030. void *priv, struct v4l2_frmsizeenum *frms)
  1031. {
  1032. if (frms->index >= ARRAY_SIZE(stk_sizes))
  1033. return -EINVAL;
  1034. switch (frms->pixel_format) {
  1035. case V4L2_PIX_FMT_RGB565:
  1036. case V4L2_PIX_FMT_RGB565X:
  1037. case V4L2_PIX_FMT_UYVY:
  1038. case V4L2_PIX_FMT_YUYV:
  1039. case V4L2_PIX_FMT_SBGGR8:
  1040. frms->type = V4L2_FRMSIZE_TYPE_DISCRETE;
  1041. frms->discrete.width = stk_sizes[frms->index].w;
  1042. frms->discrete.height = stk_sizes[frms->index].h;
  1043. return 0;
  1044. default: return -EINVAL;
  1045. }
  1046. }
  1047. static struct v4l2_file_operations v4l_stk_fops = {
  1048. .owner = THIS_MODULE,
  1049. .open = v4l_stk_open,
  1050. .release = v4l_stk_release,
  1051. .read = v4l_stk_read,
  1052. .poll = v4l_stk_poll,
  1053. .mmap = v4l_stk_mmap,
  1054. .ioctl = video_ioctl2,
  1055. };
  1056. static const struct v4l2_ioctl_ops v4l_stk_ioctl_ops = {
  1057. .vidioc_querycap = stk_vidioc_querycap,
  1058. .vidioc_enum_fmt_vid_cap = stk_vidioc_enum_fmt_vid_cap,
  1059. .vidioc_try_fmt_vid_cap = stk_vidioc_try_fmt_vid_cap,
  1060. .vidioc_s_fmt_vid_cap = stk_vidioc_s_fmt_vid_cap,
  1061. .vidioc_g_fmt_vid_cap = stk_vidioc_g_fmt_vid_cap,
  1062. .vidioc_enum_input = stk_vidioc_enum_input,
  1063. .vidioc_s_input = stk_vidioc_s_input,
  1064. .vidioc_g_input = stk_vidioc_g_input,
  1065. .vidioc_s_std = stk_vidioc_s_std,
  1066. .vidioc_reqbufs = stk_vidioc_reqbufs,
  1067. .vidioc_querybuf = stk_vidioc_querybuf,
  1068. .vidioc_qbuf = stk_vidioc_qbuf,
  1069. .vidioc_dqbuf = stk_vidioc_dqbuf,
  1070. .vidioc_streamon = stk_vidioc_streamon,
  1071. .vidioc_streamoff = stk_vidioc_streamoff,
  1072. .vidioc_queryctrl = stk_vidioc_queryctrl,
  1073. .vidioc_g_ctrl = stk_vidioc_g_ctrl,
  1074. .vidioc_s_ctrl = stk_vidioc_s_ctrl,
  1075. .vidioc_g_parm = stk_vidioc_g_parm,
  1076. .vidioc_enum_framesizes = stk_vidioc_enum_framesizes,
  1077. };
  1078. static void stk_v4l_dev_release(struct video_device *vd)
  1079. {
  1080. struct stk_camera *dev = vdev_to_camera(vd);
  1081. if (dev->sio_bufs != NULL || dev->isobufs != NULL)
  1082. STK_ERROR("We are leaking memory\n");
  1083. usb_put_intf(dev->interface);
  1084. kfree(dev);
  1085. }
  1086. static struct video_device stk_v4l_data = {
  1087. .name = "stkwebcam",
  1088. .tvnorms = V4L2_STD_UNKNOWN,
  1089. .current_norm = V4L2_STD_UNKNOWN,
  1090. .fops = &v4l_stk_fops,
  1091. .ioctl_ops = &v4l_stk_ioctl_ops,
  1092. .release = stk_v4l_dev_release,
  1093. };
  1094. static int stk_register_video_device(struct stk_camera *dev)
  1095. {
  1096. int err;
  1097. dev->vdev = stk_v4l_data;
  1098. dev->vdev.debug = debug;
  1099. dev->vdev.parent = &dev->interface->dev;
  1100. err = video_register_device(&dev->vdev, VFL_TYPE_GRABBER, -1);
  1101. if (err)
  1102. STK_ERROR("v4l registration failed\n");
  1103. else
  1104. STK_INFO("Syntek USB2.0 Camera is now controlling device %s\n",
  1105. video_device_node_name(&dev->vdev));
  1106. return err;
  1107. }
  1108. /* USB Stuff */
  1109. static int stk_camera_probe(struct usb_interface *interface,
  1110. const struct usb_device_id *id)
  1111. {
  1112. int i;
  1113. int err = 0;
  1114. struct stk_camera *dev = NULL;
  1115. struct usb_device *udev = interface_to_usbdev(interface);
  1116. struct usb_host_interface *iface_desc;
  1117. struct usb_endpoint_descriptor *endpoint;
  1118. dev = kzalloc(sizeof(struct stk_camera), GFP_KERNEL);
  1119. if (dev == NULL) {
  1120. STK_ERROR("Out of memory !\n");
  1121. return -ENOMEM;
  1122. }
  1123. spin_lock_init(&dev->spinlock);
  1124. init_waitqueue_head(&dev->wait_frame);
  1125. dev->udev = udev;
  1126. dev->interface = interface;
  1127. usb_get_intf(interface);
  1128. dev->vsettings.vflip = vflip;
  1129. dev->vsettings.hflip = hflip;
  1130. dev->n_sbufs = 0;
  1131. set_present(dev);
  1132. /* Set up the endpoint information
  1133. * use only the first isoc-in endpoint
  1134. * for the current alternate setting */
  1135. iface_desc = interface->cur_altsetting;
  1136. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1137. endpoint = &iface_desc->endpoint[i].desc;
  1138. if (!dev->isoc_ep
  1139. && usb_endpoint_is_isoc_in(endpoint)) {
  1140. /* we found an isoc in endpoint */
  1141. dev->isoc_ep = usb_endpoint_num(endpoint);
  1142. break;
  1143. }
  1144. }
  1145. if (!dev->isoc_ep) {
  1146. STK_ERROR("Could not find isoc-in endpoint");
  1147. err = -ENODEV;
  1148. goto error;
  1149. }
  1150. dev->vsettings.brightness = 0x7fff;
  1151. dev->vsettings.palette = V4L2_PIX_FMT_RGB565;
  1152. dev->vsettings.mode = MODE_VGA;
  1153. dev->frame_size = 640 * 480 * 2;
  1154. INIT_LIST_HEAD(&dev->sio_avail);
  1155. INIT_LIST_HEAD(&dev->sio_full);
  1156. usb_set_intfdata(interface, dev);
  1157. err = stk_register_video_device(dev);
  1158. if (err) {
  1159. goto error;
  1160. }
  1161. return 0;
  1162. error:
  1163. kfree(dev);
  1164. return err;
  1165. }
  1166. static void stk_camera_disconnect(struct usb_interface *interface)
  1167. {
  1168. struct stk_camera *dev = usb_get_intfdata(interface);
  1169. usb_set_intfdata(interface, NULL);
  1170. unset_present(dev);
  1171. wake_up_interruptible(&dev->wait_frame);
  1172. STK_INFO("Syntek USB2.0 Camera release resources device %s\n",
  1173. video_device_node_name(&dev->vdev));
  1174. video_unregister_device(&dev->vdev);
  1175. }
  1176. #ifdef CONFIG_PM
  1177. static int stk_camera_suspend(struct usb_interface *intf, pm_message_t message)
  1178. {
  1179. struct stk_camera *dev = usb_get_intfdata(intf);
  1180. if (is_streaming(dev)) {
  1181. stk_stop_stream(dev);
  1182. /* yes, this is ugly */
  1183. set_streaming(dev);
  1184. }
  1185. return 0;
  1186. }
  1187. static int stk_camera_resume(struct usb_interface *intf)
  1188. {
  1189. struct stk_camera *dev = usb_get_intfdata(intf);
  1190. if (!is_initialised(dev))
  1191. return 0;
  1192. unset_initialised(dev);
  1193. stk_initialise(dev);
  1194. stk_setup_format(dev);
  1195. if (is_streaming(dev))
  1196. stk_start_stream(dev);
  1197. return 0;
  1198. }
  1199. #endif
  1200. static struct usb_driver stk_camera_driver = {
  1201. .name = "stkwebcam",
  1202. .probe = stk_camera_probe,
  1203. .disconnect = stk_camera_disconnect,
  1204. .id_table = stkwebcam_table,
  1205. #ifdef CONFIG_PM
  1206. .suspend = stk_camera_suspend,
  1207. .resume = stk_camera_resume,
  1208. #endif
  1209. };
  1210. static int __init stk_camera_init(void)
  1211. {
  1212. int result;
  1213. result = usb_register(&stk_camera_driver);
  1214. if (result)
  1215. STK_ERROR("usb_register failed ! Error number %d\n", result);
  1216. return result;
  1217. }
  1218. static void __exit stk_camera_exit(void)
  1219. {
  1220. usb_deregister(&stk_camera_driver);
  1221. }
  1222. module_init(stk_camera_init);
  1223. module_exit(stk_camera_exit);