sq905c.c 9.6 KB

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