[b371844] | 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 |
|
---|
[bd8c753d] | 29 | /** @addtogroup drvusbvhc
|
---|
[b371844] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * @brief Connection handling of calls from virtual device (implementation).
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <assert.h>
|
---|
| 37 | #include <errno.h>
|
---|
[b8100da] | 38 | #include <usbvirt/hub.h>
|
---|
[b371844] | 39 |
|
---|
| 40 | #include "conn.h"
|
---|
| 41 | #include "hc.h"
|
---|
[b8507a1] | 42 | #include "hub.h"
|
---|
[b371844] | 43 |
|
---|
[ca07cd3] | 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;
|
---|
[a9b6bec] | 49 | sysarg_t answer_rc;
|
---|
[ca07cd3] | 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 |
|
---|
[e27595b] | 76 | /** Default handler for IPC methods not handled by DDF.
|
---|
[355f7c2] | 77 | *
|
---|
[eb1a2f4] | 78 | * @param fun Device handling the call.
|
---|
[e27595b] | 79 | * @param icallid Call id.
|
---|
| 80 | * @param icall Call data.
|
---|
[b371844] | 81 | */
|
---|
[eb1a2f4] | 82 | void default_connection_handler(ddf_fun_t *fun,
|
---|
[e27595b] | 83 | ipc_callid_t icallid, ipc_call_t *icall)
|
---|
[b371844] | 84 | {
|
---|
[a9b6bec] | 85 | sysarg_t method = IPC_GET_IMETHOD(*icall);
|
---|
[e27595b] | 86 |
|
---|
| 87 | if (method == IPC_M_CONNECT_TO_ME) {
|
---|
| 88 | int callback = IPC_GET_ARG5(*icall);
|
---|
| 89 | virtdev_connection_t *dev
|
---|
[6967c14] | 90 | = virtdev_add_device(callback, (sysarg_t)fibril_get_id());
|
---|
[e27595b] | 91 | if (!dev) {
|
---|
[17aca1c] | 92 | async_answer_0(icallid, EEXISTS);
|
---|
| 93 | async_hangup(callback);
|
---|
[e27595b] | 94 | return;
|
---|
[b371844] | 95 | }
|
---|
[17aca1c] | 96 | async_answer_0(icallid, EOK);
|
---|
[e27595b] | 97 |
|
---|
| 98 | char devname[DEVICE_NAME_MAXLENGTH + 1];
|
---|
| 99 | int rc = get_device_name(callback, devname, DEVICE_NAME_MAXLENGTH);
|
---|
| 100 |
|
---|
[0b31409] | 101 | usb_log_info("New virtual device `%s' (id = %x).\n",
|
---|
[6967c14] | 102 | rc == EOK ? devname : "<unknown>", dev->id);
|
---|
[e27595b] | 103 |
|
---|
| 104 | return;
|
---|
[b371844] | 105 | }
|
---|
[e27595b] | 106 |
|
---|
[17aca1c] | 107 | async_answer_0(icallid, EINVAL);
|
---|
[b371844] | 108 | }
|
---|
| 109 |
|
---|
[6967c14] | 110 | /** Callback for DDF when client disconnects.
|
---|
| 111 | *
|
---|
[bc1c6fb] | 112 | * @param fun Device function the client was connected to.
|
---|
[6967c14] | 113 | */
|
---|
[eb1a2f4] | 114 | void on_client_close(ddf_fun_t *fun)
|
---|
[6967c14] | 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 |
|
---|
[0b31409] | 124 | usb_log_info("Virtual device disconnected (id = %x).\n", dev->id);
|
---|
[6967c14] | 125 | virtdev_destroy_device(dev);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[e27595b] | 128 |
|
---|
[b371844] | 129 | /**
|
---|
| 130 | * @}
|
---|
| 131 | */
|
---|