0001
0002
0003
0004
0005
0006
0007
0008
0009 #define pr_fmt(fmt) "(stc): " fmt
0010 #include <linux/module.h>
0011 #include <linux/kernel.h>
0012 #include <linux/tty.h>
0013
0014 #include <linux/seq_file.h>
0015 #include <linux/skbuff.h>
0016
0017 #include <linux/ti_wilink_st.h>
0018
0019 extern void st_kim_recv(void *, const unsigned char *, long);
0020 void st_int_recv(void *, const unsigned char *, long);
0021
0022
0023
0024
0025
0026 static void (*st_recv) (void *, const unsigned char *, long);
0027
0028
0029 static void add_channel_to_table(struct st_data_s *st_gdata,
0030 struct st_proto_s *new_proto)
0031 {
0032 pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
0033
0034 st_gdata->list[new_proto->chnl_id] = new_proto;
0035 st_gdata->is_registered[new_proto->chnl_id] = true;
0036 }
0037
0038 static void remove_channel_from_table(struct st_data_s *st_gdata,
0039 struct st_proto_s *proto)
0040 {
0041 pr_info("%s: id %d\n", __func__, proto->chnl_id);
0042
0043 st_gdata->is_registered[proto->chnl_id] = false;
0044 }
0045
0046
0047
0048
0049
0050
0051
0052
0053 int st_get_uart_wr_room(struct st_data_s *st_gdata)
0054 {
0055 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
0056 pr_err("tty unavailable to perform write");
0057 return -1;
0058 }
0059
0060 return tty_write_room(st_gdata->tty);
0061 }
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071 int st_int_write(struct st_data_s *st_gdata,
0072 const unsigned char *data, int count)
0073 {
0074 struct tty_struct *tty;
0075 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
0076 pr_err("tty unavailable to perform write");
0077 return -EINVAL;
0078 }
0079 tty = st_gdata->tty;
0080 #ifdef VERBOSE
0081 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
0082 16, 1, data, count, 0);
0083 #endif
0084 return tty->ops->write(tty, data, count);
0085
0086 }
0087
0088
0089
0090
0091
0092 static void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
0093 {
0094 pr_debug(" %s(prot:%d) ", __func__, chnl_id);
0095
0096 if (unlikely
0097 (st_gdata == NULL || st_gdata->rx_skb == NULL
0098 || st_gdata->is_registered[chnl_id] == false)) {
0099 pr_err("chnl_id %d not registered, no data to send?",
0100 chnl_id);
0101 kfree_skb(st_gdata->rx_skb);
0102 return;
0103 }
0104
0105
0106
0107
0108
0109
0110 if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
0111 if (unlikely
0112 (st_gdata->list[chnl_id]->recv
0113 (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
0114 != 0)) {
0115 pr_err(" proto stack %d's ->recv failed", chnl_id);
0116 kfree_skb(st_gdata->rx_skb);
0117 return;
0118 }
0119 } else {
0120 pr_err(" proto stack %d's ->recv null", chnl_id);
0121 kfree_skb(st_gdata->rx_skb);
0122 }
0123 return;
0124 }
0125
0126
0127
0128
0129
0130
0131
0132 static void st_reg_complete(struct st_data_s *st_gdata, int err)
0133 {
0134 unsigned char i = 0;
0135 pr_info(" %s ", __func__);
0136 for (i = 0; i < ST_MAX_CHANNELS; i++) {
0137 if (likely(st_gdata != NULL &&
0138 st_gdata->is_registered[i] == true &&
0139 st_gdata->list[i]->reg_complete_cb != NULL)) {
0140 st_gdata->list[i]->reg_complete_cb
0141 (st_gdata->list[i]->priv_data, err);
0142 pr_info("protocol %d's cb sent %d\n", i, err);
0143 if (err) {
0144 st_gdata->is_registered[i] = false;
0145 if (st_gdata->protos_registered)
0146 st_gdata->protos_registered--;
0147 }
0148 }
0149 }
0150 }
0151
0152 static inline int st_check_data_len(struct st_data_s *st_gdata,
0153 unsigned char chnl_id, int len)
0154 {
0155 int room = skb_tailroom(st_gdata->rx_skb);
0156
0157 pr_debug("len %d room %d", len, room);
0158
0159 if (!len) {
0160
0161
0162
0163
0164
0165 st_send_frame(chnl_id, st_gdata);
0166
0167 } else if (len > room) {
0168
0169
0170
0171
0172 pr_err("Data length is too large len %d room %d", len,
0173 room);
0174 kfree_skb(st_gdata->rx_skb);
0175 } else {
0176
0177
0178
0179
0180 st_gdata->rx_state = ST_W4_DATA;
0181 st_gdata->rx_count = len;
0182 return len;
0183 }
0184
0185
0186 st_gdata->rx_state = ST_W4_PACKET_TYPE;
0187 st_gdata->rx_skb = NULL;
0188 st_gdata->rx_count = 0;
0189 st_gdata->rx_chnl = 0;
0190
0191 return 0;
0192 }
0193
0194
0195
0196
0197
0198 static inline void st_wakeup_ack(struct st_data_s *st_gdata,
0199 unsigned char cmd)
0200 {
0201 struct sk_buff *waiting_skb;
0202 unsigned long flags = 0;
0203
0204 spin_lock_irqsave(&st_gdata->lock, flags);
0205
0206
0207
0208
0209 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
0210 skb_queue_tail(&st_gdata->txq, waiting_skb);
0211
0212
0213 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
0214 spin_unlock_irqrestore(&st_gdata->lock, flags);
0215
0216
0217 st_tx_wakeup(st_gdata);
0218 }
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228 void st_int_recv(void *disc_data,
0229 const unsigned char *data, long count)
0230 {
0231 char *ptr;
0232 struct st_proto_s *proto;
0233 unsigned short payload_len = 0;
0234 int len = 0;
0235 unsigned char type = 0;
0236 unsigned char *plen;
0237 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
0238 unsigned long flags;
0239
0240 ptr = (char *)data;
0241
0242 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
0243 pr_err(" received null from TTY ");
0244 return;
0245 }
0246
0247 pr_debug("count %ld rx_state %ld"
0248 "rx_count %ld", count, st_gdata->rx_state,
0249 st_gdata->rx_count);
0250
0251 spin_lock_irqsave(&st_gdata->lock, flags);
0252
0253 while (count) {
0254 if (st_gdata->rx_count) {
0255 len = min_t(unsigned int, st_gdata->rx_count, count);
0256 skb_put_data(st_gdata->rx_skb, ptr, len);
0257 st_gdata->rx_count -= len;
0258 count -= len;
0259 ptr += len;
0260
0261 if (st_gdata->rx_count)
0262 continue;
0263
0264
0265 switch (st_gdata->rx_state) {
0266
0267 case ST_W4_DATA:
0268 pr_debug("Complete pkt received");
0269
0270
0271
0272
0273 st_send_frame(st_gdata->rx_chnl, st_gdata);
0274
0275 st_gdata->rx_state = ST_W4_PACKET_TYPE;
0276 st_gdata->rx_skb = NULL;
0277 continue;
0278
0279 case ST_W4_HEADER:
0280 proto = st_gdata->list[st_gdata->rx_chnl];
0281 plen =
0282 &st_gdata->rx_skb->data
0283 [proto->offset_len_in_hdr];
0284 pr_debug("plen pointing to %x\n", *plen);
0285 if (proto->len_size == 1)
0286 payload_len = *(unsigned char *)plen;
0287 else if (proto->len_size == 2)
0288 payload_len =
0289 __le16_to_cpu(*(unsigned short *)plen);
0290 else
0291 pr_info("%s: invalid length "
0292 "for id %d\n",
0293 __func__, proto->chnl_id);
0294 st_check_data_len(st_gdata, proto->chnl_id,
0295 payload_len);
0296 pr_debug("off %d, pay len %d\n",
0297 proto->offset_len_in_hdr, payload_len);
0298 continue;
0299 }
0300 }
0301
0302
0303
0304
0305
0306
0307
0308 switch (*ptr) {
0309 case LL_SLEEP_IND:
0310 case LL_SLEEP_ACK:
0311 case LL_WAKE_UP_IND:
0312 pr_debug("PM packet");
0313
0314
0315
0316
0317 st_ll_sleep_state(st_gdata, *ptr);
0318
0319
0320
0321
0322 spin_unlock_irqrestore(&st_gdata->lock, flags);
0323 if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
0324 st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
0325 spin_lock_irqsave(&st_gdata->lock, flags);
0326
0327 ptr++;
0328 count--;
0329 continue;
0330 case LL_WAKE_UP_ACK:
0331 pr_debug("PM packet");
0332
0333 spin_unlock_irqrestore(&st_gdata->lock, flags);
0334
0335 st_wakeup_ack(st_gdata, *ptr);
0336 spin_lock_irqsave(&st_gdata->lock, flags);
0337
0338 ptr++;
0339 count--;
0340 continue;
0341
0342 default:
0343 type = *ptr;
0344
0345
0346
0347
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357 if ((type >= ST_MAX_CHANNELS) ||
0358 (st_gdata->list[type] == NULL)) {
0359 pr_err("chip/interface misbehavior: "
0360 "dropping frame starting "
0361 "with 0x%02x\n", type);
0362 goto done;
0363 }
0364
0365 st_gdata->rx_skb = alloc_skb(
0366 st_gdata->list[type]->max_frame_size,
0367 GFP_ATOMIC);
0368 if (st_gdata->rx_skb == NULL) {
0369 pr_err("out of memory: dropping\n");
0370 goto done;
0371 }
0372
0373 skb_reserve(st_gdata->rx_skb,
0374 st_gdata->list[type]->reserve);
0375
0376 st_gdata->rx_skb->cb[0] = type;
0377 st_gdata->rx_skb->cb[1] = 0;
0378 st_gdata->rx_chnl = *ptr;
0379 st_gdata->rx_state = ST_W4_HEADER;
0380 st_gdata->rx_count = st_gdata->list[type]->hdr_len;
0381 pr_debug("rx_count %ld\n", st_gdata->rx_count);
0382 }
0383 ptr++;
0384 count--;
0385 }
0386 done:
0387 spin_unlock_irqrestore(&st_gdata->lock, flags);
0388 pr_debug("done %s", __func__);
0389 return;
0390 }
0391
0392
0393
0394
0395
0396
0397
0398 static struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
0399 {
0400 struct sk_buff *returning_skb;
0401
0402 pr_debug("%s", __func__);
0403 if (st_gdata->tx_skb != NULL) {
0404 returning_skb = st_gdata->tx_skb;
0405 st_gdata->tx_skb = NULL;
0406 return returning_skb;
0407 }
0408 return skb_dequeue(&st_gdata->txq);
0409 }
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420 static void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
0421 {
0422 unsigned long flags = 0;
0423
0424 pr_debug("%s", __func__);
0425 spin_lock_irqsave(&st_gdata->lock, flags);
0426
0427 switch (st_ll_getstate(st_gdata)) {
0428 case ST_LL_AWAKE:
0429 pr_debug("ST LL is AWAKE, sending normally");
0430 skb_queue_tail(&st_gdata->txq, skb);
0431 break;
0432 case ST_LL_ASLEEP_TO_AWAKE:
0433 skb_queue_tail(&st_gdata->tx_waitq, skb);
0434 break;
0435 case ST_LL_AWAKE_TO_ASLEEP:
0436 pr_err("ST LL is illegal state(%ld),"
0437 "purging received skb.", st_ll_getstate(st_gdata));
0438 kfree_skb(skb);
0439 break;
0440 case ST_LL_ASLEEP:
0441 skb_queue_tail(&st_gdata->tx_waitq, skb);
0442 st_ll_wakeup(st_gdata);
0443 break;
0444 default:
0445 pr_err("ST LL is illegal state(%ld),"
0446 "purging received skb.", st_ll_getstate(st_gdata));
0447 kfree_skb(skb);
0448 break;
0449 }
0450
0451 spin_unlock_irqrestore(&st_gdata->lock, flags);
0452 pr_debug("done %s", __func__);
0453 return;
0454 }
0455
0456
0457
0458
0459
0460
0461
0462 static void work_fn_write_wakeup(struct work_struct *work)
0463 {
0464 struct st_data_s *st_gdata = container_of(work, struct st_data_s,
0465 work_write_wakeup);
0466
0467 st_tx_wakeup((void *)st_gdata);
0468 }
0469 void st_tx_wakeup(struct st_data_s *st_data)
0470 {
0471 struct sk_buff *skb;
0472 unsigned long flags;
0473 pr_debug("%s", __func__);
0474
0475 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
0476 pr_debug("ST already sending");
0477
0478 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
0479 return;
0480
0481
0482
0483 }
0484 do {
0485
0486 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
0487 while ((skb = st_int_dequeue(st_data))) {
0488 int len;
0489 spin_lock_irqsave(&st_data->lock, flags);
0490
0491 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
0492 len = st_int_write(st_data, skb->data, skb->len);
0493 skb_pull(skb, len);
0494
0495 if (skb->len) {
0496
0497 st_data->tx_skb = skb;
0498 spin_unlock_irqrestore(&st_data->lock, flags);
0499 break;
0500 }
0501 kfree_skb(skb);
0502 spin_unlock_irqrestore(&st_data->lock, flags);
0503 }
0504
0505 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
0506
0507
0508 clear_bit(ST_TX_SENDING, &st_data->tx_state);
0509 }
0510
0511
0512
0513
0514 void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
0515 {
0516 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
0517 st_gdata->protos_registered,
0518 st_gdata->is_registered[0x04] == true ? 'R' : 'U',
0519 st_gdata->is_registered[0x08] == true ? 'R' : 'U',
0520 st_gdata->is_registered[0x09] == true ? 'R' : 'U');
0521 }
0522
0523
0524
0525
0526
0527
0528 long st_register(struct st_proto_s *new_proto)
0529 {
0530 struct st_data_s *st_gdata;
0531 long err = 0;
0532 unsigned long flags = 0;
0533
0534 st_kim_ref(&st_gdata, 0);
0535 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
0536 || new_proto->reg_complete_cb == NULL) {
0537 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
0538 return -EINVAL;
0539 }
0540
0541 if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
0542 pr_err("chnl_id %d not supported", new_proto->chnl_id);
0543 return -EPROTONOSUPPORT;
0544 }
0545
0546 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
0547 pr_err("chnl_id %d already registered", new_proto->chnl_id);
0548 return -EALREADY;
0549 }
0550
0551
0552 spin_lock_irqsave(&st_gdata->lock, flags);
0553
0554 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
0555 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
0556
0557
0558 add_channel_to_table(st_gdata, new_proto);
0559 st_gdata->protos_registered++;
0560 new_proto->write = st_write;
0561
0562 set_bit(ST_REG_PENDING, &st_gdata->st_state);
0563 spin_unlock_irqrestore(&st_gdata->lock, flags);
0564 return -EINPROGRESS;
0565 } else if (st_gdata->protos_registered == ST_EMPTY) {
0566 pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
0567 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
0568 st_recv = st_kim_recv;
0569
0570
0571 st_ll_enable(st_gdata);
0572
0573
0574 spin_unlock_irqrestore(&st_gdata->lock, flags);
0575
0576
0577
0578
0579
0580 err = st_kim_start(st_gdata->kim_data);
0581 if (err != 0) {
0582 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
0583 if ((st_gdata->protos_registered != ST_EMPTY) &&
0584 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
0585 pr_err(" KIM failure complete callback ");
0586 spin_lock_irqsave(&st_gdata->lock, flags);
0587 st_reg_complete(st_gdata, err);
0588 spin_unlock_irqrestore(&st_gdata->lock, flags);
0589 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
0590 }
0591 return -EINVAL;
0592 }
0593
0594 spin_lock_irqsave(&st_gdata->lock, flags);
0595
0596 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
0597 st_recv = st_int_recv;
0598
0599
0600
0601
0602
0603 if ((st_gdata->protos_registered != ST_EMPTY) &&
0604 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
0605 pr_debug(" call reg complete callback ");
0606 st_reg_complete(st_gdata, 0);
0607 }
0608 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
0609
0610
0611
0612
0613
0614 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
0615 pr_err(" proto %d already registered ",
0616 new_proto->chnl_id);
0617 spin_unlock_irqrestore(&st_gdata->lock, flags);
0618 return -EALREADY;
0619 }
0620
0621 add_channel_to_table(st_gdata, new_proto);
0622 st_gdata->protos_registered++;
0623 new_proto->write = st_write;
0624 spin_unlock_irqrestore(&st_gdata->lock, flags);
0625 return err;
0626 }
0627
0628 else {
0629 add_channel_to_table(st_gdata, new_proto);
0630 st_gdata->protos_registered++;
0631 new_proto->write = st_write;
0632
0633
0634 spin_unlock_irqrestore(&st_gdata->lock, flags);
0635 return err;
0636 }
0637 }
0638 EXPORT_SYMBOL_GPL(st_register);
0639
0640
0641
0642
0643
0644 long st_unregister(struct st_proto_s *proto)
0645 {
0646 long err = 0;
0647 unsigned long flags = 0;
0648 struct st_data_s *st_gdata;
0649
0650 pr_debug("%s: %d ", __func__, proto->chnl_id);
0651
0652 st_kim_ref(&st_gdata, 0);
0653 if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
0654 pr_err(" chnl_id %d not supported", proto->chnl_id);
0655 return -EPROTONOSUPPORT;
0656 }
0657
0658 spin_lock_irqsave(&st_gdata->lock, flags);
0659
0660 if (st_gdata->is_registered[proto->chnl_id] == false) {
0661 pr_err(" chnl_id %d not registered", proto->chnl_id);
0662 spin_unlock_irqrestore(&st_gdata->lock, flags);
0663 return -EPROTONOSUPPORT;
0664 }
0665
0666 if (st_gdata->protos_registered)
0667 st_gdata->protos_registered--;
0668
0669 remove_channel_from_table(st_gdata, proto);
0670 spin_unlock_irqrestore(&st_gdata->lock, flags);
0671
0672 if ((st_gdata->protos_registered == ST_EMPTY) &&
0673 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
0674 pr_info(" all chnl_ids unregistered ");
0675
0676
0677 if (st_gdata->tty) {
0678 tty_ldisc_flush(st_gdata->tty);
0679 stop_tty(st_gdata->tty);
0680 }
0681
0682
0683 st_kim_stop(st_gdata->kim_data);
0684
0685 st_ll_disable(st_gdata);
0686 }
0687 return err;
0688 }
0689
0690
0691
0692
0693
0694 long st_write(struct sk_buff *skb)
0695 {
0696 struct st_data_s *st_gdata;
0697 long len;
0698
0699 st_kim_ref(&st_gdata, 0);
0700 if (unlikely(skb == NULL || st_gdata == NULL
0701 || st_gdata->tty == NULL)) {
0702 pr_err("data/tty unavailable to perform write");
0703 return -EINVAL;
0704 }
0705
0706 pr_debug("%d to be written", skb->len);
0707 len = skb->len;
0708
0709
0710 st_int_enqueue(st_gdata, skb);
0711
0712 st_tx_wakeup(st_gdata);
0713
0714
0715 return len;
0716 }
0717
0718
0719 EXPORT_SYMBOL_GPL(st_unregister);
0720
0721
0722
0723
0724
0725 static int st_tty_open(struct tty_struct *tty)
0726 {
0727 struct st_data_s *st_gdata;
0728 pr_info("%s ", __func__);
0729
0730 st_kim_ref(&st_gdata, 0);
0731 st_gdata->tty = tty;
0732 tty->disc_data = st_gdata;
0733
0734
0735 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0736
0737
0738
0739 tty->receive_room = 65536;
0740
0741 tty_ldisc_flush(tty);
0742 tty_driver_flush_buffer(tty);
0743
0744
0745
0746
0747 st_kim_complete(st_gdata->kim_data);
0748 pr_debug("done %s", __func__);
0749
0750 return 0;
0751 }
0752
0753 static void st_tty_close(struct tty_struct *tty)
0754 {
0755 unsigned char i;
0756 unsigned long flags;
0757 struct st_data_s *st_gdata = tty->disc_data;
0758
0759 pr_info("%s ", __func__);
0760
0761
0762
0763
0764
0765
0766 spin_lock_irqsave(&st_gdata->lock, flags);
0767 for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
0768 if (st_gdata->is_registered[i] == true)
0769 pr_err("%d not un-registered", i);
0770 st_gdata->list[i] = NULL;
0771 st_gdata->is_registered[i] = false;
0772 }
0773 st_gdata->protos_registered = 0;
0774 spin_unlock_irqrestore(&st_gdata->lock, flags);
0775
0776
0777
0778
0779 st_kim_complete(st_gdata->kim_data);
0780 st_gdata->tty = NULL;
0781
0782 tty_ldisc_flush(tty);
0783 tty_driver_flush_buffer(tty);
0784
0785 spin_lock_irqsave(&st_gdata->lock, flags);
0786
0787 skb_queue_purge(&st_gdata->txq);
0788 skb_queue_purge(&st_gdata->tx_waitq);
0789
0790 st_gdata->rx_count = 0;
0791 st_gdata->rx_state = ST_W4_PACKET_TYPE;
0792 kfree_skb(st_gdata->rx_skb);
0793 st_gdata->rx_skb = NULL;
0794 spin_unlock_irqrestore(&st_gdata->lock, flags);
0795
0796 pr_debug("%s: done ", __func__);
0797 }
0798
0799 static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
0800 const char *tty_flags, int count)
0801 {
0802 #ifdef VERBOSE
0803 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
0804 16, 1, data, count, 0);
0805 #endif
0806
0807
0808
0809
0810
0811 st_recv(tty->disc_data, data, count);
0812 pr_debug("done %s", __func__);
0813 }
0814
0815
0816
0817
0818
0819 static void st_tty_wakeup(struct tty_struct *tty)
0820 {
0821 struct st_data_s *st_gdata = tty->disc_data;
0822 pr_debug("%s ", __func__);
0823
0824 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
0825
0826
0827
0828
0829
0830
0831 schedule_work(&st_gdata->work_write_wakeup);
0832 }
0833
0834 static void st_tty_flush_buffer(struct tty_struct *tty)
0835 {
0836 struct st_data_s *st_gdata = tty->disc_data;
0837 pr_debug("%s ", __func__);
0838
0839 kfree_skb(st_gdata->tx_skb);
0840 st_gdata->tx_skb = NULL;
0841
0842 tty_driver_flush_buffer(tty);
0843 return;
0844 }
0845
0846 static struct tty_ldisc_ops st_ldisc_ops = {
0847 .num = N_TI_WL,
0848 .name = "n_st",
0849 .open = st_tty_open,
0850 .close = st_tty_close,
0851 .receive_buf = st_tty_receive,
0852 .write_wakeup = st_tty_wakeup,
0853 .flush_buffer = st_tty_flush_buffer,
0854 .owner = THIS_MODULE
0855 };
0856
0857
0858 int st_core_init(struct st_data_s **core_data)
0859 {
0860 struct st_data_s *st_gdata;
0861 long err;
0862
0863 err = tty_register_ldisc(&st_ldisc_ops);
0864 if (err) {
0865 pr_err("error registering %d line discipline %ld",
0866 N_TI_WL, err);
0867 return err;
0868 }
0869 pr_debug("registered n_shared line discipline");
0870
0871 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
0872 if (!st_gdata) {
0873 pr_err("memory allocation failed");
0874 err = -ENOMEM;
0875 goto err_unreg_ldisc;
0876 }
0877
0878
0879
0880
0881 skb_queue_head_init(&st_gdata->txq);
0882 skb_queue_head_init(&st_gdata->tx_waitq);
0883
0884
0885 spin_lock_init(&st_gdata->lock);
0886
0887 err = st_ll_init(st_gdata);
0888 if (err) {
0889 pr_err("error during st_ll initialization(%ld)", err);
0890 goto err_free_gdata;
0891 }
0892
0893 INIT_WORK(&st_gdata->work_write_wakeup, work_fn_write_wakeup);
0894
0895 *core_data = st_gdata;
0896 return 0;
0897 err_free_gdata:
0898 kfree(st_gdata);
0899 err_unreg_ldisc:
0900 tty_unregister_ldisc(&st_ldisc_ops);
0901 return err;
0902 }
0903
0904 void st_core_exit(struct st_data_s *st_gdata)
0905 {
0906 long err;
0907
0908 err = st_ll_deinit(st_gdata);
0909 if (err)
0910 pr_err("error during deinit of ST LL %ld", err);
0911
0912 if (st_gdata != NULL) {
0913
0914 skb_queue_purge(&st_gdata->txq);
0915 skb_queue_purge(&st_gdata->tx_waitq);
0916 kfree_skb(st_gdata->rx_skb);
0917 kfree_skb(st_gdata->tx_skb);
0918
0919 tty_unregister_ldisc(&st_ldisc_ops);
0920
0921 kfree(st_gdata);
0922 }
0923 }