2 * Siano core API module
4 * This file contains implementation for the interface to sms core component
6 * author: Anatoly Greenblat
8 * Copyright (c), 2005-2008 Siano Mobile Silicon, Inc.
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 3 as
12 * published by the Free Software Foundation;
14 * Software distributed under the License is distributed on an "AS IS"
15 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
17 * See the GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/dma-mapping.h>
29 #include <linux/delay.h>
32 #include <linux/firmware.h>
34 #include "smscoreapi.h"
36 #define PERROR(fmt, args...)\
37 printk(KERN_ERR "smscore error: line %d- %s(): " fmt, \
38 __LINE__, __func__, ## args)
42 # define PWARNING(fmt, args...) printk(KERN_INFO "smscore warning: " \
43 "line %d- %s(): " fmt, \
44 __LINE__, __func__, ## args)
45 #undef PDEBUG /* undef it, just in case */
46 # define PDEBUG(fmt, args...) printk(KERN_INFO "smscore - %s(): " fmt, \
48 #else /*SMSCORE_DEBUG*/
49 #define PDEBUG(fmt, args...)
50 #define PWARNING(fmt, args...)
53 typedef struct _smscore_device_notifyee
55 struct list_head entry;
57 } smscore_device_notifyee_t;
59 typedef struct _smscore_subclient
61 struct list_head entry;
66 typedef struct _smscore_client
68 struct list_head entry;
69 smscore_device_t *coredev;
71 struct list_head idlist;
72 onresponse_t onresponse_handler;
73 onremove_t onremove_handler;
78 typedef struct _smscore_device
80 struct list_head entry;
82 struct list_head clients;
83 struct list_head subclients;
84 spinlock_t clientslock;
86 struct list_head buffers;
87 spinlock_t bufferslock;
91 int common_buffer_size;
92 dma_addr_t common_buffer_phys;
95 struct device *device;
98 unsigned long device_flags;
100 setmode_t setmode_handler;
101 detectmode_t detectmode_handler;
102 sendrequest_t sendrequest_handler;
103 preload_t preload_handler;
104 postload_t postload_handler;
106 int mode, modes_supported;
108 struct completion version_ex_done, data_download_done, trigger_done;
109 struct completion init_device_done, reload_start_done, resume_done;
110 } *psmscore_device_t;
112 typedef struct _smscore_registry_entry
114 struct list_head entry;
117 sms_device_type_st type;
118 } smscore_registry_entry_t;
120 struct list_head g_smscore_notifyees;
121 struct list_head g_smscore_devices;
122 kmutex_t g_smscore_deviceslock;
124 struct list_head g_smscore_registry;
125 kmutex_t g_smscore_registrylock;
127 static int default_mode = 1;
129 module_param(default_mode, int, 0644);
130 MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");
132 static smscore_registry_entry_t *smscore_find_registry(char *devpath)
134 smscore_registry_entry_t *entry;
135 struct list_head *next;
137 kmutex_lock(&g_smscore_registrylock);
138 for (next = g_smscore_registry.next;
139 next != &g_smscore_registry;
141 entry = (smscore_registry_entry_t *) next;
142 if (!strcmp(entry->devpath, devpath)) {
143 kmutex_unlock(&g_smscore_registrylock);
147 entry = (smscore_registry_entry_t *)
148 kmalloc(sizeof(smscore_registry_entry_t), GFP_KERNEL);
150 entry->mode = default_mode;
151 strcpy(entry->devpath, devpath);
152 list_add(&entry->entry, &g_smscore_registry);
154 printk(KERN_ERR "%s failed to create smscore_registry.\n",
156 kmutex_unlock(&g_smscore_registrylock);
160 int smscore_registry_getmode(char *devpath)
162 smscore_registry_entry_t *entry;
164 entry = smscore_find_registry(devpath);
168 printk(KERN_ERR "%s No registry found.\n", __func__);
173 sms_device_type_st smscore_registry_gettype(char *devpath)
175 smscore_registry_entry_t *entry;
177 entry = smscore_find_registry(devpath);
181 printk(KERN_ERR "%s No registry found.\n", __func__);
186 void smscore_registry_setmode(char *devpath, int mode)
188 smscore_registry_entry_t *entry;
190 entry = smscore_find_registry(devpath);
194 printk(KERN_ERR "%s No registry found.\n", __func__);
197 void smscore_registry_settype(char *devpath, sms_device_type_st type)
199 smscore_registry_entry_t *entry;
201 entry = smscore_find_registry(devpath);
205 printk(KERN_ERR "%s No registry found.\n", __func__);
210 void list_add_locked(struct list_head *new, struct list_head *head,
215 spin_lock_irqsave(lock, flags);
219 spin_unlock_irqrestore(lock, flags);
223 * register a client callback that called when device plugged in/unplugged
224 * NOTE: if devices exist callback is called immediately for each device
226 * @param hotplug callback
228 * @return 0 on success, <0 on error.
230 int smscore_register_hotplug(hotplug_t hotplug)
232 smscore_device_notifyee_t *notifyee;
233 struct list_head *next, *first;
236 kmutex_lock(&g_smscore_deviceslock);
238 notifyee = kmalloc(sizeof(smscore_device_notifyee_t), GFP_KERNEL);
240 /* now notify callback about existing devices */
241 first = &g_smscore_devices;
242 for (next = first->next;
243 next != first && !rc;
245 smscore_device_t *coredev = (smscore_device_t *) next;
246 rc = hotplug(coredev, coredev->device, 1);
250 notifyee->hotplug = hotplug;
251 list_add(¬ifyee->entry, &g_smscore_notifyees);
257 kmutex_unlock(&g_smscore_deviceslock);
263 * unregister a client callback that called when device plugged in/unplugged
265 * @param hotplug callback
268 void smscore_unregister_hotplug(hotplug_t hotplug)
270 struct list_head *next, *first;
272 kmutex_lock(&g_smscore_deviceslock);
274 first = &g_smscore_notifyees;
276 for (next = first->next; next != first;) {
277 smscore_device_notifyee_t *notifyee =
278 (smscore_device_notifyee_t *) next;
281 if (notifyee->hotplug == hotplug) {
282 list_del(¬ifyee->entry);
287 kmutex_unlock(&g_smscore_deviceslock);
290 void smscore_notify_clients(smscore_device_t *coredev)
292 smscore_client_t *client;
294 /* the client must call smscore_unregister_client from remove handler */
295 while (!list_empty(&coredev->clients)) {
296 client = (smscore_client_t *) coredev->clients.next;
297 client->onremove_handler(client->context);
301 int smscore_notify_callbacks(smscore_device_t *coredev, struct device *device,
304 struct list_head *next, *first;
307 /* note: must be called under g_deviceslock */
309 first = &g_smscore_notifyees;
311 for (next = first->next; next != first; next = next->next) {
312 rc = ((smscore_device_notifyee_t *) next)->
313 hotplug(coredev, device, arrival);
321 smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
322 dma_addr_t common_buffer_phys)
324 smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
326 printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
331 cb->offset_in_common = buffer - (u8 *) common_buffer;
332 cb->phys = common_buffer_phys + cb->offset_in_common;
338 * creates coredev object for a device, prepares buffers,
339 * creates buffer mappings, notifies registered hotplugs about new device.
341 * @param params device pointer to struct with device specific parameters
343 * @param coredev pointer to a value that receives created coredev object
345 * @return 0 on success, <0 on error.
347 int smscore_register_device(smsdevice_params_t *params,
348 smscore_device_t **coredev)
350 smscore_device_t *dev;
353 dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
355 printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
359 /* init list entry so it could be safe in smscore_unregister_device */
360 INIT_LIST_HEAD(&dev->entry);
363 INIT_LIST_HEAD(&dev->clients);
364 INIT_LIST_HEAD(&dev->buffers);
367 spin_lock_init(&dev->clientslock);
368 spin_lock_init(&dev->bufferslock);
370 /* init completion events */
371 init_completion(&dev->version_ex_done);
372 init_completion(&dev->data_download_done);
373 init_completion(&dev->trigger_done);
374 init_completion(&dev->init_device_done);
375 init_completion(&dev->reload_start_done);
376 init_completion(&dev->resume_done);
378 /* alloc common buffer */
379 dev->common_buffer_size = params->buffer_size * params->num_buffers;
380 dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
381 &dev->common_buffer_phys,
382 GFP_KERNEL | GFP_DMA);
383 if (!dev->common_buffer) {
384 smscore_unregister_device(dev);
388 /* prepare dma buffers */
389 for (buffer = dev->common_buffer;
390 dev->num_buffers < params->num_buffers;
391 dev->num_buffers++, buffer += params->buffer_size) {
392 smscore_buffer_t *cb =
393 smscore_createbuffer(buffer, dev->common_buffer,
394 dev->common_buffer_phys);
396 smscore_unregister_device(dev);
400 smscore_putbuffer(dev, cb);
403 printk(KERN_INFO "%s allocated %d buffers\n",
404 __func__, dev->num_buffers);
406 dev->mode = DEVICE_MODE_NONE;
407 dev->context = params->context;
408 dev->device = params->device;
409 dev->setmode_handler = params->setmode_handler;
410 dev->detectmode_handler = params->detectmode_handler;
411 dev->sendrequest_handler = params->sendrequest_handler;
412 dev->preload_handler = params->preload_handler;
413 dev->postload_handler = params->postload_handler;
415 dev->device_flags = params->flags;
416 strcpy(dev->devpath, params->devpath);
418 smscore_registry_settype(dev->devpath, params->device_type);
420 /* add device to devices list */
421 kmutex_lock(&g_smscore_deviceslock);
422 list_add(&dev->entry, &g_smscore_devices);
423 kmutex_unlock(&g_smscore_deviceslock);
427 printk(KERN_INFO "%s device %p created\n", __func__, dev);
433 * sets initial device mode and notifies client hotplugs that device is ready
435 * @param coredev pointer to a coredev object returned by
436 * smscore_register_device
438 * @return 0 on success, <0 on error.
440 int smscore_start_device(smscore_device_t *coredev)
442 int rc = smscore_set_device_mode(
443 coredev, smscore_registry_getmode(coredev->devpath));
445 printk(KERN_INFO "%s set device mode faile , rc %d\n",
450 kmutex_lock(&g_smscore_deviceslock);
452 rc = smscore_notify_callbacks(coredev, coredev->device, 1);
454 printk(KERN_INFO "%s device %p started, rc %d\n",
455 __func__, coredev, rc);
457 kmutex_unlock(&g_smscore_deviceslock);
462 int smscore_sendrequest_and_wait(smscore_device_t *coredev, void *buffer,
463 size_t size, struct completion *completion)
465 int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
467 printk(KERN_INFO "%s sendrequest returned error %d\n",
472 return wait_for_completion_timeout(completion,
473 msecs_to_jiffies(10000)) ?
477 int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer,
480 SmsFirmware_ST *firmware = (SmsFirmware_ST *) buffer;
482 u32 mem_address = firmware->StartAddress;
483 u8 *payload = firmware->Payload;
486 printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n",
487 __func__, mem_address, firmware->Length);
488 if (coredev->preload_handler) {
489 rc = coredev->preload_handler(coredev->context);
494 /* PAGE_SIZE buffer shall be enough and dma aligned */
495 msg = (SmsMsgHdr_ST *) kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
499 if (coredev->mode != DEVICE_MODE_NONE) {
500 PDEBUG("Sending reload command\n");
501 SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
502 sizeof(SmsMsgHdr_ST));
503 rc = smscore_sendrequest_and_wait(coredev, msg,
505 &coredev->reload_start_done);
506 mem_address = *(u32 *) &payload[20];
509 while (size && rc >= 0) {
510 SmsDataDownload_ST *DataMsg = (SmsDataDownload_ST *) msg;
511 int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);
513 SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
514 (u16)(sizeof(SmsMsgHdr_ST) +
515 sizeof(u32) + payload_size));
517 DataMsg->MemAddr = mem_address;
518 memcpy(DataMsg->Payload, payload, payload_size);
520 if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
521 (coredev->mode == DEVICE_MODE_NONE))
522 rc = coredev->sendrequest_handler(
523 coredev->context, DataMsg,
524 DataMsg->xMsgHeader.msgLength);
526 rc = smscore_sendrequest_and_wait(
528 DataMsg->xMsgHeader.msgLength,
529 &coredev->data_download_done);
531 payload += payload_size;
532 size -= payload_size;
533 mem_address += payload_size;
537 if (coredev->mode == DEVICE_MODE_NONE) {
538 SmsMsgData_ST *TriggerMsg = (SmsMsgData_ST *) msg;
540 SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
541 sizeof(SmsMsgHdr_ST) +
544 TriggerMsg->msgData[0] = firmware->StartAddress;
546 TriggerMsg->msgData[1] = 5; /* Priority */
547 TriggerMsg->msgData[2] = 0x200; /* Stack size */
548 TriggerMsg->msgData[3] = 0; /* Parameter */
549 TriggerMsg->msgData[4] = 4; /* Task ID */
551 if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
552 rc = coredev->sendrequest_handler(
553 coredev->context, TriggerMsg,
554 TriggerMsg->xMsgHeader.msgLength);
557 rc = smscore_sendrequest_and_wait(
559 TriggerMsg->xMsgHeader.msgLength,
560 &coredev->trigger_done);
562 SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
563 sizeof(SmsMsgHdr_ST));
565 rc = coredev->sendrequest_handler(coredev->context,
566 msg, msg->msgLength);
571 printk(KERN_DEBUG "%s rc=%d, postload=%p \n", __func__, rc,
572 coredev->postload_handler);
576 return ((rc >= 0) && coredev->postload_handler) ?
577 coredev->postload_handler(coredev->context) :
582 * loads specified firmware into a buffer and calls device loadfirmware_handler
584 * @param coredev pointer to a coredev object returned by
585 * smscore_register_device
586 * @param filename null-terminated string specifies firmware file name
587 * @param loadfirmware_handler device handler that loads firmware
589 * @return 0 on success, <0 on error.
591 int smscore_load_firmware_from_file(smscore_device_t *coredev, char *filename,
592 loadfirmware_t loadfirmware_handler)
596 const struct firmware *fw;
599 if (loadfirmware_handler == NULL && !(coredev->device_flags &
603 rc = request_firmware(&fw, filename, coredev->device);
605 printk(KERN_INFO "%s failed to open \"%s\"\n",
609 printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
611 fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
612 GFP_KERNEL | GFP_DMA);
614 memcpy(fw_buffer, fw->data, fw->size);
616 rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
617 smscore_load_firmware_family2(coredev,
620 loadfirmware_handler(coredev->context,
621 fw_buffer, fw->size);
625 printk(KERN_INFO "%s failed to allocate firmware buffer\n",
630 release_firmware(fw);
635 int smscore_load_firmware_from_buffer(smscore_device_t *coredev, u8 *buffer,
636 int size, int new_mode)
638 PERROR("Feature not implemented yet\n");
643 * notifies all clients registered with the device, notifies hotplugs,
644 * frees all buffers and coredev object
646 * @param coredev pointer to a coredev object returned by
647 * smscore_register_device
649 * @return 0 on success, <0 on error.
651 void smscore_unregister_device(smscore_device_t *coredev)
653 smscore_buffer_t *cb;
657 kmutex_lock(&g_smscore_deviceslock);
659 smscore_notify_clients(coredev);
660 smscore_notify_callbacks(coredev, NULL, 0);
662 /* at this point all buffers should be back
663 * onresponse must no longer be called */
666 while ((cb = smscore_getbuffer(coredev))) {
670 if (num_buffers == coredev->num_buffers)
673 printk(KERN_INFO "%s exiting although "
674 "not all buffers released.\n", __func__);
678 printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
679 coredev->num_buffers - num_buffers);
683 printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
685 if (coredev->common_buffer)
686 dma_free_coherent(NULL, coredev->common_buffer_size,
687 coredev->common_buffer,
688 coredev->common_buffer_phys);
690 list_del(&coredev->entry);
693 kmutex_unlock(&g_smscore_deviceslock);
695 printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
698 int smscore_detect_mode(smscore_device_t *coredev)
700 void *buffer = kmalloc(sizeof(SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
701 GFP_KERNEL | GFP_DMA);
702 SmsMsgHdr_ST *msg = (SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
708 SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ, sizeof(SmsMsgHdr_ST));
710 rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
711 &coredev->version_ex_done);
713 printk(KERN_ERR "%s: MSG_SMS_GET_VERSION_EX_REQ "
714 "failed first try\n", __func__);
716 if (wait_for_completion_timeout(&coredev->resume_done,
717 msecs_to_jiffies(5000))) {
718 rc = smscore_sendrequest_and_wait(
719 coredev, msg, msg->msgLength,
720 &coredev->version_ex_done);
722 printk(KERN_ERR "%s: "
723 "MSG_SMS_GET_VERSION_EX_REQ failed "
724 "second try, rc %d\n", __func__, rc);
734 char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
735 /*Stellar NOVA A0 Nova B0 VEGA*/
736 /*DVBT*/ {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
737 /*DVBH*/ {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
738 /*TDMB*/ {"none", "tdmb_nova_12mhz.inp", "none", "none"},
739 /*DABIP*/ {"none", "none", "none", "none"},
740 /*BDA*/ {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
741 /*ISDBT*/ {"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
742 /*ISDBTBDA*/{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
743 /*CMMB*/ {"none", "none", "none", "cmmb_vega_12mhz.inp"}
748 * calls device handler to change mode of operation
749 * NOTE: stellar/usb may disconnect when changing mode
751 * @param coredev pointer to a coredev object returned by
752 * smscore_register_device
753 * @param mode requested mode of operation
755 * @return 0 on success, <0 on error.
757 int smscore_set_device_mode(smscore_device_t *coredev, int mode)
761 sms_device_type_st type;
763 PDEBUG("set device mode to %d\n", mode);
764 if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
765 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
766 printk(KERN_INFO "%s invalid mode specified %d\n",
771 smscore_registry_setmode(coredev->devpath, mode);
773 if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
774 rc = smscore_detect_mode(coredev);
776 printk(KERN_INFO "%s mode detect failed %d\n",
782 if (coredev->mode == mode) {
783 printk(KERN_INFO "%s device mode %d already set\n",
788 if (!(coredev->modes_supported & (1 << mode))) {
789 type = smscore_registry_gettype(coredev->devpath);
790 rc = smscore_load_firmware_from_file(
791 coredev, smscore_fw_lkup[mode][type], NULL);
793 printk(KERN_INFO "%s load firmware "
794 "failed %d\n", __func__, rc);
798 printk(KERN_INFO "%s mode %d supported by running "
799 "firmware\n", __func__, mode);
801 buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT,
802 GFP_KERNEL | GFP_DMA);
805 (SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);
807 SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
808 sizeof(SmsMsgData_ST));
809 msg->msgData[0] = mode;
811 rc = smscore_sendrequest_and_wait(
812 coredev, msg, msg->xMsgHeader.msgLength,
813 &coredev->init_device_done);
817 printk(KERN_INFO "%s Could not allocate buffer for "
818 "init device message.\n", __func__);
822 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
823 printk(KERN_INFO "%s invalid mode specified %d\n",
828 smscore_registry_setmode(coredev->devpath, mode);
830 if (coredev->detectmode_handler)
831 coredev->detectmode_handler(coredev->context,
834 if (coredev->mode != mode && coredev->setmode_handler)
835 rc = coredev->setmode_handler(coredev->context, mode);
839 coredev->mode = mode;
840 coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
844 printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
849 * calls device handler to get current mode of operation
851 * @param coredev pointer to a coredev object returned by
852 * smscore_register_device
854 * @return current mode
856 int smscore_get_device_mode(smscore_device_t *coredev)
858 return coredev->mode;
862 * find client by response id & type within the clients list.
863 * return client handle or NULL.
865 * @param coredev pointer to a coredev object returned by
866 * smscore_register_device
867 * @param data_type client data type (SMS_DONT_CARE for all types)
868 * @param id client id (SMS_DONT_CARE for all id)
871 smscore_client_t *smscore_find_client(smscore_device_t *coredev,
872 int data_type, int id)
874 smscore_client_t *client = NULL;
875 struct list_head *next, *first;
877 struct list_head *firstid, *nextid;
880 spin_lock_irqsave(&coredev->clientslock, flags);
881 first = &coredev->clients;
882 for (next = first->next;
883 (next != first) && !client;
885 firstid = &((smscore_client_t *)next)->idlist;
886 for (nextid = firstid->next;
888 nextid = nextid->next) {
889 if ((((smscore_idlist_t *)nextid)->id == id) &&
890 (((smscore_idlist_t *)nextid)->data_type == data_type ||
891 (((smscore_idlist_t *)nextid)->data_type == 0))) {
892 client = (smscore_client_t *) next;
897 spin_unlock_irqrestore(&coredev->clientslock, flags);
902 * find client by response id/type, call clients onresponse handler
903 * return buffer to pool on error
905 * @param coredev pointer to a coredev object returned by
906 * smscore_register_device
907 * @param cb pointer to response buffer descriptor
910 void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
912 SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
913 smscore_client_t *client =
914 smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
917 static unsigned long last_sample_time = 0;
918 static int data_total = 0;
919 unsigned long time_now = jiffies_to_msecs(jiffies);
921 if (!last_sample_time)
922 last_sample_time = time_now;
924 if (time_now - last_sample_time > 10000) {
925 printk(KERN_DEBUG "\n%s data rate %d bytes/secs\n", __func__,
926 (int)((data_total * 1000) /
927 (time_now - last_sample_time)));
929 last_sample_time = time_now;
933 data_total += cb->size;
934 /* If no client registered for type & id,
935 * check for control client where type is not registered */
937 rc = client->onresponse_handler(client->context, cb);
940 switch (phdr->msgType) {
941 case MSG_SMS_GET_VERSION_EX_RES:
943 SmsVersionRes_ST *ver = (SmsVersionRes_ST *) phdr;
944 printk(KERN_DEBUG "%s: MSG_SMS_GET_VERSION_EX_RES "
945 "id %d prots 0x%x ver %d.%d\n", __func__,
946 ver->FirmwareId, ver->SupportedProtocols,
947 ver->RomVersionMajor, ver->RomVersionMinor);
949 coredev->mode = ver->FirmwareId == 255 ?
950 DEVICE_MODE_NONE : ver->FirmwareId;
951 coredev->modes_supported = ver->SupportedProtocols;
953 complete(&coredev->version_ex_done);
956 case MSG_SMS_INIT_DEVICE_RES:
957 printk(KERN_DEBUG "%s: MSG_SMS_INIT_DEVICE_RES\n",
959 complete(&coredev->init_device_done);
961 case MSG_SW_RELOAD_START_RES:
962 printk(KERN_DEBUG "%s: MSG_SW_RELOAD_START_RES\n",
964 complete(&coredev->reload_start_done);
966 case MSG_SMS_DATA_DOWNLOAD_RES:
967 complete(&coredev->data_download_done);
969 case MSG_SW_RELOAD_EXEC_RES:
970 printk(KERN_DEBUG "%s: MSG_SW_RELOAD_EXEC_RES\n",
973 case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
974 printk(KERN_DEBUG "%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
976 complete(&coredev->trigger_done);
978 case MSG_SMS_SLEEP_RESUME_COMP_IND:
979 complete(&coredev->resume_done);
984 smscore_putbuffer(coredev, cb);
989 * return pointer to next free buffer descriptor from core pool
991 * @param coredev pointer to a coredev object returned by
992 * smscore_register_device
994 * @return pointer to descriptor on success, NULL on error.
996 smscore_buffer_t *smscore_getbuffer(smscore_device_t *coredev)
998 smscore_buffer_t *cb = NULL;
1001 spin_lock_irqsave(&coredev->bufferslock, flags);
1003 if (!list_empty(&coredev->buffers)) {
1004 cb = (smscore_buffer_t *) coredev->buffers.next;
1005 list_del(&cb->entry);
1008 spin_unlock_irqrestore(&coredev->bufferslock, flags);
1014 * return buffer descriptor to a pool
1016 * @param coredev pointer to a coredev object returned by
1017 * smscore_register_device
1018 * @param cb pointer buffer descriptor
1021 void smscore_putbuffer(smscore_device_t *coredev, smscore_buffer_t *cb)
1023 list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
1026 int smscore_validate_client(smscore_device_t *coredev,
1027 smscore_client_t *client, int data_type, int id)
1029 smscore_idlist_t *listentry;
1030 smscore_client_t *registered_client;
1033 PERROR("bad parameter.\n");
1036 registered_client = smscore_find_client(coredev, data_type, id);
1037 if (registered_client == client)
1040 if (registered_client) {
1041 PERROR("The msg ID already registered to another client.\n");
1044 listentry = kzalloc(sizeof(smscore_idlist_t), GFP_KERNEL);
1046 PERROR("Can't allocate memory for client id.\n");
1050 listentry->data_type = data_type;
1051 list_add_locked(&listentry->entry, &client->idlist,
1052 &coredev->clientslock);
1057 * creates smsclient object, check that id is taken by another client
1059 * @param coredev pointer to a coredev object from clients hotplug
1060 * @param initial_id all messages with this id would be sent to this client
1061 * @param data_type all messages of this type would be sent to this client
1062 * @param onresponse_handler client handler that is called to process incoming messages
1063 * @param onremove_handler client handler that is called when device is removed
1064 * @param context client-specific context
1065 * @param client pointer to a value that receives created smsclient object
1067 * @return 0 on success, <0 on error.
1069 int smscore_register_client(smscore_device_t *coredev,
1070 smsclient_params_t *params,
1071 smscore_client_t **client)
1073 smscore_client_t *newclient;
1074 /* check that no other channel with same parameters exists */
1075 if (smscore_find_client(coredev, params->data_type,
1076 params->initial_id)) {
1077 PERROR("Client already exist.\n");
1081 newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
1083 PERROR("Failed to allocate memory for client.\n");
1087 INIT_LIST_HEAD(&newclient->idlist);
1088 newclient->coredev = coredev;
1089 newclient->onresponse_handler = params->onresponse_handler;
1090 newclient->onremove_handler = params->onremove_handler;
1091 newclient->context = params->context;
1092 list_add_locked(&newclient->entry, &coredev->clients,
1093 &coredev->clientslock);
1094 smscore_validate_client(coredev, newclient, params->data_type,
1095 params->initial_id);
1096 *client = newclient;
1097 PDEBUG("%p %d %d\n", params->context, params->data_type,
1098 params->initial_id);
1104 * frees smsclient object and all subclients associated with it
1106 * @param client pointer to smsclient object returned by
1107 * smscore_register_client
1110 void smscore_unregister_client(smscore_client_t *client)
1112 smscore_device_t *coredev = client->coredev;
1113 unsigned long flags;
1115 spin_lock_irqsave(&coredev->clientslock, flags);
1118 while (!list_empty(&client->idlist)) {
1119 smscore_idlist_t *identry =
1120 (smscore_idlist_t *) client->idlist.next;
1121 list_del(&identry->entry);
1125 printk(KERN_INFO "%s %p\n", __func__, client->context);
1127 list_del(&client->entry);
1130 spin_unlock_irqrestore(&coredev->clientslock, flags);
1134 * verifies that source id is not taken by another client,
1135 * calls device handler to send requests to the device
1137 * @param client pointer to smsclient object returned by
1138 * smscore_register_client
1139 * @param buffer pointer to a request buffer
1140 * @param size size (in bytes) of request buffer
1142 * @return 0 on success, <0 on error.
1144 int smsclient_sendrequest(smscore_client_t *client, void *buffer, size_t size)
1146 smscore_device_t *coredev;
1147 SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) buffer;
1150 if (client == NULL) {
1151 printk(KERN_ERR "%s Got NULL client\n", __func__);
1155 coredev = client->coredev;
1157 /* check that no other channel with same id exists */
1158 if (coredev == NULL) {
1159 printk(KERN_ERR "%s Got NULL coredev\n", __func__);
1163 rc = smscore_validate_client(client->coredev, client, 0,
1168 return coredev->sendrequest_handler(coredev->context, buffer, size);
1172 * return the size of large (common) buffer
1174 * @param coredev pointer to a coredev object from clients hotplug
1176 * @return size (in bytes) of the buffer
1178 int smscore_get_common_buffer_size(smscore_device_t *coredev)
1180 return coredev->common_buffer_size;
1184 * maps common buffer (if supported by platform)
1186 * @param coredev pointer to a coredev object from clients hotplug
1187 * @param vma pointer to vma struct from mmap handler
1189 * @return 0 on success, <0 on error.
1191 int smscore_map_common_buffer(smscore_device_t *coredev,
1192 struct vm_area_struct *vma)
1194 unsigned long end = vma->vm_end,
1195 start = vma->vm_start,
1196 size = PAGE_ALIGN(coredev->common_buffer_size);
1198 if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
1199 (vma->vm_flags & VM_WRITE)) {
1200 printk(KERN_INFO "%s invalid vm flags\n", __func__);
1204 if ((end - start) != size) {
1205 printk(KERN_INFO "%s invalid size %d expected %d\n",
1206 __func__, (int)(end - start), (int) size);
1210 if (remap_pfn_range(vma, start,
1211 coredev->common_buffer_phys >> PAGE_SHIFT,
1212 size, pgprot_noncached(vma->vm_page_prot)))
1214 printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1221 int smscore_module_init(void)
1225 INIT_LIST_HEAD(&g_smscore_notifyees);
1226 INIT_LIST_HEAD(&g_smscore_devices);
1227 kmutex_init(&g_smscore_deviceslock);
1229 INIT_LIST_HEAD(&g_smscore_registry);
1230 kmutex_init(&g_smscore_registrylock);
1233 rc = smsusb_register();
1236 rc = smsdvb_register();
1238 printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1243 void smscore_module_exit(void)
1246 kmutex_lock(&g_smscore_deviceslock);
1247 while (!list_empty(&g_smscore_notifyees)) {
1248 smscore_device_notifyee_t *notifyee =
1249 (smscore_device_notifyee_t *) g_smscore_notifyees.next;
1251 list_del(¬ifyee->entry);
1254 kmutex_unlock(&g_smscore_deviceslock);
1256 kmutex_lock(&g_smscore_registrylock);
1257 while (!list_empty(&g_smscore_registry)) {
1258 smscore_registry_entry_t *entry =
1259 (smscore_registry_entry_t *) g_smscore_registry.next;
1261 list_del(&entry->entry);
1264 kmutex_unlock(&g_smscore_registrylock);
1266 /* DVB UnRegister */
1267 smsdvb_unregister();
1269 /* Unregister USB */
1270 smsusb_unregister();
1272 printk(KERN_INFO "%s\n", __func__);
1275 module_init(smscore_module_init);
1276 module_exit(smscore_module_exit);
1278 MODULE_DESCRIPTION("smscore");
1279 MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1280 MODULE_LICENSE("GPL");