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