<commit_before>/*
 *  linux/drivers/net/wireless/libertas/if_sdio.c
 *
 *  Copyright 2007 <NAME>
 *
 * Inspired by if_cs.c, Copyright 2007 <NAME>
 *
 * 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 hardware has more or less no CMD53 support, so all registers
 * must be accessed using sdio_readb()/sdio_writeb().
 *
 * Transfers must be in one transaction or the firmware goes bonkers.
 * This means that the transfer must either be small enough to do a
 * byte based transfer or it must be padded to a multiple of the
 * current block size.
 *
 * As SDIO is still new to the kernel, it is unfortunately common with
 * bugs in the host controllers related to that. One such bug is that 
 * controllers cannot do transfers that aren't a multiple of 4 bytes.
 * If you don't have time to fix the host controller driver, you can
 * work around the problem by modifying if_sdio_host_to_card() and
 * if_sdio_card_to_host() to pad the data.
 */

#include <linux/moduleparam.h>
#include <linux/firmware.h>
#include <linux/netdevice.h>
#include <linux/delay.h>
#include <linux/mmc/card.h>
#include <linux/mmc/sdio_func.h>
#include <linux/mmc/sdio_ids.h>

#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "if_sdio.h"

static char *libertas_helper_name = NULL;
module_param_named(helper_name, libertas_helper_name, charp, 0644);

static char *libertas_fw_name = NULL;
module_param_named(fw_name, libertas_fw_name, charp, 0644);

static const struct sdio_device_id if_sdio_ids[] = {
	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
	{ /* end: all zeroes */						},
};

MODULE_DEVICE_TABLE(sdio, if_sdio_ids);

struct if_sdio_model {
	int model;
	const char *helper;
	const char *firmware;
};

static struct if_sdio_model if_sdio_models[] = {
	{
		/* 8385 */
		.model = 0x04,
		.helper = "sd8385_helper.bin",
		.firmware = "sd8385.bin",
	},
	{
		/* 8686 */
		.model = 0x0B,
		.helper = "sd8686_helper.bin",
		.firmware = "sd8686.bin",
	},
};

struct if_sdio_packet {
	struct if_sdio_packet	*next;
	u16			nb;
	u8			buffer[0] __attribute__((aligned(4)));
};

struct if_sdio_card {
	struct sdio_func	*func;
	wlan_private		*priv;

	int			model;
	unsigned long		ioport;

	const char		*helper;
	const char		*firmware;

	u8			buffer[65536];
	u8			int_cause;
	u32			event;

	spinlock_t		lock;
	struct if_sdio_packet	*packets;
	struct work_struct	packet_worker;
};

/********************************************************************/
/* I/O                                                              */
/********************************************************************/

static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
{
	int ret, reg;
	u16 scratch;

	if (card->model == 0x04)
		reg = IF_SDIO_SCRATCH_OLD;
	else
		reg = IF_SDIO_SCRATCH;

	scratch = sdio_readb(card->func, reg, &ret);
	if (!ret)
		scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;

	if (err)
		*err = ret;

	if (ret)
		return 0xffff;

	return scratch;
}

static int if_sdio_handle_cmd(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	unsigned long flags;

	lbs_deb_enter(LBS_DEB_SDIO);

	spin_lock_irqsave(&card->priv->adapter->driver_lock, flags);

	if (!card->priv->adapter->cur_cmd) {
		lbs_deb_sdio("discarding spurious response\n");
		ret = 0;
		goto out;
	}

	if (size > MRVDRV_SIZE_OF_CMD_BUFFER) {
		lbs_deb_sdio("response packet too large (%d bytes)\n",
			(int)size);
		ret = -E2BIG;
		goto out;
	}

	memcpy(card->priv->adapter->cur_cmd->bufvirtualaddr, buffer, size);
	card->priv->upld_len = size;

	card->int_cause |= MRVDRV_CMD_UPLD_RDY;

	libertas_interrupt(card->priv->dev);

	ret = 0;

out:
	spin_unlock_irqrestore(&card->priv->adapter->driver_lock, flags);

	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

static int if_sdio_handle_data(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	struct sk_buff *skb;
	char *data;

	lbs_deb_enter(LBS_DEB_SDIO);

	if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
		lbs_deb_sdio("response packet too large (%d bytes)\n",
			(int)size);
		ret = -E2BIG;
		goto out;
	}

	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE);
	if (!skb) {
		ret = -ENOMEM;
		goto out;
	}

	data = skb_put(skb, size);

	memcpy(data, buffer, size);

	libertas_process_rxed_packet(card->priv, skb);

	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

static int if_sdio_handle_event(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	unsigned long flags;
	u32 event;<commit_msg>libertas: make if_sdio align packets<commit_after>/*
 *  linux/drivers/net/wireless/libertas/if_sdio.c
 *
 *  Copyright 2007 <NAME>
 *
 * Inspired by if_cs.c, Copyright 2007 <NAME>
 *
 * 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 hardware has more or less no CMD53 support, so all registers
 * must be accessed using sdio_readb()/sdio_writeb().
 *
 * Transfers must be in one transaction or the firmware goes bonkers.
 * This means that the transfer must either be small enough to do a
 * byte based transfer or it must be padded to a multiple of the
 * current block size.
 *
 * As SDIO is still new to the kernel, it is unfortunately common with
 * bugs in the host controllers related to that. One such bug is that 
 * controllers cannot do transfers that aren't a multiple of 4 bytes.
 * If you don't have time to fix the host controller driver, you can
 * work around the problem by modifying if_sdio_host_to_card() and
 * if_sdio_card_to_host() to pad the data.
 */

#include <linux/moduleparam.h>
#include <linux/firmware.h>
#include <linux/netdevice.h>
#include <linux/delay.h>
#include <linux/mmc/card.h>
#include <linux/mmc/sdio_func.h>
#include <linux/mmc/sdio_ids.h>

#include "host.h"
#include "decl.h"
#include "defs.h"
#include "dev.h"
#include "if_sdio.h"

static char *libertas_helper_name = NULL;
module_param_named(helper_name, libertas_helper_name, charp, 0644);

static char *libertas_fw_name = NULL;
module_param_named(fw_name, libertas_fw_name, charp, 0644);

static const struct sdio_device_id if_sdio_ids[] = {
	{ SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_LIBERTAS) },
	{ /* end: all zeroes */						},
};

MODULE_DEVICE_TABLE(sdio, if_sdio_ids);

struct if_sdio_model {
	int model;
	const char *helper;
	const char *firmware;
};

static struct if_sdio_model if_sdio_models[] = {
	{
		/* 8385 */
		.model = 0x04,
		.helper = "sd8385_helper.bin",
		.firmware = "sd8385.bin",
	},
	{
		/* 8686 */
		.model = 0x0B,
		.helper = "sd8686_helper.bin",
		.firmware = "sd8686.bin",
	},
};

struct if_sdio_packet {
	struct if_sdio_packet	*next;
	u16			nb;
	u8			buffer[0] __attribute__((aligned(4)));
};

struct if_sdio_card {
	struct sdio_func	*func;
	wlan_private		*priv;

	int			model;
	unsigned long		ioport;

	const char		*helper;
	const char		*firmware;

	u8			buffer[65536];
	u8			int_cause;
	u32			event;

	spinlock_t		lock;
	struct if_sdio_packet	*packets;
	struct work_struct	packet_worker;
};

/********************************************************************/
/* I/O                                                              */
/********************************************************************/

static u16 if_sdio_read_scratch(struct if_sdio_card *card, int *err)
{
	int ret, reg;
	u16 scratch;

	if (card->model == 0x04)
		reg = IF_SDIO_SCRATCH_OLD;
	else
		reg = IF_SDIO_SCRATCH;

	scratch = sdio_readb(card->func, reg, &ret);
	if (!ret)
		scratch |= sdio_readb(card->func, reg + 1, &ret) << 8;

	if (err)
		*err = ret;

	if (ret)
		return 0xffff;

	return scratch;
}

static int if_sdio_handle_cmd(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	unsigned long flags;

	lbs_deb_enter(LBS_DEB_SDIO);

	spin_lock_irqsave(&card->priv->adapter->driver_lock, flags);

	if (!card->priv->adapter->cur_cmd) {
		lbs_deb_sdio("discarding spurious response\n");
		ret = 0;
		goto out;
	}

	if (size > MRVDRV_SIZE_OF_CMD_BUFFER) {
		lbs_deb_sdio("response packet too large (%d bytes)\n",
			(int)size);
		ret = -E2BIG;
		goto out;
	}

	memcpy(card->priv->adapter->cur_cmd->bufvirtualaddr, buffer, size);
	card->priv->upld_len = size;

	card->int_cause |= MRVDRV_CMD_UPLD_RDY;

	libertas_interrupt(card->priv->dev);

	ret = 0;

out:
	spin_unlock_irqrestore(&card->priv->adapter->driver_lock, flags);

	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

static int if_sdio_handle_data(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	struct sk_buff *skb;
	char *data;

	lbs_deb_enter(LBS_DEB_SDIO);

	if (size > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {
		lbs_deb_sdio("response packet too large (%d bytes)\n",
			(int)size);
		ret = -E2BIG;
		goto out;
	}

	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + NET_IP_ALIGN);
	if (!skb) {
		ret = -ENOMEM;
		goto out;
	}

	skb_reserve(skb, NET_IP_ALIGN);

	data = skb_put(skb, size);

	memcpy(data, buffer, size);

	libertas_process_rxed_packet(card->priv, skb);

	ret = 0;

out:
	lbs_deb_leave_args(LBS_DEB_SDIO, "ret %d", ret);

	return ret;
}

static int if_sdio_handle_event(struct if_sdio_card *card,
		u8 *buffer, unsigned size)
{
	int ret;
	unsigned long flags;
	u32 event;
{'fraction_non_alphanumeric': 0.1404494382022472, 'fraction_numerical': 0.017270079067831876, 'mean_word_length': 4.0835536753040715, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 69, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4540441', 'n_tokens_mistral': 3917, 'n_tokens_neox': 3622, 'n_words': 944}