vicam.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * gspca ViCam subdriver
  4. *
  5. * Copyright (C) 2011 Hans de Goede <[email protected]>
  6. *
  7. * Based on the usbvideo vicam driver, which is:
  8. *
  9. * Copyright (c) 2002 Joe Burks ([email protected]),
  10. * Chris Cheney ([email protected]),
  11. * Pavel Machek ([email protected]),
  12. * John Tyner ([email protected]),
  13. * Monroe Williams ([email protected])
  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 <[email protected]>");
  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. ret = request_ihex_firmware(&fw, VICAM_FIRMWARE,
  203. &gspca_dev->dev->dev);
  204. if (ret) {
  205. pr_err("Failed to load \"vicam/firmware.fw\": %d\n", ret);
  206. return ret;
  207. }
  208. firmware_buf = kmalloc(PAGE_SIZE, GFP_KERNEL);
  209. if (!firmware_buf) {
  210. ret = -ENOMEM;
  211. goto exit;
  212. }
  213. for (rec = (void *)fw->data; rec; rec = ihex_next_binrec(rec)) {
  214. memcpy(firmware_buf, rec->data, be16_to_cpu(rec->len));
  215. ret = vicam_control_msg(gspca_dev, 0xff, 0, 0, firmware_buf,
  216. be16_to_cpu(rec->len));
  217. if (ret < 0)
  218. break;
  219. }
  220. kfree(firmware_buf);
  221. exit:
  222. release_firmware(fw);
  223. return ret;
  224. }
  225. /* Set up for getting frames. */
  226. static int sd_start(struct gspca_dev *gspca_dev)
  227. {
  228. struct sd *sd = (struct sd *)gspca_dev;
  229. int ret;
  230. ret = vicam_set_camera_power(gspca_dev, 1);
  231. if (ret < 0)
  232. return ret;
  233. schedule_work(&sd->work_struct);
  234. return 0;
  235. }
  236. /* called on streamoff with alt==0 and on disconnect */
  237. /* the usb_lock is held at entry - restore on exit */
  238. static void sd_stop0(struct gspca_dev *gspca_dev)
  239. {
  240. struct sd *dev = (struct sd *)gspca_dev;
  241. /* wait for the work queue to terminate */
  242. mutex_unlock(&gspca_dev->usb_lock);
  243. /* This waits for vicam_dostream to finish */
  244. flush_work(&dev->work_struct);
  245. mutex_lock(&gspca_dev->usb_lock);
  246. if (gspca_dev->present)
  247. vicam_set_camera_power(gspca_dev, 0);
  248. }
  249. static int sd_init_controls(struct gspca_dev *gspca_dev)
  250. {
  251. struct v4l2_ctrl_handler *hdl = &gspca_dev->ctrl_handler;
  252. gspca_dev->vdev.ctrl_handler = hdl;
  253. v4l2_ctrl_handler_init(hdl, 2);
  254. gspca_dev->exposure = v4l2_ctrl_new_std(hdl, NULL,
  255. V4L2_CID_EXPOSURE, 0, 2047, 1, 256);
  256. gspca_dev->gain = v4l2_ctrl_new_std(hdl, NULL,
  257. V4L2_CID_GAIN, 0, 255, 1, 200);
  258. if (hdl->error) {
  259. pr_err("Could not initialize controls\n");
  260. return hdl->error;
  261. }
  262. return 0;
  263. }
  264. /* Table of supported USB devices */
  265. static const struct usb_device_id device_table[] = {
  266. {USB_DEVICE(0x04c1, 0x009d)},
  267. {USB_DEVICE(0x0602, 0x1001)},
  268. {}
  269. };
  270. MODULE_DEVICE_TABLE(usb, device_table);
  271. /* sub-driver description */
  272. static const struct sd_desc sd_desc = {
  273. .name = MODULE_NAME,
  274. .config = sd_config,
  275. .init = sd_init,
  276. .init_controls = sd_init_controls,
  277. .start = sd_start,
  278. .stop0 = sd_stop0,
  279. };
  280. /* -- device connect -- */
  281. static int sd_probe(struct usb_interface *intf,
  282. const struct usb_device_id *id)
  283. {
  284. return gspca_dev_probe(intf, id,
  285. &sd_desc,
  286. sizeof(struct sd),
  287. THIS_MODULE);
  288. }
  289. static struct usb_driver sd_driver = {
  290. .name = MODULE_NAME,
  291. .id_table = device_table,
  292. .probe = sd_probe,
  293. .disconnect = gspca_disconnect,
  294. #ifdef CONFIG_PM
  295. .suspend = gspca_suspend,
  296. .resume = gspca_resume,
  297. .reset_resume = gspca_resume,
  298. #endif
  299. };
  300. module_usb_driver(sd_driver);