st_core.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  1. /*
  2. * Shared Transport Line discipline driver Core
  3. * This hooks up ST KIM driver and ST LL driver
  4. * Copyright (C) 2009-2010 Texas Instruments
  5. * Author: Pavan Savoy <pavan_savoy@ti.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. *
  20. */
  21. #define pr_fmt(fmt) "(stc): " fmt
  22. #include <linux/module.h>
  23. #include <linux/kernel.h>
  24. #include <linux/init.h>
  25. #include <linux/tty.h>
  26. #include <linux/seq_file.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/ti_wilink_st.h>
  29. /* function pointer pointing to either,
  30. * st_kim_recv during registration to receive fw download responses
  31. * st_int_recv after registration to receive proto stack responses
  32. */
  33. void (*st_recv) (void*, const unsigned char*, long);
  34. /********************************************************************/
  35. static void add_channel_to_table(struct st_data_s *st_gdata,
  36. struct st_proto_s *new_proto)
  37. {
  38. pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
  39. /* list now has the channel id as index itself */
  40. st_gdata->list[new_proto->chnl_id] = new_proto;
  41. }
  42. static void remove_channel_from_table(struct st_data_s *st_gdata,
  43. struct st_proto_s *proto)
  44. {
  45. pr_info("%s: id %d\n", __func__, proto->chnl_id);
  46. st_gdata->list[proto->chnl_id] = NULL;
  47. }
  48. /* can be called in from
  49. * -- KIM (during fw download)
  50. * -- ST Core (during st_write)
  51. *
  52. * This is the internal write function - a wrapper
  53. * to tty->ops->write
  54. */
  55. int st_int_write(struct st_data_s *st_gdata,
  56. const unsigned char *data, int count)
  57. {
  58. struct tty_struct *tty;
  59. if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
  60. pr_err("tty unavailable to perform write");
  61. return -EINVAL;
  62. }
  63. tty = st_gdata->tty;
  64. #ifdef VERBOSE
  65. print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
  66. 16, 1, data, count, 0);
  67. #endif
  68. return tty->ops->write(tty, data, count);
  69. }
  70. /*
  71. * push the skb received to relevant
  72. * protocol stacks
  73. */
  74. void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
  75. {
  76. pr_info(" %s(prot:%d) ", __func__, chnl_id);
  77. if (unlikely
  78. (st_gdata == NULL || st_gdata->rx_skb == NULL
  79. || st_gdata->list[chnl_id] == NULL)) {
  80. pr_err("chnl_id %d not registered, no data to send?",
  81. chnl_id);
  82. kfree_skb(st_gdata->rx_skb);
  83. return;
  84. }
  85. /* this cannot fail
  86. * this shouldn't take long
  87. * - should be just skb_queue_tail for the
  88. * protocol stack driver
  89. */
  90. if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
  91. if (unlikely
  92. (st_gdata->list[chnl_id]->recv
  93. (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
  94. != 0)) {
  95. pr_err(" proto stack %d's ->recv failed", chnl_id);
  96. kfree_skb(st_gdata->rx_skb);
  97. return;
  98. }
  99. } else {
  100. pr_err(" proto stack %d's ->recv null", chnl_id);
  101. kfree_skb(st_gdata->rx_skb);
  102. }
  103. return;
  104. }
  105. /**
  106. * st_reg_complete -
  107. * to call registration complete callbacks
  108. * of all protocol stack drivers
  109. */
  110. void st_reg_complete(struct st_data_s *st_gdata, char err)
  111. {
  112. unsigned char i = 0;
  113. pr_info(" %s ", __func__);
  114. for (i = 0; i < ST_MAX_CHANNELS; i++) {
  115. if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
  116. st_gdata->list[i]->reg_complete_cb != NULL)) {
  117. st_gdata->list[i]->reg_complete_cb
  118. (st_gdata->list[i]->priv_data, err);
  119. pr_info("protocol %d's cb sent %d\n", i, err);
  120. if (err) { /* cleanup registered protocol */
  121. st_gdata->protos_registered--;
  122. st_gdata->list[i] = NULL;
  123. }
  124. }
  125. }
  126. }
  127. static inline int st_check_data_len(struct st_data_s *st_gdata,
  128. unsigned char chnl_id, int len)
  129. {
  130. int room = skb_tailroom(st_gdata->rx_skb);
  131. pr_debug("len %d room %d", len, room);
  132. if (!len) {
  133. /* Received packet has only packet header and
  134. * has zero length payload. So, ask ST CORE to
  135. * forward the packet to protocol driver (BT/FM/GPS)
  136. */
  137. st_send_frame(chnl_id, st_gdata);
  138. } else if (len > room) {
  139. /* Received packet's payload length is larger.
  140. * We can't accommodate it in created skb.
  141. */
  142. pr_err("Data length is too large len %d room %d", len,
  143. room);
  144. kfree_skb(st_gdata->rx_skb);
  145. } else {
  146. /* Packet header has non-zero payload length and
  147. * we have enough space in created skb. Lets read
  148. * payload data */
  149. st_gdata->rx_state = ST_W4_DATA;
  150. st_gdata->rx_count = len;
  151. return len;
  152. }
  153. /* Change ST state to continue to process next
  154. * packet */
  155. st_gdata->rx_state = ST_W4_PACKET_TYPE;
  156. st_gdata->rx_skb = NULL;
  157. st_gdata->rx_count = 0;
  158. st_gdata->rx_chnl = 0;
  159. return 0;
  160. }
  161. /**
  162. * st_wakeup_ack - internal function for action when wake-up ack
  163. * received
  164. */
  165. static inline void st_wakeup_ack(struct st_data_s *st_gdata,
  166. unsigned char cmd)
  167. {
  168. struct sk_buff *waiting_skb;
  169. unsigned long flags = 0;
  170. spin_lock_irqsave(&st_gdata->lock, flags);
  171. /* de-Q from waitQ and Q in txQ now that the
  172. * chip is awake
  173. */
  174. while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
  175. skb_queue_tail(&st_gdata->txq, waiting_skb);
  176. /* state forwarded to ST LL */
  177. st_ll_sleep_state(st_gdata, (unsigned long)cmd);
  178. spin_unlock_irqrestore(&st_gdata->lock, flags);
  179. /* wake up to send the recently copied skbs from waitQ */
  180. st_tx_wakeup(st_gdata);
  181. }
  182. /**
  183. * st_int_recv - ST's internal receive function.
  184. * Decodes received RAW data and forwards to corresponding
  185. * client drivers (Bluetooth,FM,GPS..etc).
  186. * This can receive various types of packets,
  187. * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
  188. * CH-8 packets from FM, CH-9 packets from GPS cores.
  189. */
  190. void st_int_recv(void *disc_data,
  191. const unsigned char *data, long count)
  192. {
  193. char *ptr;
  194. struct st_proto_s *proto;
  195. unsigned short payload_len = 0;
  196. int len = 0, type = 0;
  197. unsigned char *plen;
  198. struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
  199. ptr = (char *)data;
  200. /* tty_receive sent null ? */
  201. if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
  202. pr_err(" received null from TTY ");
  203. return;
  204. }
  205. pr_info("count %ld rx_state %ld"
  206. "rx_count %ld", count, st_gdata->rx_state,
  207. st_gdata->rx_count);
  208. /* Decode received bytes here */
  209. while (count) {
  210. if (st_gdata->rx_count) {
  211. len = min_t(unsigned int, st_gdata->rx_count, count);
  212. memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
  213. st_gdata->rx_count -= len;
  214. count -= len;
  215. ptr += len;
  216. if (st_gdata->rx_count)
  217. continue;
  218. /* Check ST RX state machine , where are we? */
  219. switch (st_gdata->rx_state) {
  220. /* Waiting for complete packet ? */
  221. case ST_W4_DATA:
  222. pr_debug("Complete pkt received");
  223. /* Ask ST CORE to forward
  224. * the packet to protocol driver */
  225. st_send_frame(st_gdata->rx_chnl, st_gdata);
  226. st_gdata->rx_state = ST_W4_PACKET_TYPE;
  227. st_gdata->rx_skb = NULL;
  228. continue;
  229. /* parse the header to know details */
  230. case ST_W4_HEADER:
  231. proto = st_gdata->list[st_gdata->rx_chnl];
  232. plen =
  233. &st_gdata->rx_skb->data
  234. [proto->offset_len_in_hdr];
  235. pr_info("plen pointing to %x\n", *plen);
  236. if (proto->len_size == 1)/* 1 byte len field */
  237. payload_len = *(unsigned char *)plen;
  238. else if (proto->len_size == 2)
  239. payload_len =
  240. __le16_to_cpu(*(unsigned short *)plen);
  241. else
  242. pr_info("%s: invalid length "
  243. "for id %d\n",
  244. __func__, proto->chnl_id);
  245. st_check_data_len(st_gdata, proto->chnl_id,
  246. payload_len);
  247. pr_info("off %d, pay len %d\n",
  248. proto->offset_len_in_hdr, payload_len);
  249. continue;
  250. } /* end of switch rx_state */
  251. }
  252. /* end of if rx_count */
  253. /* Check first byte of packet and identify module
  254. * owner (BT/FM/GPS) */
  255. switch (*ptr) {
  256. case LL_SLEEP_IND:
  257. case LL_SLEEP_ACK:
  258. case LL_WAKE_UP_IND:
  259. pr_info("PM packet");
  260. /* this takes appropriate action based on
  261. * sleep state received --
  262. */
  263. st_ll_sleep_state(st_gdata, *ptr);
  264. ptr++;
  265. count--;
  266. continue;
  267. case LL_WAKE_UP_ACK:
  268. pr_info("PM packet");
  269. /* wake up ack received */
  270. st_wakeup_ack(st_gdata, *ptr);
  271. ptr++;
  272. count--;
  273. continue;
  274. /* Unknow packet? */
  275. default:
  276. type = *ptr;
  277. st_gdata->rx_skb = alloc_skb(
  278. st_gdata->list[type]->max_frame_size,
  279. GFP_ATOMIC);
  280. skb_reserve(st_gdata->rx_skb,
  281. st_gdata->list[type]->reserve);
  282. /* next 2 required for BT only */
  283. st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
  284. st_gdata->rx_skb->cb[1] = 0; /*incoming*/
  285. st_gdata->rx_chnl = *ptr;
  286. st_gdata->rx_state = ST_W4_HEADER;
  287. st_gdata->rx_count = st_gdata->list[type]->hdr_len;
  288. pr_info("rx_count %ld\n", st_gdata->rx_count);
  289. };
  290. ptr++;
  291. count--;
  292. }
  293. pr_debug("done %s", __func__);
  294. return;
  295. }
  296. /**
  297. * st_int_dequeue - internal de-Q function.
  298. * If the previous data set was not written
  299. * completely, return that skb which has the pending data.
  300. * In normal cases, return top of txq.
  301. */
  302. struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
  303. {
  304. struct sk_buff *returning_skb;
  305. pr_debug("%s", __func__);
  306. if (st_gdata->tx_skb != NULL) {
  307. returning_skb = st_gdata->tx_skb;
  308. st_gdata->tx_skb = NULL;
  309. return returning_skb;
  310. }
  311. return skb_dequeue(&st_gdata->txq);
  312. }
  313. /**
  314. * st_int_enqueue - internal Q-ing function.
  315. * Will either Q the skb to txq or the tx_waitq
  316. * depending on the ST LL state.
  317. * If the chip is asleep, then Q it onto waitq and
  318. * wakeup the chip.
  319. * txq and waitq needs protection since the other contexts
  320. * may be sending data, waking up chip.
  321. */
  322. void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
  323. {
  324. unsigned long flags = 0;
  325. pr_debug("%s", __func__);
  326. spin_lock_irqsave(&st_gdata->lock, flags);
  327. switch (st_ll_getstate(st_gdata)) {
  328. case ST_LL_AWAKE:
  329. pr_info("ST LL is AWAKE, sending normally");
  330. skb_queue_tail(&st_gdata->txq, skb);
  331. break;
  332. case ST_LL_ASLEEP_TO_AWAKE:
  333. skb_queue_tail(&st_gdata->tx_waitq, skb);
  334. break;
  335. case ST_LL_AWAKE_TO_ASLEEP:
  336. pr_err("ST LL is illegal state(%ld),"
  337. "purging received skb.", st_ll_getstate(st_gdata));
  338. kfree_skb(skb);
  339. break;
  340. case ST_LL_ASLEEP:
  341. skb_queue_tail(&st_gdata->tx_waitq, skb);
  342. st_ll_wakeup(st_gdata);
  343. break;
  344. default:
  345. pr_err("ST LL is illegal state(%ld),"
  346. "purging received skb.", st_ll_getstate(st_gdata));
  347. kfree_skb(skb);
  348. break;
  349. }
  350. spin_unlock_irqrestore(&st_gdata->lock, flags);
  351. pr_debug("done %s", __func__);
  352. return;
  353. }
  354. /*
  355. * internal wakeup function
  356. * called from either
  357. * - TTY layer when write's finished
  358. * - st_write (in context of the protocol stack)
  359. */
  360. void st_tx_wakeup(struct st_data_s *st_data)
  361. {
  362. struct sk_buff *skb;
  363. unsigned long flags; /* for irq save flags */
  364. pr_debug("%s", __func__);
  365. /* check for sending & set flag sending here */
  366. if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
  367. pr_info("ST already sending");
  368. /* keep sending */
  369. set_bit(ST_TX_WAKEUP, &st_data->tx_state);
  370. return;
  371. /* TX_WAKEUP will be checked in another
  372. * context
  373. */
  374. }
  375. do { /* come back if st_tx_wakeup is set */
  376. /* woke-up to write */
  377. clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
  378. while ((skb = st_int_dequeue(st_data))) {
  379. int len;
  380. spin_lock_irqsave(&st_data->lock, flags);
  381. /* enable wake-up from TTY */
  382. set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
  383. len = st_int_write(st_data, skb->data, skb->len);
  384. skb_pull(skb, len);
  385. /* if skb->len = len as expected, skb->len=0 */
  386. if (skb->len) {
  387. /* would be the next skb to be sent */
  388. st_data->tx_skb = skb;
  389. spin_unlock_irqrestore(&st_data->lock, flags);
  390. break;
  391. }
  392. kfree_skb(skb);
  393. spin_unlock_irqrestore(&st_data->lock, flags);
  394. }
  395. /* if wake-up is set in another context- restart sending */
  396. } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
  397. /* clear flag sending */
  398. clear_bit(ST_TX_SENDING, &st_data->tx_state);
  399. }
  400. /********************************************************************/
  401. /* functions called from ST KIM
  402. */
  403. void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
  404. {
  405. seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
  406. st_gdata->protos_registered,
  407. st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
  408. st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
  409. st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
  410. }
  411. /********************************************************************/
  412. /*
  413. * functions called from protocol stack drivers
  414. * to be EXPORT-ed
  415. */
  416. long st_register(struct st_proto_s *new_proto)
  417. {
  418. struct st_data_s *st_gdata;
  419. long err = 0;
  420. unsigned long flags = 0;
  421. st_kim_ref(&st_gdata, 0);
  422. pr_info("%s(%d) ", __func__, new_proto->chnl_id);
  423. if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
  424. || new_proto->reg_complete_cb == NULL) {
  425. pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
  426. return -EINVAL;
  427. }
  428. if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
  429. pr_err("chnl_id %d not supported", new_proto->chnl_id);
  430. return -EPROTONOSUPPORT;
  431. }
  432. if (st_gdata->list[new_proto->chnl_id] != NULL) {
  433. pr_err("chnl_id %d already registered", new_proto->chnl_id);
  434. return -EALREADY;
  435. }
  436. /* can be from process context only */
  437. spin_lock_irqsave(&st_gdata->lock, flags);
  438. if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
  439. pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
  440. /* fw download in progress */
  441. st_kim_chip_toggle(new_proto->chnl_id, KIM_GPIO_ACTIVE);
  442. add_channel_to_table(st_gdata, new_proto);
  443. st_gdata->protos_registered++;
  444. new_proto->write = st_write;
  445. set_bit(ST_REG_PENDING, &st_gdata->st_state);
  446. spin_unlock_irqrestore(&st_gdata->lock, flags);
  447. return -EINPROGRESS;
  448. } else if (st_gdata->protos_registered == ST_EMPTY) {
  449. pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
  450. set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
  451. st_recv = st_kim_recv;
  452. /* release lock previously held - re-locked below */
  453. spin_unlock_irqrestore(&st_gdata->lock, flags);
  454. /* enable the ST LL - to set default chip state */
  455. st_ll_enable(st_gdata);
  456. /* this may take a while to complete
  457. * since it involves BT fw download
  458. */
  459. err = st_kim_start(st_gdata->kim_data);
  460. if (err != 0) {
  461. clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
  462. if ((st_gdata->protos_registered != ST_EMPTY) &&
  463. (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
  464. pr_err(" KIM failure complete callback ");
  465. st_reg_complete(st_gdata, err);
  466. }
  467. return -EINVAL;
  468. }
  469. /* the chnl_id might require other gpios to be toggled
  470. */
  471. st_kim_chip_toggle(new_proto->chnl_id, KIM_GPIO_ACTIVE);
  472. clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
  473. st_recv = st_int_recv;
  474. /* this is where all pending registration
  475. * are signalled to be complete by calling callback functions
  476. */
  477. if ((st_gdata->protos_registered != ST_EMPTY) &&
  478. (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
  479. pr_debug(" call reg complete callback ");
  480. st_reg_complete(st_gdata, 0);
  481. }
  482. clear_bit(ST_REG_PENDING, &st_gdata->st_state);
  483. /* check for already registered once more,
  484. * since the above check is old
  485. */
  486. if (st_gdata->list[new_proto->chnl_id] != NULL) {
  487. pr_err(" proto %d already registered ",
  488. new_proto->chnl_id);
  489. return -EALREADY;
  490. }
  491. spin_lock_irqsave(&st_gdata->lock, flags);
  492. add_channel_to_table(st_gdata, new_proto);
  493. st_gdata->protos_registered++;
  494. new_proto->write = st_write;
  495. spin_unlock_irqrestore(&st_gdata->lock, flags);
  496. return err;
  497. }
  498. /* if fw is already downloaded & new stack registers protocol */
  499. else {
  500. add_channel_to_table(st_gdata, new_proto);
  501. st_gdata->protos_registered++;
  502. new_proto->write = st_write;
  503. /* lock already held before entering else */
  504. spin_unlock_irqrestore(&st_gdata->lock, flags);
  505. return err;
  506. }
  507. pr_debug("done %s(%d) ", __func__, new_proto->chnl_id);
  508. }
  509. EXPORT_SYMBOL_GPL(st_register);
  510. /* to unregister a protocol -
  511. * to be called from protocol stack driver
  512. */
  513. long st_unregister(struct st_proto_s *proto)
  514. {
  515. long err = 0;
  516. unsigned long flags = 0;
  517. struct st_data_s *st_gdata;
  518. pr_debug("%s: %d ", __func__, proto->chnl_id);
  519. st_kim_ref(&st_gdata, 0);
  520. if (proto->chnl_id >= ST_MAX_CHANNELS) {
  521. pr_err(" chnl_id %d not supported", proto->chnl_id);
  522. return -EPROTONOSUPPORT;
  523. }
  524. spin_lock_irqsave(&st_gdata->lock, flags);
  525. if (st_gdata->list[proto->chnl_id] == NULL) {
  526. pr_err(" chnl_id %d not registered", proto->chnl_id);
  527. spin_unlock_irqrestore(&st_gdata->lock, flags);
  528. return -EPROTONOSUPPORT;
  529. }
  530. st_gdata->protos_registered--;
  531. remove_channel_from_table(st_gdata, proto);
  532. /* kim ignores BT in the below function
  533. * and handles the rest, BT is toggled
  534. * only in kim_start and kim_stop
  535. */
  536. st_kim_chip_toggle(proto->chnl_id, KIM_GPIO_INACTIVE);
  537. spin_unlock_irqrestore(&st_gdata->lock, flags);
  538. if ((st_gdata->protos_registered == ST_EMPTY) &&
  539. (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
  540. pr_info(" all chnl_ids unregistered ");
  541. /* stop traffic on tty */
  542. if (st_gdata->tty) {
  543. tty_ldisc_flush(st_gdata->tty);
  544. stop_tty(st_gdata->tty);
  545. }
  546. /* all chnl_ids now unregistered */
  547. st_kim_stop(st_gdata->kim_data);
  548. /* disable ST LL */
  549. st_ll_disable(st_gdata);
  550. }
  551. return err;
  552. }
  553. /*
  554. * called in protocol stack drivers
  555. * via the write function pointer
  556. */
  557. long st_write(struct sk_buff *skb)
  558. {
  559. struct st_data_s *st_gdata;
  560. #ifdef DEBUG
  561. unsigned char chnl_id = ST_MAX_CHANNELS;
  562. #endif
  563. long len;
  564. st_kim_ref(&st_gdata, 0);
  565. if (unlikely(skb == NULL || st_gdata == NULL
  566. || st_gdata->tty == NULL)) {
  567. pr_err("data/tty unavailable to perform write");
  568. return -EINVAL;
  569. }
  570. #ifdef DEBUG /* open-up skb to read the 1st byte */
  571. chnl_id = skb->data[0];
  572. if (unlikely(st_gdata->list[chnl_id] == NULL)) {
  573. pr_err(" chnl_id %d not registered, and writing? ",
  574. chnl_id);
  575. return -EINVAL;
  576. }
  577. #endif
  578. pr_debug("%d to be written", skb->len);
  579. len = skb->len;
  580. /* st_ll to decide where to enqueue the skb */
  581. st_int_enqueue(st_gdata, skb);
  582. /* wake up */
  583. st_tx_wakeup(st_gdata);
  584. /* return number of bytes written */
  585. return len;
  586. }
  587. /* for protocols making use of shared transport */
  588. EXPORT_SYMBOL_GPL(st_unregister);
  589. /********************************************************************/
  590. /*
  591. * functions called from TTY layer
  592. */
  593. static int st_tty_open(struct tty_struct *tty)
  594. {
  595. int err = 0;
  596. struct st_data_s *st_gdata;
  597. pr_info("%s ", __func__);
  598. st_kim_ref(&st_gdata, 0);
  599. st_gdata->tty = tty;
  600. tty->disc_data = st_gdata;
  601. /* don't do an wakeup for now */
  602. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  603. /* mem already allocated
  604. */
  605. tty->receive_room = 65536;
  606. /* Flush any pending characters in the driver and discipline. */
  607. tty_ldisc_flush(tty);
  608. tty_driver_flush_buffer(tty);
  609. /*
  610. * signal to UIM via KIM that -
  611. * installation of N_TI_WL ldisc is complete
  612. */
  613. st_kim_complete(st_gdata->kim_data);
  614. pr_debug("done %s", __func__);
  615. return err;
  616. }
  617. static void st_tty_close(struct tty_struct *tty)
  618. {
  619. unsigned char i = ST_MAX_CHANNELS;
  620. unsigned long flags = 0;
  621. struct st_data_s *st_gdata = tty->disc_data;
  622. pr_info("%s ", __func__);
  623. /* TODO:
  624. * if a protocol has been registered & line discipline
  625. * un-installed for some reason - what should be done ?
  626. */
  627. spin_lock_irqsave(&st_gdata->lock, flags);
  628. for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
  629. if (st_gdata->list[i] != NULL)
  630. pr_err("%d not un-registered", i);
  631. st_gdata->list[i] = NULL;
  632. }
  633. st_gdata->protos_registered = 0;
  634. spin_unlock_irqrestore(&st_gdata->lock, flags);
  635. /*
  636. * signal to UIM via KIM that -
  637. * N_TI_WL ldisc is un-installed
  638. */
  639. st_kim_complete(st_gdata->kim_data);
  640. st_gdata->tty = NULL;
  641. /* Flush any pending characters in the driver and discipline. */
  642. tty_ldisc_flush(tty);
  643. tty_driver_flush_buffer(tty);
  644. spin_lock_irqsave(&st_gdata->lock, flags);
  645. /* empty out txq and tx_waitq */
  646. skb_queue_purge(&st_gdata->txq);
  647. skb_queue_purge(&st_gdata->tx_waitq);
  648. /* reset the TTY Rx states of ST */
  649. st_gdata->rx_count = 0;
  650. st_gdata->rx_state = ST_W4_PACKET_TYPE;
  651. kfree_skb(st_gdata->rx_skb);
  652. st_gdata->rx_skb = NULL;
  653. spin_unlock_irqrestore(&st_gdata->lock, flags);
  654. pr_debug("%s: done ", __func__);
  655. }
  656. static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
  657. char *tty_flags, int count)
  658. {
  659. #define VERBOSE
  660. #ifdef VERBOSE
  661. print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
  662. 16, 1, data, count, 0);
  663. #endif
  664. /*
  665. * if fw download is in progress then route incoming data
  666. * to KIM for validation
  667. */
  668. st_recv(tty->disc_data, data, count);
  669. pr_debug("done %s", __func__);
  670. }
  671. /* wake-up function called in from the TTY layer
  672. * inside the internal wakeup function will be called
  673. */
  674. static void st_tty_wakeup(struct tty_struct *tty)
  675. {
  676. struct st_data_s *st_gdata = tty->disc_data;
  677. pr_debug("%s ", __func__);
  678. /* don't do an wakeup for now */
  679. clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
  680. /* call our internal wakeup */
  681. st_tx_wakeup((void *)st_gdata);
  682. }
  683. static void st_tty_flush_buffer(struct tty_struct *tty)
  684. {
  685. struct st_data_s *st_gdata = tty->disc_data;
  686. pr_debug("%s ", __func__);
  687. kfree_skb(st_gdata->tx_skb);
  688. st_gdata->tx_skb = NULL;
  689. tty->ops->flush_buffer(tty);
  690. return;
  691. }
  692. static struct tty_ldisc_ops st_ldisc_ops = {
  693. .magic = TTY_LDISC_MAGIC,
  694. .name = "n_st",
  695. .open = st_tty_open,
  696. .close = st_tty_close,
  697. .receive_buf = st_tty_receive,
  698. .write_wakeup = st_tty_wakeup,
  699. .flush_buffer = st_tty_flush_buffer,
  700. .owner = THIS_MODULE
  701. };
  702. /********************************************************************/
  703. int st_core_init(struct st_data_s **core_data)
  704. {
  705. struct st_data_s *st_gdata;
  706. long err;
  707. err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
  708. if (err) {
  709. pr_err("error registering %d line discipline %ld",
  710. N_TI_WL, err);
  711. return err;
  712. }
  713. pr_debug("registered n_shared line discipline");
  714. st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
  715. if (!st_gdata) {
  716. pr_err("memory allocation failed");
  717. err = tty_unregister_ldisc(N_TI_WL);
  718. if (err)
  719. pr_err("unable to un-register ldisc %ld", err);
  720. err = -ENOMEM;
  721. return err;
  722. }
  723. /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
  724. * will be pushed in this queue for actual transmission.
  725. */
  726. skb_queue_head_init(&st_gdata->txq);
  727. skb_queue_head_init(&st_gdata->tx_waitq);
  728. /* Locking used in st_int_enqueue() to avoid multiple execution */
  729. spin_lock_init(&st_gdata->lock);
  730. err = st_ll_init(st_gdata);
  731. if (err) {
  732. pr_err("error during st_ll initialization(%ld)", err);
  733. kfree(st_gdata);
  734. err = tty_unregister_ldisc(N_TI_WL);
  735. if (err)
  736. pr_err("unable to un-register ldisc");
  737. return err;
  738. }
  739. *core_data = st_gdata;
  740. return 0;
  741. }
  742. void st_core_exit(struct st_data_s *st_gdata)
  743. {
  744. long err;
  745. /* internal module cleanup */
  746. err = st_ll_deinit(st_gdata);
  747. if (err)
  748. pr_err("error during deinit of ST LL %ld", err);
  749. if (st_gdata != NULL) {
  750. /* Free ST Tx Qs and skbs */
  751. skb_queue_purge(&st_gdata->txq);
  752. skb_queue_purge(&st_gdata->tx_waitq);
  753. kfree_skb(st_gdata->rx_skb);
  754. kfree_skb(st_gdata->tx_skb);
  755. /* TTY ldisc cleanup */
  756. err = tty_unregister_ldisc(N_TI_WL);
  757. if (err)
  758. pr_err("unable to un-register ldisc %ld", err);
  759. /* free the global data pointer */
  760. kfree(st_gdata);
  761. }
  762. }