st_core.c 23 KB

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