blob: 2a7cdbc9dcab54153de589d7fcc336f988c38fe7 [file] [log] [blame]
xf.li2f424182024-08-20 00:47:34 -07001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Simple synchronous userspace interface to SPI devices
4 *
5 * Copyright (C) 2006 SWAPP
6 * Andrea Paterniani <a.paterniani@swapp-eng.it>
7 * Copyright (C) 2007 David Brownell (simplification, cleanup)
8 */
9
10#include <linux/init.h>
11#include <linux/module.h>
12#include <linux/ioctl.h>
13#include <linux/fs.h>
14#include <linux/device.h>
15#include <linux/err.h>
16#include <linux/list.h>
17#include <linux/errno.h>
18#include <linux/mutex.h>
19#include <linux/slab.h>
20#include <linux/compat.h>
21#include <linux/of.h>
22#include <linux/of_device.h>
23#include <linux/acpi.h>
24
25#include <linux/spi/spi.h>
26#include <linux/spi/spidev.h>
27
28#include <linux/uaccess.h>
29#include <linux/of_gpio.h>
30#include <linux/pinctrl/consumer.h>
31#include <linux/of_irq.h>
32#include <linux/interrupt.h>
33#include <linux/delay.h>
34/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start*/
35#include <linux/wait.h>
36#include <linux/suspend.h>
37
38#define SPI_SLAVE_FOR_YK
39/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
40/*
41 * This supports access to SPI devices using normal userspace I/O calls.
42 * Note that while traditional UNIX/POSIX I/O semantics are half duplex,
43 * and often mask message boundaries, full SPI support requires full duplex
44 * transfers. There are several kinds of internal message boundaries to
45 * handle chipselect management and other protocol options.
46 *
47 * SPI has a character major number assigned. We allocate minor numbers
48 * dynamically using a bitmask. You must use hotplug tools, such as udev
49 * (or mdev with busybox) to create and destroy the /dev/spidevB.C device
50 * nodes, since there is no fixed association of minor numbers with any
51 * particular SPI bus or device.
52 */
53#define SPIDEV_MAJOR 153 /* assigned */
54#define N_SPI_MINORS 32 /* ... up to 256 */
55
56static DECLARE_BITMAP(minors, N_SPI_MINORS);
57
58
59/* Bit masks for spi_device.mode management. Note that incorrect
60 * settings for some settings can cause *lots* of trouble for other
61 * devices on a shared bus:
62 *
63 * - CS_HIGH ... this device will be active when it shouldn't be
64 * - 3WIRE ... when active, it won't behave as it should
65 * - NO_CS ... there will be no explicit message boundaries; this
66 * is completely incompatible with the shared bus model
67 * - READY ... transfers may proceed when they shouldn't.
68 *
69 * REVISIT should changing those flags be privileged?
70 */
71#define SPI_MODE_MASK (SPI_CPHA | SPI_CPOL | SPI_CS_HIGH \
72 | SPI_LSB_FIRST | SPI_3WIRE | SPI_LOOP \
73 | SPI_NO_CS | SPI_READY | SPI_TX_DUAL \
74 | SPI_TX_QUAD | SPI_TX_OCTAL | SPI_RX_DUAL \
75 | SPI_RX_QUAD | SPI_RX_OCTAL)
76
77struct spidev_data {
78 dev_t devt;
79 spinlock_t spi_lock;
80 struct spi_device *spi;
81 struct list_head device_entry;
82
83 /* TX/RX buffers are NULL unless this device is open (users > 0) */
84 struct mutex buf_lock;
85 unsigned users;
86 u8 *tx_buffer;
87 u8 *rx_buffer;
88 u32 speed_hz;
89 u8 rd_from_rx_buffer;
90
91//#define SPIDEV_DEBUG
92#ifdef SPIDEV_DEBUG
93 struct pinctrl *pctrl;
94 struct pinctrl_state *pgpioex;
95 struct pinctrl_state *pint_ex;
96 int gpio_ex;
97 int gpio_int;
98 int irq;
99 int tx_flag;
100 int rx_cnt_in_rx_thread;
101 int rx_cnt_in_tx_thread;
102 struct semaphore wait_req;
103 struct semaphore rec_req;
104 struct semaphore rec_head_msg_req;
105 struct semaphore rec_data_msg_req;
106 spinlock_t tx_flag_lock;
107 int msg_id;
108 bool is_data_check;
109 int rx_data_check_ok_cnt;
110 int rx_data_check_err_cnt;
111#endif
112//#define TEST_SWAP_KERNEL_AND_USER
113#ifdef TEST_SWAP_KERNEL_AND_USER
114 struct pinctrl *pctrl;
115 struct pinctrl_state *pgpioex;
116 struct pinctrl_state *pint_ex;
117 struct semaphore sig_req;
118 struct semaphore sem_dma_cfg_done;
119 int gpio_ex;
120 int gpio_int;
121 int irq;
122 int pid;
123 int dma_cfg_done;
124#endif
125};
126
127static LIST_HEAD(device_list);
128static DEFINE_MUTEX(device_list_lock);
129
130static unsigned bufsiz = 4096;
131module_param(bufsiz, uint, S_IRUGO);
132MODULE_PARM_DESC(bufsiz, "data bytes in biggest supported SPI message");
133
134/*-------------------------------------------------------------------------*/
135
136static ssize_t
137spidev_sync(struct spidev_data *spidev, struct spi_message *message)
138{
139 int status;
140 struct spi_device *spi;
141
142 spin_lock_irq(&spidev->spi_lock);
143 spi = spidev->spi;
144 spin_unlock_irq(&spidev->spi_lock);
145
146 if (spi == NULL)
147 status = -ESHUTDOWN;
148 else
149 status = spi_sync(spi, message);
150
151 if (status == 0)
152 status = message->actual_length;
153
154 return status;
155}
156
157static inline ssize_t
158spidev_sync_write(struct spidev_data *spidev, size_t len)
159{
160 struct spi_transfer t = {
161 .tx_buf = spidev->tx_buffer,
162 .len = len,
163 .speed_hz = spidev->speed_hz,
164 };
165 struct spi_message m;
166
167 spi_message_init(&m);
168 spi_message_add_tail(&t, &m);
169 return spidev_sync(spidev, &m);
170}
171
172static inline ssize_t
173spidev_sync_read(struct spidev_data *spidev, size_t len)
174{
175 struct spi_transfer t = {
176 .rx_buf = spidev->rx_buffer,
177 .len = len,
178 .speed_hz = spidev->speed_hz,
179 };
180 struct spi_message m;
181
182 spi_message_init(&m);
183 spi_message_add_tail(&t, &m);
184 return spidev_sync(spidev, &m);
185}
186
187
188static inline ssize_t
189spidev_sync_write_and_read(struct spidev_data *spidev, size_t len)
190{
191 struct spi_transfer t = {
192 .tx_buf = spidev->tx_buffer,
193 .rx_buf = spidev->rx_buffer,
194 .len = len,
195 .speed_hz = spidev->speed_hz,
196 };
197 struct spi_message m;
198
199 spi_message_init(&m);
200 spi_message_add_tail(&t, &m);
201 return spidev_sync(spidev, &m);
202}
203
204
205
206/*-------------------------------------------------------------------------*/
207/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start*/
208/* Read-only message with current device setup */
209static ssize_t
210spidev_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
211{
212 struct spidev_data *spidev;
213 ssize_t status;
214 unsigned long missing;
215 /* chipselect only toggles at start or end of operation */
216 if (count > bufsiz)
217 return -EMSGSIZE;
218
219 spidev = filp->private_data;
xf.li1867bfa2024-08-20 02:32:16 -0700220 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch start */
221 if(spidev->spi->master->slave) {
222 #ifdef SPI_SLAVE_FOR_YK
223 size_t total = 0;
xf.li2f424182024-08-20 00:47:34 -0700224
xf.li1867bfa2024-08-20 02:32:16 -0700225 if (spidev->spi->rd_pos == spidev->spi->recv_pos) {
xf.li2f424182024-08-20 00:47:34 -0700226
xf.li1867bfa2024-08-20 02:32:16 -0700227 status = 0;
228 spidev->spi->is_rd_waiting = true;
229 if(0 != wait_event_freezable(spidev->spi->rd_wait, spidev->spi->recv_done)) {
230 if(spidev->spi->controller->spi_slave_rd_stop)
231 spidev->spi->controller->spi_slave_rd_stop(spidev->spi);
232 spidev->spi->is_rd_waiting = false;
233 return status;
234 }else {
235 spidev->spi->recv_done = false;
236 spidev->spi->is_rd_waiting = false;
237 }
xf.li2f424182024-08-20 00:47:34 -0700238 }
xf.li1867bfa2024-08-20 02:32:16 -0700239 mutex_lock(&spidev->buf_lock);
240 if(spidev->spi->rd_pos < spidev->spi->recv_pos) {
xf.li2f424182024-08-20 00:47:34 -0700241
xf.li1867bfa2024-08-20 02:32:16 -0700242 total = spidev->spi->recv_pos - spidev->spi->rd_pos;
243 status = (total > count) ? count : total;
xf.li2f424182024-08-20 00:47:34 -0700244 missing = copy_to_user(buf, spidev->rx_buffer+spidev->spi->rd_pos, status);
245 if (missing == status) {
246 status = -EFAULT;
247 }
248 else {
249 status = status - missing;
250 spidev->spi->rd_pos += status;
xf.li2f424182024-08-20 00:47:34 -0700251 }
252
xf.li1867bfa2024-08-20 02:32:16 -0700253 }else if(spidev->spi->rd_pos > spidev->spi->recv_pos) {
254
255 total = bufsiz - (spidev->spi->rd_pos - spidev->spi->recv_pos);
256 status = (total > count) ? count : total;
257
258 if((spidev->spi->rd_pos + status) <= bufsiz) {
259
260 missing = copy_to_user(buf, spidev->rx_buffer+spidev->spi->rd_pos, status);
261 if (missing == status) {
262 status = -EFAULT;
263 }
264 else {
265 status = status - missing;
266 spidev->spi->rd_pos += status;
267 spidev->spi->rd_pos = spidev->spi->rd_pos%bufsiz;
268 }
269 }else {
270
271 unsigned long first,rest;
272
273 first = bufsiz - spidev->spi->rd_pos;
274 missing = copy_to_user(buf, spidev->rx_buffer+spidev->spi->rd_pos, first);
275 if (missing == first) {
276 status = -EFAULT;
277 } else {
278 status = status - missing;
279 }
280
281 rest = status-first;
282 missing = copy_to_user(buf+first, spidev->rx_buffer, rest);
283 if (missing == rest) {
284 status = -EFAULT;
285 } else {
286 status = status - missing;
287 }
288 spidev->spi->rd_pos = rest;
xf.li2f424182024-08-20 00:47:34 -0700289 }
xf.li2f424182024-08-20 00:47:34 -0700290 }
xf.li1867bfa2024-08-20 02:32:16 -0700291 #else
292 mutex_lock(&spidev->buf_lock);
293 if(spidev->rd_from_rx_buffer)
294 status = count;
xf.li2f424182024-08-20 00:47:34 -0700295 else
xf.li1867bfa2024-08-20 02:32:16 -0700296 status = spidev_sync_read(spidev, count);
xf.li2f424182024-08-20 00:47:34 -0700297
xf.li1867bfa2024-08-20 02:32:16 -0700298 if (status > 0) {
299
300 missing = copy_to_user(buf, spidev->rx_buffer, status);
301 if (missing == status)
302 status = -EFAULT;
303 else
304 status = status - missing;
305 }
306 #endif
307 mutex_unlock(&spidev->buf_lock);
308 }else {
309 mutex_lock(&spidev->buf_lock);
310
311 if(spidev->rd_from_rx_buffer)
312 status = count;
313 else
314 status = spidev_sync_read(spidev, count);
315
316 if(status > 0) {
317 missing = copy_to_user(buf, spidev->rx_buffer, status);
318 if (missing == status)
319 status = -EFAULT;
320 else
321 status = status - missing;
322 }
323 mutex_unlock(&spidev->buf_lock);
324 }
325 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch end */
xf.li2f424182024-08-20 00:47:34 -0700326 return status;
327}
328/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end*/
329
330/* Write-only message with current device setup */
331static ssize_t
332spidev_write(struct file *filp, const char __user *buf,
333 size_t count, loff_t *f_pos)
334{
335 struct spidev_data *spidev;
336 ssize_t status;
337 unsigned long missing;
338
339 /* chipselect only toggles at start or end of operation */
340 if (count > bufsiz)
341 return -EMSGSIZE;
342
343 spidev = filp->private_data;
344
345 mutex_lock(&spidev->buf_lock);
346 missing = copy_from_user(spidev->tx_buffer, buf, count);
347 if (missing == 0) {
348 if(spidev->rd_from_rx_buffer)
349 status = spidev_sync_write_and_read(spidev, count);
350 else
351 status = spidev_sync_write(spidev, count);
352 }else {
353 status = -EFAULT;
354 }
355 mutex_unlock(&spidev->buf_lock);
356
357 return status;
358}
359
360static int spidev_message(struct spidev_data *spidev,
361 struct spi_ioc_transfer *u_xfers, unsigned n_xfers)
362{
363 struct spi_message msg;
364 struct spi_transfer *k_xfers;
365 struct spi_transfer *k_tmp;
366 struct spi_ioc_transfer *u_tmp;
367 unsigned n, total, tx_total, rx_total;
368 u8 *tx_buf, *rx_buf;
369 int status = -EFAULT;
370
371 spi_message_init(&msg);
372 k_xfers = kcalloc(n_xfers, sizeof(*k_tmp), GFP_KERNEL);
373 if (k_xfers == NULL)
374 return -ENOMEM;
375
376 /* Construct spi_message, copying any tx data to bounce buffer.
377 * We walk the array of user-provided transfers, using each one
378 * to initialize a kernel version of the same transfer.
379 */
380 tx_buf = spidev->tx_buffer;
381 rx_buf = spidev->rx_buffer;
382 total = 0;
383 tx_total = 0;
384 rx_total = 0;
385 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
386 n;
387 n--, k_tmp++, u_tmp++) {
388 /* Ensure that also following allocations from rx_buf/tx_buf will meet
389 * DMA alignment requirements.
390 */
391 unsigned int len_aligned = ALIGN(u_tmp->len, ARCH_KMALLOC_MINALIGN);
392
393 k_tmp->len = u_tmp->len;
394
395 total += k_tmp->len;
396 /* Since the function returns the total length of transfers
397 * on success, restrict the total to positive int values to
398 * avoid the return value looking like an error. Also check
399 * each transfer length to avoid arithmetic overflow.
400 */
401 if (total > INT_MAX || k_tmp->len > INT_MAX) {
402 status = -EMSGSIZE;
403 goto done;
404 }
405
406 if (u_tmp->rx_buf) {
407 /* this transfer needs space in RX bounce buffer */
408 rx_total += len_aligned;
409 if (rx_total > bufsiz) {
410 status = -EMSGSIZE;
411 goto done;
412 }
413 k_tmp->rx_buf = rx_buf;
414 rx_buf += len_aligned;
415 }
416 if (u_tmp->tx_buf) {
417 /* this transfer needs space in TX bounce buffer */
418 tx_total += len_aligned;
419 if (tx_total > bufsiz) {
420 status = -EMSGSIZE;
421 goto done;
422 }
423 k_tmp->tx_buf = tx_buf;
424 if (copy_from_user(tx_buf, (const u8 __user *)
425 (uintptr_t) u_tmp->tx_buf,
426 u_tmp->len))
427 goto done;
428 tx_buf += len_aligned;
429 }
430
431 k_tmp->cs_change = !!u_tmp->cs_change;
432 k_tmp->tx_nbits = u_tmp->tx_nbits;
433 k_tmp->rx_nbits = u_tmp->rx_nbits;
434 k_tmp->bits_per_word = u_tmp->bits_per_word;
435 k_tmp->delay.value = u_tmp->delay_usecs;
436 k_tmp->delay.unit = SPI_DELAY_UNIT_USECS;
437 k_tmp->speed_hz = u_tmp->speed_hz;
438 k_tmp->word_delay.value = u_tmp->word_delay_usecs;
439 k_tmp->word_delay.unit = SPI_DELAY_UNIT_USECS;
440 if (!k_tmp->speed_hz)
441 k_tmp->speed_hz = spidev->speed_hz;
442#ifdef VERBOSE
443 dev_dbg(&spidev->spi->dev,
444 " xfer len %u %s%s%s%dbits %u usec %u usec %uHz\n",
445 k_tmp->len,
446 k_tmp->rx_buf ? "rx " : "",
447 k_tmp->tx_buf ? "tx " : "",
448 k_tmp->cs_change ? "cs " : "",
449 k_tmp->bits_per_word ? : spidev->spi->bits_per_word,
450 k_tmp->delay.value,
451 k_tmp->word_delay.value,
452 k_tmp->speed_hz ? : spidev->spi->max_speed_hz);
453#endif
454 spi_message_add_tail(k_tmp, &msg);
455 }
456
457 status = spidev_sync(spidev, &msg);
458 if (status < 0)
459 goto done;
460
461 /* copy any rx data out of bounce buffer */
462 for (n = n_xfers, k_tmp = k_xfers, u_tmp = u_xfers;
463 n;
464 n--, k_tmp++, u_tmp++) {
465 if (u_tmp->rx_buf) {
466 if (copy_to_user((u8 __user *)
467 (uintptr_t) u_tmp->rx_buf, k_tmp->rx_buf,
468 u_tmp->len)) {
469 status = -EFAULT;
470 goto done;
471 }
472 }
473 }
474 status = total;
475
476done:
477 kfree(k_xfers);
478 return status;
479}
480
481static struct spi_ioc_transfer *
482spidev_get_ioc_message(unsigned int cmd, struct spi_ioc_transfer __user *u_ioc,
483 unsigned *n_ioc)
484{
485 u32 tmp;
486
487 /* Check type, command number and direction */
488 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC
489 || _IOC_NR(cmd) != _IOC_NR(SPI_IOC_MESSAGE(0))
490 || _IOC_DIR(cmd) != _IOC_WRITE)
491 return ERR_PTR(-ENOTTY);
492
493 tmp = _IOC_SIZE(cmd);
494 if ((tmp % sizeof(struct spi_ioc_transfer)) != 0)
495 return ERR_PTR(-EINVAL);
496 *n_ioc = tmp / sizeof(struct spi_ioc_transfer);
497 if (*n_ioc == 0)
498 return NULL;
499
500 /* copy into scratch area */
501 return memdup_user(u_ioc, tmp);
502}
503
504static long
505spidev_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
506{
507 int retval = 0;
508 struct spidev_data *spidev;
509 struct spi_device *spi;
510 u32 tmp;
511 unsigned n_ioc;
512 struct spi_ioc_transfer *ioc;
513
514 /* Check type and command number */
515 if (_IOC_TYPE(cmd) != SPI_IOC_MAGIC)
516 return -ENOTTY;
517
518 /* guard against device removal before, or while,
519 * we issue this ioctl.
520 */
521 spidev = filp->private_data;
522 spin_lock_irq(&spidev->spi_lock);
523 spi = spi_dev_get(spidev->spi);
524 spin_unlock_irq(&spidev->spi_lock);
525
526 if (spi == NULL)
527 return -ESHUTDOWN;
528
529 /* use the buffer lock here for triple duty:
530 * - prevent I/O (from us) so calling spi_setup() is safe;
531 * - prevent concurrent SPI_IOC_WR_* from morphing
532 * data fields while SPI_IOC_RD_* reads them;
533 * - SPI_IOC_MESSAGE needs the buffer locked "normally".
534 */
535 mutex_lock(&spidev->buf_lock);
536
537 switch (cmd) {
538 /* read requests */
539 case SPI_IOC_RD_MODE:
540 retval = put_user(spi->mode & SPI_MODE_MASK,
541 (__u8 __user *)arg);
542 break;
543 case SPI_IOC_RD_MODE32:
544 retval = put_user(spi->mode & SPI_MODE_MASK,
545 (__u32 __user *)arg);
546 break;
547 case SPI_IOC_RD_LSB_FIRST:
548 retval = put_user((spi->mode & SPI_LSB_FIRST) ? 1 : 0,
549 (__u8 __user *)arg);
550 break;
551 case SPI_IOC_RD_BITS_PER_WORD:
552 retval = put_user(spi->bits_per_word, (__u8 __user *)arg);
553 break;
554 case SPI_IOC_RD_MAX_SPEED_HZ:
555 retval = put_user(spidev->speed_hz, (__u32 __user *)arg);
556 break;
557 case SPI_IOC_RD_RD_DATA_FROM:
558 retval = put_user(spidev->rd_from_rx_buffer, (__u32 __user *)arg);
559 break;
560
561#ifdef TEST_SWAP_KERNEL_AND_USER
562 case SPI_IOC_RD_INT_ST:
563 tmp = gpio_get_value(spidev->gpio_int);
564 retval = put_user(tmp, (__u32 __user *)arg);
565 break;
566#endif
567 /* write requests */
568 case SPI_IOC_WR_MODE:
569 case SPI_IOC_WR_MODE32:
570 if (cmd == SPI_IOC_WR_MODE)
571 retval = get_user(tmp, (u8 __user *)arg);
572 else
573 retval = get_user(tmp, (u32 __user *)arg);
574 if (retval == 0) {
575 struct spi_controller *ctlr = spi->controller;
576 u32 save = spi->mode;
577
578 if (tmp & ~SPI_MODE_MASK) {
579 retval = -EINVAL;
580 break;
581 }
582
583 if (ctlr->use_gpio_descriptors && ctlr->cs_gpiods &&
584 ctlr->cs_gpiods[spi->chip_select])
585 tmp |= SPI_CS_HIGH;
586
587 tmp |= spi->mode & ~SPI_MODE_MASK;
588 spi->mode = (u16)tmp;
589 retval = spi_setup(spi);
590 if (retval < 0)
591 spi->mode = save;
592 else
593 dev_dbg(&spi->dev, "spi mode %x\n", tmp);
594 }
595 break;
596 case SPI_IOC_WR_LSB_FIRST:
597 retval = get_user(tmp, (__u8 __user *)arg);
598 if (retval == 0) {
599 u32 save = spi->mode;
600
601 if (tmp)
602 spi->mode |= SPI_LSB_FIRST;
603 else
604 spi->mode &= ~SPI_LSB_FIRST;
605 retval = spi_setup(spi);
606 if (retval < 0)
607 spi->mode = save;
608 else
609 dev_dbg(&spi->dev, "%csb first\n",
610 tmp ? 'l' : 'm');
611 }
612 break;
613 case SPI_IOC_WR_BITS_PER_WORD:
614 retval = get_user(tmp, (__u8 __user *)arg);
615 if (retval == 0) {
616 u8 save = spi->bits_per_word;
617
618 spi->bits_per_word = tmp;
619 retval = spi_setup(spi);
620 if (retval < 0)
621 spi->bits_per_word = save;
622 else
623 dev_dbg(&spi->dev, "%d bits per word\n", tmp);
624 }
625 break;
626 case SPI_IOC_WR_MAX_SPEED_HZ:
627 retval = get_user(tmp, (__u32 __user *)arg);
628 if (retval == 0) {
629 u32 save = spi->max_speed_hz;
630
631 spi->max_speed_hz = tmp;
632 retval = spi_setup(spi);
633 if (retval == 0) {
634 spidev->speed_hz = tmp;
635 dev_dbg(&spi->dev, "%d Hz (max)\n",
636 spidev->speed_hz);
637 } else {
638 spi->max_speed_hz = save;
639 }
640 }
641 break;
642 case SPI_IOC_WR_RD_DATA_FROM:
643 retval = get_user(tmp, (__u8 __user *)arg);
644 if (retval == 0) {
645 spidev->rd_from_rx_buffer = tmp;
646 dev_dbg(&spi->dev, "RD DATA FROM %s \n",
647 spidev->rd_from_rx_buffer ? "RX_BUFFER":"DEVICE");
648 }
649 break;
650/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
651#ifdef SPI_SLAVE_FOR_YK
652 case SPI_IOC_RD_BLOCK_RELEASE:
653 if(spidev->spi->is_rd_waiting == true) {
654 wake_up(&spidev->spi->rd_wait);
655 spidev->spi->recv_done = 1;
656 }
657 break;
658#endif
659/* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
660#ifdef TEST_SWAP_KERNEL_AND_USER
661 case SPI_IOC_WR_SIG_PID:
662 retval = get_user(tmp, (__u32 __user *)arg);
663 if (retval == 0) {
664 spidev->pid = tmp;
665 dev_dbg(&spi->dev, "SET SIG PID %d \n",
666 spidev->pid);
667 }else{
668 printk("%s %d %d \r\n",__FUNCTION__,__LINE__,retval);
669 }
670
671 break;
672#endif
673 default:
674 /* segmented and/or full-duplex I/O request */
675 /* Check message and copy into scratch area */
676 ioc = spidev_get_ioc_message(cmd,
677 (struct spi_ioc_transfer __user *)arg, &n_ioc);
678 if (IS_ERR(ioc)) {
679 retval = PTR_ERR(ioc);
680 break;
681 }
682 if (!ioc)
683 break; /* n_ioc is also 0 */
684
685 /* translate to spi_message, execute */
686 retval = spidev_message(spidev, ioc, n_ioc);
687 kfree(ioc);
688 break;
689 }
690
691 mutex_unlock(&spidev->buf_lock);
692 spi_dev_put(spi);
693 return retval;
694}
695
696#ifdef CONFIG_COMPAT
697static long
698spidev_compat_ioc_message(struct file *filp, unsigned int cmd,
699 unsigned long arg)
700{
701 struct spi_ioc_transfer __user *u_ioc;
702 int retval = 0;
703 struct spidev_data *spidev;
704 struct spi_device *spi;
705 unsigned n_ioc, n;
706 struct spi_ioc_transfer *ioc;
707
708 u_ioc = (struct spi_ioc_transfer __user *) compat_ptr(arg);
709
710 /* guard against device removal before, or while,
711 * we issue this ioctl.
712 */
713 spidev = filp->private_data;
714 spin_lock_irq(&spidev->spi_lock);
715 spi = spi_dev_get(spidev->spi);
716 spin_unlock_irq(&spidev->spi_lock);
717
718 if (spi == NULL)
719 return -ESHUTDOWN;
720
721 /* SPI_IOC_MESSAGE needs the buffer locked "normally" */
722 mutex_lock(&spidev->buf_lock);
723
724 /* Check message and copy into scratch area */
725 ioc = spidev_get_ioc_message(cmd, u_ioc, &n_ioc);
726 if (IS_ERR(ioc)) {
727 retval = PTR_ERR(ioc);
728 goto done;
729 }
730 if (!ioc)
731 goto done; /* n_ioc is also 0 */
732
733 /* Convert buffer pointers */
734 for (n = 0; n < n_ioc; n++) {
735 ioc[n].rx_buf = (uintptr_t) compat_ptr(ioc[n].rx_buf);
736 ioc[n].tx_buf = (uintptr_t) compat_ptr(ioc[n].tx_buf);
737 }
738
739 /* translate to spi_message, execute */
740 retval = spidev_message(spidev, ioc, n_ioc);
741 kfree(ioc);
742
743done:
744 mutex_unlock(&spidev->buf_lock);
745 spi_dev_put(spi);
746 return retval;
747}
748
749static long
750spidev_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
751{
752 if (_IOC_TYPE(cmd) == SPI_IOC_MAGIC
753 && _IOC_NR(cmd) == _IOC_NR(SPI_IOC_MESSAGE(0))
754 && _IOC_DIR(cmd) == _IOC_WRITE)
755 return spidev_compat_ioc_message(filp, cmd, arg);
756
757 return spidev_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
758}
759#else
760#define spidev_compat_ioctl NULL
761#endif /* CONFIG_COMPAT */
762
763static int spidev_open(struct inode *inode, struct file *filp)
764{
765 struct spidev_data *spidev;
766 int status = -ENXIO;
767 struct spi_device *spi;
768
769 mutex_lock(&device_list_lock);
770
771 list_for_each_entry(spidev, &device_list, device_entry) {
772 if (spidev->devt == inode->i_rdev) {
773 status = 0;
774 break;
775 }
776 }
777
778 if (status) {
779 pr_debug("spidev: nothing for minor %d\n", iminor(inode));
780 goto err_find_dev;
781 }
782
783 if (!spidev->tx_buffer) {
784 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
785 if (!spidev->tx_buffer) {
786 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
787 status = -ENOMEM;
788 goto err_find_dev;
789 }
790 }
791
792 if (!spidev->rx_buffer) {
793 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
794 if (!spidev->rx_buffer) {
795 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
796 status = -ENOMEM;
797 goto err_alloc_rx_buf;
798 }
799 }
800
xf.li1867bfa2024-08-20 02:32:16 -0700801 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch start */
802 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
803 if(spidev->spi->master->slave) {
804 #ifdef SPI_SLAVE_FOR_YK
805 if(spidev->rx_buffer) {
806 spidev->spi->rx_buf = spidev->rx_buffer;
807 if(spidev->spi->controller->spi_slave_rd_start)
808 spidev->spi->controller->spi_slave_rd_start(spidev->spi);
809 }
810 #endif
811 }
812 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
813 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch end */
xf.li2f424182024-08-20 00:47:34 -0700814 spidev->users++;
815 filp->private_data = spidev;
816 stream_open(inode, filp);
817
818 mutex_unlock(&device_list_lock);
819
820
821 spin_lock_irq(&spidev->spi_lock);
822 spi = spi_dev_get(spidev->spi);
823 spin_unlock_irq(&spidev->spi_lock);
824 if(spi && spi->master->slave)
825 pm_stay_awake(&spi->dev);
826
827 return 0;
828
829err_alloc_rx_buf:
830 kfree(spidev->tx_buffer);
831 spidev->tx_buffer = NULL;
832err_find_dev:
833 mutex_unlock(&device_list_lock);
834 return status;
835}
836
837static int spidev_release(struct inode *inode, struct file *filp)
838{
839 struct spidev_data *spidev;
840 int dofree;
841 struct spi_device *spi;
842
843 mutex_lock(&device_list_lock);
844 spidev = filp->private_data;
845 filp->private_data = NULL;
846
847 spin_lock_irq(&spidev->spi_lock);
848 /* ... after we unbound from the underlying device? */
849 dofree = (spidev->spi == NULL);
850 spin_unlock_irq(&spidev->spi_lock);
851
852 /* last close? */
853 spidev->users--;
854 if (!spidev->users) {
855
856 spin_lock_irq(&spidev->spi_lock);
857 spi = spi_dev_get(spidev->spi);
858 spin_unlock_irq(&spidev->spi_lock);
859
xf.li1867bfa2024-08-20 02:32:16 -0700860 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch start */
861 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme start */
862 if(spidev->spi->master->slave) {
863 #ifdef SPI_SLAVE_FOR_YK
864 if(spidev->rx_buffer) {
865 if(spi->controller->spi_slave_rd_stop)
866 spi->controller->spi_slave_rd_stop(spi);
867 }
868 #endif
869 }
870 /* yu.dong@20240617 [T106BUG-641] SPI packet loss problem, add kernel buffer scheme end */
871 /* yu.dong@20240715 [T106BUG-641] SPI packet loss problem, merged into ZXW patch end*/
xf.li2f424182024-08-20 00:47:34 -0700872 if(spi && spi->master->slave)
873 pm_relax(&spi->dev);
874 kfree(spidev->tx_buffer);
875 spidev->tx_buffer = NULL;
876
877 kfree(spidev->rx_buffer);
878 spidev->rx_buffer = NULL;
879
880 if (dofree)
881 kfree(spidev);
882 else
883 spidev->speed_hz = spidev->spi->max_speed_hz;
884 }
885#ifdef CONFIG_SPI_SLAVE
886 if (!dofree)
887 spi_slave_abort(spidev->spi);
888#endif
889 mutex_unlock(&device_list_lock);
890
891 return 0;
892}
893
894static const struct file_operations spidev_fops = {
895 .owner = THIS_MODULE,
896 /* REVISIT switch to aio primitives, so that userspace
897 * gets more complete API coverage. It'll simplify things
898 * too, except for the locking.
899 */
900 .write = spidev_write,
901 .read = spidev_read,
902 .unlocked_ioctl = spidev_ioctl,
903 .compat_ioctl = spidev_compat_ioctl,
904 .open = spidev_open,
905 .release = spidev_release,
906 .llseek = no_llseek,
907};
908
909/*-------------------------------------------------------------------------*/
910
911/* The main reason to have this class is to make mdev/udev create the
912 * /dev/spidevB.C character device nodes exposing our userspace API.
913 * It also simplifies memory management.
914 */
915
916static struct class *spidev_class;
917
918#ifdef CONFIG_OF
919static const struct of_device_id spidev_dt_ids[] = {
920 { .compatible = "rohm,dh2228fv" },
921 { .compatible = "lineartechnology,ltc2488" },
922 { .compatible = "ge,achc" },
923 { .compatible = "semtech,sx1301" },
924 { .compatible = "lwn,bk4" },
925 { .compatible = "dh,dhcom-board" },
926 { .compatible = "menlo,m53cpld" },
927 { .compatible = "zte,spidev" },
928 {},
929};
930MODULE_DEVICE_TABLE(of, spidev_dt_ids);
931#endif
932
933#ifdef CONFIG_ACPI
934
935/* Dummy SPI devices not to be used in production systems */
936#define SPIDEV_ACPI_DUMMY 1
937
938static const struct acpi_device_id spidev_acpi_ids[] = {
939 /*
940 * The ACPI SPT000* devices are only meant for development and
941 * testing. Systems used in production should have a proper ACPI
942 * description of the connected peripheral and they should also use
943 * a proper driver instead of poking directly to the SPI bus.
944 */
945 { "SPT0001", SPIDEV_ACPI_DUMMY },
946 { "SPT0002", SPIDEV_ACPI_DUMMY },
947 { "SPT0003", SPIDEV_ACPI_DUMMY },
948 {},
949};
950MODULE_DEVICE_TABLE(acpi, spidev_acpi_ids);
951
952static void spidev_probe_acpi(struct spi_device *spi)
953{
954 const struct acpi_device_id *id;
955
956 if (!has_acpi_companion(&spi->dev))
957 return;
958
959 id = acpi_match_device(spidev_acpi_ids, &spi->dev);
960 if (WARN_ON(!id))
961 return;
962
963 if (id->driver_data == SPIDEV_ACPI_DUMMY)
964 dev_warn(&spi->dev, "do not use this driver in production systems!\n");
965}
966#else
967static inline void spidev_probe_acpi(struct spi_device *spi) {}
968#endif
969
970#ifdef SPIDEV_DEBUG
971#define SPIDEV_ATTR(_name) \
972static struct kobj_attribute _name##_attr = { \
973 .attr = { \
974 .name = __stringify(_name), \
975 .mode = 0644, \
976 }, \
977 .show = _name##_show, \
978 .store = _name##_store, \
979}
980
981
982static void print_buf_data(void * buf,int count)
983{
984 int i = 0;
985 if(buf) {
986 unsigned char *p = buf;
987 for(i = 0;i<= count-8;i+=8) {
988 printk("%02x %02x %02x %02x %02x %02x %02x %02x \r\n",p[i],p[i+1],p[i+2],p[i+3],p[i+4],p[i+5],p[i+6],p[i+7]);
989 }
990 }
991}
992
993
994struct spi_dev_hand_msg{
995 unsigned short head;
996 unsigned int len;
997 unsigned short tail;
998};
999
1000#define MSG_HEAD 0xa5a5
1001#define MSG_TAIL 0x7e7e
1002
1003extern void slave_mode_set(struct spi_device *spi,unsigned int param);
1004extern void set_spi_timing(struct spi_device *spi,unsigned int param);
1005extern int get_spi_rx_fifo(struct spi_device *spi,unsigned char *buf);
1006static int spidev_get_rxfifo(struct spi_device *spi,unsigned char *buf)
1007{
1008 int ret = 0;
1009
1010 if(!spi || !buf)
1011 return ret;
1012 return get_spi_rx_fifo(spi,buf);
1013
1014}
1015
1016
1017static int data_to_packet(void * buf,int len)
1018{
1019 int i = 2,ret = -1;
1020 unsigned char sum = 0;
1021 unsigned char *p = (unsigned char *)buf;
1022
1023 if(!p || len < 4) {
1024 printk("%s param err! \n",__FUNCTION__);
1025 return ret;
1026 }
1027 for(i = 2;i<len-2;i++)
1028 sum += p[i];
1029 p[1] = sum;
1030 ret = 0;
1031 return ret;
1032}
1033
1034static int packet_check(void *buf,int len)
1035{
1036 unsigned char *p = (unsigned char *)buf;
1037 int i = 2,ret = -1;
1038 unsigned char sum=0;
1039 if(!p || len < 4) {
1040 printk("%s param err! \n",__FUNCTION__);
1041 return ret;
1042 }
1043 if( (p[0] == 0xa5) && (p[len-1] == 0x7e) ) {
1044 for(i = 2;i<len-2;i++)
1045 sum +=p[i];
1046 if(sum == p[1])
1047 ret = 0;
1048 }
1049 return ret;
1050
1051}
1052
1053
1054static int spi_dev_pin_init_test(struct spi_device *spi)
1055{
1056 struct spidev_data *spidev = spi_get_drvdata(spi);
1057 enum of_gpio_flags flags;
1058 static int spi_dev_pin_init_flag = 0;
1059 int status = 0;
1060
1061 if(spi_dev_pin_init_flag < 2){
1062 spidev->pctrl = devm_pinctrl_get(&spi->dev);
1063 if(!spidev->pctrl) {
1064 dev_info(&spi->dev,"get dev pctrl failed!\n",status);
1065 return status;
1066 }
1067
1068 spidev->pint_ex = pinctrl_lookup_state(spidev->pctrl, "int_ex");
1069 if (IS_ERR(spidev->pint_ex)) {
1070 dev_err(&spi->dev, "TEST: missing pint_ex \n");
1071 return status;
1072 }
1073 if (pinctrl_select_state(spidev->pctrl, spidev->pint_ex) < 0) {
1074 dev_err(&spi->dev, "TEST: slect pint_ex \n");
1075 return status;
1076 }
1077
1078 spidev->pgpioex = pinctrl_lookup_state(spidev->pctrl, "ex_gpio");
1079 if (IS_ERR(spidev->pgpioex)) {
1080 dev_err(&spi->dev, "TEST: missing ex_gpio \n");
1081 return status;
1082 }
1083
1084 spidev->gpio_ex = of_get_gpio_flags(spi->dev.of_node, 0, &flags);
1085 if (!gpio_is_valid(spidev->gpio_ex)) {
1086 dev_err(&spi->dev,"gpio_ex no found,spidev->gpio_ex=%d \n",spidev->gpio_ex);
1087 return status;
1088 }
1089 dev_info(&spi->dev,"gpio_ex found,spidev->gpio_ex=%d \n",spidev->gpio_ex);
1090
1091 status = gpio_request(spidev->gpio_ex, "gpio_ex");
1092 if (status) {
1093 pr_info("spidev->gpio_ex request error.\n");
1094 }else {
1095 gpio_direction_output(spidev->gpio_ex, 1);
1096 dev_info(&spi->dev, "spidev->gpio_ex success \n");
1097 }
1098
1099 spidev->gpio_int = of_get_gpio_flags(spi->dev.of_node, 1, &flags);
1100 if (!gpio_is_valid(spidev->gpio_int)) {
1101 dev_err(&spi->dev,"gpio_int no found,spidev->gpio_int=%d \n",spidev->gpio_int);
1102 return status;
1103 }
1104 dev_info(&spi->dev,"gpio_int found,spidev->gpio_int=%d \n",spidev->gpio_int);
1105
1106 spi_dev_pin_init_flag += 1;
1107 }
1108 return status;
1109
1110}
1111
1112static irqreturn_t spidev_master_hand_shake_irq(int irqno, void *dev_id)
1113{
1114 static int count;
1115 int gpio_in_status = 0,gpio_out_status = 0;
1116
1117 struct spidev_data *spidev = dev_id;
1118
1119 gpio_out_status = gpio_get_value(spidev->gpio_ex);
1120 gpio_in_status = gpio_get_value(spidev->gpio_int);
1121
1122 //pr_info("hand_shake_irq get = %d %d %d\n", ++count,gpio_out_status,gpio_in_status);
1123
1124 if(gpio_out_status && !gpio_in_status) {
1125 if(spidev->tx_flag == 0) {
1126 up(&spidev->rec_req); /*receive slave reqeuet*/
1127 }else {
1128 pr_info("mmm \r\n");
1129 up(&spidev->wait_req); /*first receive master req*/
1130 }
1131 }else if(!gpio_out_status && !gpio_in_status) {
1132 up(&spidev->wait_req); /*receive slave ack*/
1133 }else {
1134 pr_info("recive invalid request\n");
1135 }
1136 return IRQ_HANDLED;
1137}
1138
1139static irqreturn_t spidev_slave_hand_shake_irq(int irqno, void *dev_id)
1140{
1141 static int count;
1142 int gpio_in_status = 0,gpio_out_status = 0;
1143
1144 struct spidev_data *spidev = dev_id;
1145
1146 gpio_out_status = gpio_get_value(spidev->gpio_ex);
1147 gpio_in_status = gpio_get_value(spidev->gpio_int);
1148
1149 //pr_info("hand_shake_irq get = %d %d %d\n", ++count,gpio_out_status,gpio_in_status);
1150
1151 if(gpio_out_status && !gpio_in_status)
1152 {
1153 if(spidev->tx_flag == 0) {
1154 up(&spidev->rec_req); /*first receive master req*/
1155 }else {
1156 pr_info("sss \n");
1157 up(&spidev->wait_req);
1158 }
1159 /*. then set gpio_out low as ack. */
1160 }else if(!gpio_out_status && !gpio_in_status) {
1161 up(&spidev->wait_req); /*receive master ack*/
1162 }else {
1163 pr_info("recive invalid request\n");
1164 }
1165 return IRQ_HANDLED;
1166}
1167
1168
1169
1170static int spi_dev_irq_init_test(struct spi_device *spi)
1171{
1172 struct spidev_data *spidev = spi_get_drvdata(spi);
1173 static int spi_dev_irq_init_flag = 0;
1174 int irq = 0,ret = 0;
1175
1176 if(spi_dev_irq_init_flag < 2) {
1177 if(!spi || !spidev) {
1178 ret = -ENOENT;
1179 return ret;
1180 }
1181 irq = irq_of_parse_and_map(spi->dev.of_node, 0);
1182 if (irq <= 0) {
1183 dev_err(&spi->dev, "ERROR: invalid interrupt number, irq = %d\n",irq);
1184 return -EBUSY;
1185 }
1186 spidev->irq = irq;
1187 dev_info(&spi->dev, "used interrupt num is %d\n", spidev->irq);
1188 if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
1189 ret = devm_request_irq(&spi->dev, spidev->irq, spidev_master_hand_shake_irq,
1190 0, dev_name(&spi->dev), spidev);
1191 }else {
1192 ret = devm_request_irq(&spi->dev, spidev->irq, spidev_slave_hand_shake_irq,
1193 0, dev_name(&spi->dev), spidev);
1194 }
1195 if (ret < 0) {
1196 dev_err(&spi->dev, "probe - cannot get IRQ (%d)\n", ret);
1197 return ret;
1198 }
1199 spi_dev_irq_init_flag += 1;
1200 }
1201 return ret;
1202
1203}
1204
1205static size_t spi_dev_send_handle_pack_test(struct spidev_data *spidev,int len,struct spi_dev_hand_msg *recv_msg)
1206{
1207 struct spi_dev_hand_msg send_msg={0};
1208
1209 send_msg.head = MSG_HEAD;
1210 send_msg.len = len;
1211 send_msg.tail = MSG_TAIL;
1212
1213 struct spi_transfer t = {
1214 .tx_buf = &send_msg,
1215 .rx_buf = recv_msg,
1216 .len = sizeof(struct spi_dev_hand_msg),
1217 .speed_hz = spidev->speed_hz,
1218 };
1219 struct spi_message m;
1220
1221 spi_message_init(&m);
1222 spi_message_add_tail(&t, &m);
1223 return spidev_sync(spidev, &m);
1224
1225
1226}
1227
1228
1229
1230static size_t spi_dev_recv_handle_pack_test(struct spidev_data *spidev,int len,struct spi_dev_hand_msg *recv_msg)
1231{
1232 struct spi_dev_hand_msg send_msg={0};
1233
1234 struct spi_transfer t = {
1235 .tx_buf = &send_msg,
1236 .rx_buf = recv_msg,
1237 .len = sizeof(struct spi_dev_hand_msg),
1238 .speed_hz = spidev->speed_hz,
1239 };
1240 struct spi_message m;
1241
1242 spi_message_init(&m);
1243 spi_message_add_tail(&t, &m);
1244 return spidev_sync(spidev, &m);
1245
1246
1247}
1248
1249
1250static void wait_spi_bus_idle_status_test(struct spidev_data *spidev)
1251{
1252 int count = 0;
1253
1254 do {
1255 spin_lock_irq(&spidev->tx_flag_lock);
1256 if( gpio_get_value(spidev->gpio_ex) && gpio_get_value(spidev->gpio_int))
1257 break;
1258 else {
1259 spin_unlock(&spidev->tx_flag_lock);
1260 usleep_range(50,100);
1261 count++;
1262 if(count%20 == 0) {
1263 printk("bus busy %d us cnts.outst(%d),intst(%d).\n",count*50,
1264 gpio_get_value(spidev->gpio_ex),gpio_get_value(spidev->gpio_int));
1265 }
1266
1267 }
1268 }while(1);
1269 spidev->tx_flag = 1;
1270 spin_unlock_irq(&spidev->tx_flag_lock);
1271
1272}
1273
1274static size_t spi_dev_send_one_pack_test(struct spi_device *spi,size_t len) {
1275
1276 struct spidev_data *spidev = spi_get_drvdata(spi);
1277 struct spi_dev_hand_msg recv_msg={0};
1278 size_t status;
1279 int ret;
1280 int rx_data_flag = 0;
1281 if(len>4096)
1282 printk("len(%d) err: \r\n",len);
1283 wait_spi_bus_idle_status_test(spidev);
1284 gpio_set_value(spidev->gpio_ex,0);
1285 ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(50)); /*first ack m= 0,s=0*/
1286 if (ret < 0) {
1287 printk("first ack timeout\n");
1288 }
1289 spi_dev_send_handle_pack_test(spidev,len,&recv_msg); /*send head msg*/
1290 if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
1291 len = (recv_msg.len >= len) ? recv_msg.len : len;
1292 spidev->rx_cnt_in_tx_thread++;
1293 rx_data_flag = 1;
1294 if(len>4096)
1295 printk("len(%d) err: \r\n",len);
1296 }
1297 ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(100));
1298 if (ret < 0) {
1299 printk("second ack timeout\n");
1300 }
1301 //down(&spidev->wait_req); /*second ack m= 0,s=0*/
1302 status = spidev_sync_write_and_read(spidev,len);
1303 if(rx_data_flag && spidev->is_data_check) {
1304 ret = packet_check(spidev->rx_buffer,recv_msg.len);
1305 if(ret) {
1306 spidev->rx_data_check_err_cnt++;
1307 //dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
1308 }else {
1309 spidev->rx_data_check_ok_cnt++;
1310 //dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
1311 }
1312 }
1313 spidev->tx_flag = 0;
1314 gpio_set_value(spidev->gpio_ex,1);
1315 return status;
1316}
1317
1318
1319static size_t spi_dev_slave_send_one_pack_test(struct spi_device *spi,size_t len) {
1320
1321 struct spidev_data *spidev = spi_get_drvdata(spi);
1322 struct spi_dev_hand_msg recv_msg={0};
1323 size_t status;
1324 int ret;
1325 int rx_data_flag = 0;
1326 if(len>4096)
1327 printk("len(%d) err: \r\n",len);
1328 wait_spi_bus_idle_status_test(spidev);
1329 up(&spidev->rec_head_msg_req);/*response master tx/rx dma set */
1330 //printk("%s %d \r\n",__FUNCTION__,__LINE__);
1331 spi_dev_send_handle_pack_test(spidev,len,&recv_msg); /*send head msg*/
1332 if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
1333 if(len != recv_msg.len) {
1334 //printk("%s len=%d rec_len=%d\n",__FUNCTION__,len,recv_msg.len);
1335 len = (recv_msg.len >= len) ? recv_msg.len : len;
1336
1337 }
1338 spidev->rx_cnt_in_tx_thread++;
1339 rx_data_flag = 1;
1340 if(len>4096)
1341 printk("len(%d) err: \r\n",len);
1342 }
1343
1344 //down(&spidev->wait_req);
1345 ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(100));
1346 if (ret < 0) {
1347 printk("wait req timeout\n");
1348 }
1349 up(&spidev->rec_data_msg_req);/*response master tx/rx dma set */
1350 //printk("%s %d \r\n",__FUNCTION__,__LINE__);
1351 status = spidev_sync_write_and_read(spidev,len);
1352 if(rx_data_flag && spidev->is_data_check) {
1353 ret = packet_check(spidev->rx_buffer,recv_msg.len);
1354 if(ret) {
1355 spidev->rx_data_check_err_cnt++;
1356 //dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
1357 }else {
1358 spidev->rx_data_check_ok_cnt++;
1359 //dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
1360 }
1361 }
1362 spidev->tx_flag = 0;
1363 gpio_set_value(spidev->gpio_ex,1);
1364 return status;
1365}
1366
1367static int spi_dev_slave_read_hand_msg_process_test(void *arg)
1368{
1369 struct spi_device *spi = (struct spi_device *)arg;
1370 struct spidev_data *spidev = spi_get_drvdata(spi);
1371
1372 while(1) {
1373 down(&spidev->rec_head_msg_req);
1374 //printk("%s %d \r\n",__FUNCTION__,__LINE__);
1375 gpio_set_value(spidev->gpio_ex,0);
1376 }
1377 return 0;
1378}
1379
1380
1381static int spi_dev_slave_read_data_process_test(void *arg)
1382{
1383 struct spi_device *spi = (struct spi_device *)arg;
1384 struct spidev_data *spidev = spi_get_drvdata(spi);
1385 struct spi_dev_hand_msg *recv_msg=(struct spi_dev_hand_msg *)spidev->rx_buffer;
1386
1387 while(1) {
1388 down(&spidev->rec_data_msg_req);
1389 //printk("%s %d \r\n",__FUNCTION__,__LINE__);
1390 gpio_set_value(spidev->gpio_ex,1);
1391 usleep_range(50,100);
1392 gpio_set_value(spidev->gpio_ex,0);
1393 }
1394 return 0;
1395}
1396
1397static int spi_dev_master_read_thread_test(void *arg)
1398{
1399 struct spi_device *spi = (struct spi_device *)arg;
1400 struct spidev_data *spidev = spi_get_drvdata(spi);
1401 pid_t kid;
1402 struct pid *pid;
1403 struct task_struct * tsk;
1404 struct spi_dev_hand_msg recv_msg;
1405 int ret;
1406 if(!spidev){
1407 dev_info(&spi->dev,"spi_dev return \r\n");
1408 return 0;
1409 }
1410 while(1) {
1411
1412 down(&spidev->rec_req); /*first receive slave req*/
1413 spi_dev_recv_handle_pack_test(spidev, sizeof(struct spi_dev_hand_msg), &recv_msg);
1414 gpio_set_value(spidev->gpio_ex,0);
1415 ret = down_timeout(&spidev->wait_req, msecs_to_jiffies(100));
1416 if (ret < 0) {
1417 printk("%s wait req timeout\n",__FUNCTION__);
1418 }
1419 if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
1420 int len = recv_msg.len;
1421 spidev_sync_write_and_read(spidev, len); /*set dma and recv data msg*/
1422 spidev->rx_cnt_in_rx_thread++;
1423 if(spidev->is_data_check) {
1424 ret = packet_check(spidev->rx_buffer,len);
1425 if(ret) {
1426 spidev->rx_data_check_err_cnt++;
1427 //dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
1428 }else {
1429 spidev->rx_data_check_ok_cnt++;
1430 //dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
1431 }
1432 }
1433 gpio_set_value(spidev->gpio_ex,1);
1434 //print_buf_data(spidev->rx_buffer, len);
1435 }else {
1436 printk("%s data invalid\n",__FUNCTION__);
1437 gpio_set_value(spidev->gpio_ex,1);
1438 }
1439 }
1440 return 0;
1441}
1442
1443
1444
1445
1446static int spi_dev_slave_read_thread_test(void *arg)
1447{
1448 struct spi_device *spi = (struct spi_device *)arg;
1449 struct spidev_data *spidev = spi_get_drvdata(spi);
1450 int ret;
1451 struct spi_dev_hand_msg recv_msg;
1452
1453 if(!spidev){
1454 dev_info(&spi->dev,"spi_dev return \r\n");
1455 return 0;
1456 }
1457 while(1) {
1458
1459 down(&spidev->rec_req); /*first receive master req*/
1460 //printk("%s %d \r\n",__FUNCTION__,__LINE__);
1461 up(&spidev->rec_head_msg_req);/*response master tx/rx dma set */
1462 //printk("%s %d \r\n",__FUNCTION__,__LINE__);
1463 //spidev_sync_write_and_read(spidev,sizeof(struct spi_dev_hand_msg)); /*set dma and recv head msg*/
1464 spi_dev_recv_handle_pack_test(spidev, sizeof(struct spi_dev_hand_msg), &recv_msg);
1465 //recv_msg=(struct spi_dev_hand_msg *)spidev->rx_buffer;
1466 if(recv_msg.head == MSG_HEAD && recv_msg.tail == MSG_TAIL) {
1467 int len = recv_msg.len;
1468 up(&spidev->rec_data_msg_req); /*response master tx/rx dma set */
1469 //printk("%s %d %d \r\n",__FUNCTION__,__LINE__,len);
1470 spidev_sync_write_and_read(spidev, len); /*set dma and recv data msg*/
1471 if(spidev->is_data_check) {
1472 ret = packet_check(spidev->rx_buffer,len);
1473 if(ret) {
1474 spidev->rx_data_check_err_cnt++;
1475 //dev_info(&spi->dev,"%s packet check err \r\n",__FUNCTION__);
1476 }else {
1477 spidev->rx_data_check_ok_cnt++;
1478 //dev_info(&spi->dev,"%s packet check success \r\n",__FUNCTION__);
1479 }
1480 }
1481 gpio_set_value(spidev->gpio_ex,1);
1482 spidev->rx_cnt_in_rx_thread++;
1483 //print_buf_data(spidev->rx_buffer, len);
1484 }else {
1485 up(&spidev->rec_data_msg_req);
1486 printk("%s data invalid\n",__FUNCTION__);
1487 gpio_set_value(spidev->gpio_ex,1);
1488 }
1489 }
1490 return 0;
1491}
1492
1493
1494static int spidev_debug_test_init(struct spi_device *spi)
1495{
1496 int ret = 0;
1497 struct spidev_data *spidev = spi_get_drvdata(spi);
1498
1499 ret =spi_dev_pin_init_test(spi);
1500 if(ret) {
1501 dev_info(&spi->dev, "spi_dev_pin_init_test,ret=%d \n",ret);
1502 return ret;
1503 }
1504 spin_lock_init(&spidev->tx_flag_lock);
1505 sema_init(&spidev->wait_req, 0);
1506 sema_init(&spidev->rec_req, 0);
1507 sema_init(&spidev->rec_head_msg_req, 0);
1508 sema_init(&spidev->rec_data_msg_req, 0);
1509 spidev->tx_flag = 0;
1510 spidev->rx_cnt_in_rx_thread = 0;
1511 spidev->rx_cnt_in_tx_thread = 0;
1512 spidev->is_data_check = false;
1513 if (!spidev->tx_buffer) {
1514 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1515 if (!spidev->tx_buffer) {
1516 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1517 return ret;
1518 }
1519 }
1520
1521 if (!spidev->rx_buffer) {
1522 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1523 if (!spidev->rx_buffer) {
1524 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1525 kfree(spidev->tx_buffer);
1526 spidev->tx_buffer = NULL;
1527 return ret;
1528 }
1529 }
1530
1531 if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
1532 kernel_thread(spi_dev_master_read_thread_test,spi, 0); /* fork the main thread */
1533 }else {
1534 kernel_thread(spi_dev_slave_read_thread_test,spi, 0); /* fork the main thread */
1535 kernel_thread(spi_dev_slave_read_hand_msg_process_test,spi, 0);
1536 kernel_thread(spi_dev_slave_read_data_process_test,spi, 0);
1537 }
1538
1539 ret =spi_dev_irq_init_test(spi);
1540 if(ret) {
1541 dev_info(&spi->dev, "spi_dev_irq_init_test,ret=%d \n",ret);
1542 return ret;
1543 }
1544
1545 return ret;
1546}
1547
1548
1549static ssize_t spidevinfo_show(struct kobject *kobj, struct kobj_attribute *attr,
1550 char *buf)
1551{
1552
1553 ssize_t count = 0;
1554
1555 struct device *dev = container_of(kobj, struct device, kobj);
1556 //struct platform_device *pdev = container_of(dev, struct platform_device, dev);
1557 unsigned char cmd_str[16] = {0};
1558 u32 param1,param2,param3;
1559 u8 rwaddr,rwsize;
1560 int ret,i;
1561
1562
1563 return count;
1564
1565
1566}
1567extern void get_random_bytes(void * buf, size_t len);
1568static ssize_t spidevinfo_store(struct kobject *kobj, struct kobj_attribute *attr,
1569 const char *buf, size_t n)
1570
1571{
1572 ssize_t ret =0;
1573 struct device *dev = container_of(kobj, struct device, kobj);
1574 //struct platform_device *pdev = container_of(dev, struct platform_device, dev);
1575 struct spi_device *spi = (struct spi_device *)dev;
1576 struct spidev_data *spidev = spi_get_drvdata(spi);
1577 unsigned char cmd_str[0x20] = {0};
1578 u8 bBuf[32];
1579
1580 u32 param1 = 0,param2 = 0,param3 = 0;
1581 u32 rwaddr =0 ,rwsize = 0;
1582 int i;
1583 s8 rev = -1;
1584 size_t count = 0;
1585
1586
1587 dev_info(&spi->dev, "spidev->speed_hz:%d \n", spi->max_speed_hz);
1588
1589 sscanf(buf, "%31s %x %x %x", &cmd_str,&param1,&param2,&param3);
1590 dev_info(dev, "cmd_str:%s,param1:%x,param2:%x,param3:%x\n",cmd_str,param1,param2,param3);
1591
1592 dev_info(&spi->dev, "mode %d, %s%s%s%s%u bits/w, %u Hz max --\n",
1593 (int) (spi->mode & (SPI_CPOL | SPI_CPHA)),
1594 (spi->mode & SPI_CS_HIGH) ? "cs_high, " : "",
1595 (spi->mode & SPI_LSB_FIRST) ? "lsb, " : "",
1596 (spi->mode & SPI_3WIRE) ? "3wire, " : "",
1597 (spi->mode & SPI_LOOP) ? "loopback, " : "",
1598 spi->bits_per_word, spi->max_speed_hz);
1599
1600 count = param1;
1601 ret = strcmp(cmd_str,"spi_write");
1602 if( ret == 0) {
1603 count = param1;
1604 if (!spidev->tx_buffer) {
1605 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1606 if (!spidev->tx_buffer) {
1607 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1608 return n;
1609 }
1610 }
1611 dev_info(dev, "spidev->tx_buffer=0x%x\n",spidev->tx_buffer);
1612 for(i = 0;i<count;i++) {
1613 spidev->tx_buffer[i]=i;
1614 }
1615 print_buf_data(spidev->tx_buffer,count);
1616 ret = spidev_sync_write(spidev, count);
1617 if(ret == count) {
1618 dev_info(dev, "send len success(len:%d) \n",ret);
1619 }
1620 kfree(spidev->tx_buffer);
1621 spidev->tx_buffer = NULL;
1622 dev_info(dev, "spi write end: \n");
1623 }
1624
1625 ret = strcmp(cmd_str,"spi_read");
1626 if(ret == 0) {
1627 count = param1;
1628 if (!spidev->rx_buffer) {
1629 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1630 if (!spidev->rx_buffer) {
1631 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1632 return n;
1633 }
1634 }
1635 memset(spidev->rx_buffer,0x0,bufsiz);
1636 ret = spidev_sync_read(spidev, count);
1637
1638 if(ret == count) {
1639 dev_info(dev, "read len success(len:%d) \n",ret);
1640 print_buf_data(spidev->rx_buffer,count);
1641 }
1642 kfree(spidev->rx_buffer);
1643 spidev->rx_buffer = NULL;
1644 dev_info(dev, "spi read end: \n");
1645 }
1646
1647 ret = strcmp(cmd_str,"write_then_read");
1648 if(ret == 0) {
1649 count = param1;
1650
1651 if (!spidev->tx_buffer) {
1652 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1653 if (!spidev->tx_buffer) {
1654 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1655 return n;
1656 }
1657 }
1658
1659 if (!spidev->rx_buffer) {
1660 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1661 if (!spidev->rx_buffer) {
1662 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1663 kfree(spidev->tx_buffer);
1664 spidev->tx_buffer = NULL;
1665 return n;
1666 }
1667 }
1668 for(i = 0;i<count;i++) {
1669 spidev->tx_buffer[i]=i;
1670 }
1671 //memset(spidev->rx_buffer,0x0,bufsiz);
1672 ret = spi_write_then_read(spi, spidev->tx_buffer, count, spidev->rx_buffer, count);
1673
1674 if(ret == 0) {
1675 dev_info(dev, "spi write data(%d bytes) \n",count);
1676 print_buf_data(spidev->tx_buffer,count);
1677 dev_info(dev, "spi read data(%d bytes) \n",count);
1678 print_buf_data(spidev->rx_buffer,count);
1679 }
1680
1681 kfree(spidev->tx_buffer);
1682 spidev->tx_buffer = NULL;
1683 kfree(spidev->rx_buffer);
1684 spidev->rx_buffer = NULL;
1685 dev_info(dev, "write_then_read.\n");
1686
1687 }
1688
1689
1690 ret = strcmp(cmd_str,"write_and_read");
1691 if(ret == 0) {
1692 count = param1;
1693 if (!spidev->tx_buffer) {
1694 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1695 if (!spidev->tx_buffer) {
1696 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1697 return n;
1698 }
1699 }
1700
1701 if (!spidev->rx_buffer) {
1702 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1703 if (!spidev->rx_buffer) {
1704 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1705 kfree(spidev->tx_buffer);
1706 spidev->tx_buffer = NULL;
1707 return n;
1708 }
1709 }
1710 for(i = 0;i<count;i++) {
1711 spidev->tx_buffer[i]=i;
1712 }
1713 memset(spidev->rx_buffer,0x0,bufsiz);
1714 ret = spidev_sync_write_and_read(spidev, count);
1715
1716 if(ret == count) {
1717 dev_info(dev, "spi write data(%d bytes) \n",ret);
1718 print_buf_data(spidev->tx_buffer,count);
1719 dev_info(dev, "spi read data(%d bytes) \n",ret);
1720 print_buf_data(spidev->rx_buffer,count);
1721 dev_info(dev, "write_and_read.\n");
1722 }
1723#if 0
1724 kfree(spidev->tx_buffer);
1725 spidev->tx_buffer = NULL;
1726 kfree(spidev->rx_buffer);
1727 spidev->rx_buffer = NULL;
1728#endif
1729 dev_info(dev, "write_and_read.\n");
1730
1731 }
1732 ret = strcmp(cmd_str,"fifo_flush");
1733 if(ret == 0) {
1734 unsigned char buff[64] ={0};
1735
1736 ret = spidev_get_rxfifo(spi,buff);
1737 dev_info(dev, "get rx_fifo_len(%d bytes) \n",ret);
1738 print_buf_data(buff,ret);
1739 }
1740
1741 ret = strcmp(cmd_str,"timing-set");
1742 if(ret == 0) {
1743 dev_info(dev, "timing param(%d) \n",param1);
1744 set_spi_timing(spi,param1);
1745 }
1746
1747 ret = strcmp(cmd_str,"loop-en");
1748 if(ret == 0) {
1749 spi->mode |= SPI_LOOP;
1750 spi_setup(spi);
1751 }
1752 ret = strcmp(cmd_str,"loop-dis");
1753 if(ret == 0) {
1754 spi->mode &= ~SPI_LOOP;
1755 spi_setup(spi);
1756 }
1757 ret = strcmp(cmd_str,"speed_set");
1758 if(ret == 0) {
1759 spi->max_speed_hz = param1;
1760 spi_setup(spi);
1761 }
1762
1763 ret = strcmp(cmd_str,"mode_set");
1764 if(ret == 0) {
1765 if(param1 != 0 && param1 != 1 && param1 != 2 && param1 != 3)
1766 dev_info(dev, "param err(%d) \n",param1);
1767 else
1768 dev_info(dev, "set spi mode(%d) \n",param1);
1769 spi->mode &= (~0x3);
1770 spi->mode |= param1;
1771 ret = spi_setup(spi);
1772 dev_info(dev, "set spi mode(0x%x),ret=%d \n",spi->mode,ret);
1773 }
1774
1775
1776 ret = strcmp(cmd_str,"slave_mode_set");
1777 if(ret == 0) {
1778 if(param1 != 0 && param1 != 1 && param1 != 2 && param1 != 3)
1779 dev_info(dev, "param err(%d) \n",param1);
1780 else
1781 dev_info(dev, "set spi mode(%d) \n",param1);
1782 slave_mode_set(spi,param1);
1783 }
1784
1785 ret = strcmp(cmd_str,"send_msg_rand_len");
1786 if(ret == 0) {
1787
1788 count = 0;
1789 int times = param1;
1790 while(times--) {
1791
1792 get_random_bytes(&count,4);
1793 count = (count%0x1000) + 1;
1794 if (!spidev->tx_buffer) {
1795 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1796 if (!spidev->tx_buffer) {
1797 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1798 return n;
1799 }
1800 }
1801
1802 if (!spidev->rx_buffer) {
1803 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1804 if (!spidev->rx_buffer) {
1805 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1806 kfree(spidev->tx_buffer);
1807 spidev->tx_buffer = NULL;
1808 return n;
1809 }
1810 }
1811 for(i = 0;i<count;i++) {
1812 spidev->tx_buffer[i]=i;
1813 }
1814 //memset(spidev->rx_buffer,0x0,bufsiz);
1815 if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
1816 ret = spi_dev_send_one_pack_test(spi, count);
1817 }
1818 else {
1819 ret = spi_dev_slave_send_one_pack_test(spi, count);
1820 }
1821 if(ret == count) {
1822 #if 0
1823 dev_info(dev, "spi write data(%d bytes) \n",ret);
1824 print_buf_data(spidev->tx_buffer,count);
1825 dev_info(dev, "spi read data(%d bytes) \n",ret);
1826 print_buf_data(spidev->rx_buffer,count);
1827 #endif
1828 dev_info(dev, "write_and_read success. retain times:%d rx_cnt_in_tx_thread:%d spidev->rx_cnt_in_rx_thread:%d \n",
1829 times,spidev->rx_cnt_in_tx_thread,spidev->rx_cnt_in_rx_thread);
1830
1831 }
1832 msleep((count%5)+1);
1833 //usleep_range(5+(count%10),20);
1834 }
1835#if 0
1836 kfree(spidev->tx_buffer);
1837 spidev->tx_buffer = NULL;
1838 kfree(spidev->rx_buffer);
1839 spidev->rx_buffer = NULL;
1840#endif
1841
1842 }
1843
1844 ret = strcmp(cmd_str,"send_msg_fixed_len");
1845 if(ret == 0) {
1846 int times = param1;
1847 int debug = param3;
1848 count = param2;
1849 if(count > 4096) {
1850 printk("msg_fixed_len(%d bytes) out of range(4KB)\r\n",count);
1851 return n;
1852 }
1853 while(times--) {
1854 if (!spidev->tx_buffer) {
1855 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1856 if (!spidev->tx_buffer) {
1857 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1858 return n;
1859 }
1860 }
1861
1862 if (!spidev->rx_buffer) {
1863 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1864 if (!spidev->rx_buffer) {
1865 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1866 kfree(spidev->tx_buffer);
1867 spidev->tx_buffer = NULL;
1868 return n;
1869 }
1870 }
1871
1872 get_random_bytes(spidev->tx_buffer,count);
1873 //memset(spidev->rx_buffer,0x0,bufsiz);
1874 #if 0
1875 for(i = 0;i<count;i++) {
1876 spidev->tx_buffer[i]=i;
1877 }
1878 memset(spidev->rx_buffer,0x0,bufsiz);
1879 #endif
1880 if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
1881 ret = spi_dev_send_one_pack_test(spi, count);
1882 }
1883 else {
1884 ret = spi_dev_slave_send_one_pack_test(spi, count);
1885 }
1886 if(ret == count) {
1887 if(debug) {
1888 dev_info(dev, "spi write data(%d bytes) \n",ret);
1889 print_buf_data(spidev->tx_buffer,count);
1890 }
1891 dev_info(dev, "write_and_read success. retain times:%d rx_cnt_in_tx_thread:%d spidev->rx_cnt_in_rx_thread:%d \n",
1892 times,spidev->rx_cnt_in_tx_thread,spidev->rx_cnt_in_rx_thread);
1893 }
1894 msleep((count%5)+1);
1895 }
1896#if 0
1897 kfree(spidev->tx_buffer);
1898 spidev->tx_buffer = NULL;
1899 kfree(spidev->rx_buffer);
1900 spidev->rx_buffer = NULL;
1901#endif
1902
1903 }
1904
1905 ret = strcmp(cmd_str,"data_check_ctrl");
1906 if(ret == 0) {
1907 if(param1) {
1908 spidev->is_data_check = true;
1909 spidev->rx_data_check_ok_cnt = 0;
1910 spidev->rx_data_check_err_cnt = 0;
1911 }
1912 else {
1913 spidev->is_data_check = false;
1914 }
1915 dev_info(dev, "rx_check_ok_cnt:%d rx_check_err_cnt:%d\n",spidev->rx_data_check_ok_cnt,spidev->rx_data_check_err_cnt);
1916 }
1917 ret = strcmp(cmd_str,"send_msg_with_check");
1918 if(ret == 0) {
1919 int times = param1;
1920 int debug = param3;
1921 count = param2;
1922 if(count > 4096 || count < 4) {
1923 printk("msg_fixed_len(%d bytes) out of range(4KB)\r\n",count);
1924 return n;
1925 }
1926 while(times--) {
1927 if (!spidev->tx_buffer) {
1928 spidev->tx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1929 if (!spidev->tx_buffer) {
1930 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1931 return n;
1932 }
1933 }
1934
1935 if (!spidev->rx_buffer) {
1936 spidev->rx_buffer = kmalloc(bufsiz, GFP_KERNEL);
1937 if (!spidev->rx_buffer) {
1938 dev_dbg(&spidev->spi->dev, "open/ENOMEM\n");
1939 kfree(spidev->tx_buffer);
1940 spidev->tx_buffer = NULL;
1941 return n;
1942 }
1943 }
1944
1945 get_random_bytes(spidev->tx_buffer,count);
1946 spidev->tx_buffer[0] = 0xa5;
1947 spidev->tx_buffer[count-1] = 0x7e;
1948 ret = data_to_packet(spidev->tx_buffer,count);
1949 //memset(spidev->rx_buffer,0x0,bufsiz);
1950 if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
1951 ret = spi_dev_send_one_pack_test(spi, count);
1952 }
1953 else {
1954 ret = spi_dev_slave_send_one_pack_test(spi, count);
1955 }
1956 if(ret == count) {
1957 if(debug) {
1958 dev_info(dev, "spi write data(%d bytes) \n",ret);
1959 print_buf_data(spidev->tx_buffer,count);
1960 }
1961 dev_info(dev, "complete.retain:%d rx_cnt_in_tx_thread:%d spidev->rx_cnt_in_rx_thread:%d rx_check_ok_cnt:%d rx_check_err_cnt:%d\n",
1962 times,spidev->rx_cnt_in_tx_thread,spidev->rx_cnt_in_rx_thread,
1963 spidev->rx_data_check_ok_cnt,spidev->rx_data_check_err_cnt);
1964 }
1965 usleep_range(5+(count%10),20);
1966 }
1967 }
1968
1969 ret = strcmp(cmd_str,"gpio_out_val");
1970 if(ret == 0) {
1971 if(param1)
1972 gpio_set_value(spidev->gpio_ex,1);
1973 else
1974 gpio_set_value(spidev->gpio_ex,0);
1975 }
1976 ret = strcmp(cmd_str,"test_ktime_get");
1977 if(ret == 0) {
1978 ktime_t k_time_start = 0;
1979 ktime_t k_time_end = 0;
1980 ktime_t diff = 0;
1981
1982 k_time_start = ktime_get();
1983 gpio_set_value(spidev->gpio_ex,0);
1984 do {
1985 diff = ktime_sub(ktime_get(),k_time_start);
1986 }while(diff <= (param1*1000));
1987 gpio_set_value(spidev->gpio_ex,1);
1988 printk("test ktime_get: start=%lld end=%lld diff=%lld \r\n",k_time_start,ktime_get(),diff);
1989 }
1990 return n;
1991
1992}
1993
1994
1995SPIDEV_ATTR(spidevinfo);
1996
1997
1998static struct attribute * test_attr[] = {
1999 &spidevinfo_attr.attr,
2000
2001 NULL,
2002};
2003
2004static const struct attribute_group attr_group = {
2005 .attrs = test_attr,
2006};
2007
2008static const struct attribute_group *attr_groups[] = {
2009 &attr_group,
2010
2011 NULL,
2012};
2013#endif
2014
2015
2016#ifdef TEST_SWAP_KERNEL_AND_USER
2017
2018/* v3e
2019spi0(master)-------------------------------------spi1(slave)
2020GPIO129 <----------------------------------------INT4(GPIO51)
2021INT7(GPIO54) <-----------------------------------GPIO130
2022*/
2023
2024/* v3 mdl
20254#(master) --------------------------------------5#(slave)
2026GPIO130 <----------------------------------------INT6(GPIO53)
2027INT7(GPIO54) <-----------------------------------GPIO131
2028*/
2029
2030//#define TEST_SPI_SLAVE
2031#ifdef TEST_SPI_SLAVE
2032#define GPIO_NUM_EX 131
2033#define GPIO_NUM_INT 53
2034#else
2035#define GPIO_NUM_EX 130
2036#define GPIO_NUM_INT 54
2037#endif
2038
2039static int spi_dev_pin_init_test(struct spi_device *spi)
2040{
2041 struct spidev_data *spidev = spi_get_drvdata(spi);
2042 enum of_gpio_flags flags;
2043 int status = 0;
2044
2045
2046 spidev->pctrl = devm_pinctrl_get(&spi->dev);
2047 if(!spidev->pctrl) {
2048 dev_info(&spi->dev,"get dev pctrl failed!\n",status);
2049 return status;
2050 }
2051
2052 spidev->pint_ex = pinctrl_lookup_state(spidev->pctrl, "int_ex");
2053 if (IS_ERR(spidev->pint_ex)) {
2054 dev_err(&spi->dev, "TEST: missing pint_ex \n");
2055 return status;
2056 }
2057
2058 if (pinctrl_select_state(spidev->pctrl, spidev->pint_ex) < 0) {
2059 dev_err(&spi->dev, "TEST: slect pint_ex \n");
2060 return status;
2061 }
2062 if(strcmp(dev_name(&spi->dev),"spi1.0")==0) {
2063 spidev->gpio_ex = GPIO_NUM_EX;
2064 spidev->gpio_int = GPIO_NUM_INT;
2065 } else {
2066 spidev->gpio_ex = 130;
2067 spidev->gpio_int = 51;
2068 }
2069 return status;
2070
2071}
2072
2073static void send_signal(int sig_no,void *dev_id)
2074{
2075 int ret;
2076 struct spidev_data *spidev = (struct spidev_data *)dev_id;
2077 struct kernel_siginfo info;
2078 struct task_struct * my_task = NULL;
2079
2080 //printk("send signal %d to pid %d \n",sig_no,spidev->pid);
2081 memset(&info,0,sizeof(struct siginfo));
2082 if(spidev->pid == 0) {
2083 printk("send_signal pid is not valid \n");
2084 return;
2085 }
2086
2087 info.si_signo = sig_no;
2088 info.si_code = gpio_get_value(spidev->gpio_int);
2089 info.si_errno = gpio_get_value(spidev->gpio_ex);
2090 rcu_read_lock();
2091 my_task = pid_task(find_vpid(spidev->pid),PIDTYPE_PID);
2092 rcu_read_unlock();
2093
2094 if(!my_task) {
2095 printk("%s get pid_task failed! \n",__FUNCTION__);
2096 return;
2097 }
2098 ret = send_sig_info(sig_no, &info, my_task);
2099 if(ret < 0)
2100 printk("send signal failed! \n");
2101
2102}
2103
2104static int spi_dev_sig_process_test(void *arg)
2105{
2106 struct spi_device *spi = (struct spi_device *)arg;
2107 struct spidev_data *spidev = spi_get_drvdata(spi);
2108
2109 while(1) {
2110 down(&spidev->sig_req);
2111 send_signal(SIGUSR1,spidev);
2112
2113 }
2114 return 0;
2115}
2116static void send_dma_cfg_done_signal(int sig_no,void *dev_id)
2117{
2118 int ret;
2119 struct spidev_data *spidev = (struct spidev_data *)dev_id;
2120 struct kernel_siginfo info;
2121 struct task_struct * my_task = NULL;
2122 int dma_cfg_done = 0;
2123 //printk("send signal %d to pid %d \n",sig_no,spidev->pid);
2124 memset(&info,0,sizeof(struct siginfo));
2125
2126 if(spidev->dma_cfg_done == 1) {
2127 dma_cfg_done = spidev->dma_cfg_done;
2128 spidev->dma_cfg_done= 0;
2129 }
2130 if(spidev->pid == 0) {
2131 printk("%s is not valid\n",__FUNCTION__);
2132 return;
2133 }
2134 info.si_signo = sig_no;
2135 info.si_errno = dma_cfg_done;
2136 rcu_read_lock();
2137 my_task = pid_task(find_vpid(spidev->pid),PIDTYPE_PID);
2138 rcu_read_unlock();
2139
2140 if(!my_task) {
2141 printk("%s get pid_task failed! \n",__FUNCTION__);
2142 return;
2143 }
2144 ret = send_sig_info(sig_no, &info, my_task);
2145 if(ret < 0)
2146 printk("send signal failed! \n");
2147
2148}
2149
2150static int spi_dev_dma_cfg_done_process_test(void *arg)
2151{
2152 struct spi_device *spi = (struct spi_device *)arg;
2153 struct spidev_data *spidev = spi_get_drvdata(spi);
2154
2155 while(1) {
2156 down(&spidev->sem_dma_cfg_done);
2157 send_dma_cfg_done_signal(SIGUSR2,spidev);
2158 }
2159 return 0;
2160}
2161
2162static irqreturn_t spidev_hand_shake_irq(int irqno, void *dev_id)
2163{
2164 struct spidev_data *spidev = (struct spidev_data *)dev_id;
2165
2166 int gpio_out_status = gpio_get_value(spidev->gpio_ex);
2167 int gpio_int_status = gpio_get_value(spidev->gpio_int);
2168
2169 up(&spidev->sig_req);
2170 dev_dbg(&spidev->spi->dev,"out=%d int=%d \r\n",gpio_out_status,gpio_int_status);
2171
2172 return IRQ_HANDLED;
2173}
2174
2175
2176static int spi_dev_irq_init_test(struct spi_device *spi)
2177{
2178 struct spidev_data *spidev = spi_get_drvdata(spi);
2179 int irq = 0,ret = 0;
2180
2181 if(!spi || !spidev) {
2182 ret = -ENOENT;
2183 return ret;
2184 }
2185
2186 sema_init(&spidev->sig_req, 0);
2187 sema_init(&spidev->sem_dma_cfg_done, 0);
2188 kernel_thread(spi_dev_sig_process_test,spi, 0); /* fork the main thread */
2189 kernel_thread(spi_dev_dma_cfg_done_process_test,spi, 0); /* fork the main thread */
2190 irq = irq_of_parse_and_map(spi->dev.of_node, 0);
2191 if (irq <= 0) {
2192 dev_err(&spi->dev, "ERROR: invalid interrupt number, irq = %d\n",irq);
2193 return -EBUSY;
2194 }
2195 spidev->irq = irq;
2196 dev_info(&spi->dev, "used interrupt num is %d\n", spidev->irq);
2197
2198 ret = devm_request_irq(&spi->dev, spidev->irq, spidev_hand_shake_irq,
2199 0, dev_name(&spi->dev), spidev);
2200
2201 if (ret < 0) {
2202 dev_err(&spi->dev, "probe - cannot get IRQ (%d)\n", ret);
2203 return ret;
2204 }
2205
2206 return ret;
2207
2208}
2209
2210#endif
2211
2212#ifdef TEST_SWAP_KERNEL_AND_USER
2213void spi_dev_send_dma_cfg_down(struct spi_device *spi)
2214{
2215 struct spidev_data *spidev = spi_get_drvdata(spi);
2216 spidev->dma_cfg_done = 1;
2217 up(&spidev->sem_dma_cfg_done);
2218}
2219#else
2220void spi_dev_send_dma_cfg_down(struct spi_device *spi)
2221{
2222 return;
2223}
2224#endif
2225/*-------------------------------------------------------------------------*/
2226
2227static int spidev_probe(struct spi_device *spi)
2228{
2229 struct spidev_data *spidev;
2230 int status;
2231 unsigned long minor;
2232 u32 val;
2233 /*
2234 * spidev should never be referenced in DT without a specific
2235 * compatible string, it is a Linux implementation thing
2236 * rather than a description of the hardware.
2237 */
2238 WARN(spi->dev.of_node &&
2239 of_device_is_compatible(spi->dev.of_node, "spidev"),
2240 "%pOF: buggy DT: spidev listed directly in DT\n", spi->dev.of_node);
2241
2242 spidev_probe_acpi(spi);
2243
2244 /* Allocate driver data */
2245 spidev = kzalloc(sizeof(*spidev), GFP_KERNEL);
2246 if (!spidev)
2247 return -ENOMEM;
2248
2249 /* Initialize the driver data */
2250 spidev->spi = spi;
2251 spin_lock_init(&spidev->spi_lock);
2252 mutex_init(&spidev->buf_lock);
2253
2254 INIT_LIST_HEAD(&spidev->device_entry);
2255
2256 if (device_property_read_u32(&spi->dev, "enable_dma",&val)) {
2257 spi->dma_used = 0;
2258 dev_err(&spi->dev,"enable_dma get failed");
2259 }
2260 else {
2261 spi->dma_used = val;
2262 dev_info(&spi->dev,"enable_dma = 0x%x",val);
2263 }
2264
2265 if (device_property_read_u32(&spi->dev, "enable_trans_gap",&val)) {
2266 spi->trans_gaped = 0;
2267 dev_err(&spi->dev,"enable_trans_gap get failed");
2268 }
2269 else {
2270 spi->trans_gaped = val;
2271 dev_info(&spi->dev,"enable_trans_gap = 0x%x",val);
2272 }
2273
2274 if (device_property_read_u32(&spi->dev, "trans_gap_num",&val)) {
2275 spi->trans_gap_num = 0;
2276 dev_err(&spi->dev,"trans_gap_num get failed");
2277 }
2278 else {
2279 spi->trans_gap_num = val;
2280 dev_info(&spi->dev,"trans_gap_num = 0x%x",val);
2281 }
2282
xf.li1867bfa2024-08-20 02:32:16 -07002283 if (device_property_read_u32(&spi->dev, "setup-immediately",&val)) {
2284 spi->setup_immediately = 0;
2285 dev_err(&spi->dev,"setup-immediately get failed");
2286 }
2287 else {
2288 spi->setup_immediately = val;
2289 dev_info(&spi->dev,"setup-immediately = 0x%x",val);
2290 }
xf.li2f424182024-08-20 00:47:34 -07002291
2292 /* If we can allocate a minor number, hook up this device.
2293 * Reusing minors is fine so long as udev or mdev is working.
2294 */
2295 mutex_lock(&device_list_lock);
2296 minor = find_first_zero_bit(minors, N_SPI_MINORS);
2297 if (minor < N_SPI_MINORS) {
2298 struct device *dev;
2299
2300 spidev->devt = MKDEV(SPIDEV_MAJOR, minor);
2301 dev = device_create(spidev_class, &spi->dev, spidev->devt,
2302 spidev, "spidev%d.%d",
2303 spi->master->bus_num, spi->chip_select);
2304 status = PTR_ERR_OR_ZERO(dev);
2305 } else {
2306 dev_dbg(&spi->dev, "no minor number available!\n");
2307 status = -ENODEV;
2308 }
2309 if (status == 0) {
2310 set_bit(minor, minors);
2311 list_add(&spidev->device_entry, &device_list);
2312 }
2313 mutex_unlock(&device_list_lock);
2314
2315 spidev->speed_hz = spi->max_speed_hz;
2316 spidev->rd_from_rx_buffer = 0;
2317 if (status == 0)
2318 spi_set_drvdata(spi, spidev);
2319 else
2320 kfree(spidev);
2321 spi_setup(spi);
2322 if(0 == status && spi->master->slave)
2323 device_init_wakeup(&spi->dev, true);
2324#ifdef SPIDEV_DEBUG
2325 int ret = sysfs_create_groups(&spi->dev.kobj, attr_groups);
2326
2327 if (ret) {
2328 dev_err(&spi->dev, "create test_kobj attr group fain error=%d\n", ret);
2329 return ret;
2330 }
2331
2332 ret = spidev_debug_test_init(spi);
2333 if (ret) {
2334 dev_err(&spi->dev, "spidev_debug_test_init error=%d\n", ret);
2335 return ret;
2336 }
2337#endif
2338
2339#ifdef TEST_SWAP_KERNEL_AND_USER
2340 int ret;
2341 spidev->dma_cfg_done = 0;
2342 spidev->pid = 0;
2343 ret =spi_dev_pin_init_test(spi);
2344 if(ret) {
2345 dev_info(&spi->dev, "spi_dev_pin_init_test,ret=%d \n",ret);
2346 return ret;
2347 }
2348
2349 ret =spi_dev_irq_init_test(spi);
2350 if(ret) {
2351 dev_info(&spi->dev, "spi_dev_irq_init_test,ret=%d \n",ret);
2352 return ret;
2353 }
2354#endif
2355 return status;
2356}
2357
2358static int spidev_remove(struct spi_device *spi)
2359{
2360 struct spidev_data *spidev = spi_get_drvdata(spi);
2361
2362 /* prevent new opens */
2363 mutex_lock(&device_list_lock);
2364 /* make sure ops on existing fds can abort cleanly */
2365 spin_lock_irq(&spidev->spi_lock);
2366 spidev->spi = NULL;
2367 spin_unlock_irq(&spidev->spi_lock);
2368
2369 list_del(&spidev->device_entry);
2370 device_destroy(spidev_class, spidev->devt);
2371 clear_bit(MINOR(spidev->devt), minors);
2372 if (spidev->users == 0)
2373 kfree(spidev);
2374 mutex_unlock(&device_list_lock);
2375
2376 return 0;
2377}
2378
2379static struct spi_driver spidev_spi_driver = {
2380 .driver = {
2381 .name = "spidev",
2382 .of_match_table = of_match_ptr(spidev_dt_ids),
2383 .acpi_match_table = ACPI_PTR(spidev_acpi_ids),
2384 },
2385 .probe = spidev_probe,
2386 .remove = spidev_remove,
2387
2388 /* NOTE: suspend/resume methods are not necessary here.
2389 * We don't do anything except pass the requests to/from
2390 * the underlying controller. The refrigerator handles
2391 * most issues; the controller driver handles the rest.
2392 */
2393};
2394
2395/*-------------------------------------------------------------------------*/
2396
2397static int __init spidev_init(void)
2398{
2399 int status;
2400
2401 /* Claim our 256 reserved device numbers. Then register a class
2402 * that will key udev/mdev to add/remove /dev nodes. Last, register
2403 * the driver which manages those device numbers.
2404 */
2405 BUILD_BUG_ON(N_SPI_MINORS > 256);
2406 status = register_chrdev(SPIDEV_MAJOR, "spi", &spidev_fops);
2407 if (status < 0)
2408 return status;
2409
2410 spidev_class = class_create(THIS_MODULE, "spidev");
2411 if (IS_ERR(spidev_class)) {
2412 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
2413 return PTR_ERR(spidev_class);
2414 }
2415
2416 status = spi_register_driver(&spidev_spi_driver);
2417 if (status < 0) {
2418 class_destroy(spidev_class);
2419 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
2420 }
2421 return status;
2422}
2423module_init(spidev_init);
2424
2425static void __exit spidev_exit(void)
2426{
2427 spi_unregister_driver(&spidev_spi_driver);
2428 class_destroy(spidev_class);
2429 unregister_chrdev(SPIDEV_MAJOR, spidev_spi_driver.driver.name);
2430}
2431module_exit(spidev_exit);
2432
2433MODULE_AUTHOR("Andrea Paterniani, <a.paterniani@swapp-eng.it>");
2434MODULE_DESCRIPTION("User mode SPI device interface");
2435MODULE_LICENSE("GPL");
2436MODULE_ALIAS("spi:spidev");