summaryrefslogtreecommitdiff
path: root/xpp/slic.c
blob: b019766ab6b365147beedadb0f5919628d8ce2a9 (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
/*
 * Written by Oron Peled <oron@actcom.co.il>
 * Copyright (C) 2004-2006, Xorcom
 *
 * All rights reserved.
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "xproto.h"
#include "slic.h"

static const char rcsid[] = "$Id$";

#ifdef	__KERNEL__
#include <linux/module.h>

extern	int print_dbg;
#include "zap_debug.h"

DEF_PARM(charp,initialize_registers, "/usr/share/zaptel/initialize_registers", "The script to initialize detected cards slics");

#else
#include <stdio.h>
#endif

int slic_cmd_direct_write(slic_cmd_t *sc, xpp_line_t lines, byte reg, byte data)
{
	struct slic_reg_d	*p = (struct slic_reg_d *)&sc->content;

	sc->lines = lines;
	sc->bytes = sizeof(struct slic_reg_d);
	SLIC_REG_INIT(p, 0, reg, data);
	return sizeof(xpp_line_t) + 1 + sc->bytes;
}

int slic_cmd_direct_read(slic_cmd_t *sc, xpp_line_t lines, byte reg)
{
	struct slic_reg_d	*p = (struct slic_reg_d *)&sc->content;

	sc->lines = lines;
	sc->bytes = sizeof(struct slic_reg_d);
	SLIC_REG_INIT(p, 1, reg, 0);
	return sizeof(xpp_line_t) + 1 + sc->bytes;
}

int slic_cmd_indirect_write(slic_cmd_t *sc, xpp_line_t lines, byte reg, byte data_low, byte data_high)
{
	struct slic_reg_iw	*p = (struct slic_reg_iw *)&sc->content;

	sc->lines = lines;
	sc->bytes = sizeof(struct slic_reg_iw);
	SLIC_REG_INIT(&p->iw_data_low, 0, 0x1C, data_low);
	SLIC_REG_INIT(&p->iw_data_high, 0, 0x1D, data_high);
	SLIC_REG_INIT(&p->iw_reg, 0, 0x1E, reg);
	return sizeof(xpp_line_t) + 1 + sc->bytes;
}

int slic_cmd_indirect_read(slic_cmd_t *sc, xpp_line_t lines, byte reg)
{
	struct slic_reg_ir	*p = (struct slic_reg_ir *)&sc->content;

	sc->lines = lines;
	sc->bytes = sizeof(struct slic_reg_ir);
	SLIC_REG_INIT(&p->ir_reg, 0, 0x1E, reg);
	return sizeof(xpp_line_t) + 1 + sc->bytes;
}

void dump_slic_cmd(const char msg[], slic_cmd_t *sc)
{
	int	i;
	struct slic_reg_d	*sr;
	int	last_data_low = -1;
	int	last_data_high = -1;

	sr = (struct slic_reg_d *)&sc->content;
	if(sc->bytes > sizeof(sc->content)) {
		NOTICE("%s: Bug: sc->bytes = %d\n", __FUNCTION__, sc->bytes);
		return;
	}
	if(sc->bytes % 2) {
		NOTICE("%s: Bug: ODD sc->bytes = %d\n", __FUNCTION__, sc->bytes);
		return;
	}
	for(i = 0; i < sc->bytes/2; i++, sr++) {
		if(sr->reg_num == 0x1C) {
			last_data_low = sr->reg_data;
			continue;
		}
		if(sr->reg_num == 0x1D) {
			last_data_high = sr->reg_data;
			continue;
		}
		if(sr->reg_num == 0x1E) {
			if(last_data_low == -1 && last_data_high == -1)		// Indirect Read
				DBG("%s: LINES=0x%08X bytes=%d INDIRECT READ: register=0x%02X\n", msg, sc->lines, sc->bytes, sr->reg_data);
			else if(last_data_low == -1 || last_data_high == -1) {
				NOTICE("%s: BUG: PARTIAL INDIRECT: register=%d last_data_low=0x%X last_data_high=0x%X\n",
						msg, sr->reg_data, last_data_low, last_data_high);
			} else
				DBG("%s: LINES=0x%08X bytes=%d INDIRECT WRITE: register=%d data_low=0x%02x data_high=0x%02X\n",
						msg, sc->lines, sc->bytes, sr->reg_data, (byte)last_data_low, (byte)last_data_high);
			last_data_low = last_data_high = -1;
		} else {
			DBG("%s: LINES=0x%08X bytes=%d DIRECT %s: register=%d data=0x%02X\n",
				msg, sc->lines, sc->bytes, (sr->read) ? "READ" : "WRITE", sr->reg_num, sr->reg_data);
		}
	}
}

#ifdef	__KERNEL__

#define	MAX_ENV_STR	20

int run_initialize_registers(xpd_t *xpd)
{
	int	ret;
	xbus_t	*xbus;
	char	busstr[MAX_ENV_STR];
	char	xpdstr[MAX_ENV_STR];
	char	typestr[MAX_ENV_STR];
	char	revstr[MAX_ENV_STR];
	char	*argv[] = {
		initialize_registers,
		NULL
	};
	char	*envp[] = {
		busstr,
		xpdstr,
		typestr,
		revstr,
		NULL
	};

	BUG_ON(!xpd);
	xbus = xpd->xbus;
	if(!initialize_registers || !initialize_registers[0]) {
		NOTICE("%s/%s: No runtime register initialization\n", xbus->busname, xpd->xpdname);
		return 0;
	}
	DBG("%s/%s: running: '%s'\n", xbus->busname, xpd->xpdname, initialize_registers);
	snprintf(busstr, MAX_ENV_STR, "XPD_BUS=%s", xbus->busname);
	snprintf(xpdstr, MAX_ENV_STR, "XPD_NAME=%s", xpd->xpdname);
	snprintf(typestr, MAX_ENV_STR, "XPD_TYPE=%d", xpd->type);
	snprintf(revstr, MAX_ENV_STR, "XPD_REVISION=%d", xpd->revision);
	DBG("%s/%s: type=%d revision=%d\n", xbus->busname, xpd->xpdname, xpd->type, xpd->revision);
	ret = call_usermodehelper(initialize_registers, argv, envp, 1);
	if(ret != 0) {
		ERR("%s/%s: Failed running '%s' (errno %d, sig=%d)\n",
				xbus->busname, xpd->xpdname, initialize_registers,
				((unsigned)ret >> 8) & 0xFF, ret & 0xFF);
		ret = -EFAULT;
	}
	return ret;
}

EXPORT_SYMBOL(slic_cmd_direct_write);
EXPORT_SYMBOL(slic_cmd_direct_read);
EXPORT_SYMBOL(slic_cmd_indirect_write);
EXPORT_SYMBOL(slic_cmd_indirect_read);
EXPORT_SYMBOL(dump_slic_cmd);

#endif