p54common.c

来自「linux 内核源代码」· C语言 代码 · 共 1,020 行 · 第 1/2 页

C
1,020
字号
/* * Common code for mac80211 Prism54 drivers * * Copyright (c) 2006, Michael Wu <flamingice@sourmilk.net> * Copyright (c) 2007, Christian Lamparter <chunkeey@web.de> * * Based on the islsm (softmac prism54) driver, which is: * Copyright 2004-2006 Jean-Baptiste Note <jbnote@gmail.com>, et al. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#include <linux/init.h>#include <linux/firmware.h>#include <linux/etherdevice.h>#include <net/mac80211.h>#include "p54.h"#include "p54common.h"MODULE_AUTHOR("Michael Wu <flamingice@sourmilk.net>");MODULE_DESCRIPTION("Softmac Prism54 common code");MODULE_LICENSE("GPL");MODULE_ALIAS("prism54common");void p54_parse_firmware(struct ieee80211_hw *dev, const struct firmware *fw){	struct p54_common *priv = dev->priv;	struct bootrec_exp_if *exp_if;	struct bootrec *bootrec;	u32 *data = (u32 *)fw->data;	u32 *end_data = (u32 *)fw->data + (fw->size >> 2);	u8 *fw_version = NULL;	size_t len;	int i;	if (priv->rx_start)		return;	while (data < end_data && *data)		data++;	while (data < end_data && !*data)		data++;	bootrec = (struct bootrec *) data;	while (bootrec->data <= end_data &&	       (bootrec->data + (len = le32_to_cpu(bootrec->len))) <= end_data) {		u32 code = le32_to_cpu(bootrec->code);		switch (code) {		case BR_CODE_COMPONENT_ID:			switch (be32_to_cpu(*bootrec->data)) {			case FW_FMAC:				printk(KERN_INFO "p54: FreeMAC firmware\n");				break;			case FW_LM20:				printk(KERN_INFO "p54: LM20 firmware\n");				break;			case FW_LM86:				printk(KERN_INFO "p54: LM86 firmware\n");				break;			case FW_LM87:				printk(KERN_INFO "p54: LM87 firmware - not supported yet!\n");				break;			default:				printk(KERN_INFO "p54: unknown firmware\n");				break;			}			break;		case BR_CODE_COMPONENT_VERSION:			/* 24 bytes should be enough for all firmwares */			if (strnlen((unsigned char*)bootrec->data, 24) < 24)				fw_version = (unsigned char*)bootrec->data;			break;		case BR_CODE_DESCR:			priv->rx_start = le32_to_cpu(bootrec->data[1]);			/* FIXME add sanity checking */			priv->rx_end = le32_to_cpu(bootrec->data[2]) - 0x3500;			break;		case BR_CODE_EXPOSED_IF:			exp_if = (struct bootrec_exp_if *) bootrec->data;			for (i = 0; i < (len * sizeof(*exp_if) / 4); i++)				if (exp_if[i].if_id == 0x1a)					priv->fw_var = le16_to_cpu(exp_if[i].variant);			break;		case BR_CODE_DEPENDENT_IF:			break;		case BR_CODE_END_OF_BRA:		case LEGACY_BR_CODE_END_OF_BRA:			end_data = NULL;			break;		default:			break;		}		bootrec = (struct bootrec *)&bootrec->data[len];	}	if (fw_version)		printk(KERN_INFO "p54: FW rev %s - Softmac protocol %x.%x\n",			fw_version, priv->fw_var >> 8, priv->fw_var & 0xff);	if (priv->fw_var >= 0x300) {		/* Firmware supports QoS, use it! */		priv->tx_stats.data[0].limit = 3;		priv->tx_stats.data[1].limit = 4;		priv->tx_stats.data[2].limit = 3;		priv->tx_stats.data[3].limit = 1;		dev->queues = 4;	}}EXPORT_SYMBOL_GPL(p54_parse_firmware);static int p54_convert_rev0_to_rev1(struct ieee80211_hw *dev,				    struct pda_pa_curve_data *curve_data){	struct p54_common *priv = dev->priv;	struct pda_pa_curve_data_sample_rev1 *rev1;	struct pda_pa_curve_data_sample_rev0 *rev0;	size_t cd_len = sizeof(*curve_data) +		(curve_data->points_per_channel*sizeof(*rev1) + 2) *		 curve_data->channels;	unsigned int i, j;	void *source, *target;	priv->curve_data = kmalloc(cd_len, GFP_KERNEL);	if (!priv->curve_data)		return -ENOMEM;	memcpy(priv->curve_data, curve_data, sizeof(*curve_data));	source = curve_data->data;	target = priv->curve_data->data;	for (i = 0; i < curve_data->channels; i++) {		__le16 *freq = source;		source += sizeof(__le16);		*((__le16 *)target) = *freq;		target += sizeof(__le16);		for (j = 0; j < curve_data->points_per_channel; j++) {			rev1 = target;			rev0 = source;			rev1->rf_power = rev0->rf_power;			rev1->pa_detector = rev0->pa_detector;			rev1->data_64qam = rev0->pcv;			/* "invent" the points for the other modulations */#define SUB(x,y) (u8)((x) - (y)) > (x) ? 0 : (x) - (y)			rev1->data_16qam = SUB(rev0->pcv, 12);			rev1->data_qpsk  = SUB(rev1->data_16qam, 12);			rev1->data_bpsk  = SUB(rev1->data_qpsk, 12);			rev1->data_barker= SUB(rev1->data_bpsk, 14);#undef SUB			target += sizeof(*rev1);			source += sizeof(*rev0);		}	}	return 0;}int p54_parse_eeprom(struct ieee80211_hw *dev, void *eeprom, int len){	struct p54_common *priv = dev->priv;	struct eeprom_pda_wrap *wrap = NULL;	struct pda_entry *entry;	int i = 0;	unsigned int data_len, entry_len;	void *tmp;	int err;	wrap = (struct eeprom_pda_wrap *) eeprom;	entry = (void *)wrap->data + wrap->len;	i += 2;	i += le16_to_cpu(entry->len)*2;	while (i < len) {		entry_len = le16_to_cpu(entry->len);		data_len = ((entry_len - 1) << 1);		switch (le16_to_cpu(entry->code)) {		case PDR_MAC_ADDRESS:			SET_IEEE80211_PERM_ADDR(dev, entry->data);			break;		case PDR_PRISM_PA_CAL_OUTPUT_POWER_LIMITS:			if (data_len < 2) {				err = -EINVAL;				goto err;			}			if (2 + entry->data[1]*sizeof(*priv->output_limit) > data_len) {				err = -EINVAL;				goto err;			}			priv->output_limit = kmalloc(entry->data[1] *				sizeof(*priv->output_limit), GFP_KERNEL);			if (!priv->output_limit) {				err = -ENOMEM;				goto err;			}			memcpy(priv->output_limit, &entry->data[2],			       entry->data[1]*sizeof(*priv->output_limit));			priv->output_limit_len = entry->data[1];			break;		case PDR_PRISM_PA_CAL_CURVE_DATA:			if (data_len < sizeof(struct pda_pa_curve_data)) {				err = -EINVAL;				goto err;			}			if (((struct pda_pa_curve_data *)entry->data)->cal_method_rev) {				priv->curve_data = kmalloc(data_len, GFP_KERNEL);				if (!priv->curve_data) {					err = -ENOMEM;					goto err;				}				memcpy(priv->curve_data, entry->data, data_len);			} else {				err = p54_convert_rev0_to_rev1(dev, (struct pda_pa_curve_data *)entry->data);				if (err)					goto err;			}			break;		case PDR_PRISM_ZIF_TX_IQ_CALIBRATION:			priv->iq_autocal = kmalloc(data_len, GFP_KERNEL);			if (!priv->iq_autocal) {				err = -ENOMEM;				goto err;			}			memcpy(priv->iq_autocal, entry->data, data_len);			priv->iq_autocal_len = data_len / sizeof(struct pda_iq_autocal_entry);			break;		case PDR_INTERFACE_LIST:			tmp = entry->data;			while ((u8 *)tmp < entry->data + data_len) {				struct bootrec_exp_if *exp_if = tmp;				if (le16_to_cpu(exp_if->if_id) == 0xF)					priv->rxhw = exp_if->variant & cpu_to_le16(0x07);				tmp += sizeof(struct bootrec_exp_if);			}			break;		case PDR_HARDWARE_PLATFORM_COMPONENT_ID:			priv->version = *(u8 *)(entry->data + 1);			break;		case PDR_END:			i = len;			break;		}		entry = (void *)entry + (entry_len + 1)*2;		i += 2;		i += entry_len*2;	}	if (!priv->iq_autocal || !priv->output_limit || !priv->curve_data) {		printk(KERN_ERR "p54: not all required entries found in eeprom!\n");		err = -EINVAL;		goto err;	}	return 0;  err:	if (priv->iq_autocal) {		kfree(priv->iq_autocal);		priv->iq_autocal = NULL;	}	if (priv->output_limit) {		kfree(priv->output_limit);		priv->output_limit = NULL;	}	if (priv->curve_data) {		kfree(priv->curve_data);		priv->curve_data = NULL;	}	printk(KERN_ERR "p54: eeprom parse failed!\n");	return err;}EXPORT_SYMBOL_GPL(p54_parse_eeprom);void p54_fill_eeprom_readback(struct p54_control_hdr *hdr){	struct p54_eeprom_lm86 *eeprom_hdr;	hdr->magic1 = cpu_to_le16(0x8000);	hdr->len = cpu_to_le16(sizeof(*eeprom_hdr) + 0x2000);	hdr->type = cpu_to_le16(P54_CONTROL_TYPE_EEPROM_READBACK);	hdr->retry1 = hdr->retry2 = 0;	eeprom_hdr = (struct p54_eeprom_lm86 *) hdr->data;	eeprom_hdr->offset = 0x0;	eeprom_hdr->len = cpu_to_le16(0x2000);}EXPORT_SYMBOL_GPL(p54_fill_eeprom_readback);static void p54_rx_data(struct ieee80211_hw *dev, struct sk_buff *skb){	struct p54_rx_hdr *hdr = (struct p54_rx_hdr *) skb->data;	struct ieee80211_rx_status rx_status = {0};	u16 freq = le16_to_cpu(hdr->freq);	rx_status.ssi = hdr->rssi;	rx_status.rate = hdr->rate & 0x1f; /* report short preambles & CCK too */	rx_status.channel = freq == 2484 ? 14 : (freq - 2407)/5;	rx_status.freq = freq;	rx_status.phymode = MODE_IEEE80211G;	rx_status.antenna = hdr->antenna;	rx_status.mactime = le64_to_cpu(hdr->timestamp);	skb_pull(skb, sizeof(*hdr));	skb_trim(skb, le16_to_cpu(hdr->len));	ieee80211_rx_irqsafe(dev, skb, &rx_status);}static void inline p54_wake_free_queues(struct ieee80211_hw *dev){	struct p54_common *priv = dev->priv;	int i;	/* ieee80211_start_queues is great if all queues are really empty.	 * But, what if some are full? */	for (i = 0; i < dev->queues; i++)		if (priv->tx_stats.data[i].len < priv->tx_stats.data[i].limit)			ieee80211_wake_queue(dev, i);}static void p54_rx_frame_sent(struct ieee80211_hw *dev, struct sk_buff *skb){	struct p54_common *priv = dev->priv;	struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;	struct p54_frame_sent_hdr *payload = (struct p54_frame_sent_hdr *) hdr->data;	struct sk_buff *entry = (struct sk_buff *) priv->tx_queue.next;	u32 addr = le32_to_cpu(hdr->req_id) - 0x70;	struct memrecord *range = NULL;	u32 freed = 0;	u32 last_addr = priv->rx_start;	while (entry != (struct sk_buff *)&priv->tx_queue) {		range = (struct memrecord *)&entry->cb;		if (range->start_addr == addr) {			struct ieee80211_tx_status status = {{0}};			struct p54_control_hdr *entry_hdr;			struct p54_tx_control_allocdata *entry_data;			int pad = 0;			if (entry->next != (struct sk_buff *)&priv->tx_queue)				freed = ((struct memrecord *)&entry->next->cb)->start_addr - last_addr;			else				freed = priv->rx_end - last_addr;			last_addr = range->end_addr;			__skb_unlink(entry, &priv->tx_queue);			if (!range->control) {				kfree_skb(entry);				break;			}			memcpy(&status.control, range->control,			       sizeof(status.control));			kfree(range->control);			priv->tx_stats.data[status.control.queue].len--;			entry_hdr = (struct p54_control_hdr *) entry->data;			entry_data = (struct p54_tx_control_allocdata *) entry_hdr->data;			if ((entry_hdr->magic1 & cpu_to_le16(0x4000)) != 0)				pad = entry_data->align[0];			if (!status.control.flags & IEEE80211_TXCTL_NO_ACK) {				if (!(payload->status & 0x01))					status.flags |= IEEE80211_TX_STATUS_ACK;				else					status.excessive_retries = 1;			}			status.retry_count = payload->retries - 1;			status.ack_signal = le16_to_cpu(payload->ack_rssi);			skb_pull(entry, sizeof(*hdr) + pad + sizeof(*entry_data));			ieee80211_tx_status_irqsafe(dev, entry, &status);			break;		} else			last_addr = range->end_addr;		entry = entry->next;	}	if (freed >= IEEE80211_MAX_RTS_THRESHOLD + 0x170 +	    sizeof(struct p54_control_hdr))		p54_wake_free_queues(dev);}static void p54_rx_control(struct ieee80211_hw *dev, struct sk_buff *skb){	struct p54_control_hdr *hdr = (struct p54_control_hdr *) skb->data;	switch (le16_to_cpu(hdr->type)) {	case P54_CONTROL_TYPE_TXDONE:		p54_rx_frame_sent(dev, skb);		break;	case P54_CONTROL_TYPE_BBP:		break;	default:		printk(KERN_DEBUG "%s: not handling 0x%02x type control frame\n",		       wiphy_name(dev->wiphy), le16_to_cpu(hdr->type));		break;	}}/* returns zero if skb can be reused */int p54_rx(struct ieee80211_hw *dev, struct sk_buff *skb){	u8 type = le16_to_cpu(*((__le16 *)skb->data)) >> 8;	switch (type) {	case 0x00:	case 0x01:		p54_rx_data(dev, skb);		return -1;	case 0x4d:		/* TODO: do something better... but then again, I've never seen this happen */		printk(KERN_ERR "%s: Received fault. Probably need to restart hardware now..\n",		       wiphy_name(dev->wiphy));		break;	case 0x80:		p54_rx_control(dev, skb);		break;	default:		printk(KERN_ERR "%s: unknown frame RXed (0x%02x)\n",		       wiphy_name(dev->wiphy), type);		break;	}	return 0;}EXPORT_SYMBOL_GPL(p54_rx);/* * So, the firmware is somewhat stupid and doesn't know what places in its * memory incoming data should go to. By poking around in the firmware, we * can find some unused memory to upload our packets to. However, data that we * want the card to TX needs to stay intact until the card has told us that * it is done with it. This function finds empty places we can upload to and * marks allocated areas as reserved if necessary. p54_rx_frame_sent frees * allocated areas. */static void p54_assign_address(struct ieee80211_hw *dev, struct sk_buff *skb,			       struct p54_control_hdr *data, u32 len,			       struct ieee80211_tx_control *control){	struct p54_common *priv = dev->priv;	struct sk_buff *entry = priv->tx_queue.next;	struct sk_buff *target_skb = NULL;	struct memrecord *range;	u32 last_addr = priv->rx_start;	u32 largest_hole = 0;	u32 target_addr = priv->rx_start;	unsigned long flags;	unsigned int left;	len = (len + 0x170 + 3) & ~0x3; /* 0x70 headroom, 0x100 tailroom */	spin_lock_irqsave(&priv->tx_queue.lock, flags);	left = skb_queue_len(&priv->tx_queue);	while (left--) {		u32 hole_size;		range = (struct memrecord *)&entry->cb;		hole_size = range->start_addr - last_addr;		if (!target_skb && hole_size >= len) {			target_skb = entry->prev;			hole_size -= len;			target_addr = last_addr;		}		largest_hole = max(largest_hole, hole_size);		last_addr = range->end_addr;		entry = entry->next;	}	if (!target_skb && priv->rx_end - last_addr >= len) {		target_skb = priv->tx_queue.prev;		largest_hole = max(largest_hole, priv->rx_end - last_addr - len);		if (!skb_queue_empty(&priv->tx_queue)) {			range = (struct memrecord *)&target_skb->cb;			target_addr = range->end_addr;		}	} else		largest_hole = max(largest_hole, priv->rx_end - last_addr);	if (skb) {		range = (struct memrecord *)&skb->cb;		range->start_addr = target_addr;		range->end_addr = target_addr + len;		range->control = control;		__skb_queue_after(&priv->tx_queue, target_skb, skb);		if (largest_hole < IEEE80211_MAX_RTS_THRESHOLD + 0x170 +				   sizeof(struct p54_control_hdr))			ieee80211_stop_queues(dev);	}	spin_unlock_irqrestore(&priv->tx_queue.lock, flags);	data->req_id = cpu_to_le32(target_addr + 0x70);}static int p54_tx(struct ieee80211_hw *dev, struct sk_buff *skb,		  struct ieee80211_tx_control *control){	struct ieee80211_tx_queue_stats_data *current_queue;	struct p54_common *priv = dev->priv;	struct p54_control_hdr *hdr;

⌨️ 快捷键说明

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