Skip to content

Commit 56afdb7

Browse files
committed
Merge remote-tracking branches 'spi/topic/spidev' and 'spi/topic/spidev-test' into spi-next
3 parents 165f228 + 956b200 + 3006191 commit 56afdb7

2 files changed

Lines changed: 138 additions & 32 deletions

File tree

‎Documentation/spi/spidev_test.c‎

Lines changed: 95 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <unistd.h>
1616
#include <stdio.h>
1717
#include <stdlib.h>
18+
#include <string.h>
1819
#include <getopt.h>
1920
#include <fcntl.h>
2021
#include <sys/ioctl.h>
@@ -34,24 +35,79 @@ static uint32_t mode;
3435
static uint8_t bits = 8;
3536
static uint32_t speed = 500000;
3637
static uint16_t delay;
38+
static int verbose;
3739

38-
static void transfer(int fd)
40+
uint8_t default_tx[] = {
41+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
42+
0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
43+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
44+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
46+
0xF0, 0x0D,
47+
};
48+
49+
uint8_t default_rx[ARRAY_SIZE(default_tx)] = {0, };
50+
char *input_tx;
51+
52+
static void hex_dump(const void *src, size_t length, size_t line_size, char *prefix)
53+
{
54+
int i = 0;
55+
const unsigned char *address = src;
56+
const unsigned char *line = address;
57+
unsigned char c;
58+
59+
printf("%s | ", prefix);
60+
while (length-- > 0) {
61+
printf("%02X ", *address++);
62+
if (!(++i % line_size) || (length == 0 && i % line_size)) {
63+
if (length == 0) {
64+
while (i++ % line_size)
65+
printf("__ ");
66+
}
67+
printf(" | "); /* right close */
68+
while (line < address) {
69+
c = *line++;
70+
printf("%c", (c < 33 || c == 255) ? 0x2E : c);
71+
}
72+
printf("\n");
73+
if (length > 0)
74+
printf("%s | ", prefix);
75+
}
76+
}
77+
}
78+
79+
/*
80+
* Unescape - process hexadecimal escape character
81+
* converts shell input "\x23" -> 0x23
82+
*/
83+
int unespcape(char *_dst, char *_src, size_t len)
84+
{
85+
int ret = 0;
86+
char *src = _src;
87+
char *dst = _dst;
88+
unsigned int ch;
89+
90+
while (*src) {
91+
if (*src == '\\' && *(src+1) == 'x') {
92+
sscanf(src + 2, "%2x", &ch);
93+
src += 4;
94+
*dst++ = (unsigned char)ch;
95+
} else {
96+
*dst++ = *src++;
97+
}
98+
ret++;
99+
}
100+
return ret;
101+
}
102+
103+
static void transfer(int fd, uint8_t const *tx, uint8_t const *rx, size_t len)
39104
{
40105
int ret;
41-
uint8_t tx[] = {
42-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
43-
0x40, 0x00, 0x00, 0x00, 0x00, 0x95,
44-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
45-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
46-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
47-
0xDE, 0xAD, 0xBE, 0xEF, 0xBA, 0xAD,
48-
0xF0, 0x0D,
49-
};
50-
uint8_t rx[ARRAY_SIZE(tx)] = {0, };
106+
51107
struct spi_ioc_transfer tr = {
52108
.tx_buf = (unsigned long)tx,
53109
.rx_buf = (unsigned long)rx,
54-
.len = ARRAY_SIZE(tx),
110+
.len = len,
55111
.delay_usecs = delay,
56112
.speed_hz = speed,
57113
.bits_per_word = bits,
@@ -76,12 +132,9 @@ static void transfer(int fd)
76132
if (ret < 1)
77133
pabort("can't send spi message");
78134

79-
for (ret = 0; ret < ARRAY_SIZE(tx); ret++) {
80-
if (!(ret % 6))
81-
puts("");
82-
printf("%.2X ", rx[ret]);
83-
}
84-
puts("");
135+
if (verbose)
136+
hex_dump(tx, len, 32, "TX");
137+
hex_dump(rx, len, 32, "RX");
85138
}
86139

87140
static void print_usage(const char *prog)
@@ -97,6 +150,8 @@ static void print_usage(const char *prog)
97150
" -L --lsb least significant bit first\n"
98151
" -C --cs-high chip select active high\n"
99152
" -3 --3wire SI/SO signals shared\n"
153+
" -v --verbose Verbose (show tx buffer)\n"
154+
" -p Send data (e.g. \"1234\\xde\\xad\")\n"
100155
" -N --no-cs no chip select\n"
101156
" -R --ready slave pulls low to pause\n"
102157
" -2 --dual dual transfer\n"
@@ -121,12 +176,13 @@ static void parse_opts(int argc, char *argv[])
121176
{ "no-cs", 0, 0, 'N' },
122177
{ "ready", 0, 0, 'R' },
123178
{ "dual", 0, 0, '2' },
179+
{ "verbose", 0, 0, 'v' },
124180
{ "quad", 0, 0, '4' },
125181
{ NULL, 0, 0, 0 },
126182
};
127183
int c;
128184

129-
c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24", lopts, NULL);
185+
c = getopt_long(argc, argv, "D:s:d:b:lHOLC3NR24p:v", lopts, NULL);
130186

131187
if (c == -1)
132188
break;
@@ -165,9 +221,15 @@ static void parse_opts(int argc, char *argv[])
165221
case 'N':
166222
mode |= SPI_NO_CS;
167223
break;
224+
case 'v':
225+
verbose = 1;
226+
break;
168227
case 'R':
169228
mode |= SPI_READY;
170229
break;
230+
case 'p':
231+
input_tx = optarg;
232+
break;
171233
case '2':
172234
mode |= SPI_TX_DUAL;
173235
break;
@@ -191,6 +253,9 @@ int main(int argc, char *argv[])
191253
{
192254
int ret = 0;
193255
int fd;
256+
uint8_t *tx;
257+
uint8_t *rx;
258+
int size;
194259

195260
parse_opts(argc, argv);
196261

@@ -235,7 +300,17 @@ int main(int argc, char *argv[])
235300
printf("bits per word: %d\n", bits);
236301
printf("max speed: %d Hz (%d KHz)\n", speed, speed/1000);
237302

238-
transfer(fd);
303+
if (input_tx) {
304+
size = strlen(input_tx+1);
305+
tx = malloc(size);
306+
rx = malloc(size);
307+
size = unespcape((char *)tx, input_tx, size);
308+
transfer(fd, tx, rx, size);
309+
free(rx);
310+
free(tx);
311+
} else {
312+
transfer(fd, default_tx, default_rx, sizeof(default_tx));
313+
}
239314

240315
close(fd);
241316

‎drivers/spi/spidev.c‎

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ static int spidev_message(struct spidev_data *spidev,
223223
struct spi_transfer *k_xfers;
224224
struct spi_transfer *k_tmp;
225225
struct spi_ioc_transfer *u_tmp;
226-
unsigned n, total;
226+
unsigned n, total, tx_total, rx_total;
227227
u8 *tx_buf, *rx_buf;
228228
int status = -EFAULT;
229229

@@ -239,33 +239,52 @@ static int spidev_message(struct spidev_data *spidev,
239239
tx_buf = spidev->tx_buffer;
240240
rx_buf = spidev->rx_buffer;
241241
total = 0;
242+
tx_total = 0;
243+
rx_total = 0;
242244
for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
243245
n;
244246
n--, k_tmp++, u_tmp++) {
245247
k_tmp->len = u_tmp->len;
246248

247249
total += k_tmp->len;
248-
if (total > bufsiz) {
250+
/* Since the function returns the total length of transfers
251+
* on success, restrict the total to positive int values to
252+
* avoid the return value looking like an error. Also check
253+
* each transfer length to avoid arithmetic overflow.
254+
*/
255+
if (total > INT_MAX || k_tmp->len > INT_MAX) {
249256
status = -EMSGSIZE;
250257
goto done;
251258
}
252259

253260
if (u_tmp->rx_buf) {
261+
/* this transfer needs space in RX bounce buffer */
262+
rx_total += k_tmp->len;
263+
if (rx_total > bufsiz) {
264+
status = -EMSGSIZE;
265+
goto done;
266+
}
254267
k_tmp->rx_buf = rx_buf;
255268
if (!access_ok(VERIFY_WRITE, (u8 __user *)
256269
(uintptr_t) u_tmp->rx_buf,
257270
u_tmp->len))
258271
goto done;
272+
rx_buf += k_tmp->len;
259273
}
260274
if (u_tmp->tx_buf) {
275+
/* this transfer needs space in TX bounce buffer */
276+
tx_total += k_tmp->len;
277+
if (tx_total > bufsiz) {
278+
status = -EMSGSIZE;
279+
goto done;
280+
}
261281
k_tmp->tx_buf = tx_buf;
262282
if (copy_from_user(tx_buf, (const u8 __user *)
263283
(uintptr_t) u_tmp->tx_buf,
264284
u_tmp->len))
265285
goto done;
286+
tx_buf += k_tmp->len;
266287
}
267-
tx_buf += k_tmp->len;
268-
rx_buf += k_tmp->len;
269288

270289
k_tmp->cs_change = !!u_tmp->cs_change;
271290
k_tmp->tx_nbits = u_tmp->tx_nbits;
@@ -303,8 +322,8 @@ static int spidev_message(struct spidev_data *spidev,
303322
status = -EFAULT;
304323
goto done;
305324
}
325+
rx_buf += u_tmp->len;
306326
}
307-
rx_buf += u_tmp->len;
308327
}
309328
status = total;
310329

@@ -684,6 +703,14 @@ static const struct file_operations spidev_fops = {
684703

685704
static struct class *spidev_class;
686705

706+
#ifdef CONFIG_OF
707+
static const struct of_device_id spidev_dt_ids[] = {
708+
{ .compatible = "rohm,dh2228fv" },
709+
{},
710+
};
711+
MODULE_DEVICE_TABLE(of, spidev_dt_ids);
712+
#endif
713+
687714
/*-------------------------------------------------------------------------*/
688715

689716
static int spidev_probe(struct spi_device *spi)
@@ -692,6 +719,17 @@ static int spidev_probe(struct spi_device *spi)
692719
int status;
693720
unsigned long minor;
694721

722+
/*
723+
* spidev should never be referenced in DT without a specific
724+
* compatbile string, it is a Linux implementation thing
725+
* rather than a description of the hardware.
726+
*/
727+
if (spi->dev.of_node && !of_match_device(spidev_dt_ids, &spi->dev)) {
728+
dev_err(&spi->dev, "buggy DT: spidev listed directly in DT\n");
729+
WARN_ON(spi->dev.of_node &&
730+
!of_match_device(spidev_dt_ids, &spi->dev));
731+
}
732+
695733
/* Allocate driver data */
696734
spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
697735
if (!spidev)
@@ -758,13 +796,6 @@ static int spidev_remove(struct spi_device *spi)
758796
return 0;
759797
}
760798

761-
static const struct of_device_id spidev_dt_ids[] = {
762-
{ .compatible = "rohm,dh2228fv" },
763-
{},
764-
};
765-
766-
MODULE_DEVICE_TABLE(of, spidev_dt_ids);
767-
768799
static struct spi_driver spidev_spi_driver = {
769800
.driver = {
770801
.name = "spidev",

0 commit comments

Comments
 (0)