root/drivers/usb/misc/ezusb.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ezusb_writememory
  2. ezusb_set_reset
  3. ezusb_fx1_set_reset
  4. ezusb_ihex_firmware_download
  5. ezusb_fx1_ihex_firmware_download
  6. ezusb_fx2_set_reset
  7. ezusb_fx2_ihex_firmware_download

   1 // SPDX-License-Identifier: GPL-2.0
   2 /*
   3  * EZ-USB specific functions used by some of the USB to Serial drivers.
   4  *
   5  * Copyright (C) 1999 - 2002 Greg Kroah-Hartman (greg@kroah.com)
   6  */
   7 
   8 #include <linux/kernel.h>
   9 #include <linux/slab.h>
  10 #include <linux/module.h>
  11 #include <linux/usb.h>
  12 #include <linux/firmware.h>
  13 #include <linux/ihex.h>
  14 #include <linux/usb/ezusb.h>
  15 
  16 struct ezusb_fx_type {
  17         /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
  18         unsigned short cpucs_reg;
  19         unsigned short max_internal_adress;
  20 };
  21 
  22 static const struct ezusb_fx_type ezusb_fx1 = {
  23         .cpucs_reg = 0x7F92,
  24         .max_internal_adress = 0x1B3F,
  25 };
  26 
  27 /* Commands for writing to memory */
  28 #define WRITE_INT_RAM 0xA0
  29 #define WRITE_EXT_RAM 0xA3
  30 
  31 static int ezusb_writememory(struct usb_device *dev, int address,
  32                                 unsigned char *data, int length, __u8 request)
  33 {
  34         int result;
  35         unsigned char *transfer_buffer;
  36 
  37         if (!dev)
  38                 return -ENODEV;
  39 
  40         transfer_buffer = kmemdup(data, length, GFP_KERNEL);
  41         if (!transfer_buffer) {
  42                 dev_err(&dev->dev, "%s - kmalloc(%d) failed.\n",
  43                                                         __func__, length);
  44                 return -ENOMEM;
  45         }
  46         result = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
  47                                  USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
  48                                  address, 0, transfer_buffer, length, 3000);
  49 
  50         kfree(transfer_buffer);
  51         return result;
  52 }
  53 
  54 static int ezusb_set_reset(struct usb_device *dev, unsigned short cpucs_reg,
  55                            unsigned char reset_bit)
  56 {
  57         int response = ezusb_writememory(dev, cpucs_reg, &reset_bit, 1, WRITE_INT_RAM);
  58         if (response < 0)
  59                 dev_err(&dev->dev, "%s-%d failed: %d\n",
  60                                                 __func__, reset_bit, response);
  61         return response;
  62 }
  63 
  64 int ezusb_fx1_set_reset(struct usb_device *dev, unsigned char reset_bit)
  65 {
  66         return ezusb_set_reset(dev, ezusb_fx1.cpucs_reg, reset_bit);
  67 }
  68 EXPORT_SYMBOL_GPL(ezusb_fx1_set_reset);
  69 
  70 static int ezusb_ihex_firmware_download(struct usb_device *dev,
  71                                         struct ezusb_fx_type fx,
  72                                         const char *firmware_path)
  73 {
  74         int ret = -ENOENT;
  75         const struct firmware *firmware = NULL;
  76         const struct ihex_binrec *record;
  77 
  78         if (request_ihex_firmware(&firmware, firmware_path,
  79                                   &dev->dev)) {
  80                 dev_err(&dev->dev,
  81                         "%s - request \"%s\" failed\n",
  82                         __func__, firmware_path);
  83                 goto out;
  84         }
  85 
  86         ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
  87         if (ret < 0)
  88                 goto out;
  89 
  90         record = (const struct ihex_binrec *)firmware->data;
  91         for (; record; record = ihex_next_binrec(record)) {
  92                 if (be32_to_cpu(record->addr) > fx.max_internal_adress) {
  93                         ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
  94                                                 (unsigned char *)record->data,
  95                                                 be16_to_cpu(record->len), WRITE_EXT_RAM);
  96                         if (ret < 0) {
  97                                 dev_err(&dev->dev, "%s - ezusb_writememory "
  98                                         "failed writing internal memory "
  99                                         "(%d %04X %p %d)\n", __func__, ret,
 100                                         be32_to_cpu(record->addr), record->data,
 101                                         be16_to_cpu(record->len));
 102                                 goto out;
 103                         }
 104                 }
 105         }
 106 
 107         ret = ezusb_set_reset(dev, fx.cpucs_reg, 1);
 108         if (ret < 0)
 109                 goto out;
 110         record = (const struct ihex_binrec *)firmware->data;
 111         for (; record; record = ihex_next_binrec(record)) {
 112                 if (be32_to_cpu(record->addr) <= fx.max_internal_adress) {
 113                         ret = ezusb_writememory(dev, be32_to_cpu(record->addr),
 114                                                 (unsigned char *)record->data,
 115                                                 be16_to_cpu(record->len), WRITE_INT_RAM);
 116                         if (ret < 0) {
 117                                 dev_err(&dev->dev, "%s - ezusb_writememory "
 118                                         "failed writing external memory "
 119                                         "(%d %04X %p %d)\n", __func__, ret,
 120                                         be32_to_cpu(record->addr), record->data,
 121                                         be16_to_cpu(record->len));
 122                                 goto out;
 123                         }
 124                 }
 125         }
 126         ret = ezusb_set_reset(dev, fx.cpucs_reg, 0);
 127 out:
 128         release_firmware(firmware);
 129         return ret;
 130 }
 131 
 132 int ezusb_fx1_ihex_firmware_download(struct usb_device *dev,
 133                                      const char *firmware_path)
 134 {
 135         return ezusb_ihex_firmware_download(dev, ezusb_fx1, firmware_path);
 136 }
 137 EXPORT_SYMBOL_GPL(ezusb_fx1_ihex_firmware_download);
 138 
 139 #if 0
 140 /*
 141  * Once someone one needs these fx2 functions, uncomment them
 142  * and add them to ezusb.h and all should be good.
 143  */
 144 static struct ezusb_fx_type ezusb_fx2 = {
 145         .cpucs_reg = 0xE600,
 146         .max_internal_adress = 0x3FFF,
 147 };
 148 
 149 int ezusb_fx2_set_reset(struct usb_device *dev, unsigned char reset_bit)
 150 {
 151         return ezusb_set_reset(dev, ezusb_fx2.cpucs_reg, reset_bit);
 152 }
 153 EXPORT_SYMBOL_GPL(ezusb_fx2_set_reset);
 154 
 155 int ezusb_fx2_ihex_firmware_download(struct usb_device *dev,
 156                                      const char *firmware_path)
 157 {
 158         return ezusb_ihex_firmware_download(dev, ezusb_fx2, firmware_path);
 159 }
 160 EXPORT_SYMBOL_GPL(ezusb_fx2_ihex_firmware_download);
 161 #endif
 162 
 163 MODULE_LICENSE("GPL");

/* [<][>][^][v][top][bottom][index][help] */