Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* 
0003  * Emagic EMI 2|6 usb audio interface firmware loader.
0004  * Copyright (C) 2002
0005  *  Tapio Laxström (tapio.laxstrom@iptime.fi)
0006  *
0007  * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
0008  */
0009 #include <linux/kernel.h>
0010 #include <linux/errno.h>
0011 #include <linux/slab.h>
0012 #include <linux/module.h>
0013 #include <linux/usb.h>
0014 #include <linux/delay.h>
0015 #include <linux/firmware.h>
0016 #include <linux/ihex.h>
0017 
0018 #define EMI26_VENDOR_ID         0x086a  /* Emagic Soft-und Hardware GmBH */
0019 #define EMI26_PRODUCT_ID        0x0100  /* EMI 2|6 without firmware */
0020 #define EMI26B_PRODUCT_ID       0x0102  /* EMI 2|6 without firmware */
0021 
0022 #define ANCHOR_LOAD_INTERNAL    0xA0    /* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
0023 #define ANCHOR_LOAD_EXTERNAL    0xA3    /* This command is not implemented in the core. Requires firmware */
0024 #define ANCHOR_LOAD_FPGA    0xA5    /* This command is not implemented in the core. Requires firmware. Emagic extension */
0025 #define MAX_INTERNAL_ADDRESS    0x1B3F  /* This is the highest internal RAM address for the AN2131Q */
0026 #define CPUCS_REG       0x7F92  /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */ 
0027 #define INTERNAL_RAM(address)   (address <= MAX_INTERNAL_ADDRESS)
0028 
0029 static int emi26_writememory( struct usb_device *dev, int address,
0030                   const unsigned char *data, int length,
0031                   __u8 bRequest);
0032 static int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
0033 static int emi26_load_firmware (struct usb_device *dev);
0034 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
0035 static void emi26_disconnect(struct usb_interface *intf);
0036 
0037 /* thanks to drivers/usb/serial/keyspan_pda.c code */
0038 static int emi26_writememory (struct usb_device *dev, int address,
0039                   const unsigned char *data, int length,
0040                   __u8 request)
0041 {
0042     int result;
0043     unsigned char *buffer =  kmemdup(data, length, GFP_KERNEL);
0044 
0045     if (!buffer) {
0046         dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
0047         return -ENOMEM;
0048     }
0049     /* Note: usb_control_msg returns negative value on error or length of the
0050      *       data that was written! */
0051     result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
0052     kfree (buffer);
0053     return result;
0054 }
0055 
0056 /* thanks to drivers/usb/serial/keyspan_pda.c code */
0057 static int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
0058 {
0059     int response;
0060     dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
0061     /* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
0062     response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
0063     if (response < 0) {
0064         dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
0065     }
0066     return response;
0067 }
0068 
0069 #define FW_LOAD_SIZE        1023
0070 
0071 static int emi26_load_firmware (struct usb_device *dev)
0072 {
0073     const struct firmware *loader_fw = NULL;
0074     const struct firmware *bitstream_fw = NULL;
0075     const struct firmware *firmware_fw = NULL;
0076     const struct ihex_binrec *rec;
0077     int err = -ENOMEM;
0078     int i;
0079     __u32 addr; /* Address to write */
0080     __u8 *buf;
0081 
0082     buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
0083     if (!buf)
0084         goto wraperr;
0085 
0086     err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
0087     if (err)
0088         goto nofw;
0089 
0090     err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
0091                     &dev->dev);
0092     if (err)
0093         goto nofw;
0094 
0095     err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
0096                     &dev->dev);
0097     if (err) {
0098     nofw:
0099         dev_err(&dev->dev, "%s - request_firmware() failed\n",
0100             __func__);
0101         goto wraperr;
0102     }
0103 
0104     /* Assert reset (stop the CPU in the EMI) */
0105     err = emi26_set_reset(dev,1);
0106     if (err < 0)
0107         goto wraperr;
0108 
0109     rec = (const struct ihex_binrec *)loader_fw->data;
0110     /* 1. We need to put the loader for the FPGA into the EZ-USB */
0111     while (rec) {
0112         err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0113                     rec->data, be16_to_cpu(rec->len),
0114                     ANCHOR_LOAD_INTERNAL);
0115         if (err < 0)
0116             goto wraperr;
0117         rec = ihex_next_binrec(rec);
0118     }
0119 
0120     /* De-assert reset (let the CPU run) */
0121     err = emi26_set_reset(dev,0);
0122     if (err < 0)
0123         goto wraperr;
0124     msleep(250);    /* let device settle */
0125 
0126     /* 2. We upload the FPGA firmware into the EMI
0127      * Note: collect up to 1023 (yes!) bytes and send them with
0128      * a single request. This is _much_ faster! */
0129     rec = (const struct ihex_binrec *)bitstream_fw->data;
0130     do {
0131         i = 0;
0132         addr = be32_to_cpu(rec->addr);
0133 
0134         /* intel hex records are terminated with type 0 element */
0135         while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
0136             memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
0137             i += be16_to_cpu(rec->len);
0138             rec = ihex_next_binrec(rec);
0139         }
0140         err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
0141         if (err < 0)
0142             goto wraperr;
0143     } while (rec);
0144 
0145     /* Assert reset (stop the CPU in the EMI) */
0146     err = emi26_set_reset(dev,1);
0147     if (err < 0)
0148         goto wraperr;
0149 
0150     /* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
0151     for (rec = (const struct ihex_binrec *)loader_fw->data;
0152          rec; rec = ihex_next_binrec(rec)) {
0153         err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0154                     rec->data, be16_to_cpu(rec->len),
0155                     ANCHOR_LOAD_INTERNAL);
0156         if (err < 0)
0157             goto wraperr;
0158     }
0159     msleep(250);    /* let device settle */
0160 
0161     /* De-assert reset (let the CPU run) */
0162     err = emi26_set_reset(dev,0);
0163     if (err < 0)
0164         goto wraperr;
0165 
0166     /* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
0167 
0168     for (rec = (const struct ihex_binrec *)firmware_fw->data;
0169          rec; rec = ihex_next_binrec(rec)) {
0170         if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
0171             err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0172                         rec->data, be16_to_cpu(rec->len),
0173                         ANCHOR_LOAD_EXTERNAL);
0174             if (err < 0)
0175                 goto wraperr;
0176         }
0177     }
0178 
0179     /* Assert reset (stop the CPU in the EMI) */
0180     err = emi26_set_reset(dev,1);
0181     if (err < 0)
0182         goto wraperr;
0183 
0184     for (rec = (const struct ihex_binrec *)firmware_fw->data;
0185          rec; rec = ihex_next_binrec(rec)) {
0186         if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
0187             err = emi26_writememory(dev, be32_to_cpu(rec->addr),
0188                         rec->data, be16_to_cpu(rec->len),
0189                         ANCHOR_LOAD_INTERNAL);
0190             if (err < 0)
0191                 goto wraperr;
0192         }
0193     }
0194 
0195     /* De-assert reset (let the CPU run) */
0196     err = emi26_set_reset(dev,0);
0197     if (err < 0)
0198         goto wraperr;
0199     msleep(250);    /* let device settle */
0200 
0201     /* return 1 to fail the driver inialization
0202      * and give real driver change to load */
0203     err = 1;
0204 
0205 wraperr:
0206     if (err < 0)
0207         dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
0208             __func__, err);
0209 
0210     release_firmware(loader_fw);
0211     release_firmware(bitstream_fw);
0212     release_firmware(firmware_fw);
0213 
0214     kfree(buf);
0215     return err;
0216 }
0217 
0218 static const struct usb_device_id id_table[] = {
0219     { USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
0220     { USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
0221     { }                                             /* Terminating entry */
0222 };
0223 
0224 MODULE_DEVICE_TABLE (usb, id_table);
0225 
0226 static int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
0227 {
0228     struct usb_device *dev = interface_to_usbdev(intf);
0229 
0230     dev_info(&intf->dev, "%s start\n", __func__);
0231 
0232     emi26_load_firmware(dev);
0233 
0234     /* do not return the driver context, let real audio driver do that */
0235     return -EIO;
0236 }
0237 
0238 static void emi26_disconnect(struct usb_interface *intf)
0239 {
0240 }
0241 
0242 static struct usb_driver emi26_driver = {
0243     .name       = "emi26 - firmware loader",
0244     .probe      = emi26_probe,
0245     .disconnect = emi26_disconnect,
0246     .id_table   = id_table,
0247 };
0248 
0249 module_usb_driver(emi26_driver);
0250 
0251 MODULE_AUTHOR("Tapio Laxström");
0252 MODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
0253 MODULE_LICENSE("GPL");
0254 
0255 MODULE_FIRMWARE("emi26/loader.fw");
0256 MODULE_FIRMWARE("emi26/bitstream.fw");
0257 MODULE_FIRMWARE("emi26/firmware.fw");
0258 /* vi:ai:syntax=c:sw=8:ts=8:tw=80
0259  */