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