sq905.c 13 KB

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