Statistics
| Branch: | Revision:

root / hw / usb-bus.c @ ae60fea9

History | View | Annotate | Download (11.7 kB)

1
#include "hw.h"
2
#include "usb.h"
3
#include "qdev.h"
4
#include "sysemu.h"
5
#include "monitor.h"
6

    
7
static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent);
8

    
9
static char *usb_get_dev_path(DeviceState *dev);
10
static char *usb_get_fw_dev_path(DeviceState *qdev);
11

    
12
static struct BusInfo usb_bus_info = {
13
    .name      = "USB",
14
    .size      = sizeof(USBBus),
15
    .print_dev = usb_bus_dev_print,
16
    .get_dev_path = usb_get_dev_path,
17
    .get_fw_dev_path = usb_get_fw_dev_path,
18
    .props      = (Property[]) {
19
        DEFINE_PROP_STRING("port", USBDevice, port_path),
20
        DEFINE_PROP_END_OF_LIST()
21
    },
22
};
23
static int next_usb_bus = 0;
24
static QTAILQ_HEAD(, USBBus) busses = QTAILQ_HEAD_INITIALIZER(busses);
25

    
26
const VMStateDescription vmstate_usb_device = {
27
    .name = "USBDevice",
28
    .version_id = 1,
29
    .minimum_version_id = 1,
30
    .fields = (VMStateField []) {
31
        VMSTATE_UINT8(addr, USBDevice),
32
        VMSTATE_INT32(state, USBDevice),
33
        VMSTATE_INT32(remote_wakeup, USBDevice),
34
        VMSTATE_INT32(setup_state, USBDevice),
35
        VMSTATE_INT32(setup_len, USBDevice),
36
        VMSTATE_INT32(setup_index, USBDevice),
37
        VMSTATE_UINT8_ARRAY(setup_buf, USBDevice, 8),
38
        VMSTATE_END_OF_LIST(),
39
    }
40
};
41

    
42
void usb_bus_new(USBBus *bus, USBBusOps *ops, DeviceState *host)
43
{
44
    qbus_create_inplace(&bus->qbus, &usb_bus_info, host, NULL);
45
    bus->ops = ops;
46
    bus->busnr = next_usb_bus++;
47
    bus->qbus.allow_hotplug = 1; /* Yes, we can */
48
    QTAILQ_INIT(&bus->free);
49
    QTAILQ_INIT(&bus->used);
50
    QTAILQ_INSERT_TAIL(&busses, bus, next);
51
}
52

    
53
USBBus *usb_bus_find(int busnr)
54
{
55
    USBBus *bus;
56

    
57
    if (-1 == busnr)
58
        return QTAILQ_FIRST(&busses);
59
    QTAILQ_FOREACH(bus, &busses, next) {
60
        if (bus->busnr == busnr)
61
            return bus;
62
    }
63
    return NULL;
64
}
65

    
66
static int usb_qdev_init(DeviceState *qdev, DeviceInfo *base)
67
{
68
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
69
    USBDeviceInfo *info = DO_UPCAST(USBDeviceInfo, qdev, base);
70
    int rc;
71

    
72
    pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
73
    dev->info = info;
74
    dev->auto_attach = 1;
75
    QLIST_INIT(&dev->strings);
76
    rc = dev->info->init(dev);
77
    if (rc == 0 && dev->auto_attach)
78
        rc = usb_device_attach(dev);
79
    return rc;
80
}
81

    
82
static int usb_qdev_exit(DeviceState *qdev)
83
{
84
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
85
    USBBus *bus = usb_bus_from_device(dev);
86

    
87
    if (dev->attached) {
88
        usb_device_detach(dev);
89
    }
90
    bus->ops->device_destroy(bus, dev);
91
    if (dev->info->handle_destroy) {
92
        dev->info->handle_destroy(dev);
93
    }
94
    return 0;
95
}
96

    
97
void usb_qdev_register(USBDeviceInfo *info)
98
{
99
    info->qdev.bus_info = &usb_bus_info;
100
    info->qdev.init     = usb_qdev_init;
101
    info->qdev.unplug   = qdev_simple_unplug_cb;
102
    info->qdev.exit     = usb_qdev_exit;
103
    qdev_register(&info->qdev);
104
}
105

    
106
void usb_qdev_register_many(USBDeviceInfo *info)
107
{
108
    while (info->qdev.name) {
109
        usb_qdev_register(info);
110
        info++;
111
    }
112
}
113

    
114
USBDevice *usb_create(USBBus *bus, const char *name)
115
{
116
    DeviceState *dev;
117

    
118
#if 1
119
    /* temporary stopgap until all usb is properly qdev-ified */
120
    if (!bus) {
121
        bus = usb_bus_find(-1);
122
        if (!bus)
123
            return NULL;
124
        error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
125
                __FUNCTION__, bus->qbus.name, name);
126
    }
127
#endif
128

    
129
    dev = qdev_create(&bus->qbus, name);
130
    return DO_UPCAST(USBDevice, qdev, dev);
131
}
132

    
133
USBDevice *usb_create_simple(USBBus *bus, const char *name)
134
{
135
    USBDevice *dev = usb_create(bus, name);
136
    if (!dev) {
137
        hw_error("Failed to create USB device '%s'\n", name);
138
    }
139
    qdev_init_nofail(&dev->qdev);
140
    return dev;
141
}
142

    
143
static void usb_fill_port(USBPort *port, void *opaque, int index,
144
                          USBPortOps *ops, int speedmask)
145
{
146
    port->opaque = opaque;
147
    port->index = index;
148
    port->opaque = opaque;
149
    port->index = index;
150
    port->ops = ops;
151
    port->speedmask = speedmask;
152
    usb_port_location(port, NULL, index + 1);
153
}
154

    
155
void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
156
                       USBPortOps *ops, int speedmask)
157
{
158
    usb_fill_port(port, opaque, index, ops, speedmask);
159
    QTAILQ_INSERT_TAIL(&bus->free, port, next);
160
    bus->nfree++;
161
}
162

    
163
int usb_register_companion(const char *masterbus, USBPort *ports[],
164
                           uint32_t portcount, uint32_t firstport,
165
                           void *opaque, USBPortOps *ops, int speedmask)
166
{
167
    USBBus *bus;
168
    int i;
169

    
170
    QTAILQ_FOREACH(bus, &busses, next) {
171
        if (strcmp(bus->qbus.name, masterbus) == 0) {
172
            break;
173
        }
174
    }
175

    
176
    if (!bus || !bus->ops->register_companion) {
177
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
178
                      "an USB masterbus");
179
        if (bus) {
180
            error_printf_unless_qmp(
181
                "USB bus '%s' does not allow companion controllers\n",
182
                masterbus);
183
        }
184
        return -1;
185
    }
186

    
187
    for (i = 0; i < portcount; i++) {
188
        usb_fill_port(ports[i], opaque, i, ops, speedmask);
189
    }
190

    
191
    return bus->ops->register_companion(bus, ports, portcount, firstport);
192
}
193

    
194
void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
195
{
196
    if (upstream) {
197
        snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
198
                 upstream->path, portnr);
199
    } else {
200
        snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
201
    }
202
}
203

    
204
void usb_unregister_port(USBBus *bus, USBPort *port)
205
{
206
    if (port->dev)
207
        qdev_free(&port->dev->qdev);
208
    QTAILQ_REMOVE(&bus->free, port, next);
209
    bus->nfree--;
210
}
211

    
212
static int do_attach(USBDevice *dev)
213
{
214
    USBBus *bus = usb_bus_from_device(dev);
215
    USBPort *port;
216

    
217
    if (dev->attached) {
218
        error_report("Error: tried to attach usb device %s twice\n",
219
                dev->product_desc);
220
        return -1;
221
    }
222
    if (bus->nfree == 0) {
223
        error_report("Error: tried to attach usb device %s to a bus with no free ports\n",
224
                dev->product_desc);
225
        return -1;
226
    }
227
    if (dev->port_path) {
228
        QTAILQ_FOREACH(port, &bus->free, next) {
229
            if (strcmp(port->path, dev->port_path) == 0) {
230
                break;
231
            }
232
        }
233
        if (port == NULL) {
234
            error_report("Error: usb port %s (bus %s) not found\n",
235
                    dev->port_path, bus->qbus.name);
236
            return -1;
237
        }
238
    } else {
239
        port = QTAILQ_FIRST(&bus->free);
240
    }
241
    if (!(port->speedmask & dev->speedmask)) {
242
        error_report("Warning: speed mismatch trying to attach usb device %s to bus %s\n",
243
                dev->product_desc, bus->qbus.name);
244
        return -1;
245
    }
246

    
247
    dev->attached++;
248
    QTAILQ_REMOVE(&bus->free, port, next);
249
    bus->nfree--;
250

    
251
    usb_attach(port, dev);
252

    
253
    QTAILQ_INSERT_TAIL(&bus->used, port, next);
254
    bus->nused++;
255

    
256
    return 0;
257
}
258

    
259
int usb_device_attach(USBDevice *dev)
260
{
261
    USBBus *bus = usb_bus_from_device(dev);
262

    
263
    if (bus->nfree == 1 && dev->port_path == NULL) {
264
        /* Create a new hub and chain it on
265
           (unless a physical port location is specified). */
266
        usb_create_simple(bus, "usb-hub");
267
    }
268
    return do_attach(dev);
269
}
270

    
271
int usb_device_detach(USBDevice *dev)
272
{
273
    USBBus *bus = usb_bus_from_device(dev);
274
    USBPort *port;
275

    
276
    if (!dev->attached) {
277
        error_report("Error: tried to detach unattached usb device %s\n",
278
                dev->product_desc);
279
        return -1;
280
    }
281
    dev->attached--;
282

    
283
    QTAILQ_FOREACH(port, &bus->used, next) {
284
        if (port->dev == dev)
285
            break;
286
    }
287
    assert(port != NULL);
288

    
289
    QTAILQ_REMOVE(&bus->used, port, next);
290
    bus->nused--;
291

    
292
    usb_attach(port, NULL);
293

    
294
    QTAILQ_INSERT_TAIL(&bus->free, port, next);
295
    bus->nfree++;
296
    return 0;
297
}
298

    
299
int usb_device_delete_addr(int busnr, int addr)
300
{
301
    USBBus *bus;
302
    USBPort *port;
303
    USBDevice *dev;
304

    
305
    bus = usb_bus_find(busnr);
306
    if (!bus)
307
        return -1;
308

    
309
    QTAILQ_FOREACH(port, &bus->used, next) {
310
        if (port->dev->addr == addr)
311
            break;
312
    }
313
    if (!port)
314
        return -1;
315
    dev = port->dev;
316

    
317
    qdev_free(&dev->qdev);
318
    return 0;
319
}
320

    
321
static const char *usb_speed(unsigned int speed)
322
{
323
    static const char *txt[] = {
324
        [ USB_SPEED_LOW  ] = "1.5",
325
        [ USB_SPEED_FULL ] = "12",
326
        [ USB_SPEED_HIGH ] = "480",
327
        [ USB_SPEED_SUPER ] = "5000",
328
    };
329
    if (speed >= ARRAY_SIZE(txt))
330
        return "?";
331
    return txt[speed];
332
}
333

    
334
static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
335
{
336
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
337
    USBBus *bus = usb_bus_from_device(dev);
338

    
339
    monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
340
                   indent, "", bus->busnr, dev->addr,
341
                   dev->port ? dev->port->path : "-",
342
                   usb_speed(dev->speed), dev->product_desc,
343
                   dev->attached ? ", attached" : "");
344
}
345

    
346
static char *usb_get_dev_path(DeviceState *qdev)
347
{
348
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
349
    return qemu_strdup(dev->port->path);
350
}
351

    
352
static char *usb_get_fw_dev_path(DeviceState *qdev)
353
{
354
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
355
    char *fw_path, *in;
356
    ssize_t pos = 0, fw_len;
357
    long nr;
358

    
359
    fw_len = 32 + strlen(dev->port->path) * 6;
360
    fw_path = qemu_malloc(fw_len);
361
    in = dev->port->path;
362
    while (fw_len - pos > 0) {
363
        nr = strtol(in, &in, 10);
364
        if (in[0] == '.') {
365
            /* some hub between root port and device */
366
            pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
367
            in++;
368
        } else {
369
            /* the device itself */
370
            pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
371
                            qdev_fw_name(qdev), nr);
372
            break;
373
        }
374
    }
375
    return fw_path;
376
}
377

    
378
void usb_info(Monitor *mon)
379
{
380
    USBBus *bus;
381
    USBDevice *dev;
382
    USBPort *port;
383

    
384
    if (QTAILQ_EMPTY(&busses)) {
385
        monitor_printf(mon, "USB support not enabled\n");
386
        return;
387
    }
388

    
389
    QTAILQ_FOREACH(bus, &busses, next) {
390
        QTAILQ_FOREACH(port, &bus->used, next) {
391
            dev = port->dev;
392
            if (!dev)
393
                continue;
394
            monitor_printf(mon, "  Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
395
                           bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
396
                           dev->product_desc);
397
        }
398
    }
399
}
400

    
401
/* handle legacy -usbdevice cmd line option */
402
USBDevice *usbdevice_create(const char *cmdline)
403
{
404
    USBBus *bus = usb_bus_find(-1 /* any */);
405
    DeviceInfo *info;
406
    USBDeviceInfo *usb;
407
    char driver[32];
408
    const char *params;
409
    int len;
410

    
411
    params = strchr(cmdline,':');
412
    if (params) {
413
        params++;
414
        len = params - cmdline;
415
        if (len > sizeof(driver))
416
            len = sizeof(driver);
417
        pstrcpy(driver, len, cmdline);
418
    } else {
419
        params = "";
420
        pstrcpy(driver, sizeof(driver), cmdline);
421
    }
422

    
423
    for (info = device_info_list; info != NULL; info = info->next) {
424
        if (info->bus_info != &usb_bus_info)
425
            continue;
426
        usb = DO_UPCAST(USBDeviceInfo, qdev, info);
427
        if (usb->usbdevice_name == NULL)
428
            continue;
429
        if (strcmp(usb->usbdevice_name, driver) != 0)
430
            continue;
431
        break;
432
    }
433
    if (info == NULL) {
434
#if 0
435
        /* no error because some drivers are not converted (yet) */
436
        error_report("usbdevice %s not found", driver);
437
#endif
438
        return NULL;
439
    }
440

    
441
    if (!usb->usbdevice_init) {
442
        if (*params) {
443
            error_report("usbdevice %s accepts no params", driver);
444
            return NULL;
445
        }
446
        return usb_create_simple(bus, usb->qdev.name);
447
    }
448
    return usb->usbdevice_init(params);
449
}