sq905.c 12 KB

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