vicam.c 10 KB

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