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