st_kim.c 22 KB

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