stv06xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright (c) 2001 Jean-Fredric Clere, Nikolas Zimmermann, Georg Acher
  3. * Mark Cave-Ayland, Carlo E Prelz, Dick Streefland
  4. * Copyright (c) 2002, 2003 Tuukka Toivonen
  5. * Copyright (c) 2008 Erik Andrén
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * P/N 861037: Sensor HDCS1000 ASIC STV0600
  22. * P/N 861050-0010: Sensor HDCS1000 ASIC STV0600
  23. * P/N 861050-0020: Sensor Photobit PB100 ASIC STV0600-1 - QuickCam Express
  24. * P/N 861055: Sensor ST VV6410 ASIC STV0610 - LEGO cam
  25. * P/N 861075-0040: Sensor HDCS1000 ASIC
  26. * P/N 961179-0700: Sensor ST VV6410 ASIC STV0602 - Dexxa WebCam USB
  27. * P/N 861040-0000: Sensor ST VV6410 ASIC STV0610 - QuickCam Web
  28. */
  29. #include "stv06xx_sensor.h"
  30. MODULE_AUTHOR("Erik Andrén");
  31. MODULE_DESCRIPTION("STV06XX USB Camera Driver");
  32. MODULE_LICENSE("GPL");
  33. static int dump_bridge;
  34. static int dump_sensor;
  35. int stv06xx_write_bridge(struct sd *sd, u16 address, u16 i2c_data)
  36. {
  37. int err;
  38. struct usb_device *udev = sd->gspca_dev.dev;
  39. __u8 *buf = sd->gspca_dev.usb_buf;
  40. u8 len = (i2c_data > 0xff) ? 2 : 1;
  41. buf[0] = i2c_data & 0xff;
  42. buf[1] = (i2c_data >> 8) & 0xff;
  43. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  44. 0x04, 0x40, address, 0, buf, len,
  45. STV06XX_URB_MSG_TIMEOUT);
  46. PDEBUG(D_CONF, "Written 0x%x to address 0x%x, status: %d",
  47. i2c_data, address, err);
  48. return (err < 0) ? err : 0;
  49. }
  50. int stv06xx_read_bridge(struct sd *sd, u16 address, u8 *i2c_data)
  51. {
  52. int err;
  53. struct usb_device *udev = sd->gspca_dev.dev;
  54. __u8 *buf = sd->gspca_dev.usb_buf;
  55. err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  56. 0x04, 0xc0, address, 0, buf, 1,
  57. STV06XX_URB_MSG_TIMEOUT);
  58. *i2c_data = buf[0];
  59. PDEBUG(D_CONF, "Read 0x%x from address 0x%x, status %d",
  60. *i2c_data, address, err);
  61. return (err < 0) ? err : 0;
  62. }
  63. /* Wraps the normal write sensor bytes / words functions for writing a
  64. single value */
  65. int stv06xx_write_sensor(struct sd *sd, u8 address, u16 value)
  66. {
  67. if (sd->sensor->i2c_len == 2) {
  68. u16 data[2] = { address, value };
  69. return stv06xx_write_sensor_words(sd, data, 1);
  70. } else {
  71. u8 data[2] = { address, value };
  72. return stv06xx_write_sensor_bytes(sd, data, 1);
  73. }
  74. }
  75. static int stv06xx_write_sensor_finish(struct sd *sd)
  76. {
  77. int err = 0;
  78. if (IS_850(sd)) {
  79. struct usb_device *udev = sd->gspca_dev.dev;
  80. __u8 *buf = sd->gspca_dev.usb_buf;
  81. /* Quickam Web needs an extra packet */
  82. buf[0] = 0;
  83. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  84. 0x04, 0x40, 0x1704, 0, buf, 1,
  85. STV06XX_URB_MSG_TIMEOUT);
  86. }
  87. return (err < 0) ? err : 0;
  88. }
  89. int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
  90. {
  91. int err, i, j;
  92. struct usb_device *udev = sd->gspca_dev.dev;
  93. __u8 *buf = sd->gspca_dev.usb_buf;
  94. PDEBUG(D_USBO, "I2C: Command buffer contains %d entries", len);
  95. for (i = 0; i < len;) {
  96. /* Build the command buffer */
  97. memset(buf, 0, I2C_BUFFER_LENGTH);
  98. for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) {
  99. buf[j] = data[2*i];
  100. buf[0x10 + j] = data[2*i+1];
  101. PDEBUG(D_USBO, "I2C: Writing 0x%02x to reg 0x%02x",
  102. data[2*i+1], data[2*i]);
  103. }
  104. buf[0x20] = sd->sensor->i2c_addr;
  105. buf[0x21] = j - 1; /* Number of commands to send - 1 */
  106. buf[0x22] = I2C_WRITE_CMD;
  107. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  108. 0x04, 0x40, 0x0400, 0, buf,
  109. I2C_BUFFER_LENGTH,
  110. STV06XX_URB_MSG_TIMEOUT);
  111. if (err < 0)
  112. return err;
  113. }
  114. return stv06xx_write_sensor_finish(sd);
  115. }
  116. int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
  117. {
  118. int err, i, j;
  119. struct usb_device *udev = sd->gspca_dev.dev;
  120. __u8 *buf = sd->gspca_dev.usb_buf;
  121. PDEBUG(D_USBO, "I2C: Command buffer contains %d entries", len);
  122. for (i = 0; i < len;) {
  123. /* Build the command buffer */
  124. memset(buf, 0, I2C_BUFFER_LENGTH);
  125. for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) {
  126. buf[j] = data[2*i];
  127. buf[0x10 + j * 2] = data[2*i+1];
  128. buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8;
  129. PDEBUG(D_USBO, "I2C: Writing 0x%04x to reg 0x%02x",
  130. data[2*i+1], data[2*i]);
  131. }
  132. buf[0x20] = sd->sensor->i2c_addr;
  133. buf[0x21] = j - 1; /* Number of commands to send - 1 */
  134. buf[0x22] = I2C_WRITE_CMD;
  135. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  136. 0x04, 0x40, 0x0400, 0, buf,
  137. I2C_BUFFER_LENGTH,
  138. STV06XX_URB_MSG_TIMEOUT);
  139. if (err < 0)
  140. return err;
  141. }
  142. return stv06xx_write_sensor_finish(sd);
  143. }
  144. int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
  145. {
  146. int err;
  147. struct usb_device *udev = sd->gspca_dev.dev;
  148. __u8 *buf = sd->gspca_dev.usb_buf;
  149. err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush);
  150. if (err < 0)
  151. return err;
  152. /* Clear mem */
  153. memset(buf, 0, I2C_BUFFER_LENGTH);
  154. buf[0] = address;
  155. buf[0x20] = sd->sensor->i2c_addr;
  156. buf[0x21] = 0;
  157. /* Read I2C register */
  158. buf[0x22] = I2C_READ_CMD;
  159. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  160. 0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH,
  161. STV06XX_URB_MSG_TIMEOUT);
  162. if (err < 0) {
  163. PDEBUG(D_ERR, "I2C Read: error writing address: %d", err);
  164. return err;
  165. }
  166. err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  167. 0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len,
  168. STV06XX_URB_MSG_TIMEOUT);
  169. if (sd->sensor->i2c_len == 2)
  170. *value = buf[0] | (buf[1] << 8);
  171. else
  172. *value = buf[0];
  173. PDEBUG(D_USBO, "I2C: Read 0x%x from address 0x%x, status: %d",
  174. *value, address, err);
  175. return (err < 0) ? err : 0;
  176. }
  177. /* Dumps all bridge registers */
  178. static void stv06xx_dump_bridge(struct sd *sd)
  179. {
  180. int i;
  181. u8 data, buf;
  182. info("Dumping all stv06xx bridge registers");
  183. for (i = 0x1400; i < 0x160f; i++) {
  184. stv06xx_read_bridge(sd, i, &data);
  185. info("Read 0x%x from address 0x%x", data, i);
  186. }
  187. for (i = 0x1400; i < 0x160f; i++) {
  188. stv06xx_read_bridge(sd, i, &data);
  189. buf = data;
  190. stv06xx_write_bridge(sd, i, 0xff);
  191. stv06xx_read_bridge(sd, i, &data);
  192. if (data == 0xff)
  193. info("Register 0x%x is read/write", i);
  194. else if (data != buf)
  195. info("Register 0x%x is read/write,"
  196. "but only partially", i);
  197. else
  198. info("Register 0x%x is read-only", i);
  199. stv06xx_write_bridge(sd, i, buf);
  200. }
  201. }
  202. /* this function is called at probe and resume time */
  203. static int stv06xx_init(struct gspca_dev *gspca_dev)
  204. {
  205. struct sd *sd = (struct sd *) gspca_dev;
  206. int err;
  207. PDEBUG(D_PROBE, "Initializing camera");
  208. /* Let the usb init settle for a bit
  209. before performing the initialization */
  210. msleep(250);
  211. err = sd->sensor->init(sd);
  212. if (dump_sensor)
  213. sd->sensor->dump(sd);
  214. return (err < 0) ? err : 0;
  215. }
  216. /* Start the camera */
  217. static int stv06xx_start(struct gspca_dev *gspca_dev)
  218. {
  219. struct sd *sd = (struct sd *) gspca_dev;
  220. int err;
  221. /* Prepare the sensor for start */
  222. err = sd->sensor->start(sd);
  223. if (err < 0)
  224. goto out;
  225. /* Start isochronous streaming */
  226. err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
  227. out:
  228. if (err < 0)
  229. PDEBUG(D_STREAM, "Starting stream failed");
  230. else
  231. PDEBUG(D_STREAM, "Started streaming");
  232. return (err < 0) ? err : 0;
  233. }
  234. static void stv06xx_stopN(struct gspca_dev *gspca_dev)
  235. {
  236. int err;
  237. struct sd *sd = (struct sd *) gspca_dev;
  238. /* stop ISO-streaming */
  239. err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
  240. if (err < 0)
  241. goto out;
  242. err = sd->sensor->stop(sd);
  243. if (err < 0)
  244. goto out;
  245. out:
  246. if (err < 0)
  247. PDEBUG(D_STREAM, "Failed to stop stream");
  248. else
  249. PDEBUG(D_STREAM, "Stopped streaming");
  250. }
  251. /*
  252. * Analyse an USB packet of the data stream and store it appropriately.
  253. * Each packet contains an integral number of chunks. Each chunk has
  254. * 2-bytes identification, followed by 2-bytes that describe the chunk
  255. * length. Known/guessed chunk identifications are:
  256. * 8001/8005/C001/C005 - Begin new frame
  257. * 8002/8006/C002/C006 - End frame
  258. * 0200/4200 - Contains actual image data, bayer or compressed
  259. * 0005 - 11 bytes of unknown data
  260. * 0100 - 2 bytes of unknown data
  261. * The 0005 and 0100 chunks seem to appear only in compressed stream.
  262. */
  263. static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev,
  264. struct gspca_frame *frame, /* target */
  265. __u8 *data, /* isoc packet */
  266. int len) /* iso packet length */
  267. {
  268. PDEBUG(D_PACK, "Packet of length %d arrived", len);
  269. /* A packet may contain several frames
  270. loop until the whole packet is reached */
  271. while (len) {
  272. int id, chunk_len;
  273. if (len < 4) {
  274. PDEBUG(D_PACK, "Packet is smaller than 4 bytes");
  275. return;
  276. }
  277. /* Capture the id */
  278. id = (data[0] << 8) | data[1];
  279. /* Capture the chunk length */
  280. chunk_len = (data[2] << 8) | data[3];
  281. PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len);
  282. data += 4;
  283. len -= 4;
  284. if (len < chunk_len) {
  285. PDEBUG(D_ERR, "URB packet length is smaller"
  286. " than the specified chunk length");
  287. return;
  288. }
  289. switch (id) {
  290. case 0x0200:
  291. case 0x4200:
  292. PDEBUG(D_PACK, "Frame data packet detected");
  293. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  294. data, chunk_len);
  295. break;
  296. case 0x8001:
  297. case 0x8005:
  298. case 0xc001:
  299. case 0xc005:
  300. PDEBUG(D_PACK, "Starting new frame");
  301. /* Create a new frame, chunk length should be zero */
  302. gspca_frame_add(gspca_dev, FIRST_PACKET,
  303. frame, data, 0);
  304. if (chunk_len)
  305. PDEBUG(D_ERR, "Chunk length is "
  306. "non-zero on a SOF");
  307. break;
  308. case 0x8002:
  309. case 0x8006:
  310. case 0xc002:
  311. PDEBUG(D_PACK, "End of frame detected");
  312. /* Complete the last frame (if any) */
  313. gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
  314. if (chunk_len)
  315. PDEBUG(D_ERR, "Chunk length is "
  316. "non-zero on a EOF");
  317. break;
  318. case 0x0005:
  319. PDEBUG(D_PACK, "Chunk 0x005 detected");
  320. /* Unknown chunk with 11 bytes of data,
  321. occurs just before end of each frame
  322. in compressed mode */
  323. break;
  324. case 0x0100:
  325. PDEBUG(D_PACK, "Chunk 0x0100 detected");
  326. /* Unknown chunk with 2 bytes of data,
  327. occurs 2-3 times per USB interrupt */
  328. break;
  329. default:
  330. PDEBUG(D_PACK, "Unknown chunk %d detected", id);
  331. /* Unknown chunk */
  332. }
  333. data += chunk_len;
  334. len -= chunk_len;
  335. }
  336. }
  337. static int stv06xx_config(struct gspca_dev *gspca_dev,
  338. const struct usb_device_id *id);
  339. /* sub-driver description */
  340. static const struct sd_desc sd_desc = {
  341. .name = MODULE_NAME,
  342. .config = stv06xx_config,
  343. .init = stv06xx_init,
  344. .start = stv06xx_start,
  345. .stopN = stv06xx_stopN,
  346. .pkt_scan = stv06xx_pkt_scan
  347. };
  348. /* This function is called at probe time */
  349. static int stv06xx_config(struct gspca_dev *gspca_dev,
  350. const struct usb_device_id *id)
  351. {
  352. struct sd *sd = (struct sd *) gspca_dev;
  353. struct cam *cam;
  354. PDEBUG(D_PROBE, "Configuring camera");
  355. cam = &gspca_dev->cam;
  356. sd->desc = sd_desc;
  357. gspca_dev->sd_desc = &sd->desc;
  358. if (dump_bridge)
  359. stv06xx_dump_bridge(sd);
  360. sd->sensor = &stv06xx_sensor_vv6410;
  361. if (!sd->sensor->probe(sd))
  362. return 0;
  363. sd->sensor = &stv06xx_sensor_hdcs1x00;
  364. if (!sd->sensor->probe(sd))
  365. return 0;
  366. sd->sensor = &stv06xx_sensor_hdcs1020;
  367. if (!sd->sensor->probe(sd))
  368. return 0;
  369. sd->sensor = &stv06xx_sensor_pb0100;
  370. if (!sd->sensor->probe(sd))
  371. return 0;
  372. sd->sensor = NULL;
  373. return -ENODEV;
  374. }
  375. /* -- module initialisation -- */
  376. static const __devinitdata struct usb_device_id device_table[] = {
  377. {USB_DEVICE(0x046d, 0x0840)}, /* QuickCam Express */
  378. {USB_DEVICE(0x046d, 0x0850)}, /* LEGO cam / QuickCam Web */
  379. {USB_DEVICE(0x046d, 0x0870)}, /* Dexxa WebCam USB */
  380. {}
  381. };
  382. MODULE_DEVICE_TABLE(usb, device_table);
  383. /* -- device connect -- */
  384. static int sd_probe(struct usb_interface *intf,
  385. const struct usb_device_id *id)
  386. {
  387. PDEBUG(D_PROBE, "Probing for a stv06xx device");
  388. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  389. THIS_MODULE);
  390. }
  391. static void sd_disconnect(struct usb_interface *intf)
  392. {
  393. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  394. struct sd *sd = (struct sd *) gspca_dev;
  395. PDEBUG(D_PROBE, "Disconnecting the stv06xx device");
  396. if (sd->sensor->disconnect)
  397. sd->sensor->disconnect(sd);
  398. gspca_disconnect(intf);
  399. }
  400. static struct usb_driver sd_driver = {
  401. .name = MODULE_NAME,
  402. .id_table = device_table,
  403. .probe = sd_probe,
  404. .disconnect = sd_disconnect,
  405. #ifdef CONFIG_PM
  406. .suspend = gspca_suspend,
  407. .resume = gspca_resume,
  408. #endif
  409. };
  410. /* -- module insert / remove -- */
  411. static int __init sd_mod_init(void)
  412. {
  413. int ret;
  414. ret = usb_register(&sd_driver);
  415. if (ret < 0)
  416. return -1;
  417. PDEBUG(D_PROBE, "registered");
  418. return 0;
  419. }
  420. static void __exit sd_mod_exit(void)
  421. {
  422. usb_deregister(&sd_driver);
  423. PDEBUG(D_PROBE, "deregistered");
  424. }
  425. module_init(sd_mod_init);
  426. module_exit(sd_mod_exit);
  427. module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
  428. MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
  429. module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
  430. MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");