st_core.c 23 KB

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