stv0680.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * STV0680 USB Camera Driver
  3. *
  4. * Copyright (C) 2009 Hans de Goede <hdgoede@redhat.com>
  5. *
  6. * This module is adapted from the in kernel v4l1 stv680 driver:
  7. *
  8. * STV0680 USB Camera Driver, by Kevin Sisson (kjsisson@bellsouth.net)
  9. *
  10. * Thanks to STMicroelectronics for information on the usb commands, and
  11. * to Steve Miller at STM for his help and encouragement while I was
  12. * writing this driver.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. */
  29. #define MODULE_NAME "stv0680"
  30. #include "gspca.h"
  31. MODULE_AUTHOR("Hans de Goede <hdgoede@redhat.com>");
  32. MODULE_DESCRIPTION("STV0680 USB Camera Driver");
  33. MODULE_LICENSE("GPL");
  34. /* specific webcam descriptor */
  35. struct sd {
  36. struct gspca_dev gspca_dev; /* !! must be the first item */
  37. struct v4l2_pix_format mode;
  38. u8 orig_mode;
  39. u8 video_mode;
  40. u8 current_mode;
  41. };
  42. /* V4L2 controls supported by the driver */
  43. static const struct ctrl sd_ctrls[] = {
  44. };
  45. static int stv_sndctrl(struct gspca_dev *gspca_dev, int set, u8 req, u16 val,
  46. int size)
  47. {
  48. int ret = -1;
  49. u8 req_type = 0;
  50. switch (set) {
  51. case 0: /* 0xc1 */
  52. req_type = USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
  53. break;
  54. case 1: /* 0x41 */
  55. req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_ENDPOINT;
  56. break;
  57. case 2: /* 0x80 */
  58. req_type = USB_DIR_IN | USB_RECIP_DEVICE;
  59. break;
  60. case 3: /* 0x40 */
  61. req_type = USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE;
  62. break;
  63. }
  64. ret = usb_control_msg(gspca_dev->dev,
  65. usb_rcvctrlpipe(gspca_dev->dev, 0),
  66. req, req_type,
  67. val, 0, gspca_dev->usb_buf, size, 500);
  68. if ((ret < 0) && (req != 0x0a))
  69. PDEBUG(D_ERR,
  70. "usb_control_msg error %i, request = 0x%x, error = %i",
  71. set, req, ret);
  72. return ret;
  73. }
  74. static int stv0680_handle_error(struct gspca_dev *gspca_dev, int ret)
  75. {
  76. stv_sndctrl(gspca_dev, 0, 0x80, 0, 0x02); /* Get Last Error */
  77. PDEBUG(D_ERR, "last error: %i, command = 0x%x",
  78. gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
  79. return ret;
  80. }
  81. static int stv0680_get_video_mode(struct gspca_dev *gspca_dev)
  82. {
  83. /* Note not sure if this init of usb_buf is really necessary */
  84. memset(gspca_dev->usb_buf, 0, 8);
  85. gspca_dev->usb_buf[0] = 0x0f;
  86. if (stv_sndctrl(gspca_dev, 0, 0x87, 0, 0x08) != 0x08) {
  87. PDEBUG(D_ERR, "Get_Camera_Mode failed");
  88. return stv0680_handle_error(gspca_dev, -EIO);
  89. }
  90. return gspca_dev->usb_buf[0]; /* 01 = VGA, 03 = QVGA, 00 = CIF */
  91. }
  92. static int stv0680_set_video_mode(struct gspca_dev *gspca_dev, u8 mode)
  93. {
  94. struct sd *sd = (struct sd *) gspca_dev;
  95. if (sd->current_mode == mode)
  96. return 0;
  97. memset(gspca_dev->usb_buf, 0, 8);
  98. gspca_dev->usb_buf[0] = mode;
  99. if (stv_sndctrl(gspca_dev, 3, 0x07, 0x0100, 0x08) != 0x08) {
  100. PDEBUG(D_ERR, "Set_Camera_Mode failed");
  101. return stv0680_handle_error(gspca_dev, -EIO);
  102. }
  103. /* Verify we got what we've asked for */
  104. if (stv0680_get_video_mode(gspca_dev) != mode) {
  105. PDEBUG(D_ERR, "Error setting camera video mode!");
  106. return -EIO;
  107. }
  108. sd->current_mode = mode;
  109. return 0;
  110. }
  111. /* this function is called at probe time */
  112. static int sd_config(struct gspca_dev *gspca_dev,
  113. const struct usb_device_id *id)
  114. {
  115. int ret;
  116. struct sd *sd = (struct sd *) gspca_dev;
  117. struct cam *cam = &gspca_dev->cam;
  118. /* Give the camera some time to settle, otherwise initalization will
  119. fail on hotplug, and yes it really needs a full second. */
  120. msleep(1000);
  121. /* ping camera to be sure STV0680 is present */
  122. if (stv_sndctrl(gspca_dev, 0, 0x88, 0x5678, 0x02) != 0x02 ||
  123. gspca_dev->usb_buf[0] != 0x56 || gspca_dev->usb_buf[1] != 0x78) {
  124. PDEBUG(D_ERR, "STV(e): camera ping failed!!");
  125. return stv0680_handle_error(gspca_dev, -ENODEV);
  126. }
  127. /* get camera descriptor */
  128. if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x09) != 0x09)
  129. return stv0680_handle_error(gspca_dev, -ENODEV);
  130. if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0200, 0x22) != 0x22 ||
  131. gspca_dev->usb_buf[7] != 0xa0 || gspca_dev->usb_buf[8] != 0x23) {
  132. PDEBUG(D_ERR, "Could not get descriptor 0200.");
  133. return stv0680_handle_error(gspca_dev, -ENODEV);
  134. }
  135. if (stv_sndctrl(gspca_dev, 0, 0x8a, 0, 0x02) != 0x02)
  136. return stv0680_handle_error(gspca_dev, -ENODEV);
  137. if (stv_sndctrl(gspca_dev, 0, 0x8b, 0, 0x24) != 0x24)
  138. return stv0680_handle_error(gspca_dev, -ENODEV);
  139. if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
  140. return stv0680_handle_error(gspca_dev, -ENODEV);
  141. if (!(gspca_dev->usb_buf[7] & 0x09)) {
  142. PDEBUG(D_ERR, "Camera supports neither CIF nor QVGA mode");
  143. return -ENODEV;
  144. }
  145. if (gspca_dev->usb_buf[7] & 0x01)
  146. PDEBUG(D_PROBE, "Camera supports CIF mode");
  147. if (gspca_dev->usb_buf[7] & 0x02)
  148. PDEBUG(D_PROBE, "Camera supports VGA mode");
  149. if (gspca_dev->usb_buf[7] & 0x08)
  150. PDEBUG(D_PROBE, "Camera supports QVGA mode");
  151. if (gspca_dev->usb_buf[7] & 0x01)
  152. sd->video_mode = 0x00; /* CIF */
  153. else
  154. sd->video_mode = 0x03; /* QVGA */
  155. /* FW rev, ASIC rev, sensor ID */
  156. PDEBUG(D_PROBE, "Firmware rev is %i.%i",
  157. gspca_dev->usb_buf[0], gspca_dev->usb_buf[1]);
  158. PDEBUG(D_PROBE, "ASIC rev is %i.%i",
  159. gspca_dev->usb_buf[2], gspca_dev->usb_buf[3]);
  160. PDEBUG(D_PROBE, "Sensor ID is %i",
  161. (gspca_dev->usb_buf[4]*16) + (gspca_dev->usb_buf[5]>>4));
  162. ret = stv0680_get_video_mode(gspca_dev);
  163. if (ret < 0)
  164. return ret;
  165. sd->current_mode = sd->orig_mode = ret;
  166. ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
  167. if (ret < 0)
  168. return ret;
  169. /* Get mode details */
  170. if (stv_sndctrl(gspca_dev, 0, 0x8f, 0, 0x10) != 0x10)
  171. return stv0680_handle_error(gspca_dev, -EIO);
  172. cam->bulk = 1;
  173. cam->bulk_nurbs = 1; /* The cam cannot handle more */
  174. cam->bulk_size = (gspca_dev->usb_buf[0] << 24) |
  175. (gspca_dev->usb_buf[1] << 16) |
  176. (gspca_dev->usb_buf[2] << 8) |
  177. (gspca_dev->usb_buf[3]);
  178. sd->mode.width = (gspca_dev->usb_buf[4] << 8) |
  179. (gspca_dev->usb_buf[5]); /* 322, 356, 644 */
  180. sd->mode.height = (gspca_dev->usb_buf[6] << 8) |
  181. (gspca_dev->usb_buf[7]); /* 242, 292, 484 */
  182. sd->mode.pixelformat = V4L2_PIX_FMT_STV0680;
  183. sd->mode.field = V4L2_FIELD_NONE;
  184. sd->mode.bytesperline = sd->mode.width;
  185. sd->mode.sizeimage = cam->bulk_size;
  186. sd->mode.colorspace = V4L2_COLORSPACE_SRGB;
  187. /* origGain = gspca_dev->usb_buf[12]; */
  188. cam->cam_mode = &sd->mode;
  189. cam->nmodes = 1;
  190. ret = stv0680_set_video_mode(gspca_dev, sd->orig_mode);
  191. if (ret < 0)
  192. return ret;
  193. if (stv_sndctrl(gspca_dev, 2, 0x06, 0x0100, 0x12) != 0x12 ||
  194. gspca_dev->usb_buf[8] != 0x53 || gspca_dev->usb_buf[9] != 0x05) {
  195. PDEBUG(D_ERR, "Could not get descriptor 0100.");
  196. return stv0680_handle_error(gspca_dev, -EIO);
  197. }
  198. return 0;
  199. }
  200. /* this function is called at probe and resume time */
  201. static int sd_init(struct gspca_dev *gspca_dev)
  202. {
  203. return 0;
  204. }
  205. /* -- start the camera -- */
  206. static int sd_start(struct gspca_dev *gspca_dev)
  207. {
  208. int ret;
  209. struct sd *sd = (struct sd *) gspca_dev;
  210. ret = stv0680_set_video_mode(gspca_dev, sd->video_mode);
  211. if (ret < 0)
  212. return ret;
  213. if (stv_sndctrl(gspca_dev, 0, 0x85, 0, 0x10) != 0x10)
  214. return stv0680_handle_error(gspca_dev, -EIO);
  215. /* Start stream at:
  216. 0x0000 = CIF (352x288)
  217. 0x0100 = VGA (640x480)
  218. 0x0300 = QVGA (320x240) */
  219. if (stv_sndctrl(gspca_dev, 1, 0x09, sd->video_mode << 8, 0x0) != 0x0)
  220. return stv0680_handle_error(gspca_dev, -EIO);
  221. return 0;
  222. }
  223. static void sd_stopN(struct gspca_dev *gspca_dev)
  224. {
  225. /* This is a high priority command; it stops all lower order cmds */
  226. if (stv_sndctrl(gspca_dev, 1, 0x04, 0x0000, 0x0) != 0x0)
  227. stv0680_handle_error(gspca_dev, -EIO);
  228. }
  229. static void sd_stop0(struct gspca_dev *gspca_dev)
  230. {
  231. struct sd *sd = (struct sd *) gspca_dev;
  232. if (!sd->gspca_dev.present)
  233. return;
  234. stv0680_set_video_mode(gspca_dev, sd->orig_mode);
  235. }
  236. static void sd_pkt_scan(struct gspca_dev *gspca_dev,
  237. u8 *data,
  238. int len)
  239. {
  240. struct sd *sd = (struct sd *) gspca_dev;
  241. /* Every now and then the camera sends a 16 byte packet, no idea
  242. what it contains, but it is not image data, when this
  243. happens the frame received before this packet is corrupt,
  244. so discard it. */
  245. if (len != sd->mode.sizeimage) {
  246. gspca_dev->last_packet_type = DISCARD_PACKET;
  247. return;
  248. }
  249. /* Finish the previous frame, we do this upon reception of the next
  250. packet, even though it is already complete so that the strange 16
  251. byte packets send after a corrupt frame can discard it. */
  252. gspca_frame_add(gspca_dev, LAST_PACKET, NULL, 0);
  253. /* Store the just received frame */
  254. gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
  255. }
  256. /* sub-driver description */
  257. static const struct sd_desc sd_desc = {
  258. .name = MODULE_NAME,
  259. .ctrls = sd_ctrls,
  260. .nctrls = ARRAY_SIZE(sd_ctrls),
  261. .config = sd_config,
  262. .init = sd_init,
  263. .start = sd_start,
  264. .stopN = sd_stopN,
  265. .stop0 = sd_stop0,
  266. .pkt_scan = sd_pkt_scan,
  267. };
  268. /* -- module initialisation -- */
  269. static const __devinitdata struct usb_device_id device_table[] = {
  270. {USB_DEVICE(0x0553, 0x0202)},
  271. {USB_DEVICE(0x041e, 0x4007)},
  272. {}
  273. };
  274. MODULE_DEVICE_TABLE(usb, device_table);
  275. /* -- device connect -- */
  276. static int sd_probe(struct usb_interface *intf,
  277. const struct usb_device_id *id)
  278. {
  279. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  280. THIS_MODULE);
  281. }
  282. static struct usb_driver sd_driver = {
  283. .name = MODULE_NAME,
  284. .id_table = device_table,
  285. .probe = sd_probe,
  286. .disconnect = gspca_disconnect,
  287. #ifdef CONFIG_PM
  288. .suspend = gspca_suspend,
  289. .resume = gspca_resume,
  290. #endif
  291. };
  292. /* -- module insert / remove -- */
  293. static int __init sd_mod_init(void)
  294. {
  295. int ret;
  296. ret = usb_register(&sd_driver);
  297. if (ret < 0)
  298. return ret;
  299. PDEBUG(D_PROBE, "registered");
  300. return 0;
  301. }
  302. static void __exit sd_mod_exit(void)
  303. {
  304. usb_deregister(&sd_driver);
  305. PDEBUG(D_PROBE, "deregistered");
  306. }
  307. module_init(sd_mod_init);
  308. module_exit(sd_mod_exit);