vicam.c 10 KB

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