sq905.c 13 KB

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