Subversion Repositories HelenOS

Rev

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

Rev 3530 Rev 3531
Line 44... Line 44...
44
#include <sys/mman.h>
44
#include <sys/mman.h>
45
#include <async.h>
45
#include <async.h>
46
#include <ipc/ipc.h>
46
#include <ipc/ipc.h>
47
#include <as.h>
47
#include <as.h>
48
#include <assert.h>
48
#include <assert.h>
-
 
49
#include <futex.h>
-
 
50
#include <libadt/list.h>
49
 
51
 
-
 
52
/** Lock protecting the device connection list */
50
static int dev_phone = -1;      /* FIXME */
53
static futex_t dcl_lock = FUTEX_INITIALIZER;
-
 
54
/** Device connection list head. */
-
 
55
static LIST_INITIALIZE(dcl_head);
-
 
56
 
-
 
57
typedef struct {
-
 
58
    link_t link;
-
 
59
    int dev_handle;
-
 
60
    int dev_phone;
-
 
61
    void *com_area;
-
 
62
    size_t com_size;
-
 
63
    void *bb_buf;
-
 
64
    off_t bb_off;
-
 
65
    size_t bb_size;
-
 
66
} devcon_t;
-
 
67
 
51
static void *dev_buffer = NULL;     /* FIXME */
68
static devcon_t *devcon_search(dev_handle_t dev_handle)
-
 
69
{
-
 
70
    link_t *cur;
-
 
71
 
-
 
72
    futex_down(&dcl_lock);
-
 
73
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
-
 
74
        devcon_t *devcon = list_get_instance(cur, devcon_t, link);
-
 
75
        if (devcon->dev_handle == dev_handle) {
-
 
76
            futex_up(&dcl_lock);
-
 
77
            return devcon;
-
 
78
        }
-
 
79
    }
-
 
80
    futex_up(&dcl_lock);
-
 
81
    return NULL;
-
 
82
}
-
 
83
 
-
 
84
static int devcon_add(dev_handle_t dev_handle, int dev_phone, void *com_area,
-
 
85
   size_t com_size, void *bb_buf, off_t bb_off, size_t bb_size)
-
 
86
{
-
 
87
    link_t *cur;
-
 
88
    devcon_t *devcon;
-
 
89
 
-
 
90
    devcon = malloc(sizeof(devcon_t));
-
 
91
    if (!devcon)
-
 
92
        return ENOMEM;
-
 
93
   
-
 
94
    link_initialize(&devcon->link);
-
 
95
    devcon->dev_handle = dev_handle;
-
 
96
    devcon->dev_phone = dev_phone;
-
 
97
    devcon->com_area = com_area;
-
 
98
    devcon->com_size = com_size;
-
 
99
    devcon->bb_buf = bb_buf;
-
 
100
    devcon->bb_off = bb_off;
-
 
101
    devcon->bb_size = bb_size;
-
 
102
 
-
 
103
    futex_down(&dcl_lock);
-
 
104
    for (cur = dcl_head.next; cur != &dcl_head; cur = cur->next) {
-
 
105
        devcon_t *d = list_get_instance(cur, devcon_t, link);
52
static size_t dev_buffer_len = 0;   /* FIXME */
106
        if (d->dev_handle == dev_handle) {
-
 
107
            futex_up(&dcl_lock);
-
 
108
            free(devcon);
-
 
109
            return EEXIST;
-
 
110
        }
-
 
111
    }
-
 
112
    list_append(&devcon->link, &dcl_head);
-
 
113
    futex_up(&dcl_lock);
-
 
114
    return EOK;
-
 
115
}
-
 
116
 
53
static void *bblock = NULL;     /* FIXME */
117
static void devcon_remove(devcon_t *devcon)
-
 
118
{
-
 
119
    futex_down(&dcl_lock);
-
 
120
    list_remove(&devcon->link);
-
 
121
    futex_up(&dcl_lock);
-
 
122
}
54
 
123
 
55
int
124
int
56
block_init(dev_handle_t dev_handle, size_t com_size, off_t bb_off,
125
block_init(dev_handle_t dev_handle, size_t com_size, off_t bb_off,
57
    size_t bb_size)
126
    size_t bb_size)
58
{
127
{
59
    int rc;
128
    int rc;
-
 
129
    int dev_phone;
-
 
130
    void *com_area;
-
 
131
    void *bb_buf;
60
 
132
   
61
    bblock = malloc(bb_size);
133
    bb_buf = malloc(bb_size);
62
    if (!bblock)
134
    if (!bb_buf)
63
        return ENOMEM;
135
        return ENOMEM;
64
    dev_buffer_len = com_size;
-
 
-
 
136
   
65
    dev_buffer = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
137
    com_area = mmap(NULL, com_size, PROTO_READ | PROTO_WRITE,
66
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
138
        MAP_ANONYMOUS | MAP_PRIVATE, 0, 0);
67
    if (!dev_buffer) {
139
    if (!com_area) {
68
        free(bblock);
140
        free(bb_buf);
69
        return ENOMEM;
141
        return ENOMEM;
70
    }
142
    }
71
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
143
    dev_phone = ipc_connect_me_to(PHONE_NS, SERVICE_DEVMAP,
72
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
144
        DEVMAP_CONNECT_TO_DEVICE, dev_handle);
73
 
145
 
74
    if (dev_phone < 0) {
146
    if (dev_phone < 0) {
75
        free(bblock);
147
        free(bb_buf);
76
        munmap(dev_buffer, com_size);
148
        munmap(com_area, com_size);
77
        return dev_phone;
149
        return dev_phone;
78
    }
150
    }
79
 
151
 
80
    rc = ipc_share_out_start(dev_phone, dev_buffer,
152
    rc = ipc_share_out_start(dev_phone, com_area,
81
        AS_AREA_READ | AS_AREA_WRITE);
153
        AS_AREA_READ | AS_AREA_WRITE);
82
    if (rc != EOK) {
154
    if (rc != EOK) {
-
 
155
        free(bb_buf);
-
 
156
            munmap(com_area, com_size);
-
 
157
        ipc_hangup(dev_phone);
-
 
158
        return rc;
-
 
159
    }
-
 
160
   
-
 
161
    rc = devcon_add(dev_handle, dev_phone, com_area, com_size, bb_buf,
-
 
162
        bb_off, bb_size);
-
 
163
    if (rc != EOK) {
-
 
164
        free(bb_buf);
-
 
165
        munmap(com_area, com_size);
83
        ipc_hangup(dev_phone);
166
        ipc_hangup(dev_phone);
84
        free(bblock);
-
 
85
            munmap(dev_buffer, com_size);
-
 
86
        return rc;
167
        return rc;
87
    }
168
    }
-
 
169
 
88
    off_t bufpos = 0;
170
    off_t bufpos = 0;
89
    size_t buflen = 0;
171
    size_t buflen = 0;
90
    if (!block_read(dev_handle, &bufpos, &buflen, &bb_off,
172
    if (!block_read(dev_handle, &bufpos, &buflen, &bb_off,
91
        bblock, bb_size, bb_size)) {
173
        bb_buf, bb_size, bb_size)) {
92
            ipc_hangup(dev_phone);
174
        block_fini(dev_handle);
93
            free(bblock);
-
 
94
        munmap(dev_buffer, com_size);
-
 
95
        return EIO; /* XXX real error code */
175
        return EIO; /* XXX real error code */
96
    }
176
    }
-
 
177
   
97
    return EOK;
178
    return EOK;
98
}
179
}
99
 
180
 
100
void block_fini(dev_handle_t dev_handle)
181
void block_fini(dev_handle_t dev_handle)
101
{
182
{
-
 
183
    devcon_t *devcon = devcon_search(dev_handle);
102
    /* XXX */
184
    assert(devcon);
-
 
185
   
-
 
186
    devcon_remove(devcon);
-
 
187
 
103
    free(bblock);
188
    free(devcon->bb_buf);
104
    munmap(dev_buffer, dev_buffer_len);
189
    munmap(devcon->com_area, devcon->com_size);
105
    ipc_hangup(dev_phone);
190
    ipc_hangup(devcon->dev_phone);
-
 
191
 
-
 
192
    free(devcon);  
106
}
193
}
107
 
194
 
108
void *block_bb_get(dev_handle_t dev_handle)
195
void *block_bb_get(dev_handle_t dev_handle)
109
{
196
{
-
 
197
    devcon_t *devcon = devcon_search(dev_handle);
110
    /* XXX */
198
    assert(devcon);
111
    return bblock;
199
    return devcon->bb_buf;
112
}
200
}
113
 
201
 
114
/** Read data from a block device.
202
/** Read data from a block device.
115
 *
203
 *
116
 * @param dev_handle    Device handle of the block device.
204
 * @param dev_handle    Device handle of the block device.
Line 129... Line 217...
129
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
217
block_read(int dev_handle, off_t *bufpos, size_t *buflen, off_t *pos, void *dst,
130
    size_t size, size_t block_size)
218
    size_t size, size_t block_size)
131
{
219
{
132
    off_t offset = 0;
220
    off_t offset = 0;
133
    size_t left = size;
221
    size_t left = size;
-
 
222
    devcon_t *devcon = devcon_search(dev_handle);
-
 
223
    assert(devcon);
134
   
224
   
135
    while (left > 0) {
225
    while (left > 0) {
136
        size_t rd;
226
        size_t rd;
137
       
227
       
138
        if (*bufpos + left < *buflen)
228
        if (*bufpos + left < *buflen)
Line 143... Line 233...
143
        if (rd > 0) {
233
        if (rd > 0) {
144
            /*
234
            /*
145
             * Copy the contents of the communication buffer to the
235
             * Copy the contents of the communication buffer to the
146
             * destination buffer.
236
             * destination buffer.
147
             */
237
             */
148
            memcpy(dst + offset, dev_buffer + *bufpos, rd);
238
            memcpy(dst + offset, devcon->com_area + *bufpos, rd);
149
            offset += rd;
239
            offset += rd;
150
            *bufpos += rd;
240
            *bufpos += rd;
151
            *pos += rd;
241
            *pos += rd;
152
            left -= rd;
242
            left -= rd;
153
        }
243
        }
154
       
244
       
155
        if (*bufpos == *buflen) {
245
        if (*bufpos == *buflen) {
156
            /* Refill the communication buffer with a new block. */
246
            /* Refill the communication buffer with a new block. */
157
            ipcarg_t retval;
247
            ipcarg_t retval;
158
            int rc = async_req_2_1(dev_phone, RD_READ_BLOCK,
248
            int rc = async_req_2_1(devcon->dev_phone, RD_READ_BLOCK,
159
                *pos / block_size, block_size, &retval);
249
                *pos / block_size, block_size, &retval);
160
            if ((rc != EOK) || (retval != EOK))
250
            if ((rc != EOK) || (retval != EOK))
161
                return false;
251
                return false;
162
           
252
           
163
            *bufpos = 0;
253
            *bufpos = 0;
Line 174... Line 264...
174
    block_t *b;
264
    block_t *b;
175
    off_t bufpos = 0;
265
    off_t bufpos = 0;
176
    size_t buflen = 0;
266
    size_t buflen = 0;
177
    off_t pos = offset * bs;
267
    off_t pos = offset * bs;
178
 
268
 
179
    assert(dev_phone != -1);
-
 
180
    assert(dev_buffer);
-
 
181
 
-
 
182
    b = malloc(sizeof(block_t));
269
    b = malloc(sizeof(block_t));
183
    if (!b)
270
    if (!b)
184
        return NULL;
271
        return NULL;
185
   
272
   
186
    b->data = malloc(bs);
273
    b->data = malloc(bs);