st_kim.c 23 KB

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