smscoreapi.c 31.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
#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>
30
#include <linux/io.h>
31 32 33 34

#include <linux/firmware.h>

#include "smscoreapi.h"
35
#include "sms-cards.h"
36

37 38 39 40
int sms_debug;
module_param_named(debug, sms_debug, int, 0644);
MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");

41
struct smscore_device_notifyee_t {
42 43
	struct list_head entry;
	hotplug_t hotplug;
44
};
45

46
struct smscore_idlist_t {
47 48 49
	struct list_head entry;
	int		id;
	int		data_type;
50
};
51

52
struct smscore_client_t {
53
	struct list_head entry;
54
	struct smscore_device_t *coredev;
55
	void			*context;
56
	struct list_head 	idlist;
57 58
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
59
};
60

61
struct smscore_device_t {
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
	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;
92 93

	int board_id;
94
};
95

96 97 98 99 100 101 102 103 104 105
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;
}

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

struct list_head g_smscore_notifyees;
struct list_head g_smscore_devices;
115
struct mutex g_smscore_deviceslock;
116 117

struct list_head g_smscore_registry;
118
struct mutex g_smscore_registrylock;
119

120
static int default_mode = 4;
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
	} else
148
		sms_err("failed to create smscore_registry.");
149
	kmutex_unlock(&g_smscore_registrylock);
150 151
	return entry;
}
152

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

157 158
	entry = smscore_find_registry(devpath);
	if (entry)
159 160
		return entry->mode;
	else
161
		sms_err("No registry found.");
162

163 164 165
	return default_mode;
}

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

170 171
	entry = smscore_find_registry(devpath);
	if (entry)
172 173
		return entry->type;
	else
174
		sms_err("No registry found.");
175

176 177
	return -1;
}
178

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

183 184 185
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
186
	else
187
		sms_err("No registry found.");
188
}
189

190 191
static 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
		sms_err("No registry found.");
200 201 202
}


203 204
static void list_add_locked(struct list_head *new, struct list_head *head,
			    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
static 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
static 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 317
static struct
smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
318
				       dma_addr_t common_buffer_phys)
319
{
320 321
	struct smscore_buffer_t *cb =
		kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
322
	if (!cb) {
323
		sms_info("kmalloc(...) failed");
324 325 326 327
		return NULL;
	}

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

	return cb;
}

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

350
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
351
	if (!dev) {
352
		sms_info("kzalloc(...) failed");
353 354 355
		return -ENOMEM;
	}

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

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

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

367
	/* init completion events */
368 369 370 371 372 373 374
	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);

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

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

		smscore_putbuffer(dev, cb);
	}

400
	sms_info("allocated %d buffers", 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
	sms_info("device %p created", 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
		sms_info("set device mode faile , rc %d", rc);
442
		return rc;
443
	}
444 445 446 447 448

	kmutex_lock(&g_smscore_deviceslock);

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

449
	sms_info("device %p started, rc %d", coredev, rc);
450 451 452 453 454 455

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}

456 457 458
static int smscore_sendrequest_and_wait(struct smscore_device_t *coredev,
					void *buffer, size_t size,
					struct completion *completion)
459 460
{
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
461
	if (rc < 0) {
462
		sms_info("sendrequest returned error %d", rc);
463
		return rc;
464
	}
465

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

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

480 481
	sms_info("loading FW to addr 0x%x size %d",
		 mem_address, firmware->Length);
482
	if (coredev->preload_handler) {
483 484 485 486 487
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

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

493
	if (coredev->mode != DEVICE_MODE_NONE) {
494
		sms_debug("sending reload command.");
495
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
496
			     sizeof(struct SmsMsgHdr_ST));
497 498 499
		rc = smscore_sendrequest_and_wait(coredev, msg,
						  msg->msgLength,
						  &coredev->reload_start_done);
500
		mem_address = *(u32 *) &payload[20];
501 502
	}

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

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

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

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

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

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

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

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

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

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

567
	sms_debug("rc=%d, postload=%p ", rc,
568
		  coredev->postload_handler);
569 570 571

	kfree(msg);

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

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

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

	rc = request_firmware(&fw, filename, coredev->device);
600
	if (rc < 0) {
601
		sms_info("failed to open \"%s\"", filename);
602 603
		return rc;
	}
604
	sms_info("read FW %s, size=%zd", filename, fw->size);
605 606 607
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
608 609 610
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
611 612 613 614 615
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
616 617

		kfree(fw_buffer);
618
	} else {
619
		sms_info("failed to allocate firmware buffer");
620 621 622 623 624 625 626 627 628
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

/**
629 630
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
631
 *
632 633
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
634 635 636
 *
 * @return 0 on success, <0 on error.
 */
637
void smscore_unregister_device(struct smscore_device_t *coredev)
638
{
639
	struct smscore_buffer_t *cb;
640
	int num_buffers = 0;
641
	int retry = 0;
642 643 644 645 646 647

	kmutex_lock(&g_smscore_deviceslock);

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

648 649
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
650

651 652
	while (1) {
		while ((cb = smscore_getbuffer(coredev))) {
653
			kfree(cb);
654
			num_buffers++;
655 656 657
		}
		if (num_buffers == coredev->num_buffers)
			break;
658
		if (++retry > 10) {
659 660
			sms_info("exiting although "
				 "not all buffers released.");
661 662
			break;
		}
663

664
		sms_info("waiting for %d buffer(s)",
665
			 coredev->num_buffers - num_buffers);
666 667 668
		msleep(100);
	}

669
	sms_info("freed %d buffers", num_buffers);
670 671

	if (coredev->common_buffer)
672 673 674
		dma_free_coherent(NULL, coredev->common_buffer_size,
				  coredev->common_buffer,
				  coredev->common_buffer_phys);
675 676 677 678 679 680

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

	kmutex_unlock(&g_smscore_deviceslock);

681
	sms_info("device %p destroyed", coredev);
682 683
}

684
static int smscore_detect_mode(struct smscore_device_t *coredev)
685
{
686
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
687
			       GFP_KERNEL | GFP_DMA);
688 689
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
690 691 692 693 694
	int rc;

	if (!buffer)
		return -ENOMEM;

695 696
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
697

698 699 700
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
701
		sms_err("MSG_SMS_GET_VERSION_EX_REQ failed first try");
702

703 704
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
705 706 707
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
708
			if (rc < 0)
709 710
				sms_err("MSG_SMS_GET_VERSION_EX_REQ failed "
					"second try, rc %d", rc);
711
		} else
712 713 714 715 716 717 718 719
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

720
static char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
	/*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"}
738 739
};

740 741 742 743 744 745
static inline char *sms_get_fw_name(struct smscore_device_t *coredev,
				    int mode, enum sms_device_type_st type)
{
	char **fw = sms_get_board(smscore_get_board_id(coredev))->fw;
	return (fw && fw[mode]) ? fw[mode] : smscore_fw_lkup[mode][type];
}
746

747 748 749 750
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
751 752
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
753 754 755 756
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
757
int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
758 759 760
{
	void *buffer;
	int rc = 0;
761
	enum sms_device_type_st type;
762

763
	sms_debug("set device mode to %d", mode);
764 765
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
766
			sms_err("invalid mode specified %d", mode);
767 768 769
			return -EINVAL;
		}

770 771
		smscore_registry_setmode(coredev->devpath, mode);

772
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
773
			rc = smscore_detect_mode(coredev);
774
			if (rc < 0) {
775
				sms_err("mode detect failed %d", rc);
776
				return rc;
777
			}
778
		}
779

780
		if (coredev->mode == mode) {
781
			sms_info("device mode %d already set", mode);
782 783 784
			return 0;
		}

785
		if (!(coredev->modes_supported & (1 << mode))) {
786 787
			char *fw_filename;

788
			type = smscore_registry_gettype(coredev->devpath);
789 790 791 792
			fw_filename = sms_get_fw_name(coredev, mode, type);

			rc = smscore_load_firmware_from_file(coredev,
							     fw_filename, NULL);
793
			if (rc < 0) {
794 795 796
				sms_warn("error %d loading firmware: %s, "
					 "trying again with default firmware",
					 rc, fw_filename);
797 798

				/* try again with the default firmware */
799
				fw_filename = smscore_fw_lkup[mode][type];
800
				rc = smscore_load_firmware_from_file(coredev,
801
							     fw_filename, NULL);
802 803

				if (rc < 0) {
804 805 806
					sms_warn("error %d loading "
						 "firmware: %s", rc,
						 fw_filename);
807 808
					return rc;
				}
809
			}
810
			sms_log("firmware download success: %s", fw_filename);
811
		} else
812 813
			sms_info("mode %d supported by running "
				 "firmware", mode);
814

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

822
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
823
				     sizeof(struct SmsMsgData_ST));
824 825
			msg->msgData[0] = mode;

826 827 828
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
829 830

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

		smscore_registry_setmode(coredev->devpath, mode);

844
		if (coredev->detectmode_handler)
845 846
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
847 848 849 850 851

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

852
	if (rc >= 0) {
853 854 855 856
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

857
	if (rc != 0)
858
		sms_err("return error code %d.", rc);
859 860 861 862 863 864
	return rc;
}

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

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


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

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

934 935
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
936 937 938 939 940
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

941
	if (time_now - last_sample_time > 10000) {
942
		sms_debug("\ndata rate %d bytes/secs",
943 944
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
945 946 947 948 949 950

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
951 952
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
953 954 955
	if (client)
		rc = client->onresponse_handler(client->context, cb);

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

967 968 969
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
970

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

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

	spin_lock_irqsave(&coredev->bufferslock, flags);

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

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}

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

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

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

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

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

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

	return 0;
}

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

	spin_lock_irqsave(&coredev->clientslock, flags);


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

1142
	sms_info("%p", client->context);
1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153

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

1168
	if (client == NULL) {
1169
		sms_err("Got NULL client");
1170 1171 1172 1173
		return -EINVAL;
	}

	coredev = client->coredev;
1174

1175 1176
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1177
		sms_err("Got NULL coredev");
1178 1179 1180
		return -EINVAL;
	}

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

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


int smscore_module_init(void)
{
1192
	int rc = 0;
1193 1194 1195 1196 1197 1198 1199 1200

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

1201 1202 1203 1204 1205 1206
	/* USB Register */
	rc = smsusb_register();

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

1207
	sms_debug("rc %d", rc);
1208 1209 1210 1211 1212 1213

	return rc;
}

void smscore_module_exit(void)
{
1214

1215
	kmutex_lock(&g_smscore_deviceslock);
1216
	while (!list_empty(&g_smscore_notifyees)) {
1217 1218 1219
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
1220 1221 1222 1223 1224 1225 1226

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

	kmutex_lock(&g_smscore_registrylock);
1227
	while (!list_empty(&g_smscore_registry)) {
1228 1229 1230
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
1231 1232 1233 1234 1235 1236

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

1237 1238 1239 1240 1241 1242
	/* DVB UnRegister */
	smsdvb_unregister();

	/* Unregister USB */
	smsusb_unregister();

1243
	sms_debug("");
1244 1245 1246 1247 1248
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

1249
MODULE_DESCRIPTION("Driver for the Siano SMS1XXX USB dongle");
1250
MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1251
MODULE_LICENSE("GPL");