st_kim.c 23 KB

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