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