source: rtems/cpukit/libblock/src/blkdev.c @ 0b038bd4

Last change on this file since 0b038bd4 was 0b038bd4, checked in by Sebastian Huber <sebastian.huber@…>, on Aug 4, 2018 at 8:38:48 AM

libblock: Add RTEMS_DEPRECATED

Close #3358.

  • Property mode set to 100644
File size: 5.0 KB
Line 
1/**
2 * @file
3 *
4 * @brief Block Device Management
5 * @ingroup rtems_blkdev
6 */
7
8/*
9 * Copyright (C) 2001 OKTET Ltd., St.-Petersburg, Russia
10 * Author: Victor V. Vengerov <vvv@oktet.ru>
11 */
12
13#if HAVE_CONFIG_H
14#include "config.h"
15#endif
16
17#include <errno.h>
18#include <string.h>
19
20#include <rtems.h>
21#include <rtems/libio.h>
22#include <sys/ioctl.h>
23
24#include "rtems/diskdevs.h"
25#include "rtems/bdbuf.h"
26
27#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
28
29/* rtems_blkdev_generic_read --
30 *     Generic block device read primitive. Implemented using block device
31 *     buffer management primitives.
32 */
33rtems_device_driver
34rtems_blkdev_generic_read(
35    rtems_device_major_number major RTEMS_UNUSED,
36    rtems_device_minor_number minor RTEMS_UNUSED,
37    void                    * arg)
38{
39    rtems_status_code rc = RTEMS_SUCCESSFUL;
40    rtems_libio_rw_args_t *args = arg;
41    rtems_libio_t *iop = args->iop;
42    rtems_disk_device *dd = iop->data1;
43    uint32_t block_size = dd->block_size;
44    char *buf = args->buffer;
45    uint32_t count = args->count;
46    rtems_blkdev_bnum block = (rtems_blkdev_bnum) (args->offset / block_size);
47    uint32_t blkofs = (uint32_t) (args->offset % block_size);
48
49    args->bytes_moved = 0;
50
51    while (count > 0)
52    {
53        rtems_bdbuf_buffer *diskbuf;
54        uint32_t            copy;
55
56        rc = rtems_bdbuf_read(dd, block, &diskbuf);
57        if (rc != RTEMS_SUCCESSFUL)
58            break;
59        copy = block_size - blkofs;
60        if (copy > count)
61            copy = count;
62        memcpy(buf, (char *)diskbuf->buffer + blkofs, copy);
63        rc = rtems_bdbuf_release(diskbuf);
64        args->bytes_moved += copy;
65        if (rc != RTEMS_SUCCESSFUL)
66            break;
67        count -= copy;
68        buf += copy;
69        blkofs = 0;
70        block++;
71    }
72
73    return rc;
74}
75
76/* rtems_blkdev_generic_write --
77 *     Generic block device write primitive. Implemented using block device
78 *     buffer management primitives.
79 */
80rtems_device_driver
81rtems_blkdev_generic_write(
82    rtems_device_major_number major RTEMS_UNUSED,
83    rtems_device_minor_number minor RTEMS_UNUSED,
84    void                    * arg)
85{
86    rtems_status_code rc = RTEMS_SUCCESSFUL;
87    rtems_libio_rw_args_t *args = arg;
88    rtems_libio_t *iop = args->iop;
89    rtems_disk_device *dd = iop->data1;
90    uint32_t block_size = dd->block_size;
91    char *buf = args->buffer;
92    uint32_t count = args->count;
93    rtems_blkdev_bnum block = (rtems_blkdev_bnum) (args->offset / block_size);
94    uint32_t blkofs = (uint32_t) (args->offset % block_size);
95
96    args->bytes_moved = 0;
97
98    while (count > 0)
99    {
100        rtems_bdbuf_buffer *diskbuf;
101        uint32_t            copy;
102
103        if ((blkofs == 0) && (count >= block_size))
104            rc = rtems_bdbuf_get(dd, block, &diskbuf);
105        else
106            rc = rtems_bdbuf_read(dd, block, &diskbuf);
107        if (rc != RTEMS_SUCCESSFUL)
108            break;
109
110        copy = block_size - blkofs;
111        if (copy > count)
112            copy = count;
113        memcpy((char *)diskbuf->buffer + blkofs, buf, copy);
114        args->bytes_moved += copy;
115
116        rc = rtems_bdbuf_release_modified(diskbuf);
117        if (rc != RTEMS_SUCCESSFUL)
118            break;
119
120        count -= copy;
121        buf += copy;
122        blkofs = 0;
123        block++;
124    }
125
126    return rc;
127}
128
129/* blkdev_generic_open --
130 *     Generic block device open primitive.
131 */
132rtems_device_driver
133rtems_blkdev_generic_open(
134    rtems_device_major_number major,
135    rtems_device_minor_number minor,
136    void                    * arg)
137{
138  rtems_libio_open_close_args_t *oc = arg;
139  rtems_libio_t *iop = oc->iop;
140  dev_t dev = rtems_filesystem_make_dev_t(major, minor);
141  rtems_disk_device *dd = rtems_disk_obtain(dev);
142
143  iop->data1 = dd;
144
145  if (dd != NULL)
146    return RTEMS_SUCCESSFUL;
147  else
148    return RTEMS_UNSATISFIED;
149}
150
151
152/* blkdev_generic_close --
153 *     Generic block device close primitive.
154 */
155rtems_device_driver
156rtems_blkdev_generic_close(
157    rtems_device_major_number major RTEMS_UNUSED,
158    rtems_device_minor_number minor RTEMS_UNUSED,
159    void                    * arg)
160{
161  rtems_libio_open_close_args_t *oc = arg;
162  rtems_libio_t *iop = oc->iop;
163  rtems_disk_device *dd = iop->data1;
164
165  rtems_disk_release(dd);
166
167  return RTEMS_SUCCESSFUL;
168}
169
170/* blkdev_generic_ioctl --
171 *     Generic block device ioctl primitive.
172 */
173rtems_device_driver
174rtems_blkdev_generic_ioctl(
175    rtems_device_major_number major RTEMS_UNUSED,
176    rtems_device_minor_number minor RTEMS_UNUSED,
177    void                    * arg)
178{
179    rtems_libio_ioctl_args_t *args = arg;
180    rtems_libio_t *iop = args->iop;
181    rtems_disk_device *dd = iop->data1;
182
183    if (args->command != RTEMS_BLKIO_REQUEST)
184    {
185        args->ioctl_return = dd->ioctl(dd,
186                                                  args->command,
187                                                  args->buffer);
188    }
189    else
190    {
191        /*
192         * It is not allowed to directly access the driver circumventing the
193         * cache.
194         */
195        args->ioctl_return = -1;
196    }
197
198    return RTEMS_SUCCESSFUL;
199}
Note: See TracBrowser for help on using the repository browser.