Subversion Repositories HelenOS

Rev

Rev 4401 | Rev 4474 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4401 Rev 4416
Line 33... Line 33...
33
#include <ipc/devmap.h>
33
#include <ipc/devmap.h>
34
#include <devmap.h>
34
#include <devmap.h>
35
#include <async.h>
35
#include <async.h>
36
#include <errno.h>
36
#include <errno.h>
37
 
37
 
-
 
38
static int devmap_phone_driver = -1;
38
static int devmap_phone = -1;
39
static int devmap_phone_client = -1;
39
 
40
 
40
/** Get phone to device mapper task. */
41
/** Get phone to device mapper task. */
41
static int devmap_get_phone(unsigned int flags)
42
int devmap_get_phone(devmap_interface_t iface, unsigned int flags)
42
{
43
{
43
    int phone;
44
    switch (iface) {
-
 
45
    case DEVMAP_DRIVER:
-
 
46
        if (devmap_phone_driver >= 0)
-
 
47
            return devmap_phone_driver;
44
 
48
       
-
 
49
        if (flags & IPC_FLAG_BLOCKING)
-
 
50
            devmap_phone_driver = ipc_connect_me_to_blocking(PHONE_NS,
-
 
51
                SERVICE_DEVMAP, DEVMAP_DRIVER, 0);
-
 
52
        else
-
 
53
            devmap_phone_driver = ipc_connect_me_to(PHONE_NS,
-
 
54
                SERVICE_DEVMAP, DEVMAP_DRIVER, 0);
-
 
55
       
-
 
56
        return devmap_phone_driver;
-
 
57
    case DEVMAP_CLIENT:
45
    if (devmap_phone >= 0)
58
        if (devmap_phone_client >= 0)
46
        return devmap_phone;
59
            return devmap_phone_client;
47
 
60
       
48
    if (flags & IPC_FLAG_BLOCKING) {
61
        if (flags & IPC_FLAG_BLOCKING)
49
        phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP,
62
            devmap_phone_client = ipc_connect_me_to_blocking(PHONE_NS,
50
            DEVMAP_CLIENT, 0);
63
                SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
51
    } else {
64
        else
52
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
65
            devmap_phone_client = ipc_connect_me_to(PHONE_NS,
53
            DEVMAP_CLIENT, 0);
66
                SERVICE_DEVMAP, DEVMAP_CLIENT, 0);
-
 
67
       
-
 
68
        return devmap_phone_client;
-
 
69
    default:
-
 
70
        return -1;
54
    }
71
    }
-
 
72
}
55
 
73
 
-
 
74
void devmap_hangup_phone(devmap_interface_t iface)
-
 
75
{
56
    if (phone < 0)
76
    switch (iface) {
-
 
77
    case DEVMAP_DRIVER:
-
 
78
        if (devmap_phone_driver >= 0) {
-
 
79
            ipc_hangup(devmap_phone_driver);
57
        return phone;
80
            devmap_phone_driver = -1;
58
 
81
        }
-
 
82
        break;
-
 
83
    case DEVMAP_CLIENT:
-
 
84
        if (devmap_phone_client >= 0) {
-
 
85
            ipc_hangup(devmap_phone_client);
59
    devmap_phone = phone;
86
            devmap_phone_client = -1;
-
 
87
        }
-
 
88
        break;
-
 
89
    default:
60
    return phone;
90
        break;
-
 
91
    }
61
}
92
}
62
 
93
 
63
/** Register new driver with devmap. */
94
/** Register new driver with devmap. */
64
int devmap_driver_register(const char *name, async_client_conn_t conn)
95
int devmap_driver_register(const char *name, async_client_conn_t conn)
65
{
96
{
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,
97
    int phone = devmap_get_phone(DEVMAP_DRIVER, IPC_FLAG_BLOCKING);
73
        DEVMAP_DRIVER, 0);
-
 
74
 
98
   
75
    if (phone < 0) {
99
    if (phone < 0)
76
        return phone;
100
        return phone;
77
    }
-
 
78
   
101
   
-
 
102
    ipc_call_t answer;
79
    req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
103
    aid_t req = async_send_2(phone, DEVMAP_DRIVER_REGISTER, 0, 0, &answer);
-
 
104
   
80
    retval = ipc_data_write_start(phone, name, str_size(name) + 1);
105
    ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1);
81
 
106
   
82
    if (retval != EOK) {
107
    if (retval != EOK) {
83
        async_wait_for(req, NULL);
108
        async_wait_for(req, NULL);
84
        ipc_hangup(phone);
-
 
85
        return -1;
109
        return -1;
86
    }
110
    }
87
 
111
   
88
    async_set_client_connection(conn);
112
    async_set_client_connection(conn);
89
 
113
   
-
 
114
    ipcarg_t callback_phonehash;
90
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
115
    ipc_connect_to_me(phone, 0, 0, 0, &callback_phonehash);
91
    async_wait_for(req, &retval);
116
    async_wait_for(req, &retval);
92
 
117
   
93
    return phone;
118
    return retval;
94
}
119
}
95
 
120
 
-
 
121
/** Register new device.
-
 
122
 *
-
 
123
 * @param name   Device name.
-
 
124
 * @param handle Output: Handle to the created instance of device.
-
 
125
 *
-
 
126
 */
96
int devmap_device_get_handle(const char *name, dev_handle_t *handle,
127
int devmap_device_register(const char *name, dev_handle_t *handle)
97
    unsigned int flags)
-
 
98
{
128
{
-
 
129
    int phone = devmap_get_phone(DEVMAP_DRIVER, IPC_FLAG_BLOCKING);
-
 
130
   
99
    ipcarg_t retval;
131
    if (phone < 0)
100
    aid_t req;
132
        return phone;
-
 
133
   
101
    ipc_call_t answer;
134
    ipc_call_t answer;
-
 
135
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_REGISTER, 0, 0,
102
    int phone;
136
        &answer);
-
 
137
   
-
 
138
    ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1);
-
 
139
   
-
 
140
    if (retval != EOK) {
-
 
141
        async_wait_for(req, NULL);
-
 
142
        return retval;
-
 
143
    }
-
 
144
   
-
 
145
    async_wait_for(req, &retval);
-
 
146
   
-
 
147
    if (retval != EOK) {
-
 
148
        if (handle != NULL)
-
 
149
            *handle = -1;
-
 
150
        return retval;
-
 
151
    }
-
 
152
   
-
 
153
    if (handle != NULL)
-
 
154
        *handle = (dev_handle_t) IPC_GET_ARG1(answer);
-
 
155
   
-
 
156
    return retval;
-
 
157
}
103
 
158
 
-
 
159
int devmap_device_get_handle(const char *name, dev_handle_t *handle, unsigned int flags)
-
 
160
{
104
    phone = devmap_get_phone(flags);
161
    int phone = devmap_get_phone(DEVMAP_CLIENT, flags);
-
 
162
   
105
    if (phone < 0)
163
    if (phone < 0)
106
        return phone;
164
        return phone;
107
 
165
   
-
 
166
    ipc_call_t answer;
108
    req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0,
167
    aid_t req = async_send_2(phone, DEVMAP_DEVICE_GET_HANDLE, flags, 0,
109
        &answer);
168
        &answer);
110
 
169
   
111
    retval = ipc_data_write_start(phone, name, str_size(name) + 1);
170
    ipcarg_t retval = ipc_data_write_start(phone, name, str_size(name) + 1);
112
 
171
   
113
    if (retval != EOK) {
172
    if (retval != EOK) {
114
        async_wait_for(req, NULL);
173
        async_wait_for(req, NULL);
115
        return retval;
174
        return retval;
116
    }
175
    }
117
 
176
   
118
    async_wait_for(req, &retval);
177
    async_wait_for(req, &retval);
119
 
178
   
120
    if (retval != EOK) {
179
    if (retval != EOK) {
121
        if (handle != NULL)
180
        if (handle != NULL)
122
            *handle = -1;
181
            *handle = -1;
123
        return retval;
182
        return retval;
124
    }
183
    }
125
 
184
   
126
    if (handle != NULL)
185
    if (handle != NULL)
127
        *handle = (int) IPC_GET_ARG1(answer);
186
        *handle = (dev_handle_t) IPC_GET_ARG1(answer);
128
 
187
   
129
    return retval;
188
    return retval;
130
}
189
}
131
 
190
 
132
int devmap_device_connect(dev_handle_t handle, unsigned int flags)
191
int devmap_device_connect(dev_handle_t handle, unsigned int flags)
133
{
192
{
134
    int phone;
193
    int phone;
135
 
194
   
136
    if (flags & IPC_FLAG_BLOCKING) {
195
    if (flags & IPC_FLAG_BLOCKING) {
137
        phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP,
196
        phone = ipc_connect_me_to_blocking(PHONE_NS, SERVICE_DEVMAP,
138
            DEVMAP_CONNECT_TO_DEVICE, handle);
197
            DEVMAP_CONNECT_TO_DEVICE, handle);
139
    } else {
198
    } else {
140
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
199
        phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
141
            DEVMAP_CONNECT_TO_DEVICE, handle);
200
            DEVMAP_CONNECT_TO_DEVICE, handle);
142
    }
201
    }
143
 
202
   
144
    return phone;
203
    return phone;
145
}
204
}
146
 
205
 
147
/** Register new device.
-
 
148
 *
-
 
149
 * @param driver_phone
-
 
150
 * @param name Device name.
206
ipcarg_t devmap_device_get_count(void)
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
{
207
{
-
 
208
    int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
-
 
209
   
155
    ipcarg_t retval;
210
    if (phone < 0)
156
    aid_t req;
211
        return 0;
157
    ipc_call_t answer;
-
 
158
 
212
   
-
 
213
    ipcarg_t count;
159
    req = async_send_2(driver_phone, DEVMAP_DEVICE_REGISTER, 0, 0,
214
    int retval = ipc_call_sync_0_1(phone, DEVMAP_DEVICE_GET_COUNT, &count);
-
 
215
    if (retval != EOK)
160
        &answer);
216
        return 0;
161
 
217
   
162
    retval = ipc_data_write_start(driver_phone, name, str_size(name) + 1);
218
    return count;
-
 
219
}
163
 
220
 
-
 
221
ipcarg_t devmap_device_get_devices(ipcarg_t count, dev_desc_t *data)
-
 
222
{
-
 
223
    int phone = devmap_get_phone(DEVMAP_CLIENT, IPC_FLAG_BLOCKING);
-
 
224
   
-
 
225
    if (phone < 0)
-
 
226
        return 0;
-
 
227
   
-
 
228
    ipc_call_t answer;
-
 
229
    aid_t req = async_send_0(phone, DEVMAP_DEVICE_GET_DEVICES, &answer);
-
 
230
   
-
 
231
    ipcarg_t retval = ipc_data_read_start(phone, data, count * sizeof(dev_desc_t));
-
 
232
   
164
    if (retval != EOK) {
233
    if (retval != EOK) {
165
        async_wait_for(req, NULL);
234
        async_wait_for(req, NULL);
166
        return retval;
235
        return 0;
167
    }
236
    }
168
 
237
   
169
    async_wait_for(req, &retval);
238
    async_wait_for(req, &retval);
170
 
239
   
171
    if (retval != EOK) {
240
    if (retval != EOK)
172
        if (handle != NULL)
-
 
173
            *handle = -1;
-
 
174
        return retval;
241
        return 0;
175
    }
242
   
176
 
-
 
177
    *handle = (int) IPC_GET_ARG1(answer);
243
    return IPC_GET_ARG1(answer);
178
    return retval;
-
 
179
}
244
}