blob: e50039fb14749f0f700505c3be183f1ad52e693a [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.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/init.h>
24#include <linux/module.h>
25#include <linux/ioctl.h>
26#include <linux/fs.h>
27#include <linux/device.h>
David Brownellb2c8dad2008-06-05 22:45:50 -070028#include <linux/err.h>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070029#include <linux/list.h>
30#include <linux/errno.h>
31#include <linux/mutex.h>
32#include <linux/slab.h>
Bernhard Walle7d48ec32011-02-03 09:37:18 +010033#include <linux/compat.h>
Maxime Ripard880cfd42012-10-31 11:30:08 +010034#include <linux/of.h>
35#include <linux/of_device.h>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070036
37#include <linux/spi/spi.h>
38#include <linux/spi/spidev.h>
39
Jingoo Han95c63cf2013-10-14 10:36:54 +090040#include <linux/uaccess.h>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070041
42
43/*
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040044 * This supports access to SPI devices using normal userspace I/O calls.
Andrea Paterniani814a8d52007-05-08 00:32:15 -070045 * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
46 * and often mask message boundaries, full SPI support requires full duplex
Thadeu Lima de Souza Cascardo137f1182009-10-22 17:34:29 -020047 * transfers. There are several kinds of internal message boundaries to
Andrea Paterniani814a8d52007-05-08 00:32:15 -070048 * handle chipselect management and other protocol options.
49 *
50 * SPI has a character major number assigned. We allocate minor numbers
51 * dynamically using a bitmask. You must use hotplug tools, such as udev
52 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
53 * nodes, since there is no fixed association of minor numbers with any
54 * particular SPI bus or device.
55 */
56#define SPIDEV_MAJOR 153 /* assigned */
57#define N_SPI_MINORS 32 /* ... up to 256 */
58
Thadeu Lima de Souza Cascardo8ae1c922009-12-14 14:20:23 -080059static DECLARE_BITMAP(minors, N_SPI_MINORS);
Andrea Paterniani814a8d52007-05-08 00:32:15 -070060
61
Anton Vorontsov6f166e32007-07-31 00:38:43 -070062/* Bit masks for spi_device.mode management. Note that incorrect
David Brownellb55f6272009-06-30 11:41:26 -070063 * settings for some settings can cause *lots* of trouble for other
64 * devices on a shared bus:
Anton Vorontsov6f166e32007-07-31 00:38:43 -070065 *
David Brownellb55f6272009-06-30 11:41:26 -070066 * - CS_HIGH ... this device will be active when it shouldn't be
67 * - 3WIRE ... when active, it won't behave as it should
68 * - NO_CS ... there will be no explicit message boundaries; this
69 * is completely incompatible with the shared bus model
70 * - READY ... transfers may proceed when they shouldn't.
71 *
72 * REVISIT should changing those flags be privileged?
Anton Vorontsov6f166e32007-07-31 00:38:43 -070073 */
74#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
David Brownellb55f6272009-06-30 11:41:26 -070075 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
Geert Uytterhoevendc64d392014-02-25 11:40:17 +010076 | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
77 | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD)
Andrea Paterniani814a8d52007-05-08 00:32:15 -070078
79struct spidev_data {
David Brownellb2c8dad2008-06-05 22:45:50 -070080 dev_t devt;
David Brownell25d5cb42008-05-23 13:05:03 -070081 spinlock_t spi_lock;
Andrea Paterniani814a8d52007-05-08 00:32:15 -070082 struct spi_device *spi;
83 struct list_head device_entry;
84
Ray Jui865f6d12014-10-09 11:19:25 -070085 /* TX/RX buffers are NULL unless this device is open (users > 0) */
Andrea Paterniani814a8d52007-05-08 00:32:15 -070086 struct mutex buf_lock;
87 unsigned users;
Ray Jui865f6d12014-10-09 11:19:25 -070088 u8 *tx_buffer;
89 u8 *rx_buffer;
Andrea Paterniani814a8d52007-05-08 00:32:15 -070090};
91
92static LIST_HEAD(device_list);
93static DEFINE_MUTEX(device_list_lock);
94
95static unsigned bufsiz = 4096;
96module_param(bufsiz, uint, S_IRUGO);
97MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
98
99/*-------------------------------------------------------------------------*/
100
David Brownell25d5cb42008-05-23 13:05:03 -0700101/*
102 * We can't use the standard synchronous wrappers for file I/O; we
103 * need to protect against async removal of the underlying spi_device.
104 */
105static void spidev_complete(void *arg)
106{
107 complete(arg);
108}
109
110static ssize_t
111spidev_sync(struct spidev_data *spidev, struct spi_message *message)
112{
113 DECLARE_COMPLETION_ONSTACK(done);
114 int status;
115
116 message->complete = spidev_complete;
117 message->context = &done;
118
119 spin_lock_irq(&spidev->spi_lock);
120 if (spidev->spi == NULL)
121 status = -ESHUTDOWN;
122 else
123 status = spi_async(spidev->spi, message);
124 spin_unlock_irq(&spidev->spi_lock);
125
126 if (status == 0) {
127 wait_for_completion(&done);
128 status = message->status;
129 if (status == 0)
130 status = message->actual_length;
131 }
132 return status;
133}
134
135static inline ssize_t
136spidev_sync_write(struct spidev_data *spidev, size_t len)
137{
138 struct spi_transfer t = {
Ray Jui865f6d12014-10-09 11:19:25 -0700139 .tx_buf = spidev->tx_buffer,
David Brownell25d5cb42008-05-23 13:05:03 -0700140 .len = len,
141 };
142 struct spi_message m;
143
144 spi_message_init(&m);
145 spi_message_add_tail(&t, &m);
146 return spidev_sync(spidev, &m);
147}
148
149static inline ssize_t
150spidev_sync_read(struct spidev_data *spidev, size_t len)
151{
152 struct spi_transfer t = {
Ray Jui865f6d12014-10-09 11:19:25 -0700153 .rx_buf = spidev->rx_buffer,
David Brownell25d5cb42008-05-23 13:05:03 -0700154 .len = len,
155 };
156 struct spi_message m;
157
158 spi_message_init(&m);
159 spi_message_add_tail(&t, &m);
160 return spidev_sync(spidev, &m);
161}
162
163/*-------------------------------------------------------------------------*/
164
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700165/* Read-only message with current device setup */
166static ssize_t
167spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
168{
169 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700170 ssize_t status = 0;
171
172 /* chipselect only toggles at start or end of operation */
173 if (count > bufsiz)
174 return -EMSGSIZE;
175
176 spidev = filp->private_data;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700177
178 mutex_lock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700179 status = spidev_sync_read(spidev, count);
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700180 if (status > 0) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700181 unsigned long missing;
182
Ray Jui865f6d12014-10-09 11:19:25 -0700183 missing = copy_to_user(buf, spidev->rx_buffer, status);
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700184 if (missing == status)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700185 status = -EFAULT;
186 else
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700187 status = status - missing;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700188 }
189 mutex_unlock(&spidev->buf_lock);
190
191 return status;
192}
193
194/* Write-only message with current device setup */
195static ssize_t
196spidev_write(struct file *filp, const char __user *buf,
197 size_t count, loff_t *f_pos)
198{
199 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700200 ssize_t status = 0;
201 unsigned long missing;
202
203 /* chipselect only toggles at start or end of operation */
204 if (count > bufsiz)
205 return -EMSGSIZE;
206
207 spidev = filp->private_data;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700208
209 mutex_lock(&spidev->buf_lock);
Ray Jui865f6d12014-10-09 11:19:25 -0700210 missing = copy_from_user(spidev->tx_buffer, buf, count);
Jingoo Han95c63cf2013-10-14 10:36:54 +0900211 if (missing == 0)
David Brownell25d5cb42008-05-23 13:05:03 -0700212 status = spidev_sync_write(spidev, count);
Jingoo Han95c63cf2013-10-14 10:36:54 +0900213 else
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700214 status = -EFAULT;
215 mutex_unlock(&spidev->buf_lock);
216
217 return status;
218}
219
220static int spidev_message(struct spidev_data *spidev,
221 struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
222{
223 struct spi_message msg;
224 struct spi_transfer *k_xfers;
225 struct spi_transfer *k_tmp;
226 struct spi_ioc_transfer *u_tmp;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700227 unsigned n, total;
Ray Jui865f6d12014-10-09 11:19:25 -0700228 u8 *tx_buf, *rx_buf;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700229 int status = -EFAULT;
230
231 spi_message_init(&msg);
232 k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
233 if (k_xfers == NULL)
234 return -ENOMEM;
235
236 /* Construct spi_message, copying any tx data to bounce buffer.
237 * We walk the array of user-provided transfers, using each one
238 * to initialize a kernel version of the same transfer.
239 */
Ray Jui865f6d12014-10-09 11:19:25 -0700240 tx_buf = spidev->tx_buffer;
241 rx_buf = spidev->rx_buffer;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700242 total = 0;
243 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
244 n;
245 n--, k_tmp++, u_tmp++) {
246 k_tmp->len = u_tmp->len;
247
Domen Puncerda90fa82007-05-23 13:57:39 -0700248 total += k_tmp->len;
249 if (total > bufsiz) {
250 status = -EMSGSIZE;
251 goto done;
252 }
253
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700254 if (u_tmp->rx_buf) {
Ray Jui865f6d12014-10-09 11:19:25 -0700255 k_tmp->rx_buf = rx_buf;
David Brownell96ddbf52007-08-10 13:01:09 -0700256 if (!access_ok(VERIFY_WRITE, (u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000257 (uintptr_t) u_tmp->rx_buf,
David Brownell96ddbf52007-08-10 13:01:09 -0700258 u_tmp->len))
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700259 goto done;
260 }
261 if (u_tmp->tx_buf) {
Ray Jui865f6d12014-10-09 11:19:25 -0700262 k_tmp->tx_buf = tx_buf;
263 if (copy_from_user(tx_buf, (const u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000264 (uintptr_t) u_tmp->tx_buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700265 u_tmp->len))
266 goto done;
267 }
Ray Jui865f6d12014-10-09 11:19:25 -0700268 tx_buf += k_tmp->len;
269 rx_buf += k_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700270
271 k_tmp->cs_change = !!u_tmp->cs_change;
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100272 k_tmp->tx_nbits = u_tmp->tx_nbits;
273 k_tmp->rx_nbits = u_tmp->rx_nbits;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700274 k_tmp->bits_per_word = u_tmp->bits_per_word;
275 k_tmp->delay_usecs = u_tmp->delay_usecs;
276 k_tmp->speed_hz = u_tmp->speed_hz;
277#ifdef VERBOSE
Florian Fainelli41df70d2009-12-07 14:28:43 +0000278 dev_dbg(&spidev->spi->dev,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700279 " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
280 u_tmp->len,
281 u_tmp->rx_buf ? "rx " : "",
282 u_tmp->tx_buf ? "tx " : "",
283 u_tmp->cs_change ? "cs " : "",
Florian Fainelli41df70d2009-12-07 14:28:43 +0000284 u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700285 u_tmp->delay_usecs,
Florian Fainelli41df70d2009-12-07 14:28:43 +0000286 u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700287#endif
288 spi_message_add_tail(k_tmp, &msg);
289 }
290
David Brownell25d5cb42008-05-23 13:05:03 -0700291 status = spidev_sync(spidev, &msg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700292 if (status < 0)
293 goto done;
294
295 /* copy any rx data out of bounce buffer */
Ray Jui865f6d12014-10-09 11:19:25 -0700296 rx_buf = spidev->rx_buffer;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700297 for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
298 if (u_tmp->rx_buf) {
David Brownell4917d922007-07-17 04:04:04 -0700299 if (__copy_to_user((u8 __user *)
Ray Jui865f6d12014-10-09 11:19:25 -0700300 (uintptr_t) u_tmp->rx_buf, rx_buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700301 u_tmp->len)) {
302 status = -EFAULT;
303 goto done;
304 }
305 }
Ray Jui865f6d12014-10-09 11:19:25 -0700306 rx_buf += u_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700307 }
308 status = total;
309
310done:
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700311 kfree(k_xfers);
312 return status;
313}
314
Alan Cox4ef754b2008-07-23 21:29:55 -0700315static long
316spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700317{
318 int err = 0;
319 int retval = 0;
320 struct spidev_data *spidev;
321 struct spi_device *spi;
322 u32 tmp;
323 unsigned n_ioc;
324 struct spi_ioc_transfer *ioc;
325
326 /* Check type and command number */
327 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
328 return -ENOTTY;
329
330 /* Check access direction once here; don't repeat below.
331 * IOC_DIR is from the user perspective, while access_ok is
332 * from the kernel perspective; so they look reversed.
333 */
334 if (_IOC_DIR(cmd) & _IOC_READ)
335 err = !access_ok(VERIFY_WRITE,
336 (void __user *)arg, _IOC_SIZE(cmd));
337 if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
338 err = !access_ok(VERIFY_READ,
339 (void __user *)arg, _IOC_SIZE(cmd));
340 if (err)
341 return -EFAULT;
342
David Brownell25d5cb42008-05-23 13:05:03 -0700343 /* guard against device removal before, or while,
344 * we issue this ioctl.
345 */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700346 spidev = filp->private_data;
David Brownell25d5cb42008-05-23 13:05:03 -0700347 spin_lock_irq(&spidev->spi_lock);
348 spi = spi_dev_get(spidev->spi);
349 spin_unlock_irq(&spidev->spi_lock);
350
351 if (spi == NULL)
352 return -ESHUTDOWN;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700353
Alan Cox4ef754b2008-07-23 21:29:55 -0700354 /* use the buffer lock here for triple duty:
355 * - prevent I/O (from us) so calling spi_setup() is safe;
356 * - prevent concurrent SPI_IOC_WR_* from morphing
357 * data fields while SPI_IOC_RD_* reads them;
358 * - SPI_IOC_MESSAGE needs the buffer locked "normally".
359 */
360 mutex_lock(&spidev->buf_lock);
361
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700362 switch (cmd) {
363 /* read requests */
364 case SPI_IOC_RD_MODE:
365 retval = __put_user(spi->mode & SPI_MODE_MASK,
366 (__u8 __user *)arg);
367 break;
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100368 case SPI_IOC_RD_MODE32:
369 retval = __put_user(spi->mode & SPI_MODE_MASK,
370 (__u32 __user *)arg);
371 break;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700372 case SPI_IOC_RD_LSB_FIRST:
373 retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
374 (__u8 __user *)arg);
375 break;
376 case SPI_IOC_RD_BITS_PER_WORD:
377 retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
378 break;
379 case SPI_IOC_RD_MAX_SPEED_HZ:
380 retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
381 break;
382
383 /* write requests */
384 case SPI_IOC_WR_MODE:
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100385 case SPI_IOC_WR_MODE32:
386 if (cmd == SPI_IOC_WR_MODE)
387 retval = __get_user(tmp, (u8 __user *)arg);
388 else
389 retval = __get_user(tmp, (u32 __user *)arg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700390 if (retval == 0) {
Geert Uytterhoevene6456182014-02-25 11:40:16 +0100391 u32 save = spi->mode;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700392
393 if (tmp & ~SPI_MODE_MASK) {
394 retval = -EINVAL;
395 break;
396 }
397
398 tmp |= spi->mode & ~SPI_MODE_MASK;
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100399 spi->mode = (u16)tmp;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700400 retval = spi_setup(spi);
401 if (retval < 0)
402 spi->mode = save;
403 else
Geert Uytterhoevendc64d392014-02-25 11:40:17 +0100404 dev_dbg(&spi->dev, "spi mode %x\n", tmp);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700405 }
406 break;
407 case SPI_IOC_WR_LSB_FIRST:
408 retval = __get_user(tmp, (__u8 __user *)arg);
409 if (retval == 0) {
Geert Uytterhoevene6456182014-02-25 11:40:16 +0100410 u32 save = spi->mode;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700411
412 if (tmp)
413 spi->mode |= SPI_LSB_FIRST;
414 else
415 spi->mode &= ~SPI_LSB_FIRST;
416 retval = spi_setup(spi);
417 if (retval < 0)
418 spi->mode = save;
419 else
420 dev_dbg(&spi->dev, "%csb first\n",
421 tmp ? 'l' : 'm');
422 }
423 break;
424 case SPI_IOC_WR_BITS_PER_WORD:
425 retval = __get_user(tmp, (__u8 __user *)arg);
426 if (retval == 0) {
427 u8 save = spi->bits_per_word;
428
429 spi->bits_per_word = tmp;
430 retval = spi_setup(spi);
431 if (retval < 0)
432 spi->bits_per_word = save;
433 else
434 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
435 }
436 break;
437 case SPI_IOC_WR_MAX_SPEED_HZ:
438 retval = __get_user(tmp, (__u32 __user *)arg);
439 if (retval == 0) {
440 u32 save = spi->max_speed_hz;
441
442 spi->max_speed_hz = tmp;
443 retval = spi_setup(spi);
444 if (retval < 0)
445 spi->max_speed_hz = save;
446 else
447 dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
448 }
449 break;
450
451 default:
452 /* segmented and/or full-duplex I/O request */
453 if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
David Brownell25d5cb42008-05-23 13:05:03 -0700454 || _IOC_DIR(cmd) != _IOC_WRITE) {
455 retval = -ENOTTY;
456 break;
457 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700458
459 tmp = _IOC_SIZE(cmd);
460 if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
461 retval = -EINVAL;
462 break;
463 }
464 n_ioc = tmp / sizeof(struct spi_ioc_transfer);
465 if (n_ioc == 0)
466 break;
467
468 /* copy into scratch area */
469 ioc = kmalloc(tmp, GFP_KERNEL);
470 if (!ioc) {
471 retval = -ENOMEM;
472 break;
473 }
474 if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
Florin Malita9bea3f22007-05-23 13:57:45 -0700475 kfree(ioc);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700476 retval = -EFAULT;
477 break;
478 }
479
480 /* translate to spi_message, execute */
481 retval = spidev_message(spidev, ioc, n_ioc);
482 kfree(ioc);
483 break;
484 }
Alan Cox4ef754b2008-07-23 21:29:55 -0700485
486 mutex_unlock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700487 spi_dev_put(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700488 return retval;
489}
490
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100491#ifdef CONFIG_COMPAT
492static long
493spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
494{
495 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
496}
497#else
498#define spidev_compat_ioctl NULL
499#endif /* CONFIG_COMPAT */
500
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700501static int spidev_open(struct inode *inode, struct file *filp)
502{
503 struct spidev_data *spidev;
504 int status = -ENXIO;
505
506 mutex_lock(&device_list_lock);
507
508 list_for_each_entry(spidev, &device_list, device_entry) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700509 if (spidev->devt == inode->i_rdev) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700510 status = 0;
511 break;
512 }
513 }
Ray Jui865f6d12014-10-09 11:19:25 -0700514
515 if (status) {
516 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
517 goto err_find_dev;
518 }
519
520 if (!spidev->tx_buffer) {
521 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
522 if (!spidev->tx_buffer) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700523 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
524 status = -ENOMEM;
Ray Jui865f6d12014-10-09 11:19:25 -0700525 goto err_find_dev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700526 }
527 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700528
Ray Jui865f6d12014-10-09 11:19:25 -0700529 if (!spidev->rx_buffer) {
530 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
531 if (!spidev->rx_buffer) {
532 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
533 status = -ENOMEM;
534 goto err_alloc_rx_buf;
535 }
536 }
537
538 spidev->users++;
539 filp->private_data = spidev;
540 nonseekable_open(inode, filp);
541
542 mutex_unlock(&device_list_lock);
543 return 0;
544
545err_alloc_rx_buf:
546 kfree(spidev->tx_buffer);
547 spidev->tx_buffer = NULL;
548err_find_dev:
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700549 mutex_unlock(&device_list_lock);
550 return status;
551}
552
553static int spidev_release(struct inode *inode, struct file *filp)
554{
555 struct spidev_data *spidev;
556 int status = 0;
557
558 mutex_lock(&device_list_lock);
559 spidev = filp->private_data;
560 filp->private_data = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700561
562 /* last close? */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700563 spidev->users--;
564 if (!spidev->users) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700565 int dofree;
566
Ray Jui865f6d12014-10-09 11:19:25 -0700567 kfree(spidev->tx_buffer);
568 spidev->tx_buffer = NULL;
569
570 kfree(spidev->rx_buffer);
571 spidev->rx_buffer = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700572
573 /* ... after we unbound from the underlying device? */
574 spin_lock_irq(&spidev->spi_lock);
575 dofree = (spidev->spi == NULL);
576 spin_unlock_irq(&spidev->spi_lock);
577
578 if (dofree)
579 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700580 }
581 mutex_unlock(&device_list_lock);
582
583 return status;
584}
585
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700586static const struct file_operations spidev_fops = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700587 .owner = THIS_MODULE,
588 /* REVISIT switch to aio primitives, so that userspace
589 * gets more complete API coverage. It'll simplify things
590 * too, except for the locking.
591 */
592 .write = spidev_write,
593 .read = spidev_read,
Alan Cox4ef754b2008-07-23 21:29:55 -0700594 .unlocked_ioctl = spidev_ioctl,
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100595 .compat_ioctl = spidev_compat_ioctl,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700596 .open = spidev_open,
597 .release = spidev_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200598 .llseek = no_llseek,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700599};
600
601/*-------------------------------------------------------------------------*/
602
603/* The main reason to have this class is to make mdev/udev create the
604 * /dev/spidevB.C character device nodes exposing our userspace API.
605 * It also simplifies memory management.
606 */
607
David Brownellb2c8dad2008-06-05 22:45:50 -0700608static struct class *spidev_class;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700609
610/*-------------------------------------------------------------------------*/
611
Grant Likelyfd4a3192012-12-07 16:57:14 +0000612static int spidev_probe(struct spi_device *spi)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700613{
614 struct spidev_data *spidev;
615 int status;
616 unsigned long minor;
617
618 /* Allocate driver data */
619 spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
620 if (!spidev)
621 return -ENOMEM;
622
623 /* Initialize the driver data */
624 spidev->spi = spi;
David Brownell25d5cb42008-05-23 13:05:03 -0700625 spin_lock_init(&spidev->spi_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700626 mutex_init(&spidev->buf_lock);
627
628 INIT_LIST_HEAD(&spidev->device_entry);
629
630 /* If we can allocate a minor number, hook up this device.
631 * Reusing minors is fine so long as udev or mdev is working.
632 */
633 mutex_lock(&device_list_lock);
Domen Puncer0a4dd772007-05-15 23:57:05 -0700634 minor = find_first_zero_bit(minors, N_SPI_MINORS);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700635 if (minor < N_SPI_MINORS) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700636 struct device *dev;
637
638 spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700639 dev = device_create(spidev_class, &spi->dev, spidev->devt,
640 spidev, "spidev%d.%d",
641 spi->master->bus_num, spi->chip_select);
Rusty Russell8c6ffba2013-07-15 11:20:32 +0930642 status = PTR_ERR_OR_ZERO(dev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700643 } else {
644 dev_dbg(&spi->dev, "no minor number available!\n");
645 status = -ENODEV;
646 }
647 if (status == 0) {
648 set_bit(minor, minors);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700649 list_add(&spidev->device_entry, &device_list);
650 }
651 mutex_unlock(&device_list_lock);
652
Wolfgang Ockeraaacf4b2008-12-01 13:13:52 -0800653 if (status == 0)
654 spi_set_drvdata(spi, spidev);
655 else
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700656 kfree(spidev);
657
658 return status;
659}
660
Grant Likelyfd4a3192012-12-07 16:57:14 +0000661static int spidev_remove(struct spi_device *spi)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700662{
David Brownellb2c8dad2008-06-05 22:45:50 -0700663 struct spidev_data *spidev = spi_get_drvdata(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700664
David Brownell25d5cb42008-05-23 13:05:03 -0700665 /* make sure ops on existing fds can abort cleanly */
666 spin_lock_irq(&spidev->spi_lock);
667 spidev->spi = NULL;
668 spin_unlock_irq(&spidev->spi_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700669
David Brownell25d5cb42008-05-23 13:05:03 -0700670 /* prevent new opens */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700671 mutex_lock(&device_list_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700672 list_del(&spidev->device_entry);
David Brownellb2c8dad2008-06-05 22:45:50 -0700673 device_destroy(spidev_class, spidev->devt);
674 clear_bit(MINOR(spidev->devt), minors);
675 if (spidev->users == 0)
676 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700677 mutex_unlock(&device_list_lock);
678
679 return 0;
680}
681
Maxime Ripard880cfd42012-10-31 11:30:08 +0100682static const struct of_device_id spidev_dt_ids[] = {
Maxime Ripard8fad8052012-10-31 11:30:09 +0100683 { .compatible = "rohm,dh2228fv" },
Maxime Ripard880cfd42012-10-31 11:30:08 +0100684 {},
685};
686
687MODULE_DEVICE_TABLE(of, spidev_dt_ids);
688
Mike Frysingerdb389b62009-12-14 14:20:22 -0800689static struct spi_driver spidev_spi_driver = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700690 .driver = {
691 .name = "spidev",
692 .owner = THIS_MODULE,
Maxime Ripard880cfd42012-10-31 11:30:08 +0100693 .of_match_table = of_match_ptr(spidev_dt_ids),
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700694 },
695 .probe = spidev_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000696 .remove = spidev_remove,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700697
698 /* NOTE: suspend/resume methods are not necessary here.
699 * We don't do anything except pass the requests to/from
700 * the underlying controller. The refrigerator handles
701 * most issues; the controller driver handles the rest.
702 */
703};
704
705/*-------------------------------------------------------------------------*/
706
707static int __init spidev_init(void)
708{
709 int status;
710
711 /* Claim our 256 reserved device numbers. Then register a class
712 * that will key udev/mdev to add/remove /dev nodes. Last, register
713 * the driver which manages those device numbers.
714 */
715 BUILD_BUG_ON(N_SPI_MINORS > 256);
716 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
717 if (status < 0)
718 return status;
719
David Brownellb2c8dad2008-06-05 22:45:50 -0700720 spidev_class = class_create(THIS_MODULE, "spidev");
721 if (IS_ERR(spidev_class)) {
Mike Frysingerdb389b62009-12-14 14:20:22 -0800722 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
David Brownellb2c8dad2008-06-05 22:45:50 -0700723 return PTR_ERR(spidev_class);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700724 }
725
Mike Frysingerdb389b62009-12-14 14:20:22 -0800726 status = spi_register_driver(&spidev_spi_driver);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700727 if (status < 0) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700728 class_destroy(spidev_class);
Mike Frysingerdb389b62009-12-14 14:20:22 -0800729 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700730 }
731 return status;
732}
733module_init(spidev_init);
734
735static void __exit spidev_exit(void)
736{
Mike Frysingerdb389b62009-12-14 14:20:22 -0800737 spi_unregister_driver(&spidev_spi_driver);
David Brownellb2c8dad2008-06-05 22:45:50 -0700738 class_destroy(spidev_class);
Mike Frysingerdb389b62009-12-14 14:20:22 -0800739 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700740}
741module_exit(spidev_exit);
742
743MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
744MODULE_DESCRIPTION("User mode SPI device interface");
745MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700746MODULE_ALIAS("spi:spidev");