aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/misc
diff options
context:
space:
mode:
authorGreg Kroah-Hartman <gregkh@suse.de>2010-10-28 12:44:56 -0400
committerGreg Kroah-Hartman <gregkh@suse.de>2010-10-28 12:44:56 -0400
commite4c5bf8e3dca827a1b3a6fac494eae8c74b7e1e7 (patch)
treeea51b391f7d74ca695dcb9f5e46eb02688a92ed9 /drivers/misc
parent81280572ca6f54009edfa4deee563e8678784218 (diff)
parenta4ac0d847af9dd34d5953a5e264400326144b6b2 (diff)
Merge 'staging-next' to Linus's tree
This merges the staging-next tree to Linus's tree and resolves some conflicts that were present due to changes in other trees that were affected by files here. Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/misc')
-rw-r--r--drivers/misc/Kconfig1
-rw-r--r--drivers/misc/Makefile1
-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.c992
-rw-r--r--drivers/misc/ti-st/st_kim.c799
-rw-r--r--drivers/misc/ti-st/st_ll.c150
7 files changed, 1966 insertions, 0 deletions
diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
index 5a74db75f66f..39a2173b4e6c 100644
--- a/drivers/misc/Kconfig
+++ b/drivers/misc/Kconfig
@@ -447,5 +447,6 @@ source "drivers/misc/c2port/Kconfig"
447source "drivers/misc/eeprom/Kconfig" 447source "drivers/misc/eeprom/Kconfig"
448source "drivers/misc/cb710/Kconfig" 448source "drivers/misc/cb710/Kconfig"
449source "drivers/misc/iwmc3200top/Kconfig" 449source "drivers/misc/iwmc3200top/Kconfig"
450source "drivers/misc/ti-st/Kconfig"
450 451
451endif # MISC_DEVICES 452endif # MISC_DEVICES
diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
index 4be5c6fc5ef4..47af4cd08f01 100644
--- a/drivers/misc/Makefile
+++ b/drivers/misc/Makefile
@@ -40,3 +40,4 @@ obj-y += cb710/
40obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o 40obj-$(CONFIG_VMWARE_BALLOON) += vmw_balloon.o
41obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o 41obj-$(CONFIG_ARM_CHARLCD) += arm-charlcd.o
42obj-$(CONFIG_PCH_PHUB) += pch_phub.o 42obj-$(CONFIG_PCH_PHUB) += pch_phub.o
43obj-y += ti-st/
diff --git a/drivers/misc/ti-st/Kconfig b/drivers/misc/ti-st/Kconfig
new file mode 100644
index 000000000000..2c8c3f39710d
--- /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 RFKILL
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..f9aad06d1ae5
--- /dev/null
+++ b/drivers/misc/ti-st/st_core.c
@@ -0,0 +1,992 @@
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/* function pointer pointing to either,
35 * st_kim_recv during registration to receive fw download responses
36 * st_int_recv after registration to receive proto stack responses
37 */
38void (*st_recv) (void*, const unsigned char*, long);
39
40/********************************************************************/
41#if 0
42/* internal misc functions */
43bool is_protocol_list_empty(void)
44{
45 unsigned char i = 0;
46 pr_debug(" %s ", __func__);
47 for (i = 0; i < ST_MAX; i++) {
48 if (st_gdata->list[i] != NULL)
49 return ST_NOTEMPTY;
50 /* not empty */
51 }
52 /* list empty */
53 return ST_EMPTY;
54}
55#endif
56
57/* can be called in from
58 * -- KIM (during fw download)
59 * -- ST Core (during st_write)
60 *
61 * This is the internal write function - a wrapper
62 * to tty->ops->write
63 */
64int st_int_write(struct st_data_s *st_gdata,
65 const unsigned char *data, int count)
66{
67 struct tty_struct *tty;
68 if (unlikely(st_gdata == NULL || st_gdata->tty == NULL)) {
69 pr_err("tty unavailable to perform write");
70 return -1;
71 }
72 tty = st_gdata->tty;
73#ifdef VERBOSE
74 print_hex_dump(KERN_DEBUG, "<out<", DUMP_PREFIX_NONE,
75 16, 1, data, count, 0);
76#endif
77 return tty->ops->write(tty, data, count);
78
79}
80
81/*
82 * push the skb received to relevant
83 * protocol stacks
84 */
85void st_send_frame(enum proto_type protoid, struct st_data_s *st_gdata)
86{
87 pr_info(" %s(prot:%d) ", __func__, protoid);
88
89 if (unlikely
90 (st_gdata == NULL || st_gdata->rx_skb == NULL
91 || st_gdata->list[protoid] == NULL)) {
92 pr_err("protocol %d not registered, no data to send?",
93 protoid);
94 kfree_skb(st_gdata->rx_skb);
95 return;
96 }
97 /* this cannot fail
98 * this shouldn't take long
99 * - should be just skb_queue_tail for the
100 * protocol stack driver
101 */
102 if (likely(st_gdata->list[protoid]->recv != NULL)) {
103 if (unlikely
104 (st_gdata->list[protoid]->recv
105 (st_gdata->list[protoid]->priv_data, st_gdata->rx_skb)
106 != 0)) {
107 pr_err(" proto stack %d's ->recv failed", protoid);
108 kfree_skb(st_gdata->rx_skb);
109 return;
110 }
111 } else {
112 pr_err(" proto stack %d's ->recv null", protoid);
113 kfree_skb(st_gdata->rx_skb);
114 }
115 return;
116}
117
118/**
119 * st_reg_complete -
120 * to call registration complete callbacks
121 * of all protocol stack drivers
122 */
123void st_reg_complete(struct st_data_s *st_gdata, char err)
124{
125 unsigned char i = 0;
126 pr_info(" %s ", __func__);
127 for (i = 0; i < ST_MAX; i++) {
128 if (likely(st_gdata != NULL && st_gdata->list[i] != NULL &&
129 st_gdata->list[i]->reg_complete_cb != NULL))
130 st_gdata->list[i]->reg_complete_cb
131 (st_gdata->list[i]->priv_data, err);
132 }
133}
134
135static inline int st_check_data_len(struct st_data_s *st_gdata,
136 int protoid, int len)
137{
138 int room = skb_tailroom(st_gdata->rx_skb);
139
140 pr_debug("len %d room %d", len, room);
141
142 if (!len) {
143 /* Received packet has only packet header and
144 * has zero length payload. So, ask ST CORE to
145 * forward the packet to protocol driver (BT/FM/GPS)
146 */
147 st_send_frame(protoid, st_gdata);
148
149 } else if (len > room) {
150 /* Received packet's payload length is larger.
151 * We can't accommodate it in created skb.
152 */
153 pr_err("Data length is too large len %d room %d", len,
154 room);
155 kfree_skb(st_gdata->rx_skb);
156 } else {
157 /* Packet header has non-zero payload length and
158 * we have enough space in created skb. Lets read
159 * payload data */
160 st_gdata->rx_state = ST_BT_W4_DATA;
161 st_gdata->rx_count = len;
162 return len;
163 }
164
165 /* Change ST state to continue to process next
166 * packet */
167 st_gdata->rx_state = ST_W4_PACKET_TYPE;
168 st_gdata->rx_skb = NULL;
169 st_gdata->rx_count = 0;
170
171 return 0;
172}
173
174/**
175 * st_wakeup_ack - internal function for action when wake-up ack
176 * received
177 */
178static inline void st_wakeup_ack(struct st_data_s *st_gdata,
179 unsigned char cmd)
180{
181 struct sk_buff *waiting_skb;
182 unsigned long flags = 0;
183
184 spin_lock_irqsave(&st_gdata->lock, flags);
185 /* de-Q from waitQ and Q in txQ now that the
186 * chip is awake
187 */
188 while ((waiting_skb = skb_dequeue(&st_gdata->tx_waitq)))
189 skb_queue_tail(&st_gdata->txq, waiting_skb);
190
191 /* state forwarded to ST LL */
192 st_ll_sleep_state(st_gdata, (unsigned long)cmd);
193 spin_unlock_irqrestore(&st_gdata->lock, flags);
194
195 /* wake up to send the recently copied skbs from waitQ */
196 st_tx_wakeup(st_gdata);
197}
198
199/**
200 * st_int_recv - ST's internal receive function.
201 * Decodes received RAW data and forwards to corresponding
202 * client drivers (Bluetooth,FM,GPS..etc).
203 * This can receive various types of packets,
204 * HCI-Events, ACL, SCO, 4 types of HCI-LL PM packets
205 * CH-8 packets from FM, CH-9 packets from GPS cores.
206 */
207void st_int_recv(void *disc_data,
208 const unsigned char *data, long count)
209{
210 char *ptr;
211 struct hci_event_hdr *eh;
212 struct hci_acl_hdr *ah;
213 struct hci_sco_hdr *sh;
214 struct fm_event_hdr *fm;
215 struct gps_event_hdr *gps;
216 int len = 0, type = 0, dlen = 0;
217 static enum proto_type protoid = ST_MAX;
218 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
219
220 ptr = (char *)data;
221 /* tty_receive sent null ? */
222 if (unlikely(ptr == NULL) || (st_gdata == NULL)) {
223 pr_err(" received null from TTY ");
224 return;
225 }
226
227 pr_info("count %ld rx_state %ld"
228 "rx_count %ld", count, st_gdata->rx_state,
229 st_gdata->rx_count);
230
231 /* Decode received bytes here */
232 while (count) {
233 if (st_gdata->rx_count) {
234 len = min_t(unsigned int, st_gdata->rx_count, count);
235 memcpy(skb_put(st_gdata->rx_skb, len), ptr, len);
236 st_gdata->rx_count -= len;
237 count -= len;
238 ptr += len;
239
240 if (st_gdata->rx_count)
241 continue;
242
243 /* Check ST RX state machine , where are we? */
244 switch (st_gdata->rx_state) {
245
246 /* Waiting for complete packet ? */
247 case ST_BT_W4_DATA:
248 pr_debug("Complete pkt received");
249
250 /* Ask ST CORE to forward
251 * the packet to protocol driver */
252 st_send_frame(protoid, st_gdata);
253
254 st_gdata->rx_state = ST_W4_PACKET_TYPE;
255 st_gdata->rx_skb = NULL;
256 protoid = ST_MAX; /* is this required ? */
257 continue;
258
259 /* Waiting for Bluetooth event header ? */
260 case ST_BT_W4_EVENT_HDR:
261 eh = (struct hci_event_hdr *)st_gdata->rx_skb->
262 data;
263
264 pr_debug("Event header: evt 0x%2.2x"
265 "plen %d", eh->evt, eh->plen);
266
267 st_check_data_len(st_gdata, protoid, eh->plen);
268 continue;
269
270 /* Waiting for Bluetooth acl header ? */
271 case ST_BT_W4_ACL_HDR:
272 ah = (struct hci_acl_hdr *)st_gdata->rx_skb->
273 data;
274 dlen = __le16_to_cpu(ah->dlen);
275
276 pr_info("ACL header: dlen %d", dlen);
277
278 st_check_data_len(st_gdata, protoid, dlen);
279 continue;
280
281 /* Waiting for Bluetooth sco header ? */
282 case ST_BT_W4_SCO_HDR:
283 sh = (struct hci_sco_hdr *)st_gdata->rx_skb->
284 data;
285
286 pr_info("SCO header: dlen %d", sh->dlen);
287
288 st_check_data_len(st_gdata, protoid, sh->dlen);
289 continue;
290 case ST_FM_W4_EVENT_HDR:
291 fm = (struct fm_event_hdr *)st_gdata->rx_skb->
292 data;
293 pr_info("FM Header: ");
294 st_check_data_len(st_gdata, ST_FM, fm->plen);
295 continue;
296 /* TODO : Add GPS packet machine logic here */
297 case ST_GPS_W4_EVENT_HDR:
298 /* [0x09 pkt hdr][R/W byte][2 byte len] */
299 gps = (struct gps_event_hdr *)st_gdata->rx_skb->
300 data;
301 pr_info("GPS Header: ");
302 st_check_data_len(st_gdata, ST_GPS, gps->plen);
303 continue;
304 } /* end of switch rx_state */
305 }
306
307 /* end of if rx_count */
308 /* Check first byte of packet and identify module
309 * owner (BT/FM/GPS) */
310 switch (*ptr) {
311
312 /* Bluetooth event packet? */
313 case HCI_EVENT_PKT:
314 pr_info("Event packet");
315 st_gdata->rx_state = ST_BT_W4_EVENT_HDR;
316 st_gdata->rx_count = HCI_EVENT_HDR_SIZE;
317 type = HCI_EVENT_PKT;
318 protoid = ST_BT;
319 break;
320
321 /* Bluetooth acl packet? */
322 case HCI_ACLDATA_PKT:
323 pr_info("ACL packet");
324 st_gdata->rx_state = ST_BT_W4_ACL_HDR;
325 st_gdata->rx_count = HCI_ACL_HDR_SIZE;
326 type = HCI_ACLDATA_PKT;
327 protoid = ST_BT;
328 break;
329
330 /* Bluetooth sco packet? */
331 case HCI_SCODATA_PKT:
332 pr_info("SCO packet");
333 st_gdata->rx_state = ST_BT_W4_SCO_HDR;
334 st_gdata->rx_count = HCI_SCO_HDR_SIZE;
335 type = HCI_SCODATA_PKT;
336 protoid = ST_BT;
337 break;
338
339 /* Channel 8(FM) packet? */
340 case ST_FM_CH8_PKT:
341 pr_info("FM CH8 packet");
342 type = ST_FM_CH8_PKT;
343 st_gdata->rx_state = ST_FM_W4_EVENT_HDR;
344 st_gdata->rx_count = FM_EVENT_HDR_SIZE;
345 protoid = ST_FM;
346 break;
347
348 /* Channel 9(GPS) packet? */
349 case 0x9: /*ST_LL_GPS_CH9_PKT */
350 pr_info("GPS CH9 packet");
351 type = 0x9; /* ST_LL_GPS_CH9_PKT; */
352 protoid = ST_GPS;
353 st_gdata->rx_state = ST_GPS_W4_EVENT_HDR;
354 st_gdata->rx_count = 3; /* GPS_EVENT_HDR_SIZE -1*/
355 break;
356 case LL_SLEEP_IND:
357 case LL_SLEEP_ACK:
358 case LL_WAKE_UP_IND:
359 pr_info("PM packet");
360 /* this takes appropriate action based on
361 * sleep state received --
362 */
363 st_ll_sleep_state(st_gdata, *ptr);
364 ptr++;
365 count--;
366 continue;
367 case LL_WAKE_UP_ACK:
368 pr_info("PM packet");
369 /* wake up ack received */
370 st_wakeup_ack(st_gdata, *ptr);
371 ptr++;
372 count--;
373 continue;
374 /* Unknow packet? */
375 default:
376 pr_err("Unknown packet type %2.2x", (__u8) *ptr);
377 ptr++;
378 count--;
379 continue;
380 };
381 ptr++;
382 count--;
383
384 switch (protoid) {
385 case ST_BT:
386 /* Allocate new packet to hold received data */
387 st_gdata->rx_skb =
388 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
389 if (!st_gdata->rx_skb) {
390 pr_err("Can't allocate mem for new packet");
391 st_gdata->rx_state = ST_W4_PACKET_TYPE;
392 st_gdata->rx_count = 0;
393 return;
394 }
395 bt_cb(st_gdata->rx_skb)->pkt_type = type;
396 break;
397 case ST_FM: /* for FM */
398 st_gdata->rx_skb =
399 alloc_skb(FM_MAX_FRAME_SIZE, GFP_ATOMIC);
400 if (!st_gdata->rx_skb) {
401 pr_err("Can't allocate mem for new packet");
402 st_gdata->rx_state = ST_W4_PACKET_TYPE;
403 st_gdata->rx_count = 0;
404 return;
405 }
406 /* place holder 0x08 */
407 skb_reserve(st_gdata->rx_skb, 1);
408 st_gdata->rx_skb->cb[0] = ST_FM_CH8_PKT;
409 break;
410 case ST_GPS:
411 /* for GPS */
412 st_gdata->rx_skb =
413 alloc_skb(100 /*GPS_MAX_FRAME_SIZE */ , GFP_ATOMIC);
414 if (!st_gdata->rx_skb) {
415 pr_err("Can't allocate mem for new packet");
416 st_gdata->rx_state = ST_W4_PACKET_TYPE;
417 st_gdata->rx_count = 0;
418 return;
419 }
420 /* place holder 0x09 */
421 skb_reserve(st_gdata->rx_skb, 1);
422 st_gdata->rx_skb->cb[0] = 0x09; /*ST_GPS_CH9_PKT; */
423 break;
424 case ST_MAX:
425 break;
426 }
427 }
428 pr_debug("done %s", __func__);
429 return;
430}
431
432/**
433 * st_int_dequeue - internal de-Q function.
434 * If the previous data set was not written
435 * completely, return that skb which has the pending data.
436 * In normal cases, return top of txq.
437 */
438struct sk_buff *st_int_dequeue(struct st_data_s *st_gdata)
439{
440 struct sk_buff *returning_skb;
441
442 pr_debug("%s", __func__);
443 if (st_gdata->tx_skb != NULL) {
444 returning_skb = st_gdata->tx_skb;
445 st_gdata->tx_skb = NULL;
446 return returning_skb;
447 }
448 return skb_dequeue(&st_gdata->txq);
449}
450
451/**
452 * st_int_enqueue - internal Q-ing function.
453 * Will either Q the skb to txq or the tx_waitq
454 * depending on the ST LL state.
455 * If the chip is asleep, then Q it onto waitq and
456 * wakeup the chip.
457 * txq and waitq needs protection since the other contexts
458 * may be sending data, waking up chip.
459 */
460void st_int_enqueue(struct st_data_s *st_gdata, struct sk_buff *skb)
461{
462 unsigned long flags = 0;
463
464 pr_debug("%s", __func__);
465 spin_lock_irqsave(&st_gdata->lock, flags);
466
467 switch (st_ll_getstate(st_gdata)) {
468 case ST_LL_AWAKE:
469 pr_info("ST LL is AWAKE, sending normally");
470 skb_queue_tail(&st_gdata->txq, skb);
471 break;
472 case ST_LL_ASLEEP_TO_AWAKE:
473 skb_queue_tail(&st_gdata->tx_waitq, skb);
474 break;
475 case ST_LL_AWAKE_TO_ASLEEP:
476 pr_err("ST LL is illegal state(%ld),"
477 "purging received skb.", st_ll_getstate(st_gdata));
478 kfree_skb(skb);
479 break;
480 case ST_LL_ASLEEP:
481 skb_queue_tail(&st_gdata->tx_waitq, skb);
482 st_ll_wakeup(st_gdata);
483 break;
484 default:
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 }
490
491 spin_unlock_irqrestore(&st_gdata->lock, flags);
492 pr_debug("done %s", __func__);
493 return;
494}
495
496/*
497 * internal wakeup function
498 * called from either
499 * - TTY layer when write's finished
500 * - st_write (in context of the protocol stack)
501 */
502void st_tx_wakeup(struct st_data_s *st_data)
503{
504 struct sk_buff *skb;
505 unsigned long flags; /* for irq save flags */
506 pr_debug("%s", __func__);
507 /* check for sending & set flag sending here */
508 if (test_and_set_bit(ST_TX_SENDING, &st_data->tx_state)) {
509 pr_info("ST already sending");
510 /* keep sending */
511 set_bit(ST_TX_WAKEUP, &st_data->tx_state);
512 return;
513 /* TX_WAKEUP will be checked in another
514 * context
515 */
516 }
517 do { /* come back if st_tx_wakeup is set */
518 /* woke-up to write */
519 clear_bit(ST_TX_WAKEUP, &st_data->tx_state);
520 while ((skb = st_int_dequeue(st_data))) {
521 int len;
522 spin_lock_irqsave(&st_data->lock, flags);
523 /* enable wake-up from TTY */
524 set_bit(TTY_DO_WRITE_WAKEUP, &st_data->tty->flags);
525 len = st_int_write(st_data, skb->data, skb->len);
526 skb_pull(skb, len);
527 /* if skb->len = len as expected, skb->len=0 */
528 if (skb->len) {
529 /* would be the next skb to be sent */
530 st_data->tx_skb = skb;
531 spin_unlock_irqrestore(&st_data->lock, flags);
532 break;
533 }
534 kfree_skb(skb);
535 spin_unlock_irqrestore(&st_data->lock, flags);
536 }
537 /* if wake-up is set in another context- restart sending */
538 } while (test_bit(ST_TX_WAKEUP, &st_data->tx_state));
539
540 /* clear flag sending */
541 clear_bit(ST_TX_SENDING, &st_data->tx_state);
542}
543
544/********************************************************************/
545/* functions called from ST KIM
546*/
547void kim_st_list_protocols(struct st_data_s *st_gdata, void *buf)
548{
549 seq_printf(buf, "[%d]\nBT=%c\nFM=%c\nGPS=%c\n",
550 st_gdata->protos_registered,
551 st_gdata->list[ST_BT] != NULL ? 'R' : 'U',
552 st_gdata->list[ST_FM] != NULL ? 'R' : 'U',
553 st_gdata->list[ST_GPS] != NULL ? 'R' : 'U');
554}
555
556/********************************************************************/
557/*
558 * functions called from protocol stack drivers
559 * to be EXPORT-ed
560 */
561long st_register(struct st_proto_s *new_proto)
562{
563 struct st_data_s *st_gdata;
564 long err = 0;
565 unsigned long flags = 0;
566
567 st_kim_ref(&st_gdata, 0);
568 pr_info("%s(%d) ", __func__, new_proto->type);
569 if (st_gdata == NULL || new_proto == NULL || new_proto->recv == NULL
570 || new_proto->reg_complete_cb == NULL) {
571 pr_err("gdata/new_proto/recv or reg_complete_cb not ready");
572 return -1;
573 }
574
575 if (new_proto->type < ST_BT || new_proto->type >= ST_MAX) {
576 pr_err("protocol %d not supported", new_proto->type);
577 return -EPROTONOSUPPORT;
578 }
579
580 if (st_gdata->list[new_proto->type] != NULL) {
581 pr_err("protocol %d already registered", new_proto->type);
582 return -EALREADY;
583 }
584
585 /* can be from process context only */
586 spin_lock_irqsave(&st_gdata->lock, flags);
587
588 if (test_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state)) {
589 pr_info(" ST_REG_IN_PROGRESS:%d ", new_proto->type);
590 /* fw download in progress */
591 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
592
593 st_gdata->list[new_proto->type] = new_proto;
594 st_gdata->protos_registered++;
595 new_proto->write = st_write;
596
597 set_bit(ST_REG_PENDING, &st_gdata->st_state);
598 spin_unlock_irqrestore(&st_gdata->lock, flags);
599 return -EINPROGRESS;
600 } else if (st_gdata->protos_registered == ST_EMPTY) {
601 pr_info(" protocol list empty :%d ", new_proto->type);
602 set_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
603 st_recv = st_kim_recv;
604
605 /* release lock previously held - re-locked below */
606 spin_unlock_irqrestore(&st_gdata->lock, flags);
607
608 /* enable the ST LL - to set default chip state */
609 st_ll_enable(st_gdata);
610 /* this may take a while to complete
611 * since it involves BT fw download
612 */
613 err = st_kim_start(st_gdata->kim_data);
614 if (err != 0) {
615 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
616 if ((st_gdata->protos_registered != ST_EMPTY) &&
617 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
618 pr_err(" KIM failure complete callback ");
619 st_reg_complete(st_gdata, -1);
620 }
621
622 return -1;
623 }
624
625 /* the protocol might require other gpios to be toggled
626 */
627 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
628
629 clear_bit(ST_REG_IN_PROGRESS, &st_gdata->st_state);
630 st_recv = st_int_recv;
631
632 /* this is where all pending registration
633 * are signalled to be complete by calling callback functions
634 */
635 if ((st_gdata->protos_registered != ST_EMPTY) &&
636 (test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
637 pr_debug(" call reg complete callback ");
638 st_reg_complete(st_gdata, 0);
639 }
640 clear_bit(ST_REG_PENDING, &st_gdata->st_state);
641
642 /* check for already registered once more,
643 * since the above check is old
644 */
645 if (st_gdata->list[new_proto->type] != NULL) {
646 pr_err(" proto %d already registered ",
647 new_proto->type);
648 return -EALREADY;
649 }
650
651 spin_lock_irqsave(&st_gdata->lock, flags);
652 st_gdata->list[new_proto->type] = new_proto;
653 st_gdata->protos_registered++;
654 new_proto->write = st_write;
655 spin_unlock_irqrestore(&st_gdata->lock, flags);
656 return err;
657 }
658 /* if fw is already downloaded & new stack registers protocol */
659 else {
660 switch (new_proto->type) {
661 case ST_BT:
662 /* do nothing */
663 break;
664 case ST_FM:
665 case ST_GPS:
666 st_kim_chip_toggle(new_proto->type, KIM_GPIO_ACTIVE);
667 break;
668 case ST_MAX:
669 default:
670 pr_err("%d protocol not supported",
671 new_proto->type);
672 spin_unlock_irqrestore(&st_gdata->lock, flags);
673 return -EPROTONOSUPPORT;
674 }
675 st_gdata->list[new_proto->type] = new_proto;
676 st_gdata->protos_registered++;
677 new_proto->write = st_write;
678
679 /* lock already held before entering else */
680 spin_unlock_irqrestore(&st_gdata->lock, flags);
681 return err;
682 }
683 pr_debug("done %s(%d) ", __func__, new_proto->type);
684}
685EXPORT_SYMBOL_GPL(st_register);
686
687/* to unregister a protocol -
688 * to be called from protocol stack driver
689 */
690long st_unregister(enum proto_type type)
691{
692 long err = 0;
693 unsigned long flags = 0;
694 struct st_data_s *st_gdata;
695
696 pr_debug("%s: %d ", __func__, type);
697
698 st_kim_ref(&st_gdata, 0);
699 if (type < ST_BT || type >= ST_MAX) {
700 pr_err(" protocol %d not supported", type);
701 return -EPROTONOSUPPORT;
702 }
703
704 spin_lock_irqsave(&st_gdata->lock, flags);
705
706 if (st_gdata->list[type] == NULL) {
707 pr_err(" protocol %d not registered", type);
708 spin_unlock_irqrestore(&st_gdata->lock, flags);
709 return -EPROTONOSUPPORT;
710 }
711
712 st_gdata->protos_registered--;
713 st_gdata->list[type] = NULL;
714
715 /* kim ignores BT in the below function
716 * and handles the rest, BT is toggled
717 * only in kim_start and kim_stop
718 */
719 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
720 spin_unlock_irqrestore(&st_gdata->lock, flags);
721
722 if ((st_gdata->protos_registered == ST_EMPTY) &&
723 (!test_bit(ST_REG_PENDING, &st_gdata->st_state))) {
724 pr_info(" all protocols unregistered ");
725
726 /* stop traffic on tty */
727 if (st_gdata->tty) {
728 tty_ldisc_flush(st_gdata->tty);
729 stop_tty(st_gdata->tty);
730 }
731
732 /* all protocols now unregistered */
733 st_kim_stop(st_gdata->kim_data);
734 /* disable ST LL */
735 st_ll_disable(st_gdata);
736 }
737 return err;
738}
739
740/*
741 * called in protocol stack drivers
742 * via the write function pointer
743 */
744long st_write(struct sk_buff *skb)
745{
746 struct st_data_s *st_gdata;
747#ifdef DEBUG
748 enum proto_type protoid = ST_MAX;
749#endif
750 long len;
751
752 st_kim_ref(&st_gdata, 0);
753 if (unlikely(skb == NULL || st_gdata == NULL
754 || st_gdata->tty == NULL)) {
755 pr_err("data/tty unavailable to perform write");
756 return -1;
757 }
758#ifdef DEBUG /* open-up skb to read the 1st byte */
759 switch (skb->data[0]) {
760 case HCI_COMMAND_PKT:
761 case HCI_ACLDATA_PKT:
762 case HCI_SCODATA_PKT:
763 protoid = ST_BT;
764 break;
765 case ST_FM_CH8_PKT:
766 protoid = ST_FM;
767 break;
768 case 0x09:
769 protoid = ST_GPS;
770 break;
771 }
772 if (unlikely(st_gdata->list[protoid] == NULL)) {
773 pr_err(" protocol %d not registered, and writing? ",
774 protoid);
775 return -1;
776 }
777#endif
778 pr_debug("%d to be written", skb->len);
779 len = skb->len;
780
781 /* st_ll to decide where to enqueue the skb */
782 st_int_enqueue(st_gdata, skb);
783 /* wake up */
784 st_tx_wakeup(st_gdata);
785
786 /* return number of bytes written */
787 return len;
788}
789
790/* for protocols making use of shared transport */
791EXPORT_SYMBOL_GPL(st_unregister);
792
793/********************************************************************/
794/*
795 * functions called from TTY layer
796 */
797static int st_tty_open(struct tty_struct *tty)
798{
799 int err = 0;
800 struct st_data_s *st_gdata;
801 pr_info("%s ", __func__);
802
803 st_kim_ref(&st_gdata, 0);
804 st_gdata->tty = tty;
805 tty->disc_data = st_gdata;
806
807 /* don't do an wakeup for now */
808 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
809
810 /* mem already allocated
811 */
812 tty->receive_room = 65536;
813 /* Flush any pending characters in the driver and discipline. */
814 tty_ldisc_flush(tty);
815 tty_driver_flush_buffer(tty);
816 /*
817 * signal to UIM via KIM that -
818 * installation of N_TI_WL ldisc is complete
819 */
820 st_kim_complete(st_gdata->kim_data);
821 pr_debug("done %s", __func__);
822 return err;
823}
824
825static void st_tty_close(struct tty_struct *tty)
826{
827 unsigned char i = ST_MAX;
828 unsigned long flags = 0;
829 struct st_data_s *st_gdata = tty->disc_data;
830
831 pr_info("%s ", __func__);
832
833 /* TODO:
834 * if a protocol has been registered & line discipline
835 * un-installed for some reason - what should be done ?
836 */
837 spin_lock_irqsave(&st_gdata->lock, flags);
838 for (i = ST_BT; i < ST_MAX; i++) {
839 if (st_gdata->list[i] != NULL)
840 pr_err("%d not un-registered", i);
841 st_gdata->list[i] = NULL;
842 }
843 st_gdata->protos_registered = 0;
844 spin_unlock_irqrestore(&st_gdata->lock, flags);
845 /*
846 * signal to UIM via KIM that -
847 * N_TI_WL ldisc is un-installed
848 */
849 st_kim_complete(st_gdata->kim_data);
850 st_gdata->tty = NULL;
851 /* Flush any pending characters in the driver and discipline. */
852 tty_ldisc_flush(tty);
853 tty_driver_flush_buffer(tty);
854
855 spin_lock_irqsave(&st_gdata->lock, flags);
856 /* empty out txq and tx_waitq */
857 skb_queue_purge(&st_gdata->txq);
858 skb_queue_purge(&st_gdata->tx_waitq);
859 /* reset the TTY Rx states of ST */
860 st_gdata->rx_count = 0;
861 st_gdata->rx_state = ST_W4_PACKET_TYPE;
862 kfree_skb(st_gdata->rx_skb);
863 st_gdata->rx_skb = NULL;
864 spin_unlock_irqrestore(&st_gdata->lock, flags);
865
866 pr_debug("%s: done ", __func__);
867}
868
869static void st_tty_receive(struct tty_struct *tty, const unsigned char *data,
870 char *tty_flags, int count)
871{
872
873#ifdef VERBOSE
874 print_hex_dump(KERN_DEBUG, ">in>", DUMP_PREFIX_NONE,
875 16, 1, data, count, 0);
876#endif
877
878 /*
879 * if fw download is in progress then route incoming data
880 * to KIM for validation
881 */
882 st_recv(tty->disc_data, data, count);
883 pr_debug("done %s", __func__);
884}
885
886/* wake-up function called in from the TTY layer
887 * inside the internal wakeup function will be called
888 */
889static void st_tty_wakeup(struct tty_struct *tty)
890{
891 struct st_data_s *st_gdata = tty->disc_data;
892 pr_debug("%s ", __func__);
893 /* don't do an wakeup for now */
894 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
895
896 /* call our internal wakeup */
897 st_tx_wakeup((void *)st_gdata);
898}
899
900static void st_tty_flush_buffer(struct tty_struct *tty)
901{
902 struct st_data_s *st_gdata = tty->disc_data;
903 pr_debug("%s ", __func__);
904
905 kfree_skb(st_gdata->tx_skb);
906 st_gdata->tx_skb = NULL;
907
908 tty->ops->flush_buffer(tty);
909 return;
910}
911
912static struct tty_ldisc_ops st_ldisc_ops = {
913 .magic = TTY_LDISC_MAGIC,
914 .name = "n_st",
915 .open = st_tty_open,
916 .close = st_tty_close,
917 .receive_buf = st_tty_receive,
918 .write_wakeup = st_tty_wakeup,
919 .flush_buffer = st_tty_flush_buffer,
920 .owner = THIS_MODULE
921};
922
923/********************************************************************/
924int st_core_init(struct st_data_s **core_data)
925{
926 struct st_data_s *st_gdata;
927 long err;
928
929 err = tty_register_ldisc(N_TI_WL, &st_ldisc_ops);
930 if (err) {
931 pr_err("error registering %d line discipline %ld",
932 N_TI_WL, err);
933 return err;
934 }
935 pr_debug("registered n_shared line discipline");
936
937 st_gdata = kzalloc(sizeof(struct st_data_s), GFP_KERNEL);
938 if (!st_gdata) {
939 pr_err("memory allocation failed");
940 err = tty_unregister_ldisc(N_TI_WL);
941 if (err)
942 pr_err("unable to un-register ldisc %ld", err);
943 err = -ENOMEM;
944 return err;
945 }
946
947 /* Initialize ST TxQ and Tx waitQ queue head. All BT/FM/GPS module skb's
948 * will be pushed in this queue for actual transmission.
949 */
950 skb_queue_head_init(&st_gdata->txq);
951 skb_queue_head_init(&st_gdata->tx_waitq);
952
953 /* Locking used in st_int_enqueue() to avoid multiple execution */
954 spin_lock_init(&st_gdata->lock);
955
956 err = st_ll_init(st_gdata);
957 if (err) {
958 pr_err("error during st_ll initialization(%ld)", err);
959 kfree(st_gdata);
960 err = tty_unregister_ldisc(N_TI_WL);
961 if (err)
962 pr_err("unable to un-register ldisc");
963 return -1;
964 }
965 *core_data = st_gdata;
966 return 0;
967}
968
969void st_core_exit(struct st_data_s *st_gdata)
970{
971 long err;
972 /* internal module cleanup */
973 err = st_ll_deinit(st_gdata);
974 if (err)
975 pr_err("error during deinit of ST LL %ld", err);
976
977 if (st_gdata != NULL) {
978 /* Free ST Tx Qs and skbs */
979 skb_queue_purge(&st_gdata->txq);
980 skb_queue_purge(&st_gdata->tx_waitq);
981 kfree_skb(st_gdata->rx_skb);
982 kfree_skb(st_gdata->tx_skb);
983 /* TTY ldisc cleanup */
984 err = tty_unregister_ldisc(N_TI_WL);
985 if (err)
986 pr_err("unable to un-register ldisc %ld", err);
987 /* free the global data pointer */
988 kfree(st_gdata);
989 }
990}
991
992
diff --git a/drivers/misc/ti-st/st_kim.c b/drivers/misc/ti-st/st_kim.c
new file mode 100644
index 000000000000..73b6c8b0e869
--- /dev/null
+++ b/drivers/misc/ti-st/st_kim.c
@@ -0,0 +1,799 @@
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 */
78static struct 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 const unsigned char *ptr;
161 struct hci_event_hdr *eh;
162 int len = 0, type = 0;
163
164 pr_debug("%s", __func__);
165 /* Decode received bytes here */
166 ptr = data;
167 if (unlikely(ptr == NULL)) {
168 pr_err(" received null from TTY ");
169 return;
170 }
171
172 while (count) {
173 if (kim_gdata->rx_count) {
174 len = min_t(unsigned int, kim_gdata->rx_count, count);
175 memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
176 kim_gdata->rx_count -= len;
177 count -= len;
178 ptr += len;
179
180 if (kim_gdata->rx_count)
181 continue;
182
183 /* Check ST RX state machine , where are we? */
184 switch (kim_gdata->rx_state) {
185 /* Waiting for complete packet ? */
186 case ST_BT_W4_DATA:
187 pr_debug("Complete pkt received");
188 validate_firmware_response(kim_gdata);
189 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
190 kim_gdata->rx_skb = NULL;
191 continue;
192 /* Waiting for Bluetooth event header ? */
193 case ST_BT_W4_EVENT_HDR:
194 eh = (struct hci_event_hdr *)kim_gdata->
195 rx_skb->data;
196 pr_debug("Event header: evt 0x%2.2x"
197 "plen %d", eh->evt, eh->plen);
198 kim_check_data_len(kim_gdata, eh->plen);
199 continue;
200 } /* end of switch */
201 } /* end of if rx_state */
202 switch (*ptr) {
203 /* Bluetooth event packet? */
204 case HCI_EVENT_PKT:
205 pr_info("Event packet");
206 kim_gdata->rx_state = ST_BT_W4_EVENT_HDR;
207 kim_gdata->rx_count = HCI_EVENT_HDR_SIZE;
208 type = HCI_EVENT_PKT;
209 break;
210 default:
211 pr_info("unknown packet");
212 ptr++;
213 count--;
214 continue;
215 }
216 ptr++;
217 count--;
218 kim_gdata->rx_skb =
219 bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
220 if (!kim_gdata->rx_skb) {
221 pr_err("can't allocate mem for new packet");
222 kim_gdata->rx_state = ST_W4_PACKET_TYPE;
223 kim_gdata->rx_count = 0;
224 return;
225 }
226 bt_cb(kim_gdata->rx_skb)->pkt_type = type;
227 }
228 pr_info("done %s", __func__);
229 return;
230}
231
232static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
233{
234 unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
235 const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
236
237 pr_debug("%s", __func__);
238
239 INIT_COMPLETION(kim_gdata->kim_rcvd);
240 if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
241 pr_err("kim: couldn't write 4 bytes");
242 return -1;
243 }
244
245 if (!wait_for_completion_timeout
246 (&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME))) {
247 pr_err(" waiting for ver info- timed out ");
248 return -1;
249 }
250
251 version =
252 MAKEWORD(kim_gdata->resp_buffer[13],
253 kim_gdata->resp_buffer[14]);
254 chip = (version & 0x7C00) >> 10;
255 min_ver = (version & 0x007F);
256 maj_ver = (version & 0x0380) >> 7;
257
258 if (version & 0x8000)
259 maj_ver |= 0x0008;
260
261 sprintf(bts_scr_name, "TIInit_%d.%d.%d.bts", chip, maj_ver, min_ver);
262
263 /* to be accessed later via sysfs entry */
264 kim_gdata->version.full = version;
265 kim_gdata->version.chip = chip;
266 kim_gdata->version.maj_ver = maj_ver;
267 kim_gdata->version.min_ver = min_ver;
268
269 pr_info("%s", bts_scr_name);
270 return 0;
271}
272
273/**
274 * download_firmware -
275 * internal function which parses through the .bts firmware
276 * script file intreprets SEND, DELAY actions only as of now
277 */
278static long download_firmware(struct kim_data_s *kim_gdata)
279{
280 long err = 0;
281 long len = 0;
282 unsigned char *ptr = NULL;
283 unsigned char *action_ptr = NULL;
284 unsigned char bts_scr_name[30] = { 0 }; /* 30 char long bts scr name? */
285
286 err = read_local_version(kim_gdata, bts_scr_name);
287 if (err != 0) {
288 pr_err("kim: failed to read local ver");
289 return err;
290 }
291 err =
292 request_firmware(&kim_gdata->fw_entry, bts_scr_name,
293 &kim_gdata->kim_pdev->dev);
294 if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
295 (kim_gdata->fw_entry->size == 0))) {
296 pr_err(" request_firmware failed(errno %ld) for %s", err,
297 bts_scr_name);
298 return -1;
299 }
300 ptr = (void *)kim_gdata->fw_entry->data;
301 len = kim_gdata->fw_entry->size;
302 /* bts_header to remove out magic number and
303 * version
304 */
305 ptr += sizeof(struct bts_header);
306 len -= sizeof(struct bts_header);
307
308 while (len > 0 && ptr) {
309 pr_debug(" action size %d, type %d ",
310 ((struct bts_action *)ptr)->size,
311 ((struct bts_action *)ptr)->type);
312
313 switch (((struct bts_action *)ptr)->type) {
314 case ACTION_SEND_COMMAND: /* action send */
315 action_ptr = &(((struct bts_action *)ptr)->data[0]);
316 if (unlikely
317 (((struct hci_command *)action_ptr)->opcode ==
318 0xFF36)) {
319 /* ignore remote change
320 * baud rate HCI VS command */
321 pr_err
322 (" change remote baud"
323 " rate command in firmware");
324 break;
325 }
326
327 INIT_COMPLETION(kim_gdata->kim_rcvd);
328 err = st_int_write(kim_gdata->core_data,
329 ((struct bts_action_send *)action_ptr)->data,
330 ((struct bts_action *)ptr)->size);
331 if (unlikely(err < 0)) {
332 release_firmware(kim_gdata->fw_entry);
333 return -1;
334 }
335 if (!wait_for_completion_timeout
336 (&kim_gdata->kim_rcvd,
337 msecs_to_jiffies(CMD_RESP_TIME))) {
338 pr_err
339 (" response timeout during fw download ");
340 /* timed out */
341 release_firmware(kim_gdata->fw_entry);
342 return -1;
343 }
344 break;
345 case ACTION_DELAY: /* sleep */
346 pr_info("sleep command in scr");
347 action_ptr = &(((struct bts_action *)ptr)->data[0]);
348 mdelay(((struct bts_action_delay *)action_ptr)->msec);
349 break;
350 }
351 len =
352 len - (sizeof(struct bts_action) +
353 ((struct bts_action *)ptr)->size);
354 ptr =
355 ptr + sizeof(struct bts_action) +
356 ((struct bts_action *)ptr)->size;
357 }
358 /* fw download complete */
359 release_firmware(kim_gdata->fw_entry);
360 return 0;
361}
362
363/**********************************************************************/
364/* functions called from ST core */
365/* function to toggle the GPIO
366 * needs to know whether the GPIO is active high or active low
367 */
368void st_kim_chip_toggle(enum proto_type type, enum kim_gpio_state state)
369{
370 struct platform_device *kim_pdev;
371 struct kim_data_s *kim_gdata;
372 pr_info(" %s ", __func__);
373
374 kim_pdev = st_get_plat_device(0);
375 kim_gdata = dev_get_drvdata(&kim_pdev->dev);
376
377 if (kim_gdata->gpios[type] == -1) {
378 pr_info(" gpio not requested for protocol %s",
379 protocol_names[type]);
380 return;
381 }
382 switch (type) {
383 case ST_BT:
384 /*Do Nothing */
385 break;
386
387 case ST_FM:
388 if (state == KIM_GPIO_ACTIVE)
389 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_LOW);
390 else
391 gpio_set_value(kim_gdata->gpios[ST_FM], GPIO_HIGH);
392 break;
393
394 case ST_GPS:
395 if (state == KIM_GPIO_ACTIVE)
396 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_HIGH);
397 else
398 gpio_set_value(kim_gdata->gpios[ST_GPS], GPIO_LOW);
399 break;
400
401 case ST_MAX:
402 default:
403 break;
404 }
405
406 return;
407}
408
409/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
410 * can be because of
411 * 1. response to read local version
412 * 2. during send/recv's of firmware download
413 */
414void st_kim_recv(void *disc_data, const unsigned char *data, long count)
415{
416 struct st_data_s *st_gdata = (struct st_data_s *)disc_data;
417 struct kim_data_s *kim_gdata = st_gdata->kim_data;
418
419 pr_info(" %s ", __func__);
420 /* copy to local buffer */
421 if (unlikely(data[4] == 0x01 && data[5] == 0x10 && data[0] == 0x04)) {
422 /* must be the read_ver_cmd */
423 memcpy(kim_gdata->resp_buffer, data, count);
424 complete_all(&kim_gdata->kim_rcvd);
425 return;
426 } else {
427 kim_int_recv(kim_gdata, data, count);
428 /* either completes or times out */
429 }
430 return;
431}
432
433/* to signal completion of line discipline installation
434 * called from ST Core, upon tty_open
435 */
436void st_kim_complete(void *kim_data)
437{
438 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
439 complete(&kim_gdata->ldisc_installed);
440}
441
442/**
443 * st_kim_start - called from ST Core upon 1st registration
444 * This involves toggling the chip enable gpio, reading
445 * the firmware version from chip, forming the fw file name
446 * based on the chip version, requesting the fw, parsing it
447 * and perform download(send/recv).
448 */
449long st_kim_start(void *kim_data)
450{
451 long err = 0;
452 long retry = POR_RETRY_COUNT;
453 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
454
455 pr_info(" %s", __func__);
456
457 do {
458 /* TODO: this is only because rfkill sub-system
459 * doesn't send events to user-space if the state
460 * isn't changed
461 */
462 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
463 /* Configure BT nShutdown to HIGH state */
464 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
465 mdelay(5); /* FIXME: a proper toggle */
466 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
467 mdelay(100);
468 /* re-initialize the completion */
469 INIT_COMPLETION(kim_gdata->ldisc_installed);
470#if 0 /* older way of signalling user-space UIM */
471 /* send signal to UIM */
472 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 0);
473 if (err != 0) {
474 pr_info(" sending SIGUSR2 to uim failed %ld", err);
475 err = -1;
476 continue;
477 }
478#endif
479 /* unblock and send event to UIM via /dev/rfkill */
480 rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 0);
481 /* wait for ldisc to be installed */
482 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
483 msecs_to_jiffies(LDISC_TIME));
484 if (!err) { /* timeout */
485 pr_err("line disc installation timed out ");
486 err = -1;
487 continue;
488 } else {
489 /* ldisc installed now */
490 pr_info(" line discipline installed ");
491 err = download_firmware(kim_gdata);
492 if (err != 0) {
493 pr_err("download firmware failed");
494 continue;
495 } else { /* on success don't retry */
496 break;
497 }
498 }
499 } while (retry--);
500 return err;
501}
502
503/**
504 * st_kim_stop - called from ST Core, on the last un-registration
505 * toggle low the chip enable gpio
506 */
507long st_kim_stop(void *kim_data)
508{
509 long err = 0;
510 struct kim_data_s *kim_gdata = (struct kim_data_s *)kim_data;
511
512 INIT_COMPLETION(kim_gdata->ldisc_installed);
513#if 0 /* older way of signalling user-space UIM */
514 /* send signal to UIM */
515 err = kill_pid(find_get_pid(kim_gdata->uim_pid), SIGUSR2, 1);
516 if (err != 0) {
517 pr_err("sending SIGUSR2 to uim failed %ld", err);
518 return -1;
519 }
520#endif
521 /* set BT rfkill to be blocked */
522 err = rfkill_set_hw_state(kim_gdata->rfkill[ST_BT], 1);
523
524 /* wait for ldisc to be un-installed */
525 err = wait_for_completion_timeout(&kim_gdata->ldisc_installed,
526 msecs_to_jiffies(LDISC_TIME));
527 if (!err) { /* timeout */
528 pr_err(" timed out waiting for ldisc to be un-installed");
529 return -1;
530 }
531
532 /* By default configure BT nShutdown to LOW state */
533 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
534 mdelay(1);
535 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_HIGH);
536 mdelay(1);
537 gpio_set_value(kim_gdata->gpios[ST_BT], GPIO_LOW);
538 return err;
539}
540
541/**********************************************************************/
542/* functions called from subsystems */
543/* called when debugfs entry is read from */
544
545static int show_version(struct seq_file *s, void *unused)
546{
547 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
548 seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
549 kim_gdata->version.chip, kim_gdata->version.maj_ver,
550 kim_gdata->version.min_ver);
551 return 0;
552}
553
554static int show_list(struct seq_file *s, void *unused)
555{
556 struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
557 kim_st_list_protocols(kim_gdata->core_data, s);
558 return 0;
559}
560
561/* function called from rfkill subsystem, when someone from
562 * user space would write 0/1 on the sysfs entry
563 * /sys/class/rfkill/rfkill0,1,3/state
564 */
565static int kim_toggle_radio(void *data, bool blocked)
566{
567 enum proto_type type = *((enum proto_type *)data);
568 pr_debug(" %s: %d ", __func__, type);
569
570 switch (type) {
571 case ST_BT:
572 /* do nothing */
573 break;
574 case ST_FM:
575 case ST_GPS:
576 if (blocked)
577 st_kim_chip_toggle(type, KIM_GPIO_INACTIVE);
578 else
579 st_kim_chip_toggle(type, KIM_GPIO_ACTIVE);
580 break;
581 case ST_MAX:
582 pr_err(" wrong proto type ");
583 break;
584 }
585 return 0;
586}
587
588/**
589 * st_kim_ref - reference the core's data
590 * This references the per-ST platform device in the arch/xx/
591 * board-xx.c file.
592 * This would enable multiple such platform devices to exist
593 * on a given platform
594 */
595void st_kim_ref(struct st_data_s **core_data, int id)
596{
597 struct platform_device *pdev;
598 struct kim_data_s *kim_gdata;
599 /* get kim_gdata reference from platform device */
600 pdev = st_get_plat_device(id);
601 kim_gdata = dev_get_drvdata(&pdev->dev);
602 *core_data = kim_gdata->core_data;
603}
604
605static int kim_version_open(struct inode *i, struct file *f)
606{
607 return single_open(f, show_version, i->i_private);
608}
609
610static int kim_list_open(struct inode *i, struct file *f)
611{
612 return single_open(f, show_list, i->i_private);
613}
614
615static const struct file_operations version_debugfs_fops = {
616 /* version info */
617 .open = kim_version_open,
618 .read = seq_read,
619 .llseek = seq_lseek,
620 .release = single_release,
621};
622static const struct file_operations list_debugfs_fops = {
623 /* protocols info */
624 .open = kim_list_open,
625 .read = seq_read,
626 .llseek = seq_lseek,
627 .release = single_release,
628};
629
630/**********************************************************************/
631/* functions called from platform device driver subsystem
632 * need to have a relevant platform device entry in the platform's
633 * board-*.c file
634 */
635
636struct dentry *kim_debugfs_dir;
637static int kim_probe(struct platform_device *pdev)
638{
639 long status;
640 long proto;
641 long *gpios = pdev->dev.platform_data;
642 struct kim_data_s *kim_gdata;
643
644 if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
645 /* multiple devices could exist */
646 st_kim_devices[pdev->id] = pdev;
647 } else {
648 /* platform's sure about existance of 1 device */
649 st_kim_devices[0] = pdev;
650 }
651
652 kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
653 if (!kim_gdata) {
654 pr_err("no mem to allocate");
655 return -ENOMEM;
656 }
657 dev_set_drvdata(&pdev->dev, kim_gdata);
658
659 status = st_core_init(&kim_gdata->core_data);
660 if (status != 0) {
661 pr_err(" ST core init failed");
662 return -1;
663 }
664 /* refer to itself */
665 kim_gdata->core_data->kim_data = kim_gdata;
666
667 for (proto = 0; proto < ST_MAX; proto++) {
668 kim_gdata->gpios[proto] = gpios[proto];
669 pr_info(" %ld gpio to be requested", gpios[proto]);
670 }
671
672 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
673 /* Claim the Bluetooth/FM/GPIO
674 * nShutdown gpio from the system
675 */
676 status = gpio_request(gpios[proto], "kim");
677 if (unlikely(status)) {
678 pr_err(" gpio %ld request failed ", gpios[proto]);
679 proto -= 1;
680 while (proto >= 0) {
681 if (gpios[proto] != -1)
682 gpio_free(gpios[proto]);
683 }
684 return status;
685 }
686
687 /* Configure nShutdown GPIO as output=0 */
688 status =
689 gpio_direction_output(gpios[proto], 0);
690 if (unlikely(status)) {
691 pr_err(" unable to configure gpio %ld",
692 gpios[proto]);
693 proto -= 1;
694 while (proto >= 0) {
695 if (gpios[proto] != -1)
696 gpio_free(gpios[proto]);
697 }
698 return status;
699 }
700 }
701 /* get reference of pdev for request_firmware
702 */
703 kim_gdata->kim_pdev = pdev;
704 init_completion(&kim_gdata->kim_rcvd);
705 init_completion(&kim_gdata->ldisc_installed);
706
707 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
708 /* TODO: should all types be rfkill_type_bt ? */
709 kim_gdata->rf_protos[proto] = proto;
710 kim_gdata->rfkill[proto] = rfkill_alloc(protocol_names[proto],
711 &pdev->dev, RFKILL_TYPE_BLUETOOTH,
712 &kim_rfkill_ops, &kim_gdata->rf_protos[proto]);
713 if (kim_gdata->rfkill[proto] == NULL) {
714 pr_err("cannot create rfkill entry for gpio %ld",
715 gpios[proto]);
716 continue;
717 }
718 /* block upon creation */
719 rfkill_init_sw_state(kim_gdata->rfkill[proto], 1);
720 status = rfkill_register(kim_gdata->rfkill[proto]);
721 if (unlikely(status)) {
722 pr_err("rfkill registration failed for gpio %ld",
723 gpios[proto]);
724 rfkill_unregister(kim_gdata->rfkill[proto]);
725 continue;
726 }
727 pr_info("rfkill entry created for %ld", gpios[proto]);
728 }
729
730 kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
731 if (IS_ERR(kim_debugfs_dir)) {
732 pr_err(" debugfs entries creation failed ");
733 kim_debugfs_dir = NULL;
734 return -1;
735 }
736
737 debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
738 kim_gdata, &version_debugfs_fops);
739 debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
740 kim_gdata, &list_debugfs_fops);
741 pr_info(" debugfs entries created ");
742 return 0;
743}
744
745static int kim_remove(struct platform_device *pdev)
746{
747 /* free the GPIOs requested
748 */
749 long *gpios = pdev->dev.platform_data;
750 long proto;
751 struct kim_data_s *kim_gdata;
752
753 kim_gdata = dev_get_drvdata(&pdev->dev);
754
755 for (proto = 0; (proto < ST_MAX) && (gpios[proto] != -1); proto++) {
756 /* Claim the Bluetooth/FM/GPIO
757 * nShutdown gpio from the system
758 */
759 gpio_free(gpios[proto]);
760 rfkill_unregister(kim_gdata->rfkill[proto]);
761 rfkill_destroy(kim_gdata->rfkill[proto]);
762 kim_gdata->rfkill[proto] = NULL;
763 }
764 pr_info("kim: GPIO Freed");
765 debugfs_remove_recursive(kim_debugfs_dir);
766 kim_gdata->kim_pdev = NULL;
767 st_core_exit(kim_gdata->core_data);
768
769 kfree(kim_gdata);
770 kim_gdata = NULL;
771 return 0;
772}
773
774/**********************************************************************/
775/* entry point for ST KIM module, called in from ST Core */
776
777static int __init st_kim_init(void)
778{
779 long ret = 0;
780 ret = platform_driver_register(&kim_platform_driver);
781 if (ret != 0) {
782 pr_err("platform drv registration failed");
783 return -1;
784 }
785 return 0;
786}
787
788static void __exit st_kim_deinit(void)
789{
790 /* the following returns void */
791 platform_driver_unregister(&kim_platform_driver);
792}
793
794
795module_init(st_kim_init);
796module_exit(st_kim_deinit);
797MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
798MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
799MODULE_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}