blob: 5c42974c13431b3155d495c17e4b234749b47600 [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;
José Adolfo Galdámez900469d2015-06-20 23:45:36 -0600270 /* Check total length of transfers. Also check each
271 * transfer length to avoid arithmetic overflow.
272 */
273 if (total > bufsiz || k_tmp->len > bufsiz) {
Domen Puncerda90fa82007-05-23 13:57:39 -0700274 status = -EMSGSIZE;
275 goto done;
276 }
277
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700278 if (u_tmp->rx_buf) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700279 k_tmp->rx_buf = bufrx;
David Brownell96ddbf52007-08-10 13:01:09 -0700280 if (!access_ok(VERIFY_WRITE, (u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000281 (uintptr_t) u_tmp->rx_buf,
David Brownell96ddbf52007-08-10 13:01:09 -0700282 u_tmp->len))
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700283 goto done;
284 }
285 if (u_tmp->tx_buf) {
286 k_tmp->tx_buf = buf;
David Brownell4917d922007-07-17 04:04:04 -0700287 if (copy_from_user(buf, (const u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000288 (uintptr_t) u_tmp->tx_buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700289 u_tmp->len))
290 goto done;
291 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700292 buf += k_tmp->len;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293 bufrx += k_tmp->len;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700294
295 k_tmp->cs_change = !!u_tmp->cs_change;
296 k_tmp->bits_per_word = u_tmp->bits_per_word;
297 k_tmp->delay_usecs = u_tmp->delay_usecs;
298 k_tmp->speed_hz = u_tmp->speed_hz;
299#ifdef VERBOSE
Florian Fainelli41df70d2009-12-07 14:28:43 +0000300 dev_dbg(&spidev->spi->dev,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700301 " xfer len %zd %s%s%s%dbits %u usec %uHz\n",
302 u_tmp->len,
303 u_tmp->rx_buf ? "rx " : "",
304 u_tmp->tx_buf ? "tx " : "",
305 u_tmp->cs_change ? "cs " : "",
Florian Fainelli41df70d2009-12-07 14:28:43 +0000306 u_tmp->bits_per_word ? : spidev->spi->bits_per_word,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700307 u_tmp->delay_usecs,
Florian Fainelli41df70d2009-12-07 14:28:43 +0000308 u_tmp->speed_hz ? : spidev->spi->max_speed_hz);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700309#endif
310 spi_message_add_tail(k_tmp, &msg);
311 }
312
David Brownell25d5cb42008-05-23 13:05:03 -0700313 status = spidev_sync(spidev, &msg);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700314 if (status < 0)
315 goto done;
316
317 /* copy any rx data out of bounce buffer */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700318 buf = spidev->bufferrx;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700319 for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) {
320 if (u_tmp->rx_buf) {
David Brownell4917d922007-07-17 04:04:04 -0700321 if (__copy_to_user((u8 __user *)
Al Viro142956a2007-10-29 05:11:28 +0000322 (uintptr_t) u_tmp->rx_buf, buf,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700323 u_tmp->len)) {
324 status = -EFAULT;
325 goto done;
326 }
327 }
328 buf += u_tmp->len;
329 }
330 status = total;
331
332done:
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700333 kfree(k_xfers);
334 return status;
335}
336
Alan Cox4ef754b2008-07-23 21:29:55 -0700337static long
338spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700339{
340 int err = 0;
341 int retval = 0;
342 struct spidev_data *spidev;
343 struct spi_device *spi;
344 u32 tmp;
345 unsigned n_ioc;
346 struct spi_ioc_transfer *ioc;
347
348 /* Check type and command number */
349 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
350 return -ENOTTY;
351
352 /* Check access direction once here; don't repeat below.
353 * IOC_DIR is from the user perspective, while access_ok is
354 * from the kernel perspective; so they look reversed.
355 */
356 if (_IOC_DIR(cmd) & _IOC_READ)
357 err = !access_ok(VERIFY_WRITE,
358 (void __user *)arg, _IOC_SIZE(cmd));
359 if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE)
360 err = !access_ok(VERIFY_READ,
361 (void __user *)arg, _IOC_SIZE(cmd));
362 if (err)
363 return -EFAULT;
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:
387 retval = __put_user(spi->mode & SPI_MODE_MASK,
388 (__u8 __user *)arg);
389 break;
390 case SPI_IOC_RD_LSB_FIRST:
391 retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
392 (__u8 __user *)arg);
393 break;
394 case SPI_IOC_RD_BITS_PER_WORD:
395 retval = __put_user(spi->bits_per_word, (__u8 __user *)arg);
396 break;
397 case SPI_IOC_RD_MAX_SPEED_HZ:
398 retval = __put_user(spi->max_speed_hz, (__u32 __user *)arg);
399 break;
400
401 /* write requests */
402 case SPI_IOC_WR_MODE:
403 retval = __get_user(tmp, (u8 __user *)arg);
404 if (retval == 0) {
405 u8 save = spi->mode;
406
407 if (tmp & ~SPI_MODE_MASK) {
408 retval = -EINVAL;
409 break;
410 }
411
412 tmp |= spi->mode & ~SPI_MODE_MASK;
413 spi->mode = (u8)tmp;
414 retval = spi_setup(spi);
415 if (retval < 0)
416 spi->mode = save;
417 else
418 dev_dbg(&spi->dev, "spi mode %02x\n", tmp);
419 }
420 break;
421 case SPI_IOC_WR_LSB_FIRST:
422 retval = __get_user(tmp, (__u8 __user *)arg);
423 if (retval == 0) {
424 u8 save = spi->mode;
425
426 if (tmp)
427 spi->mode |= SPI_LSB_FIRST;
428 else
429 spi->mode &= ~SPI_LSB_FIRST;
430 retval = spi_setup(spi);
431 if (retval < 0)
432 spi->mode = save;
433 else
434 dev_dbg(&spi->dev, "%csb first\n",
435 tmp ? 'l' : 'm');
436 }
437 break;
438 case SPI_IOC_WR_BITS_PER_WORD:
439 retval = __get_user(tmp, (__u8 __user *)arg);
440 if (retval == 0) {
441 u8 save = spi->bits_per_word;
442
443 spi->bits_per_word = tmp;
444 retval = spi_setup(spi);
445 if (retval < 0)
446 spi->bits_per_word = save;
447 else
448 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
449 }
450 break;
451 case SPI_IOC_WR_MAX_SPEED_HZ:
452 retval = __get_user(tmp, (__u32 __user *)arg);
453 if (retval == 0) {
454 u32 save = spi->max_speed_hz;
455
456 spi->max_speed_hz = tmp;
457 retval = spi_setup(spi);
458 if (retval < 0)
459 spi->max_speed_hz = save;
460 else
461 dev_dbg(&spi->dev, "%d Hz (max)\n", tmp);
462 }
463 break;
464
465 default:
466 /* segmented and/or full-duplex I/O request */
467 if (_IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
David Brownell25d5cb42008-05-23 13:05:03 -0700468 || _IOC_DIR(cmd) != _IOC_WRITE) {
469 retval = -ENOTTY;
470 break;
471 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700472
473 tmp = _IOC_SIZE(cmd);
474 if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) {
475 retval = -EINVAL;
476 break;
477 }
478 n_ioc = tmp / sizeof(struct spi_ioc_transfer);
479 if (n_ioc == 0)
480 break;
481
482 /* copy into scratch area */
483 ioc = kmalloc(tmp, GFP_KERNEL);
484 if (!ioc) {
485 retval = -ENOMEM;
486 break;
487 }
488 if (__copy_from_user(ioc, (void __user *)arg, tmp)) {
Florin Malita9bea3f22007-05-23 13:57:45 -0700489 kfree(ioc);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700490 retval = -EFAULT;
491 break;
492 }
493
494 /* translate to spi_message, execute */
495 retval = spidev_message(spidev, ioc, n_ioc);
496 kfree(ioc);
497 break;
498 }
Alan Cox4ef754b2008-07-23 21:29:55 -0700499
500 mutex_unlock(&spidev->buf_lock);
David Brownell25d5cb42008-05-23 13:05:03 -0700501 spi_dev_put(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700502 return retval;
503}
504
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100505#ifdef CONFIG_COMPAT
506static long
507spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
508{
509 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
510}
511#else
512#define spidev_compat_ioctl NULL
513#endif /* CONFIG_COMPAT */
514
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700515static int spidev_open(struct inode *inode, struct file *filp)
516{
517 struct spidev_data *spidev;
518 int status = -ENXIO;
519
520 mutex_lock(&device_list_lock);
521
522 list_for_each_entry(spidev, &device_list, device_entry) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700523 if (spidev->devt == inode->i_rdev) {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700524 status = 0;
525 break;
526 }
527 }
528 if (status == 0) {
529 if (!spidev->buffer) {
530 spidev->buffer = kmalloc(bufsiz, GFP_KERNEL);
531 if (!spidev->buffer) {
532 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
533 status = -ENOMEM;
534 }
535 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700536 if (!spidev->bufferrx) {
537 spidev->bufferrx = kmalloc(bufsiz, GFP_KERNEL);
538 if (!spidev->bufferrx) {
539 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
540 kfree(spidev->buffer);
541 spidev->buffer = NULL;
542 status = -ENOMEM;
543 }
544 }
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700545 if (status == 0) {
546 spidev->users++;
547 filp->private_data = spidev;
548 nonseekable_open(inode, filp);
549 }
550 } else
551 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
552
553 mutex_unlock(&device_list_lock);
554 return status;
555}
556
557static int spidev_release(struct inode *inode, struct file *filp)
558{
559 struct spidev_data *spidev;
560 int status = 0;
561
562 mutex_lock(&device_list_lock);
563 spidev = filp->private_data;
564 filp->private_data = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700565
566 /* last close? */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700567 spidev->users--;
568 if (!spidev->users) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700569 int dofree;
570
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700571 kfree(spidev->buffer);
572 spidev->buffer = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700573 kfree(spidev->bufferrx);
574 spidev->bufferrx = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700575
576 /* ... after we unbound from the underlying device? */
577 spin_lock_irq(&spidev->spi_lock);
578 dofree = (spidev->spi == NULL);
579 spin_unlock_irq(&spidev->spi_lock);
580
581 if (dofree)
582 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700583 }
584 mutex_unlock(&device_list_lock);
585
586 return status;
587}
588
Alexey Dobriyan828c0952009-10-01 15:43:56 -0700589static const struct file_operations spidev_fops = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700590 .owner = THIS_MODULE,
591 /* REVISIT switch to aio primitives, so that userspace
592 * gets more complete API coverage. It'll simplify things
593 * too, except for the locking.
594 */
595 .write = spidev_write,
596 .read = spidev_read,
Alan Cox4ef754b2008-07-23 21:29:55 -0700597 .unlocked_ioctl = spidev_ioctl,
Bernhard Walle7d48ec32011-02-03 09:37:18 +0100598 .compat_ioctl = spidev_compat_ioctl,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700599 .open = spidev_open,
600 .release = spidev_release,
Arnd Bergmann6038f372010-08-15 18:52:59 +0200601 .llseek = no_llseek,
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700602};
603
604/*-------------------------------------------------------------------------*/
605
606/* The main reason to have this class is to make mdev/udev create the
607 * /dev/spidevB.C character device nodes exposing our userspace API.
608 * It also simplifies memory management.
609 */
610
David Brownellb2c8dad2008-06-05 22:45:50 -0700611static struct class *spidev_class;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700612
613/*-------------------------------------------------------------------------*/
614
Mike Frysingerdb389b62009-12-14 14:20:22 -0800615static int __devinit spidev_probe(struct spi_device *spi)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700616{
617 struct spidev_data *spidev;
618 int status;
619 unsigned long minor;
620
621 /* Allocate driver data */
622 spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
623 if (!spidev)
624 return -ENOMEM;
625
626 /* Initialize the driver data */
627 spidev->spi = spi;
David Brownell25d5cb42008-05-23 13:05:03 -0700628 spin_lock_init(&spidev->spi_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700629 mutex_init(&spidev->buf_lock);
630
631 INIT_LIST_HEAD(&spidev->device_entry);
632
633 /* If we can allocate a minor number, hook up this device.
634 * Reusing minors is fine so long as udev or mdev is working.
635 */
636 mutex_lock(&device_list_lock);
Domen Puncer0a4dd772007-05-15 23:57:05 -0700637 minor = find_first_zero_bit(minors, N_SPI_MINORS);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700638 if (minor < N_SPI_MINORS) {
David Brownellb2c8dad2008-06-05 22:45:50 -0700639 struct device *dev;
640
641 spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
Greg Kroah-Hartmana9b12612008-07-21 20:03:34 -0700642 dev = device_create(spidev_class, &spi->dev, spidev->devt,
643 spidev, "spidev%d.%d",
644 spi->master->bus_num, spi->chip_select);
David Brownellb2c8dad2008-06-05 22:45:50 -0700645 status = IS_ERR(dev) ? PTR_ERR(dev) : 0;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700646 } else {
647 dev_dbg(&spi->dev, "no minor number available!\n");
648 status = -ENODEV;
649 }
650 if (status == 0) {
651 set_bit(minor, minors);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700652 list_add(&spidev->device_entry, &device_list);
653 }
654 mutex_unlock(&device_list_lock);
655
Wolfgang Ockeraaacf4b2008-12-01 13:13:52 -0800656 if (status == 0)
657 spi_set_drvdata(spi, spidev);
658 else
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700659 kfree(spidev);
660
661 return status;
662}
663
Mike Frysingerdb389b62009-12-14 14:20:22 -0800664static int __devexit spidev_remove(struct spi_device *spi)
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700665{
David Brownellb2c8dad2008-06-05 22:45:50 -0700666 struct spidev_data *spidev = spi_get_drvdata(spi);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700667
David Brownell25d5cb42008-05-23 13:05:03 -0700668 /* make sure ops on existing fds can abort cleanly */
669 spin_lock_irq(&spidev->spi_lock);
670 spidev->spi = NULL;
David Brownellb2c8dad2008-06-05 22:45:50 -0700671 spi_set_drvdata(spi, NULL);
David Brownell25d5cb42008-05-23 13:05:03 -0700672 spin_unlock_irq(&spidev->spi_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700673
David Brownell25d5cb42008-05-23 13:05:03 -0700674 /* prevent new opens */
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700675 mutex_lock(&device_list_lock);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700676 list_del(&spidev->device_entry);
David Brownellb2c8dad2008-06-05 22:45:50 -0700677 device_destroy(spidev_class, spidev->devt);
678 clear_bit(MINOR(spidev->devt), minors);
679 if (spidev->users == 0)
680 kfree(spidev);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700681 mutex_unlock(&device_list_lock);
682
683 return 0;
684}
685
Mike Frysingerdb389b62009-12-14 14:20:22 -0800686static struct spi_driver spidev_spi_driver = {
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700687 .driver = {
688 .name = "spidev",
689 .owner = THIS_MODULE,
690 },
691 .probe = spidev_probe,
692 .remove = __devexit_p(spidev_remove),
693
694 /* NOTE: suspend/resume methods are not necessary here.
695 * We don't do anything except pass the requests to/from
696 * the underlying controller. The refrigerator handles
697 * most issues; the controller driver handles the rest.
698 */
699};
700
701/*-------------------------------------------------------------------------*/
702
703static int __init spidev_init(void)
704{
705 int status;
706
707 /* Claim our 256 reserved device numbers. Then register a class
708 * that will key udev/mdev to add/remove /dev nodes. Last, register
709 * the driver which manages those device numbers.
710 */
711 BUILD_BUG_ON(N_SPI_MINORS > 256);
712 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
713 if (status < 0)
714 return status;
715
David Brownellb2c8dad2008-06-05 22:45:50 -0700716 spidev_class = class_create(THIS_MODULE, "spidev");
717 if (IS_ERR(spidev_class)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 status = PTR_ERR(spidev_class);
719 goto error_class;
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700720 }
721
Mike Frysingerdb389b62009-12-14 14:20:22 -0800722 status = spi_register_driver(&spidev_spi_driver);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 if (status < 0)
724 goto error_register;
725
726 if (busnum != -1 && chipselect != -1) {
727 struct spi_board_info chip = {
728 .modalias = "spidev",
729 .mode = spimode,
730 .bus_num = busnum,
731 .chip_select = chipselect,
732 .max_speed_hz = maxspeed,
733 };
734
735 struct spi_master *master;
736
737 master = spi_busnum_to_master(busnum);
738 if (!master) {
739 status = -ENODEV;
740 goto error_busnum;
741 }
742
743 /* We create a virtual device that will sit on the bus */
744 spi = spi_new_device(master, &chip);
745 if (!spi) {
746 status = -EBUSY;
747 goto error_mem;
748 }
749 dev_dbg(&spi->dev, "busnum=%d cs=%d bufsiz=%d maxspeed=%d",
750 busnum, chipselect, bufsiz, maxspeed);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700751 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700752 return 0;
753error_mem:
754error_busnum:
755 spi_unregister_driver(&spidev_spi_driver);
756error_register:
757 class_destroy(spidev_class);
758error_class:
759 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700760 return status;
761}
762module_init(spidev_init);
763
764static void __exit spidev_exit(void)
765{
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 if (spi) {
767 spi_unregister_device(spi);
768 spi = NULL;
769 }
Mike Frysingerdb389b62009-12-14 14:20:22 -0800770 spi_unregister_driver(&spidev_spi_driver);
David Brownellb2c8dad2008-06-05 22:45:50 -0700771 class_destroy(spidev_class);
Mike Frysingerdb389b62009-12-14 14:20:22 -0800772 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
Andrea Paterniani814a8d52007-05-08 00:32:15 -0700773}
774module_exit(spidev_exit);
775
776MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
777MODULE_DESCRIPTION("User mode SPI device interface");
778MODULE_LICENSE("GPL");
Anton Vorontsove0626e32009-09-22 16:46:08 -0700779MODULE_ALIAS("spi:spidev");