sq905.c 13 KB

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