smscoreapi.c 33.5 KB
Newer Older
1
/*
2 3 4 5 6
 *  Siano core API module
 *
 *  This file contains implementation for the interface to sms core component
 *
 *  author: Anatoly Greenblat
7
 *
8
 *  Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
9 10
 *
 *  This program is free software; you can redistribute it and/or modify
11 12
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation;
13
 *
14 15
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
16
 *
17
 *  See the GNU General Public License for more details.
18 19 20 21 22 23
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

24 25 26 27 28 29 30 31 32 33 34 35
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/dma-mapping.h>
#include <linux/delay.h>
#include <asm/io.h>

#include <linux/firmware.h>

#include "smscoreapi.h"

36 37 38
#define PERROR(fmt, args...)\
	printk(KERN_ERR "smscore error: line %d- %s(): " fmt, \
		__LINE__,  __func__, ## args)
39 40 41

#ifdef SMSCORE_DEBUG
#undef PWARNING
42 43 44
#  define PWARNING(fmt, args...) printk(KERN_INFO "smscore warning: " \
					"line %d- %s(): " fmt, \
					__LINE__, __func__, ## args)
45
#undef PDEBUG					/* undef it, just in case */
46 47
#  define PDEBUG(fmt, args...)   printk(KERN_INFO "smscore - %s(): " fmt, \
					__func__, ## args)
48 49 50 51 52
#else /*SMSCORE_DEBUG*/
#define PDEBUG(fmt, args...)
#define PWARNING(fmt, args...)
#endif

53
struct smscore_device_notifyee_t {
54 55
	struct list_head entry;
	hotplug_t hotplug;
56
};
57

58
struct smscore_idlist_t {
59 60 61
	struct list_head entry;
	int		id;
	int		data_type;
62
};
63

64
struct smscore_client_t {
65
	struct list_head entry;
66
	struct smscore_device_t *coredev;
67
	void			*context;
68
	struct list_head 	idlist;
69 70
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
71
};
72

73
struct smscore_device_t {
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
	struct list_head entry;

	struct list_head clients;
	struct list_head subclients;
	spinlock_t		clientslock;

	struct list_head buffers;
	spinlock_t		bufferslock;
	int				num_buffers;

	void			*common_buffer;
	int				common_buffer_size;
	dma_addr_t		common_buffer_phys;

	void			*context;
	struct device	*device;

	char			devpath[32];
	unsigned long	device_flags;

	setmode_t		setmode_handler;
	detectmode_t	detectmode_handler;
	sendrequest_t	sendrequest_handler;
	preload_t		preload_handler;
	postload_t		postload_handler;

	int				mode, modes_supported;

	struct completion version_ex_done, data_download_done, trigger_done;
	struct completion init_device_done, reload_start_done, resume_done;
104
};
105

106
struct smscore_registry_entry_t {
107 108 109
	struct list_head entry;
	char			devpath[32];
	int				mode;
110 111
	enum sms_device_type_st	type;
};
112 113 114 115 116 117 118 119 120

struct list_head g_smscore_notifyees;
struct list_head g_smscore_devices;
kmutex_t g_smscore_deviceslock;

struct list_head g_smscore_registry;
kmutex_t g_smscore_registrylock;

static int default_mode = 1;
121

122 123 124
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

125
static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
126
{
127
	struct smscore_registry_entry_t *entry;
128 129 130
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);
131 132 133
	for (next = g_smscore_registry.next;
	     next != &g_smscore_registry;
	     next = next->next) {
134
		entry = (struct smscore_registry_entry_t *) next;
135
		if (!strcmp(entry->devpath, devpath)) {
136
			kmutex_unlock(&g_smscore_registrylock);
137
			return entry;
138 139
		}
	}
140 141 142
	entry = (struct smscore_registry_entry_t *)
			kmalloc(sizeof(struct smscore_registry_entry_t),
				GFP_KERNEL);
143
	if (entry) {
144 145 146
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);
		list_add(&entry->entry, &g_smscore_registry);
147 148 149
	} else
		printk(KERN_ERR "%s failed to create smscore_registry.\n",
		       __func__);
150
	kmutex_unlock(&g_smscore_registrylock);
151 152
	return entry;
}
153

154
int smscore_registry_getmode(char *devpath)
155
{
156
	struct smscore_registry_entry_t *entry;
157

158 159
	entry = smscore_find_registry(devpath);
	if (entry)
160 161
		return entry->mode;
	else
162 163
		printk(KERN_ERR "%s No registry found.\n", __func__);

164 165 166
	return default_mode;
}

167
enum sms_device_type_st smscore_registry_gettype(char *devpath)
168
{
169
	struct smscore_registry_entry_t *entry;
170

171 172
	entry = smscore_find_registry(devpath);
	if (entry)
173 174
		return entry->type;
	else
175 176
		printk(KERN_ERR "%s No registry found.\n", __func__);

177 178
	return -1;
}
179

180 181
void smscore_registry_setmode(char *devpath, int mode)
{
182
	struct smscore_registry_entry_t *entry;
183

184 185 186
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
187
	else
188 189
		printk(KERN_ERR "%s No registry found.\n", __func__);
}
190

191
void smscore_registry_settype(char *devpath, enum sms_device_type_st type)
192
{
193
	struct smscore_registry_entry_t *entry;
194

195 196
	entry = smscore_find_registry(devpath);
	if (entry)
197 198
		entry->type = type;
	else
199
		printk(KERN_ERR "%s No registry found.\n", __func__);
200 201 202
}


203
void list_add_locked(struct list_head *new, struct list_head *head,
204
		     spinlock_t *lock)
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
{
	unsigned long flags;

	spin_lock_irqsave(lock, flags);

	list_add(new, head);

	spin_unlock_irqrestore(lock, flags);
}

/**
 * register a client callback that called when device plugged in/unplugged
 * NOTE: if devices exist callback is called immediately for each device
 *
 * @param hotplug callback
 *
 * @return 0 on success, <0 on error.
 */
int smscore_register_hotplug(hotplug_t hotplug)
{
225
	struct smscore_device_notifyee_t *notifyee;
226 227 228 229 230
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

231 232
	notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
			   GFP_KERNEL);
233 234
	if (notifyee) {
		/* now notify callback about existing devices */
235
		first = &g_smscore_devices;
236 237 238
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
239 240
			struct smscore_device_t *coredev =
				(struct smscore_device_t *) next;
241 242 243
			rc = hotplug(coredev, coredev->device, 1);
		}

244
		if (rc >= 0) {
245 246
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
247
		} else
248
			kfree(notifyee);
249
	} else
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
		rc = -ENOMEM;

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

/**
 * unregister a client callback that called when device plugged in/unplugged
 *
 * @param hotplug callback
 *
 */
void smscore_unregister_hotplug(hotplug_t hotplug)
{
	struct list_head *next, *first;

	kmutex_lock(&g_smscore_deviceslock);

	first = &g_smscore_notifyees;

271
	for (next = first->next; next != first;) {
272 273
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *) next;
274 275
		next = next->next;

276
		if (notifyee->hotplug == hotplug) {
277 278 279 280 281 282 283 284
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}

285
void smscore_notify_clients(struct smscore_device_t *coredev)
286
{
287
	struct smscore_client_t *client;
288

289 290
	/* the client must call smscore_unregister_client from remove handler */
	while (!list_empty(&coredev->clients)) {
291
		client = (struct smscore_client_t *) coredev->clients.next;
292 293 294 295
		client->onremove_handler(client->context);
	}
}

296 297
int smscore_notify_callbacks(struct smscore_device_t *coredev,
			     struct device *device, int arrival)
298 299 300 301
{
	struct list_head *next, *first;
	int rc = 0;

302
	/* note: must be called under g_deviceslock */
303 304 305

	first = &g_smscore_notifyees;

306
	for (next = first->next; next != first; next = next->next) {
307
		rc = ((struct smscore_device_notifyee_t *) next)->
308
				hotplug(coredev, device, arrival);
309 310 311 312 313 314 315
		if (rc < 0)
			break;
	}

	return rc;
}

316
struct smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
317
				       dma_addr_t common_buffer_phys)
318
{
319 320
	struct smscore_buffer_t *cb =
		kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
321
	if (!cb) {
322
		printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
323 324 325 326
		return NULL;
	}

	cb->p = buffer;
327
	cb->offset_in_common = buffer - (u8 *) common_buffer;
328 329 330 331 332 333
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
334 335
 * creates coredev object for a device, prepares buffers,
 * creates buffer mappings, notifies registered hotplugs about new device.
336
 *
337 338
 * @param params device pointer to struct with device specific parameters
 *               and handlers
339 340 341 342
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
343 344
int smscore_register_device(struct smsdevice_params_t *params,
			    struct smscore_device_t **coredev)
345
{
346
	struct smscore_device_t *dev;
347 348
	u8 *buffer;

349
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
350
	if (!dev) {
351
		printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
352 353 354
		return -ENOMEM;
	}

355
	/* init list entry so it could be safe in smscore_unregister_device */
356 357
	INIT_LIST_HEAD(&dev->entry);

358
	/* init queues */
359 360 361
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

362
	/* init locks */
363 364 365
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

366
	/* init completion events */
367 368 369 370 371 372 373
	init_completion(&dev->version_ex_done);
	init_completion(&dev->data_download_done);
	init_completion(&dev->trigger_done);
	init_completion(&dev->init_device_done);
	init_completion(&dev->reload_start_done);
	init_completion(&dev->resume_done);

374
	/* alloc common buffer */
375
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
376 377 378 379
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
						&dev->common_buffer_phys,
						GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer) {
380 381 382 383
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

384 385 386
	/* prepare dma buffers */
	for (buffer = dev->common_buffer;
	     dev->num_buffers < params->num_buffers;
387
	     dev->num_buffers++, buffer += params->buffer_size) {
388
		struct smscore_buffer_t *cb =
389 390
			smscore_createbuffer(buffer, dev->common_buffer,
					     dev->common_buffer_phys);
391
		if (!cb) {
392 393 394 395 396 397 398
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

399 400
	printk(KERN_INFO "%s allocated %d buffers\n",
	       __func__, dev->num_buffers);
401 402 403 404 405 406 407 408 409 410 411 412 413

	dev->mode = DEVICE_MODE_NONE;
	dev->context = params->context;
	dev->device = params->device;
	dev->setmode_handler = params->setmode_handler;
	dev->detectmode_handler = params->detectmode_handler;
	dev->sendrequest_handler = params->sendrequest_handler;
	dev->preload_handler = params->preload_handler;
	dev->postload_handler = params->postload_handler;

	dev->device_flags = params->flags;
	strcpy(dev->devpath, params->devpath);

414
	smscore_registry_settype(dev->devpath, params->device_type);
415

416
	/* add device to devices list */
417 418 419 420 421 422
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

423
	printk(KERN_INFO "%s device %p created\n", __func__, dev);
424 425 426 427 428 429 430

	return 0;
}

/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
431 432
 * @param coredev pointer to a coredev object returned by
 * 		  smscore_register_device
433 434 435
 *
 * @return 0 on success, <0 on error.
 */
436
int smscore_start_device(struct smscore_device_t *coredev)
437
{
438 439
	int rc = smscore_set_device_mode(
			coredev, smscore_registry_getmode(coredev->devpath));
440
	if (rc < 0) {
441 442
		printk(KERN_INFO "%s set device mode faile , rc %d\n",
		       __func__, rc);
443
		return rc;
444
	}
445 446 447 448 449

	kmutex_lock(&g_smscore_deviceslock);

	rc = smscore_notify_callbacks(coredev, coredev->device, 1);

450 451
	printk(KERN_INFO "%s device %p started, rc %d\n",
	       __func__, coredev, rc);
452 453 454 455 456 457

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

458
int smscore_sendrequest_and_wait(struct smscore_device_t *coredev, void *buffer,
459
				 size_t size, struct completion *completion)
460 461
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
462 463 464
	if (rc < 0) {
		printk(KERN_INFO "%s sendrequest returned error %d\n",
		       __func__, rc);
465
		return rc;
466
	}
467

468 469 470
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(10000)) ?
						0 : -ETIME;
471 472
}

473 474
int smscore_load_firmware_family2(struct smscore_device_t *coredev,
				  void *buffer, size_t size)
475
{
476 477
	struct SmsFirmware_ST *firmware = (struct SmsFirmware_ST *) buffer;
	struct SmsMsgHdr_ST *msg;
478
	u32 mem_address = firmware->StartAddress;
479
	u8 *payload = firmware->Payload;
480 481
	int rc = 0;

482 483 484
	printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n",
	       __func__, mem_address, firmware->Length);
	if (coredev->preload_handler) {
485 486 487 488 489
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

490
	/* PAGE_SIZE buffer shall be enough and dma aligned */
491
	msg = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
492 493 494
	if (!msg)
		return -ENOMEM;

495
	if (coredev->mode != DEVICE_MODE_NONE) {
496
		PDEBUG("Sending reload command\n");
497
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
498
			     sizeof(struct SmsMsgHdr_ST));
499 500 501
		rc = smscore_sendrequest_and_wait(coredev, msg,
						  msg->msgLength,
						  &coredev->reload_start_done);
502
		mem_address = *(u32 *) &payload[20];
503 504
	}

505
	while (size && rc >= 0) {
506 507
		struct SmsDataDownload_ST *DataMsg =
			(struct SmsDataDownload_ST *) msg;
508 509
		int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);

510
		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
511
			     (u16)(sizeof(struct SmsMsgHdr_ST) +
512
				      sizeof(u32) + payload_size));
513 514 515 516

		DataMsg->MemAddr = mem_address;
		memcpy(DataMsg->Payload, payload, payload_size);

517 518
		if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
		    (coredev->mode == DEVICE_MODE_NONE))
519 520 521
			rc = coredev->sendrequest_handler(
				coredev->context, DataMsg,
				DataMsg->xMsgHeader.msgLength);
522
		else
523 524 525 526
			rc = smscore_sendrequest_and_wait(
				coredev, DataMsg,
				DataMsg->xMsgHeader.msgLength,
				&coredev->data_download_done);
527 528 529 530 531 532

		payload += payload_size;
		size -= payload_size;
		mem_address += payload_size;
	}

533 534
	if (rc >= 0) {
		if (coredev->mode == DEVICE_MODE_NONE) {
535 536
			struct SmsMsgData_ST *TriggerMsg =
				(struct SmsMsgData_ST *) msg;
537

538
			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
539
				     sizeof(struct SmsMsgHdr_ST) +
540
				     sizeof(u32) * 5);
541

542 543
			TriggerMsg->msgData[0] = firmware->StartAddress;
						/* Entry point */
544 545 546 547
			TriggerMsg->msgData[1] = 5; /* Priority */
			TriggerMsg->msgData[2] = 0x200; /* Stack size */
			TriggerMsg->msgData[3] = 0; /* Parameter */
			TriggerMsg->msgData[4] = 4; /* Task ID */
548

549
			if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
550 551 552
				rc = coredev->sendrequest_handler(
					coredev->context, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength);
553
				msleep(100);
554
			} else
555 556 557 558
				rc = smscore_sendrequest_and_wait(
					coredev, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength,
					&coredev->trigger_done);
559 560
		} else {
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
561
				     sizeof(struct SmsMsgHdr_ST));
562

563 564
			rc = coredev->sendrequest_handler(coredev->context,
							  msg, msg->msgLength);
565
		}
566
		msleep(500);
567 568
	}

569
	printk(KERN_DEBUG "%s rc=%d, postload=%p \n", __func__, rc,
570
	       coredev->postload_handler);
571 572 573

	kfree(msg);

574
	return ((rc >= 0) && coredev->postload_handler) ?
575 576 577 578 579 580 581
		coredev->postload_handler(coredev->context) :
		rc;
}

/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
582 583
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
584 585 586 587 588
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
589 590
int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
				    char *filename,
591
				    loadfirmware_t loadfirmware_handler)
592 593 594
{
	int rc = -ENOENT;
	const struct firmware *fw;
595
	u8 *fw_buffer;
596

597 598
	if (loadfirmware_handler == NULL && !(coredev->device_flags &
					      SMS_DEVICE_FAMILY2))
599 600 601
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
602 603 604
	if (rc < 0) {
		printk(KERN_INFO "%s failed to open \"%s\"\n",
		       __func__, filename);
605 606
		return rc;
	}
607 608 609 610 611
	printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
	       filename, fw->size);
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
612 613 614
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
615 616 617 618 619
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
620 621

		kfree(fw_buffer);
622 623 624
	} else {
		printk(KERN_INFO "%s failed to allocate firmware buffer\n",
		       __func__);
625 626 627 628 629 630 631 632
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

633 634
int smscore_load_firmware_from_buffer(struct smscore_device_t *coredev,
				      u8 *buffer, int size, int new_mode)
635 636 637 638 639
{
	PERROR("Feature not implemented yet\n");
	return -EFAULT;
}

640
/**
641 642
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
643
 *
644 645
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
646 647 648
 *
 * @return 0 on success, <0 on error.
 */
649
void smscore_unregister_device(struct smscore_device_t *coredev)
650
{
651
	struct smscore_buffer_t *cb;
652
	int num_buffers = 0;
653
	int retry = 0;
654 655 656 657 658 659

	kmutex_lock(&g_smscore_deviceslock);

	smscore_notify_clients(coredev);
	smscore_notify_callbacks(coredev, NULL, 0);

660 661
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
662

663 664
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
665
			kfree(cb);
666
			num_buffers++;
667 668 669
		}
		if (num_buffers == coredev->num_buffers)
			break;
670 671 672
		if (++retry > 10) {
			printk(KERN_INFO "%s exiting although "
			       "not all buffers released.\n", __func__);
673 674
			break;
		}
675

676 677
		printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
		       coredev->num_buffers - num_buffers);
678 679 680
		msleep(100);
	}

681
	printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
682 683

	if (coredev->common_buffer)
684 685 686
		dma_free_coherent(NULL, coredev->common_buffer_size,
				  coredev->common_buffer,
				  coredev->common_buffer_phys);
687 688 689 690 691 692

	list_del(&coredev->entry);
	kfree(coredev);

	kmutex_unlock(&g_smscore_deviceslock);

693
	printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
694 695
}

696
int smscore_detect_mode(struct smscore_device_t *coredev)
697
{
698
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
699
			       GFP_KERNEL | GFP_DMA);
700 701
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
702 703 704 705 706
	int rc;

	if (!buffer)
		return -ENOMEM;

707 708
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
709

710 711 712
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
713 714
		printk(KERN_ERR "%s: MSG_SMS_GET_VERSION_EX_REQ "
		       "failed first try\n", __func__);
715

716 717
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
718 719 720
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
721
			if (rc < 0)
722 723
				printk(KERN_ERR "%s: "
				       "MSG_SMS_GET_VERSION_EX_REQ failed "
724
				       "second try, rc %d\n", __func__, rc);
725
		} else
726 727 728 729 730 731 732 733
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

734
char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
	/*Stellar		NOVA A0		Nova B0		VEGA*/
	/*DVBT*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*DVBH*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*TDMB*/
	{"none", "tdmb_nova_12mhz.inp", "none", "none"},
	/*DABIP*/
	{"none", "none", "none", "none"},
	/*BDA*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*ISDBT*/
	{"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
	/*ISDBTBDA*/
	{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
	/*CMMB*/
	{"none", "none", "none", "cmmb_vega_12mhz.inp"}
752 753
};

754

755 756 757 758
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
759 760
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
761 762 763 764
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
765
int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
766 767 768
{
	void *buffer;
	int rc = 0;
769
	enum sms_device_type_st type;
770

771 772 773 774 775
	PDEBUG("set device mode to %d\n", mode);
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
			printk(KERN_INFO "%s invalid mode specified %d\n",
			       __func__, mode);
776 777 778
			return -EINVAL;
		}

779 780
		smscore_registry_setmode(coredev->devpath, mode);

781
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
782
			rc = smscore_detect_mode(coredev);
783 784 785
			if (rc < 0) {
				printk(KERN_INFO "%s mode detect failed %d\n",
				       __func__, rc);
786
				return rc;
787
			}
788
		}
789

790 791 792
		if (coredev->mode == mode) {
			printk(KERN_INFO "%s device mode %d already set\n",
			       __func__, mode);
793 794 795
			return 0;
		}

796 797
		if (!(coredev->modes_supported & (1 << mode))) {
			type = smscore_registry_gettype(coredev->devpath);
798 799
			rc = smscore_load_firmware_from_file(
				coredev, smscore_fw_lkup[mode][type], NULL);
800
			if (rc < 0) {
801 802
				printk(KERN_INFO "%s load firmware "
				       "failed %d\n", __func__, rc);
803
				return rc;
804 805
			}
		} else
806 807
			printk(KERN_INFO "%s mode %d supported by running "
			       "firmware\n", __func__, mode);
808

809 810
		buffer = kmalloc(sizeof(struct SmsMsgData_ST) +
				 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
811
		if (buffer) {
812 813 814
			struct SmsMsgData_ST *msg =
				(struct SmsMsgData_ST *)
					SMS_ALIGN_ADDRESS(buffer);
815

816
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
817
				     sizeof(struct SmsMsgData_ST));
818 819
			msg->msgData[0] = mode;

820 821 822
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
823 824

			kfree(buffer);
825
		} else {
826 827
			printk(KERN_INFO "%s Could not allocate buffer for "
			       "init device message.\n", __func__);
828
			rc = -ENOMEM;
829 830 831 832 833
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
			printk(KERN_INFO "%s invalid mode specified %d\n",
			       __func__, mode);
834 835 836 837 838
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

839
		if (coredev->detectmode_handler)
840 841
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
842 843 844 845 846

		if (coredev->mode != mode && coredev->setmode_handler)
			rc = coredev->setmode_handler(coredev->context, mode);
	}

847
	if (rc >= 0) {
848 849 850 851
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

852 853
	if (rc != 0)
		printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
854 855 856 857 858 859
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
860 861
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
862 863 864
 *
 * @return current mode
 */
865
int smscore_get_device_mode(struct smscore_device_t *coredev)
866 867 868 869
{
	return coredev->mode;
}

870 871 872 873
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
874 875
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
876
 * @param data_type client data type (SMS_DONT_CARE for all types)
877
 * @param id client id (SMS_DONT_CARE for all id)
878 879
 *
 */
880
struct smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
881
				      int data_type, int id)
882
{
883
	struct smscore_client_t *client = NULL;
884 885
	struct list_head *next, *first;
	unsigned long flags;
886
	struct list_head *firstid, *nextid;
887 888 889 890


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
891 892 893
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
894
		firstid = &((struct smscore_client_t *)next)->idlist;
895 896 897
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
898 899 900 901
			if ((((struct smscore_idlist_t *)nextid)->id == id) &&
			    (((struct smscore_idlist_t *)nextid)->data_type == data_type ||
			    (((struct smscore_idlist_t *)nextid)->data_type == 0))) {
				client = (struct smscore_client_t *) next;
902 903
				break;
			}
904 905 906 907 908 909 910 911 912 913
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
914 915
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
916 917 918
 * @param cb pointer to response buffer descriptor
 *
 */
919 920
void smscore_onresponse(struct smscore_device_t *coredev,
			struct smscore_buffer_t *cb)
921
{
922 923 924
	struct SmsMsgHdr_ST *phdr =
		(struct SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
	struct smscore_client_t *client =
925
		smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
926 927
	int rc = -EBUSY;

928 929
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
930 931 932 933 934
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

935
	if (time_now - last_sample_time > 10000) {
936
		printk(KERN_DEBUG "\n%s data rate %d bytes/secs\n", __func__,
937 938
		       (int)((data_total * 1000) /
			     (time_now - last_sample_time)));
939 940 941 942 943 944

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
945 946
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
947 948 949
	if (client)
		rc = client->onresponse_handler(client->context, cb);

950 951 952
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
953
		{
954 955
			struct SmsVersionRes_ST *ver =
				(struct SmsVersionRes_ST *) phdr;
956 957
			printk(KERN_DEBUG "%s: MSG_SMS_GET_VERSION_EX_RES "
			       "id %d prots 0x%x ver %d.%d\n", __func__,
958 959
			       ver->FirmwareId, ver->SupportedProtocols,
			       ver->RomVersionMajor, ver->RomVersionMinor);
960

961 962 963
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
964

965 966 967 968
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
969 970
			printk(KERN_DEBUG "%s: MSG_SMS_INIT_DEVICE_RES\n",
			       __func__);
971 972 973
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
974 975
			printk(KERN_DEBUG "%s: MSG_SW_RELOAD_START_RES\n",
			       __func__);
976 977 978 979 980 981
			complete(&coredev->reload_start_done);
			break;
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
982 983
			printk(KERN_DEBUG "%s: MSG_SW_RELOAD_EXEC_RES\n",
			       __func__);
984 985
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
986 987
			printk(KERN_DEBUG
			       "%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
988 989 990 991 992 993 994 995
			       __func__);
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
		default:
			break;
996 997 998 999 1000 1001 1002 1003
		}
		smscore_putbuffer(coredev, cb);
	}
}

/**
 * return pointer to next free buffer descriptor from core pool
 *
1004 1005
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1006 1007 1008
 *
 * @return pointer to descriptor on success, NULL on error.
 */
1009
struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1010
{
1011
	struct smscore_buffer_t *cb = NULL;
1012 1013 1014 1015
	unsigned long flags;

	spin_lock_irqsave(&coredev->bufferslock, flags);

1016
	if (!list_empty(&coredev->buffers)) {
1017
		cb = (struct smscore_buffer_t *) coredev->buffers.next;
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
		list_del(&cb->entry);
	}

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}

/**
 * return buffer descriptor to a pool
 *
1029 1030
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1031 1032 1033
 * @param cb pointer buffer descriptor
 *
 */
1034 1035
void smscore_putbuffer(struct smscore_device_t *coredev,
		       struct smscore_buffer_t *cb)
1036 1037 1038 1039
{
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}

1040 1041 1042
int smscore_validate_client(struct smscore_device_t *coredev,
			    struct smscore_client_t *client,
			    int data_type, int id)
1043
{
1044 1045
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1046

1047
	if (!client) {
1048 1049 1050 1051
		PERROR("bad parameter.\n");
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1052
	if (registered_client == client)
1053
		return 0;
1054

1055
	if (registered_client) {
1056 1057 1058
		PERROR("The msg ID already registered to another client.\n");
		return -EEXIST;
	}
1059
	listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
1060
	if (!listentry) {
1061
		PERROR("Can't allocate memory for client id.\n");
1062
		return -ENOMEM;
1063 1064 1065
	}
	listentry->id = id;
	listentry->data_type = data_type;
1066 1067
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1068 1069 1070 1071 1072 1073 1074 1075 1076
	return 0;
}

/**
 * creates smsclient object, check that id is taken by another client
 *
 * @param coredev pointer to a coredev object from clients hotplug
 * @param initial_id all messages with this id would be sent to this client
 * @param data_type all messages of this type would be sent to this client
1077 1078
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1079 1080 1081 1082 1083 1084
 * @param onremove_handler client handler that is called when device is removed
 * @param context client-specific context
 * @param client pointer to a value that receives created smsclient object
 *
 * @return 0 on success, <0 on error.
 */
1085 1086 1087
int smscore_register_client(struct smscore_device_t *coredev,
			    struct smsclient_params_t *params,
			    struct smscore_client_t **client)
1088
{
1089
	struct smscore_client_t *newclient;
1090 1091 1092
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1093
		PERROR("Client already exist.\n");
1094
		return -EEXIST;
1095
	}
1096

1097
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1098
	if (!newclient) {
1099 1100
		PERROR("Failed to allocate memory for client.\n");
		return -ENOMEM;
1101 1102
	}

1103
	INIT_LIST_HEAD(&newclient->idlist);
1104 1105 1106 1107
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1108 1109 1110 1111
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1112
	*client = newclient;
1113 1114
	PDEBUG("%p %d %d\n", params->context, params->data_type,
	       params->initial_id);
1115 1116 1117 1118 1119 1120 1121

	return 0;
}

/**
 * frees smsclient object and all subclients associated with it
 *
1122 1123
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1124 1125
 *
 */
1126
void smscore_unregister_client(struct smscore_client_t *client)
1127
{
1128
	struct smscore_device_t *coredev = client->coredev;
1129 1130 1131 1132 1133
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1134
	while (!list_empty(&client->idlist)) {
1135 1136
		struct smscore_idlist_t *identry =
			(struct smscore_idlist_t *) client->idlist.next;
1137 1138
		list_del(&identry->entry);
		kfree(identry);
1139 1140
	}

1141
	printk(KERN_INFO "%s %p\n", __func__, client->context);
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152

	list_del(&client->entry);
	kfree(client);

	spin_unlock_irqrestore(&coredev->clientslock, flags);
}

/**
 * verifies that source id is not taken by another client,
 * calls device handler to send requests to the device
 *
1153 1154
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1155 1156 1157 1158 1159
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1160 1161
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1162
{
1163 1164
	struct smscore_device_t *coredev;
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
1165 1166
	int rc;

1167 1168
	if (client == NULL) {
		printk(KERN_ERR "%s Got NULL client\n", __func__);
1169 1170 1171 1172
		return -EINVAL;
	}

	coredev = client->coredev;
1173

1174 1175 1176
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
		printk(KERN_ERR "%s Got NULL coredev\n", __func__);
1177 1178 1179
		return -EINVAL;
	}

1180 1181
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
	if (rc < 0)
		return rc;

	return coredev->sendrequest_handler(coredev->context, buffer, size);
}

/**
 * return the size of large (common) buffer
 *
 * @param coredev pointer to a coredev object from clients hotplug
 *
 * @return size (in bytes) of the buffer
 */
1195
int smscore_get_common_buffer_size(struct smscore_device_t *coredev)
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
{
	return coredev->common_buffer_size;
}

/**
 * maps common buffer (if supported by platform)
 *
 * @param coredev pointer to a coredev object from clients hotplug
 * @param vma pointer to vma struct from mmap handler
 *
 * @return 0 on success, <0 on error.
 */
1208
int smscore_map_common_buffer(struct smscore_device_t *coredev,
1209
			       struct vm_area_struct *vma)
1210
{
1211 1212 1213
	unsigned long end = vma->vm_end,
		      start = vma->vm_start,
		      size = PAGE_ALIGN(coredev->common_buffer_size);
1214

1215 1216
	if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
	     (vma->vm_flags & VM_WRITE)) {
1217
		printk(KERN_INFO "%s invalid vm flags\n", __func__);
1218 1219 1220
		return -EINVAL;
	}

1221 1222 1223
	if ((end - start) != size) {
		printk(KERN_INFO "%s invalid size %d expected %d\n",
		       __func__, (int)(end - start), (int) size);
1224 1225 1226
		return -EINVAL;
	}

1227 1228
	if (remap_pfn_range(vma, start,
			    coredev->common_buffer_phys >> PAGE_SHIFT,
1229
			    size, pgprot_noncached(vma->vm_page_prot))) {
1230
		printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1231 1232 1233 1234 1235 1236 1237 1238
		return -EAGAIN;
	}

	return 0;
}

int smscore_module_init(void)
{
1239
	int rc = 0;
1240 1241 1242 1243 1244 1245 1246 1247

	INIT_LIST_HEAD(&g_smscore_notifyees);
	INIT_LIST_HEAD(&g_smscore_devices);
	kmutex_init(&g_smscore_deviceslock);

	INIT_LIST_HEAD(&g_smscore_registry);
	kmutex_init(&g_smscore_registrylock);

1248 1249 1250 1251 1252 1253
	/* USB Register */
	rc = smsusb_register();

	/* DVB Register */
	rc = smsdvb_register();

1254
	printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1255 1256 1257 1258 1259 1260

	return rc;
}

void smscore_module_exit(void)
{
1261

1262
	kmutex_lock(&g_smscore_deviceslock);
1263
	while (!list_empty(&g_smscore_notifyees)) {
1264 1265 1266
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
1267 1268 1269 1270 1271 1272 1273

		list_del(&notifyee->entry);
		kfree(notifyee);
	}
	kmutex_unlock(&g_smscore_deviceslock);

	kmutex_lock(&g_smscore_registrylock);
1274
	while (!list_empty(&g_smscore_registry)) {
1275 1276 1277
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
1278 1279 1280 1281 1282 1283

		list_del(&entry->entry);
		kfree(entry);
	}
	kmutex_unlock(&g_smscore_registrylock);

1284 1285 1286 1287 1288 1289
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1290
	printk(KERN_INFO "%s\n", __func__);
1291 1292 1293 1294 1295 1296
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

MODULE_DESCRIPTION("smscore");
1297
MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1298
MODULE_LICENSE("GPL");