stv06xx.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  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, "Reading 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 (sd->bridge == BRIDGE_STV610) {
  79. struct usb_device *udev = sd->gspca_dev.dev;
  80. __u8 *buf = sd->gspca_dev.usb_buf;
  81. buf[0] = 0;
  82. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  83. 0x04, 0x40, 0x1704, 0, buf, 1,
  84. STV06XX_URB_MSG_TIMEOUT);
  85. }
  86. return (err < 0) ? err : 0;
  87. }
  88. int stv06xx_write_sensor_bytes(struct sd *sd, const u8 *data, u8 len)
  89. {
  90. int err, i, j;
  91. struct usb_device *udev = sd->gspca_dev.dev;
  92. __u8 *buf = sd->gspca_dev.usb_buf;
  93. PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
  94. for (i = 0; i < len;) {
  95. /* Build the command buffer */
  96. memset(buf, 0, I2C_BUFFER_LENGTH);
  97. for (j = 0; j < I2C_MAX_BYTES && i < len; j++, i++) {
  98. buf[j] = data[2*i];
  99. buf[0x10 + j] = data[2*i+1];
  100. PDEBUG(D_CONF, "I2C: Writing 0x%02x to reg 0x%02x",
  101. data[2*i+1], data[2*i]);
  102. }
  103. buf[0x20] = sd->sensor->i2c_addr;
  104. buf[0x21] = j - 1; /* Number of commands to send - 1 */
  105. buf[0x22] = I2C_WRITE_CMD;
  106. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  107. 0x04, 0x40, 0x0400, 0, buf,
  108. I2C_BUFFER_LENGTH,
  109. STV06XX_URB_MSG_TIMEOUT);
  110. if (err < 0)
  111. return err;
  112. }
  113. return stv06xx_write_sensor_finish(sd);
  114. }
  115. int stv06xx_write_sensor_words(struct sd *sd, const u16 *data, u8 len)
  116. {
  117. int err, i, j;
  118. struct usb_device *udev = sd->gspca_dev.dev;
  119. __u8 *buf = sd->gspca_dev.usb_buf;
  120. PDEBUG(D_CONF, "I2C: Command buffer contains %d entries", len);
  121. for (i = 0; i < len;) {
  122. /* Build the command buffer */
  123. memset(buf, 0, I2C_BUFFER_LENGTH);
  124. for (j = 0; j < I2C_MAX_WORDS && i < len; j++, i++) {
  125. buf[j] = data[2*i];
  126. buf[0x10 + j * 2] = data[2*i+1];
  127. buf[0x10 + j * 2 + 1] = data[2*i+1] >> 8;
  128. PDEBUG(D_CONF, "I2C: Writing 0x%04x to reg 0x%02x",
  129. data[2*i+1], data[2*i]);
  130. }
  131. buf[0x20] = sd->sensor->i2c_addr;
  132. buf[0x21] = j - 1; /* Number of commands to send - 1 */
  133. buf[0x22] = I2C_WRITE_CMD;
  134. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  135. 0x04, 0x40, 0x0400, 0, buf,
  136. I2C_BUFFER_LENGTH,
  137. STV06XX_URB_MSG_TIMEOUT);
  138. if (err < 0)
  139. return err;
  140. }
  141. return stv06xx_write_sensor_finish(sd);
  142. }
  143. int stv06xx_read_sensor(struct sd *sd, const u8 address, u16 *value)
  144. {
  145. int err;
  146. struct usb_device *udev = sd->gspca_dev.dev;
  147. __u8 *buf = sd->gspca_dev.usb_buf;
  148. err = stv06xx_write_bridge(sd, STV_I2C_FLUSH, sd->sensor->i2c_flush);
  149. if (err < 0)
  150. return err;
  151. /* Clear mem */
  152. memset(buf, 0, I2C_BUFFER_LENGTH);
  153. buf[0] = address;
  154. buf[0x20] = sd->sensor->i2c_addr;
  155. buf[0x21] = 0;
  156. /* Read I2C register */
  157. buf[0x22] = I2C_READ_CMD;
  158. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  159. 0x04, 0x40, 0x1400, 0, buf, I2C_BUFFER_LENGTH,
  160. STV06XX_URB_MSG_TIMEOUT);
  161. if (err < 0) {
  162. PDEBUG(D_ERR, "I2C: Read error writing address: %d", err);
  163. return err;
  164. }
  165. err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  166. 0x04, 0xc0, 0x1410, 0, buf, sd->sensor->i2c_len,
  167. STV06XX_URB_MSG_TIMEOUT);
  168. if (sd->sensor->i2c_len == 2)
  169. *value = buf[0] | (buf[1] << 8);
  170. else
  171. *value = buf[0];
  172. PDEBUG(D_CONF, "I2C: Read 0x%x from address 0x%x, status: %d",
  173. *value, address, err);
  174. return (err < 0) ? err : 0;
  175. }
  176. /* Dumps all bridge registers */
  177. static void stv06xx_dump_bridge(struct sd *sd)
  178. {
  179. int i;
  180. u8 data, buf;
  181. info("Dumping all stv06xx bridge registers");
  182. for (i = 0x1400; i < 0x160f; i++) {
  183. stv06xx_read_bridge(sd, i, &data);
  184. info("Read 0x%x from address 0x%x", data, i);
  185. }
  186. for (i = 0x1400; i < 0x160f; i++) {
  187. stv06xx_read_bridge(sd, i, &data);
  188. buf = data;
  189. stv06xx_write_bridge(sd, i, 0xff);
  190. stv06xx_read_bridge(sd, i, &data);
  191. if (data == 0xff)
  192. info("Register 0x%x is read/write", i);
  193. else if (data != buf)
  194. info("Register 0x%x is read/write,"
  195. "but only partially", i);
  196. else
  197. info("Register 0x%x is read-only", i);
  198. stv06xx_write_bridge(sd, i, buf);
  199. }
  200. }
  201. /* this function is called at probe and resume time */
  202. static int stv06xx_init(struct gspca_dev *gspca_dev)
  203. {
  204. struct sd *sd = (struct sd *) gspca_dev;
  205. int err;
  206. PDEBUG(D_PROBE, "Initializing camera");
  207. /* Let the usb init settle for a bit
  208. before performing the initialization */
  209. msleep(250);
  210. err = sd->sensor->init(sd);
  211. if (dump_sensor && sd->sensor->dump)
  212. sd->sensor->dump(sd);
  213. return (err < 0) ? err : 0;
  214. }
  215. /* Start the camera */
  216. static int stv06xx_start(struct gspca_dev *gspca_dev)
  217. {
  218. struct sd *sd = (struct sd *) gspca_dev;
  219. int err;
  220. /* Prepare the sensor for start */
  221. err = sd->sensor->start(sd);
  222. if (err < 0)
  223. goto out;
  224. /* Start isochronous streaming */
  225. err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 1);
  226. out:
  227. if (err < 0)
  228. PDEBUG(D_STREAM, "Starting stream failed");
  229. else
  230. PDEBUG(D_STREAM, "Started streaming");
  231. return (err < 0) ? err : 0;
  232. }
  233. static void stv06xx_stopN(struct gspca_dev *gspca_dev)
  234. {
  235. int err;
  236. struct sd *sd = (struct sd *) gspca_dev;
  237. /* stop ISO-streaming */
  238. err = stv06xx_write_bridge(sd, STV_ISO_ENABLE, 0);
  239. if (err < 0)
  240. goto out;
  241. err = sd->sensor->stop(sd);
  242. out:
  243. if (err < 0)
  244. PDEBUG(D_STREAM, "Failed to stop stream");
  245. else
  246. PDEBUG(D_STREAM, "Stopped streaming");
  247. }
  248. /*
  249. * Analyse an USB packet of the data stream and store it appropriately.
  250. * Each packet contains an integral number of chunks. Each chunk has
  251. * 2-bytes identification, followed by 2-bytes that describe the chunk
  252. * length. Known/guessed chunk identifications are:
  253. * 8001/8005/C001/C005 - Begin new frame
  254. * 8002/8006/C002/C006 - End frame
  255. * 0200/4200 - Contains actual image data, bayer or compressed
  256. * 0005 - 11 bytes of unknown data
  257. * 0100 - 2 bytes of unknown data
  258. * The 0005 and 0100 chunks seem to appear only in compressed stream.
  259. */
  260. static void stv06xx_pkt_scan(struct gspca_dev *gspca_dev,
  261. struct gspca_frame *frame, /* target */
  262. __u8 *data, /* isoc packet */
  263. int len) /* iso packet length */
  264. {
  265. struct sd *sd = (struct sd *) gspca_dev;
  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. gspca_dev->last_packet_type = DISCARD_PACKET;
  286. return;
  287. }
  288. /* First byte seem to be 02=data 2nd byte is unknown??? */
  289. if (sd->bridge == BRIDGE_ST6422 && (id & 0xFF00) == 0x0200)
  290. goto frame_data;
  291. switch (id) {
  292. case 0x0200:
  293. case 0x4200:
  294. frame_data:
  295. PDEBUG(D_PACK, "Frame data packet detected");
  296. if (sd->to_skip) {
  297. int skip = (sd->to_skip < chunk_len) ?
  298. sd->to_skip : chunk_len;
  299. data += skip;
  300. len -= skip;
  301. chunk_len -= skip;
  302. sd->to_skip -= skip;
  303. }
  304. gspca_frame_add(gspca_dev, INTER_PACKET, frame,
  305. data, chunk_len);
  306. break;
  307. case 0x8001:
  308. case 0x8005:
  309. case 0xc001:
  310. case 0xc005:
  311. PDEBUG(D_PACK, "Starting new frame");
  312. /* Create a new frame, chunk length should be zero */
  313. gspca_frame_add(gspca_dev, FIRST_PACKET,
  314. frame, data, 0);
  315. if (sd->bridge == BRIDGE_ST6422)
  316. sd->to_skip = gspca_dev->width * 4;
  317. if (chunk_len)
  318. PDEBUG(D_ERR, "Chunk length is "
  319. "non-zero on a SOF");
  320. break;
  321. case 0x8002:
  322. case 0x8006:
  323. case 0xc002:
  324. PDEBUG(D_PACK, "End of frame detected");
  325. /* Complete the last frame (if any) */
  326. gspca_frame_add(gspca_dev, LAST_PACKET, frame, data, 0);
  327. if (chunk_len)
  328. PDEBUG(D_ERR, "Chunk length is "
  329. "non-zero on a EOF");
  330. break;
  331. case 0x0005:
  332. PDEBUG(D_PACK, "Chunk 0x005 detected");
  333. /* Unknown chunk with 11 bytes of data,
  334. occurs just before end of each frame
  335. in compressed mode */
  336. break;
  337. case 0x0100:
  338. PDEBUG(D_PACK, "Chunk 0x0100 detected");
  339. /* Unknown chunk with 2 bytes of data,
  340. occurs 2-3 times per USB interrupt */
  341. break;
  342. case 0x42ff:
  343. PDEBUG(D_PACK, "Chunk 0x42ff detected");
  344. /* Special chunk seen sometimes on the ST6422 */
  345. break;
  346. default:
  347. PDEBUG(D_PACK, "Unknown chunk 0x%04x detected", id);
  348. /* Unknown chunk */
  349. }
  350. data += chunk_len;
  351. len -= chunk_len;
  352. }
  353. }
  354. static int stv06xx_config(struct gspca_dev *gspca_dev,
  355. const struct usb_device_id *id);
  356. /* sub-driver description */
  357. static const struct sd_desc sd_desc = {
  358. .name = MODULE_NAME,
  359. .config = stv06xx_config,
  360. .init = stv06xx_init,
  361. .start = stv06xx_start,
  362. .stopN = stv06xx_stopN,
  363. .pkt_scan = stv06xx_pkt_scan
  364. };
  365. /* This function is called at probe time */
  366. static int stv06xx_config(struct gspca_dev *gspca_dev,
  367. const struct usb_device_id *id)
  368. {
  369. struct sd *sd = (struct sd *) gspca_dev;
  370. struct cam *cam;
  371. PDEBUG(D_PROBE, "Configuring camera");
  372. cam = &gspca_dev->cam;
  373. sd->desc = sd_desc;
  374. sd->bridge = id->driver_info;
  375. gspca_dev->sd_desc = &sd->desc;
  376. if (dump_bridge)
  377. stv06xx_dump_bridge(sd);
  378. sd->sensor = &stv06xx_sensor_st6422;
  379. if (!sd->sensor->probe(sd))
  380. return 0;
  381. sd->sensor = &stv06xx_sensor_vv6410;
  382. if (!sd->sensor->probe(sd))
  383. return 0;
  384. sd->sensor = &stv06xx_sensor_hdcs1x00;
  385. if (!sd->sensor->probe(sd))
  386. return 0;
  387. sd->sensor = &stv06xx_sensor_hdcs1020;
  388. if (!sd->sensor->probe(sd))
  389. return 0;
  390. sd->sensor = &stv06xx_sensor_pb0100;
  391. if (!sd->sensor->probe(sd))
  392. return 0;
  393. sd->sensor = NULL;
  394. return -ENODEV;
  395. }
  396. /* -- module initialisation -- */
  397. static const __devinitdata struct usb_device_id device_table[] = {
  398. /* QuickCam Express */
  399. {USB_DEVICE(0x046d, 0x0840), .driver_info = BRIDGE_STV600 },
  400. /* LEGO cam / QuickCam Web */
  401. {USB_DEVICE(0x046d, 0x0850), .driver_info = BRIDGE_STV610 },
  402. /* Dexxa WebCam USB */
  403. {USB_DEVICE(0x046d, 0x0870), .driver_info = BRIDGE_STV602 },
  404. /* QuickCam Messenger */
  405. {USB_DEVICE(0x046D, 0x08F0), .driver_info = BRIDGE_ST6422 },
  406. /* QuickCam Communicate */
  407. {USB_DEVICE(0x046D, 0x08F5), .driver_info = BRIDGE_ST6422 },
  408. /* QuickCam Messenger (new) */
  409. {USB_DEVICE(0x046D, 0x08F6), .driver_info = BRIDGE_ST6422 },
  410. /* QuickCam Messenger (new) */
  411. {USB_DEVICE(0x046D, 0x08DA), .driver_info = BRIDGE_ST6422 },
  412. {}
  413. };
  414. MODULE_DEVICE_TABLE(usb, device_table);
  415. /* -- device connect -- */
  416. static int sd_probe(struct usb_interface *intf,
  417. const struct usb_device_id *id)
  418. {
  419. PDEBUG(D_PROBE, "Probing for a stv06xx device");
  420. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  421. THIS_MODULE);
  422. }
  423. static void sd_disconnect(struct usb_interface *intf)
  424. {
  425. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  426. struct sd *sd = (struct sd *) gspca_dev;
  427. PDEBUG(D_PROBE, "Disconnecting the stv06xx device");
  428. if (sd->sensor->disconnect)
  429. sd->sensor->disconnect(sd);
  430. gspca_disconnect(intf);
  431. }
  432. static struct usb_driver sd_driver = {
  433. .name = MODULE_NAME,
  434. .id_table = device_table,
  435. .probe = sd_probe,
  436. .disconnect = sd_disconnect,
  437. #ifdef CONFIG_PM
  438. .suspend = gspca_suspend,
  439. .resume = gspca_resume,
  440. #endif
  441. };
  442. /* -- module insert / remove -- */
  443. static int __init sd_mod_init(void)
  444. {
  445. int ret;
  446. ret = usb_register(&sd_driver);
  447. if (ret < 0)
  448. return ret;
  449. PDEBUG(D_PROBE, "registered");
  450. return 0;
  451. }
  452. static void __exit sd_mod_exit(void)
  453. {
  454. usb_deregister(&sd_driver);
  455. PDEBUG(D_PROBE, "deregistered");
  456. }
  457. module_init(sd_mod_init);
  458. module_exit(sd_mod_exit);
  459. module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
  460. MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
  461. module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
  462. MODULE_PARM_DESC(dump_sensor, "Dumps all sensor registers at startup");