st_kim.c 23 KB

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