aboutsummaryrefslogtreecommitdiff
path: root/externals/gridflow/optional/usb.c
blob: b12b5a105d71ce93254f0f6f5019c6dbba76932e (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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
	$Id: usb.c,v 1.2 2006-03-15 04:45:31 matju Exp $

	GridFlow
	Copyright (c) 2001,2002,2003 by Mathieu Bouchard

	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.

	See file ../COPYING for further informations on licensing terms.

	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.
*/

#include <usb.h>
#include "../base/grid.h.fcs"

static Ruby cUSB;

struct named_int {const char *name; int v;};

named_int usb_class_choice[] = {
	{"USB_CLASS_PER_INTERFACE",0},
	{"USB_CLASS_AUDIO",1},
	{"USB_CLASS_COMM",2},
	{"USB_CLASS_HID",3},
	{"USB_CLASS_PRINTER",7},
	{"USB_CLASS_MASS_STORAGE",8},
	{"USB_CLASS_HUB",9},
	{"USB_CLASS_DATA",10},
	{"USB_CLASS_VENDOR_SPEC",0xff},
	{0,0},
};

named_int usb_descriptor_types_choices[] = {
	{"USB_DT_DEVICE",0x01},
	{"USB_DT_CONFIG",0x02},
	{"USB_DT_STRING",0x03},
	{"USB_DT_INTERFACE",0x04},
	{"USB_DT_ENDPOINT",0x05},
	{"USB_DT_HID",0x21},
	{"USB_DT_REPORT",0x22},
	{"USB_DT_PHYSICAL",0x23},
	{"USB_DT_HUB",0x29},
	{0,0},
};

named_int usb_descriptor_types_sizes[] = {
	{"USB_DT_DEVICE_SIZE",18},
	{"USB_DT_CONFIG_SIZE",9},
	{"USB_DT_INTERFACE_SIZE",9},
	{"USB_DT_ENDPOINT_SIZE",7},
	{"USB_DT_ENDPOINT_AUDIO_SIZE",9},/* Audio extension */
	{"USB_DT_HUB_NONVAR_SIZE",7},
	{0,0},
};

named_int usb_endpoints_choices[] = {
	{"USB_ENDPOINT_ADDRESS_MASK",0x0f},/* in bEndpointAddress */
	{"USB_ENDPOINT_DIR_MASK",0x80},
	{"USB_ENDPOINT_TYPE_MASK",0x03},/* in bmAttributes */
	{"USB_ENDPOINT_TYPE_CONTROL",0},
	{"USB_ENDPOINT_TYPE_ISOCHRONOUS",1},
	{"USB_ENDPOINT_TYPE_BULK",2},
	{"USB_ENDPOINT_TYPE_INTERRUPT",3},
	{0,0},
};

named_int usb_requests_choice[] = {
	{"USB_REQ_GET_STATUS",0x00},
	{"USB_REQ_CLEAR_FEATURE",0x01},
	{"USB_REQ_SET_FEATURE",0x03},
	{"USB_REQ_SET_ADDRESS",0x05},
	{"USB_REQ_GET_DESCRIPTOR",0x06},
	{"USB_REQ_SET_DESCRIPTOR",0x07},
	{"USB_REQ_GET_CONFIGURATION",0x08},
	{"USB_REQ_SET_CONFIGURATION",0x09},
	{"USB_REQ_GET_INTERFACE",0x0A},
	{"USB_REQ_SET_INTERFACE",0x0B},
	{"USB_REQ_SYNCH_FRAME",0x0C},
	{0,0},
};

named_int usb_type_choice[] = {
	{"USB_TYPE_STANDARD",(0x00 << 5)},
	{"USB_TYPE_CLASS",(0x01 << 5)},
	{"USB_TYPE_VENDOR",(0x02 << 5)},
	{"USB_TYPE_RESERVED",(0x03 << 5)},
	{0,0},
};

named_int usb_recipient_choice[] = {
	{"USB_RECIP_DEVICE",0x00},
	{"USB_RECIP_INTERFACE",0x01},
	{"USB_RECIP_ENDPOINT",0x02},
	{"USB_RECIP_OTHER",0x03},
	{0,0},
};

named_int usb_misc[] = {
	{"USB_MAXENDPOINTS",32},
	{"USB_MAXINTERFACES",32},
	{"USB_MAXALTSETTING",128},
	{"USB_MAXCONFIG",8},
	{"USB_ENDPOINT_IN",0x80},
	{"USB_ENDPOINT_OUT",0x00},
	{"USB_ERROR_BEGIN",500000},
	{0,0},
};

named_int* usb_all_defines[] = {
	usb_class_choice,
	usb_descriptor_types_choices,
	usb_descriptor_types_sizes,
	usb_endpoints_choices,
	usb_requests_choice,
	usb_type_choice,
	usb_recipient_choice,
	usb_misc,
};

#define COMMA ,

//14
#define USB_DEVICE_DESCRIPTOR(MANGLE,SEP) \
	MANGLE(bLength)SEP\
	MANGLE(bDescriptorType)SEP\
	MANGLE(bcdUSB)SEP\
	MANGLE(bDeviceClass)SEP\
	MANGLE(bDeviceSubClass)SEP\
	MANGLE(bDeviceProtocol)SEP\
	MANGLE(bMaxPacketSize0)SEP\
	MANGLE(idVendor)SEP\
	MANGLE(idProduct)SEP\
	MANGLE(bcdDevice)SEP\
	MANGLE(iManufacturer)SEP\
	MANGLE(iProduct)SEP\
	MANGLE(iSerialNumber)SEP\
	MANGLE(bNumConfigurations)

//8
#define USB_ENDPOINT_DESCRIPTOR(MANGLE,SEP) \
	MANGLE(bLength)SEP\
	MANGLE(bDescriptorType)SEP\
	MANGLE(bEndpointAddress)SEP\
	MANGLE(bmAttributes)SEP\
	MANGLE(wMaxPacketSize)SEP\
	MANGLE(bInterval)SEP\
	MANGLE(bRefresh)SEP\
	MANGLE(bSynchAddress)
//	  MANGLE(extras)

//9
#define USB_INTERFACE_DESCRIPTOR(MANGLE,SEP) \
	MANGLE(bLength)SEP\
	MANGLE(bDescriptorType)SEP\
	MANGLE(bInterfaceNumber)SEP\
	MANGLE(bAlternateSetting)SEP\
	MANGLE(bNumEndpoints)SEP\
	MANGLE(bInterfaceClass)SEP\
	MANGLE(bInterfaceSubClass)SEP\
	MANGLE(bInterfaceProtocol)SEP\
	MANGLE(iInterface)
//        MANGLE(endpoint)
//        MANGLE(extras)

//8
#define USB_CONFIG_DESCRIPTOR(MANGLE,SEP) \
	MANGLE(bLength)SEP\
	MANGLE(bDescriptorType)SEP\
	MANGLE(wTotalLength)SEP\
	MANGLE(bNumInterfaces)SEP\
	MANGLE(bConfigurationValue)SEP\
	MANGLE(iConfiguration)SEP\
	MANGLE(bmAttributes)SEP\
	MANGLE(MaxPower)
//	  MANGLE(interface)
//	  MANGLE(extras)

static Ruby usb_get_endpoint (struct usb_endpoint_descriptor *self) {
#define MANGLE(X) INT2NUM(self->X)
	return rb_funcall(rb_const_get(cUSB,SI(Endpoint)),SI(new),8,
		USB_ENDPOINT_DESCRIPTOR(MANGLE,COMMA));
#undef MANGLE
}

static Ruby usb_get_interface (struct usb_interface_descriptor *self) {
#define MANGLE(X) INT2NUM(self->X)
	return rb_funcall(rb_const_get(cUSB,SI(Interface)),SI(new),9+1,
		USB_INTERFACE_DESCRIPTOR(MANGLE,COMMA),
		usb_get_endpoint(self->endpoint));
#undef MANGLE
}

static Ruby usb_get_config (struct usb_config_descriptor *self) {
	if (!self) {
		fprintf(stderr,"warning: usb_get_config: null pointer\n");
		return Qnil;
	}
#define MANGLE(X) INT2NUM(self->X)
	Ruby interface = rb_ary_new();
	for (int i=0; i<self->interface->num_altsetting; i++) {
		rb_ary_push(interface, usb_get_interface(&self->interface->altsetting[i]));
	}
	return rb_funcall(rb_const_get(cUSB,SI(Config)),SI(new),8+1,
		USB_CONFIG_DESCRIPTOR(MANGLE,COMMA), interface);
#undef MANGLE
}

static Ruby usb_scan_bus (usb_bus *bus) {
	Ruby rbus = rb_ary_new();
	for (struct usb_device *dev=bus->devices; dev; dev=dev->next) {
		struct usb_device_descriptor *devd = &dev->descriptor;
		Ruby config = rb_ary_new();
		for (int i=0; i<devd->bNumConfigurations; i++) {
			rb_ary_push(config,usb_get_config(&dev->config[i]));
		}
#define MANGLE(X) INT2NUM(devd->X)
		rb_ary_push(rbus, rb_funcall(rb_const_get(cUSB,SI(Device)),SI(new),14+3,
			USB_DEVICE_DESCRIPTOR(MANGLE,COMMA),
			rb_str_new2(dev->filename),
			PTR2FIX(dev),
			config));
#undef MANGLE
	}
	return rbus;
}

\class USB < CObject
class USB : public CObject {
	usb_dev_handle *h;
public:
	\decl void initialize (Ruby dev);
	\decl int close ();
	\decl int bulk_write (int ep, String s, int timeout);
	\decl int bulk_read (int ep, String s, int timeout);
	\decl int claim_interface(int interface);
	\decl int release_interface(int interface);
	\decl int set_configuration(int configuration);
	\decl int set_altinterface(int alternate);
	\decl int control_msg(int requesttype, int request, int value, int index, String s, int timeout);
	\decl int resetep(int ep);
	\decl int clear_halt(int ep);
	\decl int reset();
	//\decl int get_string(int index, int langid, String s);
	//\decl int get_string_simple(int index, String s);
};

\def void initialize (Ruby dev) {
	Ruby ptr = rb_funcall(dev,SI(ptr),0);
	rb_ivar_set(rself, SI(@dev), ptr);
	h = usb_open(FIX2PTR(struct usb_device,ptr));
	if (!h) RAISE("usb_open returned null handle");
}

\def int close () {
	if (!h) RAISE("USB closed");
	int r = usb_close(h);
	h=0;
	return r;
}

#define TRAP(stuff) int r=(stuff); if (r<0) RAISE("%s", usb_strerror()); else return r;

\def int bulk_write (int ep, String s, int timeout) {
	if (!h) RAISE("USB closed");
	TRAP(usb_bulk_write(h, ep, rb_str_ptr(s), rb_str_len(s), timeout));}
\def int bulk_read (int ep, String s, int timeout) {
	if (!h) RAISE("USB closed");
	gfpost("%d, '%s', %d",ep,rb_str_ptr(s),timeout);
	TRAP(usb_bulk_read(h, ep, rb_str_ptr(s), rb_str_len(s), timeout));}
\def int claim_interface(int interface) {
	TRAP(usb_claim_interface(h, interface));}
\def int release_interface(int interface) {
	TRAP(usb_release_interface(h, interface));}
\def int set_configuration(int configuration) {
	TRAP(usb_set_configuration(h, configuration));}
\def int set_altinterface(int alternate) {
	TRAP(usb_set_altinterface(h, alternate));}
\def int control_msg(int requesttype, int request, int value, int index, String s, int timeout) {
	TRAP(usb_control_msg(h, requesttype, request, value, index, rb_str_ptr(s), rb_str_len(s), timeout));}
\def int resetep(int ep) {
	TRAP(usb_resetep(h, ep));}
\def int clear_halt(int ep) {
	TRAP(usb_clear_halt(h, ep));}
\def int reset() {
	TRAP(usb_reset(h));}
/*\def int get_string(int index, int langid, String s) {
	TRAP(usb_get_string(h, index, langid, rb_str_ptr(s), rb_str_len(s)));}*/
/*\def int get_string_simple(int index, String s) {
	TRAP(usb_get_string_simple(h, index, rb_str_ptr(s), rb_str_len(s)));}*/

// not handled yet:
// struct usb_string_descriptor
// struct usb_hid_descriptor
// void usb_set_debug(int level);
// Device usb_device(USB dev);
// get_string and get_string_simple (not present in libusb 0.1.4)

\classinfo
\end class USB

static Ruby USB_s_new(Ruby argc, Ruby *argv, Ruby qlass) {
	USB *self = new USB();
	Ruby rself = Data_Wrap_Struct(qlass, 0, CObject_free, self);
	self->rself = rself;
	Ruby keep = rb_ivar_get(mGridFlow, rb_intern("@fobjects"));
	rb_hash_aset(keep,rself,Qtrue); /* prevent sweeping (leak) (!@#$ WHAT??) */
	rb_funcall2(rself,SI(initialize),argc,argv);
	return rself;
}

#define SDEF(_class_,_name_,_argc_) \
	rb_define_singleton_method(c##_class_,#_name_,(RMethod)_class_##_s_##_name_,_argc_)

void startup_usb () {
	cUSB = rb_define_class_under(mGridFlow, "USB", rb_cObject);
	//rb_define_singleton_method(cUSB, "new", USB_new, 1);
	EVAL("class Symbol; def decap; x=to_s; x[0..0]=x[0..0].downcase; x.intern end end");
	SDEF(USB,new,-1);
	define_many_methods(cUSB, ciUSB.methodsn, ciUSB.methods);
#define MANGLE(X) rb_funcall(SYM(X),SI(decap),0)
	rb_const_set(cUSB, SI(Device), rb_funcall(EVAL("Struct"),SI(new),14+3,
		USB_DEVICE_DESCRIPTOR(MANGLE,COMMA), SYM(filename), SYM(ptr), SYM(config)));
	rb_const_set(cUSB, SI(Endpoint), rb_funcall(EVAL("Struct"),SI(new),8,
		USB_ENDPOINT_DESCRIPTOR(MANGLE,COMMA)));
	rb_const_set(cUSB, SI(Interface), rb_funcall(EVAL("Struct"),SI(new),9+1,
		USB_INTERFACE_DESCRIPTOR(MANGLE,COMMA), SYM(endpoint)));
	rb_const_set(cUSB, SI(Config), rb_funcall(EVAL("Struct"),SI(new),8+1,
		USB_CONFIG_DESCRIPTOR(MANGLE,COMMA), SYM(interface)));
#undef MANGLE
	for(int i=0; i<COUNT(usb_all_defines); i++) {
		named_int *ud = usb_all_defines[i];
		for(; ud->name; ud++) {
			rb_const_set(cUSB, rb_intern(ud->name), INT2NUM(ud->v));
		}
	}
	//usb_set_debug(42);
	usb_init();
	usb_find_busses();
	usb_find_devices();
	Ruby busses = rb_hash_new();
	rb_ivar_set(cUSB, SI(@busses), busses);
	for (usb_bus *bus=usb_get_busses(); bus; bus=bus->next) {
		rb_hash_aset(busses,rb_str_new2(bus->dirname),usb_scan_bus(bus));
	}
	//IEVAL(cUSB,"STDERR.print '@busses = '; STDERR.puts @busses.inspect");
	rb_require("gridflow/optional/usb.rb");
}