1 | /*
|
---|
2 | * Copyright (c) 2010 Vojtech Horky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup usb
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief Virtual device management (implementation).
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <ipc/ipc.h>
|
---|
37 | #include <adt/list.h>
|
---|
38 | #include <bool.h>
|
---|
39 | #include <async.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <stdio.h>
|
---|
42 | #include <errno.h>
|
---|
43 | #include <str_error.h>
|
---|
44 |
|
---|
45 | #include <usbvirt/ids.h>
|
---|
46 | #include <usbvirt/hub.h>
|
---|
47 |
|
---|
48 | #include "devices.h"
|
---|
49 |
|
---|
50 | #define list_foreach(pos, head) \
|
---|
51 | for (pos = (head)->next; pos != (head); \
|
---|
52 | pos = pos->next)
|
---|
53 |
|
---|
54 | LIST_INITIALIZE(devices);
|
---|
55 |
|
---|
56 | /** Recognise device by id.
|
---|
57 | *
|
---|
58 | * @param id Device id.
|
---|
59 | * @param phone Callback phone.
|
---|
60 | */
|
---|
61 | virtdev_connection_t *virtdev_recognise(int id, int phone)
|
---|
62 | {
|
---|
63 | virtdev_connection_t * dev = NULL;
|
---|
64 | switch (id) {
|
---|
65 | case USBVIRT_DEV_KEYBOARD_ID:
|
---|
66 | dev = virtdev_add_device(phone);
|
---|
67 | break;
|
---|
68 | default:
|
---|
69 | break;
|
---|
70 | }
|
---|
71 |
|
---|
72 | /*
|
---|
73 | * We do not want to mess-up the virtdev_add_device() as
|
---|
74 | * the id is needed only before device probing/detection
|
---|
75 | * is implemented.
|
---|
76 | *
|
---|
77 | * However, that does not mean that this will happen soon.
|
---|
78 | */
|
---|
79 | if (dev) {
|
---|
80 | dev->id = id;
|
---|
81 | }
|
---|
82 |
|
---|
83 | return dev;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /** Create virtual device.
|
---|
87 | *
|
---|
88 | * @param address USB address.
|
---|
89 | * @param phone Callback phone.
|
---|
90 | * @return New device.
|
---|
91 | * @retval NULL Out of memory or address already occupied.
|
---|
92 | */
|
---|
93 | virtdev_connection_t *virtdev_add_device(int phone)
|
---|
94 | {
|
---|
95 | virtdev_connection_t *dev = (virtdev_connection_t *)
|
---|
96 | malloc(sizeof(virtdev_connection_t));
|
---|
97 | dev->phone = phone;
|
---|
98 | list_append(&dev->link, &devices);
|
---|
99 |
|
---|
100 | return dev;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /** Destroy virtual device.
|
---|
104 | */
|
---|
105 | void virtdev_destroy_device(virtdev_connection_t *dev)
|
---|
106 | {
|
---|
107 | list_remove(&dev->link);
|
---|
108 | free(dev);
|
---|
109 | }
|
---|
110 |
|
---|
111 | /** Send data to all connected devices.
|
---|
112 | *
|
---|
113 | * @param transaction Transaction to be sent over the bus.
|
---|
114 | */
|
---|
115 | usb_transaction_outcome_t virtdev_send_to_all(transaction_t *transaction)
|
---|
116 | {
|
---|
117 | link_t *pos;
|
---|
118 | list_foreach(pos, &devices) {
|
---|
119 | virtdev_connection_t *dev
|
---|
120 | = list_get_instance(pos, virtdev_connection_t, link);
|
---|
121 |
|
---|
122 | ipc_call_t answer_data;
|
---|
123 | ipcarg_t answer_rc;
|
---|
124 | aid_t req;
|
---|
125 | int rc;
|
---|
126 |
|
---|
127 | req = async_send_3(dev->phone,
|
---|
128 | IPC_M_USBVIRT_DATA_TO_DEVICE,
|
---|
129 | transaction->target.address,
|
---|
130 | transaction->target.endpoint,
|
---|
131 | transaction->type,
|
---|
132 | &answer_data);
|
---|
133 |
|
---|
134 | rc = async_data_write_start(dev->phone,
|
---|
135 | transaction->buffer, transaction->len);
|
---|
136 | if (rc != EOK) {
|
---|
137 | async_wait_for(req, NULL);
|
---|
138 | } else {
|
---|
139 | async_wait_for(req, &answer_rc);
|
---|
140 | rc = (int)answer_rc;
|
---|
141 | }
|
---|
142 | }
|
---|
143 |
|
---|
144 | /*
|
---|
145 | * TODO: maybe screw some transactions to get more
|
---|
146 | * real-life image.
|
---|
147 | */
|
---|
148 | return USB_OUTCOME_OK;
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * @}
|
---|
153 | */
|
---|