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