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 <mjg@redhat.com>
  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. * <johannes@sipsolutions.de>, and this driver is based on code by Ronald
  18. * Bultje <rbultje@ronald.bitfreak.net>
  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. ret = -ENODEV;
  43. goto out;
  44. }
  45. ptr = firmware->data;
  46. buf[0] = 0x01;
  47. if (usb_control_msg
  48. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
  49. 300) != 1) {
  50. printk(KERN_ERR
  51. "Failed to initialise isight firmware loader\n");
  52. ret = -ENODEV;
  53. goto out;
  54. }
  55. while (ptr+4 <= firmware->data+firmware->size) {
  56. memcpy(data, ptr, 4);
  57. len = (data[0] << 8 | data[1]);
  58. req = (data[2] << 8 | data[3]);
  59. ptr += 4;
  60. if (len == 0x8001)
  61. break; /* success */
  62. else if (len == 0)
  63. continue;
  64. for (; len > 0; req += 50) {
  65. llen = min(len, 50);
  66. len -= llen;
  67. if (ptr+llen > firmware->data+firmware->size) {
  68. printk(KERN_ERR
  69. "Malformed isight firmware");
  70. ret = -ENODEV;
  71. goto out;
  72. }
  73. memcpy(buf, ptr, llen);
  74. ptr += llen;
  75. if (usb_control_msg
  76. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
  77. buf, llen, 300) != llen) {
  78. printk(KERN_ERR
  79. "Failed to load isight firmware\n");
  80. ret = -ENODEV;
  81. goto out;
  82. }
  83. }
  84. }
  85. buf[0] = 0x00;
  86. if (usb_control_msg
  87. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
  88. 300) != 1) {
  89. printk(KERN_ERR "isight firmware loading completion failed\n");
  90. ret = -ENODEV;
  91. }
  92. out:
  93. kfree(buf);
  94. release_firmware(firmware);
  95. return ret;
  96. }
  97. MODULE_FIRMWARE("isight.fw");
  98. static void isight_firmware_disconnect(struct usb_interface *intf)
  99. {
  100. }
  101. static struct usb_driver isight_firmware_driver = {
  102. .name = "isight_firmware",
  103. .probe = isight_firmware_load,
  104. .disconnect = isight_firmware_disconnect,
  105. .id_table = id_table,
  106. };
  107. module_usb_driver(isight_firmware_driver);
  108. MODULE_LICENSE("GPL");
  109. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");