File Index
Files and directories
Sign in
FILE

spidev.c

File preview with syntax highlighting.

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