st_core.c 24 KB

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