st_core.c 26 KB

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