device.c 11.2 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2007-01-21     Bernard      the first version
B
bernard.xiong 已提交
9
 * 2010-05-04     Bernard      add rt_device_init implementation
B
Bernard Xiong 已提交
10
 * 2012-10-20     Bernard      add device check in register function,
11
 *                             provided by Rob <rdent@iinet.net.au>
12
 * 2012-12-25     Bernard      return RT_EOK if the device interface not exist.
G
Grissiom 已提交
13
 * 2013-07-09     Grissiom     add ref_count support
14
 * 2016-04-02     Bernard      fix the open_flag initialization issue.
15 16 17
 */

#include <rtthread.h>
18 19 20
#if defined(RT_USING_POSIX)
#include <rtdevice.h> /* for wqueue_init */
#endif
21 22 23

#ifdef RT_USING_DEVICE

B
Bernard Xiong 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#ifdef RT_USING_DEVICE_OPS
#define device_init     (dev->ops->init)
#define device_open     (dev->ops->open)
#define device_close    (dev->ops->close)
#define device_read     (dev->ops->read)
#define device_write    (dev->ops->write)
#define device_control  (dev->ops->control)
#else
#define device_init     (dev->init)
#define device_open     (dev->open)
#define device_close    (dev->close)
#define device_read     (dev->read)
#define device_write    (dev->write)
#define device_control  (dev->control)
#endif

40 41 42 43 44
/**
 * This function registers a device driver with specified name.
 *
 * @param dev the pointer of device driver structure
 * @param name the device driver's name
B
Bernard Xiong 已提交
45
 * @param flags the capabilities flag of device
46 47 48
 *
 * @return the error code, RT_EOK on initialization successfully.
 */
49 50 51
rt_err_t rt_device_register(rt_device_t dev,
                            const char *name,
                            rt_uint16_t flags)
52
{
53 54
    if (dev == RT_NULL)
        return -RT_ERROR;
55

56 57
    if (rt_device_find(name) != RT_NULL)
        return -RT_ERROR;
58

59 60
    rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
    dev->flag = flags;
G
Grissiom 已提交
61
    dev->ref_count = 0;
62
    dev->open_flag = 0;
63

B
bernard 已提交
64
#if defined(RT_USING_POSIX)
B
bernard 已提交
65
    dev->fops = RT_NULL;
66
    rt_wqueue_init(&(dev->wait_queue));
B
bernard 已提交
67 68
#endif

69
    return RT_EOK;
70
}
71
RTM_EXPORT(rt_device_register);
72 73

/**
B
bernard.xiong@gmail.com 已提交
74
 * This function removes a previously registered device driver
75 76 77 78 79 80 81
 *
 * @param dev the pointer of device driver structure
 *
 * @return the error code, RT_EOK on successfully.
 */
rt_err_t rt_device_unregister(rt_device_t dev)
{
82
    RT_ASSERT(dev != RT_NULL);
83 84
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
    RT_ASSERT(rt_object_is_systemobject(&dev->parent));
85

86
    rt_object_detach(&(dev->parent));
87

88
    return RT_EOK;
89
}
90
RTM_EXPORT(rt_device_unregister);
91 92 93 94 95

/**
 * This function initializes all registered device driver
 *
 * @return the error code, RT_EOK on successfully.
96
 *
97
 * @deprecated since 1.2.x, this function is not needed because the initialization
B
Bernard Xiong 已提交
98
 *             of a device is performed when application opens it.
99
 */
D
dzzxzz 已提交
100
rt_err_t rt_device_init_all(void)
101
{
102
    return RT_EOK;
103 104 105 106 107 108 109 110 111
}

/**
 * This function finds a device driver by specified name.
 *
 * @param name the device driver's name
 *
 * @return the registered device driver on successful, or RT_NULL on failure.
 */
D
dzzxzz 已提交
112
rt_device_t rt_device_find(const char *name)
113
{
114
    return (rt_device_t)rt_object_find(name, RT_Object_Class_Device);
B
bernard.xiong 已提交
115
}
116
RTM_EXPORT(rt_device_find);
B
bernard.xiong 已提交
117

118 119 120 121 122 123 124 125 126 127 128
#ifdef RT_USING_HEAP
/**
 * This function creates a device object with user data size.
 *
 * @param type, the kind type of this device object.
 * @param attach_size, the size of user data.
 *
 * @return the allocated device object, or RT_NULL when failed.
 */
rt_device_t rt_device_create(int type, int attach_size)
{
B
BernardXiong 已提交
129 130
    int size;
    rt_device_t device;
131

B
BernardXiong 已提交
132
    size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
133
    attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
134
    /* use the total size */
B
BernardXiong 已提交
135
    size += attach_size;
136

B
BernardXiong 已提交
137 138 139 140 141 142
    device = (rt_device_t)rt_malloc(size);
    if (device)
    {
        rt_memset(device, 0x0, sizeof(struct rt_device));
        device->type = (enum rt_device_class_type)type;
    }
143

B
BernardXiong 已提交
144
    return device;
145 146 147 148 149 150
}
RTM_EXPORT(rt_device_create);

/**
 * This function destroy the specific device object.
 *
151
 * @param dev, the specific device object.
152
 */
153
void rt_device_destroy(rt_device_t dev)
154
{
155 156 157 158 159
    RT_ASSERT(dev != RT_NULL);
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
    RT_ASSERT(rt_object_is_systemobject(&dev->parent) == RT_FALSE);

    rt_object_detach(&(dev->parent));
160

B
BernardXiong 已提交
161
    /* release this device object */
162
    rt_free(dev);
163 164 165 166
}
RTM_EXPORT(rt_device_destroy);
#endif

B
bernard.xiong 已提交
167
/**
B
bernard.xiong@gmail.com 已提交
168
 * This function will initialize the specified device
B
bernard.xiong 已提交
169 170
 *
 * @param dev the pointer of device driver structure
B
Bernard Xiong 已提交
171
 *
B
bernard.xiong 已提交
172 173 174 175
 * @return the result
 */
rt_err_t rt_device_init(rt_device_t dev)
{
176
    rt_err_t result = RT_EOK;
177

178 179
    RT_ASSERT(dev != RT_NULL);

B
Bernard Xiong 已提交
180
    /* get device_init handler */
B
Bernard Xiong 已提交
181
    if (device_init != RT_NULL)
182 183 184
    {
        if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
        {
B
Bernard Xiong 已提交
185
            result = device_init(dev);
186 187 188 189 190 191 192 193 194 195 196 197 198
            if (result != RT_EOK)
            {
                rt_kprintf("To initialize device:%s failed. The error code is %d\n",
                           dev->parent.name, result);
            }
            else
            {
                dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
            }
        }
    }

    return result;
199 200 201 202 203 204
}

/**
 * This function will open a device
 *
 * @param dev the pointer of device driver structure
B
bernard.xiong@gmail.com 已提交
205
 * @param oflag the flags for device open
206 207 208 209 210
 *
 * @return the result
 */
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
211
    rt_err_t result = RT_EOK;
212 213

    RT_ASSERT(dev != RT_NULL);
214
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
215 216 217 218

    /* if device is not initialized, initialize it. */
    if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
    {
B
Bernard Xiong 已提交
219
        if (device_init != RT_NULL)
220
        {
B
Bernard Xiong 已提交
221
            result = device_init(dev);
222 223 224 225 226 227 228 229 230 231 232 233 234
            if (result != RT_EOK)
            {
                rt_kprintf("To initialize device:%s failed. The error code is %d\n",
                           dev->parent.name, result);

                return result;
            }
        }

        dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
    }

    /* device is a stand alone device and opened */
235 236 237
    if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
        (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
    {
238
        return -RT_EBUSY;
239
    }
240

B
Bernard Xiong 已提交
241
    /* call device_open interface */
B
Bernard Xiong 已提交
242
    if (device_open != RT_NULL)
243
    {
B
Bernard Xiong 已提交
244
        result = device_open(dev, oflag);
245
    }
B
bernard 已提交
246 247 248 249 250
    else
    {
        /* set open flag */
        dev->open_flag = (oflag & RT_DEVICE_OFLAG_MASK);
    }
251 252 253

    /* set open flag */
    if (result == RT_EOK || result == -RT_ENOSYS)
254
    {
B
bernard 已提交
255
        dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
256 257 258 259 260 261

        dev->ref_count++;
        /* don't let bad things happen silently. If you are bitten by this assert,
         * please set the ref_count to a bigger type. */
        RT_ASSERT(dev->ref_count != 0);
    }
262 263

    return result;
264
}
265
RTM_EXPORT(rt_device_open);
266 267 268 269 270 271 272 273 274 275

/**
 * This function will close a device
 *
 * @param dev the pointer of device driver structure
 *
 * @return the result
 */
rt_err_t rt_device_close(rt_device_t dev)
{
276
    rt_err_t result = RT_EOK;
277 278

    RT_ASSERT(dev != RT_NULL);
279
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
280

G
Grissiom 已提交
281 282 283 284 285 286 287 288
    if (dev->ref_count == 0)
        return -RT_ERROR;

    dev->ref_count--;

    if (dev->ref_count != 0)
        return RT_EOK;

B
Bernard Xiong 已提交
289
    /* call device_close interface */
B
Bernard Xiong 已提交
290
    if (device_close != RT_NULL)
291
    {
B
Bernard Xiong 已提交
292
        result = device_close(dev);
293 294 295 296 297 298 299
    }

    /* set open flag */
    if (result == RT_EOK || result == -RT_ENOSYS)
        dev->open_flag = RT_DEVICE_OFLAG_CLOSE;

    return result;
300
}
301
RTM_EXPORT(rt_device_close);
302 303 304 305 306 307 308 309 310 311

/**
 * This function will read some data from a device.
 *
 * @param dev the pointer of device driver structure
 * @param pos the position of reading
 * @param buffer the data buffer to save read data
 * @param size the size of buffer
 *
 * @return the actually read size on successful, otherwise negative returned.
B
bernard.xiong@gmail.com 已提交
312 313
 *
 * @note since 0.4.0, the unit of size/pos is a block for block device.
314
 */
315 316 317 318
rt_size_t rt_device_read(rt_device_t dev,
                         rt_off_t    pos,
                         void       *buffer,
                         rt_size_t   size)
319
{
320
    RT_ASSERT(dev != RT_NULL);
321
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
322

G
Grissiom 已提交
323 324 325 326 327 328
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

B
Bernard Xiong 已提交
329
    /* call device_read interface */
B
Bernard Xiong 已提交
330
    if (device_read != RT_NULL)
331
    {
B
Bernard Xiong 已提交
332
        return device_read(dev, pos, buffer, size);
333
    }
334

335 336
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
337

338
    return 0;
339
}
340
RTM_EXPORT(rt_device_read);
341 342 343 344 345 346 347 348 349 350

/**
 * This function will write some data to a device.
 *
 * @param dev the pointer of device driver structure
 * @param pos the position of written
 * @param buffer the data buffer to be written to device
 * @param size the size of buffer
 *
 * @return the actually written size on successful, otherwise negative returned.
B
bernard.xiong@gmail.com 已提交
351 352
 *
 * @note since 0.4.0, the unit of size/pos is a block for block device.
353
 */
354 355 356 357
rt_size_t rt_device_write(rt_device_t dev,
                          rt_off_t    pos,
                          const void *buffer,
                          rt_size_t   size)
358
{
359
    RT_ASSERT(dev != RT_NULL);
360
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
361

G
Grissiom 已提交
362 363 364 365 366 367
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

B
Bernard Xiong 已提交
368
    /* call device_write interface */
B
Bernard Xiong 已提交
369
    if (device_write != RT_NULL)
370
    {
B
Bernard Xiong 已提交
371
        return device_write(dev, pos, buffer, size);
372
    }
373

374 375
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
376

377
    return 0;
378
}
379
RTM_EXPORT(rt_device_write);
380 381 382 383 384 385 386 387 388 389

/**
 * This function will perform a variety of control functions on devices.
 *
 * @param dev the pointer of device driver structure
 * @param cmd the command sent to device
 * @param arg the argument of command
 *
 * @return the result
 */
B
bernard 已提交
390
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
391
{
392
    RT_ASSERT(dev != RT_NULL);
393
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
394

B
Bernard Xiong 已提交
395
    /* call device_write interface */
B
Bernard Xiong 已提交
396
    if (device_control != RT_NULL)
397
    {
B
Bernard Xiong 已提交
398
        return device_control(dev, cmd, arg);
399
    }
400

B
bernard 已提交
401
    return -RT_ENOSYS;
402
}
403
RTM_EXPORT(rt_device_control);
404

B
bernard.xiong@gmail.com 已提交
405
/**
B
Bernard Xiong 已提交
406 407
 * This function will set the reception indication callback function. This callback function
 * is invoked when this device receives data.
B
bernard.xiong@gmail.com 已提交
408 409 410 411 412 413
 *
 * @param dev the pointer of device driver structure
 * @param rx_ind the indication callback function
 *
 * @return RT_EOK
 */
414 415 416
rt_err_t
rt_device_set_rx_indicate(rt_device_t dev,
                          rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
417
{
418
    RT_ASSERT(dev != RT_NULL);
419
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
420

421
    dev->rx_indicate = rx_ind;
D
dzzxzz 已提交
422

423
    return RT_EOK;
424
}
425
RTM_EXPORT(rt_device_set_rx_indicate);
426

B
bernard.xiong@gmail.com 已提交
427
/**
B
Bernard Xiong 已提交
428
 * This function will set the indication callback function when device has
429
 * written data to physical hardware.
B
bernard.xiong@gmail.com 已提交
430 431 432 433 434 435
 *
 * @param dev the pointer of device driver structure
 * @param tx_done the indication callback function
 *
 * @return RT_EOK
 */
436 437 438
rt_err_t
rt_device_set_tx_complete(rt_device_t dev,
                          rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
439
{
440
    RT_ASSERT(dev != RT_NULL);
441
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
442

443
    dev->tx_complete = tx_done;
D
dzzxzz 已提交
444

445
    return RT_EOK;
446
}
447
RTM_EXPORT(rt_device_set_tx_complete);
448 449

#endif