aboutsummaryrefslogtreecommitdiffstats
path: root/drv_USBHUB.c
blob: 770d50b7474a0120bbdf4bc81b02896bd133c0be (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
/* $Id$
 * $URL$
 *
 * new style driver for USBLCD displays
 *
 * Copyright (C) 2006 Ernst Bachmann <e.bachmann@xebec.de>
 * Copyright (C) 2004,2006 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * Based on the USBLCD driver Copyright (C) 2003 Michael Reinelt <reinelt@eunet.at>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux 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, or (at your option)
 * any later version.
 *
 * LCD4Linux 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.
 *
 */

/* 
 *
 * exported fuctions:
 *
 * struct DRIVER drv_USBHUB
 *
 */

#include "config.h"

#ifdef HAVE_USB_H
# include <usb.h>
#else
# error The USB-HUB driver only makes sense with USB support
#endif

#include "debug.h"
#include "cfg.h"
#include "qprintf.h"
#include "udelay.h"
#include "drv.h"
#include "drv_generic_gpio.h"



#define HUB_CONTROL_PORT 0x23
#define HUB_SET_FEATURE 3
#define HUB_SET_INDICATOR 22

static char Name[] = "USBHUB";

/* TODO: Better not specify defaults here, 
 * instead look for the first suitable HUB arround if
 * no Vendor/Product specified in config.
 */

static int hubVendor = 0x0409;
static int hubProduct = 0x0058;

static usb_dev_handle *hub = NULL;

typedef struct _usb_hub_descriptor {
    u_int8_t bLength;
    u_int8_t bDescriptorType;
    u_int8_t nNbrPorts;
    u_int8_t wHubCharacteristicLow;
    u_int8_t wHubCharacteristicHigh;
    u_int8_t bPwrOn2PwrGood;
    u_int8_t bHubContrCurrent;
    u_int8_t deviceRemovable;
    u_int8_t PortPwrCtrlMask[8];
} usb_hub_descriptor;

/****************************************/
/***  hardware dependant functions    ***/
/****************************************/


static int drv_UH_open(void)
{
    struct usb_bus *busses, *bus;
    struct usb_device *dev;

    hub = NULL;

    info("%s: scanning for an USB HUB (0x%04x:0x%04x)...", Name, hubVendor, hubProduct);

    usb_init();
    usb_find_busses();
    usb_find_devices();
    busses = usb_get_busses();

    for (bus = busses; bus; bus = bus->next) {
	for (dev = bus->devices; dev; dev = dev->next) {
	    if ((dev->descriptor.idVendor == hubVendor) && (dev->descriptor.idProduct == hubProduct)) {

		unsigned int v = dev->descriptor.bcdDevice;

		info("%s: found USBHUB V%1d%1d.%1d%1d on bus %s device %s", Name,
		     (v & 0xF000) >> 12, (v & 0xF00) >> 8, (v & 0xF0) >> 4, (v & 0xF), bus->dirname, dev->filename);

		if (dev->descriptor.bDeviceClass != USB_CLASS_HUB) {
		    error("%s: the specified device claims to be no HUB", Name);
		    return -1;
		}

		hub = usb_open(dev);
		if (!hub) {
		    error("%s: usb_open() failed!", Name);
		    return -1;
		}
		return 0;
	    }
	}
    }
    error("%s: could not find a USB HUB", Name);
    return -1;
}


static int drv_UH_close(void)
{
    debug("closing USB handle");

    usb_close(hub);

    return 0;
}


/*
 * Set the Indicator status on port "num+1" to val.
 * according to the USB Specification, the following values would be allowed:
 *
 *   0 : Automatic color (display link state etc)
 *   1 : Amber
 *   2 : Green
 *   3 : Off
 *   4..255: Reserved
 *
 */

static int drv_UH_set(const int num, const int val)
{
    int ret;

    if (!hub)
	return -1;

    if (val < 0 || val > 3) {
	info("%s: value %d out of range (0..3)", Name, val);
	return -1;
    }

    if ((ret = usb_control_msg(hub,
			       HUB_CONTROL_PORT,
			       HUB_SET_FEATURE, HUB_SET_INDICATOR, (val << 8) | (num + 1), NULL, 0, 1000)) != 0) {
	info("%s: usb_control_msg failed with %d", Name, ret);
	return -1;
    }

    return 0;
}


static int drv_UH_start(const char *section, const __attribute__ ((unused))
			int quiet)
{
    char *buf;

    usb_hub_descriptor hub_desc;
    int ret;


    buf = cfg_get(section, "Vendor", NULL);
    if (buf) {
	if (!*buf) {
	    error("%s: Strange Vendor Specification", Name);
	    return -1;
	}
	if (sscanf(buf, "0x%x", &hubVendor) != 1) {
	    error("%s: Strange Vendor Specification: [%s]", Name, buf);
	    return -1;
	}
    }

    buf = cfg_get(section, "Product", NULL);
    if (buf) {
	if (!*buf) {
	    error("%s: Strange Product Specification", Name);
	    return -1;
	}
	if (sscanf(buf, "0x%x", &hubProduct) != 1) {
	    error("%s: Strange Product Specification: [%s]", Name, buf);
	    return -1;
	}
    }

    if (drv_UH_open() < 0) {
	return -1;
    }


    if ((ret = usb_control_msg(hub,
			       USB_ENDPOINT_IN | USB_TYPE_CLASS | USB_RECIP_DEVICE,
			       USB_REQ_GET_DESCRIPTOR, USB_DT_HUB << 8, 0, (char *) &hub_desc, sizeof(hub_desc),
			       1000)) <= 8) {
	error("%s: hub_get_descriptor failed with %d", Name, ret);
	drv_UH_close();
	return -1;
    }
    GPOS = hub_desc.nNbrPorts;
    debug("%s: HUB claims to have %d ports. Configuring them as GPOs", Name, GPOS);
    if (!(hub_desc.wHubCharacteristicLow & 0x80)) {
	error("%s: HUB claims to have no Indicator LEDs (Characteristics 0x%04x). Bailing out.", Name,
	      (hub_desc.wHubCharacteristicHigh << 8) | hub_desc.wHubCharacteristicLow);
	/* The HUB Tells us that there are no LEDs to control. Breaking? Maybe don't trust it and continue anyways? */
	drv_UH_close();
	return -1;

    }

    return 0;
}


/****************************************/
/***            plugins               ***/
/****************************************/

/* none at the moment... */


/****************************************/
/***        widget callbacks          ***/
/****************************************/


/* using drv_generic_text_draw(W) */
/* using drv_generic_text_icon_draw(W) */
/* using drv_generic_text_bar_draw(W) */


/****************************************/
/***        exported functions        ***/
/****************************************/


/* list models */
int drv_UH_list(void)
{
    printf("generic");
    return 0;
}


/* initialize driver & display */
int drv_UH_init(const char *section, const int quiet)
{
    int ret;
    int i;

    info("%s: %s", Name, "$Rev$");



    /* start display */
    if ((ret = drv_UH_start(section, quiet)) != 0)
	return ret;


    /* real worker functions */
    drv_generic_gpio_real_set = drv_UH_set;


    /* initialize generic GPIO driver */
    if ((ret = drv_generic_gpio_init(section, Name)) != 0)
	return ret;

    /* register gpio widget, done already by generic_gpio */

    /* register plugins */
    /* none at the moment... */

    /* greeting */
    if (!quiet) {
	/* Light all LEDS green for a greeting */
	for (i = 0; i < GPOS; ++i) {
	    drv_UH_set(i, 2);
	}
	sleep(1);
	for (i = 0; i < GPOS; ++i) {
	    drv_UH_set(i, 3);	// OFF
	}
    }


    return 0;
}


/* close driver & display */
int drv_UH_quit(const int quiet)
{
    int i;
    debug("%s: shutting down.", Name);

    /* say goodbye... */
    if (!quiet) {
	/* Light all LEDS amber for a goodbye */
	for (i = 0; i < GPOS; ++i) {
	    drv_UH_set(i, 1);
	}
	sleep(1);

    }

    drv_generic_gpio_quit();

    drv_UH_close();

    info("%s: shutdown complete.", Name);
    return 0;
}


DRIVER drv_USBHUB = {
  name:Name,
  list:drv_UH_list,
  init:drv_UH_init,
  quit:drv_UH_quit,
};