st_kim.c 22 KB

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