spi.c revision 1.34 1 /* $NetBSD: spi.c,v 1.34 2025/09/13 17:51:08 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
5 * Copyright (c) 2006 Garrett D'Amore.
6 * All rights reserved.
7 *
8 * Portions of this code were written by Garrett D'Amore for the
9 * Champaign-Urbana Community Wireless Network Project.
10 *
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
13 * conditions are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer in the documentation and/or other materials provided
19 * with the distribution.
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgements:
22 * This product includes software developed by the Urbana-Champaign
23 * Independent Media Center.
24 * This product includes software developed by Garrett D'Amore.
25 * 4. Urbana-Champaign Independent Media Center's name and Garrett
26 * D'Amore's name may not be used to endorse or promote products
27 * derived from this software without specific prior written permission.
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
30 * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
32 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33 * ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
34 * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
35 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 */
43
44 #include "opt_fdt.h" /* XXX */
45
46 #include <sys/cdefs.h>
47 __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.34 2025/09/13 17:51:08 thorpej Exp $");
48
49 #include "locators.h"
50
51 #include <sys/param.h>
52 #include <sys/systm.h>
53 #include <sys/device.h>
54 #include <sys/conf.h>
55 #include <sys/malloc.h>
56 #include <sys/mutex.h>
57 #include <sys/condvar.h>
58 #include <sys/errno.h>
59
60 #include <dev/spi/spivar.h>
61 #include <dev/spi/spi_io.h>
62
63 #ifdef FDT
64 #include <dev/fdt/fdt_spi.h> /* XXX */
65 #include <dev/ofw/openfirm.h> /* XXX */
66 #endif
67
68 #include "ioconf.h"
69 #include "locators.h"
70
71 struct spi_softc {
72 device_t sc_dev;
73 const struct spi_controller *sc_controller;
74 int sc_mode;
75 int sc_speed;
76 int sc_slave;
77 int sc_nslaves;
78 spi_handle_t sc_slaves;
79 kmutex_t sc_slave_state_lock;
80 kmutex_t sc_lock;
81 kcondvar_t sc_cv;
82 kmutex_t sc_dev_lock;
83 int sc_flags;
84 #define SPIC_BUSY 1
85 };
86
87 static dev_type_open(spi_open);
88 static dev_type_close(spi_close);
89 static dev_type_ioctl(spi_ioctl);
90
91 const struct cdevsw spi_cdevsw = {
92 .d_open = spi_open,
93 .d_close = spi_close,
94 .d_read = noread,
95 .d_write = nowrite,
96 .d_ioctl = spi_ioctl,
97 .d_stop = nostop,
98 .d_tty = notty,
99 .d_poll = nopoll,
100 .d_mmap = nommap,
101 .d_kqfilter = nokqfilter,
102 .d_discard = nodiscard,
103 .d_flag = D_OTHER | D_MPSAFE
104 };
105
106 /*
107 * SPI slave device. We have one of these per slave.
108 */
109 struct spi_handle {
110 struct spi_softc *sh_sc; /* static */
111 const struct spi_controller *sh_controller; /* static */
112 int sh_slave; /* static */
113 int sh_mode; /* locked by owning child */
114 int sh_speed; /* locked by owning child */
115 int sh_flags; /* vv slave_state_lock vv */
116 #define SPIH_ATTACHED __BIT(0)
117 #define SPIH_DIRECT __BIT(1)
118 device_t sh_dev; /* ^^ slave_state_lock ^^ */
119 };
120
121 #define SPI_MAXDATA 4096
122
123 /*
124 * API for bus drivers.
125 */
126
127 int
128 spibus_print(void *aux, const char *pnp)
129 {
130
131 if (pnp != NULL)
132 aprint_normal("spi at %s", pnp);
133
134 return (UNCONF);
135 }
136
137
138 static int
139 spi_match(device_t parent, cfdata_t cf, void *aux)
140 {
141
142 return 1;
143 }
144
145 static int
146 spi_print(void *aux, const char *pnp)
147 {
148 struct spi_attach_args *sa = aux;
149
150 if (sa->sa_handle->sh_slave != -1)
151 aprint_normal(" slave %d", sa->sa_handle->sh_slave);
152
153 return (UNCONF);
154 }
155
156 static void
157 spi_attach_child(struct spi_softc *sc, struct spi_attach_args *sa,
158 int chip_select, cfdata_t cf)
159 {
160 spi_handle_t sh;
161 device_t newdev = NULL;
162 bool is_direct = cf == NULL;
163 const int skip_flags = is_direct ? SPIH_ATTACHED
164 : (SPIH_ATTACHED | SPIH_DIRECT);
165 const int claim_flags = skip_flags ^ SPIH_DIRECT;
166 int locs[SPICF_NLOCS] = { 0 };
167
168 if (chip_select < 0 ||
169 chip_select >= sc->sc_controller->sct_nslaves) {
170 return;
171 }
172
173 sh = &sc->sc_slaves[chip_select];
174
175 mutex_enter(&sc->sc_slave_state_lock);
176 if (ISSET(sh->sh_flags, skip_flags)) {
177 mutex_exit(&sc->sc_slave_state_lock);
178 return;
179 }
180
181 /* Keep others off of this chip select. */
182 SET(sh->sh_flags, claim_flags);
183 mutex_exit(&sc->sc_slave_state_lock);
184
185 locs[SPICF_SLAVE] = chip_select;
186 sa->sa_handle = sh;
187
188 if (is_direct) {
189 newdev = config_found(sc->sc_dev, sa, spi_print,
190 CFARGS(.submatch = config_stdsubmatch,
191 .locators = locs,
192 .devhandle = sa->sa_devhandle));
193 } else {
194 if (config_probe(sc->sc_dev, cf, &sa)) {
195 newdev = config_attach(sc->sc_dev, cf, &sa, spi_print,
196 CFARGS(.locators = locs));
197 }
198 }
199
200 mutex_enter(&sc->sc_slave_state_lock);
201 if (newdev == NULL) {
202 /*
203 * Clear our claim on this chip select (yes, just
204 * the ATTACHED flag; we want to keep indirects off
205 * of chip selects for which there is a device tree
206 * node).
207 */
208 CLR(sh->sh_flags, SPIH_ATTACHED);
209 } else {
210 /* Record the child for posterity. */
211 sh->sh_dev = newdev;
212 }
213 mutex_exit(&sc->sc_slave_state_lock);
214 }
215
216 static int
217 spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
218 {
219 struct spi_softc *sc = device_private(parent);
220
221 if (cf->cf_loc[SPICF_SLAVE] == SPICF_SLAVE_DEFAULT) {
222 /* No wildcards for indirect on SPI. */
223 return 0;
224 }
225
226 struct spi_attach_args sa = { 0 };
227 spi_attach_child(sc, &sa, cf->cf_loc[SPICF_SLAVE], cf);
228
229 return 0;
230 }
231
232 /*
233 * XXX this is the same as i2c_fill_compat. It could be refactored into a
234 * common fill_compat function with pointers to compat & ncompat instead
235 * of attach_args as the first parameter.
236 */
237 static void
238 spi_fill_compat(struct spi_attach_args *sa, const char *compat, size_t len,
239 char **buffer)
240 {
241 int count, i;
242 const char *c, *start, **ptr;
243
244 *buffer = NULL;
245 for (i = count = 0, c = compat; i < len; i++, c++)
246 if (*c == 0)
247 count++;
248 count += 2;
249 ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
250 if (!ptr)
251 return;
252
253 for (i = count = 0, start = c = compat; i < len; i++, c++) {
254 if (*c == 0) {
255 ptr[count++] = start;
256 start = c + 1;
257 }
258 }
259 if (start < compat + len) {
260 /* last string not 0 terminated */
261 size_t l = c - start;
262 *buffer = malloc(l + 1, M_TEMP, M_WAITOK);
263 memcpy(*buffer, start, l);
264 (*buffer)[l] = 0;
265 ptr[count++] = *buffer;
266 }
267 ptr[count] = NULL;
268
269 sa->sa_compat = ptr;
270 sa->sa_ncompat = count;
271 }
272
273 static void
274 spi_direct_attach_child_devices(struct spi_softc *sc)
275 {
276 unsigned int count;
277 prop_dictionary_t child;
278 prop_array_t child_devices;
279 prop_data_t cdata;
280 devhandle_t parent_handle = device_handle(sc->sc_dev);
281 devhandle_t child_handle;
282 uint32_t chip_select;
283 uint64_t cookie;
284 struct spi_attach_args sa;
285 int loc[SPICF_NLOCS];
286 char *buf;
287 int i;
288
289 /* XXX A better way is coming, I promise... */
290 switch (devhandle_type(parent_handle)) {
291 #ifdef FDT
292 case DEVHANDLE_TYPE_OF:
293 child_devices = of_copy_spi_devs(sc->sc_dev);
294 break;
295 #endif
296 default:
297 child_devices = NULL;
298 break;
299 }
300
301 if (child_devices == NULL) {
302 return;
303 }
304
305 memset(loc, 0, sizeof loc);
306 count = prop_array_count(child_devices);
307 for (i = 0; i < count; i++) {
308 child = prop_array_get(child_devices, i);
309 if (!child)
310 continue;
311 if (!prop_dictionary_get_uint32(child, "slave", &chip_select))
312 continue;
313 if (!prop_dictionary_get_uint64(child, "cookie", &cookie))
314 continue;
315 if (!(cdata = prop_dictionary_get(child, "compatible")))
316 continue;
317 loc[SPICF_SLAVE] = chip_select;
318
319 memset(&sa, 0, sizeof sa);
320 sa.sa_handle = &sc->sc_slaves[chip_select];
321
322 /* XXX Really, I promise, it'll get better... */
323 switch (devhandle_type(parent_handle)) {
324 #ifdef FDT
325 case DEVHANDLE_TYPE_OF:
326 child_handle = devhandle_from_of(parent_handle,
327 (int)cookie);
328 break;
329 #endif
330 default:
331 child_handle = devhandle_invalid();
332 }
333 sa.sa_devhandle = child_handle;
334
335 buf = NULL;
336 spi_fill_compat(&sa,
337 prop_data_value(cdata),
338 prop_data_size(cdata), &buf);
339
340 spi_attach_child(sc, &sa, chip_select, NULL);
341
342 if (sa.sa_compat)
343 free(sa.sa_compat, M_TEMP);
344 if (buf)
345 free(buf, M_TEMP);
346 }
347 prop_object_release(child_devices);
348 }
349
350 int
351 spi_compatible_match(const struct spi_attach_args *sa,
352 const cfdata_t cf __unused,
353 const struct device_compatible_entry *compats)
354 {
355 int match_result;
356
357 match_result = device_compatible_match(sa->sa_compat, sa->sa_ncompat,
358 compats);
359 if (match_result) {
360 match_result = SPI_MATCH_DIRECT_COMPATIBLE + match_result - 1;
361 }
362
363 return match_result ? match_result : SPI_MATCH_DEFAULT /* XXX */;
364 }
365
366 const struct device_compatible_entry *
367 spi_compatible_lookup(const struct spi_attach_args *sa,
368 const struct device_compatible_entry *compats)
369 {
370 return device_compatible_lookup(sa->sa_compat, sa->sa_ncompat,
371 compats);
372 }
373
374 bool
375 spi_use_direct_match(const struct spi_attach_args *sa,
376 const struct device_compatible_entry *compats,
377 int *match_resultp)
378 {
379 KASSERT(match_resultp != NULL);
380
381 if (sa->sa_ncompat > 0 && sa->sa_compat != NULL) {
382 *match_resultp = spi_compatible_match(sa, NULL, compats);
383 return true;
384 }
385
386 return false;
387 }
388
389 /*
390 * API for device drivers.
391 *
392 * We provide wrapper routines to decouple the ABI for the SPI
393 * device drivers from the ABI for the SPI bus drivers.
394 */
395 static void
396 spi_attach(device_t parent, device_t self, void *aux)
397 {
398 struct spi_softc *sc = device_private(self);
399 struct spibus_attach_args *sba = aux;
400 int i;
401
402 aprint_naive(": SPI bus\n");
403 aprint_normal(": SPI bus\n");
404
405 mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NONE);
406 mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
407 mutex_init(&sc->sc_slave_state_lock, MUTEX_DEFAULT, IPL_NONE);
408 cv_init(&sc->sc_cv, "spictl");
409
410 sc->sc_dev = self;
411 sc->sc_controller = sba->sba_controller;
412 sc->sc_nslaves = sba->sba_controller->sct_nslaves;
413 /* allocate slave structures */
414 sc->sc_slaves = malloc(sizeof(*sc->sc_slaves) * sc->sc_nslaves,
415 M_DEVBUF, M_WAITOK | M_ZERO);
416
417 sc->sc_speed = 0;
418 sc->sc_mode = -1;
419 sc->sc_slave = -1;
420
421 /*
422 * Initialize slave handles
423 */
424 for (i = 0; i < sc->sc_nslaves; i++) {
425 sc->sc_slaves[i].sh_slave = i;
426 sc->sc_slaves[i].sh_sc = sc;
427 sc->sc_slaves[i].sh_controller = sc->sc_controller;
428 }
429
430 /* XXX Need a better way for this. */
431 switch (devhandle_type(device_handle(sc->sc_dev))) {
432 #ifdef FDT
433 case DEVHANDLE_TYPE_OF:
434 fdtbus_register_spi_controller(self, sc->sc_controller);
435 break;
436 #endif /* FDT */
437 default:
438 break;
439 }
440
441 /* First attach devices known to be present via the device tree. */
442 spi_direct_attach_child_devices(sc);
443
444 /* Then do any other devices the user may have manually wired */
445 config_search(self, NULL,
446 CFARGS(.search = spi_search));
447 }
448
449 static int
450 spi_open(dev_t dev, int flag, int fmt, lwp_t *l)
451 {
452 struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
453
454 if (sc == NULL)
455 return ENXIO;
456
457 return 0;
458 }
459
460 static int
461 spi_close(dev_t dev, int flag, int fmt, lwp_t *l)
462 {
463
464 return 0;
465 }
466
467 static int
468 spi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
469 {
470 struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
471 spi_handle_t sh;
472 spi_ioctl_configure_t *sic;
473 spi_ioctl_transfer_t *sit;
474 uint8_t *sbuf, *rbuf;
475 int error;
476
477 if (sc == NULL)
478 return ENXIO;
479
480 mutex_enter(&sc->sc_dev_lock);
481
482 switch (cmd) {
483 case SPI_IOCTL_CONFIGURE:
484 sic = (spi_ioctl_configure_t *)data;
485 if (sic->sic_addr < 0 || sic->sic_addr >= sc->sc_nslaves) {
486 error = EINVAL;
487 break;
488 }
489 sh = &sc->sc_slaves[sic->sic_addr];
490 error = spi_configure(sc->sc_dev, sh, sic->sic_mode,
491 sic->sic_speed);
492 break;
493 case SPI_IOCTL_TRANSFER:
494 sit = (spi_ioctl_transfer_t *)data;
495 if (sit->sit_addr < 0 || sit->sit_addr >= sc->sc_nslaves) {
496 error = EINVAL;
497 break;
498 }
499 if ((sit->sit_send && sit->sit_sendlen == 0)
500 || (sit->sit_recv && sit->sit_recvlen == 0)) {
501 error = EINVAL;
502 break;
503 }
504 sh = &sc->sc_slaves[sit->sit_addr];
505 sbuf = rbuf = NULL;
506 error = 0;
507 if (sit->sit_send && sit->sit_sendlen <= SPI_MAXDATA) {
508 sbuf = malloc(sit->sit_sendlen, M_DEVBUF, M_WAITOK);
509 error = copyin(sit->sit_send, sbuf, sit->sit_sendlen);
510 }
511 if (sit->sit_recv && sit->sit_recvlen <= SPI_MAXDATA) {
512 rbuf = malloc(sit->sit_recvlen, M_DEVBUF, M_WAITOK);
513 }
514 if (error == 0) {
515 if (sbuf && rbuf)
516 error = spi_send_recv(sh,
517 sit->sit_sendlen, sbuf,
518 sit->sit_recvlen, rbuf);
519 else if (sbuf)
520 error = spi_send(sh,
521 sit->sit_sendlen, sbuf);
522 else if (rbuf)
523 error = spi_recv(sh,
524 sit->sit_recvlen, rbuf);
525 }
526 if (rbuf) {
527 if (error == 0)
528 error = copyout(rbuf, sit->sit_recv,
529 sit->sit_recvlen);
530 free(rbuf, M_DEVBUF);
531 }
532 if (sbuf) {
533 free(sbuf, M_DEVBUF);
534 }
535 break;
536 default:
537 error = ENODEV;
538 break;
539 }
540
541 mutex_exit(&sc->sc_dev_lock);
542
543 return error;
544 }
545
546 CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
547 spi_match, spi_attach, NULL, NULL);
548
549 /*
550 * Configure. This should be the first thing that the SPI driver
551 * should do, to configure which mode (e.g. SPI_MODE_0, which is the
552 * same as Philips Microwire mode), and speed. If the bus driver
553 * cannot run fast enough, then it should just configure the fastest
554 * mode that it can support. If the bus driver cannot run slow
555 * enough, then the device is incompatible and an error should be
556 * returned.
557 */
558 int
559 spi_configure(device_t dev __unused, spi_handle_t sh, int mode, int speed)
560 {
561
562 sh->sh_mode = mode;
563 sh->sh_speed = speed;
564
565 /* No need to report errors; no failures. */
566
567 return 0;
568 }
569
570 /*
571 * Acquire controller
572 */
573 static void
574 spi_acquire(spi_handle_t sh)
575 {
576 struct spi_softc *sc = sh->sh_sc;
577
578 mutex_enter(&sc->sc_lock);
579 while ((sc->sc_flags & SPIC_BUSY) != 0)
580 cv_wait(&sc->sc_cv, &sc->sc_lock);
581 sc->sc_flags |= SPIC_BUSY;
582 mutex_exit(&sc->sc_lock);
583 }
584
585 /*
586 * Release controller
587 */
588 static void
589 spi_release(spi_handle_t sh)
590 {
591 struct spi_softc *sc = sh->sh_sc;
592
593 mutex_enter(&sc->sc_lock);
594 sc->sc_flags &= ~SPIC_BUSY;
595 cv_broadcast(&sc->sc_cv);
596 mutex_exit(&sc->sc_lock);
597 }
598
599 void
600 spi_transfer_init(struct spi_transfer *st)
601 {
602
603 mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_VM);
604 cv_init(&st->st_cv, "spixfr");
605
606 st->st_flags = 0;
607 st->st_errno = 0;
608 st->st_done = NULL;
609 st->st_chunks = NULL;
610 st->st_private = NULL;
611 st->st_slave = -1;
612 }
613
614 void
615 spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
616 uint8_t *rptr)
617 {
618
619 chunk->chunk_write = chunk->chunk_wptr = wptr;
620 chunk->chunk_read = chunk->chunk_rptr = rptr;
621 chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
622 chunk->chunk_next = NULL;
623 }
624
625 void
626 spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
627 {
628 struct spi_chunk **cpp;
629
630 /* this is an O(n) insert -- perhaps we should use a simpleq? */
631 for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
632 *cpp = chunk;
633 }
634
635 int
636 spi_transfer(spi_handle_t sh, struct spi_transfer *st)
637 {
638 struct spi_softc *sc = sh->sh_sc;
639 const struct spi_controller *tag = sh->sh_controller;
640 struct spi_chunk *chunk;
641 int error;
642
643 /*
644 * Initialize "resid" counters and pointers, so that callers
645 * and bus drivers don't have to.
646 */
647 for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
648 chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
649 chunk->chunk_wptr = chunk->chunk_write;
650 chunk->chunk_rptr = chunk->chunk_read;
651 }
652
653 /*
654 * Match slave and parameters to handle
655 */
656 st->st_slave = sh->sh_slave;
657
658 /*
659 * Reserve controller during transaction
660 */
661 spi_acquire(sh);
662
663 st->st_spiprivate = (void *)sh;
664
665 /*
666 * Reconfigure controller
667 *
668 * XXX backends don't configure per-slave parameters
669 * Whenever we switch slaves or change mode or speed, we
670 * need to tell the backend.
671 */
672 if (sc->sc_slave != sh->sh_slave
673 || sc->sc_mode != sh->sh_mode
674 || sc->sc_speed != sh->sh_speed) {
675 error = (*tag->sct_configure)(tag->sct_cookie,
676 sh->sh_slave, sh->sh_mode, sh->sh_speed);
677 if (error)
678 return error;
679 }
680 sc->sc_mode = sh->sh_mode;
681 sc->sc_speed = sh->sh_speed;
682 sc->sc_slave = sh->sh_slave;
683
684 error = (*tag->sct_transfer)(tag->sct_cookie, st);
685
686 return error;
687 }
688
689 void
690 spi_wait(struct spi_transfer *st)
691 {
692 spi_handle_t sh = st->st_spiprivate;
693
694 mutex_enter(&st->st_lock);
695 while (!(st->st_flags & SPI_F_DONE)) {
696 cv_wait(&st->st_cv, &st->st_lock);
697 }
698 mutex_exit(&st->st_lock);
699 cv_destroy(&st->st_cv);
700 mutex_destroy(&st->st_lock);
701
702 /*
703 * End transaction
704 */
705 spi_release(sh);
706 }
707
708 void
709 spi_done(struct spi_transfer *st, int err)
710 {
711
712 mutex_enter(&st->st_lock);
713 if ((st->st_errno = err) != 0) {
714 st->st_flags |= SPI_F_ERROR;
715 }
716 st->st_flags |= SPI_F_DONE;
717 if (st->st_done != NULL) {
718 (*st->st_done)(st);
719 } else {
720 cv_broadcast(&st->st_cv);
721 }
722 mutex_exit(&st->st_lock);
723 }
724
725 /*
726 * Some convenience routines. These routines block until the work
727 * is done.
728 *
729 * spi_recv - receives data from the bus
730 *
731 * spi_send - sends data to the bus
732 *
733 * spi_send_recv - sends data to the bus, and then receives. Note that this is
734 * done synchronously, i.e. send a command and get the response. This is
735 * not full duplex. If you want full duplex, you can't use these convenience
736 * wrappers.
737 */
738 int
739 spi_recv(spi_handle_t sh, int cnt, uint8_t *data)
740 {
741 struct spi_transfer trans;
742 struct spi_chunk chunk;
743
744 spi_transfer_init(&trans);
745 spi_chunk_init(&chunk, cnt, NULL, data);
746 spi_transfer_add(&trans, &chunk);
747
748 /* enqueue it and wait for it to complete */
749 spi_transfer(sh, &trans);
750 spi_wait(&trans);
751
752 if (trans.st_flags & SPI_F_ERROR)
753 return trans.st_errno;
754
755 return 0;
756 }
757
758 int
759 spi_send(spi_handle_t sh, int cnt, const uint8_t *data)
760 {
761 struct spi_transfer trans;
762 struct spi_chunk chunk;
763
764 spi_transfer_init(&trans);
765 spi_chunk_init(&chunk, cnt, data, NULL);
766 spi_transfer_add(&trans, &chunk);
767
768 /* enqueue it and wait for it to complete */
769 spi_transfer(sh, &trans);
770 spi_wait(&trans);
771
772 if (trans.st_flags & SPI_F_ERROR)
773 return trans.st_errno;
774
775 return 0;
776 }
777
778 int
779 spi_send_recv(spi_handle_t sh, int scnt, const uint8_t *snd,
780 int rcnt, uint8_t *rcv)
781 {
782 struct spi_transfer trans;
783 struct spi_chunk chunk1, chunk2;
784
785 spi_transfer_init(&trans);
786 spi_chunk_init(&chunk1, scnt, snd, NULL);
787 spi_chunk_init(&chunk2, rcnt, NULL, rcv);
788 spi_transfer_add(&trans, &chunk1);
789 spi_transfer_add(&trans, &chunk2);
790
791 /* enqueue it and wait for it to complete */
792 spi_transfer(sh, &trans);
793 spi_wait(&trans);
794
795 if (trans.st_flags & SPI_F_ERROR)
796 return trans.st_errno;
797
798 return 0;
799 }
800