1 | #include <errno.h>
|
---|
2 | #include "vhcd.h"
|
---|
3 | #include "hub/virthub.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | static vhc_virtdev_t *vhc_virtdev_create()
|
---|
7 | {
|
---|
8 | vhc_virtdev_t *dev = malloc(sizeof(vhc_virtdev_t));
|
---|
9 | if (dev == NULL) {
|
---|
10 | return NULL;
|
---|
11 | }
|
---|
12 | dev->address = 0;
|
---|
13 | dev->dev_phone = -1;
|
---|
14 | dev->dev_local = NULL;
|
---|
15 | dev->plugged = true;
|
---|
16 | link_initialize(&dev->link);
|
---|
17 | fibril_mutex_initialize(&dev->guard);
|
---|
18 | list_initialize(&dev->transfer_queue);
|
---|
19 |
|
---|
20 | return dev;
|
---|
21 | }
|
---|
22 |
|
---|
23 | static int vhc_virtdev_plug_generic(vhc_data_t *vhc,
|
---|
24 | int phone, usbvirt_device_t *virtdev,
|
---|
25 | uintptr_t *handle, bool connect)
|
---|
26 | {
|
---|
27 | vhc_virtdev_t *dev = vhc_virtdev_create();
|
---|
28 | if (dev == NULL) {
|
---|
29 | return ENOMEM;
|
---|
30 | }
|
---|
31 |
|
---|
32 | dev->dev_phone = phone;
|
---|
33 | dev->dev_local = virtdev;
|
---|
34 |
|
---|
35 | fibril_mutex_lock(&vhc->guard);
|
---|
36 | list_append(&dev->link, &vhc->devices);
|
---|
37 | fibril_mutex_unlock(&vhc->guard);
|
---|
38 |
|
---|
39 | fid_t fibril = fibril_create(vhc_transfer_queue_processor, dev);
|
---|
40 | if (fibril == 0) {
|
---|
41 | free(dev);
|
---|
42 | return ENOMEM;
|
---|
43 | }
|
---|
44 | fibril_add_ready(fibril);
|
---|
45 |
|
---|
46 | if (handle != NULL) {
|
---|
47 | *handle = (uintptr_t) dev;
|
---|
48 | }
|
---|
49 |
|
---|
50 | if (connect) {
|
---|
51 | // FIXME: check status
|
---|
52 | (void) virthub_connect_device(vhc->hub, dev);
|
---|
53 | }
|
---|
54 |
|
---|
55 | return EOK;
|
---|
56 | }
|
---|
57 |
|
---|
58 | int vhc_virtdev_plug(vhc_data_t *vhc, int phone, uintptr_t *handle)
|
---|
59 | {
|
---|
60 | return vhc_virtdev_plug_generic(vhc, phone, NULL, handle, true);
|
---|
61 | }
|
---|
62 |
|
---|
63 | int vhc_virtdev_plug_local(vhc_data_t *vhc, usbvirt_device_t *dev, uintptr_t *handle)
|
---|
64 | {
|
---|
65 | return vhc_virtdev_plug_generic(vhc, -1, dev, handle, true);
|
---|
66 | }
|
---|
67 |
|
---|
68 | int vhc_virtdev_plug_hub(vhc_data_t *vhc, usbvirt_device_t *dev, uintptr_t *handle)
|
---|
69 | {
|
---|
70 | return vhc_virtdev_plug_generic(vhc, -1, dev, handle, false);
|
---|
71 | }
|
---|
72 |
|
---|
73 | void vhc_virtdev_unplug(vhc_data_t *vhc, uintptr_t handle)
|
---|
74 | {
|
---|
75 | vhc_virtdev_t *dev = (vhc_virtdev_t *) handle;
|
---|
76 |
|
---|
77 | // FIXME: check status
|
---|
78 | (void) virthub_disconnect_device(vhc->hub, dev);
|
---|
79 |
|
---|
80 | fibril_mutex_lock(&vhc->guard);
|
---|
81 | fibril_mutex_lock(&dev->guard);
|
---|
82 | dev->plugged = false;
|
---|
83 | list_remove(&dev->link);
|
---|
84 | fibril_mutex_unlock(&dev->guard);
|
---|
85 | fibril_mutex_unlock(&vhc->guard);
|
---|
86 | }
|
---|