File: device.c

package info (click to toggle)
usbmgr 1.0.0-5
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 460 kB
  • ctags: 323
  • sloc: ansic: 2,118; sh: 545; makefile: 154
file content (230 lines) | stat: -rw-r--r-- 4,997 bytes parent folder | download | duplicates (3)
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
/*
 *  
 *  Copyright (c) by Shuu Yamaguchi <shuu@wondernetworkresources.com>
 *
 *  $Id: device.c,v 4.2 2001/01/04 14:31:32 shuu Exp $
 *
 *  Can be freely distributed and used under the terms of the GNU GPL.
 */
#include	<stdio.h>
#include	<string.h>
#include	<limits.h>
#include	<unistd.h>
#include	<stdlib.h>

#include	"usbmgr.h"

static struct node *device_ring = NULL;


/*
 * create usbdevice
 */
#if 0
struct usbdev *
create_device(struct usbmgr_device_descriptor desc)
{
	struct usbdev *dev;

	dev = (struct usbdev *)Malloc(sizeof(struct usbdev));
	if (dev == NULL)
		return NULL;
	INIT_NODE(&(dev->link));
	dev->desc = desc;
	dev->status = USBMGR_ST_UNKNOWN;
	dev->type = USBMGR_TYPE_NONE;
	link_node(&device_ring,&(dev->link));

	return dev;
}
#else
struct node *
create_device(struct usbmgr_device *dev)
{
	struct node *np;

	np = create_node_data_link(&device_ring,dev,sizeof(struct usbmgr_device)); 
	if (np == NULL)
		return NULL;
	NODE_STATUS(np) = USBMGR_ST_UNKNOWN;
	NODE_TYPE(np) = USBMGR_TYPE_NONE;

	return np;
}
#endif

/*
 * search device using config
 */
#if 0
struct usbdev *
find_device(struct usbdev target)
{
	struct usbdev *dev;

	if (device_ring == NULL)
		return NULL;
	for(dev = (struct usbdev *)device_ring; ;dev = (struct usbdev *)dev->link.next) {
		if ((dev->desc.bDeviceClass == target.desc.bDeviceClass) &&
			(dev->desc.bDeviceSubClass == target.desc.bDeviceSubClass) &&
			(dev->desc.bDeviceProtocol == target.desc.bDeviceProtocol) &&
			(dev->desc.idVendor == target.desc.idVendor) &&
			(dev->desc.idProduct == target.desc.idProduct) ) {
			dev->status |= USBMGR_ST_ACTIVE;
			return dev;
		}
		if (dev->link.next == (struct node *)device_ring)
			break;
	}
	return NULL;
}
#else
struct node *
find_device(struct usbmgr_device *dev)
{
	struct node *np;

	if (device_ring == NULL)
		return NULL;
	for (np = device_ring; ;np = np->next) { 
		if ((DEVICE_CLASS(dev) == NODE_CLASS(np)) &&
			(DEVICE_SUBCLASS(dev) == NODE_SUBCLASS(np)) &&
			(DEVICE_PROTOCOL(dev) == NODE_PROTOCOL(np)) &&
			(DEVICE_VENDOR(dev) == NODE_VENDOR(np)) &&
			(DEVICE_PRODUCT(dev) == NODE_PRODUCT(np)) ) {

			NODE_STATUS(np) |= USBMGR_ST_ACTIVE;

			return np;
		}
		if (np->next == device_ring)
			break;
	}
	return NULL;
}
#endif

/*
 * delete device
 */
void
delete_device(int force)
{
	struct node *np,*next,*end;

	if (device_ring == NULL)
		return;
	end = device_ring->prev;
	for(np = device_ring; ;np = next) {
		next = np->next;
		if (force || !(NODE_STATUS(np) & USBMGR_ST_ACTIVE)) {
			char path[PATH_MAX+1];

			if (NODE_TYPE(np) & USBMGR_TYPE_SCRIPT) {
				if (check_vendor_file(np,path,BUILD_SCRIPT) == INVALID) {
					search_class_file(np,path,BUILD_SCRIPT);
				}
				if (load_from_file(np,path,SCRIPT_STOP) == 0)
					syslog(LOG_INFO,mesg[MSG_STOP],path);
			}
			if ((check_vendor_file(np,path,BUILD_MODULE) == INVALID)  &&
				(search_class_file(np,path,BUILD_MODULE) == INVALID)) {
				beep(INVALID);
				DPRINTF(LOG_DEBUG,"not identify device -> beep fail");
			} else {
				beep(GOOD);
				DPRINTF(LOG_DEBUG,"identify device -> beep good");
				load_from_file(np,path,MODULE_UNLOAD);
			}
			delete_node_data_link(&device_ring,np);
		} else {
			NODE_STATUS(np) &= ~USBMGR_ST_ACTIVE;
		}
		if (np == end)
			break;
	}
#ifdef	DEBUG
	if (force)
		printf("device_ring %p\n",device_ring);
#endif
}

#if 0
int
check_vendor_file(struct usbdev dev,char *path,int flag)
{
	char *file;

	if (flag & BUILD_MODULE)
		file = USBMGR_MODULE_FILE;
	else if (flag & BUILD_SCRIPT)
		file = USBMGR_SCRIPT_FILE;
	else /* BUILD_DIR */
		file = "";

	if (!(dev.type & USBMGR_TYPE_VENDOR))
		return -1;
	sprintf(path,"%s/%s/%04x/%04x/%s",
		conf_dir,vendor_dir,dev.desc.idVendor,dev.desc.idProduct,file);
	if (access(path,R_OK) == -1)
		return -1;
	return 0;
}
#else
int
check_vendor_file(struct node *np,char *path,int flag)
{
	char *file;

	if (flag & BUILD_MODULE)
		file = USBMGR_MODULE_FILE;
	else if (flag & BUILD_SCRIPT)
		file = USBMGR_SCRIPT_FILE;
	else /* BUILD_DIR */
		file = "";

	if (!(NODE_TYPE(np) & USBMGR_TYPE_VENDOR))
		return INVALID;
	sprintf(path,"%s/%s/%04x/%04x/%s",
		conf_dir,vendor_dir,
		NODE_VENDOR(np),NODE_PRODUCT(np),file);
	if (access(path,R_OK) == -1)
		return INVALID;
	return GOOD;
}
#endif

#if 0
void
validate_desc(struct usbmgr_device *dev)
{
	dev->type = USBMGR_TYPE_NONE;	
	if (dev->desc.idVendor != 0)
		dev->type |= USBMGR_TYPE_VENDOR;
	if (dev->desc.bDeviceClass != 0) 
		dev->type |= USBMGR_TYPE_CLASS;
}
#else
void
validate_desc(struct node *np)
{
	NODE_TYPE(np) = USBMGR_TYPE_NONE;	
	if (NODE_VENDOR(np) != VENDOR_NONE)
		NODE_TYPE(np) |= USBMGR_TYPE_VENDOR;
	if (NODE_CLASS(np) != CLASS_NONE) 
		NODE_TYPE(np) |= USBMGR_TYPE_CLASS;
}
#endif


void
print_device(struct node *np)
{
	if (NODE_TYPE(np) & USBMGR_TYPE_VENDOR)
		syslog(LOG_INFO,mesg[MSG_VENDOR_S],
			NODE_VENDOR(np),NODE_PRODUCT(np));
	if (NODE_TYPE(np) & USBMGR_TYPE_CLASS)
		syslog(LOG_INFO,mesg[MSG_CLASS_S],
			NODE_CLASS(np),NODE_SUBCLASS(np),NODE_PROTOCOL(np));
}