[c4c6025] | 1 | /*
|
---|
| 2 | * Copyright (c) 2017 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | #include <async.h>
|
---|
| 30 | #include <errno.h>
|
---|
| 31 | #include <io/serial.h>
|
---|
| 32 | #include <ipc/serial_ctl.h>
|
---|
| 33 | #include <ipc/services.h>
|
---|
| 34 | #include <stdlib.h>
|
---|
| 35 |
|
---|
| 36 | /** Open serial port device.
|
---|
| 37 | *
|
---|
| 38 | * @param sess Session with the serial port device
|
---|
| 39 | * @param rchardev Place to store pointer to the new serial port device structure
|
---|
| 40 | *
|
---|
| 41 | * @return EOK on success, ENOMEM if out of memory, EIO on I/O error
|
---|
| 42 | */
|
---|
[b7fd2a0] | 43 | errno_t serial_open(async_sess_t *sess, serial_t **rserial)
|
---|
[c4c6025] | 44 | {
|
---|
| 45 | serial_t *serial;
|
---|
| 46 |
|
---|
| 47 | serial = calloc(1, sizeof(serial_t));
|
---|
| 48 | if (serial == NULL)
|
---|
| 49 | return ENOMEM;
|
---|
| 50 |
|
---|
| 51 | serial->sess = sess;
|
---|
| 52 | *rserial = serial;
|
---|
| 53 |
|
---|
| 54 | return EOK;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /** Close serial port device.
|
---|
| 58 | *
|
---|
| 59 | * Frees the serial port device structure. The underlying session is
|
---|
| 60 | * not affected.
|
---|
| 61 | *
|
---|
| 62 | * @param serial Serial port device or @c NULL
|
---|
| 63 | */
|
---|
| 64 | void serial_close(serial_t *serial)
|
---|
| 65 | {
|
---|
| 66 | free(serial);
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | /** Set serial port communication properties. */
|
---|
[b7fd2a0] | 70 | errno_t serial_set_comm_props(serial_t *serial, unsigned rate,
|
---|
[c4c6025] | 71 | serial_parity_t parity, unsigned datab, unsigned stopb)
|
---|
| 72 | {
|
---|
| 73 | async_exch_t *exch = async_exchange_begin(serial->sess);
|
---|
| 74 |
|
---|
[b7fd2a0] | 75 | errno_t rc = async_req_4_0(exch, SERIAL_SET_COM_PROPS, rate, parity,
|
---|
[c4c6025] | 76 | datab, stopb);
|
---|
| 77 |
|
---|
| 78 | async_exchange_end(exch);
|
---|
| 79 | return rc;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | /** Get serial port communication properties. */
|
---|
[b7fd2a0] | 83 | errno_t serial_get_comm_props(serial_t *serial, unsigned *rrate,
|
---|
[c4c6025] | 84 | serial_parity_t *rparity, unsigned *rdatab, unsigned *rstopb)
|
---|
| 85 | {
|
---|
| 86 | async_exch_t *exch = async_exchange_begin(serial->sess);
|
---|
| 87 | sysarg_t rate, parity, datab, stopb;
|
---|
| 88 |
|
---|
[b7fd2a0] | 89 | errno_t rc = async_req_0_4(exch, SERIAL_GET_COM_PROPS, &rate, &parity,
|
---|
[c4c6025] | 90 | &datab, &stopb);
|
---|
| 91 |
|
---|
| 92 | async_exchange_end(exch);
|
---|
| 93 | if (rc != EOK)
|
---|
| 94 | return rc;
|
---|
| 95 |
|
---|
| 96 | *rrate = rate;
|
---|
| 97 | *rparity = parity;
|
---|
| 98 | *rdatab = datab;
|
---|
| 99 | *rstopb = stopb;
|
---|
| 100 |
|
---|
| 101 | return EOK;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /** @}
|
---|
| 105 | */
|
---|