st_core.c 24 KB

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