1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Simple synchronous userspace interface to SPI devices 4 * 5 * Copyright (C) 2006 SWAPP 6 * Andrea Paterniani <a.paterniani@swapp-eng.it> 7 * Copyright (C) 2007 David Brownell (simplification, cleanup) 8 */ 9 10 #include <linux/init.h> 11 #include <linux/ioctl.h> 12 #include <linux/fs.h> 13 #include <linux/device.h> 14 #include <linux/err.h> 15 #include <linux/list.h> 16 #include <linux/errno.h> 17 #include <linux/module.h> 18 #include <linux/mutex.h> 19 #include <linux/property.h> 20 #include <linux/slab.h> 21 #include <linux/compat.h> 22 23 #include <linux/spi/spi.h> 24 #include <linux/spi/spidev.h> 25 26 #include <linux/uaccess.h> 27 28 29 /* 30 * This supports access to SPI devices using normal userspace I/O calls. 31 * Note that while traditional UNIX/POSIX I/O semantics are half duplex, 32 * and often mask message boundaries, full SPI support requires full duplex 33 * transfers. There are several kinds of internal message boundaries to 34 * handle chipselect management and other protocol options. 35 * 36 * SPI has a character major number assigned. We allocate minor numbers 37 * dynamically using a bitmask. You must use hotplug tools, such as udev 38 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device 39 * nodes, since there is no fixed association of minor numbers with any 40 * particular SPI bus or device. 41 */ 42 #define SPIDEV_MAJOR 153 /* assigned */ 43 #define N_SPI_MINORS 32 /* ... up to 256 */ 44 45 static DECLARE_BITMAP(minors, N_SPI_MINORS); 46 47 static_assert(N_SPI_MINORS > 0 && N_SPI_MINORS <= 256); 48 49 /* Bit masks for spi_device.mode management. Note that incorrect 50 * settings for some settings can cause *lots* of trouble for other 51 * devices on a shared bus: 52 * 53 * - CS_HIGH ... this device will be active when it shouldn't be 54 * - 3WIRE ... when active, it won't behave as it should 55 * - NO_CS ... there will be no explicit message boundaries; this 56 * is completely incompatible with the shared bus model 57 * - READY ... transfers may proceed when they shouldn't. 58 * 59 * REVISIT should changing those flags be privileged? 60 */ 61 #define SPI_MODE_MASK (SPI_MODE_X_MASK | SPI_CS_HIGH \ 62 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \ 63 | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \ 64 | SPI_TX_QUAD | SPI_TX_OCTAL | SPI_RX_DUAL \ 65 | SPI_RX_QUAD | SPI_RX_OCTAL \ 66 | SPI_RX_CPHA_FLIP | SPI_3WIRE_HIZ \ 67 | SPI_MOSI_IDLE_LOW) 68 69 struct spidev_data { 70 dev_t devt; 71 struct mutex spi_lock; 72 struct spi_device *spi; 73 struct list_head device_entry; 74 75 /* TX/RX buffers are NULL unless this device is open (users > 0) */ 76 unsigned users; 77 u8 *tx_buffer; 78 u8 *rx_buffer; 79 u32 speed_hz; 80 }; 81 82 static LIST_HEAD(device_list); 83 static DEFINE_MUTEX(device_list_lock); 84 85 static unsigned bufsiz = 4096; 86 module_param(bufsiz, uint, S_IRUGO); 87 MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message"); 88 89 /*-------------------------------------------------------------------------*/ 90 91 static ssize_t 92 spidev_sync_unlocked(struct spi_device *spi, struct spi_message *message) 93 { 94 ssize_t status; 95 96 status = spi_sync(spi, message); 97 if (status == 0) 98 status = message->actual_length; 99 100 return status; 101 } 102 103 static inline ssize_t 104 spidev_sync_write(struct spidev_data *spidev, size_t len) 105 { 106 struct spi_transfer t = { 107 .tx_buf = spidev->tx_buffer, 108 .len = len, 109 .speed_hz = spidev->speed_hz, 110 }; 111 struct spi_message m; 112 113 spi_message_init(&m); 114 spi_message_add_tail(&t, &m); 115 116 return spidev_sync_unlocked(spidev->spi, &m); 117 } 118 119 static inline ssize_t 120 spidev_sync_read(struct spidev_data *spidev, size_t len) 121 { 122 struct spi_transfer t = { 123 .rx_buf = spidev->rx_buffer, 124 .len = len, 125 .speed_hz = spidev->speed_hz, 126 }; 127 struct spi_message m; 128 129 spi_message_init(&m); 130 spi_message_add_tail(&t, &m); 131 132 return spidev_sync_unlocked(spidev->spi, &m); 133 } 134 135 /*-------------------------------------------------------------------------*/ 136 137 /* Read-only message with current device setup */ 138 static ssize_t 139 spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) 140 { 141 struct spidev_data *spidev; 142 ssize_t status = -ESHUTDOWN; 143 144 /* chipselect only toggles at start or end of operation */ 145 if (count > bufsiz) 146 return -EMSGSIZE; 147 148 spidev = filp->private_data; 149 150 mutex_lock(&spidev->spi_lock); 151 152 if (spidev->spi == NULL) 153 goto err_spi_removed; 154 155 status = spidev_sync_read(spidev, count); 156 if (status > 0) { 157 unsigned long missing; 158 159 missing = copy_to_user(buf, spidev->rx_buffer, status); 160 if (missing == status) 161 status = -EFAULT; 162 else 163 status = status - missing; 164 } 165 166 err_spi_removed: 167 mutex_unlock(&spidev->spi_lock); 168 169 return status; 170 } 171 172 /* Write-only message with current device setup */ 173 static ssize_t 174 spidev_write(struct file *filp, const char __user *buf, 175 size_t count, loff_t *f_pos) 176 { 177 struct spidev_data *spidev; 178 ssize_t status = -ESHUTDOWN; 179 unsigned long missing; 180 181 /* chipselect only toggles at start or end of operation */ 182 if (count > bufsiz) 183 return -EMSGSIZE; 184 185 spidev = filp->private_data; 186 187 mutex_lock(&spidev->spi_lock); 188 189 if (spidev->spi == NULL) 190 goto err_spi_removed; 191 192 missing = copy_from_user(spidev->tx_buffer, buf, count); 193 if (missing == 0) 194 status = spidev_sync_write(spidev, count); 195 else 196 status = -EFAULT; 197 198 err_spi_removed: 199 mutex_unlock(&spidev->spi_lock); 200 201 return status; 202 } 203 204 static int spidev_message(struct spidev_data *spidev, 205 struct spi_ioc_transfer *u_xfers, unsigned n_xfers) 206 { 207 struct spi_message msg; 208 struct spi_transfer *k_xfers; 209 struct spi_transfer *k_tmp; 210 struct spi_ioc_transfer *u_tmp; 211 unsigned n, total, tx_total, rx_total; 212 u8 *tx_buf, *rx_buf; 213 int status = -EFAULT; 214 215 spi_message_init(&msg); 216 k_xfers = kzalloc_objs(*k_tmp, n_xfers); 217 if (k_xfers == NULL) 218 return -ENOMEM; 219 220 /* Construct spi_message, copying any tx data to bounce buffer. 221 * We walk the array of user-provided transfers, using each one 222 * to initialize a kernel version of the same transfer. 223 */ 224 tx_buf = spidev->tx_buffer; 225 rx_buf = spidev->rx_buffer; 226 total = 0; 227 tx_total = 0; 228 rx_total = 0; 229 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; 230 n; 231 n--, k_tmp++, u_tmp++) { 232 /* Ensure that also following allocations from rx_buf/tx_buf will meet 233 * DMA alignment requirements. 234 */ 235 unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_DMA_MINALIGN); 236 237 k_tmp->len = u_tmp->len; 238 239 total += k_tmp->len; 240 /* Since the function returns the total length of transfers 241 * on success, restrict the total to positive int values to 242 * avoid the return value looking like an error. Also check 243 * each transfer length to avoid arithmetic overflow. 244 */ 245 if (total > INT_MAX || k_tmp->len > INT_MAX) { 246 status = -EMSGSIZE; 247 goto done; 248 } 249 250 if (u_tmp->rx_buf) { 251 /* this transfer needs space in RX bounce buffer */ 252 rx_total += len_aligned; 253 if (rx_total > bufsiz) { 254 status = -EMSGSIZE; 255 goto done; 256 } 257 k_tmp->rx_buf = rx_buf; 258 rx_buf += len_aligned; 259 } 260 if (u_tmp->tx_buf) { 261 /* this transfer needs space in TX bounce buffer */ 262 tx_total += len_aligned; 263 if (tx_total > bufsiz) { 264 status = -EMSGSIZE; 265 goto done; 266 } 267 k_tmp->tx_buf = tx_buf; 268 if (copy_from_user(tx_buf, (const u8 __user *) 269 (uintptr_t) u_tmp->tx_buf, 270 u_tmp->len)) 271 goto done; 272 tx_buf += len_aligned; 273 } 274 275 k_tmp->cs_change = !!u_tmp->cs_change; 276 k_tmp->tx_nbits = u_tmp->tx_nbits; 277 k_tmp->rx_nbits = u_tmp->rx_nbits; 278 k_tmp->bits_per_word = u_tmp->bits_per_word; 279 k_tmp->delay.value = u_tmp->delay_usecs; 280 k_tmp->delay.unit = SPI_DELAY_UNIT_USECS; 281 k_tmp->speed_hz = u_tmp->speed_hz; 282 k_tmp->word_delay.value = u_tmp->word_delay_usecs; 283 k_tmp->word_delay.unit = SPI_DELAY_UNIT_USECS; 284 if (!k_tmp->speed_hz) 285 k_tmp->speed_hz = spidev->speed_hz; 286 #ifdef VERBOSE 287 dev_dbg(&spidev->spi->dev, 288 " xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n", 289 k_tmp->len, 290 k_tmp->rx_buf ? "rx " : "", 291 k_tmp->tx_buf ? "tx " : "", 292 k_tmp->cs_change ? "cs " : "", 293 k_tmp->bits_per_word ? : spidev->spi->bits_per_word, 294 k_tmp->delay.value, 295 k_tmp->word_delay.value, 296 k_tmp->speed_hz ? : spidev->spi->max_speed_hz); 297 #endif 298 spi_message_add_tail(k_tmp, &msg); 299 } 300 301 status = spidev_sync_unlocked(spidev->spi, &msg); 302 if (status < 0) 303 goto done; 304 305 /* copy any rx data out of bounce buffer */ 306 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; 307 n; 308 n--, k_tmp++, u_tmp++) { 309 if (u_tmp->rx_buf) { 310 if (copy_to_user((u8 __user *) 311 (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf, 312 u_tmp->len)) { 313 status = -EFAULT; 314 goto done; 315 } 316 } 317 } 318 status = total; 319 320 done: 321 kfree(k_xfers); 322 return status; 323 } 324 325 static struct spi_ioc_transfer * 326 spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc, 327 unsigned *n_ioc) 328 { 329 u32 tmp; 330 331 /* Check type, command number and direction */ 332 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC 333 || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0)) 334 || _IOC_DIR(cmd) != _IOC_WRITE) 335 return ERR_PTR(-ENOTTY); 336 337 tmp = _IOC_SIZE(cmd); 338 if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) 339 return ERR_PTR(-EINVAL); 340 *n_ioc = tmp / sizeof(struct spi_ioc_transfer); 341 if (*n_ioc == 0) 342 return NULL; 343 344 /* copy into scratch area */ 345 return memdup_user(u_ioc, tmp); 346 } 347 348 static long 349 spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 350 { 351 int retval = 0; 352 struct spidev_data *spidev; 353 struct spi_device *spi; 354 struct spi_controller *ctlr; 355 u32 tmp; 356 unsigned n_ioc; 357 struct spi_ioc_transfer *ioc; 358 359 /* Check type and command number */ 360 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC) 361 return -ENOTTY; 362 363 /* guard against device removal before, or while, 364 * we issue this ioctl. 365 */ 366 spidev = filp->private_data; 367 mutex_lock(&spidev->spi_lock); 368 spi = spi_dev_get(spidev->spi); 369 if (spi == NULL) { 370 mutex_unlock(&spidev->spi_lock); 371 return -ESHUTDOWN; 372 } 373 374 ctlr = spi->controller; 375 376 switch (cmd) { 377 /* read requests */ 378 case SPI_IOC_RD_MODE: 379 case SPI_IOC_RD_MODE32: 380 tmp = spi->mode & SPI_MODE_MASK; 381 382 if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0)) 383 tmp &= ~SPI_CS_HIGH; 384 385 if (cmd == SPI_IOC_RD_MODE) 386 retval = put_user(tmp, (__u8 __user *)arg); 387 else 388 retval = put_user(tmp, (__u32 __user *)arg); 389 break; 390 case SPI_IOC_RD_LSB_FIRST: 391 retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0, 392 (__u8 __user *)arg); 393 break; 394 case SPI_IOC_RD_BITS_PER_WORD: 395 retval = put_user(spi->bits_per_word, (__u8 __user *)arg); 396 break; 397 case SPI_IOC_RD_MAX_SPEED_HZ: 398 retval = put_user(spidev->speed_hz, (__u32 __user *)arg); 399 break; 400 401 /* write requests */ 402 case SPI_IOC_WR_MODE: 403 case SPI_IOC_WR_MODE32: 404 if (cmd == SPI_IOC_WR_MODE) 405 retval = get_user(tmp, (u8 __user *)arg); 406 else 407 retval = get_user(tmp, (u32 __user *)arg); 408 if (retval == 0) { 409 u32 save = spi->mode; 410 411 if (tmp & ~SPI_MODE_MASK) { 412 retval = -EINVAL; 413 break; 414 } 415 416 if (ctlr->use_gpio_descriptors && spi_get_csgpiod(spi, 0)) 417 tmp |= SPI_CS_HIGH; 418 419 tmp |= spi->mode & ~SPI_MODE_MASK; 420 spi->mode = tmp & SPI_MODE_USER_MASK; 421 retval = spi_setup(spi); 422 if (retval < 0) 423 spi->mode = save; 424 else 425 dev_dbg(&spi->dev, "spi mode %x\n", tmp); 426 } 427 break; 428 case SPI_IOC_WR_LSB_FIRST: 429 retval = get_user(tmp, (__u8 __user *)arg); 430 if (retval == 0) { 431 u32 save = spi->mode; 432 433 if (tmp) 434 spi->mode |= SPI_LSB_FIRST; 435 else 436 spi->mode &= ~SPI_LSB_FIRST; 437 retval = spi_setup(spi); 438 if (retval < 0) 439 spi->mode = save; 440 else 441 dev_dbg(&spi->dev, "%csb first\n", 442 tmp ? 'l' : 'm'); 443 } 444 break; 445 case SPI_IOC_WR_BITS_PER_WORD: 446 retval = get_user(tmp, (__u8 __user *)arg); 447 if (retval == 0) { 448 u8 save = spi->bits_per_word; 449 450 spi->bits_per_word = tmp; 451 retval = spi_setup(spi); 452 if (retval < 0) 453 spi->bits_per_word = save; 454 else 455 dev_dbg(&spi->dev, "%d bits per word\n", tmp); 456 } 457 break; 458 case SPI_IOC_WR_MAX_SPEED_HZ: { 459 u32 save; 460 461 retval = get_user(tmp, (__u32 __user *)arg); 462 if (retval) 463 break; 464 if (tmp == 0) { 465 retval = -EINVAL; 466 break; 467 } 468 469 save = spi->max_speed_hz; 470 471 spi->max_speed_hz = tmp; 472 retval = spi_setup(spi); 473 if (retval == 0) { 474 spidev->speed_hz = tmp; 475 dev_dbg(&spi->dev, "%d Hz (max)\n", spidev->speed_hz); 476 } 477 478 spi->max_speed_hz = save; 479 break; 480 } 481 default: 482 /* segmented and/or full-duplex I/O request */ 483 /* Check message and copy into scratch area */ 484 ioc = spidev_get_ioc_message(cmd, 485 (struct spi_ioc_transfer __user *)arg, &n_ioc); 486 if (IS_ERR(ioc)) { 487 retval = PTR_ERR(ioc); 488 break; 489 } 490 if (!ioc) 491 break; /* n_ioc is also 0 */ 492 493 /* translate to spi_message, execute */ 494 retval = spidev_message(spidev, ioc, n_ioc); 495 kfree(ioc); 496 break; 497 } 498 499 spi_dev_put(spi); 500 mutex_unlock(&spidev->spi_lock); 501 return retval; 502 } 503 504 #ifdef CONFIG_COMPAT 505 static long 506 spidev_compat_ioc_message(struct file *filp, unsigned int cmd, 507 unsigned long arg) 508 { 509 struct spi_ioc_transfer __user *u_ioc; 510 int retval = 0; 511 struct spidev_data *spidev; 512 struct spi_device *spi; 513 unsigned n_ioc, n; 514 struct spi_ioc_transfer *ioc; 515 516 u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg); 517 518 /* guard against device removal before, or while, 519 * we issue this ioctl. 520 */ 521 spidev = filp->private_data; 522 mutex_lock(&spidev->spi_lock); 523 spi = spi_dev_get(spidev->spi); 524 if (spi == NULL) { 525 mutex_unlock(&spidev->spi_lock); 526 return -ESHUTDOWN; 527 } 528 529 /* Check message and copy into scratch area */ 530 ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc); 531 if (IS_ERR(ioc)) { 532 retval = PTR_ERR(ioc); 533 goto done; 534 } 535 if (!ioc) 536 goto done; /* n_ioc is also 0 */ 537 538 /* Convert buffer pointers */ 539 for (n = 0; n < n_ioc; n++) { 540 ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf); 541 ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf); 542 } 543 544 /* translate to spi_message, execute */ 545 retval = spidev_message(spidev, ioc, n_ioc); 546 kfree(ioc); 547 548 done: 549 spi_dev_put(spi); 550 mutex_unlock(&spidev->spi_lock); 551 return retval; 552 } 553 554 static long 555 spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) 556 { 557 if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC 558 && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0)) 559 && _IOC_DIR(cmd) == _IOC_WRITE) 560 return spidev_compat_ioc_message(filp, cmd, arg); 561 562 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); 563 } 564 #else 565 #define spidev_compat_ioctl NULL 566 #endif /* CONFIG_COMPAT */ 567 568 static int spidev_open(struct inode *inode, struct file *filp) 569 { 570 struct spidev_data *spidev = NULL, *iter; 571 int status = -ENXIO; 572 573 mutex_lock(&device_list_lock); 574 575 list_for_each_entry(iter, &device_list, device_entry) { 576 if (iter->devt == inode->i_rdev) { 577 status = 0; 578 spidev = iter; 579 break; 580 } 581 } 582 583 if (!spidev) { 584 pr_debug("spidev: nothing for minor %d\n", iminor(inode)); 585 goto err_find_dev; 586 } 587 588 if (!spidev->tx_buffer) { 589 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL); 590 if (!spidev->tx_buffer) { 591 status = -ENOMEM; 592 goto err_find_dev; 593 } 594 } 595 596 if (!spidev->rx_buffer) { 597 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL); 598 if (!spidev->rx_buffer) { 599 status = -ENOMEM; 600 goto err_alloc_rx_buf; 601 } 602 } 603 604 spidev->users++; 605 filp->private_data = spidev; 606 stream_open(inode, filp); 607 608 mutex_unlock(&device_list_lock); 609 return 0; 610 611 err_alloc_rx_buf: 612 kfree(spidev->tx_buffer); 613 spidev->tx_buffer = NULL; 614 err_find_dev: 615 mutex_unlock(&device_list_lock); 616 return status; 617 } 618 619 static int spidev_release(struct inode *inode, struct file *filp) 620 { 621 struct spidev_data *spidev; 622 int dofree; 623 624 mutex_lock(&device_list_lock); 625 spidev = filp->private_data; 626 filp->private_data = NULL; 627 628 mutex_lock(&spidev->spi_lock); 629 /* ... after we unbound from the underlying device? */ 630 dofree = (spidev->spi == NULL); 631 mutex_unlock(&spidev->spi_lock); 632 633 /* last close? */ 634 spidev->users--; 635 if (!spidev->users) { 636 637 kfree(spidev->tx_buffer); 638 spidev->tx_buffer = NULL; 639 640 kfree(spidev->rx_buffer); 641 spidev->rx_buffer = NULL; 642 643 if (dofree) 644 kfree(spidev); 645 else 646 spidev->speed_hz = spidev->spi->max_speed_hz; 647 } 648 #ifdef CONFIG_SPI_SLAVE 649 if (!dofree) 650 spi_target_abort(spidev->spi); 651 #endif 652 mutex_unlock(&device_list_lock); 653 654 return 0; 655 } 656 657 static const struct file_operations spidev_fops = { 658 .owner = THIS_MODULE, 659 /* REVISIT switch to aio primitives, so that userspace 660 * gets more complete API coverage. It'll simplify things 661 * too, except for the locking. 662 */ 663 .write = spidev_write, 664 .read = spidev_read, 665 .unlocked_ioctl = spidev_ioctl, 666 .compat_ioctl = spidev_compat_ioctl, 667 .open = spidev_open, 668 .release = spidev_release, 669 }; 670 671 /*-------------------------------------------------------------------------*/ 672 673 /* The main reason to have this class is to make mdev/udev create the 674 * /dev/spidevB.C character device nodes exposing our userspace API. 675 * It also simplifies memory management. 676 */ 677 678 static const struct class spidev_class = { 679 .name = "spidev", 680 }; 681 682 /* 683 * The spi device ids are expected to match the device names of the 684 * spidev_dt_ids array below. Both arrays are kept in the same ordering. 685 */ 686 static const struct spi_device_id spidev_spi_ids[] = { 687 { .name = /* abb */ "spi-sensor" }, 688 { .name = /* arduino */ "unoq-mcu" }, 689 { .name = /* cisco */ "spi-petra" }, 690 { .name = /* dh */ "dhcom-board" }, 691 { .name = /* elgin */ "jg10309-01" }, 692 { .name = /* gocontroll */ "moduline-module-slot"}, 693 { .name = /* lineartechnology */ "ltc2488" }, 694 { .name = /* lwn */ "bk4" }, 695 { .name = /* lwn */ "bk4-spi" }, 696 { .name = /* menlo */ "m53cpld" }, 697 { .name = /* micron */ "spi-authenta" }, 698 { .name = /* rohm */ "bh2228fv" }, 699 { .name = /* rohm */ "dh2228fv" }, 700 { .name = /* semtech */ "sx1301" }, 701 { .name = /* silabs */ "em3581" }, 702 { .name = /* silabs */ "si3210" }, 703 {}, 704 }; 705 MODULE_DEVICE_TABLE(spi, spidev_spi_ids); 706 707 /* 708 * spidev should never be referenced in DT without a specific compatible string, 709 * it is a Linux implementation thing rather than a description of the hardware. 710 */ 711 static int spidev_of_check(struct device *dev) 712 { 713 if (device_property_match_string(dev, "compatible", "spidev") < 0) 714 return 0; 715 716 dev_err(dev, "spidev listed directly in DT is not supported\n"); 717 return -EINVAL; 718 } 719 720 static const struct of_device_id spidev_dt_ids[] = { 721 { .compatible = "abb,spi-sensor", .data = &spidev_of_check }, 722 { .compatible = "arduino,unoq-mcu", .data = &spidev_of_check }, 723 { .compatible = "cisco,spi-petra", .data = &spidev_of_check }, 724 { .compatible = "dh,dhcom-board", .data = &spidev_of_check }, 725 { .compatible = "elgin,jg10309-01", .data = &spidev_of_check }, 726 { .compatible = "gocontroll,moduline-module-slot", .data = &spidev_of_check}, 727 { .compatible = "lineartechnology,ltc2488", .data = &spidev_of_check }, 728 { .compatible = "lwn,bk4", .data = &spidev_of_check }, 729 { .compatible = "lwn,bk4-spi", .data = &spidev_of_check }, 730 { .compatible = "menlo,m53cpld", .data = &spidev_of_check }, 731 { .compatible = "micron,spi-authenta", .data = &spidev_of_check }, 732 { .compatible = "rohm,bh2228fv", .data = &spidev_of_check }, 733 { .compatible = "rohm,dh2228fv", .data = &spidev_of_check }, 734 { .compatible = "semtech,sx1301", .data = &spidev_of_check }, 735 { .compatible = "silabs,em3581", .data = &spidev_of_check }, 736 { .compatible = "silabs,si3210", .data = &spidev_of_check }, 737 {}, 738 }; 739 MODULE_DEVICE_TABLE(of, spidev_dt_ids); 740 741 /* Dummy SPI devices not to be used in production systems */ 742 static int spidev_acpi_check(struct device *dev) 743 { 744 dev_warn(dev, "do not use this driver in production systems!\n"); 745 return 0; 746 } 747 748 static const struct acpi_device_id spidev_acpi_ids[] = { 749 /* 750 * The ACPI SPT000* devices are only meant for development and 751 * testing. Systems used in production should have a proper ACPI 752 * description of the connected peripheral and they should also use 753 * a proper driver instead of poking directly to the SPI bus. 754 */ 755 { "SPT0001", (kernel_ulong_t)&spidev_acpi_check }, 756 { "SPT0002", (kernel_ulong_t)&spidev_acpi_check }, 757 { "SPT0003", (kernel_ulong_t)&spidev_acpi_check }, 758 {}, 759 }; 760 MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids); 761 762 /*-------------------------------------------------------------------------*/ 763 764 static int spidev_probe(struct spi_device *spi) 765 { 766 int (*match)(struct device *dev); 767 struct spidev_data *spidev; 768 int status; 769 unsigned long minor; 770 771 match = device_get_match_data(&spi->dev); 772 if (match) { 773 status = match(&spi->dev); 774 if (status) 775 return status; 776 } 777 778 /* Allocate driver data */ 779 spidev = kzalloc_obj(*spidev); 780 if (!spidev) 781 return -ENOMEM; 782 783 /* Initialize the driver data */ 784 spidev->spi = spi; 785 mutex_init(&spidev->spi_lock); 786 787 INIT_LIST_HEAD(&spidev->device_entry); 788 789 /* If we can allocate a minor number, hook up this device. 790 * Reusing minors is fine so long as udev or mdev is working. 791 */ 792 mutex_lock(&device_list_lock); 793 minor = find_first_zero_bit(minors, N_SPI_MINORS); 794 if (minor < N_SPI_MINORS) { 795 struct device *dev; 796 797 spidev->devt = MKDEV(SPIDEV_MAJOR, minor); 798 dev = device_create(&spidev_class, &spi->dev, spidev->devt, 799 spidev, "spidev%d.%d", 800 spi->controller->bus_num, spi_get_chipselect(spi, 0)); 801 status = PTR_ERR_OR_ZERO(dev); 802 } else { 803 dev_dbg(&spi->dev, "no minor number available!\n"); 804 status = -ENODEV; 805 } 806 if (status == 0) { 807 set_bit(minor, minors); 808 list_add(&spidev->device_entry, &device_list); 809 } 810 mutex_unlock(&device_list_lock); 811 812 spidev->speed_hz = spi->max_speed_hz; 813 814 if (status == 0) 815 spi_set_drvdata(spi, spidev); 816 else 817 kfree(spidev); 818 819 return status; 820 } 821 822 static void spidev_remove(struct spi_device *spi) 823 { 824 struct spidev_data *spidev = spi_get_drvdata(spi); 825 826 /* prevent new opens */ 827 mutex_lock(&device_list_lock); 828 /* make sure ops on existing fds can abort cleanly */ 829 mutex_lock(&spidev->spi_lock); 830 spidev->spi = NULL; 831 mutex_unlock(&spidev->spi_lock); 832 833 list_del(&spidev->device_entry); 834 device_destroy(&spidev_class, spidev->devt); 835 clear_bit(MINOR(spidev->devt), minors); 836 if (spidev->users == 0) 837 kfree(spidev); 838 mutex_unlock(&device_list_lock); 839 } 840 841 static struct spi_driver spidev_spi_driver = { 842 .driver = { 843 .name = "spidev", 844 .of_match_table = spidev_dt_ids, 845 .acpi_match_table = spidev_acpi_ids, 846 }, 847 .probe = spidev_probe, 848 .remove = spidev_remove, 849 .id_table = spidev_spi_ids, 850 851 /* NOTE: suspend/resume methods are not necessary here. 852 * We don't do anything except pass the requests to/from 853 * the underlying controller. The refrigerator handles 854 * most issues; the controller driver handles the rest. 855 */ 856 }; 857 858 /*-------------------------------------------------------------------------*/ 859 860 static int __init spidev_init(void) 861 { 862 int status; 863 864 /* Claim our 256 reserved device numbers. Then register a class 865 * that will key udev/mdev to add/remove /dev nodes. Last, register 866 * the driver which manages those device numbers. 867 */ 868 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops); 869 if (status < 0) 870 return status; 871 872 status = class_register(&spidev_class); 873 if (status) { 874 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); 875 return status; 876 } 877 878 status = spi_register_driver(&spidev_spi_driver); 879 if (status < 0) { 880 class_unregister(&spidev_class); 881 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); 882 } 883 return status; 884 } 885 module_init(spidev_init); 886 887 static void __exit spidev_exit(void) 888 { 889 spi_unregister_driver(&spidev_spi_driver); 890 class_unregister(&spidev_class); 891 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); 892 } 893 module_exit(spidev_exit); 894 895 MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>"); 896 MODULE_DESCRIPTION("User mode SPI device interface"); 897 MODULE_LICENSE("GPL"); 898 MODULE_ALIAS("spi:spidev"); 899