xref: /openbmc/linux/drivers/hsi/clients/cmt_speech.c (revision 99b7e93c)
1 /*
2  * cmt_speech.c - HSI CMT speech driver
3  *
4  * Copyright (C) 2008,2009,2010 Nokia Corporation. All rights reserved.
5  *
6  * Contact: Kai Vehmanen <kai.vehmanen@nokia.com>
7  * Original author: Peter Ujfalusi <peter.ujfalusi@nokia.com>
8  *
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.
12  *
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.
17  *
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
21  * 02110-1301 USA
22  */
23 
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>
30 #include <linux/mm.h>
31 #include <linux/slab.h>
32 #include <linux/fs.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>
41 
42 #define CS_MMAP_SIZE	PAGE_SIZE
43 
44 struct char_queue {
45 	struct list_head	list;
46 	u32			msg;
47 };
48 
49 struct cs_char {
50 	unsigned int		opened;
51 	struct hsi_client	*cl;
52 	struct cs_hsi_iface	*hi;
53 	struct list_head	chardev_queue;
54 	struct list_head	dataind_queue;
55 	int			dataind_pending;
56 	/* mmap things */
57 	unsigned long		mmap_base;
58 	unsigned long		mmap_size;
59 	spinlock_t		lock;
60 	struct fasync_struct	*async_queue;
61 	wait_queue_head_t	wait;
62 	/* hsi channel ids */
63 	int                     channel_id_cmd;
64 	int                     channel_id_data;
65 };
66 
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)
71 
72 #define TARGET_MASK			0xf000000
73 #define TARGET_REMOTE			(1 << CS_DOMAIN_SHIFT)
74 #define TARGET_LOCAL			0
75 
76 /* Number of pre-allocated commands buffers */
77 #define CS_MAX_CMDS		        4
78 
79 /*
80  * During data transfers, transactions must be handled
81  * within 20ms (fixed value in cmtspeech HSI protocol)
82  */
83 #define CS_QOS_LATENCY_FOR_DATA_USEC	20000
84 
85 /* Timeout to wait for pending HSI transfers to complete */
86 #define CS_HSI_TRANSFER_TIMEOUT_MS      500
87 
88 
89 #define RX_PTR_BOUNDARY_SHIFT		8
90 #define RX_PTR_MAX_SHIFT		(RX_PTR_BOUNDARY_SHIFT + \
91 						CS_MAX_BUFFERS_SHIFT)
92 struct cs_hsi_iface {
93 	struct hsi_client		*cl;
94 	struct hsi_client		*master;
95 
96 	unsigned int			iface_state;
97 	unsigned int			wakeline_state;
98 	unsigned int			control_state;
99 	unsigned int			data_state;
100 
101 	/* state exposed to application */
102 	struct cs_mmap_config_block	*mmap_cfg;
103 
104 	unsigned long			mmap_base;
105 	unsigned long			mmap_size;
106 
107 	unsigned int			rx_slot;
108 	unsigned int			tx_slot;
109 
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];
118 
119 	/* size of aligned memory blocks */
120 	unsigned int			slot_size;
121 	unsigned int			flags;
122 
123 	struct list_head		cmdqueue;
124 
125 	struct hsi_msg			*data_rx_msg;
126 	struct hsi_msg			*data_tx_msg;
127 	wait_queue_head_t		datawait;
128 
129 	struct pm_qos_request           pm_qos_req;
130 
131 	spinlock_t			lock;
132 };
133 
134 static struct cs_char cs_char_data;
135 
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);
138 
139 static inline void rx_ptr_shift_too_big(void)
140 {
141 	BUILD_BUG_ON((1LLU << RX_PTR_MAX_SHIFT) > UINT_MAX);
142 }
143 
144 static void cs_notify(u32 message, struct list_head *head)
145 {
146 	struct char_queue *entry;
147 
148 	spin_lock(&cs_char_data.lock);
149 
150 	if (!cs_char_data.opened) {
151 		spin_unlock(&cs_char_data.lock);
152 		goto out;
153 	}
154 
155 	entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
156 	if (!entry) {
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);
160 		goto out;
161 	}
162 
163 	entry->msg = message;
164 	list_add_tail(&entry->list, head);
165 
166 	spin_unlock(&cs_char_data.lock);
167 
168 	wake_up_interruptible(&cs_char_data.wait);
169 	kill_fasync(&cs_char_data.async_queue, SIGIO, POLL_IN);
170 
171 out:
172 	return;
173 }
174 
175 static u32 cs_pop_entry(struct list_head *head)
176 {
177 	struct char_queue *entry;
178 	u32 data;
179 
180 	entry = list_entry(head->next, struct char_queue, list);
181 	data = entry->msg;
182 	list_del(&entry->list);
183 	kfree(entry);
184 
185 	return data;
186 }
187 
188 static void cs_notify_control(u32 message)
189 {
190 	cs_notify(message, &cs_char_data.chardev_queue);
191 }
192 
193 static void cs_notify_data(u32 message, int maxlength)
194 {
195 	cs_notify(message, &cs_char_data.dataind_queue);
196 
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);
203 
204 		cs_pop_entry(&cs_char_data.dataind_queue);
205 		cs_char_data.dataind_pending--;
206 	}
207 	spin_unlock(&cs_char_data.lock);
208 }
209 
210 static inline void cs_set_cmd(struct hsi_msg *msg, u32 cmd)
211 {
212 	u32 *data = sg_virt(msg->sgt.sgl);
213 	*data = cmd;
214 }
215 
216 static inline u32 cs_get_cmd(struct hsi_msg *msg)
217 {
218 	u32 *data = sg_virt(msg->sgt.sgl);
219 	return *data;
220 }
221 
222 static void cs_release_cmd(struct hsi_msg *msg)
223 {
224 	struct cs_hsi_iface *hi = msg->context;
225 
226 	list_add_tail(&msg->link, &hi->cmdqueue);
227 }
228 
229 static void cs_cmd_destructor(struct hsi_msg *msg)
230 {
231 	struct cs_hsi_iface *hi = msg->context;
232 
233 	spin_lock(&hi->lock);
234 
235 	dev_dbg(&cs_char_data.cl->device, "control cmd destructor\n");
236 
237 	if (hi->iface_state != CS_STATE_CLOSED)
238 		dev_err(&hi->cl->device, "Cmd flushed while driver active\n");
239 
240 	if (msg->ttype == HSI_MSG_READ)
241 		hi->control_state &=
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;
246 
247 	cs_release_cmd(msg);
248 
249 	spin_unlock(&hi->lock);
250 }
251 
252 static struct hsi_msg *cs_claim_cmd(struct cs_hsi_iface* ssi)
253 {
254 	struct hsi_msg *msg;
255 
256 	BUG_ON(list_empty(&ssi->cmdqueue));
257 
258 	msg = list_first_entry(&ssi->cmdqueue, struct hsi_msg, link);
259 	list_del(&msg->link);
260 	msg->destructor = cs_cmd_destructor;
261 
262 	return msg;
263 }
264 
265 static void cs_free_cmds(struct cs_hsi_iface *ssi)
266 {
267 	struct hsi_msg *msg, *tmp;
268 
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));
273 		hsi_free_msg(msg);
274 	}
275 }
276 
277 static int cs_alloc_cmds(struct cs_hsi_iface *hi)
278 {
279 	struct hsi_msg *msg;
280 	u32 *buf;
281 	unsigned int i;
282 
283 	INIT_LIST_HEAD(&hi->cmdqueue);
284 
285 	for (i = 0; i < CS_MAX_CMDS; i++) {
286 		msg = hsi_alloc_msg(1, GFP_KERNEL);
287 		if (!msg)
288 			goto out;
289 		buf = kmalloc(sizeof(*buf), GFP_KERNEL);
290 		if (!buf) {
291 			hsi_free_msg(msg);
292 			goto out;
293 		}
294 		sg_init_one(msg->sgt.sgl, buf, sizeof(*buf));
295 		msg->channel = cs_char_data.channel_id_cmd;
296 		msg->context = hi;
297 		list_add_tail(&msg->link, &hi->cmdqueue);
298 	}
299 
300 	return 0;
301 
302 out:
303 	cs_free_cmds(hi);
304 	return -ENOMEM;
305 }
306 
307 static void cs_hsi_data_destructor(struct hsi_msg *msg)
308 {
309 	struct cs_hsi_iface *hi = msg->context;
310 	const char *dir = (msg->ttype == HSI_MSG_READ) ? "TX" : "RX";
311 
312 	dev_dbg(&cs_char_data.cl->device, "Freeing data %s message\n", dir);
313 
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)
319 		hi->data_state &=
320 			~(SSI_CHANNEL_STATE_POLL | SSI_CHANNEL_STATE_READING);
321 	else
322 		hi->data_state &= ~SSI_CHANNEL_STATE_WRITING;
323 
324 	msg->status = HSI_STATUS_COMPLETED;
325 	if (unlikely(waitqueue_active(&hi->datawait)))
326 		wake_up_interruptible(&hi->datawait);
327 
328 	spin_unlock(&hi->lock);
329 }
330 
331 static int cs_hsi_alloc_data(struct cs_hsi_iface *hi)
332 {
333 	struct hsi_msg *txmsg, *rxmsg;
334 	int res = 0;
335 
336 	rxmsg = hsi_alloc_msg(1, GFP_KERNEL);
337 	if (!rxmsg) {
338 		res = -ENOMEM;
339 		goto out1;
340 	}
341 	rxmsg->channel = cs_char_data.channel_id_data;
342 	rxmsg->destructor = cs_hsi_data_destructor;
343 	rxmsg->context = hi;
344 
345 	txmsg = hsi_alloc_msg(1, GFP_KERNEL);
346 	if (!txmsg) {
347 		res = -ENOMEM;
348 		goto out2;
349 	}
350 	txmsg->channel = cs_char_data.channel_id_data;
351 	txmsg->destructor = cs_hsi_data_destructor;
352 	txmsg->context = hi;
353 
354 	hi->data_rx_msg = rxmsg;
355 	hi->data_tx_msg = txmsg;
356 
357 	return 0;
358 
359 out2:
360 	hsi_free_msg(rxmsg);
361 out1:
362 	return res;
363 }
364 
365 static void cs_hsi_free_data_msg(struct hsi_msg *msg)
366 {
367 	WARN_ON(msg->status != HSI_STATUS_COMPLETED &&
368 					msg->status != HSI_STATUS_ERROR);
369 	hsi_free_msg(msg);
370 }
371 
372 static void cs_hsi_free_data(struct cs_hsi_iface *hi)
373 {
374 	cs_hsi_free_data_msg(hi->data_rx_msg);
375 	cs_hsi_free_data_msg(hi->data_tx_msg);
376 }
377 
378 static inline void __cs_hsi_error_pre(struct cs_hsi_iface *hi,
379 					struct hsi_msg *msg, const char *info,
380 					unsigned int *state)
381 {
382 	spin_lock(&hi->lock);
383 	dev_err(&hi->cl->device, "HSI %s error, msg %d, state %u\n",
384 		info, msg->status, *state);
385 }
386 
387 static inline void __cs_hsi_error_post(struct cs_hsi_iface *hi)
388 {
389 	spin_unlock(&hi->lock);
390 }
391 
392 static inline void __cs_hsi_error_read_bits(unsigned int *state)
393 {
394 	*state |= SSI_CHANNEL_STATE_ERROR;
395 	*state &= ~(SSI_CHANNEL_STATE_READING | SSI_CHANNEL_STATE_POLL);
396 }
397 
398 static inline void __cs_hsi_error_write_bits(unsigned int *state)
399 {
400 	*state |= SSI_CHANNEL_STATE_ERROR;
401 	*state &= ~SSI_CHANNEL_STATE_WRITING;
402 }
403 
404 static void cs_hsi_control_read_error(struct cs_hsi_iface *hi,
405 							struct hsi_msg *msg)
406 {
407 	__cs_hsi_error_pre(hi, msg, "control read", &hi->control_state);
408 	cs_release_cmd(msg);
409 	__cs_hsi_error_read_bits(&hi->control_state);
410 	__cs_hsi_error_post(hi);
411 }
412 
413 static void cs_hsi_control_write_error(struct cs_hsi_iface *hi,
414 							struct hsi_msg *msg)
415 {
416 	__cs_hsi_error_pre(hi, msg, "control write", &hi->control_state);
417 	cs_release_cmd(msg);
418 	__cs_hsi_error_write_bits(&hi->control_state);
419 	__cs_hsi_error_post(hi);
420 
421 }
422 
423 static void cs_hsi_data_read_error(struct cs_hsi_iface *hi, struct hsi_msg *msg)
424 {
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);
428 }
429 
430 static void cs_hsi_data_write_error(struct cs_hsi_iface *hi,
431 							struct hsi_msg *msg)
432 {
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);
436 }
437 
438 static void cs_hsi_read_on_control_complete(struct hsi_msg *msg)
439 {
440 	u32 cmd = cs_get_cmd(msg);
441 	struct cs_hsi_iface *hi = msg->context;
442 
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);
449 		goto out;
450 	}
451 	dev_dbg(&hi->cl->device, "Read on control: %08X\n", cmd);
452 	cs_release_cmd(msg);
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);
457 	}
458 	spin_unlock(&hi->lock);
459 
460 	cs_notify_control(cmd);
461 
462 out:
463 	cs_hsi_read_on_control(hi);
464 }
465 
466 static void cs_hsi_peek_on_control_complete(struct hsi_msg *msg)
467 {
468 	struct cs_hsi_iface *hi = msg->context;
469 	int ret;
470 
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);
474 		return;
475 	}
476 
477 	WARN_ON(!(hi->control_state & SSI_CHANNEL_STATE_READING));
478 
479 	dev_dbg(&hi->cl->device, "Peek on control complete, reading\n");
480 	msg->sgt.nents = 1;
481 	msg->complete = cs_hsi_read_on_control_complete;
482 	ret = hsi_async_read(hi->cl, msg);
483 	if (ret)
484 		cs_hsi_control_read_error(hi, msg);
485 }
486 
487 static void cs_hsi_read_on_control(struct cs_hsi_iface *hi)
488 {
489 	struct hsi_msg *msg;
490 	int ret;
491 
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",
495 			hi->control_state);
496 		spin_unlock(&hi->lock);
497 		return;
498 	}
499 	if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
500 		dev_err(&hi->cl->device, "Control read error (%d)\n",
501 			hi->control_state);
502 		spin_unlock(&hi->lock);
503 		return;
504 	}
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);
509 
510 	msg->sgt.nents = 0;
511 	msg->complete = cs_hsi_peek_on_control_complete;
512 	ret = hsi_async_read(hi->cl, msg);
513 	if (ret)
514 		cs_hsi_control_read_error(hi, msg);
515 }
516 
517 static void cs_hsi_write_on_control_complete(struct hsi_msg *msg)
518 {
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;
523 		cs_release_cmd(msg);
524 		spin_unlock(&hi->lock);
525 	} else if (msg->status == HSI_STATUS_ERROR) {
526 		cs_hsi_control_write_error(hi, msg);
527 	} else {
528 		dev_err(&hi->cl->device,
529 			"unexpected status in control write callback %d\n",
530 			msg->status);
531 	}
532 }
533 
534 static int cs_hsi_write_on_control(struct cs_hsi_iface *hi, u32 message)
535 {
536 	struct hsi_msg *msg;
537 	int ret;
538 
539 	spin_lock(&hi->lock);
540 	if (hi->control_state & SSI_CHANNEL_STATE_ERROR) {
541 		spin_unlock(&hi->lock);
542 		return -EIO;
543 	}
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);
548 		return -EBUSY;
549 	}
550 	hi->control_state |= SSI_CHANNEL_STATE_WRITING;
551 	msg = cs_claim_cmd(hi);
552 	spin_unlock(&hi->lock);
553 
554 	cs_set_cmd(msg, message);
555 	msg->sgt.nents = 1;
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);
560 	if (ret) {
561 		dev_err(&hi->cl->device,
562 			"async_write failed with %d\n", ret);
563 		cs_hsi_control_write_error(hi, msg);
564 	}
565 
566 	/*
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()).
572 	 */
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);
576 	}
577 
578 	return 0;
579 }
580 
581 static void cs_hsi_read_on_data_complete(struct hsi_msg *msg)
582 {
583 	struct cs_hsi_iface *hi = msg->context;
584 	u32 payload;
585 
586 	if (unlikely(msg->status == HSI_STATUS_ERROR)) {
587 		cs_hsi_data_read_error(hi, msg);
588 		return;
589 	}
590 
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;
596 	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);
603 
604 	cs_notify_data(payload, hi->rx_bufs);
605 	cs_hsi_read_on_data(hi);
606 }
607 
608 static void cs_hsi_peek_on_data_complete(struct hsi_msg *msg)
609 {
610 	struct cs_hsi_iface *hi = msg->context;
611 	u32 *address;
612 	int ret;
613 
614 	if (unlikely(msg->status == HSI_STATUS_ERROR)) {
615 		cs_hsi_data_read_error(hi, msg);
616 		return;
617 	}
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);
621 		return;
622 	}
623 
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);
629 
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);
633 	msg->sgt.nents = 1;
634 	msg->complete = cs_hsi_read_on_data_complete;
635 	ret = hsi_async_read(hi->cl, msg);
636 	if (ret)
637 		cs_hsi_data_read_error(hi, msg);
638 }
639 
640 /*
641  * Read/write transaction is ongoing. Returns false if in
642  * SSI_CHANNEL_STATE_POLL state.
643  */
644 static inline int cs_state_xfer_active(unsigned int state)
645 {
646 	return (state & SSI_CHANNEL_STATE_WRITING) ||
647 		(state & SSI_CHANNEL_STATE_READING);
648 }
649 
650 /*
651  * No pending read/writes
652  */
653 static inline int cs_state_idle(unsigned int state)
654 {
655 	return !(state & ~SSI_CHANNEL_STATE_ERROR);
656 }
657 
658 static void cs_hsi_read_on_data(struct cs_hsi_iface *hi)
659 {
660 	struct hsi_msg *rxmsg;
661 	int ret;
662 
663 	spin_lock(&hi->lock);
664 	if (hi->data_state &
665 		(SSI_CHANNEL_STATE_READING | SSI_CHANNEL_STATE_POLL)) {
666 		dev_dbg(&hi->cl->device, "Data read already pending (%u)\n",
667 			hi->data_state);
668 		spin_unlock(&hi->lock);
669 		return;
670 	}
671 	hi->data_state |= SSI_CHANNEL_STATE_POLL;
672 	spin_unlock(&hi->lock);
673 
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;
678 
679 	ret = hsi_async_read(hi->cl, rxmsg);
680 	if (ret)
681 		cs_hsi_data_read_error(hi, rxmsg);
682 }
683 
684 static void cs_hsi_write_on_data_complete(struct hsi_msg *msg)
685 {
686 	struct cs_hsi_iface *hi = msg->context;
687 
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);
694 	} else {
695 		cs_hsi_data_write_error(hi, msg);
696 	}
697 }
698 
699 static int cs_hsi_write_on_data(struct cs_hsi_iface *hi, unsigned int slot)
700 {
701 	u32 *address;
702 	struct hsi_msg *txmsg;
703 	int ret;
704 
705 	spin_lock(&hi->lock);
706 	if (hi->iface_state != CS_STATE_CONFIGURED) {
707 		dev_err(&hi->cl->device, "Not configured, aborting\n");
708 		ret = -EINVAL;
709 		goto error;
710 	}
711 	if (hi->data_state & SSI_CHANNEL_STATE_ERROR) {
712 		dev_err(&hi->cl->device, "HSI error, aborting\n");
713 		ret = -EIO;
714 		goto error;
715 	}
716 	if (hi->data_state & SSI_CHANNEL_STATE_WRITING) {
717 		dev_err(&hi->cl->device, "Write pending on data channel.\n");
718 		ret = -EBUSY;
719 		goto error;
720 	}
721 	hi->data_state |= SSI_CHANNEL_STATE_WRITING;
722 	spin_unlock(&hi->lock);
723 
724 	hi->tx_slot = slot;
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);
730 	if (ret)
731 		cs_hsi_data_write_error(hi, txmsg);
732 
733 	return ret;
734 
735 error:
736 	spin_unlock(&hi->lock);
737 	if (ret == -EIO)
738 		cs_hsi_data_write_error(hi, hi->data_tx_msg);
739 
740 	return ret;
741 }
742 
743 static unsigned int cs_hsi_get_state(struct cs_hsi_iface *hi)
744 {
745 	return hi->iface_state;
746 }
747 
748 static int cs_hsi_command(struct cs_hsi_iface *hi, u32 cmd)
749 {
750 	int ret = 0;
751 
752 	local_bh_disable();
753 	switch (cmd & TARGET_MASK) {
754 	case TARGET_REMOTE:
755 		ret = cs_hsi_write_on_control(hi, cmd);
756 		break;
757 	case TARGET_LOCAL:
758 		if ((cmd & CS_CMD_MASK) == CS_TX_DATA_READY)
759 			ret = cs_hsi_write_on_data(hi, cmd & CS_PARAM_MASK);
760 		else
761 			ret = -EINVAL;
762 		break;
763 	default:
764 		ret = -EINVAL;
765 		break;
766 	}
767 	local_bh_enable();
768 
769 	return ret;
770 }
771 
772 static void cs_hsi_set_wakeline(struct cs_hsi_iface *hi, bool new_state)
773 {
774 	int change = 0;
775 
776 	spin_lock_bh(&hi->lock);
777 	if (hi->wakeline_state != new_state) {
778 		hi->wakeline_state = new_state;
779 		change = 1;
780 		dev_dbg(&hi->cl->device, "setting wake line to %d (%p)\n",
781 			new_state, hi->cl);
782 	}
783 	spin_unlock_bh(&hi->lock);
784 
785 	if (change) {
786 		if (new_state)
787 			ssip_slave_start_tx(hi->master);
788 		else
789 			ssip_slave_stop_tx(hi->master);
790 	}
791 
792 	dev_dbg(&hi->cl->device, "wake line set to %d (%p)\n",
793 		new_state, hi->cl);
794 }
795 
796 static void set_buffer_sizes(struct cs_hsi_iface *hi, int rx_bufs, int tx_bufs)
797 {
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;
802 
803 	if (hi->flags & CS_FEAT_ROLLING_RX_COUNTER) {
804 		/*
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
809 		 * calculation.
810 		 */
811 		hi->rx_ptr_boundary = (rx_bufs << RX_PTR_BOUNDARY_SHIFT);
812 		hi->mmap_cfg->rx_ptr_boundary = hi->rx_ptr_boundary;
813 	} else {
814 		hi->rx_ptr_boundary = hi->rx_bufs;
815 	}
816 }
817 
818 static int check_buf_params(struct cs_hsi_iface *hi,
819 					const struct cs_buffer_config *buf_cfg)
820 {
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));
824 	int r = 0;
825 
826 	if (buf_cfg->rx_bufs > CS_MAX_BUFFERS ||
827 					buf_cfg->tx_bufs > CS_MAX_BUFFERS) {
828 		r = -EINVAL;
829 	} else if ((buf_size_aligned + ctrl_size_aligned) >= hi->mmap_size) {
830 		dev_err(&hi->cl->device, "No space for the requested buffer "
831 			"configuration\n");
832 		r = -ENOBUFS;
833 	}
834 
835 	return r;
836 }
837 
838 /**
839  * Block until pending data transfers have completed.
840  */
841 static int cs_hsi_data_sync(struct cs_hsi_iface *hi)
842 {
843 	int r = 0;
844 
845 	spin_lock_bh(&hi->lock);
846 
847 	if (!cs_state_xfer_active(hi->data_state)) {
848 		dev_dbg(&hi->cl->device, "hsi_data_sync break, idle\n");
849 		goto out;
850 	}
851 
852 	for (;;) {
853 		int s;
854 		DEFINE_WAIT(wait);
855 		if (!cs_state_xfer_active(hi->data_state))
856 			goto out;
857 		if (signal_pending(current)) {
858 			r = -ERESTARTSYS;
859 			goto out;
860 		}
861 		/**
862 		 * prepare_to_wait must be called with hi->lock held
863 		 * so that callbacks can check for waitqueue_active()
864 		 */
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);
871 		if (!s) {
872 			dev_dbg(&hi->cl->device,
873 				"hsi_data_sync timeout after %d ms\n",
874 				CS_HSI_TRANSFER_TIMEOUT_MS);
875 			r = -EIO;
876 			goto out;
877 		}
878 	}
879 
880 out:
881 	spin_unlock_bh(&hi->lock);
882 	dev_dbg(&hi->cl->device, "hsi_data_sync done with res %d\n", r);
883 
884 	return r;
885 }
886 
887 static void cs_hsi_data_enable(struct cs_hsi_iface *hi,
888 					struct cs_buffer_config *buf_cfg)
889 {
890 	unsigned int data_start, i;
891 
892 	BUG_ON(hi->buf_size == 0);
893 
894 	set_buffer_sizes(hi, buf_cfg->rx_bufs, buf_cfg->tx_bufs);
895 
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);
900 
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);
905 
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]);
911 	}
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]);
918 	}
919 
920 	hi->iface_state = CS_STATE_CONFIGURED;
921 }
922 
923 static void cs_hsi_data_disable(struct cs_hsi_iface *hi, int old_state)
924 {
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;
929 	}
930 }
931 
932 static int cs_hsi_buf_config(struct cs_hsi_iface *hi,
933 					struct cs_buffer_config *buf_cfg)
934 {
935 	int r = 0;
936 	unsigned int old_state = hi->iface_state;
937 
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);
943 
944 	/*
945 	 * make sure that no non-zero data reads are ongoing before
946 	 * proceeding to change the buffer layout
947 	 */
948 	r = cs_hsi_data_sync(hi);
949 	if (r < 0)
950 		return r;
951 
952 	WARN_ON(cs_state_xfer_active(hi->data_state));
953 
954 	spin_lock_bh(&hi->lock);
955 	r = check_buf_params(hi, buf_cfg);
956 	if (r < 0)
957 		goto error;
958 
959 	hi->buf_size = buf_cfg->buf_size;
960 	hi->mmap_cfg->buf_size = hi->buf_size;
961 	hi->flags = buf_cfg->flags;
962 
963 	hi->rx_slot = 0;
964 	hi->tx_slot = 0;
965 	hi->slot_size = 0;
966 
967 	if (hi->buf_size)
968 		cs_hsi_data_enable(hi, buf_cfg);
969 	else
970 		cs_hsi_data_disable(hi, old_state);
971 
972 	spin_unlock_bh(&hi->lock);
973 
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);
979 			local_bh_disable();
980 			cs_hsi_read_on_data(hi);
981 			local_bh_enable();
982 		} else if (old_state == CS_STATE_CONFIGURED) {
983 			pm_qos_remove_request(&hi->pm_qos_req);
984 		}
985 	}
986 	return r;
987 
988 error:
989 	spin_unlock_bh(&hi->lock);
990 	return r;
991 }
992 
993 static int cs_hsi_start(struct cs_hsi_iface **hi, struct hsi_client *cl,
994 			unsigned long mmap_base, unsigned long mmap_size)
995 {
996 	int err = 0;
997 	struct cs_hsi_iface *hsi_if = kzalloc(sizeof(*hsi_if), GFP_KERNEL);
998 
999 	dev_dbg(&cl->device, "cs_hsi_start\n");
1000 
1001 	if (!hsi_if) {
1002 		err = -ENOMEM;
1003 		goto leave0;
1004 	}
1005 	spin_lock_init(&hsi_if->lock);
1006 	hsi_if->cl = cl;
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);
1014 	if (err < 0) {
1015 		dev_err(&cl->device, "Unable to alloc HSI messages\n");
1016 		goto leave1;
1017 	}
1018 	err = cs_hsi_alloc_data(hsi_if);
1019 	if (err < 0) {
1020 		dev_err(&cl->device, "Unable to alloc HSI messages for data\n");
1021 		goto leave2;
1022 	}
1023 	err = hsi_claim_port(cl, 1);
1024 	if (err < 0) {
1025 		dev_err(&cl->device,
1026 				"Could not open, HSI port already claimed\n");
1027 		goto leave3;
1028 	}
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");
1033 		goto leave4;
1034 	}
1035 	if (!ssip_slave_running(hsi_if->master)) {
1036 		err = -ENODEV;
1037 		dev_err(&cl->device,
1038 				"HSI port not initialized\n");
1039 		goto leave4;
1040 	}
1041 
1042 	hsi_if->iface_state = CS_STATE_OPENED;
1043 	local_bh_disable();
1044 	cs_hsi_read_on_control(hsi_if);
1045 	local_bh_enable();
1046 
1047 	dev_dbg(&cl->device, "cs_hsi_start...done\n");
1048 
1049 	BUG_ON(!hi);
1050 	*hi = hsi_if;
1051 
1052 	return 0;
1053 
1054 leave4:
1055 	hsi_release_port(cl);
1056 leave3:
1057 	cs_hsi_free_data(hsi_if);
1058 leave2:
1059 	cs_free_cmds(hsi_if);
1060 leave1:
1061 	kfree(hsi_if);
1062 leave0:
1063 	dev_dbg(&cl->device, "cs_hsi_start...done/error\n\n");
1064 
1065 	return err;
1066 }
1067 
1068 static void cs_hsi_stop(struct cs_hsi_iface *hi)
1069 {
1070 	dev_dbg(&hi->cl->device, "cs_hsi_stop\n");
1071 	cs_hsi_set_wakeline(hi, 0);
1072 	ssip_slave_put_master(hi->master);
1073 
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);
1077 
1078 	/*
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.
1082 	 */
1083 	WARN_ON(!cs_state_idle(hi->control_state));
1084 	WARN_ON(!cs_state_idle(hi->data_state));
1085 
1086 	if (pm_qos_request_active(&hi->pm_qos_req))
1087 		pm_qos_remove_request(&hi->pm_qos_req);
1088 
1089 	spin_lock_bh(&hi->lock);
1090 	cs_hsi_free_data(hi);
1091 	cs_free_cmds(hi);
1092 	spin_unlock_bh(&hi->lock);
1093 	kfree(hi);
1094 }
1095 
1096 static int cs_char_vma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1097 {
1098 	struct cs_char *csdata = vma->vm_private_data;
1099 	struct page *page;
1100 
1101 	page = virt_to_page(csdata->mmap_base);
1102 	get_page(page);
1103 	vmf->page = page;
1104 
1105 	return 0;
1106 }
1107 
1108 static struct vm_operations_struct cs_char_vm_ops = {
1109 	.fault	= cs_char_vma_fault,
1110 };
1111 
1112 static int cs_char_fasync(int fd, struct file *file, int on)
1113 {
1114 	struct cs_char *csdata = file->private_data;
1115 
1116 	if (fasync_helper(fd, file, on, &csdata->async_queue) < 0)
1117 		return -EIO;
1118 
1119 	return 0;
1120 }
1121 
1122 static unsigned int cs_char_poll(struct file *file, poll_table *wait)
1123 {
1124 	struct cs_char *csdata = file->private_data;
1125 	unsigned int ret = 0;
1126 
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);
1134 
1135 	return ret;
1136 }
1137 
1138 static ssize_t cs_char_read(struct file *file, char __user *buf, size_t count,
1139 								loff_t *unused)
1140 {
1141 	struct cs_char *csdata = file->private_data;
1142 	u32 data;
1143 	ssize_t retval;
1144 
1145 	if (count < sizeof(data))
1146 		return -EINVAL;
1147 
1148 	for (;;) {
1149 		DEFINE_WAIT(wait);
1150 
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--;
1157 		} else {
1158 			data = 0;
1159 		}
1160 		spin_unlock_bh(&csdata->lock);
1161 
1162 		if (data)
1163 			break;
1164 		if (file->f_flags & O_NONBLOCK) {
1165 			retval = -EAGAIN;
1166 			goto out;
1167 		} else if (signal_pending(current)) {
1168 			retval = -ERESTARTSYS;
1169 			goto out;
1170 		}
1171 		prepare_to_wait_exclusive(&csdata->wait, &wait,
1172 						TASK_INTERRUPTIBLE);
1173 		schedule();
1174 		finish_wait(&csdata->wait, &wait);
1175 	}
1176 
1177 	retval = put_user(data, (u32 __user *)buf);
1178 	if (!retval)
1179 		retval = sizeof(data);
1180 
1181 out:
1182 	return retval;
1183 }
1184 
1185 static ssize_t cs_char_write(struct file *file, const char __user *buf,
1186 						size_t count, loff_t *unused)
1187 {
1188 	struct cs_char *csdata = file->private_data;
1189 	u32 data;
1190 	int err;
1191 	ssize_t	retval;
1192 
1193 	if (count < sizeof(data))
1194 		return -EINVAL;
1195 
1196 	if (get_user(data, (u32 __user *)buf))
1197 		retval = -EFAULT;
1198 	else
1199 		retval = count;
1200 
1201 	err = cs_hsi_command(csdata->hi, data);
1202 	if (err < 0)
1203 		retval = err;
1204 
1205 	return retval;
1206 }
1207 
1208 static long cs_char_ioctl(struct file *file, unsigned int cmd,
1209 				unsigned long arg)
1210 {
1211 	struct cs_char *csdata = file->private_data;
1212 	int r = 0;
1213 
1214 	switch (cmd) {
1215 	case CS_GET_STATE: {
1216 		unsigned int state;
1217 
1218 		state = cs_hsi_get_state(csdata->hi);
1219 		if (copy_to_user((void __user *)arg, &state, sizeof(state)))
1220 			r = -EFAULT;
1221 
1222 		break;
1223 	}
1224 	case CS_SET_WAKELINE: {
1225 		unsigned int state;
1226 
1227 		if (copy_from_user(&state, (void __user *)arg, sizeof(state))) {
1228 			r = -EFAULT;
1229 			break;
1230 		}
1231 
1232 		if (state > 1) {
1233 			r = -EINVAL;
1234 			break;
1235 		}
1236 
1237 		cs_hsi_set_wakeline(csdata->hi, !!state);
1238 
1239 		break;
1240 	}
1241 	case CS_GET_IF_VERSION: {
1242 		unsigned int ifver = CS_IF_VERSION;
1243 
1244 		if (copy_to_user((void __user *)arg, &ifver, sizeof(ifver)))
1245 			r = -EFAULT;
1246 
1247 		break;
1248 	}
1249 	case CS_CONFIG_BUFS: {
1250 		struct cs_buffer_config buf_cfg;
1251 
1252 		if (copy_from_user(&buf_cfg, (void __user *)arg,
1253 							sizeof(buf_cfg)))
1254 			r = -EFAULT;
1255 		else
1256 			r = cs_hsi_buf_config(csdata->hi, &buf_cfg);
1257 
1258 		break;
1259 	}
1260 	default:
1261 		r = -ENOTTY;
1262 		break;
1263 	}
1264 
1265 	return r;
1266 }
1267 
1268 static int cs_char_mmap(struct file *file, struct vm_area_struct *vma)
1269 {
1270 	if (vma->vm_end < vma->vm_start)
1271 		return -EINVAL;
1272 
1273 	if (((vma->vm_end - vma->vm_start) >> PAGE_SHIFT) != 1)
1274 		return -EINVAL;
1275 
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;
1279 
1280 	return 0;
1281 }
1282 
1283 static int cs_char_open(struct inode *unused, struct file *file)
1284 {
1285 	int ret = 0;
1286 	unsigned long p;
1287 
1288 	spin_lock_bh(&cs_char_data.lock);
1289 	if (cs_char_data.opened) {
1290 		ret = -EBUSY;
1291 		spin_unlock_bh(&cs_char_data.lock);
1292 		goto out1;
1293 	}
1294 	cs_char_data.opened = 1;
1295 	cs_char_data.dataind_pending = 0;
1296 	spin_unlock_bh(&cs_char_data.lock);
1297 
1298 	p = get_zeroed_page(GFP_KERNEL);
1299 	if (!p) {
1300 		ret = -ENOMEM;
1301 		goto out2;
1302 	}
1303 
1304 	ret = cs_hsi_start(&cs_char_data.hi, cs_char_data.cl, p, CS_MMAP_SIZE);
1305 	if (ret) {
1306 		dev_err(&cs_char_data.cl->device, "Unable to initialize HSI\n");
1307 		goto out3;
1308 	}
1309 
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;
1313 
1314 	file->private_data = &cs_char_data;
1315 
1316 	return 0;
1317 
1318 out3:
1319 	free_page(p);
1320 out2:
1321 	spin_lock_bh(&cs_char_data.lock);
1322 	cs_char_data.opened = 0;
1323 	spin_unlock_bh(&cs_char_data.lock);
1324 out1:
1325 	return ret;
1326 }
1327 
1328 static void cs_free_char_queue(struct list_head *head)
1329 {
1330 	struct char_queue *entry;
1331 	struct list_head *cursor, *next;
1332 
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);
1337 			kfree(entry);
1338 		}
1339 	}
1340 
1341 }
1342 
1343 static int cs_char_release(struct inode *unused, struct file *file)
1344 {
1345 	struct cs_char *csdata = file->private_data;
1346 
1347 	cs_hsi_stop(csdata->hi);
1348 	spin_lock_bh(&csdata->lock);
1349 	csdata->hi = NULL;
1350 	free_page(csdata->mmap_base);
1351 	cs_free_char_queue(&csdata->chardev_queue);
1352 	cs_free_char_queue(&csdata->dataind_queue);
1353 	csdata->opened = 0;
1354 	spin_unlock_bh(&csdata->lock);
1355 
1356 	return 0;
1357 }
1358 
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,
1369 };
1370 
1371 static struct miscdevice cs_char_miscdev = {
1372 	.minor	= MISC_DYNAMIC_MINOR,
1373 	.name	= "cmt_speech",
1374 	.fops	= &cs_char_fops
1375 };
1376 
1377 static int cs_hsi_client_probe(struct device *dev)
1378 {
1379 	int err = 0;
1380 	struct hsi_client *cl = to_hsi_client(dev);
1381 
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);
1390 
1391 	cs_char_data.channel_id_cmd = hsi_get_channel_id_by_name(cl,
1392 		"speech-control");
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);
1396 		return err;
1397 	}
1398 
1399 	cs_char_data.channel_id_data = hsi_get_channel_id_by_name(cl,
1400 		"speech-data");
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);
1404 		return err;
1405 	}
1406 
1407 	err = misc_register(&cs_char_miscdev);
1408 	if (err)
1409 		dev_err(dev, "Failed to register: %d\n", err);
1410 
1411 	return err;
1412 }
1413 
1414 static int cs_hsi_client_remove(struct device *dev)
1415 {
1416 	struct cs_hsi_iface *hi;
1417 
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);
1424 	if (hi)
1425 		cs_hsi_stop(hi);
1426 
1427 	return 0;
1428 }
1429 
1430 static struct hsi_client_driver cs_hsi_driver = {
1431 	.driver = {
1432 		.name	= "cmt-speech",
1433 		.owner	= THIS_MODULE,
1434 		.probe	= cs_hsi_client_probe,
1435 		.remove	= cs_hsi_client_remove,
1436 	},
1437 };
1438 
1439 static int __init cs_char_init(void)
1440 {
1441 	pr_info("CMT speech driver added\n");
1442 	return hsi_register_client_driver(&cs_hsi_driver);
1443 }
1444 module_init(cs_char_init);
1445 
1446 static void __exit cs_char_exit(void)
1447 {
1448 	hsi_unregister_client_driver(&cs_hsi_driver);
1449 	pr_info("CMT speech driver removed\n");
1450 }
1451 module_exit(cs_char_exit);
1452 
1453 MODULE_ALIAS("hsi:cmt-speech");
1454 MODULE_AUTHOR("Kai Vehmanen <kai.vehmanen@nokia.com>");
1455 MODULE_AUTHOR("Peter Ujfalusi <peter.ujfalusi@nokia.com>");
1456 MODULE_DESCRIPTION("CMT speech driver");
1457 MODULE_LICENSE("GPL v2");
1458