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 drvusbvhc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief Connection handling of calls from virtual device (implementation).
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <errno.h>
|
---|
38 | #include <usbvirt/hub.h>
|
---|
39 |
|
---|
40 | #include "conn.h"
|
---|
41 | #include "hc.h"
|
---|
42 | #include "hub.h"
|
---|
43 |
|
---|
44 | #define DEVICE_NAME_MAXLENGTH 32
|
---|
45 |
|
---|
46 | static int get_device_name(int phone, char *buffer, size_t len)
|
---|
47 | {
|
---|
48 | ipc_call_t answer_data;
|
---|
49 | sysarg_t answer_rc;
|
---|
50 | aid_t req;
|
---|
51 | int rc;
|
---|
52 |
|
---|
53 | req = async_send_0(phone,
|
---|
54 | IPC_M_USBVIRT_GET_NAME,
|
---|
55 | &answer_data);
|
---|
56 |
|
---|
57 | rc = async_data_read_start(phone, buffer, len);
|
---|
58 | if (rc != EOK) {
|
---|
59 | async_wait_for(req, NULL);
|
---|
60 | return EINVAL;
|
---|
61 | }
|
---|
62 |
|
---|
63 | async_wait_for(req, &answer_rc);
|
---|
64 | rc = (int)answer_rc;
|
---|
65 |
|
---|
66 | if (IPC_GET_ARG1(answer_data) < len) {
|
---|
67 | len = IPC_GET_ARG1(answer_data);
|
---|
68 | } else {
|
---|
69 | len--;
|
---|
70 | }
|
---|
71 | buffer[len] = 0;
|
---|
72 |
|
---|
73 | return rc;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /** Default handler for IPC methods not handled by DDF.
|
---|
77 | *
|
---|
78 | * @param fun Device handling the call.
|
---|
79 | * @param icallid Call id.
|
---|
80 | * @param icall Call data.
|
---|
81 | */
|
---|
82 | void default_connection_handler(ddf_fun_t *fun,
|
---|
83 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
84 | {
|
---|
85 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
86 |
|
---|
87 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
88 | int callback = IPC_GET_ARG5(*icall);
|
---|
89 | virtdev_connection_t *dev
|
---|
90 | = virtdev_add_device(callback, (sysarg_t)fibril_get_id());
|
---|
91 | if (!dev) {
|
---|
92 | async_answer_0(icallid, EEXISTS);
|
---|
93 | async_hangup(callback);
|
---|
94 | return;
|
---|
95 | }
|
---|
96 | async_answer_0(icallid, EOK);
|
---|
97 |
|
---|
98 | char devname[DEVICE_NAME_MAXLENGTH + 1];
|
---|
99 | int rc = get_device_name(callback, devname, DEVICE_NAME_MAXLENGTH);
|
---|
100 |
|
---|
101 | usb_log_info("New virtual device `%s' (id = %x).\n",
|
---|
102 | rc == EOK ? devname : "<unknown>", dev->id);
|
---|
103 |
|
---|
104 | return;
|
---|
105 | }
|
---|
106 |
|
---|
107 | async_answer_0(icallid, EINVAL);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /** Callback for DDF when client disconnects.
|
---|
111 | *
|
---|
112 | * @param d Device the client was connected to.
|
---|
113 | */
|
---|
114 | void on_client_close(ddf_fun_t *fun)
|
---|
115 | {
|
---|
116 | /*
|
---|
117 | * Maybe a virtual device is being unplugged.
|
---|
118 | */
|
---|
119 | virtdev_connection_t *dev = virtdev_find((sysarg_t)fibril_get_id());
|
---|
120 | if (dev == NULL) {
|
---|
121 | return;
|
---|
122 | }
|
---|
123 |
|
---|
124 | usb_log_info("Virtual device disconnected (id = %x).\n", dev->id);
|
---|
125 | virtdev_destroy_device(dev);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * @}
|
---|
131 | */
|
---|