/drivers/media/video/zr364xx.c

https://gitlab.com/TeamCarbonXtreme/android_kernel_samsung_msm7x27 · C · 1723 lines · 1340 code · 250 blank · 133 comment · 189 complexity · 239f4611c7c906060549a62ad5e8fbb6 MD5 · raw file

  1. /*
  2. * Zoran 364xx based USB webcam module version 0.73
  3. *
  4. * Allows you to use your USB webcam with V4L2 applications
  5. * This is still in heavy developpement !
  6. *
  7. * Copyright (C) 2004 Antoine Jacquet <royale@zerezo.com>
  8. * http://royale.zerezo.com/zr364xx/
  9. *
  10. * Heavily inspired by usb-skeleton.c, vicam.c, cpia.c and spca50x.c drivers
  11. * V4L2 version inspired by meye.c driver
  12. *
  13. * Some video buffer code by Lamarque based on s2255drv.c and vivi.c drivers.
  14. *
  15. * This program is free software; you can redistribute it and/or modify
  16. * it under the terms of the GNU General Public License as published by
  17. * the Free Software Foundation; either version 2 of the License, or
  18. * (at your option) any later version.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU General Public License
  26. * along with this program; if not, write to the Free Software
  27. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  28. */
  29. #include <linux/module.h>
  30. #include <linux/version.h>
  31. #include <linux/init.h>
  32. #include <linux/usb.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/slab.h>
  35. #include <linux/proc_fs.h>
  36. #include <linux/highmem.h>
  37. #include <media/v4l2-common.h>
  38. #include <media/v4l2-ioctl.h>
  39. #include <media/videobuf-vmalloc.h>
  40. /* Version Information */
  41. #define DRIVER_VERSION "v0.73"
  42. #define ZR364XX_VERSION_CODE KERNEL_VERSION(0, 7, 3)
  43. #define DRIVER_AUTHOR "Antoine Jacquet, http://royale.zerezo.com/"
  44. #define DRIVER_DESC "Zoran 364xx"
  45. /* Camera */
  46. #define FRAMES 1
  47. #define MAX_FRAME_SIZE 200000
  48. #define BUFFER_SIZE 0x1000
  49. #define CTRL_TIMEOUT 500
  50. #define ZR364XX_DEF_BUFS 4
  51. #define ZR364XX_READ_IDLE 0
  52. #define ZR364XX_READ_FRAME 1
  53. /* Debug macro */
  54. #define DBG(fmt, args...) \
  55. do { \
  56. if (debug) { \
  57. printk(KERN_INFO KBUILD_MODNAME " " fmt, ##args); \
  58. } \
  59. } while (0)
  60. /*#define FULL_DEBUG 1*/
  61. #ifdef FULL_DEBUG
  62. #define _DBG DBG
  63. #else
  64. #define _DBG(fmt, args...)
  65. #endif
  66. /* Init methods, need to find nicer names for these
  67. * the exact names of the chipsets would be the best if someone finds it */
  68. #define METHOD0 0
  69. #define METHOD1 1
  70. #define METHOD2 2
  71. #define METHOD3 3
  72. /* Module parameters */
  73. static int debug;
  74. static int mode;
  75. /* Module parameters interface */
  76. module_param(debug, int, 0644);
  77. MODULE_PARM_DESC(debug, "Debug level");
  78. module_param(mode, int, 0644);
  79. MODULE_PARM_DESC(mode, "0 = 320x240, 1 = 160x120, 2 = 640x480");
  80. /* Devices supported by this driver
  81. * .driver_info contains the init method used by the camera */
  82. static struct usb_device_id device_table[] = {
  83. {USB_DEVICE(0x08ca, 0x0109), .driver_info = METHOD0 },
  84. {USB_DEVICE(0x041e, 0x4024), .driver_info = METHOD0 },
  85. {USB_DEVICE(0x0d64, 0x0108), .driver_info = METHOD0 },
  86. {USB_DEVICE(0x0546, 0x3187), .driver_info = METHOD0 },
  87. {USB_DEVICE(0x0d64, 0x3108), .driver_info = METHOD0 },
  88. {USB_DEVICE(0x0595, 0x4343), .driver_info = METHOD0 },
  89. {USB_DEVICE(0x0bb0, 0x500d), .driver_info = METHOD0 },
  90. {USB_DEVICE(0x0feb, 0x2004), .driver_info = METHOD0 },
  91. {USB_DEVICE(0x055f, 0xb500), .driver_info = METHOD0 },
  92. {USB_DEVICE(0x08ca, 0x2062), .driver_info = METHOD2 },
  93. {USB_DEVICE(0x052b, 0x1a18), .driver_info = METHOD1 },
  94. {USB_DEVICE(0x04c8, 0x0729), .driver_info = METHOD0 },
  95. {USB_DEVICE(0x04f2, 0xa208), .driver_info = METHOD0 },
  96. {USB_DEVICE(0x0784, 0x0040), .driver_info = METHOD1 },
  97. {USB_DEVICE(0x06d6, 0x0034), .driver_info = METHOD0 },
  98. {USB_DEVICE(0x0a17, 0x0062), .driver_info = METHOD2 },
  99. {USB_DEVICE(0x06d6, 0x003b), .driver_info = METHOD0 },
  100. {USB_DEVICE(0x0a17, 0x004e), .driver_info = METHOD2 },
  101. {USB_DEVICE(0x041e, 0x405d), .driver_info = METHOD2 },
  102. {USB_DEVICE(0x08ca, 0x2102), .driver_info = METHOD3 },
  103. {USB_DEVICE(0x06d6, 0x003d), .driver_info = METHOD0 },
  104. {} /* Terminating entry */
  105. };
  106. MODULE_DEVICE_TABLE(usb, device_table);
  107. struct zr364xx_mode {
  108. u32 color; /* output video color format */
  109. u32 brightness; /* brightness */
  110. };
  111. /* frame structure */
  112. struct zr364xx_framei {
  113. unsigned long ulState; /* ulState:ZR364XX_READ_IDLE,
  114. ZR364XX_READ_FRAME */
  115. void *lpvbits; /* image data */
  116. unsigned long cur_size; /* current data copied to it */
  117. };
  118. /* image buffer structure */
  119. struct zr364xx_bufferi {
  120. unsigned long dwFrames; /* number of frames in buffer */
  121. struct zr364xx_framei frame[FRAMES]; /* array of FRAME structures */
  122. };
  123. struct zr364xx_dmaqueue {
  124. struct list_head active;
  125. struct zr364xx_camera *cam;
  126. };
  127. struct zr364xx_pipeinfo {
  128. u32 transfer_size;
  129. u8 *transfer_buffer;
  130. u32 state;
  131. void *stream_urb;
  132. void *cam; /* back pointer to zr364xx_camera struct */
  133. u32 err_count;
  134. u32 idx;
  135. };
  136. struct zr364xx_fmt {
  137. char *name;
  138. u32 fourcc;
  139. int depth;
  140. };
  141. /* image formats. */
  142. static const struct zr364xx_fmt formats[] = {
  143. {
  144. .name = "JPG",
  145. .fourcc = V4L2_PIX_FMT_JPEG,
  146. .depth = 24
  147. }
  148. };
  149. /* Camera stuff */
  150. struct zr364xx_camera {
  151. struct usb_device *udev; /* save off the usb device pointer */
  152. struct usb_interface *interface;/* the interface for this device */
  153. struct video_device *vdev; /* v4l video device */
  154. int nb;
  155. struct zr364xx_bufferi buffer;
  156. int skip;
  157. int width;
  158. int height;
  159. int method;
  160. struct mutex lock;
  161. struct mutex open_lock;
  162. int users;
  163. spinlock_t slock;
  164. struct zr364xx_dmaqueue vidq;
  165. int resources;
  166. int last_frame;
  167. int cur_frame;
  168. unsigned long frame_count;
  169. int b_acquire;
  170. struct zr364xx_pipeinfo pipe[1];
  171. u8 read_endpoint;
  172. const struct zr364xx_fmt *fmt;
  173. struct videobuf_queue vb_vidq;
  174. enum v4l2_buf_type type;
  175. struct zr364xx_mode mode;
  176. };
  177. /* buffer for one video frame */
  178. struct zr364xx_buffer {
  179. /* common v4l buffer stuff -- must be first */
  180. struct videobuf_buffer vb;
  181. const struct zr364xx_fmt *fmt;
  182. };
  183. /* function used to send initialisation commands to the camera */
  184. static int send_control_msg(struct usb_device *udev, u8 request, u16 value,
  185. u16 index, unsigned char *cp, u16 size)
  186. {
  187. int status;
  188. unsigned char *transfer_buffer = kmalloc(size, GFP_KERNEL);
  189. if (!transfer_buffer) {
  190. dev_err(&udev->dev, "kmalloc(%d) failed\n", size);
  191. return -ENOMEM;
  192. }
  193. memcpy(transfer_buffer, cp, size);
  194. status = usb_control_msg(udev,
  195. usb_sndctrlpipe(udev, 0),
  196. request,
  197. USB_DIR_OUT | USB_TYPE_VENDOR |
  198. USB_RECIP_DEVICE, value, index,
  199. transfer_buffer, size, CTRL_TIMEOUT);
  200. kfree(transfer_buffer);
  201. if (status < 0)
  202. dev_err(&udev->dev,
  203. "Failed sending control message, error %d.\n", status);
  204. return status;
  205. }
  206. /* Control messages sent to the camera to initialize it
  207. * and launch the capture */
  208. typedef struct {
  209. unsigned int value;
  210. unsigned int size;
  211. unsigned char *bytes;
  212. } message;
  213. /* method 0 */
  214. static unsigned char m0d1[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  215. static unsigned char m0d2[] = { 0, 0, 0, 0, 0, 0 };
  216. static unsigned char m0d3[] = { 0, 0 };
  217. static message m0[] = {
  218. {0x1f30, 0, NULL},
  219. {0xd000, 0, NULL},
  220. {0x3370, sizeof(m0d1), m0d1},
  221. {0x2000, 0, NULL},
  222. {0x2f0f, 0, NULL},
  223. {0x2610, sizeof(m0d2), m0d2},
  224. {0xe107, 0, NULL},
  225. {0x2502, 0, NULL},
  226. {0x1f70, 0, NULL},
  227. {0xd000, 0, NULL},
  228. {0x9a01, sizeof(m0d3), m0d3},
  229. {-1, -1, NULL}
  230. };
  231. /* method 1 */
  232. static unsigned char m1d1[] = { 0xff, 0xff };
  233. static unsigned char m1d2[] = { 0x00, 0x00 };
  234. static message m1[] = {
  235. {0x1f30, 0, NULL},
  236. {0xd000, 0, NULL},
  237. {0xf000, 0, NULL},
  238. {0x2000, 0, NULL},
  239. {0x2f0f, 0, NULL},
  240. {0x2650, 0, NULL},
  241. {0xe107, 0, NULL},
  242. {0x2502, sizeof(m1d1), m1d1},
  243. {0x1f70, 0, NULL},
  244. {0xd000, 0, NULL},
  245. {0xd000, 0, NULL},
  246. {0xd000, 0, NULL},
  247. {0x9a01, sizeof(m1d2), m1d2},
  248. {-1, -1, NULL}
  249. };
  250. /* method 2 */
  251. static unsigned char m2d1[] = { 0xff, 0xff };
  252. static message m2[] = {
  253. {0x1f30, 0, NULL},
  254. {0xf000, 0, NULL},
  255. {0x2000, 0, NULL},
  256. {0x2f0f, 0, NULL},
  257. {0x2650, 0, NULL},
  258. {0xe107, 0, NULL},
  259. {0x2502, sizeof(m2d1), m2d1},
  260. {0x1f70, 0, NULL},
  261. {-1, -1, NULL}
  262. };
  263. /* init table */
  264. static message *init[4] = { m0, m1, m2, m2 };
  265. /* JPEG static data in header (Huffman table, etc) */
  266. static unsigned char header1[] = {
  267. 0xFF, 0xD8,
  268. /*
  269. 0xFF, 0xE0, 0x00, 0x10, 'J', 'F', 'I', 'F',
  270. 0x00, 0x01, 0x01, 0x00, 0x33, 0x8A, 0x00, 0x00, 0x33, 0x88,
  271. */
  272. 0xFF, 0xDB, 0x00, 0x84
  273. };
  274. static unsigned char header2[] = {
  275. 0xFF, 0xC4, 0x00, 0x1F, 0x00, 0x00, 0x01, 0x05, 0x01, 0x01, 0x01,
  276. 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  277. 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B,
  278. 0xFF, 0xC4, 0x00, 0xB5, 0x10, 0x00, 0x02, 0x01, 0x03, 0x03, 0x02,
  279. 0x04, 0x03, 0x05, 0x05, 0x04, 0x04, 0x00, 0x00, 0x01, 0x7D, 0x01,
  280. 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06,
  281. 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xA1,
  282. 0x08, 0x23, 0x42, 0xB1, 0xC1, 0x15, 0x52, 0xD1, 0xF0, 0x24, 0x33,
  283. 0x62, 0x72, 0x82, 0x09, 0x0A, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x25,
  284. 0x26, 0x27, 0x28, 0x29, 0x2A, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  285. 0x3A, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54,
  286. 0x55, 0x56, 0x57, 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67,
  287. 0x68, 0x69, 0x6A, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A,
  288. 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94,
  289. 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6,
  290. 0xA7, 0xA8, 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8,
  291. 0xB9, 0xBA, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA,
  292. 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE1, 0xE2,
  293. 0xE3, 0xE4, 0xE5, 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF1, 0xF2, 0xF3,
  294. 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFF, 0xC4, 0x00, 0x1F,
  295. 0x01, 0x00, 0x03, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  296. 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03, 0x04,
  297. 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0xFF, 0xC4, 0x00, 0xB5,
  298. 0x11, 0x00, 0x02, 0x01, 0x02, 0x04, 0x04, 0x03, 0x04, 0x07, 0x05,
  299. 0x04, 0x04, 0x00, 0x01, 0x02, 0x77, 0x00, 0x01, 0x02, 0x03, 0x11,
  300. 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  301. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xA1, 0xB1, 0xC1,
  302. 0x09, 0x23, 0x33, 0x52, 0xF0, 0x15, 0x62, 0x72, 0xD1, 0x0A, 0x16,
  303. 0x24, 0x34, 0xE1, 0x25, 0xF1, 0x17, 0x18, 0x19, 0x1A, 0x26, 0x27,
  304. 0x28, 0x29, 0x2A, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x43, 0x44,
  305. 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x53, 0x54, 0x55, 0x56, 0x57,
  306. 0x58, 0x59, 0x5A, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6A,
  307. 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x82, 0x83, 0x84,
  308. 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x92, 0x93, 0x94, 0x95, 0x96,
  309. 0x97, 0x98, 0x99, 0x9A, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7, 0xA8,
  310. 0xA9, 0xAA, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA,
  311. 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xD2, 0xD3,
  312. 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xE2, 0xE3, 0xE4, 0xE5,
  313. 0xE6, 0xE7, 0xE8, 0xE9, 0xEA, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
  314. 0xF8, 0xF9, 0xFA, 0xFF, 0xC0, 0x00, 0x11, 0x08, 0x00, 0xF0, 0x01,
  315. 0x40, 0x03, 0x01, 0x21, 0x00, 0x02, 0x11, 0x01, 0x03, 0x11, 0x01,
  316. 0xFF, 0xDA, 0x00, 0x0C, 0x03, 0x01, 0x00, 0x02, 0x11, 0x03, 0x11,
  317. 0x00, 0x3F, 0x00
  318. };
  319. static unsigned char header3;
  320. /* ------------------------------------------------------------------
  321. Videobuf operations
  322. ------------------------------------------------------------------*/
  323. static int buffer_setup(struct videobuf_queue *vq, unsigned int *count,
  324. unsigned int *size)
  325. {
  326. struct zr364xx_camera *cam = vq->priv_data;
  327. *size = cam->width * cam->height * (cam->fmt->depth >> 3);
  328. if (*count == 0)
  329. *count = ZR364XX_DEF_BUFS;
  330. if (*size * *count > ZR364XX_DEF_BUFS * 1024 * 1024)
  331. *count = (ZR364XX_DEF_BUFS * 1024 * 1024) / *size;
  332. return 0;
  333. }
  334. static void free_buffer(struct videobuf_queue *vq, struct zr364xx_buffer *buf)
  335. {
  336. _DBG("%s\n", __func__);
  337. if (in_interrupt())
  338. BUG();
  339. videobuf_vmalloc_free(&buf->vb);
  340. buf->vb.state = VIDEOBUF_NEEDS_INIT;
  341. }
  342. static int buffer_prepare(struct videobuf_queue *vq, struct videobuf_buffer *vb,
  343. enum v4l2_field field)
  344. {
  345. struct zr364xx_camera *cam = vq->priv_data;
  346. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  347. vb);
  348. int rc;
  349. DBG("%s, field=%d, fmt name = %s\n", __func__, field, cam->fmt != NULL ?
  350. cam->fmt->name : "");
  351. if (cam->fmt == NULL)
  352. return -EINVAL;
  353. buf->vb.size = cam->width * cam->height * (cam->fmt->depth >> 3);
  354. if (buf->vb.baddr != 0 && buf->vb.bsize < buf->vb.size) {
  355. DBG("invalid buffer prepare\n");
  356. return -EINVAL;
  357. }
  358. buf->fmt = cam->fmt;
  359. buf->vb.width = cam->width;
  360. buf->vb.height = cam->height;
  361. buf->vb.field = field;
  362. if (buf->vb.state == VIDEOBUF_NEEDS_INIT) {
  363. rc = videobuf_iolock(vq, &buf->vb, NULL);
  364. if (rc < 0)
  365. goto fail;
  366. }
  367. buf->vb.state = VIDEOBUF_PREPARED;
  368. return 0;
  369. fail:
  370. free_buffer(vq, buf);
  371. return rc;
  372. }
  373. static void buffer_queue(struct videobuf_queue *vq, struct videobuf_buffer *vb)
  374. {
  375. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  376. vb);
  377. struct zr364xx_camera *cam = vq->priv_data;
  378. _DBG("%s\n", __func__);
  379. buf->vb.state = VIDEOBUF_QUEUED;
  380. list_add_tail(&buf->vb.queue, &cam->vidq.active);
  381. }
  382. static void buffer_release(struct videobuf_queue *vq,
  383. struct videobuf_buffer *vb)
  384. {
  385. struct zr364xx_buffer *buf = container_of(vb, struct zr364xx_buffer,
  386. vb);
  387. _DBG("%s\n", __func__);
  388. free_buffer(vq, buf);
  389. }
  390. static struct videobuf_queue_ops zr364xx_video_qops = {
  391. .buf_setup = buffer_setup,
  392. .buf_prepare = buffer_prepare,
  393. .buf_queue = buffer_queue,
  394. .buf_release = buffer_release,
  395. };
  396. /********************/
  397. /* V4L2 integration */
  398. /********************/
  399. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  400. enum v4l2_buf_type type);
  401. static ssize_t zr364xx_read(struct file *file, char __user *buf, size_t count,
  402. loff_t * ppos)
  403. {
  404. struct zr364xx_camera *cam = video_drvdata(file);
  405. _DBG("%s\n", __func__);
  406. if (!buf)
  407. return -EINVAL;
  408. if (!count)
  409. return -EINVAL;
  410. if (cam->type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
  411. zr364xx_vidioc_streamon(file, cam, cam->type) == 0) {
  412. DBG("%s: reading %d bytes at pos %d.\n", __func__, (int) count,
  413. (int) *ppos);
  414. /* NoMan Sux ! */
  415. return videobuf_read_one(&cam->vb_vidq, buf, count, ppos,
  416. file->f_flags & O_NONBLOCK);
  417. }
  418. return 0;
  419. }
  420. /* video buffer vmalloc implementation based partly on VIVI driver which is
  421. * Copyright (c) 2006 by
  422. * Mauro Carvalho Chehab <mchehab--a.t--infradead.org>
  423. * Ted Walther <ted--a.t--enumera.com>
  424. * John Sokol <sokol--a.t--videotechnology.com>
  425. * http://v4l.videotechnology.com/
  426. *
  427. */
  428. static void zr364xx_fillbuff(struct zr364xx_camera *cam,
  429. struct zr364xx_buffer *buf,
  430. int jpgsize)
  431. {
  432. int pos = 0;
  433. struct timeval ts;
  434. const char *tmpbuf;
  435. char *vbuf = videobuf_to_vmalloc(&buf->vb);
  436. unsigned long last_frame;
  437. struct zr364xx_framei *frm;
  438. if (!vbuf)
  439. return;
  440. last_frame = cam->last_frame;
  441. if (last_frame != -1) {
  442. frm = &cam->buffer.frame[last_frame];
  443. tmpbuf = (const char *)cam->buffer.frame[last_frame].lpvbits;
  444. switch (buf->fmt->fourcc) {
  445. case V4L2_PIX_FMT_JPEG:
  446. buf->vb.size = jpgsize;
  447. memcpy(vbuf, tmpbuf, buf->vb.size);
  448. break;
  449. default:
  450. printk(KERN_DEBUG KBUILD_MODNAME ": unknown format?\n");
  451. }
  452. cam->last_frame = -1;
  453. } else {
  454. printk(KERN_ERR KBUILD_MODNAME ": =======no frame\n");
  455. return;
  456. }
  457. DBG("%s: Buffer 0x%08lx size= %d\n", __func__,
  458. (unsigned long)vbuf, pos);
  459. /* tell v4l buffer was filled */
  460. buf->vb.field_count = cam->frame_count * 2;
  461. do_gettimeofday(&ts);
  462. buf->vb.ts = ts;
  463. buf->vb.state = VIDEOBUF_DONE;
  464. }
  465. static int zr364xx_got_frame(struct zr364xx_camera *cam, int jpgsize)
  466. {
  467. struct zr364xx_dmaqueue *dma_q = &cam->vidq;
  468. struct zr364xx_buffer *buf;
  469. unsigned long flags = 0;
  470. int rc = 0;
  471. DBG("wakeup: %p\n", &dma_q);
  472. spin_lock_irqsave(&cam->slock, flags);
  473. if (list_empty(&dma_q->active)) {
  474. DBG("No active queue to serve\n");
  475. rc = -1;
  476. goto unlock;
  477. }
  478. buf = list_entry(dma_q->active.next,
  479. struct zr364xx_buffer, vb.queue);
  480. if (!waitqueue_active(&buf->vb.done)) {
  481. /* no one active */
  482. rc = -1;
  483. goto unlock;
  484. }
  485. list_del(&buf->vb.queue);
  486. do_gettimeofday(&buf->vb.ts);
  487. DBG("[%p/%d] wakeup\n", buf, buf->vb.i);
  488. zr364xx_fillbuff(cam, buf, jpgsize);
  489. wake_up(&buf->vb.done);
  490. DBG("wakeup [buf/i] [%p/%d]\n", buf, buf->vb.i);
  491. unlock:
  492. spin_unlock_irqrestore(&cam->slock, flags);
  493. return 0;
  494. }
  495. /* this function moves the usb stream read pipe data
  496. * into the system buffers.
  497. * returns 0 on success, EAGAIN if more data to process (call this
  498. * function again).
  499. */
  500. static int zr364xx_read_video_callback(struct zr364xx_camera *cam,
  501. struct zr364xx_pipeinfo *pipe_info,
  502. struct urb *purb)
  503. {
  504. unsigned char *pdest;
  505. unsigned char *psrc;
  506. s32 idx = -1;
  507. struct zr364xx_framei *frm;
  508. int i = 0;
  509. unsigned char *ptr = NULL;
  510. _DBG("buffer to user\n");
  511. idx = cam->cur_frame;
  512. frm = &cam->buffer.frame[idx];
  513. /* swap bytes if camera needs it */
  514. if (cam->method == METHOD0) {
  515. u16 *buf = (u16 *)pipe_info->transfer_buffer;
  516. for (i = 0; i < purb->actual_length/2; i++)
  517. swab16s(buf + i);
  518. }
  519. /* search done. now find out if should be acquiring */
  520. if (!cam->b_acquire) {
  521. /* we found a frame, but this channel is turned off */
  522. frm->ulState = ZR364XX_READ_IDLE;
  523. return -EINVAL;
  524. }
  525. psrc = (u8 *)pipe_info->transfer_buffer;
  526. ptr = pdest = frm->lpvbits;
  527. if (frm->ulState == ZR364XX_READ_IDLE) {
  528. frm->ulState = ZR364XX_READ_FRAME;
  529. frm->cur_size = 0;
  530. _DBG("jpeg header, ");
  531. memcpy(ptr, header1, sizeof(header1));
  532. ptr += sizeof(header1);
  533. header3 = 0;
  534. memcpy(ptr, &header3, 1);
  535. ptr++;
  536. memcpy(ptr, psrc, 64);
  537. ptr += 64;
  538. header3 = 1;
  539. memcpy(ptr, &header3, 1);
  540. ptr++;
  541. memcpy(ptr, psrc + 64, 64);
  542. ptr += 64;
  543. memcpy(ptr, header2, sizeof(header2));
  544. ptr += sizeof(header2);
  545. memcpy(ptr, psrc + 128,
  546. purb->actual_length - 128);
  547. ptr += purb->actual_length - 128;
  548. _DBG("header : %d %d %d %d %d %d %d %d %d\n",
  549. psrc[0], psrc[1], psrc[2],
  550. psrc[3], psrc[4], psrc[5],
  551. psrc[6], psrc[7], psrc[8]);
  552. frm->cur_size = ptr - pdest;
  553. } else {
  554. if (frm->cur_size + purb->actual_length > MAX_FRAME_SIZE) {
  555. dev_info(&cam->udev->dev,
  556. "%s: buffer (%d bytes) too small to hold "
  557. "frame data. Discarding frame data.\n",
  558. __func__, MAX_FRAME_SIZE);
  559. } else {
  560. pdest += frm->cur_size;
  561. memcpy(pdest, psrc, purb->actual_length);
  562. frm->cur_size += purb->actual_length;
  563. }
  564. }
  565. /*_DBG("cur_size %lu urb size %d\n", frm->cur_size,
  566. purb->actual_length);*/
  567. if (purb->actual_length < pipe_info->transfer_size) {
  568. _DBG("****************Buffer[%d]full*************\n", idx);
  569. cam->last_frame = cam->cur_frame;
  570. cam->cur_frame++;
  571. /* end of system frame ring buffer, start at zero */
  572. if (cam->cur_frame == cam->buffer.dwFrames)
  573. cam->cur_frame = 0;
  574. /* frame ready */
  575. /* go back to find the JPEG EOI marker */
  576. ptr = pdest = frm->lpvbits;
  577. ptr += frm->cur_size - 2;
  578. while (ptr > pdest) {
  579. if (*ptr == 0xFF && *(ptr + 1) == 0xD9
  580. && *(ptr + 2) == 0xFF)
  581. break;
  582. ptr--;
  583. }
  584. if (ptr == pdest)
  585. DBG("No EOI marker\n");
  586. /* Sometimes there is junk data in the middle of the picture,
  587. * we want to skip this bogus frames */
  588. while (ptr > pdest) {
  589. if (*ptr == 0xFF && *(ptr + 1) == 0xFF
  590. && *(ptr + 2) == 0xFF)
  591. break;
  592. ptr--;
  593. }
  594. if (ptr != pdest) {
  595. DBG("Bogus frame ? %d\n", ++(cam->nb));
  596. } else if (cam->b_acquire) {
  597. /* we skip the 2 first frames which are usually buggy */
  598. if (cam->skip)
  599. cam->skip--;
  600. else {
  601. _DBG("jpeg(%lu): %d %d %d %d %d %d %d %d\n",
  602. frm->cur_size,
  603. pdest[0], pdest[1], pdest[2], pdest[3],
  604. pdest[4], pdest[5], pdest[6], pdest[7]);
  605. zr364xx_got_frame(cam, frm->cur_size);
  606. }
  607. }
  608. cam->frame_count++;
  609. frm->ulState = ZR364XX_READ_IDLE;
  610. frm->cur_size = 0;
  611. }
  612. /* done successfully */
  613. return 0;
  614. }
  615. static int res_get(struct zr364xx_camera *cam)
  616. {
  617. /* is it free? */
  618. mutex_lock(&cam->lock);
  619. if (cam->resources) {
  620. /* no, someone else uses it */
  621. mutex_unlock(&cam->lock);
  622. return 0;
  623. }
  624. /* it's free, grab it */
  625. cam->resources = 1;
  626. _DBG("res: get\n");
  627. mutex_unlock(&cam->lock);
  628. return 1;
  629. }
  630. static inline int res_check(struct zr364xx_camera *cam)
  631. {
  632. return cam->resources;
  633. }
  634. static void res_free(struct zr364xx_camera *cam)
  635. {
  636. mutex_lock(&cam->lock);
  637. cam->resources = 0;
  638. mutex_unlock(&cam->lock);
  639. _DBG("res: put\n");
  640. }
  641. static int zr364xx_vidioc_querycap(struct file *file, void *priv,
  642. struct v4l2_capability *cap)
  643. {
  644. struct zr364xx_camera *cam = video_drvdata(file);
  645. strlcpy(cap->driver, DRIVER_DESC, sizeof(cap->driver));
  646. strlcpy(cap->card, cam->udev->product, sizeof(cap->card));
  647. strlcpy(cap->bus_info, dev_name(&cam->udev->dev),
  648. sizeof(cap->bus_info));
  649. cap->version = ZR364XX_VERSION_CODE;
  650. cap->capabilities = V4L2_CAP_VIDEO_CAPTURE |
  651. V4L2_CAP_READWRITE |
  652. V4L2_CAP_STREAMING;
  653. return 0;
  654. }
  655. static int zr364xx_vidioc_enum_input(struct file *file, void *priv,
  656. struct v4l2_input *i)
  657. {
  658. if (i->index != 0)
  659. return -EINVAL;
  660. strcpy(i->name, DRIVER_DESC " Camera");
  661. i->type = V4L2_INPUT_TYPE_CAMERA;
  662. return 0;
  663. }
  664. static int zr364xx_vidioc_g_input(struct file *file, void *priv,
  665. unsigned int *i)
  666. {
  667. *i = 0;
  668. return 0;
  669. }
  670. static int zr364xx_vidioc_s_input(struct file *file, void *priv,
  671. unsigned int i)
  672. {
  673. if (i != 0)
  674. return -EINVAL;
  675. return 0;
  676. }
  677. static int zr364xx_vidioc_queryctrl(struct file *file, void *priv,
  678. struct v4l2_queryctrl *c)
  679. {
  680. struct zr364xx_camera *cam;
  681. if (file == NULL)
  682. return -ENODEV;
  683. cam = video_drvdata(file);
  684. switch (c->id) {
  685. case V4L2_CID_BRIGHTNESS:
  686. c->type = V4L2_CTRL_TYPE_INTEGER;
  687. strcpy(c->name, "Brightness");
  688. c->minimum = 0;
  689. c->maximum = 127;
  690. c->step = 1;
  691. c->default_value = cam->mode.brightness;
  692. c->flags = 0;
  693. break;
  694. default:
  695. return -EINVAL;
  696. }
  697. return 0;
  698. }
  699. static int zr364xx_vidioc_s_ctrl(struct file *file, void *priv,
  700. struct v4l2_control *c)
  701. {
  702. struct zr364xx_camera *cam;
  703. int temp;
  704. if (file == NULL)
  705. return -ENODEV;
  706. cam = video_drvdata(file);
  707. switch (c->id) {
  708. case V4L2_CID_BRIGHTNESS:
  709. cam->mode.brightness = c->value;
  710. /* hardware brightness */
  711. mutex_lock(&cam->lock);
  712. send_control_msg(cam->udev, 1, 0x2001, 0, NULL, 0);
  713. temp = (0x60 << 8) + 127 - cam->mode.brightness;
  714. send_control_msg(cam->udev, 1, temp, 0, NULL, 0);
  715. mutex_unlock(&cam->lock);
  716. break;
  717. default:
  718. return -EINVAL;
  719. }
  720. return 0;
  721. }
  722. static int zr364xx_vidioc_g_ctrl(struct file *file, void *priv,
  723. struct v4l2_control *c)
  724. {
  725. struct zr364xx_camera *cam;
  726. if (file == NULL)
  727. return -ENODEV;
  728. cam = video_drvdata(file);
  729. switch (c->id) {
  730. case V4L2_CID_BRIGHTNESS:
  731. c->value = cam->mode.brightness;
  732. break;
  733. default:
  734. return -EINVAL;
  735. }
  736. return 0;
  737. }
  738. static int zr364xx_vidioc_enum_fmt_vid_cap(struct file *file,
  739. void *priv, struct v4l2_fmtdesc *f)
  740. {
  741. if (f->index > 0)
  742. return -EINVAL;
  743. f->flags = V4L2_FMT_FLAG_COMPRESSED;
  744. strcpy(f->description, formats[0].name);
  745. f->pixelformat = formats[0].fourcc;
  746. return 0;
  747. }
  748. static char *decode_fourcc(__u32 pixelformat, char *buf)
  749. {
  750. buf[0] = pixelformat & 0xff;
  751. buf[1] = (pixelformat >> 8) & 0xff;
  752. buf[2] = (pixelformat >> 16) & 0xff;
  753. buf[3] = (pixelformat >> 24) & 0xff;
  754. buf[4] = '\0';
  755. return buf;
  756. }
  757. static int zr364xx_vidioc_try_fmt_vid_cap(struct file *file, void *priv,
  758. struct v4l2_format *f)
  759. {
  760. struct zr364xx_camera *cam = video_drvdata(file);
  761. char pixelformat_name[5];
  762. if (cam == NULL)
  763. return -ENODEV;
  764. if (f->fmt.pix.pixelformat != V4L2_PIX_FMT_JPEG) {
  765. DBG("%s: unsupported pixelformat V4L2_PIX_FMT_%s\n", __func__,
  766. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name));
  767. return -EINVAL;
  768. }
  769. if (!(f->fmt.pix.width == 160 && f->fmt.pix.height == 120) &&
  770. !(f->fmt.pix.width == 640 && f->fmt.pix.height == 480)) {
  771. f->fmt.pix.width = 320;
  772. f->fmt.pix.height = 240;
  773. }
  774. f->fmt.pix.field = V4L2_FIELD_NONE;
  775. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  776. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  777. f->fmt.pix.colorspace = 0;
  778. f->fmt.pix.priv = 0;
  779. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  780. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  781. f->fmt.pix.field);
  782. return 0;
  783. }
  784. static int zr364xx_vidioc_g_fmt_vid_cap(struct file *file, void *priv,
  785. struct v4l2_format *f)
  786. {
  787. struct zr364xx_camera *cam;
  788. if (file == NULL)
  789. return -ENODEV;
  790. cam = video_drvdata(file);
  791. f->fmt.pix.pixelformat = formats[0].fourcc;
  792. f->fmt.pix.field = V4L2_FIELD_NONE;
  793. f->fmt.pix.width = cam->width;
  794. f->fmt.pix.height = cam->height;
  795. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  796. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  797. f->fmt.pix.colorspace = 0;
  798. f->fmt.pix.priv = 0;
  799. return 0;
  800. }
  801. static int zr364xx_vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  802. struct v4l2_format *f)
  803. {
  804. struct zr364xx_camera *cam = video_drvdata(file);
  805. struct videobuf_queue *q = &cam->vb_vidq;
  806. char pixelformat_name[5];
  807. int ret = zr364xx_vidioc_try_fmt_vid_cap(file, cam, f);
  808. int i;
  809. if (ret < 0)
  810. return ret;
  811. mutex_lock(&q->vb_lock);
  812. if (videobuf_queue_is_busy(&cam->vb_vidq)) {
  813. DBG("%s queue busy\n", __func__);
  814. ret = -EBUSY;
  815. goto out;
  816. }
  817. if (res_check(cam)) {
  818. DBG("%s can't change format after started\n", __func__);
  819. ret = -EBUSY;
  820. goto out;
  821. }
  822. cam->width = f->fmt.pix.width;
  823. cam->height = f->fmt.pix.height;
  824. dev_info(&cam->udev->dev, "%s: %dx%d mode selected\n", __func__,
  825. cam->width, cam->height);
  826. f->fmt.pix.bytesperline = f->fmt.pix.width * 2;
  827. f->fmt.pix.sizeimage = f->fmt.pix.height * f->fmt.pix.bytesperline;
  828. f->fmt.pix.colorspace = 0;
  829. f->fmt.pix.priv = 0;
  830. cam->vb_vidq.field = f->fmt.pix.field;
  831. cam->mode.color = V4L2_PIX_FMT_JPEG;
  832. if (f->fmt.pix.width == 160 && f->fmt.pix.height == 120)
  833. mode = 1;
  834. else if (f->fmt.pix.width == 640 && f->fmt.pix.height == 480)
  835. mode = 2;
  836. else
  837. mode = 0;
  838. m0d1[0] = mode;
  839. m1[2].value = 0xf000 + mode;
  840. m2[1].value = 0xf000 + mode;
  841. /* special case for METHOD3, the modes are different */
  842. if (cam->method == METHOD3) {
  843. switch (mode) {
  844. case 1:
  845. m2[1].value = 0xf000 + 4;
  846. break;
  847. case 2:
  848. m2[1].value = 0xf000 + 0;
  849. break;
  850. default:
  851. m2[1].value = 0xf000 + 1;
  852. break;
  853. }
  854. }
  855. header2[437] = cam->height / 256;
  856. header2[438] = cam->height % 256;
  857. header2[439] = cam->width / 256;
  858. header2[440] = cam->width % 256;
  859. for (i = 0; init[cam->method][i].size != -1; i++) {
  860. ret =
  861. send_control_msg(cam->udev, 1, init[cam->method][i].value,
  862. 0, init[cam->method][i].bytes,
  863. init[cam->method][i].size);
  864. if (ret < 0) {
  865. dev_err(&cam->udev->dev,
  866. "error during resolution change sequence: %d\n", i);
  867. goto out;
  868. }
  869. }
  870. /* Added some delay here, since opening/closing the camera quickly,
  871. * like Ekiga does during its startup, can crash the webcam
  872. */
  873. mdelay(100);
  874. cam->skip = 2;
  875. ret = 0;
  876. out:
  877. mutex_unlock(&q->vb_lock);
  878. DBG("%s: V4L2_PIX_FMT_%s (%d) ok!\n", __func__,
  879. decode_fourcc(f->fmt.pix.pixelformat, pixelformat_name),
  880. f->fmt.pix.field);
  881. return ret;
  882. }
  883. static int zr364xx_vidioc_reqbufs(struct file *file, void *priv,
  884. struct v4l2_requestbuffers *p)
  885. {
  886. int rc;
  887. struct zr364xx_camera *cam = video_drvdata(file);
  888. rc = videobuf_reqbufs(&cam->vb_vidq, p);
  889. return rc;
  890. }
  891. static int zr364xx_vidioc_querybuf(struct file *file,
  892. void *priv,
  893. struct v4l2_buffer *p)
  894. {
  895. int rc;
  896. struct zr364xx_camera *cam = video_drvdata(file);
  897. rc = videobuf_querybuf(&cam->vb_vidq, p);
  898. return rc;
  899. }
  900. static int zr364xx_vidioc_qbuf(struct file *file,
  901. void *priv,
  902. struct v4l2_buffer *p)
  903. {
  904. int rc;
  905. struct zr364xx_camera *cam = video_drvdata(file);
  906. _DBG("%s\n", __func__);
  907. rc = videobuf_qbuf(&cam->vb_vidq, p);
  908. return rc;
  909. }
  910. static int zr364xx_vidioc_dqbuf(struct file *file,
  911. void *priv,
  912. struct v4l2_buffer *p)
  913. {
  914. int rc;
  915. struct zr364xx_camera *cam = video_drvdata(file);
  916. _DBG("%s\n", __func__);
  917. rc = videobuf_dqbuf(&cam->vb_vidq, p, file->f_flags & O_NONBLOCK);
  918. return rc;
  919. }
  920. static void read_pipe_completion(struct urb *purb)
  921. {
  922. struct zr364xx_pipeinfo *pipe_info;
  923. struct zr364xx_camera *cam;
  924. int pipe;
  925. pipe_info = purb->context;
  926. _DBG("%s %p, status %d\n", __func__, purb, purb->status);
  927. if (pipe_info == NULL) {
  928. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  929. return;
  930. }
  931. cam = pipe_info->cam;
  932. if (cam == NULL) {
  933. printk(KERN_ERR KBUILD_MODNAME ": no context!\n");
  934. return;
  935. }
  936. /* if shutting down, do not resubmit, exit immediately */
  937. if (purb->status == -ESHUTDOWN) {
  938. DBG("%s, err shutdown\n", __func__);
  939. pipe_info->err_count++;
  940. return;
  941. }
  942. if (pipe_info->state == 0) {
  943. DBG("exiting USB pipe\n");
  944. return;
  945. }
  946. if (purb->actual_length < 0 ||
  947. purb->actual_length > pipe_info->transfer_size) {
  948. dev_err(&cam->udev->dev, "wrong number of bytes\n");
  949. return;
  950. }
  951. if (purb->status == 0)
  952. zr364xx_read_video_callback(cam, pipe_info, purb);
  953. else {
  954. pipe_info->err_count++;
  955. DBG("%s: failed URB %d\n", __func__, purb->status);
  956. }
  957. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  958. /* reuse urb */
  959. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  960. pipe,
  961. pipe_info->transfer_buffer,
  962. pipe_info->transfer_size,
  963. read_pipe_completion, pipe_info);
  964. if (pipe_info->state != 0) {
  965. purb->status = usb_submit_urb(pipe_info->stream_urb,
  966. GFP_ATOMIC);
  967. if (purb->status)
  968. dev_err(&cam->udev->dev,
  969. "error submitting urb (error=%i)\n",
  970. purb->status);
  971. } else
  972. DBG("read pipe complete state 0\n");
  973. }
  974. static int zr364xx_start_readpipe(struct zr364xx_camera *cam)
  975. {
  976. int pipe;
  977. int retval;
  978. struct zr364xx_pipeinfo *pipe_info = cam->pipe;
  979. pipe = usb_rcvbulkpipe(cam->udev, cam->read_endpoint);
  980. DBG("%s: start pipe IN x%x\n", __func__, cam->read_endpoint);
  981. pipe_info->state = 1;
  982. pipe_info->err_count = 0;
  983. pipe_info->stream_urb = usb_alloc_urb(0, GFP_KERNEL);
  984. if (!pipe_info->stream_urb) {
  985. dev_err(&cam->udev->dev, "ReadStream: Unable to alloc URB\n");
  986. return -ENOMEM;
  987. }
  988. /* transfer buffer allocated in board_init */
  989. usb_fill_bulk_urb(pipe_info->stream_urb, cam->udev,
  990. pipe,
  991. pipe_info->transfer_buffer,
  992. pipe_info->transfer_size,
  993. read_pipe_completion, pipe_info);
  994. DBG("submitting URB %p\n", pipe_info->stream_urb);
  995. retval = usb_submit_urb(pipe_info->stream_urb, GFP_KERNEL);
  996. if (retval) {
  997. printk(KERN_ERR KBUILD_MODNAME ": start read pipe failed\n");
  998. return retval;
  999. }
  1000. return 0;
  1001. }
  1002. static void zr364xx_stop_readpipe(struct zr364xx_camera *cam)
  1003. {
  1004. struct zr364xx_pipeinfo *pipe_info;
  1005. if (cam == NULL) {
  1006. printk(KERN_ERR KBUILD_MODNAME ": invalid device\n");
  1007. return;
  1008. }
  1009. DBG("stop read pipe\n");
  1010. pipe_info = cam->pipe;
  1011. if (pipe_info) {
  1012. if (pipe_info->state != 0)
  1013. pipe_info->state = 0;
  1014. if (pipe_info->stream_urb) {
  1015. /* cancel urb */
  1016. usb_kill_urb(pipe_info->stream_urb);
  1017. usb_free_urb(pipe_info->stream_urb);
  1018. pipe_info->stream_urb = NULL;
  1019. }
  1020. }
  1021. return;
  1022. }
  1023. /* starts acquisition process */
  1024. static int zr364xx_start_acquire(struct zr364xx_camera *cam)
  1025. {
  1026. int j;
  1027. DBG("start acquire\n");
  1028. cam->last_frame = -1;
  1029. cam->cur_frame = 0;
  1030. for (j = 0; j < FRAMES; j++) {
  1031. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  1032. cam->buffer.frame[j].cur_size = 0;
  1033. }
  1034. cam->b_acquire = 1;
  1035. return 0;
  1036. }
  1037. static inline int zr364xx_stop_acquire(struct zr364xx_camera *cam)
  1038. {
  1039. cam->b_acquire = 0;
  1040. return 0;
  1041. }
  1042. static int zr364xx_vidioc_streamon(struct file *file, void *priv,
  1043. enum v4l2_buf_type type)
  1044. {
  1045. struct zr364xx_camera *cam = video_drvdata(file);
  1046. int j;
  1047. int res;
  1048. DBG("%s\n", __func__);
  1049. if (cam->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1050. dev_err(&cam->udev->dev, "invalid fh type0\n");
  1051. return -EINVAL;
  1052. }
  1053. if (cam->type != type) {
  1054. dev_err(&cam->udev->dev, "invalid fh type1\n");
  1055. return -EINVAL;
  1056. }
  1057. if (!res_get(cam)) {
  1058. dev_err(&cam->udev->dev, "stream busy\n");
  1059. return -EBUSY;
  1060. }
  1061. cam->last_frame = -1;
  1062. cam->cur_frame = 0;
  1063. cam->frame_count = 0;
  1064. for (j = 0; j < FRAMES; j++) {
  1065. cam->buffer.frame[j].ulState = ZR364XX_READ_IDLE;
  1066. cam->buffer.frame[j].cur_size = 0;
  1067. }
  1068. res = videobuf_streamon(&cam->vb_vidq);
  1069. if (res == 0) {
  1070. zr364xx_start_acquire(cam);
  1071. } else {
  1072. res_free(cam);
  1073. }
  1074. return res;
  1075. }
  1076. static int zr364xx_vidioc_streamoff(struct file *file, void *priv,
  1077. enum v4l2_buf_type type)
  1078. {
  1079. int res;
  1080. struct zr364xx_camera *cam = video_drvdata(file);
  1081. DBG("%s\n", __func__);
  1082. if (cam->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) {
  1083. dev_err(&cam->udev->dev, "invalid fh type0\n");
  1084. return -EINVAL;
  1085. }
  1086. if (cam->type != type) {
  1087. dev_err(&cam->udev->dev, "invalid fh type1\n");
  1088. return -EINVAL;
  1089. }
  1090. zr364xx_stop_acquire(cam);
  1091. res = videobuf_streamoff(&cam->vb_vidq);
  1092. if (res < 0)
  1093. return res;
  1094. res_free(cam);
  1095. return 0;
  1096. }
  1097. /* open the camera */
  1098. static int zr364xx_open(struct file *file)
  1099. {
  1100. struct video_device *vdev = video_devdata(file);
  1101. struct zr364xx_camera *cam = video_drvdata(file);
  1102. struct usb_device *udev = cam->udev;
  1103. int i, err;
  1104. DBG("%s\n", __func__);
  1105. mutex_lock(&cam->open_lock);
  1106. if (cam->users) {
  1107. err = -EBUSY;
  1108. goto out;
  1109. }
  1110. for (i = 0; init[cam->method][i].size != -1; i++) {
  1111. err =
  1112. send_control_msg(udev, 1, init[cam->method][i].value,
  1113. 0, init[cam->method][i].bytes,
  1114. init[cam->method][i].size);
  1115. if (err < 0) {
  1116. dev_err(&cam->udev->dev,
  1117. "error during open sequence: %d\n", i);
  1118. goto out;
  1119. }
  1120. }
  1121. cam->skip = 2;
  1122. cam->users++;
  1123. file->private_data = vdev;
  1124. cam->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  1125. cam->fmt = formats;
  1126. videobuf_queue_vmalloc_init(&cam->vb_vidq, &zr364xx_video_qops,
  1127. NULL, &cam->slock,
  1128. cam->type,
  1129. V4L2_FIELD_NONE,
  1130. sizeof(struct zr364xx_buffer), cam);
  1131. /* Added some delay here, since opening/closing the camera quickly,
  1132. * like Ekiga does during its startup, can crash the webcam
  1133. */
  1134. mdelay(100);
  1135. err = 0;
  1136. out:
  1137. mutex_unlock(&cam->open_lock);
  1138. DBG("%s: %d\n", __func__, err);
  1139. return err;
  1140. }
  1141. static void zr364xx_destroy(struct zr364xx_camera *cam)
  1142. {
  1143. unsigned long i;
  1144. if (!cam) {
  1145. printk(KERN_ERR KBUILD_MODNAME ", %s: no device\n", __func__);
  1146. return;
  1147. }
  1148. mutex_lock(&cam->open_lock);
  1149. if (cam->vdev)
  1150. video_unregister_device(cam->vdev);
  1151. cam->vdev = NULL;
  1152. /* stops the read pipe if it is running */
  1153. if (cam->b_acquire)
  1154. zr364xx_stop_acquire(cam);
  1155. zr364xx_stop_readpipe(cam);
  1156. /* release sys buffers */
  1157. for (i = 0; i < FRAMES; i++) {
  1158. if (cam->buffer.frame[i].lpvbits) {
  1159. DBG("vfree %p\n", cam->buffer.frame[i].lpvbits);
  1160. vfree(cam->buffer.frame[i].lpvbits);
  1161. }
  1162. cam->buffer.frame[i].lpvbits = NULL;
  1163. }
  1164. /* release transfer buffer */
  1165. kfree(cam->pipe->transfer_buffer);
  1166. cam->pipe->transfer_buffer = NULL;
  1167. mutex_unlock(&cam->open_lock);
  1168. kfree(cam);
  1169. cam = NULL;
  1170. }
  1171. /* release the camera */
  1172. static int zr364xx_release(struct file *file)
  1173. {
  1174. struct zr364xx_camera *cam;
  1175. struct usb_device *udev;
  1176. int i, err;
  1177. DBG("%s\n", __func__);
  1178. cam = video_drvdata(file);
  1179. if (!cam)
  1180. return -ENODEV;
  1181. mutex_lock(&cam->open_lock);
  1182. udev = cam->udev;
  1183. /* turn off stream */
  1184. if (res_check(cam)) {
  1185. if (cam->b_acquire)
  1186. zr364xx_stop_acquire(cam);
  1187. videobuf_streamoff(&cam->vb_vidq);
  1188. res_free(cam);
  1189. }
  1190. cam->users--;
  1191. file->private_data = NULL;
  1192. for (i = 0; i < 2; i++) {
  1193. err =
  1194. send_control_msg(udev, 1, init[cam->method][i].value,
  1195. 0, init[cam->method][i].bytes,
  1196. init[cam->method][i].size);
  1197. if (err < 0) {
  1198. dev_err(&udev->dev, "error during release sequence\n");
  1199. goto out;
  1200. }
  1201. }
  1202. /* Added some delay here, since opening/closing the camera quickly,
  1203. * like Ekiga does during its startup, can crash the webcam
  1204. */
  1205. mdelay(100);
  1206. err = 0;
  1207. out:
  1208. mutex_unlock(&cam->open_lock);
  1209. return err;
  1210. }
  1211. static int zr364xx_mmap(struct file *file, struct vm_area_struct *vma)
  1212. {
  1213. struct zr364xx_camera *cam = video_drvdata(file);
  1214. int ret;
  1215. if (cam == NULL) {
  1216. DBG("%s: cam == NULL\n", __func__);
  1217. return -ENODEV;
  1218. }
  1219. DBG("mmap called, vma=0x%08lx\n", (unsigned long)vma);
  1220. ret = videobuf_mmap_mapper(&cam->vb_vidq, vma);
  1221. DBG("vma start=0x%08lx, size=%ld, ret=%d\n",
  1222. (unsigned long)vma->vm_start,
  1223. (unsigned long)vma->vm_end - (unsigned long)vma->vm_start, ret);
  1224. return ret;
  1225. }
  1226. static unsigned int zr364xx_poll(struct file *file,
  1227. struct poll_table_struct *wait)
  1228. {
  1229. struct zr364xx_camera *cam = video_drvdata(file);
  1230. struct videobuf_queue *q = &cam->vb_vidq;
  1231. _DBG("%s\n", __func__);
  1232. if (cam->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
  1233. return POLLERR;
  1234. return videobuf_poll_stream(file, q, wait);
  1235. }
  1236. static const struct v4l2_file_operations zr364xx_fops = {
  1237. .owner = THIS_MODULE,
  1238. .open = zr364xx_open,
  1239. .release = zr364xx_release,
  1240. .read = zr364xx_read,
  1241. .mmap = zr364xx_mmap,
  1242. .ioctl = video_ioctl2,
  1243. .poll = zr364xx_poll,
  1244. };
  1245. static const struct v4l2_ioctl_ops zr364xx_ioctl_ops = {
  1246. .vidioc_querycap = zr364xx_vidioc_querycap,
  1247. .vidioc_enum_fmt_vid_cap = zr364xx_vidioc_enum_fmt_vid_cap,
  1248. .vidioc_try_fmt_vid_cap = zr364xx_vidioc_try_fmt_vid_cap,
  1249. .vidioc_s_fmt_vid_cap = zr364xx_vidioc_s_fmt_vid_cap,
  1250. .vidioc_g_fmt_vid_cap = zr364xx_vidioc_g_fmt_vid_cap,
  1251. .vidioc_enum_input = zr364xx_vidioc_enum_input,
  1252. .vidioc_g_input = zr364xx_vidioc_g_input,
  1253. .vidioc_s_input = zr364xx_vidioc_s_input,
  1254. .vidioc_streamon = zr364xx_vidioc_streamon,
  1255. .vidioc_streamoff = zr364xx_vidioc_streamoff,
  1256. .vidioc_queryctrl = zr364xx_vidioc_queryctrl,
  1257. .vidioc_g_ctrl = zr364xx_vidioc_g_ctrl,
  1258. .vidioc_s_ctrl = zr364xx_vidioc_s_ctrl,
  1259. .vidioc_reqbufs = zr364xx_vidioc_reqbufs,
  1260. .vidioc_querybuf = zr364xx_vidioc_querybuf,
  1261. .vidioc_qbuf = zr364xx_vidioc_qbuf,
  1262. .vidioc_dqbuf = zr364xx_vidioc_dqbuf,
  1263. };
  1264. static struct video_device zr364xx_template = {
  1265. .name = DRIVER_DESC,
  1266. .fops = &zr364xx_fops,
  1267. .ioctl_ops = &zr364xx_ioctl_ops,
  1268. .release = video_device_release,
  1269. };
  1270. /*******************/
  1271. /* USB integration */
  1272. /*******************/
  1273. static int zr364xx_board_init(struct zr364xx_camera *cam)
  1274. {
  1275. struct zr364xx_pipeinfo *pipe = cam->pipe;
  1276. unsigned long i;
  1277. DBG("board init: %p\n", cam);
  1278. memset(pipe, 0, sizeof(*pipe));
  1279. pipe->cam = cam;
  1280. pipe->transfer_size = BUFFER_SIZE;
  1281. pipe->transfer_buffer = kzalloc(pipe->transfer_size,
  1282. GFP_KERNEL);
  1283. if (pipe->transfer_buffer == NULL) {
  1284. DBG("out of memory!\n");
  1285. return -ENOMEM;
  1286. }
  1287. cam->b_acquire = 0;
  1288. cam->frame_count = 0;
  1289. /*** start create system buffers ***/
  1290. for (i = 0; i < FRAMES; i++) {
  1291. /* always allocate maximum size for system buffers */
  1292. cam->buffer.frame[i].lpvbits = vmalloc(MAX_FRAME_SIZE);
  1293. DBG("valloc %p, idx %lu, pdata %p\n",
  1294. &cam->buffer.frame[i], i,
  1295. cam->buffer.frame[i].lpvbits);
  1296. if (cam->buffer.frame[i].lpvbits == NULL) {
  1297. printk(KERN_INFO KBUILD_MODNAME ": out of memory. "
  1298. "Using less frames\n");
  1299. break;
  1300. }
  1301. }
  1302. if (i == 0) {
  1303. printk(KERN_INFO KBUILD_MODNAME ": out of memory. Aborting\n");
  1304. kfree(cam->pipe->transfer_buffer);
  1305. cam->pipe->transfer_buffer = NULL;
  1306. return -ENOMEM;
  1307. } else
  1308. cam->buffer.dwFrames = i;
  1309. /* make sure internal states are set */
  1310. for (i = 0; i < FRAMES; i++) {
  1311. cam->buffer.frame[i].ulState = ZR364XX_READ_IDLE;
  1312. cam->buffer.frame[i].cur_size = 0;
  1313. }
  1314. cam->cur_frame = 0;
  1315. cam->last_frame = -1;
  1316. /*** end create system buffers ***/
  1317. /* start read pipe */
  1318. zr364xx_start_readpipe(cam);
  1319. DBG(": board initialized\n");
  1320. return 0;
  1321. }
  1322. static int zr364xx_probe(struct usb_interface *intf,
  1323. const struct usb_device_id *id)
  1324. {
  1325. struct usb_device *udev = interface_to_usbdev(intf);
  1326. struct zr364xx_camera *cam = NULL;
  1327. struct usb_host_interface *iface_desc;
  1328. struct usb_endpoint_descriptor *endpoint;
  1329. int err;
  1330. int i;
  1331. DBG("probing...\n");
  1332. dev_info(&intf->dev, DRIVER_DESC " compatible webcam plugged\n");
  1333. dev_info(&intf->dev, "model %04x:%04x detected\n",
  1334. le16_to_cpu(udev->descriptor.idVendor),
  1335. le16_to_cpu(udev->descriptor.idProduct));
  1336. cam = kzalloc(sizeof(struct zr364xx_camera), GFP_KERNEL);
  1337. if (cam == NULL) {
  1338. dev_err(&udev->dev, "cam: out of memory !\n");
  1339. return -ENOMEM;
  1340. }
  1341. /* save the init method used by this camera */
  1342. cam->method = id->driver_info;
  1343. cam->vdev = video_device_alloc();
  1344. if (cam->vdev == NULL) {
  1345. dev_err(&udev->dev, "cam->vdev: out of memory !\n");
  1346. kfree(cam);
  1347. cam = NULL;
  1348. return -ENOMEM;
  1349. }
  1350. memcpy(cam->vdev, &zr364xx_template, sizeof(zr364xx_template));
  1351. cam->vdev->parent = &intf->dev;
  1352. video_set_drvdata(cam->vdev, cam);
  1353. if (debug)
  1354. cam->vdev->debug = V4L2_DEBUG_IOCTL | V4L2_DEBUG_IOCTL_ARG;
  1355. cam->udev = udev;
  1356. switch (mode) {
  1357. case 1:
  1358. dev_info(&udev->dev, "160x120 mode selected\n");
  1359. cam->width = 160;
  1360. cam->height = 120;
  1361. break;
  1362. case 2:
  1363. dev_info(&udev->dev, "640x480 mode selected\n");
  1364. cam->width = 640;
  1365. cam->height = 480;
  1366. break;
  1367. default:
  1368. dev_info(&udev->dev, "320x240 mode selected\n");
  1369. cam->width = 320;
  1370. cam->height = 240;
  1371. break;
  1372. }
  1373. m0d1[0] = mode;
  1374. m1[2].value = 0xf000 + mode;
  1375. m2[1].value = 0xf000 + mode;
  1376. /* special case for METHOD3, the modes are different */
  1377. if (cam->method == METHOD3) {
  1378. switch (mode) {
  1379. case 1:
  1380. m2[1].value = 0xf000 + 4;
  1381. break;
  1382. case 2:
  1383. m2[1].value = 0xf000 + 0;
  1384. break;
  1385. default:
  1386. m2[1].value = 0xf000 + 1;
  1387. break;
  1388. }
  1389. }
  1390. header2[437] = cam->height / 256;
  1391. header2[438] = cam->height % 256;
  1392. header2[439] = cam->width / 256;
  1393. header2[440] = cam->width % 256;
  1394. cam->users = 0;
  1395. cam->nb = 0;
  1396. cam->mode.brightness = 64;
  1397. mutex_init(&cam->lock);
  1398. mutex_init(&cam->open_lock);
  1399. DBG("dev: %p, udev %p interface %p\n", cam, cam->udev, intf);
  1400. /* set up the endpoint information */
  1401. iface_desc = intf->cur_altsetting;
  1402. DBG("num endpoints %d\n", iface_desc->desc.bNumEndpoints);
  1403. for (i = 0; i < iface_desc->desc.bNumEndpoints; ++i) {
  1404. endpoint = &iface_desc->endpoint[i].desc;
  1405. if (!cam->read_endpoint && usb_endpoint_is_bulk_in(endpoint)) {
  1406. /* we found the bulk in endpoint */
  1407. cam->read_endpoint = endpoint->bEndpointAddress;
  1408. }
  1409. }
  1410. if (!cam->read_endpoint) {
  1411. dev_err(&intf->dev, "Could not find bulk-in endpoint\n");
  1412. return -ENOMEM;
  1413. }
  1414. /* v4l */
  1415. INIT_LIST_HEAD(&cam->vidq.active);
  1416. cam->vidq.cam = cam;
  1417. err = video_register_device(cam->vdev, VFL_TYPE_GRABBER, -1);
  1418. if (err) {
  1419. dev_err(&udev->dev, "video_register_device failed\n");
  1420. video_device_release(cam->vdev);
  1421. kfree(cam);
  1422. cam = NULL;
  1423. return err;
  1424. }
  1425. usb_set_intfdata(intf, cam);
  1426. /* load zr364xx board specific */
  1427. err = zr364xx_board_init(cam);
  1428. if (err) {
  1429. spin_lock_init(&cam->slock);
  1430. return err;
  1431. }
  1432. spin_lock_init(&cam->slock);
  1433. dev_info(&udev->dev, DRIVER_DESC " controlling device %s\n",
  1434. video_device_node_name(cam->vdev));
  1435. return 0;
  1436. }
  1437. static void zr364xx_disconnect(struct usb_interface *intf)
  1438. {
  1439. struct zr364xx_camera *cam = usb_get_intfdata(intf);
  1440. videobuf_mmap_free(&cam->vb_vidq);
  1441. usb_set_intfdata(intf, NULL);
  1442. dev_info(&intf->dev, DRIVER_DESC " webcam unplugged\n");
  1443. zr364xx_destroy(cam);
  1444. }
  1445. /**********************/
  1446. /* Module integration */
  1447. /**********************/
  1448. static struct usb_driver zr364xx_driver = {
  1449. .name = "zr364xx",
  1450. .probe = zr364xx_probe,
  1451. .disconnect = zr364xx_disconnect,
  1452. .id_table = device_table
  1453. };
  1454. static int __init zr364xx_init(void)
  1455. {
  1456. int retval;
  1457. retval = usb_register(&zr364xx_driver);
  1458. if (retval)
  1459. printk(KERN_ERR KBUILD_MODNAME ": usb_register failed!\n");
  1460. else
  1461. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  1462. return retval;
  1463. }
  1464. static void __exit zr364xx_exit(void)
  1465. {
  1466. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC " module unloaded\n");
  1467. usb_deregister(&zr364xx_driver);
  1468. }
  1469. module_init(zr364xx_init);
  1470. module_exit(zr364xx_exit);
  1471. MODULE_AUTHOR(DRIVER_AUTHOR);
  1472. MODULE_DESCRIPTION(DRIVER_DESC);
  1473. MODULE_LICENSE("GPL");