vicam.c 9.3 KB

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