isight_firmware.c 3.3 KB

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