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