blob: aaf026574f875b734c2bbd6f80955e06223bf8e7 [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>
Andrea Paterniani814a8d52007-05-08 00:32:15 -070034
35#include <linux/spi/spi.h>
36#include <linux/spi/spidev.h>
37
38#include <asm/uaccess.h>
39
40
41/*
Uwe Kleine-Königb5950762010-11-01 15:38:34 -040042 * This supports access to SPI devices using normal userspace I/O calls.
Andrea Paterniani814a8d52007-05-08 00:32:15 -070043 * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
44 * and often mask message boundaries, full SPI support requires full duplex
Thadeu Lima de Souza Cascardo137f1182009-10-22 17:34:29 -020045 * transfers. There are several kinds of internal message boundaries to
Andrea Paterniani814a8d52007-05-08 00:32:15 -070046 * handle chipselect management and other protocol options.
47 *
48 * SPI has a character major number assigned. We allocate minor numbers
49 * dynamically using a bitmask. You must use hotplug tools, such as udev
50 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
51 * nodes, since there is no fixed association of minor numbers with any
52 * particular SPI bus or device.
53 */
54#define SPIDEV_MAJOR 153 /* assigned */
55#define N_SPI_MINORS 32 /* ... up to 256 */
56
Thadeu Lima de Souza Cascardo8ae1c922009-12-14 14:20:23 -080057static DECLARE_BITMAP(minors, N_SPI_MINORS);
Andrea Paterniani814a8d52007-05-08 00:32:15 -070058
59
Anton Vorontsov6f166e32007-07-31 00:38:43 -070060/* Bit masks for spi_device.mode management. Note that incorrect
David Brownellb55f6272009-06-30 11:41:26 -070061 * settings for some settings can cause *lots* of trouble for other
62 * devices on a shared bus:
Anton Vorontsov6f166e32007-07-31 00:38:43 -070063 *
David Brownellb55f6272009-06-30 11:41:26 -070064 * - CS_HIGH ... this device will be active when it shouldn't be
65 * - 3WIRE ... when active, it won't behave as it should
66 * - NO_CS ... there will be no explicit message boundaries; this
67 * is completely incompatible with the shared bus model
68 * - READY ... transfers may proceed when they shouldn't.
69 *
70 * REVISIT should changing those flags be privileged?
Anton Vorontsov6f166e32007-07-31 00:38:43 -070071 */
72#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
David Brownellb55f6272009-06-30 11:41:26 -070073 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
74 | SPI_NO_CS | SPI_READY)
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
David Brownellb2c8dad2008-06-05 22:45:50 -070082 /* buffer is NULL unless this device is open (users > 0) */
Andrea Paterniani814a8d52007-05-08 00:32:15 -070083 struct mutex buf_lock;
84 unsigned users;
85 u8 *buffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070086 u8 *bufferrx;
Andrea Paterniani814a8d52007-05-08 00:32:15 -070087};
88
89static LIST_HEAD(device_list);
90static DEFINE_MUTEX(device_list_lock);
91
92static unsigned bufsiz = 4096;
93module_param(bufsiz, uint, S_IRUGO);
94MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096/*
97 * This can be used for testing the controller, given the busnum and the
98 * cs required to use. If those parameters are used, spidev is
99 * dynamically added as device on the busnum, and messages can be sent
100 * via this interface.
101 */
102static int busnum = -1;
103module_param(busnum, int, S_IRUGO);
104MODULE_PARM_DESC(busnum, "bus num of the controller");
105
106static int chipselect = -1;
107module_param(chipselect, int, S_IRUGO);
108MODULE_PARM_DESC(chipselect, "chip select of the desired device");
109
110static int maxspeed = 10000000;
111module_param(maxspeed, int, S_IRUGO);
112MODULE_PARM_DESC(maxspeed, "max_speed of the desired device");
113
114static int spimode = SPI_MODE_3;
115module_param(spimode, int, S_IRUGO);
116MODULE_PARM_DESC(spimode, "mode of the desired device");
117
118static struct spi_device *spi;
119
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700120/*-------------------------------------------------------------------------*/
121
David Brownell25d5cb42008-05-23 13:05:03 -0700122/*
123 * We can't use the standard synchronous wrappers for file I/O; we
124 * need to protect against async removal of the underlying spi_device.
125 */
126static void spidev_complete(void *arg)
127{
128 complete(arg);
129}
130
131static ssize_t
132spidev_sync(struct spidev_data *spidev, struct spi_message *message)
133{
134 DECLARE_COMPLETION_ONSTACK(done);
135 int status;
136
137 message->complete = spidev_complete;
138 message->context = &done;
139
140 spin_lock_irq(&spidev->spi_lock);
141 if (spidev->spi == NULL)
142 status = -ESHUTDOWN;
143 else
144 status = spi_async(spidev->spi, message);
145 spin_unlock_irq(&spidev->spi_lock);
146
147 if (status == 0) {
148 wait_for_completion(&done);
149 status = message->status;
150 if (status == 0)
151 status = message->actual_length;
152 }
153 return status;
154}
155
156static inline ssize_t
157spidev_sync_write(struct spidev_data *spidev, size_t len)
158{
159 struct spi_transfer t = {
160 .tx_buf = spidev->buffer,
161 .len = len,
162 };
163 struct spi_message m;
164
165 spi_message_init(&m);
166 spi_message_add_tail(&t, &m);
167 return spidev_sync(spidev, &m);
168}
169
170static inline ssize_t
171spidev_sync_read(struct spidev_data *spidev, size_t len)
172{
173 struct spi_transfer t = {
174 .rx_buf = spidev->buffer,
175 .len = len,
176 };
177 struct spi_message m;
178
179 spi_message_init(&m);
180 spi_message_add_tail(&t, &m);
181 return spidev_sync(spidev, &m);
182}
183
184/*-------------------------------------------------------------------------*/
185
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700186/* Read-only message with current device setup */
187static ssize_t
188spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
189{
190 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700191 ssize_t status = 0;
192
193 /* chipselect only toggles at start or end of operation */
194 if (count > bufsiz)
195 return -EMSGSIZE;
196
197 spidev = filp->private_data;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700198
199 mutex_lock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700200 status = spidev_sync_read(spidev, count);
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700201 if (status > 0) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700202 unsigned long missing;
203
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700204 missing = copy_to_user(buf, spidev->buffer, status);
205 if (missing == status)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700206 status = -EFAULT;
207 else
Sebastian Siewior4b1295b2008-07-04 09:59:56 -0700208 status = status - missing;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700209 }
210 mutex_unlock(&spidev->buf_lock);
211
212 return status;
213}
214
215/* Write-only message with current device setup */
216static ssize_t
217spidev_write(struct file *filp, const char __user *buf,
218 size_t count, loff_t *f_pos)
219{
220 struct spidev_data *spidev;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700221 ssize_t status = 0;
222 unsigned long missing;
223
224 /* chipselect only toggles at start or end of operation */
225 if (count > bufsiz)
226 return -EMSGSIZE;
227
228 spidev = filp->private_data;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700229
230 mutex_lock(&spidev->buf_lock);
231 missing = copy_from_user(spidev->buffer, buf, count);
232 if (missing == 0) {
David Brownell25d5cb42008-05-23 13:05:03 -0700233 status = spidev_sync_write(spidev, count);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700234 } else
235 status = -EFAULT;
236 mutex_unlock(&spidev->buf_lock);
237
238 return status;
239}
240
241static int spidev_message(struct spidev_data *spidev,
242 struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
243{
244 struct spi_message msg;
245 struct spi_transfer *k_xfers;
246 struct spi_transfer *k_tmp;
247 struct spi_ioc_transfer *u_tmp;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700248 unsigned n, total;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700249 u8 *buf, *bufrx;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700250 int status = -EFAULT;
251
252 spi_message_init(&msg);
253 k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
254 if (k_xfers == NULL)
255 return -ENOMEM;
256
257 /* Construct spi_message, copying any tx data to bounce buffer.
258 * We walk the array of user-provided transfers, using each one
259 * to initialize a kernel version of the same transfer.
260 */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700261 buf = spidev->buffer;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700262 bufrx = spidev->bufferrx;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700263 total = 0;
264 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
265 n;
266 n--, k_tmp++, u_tmp++) {
267 k_tmp->len = u_tmp->len;
268
Domen Puncerda90fa82007-05-23 13:57:39 -0700269 total += k_tmp->len;
270 if (total > bufsiz) {
271 status = -EMSGSIZE;
272 goto done;
273 }
274
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700275 if (u_tmp->rx_buf) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700276 k_tmp->rx_buf = bufrx;
David Brownell96ddbf52007-08-10 13:01:09 -0700277 if (!access_ok(VERIFY_WRITE, (u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000278 (uintptr_t) u_tmp->rx_buf,
David Brownell96ddbf52007-08-10 13:01:09 -0700279 u_tmp->len))
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700280 goto done;
281 }
282 if (u_tmp->tx_buf) {
283 k_tmp->tx_buf = buf;
David Brownell4917d922007-07-17 04:04:04 -0700284 if (copy_from_user(buf, (const u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000285 (uintptr_t) u_tmp->tx_buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700286 u_tmp->len))
287 goto done;
288 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700289 buf += k_tmp->len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700290 bufrx += k_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700291
292 k_tmp->cs_change = !!u_tmp->cs_change;
293 k_tmp->bits_per_word = u_tmp->bits_per_word;
294 k_tmp->delay_usecs = u_tmp->delay_usecs;
295 k_tmp->speed_hz = u_tmp->speed_hz;
296#ifdef VERBOSE
Florian Fainelli41df70d2009-12-07 14:28:43 +0000297 dev_dbg(&spidev->spi->dev,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700298 " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
299 u_tmp->len,
300 u_tmp->rx_buf ? "rx " : "",
301 u_tmp->tx_buf ? "tx " : "",
302 u_tmp->cs_change ? "cs " : "",
Florian Fainelli41df70d2009-12-07 14:28:43 +0000303 u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700304 u_tmp->delay_usecs,
Florian Fainelli41df70d2009-12-07 14:28:43 +0000305 u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700306#endif
307 spi_message_add_tail(k_tmp, &msg);
308 }
309
David Brownell25d5cb42008-05-23 13:05:03 -0700310 status = spidev_sync(spidev, &msg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700311 if (status < 0)
312 goto done;
313
314 /* copy any rx data out of bounce buffer */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700315 buf = spidev->bufferrx;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700316 for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
317 if (u_tmp->rx_buf) {
David Brownell4917d922007-07-17 04:04:04 -0700318 if (__copy_to_user((u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000319 (uintptr_t) u_tmp->rx_buf, buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700320 u_tmp->len)) {
321 status = -EFAULT;
322 goto done;
323 }
324 }
325 buf += u_tmp->len;
326 }
327 status = total;
328
329done:
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700330 kfree(k_xfers);
331 return status;
332}
333
Alan Cox4ef754b2008-07-23 21:29:55 -0700334static long
335spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700336{
337 int err = 0;
338 int retval = 0;
339 struct spidev_data *spidev;
340 struct spi_device *spi;
341 u32 tmp;
342 unsigned n_ioc;
343 struct spi_ioc_transfer *ioc;
344
345 /* Check type and command number */
346 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
347 return -ENOTTY;
348
349 /* Check access direction once here; don't repeat below.
350 * IOC_DIR is from the user perspective, while access_ok is
351 * from the kernel perspective; so they look reversed.
352 */
353 if (_IOC_DIR(cmd) & _IOC_READ)
354 err = !access_ok(VERIFY_WRITE,
355 (void __user *)arg, _IOC_SIZE(cmd));
356 if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
357 err = !access_ok(VERIFY_READ,
358 (void __user *)arg, _IOC_SIZE(cmd));
359 if (err)
360 return -EFAULT;
361
David Brownell25d5cb42008-05-23 13:05:03 -0700362 /* guard against device removal before, or while,
363 * we issue this ioctl.
364 */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700365 spidev = filp->private_data;
David Brownell25d5cb42008-05-23 13:05:03 -0700366 spin_lock_irq(&spidev->spi_lock);
367 spi = spi_dev_get(spidev->spi);
368 spin_unlock_irq(&spidev->spi_lock);
369
370 if (spi == NULL)
371 return -ESHUTDOWN;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700372
Alan Cox4ef754b2008-07-23 21:29:55 -0700373 /* use the buffer lock here for triple duty:
374 * - prevent I/O (from us) so calling spi_setup() is safe;
375 * - prevent concurrent SPI_IOC_WR_* from morphing
376 * data fields while SPI_IOC_RD_* reads them;
377 * - SPI_IOC_MESSAGE needs the buffer locked "normally".
378 */
379 mutex_lock(&spidev->buf_lock);
380
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700381 switch (cmd) {
382 /* read requests */
383 case SPI_IOC_RD_MODE:
384 retval = __put_user(spi->mode & SPI_MODE_MASK,
385 (__u8 __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(spi->max_speed_hz, (__u32 __user *)arg);
396 break;
397
398 /* write requests */
399 case SPI_IOC_WR_MODE:
400 retval = __get_user(tmp, (u8 __user *)arg);
401 if (retval == 0) {
402 u8 save = spi->mode;
403
404 if (tmp & ~SPI_MODE_MASK) {
405 retval = -EINVAL;
406 break;
407 }
408
409 tmp |= spi->mode & ~SPI_MODE_MASK;
410 spi->mode = (u8)tmp;
411 retval = spi_setup(spi);
412 if (retval < 0)
413 spi->mode = save;
414 else
415 dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
416 }
417 break;
418 case SPI_IOC_WR_LSB_FIRST:
419 retval = __get_user(tmp, (__u8 __user *)arg);
420 if (retval == 0) {
421 u8 save = spi->mode;
422
423 if (tmp)
424 spi->mode |= SPI_LSB_FIRST;
425 else
426 spi->mode &= ~SPI_LSB_FIRST;
427 retval = spi_setup(spi);
428 if (retval < 0)
429 spi->mode = save;
430 else
431 dev_dbg(&spi->dev, "%csb first\n",
432 tmp ? 'l' : 'm');
433 }
434 break;
435 case SPI_IOC_WR_BITS_PER_WORD:
436 retval = __get_user(tmp, (__u8 __user *)arg);
437 if (retval == 0) {
438 u8 save = spi->bits_per_word;
439
440 spi->bits_per_word = tmp;
441 retval = spi_setup(spi);
442 if (retval < 0)
443 spi->bits_per_word = save;
444 else
445 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
446 }
447 break;
448 case SPI_IOC_WR_MAX_SPEED_HZ:
449 retval = __get_user(tmp, (__u32 __user *)arg);
450 if (retval == 0) {
451 u32 save = spi->max_speed_hz;
452
453 spi->max_speed_hz = tmp;
454 retval = spi_setup(spi);
455 if (retval < 0)
456 spi->max_speed_hz = save;
457 else
458 dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
459 }
460 break;
461
462 default:
463 /* segmented and/or full-duplex I/O request */
464 if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
David Brownell25d5cb42008-05-23 13:05:03 -0700465 || _IOC_DIR(cmd) != _IOC_WRITE) {
466 retval = -ENOTTY;
467 break;
468 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700469
470 tmp = _IOC_SIZE(cmd);
471 if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
472 retval = -EINVAL;
473 break;
474 }
475 n_ioc = tmp / sizeof(struct spi_ioc_transfer);
476 if (n_ioc == 0)
477 break;
478
479 /* copy into scratch area */
480 ioc = kmalloc(tmp, GFP_KERNEL);
481 if (!ioc) {
482 retval = -ENOMEM;
483 break;
484 }
485 if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
Florin Malita9bea3f22007-05-23 13:57:45 -0700486 kfree(ioc);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700487 retval = -EFAULT;
488 break;
489 }
490
491 /* translate to spi_message, execute */
492 retval = spidev_message(spidev, ioc, n_ioc);
493 kfree(ioc);
494 break;
495 }
Alan Cox4ef754b2008-07-23 21:29:55 -0700496
497 mutex_unlock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700498 spi_dev_put(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700499 return retval;
500}
501
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100502#ifdef CONFIG_COMPAT
503static long
504spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
505{
506 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
507}
508#else
509#define spidev_compat_ioctl NULL
510#endif /* CONFIG_COMPAT */
511
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700512static int spidev_open(struct inode *inode, struct file *filp)
513{
514 struct spidev_data *spidev;
515 int status = -ENXIO;
516
517 mutex_lock(&device_list_lock);
518
519 list_for_each_entry(spidev, &device_list, device_entry) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700520 if (spidev->devt == inode->i_rdev) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700521 status = 0;
522 break;
523 }
524 }
525 if (status == 0) {
526 if (!spidev->buffer) {
527 spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
528 if (!spidev->buffer) {
529 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
530 status = -ENOMEM;
531 }
532 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700533 if (!spidev->bufferrx) {
534 spidev->bufferrx = kmalloc(bufsiz, GFP_KERNEL);
535 if (!spidev->bufferrx) {
536 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
537 kfree(spidev->buffer);
538 spidev->buffer = NULL;
539 status = -ENOMEM;
540 }
541 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700542 if (status == 0) {
543 spidev->users++;
544 filp->private_data = spidev;
545 nonseekable_open(inode, filp);
546 }
547 } else
548 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
549
550 mutex_unlock(&device_list_lock);
551 return status;
552}
553
554static int spidev_release(struct inode *inode, struct file *filp)
555{
556 struct spidev_data *spidev;
557 int status = 0;
558
559 mutex_lock(&device_list_lock);
560 spidev = filp->private_data;
561 filp->private_data = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700562
563 /* last close? */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700564 spidev->users--;
565 if (!spidev->users) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700566 int dofree;
567
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700568 kfree(spidev->buffer);
569 spidev->buffer = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700570 kfree(spidev->bufferrx);
571 spidev->bufferrx = 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
Mike Frysingerdb389b62009-12-14 14:20:22 -0800612static int __devinit 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);
David Brownellb2c8dad2008-06-05 22:45:50 -0700642 status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
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
Mike Frysingerdb389b62009-12-14 14:20:22 -0800661static int __devexit 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;
David Brownellb2c8dad2008-06-05 22:45:50 -0700668 spi_set_drvdata(spi, NULL);
David Brownell25d5cb42008-05-23 13:05:03 -0700669 spin_unlock_irq(&spidev->spi_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700670
David Brownell25d5cb42008-05-23 13:05:03 -0700671 /* prevent new opens */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700672 mutex_lock(&device_list_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700673 list_del(&spidev->device_entry);
David Brownellb2c8dad2008-06-05 22:45:50 -0700674 device_destroy(spidev_class, spidev->devt);
675 clear_bit(MINOR(spidev->devt), minors);
676 if (spidev->users == 0)
677 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700678 mutex_unlock(&device_list_lock);
679
680 return 0;
681}
682
Mike Frysingerdb389b62009-12-14 14:20:22 -0800683static struct spi_driver spidev_spi_driver = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700684 .driver = {
685 .name = "spidev",
686 .owner = THIS_MODULE,
687 },
688 .probe = spidev_probe,
689 .remove = __devexit_p(spidev_remove),
690
691 /* NOTE: suspend/resume methods are not necessary here.
692 * We don't do anything except pass the requests to/from
693 * the underlying controller. The refrigerator handles
694 * most issues; the controller driver handles the rest.
695 */
696};
697
698/*-------------------------------------------------------------------------*/
699
700static int __init spidev_init(void)
701{
702 int status;
703
704 /* Claim our 256 reserved device numbers. Then register a class
705 * that will key udev/mdev to add/remove /dev nodes. Last, register
706 * the driver which manages those device numbers.
707 */
708 BUILD_BUG_ON(N_SPI_MINORS > 256);
709 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
710 if (status < 0)
711 return status;
712
David Brownellb2c8dad2008-06-05 22:45:50 -0700713 spidev_class = class_create(THIS_MODULE, "spidev");
714 if (IS_ERR(spidev_class)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700715 status = PTR_ERR(spidev_class);
716 goto error_class;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700717 }
718
Mike Frysingerdb389b62009-12-14 14:20:22 -0800719 status = spi_register_driver(&spidev_spi_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 if (status < 0)
721 goto error_register;
722
723 if (busnum != -1 && chipselect != -1) {
724 struct spi_board_info chip = {
725 .modalias = "spidev",
726 .mode = spimode,
727 .bus_num = busnum,
728 .chip_select = chipselect,
729 .max_speed_hz = maxspeed,
730 };
731
732 struct spi_master *master;
733
734 master = spi_busnum_to_master(busnum);
735 if (!master) {
736 status = -ENODEV;
737 goto error_busnum;
738 }
739
740 /* We create a virtual device that will sit on the bus */
741 spi = spi_new_device(master, &chip);
742 if (!spi) {
743 status = -EBUSY;
744 goto error_mem;
745 }
746 dev_dbg(&spi->dev, "busnum=%d cs=%d bufsiz=%d maxspeed=%d",
747 busnum, chipselect, bufsiz, maxspeed);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700748 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700749 return 0;
750error_mem:
751error_busnum:
752 spi_unregister_driver(&spidev_spi_driver);
753error_register:
754 class_destroy(spidev_class);
755error_class:
756 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700757 return status;
758}
759module_init(spidev_init);
760
761static void __exit spidev_exit(void)
762{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700763 if (spi) {
764 spi_unregister_device(spi);
765 spi = NULL;
766 }
Mike Frysingerdb389b62009-12-14 14:20:22 -0800767 spi_unregister_driver(&spidev_spi_driver);
David Brownellb2c8dad2008-06-05 22:45:50 -0700768 class_destroy(spidev_class);
Mike Frysingerdb389b62009-12-14 14:20:22 -0800769 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700770}
771module_exit(spidev_exit);
772
773MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
774MODULE_DESCRIPTION("User mode SPI device interface");
775MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700776MODULE_ALIAS("spi:spidev");