1 | /*! @file |
---|
2 | * @brief Test disk block device implementation. |
---|
3 | * |
---|
4 | * Copyright (C) 2010 OKTET Labs, St.-Petersburg, Russia |
---|
5 | * Author: Oleg Kravtsov <Oleg.Kravtsov@oktetlabs.ru> |
---|
6 | * |
---|
7 | * The license and distribution terms for this file may be |
---|
8 | * found in the file LICENSE in this distribution or at |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | */ |
---|
11 | |
---|
12 | #if HAVE_CONFIG_H |
---|
13 | #include "config.h" |
---|
14 | #endif |
---|
15 | |
---|
16 | #include <rtems.h> |
---|
17 | #include <rtems/libio.h> |
---|
18 | #include <errno.h> |
---|
19 | #include <stdlib.h> |
---|
20 | #include <stdio.h> |
---|
21 | #include <string.h> |
---|
22 | #include <inttypes.h> |
---|
23 | |
---|
24 | #include "rtems/blkdev.h" |
---|
25 | #include "rtems/diskdevs.h" |
---|
26 | |
---|
27 | #include "bdbuf_tests.h" |
---|
28 | |
---|
29 | static Objects_Id drvq_id = OBJECTS_ID_NONE; |
---|
30 | static Objects_Id testq_id = OBJECTS_ID_NONE; |
---|
31 | |
---|
32 | static int |
---|
33 | test_disk_ioctl(rtems_disk_device *dd, uint32_t req, void *argp) |
---|
34 | { |
---|
35 | rtems_status_code rc; |
---|
36 | bdbuf_test_msg msg; |
---|
37 | size_t msg_size; |
---|
38 | |
---|
39 | switch (req) |
---|
40 | { |
---|
41 | case RTEMS_BLKIO_REQUEST: |
---|
42 | { |
---|
43 | rtems_blkdev_request *r = argp; |
---|
44 | rtems_blkdev_sg_buffer *sg; |
---|
45 | unsigned int i; |
---|
46 | |
---|
47 | printk("DISK_DRV: %s ", |
---|
48 | r->req == RTEMS_BLKDEV_REQ_READ ? "R" : |
---|
49 | r->req == RTEMS_BLKDEV_REQ_WRITE ? "W" : "?"); |
---|
50 | for (i = 0, sg = r->bufs; i < r->bufnum; i++, sg++) |
---|
51 | { |
---|
52 | printk("[%d] ", sg->block); |
---|
53 | } |
---|
54 | printk("\n"); |
---|
55 | break; |
---|
56 | } |
---|
57 | |
---|
58 | default: |
---|
59 | printk("%s() Unexpected request comes %u\n", |
---|
60 | __FUNCTION__, req); |
---|
61 | return -1; |
---|
62 | } |
---|
63 | |
---|
64 | memset(&msg, 0, sizeof(msg)); |
---|
65 | msg.type = BDBUF_TEST_MSG_TYPE_DRIVER_REQ; |
---|
66 | msg.val.driver_req.dd = dd; |
---|
67 | msg.val.driver_req.req = req; |
---|
68 | msg.val.driver_req.argp = argp; |
---|
69 | |
---|
70 | rc = rtems_message_queue_send(testq_id, &msg, sizeof(msg)); |
---|
71 | if (rc != RTEMS_SUCCESSFUL) |
---|
72 | { |
---|
73 | printf("Error while sending a message to Test task: %u\n", rc); |
---|
74 | return -1; |
---|
75 | } |
---|
76 | |
---|
77 | /* Wait for a reply from the test task */ |
---|
78 | msg_size = sizeof(msg); |
---|
79 | rc = rtems_message_queue_receive(drvq_id, &msg, &msg_size, |
---|
80 | RTEMS_WAIT, RTEMS_NO_TIMEOUT); |
---|
81 | if (rc != RTEMS_SUCCESSFUL) |
---|
82 | { |
---|
83 | printf("Error while reading a message from Test task: %u\n", rc); |
---|
84 | return rc; |
---|
85 | } |
---|
86 | if (msg.type != BDBUF_TEST_MSG_TYPE_DRIVER_REPLY) |
---|
87 | { |
---|
88 | printf("Unexpected message comes to test disk driver: %d\n", |
---|
89 | msg.type); |
---|
90 | return -1; |
---|
91 | } |
---|
92 | |
---|
93 | if (msg.val.driver_reply.ret_val != 0) |
---|
94 | { |
---|
95 | errno = msg.val.driver_reply.ret_errno; |
---|
96 | } |
---|
97 | else |
---|
98 | { |
---|
99 | rtems_blkdev_request *r = (rtems_blkdev_request *)argp; |
---|
100 | |
---|
101 | r->req_done(r->done_arg, msg.val.driver_reply.res_status); |
---|
102 | } |
---|
103 | |
---|
104 | return msg.val.driver_reply.ret_val; |
---|
105 | } |
---|
106 | |
---|
107 | rtems_device_driver |
---|
108 | test_disk_initialize( |
---|
109 | rtems_device_major_number major, |
---|
110 | rtems_device_minor_number minor, |
---|
111 | void *arg) |
---|
112 | { |
---|
113 | rtems_status_code rc; |
---|
114 | dev_t dev; |
---|
115 | |
---|
116 | rc = rtems_disk_io_initialize(); |
---|
117 | if (rc != RTEMS_SUCCESSFUL) |
---|
118 | return rc; |
---|
119 | |
---|
120 | dev = rtems_filesystem_make_dev_t(major, minor); |
---|
121 | rc = rtems_disk_create_phys(dev, |
---|
122 | TEST_DISK_BLOCK_SIZE, TEST_DISK_BLOCK_NUM, |
---|
123 | test_disk_ioctl, |
---|
124 | NULL, |
---|
125 | TEST_DISK_NAME); |
---|
126 | if (rc != RTEMS_SUCCESSFUL) |
---|
127 | { |
---|
128 | printf("Failed to create %s disk\n", TEST_DISK_NAME); |
---|
129 | return rc; |
---|
130 | } |
---|
131 | |
---|
132 | rc = bdbuf_test_create_drv_rx_queue(&drvq_id); |
---|
133 | if (rc != RTEMS_SUCCESSFUL) |
---|
134 | { |
---|
135 | printk("%s() Failed to create Msg Queue for RX: %u\n", |
---|
136 | __FUNCTION__, rc); |
---|
137 | return rc; |
---|
138 | } |
---|
139 | |
---|
140 | rc = bdbuf_test_create_drv_tx_queue(&testq_id); |
---|
141 | if (rc != RTEMS_SUCCESSFUL) |
---|
142 | { |
---|
143 | printf("%s() Failed to get Msg Queue for TX: %u\n", |
---|
144 | __FUNCTION__, rc); |
---|
145 | return rc; |
---|
146 | } |
---|
147 | |
---|
148 | printk("TEST DISK - OK\n"); |
---|
149 | return RTEMS_SUCCESSFUL; |
---|
150 | } |
---|
151 | |
---|