Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 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 | |
| 24 | #include <linux/usb.h> |
| 25 | #include <linux/firmware.h> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/module.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 28 | #include <linux/slab.h> |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 29 | |
Németh Márton | 33b9e16 | 2010-01-10 15:34:45 +0100 | [diff] [blame] | 30 | static const struct usb_device_id id_table[] = { |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 31 | {USB_DEVICE(0x05ac, 0x8300)}, |
| 32 | {}, |
| 33 | }; |
| 34 | |
| 35 | MODULE_DEVICE_TABLE(usb, id_table); |
| 36 | |
| 37 | static int isight_firmware_load(struct usb_interface *intf, |
| 38 | const struct usb_device_id *id) |
| 39 | { |
| 40 | struct usb_device *dev = interface_to_usbdev(intf); |
| 41 | int llen, len, req, ret = 0; |
| 42 | const struct firmware *firmware; |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 43 | unsigned char *buf = kmalloc(50, GFP_KERNEL); |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 44 | unsigned char data[4]; |
gregkh@suse.de | ed5a282 | 2008-05-29 10:17:38 -0700 | [diff] [blame] | 45 | const u8 *ptr; |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 46 | |
| 47 | if (!buf) |
| 48 | return -ENOMEM; |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 49 | |
| 50 | if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) { |
| 51 | printk(KERN_ERR "Unable to load isight firmware\n"); |
Parag Warudkar | ff1a4a7 | 2008-08-12 15:08:46 -0700 | [diff] [blame] | 52 | ret = -ENODEV; |
| 53 | goto out; |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | ptr = firmware->data; |
| 57 | |
Greg Kroah-Hartman | 59bf5cf | 2011-12-05 14:02:59 -0800 | [diff] [blame] | 58 | buf[0] = 0x01; |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 59 | if (usb_control_msg |
Greg Kroah-Hartman | 59bf5cf | 2011-12-05 14:02:59 -0800 | [diff] [blame] | 60 | (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1, |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 61 | 300) != 1) { |
| 62 | printk(KERN_ERR |
| 63 | "Failed to initialise isight firmware loader\n"); |
| 64 | ret = -ENODEV; |
| 65 | goto out; |
| 66 | } |
| 67 | |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 68 | while (ptr+4 <= firmware->data+firmware->size) { |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 69 | memcpy(data, ptr, 4); |
| 70 | len = (data[0] << 8 | data[1]); |
| 71 | req = (data[2] << 8 | data[3]); |
| 72 | ptr += 4; |
| 73 | |
| 74 | if (len == 0x8001) |
| 75 | break; /* success */ |
| 76 | else if (len == 0) |
| 77 | continue; |
| 78 | |
| 79 | for (; len > 0; req += 50) { |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 80 | llen = min(len, 50); |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 81 | len -= llen; |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 82 | if (ptr+llen > firmware->data+firmware->size) { |
| 83 | printk(KERN_ERR |
| 84 | "Malformed isight firmware"); |
| 85 | ret = -ENODEV; |
| 86 | goto out; |
| 87 | } |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 88 | memcpy(buf, ptr, llen); |
| 89 | |
| 90 | ptr += llen; |
| 91 | |
| 92 | if (usb_control_msg |
| 93 | (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0, |
| 94 | buf, llen, 300) != llen) { |
| 95 | printk(KERN_ERR |
| 96 | "Failed to load isight firmware\n"); |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 97 | ret = -ENODEV; |
| 98 | goto out; |
| 99 | } |
| 100 | |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 101 | } |
| 102 | } |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 103 | |
Greg Kroah-Hartman | 59bf5cf | 2011-12-05 14:02:59 -0800 | [diff] [blame] | 104 | buf[0] = 0x00; |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 105 | if (usb_control_msg |
Greg Kroah-Hartman | 59bf5cf | 2011-12-05 14:02:59 -0800 | [diff] [blame] | 106 | (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1, |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 107 | 300) != 1) { |
| 108 | printk(KERN_ERR "isight firmware loading completion failed\n"); |
| 109 | ret = -ENODEV; |
| 110 | } |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 111 | |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 112 | out: |
Matthew Garrett | 62b5884 | 2008-06-06 12:35:15 -0700 | [diff] [blame] | 113 | kfree(buf); |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 114 | release_firmware(firmware); |
| 115 | return ret; |
| 116 | } |
| 117 | |
Ben Hutchings | 4e0961d | 2010-01-13 23:39:43 +0000 | [diff] [blame] | 118 | MODULE_FIRMWARE("isight.fw"); |
| 119 | |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 120 | static void isight_firmware_disconnect(struct usb_interface *intf) |
| 121 | { |
| 122 | } |
| 123 | |
| 124 | static struct usb_driver isight_firmware_driver = { |
| 125 | .name = "isight_firmware", |
| 126 | .probe = isight_firmware_load, |
| 127 | .disconnect = isight_firmware_disconnect, |
| 128 | .id_table = id_table, |
| 129 | }; |
| 130 | |
Greg Kroah-Hartman | 65db430 | 2011-11-18 09:34:02 -0800 | [diff] [blame] | 131 | module_usb_driver(isight_firmware_driver); |
Matthew Garrett | 62d104d | 2008-05-20 20:06:28 +0100 | [diff] [blame] | 132 | |
| 133 | MODULE_LICENSE("GPL"); |
| 134 | MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>"); |