isight_firmware.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. * Driver for loading USB isight firmware
  3. *
  4. * Copyright (C) 2008 Matthew Garrett <mjg@redhat.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the Free
  8. * Software Foundation, version 2.
  9. *
  10. * The USB isight cameras in recent Apples are roughly compatible with the USB
  11. * video class specification, and can be driven by uvcvideo. However, they
  12. * need firmware to be loaded beforehand. After firmware loading, the device
  13. * detaches from the USB bus and reattaches with a new device ID. It can then
  14. * be claimed by the uvc driver.
  15. *
  16. * The firmware is non-free and must be extracted by the user. Tools to do this
  17. * are available at http://bersace03.free.fr/ift/
  18. *
  19. * The isight firmware loading was reverse engineered by Johannes Berg
  20. * <johannes@sipsolutions.de>, and this driver is based on code by Ronald
  21. * Bultje <rbultje@ronald.bitfreak.net>
  22. */
  23. #include <linux/usb.h>
  24. #include <linux/firmware.h>
  25. #include <linux/errno.h>
  26. #include <linux/module.h>
  27. static struct usb_device_id id_table[] = {
  28. {USB_DEVICE(0x05ac, 0x8300)},
  29. {},
  30. };
  31. MODULE_DEVICE_TABLE(usb, id_table);
  32. static int isight_firmware_load(struct usb_interface *intf,
  33. const struct usb_device_id *id)
  34. {
  35. struct usb_device *dev = interface_to_usbdev(intf);
  36. int llen, len, req, ret = 0;
  37. const struct firmware *firmware;
  38. unsigned char *buf;
  39. unsigned char data[4];
  40. char *ptr;
  41. if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
  42. printk(KERN_ERR "Unable to load isight firmware\n");
  43. return -ENODEV;
  44. }
  45. ptr = firmware->data;
  46. if (usb_control_msg
  47. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, "\1", 1,
  48. 300) != 1) {
  49. printk(KERN_ERR
  50. "Failed to initialise isight firmware loader\n");
  51. ret = -ENODEV;
  52. goto out;
  53. }
  54. while (1) {
  55. memcpy(data, ptr, 4);
  56. len = (data[0] << 8 | data[1]);
  57. req = (data[2] << 8 | data[3]);
  58. ptr += 4;
  59. if (len == 0x8001)
  60. break; /* success */
  61. else if (len == 0)
  62. continue;
  63. for (; len > 0; req += 50) {
  64. llen = len > 50 ? 50 : len;
  65. len -= llen;
  66. buf = kmalloc(llen, GFP_KERNEL);
  67. memcpy(buf, ptr, llen);
  68. ptr += llen;
  69. if (usb_control_msg
  70. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
  71. buf, llen, 300) != llen) {
  72. printk(KERN_ERR
  73. "Failed to load isight firmware\n");
  74. kfree(buf);
  75. ret = -ENODEV;
  76. goto out;
  77. }
  78. kfree(buf);
  79. }
  80. }
  81. if (usb_control_msg
  82. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, "\0", 1,
  83. 300) != 1) {
  84. printk(KERN_ERR "isight firmware loading completion failed\n");
  85. ret = -ENODEV;
  86. }
  87. out:
  88. release_firmware(firmware);
  89. return ret;
  90. }
  91. static void isight_firmware_disconnect(struct usb_interface *intf)
  92. {
  93. }
  94. static struct usb_driver isight_firmware_driver = {
  95. .name = "isight_firmware",
  96. .probe = isight_firmware_load,
  97. .disconnect = isight_firmware_disconnect,
  98. .id_table = id_table,
  99. };
  100. static int __init isight_firmware_init(void)
  101. {
  102. return usb_register(&isight_firmware_driver);
  103. }
  104. static void __exit isight_firmware_exit(void)
  105. {
  106. usb_deregister(&isight_firmware_driver);
  107. }
  108. module_init(isight_firmware_init);
  109. module_exit(isight_firmware_exit);
  110. MODULE_LICENSE("GPL");
  111. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");