blob: 4d30095d6ad2193a6d6064891b67dda2a19ebb75 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Matthew Garrett62d104d2008-05-20 20:06:28 +01002/*
3 * Driver for loading USB isight firmware
4 *
5 * Copyright (C) 2008 Matthew Garrett <mjg@redhat.com>
6 *
Matthew Garrett62d104d2008-05-20 20:06:28 +01007 * 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
21#include <linux/usb.h>
22#include <linux/firmware.h>
23#include <linux/errno.h>
24#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Matthew Garrett62d104d2008-05-20 20:06:28 +010026
Németh Márton33b9e162010-01-10 15:34:45 +010027static const struct usb_device_id id_table[] = {
Matthew Garrett62d104d2008-05-20 20:06:28 +010028 {USB_DEVICE(0x05ac, 0x8300)},
29 {},
30};
31
32MODULE_DEVICE_TABLE(usb, id_table);
33
34static int isight_firmware_load(struct usb_interface *intf,
35 const struct usb_device_id *id)
36{
37 struct usb_device *dev = interface_to_usbdev(intf);
38 int llen, len, req, ret = 0;
39 const struct firmware *firmware;
Matthew Garrett62b58842008-06-06 12:35:15 -070040 unsigned char *buf = kmalloc(50, GFP_KERNEL);
Matthew Garrett62d104d2008-05-20 20:06:28 +010041 unsigned char data[4];
gregkh@suse.deed5a2822008-05-29 10:17:38 -070042 const u8 *ptr;
Matthew Garrett62b58842008-06-06 12:35:15 -070043
44 if (!buf)
45 return -ENOMEM;
Matthew Garrett62d104d2008-05-20 20:06:28 +010046
47 if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
48 printk(KERN_ERR "Unable to load isight firmware\n");
Parag Warudkarff1a4a72008-08-12 15:08:46 -070049 ret = -ENODEV;
50 goto out;
Matthew Garrett62d104d2008-05-20 20:06:28 +010051 }
52
53 ptr = firmware->data;
54
Greg Kroah-Hartman59bf5cf2011-12-05 14:02:59 -080055 buf[0] = 0x01;
Matthew Garrett62d104d2008-05-20 20:06:28 +010056 if (usb_control_msg
Greg Kroah-Hartman59bf5cf2011-12-05 14:02:59 -080057 (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
Matthew Garrett62d104d2008-05-20 20:06:28 +010058 300) != 1) {
59 printk(KERN_ERR
60 "Failed to initialise isight firmware loader\n");
61 ret = -ENODEV;
62 goto out;
63 }
64
Matthew Garrett62b58842008-06-06 12:35:15 -070065 while (ptr+4 <= firmware->data+firmware->size) {
Matthew Garrett62d104d2008-05-20 20:06:28 +010066 memcpy(data, ptr, 4);
67 len = (data[0] << 8 | data[1]);
68 req = (data[2] << 8 | data[3]);
69 ptr += 4;
70
71 if (len == 0x8001)
72 break; /* success */
73 else if (len == 0)
74 continue;
75
76 for (; len > 0; req += 50) {
Matthew Garrett62b58842008-06-06 12:35:15 -070077 llen = min(len, 50);
Matthew Garrett62d104d2008-05-20 20:06:28 +010078 len -= llen;
Matthew Garrett62b58842008-06-06 12:35:15 -070079 if (ptr+llen > firmware->data+firmware->size) {
80 printk(KERN_ERR
81 "Malformed isight firmware");
82 ret = -ENODEV;
83 goto out;
84 }
Matthew Garrett62d104d2008-05-20 20:06:28 +010085 memcpy(buf, ptr, llen);
86
87 ptr += llen;
88
89 if (usb_control_msg
90 (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
91 buf, llen, 300) != llen) {
92 printk(KERN_ERR
93 "Failed to load isight firmware\n");
Matthew Garrett62d104d2008-05-20 20:06:28 +010094 ret = -ENODEV;
95 goto out;
96 }
97
Matthew Garrett62d104d2008-05-20 20:06:28 +010098 }
99 }
Matthew Garrett62b58842008-06-06 12:35:15 -0700100
Greg Kroah-Hartman59bf5cf2011-12-05 14:02:59 -0800101 buf[0] = 0x00;
Matthew Garrett62d104d2008-05-20 20:06:28 +0100102 if (usb_control_msg
Greg Kroah-Hartman59bf5cf2011-12-05 14:02:59 -0800103 (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
Matthew Garrett62d104d2008-05-20 20:06:28 +0100104 300) != 1) {
105 printk(KERN_ERR "isight firmware loading completion failed\n");
106 ret = -ENODEV;
107 }
Matthew Garrett62b58842008-06-06 12:35:15 -0700108
Matthew Garrett62d104d2008-05-20 20:06:28 +0100109out:
Matthew Garrett62b58842008-06-06 12:35:15 -0700110 kfree(buf);
Matthew Garrett62d104d2008-05-20 20:06:28 +0100111 release_firmware(firmware);
112 return ret;
113}
114
Ben Hutchings4e0961d2010-01-13 23:39:43 +0000115MODULE_FIRMWARE("isight.fw");
116
Matthew Garrett62d104d2008-05-20 20:06:28 +0100117static void isight_firmware_disconnect(struct usb_interface *intf)
118{
119}
120
121static struct usb_driver isight_firmware_driver = {
122 .name = "isight_firmware",
123 .probe = isight_firmware_load,
124 .disconnect = isight_firmware_disconnect,
125 .id_table = id_table,
126};
127
Greg Kroah-Hartman65db4302011-11-18 09:34:02 -0800128module_usb_driver(isight_firmware_driver);
Matthew Garrett62d104d2008-05-20 20:06:28 +0100129
130MODULE_LICENSE("GPL");
131MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");