st_core.c 27 KB

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