Linux Audio

Check our new training course

Linux debugging, profiling, tracing and performance analysis training

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