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
|
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minix/config.h>
#include <minix/const.h>
#include <minix/devman.h>
#include <minix/usb.h>
#include <minix/sysutil.h>
#include "local.h"
#define CHECKOUTOFMEM(ptr) if(ptr == NULL) \
panic("Out of memory! (%s, line %d)", \
__FILE__, __LINE__)
static int (*bind_cb) (struct devman_usb_bind_cb_data *data, endpoint_t ep);
static int (*unbind_cb) (struct devman_usb_bind_cb_data *data, endpoint_t ep);
/****************************************************************************
* devman_usb_add_attr *
***************************************************************************/
static void
devman_usb_add_attr
(struct devman_dev *dev, const char *name, const char *data)
{
struct devman_static_attribute *attr = (struct devman_static_attribute *)
malloc(sizeof(struct devman_static_attribute));
CHECKOUTOFMEM(attr);
attr->name = malloc((strlen(name)+1)*sizeof(char));
memcpy(attr->name, name, (strlen(name)+1));
attr->data = malloc((strlen(data)+1)*sizeof(char));
memcpy(attr->data, data, (strlen(data)+1));
TAILQ_INSERT_TAIL(&dev->attrs, attr, list);
}
/****************************************************************************
* add_device_attributes *
***************************************************************************/
static void
add_device_attributes
(struct devman_usb_dev *udev)
{
int ret;
char data[32];
ret = snprintf(data,sizeof(data),"0x%02x",udev->desc->bDeviceClass);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(udev->dev, "bDeviceClass", data);
ret = snprintf(data,sizeof(data),"0x%02x",udev->desc->bDeviceSubClass);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(udev->dev, "bDeviceSubClass", data);
ret = snprintf(data,sizeof(data),"0x%02x",udev->desc->bDeviceProtocol);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(udev->dev, "bDeviceProtocol", data);
ret = snprintf(data,sizeof(data),"0x%04x",UGETW(udev->desc->idVendor));
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(udev->dev, "idVendor", data);
ret = snprintf(data,sizeof(data),"0x%04x",UGETW(udev->desc->idProduct));
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(udev->dev, "idProduct", data);
if (udev->product)
devman_usb_add_attr(udev->dev, "Product", udev->product);
if (udev->manufacturer)
devman_usb_add_attr(udev->dev, "Manufacturer", udev->manufacturer);
if (udev->serial)
devman_usb_add_attr(udev->dev, "SerialNumber", udev->serial);
devman_usb_add_attr(udev->dev, "dev_type", "USB_DEV");
}
/****************************************************************************
* add_interface_attributes *
***************************************************************************/
static void
add_interface_attributes
(struct devman_usb_interface *intf)
{
int ret;
char data[32];
ret = snprintf(data,sizeof(data),"0x%02x",intf->desc->bInterfaceNumber);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(intf->dev, "bInterfaceNumber", data);
ret = snprintf(data,sizeof(data),"0x%02x",intf->desc->bAlternateSetting);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(intf->dev, "bAlternateSetting", data);
ret = snprintf(data,sizeof(data),"0x%02x",intf->desc->bNumEndpoints);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(intf->dev, "bNumEndpoints", data);
ret = snprintf(data,sizeof(data),"0x%02x",intf->desc->bInterfaceClass);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(intf->dev, "bInterfaceClass", data);
ret = snprintf(data,sizeof(data),"0x%02x",intf->desc->bInterfaceSubClass);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(intf->dev, "bInterfaceSubClass", data);
ret = snprintf(data,sizeof(data),"0x%02x",intf->desc->bInterfaceProtocol);
if (ret < 0) {
panic("add_device_attributes: snprintf failed");
}
devman_usb_add_attr(intf->dev, "bInterfaceProtocol", data);
devman_usb_add_attr(intf->dev, "dev_type", "USB_INTF");
}
/****************************************************************************
* devman_usb_device_new *
***************************************************************************/
struct devman_usb_dev*
devman_usb_device_new
(int dev_id)
{
struct devman_usb_dev *udev = NULL;
struct devman_dev * dev = NULL;
udev = (struct devman_usb_dev *) malloc(sizeof(struct devman_usb_dev));
CHECKOUTOFMEM(udev);
/* allocate device */
dev = (struct devman_dev *) malloc(sizeof(struct devman_dev));
CHECKOUTOFMEM(dev);
udev->dev_id = dev_id;
udev->dev = dev;
dev->parent_dev_id = 0; /* For now add it directly to the root dev */
snprintf(dev->name, DEVMAN_DEV_NAME_LEN, "USB%d", dev_id);
TAILQ_INIT(&dev->attrs);
return udev;
}
/****************************************************************************
* devman_usb_device_delete *
***************************************************************************/
void devman_usb_device_delete(struct devman_usb_dev *udev)
{
int i;
struct devman_static_attribute *attr,*temp;
for (i=0; i < udev->intf_count; i++) {
TAILQ_FOREACH_SAFE(attr, &udev->interfaces[i].dev->attrs, list, temp)
{
free(attr->name);
free(attr->data);
free(attr);
}
free(udev->interfaces[i].dev);
}
TAILQ_FOREACH_SAFE(attr, &udev->dev->attrs, list, temp) {
free(attr->name);
free(attr->data);
free(attr);
}
free(udev->dev);
free(udev);
}
static int devman_usb_bind_cb(void *data, endpoint_t ep) {
if (bind_cb) {
return bind_cb((struct devman_usb_bind_cb_data *) data, ep);
} else {
return ENODEV;
}
}
static int devman_usb_unbind_cb(void *data, endpoint_t ep) {
if (unbind_cb) {
return unbind_cb((struct devman_usb_bind_cb_data *) data, ep);
} else {
return ENODEV;
}
}
/****************************************************************************
* devman_usb_device_add *
***************************************************************************/
int devman_usb_device_add(struct devman_usb_dev *dev)
{
int i,res = 0;
add_device_attributes(dev);
/* add the USB device */
dev->cb_data.dev_id = dev->dev_id;
dev->cb_data.interface = -1;
dev->dev->bind_cb = devman_usb_bind_cb;
dev->dev->unbind_cb = devman_usb_unbind_cb;
dev->dev->data = &dev->cb_data;
res = devman_add_device(dev->dev);
if (res != 0) {
panic("devman_usb_device_add(): devman_add_device failed.");
}
/* add the USB interfaces */
for (i=0; i < dev->intf_count; i++) {
/* prepare */
dev->interfaces[i].dev =
(struct devman_dev *) malloc(sizeof(struct devman_dev));
CHECKOUTOFMEM(dev->interfaces[i].dev);
TAILQ_INIT(&dev->interfaces[i].dev->attrs);
snprintf(dev->interfaces[i].dev->name, DEVMAN_DEV_NAME_LEN,
"intf%d", i);
add_interface_attributes(&dev->interfaces[i]);
dev->interfaces[i].dev->parent_dev_id = dev->dev->dev_id;
dev->interfaces[i].cb_data.dev_id = dev->dev_id;
dev->interfaces[i].cb_data.interface =
dev->interfaces[i].desc->bInterfaceNumber;
dev->interfaces[i].dev->bind_cb = devman_usb_bind_cb;
dev->interfaces[i].dev->unbind_cb = devman_usb_unbind_cb;
dev->interfaces[i].dev->data = &dev->interfaces[i].cb_data;
/* add */
res = devman_add_device(dev->interfaces[i].dev);
if (res != 0) {
panic("devman_usb_device_add(): devman_add_device failed.");
}
}
return res;
}
/****************************************************************************
* devman_usb_device_remove *
***************************************************************************/
int devman_usb_device_remove(struct devman_usb_dev *dev)
{
int i, res = 0;
for (i=0; i < dev->intf_count; i++) {
res = devman_del_device(dev->interfaces[i].dev);
if (res != 0) {
panic("devman_usb_device_remove(): devman_del_device failed.");
}
}
res = devman_del_device(dev->dev);
return res;
}
/****************************************************************************
* devman_usb_init *
***************************************************************************/
void devman_usb_init
(int (*_bind_cb) (struct devman_usb_bind_cb_data *data, endpoint_t ep),
int (*_unbind_cb) (struct devman_usb_bind_cb_data *data, endpoint_t ep))
{
bind_cb = _bind_cb;
unbind_cb = _unbind_cb;
}
|