summaryrefslogtreecommitdiff
path: root/deva/device.c
blob: 3797b17f2864c5be17bd616fa84e855183657711 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* deva-class.c - Deva class for the deva server.
   Copyright (C) 2003, 2004 Free Software Foundation, Inc.
   Written by Marcus Brinkmann.

   This file is part of the GNU Hurd.

   The GNU Hurd is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU Hurd is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, write to the Free
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
   02111-1307 USA.  */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>

#include <l4.h>
#include <hurd/cap-server.h>

#include "deva.h"
#include "device.h"


static void
device_reinit (hurd_cap_class_t cap_class, hurd_cap_obj_t obj)
{
  device_t *dev = hurd_cap_obj_to_user (device_t *, obj);

  /* FIXME: Release resources.  */
}


static error_t
device_io_read (hurd_cap_rpc_context_t ctx)
{
  error_t err;
  device_t *dev = hurd_cap_obj_to_user (device_t *, ctx->obj);
  int chr;

  err =  (*dev->io_read) (dev, &chr);
  if (err)
    return err;

  /* Prepare reply message.  */
  l4_msg_clear (ctx->msg);
  l4_msg_append_word (ctx->msg, (l4_word_t) chr);

  return 0;
}


static error_t
device_io_write (hurd_cap_rpc_context_t ctx)
{
  device_t *dev = hurd_cap_obj_to_user (device_t *, ctx->obj);
  int chr;

  chr = (int) l4_msg_word (ctx->msg, 1);

  return (*dev->io_write) (dev, chr);
}


static error_t
device_demuxer (hurd_cap_rpc_context_t ctx)
{
  error_t err = 0;

  switch (l4_msg_label (ctx->msg))
    {
      /* DEVICE_IO_READ */
    case 768:
      err = device_io_read (ctx);
      break;

      /* DEVICE_IO_WRITE */
    case 769:
      err = device_io_write (ctx);
      break;

    default:
      err = EOPNOTSUPP;
    }

  return err;
}



static struct hurd_cap_class device_class;

/* Initialize the device class subsystem.  */
error_t
device_class_init ()
{
  return hurd_cap_class_init (&device_class, device_t *,
			      NULL, NULL, device_reinit, NULL,
			      device_demuxer);
}


/* Allocate a new device object.  The object returned is locked and
   has one reference.  */
error_t
device_alloc (hurd_cap_obj_t *r_obj, enum device_type type)
{
  error_t err;
  hurd_cap_obj_t obj;
  device_t *dev;

  err = hurd_cap_class_alloc (&device_class, &obj);
  if (err)
    return err;

  dev = hurd_cap_obj_to_user (device_t *, obj);

  switch (type)
    {
    case DEVICE_CONSOLE:
      device_console_init (dev);
      break;

    case DEVICE_SERIAL:
      device_serial_init (dev);
      break;

    default:
      break;
    }

  *r_obj = obj;
  return 0;
}