smscoreapi.c 32.6 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 39
int sms_debug;
module_param_named(debug, sms_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");

40
#define PERROR(fmt, args...)\
41
	sms_err("smscore error: line %d- %s(): " fmt, \
42
		__LINE__,  __func__, ## args)
43 44 45

#ifdef SMSCORE_DEBUG
#undef PWARNING
46
#  define PWARNING(fmt, args...) sms_info("smscore warning: " \
47 48
					"line %d- %s(): " fmt, \
					__LINE__, __func__, ## args)
49
#undef PDEBUG					/* undef it, just in case */
50
#  define PDEBUG(fmt, args...)   sms_info("smscore - %s(): " fmt, \
51
					__func__, ## args)
52 53 54 55 56
#else /*SMSCORE_DEBUG*/
#define PDEBUG(fmt, args...)
#define PWARNING(fmt, args...)
#endif

57
struct smscore_device_notifyee_t {
58 59
	struct list_head entry;
	hotplug_t hotplug;
60
};
61

62
struct smscore_idlist_t {
63 64 65
	struct list_head entry;
	int		id;
	int		data_type;
66
};
67

68
struct smscore_client_t {
69
	struct list_head entry;
70
	struct smscore_device_t *coredev;
71
	void			*context;
72
	struct list_head 	idlist;
73 74
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
75
};
76

77
struct smscore_device_t {
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 104 105 106 107
	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;
108 109

	int board_id;
110
};
111

112 113 114 115 116 117 118 119 120 121
void smscore_set_board_id(struct smscore_device_t *core, int id)
{
	core->board_id = id;
}

int smscore_get_board_id(struct smscore_device_t *core)
{
	return core->board_id;
}

122
struct smscore_registry_entry_t {
123 124 125
	struct list_head entry;
	char			devpath[32];
	int				mode;
126 127
	enum sms_device_type_st	type;
};
128 129 130 131 132 133 134 135

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;

136
static int default_mode = 4;
137

138 139 140
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

141
static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
142
{
143
	struct smscore_registry_entry_t *entry;
144 145 146
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);
147 148 149
	for (next = g_smscore_registry.next;
	     next != &g_smscore_registry;
	     next = next->next) {
150
		entry = (struct smscore_registry_entry_t *) next;
151
		if (!strcmp(entry->devpath, devpath)) {
152
			kmutex_unlock(&g_smscore_registrylock);
153
			return entry;
154 155
		}
	}
156 157 158
	entry = (struct smscore_registry_entry_t *)
			kmalloc(sizeof(struct smscore_registry_entry_t),
				GFP_KERNEL);
159
	if (entry) {
160 161 162
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);
		list_add(&entry->entry, &g_smscore_registry);
163
	} else
164
		sms_err("failed to create smscore_registry.");
165
	kmutex_unlock(&g_smscore_registrylock);
166 167
	return entry;
}
168

169
int smscore_registry_getmode(char *devpath)
170
{
171
	struct smscore_registry_entry_t *entry;
172

173 174
	entry = smscore_find_registry(devpath);
	if (entry)
175 176
		return entry->mode;
	else
177
		sms_err("No registry found.");
178

179 180 181
	return default_mode;
}

182
enum sms_device_type_st smscore_registry_gettype(char *devpath)
183
{
184
	struct smscore_registry_entry_t *entry;
185

186 187
	entry = smscore_find_registry(devpath);
	if (entry)
188 189
		return entry->type;
	else
190
		sms_err("No registry found.");
191

192 193
	return -1;
}
194

195 196
void smscore_registry_setmode(char *devpath, int mode)
{
197
	struct smscore_registry_entry_t *entry;
198

199 200 201
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
202
	else
203
		sms_err("No registry found.");
204
}
205

206
void smscore_registry_settype(char *devpath, enum sms_device_type_st type)
207
{
208
	struct smscore_registry_entry_t *entry;
209

210 211
	entry = smscore_find_registry(devpath);
	if (entry)
212 213
		entry->type = type;
	else
214
		sms_err("No registry found.");
215 216 217
}


218
void list_add_locked(struct list_head *new, struct list_head *head,
219
		     spinlock_t *lock)
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
{
	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)
{
240
	struct smscore_device_notifyee_t *notifyee;
241 242 243 244 245
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

246 247
	notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
			   GFP_KERNEL);
248 249
	if (notifyee) {
		/* now notify callback about existing devices */
250
		first = &g_smscore_devices;
251 252 253
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
254 255
			struct smscore_device_t *coredev =
				(struct smscore_device_t *) next;
256 257 258
			rc = hotplug(coredev, coredev->device, 1);
		}

259
		if (rc >= 0) {
260 261
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
262
		} else
263
			kfree(notifyee);
264
	} else
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		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;

286
	for (next = first->next; next != first;) {
287 288
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *) next;
289 290
		next = next->next;

291
		if (notifyee->hotplug == hotplug) {
292 293 294 295 296 297 298 299
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}

300
void smscore_notify_clients(struct smscore_device_t *coredev)
301
{
302
	struct smscore_client_t *client;
303

304 305
	/* the client must call smscore_unregister_client from remove handler */
	while (!list_empty(&coredev->clients)) {
306
		client = (struct smscore_client_t *) coredev->clients.next;
307 308 309 310
		client->onremove_handler(client->context);
	}
}

311 312
int smscore_notify_callbacks(struct smscore_device_t *coredev,
			     struct device *device, int arrival)
313 314 315 316
{
	struct list_head *next, *first;
	int rc = 0;

317
	/* note: must be called under g_deviceslock */
318 319 320

	first = &g_smscore_notifyees;

321
	for (next = first->next; next != first; next = next->next) {
322
		rc = ((struct smscore_device_notifyee_t *) next)->
323
				hotplug(coredev, device, arrival);
324 325 326 327 328 329 330
		if (rc < 0)
			break;
	}

	return rc;
}

331
struct smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
332
				       dma_addr_t common_buffer_phys)
333
{
334 335
	struct smscore_buffer_t *cb =
		kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
336
	if (!cb) {
337
		sms_info("kmalloc(...) failed");
338 339 340 341
		return NULL;
	}

	cb->p = buffer;
342
	cb->offset_in_common = buffer - (u8 *) common_buffer;
343 344 345 346 347 348
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
349 350
 * creates coredev object for a device, prepares buffers,
 * creates buffer mappings, notifies registered hotplugs about new device.
351
 *
352 353
 * @param params device pointer to struct with device specific parameters
 *               and handlers
354 355 356 357
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
358 359
int smscore_register_device(struct smsdevice_params_t *params,
			    struct smscore_device_t **coredev)
360
{
361
	struct smscore_device_t *dev;
362 363
	u8 *buffer;

364
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
365
	if (!dev) {
366
		sms_info("kzalloc(...) failed");
367 368 369
		return -ENOMEM;
	}

370
	/* init list entry so it could be safe in smscore_unregister_device */
371 372
	INIT_LIST_HEAD(&dev->entry);

373
	/* init queues */
374 375 376
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

377
	/* init locks */
378 379 380
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

381
	/* init completion events */
382 383 384 385 386 387 388
	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);

389
	/* alloc common buffer */
390
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
391 392 393 394
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
						&dev->common_buffer_phys,
						GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer) {
395 396 397 398
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

399 400 401
	/* prepare dma buffers */
	for (buffer = dev->common_buffer;
	     dev->num_buffers < params->num_buffers;
402
	     dev->num_buffers++, buffer += params->buffer_size) {
403
		struct smscore_buffer_t *cb =
404 405
			smscore_createbuffer(buffer, dev->common_buffer,
					     dev->common_buffer_phys);
406
		if (!cb) {
407 408 409 410 411 412 413
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

414
	sms_info("allocated %d buffers", dev->num_buffers);
415 416 417 418 419 420 421 422 423 424 425 426 427

	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);

428
	smscore_registry_settype(dev->devpath, params->device_type);
429

430
	/* add device to devices list */
431 432 433 434 435 436
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

437
	sms_info("device %p created", dev);
438 439 440 441 442 443 444

	return 0;
}

/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
445 446
 * @param coredev pointer to a coredev object returned by
 * 		  smscore_register_device
447 448 449
 *
 * @return 0 on success, <0 on error.
 */
450
int smscore_start_device(struct smscore_device_t *coredev)
451
{
452 453
	int rc = smscore_set_device_mode(
			coredev, smscore_registry_getmode(coredev->devpath));
454
	if (rc < 0) {
455
		sms_info("set device mode faile , rc %d", rc);
456
		return rc;
457
	}
458 459 460 461 462

	kmutex_lock(&g_smscore_deviceslock);

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

463
	sms_info("device %p started, rc %d", coredev, rc);
464 465 466 467 468 469

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

470
int smscore_sendrequest_and_wait(struct smscore_device_t *coredev, void *buffer,
471
				 size_t size, struct completion *completion)
472 473
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
474
	if (rc < 0) {
475
		sms_info("sendrequest returned error %d", rc);
476
		return rc;
477
	}
478

479 480 481
	return wait_for_completion_timeout(completion,
					   msecs_to_jiffies(10000)) ?
						0 : -ETIME;
482 483
}

484 485
int smscore_load_firmware_family2(struct smscore_device_t *coredev,
				  void *buffer, size_t size)
486
{
487 488
	struct SmsFirmware_ST *firmware = (struct SmsFirmware_ST *) buffer;
	struct SmsMsgHdr_ST *msg;
489
	u32 mem_address = firmware->StartAddress;
490
	u8 *payload = firmware->Payload;
491 492
	int rc = 0;

493 494
	sms_info("loading FW to addr 0x%x size %d",
		 mem_address, firmware->Length);
495
	if (coredev->preload_handler) {
496 497 498 499 500
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

501
	/* PAGE_SIZE buffer shall be enough and dma aligned */
502
	msg = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
503 504 505
	if (!msg)
		return -ENOMEM;

506
	if (coredev->mode != DEVICE_MODE_NONE) {
507
		PDEBUG("Sending reload command");
508
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
509
			     sizeof(struct SmsMsgHdr_ST));
510 511 512
		rc = smscore_sendrequest_and_wait(coredev, msg,
						  msg->msgLength,
						  &coredev->reload_start_done);
513
		mem_address = *(u32 *) &payload[20];
514 515
	}

516
	while (size && rc >= 0) {
517 518
		struct SmsDataDownload_ST *DataMsg =
			(struct SmsDataDownload_ST *) msg;
519 520
		int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);

521
		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
522
			     (u16)(sizeof(struct SmsMsgHdr_ST) +
523
				      sizeof(u32) + payload_size));
524 525 526 527

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

528 529
		if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
		    (coredev->mode == DEVICE_MODE_NONE))
530 531 532
			rc = coredev->sendrequest_handler(
				coredev->context, DataMsg,
				DataMsg->xMsgHeader.msgLength);
533
		else
534 535 536 537
			rc = smscore_sendrequest_and_wait(
				coredev, DataMsg,
				DataMsg->xMsgHeader.msgLength,
				&coredev->data_download_done);
538 539 540 541 542 543

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

544 545
	if (rc >= 0) {
		if (coredev->mode == DEVICE_MODE_NONE) {
546 547
			struct SmsMsgData_ST *TriggerMsg =
				(struct SmsMsgData_ST *) msg;
548

549
			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
550
				     sizeof(struct SmsMsgHdr_ST) +
551
				     sizeof(u32) * 5);
552

553 554
			TriggerMsg->msgData[0] = firmware->StartAddress;
						/* Entry point */
555 556 557 558
			TriggerMsg->msgData[1] = 5; /* Priority */
			TriggerMsg->msgData[2] = 0x200; /* Stack size */
			TriggerMsg->msgData[3] = 0; /* Parameter */
			TriggerMsg->msgData[4] = 4; /* Task ID */
559

560
			if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
561 562 563
				rc = coredev->sendrequest_handler(
					coredev->context, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength);
564
				msleep(100);
565
			} else
566 567 568 569
				rc = smscore_sendrequest_and_wait(
					coredev, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength,
					&coredev->trigger_done);
570 571
		} else {
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
572
				     sizeof(struct SmsMsgHdr_ST));
573

574 575
			rc = coredev->sendrequest_handler(coredev->context,
							  msg, msg->msgLength);
576
		}
577
		msleep(500);
578 579
	}

580
	sms_debug("rc=%d, postload=%p ", rc,
581
		  coredev->postload_handler);
582 583 584

	kfree(msg);

585
	return ((rc >= 0) && coredev->postload_handler) ?
586 587 588 589 590 591 592
		coredev->postload_handler(coredev->context) :
		rc;
}

/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
593 594
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
595 596 597 598 599
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
600 601
int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
				    char *filename,
602
				    loadfirmware_t loadfirmware_handler)
603 604 605
{
	int rc = -ENOENT;
	const struct firmware *fw;
606
	u8 *fw_buffer;
607

608 609
	if (loadfirmware_handler == NULL && !(coredev->device_flags &
					      SMS_DEVICE_FAMILY2))
610 611 612
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
613
	if (rc < 0) {
614
		sms_info("failed to open \"%s\"", filename);
615 616
		return rc;
	}
617
	sms_info("read FW %s, size=%d\"", filename, fw->size);
618 619 620
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
621 622 623
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
624 625 626 627 628
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
629 630

		kfree(fw_buffer);
631
	} else {
632
		sms_info("failed to allocate firmware buffer");
633 634 635 636 637 638 639 640
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

641 642
int smscore_load_firmware_from_buffer(struct smscore_device_t *coredev,
				      u8 *buffer, int size, int new_mode)
643
{
644
	PERROR("Feature not implemented yet");
645 646 647
	return -EFAULT;
}

648
/**
649 650
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
651
 *
652 653
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
654 655 656
 *
 * @return 0 on success, <0 on error.
 */
657
void smscore_unregister_device(struct smscore_device_t *coredev)
658
{
659
	struct smscore_buffer_t *cb;
660
	int num_buffers = 0;
661
	int retry = 0;
662 663 664 665 666 667

	kmutex_lock(&g_smscore_deviceslock);

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

668 669
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
670

671 672
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
673
			kfree(cb);
674
			num_buffers++;
675 676 677
		}
		if (num_buffers == coredev->num_buffers)
			break;
678
		if (++retry > 10) {
679 680
			sms_info("exiting although "
				 "not all buffers released.");
681 682
			break;
		}
683

684
		sms_info("waiting for %d buffer(s)",
685
			 coredev->num_buffers - num_buffers);
686 687 688
		msleep(100);
	}

689
	sms_info("freed %d buffers", num_buffers);
690 691

	if (coredev->common_buffer)
692 693 694
		dma_free_coherent(NULL, coredev->common_buffer_size,
				  coredev->common_buffer,
				  coredev->common_buffer_phys);
695 696 697 698 699 700

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

	kmutex_unlock(&g_smscore_deviceslock);

701
	sms_info("device %p destroyed", coredev);
702 703
}

704
int smscore_detect_mode(struct smscore_device_t *coredev)
705
{
706
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
707
			       GFP_KERNEL | GFP_DMA);
708 709
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
710 711 712 713 714
	int rc;

	if (!buffer)
		return -ENOMEM;

715 716
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
717

718 719 720
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
721
		sms_err("MSG_SMS_GET_VERSION_EX_REQ failed first try");
722

723 724
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
725 726 727
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
728
			if (rc < 0)
729 730
				sms_err("MSG_SMS_GET_VERSION_EX_REQ failed "
					"second try, rc %d", rc);
731
		} else
732 733 734 735 736 737 738 739
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

740
char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
	/*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"}
758 759
};

760

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

777
	PDEBUG("set device mode to %d", mode);
778 779
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
780
			sms_info("invalid mode specified %d", mode);
781 782 783
			return -EINVAL;
		}

784 785
		smscore_registry_setmode(coredev->devpath, mode);

786
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
787
			rc = smscore_detect_mode(coredev);
788
			if (rc < 0) {
789
				sms_info("mode detect failed %d", rc);
790
				return rc;
791
			}
792
		}
793

794
		if (coredev->mode == mode) {
795
			sms_info("device mode %d already set", mode);
796 797 798
			return 0;
		}

799 800
		if (!(coredev->modes_supported & (1 << mode))) {
			type = smscore_registry_gettype(coredev->devpath);
801 802
			rc = smscore_load_firmware_from_file(
				coredev, smscore_fw_lkup[mode][type], NULL);
803
			if (rc < 0) {
804 805
				sms_info("load firmware "
					 "failed %d", rc);
806
				return rc;
807 808
			}
		} else
809 810
			sms_info("mode %d supported by running "
				 "firmware", mode);
811

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

819
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
820
				     sizeof(struct SmsMsgData_ST));
821 822
			msg->msgData[0] = mode;

823 824 825
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
826 827

			kfree(buffer);
828
		} else {
829 830
			sms_info("Could not allocate buffer for "
				 "init device message.");
831
			rc = -ENOMEM;
832 833 834
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
835
			sms_info("invalid mode specified %d", mode);
836 837 838 839 840
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

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

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

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

854
	if (rc != 0)
855
		sms_info("return error code %d.", rc);
856 857 858 859 860 861
	return rc;
}

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

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


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
893 894 895
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
896
		firstid = &((struct smscore_client_t *)next)->idlist;
897 898 899
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
900 901 902 903
			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;
904 905
				break;
			}
906 907 908 909 910 911 912 913 914 915
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

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

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

	if (!last_sample_time)
		last_sample_time = time_now;

937
	if (time_now - last_sample_time > 10000) {
938
		sms_debug("\ndata rate %d bytes/secs",
939 940
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
941 942 943 944 945 946

		last_sample_time = time_now;
		data_total = 0;
	}

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

952 953 954
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
955
		{
956 957
			struct SmsVersionRes_ST *ver =
				(struct SmsVersionRes_ST *) phdr;
958 959
			sms_debug("MSG_SMS_GET_VERSION_EX_RES "
				  "id %d prots 0x%x ver %d.%d",
960 961
				  ver->FirmwareId, ver->SupportedProtocols,
				  ver->RomVersionMajor, ver->RomVersionMinor);
962

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

967 968 969 970
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
971
			sms_debug("MSG_SMS_INIT_DEVICE_RES");
972 973 974
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
975
			sms_debug("MSG_SW_RELOAD_START_RES");
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
			sms_debug("MSG_SW_RELOAD_EXEC_RES");
983 984
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
985
			sms_debug("MSG_SMS_SWDOWNLOAD_TRIGGER_RES");
986 987 988 989 990 991 992
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
		default:
			break;
993 994 995 996 997 998 999 1000
		}
		smscore_putbuffer(coredev, cb);
	}
}

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

	spin_lock_irqsave(&coredev->bufferslock, flags);

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

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}

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

1037 1038 1039
int smscore_validate_client(struct smscore_device_t *coredev,
			    struct smscore_client_t *client,
			    int data_type, int id)
1040
{
1041 1042
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1043

1044
	if (!client) {
1045
		PERROR("bad parameter.");
1046 1047 1048
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1049
	if (registered_client == client)
1050
		return 0;
1051

1052
	if (registered_client) {
1053
		PERROR("The msg ID already registered to another client.");
1054 1055
		return -EEXIST;
	}
1056
	listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
1057
	if (!listentry) {
1058
		PERROR("Can't allocate memory for client id.");
1059
		return -ENOMEM;
1060 1061 1062
	}
	listentry->id = id;
	listentry->data_type = data_type;
1063 1064
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1065 1066 1067 1068 1069 1070 1071 1072 1073
	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
1074 1075
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1076 1077 1078 1079 1080 1081
 * @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.
 */
1082 1083 1084
int smscore_register_client(struct smscore_device_t *coredev,
			    struct smsclient_params_t *params,
			    struct smscore_client_t **client)
1085
{
1086
	struct smscore_client_t *newclient;
1087 1088 1089
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1090
		PERROR("Client already exist.");
1091
		return -EEXIST;
1092
	}
1093

1094
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1095
	if (!newclient) {
1096
		PERROR("Failed to allocate memory for client.");
1097
		return -ENOMEM;
1098 1099
	}

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

	return 0;
}

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

	spin_lock_irqsave(&coredev->clientslock, flags);


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

1138
	sms_info("%p", client->context);
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149

	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
 *
1150 1151
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1152 1153 1154 1155 1156
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1157 1158
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1159
{
1160 1161
	struct smscore_device_t *coredev;
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
1162 1163
	int rc;

1164
	if (client == NULL) {
1165
		sms_err("Got NULL client");
1166 1167 1168 1169
		return -EINVAL;
	}

	coredev = client->coredev;
1170

1171 1172
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1173
		sms_err("Got NULL coredev");
1174 1175 1176
		return -EINVAL;
	}

1177 1178
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
	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
 */
1192
int smscore_get_common_buffer_size(struct smscore_device_t *coredev)
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
{
	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.
 */
1205
int smscore_map_common_buffer(struct smscore_device_t *coredev,
1206
			       struct vm_area_struct *vma)
1207
{
1208 1209 1210
	unsigned long end = vma->vm_end,
		      start = vma->vm_start,
		      size = PAGE_ALIGN(coredev->common_buffer_size);
1211

1212 1213
	if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
	     (vma->vm_flags & VM_WRITE)) {
1214
		sms_info("invalid vm flags");
1215 1216 1217
		return -EINVAL;
	}

1218
	if ((end - start) != size) {
1219 1220
		sms_info("invalid size %d expected %d",
			 (int)(end - start), (int) size);
1221 1222 1223
		return -EINVAL;
	}

1224 1225
	if (remap_pfn_range(vma, start,
			    coredev->common_buffer_phys >> PAGE_SHIFT,
1226
			    size, pgprot_noncached(vma->vm_page_prot))) {
1227
		sms_info("remap_page_range failed");
1228 1229 1230 1231 1232 1233 1234 1235
		return -EAGAIN;
	}

	return 0;
}

int smscore_module_init(void)
{
1236
	int rc = 0;
1237 1238 1239 1240 1241 1242 1243 1244

	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);

1245 1246 1247 1248 1249 1250
	/* USB Register */
	rc = smsusb_register();

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

1251
	sms_debug("rc %d", rc);
1252 1253 1254 1255 1256 1257

	return rc;
}

void smscore_module_exit(void)
{
1258

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

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

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

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

1281 1282 1283 1284 1285 1286
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1287
	sms_debug("");
1288 1289 1290 1291 1292 1293
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

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