From 3c43a826c6b31865c23ee06bfd9be63d70bf278e Mon Sep 17 00:00:00 2001 From: Oleksandr Tymoshenko Date: Fri, 16 Dec 2016 19:09:57 +0000 Subject: [PATCH] [spigen] Make "data" part of spigen_transfer optional Make st_data part of spigen_transfer optional by letting pass zero length and NULL pointer. SPI controller drivers handle this case fine. MFC after: 1 week --- sys/dev/spibus/spigen.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/sys/dev/spibus/spigen.c b/sys/dev/spibus/spigen.c index debcf451c32f..ae9de97dda72 100644 --- a/sys/dev/spibus/spigen.c +++ b/sys/dev/spibus/spigen.c @@ -204,7 +204,7 @@ spigen_transfer(struct cdev *cdev, struct spigen_transfer *st) int error = 0; mtx_lock(&sc->sc_mtx); - if (st->st_command.iov_len == 0 || st->st_data.iov_len == 0) + if (st->st_command.iov_len == 0) error = EINVAL; else if (st->st_command.iov_len > sc->sc_command_length_max || st->st_data.iov_len > sc->sc_data_length_max) @@ -221,16 +221,20 @@ spigen_transfer(struct cdev *cdev, struct spigen_transfer *st) M_DEVBUF, M_WAITOK); if (transfer.tx_cmd == NULL) return (ENOMEM); - transfer.tx_data = transfer.rx_data = malloc(st->st_data.iov_len, - M_DEVBUF, M_WAITOK); - if (transfer.tx_data == NULL) { - free(transfer.tx_cmd, M_DEVBUF); - return (ENOMEM); + if (st->st_data.iov_len > 0) { + transfer.tx_data = transfer.rx_data = malloc(st->st_data.iov_len, + M_DEVBUF, M_WAITOK); + if (transfer.tx_data == NULL) { + free(transfer.tx_cmd, M_DEVBUF); + return (ENOMEM); + } } + else + transfer.tx_data = transfer.rx_data = NULL; error = copyin(st->st_command.iov_base, transfer.tx_cmd, transfer.tx_cmd_sz = transfer.rx_cmd_sz = st->st_command.iov_len); - if (error == 0) + if ((error == 0) && (st->st_data.iov_len > 0)) error = copyin(st->st_data.iov_base, transfer.tx_data, transfer.tx_data_sz = transfer.rx_data_sz = st->st_data.iov_len); @@ -239,7 +243,7 @@ spigen_transfer(struct cdev *cdev, struct spigen_transfer *st) if (error == 0) { error = copyout(transfer.rx_cmd, st->st_command.iov_base, transfer.rx_cmd_sz); - if (error == 0) + if ((error == 0) && (st->st_data.iov_len > 0)) error = copyout(transfer.rx_data, st->st_data.iov_base, transfer.rx_data_sz); }