sq905.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * SQ905 subdriver
  4. *
  5. * Copyright (C) 2008, 2009 Adam Baker and Theodore Kilgore
  6. */
  7. /*
  8. * History and Acknowledgments
  9. *
  10. * The original Linux driver for SQ905 based cameras was written by
  11. * Marcell Lengyel and further developed by many other contributors
  12. * and is available from http://sourceforge.net/projects/sqcam/
  13. *
  14. * This driver takes advantage of the reverse engineering work done for
  15. * that driver and for libgphoto2 but shares no code with them.
  16. *
  17. * This driver has used as a base the finepix driver and other gspca
  18. * based drivers and may still contain code fragments taken from those
  19. * drivers.
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #define MODULE_NAME "sq905"
  23. #include <linux/workqueue.h>
  24. #include <linux/slab.h>
  25. #include "gspca.h"
  26. MODULE_AUTHOR("Adam Baker <[email protected]>, Theodore Kilgore <[email protected]>");
  27. MODULE_DESCRIPTION("GSPCA/SQ905 USB Camera Driver");
  28. MODULE_LICENSE("GPL");
  29. /* Default timeouts, in ms */
  30. #define SQ905_CMD_TIMEOUT 500
  31. #define SQ905_DATA_TIMEOUT 1000
  32. /* Maximum transfer size to use. */
  33. #define SQ905_MAX_TRANSFER 0x8000
  34. #define FRAME_HEADER_LEN 64
  35. /* The known modes, or registers. These go in the "value" slot. */
  36. /* 00 is "none" obviously */
  37. #define SQ905_BULK_READ 0x03 /* precedes any bulk read */
  38. #define SQ905_COMMAND 0x06 /* precedes the command codes below */
  39. #define SQ905_PING 0x07 /* when reading an "idling" command */
  40. #define SQ905_READ_DONE 0xc0 /* ack bulk read completed */
  41. /* Any non-zero value in the bottom 2 bits of the 2nd byte of
  42. * the ID appears to indicate the camera can do 640*480. If the
  43. * LSB of that byte is set the image is just upside down, otherwise
  44. * it is rotated 180 degrees. */
  45. #define SQ905_HIRES_MASK 0x00000300
  46. #define SQ905_ORIENTATION_MASK 0x00000100
  47. /* Some command codes. These go in the "index" slot. */
  48. #define SQ905_ID 0xf0 /* asks for model string */
  49. #define SQ905_CONFIG 0x20 /* gets photo alloc. table, not used here */
  50. #define SQ905_DATA 0x30 /* accesses photo data, not used here */
  51. #define SQ905_CLEAR 0xa0 /* clear everything */
  52. #define SQ905_CAPTURE_LOW 0x60 /* Starts capture at 160x120 */
  53. #define SQ905_CAPTURE_MED 0x61 /* Starts capture at 320x240 */
  54. #define SQ905_CAPTURE_HIGH 0x62 /* Starts capture at 640x480 (some cams only) */
  55. /* note that the capture command also controls the output dimensions */
  56. /* Structure to hold all of our device specific stuff */
  57. struct sd {
  58. struct gspca_dev gspca_dev; /* !! must be the first item */
  59. /*
  60. * Driver stuff
  61. */
  62. struct work_struct work_struct;
  63. struct workqueue_struct *work_thread;
  64. };
  65. static struct v4l2_pix_format sq905_mode[] = {
  66. { 160, 120, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  67. .bytesperline = 160,
  68. .sizeimage = 160 * 120,
  69. .colorspace = V4L2_COLORSPACE_SRGB,
  70. .priv = 0},
  71. { 320, 240, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  72. .bytesperline = 320,
  73. .sizeimage = 320 * 240,
  74. .colorspace = V4L2_COLORSPACE_SRGB,
  75. .priv = 0},
  76. { 640, 480, V4L2_PIX_FMT_SBGGR8, V4L2_FIELD_NONE,
  77. .bytesperline = 640,
  78. .sizeimage = 640 * 480,
  79. .colorspace = V4L2_COLORSPACE_SRGB,
  80. .priv = 0}
  81. };
  82. /*
  83. * Send a command to the camera.
  84. */
  85. static int sq905_command(struct gspca_dev *gspca_dev, u16 index)
  86. {
  87. int ret;
  88. gspca_dev->usb_buf[0] = '\0';
  89. ret = usb_control_msg(gspca_dev->dev,
  90. usb_sndctrlpipe(gspca_dev->dev, 0),
  91. USB_REQ_SYNCH_FRAME, /* request */
  92. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  93. SQ905_COMMAND, index, gspca_dev->usb_buf, 1,
  94. SQ905_CMD_TIMEOUT);
  95. if (ret < 0) {
  96. pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
  97. return ret;
  98. }
  99. ret = usb_control_msg(gspca_dev->dev,
  100. usb_rcvctrlpipe(gspca_dev->dev, 0),
  101. USB_REQ_SYNCH_FRAME, /* request */
  102. USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  103. SQ905_PING, 0, gspca_dev->usb_buf, 1,
  104. SQ905_CMD_TIMEOUT);
  105. if (ret < 0) {
  106. pr_err("%s: usb_control_msg failed 2 (%d)\n", __func__, ret);
  107. return ret;
  108. }
  109. return 0;
  110. }
  111. /*
  112. * Acknowledge the end of a frame - see warning on sq905_command.
  113. */
  114. static int sq905_ack_frame(struct gspca_dev *gspca_dev)
  115. {
  116. int ret;
  117. gspca_dev->usb_buf[0] = '\0';
  118. ret = usb_control_msg(gspca_dev->dev,
  119. usb_sndctrlpipe(gspca_dev->dev, 0),
  120. USB_REQ_SYNCH_FRAME, /* request */
  121. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  122. SQ905_READ_DONE, 0, gspca_dev->usb_buf, 1,
  123. SQ905_CMD_TIMEOUT);
  124. if (ret < 0) {
  125. pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
  126. return ret;
  127. }
  128. return 0;
  129. }
  130. /*
  131. * request and read a block of data - see warning on sq905_command.
  132. */
  133. static int
  134. sq905_read_data(struct gspca_dev *gspca_dev, u8 *data, int size, int need_lock)
  135. {
  136. int ret;
  137. int act_len = 0;
  138. gspca_dev->usb_buf[0] = '\0';
  139. if (need_lock)
  140. mutex_lock(&gspca_dev->usb_lock);
  141. ret = usb_control_msg(gspca_dev->dev,
  142. usb_sndctrlpipe(gspca_dev->dev, 0),
  143. USB_REQ_SYNCH_FRAME, /* request */
  144. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  145. SQ905_BULK_READ, size, gspca_dev->usb_buf,
  146. 1, SQ905_CMD_TIMEOUT);
  147. if (need_lock)
  148. mutex_unlock(&gspca_dev->usb_lock);
  149. if (ret < 0) {
  150. pr_err("%s: usb_control_msg failed (%d)\n", __func__, ret);
  151. return ret;
  152. }
  153. ret = usb_bulk_msg(gspca_dev->dev,
  154. usb_rcvbulkpipe(gspca_dev->dev, 0x81),
  155. data, size, &act_len, SQ905_DATA_TIMEOUT);
  156. /* successful, it returns 0, otherwise negative */
  157. if (ret < 0 || act_len != size) {
  158. pr_err("bulk read fail (%d) len %d/%d\n", ret, act_len, size);
  159. return -EIO;
  160. }
  161. return 0;
  162. }
  163. /*
  164. * This function is called as a workqueue function and runs whenever the camera
  165. * is streaming data. Because it is a workqueue function it is allowed to sleep
  166. * so we can use synchronous USB calls. To avoid possible collisions with other
  167. * threads attempting to use gspca_dev->usb_buf we take the usb_lock when
  168. * performing USB operations using it. In practice we don't really need this
  169. * as the camera doesn't provide any controls.
  170. */
  171. static void sq905_dostream(struct work_struct *work)
  172. {
  173. struct sd *dev = container_of(work, struct sd, work_struct);
  174. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  175. int bytes_left; /* bytes remaining in current frame. */
  176. int data_len; /* size to use for the next read. */
  177. int header_read; /* true if we have already read the frame header. */
  178. int packet_type;
  179. int frame_sz;
  180. int ret;
  181. u8 *data;
  182. u8 *buffer;
  183. buffer = kmalloc(SQ905_MAX_TRANSFER, GFP_KERNEL);
  184. if (!buffer) {
  185. pr_err("Couldn't allocate USB buffer\n");
  186. goto quit_stream;
  187. }
  188. frame_sz = gspca_dev->cam.cam_mode[gspca_dev->curr_mode].sizeimage
  189. + FRAME_HEADER_LEN;
  190. while (gspca_dev->present && gspca_dev->streaming) {
  191. #ifdef CONFIG_PM
  192. if (gspca_dev->frozen)
  193. break;
  194. #endif
  195. /* request some data and then read it until we have
  196. * a complete frame. */
  197. bytes_left = frame_sz;
  198. header_read = 0;
  199. /* Note we do not check for gspca_dev->streaming here, as
  200. we must finish reading an entire frame, otherwise the
  201. next time we stream we start reading in the middle of a
  202. frame. */
  203. while (bytes_left > 0 && gspca_dev->present) {
  204. data_len = bytes_left > SQ905_MAX_TRANSFER ?
  205. SQ905_MAX_TRANSFER : bytes_left;
  206. ret = sq905_read_data(gspca_dev, buffer, data_len, 1);
  207. if (ret < 0)
  208. goto quit_stream;
  209. gspca_dbg(gspca_dev, D_PACK,
  210. "Got %d bytes out of %d for frame\n",
  211. data_len, bytes_left);
  212. bytes_left -= data_len;
  213. data = buffer;
  214. if (!header_read) {
  215. packet_type = FIRST_PACKET;
  216. /* The first 64 bytes of each frame are
  217. * a header full of FF 00 bytes */
  218. data += FRAME_HEADER_LEN;
  219. data_len -= FRAME_HEADER_LEN;
  220. header_read = 1;
  221. } else if (bytes_left == 0) {
  222. packet_type = LAST_PACKET;
  223. } else {
  224. packet_type = INTER_PACKET;
  225. }
  226. gspca_frame_add(gspca_dev, packet_type,
  227. data, data_len);
  228. /* If entire frame fits in one packet we still
  229. need to add a LAST_PACKET */
  230. if (packet_type == FIRST_PACKET &&
  231. bytes_left == 0)
  232. gspca_frame_add(gspca_dev, LAST_PACKET,
  233. NULL, 0);
  234. }
  235. if (gspca_dev->present) {
  236. /* acknowledge the frame */
  237. mutex_lock(&gspca_dev->usb_lock);
  238. ret = sq905_ack_frame(gspca_dev);
  239. mutex_unlock(&gspca_dev->usb_lock);
  240. if (ret < 0)
  241. goto quit_stream;
  242. }
  243. }
  244. quit_stream:
  245. if (gspca_dev->present) {
  246. mutex_lock(&gspca_dev->usb_lock);
  247. sq905_command(gspca_dev, SQ905_CLEAR);
  248. mutex_unlock(&gspca_dev->usb_lock);
  249. }
  250. kfree(buffer);
  251. }
  252. /* This function is called at probe time just before sd_init */
  253. static int sd_config(struct gspca_dev *gspca_dev,
  254. const struct usb_device_id *id)
  255. {
  256. struct cam *cam = &gspca_dev->cam;
  257. struct sd *dev = (struct sd *) gspca_dev;
  258. /* We don't use the buffer gspca allocates so make it small. */
  259. cam->bulk = 1;
  260. cam->bulk_size = 64;
  261. INIT_WORK(&dev->work_struct, sq905_dostream);
  262. return 0;
  263. }
  264. /* called on streamoff with alt==0 and on disconnect */
  265. /* the usb_lock is held at entry - restore on exit */
  266. static void sd_stop0(struct gspca_dev *gspca_dev)
  267. {
  268. struct sd *dev = (struct sd *) gspca_dev;
  269. /* wait for the work queue to terminate */
  270. mutex_unlock(&gspca_dev->usb_lock);
  271. /* This waits for sq905_dostream to finish */
  272. destroy_workqueue(dev->work_thread);
  273. dev->work_thread = NULL;
  274. mutex_lock(&gspca_dev->usb_lock);
  275. }
  276. /* this function is called at probe and resume time */
  277. static int sd_init(struct gspca_dev *gspca_dev)
  278. {
  279. u32 ident;
  280. int ret;
  281. /* connect to the camera and read
  282. * the model ID and process that and put it away.
  283. */
  284. ret = sq905_command(gspca_dev, SQ905_CLEAR);
  285. if (ret < 0)
  286. return ret;
  287. ret = sq905_command(gspca_dev, SQ905_ID);
  288. if (ret < 0)
  289. return ret;
  290. ret = sq905_read_data(gspca_dev, gspca_dev->usb_buf, 4, 0);
  291. if (ret < 0)
  292. return ret;
  293. /* usb_buf is allocated with kmalloc so is aligned.
  294. * Camera model number is the right way round if we assume this
  295. * reverse engineered ID is supposed to be big endian. */
  296. ident = be32_to_cpup((__be32 *)gspca_dev->usb_buf);
  297. ret = sq905_command(gspca_dev, SQ905_CLEAR);
  298. if (ret < 0)
  299. return ret;
  300. gspca_dbg(gspca_dev, D_CONF, "SQ905 camera ID %08x detected\n", ident);
  301. gspca_dev->cam.cam_mode = sq905_mode;
  302. gspca_dev->cam.nmodes = ARRAY_SIZE(sq905_mode);
  303. if (!(ident & SQ905_HIRES_MASK))
  304. gspca_dev->cam.nmodes--;
  305. if (ident & SQ905_ORIENTATION_MASK)
  306. gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP;
  307. else
  308. gspca_dev->cam.input_flags = V4L2_IN_ST_VFLIP |
  309. V4L2_IN_ST_HFLIP;
  310. return 0;
  311. }
  312. /* Set up for getting frames. */
  313. static int sd_start(struct gspca_dev *gspca_dev)
  314. {
  315. struct sd *dev = (struct sd *) gspca_dev;
  316. int ret;
  317. /* "Open the shutter" and set size, to start capture */
  318. switch (gspca_dev->curr_mode) {
  319. default:
  320. /* case 2: */
  321. gspca_dbg(gspca_dev, D_STREAM, "Start streaming at high resolution\n");
  322. ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_HIGH);
  323. break;
  324. case 1:
  325. gspca_dbg(gspca_dev, D_STREAM, "Start streaming at medium resolution\n");
  326. ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_MED);
  327. break;
  328. case 0:
  329. gspca_dbg(gspca_dev, D_STREAM, "Start streaming at low resolution\n");
  330. ret = sq905_command(&dev->gspca_dev, SQ905_CAPTURE_LOW);
  331. }
  332. if (ret < 0) {
  333. gspca_err(gspca_dev, "Start streaming command failed\n");
  334. return ret;
  335. }
  336. /* Start the workqueue function to do the streaming */
  337. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  338. if (!dev->work_thread)
  339. return -ENOMEM;
  340. queue_work(dev->work_thread, &dev->work_struct);
  341. return 0;
  342. }
  343. /* Table of supported USB devices */
  344. static const struct usb_device_id device_table[] = {
  345. {USB_DEVICE(0x2770, 0x9120)},
  346. {}
  347. };
  348. MODULE_DEVICE_TABLE(usb, device_table);
  349. /* sub-driver description */
  350. static const struct sd_desc sd_desc = {
  351. .name = MODULE_NAME,
  352. .config = sd_config,
  353. .init = sd_init,
  354. .start = sd_start,
  355. .stop0 = sd_stop0,
  356. };
  357. /* -- device connect -- */
  358. static int sd_probe(struct usb_interface *intf,
  359. const struct usb_device_id *id)
  360. {
  361. return gspca_dev_probe(intf, id,
  362. &sd_desc,
  363. sizeof(struct sd),
  364. THIS_MODULE);
  365. }
  366. static struct usb_driver sd_driver = {
  367. .name = MODULE_NAME,
  368. .id_table = device_table,
  369. .probe = sd_probe,
  370. .disconnect = gspca_disconnect,
  371. #ifdef CONFIG_PM
  372. .suspend = gspca_suspend,
  373. .resume = gspca_resume,
  374. .reset_resume = gspca_resume,
  375. #endif
  376. };
  377. module_usb_driver(sd_driver);