device.c 12.1 KB
Newer Older
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
B
Bernard Xiong 已提交
3
 *
4 5 6 7 8
 * SPDX-License-Identifier: Apache-2.0
 */

/*
 * File      : device.c
9 10 11 12
 *
 * Change Logs:
 * Date           Author       Notes
 * 2007-01-21     Bernard      the first version
B
bernard.xiong 已提交
13
 * 2010-05-04     Bernard      add rt_device_init implementation
B
Bernard Xiong 已提交
14
 * 2012-10-20     Bernard      add device check in register function,
15
 *                             provided by Rob <rdent@iinet.net.au>
16
 * 2012-12-25     Bernard      return RT_EOK if the device interface not exist.
G
Grissiom 已提交
17
 * 2013-07-09     Grissiom     add ref_count support
18
 * 2016-04-02     Bernard      fix the open_flag initialization issue.
19 20 21
 */

#include <rtthread.h>
22 23 24
#if defined(RT_USING_POSIX)
#include <rtdevice.h> /* for wqueue_init */
#endif
25 26 27

#ifdef RT_USING_DEVICE

B
Bernard Xiong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#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

44 45 46 47 48
/**
 * 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 已提交
49
 * @param flags the capabilities flag of device
50 51 52
 *
 * @return the error code, RT_EOK on initialization successfully.
 */
53 54 55
rt_err_t rt_device_register(rt_device_t dev,
                            const char *name,
                            rt_uint16_t flags)
56
{
57 58
    if (dev == RT_NULL)
        return -RT_ERROR;
59

60 61
    if (rt_device_find(name) != RT_NULL)
        return -RT_ERROR;
62

63 64
    rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
    dev->flag = flags;
G
Grissiom 已提交
65
    dev->ref_count = 0;
66
    dev->open_flag = 0;
67

B
bernard 已提交
68
#if defined(RT_USING_POSIX)
B
bernard 已提交
69
    dev->fops = RT_NULL;
70
    rt_wqueue_init(&(dev->wait_queue));
B
bernard 已提交
71 72
#endif

73
    return RT_EOK;
74
}
75
RTM_EXPORT(rt_device_register);
76 77

/**
B
bernard.xiong@gmail.com 已提交
78
 * This function removes a previously registered device driver
79 80 81 82 83 84 85
 *
 * @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)
{
86
    RT_ASSERT(dev != RT_NULL);
87 88
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
    RT_ASSERT(rt_object_is_systemobject(&dev->parent));
89

90
    rt_object_detach(&(dev->parent));
91

92
    return RT_EOK;
93
}
94
RTM_EXPORT(rt_device_unregister);
95 96 97 98 99

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

/**
 * 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 已提交
116
rt_device_t rt_device_find(const char *name)
117
{
118 119 120 121 122 123 124 125 126
    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 */
127 128
    information = rt_object_get_information(RT_Object_Class_Device);
    RT_ASSERT(information != RT_NULL);
129 130 131
    for (node  = information->object_list.next;
         node != &(information->object_list);
         node  = node->next)
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
    {
        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 已提交
150
}
151
RTM_EXPORT(rt_device_find);
B
bernard.xiong 已提交
152

153 154 155 156 157 158 159 160 161 162 163
#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 已提交
164 165
    int size;
    rt_device_t device;
166

B
BernardXiong 已提交
167
    size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
168 169
    attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
    /* use the totoal size */
B
BernardXiong 已提交
170
    size += attach_size;
171

B
BernardXiong 已提交
172 173 174 175 176 177
    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;
    }
178

B
BernardXiong 已提交
179
    return device;
180 181 182 183 184 185
}
RTM_EXPORT(rt_device_create);

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

B
BernardXiong 已提交
196
    /* release this device object */
197
    rt_free(dev);
198 199 200 201
}
RTM_EXPORT(rt_device_destroy);
#endif

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

213 214 215
    RT_ASSERT(dev != RT_NULL);

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

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

    RT_ASSERT(dev != RT_NULL);
249
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
250 251 252 253

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

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

    /* set open flag */
    if (result == RT_EOK || result == -RT_ENOSYS)
289
    {
B
bernard 已提交
290
        dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
291 292 293 294 295 296

        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);
    }
297 298

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

/**
 * 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)
{
311
    rt_err_t result = RT_EOK;
312 313

    RT_ASSERT(dev != RT_NULL);
314
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
315

G
Grissiom 已提交
316 317 318 319 320 321 322 323
    if (dev->ref_count == 0)
        return -RT_ERROR;

    dev->ref_count--;

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

324
    /* call device close interface */
B
Bernard Xiong 已提交
325
    if (device_close != RT_NULL)
326
    {
B
Bernard Xiong 已提交
327
        result = device_close(dev);
328 329 330 331 332 333 334
    }

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

    return result;
335
}
336
RTM_EXPORT(rt_device_close);
337 338 339 340 341 342 343 344 345 346

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

G
Grissiom 已提交
358 359 360 361 362 363
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

364
    /* call device read interface */
B
Bernard Xiong 已提交
365
    if (device_read != RT_NULL)
366
    {
B
Bernard Xiong 已提交
367
        return device_read(dev, pos, buffer, size);
368
    }
369

370 371
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
372

373
    return 0;
374
}
375
RTM_EXPORT(rt_device_read);
376 377 378 379 380 381 382 383 384 385

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

G
Grissiom 已提交
397 398 399 400 401 402
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

403
    /* call device write interface */
B
Bernard Xiong 已提交
404
    if (device_write != RT_NULL)
405
    {
B
Bernard Xiong 已提交
406
        return device_write(dev, pos, buffer, size);
407
    }
408

409 410
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
411

412
    return 0;
413
}
414
RTM_EXPORT(rt_device_write);
415 416 417 418 419 420 421 422 423 424

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

430
    /* call device write interface */
B
Bernard Xiong 已提交
431
    if (device_control != RT_NULL)
432
    {
B
Bernard Xiong 已提交
433
        return device_control(dev, cmd, arg);
434
    }
435

B
bernard 已提交
436
    return -RT_ENOSYS;
437
}
438
RTM_EXPORT(rt_device_control);
439

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

456
    dev->rx_indicate = rx_ind;
D
dzzxzz 已提交
457

458
    return RT_EOK;
459
}
460
RTM_EXPORT(rt_device_set_rx_indicate);
461

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

478
    dev->tx_complete = tx_done;
D
dzzxzz 已提交
479

480
    return RT_EOK;
481
}
482
RTM_EXPORT(rt_device_set_tx_complete);
483 484

#endif