summaryrefslogtreecommitdiffstats
path: root/drivers/ieee1394/cmp.c
blob: 69aed26e83a1a97d1634225765396a4220540389 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
/* -*- c-basic-offset: 8 -*-
 *
 * cmp.c - Connection Management Procedures
 * Copyright (C) 2001 Kristian H�gsberg
 *
 * 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.
 */

/* TODO
 * ----
 *
 * - Implement IEC61883-1 output plugs and connection management.
 *   This should probably be part of the general subsystem, as it could
 *   be shared with dv1394.
 *
 * - Add IEC61883 unit directory when loading this module.  This
 *   requires a run-time changeable config rom.
 */

#include <linux/module.h>
#include <linux/list.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <linux/wait.h>
#include <linux/interrupt.h>

#include "hosts.h"
#include "highlevel.h"
#include "ieee1394.h"
#include "ieee1394_core.h"
#include "cmp.h"

struct plug {
	union {
		struct cmp_pcr pcr;
		quadlet_t quadlet;
	} u;
	void (*update)(struct cmp_pcr *plug, void *data);
	void *data;
};

struct cmp_host {
	struct hpsb_host *host;

	union {
		struct cmp_mpr ompr;
		quadlet_t ompr_quadlet;
	} u;
	struct plug opcr[2];

	union {
		struct cmp_mpr impr;
		quadlet_t impr_quadlet;
	} v;
	struct plug ipcr[2];
};

enum {
	CMP_P2P_CONNECTION,
	CMP_BC_CONNECTION
};

#define CSR_PCR_MAP      0x900
#define CSR_PCR_MAP_END  0x9fc

static struct hpsb_highlevel cmp_highlevel;

static void cmp_add_host(struct hpsb_host *host);
static void cmp_host_reset(struct hpsb_host *host);
static int pcr_read(struct hpsb_host *host, int nodeid, quadlet_t *buf,
		    u64 addr, size_t length, u16 flags);
static int pcr_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
		    u64 addr, quadlet_t data, quadlet_t arg, int extcode, u16 flags);

static struct hpsb_highlevel cmp_highlevel = {
	.name =		"cmp",
	.add_host =	cmp_add_host,
	.host_reset =	cmp_host_reset,
};

static struct hpsb_address_ops pcr_ops = {
	.read =	pcr_read,
	.lock =	pcr_lock,
};


struct cmp_pcr *
cmp_register_opcr(struct hpsb_host *host, int opcr_number, int payload,
		  void (*update)(struct cmp_pcr *pcr, void *data),
		  void *data)
{
	struct cmp_host *ch;
	struct plug *plug;

	ch = hpsb_get_hostinfo(&cmp_highlevel, host);

	if (opcr_number >= ch->u.ompr.nplugs ||
	    ch->opcr[opcr_number].update != NULL)
		return NULL;

	plug = &ch->opcr[opcr_number];
	plug->u.pcr.online = 1;
	plug->u.pcr.bcast_count = 0;
	plug->u.pcr.p2p_count = 0;
	plug->u.pcr.overhead = 0;
	plug->u.pcr.payload = payload;
	plug->update = update;
	plug->data = data;

	return &plug->u.pcr;
}

void cmp_unregister_opcr(struct hpsb_host *host, struct cmp_pcr *opcr)
{
	struct cmp_host *ch;
	struct plug *plug;

	ch = hpsb_get_hostinfo(&cmp_highlevel, host);
	plug = (struct plug *)opcr;
	if (plug - ch->opcr >= ch->u.ompr.nplugs) BUG();

	plug->u.pcr.online = 0;
	plug->update = NULL;
}

static void reset_plugs(struct cmp_host *ch)
{
	int i;

	ch->u.ompr.non_persistent_ext = 0xff;
	for (i = 0; i < ch->u.ompr.nplugs; i++) {
		ch->opcr[i].u.pcr.bcast_count = 0;
		ch->opcr[i].u.pcr.p2p_count = 0;
		ch->opcr[i].u.pcr.overhead = 0;
	}
}

static void cmp_add_host(struct hpsb_host *host)
{
	struct cmp_host *ch = hpsb_create_hostinfo(&cmp_highlevel, host, sizeof (*ch));

	if (ch == NULL) {
		HPSB_ERR("Failed to allocate cmp_host");
		return;
	}

	hpsb_register_addrspace(&cmp_highlevel, host, &pcr_ops,
				CSR_REGISTER_BASE + CSR_PCR_MAP,
				CSR_REGISTER_BASE + CSR_PCR_MAP_END);

	ch->host = host;
	ch->u.ompr.rate = IEEE1394_SPEED_100;
	ch->u.ompr.bcast_channel_base = 63;
	ch->u.ompr.nplugs = 2;

	reset_plugs(ch);
}

static void cmp_host_reset(struct hpsb_host *host)
{
	struct cmp_host *ch;

	ch = hpsb_get_hostinfo(&cmp_highlevel, host);
	if (ch == NULL) {
		HPSB_ERR("cmp: Tried to reset unknown host");
		return;
	}

	reset_plugs(ch);
}

static int pcr_read(struct hpsb_host *host, int nodeid, quadlet_t *buf,
		    u64 addr, size_t length, u16 flags)
{
	int csraddr = addr - CSR_REGISTER_BASE;
	int plug;
	struct cmp_host *ch;

	if (length != 4)
		return RCODE_TYPE_ERROR;

	ch = hpsb_get_hostinfo(&cmp_highlevel, host);
	if (csraddr == 0x900) {
		*buf = cpu_to_be32(ch->u.ompr_quadlet);
		return RCODE_COMPLETE;
	}
	else if (csraddr < 0x904 + ch->u.ompr.nplugs * 4) {
		plug = (csraddr - 0x904) / 4;
		*buf = cpu_to_be32(ch->opcr[plug].u.quadlet);
		return RCODE_COMPLETE;
	}
	else if (csraddr < 0x980) {
		return RCODE_ADDRESS_ERROR;
	}
	else if (csraddr == 0x980) {
		*buf = cpu_to_be32(ch->v.impr_quadlet);
		return RCODE_COMPLETE;
	}
	else if (csraddr < 0x984 + ch->v.impr.nplugs * 4) {
		plug = (csraddr - 0x984) / 4;
		*buf = cpu_to_be32(ch->ipcr[plug].u.quadlet);
		return RCODE_COMPLETE;
	}
	else
		return RCODE_ADDRESS_ERROR;
}

static int pcr_lock(struct hpsb_host *host, int nodeid, quadlet_t *store,
		    u64 addr, quadlet_t data, quadlet_t arg, int extcode, u16 flags)
{
	int csraddr = addr - CSR_REGISTER_BASE;
	int plug;
	struct cmp_host *ch;

	ch = hpsb_get_hostinfo(&cmp_highlevel, host);

	if (extcode != EXTCODE_COMPARE_SWAP)
		return RCODE_TYPE_ERROR;

	if (csraddr == 0x900) {
		/* FIXME: Ignore writes to bits 30-31 and 0-7 */
		*store = cpu_to_be32(ch->u.ompr_quadlet);
		if (arg == cpu_to_be32(ch->u.ompr_quadlet))
			ch->u.ompr_quadlet = be32_to_cpu(data);

		return RCODE_COMPLETE;
	}
	if (csraddr < 0x904 + ch->u.ompr.nplugs * 4) {
		plug = (csraddr - 0x904) / 4;
		*store = cpu_to_be32(ch->opcr[plug].u.quadlet);

		if (arg == *store)
			ch->opcr[plug].u.quadlet = be32_to_cpu(data);

		if (be32_to_cpu(*store) != ch->opcr[plug].u.quadlet &&
		    ch->opcr[plug].update != NULL)
			ch->opcr[plug].update(&ch->opcr[plug].u.pcr,
					      ch->opcr[plug].data);

		return RCODE_COMPLETE;
	}
	else if (csraddr < 0x980) {
		return RCODE_ADDRESS_ERROR;
	}
	else if (csraddr == 0x980) {
		/* FIXME: Ignore writes to bits 24-31 and 0-7 */
		*store = cpu_to_be32(ch->u.ompr_quadlet);
		if (arg == cpu_to_be32(ch->u.ompr_quadlet))
			ch->u.ompr_quadlet = be32_to_cpu(data);

		return RCODE_COMPLETE;
	}
	else if (csraddr < 0x984 + ch->v.impr.nplugs * 4) {
		plug = (csraddr - 0x984) / 4;
		*store = cpu_to_be32(ch->ipcr[plug].u.quadlet);

		if (arg == *store)
			ch->ipcr[plug].u.quadlet = be32_to_cpu(data);

		if (be32_to_cpu(*store) != ch->ipcr[plug].u.quadlet &&
		    ch->ipcr[plug].update != NULL)
			ch->ipcr[plug].update(&ch->ipcr[plug].u.pcr,
					      ch->ipcr[plug].data);

		return RCODE_COMPLETE;
	}
	else
		return RCODE_ADDRESS_ERROR;
}


/* Module interface */

MODULE_AUTHOR("Kristian Hogsberg <hogsberg@users.sf.net>");
MODULE_DESCRIPTION("Connection Management Procedures (CMP)");
MODULE_SUPPORTED_DEVICE("cmp");
MODULE_LICENSE("GPL");

EXPORT_SYMBOL(cmp_register_opcr);
EXPORT_SYMBOL(cmp_unregister_opcr);

static int __init cmp_init_module (void)
{
	hpsb_register_highlevel (&cmp_highlevel);

	HPSB_INFO("Loaded CMP driver");

	return 0;
}

static void __exit cmp_exit_module (void)
{
        hpsb_unregister_highlevel(&cmp_highlevel);

	HPSB_INFO("Unloaded CMP driver");
}

module_init(cmp_init_module);
module_exit(cmp_exit_module);