vicam.c 10 KB

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