Statistics
| Branch: | Revision:

root / hw / usb-bus.c @ 2afee49f

History | View | Annotate | Download (12.2 kB)

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

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

    
10
static char *usb_get_dev_path(DeviceState *dev);
11
static char *usb_get_fw_dev_path(DeviceState *qdev);
12
static int usb_qdev_exit(DeviceState *qdev);
13

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

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

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

    
55
USBBus *usb_bus_find(int busnr)
56
{
57
    USBBus *bus;
58

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

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

    
74
    pstrcpy(dev->product_desc, sizeof(dev->product_desc), info->product_desc);
75
    dev->info = info;
76
    dev->auto_attach = 1;
77
    QLIST_INIT(&dev->strings);
78
    rc = usb_claim_port(dev);
79
    if (rc != 0) {
80
        return rc;
81
    }
82
    rc = dev->info->init(dev);
83
    if (rc != 0) {
84
        usb_release_port(dev);
85
        return rc;
86
    }
87
    if (dev->auto_attach) {
88
        rc = usb_device_attach(dev);
89
        if (rc != 0) {
90
            usb_qdev_exit(qdev);
91
            return rc;
92
        }
93
    }
94
    return 0;
95
}
96

    
97
static int usb_qdev_exit(DeviceState *qdev)
98
{
99
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
100

    
101
    if (dev->attached) {
102
        usb_device_detach(dev);
103
    }
104
    if (dev->info->handle_destroy) {
105
        dev->info->handle_destroy(dev);
106
    }
107
    if (dev->port) {
108
        usb_release_port(dev);
109
    }
110
    return 0;
111
}
112

    
113
void usb_qdev_register(USBDeviceInfo *info)
114
{
115
    info->qdev.bus_info = &usb_bus_info;
116
    info->qdev.init     = usb_qdev_init;
117
    info->qdev.unplug   = qdev_simple_unplug_cb;
118
    info->qdev.exit     = usb_qdev_exit;
119
    qdev_register(&info->qdev);
120
}
121

    
122
void usb_qdev_register_many(USBDeviceInfo *info)
123
{
124
    while (info->qdev.name) {
125
        usb_qdev_register(info);
126
        info++;
127
    }
128
}
129

    
130
USBDevice *usb_create(USBBus *bus, const char *name)
131
{
132
    DeviceState *dev;
133

    
134
#if 1
135
    /* temporary stopgap until all usb is properly qdev-ified */
136
    if (!bus) {
137
        bus = usb_bus_find(-1);
138
        if (!bus)
139
            return NULL;
140
        error_report("%s: no bus specified, using \"%s\" for \"%s\"\n",
141
                __FUNCTION__, bus->qbus.name, name);
142
    }
143
#endif
144

    
145
    dev = qdev_create(&bus->qbus, name);
146
    return DO_UPCAST(USBDevice, qdev, dev);
147
}
148

    
149
USBDevice *usb_create_simple(USBBus *bus, const char *name)
150
{
151
    USBDevice *dev = usb_create(bus, name);
152
    int rc;
153

    
154
    if (!dev) {
155
        error_report("Failed to create USB device '%s'\n", name);
156
        return NULL;
157
    }
158
    rc = qdev_init(&dev->qdev);
159
    if (rc < 0) {
160
        error_report("Failed to initialize USB device '%s'\n", name);
161
        return NULL;
162
    }
163
    return dev;
164
}
165

    
166
static void usb_fill_port(USBPort *port, void *opaque, int index,
167
                          USBPortOps *ops, int speedmask)
168
{
169
    port->opaque = opaque;
170
    port->index = index;
171
    port->ops = ops;
172
    port->speedmask = speedmask;
173
    usb_port_location(port, NULL, index + 1);
174
}
175

    
176
void usb_register_port(USBBus *bus, USBPort *port, void *opaque, int index,
177
                       USBPortOps *ops, int speedmask)
178
{
179
    usb_fill_port(port, opaque, index, ops, speedmask);
180
    QTAILQ_INSERT_TAIL(&bus->free, port, next);
181
    bus->nfree++;
182
}
183

    
184
int usb_register_companion(const char *masterbus, USBPort *ports[],
185
                           uint32_t portcount, uint32_t firstport,
186
                           void *opaque, USBPortOps *ops, int speedmask)
187
{
188
    USBBus *bus;
189
    int i;
190

    
191
    QTAILQ_FOREACH(bus, &busses, next) {
192
        if (strcmp(bus->qbus.name, masterbus) == 0) {
193
            break;
194
        }
195
    }
196

    
197
    if (!bus || !bus->ops->register_companion) {
198
        qerror_report(QERR_INVALID_PARAMETER_VALUE, "masterbus",
199
                      "an USB masterbus");
200
        if (bus) {
201
            error_printf_unless_qmp(
202
                "USB bus '%s' does not allow companion controllers\n",
203
                masterbus);
204
        }
205
        return -1;
206
    }
207

    
208
    for (i = 0; i < portcount; i++) {
209
        usb_fill_port(ports[i], opaque, i, ops, speedmask);
210
    }
211

    
212
    return bus->ops->register_companion(bus, ports, portcount, firstport);
213
}
214

    
215
void usb_port_location(USBPort *downstream, USBPort *upstream, int portnr)
216
{
217
    if (upstream) {
218
        snprintf(downstream->path, sizeof(downstream->path), "%s.%d",
219
                 upstream->path, portnr);
220
    } else {
221
        snprintf(downstream->path, sizeof(downstream->path), "%d", portnr);
222
    }
223
}
224

    
225
void usb_unregister_port(USBBus *bus, USBPort *port)
226
{
227
    if (port->dev)
228
        qdev_free(&port->dev->qdev);
229
    QTAILQ_REMOVE(&bus->free, port, next);
230
    bus->nfree--;
231
}
232

    
233
int usb_claim_port(USBDevice *dev)
234
{
235
    USBBus *bus = usb_bus_from_device(dev);
236
    USBPort *port;
237

    
238
    assert(dev->port == NULL);
239

    
240
    if (dev->port_path) {
241
        QTAILQ_FOREACH(port, &bus->free, next) {
242
            if (strcmp(port->path, dev->port_path) == 0) {
243
                break;
244
            }
245
        }
246
        if (port == NULL) {
247
            error_report("Error: usb port %s (bus %s) not found (in use?)\n",
248
                         dev->port_path, bus->qbus.name);
249
            return -1;
250
        }
251
    } else {
252
        if (bus->nfree == 1 && strcmp(dev->qdev.info->name, "usb-hub") != 0) {
253
            /* Create a new hub and chain it on */
254
            usb_create_simple(bus, "usb-hub");
255
        }
256
        if (bus->nfree == 0) {
257
            error_report("Error: tried to attach usb device %s to a bus "
258
                         "with no free ports\n", dev->product_desc);
259
            return -1;
260
        }
261
        port = QTAILQ_FIRST(&bus->free);
262
    }
263
    trace_usb_port_claim(bus->busnr, port->path);
264

    
265
    QTAILQ_REMOVE(&bus->free, port, next);
266
    bus->nfree--;
267

    
268
    dev->port = port;
269
    port->dev = dev;
270

    
271
    QTAILQ_INSERT_TAIL(&bus->used, port, next);
272
    bus->nused++;
273
    return 0;
274
}
275

    
276
void usb_release_port(USBDevice *dev)
277
{
278
    USBBus *bus = usb_bus_from_device(dev);
279
    USBPort *port = dev->port;
280

    
281
    assert(port != NULL);
282
    trace_usb_port_release(bus->busnr, port->path);
283

    
284
    QTAILQ_REMOVE(&bus->used, port, next);
285
    bus->nused--;
286

    
287
    dev->port = NULL;
288
    port->dev = NULL;
289

    
290
    QTAILQ_INSERT_TAIL(&bus->free, port, next);
291
    bus->nfree++;
292
}
293

    
294
int usb_device_attach(USBDevice *dev)
295
{
296
    USBBus *bus = usb_bus_from_device(dev);
297
    USBPort *port = dev->port;
298

    
299
    assert(port != NULL);
300
    assert(!dev->attached);
301
    trace_usb_port_attach(bus->busnr, port->path);
302

    
303
    if (!(port->speedmask & dev->speedmask)) {
304
        error_report("Warning: speed mismatch trying to attach "
305
                     "usb device %s to bus %s\n",
306
                     dev->product_desc, bus->qbus.name);
307
        return -1;
308
    }
309

    
310
    dev->attached++;
311
    usb_attach(port);
312

    
313
    return 0;
314
}
315

    
316
int usb_device_detach(USBDevice *dev)
317
{
318
    USBBus *bus = usb_bus_from_device(dev);
319
    USBPort *port = dev->port;
320

    
321
    assert(port != NULL);
322
    assert(dev->attached);
323
    trace_usb_port_detach(bus->busnr, port->path);
324

    
325
    usb_detach(port);
326
    dev->attached--;
327
    return 0;
328
}
329

    
330
int usb_device_delete_addr(int busnr, int addr)
331
{
332
    USBBus *bus;
333
    USBPort *port;
334
    USBDevice *dev;
335

    
336
    bus = usb_bus_find(busnr);
337
    if (!bus)
338
        return -1;
339

    
340
    QTAILQ_FOREACH(port, &bus->used, next) {
341
        if (port->dev->addr == addr)
342
            break;
343
    }
344
    if (!port)
345
        return -1;
346
    dev = port->dev;
347

    
348
    qdev_free(&dev->qdev);
349
    return 0;
350
}
351

    
352
static const char *usb_speed(unsigned int speed)
353
{
354
    static const char *txt[] = {
355
        [ USB_SPEED_LOW  ] = "1.5",
356
        [ USB_SPEED_FULL ] = "12",
357
        [ USB_SPEED_HIGH ] = "480",
358
        [ USB_SPEED_SUPER ] = "5000",
359
    };
360
    if (speed >= ARRAY_SIZE(txt))
361
        return "?";
362
    return txt[speed];
363
}
364

    
365
static void usb_bus_dev_print(Monitor *mon, DeviceState *qdev, int indent)
366
{
367
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
368
    USBBus *bus = usb_bus_from_device(dev);
369

    
370
    monitor_printf(mon, "%*saddr %d.%d, port %s, speed %s, name %s%s\n",
371
                   indent, "", bus->busnr, dev->addr,
372
                   dev->port ? dev->port->path : "-",
373
                   usb_speed(dev->speed), dev->product_desc,
374
                   dev->attached ? ", attached" : "");
375
}
376

    
377
static char *usb_get_dev_path(DeviceState *qdev)
378
{
379
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
380
    return g_strdup(dev->port->path);
381
}
382

    
383
static char *usb_get_fw_dev_path(DeviceState *qdev)
384
{
385
    USBDevice *dev = DO_UPCAST(USBDevice, qdev, qdev);
386
    char *fw_path, *in;
387
    ssize_t pos = 0, fw_len;
388
    long nr;
389

    
390
    fw_len = 32 + strlen(dev->port->path) * 6;
391
    fw_path = g_malloc(fw_len);
392
    in = dev->port->path;
393
    while (fw_len - pos > 0) {
394
        nr = strtol(in, &in, 10);
395
        if (in[0] == '.') {
396
            /* some hub between root port and device */
397
            pos += snprintf(fw_path + pos, fw_len - pos, "hub@%ld/", nr);
398
            in++;
399
        } else {
400
            /* the device itself */
401
            pos += snprintf(fw_path + pos, fw_len - pos, "%s@%ld",
402
                            qdev_fw_name(qdev), nr);
403
            break;
404
        }
405
    }
406
    return fw_path;
407
}
408

    
409
void usb_info(Monitor *mon)
410
{
411
    USBBus *bus;
412
    USBDevice *dev;
413
    USBPort *port;
414

    
415
    if (QTAILQ_EMPTY(&busses)) {
416
        monitor_printf(mon, "USB support not enabled\n");
417
        return;
418
    }
419

    
420
    QTAILQ_FOREACH(bus, &busses, next) {
421
        QTAILQ_FOREACH(port, &bus->used, next) {
422
            dev = port->dev;
423
            if (!dev)
424
                continue;
425
            monitor_printf(mon, "  Device %d.%d, Port %s, Speed %s Mb/s, Product %s\n",
426
                           bus->busnr, dev->addr, port->path, usb_speed(dev->speed),
427
                           dev->product_desc);
428
        }
429
    }
430
}
431

    
432
/* handle legacy -usbdevice cmd line option */
433
USBDevice *usbdevice_create(const char *cmdline)
434
{
435
    USBBus *bus = usb_bus_find(-1 /* any */);
436
    DeviceInfo *info;
437
    USBDeviceInfo *usb;
438
    char driver[32];
439
    const char *params;
440
    int len;
441

    
442
    params = strchr(cmdline,':');
443
    if (params) {
444
        params++;
445
        len = params - cmdline;
446
        if (len > sizeof(driver))
447
            len = sizeof(driver);
448
        pstrcpy(driver, len, cmdline);
449
    } else {
450
        params = "";
451
        pstrcpy(driver, sizeof(driver), cmdline);
452
    }
453

    
454
    for (info = device_info_list; info != NULL; info = info->next) {
455
        if (info->bus_info != &usb_bus_info)
456
            continue;
457
        usb = DO_UPCAST(USBDeviceInfo, qdev, info);
458
        if (usb->usbdevice_name == NULL)
459
            continue;
460
        if (strcmp(usb->usbdevice_name, driver) != 0)
461
            continue;
462
        break;
463
    }
464
    if (info == NULL) {
465
#if 0
466
        /* no error because some drivers are not converted (yet) */
467
        error_report("usbdevice %s not found", driver);
468
#endif
469
        return NULL;
470
    }
471

    
472
    if (!usb->usbdevice_init) {
473
        if (*params) {
474
            error_report("usbdevice %s accepts no params", driver);
475
            return NULL;
476
        }
477
        return usb_create_simple(bus, usb->qdev.name);
478
    }
479
    return usb->usbdevice_init(params);
480
}