st_core.c 24 KB

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