spidev.c 17.6 KB
Newer Older
A
Andrea Paterniani 已提交
1
/*
G
Grant Likely 已提交
2
 * Simple synchronous userspace interface to SPI devices
A
Andrea Paterniani 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 *
 * Copyright (C) 2006 SWAPP
 *	Andrea Paterniani <a.paterniani@swapp-eng.it>
 * Copyright (C) 2007 David Brownell (simplification, cleanup)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/ioctl.h>
#include <linux/fs.h>
#include <linux/device.h>
28
#include <linux/err.h>
A
Andrea Paterniani 已提交
29 30 31 32
#include <linux/list.h>
#include <linux/errno.h>
#include <linux/mutex.h>
#include <linux/slab.h>
33
#include <linux/compat.h>
34 35
#include <linux/of.h>
#include <linux/of_device.h>
A
Andrea Paterniani 已提交
36 37 38 39 40 41 42 43

#include <linux/spi/spi.h>
#include <linux/spi/spidev.h>

#include <asm/uaccess.h>


/*
44
 * This supports access to SPI devices using normal userspace I/O calls.
A
Andrea Paterniani 已提交
45 46
 * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
 * and often mask message boundaries, full SPI support requires full duplex
47
 * transfers.  There are several kinds of internal message boundaries to
A
Andrea Paterniani 已提交
48 49 50 51 52 53 54 55 56 57 58
 * handle chipselect management and other protocol options.
 *
 * SPI has a character major number assigned.  We allocate minor numbers
 * dynamically using a bitmask.  You must use hotplug tools, such as udev
 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
 * nodes, since there is no fixed association of minor numbers with any
 * particular SPI bus or device.
 */
#define SPIDEV_MAJOR			153	/* assigned */
#define N_SPI_MINORS			32	/* ... up to 256 */

59
static DECLARE_BITMAP(minors, N_SPI_MINORS);
A
Andrea Paterniani 已提交
60 61


62
/* Bit masks for spi_device.mode management.  Note that incorrect
D
David Brownell 已提交
63 64
 * settings for some settings can cause *lots* of trouble for other
 * devices on a shared bus:
65
 *
D
David Brownell 已提交
66 67 68 69 70 71 72
 *  - CS_HIGH ... this device will be active when it shouldn't be
 *  - 3WIRE ... when active, it won't behave as it should
 *  - NO_CS ... there will be no explicit message boundaries; this
 *	is completely incompatible with the shared bus model
 *  - READY ... transfers may proceed when they shouldn't.
 *
 * REVISIT should changing those flags be privileged?
73 74
 */
#define SPI_MODE_MASK		(SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
D
David Brownell 已提交
75 76
				| SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
				| SPI_NO_CS | SPI_READY)
A
Andrea Paterniani 已提交
77 78

struct spidev_data {
79
	dev_t			devt;
80
	spinlock_t		spi_lock;
A
Andrea Paterniani 已提交
81 82 83
	struct spi_device	*spi;
	struct list_head	device_entry;

84
	/* buffer is NULL unless this device is open (users > 0) */
A
Andrea Paterniani 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98
	struct mutex		buf_lock;
	unsigned		users;
	u8			*buffer;
};

static LIST_HEAD(device_list);
static DEFINE_MUTEX(device_list_lock);

static unsigned bufsiz = 4096;
module_param(bufsiz, uint, S_IRUGO);
MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");

/*-------------------------------------------------------------------------*/

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
/*
 * We can't use the standard synchronous wrappers for file I/O; we
 * need to protect against async removal of the underlying spi_device.
 */
static void spidev_complete(void *arg)
{
	complete(arg);
}

static ssize_t
spidev_sync(struct spidev_data *spidev, struct spi_message *message)
{
	DECLARE_COMPLETION_ONSTACK(done);
	int status;

	message->complete = spidev_complete;
	message->context = &done;

	spin_lock_irq(&spidev->spi_lock);
	if (spidev->spi == NULL)
		status = -ESHUTDOWN;
	else
		status = spi_async(spidev->spi, message);
	spin_unlock_irq(&spidev->spi_lock);

	if (status == 0) {
		wait_for_completion(&done);
		status = message->status;
		if (status == 0)
			status = message->actual_length;
	}
	return status;
}

static inline ssize_t
spidev_sync_write(struct spidev_data *spidev, size_t len)
{
	struct spi_transfer	t = {
			.tx_buf		= spidev->buffer,
			.len		= len,
		};
	struct spi_message	m;

	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
	return spidev_sync(spidev, &m);
}

static inline ssize_t
spidev_sync_read(struct spidev_data *spidev, size_t len)
{
	struct spi_transfer	t = {
			.rx_buf		= spidev->buffer,
			.len		= len,
		};
	struct spi_message	m;

	spi_message_init(&m);
	spi_message_add_tail(&t, &m);
	return spidev_sync(spidev, &m);
}

/*-------------------------------------------------------------------------*/

A
Andrea Paterniani 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175 176
/* Read-only message with current device setup */
static ssize_t
spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	struct spidev_data	*spidev;
	ssize_t			status = 0;

	/* chipselect only toggles at start or end of operation */
	if (count > bufsiz)
		return -EMSGSIZE;

	spidev = filp->private_data;

	mutex_lock(&spidev->buf_lock);
177
	status = spidev_sync_read(spidev, count);
178
	if (status > 0) {
A
Andrea Paterniani 已提交
179 180
		unsigned long	missing;

181 182
		missing = copy_to_user(buf, spidev->buffer, status);
		if (missing == status)
A
Andrea Paterniani 已提交
183 184
			status = -EFAULT;
		else
185
			status = status - missing;
A
Andrea Paterniani 已提交
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
	}
	mutex_unlock(&spidev->buf_lock);

	return status;
}

/* Write-only message with current device setup */
static ssize_t
spidev_write(struct file *filp, const char __user *buf,
		size_t count, loff_t *f_pos)
{
	struct spidev_data	*spidev;
	ssize_t			status = 0;
	unsigned long		missing;

	/* chipselect only toggles at start or end of operation */
	if (count > bufsiz)
		return -EMSGSIZE;

	spidev = filp->private_data;

	mutex_lock(&spidev->buf_lock);
	missing = copy_from_user(spidev->buffer, buf, count);
	if (missing == 0) {
210
		status = spidev_sync_write(spidev, count);
A
Andrea Paterniani 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
	} else
		status = -EFAULT;
	mutex_unlock(&spidev->buf_lock);

	return status;
}

static int spidev_message(struct spidev_data *spidev,
		struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
{
	struct spi_message	msg;
	struct spi_transfer	*k_xfers;
	struct spi_transfer	*k_tmp;
	struct spi_ioc_transfer *u_tmp;
	unsigned		n, total;
	u8			*buf;
	int			status = -EFAULT;

	spi_message_init(&msg);
	k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
	if (k_xfers == NULL)
		return -ENOMEM;

	/* Construct spi_message, copying any tx data to bounce buffer.
	 * We walk the array of user-provided transfers, using each one
	 * to initialize a kernel version of the same transfer.
	 */
	buf = spidev->buffer;
	total = 0;
	for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
			n;
			n--, k_tmp++, u_tmp++) {
		k_tmp->len = u_tmp->len;

245 246 247 248 249 250
		total += k_tmp->len;
		if (total > bufsiz) {
			status = -EMSGSIZE;
			goto done;
		}

A
Andrea Paterniani 已提交
251 252
		if (u_tmp->rx_buf) {
			k_tmp->rx_buf = buf;
D
David Brownell 已提交
253
			if (!access_ok(VERIFY_WRITE, (u8 __user *)
A
Al Viro 已提交
254
						(uintptr_t) u_tmp->rx_buf,
D
David Brownell 已提交
255
						u_tmp->len))
A
Andrea Paterniani 已提交
256 257 258 259
				goto done;
		}
		if (u_tmp->tx_buf) {
			k_tmp->tx_buf = buf;
D
David Brownell 已提交
260
			if (copy_from_user(buf, (const u8 __user *)
A
Al Viro 已提交
261
						(uintptr_t) u_tmp->tx_buf,
A
Andrea Paterniani 已提交
262 263 264 265 266 267 268 269 270 271
					u_tmp->len))
				goto done;
		}
		buf += k_tmp->len;

		k_tmp->cs_change = !!u_tmp->cs_change;
		k_tmp->bits_per_word = u_tmp->bits_per_word;
		k_tmp->delay_usecs = u_tmp->delay_usecs;
		k_tmp->speed_hz = u_tmp->speed_hz;
#ifdef VERBOSE
272
		dev_dbg(&spidev->spi->dev,
A
Andrea Paterniani 已提交
273 274 275 276 277
			"  xfer len %zd %s%s%s%dbits %u usec %uHz\n",
			u_tmp->len,
			u_tmp->rx_buf ? "rx " : "",
			u_tmp->tx_buf ? "tx " : "",
			u_tmp->cs_change ? "cs " : "",
278
			u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
A
Andrea Paterniani 已提交
279
			u_tmp->delay_usecs,
280
			u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
A
Andrea Paterniani 已提交
281 282 283 284
#endif
		spi_message_add_tail(k_tmp, &msg);
	}

285
	status = spidev_sync(spidev, &msg);
A
Andrea Paterniani 已提交
286 287 288 289 290 291 292
	if (status < 0)
		goto done;

	/* copy any rx data out of bounce buffer */
	buf = spidev->buffer;
	for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
		if (u_tmp->rx_buf) {
D
David Brownell 已提交
293
			if (__copy_to_user((u8 __user *)
A
Al Viro 已提交
294
					(uintptr_t) u_tmp->rx_buf, buf,
A
Andrea Paterniani 已提交
295 296 297 298 299 300 301 302 303 304 305 306 307 308
					u_tmp->len)) {
				status = -EFAULT;
				goto done;
			}
		}
		buf += u_tmp->len;
	}
	status = total;

done:
	kfree(k_xfers);
	return status;
}

A
Alan Cox 已提交
309 310
static long
spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
A
Andrea Paterniani 已提交
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
{
	int			err = 0;
	int			retval = 0;
	struct spidev_data	*spidev;
	struct spi_device	*spi;
	u32			tmp;
	unsigned		n_ioc;
	struct spi_ioc_transfer	*ioc;

	/* Check type and command number */
	if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
		return -ENOTTY;

	/* Check access direction once here; don't repeat below.
	 * IOC_DIR is from the user perspective, while access_ok is
	 * from the kernel perspective; so they look reversed.
	 */
	if (_IOC_DIR(cmd) & _IOC_READ)
		err = !access_ok(VERIFY_WRITE,
				(void __user *)arg, _IOC_SIZE(cmd));
	if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
		err = !access_ok(VERIFY_READ,
				(void __user *)arg, _IOC_SIZE(cmd));
	if (err)
		return -EFAULT;

337 338 339
	/* guard against device removal before, or while,
	 * we issue this ioctl.
	 */
A
Andrea Paterniani 已提交
340
	spidev = filp->private_data;
341 342 343 344 345 346
	spin_lock_irq(&spidev->spi_lock);
	spi = spi_dev_get(spidev->spi);
	spin_unlock_irq(&spidev->spi_lock);

	if (spi == NULL)
		return -ESHUTDOWN;
A
Andrea Paterniani 已提交
347

A
Alan Cox 已提交
348 349 350 351 352 353 354 355
	/* use the buffer lock here for triple duty:
	 *  - prevent I/O (from us) so calling spi_setup() is safe;
	 *  - prevent concurrent SPI_IOC_WR_* from morphing
	 *    data fields while SPI_IOC_RD_* reads them;
	 *  - SPI_IOC_MESSAGE needs the buffer locked "normally".
	 */
	mutex_lock(&spidev->buf_lock);

A
Andrea Paterniani 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
	switch (cmd) {
	/* read requests */
	case SPI_IOC_RD_MODE:
		retval = __put_user(spi->mode & SPI_MODE_MASK,
					(__u8 __user *)arg);
		break;
	case SPI_IOC_RD_LSB_FIRST:
		retval = __put_user((spi->mode & SPI_LSB_FIRST) ?  1 : 0,
					(__u8 __user *)arg);
		break;
	case SPI_IOC_RD_BITS_PER_WORD:
		retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
		break;
	case SPI_IOC_RD_MAX_SPEED_HZ:
		retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
		break;

	/* write requests */
	case SPI_IOC_WR_MODE:
		retval = __get_user(tmp, (u8 __user *)arg);
		if (retval == 0) {
			u8	save = spi->mode;

			if (tmp & ~SPI_MODE_MASK) {
				retval = -EINVAL;
				break;
			}

			tmp |= spi->mode & ~SPI_MODE_MASK;
			spi->mode = (u8)tmp;
			retval = spi_setup(spi);
			if (retval < 0)
				spi->mode = save;
			else
				dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
		}
		break;
	case SPI_IOC_WR_LSB_FIRST:
		retval = __get_user(tmp, (__u8 __user *)arg);
		if (retval == 0) {
			u8	save = spi->mode;

			if (tmp)
				spi->mode |= SPI_LSB_FIRST;
			else
				spi->mode &= ~SPI_LSB_FIRST;
			retval = spi_setup(spi);
			if (retval < 0)
				spi->mode = save;
			else
				dev_dbg(&spi->dev, "%csb first\n",
						tmp ? 'l' : 'm');
		}
		break;
	case SPI_IOC_WR_BITS_PER_WORD:
		retval = __get_user(tmp, (__u8 __user *)arg);
		if (retval == 0) {
			u8	save = spi->bits_per_word;

			spi->bits_per_word = tmp;
			retval = spi_setup(spi);
			if (retval < 0)
				spi->bits_per_word = save;
			else
				dev_dbg(&spi->dev, "%d bits per word\n", tmp);
		}
		break;
	case SPI_IOC_WR_MAX_SPEED_HZ:
		retval = __get_user(tmp, (__u32 __user *)arg);
		if (retval == 0) {
			u32	save = spi->max_speed_hz;

			spi->max_speed_hz = tmp;
			retval = spi_setup(spi);
			if (retval < 0)
				spi->max_speed_hz = save;
			else
				dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
		}
		break;

	default:
		/* segmented and/or full-duplex I/O request */
		if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
440 441 442 443
				|| _IOC_DIR(cmd) != _IOC_WRITE) {
			retval = -ENOTTY;
			break;
		}
A
Andrea Paterniani 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460

		tmp = _IOC_SIZE(cmd);
		if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
			retval = -EINVAL;
			break;
		}
		n_ioc = tmp / sizeof(struct spi_ioc_transfer);
		if (n_ioc == 0)
			break;

		/* copy into scratch area */
		ioc = kmalloc(tmp, GFP_KERNEL);
		if (!ioc) {
			retval = -ENOMEM;
			break;
		}
		if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
461
			kfree(ioc);
A
Andrea Paterniani 已提交
462 463 464 465 466 467 468 469 470
			retval = -EFAULT;
			break;
		}

		/* translate to spi_message, execute */
		retval = spidev_message(spidev, ioc, n_ioc);
		kfree(ioc);
		break;
	}
A
Alan Cox 已提交
471 472

	mutex_unlock(&spidev->buf_lock);
473
	spi_dev_put(spi);
A
Andrea Paterniani 已提交
474 475 476
	return retval;
}

477 478 479 480 481 482 483 484 485 486
#ifdef CONFIG_COMPAT
static long
spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
	return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
}
#else
#define spidev_compat_ioctl NULL
#endif /* CONFIG_COMPAT */

A
Andrea Paterniani 已提交
487 488 489 490 491 492 493 494
static int spidev_open(struct inode *inode, struct file *filp)
{
	struct spidev_data	*spidev;
	int			status = -ENXIO;

	mutex_lock(&device_list_lock);

	list_for_each_entry(spidev, &device_list, device_entry) {
495
		if (spidev->devt == inode->i_rdev) {
A
Andrea Paterniani 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
			status = 0;
			break;
		}
	}
	if (status == 0) {
		if (!spidev->buffer) {
			spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
			if (!spidev->buffer) {
				dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
				status = -ENOMEM;
			}
		}
		if (status == 0) {
			spidev->users++;
			filp->private_data = spidev;
			nonseekable_open(inode, filp);
		}
	} else
		pr_debug("spidev: nothing for minor %d\n", iminor(inode));

	mutex_unlock(&device_list_lock);
	return status;
}

static int spidev_release(struct inode *inode, struct file *filp)
{
	struct spidev_data	*spidev;
	int			status = 0;

	mutex_lock(&device_list_lock);
	spidev = filp->private_data;
	filp->private_data = NULL;
528 529

	/* last close? */
A
Andrea Paterniani 已提交
530 531
	spidev->users--;
	if (!spidev->users) {
532 533
		int		dofree;

A
Andrea Paterniani 已提交
534 535
		kfree(spidev->buffer);
		spidev->buffer = NULL;
536 537 538 539 540 541 542 543

		/* ... after we unbound from the underlying device? */
		spin_lock_irq(&spidev->spi_lock);
		dofree = (spidev->spi == NULL);
		spin_unlock_irq(&spidev->spi_lock);

		if (dofree)
			kfree(spidev);
A
Andrea Paterniani 已提交
544 545 546 547 548 549
	}
	mutex_unlock(&device_list_lock);

	return status;
}

550
static const struct file_operations spidev_fops = {
A
Andrea Paterniani 已提交
551 552 553 554 555 556 557
	.owner =	THIS_MODULE,
	/* REVISIT switch to aio primitives, so that userspace
	 * gets more complete API coverage.  It'll simplify things
	 * too, except for the locking.
	 */
	.write =	spidev_write,
	.read =		spidev_read,
A
Alan Cox 已提交
558
	.unlocked_ioctl = spidev_ioctl,
559
	.compat_ioctl = spidev_compat_ioctl,
A
Andrea Paterniani 已提交
560 561
	.open =		spidev_open,
	.release =	spidev_release,
562
	.llseek =	no_llseek,
A
Andrea Paterniani 已提交
563 564 565 566 567 568 569 570 571
};

/*-------------------------------------------------------------------------*/

/* The main reason to have this class is to make mdev/udev create the
 * /dev/spidevB.C character device nodes exposing our userspace API.
 * It also simplifies memory management.
 */

572
static struct class *spidev_class;
A
Andrea Paterniani 已提交
573 574 575

/*-------------------------------------------------------------------------*/

576
static int spidev_probe(struct spi_device *spi)
A
Andrea Paterniani 已提交
577 578 579 580 581 582 583 584 585 586 587 588
{
	struct spidev_data	*spidev;
	int			status;
	unsigned long		minor;

	/* Allocate driver data */
	spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
	if (!spidev)
		return -ENOMEM;

	/* Initialize the driver data */
	spidev->spi = spi;
589
	spin_lock_init(&spidev->spi_lock);
A
Andrea Paterniani 已提交
590 591 592 593 594 595 596 597
	mutex_init(&spidev->buf_lock);

	INIT_LIST_HEAD(&spidev->device_entry);

	/* If we can allocate a minor number, hook up this device.
	 * Reusing minors is fine so long as udev or mdev is working.
	 */
	mutex_lock(&device_list_lock);
598
	minor = find_first_zero_bit(minors, N_SPI_MINORS);
A
Andrea Paterniani 已提交
599
	if (minor < N_SPI_MINORS) {
600 601 602
		struct device *dev;

		spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
603 604 605
		dev = device_create(spidev_class, &spi->dev, spidev->devt,
				    spidev, "spidev%d.%d",
				    spi->master->bus_num, spi->chip_select);
606
		status = PTR_ERR_OR_ZERO(dev);
A
Andrea Paterniani 已提交
607 608 609 610 611 612 613 614 615 616
	} else {
		dev_dbg(&spi->dev, "no minor number available!\n");
		status = -ENODEV;
	}
	if (status == 0) {
		set_bit(minor, minors);
		list_add(&spidev->device_entry, &device_list);
	}
	mutex_unlock(&device_list_lock);

617 618 619
	if (status == 0)
		spi_set_drvdata(spi, spidev);
	else
A
Andrea Paterniani 已提交
620 621 622 623 624
		kfree(spidev);

	return status;
}

625
static int spidev_remove(struct spi_device *spi)
A
Andrea Paterniani 已提交
626
{
627
	struct spidev_data	*spidev = spi_get_drvdata(spi);
A
Andrea Paterniani 已提交
628

629 630 631
	/* make sure ops on existing fds can abort cleanly */
	spin_lock_irq(&spidev->spi_lock);
	spidev->spi = NULL;
632
	spi_set_drvdata(spi, NULL);
633
	spin_unlock_irq(&spidev->spi_lock);
A
Andrea Paterniani 已提交
634

635 636
	/* prevent new opens */
	mutex_lock(&device_list_lock);
A
Andrea Paterniani 已提交
637
	list_del(&spidev->device_entry);
638 639 640 641
	device_destroy(spidev_class, spidev->devt);
	clear_bit(MINOR(spidev->devt), minors);
	if (spidev->users == 0)
		kfree(spidev);
A
Andrea Paterniani 已提交
642 643 644 645 646
	mutex_unlock(&device_list_lock);

	return 0;
}

647
static const struct of_device_id spidev_dt_ids[] = {
648
	{ .compatible = "rohm,dh2228fv" },
649 650 651 652 653
	{},
};

MODULE_DEVICE_TABLE(of, spidev_dt_ids);

654
static struct spi_driver spidev_spi_driver = {
A
Andrea Paterniani 已提交
655 656 657
	.driver = {
		.name =		"spidev",
		.owner =	THIS_MODULE,
658
		.of_match_table = of_match_ptr(spidev_dt_ids),
A
Andrea Paterniani 已提交
659 660
	},
	.probe =	spidev_probe,
661
	.remove =	spidev_remove,
A
Andrea Paterniani 已提交
662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684

	/* NOTE:  suspend/resume methods are not necessary here.
	 * We don't do anything except pass the requests to/from
	 * the underlying controller.  The refrigerator handles
	 * most issues; the controller driver handles the rest.
	 */
};

/*-------------------------------------------------------------------------*/

static int __init spidev_init(void)
{
	int status;

	/* Claim our 256 reserved device numbers.  Then register a class
	 * that will key udev/mdev to add/remove /dev nodes.  Last, register
	 * the driver which manages those device numbers.
	 */
	BUILD_BUG_ON(N_SPI_MINORS > 256);
	status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
	if (status < 0)
		return status;

685 686
	spidev_class = class_create(THIS_MODULE, "spidev");
	if (IS_ERR(spidev_class)) {
687
		unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
688
		return PTR_ERR(spidev_class);
A
Andrea Paterniani 已提交
689 690
	}

691
	status = spi_register_driver(&spidev_spi_driver);
A
Andrea Paterniani 已提交
692
	if (status < 0) {
693
		class_destroy(spidev_class);
694
		unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
A
Andrea Paterniani 已提交
695 696 697 698 699 700 701
	}
	return status;
}
module_init(spidev_init);

static void __exit spidev_exit(void)
{
702
	spi_unregister_driver(&spidev_spi_driver);
703
	class_destroy(spidev_class);
704
	unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
A
Andrea Paterniani 已提交
705 706 707 708 709 710
}
module_exit(spidev_exit);

MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
MODULE_DESCRIPTION("User mode SPI device interface");
MODULE_LICENSE("GPL");
711
MODULE_ALIAS("spi:spidev");