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