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