isight_firmware.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 = kmalloc(50, GFP_KERNEL);
  39. unsigned char data[4];
  40. const u8 *ptr;
  41. if (!buf)
  42. return -ENOMEM;
  43. if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
  44. printk(KERN_ERR "Unable to load isight firmware\n");
  45. ret = -ENODEV;
  46. goto out;
  47. }
  48. ptr = firmware->data;
  49. if (usb_control_msg
  50. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, "\1", 1,
  51. 300) != 1) {
  52. printk(KERN_ERR
  53. "Failed to initialise isight firmware loader\n");
  54. ret = -ENODEV;
  55. goto out;
  56. }
  57. while (ptr+4 <= firmware->data+firmware->size) {
  58. memcpy(data, ptr, 4);
  59. len = (data[0] << 8 | data[1]);
  60. req = (data[2] << 8 | data[3]);
  61. ptr += 4;
  62. if (len == 0x8001)
  63. break; /* success */
  64. else if (len == 0)
  65. continue;
  66. for (; len > 0; req += 50) {
  67. llen = min(len, 50);
  68. len -= llen;
  69. if (ptr+llen > firmware->data+firmware->size) {
  70. printk(KERN_ERR
  71. "Malformed isight firmware");
  72. ret = -ENODEV;
  73. goto out;
  74. }
  75. memcpy(buf, ptr, llen);
  76. ptr += llen;
  77. if (usb_control_msg
  78. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
  79. buf, llen, 300) != llen) {
  80. printk(KERN_ERR
  81. "Failed to load isight firmware\n");
  82. ret = -ENODEV;
  83. goto out;
  84. }
  85. }
  86. }
  87. if (usb_control_msg
  88. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, "\0", 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. 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. static int __init isight_firmware_init(void)
  108. {
  109. return usb_register(&isight_firmware_driver);
  110. }
  111. static void __exit isight_firmware_exit(void)
  112. {
  113. usb_deregister(&isight_firmware_driver);
  114. }
  115. module_init(isight_firmware_init);
  116. module_exit(isight_firmware_exit);
  117. MODULE_LICENSE("GPL");
  118. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");