isight_firmware.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include <linux/slab.h>
  28. static const struct usb_device_id id_table[] = {
  29. {USB_DEVICE(0x05ac, 0x8300)},
  30. {},
  31. };
  32. MODULE_DEVICE_TABLE(usb, id_table);
  33. static int isight_firmware_load(struct usb_interface *intf,
  34. const struct usb_device_id *id)
  35. {
  36. struct usb_device *dev = interface_to_usbdev(intf);
  37. int llen, len, req, ret = 0;
  38. const struct firmware *firmware;
  39. unsigned char *buf = kmalloc(50, GFP_KERNEL);
  40. unsigned char data[4];
  41. const u8 *ptr;
  42. if (!buf)
  43. return -ENOMEM;
  44. if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
  45. printk(KERN_ERR "Unable to load isight firmware\n");
  46. ret = -ENODEV;
  47. goto out;
  48. }
  49. ptr = firmware->data;
  50. if (usb_control_msg
  51. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, "\1", 1,
  52. 300) != 1) {
  53. printk(KERN_ERR
  54. "Failed to initialise isight firmware loader\n");
  55. ret = -ENODEV;
  56. goto out;
  57. }
  58. while (ptr+4 <= firmware->data+firmware->size) {
  59. memcpy(data, ptr, 4);
  60. len = (data[0] << 8 | data[1]);
  61. req = (data[2] << 8 | data[3]);
  62. ptr += 4;
  63. if (len == 0x8001)
  64. break; /* success */
  65. else if (len == 0)
  66. continue;
  67. for (; len > 0; req += 50) {
  68. llen = min(len, 50);
  69. len -= llen;
  70. if (ptr+llen > firmware->data+firmware->size) {
  71. printk(KERN_ERR
  72. "Malformed isight firmware");
  73. ret = -ENODEV;
  74. goto out;
  75. }
  76. memcpy(buf, ptr, llen);
  77. ptr += llen;
  78. if (usb_control_msg
  79. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
  80. buf, llen, 300) != llen) {
  81. printk(KERN_ERR
  82. "Failed to load isight firmware\n");
  83. ret = -ENODEV;
  84. goto out;
  85. }
  86. }
  87. }
  88. if (usb_control_msg
  89. (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, "\0", 1,
  90. 300) != 1) {
  91. printk(KERN_ERR "isight firmware loading completion failed\n");
  92. ret = -ENODEV;
  93. }
  94. out:
  95. kfree(buf);
  96. release_firmware(firmware);
  97. return ret;
  98. }
  99. MODULE_FIRMWARE("isight.fw");
  100. static void isight_firmware_disconnect(struct usb_interface *intf)
  101. {
  102. }
  103. static struct usb_driver isight_firmware_driver = {
  104. .name = "isight_firmware",
  105. .probe = isight_firmware_load,
  106. .disconnect = isight_firmware_disconnect,
  107. .id_table = id_table,
  108. };
  109. static int __init isight_firmware_init(void)
  110. {
  111. return usb_register(&isight_firmware_driver);
  112. }
  113. static void __exit isight_firmware_exit(void)
  114. {
  115. usb_deregister(&isight_firmware_driver);
  116. }
  117. module_init(isight_firmware_init);
  118. module_exit(isight_firmware_exit);
  119. MODULE_LICENSE("GPL");
  120. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");