blob: be503a0e6ef73843c3a1a63cc647ebb09b4c4dab [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
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)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00008 */
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++) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200226 /* 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
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000231 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 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200246 rx_total += len_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 if (rx_total > bufsiz) {
248 status = -EMSGSIZE;
249 goto done;
250 }
251 k_tmp->rx_buf = rx_buf;
Olivier Deprez0e641232021-09-23 10:07:05 +0200252 rx_buf += len_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000253 }
254 if (u_tmp->tx_buf) {
255 /* this transfer needs space in TX bounce buffer */
Olivier Deprez0e641232021-09-23 10:07:05 +0200256 tx_total += len_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000257 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;
Olivier Deprez0e641232021-09-23 10:07:05 +0200266 tx_buf += len_aligned;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 }
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;
David Brazdil0f672f62019-12-10 10:32:29 +0000275 k_tmp->word_delay_usecs = u_tmp->word_delay_usecs;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276 if (!k_tmp->speed_hz)
277 k_tmp->speed_hz = spidev->speed_hz;
278#ifdef VERBOSE
279 dev_dbg(&spidev->spi->dev,
David Brazdil0f672f62019-12-10 10:32:29 +0000280 " xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n",
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000281 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,
David Brazdil0f672f62019-12-10 10:32:29 +0000287 u_tmp->word_delay_usecs,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000288 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 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200298 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
299 n;
300 n--, k_tmp++, u_tmp++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000301 if (u_tmp->rx_buf) {
302 if (copy_to_user((u8 __user *)
Olivier Deprez0e641232021-09-23 10:07:05 +0200303 (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304 u_tmp->len)) {
305 status = -EFAULT;
306 goto done;
307 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000308 }
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 retval = put_user(spi->mode & SPI_MODE_MASK,
377 (__u8 __user *)arg);
378 break;
379 case SPI_IOC_RD_MODE32:
380 retval = put_user(spi->mode & SPI_MODE_MASK,
381 (__u32 __user *)arg);
382 break;
383 case SPI_IOC_RD_LSB_FIRST:
384 retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
385 (__u8 __user *)arg);
386 break;
387 case SPI_IOC_RD_BITS_PER_WORD:
388 retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
389 break;
390 case SPI_IOC_RD_MAX_SPEED_HZ:
391 retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
392 break;
393
394 /* write requests */
395 case SPI_IOC_WR_MODE:
396 case SPI_IOC_WR_MODE32:
397 if (cmd == SPI_IOC_WR_MODE)
398 retval = get_user(tmp, (u8 __user *)arg);
399 else
400 retval = get_user(tmp, (u32 __user *)arg);
401 if (retval == 0) {
Olivier Deprez0e641232021-09-23 10:07:05 +0200402 struct spi_controller *ctlr = spi->controller;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000403 u32 save = spi->mode;
404
405 if (tmp & ~SPI_MODE_MASK) {
406 retval = -EINVAL;
407 break;
408 }
409
Olivier Deprez0e641232021-09-23 10:07:05 +0200410 if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
411 ctlr->cs_gpiods[spi->chip_select])
412 tmp |= SPI_CS_HIGH;
413
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000414 tmp |= spi->mode & ~SPI_MODE_MASK;
415 spi->mode = (u16)tmp;
416 retval = spi_setup(spi);
417 if (retval < 0)
418 spi->mode = save;
419 else
420 dev_dbg(&spi->dev, "spi mode %x\n", tmp);
421 }
422 break;
423 case SPI_IOC_WR_LSB_FIRST:
424 retval = get_user(tmp, (__u8 __user *)arg);
425 if (retval == 0) {
426 u32 save = spi->mode;
427
428 if (tmp)
429 spi->mode |= SPI_LSB_FIRST;
430 else
431 spi->mode &= ~SPI_LSB_FIRST;
432 retval = spi_setup(spi);
433 if (retval < 0)
434 spi->mode = save;
435 else
436 dev_dbg(&spi->dev, "%csb first\n",
437 tmp ? 'l' : 'm');
438 }
439 break;
440 case SPI_IOC_WR_BITS_PER_WORD:
441 retval = get_user(tmp, (__u8 __user *)arg);
442 if (retval == 0) {
443 u8 save = spi->bits_per_word;
444
445 spi->bits_per_word = tmp;
446 retval = spi_setup(spi);
447 if (retval < 0)
448 spi->bits_per_word = save;
449 else
450 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
451 }
452 break;
453 case SPI_IOC_WR_MAX_SPEED_HZ:
454 retval = get_user(tmp, (__u32 __user *)arg);
455 if (retval == 0) {
456 u32 save = spi->max_speed_hz;
457
458 spi->max_speed_hz = tmp;
459 retval = spi_setup(spi);
460 if (retval >= 0)
461 spidev->speed_hz = tmp;
462 else
463 dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
464 spi->max_speed_hz = save;
465 }
466 break;
467
468 default:
469 /* segmented and/or full-duplex I/O request */
470 /* Check message and copy into scratch area */
471 ioc = spidev_get_ioc_message(cmd,
472 (struct spi_ioc_transfer __user *)arg, &n_ioc);
473 if (IS_ERR(ioc)) {
474 retval = PTR_ERR(ioc);
475 break;
476 }
477 if (!ioc)
478 break; /* n_ioc is also 0 */
479
480 /* translate to spi_message, execute */
481 retval = spidev_message(spidev, ioc, n_ioc);
482 kfree(ioc);
483 break;
484 }
485
486 mutex_unlock(&spidev->buf_lock);
487 spi_dev_put(spi);
488 return retval;
489}
490
491#ifdef CONFIG_COMPAT
492static long
493spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
494 unsigned long arg)
495{
496 struct spi_ioc_transfer __user *u_ioc;
497 int retval = 0;
498 struct spidev_data *spidev;
499 struct spi_device *spi;
500 unsigned n_ioc, n;
501 struct spi_ioc_transfer *ioc;
502
503 u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
504
505 /* guard against device removal before, or while,
506 * we issue this ioctl.
507 */
508 spidev = filp->private_data;
509 spin_lock_irq(&spidev->spi_lock);
510 spi = spi_dev_get(spidev->spi);
511 spin_unlock_irq(&spidev->spi_lock);
512
513 if (spi == NULL)
514 return -ESHUTDOWN;
515
516 /* SPI_IOC_MESSAGE needs the buffer locked "normally" */
517 mutex_lock(&spidev->buf_lock);
518
519 /* Check message and copy into scratch area */
520 ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc);
521 if (IS_ERR(ioc)) {
522 retval = PTR_ERR(ioc);
523 goto done;
524 }
525 if (!ioc)
526 goto done; /* n_ioc is also 0 */
527
528 /* Convert buffer pointers */
529 for (n = 0; n < n_ioc; n++) {
530 ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
531 ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
532 }
533
534 /* translate to spi_message, execute */
535 retval = spidev_message(spidev, ioc, n_ioc);
536 kfree(ioc);
537
538done:
539 mutex_unlock(&spidev->buf_lock);
540 spi_dev_put(spi);
541 return retval;
542}
543
544static long
545spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
546{
547 if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
548 && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
549 && _IOC_DIR(cmd) == _IOC_WRITE)
550 return spidev_compat_ioc_message(filp, cmd, arg);
551
552 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
553}
554#else
555#define spidev_compat_ioctl NULL
556#endif /* CONFIG_COMPAT */
557
558static int spidev_open(struct inode *inode, struct file *filp)
559{
560 struct spidev_data *spidev;
561 int status = -ENXIO;
562
563 mutex_lock(&device_list_lock);
564
565 list_for_each_entry(spidev, &device_list, device_entry) {
566 if (spidev->devt == inode->i_rdev) {
567 status = 0;
568 break;
569 }
570 }
571
572 if (status) {
573 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
574 goto err_find_dev;
575 }
576
577 if (!spidev->tx_buffer) {
578 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
579 if (!spidev->tx_buffer) {
580 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
581 status = -ENOMEM;
582 goto err_find_dev;
583 }
584 }
585
586 if (!spidev->rx_buffer) {
587 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
588 if (!spidev->rx_buffer) {
589 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
590 status = -ENOMEM;
591 goto err_alloc_rx_buf;
592 }
593 }
594
595 spidev->users++;
596 filp->private_data = spidev;
David Brazdil0f672f62019-12-10 10:32:29 +0000597 stream_open(inode, filp);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000598
599 mutex_unlock(&device_list_lock);
600 return 0;
601
602err_alloc_rx_buf:
603 kfree(spidev->tx_buffer);
604 spidev->tx_buffer = NULL;
605err_find_dev:
606 mutex_unlock(&device_list_lock);
607 return status;
608}
609
610static int spidev_release(struct inode *inode, struct file *filp)
611{
612 struct spidev_data *spidev;
Olivier Deprez0e641232021-09-23 10:07:05 +0200613 int dofree;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614
615 mutex_lock(&device_list_lock);
616 spidev = filp->private_data;
617 filp->private_data = NULL;
618
Olivier Deprez0e641232021-09-23 10:07:05 +0200619 spin_lock_irq(&spidev->spi_lock);
620 /* ... after we unbound from the underlying device? */
621 dofree = (spidev->spi == NULL);
622 spin_unlock_irq(&spidev->spi_lock);
623
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000624 /* last close? */
625 spidev->users--;
626 if (!spidev->users) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000627
628 kfree(spidev->tx_buffer);
629 spidev->tx_buffer = NULL;
630
631 kfree(spidev->rx_buffer);
632 spidev->rx_buffer = NULL;
633
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000634 if (dofree)
635 kfree(spidev);
Olivier Deprez0e641232021-09-23 10:07:05 +0200636 else
637 spidev->speed_hz = spidev->spi->max_speed_hz;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000638 }
Olivier Deprez0e641232021-09-23 10:07:05 +0200639#ifdef CONFIG_SPI_SLAVE
640 if (!dofree)
641 spi_slave_abort(spidev->spi);
642#endif
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000643 mutex_unlock(&device_list_lock);
644
645 return 0;
646}
647
648static const struct file_operations spidev_fops = {
649 .owner = THIS_MODULE,
650 /* REVISIT switch to aio primitives, so that userspace
651 * gets more complete API coverage. It'll simplify things
652 * too, except for the locking.
653 */
654 .write = spidev_write,
655 .read = spidev_read,
656 .unlocked_ioctl = spidev_ioctl,
657 .compat_ioctl = spidev_compat_ioctl,
658 .open = spidev_open,
659 .release = spidev_release,
660 .llseek = no_llseek,
661};
662
663/*-------------------------------------------------------------------------*/
664
665/* The main reason to have this class is to make mdev/udev create the
666 * /dev/spidevB.C character device nodes exposing our userspace API.
667 * It also simplifies memory management.
668 */
669
670static struct class *spidev_class;
671
672#ifdef CONFIG_OF
673static const struct of_device_id spidev_dt_ids[] = {
674 { .compatible = "rohm,dh2228fv" },
675 { .compatible = "lineartechnology,ltc2488" },
676 { .compatible = "ge,achc" },
677 { .compatible = "semtech,sx1301" },
David Brazdil0f672f62019-12-10 10:32:29 +0000678 { .compatible = "lwn,bk4" },
679 { .compatible = "dh,dhcom-board" },
680 { .compatible = "menlo,m53cpld" },
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000681 {},
682};
683MODULE_DEVICE_TABLE(of, spidev_dt_ids);
684#endif
685
686#ifdef CONFIG_ACPI
687
688/* Dummy SPI devices not to be used in production systems */
689#define SPIDEV_ACPI_DUMMY 1
690
691static const struct acpi_device_id spidev_acpi_ids[] = {
692 /*
693 * The ACPI SPT000* devices are only meant for development and
694 * testing. Systems used in production should have a proper ACPI
695 * description of the connected peripheral and they should also use
696 * a proper driver instead of poking directly to the SPI bus.
697 */
698 { "SPT0001", SPIDEV_ACPI_DUMMY },
699 { "SPT0002", SPIDEV_ACPI_DUMMY },
700 { "SPT0003", SPIDEV_ACPI_DUMMY },
701 {},
702};
703MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids);
704
705static void spidev_probe_acpi(struct spi_device *spi)
706{
707 const struct acpi_device_id *id;
708
709 if (!has_acpi_companion(&spi->dev))
710 return;
711
712 id = acpi_match_device(spidev_acpi_ids, &spi->dev);
713 if (WARN_ON(!id))
714 return;
715
716 if (id->driver_data == SPIDEV_ACPI_DUMMY)
717 dev_warn(&spi->dev, "do not use this driver in production systems!\n");
718}
719#else
720static inline void spidev_probe_acpi(struct spi_device *spi) {}
721#endif
722
723/*-------------------------------------------------------------------------*/
724
725static int spidev_probe(struct spi_device *spi)
726{
727 struct spidev_data *spidev;
728 int status;
729 unsigned long minor;
730
731 /*
732 * spidev should never be referenced in DT without a specific
733 * compatible string, it is a Linux implementation thing
734 * rather than a description of the hardware.
735 */
David Brazdil0f672f62019-12-10 10:32:29 +0000736 WARN(spi->dev.of_node &&
737 of_device_is_compatible(spi->dev.of_node, "spidev"),
738 "%pOF: buggy DT: spidev listed directly in DT\n", spi->dev.of_node);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000739
740 spidev_probe_acpi(spi);
741
742 /* Allocate driver data */
743 spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
744 if (!spidev)
745 return -ENOMEM;
746
747 /* Initialize the driver data */
748 spidev->spi = spi;
749 spin_lock_init(&spidev->spi_lock);
750 mutex_init(&spidev->buf_lock);
751
752 INIT_LIST_HEAD(&spidev->device_entry);
753
754 /* If we can allocate a minor number, hook up this device.
755 * Reusing minors is fine so long as udev or mdev is working.
756 */
757 mutex_lock(&device_list_lock);
758 minor = find_first_zero_bit(minors, N_SPI_MINORS);
759 if (minor < N_SPI_MINORS) {
760 struct device *dev;
761
762 spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
763 dev = device_create(spidev_class, &spi->dev, spidev->devt,
764 spidev, "spidev%d.%d",
765 spi->master->bus_num, spi->chip_select);
766 status = PTR_ERR_OR_ZERO(dev);
767 } else {
768 dev_dbg(&spi->dev, "no minor number available!\n");
769 status = -ENODEV;
770 }
771 if (status == 0) {
772 set_bit(minor, minors);
773 list_add(&spidev->device_entry, &device_list);
774 }
775 mutex_unlock(&device_list_lock);
776
777 spidev->speed_hz = spi->max_speed_hz;
778
779 if (status == 0)
780 spi_set_drvdata(spi, spidev);
781 else
782 kfree(spidev);
783
784 return status;
785}
786
787static int spidev_remove(struct spi_device *spi)
788{
789 struct spidev_data *spidev = spi_get_drvdata(spi);
790
Olivier Deprez0e641232021-09-23 10:07:05 +0200791 /* prevent new opens */
792 mutex_lock(&device_list_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000793 /* make sure ops on existing fds can abort cleanly */
794 spin_lock_irq(&spidev->spi_lock);
795 spidev->spi = NULL;
796 spin_unlock_irq(&spidev->spi_lock);
797
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000798 list_del(&spidev->device_entry);
799 device_destroy(spidev_class, spidev->devt);
800 clear_bit(MINOR(spidev->devt), minors);
801 if (spidev->users == 0)
802 kfree(spidev);
803 mutex_unlock(&device_list_lock);
804
805 return 0;
806}
807
808static struct spi_driver spidev_spi_driver = {
809 .driver = {
810 .name = "spidev",
811 .of_match_table = of_match_ptr(spidev_dt_ids),
812 .acpi_match_table = ACPI_PTR(spidev_acpi_ids),
813 },
814 .probe = spidev_probe,
815 .remove = spidev_remove,
816
817 /* NOTE: suspend/resume methods are not necessary here.
818 * We don't do anything except pass the requests to/from
819 * the underlying controller. The refrigerator handles
820 * most issues; the controller driver handles the rest.
821 */
822};
823
824/*-------------------------------------------------------------------------*/
825
826static int __init spidev_init(void)
827{
828 int status;
829
830 /* Claim our 256 reserved device numbers. Then register a class
831 * that will key udev/mdev to add/remove /dev nodes. Last, register
832 * the driver which manages those device numbers.
833 */
834 BUILD_BUG_ON(N_SPI_MINORS > 256);
835 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
836 if (status < 0)
837 return status;
838
839 spidev_class = class_create(THIS_MODULE, "spidev");
840 if (IS_ERR(spidev_class)) {
841 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
842 return PTR_ERR(spidev_class);
843 }
844
845 status = spi_register_driver(&spidev_spi_driver);
846 if (status < 0) {
847 class_destroy(spidev_class);
848 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
849 }
850 return status;
851}
852module_init(spidev_init);
853
854static void __exit spidev_exit(void)
855{
856 spi_unregister_driver(&spidev_spi_driver);
857 class_destroy(spidev_class);
858 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
859}
860module_exit(spidev_exit);
861
862MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
863MODULE_DESCRIPTION("User mode SPI device interface");
864MODULE_LICENSE("GPL");
865MODULE_ALIAS("spi:spidev");