Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset c657bd7 in mainline


Ignore:
Timestamp:
2017-11-20T10:06:59Z (4 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master
Children:
19ea61d
Parents:
5d50c419
git-author:
Jiri Svoboda <jiri@…> (2017-11-19 22:05:26)
git-committer:
Jiri Svoboda <jiri@…> (2017-11-20 10:06:59)
Message:

Less is sometimes more. Need chardev_read to be able to return less bytes than requested if less is available. Otherwise cannot read variable-sized packets except yte-by-byte.

Location:
uspace
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • uspace/drv/char/i8042/buffer.h

    r5d50c419 rc657bd7  
    113113 *
    114114 * @param buffer Cyclic buffer to read from.
     115 * @param rb Place to store byte read
     116 * @param wait @c True to wait until byte is available
    115117 *
    116  * @return Byte read.
     118 * @return EOK on success, EAGAIN if @c wait is false and no data is available
    117119 *
    118120 */
    119 static inline uint8_t buffer_read(buffer_t *buffer)
     121static inline int buffer_read(buffer_t *buffer, uint8_t *rb, bool wait)
    120122{
    121123        fibril_mutex_lock(&buffer->guard);
    122124       
    123125        /* Buffer is empty. */
    124         while (buffer->write_head == buffer->read_head)
     126        while (wait && buffer->write_head == buffer->read_head)
    125127                fibril_condvar_wait(&buffer->change, &buffer->guard);
     128       
     129        if (buffer->write_head == buffer->read_head) {
     130                fibril_mutex_unlock(&buffer->guard);
     131                return EAGAIN;
     132        }
    126133       
    127134        /* Next position. */
     
    144151       
    145152        fibril_mutex_unlock(&buffer->guard);
    146         return data;
     153        *rb = data;
     154        return EOK;
    147155}
    148156
  • uspace/drv/char/i8042/i8042.c

    r5d50c419 rc657bd7  
    374374        i8042_t *i8042 = port->ctl;
    375375        uint8_t *destp = (uint8_t *)dest;
     376        int rc;
     377        size_t i;
    376378       
    377379        buffer_t *buffer = (port == i8042->aux) ?
    378380            &i8042->aux_buffer : &i8042->kbd_buffer;
    379381       
    380         for (size_t i = 0; i < size; ++i)
    381                 *destp++ = buffer_read(buffer);
    382        
    383         return size;
     382        for (i = 0; i < size; ++i) {
     383                rc = buffer_read(buffer, destp, i == 0);
     384                if (rc != EOK)
     385                        break;
     386                ++destp;
     387        }
     388       
     389        return i;
    384390}
    385391
  • uspace/drv/char/pl050/pl050.c

    r5d50c419 rc657bd7  
    246246        left = size;
    247247        while (left > 0) {
    248                 while (pl050->buf_rp == pl050->buf_wp)
     248                while (left == size && pl050->buf_rp == pl050->buf_wp)
    249249                        fibril_condvar_wait(&pl050->buf_cv, &pl050->buf_lock);
     250                if (pl050->buf_rp == pl050->buf_wp)
     251                        break;
    250252                *bp++ = pl050->buffer[pl050->buf_rp];
    251253                --left;
     
    255257        fibril_mutex_unlock(&pl050->buf_lock);
    256258
    257         return size;
     259        return size - left;
    258260}
    259261
  • uspace/drv/hid/ps2mouse/ps2mouse.c

    r5d50c419 rc657bd7  
    212212}
    213213
     214/** Read fixed-size mouse packet.
     215 *
     216 * Continue reading until entire packet is received.
     217 *
     218 * @param mouse Mouse device
     219 * @param pbuf Buffer for storing packet
     220 * @param psize Packet size
     221 *
     222 * @return EOK on success or non-zero error code
     223 */
     224static int ps2_mouse_read_packet(ps2_mouse_t *mouse, void *pbuf, size_t psize)
     225{
     226        int rc;
     227        size_t pos;
     228        size_t nread;
     229
     230        pos = 0;
     231        while (pos < psize) {
     232                rc = chardev_read(mouse->chardev, pbuf + pos, psize - pos,
     233                    &nread);
     234                if (rc != EOK) {
     235                        ddf_msg(LVL_WARN, "Error reading packet.");
     236                        return rc;
     237                }
     238
     239                pos += nread;
     240        }
     241
     242        return EOK;
     243}
     244
    214245/** Get data and parse ps2 protocol packets.
    215246 * @param arg Pointer to ps2_mouse_t structure.
     
    219250{
    220251        ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
    221         size_t nread;
    222252        int rc;
    223253
     
    225255        while (1) {
    226256                uint8_t packet[PS2_BUFSIZE] = {};
    227                 rc = chardev_read(mouse->chardev, packet, PS2_BUFSIZE, &nread);
    228                 if (rc != EOK || nread != PS2_BUFSIZE) {
    229                         ddf_msg(LVL_WARN, "Incorrect packet size: %zd.", nread);
     257                rc = ps2_mouse_read_packet(mouse, packet, PS2_BUFSIZE);
     258                if (rc != EOK)
    230259                        continue;
    231                 }
    232260
    233261                ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx.",
     
    274302{
    275303        ps2_mouse_t *mouse = (ps2_mouse_t *) arg;
    276         size_t nread;
    277304        int rc;
    278305
     
    280307        while (1) {
    281308                uint8_t packet[INTELLIMOUSE_BUFSIZE] = {};
    282                 rc = chardev_read(mouse->chardev, packet, INTELLIMOUSE_BUFSIZE,
    283                     &nread);
    284                 if (rc != EOK || nread != INTELLIMOUSE_BUFSIZE) {
    285                         ddf_msg(LVL_WARN, "Incorrect packet size: %zd.", nread);
     309                rc = ps2_mouse_read_packet(mouse, packet, INTELLIMOUSE_BUFSIZE);
     310                if (rc != EOK)
    286311                        continue;
    287                 }
     312
    288313                ddf_msg(LVL_DEBUG2, "Got packet: %hhx:%hhx:%hhx:%hhx.",
    289314                    packet[0], packet[1], packet[2], packet[3]);
  • uspace/drv/hid/xtkbd/xtkbd.c

    r5d50c419 rc657bd7  
    353353               
    354354                size_t nwr;
    355                 int rc = chardev_write(kbd->chardev, cmds, sizeof(cmds), &nwr);
    356                 if (nwr != sizeof(cmds))
    357                         rc = EIO;
    358 
     355                int rc = chardev_write(kbd->chardev, &cmds[0], 1, &nwr);
     356                if (rc != EOK) {
     357                        async_answer_0(icallid, rc);
     358                        break;
     359                }
     360
     361                rc = chardev_write(kbd->chardev, &cmds[1], 1, &nwr);
    359362                async_answer_0(icallid, rc);
    360363                break;
  • uspace/lib/c/generic/io/chardev.c

    r5d50c419 rc657bd7  
    7777}
    7878
    79 int chardev_read(chardev_t *chardev, void *data, size_t size, size_t *nread)
     79/** Read from character device.
     80 *
     81 * Read as much data as is available from character device up to @a size
     82 * bytes into @a buf. On success EOK is returned and at least one byte
     83 * is read (if no byte is available the function blocks). The number
     84 * of bytes read is stored in @a *nread.
     85 *
     86 * On error a non-zero error code is returned and @a *nread is filled with
     87 * the number of bytes that were successfully transferred.
     88 *
     89 * @param chardev Character device
     90 * @param buf Destination buffer
     91 * @param size Maximum number of bytes to read
     92 * @param nread Place to store actual number of bytes read
     93 *
     94 * @return EOK on success or non-zero error code
     95 */
     96int chardev_read(chardev_t *chardev, void *buf, size_t size, size_t *nread)
    8097{
    8198        if (size > 4 * sizeof(sysarg_t))
     
    88105        async_exchange_end(exch);
    89106        if (ret > 0 && (size_t)ret <= size)
    90                 memcpy(data, message, size);
     107                memcpy(buf, message, size);
    91108
    92109        if (ret < 0) {
     
    99116}
    100117
     118/** Write to character device.
     119 *
     120 * Write @a size bytes from @a data to character device. On success EOK
     121 * is returned, all bytes were written and @a *nwritten is set to @a size.
     122 *
     123 * On error a non-zero error code is returned and @a *nwritten is filled with
     124 * the number of bytes that were successfully transferred.
     125 *
     126 * @param chardev Character device
     127 * @param buf Destination buffer
     128 * @param size Maximum number of bytes to read
     129 * @param nwritten Place to store actual number of bytes written
     130 *
     131 * @return EOK on success or non-zero error code
     132 */
    101133int chardev_write(chardev_t *chardev, const void *data, size_t size,
    102134    size_t *nwritten)
Note: See TracChangeset for help on using the changeset viewer.