st_kim.c 23 KB

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