st_core.c 23 KB

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