device.c 11.7 KB
Newer Older
1
/*
mysterywolf's avatar
mysterywolf 已提交
2
 * Copyright (c) 2006-2021, 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
 * 2021-03-19     Meco Man     remove rt_device_init_all()
16 17 18
 */

#include <rtthread.h>
19
#ifdef RT_USING_POSIX_DEVIO
20
#include <rtdevice.h> /* for wqueue_init */
21
#endif /* RT_USING_POSIX_DEVIO */
22 23 24

#ifdef RT_USING_DEVICE

B
Bernard Xiong 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
#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)
mysterywolf's avatar
mysterywolf 已提交
39
#endif /* RT_USING_DEVICE_OPS */
B
Bernard Xiong 已提交
40

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

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

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

67
#ifdef RT_USING_POSIX_DEVIO
B
bernard 已提交
68
    dev->fops = RT_NULL;
69
    rt_wqueue_init(&(dev->wait_queue));
70
#endif /* RT_USING_POSIX_DEVIO */
B
bernard 已提交
71

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

/**
77
 * @brief This function removes a previously registered device driver.
78
 *
Nameless-Y's avatar
Nameless-Y 已提交
79
 * @param dev is the pointer of device driver structure.
80 81 82 83 84
 *
 * @return the error code, RT_EOK on successfully.
 */
rt_err_t rt_device_unregister(rt_device_t dev)
{
mysterywolf's avatar
mysterywolf 已提交
85
    /* parameter check */
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
 * @brief This function finds a device driver by specified name.
98
 *
Nameless-Y's avatar
Nameless-Y 已提交
99
 * @param name is the device driver's name.
100 101 102
 *
 * @return the registered device driver on successful, or RT_NULL on failure.
 */
D
dzzxzz 已提交
103
rt_device_t rt_device_find(const char *name)
104
{
105
    return (rt_device_t)rt_object_find(name, RT_Object_Class_Device);
B
bernard.xiong 已提交
106
}
107
RTM_EXPORT(rt_device_find);
B
bernard.xiong 已提交
108

109 110
#ifdef RT_USING_HEAP
/**
111
 * @brief This function creates a device object with user data size.
112
 *
Nameless-Y's avatar
Nameless-Y 已提交
113
 * @param type is the type of the device object.
114
 *
Nameless-Y's avatar
Nameless-Y 已提交
115
 * @param attach_size is the size of user data.
116 117 118 119 120
 *
 * @return the allocated device object, or RT_NULL when failed.
 */
rt_device_t rt_device_create(int type, int attach_size)
{
B
BernardXiong 已提交
121 122
    int size;
    rt_device_t device;
123

B
BernardXiong 已提交
124
    size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
125
    attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
126
    /* use the total size */
B
BernardXiong 已提交
127
    size += attach_size;
128

B
BernardXiong 已提交
129 130 131 132 133 134
    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;
    }
135

B
BernardXiong 已提交
136
    return device;
137 138 139 140
}
RTM_EXPORT(rt_device_create);

/**
141
 * @brief This function destroy the specific device object.
142
 *
Nameless-Y's avatar
Nameless-Y 已提交
143
 * @param dev is a specific device object.
144
 */
145
void rt_device_destroy(rt_device_t dev)
146
{
mysterywolf's avatar
mysterywolf 已提交
147
    /* parameter check */
148 149 150 151 152
    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));
153

B
BernardXiong 已提交
154
    /* release this device object */
155
    rt_free(dev);
156 157
}
RTM_EXPORT(rt_device_destroy);
mysterywolf's avatar
mysterywolf 已提交
158
#endif /* RT_USING_HEAP */
159

B
bernard.xiong 已提交
160
/**
161
 * @brief This function will initialize the specified device.
B
bernard.xiong 已提交
162
 *
Nameless-Y's avatar
Nameless-Y 已提交
163
 * @param dev is the pointer of device driver structure.
B
Bernard Xiong 已提交
164
 *
165
 * @return the result, RT_EOK on successfully.
B
bernard.xiong 已提交
166 167 168
 */
rt_err_t rt_device_init(rt_device_t dev)
{
169
    rt_err_t result = RT_EOK;
170

171 172
    RT_ASSERT(dev != RT_NULL);

B
Bernard Xiong 已提交
173
    /* get device_init handler */
B
Bernard Xiong 已提交
174
    if (device_init != RT_NULL)
175 176 177
    {
        if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
        {
B
Bernard Xiong 已提交
178
            result = device_init(dev);
179 180 181 182 183 184 185 186 187 188 189 190 191
            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;
192 193 194
}

/**
195 196
 * @brief This function will open a device.
 *
Nameless-Y's avatar
Nameless-Y 已提交
197
 * @param dev is the pointer of device driver structure.
198
 *
Nameless-Y's avatar
Nameless-Y 已提交
199
 * @param oflag is the flags for device open.
200
 *
201
 * @return the result, RT_EOK on successfully.
202 203 204
 */
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
205
    rt_err_t result = RT_EOK;
206

mysterywolf's avatar
mysterywolf 已提交
207
    /* parameter check */
208
    RT_ASSERT(dev != RT_NULL);
209
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
210 211 212 213

    /* if device is not initialized, initialize it. */
    if (!(dev->flag & RT_DEVICE_FLAG_ACTIVATED))
    {
B
Bernard Xiong 已提交
214
        if (device_init != RT_NULL)
215
        {
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);

                return result;
            }
        }

        dev->flag |= RT_DEVICE_FLAG_ACTIVATED;
    }

    /* device is a stand alone device and opened */
230 231 232
    if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) &&
        (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
    {
233
        return -RT_EBUSY;
234
    }
235

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

    /* set open flag */
    if (result == RT_EOK || result == -RT_ENOSYS)
249
    {
B
bernard 已提交
250
        dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
251 252 253 254 255 256

        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);
    }
257 258

    return result;
259
}
260
RTM_EXPORT(rt_device_open);
261 262

/**
263
 * @brief This function will close a device.
264
 *
Nameless-Y's avatar
Nameless-Y 已提交
265
 * @param dev is the pointer of device driver structure.
266
 *
267
 * @return the result, RT_EOK on successfully.
268 269 270
 */
rt_err_t rt_device_close(rt_device_t dev)
{
271
    rt_err_t result = RT_EOK;
272

mysterywolf's avatar
mysterywolf 已提交
273
    /* parameter check */
274
    RT_ASSERT(dev != RT_NULL);
275
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
276

G
Grissiom 已提交
277 278 279 280 281 282 283 284
    if (dev->ref_count == 0)
        return -RT_ERROR;

    dev->ref_count--;

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

B
Bernard Xiong 已提交
285
    /* call device_close interface */
B
Bernard Xiong 已提交
286
    if (device_close != RT_NULL)
287
    {
B
Bernard Xiong 已提交
288
        result = device_close(dev);
289 290 291 292 293 294 295
    }

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

    return result;
296
}
297
RTM_EXPORT(rt_device_close);
298 299

/**
300 301
 * @brief This function will read some data from a device.
 *
Nameless-Y's avatar
Nameless-Y 已提交
302
 * @param dev is the pointer of device driver structure.
303
 *
Nameless-Y's avatar
Nameless-Y 已提交
304
 * @param pos is the position when reading.
305
 *
Nameless-Y's avatar
Nameless-Y 已提交
306
 * @param buffer is a data buffer to save the read data.
307
 *
Nameless-Y's avatar
Nameless-Y 已提交
308
 * @param size is the size of buffer.
309
 *
J
Jackistang 已提交
310
 * @return the actually read size on successful, otherwise zero returned.
B
bernard.xiong@gmail.com 已提交
311 312
 *
 * @note since 0.4.0, the unit of size/pos is a block for block device.
313
 */
314 315 316 317
rt_size_t rt_device_read(rt_device_t dev,
                         rt_off_t    pos,
                         void       *buffer,
                         rt_size_t   size)
318
{
mysterywolf's avatar
mysterywolf 已提交
319
    /* parameter check */
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
 * @brief This function will write some data to a device.
344
 *
Nameless-Y's avatar
Nameless-Y 已提交
345
 * @param dev is the pointer of device driver structure.
346
 *
Nameless-Y's avatar
Nameless-Y 已提交
347
 * @param pos is the position when writing.
348
 *
Nameless-Y's avatar
Nameless-Y 已提交
349
 * @param buffer is the data buffer to be written to device.
350
 *
Nameless-Y's avatar
Nameless-Y 已提交
351
 * @param size is the size of buffer.
352
 *
J
Jackistang 已提交
353
 * @return the actually written size on successful, otherwise zero returned.
B
bernard.xiong@gmail.com 已提交
354 355
 *
 * @note since 0.4.0, the unit of size/pos is a block for block device.
356
 */
357 358 359 360
rt_size_t rt_device_write(rt_device_t dev,
                          rt_off_t    pos,
                          const void *buffer,
                          rt_size_t   size)
361
{
mysterywolf's avatar
mysterywolf 已提交
362
    /* parameter check */
363
    RT_ASSERT(dev != RT_NULL);
364
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
365

G
Grissiom 已提交
366 367 368 369 370 371
    if (dev->ref_count == 0)
    {
        rt_set_errno(-RT_ERROR);
        return 0;
    }

B
Bernard Xiong 已提交
372
    /* call device_write interface */
B
Bernard Xiong 已提交
373
    if (device_write != RT_NULL)
374
    {
B
Bernard Xiong 已提交
375
        return device_write(dev, pos, buffer, size);
376
    }
377

378 379
    /* set error code */
    rt_set_errno(-RT_ENOSYS);
D
dzzxzz 已提交
380

381
    return 0;
382
}
383
RTM_EXPORT(rt_device_write);
384 385

/**
386 387
 * @brief This function will perform a variety of control functions on devices.
 *
Nameless-Y's avatar
Nameless-Y 已提交
388
 * @param dev is the pointer of device driver structure.
389
 *
Nameless-Y's avatar
Nameless-Y 已提交
390
 * @param cmd is the command sent to device.
391
 *
Nameless-Y's avatar
Nameless-Y 已提交
392
 * @param arg is the argument of command.
393 394
 *
 * @return the result, -RT_ENOSYS for failed.
395
 */
B
bernard 已提交
396
rt_err_t rt_device_control(rt_device_t dev, int cmd, void *arg)
397
{
mysterywolf's avatar
mysterywolf 已提交
398
    /* parameter check */
399
    RT_ASSERT(dev != RT_NULL);
400
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
401

B
Bernard Xiong 已提交
402
    /* call device_write interface */
B
Bernard Xiong 已提交
403
    if (device_control != RT_NULL)
404
    {
B
Bernard Xiong 已提交
405
        return device_control(dev, cmd, arg);
406
    }
407

B
bernard 已提交
408
    return -RT_ENOSYS;
409
}
410
RTM_EXPORT(rt_device_control);
411

B
bernard.xiong@gmail.com 已提交
412
/**
413 414 415
 * @brief This function will set the reception indication callback function. This callback function
 *        is invoked when this device receives data.
 *
Nameless-Y's avatar
Nameless-Y 已提交
416
 * @param dev is the pointer of device driver structure.
B
bernard.xiong@gmail.com 已提交
417
 *
Nameless-Y's avatar
Nameless-Y 已提交
418
 * @param rx_ind is the indication callback function.
B
bernard.xiong@gmail.com 已提交
419
 *
Nameless-Y's avatar
Nameless-Y 已提交
420
 * @return RT_EOK
B
bernard.xiong@gmail.com 已提交
421
 */
mysterywolf's avatar
mysterywolf 已提交
422 423 424
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))
425
{
mysterywolf's avatar
mysterywolf 已提交
426
    /* parameter check */
427
    RT_ASSERT(dev != RT_NULL);
428
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
429

430
    dev->rx_indicate = rx_ind;
D
dzzxzz 已提交
431

432
    return RT_EOK;
433
}
434
RTM_EXPORT(rt_device_set_rx_indicate);
435

B
bernard.xiong@gmail.com 已提交
436
/**
Nameless-Y's avatar
Nameless-Y 已提交
437 438
 * @brief This function will set a callback function. The callback function
 *        will be called when device has written data to physical hardware.
439
 *
Nameless-Y's avatar
Nameless-Y 已提交
440
 * @param dev is the pointer of device driver structure.
B
bernard.xiong@gmail.com 已提交
441
 *
Nameless-Y's avatar
Nameless-Y 已提交
442
 * @param tx_done is the indication callback function.
B
bernard.xiong@gmail.com 已提交
443
 *
Nameless-Y's avatar
Nameless-Y 已提交
444
 * @return RT_EOK
B
bernard.xiong@gmail.com 已提交
445
 */
mysterywolf's avatar
mysterywolf 已提交
446 447 448
rt_err_t rt_device_set_tx_complete(rt_device_t dev,
                                   rt_err_t (*tx_done)(rt_device_t dev,
                                   void *buffer))
449
{
mysterywolf's avatar
mysterywolf 已提交
450
    /* parameter check */
451
    RT_ASSERT(dev != RT_NULL);
452
    RT_ASSERT(rt_object_get_type(&dev->parent) == RT_Object_Class_Device);
453

454
    dev->tx_complete = tx_done;
D
dzzxzz 已提交
455

456
    return RT_EOK;
457
}
458
RTM_EXPORT(rt_device_set_tx_complete);
459

mysterywolf's avatar
mysterywolf 已提交
460
#endif /* RT_USING_DEVICE */