st_core.c 23 KB

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