aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc/ti-st
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/misc/ti-st')
-rw-r--r--drivers/misc/ti-st/Kconfig17
-rw-r--r--drivers/misc/ti-st/Makefile6
-rw-r--r--drivers/misc/ti-st/st_core.c872
-rw-r--r--drivers/misc/ti-st/st_kim.c797
-rw-r--r--drivers/misc/ti-st/st_ll.c150
5 files changed, 1842 insertions, 0 deletions
diff --git a/drivers/misc/ti-st/Kconfig b/drivers/misc/ti-st/Kconfig
new file mode 100644
index 000000000000..abb5de1afce3
--- /dev/null
+++ b/drivers/misc/ti-st/Kconfig
@@ -0,0 +1,17 @@
1#
2# TI's shared transport line discipline and the protocol
3# drivers (BT, FM and GPS)
4#
5menu "Texas Instruments shared transport line discipline"
6config TI_ST
7 tristate "Shared transport core driver"
8 depends on NET && GPIOLIB
9 select FW_LOADER
10 help
11 This enables the shared transport core driver for TI
12 BT / FM and GPS combo chips. This enables protocol drivers
13 to register themselves with core and send data, the responses
14 are returned to relevant protocol drivers based on their
15 packet types.
16
17endmenu
diff --git a/drivers/misc/ti-st/Makefile b/drivers/misc/ti-st/Makefile
new file mode 100644
index 000000000000..78d7ebb14749
--- /dev/null
+++ b/drivers/misc/ti-st/Makefile
@@ -0,0 +1,6 @@
1#
2# Makefile for TI's shared transport line discipline
3# and its protocol drivers (BT, FM, GPS)
4#
5obj-$(CONFIG_TI_ST) += st_drv.o
6st_drv-objs := st_core.o st_kim.o st_ll.o
diff --git a/drivers/misc/ti-st/st_core.c b/drivers/misc/ti-st/st_core.c
new file mode 100644
index 000000000000..54c91ffe4a91
--- /dev/null
+++ b/drivers/misc/ti-st/st_core.c
@@ -0,0 +1,872 @@
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#include <linux/seq_file.h>
29#include <linux/skbuff.h>
30
31#include <linux/ti_wilink_st.h>
32
33/* function pointer pointing to either,
34 * st_kim_recv during registration to receive fw download responses
35 * st_int_recv after registration to receive proto stack responses
36 */
37void (*st_recv) (void*, const unsigned char*, long);
38
39/********************************************************************/
40static void add_channel_to_table(struct st_data_s *st_gdata,
41 struct st_proto_s *new_proto)
42{
43 pr_info("%s: id %d\n", __func__, new_proto->chnl_id);
44 /* list now has the channel id as index itself */
45 st_gdata->list[new_proto->chnl_id] = new_proto;
46 st_gdata->is_registered[new_proto->chnl_id] = true;
47}
48
49static void remove_channel_from_table(struct st_data_s *st_gdata,
50 struct st_proto_s *proto)
51{
52 pr_info("%s: id %d\n", __func__, proto->chnl_id);
53/* st_gdata->list[proto->chnl_id] = NULL; */
54 st_gdata->is_registered[proto->chnl_id] = false;
55}
56
57/*
58 * called from KIM during firmware download.
59 *
60 * This is a wrapper function to tty->ops->write_room.
61 * It returns number of free space available in
62 * uart tx buffer.
63 */
64int st_get_uart_wr_room(struct st_data_s *st_gdata)
65{
66 struct tty_struct *tty;
67 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
68 pr_err("tty unavailable to perform write");
69 return -1;
70 }
71 tty = st_gdata->tty;
72 return tty->ops->write_room(tty);
73}
74
75/* can be called in from
76 * -- KIM (during fw download)
77 * -- ST Core (during st_write)
78 *
79 * This is the internal write function - a wrapper
80 * to tty->ops->write
81 */
82int st_int_write(struct st_data_s *st_gdata,
83 const unsigned char *data, int count)
84{
85 struct tty_struct *tty;
86 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
87 pr_err("tty unavailable to perform write");
88 return -EINVAL;
89 }
90 tty = st_gdata->tty;
91#ifdef VERBOSE
92 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
93 16, 1, data, count, 0);
94#endif
95 return tty->ops->write(tty, data, count);
96
97}
98
99/*
100 * push the skb received to relevant
101 * protocol stacks
102 */
103void st_send_frame(unsigned char chnl_id, struct st_data_s *st_gdata)
104{
105 pr_debug(" %s(prot:%d) ", __func__, chnl_id);
106
107 if (unlikely
108 (st_gdata == NULL || st_gdata->rx_skb == NULL
109 || st_gdata->is_registered[chnl_id] == false)) {
110 pr_err("chnl_id %d not registered, no data to send?",
111 chnl_id);
112 kfree_skb(st_gdata->rx_skb);
113 return;
114 }
115 /* this cannot fail
116 * this shouldn't take long
117 * - should be just skb_queue_tail for the
118 * protocol stack driver
119 */
120 if (likely(st_gdata->list[chnl_id]->recv != NULL)) {
121 if (unlikely
122 (st_gdata->list[chnl_id]->recv
123 (st_gdata->list[chnl_id]->priv_data, st_gdata->rx_skb)
124 != 0)) {
125 pr_err(" proto stack %d's ->recv failed", chnl_id);
126 kfree_skb(st_gdata->rx_skb);
127 return;
128 }
129 } else {
130 pr_err(" proto stack %d's ->recv null", chnl_id);
131 kfree_skb(st_gdata->rx_skb);
132 }
133 return;
134}
135
136/**
137 * st_reg_complete -
138 * to call registration complete callbacks
139 * of all protocol stack drivers
140 */
141void st_reg_complete(struct st_data_s *st_gdata, char err)
142{
143 unsigned char i = 0;
144 pr_info(" %s ", __func__);
145 for (i = 0; i < ST_MAX_CHANNELS; i++) {
146 if (likely(st_gdata != NULL &&
147 st_gdata->is_registered[i] == true &&
148 st_gdata->list[i]->reg_complete_cb != NULL)) {
149 st_gdata->list[i]->reg_complete_cb
150 (st_gdata->list[i]->priv_data, err);
151 pr_info("protocol %d's cb sent %d\n", i, err);
152 if (err) { /* cleanup registered protocol */
153 st_gdata->protos_registered--;
154 st_gdata->is_registered[i] = false;
155 }
156 }
157 }
158}
159
160static inline int st_check_data_len(struct st_data_s *st_gdata,
161 unsigned char chnl_id, int len)
162{
163 int room = skb_tailroom(st_gdata->rx_skb);
164
165 pr_debug("len %d room %d", len, room);
166
167 if (!len) {
168 /* Received packet has only packet header and
169 * has zero length payload. So, ask ST CORE to
170 * forward the packet to protocol driver (BT/FM/GPS)
171 */
172 st_send_frame(chnl_id, st_gdata);
173
174 } else if (len > room) {
175 /* Received packet's payload length is larger.
176 * We can't accommodate it in created skb.
177 */
178 pr_err("Data length is too large len %d room %d", len,
179 room);
180 kfree_skb(st_gdata->rx_skb);
181 } else {
182 /* Packet header has non-zero payload length and
183 * we have enough space in created skb. Lets read
184 * payload data */
185 st_gdata->rx_state = ST_W4_DATA;
186 st_gdata->rx_count = len;
187 return len;
188 }
189
190 /* Change ST state to continue to process next
191 * packet */
192 st_gdata->rx_state = ST_W4_PACKET_TYPE;
193 st_gdata->rx_skb = NULL;
194 st_gdata->rx_count = 0;
195 st_gdata->rx_chnl = 0;
196
197 return 0;
198}
199
200/**
201 * st_wakeup_ack - internal function for action when wake-up ack
202 * received
203 */
204static inline void st_wakeup_ack(struct st_data_s *st_gdata,
205 unsigned char cmd)
206{
207 struct sk_buff *waiting_skb;
208 unsigned long flags = 0;
209
210 spin_lock_irqsave(&st_gdata->lock, flags);
211 /* de-Q from waitQ and Q in txQ now that the
212 * chip is awake
213 */
214 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
215 skb_queue_tail(&st_gdata->txq, waiting_skb);
216
217 /* state forwarded to ST LL */
218 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
219 spin_unlock_irqrestore(&st_gdata->lock, flags);
220
221 /* wake up to send the recently copied skbs from waitQ */
222 st_tx_wakeup(st_gdata);
223}
224
225/**
226 * st_int_recv - ST's internal receive function.
227 * Decodes received RAW data and forwards to corresponding
228 * client drivers (Bluetooth,FM,GPS..etc).
229 * This can receive various types of packets,
230 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
231 * CH-8 packets from FM, CH-9 packets from GPS cores.
232 */
233void st_int_recv(void *disc_data,
234 const unsigned char *data, long count)
235{
236 char *ptr;
237 struct st_proto_s *proto;
238 unsigned short payload_len = 0;
239 int len = 0, type = 0;
240 unsigned char *plen;
241 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
242 unsigned long flags;
243
244 ptr = (char *)data;
245 /* tty_receive sent null ? */
246 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
247 pr_err(" received null from TTY ");
248 return;
249 }
250
251 pr_debug("count %ld rx_state %ld"
252 "rx_count %ld", count, st_gdata->rx_state,
253 st_gdata->rx_count);
254
255 spin_lock_irqsave(&st_gdata->lock, flags);
256 /* Decode received bytes here */
257 while (count) {
258 if (st_gdata->rx_count) {
259 len = min_t(unsigned int, st_gdata->rx_count, count);
260 memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
261 st_gdata->rx_count -= len;
262 count -= len;
263 ptr += len;
264
265 if (st_gdata->rx_count)
266 continue;
267
268 /* Check ST RX state machine , where are we? */
269 switch (st_gdata->rx_state) {
270 /* Waiting for complete packet ? */
271 case ST_W4_DATA:
272 pr_debug("Complete pkt received");
273 /* Ask ST CORE to forward
274 * the packet to protocol driver */
275 st_send_frame(st_gdata->rx_chnl, st_gdata);
276
277 st_gdata->rx_state = ST_W4_PACKET_TYPE;
278 st_gdata->rx_skb = NULL;
279 continue;
280 /* parse the header to know details */
281 case ST_W4_HEADER:
282 proto = st_gdata->list[st_gdata->rx_chnl];
283 plen =
284 &st_gdata->rx_skb->data
285 [proto->offset_len_in_hdr];
286 pr_debug("plen pointing to %x\n", *plen);
287 if (proto->len_size == 1)/* 1 byte len field */
288 payload_len = *(unsigned char *)plen;
289 else if (proto->len_size == 2)
290 payload_len =
291 __le16_to_cpu(*(unsigned short *)plen);
292 else
293 pr_info("%s: invalid length "
294 "for id %d\n",
295 __func__, proto->chnl_id);
296 st_check_data_len(st_gdata, proto->chnl_id,
297 payload_len);
298 pr_debug("off %d, pay len %d\n",
299 proto->offset_len_in_hdr, payload_len);
300 continue;
301 } /* end of switch rx_state */
302 }
303
304 /* end of if rx_count */
305 /* Check first byte of packet and identify module
306 * owner (BT/FM/GPS) */
307 switch (*ptr) {
308 case LL_SLEEP_IND:
309 case LL_SLEEP_ACK:
310 case LL_WAKE_UP_IND:
311 pr_debug("PM packet");
312 /* this takes appropriate action based on
313 * sleep state received --
314 */
315 st_ll_sleep_state(st_gdata, *ptr);
316 /* if WAKEUP_IND collides copy from waitq to txq
317 * and assume chip awake
318 */
319 spin_unlock_irqrestore(&st_gdata->lock, flags);
320 if (st_ll_getstate(st_gdata) == ST_LL_AWAKE)
321 st_wakeup_ack(st_gdata, LL_WAKE_UP_ACK);
322 spin_lock_irqsave(&st_gdata->lock, flags);
323
324 ptr++;
325 count--;
326 continue;
327 case LL_WAKE_UP_ACK:
328 pr_debug("PM packet");
329
330 spin_unlock_irqrestore(&st_gdata->lock, flags);
331 /* wake up ack received */
332 st_wakeup_ack(st_gdata, *ptr);
333 spin_lock_irqsave(&st_gdata->lock, flags);
334
335 ptr++;
336 count--;
337 continue;
338 /* Unknow packet? */
339 default:
340 type = *ptr;
341 st_gdata->rx_skb = alloc_skb(
342 st_gdata->list[type]->max_frame_size,
343 GFP_ATOMIC);
344 skb_reserve(st_gdata->rx_skb,
345 st_gdata->list[type]->reserve);
346 /* next 2 required for BT only */
347 st_gdata->rx_skb->cb[0] = type; /*pkt_type*/
348 st_gdata->rx_skb->cb[1] = 0; /*incoming*/
349 st_gdata->rx_chnl = *ptr;
350 st_gdata->rx_state = ST_W4_HEADER;
351 st_gdata->rx_count = st_gdata->list[type]->hdr_len;
352 pr_debug("rx_count %ld\n", st_gdata->rx_count);
353 };
354 ptr++;
355 count--;
356 }
357 spin_unlock_irqrestore(&st_gdata->lock, flags);
358 pr_debug("done %s", __func__);
359 return;
360}
361
362/**
363 * st_int_dequeue - internal de-Q function.
364 * If the previous data set was not written
365 * completely, return that skb which has the pending data.
366 * In normal cases, return top of txq.
367 */
368struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
369{
370 struct sk_buff *returning_skb;
371
372 pr_debug("%s", __func__);
373 if (st_gdata->tx_skb != NULL) {
374 returning_skb = st_gdata->tx_skb;
375 st_gdata->tx_skb = NULL;
376 return returning_skb;
377 }
378 return skb_dequeue(&st_gdata->txq);
379}
380
381/**
382 * st_int_enqueue - internal Q-ing function.
383 * Will either Q the skb to txq or the tx_waitq
384 * depending on the ST LL state.
385 * If the chip is asleep, then Q it onto waitq and
386 * wakeup the chip.
387 * txq and waitq needs protection since the other contexts
388 * may be sending data, waking up chip.
389 */
390void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
391{
392 unsigned long flags = 0;
393
394 pr_debug("%s", __func__);
395 spin_lock_irqsave(&st_gdata->lock, flags);
396
397 switch (st_ll_getstate(st_gdata)) {
398 case ST_LL_AWAKE:
399 pr_debug("ST LL is AWAKE, sending normally");
400 skb_queue_tail(&st_gdata->txq, skb);
401 break;
402 case ST_LL_ASLEEP_TO_AWAKE:
403 skb_queue_tail(&st_gdata->tx_waitq, skb);
404 break;
405 case ST_LL_AWAKE_TO_ASLEEP:
406 pr_err("ST LL is illegal state(%ld),"
407 "purging received skb.", st_ll_getstate(st_gdata));
408 kfree_skb(skb);
409 break;
410 case ST_LL_ASLEEP:
411 skb_queue_tail(&st_gdata->tx_waitq, skb);
412 st_ll_wakeup(st_gdata);
413 break;
414 default:
415 pr_err("ST LL is illegal state(%ld),"
416 "purging received skb.", st_ll_getstate(st_gdata));
417 kfree_skb(skb);
418 break;
419 }
420
421 spin_unlock_irqrestore(&st_gdata->lock, flags);
422 pr_debug("done %s", __func__);
423 return;
424}
425
426/*
427 * internal wakeup function
428 * called from either
429 * - TTY layer when write's finished
430 * - st_write (in context of the protocol stack)
431 */
432void st_tx_wakeup(struct st_data_s *st_data)
433{
434 struct sk_buff *skb;
435 unsigned long flags; /* for irq save flags */
436 pr_debug("%s", __func__);
437 /* check for sending & set flag sending here */
438 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
439 pr_debug("ST already sending");
440 /* keep sending */
441 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
442 return;
443 /* TX_WAKEUP will be checked in another
444 * context
445 */
446 }
447 do { /* come back if st_tx_wakeup is set */
448 /* woke-up to write */
449 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
450 while ((skb = st_int_dequeue(st_data))) {
451 int len;
452 spin_lock_irqsave(&st_data->lock, flags);
453 /* enable wake-up from TTY */
454 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
455 len = st_int_write(st_data, skb->data, skb->len);
456 skb_pull(skb, len);
457 /* if skb->len = len as expected, skb->len=0 */
458 if (skb->len) {
459 /* would be the next skb to be sent */
460 st_data->tx_skb = skb;
461 spin_unlock_irqrestore(&st_data->lock, flags);
462 break;
463 }
464 kfree_skb(skb);
465 spin_unlock_irqrestore(&st_data->lock, flags);
466 }
467 /* if wake-up is set in another context- restart sending */
468 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
469
470 /* clear flag sending */
471 clear_bit(ST_TX_SENDING, &st_data->tx_state);
472}
473
474/********************************************************************/
475/* functions called from ST KIM
476*/
477void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
478{
479 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
480 st_gdata->protos_registered,
481 st_gdata->is_registered[0x04] == true ? 'R' : 'U',
482 st_gdata->is_registered[0x08] == true ? 'R' : 'U',
483 st_gdata->is_registered[0x09] == true ? 'R' : 'U');
484}
485
486/********************************************************************/
487/*
488 * functions called from protocol stack drivers
489 * to be EXPORT-ed
490 */
491long st_register(struct st_proto_s *new_proto)
492{
493 struct st_data_s *st_gdata;
494 long err = 0;
495 unsigned long flags = 0;
496
497 st_kim_ref(&st_gdata, 0);
498 pr_info("%s(%d) ", __func__, new_proto->chnl_id);
499 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
500 || new_proto->reg_complete_cb == NULL) {
501 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
502 return -EINVAL;
503 }
504
505 if (new_proto->chnl_id >= ST_MAX_CHANNELS) {
506 pr_err("chnl_id %d not supported", new_proto->chnl_id);
507 return -EPROTONOSUPPORT;
508 }
509
510 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
511 pr_err("chnl_id %d already registered", new_proto->chnl_id);
512 return -EALREADY;
513 }
514
515 /* can be from process context only */
516 spin_lock_irqsave(&st_gdata->lock, flags);
517
518 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
519 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->chnl_id);
520 /* fw download in progress */
521
522 add_channel_to_table(st_gdata, new_proto);
523 st_gdata->protos_registered++;
524 new_proto->write = st_write;
525
526 set_bit(ST_REG_PENDING, &st_gdata->st_state);
527 spin_unlock_irqrestore(&st_gdata->lock, flags);
528 return -EINPROGRESS;
529 } else if (st_gdata->protos_registered == ST_EMPTY) {
530 pr_info(" chnl_id list empty :%d ", new_proto->chnl_id);
531 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
532 st_recv = st_kim_recv;
533
534 /* release lock previously held - re-locked below */
535 spin_unlock_irqrestore(&st_gdata->lock, flags);
536
537 /* enable the ST LL - to set default chip state */
538 st_ll_enable(st_gdata);
539 /* this may take a while to complete
540 * since it involves BT fw download
541 */
542 err = st_kim_start(st_gdata->kim_data);
543 if (err != 0) {
544 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
545 if ((st_gdata->protos_registered != ST_EMPTY) &&
546 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
547 pr_err(" KIM failure complete callback ");
548 st_reg_complete(st_gdata, err);
549 }
550 return -EINVAL;
551 }
552
553 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
554 st_recv = st_int_recv;
555
556 /* this is where all pending registration
557 * are signalled to be complete by calling callback functions
558 */
559 if ((st_gdata->protos_registered != ST_EMPTY) &&
560 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
561 pr_debug(" call reg complete callback ");
562 st_reg_complete(st_gdata, 0);
563 }
564 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
565
566 /* check for already registered once more,
567 * since the above check is old
568 */
569 if (st_gdata->is_registered[new_proto->chnl_id] == true) {
570 pr_err(" proto %d already registered ",
571 new_proto->chnl_id);
572 return -EALREADY;
573 }
574
575 spin_lock_irqsave(&st_gdata->lock, flags);
576 add_channel_to_table(st_gdata, new_proto);
577 st_gdata->protos_registered++;
578 new_proto->write = st_write;
579 spin_unlock_irqrestore(&st_gdata->lock, flags);
580 return err;
581 }
582 /* if fw is already downloaded & new stack registers protocol */
583 else {
584 add_channel_to_table(st_gdata, new_proto);
585 st_gdata->protos_registered++;
586 new_proto->write = st_write;
587
588 /* lock already held before entering else */
589 spin_unlock_irqrestore(&st_gdata->lock, flags);
590 return err;
591 }
592 pr_debug("done %s(%d) ", __func__, new_proto->chnl_id);
593}
594EXPORT_SYMBOL_GPL(st_register);
595
596/* to unregister a protocol -
597 * to be called from protocol stack driver
598 */
599long st_unregister(struct st_proto_s *proto)
600{
601 long err = 0;
602 unsigned long flags = 0;
603 struct st_data_s *st_gdata;
604
605 pr_debug("%s: %d ", __func__, proto->chnl_id);
606
607 st_kim_ref(&st_gdata, 0);
608 if (!st_gdata || proto->chnl_id >= ST_MAX_CHANNELS) {
609 pr_err(" chnl_id %d not supported", proto->chnl_id);
610 return -EPROTONOSUPPORT;
611 }
612
613 spin_lock_irqsave(&st_gdata->lock, flags);
614
615 if (st_gdata->list[proto->chnl_id] == NULL) {
616 pr_err(" chnl_id %d not registered", proto->chnl_id);
617 spin_unlock_irqrestore(&st_gdata->lock, flags);
618 return -EPROTONOSUPPORT;
619 }
620
621 st_gdata->protos_registered--;
622 remove_channel_from_table(st_gdata, proto);
623 spin_unlock_irqrestore(&st_gdata->lock, flags);
624
625 if ((st_gdata->protos_registered == ST_EMPTY) &&
626 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
627 pr_info(" all chnl_ids unregistered ");
628
629 /* stop traffic on tty */
630 if (st_gdata->tty) {
631 tty_ldisc_flush(st_gdata->tty);
632 stop_tty(st_gdata->tty);
633 }
634
635 /* all chnl_ids now unregistered */
636 st_kim_stop(st_gdata->kim_data);
637 /* disable ST LL */
638 st_ll_disable(st_gdata);
639 }
640 return err;
641}
642
643/*
644 * called in protocol stack drivers
645 * via the write function pointer
646 */
647long st_write(struct sk_buff *skb)
648{
649 struct st_data_s *st_gdata;
650 long len;
651
652 st_kim_ref(&st_gdata, 0);
653 if (unlikely(skb == NULL || st_gdata == NULL
654 || st_gdata->tty == NULL)) {
655 pr_err("data/tty unavailable to perform write");
656 return -EINVAL;
657 }
658
659 pr_debug("%d to be written", skb->len);
660 len = skb->len;
661
662 /* st_ll to decide where to enqueue the skb */
663 st_int_enqueue(st_gdata, skb);
664 /* wake up */
665 st_tx_wakeup(st_gdata);
666
667 /* return number of bytes written */
668 return len;
669}
670
671/* for protocols making use of shared transport */
672EXPORT_SYMBOL_GPL(st_unregister);
673
674/********************************************************************/
675/*
676 * functions called from TTY layer
677 */
678static int st_tty_open(struct tty_struct *tty)
679{
680 int err = 0;
681 struct st_data_s *st_gdata;
682 pr_info("%s ", __func__);
683
684 st_kim_ref(&st_gdata, 0);
685 st_gdata->tty = tty;
686 tty->disc_data = st_gdata;
687
688 /* don't do an wakeup for now */
689 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
690
691 /* mem already allocated
692 */
693 tty->receive_room = 65536;
694 /* Flush any pending characters in the driver and discipline. */
695 tty_ldisc_flush(tty);
696 tty_driver_flush_buffer(tty);
697 /*
698 * signal to UIM via KIM that -
699 * installation of N_TI_WL ldisc is complete
700 */
701 st_kim_complete(st_gdata->kim_data);
702 pr_debug("done %s", __func__);
703 return err;
704}
705
706static void st_tty_close(struct tty_struct *tty)
707{
708 unsigned char i = ST_MAX_CHANNELS;
709 unsigned long flags = 0;
710 struct st_data_s *st_gdata = tty->disc_data;
711
712 pr_info("%s ", __func__);
713
714 /* TODO:
715 * if a protocol has been registered & line discipline
716 * un-installed for some reason - what should be done ?
717 */
718 spin_lock_irqsave(&st_gdata->lock, flags);
719 for (i = ST_BT; i < ST_MAX_CHANNELS; i++) {
720 if (st_gdata->list[i] != NULL)
721 pr_err("%d not un-registered", i);
722 st_gdata->list[i] = NULL;
723 }
724 st_gdata->protos_registered = 0;
725 spin_unlock_irqrestore(&st_gdata->lock, flags);
726 /*
727 * signal to UIM via KIM that -
728 * N_TI_WL ldisc is un-installed
729 */
730 st_kim_complete(st_gdata->kim_data);
731 st_gdata->tty = NULL;
732 /* Flush any pending characters in the driver and discipline. */
733 tty_ldisc_flush(tty);
734 tty_driver_flush_buffer(tty);
735
736 spin_lock_irqsave(&st_gdata->lock, flags);
737 /* empty out txq and tx_waitq */
738 skb_queue_purge(&st_gdata->txq);
739 skb_queue_purge(&st_gdata->tx_waitq);
740 /* reset the TTY Rx states of ST */
741 st_gdata->rx_count = 0;
742 st_gdata->rx_state = ST_W4_PACKET_TYPE;
743 kfree_skb(st_gdata->rx_skb);
744 st_gdata->rx_skb = NULL;
745 spin_unlock_irqrestore(&st_gdata->lock, flags);
746
747 pr_debug("%s: done ", __func__);
748}
749
750static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
751 char *tty_flags, int count)
752{
753#ifdef VERBOSE
754 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
755 16, 1, data, count, 0);
756#endif
757
758 /*
759 * if fw download is in progress then route incoming data
760 * to KIM for validation
761 */
762 st_recv(tty->disc_data, data, count);
763 pr_debug("done %s", __func__);
764}
765
766/* wake-up function called in from the TTY layer
767 * inside the internal wakeup function will be called
768 */
769static void st_tty_wakeup(struct tty_struct *tty)
770{
771 struct st_data_s *st_gdata = tty->disc_data;
772 pr_debug("%s ", __func__);
773 /* don't do an wakeup for now */
774 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
775
776 /* call our internal wakeup */
777 st_tx_wakeup((void *)st_gdata);
778}
779
780static void st_tty_flush_buffer(struct tty_struct *tty)
781{
782 struct st_data_s *st_gdata = tty->disc_data;
783 pr_debug("%s ", __func__);
784
785 kfree_skb(st_gdata->tx_skb);
786 st_gdata->tx_skb = NULL;
787
788 tty->ops->flush_buffer(tty);
789 return;
790}
791
792static struct tty_ldisc_ops st_ldisc_ops = {
793 .magic = TTY_LDISC_MAGIC,
794 .name = "n_st",
795 .open = st_tty_open,
796 .close = st_tty_close,
797 .receive_buf = st_tty_receive,
798 .write_wakeup = st_tty_wakeup,
799 .flush_buffer = st_tty_flush_buffer,
800 .owner = THIS_MODULE
801};
802
803/********************************************************************/
804int st_core_init(struct st_data_s **core_data)
805{
806 struct st_data_s *st_gdata;
807 long err;
808
809 err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
810 if (err) {
811 pr_err("error registering %d line discipline %ld",
812 N_TI_WL, err);
813 return err;
814 }
815 pr_debug("registered n_shared line discipline");
816
817 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
818 if (!st_gdata) {
819 pr_err("memory allocation failed");
820 err = tty_unregister_ldisc(N_TI_WL);
821 if (err)
822 pr_err("unable to un-register ldisc %ld", err);
823 err = -ENOMEM;
824 return err;
825 }
826
827 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
828 * will be pushed in this queue for actual transmission.
829 */
830 skb_queue_head_init(&st_gdata->txq);
831 skb_queue_head_init(&st_gdata->tx_waitq);
832
833 /* Locking used in st_int_enqueue() to avoid multiple execution */
834 spin_lock_init(&st_gdata->lock);
835
836 err = st_ll_init(st_gdata);
837 if (err) {
838 pr_err("error during st_ll initialization(%ld)", err);
839 kfree(st_gdata);
840 err = tty_unregister_ldisc(N_TI_WL);
841 if (err)
842 pr_err("unable to un-register ldisc");
843 return err;
844 }
845 *core_data = st_gdata;
846 return 0;
847}
848
849void st_core_exit(struct st_data_s *st_gdata)
850{
851 long err;
852 /* internal module cleanup */
853 err = st_ll_deinit(st_gdata);
854 if (err)
855 pr_err("error during deinit of ST LL %ld", err);
856
857 if (st_gdata != NULL) {
858 /* Free ST Tx Qs and skbs */
859 skb_queue_purge(&st_gdata->txq);
860 skb_queue_purge(&st_gdata->tx_waitq);
861 kfree_skb(st_gdata->rx_skb);
862 kfree_skb(st_gdata->tx_skb);
863 /* TTY ldisc cleanup */
864 err = tty_unregister_ldisc(N_TI_WL);
865 if (err)
866 pr_err("unable to un-register ldisc %ld", err);
867 /* free the global data pointer */
868 kfree(st_gdata);
869 }
870}
871
872
diff --git a/drivers/misc/ti-st/st_kim.c b/drivers/misc/ti-st/st_kim.c
new file mode 100644
index 000000000000..38fd2f04c07e
--- /dev/null
+++ b/drivers/misc/ti-st/st_kim.c
@@ -0,0 +1,797 @@
1/*
2 * Shared Transport Line discipline driver Core
3 * Init Manager module responsible for GPIO control
4 * and firmware download
5 * Copyright (C) 2009-2010 Texas Instruments
6 * Author: Pavan Savoy <pavan_savoy@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
23#define pr_fmt(fmt) "(stk) :" fmt
24#include <linux/platform_device.h>
25#include <linux/jiffies.h>
26#include <linux/firmware.h>
27#include <linux/delay.h>
28#include <linux/wait.h>
29#include <linux/gpio.h>
30#include <linux/debugfs.h>
31#include <linux/seq_file.h>
32#include <linux/sched.h>
33#include <linux/sysfs.h>
34#include <linux/tty.h>
35
36#include <linux/skbuff.h>
37#include <linux/ti_wilink_st.h>
38
39
40#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
41static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
42
43/**********************************************************************/
44/* internal functions */
45
46/**
47 * st_get_plat_device -
48 * function which returns the reference to the platform device
49 * requested by id. As of now only 1 such device exists (id=0)
50 * the context requesting for reference can get the id to be
51 * requested by a. The protocol driver which is registering or
52 * b. the tty device which is opened.
53 */
54static struct platform_device *st_get_plat_device(int id)
55{
56 return st_kim_devices[id];
57}
58
59/**
60 * validate_firmware_response -
61 * function to return whether the firmware response was proper
62 * in case of error don't complete so that waiting for proper
63 * response times out
64 */
65void validate_firmware_response(struct kim_data_s *kim_gdata)
66{
67 struct sk_buff *skb = kim_gdata->rx_skb;
68 if (unlikely(skb->data[5] != 0)) {
69 pr_err("no proper response during fw download");
70 pr_err("data6 %x", skb->data[5]);
71 return; /* keep waiting for the proper response */
72 }
73 /* becos of all the script being downloaded */
74 complete_all(&kim_gdata->kim_rcvd);
75 kfree_skb(skb);
76}
77
78/* check for data len received inside kim_int_recv
79 * most often hit the last case to update state to waiting for data
80 */
81static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
82{
83 register int room = skb_tailroom(kim_gdata->rx_skb);
84
85 pr_debug("len %d room %d", len, room);
86
87 if (!len) {
88 validate_firmware_response(kim_gdata);
89 } else if (len > room) {
90 /* Received packet's payload length is larger.
91 * We can't accommodate it in created skb.
92 */
93 pr_err("Data length is too large len %d room %d", len,
94 room);
95 kfree_skb(kim_gdata->rx_skb);
96 } else {
97 /* Packet header has non-zero payload length and
98 * we have enough space in created skb. Lets read
99 * payload data */
100 kim_gdata->rx_state = ST_W4_DATA;
101 kim_gdata->rx_count = len;
102 return len;
103 }
104
105 /* Change ST LL state to continue to process next
106 * packet */
107 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
108 kim_gdata->rx_skb = NULL;
109 kim_gdata->rx_count = 0;
110
111 return 0;
112}
113
114/**
115 * kim_int_recv - receive function called during firmware download
116 * firmware download responses on different UART drivers
117 * have been observed to come in bursts of different
118 * tty_receive and hence the logic
119 */
120void kim_int_recv(struct kim_data_s *kim_gdata,
121 const unsigned char *data, long count)
122{
123 const unsigned char *ptr;
124 int len = 0, type = 0;
125 unsigned char *plen;
126
127 pr_debug("%s", __func__);
128 /* Decode received bytes here */
129 ptr = data;
130 if (unlikely(ptr == NULL)) {
131 pr_err(" received null from TTY ");
132 return;
133 }
134
135 while (count) {
136 if (kim_gdata->rx_count) {
137 len = min_t(unsigned int, kim_gdata->rx_count, count);
138 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
139 kim_gdata->rx_count -= len;
140 count -= len;
141 ptr += len;
142
143 if (kim_gdata->rx_count)
144 continue;
145
146 /* Check ST RX state machine , where are we? */
147 switch (kim_gdata->rx_state) {
148 /* Waiting for complete packet ? */
149 case ST_W4_DATA:
150 pr_debug("Complete pkt received");
151 validate_firmware_response(kim_gdata);
152 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
153 kim_gdata->rx_skb = NULL;
154 continue;
155 /* Waiting for Bluetooth event header ? */
156 case ST_W4_HEADER:
157 plen =
158 (unsigned char *)&kim_gdata->rx_skb->data[1];
159 pr_debug("event hdr: plen 0x%02x\n", *plen);
160 kim_check_data_len(kim_gdata, *plen);
161 continue;
162 } /* end of switch */
163 } /* end of if rx_state */
164 switch (*ptr) {
165 /* Bluetooth event packet? */
166 case 0x04:
167 kim_gdata->rx_state = ST_W4_HEADER;
168 kim_gdata->rx_count = 2;
169 type = *ptr;
170 break;
171 default:
172 pr_info("unknown packet");
173 ptr++;
174 count--;
175 continue;
176 }
177 ptr++;
178 count--;
179 kim_gdata->rx_skb =
180 alloc_skb(1024+8, GFP_ATOMIC);
181 if (!kim_gdata->rx_skb) {
182 pr_err("can't allocate mem for new packet");
183 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
184 kim_gdata->rx_count = 0;
185 return;
186 }
187 skb_reserve(kim_gdata->rx_skb, 8);
188 kim_gdata->rx_skb->cb[0] = 4;
189 kim_gdata->rx_skb->cb[1] = 0;
190
191 }
192 return;
193}
194
195static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
196{
197 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
198 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
199
200 pr_debug("%s", __func__);
201
202 INIT_COMPLETION(kim_gdata->kim_rcvd);
203 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
204 pr_err("kim: couldn't write 4 bytes");
205 return -EIO;
206 }
207
208 if (!wait_for_completion_timeout
209 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
210 pr_err(" waiting for ver info- timed out ");
211 return -ETIMEDOUT;
212 }
213
214 version =
215 MAKEWORD(kim_gdata->resp_buffer[13],
216 kim_gdata->resp_buffer[14]);
217 chip = (version & 0x7C00) >> 10;
218 min_ver = (version & 0x007F);
219 maj_ver = (version & 0x0380) >> 7;
220
221 if (version & 0x8000)
222 maj_ver |= 0x0008;
223
224 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
225
226 /* to be accessed later via sysfs entry */
227 kim_gdata->version.full = version;
228 kim_gdata->version.chip = chip;
229 kim_gdata->version.maj_ver = maj_ver;
230 kim_gdata->version.min_ver = min_ver;
231
232 pr_info("%s", bts_scr_name);
233 return 0;
234}
235
236void skip_change_remote_baud(unsigned char **ptr, long *len)
237{
238 unsigned char *nxt_action, *cur_action;
239 cur_action = *ptr;
240
241 nxt_action = cur_action + sizeof(struct bts_action) +
242 ((struct bts_action *) cur_action)->size;
243
244 if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
245 pr_err("invalid action after change remote baud command");
246 } else {
247 *ptr = *ptr + sizeof(struct bts_action) +
248 ((struct bts_action *)cur_action)->size;
249 *len = *len - (sizeof(struct bts_action) +
250 ((struct bts_action *)cur_action)->size);
251 /* warn user on not commenting these in firmware */
252 pr_warn("skipping the wait event of change remote baud");
253 }
254}
255
256/**
257 * download_firmware -
258 * internal function which parses through the .bts firmware
259 * script file intreprets SEND, DELAY actions only as of now
260 */
261static long download_firmware(struct kim_data_s *kim_gdata)
262{
263 long err = 0;
264 long len = 0;
265 unsigned char *ptr = NULL;
266 unsigned char *action_ptr = NULL;
267 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
268 int wr_room_space;
269 int cmd_size;
270 unsigned long timeout;
271
272 err = read_local_version(kim_gdata, bts_scr_name);
273 if (err != 0) {
274 pr_err("kim: failed to read local ver");
275 return err;
276 }
277 err =
278 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
279 &kim_gdata->kim_pdev->dev);
280 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
281 (kim_gdata->fw_entry->size == 0))) {
282 pr_err(" request_firmware failed(errno %ld) for %s", err,
283 bts_scr_name);
284 return -EINVAL;
285 }
286 ptr = (void *)kim_gdata->fw_entry->data;
287 len = kim_gdata->fw_entry->size;
288 /* bts_header to remove out magic number and
289 * version
290 */
291 ptr += sizeof(struct bts_header);
292 len -= sizeof(struct bts_header);
293
294 while (len > 0 && ptr) {
295 pr_debug(" action size %d, type %d ",
296 ((struct bts_action *)ptr)->size,
297 ((struct bts_action *)ptr)->type);
298
299 switch (((struct bts_action *)ptr)->type) {
300 case ACTION_SEND_COMMAND: /* action send */
301 action_ptr = &(((struct bts_action *)ptr)->data[0]);
302 if (unlikely
303 (((struct hci_command *)action_ptr)->opcode ==
304 0xFF36)) {
305 /* ignore remote change
306 * baud rate HCI VS command */
307 pr_warn("change remote baud"
308 " rate command in firmware");
309 skip_change_remote_baud(&ptr, &len);
310 break;
311 }
312 /*
313 * Make sure we have enough free space in uart
314 * tx buffer to write current firmware command
315 */
316 cmd_size = ((struct bts_action *)ptr)->size;
317 timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
318 do {
319 wr_room_space =
320 st_get_uart_wr_room(kim_gdata->core_data);
321 if (wr_room_space < 0) {
322 pr_err("Unable to get free "
323 "space info from uart tx buffer");
324 release_firmware(kim_gdata->fw_entry);
325 return wr_room_space;
326 }
327 mdelay(1); /* wait 1ms before checking room */
328 } while ((wr_room_space < cmd_size) &&
329 time_before(jiffies, timeout));
330
331 /* Timeout happened ? */
332 if (time_after_eq(jiffies, timeout)) {
333 pr_err("Timeout while waiting for free "
334 "free space in uart tx buffer");
335 release_firmware(kim_gdata->fw_entry);
336 return -ETIMEDOUT;
337 }
338
339 /*
340 * Free space found in uart buffer, call st_int_write
341 * to send current firmware command to the uart tx
342 * buffer.
343 */
344 err = st_int_write(kim_gdata->core_data,
345 ((struct bts_action_send *)action_ptr)->data,
346 ((struct bts_action *)ptr)->size);
347 if (unlikely(err < 0)) {
348 release_firmware(kim_gdata->fw_entry);
349 return err;
350 }
351 /*
352 * Check number of bytes written to the uart tx buffer
353 * and requested command write size
354 */
355 if (err != cmd_size) {
356 pr_err("Number of bytes written to uart "
357 "tx buffer are not matching with "
358 "requested cmd write size");
359 release_firmware(kim_gdata->fw_entry);
360 return -EIO;
361 }
362 break;
363 case ACTION_WAIT_EVENT: /* wait */
364 if (!wait_for_completion_timeout
365 (&kim_gdata->kim_rcvd,
366 msecs_to_jiffies(CMD_RESP_TIME))) {
367 pr_err("response timeout during fw download ");
368 /* timed out */
369 release_firmware(kim_gdata->fw_entry);
370 return -ETIMEDOUT;
371 }
372 INIT_COMPLETION(kim_gdata->kim_rcvd);
373 break;
374 case ACTION_DELAY: /* sleep */
375 pr_info("sleep command in scr");
376 action_ptr = &(((struct bts_action *)ptr)->data[0]);
377 mdelay(((struct bts_action_delay *)action_ptr)->msec);
378 break;
379 }
380 len =
381 len - (sizeof(struct bts_action) +
382 ((struct bts_action *)ptr)->size);
383 ptr =
384 ptr + sizeof(struct bts_action) +
385 ((struct bts_action *)ptr)->size;
386 }
387 /* fw download complete */
388 release_firmware(kim_gdata->fw_entry);
389 return 0;
390}
391
392/**********************************************************************/
393/* functions called from ST core */
394/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
395 * can be because of
396 * 1. response to read local version
397 * 2. during send/recv's of firmware download
398 */
399void st_kim_recv(void *disc_data, const unsigned char *data, long count)
400{
401 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
402 struct kim_data_s *kim_gdata = st_gdata->kim_data;
403
404 /* copy to local buffer */
405 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
406 /* must be the read_ver_cmd */
407 memcpy(kim_gdata->resp_buffer, data, count);
408 complete_all(&kim_gdata->kim_rcvd);
409 return;
410 } else {
411 kim_int_recv(kim_gdata, data, count);
412 /* either completes or times out */
413 }
414 return;
415}
416
417/* to signal completion of line discipline installation
418 * called from ST Core, upon tty_open
419 */
420void st_kim_complete(void *kim_data)
421{
422 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
423 complete(&kim_gdata->ldisc_installed);
424}
425
426/**
427 * st_kim_start - called from ST Core upon 1st registration
428 * This involves toggling the chip enable gpio, reading
429 * the firmware version from chip, forming the fw file name
430 * based on the chip version, requesting the fw, parsing it
431 * and perform download(send/recv).
432 */
433long st_kim_start(void *kim_data)
434{
435 long err = 0;
436 long retry = POR_RETRY_COUNT;
437 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
438
439 pr_info(" %s", __func__);
440
441 do {
442 /* Configure BT nShutdown to HIGH state */
443 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
444 mdelay(5); /* FIXME: a proper toggle */
445 gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
446 mdelay(100);
447 /* re-initialize the completion */
448 INIT_COMPLETION(kim_gdata->ldisc_installed);
449 /* send notification to UIM */
450 kim_gdata->ldisc_install = 1;
451 pr_info("ldisc_install = 1");
452 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
453 NULL, "install");
454 /* wait for ldisc to be installed */
455 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
456 msecs_to_jiffies(LDISC_TIME));
457 if (!err) { /* timeout */
458 pr_err("line disc installation timed out ");
459 kim_gdata->ldisc_install = 0;
460 pr_info("ldisc_install = 0");
461 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
462 NULL, "install");
463 err = -ETIMEDOUT;
464 continue;
465 } else {
466 /* ldisc installed now */
467 pr_info(" line discipline installed ");
468 err = download_firmware(kim_gdata);
469 if (err != 0) {
470 pr_err("download firmware failed");
471 kim_gdata->ldisc_install = 0;
472 pr_info("ldisc_install = 0");
473 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
474 NULL, "install");
475 continue;
476 } else { /* on success don't retry */
477 break;
478 }
479 }
480 } while (retry--);
481 return err;
482}
483
484/**
485 * st_kim_stop - called from ST Core, on the last un-registration
486 * toggle low the chip enable gpio
487 */
488long st_kim_stop(void *kim_data)
489{
490 long err = 0;
491 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
492
493 INIT_COMPLETION(kim_gdata->ldisc_installed);
494
495 /* Flush any pending characters in the driver and discipline. */
496 tty_ldisc_flush(kim_gdata->core_data->tty);
497 tty_driver_flush_buffer(kim_gdata->core_data->tty);
498
499 /* send uninstall notification to UIM */
500 pr_info("ldisc_install = 0");
501 kim_gdata->ldisc_install = 0;
502 sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
503
504 /* wait for ldisc to be un-installed */
505 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
506 msecs_to_jiffies(LDISC_TIME));
507 if (!err) { /* timeout */
508 pr_err(" timed out waiting for ldisc to be un-installed");
509 return -ETIMEDOUT;
510 }
511
512 /* By default configure BT nShutdown to LOW state */
513 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
514 mdelay(1);
515 gpio_set_value(kim_gdata->nshutdown, GPIO_HIGH);
516 mdelay(1);
517 gpio_set_value(kim_gdata->nshutdown, GPIO_LOW);
518 return err;
519}
520
521/**********************************************************************/
522/* functions called from subsystems */
523/* called when debugfs entry is read from */
524
525static int show_version(struct seq_file *s, void *unused)
526{
527 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
528 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
529 kim_gdata->version.chip, kim_gdata->version.maj_ver,
530 kim_gdata->version.min_ver);
531 return 0;
532}
533
534static int show_list(struct seq_file *s, void *unused)
535{
536 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
537 kim_st_list_protocols(kim_gdata->core_data, s);
538 return 0;
539}
540
541static ssize_t show_install(struct device *dev,
542 struct device_attribute *attr, char *buf)
543{
544 struct kim_data_s *kim_data = dev_get_drvdata(dev);
545 return sprintf(buf, "%d\n", kim_data->ldisc_install);
546}
547
548static ssize_t show_dev_name(struct device *dev,
549 struct device_attribute *attr, char *buf)
550{
551 struct kim_data_s *kim_data = dev_get_drvdata(dev);
552 return sprintf(buf, "%s\n", kim_data->dev_name);
553}
554
555static ssize_t show_baud_rate(struct device *dev,
556 struct device_attribute *attr, char *buf)
557{
558 struct kim_data_s *kim_data = dev_get_drvdata(dev);
559 return sprintf(buf, "%ld\n", kim_data->baud_rate);
560}
561
562static ssize_t show_flow_cntrl(struct device *dev,
563 struct device_attribute *attr, char *buf)
564{
565 struct kim_data_s *kim_data = dev_get_drvdata(dev);
566 return sprintf(buf, "%d\n", kim_data->flow_cntrl);
567}
568
569/* structures specific for sysfs entries */
570static struct kobj_attribute ldisc_install =
571__ATTR(install, 0444, (void *)show_install, NULL);
572
573static struct kobj_attribute uart_dev_name =
574__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
575
576static struct kobj_attribute uart_baud_rate =
577__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
578
579static struct kobj_attribute uart_flow_cntrl =
580__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
581
582static struct attribute *uim_attrs[] = {
583 &ldisc_install.attr,
584 &uart_dev_name.attr,
585 &uart_baud_rate.attr,
586 &uart_flow_cntrl.attr,
587 NULL,
588};
589
590static struct attribute_group uim_attr_grp = {
591 .attrs = uim_attrs,
592};
593
594/**
595 * st_kim_ref - reference the core's data
596 * This references the per-ST platform device in the arch/xx/
597 * board-xx.c file.
598 * This would enable multiple such platform devices to exist
599 * on a given platform
600 */
601void st_kim_ref(struct st_data_s **core_data, int id)
602{
603 struct platform_device *pdev;
604 struct kim_data_s *kim_gdata;
605 /* get kim_gdata reference from platform device */
606 pdev = st_get_plat_device(id);
607 if (!pdev) {
608 *core_data = NULL;
609 return;
610 }
611 kim_gdata = dev_get_drvdata(&pdev->dev);
612 *core_data = kim_gdata->core_data;
613}
614
615static int kim_version_open(struct inode *i, struct file *f)
616{
617 return single_open(f, show_version, i->i_private);
618}
619
620static int kim_list_open(struct inode *i, struct file *f)
621{
622 return single_open(f, show_list, i->i_private);
623}
624
625static const struct file_operations version_debugfs_fops = {
626 /* version info */
627 .open = kim_version_open,
628 .read = seq_read,
629 .llseek = seq_lseek,
630 .release = single_release,
631};
632static const struct file_operations list_debugfs_fops = {
633 /* protocols info */
634 .open = kim_list_open,
635 .read = seq_read,
636 .llseek = seq_lseek,
637 .release = single_release,
638};
639
640/**********************************************************************/
641/* functions called from platform device driver subsystem
642 * need to have a relevant platform device entry in the platform's
643 * board-*.c file
644 */
645
646struct dentry *kim_debugfs_dir;
647static int kim_probe(struct platform_device *pdev)
648{
649 long status;
650 struct kim_data_s *kim_gdata;
651 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
652
653 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
654 /* multiple devices could exist */
655 st_kim_devices[pdev->id] = pdev;
656 } else {
657 /* platform's sure about existence of 1 device */
658 st_kim_devices[0] = pdev;
659 }
660
661 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
662 if (!kim_gdata) {
663 pr_err("no mem to allocate");
664 return -ENOMEM;
665 }
666 dev_set_drvdata(&pdev->dev, kim_gdata);
667
668 status = st_core_init(&kim_gdata->core_data);
669 if (status != 0) {
670 pr_err(" ST core init failed");
671 return -EIO;
672 }
673 /* refer to itself */
674 kim_gdata->core_data->kim_data = kim_gdata;
675
676 /* Claim the chip enable nShutdown gpio from the system */
677 kim_gdata->nshutdown = pdata->nshutdown_gpio;
678 status = gpio_request(kim_gdata->nshutdown, "kim");
679 if (unlikely(status)) {
680 pr_err(" gpio %ld request failed ", kim_gdata->nshutdown);
681 return status;
682 }
683
684 /* Configure nShutdown GPIO as output=0 */
685 status = gpio_direction_output(kim_gdata->nshutdown, 0);
686 if (unlikely(status)) {
687 pr_err(" unable to configure gpio %ld", kim_gdata->nshutdown);
688 return status;
689 }
690 /* get reference of pdev for request_firmware
691 */
692 kim_gdata->kim_pdev = pdev;
693 init_completion(&kim_gdata->kim_rcvd);
694 init_completion(&kim_gdata->ldisc_installed);
695
696 status = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
697 if (status) {
698 pr_err("failed to create sysfs entries");
699 return status;
700 }
701
702 /* copying platform data */
703 strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
704 kim_gdata->flow_cntrl = pdata->flow_cntrl;
705 kim_gdata->baud_rate = pdata->baud_rate;
706 pr_info("sysfs entries created\n");
707
708 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
709 if (IS_ERR(kim_debugfs_dir)) {
710 pr_err(" debugfs entries creation failed ");
711 kim_debugfs_dir = NULL;
712 return -EIO;
713 }
714
715 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
716 kim_gdata, &version_debugfs_fops);
717 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
718 kim_gdata, &list_debugfs_fops);
719 pr_info(" debugfs entries created ");
720 return 0;
721}
722
723static int kim_remove(struct platform_device *pdev)
724{
725 /* free the GPIOs requested */
726 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
727 struct kim_data_s *kim_gdata;
728
729 kim_gdata = dev_get_drvdata(&pdev->dev);
730
731 /* Free the Bluetooth/FM/GPIO
732 * nShutdown gpio from the system
733 */
734 gpio_free(pdata->nshutdown_gpio);
735 pr_info("nshutdown GPIO Freed");
736
737 debugfs_remove_recursive(kim_debugfs_dir);
738 sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
739 pr_info("sysfs entries removed");
740
741 kim_gdata->kim_pdev = NULL;
742 st_core_exit(kim_gdata->core_data);
743
744 kfree(kim_gdata);
745 kim_gdata = NULL;
746 return 0;
747}
748
749int kim_suspend(struct platform_device *pdev, pm_message_t state)
750{
751 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
752
753 if (pdata->suspend)
754 return pdata->suspend(pdev, state);
755
756 return -EOPNOTSUPP;
757}
758
759int kim_resume(struct platform_device *pdev)
760{
761 struct ti_st_plat_data *pdata = pdev->dev.platform_data;
762
763 if (pdata->resume)
764 return pdata->resume(pdev);
765
766 return -EOPNOTSUPP;
767}
768
769/**********************************************************************/
770/* entry point for ST KIM module, called in from ST Core */
771static struct platform_driver kim_platform_driver = {
772 .probe = kim_probe,
773 .remove = kim_remove,
774 .suspend = kim_suspend,
775 .resume = kim_resume,
776 .driver = {
777 .name = "kim",
778 .owner = THIS_MODULE,
779 },
780};
781
782static int __init st_kim_init(void)
783{
784 return platform_driver_register(&kim_platform_driver);
785}
786
787static void __exit st_kim_deinit(void)
788{
789 platform_driver_unregister(&kim_platform_driver);
790}
791
792
793module_init(st_kim_init);
794module_exit(st_kim_deinit);
795MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
796MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
797MODULE_LICENSE("GPL");
diff --git a/drivers/misc/ti-st/st_ll.c b/drivers/misc/ti-st/st_ll.c
new file mode 100644
index 000000000000..3f2495138855
--- /dev/null
+++ b/drivers/misc/ti-st/st_ll.c
@@ -0,0 +1,150 @@
1/*
2 * Shared Transport driver
3 * HCI-LL module responsible for TI proprietary HCI_LL protocol
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) "(stll) :" fmt
23#include <linux/skbuff.h>
24#include <linux/module.h>
25#include <linux/ti_wilink_st.h>
26
27/**********************************************************************/
28/* internal functions */
29static void send_ll_cmd(struct st_data_s *st_data,
30 unsigned char cmd)
31{
32
33 pr_debug("%s: writing %x", __func__, cmd);
34 st_int_write(st_data, &cmd, 1);
35 return;
36}
37
38static void ll_device_want_to_sleep(struct st_data_s *st_data)
39{
40 pr_debug("%s", __func__);
41 /* sanity check */
42 if (st_data->ll_state != ST_LL_AWAKE)
43 pr_err("ERR hcill: ST_LL_GO_TO_SLEEP_IND"
44 "in state %ld", st_data->ll_state);
45
46 send_ll_cmd(st_data, LL_SLEEP_ACK);
47 /* update state */
48 st_data->ll_state = ST_LL_ASLEEP;
49}
50
51static void ll_device_want_to_wakeup(struct st_data_s *st_data)
52{
53 /* diff actions in diff states */
54 switch (st_data->ll_state) {
55 case ST_LL_ASLEEP:
56 send_ll_cmd(st_data, LL_WAKE_UP_ACK); /* send wake_ack */
57 break;
58 case ST_LL_ASLEEP_TO_AWAKE:
59 /* duplicate wake_ind */
60 pr_err("duplicate wake_ind while waiting for Wake ack");
61 break;
62 case ST_LL_AWAKE:
63 /* duplicate wake_ind */
64 pr_err("duplicate wake_ind already AWAKE");
65 break;
66 case ST_LL_AWAKE_TO_ASLEEP:
67 /* duplicate wake_ind */
68 pr_err("duplicate wake_ind");
69 break;
70 }
71 /* update state */
72 st_data->ll_state = ST_LL_AWAKE;
73}
74
75/**********************************************************************/
76/* functions invoked by ST Core */
77
78/* called when ST Core wants to
79 * enable ST LL */
80void st_ll_enable(struct st_data_s *ll)
81{
82 ll->ll_state = ST_LL_AWAKE;
83}
84
85/* called when ST Core /local module wants to
86 * disable ST LL */
87void st_ll_disable(struct st_data_s *ll)
88{
89 ll->ll_state = ST_LL_INVALID;
90}
91
92/* called when ST Core wants to update the state */
93void st_ll_wakeup(struct st_data_s *ll)
94{
95 if (likely(ll->ll_state != ST_LL_AWAKE)) {
96 send_ll_cmd(ll, LL_WAKE_UP_IND); /* WAKE_IND */
97 ll->ll_state = ST_LL_ASLEEP_TO_AWAKE;
98 } else {
99 /* don't send the duplicate wake_indication */
100 pr_err(" Chip already AWAKE ");
101 }
102}
103
104/* called when ST Core wants the state */
105unsigned long st_ll_getstate(struct st_data_s *ll)
106{
107 pr_debug(" returning state %ld", ll->ll_state);
108 return ll->ll_state;
109}
110
111/* called from ST Core, when a PM related packet arrives */
112unsigned long st_ll_sleep_state(struct st_data_s *st_data,
113 unsigned char cmd)
114{
115 switch (cmd) {
116 case LL_SLEEP_IND: /* sleep ind */
117 pr_debug("sleep indication recvd");
118 ll_device_want_to_sleep(st_data);
119 break;
120 case LL_SLEEP_ACK: /* sleep ack */
121 pr_err("sleep ack rcvd: host shouldn't");
122 break;
123 case LL_WAKE_UP_IND: /* wake ind */
124 pr_debug("wake indication recvd");
125 ll_device_want_to_wakeup(st_data);
126 break;
127 case LL_WAKE_UP_ACK: /* wake ack */
128 pr_debug("wake ack rcvd");
129 st_data->ll_state = ST_LL_AWAKE;
130 break;
131 default:
132 pr_err(" unknown input/state ");
133 return -EINVAL;
134 }
135 return 0;
136}
137
138/* Called from ST CORE to initialize ST LL */
139long st_ll_init(struct st_data_s *ll)
140{
141 /* set state to invalid */
142 ll->ll_state = ST_LL_INVALID;
143 return 0;
144}
145
146/* Called from ST CORE to de-initialize ST LL */
147long st_ll_deinit(struct st_data_s *ll)
148{
149 return 0;
150}