blob: 221741f382606688a1fafa5d6ad3f7a163f6571e [file] [log] [blame]
Andrea Paterniani814a8d52007-05-08 00:32:15 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Simple synchronous userspace interface to SPI devices
Andrea Paterniani814a8d52007-05-08 00:32:15 -07003 *
4 * Copyright (C) 2006 SWAPP
5 * Andrea Paterniani <a.paterniani@swapp-eng.it>
6 * Copyright (C) 2007 David Brownell (simplification, cleanup)
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
Andrea Paterniani814a8d52007-05-08 00:32:15 -070017 */
18
19#include <linux/init.h>
20#include <linux/module.h>
21#include <linux/ioctl.h>
22#include <linux/fs.h>
23#include <linux/device.h>
David Brownellb2c8dad2008-06-05 22:45:50 -070024#include <linux/err.h>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070025#include <linux/list.h>
26#include <linux/errno.h>
27#include <linux/mutex.h>
28#include <linux/slab.h>
Bernhard Walle7d48ec32011-02-03 09:37:18 +010029#include <linux/compat.h>
Maxime Ripard880cfd42012-10-31 11:30:08 +010030#include <linux/of.h>
31#include <linux/of_device.h>
Mika Westerbergcf9f4322016-07-04 17:18:15 +030032#include <linux/acpi.h>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070033
34#include <linux/spi/spi.h>
35#include <linux/spi/spidev.h>
36
Jingoo Han95c63cf2013-10-14 10:36:54 +090037#include <linux/uaccess.h>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070038
39
40/*
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040041 * This supports access to SPI devices using normal userspace I/O calls.
Andrea Paterniani814a8d52007-05-08 00:32:15 -070042 * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
43 * and often mask message boundaries, full SPI support requires full duplex
Thadeu Lima de Souza Cascardo137f11882009-10-22 17:34:29 -020044 * transfers. There are several kinds of internal message boundaries to
Andrea Paterniani814a8d52007-05-08 00:32:15 -070045 * handle chipselect management and other protocol options.
46 *
47 * SPI has a character major number assigned. We allocate minor numbers
48 * dynamically using a bitmask. You must use hotplug tools, such as udev
49 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
50 * nodes, since there is no fixed association of minor numbers with any
51 * particular SPI bus or device.
52 */
53#define SPIDEV_MAJOR 153 /* assigned */
54#define N_SPI_MINORS 32 /* ... up to 256 */
55
Thadeu Lima de Souza Cascardo8ae1c922009-12-14 14:20:23 -080056static DECLARE_BITMAP(minors, N_SPI_MINORS);
Andrea Paterniani814a8d52007-05-08 00:32:15 -070057
58
Anton Vorontsov6f166e32007-07-31 00:38:43 -070059/* Bit masks for spi_device.mode management. Note that incorrect
David Brownellb55f6272009-06-30 11:41:26 -070060 * settings for some settings can cause *lots* of trouble for other
61 * devices on a shared bus:
Anton Vorontsov6f166e32007-07-31 00:38:43 -070062 *
David Brownellb55f6272009-06-30 11:41:26 -070063 * - CS_HIGH ... this device will be active when it shouldn't be
64 * - 3WIRE ... when active, it won't behave as it should
65 * - NO_CS ... there will be no explicit message boundaries; this
66 * is completely incompatible with the shared bus model
67 * - READY ... transfers may proceed when they shouldn't.
68 *
69 * REVISIT should changing those flags be privileged?
Anton Vorontsov6f166e32007-07-31 00:38:43 -070070 */
71#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
David Brownellb55f6272009-06-30 11:41:26 -070072 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
Geert Uytterhoevendc64d392014-02-25 11:40:17 +010073 | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
74 | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)
Andrea Paterniani814a8d52007-05-08 00:32:15 -070075
76struct spidev_data {
David Brownellb2c8dad2008-06-05 22:45:50 -070077 dev_t devt;
David Brownell25d5cb42008-05-23 13:05:03 -070078 spinlock_t spi_lock;
Andrea Paterniani814a8d52007-05-08 00:32:15 -070079 struct spi_device *spi;
80 struct list_head device_entry;
81
Ray Jui865f6d12014-10-09 11:19:25 -070082 /* TX/RX buffers are NULL unless this device is open (users > 0) */
Andrea Paterniani814a8d52007-05-08 00:32:15 -070083 struct mutex buf_lock;
84 unsigned users;
Ray Jui865f6d12014-10-09 11:19:25 -070085 u8 *tx_buffer;
86 u8 *rx_buffer;
Mark Brown91690512014-11-08 10:28:10 +000087 u32 speed_hz;
Andrea Paterniani814a8d52007-05-08 00:32:15 -070088};
89
90static LIST_HEAD(device_list);
91static DEFINE_MUTEX(device_list_lock);
92
93static unsigned bufsiz = 4096;
94module_param(bufsiz, uint, S_IRUGO);
95MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
96
97/*-------------------------------------------------------------------------*/
98
David Brownell25d5cb42008-05-23 13:05:03 -070099static ssize_t
100spidev_sync(struct spidev_data *spidev, struct spi_message *message)
101{
102 DECLARE_COMPLETION_ONSTACK(done);
103 int status;
Martin Sperl98d6f472015-04-23 07:56:01 +0000104 struct spi_device *spi;
David Brownell25d5cb42008-05-23 13:05:03 -0700105
106 spin_lock_irq(&spidev->spi_lock);
Martin Sperl98d6f472015-04-23 07:56:01 +0000107 spi = spidev->spi;
David Brownell25d5cb42008-05-23 13:05:03 -0700108 spin_unlock_irq(&spidev->spi_lock);
109
Martin Sperl98d6f472015-04-23 07:56:01 +0000110 if (spi == NULL)
111 status = -ESHUTDOWN;
112 else
113 status = spi_sync(spi, message);
114
115 if (status == 0)
116 status = message->actual_length;
117
David Brownell25d5cb42008-05-23 13:05:03 -0700118 return status;
119}
120
121static inline ssize_t
122spidev_sync_write(struct spidev_data *spidev, size_t len)
123{
124 struct spi_transfer t = {
Ray Jui865f6d12014-10-09 11:19:25 -0700125 .tx_buf = spidev->tx_buffer,
David Brownell25d5cb42008-05-23 13:05:03 -0700126 .len = len,
Mark Brown91690512014-11-08 10:28:10 +0000127 .speed_hz = spidev->speed_hz,
David Brownell25d5cb42008-05-23 13:05:03 -0700128 };
129 struct spi_message m;
130
131 spi_message_init(&m);
132 spi_message_add_tail(&t, &m);
133 return spidev_sync(spidev, &m);
134}
135
136static inline ssize_t
137spidev_sync_read(struct spidev_data *spidev, size_t len)
138{
139 struct spi_transfer t = {
Ray Jui865f6d12014-10-09 11:19:25 -0700140 .rx_buf = spidev->rx_buffer,
David Brownell25d5cb42008-05-23 13:05:03 -0700141 .len = len,
Mark Brown91690512014-11-08 10:28:10 +0000142 .speed_hz = spidev->speed_hz,
David Brownell25d5cb42008-05-23 13:05:03 -0700143 };
144 struct spi_message m;
145
146 spi_message_init(&m);
147 spi_message_add_tail(&t, &m);
148 return spidev_sync(spidev, &m);
149}
150
151/*-------------------------------------------------------------------------*/
152
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700153/* Read-only message with current device setup */
154static ssize_t
155spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
156{
157 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700158 ssize_t status = 0;
159
160 /* chipselect only toggles at start or end of operation */
161 if (count > bufsiz)
162 return -EMSGSIZE;
163
164 spidev = filp->private_data;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700165
166 mutex_lock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700167 status = spidev_sync_read(spidev, count);
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700168 if (status > 0) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700169 unsigned long missing;
170
Ray Jui865f6d12014-10-09 11:19:25 -0700171 missing = copy_to_user(buf, spidev->rx_buffer, status);
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700172 if (missing == status)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700173 status = -EFAULT;
174 else
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700175 status = status - missing;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700176 }
177 mutex_unlock(&spidev->buf_lock);
178
179 return status;
180}
181
182/* Write-only message with current device setup */
183static ssize_t
184spidev_write(struct file *filp, const char __user *buf,
185 size_t count, loff_t *f_pos)
186{
187 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700188 ssize_t status = 0;
189 unsigned long missing;
190
191 /* chipselect only toggles at start or end of operation */
192 if (count > bufsiz)
193 return -EMSGSIZE;
194
195 spidev = filp->private_data;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700196
197 mutex_lock(&spidev->buf_lock);
Ray Jui865f6d12014-10-09 11:19:25 -0700198 missing = copy_from_user(spidev->tx_buffer, buf, count);
Jingoo Han95c63cf2013-10-14 10:36:54 +0900199 if (missing == 0)
David Brownell25d5cb42008-05-23 13:05:03 -0700200 status = spidev_sync_write(spidev, count);
Jingoo Han95c63cf2013-10-14 10:36:54 +0900201 else
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700202 status = -EFAULT;
203 mutex_unlock(&spidev->buf_lock);
204
205 return status;
206}
207
208static int spidev_message(struct spidev_data *spidev,
209 struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
210{
211 struct spi_message msg;
212 struct spi_transfer *k_xfers;
213 struct spi_transfer *k_tmp;
214 struct spi_ioc_transfer *u_tmp;
Ian Abbott9a12bff2015-02-16 15:00:47 +0000215 unsigned n, total, tx_total, rx_total;
Ray Jui865f6d12014-10-09 11:19:25 -0700216 u8 *tx_buf, *rx_buf;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700217 int status = -EFAULT;
218
219 spi_message_init(&msg);
220 k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
221 if (k_xfers == NULL)
222 return -ENOMEM;
223
224 /* Construct spi_message, copying any tx data to bounce buffer.
225 * We walk the array of user-provided transfers, using each one
226 * to initialize a kernel version of the same transfer.
227 */
Ray Jui865f6d12014-10-09 11:19:25 -0700228 tx_buf = spidev->tx_buffer;
229 rx_buf = spidev->rx_buffer;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700230 total = 0;
Ian Abbott9a12bff2015-02-16 15:00:47 +0000231 tx_total = 0;
232 rx_total = 0;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700233 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
234 n;
235 n--, k_tmp++, u_tmp++) {
236 k_tmp->len = u_tmp->len;
237
Domen Puncerda90fa82007-05-23 13:57:39 -0700238 total += k_tmp->len;
Ian Abbott9a12bff2015-02-16 15:00:47 +0000239 /* Since the function returns the total length of transfers
240 * on success, restrict the total to positive int values to
Ian Abbottf20fbaa2015-03-23 17:50:27 +0000241 * avoid the return value looking like an error. Also check
242 * each transfer length to avoid arithmetic overflow.
Ian Abbott9a12bff2015-02-16 15:00:47 +0000243 */
Ian Abbottf20fbaa2015-03-23 17:50:27 +0000244 if (total > INT_MAX || k_tmp->len > INT_MAX) {
Domen Puncerda90fa82007-05-23 13:57:39 -0700245 status = -EMSGSIZE;
246 goto done;
247 }
248
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700249 if (u_tmp->rx_buf) {
Ian Abbott9a12bff2015-02-16 15:00:47 +0000250 /* this transfer needs space in RX bounce buffer */
251 rx_total += k_tmp->len;
252 if (rx_total > bufsiz) {
253 status = -EMSGSIZE;
254 goto done;
255 }
Ray Jui865f6d12014-10-09 11:19:25 -0700256 k_tmp->rx_buf = rx_buf;
Ian Abbott9a12bff2015-02-16 15:00:47 +0000257 rx_buf += k_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700258 }
259 if (u_tmp->tx_buf) {
Ian Abbott9a12bff2015-02-16 15:00:47 +0000260 /* this transfer needs space in TX bounce buffer */
261 tx_total += k_tmp->len;
262 if (tx_total > bufsiz) {
263 status = -EMSGSIZE;
264 goto done;
265 }
Ray Jui865f6d12014-10-09 11:19:25 -0700266 k_tmp->tx_buf = tx_buf;
267 if (copy_from_user(tx_buf, (const u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000268 (uintptr_t) u_tmp->tx_buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700269 u_tmp->len))
270 goto done;
Ian Abbott9a12bff2015-02-16 15:00:47 +0000271 tx_buf += k_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700272 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700273
274 k_tmp->cs_change = !!u_tmp->cs_change;
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100275 k_tmp->tx_nbits = u_tmp->tx_nbits;
276 k_tmp->rx_nbits = u_tmp->rx_nbits;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700277 k_tmp->bits_per_word = u_tmp->bits_per_word;
278 k_tmp->delay_usecs = u_tmp->delay_usecs;
279 k_tmp->speed_hz = u_tmp->speed_hz;
Mark Brown91690512014-11-08 10:28:10 +0000280 if (!k_tmp->speed_hz)
281 k_tmp->speed_hz = spidev->speed_hz;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700282#ifdef VERBOSE
Florian Fainelli41df70d2009-12-07 14:28:43 +0000283 dev_dbg(&spidev->spi->dev,
Geert Uytterhoevenf15c5842015-11-30 15:19:06 +0100284 " xfer len %u %s%s%s%dbits %u usec %uHz\n",
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700285 u_tmp->len,
286 u_tmp->rx_buf ? "rx " : "",
287 u_tmp->tx_buf ? "tx " : "",
288 u_tmp->cs_change ? "cs " : "",
Florian Fainelli41df70d2009-12-07 14:28:43 +0000289 u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700290 u_tmp->delay_usecs,
Florian Fainelli41df70d2009-12-07 14:28:43 +0000291 u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700292#endif
293 spi_message_add_tail(k_tmp, &msg);
294 }
295
David Brownell25d5cb42008-05-23 13:05:03 -0700296 status = spidev_sync(spidev, &msg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700297 if (status < 0)
298 goto done;
299
300 /* copy any rx data out of bounce buffer */
Ray Jui865f6d12014-10-09 11:19:25 -0700301 rx_buf = spidev->rx_buffer;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700302 for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
303 if (u_tmp->rx_buf) {
Al Viro251d5952017-04-20 15:47:34 -0400304 if (copy_to_user((u8 __user *)
Ray Jui865f6d12014-10-09 11:19:25 -0700305 (uintptr_t) u_tmp->rx_buf, rx_buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700306 u_tmp->len)) {
307 status = -EFAULT;
308 goto done;
309 }
Ian Abbott9a12bff2015-02-16 15:00:47 +0000310 rx_buf += u_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700311 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700312 }
313 status = total;
314
315done:
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700316 kfree(k_xfers);
317 return status;
318}
319
Ian Abbott7782a1a2015-01-30 18:43:33 +0000320static struct spi_ioc_transfer *
321spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
322 unsigned *n_ioc)
323{
324 struct spi_ioc_transfer *ioc;
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 ioc = kmalloc(tmp, GFP_KERNEL);
342 if (!ioc)
343 return ERR_PTR(-ENOMEM);
344 if (__copy_from_user(ioc, u_ioc, tmp)) {
345 kfree(ioc);
346 return ERR_PTR(-EFAULT);
347 }
348 return ioc;
349}
350
Alan Cox4ef754b2008-07-23 21:29:55 -0700351static long
352spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700353{
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700354 int retval = 0;
355 struct spidev_data *spidev;
356 struct spi_device *spi;
357 u32 tmp;
358 unsigned n_ioc;
359 struct spi_ioc_transfer *ioc;
360
361 /* Check type and command number */
362 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
363 return -ENOTTY;
364
David Brownell25d5cb42008-05-23 13:05:03 -0700365 /* guard against device removal before, or while,
366 * we issue this ioctl.
367 */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700368 spidev = filp->private_data;
David Brownell25d5cb42008-05-23 13:05:03 -0700369 spin_lock_irq(&spidev->spi_lock);
370 spi = spi_dev_get(spidev->spi);
371 spin_unlock_irq(&spidev->spi_lock);
372
373 if (spi == NULL)
374 return -ESHUTDOWN;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700375
Alan Cox4ef754b2008-07-23 21:29:55 -0700376 /* use the buffer lock here for triple duty:
377 * - prevent I/O (from us) so calling spi_setup() is safe;
378 * - prevent concurrent SPI_IOC_WR_* from morphing
379 * data fields while SPI_IOC_RD_* reads them;
380 * - SPI_IOC_MESSAGE needs the buffer locked "normally".
381 */
382 mutex_lock(&spidev->buf_lock);
383
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700384 switch (cmd) {
385 /* read requests */
386 case SPI_IOC_RD_MODE:
Al Viro251d5952017-04-20 15:47:34 -0400387 retval = put_user(spi->mode & SPI_MODE_MASK,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700388 (__u8 __user *)arg);
389 break;
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100390 case SPI_IOC_RD_MODE32:
Al Viro251d5952017-04-20 15:47:34 -0400391 retval = put_user(spi->mode & SPI_MODE_MASK,
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100392 (__u32 __user *)arg);
393 break;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700394 case SPI_IOC_RD_LSB_FIRST:
Al Viro251d5952017-04-20 15:47:34 -0400395 retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700396 (__u8 __user *)arg);
397 break;
398 case SPI_IOC_RD_BITS_PER_WORD:
Al Viro251d5952017-04-20 15:47:34 -0400399 retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700400 break;
401 case SPI_IOC_RD_MAX_SPEED_HZ:
Al Viro251d5952017-04-20 15:47:34 -0400402 retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700403 break;
404
405 /* write requests */
406 case SPI_IOC_WR_MODE:
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100407 case SPI_IOC_WR_MODE32:
408 if (cmd == SPI_IOC_WR_MODE)
Al Viro251d5952017-04-20 15:47:34 -0400409 retval = get_user(tmp, (u8 __user *)arg);
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100410 else
Al Viro251d5952017-04-20 15:47:34 -0400411 retval = get_user(tmp, (u32 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700412 if (retval == 0) {
Geert Uytterhoevene6456182014-02-25 11:40:16 +0100413 u32 save = spi->mode;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700414
415 if (tmp & ~SPI_MODE_MASK) {
416 retval = -EINVAL;
417 break;
418 }
419
420 tmp |= spi->mode & ~SPI_MODE_MASK;
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100421 spi->mode = (u16)tmp;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700422 retval = spi_setup(spi);
423 if (retval < 0)
424 spi->mode = save;
425 else
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100426 dev_dbg(&spi->dev, "spi mode %x\n", tmp);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700427 }
428 break;
429 case SPI_IOC_WR_LSB_FIRST:
Al Viro251d5952017-04-20 15:47:34 -0400430 retval = get_user(tmp, (__u8 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700431 if (retval == 0) {
Geert Uytterhoevene6456182014-02-25 11:40:16 +0100432 u32 save = spi->mode;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700433
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:
Al Viro251d5952017-04-20 15:47:34 -0400447 retval = get_user(tmp, (__u8 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700448 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:
Al Viro251d5952017-04-20 15:47:34 -0400460 retval = get_user(tmp, (__u32 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700461 if (retval == 0) {
462 u32 save = spi->max_speed_hz;
463
464 spi->max_speed_hz = tmp;
465 retval = spi_setup(spi);
Mark Brown91690512014-11-08 10:28:10 +0000466 if (retval >= 0)
467 spidev->speed_hz = tmp;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700468 else
469 dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
Mark Brown91690512014-11-08 10:28:10 +0000470 spi->max_speed_hz = save;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700471 }
472 break;
473
474 default:
475 /* segmented and/or full-duplex I/O request */
Ian Abbott7782a1a2015-01-30 18:43:33 +0000476 /* Check message and copy into scratch area */
477 ioc = spidev_get_ioc_message(cmd,
478 (struct spi_ioc_transfer __user *)arg, &n_ioc);
479 if (IS_ERR(ioc)) {
480 retval = PTR_ERR(ioc);
David Brownell25d5cb42008-05-23 13:05:03 -0700481 break;
482 }
Ian Abbott7782a1a2015-01-30 18:43:33 +0000483 if (!ioc)
484 break; /* n_ioc is also 0 */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700485
486 /* translate to spi_message, execute */
487 retval = spidev_message(spidev, ioc, n_ioc);
488 kfree(ioc);
489 break;
490 }
Alan Cox4ef754b2008-07-23 21:29:55 -0700491
492 mutex_unlock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700493 spi_dev_put(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700494 return retval;
495}
496
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100497#ifdef CONFIG_COMPAT
498static long
Ian Abbott7782a1a2015-01-30 18:43:33 +0000499spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
500 unsigned long arg)
501{
502 struct spi_ioc_transfer __user *u_ioc;
503 int retval = 0;
504 struct spidev_data *spidev;
505 struct spi_device *spi;
506 unsigned n_ioc, n;
507 struct spi_ioc_transfer *ioc;
508
509 u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
Ian Abbott7782a1a2015-01-30 18:43:33 +0000510
511 /* guard against device removal before, or while,
512 * we issue this ioctl.
513 */
514 spidev = filp->private_data;
515 spin_lock_irq(&spidev->spi_lock);
516 spi = spi_dev_get(spidev->spi);
517 spin_unlock_irq(&spidev->spi_lock);
518
519 if (spi == NULL)
520 return -ESHUTDOWN;
521
522 /* SPI_IOC_MESSAGE needs the buffer locked "normally" */
523 mutex_lock(&spidev->buf_lock);
524
525 /* Check message and copy into scratch area */
526 ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc);
527 if (IS_ERR(ioc)) {
528 retval = PTR_ERR(ioc);
529 goto done;
530 }
531 if (!ioc)
532 goto done; /* n_ioc is also 0 */
533
534 /* Convert buffer pointers */
535 for (n = 0; n < n_ioc; n++) {
536 ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
537 ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
538 }
539
540 /* translate to spi_message, execute */
541 retval = spidev_message(spidev, ioc, n_ioc);
542 kfree(ioc);
543
544done:
545 mutex_unlock(&spidev->buf_lock);
546 spi_dev_put(spi);
547 return retval;
548}
549
550static long
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100551spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
552{
Ian Abbott7782a1a2015-01-30 18:43:33 +0000553 if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
554 && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
555 && _IOC_DIR(cmd) == _IOC_WRITE)
556 return spidev_compat_ioc_message(filp, cmd, arg);
557
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100558 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
559}
560#else
561#define spidev_compat_ioctl NULL
562#endif /* CONFIG_COMPAT */
563
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700564static int spidev_open(struct inode *inode, struct file *filp)
565{
566 struct spidev_data *spidev;
567 int status = -ENXIO;
568
569 mutex_lock(&device_list_lock);
570
571 list_for_each_entry(spidev, &device_list, device_entry) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700572 if (spidev->devt == inode->i_rdev) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700573 status = 0;
574 break;
575 }
576 }
Ray Jui865f6d12014-10-09 11:19:25 -0700577
578 if (status) {
579 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
580 goto err_find_dev;
581 }
582
583 if (!spidev->tx_buffer) {
584 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
585 if (!spidev->tx_buffer) {
Colin Ian King89a63562015-07-31 13:42:29 +0100586 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
587 status = -ENOMEM;
Ray Jui865f6d12014-10-09 11:19:25 -0700588 goto err_find_dev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700589 }
Colin Ian King89a63562015-07-31 13:42:29 +0100590 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700591
Ray Jui865f6d12014-10-09 11:19:25 -0700592 if (!spidev->rx_buffer) {
593 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
594 if (!spidev->rx_buffer) {
595 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
596 status = -ENOMEM;
597 goto err_alloc_rx_buf;
598 }
599 }
600
601 spidev->users++;
602 filp->private_data = spidev;
603 nonseekable_open(inode, filp);
604
605 mutex_unlock(&device_list_lock);
606 return 0;
607
608err_alloc_rx_buf:
609 kfree(spidev->tx_buffer);
610 spidev->tx_buffer = NULL;
611err_find_dev:
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700612 mutex_unlock(&device_list_lock);
613 return status;
614}
615
616static int spidev_release(struct inode *inode, struct file *filp)
617{
618 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700619
620 mutex_lock(&device_list_lock);
621 spidev = filp->private_data;
622 filp->private_data = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700623
624 /* last close? */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700625 spidev->users--;
626 if (!spidev->users) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700627 int dofree;
628
Ray Jui865f6d12014-10-09 11:19:25 -0700629 kfree(spidev->tx_buffer);
630 spidev->tx_buffer = NULL;
631
632 kfree(spidev->rx_buffer);
633 spidev->rx_buffer = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700634
Mark Brown56ea1072015-11-16 13:57:37 +0000635 spin_lock_irq(&spidev->spi_lock);
Sudip Mukherjeedd85ebf2015-09-10 16:48:13 +0530636 if (spidev->spi)
637 spidev->speed_hz = spidev->spi->max_speed_hz;
Mark Brown91690512014-11-08 10:28:10 +0000638
David Brownellb2c8dad2008-06-05 22:45:50 -0700639 /* ... after we unbound from the underlying device? */
David Brownellb2c8dad2008-06-05 22:45:50 -0700640 dofree = (spidev->spi == NULL);
641 spin_unlock_irq(&spidev->spi_lock);
642
643 if (dofree)
644 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700645 }
646 mutex_unlock(&device_list_lock);
647
Fabio Estevam99472cc2015-05-09 12:57:19 -0300648 return 0;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700649}
650
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700651static const struct file_operations spidev_fops = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700652 .owner = THIS_MODULE,
653 /* REVISIT switch to aio primitives, so that userspace
654 * gets more complete API coverage. It'll simplify things
655 * too, except for the locking.
656 */
657 .write = spidev_write,
658 .read = spidev_read,
Alan Cox4ef754b2008-07-23 21:29:55 -0700659 .unlocked_ioctl = spidev_ioctl,
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100660 .compat_ioctl = spidev_compat_ioctl,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700661 .open = spidev_open,
662 .release = spidev_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200663 .llseek = no_llseek,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700664};
665
666/*-------------------------------------------------------------------------*/
667
668/* The main reason to have this class is to make mdev/udev create the
669 * /dev/spidevB.C character device nodes exposing our userspace API.
670 * It also simplifies memory management.
671 */
672
David Brownellb2c8dad2008-06-05 22:45:50 -0700673static struct class *spidev_class;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700674
Mark Brown956b200a2015-03-27 16:36:04 -0700675#ifdef CONFIG_OF
676static const struct of_device_id spidev_dt_ids[] = {
677 { .compatible = "rohm,dh2228fv" },
Jiri Prchal6fec9192015-06-25 13:44:19 +0200678 { .compatible = "lineartechnology,ltc2488" },
Fabien Lahoudere144235e2016-10-06 16:44:27 +0200679 { .compatible = "ge,achc" },
Ben Whitten91b463462017-03-16 11:00:30 +0000680 { .compatible = "semtech,sx1301" },
Mark Brown956b200a2015-03-27 16:36:04 -0700681 {},
682};
683MODULE_DEVICE_TABLE(of, spidev_dt_ids);
684#endif
685
Mika Westerbergcf9f4322016-07-04 17:18:15 +0300686#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
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700723/*-------------------------------------------------------------------------*/
724
Grant Likelyfd4a3192012-12-07 16:57:14 +0000725static int spidev_probe(struct spi_device *spi)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700726{
727 struct spidev_data *spidev;
728 int status;
729 unsigned long minor;
730
Mark Brown956b200a2015-03-27 16:36:04 -0700731 /*
732 * spidev should never be referenced in DT without a specific
Fabio Estevamffe22882015-07-15 14:22:33 -0300733 * compatible string, it is a Linux implementation thing
Mark Brown956b200a2015-03-27 16:36:04 -0700734 * rather than a description of the hardware.
735 */
736 if (spi->dev.of_node && !of_match_device(spidev_dt_ids, &spi->dev)) {
737 dev_err(&spi->dev, "buggy DT: spidev listed directly in DT\n");
738 WARN_ON(spi->dev.of_node &&
739 !of_match_device(spidev_dt_ids, &spi->dev));
740 }
741
Mika Westerbergcf9f4322016-07-04 17:18:15 +0300742 spidev_probe_acpi(spi);
743
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700744 /* Allocate driver data */
745 spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
746 if (!spidev)
747 return -ENOMEM;
748
749 /* Initialize the driver data */
750 spidev->spi = spi;
David Brownell25d5cb42008-05-23 13:05:03 -0700751 spin_lock_init(&spidev->spi_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700752 mutex_init(&spidev->buf_lock);
753
754 INIT_LIST_HEAD(&spidev->device_entry);
755
756 /* If we can allocate a minor number, hook up this device.
757 * Reusing minors is fine so long as udev or mdev is working.
758 */
759 mutex_lock(&device_list_lock);
Domen Puncer0a4dd772007-05-15 23:57:05 -0700760 minor = find_first_zero_bit(minors, N_SPI_MINORS);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700761 if (minor < N_SPI_MINORS) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700762 struct device *dev;
763
764 spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700765 dev = device_create(spidev_class, &spi->dev, spidev->devt,
766 spidev, "spidev%d.%d",
767 spi->master->bus_num, spi->chip_select);
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930768 status = PTR_ERR_OR_ZERO(dev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700769 } else {
770 dev_dbg(&spi->dev, "no minor number available!\n");
771 status = -ENODEV;
772 }
773 if (status == 0) {
774 set_bit(minor, minors);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700775 list_add(&spidev->device_entry, &device_list);
776 }
777 mutex_unlock(&device_list_lock);
778
Mark Brown91690512014-11-08 10:28:10 +0000779 spidev->speed_hz = spi->max_speed_hz;
780
Wolfgang Ockeraaacf4b2008-12-01 13:13:52 -0800781 if (status == 0)
782 spi_set_drvdata(spi, spidev);
783 else
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700784 kfree(spidev);
785
786 return status;
787}
788
Grant Likelyfd4a3192012-12-07 16:57:14 +0000789static int spidev_remove(struct spi_device *spi)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700790{
David Brownellb2c8dad2008-06-05 22:45:50 -0700791 struct spidev_data *spidev = spi_get_drvdata(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700792
David Brownell25d5cb42008-05-23 13:05:03 -0700793 /* 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);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700797
David Brownell25d5cb42008-05-23 13:05:03 -0700798 /* prevent new opens */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700799 mutex_lock(&device_list_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700800 list_del(&spidev->device_entry);
David Brownellb2c8dad2008-06-05 22:45:50 -0700801 device_destroy(spidev_class, spidev->devt);
802 clear_bit(MINOR(spidev->devt), minors);
803 if (spidev->users == 0)
804 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700805 mutex_unlock(&device_list_lock);
806
807 return 0;
808}
809
Mike Frysingerdb389b62009-12-14 14:20:22 -0800810static struct spi_driver spidev_spi_driver = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700811 .driver = {
812 .name = "spidev",
Maxime Ripard880cfd42012-10-31 11:30:08 +0100813 .of_match_table = of_match_ptr(spidev_dt_ids),
Mika Westerbergcf9f4322016-07-04 17:18:15 +0300814 .acpi_match_table = ACPI_PTR(spidev_acpi_ids),
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700815 },
816 .probe = spidev_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000817 .remove = spidev_remove,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700818
819 /* NOTE: suspend/resume methods are not necessary here.
820 * We don't do anything except pass the requests to/from
821 * the underlying controller. The refrigerator handles
822 * most issues; the controller driver handles the rest.
823 */
824};
825
826/*-------------------------------------------------------------------------*/
827
828static int __init spidev_init(void)
829{
830 int status;
831
832 /* Claim our 256 reserved device numbers. Then register a class
833 * that will key udev/mdev to add/remove /dev nodes. Last, register
834 * the driver which manages those device numbers.
835 */
836 BUILD_BUG_ON(N_SPI_MINORS > 256);
837 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
838 if (status < 0)
839 return status;
840
David Brownellb2c8dad2008-06-05 22:45:50 -0700841 spidev_class = class_create(THIS_MODULE, "spidev");
842 if (IS_ERR(spidev_class)) {
Mike Frysingerdb389b62009-12-14 14:20:22 -0800843 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
David Brownellb2c8dad2008-06-05 22:45:50 -0700844 return PTR_ERR(spidev_class);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700845 }
846
Mike Frysingerdb389b62009-12-14 14:20:22 -0800847 status = spi_register_driver(&spidev_spi_driver);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700848 if (status < 0) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700849 class_destroy(spidev_class);
Mike Frysingerdb389b62009-12-14 14:20:22 -0800850 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700851 }
852 return status;
853}
854module_init(spidev_init);
855
856static void __exit spidev_exit(void)
857{
Mike Frysingerdb389b62009-12-14 14:20:22 -0800858 spi_unregister_driver(&spidev_spi_driver);
David Brownellb2c8dad2008-06-05 22:45:50 -0700859 class_destroy(spidev_class);
Mike Frysingerdb389b62009-12-14 14:20:22 -0800860 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700861}
862module_exit(spidev_exit);
863
864MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
865MODULE_DESCRIPTION("User mode SPI device interface");
866MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700867MODULE_ALIAS("spi:spidev");