device.c 12.0 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
98
 *             of a device is performed when applicaiton 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 115 116 117 118 119 120 121 122
    struct rt_object *object;
    struct rt_list_node *node;
    struct rt_object_information *information;

    /* enter critical */
    if (rt_thread_self() != RT_NULL)
        rt_enter_critical();

    /* try to find device object */
123 124
    information = rt_object_get_information(RT_Object_Class_Device);
    RT_ASSERT(information != RT_NULL);
125 126 127
    for (node  = information->object_list.next;
         node != &(information->object_list);
         node  = node->next)
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
    {
        object = rt_list_entry(node, struct rt_object, list);
        if (rt_strncmp(object->name, name, RT_NAME_MAX) == 0)
        {
            /* leave critical */
            if (rt_thread_self() != RT_NULL)
                rt_exit_critical();

            return (rt_device_t)object;
        }
    }

    /* leave critical */
    if (rt_thread_self() != RT_NULL)
        rt_exit_critical();

    /* not found */
    return RT_NULL;
B
bernard.xiong 已提交
146
}
147
RTM_EXPORT(rt_device_find);
B
bernard.xiong 已提交
148

149 150 151 152 153 154 155 156 157 158 159
#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 已提交
160 161
    int size;
    rt_device_t device;
162

B
BernardXiong 已提交
163
    size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
164 165
    attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
    /* use the totoal size */
B
BernardXiong 已提交
166
    size += attach_size;
167

B
BernardXiong 已提交
168 169 170 171 172 173
    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;
    }
174

B
BernardXiong 已提交
175
    return device;
176 177 178 179 180 181
}
RTM_EXPORT(rt_device_create);

/**
 * This function destroy the specific device object.
 *
182
 * @param dev, the specific device object.
183
 */
184
void rt_device_destroy(rt_device_t dev)
185
{
186 187 188 189 190
    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));
191

B
BernardXiong 已提交
192
    /* release this device object */
193
    rt_free(dev);
194 195 196 197
}
RTM_EXPORT(rt_device_destroy);
#endif

B
bernard.xiong 已提交
198
/**
B
bernard.xiong@gmail.com 已提交
199
 * This function will initialize the specified device
B
bernard.xiong 已提交
200 201
 *
 * @param dev the pointer of device driver structure
B
Bernard Xiong 已提交
202
 *
B
bernard.xiong 已提交
203 204 205 206
 * @return the result
 */
rt_err_t rt_device_init(rt_device_t dev)
{
207
    rt_err_t result = RT_EOK;
208

209 210 211
    RT_ASSERT(dev != RT_NULL);

    /* get device init handler */
B
Bernard Xiong 已提交
212
    if (device_init != RT_NULL)
213 214 215
    {
        if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
        {
B
Bernard Xiong 已提交
216
            result = device_init(dev);
217 218 219 220 221 222 223 224 225 226 227 228 229
            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;
230 231 232 233 234 235
}

/**
 * This function will open a device
 *
 * @param dev the pointer of device driver structure
B
bernard.xiong@gmail.com 已提交
236
 * @param oflag the flags for device open
237 238 239 240 241
 *
 * @return the result
 */
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
242
    rt_err_t result = RT_EOK;
243 244

    RT_ASSERT(dev != RT_NULL);
245
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
246 247 248 249

    /* if device is not initialized, initialize it. */
    if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
    {
B
Bernard Xiong 已提交
250
        if (device_init != RT_NULL)
251
        {
B
Bernard Xiong 已提交
252
            result = device_init(dev);
253 254 255 256 257 258 259 260 261 262 263 264 265
            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 */
266 267 268
    if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
        (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
    {
269
        return -RT_EBUSY;
270
    }
271 272

    /* call device open interface */
B
Bernard Xiong 已提交
273
    if (device_open != RT_NULL)
274
    {
B
Bernard Xiong 已提交
275
        result = device_open(dev, oflag);
276
    }
B
bernard 已提交
277 278 279 280 281
    else
    {
        /* set open flag */
        dev->open_flag = (oflag & RT_DEVICE_OFLAG_MASK);
    }
282 283 284

    /* set open flag */
    if (result == RT_EOK || result == -RT_ENOSYS)
285
    {
B
bernard 已提交
286
        dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
287 288 289 290 291 292

        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);
    }
293 294

    return result;
295
}
296
RTM_EXPORT(rt_device_open);
297 298 299 300 301 302 303 304 305 306

/**
 * 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)
{
307
    rt_err_t result = RT_EOK;
308 309

    RT_ASSERT(dev != RT_NULL);
310
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
311

G
Grissiom 已提交
312 313 314 315 316 317 318 319
    if (dev->ref_count == 0)
        return -RT_ERROR;

    dev->ref_count--;

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

320
    /* call device close interface */
B
Bernard Xiong 已提交
321
    if (device_close != RT_NULL)
322
    {
B
Bernard Xiong 已提交
323
        result = device_close(dev);
324 325 326 327 328 329 330
    }

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

    return result;
331
}
332
RTM_EXPORT(rt_device_close);
333 334 335 336 337 338 339 340 341 342

/**
 * 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 已提交
343 344
 *
 * @note since 0.4.0, the unit of size/pos is a block for block device.
345
 */
346 347 348 349
rt_size_t rt_device_read(rt_device_t dev,
                         rt_off_t    pos,
                         void       *buffer,
                         rt_size_t   size)
350
{
351
    RT_ASSERT(dev != RT_NULL);
352
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
353

G
Grissiom 已提交
354 355 356 357 358 359
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

360
    /* call device read interface */
B
Bernard Xiong 已提交
361
    if (device_read != RT_NULL)
362
    {
B
Bernard Xiong 已提交
363
        return device_read(dev, pos, buffer, size);
364
    }
365

366 367
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
368

369
    return 0;
370
}
371
RTM_EXPORT(rt_device_read);
372 373 374 375 376 377 378 379 380 381

/**
 * 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 已提交
382 383
 *
 * @note since 0.4.0, the unit of size/pos is a block for block device.
384
 */
385 386 387 388
rt_size_t rt_device_write(rt_device_t dev,
                          rt_off_t    pos,
                          const void *buffer,
                          rt_size_t   size)
389
{
390
    RT_ASSERT(dev != RT_NULL);
391
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
392

G
Grissiom 已提交
393 394 395 396 397 398
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

399
    /* call device write interface */
B
Bernard Xiong 已提交
400
    if (device_write != RT_NULL)
401
    {
B
Bernard Xiong 已提交
402
        return device_write(dev, pos, buffer, size);
403
    }
404

405 406
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
407

408
    return 0;
409
}
410
RTM_EXPORT(rt_device_write);
411 412 413 414 415 416 417 418 419 420

/**
 * 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 已提交
421
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
422
{
423
    RT_ASSERT(dev != RT_NULL);
424
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
425

426
    /* call device write interface */
B
Bernard Xiong 已提交
427
    if (device_control != RT_NULL)
428
    {
B
Bernard Xiong 已提交
429
        return device_control(dev, cmd, arg);
430
    }
431

B
bernard 已提交
432
    return -RT_ENOSYS;
433
}
434
RTM_EXPORT(rt_device_control);
435

B
bernard.xiong@gmail.com 已提交
436
/**
B
Bernard Xiong 已提交
437 438
 * This function will set the reception indication callback function. This callback function
 * is invoked when this device receives data.
B
bernard.xiong@gmail.com 已提交
439 440 441 442 443 444
 *
 * @param dev the pointer of device driver structure
 * @param rx_ind the indication callback function
 *
 * @return RT_EOK
 */
445 446 447
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))
448
{
449
    RT_ASSERT(dev != RT_NULL);
450
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
451

452
    dev->rx_indicate = rx_ind;
D
dzzxzz 已提交
453

454
    return RT_EOK;
455
}
456
RTM_EXPORT(rt_device_set_rx_indicate);
457

B
bernard.xiong@gmail.com 已提交
458
/**
B
Bernard Xiong 已提交
459
 * This function will set the indication callback function when device has
460
 * written data to physical hardware.
B
bernard.xiong@gmail.com 已提交
461 462 463 464 465 466
 *
 * @param dev the pointer of device driver structure
 * @param tx_done the indication callback function
 *
 * @return RT_EOK
 */
467 468 469
rt_err_t
rt_device_set_tx_complete(rt_device_t dev,
                          rt_err_t (*tx_done)(rt_device_t dev, void *buffer))
470
{
471
    RT_ASSERT(dev != RT_NULL);
472
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
473

474
    dev->tx_complete = tx_done;
D
dzzxzz 已提交
475

476
    return RT_EOK;
477
}
478
RTM_EXPORT(rt_device_set_tx_complete);
479 480

#endif