Rev 4416 | Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
4401 | svoboda | 1 | /* |
2 | * Copyright (c) 2007 Josef Cejka |
||
3 | * Copyright (c) 2009 Jiri Svoboda |
||
4 | * All rights reserved. |
||
5 | * |
||
6 | * Redistribution and use in source and binary forms, with or without |
||
7 | * modification, are permitted provided that the following conditions |
||
8 | * are met: |
||
9 | * |
||
10 | * - Redistributions of source code must retain the above copyright |
||
11 | * notice, this list of conditions and the following disclaimer. |
||
12 | * - Redistributions in binary form must reproduce the above copyright |
||
13 | * notice, this list of conditions and the following disclaimer in the |
||
14 | * documentation and/or other materials provided with the distribution. |
||
15 | * - The name of the author may not be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <string.h> |
||
31 | #include <ipc/ipc.h> |
||
32 | #include <ipc/services.h> |
||
33 | #include <ipc/devmap.h> |
||
34 | #include <devmap.h> |
||
35 | #include <async.h> |
||
36 | #include <errno.h> |
||
37 | |||
38 | static int devmap_phone = -1; |
||
39 | |||
40 | /** Get phone to device mapper task. */ |
||
41 | static int devmap_get_phone(unsigned int flags) |
||
42 | { |
||
43 | int phone; |
||
44 | |||
45 | if (devmap_phone >= 0) |
||
46 | return devmap_phone; |
||
47 | |||
48 | if (flags & IPC_FLAG_BLOCKING) { |
||
49 | phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, |
||
50 | DEVMAP_CLIENT, 0); |
||
51 | } else { |
||
52 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
||
53 | DEVMAP_CLIENT, 0); |
||
54 | } |
||
55 | |||
56 | if (phone < 0) |
||
57 | return phone; |
||
58 | |||
59 | devmap_phone = phone; |
||
60 | return phone; |
||
61 | } |
||
62 | |||
63 | /** Register new driver with devmap. */ |
||
64 | int devmap_driver_register(const char *name, async_client_conn_t conn) |
||
65 | { |
||
66 | ipcarg_t retval; |
||
67 | aid_t req; |
||
68 | ipc_call_t answer; |
||
69 | int phone; |
||
70 | ipcarg_t callback_phonehash; |
||
71 | |||
72 | phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, |
||
73 | DEVMAP_DRIVER, 0); |
||
74 | |||
75 | if (phone < 0) { |
||
76 | return phone; |
||
77 | } |
||
78 | |||
79 | req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer); |
||
80 | retval = ipc_data_write_start(phone, name, str_size(name) + 1); |
||
81 | |||
82 | if (retval != EOK) { |
||
83 | async_wait_for(req, NULL); |
||
84 | ipc_hangup(phone); |
||
85 | return -1; |
||
86 | } |
||
87 | |||
88 | async_set_client_connection(conn); |
||
89 | |||
90 | ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash); |
||
91 | async_wait_for(req, &retval); |
||
92 | |||
93 | return phone; |
||
94 | } |
||
95 | |||
96 | int devmap_device_get_handle(const char *name, dev_handle_t *handle, |
||
97 | unsigned int flags) |
||
98 | { |
||
99 | ipcarg_t retval; |
||
100 | aid_t req; |
||
101 | ipc_call_t answer; |
||
102 | int phone; |
||
103 | |||
104 | phone = devmap_get_phone(flags); |
||
105 | if (phone < 0) |
||
106 | return phone; |
||
107 | |||
108 | req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0, |
||
109 | &answer); |
||
110 | |||
111 | retval = ipc_data_write_start(phone, name, str_size(name) + 1); |
||
112 | |||
113 | if (retval != EOK) { |
||
114 | async_wait_for(req, NULL); |
||
115 | return retval; |
||
116 | } |
||
117 | |||
118 | async_wait_for(req, &retval); |
||
119 | |||
120 | if (retval != EOK) { |
||
121 | if (handle != NULL) |
||
122 | *handle = -1; |
||
123 | return retval; |
||
124 | } |
||
125 | |||
126 | if (handle != NULL) |
||
127 | *handle = (int) IPC_GET_ARG1(answer); |
||
128 | |||
129 | return retval; |
||
130 | } |
||
131 | |||
132 | int devmap_device_connect(dev_handle_t handle, unsigned int flags) |
||
133 | { |
||
134 | int phone; |
||
135 | |||
136 | if (flags & IPC_FLAG_BLOCKING) { |
||
137 | phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP, |
||
138 | DEVMAP_CONNECT_TO_DEVICE, handle); |
||
139 | } else { |
||
140 | phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP, |
||
141 | DEVMAP_CONNECT_TO_DEVICE, handle); |
||
142 | } |
||
143 | |||
144 | return phone; |
||
145 | } |
||
146 | |||
147 | /** Register new device. |
||
148 | * |
||
149 | * @param driver_phone |
||
150 | * @param name Device name. |
||
151 | * @param handle Output: Handle to the created instance of device. |
||
152 | */ |
||
153 | int devmap_device_register(int driver_phone, const char *name, int *handle) |
||
154 | { |
||
155 | ipcarg_t retval; |
||
156 | aid_t req; |
||
157 | ipc_call_t answer; |
||
158 | |||
159 | req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0, |
||
160 | &answer); |
||
161 | |||
162 | retval = ipc_data_write_start(driver_phone, name, str_size(name) + 1); |
||
163 | |||
164 | if (retval != EOK) { |
||
165 | async_wait_for(req, NULL); |
||
166 | return retval; |
||
167 | } |
||
168 | |||
169 | async_wait_for(req, &retval); |
||
170 | |||
171 | if (retval != EOK) { |
||
172 | if (handle != NULL) |
||
173 | *handle = -1; |
||
174 | return retval; |
||
175 | } |
||
176 | |||
177 | *handle = (int) IPC_GET_ARG1(answer); |
||
178 | return retval; |
||
179 | } |