m5602_core.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. /*
  2. * USB Driver for ALi m5602 based webcams
  3. *
  4. * Copyright (C) 2008 Erik Andrén
  5. * Copyright (C) 2007 Ilyes Gouta. Based on the m5603x Linux Driver Project.
  6. * Copyright (C) 2005 m5603x Linux Driver Project <m5602@x3ng.com.br>
  7. *
  8. * Portions of code to USB interface and ALi driver software,
  9. * Copyright (c) 2006 Willem Duinker
  10. * v4l2 interface modeled after the V4L2 driver
  11. * for SN9C10x PC Camera Controllers
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License as
  15. * published by the Free Software Foundation, version 2.
  16. *
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include "m5602_ov9650.h"
  20. #include "m5602_ov7660.h"
  21. #include "m5602_mt9m111.h"
  22. #include "m5602_po1030.h"
  23. #include "m5602_s5k83a.h"
  24. #include "m5602_s5k4aa.h"
  25. /* Kernel module parameters */
  26. int force_sensor;
  27. static bool dump_bridge;
  28. bool dump_sensor;
  29. static const struct usb_device_id m5602_table[] = {
  30. {USB_DEVICE(0x0402, 0x5602)},
  31. {}
  32. };
  33. MODULE_DEVICE_TABLE(usb, m5602_table);
  34. /* Reads a byte from the m5602 */
  35. int m5602_read_bridge(struct sd *sd, const u8 address, u8 *i2c_data)
  36. {
  37. int err;
  38. struct gspca_dev *gspca_dev = (struct gspca_dev *) sd;
  39. struct usb_device *udev = sd->gspca_dev.dev;
  40. __u8 *buf = sd->gspca_dev.usb_buf;
  41. err = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),
  42. 0x04, 0xc0, 0x14,
  43. 0x8100 + address, buf,
  44. 1, M5602_URB_MSG_TIMEOUT);
  45. *i2c_data = buf[0];
  46. PDEBUG(D_CONF, "Reading bridge register 0x%x containing 0x%x",
  47. address, *i2c_data);
  48. /* usb_control_msg(...) returns the number of bytes sent upon success,
  49. mask that and return zero instead*/
  50. return (err < 0) ? err : 0;
  51. }
  52. /* Writes a byte to the m5602 */
  53. int m5602_write_bridge(struct sd *sd, const u8 address, const u8 i2c_data)
  54. {
  55. int err;
  56. struct gspca_dev *gspca_dev = (struct gspca_dev *) sd;
  57. struct usb_device *udev = sd->gspca_dev.dev;
  58. __u8 *buf = sd->gspca_dev.usb_buf;
  59. PDEBUG(D_CONF, "Writing bridge register 0x%x with 0x%x",
  60. address, i2c_data);
  61. memcpy(buf, bridge_urb_skeleton,
  62. sizeof(bridge_urb_skeleton));
  63. buf[1] = address;
  64. buf[3] = i2c_data;
  65. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  66. 0x04, 0x40, 0x19,
  67. 0x0000, buf,
  68. 4, M5602_URB_MSG_TIMEOUT);
  69. /* usb_control_msg(...) returns the number of bytes sent upon success,
  70. mask that and return zero instead */
  71. return (err < 0) ? err : 0;
  72. }
  73. static int m5602_wait_for_i2c(struct sd *sd)
  74. {
  75. int err;
  76. u8 data;
  77. do {
  78. err = m5602_read_bridge(sd, M5602_XB_I2C_STATUS, &data);
  79. } while ((data & I2C_BUSY) && !err);
  80. return err;
  81. }
  82. int m5602_read_sensor(struct sd *sd, const u8 address,
  83. u8 *i2c_data, const u8 len)
  84. {
  85. int err, i;
  86. struct gspca_dev *gspca_dev = (struct gspca_dev *) sd;
  87. if (!len || len > sd->sensor->i2c_regW)
  88. return -EINVAL;
  89. err = m5602_wait_for_i2c(sd);
  90. if (err < 0)
  91. return err;
  92. err = m5602_write_bridge(sd, M5602_XB_I2C_DEV_ADDR,
  93. sd->sensor->i2c_slave_id);
  94. if (err < 0)
  95. return err;
  96. err = m5602_write_bridge(sd, M5602_XB_I2C_REG_ADDR, address);
  97. if (err < 0)
  98. return err;
  99. /* Sensors with registers that are of only
  100. one byte width are differently read */
  101. /* FIXME: This works with the ov9650, but has issues with the po1030 */
  102. if (sd->sensor->i2c_regW == 1) {
  103. err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 1);
  104. if (err < 0)
  105. return err;
  106. err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x08);
  107. } else {
  108. err = m5602_write_bridge(sd, M5602_XB_I2C_CTRL, 0x18 + len);
  109. }
  110. for (i = 0; (i < len) && !err; i++) {
  111. err = m5602_wait_for_i2c(sd);
  112. if (err < 0)
  113. return err;
  114. err = m5602_read_bridge(sd, M5602_XB_I2C_DATA, &(i2c_data[i]));
  115. PDEBUG(D_CONF, "Reading sensor register "
  116. "0x%x containing 0x%x ", address, *i2c_data);
  117. }
  118. return err;
  119. }
  120. int m5602_write_sensor(struct sd *sd, const u8 address,
  121. u8 *i2c_data, const u8 len)
  122. {
  123. int err, i;
  124. u8 *p;
  125. struct gspca_dev *gspca_dev = (struct gspca_dev *) sd;
  126. struct usb_device *udev = sd->gspca_dev.dev;
  127. __u8 *buf = sd->gspca_dev.usb_buf;
  128. /* No sensor with a data width larger than 16 bits has yet been seen */
  129. if (len > sd->sensor->i2c_regW || !len)
  130. return -EINVAL;
  131. memcpy(buf, sensor_urb_skeleton,
  132. sizeof(sensor_urb_skeleton));
  133. buf[11] = sd->sensor->i2c_slave_id;
  134. buf[15] = address;
  135. /* Special case larger sensor writes */
  136. p = buf + 16;
  137. /* Copy a four byte write sequence for each byte to be written to */
  138. for (i = 0; i < len; i++) {
  139. memcpy(p, sensor_urb_skeleton + 16, 4);
  140. p[3] = i2c_data[i];
  141. p += 4;
  142. PDEBUG(D_CONF, "Writing sensor register 0x%x with 0x%x",
  143. address, i2c_data[i]);
  144. }
  145. /* Copy the tailer */
  146. memcpy(p, sensor_urb_skeleton + 20, 4);
  147. /* Set the total length */
  148. p[3] = 0x10 + len;
  149. err = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),
  150. 0x04, 0x40, 0x19,
  151. 0x0000, buf,
  152. 20 + len * 4, M5602_URB_MSG_TIMEOUT);
  153. return (err < 0) ? err : 0;
  154. }
  155. /* Dump all the registers of the m5602 bridge,
  156. unfortunately this breaks the camera until it's power cycled */
  157. static void m5602_dump_bridge(struct sd *sd)
  158. {
  159. int i;
  160. for (i = 0; i < 0x80; i++) {
  161. unsigned char val = 0;
  162. m5602_read_bridge(sd, i, &val);
  163. pr_info("ALi m5602 address 0x%x contains 0x%x\n", i, val);
  164. }
  165. pr_info("Warning: The ALi m5602 webcam probably won't work until it's power cycled\n");
  166. }
  167. static int m5602_probe_sensor(struct sd *sd)
  168. {
  169. /* Try the po1030 */
  170. sd->sensor = &po1030;
  171. if (!sd->sensor->probe(sd))
  172. return 0;
  173. /* Try the mt9m111 sensor */
  174. sd->sensor = &mt9m111;
  175. if (!sd->sensor->probe(sd))
  176. return 0;
  177. /* Try the s5k4aa */
  178. sd->sensor = &s5k4aa;
  179. if (!sd->sensor->probe(sd))
  180. return 0;
  181. /* Try the ov9650 */
  182. sd->sensor = &ov9650;
  183. if (!sd->sensor->probe(sd))
  184. return 0;
  185. /* Try the ov7660 */
  186. sd->sensor = &ov7660;
  187. if (!sd->sensor->probe(sd))
  188. return 0;
  189. /* Try the s5k83a */
  190. sd->sensor = &s5k83a;
  191. if (!sd->sensor->probe(sd))
  192. return 0;
  193. /* More sensor probe function goes here */
  194. pr_info("Failed to find a sensor\n");
  195. sd->sensor = NULL;
  196. return -ENODEV;
  197. }
  198. static int m5602_configure(struct gspca_dev *gspca_dev,
  199. const struct usb_device_id *id);
  200. static int m5602_init(struct gspca_dev *gspca_dev)
  201. {
  202. struct sd *sd = (struct sd *) gspca_dev;
  203. int err;
  204. PDEBUG(D_CONF, "Initializing ALi m5602 webcam");
  205. /* Run the init sequence */
  206. err = sd->sensor->init(sd);
  207. return err;
  208. }
  209. static int m5602_init_controls(struct gspca_dev *gspca_dev)
  210. {
  211. struct sd *sd = (struct sd *) gspca_dev;
  212. if (!sd->sensor->init_controls)
  213. return 0;
  214. return sd->sensor->init_controls(sd);
  215. }
  216. static int m5602_start_transfer(struct gspca_dev *gspca_dev)
  217. {
  218. struct sd *sd = (struct sd *) gspca_dev;
  219. __u8 *buf = sd->gspca_dev.usb_buf;
  220. int err;
  221. /* Send start command to the camera */
  222. const u8 buffer[4] = {0x13, 0xf9, 0x0f, 0x01};
  223. if (sd->sensor->start)
  224. sd->sensor->start(sd);
  225. memcpy(buf, buffer, sizeof(buffer));
  226. err = usb_control_msg(gspca_dev->dev,
  227. usb_sndctrlpipe(gspca_dev->dev, 0),
  228. 0x04, 0x40, 0x19, 0x0000, buf,
  229. sizeof(buffer), M5602_URB_MSG_TIMEOUT);
  230. PDEBUG(D_STREAM, "Transfer started");
  231. return (err < 0) ? err : 0;
  232. }
  233. static void m5602_urb_complete(struct gspca_dev *gspca_dev,
  234. u8 *data, int len)
  235. {
  236. struct sd *sd = (struct sd *) gspca_dev;
  237. if (len < 6) {
  238. PDEBUG(D_PACK, "Packet is less than 6 bytes");
  239. return;
  240. }
  241. /* Frame delimiter: ff xx xx xx ff ff */
  242. if (data[0] == 0xff && data[4] == 0xff && data[5] == 0xff &&
  243. data[2] != sd->frame_id) {
  244. PDEBUG(D_FRAM, "Frame delimiter detected");
  245. sd->frame_id = data[2];
  246. /* Remove the extra fluff appended on each header */
  247. data += 6;
  248. len -= 6;
  249. /* Complete the last frame (if any) */
  250. gspca_frame_add(gspca_dev, LAST_PACKET,
  251. NULL, 0);
  252. sd->frame_count++;
  253. /* Create a new frame */
  254. gspca_frame_add(gspca_dev, FIRST_PACKET, data, len);
  255. PDEBUG(D_FRAM, "Starting new frame %d",
  256. sd->frame_count);
  257. } else {
  258. int cur_frame_len;
  259. cur_frame_len = gspca_dev->image_len;
  260. /* Remove urb header */
  261. data += 4;
  262. len -= 4;
  263. if (cur_frame_len + len <= gspca_dev->frsz) {
  264. PDEBUG(D_FRAM, "Continuing frame %d copying %d bytes",
  265. sd->frame_count, len);
  266. gspca_frame_add(gspca_dev, INTER_PACKET,
  267. data, len);
  268. } else {
  269. /* Add the remaining data up to frame size */
  270. gspca_frame_add(gspca_dev, INTER_PACKET, data,
  271. gspca_dev->frsz - cur_frame_len);
  272. }
  273. }
  274. }
  275. static void m5602_stop_transfer(struct gspca_dev *gspca_dev)
  276. {
  277. struct sd *sd = (struct sd *) gspca_dev;
  278. /* Run the sensor specific end transfer sequence */
  279. if (sd->sensor->stop)
  280. sd->sensor->stop(sd);
  281. }
  282. /* sub-driver description */
  283. static const struct sd_desc sd_desc = {
  284. .name = MODULE_NAME,
  285. .config = m5602_configure,
  286. .init = m5602_init,
  287. .init_controls = m5602_init_controls,
  288. .start = m5602_start_transfer,
  289. .stopN = m5602_stop_transfer,
  290. .pkt_scan = m5602_urb_complete
  291. };
  292. /* this function is called at probe time */
  293. static int m5602_configure(struct gspca_dev *gspca_dev,
  294. const struct usb_device_id *id)
  295. {
  296. struct sd *sd = (struct sd *) gspca_dev;
  297. struct cam *cam;
  298. int err;
  299. cam = &gspca_dev->cam;
  300. if (dump_bridge)
  301. m5602_dump_bridge(sd);
  302. /* Probe sensor */
  303. err = m5602_probe_sensor(sd);
  304. if (err)
  305. goto fail;
  306. return 0;
  307. fail:
  308. PERR("ALi m5602 webcam failed");
  309. cam->cam_mode = NULL;
  310. cam->nmodes = 0;
  311. return err;
  312. }
  313. static int m5602_probe(struct usb_interface *intf,
  314. const struct usb_device_id *id)
  315. {
  316. return gspca_dev_probe(intf, id, &sd_desc, sizeof(struct sd),
  317. THIS_MODULE);
  318. }
  319. static void m5602_disconnect(struct usb_interface *intf)
  320. {
  321. struct gspca_dev *gspca_dev = usb_get_intfdata(intf);
  322. struct sd *sd = (struct sd *) gspca_dev;
  323. if (sd->sensor->disconnect)
  324. sd->sensor->disconnect(sd);
  325. gspca_disconnect(intf);
  326. }
  327. static struct usb_driver sd_driver = {
  328. .name = MODULE_NAME,
  329. .id_table = m5602_table,
  330. .probe = m5602_probe,
  331. #ifdef CONFIG_PM
  332. .suspend = gspca_suspend,
  333. .resume = gspca_resume,
  334. .reset_resume = gspca_resume,
  335. #endif
  336. .disconnect = m5602_disconnect
  337. };
  338. module_usb_driver(sd_driver);
  339. MODULE_AUTHOR(DRIVER_AUTHOR);
  340. MODULE_DESCRIPTION(DRIVER_DESC);
  341. MODULE_LICENSE("GPL");
  342. module_param(force_sensor, int, S_IRUGO | S_IWUSR);
  343. MODULE_PARM_DESC(force_sensor,
  344. "forces detection of a sensor, "
  345. "1 = OV9650, 2 = S5K83A, 3 = S5K4AA, "
  346. "4 = MT9M111, 5 = PO1030, 6 = OV7660");
  347. module_param(dump_bridge, bool, S_IRUGO | S_IWUSR);
  348. MODULE_PARM_DESC(dump_bridge, "Dumps all usb bridge registers at startup");
  349. module_param(dump_sensor, bool, S_IRUGO | S_IWUSR);
  350. MODULE_PARM_DESC(dump_sensor, "Dumps all usb sensor registers "
  351. "at startup providing a sensor is found");