isight_firmware.c 3.1 KB

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