summaryrefslogtreecommitdiff
path: root/minix/servers/devman/bind.c
blob: 298a356ee05481fbbe596ee39a0bc6ac9a5a07cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "devman.h"
#include "proto.h"

/*****************************************************************************
 *    do_bind_device                                                         *
 ****************************************************************************/
int do_bind_device(message *m)
{
	struct devman_device *dev;
	int res;
	endpoint_t src = m->m_source;

	/* check if msg comes from RS */
	if (src != RS_PROC_NR) {
		m->DEVMAN_RESULT = EPERM;
		printf("[W] could bind message from somebody else than RS\n");
		
		return 0;
	} else {
		/* get the device */
		dev = devman_find_device(m->DEVMAN_DEVICE_ID);
		/* bind device at device provider*/
		if (dev != NULL) {
			m->m_type = DEVMAN_BIND;
			/* ...device ID and endpoint is still set */

#ifdef DEBUG
			printf("devman: bind call to %d for dev %d\n",
			    dev->owner, m->DEVMAN_DEVICE_ID);
#endif
			
			res = ipc_sendrec(dev->owner, m);
			if (res != OK) {
				printf("[W] devman.do_bind_device(): could not send "
				       "message to device owner (%d)\n", res);
				m->DEVMAN_RESULT= res;
			} else if (m->DEVMAN_RESULT != OK) {
				printf("[W] devman.do_bind_device(): driver could"
				       " not bind device (%ld)\n", m->DEVMAN_RESULT);
			} else {
				dev->state = DEVMAN_DEVICE_BOUND;
				devman_get_device(dev);
			}
		} else {
			m->DEVMAN_RESULT = ENODEV;
		}
		m->m_type = DEVMAN_REPLY;
		ipc_send(RS_PROC_NR, m);
	}
	return 0;
}

/*****************************************************************************
 *    do_unbind_device                                                       *
 ****************************************************************************/
int do_unbind_device(message *m)
{
	struct devman_device *dev;
	int res;
	endpoint_t src = m->m_source;

	/* check if msg comes from RS */
	if (src != RS_PROC_NR) {
		m->DEVMAN_RESULT = EPERM;
		printf("[W] devman.do_unbind_device(): unbind message from somebody"
		       "else than RS (%d)\n", src);
		return 0;
	} else {
		/* get the device */
		dev = devman_find_device(m->DEVMAN_DEVICE_ID);
		/* bind device at device provider*/
		if (dev != NULL) {

			m->m_type = DEVMAN_UNBIND;
			/* ...device ID and endpoint is still set */
#ifdef DEBUG
			printf("devman: unbind call to %d for dev %d\n",
			    dev->owner, m->DEVMAN_DEVICE_ID);
#endif
			res = ipc_sendrec(dev->owner, m);
			if (res != OK) {
				printf("[W] devman.do_unbind_device(): could not send "
				       "message to device owner (%d)\n", res);
				m->DEVMAN_RESULT= res;
			} else if (m->DEVMAN_RESULT != OK && m->DEVMAN_RESULT != 19) {
				/* device drive deleted device already? */
				printf("[W] devman.do_unbind_device(): driver could"
				       " not unbind device (%ld)\n", m->DEVMAN_RESULT);
			} else { 
				if (dev->state != DEVMAN_DEVICE_ZOMBIE) {
					dev->state = DEVMAN_DEVICE_UNBOUND;
				}
				devman_put_device(dev);
				m->DEVMAN_RESULT = OK;
			}
		} else {
			/* this might be the case, but perhaps its better to keep 
			   the device in the db as long a driver is bound to it*/
			m->DEVMAN_RESULT = ENODEV;
		}
		m->m_type = DEVMAN_REPLY;
		ipc_send(RS_PROC_NR, m);
	}
	return 0;
}