1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Shared Transport Line discipline driver Core
4  *	This hooks up ST KIM driver and ST LL driver
5  *  Copyright (C) 2009-2010 Texas Instruments
6  *  Author: Pavan Savoy <pavan_savoy@ti.com>
7  */
8 
9 #define pr_fmt(fmt)	"(stc): " fmt
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/tty.h>
13 
14 #include <linux/seq_file.h>
15 #include <linux/skbuff.h>
16 
17 #include <linux/ti_wilink_st.h>
18 
19 extern void st_kim_recv(void *, const unsigned char *, long);
20 void st_int_recv(void *, const unsigned char *, long);
21 /* function pointer pointing to either,
22  * st_kim_recv during registration to receive fw download responses
23  * st_int_recv after registration to receive proto stack responses
24  */
25 static void (*st_recv) (void *, const unsigned char *, long);
26 
27 /********************************************************************/
add_channel_to_table(struct st_data_s * st_gdata,struct st_proto_s * new_proto)28 static void add_channel_to_table(struct st_data_s *st_gdata,
29 		struct st_proto_s *new_proto)
30 {
31 	pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
32 	/* list now has the channel id as index itself */
33 	st_gdata->list[new_proto->chnl_id] = new_proto;
34 	st_gdata->is_registered[new_proto->chnl_id] = true;
35 }
36 
remove_channel_from_table(struct st_data_s * st_gdata,struct st_proto_s * proto)37 static void remove_channel_from_table(struct st_data_s *st_gdata,
38 		struct st_proto_s *proto)
39 {
40 	pr_info("%s: id %d\n", __func__, proto->chnl_id);
41 /*	st_gdata->list[proto->chnl_id] = NULL; */
42 	st_gdata->is_registered[proto->chnl_id] = false;
43 }
44 
45 /*
46  * called from KIM during firmware download.
47  *
48  * This is a wrapper function to tty->ops->write_room.
49  * It returns number of free space available in
50  * uart tx buffer.
51  */
st_get_uart_wr_room(struct st_data_s * st_gdata)52 int st_get_uart_wr_room(struct st_data_s *st_gdata)
53 {
54 	struct tty_struct *tty;
55 	if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
56 		pr_err("tty unavailable to perform write");
57 		return -1;
58 	}
59 	tty = st_gdata->tty;
60 	return tty->ops->write_room(tty);
61 }
62 
63 /* can be called in from
64  * -- KIM (during fw download)
65  * -- ST Core (during st_write)
66  *
67  *  This is the internal write function - a wrapper
68  *  to tty->ops->write
69  */
st_int_write(struct st_data_s * st_gdata,const unsigned char * data,int count)70 int st_int_write(struct st_data_s *st_gdata,
71 	const unsigned char *data, int count)
72 {
73 	struct tty_struct *tty;
74 	if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
75 		pr_err("tty unavailable to perform write");
76 		return -EINVAL;
77 	}
78 	tty = st_gdata->tty;
79 #ifdef VERBOSE
80 	print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
81 		16, 1, data, count, 0);
82 #endif
83 	return tty->ops->write(tty, data, count);
84 
85 }
86 
87 /*
88  * push the skb received to relevant
89  * protocol stacks
90  */
st_send_frame(unsigned char chnl_id,struct st_data_s * st_gdata)91 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
92 {
93 	pr_debug(" %s(prot:%d) ", __func__, chnl_id);
94 
95 	if (unlikely
96 	    (st_gdata == NULL || st_gdata->rx_skb == NULL
97 	     || st_gdata->is_registered[chnl_id] == false)) {
98 		pr_err("chnl_id %d not registered, no data to send?",
99 			   chnl_id);
100 		kfree_skb(st_gdata->rx_skb);
101 		return;
102 	}
103 	/* this cannot fail
104 	 * this shouldn't take long
105 	 * - should be just skb_queue_tail for the
106 	 *   protocol stack driver
107 	 */
108 	if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
109 		if (unlikely
110 			(st_gdata->list[chnl_id]->recv
111 			(st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
112 			     != 0)) {
113 			pr_err(" proto stack %d's ->recv failed", chnl_id);
114 			kfree_skb(st_gdata->rx_skb);
115 			return;
116 		}
117 	} else {
118 		pr_err(" proto stack %d's ->recv null", chnl_id);
119 		kfree_skb(st_gdata->rx_skb);
120 	}
121 	return;
122 }
123 
124 /**
125  * st_reg_complete -
126  * to call registration complete callbacks
127  * of all protocol stack drivers
128  * This function is being called with spin lock held, protocol drivers are
129  * only expected to complete their waits and do nothing more than that.
130  */
st_reg_complete(struct st_data_s * st_gdata,int err)131 static void st_reg_complete(struct st_data_s *st_gdata, int err)
132 {
133 	unsigned char i = 0;
134 	pr_info(" %s ", __func__);
135 	for (i = 0; i < ST_MAX_CHANNELS; i++) {
136 		if (likely(st_gdata != NULL &&
137 			st_gdata->is_registered[i] == true &&
138 				st_gdata->list[i]->reg_complete_cb != NULL)) {
139 			st_gdata->list[i]->reg_complete_cb
140 				(st_gdata->list[i]->priv_data, err);
141 			pr_info("protocol %d's cb sent %d\n", i, err);
142 			if (err) { /* cleanup registered protocol */
143 				st_gdata->is_registered[i] = false;
144 				if (st_gdata->protos_registered)
145 					st_gdata->protos_registered--;
146 			}
147 		}
148 	}
149 }
150 
st_check_data_len(struct st_data_s * st_gdata,unsigned char chnl_id,int len)151 static inline int st_check_data_len(struct st_data_s *st_gdata,
152 	unsigned char chnl_id, int len)
153 {
154 	int room = skb_tailroom(st_gdata->rx_skb);
155 
156 	pr_debug("len %d room %d", len, room);
157 
158 	if (!len) {
159 		/* Received packet has only packet header and
160 		 * has zero length payload. So, ask ST CORE to
161 		 * forward the packet to protocol driver (BT/FM/GPS)
162 		 */
163 		st_send_frame(chnl_id, st_gdata);
164 
165 	} else if (len > room) {
166 		/* Received packet's payload length is larger.
167 		 * We can't accommodate it in created skb.
168 		 */
169 		pr_err("Data length is too large len %d room %d", len,
170 			   room);
171 		kfree_skb(st_gdata->rx_skb);
172 	} else {
173 		/* Packet header has non-zero payload length and
174 		 * we have enough space in created skb. Lets read
175 		 * payload data */
176 		st_gdata->rx_state = ST_W4_DATA;
177 		st_gdata->rx_count = len;
178 		return len;
179 	}
180 
181 	/* Change ST state to continue to process next
182 	 * packet */
183 	st_gdata->rx_state = ST_W4_PACKET_TYPE;
184 	st_gdata->rx_skb = NULL;
185 	st_gdata->rx_count = 0;
186 	st_gdata->rx_chnl = 0;
187 
188 	return 0;
189 }
190 
191 /**
192  * st_wakeup_ack - internal function for action when wake-up ack
193  *	received
194  */
st_wakeup_ack(struct st_data_s * st_gdata,unsigned char cmd)195 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
196 	unsigned char cmd)
197 {
198 	struct sk_buff *waiting_skb;
199 	unsigned long flags = 0;
200 
201 	spin_lock_irqsave(&st_gdata->lock, flags);
202 	/* de-Q from waitQ and Q in txQ now that the
203 	 * chip is awake
204 	 */
205 	while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
206 		skb_queue_tail(&st_gdata->txq, waiting_skb);
207 
208 	/* state forwarded to ST LL */
209 	st_ll_sleep_state(st_gdata, (unsigned long)cmd);
210 	spin_unlock_irqrestore(&st_gdata->lock, flags);
211 
212 	/* wake up to send the recently copied skbs from waitQ */
213 	st_tx_wakeup(st_gdata);
214 }
215 
216 /**
217  * st_int_recv - ST's internal receive function.
218  *	Decodes received RAW data and forwards to corresponding
219  *	client drivers (Bluetooth,FM,GPS..etc).
220  *	This can receive various types of packets,
221  *	HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
222  *	CH-8 packets from FM, CH-9 packets from GPS cores.
223  */
st_int_recv(void * disc_data,const unsigned char * data,long count)224 void st_int_recv(void *disc_data,
225 	const unsigned char *data, long count)
226 {
227 	char *ptr;
228 	struct st_proto_s *proto;
229 	unsigned short payload_len = 0;
230 	int len = 0;
231 	unsigned char type = 0;
232 	unsigned char *plen;
233 	struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
234 	unsigned long flags;
235 
236 	ptr = (char *)data;
237 	/* tty_receive sent null ? */
238 	if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
239 		pr_err(" received null from TTY ");
240 		return;
241 	}
242 
243 	pr_debug("count %ld rx_state %ld"
244 		   "rx_count %ld", count, st_gdata->rx_state,
245 		   st_gdata->rx_count);
246 
247 	spin_lock_irqsave(&st_gdata->lock, flags);
248 	/* Decode received bytes here */
249 	while (count) {
250 		if (st_gdata->rx_count) {
251 			len = min_t(unsigned int, st_gdata->rx_count, count);
252 			skb_put_data(st_gdata->rx_skb, ptr, len);
253 			st_gdata->rx_count -= len;
254 			count -= len;
255 			ptr += len;
256 
257 			if (st_gdata->rx_count)
258 				continue;
259 
260 			/* Check ST RX state machine , where are we? */
261 			switch (st_gdata->rx_state) {
262 			/* Waiting for complete packet ? */
263 			case ST_W4_DATA:
264 				pr_debug("Complete pkt received");
265 				/* Ask ST CORE to forward
266 				 * the packet to protocol driver */
267 				st_send_frame(st_gdata->rx_chnl, st_gdata);
268 
269 				st_gdata->rx_state = ST_W4_PACKET_TYPE;
270 				st_gdata->rx_skb = NULL;
271 				continue;
272 			/* parse the header to know details */
273 			case ST_W4_HEADER:
274 				proto = st_gdata->list[st_gdata->rx_chnl];
275 				plen =
276 				&st_gdata->rx_skb->data
277 				[proto->offset_len_in_hdr];
278 				pr_debug("plen pointing to %x\n", *plen);
279 				if (proto->len_size == 1)/* 1 byte len field */
280 					payload_len = *(unsigned char *)plen;
281 				else if (proto->len_size == 2)
282 					payload_len =
283 					__le16_to_cpu(*(unsigned short *)plen);
284 				else
285 					pr_info("%s: invalid length "
286 					"for id %d\n",
287 					__func__, proto->chnl_id);
288 				st_check_data_len(st_gdata, proto->chnl_id,
289 						payload_len);
290 				pr_debug("off %d, pay len %d\n",
291 					proto->offset_len_in_hdr, payload_len);
292 				continue;
293 			}	/* end of switch rx_state */
294 		}
295 
296 		/* end of if rx_count */
297 		/* Check first byte of packet and identify module
298 		 * owner (BT/FM/GPS) */
299 		switch (*ptr) {
300 		case LL_SLEEP_IND:
301 		case LL_SLEEP_ACK:
302 		case LL_WAKE_UP_IND:
303 			pr_debug("PM packet");
304 			/* this takes appropriate action based on
305 			 * sleep state received --
306 			 */
307 			st_ll_sleep_state(st_gdata, *ptr);
308 			/* if WAKEUP_IND collides copy from waitq to txq
309 			 * and assume chip awake
310 			 */
311 			spin_unlock_irqrestore(&st_gdata->lock, flags);
312 			if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
313 				st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
314 			spin_lock_irqsave(&st_gdata->lock, flags);
315 
316 			ptr++;
317 			count--;
318 			continue;
319 		case LL_WAKE_UP_ACK:
320 			pr_debug("PM packet");
321 
322 			spin_unlock_irqrestore(&st_gdata->lock, flags);
323 			/* wake up ack received */
324 			st_wakeup_ack(st_gdata, *ptr);
325 			spin_lock_irqsave(&st_gdata->lock, flags);
326 
327 			ptr++;
328 			count--;
329 			continue;
330 			/* Unknow packet? */
331 		default:
332 			type = *ptr;
333 
334 			/* Default case means non-HCILL packets,
335 			 * possibilities are packets for:
336 			 * (a) valid protocol -  Supported Protocols within
337 			 *     the ST_MAX_CHANNELS.
338 			 * (b) registered protocol - Checked by
339 			 *     "st_gdata->list[type] == NULL)" are supported
340 			 *     protocols only.
341 			 *  Rules out any invalid protocol and
342 			 *  unregistered protocols with channel ID < 16.
343 			 */
344 
345 			if ((type >= ST_MAX_CHANNELS) ||
346 					(st_gdata->list[type] == NULL)) {
347 				pr_err("chip/interface misbehavior: "
348 						"dropping frame starting "
349 						"with 0x%02x\n", type);
350 				goto done;
351 			}
352 
353 			st_gdata->rx_skb = alloc_skb(
354 					st_gdata->list[type]->max_frame_size,
355 					GFP_ATOMIC);
356 			if (st_gdata->rx_skb == NULL) {
357 				pr_err("out of memory: dropping\n");
358 				goto done;
359 			}
360 
361 			skb_reserve(st_gdata->rx_skb,
362 					st_gdata->list[type]->reserve);
363 			/* next 2 required for BT only */
364 			st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
365 			st_gdata->rx_skb->cb[1] = 0; /*incoming*/
366 			st_gdata->rx_chnl = *ptr;
367 			st_gdata->rx_state = ST_W4_HEADER;
368 			st_gdata->rx_count = st_gdata->list[type]->hdr_len;
369 			pr_debug("rx_count %ld\n", st_gdata->rx_count);
370 		};
371 		ptr++;
372 		count--;
373 	}
374 done:
375 	spin_unlock_irqrestore(&st_gdata->lock, flags);
376 	pr_debug("done %s", __func__);
377 	return;
378 }
379 
380 /**
381  * st_int_dequeue - internal de-Q function.
382  *	If the previous data set was not written
383  *	completely, return that skb which has the pending data.
384  *	In normal cases, return top of txq.
385  */
st_int_dequeue(struct st_data_s * st_gdata)386 static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
387 {
388 	struct sk_buff *returning_skb;
389 
390 	pr_debug("%s", __func__);
391 	if (st_gdata->tx_skb != NULL) {
392 		returning_skb = st_gdata->tx_skb;
393 		st_gdata->tx_skb = NULL;
394 		return returning_skb;
395 	}
396 	return skb_dequeue(&st_gdata->txq);
397 }
398 
399 /**
400  * st_int_enqueue - internal Q-ing function.
401  *	Will either Q the skb to txq or the tx_waitq
402  *	depending on the ST LL state.
403  *	If the chip is asleep, then Q it onto waitq and
404  *	wakeup the chip.
405  *	txq and waitq needs protection since the other contexts
406  *	may be sending data, waking up chip.
407  */
st_int_enqueue(struct st_data_s * st_gdata,struct sk_buff * skb)408 static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
409 {
410 	unsigned long flags = 0;
411 
412 	pr_debug("%s", __func__);
413 	spin_lock_irqsave(&st_gdata->lock, flags);
414 
415 	switch (st_ll_getstate(st_gdata)) {
416 	case ST_LL_AWAKE:
417 		pr_debug("ST LL is AWAKE, sending normally");
418 		skb_queue_tail(&st_gdata->txq, skb);
419 		break;
420 	case ST_LL_ASLEEP_TO_AWAKE:
421 		skb_queue_tail(&st_gdata->tx_waitq, skb);
422 		break;
423 	case ST_LL_AWAKE_TO_ASLEEP:
424 		pr_err("ST LL is illegal state(%ld),"
425 			   "purging received skb.", st_ll_getstate(st_gdata));
426 		kfree_skb(skb);
427 		break;
428 	case ST_LL_ASLEEP:
429 		skb_queue_tail(&st_gdata->tx_waitq, skb);
430 		st_ll_wakeup(st_gdata);
431 		break;
432 	default:
433 		pr_err("ST LL is illegal state(%ld),"
434 			   "purging received skb.", st_ll_getstate(st_gdata));
435 		kfree_skb(skb);
436 		break;
437 	}
438 
439 	spin_unlock_irqrestore(&st_gdata->lock, flags);
440 	pr_debug("done %s", __func__);
441 	return;
442 }
443 
444 /*
445  * internal wakeup function
446  * called from either
447  * - TTY layer when write's finished
448  * - st_write (in context of the protocol stack)
449  */
work_fn_write_wakeup(struct work_struct * work)450 static void work_fn_write_wakeup(struct work_struct *work)
451 {
452 	struct st_data_s *st_gdata = container_of(work, struct st_data_s,
453 			work_write_wakeup);
454 
455 	st_tx_wakeup((void *)st_gdata);
456 }
st_tx_wakeup(struct st_data_s * st_data)457 void st_tx_wakeup(struct st_data_s *st_data)
458 {
459 	struct sk_buff *skb;
460 	unsigned long flags;	/* for irq save flags */
461 	pr_debug("%s", __func__);
462 	/* check for sending & set flag sending here */
463 	if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
464 		pr_debug("ST already sending");
465 		/* keep sending */
466 		set_bit(ST_TX_WAKEUP, &st_data->tx_state);
467 		return;
468 		/* TX_WAKEUP will be checked in another
469 		 * context
470 		 */
471 	}
472 	do {			/* come back if st_tx_wakeup is set */
473 		/* woke-up to write */
474 		clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
475 		while ((skb = st_int_dequeue(st_data))) {
476 			int len;
477 			spin_lock_irqsave(&st_data->lock, flags);
478 			/* enable wake-up from TTY */
479 			set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
480 			len = st_int_write(st_data, skb->data, skb->len);
481 			skb_pull(skb, len);
482 			/* if skb->len = len as expected, skb->len=0 */
483 			if (skb->len) {
484 				/* would be the next skb to be sent */
485 				st_data->tx_skb = skb;
486 				spin_unlock_irqrestore(&st_data->lock, flags);
487 				break;
488 			}
489 			kfree_skb(skb);
490 			spin_unlock_irqrestore(&st_data->lock, flags);
491 		}
492 		/* if wake-up is set in another context- restart sending */
493 	} while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
494 
495 	/* clear flag sending */
496 	clear_bit(ST_TX_SENDING, &st_data->tx_state);
497 }
498 
499 /********************************************************************/
500 /* functions called from ST KIM
501 */
kim_st_list_protocols(struct st_data_s * st_gdata,void * buf)502 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
503 {
504 	seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
505 			st_gdata->protos_registered,
506 			st_gdata->is_registered[0x04] == true ? 'R' : 'U',
507 			st_gdata->is_registered[0x08] == true ? 'R' : 'U',
508 			st_gdata->is_registered[0x09] == true ? 'R' : 'U');
509 }
510 
511 /********************************************************************/
512 /*
513  * functions called from protocol stack drivers
514  * to be EXPORT-ed
515  */
st_register(struct st_proto_s * new_proto)516 long st_register(struct st_proto_s *new_proto)
517 {
518 	struct st_data_s	*st_gdata;
519 	long err = 0;
520 	unsigned long flags = 0;
521 
522 	st_kim_ref(&st_gdata, 0);
523 	if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
524 	    || new_proto->reg_complete_cb == NULL) {
525 		pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
526 		return -EINVAL;
527 	}
528 
529 	if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
530 		pr_err("chnl_id %d not supported", new_proto->chnl_id);
531 		return -EPROTONOSUPPORT;
532 	}
533 
534 	if (st_gdata->is_registered[new_proto->chnl_id] == true) {
535 		pr_err("chnl_id %d already registered", new_proto->chnl_id);
536 		return -EALREADY;
537 	}
538 
539 	/* can be from process context only */
540 	spin_lock_irqsave(&st_gdata->lock, flags);
541 
542 	if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
543 		pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
544 		/* fw download in progress */
545 
546 		add_channel_to_table(st_gdata, new_proto);
547 		st_gdata->protos_registered++;
548 		new_proto->write = st_write;
549 
550 		set_bit(ST_REG_PENDING, &st_gdata->st_state);
551 		spin_unlock_irqrestore(&st_gdata->lock, flags);
552 		return -EINPROGRESS;
553 	} else if (st_gdata->protos_registered == ST_EMPTY) {
554 		pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
555 		set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
556 		st_recv = st_kim_recv;
557 
558 		/* enable the ST LL - to set default chip state */
559 		st_ll_enable(st_gdata);
560 
561 		/* release lock previously held - re-locked below */
562 		spin_unlock_irqrestore(&st_gdata->lock, flags);
563 
564 		/* this may take a while to complete
565 		 * since it involves BT fw download
566 		 */
567 		err = st_kim_start(st_gdata->kim_data);
568 		if (err != 0) {
569 			clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
570 			if ((st_gdata->protos_registered != ST_EMPTY) &&
571 			    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
572 				pr_err(" KIM failure complete callback ");
573 				spin_lock_irqsave(&st_gdata->lock, flags);
574 				st_reg_complete(st_gdata, err);
575 				spin_unlock_irqrestore(&st_gdata->lock, flags);
576 				clear_bit(ST_REG_PENDING, &st_gdata->st_state);
577 			}
578 			return -EINVAL;
579 		}
580 
581 		spin_lock_irqsave(&st_gdata->lock, flags);
582 
583 		clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
584 		st_recv = st_int_recv;
585 
586 		/* this is where all pending registration
587 		 * are signalled to be complete by calling callback functions
588 		 */
589 		if ((st_gdata->protos_registered != ST_EMPTY) &&
590 		    (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
591 			pr_debug(" call reg complete callback ");
592 			st_reg_complete(st_gdata, 0);
593 		}
594 		clear_bit(ST_REG_PENDING, &st_gdata->st_state);
595 
596 		/* check for already registered once more,
597 		 * since the above check is old
598 		 */
599 		if (st_gdata->is_registered[new_proto->chnl_id] == true) {
600 			pr_err(" proto %d already registered ",
601 				   new_proto->chnl_id);
602 			spin_unlock_irqrestore(&st_gdata->lock, flags);
603 			return -EALREADY;
604 		}
605 
606 		add_channel_to_table(st_gdata, new_proto);
607 		st_gdata->protos_registered++;
608 		new_proto->write = st_write;
609 		spin_unlock_irqrestore(&st_gdata->lock, flags);
610 		return err;
611 	}
612 	/* if fw is already downloaded & new stack registers protocol */
613 	else {
614 		add_channel_to_table(st_gdata, new_proto);
615 		st_gdata->protos_registered++;
616 		new_proto->write = st_write;
617 
618 		/* lock already held before entering else */
619 		spin_unlock_irqrestore(&st_gdata->lock, flags);
620 		return err;
621 	}
622 }
623 EXPORT_SYMBOL_GPL(st_register);
624 
625 /* to unregister a protocol -
626  * to be called from protocol stack driver
627  */
st_unregister(struct st_proto_s * proto)628 long st_unregister(struct st_proto_s *proto)
629 {
630 	long err = 0;
631 	unsigned long flags = 0;
632 	struct st_data_s	*st_gdata;
633 
634 	pr_debug("%s: %d ", __func__, proto->chnl_id);
635 
636 	st_kim_ref(&st_gdata, 0);
637 	if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
638 		pr_err(" chnl_id %d not supported", proto->chnl_id);
639 		return -EPROTONOSUPPORT;
640 	}
641 
642 	spin_lock_irqsave(&st_gdata->lock, flags);
643 
644 	if (st_gdata->is_registered[proto->chnl_id] == false) {
645 		pr_err(" chnl_id %d not registered", proto->chnl_id);
646 		spin_unlock_irqrestore(&st_gdata->lock, flags);
647 		return -EPROTONOSUPPORT;
648 	}
649 
650 	if (st_gdata->protos_registered)
651 		st_gdata->protos_registered--;
652 
653 	remove_channel_from_table(st_gdata, proto);
654 	spin_unlock_irqrestore(&st_gdata->lock, flags);
655 
656 	if ((st_gdata->protos_registered == ST_EMPTY) &&
657 	    (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
658 		pr_info(" all chnl_ids unregistered ");
659 
660 		/* stop traffic on tty */
661 		if (st_gdata->tty) {
662 			tty_ldisc_flush(st_gdata->tty);
663 			stop_tty(st_gdata->tty);
664 		}
665 
666 		/* all chnl_ids now unregistered */
667 		st_kim_stop(st_gdata->kim_data);
668 		/* disable ST LL */
669 		st_ll_disable(st_gdata);
670 	}
671 	return err;
672 }
673 
674 /*
675  * called in protocol stack drivers
676  * via the write function pointer
677  */
st_write(struct sk_buff * skb)678 long st_write(struct sk_buff *skb)
679 {
680 	struct st_data_s *st_gdata;
681 	long len;
682 
683 	st_kim_ref(&st_gdata, 0);
684 	if (unlikely(skb == NULL || st_gdata == NULL
685 		|| st_gdata->tty == NULL)) {
686 		pr_err("data/tty unavailable to perform write");
687 		return -EINVAL;
688 	}
689 
690 	pr_debug("%d to be written", skb->len);
691 	len = skb->len;
692 
693 	/* st_ll to decide where to enqueue the skb */
694 	st_int_enqueue(st_gdata, skb);
695 	/* wake up */
696 	st_tx_wakeup(st_gdata);
697 
698 	/* return number of bytes written */
699 	return len;
700 }
701 
702 /* for protocols making use of shared transport */
703 EXPORT_SYMBOL_GPL(st_unregister);
704 
705 /********************************************************************/
706 /*
707  * functions called from TTY layer
708  */
st_tty_open(struct tty_struct * tty)709 static int st_tty_open(struct tty_struct *tty)
710 {
711 	int err = 0;
712 	struct st_data_s *st_gdata;
713 	pr_info("%s ", __func__);
714 
715 	st_kim_ref(&st_gdata, 0);
716 	st_gdata->tty = tty;
717 	tty->disc_data = st_gdata;
718 
719 	/* don't do an wakeup for now */
720 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
721 
722 	/* mem already allocated
723 	 */
724 	tty->receive_room = 65536;
725 	/* Flush any pending characters in the driver and discipline. */
726 	tty_ldisc_flush(tty);
727 	tty_driver_flush_buffer(tty);
728 	/*
729 	 * signal to UIM via KIM that -
730 	 * installation of N_TI_WL ldisc is complete
731 	 */
732 	st_kim_complete(st_gdata->kim_data);
733 	pr_debug("done %s", __func__);
734 	return err;
735 }
736 
st_tty_close(struct tty_struct * tty)737 static void st_tty_close(struct tty_struct *tty)
738 {
739 	unsigned char i = ST_MAX_CHANNELS;
740 	unsigned long flags = 0;
741 	struct	st_data_s *st_gdata = tty->disc_data;
742 
743 	pr_info("%s ", __func__);
744 
745 	/* TODO:
746 	 * if a protocol has been registered & line discipline
747 	 * un-installed for some reason - what should be done ?
748 	 */
749 	spin_lock_irqsave(&st_gdata->lock, flags);
750 	for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
751 		if (st_gdata->is_registered[i] == true)
752 			pr_err("%d not un-registered", i);
753 		st_gdata->list[i] = NULL;
754 		st_gdata->is_registered[i] = false;
755 	}
756 	st_gdata->protos_registered = 0;
757 	spin_unlock_irqrestore(&st_gdata->lock, flags);
758 	/*
759 	 * signal to UIM via KIM that -
760 	 * N_TI_WL ldisc is un-installed
761 	 */
762 	st_kim_complete(st_gdata->kim_data);
763 	st_gdata->tty = NULL;
764 	/* Flush any pending characters in the driver and discipline. */
765 	tty_ldisc_flush(tty);
766 	tty_driver_flush_buffer(tty);
767 
768 	spin_lock_irqsave(&st_gdata->lock, flags);
769 	/* empty out txq and tx_waitq */
770 	skb_queue_purge(&st_gdata->txq);
771 	skb_queue_purge(&st_gdata->tx_waitq);
772 	/* reset the TTY Rx states of ST */
773 	st_gdata->rx_count = 0;
774 	st_gdata->rx_state = ST_W4_PACKET_TYPE;
775 	kfree_skb(st_gdata->rx_skb);
776 	st_gdata->rx_skb = NULL;
777 	spin_unlock_irqrestore(&st_gdata->lock, flags);
778 
779 	pr_debug("%s: done ", __func__);
780 }
781 
st_tty_receive(struct tty_struct * tty,const unsigned char * data,char * tty_flags,int count)782 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
783 			   char *tty_flags, int count)
784 {
785 #ifdef VERBOSE
786 	print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
787 		16, 1, data, count, 0);
788 #endif
789 
790 	/*
791 	 * if fw download is in progress then route incoming data
792 	 * to KIM for validation
793 	 */
794 	st_recv(tty->disc_data, data, count);
795 	pr_debug("done %s", __func__);
796 }
797 
798 /* wake-up function called in from the TTY layer
799  * inside the internal wakeup function will be called
800  */
st_tty_wakeup(struct tty_struct * tty)801 static void st_tty_wakeup(struct tty_struct *tty)
802 {
803 	struct	st_data_s *st_gdata = tty->disc_data;
804 	pr_debug("%s ", __func__);
805 	/* don't do an wakeup for now */
806 	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
807 
808 	/*
809 	 * schedule the internal wakeup instead of calling directly to
810 	 * avoid lockup (port->lock needed in tty->ops->write is
811 	 * already taken here
812 	 */
813 	schedule_work(&st_gdata->work_write_wakeup);
814 }
815 
st_tty_flush_buffer(struct tty_struct * tty)816 static void st_tty_flush_buffer(struct tty_struct *tty)
817 {
818 	struct	st_data_s *st_gdata = tty->disc_data;
819 	pr_debug("%s ", __func__);
820 
821 	kfree_skb(st_gdata->tx_skb);
822 	st_gdata->tx_skb = NULL;
823 
824 	tty_driver_flush_buffer(tty);
825 	return;
826 }
827 
828 static struct tty_ldisc_ops st_ldisc_ops = {
829 	.magic = TTY_LDISC_MAGIC,
830 	.name = "n_st",
831 	.open = st_tty_open,
832 	.close = st_tty_close,
833 	.receive_buf = st_tty_receive,
834 	.write_wakeup = st_tty_wakeup,
835 	.flush_buffer = st_tty_flush_buffer,
836 	.owner = THIS_MODULE
837 };
838 
839 /********************************************************************/
st_core_init(struct st_data_s ** core_data)840 int st_core_init(struct st_data_s **core_data)
841 {
842 	struct st_data_s *st_gdata;
843 	long err;
844 
845 	err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
846 	if (err) {
847 		pr_err("error registering %d line discipline %ld",
848 			   N_TI_WL, err);
849 		return err;
850 	}
851 	pr_debug("registered n_shared line discipline");
852 
853 	st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
854 	if (!st_gdata) {
855 		pr_err("memory allocation failed");
856 		err = tty_unregister_ldisc(N_TI_WL);
857 		if (err)
858 			pr_err("unable to un-register ldisc %ld", err);
859 		err = -ENOMEM;
860 		return err;
861 	}
862 
863 	/* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
864 	 * will be pushed in this queue for actual transmission.
865 	 */
866 	skb_queue_head_init(&st_gdata->txq);
867 	skb_queue_head_init(&st_gdata->tx_waitq);
868 
869 	/* Locking used in st_int_enqueue() to avoid multiple execution */
870 	spin_lock_init(&st_gdata->lock);
871 
872 	err = st_ll_init(st_gdata);
873 	if (err) {
874 		pr_err("error during st_ll initialization(%ld)", err);
875 		kfree(st_gdata);
876 		err = tty_unregister_ldisc(N_TI_WL);
877 		if (err)
878 			pr_err("unable to un-register ldisc");
879 		return err;
880 	}
881 
882 	INIT_WORK(&st_gdata->work_write_wakeup, work_fn_write_wakeup);
883 
884 	*core_data = st_gdata;
885 	return 0;
886 }
887 
st_core_exit(struct st_data_s * st_gdata)888 void st_core_exit(struct st_data_s *st_gdata)
889 {
890 	long err;
891 	/* internal module cleanup */
892 	err = st_ll_deinit(st_gdata);
893 	if (err)
894 		pr_err("error during deinit of ST LL %ld", err);
895 
896 	if (st_gdata != NULL) {
897 		/* Free ST Tx Qs and skbs */
898 		skb_queue_purge(&st_gdata->txq);
899 		skb_queue_purge(&st_gdata->tx_waitq);
900 		kfree_skb(st_gdata->rx_skb);
901 		kfree_skb(st_gdata->tx_skb);
902 		/* TTY ldisc cleanup */
903 		err = tty_unregister_ldisc(N_TI_WL);
904 		if (err)
905 			pr_err("unable to un-register ldisc %ld", err);
906 		/* free the global data pointer */
907 		kfree(st_gdata);
908 	}
909 }
910