Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Driver for loading USB isight firmware
0004  *
0005  * Copyright (C) 2008 Matthew Garrett <mjg@redhat.com>
0006  *
0007  * The USB isight cameras in recent Apples are roughly compatible with the USB
0008  * video class specification, and can be driven by uvcvideo. However, they
0009  * need firmware to be loaded beforehand. After firmware loading, the device
0010  * detaches from the USB bus and reattaches with a new device ID. It can then
0011  * be claimed by the uvc driver.
0012  *
0013  * The firmware is non-free and must be extracted by the user. Tools to do this
0014  * are available at http://bersace03.free.fr/ift/
0015  *
0016  * The isight firmware loading was reverse engineered by Johannes Berg
0017  * <johannes@sipsolutions.de>, and this driver is based on code by Ronald
0018  * Bultje <rbultje@ronald.bitfreak.net>
0019  */
0020 
0021 #include <linux/usb.h>
0022 #include <linux/firmware.h>
0023 #include <linux/errno.h>
0024 #include <linux/module.h>
0025 #include <linux/slab.h>
0026 
0027 static const struct usb_device_id id_table[] = {
0028     {USB_DEVICE(0x05ac, 0x8300)},
0029     {},
0030 };
0031 
0032 MODULE_DEVICE_TABLE(usb, id_table);
0033 
0034 static int isight_firmware_load(struct usb_interface *intf,
0035                 const struct usb_device_id *id)
0036 {
0037     struct usb_device *dev = interface_to_usbdev(intf);
0038     int llen, len, req, ret = 0;
0039     const struct firmware *firmware;
0040     unsigned char *buf = kmalloc(50, GFP_KERNEL);
0041     unsigned char data[4];
0042     const u8 *ptr;
0043 
0044     if (!buf)
0045         return -ENOMEM;
0046 
0047     if (request_firmware(&firmware, "isight.fw", &dev->dev) != 0) {
0048         printk(KERN_ERR "Unable to load isight firmware\n");
0049         ret = -ENODEV;
0050         goto out;
0051     }
0052 
0053     ptr = firmware->data;
0054 
0055     buf[0] = 0x01;
0056     if (usb_control_msg
0057         (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
0058          300) != 1) {
0059         printk(KERN_ERR
0060                "Failed to initialise isight firmware loader\n");
0061         ret = -ENODEV;
0062         goto out;
0063     }
0064 
0065     while (ptr+4 <= firmware->data+firmware->size) {
0066         memcpy(data, ptr, 4);
0067         len = (data[0] << 8 | data[1]);
0068         req = (data[2] << 8 | data[3]);
0069         ptr += 4;
0070 
0071         if (len == 0x8001)
0072             break;  /* success */
0073         else if (len == 0)
0074             continue;
0075 
0076         for (; len > 0; req += 50) {
0077             llen = min(len, 50);
0078             len -= llen;
0079             if (ptr+llen > firmware->data+firmware->size) {
0080                 printk(KERN_ERR
0081                        "Malformed isight firmware");
0082                 ret = -ENODEV;
0083                 goto out;
0084             }
0085             memcpy(buf, ptr, llen);
0086 
0087             ptr += llen;
0088 
0089             if (usb_control_msg
0090                 (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, req, 0,
0091                  buf, llen, 300) != llen) {
0092                 printk(KERN_ERR
0093                        "Failed to load isight firmware\n");
0094                 ret = -ENODEV;
0095                 goto out;
0096             }
0097 
0098         }
0099     }
0100 
0101     buf[0] = 0x00;
0102     if (usb_control_msg
0103         (dev, usb_sndctrlpipe(dev, 0), 0xa0, 0x40, 0xe600, 0, buf, 1,
0104          300) != 1) {
0105         printk(KERN_ERR "isight firmware loading completion failed\n");
0106         ret = -ENODEV;
0107     }
0108 
0109 out:
0110     kfree(buf);
0111     release_firmware(firmware);
0112     return ret;
0113 }
0114 
0115 MODULE_FIRMWARE("isight.fw");
0116 
0117 static void isight_firmware_disconnect(struct usb_interface *intf)
0118 {
0119 }
0120 
0121 static struct usb_driver isight_firmware_driver = {
0122     .name = "isight_firmware",
0123     .probe = isight_firmware_load,
0124     .disconnect = isight_firmware_disconnect,
0125     .id_table = id_table,
0126 };
0127 
0128 module_usb_driver(isight_firmware_driver);
0129 
0130 MODULE_LICENSE("GPL");
0131 MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");