vicam.c 10 KB

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