smscoreapi.c 42.1 KB
Newer Older
1
/*
2 3 4 5
 *  Siano core API module
 *
 *  This file contains implementation for the interface to sms core component
 *
6
 *  author: Uri Shkolnik
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
 *  it under the terms of the GNU General Public License version 2 as
12
 *  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

#include <linux/firmware.h>
33
#include <linux/wait.h>
34
#include <asm/byteorder.h>
35 36

#include "smscoreapi.h"
37
#include "sms-cards.h"
38 39
#include "smsir.h"
#include "smsendian.h"
40

41
static int sms_dbg;
42
module_param_named(debug, sms_dbg, int, 0644);
43 44
MODULE_PARM_DESC(debug, "set debug level (info=1, adv=2 (or-able))");

45
struct smscore_device_notifyee_t {
46 47
	struct list_head entry;
	hotplug_t hotplug;
48
};
49

50
struct smscore_idlist_t {
51 52 53
	struct list_head entry;
	int		id;
	int		data_type;
54
};
55

56
struct smscore_client_t {
57
	struct list_head entry;
58
	struct smscore_device_t *coredev;
59
	void			*context;
60
	struct list_head 	idlist;
61 62
	onresponse_t	onresponse_handler;
	onremove_t		onremove_handler;
63
};
64

65 66 67 68 69
void smscore_set_board_id(struct smscore_device_t *core, int id)
{
	core->board_id = id;
}

70 71 72 73 74 75
int smscore_led_state(struct smscore_device_t *core, int led)
{
	if (led >= 0)
		core->led_state = led;
	return core->led_state;
}
76
EXPORT_SYMBOL_GPL(smscore_set_board_id);
77

78 79 80 81
int smscore_get_board_id(struct smscore_device_t *core)
{
	return core->board_id;
}
82
EXPORT_SYMBOL_GPL(smscore_get_board_id);
83

84
struct smscore_registry_entry_t {
85 86 87
	struct list_head entry;
	char			devpath[32];
	int				mode;
88 89
	enum sms_device_type_st	type;
};
90

91 92 93
static struct list_head g_smscore_notifyees;
static struct list_head g_smscore_devices;
static struct mutex g_smscore_deviceslock;
94

95 96
static struct list_head g_smscore_registry;
static struct mutex g_smscore_registrylock;
97

98
static int default_mode = 4;
99

100 101 102
module_param(default_mode, int, 0644);
MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");

103
static struct smscore_registry_entry_t *smscore_find_registry(char *devpath)
104
{
105
	struct smscore_registry_entry_t *entry;
106 107 108
	struct list_head *next;

	kmutex_lock(&g_smscore_registrylock);
109 110 111
	for (next = g_smscore_registry.next;
	     next != &g_smscore_registry;
	     next = next->next) {
112
		entry = (struct smscore_registry_entry_t *) next;
113
		if (!strcmp(entry->devpath, devpath)) {
114
			kmutex_unlock(&g_smscore_registrylock);
115
			return entry;
116 117
		}
	}
118 119 120
	entry = (struct smscore_registry_entry_t *)
			kmalloc(sizeof(struct smscore_registry_entry_t),
				GFP_KERNEL);
121
	if (entry) {
122 123 124
		entry->mode = default_mode;
		strcpy(entry->devpath, devpath);
		list_add(&entry->entry, &g_smscore_registry);
125
	} else
126
		sms_err("failed to create smscore_registry.");
127
	kmutex_unlock(&g_smscore_registrylock);
128 129
	return entry;
}
130

131
int smscore_registry_getmode(char *devpath)
132
{
133
	struct smscore_registry_entry_t *entry;
134

135 136
	entry = smscore_find_registry(devpath);
	if (entry)
137 138
		return entry->mode;
	else
139
		sms_err("No registry found.");
140

141 142
	return default_mode;
}
143
EXPORT_SYMBOL_GPL(smscore_registry_getmode);
144

145
static enum sms_device_type_st smscore_registry_gettype(char *devpath)
146
{
147
	struct smscore_registry_entry_t *entry;
148

149 150
	entry = smscore_find_registry(devpath);
	if (entry)
151 152
		return entry->type;
	else
153
		sms_err("No registry found.");
154

155 156
	return -1;
}
157

158 159
void smscore_registry_setmode(char *devpath, int mode)
{
160
	struct smscore_registry_entry_t *entry;
161

162 163 164
	entry = smscore_find_registry(devpath);
	if (entry)
		entry->mode = mode;
165
	else
166
		sms_err("No registry found.");
167
}
168

169 170
static void smscore_registry_settype(char *devpath,
				     enum sms_device_type_st type)
171
{
172
	struct smscore_registry_entry_t *entry;
173

174 175
	entry = smscore_find_registry(devpath);
	if (entry)
176 177
		entry->type = type;
	else
178
		sms_err("No registry found.");
179 180 181
}


182 183
static void list_add_locked(struct list_head *new, struct list_head *head,
			    spinlock_t *lock)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
{
	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)
{
204
	struct smscore_device_notifyee_t *notifyee;
205 206 207 208 209
	struct list_head *next, *first;
	int rc = 0;

	kmutex_lock(&g_smscore_deviceslock);

210 211
	notifyee = kmalloc(sizeof(struct smscore_device_notifyee_t),
			   GFP_KERNEL);
212 213
	if (notifyee) {
		/* now notify callback about existing devices */
214
		first = &g_smscore_devices;
215 216 217
		for (next = first->next;
		     next != first && !rc;
		     next = next->next) {
218 219
			struct smscore_device_t *coredev =
				(struct smscore_device_t *) next;
220 221 222
			rc = hotplug(coredev, coredev->device, 1);
		}

223
		if (rc >= 0) {
224 225
			notifyee->hotplug = hotplug;
			list_add(&notifyee->entry, &g_smscore_notifyees);
226
		} else
227
			kfree(notifyee);
228
	} else
229 230 231 232 233 234
		rc = -ENOMEM;

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}
235
EXPORT_SYMBOL_GPL(smscore_register_hotplug);
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250

/**
 * 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;

251
	for (next = first->next; next != first;) {
252 253
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *) next;
254 255
		next = next->next;

256
		if (notifyee->hotplug == hotplug) {
257 258 259 260 261 262 263
			list_del(&notifyee->entry);
			kfree(notifyee);
		}
	}

	kmutex_unlock(&g_smscore_deviceslock);
}
264
EXPORT_SYMBOL_GPL(smscore_unregister_hotplug);
265

266
static void smscore_notify_clients(struct smscore_device_t *coredev)
267
{
268
	struct smscore_client_t *client;
269

270 271
	/* the client must call smscore_unregister_client from remove handler */
	while (!list_empty(&coredev->clients)) {
272
		client = (struct smscore_client_t *) coredev->clients.next;
273 274 275 276
		client->onremove_handler(client->context);
	}
}

277 278
static int smscore_notify_callbacks(struct smscore_device_t *coredev,
				    struct device *device, int arrival)
279 280 281 282
{
	struct list_head *next, *first;
	int rc = 0;

283
	/* note: must be called under g_deviceslock */
284 285 286

	first = &g_smscore_notifyees;

287
	for (next = first->next; next != first; next = next->next) {
288
		rc = ((struct smscore_device_notifyee_t *) next)->
289
				hotplug(coredev, device, arrival);
290 291 292 293 294 295 296
		if (rc < 0)
			break;
	}

	return rc;
}

297 298
static struct
smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
299
				       dma_addr_t common_buffer_phys)
300
{
301 302
	struct smscore_buffer_t *cb =
		kmalloc(sizeof(struct smscore_buffer_t), GFP_KERNEL);
303
	if (!cb) {
304
		sms_info("kmalloc(...) failed");
305 306 307 308
		return NULL;
	}

	cb->p = buffer;
309
	cb->offset_in_common = buffer - (u8 *) common_buffer;
310 311 312 313 314 315
	cb->phys = common_buffer_phys + cb->offset_in_common;

	return cb;
}

/**
316 317
 * creates coredev object for a device, prepares buffers,
 * creates buffer mappings, notifies registered hotplugs about new device.
318
 *
319 320
 * @param params device pointer to struct with device specific parameters
 *               and handlers
321 322 323 324
 * @param coredev pointer to a value that receives created coredev object
 *
 * @return 0 on success, <0 on error.
 */
325 326
int smscore_register_device(struct smsdevice_params_t *params,
			    struct smscore_device_t **coredev)
327
{
328
	struct smscore_device_t *dev;
329 330
	u8 *buffer;

331
	dev = kzalloc(sizeof(struct smscore_device_t), GFP_KERNEL);
332
	if (!dev) {
333
		sms_info("kzalloc(...) failed");
334 335 336
		return -ENOMEM;
	}

337
	/* init list entry so it could be safe in smscore_unregister_device */
338 339
	INIT_LIST_HEAD(&dev->entry);

340
	/* init queues */
341 342 343
	INIT_LIST_HEAD(&dev->clients);
	INIT_LIST_HEAD(&dev->buffers);

344
	/* init locks */
345 346 347
	spin_lock_init(&dev->clientslock);
	spin_lock_init(&dev->bufferslock);

348
	/* init completion events */
349 350 351 352 353 354
	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);
355 356 357
	init_completion(&dev->gpio_configuration_done);
	init_completion(&dev->gpio_set_level_done);
	init_completion(&dev->gpio_get_level_done);
358
	init_completion(&dev->ir_init_done);
359

360 361 362
	/* Buffer management */
	init_waitqueue_head(&dev->buffer_mng_waitq);

363
	/* alloc common buffer */
364
	dev->common_buffer_size = params->buffer_size * params->num_buffers;
365 366 367 368
	dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
						&dev->common_buffer_phys,
						GFP_KERNEL | GFP_DMA);
	if (!dev->common_buffer) {
369 370 371 372
		smscore_unregister_device(dev);
		return -ENOMEM;
	}

373 374 375
	/* prepare dma buffers */
	for (buffer = dev->common_buffer;
	     dev->num_buffers < params->num_buffers;
376
	     dev->num_buffers++, buffer += params->buffer_size) {
377
		struct smscore_buffer_t *cb =
378 379
			smscore_createbuffer(buffer, dev->common_buffer,
					     dev->common_buffer_phys);
380
		if (!cb) {
381 382 383 384 385 386 387
			smscore_unregister_device(dev);
			return -ENOMEM;
		}

		smscore_putbuffer(dev, cb);
	}

388
	sms_info("allocated %d buffers", dev->num_buffers);
389 390 391 392 393 394 395 396 397 398 399 400 401

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

402
	smscore_registry_settype(dev->devpath, params->device_type);
403

404
	/* add device to devices list */
405 406 407 408 409 410
	kmutex_lock(&g_smscore_deviceslock);
	list_add(&dev->entry, &g_smscore_devices);
	kmutex_unlock(&g_smscore_deviceslock);

	*coredev = dev;

411
	sms_info("device %p created", dev);
412 413 414

	return 0;
}
415
EXPORT_SYMBOL_GPL(smscore_register_device);
416

417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

static int smscore_sendrequest_and_wait(struct smscore_device_t *coredev,
		void *buffer, size_t size, struct completion *completion) {
	int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
	if (rc < 0) {
		sms_info("sendrequest returned error %d", rc);
		return rc;
	}

	return wait_for_completion_timeout(completion,
			msecs_to_jiffies(SMS_PROTOCOL_MAX_RAOUNDTRIP_MS)) ?
			0 : -ETIME;
}

/**
 * Starts & enables IR operations
 *
 * @return 0 on success, < 0 on error.
 */
static int smscore_init_ir(struct smscore_device_t *coredev)
{
	int ir_io;
	int rc;
	void *buffer;

	coredev->ir.input_dev = NULL;
	ir_io = sms_get_board(smscore_get_board_id(coredev))->board_cfg.ir;
	if (ir_io) {/* only if IR port exist we use IR sub-module */
		sms_info("IR loading");
		rc = sms_ir_init(coredev);

		if	(rc != 0)
			sms_err("Error initialization DTV IR sub-module");
		else {
			buffer = kmalloc(sizeof(struct SmsMsgData_ST2) +
						SMS_DMA_ALIGNMENT,
						GFP_KERNEL | GFP_DMA);
			if (buffer) {
				struct SmsMsgData_ST2 *msg =
				(struct SmsMsgData_ST2 *)
				SMS_ALIGN_ADDRESS(buffer);

				SMS_INIT_MSG(&msg->xMsgHeader,
						MSG_SMS_START_IR_REQ,
						sizeof(struct SmsMsgData_ST2));
				msg->msgData[0] = coredev->ir.controller;
				msg->msgData[1] = coredev->ir.timeout;

				smsendian_handle_tx_message(
					(struct SmsMsgHdr_ST2 *)msg);
				rc = smscore_sendrequest_and_wait(coredev, msg,
						msg->xMsgHeader. msgLength,
						&coredev->ir_init_done);

				kfree(buffer);
			} else
				sms_err
				("Sending IR initialization message failed");
		}
	} else
		sms_info("IR port has not been detected");

	return 0;
}

482 483 484
/**
 * sets initial device mode and notifies client hotplugs that device is ready
 *
485 486
 * @param coredev pointer to a coredev object returned by
 * 		  smscore_register_device
487 488 489
 *
 * @return 0 on success, <0 on error.
 */
490
int smscore_start_device(struct smscore_device_t *coredev)
491
{
492 493
	int rc = smscore_set_device_mode(
			coredev, smscore_registry_getmode(coredev->devpath));
494
	if (rc < 0) {
495
		sms_info("set device mode faile , rc %d", rc);
496
		return rc;
497
	}
498 499 500 501

	kmutex_lock(&g_smscore_deviceslock);

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

504
	sms_info("device %p started, rc %d", coredev, rc);
505 506 507 508 509

	kmutex_unlock(&g_smscore_deviceslock);

	return rc;
}
510
EXPORT_SYMBOL_GPL(smscore_start_device);
511 512


513 514
static int smscore_load_firmware_family2(struct smscore_device_t *coredev,
					 void *buffer, size_t size)
515
{
516 517
	struct SmsFirmware_ST *firmware = (struct SmsFirmware_ST *) buffer;
	struct SmsMsgHdr_ST *msg;
518
	u32 mem_address;
519
	u8 *payload = firmware->Payload;
520
	int rc = 0;
521 522 523 524
	firmware->StartAddress = le32_to_cpu(firmware->StartAddress);
	firmware->Length = le32_to_cpu(firmware->Length);

	mem_address = firmware->StartAddress;
525

526 527
	sms_info("loading FW to addr 0x%x size %d",
		 mem_address, firmware->Length);
528
	if (coredev->preload_handler) {
529 530 531 532 533
		rc = coredev->preload_handler(coredev->context);
		if (rc < 0)
			return rc;
	}

534
	/* PAGE_SIZE buffer shall be enough and dma aligned */
535
	msg = kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
536 537 538
	if (!msg)
		return -ENOMEM;

539
	if (coredev->mode != DEVICE_MODE_NONE) {
540
		sms_debug("sending reload command.");
541
		SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
542
			     sizeof(struct SmsMsgHdr_ST));
543 544 545
		rc = smscore_sendrequest_and_wait(coredev, msg,
						  msg->msgLength,
						  &coredev->reload_start_done);
546
		mem_address = *(u32 *) &payload[20];
547 548
	}

549
	while (size && rc >= 0) {
550 551
		struct SmsDataDownload_ST *DataMsg =
			(struct SmsDataDownload_ST *) msg;
552 553
		int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);

554
		SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
555
			     (u16)(sizeof(struct SmsMsgHdr_ST) +
556
				      sizeof(u32) + payload_size));
557 558 559 560

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

561 562
		if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
		    (coredev->mode == DEVICE_MODE_NONE))
563 564 565
			rc = coredev->sendrequest_handler(
				coredev->context, DataMsg,
				DataMsg->xMsgHeader.msgLength);
566
		else
567 568 569 570
			rc = smscore_sendrequest_and_wait(
				coredev, DataMsg,
				DataMsg->xMsgHeader.msgLength,
				&coredev->data_download_done);
571 572 573 574 575 576

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

577 578
	if (rc >= 0) {
		if (coredev->mode == DEVICE_MODE_NONE) {
579 580
			struct SmsMsgData_ST *TriggerMsg =
				(struct SmsMsgData_ST *) msg;
581

582
			SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
583
				     sizeof(struct SmsMsgHdr_ST) +
584
				     sizeof(u32) * 5);
585

586 587
			TriggerMsg->msgData[0] = firmware->StartAddress;
						/* Entry point */
588 589 590 591
			TriggerMsg->msgData[1] = 5; /* Priority */
			TriggerMsg->msgData[2] = 0x200; /* Stack size */
			TriggerMsg->msgData[3] = 0; /* Parameter */
			TriggerMsg->msgData[4] = 4; /* Task ID */
592

593
			if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
594 595 596
				rc = coredev->sendrequest_handler(
					coredev->context, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength);
597
				msleep(100);
598
			} else
599 600 601 602
				rc = smscore_sendrequest_and_wait(
					coredev, TriggerMsg,
					TriggerMsg->xMsgHeader.msgLength,
					&coredev->trigger_done);
603 604
		} else {
			SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
605
				     sizeof(struct SmsMsgHdr_ST));
606

607 608
			rc = coredev->sendrequest_handler(coredev->context,
							  msg, msg->msgLength);
609
		}
610
		msleep(500);
611 612
	}

613
	sms_debug("rc=%d, postload=%p ", rc,
614
		  coredev->postload_handler);
615 616 617

	kfree(msg);

618
	return ((rc >= 0) && coredev->postload_handler) ?
619 620 621 622 623 624 625
		coredev->postload_handler(coredev->context) :
		rc;
}

/**
 * loads specified firmware into a buffer and calls device loadfirmware_handler
 *
626 627
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
628 629 630 631 632
 * @param filename null-terminated string specifies firmware file name
 * @param loadfirmware_handler device handler that loads firmware
 *
 * @return 0 on success, <0 on error.
 */
633 634 635
static int smscore_load_firmware_from_file(struct smscore_device_t *coredev,
					   char *filename,
					   loadfirmware_t loadfirmware_handler)
636 637 638
{
	int rc = -ENOENT;
	const struct firmware *fw;
639
	u8 *fw_buffer;
640

641 642
	if (loadfirmware_handler == NULL && !(coredev->device_flags &
					      SMS_DEVICE_FAMILY2))
643 644 645
		return -EINVAL;

	rc = request_firmware(&fw, filename, coredev->device);
646
	if (rc < 0) {
647
		sms_info("failed to open \"%s\"", filename);
648 649
		return rc;
	}
650
	sms_info("read FW %s, size=%zd", filename, fw->size);
651 652 653
	fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
			    GFP_KERNEL | GFP_DMA);
	if (fw_buffer) {
654 655 656
		memcpy(fw_buffer, fw->data, fw->size);

		rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
657 658 659 660 661
		      smscore_load_firmware_family2(coredev,
						    fw_buffer,
						    fw->size) :
		      loadfirmware_handler(coredev->context,
					   fw_buffer, fw->size);
662 663

		kfree(fw_buffer);
664
	} else {
665
		sms_info("failed to allocate firmware buffer");
666 667 668 669 670 671 672 673 674
		rc = -ENOMEM;
	}

	release_firmware(fw);

	return rc;
}

/**
675 676
 * notifies all clients registered with the device, notifies hotplugs,
 * frees all buffers and coredev object
677
 *
678 679
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
680 681 682
 *
 * @return 0 on success, <0 on error.
 */
683
void smscore_unregister_device(struct smscore_device_t *coredev)
684
{
685
	struct smscore_buffer_t *cb;
686
	int num_buffers = 0;
687
	int retry = 0;
688 689 690

	kmutex_lock(&g_smscore_deviceslock);

691 692 693
	/* Release input device (IR) resources */
	sms_ir_exit(coredev);

694 695 696
	smscore_notify_clients(coredev);
	smscore_notify_callbacks(coredev, NULL, 0);

697 698
	/* at this point all buffers should be back
	 * onresponse must no longer be called */
699

700
	while (1) {
701 702 703
		while (!list_empty(&coredev->buffers)) {
			cb = (struct smscore_buffer_t *) coredev->buffers.next;
			list_del(&cb->entry);
704
			kfree(cb);
705
			num_buffers++;
706 707 708
		}
		if (num_buffers == coredev->num_buffers)
			break;
709
		if (++retry > 10) {
710 711
			sms_info("exiting although "
				 "not all buffers released.");
712 713
			break;
		}
714

715
		sms_info("waiting for %d buffer(s)",
716
			 coredev->num_buffers - num_buffers);
717 718 719
		msleep(100);
	}

720
	sms_info("freed %d buffers", num_buffers);
721 722

	if (coredev->common_buffer)
723
		dma_free_coherent(NULL, coredev->common_buffer_size,
724 725 726 727
			coredev->common_buffer, coredev->common_buffer_phys);

	if (coredev->fw_buf != NULL)
		kfree(coredev->fw_buf);
728 729 730 731 732 733

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

	kmutex_unlock(&g_smscore_deviceslock);

734
	sms_info("device %p destroyed", coredev);
735
}
736
EXPORT_SYMBOL_GPL(smscore_unregister_device);
737

738
static int smscore_detect_mode(struct smscore_device_t *coredev)
739
{
740
	void *buffer = kmalloc(sizeof(struct SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
741
			       GFP_KERNEL | GFP_DMA);
742 743
	struct SmsMsgHdr_ST *msg =
		(struct SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
744 745 746 747 748
	int rc;

	if (!buffer)
		return -ENOMEM;

749 750
	SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ,
		     sizeof(struct SmsMsgHdr_ST));
751

752 753 754
	rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
					  &coredev->version_ex_done);
	if (rc == -ETIME) {
755
		sms_err("MSG_SMS_GET_VERSION_EX_REQ failed first try");
756

757 758
		if (wait_for_completion_timeout(&coredev->resume_done,
						msecs_to_jiffies(5000))) {
759 760 761
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->msgLength,
				&coredev->version_ex_done);
762
			if (rc < 0)
763 764
				sms_err("MSG_SMS_GET_VERSION_EX_REQ failed "
					"second try, rc %d", rc);
765
		} else
766 767 768 769 770 771 772 773
			rc = -ETIME;
	}

	kfree(buffer);

	return rc;
}

774
static char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
775 776 777 778 779 780
	/*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*/
781
	{"none", "tdmb_nova_12mhz.inp", "tdmb_nova_12mhz_b0.inp", "none"},
782 783 784 785 786
	/*DABIP*/
	{"none", "none", "none", "none"},
	/*BDA*/
	{"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
	/*ISDBT*/
787
	{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
788 789 790 791
	/*ISDBTBDA*/
	{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
	/*CMMB*/
	{"none", "none", "none", "cmmb_vega_12mhz.inp"}
792 793
};

794 795 796 797 798 799
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];
}
800

801 802 803 804
/**
 * calls device handler to change mode of operation
 * NOTE: stellar/usb may disconnect when changing mode
 *
805 806
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
807 808 809 810
 * @param mode requested mode of operation
 *
 * @return 0 on success, <0 on error.
 */
811
int smscore_set_device_mode(struct smscore_device_t *coredev, int mode)
812 813 814
{
	void *buffer;
	int rc = 0;
815
	enum sms_device_type_st type;
816

817
	sms_debug("set device mode to %d", mode);
818
	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
819
		if (mode < DEVICE_MODE_DVBT || mode >= DEVICE_MODE_RAW_TUNER) {
820
			sms_err("invalid mode specified %d", mode);
821 822 823
			return -EINVAL;
		}

824 825
		smscore_registry_setmode(coredev->devpath, mode);

826
		if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
827
			rc = smscore_detect_mode(coredev);
828
			if (rc < 0) {
829
				sms_err("mode detect failed %d", rc);
830
				return rc;
831
			}
832
		}
833

834
		if (coredev->mode == mode) {
835
			sms_info("device mode %d already set", mode);
836 837 838
			return 0;
		}

839
		if (!(coredev->modes_supported & (1 << mode))) {
840 841
			char *fw_filename;

842
			type = smscore_registry_gettype(coredev->devpath);
843 844 845 846
			fw_filename = sms_get_fw_name(coredev, mode, type);

			rc = smscore_load_firmware_from_file(coredev,
							     fw_filename, NULL);
847
			if (rc < 0) {
848 849 850
				sms_warn("error %d loading firmware: %s, "
					 "trying again with default firmware",
					 rc, fw_filename);
851 852

				/* try again with the default firmware */
853
				fw_filename = smscore_fw_lkup[mode][type];
854
				rc = smscore_load_firmware_from_file(coredev,
855
							     fw_filename, NULL);
856 857

				if (rc < 0) {
858 859 860
					sms_warn("error %d loading "
						 "firmware: %s", rc,
						 fw_filename);
861 862
					return rc;
				}
863
			}
864
			sms_log("firmware download success: %s", fw_filename);
865
		} else
866 867
			sms_info("mode %d supported by running "
				 "firmware", mode);
868

869 870
		buffer = kmalloc(sizeof(struct SmsMsgData_ST) +
				 SMS_DMA_ALIGNMENT, GFP_KERNEL | GFP_DMA);
871
		if (buffer) {
872 873 874
			struct SmsMsgData_ST *msg =
				(struct SmsMsgData_ST *)
					SMS_ALIGN_ADDRESS(buffer);
875

876
			SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
877
				     sizeof(struct SmsMsgData_ST));
878 879
			msg->msgData[0] = mode;

880 881 882
			rc = smscore_sendrequest_and_wait(
				coredev, msg, msg->xMsgHeader.msgLength,
				&coredev->init_device_done);
883 884

			kfree(buffer);
885
		} else {
886 887
			sms_err("Could not allocate buffer for "
				"init device message.");
888
			rc = -ENOMEM;
889 890 891
		}
	} else {
		if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
892
			sms_err("invalid mode specified %d", mode);
893 894 895 896 897
			return -EINVAL;
		}

		smscore_registry_setmode(coredev->devpath, mode);

898
		if (coredev->detectmode_handler)
899 900
			coredev->detectmode_handler(coredev->context,
						    &coredev->mode);
901 902 903 904 905

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

906
	if (rc >= 0) {
907 908 909 910
		coredev->mode = mode;
		coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
	}

911
	if (rc < 0)
912
		sms_err("return error code %d.", rc);
913 914 915 916 917 918
	return rc;
}

/**
 * calls device handler to get current mode of operation
 *
919 920
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
921 922 923
 *
 * @return current mode
 */
924
int smscore_get_device_mode(struct smscore_device_t *coredev)
925 926 927
{
	return coredev->mode;
}
928
EXPORT_SYMBOL_GPL(smscore_get_device_mode);
929

930 931 932 933
/**
 * find client by response id & type within the clients list.
 * return client handle or NULL.
 *
934 935
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
936
 * @param data_type client data type (SMS_DONT_CARE for all types)
937
 * @param id client id (SMS_DONT_CARE for all id)
938 939
 *
 */
940 941
static struct
smscore_client_t *smscore_find_client(struct smscore_device_t *coredev,
942
				      int data_type, int id)
943
{
944
	struct smscore_client_t *client = NULL;
945 946
	struct list_head *next, *first;
	unsigned long flags;
947
	struct list_head *firstid, *nextid;
948 949 950 951


	spin_lock_irqsave(&coredev->clientslock, flags);
	first = &coredev->clients;
952 953 954
	for (next = first->next;
	     (next != first) && !client;
	     next = next->next) {
955
		firstid = &((struct smscore_client_t *)next)->idlist;
956 957 958
		for (nextid = firstid->next;
		     nextid != firstid;
		     nextid = nextid->next) {
959 960 961 962
			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;
963 964
				break;
			}
965 966 967 968 969 970 971 972 973 974
		}
	}
	spin_unlock_irqrestore(&coredev->clientslock, flags);
	return client;
}

/**
 * find client by response id/type, call clients onresponse handler
 * return buffer to pool on error
 *
975 976
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
977 978 979
 * @param cb pointer to response buffer descriptor
 *
 */
980
void smscore_onresponse(struct smscore_device_t *coredev,
981 982 983 984
		struct smscore_buffer_t *cb) {
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) ((u8 *) cb->p
			+ cb->offset);
	struct smscore_client_t *client;
985
	int rc = -EBUSY;
986 987
	static unsigned long last_sample_time; /* = 0; */
	static int data_total; /* = 0; */
988 989 990 991 992
	unsigned long time_now = jiffies_to_msecs(jiffies);

	if (!last_sample_time)
		last_sample_time = time_now;

993
	if (time_now - last_sample_time > 10000) {
994
		sms_debug("\ndata rate %d bytes/secs",
995 996
			  (int)((data_total * 1000) /
				(time_now - last_sample_time)));
997 998 999 1000 1001 1002

		last_sample_time = time_now;
		data_total = 0;
	}

	data_total += cb->size;
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	/* Do we need to re-route? */
	if ((phdr->msgType == MSG_SMS_HO_PER_SLICES_IND) ||
			(phdr->msgType == MSG_SMS_TRANSMISSION_IND)) {
		if (coredev->mode == DEVICE_MODE_DVBT_BDA)
			phdr->msgDstId = DVBT_BDA_CONTROL_MSG_ID;
	}


	client = smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);

1013 1014
	/* If no client registered for type & id,
	 * check for control client where type is not registered */
1015 1016 1017
	if (client)
		rc = client->onresponse_handler(client->context, cb);

1018 1019 1020
	if (rc < 0) {
		switch (phdr->msgType) {
		case MSG_SMS_GET_VERSION_EX_RES:
1021
		{
1022 1023
			struct SmsVersionRes_ST *ver =
				(struct SmsVersionRes_ST *) phdr;
1024 1025
			sms_debug("MSG_SMS_GET_VERSION_EX_RES "
				  "id %d prots 0x%x ver %d.%d",
1026 1027
				  ver->FirmwareId, ver->SupportedProtocols,
				  ver->RomVersionMajor, ver->RomVersionMinor);
1028

1029 1030 1031
			coredev->mode = ver->FirmwareId == 255 ?
				DEVICE_MODE_NONE : ver->FirmwareId;
			coredev->modes_supported = ver->SupportedProtocols;
1032

1033 1034 1035 1036
			complete(&coredev->version_ex_done);
			break;
		}
		case MSG_SMS_INIT_DEVICE_RES:
1037
			sms_debug("MSG_SMS_INIT_DEVICE_RES");
1038 1039 1040
			complete(&coredev->init_device_done);
			break;
		case MSG_SW_RELOAD_START_RES:
1041
			sms_debug("MSG_SW_RELOAD_START_RES");
1042 1043 1044 1045 1046 1047
			complete(&coredev->reload_start_done);
			break;
		case MSG_SMS_DATA_DOWNLOAD_RES:
			complete(&coredev->data_download_done);
			break;
		case MSG_SW_RELOAD_EXEC_RES:
1048
			sms_debug("MSG_SW_RELOAD_EXEC_RES");
1049 1050
			break;
		case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
1051
			sms_debug("MSG_SMS_SWDOWNLOAD_TRIGGER_RES");
1052 1053 1054 1055 1056
			complete(&coredev->trigger_done);
			break;
		case MSG_SMS_SLEEP_RESUME_COMP_IND:
			complete(&coredev->resume_done);
			break;
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
		case MSG_SMS_GPIO_CONFIG_EX_RES:
			sms_debug("MSG_SMS_GPIO_CONFIG_EX_RES");
			complete(&coredev->gpio_configuration_done);
			break;
		case MSG_SMS_GPIO_SET_LEVEL_RES:
			sms_debug("MSG_SMS_GPIO_SET_LEVEL_RES");
			complete(&coredev->gpio_set_level_done);
			break;
		case MSG_SMS_GPIO_GET_LEVEL_RES:
		{
			u32 *msgdata = (u32 *) phdr;
			coredev->gpio_get_res = msgdata[1];
			sms_debug("MSG_SMS_GPIO_GET_LEVEL_RES gpio level %d",
					coredev->gpio_get_res);
			complete(&coredev->gpio_get_level_done);
			break;
		}
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085
		case MSG_SMS_START_IR_RES:
			complete(&coredev->ir_init_done);
			break;
		case MSG_SMS_IR_SAMPLES_IND:
			sms_ir_event(coredev,
				(const char *)
				((char *)phdr
				+ sizeof(struct SmsMsgHdr_ST)),
				(int)phdr->msgLength
				- sizeof(struct SmsMsgHdr_ST));
			break;

1086 1087
		default:
			break;
1088 1089 1090 1091
		}
		smscore_putbuffer(coredev, cb);
	}
}
1092
EXPORT_SYMBOL_GPL(smscore_onresponse);
1093 1094 1095 1096

/**
 * return pointer to next free buffer descriptor from core pool
 *
1097 1098
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1099 1100 1101
 *
 * @return pointer to descriptor on success, NULL on error.
 */
1102
struct smscore_buffer_t *smscore_getbuffer(struct smscore_device_t *coredev)
1103
{
1104
	struct smscore_buffer_t *cb = NULL;
1105 1106
	unsigned long flags;

1107 1108
	DEFINE_WAIT(wait);

1109 1110
	spin_lock_irqsave(&coredev->bufferslock, flags);

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
	/* This function must return a valid buffer, since the buffer list is
	 * finite, we check that there is an available buffer, if not, we wait
	 * until such buffer become available.
	 */

	prepare_to_wait(&coredev->buffer_mng_waitq, &wait, TASK_INTERRUPTIBLE);

	if (list_empty(&coredev->buffers))
		schedule();

	finish_wait(&coredev->buffer_mng_waitq, &wait);

	cb = (struct smscore_buffer_t *) coredev->buffers.next;
	list_del(&cb->entry);
1125 1126 1127 1128 1129

	spin_unlock_irqrestore(&coredev->bufferslock, flags);

	return cb;
}
1130
EXPORT_SYMBOL_GPL(smscore_getbuffer);
1131 1132 1133 1134

/**
 * return buffer descriptor to a pool
 *
1135 1136
 * @param coredev pointer to a coredev object returned by
 *                smscore_register_device
1137 1138 1139
 * @param cb pointer buffer descriptor
 *
 */
1140
void smscore_putbuffer(struct smscore_device_t *coredev,
1141 1142
		struct smscore_buffer_t *cb) {
	wake_up_interruptible(&coredev->buffer_mng_waitq);
1143 1144
	list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
}
1145
EXPORT_SYMBOL_GPL(smscore_putbuffer);
1146

1147 1148 1149
static int smscore_validate_client(struct smscore_device_t *coredev,
				   struct smscore_client_t *client,
				   int data_type, int id)
1150
{
1151 1152
	struct smscore_idlist_t *listentry;
	struct smscore_client_t *registered_client;
1153

1154
	if (!client) {
1155
		sms_err("bad parameter.");
1156 1157 1158
		return -EFAULT;
	}
	registered_client = smscore_find_client(coredev, data_type, id);
1159
	if (registered_client == client)
1160
		return 0;
1161

1162
	if (registered_client) {
1163
		sms_err("The msg ID already registered to another client.");
1164 1165
		return -EEXIST;
	}
1166
	listentry = kzalloc(sizeof(struct smscore_idlist_t), GFP_KERNEL);
1167
	if (!listentry) {
1168
		sms_err("Can't allocate memory for client id.");
1169
		return -ENOMEM;
1170 1171 1172
	}
	listentry->id = id;
	listentry->data_type = data_type;
1173 1174
	list_add_locked(&listentry->entry, &client->idlist,
			&coredev->clientslock);
1175 1176 1177 1178 1179 1180 1181 1182 1183
	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
1184 1185
 * @param onresponse_handler client handler that is called to
 *                           process incoming messages
1186 1187 1188 1189 1190 1191
 * @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.
 */
1192 1193 1194
int smscore_register_client(struct smscore_device_t *coredev,
			    struct smsclient_params_t *params,
			    struct smscore_client_t **client)
1195
{
1196
	struct smscore_client_t *newclient;
1197 1198 1199
	/* check that no other channel with same parameters exists */
	if (smscore_find_client(coredev, params->data_type,
				params->initial_id)) {
1200
		sms_err("Client already exist.");
1201
		return -EEXIST;
1202
	}
1203

1204
	newclient = kzalloc(sizeof(struct smscore_client_t), GFP_KERNEL);
1205
	if (!newclient) {
1206
		sms_err("Failed to allocate memory for client.");
1207
		return -ENOMEM;
1208 1209
	}

1210
	INIT_LIST_HEAD(&newclient->idlist);
1211 1212 1213 1214
	newclient->coredev = coredev;
	newclient->onresponse_handler = params->onresponse_handler;
	newclient->onremove_handler = params->onremove_handler;
	newclient->context = params->context;
1215 1216 1217 1218
	list_add_locked(&newclient->entry, &coredev->clients,
			&coredev->clientslock);
	smscore_validate_client(coredev, newclient, params->data_type,
				params->initial_id);
1219
	*client = newclient;
1220 1221
	sms_debug("%p %d %d", params->context, params->data_type,
		  params->initial_id);
1222 1223 1224

	return 0;
}
1225
EXPORT_SYMBOL_GPL(smscore_register_client);
1226 1227 1228 1229

/**
 * frees smsclient object and all subclients associated with it
 *
1230 1231
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1232 1233
 *
 */
1234
void smscore_unregister_client(struct smscore_client_t *client)
1235
{
1236
	struct smscore_device_t *coredev = client->coredev;
1237 1238 1239 1240 1241
	unsigned long flags;

	spin_lock_irqsave(&coredev->clientslock, flags);


1242
	while (!list_empty(&client->idlist)) {
1243 1244
		struct smscore_idlist_t *identry =
			(struct smscore_idlist_t *) client->idlist.next;
1245 1246
		list_del(&identry->entry);
		kfree(identry);
1247 1248
	}

1249
	sms_info("%p", client->context);
1250 1251 1252 1253 1254 1255

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

	spin_unlock_irqrestore(&coredev->clientslock, flags);
}
1256
EXPORT_SYMBOL_GPL(smscore_unregister_client);
1257 1258 1259 1260 1261

/**
 * verifies that source id is not taken by another client,
 * calls device handler to send requests to the device
 *
1262 1263
 * @param client pointer to smsclient object returned by
 *               smscore_register_client
1264 1265 1266 1267 1268
 * @param buffer pointer to a request buffer
 * @param size size (in bytes) of request buffer
 *
 * @return 0 on success, <0 on error.
 */
1269 1270
int smsclient_sendrequest(struct smscore_client_t *client,
			  void *buffer, size_t size)
1271
{
1272 1273
	struct smscore_device_t *coredev;
	struct SmsMsgHdr_ST *phdr = (struct SmsMsgHdr_ST *) buffer;
1274 1275
	int rc;

1276
	if (client == NULL) {
1277
		sms_err("Got NULL client");
1278 1279 1280 1281
		return -EINVAL;
	}

	coredev = client->coredev;
1282

1283 1284
	/* check that no other channel with same id exists */
	if (coredev == NULL) {
1285
		sms_err("Got NULL coredev");
1286 1287 1288
		return -EINVAL;
	}

1289 1290
	rc = smscore_validate_client(client->coredev, client, 0,
				     phdr->msgSrcId);
1291 1292 1293 1294 1295
	if (rc < 0)
		return rc;

	return coredev->sendrequest_handler(coredev->context, buffer, size);
}
1296
EXPORT_SYMBOL_GPL(smsclient_sendrequest);
1297 1298


1299
/* old GPIO managments implementation */
1300
int smscore_configure_gpio(struct smscore_device_t *coredev, u32 pin,
1301
			   struct smscore_config_gpio *pinconfig)
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
{
	struct {
		struct SmsMsgHdr_ST hdr;
		u32 data[6];
	} msg;

	if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
		msg.hdr.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
		msg.hdr.msgDstId = HIF_TASK;
		msg.hdr.msgFlags = 0;
		msg.hdr.msgType  = MSG_SMS_GPIO_CONFIG_EX_REQ;
		msg.hdr.msgLength = sizeof(msg);

		msg.data[0] = pin;
		msg.data[1] = pinconfig->pullupdown;

		/* Convert slew rate for Nova: Fast(0) = 3 / Slow(1) = 0; */
		msg.data[2] = pinconfig->outputslewrate == 0 ? 3 : 0;

		switch (pinconfig->outputdriving) {
		case SMS_GPIO_OUTPUTDRIVING_16mA:
			msg.data[3] = 7; /* Nova - 16mA */
			break;
		case SMS_GPIO_OUTPUTDRIVING_12mA:
			msg.data[3] = 5; /* Nova - 11mA */
			break;
		case SMS_GPIO_OUTPUTDRIVING_8mA:
			msg.data[3] = 3; /* Nova - 7mA */
			break;
		case SMS_GPIO_OUTPUTDRIVING_4mA:
		default:
			msg.data[3] = 2; /* Nova - 4mA */
			break;
		}

		msg.data[4] = pinconfig->direction;
		msg.data[5] = 0;
	} else /* TODO: SMS_DEVICE_FAMILY1 */
		return -EINVAL;

	return coredev->sendrequest_handler(coredev->context,
					    &msg, sizeof(msg));
}

int smscore_set_gpio(struct smscore_device_t *coredev, u32 pin, int level)
{
	struct {
		struct SmsMsgHdr_ST hdr;
		u32 data[3];
	} msg;

	if (pin > MAX_GPIO_PIN_NUMBER)
		return -EINVAL;

	msg.hdr.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	msg.hdr.msgDstId = HIF_TASK;
	msg.hdr.msgFlags = 0;
	msg.hdr.msgType  = MSG_SMS_GPIO_SET_LEVEL_REQ;
	msg.hdr.msgLength = sizeof(msg);

	msg.data[0] = pin;
	msg.data[1] = level ? 1 : 0;
	msg.data[2] = 0;

	return coredev->sendrequest_handler(coredev->context,
					    &msg, sizeof(msg));
}

1370
/* new GPIO management implementation */
1371 1372 1373 1374 1375
static int GetGpioPinParams(u32 PinNum, u32 *pTranslatedPinNum,
		u32 *pGroupNum, u32 *pGroupCfg) {

	*pGroupCfg = 1;

1376
	if (PinNum <= 1)	{
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424
		*pTranslatedPinNum = 0;
		*pGroupNum = 9;
		*pGroupCfg = 2;
	} else if (PinNum >= 2 && PinNum <= 6) {
		*pTranslatedPinNum = 2;
		*pGroupNum = 0;
		*pGroupCfg = 2;
	} else if (PinNum >= 7 && PinNum <= 11) {
		*pTranslatedPinNum = 7;
		*pGroupNum = 1;
	} else if (PinNum >= 12 && PinNum <= 15) {
		*pTranslatedPinNum = 12;
		*pGroupNum = 2;
		*pGroupCfg = 3;
	} else if (PinNum == 16) {
		*pTranslatedPinNum = 16;
		*pGroupNum = 23;
	} else if (PinNum >= 17 && PinNum <= 24) {
		*pTranslatedPinNum = 17;
		*pGroupNum = 3;
	} else if (PinNum == 25) {
		*pTranslatedPinNum = 25;
		*pGroupNum = 6;
	} else if (PinNum >= 26 && PinNum <= 28) {
		*pTranslatedPinNum = 26;
		*pGroupNum = 4;
	} else if (PinNum == 29) {
		*pTranslatedPinNum = 29;
		*pGroupNum = 5;
		*pGroupCfg = 2;
	} else if (PinNum == 30) {
		*pTranslatedPinNum = 30;
		*pGroupNum = 8;
	} else if (PinNum == 31) {
		*pTranslatedPinNum = 31;
		*pGroupNum = 17;
	} else
		return -1;

	*pGroupCfg <<= 24;

	return 0;
}

int smscore_gpio_configure(struct smscore_device_t *coredev, u8 PinNum,
		struct smscore_gpio_config *pGpioConfig) {

	u32 totalLen;
1425 1426
	u32 TranslatedPinNum = 0;
	u32 GroupNum = 0;
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601
	u32 ElectricChar;
	u32 groupCfg;
	void *buffer;
	int rc;

	struct SetGpioMsg {
		struct SmsMsgHdr_ST xMsgHeader;
		u32 msgData[6];
	} *pMsg;


	if (PinNum > MAX_GPIO_PIN_NUMBER)
		return -EINVAL;

	if (pGpioConfig == NULL)
		return -EINVAL;

	totalLen = sizeof(struct SmsMsgHdr_ST) + (sizeof(u32) * 6);

	buffer = kmalloc(totalLen + SMS_DMA_ALIGNMENT,
			GFP_KERNEL | GFP_DMA);
	if (!buffer)
		return -ENOMEM;

	pMsg = (struct SetGpioMsg *) SMS_ALIGN_ADDRESS(buffer);

	pMsg->xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	pMsg->xMsgHeader.msgDstId = HIF_TASK;
	pMsg->xMsgHeader.msgFlags = 0;
	pMsg->xMsgHeader.msgLength = (u16) totalLen;
	pMsg->msgData[0] = PinNum;

	if (!(coredev->device_flags & SMS_DEVICE_FAMILY2)) {
		pMsg->xMsgHeader.msgType = MSG_SMS_GPIO_CONFIG_REQ;
		if (GetGpioPinParams(PinNum, &TranslatedPinNum, &GroupNum,
				&groupCfg) != 0)
			return -EINVAL;

		pMsg->msgData[1] = TranslatedPinNum;
		pMsg->msgData[2] = GroupNum;
		ElectricChar = (pGpioConfig->PullUpDown)
				| (pGpioConfig->InputCharacteristics << 2)
				| (pGpioConfig->OutputSlewRate << 3)
				| (pGpioConfig->OutputDriving << 4);
		pMsg->msgData[3] = ElectricChar;
		pMsg->msgData[4] = pGpioConfig->Direction;
		pMsg->msgData[5] = groupCfg;
	} else {
		pMsg->xMsgHeader.msgType = MSG_SMS_GPIO_CONFIG_EX_REQ;
		pMsg->msgData[1] = pGpioConfig->PullUpDown;
		pMsg->msgData[2] = pGpioConfig->OutputSlewRate;
		pMsg->msgData[3] = pGpioConfig->OutputDriving;
		pMsg->msgData[4] = pGpioConfig->Direction;
		pMsg->msgData[5] = 0;
	}

	smsendian_handle_tx_message((struct SmsMsgHdr_ST *)pMsg);
	rc = smscore_sendrequest_and_wait(coredev, pMsg, totalLen,
			&coredev->gpio_configuration_done);

	if (rc != 0) {
		if (rc == -ETIME)
			sms_err("smscore_gpio_configure timeout");
		else
			sms_err("smscore_gpio_configure error");
	}
	kfree(buffer);

	return rc;
}

int smscore_gpio_set_level(struct smscore_device_t *coredev, u8 PinNum,
		u8 NewLevel) {

	u32 totalLen;
	int rc;
	void *buffer;

	struct SetGpioMsg {
		struct SmsMsgHdr_ST xMsgHeader;
		u32 msgData[3]; /* keep it 3 ! */
	} *pMsg;

	if ((NewLevel > 1) || (PinNum > MAX_GPIO_PIN_NUMBER) ||
			(PinNum > MAX_GPIO_PIN_NUMBER))
		return -EINVAL;

	totalLen = sizeof(struct SmsMsgHdr_ST) +
			(3 * sizeof(u32)); /* keep it 3 ! */

	buffer = kmalloc(totalLen + SMS_DMA_ALIGNMENT,
			GFP_KERNEL | GFP_DMA);
	if (!buffer)
		return -ENOMEM;

	pMsg = (struct SetGpioMsg *) SMS_ALIGN_ADDRESS(buffer);

	pMsg->xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	pMsg->xMsgHeader.msgDstId = HIF_TASK;
	pMsg->xMsgHeader.msgFlags = 0;
	pMsg->xMsgHeader.msgType = MSG_SMS_GPIO_SET_LEVEL_REQ;
	pMsg->xMsgHeader.msgLength = (u16) totalLen;
	pMsg->msgData[0] = PinNum;
	pMsg->msgData[1] = NewLevel;

	/* Send message to SMS */
	smsendian_handle_tx_message((struct SmsMsgHdr_ST *)pMsg);
	rc = smscore_sendrequest_and_wait(coredev, pMsg, totalLen,
			&coredev->gpio_set_level_done);

	if (rc != 0) {
		if (rc == -ETIME)
			sms_err("smscore_gpio_set_level timeout");
		else
			sms_err("smscore_gpio_set_level error");
	}
	kfree(buffer);

	return rc;
}

int smscore_gpio_get_level(struct smscore_device_t *coredev, u8 PinNum,
		u8 *level) {

	u32 totalLen;
	int rc;
	void *buffer;

	struct SetGpioMsg {
		struct SmsMsgHdr_ST xMsgHeader;
		u32 msgData[2];
	} *pMsg;


	if (PinNum > MAX_GPIO_PIN_NUMBER)
		return -EINVAL;

	totalLen = sizeof(struct SmsMsgHdr_ST) + (2 * sizeof(u32));

	buffer = kmalloc(totalLen + SMS_DMA_ALIGNMENT,
			GFP_KERNEL | GFP_DMA);
	if (!buffer)
		return -ENOMEM;

	pMsg = (struct SetGpioMsg *) SMS_ALIGN_ADDRESS(buffer);

	pMsg->xMsgHeader.msgSrcId = DVBT_BDA_CONTROL_MSG_ID;
	pMsg->xMsgHeader.msgDstId = HIF_TASK;
	pMsg->xMsgHeader.msgFlags = 0;
	pMsg->xMsgHeader.msgType = MSG_SMS_GPIO_GET_LEVEL_REQ;
	pMsg->xMsgHeader.msgLength = (u16) totalLen;
	pMsg->msgData[0] = PinNum;
	pMsg->msgData[1] = 0;

	/* Send message to SMS */
	smsendian_handle_tx_message((struct SmsMsgHdr_ST *)pMsg);
	rc = smscore_sendrequest_and_wait(coredev, pMsg, totalLen,
			&coredev->gpio_get_level_done);

	if (rc != 0) {
		if (rc == -ETIME)
			sms_err("smscore_gpio_get_level timeout");
		else
			sms_err("smscore_gpio_get_level error");
	}
	kfree(buffer);

	/* Its a race between other gpio_get_level() and the copy of the single
	 * global 'coredev->gpio_get_res' to  the function's variable 'level'
	 */
	*level = coredev->gpio_get_res;

	return rc;
}

1602
static int __init smscore_module_init(void)
1603
{
1604
	int rc = 0;
1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615

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

	return rc;
}

1616
static void __exit smscore_module_exit(void)
1617 1618
{
	kmutex_lock(&g_smscore_deviceslock);
1619
	while (!list_empty(&g_smscore_notifyees)) {
1620 1621 1622
		struct smscore_device_notifyee_t *notifyee =
			(struct smscore_device_notifyee_t *)
				g_smscore_notifyees.next;
1623 1624 1625 1626 1627 1628 1629

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

	kmutex_lock(&g_smscore_registrylock);
1630
	while (!list_empty(&g_smscore_registry)) {
1631 1632 1633
		struct smscore_registry_entry_t *entry =
			(struct smscore_registry_entry_t *)
				g_smscore_registry.next;
1634 1635 1636 1637 1638 1639

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

1640
	sms_debug("");
1641 1642 1643 1644 1645
}

module_init(smscore_module_init);
module_exit(smscore_module_exit);

1646 1647
MODULE_DESCRIPTION("Siano MDTV Core module");
MODULE_AUTHOR("Siano Mobile Silicon, Inc. (uris@siano-ms.com)");
1648
MODULE_LICENSE("GPL");