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

📄 ieee1394_transactions.c

📁 IEE1394 火线接口驱动 for linux
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * IEEE 1394 for Linux * * Transaction support. * * Copyright (C) 1999 Andreas E. Bombe * * This code is licensed under the GPL.  See the file COPYING in the root * directory of the kernel sources for details. */#include <linux/sched.h>#include <linux/bitops.h>#include <asm/errno.h>#include <linux/interrupt.h>#include "ieee1394.h"#include "ieee1394_types.h"#include "hosts.h"#include "ieee1394_core.h"#include "highlevel.h"#include "nodemgr.h"#define PREP_ASYNC_HEAD_ADDRESS(tc) \        packet->tcode = tc; \        packet->header[0] = (packet->node_id << 16) | (packet->tlabel << 10) \                | (1 << 8) | (tc << 4); \        packet->header[1] = (packet->host->node_id << 16) | (addr >> 32); \        packet->header[2] = addr & 0xffffffffstatic void fill_async_readquad(struct hpsb_packet *packet, u64 addr){        PREP_ASYNC_HEAD_ADDRESS(TCODE_READQ);        packet->header_size = 12;        packet->data_size = 0;        packet->expect_response = 1;}static void fill_async_readblock(struct hpsb_packet *packet, u64 addr, int length){        PREP_ASYNC_HEAD_ADDRESS(TCODE_READB);        packet->header[3] = length << 16;        packet->header_size = 16;        packet->data_size = 0;        packet->expect_response = 1;}static void fill_async_writequad(struct hpsb_packet *packet, u64 addr, quadlet_t data){        PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEQ);        packet->header[3] = data;        packet->header_size = 16;        packet->data_size = 0;        packet->expect_response = 1;}static void fill_async_writeblock(struct hpsb_packet *packet, u64 addr, int length){        PREP_ASYNC_HEAD_ADDRESS(TCODE_WRITEB);        packet->header[3] = length << 16;        packet->header_size = 16;        packet->expect_response = 1;        packet->data_size = length + (length % 4 ? 4 - (length % 4) : 0);}static void fill_async_lock(struct hpsb_packet *packet, u64 addr, int extcode,                      int length){        PREP_ASYNC_HEAD_ADDRESS(TCODE_LOCK_REQUEST);        packet->header[3] = (length << 16) | extcode;        packet->header_size = 16;        packet->data_size = length;        packet->expect_response = 1;}static void fill_iso_packet(struct hpsb_packet *packet, int length, int channel,                     int tag, int sync){        packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)                | (TCODE_ISO_DATA << 4) | sync;        packet->header_size = 4;        packet->data_size = length;        packet->type = hpsb_iso;        packet->tcode = TCODE_ISO_DATA;}static void fill_phy_packet(struct hpsb_packet *packet, quadlet_t data) {         packet->header[0] = data;        packet->header[1] = ~data;         packet->header_size = 8;        packet->data_size = 0;        packet->expect_response = 0;        packet->type = hpsb_raw;             /* No CRC added */        packet->speed_code = IEEE1394_SPEED_100; /* Force speed to be 100Mbps */}static void fill_async_stream_packet(struct hpsb_packet *packet, int length,				     int channel, int tag, int sync){	packet->header[0] = (length << 16) | (tag << 14) | (channel << 8)	                  | (TCODE_STREAM_DATA << 4) | sync;	packet->header_size = 4;	packet->data_size = length;	packet->type = hpsb_async;	packet->tcode = TCODE_ISO_DATA;}/** * hpsb_get_tlabel - allocate a transaction label * @packet: the packet who's tlabel/tpool we set * * Every asynchronous transaction on the 1394 bus needs a transaction * label to match the response to the request.  This label has to be * different from any other transaction label in an outstanding request to * the same node to make matching possible without ambiguity. * * There are 64 different tlabels, so an allocated tlabel has to be freed * with hpsb_free_tlabel() after the transaction is complete (unless it's * reused again for the same target node). * * Return value: Zero on success, otherwise non-zero. A non-zero return * generally means there are no available tlabels. If this is called out * of interrupt or atomic context, then it will sleep until can return a * tlabel. */int hpsb_get_tlabel(struct hpsb_packet *packet){	unsigned long flags;	struct hpsb_tlabel_pool *tp;	tp = &packet->host->tpool[packet->node_id & NODE_MASK];	if (in_interrupt()) {		if (down_trylock(&tp->count))			return 1;	} else {		down(&tp->count);	}	spin_lock_irqsave(&tp->lock, flags);		packet->tlabel = find_next_zero_bit(tp->pool, 64, tp->next);	if (packet->tlabel > 63)		packet->tlabel = find_first_zero_bit(tp->pool, 64);	tp->next = (packet->tlabel + 1) % 64;	/* Should _never_ happen */	BUG_ON(test_and_set_bit(packet->tlabel, tp->pool));	tp->allocations++;	spin_unlock_irqrestore(&tp->lock, flags);	return 0;}/**  * hpsb_free_tlabel - free an allocated transaction label * @packet: packet whos tlabel/tpool needs to be cleared * * Frees the transaction label allocated with hpsb_get_tlabel().  The * tlabel has to be freed after the transaction is complete (i.e. response * was received for a split transaction or packet was sent for a unified * transaction). * * A tlabel must not be freed twice. */void hpsb_free_tlabel(struct hpsb_packet *packet){        unsigned long flags;	struct hpsb_tlabel_pool *tp;		tp = &packet->host->tpool[packet->node_id & NODE_MASK];	BUG_ON(packet->tlabel > 63 || packet->tlabel < 0);        spin_lock_irqsave(&tp->lock, flags);	BUG_ON(!test_and_clear_bit(packet->tlabel, tp->pool));        spin_unlock_irqrestore(&tp->lock, flags);	up(&tp->count);}int hpsb_packet_success(struct hpsb_packet *packet){        switch (packet->ack_code) {        case ACK_PENDING:                switch ((packet->header[1] >> 12) & 0xf) {                case RCODE_COMPLETE:                        return 0;                case RCODE_CONFLICT_ERROR:                        return -EAGAIN;                case RCODE_DATA_ERROR:                        return -EREMOTEIO;                case RCODE_TYPE_ERROR:                        return -EACCES;                case RCODE_ADDRESS_ERROR:                        return -EINVAL;                default:                        HPSB_ERR("received reserved rcode %d from node %d",                                 (packet->header[1] >> 12) & 0xf,                                  packet->node_id);                        return -EAGAIN;                }                HPSB_PANIC("reached unreachable code 1 in %s", __FUNCTION__);        case ACK_BUSY_X:        case ACK_BUSY_A:        case ACK_BUSY_B:                return -EBUSY;        case ACK_TYPE_ERROR:                return -EACCES;        case ACK_COMPLETE:                if (packet->tcode == TCODE_WRITEQ                    || packet->tcode == TCODE_WRITEB) {                        return 0;                } else {                        HPSB_ERR("impossible ack_complete from node %d "                                 "(tcode %d)", packet->node_id, packet->tcode);                        return -EAGAIN;                }        case ACK_DATA_ERROR:                if (packet->tcode == TCODE_WRITEB                    || packet->tcode == TCODE_LOCK_REQUEST) {                        return -EAGAIN;                } else {                        HPSB_ERR("impossible ack_data_error from node %d "                                 "(tcode %d)", packet->node_id, packet->tcode);                        return -EAGAIN;                }        case ACKX_NONE:        case ACKX_SEND_ERROR:        case ACKX_ABORTED:        case ACKX_TIMEOUT:                /* error while sending */                return -EAGAIN;        default:                HPSB_ERR("got invalid ack %d from node %d (tcode %d)",                         packet->ack_code, packet->node_id, packet->tcode);                return -EAGAIN;        }        HPSB_PANIC("reached unreachable code 2 in %s", __FUNCTION__);}struct hpsb_packet *hpsb_make_readpacket(struct hpsb_host *host, nodeid_t node,					 u64 addr, size_t length){        struct hpsb_packet *packet;	if (length == 0)		return NULL;	packet = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));	if (!packet)		return NULL;	packet->host = host;	packet->node_id = node;	if (hpsb_get_tlabel(packet)) {		free_hpsb_packet(packet);		return NULL;	}	if (length == 4)		fill_async_readquad(packet, addr);	else		fill_async_readblock(packet, addr, length);	return packet;}struct hpsb_packet *hpsb_make_writepacket (struct hpsb_host *host, nodeid_t node,					   u64 addr, quadlet_t *buffer, size_t length){	struct hpsb_packet *packet;	if (length == 0)		return NULL;	packet = alloc_hpsb_packet(length + (length % 4 ? 4 - (length % 4) : 0));	if (!packet)		return NULL;	if (length % 4) { /* zero padding bytes */		packet->data[length >> 2] = 0;	}	packet->host = host;	packet->node_id = node;	if (hpsb_get_tlabel(packet)) {		free_hpsb_packet(packet);		return NULL;	}	if (length == 4) {		fill_async_writequad(packet, addr, buffer ? *buffer : 0);	} else {		fill_async_writeblock(packet, addr, length);		if (buffer)			memcpy(packet->data, buffer, length);	}	return packet;}

⌨️ 快捷键说明

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