st_kim.c 24 KB

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