source: mainline/uspace/lib/libdrv/generic/driver.c@ eff1a590

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

child device registration - match ids

  • Property mode set to 100644
File size: 4.7 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 <string.h>
48#include <ctype.h>
49#include <errno.h>
50
51#include <devman.h>
52#include <ipc/devman.h>
53#include <ipc/driver.h>
54
55#include "driver.h"
56
57static driver_t *driver;
58LIST_INITIALIZE(devices);
59
60static device_t* driver_create_device()
61{
62 device_t *dev = (device_t *)malloc(sizeof(device_t));
63 if (NULL != dev) {
64 memset(dev, 0, sizeof(device_t));
65 }
66 return dev;
67}
68
69static void driver_add_device(ipc_callid_t iid, ipc_call_t *icall)
70{
71 printf("%s: driver_add_device\n", driver->name);
72
73 // result of the operation - device was added, device is not present etc.
74 ipcarg_t ret = 0;
75 device_handle_t dev_handle = IPC_GET_ARG1(*icall);
76 device_t *dev = driver_create_device();
77 dev->handle = dev_handle;
78 if (driver->driver_ops->add_device(dev)) {
79 list_append(&dev->link, &devices);
80 // TODO set return value
81 }
82 printf("%s: new device with handle = %x was added.\n", driver->name, dev_handle);
83
84 ipcarg_t r = ipc_answer_1(iid, EOK, ret);
85}
86
87static void driver_connection_devman(ipc_callid_t iid, ipc_call_t *icall)
88{
89 printf("%s: driver_connection_devman \n", driver->name);
90
91 /* Accept connection */
92 ipc_answer_0(iid, EOK);
93
94 bool cont = true;
95 while (cont) {
96 ipc_call_t call;
97 ipc_callid_t callid = async_get_call(&call);
98
99 switch (IPC_GET_METHOD(call)) {
100 case IPC_M_PHONE_HUNGUP:
101 cont = false;
102 continue;
103 case DRIVER_ADD_DEVICE:
104 driver_add_device(callid, &call);
105 break;
106 default:
107 if (!(callid & IPC_CALLID_NOTIFICATION))
108 ipc_answer_0(callid, ENOENT);
109 }
110 }
111}
112
113static void driver_connection_driver(ipc_callid_t iid, ipc_call_t *icall)
114{
115 // TODO later
116}
117
118static void driver_connection_client(ipc_callid_t iid, ipc_call_t *icall)
119{
120 // TODO later
121}
122
123
124/** Function for handling connections to device driver.
125 *
126 */
127static void driver_connection(ipc_callid_t iid, ipc_call_t *icall)
128{
129 /* Select interface */
130 switch ((ipcarg_t) (IPC_GET_ARG1(*icall))) {
131 case DRIVER_DEVMAN:
132 // handle PnP events from device manager
133 driver_connection_devman(iid, icall);
134 break;
135 case DRIVER_DRIVER:
136 // handle request from drivers of child devices
137 driver_connection_driver(iid, icall);
138 break;
139 case DRIVER_CLIENT:
140 // handle requests from client applications
141 driver_connection_client(iid, icall);
142 break;
143
144 default:
145 /* No such interface */
146 ipc_answer_0(iid, ENOENT);
147 }
148}
149
150bool child_device_register(device_t *child, device_t *parent)
151{
152 printf("%s: child_device_register\n", driver->name);
153
154 assert(NULL != child->name);
155
156 if (EOK == devman_child_device_register(child->name, &child->match_ids, parent->handle, &child->handle)) {
157 return true;
158 }
159 return false;
160}
161
162int driver_main(driver_t *drv)
163{
164 // remember the driver structure - driver_ops will be called by generic handler for incoming connections
165 driver = drv;
166
167 // register driver by device manager with generic handler for incoming connections
168 devman_driver_register(driver->name, driver_connection);
169
170 async_manager();
171
172 // Never reached
173 return 0;
174}
175
176/**
177 * @}
178 */
Note: See TracBrowser for help on using the repository browser.