source: mainline/uspace/lib/drv/generic/driver.c@ 692c40cb

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

Introduce device classes.Device class specifies functional type of the device. Device classes are identified by their string names (actually device class is just a string value). Device classes can be dynamically added. A device can be added to any number of classes.

  • Property mode set to 100644
File size: 9.9 KB
Line 
1/*
2 * Copyright (c) 2010 Lenka Trochtova
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/**
30 * @defgroup libdrv generic device driver support.
31 * @brief HelenOS generic device driver support.
32 * @{
33 */
34
35/** @file
36 */
37
38#include <assert.h>
39#include <ipc/services.h>
40#include <ipc/ns.h>
41#include <async.h>
42#include <stdio.h>
43#include <errno.h>
44#include <bool.h>
45#include <fibril_synch.h>
46#include <stdlib.h>
47#include <str.h>
48#include <ctype.h>
49#include <errno.h>
50
51#include <ipc/driver.h>
52
53#include "driver.h"
54
55// driver structure
56
57static driver_t *driver;
58
59// devices
60
61LIST_INITIALIZE(devices);
62FIBRIL_MUTEX_INITIALIZE(devices_mutex);
63
64// interrupts
65
66static interrupt_context_list_t interrupt_contexts;
67
68static irq_cmd_t default_cmds[] = {
69 {
70 .cmd = CMD_ACCEPT
71 }
72};
73
74static irq_code_t default_pseudocode = {
75 sizeof(default_cmds) / sizeof(irq_cmd_t),
76 default_cmds
77};
78
79
80static void driver_irq_handler(ipc_callid_t iid, ipc_call_t *icall)
81{
82 int id = (int)IPC_GET_METHOD(*icall);
83 interrupt_context_t *ctx = find_interrupt_context_by_id(&interrupt_contexts, id);
84 if (NULL != ctx && NULL != ctx->handler) {
85 (*ctx->handler)(ctx->dev, iid, icall);
86 }
87}
88
89int register_interrupt_handler(device_t *dev, int irq, interrupt_handler_t *handler, irq_code_t *pseudocode)
90{
91 interrupt_context_t *ctx = create_interrupt_context();
92
93 ctx->dev = dev;
94 ctx->irq = irq;
95 ctx->handler = handler;
96
97 add_interrupt_context(&interrupt_contexts, ctx);
98
99 if (NULL == pseudocode) {
100 pseudocode = &default_pseudocode;
101 }
102
103 int res = ipc_register_irq(irq, dev->handle, ctx->id, pseudocode);
104 if (0 != res) {
105 remove_interrupt_context(&interrupt_contexts, ctx);
106 delete_interrupt_context(ctx);
107 }
108 return res;
109}
110
111int unregister_interrupt_handler(device_t *dev, int irq)
112{
113 interrupt_context_t *ctx = find_interrupt_context(&interrupt_contexts, dev, irq);
114 int res = ipc_unregister_irq(irq, dev->handle);
115 if (NULL != ctx) {
116 remove_interrupt_context(&interrupt_contexts, ctx);
117 delete_interrupt_context(ctx);
118 }
119 return res;
120}
121
122static void add_to_devices_list(device_t *dev)
123{
124 fibril_mutex_lock(&devices_mutex);
125 list_append(&dev->link, &devices);
126 fibril_mutex_unlock(&devices_mutex);
127}
128
129static void remove_from_devices_list(device_t *dev)
130{
131 fibril_mutex_lock(&devices_mutex);
132 list_remove(&dev->link);
133 fibril_mutex_unlock(&devices_mutex);
134}
135
136static device_t * driver_get_device(link_t *devices, device_handle_t handle)
137{
138 device_t *dev = NULL;
139
140 fibril_mutex_lock(&devices_mutex);
141 link_t *link = devices->next;
142 while (link != devices) {
143 dev = list_get_instance(link, device_t, link);
144 if (handle == dev->handle) {
145 fibril_mutex_unlock(&devices_mutex);
146 return dev;
147 }
148 link = link->next;
149 }
150 fibril_mutex_unlock(&devices_mutex);
151
152 return NULL;
153}
154
155static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
156{
157 char *dev_name = NULL;
158 int res = EOK;
159
160 device_handle_t dev_handle = IPC_GET_ARG1(*icall);
161 device_t *dev = create_device();
162 dev->handle = dev_handle;
163
164 async_data_write_accept((void **)&dev_name, true, 0, 0, 0, 0);
165 dev->name = dev_name;
166
167 add_to_devices_list(dev);
168 res = driver->driver_ops->add_device(dev);
169 if (0 == res) {
170 printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
171 } else {
172 printf("%s: failed to add a new device with handle = %d.\n", driver->name, dev_handle);
173 remove_from_devices_list(dev);
174 delete_device(dev);
175 }
176
177 ipc_answer_0(iid, res);
178}
179
180static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
181{
182 /* Accept connection */
183 ipc_answer_0(iid, EOK);
184
185 bool cont = true;
186 while (cont) {
187 ipc_call_t call;
188 ipc_callid_t callid = async_get_call(&call);
189
190 switch (IPC_GET_METHOD(call)) {
191 case IPC_M_PHONE_HUNGUP:
192 cont = false;
193 continue;
194 case DRIVER_ADD_DEVICE:
195 driver_add_device(callid, &call);
196 break;
197 default:
198 if (!(callid & IPC_CALLID_NOTIFICATION))
199 ipc_answer_0(callid, ENOENT);
200 }
201 }
202}
203
204/**
205 * Generic client connection handler both for applications and drivers.
206 *
207 * @param drv true for driver client, false for other clients (applications, services etc.).
208 */
209static void driver_connection_gen(ipc_callid_t iid, ipc_call_t *icall, bool drv)
210{
211 // Answer the first IPC_M_CONNECT_ME_TO call and remember the handle of the device to which the client connected.
212 device_handle_t handle = IPC_GET_ARG2(*icall);
213 device_t *dev = driver_get_device(&devices, handle);
214
215 if (dev == NULL) {
216 printf("%s: driver_connection_gen error - no device with handle %x was found.\n", driver->name, handle);
217 ipc_answer_0(iid, ENOENT);
218 return;
219 }
220
221
222 // TODO - if the client is not a driver, check whether it is allowed to use the device
223
224 int ret = EOK;
225 // open the device
226 if (NULL != dev->class && NULL != dev->class->open) {
227 ret = (*dev->class->open)(dev);
228 }
229
230 ipc_answer_0(iid, ret);
231
232 while (1) {
233 ipc_callid_t callid;
234 ipc_call_t call;
235 callid = async_get_call(&call);
236 ipcarg_t method = IPC_GET_METHOD(call);
237 int iface_idx;
238
239 switch (method) {
240 case IPC_M_PHONE_HUNGUP:
241 // close the device
242 if (NULL != dev->class && NULL != dev->class->close) {
243 (*dev->class->close)(dev);
244 }
245 ipc_answer_0(callid, EOK);
246 return;
247 default:
248 // convert ipc interface id to interface index
249
250 iface_idx = DEV_IFACE_IDX(method);
251
252 if (!is_valid_iface_idx(iface_idx)) {
253 remote_handler_t *default_handler;
254 if (NULL != (default_handler = device_get_default_handler(dev))) {
255 (*default_handler)(dev, callid, &call);
256 break;
257 }
258 // this is not device's interface and the default handler is not provided
259 printf("%s: driver_connection_gen error - invalid interface id %d.", driver->name, iface_idx);
260 ipc_answer_0(callid, ENOTSUP);
261 break;
262 }
263
264 // calling one of the device's interfaces
265
266 // get the device interface structure
267 void *iface = device_get_iface(dev, iface_idx);
268 if (NULL == iface) {
269 printf("%s: driver_connection_gen error - ", driver->name);
270 printf("device with handle %d has no interface with id %d.\n", handle, iface_idx);
271 ipc_answer_0(callid, ENOTSUP);
272 break;
273 }
274
275 // get the corresponding interface for remote request handling ("remote interface")
276 remote_iface_t* rem_iface = get_remote_iface(iface_idx);
277 assert(NULL != rem_iface);
278
279 // get the method of the remote interface
280 ipcarg_t iface_method_idx = IPC_GET_ARG1(call);
281 remote_iface_func_ptr_t iface_method_ptr = get_remote_method(rem_iface, iface_method_idx);
282 if (NULL == iface_method_ptr) {
283 // the interface has not such method
284 printf("%s: driver_connection_gen error - invalid interface method.", driver->name);
285 ipc_answer_0(callid, ENOTSUP);
286 break;
287 }
288
289 // call the remote interface's method, which will receive parameters from the remote client
290 // and it will pass it to the corresponding local interface method associated with the device
291 // by its driver
292 (*iface_method_ptr)(dev, iface, callid, &call);
293 break;
294 }
295 }
296}
297
298static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
299{
300 driver_connection_gen(iid, icall, true);
301}
302
303static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
304{
305 driver_connection_gen(iid, icall, false);
306}
307
308
309/** Function for handling connections to device driver.
310 *
311 */
312static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
313{
314 /* Select interface */
315 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
316 case DRIVER_DEVMAN:
317 // handle PnP events from device manager
318 driver_connection_devman(iid, icall);
319 break;
320 case DRIVER_DRIVER:
321 // handle request from drivers of child devices
322 driver_connection_driver(iid, icall);
323 break;
324 case DRIVER_CLIENT:
325 // handle requests from client applications
326 driver_connection_client(iid, icall);
327 break;
328
329 default:
330 /* No such interface */
331 ipc_answer_0(iid, ENOENT);
332 }
333}
334
335int child_device_register(device_t *child, device_t *parent)
336{
337 // printf("%s: child_device_register\n", driver->name);
338
339 assert(NULL != child->name);
340
341 int res;
342
343 add_to_devices_list(child);
344 if (EOK == (res = devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle))) {
345 return res;
346 }
347 remove_from_devices_list(child);
348 return res;
349}
350
351int driver_main(driver_t *drv)
352{
353 // remember the driver structure - driver_ops will be called by generic handler for incoming connections
354 driver = drv;
355
356 // initialize the list of interrupt contexts
357 init_interrupt_context_list(&interrupt_contexts);
358
359 // set generic interrupt handler
360 async_set_interrupt_received(driver_irq_handler);
361
362 // register driver by device manager with generic handler for incoming connections
363 devman_driver_register(driver->name, driver_connection);
364
365 async_manager();
366
367 // Never reached
368 return 0;
369}
370
371/**
372 * @}
373 */
Note: See TracBrowser for help on using the repository browser.