st_kim.c 22 KB

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