1 /* 2 * Shared Transport Line discipline driver Core 3 * Init Manager module responsible for GPIO control 4 * and firmware download 5 * Copyright (C) 2009-2010 Texas Instruments 6 * Author: Pavan Savoy <pavan_savoy@ti.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 as 10 * published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * 21 */ 22 23 #define pr_fmt(fmt) "(stk) :" fmt 24 #include <linux/platform_device.h> 25 #include <linux/jiffies.h> 26 #include <linux/firmware.h> 27 #include <linux/delay.h> 28 #include <linux/wait.h> 29 #include <linux/gpio.h> 30 #include <linux/debugfs.h> 31 #include <linux/seq_file.h> 32 #include <linux/sched.h> 33 #include <linux/sysfs.h> 34 #include <linux/tty.h> 35 36 #include <linux/skbuff.h> 37 #include <linux/ti_wilink_st.h> 38 #include <linux/module.h> 39 40 #define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */ 41 static struct platform_device *st_kim_devices[MAX_ST_DEVICES]; 42 43 /**********************************************************************/ 44 /* internal functions */ 45 46 /** 47 * st_get_plat_device - 48 * function which returns the reference to the platform device 49 * requested by id. As of now only 1 such device exists (id=0) 50 * the context requesting for reference can get the id to be 51 * requested by a. The protocol driver which is registering or 52 * b. the tty device which is opened. 53 */ 54 static struct platform_device *st_get_plat_device(int id) 55 { 56 return st_kim_devices[id]; 57 } 58 59 /** 60 * validate_firmware_response - 61 * function to return whether the firmware response was proper 62 * in case of error don't complete so that waiting for proper 63 * response times out 64 */ 65 static void validate_firmware_response(struct kim_data_s *kim_gdata) 66 { 67 struct sk_buff *skb = kim_gdata->rx_skb; 68 if (!skb) 69 return; 70 71 /* these magic numbers are the position in the response buffer which 72 * allows us to distinguish whether the response is for the read 73 * version info. command 74 */ 75 if (skb->data[2] == 0x01 && skb->data[3] == 0x01 && 76 skb->data[4] == 0x10 && skb->data[5] == 0x00) { 77 /* fw version response */ 78 memcpy(kim_gdata->resp_buffer, 79 kim_gdata->rx_skb->data, 80 kim_gdata->rx_skb->len); 81 kim_gdata->rx_state = ST_W4_PACKET_TYPE; 82 kim_gdata->rx_skb = NULL; 83 kim_gdata->rx_count = 0; 84 } else if (unlikely(skb->data[5] != 0)) { 85 pr_err("no proper response during fw download"); 86 pr_err("data6 %x", skb->data[5]); 87 kfree_skb(skb); 88 return; /* keep waiting for the proper response */ 89 } 90 /* becos of all the script being downloaded */ 91 complete_all(&kim_gdata->kim_rcvd); 92 kfree_skb(skb); 93 } 94 95 /* check for data len received inside kim_int_recv 96 * most often hit the last case to update state to waiting for data 97 */ 98 static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len) 99 { 100 register int room = skb_tailroom(kim_gdata->rx_skb); 101 102 pr_debug("len %d room %d", len, room); 103 104 if (!len) { 105 validate_firmware_response(kim_gdata); 106 } else if (len > room) { 107 /* Received packet's payload length is larger. 108 * We can't accommodate it in created skb. 109 */ 110 pr_err("Data length is too large len %d room %d", len, 111 room); 112 kfree_skb(kim_gdata->rx_skb); 113 } else { 114 /* Packet header has non-zero payload length and 115 * we have enough space in created skb. Lets read 116 * payload data */ 117 kim_gdata->rx_state = ST_W4_DATA; 118 kim_gdata->rx_count = len; 119 return len; 120 } 121 122 /* Change ST LL state to continue to process next 123 * packet */ 124 kim_gdata->rx_state = ST_W4_PACKET_TYPE; 125 kim_gdata->rx_skb = NULL; 126 kim_gdata->rx_count = 0; 127 128 return 0; 129 } 130 131 /** 132 * kim_int_recv - receive function called during firmware download 133 * firmware download responses on different UART drivers 134 * have been observed to come in bursts of different 135 * tty_receive and hence the logic 136 */ 137 static void kim_int_recv(struct kim_data_s *kim_gdata, 138 const unsigned char *data, long count) 139 { 140 const unsigned char *ptr; 141 int len = 0; 142 unsigned char *plen; 143 144 pr_debug("%s", __func__); 145 /* Decode received bytes here */ 146 ptr = data; 147 if (unlikely(ptr == NULL)) { 148 pr_err(" received null from TTY "); 149 return; 150 } 151 152 while (count) { 153 if (kim_gdata->rx_count) { 154 len = min_t(unsigned int, kim_gdata->rx_count, count); 155 skb_put_data(kim_gdata->rx_skb, ptr, len); 156 kim_gdata->rx_count -= len; 157 count -= len; 158 ptr += len; 159 160 if (kim_gdata->rx_count) 161 continue; 162 163 /* Check ST RX state machine , where are we? */ 164 switch (kim_gdata->rx_state) { 165 /* Waiting for complete packet ? */ 166 case ST_W4_DATA: 167 pr_debug("Complete pkt received"); 168 validate_firmware_response(kim_gdata); 169 kim_gdata->rx_state = ST_W4_PACKET_TYPE; 170 kim_gdata->rx_skb = NULL; 171 continue; 172 /* Waiting for Bluetooth event header ? */ 173 case ST_W4_HEADER: 174 plen = 175 (unsigned char *)&kim_gdata->rx_skb->data[1]; 176 pr_debug("event hdr: plen 0x%02x\n", *plen); 177 kim_check_data_len(kim_gdata, *plen); 178 continue; 179 } /* end of switch */ 180 } /* end of if rx_state */ 181 switch (*ptr) { 182 /* Bluetooth event packet? */ 183 case 0x04: 184 kim_gdata->rx_state = ST_W4_HEADER; 185 kim_gdata->rx_count = 2; 186 break; 187 default: 188 pr_info("unknown packet"); 189 ptr++; 190 count--; 191 continue; 192 } 193 ptr++; 194 count--; 195 kim_gdata->rx_skb = 196 alloc_skb(1024+8, GFP_ATOMIC); 197 if (!kim_gdata->rx_skb) { 198 pr_err("can't allocate mem for new packet"); 199 kim_gdata->rx_state = ST_W4_PACKET_TYPE; 200 kim_gdata->rx_count = 0; 201 return; 202 } 203 skb_reserve(kim_gdata->rx_skb, 8); 204 kim_gdata->rx_skb->cb[0] = 4; 205 kim_gdata->rx_skb->cb[1] = 0; 206 207 } 208 return; 209 } 210 211 static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name) 212 { 213 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0; 214 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 }; 215 long timeout; 216 217 pr_debug("%s", __func__); 218 219 reinit_completion(&kim_gdata->kim_rcvd); 220 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) { 221 pr_err("kim: couldn't write 4 bytes"); 222 return -EIO; 223 } 224 225 timeout = wait_for_completion_interruptible_timeout( 226 &kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME)); 227 if (timeout <= 0) { 228 pr_err(" waiting for ver info- timed out or received signal"); 229 return timeout ? -ERESTARTSYS : -ETIMEDOUT; 230 } 231 reinit_completion(&kim_gdata->kim_rcvd); 232 /* the positions 12 & 13 in the response buffer provide with the 233 * chip, major & minor numbers 234 */ 235 236 version = 237 MAKEWORD(kim_gdata->resp_buffer[12], 238 kim_gdata->resp_buffer[13]); 239 chip = (version & 0x7C00) >> 10; 240 min_ver = (version & 0x007F); 241 maj_ver = (version & 0x0380) >> 7; 242 243 if (version & 0x8000) 244 maj_ver |= 0x0008; 245 246 sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts", 247 chip, maj_ver, min_ver); 248 249 /* to be accessed later via sysfs entry */ 250 kim_gdata->version.full = version; 251 kim_gdata->version.chip = chip; 252 kim_gdata->version.maj_ver = maj_ver; 253 kim_gdata->version.min_ver = min_ver; 254 255 pr_info("%s", bts_scr_name); 256 return 0; 257 } 258 259 static void skip_change_remote_baud(unsigned char **ptr, long *len) 260 { 261 unsigned char *nxt_action, *cur_action; 262 cur_action = *ptr; 263 264 nxt_action = cur_action + sizeof(struct bts_action) + 265 ((struct bts_action *) cur_action)->size; 266 267 if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) { 268 pr_err("invalid action after change remote baud command"); 269 } else { 270 *ptr = *ptr + sizeof(struct bts_action) + 271 ((struct bts_action *)cur_action)->size; 272 *len = *len - (sizeof(struct bts_action) + 273 ((struct bts_action *)cur_action)->size); 274 /* warn user on not commenting these in firmware */ 275 pr_warn("skipping the wait event of change remote baud"); 276 } 277 } 278 279 /** 280 * download_firmware - 281 * internal function which parses through the .bts firmware 282 * script file intreprets SEND, DELAY actions only as of now 283 */ 284 static long download_firmware(struct kim_data_s *kim_gdata) 285 { 286 long err = 0; 287 long len = 0; 288 unsigned char *ptr = NULL; 289 unsigned char *action_ptr = NULL; 290 unsigned char bts_scr_name[40] = { 0 }; /* 40 char long bts scr name? */ 291 int wr_room_space; 292 int cmd_size; 293 unsigned long timeout; 294 295 err = read_local_version(kim_gdata, bts_scr_name); 296 if (err != 0) { 297 pr_err("kim: failed to read local ver"); 298 return err; 299 } 300 err = 301 request_firmware(&kim_gdata->fw_entry, bts_scr_name, 302 &kim_gdata->kim_pdev->dev); 303 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) || 304 (kim_gdata->fw_entry->size == 0))) { 305 pr_err(" request_firmware failed(errno %ld) for %s", err, 306 bts_scr_name); 307 return -EINVAL; 308 } 309 ptr = (void *)kim_gdata->fw_entry->data; 310 len = kim_gdata->fw_entry->size; 311 /* bts_header to remove out magic number and 312 * version 313 */ 314 ptr += sizeof(struct bts_header); 315 len -= sizeof(struct bts_header); 316 317 while (len > 0 && ptr) { 318 pr_debug(" action size %d, type %d ", 319 ((struct bts_action *)ptr)->size, 320 ((struct bts_action *)ptr)->type); 321 322 switch (((struct bts_action *)ptr)->type) { 323 case ACTION_SEND_COMMAND: /* action send */ 324 pr_debug("S"); 325 action_ptr = &(((struct bts_action *)ptr)->data[0]); 326 if (unlikely 327 (((struct hci_command *)action_ptr)->opcode == 328 0xFF36)) { 329 /* ignore remote change 330 * baud rate HCI VS command */ 331 pr_warn("change remote baud" 332 " rate command in firmware"); 333 skip_change_remote_baud(&ptr, &len); 334 break; 335 } 336 /* 337 * Make sure we have enough free space in uart 338 * tx buffer to write current firmware command 339 */ 340 cmd_size = ((struct bts_action *)ptr)->size; 341 timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME); 342 do { 343 wr_room_space = 344 st_get_uart_wr_room(kim_gdata->core_data); 345 if (wr_room_space < 0) { 346 pr_err("Unable to get free " 347 "space info from uart tx buffer"); 348 release_firmware(kim_gdata->fw_entry); 349 return wr_room_space; 350 } 351 mdelay(1); /* wait 1ms before checking room */ 352 } while ((wr_room_space < cmd_size) && 353 time_before(jiffies, timeout)); 354 355 /* Timeout happened ? */ 356 if (time_after_eq(jiffies, timeout)) { 357 pr_err("Timeout while waiting for free " 358 "free space in uart tx buffer"); 359 release_firmware(kim_gdata->fw_entry); 360 return -ETIMEDOUT; 361 } 362 /* reinit completion before sending for the 363 * relevant wait 364 */ 365 reinit_completion(&kim_gdata->kim_rcvd); 366 367 /* 368 * Free space found in uart buffer, call st_int_write 369 * to send current firmware command to the uart tx 370 * buffer. 371 */ 372 err = st_int_write(kim_gdata->core_data, 373 ((struct bts_action_send *)action_ptr)->data, 374 ((struct bts_action *)ptr)->size); 375 if (unlikely(err < 0)) { 376 release_firmware(kim_gdata->fw_entry); 377 return err; 378 } 379 /* 380 * Check number of bytes written to the uart tx buffer 381 * and requested command write size 382 */ 383 if (err != cmd_size) { 384 pr_err("Number of bytes written to uart " 385 "tx buffer are not matching with " 386 "requested cmd write size"); 387 release_firmware(kim_gdata->fw_entry); 388 return -EIO; 389 } 390 break; 391 case ACTION_WAIT_EVENT: /* wait */ 392 pr_debug("W"); 393 err = wait_for_completion_interruptible_timeout( 394 &kim_gdata->kim_rcvd, 395 msecs_to_jiffies(CMD_RESP_TIME)); 396 if (err <= 0) { 397 pr_err("response timeout/signaled during fw download "); 398 /* timed out */ 399 release_firmware(kim_gdata->fw_entry); 400 return err ? -ERESTARTSYS : -ETIMEDOUT; 401 } 402 reinit_completion(&kim_gdata->kim_rcvd); 403 break; 404 case ACTION_DELAY: /* sleep */ 405 pr_info("sleep command in scr"); 406 action_ptr = &(((struct bts_action *)ptr)->data[0]); 407 mdelay(((struct bts_action_delay *)action_ptr)->msec); 408 break; 409 } 410 len = 411 len - (sizeof(struct bts_action) + 412 ((struct bts_action *)ptr)->size); 413 ptr = 414 ptr + sizeof(struct bts_action) + 415 ((struct bts_action *)ptr)->size; 416 } 417 /* fw download complete */ 418 release_firmware(kim_gdata->fw_entry); 419 return 0; 420 } 421 422 /**********************************************************************/ 423 /* functions called from ST core */ 424 /* called from ST Core, when REG_IN_PROGRESS (registration in progress) 425 * can be because of 426 * 1. response to read local version 427 * 2. during send/recv's of firmware download 428 */ 429 void st_kim_recv(void *disc_data, const unsigned char *data, long count) 430 { 431 struct st_data_s *st_gdata = (struct st_data_s *)disc_data; 432 struct kim_data_s *kim_gdata = st_gdata->kim_data; 433 434 /* proceed to gather all data and distinguish read fw version response 435 * from other fw responses when data gathering is complete 436 */ 437 kim_int_recv(kim_gdata, data, count); 438 return; 439 } 440 441 /* to signal completion of line discipline installation 442 * called from ST Core, upon tty_open 443 */ 444 void st_kim_complete(void *kim_data) 445 { 446 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; 447 complete(&kim_gdata->ldisc_installed); 448 } 449 450 /** 451 * st_kim_start - called from ST Core upon 1st registration 452 * This involves toggling the chip enable gpio, reading 453 * the firmware version from chip, forming the fw file name 454 * based on the chip version, requesting the fw, parsing it 455 * and perform download(send/recv). 456 */ 457 long st_kim_start(void *kim_data) 458 { 459 long err = 0; 460 long retry = POR_RETRY_COUNT; 461 struct ti_st_plat_data *pdata; 462 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; 463 464 pr_info(" %s", __func__); 465 pdata = kim_gdata->kim_pdev->dev.platform_data; 466 467 do { 468 /* platform specific enabling code here */ 469 if (pdata->chip_enable) 470 pdata->chip_enable(kim_gdata); 471 472 /* Configure BT nShutdown to HIGH state */ 473 gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW); 474 mdelay(5); /* FIXME: a proper toggle */ 475 gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH); 476 mdelay(100); 477 /* re-initialize the completion */ 478 reinit_completion(&kim_gdata->ldisc_installed); 479 /* send notification to UIM */ 480 kim_gdata->ldisc_install = 1; 481 pr_info("ldisc_install = 1"); 482 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, 483 NULL, "install"); 484 /* wait for ldisc to be installed */ 485 err = wait_for_completion_interruptible_timeout( 486 &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME)); 487 if (!err) { 488 /* ldisc installation timeout, 489 * flush uart, power cycle BT_EN */ 490 pr_err("ldisc installation timeout"); 491 err = st_kim_stop(kim_gdata); 492 continue; 493 } else { 494 /* ldisc installed now */ 495 pr_info("line discipline installed"); 496 err = download_firmware(kim_gdata); 497 if (err != 0) { 498 /* ldisc installed but fw download failed, 499 * flush uart & power cycle BT_EN */ 500 pr_err("download firmware failed"); 501 err = st_kim_stop(kim_gdata); 502 continue; 503 } else { /* on success don't retry */ 504 break; 505 } 506 } 507 } while (retry--); 508 return err; 509 } 510 511 /** 512 * st_kim_stop - stop communication with chip. 513 * This can be called from ST Core/KIM, on the- 514 * (a) last un-register when chip need not be powered there-after, 515 * (b) upon failure to either install ldisc or download firmware. 516 * The function is responsible to (a) notify UIM about un-installation, 517 * (b) flush UART if the ldisc was installed. 518 * (c) reset BT_EN - pull down nshutdown at the end. 519 * (d) invoke platform's chip disabling routine. 520 */ 521 long st_kim_stop(void *kim_data) 522 { 523 long err = 0; 524 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data; 525 struct ti_st_plat_data *pdata = 526 kim_gdata->kim_pdev->dev.platform_data; 527 struct tty_struct *tty = kim_gdata->core_data->tty; 528 529 reinit_completion(&kim_gdata->ldisc_installed); 530 531 if (tty) { /* can be called before ldisc is installed */ 532 /* Flush any pending characters in the driver and discipline. */ 533 tty_ldisc_flush(tty); 534 tty_driver_flush_buffer(tty); 535 } 536 537 /* send uninstall notification to UIM */ 538 pr_info("ldisc_install = 0"); 539 kim_gdata->ldisc_install = 0; 540 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install"); 541 542 /* wait for ldisc to be un-installed */ 543 err = wait_for_completion_interruptible_timeout( 544 &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME)); 545 if (!err) { /* timeout */ 546 pr_err(" timed out waiting for ldisc to be un-installed"); 547 err = -ETIMEDOUT; 548 } 549 550 /* By default configure BT nShutdown to LOW state */ 551 gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW); 552 mdelay(1); 553 gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH); 554 mdelay(1); 555 gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW); 556 557 /* platform specific disable */ 558 if (pdata->chip_disable) 559 pdata->chip_disable(kim_gdata); 560 return err; 561 } 562 563 /**********************************************************************/ 564 /* functions called from subsystems */ 565 /* called when debugfs entry is read from */ 566 567 static int show_version(struct seq_file *s, void *unused) 568 { 569 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; 570 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full, 571 kim_gdata->version.chip, kim_gdata->version.maj_ver, 572 kim_gdata->version.min_ver); 573 return 0; 574 } 575 576 static int show_list(struct seq_file *s, void *unused) 577 { 578 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private; 579 kim_st_list_protocols(kim_gdata->core_data, s); 580 return 0; 581 } 582 583 static ssize_t show_install(struct device *dev, 584 struct device_attribute *attr, char *buf) 585 { 586 struct kim_data_s *kim_data = dev_get_drvdata(dev); 587 return sprintf(buf, "%d\n", kim_data->ldisc_install); 588 } 589 590 #ifdef DEBUG 591 static ssize_t store_dev_name(struct device *dev, 592 struct device_attribute *attr, const char *buf, size_t count) 593 { 594 struct kim_data_s *kim_data = dev_get_drvdata(dev); 595 pr_debug("storing dev name >%s<", buf); 596 strncpy(kim_data->dev_name, buf, count); 597 pr_debug("stored dev name >%s<", kim_data->dev_name); 598 return count; 599 } 600 601 static ssize_t store_baud_rate(struct device *dev, 602 struct device_attribute *attr, const char *buf, size_t count) 603 { 604 struct kim_data_s *kim_data = dev_get_drvdata(dev); 605 pr_debug("storing baud rate >%s<", buf); 606 sscanf(buf, "%ld", &kim_data->baud_rate); 607 pr_debug("stored baud rate >%ld<", kim_data->baud_rate); 608 return count; 609 } 610 #endif /* if DEBUG */ 611 612 static ssize_t show_dev_name(struct device *dev, 613 struct device_attribute *attr, char *buf) 614 { 615 struct kim_data_s *kim_data = dev_get_drvdata(dev); 616 return sprintf(buf, "%s\n", kim_data->dev_name); 617 } 618 619 static ssize_t show_baud_rate(struct device *dev, 620 struct device_attribute *attr, char *buf) 621 { 622 struct kim_data_s *kim_data = dev_get_drvdata(dev); 623 return sprintf(buf, "%d\n", kim_data->baud_rate); 624 } 625 626 static ssize_t show_flow_cntrl(struct device *dev, 627 struct device_attribute *attr, char *buf) 628 { 629 struct kim_data_s *kim_data = dev_get_drvdata(dev); 630 return sprintf(buf, "%d\n", kim_data->flow_cntrl); 631 } 632 633 /* structures specific for sysfs entries */ 634 static struct kobj_attribute ldisc_install = 635 __ATTR(install, 0444, (void *)show_install, NULL); 636 637 static struct kobj_attribute uart_dev_name = 638 #ifdef DEBUG /* TODO: move this to debug-fs if possible */ 639 __ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name); 640 #else 641 __ATTR(dev_name, 0444, (void *)show_dev_name, NULL); 642 #endif 643 644 static struct kobj_attribute uart_baud_rate = 645 #ifdef DEBUG /* TODO: move to debugfs */ 646 __ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate); 647 #else 648 __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL); 649 #endif 650 651 static struct kobj_attribute uart_flow_cntrl = 652 __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL); 653 654 static struct attribute *uim_attrs[] = { 655 &ldisc_install.attr, 656 &uart_dev_name.attr, 657 &uart_baud_rate.attr, 658 &uart_flow_cntrl.attr, 659 NULL, 660 }; 661 662 static const struct attribute_group uim_attr_grp = { 663 .attrs = uim_attrs, 664 }; 665 666 /** 667 * st_kim_ref - reference the core's data 668 * This references the per-ST platform device in the arch/xx/ 669 * board-xx.c file. 670 * This would enable multiple such platform devices to exist 671 * on a given platform 672 */ 673 void st_kim_ref(struct st_data_s **core_data, int id) 674 { 675 struct platform_device *pdev; 676 struct kim_data_s *kim_gdata; 677 /* get kim_gdata reference from platform device */ 678 pdev = st_get_plat_device(id); 679 if (!pdev) 680 goto err; 681 kim_gdata = platform_get_drvdata(pdev); 682 if (!kim_gdata) 683 goto err; 684 685 *core_data = kim_gdata->core_data; 686 return; 687 err: 688 *core_data = NULL; 689 } 690 691 static int kim_version_open(struct inode *i, struct file *f) 692 { 693 return single_open(f, show_version, i->i_private); 694 } 695 696 static int kim_list_open(struct inode *i, struct file *f) 697 { 698 return single_open(f, show_list, i->i_private); 699 } 700 701 static const struct file_operations version_debugfs_fops = { 702 /* version info */ 703 .open = kim_version_open, 704 .read = seq_read, 705 .llseek = seq_lseek, 706 .release = single_release, 707 }; 708 static const struct file_operations list_debugfs_fops = { 709 /* protocols info */ 710 .open = kim_list_open, 711 .read = seq_read, 712 .llseek = seq_lseek, 713 .release = single_release, 714 }; 715 716 /**********************************************************************/ 717 /* functions called from platform device driver subsystem 718 * need to have a relevant platform device entry in the platform's 719 * board-*.c file 720 */ 721 722 static struct dentry *kim_debugfs_dir; 723 static int kim_probe(struct platform_device *pdev) 724 { 725 struct kim_data_s *kim_gdata; 726 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 727 int err; 728 729 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) { 730 /* multiple devices could exist */ 731 st_kim_devices[pdev->id] = pdev; 732 } else { 733 /* platform's sure about existence of 1 device */ 734 st_kim_devices[0] = pdev; 735 } 736 737 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_KERNEL); 738 if (!kim_gdata) { 739 pr_err("no mem to allocate"); 740 return -ENOMEM; 741 } 742 platform_set_drvdata(pdev, kim_gdata); 743 744 err = st_core_init(&kim_gdata->core_data); 745 if (err != 0) { 746 pr_err(" ST core init failed"); 747 err = -EIO; 748 goto err_core_init; 749 } 750 /* refer to itself */ 751 kim_gdata->core_data->kim_data = kim_gdata; 752 753 /* Claim the chip enable nShutdown gpio from the system */ 754 kim_gdata->nshutdown = pdata->nshutdown_gpio; 755 err = gpio_request(kim_gdata->nshutdown, "kim"); 756 if (unlikely(err)) { 757 pr_err(" gpio %d request failed ", kim_gdata->nshutdown); 758 goto err_sysfs_group; 759 } 760 761 /* Configure nShutdown GPIO as output=0 */ 762 err = gpio_direction_output(kim_gdata->nshutdown, 0); 763 if (unlikely(err)) { 764 pr_err(" unable to configure gpio %d", kim_gdata->nshutdown); 765 goto err_sysfs_group; 766 } 767 /* get reference of pdev for request_firmware 768 */ 769 kim_gdata->kim_pdev = pdev; 770 init_completion(&kim_gdata->kim_rcvd); 771 init_completion(&kim_gdata->ldisc_installed); 772 773 err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp); 774 if (err) { 775 pr_err("failed to create sysfs entries"); 776 goto err_sysfs_group; 777 } 778 779 /* copying platform data */ 780 strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN); 781 kim_gdata->flow_cntrl = pdata->flow_cntrl; 782 kim_gdata->baud_rate = pdata->baud_rate; 783 pr_info("sysfs entries created\n"); 784 785 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL); 786 if (!kim_debugfs_dir) { 787 pr_err(" debugfs entries creation failed "); 788 return 0; 789 } 790 791 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir, 792 kim_gdata, &version_debugfs_fops); 793 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir, 794 kim_gdata, &list_debugfs_fops); 795 return 0; 796 797 err_sysfs_group: 798 st_core_exit(kim_gdata->core_data); 799 800 err_core_init: 801 kfree(kim_gdata); 802 803 return err; 804 } 805 806 static int kim_remove(struct platform_device *pdev) 807 { 808 /* free the GPIOs requested */ 809 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 810 struct kim_data_s *kim_gdata; 811 812 kim_gdata = platform_get_drvdata(pdev); 813 814 /* Free the Bluetooth/FM/GPIO 815 * nShutdown gpio from the system 816 */ 817 gpio_free(pdata->nshutdown_gpio); 818 pr_info("nshutdown GPIO Freed"); 819 820 debugfs_remove_recursive(kim_debugfs_dir); 821 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp); 822 pr_info("sysfs entries removed"); 823 824 kim_gdata->kim_pdev = NULL; 825 st_core_exit(kim_gdata->core_data); 826 827 kfree(kim_gdata); 828 kim_gdata = NULL; 829 return 0; 830 } 831 832 static int kim_suspend(struct platform_device *pdev, pm_message_t state) 833 { 834 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 835 836 if (pdata->suspend) 837 return pdata->suspend(pdev, state); 838 839 return 0; 840 } 841 842 static int kim_resume(struct platform_device *pdev) 843 { 844 struct ti_st_plat_data *pdata = pdev->dev.platform_data; 845 846 if (pdata->resume) 847 return pdata->resume(pdev); 848 849 return 0; 850 } 851 852 /**********************************************************************/ 853 /* entry point for ST KIM module, called in from ST Core */ 854 static struct platform_driver kim_platform_driver = { 855 .probe = kim_probe, 856 .remove = kim_remove, 857 .suspend = kim_suspend, 858 .resume = kim_resume, 859 .driver = { 860 .name = "kim", 861 }, 862 }; 863 864 module_platform_driver(kim_platform_driver); 865 866 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>"); 867 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips "); 868 MODULE_LICENSE("GPL"); 869