sq905c.c 9.5 KB

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