WIP FPC-III support
[linux/fpc-iii.git] / drivers / hsi / clients / cmt_speech.c
blob89869c66fb9d0f68c42a8856cf4ad8c591def2f7
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
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>
9 */
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>
17 #include <linux/mm.h>
18 #include <linux/slab.h>
19 #include <linux/fs.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
31 struct char_queue {
32 struct list_head list;
33 u32 msg;
36 struct cs_char {
37 unsigned int opened;
38 struct hsi_client *cl;
39 struct cs_hsi_iface *hi;
40 struct list_head chardev_queue;
41 struct list_head dataind_queue;
42 int dataind_pending;
43 /* mmap things */
44 unsigned long mmap_base;
45 unsigned long mmap_size;
46 spinlock_t lock;
47 struct fasync_struct *async_queue;
48 wait_queue_head_t wait;
49 /* hsi channel ids */
50 int channel_id_cmd;
51 int channel_id_data;
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 */
64 #define CS_MAX_CMDS 4
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 + \
78 CS_MAX_BUFFERS_SHIFT)
79 struct cs_hsi_iface {
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;
94 unsigned int rx_slot;
95 unsigned int tx_slot;
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;
108 unsigned int flags;
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;
118 spinlock_t lock;
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);
139 goto out;
142 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
143 if (!entry) {
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);
147 goto out;
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);
158 out:
159 return;
162 static u32 cs_pop_entry(struct list_head *head)
164 struct char_queue *entry;
165 u32 data;
167 entry = list_entry(head->next, struct char_queue, list);
168 data = entry->msg;
169 list_del(&entry->list);
170 kfree(entry);
172 return data;
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);
200 *data = cmd;
203 static inline u32 cs_get_cmd(struct hsi_msg *msg)
205 u32 *data = sg_virt(msg->sgt.sgl);
206 return *data;
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)
228 hi->control_state &=
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;
234 cs_release_cmd(msg);
236 spin_unlock(&hi->lock);
239 static struct hsi_msg *cs_claim_cmd(struct cs_hsi_iface* ssi)
241 struct hsi_msg *msg;
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;
249 return msg;
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));
260 hsi_free_msg(msg);
264 static int cs_alloc_cmds(struct cs_hsi_iface *hi)
266 struct hsi_msg *msg;
267 u32 *buf;
268 unsigned int i;
270 INIT_LIST_HEAD(&hi->cmdqueue);
272 for (i = 0; i < CS_MAX_CMDS; i++) {
273 msg = hsi_alloc_msg(1, GFP_KERNEL);
274 if (!msg)
275 goto out;
276 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
277 if (!buf) {
278 hsi_free_msg(msg);
279 goto out;
281 sg_init_one(msg->sgt.sgl, buf, sizeof(*buf));
282 msg->channel = cs_char_data.channel_id_cmd;
283 msg->context = hi;
284 list_add_tail(&msg->link, &hi->cmdqueue);
287 return 0;
289 out:
290 cs_free_cmds(hi);
291 return -ENOMEM;
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)
306 hi->data_state &=
307 ~(SSI_CHANNEL_STATE_POLL | SSI_CHANNEL_STATE_READING);
308 else
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;
321 int res = 0;
323 rxmsg = hsi_alloc_msg(1, GFP_KERNEL);
324 if (!rxmsg) {
325 res = -ENOMEM;
326 goto out1;
328 rxmsg->channel = cs_char_data.channel_id_data;
329 rxmsg->destructor = cs_hsi_data_destructor;
330 rxmsg->context = hi;
332 txmsg = hsi_alloc_msg(1, GFP_KERNEL);
333 if (!txmsg) {
334 res = -ENOMEM;
335 goto out2;
337 txmsg->channel = cs_char_data.channel_id_data;
338 txmsg->destructor = cs_hsi_data_destructor;
339 txmsg->context = hi;
341 hi->data_rx_msg = rxmsg;
342 hi->data_tx_msg = txmsg;
344 return 0;
346 out2:
347 hsi_free_msg(rxmsg);
348 out1:
349 return res;
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);
356 hsi_free_msg(msg);
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,
367 unsigned int *state)
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,
392 struct hsi_msg *msg)
394 __cs_hsi_error_pre(hi, msg, "control read", &hi->control_state);
395 cs_release_cmd(msg);
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,
401 struct hsi_msg *msg)
403 __cs_hsi_error_pre(hi, msg, "control write", &hi->control_state);
404 cs_release_cmd(msg);
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,
418 struct hsi_msg *msg)
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);
436 goto out;
438 dev_dbg(&hi->cl->device, "Read on control: %08X\n", cmd);
439 cs_release_cmd(msg);
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);
454 out:
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;
461 int ret;
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);
466 return;
469 WARN_ON(!(hi->control_state & SSI_CHANNEL_STATE_READING));
471 dev_dbg(&hi->cl->device, "Peek on control complete, reading\n");
472 msg->sgt.nents = 1;
473 msg->complete = cs_hsi_read_on_control_complete;
474 ret = hsi_async_read(hi->cl, msg);
475 if (ret)
476 cs_hsi_control_read_error(hi, msg);
479 static void cs_hsi_read_on_control(struct cs_hsi_iface *hi)
481 struct hsi_msg *msg;
482 int ret;
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",
487 hi->control_state);
488 spin_unlock(&hi->lock);
489 return;
491 if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
492 dev_err(&hi->cl->device, "Control read error (%d)\n",
493 hi->control_state);
494 spin_unlock(&hi->lock);
495 return;
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);
502 msg->sgt.nents = 0;
503 msg->complete = cs_hsi_peek_on_control_complete;
504 ret = hsi_async_read(hi->cl, msg);
505 if (ret)
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;
515 cs_release_cmd(msg);
516 spin_unlock(&hi->lock);
517 } else if (msg->status == HSI_STATUS_ERROR) {
518 cs_hsi_control_write_error(hi, msg);
519 } else {
520 dev_err(&hi->cl->device,
521 "unexpected status in control write callback %d\n",
522 msg->status);
526 static int cs_hsi_write_on_control(struct cs_hsi_iface *hi, u32 message)
528 struct hsi_msg *msg;
529 int ret;
531 spin_lock(&hi->lock);
532 if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
533 spin_unlock(&hi->lock);
534 return -EIO;
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);
540 return -EBUSY;
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);
547 msg->sgt.nents = 1;
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);
552 if (ret) {
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);
570 return 0;
573 static void cs_hsi_read_on_data_complete(struct hsi_msg *msg)
575 struct cs_hsi_iface *hi = msg->context;
576 u32 payload;
578 if (unlikely(msg->status == HSI_STATUS_ERROR)) {
579 cs_hsi_data_read_error(hi, msg);
580 return;
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;
588 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;
603 u32 *address;
604 int ret;
606 if (unlikely(msg->status == HSI_STATUS_ERROR)) {
607 cs_hsi_data_read_error(hi, msg);
608 return;
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);
613 return;
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);
625 msg->sgt.nents = 1;
626 msg->complete = cs_hsi_read_on_data_complete;
627 ret = hsi_async_read(hi->cl, msg);
628 if (ret)
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;
653 int ret;
655 spin_lock(&hi->lock);
656 if (hi->data_state &
657 (SSI_CHANNEL_STATE_READING | SSI_CHANNEL_STATE_POLL)) {
658 dev_dbg(&hi->cl->device, "Data read already pending (%u)\n",
659 hi->data_state);
660 spin_unlock(&hi->lock);
661 return;
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);
672 if (ret)
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);
686 } else {
687 cs_hsi_data_write_error(hi, msg);
691 static int cs_hsi_write_on_data(struct cs_hsi_iface *hi, unsigned int slot)
693 u32 *address;
694 struct hsi_msg *txmsg;
695 int ret;
697 spin_lock(&hi->lock);
698 if (hi->iface_state != CS_STATE_CONFIGURED) {
699 dev_err(&hi->cl->device, "Not configured, aborting\n");
700 ret = -EINVAL;
701 goto error;
703 if (hi->data_state & SSI_CHANNEL_STATE_ERROR) {
704 dev_err(&hi->cl->device, "HSI error, aborting\n");
705 ret = -EIO;
706 goto error;
708 if (hi->data_state & SSI_CHANNEL_STATE_WRITING) {
709 dev_err(&hi->cl->device, "Write pending on data channel.\n");
710 ret = -EBUSY;
711 goto error;
713 hi->data_state |= SSI_CHANNEL_STATE_WRITING;
714 spin_unlock(&hi->lock);
716 hi->tx_slot = slot;
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);
722 if (ret)
723 cs_hsi_data_write_error(hi, txmsg);
725 return ret;
727 error:
728 spin_unlock(&hi->lock);
729 if (ret == -EIO)
730 cs_hsi_data_write_error(hi, hi->data_tx_msg);
732 return ret;
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)
742 int ret = 0;
744 local_bh_disable();
745 switch (cmd & TARGET_MASK) {
746 case TARGET_REMOTE:
747 ret = cs_hsi_write_on_control(hi, cmd);
748 break;
749 case TARGET_LOCAL:
750 if ((cmd & CS_CMD_MASK) == CS_TX_DATA_READY)
751 ret = cs_hsi_write_on_data(hi, cmd & CS_PARAM_MASK);
752 else
753 ret = -EINVAL;
754 break;
755 default:
756 ret = -EINVAL;
757 break;
759 local_bh_enable();
761 return ret;
764 static void cs_hsi_set_wakeline(struct cs_hsi_iface *hi, bool new_state)
766 int change = 0;
768 spin_lock_bh(&hi->lock);
769 if (hi->wakeline_state != new_state) {
770 hi->wakeline_state = new_state;
771 change = 1;
772 dev_dbg(&hi->cl->device, "setting wake line to %d (%p)\n",
773 new_state, hi->cl);
775 spin_unlock_bh(&hi->lock);
777 if (change) {
778 if (new_state)
779 ssip_slave_start_tx(hi->master);
780 else
781 ssip_slave_stop_tx(hi->master);
784 dev_dbg(&hi->cl->device, "wake line set to %d (%p)\n",
785 new_state, hi->cl);
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
801 * calculation.
803 hi->rx_ptr_boundary = (rx_bufs << RX_PTR_BOUNDARY_SHIFT);
804 hi->mmap_cfg->rx_ptr_boundary = hi->rx_ptr_boundary;
805 } else {
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));
816 int r = 0;
818 if (buf_cfg->rx_bufs > CS_MAX_BUFFERS ||
819 buf_cfg->tx_bufs > CS_MAX_BUFFERS) {
820 r = -EINVAL;
821 } else if ((buf_size_aligned + ctrl_size_aligned) >= hi->mmap_size) {
822 dev_err(&hi->cl->device, "No space for the requested buffer "
823 "configuration\n");
824 r = -ENOBUFS;
827 return r;
831 * Block until pending data transfers have completed.
833 static int cs_hsi_data_sync(struct cs_hsi_iface *hi)
835 int r = 0;
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");
841 goto out;
844 for (;;) {
845 int s;
846 DEFINE_WAIT(wait);
847 if (!cs_state_xfer_active(hi->data_state))
848 goto out;
849 if (signal_pending(current)) {
850 r = -ERESTARTSYS;
851 goto out;
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);
863 if (!s) {
864 dev_dbg(&hi->cl->device,
865 "hsi_data_sync timeout after %d ms\n",
866 CS_HSI_TRANSFER_TIMEOUT_MS);
867 r = -EIO;
868 goto out;
872 out:
873 spin_unlock_bh(&hi->lock);
874 dev_dbg(&hi->cl->device, "hsi_data_sync done with res %d\n", r);
876 return 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)
927 int r = 0;
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);
941 if (r < 0)
942 return r;
944 WARN_ON(cs_state_xfer_active(hi->data_state));
946 spin_lock_bh(&hi->lock);
947 r = check_buf_params(hi, buf_cfg);
948 if (r < 0)
949 goto error;
951 hi->buf_size = buf_cfg->buf_size;
952 hi->mmap_cfg->buf_size = hi->buf_size;
953 hi->flags = buf_cfg->flags;
955 hi->rx_slot = 0;
956 hi->tx_slot = 0;
957 hi->slot_size = 0;
959 if (hi->buf_size)
960 cs_hsi_data_enable(hi, buf_cfg);
961 else
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);
970 local_bh_disable();
971 cs_hsi_read_on_data(hi);
972 local_bh_enable();
973 } else if (old_state == CS_STATE_CONFIGURED) {
974 cpu_latency_qos_remove_request(&hi->pm_qos_req);
977 return r;
979 error:
980 spin_unlock_bh(&hi->lock);
981 return r;
984 static int cs_hsi_start(struct cs_hsi_iface **hi, struct hsi_client *cl,
985 unsigned long mmap_base, unsigned long mmap_size)
987 int err = 0;
988 struct cs_hsi_iface *hsi_if = kzalloc(sizeof(*hsi_if), GFP_KERNEL);
990 dev_dbg(&cl->device, "cs_hsi_start\n");
992 if (!hsi_if) {
993 err = -ENOMEM;
994 goto leave0;
996 spin_lock_init(&hsi_if->lock);
997 hsi_if->cl = cl;
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);
1005 if (err < 0) {
1006 dev_err(&cl->device, "Unable to alloc HSI messages\n");
1007 goto leave1;
1009 err = cs_hsi_alloc_data(hsi_if);
1010 if (err < 0) {
1011 dev_err(&cl->device, "Unable to alloc HSI messages for data\n");
1012 goto leave2;
1014 err = hsi_claim_port(cl, 1);
1015 if (err < 0) {
1016 dev_err(&cl->device,
1017 "Could not open, HSI port already claimed\n");
1018 goto leave3;
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");
1024 goto leave4;
1026 if (!ssip_slave_running(hsi_if->master)) {
1027 err = -ENODEV;
1028 dev_err(&cl->device,
1029 "HSI port not initialized\n");
1030 goto leave4;
1033 hsi_if->iface_state = CS_STATE_OPENED;
1034 local_bh_disable();
1035 cs_hsi_read_on_control(hsi_if);
1036 local_bh_enable();
1038 dev_dbg(&cl->device, "cs_hsi_start...done\n");
1040 BUG_ON(!hi);
1041 *hi = hsi_if;
1043 return 0;
1045 leave4:
1046 hsi_release_port(cl);
1047 leave3:
1048 cs_hsi_free_data(hsi_if);
1049 leave2:
1050 cs_free_cmds(hsi_if);
1051 leave1:
1052 kfree(hsi_if);
1053 leave0:
1054 dev_dbg(&cl->device, "cs_hsi_start...done/error\n\n");
1056 return err;
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);
1082 cs_free_cmds(hi);
1083 spin_unlock_bh(&hi->lock);
1084 kfree(hi);
1087 static vm_fault_t cs_char_vma_fault(struct vm_fault *vmf)
1089 struct cs_char *csdata = vmf->vma->vm_private_data;
1090 struct page *page;
1092 page = virt_to_page(csdata->mmap_base);
1093 get_page(page);
1094 vmf->page = page;
1096 return 0;
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)
1108 return -EIO;
1110 return 0;
1113 static __poll_t cs_char_poll(struct file *file, poll_table *wait)
1115 struct cs_char *csdata = file->private_data;
1116 __poll_t ret = 0;
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);
1126 return ret;
1129 static ssize_t cs_char_read(struct file *file, char __user *buf, size_t count,
1130 loff_t *unused)
1132 struct cs_char *csdata = file->private_data;
1133 u32 data;
1134 ssize_t retval;
1136 if (count < sizeof(data))
1137 return -EINVAL;
1139 for (;;) {
1140 DEFINE_WAIT(wait);
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--;
1148 } else {
1149 data = 0;
1151 spin_unlock_bh(&csdata->lock);
1153 if (data)
1154 break;
1155 if (file->f_flags & O_NONBLOCK) {
1156 retval = -EAGAIN;
1157 goto out;
1158 } else if (signal_pending(current)) {
1159 retval = -ERESTARTSYS;
1160 goto out;
1162 prepare_to_wait_exclusive(&csdata->wait, &wait,
1163 TASK_INTERRUPTIBLE);
1164 schedule();
1165 finish_wait(&csdata->wait, &wait);
1168 retval = put_user(data, (u32 __user *)buf);
1169 if (!retval)
1170 retval = sizeof(data);
1172 out:
1173 return retval;
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;
1180 u32 data;
1181 int err;
1182 ssize_t retval;
1184 if (count < sizeof(data))
1185 return -EINVAL;
1187 if (get_user(data, (u32 __user *)buf))
1188 retval = -EFAULT;
1189 else
1190 retval = count;
1192 err = cs_hsi_command(csdata->hi, data);
1193 if (err < 0)
1194 retval = err;
1196 return retval;
1199 static long cs_char_ioctl(struct file *file, unsigned int cmd,
1200 unsigned long arg)
1202 struct cs_char *csdata = file->private_data;
1203 int r = 0;
1205 switch (cmd) {
1206 case CS_GET_STATE: {
1207 unsigned int state;
1209 state = cs_hsi_get_state(csdata->hi);
1210 if (copy_to_user((void __user *)arg, &state, sizeof(state)))
1211 r = -EFAULT;
1213 break;
1215 case CS_SET_WAKELINE: {
1216 unsigned int state;
1218 if (copy_from_user(&state, (void __user *)arg, sizeof(state))) {
1219 r = -EFAULT;
1220 break;
1223 if (state > 1) {
1224 r = -EINVAL;
1225 break;
1228 cs_hsi_set_wakeline(csdata->hi, !!state);
1230 break;
1232 case CS_GET_IF_VERSION: {
1233 unsigned int ifver = CS_IF_VERSION;
1235 if (copy_to_user((void __user *)arg, &ifver, sizeof(ifver)))
1236 r = -EFAULT;
1238 break;
1240 case CS_CONFIG_BUFS: {
1241 struct cs_buffer_config buf_cfg;
1243 if (copy_from_user(&buf_cfg, (void __user *)arg,
1244 sizeof(buf_cfg)))
1245 r = -EFAULT;
1246 else
1247 r = cs_hsi_buf_config(csdata->hi, &buf_cfg);
1249 break;
1251 default:
1252 r = -ENOTTY;
1253 break;
1256 return r;
1259 static int cs_char_mmap(struct file *file, struct vm_area_struct *vma)
1261 if (vma->vm_end < vma->vm_start)
1262 return -EINVAL;
1264 if (vma_pages(vma) != 1)
1265 return -EINVAL;
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;
1271 return 0;
1274 static int cs_char_open(struct inode *unused, struct file *file)
1276 int ret = 0;
1277 unsigned long p;
1279 spin_lock_bh(&cs_char_data.lock);
1280 if (cs_char_data.opened) {
1281 ret = -EBUSY;
1282 spin_unlock_bh(&cs_char_data.lock);
1283 goto out1;
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);
1290 if (!p) {
1291 ret = -ENOMEM;
1292 goto out2;
1295 ret = cs_hsi_start(&cs_char_data.hi, cs_char_data.cl, p, CS_MMAP_SIZE);
1296 if (ret) {
1297 dev_err(&cs_char_data.cl->device, "Unable to initialize HSI\n");
1298 goto out3;
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;
1307 return 0;
1309 out3:
1310 free_page(p);
1311 out2:
1312 spin_lock_bh(&cs_char_data.lock);
1313 cs_char_data.opened = 0;
1314 spin_unlock_bh(&cs_char_data.lock);
1315 out1:
1316 return ret;
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);
1328 kfree(entry);
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);
1340 csdata->hi = NULL;
1341 free_page(csdata->mmap_base);
1342 cs_free_char_queue(&csdata->chardev_queue);
1343 cs_free_char_queue(&csdata->dataind_queue);
1344 csdata->opened = 0;
1345 spin_unlock_bh(&csdata->lock);
1347 return 0;
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)
1370 int err = 0;
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,
1383 "speech-control");
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);
1387 return err;
1390 cs_char_data.channel_id_data = hsi_get_channel_id_by_name(cl,
1391 "speech-data");
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);
1395 return err;
1398 err = misc_register(&cs_char_miscdev);
1399 if (err)
1400 dev_err(dev, "Failed to register: %d\n", err);
1402 return 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);
1415 if (hi)
1416 cs_hsi_stop(hi);
1418 return 0;
1421 static struct hsi_client_driver cs_hsi_driver = {
1422 .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");