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