source: mainline/uspace/lib/libc/generic/devman.c@ d347b53

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since d347b53 was d347b53, checked in by Lenka Trochtova <trochtova.lenka@…>, 16 years ago

child device registration - parts of code

  • Property mode set to 100644
File size: 4.4 KB
Line 
1/*
2 * Copyright (c) 2007 Josef Cejka
3 * Copyright (c) 2009 Jiri Svoboda
4 * Copyright (c) 2010 Lenka Trochtova
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * - The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <string.h>
32#include <stdio.h>
33#include <ipc/ipc.h>
34#include <ipc/services.h>
35#include <ipc/devman.h>
36#include <devman.h>
37#include <async.h>
38#include <errno.h>
39#include <malloc.h>
40#include <bool.h>
41
42static int devman_phone_driver = -1;
43static int devman_phone_client = -1;
44
45int devman_get_phone(devman_interface_t iface, unsigned int flags)
46{
47 switch (iface) {
48 case DEVMAN_DRIVER:
49 if (devman_phone_driver >= 0)
50 return devman_phone_driver;
51
52 if (flags & IPC_FLAG_BLOCKING)
53 devman_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
54 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
55 else
56 devman_phone_driver = ipc_connect_me_to(PHONE_NS,
57 SERVICE_DEVMAN, DEVMAN_DRIVER, 0);
58
59 return devman_phone_driver;
60 case DEVMAN_CLIENT:
61 if (devman_phone_client >= 0)
62 return devman_phone_client;
63
64 if (flags & IPC_FLAG_BLOCKING)
65 devman_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
66 SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
67 else
68 devman_phone_client = ipc_connect_me_to(PHONE_NS,
69 SERVICE_DEVMAN, DEVMAN_CLIENT, 0);
70
71 return devman_phone_client;
72 default:
73 return -1;
74 }
75}
76
77/** Register running driver with device manager. */
78int devman_driver_register(const char *name, async_client_conn_t conn)
79{
80 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
81
82 if (phone < 0)
83 return phone;
84
85 async_serialize_start();
86
87 ipc_call_t answer;
88 aid_t req = async_send_2(phone, DEVMAN_DRIVER_REGISTER, 0, 0, &answer);
89
90 ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
91 if (retval != EOK) {
92 async_wait_for(req, NULL);
93 async_serialize_end();
94 return -1;
95 }
96
97 async_set_client_connection(conn);
98
99 ipcarg_t callback_phonehash;
100 ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
101 async_wait_for(req, &retval);
102
103 async_serialize_end();
104
105 return retval;
106}
107
108int devman_child_device_register(
109 const char *name, match_id_list_t *match_ids, device_handle_t parent_handle, device_handle_t *handle)
110{
111 int phone = devman_get_phone(DEVMAN_DRIVER, IPC_FLAG_BLOCKING);
112
113 if (phone < 0)
114 return phone;
115
116 async_serialize_start();
117
118 ipc_call_t answer;
119 aid_t req = async_send_1(phone, DEVMAN_ADD_CHILD_DEVICE, parent_handle, &answer);
120
121 ipcarg_t retval = async_data_write_start(phone, name, str_size(name));
122 if (retval != EOK) {
123 async_wait_for(req, NULL);
124 async_serialize_end();
125 return retval;
126 }
127
128 // TODO match ids
129
130 async_wait_for(req, &retval);
131
132 async_serialize_end();
133
134 if (retval != EOK) {
135 if (handle != NULL) {
136 *handle = -1;
137 }
138 return retval;
139 }
140
141 if (handle != NULL)
142 *handle = (int) IPC_GET_ARG1(answer);
143}
144
145void devman_hangup_phone(devman_interface_t iface)
146{
147 switch (iface) {
148 case DEVMAN_DRIVER:
149 if (devman_phone_driver >= 0) {
150 ipc_hangup(devman_phone_driver);
151 devman_phone_driver = -1;
152 }
153 break;
154 case DEVMAN_CLIENT:
155 if (devman_phone_client >= 0) {
156 ipc_hangup(devman_phone_client);
157 devman_phone_client = -1;
158 }
159 break;
160 default:
161 break;
162 }
163}
Note: See TracBrowser for help on using the repository browser.