Linux Audio

Check our new training course

Loading...
v4.6
  1/*
  2 *  Shared Transport Line discipline driver Core
  3 *	Init Manager module responsible for GPIO control
  4 *	and firmware download
  5 *  Copyright (C) 2009-2010 Texas Instruments
  6 *  Author: Pavan Savoy <pavan_savoy@ti.com>
  7 *
  8 *  This program is free software; you can redistribute it and/or modify
  9 *  it under the terms of the GNU General Public License version 2 as
 10 *  published by the Free Software Foundation.
 11 *
 12 *  This program is distributed in the hope that it will be useful,
 13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with this program; if not, write to the Free Software
 19 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20 *
 21 */
 22
 23#define pr_fmt(fmt) "(stk) :" fmt
 24#include <linux/platform_device.h>
 25#include <linux/jiffies.h>
 26#include <linux/firmware.h>
 27#include <linux/delay.h>
 28#include <linux/wait.h>
 29#include <linux/gpio.h>
 30#include <linux/debugfs.h>
 31#include <linux/seq_file.h>
 32#include <linux/sched.h>
 33#include <linux/sysfs.h>
 34#include <linux/tty.h>
 35
 36#include <linux/skbuff.h>
 37#include <linux/ti_wilink_st.h>
 38#include <linux/module.h>
 39
 40#define MAX_ST_DEVICES	3	/* Imagine 1 on each UART for now */
 41static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
 42
 43/**********************************************************************/
 44/* internal functions */
 45
 46/**
 47 * st_get_plat_device -
 48 *	function which returns the reference to the platform device
 49 *	requested by id. As of now only 1 such device exists (id=0)
 50 *	the context requesting for reference can get the id to be
 51 *	requested by a. The protocol driver which is registering or
 52 *	b. the tty device which is opened.
 53 */
 54static struct platform_device *st_get_plat_device(int id)
 55{
 56	return st_kim_devices[id];
 57}
 58
 59/**
 60 * validate_firmware_response -
 61 *	function to return whether the firmware response was proper
 62 *	in case of error don't complete so that waiting for proper
 63 *	response times out
 64 */
 65static void validate_firmware_response(struct kim_data_s *kim_gdata)
 66{
 67	struct sk_buff *skb = kim_gdata->rx_skb;
 68	if (!skb)
 69		return;
 70
 71	/* these magic numbers are the position in the response buffer which
 72	 * allows us to distinguish whether the response is for the read
 73	 * version info. command
 74	 */
 75	if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
 76			skb->data[4] == 0x10 && skb->data[5] == 0x00) {
 77		/* fw version response */
 78		memcpy(kim_gdata->resp_buffer,
 79				kim_gdata->rx_skb->data,
 80				kim_gdata->rx_skb->len);
 81		complete_all(&kim_gdata->kim_rcvd);
 82		kim_gdata->rx_state = ST_W4_PACKET_TYPE;
 83		kim_gdata->rx_skb = NULL;
 84		kim_gdata->rx_count = 0;
 85	} else if (unlikely(skb->data[5] != 0)) {
 86		pr_err("no proper response during fw download");
 87		pr_err("data6 %x", skb->data[5]);
 88		kfree_skb(skb);
 89		return;		/* keep waiting for the proper response */
 90	}
 91	/* becos of all the script being downloaded */
 92	complete_all(&kim_gdata->kim_rcvd);
 93	kfree_skb(skb);
 94}
 95
 96/* check for data len received inside kim_int_recv
 97 * most often hit the last case to update state to waiting for data
 98 */
 99static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
100{
101	register int room = skb_tailroom(kim_gdata->rx_skb);
102
103	pr_debug("len %d room %d", len, room);
104
105	if (!len) {
106		validate_firmware_response(kim_gdata);
107	} else if (len > room) {
108		/* Received packet's payload length is larger.
109		 * We can't accommodate it in created skb.
110		 */
111		pr_err("Data length is too large len %d room %d", len,
112			   room);
113		kfree_skb(kim_gdata->rx_skb);
114	} else {
115		/* Packet header has non-zero payload length and
116		 * we have enough space in created skb. Lets read
117		 * payload data */
118		kim_gdata->rx_state = ST_W4_DATA;
119		kim_gdata->rx_count = len;
120		return len;
121	}
122
123	/* Change ST LL state to continue to process next
124	 * packet */
125	kim_gdata->rx_state = ST_W4_PACKET_TYPE;
126	kim_gdata->rx_skb = NULL;
127	kim_gdata->rx_count = 0;
128
129	return 0;
130}
131
132/**
133 * kim_int_recv - receive function called during firmware download
134 *	firmware download responses on different UART drivers
135 *	have been observed to come in bursts of different
136 *	tty_receive and hence the logic
137 */
138static void kim_int_recv(struct kim_data_s *kim_gdata,
139	const unsigned char *data, long count)
140{
141	const unsigned char *ptr;
142	int len = 0, type = 0;
143	unsigned char *plen;
144
145	pr_debug("%s", __func__);
146	/* Decode received bytes here */
147	ptr = data;
148	if (unlikely(ptr == NULL)) {
149		pr_err(" received null from TTY ");
150		return;
151	}
152
153	while (count) {
154		if (kim_gdata->rx_count) {
155			len = min_t(unsigned int, kim_gdata->rx_count, count);
156			memcpy(skb_put(kim_gdata->rx_skb, len), ptr, len);
157			kim_gdata->rx_count -= len;
158			count -= len;
159			ptr += len;
160
161			if (kim_gdata->rx_count)
162				continue;
163
164			/* Check ST RX state machine , where are we? */
165			switch (kim_gdata->rx_state) {
166				/* Waiting for complete packet ? */
167			case ST_W4_DATA:
168				pr_debug("Complete pkt received");
169				validate_firmware_response(kim_gdata);
170				kim_gdata->rx_state = ST_W4_PACKET_TYPE;
171				kim_gdata->rx_skb = NULL;
172				continue;
173				/* Waiting for Bluetooth event header ? */
174			case ST_W4_HEADER:
175				plen =
176				(unsigned char *)&kim_gdata->rx_skb->data[1];
177				pr_debug("event hdr: plen 0x%02x\n", *plen);
178				kim_check_data_len(kim_gdata, *plen);
179				continue;
180			}	/* end of switch */
181		}		/* end of if rx_state */
182		switch (*ptr) {
183			/* Bluetooth event packet? */
184		case 0x04:
185			kim_gdata->rx_state = ST_W4_HEADER;
186			kim_gdata->rx_count = 2;
187			type = *ptr;
188			break;
189		default:
190			pr_info("unknown packet");
191			ptr++;
192			count--;
193			continue;
194		}
195		ptr++;
196		count--;
197		kim_gdata->rx_skb =
198			alloc_skb(1024+8, GFP_ATOMIC);
199		if (!kim_gdata->rx_skb) {
200			pr_err("can't allocate mem for new packet");
201			kim_gdata->rx_state = ST_W4_PACKET_TYPE;
202			kim_gdata->rx_count = 0;
203			return;
204		}
205		skb_reserve(kim_gdata->rx_skb, 8);
206		kim_gdata->rx_skb->cb[0] = 4;
207		kim_gdata->rx_skb->cb[1] = 0;
208
209	}
210	return;
211}
212
213static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
214{
215	unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
216	const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
217	long timeout;
218
219	pr_debug("%s", __func__);
220
221	reinit_completion(&kim_gdata->kim_rcvd);
222	if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
223		pr_err("kim: couldn't write 4 bytes");
224		return -EIO;
225	}
226
227	timeout = wait_for_completion_interruptible_timeout(
228		&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME));
229	if (timeout <= 0) {
230		pr_err(" waiting for ver info- timed out or received signal");
231		return timeout ? -ERESTARTSYS : -ETIMEDOUT;
232	}
233	reinit_completion(&kim_gdata->kim_rcvd);
234	/* the positions 12 & 13 in the response buffer provide with the
235	 * chip, major & minor numbers
236	 */
237
238	version =
239		MAKEWORD(kim_gdata->resp_buffer[12],
240				kim_gdata->resp_buffer[13]);
241	chip = (version & 0x7C00) >> 10;
242	min_ver = (version & 0x007F);
243	maj_ver = (version & 0x0380) >> 7;
244
245	if (version & 0x8000)
246		maj_ver |= 0x0008;
247
248	sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
249		chip, maj_ver, min_ver);
250
251	/* to be accessed later via sysfs entry */
252	kim_gdata->version.full = version;
253	kim_gdata->version.chip = chip;
254	kim_gdata->version.maj_ver = maj_ver;
255	kim_gdata->version.min_ver = min_ver;
256
257	pr_info("%s", bts_scr_name);
258	return 0;
259}
260
261static void skip_change_remote_baud(unsigned char **ptr, long *len)
262{
263	unsigned char *nxt_action, *cur_action;
264	cur_action = *ptr;
265
266	nxt_action = cur_action + sizeof(struct bts_action) +
267		((struct bts_action *) cur_action)->size;
268
269	if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
270		pr_err("invalid action after change remote baud command");
271	} else {
272		*ptr = *ptr + sizeof(struct bts_action) +
273			((struct bts_action *)cur_action)->size;
274		*len = *len - (sizeof(struct bts_action) +
275				((struct bts_action *)cur_action)->size);
276		/* warn user on not commenting these in firmware */
277		pr_warn("skipping the wait event of change remote baud");
278	}
279}
280
281/**
282 * download_firmware -
283 *	internal function which parses through the .bts firmware
284 *	script file intreprets SEND, DELAY actions only as of now
285 */
286static long download_firmware(struct kim_data_s *kim_gdata)
287{
288	long err = 0;
289	long len = 0;
290	unsigned char *ptr = NULL;
291	unsigned char *action_ptr = NULL;
292	unsigned char bts_scr_name[40] = { 0 };	/* 40 char long bts scr name? */
293	int wr_room_space;
294	int cmd_size;
295	unsigned long timeout;
296
297	err = read_local_version(kim_gdata, bts_scr_name);
298	if (err != 0) {
299		pr_err("kim: failed to read local ver");
300		return err;
301	}
302	err =
303	    request_firmware(&kim_gdata->fw_entry, bts_scr_name,
304			     &kim_gdata->kim_pdev->dev);
305	if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
306		     (kim_gdata->fw_entry->size == 0))) {
307		pr_err(" request_firmware failed(errno %ld) for %s", err,
308			   bts_scr_name);
309		return -EINVAL;
310	}
311	ptr = (void *)kim_gdata->fw_entry->data;
312	len = kim_gdata->fw_entry->size;
313	/* bts_header to remove out magic number and
314	 * version
315	 */
316	ptr += sizeof(struct bts_header);
317	len -= sizeof(struct bts_header);
318
319	while (len > 0 && ptr) {
320		pr_debug(" action size %d, type %d ",
321			   ((struct bts_action *)ptr)->size,
322			   ((struct bts_action *)ptr)->type);
323
324		switch (((struct bts_action *)ptr)->type) {
325		case ACTION_SEND_COMMAND:	/* action send */
326			pr_debug("S");
327			action_ptr = &(((struct bts_action *)ptr)->data[0]);
328			if (unlikely
329			    (((struct hci_command *)action_ptr)->opcode ==
330			     0xFF36)) {
331				/* ignore remote change
332				 * baud rate HCI VS command */
333				pr_warn("change remote baud"
334				    " rate command in firmware");
335				skip_change_remote_baud(&ptr, &len);
336				break;
337			}
338			/*
339			 * Make sure we have enough free space in uart
340			 * tx buffer to write current firmware command
341			 */
342			cmd_size = ((struct bts_action *)ptr)->size;
343			timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
344			do {
345				wr_room_space =
346					st_get_uart_wr_room(kim_gdata->core_data);
347				if (wr_room_space < 0) {
348					pr_err("Unable to get free "
349							"space info from uart tx buffer");
350					release_firmware(kim_gdata->fw_entry);
351					return wr_room_space;
352				}
353				mdelay(1); /* wait 1ms before checking room */
354			} while ((wr_room_space < cmd_size) &&
355					time_before(jiffies, timeout));
356
357			/* Timeout happened ? */
358			if (time_after_eq(jiffies, timeout)) {
359				pr_err("Timeout while waiting for free "
360						"free space in uart tx buffer");
361				release_firmware(kim_gdata->fw_entry);
362				return -ETIMEDOUT;
363			}
364			/* reinit completion before sending for the
365			 * relevant wait
366			 */
367			reinit_completion(&kim_gdata->kim_rcvd);
368
369			/*
370			 * Free space found in uart buffer, call st_int_write
371			 * to send current firmware command to the uart tx
372			 * buffer.
373			 */
374			err = st_int_write(kim_gdata->core_data,
375			((struct bts_action_send *)action_ptr)->data,
376					   ((struct bts_action *)ptr)->size);
377			if (unlikely(err < 0)) {
378				release_firmware(kim_gdata->fw_entry);
379				return err;
380			}
381			/*
382			 * Check number of bytes written to the uart tx buffer
383			 * and requested command write size
384			 */
385			if (err != cmd_size) {
386				pr_err("Number of bytes written to uart "
387						"tx buffer are not matching with "
388						"requested cmd write size");
389				release_firmware(kim_gdata->fw_entry);
390				return -EIO;
391			}
392			break;
393		case ACTION_WAIT_EVENT:  /* wait */
394			pr_debug("W");
395			err = wait_for_completion_interruptible_timeout(
396					&kim_gdata->kim_rcvd,
397					msecs_to_jiffies(CMD_RESP_TIME));
398			if (err <= 0) {
399				pr_err("response timeout/signaled during fw download ");
400				/* timed out */
401				release_firmware(kim_gdata->fw_entry);
402				return err ? -ERESTARTSYS : -ETIMEDOUT;
403			}
404			reinit_completion(&kim_gdata->kim_rcvd);
405			break;
406		case ACTION_DELAY:	/* sleep */
407			pr_info("sleep command in scr");
408			action_ptr = &(((struct bts_action *)ptr)->data[0]);
409			mdelay(((struct bts_action_delay *)action_ptr)->msec);
410			break;
411		}
412		len =
413		    len - (sizeof(struct bts_action) +
414			   ((struct bts_action *)ptr)->size);
415		ptr =
416		    ptr + sizeof(struct bts_action) +
417		    ((struct bts_action *)ptr)->size;
418	}
419	/* fw download complete */
420	release_firmware(kim_gdata->fw_entry);
421	return 0;
422}
423
424/**********************************************************************/
425/* functions called from ST core */
426/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
427 * can be because of
428 * 1. response to read local version
429 * 2. during send/recv's of firmware download
430 */
431void st_kim_recv(void *disc_data, const unsigned char *data, long count)
432{
433	struct st_data_s	*st_gdata = (struct st_data_s *)disc_data;
434	struct kim_data_s	*kim_gdata = st_gdata->kim_data;
435
436	/* proceed to gather all data and distinguish read fw version response
437	 * from other fw responses when data gathering is complete
438	 */
439	kim_int_recv(kim_gdata, data, count);
440	return;
441}
442
443/* to signal completion of line discipline installation
444 * called from ST Core, upon tty_open
445 */
446void st_kim_complete(void *kim_data)
447{
448	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
449	complete(&kim_gdata->ldisc_installed);
450}
451
452/**
453 * st_kim_start - called from ST Core upon 1st registration
454 *	This involves toggling the chip enable gpio, reading
455 *	the firmware version from chip, forming the fw file name
456 *	based on the chip version, requesting the fw, parsing it
457 *	and perform download(send/recv).
458 */
459long st_kim_start(void *kim_data)
460{
461	long err = 0;
462	long retry = POR_RETRY_COUNT;
463	struct ti_st_plat_data	*pdata;
464	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
465
466	pr_info(" %s", __func__);
467	pdata = kim_gdata->kim_pdev->dev.platform_data;
468
469	do {
470		/* platform specific enabling code here */
471		if (pdata->chip_enable)
472			pdata->chip_enable(kim_gdata);
473
474		/* Configure BT nShutdown to HIGH state */
475		gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
476		mdelay(5);	/* FIXME: a proper toggle */
477		gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
478		mdelay(100);
479		/* re-initialize the completion */
480		reinit_completion(&kim_gdata->ldisc_installed);
481		/* send notification to UIM */
482		kim_gdata->ldisc_install = 1;
483		pr_info("ldisc_install = 1");
484		sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
485				NULL, "install");
486		/* wait for ldisc to be installed */
487		err = wait_for_completion_interruptible_timeout(
488			&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
489		if (!err) {
490			/* ldisc installation timeout,
491			 * flush uart, power cycle BT_EN */
492			pr_err("ldisc installation timeout");
493			err = st_kim_stop(kim_gdata);
494			continue;
495		} else {
496			/* ldisc installed now */
497			pr_info("line discipline installed");
498			err = download_firmware(kim_gdata);
499			if (err != 0) {
500				/* ldisc installed but fw download failed,
501				 * flush uart & power cycle BT_EN */
502				pr_err("download firmware failed");
503				err = st_kim_stop(kim_gdata);
504				continue;
505			} else {	/* on success don't retry */
506				break;
507			}
508		}
509	} while (retry--);
510	return err;
511}
512
513/**
514 * st_kim_stop - stop communication with chip.
515 *	This can be called from ST Core/KIM, on the-
516 *	(a) last un-register when chip need not be powered there-after,
517 *	(b) upon failure to either install ldisc or download firmware.
518 *	The function is responsible to (a) notify UIM about un-installation,
519 *	(b) flush UART if the ldisc was installed.
520 *	(c) reset BT_EN - pull down nshutdown at the end.
521 *	(d) invoke platform's chip disabling routine.
522 */
523long st_kim_stop(void *kim_data)
524{
525	long err = 0;
526	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
527	struct ti_st_plat_data	*pdata =
528		kim_gdata->kim_pdev->dev.platform_data;
529	struct tty_struct	*tty = kim_gdata->core_data->tty;
530
531	reinit_completion(&kim_gdata->ldisc_installed);
532
533	if (tty) {	/* can be called before ldisc is installed */
534		/* Flush any pending characters in the driver and discipline. */
535		tty_ldisc_flush(tty);
536		tty_driver_flush_buffer(tty);
537	}
538
539	/* send uninstall notification to UIM */
540	pr_info("ldisc_install = 0");
541	kim_gdata->ldisc_install = 0;
542	sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
543
544	/* wait for ldisc to be un-installed */
545	err = wait_for_completion_interruptible_timeout(
546		&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
547	if (!err) {		/* timeout */
548		pr_err(" timed out waiting for ldisc to be un-installed");
549		err = -ETIMEDOUT;
550	}
551
552	/* By default configure BT nShutdown to LOW state */
553	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
554	mdelay(1);
555	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
556	mdelay(1);
557	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
558
559	/* platform specific disable */
560	if (pdata->chip_disable)
561		pdata->chip_disable(kim_gdata);
562	return err;
563}
564
565/**********************************************************************/
566/* functions called from subsystems */
567/* called when debugfs entry is read from */
568
569static int show_version(struct seq_file *s, void *unused)
570{
571	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
572	seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
573			kim_gdata->version.chip, kim_gdata->version.maj_ver,
574			kim_gdata->version.min_ver);
575	return 0;
576}
577
578static int show_list(struct seq_file *s, void *unused)
579{
580	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
581	kim_st_list_protocols(kim_gdata->core_data, s);
582	return 0;
583}
584
585static ssize_t show_install(struct device *dev,
586		struct device_attribute *attr, char *buf)
587{
588	struct kim_data_s *kim_data = dev_get_drvdata(dev);
589	return sprintf(buf, "%d\n", kim_data->ldisc_install);
590}
591
592#ifdef DEBUG
593static ssize_t store_dev_name(struct device *dev,
594		struct device_attribute *attr, const char *buf, size_t count)
595{
596	struct kim_data_s *kim_data = dev_get_drvdata(dev);
597	pr_debug("storing dev name >%s<", buf);
598	strncpy(kim_data->dev_name, buf, count);
599	pr_debug("stored dev name >%s<", kim_data->dev_name);
600	return count;
601}
602
603static ssize_t store_baud_rate(struct device *dev,
604		struct device_attribute *attr, const char *buf, size_t count)
605{
606	struct kim_data_s *kim_data = dev_get_drvdata(dev);
607	pr_debug("storing baud rate >%s<", buf);
608	sscanf(buf, "%ld", &kim_data->baud_rate);
609	pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
610	return count;
611}
612#endif	/* if DEBUG */
613
614static ssize_t show_dev_name(struct device *dev,
615		struct device_attribute *attr, char *buf)
616{
617	struct kim_data_s *kim_data = dev_get_drvdata(dev);
618	return sprintf(buf, "%s\n", kim_data->dev_name);
619}
620
621static ssize_t show_baud_rate(struct device *dev,
622		struct device_attribute *attr, char *buf)
623{
624	struct kim_data_s *kim_data = dev_get_drvdata(dev);
625	return sprintf(buf, "%d\n", kim_data->baud_rate);
626}
627
628static ssize_t show_flow_cntrl(struct device *dev,
629		struct device_attribute *attr, char *buf)
630{
631	struct kim_data_s *kim_data = dev_get_drvdata(dev);
632	return sprintf(buf, "%d\n", kim_data->flow_cntrl);
633}
634
635/* structures specific for sysfs entries */
636static struct kobj_attribute ldisc_install =
637__ATTR(install, 0444, (void *)show_install, NULL);
638
639static struct kobj_attribute uart_dev_name =
640#ifdef DEBUG	/* TODO: move this to debug-fs if possible */
641__ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
642#else
643__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
644#endif
645
646static struct kobj_attribute uart_baud_rate =
647#ifdef DEBUG	/* TODO: move to debugfs */
648__ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
649#else
650__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
651#endif
652
653static struct kobj_attribute uart_flow_cntrl =
654__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
655
656static struct attribute *uim_attrs[] = {
657	&ldisc_install.attr,
658	&uart_dev_name.attr,
659	&uart_baud_rate.attr,
660	&uart_flow_cntrl.attr,
661	NULL,
662};
663
664static struct attribute_group uim_attr_grp = {
665	.attrs = uim_attrs,
666};
667
668/**
669 * st_kim_ref - reference the core's data
670 *	This references the per-ST platform device in the arch/xx/
671 *	board-xx.c file.
672 *	This would enable multiple such platform devices to exist
673 *	on a given platform
674 */
675void st_kim_ref(struct st_data_s **core_data, int id)
676{
677	struct platform_device	*pdev;
678	struct kim_data_s	*kim_gdata;
679	/* get kim_gdata reference from platform device */
680	pdev = st_get_plat_device(id);
681	if (!pdev)
682		goto err;
683	kim_gdata = platform_get_drvdata(pdev);
684	if (!kim_gdata)
685		goto err;
686
687	*core_data = kim_gdata->core_data;
688	return;
689err:
690	*core_data = NULL;
691}
692
693static int kim_version_open(struct inode *i, struct file *f)
694{
695	return single_open(f, show_version, i->i_private);
696}
697
698static int kim_list_open(struct inode *i, struct file *f)
699{
700	return single_open(f, show_list, i->i_private);
701}
702
703static const struct file_operations version_debugfs_fops = {
704	/* version info */
705	.open = kim_version_open,
706	.read = seq_read,
707	.llseek = seq_lseek,
708	.release = single_release,
709};
710static const struct file_operations list_debugfs_fops = {
711	/* protocols info */
712	.open = kim_list_open,
713	.read = seq_read,
714	.llseek = seq_lseek,
715	.release = single_release,
716};
717
718/**********************************************************************/
719/* functions called from platform device driver subsystem
720 * need to have a relevant platform device entry in the platform's
721 * board-*.c file
722 */
723
724static struct dentry *kim_debugfs_dir;
725static int kim_probe(struct platform_device *pdev)
726{
727	struct kim_data_s	*kim_gdata;
728	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
729	int err;
730
731	if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
732		/* multiple devices could exist */
733		st_kim_devices[pdev->id] = pdev;
734	} else {
735		/* platform's sure about existence of 1 device */
736		st_kim_devices[0] = pdev;
737	}
738
739	kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
740	if (!kim_gdata) {
741		pr_err("no mem to allocate");
742		return -ENOMEM;
743	}
744	platform_set_drvdata(pdev, kim_gdata);
745
746	err = st_core_init(&kim_gdata->core_data);
747	if (err != 0) {
748		pr_err(" ST core init failed");
749		err = -EIO;
750		goto err_core_init;
751	}
752	/* refer to itself */
753	kim_gdata->core_data->kim_data = kim_gdata;
754
755	/* Claim the chip enable nShutdown gpio from the system */
756	kim_gdata->nshutdown = pdata->nshutdown_gpio;
757	err = gpio_request(kim_gdata->nshutdown, "kim");
758	if (unlikely(err)) {
759		pr_err(" gpio %d request failed ", kim_gdata->nshutdown);
760		return err;
761	}
762
763	/* Configure nShutdown GPIO as output=0 */
764	err = gpio_direction_output(kim_gdata->nshutdown, 0);
765	if (unlikely(err)) {
766		pr_err(" unable to configure gpio %d", kim_gdata->nshutdown);
767		return err;
768	}
769	/* get reference of pdev for request_firmware
770	 */
771	kim_gdata->kim_pdev = pdev;
772	init_completion(&kim_gdata->kim_rcvd);
773	init_completion(&kim_gdata->ldisc_installed);
774
775	err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
776	if (err) {
777		pr_err("failed to create sysfs entries");
778		goto err_sysfs_group;
779	}
780
781	/* copying platform data */
782	strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
783	kim_gdata->flow_cntrl = pdata->flow_cntrl;
784	kim_gdata->baud_rate = pdata->baud_rate;
785	pr_info("sysfs entries created\n");
786
787	kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
788	if (!kim_debugfs_dir) {
789		pr_err(" debugfs entries creation failed ");
790		return 0;
791	}
792
793	debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
794				kim_gdata, &version_debugfs_fops);
795	debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
796				kim_gdata, &list_debugfs_fops);
797	return 0;
798
799err_sysfs_group:
800	st_core_exit(kim_gdata->core_data);
801
802err_core_init:
803	kfree(kim_gdata);
804
805	return err;
806}
807
808static int kim_remove(struct platform_device *pdev)
809{
810	/* free the GPIOs requested */
811	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
812	struct kim_data_s	*kim_gdata;
813
814	kim_gdata = platform_get_drvdata(pdev);
815
816	/* Free the Bluetooth/FM/GPIO
817	 * nShutdown gpio from the system
818	 */
819	gpio_free(pdata->nshutdown_gpio);
820	pr_info("nshutdown GPIO Freed");
821
822	debugfs_remove_recursive(kim_debugfs_dir);
823	sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
824	pr_info("sysfs entries removed");
825
826	kim_gdata->kim_pdev = NULL;
827	st_core_exit(kim_gdata->core_data);
828
829	kfree(kim_gdata);
830	kim_gdata = NULL;
831	return 0;
832}
833
834static int kim_suspend(struct platform_device *pdev, pm_message_t state)
835{
836	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
837
838	if (pdata->suspend)
839		return pdata->suspend(pdev, state);
840
841	return 0;
842}
843
844static int kim_resume(struct platform_device *pdev)
845{
846	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
847
848	if (pdata->resume)
849		return pdata->resume(pdev);
850
851	return 0;
852}
853
854/**********************************************************************/
855/* entry point for ST KIM module, called in from ST Core */
856static struct platform_driver kim_platform_driver = {
857	.probe = kim_probe,
858	.remove = kim_remove,
859	.suspend = kim_suspend,
860	.resume = kim_resume,
861	.driver = {
862		.name = "kim",
863	},
864};
865
866module_platform_driver(kim_platform_driver);
867
868MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
869MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
870MODULE_LICENSE("GPL");
v4.17
  1/*
  2 *  Shared Transport Line discipline driver Core
  3 *	Init Manager module responsible for GPIO control
  4 *	and firmware download
  5 *  Copyright (C) 2009-2010 Texas Instruments
  6 *  Author: Pavan Savoy <pavan_savoy@ti.com>
  7 *
  8 *  This program is free software; you can redistribute it and/or modify
  9 *  it under the terms of the GNU General Public License version 2 as
 10 *  published by the Free Software Foundation.
 11 *
 12 *  This program is distributed in the hope that it will be useful,
 13 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 *  GNU General Public License for more details.
 16 *
 17 *  You should have received a copy of the GNU General Public License
 18 *  along with this program; if not, write to the Free Software
 19 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 20 *
 21 */
 22
 23#define pr_fmt(fmt) "(stk) :" fmt
 24#include <linux/platform_device.h>
 25#include <linux/jiffies.h>
 26#include <linux/firmware.h>
 27#include <linux/delay.h>
 28#include <linux/wait.h>
 29#include <linux/gpio.h>
 30#include <linux/debugfs.h>
 31#include <linux/seq_file.h>
 32#include <linux/sched.h>
 33#include <linux/sysfs.h>
 34#include <linux/tty.h>
 35
 36#include <linux/skbuff.h>
 37#include <linux/ti_wilink_st.h>
 38#include <linux/module.h>
 39
 40#define MAX_ST_DEVICES	3	/* Imagine 1 on each UART for now */
 41static struct platform_device *st_kim_devices[MAX_ST_DEVICES];
 42
 43/**********************************************************************/
 44/* internal functions */
 45
 46/**
 47 * st_get_plat_device -
 48 *	function which returns the reference to the platform device
 49 *	requested by id. As of now only 1 such device exists (id=0)
 50 *	the context requesting for reference can get the id to be
 51 *	requested by a. The protocol driver which is registering or
 52 *	b. the tty device which is opened.
 53 */
 54static struct platform_device *st_get_plat_device(int id)
 55{
 56	return st_kim_devices[id];
 57}
 58
 59/**
 60 * validate_firmware_response -
 61 *	function to return whether the firmware response was proper
 62 *	in case of error don't complete so that waiting for proper
 63 *	response times out
 64 */
 65static void validate_firmware_response(struct kim_data_s *kim_gdata)
 66{
 67	struct sk_buff *skb = kim_gdata->rx_skb;
 68	if (!skb)
 69		return;
 70
 71	/* these magic numbers are the position in the response buffer which
 72	 * allows us to distinguish whether the response is for the read
 73	 * version info. command
 74	 */
 75	if (skb->data[2] == 0x01 && skb->data[3] == 0x01 &&
 76			skb->data[4] == 0x10 && skb->data[5] == 0x00) {
 77		/* fw version response */
 78		memcpy(kim_gdata->resp_buffer,
 79				kim_gdata->rx_skb->data,
 80				kim_gdata->rx_skb->len);
 
 81		kim_gdata->rx_state = ST_W4_PACKET_TYPE;
 82		kim_gdata->rx_skb = NULL;
 83		kim_gdata->rx_count = 0;
 84	} else if (unlikely(skb->data[5] != 0)) {
 85		pr_err("no proper response during fw download");
 86		pr_err("data6 %x", skb->data[5]);
 87		kfree_skb(skb);
 88		return;		/* keep waiting for the proper response */
 89	}
 90	/* becos of all the script being downloaded */
 91	complete_all(&kim_gdata->kim_rcvd);
 92	kfree_skb(skb);
 93}
 94
 95/* check for data len received inside kim_int_recv
 96 * most often hit the last case to update state to waiting for data
 97 */
 98static inline int kim_check_data_len(struct kim_data_s *kim_gdata, int len)
 99{
100	register int room = skb_tailroom(kim_gdata->rx_skb);
101
102	pr_debug("len %d room %d", len, room);
103
104	if (!len) {
105		validate_firmware_response(kim_gdata);
106	} else if (len > room) {
107		/* Received packet's payload length is larger.
108		 * We can't accommodate it in created skb.
109		 */
110		pr_err("Data length is too large len %d room %d", len,
111			   room);
112		kfree_skb(kim_gdata->rx_skb);
113	} else {
114		/* Packet header has non-zero payload length and
115		 * we have enough space in created skb. Lets read
116		 * payload data */
117		kim_gdata->rx_state = ST_W4_DATA;
118		kim_gdata->rx_count = len;
119		return len;
120	}
121
122	/* Change ST LL state to continue to process next
123	 * packet */
124	kim_gdata->rx_state = ST_W4_PACKET_TYPE;
125	kim_gdata->rx_skb = NULL;
126	kim_gdata->rx_count = 0;
127
128	return 0;
129}
130
131/**
132 * kim_int_recv - receive function called during firmware download
133 *	firmware download responses on different UART drivers
134 *	have been observed to come in bursts of different
135 *	tty_receive and hence the logic
136 */
137static void kim_int_recv(struct kim_data_s *kim_gdata,
138	const unsigned char *data, long count)
139{
140	const unsigned char *ptr;
141	int len = 0, type = 0;
142	unsigned char *plen;
143
144	pr_debug("%s", __func__);
145	/* Decode received bytes here */
146	ptr = data;
147	if (unlikely(ptr == NULL)) {
148		pr_err(" received null from TTY ");
149		return;
150	}
151
152	while (count) {
153		if (kim_gdata->rx_count) {
154			len = min_t(unsigned int, kim_gdata->rx_count, count);
155			skb_put_data(kim_gdata->rx_skb, ptr, len);
156			kim_gdata->rx_count -= len;
157			count -= len;
158			ptr += len;
159
160			if (kim_gdata->rx_count)
161				continue;
162
163			/* Check ST RX state machine , where are we? */
164			switch (kim_gdata->rx_state) {
165				/* Waiting for complete packet ? */
166			case ST_W4_DATA:
167				pr_debug("Complete pkt received");
168				validate_firmware_response(kim_gdata);
169				kim_gdata->rx_state = ST_W4_PACKET_TYPE;
170				kim_gdata->rx_skb = NULL;
171				continue;
172				/* Waiting for Bluetooth event header ? */
173			case ST_W4_HEADER:
174				plen =
175				(unsigned char *)&kim_gdata->rx_skb->data[1];
176				pr_debug("event hdr: plen 0x%02x\n", *plen);
177				kim_check_data_len(kim_gdata, *plen);
178				continue;
179			}	/* end of switch */
180		}		/* end of if rx_state */
181		switch (*ptr) {
182			/* Bluetooth event packet? */
183		case 0x04:
184			kim_gdata->rx_state = ST_W4_HEADER;
185			kim_gdata->rx_count = 2;
186			type = *ptr;
187			break;
188		default:
189			pr_info("unknown packet");
190			ptr++;
191			count--;
192			continue;
193		}
194		ptr++;
195		count--;
196		kim_gdata->rx_skb =
197			alloc_skb(1024+8, GFP_ATOMIC);
198		if (!kim_gdata->rx_skb) {
199			pr_err("can't allocate mem for new packet");
200			kim_gdata->rx_state = ST_W4_PACKET_TYPE;
201			kim_gdata->rx_count = 0;
202			return;
203		}
204		skb_reserve(kim_gdata->rx_skb, 8);
205		kim_gdata->rx_skb->cb[0] = 4;
206		kim_gdata->rx_skb->cb[1] = 0;
207
208	}
209	return;
210}
211
212static long read_local_version(struct kim_data_s *kim_gdata, char *bts_scr_name)
213{
214	unsigned short version = 0, chip = 0, min_ver = 0, maj_ver = 0;
215	const char read_ver_cmd[] = { 0x01, 0x01, 0x10, 0x00 };
216	long timeout;
217
218	pr_debug("%s", __func__);
219
220	reinit_completion(&kim_gdata->kim_rcvd);
221	if (4 != st_int_write(kim_gdata->core_data, read_ver_cmd, 4)) {
222		pr_err("kim: couldn't write 4 bytes");
223		return -EIO;
224	}
225
226	timeout = wait_for_completion_interruptible_timeout(
227		&kim_gdata->kim_rcvd, msecs_to_jiffies(CMD_RESP_TIME));
228	if (timeout <= 0) {
229		pr_err(" waiting for ver info- timed out or received signal");
230		return timeout ? -ERESTARTSYS : -ETIMEDOUT;
231	}
232	reinit_completion(&kim_gdata->kim_rcvd);
233	/* the positions 12 & 13 in the response buffer provide with the
234	 * chip, major & minor numbers
235	 */
236
237	version =
238		MAKEWORD(kim_gdata->resp_buffer[12],
239				kim_gdata->resp_buffer[13]);
240	chip = (version & 0x7C00) >> 10;
241	min_ver = (version & 0x007F);
242	maj_ver = (version & 0x0380) >> 7;
243
244	if (version & 0x8000)
245		maj_ver |= 0x0008;
246
247	sprintf(bts_scr_name, "ti-connectivity/TIInit_%d.%d.%d.bts",
248		chip, maj_ver, min_ver);
249
250	/* to be accessed later via sysfs entry */
251	kim_gdata->version.full = version;
252	kim_gdata->version.chip = chip;
253	kim_gdata->version.maj_ver = maj_ver;
254	kim_gdata->version.min_ver = min_ver;
255
256	pr_info("%s", bts_scr_name);
257	return 0;
258}
259
260static void skip_change_remote_baud(unsigned char **ptr, long *len)
261{
262	unsigned char *nxt_action, *cur_action;
263	cur_action = *ptr;
264
265	nxt_action = cur_action + sizeof(struct bts_action) +
266		((struct bts_action *) cur_action)->size;
267
268	if (((struct bts_action *) nxt_action)->type != ACTION_WAIT_EVENT) {
269		pr_err("invalid action after change remote baud command");
270	} else {
271		*ptr = *ptr + sizeof(struct bts_action) +
272			((struct bts_action *)cur_action)->size;
273		*len = *len - (sizeof(struct bts_action) +
274				((struct bts_action *)cur_action)->size);
275		/* warn user on not commenting these in firmware */
276		pr_warn("skipping the wait event of change remote baud");
277	}
278}
279
280/**
281 * download_firmware -
282 *	internal function which parses through the .bts firmware
283 *	script file intreprets SEND, DELAY actions only as of now
284 */
285static long download_firmware(struct kim_data_s *kim_gdata)
286{
287	long err = 0;
288	long len = 0;
289	unsigned char *ptr = NULL;
290	unsigned char *action_ptr = NULL;
291	unsigned char bts_scr_name[40] = { 0 };	/* 40 char long bts scr name? */
292	int wr_room_space;
293	int cmd_size;
294	unsigned long timeout;
295
296	err = read_local_version(kim_gdata, bts_scr_name);
297	if (err != 0) {
298		pr_err("kim: failed to read local ver");
299		return err;
300	}
301	err =
302	    request_firmware(&kim_gdata->fw_entry, bts_scr_name,
303			     &kim_gdata->kim_pdev->dev);
304	if (unlikely((err != 0) || (kim_gdata->fw_entry->data == NULL) ||
305		     (kim_gdata->fw_entry->size == 0))) {
306		pr_err(" request_firmware failed(errno %ld) for %s", err,
307			   bts_scr_name);
308		return -EINVAL;
309	}
310	ptr = (void *)kim_gdata->fw_entry->data;
311	len = kim_gdata->fw_entry->size;
312	/* bts_header to remove out magic number and
313	 * version
314	 */
315	ptr += sizeof(struct bts_header);
316	len -= sizeof(struct bts_header);
317
318	while (len > 0 && ptr) {
319		pr_debug(" action size %d, type %d ",
320			   ((struct bts_action *)ptr)->size,
321			   ((struct bts_action *)ptr)->type);
322
323		switch (((struct bts_action *)ptr)->type) {
324		case ACTION_SEND_COMMAND:	/* action send */
325			pr_debug("S");
326			action_ptr = &(((struct bts_action *)ptr)->data[0]);
327			if (unlikely
328			    (((struct hci_command *)action_ptr)->opcode ==
329			     0xFF36)) {
330				/* ignore remote change
331				 * baud rate HCI VS command */
332				pr_warn("change remote baud"
333				    " rate command in firmware");
334				skip_change_remote_baud(&ptr, &len);
335				break;
336			}
337			/*
338			 * Make sure we have enough free space in uart
339			 * tx buffer to write current firmware command
340			 */
341			cmd_size = ((struct bts_action *)ptr)->size;
342			timeout = jiffies + msecs_to_jiffies(CMD_WR_TIME);
343			do {
344				wr_room_space =
345					st_get_uart_wr_room(kim_gdata->core_data);
346				if (wr_room_space < 0) {
347					pr_err("Unable to get free "
348							"space info from uart tx buffer");
349					release_firmware(kim_gdata->fw_entry);
350					return wr_room_space;
351				}
352				mdelay(1); /* wait 1ms before checking room */
353			} while ((wr_room_space < cmd_size) &&
354					time_before(jiffies, timeout));
355
356			/* Timeout happened ? */
357			if (time_after_eq(jiffies, timeout)) {
358				pr_err("Timeout while waiting for free "
359						"free space in uart tx buffer");
360				release_firmware(kim_gdata->fw_entry);
361				return -ETIMEDOUT;
362			}
363			/* reinit completion before sending for the
364			 * relevant wait
365			 */
366			reinit_completion(&kim_gdata->kim_rcvd);
367
368			/*
369			 * Free space found in uart buffer, call st_int_write
370			 * to send current firmware command to the uart tx
371			 * buffer.
372			 */
373			err = st_int_write(kim_gdata->core_data,
374			((struct bts_action_send *)action_ptr)->data,
375					   ((struct bts_action *)ptr)->size);
376			if (unlikely(err < 0)) {
377				release_firmware(kim_gdata->fw_entry);
378				return err;
379			}
380			/*
381			 * Check number of bytes written to the uart tx buffer
382			 * and requested command write size
383			 */
384			if (err != cmd_size) {
385				pr_err("Number of bytes written to uart "
386						"tx buffer are not matching with "
387						"requested cmd write size");
388				release_firmware(kim_gdata->fw_entry);
389				return -EIO;
390			}
391			break;
392		case ACTION_WAIT_EVENT:  /* wait */
393			pr_debug("W");
394			err = wait_for_completion_interruptible_timeout(
395					&kim_gdata->kim_rcvd,
396					msecs_to_jiffies(CMD_RESP_TIME));
397			if (err <= 0) {
398				pr_err("response timeout/signaled during fw download ");
399				/* timed out */
400				release_firmware(kim_gdata->fw_entry);
401				return err ? -ERESTARTSYS : -ETIMEDOUT;
402			}
403			reinit_completion(&kim_gdata->kim_rcvd);
404			break;
405		case ACTION_DELAY:	/* sleep */
406			pr_info("sleep command in scr");
407			action_ptr = &(((struct bts_action *)ptr)->data[0]);
408			mdelay(((struct bts_action_delay *)action_ptr)->msec);
409			break;
410		}
411		len =
412		    len - (sizeof(struct bts_action) +
413			   ((struct bts_action *)ptr)->size);
414		ptr =
415		    ptr + sizeof(struct bts_action) +
416		    ((struct bts_action *)ptr)->size;
417	}
418	/* fw download complete */
419	release_firmware(kim_gdata->fw_entry);
420	return 0;
421}
422
423/**********************************************************************/
424/* functions called from ST core */
425/* called from ST Core, when REG_IN_PROGRESS (registration in progress)
426 * can be because of
427 * 1. response to read local version
428 * 2. during send/recv's of firmware download
429 */
430void st_kim_recv(void *disc_data, const unsigned char *data, long count)
431{
432	struct st_data_s	*st_gdata = (struct st_data_s *)disc_data;
433	struct kim_data_s	*kim_gdata = st_gdata->kim_data;
434
435	/* proceed to gather all data and distinguish read fw version response
436	 * from other fw responses when data gathering is complete
437	 */
438	kim_int_recv(kim_gdata, data, count);
439	return;
440}
441
442/* to signal completion of line discipline installation
443 * called from ST Core, upon tty_open
444 */
445void st_kim_complete(void *kim_data)
446{
447	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
448	complete(&kim_gdata->ldisc_installed);
449}
450
451/**
452 * st_kim_start - called from ST Core upon 1st registration
453 *	This involves toggling the chip enable gpio, reading
454 *	the firmware version from chip, forming the fw file name
455 *	based on the chip version, requesting the fw, parsing it
456 *	and perform download(send/recv).
457 */
458long st_kim_start(void *kim_data)
459{
460	long err = 0;
461	long retry = POR_RETRY_COUNT;
462	struct ti_st_plat_data	*pdata;
463	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
464
465	pr_info(" %s", __func__);
466	pdata = kim_gdata->kim_pdev->dev.platform_data;
467
468	do {
469		/* platform specific enabling code here */
470		if (pdata->chip_enable)
471			pdata->chip_enable(kim_gdata);
472
473		/* Configure BT nShutdown to HIGH state */
474		gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
475		mdelay(5);	/* FIXME: a proper toggle */
476		gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
477		mdelay(100);
478		/* re-initialize the completion */
479		reinit_completion(&kim_gdata->ldisc_installed);
480		/* send notification to UIM */
481		kim_gdata->ldisc_install = 1;
482		pr_info("ldisc_install = 1");
483		sysfs_notify(&kim_gdata->kim_pdev->dev.kobj,
484				NULL, "install");
485		/* wait for ldisc to be installed */
486		err = wait_for_completion_interruptible_timeout(
487			&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
488		if (!err) {
489			/* ldisc installation timeout,
490			 * flush uart, power cycle BT_EN */
491			pr_err("ldisc installation timeout");
492			err = st_kim_stop(kim_gdata);
493			continue;
494		} else {
495			/* ldisc installed now */
496			pr_info("line discipline installed");
497			err = download_firmware(kim_gdata);
498			if (err != 0) {
499				/* ldisc installed but fw download failed,
500				 * flush uart & power cycle BT_EN */
501				pr_err("download firmware failed");
502				err = st_kim_stop(kim_gdata);
503				continue;
504			} else {	/* on success don't retry */
505				break;
506			}
507		}
508	} while (retry--);
509	return err;
510}
511
512/**
513 * st_kim_stop - stop communication with chip.
514 *	This can be called from ST Core/KIM, on the-
515 *	(a) last un-register when chip need not be powered there-after,
516 *	(b) upon failure to either install ldisc or download firmware.
517 *	The function is responsible to (a) notify UIM about un-installation,
518 *	(b) flush UART if the ldisc was installed.
519 *	(c) reset BT_EN - pull down nshutdown at the end.
520 *	(d) invoke platform's chip disabling routine.
521 */
522long st_kim_stop(void *kim_data)
523{
524	long err = 0;
525	struct kim_data_s	*kim_gdata = (struct kim_data_s *)kim_data;
526	struct ti_st_plat_data	*pdata =
527		kim_gdata->kim_pdev->dev.platform_data;
528	struct tty_struct	*tty = kim_gdata->core_data->tty;
529
530	reinit_completion(&kim_gdata->ldisc_installed);
531
532	if (tty) {	/* can be called before ldisc is installed */
533		/* Flush any pending characters in the driver and discipline. */
534		tty_ldisc_flush(tty);
535		tty_driver_flush_buffer(tty);
536	}
537
538	/* send uninstall notification to UIM */
539	pr_info("ldisc_install = 0");
540	kim_gdata->ldisc_install = 0;
541	sysfs_notify(&kim_gdata->kim_pdev->dev.kobj, NULL, "install");
542
543	/* wait for ldisc to be un-installed */
544	err = wait_for_completion_interruptible_timeout(
545		&kim_gdata->ldisc_installed, msecs_to_jiffies(LDISC_TIME));
546	if (!err) {		/* timeout */
547		pr_err(" timed out waiting for ldisc to be un-installed");
548		err = -ETIMEDOUT;
549	}
550
551	/* By default configure BT nShutdown to LOW state */
552	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
553	mdelay(1);
554	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_HIGH);
555	mdelay(1);
556	gpio_set_value_cansleep(kim_gdata->nshutdown, GPIO_LOW);
557
558	/* platform specific disable */
559	if (pdata->chip_disable)
560		pdata->chip_disable(kim_gdata);
561	return err;
562}
563
564/**********************************************************************/
565/* functions called from subsystems */
566/* called when debugfs entry is read from */
567
568static int show_version(struct seq_file *s, void *unused)
569{
570	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
571	seq_printf(s, "%04X %d.%d.%d\n", kim_gdata->version.full,
572			kim_gdata->version.chip, kim_gdata->version.maj_ver,
573			kim_gdata->version.min_ver);
574	return 0;
575}
576
577static int show_list(struct seq_file *s, void *unused)
578{
579	struct kim_data_s *kim_gdata = (struct kim_data_s *)s->private;
580	kim_st_list_protocols(kim_gdata->core_data, s);
581	return 0;
582}
583
584static ssize_t show_install(struct device *dev,
585		struct device_attribute *attr, char *buf)
586{
587	struct kim_data_s *kim_data = dev_get_drvdata(dev);
588	return sprintf(buf, "%d\n", kim_data->ldisc_install);
589}
590
591#ifdef DEBUG
592static ssize_t store_dev_name(struct device *dev,
593		struct device_attribute *attr, const char *buf, size_t count)
594{
595	struct kim_data_s *kim_data = dev_get_drvdata(dev);
596	pr_debug("storing dev name >%s<", buf);
597	strncpy(kim_data->dev_name, buf, count);
598	pr_debug("stored dev name >%s<", kim_data->dev_name);
599	return count;
600}
601
602static ssize_t store_baud_rate(struct device *dev,
603		struct device_attribute *attr, const char *buf, size_t count)
604{
605	struct kim_data_s *kim_data = dev_get_drvdata(dev);
606	pr_debug("storing baud rate >%s<", buf);
607	sscanf(buf, "%ld", &kim_data->baud_rate);
608	pr_debug("stored baud rate >%ld<", kim_data->baud_rate);
609	return count;
610}
611#endif	/* if DEBUG */
612
613static ssize_t show_dev_name(struct device *dev,
614		struct device_attribute *attr, char *buf)
615{
616	struct kim_data_s *kim_data = dev_get_drvdata(dev);
617	return sprintf(buf, "%s\n", kim_data->dev_name);
618}
619
620static ssize_t show_baud_rate(struct device *dev,
621		struct device_attribute *attr, char *buf)
622{
623	struct kim_data_s *kim_data = dev_get_drvdata(dev);
624	return sprintf(buf, "%d\n", kim_data->baud_rate);
625}
626
627static ssize_t show_flow_cntrl(struct device *dev,
628		struct device_attribute *attr, char *buf)
629{
630	struct kim_data_s *kim_data = dev_get_drvdata(dev);
631	return sprintf(buf, "%d\n", kim_data->flow_cntrl);
632}
633
634/* structures specific for sysfs entries */
635static struct kobj_attribute ldisc_install =
636__ATTR(install, 0444, (void *)show_install, NULL);
637
638static struct kobj_attribute uart_dev_name =
639#ifdef DEBUG	/* TODO: move this to debug-fs if possible */
640__ATTR(dev_name, 0644, (void *)show_dev_name, (void *)store_dev_name);
641#else
642__ATTR(dev_name, 0444, (void *)show_dev_name, NULL);
643#endif
644
645static struct kobj_attribute uart_baud_rate =
646#ifdef DEBUG	/* TODO: move to debugfs */
647__ATTR(baud_rate, 0644, (void *)show_baud_rate, (void *)store_baud_rate);
648#else
649__ATTR(baud_rate, 0444, (void *)show_baud_rate, NULL);
650#endif
651
652static struct kobj_attribute uart_flow_cntrl =
653__ATTR(flow_cntrl, 0444, (void *)show_flow_cntrl, NULL);
654
655static struct attribute *uim_attrs[] = {
656	&ldisc_install.attr,
657	&uart_dev_name.attr,
658	&uart_baud_rate.attr,
659	&uart_flow_cntrl.attr,
660	NULL,
661};
662
663static const struct attribute_group uim_attr_grp = {
664	.attrs = uim_attrs,
665};
666
667/**
668 * st_kim_ref - reference the core's data
669 *	This references the per-ST platform device in the arch/xx/
670 *	board-xx.c file.
671 *	This would enable multiple such platform devices to exist
672 *	on a given platform
673 */
674void st_kim_ref(struct st_data_s **core_data, int id)
675{
676	struct platform_device	*pdev;
677	struct kim_data_s	*kim_gdata;
678	/* get kim_gdata reference from platform device */
679	pdev = st_get_plat_device(id);
680	if (!pdev)
681		goto err;
682	kim_gdata = platform_get_drvdata(pdev);
683	if (!kim_gdata)
684		goto err;
685
686	*core_data = kim_gdata->core_data;
687	return;
688err:
689	*core_data = NULL;
690}
691
692static int kim_version_open(struct inode *i, struct file *f)
693{
694	return single_open(f, show_version, i->i_private);
695}
696
697static int kim_list_open(struct inode *i, struct file *f)
698{
699	return single_open(f, show_list, i->i_private);
700}
701
702static const struct file_operations version_debugfs_fops = {
703	/* version info */
704	.open = kim_version_open,
705	.read = seq_read,
706	.llseek = seq_lseek,
707	.release = single_release,
708};
709static const struct file_operations list_debugfs_fops = {
710	/* protocols info */
711	.open = kim_list_open,
712	.read = seq_read,
713	.llseek = seq_lseek,
714	.release = single_release,
715};
716
717/**********************************************************************/
718/* functions called from platform device driver subsystem
719 * need to have a relevant platform device entry in the platform's
720 * board-*.c file
721 */
722
723static struct dentry *kim_debugfs_dir;
724static int kim_probe(struct platform_device *pdev)
725{
726	struct kim_data_s	*kim_gdata;
727	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
728	int err;
729
730	if ((pdev->id != -1) && (pdev->id < MAX_ST_DEVICES)) {
731		/* multiple devices could exist */
732		st_kim_devices[pdev->id] = pdev;
733	} else {
734		/* platform's sure about existence of 1 device */
735		st_kim_devices[0] = pdev;
736	}
737
738	kim_gdata = kzalloc(sizeof(struct kim_data_s), GFP_ATOMIC);
739	if (!kim_gdata) {
740		pr_err("no mem to allocate");
741		return -ENOMEM;
742	}
743	platform_set_drvdata(pdev, kim_gdata);
744
745	err = st_core_init(&kim_gdata->core_data);
746	if (err != 0) {
747		pr_err(" ST core init failed");
748		err = -EIO;
749		goto err_core_init;
750	}
751	/* refer to itself */
752	kim_gdata->core_data->kim_data = kim_gdata;
753
754	/* Claim the chip enable nShutdown gpio from the system */
755	kim_gdata->nshutdown = pdata->nshutdown_gpio;
756	err = gpio_request(kim_gdata->nshutdown, "kim");
757	if (unlikely(err)) {
758		pr_err(" gpio %d request failed ", kim_gdata->nshutdown);
759		return err;
760	}
761
762	/* Configure nShutdown GPIO as output=0 */
763	err = gpio_direction_output(kim_gdata->nshutdown, 0);
764	if (unlikely(err)) {
765		pr_err(" unable to configure gpio %d", kim_gdata->nshutdown);
766		return err;
767	}
768	/* get reference of pdev for request_firmware
769	 */
770	kim_gdata->kim_pdev = pdev;
771	init_completion(&kim_gdata->kim_rcvd);
772	init_completion(&kim_gdata->ldisc_installed);
773
774	err = sysfs_create_group(&pdev->dev.kobj, &uim_attr_grp);
775	if (err) {
776		pr_err("failed to create sysfs entries");
777		goto err_sysfs_group;
778	}
779
780	/* copying platform data */
781	strncpy(kim_gdata->dev_name, pdata->dev_name, UART_DEV_NAME_LEN);
782	kim_gdata->flow_cntrl = pdata->flow_cntrl;
783	kim_gdata->baud_rate = pdata->baud_rate;
784	pr_info("sysfs entries created\n");
785
786	kim_debugfs_dir = debugfs_create_dir("ti-st", NULL);
787	if (!kim_debugfs_dir) {
788		pr_err(" debugfs entries creation failed ");
789		return 0;
790	}
791
792	debugfs_create_file("version", S_IRUGO, kim_debugfs_dir,
793				kim_gdata, &version_debugfs_fops);
794	debugfs_create_file("protocols", S_IRUGO, kim_debugfs_dir,
795				kim_gdata, &list_debugfs_fops);
796	return 0;
797
798err_sysfs_group:
799	st_core_exit(kim_gdata->core_data);
800
801err_core_init:
802	kfree(kim_gdata);
803
804	return err;
805}
806
807static int kim_remove(struct platform_device *pdev)
808{
809	/* free the GPIOs requested */
810	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
811	struct kim_data_s	*kim_gdata;
812
813	kim_gdata = platform_get_drvdata(pdev);
814
815	/* Free the Bluetooth/FM/GPIO
816	 * nShutdown gpio from the system
817	 */
818	gpio_free(pdata->nshutdown_gpio);
819	pr_info("nshutdown GPIO Freed");
820
821	debugfs_remove_recursive(kim_debugfs_dir);
822	sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
823	pr_info("sysfs entries removed");
824
825	kim_gdata->kim_pdev = NULL;
826	st_core_exit(kim_gdata->core_data);
827
828	kfree(kim_gdata);
829	kim_gdata = NULL;
830	return 0;
831}
832
833static int kim_suspend(struct platform_device *pdev, pm_message_t state)
834{
835	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
836
837	if (pdata->suspend)
838		return pdata->suspend(pdev, state);
839
840	return 0;
841}
842
843static int kim_resume(struct platform_device *pdev)
844{
845	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
846
847	if (pdata->resume)
848		return pdata->resume(pdev);
849
850	return 0;
851}
852
853/**********************************************************************/
854/* entry point for ST KIM module, called in from ST Core */
855static struct platform_driver kim_platform_driver = {
856	.probe = kim_probe,
857	.remove = kim_remove,
858	.suspend = kim_suspend,
859	.resume = kim_resume,
860	.driver = {
861		.name = "kim",
862	},
863};
864
865module_platform_driver(kim_platform_driver);
866
867MODULE_AUTHOR("Pavan Savoy <pavan_savoy@ti.com>");
868MODULE_DESCRIPTION("Shared Transport Driver for TI BT/FM/GPS combo chips ");
869MODULE_LICENSE("GPL");