aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc
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
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')
-rw-r--r--drivers/misc/ti-st/st_core.c1031
-rw-r--r--drivers/misc/ti-st/st_kim.c798
-rw-r--r--drivers/misc/ti-st/st_ll.c150
3 files changed, 1979 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
diff --git a/drivers/misc/ti-st/st_kim.c b/drivers/misc/ti-st/st_kim.c
new file mode 100644
index 000000000000..6d23a7222627
--- /dev/null
+++ b/drivers/misc/ti-st/st_kim.c
@@ -0,0 +1,798 @@
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/rfkill.h>
34
35/* understand BT events for fw response */
36#include <net/bluetooth/bluetooth.h>
37#include <net/bluetooth/hci_core.h>
38#include <net/bluetooth/hci.h>
39
40#include <linux/ti_wilink_st.h>
41
42
43static int kim_probe(struct platform_device *pdev);
44static int kim_remove(struct platform_device *pdev);
45
46/* KIM platform device driver structure */
47static struct platform_driver kim_platform_driver = {
48 .probe = kim_probe,
49 .remove = kim_remove,
50 /* TODO: ST driver power management during suspend/resume ?
51 */
52#if 0
53 .suspend = kim_suspend,
54 .resume = kim_resume,
55#endif
56 .driver = {
57 .name = "kim",
58 .owner = THIS_MODULE,
59 },
60};
61
62static int kim_toggle_radio(void*, bool);
63static const struct rfkill_ops kim_rfkill_ops = {
64 .set_block = kim_toggle_radio,
65};
66
67/* strings to be used for rfkill entries and by
68 * ST Core to be used for sysfs debug entry
69 */
70#define PROTO_ENTRY(type, name) name
71const unsigned char *protocol_names[] = {
72 PROTO_ENTRY(ST_BT, "Bluetooth"),
73 PROTO_ENTRY(ST_FM, "FM"),
74 PROTO_ENTRY(ST_GPS, "GPS"),
75};
76
77#define MAX_ST_DEVICES 3 /* Imagine 1 on each UART for now */
78struct platform_device *st_kim_devices[MAX_ST_DEVICES];
79
80/**********************************************************************/
81/* internal functions */
82
83/**
84 * st_get_plat_device -
85 * function which returns the reference to the platform device
86 * requested by id. As of now only 1 such device exists (id=0)
87 * the context requesting for reference can get the id to be
88 * requested by a. The protocol driver which is registering or
89 * b. the tty device which is opened.
90 */
91static struct platform_device *st_get_plat_device(int id)
92{
93 return st_kim_devices[id];
94}
95
96/**
97 * validate_firmware_response -
98 * function to return whether the firmware response was proper
99 * in case of error don't complete so that waiting for proper
100 * response times out
101 */
102void validate_firmware_response(struct kim_data_s *kim_gdata)
103{
104 struct sk_buff *skb = kim_gdata->rx_skb;
105 if (unlikely(skb->data[5] != 0)) {
106 pr_err("no proper response during fw download");
107 pr_err("data6 %x", skb->data[5]);
108 return; /* keep waiting for the proper response */
109 }
110 /* becos of all the script being downloaded */
111 complete_all(&kim_gdata->kim_rcvd);
112 kfree_skb(skb);
113}
114
115/* check for data len received inside kim_int_recv
116 * most often hit the last case to update state to waiting for data
117 */
118static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
119{
120 register int room = skb_tailroom(kim_gdata->rx_skb);
121
122 pr_debug("len %d room %d", len, room);
123
124 if (!len) {
125 validate_firmware_response(kim_gdata);
126 } else if (len > room) {
127 /* Received packet's payload length is larger.
128 * We can't accommodate it in created skb.
129 */
130 pr_err("Data length is too large len %d room %d", len,
131 room);
132 kfree_skb(kim_gdata->rx_skb);
133 } else {
134 /* Packet header has non-zero payload length and
135 * we have enough space in created skb. Lets read
136 * payload data */
137 kim_gdata->rx_state = ST_BT_W4_DATA;
138 kim_gdata->rx_count = len;
139 return len;
140 }
141
142 /* Change ST LL state to continue to process next
143 * packet */
144 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
145 kim_gdata->rx_skb = NULL;
146 kim_gdata->rx_count = 0;
147
148 return 0;
149}
150
151/**
152 * kim_int_recv - receive function called during firmware download
153 * firmware download responses on different UART drivers
154 * have been observed to come in bursts of different
155 * tty_receive and hence the logic
156 */
157void kim_int_recv(struct kim_data_s *kim_gdata,
158 const unsigned char *data, long count)
159{
160 register char *ptr;
161 struct hci_event_hdr *eh;
162 register int len = 0, type = 0;
163
164 pr_debug("%s", __func__);
165 /* Decode received bytes here */
166 ptr = (char *)data;
167 if (unlikely(ptr == NULL)) {
168 pr_err(" received null from TTY ");
169 return;
170 }
171 while (count) {
172 if (kim_gdata->rx_count) {
173 len = min_t(unsigned int, kim_gdata->rx_count, count);
174 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
175 kim_gdata->rx_count -= len;
176 count -= len;
177 ptr += len;
178
179 if (kim_gdata->rx_count)
180 continue;
181
182 /* Check ST RX state machine , where are we? */
183 switch (kim_gdata->rx_state) {
184 /* Waiting for complete packet ? */
185 case ST_BT_W4_DATA:
186 pr_debug("Complete pkt received");
187 validate_firmware_response(kim_gdata);
188 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
189 kim_gdata->rx_skb = NULL;
190 continue;
191 /* Waiting for Bluetooth event header ? */
192 case ST_BT_W4_EVENT_HDR:
193 eh = (struct hci_event_hdr *)kim_gdata->
194 rx_skb->data;
195 pr_debug("Event header: evt 0x%2.2x"
196 "plen %d", eh->evt, eh->plen);
197 kim_check_data_len(kim_gdata, eh->plen);
198 continue;
199 } /* end of switch */
200 } /* end of if rx_state */
201 switch (*ptr) {
202 /* Bluetooth event packet? */
203 case HCI_EVENT_PKT:
204 pr_info("Event packet");
205 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
206 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
207 type = HCI_EVENT_PKT;
208 break;
209 default:
210 pr_info("unknown packet");
211 ptr++;
212 count--;
213 continue;
214 }
215 ptr++;
216 count--;
217 kim_gdata->rx_skb =
218 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
219 if (!kim_gdata->rx_skb) {
220 pr_err("can't allocate mem for new packet");
221 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
222 kim_gdata->rx_count = 0;
223 return;
224 }
225 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
226 }
227 pr_info("done %s", __func__);
228 return;
229}
230
231static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
232{
233 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
234 char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
235
236 pr_debug("%s", __func__);
237
238 INIT_COMPLETION(kim_gdata->kim_rcvd);
239 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
240 pr_err("kim: couldn't write 4 bytes");
241 return -1;
242 }
243
244 if (!wait_for_completion_timeout
245 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
246 pr_err(" waiting for ver info- timed out ");
247 return -1;
248 }
249
250 version =
251 MAKEWORD(kim_gdata->resp_buffer[13],
252 kim_gdata->resp_buffer[14]);
253 chip = (version & 0x7C00) >> 10;
254 min_ver = (version & 0x007F);
255 maj_ver = (version & 0x0380) >> 7;
256
257 if (version & 0x8000)
258 maj_ver |= 0x0008;
259
260 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
261
262 /* to be accessed later via sysfs entry */
263 kim_gdata->version.full = version;
264 kim_gdata->version.chip = chip;
265 kim_gdata->version.maj_ver = maj_ver;
266 kim_gdata->version.min_ver = min_ver;
267
268 pr_info("%s", bts_scr_name);
269 return 0;
270}
271
272/**
273 * download_firmware -
274 * internal function which parses through the .bts firmware
275 * script file intreprets SEND, DELAY actions only as of now
276 */
277static long download_firmware(struct kim_data_s *kim_gdata)
278{
279 long err = 0;
280 long len = 0;
281 register unsigned char *ptr = NULL;
282 register unsigned char *action_ptr = NULL;
283 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
284
285 err = read_local_version(kim_gdata, bts_scr_name);
286 if (err != 0) {
287 pr_err("kim: failed to read local ver");
288 return err;
289 }
290 err =
291 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
292 &kim_gdata->kim_pdev->dev);
293 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
294 (kim_gdata->fw_entry->size == 0))) {
295 pr_err(" request_firmware failed(errno %ld) for %s", err,
296 bts_scr_name);
297 return -1;
298 }
299 ptr = (void *)kim_gdata->fw_entry->data;
300 len = kim_gdata->fw_entry->size;
301 /* bts_header to remove out magic number and
302 * version
303 */
304 ptr += sizeof(struct bts_header);
305 len -= sizeof(struct bts_header);
306
307 while (len > 0 && ptr) {
308 pr_debug(" action size %d, type %d ",
309 ((struct bts_action *)ptr)->size,
310 ((struct bts_action *)ptr)->type);
311
312 switch (((struct bts_action *)ptr)->type) {
313 case ACTION_SEND_COMMAND: /* action send */
314 action_ptr = &(((struct bts_action *)ptr)->data[0]);
315 if (unlikely
316 (((struct hci_command *)action_ptr)->opcode ==
317 0xFF36)) {
318 /* ignore remote change
319 * baud rate HCI VS command */
320 pr_err
321 (" change remote baud"
322 " rate command in firmware");
323 break;
324 }
325
326 INIT_COMPLETION(kim_gdata->kim_rcvd);
327 err = st_int_write(kim_gdata->core_data,
328 ((struct bts_action_send *)action_ptr)->data,
329 ((struct bts_action *)ptr)->size);
330 if (unlikely(err < 0)) {
331 release_firmware(kim_gdata->fw_entry);
332 return -1;
333 }
334 if (!wait_for_completion_timeout
335 (&kim_gdata->kim_rcvd,
336 msecs_to_jiffies(CMD_RESP_TIME))) {
337 pr_err
338 (" response timeout during fw download ");
339 /* timed out */
340 release_firmware(kim_gdata->fw_entry);
341 return -1;
342 }
343 break;
344 case ACTION_DELAY: /* sleep */
345 pr_info("sleep command in scr");
346 action_ptr = &(((struct bts_action *)ptr)->data[0]);
347 mdelay(((struct bts_action_delay *)action_ptr)->msec);
348 break;
349 }
350 len =
351 len - (sizeof(struct bts_action) +
352 ((struct bts_action *)ptr)->size);
353 ptr =
354 ptr + sizeof(struct bts_action) +
355 ((struct bts_action *)ptr)->size;
356 }
357 /* fw download complete */
358 release_firmware(kim_gdata->fw_entry);
359 return 0;
360}
361
362/**********************************************************************/
363/* functions called from ST core */
364/* function to toggle the GPIO
365 * needs to know whether the GPIO is active high or active low
366 */
367void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
368{
369 struct platform_device *kim_pdev;
370 struct kim_data_s *kim_gdata;
371 pr_info(" %s ", __func__);
372
373 kim_pdev = st_get_plat_device(0);
374 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
375
376 if (kim_gdata->gpios[type] == -1) {
377 pr_info(" gpio not requested for protocol %s",
378 protocol_names[type]);
379 return;
380 }
381 switch (type) {
382 case ST_BT:
383 /*Do Nothing */
384 break;
385
386 case ST_FM:
387 if (state == KIM_GPIO_ACTIVE)
388 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
389 else
390 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
391 break;
392
393 case ST_GPS:
394 if (state == KIM_GPIO_ACTIVE)
395 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
396 else
397 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
398 break;
399
400 case ST_MAX:
401 default:
402 break;
403 }
404
405 return;
406}
407
408/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
409 * can be because of
410 * 1. response to read local version
411 * 2. during send/recv's of firmware download
412 */
413void st_kim_recv(void *disc_data, const unsigned char *data, long count)
414{
415 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
416 struct kim_data_s *kim_gdata = st_gdata->kim_data;
417
418 pr_info(" %s ", __func__);
419 /* copy to local buffer */
420 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
421 /* must be the read_ver_cmd */
422 memcpy(kim_gdata->resp_buffer, data, count);
423 complete_all(&kim_gdata->kim_rcvd);
424 return;
425 } else {
426 kim_int_recv(kim_gdata, data, count);
427 /* either completes or times out */
428 }
429 return;
430}
431
432/* to signal completion of line discipline installation
433 * called from ST Core, upon tty_open
434 */
435void st_kim_complete(void *kim_data)
436{
437 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
438 complete(&kim_gdata->ldisc_installed);
439}
440
441/**
442 * st_kim_start - called from ST Core upon 1st registration
443 * This involves toggling the chip enable gpio, reading
444 * the firmware version from chip, forming the fw file name
445 * based on the chip version, requesting the fw, parsing it
446 * and perform download(send/recv).
447 */
448long st_kim_start(void *kim_data)
449{
450 long err = 0;
451 long retry = POR_RETRY_COUNT;
452 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
453
454 pr_info(" %s", __func__);
455
456 do {
457 /* TODO: this is only because rfkill sub-system
458 * doesn't send events to user-space if the state
459 * isn't changed
460 */
461 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
462 /* Configure BT nShutdown to HIGH state */
463 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
464 mdelay(5); /* FIXME: a proper toggle */
465 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
466 mdelay(100);
467 /* re-initialize the completion */
468 INIT_COMPLETION(kim_gdata->ldisc_installed);
469#if 0 /* older way of signalling user-space UIM */
470 /* send signal to UIM */
471 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
472 if (err != 0) {
473 pr_info(" sending SIGUSR2 to uim failed %ld", err);
474 err = -1;
475 continue;
476 }
477#endif
478 /* unblock and send event to UIM via /dev/rfkill */
479 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
480 /* wait for ldisc to be installed */
481 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
482 msecs_to_jiffies(LDISC_TIME));
483 if (!err) { /* timeout */
484 pr_err("line disc installation timed out ");
485 err = -1;
486 continue;
487 } else {
488 /* ldisc installed now */
489 pr_info(" line discipline installed ");
490 err = download_firmware(kim_gdata);
491 if (err != 0) {
492 pr_err("download firmware failed");
493 continue;
494 } else { /* on success don't retry */
495 break;
496 }
497 }
498 } while (retry--);
499 return err;
500}
501
502/**
503 * st_kim_stop - called from ST Core, on the last un-registration
504 * toggle low the chip enable gpio
505 */
506long st_kim_stop(void *kim_data)
507{
508 long err = 0;
509 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
510
511 INIT_COMPLETION(kim_gdata->ldisc_installed);
512#if 0 /* older way of signalling user-space UIM */
513 /* send signal to UIM */
514 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
515 if (err != 0) {
516 pr_err("sending SIGUSR2 to uim failed %ld", err);
517 return -1;
518 }
519#endif
520 /* set BT rfkill to be blocked */
521 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
522
523 /* wait for ldisc to be un-installed */
524 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
525 msecs_to_jiffies(LDISC_TIME));
526 if (!err) { /* timeout */
527 pr_err(" timed out waiting for ldisc to be un-installed");
528 return -1;
529 }
530
531 /* By default configure BT nShutdown to LOW state */
532 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
533 mdelay(1);
534 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
535 mdelay(1);
536 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
537 return err;
538}
539
540/**********************************************************************/
541/* functions called from subsystems */
542/* called when debugfs entry is read from */
543
544static int show_version(struct seq_file *s, void *unused)
545{
546 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
547 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
548 kim_gdata->version.chip, kim_gdata->version.maj_ver,
549 kim_gdata->version.min_ver);
550 return 0;
551}
552
553static int show_list(struct seq_file *s, void *unused)
554{
555 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
556 kim_st_list_protocols(kim_gdata->core_data, s);
557 return 0;
558}
559
560/* function called from rfkill subsystem, when someone from
561 * user space would write 0/1 on the sysfs entry
562 * /sys/class/rfkill/rfkill0,1,3/state
563 */
564static int kim_toggle_radio(void *data, bool blocked)
565{
566 enum proto_type type = *((enum proto_type *)data);
567 pr_debug(" %s: %d ", __func__, type);
568
569 switch (type) {
570 case ST_BT:
571 /* do nothing */
572 break;
573 case ST_FM:
574 case ST_GPS:
575 if (blocked)
576 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
577 else
578 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
579 break;
580 case ST_MAX:
581 pr_err(" wrong proto type ");
582 break;
583 }
584 return 0;
585}
586
587/**
588 * st_kim_ref - reference the core's data
589 * This references the per-ST platform device in the arch/xx/
590 * board-xx.c file.
591 * This would enable multiple such platform devices to exist
592 * on a given platform
593 */
594void st_kim_ref(struct st_data_s **core_data, int id)
595{
596 struct platform_device *pdev;
597 struct kim_data_s *kim_gdata;
598 /* get kim_gdata reference from platform device */
599 pdev = st_get_plat_device(id);
600 kim_gdata = dev_get_drvdata(&pdev->dev);
601 *core_data = kim_gdata->core_data;
602}
603
604static int kim_version_open(struct inode *i, struct file *f)
605{
606 return single_open(f, show_version, i->i_private);
607}
608
609static int kim_list_open(struct inode *i, struct file *f)
610{
611 return single_open(f, show_list, i->i_private);
612}
613
614static const struct file_operations version_debugfs_fops = {
615 /* version info */
616 .open = kim_version_open,
617 .read = seq_read,
618 .llseek = seq_lseek,
619 .release = single_release,
620};
621static const struct file_operations list_debugfs_fops = {
622 /* protocols info */
623 .open = kim_list_open,
624 .read = seq_read,
625 .llseek = seq_lseek,
626 .release = single_release,
627};
628
629/**********************************************************************/
630/* functions called from platform device driver subsystem
631 * need to have a relevant platform device entry in the platform's
632 * board-*.c file
633 */
634
635struct dentry *kim_debugfs_dir;
636static int kim_probe(struct platform_device *pdev)
637{
638 long status;
639 long proto;
640 long *gpios = pdev->dev.platform_data;
641 struct kim_data_s *kim_gdata;
642
643 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
644 /* multiple devices could exist */
645 st_kim_devices[pdev->id] = pdev;
646 } else {
647 /* platform's sure about existance of 1 device */
648 st_kim_devices[0] = pdev;
649 }
650
651 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
652 if (!kim_gdata) {
653 pr_err("no mem to allocate");
654 return -ENOMEM;
655 }
656 dev_set_drvdata(&pdev->dev, kim_gdata);
657
658 status = st_core_init(&kim_gdata->core_data);
659 if (status != 0) {
660 pr_err(" ST core init failed");
661 return -1;
662 }
663 /* refer to itself */
664 kim_gdata->core_data->kim_data = kim_gdata;
665
666 for (proto = 0; proto < ST_MAX; proto++) {
667 kim_gdata->gpios[proto] = gpios[proto];
668 pr_info(" %ld gpio to be requested", gpios[proto]);
669 }
670
671 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
672 /* Claim the Bluetooth/FM/GPIO
673 * nShutdown gpio from the system
674 */
675 status = gpio_request(gpios[proto], "kim");
676 if (unlikely(status)) {
677 pr_err(" gpio %ld request failed ", gpios[proto]);
678 proto -= 1;
679 while (proto >= 0) {
680 if (gpios[proto] != -1)
681 gpio_free(gpios[proto]);
682 }
683 return status;
684 }
685
686 /* Configure nShutdown GPIO as output=0 */
687 status =
688 gpio_direction_output(gpios[proto], 0);
689 if (unlikely(status)) {
690 pr_err(" unable to configure gpio %ld",
691 gpios[proto]);
692 proto -= 1;
693 while (proto >= 0) {
694 if (gpios[proto] != -1)
695 gpio_free(gpios[proto]);
696 }
697 return status;
698 }
699 }
700 /* get reference of pdev for request_firmware
701 */
702 kim_gdata->kim_pdev = pdev;
703 init_completion(&kim_gdata->kim_rcvd);
704 init_completion(&kim_gdata->ldisc_installed);
705
706 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
707 /* TODO: should all types be rfkill_type_bt ? */
708 kim_gdata->rf_protos[proto] = proto;
709 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
710 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
711 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
712 if (kim_gdata->rfkill[proto] == NULL) {
713 pr_err("cannot create rfkill entry for gpio %ld",
714 gpios[proto]);
715 continue;
716 }
717 /* block upon creation */
718 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
719 status = rfkill_register(kim_gdata->rfkill[proto]);
720 if (unlikely(status)) {
721 pr_err("rfkill registration failed for gpio %ld",
722 gpios[proto]);
723 rfkill_unregister(kim_gdata->rfkill[proto]);
724 continue;
725 }
726 pr_info("rfkill entry created for %ld", gpios[proto]);
727 }
728
729 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
730 if (IS_ERR(kim_debugfs_dir)) {
731 pr_err(" debugfs entries creation failed ");
732 kim_debugfs_dir = NULL;
733 return -1;
734 }
735
736 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
737 kim_gdata, &version_debugfs_fops);
738 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
739 kim_gdata, &list_debugfs_fops);
740 pr_info(" debugfs entries created ");
741 return 0;
742}
743
744static int kim_remove(struct platform_device *pdev)
745{
746 /* free the GPIOs requested
747 */
748 long *gpios = pdev->dev.platform_data;
749 long proto;
750 struct kim_data_s *kim_gdata;
751
752 kim_gdata = dev_get_drvdata(&pdev->dev);
753
754 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
755 /* Claim the Bluetooth/FM/GPIO
756 * nShutdown gpio from the system
757 */
758 gpio_free(gpios[proto]);
759 rfkill_unregister(kim_gdata->rfkill[proto]);
760 rfkill_destroy(kim_gdata->rfkill[proto]);
761 kim_gdata->rfkill[proto] = NULL;
762 }
763 pr_info("kim: GPIO Freed");
764 debugfs_remove_recursive(kim_debugfs_dir);
765 kim_gdata->kim_pdev = NULL;
766 st_core_exit(kim_gdata->core_data);
767
768 kfree(kim_gdata);
769 kim_gdata = NULL;
770 return 0;
771}
772
773/**********************************************************************/
774/* entry point for ST KIM module, called in from ST Core */
775
776static int __init st_kim_init(void)
777{
778 long ret = 0;
779 ret = platform_driver_register(&kim_platform_driver);
780 if (ret != 0) {
781 pr_err("platform drv registration failed");
782 return -1;
783 }
784 return 0;
785}
786
787static void __exit st_kim_deinit(void)
788{
789 /* the following returns void */
790 platform_driver_unregister(&kim_platform_driver);
791}
792
793
794module_init(st_kim_init);
795module_exit(st_kim_deinit);
796MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
797MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
798MODULE_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..2bda8dea15b0
--- /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_info("%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_info("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_info("wake indication recvd");
125 ll_device_want_to_wakeup(st_data);
126 break;
127 case LL_WAKE_UP_ACK: /* wake ack */
128 pr_info("wake ack rcvd");
129 st_data->ll_state = ST_LL_AWAKE;
130 break;
131 default:
132 pr_err(" unknown input/state ");
133 return -1;
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}