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