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