st_core.c 25 KB

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