⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 zd_usb.c

📁 linux内核源码
💻 C
📖 第 1 页 / 共 3 页
字号:
/* zd_usb.c * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#include <linux/kernel.h>#include <linux/init.h>#include <linux/module.h>#include <linux/firmware.h>#include <linux/device.h>#include <linux/errno.h>#include <linux/skbuff.h>#include <linux/usb.h>#include <linux/workqueue.h>#include <net/ieee80211.h>#include <asm/unaligned.h>#include "zd_def.h"#include "zd_netdev.h"#include "zd_mac.h"#include "zd_usb.h"static struct usb_device_id usb_ids[] = {	/* ZD1211 */	{ USB_DEVICE(0x0ace, 0x1211), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x07b8, 0x6001), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x126f, 0xa006), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x6891, 0xa727), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0df6, 0x9071), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0df6, 0x9075), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x157e, 0x300b), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x079b, 0x004a), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x1740, 0x2000), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x157e, 0x3204), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0586, 0x3402), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0b3b, 0x5630), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0b05, 0x170c), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x1435, 0x0711), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0586, 0x3409), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0b3b, 0x1630), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0586, 0x3401), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x14ea, 0xab13), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x13b1, 0x001e), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x0586, 0x3407), .driver_info = DEVICE_ZD1211 },	{ USB_DEVICE(0x129b, 0x1666), .driver_info = DEVICE_ZD1211 },	/* ZD1211B */	{ USB_DEVICE(0x0ace, 0x1215), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x157e, 0x300d), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x079b, 0x0062), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x1582, 0x6003), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x050d, 0x705c), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x083a, 0x4505), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0471, 0x1236), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x13b1, 0x0024), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0586, 0x340f), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0b05, 0x171b), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0586, 0x3410), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0baf, 0x0121), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0586, 0x3412), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0586, 0x3413), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0053, 0x5301), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0411, 0x00da), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x2019, 0x5303), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x129b, 0x1667), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0cde, 0x001a), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0586, 0x340a), .driver_info = DEVICE_ZD1211B },	{ USB_DEVICE(0x0471, 0x1237), .driver_info = DEVICE_ZD1211B },	/* "Driverless" devices that need ejecting */	{ USB_DEVICE(0x0ace, 0x2011), .driver_info = DEVICE_INSTALLER },	{ USB_DEVICE(0x0ace, 0x20ff), .driver_info = DEVICE_INSTALLER },	{}};MODULE_LICENSE("GPL");MODULE_DESCRIPTION("USB driver for devices with the ZD1211 chip.");MODULE_AUTHOR("Ulrich Kunitz");MODULE_AUTHOR("Daniel Drake");MODULE_VERSION("1.0");MODULE_DEVICE_TABLE(usb, usb_ids);#define FW_ZD1211_PREFIX	"zd1211/zd1211_"#define FW_ZD1211B_PREFIX	"zd1211/zd1211b_"/* USB device initialization */static int request_fw_file(	const struct firmware **fw, const char *name, struct device *device){	int r;	dev_dbg_f(device, "fw name %s\n", name);	r = request_firmware(fw, name, device);	if (r)		dev_err(device,		       "Could not load firmware file %s. Error number %d\n",		       name, r);	return r;}static inline u16 get_bcdDevice(const struct usb_device *udev){	return le16_to_cpu(udev->descriptor.bcdDevice);}enum upload_code_flags {	REBOOT = 1,};/* Ensures that MAX_TRANSFER_SIZE is even. */#define MAX_TRANSFER_SIZE (USB_MAX_TRANSFER_SIZE & ~1)static int upload_code(struct usb_device *udev,	const u8 *data, size_t size, u16 code_offset, int flags){	u8 *p;	int r;	/* USB request blocks need "kmalloced" buffers.	 */	p = kmalloc(MAX_TRANSFER_SIZE, GFP_KERNEL);	if (!p) {		dev_err(&udev->dev, "out of memory\n");		r = -ENOMEM;		goto error;	}	size &= ~1;	while (size > 0) {		size_t transfer_size = size <= MAX_TRANSFER_SIZE ?			size : MAX_TRANSFER_SIZE;		dev_dbg_f(&udev->dev, "transfer size %zu\n", transfer_size);		memcpy(p, data, transfer_size);		r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0),			USB_REQ_FIRMWARE_DOWNLOAD,			USB_DIR_OUT | USB_TYPE_VENDOR,			code_offset, 0, p, transfer_size, 1000 /* ms */);		if (r < 0) {			dev_err(&udev->dev,			       "USB control request for firmware upload"			       " failed. Error number %d\n", r);			goto error;		}		transfer_size = r & ~1;		size -= transfer_size;		data += transfer_size;		code_offset += transfer_size/sizeof(u16);	}	if (flags & REBOOT) {		u8 ret;		r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),			USB_REQ_FIRMWARE_CONFIRM,			USB_DIR_IN | USB_TYPE_VENDOR,			0, 0, &ret, sizeof(ret), 5000 /* ms */);		if (r != sizeof(ret)) {			dev_err(&udev->dev,				"control request firmeware confirmation failed."				" Return value %d\n", r);			if (r >= 0)				r = -ENODEV;			goto error;		}		if (ret & 0x80) {			dev_err(&udev->dev,				"Internal error while downloading."				" Firmware confirm return value %#04x\n",				(unsigned int)ret);			r = -ENODEV;			goto error;		}		dev_dbg_f(&udev->dev, "firmware confirm return value %#04x\n",			(unsigned int)ret);	}	r = 0;error:	kfree(p);	return r;}static u16 get_word(const void *data, u16 offset){	const __le16 *p = data;	return le16_to_cpu(p[offset]);}static char *get_fw_name(struct zd_usb *usb, char *buffer, size_t size,	               const char* postfix){	scnprintf(buffer, size, "%s%s",		usb->is_zd1211b ?			FW_ZD1211B_PREFIX : FW_ZD1211_PREFIX,		postfix);	return buffer;}static int handle_version_mismatch(struct zd_usb *usb,	const struct firmware *ub_fw){	struct usb_device *udev = zd_usb_to_usbdev(usb);	const struct firmware *ur_fw = NULL;	int offset;	int r = 0;	char fw_name[128];	r = request_fw_file(&ur_fw,		get_fw_name(usb, fw_name, sizeof(fw_name), "ur"),		&udev->dev);	if (r)		goto error;	r = upload_code(udev, ur_fw->data, ur_fw->size, FW_START, REBOOT);	if (r)		goto error;	offset = (E2P_BOOT_CODE_OFFSET * sizeof(u16));	r = upload_code(udev, ub_fw->data + offset, ub_fw->size - offset,		E2P_START + E2P_BOOT_CODE_OFFSET, REBOOT);	/* At this point, the vendor driver downloads the whole firmware	 * image, hacks around with version IDs, and uploads it again,	 * completely overwriting the boot code. We do not do this here as	 * it is not required on any tested devices, and it is suspected to	 * cause problems. */error:	release_firmware(ur_fw);	return r;}static int upload_firmware(struct zd_usb *usb){	int r;	u16 fw_bcdDevice;	u16 bcdDevice;	struct usb_device *udev = zd_usb_to_usbdev(usb);	const struct firmware *ub_fw = NULL;	const struct firmware *uph_fw = NULL;	char fw_name[128];	bcdDevice = get_bcdDevice(udev);	r = request_fw_file(&ub_fw,		get_fw_name(usb, fw_name, sizeof(fw_name), "ub"),		&udev->dev);	if (r)		goto error;	fw_bcdDevice = get_word(ub_fw->data, E2P_DATA_OFFSET);	if (fw_bcdDevice != bcdDevice) {		dev_info(&udev->dev,			"firmware version %#06x and device bootcode version "			"%#06x differ\n", fw_bcdDevice, bcdDevice);		if (bcdDevice <= 0x4313)			dev_warn(&udev->dev, "device has old bootcode, please "				"report success or failure\n");		r = handle_version_mismatch(usb, ub_fw);		if (r)			goto error;	} else {		dev_dbg_f(&udev->dev,			"firmware device id %#06x is equal to the "			"actual device id\n", fw_bcdDevice);	}	r = request_fw_file(&uph_fw,		get_fw_name(usb, fw_name, sizeof(fw_name), "uphr"),		&udev->dev);	if (r)		goto error;	r = upload_code(udev, uph_fw->data, uph_fw->size, FW_START, REBOOT);	if (r) {		dev_err(&udev->dev,			"Could not upload firmware code uph. Error number %d\n",			r);	}	/* FALL-THROUGH */error:	release_firmware(ub_fw);	release_firmware(uph_fw);	return r;}/* Read data from device address space using "firmware interface" which does * not require firmware to be loaded. */int zd_usb_read_fw(struct zd_usb *usb, zd_addr_t addr, u8 *data, u16 len){	int r;	struct usb_device *udev = zd_usb_to_usbdev(usb);	r = usb_control_msg(udev, usb_rcvctrlpipe(udev, 0),		USB_REQ_FIRMWARE_READ_DATA, USB_DIR_IN | 0x40, addr, 0,		data, len, 5000);	if (r < 0) {		dev_err(&udev->dev,			"read over firmware interface failed: %d\n", r);		return r;	} else if (r != len) {		dev_err(&udev->dev,			"incomplete read over firmware interface: %d/%d\n",			r, len);		return -EIO;	}	return 0;}#define urb_dev(urb) (&(urb)->dev->dev)static inline void handle_regs_int(struct urb *urb){	struct zd_usb *usb = urb->context;	struct zd_usb_interrupt *intr = &usb->intr;	int len;	ZD_ASSERT(in_interrupt());	spin_lock(&intr->lock);	if (intr->read_regs_enabled) {		intr->read_regs.length = len = urb->actual_length;		if (len > sizeof(intr->read_regs.buffer))			len = sizeof(intr->read_regs.buffer);		memcpy(intr->read_regs.buffer, urb->transfer_buffer, len);		intr->read_regs_enabled = 0;		complete(&intr->read_regs.completion);		goto out;	}	dev_dbg_f(urb_dev(urb), "regs interrupt ignored\n");out:	spin_unlock(&intr->lock);}static inline void handle_retry_failed_int(struct urb *urb){	struct zd_usb *usb = urb->context;	struct zd_mac *mac = zd_usb_to_mac(usb);	struct ieee80211_device *ieee = zd_mac_to_ieee80211(mac);	ieee->stats.tx_errors++;	ieee->ieee_stats.tx_retry_limit_exceeded++;	dev_dbg_f(urb_dev(urb), "retry failed interrupt\n");}static void int_urb_complete(struct urb *urb){	int r;	struct usb_int_header *hdr;	switch (urb->status) {	case 0:		break;	case -ESHUTDOWN:	case -EINVAL:	case -ENODEV:	case -ENOENT:	case -ECONNRESET:	case -EPIPE:		goto kfree;	default:		goto resubmit;	}	if (urb->actual_length < sizeof(hdr)) {		dev_dbg_f(urb_dev(urb), "error: urb %p to small\n", urb);		goto resubmit;	}	hdr = urb->transfer_buffer;	if (hdr->type != USB_INT_TYPE) {		dev_dbg_f(urb_dev(urb), "error: urb %p wrong type\n", urb);		goto resubmit;	}	switch (hdr->id) {	case USB_INT_ID_REGS:		handle_regs_int(urb);		break;	case USB_INT_ID_RETRY_FAILED:		handle_retry_failed_int(urb);		break;	default:		dev_dbg_f(urb_dev(urb), "error: urb %p unknown id %x\n", urb,			(unsigned int)hdr->id);		goto resubmit;	}resubmit:	r = usb_submit_urb(urb, GFP_ATOMIC);	if (r) {		dev_dbg_f(urb_dev(urb), "resubmit urb %p\n", urb);		goto kfree;	}	return;kfree:	kfree(urb->transfer_buffer);}static inline int int_urb_interval(struct usb_device *udev){	switch (udev->speed) {	case USB_SPEED_HIGH:		return 4;	case USB_SPEED_LOW:		return 10;	case USB_SPEED_FULL:	default:		return 1;	}}static inline int usb_int_enabled(struct zd_usb *usb){	unsigned long flags;	struct zd_usb_interrupt *intr = &usb->intr;	struct urb *urb;	spin_lock_irqsave(&intr->lock, flags);	urb = intr->urb;	spin_unlock_irqrestore(&intr->lock, flags);	return urb != NULL;}int zd_usb_enable_int(struct zd_usb *usb){	int r;	struct usb_device *udev;	struct zd_usb_interrupt *intr = &usb->intr;	void *transfer_buffer = NULL;	struct urb *urb;	dev_dbg_f(zd_usb_dev(usb), "\n");	urb = usb_alloc_urb(0, GFP_KERNEL);	if (!urb) {		r = -ENOMEM;		goto out;	}	ZD_ASSERT(!irqs_disabled());	spin_lock_irq(&intr->lock);	if (intr->urb) {		spin_unlock_irq(&intr->lock);		r = 0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -