11da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/*
21da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Emagic EMI 2|6 usb audio interface firmware loader.
31da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * Copyright (C) 2002
496de0e252cedffad61b3cb5e05662c591898e69aJan Engelhardt * 	Tapio Laxström (tapio.laxstrom@iptime.fi)
51da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
61da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * This program is free software; you can redistribute it and/or modify
71da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * it under the terms of the GNU General Public License, as published by
81da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * the Free Software Foundation, version 2.
91da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds *
101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds * emi26.c,v 1.13 2002/03/08 13:10:26 tapio Exp
111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
121da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/kernel.h>
131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/errno.h>
141da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/slab.h>
151da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/module.h>
161da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#include <linux/usb.h>
1716c23f7d88cbcce491f9370b2846fad66e8ef319Monty#include <linux/delay.h>
18ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse#include <linux/firmware.h>
19ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse#include <linux/ihex.h>
201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define EMI26_VENDOR_ID 		0x086a  /* Emagic Soft-und Hardware GmBH */
221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define EMI26_PRODUCT_ID		0x0100	/* EMI 2|6 without firmware */
231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define EMI26B_PRODUCT_ID		0x0102	/* EMI 2|6 without firmware */
241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ANCHOR_LOAD_INTERNAL	0xA0	/* Vendor specific request code for Anchor Upload/Download (This one is implemented in the core) */
261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ANCHOR_LOAD_EXTERNAL	0xA3	/* This command is not implemented in the core. Requires firmware */
271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define ANCHOR_LOAD_FPGA	0xA5	/* This command is not implemented in the core. Requires firmware. Emagic extension */
281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define MAX_INTERNAL_ADDRESS	0x1B3F	/* This is the highest internal RAM address for the AN2131Q */
291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define CPUCS_REG		0x7F92  /* EZ-USB Control and Status Register.  Bit 0 controls 8051 reset */
301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define INTERNAL_RAM(address)   (address <= MAX_INTERNAL_ADDRESS)
311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
32ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhousestatic int emi26_writememory( struct usb_device *dev, int address,
33ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			      const unsigned char *data, int length,
34ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			      __u8 bRequest);
351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int emi26_set_reset(struct usb_device *dev, unsigned char reset_bit);
361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int emi26_load_firmware (struct usb_device *dev);
371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id);
381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void emi26_disconnect(struct usb_interface *intf);
391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* thanks to drivers/usb/serial/keyspan_pda.c code */
41ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhousestatic int emi26_writememory (struct usb_device *dev, int address,
42ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			      const unsigned char *data, int length,
43ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			      __u8 request)
441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int result;
465d7efe5b3768bf53df9b87380ea68baacf11f933Eric Sesterhenn	unsigned char *buffer =  kmemdup(data, length, GFP_KERNEL);
471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (!buffer) {
49fd3f1917e345d852ef9ae36178719f4e639f70aeGreg Kroah-Hartman		dev_err(&dev->dev, "kmalloc(%d) failed.\n", length);
501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		return -ENOMEM;
511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Note: usb_control_msg returns negative value on error or length of the
531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * 		 data that was written! */
541da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	result = usb_control_msg (dev, usb_sndctrlpipe(dev, 0), request, 0x40, address, 0, buffer, length, 300);
551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree (buffer);
561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return result;
571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
581da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
591da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* thanks to drivers/usb/serial/keyspan_pda.c code */
601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int emi26_set_reset (struct usb_device *dev, unsigned char reset_bit)
611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int response;
631b29a375fb0b79a11a2d18e7bf5f6da422a35025Greg Kroah-Hartman	dev_info(&dev->dev, "%s - %d\n", __func__, reset_bit);
64441b62c1edb986827154768d89bbac0ba779984fHarvey Harrison	/* printk(KERN_DEBUG "%s - %d", __func__, reset_bit); */
651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	response = emi26_writememory (dev, CPUCS_REG, &reset_bit, 1, 0xa0);
661da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	if (response < 0) {
67fd3f1917e345d852ef9ae36178719f4e639f70aeGreg Kroah-Hartman		dev_err(&dev->dev, "set_reset (%d) failed\n", reset_bit);
681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return response;
701da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
711da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
721da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds#define FW_LOAD_SIZE		1023
731da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
741da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int emi26_load_firmware (struct usb_device *dev)
751da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
76ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	const struct firmware *loader_fw = NULL;
77ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	const struct firmware *bitstream_fw = NULL;
78ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	const struct firmware *firmware_fw = NULL;
79ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	const struct ihex_binrec *rec;
80b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	int err = -ENOMEM;
811da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	int i;
821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__u32 addr;	/* Address to write */
831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	__u8 *buf;
841da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	buf = kmalloc(FW_LOAD_SIZE, GFP_KERNEL);
86b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (!buf)
871da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto wraperr;
881da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
89ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	err = request_ihex_firmware(&loader_fw, "emi26/loader.fw", &dev->dev);
90ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	if (err)
91ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		goto nofw;
92ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse
93ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	err = request_ihex_firmware(&bitstream_fw, "emi26/bitstream.fw",
94ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse				    &dev->dev);
95ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	if (err)
96ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		goto nofw;
97ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse
98ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	err = request_ihex_firmware(&firmware_fw, "emi26/firmware.fw",
99ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse				    &dev->dev);
100ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	if (err) {
101ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	nofw:
102fd3f1917e345d852ef9ae36178719f4e639f70aeGreg Kroah-Hartman		dev_err(&dev->dev, "%s - request_firmware() failed\n",
103fd3f1917e345d852ef9ae36178719f4e639f70aeGreg Kroah-Hartman			__func__);
104ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		goto wraperr;
105ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	}
106ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse
1071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Assert reset (stop the CPU in the EMI) */
1081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = emi26_set_reset(dev,1);
109b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
1101da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto wraperr;
1111da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
112ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	rec = (const struct ihex_binrec *)loader_fw->data;
1131da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* 1. We need to put the loader for the FPGA into the EZ-USB */
114ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	while (rec) {
115ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		err = emi26_writememory(dev, be32_to_cpu(rec->addr),
116ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse					rec->data, be16_to_cpu(rec->len),
117ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse					ANCHOR_LOAD_INTERNAL);
118b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman		if (err < 0)
1191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto wraperr;
120ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		rec = ihex_next_binrec(rec);
1211da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* De-assert reset (let the CPU run) */
1241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = emi26_set_reset(dev,0);
125b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
126cf4cf0bb89cbff95c5be8f8d3c68e55f38f94ba7Oliver Neukum		goto wraperr;
12716c23f7d88cbcce491f9370b2846fad66e8ef319Monty	msleep(250);	/* let device settle */
1281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* 2. We upload the FPGA firmware into the EMI
1301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * Note: collect up to 1023 (yes!) bytes and send them with
1311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * a single request. This is _much_ faster! */
132ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	rec = (const struct ihex_binrec *)bitstream_fw->data;
1331da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	do {
1341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		i = 0;
135ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		addr = be32_to_cpu(rec->addr);
1361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		/* intel hex records are terminated with type 0 element */
138ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		while (rec && (i + be16_to_cpu(rec->len) < FW_LOAD_SIZE)) {
139ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			memcpy(buf + i, rec->data, be16_to_cpu(rec->len));
140ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			i += be16_to_cpu(rec->len);
141ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			rec = ihex_next_binrec(rec);
1421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		err = emi26_writememory(dev, addr, buf, i, ANCHOR_LOAD_FPGA);
144b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman		if (err < 0)
1451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto wraperr;
146327d74f6b65ddc8a042c43c11fdd4be0bb354668Marcin Slusarz	} while (rec);
1471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Assert reset (stop the CPU in the EMI) */
1491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = emi26_set_reset(dev,1);
150b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
1511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto wraperr;
1521da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* 3. We need to put the loader for the firmware into the EZ-USB (again...) */
154ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	for (rec = (const struct ihex_binrec *)loader_fw->data;
155ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	     rec; rec = ihex_next_binrec(rec)) {
156ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		err = emi26_writememory(dev, be32_to_cpu(rec->addr),
157ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse					rec->data, be16_to_cpu(rec->len),
158ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse					ANCHOR_LOAD_INTERNAL);
159b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman		if (err < 0)
1601da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds			goto wraperr;
1611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
16216c23f7d88cbcce491f9370b2846fad66e8ef319Monty	msleep(250);	/* let device settle */
1631da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1641da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* De-assert reset (let the CPU run) */
1651da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = emi26_set_reset(dev,0);
166b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
1671da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto wraperr;
1681da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1691da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* 4. We put the part of the firmware that lies in the external RAM into the EZ-USB */
170ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse
171ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	for (rec = (const struct ihex_binrec *)firmware_fw->data;
172ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	     rec; rec = ihex_next_binrec(rec)) {
173ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		if (!INTERNAL_RAM(be32_to_cpu(rec->addr))) {
174ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			err = emi26_writememory(dev, be32_to_cpu(rec->addr),
175ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse						rec->data, be16_to_cpu(rec->len),
176ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse						ANCHOR_LOAD_EXTERNAL);
177b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman			if (err < 0)
1781da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				goto wraperr;
1791da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1801da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
181b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman
1821da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* Assert reset (stop the CPU in the EMI) */
1831da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = emi26_set_reset(dev,1);
184b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
1851da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto wraperr;
1861da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
187ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	for (rec = (const struct ihex_binrec *)firmware_fw->data;
188ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	     rec; rec = ihex_next_binrec(rec)) {
189ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse		if (INTERNAL_RAM(be32_to_cpu(rec->addr))) {
190ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse			err = emi26_writememory(dev, be32_to_cpu(rec->addr),
191ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse						rec->data, be16_to_cpu(rec->len),
192ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse						ANCHOR_LOAD_INTERNAL);
193b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman			if (err < 0)
1941da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds				goto wraperr;
1951da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		}
1961da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	}
1971da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
1981da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* De-assert reset (let the CPU run) */
1991da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = emi26_set_reset(dev,0);
200b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
2011da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds		goto wraperr;
20216c23f7d88cbcce491f9370b2846fad66e8ef319Monty	msleep(250);	/* let device settle */
2031da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2041da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* return 1 to fail the driver inialization
2051da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	 * and give real driver change to load */
2061da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	err = 1;
2071da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2081da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldswraperr:
209b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman	if (err < 0)
210b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman		dev_err(&dev->dev,"%s - error loading firmware: error = %d\n",
211b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman			__func__, err);
212b412284b969845615e860001b2f34614ece1d576Greg Kroah-Hartman
213ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	release_firmware(loader_fw);
214ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	release_firmware(bitstream_fw);
215ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse	release_firmware(firmware_fw);
216ae93a55bf948753de0bb8e43fa9c027f786abb05David Woodhouse
2171da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	kfree(buf);
2181da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return err;
2191da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2201da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
22133b9e16243fd69493be3ddda7be73226c8be586aNémeth Mártonstatic const struct usb_device_id id_table[] = {
2221da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(EMI26_VENDOR_ID, EMI26_PRODUCT_ID) },
2231da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ USB_DEVICE(EMI26_VENDOR_ID, EMI26B_PRODUCT_ID) },
2241da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	{ }                                             /* Terminating entry */
2251da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2261da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2271da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DEVICE_TABLE (usb, id_table);
2281da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2291da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic int emi26_probe(struct usb_interface *intf, const struct usb_device_id *id)
2301da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2311da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	struct usb_device *dev = interface_to_usbdev(intf);
2321da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2331b29a375fb0b79a11a2d18e7bf5f6da422a35025Greg Kroah-Hartman	dev_info(&intf->dev, "%s start\n", __func__);
2341da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2351da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	emi26_load_firmware(dev);
2361da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2371da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	/* do not return the driver context, let real audio driver do that */
2381da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	return -EIO;
2391da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2401da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2411da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic void emi26_disconnect(struct usb_interface *intf)
2421da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds{
2431da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds}
2441da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
2451da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvaldsstatic struct usb_driver emi26_driver = {
2461da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.name		= "emi26 - firmware loader",
2471da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.probe		= emi26_probe,
2481da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.disconnect	= emi26_disconnect,
2491da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds	.id_table	= id_table,
2501da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds};
2511da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25265db43054065790a75291b0834657445fea2cf56Greg Kroah-Hartmanmodule_usb_driver(emi26_driver);
2531da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
25496de0e252cedffad61b3cb5e05662c591898e69aJan EngelhardtMODULE_AUTHOR("Tapio Laxström");
2551da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_DESCRIPTION("Emagic EMI 2|6 firmware loader.");
2561da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus TorvaldsMODULE_LICENSE("GPL");
2571da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds
258ae93a55bf948753de0bb8e43fa9c027f786abb05David WoodhouseMODULE_FIRMWARE("emi26/loader.fw");
259ae93a55bf948753de0bb8e43fa9c027f786abb05David WoodhouseMODULE_FIRMWARE("emi26/bitstream.fw");
260ae93a55bf948753de0bb8e43fa9c027f786abb05David WoodhouseMODULE_FIRMWARE("emi26/firmware.fw");
2611da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds/* vi:ai:syntax=c:sw=8:ts=8:tw=80
2621da177e4c3f41524e886b7f1b8a0c1fc7321cacLinus Torvalds */
263