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