1 // SPDX-License-Identifier: GPL-2.0-only
3 * cmt_speech.c - HSI CMT speech driver
5 * Copyright (C) 2008,2009,2010 Nokia Corporation. All rights reserved.
7 * Contact: Kai Vehmanen <kai.vehmanen@nokia.com>
8 * Original author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
11 #include <linux/errno.h>
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/init.h>
15 #include <linux/device.h>
16 #include <linux/miscdevice.h>
18 #include <linux/slab.h>
20 #include <linux/poll.h>
21 #include <linux/sched/signal.h>
22 #include <linux/ioctl.h>
23 #include <linux/uaccess.h>
24 #include <linux/pm_qos.h>
25 #include <linux/hsi/hsi.h>
26 #include <linux/hsi/ssi_protocol.h>
27 #include <linux/hsi/cs-protocol.h>
29 #define CS_MMAP_SIZE PAGE_SIZE
32 struct list_head list
;
38 struct hsi_client
*cl
;
39 struct cs_hsi_iface
*hi
;
40 struct list_head chardev_queue
;
41 struct list_head dataind_queue
;
44 unsigned long mmap_base
;
45 unsigned long mmap_size
;
47 struct fasync_struct
*async_queue
;
48 wait_queue_head_t wait
;
54 #define SSI_CHANNEL_STATE_READING 1
55 #define SSI_CHANNEL_STATE_WRITING (1 << 1)
56 #define SSI_CHANNEL_STATE_POLL (1 << 2)
57 #define SSI_CHANNEL_STATE_ERROR (1 << 3)
59 #define TARGET_MASK 0xf000000
60 #define TARGET_REMOTE (1 << CS_DOMAIN_SHIFT)
61 #define TARGET_LOCAL 0
63 /* Number of pre-allocated commands buffers */
67 * During data transfers, transactions must be handled
68 * within 20ms (fixed value in cmtspeech HSI protocol)
70 #define CS_QOS_LATENCY_FOR_DATA_USEC 20000
72 /* Timeout to wait for pending HSI transfers to complete */
73 #define CS_HSI_TRANSFER_TIMEOUT_MS 500
76 #define RX_PTR_BOUNDARY_SHIFT 8
77 #define RX_PTR_MAX_SHIFT (RX_PTR_BOUNDARY_SHIFT + \
80 struct hsi_client
*cl
;
81 struct hsi_client
*master
;
83 unsigned int iface_state
;
84 unsigned int wakeline_state
;
85 unsigned int control_state
;
86 unsigned int data_state
;
88 /* state exposed to application */
89 struct cs_mmap_config_block
*mmap_cfg
;
91 unsigned long mmap_base
;
92 unsigned long mmap_size
;
97 /* note: for security reasons, we do not trust the contents of
98 * mmap_cfg, but instead duplicate the variables here */
99 unsigned int buf_size
;
100 unsigned int rx_bufs
;
101 unsigned int tx_bufs
;
102 unsigned int rx_ptr_boundary
;
103 unsigned int rx_offsets
[CS_MAX_BUFFERS
];
104 unsigned int tx_offsets
[CS_MAX_BUFFERS
];
106 /* size of aligned memory blocks */
107 unsigned int slot_size
;
110 struct list_head cmdqueue
;
112 struct hsi_msg
*data_rx_msg
;
113 struct hsi_msg
*data_tx_msg
;
114 wait_queue_head_t datawait
;
116 struct pm_qos_request pm_qos_req
;
121 static struct cs_char cs_char_data
;
123 static void cs_hsi_read_on_control(struct cs_hsi_iface
*hi
);
124 static void cs_hsi_read_on_data(struct cs_hsi_iface
*hi
);
126 static inline void rx_ptr_shift_too_big(void)
128 BUILD_BUG_ON((1LLU << RX_PTR_MAX_SHIFT
) > UINT_MAX
);
131 static void cs_notify(u32 message
, struct list_head
*head
)
133 struct char_queue
*entry
;
135 spin_lock(&cs_char_data
.lock
);
137 if (!cs_char_data
.opened
) {
138 spin_unlock(&cs_char_data
.lock
);
142 entry
= kmalloc(sizeof(*entry
), GFP_ATOMIC
);
144 dev_err(&cs_char_data
.cl
->device
,
145 "Can't allocate new entry for the queue.\n");
146 spin_unlock(&cs_char_data
.lock
);
150 entry
->msg
= message
;
151 list_add_tail(&entry
->list
, head
);
153 spin_unlock(&cs_char_data
.lock
);
155 wake_up_interruptible(&cs_char_data
.wait
);
156 kill_fasync(&cs_char_data
.async_queue
, SIGIO
, POLL_IN
);
162 static u32
cs_pop_entry(struct list_head
*head
)
164 struct char_queue
*entry
;
167 entry
= list_entry(head
->next
, struct char_queue
, list
);
169 list_del(&entry
->list
);
175 static void cs_notify_control(u32 message
)
177 cs_notify(message
, &cs_char_data
.chardev_queue
);
180 static void cs_notify_data(u32 message
, int maxlength
)
182 cs_notify(message
, &cs_char_data
.dataind_queue
);
184 spin_lock(&cs_char_data
.lock
);
185 cs_char_data
.dataind_pending
++;
186 while (cs_char_data
.dataind_pending
> maxlength
&&
187 !list_empty(&cs_char_data
.dataind_queue
)) {
188 dev_dbg(&cs_char_data
.cl
->device
, "data notification "
189 "queue overrun (%u entries)\n", cs_char_data
.dataind_pending
);
191 cs_pop_entry(&cs_char_data
.dataind_queue
);
192 cs_char_data
.dataind_pending
--;
194 spin_unlock(&cs_char_data
.lock
);
197 static inline void cs_set_cmd(struct hsi_msg
*msg
, u32 cmd
)
199 u32
*data
= sg_virt(msg
->sgt
.sgl
);
203 static inline u32
cs_get_cmd(struct hsi_msg
*msg
)
205 u32
*data
= sg_virt(msg
->sgt
.sgl
);
209 static void cs_release_cmd(struct hsi_msg
*msg
)
211 struct cs_hsi_iface
*hi
= msg
->context
;
213 list_add_tail(&msg
->link
, &hi
->cmdqueue
);
216 static void cs_cmd_destructor(struct hsi_msg
*msg
)
218 struct cs_hsi_iface
*hi
= msg
->context
;
220 spin_lock(&hi
->lock
);
222 dev_dbg(&cs_char_data
.cl
->device
, "control cmd destructor\n");
224 if (hi
->iface_state
!= CS_STATE_CLOSED
)
225 dev_err(&hi
->cl
->device
, "Cmd flushed while driver active\n");
227 if (msg
->ttype
== HSI_MSG_READ
)
229 ~(SSI_CHANNEL_STATE_POLL
| SSI_CHANNEL_STATE_READING
);
230 else if (msg
->ttype
== HSI_MSG_WRITE
&&
231 hi
->control_state
& SSI_CHANNEL_STATE_WRITING
)
232 hi
->control_state
&= ~SSI_CHANNEL_STATE_WRITING
;
236 spin_unlock(&hi
->lock
);
239 static struct hsi_msg
*cs_claim_cmd(struct cs_hsi_iface
* ssi
)
243 BUG_ON(list_empty(&ssi
->cmdqueue
));
245 msg
= list_first_entry(&ssi
->cmdqueue
, struct hsi_msg
, link
);
246 list_del(&msg
->link
);
247 msg
->destructor
= cs_cmd_destructor
;
252 static void cs_free_cmds(struct cs_hsi_iface
*ssi
)
254 struct hsi_msg
*msg
, *tmp
;
256 list_for_each_entry_safe(msg
, tmp
, &ssi
->cmdqueue
, link
) {
257 list_del(&msg
->link
);
258 msg
->destructor
= NULL
;
259 kfree(sg_virt(msg
->sgt
.sgl
));
264 static int cs_alloc_cmds(struct cs_hsi_iface
*hi
)
270 INIT_LIST_HEAD(&hi
->cmdqueue
);
272 for (i
= 0; i
< CS_MAX_CMDS
; i
++) {
273 msg
= hsi_alloc_msg(1, GFP_KERNEL
);
276 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
281 sg_init_one(msg
->sgt
.sgl
, buf
, sizeof(*buf
));
282 msg
->channel
= cs_char_data
.channel_id_cmd
;
284 list_add_tail(&msg
->link
, &hi
->cmdqueue
);
294 static void cs_hsi_data_destructor(struct hsi_msg
*msg
)
296 struct cs_hsi_iface
*hi
= msg
->context
;
297 const char *dir
= (msg
->ttype
== HSI_MSG_READ
) ? "TX" : "RX";
299 dev_dbg(&cs_char_data
.cl
->device
, "Freeing data %s message\n", dir
);
301 spin_lock(&hi
->lock
);
302 if (hi
->iface_state
!= CS_STATE_CLOSED
)
303 dev_err(&cs_char_data
.cl
->device
,
304 "Data %s flush while device active\n", dir
);
305 if (msg
->ttype
== HSI_MSG_READ
)
307 ~(SSI_CHANNEL_STATE_POLL
| SSI_CHANNEL_STATE_READING
);
309 hi
->data_state
&= ~SSI_CHANNEL_STATE_WRITING
;
311 msg
->status
= HSI_STATUS_COMPLETED
;
312 if (unlikely(waitqueue_active(&hi
->datawait
)))
313 wake_up_interruptible(&hi
->datawait
);
315 spin_unlock(&hi
->lock
);
318 static int cs_hsi_alloc_data(struct cs_hsi_iface
*hi
)
320 struct hsi_msg
*txmsg
, *rxmsg
;
323 rxmsg
= hsi_alloc_msg(1, GFP_KERNEL
);
328 rxmsg
->channel
= cs_char_data
.channel_id_data
;
329 rxmsg
->destructor
= cs_hsi_data_destructor
;
332 txmsg
= hsi_alloc_msg(1, GFP_KERNEL
);
337 txmsg
->channel
= cs_char_data
.channel_id_data
;
338 txmsg
->destructor
= cs_hsi_data_destructor
;
341 hi
->data_rx_msg
= rxmsg
;
342 hi
->data_tx_msg
= txmsg
;
352 static void cs_hsi_free_data_msg(struct hsi_msg
*msg
)
354 WARN_ON(msg
->status
!= HSI_STATUS_COMPLETED
&&
355 msg
->status
!= HSI_STATUS_ERROR
);
359 static void cs_hsi_free_data(struct cs_hsi_iface
*hi
)
361 cs_hsi_free_data_msg(hi
->data_rx_msg
);
362 cs_hsi_free_data_msg(hi
->data_tx_msg
);
365 static inline void __cs_hsi_error_pre(struct cs_hsi_iface
*hi
,
366 struct hsi_msg
*msg
, const char *info
,
369 spin_lock(&hi
->lock
);
370 dev_err(&hi
->cl
->device
, "HSI %s error, msg %d, state %u\n",
371 info
, msg
->status
, *state
);
374 static inline void __cs_hsi_error_post(struct cs_hsi_iface
*hi
)
376 spin_unlock(&hi
->lock
);
379 static inline void __cs_hsi_error_read_bits(unsigned int *state
)
381 *state
|= SSI_CHANNEL_STATE_ERROR
;
382 *state
&= ~(SSI_CHANNEL_STATE_READING
| SSI_CHANNEL_STATE_POLL
);
385 static inline void __cs_hsi_error_write_bits(unsigned int *state
)
387 *state
|= SSI_CHANNEL_STATE_ERROR
;
388 *state
&= ~SSI_CHANNEL_STATE_WRITING
;
391 static void cs_hsi_control_read_error(struct cs_hsi_iface
*hi
,
394 __cs_hsi_error_pre(hi
, msg
, "control read", &hi
->control_state
);
396 __cs_hsi_error_read_bits(&hi
->control_state
);
397 __cs_hsi_error_post(hi
);
400 static void cs_hsi_control_write_error(struct cs_hsi_iface
*hi
,
403 __cs_hsi_error_pre(hi
, msg
, "control write", &hi
->control_state
);
405 __cs_hsi_error_write_bits(&hi
->control_state
);
406 __cs_hsi_error_post(hi
);
410 static void cs_hsi_data_read_error(struct cs_hsi_iface
*hi
, struct hsi_msg
*msg
)
412 __cs_hsi_error_pre(hi
, msg
, "data read", &hi
->data_state
);
413 __cs_hsi_error_read_bits(&hi
->data_state
);
414 __cs_hsi_error_post(hi
);
417 static void cs_hsi_data_write_error(struct cs_hsi_iface
*hi
,
420 __cs_hsi_error_pre(hi
, msg
, "data write", &hi
->data_state
);
421 __cs_hsi_error_write_bits(&hi
->data_state
);
422 __cs_hsi_error_post(hi
);
425 static void cs_hsi_read_on_control_complete(struct hsi_msg
*msg
)
427 u32 cmd
= cs_get_cmd(msg
);
428 struct cs_hsi_iface
*hi
= msg
->context
;
430 spin_lock(&hi
->lock
);
431 hi
->control_state
&= ~SSI_CHANNEL_STATE_READING
;
432 if (msg
->status
== HSI_STATUS_ERROR
) {
433 dev_err(&hi
->cl
->device
, "Control RX error detected\n");
434 spin_unlock(&hi
->lock
);
435 cs_hsi_control_read_error(hi
, msg
);
438 dev_dbg(&hi
->cl
->device
, "Read on control: %08X\n", cmd
);
440 if (hi
->flags
& CS_FEAT_TSTAMP_RX_CTRL
) {
441 struct timespec64 tspec
;
442 struct cs_timestamp
*tstamp
=
443 &hi
->mmap_cfg
->tstamp_rx_ctrl
;
445 ktime_get_ts64(&tspec
);
447 tstamp
->tv_sec
= (__u32
) tspec
.tv_sec
;
448 tstamp
->tv_nsec
= (__u32
) tspec
.tv_nsec
;
450 spin_unlock(&hi
->lock
);
452 cs_notify_control(cmd
);
455 cs_hsi_read_on_control(hi
);
458 static void cs_hsi_peek_on_control_complete(struct hsi_msg
*msg
)
460 struct cs_hsi_iface
*hi
= msg
->context
;
463 if (msg
->status
== HSI_STATUS_ERROR
) {
464 dev_err(&hi
->cl
->device
, "Control peek RX error detected\n");
465 cs_hsi_control_read_error(hi
, msg
);
469 WARN_ON(!(hi
->control_state
& SSI_CHANNEL_STATE_READING
));
471 dev_dbg(&hi
->cl
->device
, "Peek on control complete, reading\n");
473 msg
->complete
= cs_hsi_read_on_control_complete
;
474 ret
= hsi_async_read(hi
->cl
, msg
);
476 cs_hsi_control_read_error(hi
, msg
);
479 static void cs_hsi_read_on_control(struct cs_hsi_iface
*hi
)
484 spin_lock(&hi
->lock
);
485 if (hi
->control_state
& SSI_CHANNEL_STATE_READING
) {
486 dev_err(&hi
->cl
->device
, "Control read already pending (%d)\n",
488 spin_unlock(&hi
->lock
);
491 if (hi
->control_state
& SSI_CHANNEL_STATE_ERROR
) {
492 dev_err(&hi
->cl
->device
, "Control read error (%d)\n",
494 spin_unlock(&hi
->lock
);
497 hi
->control_state
|= SSI_CHANNEL_STATE_READING
;
498 dev_dbg(&hi
->cl
->device
, "Issuing RX on control\n");
499 msg
= cs_claim_cmd(hi
);
500 spin_unlock(&hi
->lock
);
503 msg
->complete
= cs_hsi_peek_on_control_complete
;
504 ret
= hsi_async_read(hi
->cl
, msg
);
506 cs_hsi_control_read_error(hi
, msg
);
509 static void cs_hsi_write_on_control_complete(struct hsi_msg
*msg
)
511 struct cs_hsi_iface
*hi
= msg
->context
;
512 if (msg
->status
== HSI_STATUS_COMPLETED
) {
513 spin_lock(&hi
->lock
);
514 hi
->control_state
&= ~SSI_CHANNEL_STATE_WRITING
;
516 spin_unlock(&hi
->lock
);
517 } else if (msg
->status
== HSI_STATUS_ERROR
) {
518 cs_hsi_control_write_error(hi
, msg
);
520 dev_err(&hi
->cl
->device
,
521 "unexpected status in control write callback %d\n",
526 static int cs_hsi_write_on_control(struct cs_hsi_iface
*hi
, u32 message
)
531 spin_lock(&hi
->lock
);
532 if (hi
->control_state
& SSI_CHANNEL_STATE_ERROR
) {
533 spin_unlock(&hi
->lock
);
536 if (hi
->control_state
& SSI_CHANNEL_STATE_WRITING
) {
537 dev_err(&hi
->cl
->device
,
538 "Write still pending on control channel.\n");
539 spin_unlock(&hi
->lock
);
542 hi
->control_state
|= SSI_CHANNEL_STATE_WRITING
;
543 msg
= cs_claim_cmd(hi
);
544 spin_unlock(&hi
->lock
);
546 cs_set_cmd(msg
, message
);
548 msg
->complete
= cs_hsi_write_on_control_complete
;
549 dev_dbg(&hi
->cl
->device
,
550 "Sending control message %08X\n", message
);
551 ret
= hsi_async_write(hi
->cl
, msg
);
553 dev_err(&hi
->cl
->device
,
554 "async_write failed with %d\n", ret
);
555 cs_hsi_control_write_error(hi
, msg
);
559 * Make sure control read is always pending when issuing
560 * new control writes. This is needed as the controller
561 * may flush our messages if e.g. the peer device reboots
562 * unexpectedly (and we cannot directly resubmit a new read from
563 * the message destructor; see cs_cmd_destructor()).
565 if (!(hi
->control_state
& SSI_CHANNEL_STATE_READING
)) {
566 dev_err(&hi
->cl
->device
, "Restarting control reads\n");
567 cs_hsi_read_on_control(hi
);
573 static void cs_hsi_read_on_data_complete(struct hsi_msg
*msg
)
575 struct cs_hsi_iface
*hi
= msg
->context
;
578 if (unlikely(msg
->status
== HSI_STATUS_ERROR
)) {
579 cs_hsi_data_read_error(hi
, msg
);
583 spin_lock(&hi
->lock
);
584 WARN_ON(!(hi
->data_state
& SSI_CHANNEL_STATE_READING
));
585 hi
->data_state
&= ~SSI_CHANNEL_STATE_READING
;
586 payload
= CS_RX_DATA_RECEIVED
;
587 payload
|= hi
->rx_slot
;
589 hi
->rx_slot
%= hi
->rx_ptr_boundary
;
590 /* expose current rx ptr in mmap area */
591 hi
->mmap_cfg
->rx_ptr
= hi
->rx_slot
;
592 if (unlikely(waitqueue_active(&hi
->datawait
)))
593 wake_up_interruptible(&hi
->datawait
);
594 spin_unlock(&hi
->lock
);
596 cs_notify_data(payload
, hi
->rx_bufs
);
597 cs_hsi_read_on_data(hi
);
600 static void cs_hsi_peek_on_data_complete(struct hsi_msg
*msg
)
602 struct cs_hsi_iface
*hi
= msg
->context
;
606 if (unlikely(msg
->status
== HSI_STATUS_ERROR
)) {
607 cs_hsi_data_read_error(hi
, msg
);
610 if (unlikely(hi
->iface_state
!= CS_STATE_CONFIGURED
)) {
611 dev_err(&hi
->cl
->device
, "Data received in invalid state\n");
612 cs_hsi_data_read_error(hi
, msg
);
616 spin_lock(&hi
->lock
);
617 WARN_ON(!(hi
->data_state
& SSI_CHANNEL_STATE_POLL
));
618 hi
->data_state
&= ~SSI_CHANNEL_STATE_POLL
;
619 hi
->data_state
|= SSI_CHANNEL_STATE_READING
;
620 spin_unlock(&hi
->lock
);
622 address
= (u32
*)(hi
->mmap_base
+
623 hi
->rx_offsets
[hi
->rx_slot
% hi
->rx_bufs
]);
624 sg_init_one(msg
->sgt
.sgl
, address
, hi
->buf_size
);
626 msg
->complete
= cs_hsi_read_on_data_complete
;
627 ret
= hsi_async_read(hi
->cl
, msg
);
629 cs_hsi_data_read_error(hi
, msg
);
633 * Read/write transaction is ongoing. Returns false if in
634 * SSI_CHANNEL_STATE_POLL state.
636 static inline int cs_state_xfer_active(unsigned int state
)
638 return (state
& SSI_CHANNEL_STATE_WRITING
) ||
639 (state
& SSI_CHANNEL_STATE_READING
);
643 * No pending read/writes
645 static inline int cs_state_idle(unsigned int state
)
647 return !(state
& ~SSI_CHANNEL_STATE_ERROR
);
650 static void cs_hsi_read_on_data(struct cs_hsi_iface
*hi
)
652 struct hsi_msg
*rxmsg
;
655 spin_lock(&hi
->lock
);
657 (SSI_CHANNEL_STATE_READING
| SSI_CHANNEL_STATE_POLL
)) {
658 dev_dbg(&hi
->cl
->device
, "Data read already pending (%u)\n",
660 spin_unlock(&hi
->lock
);
663 hi
->data_state
|= SSI_CHANNEL_STATE_POLL
;
664 spin_unlock(&hi
->lock
);
666 rxmsg
= hi
->data_rx_msg
;
667 sg_init_one(rxmsg
->sgt
.sgl
, (void *)hi
->mmap_base
, 0);
668 rxmsg
->sgt
.nents
= 0;
669 rxmsg
->complete
= cs_hsi_peek_on_data_complete
;
671 ret
= hsi_async_read(hi
->cl
, rxmsg
);
673 cs_hsi_data_read_error(hi
, rxmsg
);
676 static void cs_hsi_write_on_data_complete(struct hsi_msg
*msg
)
678 struct cs_hsi_iface
*hi
= msg
->context
;
680 if (msg
->status
== HSI_STATUS_COMPLETED
) {
681 spin_lock(&hi
->lock
);
682 hi
->data_state
&= ~SSI_CHANNEL_STATE_WRITING
;
683 if (unlikely(waitqueue_active(&hi
->datawait
)))
684 wake_up_interruptible(&hi
->datawait
);
685 spin_unlock(&hi
->lock
);
687 cs_hsi_data_write_error(hi
, msg
);
691 static int cs_hsi_write_on_data(struct cs_hsi_iface
*hi
, unsigned int slot
)
694 struct hsi_msg
*txmsg
;
697 spin_lock(&hi
->lock
);
698 if (hi
->iface_state
!= CS_STATE_CONFIGURED
) {
699 dev_err(&hi
->cl
->device
, "Not configured, aborting\n");
703 if (hi
->data_state
& SSI_CHANNEL_STATE_ERROR
) {
704 dev_err(&hi
->cl
->device
, "HSI error, aborting\n");
708 if (hi
->data_state
& SSI_CHANNEL_STATE_WRITING
) {
709 dev_err(&hi
->cl
->device
, "Write pending on data channel.\n");
713 hi
->data_state
|= SSI_CHANNEL_STATE_WRITING
;
714 spin_unlock(&hi
->lock
);
717 address
= (u32
*)(hi
->mmap_base
+ hi
->tx_offsets
[hi
->tx_slot
]);
718 txmsg
= hi
->data_tx_msg
;
719 sg_init_one(txmsg
->sgt
.sgl
, address
, hi
->buf_size
);
720 txmsg
->complete
= cs_hsi_write_on_data_complete
;
721 ret
= hsi_async_write(hi
->cl
, txmsg
);
723 cs_hsi_data_write_error(hi
, txmsg
);
728 spin_unlock(&hi
->lock
);
730 cs_hsi_data_write_error(hi
, hi
->data_tx_msg
);
735 static unsigned int cs_hsi_get_state(struct cs_hsi_iface
*hi
)
737 return hi
->iface_state
;
740 static int cs_hsi_command(struct cs_hsi_iface
*hi
, u32 cmd
)
745 switch (cmd
& TARGET_MASK
) {
747 ret
= cs_hsi_write_on_control(hi
, cmd
);
750 if ((cmd
& CS_CMD_MASK
) == CS_TX_DATA_READY
)
751 ret
= cs_hsi_write_on_data(hi
, cmd
& CS_PARAM_MASK
);
764 static void cs_hsi_set_wakeline(struct cs_hsi_iface
*hi
, bool new_state
)
768 spin_lock_bh(&hi
->lock
);
769 if (hi
->wakeline_state
!= new_state
) {
770 hi
->wakeline_state
= new_state
;
772 dev_dbg(&hi
->cl
->device
, "setting wake line to %d (%p)\n",
775 spin_unlock_bh(&hi
->lock
);
779 ssip_slave_start_tx(hi
->master
);
781 ssip_slave_stop_tx(hi
->master
);
784 dev_dbg(&hi
->cl
->device
, "wake line set to %d (%p)\n",
788 static void set_buffer_sizes(struct cs_hsi_iface
*hi
, int rx_bufs
, int tx_bufs
)
790 hi
->rx_bufs
= rx_bufs
;
791 hi
->tx_bufs
= tx_bufs
;
792 hi
->mmap_cfg
->rx_bufs
= rx_bufs
;
793 hi
->mmap_cfg
->tx_bufs
= tx_bufs
;
795 if (hi
->flags
& CS_FEAT_ROLLING_RX_COUNTER
) {
797 * For more robust overrun detection, let the rx
798 * pointer run in range 0..'boundary-1'. Boundary
799 * is a multiple of rx_bufs, and limited in max size
800 * by RX_PTR_MAX_SHIFT to allow for fast ptr-diff
803 hi
->rx_ptr_boundary
= (rx_bufs
<< RX_PTR_BOUNDARY_SHIFT
);
804 hi
->mmap_cfg
->rx_ptr_boundary
= hi
->rx_ptr_boundary
;
806 hi
->rx_ptr_boundary
= hi
->rx_bufs
;
810 static int check_buf_params(struct cs_hsi_iface
*hi
,
811 const struct cs_buffer_config
*buf_cfg
)
813 size_t buf_size_aligned
= L1_CACHE_ALIGN(buf_cfg
->buf_size
) *
814 (buf_cfg
->rx_bufs
+ buf_cfg
->tx_bufs
);
815 size_t ctrl_size_aligned
= L1_CACHE_ALIGN(sizeof(*hi
->mmap_cfg
));
818 if (buf_cfg
->rx_bufs
> CS_MAX_BUFFERS
||
819 buf_cfg
->tx_bufs
> CS_MAX_BUFFERS
) {
821 } else if ((buf_size_aligned
+ ctrl_size_aligned
) >= hi
->mmap_size
) {
822 dev_err(&hi
->cl
->device
, "No space for the requested buffer "
831 * Block until pending data transfers have completed.
833 static int cs_hsi_data_sync(struct cs_hsi_iface
*hi
)
837 spin_lock_bh(&hi
->lock
);
839 if (!cs_state_xfer_active(hi
->data_state
)) {
840 dev_dbg(&hi
->cl
->device
, "hsi_data_sync break, idle\n");
847 if (!cs_state_xfer_active(hi
->data_state
))
849 if (signal_pending(current
)) {
854 * prepare_to_wait must be called with hi->lock held
855 * so that callbacks can check for waitqueue_active()
857 prepare_to_wait(&hi
->datawait
, &wait
, TASK_INTERRUPTIBLE
);
858 spin_unlock_bh(&hi
->lock
);
859 s
= schedule_timeout(
860 msecs_to_jiffies(CS_HSI_TRANSFER_TIMEOUT_MS
));
861 spin_lock_bh(&hi
->lock
);
862 finish_wait(&hi
->datawait
, &wait
);
864 dev_dbg(&hi
->cl
->device
,
865 "hsi_data_sync timeout after %d ms\n",
866 CS_HSI_TRANSFER_TIMEOUT_MS
);
873 spin_unlock_bh(&hi
->lock
);
874 dev_dbg(&hi
->cl
->device
, "hsi_data_sync done with res %d\n", r
);
879 static void cs_hsi_data_enable(struct cs_hsi_iface
*hi
,
880 struct cs_buffer_config
*buf_cfg
)
882 unsigned int data_start
, i
;
884 BUG_ON(hi
->buf_size
== 0);
886 set_buffer_sizes(hi
, buf_cfg
->rx_bufs
, buf_cfg
->tx_bufs
);
888 hi
->slot_size
= L1_CACHE_ALIGN(hi
->buf_size
);
889 dev_dbg(&hi
->cl
->device
,
890 "setting slot size to %u, buf size %u, align %u\n",
891 hi
->slot_size
, hi
->buf_size
, L1_CACHE_BYTES
);
893 data_start
= L1_CACHE_ALIGN(sizeof(*hi
->mmap_cfg
));
894 dev_dbg(&hi
->cl
->device
,
895 "setting data start at %u, cfg block %u, align %u\n",
896 data_start
, sizeof(*hi
->mmap_cfg
), L1_CACHE_BYTES
);
898 for (i
= 0; i
< hi
->mmap_cfg
->rx_bufs
; i
++) {
899 hi
->rx_offsets
[i
] = data_start
+ i
* hi
->slot_size
;
900 hi
->mmap_cfg
->rx_offsets
[i
] = hi
->rx_offsets
[i
];
901 dev_dbg(&hi
->cl
->device
, "DL buf #%u at %u\n",
902 i
, hi
->rx_offsets
[i
]);
904 for (i
= 0; i
< hi
->mmap_cfg
->tx_bufs
; i
++) {
905 hi
->tx_offsets
[i
] = data_start
+
906 (i
+ hi
->mmap_cfg
->rx_bufs
) * hi
->slot_size
;
907 hi
->mmap_cfg
->tx_offsets
[i
] = hi
->tx_offsets
[i
];
908 dev_dbg(&hi
->cl
->device
, "UL buf #%u at %u\n",
909 i
, hi
->rx_offsets
[i
]);
912 hi
->iface_state
= CS_STATE_CONFIGURED
;
915 static void cs_hsi_data_disable(struct cs_hsi_iface
*hi
, int old_state
)
917 if (old_state
== CS_STATE_CONFIGURED
) {
918 dev_dbg(&hi
->cl
->device
,
919 "closing data channel with slot size 0\n");
920 hi
->iface_state
= CS_STATE_OPENED
;
924 static int cs_hsi_buf_config(struct cs_hsi_iface
*hi
,
925 struct cs_buffer_config
*buf_cfg
)
928 unsigned int old_state
= hi
->iface_state
;
930 spin_lock_bh(&hi
->lock
);
931 /* Prevent new transactions during buffer reconfig */
932 if (old_state
== CS_STATE_CONFIGURED
)
933 hi
->iface_state
= CS_STATE_OPENED
;
934 spin_unlock_bh(&hi
->lock
);
937 * make sure that no non-zero data reads are ongoing before
938 * proceeding to change the buffer layout
940 r
= cs_hsi_data_sync(hi
);
944 WARN_ON(cs_state_xfer_active(hi
->data_state
));
946 spin_lock_bh(&hi
->lock
);
947 r
= check_buf_params(hi
, buf_cfg
);
951 hi
->buf_size
= buf_cfg
->buf_size
;
952 hi
->mmap_cfg
->buf_size
= hi
->buf_size
;
953 hi
->flags
= buf_cfg
->flags
;
960 cs_hsi_data_enable(hi
, buf_cfg
);
962 cs_hsi_data_disable(hi
, old_state
);
964 spin_unlock_bh(&hi
->lock
);
966 if (old_state
!= hi
->iface_state
) {
967 if (hi
->iface_state
== CS_STATE_CONFIGURED
) {
968 cpu_latency_qos_add_request(&hi
->pm_qos_req
,
969 CS_QOS_LATENCY_FOR_DATA_USEC
);
971 cs_hsi_read_on_data(hi
);
973 } else if (old_state
== CS_STATE_CONFIGURED
) {
974 cpu_latency_qos_remove_request(&hi
->pm_qos_req
);
980 spin_unlock_bh(&hi
->lock
);
984 static int cs_hsi_start(struct cs_hsi_iface
**hi
, struct hsi_client
*cl
,
985 unsigned long mmap_base
, unsigned long mmap_size
)
988 struct cs_hsi_iface
*hsi_if
= kzalloc(sizeof(*hsi_if
), GFP_KERNEL
);
990 dev_dbg(&cl
->device
, "cs_hsi_start\n");
996 spin_lock_init(&hsi_if
->lock
);
998 hsi_if
->iface_state
= CS_STATE_CLOSED
;
999 hsi_if
->mmap_cfg
= (struct cs_mmap_config_block
*)mmap_base
;
1000 hsi_if
->mmap_base
= mmap_base
;
1001 hsi_if
->mmap_size
= mmap_size
;
1002 memset(hsi_if
->mmap_cfg
, 0, sizeof(*hsi_if
->mmap_cfg
));
1003 init_waitqueue_head(&hsi_if
->datawait
);
1004 err
= cs_alloc_cmds(hsi_if
);
1006 dev_err(&cl
->device
, "Unable to alloc HSI messages\n");
1009 err
= cs_hsi_alloc_data(hsi_if
);
1011 dev_err(&cl
->device
, "Unable to alloc HSI messages for data\n");
1014 err
= hsi_claim_port(cl
, 1);
1016 dev_err(&cl
->device
,
1017 "Could not open, HSI port already claimed\n");
1020 hsi_if
->master
= ssip_slave_get_master(cl
);
1021 if (IS_ERR(hsi_if
->master
)) {
1022 err
= PTR_ERR(hsi_if
->master
);
1023 dev_err(&cl
->device
, "Could not get HSI master client\n");
1026 if (!ssip_slave_running(hsi_if
->master
)) {
1028 dev_err(&cl
->device
,
1029 "HSI port not initialized\n");
1033 hsi_if
->iface_state
= CS_STATE_OPENED
;
1035 cs_hsi_read_on_control(hsi_if
);
1038 dev_dbg(&cl
->device
, "cs_hsi_start...done\n");
1046 hsi_release_port(cl
);
1048 cs_hsi_free_data(hsi_if
);
1050 cs_free_cmds(hsi_if
);
1054 dev_dbg(&cl
->device
, "cs_hsi_start...done/error\n\n");
1059 static void cs_hsi_stop(struct cs_hsi_iface
*hi
)
1061 dev_dbg(&hi
->cl
->device
, "cs_hsi_stop\n");
1062 cs_hsi_set_wakeline(hi
, 0);
1063 ssip_slave_put_master(hi
->master
);
1065 /* hsi_release_port() needs to be called with CS_STATE_CLOSED */
1066 hi
->iface_state
= CS_STATE_CLOSED
;
1067 hsi_release_port(hi
->cl
);
1070 * hsi_release_port() should flush out all the pending
1071 * messages, so cs_state_idle() should be true for both
1072 * control and data channels.
1074 WARN_ON(!cs_state_idle(hi
->control_state
));
1075 WARN_ON(!cs_state_idle(hi
->data_state
));
1077 if (cpu_latency_qos_request_active(&hi
->pm_qos_req
))
1078 cpu_latency_qos_remove_request(&hi
->pm_qos_req
);
1080 spin_lock_bh(&hi
->lock
);
1081 cs_hsi_free_data(hi
);
1083 spin_unlock_bh(&hi
->lock
);
1087 static vm_fault_t
cs_char_vma_fault(struct vm_fault
*vmf
)
1089 struct cs_char
*csdata
= vmf
->vma
->vm_private_data
;
1092 page
= virt_to_page(csdata
->mmap_base
);
1099 static const struct vm_operations_struct cs_char_vm_ops
= {
1100 .fault
= cs_char_vma_fault
,
1103 static int cs_char_fasync(int fd
, struct file
*file
, int on
)
1105 struct cs_char
*csdata
= file
->private_data
;
1107 if (fasync_helper(fd
, file
, on
, &csdata
->async_queue
) < 0)
1113 static __poll_t
cs_char_poll(struct file
*file
, poll_table
*wait
)
1115 struct cs_char
*csdata
= file
->private_data
;
1118 poll_wait(file
, &cs_char_data
.wait
, wait
);
1119 spin_lock_bh(&csdata
->lock
);
1120 if (!list_empty(&csdata
->chardev_queue
))
1121 ret
= EPOLLIN
| EPOLLRDNORM
;
1122 else if (!list_empty(&csdata
->dataind_queue
))
1123 ret
= EPOLLIN
| EPOLLRDNORM
;
1124 spin_unlock_bh(&csdata
->lock
);
1129 static ssize_t
cs_char_read(struct file
*file
, char __user
*buf
, size_t count
,
1132 struct cs_char
*csdata
= file
->private_data
;
1136 if (count
< sizeof(data
))
1142 spin_lock_bh(&csdata
->lock
);
1143 if (!list_empty(&csdata
->chardev_queue
)) {
1144 data
= cs_pop_entry(&csdata
->chardev_queue
);
1145 } else if (!list_empty(&csdata
->dataind_queue
)) {
1146 data
= cs_pop_entry(&csdata
->dataind_queue
);
1147 csdata
->dataind_pending
--;
1151 spin_unlock_bh(&csdata
->lock
);
1155 if (file
->f_flags
& O_NONBLOCK
) {
1158 } else if (signal_pending(current
)) {
1159 retval
= -ERESTARTSYS
;
1162 prepare_to_wait_exclusive(&csdata
->wait
, &wait
,
1163 TASK_INTERRUPTIBLE
);
1165 finish_wait(&csdata
->wait
, &wait
);
1168 retval
= put_user(data
, (u32 __user
*)buf
);
1170 retval
= sizeof(data
);
1176 static ssize_t
cs_char_write(struct file
*file
, const char __user
*buf
,
1177 size_t count
, loff_t
*unused
)
1179 struct cs_char
*csdata
= file
->private_data
;
1184 if (count
< sizeof(data
))
1187 if (get_user(data
, (u32 __user
*)buf
))
1192 err
= cs_hsi_command(csdata
->hi
, data
);
1199 static long cs_char_ioctl(struct file
*file
, unsigned int cmd
,
1202 struct cs_char
*csdata
= file
->private_data
;
1206 case CS_GET_STATE
: {
1209 state
= cs_hsi_get_state(csdata
->hi
);
1210 if (copy_to_user((void __user
*)arg
, &state
, sizeof(state
)))
1215 case CS_SET_WAKELINE
: {
1218 if (copy_from_user(&state
, (void __user
*)arg
, sizeof(state
))) {
1228 cs_hsi_set_wakeline(csdata
->hi
, !!state
);
1232 case CS_GET_IF_VERSION
: {
1233 unsigned int ifver
= CS_IF_VERSION
;
1235 if (copy_to_user((void __user
*)arg
, &ifver
, sizeof(ifver
)))
1240 case CS_CONFIG_BUFS
: {
1241 struct cs_buffer_config buf_cfg
;
1243 if (copy_from_user(&buf_cfg
, (void __user
*)arg
,
1247 r
= cs_hsi_buf_config(csdata
->hi
, &buf_cfg
);
1259 static int cs_char_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1261 if (vma
->vm_end
< vma
->vm_start
)
1264 if (vma_pages(vma
) != 1)
1267 vma
->vm_flags
|= VM_IO
| VM_DONTDUMP
| VM_DONTEXPAND
;
1268 vma
->vm_ops
= &cs_char_vm_ops
;
1269 vma
->vm_private_data
= file
->private_data
;
1274 static int cs_char_open(struct inode
*unused
, struct file
*file
)
1279 spin_lock_bh(&cs_char_data
.lock
);
1280 if (cs_char_data
.opened
) {
1282 spin_unlock_bh(&cs_char_data
.lock
);
1285 cs_char_data
.opened
= 1;
1286 cs_char_data
.dataind_pending
= 0;
1287 spin_unlock_bh(&cs_char_data
.lock
);
1289 p
= get_zeroed_page(GFP_KERNEL
);
1295 ret
= cs_hsi_start(&cs_char_data
.hi
, cs_char_data
.cl
, p
, CS_MMAP_SIZE
);
1297 dev_err(&cs_char_data
.cl
->device
, "Unable to initialize HSI\n");
1301 /* these are only used in release so lock not needed */
1302 cs_char_data
.mmap_base
= p
;
1303 cs_char_data
.mmap_size
= CS_MMAP_SIZE
;
1305 file
->private_data
= &cs_char_data
;
1312 spin_lock_bh(&cs_char_data
.lock
);
1313 cs_char_data
.opened
= 0;
1314 spin_unlock_bh(&cs_char_data
.lock
);
1319 static void cs_free_char_queue(struct list_head
*head
)
1321 struct char_queue
*entry
;
1322 struct list_head
*cursor
, *next
;
1324 if (!list_empty(head
)) {
1325 list_for_each_safe(cursor
, next
, head
) {
1326 entry
= list_entry(cursor
, struct char_queue
, list
);
1327 list_del(&entry
->list
);
1334 static int cs_char_release(struct inode
*unused
, struct file
*file
)
1336 struct cs_char
*csdata
= file
->private_data
;
1338 cs_hsi_stop(csdata
->hi
);
1339 spin_lock_bh(&csdata
->lock
);
1341 free_page(csdata
->mmap_base
);
1342 cs_free_char_queue(&csdata
->chardev_queue
);
1343 cs_free_char_queue(&csdata
->dataind_queue
);
1345 spin_unlock_bh(&csdata
->lock
);
1350 static const struct file_operations cs_char_fops
= {
1351 .owner
= THIS_MODULE
,
1352 .read
= cs_char_read
,
1353 .write
= cs_char_write
,
1354 .poll
= cs_char_poll
,
1355 .unlocked_ioctl
= cs_char_ioctl
,
1356 .mmap
= cs_char_mmap
,
1357 .open
= cs_char_open
,
1358 .release
= cs_char_release
,
1359 .fasync
= cs_char_fasync
,
1362 static struct miscdevice cs_char_miscdev
= {
1363 .minor
= MISC_DYNAMIC_MINOR
,
1364 .name
= "cmt_speech",
1365 .fops
= &cs_char_fops
1368 static int cs_hsi_client_probe(struct device
*dev
)
1371 struct hsi_client
*cl
= to_hsi_client(dev
);
1373 dev_dbg(dev
, "hsi_client_probe\n");
1374 init_waitqueue_head(&cs_char_data
.wait
);
1375 spin_lock_init(&cs_char_data
.lock
);
1376 cs_char_data
.opened
= 0;
1377 cs_char_data
.cl
= cl
;
1378 cs_char_data
.hi
= NULL
;
1379 INIT_LIST_HEAD(&cs_char_data
.chardev_queue
);
1380 INIT_LIST_HEAD(&cs_char_data
.dataind_queue
);
1382 cs_char_data
.channel_id_cmd
= hsi_get_channel_id_by_name(cl
,
1384 if (cs_char_data
.channel_id_cmd
< 0) {
1385 err
= cs_char_data
.channel_id_cmd
;
1386 dev_err(dev
, "Could not get cmd channel (%d)\n", err
);
1390 cs_char_data
.channel_id_data
= hsi_get_channel_id_by_name(cl
,
1392 if (cs_char_data
.channel_id_data
< 0) {
1393 err
= cs_char_data
.channel_id_data
;
1394 dev_err(dev
, "Could not get data channel (%d)\n", err
);
1398 err
= misc_register(&cs_char_miscdev
);
1400 dev_err(dev
, "Failed to register: %d\n", err
);
1405 static int cs_hsi_client_remove(struct device
*dev
)
1407 struct cs_hsi_iface
*hi
;
1409 dev_dbg(dev
, "hsi_client_remove\n");
1410 misc_deregister(&cs_char_miscdev
);
1411 spin_lock_bh(&cs_char_data
.lock
);
1412 hi
= cs_char_data
.hi
;
1413 cs_char_data
.hi
= NULL
;
1414 spin_unlock_bh(&cs_char_data
.lock
);
1421 static struct hsi_client_driver cs_hsi_driver
= {
1423 .name
= "cmt-speech",
1424 .owner
= THIS_MODULE
,
1425 .probe
= cs_hsi_client_probe
,
1426 .remove
= cs_hsi_client_remove
,
1430 static int __init
cs_char_init(void)
1432 pr_info("CMT speech driver added\n");
1433 return hsi_register_client_driver(&cs_hsi_driver
);
1435 module_init(cs_char_init
);
1437 static void __exit
cs_char_exit(void)
1439 hsi_unregister_client_driver(&cs_hsi_driver
);
1440 pr_info("CMT speech driver removed\n");
1442 module_exit(cs_char_exit
);
1444 MODULE_ALIAS("hsi:cmt-speech");
1445 MODULE_AUTHOR("Kai Vehmanen <kai.vehmanen@nokia.com>");
1446 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
1447 MODULE_DESCRIPTION("CMT speech driver");
1448 MODULE_LICENSE("GPL v2");