st_kim.c 24 KB

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