Commit | Line | Data |
---|---|---|
814a8d50 | 1 | /* |
ca632f55 | 2 | * Simple synchronous userspace interface to SPI devices |
814a8d50 AP |
3 | * |
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. | |
814a8d50 AP |
17 | */ |
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> | |
b2c8dadd | 24 | #include <linux/err.h> |
814a8d50 AP |
25 | #include <linux/list.h> |
26 | #include <linux/errno.h> | |
27 | #include <linux/mutex.h> | |
28 | #include <linux/slab.h> | |
7d48ec36 | 29 | #include <linux/compat.h> |
880cfd43 MR |
30 | #include <linux/of.h> |
31 | #include <linux/of_device.h> | |
814a8d50 AP |
32 | |
33 | #include <linux/spi/spi.h> | |
34 | #include <linux/spi/spidev.h> | |
35 | ||
95c63cfb | 36 | #include <linux/uaccess.h> |
814a8d50 AP |
37 | |
38 | ||
39 | /* | |
b595076a | 40 | * This supports access to SPI devices using normal userspace I/O calls. |
814a8d50 AP |
41 | * Note that while traditional UNIX/POSIX I/O semantics are half duplex, |
42 | * and often mask message boundaries, full SPI support requires full duplex | |
137f1188 | 43 | * transfers. There are several kinds of internal message boundaries to |
814a8d50 AP |
44 | * handle chipselect management and other protocol options. |
45 | * | |
46 | * SPI has a character major number assigned. We allocate minor numbers | |
47 | * dynamically using a bitmask. You must use hotplug tools, such as udev | |
48 | * (or mdev with busybox) to create and destroy the /dev/spidevB.C device | |
49 | * nodes, since there is no fixed association of minor numbers with any | |
50 | * particular SPI bus or device. | |
51 | */ | |
52 | #define SPIDEV_MAJOR 153 /* assigned */ | |
53 | #define N_SPI_MINORS 32 /* ... up to 256 */ | |
54 | ||
8ae1c924 | 55 | static DECLARE_BITMAP(minors, N_SPI_MINORS); |
814a8d50 AP |
56 | |
57 | ||
6f166e38 | 58 | /* Bit masks for spi_device.mode management. Note that incorrect |
b55f627f DB |
59 | * settings for some settings can cause *lots* of trouble for other |
60 | * devices on a shared bus: | |
6f166e38 | 61 | * |
b55f627f DB |
62 | * - CS_HIGH ... this device will be active when it shouldn't be |
63 | * - 3WIRE ... when active, it won't behave as it should | |
64 | * - NO_CS ... there will be no explicit message boundaries; this | |
65 | * is completely incompatible with the shared bus model | |
66 | * - READY ... transfers may proceed when they shouldn't. | |
67 | * | |
68 | * REVISIT should changing those flags be privileged? | |
6f166e38 AV |
69 | */ |
70 | #define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \ | |
b55f627f | 71 | | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \ |
dc64d39b GU |
72 | | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \ |
73 | | SPI_TX_QUAD | SPI_RX_DUAL | SPI_RX_QUAD) | |
814a8d50 AP |
74 | |
75 | struct spidev_data { | |
b2c8dadd | 76 | dev_t devt; |
25d5cb4b | 77 | spinlock_t spi_lock; |
814a8d50 AP |
78 | struct spi_device *spi; |
79 | struct list_head device_entry; | |
80 | ||
865f6d19 | 81 | /* TX/RX buffers are NULL unless this device is open (users > 0) */ |
814a8d50 AP |
82 | struct mutex buf_lock; |
83 | unsigned users; | |
865f6d19 RJ |
84 | u8 *tx_buffer; |
85 | u8 *rx_buffer; | |
91690516 | 86 | u32 speed_hz; |
814a8d50 AP |
87 | }; |
88 | ||
89 | static LIST_HEAD(device_list); | |
90 | static DEFINE_MUTEX(device_list_lock); | |
91 | ||
92 | static unsigned bufsiz = 4096; | |
93 | module_param(bufsiz, uint, S_IRUGO); | |
94 | MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message"); | |
95 | ||
96 | /*-------------------------------------------------------------------------*/ | |
97 | ||
25d5cb4b DB |
98 | /* |
99 | * We can't use the standard synchronous wrappers for file I/O; we | |
100 | * need to protect against async removal of the underlying spi_device. | |
101 | */ | |
102 | static void spidev_complete(void *arg) | |
103 | { | |
104 | complete(arg); | |
105 | } | |
106 | ||
107 | static ssize_t | |
108 | spidev_sync(struct spidev_data *spidev, struct spi_message *message) | |
109 | { | |
110 | DECLARE_COMPLETION_ONSTACK(done); | |
111 | int status; | |
112 | ||
113 | message->complete = spidev_complete; | |
114 | message->context = &done; | |
115 | ||
116 | spin_lock_irq(&spidev->spi_lock); | |
117 | if (spidev->spi == NULL) | |
118 | status = -ESHUTDOWN; | |
119 | else | |
120 | status = spi_async(spidev->spi, message); | |
121 | spin_unlock_irq(&spidev->spi_lock); | |
122 | ||
123 | if (status == 0) { | |
124 | wait_for_completion(&done); | |
125 | status = message->status; | |
126 | if (status == 0) | |
127 | status = message->actual_length; | |
128 | } | |
129 | return status; | |
130 | } | |
131 | ||
132 | static inline ssize_t | |
133 | spidev_sync_write(struct spidev_data *spidev, size_t len) | |
134 | { | |
135 | struct spi_transfer t = { | |
865f6d19 | 136 | .tx_buf = spidev->tx_buffer, |
25d5cb4b | 137 | .len = len, |
91690516 | 138 | .speed_hz = spidev->speed_hz, |
25d5cb4b DB |
139 | }; |
140 | struct spi_message m; | |
141 | ||
142 | spi_message_init(&m); | |
143 | spi_message_add_tail(&t, &m); | |
144 | return spidev_sync(spidev, &m); | |
145 | } | |
146 | ||
147 | static inline ssize_t | |
148 | spidev_sync_read(struct spidev_data *spidev, size_t len) | |
149 | { | |
150 | struct spi_transfer t = { | |
865f6d19 | 151 | .rx_buf = spidev->rx_buffer, |
25d5cb4b | 152 | .len = len, |
91690516 | 153 | .speed_hz = spidev->speed_hz, |
25d5cb4b DB |
154 | }; |
155 | struct spi_message m; | |
156 | ||
157 | spi_message_init(&m); | |
158 | spi_message_add_tail(&t, &m); | |
159 | return spidev_sync(spidev, &m); | |
160 | } | |
161 | ||
162 | /*-------------------------------------------------------------------------*/ | |
163 | ||
814a8d50 AP |
164 | /* Read-only message with current device setup */ |
165 | static ssize_t | |
166 | spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos) | |
167 | { | |
168 | struct spidev_data *spidev; | |
814a8d50 AP |
169 | ssize_t status = 0; |
170 | ||
171 | /* chipselect only toggles at start or end of operation */ | |
172 | if (count > bufsiz) | |
173 | return -EMSGSIZE; | |
174 | ||
175 | spidev = filp->private_data; | |
814a8d50 AP |
176 | |
177 | mutex_lock(&spidev->buf_lock); | |
25d5cb4b | 178 | status = spidev_sync_read(spidev, count); |
4b1295b0 | 179 | if (status > 0) { |
814a8d50 AP |
180 | unsigned long missing; |
181 | ||
865f6d19 | 182 | missing = copy_to_user(buf, spidev->rx_buffer, status); |
4b1295b0 | 183 | if (missing == status) |
814a8d50 AP |
184 | status = -EFAULT; |
185 | else | |
4b1295b0 | 186 | status = status - missing; |
814a8d50 AP |
187 | } |
188 | mutex_unlock(&spidev->buf_lock); | |
189 | ||
190 | return status; | |
191 | } | |
192 | ||
193 | /* Write-only message with current device setup */ | |
194 | static ssize_t | |
195 | spidev_write(struct file *filp, const char __user *buf, | |
196 | size_t count, loff_t *f_pos) | |
197 | { | |
198 | struct spidev_data *spidev; | |
814a8d50 AP |
199 | ssize_t status = 0; |
200 | unsigned long missing; | |
201 | ||
202 | /* chipselect only toggles at start or end of operation */ | |
203 | if (count > bufsiz) | |
204 | return -EMSGSIZE; | |
205 | ||
206 | spidev = filp->private_data; | |
814a8d50 AP |
207 | |
208 | mutex_lock(&spidev->buf_lock); | |
865f6d19 | 209 | missing = copy_from_user(spidev->tx_buffer, buf, count); |
95c63cfb | 210 | if (missing == 0) |
25d5cb4b | 211 | status = spidev_sync_write(spidev, count); |
95c63cfb | 212 | else |
814a8d50 AP |
213 | status = -EFAULT; |
214 | mutex_unlock(&spidev->buf_lock); | |
215 | ||
216 | return status; | |
217 | } | |
218 | ||
219 | static int spidev_message(struct spidev_data *spidev, | |
220 | struct spi_ioc_transfer *u_xfers, unsigned n_xfers) | |
221 | { | |
222 | struct spi_message msg; | |
223 | struct spi_transfer *k_xfers; | |
224 | struct spi_transfer *k_tmp; | |
225 | struct spi_ioc_transfer *u_tmp; | |
9a12bff7 | 226 | unsigned n, total, tx_total, rx_total; |
865f6d19 | 227 | u8 *tx_buf, *rx_buf; |
814a8d50 AP |
228 | int status = -EFAULT; |
229 | ||
230 | spi_message_init(&msg); | |
231 | k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL); | |
232 | if (k_xfers == NULL) | |
233 | return -ENOMEM; | |
234 | ||
235 | /* Construct spi_message, copying any tx data to bounce buffer. | |
236 | * We walk the array of user-provided transfers, using each one | |
237 | * to initialize a kernel version of the same transfer. | |
238 | */ | |
865f6d19 RJ |
239 | tx_buf = spidev->tx_buffer; |
240 | rx_buf = spidev->rx_buffer; | |
814a8d50 | 241 | total = 0; |
9a12bff7 IA |
242 | tx_total = 0; |
243 | rx_total = 0; | |
814a8d50 AP |
244 | for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers; |
245 | n; | |
246 | n--, k_tmp++, u_tmp++) { | |
247 | k_tmp->len = u_tmp->len; | |
248 | ||
da90fa8f | 249 | total += k_tmp->len; |
9a12bff7 IA |
250 | /* Since the function returns the total length of transfers |
251 | * on success, restrict the total to positive int values to | |
252 | * avoid the return value looking like an error. | |
253 | */ | |
254 | if (total > INT_MAX) { | |
da90fa8f DP |
255 | status = -EMSGSIZE; |
256 | goto done; | |
257 | } | |
258 | ||
814a8d50 | 259 | if (u_tmp->rx_buf) { |
9a12bff7 IA |
260 | /* this transfer needs space in RX bounce buffer */ |
261 | rx_total += k_tmp->len; | |
262 | if (rx_total > bufsiz) { | |
263 | status = -EMSGSIZE; | |
264 | goto done; | |
265 | } | |
865f6d19 | 266 | k_tmp->rx_buf = rx_buf; |
96ddbf50 | 267 | if (!access_ok(VERIFY_WRITE, (u8 __user *) |
142956af | 268 | (uintptr_t) u_tmp->rx_buf, |
96ddbf50 | 269 | u_tmp->len)) |
814a8d50 | 270 | goto done; |
9a12bff7 | 271 | rx_buf += k_tmp->len; |
814a8d50 AP |
272 | } |
273 | if (u_tmp->tx_buf) { | |
9a12bff7 IA |
274 | /* this transfer needs space in TX bounce buffer */ |
275 | tx_total += k_tmp->len; | |
276 | if (tx_total > bufsiz) { | |
277 | status = -EMSGSIZE; | |
278 | goto done; | |
279 | } | |
865f6d19 RJ |
280 | k_tmp->tx_buf = tx_buf; |
281 | if (copy_from_user(tx_buf, (const u8 __user *) | |
142956af | 282 | (uintptr_t) u_tmp->tx_buf, |
814a8d50 AP |
283 | u_tmp->len)) |
284 | goto done; | |
9a12bff7 | 285 | tx_buf += k_tmp->len; |
814a8d50 | 286 | } |
814a8d50 AP |
287 | |
288 | k_tmp->cs_change = !!u_tmp->cs_change; | |
dc64d39b GU |
289 | k_tmp->tx_nbits = u_tmp->tx_nbits; |
290 | k_tmp->rx_nbits = u_tmp->rx_nbits; | |
814a8d50 AP |
291 | k_tmp->bits_per_word = u_tmp->bits_per_word; |
292 | k_tmp->delay_usecs = u_tmp->delay_usecs; | |
293 | k_tmp->speed_hz = u_tmp->speed_hz; | |
91690516 MB |
294 | if (!k_tmp->speed_hz) |
295 | k_tmp->speed_hz = spidev->speed_hz; | |
814a8d50 | 296 | #ifdef VERBOSE |
41df70d9 | 297 | dev_dbg(&spidev->spi->dev, |
814a8d50 AP |
298 | " 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 " : "", | |
41df70d9 | 303 | u_tmp->bits_per_word ? : spidev->spi->bits_per_word, |
814a8d50 | 304 | u_tmp->delay_usecs, |
41df70d9 | 305 | u_tmp->speed_hz ? : spidev->spi->max_speed_hz); |
814a8d50 AP |
306 | #endif |
307 | spi_message_add_tail(k_tmp, &msg); | |
308 | } | |
309 | ||
25d5cb4b | 310 | status = spidev_sync(spidev, &msg); |
814a8d50 AP |
311 | if (status < 0) |
312 | goto done; | |
313 | ||
314 | /* copy any rx data out of bounce buffer */ | |
865f6d19 | 315 | rx_buf = spidev->rx_buffer; |
814a8d50 AP |
316 | for (n = n_xfers, u_tmp = u_xfers; n; n--, u_tmp++) { |
317 | if (u_tmp->rx_buf) { | |
4917d927 | 318 | if (__copy_to_user((u8 __user *) |
865f6d19 | 319 | (uintptr_t) u_tmp->rx_buf, rx_buf, |
814a8d50 AP |
320 | u_tmp->len)) { |
321 | status = -EFAULT; | |
322 | goto done; | |
323 | } | |
9a12bff7 | 324 | rx_buf += u_tmp->len; |
814a8d50 | 325 | } |
814a8d50 AP |
326 | } |
327 | status = total; | |
328 | ||
329 | done: | |
814a8d50 AP |
330 | kfree(k_xfers); |
331 | return status; | |
332 | } | |
333 | ||
7782a1a9 IA |
334 | static struct spi_ioc_transfer * |
335 | spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc, | |
336 | unsigned *n_ioc) | |
337 | { | |
338 | struct spi_ioc_transfer *ioc; | |
339 | u32 tmp; | |
340 | ||
341 | /* Check type, command number and direction */ | |
342 | if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC | |
343 | || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0)) | |
344 | || _IOC_DIR(cmd) != _IOC_WRITE) | |
345 | return ERR_PTR(-ENOTTY); | |
346 | ||
347 | tmp = _IOC_SIZE(cmd); | |
348 | if ((tmp % sizeof(struct spi_ioc_transfer)) != 0) | |
349 | return ERR_PTR(-EINVAL); | |
350 | *n_ioc = tmp / sizeof(struct spi_ioc_transfer); | |
351 | if (*n_ioc == 0) | |
352 | return NULL; | |
353 | ||
354 | /* copy into scratch area */ | |
355 | ioc = kmalloc(tmp, GFP_KERNEL); | |
356 | if (!ioc) | |
357 | return ERR_PTR(-ENOMEM); | |
358 | if (__copy_from_user(ioc, u_ioc, tmp)) { | |
359 | kfree(ioc); | |
360 | return ERR_PTR(-EFAULT); | |
361 | } | |
362 | return ioc; | |
363 | } | |
364 | ||
4ef754b7 AC |
365 | static long |
366 | spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
814a8d50 AP |
367 | { |
368 | int err = 0; | |
369 | int retval = 0; | |
370 | struct spidev_data *spidev; | |
371 | struct spi_device *spi; | |
372 | u32 tmp; | |
373 | unsigned n_ioc; | |
374 | struct spi_ioc_transfer *ioc; | |
375 | ||
376 | /* Check type and command number */ | |
377 | if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC) | |
378 | return -ENOTTY; | |
379 | ||
380 | /* Check access direction once here; don't repeat below. | |
381 | * IOC_DIR is from the user perspective, while access_ok is | |
382 | * from the kernel perspective; so they look reversed. | |
383 | */ | |
384 | if (_IOC_DIR(cmd) & _IOC_READ) | |
385 | err = !access_ok(VERIFY_WRITE, | |
386 | (void __user *)arg, _IOC_SIZE(cmd)); | |
387 | if (err == 0 && _IOC_DIR(cmd) & _IOC_WRITE) | |
388 | err = !access_ok(VERIFY_READ, | |
389 | (void __user *)arg, _IOC_SIZE(cmd)); | |
390 | if (err) | |
391 | return -EFAULT; | |
392 | ||
25d5cb4b DB |
393 | /* guard against device removal before, or while, |
394 | * we issue this ioctl. | |
395 | */ | |
814a8d50 | 396 | spidev = filp->private_data; |
25d5cb4b DB |
397 | spin_lock_irq(&spidev->spi_lock); |
398 | spi = spi_dev_get(spidev->spi); | |
399 | spin_unlock_irq(&spidev->spi_lock); | |
400 | ||
401 | if (spi == NULL) | |
402 | return -ESHUTDOWN; | |
814a8d50 | 403 | |
4ef754b7 AC |
404 | /* use the buffer lock here for triple duty: |
405 | * - prevent I/O (from us) so calling spi_setup() is safe; | |
406 | * - prevent concurrent SPI_IOC_WR_* from morphing | |
407 | * data fields while SPI_IOC_RD_* reads them; | |
408 | * - SPI_IOC_MESSAGE needs the buffer locked "normally". | |
409 | */ | |
410 | mutex_lock(&spidev->buf_lock); | |
411 | ||
814a8d50 AP |
412 | switch (cmd) { |
413 | /* read requests */ | |
414 | case SPI_IOC_RD_MODE: | |
415 | retval = __put_user(spi->mode & SPI_MODE_MASK, | |
416 | (__u8 __user *)arg); | |
417 | break; | |
dc64d39b GU |
418 | case SPI_IOC_RD_MODE32: |
419 | retval = __put_user(spi->mode & SPI_MODE_MASK, | |
420 | (__u32 __user *)arg); | |
421 | break; | |
814a8d50 AP |
422 | case SPI_IOC_RD_LSB_FIRST: |
423 | retval = __put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0, | |
424 | (__u8 __user *)arg); | |
425 | break; | |
426 | case SPI_IOC_RD_BITS_PER_WORD: | |
427 | retval = __put_user(spi->bits_per_word, (__u8 __user *)arg); | |
428 | break; | |
429 | case SPI_IOC_RD_MAX_SPEED_HZ: | |
91690516 | 430 | retval = __put_user(spidev->speed_hz, (__u32 __user *)arg); |
814a8d50 AP |
431 | break; |
432 | ||
433 | /* write requests */ | |
434 | case SPI_IOC_WR_MODE: | |
dc64d39b GU |
435 | case SPI_IOC_WR_MODE32: |
436 | if (cmd == SPI_IOC_WR_MODE) | |
437 | retval = __get_user(tmp, (u8 __user *)arg); | |
438 | else | |
439 | retval = __get_user(tmp, (u32 __user *)arg); | |
814a8d50 | 440 | if (retval == 0) { |
e6456186 | 441 | u32 save = spi->mode; |
814a8d50 AP |
442 | |
443 | if (tmp & ~SPI_MODE_MASK) { | |
444 | retval = -EINVAL; | |
445 | break; | |
446 | } | |
447 | ||
448 | tmp |= spi->mode & ~SPI_MODE_MASK; | |
dc64d39b | 449 | spi->mode = (u16)tmp; |
814a8d50 AP |
450 | retval = spi_setup(spi); |
451 | if (retval < 0) | |
452 | spi->mode = save; | |
453 | else | |
dc64d39b | 454 | dev_dbg(&spi->dev, "spi mode %x\n", tmp); |
814a8d50 AP |
455 | } |
456 | break; | |
457 | case SPI_IOC_WR_LSB_FIRST: | |
458 | retval = __get_user(tmp, (__u8 __user *)arg); | |
459 | if (retval == 0) { | |
e6456186 | 460 | u32 save = spi->mode; |
814a8d50 AP |
461 | |
462 | if (tmp) | |
463 | spi->mode |= SPI_LSB_FIRST; | |
464 | else | |
465 | spi->mode &= ~SPI_LSB_FIRST; | |
466 | retval = spi_setup(spi); | |
467 | if (retval < 0) | |
468 | spi->mode = save; | |
469 | else | |
470 | dev_dbg(&spi->dev, "%csb first\n", | |
471 | tmp ? 'l' : 'm'); | |
472 | } | |
473 | break; | |
474 | case SPI_IOC_WR_BITS_PER_WORD: | |
475 | retval = __get_user(tmp, (__u8 __user *)arg); | |
476 | if (retval == 0) { | |
477 | u8 save = spi->bits_per_word; | |
478 | ||
479 | spi->bits_per_word = tmp; | |
480 | retval = spi_setup(spi); | |
481 | if (retval < 0) | |
482 | spi->bits_per_word = save; | |
483 | else | |
484 | dev_dbg(&spi->dev, "%d bits per word\n", tmp); | |
485 | } | |
486 | break; | |
487 | case SPI_IOC_WR_MAX_SPEED_HZ: | |
488 | retval = __get_user(tmp, (__u32 __user *)arg); | |
489 | if (retval == 0) { | |
490 | u32 save = spi->max_speed_hz; | |
491 | ||
492 | spi->max_speed_hz = tmp; | |
493 | retval = spi_setup(spi); | |
91690516 MB |
494 | if (retval >= 0) |
495 | spidev->speed_hz = tmp; | |
814a8d50 AP |
496 | else |
497 | dev_dbg(&spi->dev, "%d Hz (max)\n", tmp); | |
91690516 | 498 | spi->max_speed_hz = save; |
814a8d50 AP |
499 | } |
500 | break; | |
501 | ||
502 | default: | |
503 | /* segmented and/or full-duplex I/O request */ | |
7782a1a9 IA |
504 | /* Check message and copy into scratch area */ |
505 | ioc = spidev_get_ioc_message(cmd, | |
506 | (struct spi_ioc_transfer __user *)arg, &n_ioc); | |
507 | if (IS_ERR(ioc)) { | |
508 | retval = PTR_ERR(ioc); | |
814a8d50 AP |
509 | break; |
510 | } | |
7782a1a9 IA |
511 | if (!ioc) |
512 | break; /* n_ioc is also 0 */ | |
814a8d50 AP |
513 | |
514 | /* translate to spi_message, execute */ | |
515 | retval = spidev_message(spidev, ioc, n_ioc); | |
516 | kfree(ioc); | |
517 | break; | |
518 | } | |
4ef754b7 AC |
519 | |
520 | mutex_unlock(&spidev->buf_lock); | |
25d5cb4b | 521 | spi_dev_put(spi); |
814a8d50 AP |
522 | return retval; |
523 | } | |
524 | ||
7d48ec36 | 525 | #ifdef CONFIG_COMPAT |
7782a1a9 IA |
526 | static long |
527 | spidev_compat_ioc_message(struct file *filp, unsigned int cmd, | |
528 | unsigned long arg) | |
529 | { | |
530 | struct spi_ioc_transfer __user *u_ioc; | |
531 | int retval = 0; | |
532 | struct spidev_data *spidev; | |
533 | struct spi_device *spi; | |
534 | unsigned n_ioc, n; | |
535 | struct spi_ioc_transfer *ioc; | |
536 | ||
537 | u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg); | |
538 | if (!access_ok(VERIFY_READ, u_ioc, _IOC_SIZE(cmd))) | |
539 | return -EFAULT; | |
540 | ||
541 | /* guard against device removal before, or while, | |
542 | * we issue this ioctl. | |
543 | */ | |
544 | spidev = filp->private_data; | |
545 | spin_lock_irq(&spidev->spi_lock); | |
546 | spi = spi_dev_get(spidev->spi); | |
547 | spin_unlock_irq(&spidev->spi_lock); | |
548 | ||
549 | if (spi == NULL) | |
550 | return -ESHUTDOWN; | |
551 | ||
552 | /* SPI_IOC_MESSAGE needs the buffer locked "normally" */ | |
553 | mutex_lock(&spidev->buf_lock); | |
554 | ||
555 | /* Check message and copy into scratch area */ | |
556 | ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc); | |
557 | if (IS_ERR(ioc)) { | |
558 | retval = PTR_ERR(ioc); | |
559 | goto done; | |
560 | } | |
561 | if (!ioc) | |
562 | goto done; /* n_ioc is also 0 */ | |
563 | ||
564 | /* Convert buffer pointers */ | |
565 | for (n = 0; n < n_ioc; n++) { | |
566 | ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf); | |
567 | ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf); | |
568 | } | |
569 | ||
570 | /* translate to spi_message, execute */ | |
571 | retval = spidev_message(spidev, ioc, n_ioc); | |
572 | kfree(ioc); | |
573 | ||
574 | done: | |
575 | mutex_unlock(&spidev->buf_lock); | |
576 | spi_dev_put(spi); | |
577 | return retval; | |
578 | } | |
579 | ||
7d48ec36 BW |
580 | static long |
581 | spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) | |
582 | { | |
7782a1a9 IA |
583 | if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC |
584 | && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0)) | |
585 | && _IOC_DIR(cmd) == _IOC_WRITE) | |
586 | return spidev_compat_ioc_message(filp, cmd, arg); | |
587 | ||
7d48ec36 BW |
588 | return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
589 | } | |
590 | #else | |
591 | #define spidev_compat_ioctl NULL | |
592 | #endif /* CONFIG_COMPAT */ | |
593 | ||
814a8d50 AP |
594 | static int spidev_open(struct inode *inode, struct file *filp) |
595 | { | |
596 | struct spidev_data *spidev; | |
597 | int status = -ENXIO; | |
598 | ||
599 | mutex_lock(&device_list_lock); | |
600 | ||
601 | list_for_each_entry(spidev, &device_list, device_entry) { | |
b2c8dadd | 602 | if (spidev->devt == inode->i_rdev) { |
814a8d50 AP |
603 | status = 0; |
604 | break; | |
605 | } | |
606 | } | |
865f6d19 RJ |
607 | |
608 | if (status) { | |
609 | pr_debug("spidev: nothing for minor %d\n", iminor(inode)); | |
610 | goto err_find_dev; | |
611 | } | |
612 | ||
613 | if (!spidev->tx_buffer) { | |
614 | spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL); | |
615 | if (!spidev->tx_buffer) { | |
814a8d50 AP |
616 | dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); |
617 | status = -ENOMEM; | |
865f6d19 | 618 | goto err_find_dev; |
814a8d50 AP |
619 | } |
620 | } | |
865f6d19 RJ |
621 | |
622 | if (!spidev->rx_buffer) { | |
623 | spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL); | |
624 | if (!spidev->rx_buffer) { | |
625 | dev_dbg(&spidev->spi->dev, "open/ENOMEM\n"); | |
626 | status = -ENOMEM; | |
627 | goto err_alloc_rx_buf; | |
814a8d50 | 628 | } |
865f6d19 RJ |
629 | } |
630 | ||
631 | spidev->users++; | |
632 | filp->private_data = spidev; | |
633 | nonseekable_open(inode, filp); | |
814a8d50 | 634 | |
865f6d19 RJ |
635 | mutex_unlock(&device_list_lock); |
636 | return 0; | |
637 | ||
638 | err_alloc_rx_buf: | |
639 | kfree(spidev->tx_buffer); | |
640 | spidev->tx_buffer = NULL; | |
641 | err_find_dev: | |
814a8d50 AP |
642 | mutex_unlock(&device_list_lock); |
643 | return status; | |
644 | } | |
645 | ||
646 | static int spidev_release(struct inode *inode, struct file *filp) | |
647 | { | |
648 | struct spidev_data *spidev; | |
649 | int status = 0; | |
650 | ||
651 | mutex_lock(&device_list_lock); | |
652 | spidev = filp->private_data; | |
653 | filp->private_data = NULL; | |
b2c8dadd DB |
654 | |
655 | /* last close? */ | |
814a8d50 AP |
656 | spidev->users--; |
657 | if (!spidev->users) { | |
b2c8dadd DB |
658 | int dofree; |
659 | ||
865f6d19 RJ |
660 | kfree(spidev->tx_buffer); |
661 | spidev->tx_buffer = NULL; | |
662 | ||
663 | kfree(spidev->rx_buffer); | |
664 | spidev->rx_buffer = NULL; | |
b2c8dadd | 665 | |
91690516 MB |
666 | spidev->speed_hz = spidev->spi->max_speed_hz; |
667 | ||
b2c8dadd DB |
668 | /* ... after we unbound from the underlying device? */ |
669 | spin_lock_irq(&spidev->spi_lock); | |
670 | dofree = (spidev->spi == NULL); | |
671 | spin_unlock_irq(&spidev->spi_lock); | |
672 | ||
673 | if (dofree) | |
674 | kfree(spidev); | |
814a8d50 AP |
675 | } |
676 | mutex_unlock(&device_list_lock); | |
677 | ||
678 | return status; | |
679 | } | |
680 | ||
828c0950 | 681 | static const struct file_operations spidev_fops = { |
814a8d50 AP |
682 | .owner = THIS_MODULE, |
683 | /* REVISIT switch to aio primitives, so that userspace | |
684 | * gets more complete API coverage. It'll simplify things | |
685 | * too, except for the locking. | |
686 | */ | |
687 | .write = spidev_write, | |
688 | .read = spidev_read, | |
4ef754b7 | 689 | .unlocked_ioctl = spidev_ioctl, |
7d48ec36 | 690 | .compat_ioctl = spidev_compat_ioctl, |
814a8d50 AP |
691 | .open = spidev_open, |
692 | .release = spidev_release, | |
6038f373 | 693 | .llseek = no_llseek, |
814a8d50 AP |
694 | }; |
695 | ||
696 | /*-------------------------------------------------------------------------*/ | |
697 | ||
698 | /* The main reason to have this class is to make mdev/udev create the | |
699 | * /dev/spidevB.C character device nodes exposing our userspace API. | |
700 | * It also simplifies memory management. | |
701 | */ | |
702 | ||
b2c8dadd | 703 | static struct class *spidev_class; |
814a8d50 AP |
704 | |
705 | /*-------------------------------------------------------------------------*/ | |
706 | ||
fd4a319b | 707 | static int spidev_probe(struct spi_device *spi) |
814a8d50 AP |
708 | { |
709 | struct spidev_data *spidev; | |
710 | int status; | |
711 | unsigned long minor; | |
712 | ||
713 | /* Allocate driver data */ | |
714 | spidev = kzalloc(sizeof(*spidev), GFP_KERNEL); | |
715 | if (!spidev) | |
716 | return -ENOMEM; | |
717 | ||
718 | /* Initialize the driver data */ | |
719 | spidev->spi = spi; | |
25d5cb4b | 720 | spin_lock_init(&spidev->spi_lock); |
814a8d50 AP |
721 | mutex_init(&spidev->buf_lock); |
722 | ||
723 | INIT_LIST_HEAD(&spidev->device_entry); | |
724 | ||
725 | /* If we can allocate a minor number, hook up this device. | |
726 | * Reusing minors is fine so long as udev or mdev is working. | |
727 | */ | |
728 | mutex_lock(&device_list_lock); | |
0a4dd778 | 729 | minor = find_first_zero_bit(minors, N_SPI_MINORS); |
814a8d50 | 730 | if (minor < N_SPI_MINORS) { |
b2c8dadd DB |
731 | struct device *dev; |
732 | ||
733 | spidev->devt = MKDEV(SPIDEV_MAJOR, minor); | |
a9b12619 GKH |
734 | dev = device_create(spidev_class, &spi->dev, spidev->devt, |
735 | spidev, "spidev%d.%d", | |
736 | spi->master->bus_num, spi->chip_select); | |
8c6ffba0 | 737 | status = PTR_ERR_OR_ZERO(dev); |
814a8d50 AP |
738 | } else { |
739 | dev_dbg(&spi->dev, "no minor number available!\n"); | |
740 | status = -ENODEV; | |
741 | } | |
742 | if (status == 0) { | |
743 | set_bit(minor, minors); | |
814a8d50 AP |
744 | list_add(&spidev->device_entry, &device_list); |
745 | } | |
746 | mutex_unlock(&device_list_lock); | |
747 | ||
91690516 MB |
748 | spidev->speed_hz = spi->max_speed_hz; |
749 | ||
aaacf4bb WO |
750 | if (status == 0) |
751 | spi_set_drvdata(spi, spidev); | |
752 | else | |
814a8d50 AP |
753 | kfree(spidev); |
754 | ||
755 | return status; | |
756 | } | |
757 | ||
fd4a319b | 758 | static int spidev_remove(struct spi_device *spi) |
814a8d50 | 759 | { |
b2c8dadd | 760 | struct spidev_data *spidev = spi_get_drvdata(spi); |
814a8d50 | 761 | |
25d5cb4b DB |
762 | /* make sure ops on existing fds can abort cleanly */ |
763 | spin_lock_irq(&spidev->spi_lock); | |
764 | spidev->spi = NULL; | |
765 | spin_unlock_irq(&spidev->spi_lock); | |
814a8d50 | 766 | |
25d5cb4b DB |
767 | /* prevent new opens */ |
768 | mutex_lock(&device_list_lock); | |
814a8d50 | 769 | list_del(&spidev->device_entry); |
b2c8dadd DB |
770 | device_destroy(spidev_class, spidev->devt); |
771 | clear_bit(MINOR(spidev->devt), minors); | |
772 | if (spidev->users == 0) | |
773 | kfree(spidev); | |
814a8d50 AP |
774 | mutex_unlock(&device_list_lock); |
775 | ||
776 | return 0; | |
777 | } | |
778 | ||
880cfd43 | 779 | static const struct of_device_id spidev_dt_ids[] = { |
8fad805b | 780 | { .compatible = "rohm,dh2228fv" }, |
880cfd43 MR |
781 | {}, |
782 | }; | |
783 | ||
784 | MODULE_DEVICE_TABLE(of, spidev_dt_ids); | |
785 | ||
db389b61 | 786 | static struct spi_driver spidev_spi_driver = { |
814a8d50 AP |
787 | .driver = { |
788 | .name = "spidev", | |
789 | .owner = THIS_MODULE, | |
880cfd43 | 790 | .of_match_table = of_match_ptr(spidev_dt_ids), |
814a8d50 AP |
791 | }, |
792 | .probe = spidev_probe, | |
fd4a319b | 793 | .remove = spidev_remove, |
814a8d50 AP |
794 | |
795 | /* NOTE: suspend/resume methods are not necessary here. | |
796 | * We don't do anything except pass the requests to/from | |
797 | * the underlying controller. The refrigerator handles | |
798 | * most issues; the controller driver handles the rest. | |
799 | */ | |
800 | }; | |
801 | ||
802 | /*-------------------------------------------------------------------------*/ | |
803 | ||
804 | static int __init spidev_init(void) | |
805 | { | |
806 | int status; | |
807 | ||
808 | /* Claim our 256 reserved device numbers. Then register a class | |
809 | * that will key udev/mdev to add/remove /dev nodes. Last, register | |
810 | * the driver which manages those device numbers. | |
811 | */ | |
812 | BUILD_BUG_ON(N_SPI_MINORS > 256); | |
813 | status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops); | |
814 | if (status < 0) | |
815 | return status; | |
816 | ||
b2c8dadd DB |
817 | spidev_class = class_create(THIS_MODULE, "spidev"); |
818 | if (IS_ERR(spidev_class)) { | |
db389b61 | 819 | unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); |
b2c8dadd | 820 | return PTR_ERR(spidev_class); |
814a8d50 AP |
821 | } |
822 | ||
db389b61 | 823 | status = spi_register_driver(&spidev_spi_driver); |
814a8d50 | 824 | if (status < 0) { |
b2c8dadd | 825 | class_destroy(spidev_class); |
db389b61 | 826 | unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); |
814a8d50 AP |
827 | } |
828 | return status; | |
829 | } | |
830 | module_init(spidev_init); | |
831 | ||
832 | static void __exit spidev_exit(void) | |
833 | { | |
db389b61 | 834 | spi_unregister_driver(&spidev_spi_driver); |
b2c8dadd | 835 | class_destroy(spidev_class); |
db389b61 | 836 | unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name); |
814a8d50 AP |
837 | } |
838 | module_exit(spidev_exit); | |
839 | ||
840 | MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>"); | |
841 | MODULE_DESCRIPTION("User mode SPI device interface"); | |
842 | MODULE_LICENSE("GPL"); | |
e0626e38 | 843 | MODULE_ALIAS("spi:spidev"); |