st_core.c 24 KB

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