vicam.c 9.9 KB

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