2 * cmt_speech.c - HSI CMT speech driver
4 * Copyright (C) 2008,2009,2010 Nokia Corporation. All rights reserved.
6 * Contact: Kai Vehmanen <kai.vehmanen@nokia.com>
7 * Original author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
24 #include <linux/errno.h>
25 #include <linux/module.h>
26 #include <linux/types.h>
27 #include <linux/init.h>
28 #include <linux/device.h>
29 #include <linux/miscdevice.h>
31 #include <linux/slab.h>
33 #include <linux/poll.h>
34 #include <linux/sched.h>
35 #include <linux/ioctl.h>
36 #include <linux/uaccess.h>
37 #include <linux/pm_qos.h>
38 #include <linux/hsi/hsi.h>
39 #include <linux/hsi/ssi_protocol.h>
40 #include <linux/hsi/cs-protocol.h>
42 #define CS_MMAP_SIZE PAGE_SIZE
45 struct list_head list;
51 struct hsi_client *cl;
52 struct cs_hsi_iface *hi;
53 struct list_head chardev_queue;
54 struct list_head dataind_queue;
57 unsigned long mmap_base;
58 unsigned long mmap_size;
60 struct fasync_struct *async_queue;
61 wait_queue_head_t wait;
67 #define SSI_CHANNEL_STATE_READING 1
68 #define SSI_CHANNEL_STATE_WRITING (1 << 1)
69 #define SSI_CHANNEL_STATE_POLL (1 << 2)
70 #define SSI_CHANNEL_STATE_ERROR (1 << 3)
72 #define TARGET_MASK 0xf000000
73 #define TARGET_REMOTE (1 << CS_DOMAIN_SHIFT)
74 #define TARGET_LOCAL 0
76 /* Number of pre-allocated commands buffers */
80 * During data transfers, transactions must be handled
81 * within 20ms (fixed value in cmtspeech HSI protocol)
83 #define CS_QOS_LATENCY_FOR_DATA_USEC 20000
85 /* Timeout to wait for pending HSI transfers to complete */
86 #define CS_HSI_TRANSFER_TIMEOUT_MS 500
89 #define RX_PTR_BOUNDARY_SHIFT 8
90 #define RX_PTR_MAX_SHIFT (RX_PTR_BOUNDARY_SHIFT + \
93 struct hsi_client *cl;
94 struct hsi_client *master;
96 unsigned int iface_state;
97 unsigned int wakeline_state;
98 unsigned int control_state;
99 unsigned int data_state;
101 /* state exposed to application */
102 struct cs_mmap_config_block *mmap_cfg;
104 unsigned long mmap_base;
105 unsigned long mmap_size;
107 unsigned int rx_slot;
108 unsigned int tx_slot;
110 /* note: for security reasons, we do not trust the contents of
111 * mmap_cfg, but instead duplicate the variables here */
112 unsigned int buf_size;
113 unsigned int rx_bufs;
114 unsigned int tx_bufs;
115 unsigned int rx_ptr_boundary;
116 unsigned int rx_offsets[CS_MAX_BUFFERS];
117 unsigned int tx_offsets[CS_MAX_BUFFERS];
119 /* size of aligned memory blocks */
120 unsigned int slot_size;
123 struct list_head cmdqueue;
125 struct hsi_msg *data_rx_msg;
126 struct hsi_msg *data_tx_msg;
127 wait_queue_head_t datawait;
129 struct pm_qos_request pm_qos_req;
134 static struct cs_char cs_char_data;
136 static void cs_hsi_read_on_control(struct cs_hsi_iface *hi);
137 static void cs_hsi_read_on_data(struct cs_hsi_iface *hi);
139 static inline void rx_ptr_shift_too_big(void)
141 BUILD_BUG_ON((1LLU << RX_PTR_MAX_SHIFT) > UINT_MAX);
144 static void cs_notify(u32 message, struct list_head *head)
146 struct char_queue *entry;
148 spin_lock(&cs_char_data.lock);
150 if (!cs_char_data.opened) {
151 spin_unlock(&cs_char_data.lock);
155 entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
157 dev_err(&cs_char_data.cl->device,
158 "Can't allocate new entry for the queue.\n");
159 spin_unlock(&cs_char_data.lock);
163 entry->msg = message;
164 list_add_tail(&entry->list, head);
166 spin_unlock(&cs_char_data.lock);
168 wake_up_interruptible(&cs_char_data.wait);
169 kill_fasync(&cs_char_data.async_queue, SIGIO, POLL_IN);
175 static u32 cs_pop_entry(struct list_head *head)
177 struct char_queue *entry;
180 entry = list_entry(head->next, struct char_queue, list);
182 list_del(&entry->list);
188 static void cs_notify_control(u32 message)
190 cs_notify(message, &cs_char_data.chardev_queue);
193 static void cs_notify_data(u32 message, int maxlength)
195 cs_notify(message, &cs_char_data.dataind_queue);
197 spin_lock(&cs_char_data.lock);
198 cs_char_data.dataind_pending++;
199 while (cs_char_data.dataind_pending > maxlength &&
200 !list_empty(&cs_char_data.dataind_queue)) {
201 dev_dbg(&cs_char_data.cl->device, "data notification "
202 "queue overrun (%u entries)\n", cs_char_data.dataind_pending);
204 cs_pop_entry(&cs_char_data.dataind_queue);
205 cs_char_data.dataind_pending--;
207 spin_unlock(&cs_char_data.lock);
210 static inline void cs_set_cmd(struct hsi_msg *msg, u32 cmd)
212 u32 *data = sg_virt(msg->sgt.sgl);
216 static inline u32 cs_get_cmd(struct hsi_msg *msg)
218 u32 *data = sg_virt(msg->sgt.sgl);
222 static void cs_release_cmd(struct hsi_msg *msg)
224 struct cs_hsi_iface *hi = msg->context;
226 list_add_tail(&msg->link, &hi->cmdqueue);
229 static void cs_cmd_destructor(struct hsi_msg *msg)
231 struct cs_hsi_iface *hi = msg->context;
233 spin_lock(&hi->lock);
235 dev_dbg(&cs_char_data.cl->device, "control cmd destructor\n");
237 if (hi->iface_state != CS_STATE_CLOSED)
238 dev_err(&hi->cl->device, "Cmd flushed while driver active\n");
240 if (msg->ttype == HSI_MSG_READ)
242 ~(SSI_CHANNEL_STATE_POLL | SSI_CHANNEL_STATE_READING);
243 else if (msg->ttype == HSI_MSG_WRITE &&
244 hi->control_state & SSI_CHANNEL_STATE_WRITING)
245 hi->control_state &= ~SSI_CHANNEL_STATE_WRITING;
249 spin_unlock(&hi->lock);
252 static struct hsi_msg *cs_claim_cmd(struct cs_hsi_iface* ssi)
256 BUG_ON(list_empty(&ssi->cmdqueue));
258 msg = list_first_entry(&ssi->cmdqueue, struct hsi_msg, link);
259 list_del(&msg->link);
260 msg->destructor = cs_cmd_destructor;
265 static void cs_free_cmds(struct cs_hsi_iface *ssi)
267 struct hsi_msg *msg, *tmp;
269 list_for_each_entry_safe(msg, tmp, &ssi->cmdqueue, link) {
270 list_del(&msg->link);
271 msg->destructor = NULL;
272 kfree(sg_virt(msg->sgt.sgl));
277 static int cs_alloc_cmds(struct cs_hsi_iface *hi)
283 INIT_LIST_HEAD(&hi->cmdqueue);
285 for (i = 0; i < CS_MAX_CMDS; i++) {
286 msg = hsi_alloc_msg(1, GFP_KERNEL);
289 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
294 sg_init_one(msg->sgt.sgl, buf, sizeof(*buf));
295 msg->channel = cs_char_data.channel_id_cmd;
297 list_add_tail(&msg->link, &hi->cmdqueue);
307 static void cs_hsi_data_destructor(struct hsi_msg *msg)
309 struct cs_hsi_iface *hi = msg->context;
310 const char *dir = (msg->ttype == HSI_MSG_READ) ? "TX" : "RX";
312 dev_dbg(&cs_char_data.cl->device, "Freeing data %s message\n", dir);
314 spin_lock(&hi->lock);
315 if (hi->iface_state != CS_STATE_CLOSED)
316 dev_err(&cs_char_data.cl->device,
317 "Data %s flush while device active\n", dir);
318 if (msg->ttype == HSI_MSG_READ)
320 ~(SSI_CHANNEL_STATE_POLL | SSI_CHANNEL_STATE_READING);
322 hi->data_state &= ~SSI_CHANNEL_STATE_WRITING;
324 msg->status = HSI_STATUS_COMPLETED;
325 if (unlikely(waitqueue_active(&hi->datawait)))
326 wake_up_interruptible(&hi->datawait);
328 spin_unlock(&hi->lock);
331 static int cs_hsi_alloc_data(struct cs_hsi_iface *hi)
333 struct hsi_msg *txmsg, *rxmsg;
336 rxmsg = hsi_alloc_msg(1, GFP_KERNEL);
341 rxmsg->channel = cs_char_data.channel_id_data;
342 rxmsg->destructor = cs_hsi_data_destructor;
345 txmsg = hsi_alloc_msg(1, GFP_KERNEL);
350 txmsg->channel = cs_char_data.channel_id_data;
351 txmsg->destructor = cs_hsi_data_destructor;
354 hi->data_rx_msg = rxmsg;
355 hi->data_tx_msg = txmsg;
365 static void cs_hsi_free_data_msg(struct hsi_msg *msg)
367 WARN_ON(msg->status != HSI_STATUS_COMPLETED &&
368 msg->status != HSI_STATUS_ERROR);
372 static void cs_hsi_free_data(struct cs_hsi_iface *hi)
374 cs_hsi_free_data_msg(hi->data_rx_msg);
375 cs_hsi_free_data_msg(hi->data_tx_msg);
378 static inline void __cs_hsi_error_pre(struct cs_hsi_iface *hi,
379 struct hsi_msg *msg, const char *info,
382 spin_lock(&hi->lock);
383 dev_err(&hi->cl->device, "HSI %s error, msg %d, state %u\n",
384 info, msg->status, *state);
387 static inline void __cs_hsi_error_post(struct cs_hsi_iface *hi)
389 spin_unlock(&hi->lock);
392 static inline void __cs_hsi_error_read_bits(unsigned int *state)
394 *state |= SSI_CHANNEL_STATE_ERROR;
395 *state &= ~(SSI_CHANNEL_STATE_READING | SSI_CHANNEL_STATE_POLL);
398 static inline void __cs_hsi_error_write_bits(unsigned int *state)
400 *state |= SSI_CHANNEL_STATE_ERROR;
401 *state &= ~SSI_CHANNEL_STATE_WRITING;
404 static void cs_hsi_control_read_error(struct cs_hsi_iface *hi,
407 __cs_hsi_error_pre(hi, msg, "control read", &hi->control_state);
409 __cs_hsi_error_read_bits(&hi->control_state);
410 __cs_hsi_error_post(hi);
413 static void cs_hsi_control_write_error(struct cs_hsi_iface *hi,
416 __cs_hsi_error_pre(hi, msg, "control write", &hi->control_state);
418 __cs_hsi_error_write_bits(&hi->control_state);
419 __cs_hsi_error_post(hi);
423 static void cs_hsi_data_read_error(struct cs_hsi_iface *hi, struct hsi_msg *msg)
425 __cs_hsi_error_pre(hi, msg, "data read", &hi->data_state);
426 __cs_hsi_error_read_bits(&hi->data_state);
427 __cs_hsi_error_post(hi);
430 static void cs_hsi_data_write_error(struct cs_hsi_iface *hi,
433 __cs_hsi_error_pre(hi, msg, "data write", &hi->data_state);
434 __cs_hsi_error_write_bits(&hi->data_state);
435 __cs_hsi_error_post(hi);
438 static void cs_hsi_read_on_control_complete(struct hsi_msg *msg)
440 u32 cmd = cs_get_cmd(msg);
441 struct cs_hsi_iface *hi = msg->context;
443 spin_lock(&hi->lock);
444 hi->control_state &= ~SSI_CHANNEL_STATE_READING;
445 if (msg->status == HSI_STATUS_ERROR) {
446 dev_err(&hi->cl->device, "Control RX error detected\n");
447 cs_hsi_control_read_error(hi, msg);
448 spin_unlock(&hi->lock);
451 dev_dbg(&hi->cl->device, "Read on control: %08X\n", cmd);
453 if (hi->flags & CS_FEAT_TSTAMP_RX_CTRL) {
454 struct timespec tspec;
455 struct cs_timestamp *tstamp =
456 &hi->mmap_cfg->tstamp_rx_ctrl;
458 ktime_get_ts(&tspec);
460 tstamp->tv_sec = (__u32) tspec.tv_sec;
461 tstamp->tv_nsec = (__u32) tspec.tv_nsec;
463 spin_unlock(&hi->lock);
465 cs_notify_control(cmd);
468 cs_hsi_read_on_control(hi);
471 static void cs_hsi_peek_on_control_complete(struct hsi_msg *msg)
473 struct cs_hsi_iface *hi = msg->context;
476 if (msg->status == HSI_STATUS_ERROR) {
477 dev_err(&hi->cl->device, "Control peek RX error detected\n");
478 cs_hsi_control_read_error(hi, msg);
482 WARN_ON(!(hi->control_state & SSI_CHANNEL_STATE_READING));
484 dev_dbg(&hi->cl->device, "Peek on control complete, reading\n");
486 msg->complete = cs_hsi_read_on_control_complete;
487 ret = hsi_async_read(hi->cl, msg);
489 cs_hsi_control_read_error(hi, msg);
492 static void cs_hsi_read_on_control(struct cs_hsi_iface *hi)
497 spin_lock(&hi->lock);
498 if (hi->control_state & SSI_CHANNEL_STATE_READING) {
499 dev_err(&hi->cl->device, "Control read already pending (%d)\n",
501 spin_unlock(&hi->lock);
504 if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
505 dev_err(&hi->cl->device, "Control read error (%d)\n",
507 spin_unlock(&hi->lock);
510 hi->control_state |= SSI_CHANNEL_STATE_READING;
511 dev_dbg(&hi->cl->device, "Issuing RX on control\n");
512 msg = cs_claim_cmd(hi);
513 spin_unlock(&hi->lock);
516 msg->complete = cs_hsi_peek_on_control_complete;
517 ret = hsi_async_read(hi->cl, msg);
519 cs_hsi_control_read_error(hi, msg);
522 static void cs_hsi_write_on_control_complete(struct hsi_msg *msg)
524 struct cs_hsi_iface *hi = msg->context;
525 if (msg->status == HSI_STATUS_COMPLETED) {
526 spin_lock(&hi->lock);
527 hi->control_state &= ~SSI_CHANNEL_STATE_WRITING;
529 spin_unlock(&hi->lock);
530 } else if (msg->status == HSI_STATUS_ERROR) {
531 cs_hsi_control_write_error(hi, msg);
533 dev_err(&hi->cl->device,
534 "unexpected status in control write callback %d\n",
539 static int cs_hsi_write_on_control(struct cs_hsi_iface *hi, u32 message)
544 spin_lock(&hi->lock);
545 if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
546 spin_unlock(&hi->lock);
549 if (hi->control_state & SSI_CHANNEL_STATE_WRITING) {
550 dev_err(&hi->cl->device,
551 "Write still pending on control channel.\n");
552 spin_unlock(&hi->lock);
555 hi->control_state |= SSI_CHANNEL_STATE_WRITING;
556 msg = cs_claim_cmd(hi);
557 spin_unlock(&hi->lock);
559 cs_set_cmd(msg, message);
561 msg->complete = cs_hsi_write_on_control_complete;
562 dev_dbg(&hi->cl->device,
563 "Sending control message %08X\n", message);
564 ret = hsi_async_write(hi->cl, msg);
566 dev_err(&hi->cl->device,
567 "async_write failed with %d\n", ret);
568 cs_hsi_control_write_error(hi, msg);
572 * Make sure control read is always pending when issuing
573 * new control writes. This is needed as the controller
574 * may flush our messages if e.g. the peer device reboots
575 * unexpectedly (and we cannot directly resubmit a new read from
576 * the message destructor; see cs_cmd_destructor()).
578 if (!(hi->control_state & SSI_CHANNEL_STATE_READING)) {
579 dev_err(&hi->cl->device, "Restarting control reads\n");
580 cs_hsi_read_on_control(hi);
586 static void cs_hsi_read_on_data_complete(struct hsi_msg *msg)
588 struct cs_hsi_iface *hi = msg->context;
591 if (unlikely(msg->status == HSI_STATUS_ERROR)) {
592 cs_hsi_data_read_error(hi, msg);
596 spin_lock(&hi->lock);
597 WARN_ON(!(hi->data_state & SSI_CHANNEL_STATE_READING));
598 hi->data_state &= ~SSI_CHANNEL_STATE_READING;
599 payload = CS_RX_DATA_RECEIVED;
600 payload |= hi->rx_slot;
602 hi->rx_slot %= hi->rx_ptr_boundary;
603 /* expose current rx ptr in mmap area */
604 hi->mmap_cfg->rx_ptr = hi->rx_slot;
605 if (unlikely(waitqueue_active(&hi->datawait)))
606 wake_up_interruptible(&hi->datawait);
607 spin_unlock(&hi->lock);
609 cs_notify_data(payload, hi->rx_bufs);
610 cs_hsi_read_on_data(hi);
613 static void cs_hsi_peek_on_data_complete(struct hsi_msg *msg)
615 struct cs_hsi_iface *hi = msg->context;
619 if (unlikely(msg->status == HSI_STATUS_ERROR)) {
620 cs_hsi_data_read_error(hi, msg);
623 if (unlikely(hi->iface_state != CS_STATE_CONFIGURED)) {
624 dev_err(&hi->cl->device, "Data received in invalid state\n");
625 cs_hsi_data_read_error(hi, msg);
629 spin_lock(&hi->lock);
630 WARN_ON(!(hi->data_state & SSI_CHANNEL_STATE_POLL));
631 hi->data_state &= ~SSI_CHANNEL_STATE_POLL;
632 hi->data_state |= SSI_CHANNEL_STATE_READING;
633 spin_unlock(&hi->lock);
635 address = (u32 *)(hi->mmap_base +
636 hi->rx_offsets[hi->rx_slot % hi->rx_bufs]);
637 sg_init_one(msg->sgt.sgl, address, hi->buf_size);
639 msg->complete = cs_hsi_read_on_data_complete;
640 ret = hsi_async_read(hi->cl, msg);
642 cs_hsi_data_read_error(hi, msg);
646 * Read/write transaction is ongoing. Returns false if in
647 * SSI_CHANNEL_STATE_POLL state.
649 static inline int cs_state_xfer_active(unsigned int state)
651 return (state & SSI_CHANNEL_STATE_WRITING) ||
652 (state & SSI_CHANNEL_STATE_READING);
656 * No pending read/writes
658 static inline int cs_state_idle(unsigned int state)
660 return !(state & ~SSI_CHANNEL_STATE_ERROR);
663 static void cs_hsi_read_on_data(struct cs_hsi_iface *hi)
665 struct hsi_msg *rxmsg;
668 spin_lock(&hi->lock);
670 (SSI_CHANNEL_STATE_READING | SSI_CHANNEL_STATE_POLL)) {
671 dev_dbg(&hi->cl->device, "Data read already pending (%u)\n",
673 spin_unlock(&hi->lock);
676 hi->data_state |= SSI_CHANNEL_STATE_POLL;
677 spin_unlock(&hi->lock);
679 rxmsg = hi->data_rx_msg;
680 sg_init_one(rxmsg->sgt.sgl, (void *)hi->mmap_base, 0);
681 rxmsg->sgt.nents = 0;
682 rxmsg->complete = cs_hsi_peek_on_data_complete;
684 ret = hsi_async_read(hi->cl, rxmsg);
686 cs_hsi_data_read_error(hi, rxmsg);
689 static void cs_hsi_write_on_data_complete(struct hsi_msg *msg)
691 struct cs_hsi_iface *hi = msg->context;
693 if (msg->status == HSI_STATUS_COMPLETED) {
694 spin_lock(&hi->lock);
695 hi->data_state &= ~SSI_CHANNEL_STATE_WRITING;
696 if (unlikely(waitqueue_active(&hi->datawait)))
697 wake_up_interruptible(&hi->datawait);
698 spin_unlock(&hi->lock);
700 cs_hsi_data_write_error(hi, msg);
704 static int cs_hsi_write_on_data(struct cs_hsi_iface *hi, unsigned int slot)
707 struct hsi_msg *txmsg;
710 spin_lock(&hi->lock);
711 if (hi->iface_state != CS_STATE_CONFIGURED) {
712 dev_err(&hi->cl->device, "Not configured, aborting\n");
716 if (hi->data_state & SSI_CHANNEL_STATE_ERROR) {
717 dev_err(&hi->cl->device, "HSI error, aborting\n");
721 if (hi->data_state & SSI_CHANNEL_STATE_WRITING) {
722 dev_err(&hi->cl->device, "Write pending on data channel.\n");
726 hi->data_state |= SSI_CHANNEL_STATE_WRITING;
727 spin_unlock(&hi->lock);
730 address = (u32 *)(hi->mmap_base + hi->tx_offsets[hi->tx_slot]);
731 txmsg = hi->data_tx_msg;
732 sg_init_one(txmsg->sgt.sgl, address, hi->buf_size);
733 txmsg->complete = cs_hsi_write_on_data_complete;
734 ret = hsi_async_write(hi->cl, txmsg);
736 cs_hsi_data_write_error(hi, txmsg);
741 spin_unlock(&hi->lock);
743 cs_hsi_data_write_error(hi, hi->data_tx_msg);
748 static unsigned int cs_hsi_get_state(struct cs_hsi_iface *hi)
750 return hi->iface_state;
753 static int cs_hsi_command(struct cs_hsi_iface *hi, u32 cmd)
758 switch (cmd & TARGET_MASK) {
760 ret = cs_hsi_write_on_control(hi, cmd);
763 if ((cmd & CS_CMD_MASK) == CS_TX_DATA_READY)
764 ret = cs_hsi_write_on_data(hi, cmd & CS_PARAM_MASK);
777 static void cs_hsi_set_wakeline(struct cs_hsi_iface *hi, bool new_state)
781 spin_lock_bh(&hi->lock);
782 if (hi->wakeline_state != new_state) {
783 hi->wakeline_state = new_state;
785 dev_dbg(&hi->cl->device, "setting wake line to %d (%p)\n",
788 spin_unlock_bh(&hi->lock);
792 ssip_slave_start_tx(hi->master);
794 ssip_slave_stop_tx(hi->master);
797 dev_dbg(&hi->cl->device, "wake line set to %d (%p)\n",
801 static void set_buffer_sizes(struct cs_hsi_iface *hi, int rx_bufs, int tx_bufs)
803 hi->rx_bufs = rx_bufs;
804 hi->tx_bufs = tx_bufs;
805 hi->mmap_cfg->rx_bufs = rx_bufs;
806 hi->mmap_cfg->tx_bufs = tx_bufs;
808 if (hi->flags & CS_FEAT_ROLLING_RX_COUNTER) {
810 * For more robust overrun detection, let the rx
811 * pointer run in range 0..'boundary-1'. Boundary
812 * is a multiple of rx_bufs, and limited in max size
813 * by RX_PTR_MAX_SHIFT to allow for fast ptr-diff
816 hi->rx_ptr_boundary = (rx_bufs << RX_PTR_BOUNDARY_SHIFT);
817 hi->mmap_cfg->rx_ptr_boundary = hi->rx_ptr_boundary;
819 hi->rx_ptr_boundary = hi->rx_bufs;
823 static int check_buf_params(struct cs_hsi_iface *hi,
824 const struct cs_buffer_config *buf_cfg)
826 size_t buf_size_aligned = L1_CACHE_ALIGN(buf_cfg->buf_size) *
827 (buf_cfg->rx_bufs + buf_cfg->tx_bufs);
828 size_t ctrl_size_aligned = L1_CACHE_ALIGN(sizeof(*hi->mmap_cfg));
831 if (buf_cfg->rx_bufs > CS_MAX_BUFFERS ||
832 buf_cfg->tx_bufs > CS_MAX_BUFFERS) {
834 } else if ((buf_size_aligned + ctrl_size_aligned) >= hi->mmap_size) {
835 dev_err(&hi->cl->device, "No space for the requested buffer "
844 * Block until pending data transfers have completed.
846 static int cs_hsi_data_sync(struct cs_hsi_iface *hi)
850 spin_lock_bh(&hi->lock);
852 if (!cs_state_xfer_active(hi->data_state)) {
853 dev_dbg(&hi->cl->device, "hsi_data_sync break, idle\n");
860 if (!cs_state_xfer_active(hi->data_state))
862 if (signal_pending(current)) {
867 * prepare_to_wait must be called with hi->lock held
868 * so that callbacks can check for waitqueue_active()
870 prepare_to_wait(&hi->datawait, &wait, TASK_INTERRUPTIBLE);
871 spin_unlock_bh(&hi->lock);
872 s = schedule_timeout(
873 msecs_to_jiffies(CS_HSI_TRANSFER_TIMEOUT_MS));
874 spin_lock_bh(&hi->lock);
875 finish_wait(&hi->datawait, &wait);
877 dev_dbg(&hi->cl->device,
878 "hsi_data_sync timeout after %d ms\n",
879 CS_HSI_TRANSFER_TIMEOUT_MS);
886 spin_unlock_bh(&hi->lock);
887 dev_dbg(&hi->cl->device, "hsi_data_sync done with res %d\n", r);
892 static void cs_hsi_data_enable(struct cs_hsi_iface *hi,
893 struct cs_buffer_config *buf_cfg)
895 unsigned int data_start, i;
897 BUG_ON(hi->buf_size == 0);
899 set_buffer_sizes(hi, buf_cfg->rx_bufs, buf_cfg->tx_bufs);
901 hi->slot_size = L1_CACHE_ALIGN(hi->buf_size);
902 dev_dbg(&hi->cl->device,
903 "setting slot size to %u, buf size %u, align %u\n",
904 hi->slot_size, hi->buf_size, L1_CACHE_BYTES);
906 data_start = L1_CACHE_ALIGN(sizeof(*hi->mmap_cfg));
907 dev_dbg(&hi->cl->device,
908 "setting data start at %u, cfg block %u, align %u\n",
909 data_start, sizeof(*hi->mmap_cfg), L1_CACHE_BYTES);
911 for (i = 0; i < hi->mmap_cfg->rx_bufs; i++) {
912 hi->rx_offsets[i] = data_start + i * hi->slot_size;
913 hi->mmap_cfg->rx_offsets[i] = hi->rx_offsets[i];
914 dev_dbg(&hi->cl->device, "DL buf #%u at %u\n",
915 i, hi->rx_offsets[i]);
917 for (i = 0; i < hi->mmap_cfg->tx_bufs; i++) {
918 hi->tx_offsets[i] = data_start +
919 (i + hi->mmap_cfg->rx_bufs) * hi->slot_size;
920 hi->mmap_cfg->tx_offsets[i] = hi->tx_offsets[i];
921 dev_dbg(&hi->cl->device, "UL buf #%u at %u\n",
922 i, hi->rx_offsets[i]);
925 hi->iface_state = CS_STATE_CONFIGURED;
928 static void cs_hsi_data_disable(struct cs_hsi_iface *hi, int old_state)
930 if (old_state == CS_STATE_CONFIGURED) {
931 dev_dbg(&hi->cl->device,
932 "closing data channel with slot size 0\n");
933 hi->iface_state = CS_STATE_OPENED;
937 static int cs_hsi_buf_config(struct cs_hsi_iface *hi,
938 struct cs_buffer_config *buf_cfg)
941 unsigned int old_state = hi->iface_state;
943 spin_lock_bh(&hi->lock);
944 /* Prevent new transactions during buffer reconfig */
945 if (old_state == CS_STATE_CONFIGURED)
946 hi->iface_state = CS_STATE_OPENED;
947 spin_unlock_bh(&hi->lock);
950 * make sure that no non-zero data reads are ongoing before
951 * proceeding to change the buffer layout
953 r = cs_hsi_data_sync(hi);
957 WARN_ON(cs_state_xfer_active(hi->data_state));
959 spin_lock_bh(&hi->lock);
960 r = check_buf_params(hi, buf_cfg);
964 hi->buf_size = buf_cfg->buf_size;
965 hi->mmap_cfg->buf_size = hi->buf_size;
966 hi->flags = buf_cfg->flags;
973 cs_hsi_data_enable(hi, buf_cfg);
975 cs_hsi_data_disable(hi, old_state);
977 spin_unlock_bh(&hi->lock);
979 if (old_state != hi->iface_state) {
980 if (hi->iface_state == CS_STATE_CONFIGURED) {
981 pm_qos_add_request(&hi->pm_qos_req,
982 PM_QOS_CPU_DMA_LATENCY,
983 CS_QOS_LATENCY_FOR_DATA_USEC);
985 cs_hsi_read_on_data(hi);
987 } else if (old_state == CS_STATE_CONFIGURED) {
988 pm_qos_remove_request(&hi->pm_qos_req);
994 spin_unlock_bh(&hi->lock);
998 static int cs_hsi_start(struct cs_hsi_iface **hi, struct hsi_client *cl,
999 unsigned long mmap_base, unsigned long mmap_size)
1002 struct cs_hsi_iface *hsi_if = kzalloc(sizeof(*hsi_if), GFP_KERNEL);
1004 dev_dbg(&cl->device, "cs_hsi_start\n");
1010 spin_lock_init(&hsi_if->lock);
1012 hsi_if->iface_state = CS_STATE_CLOSED;
1013 hsi_if->mmap_cfg = (struct cs_mmap_config_block *)mmap_base;
1014 hsi_if->mmap_base = mmap_base;
1015 hsi_if->mmap_size = mmap_size;
1016 memset(hsi_if->mmap_cfg, 0, sizeof(*hsi_if->mmap_cfg));
1017 init_waitqueue_head(&hsi_if->datawait);
1018 err = cs_alloc_cmds(hsi_if);
1020 dev_err(&cl->device, "Unable to alloc HSI messages\n");
1023 err = cs_hsi_alloc_data(hsi_if);
1025 dev_err(&cl->device, "Unable to alloc HSI messages for data\n");
1028 err = hsi_claim_port(cl, 1);
1030 dev_err(&cl->device,
1031 "Could not open, HSI port already claimed\n");
1034 hsi_if->master = ssip_slave_get_master(cl);
1035 if (IS_ERR(hsi_if->master)) {
1036 err = PTR_ERR(hsi_if->master);
1037 dev_err(&cl->device, "Could not get HSI master client\n");
1040 if (!ssip_slave_running(hsi_if->master)) {
1042 dev_err(&cl->device,
1043 "HSI port not initialized\n");
1047 hsi_if->iface_state = CS_STATE_OPENED;
1049 cs_hsi_read_on_control(hsi_if);
1052 dev_dbg(&cl->device, "cs_hsi_start...done\n");
1060 hsi_release_port(cl);
1062 cs_hsi_free_data(hsi_if);
1064 cs_free_cmds(hsi_if);
1068 dev_dbg(&cl->device, "cs_hsi_start...done/error\n\n");
1073 static void cs_hsi_stop(struct cs_hsi_iface *hi)
1075 dev_dbg(&hi->cl->device, "cs_hsi_stop\n");
1076 cs_hsi_set_wakeline(hi, 0);
1077 ssip_slave_put_master(hi->master);
1079 /* hsi_release_port() needs to be called with CS_STATE_CLOSED */
1080 hi->iface_state = CS_STATE_CLOSED;
1081 hsi_release_port(hi->cl);
1084 * hsi_release_port() should flush out all the pending
1085 * messages, so cs_state_idle() should be true for both
1086 * control and data channels.
1088 WARN_ON(!cs_state_idle(hi->control_state));
1089 WARN_ON(!cs_state_idle(hi->data_state));
1091 if (pm_qos_request_active(&hi->pm_qos_req))
1092 pm_qos_remove_request(&hi->pm_qos_req);
1094 spin_lock_bh(&hi->lock);
1095 cs_hsi_free_data(hi);
1097 spin_unlock_bh(&hi->lock);
1101 static int cs_char_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1103 struct cs_char *csdata = vma->vm_private_data;
1106 page = virt_to_page(csdata->mmap_base);
1113 static struct vm_operations_struct cs_char_vm_ops = {
1114 .fault = cs_char_vma_fault,
1117 static int cs_char_fasync(int fd, struct file *file, int on)
1119 struct cs_char *csdata = file->private_data;
1121 if (fasync_helper(fd, file, on, &csdata->async_queue) < 0)
1127 static unsigned int cs_char_poll(struct file *file, poll_table *wait)
1129 struct cs_char *csdata = file->private_data;
1130 unsigned int ret = 0;
1132 poll_wait(file, &cs_char_data.wait, wait);
1133 spin_lock_bh(&csdata->lock);
1134 if (!list_empty(&csdata->chardev_queue))
1135 ret = POLLIN | POLLRDNORM;
1136 else if (!list_empty(&csdata->dataind_queue))
1137 ret = POLLIN | POLLRDNORM;
1138 spin_unlock_bh(&csdata->lock);
1143 static ssize_t cs_char_read(struct file *file, char __user *buf, size_t count,
1146 struct cs_char *csdata = file->private_data;
1150 if (count < sizeof(data))
1156 spin_lock_bh(&csdata->lock);
1157 if (!list_empty(&csdata->chardev_queue)) {
1158 data = cs_pop_entry(&csdata->chardev_queue);
1159 } else if (!list_empty(&csdata->dataind_queue)) {
1160 data = cs_pop_entry(&csdata->dataind_queue);
1161 csdata->dataind_pending--;
1165 spin_unlock_bh(&csdata->lock);
1169 if (file->f_flags & O_NONBLOCK) {
1172 } else if (signal_pending(current)) {
1173 retval = -ERESTARTSYS;
1176 prepare_to_wait_exclusive(&csdata->wait, &wait,
1177 TASK_INTERRUPTIBLE);
1179 finish_wait(&csdata->wait, &wait);
1182 retval = put_user(data, (u32 __user *)buf);
1184 retval = sizeof(data);
1190 static ssize_t cs_char_write(struct file *file, const char __user *buf,
1191 size_t count, loff_t *unused)
1193 struct cs_char *csdata = file->private_data;
1198 if (count < sizeof(data))
1201 if (get_user(data, (u32 __user *)buf))
1206 err = cs_hsi_command(csdata->hi, data);
1213 static long cs_char_ioctl(struct file *file, unsigned int cmd,
1216 struct cs_char *csdata = file->private_data;
1220 case CS_GET_STATE: {
1223 state = cs_hsi_get_state(csdata->hi);
1224 if (copy_to_user((void __user *)arg, &state, sizeof(state)))
1229 case CS_SET_WAKELINE: {
1232 if (copy_from_user(&state, (void __user *)arg, sizeof(state))) {
1242 cs_hsi_set_wakeline(csdata->hi, !!state);
1246 case CS_GET_IF_VERSION: {
1247 unsigned int ifver = CS_IF_VERSION;
1249 if (copy_to_user((void __user *)arg, &ifver, sizeof(ifver)))
1254 case CS_CONFIG_BUFS: {
1255 struct cs_buffer_config buf_cfg;
1257 if (copy_from_user(&buf_cfg, (void __user *)arg,
1261 r = cs_hsi_buf_config(csdata->hi, &buf_cfg);
1273 static int cs_char_mmap(struct file *file, struct vm_area_struct *vma)
1275 if (vma->vm_end < vma->vm_start)
1278 if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) != 1)
1281 vma->vm_flags |= VM_IO | VM_DONTDUMP | VM_DONTEXPAND;
1282 vma->vm_ops = &cs_char_vm_ops;
1283 vma->vm_private_data = file->private_data;
1288 static int cs_char_open(struct inode *unused, struct file *file)
1293 spin_lock_bh(&cs_char_data.lock);
1294 if (cs_char_data.opened) {
1296 spin_unlock_bh(&cs_char_data.lock);
1299 cs_char_data.opened = 1;
1300 cs_char_data.dataind_pending = 0;
1301 spin_unlock_bh(&cs_char_data.lock);
1303 p = get_zeroed_page(GFP_KERNEL);
1309 ret = cs_hsi_start(&cs_char_data.hi, cs_char_data.cl, p, CS_MMAP_SIZE);
1311 dev_err(&cs_char_data.cl->device, "Unable to initialize HSI\n");
1315 /* these are only used in release so lock not needed */
1316 cs_char_data.mmap_base = p;
1317 cs_char_data.mmap_size = CS_MMAP_SIZE;
1319 file->private_data = &cs_char_data;
1326 spin_lock_bh(&cs_char_data.lock);
1327 cs_char_data.opened = 0;
1328 spin_unlock_bh(&cs_char_data.lock);
1333 static void cs_free_char_queue(struct list_head *head)
1335 struct char_queue *entry;
1336 struct list_head *cursor, *next;
1338 if (!list_empty(head)) {
1339 list_for_each_safe(cursor, next, head) {
1340 entry = list_entry(cursor, struct char_queue, list);
1341 list_del(&entry->list);
1348 static int cs_char_release(struct inode *unused, struct file *file)
1350 struct cs_char *csdata = file->private_data;
1352 cs_hsi_stop(csdata->hi);
1353 spin_lock_bh(&csdata->lock);
1355 free_page(csdata->mmap_base);
1356 cs_free_char_queue(&csdata->chardev_queue);
1357 cs_free_char_queue(&csdata->dataind_queue);
1359 spin_unlock_bh(&csdata->lock);
1364 static const struct file_operations cs_char_fops = {
1365 .owner = THIS_MODULE,
1366 .read = cs_char_read,
1367 .write = cs_char_write,
1368 .poll = cs_char_poll,
1369 .unlocked_ioctl = cs_char_ioctl,
1370 .mmap = cs_char_mmap,
1371 .open = cs_char_open,
1372 .release = cs_char_release,
1373 .fasync = cs_char_fasync,
1376 static struct miscdevice cs_char_miscdev = {
1377 .minor = MISC_DYNAMIC_MINOR,
1378 .name = "cmt_speech",
1379 .fops = &cs_char_fops
1382 static int cs_hsi_client_probe(struct device *dev)
1385 struct hsi_client *cl = to_hsi_client(dev);
1387 dev_dbg(dev, "hsi_client_probe\n");
1388 init_waitqueue_head(&cs_char_data.wait);
1389 spin_lock_init(&cs_char_data.lock);
1390 cs_char_data.opened = 0;
1391 cs_char_data.cl = cl;
1392 cs_char_data.hi = NULL;
1393 INIT_LIST_HEAD(&cs_char_data.chardev_queue);
1394 INIT_LIST_HEAD(&cs_char_data.dataind_queue);
1396 cs_char_data.channel_id_cmd = hsi_get_channel_id_by_name(cl,
1398 if (cs_char_data.channel_id_cmd < 0) {
1399 err = cs_char_data.channel_id_cmd;
1400 dev_err(dev, "Could not get cmd channel (%d)\n", err);
1404 cs_char_data.channel_id_data = hsi_get_channel_id_by_name(cl,
1406 if (cs_char_data.channel_id_data < 0) {
1407 err = cs_char_data.channel_id_data;
1408 dev_err(dev, "Could not get data channel (%d)\n", err);
1412 err = misc_register(&cs_char_miscdev);
1414 dev_err(dev, "Failed to register: %d\n", err);
1419 static int cs_hsi_client_remove(struct device *dev)
1421 struct cs_hsi_iface *hi;
1423 dev_dbg(dev, "hsi_client_remove\n");
1424 misc_deregister(&cs_char_miscdev);
1425 spin_lock_bh(&cs_char_data.lock);
1426 hi = cs_char_data.hi;
1427 cs_char_data.hi = NULL;
1428 spin_unlock_bh(&cs_char_data.lock);
1435 static struct hsi_client_driver cs_hsi_driver = {
1437 .name = "cmt-speech",
1438 .owner = THIS_MODULE,
1439 .probe = cs_hsi_client_probe,
1440 .remove = cs_hsi_client_remove,
1444 static int __init cs_char_init(void)
1446 pr_info("CMT speech driver added\n");
1447 return hsi_register_client_driver(&cs_hsi_driver);
1449 module_init(cs_char_init);
1451 static void __exit cs_char_exit(void)
1453 hsi_unregister_client_driver(&cs_hsi_driver);
1454 pr_info("CMT speech driver removed\n");
1456 module_exit(cs_char_exit);
1458 MODULE_ALIAS("hsi:cmt-speech");
1459 MODULE_AUTHOR("Kai Vehmanen <kai.vehmanen@nokia.com>");
1460 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
1461 MODULE_DESCRIPTION("CMT speech driver");
1462 MODULE_LICENSE("GPL v2");