device.c 12.0 KB
Newer Older
1 2 3
/*
 * File      : device.c
 * This file is part of RT-Thread RTOS
G
Grissiom 已提交
4
 * COPYRIGHT (C) 2006 - 2013, RT-Thread Development Team
5
 *
B
Bernard Xiong 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22
 *
 * Change Logs:
 * Date           Author       Notes
 * 2007-01-21     Bernard      the first version
B
bernard.xiong 已提交
23
 * 2010-05-04     Bernard      add rt_device_init implementation
B
Bernard Xiong 已提交
24
 * 2012-10-20     Bernard      add device check in register function,
25
 *                             provided by Rob <rdent@iinet.net.au>
26
 * 2012-12-25     Bernard      return RT_EOK if the device interface not exist.
G
Grissiom 已提交
27
 * 2013-07-09     Grissiom     add ref_count support
28
 * 2016-04-02     Bernard      fix the open_flag initialization issue.
29 30 31
 */

#include <rtthread.h>
32 33 34
#if defined(RT_USING_POSIX)
#include <rtdevice.h> /* for wqueue_init */
#endif
35 36 37

#ifdef RT_USING_DEVICE

B
Bernard Xiong 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#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

54 55 56 57 58
/**
 * 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 已提交
59
 * @param flags the capabilities flag of device
60 61 62
 *
 * @return the error code, RT_EOK on initialization successfully.
 */
63 64 65
rt_err_t rt_device_register(rt_device_t dev,
                            const char *name,
                            rt_uint16_t flags)
66
{
67 68
    if (dev == RT_NULL)
        return -RT_ERROR;
69

70 71
    if (rt_device_find(name) != RT_NULL)
        return -RT_ERROR;
72

73 74
    rt_object_init(&(dev->parent), RT_Object_Class_Device, name);
    dev->flag = flags;
G
Grissiom 已提交
75
    dev->ref_count = 0;
76
    dev->open_flag = 0;
77

B
bernard 已提交
78
#if defined(RT_USING_POSIX)
B
bernard 已提交
79
    dev->fops = RT_NULL;
80
    rt_wqueue_init(&(dev->wait_queue));
B
bernard 已提交
81 82
#endif

83
    return RT_EOK;
84
}
85
RTM_EXPORT(rt_device_register);
86 87

/**
B
bernard.xiong@gmail.com 已提交
88
 * This function removes a previously registered device driver
89 90 91 92 93 94 95
 *
 * @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)
{
96
    RT_ASSERT(dev != RT_NULL);
97

98
    rt_object_detach(&(dev->parent));
99

100
    return RT_EOK;
101
}
102
RTM_EXPORT(rt_device_unregister);
103 104 105 106 107

/**
 * This function initializes all registered device driver
 *
 * @return the error code, RT_EOK on successfully.
108
 *
109
 * @deprecated since 1.2.x, this function is not needed because the initialization
110
 *             of a device is performed when applicaiton opens it.
111
 */
D
dzzxzz 已提交
112
rt_err_t rt_device_init_all(void)
113
{
114
    return RT_EOK;
115 116 117 118 119 120 121 122 123
}

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

161 162 163 164 165 166 167 168 169 170 171
#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 已提交
172 173
    int size;
    rt_device_t device;
174

B
BernardXiong 已提交
175
    size = RT_ALIGN(sizeof(struct rt_device), RT_ALIGN_SIZE);
B
Bernard Xiong 已提交
176 177
    attach_size = RT_ALIGN(attach_size, RT_ALIGN_SIZE);
    /* use the totoal size */
B
BernardXiong 已提交
178
    size += attach_size;
179

B
BernardXiong 已提交
180 181 182 183 184 185
    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;
    }
186

B
BernardXiong 已提交
187
    return device;
188 189 190 191 192 193 194 195 196 197
}
RTM_EXPORT(rt_device_create);

/**
 * This function destroy the specific device object.
 *
 * @param device, the specific device object.
 */
void rt_device_destroy(rt_device_t device)
{
B
BernardXiong 已提交
198 199
    /* unregister device firstly */
    rt_device_unregister(device);
200

B
BernardXiong 已提交
201 202
    /* release this device object */
    rt_free(device);
203 204 205 206
}
RTM_EXPORT(rt_device_destroy);
#endif

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

218 219 220
    RT_ASSERT(dev != RT_NULL);

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

/**
 * This function will open a device
 *
 * @param dev the pointer of device driver structure
B
bernard.xiong@gmail.com 已提交
245
 * @param oflag the flags for device open
246 247 248 249 250
 *
 * @return the result
 */
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{
251
    rt_err_t result = RT_EOK;
252 253 254 255 256 257

    RT_ASSERT(dev != RT_NULL);

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

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

    /* set open flag */
    if (result == RT_EOK || result == -RT_ENOSYS)
293
    {
B
bernard 已提交
294
        dev->open_flag |= RT_DEVICE_OFLAG_OPEN;
295 296 297 298 299 300

        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);
    }
301 302

    return result;
303
}
304
RTM_EXPORT(rt_device_open);
305 306 307 308 309 310 311 312 313 314

/**
 * 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)
{
315
    rt_err_t result = RT_EOK;
316 317 318

    RT_ASSERT(dev != RT_NULL);

G
Grissiom 已提交
319 320 321 322 323 324 325 326
    if (dev->ref_count == 0)
        return -RT_ERROR;

    dev->ref_count--;

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

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

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

    return result;
338
}
339
RTM_EXPORT(rt_device_close);
340 341 342 343 344 345 346 347 348 349

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

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

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

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

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

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

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

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

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

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

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

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

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

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

476
    dev->tx_complete = tx_done;
D
dzzxzz 已提交
477

478
    return RT_EOK;
479
}
480
RTM_EXPORT(rt_device_set_tx_complete);
481 482

#endif