Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  Shared Transport Line discipline driver Core
0004  *  Init Manager module responsible for GPIO control
0005  *  and firmware download
0006  *  Copyright (C) 2009-2010 Texas Instruments
0007  *  Author: Pavan Savoy <pavan_savoy@ti.com>
0008  */
0009 
0010 #define pr_fmt(fmt) "(stk) :" fmt
0011 #include <linux/platform_device.h>
0012 #include <linux/jiffies.h>
0013 #include <linux/firmware.h>
0014 #include <linux/delay.h>
0015 #include <linux/wait.h>
0016 #include <linux/gpio.h>
0017 #include <linux/debugfs.h>
0018 #include <linux/seq_file.h>
0019 #include <linux/sched.h>
0020 #include <linux/sysfs.h>
0021 #include <linux/tty.h>
0022 
0023 #include <linux/skbuff.h>
0024 #include <linux/ti_wilink_st.h>
0025 #include <linux/module.h>
0026 
0027 #define MAX_ST_DEVICES  3   /* Imagine 1 on each UART for now */
0028 static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
0029 
0030 /**********************************************************************/
0031 /* internal functions */
0032 
0033 /*
0034  * st_get_plat_device -
0035  *  function which returns the reference to the platform device
0036  *  requested by id. As of now only 1 such device exists (id=0)
0037  *  the context requesting for reference can get the id to be
0038  *  requested by a. The protocol driver which is registering or
0039  *  b. the tty device which is opened.
0040  */
0041 static struct platform_device *st_get_plat_device(int id)
0042 {
0043     return st_kim_devices[id];
0044 }
0045 
0046 /*
0047  * validate_firmware_response -
0048  *  function to return whether the firmware response was proper
0049  *  in case of error don't complete so that waiting for proper
0050  *  response times out
0051  */
0052 static void validate_firmware_response(struct kim_data_s *kim_gdata)
0053 {
0054     struct sk_buff *skb = kim_gdata->rx_skb;
0055     if (!skb)
0056         return;
0057 
0058     /*
0059      * these magic numbers are the position in the response buffer which
0060      * allows us to distinguish whether the response is for the read
0061      * version info. command
0062      */
0063     if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
0064             skb->data[4] == 0x10 && skb->data[5] == 0x00) {
0065         /* fw version response */
0066         memcpy(kim_gdata->resp_buffer,
0067                 kim_gdata->rx_skb->data,
0068                 kim_gdata->rx_skb->len);
0069         kim_gdata->rx_state = ST_W4_PACKET_TYPE;
0070         kim_gdata->rx_skb = NULL;
0071         kim_gdata->rx_count = 0;
0072     } else if (unlikely(skb->data[5] != 0)) {
0073         pr_err("no proper response during fw download");
0074         pr_err("data6 %x", skb->data[5]);
0075         kfree_skb(skb);
0076         return;     /* keep waiting for the proper response */
0077     }
0078     /* becos of all the script being downloaded */
0079     complete_all(&kim_gdata->kim_rcvd);
0080     kfree_skb(skb);
0081 }
0082 
0083 /*
0084  * check for data len received inside kim_int_recv
0085  * most often hit the last case to update state to waiting for data
0086  */
0087 static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
0088 {
0089     register int room = skb_tailroom(kim_gdata->rx_skb);
0090 
0091     pr_debug("len %d room %d", len, room);
0092 
0093     if (!len) {
0094         validate_firmware_response(kim_gdata);
0095     } else if (len > room) {
0096         /*
0097          * Received packet's payload length is larger.
0098          * We can't accommodate it in created skb.
0099          */
0100         pr_err("Data length is too large len %d room %d", len,
0101                room);
0102         kfree_skb(kim_gdata->rx_skb);
0103     } else {
0104         /*
0105          * Packet header has non-zero payload length and
0106          * we have enough space in created skb. Lets read
0107          * payload data */
0108         kim_gdata->rx_state = ST_W4_DATA;
0109         kim_gdata->rx_count = len;
0110         return len;
0111     }
0112 
0113     /*
0114      * Change ST LL state to continue to process next
0115      * packet
0116      */
0117     kim_gdata->rx_state = ST_W4_PACKET_TYPE;
0118     kim_gdata->rx_skb = NULL;
0119     kim_gdata->rx_count = 0;
0120 
0121     return 0;
0122 }
0123 
0124 /*
0125  * kim_int_recv - receive function called during firmware download
0126  *  firmware download responses on different UART drivers
0127  *  have been observed to come in bursts of different
0128  *  tty_receive and hence the logic
0129  */
0130 static void kim_int_recv(struct kim_data_s *kim_gdata,
0131     const unsigned char *data, long count)
0132 {
0133     const unsigned char *ptr;
0134     int len = 0;
0135     unsigned char *plen;
0136 
0137     pr_debug("%s", __func__);
0138     /* Decode received bytes here */
0139     ptr = data;
0140     if (unlikely(ptr == NULL)) {
0141         pr_err(" received null from TTY ");
0142         return;
0143     }
0144 
0145     while (count) {
0146         if (kim_gdata->rx_count) {
0147             len = min_t(unsigned int, kim_gdata->rx_count, count);
0148             skb_put_data(kim_gdata->rx_skb, ptr, len);
0149             kim_gdata->rx_count -= len;
0150             count -= len;
0151             ptr += len;
0152 
0153             if (kim_gdata->rx_count)
0154                 continue;
0155 
0156             /* Check ST RX state machine , where are we? */
0157             switch (kim_gdata->rx_state) {
0158                 /* Waiting for complete packet ? */
0159             case ST_W4_DATA:
0160                 pr_debug("Complete pkt received");
0161                 validate_firmware_response(kim_gdata);
0162                 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
0163                 kim_gdata->rx_skb = NULL;
0164                 continue;
0165                 /* Waiting for Bluetooth event header ? */
0166             case ST_W4_HEADER:
0167                 plen =
0168                 (unsigned char *)&kim_gdata->rx_skb->data[1];
0169                 pr_debug("event hdr: plen 0x%02x\n", *plen);
0170                 kim_check_data_len(kim_gdata, *plen);
0171                 continue;
0172             }   /* end of switch */
0173         }       /* end of if rx_state */
0174         switch (*ptr) {
0175             /* Bluetooth event packet? */
0176         case 0x04:
0177             kim_gdata->rx_state = ST_W4_HEADER;
0178             kim_gdata->rx_count = 2;
0179             break;
0180         default:
0181             pr_info("unknown packet");
0182             ptr++;
0183             count--;
0184             continue;
0185         }
0186         ptr++;
0187         count--;
0188         kim_gdata->rx_skb =
0189             alloc_skb(1024+8, GFP_ATOMIC);
0190         if (!kim_gdata->rx_skb) {
0191             pr_err("can't allocate mem for new packet");
0192             kim_gdata->rx_state = ST_W4_PACKET_TYPE;
0193             kim_gdata->rx_count = 0;
0194             return;
0195         }
0196         skb_reserve(kim_gdata->rx_skb, 8);
0197         kim_gdata->rx_skb->cb[0] = 4;
0198         kim_gdata->rx_skb->cb[1] = 0;
0199 
0200     }
0201     return;
0202 }
0203 
0204 static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
0205 {
0206     unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
0207     static const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
0208     long timeout;
0209 
0210     pr_debug("%s", __func__);
0211 
0212     reinit_completion(&kim_gdata->kim_rcvd);
0213     if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
0214         pr_err("kim: couldn't write 4 bytes");
0215         return -EIO;
0216     }
0217 
0218     timeout = wait_for_completion_interruptible_timeout(
0219         &kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME));
0220     if (timeout <= 0) {
0221         pr_err(" waiting for ver info- timed out or received signal");
0222         return timeout ? -ERESTARTSYS : -ETIMEDOUT;
0223     }
0224     reinit_completion(&kim_gdata->kim_rcvd);
0225     /*
0226      * the positions 12 & 13 in the response buffer provide with the
0227      * chip, major & minor numbers
0228      */
0229 
0230     version =
0231         MAKEWORD(kim_gdata->resp_buffer[12],
0232                 kim_gdata->resp_buffer[13]);
0233     chip = (version & 0x7C00) >> 10;
0234     min_ver = (version & 0x007F);
0235     maj_ver = (version & 0x0380) >> 7;
0236 
0237     if (version & 0x8000)
0238         maj_ver |= 0x0008;
0239 
0240     sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
0241         chip, maj_ver, min_ver);
0242 
0243     /* to be accessed later via sysfs entry */
0244     kim_gdata->version.full = version;
0245     kim_gdata->version.chip = chip;
0246     kim_gdata->version.maj_ver = maj_ver;
0247     kim_gdata->version.min_ver = min_ver;
0248 
0249     pr_info("%s", bts_scr_name);
0250     return 0;
0251 }
0252 
0253 static void skip_change_remote_baud(unsigned char **ptr, long *len)
0254 {
0255     unsigned char *nxt_action, *cur_action;
0256     cur_action = *ptr;
0257 
0258     nxt_action = cur_action + sizeof(struct bts_action) +
0259         ((struct bts_action *) cur_action)->size;
0260 
0261     if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
0262         pr_err("invalid action after change remote baud command");
0263     } else {
0264         *ptr = *ptr + sizeof(struct bts_action) +
0265             ((struct bts_action *)cur_action)->size;
0266         *len = *len - (sizeof(struct bts_action) +
0267                 ((struct bts_action *)cur_action)->size);
0268         /* warn user on not commenting these in firmware */
0269         pr_warn("skipping the wait event of change remote baud");
0270     }
0271 }
0272 
0273 /*
0274  * download_firmware -
0275  *  internal function which parses through the .bts firmware
0276  *  script file intreprets SEND, DELAY actions only as of now
0277  */
0278 static long download_firmware(struct kim_data_s *kim_gdata)
0279 {
0280     long err = 0;
0281     long len = 0;
0282     unsigned char *ptr = NULL;
0283     unsigned char *action_ptr = NULL;
0284     unsigned char bts_scr_name[40] = { 0 }; /* 40 char long bts scr name? */
0285     int wr_room_space;
0286     int cmd_size;
0287     unsigned long timeout;
0288 
0289     err = read_local_version(kim_gdata, bts_scr_name);
0290     if (err != 0) {
0291         pr_err("kim: failed to read local ver");
0292         return err;
0293     }
0294     err =
0295         request_firmware(&kim_gdata->fw_entry, bts_scr_name,
0296                  &kim_gdata->kim_pdev->dev);
0297     if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
0298              (kim_gdata->fw_entry->size == 0))) {
0299         pr_err(" request_firmware failed(errno %ld) for %s", err,
0300                bts_scr_name);
0301         return -EINVAL;
0302     }
0303     ptr = (void *)kim_gdata->fw_entry->data;
0304     len = kim_gdata->fw_entry->size;
0305     /*
0306      * bts_header to remove out magic number and
0307      * version
0308      */
0309     ptr += sizeof(struct bts_header);
0310     len -= sizeof(struct bts_header);
0311 
0312     while (len > 0 && ptr) {
0313         pr_debug(" action size %d, type %d ",
0314                ((struct bts_action *)ptr)->size,
0315                ((struct bts_action *)ptr)->type);
0316 
0317         switch (((struct bts_action *)ptr)->type) {
0318         case ACTION_SEND_COMMAND:   /* action send */
0319             pr_debug("S");
0320             action_ptr = &(((struct bts_action *)ptr)->data[0]);
0321             if (unlikely
0322                 (((struct hci_command *)action_ptr)->opcode ==
0323                  0xFF36)) {
0324                 /*
0325                  * ignore remote change
0326                  * baud rate HCI VS command
0327                  */
0328                 pr_warn("change remote baud"
0329                     " rate command in firmware");
0330                 skip_change_remote_baud(&ptr, &len);
0331                 break;
0332             }
0333             /*
0334              * Make sure we have enough free space in uart
0335              * tx buffer to write current firmware command
0336              */
0337             cmd_size = ((struct bts_action *)ptr)->size;
0338             timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
0339             do {
0340                 wr_room_space =
0341                     st_get_uart_wr_room(kim_gdata->core_data);
0342                 if (wr_room_space < 0) {
0343                     pr_err("Unable to get free "
0344                             "space info from uart tx buffer");
0345                     release_firmware(kim_gdata->fw_entry);
0346                     return wr_room_space;
0347                 }
0348                 mdelay(1); /* wait 1ms before checking room */
0349             } while ((wr_room_space < cmd_size) &&
0350                     time_before(jiffies, timeout));
0351 
0352             /* Timeout happened ? */
0353             if (time_after_eq(jiffies, timeout)) {
0354                 pr_err("Timeout while waiting for free "
0355                         "free space in uart tx buffer");
0356                 release_firmware(kim_gdata->fw_entry);
0357                 return -ETIMEDOUT;
0358             }
0359             /*
0360              * reinit completion before sending for the
0361              * relevant wait
0362              */
0363             reinit_completion(&kim_gdata->kim_rcvd);
0364 
0365             /*
0366              * Free space found in uart buffer, call st_int_write
0367              * to send current firmware command to the uart tx
0368              * buffer.
0369              */
0370             err = st_int_write(kim_gdata->core_data,
0371             ((struct bts_action_send *)action_ptr)->data,
0372                        ((struct bts_action *)ptr)->size);
0373             if (unlikely(err < 0)) {
0374                 release_firmware(kim_gdata->fw_entry);
0375                 return err;
0376             }
0377             /*
0378              * Check number of bytes written to the uart tx buffer
0379              * and requested command write size
0380              */
0381             if (err != cmd_size) {
0382                 pr_err("Number of bytes written to uart "
0383                         "tx buffer are not matching with "
0384                         "requested cmd write size");
0385                 release_firmware(kim_gdata->fw_entry);
0386                 return -EIO;
0387             }
0388             break;
0389         case ACTION_WAIT_EVENT:  /* wait */
0390             pr_debug("W");
0391             err = wait_for_completion_interruptible_timeout(
0392                     &kim_gdata->kim_rcvd,
0393                     msecs_to_jiffies(CMD_RESP_TIME));
0394             if (err <= 0) {
0395                 pr_err("response timeout/signaled during fw download ");
0396                 /* timed out */
0397                 release_firmware(kim_gdata->fw_entry);
0398                 return err ? -ERESTARTSYS : -ETIMEDOUT;
0399             }
0400             reinit_completion(&kim_gdata->kim_rcvd);
0401             break;
0402         case ACTION_DELAY:  /* sleep */
0403             pr_info("sleep command in scr");
0404             action_ptr = &(((struct bts_action *)ptr)->data[0]);
0405             mdelay(((struct bts_action_delay *)action_ptr)->msec);
0406             break;
0407         }
0408         len =
0409             len - (sizeof(struct bts_action) +
0410                ((struct bts_action *)ptr)->size);
0411         ptr =
0412             ptr + sizeof(struct bts_action) +
0413             ((struct bts_action *)ptr)->size;
0414     }
0415     /* fw download complete */
0416     release_firmware(kim_gdata->fw_entry);
0417     return 0;
0418 }
0419 
0420 /**********************************************************************/
0421 /* functions called from ST core */
0422 /* called from ST Core, when REG_IN_PROGRESS (registration in progress)
0423  * can be because of
0424  * 1. response to read local version
0425  * 2. during send/recv's of firmware download
0426  */
0427 void st_kim_recv(void *disc_data, const unsigned char *data, long count)
0428 {
0429     struct st_data_s    *st_gdata = (struct st_data_s *)disc_data;
0430     struct kim_data_s   *kim_gdata = st_gdata->kim_data;
0431 
0432     /*
0433      * proceed to gather all data and distinguish read fw version response
0434      * from other fw responses when data gathering is complete
0435      */
0436     kim_int_recv(kim_gdata, data, count);
0437     return;
0438 }
0439 
0440 /*
0441  * to signal completion of line discipline installation
0442  * called from ST Core, upon tty_open
0443  */
0444 void st_kim_complete(void *kim_data)
0445 {
0446     struct kim_data_s   *kim_gdata = (struct kim_data_s *)kim_data;
0447     complete(&kim_gdata->ldisc_installed);
0448 }
0449 
0450 /*
0451  * st_kim_start - called from ST Core upon 1st registration
0452  *  This involves toggling the chip enable gpio, reading
0453  *  the firmware version from chip, forming the fw file name
0454  *  based on the chip version, requesting the fw, parsing it
0455  *  and perform download(send/recv).
0456  */
0457 long st_kim_start(void *kim_data)
0458 {
0459     long err = 0;
0460     long retry = POR_RETRY_COUNT;
0461     struct ti_st_plat_data  *pdata;
0462     struct kim_data_s   *kim_gdata = (struct kim_data_s *)kim_data;
0463 
0464     pr_info(" %s", __func__);
0465     pdata = kim_gdata->kim_pdev->dev.platform_data;
0466 
0467     do {
0468         /* platform specific enabling code here */
0469         if (pdata->chip_enable)
0470             pdata->chip_enable(kim_gdata);
0471 
0472         /* Configure BT nShutdown to HIGH state */
0473         gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
0474         mdelay(5);  /* FIXME: a proper toggle */
0475         gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
0476         mdelay(100);
0477         /* re-initialize the completion */
0478         reinit_completion(&kim_gdata->ldisc_installed);
0479         /* send notification to UIM */
0480         kim_gdata->ldisc_install = 1;
0481         pr_info("ldisc_install = 1");
0482         sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
0483                 NULL, "install");
0484         /* wait for ldisc to be installed */
0485         err = wait_for_completion_interruptible_timeout(
0486             &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
0487         if (!err) {
0488             /*
0489              * ldisc installation timeout,
0490              * flush uart, power cycle BT_EN
0491              */
0492             pr_err("ldisc installation timeout");
0493             err = st_kim_stop(kim_gdata);
0494             continue;
0495         } else {
0496             /* ldisc installed now */
0497             pr_info("line discipline installed");
0498             err = download_firmware(kim_gdata);
0499             if (err != 0) {
0500                 /*
0501                  * ldisc installed but fw download failed,
0502                  * flush uart & power cycle BT_EN
0503                  */
0504                 pr_err("download firmware failed");
0505                 err = st_kim_stop(kim_gdata);
0506                 continue;
0507             } else {    /* on success don't retry */
0508                 break;
0509             }
0510         }
0511     } while (retry--);
0512     return err;
0513 }
0514 
0515 /*
0516  * st_kim_stop - stop communication with chip.
0517  *  This can be called from ST Core/KIM, on the-
0518  *  (a) last un-register when chip need not be powered there-after,
0519  *  (b) upon failure to either install ldisc or download firmware.
0520  *  The function is responsible to (a) notify UIM about un-installation,
0521  *  (b) flush UART if the ldisc was installed.
0522  *  (c) reset BT_EN - pull down nshutdown at the end.
0523  *  (d) invoke platform's chip disabling routine.
0524  */
0525 long st_kim_stop(void *kim_data)
0526 {
0527     long err = 0;
0528     struct kim_data_s   *kim_gdata = (struct kim_data_s *)kim_data;
0529     struct ti_st_plat_data  *pdata =
0530         kim_gdata->kim_pdev->dev.platform_data;
0531     struct tty_struct   *tty = kim_gdata->core_data->tty;
0532 
0533     reinit_completion(&kim_gdata->ldisc_installed);
0534 
0535     if (tty) {  /* can be called before ldisc is installed */
0536         /* Flush any pending characters in the driver and discipline. */
0537         tty_ldisc_flush(tty);
0538         tty_driver_flush_buffer(tty);
0539     }
0540 
0541     /* send uninstall notification to UIM */
0542     pr_info("ldisc_install = 0");
0543     kim_gdata->ldisc_install = 0;
0544     sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
0545 
0546     /* wait for ldisc to be un-installed */
0547     err = wait_for_completion_interruptible_timeout(
0548         &kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
0549     if (!err) {     /* timeout */
0550         pr_err(" timed out waiting for ldisc to be un-installed");
0551         err = -ETIMEDOUT;
0552     }
0553 
0554     /* By default configure BT nShutdown to LOW state */
0555     gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
0556     mdelay(1);
0557     gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
0558     mdelay(1);
0559     gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
0560 
0561     /* platform specific disable */
0562     if (pdata->chip_disable)
0563         pdata->chip_disable(kim_gdata);
0564     return err;
0565 }
0566 
0567 /**********************************************************************/
0568 /* functions called from subsystems */
0569 /* called when debugfs entry is read from */
0570 
0571 static int version_show(struct seq_file *s, void *unused)
0572 {
0573     struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
0574     seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
0575             kim_gdata->version.chip, kim_gdata->version.maj_ver,
0576             kim_gdata->version.min_ver);
0577     return 0;
0578 }
0579 
0580 static int list_show(struct seq_file *s, void *unused)
0581 {
0582     struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
0583     kim_st_list_protocols(kim_gdata->core_data, s);
0584     return 0;
0585 }
0586 
0587 static ssize_t show_install(struct device *dev,
0588         struct device_attribute *attr, char *buf)
0589 {
0590     struct kim_data_s *kim_data = dev_get_drvdata(dev);
0591     return sprintf(buf, "%d\n", kim_data->ldisc_install);
0592 }
0593 
0594 #ifdef DEBUG
0595 static ssize_t store_dev_name(struct device *dev,
0596         struct device_attribute *attr, const char *buf, size_t count)
0597 {
0598     struct kim_data_s *kim_data = dev_get_drvdata(dev);
0599     pr_debug("storing dev name >%s<", buf);
0600     strncpy(kim_data->dev_name, buf, count);
0601     pr_debug("stored dev name >%s<", kim_data->dev_name);
0602     return count;
0603 }
0604 
0605 static ssize_t store_baud_rate(struct device *dev,
0606         struct device_attribute *attr, const char *buf, size_t count)
0607 {
0608     struct kim_data_s *kim_data = dev_get_drvdata(dev);
0609     pr_debug("storing baud rate >%s<", buf);
0610     sscanf(buf, "%ld", &kim_data->baud_rate);
0611     pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
0612     return count;
0613 }
0614 #endif  /* if DEBUG */
0615 
0616 static ssize_t show_dev_name(struct device *dev,
0617         struct device_attribute *attr, char *buf)
0618 {
0619     struct kim_data_s *kim_data = dev_get_drvdata(dev);
0620     return sprintf(buf, "%s\n", kim_data->dev_name);
0621 }
0622 
0623 static ssize_t show_baud_rate(struct device *dev,
0624         struct device_attribute *attr, char *buf)
0625 {
0626     struct kim_data_s *kim_data = dev_get_drvdata(dev);
0627     return sprintf(buf, "%d\n", kim_data->baud_rate);
0628 }
0629 
0630 static ssize_t show_flow_cntrl(struct device *dev,
0631         struct device_attribute *attr, char *buf)
0632 {
0633     struct kim_data_s *kim_data = dev_get_drvdata(dev);
0634     return sprintf(buf, "%d\n", kim_data->flow_cntrl);
0635 }
0636 
0637 /* structures specific for sysfs entries */
0638 static struct kobj_attribute ldisc_install =
0639 __ATTR(install, 0444, (void *)show_install, NULL);
0640 
0641 static struct kobj_attribute uart_dev_name =
0642 #ifdef DEBUG    /* TODO: move this to debug-fs if possible */
0643 __ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
0644 #else
0645 __ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
0646 #endif
0647 
0648 static struct kobj_attribute uart_baud_rate =
0649 #ifdef DEBUG    /* TODO: move to debugfs */
0650 __ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
0651 #else
0652 __ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
0653 #endif
0654 
0655 static struct kobj_attribute uart_flow_cntrl =
0656 __ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
0657 
0658 static struct attribute *uim_attrs[] = {
0659     &ldisc_install.attr,
0660     &uart_dev_name.attr,
0661     &uart_baud_rate.attr,
0662     &uart_flow_cntrl.attr,
0663     NULL,
0664 };
0665 
0666 static const struct attribute_group uim_attr_grp = {
0667     .attrs = uim_attrs,
0668 };
0669 
0670 /*
0671  * st_kim_ref - reference the core's data
0672  *  This references the per-ST platform device in the arch/xx/
0673  *  board-xx.c file.
0674  *  This would enable multiple such platform devices to exist
0675  *  on a given platform
0676  */
0677 void st_kim_ref(struct st_data_s **core_data, int id)
0678 {
0679     struct platform_device  *pdev;
0680     struct kim_data_s   *kim_gdata;
0681     /* get kim_gdata reference from platform device */
0682     pdev = st_get_plat_device(id);
0683     if (!pdev)
0684         goto err;
0685     kim_gdata = platform_get_drvdata(pdev);
0686     if (!kim_gdata)
0687         goto err;
0688 
0689     *core_data = kim_gdata->core_data;
0690     return;
0691 err:
0692     *core_data = NULL;
0693 }
0694 
0695 DEFINE_SHOW_ATTRIBUTE(version);
0696 DEFINE_SHOW_ATTRIBUTE(list);
0697 
0698 /**********************************************************************/
0699 /* functions called from platform device driver subsystem
0700  * need to have a relevant platform device entry in the platform's
0701  * board-*.c file
0702  */
0703 
0704 static struct dentry *kim_debugfs_dir;
0705 static int kim_probe(struct platform_device *pdev)
0706 {
0707     struct kim_data_s   *kim_gdata;
0708     struct ti_st_plat_data  *pdata = pdev->dev.platform_data;
0709     int err;
0710 
0711     if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
0712         /* multiple devices could exist */
0713         st_kim_devices[pdev->id] = pdev;
0714     } else {
0715         /* platform's sure about existence of 1 device */
0716         st_kim_devices[0] = pdev;
0717     }
0718 
0719     kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_KERNEL);
0720     if (!kim_gdata) {
0721         pr_err("no mem to allocate");
0722         return -ENOMEM;
0723     }
0724     platform_set_drvdata(pdev, kim_gdata);
0725 
0726     err = st_core_init(&kim_gdata->core_data);
0727     if (err != 0) {
0728         pr_err(" ST core init failed");
0729         err = -EIO;
0730         goto err_core_init;
0731     }
0732     /* refer to itself */
0733     kim_gdata->core_data->kim_data = kim_gdata;
0734 
0735     /* Claim the chip enable nShutdown gpio from the system */
0736     kim_gdata->nshutdown = pdata->nshutdown_gpio;
0737     err = gpio_request(kim_gdata->nshutdown, "kim");
0738     if (unlikely(err)) {
0739         pr_err(" gpio %d request failed ", kim_gdata->nshutdown);
0740         goto err_sysfs_group;
0741     }
0742 
0743     /* Configure nShutdown GPIO as output=0 */
0744     err = gpio_direction_output(kim_gdata->nshutdown, 0);
0745     if (unlikely(err)) {
0746         pr_err(" unable to configure gpio %d", kim_gdata->nshutdown);
0747         goto err_sysfs_group;
0748     }
0749     /* get reference of pdev for request_firmware */
0750     kim_gdata->kim_pdev = pdev;
0751     init_completion(&kim_gdata->kim_rcvd);
0752     init_completion(&kim_gdata->ldisc_installed);
0753 
0754     err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
0755     if (err) {
0756         pr_err("failed to create sysfs entries");
0757         goto err_sysfs_group;
0758     }
0759 
0760     /* copying platform data */
0761     strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
0762     kim_gdata->flow_cntrl = pdata->flow_cntrl;
0763     kim_gdata->baud_rate = pdata->baud_rate;
0764     pr_info("sysfs entries created\n");
0765 
0766     kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
0767 
0768     debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
0769                 kim_gdata, &version_fops);
0770     debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
0771                 kim_gdata, &list_fops);
0772     return 0;
0773 
0774 err_sysfs_group:
0775     st_core_exit(kim_gdata->core_data);
0776 
0777 err_core_init:
0778     kfree(kim_gdata);
0779 
0780     return err;
0781 }
0782 
0783 static int kim_remove(struct platform_device *pdev)
0784 {
0785     /* free the GPIOs requested */
0786     struct ti_st_plat_data  *pdata = pdev->dev.platform_data;
0787     struct kim_data_s   *kim_gdata;
0788 
0789     kim_gdata = platform_get_drvdata(pdev);
0790 
0791     /*
0792      * Free the Bluetooth/FM/GPIO
0793      * nShutdown gpio from the system
0794      */
0795     gpio_free(pdata->nshutdown_gpio);
0796     pr_info("nshutdown GPIO Freed");
0797 
0798     debugfs_remove_recursive(kim_debugfs_dir);
0799     sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
0800     pr_info("sysfs entries removed");
0801 
0802     kim_gdata->kim_pdev = NULL;
0803     st_core_exit(kim_gdata->core_data);
0804 
0805     kfree(kim_gdata);
0806     kim_gdata = NULL;
0807     return 0;
0808 }
0809 
0810 static int kim_suspend(struct platform_device *pdev, pm_message_t state)
0811 {
0812     struct ti_st_plat_data  *pdata = pdev->dev.platform_data;
0813 
0814     if (pdata->suspend)
0815         return pdata->suspend(pdev, state);
0816 
0817     return 0;
0818 }
0819 
0820 static int kim_resume(struct platform_device *pdev)
0821 {
0822     struct ti_st_plat_data  *pdata = pdev->dev.platform_data;
0823 
0824     if (pdata->resume)
0825         return pdata->resume(pdev);
0826 
0827     return 0;
0828 }
0829 
0830 /**********************************************************************/
0831 /* entry point for ST KIM module, called in from ST Core */
0832 static struct platform_driver kim_platform_driver = {
0833     .probe = kim_probe,
0834     .remove = kim_remove,
0835     .suspend = kim_suspend,
0836     .resume = kim_resume,
0837     .driver = {
0838         .name = "kim",
0839     },
0840 };
0841 
0842 module_platform_driver(kim_platform_driver);
0843 
0844 MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
0845 MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
0846 MODULE_LICENSE("GPL");