st_kim.c 21 KB

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