xref: /linux/drivers/usb/misc/isight_firmware.c (revision 5fd54ace4721fc5ce2bb5aef6318fcf17f421460)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for loading USB isight firmware
4  *
5  * Copyright (C) 2008 Matthew Garrett <mjg@redhat.com>
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation, version 2.
10  *
11  * The USB isight cameras in recent Apples are roughly compatible with the USB
12  * video class specification, and can be driven by uvcvideo. However, they
13  * need firmware to be loaded beforehand. After firmware loading, the device
14  * detaches from the USB bus and reattaches with a new device ID. It can then
15  * be claimed by the uvc driver.
16  *
17  * The firmware is non-free and must be extracted by the user. Tools to do this
18  * are available at http://bersace03.free.fr/ift/
19  *
20  * The isight firmware loading was reverse engineered by Johannes Berg
21  * <johannes@sipsolutions.de>, and this driver is based on code by Ronald
22  * Bultje <rbultje@ronald.bitfreak.net>
23  */
24 
25 #include <linux/usb.h>
26 #include <linux/firmware.h>
27 #include <linux/errno.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 
31 static const struct usb_device_id id_table[] = {
32 	{USB_DEVICE(0x05ac, 0x8300)},
33 	{},
34 };
35 
36 MODULE_DEVICE_TABLE(usb, id_table);
37 
38 static int isight_firmware_load(struct usb_interface *intf,
39 				const struct usb_device_id *id)
40 {
41 	struct usb_device *dev = interface_to_usbdev(intf);
42 	int llen, len, req, ret = 0;
43 	const struct firmware *firmware;
44 	unsigned char *buf = kmalloc(50, GFP_KERNEL);
45 	unsigned char data[4];
46 	const u8 *ptr;
47 
48 	if (!buf)
49 		return -ENOMEM;
50 
51 	if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
52 		printk(KERN_ERR "Unable to load isight firmware\n");
53 		ret = -ENODEV;
54 		goto out;
55 	}
56 
57 	ptr = firmware->data;
58 
59 	buf[0] = 0x01;
60 	if (usb_control_msg
61 	    (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
62 	     300) != 1) {
63 		printk(KERN_ERR
64 		       "Failed to initialise isight firmware loader\n");
65 		ret = -ENODEV;
66 		goto out;
67 	}
68 
69 	while (ptr+4 <= firmware->data+firmware->size) {
70 		memcpy(data, ptr, 4);
71 		len = (data[0] << 8 | data[1]);
72 		req = (data[2] << 8 | data[3]);
73 		ptr += 4;
74 
75 		if (len == 0x8001)
76 			break;	/* success */
77 		else if (len == 0)
78 			continue;
79 
80 		for (; len > 0; req += 50) {
81 			llen = min(len, 50);
82 			len -= llen;
83 			if (ptr+llen > firmware->data+firmware->size) {
84 				printk(KERN_ERR
85 				       "Malformed isight firmware");
86 				ret = -ENODEV;
87 				goto out;
88 			}
89 			memcpy(buf, ptr, llen);
90 
91 			ptr += llen;
92 
93 			if (usb_control_msg
94 			    (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
95 			     buf, llen, 300) != llen) {
96 				printk(KERN_ERR
97 				       "Failed to load isight firmware\n");
98 				ret = -ENODEV;
99 				goto out;
100 			}
101 
102 		}
103 	}
104 
105 	buf[0] = 0x00;
106 	if (usb_control_msg
107 	    (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
108 	     300) != 1) {
109 		printk(KERN_ERR "isight firmware loading completion failed\n");
110 		ret = -ENODEV;
111 	}
112 
113 out:
114 	kfree(buf);
115 	release_firmware(firmware);
116 	return ret;
117 }
118 
119 MODULE_FIRMWARE("isight.fw");
120 
121 static void isight_firmware_disconnect(struct usb_interface *intf)
122 {
123 }
124 
125 static struct usb_driver isight_firmware_driver = {
126 	.name = "isight_firmware",
127 	.probe = isight_firmware_load,
128 	.disconnect = isight_firmware_disconnect,
129 	.id_table = id_table,
130 };
131 
132 module_usb_driver(isight_firmware_driver);
133 
134 MODULE_LICENSE("GPL");
135 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
136