15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
262d104d0SMatthew Garrett /*
362d104d0SMatthew Garrett  * Driver for loading USB isight firmware
462d104d0SMatthew Garrett  *
562d104d0SMatthew Garrett  * Copyright (C) 2008 Matthew Garrett <mjg@redhat.com>
662d104d0SMatthew Garrett  *
762d104d0SMatthew Garrett  * The USB isight cameras in recent Apples are roughly compatible with the USB
862d104d0SMatthew Garrett  * video class specification, and can be driven by uvcvideo. However, they
962d104d0SMatthew Garrett  * need firmware to be loaded beforehand. After firmware loading, the device
1062d104d0SMatthew Garrett  * detaches from the USB bus and reattaches with a new device ID. It can then
1162d104d0SMatthew Garrett  * be claimed by the uvc driver.
1262d104d0SMatthew Garrett  *
1362d104d0SMatthew Garrett  * The firmware is non-free and must be extracted by the user. Tools to do this
1462d104d0SMatthew Garrett  * are available at http://bersace03.free.fr/ift/
1562d104d0SMatthew Garrett  *
1662d104d0SMatthew Garrett  * The isight firmware loading was reverse engineered by Johannes Berg
1762d104d0SMatthew Garrett  * <johannes@sipsolutions.de>, and this driver is based on code by Ronald
1862d104d0SMatthew Garrett  * Bultje <rbultje@ronald.bitfreak.net>
1962d104d0SMatthew Garrett  */
2062d104d0SMatthew Garrett 
2162d104d0SMatthew Garrett #include <linux/usb.h>
2262d104d0SMatthew Garrett #include <linux/firmware.h>
2362d104d0SMatthew Garrett #include <linux/errno.h>
2462d104d0SMatthew Garrett #include <linux/module.h>
255a0e3ad6STejun Heo #include <linux/slab.h>
2662d104d0SMatthew Garrett 
2733b9e162SNémeth Márton static const struct usb_device_id id_table[] = {
2862d104d0SMatthew Garrett 	{USB_DEVICE(0x05ac, 0x8300)},
2962d104d0SMatthew Garrett 	{},
3062d104d0SMatthew Garrett };
3162d104d0SMatthew Garrett 
3262d104d0SMatthew Garrett MODULE_DEVICE_TABLE(usb, id_table);
3362d104d0SMatthew Garrett 
isight_firmware_load(struct usb_interface * intf,const struct usb_device_id * id)3462d104d0SMatthew Garrett static int isight_firmware_load(struct usb_interface *intf,
3562d104d0SMatthew Garrett 				const struct usb_device_id *id)
3662d104d0SMatthew Garrett {
3762d104d0SMatthew Garrett 	struct usb_device *dev = interface_to_usbdev(intf);
3862d104d0SMatthew Garrett 	int llen, len, req, ret = 0;
3962d104d0SMatthew Garrett 	const struct firmware *firmware;
4062b58848SMatthew Garrett 	unsigned char *buf = kmalloc(50, GFP_KERNEL);
4162d104d0SMatthew Garrett 	unsigned char data[4];
42ed5a2825Sgregkh@suse.de 	const u8 *ptr;
4362b58848SMatthew Garrett 
4462b58848SMatthew Garrett 	if (!buf)
4562b58848SMatthew Garrett 		return -ENOMEM;
4662d104d0SMatthew Garrett 
4762d104d0SMatthew Garrett 	if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
4862d104d0SMatthew Garrett 		printk(KERN_ERR "Unable to load isight firmware\n");
49ff1a4a7bSParag Warudkar 		ret = -ENODEV;
50ff1a4a7bSParag Warudkar 		goto out;
5162d104d0SMatthew Garrett 	}
5262d104d0SMatthew Garrett 
5362d104d0SMatthew Garrett 	ptr = firmware->data;
5462d104d0SMatthew Garrett 
5559bf5cf9SGreg Kroah-Hartman 	buf[0] = 0x01;
5662d104d0SMatthew Garrett 	if (usb_control_msg
5759bf5cf9SGreg Kroah-Hartman 	    (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
5862d104d0SMatthew Garrett 	     300) != 1) {
5962d104d0SMatthew Garrett 		printk(KERN_ERR
6062d104d0SMatthew Garrett 		       "Failed to initialise isight firmware loader\n");
6162d104d0SMatthew Garrett 		ret = -ENODEV;
6262d104d0SMatthew Garrett 		goto out;
6362d104d0SMatthew Garrett 	}
6462d104d0SMatthew Garrett 
6562b58848SMatthew Garrett 	while (ptr+4 <= firmware->data+firmware->size) {
6662d104d0SMatthew Garrett 		memcpy(data, ptr, 4);
6762d104d0SMatthew Garrett 		len = (data[0] << 8 | data[1]);
6862d104d0SMatthew Garrett 		req = (data[2] << 8 | data[3]);
6962d104d0SMatthew Garrett 		ptr += 4;
7062d104d0SMatthew Garrett 
7162d104d0SMatthew Garrett 		if (len == 0x8001)
7262d104d0SMatthew Garrett 			break;	/* success */
7362d104d0SMatthew Garrett 		else if (len == 0)
7462d104d0SMatthew Garrett 			continue;
7562d104d0SMatthew Garrett 
7662d104d0SMatthew Garrett 		for (; len > 0; req += 50) {
7762b58848SMatthew Garrett 			llen = min(len, 50);
7862d104d0SMatthew Garrett 			len -= llen;
7962b58848SMatthew Garrett 			if (ptr+llen > firmware->data+firmware->size) {
8062b58848SMatthew Garrett 				printk(KERN_ERR
8162b58848SMatthew Garrett 				       "Malformed isight firmware");
8262b58848SMatthew Garrett 				ret = -ENODEV;
8362b58848SMatthew Garrett 				goto out;
8462b58848SMatthew Garrett 			}
8562d104d0SMatthew Garrett 			memcpy(buf, ptr, llen);
8662d104d0SMatthew Garrett 
8762d104d0SMatthew Garrett 			ptr += llen;
8862d104d0SMatthew Garrett 
8962d104d0SMatthew Garrett 			if (usb_control_msg
9062d104d0SMatthew Garrett 			    (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
9162d104d0SMatthew Garrett 			     buf, llen, 300) != llen) {
9262d104d0SMatthew Garrett 				printk(KERN_ERR
9362d104d0SMatthew Garrett 				       "Failed to load isight firmware\n");
9462d104d0SMatthew Garrett 				ret = -ENODEV;
9562d104d0SMatthew Garrett 				goto out;
9662d104d0SMatthew Garrett 			}
9762d104d0SMatthew Garrett 
9862d104d0SMatthew Garrett 		}
9962d104d0SMatthew Garrett 	}
10062b58848SMatthew Garrett 
10159bf5cf9SGreg Kroah-Hartman 	buf[0] = 0x00;
10262d104d0SMatthew Garrett 	if (usb_control_msg
10359bf5cf9SGreg Kroah-Hartman 	    (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
10462d104d0SMatthew Garrett 	     300) != 1) {
10562d104d0SMatthew Garrett 		printk(KERN_ERR "isight firmware loading completion failed\n");
10662d104d0SMatthew Garrett 		ret = -ENODEV;
10762d104d0SMatthew Garrett 	}
10862b58848SMatthew Garrett 
10962d104d0SMatthew Garrett out:
11062b58848SMatthew Garrett 	kfree(buf);
11162d104d0SMatthew Garrett 	release_firmware(firmware);
11262d104d0SMatthew Garrett 	return ret;
11362d104d0SMatthew Garrett }
11462d104d0SMatthew Garrett 
1154e0961d5SBen Hutchings MODULE_FIRMWARE("isight.fw");
1164e0961d5SBen Hutchings 
isight_firmware_disconnect(struct usb_interface * intf)11762d104d0SMatthew Garrett static void isight_firmware_disconnect(struct usb_interface *intf)
11862d104d0SMatthew Garrett {
11962d104d0SMatthew Garrett }
12062d104d0SMatthew Garrett 
12162d104d0SMatthew Garrett static struct usb_driver isight_firmware_driver = {
12262d104d0SMatthew Garrett 	.name = "isight_firmware",
12362d104d0SMatthew Garrett 	.probe = isight_firmware_load,
12462d104d0SMatthew Garrett 	.disconnect = isight_firmware_disconnect,
12562d104d0SMatthew Garrett 	.id_table = id_table,
12662d104d0SMatthew Garrett };
12762d104d0SMatthew Garrett 
12865db4305SGreg Kroah-Hartman module_usb_driver(isight_firmware_driver);
12962d104d0SMatthew Garrett 
13062d104d0SMatthew Garrett MODULE_LICENSE("GPL");
13162d104d0SMatthew Garrett MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
132