st_kim.c 23 KB

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