vicam.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * gspca ViCam subdriver
  3. *
  4. * Copyright (C) 2011 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * Based on the usbvideo vicam driver, which is:
  7. *
  8. * Copyright (c) 2002 Joe Burks (jburks@wavicle.org),
  9. * Chris Cheney (chris.cheney@gmail.com),
  10. * Pavel Machek (pavel@ucw.cz),
  11. * John Tyner (jtyner@cs.ucr.edu),
  12. * Monroe Williams (monroe@pobox.com)
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. */
  24. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #define MODULE_NAME "vicam"
  26. #define HEADER_SIZE 64
  27. #include <linux/workqueue.h>
  28. #include <linux/slab.h>
  29. #include <linux/firmware.h>
  30. #include <linux/ihex.h>
  31. #include "gspca.h"
  32. #define VICAM_FIRMWARE "vicam/firmware.fw"
  33. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  34. MODULE_DESCRIPTION("GSPCA ViCam USB Camera Driver");
  35. MODULE_LICENSE("GPL");
  36. MODULE_FIRMWARE(VICAM_FIRMWARE);
  37. struct sd {
  38. struct gspca_dev gspca_dev; /* !! must be the first item */
  39. struct work_struct work_struct;
  40. };
  41. /* The vicam sensor has a resolution of 512 x 244, with I believe square
  42. pixels, but this is forced to a 4:3 ratio by optics. So it has
  43. non square pixels :( */
  44. static struct v4l2_pix_format vicam_mode[] = {
  45. { 256, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  46. .bytesperline = 256,
  47. .sizeimage = 256 * 122,
  48. .colorspace = V4L2_COLORSPACE_SRGB,},
  49. /* 2 modes with somewhat more square pixels */
  50. { 256, 200, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  51. .bytesperline = 256,
  52. .sizeimage = 256 * 200,
  53. .colorspace = V4L2_COLORSPACE_SRGB,},
  54. { 256, 240, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  55. .bytesperline = 256,
  56. .sizeimage = 256 * 240,
  57. .colorspace = V4L2_COLORSPACE_SRGB,},
  58. #if 0 /* This mode has extremely non square pixels, testing use only */
  59. { 512, 122, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  60. .bytesperline = 512,
  61. .sizeimage = 512 * 122,
  62. .colorspace = V4L2_COLORSPACE_SRGB,},
  63. #endif
  64. { 512, 244, V4L2_PIX_FMT_SGRBG8, V4L2_FIELD_NONE,
  65. .bytesperline = 512,
  66. .sizeimage = 512 * 244,
  67. .colorspace = V4L2_COLORSPACE_SRGB,},
  68. };
  69. static int vicam_control_msg(struct gspca_dev *gspca_dev, u8 request,
  70. u16 value, u16 index, u8 *data, u16 len)
  71. {
  72. int ret;
  73. ret = usb_control_msg(gspca_dev->dev,
  74. usb_sndctrlpipe(gspca_dev->dev, 0),
  75. request,
  76. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  77. value, index, data, len, 1000);
  78. if (ret < 0)
  79. pr_err("control msg req %02X error %d\n", request, ret);
  80. return ret;
  81. }
  82. static int vicam_set_camera_power(struct gspca_dev *gspca_dev, int state)
  83. {
  84. int ret;
  85. ret = vicam_control_msg(gspca_dev, 0x50, state, 0, NULL, 0);
  86. if (ret < 0)
  87. return ret;
  88. if (state)
  89. ret = vicam_control_msg(gspca_dev, 0x55, 1, 0, NULL, 0);
  90. return ret;
  91. }
  92. /*
  93. * request and read a block of data
  94. */
  95. static int vicam_read_frame(struct gspca_dev *gspca_dev, u8 *data, int size)
  96. {
  97. int ret, unscaled_height, act_len = 0;
  98. u8 *req_data = gspca_dev->usb_buf;
  99. s32 expo = v4l2_ctrl_g_ctrl(gspca_dev->exposure);
  100. s32 gain = v4l2_ctrl_g_ctrl(gspca_dev->gain);
  101. memset(req_data, 0, 16);
  102. req_data[0] = gain;
  103. if (gspca_dev->pixfmt.width == 256)
  104. req_data[1] |= 0x01; /* low nibble x-scale */
  105. if (gspca_dev->pixfmt.height <= 122) {
  106. req_data[1] |= 0x10; /* high nibble y-scale */
  107. unscaled_height = gspca_dev->pixfmt.height * 2;
  108. } else
  109. unscaled_height = gspca_dev->pixfmt.height;
  110. req_data[2] = 0x90; /* unknown, does not seem to do anything */
  111. if (unscaled_height <= 200)
  112. req_data[3] = 0x06; /* vend? */
  113. else if (unscaled_height <= 242) /* Yes 242 not 240 */
  114. req_data[3] = 0x07; /* vend? */
  115. else /* Up to 244 lines with req_data[3] == 0x08 */
  116. req_data[3] = 0x08; /* vend? */
  117. if (expo < 256) {
  118. /* Frame rate maxed out, use partial frame expo time */
  119. req_data[4] = 255 - expo;
  120. req_data[5] = 0x00;
  121. req_data[6] = 0x00;
  122. req_data[7] = 0x01;
  123. } else {
  124. /* Modify frame rate */
  125. req_data[4] = 0x00;
  126. req_data[5] = 0x00;
  127. req_data[6] = expo & 0xFF;
  128. req_data[7] = expo >> 8;
  129. }
  130. req_data[8] = ((244 - unscaled_height) / 2) & ~0x01; /* vstart */
  131. /* bytes 9-15 do not seem to affect exposure or image quality */
  132. mutex_lock(&gspca_dev->usb_lock);
  133. ret = vicam_control_msg(gspca_dev, 0x51, 0x80, 0, req_data, 16);
  134. mutex_unlock(&gspca_dev->usb_lock);
  135. if (ret < 0)
  136. return ret;
  137. ret = usb_bulk_msg(gspca_dev->dev,
  138. usb_rcvbulkpipe(gspca_dev->dev, 0x81),
  139. data, size, &act_len, 10000);
  140. /* successful, it returns 0, otherwise negative */
  141. if (ret < 0 || act_len != size) {
  142. pr_err("bulk read fail (%d) len %d/%d\n",
  143. ret, act_len, size);
  144. return -EIO;
  145. }
  146. return 0;
  147. }
  148. /*
  149. * This function is called as a workqueue function and runs whenever the camera
  150. * is streaming data. Because it is a workqueue function it is allowed to sleep
  151. * so we can use synchronous USB calls. To avoid possible collisions with other
  152. * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
  153. * performing USB operations using it. In practice we don't really need this
  154. * as the cameras controls are only written from the workqueue.
  155. */
  156. static void vicam_dostream(struct work_struct *work)
  157. {
  158. struct sd *sd = container_of(work, struct sd, work_struct);
  159. struct gspca_dev *gspca_dev = &sd->gspca_dev;
  160. int ret, frame_sz;
  161. u8 *buffer;
  162. frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage +
  163. HEADER_SIZE;
  164. buffer = kmalloc(frame_sz, GFP_KERNEL);
  165. if (!buffer) {
  166. pr_err("Couldn't allocate USB buffer\n");
  167. goto exit;
  168. }
  169. while (gspca_dev->present && gspca_dev->streaming) {
  170. #ifdef CONFIG_PM
  171. if (gspca_dev->frozen)
  172. break;
  173. #endif
  174. ret = vicam_read_frame(gspca_dev, buffer, frame_sz);
  175. if (ret < 0)
  176. break;
  177. /* Note the frame header contents seem to be completely
  178. constant, they do not change with either image, or
  179. settings. So we simply discard it. The frames have
  180. a very similar 64 byte footer, which we don't even
  181. bother reading from the cam */
  182. gspca_frame_add(gspca_dev, FIRST_PACKET,
  183. buffer + HEADER_SIZE,
  184. frame_sz - HEADER_SIZE);
  185. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  186. }
  187. exit:
  188. kfree(buffer);
  189. }
  190. /* This function is called at probe time just before sd_init */
  191. static int sd_config(struct gspca_dev *gspca_dev,
  192. const struct usb_device_id *id)
  193. {
  194. struct cam *cam = &gspca_dev->cam;
  195. struct sd *sd = (struct sd *)gspca_dev;
  196. /* We don't use the buffer gspca allocates so make it small. */
  197. cam->bulk = 1;
  198. cam->bulk_size = 64;
  199. cam->cam_mode = vicam_mode;
  200. cam->nmodes = ARRAY_SIZE(vicam_mode);
  201. INIT_WORK(&sd->work_struct, vicam_dostream);
  202. return 0;
  203. }
  204. /* this function is called at probe and resume time */
  205. static int sd_init(struct gspca_dev *gspca_dev)
  206. {
  207. int ret;
  208. const struct ihex_binrec *rec;
  209. const struct firmware *uninitialized_var(fw);
  210. u8 *firmware_buf;
  211. ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
  212. &gspca_dev->dev->dev);
  213. if (ret)
  214. return ret;
  215. firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  216. if (!firmware_buf) {
  217. ret = -ENOMEM;
  218. goto exit;
  219. }
  220. for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
  221. memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
  222. ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
  223. be16_to_cpu(rec->len));
  224. if (ret < 0)
  225. break;
  226. }
  227. kfree(firmware_buf);
  228. exit:
  229. release_firmware(fw);
  230. return ret;
  231. }
  232. /* Set up for getting frames. */
  233. static int sd_start(struct gspca_dev *gspca_dev)
  234. {
  235. struct sd *sd = (struct sd *)gspca_dev;
  236. int ret;
  237. ret = vicam_set_camera_power(gspca_dev, 1);
  238. if (ret < 0)
  239. return ret;
  240. schedule_work(&sd->work_struct);
  241. return 0;
  242. }
  243. /* called on streamoff with alt==0 and on disconnect */
  244. /* the usb_lock is held at entry - restore on exit */
  245. static void sd_stop0(struct gspca_dev *gspca_dev)
  246. {
  247. struct sd *dev = (struct sd *)gspca_dev;
  248. /* wait for the work queue to terminate */
  249. mutex_unlock(&gspca_dev->usb_lock);
  250. /* This waits for vicam_dostream to finish */
  251. flush_work(&dev->work_struct);
  252. mutex_lock(&gspca_dev->usb_lock);
  253. if (gspca_dev->present)
  254. vicam_set_camera_power(gspca_dev, 0);
  255. }
  256. static int sd_init_controls(struct gspca_dev *gspca_dev)
  257. {
  258. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  259. gspca_dev->vdev.ctrl_handler = hdl;
  260. v4l2_ctrl_handler_init(hdl, 2);
  261. gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
  262. V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
  263. gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
  264. V4L2_CID_GAIN, 0, 255, 1, 200);
  265. if (hdl->error) {
  266. pr_err("Could not initialize controls\n");
  267. return hdl->error;
  268. }
  269. return 0;
  270. }
  271. /* Table of supported USB devices */
  272. static const struct usb_device_id device_table[] = {
  273. {USB_DEVICE(0x04c1, 0x009d)},
  274. {USB_DEVICE(0x0602, 0x1001)},
  275. {}
  276. };
  277. MODULE_DEVICE_TABLE(usb, device_table);
  278. /* sub-driver description */
  279. static const struct sd_desc sd_desc = {
  280. .name = MODULE_NAME,
  281. .config = sd_config,
  282. .init = sd_init,
  283. .init_controls = sd_init_controls,
  284. .start = sd_start,
  285. .stop0 = sd_stop0,
  286. };
  287. /* -- device connect -- */
  288. static int sd_probe(struct usb_interface *intf,
  289. const struct usb_device_id *id)
  290. {
  291. return gspca_dev_probe(intf, id,
  292. &sd_desc,
  293. sizeof(struct sd),
  294. THIS_MODULE);
  295. }
  296. static struct usb_driver sd_driver = {
  297. .name = MODULE_NAME,
  298. .id_table = device_table,
  299. .probe = sd_probe,
  300. .disconnect = gspca_disconnect,
  301. #ifdef CONFIG_PM
  302. .suspend = gspca_suspend,
  303. .resume = gspca_resume,
  304. .reset_resume = gspca_resume,
  305. #endif
  306. };
  307. module_usb_driver(sd_driver);