stv06xx.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. out:
  244. if (err < 0)
  245. PDEBUG(D_STREAM, "Failed to stop stream");
  246. else
  247. PDEBUG(D_STREAM, "Stopped streaming");
  248. }
  249. /*
  250. * Analyse an USB packet of the data stream and store it appropriately.
  251. * Each packet contains an integral number of chunks. Each chunk has
  252. * 2-bytes identification, followed by 2-bytes that describe the chunk
  253. * length. Known/guessed chunk identifications are:
  254. * 8001/8005/C001/C005 - Begin new frame
  255. * 8002/8006/C002/C006 - End frame
  256. * 0200/4200 - Contains actual image data, bayer or compressed
  257. * 0005 - 11 bytes of unknown data
  258. * 0100 - 2 bytes of unknown data
  259. * The 0005 and 0100 chunks seem to appear only in compressed stream.
  260. */
  261. static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev,
  262. struct gspca_frame *frame, /* target */
  263. __u8 *data, /* isoc packet */
  264. int len) /* iso packet length */
  265. {
  266. PDEBUG(D_PACK, "Packet of length %d arrived", len);
  267. /* A packet may contain several frames
  268. loop until the whole packet is reached */
  269. while (len) {
  270. int id, chunk_len;
  271. if (len < 4) {
  272. PDEBUG(D_PACK, "Packet is smaller than 4 bytes");
  273. return;
  274. }
  275. /* Capture the id */
  276. id = (data[0] << 8) | data[1];
  277. /* Capture the chunk length */
  278. chunk_len = (data[2] << 8) | data[3];
  279. PDEBUG(D_PACK, "Chunk id: %x, length: %d", id, chunk_len);
  280. data += 4;
  281. len -= 4;
  282. if (len < chunk_len) {
  283. PDEBUG(D_ERR, "URB packet length is smaller"
  284. " than the specified chunk length");
  285. return;
  286. }
  287. switch (id) {
  288. case 0x0200:
  289. case 0x4200:
  290. PDEBUG(D_PACK, "Frame data packet detected");
  291. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  292. data, chunk_len);
  293. break;
  294. case 0x8001:
  295. case 0x8005:
  296. case 0xc001:
  297. case 0xc005:
  298. PDEBUG(D_PACK, "Starting new frame");
  299. /* Create a new frame, chunk length should be zero */
  300. gspca_frame_add(gspca_dev, FIRST_PACKET,
  301. frame, data, 0);
  302. if (chunk_len)
  303. PDEBUG(D_ERR, "Chunk length is "
  304. "non-zero on a SOF");
  305. break;
  306. case 0x8002:
  307. case 0x8006:
  308. case 0xc002:
  309. PDEBUG(D_PACK, "End of frame detected");
  310. /* Complete the last frame (if any) */
  311. gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
  312. if (chunk_len)
  313. PDEBUG(D_ERR, "Chunk length is "
  314. "non-zero on a EOF");
  315. break;
  316. case 0x0005:
  317. PDEBUG(D_PACK, "Chunk 0x005 detected");
  318. /* Unknown chunk with 11 bytes of data,
  319. occurs just before end of each frame
  320. in compressed mode */
  321. break;
  322. case 0x0100:
  323. PDEBUG(D_PACK, "Chunk 0x0100 detected");
  324. /* Unknown chunk with 2 bytes of data,
  325. occurs 2-3 times per USB interrupt */
  326. break;
  327. default:
  328. PDEBUG(D_PACK, "Unknown chunk %d detected", id);
  329. /* Unknown chunk */
  330. }
  331. data += chunk_len;
  332. len -= chunk_len;
  333. }
  334. }
  335. static int stv06xx_config(struct gspca_dev *gspca_dev,
  336. const struct usb_device_id *id);
  337. /* sub-driver description */
  338. static const struct sd_desc sd_desc = {
  339. .name = MODULE_NAME,
  340. .config = stv06xx_config,
  341. .init = stv06xx_init,
  342. .start = stv06xx_start,
  343. .stopN = stv06xx_stopN,
  344. .pkt_scan = stv06xx_pkt_scan
  345. };
  346. /* This function is called at probe time */
  347. static int stv06xx_config(struct gspca_dev *gspca_dev,
  348. const struct usb_device_id *id)
  349. {
  350. struct sd *sd = (struct sd *) gspca_dev;
  351. struct cam *cam;
  352. PDEBUG(D_PROBE, "Configuring camera");
  353. cam = &gspca_dev->cam;
  354. sd->desc = sd_desc;
  355. gspca_dev->sd_desc = &sd->desc;
  356. if (dump_bridge)
  357. stv06xx_dump_bridge(sd);
  358. sd->sensor = &stv06xx_sensor_vv6410;
  359. if (!sd->sensor->probe(sd))
  360. return 0;
  361. sd->sensor = &stv06xx_sensor_hdcs1x00;
  362. if (!sd->sensor->probe(sd))
  363. return 0;
  364. sd->sensor = &stv06xx_sensor_hdcs1020;
  365. if (!sd->sensor->probe(sd))
  366. return 0;
  367. sd->sensor = &stv06xx_sensor_pb0100;
  368. if (!sd->sensor->probe(sd))
  369. return 0;
  370. sd->sensor = NULL;
  371. return -ENODEV;
  372. }
  373. /* -- module initialisation -- */
  374. static const __devinitdata struct usb_device_id device_table[] = {
  375. {USB_DEVICE(0x046d, 0x0840)}, /* QuickCam Express */
  376. {USB_DEVICE(0x046d, 0x0850)}, /* LEGO cam / QuickCam Web */
  377. {USB_DEVICE(0x046d, 0x0870)}, /* Dexxa WebCam USB */
  378. {}
  379. };
  380. MODULE_DEVICE_TABLE(usb, device_table);
  381. /* -- device connect -- */
  382. static int sd_probe(struct usb_interface *intf,
  383. const struct usb_device_id *id)
  384. {
  385. PDEBUG(D_PROBE, "Probing for a stv06xx device");
  386. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  387. THIS_MODULE);
  388. }
  389. static void sd_disconnect(struct usb_interface *intf)
  390. {
  391. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  392. struct sd *sd = (struct sd *) gspca_dev;
  393. PDEBUG(D_PROBE, "Disconnecting the stv06xx device");
  394. if (sd->sensor->disconnect)
  395. sd->sensor->disconnect(sd);
  396. gspca_disconnect(intf);
  397. }
  398. static struct usb_driver sd_driver = {
  399. .name = MODULE_NAME,
  400. .id_table = device_table,
  401. .probe = sd_probe,
  402. .disconnect = sd_disconnect,
  403. #ifdef CONFIG_PM
  404. .suspend = gspca_suspend,
  405. .resume = gspca_resume,
  406. #endif
  407. };
  408. /* -- module insert / remove -- */
  409. static int __init sd_mod_init(void)
  410. {
  411. int ret;
  412. ret = usb_register(&sd_driver);
  413. if (ret < 0)
  414. return ret;
  415. PDEBUG(D_PROBE, "registered");
  416. return 0;
  417. }
  418. static void __exit sd_mod_exit(void)
  419. {
  420. usb_deregister(&sd_driver);
  421. PDEBUG(D_PROBE, "deregistered");
  422. }
  423. module_init(sd_mod_init);
  424. module_exit(sd_mod_exit);
  425. module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
  426. MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
  427. module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
  428. MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");