sq905c.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * SQ905C subdriver
  3. *
  4. * Copyright (C) 2009 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. *
  22. * This driver uses work done in
  23. * libgphoto2/camlibs/digigr8, Copyright (C) Theodore Kilgore.
  24. *
  25. * This driver has also used as a base the sq905c driver
  26. * and may contain code fragments from it.
  27. */
  28. #define MODULE_NAME "sq905c"
  29. #include <linux/workqueue.h>
  30. #include "gspca.h"
  31. MODULE_AUTHOR("Theodore Kilgore <kilgota@auburn.edu>");
  32. MODULE_DESCRIPTION("GSPCA/SQ905C USB Camera Driver");
  33. MODULE_LICENSE("GPL");
  34. /* Default timeouts, in ms */
  35. #define SQ905C_CMD_TIMEOUT 500
  36. #define SQ905C_DATA_TIMEOUT 1000
  37. /* Maximum transfer size to use. */
  38. #define SQ905C_MAX_TRANSFER 0x8000
  39. #define FRAME_HEADER_LEN 0x50
  40. /* Commands. These go in the "value" slot. */
  41. #define SQ905C_CLEAR 0xa0 /* clear everything */
  42. #define SQ905C_CAPTURE_LOW 0xa040 /* Starts capture at 160x120 */
  43. #define SQ905C_CAPTURE_MED 0x1440 /* Starts capture at 320x240 */
  44. #define SQ905C_CAPTURE_HI 0x2840 /* Starts capture at 320x240 */
  45. /* For capture, this must go in the "index" slot. */
  46. #define SQ905C_CAPTURE_INDEX 0x110f
  47. /* Structure to hold all of our device specific stuff */
  48. struct sd {
  49. struct gspca_dev gspca_dev; /* !! must be the first item */
  50. const struct v4l2_pix_format *cap_mode;
  51. /* Driver stuff */
  52. struct work_struct work_struct;
  53. struct workqueue_struct *work_thread;
  54. };
  55. /*
  56. * Most of these cameras will do 640x480 and 320x240. 160x120 works
  57. * in theory but gives very poor output. Therefore, not supported.
  58. * The 0x2770:0x9050 cameras have max resolution of 320x240.
  59. */
  60. static struct v4l2_pix_format sq905c_mode[] = {
  61. { 320, 240, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
  62. .bytesperline = 320,
  63. .sizeimage = 320 * 240,
  64. .colorspace = V4L2_COLORSPACE_SRGB,
  65. .priv = 0},
  66. { 640, 480, V4L2_PIX_FMT_SQ905C, V4L2_FIELD_NONE,
  67. .bytesperline = 640,
  68. .sizeimage = 640 * 480,
  69. .colorspace = V4L2_COLORSPACE_SRGB,
  70. .priv = 0}
  71. };
  72. /* Send a command to the camera. */
  73. static int sq905c_command(struct gspca_dev *gspca_dev, u16 command, u16 index)
  74. {
  75. int ret;
  76. ret = usb_control_msg(gspca_dev->dev,
  77. usb_sndctrlpipe(gspca_dev->dev, 0),
  78. USB_REQ_SYNCH_FRAME, /* request */
  79. USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  80. command, index, NULL, 0,
  81. SQ905C_CMD_TIMEOUT);
  82. if (ret < 0) {
  83. PDEBUG(D_ERR, "%s: usb_control_msg failed (%d)",
  84. __func__, ret);
  85. return ret;
  86. }
  87. return 0;
  88. }
  89. /* This function is called as a workqueue function and runs whenever the camera
  90. * is streaming data. Because it is a workqueue function it is allowed to sleep
  91. * so we can use synchronous USB calls. To avoid possible collisions with other
  92. * threads attempting to use the camera's USB interface the gspca usb_lock is
  93. * used when performing the one USB control operation inside the workqueue,
  94. * which tells the camera to close the stream. In practice the only thing
  95. * which needs to be protected against is the usb_set_interface call that
  96. * gspca makes during stream_off. Otherwise the camera doesn't provide any
  97. * controls that the user could try to change.
  98. */
  99. static void sq905c_dostream(struct work_struct *work)
  100. {
  101. struct sd *dev = container_of(work, struct sd, work_struct);
  102. struct gspca_dev *gspca_dev = &dev->gspca_dev;
  103. struct gspca_frame *frame;
  104. int bytes_left; /* bytes remaining in current frame. */
  105. int data_len; /* size to use for the next read. */
  106. int act_len;
  107. int discarding = 0; /* true if we failed to get space for frame. */
  108. int packet_type;
  109. int ret;
  110. u8 *buffer;
  111. buffer = kmalloc(SQ905C_MAX_TRANSFER, GFP_KERNEL | GFP_DMA);
  112. if (!buffer) {
  113. PDEBUG(D_ERR, "Couldn't allocate USB buffer");
  114. goto quit_stream;
  115. }
  116. while (gspca_dev->present && gspca_dev->streaming) {
  117. if (!gspca_dev->present)
  118. goto quit_stream;
  119. /* Request the header, which tells the size to download */
  120. ret = usb_bulk_msg(gspca_dev->dev,
  121. usb_rcvbulkpipe(gspca_dev->dev, 0x81),
  122. buffer, FRAME_HEADER_LEN, &act_len,
  123. SQ905C_DATA_TIMEOUT);
  124. PDEBUG(D_STREAM,
  125. "Got %d bytes out of %d for header",
  126. act_len, FRAME_HEADER_LEN);
  127. if (ret < 0 || act_len < FRAME_HEADER_LEN)
  128. goto quit_stream;
  129. /* size is read from 4 bytes starting 0x40, little endian */
  130. bytes_left = buffer[0x40]|(buffer[0x41]<<8)|(buffer[0x42]<<16)
  131. |(buffer[0x43]<<24);
  132. PDEBUG(D_STREAM, "bytes_left = 0x%x", bytes_left);
  133. /* We keep the header. It has other information, too. */
  134. packet_type = FIRST_PACKET;
  135. frame = gspca_get_i_frame(gspca_dev);
  136. if (frame && !discarding) {
  137. gspca_frame_add(gspca_dev, packet_type,
  138. frame, buffer, FRAME_HEADER_LEN);
  139. } else
  140. discarding = 1;
  141. while (bytes_left > 0) {
  142. data_len = bytes_left > SQ905C_MAX_TRANSFER ?
  143. SQ905C_MAX_TRANSFER : bytes_left;
  144. if (!gspca_dev->present)
  145. goto quit_stream;
  146. ret = usb_bulk_msg(gspca_dev->dev,
  147. usb_rcvbulkpipe(gspca_dev->dev, 0x81),
  148. buffer, data_len, &act_len,
  149. SQ905C_DATA_TIMEOUT);
  150. if (ret < 0 || act_len < data_len)
  151. goto quit_stream;
  152. PDEBUG(D_STREAM,
  153. "Got %d bytes out of %d for frame",
  154. data_len, bytes_left);
  155. bytes_left -= data_len;
  156. if (bytes_left == 0)
  157. packet_type = LAST_PACKET;
  158. else
  159. packet_type = INTER_PACKET;
  160. frame = gspca_get_i_frame(gspca_dev);
  161. if (frame && !discarding)
  162. gspca_frame_add(gspca_dev, packet_type,
  163. frame, buffer, data_len);
  164. else
  165. discarding = 1;
  166. }
  167. }
  168. quit_stream:
  169. mutex_lock(&gspca_dev->usb_lock);
  170. if (gspca_dev->present)
  171. sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
  172. mutex_unlock(&gspca_dev->usb_lock);
  173. kfree(buffer);
  174. }
  175. /* This function is called at probe time just before sd_init */
  176. static int sd_config(struct gspca_dev *gspca_dev,
  177. const struct usb_device_id *id)
  178. {
  179. struct cam *cam = &gspca_dev->cam;
  180. struct sd *dev = (struct sd *) gspca_dev;
  181. PDEBUG(D_PROBE,
  182. "SQ9050 camera detected"
  183. " (vid/pid 0x%04X:0x%04X)", id->idVendor, id->idProduct);
  184. cam->cam_mode = sq905c_mode;
  185. cam->nmodes = 2;
  186. if (id->idProduct == 0x9050)
  187. cam->nmodes = 1;
  188. /* We don't use the buffer gspca allocates so make it small. */
  189. cam->bulk_size = 32;
  190. cam->bulk = 1;
  191. INIT_WORK(&dev->work_struct, sq905c_dostream);
  192. return 0;
  193. }
  194. /* called on streamoff with alt==0 and on disconnect */
  195. /* the usb_lock is held at entry - restore on exit */
  196. static void sd_stop0(struct gspca_dev *gspca_dev)
  197. {
  198. struct sd *dev = (struct sd *) gspca_dev;
  199. /* wait for the work queue to terminate */
  200. mutex_unlock(&gspca_dev->usb_lock);
  201. /* This waits for sq905c_dostream to finish */
  202. destroy_workqueue(dev->work_thread);
  203. dev->work_thread = NULL;
  204. mutex_lock(&gspca_dev->usb_lock);
  205. }
  206. /* this function is called at probe and resume time */
  207. static int sd_init(struct gspca_dev *gspca_dev)
  208. {
  209. int ret;
  210. /* connect to the camera and reset it. */
  211. ret = sq905c_command(gspca_dev, SQ905C_CLEAR, 0);
  212. return ret;
  213. }
  214. /* Set up for getting frames. */
  215. static int sd_start(struct gspca_dev *gspca_dev)
  216. {
  217. struct sd *dev = (struct sd *) gspca_dev;
  218. int ret;
  219. dev->cap_mode = gspca_dev->cam.cam_mode;
  220. /* "Open the shutter" and set size, to start capture */
  221. switch (gspca_dev->width) {
  222. case 640:
  223. PDEBUG(D_STREAM, "Start streaming at high resolution");
  224. dev->cap_mode++;
  225. ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_HI,
  226. SQ905C_CAPTURE_INDEX);
  227. break;
  228. default: /* 320 */
  229. PDEBUG(D_STREAM, "Start streaming at medium resolution");
  230. ret = sq905c_command(gspca_dev, SQ905C_CAPTURE_MED,
  231. SQ905C_CAPTURE_INDEX);
  232. }
  233. if (ret < 0) {
  234. PDEBUG(D_ERR, "Start streaming command failed");
  235. return ret;
  236. }
  237. /* Start the workqueue function to do the streaming */
  238. dev->work_thread = create_singlethread_workqueue(MODULE_NAME);
  239. queue_work(dev->work_thread, &dev->work_struct);
  240. return 0;
  241. }
  242. /* Table of supported USB devices */
  243. static const __devinitdata struct usb_device_id device_table[] = {
  244. {USB_DEVICE(0x2770, 0x905c)},
  245. {USB_DEVICE(0x2770, 0x9050)},
  246. {USB_DEVICE(0x2770, 0x913d)},
  247. {}
  248. };
  249. MODULE_DEVICE_TABLE(usb, device_table);
  250. /* sub-driver description */
  251. static const struct sd_desc sd_desc = {
  252. .name = MODULE_NAME,
  253. .config = sd_config,
  254. .init = sd_init,
  255. .start = sd_start,
  256. .stop0 = sd_stop0,
  257. };
  258. /* -- device connect -- */
  259. static int sd_probe(struct usb_interface *intf,
  260. const struct usb_device_id *id)
  261. {
  262. return gspca_dev_probe(intf, id,
  263. &sd_desc,
  264. sizeof(struct sd),
  265. THIS_MODULE);
  266. }
  267. static struct usb_driver sd_driver = {
  268. .name = MODULE_NAME,
  269. .id_table = device_table,
  270. .probe = sd_probe,
  271. .disconnect = gspca_disconnect,
  272. #ifdef CONFIG_PM
  273. .suspend = gspca_suspend,
  274. .resume = gspca_resume,
  275. #endif
  276. };
  277. /* -- module insert / remove -- */
  278. static int __init sd_mod_init(void)
  279. {
  280. int ret;
  281. ret = usb_register(&sd_driver);
  282. if (ret < 0)
  283. return ret;
  284. PDEBUG(D_PROBE, "registered");
  285. return 0;
  286. }
  287. static void __exit sd_mod_exit(void)
  288. {
  289. usb_deregister(&sd_driver);
  290. PDEBUG(D_PROBE, "deregistered");
  291. }
  292. module_init(sd_mod_init);
  293. module_exit(sd_mod_exit);