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