2 * cmt_speech.c - HSI CMT speech driver
4 * Copyright (C) 2008,2009,2010 Nokia Corporation. All rights reserved.
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 *tstamp =
455 &hi->mmap_cfg->tstamp_rx_ctrl;
456 do_posix_clock_monotonic_gettime(tstamp);
458 spin_unlock(&hi->lock);
460 cs_notify_control(cmd);
463 cs_hsi_read_on_control(hi);
466 static void cs_hsi_peek_on_control_complete(struct hsi_msg *msg)
468 struct cs_hsi_iface *hi = msg->context;
471 if (msg->status == HSI_STATUS_ERROR) {
472 dev_err(&hi->cl->device, "Control peek RX error detected\n");
473 cs_hsi_control_read_error(hi, msg);
477 WARN_ON(!(hi->control_state & SSI_CHANNEL_STATE_READING));
479 dev_dbg(&hi->cl->device, "Peek on control complete, reading\n");
481 msg->complete = cs_hsi_read_on_control_complete;
482 ret = hsi_async_read(hi->cl, msg);
484 cs_hsi_control_read_error(hi, msg);
487 static void cs_hsi_read_on_control(struct cs_hsi_iface *hi)
492 spin_lock(&hi->lock);
493 if (hi->control_state & SSI_CHANNEL_STATE_READING) {
494 dev_err(&hi->cl->device, "Control read already pending (%d)\n",
496 spin_unlock(&hi->lock);
499 if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
500 dev_err(&hi->cl->device, "Control read error (%d)\n",
502 spin_unlock(&hi->lock);
505 hi->control_state |= SSI_CHANNEL_STATE_READING;
506 dev_dbg(&hi->cl->device, "Issuing RX on control\n");
507 msg = cs_claim_cmd(hi);
508 spin_unlock(&hi->lock);
511 msg->complete = cs_hsi_peek_on_control_complete;
512 ret = hsi_async_read(hi->cl, msg);
514 cs_hsi_control_read_error(hi, msg);
517 static void cs_hsi_write_on_control_complete(struct hsi_msg *msg)
519 struct cs_hsi_iface *hi = msg->context;
520 if (msg->status == HSI_STATUS_COMPLETED) {
521 spin_lock(&hi->lock);
522 hi->control_state &= ~SSI_CHANNEL_STATE_WRITING;
524 spin_unlock(&hi->lock);
525 } else if (msg->status == HSI_STATUS_ERROR) {
526 cs_hsi_control_write_error(hi, msg);
528 dev_err(&hi->cl->device,
529 "unexpected status in control write callback %d\n",
534 static int cs_hsi_write_on_control(struct cs_hsi_iface *hi, u32 message)
539 spin_lock(&hi->lock);
540 if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
541 spin_unlock(&hi->lock);
544 if (hi->control_state & SSI_CHANNEL_STATE_WRITING) {
545 dev_err(&hi->cl->device,
546 "Write still pending on control channel.\n");
547 spin_unlock(&hi->lock);
550 hi->control_state |= SSI_CHANNEL_STATE_WRITING;
551 msg = cs_claim_cmd(hi);
552 spin_unlock(&hi->lock);
554 cs_set_cmd(msg, message);
556 msg->complete = cs_hsi_write_on_control_complete;
557 dev_dbg(&hi->cl->device,
558 "Sending control message %08X\n", message);
559 ret = hsi_async_write(hi->cl, msg);
561 dev_err(&hi->cl->device,
562 "async_write failed with %d\n", ret);
563 cs_hsi_control_write_error(hi, msg);
567 * Make sure control read is always pending when issuing
568 * new control writes. This is needed as the controller
569 * may flush our messages if e.g. the peer device reboots
570 * unexpectedly (and we cannot directly resubmit a new read from
571 * the message destructor; see cs_cmd_destructor()).
573 if (!(hi->control_state & SSI_CHANNEL_STATE_READING)) {
574 dev_err(&hi->cl->device, "Restarting control reads\n");
575 cs_hsi_read_on_control(hi);
581 static void cs_hsi_read_on_data_complete(struct hsi_msg *msg)
583 struct cs_hsi_iface *hi = msg->context;
586 if (unlikely(msg->status == HSI_STATUS_ERROR)) {
587 cs_hsi_data_read_error(hi, msg);
591 spin_lock(&hi->lock);
592 WARN_ON(!(hi->data_state & SSI_CHANNEL_STATE_READING));
593 hi->data_state &= ~SSI_CHANNEL_STATE_READING;
594 payload = CS_RX_DATA_RECEIVED;
595 payload |= hi->rx_slot;
597 hi->rx_slot %= hi->rx_ptr_boundary;
598 /* expose current rx ptr in mmap area */
599 hi->mmap_cfg->rx_ptr = hi->rx_slot;
600 if (unlikely(waitqueue_active(&hi->datawait)))
601 wake_up_interruptible(&hi->datawait);
602 spin_unlock(&hi->lock);
604 cs_notify_data(payload, hi->rx_bufs);
605 cs_hsi_read_on_data(hi);
608 static void cs_hsi_peek_on_data_complete(struct hsi_msg *msg)
610 struct cs_hsi_iface *hi = msg->context;
614 if (unlikely(msg->status == HSI_STATUS_ERROR)) {
615 cs_hsi_data_read_error(hi, msg);
618 if (unlikely(hi->iface_state != CS_STATE_CONFIGURED)) {
619 dev_err(&hi->cl->device, "Data received in invalid state\n");
620 cs_hsi_data_read_error(hi, msg);
624 spin_lock(&hi->lock);
625 WARN_ON(!(hi->data_state & SSI_CHANNEL_STATE_POLL));
626 hi->data_state &= ~SSI_CHANNEL_STATE_POLL;
627 hi->data_state |= SSI_CHANNEL_STATE_READING;
628 spin_unlock(&hi->lock);
630 address = (u32 *)(hi->mmap_base +
631 hi->rx_offsets[hi->rx_slot % hi->rx_bufs]);
632 sg_init_one(msg->sgt.sgl, address, hi->buf_size);
634 msg->complete = cs_hsi_read_on_data_complete;
635 ret = hsi_async_read(hi->cl, msg);
637 cs_hsi_data_read_error(hi, msg);
641 * Read/write transaction is ongoing. Returns false if in
642 * SSI_CHANNEL_STATE_POLL state.
644 static inline int cs_state_xfer_active(unsigned int state)
646 return (state & SSI_CHANNEL_STATE_WRITING) ||
647 (state & SSI_CHANNEL_STATE_READING);
651 * No pending read/writes
653 static inline int cs_state_idle(unsigned int state)
655 return !(state & ~SSI_CHANNEL_STATE_ERROR);
658 static void cs_hsi_read_on_data(struct cs_hsi_iface *hi)
660 struct hsi_msg *rxmsg;
663 spin_lock(&hi->lock);
665 (SSI_CHANNEL_STATE_READING | SSI_CHANNEL_STATE_POLL)) {
666 dev_dbg(&hi->cl->device, "Data read already pending (%u)\n",
668 spin_unlock(&hi->lock);
671 hi->data_state |= SSI_CHANNEL_STATE_POLL;
672 spin_unlock(&hi->lock);
674 rxmsg = hi->data_rx_msg;
675 sg_init_one(rxmsg->sgt.sgl, (void *)hi->mmap_base, 0);
676 rxmsg->sgt.nents = 0;
677 rxmsg->complete = cs_hsi_peek_on_data_complete;
679 ret = hsi_async_read(hi->cl, rxmsg);
681 cs_hsi_data_read_error(hi, rxmsg);
684 static void cs_hsi_write_on_data_complete(struct hsi_msg *msg)
686 struct cs_hsi_iface *hi = msg->context;
688 if (msg->status == HSI_STATUS_COMPLETED) {
689 spin_lock(&hi->lock);
690 hi->data_state &= ~SSI_CHANNEL_STATE_WRITING;
691 if (unlikely(waitqueue_active(&hi->datawait)))
692 wake_up_interruptible(&hi->datawait);
693 spin_unlock(&hi->lock);
695 cs_hsi_data_write_error(hi, msg);
699 static int cs_hsi_write_on_data(struct cs_hsi_iface *hi, unsigned int slot)
702 struct hsi_msg *txmsg;
705 spin_lock(&hi->lock);
706 if (hi->iface_state != CS_STATE_CONFIGURED) {
707 dev_err(&hi->cl->device, "Not configured, aborting\n");
711 if (hi->data_state & SSI_CHANNEL_STATE_ERROR) {
712 dev_err(&hi->cl->device, "HSI error, aborting\n");
716 if (hi->data_state & SSI_CHANNEL_STATE_WRITING) {
717 dev_err(&hi->cl->device, "Write pending on data channel.\n");
721 hi->data_state |= SSI_CHANNEL_STATE_WRITING;
722 spin_unlock(&hi->lock);
725 address = (u32 *)(hi->mmap_base + hi->tx_offsets[hi->tx_slot]);
726 txmsg = hi->data_tx_msg;
727 sg_init_one(txmsg->sgt.sgl, address, hi->buf_size);
728 txmsg->complete = cs_hsi_write_on_data_complete;
729 ret = hsi_async_write(hi->cl, txmsg);
731 cs_hsi_data_write_error(hi, txmsg);
736 spin_unlock(&hi->lock);
738 cs_hsi_data_write_error(hi, hi->data_tx_msg);
743 static unsigned int cs_hsi_get_state(struct cs_hsi_iface *hi)
745 return hi->iface_state;
748 static int cs_hsi_command(struct cs_hsi_iface *hi, u32 cmd)
753 switch (cmd & TARGET_MASK) {
755 ret = cs_hsi_write_on_control(hi, cmd);
758 if ((cmd & CS_CMD_MASK) == CS_TX_DATA_READY)
759 ret = cs_hsi_write_on_data(hi, cmd & CS_PARAM_MASK);
772 static void cs_hsi_set_wakeline(struct cs_hsi_iface *hi, bool new_state)
776 spin_lock_bh(&hi->lock);
777 if (hi->wakeline_state != new_state) {
778 hi->wakeline_state = new_state;
780 dev_dbg(&hi->cl->device, "setting wake line to %d (%p)\n",
783 spin_unlock_bh(&hi->lock);
787 ssip_slave_start_tx(hi->master);
789 ssip_slave_stop_tx(hi->master);
792 dev_dbg(&hi->cl->device, "wake line set to %d (%p)\n",
796 static void set_buffer_sizes(struct cs_hsi_iface *hi, int rx_bufs, int tx_bufs)
798 hi->rx_bufs = rx_bufs;
799 hi->tx_bufs = tx_bufs;
800 hi->mmap_cfg->rx_bufs = rx_bufs;
801 hi->mmap_cfg->tx_bufs = tx_bufs;
803 if (hi->flags & CS_FEAT_ROLLING_RX_COUNTER) {
805 * For more robust overrun detection, let the rx
806 * pointer run in range 0..'boundary-1'. Boundary
807 * is a multiple of rx_bufs, and limited in max size
808 * by RX_PTR_MAX_SHIFT to allow for fast ptr-diff
811 hi->rx_ptr_boundary = (rx_bufs << RX_PTR_BOUNDARY_SHIFT);
812 hi->mmap_cfg->rx_ptr_boundary = hi->rx_ptr_boundary;
814 hi->rx_ptr_boundary = hi->rx_bufs;
818 static int check_buf_params(struct cs_hsi_iface *hi,
819 const struct cs_buffer_config *buf_cfg)
821 size_t buf_size_aligned = L1_CACHE_ALIGN(buf_cfg->buf_size) *
822 (buf_cfg->rx_bufs + buf_cfg->tx_bufs);
823 size_t ctrl_size_aligned = L1_CACHE_ALIGN(sizeof(*hi->mmap_cfg));
826 if (buf_cfg->rx_bufs > CS_MAX_BUFFERS ||
827 buf_cfg->tx_bufs > CS_MAX_BUFFERS) {
829 } else if ((buf_size_aligned + ctrl_size_aligned) >= hi->mmap_size) {
830 dev_err(&hi->cl->device, "No space for the requested buffer "
839 * Block until pending data transfers have completed.
841 static int cs_hsi_data_sync(struct cs_hsi_iface *hi)
845 spin_lock_bh(&hi->lock);
847 if (!cs_state_xfer_active(hi->data_state)) {
848 dev_dbg(&hi->cl->device, "hsi_data_sync break, idle\n");
855 if (!cs_state_xfer_active(hi->data_state))
857 if (signal_pending(current)) {
862 * prepare_to_wait must be called with hi->lock held
863 * so that callbacks can check for waitqueue_active()
865 prepare_to_wait(&hi->datawait, &wait, TASK_INTERRUPTIBLE);
866 spin_unlock_bh(&hi->lock);
867 s = schedule_timeout(
868 msecs_to_jiffies(CS_HSI_TRANSFER_TIMEOUT_MS));
869 spin_lock_bh(&hi->lock);
870 finish_wait(&hi->datawait, &wait);
872 dev_dbg(&hi->cl->device,
873 "hsi_data_sync timeout after %d ms\n",
874 CS_HSI_TRANSFER_TIMEOUT_MS);
881 spin_unlock_bh(&hi->lock);
882 dev_dbg(&hi->cl->device, "hsi_data_sync done with res %d\n", r);
887 static void cs_hsi_data_enable(struct cs_hsi_iface *hi,
888 struct cs_buffer_config *buf_cfg)
890 unsigned int data_start, i;
892 BUG_ON(hi->buf_size == 0);
894 set_buffer_sizes(hi, buf_cfg->rx_bufs, buf_cfg->tx_bufs);
896 hi->slot_size = L1_CACHE_ALIGN(hi->buf_size);
897 dev_dbg(&hi->cl->device,
898 "setting slot size to %u, buf size %u, align %u\n",
899 hi->slot_size, hi->buf_size, L1_CACHE_BYTES);
901 data_start = L1_CACHE_ALIGN(sizeof(*hi->mmap_cfg));
902 dev_dbg(&hi->cl->device,
903 "setting data start at %u, cfg block %u, align %u\n",
904 data_start, sizeof(*hi->mmap_cfg), L1_CACHE_BYTES);
906 for (i = 0; i < hi->mmap_cfg->rx_bufs; i++) {
907 hi->rx_offsets[i] = data_start + i * hi->slot_size;
908 hi->mmap_cfg->rx_offsets[i] = hi->rx_offsets[i];
909 dev_dbg(&hi->cl->device, "DL buf #%u at %u\n",
910 i, hi->rx_offsets[i]);
912 for (i = 0; i < hi->mmap_cfg->tx_bufs; i++) {
913 hi->tx_offsets[i] = data_start +
914 (i + hi->mmap_cfg->rx_bufs) * hi->slot_size;
915 hi->mmap_cfg->tx_offsets[i] = hi->tx_offsets[i];
916 dev_dbg(&hi->cl->device, "UL buf #%u at %u\n",
917 i, hi->rx_offsets[i]);
920 hi->iface_state = CS_STATE_CONFIGURED;
923 static void cs_hsi_data_disable(struct cs_hsi_iface *hi, int old_state)
925 if (old_state == CS_STATE_CONFIGURED) {
926 dev_dbg(&hi->cl->device,
927 "closing data channel with slot size 0\n");
928 hi->iface_state = CS_STATE_OPENED;
932 static int cs_hsi_buf_config(struct cs_hsi_iface *hi,
933 struct cs_buffer_config *buf_cfg)
936 unsigned int old_state = hi->iface_state;
938 spin_lock_bh(&hi->lock);
939 /* Prevent new transactions during buffer reconfig */
940 if (old_state == CS_STATE_CONFIGURED)
941 hi->iface_state = CS_STATE_OPENED;
942 spin_unlock_bh(&hi->lock);
945 * make sure that no non-zero data reads are ongoing before
946 * proceeding to change the buffer layout
948 r = cs_hsi_data_sync(hi);
952 WARN_ON(cs_state_xfer_active(hi->data_state));
954 spin_lock_bh(&hi->lock);
955 r = check_buf_params(hi, buf_cfg);
959 hi->buf_size = buf_cfg->buf_size;
960 hi->mmap_cfg->buf_size = hi->buf_size;
961 hi->flags = buf_cfg->flags;
968 cs_hsi_data_enable(hi, buf_cfg);
970 cs_hsi_data_disable(hi, old_state);
972 spin_unlock_bh(&hi->lock);
974 if (old_state != hi->iface_state) {
975 if (hi->iface_state == CS_STATE_CONFIGURED) {
976 pm_qos_add_request(&hi->pm_qos_req,
977 PM_QOS_CPU_DMA_LATENCY,
978 CS_QOS_LATENCY_FOR_DATA_USEC);
980 cs_hsi_read_on_data(hi);
982 } else if (old_state == CS_STATE_CONFIGURED) {
983 pm_qos_remove_request(&hi->pm_qos_req);
989 spin_unlock_bh(&hi->lock);
993 static int cs_hsi_start(struct cs_hsi_iface **hi, struct hsi_client *cl,
994 unsigned long mmap_base, unsigned long mmap_size)
997 struct cs_hsi_iface *hsi_if = kzalloc(sizeof(*hsi_if), GFP_KERNEL);
999 dev_dbg(&cl->device, "cs_hsi_start\n");
1005 spin_lock_init(&hsi_if->lock);
1007 hsi_if->iface_state = CS_STATE_CLOSED;
1008 hsi_if->mmap_cfg = (struct cs_mmap_config_block *)mmap_base;
1009 hsi_if->mmap_base = mmap_base;
1010 hsi_if->mmap_size = mmap_size;
1011 memset(hsi_if->mmap_cfg, 0, sizeof(*hsi_if->mmap_cfg));
1012 init_waitqueue_head(&hsi_if->datawait);
1013 err = cs_alloc_cmds(hsi_if);
1015 dev_err(&cl->device, "Unable to alloc HSI messages\n");
1018 err = cs_hsi_alloc_data(hsi_if);
1020 dev_err(&cl->device, "Unable to alloc HSI messages for data\n");
1023 err = hsi_claim_port(cl, 1);
1025 dev_err(&cl->device,
1026 "Could not open, HSI port already claimed\n");
1029 hsi_if->master = ssip_slave_get_master(cl);
1030 if (IS_ERR(hsi_if->master)) {
1031 err = PTR_ERR(hsi_if->master);
1032 dev_err(&cl->device, "Could not get HSI master client\n");
1035 if (!ssip_slave_running(hsi_if->master)) {
1037 dev_err(&cl->device,
1038 "HSI port not initialized\n");
1042 hsi_if->iface_state = CS_STATE_OPENED;
1044 cs_hsi_read_on_control(hsi_if);
1047 dev_dbg(&cl->device, "cs_hsi_start...done\n");
1055 hsi_release_port(cl);
1057 cs_hsi_free_data(hsi_if);
1059 cs_free_cmds(hsi_if);
1063 dev_dbg(&cl->device, "cs_hsi_start...done/error\n\n");
1068 static void cs_hsi_stop(struct cs_hsi_iface *hi)
1070 dev_dbg(&hi->cl->device, "cs_hsi_stop\n");
1071 cs_hsi_set_wakeline(hi, 0);
1072 ssip_slave_put_master(hi->master);
1074 /* hsi_release_port() needs to be called with CS_STATE_CLOSED */
1075 hi->iface_state = CS_STATE_CLOSED;
1076 hsi_release_port(hi->cl);
1079 * hsi_release_port() should flush out all the pending
1080 * messages, so cs_state_idle() should be true for both
1081 * control and data channels.
1083 WARN_ON(!cs_state_idle(hi->control_state));
1084 WARN_ON(!cs_state_idle(hi->data_state));
1086 if (pm_qos_request_active(&hi->pm_qos_req))
1087 pm_qos_remove_request(&hi->pm_qos_req);
1089 spin_lock_bh(&hi->lock);
1090 cs_hsi_free_data(hi);
1092 spin_unlock_bh(&hi->lock);
1096 static int cs_char_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1098 struct cs_char *csdata = vma->vm_private_data;
1101 page = virt_to_page(csdata->mmap_base);
1108 static struct vm_operations_struct cs_char_vm_ops = {
1109 .fault = cs_char_vma_fault,
1112 static int cs_char_fasync(int fd, struct file *file, int on)
1114 struct cs_char *csdata = file->private_data;
1116 if (fasync_helper(fd, file, on, &csdata->async_queue) < 0)
1122 static unsigned int cs_char_poll(struct file *file, poll_table *wait)
1124 struct cs_char *csdata = file->private_data;
1125 unsigned int ret = 0;
1127 poll_wait(file, &cs_char_data.wait, wait);
1128 spin_lock_bh(&csdata->lock);
1129 if (!list_empty(&csdata->chardev_queue))
1130 ret = POLLIN | POLLRDNORM;
1131 else if (!list_empty(&csdata->dataind_queue))
1132 ret = POLLIN | POLLRDNORM;
1133 spin_unlock_bh(&csdata->lock);
1138 static ssize_t cs_char_read(struct file *file, char __user *buf, size_t count,
1141 struct cs_char *csdata = file->private_data;
1145 if (count < sizeof(data))
1151 spin_lock_bh(&csdata->lock);
1152 if (!list_empty(&csdata->chardev_queue)) {
1153 data = cs_pop_entry(&csdata->chardev_queue);
1154 } else if (!list_empty(&csdata->dataind_queue)) {
1155 data = cs_pop_entry(&csdata->dataind_queue);
1156 csdata->dataind_pending--;
1160 spin_unlock_bh(&csdata->lock);
1164 if (file->f_flags & O_NONBLOCK) {
1167 } else if (signal_pending(current)) {
1168 retval = -ERESTARTSYS;
1171 prepare_to_wait_exclusive(&csdata->wait, &wait,
1172 TASK_INTERRUPTIBLE);
1174 finish_wait(&csdata->wait, &wait);
1177 retval = put_user(data, (u32 __user *)buf);
1179 retval = sizeof(data);
1185 static ssize_t cs_char_write(struct file *file, const char __user *buf,
1186 size_t count, loff_t *unused)
1188 struct cs_char *csdata = file->private_data;
1193 if (count < sizeof(data))
1196 if (get_user(data, (u32 __user *)buf))
1201 err = cs_hsi_command(csdata->hi, data);
1208 static long cs_char_ioctl(struct file *file, unsigned int cmd,
1211 struct cs_char *csdata = file->private_data;
1215 case CS_GET_STATE: {
1218 state = cs_hsi_get_state(csdata->hi);
1219 if (copy_to_user((void __user *)arg, &state, sizeof(state)))
1224 case CS_SET_WAKELINE: {
1227 if (copy_from_user(&state, (void __user *)arg, sizeof(state))) {
1237 cs_hsi_set_wakeline(csdata->hi, !!state);
1241 case CS_GET_IF_VERSION: {
1242 unsigned int ifver = CS_IF_VERSION;
1244 if (copy_to_user((void __user *)arg, &ifver, sizeof(ifver)))
1249 case CS_CONFIG_BUFS: {
1250 struct cs_buffer_config buf_cfg;
1252 if (copy_from_user(&buf_cfg, (void __user *)arg,
1256 r = cs_hsi_buf_config(csdata->hi, &buf_cfg);
1268 static int cs_char_mmap(struct file *file, struct vm_area_struct *vma)
1270 if (vma->vm_end < vma->vm_start)
1273 if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) != 1)
1276 vma->vm_flags |= VM_IO | VM_DONTDUMP | VM_DONTEXPAND;
1277 vma->vm_ops = &cs_char_vm_ops;
1278 vma->vm_private_data = file->private_data;
1283 static int cs_char_open(struct inode *unused, struct file *file)
1288 spin_lock_bh(&cs_char_data.lock);
1289 if (cs_char_data.opened) {
1291 spin_unlock_bh(&cs_char_data.lock);
1294 cs_char_data.opened = 1;
1295 cs_char_data.dataind_pending = 0;
1296 spin_unlock_bh(&cs_char_data.lock);
1298 p = get_zeroed_page(GFP_KERNEL);
1304 ret = cs_hsi_start(&cs_char_data.hi, cs_char_data.cl, p, CS_MMAP_SIZE);
1306 dev_err(&cs_char_data.cl->device, "Unable to initialize HSI\n");
1310 /* these are only used in release so lock not needed */
1311 cs_char_data.mmap_base = p;
1312 cs_char_data.mmap_size = CS_MMAP_SIZE;
1314 file->private_data = &cs_char_data;
1321 spin_lock_bh(&cs_char_data.lock);
1322 cs_char_data.opened = 0;
1323 spin_unlock_bh(&cs_char_data.lock);
1328 static void cs_free_char_queue(struct list_head *head)
1330 struct char_queue *entry;
1331 struct list_head *cursor, *next;
1333 if (!list_empty(head)) {
1334 list_for_each_safe(cursor, next, head) {
1335 entry = list_entry(cursor, struct char_queue, list);
1336 list_del(&entry->list);
1343 static int cs_char_release(struct inode *unused, struct file *file)
1345 struct cs_char *csdata = file->private_data;
1347 cs_hsi_stop(csdata->hi);
1348 spin_lock_bh(&csdata->lock);
1350 free_page(csdata->mmap_base);
1351 cs_free_char_queue(&csdata->chardev_queue);
1352 cs_free_char_queue(&csdata->dataind_queue);
1354 spin_unlock_bh(&csdata->lock);
1359 static const struct file_operations cs_char_fops = {
1360 .owner = THIS_MODULE,
1361 .read = cs_char_read,
1362 .write = cs_char_write,
1363 .poll = cs_char_poll,
1364 .unlocked_ioctl = cs_char_ioctl,
1365 .mmap = cs_char_mmap,
1366 .open = cs_char_open,
1367 .release = cs_char_release,
1368 .fasync = cs_char_fasync,
1371 static struct miscdevice cs_char_miscdev = {
1372 .minor = MISC_DYNAMIC_MINOR,
1373 .name = "cmt_speech",
1374 .fops = &cs_char_fops
1377 static int cs_hsi_client_probe(struct device *dev)
1380 struct hsi_client *cl = to_hsi_client(dev);
1382 dev_dbg(dev, "hsi_client_probe\n");
1383 init_waitqueue_head(&cs_char_data.wait);
1384 spin_lock_init(&cs_char_data.lock);
1385 cs_char_data.opened = 0;
1386 cs_char_data.cl = cl;
1387 cs_char_data.hi = NULL;
1388 INIT_LIST_HEAD(&cs_char_data.chardev_queue);
1389 INIT_LIST_HEAD(&cs_char_data.dataind_queue);
1391 cs_char_data.channel_id_cmd = hsi_get_channel_id_by_name(cl,
1393 if (cs_char_data.channel_id_cmd < 0) {
1394 err = cs_char_data.channel_id_cmd;
1395 dev_err(dev, "Could not get cmd channel (%d)\n", err);
1399 cs_char_data.channel_id_data = hsi_get_channel_id_by_name(cl,
1401 if (cs_char_data.channel_id_data < 0) {
1402 err = cs_char_data.channel_id_data;
1403 dev_err(dev, "Could not get data channel (%d)\n", err);
1407 err = misc_register(&cs_char_miscdev);
1409 dev_err(dev, "Failed to register: %d\n", err);
1414 static int cs_hsi_client_remove(struct device *dev)
1416 struct cs_hsi_iface *hi;
1418 dev_dbg(dev, "hsi_client_remove\n");
1419 misc_deregister(&cs_char_miscdev);
1420 spin_lock_bh(&cs_char_data.lock);
1421 hi = cs_char_data.hi;
1422 cs_char_data.hi = NULL;
1423 spin_unlock_bh(&cs_char_data.lock);
1430 static struct hsi_client_driver cs_hsi_driver = {
1432 .name = "cmt-speech",
1433 .owner = THIS_MODULE,
1434 .probe = cs_hsi_client_probe,
1435 .remove = cs_hsi_client_remove,
1439 static int __init cs_char_init(void)
1441 pr_info("CMT speech driver added\n");
1442 return hsi_register_client_driver(&cs_hsi_driver);
1444 module_init(cs_char_init);
1446 static void __exit cs_char_exit(void)
1448 hsi_unregister_client_driver(&cs_hsi_driver);
1449 pr_info("CMT speech driver removed\n");
1451 module_exit(cs_char_exit);
1453 MODULE_ALIAS("hsi:cmt-speech");
1456 MODULE_DESCRIPTION("CMT speech driver");
1457 MODULE_LICENSE("GPL v2");