V4L/DVB (8281): sms1xxx: remove INT / UINT typedefs
[safe/jmp/linux-2.6] / drivers / media / dvb / siano / smscoreapi.c
1 /*
2  *  Siano core API module
3  *
4  *  This file contains implementation for the interface to sms core component
5  *
6  *  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  *  it under the terms of the GNU General Public License version 3 as
12  *  published by the Free Software Foundation;
13  *
14  *  Software distributed under the License is distributed on an "AS IS"
15  *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
16  *
17  *  See the GNU General Public License for more details.
18  *
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.
22  */
23
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>
30 #include <asm/io.h>
31
32 #include <linux/firmware.h>
33
34 #include "smscoreapi.h"
35
36 #define PERROR(fmt, args...)\
37         printk(KERN_ERR "smscore error: line %d- %s(): " fmt, \
38                 __LINE__,  __func__, ## args)
39
40 #ifdef SMSCORE_DEBUG
41 #undef PWARNING
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, \
47                                         __func__, ## args)
48 #else /*SMSCORE_DEBUG*/
49 #define PDEBUG(fmt, args...)
50 #define PWARNING(fmt, args...)
51 #endif
52
53 typedef struct _smscore_device_notifyee
54 {
55         struct list_head entry;
56         hotplug_t hotplug;
57 } smscore_device_notifyee_t;
58
59 typedef struct _smscore_subclient
60 {
61         struct list_head entry;
62         int             id;
63         int             data_type;
64 } smscore_idlist_t;
65
66 typedef struct _smscore_client
67 {
68         struct list_head entry;
69         smscore_device_t *coredev;
70         void                    *context;
71         struct list_head        idlist;
72         onresponse_t    onresponse_handler;
73         onremove_t              onremove_handler;
74 } *psmscore_client_t;
75
76
77
78 typedef struct _smscore_device
79 {
80         struct list_head entry;
81
82         struct list_head clients;
83         struct list_head subclients;
84         spinlock_t              clientslock;
85
86         struct list_head buffers;
87         spinlock_t              bufferslock;
88         int                             num_buffers;
89
90         void                    *common_buffer;
91         int                             common_buffer_size;
92         dma_addr_t              common_buffer_phys;
93
94         void                    *context;
95         struct device   *device;
96
97         char                    devpath[32];
98         unsigned long   device_flags;
99
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;
105
106         int                             mode, modes_supported;
107
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;
111
112 typedef struct _smscore_registry_entry
113 {
114         struct list_head entry;
115         char                    devpath[32];
116         int                             mode;
117         sms_device_type_st      type;
118 } smscore_registry_entry_t;
119
120 struct list_head g_smscore_notifyees;
121 struct list_head g_smscore_devices;
122 kmutex_t g_smscore_deviceslock;
123
124 struct list_head g_smscore_registry;
125 kmutex_t g_smscore_registrylock;
126
127 static int default_mode = 1;
128
129 module_param(default_mode, int, 0644);
130 MODULE_PARM_DESC(default_mode, "default firmware id (device mode)");
131
132 static smscore_registry_entry_t *smscore_find_registry(char *devpath)
133 {
134         smscore_registry_entry_t *entry;
135         struct list_head *next;
136
137         kmutex_lock(&g_smscore_registrylock);
138         for (next = g_smscore_registry.next;
139              next != &g_smscore_registry;
140              next = next->next) {
141                 entry = (smscore_registry_entry_t *) next;
142                 if (!strcmp(entry->devpath, devpath)) {
143                         kmutex_unlock(&g_smscore_registrylock);
144                         return entry;
145                 }
146         }
147         entry = (smscore_registry_entry_t *)
148                         kmalloc(sizeof(smscore_registry_entry_t), GFP_KERNEL);
149         if (entry) {
150                 entry->mode = default_mode;
151                 strcpy(entry->devpath, devpath);
152                 list_add(&entry->entry, &g_smscore_registry);
153         } else
154                 printk(KERN_ERR "%s failed to create smscore_registry.\n",
155                        __func__);
156         kmutex_unlock(&g_smscore_registrylock);
157         return entry;
158 }
159
160 int smscore_registry_getmode(char *devpath)
161 {
162         smscore_registry_entry_t *entry;
163
164         entry = smscore_find_registry(devpath);
165         if (entry)
166                 return entry->mode;
167         else
168                 printk(KERN_ERR "%s No registry found.\n", __func__);
169
170         return default_mode;
171 }
172
173 sms_device_type_st smscore_registry_gettype(char *devpath)
174 {
175         smscore_registry_entry_t *entry;
176
177         entry = smscore_find_registry(devpath);
178         if (entry)
179                 return entry->type;
180         else
181                 printk(KERN_ERR "%s No registry found.\n", __func__);
182
183         return -1;
184 }
185
186 void smscore_registry_setmode(char *devpath, int mode)
187 {
188         smscore_registry_entry_t *entry;
189
190         entry = smscore_find_registry(devpath);
191         if (entry)
192                 entry->mode = mode;
193         else
194                 printk(KERN_ERR "%s No registry found.\n", __func__);
195 }
196
197 void smscore_registry_settype(char *devpath, sms_device_type_st type)
198 {
199         smscore_registry_entry_t *entry;
200
201         entry = smscore_find_registry(devpath);
202         if (entry)
203                 entry->type = type;
204         else
205                 printk(KERN_ERR "%s No registry found.\n", __func__);
206 }
207
208
209
210 void list_add_locked(struct list_head *new, struct list_head *head,
211                      spinlock_t *lock)
212 {
213         unsigned long flags;
214
215         spin_lock_irqsave(lock, flags);
216
217         list_add(new, head);
218
219         spin_unlock_irqrestore(lock, flags);
220 }
221
222 /**
223  * register a client callback that called when device plugged in/unplugged
224  * NOTE: if devices exist callback is called immediately for each device
225  *
226  * @param hotplug callback
227  *
228  * @return 0 on success, <0 on error.
229  */
230 int smscore_register_hotplug(hotplug_t hotplug)
231 {
232         smscore_device_notifyee_t *notifyee;
233         struct list_head *next, *first;
234         int rc = 0;
235
236         kmutex_lock(&g_smscore_deviceslock);
237
238         notifyee = kmalloc(sizeof(smscore_device_notifyee_t), GFP_KERNEL);
239         if (notifyee) {
240                 /* now notify callback about existing devices */
241                 first = &g_smscore_devices;
242                 for (next = first->next;
243                      next != first && !rc;
244                      next = next->next) {
245                         smscore_device_t *coredev = (smscore_device_t *) next;
246                         rc = hotplug(coredev, coredev->device, 1);
247                 }
248
249                 if (rc >= 0) {
250                         notifyee->hotplug = hotplug;
251                         list_add(&notifyee->entry, &g_smscore_notifyees);
252                 } else
253                         kfree(notifyee);
254         } else
255                 rc = -ENOMEM;
256
257         kmutex_unlock(&g_smscore_deviceslock);
258
259         return rc;
260 }
261
262 /**
263  * unregister a client callback that called when device plugged in/unplugged
264  *
265  * @param hotplug callback
266  *
267  */
268 void smscore_unregister_hotplug(hotplug_t hotplug)
269 {
270         struct list_head *next, *first;
271
272         kmutex_lock(&g_smscore_deviceslock);
273
274         first = &g_smscore_notifyees;
275
276         for (next = first->next; next != first;) {
277                 smscore_device_notifyee_t *notifyee =
278                         (smscore_device_notifyee_t *) next;
279                 next = next->next;
280
281                 if (notifyee->hotplug == hotplug) {
282                         list_del(&notifyee->entry);
283                         kfree(notifyee);
284                 }
285         }
286
287         kmutex_unlock(&g_smscore_deviceslock);
288 }
289
290 void smscore_notify_clients(smscore_device_t *coredev)
291 {
292         smscore_client_t *client;
293
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);
298         }
299 }
300
301 int smscore_notify_callbacks(smscore_device_t *coredev, struct device *device,
302                              int arrival)
303 {
304         struct list_head *next, *first;
305         int rc = 0;
306
307         /* note: must be called under g_deviceslock */
308
309         first = &g_smscore_notifyees;
310
311         for (next = first->next; next != first; next = next->next) {
312                 rc = ((smscore_device_notifyee_t *) next)->hotplug(coredev, device, arrival);
313                 if (rc < 0)
314                         break;
315         }
316
317         return rc;
318 }
319
320 smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
321                                        dma_addr_t common_buffer_phys)
322 {
323         smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
324         if (!cb) {
325                 printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
326                 return NULL;
327         }
328
329         cb->p = buffer;
330         cb->offset_in_common = buffer - (u8 *) common_buffer;
331         cb->phys = common_buffer_phys + cb->offset_in_common;
332
333         return cb;
334 }
335
336 /**
337  * creates coredev object for a device, prepares buffers,
338  * creates buffer mappings, notifies registered hotplugs about new device.
339  *
340  * @param params device pointer to struct with device specific parameters and handlers
341  * @param coredev pointer to a value that receives created coredev object
342  *
343  * @return 0 on success, <0 on error.
344  */
345 int smscore_register_device(smsdevice_params_t *params,
346                             smscore_device_t **coredev)
347 {
348         smscore_device_t *dev;
349         u8 *buffer;
350
351         dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
352         if (!dev) {
353                 printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
354                 return -ENOMEM;
355         }
356
357         /* init list entry so it could be safe in smscore_unregister_device */
358         INIT_LIST_HEAD(&dev->entry);
359
360         /* init queues */
361         INIT_LIST_HEAD(&dev->clients);
362         INIT_LIST_HEAD(&dev->buffers);
363
364         /* init locks */
365         spin_lock_init(&dev->clientslock);
366         spin_lock_init(&dev->bufferslock);
367
368         /* init completion events */
369         init_completion(&dev->version_ex_done);
370         init_completion(&dev->data_download_done);
371         init_completion(&dev->trigger_done);
372         init_completion(&dev->init_device_done);
373         init_completion(&dev->reload_start_done);
374         init_completion(&dev->resume_done);
375
376         /* alloc common buffer */
377         dev->common_buffer_size = params->buffer_size * params->num_buffers;
378         dev->common_buffer = dma_alloc_coherent(NULL, dev->common_buffer_size,
379                                                 &dev->common_buffer_phys,
380                                                 GFP_KERNEL | GFP_DMA);
381         if (!dev->common_buffer) {
382                 smscore_unregister_device(dev);
383                 return -ENOMEM;
384         }
385
386         /* prepare dma buffers */
387         for (buffer = dev->common_buffer;
388              dev->num_buffers < params->num_buffers;
389              dev->num_buffers ++, buffer += params->buffer_size) {
390                 smscore_buffer_t *cb = smscore_createbuffer(buffer, dev->common_buffer, dev->common_buffer_phys);
391                 if (!cb) {
392                         smscore_unregister_device(dev);
393                         return -ENOMEM;
394                 }
395
396                 smscore_putbuffer(dev, cb);
397         }
398
399         printk(KERN_INFO "%s allocated %d buffers\n",
400                __func__, dev->num_buffers);
401
402         dev->mode = DEVICE_MODE_NONE;
403         dev->context = params->context;
404         dev->device = params->device;
405         dev->setmode_handler = params->setmode_handler;
406         dev->detectmode_handler = params->detectmode_handler;
407         dev->sendrequest_handler = params->sendrequest_handler;
408         dev->preload_handler = params->preload_handler;
409         dev->postload_handler = params->postload_handler;
410
411         dev->device_flags = params->flags;
412         strcpy(dev->devpath, params->devpath);
413
414         smscore_registry_settype(dev->devpath, params->device_type);
415
416         /* add device to devices list */
417         kmutex_lock(&g_smscore_deviceslock);
418         list_add(&dev->entry, &g_smscore_devices);
419         kmutex_unlock(&g_smscore_deviceslock);
420
421         *coredev = dev;
422
423         printk(KERN_INFO "%s device %p created\n", __func__, dev);
424
425         return 0;
426 }
427
428 /**
429  * sets initial device mode and notifies client hotplugs that device is ready
430  *
431  * @param coredev pointer to a coredev object returned by smscore_register_device
432  *
433  * @return 0 on success, <0 on error.
434  */
435 int smscore_start_device(smscore_device_t *coredev)
436 {
437         int rc = smscore_set_device_mode(coredev, smscore_registry_getmode(coredev->devpath));
438         if (rc < 0) {
439                 printk(KERN_INFO "%s set device mode faile , rc %d\n", __func__, rc);
440                 return rc;
441         }
442
443         kmutex_lock(&g_smscore_deviceslock);
444
445         rc = smscore_notify_callbacks(coredev, coredev->device, 1);
446
447         printk(KERN_INFO "%s device %p started, rc %d\n",
448                __func__, coredev, rc);
449
450         kmutex_unlock(&g_smscore_deviceslock);
451
452         return rc;
453 }
454
455 int smscore_sendrequest_and_wait(smscore_device_t *coredev, void *buffer,
456                                  size_t size, struct completion *completion)
457 {
458         int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
459         if (rc < 0) {
460                 printk(KERN_INFO "%s sendrequest returned error %d\n",
461                        __func__, rc);
462                 return rc;
463         }
464
465         return wait_for_completion_timeout(completion,
466                                            msecs_to_jiffies(10000)) ?
467                                                 0 : -ETIME;
468 }
469
470 int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer,
471                                   size_t size)
472 {
473         SmsFirmware_ST *firmware = (SmsFirmware_ST *) buffer;
474         SmsMsgHdr_ST *msg;
475         u32 mem_address = firmware->StartAddress;
476         u8 *payload = firmware->Payload;
477         int rc = 0;
478
479         printk(KERN_INFO "%s loading FW to addr 0x%x size %d\n",
480                __func__, mem_address, firmware->Length);
481         if (coredev->preload_handler) {
482                 rc = coredev->preload_handler(coredev->context);
483                 if (rc < 0)
484                         return rc;
485         }
486
487         /* PAGE_SIZE buffer shall be enough and dma aligned */
488         msg = (SmsMsgHdr_ST *) kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
489         if (!msg)
490                 return -ENOMEM;
491
492         if (coredev->mode != DEVICE_MODE_NONE) {
493                 PDEBUG("Sending reload command\n");
494                 SMS_INIT_MSG(msg, MSG_SW_RELOAD_START_REQ,
495                              sizeof(SmsMsgHdr_ST));
496                 rc = smscore_sendrequest_and_wait(coredev, msg,
497                                                   msg->msgLength,
498                                                   &coredev->reload_start_done);
499                 mem_address = *(u32 *) &payload[20];
500         }
501
502         while (size && rc >= 0)
503         {
504                 SmsDataDownload_ST *DataMsg = (SmsDataDownload_ST *) msg;
505                 int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);
506
507                 SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
508                              (u16)(sizeof(SmsMsgHdr_ST) +
509                                       sizeof(u32) + payload_size));
510
511                 DataMsg->MemAddr = mem_address;
512                 memcpy(DataMsg->Payload, payload, payload_size);
513
514                 if ((coredev->device_flags & SMS_ROM_NO_RESPONSE) &&
515                     (coredev->mode == DEVICE_MODE_NONE))
516                         rc = coredev->sendrequest_handler(coredev->context, DataMsg, DataMsg->xMsgHeader.msgLength);
517                 else
518                         rc = smscore_sendrequest_and_wait(coredev, DataMsg, DataMsg->xMsgHeader.msgLength, &coredev->data_download_done);
519
520                 payload += payload_size;
521                 size -= payload_size;
522                 mem_address += payload_size;
523         }
524
525         if (rc >= 0) {
526                 if (coredev->mode == DEVICE_MODE_NONE) {
527                         SmsMsgData_ST *TriggerMsg = (SmsMsgData_ST *) msg;
528
529                         SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
530                                      sizeof(SmsMsgHdr_ST) +
531                                      sizeof(u32) * 5);
532
533                         TriggerMsg->msgData[0] = firmware->StartAddress;        // Entry point
534                         TriggerMsg->msgData[1] = 5;                                                     // Priority
535                         TriggerMsg->msgData[2] = 0x200;                                         // Stack size
536                         TriggerMsg->msgData[3] = 0;                                                     // Parameter
537                         TriggerMsg->msgData[4] = 4;                                                     // Task ID
538
539                         if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
540                                 rc = coredev->sendrequest_handler(coredev->context, TriggerMsg, TriggerMsg->xMsgHeader.msgLength);
541                                 msleep(100);
542                         } else
543                                 rc = smscore_sendrequest_and_wait(coredev, TriggerMsg, TriggerMsg->xMsgHeader.msgLength, &coredev->trigger_done);
544                 } else {
545                         SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
546                                      sizeof(SmsMsgHdr_ST));
547
548                         rc = coredev->sendrequest_handler(coredev->context,
549                                                           msg, msg->msgLength);
550                 }
551                 msleep(500);
552         }
553
554         printk("%s rc=%d, postload=%p \n", __func__, rc,
555                coredev->postload_handler);
556
557         kfree(msg);
558
559         return ((rc >= 0) && coredev->postload_handler) ?
560                 coredev->postload_handler(coredev->context) :
561                 rc;
562 }
563
564 /**
565  * loads specified firmware into a buffer and calls device loadfirmware_handler
566  *
567  * @param coredev pointer to a coredev object returned by smscore_register_device
568  * @param filename null-terminated string specifies firmware file name
569  * @param loadfirmware_handler device handler that loads firmware
570  *
571  * @return 0 on success, <0 on error.
572  */
573 int smscore_load_firmware_from_file(smscore_device_t *coredev, char *filename,
574                                     loadfirmware_t loadfirmware_handler)
575 {
576         int rc = -ENOENT;
577
578         const struct firmware *fw;
579         u8 *fw_buffer;
580
581         if (loadfirmware_handler == NULL && !(coredev->device_flags &
582                                               SMS_DEVICE_FAMILY2))
583                 return -EINVAL;
584
585         rc = request_firmware(&fw, filename, coredev->device);
586         if (rc < 0) {
587                 printk(KERN_INFO "%s failed to open \"%s\"\n",
588                        __func__, filename);
589                 return rc;
590         }
591         printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
592                filename, fw->size);
593         fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
594                             GFP_KERNEL | GFP_DMA);
595         if (fw_buffer) {
596                 memcpy(fw_buffer, fw->data, fw->size);
597
598                 rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
599                       smscore_load_firmware_family2(coredev, fw_buffer, fw->size) :
600                         loadfirmware_handler(coredev->context, fw_buffer, fw->size);
601
602                 kfree(fw_buffer);
603         } else {
604                 printk(KERN_INFO "%s failed to allocate firmware buffer\n",
605                        __func__);
606                 rc = -ENOMEM;
607         }
608
609         release_firmware(fw);
610
611         return rc;
612 }
613
614 int smscore_load_firmware_from_buffer(smscore_device_t *coredev, u8 *buffer,
615                                       int size, int new_mode)
616 {
617         PERROR("Feature not implemented yet\n");
618         return -EFAULT;
619 }
620
621 /**
622  * notifies all clients registered with the device, notifies hotplugs, frees all buffers and coredev object
623  *
624  * @param coredev pointer to a coredev object returned by smscore_register_device
625  *
626  * @return 0 on success, <0 on error.
627  */
628 void smscore_unregister_device(smscore_device_t *coredev)
629 {
630         smscore_buffer_t *cb;
631         int num_buffers = 0;
632         int retry = 0;
633
634         kmutex_lock(&g_smscore_deviceslock);
635
636         smscore_notify_clients(coredev);
637         smscore_notify_callbacks(coredev, NULL, 0);
638
639         /* at this point all buffers should be back
640          * onresponse must no longer be called */
641
642         while (1) {
643                 while ((cb = smscore_getbuffer(coredev))) {
644                         kfree(cb);
645                         num_buffers ++;
646                 }
647                 if (num_buffers == coredev->num_buffers)
648                         break;
649                 if (++retry > 10) {
650                         printk(KERN_INFO "%s exiting although "
651                                "not all buffers released.\n", __func__);
652                         break;
653                 }
654
655                 printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
656                        coredev->num_buffers - num_buffers);
657                 msleep(100);
658         }
659
660         printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
661
662         if (coredev->common_buffer)
663                 dma_free_coherent(NULL, coredev->common_buffer_size,
664                                   coredev->common_buffer,
665                                   coredev->common_buffer_phys);
666
667         list_del(&coredev->entry);
668         kfree(coredev);
669
670         kmutex_unlock(&g_smscore_deviceslock);
671
672         printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
673 }
674
675 int smscore_detect_mode(smscore_device_t *coredev)
676 {
677         void *buffer = kmalloc(sizeof(SmsMsgHdr_ST) + SMS_DMA_ALIGNMENT,
678                                GFP_KERNEL | GFP_DMA);
679         SmsMsgHdr_ST *msg = (SmsMsgHdr_ST *) SMS_ALIGN_ADDRESS(buffer);
680         int rc;
681
682         if (!buffer)
683                 return -ENOMEM;
684
685         SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ, sizeof(SmsMsgHdr_ST));
686
687         rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
688                                           &coredev->version_ex_done);
689         if (rc == -ETIME) {
690                 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed first try\n", __func__);
691
692                 if (wait_for_completion_timeout(&coredev->resume_done,
693                                                 msecs_to_jiffies(5000))) {
694                         rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength, &coredev->version_ex_done);
695                         if (rc < 0)
696                                 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed second try, rc %d\n", __func__, rc);
697                 } else
698                         rc = -ETIME;
699         }
700
701         kfree(buffer);
702
703         return rc;
704 }
705
706 char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
707         /*Stellar                       NOVA A0                 Nova B0                         VEGA*/
708         /*DVBT*/  {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
709         /*DVBH*/  {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
710         /*TDMB*/  {"none", "tdmb_nova_12mhz.inp", "none", "none"},
711         /*DABIP*/ {"none", "none", "none", "none"},
712         /*BDA*/   {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
713         /*ISDBT*/ {"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
714         /*ISDBTBDA*/{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
715         /*CMMB*/  {"none", "none", "none", "cmmb_vega_12mhz.inp"}
716 };
717
718
719 /**
720  * calls device handler to change mode of operation
721  * NOTE: stellar/usb may disconnect when changing mode
722  *
723  * @param coredev pointer to a coredev object returned by smscore_register_device
724  * @param mode requested mode of operation
725  *
726  * @return 0 on success, <0 on error.
727  */
728 int smscore_set_device_mode(smscore_device_t *coredev, int mode)
729 {
730         void *buffer;
731         int rc = 0;
732         sms_device_type_st type;
733
734         PDEBUG("set device mode to %d\n", mode);
735         if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
736                 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
737                         printk(KERN_INFO "%s invalid mode specified %d\n",
738                                __func__, mode);
739                         return -EINVAL;
740                 }
741
742                 smscore_registry_setmode(coredev->devpath, mode);
743
744                 if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
745                         rc = smscore_detect_mode(coredev);
746                         if (rc < 0) {
747                                 printk(KERN_INFO "%s mode detect failed %d\n",
748                                        __func__, rc);
749                                 return rc;
750                         }
751                 }
752
753                 if (coredev->mode == mode) {
754                         printk(KERN_INFO "%s device mode %d already set\n",
755                                __func__, mode);
756                         return 0;
757                 }
758
759                 if (!(coredev->modes_supported & (1 << mode))) {
760                         type = smscore_registry_gettype(coredev->devpath);
761                         rc = smscore_load_firmware_from_file(coredev, smscore_fw_lkup[mode][type], NULL);
762                         if (rc < 0) {
763                                 printk(KERN_INFO "%s load firmware failed %d\n", __func__, rc);
764                                 return rc;
765                         }
766                 } else
767                         printk(KERN_INFO "%s mode %d supported by running firmware\n", __func__, mode);
768
769                 buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT,
770                                  GFP_KERNEL | GFP_DMA);
771                 if (buffer) {
772                         SmsMsgData_ST *msg = (SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);
773
774                         SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ, sizeof(SmsMsgData_ST));
775                         msg->msgData[0] = mode;
776
777                         rc = smscore_sendrequest_and_wait(coredev, msg, msg->xMsgHeader.msgLength, &coredev->init_device_done);
778
779                         kfree(buffer);
780                 } else {
781                         printk(KERN_INFO "%s Could not allocate buffer for init device message.\n", __func__);
782                         rc = -ENOMEM;
783                 }
784         } else {
785                 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
786                         printk(KERN_INFO "%s invalid mode specified %d\n",
787                                __func__, mode);
788                         return -EINVAL;
789                 }
790
791                 smscore_registry_setmode(coredev->devpath, mode);
792
793                 if (coredev->detectmode_handler)
794                         coredev->detectmode_handler(coredev->context,
795                                                     &coredev->mode);
796
797                 if (coredev->mode != mode && coredev->setmode_handler)
798                         rc = coredev->setmode_handler(coredev->context, mode);
799         }
800
801         if (rc >= 0) {
802                 coredev->mode = mode;
803                 coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
804         }
805
806         if (rc != 0)
807                 printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
808         return rc;
809 }
810
811 /**
812  * calls device handler to get current mode of operation
813  *
814  * @param coredev pointer to a coredev object returned by smscore_register_device
815  *
816  * @return current mode
817  */
818 int smscore_get_device_mode(smscore_device_t *coredev)
819 {
820         return coredev->mode;
821 }
822
823 /**
824  * find client by response id & type within the clients list.
825  * return client handle or NULL.
826  *
827  * @param coredev pointer to a coredev object returned by smscore_register_device
828  * @param data_type client data type (SMS_DONT_CARE for all types)
829  * @param id client id (SMS_DONT_CARE for all id)
830  *
831  */
832 smscore_client_t *smscore_find_client(smscore_device_t *coredev, int data_type, int id)
833 {
834         smscore_client_t *client = NULL;
835         struct list_head *next, *first;
836         unsigned long flags;
837         struct list_head *firstid, *nextid;
838
839
840         spin_lock_irqsave(&coredev->clientslock, flags);
841         first = &coredev->clients;
842         for (next = first->next;
843              (next != first) && !client;
844              next = next->next) {
845                 firstid = &((smscore_client_t*)next)->idlist;
846                 for (nextid = firstid->next;
847                      nextid != firstid;
848                      nextid = nextid->next) {
849                         if ((((smscore_idlist_t*)nextid)->id  == id) &&
850                             (((smscore_idlist_t*)nextid)->data_type  == data_type ||
851                             (((smscore_idlist_t*)nextid)->data_type  == 0))) {
852                                 client = (smscore_client_t*) next;
853                                 break;
854                         }
855                 }
856         }
857         spin_unlock_irqrestore(&coredev->clientslock, flags);
858         return client;
859 }
860
861 /**
862  * find client by response id/type, call clients onresponse handler
863  * return buffer to pool on error
864  *
865  * @param coredev pointer to a coredev object returned by smscore_register_device
866  * @param cb pointer to response buffer descriptor
867  *
868  */
869 void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
870 {
871         SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
872         smscore_client_t *client = smscore_find_client(coredev, phdr->msgType,
873                                                        phdr->msgDstId);
874         int rc = -EBUSY;
875
876         static unsigned long last_sample_time = 0;
877         static int data_total = 0;
878         unsigned long time_now = jiffies_to_msecs(jiffies);
879
880         if (!last_sample_time)
881                 last_sample_time = time_now;
882
883         if (time_now - last_sample_time > 10000)
884         {
885                 printk("\n%s data rate %d bytes/secs\n", __func__,
886                        (int)((data_total * 1000) /
887                              (time_now - last_sample_time)));
888
889                 last_sample_time = time_now;
890                 data_total = 0;
891         }
892
893         data_total += cb->size;
894         /* If no client registered for type & id,
895          * check for control client where type is not registered */
896         if (client)
897                 rc = client->onresponse_handler(client->context, cb);
898
899         if (rc < 0) {
900                 switch (phdr->msgType) {
901                 case MSG_SMS_GET_VERSION_EX_RES:
902                 {
903                         SmsVersionRes_ST *ver = (SmsVersionRes_ST *) phdr;
904                         printk("%s: MSG_SMS_GET_VERSION_EX_RES id %d "
905                                "prots 0x%x ver %d.%d\n", __func__,
906                                ver->FirmwareId, ver->SupportedProtocols,
907                                ver->RomVersionMajor, ver->RomVersionMinor);
908
909                         coredev->mode = ver->FirmwareId == 255 ?
910                                 DEVICE_MODE_NONE : ver->FirmwareId;
911                         coredev->modes_supported = ver->SupportedProtocols;
912
913                         complete(&coredev->version_ex_done);
914                         break;
915                 }
916                 case MSG_SMS_INIT_DEVICE_RES:
917                         printk("%s: MSG_SMS_INIT_DEVICE_RES\n", __func__);
918                         complete(&coredev->init_device_done);
919                         break;
920                 case MSG_SW_RELOAD_START_RES:
921                         printk("%s: MSG_SW_RELOAD_START_RES\n", __func__);
922                         complete(&coredev->reload_start_done);
923                         break;
924                 case MSG_SMS_DATA_DOWNLOAD_RES:
925                         complete(&coredev->data_download_done);
926                         break;
927                 case MSG_SW_RELOAD_EXEC_RES:
928                         printk("%s: MSG_SW_RELOAD_EXEC_RES\n", __func__);
929                         break;
930                 case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
931                         printk("%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
932                                __func__);
933                         complete(&coredev->trigger_done);
934                         break;
935                 case MSG_SMS_SLEEP_RESUME_COMP_IND:
936                         complete(&coredev->resume_done);
937                         break;
938                 default:
939                         break;
940                 }
941                 smscore_putbuffer(coredev, cb);
942         }
943 }
944
945 /**
946  * return pointer to next free buffer descriptor from core pool
947  *
948  * @param coredev pointer to a coredev object returned by smscore_register_device
949  *
950  * @return pointer to descriptor on success, NULL on error.
951  */
952 smscore_buffer_t *smscore_getbuffer(smscore_device_t *coredev)
953 {
954         smscore_buffer_t *cb = NULL;
955         unsigned long flags;
956
957         spin_lock_irqsave(&coredev->bufferslock, flags);
958
959         if (!list_empty(&coredev->buffers)) {
960                 cb = (smscore_buffer_t *) coredev->buffers.next;
961                 list_del(&cb->entry);
962         }
963
964         spin_unlock_irqrestore(&coredev->bufferslock, flags);
965
966         return cb;
967 }
968
969 /**
970  * return buffer descriptor to a pool
971  *
972  * @param coredev pointer to a coredev object returned by smscore_register_device
973  * @param cb pointer buffer descriptor
974  *
975  */
976 void smscore_putbuffer(smscore_device_t *coredev, smscore_buffer_t *cb)
977 {
978         list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
979 }
980
981 int smscore_validate_client(smscore_device_t *coredev,
982                             smscore_client_t *client, int data_type, int id)
983 {
984         smscore_idlist_t *listentry;
985         smscore_client_t *registered_client;
986
987         if (!client) {
988                 PERROR("bad parameter.\n");
989                 return -EFAULT;
990         }
991         registered_client = smscore_find_client(coredev, data_type, id);
992         if (registered_client == client) {
993                 return 0;
994         }
995         if (registered_client) {
996                 PERROR("The msg ID already registered to another client.\n");
997                 return -EEXIST;
998         }
999         listentry = kzalloc(sizeof(smscore_idlist_t), GFP_KERNEL);
1000         if (!listentry) {
1001                 PERROR("Can't allocate memory for client id.\n");
1002                 return -ENOMEM;
1003         }
1004         listentry->id = id;
1005         listentry->data_type = data_type;
1006         list_add_locked(&listentry->entry, &client->idlist,
1007                         &coredev->clientslock);
1008         return 0;
1009 }
1010
1011 /**
1012  * creates smsclient object, check that id is taken by another client
1013  *
1014  * @param coredev pointer to a coredev object from clients hotplug
1015  * @param initial_id all messages with this id would be sent to this client
1016  * @param data_type all messages of this type would be sent to this client
1017  * @param onresponse_handler client handler that is called to process incoming messages
1018  * @param onremove_handler client handler that is called when device is removed
1019  * @param context client-specific context
1020  * @param client pointer to a value that receives created smsclient object
1021  *
1022  * @return 0 on success, <0 on error.
1023  */
1024 int smscore_register_client(smscore_device_t *coredev, smsclient_params_t *params, smscore_client_t **client)
1025 {
1026         smscore_client_t *newclient;
1027         /* check that no other channel with same parameters exists */
1028         if (smscore_find_client(coredev, params->data_type,
1029                                 params->initial_id)) {
1030                 PERROR("Client already exist.\n");
1031                 return -EEXIST;
1032         }
1033
1034         newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
1035         if (!newclient) {
1036                 PERROR("Failed to allocate memory for client.\n");
1037                 return -ENOMEM;
1038         }
1039
1040         INIT_LIST_HEAD(&newclient->idlist);
1041         newclient->coredev = coredev;
1042         newclient->onresponse_handler = params->onresponse_handler;
1043         newclient->onremove_handler = params->onremove_handler;
1044         newclient->context = params->context;
1045         list_add_locked(&newclient->entry, &coredev->clients,
1046                         &coredev->clientslock);
1047         smscore_validate_client(coredev, newclient, params->data_type,
1048                                 params->initial_id);
1049         *client = newclient;
1050         PDEBUG("%p %d %d\n", params->context, params->data_type,
1051                params->initial_id);
1052
1053         return 0;
1054 }
1055
1056 /**
1057  * frees smsclient object and all subclients associated with it
1058  *
1059  * @param client pointer to smsclient object returned by smscore_register_client
1060  *
1061  */
1062 void smscore_unregister_client(smscore_client_t *client)
1063 {
1064         smscore_device_t *coredev = client->coredev;
1065         unsigned long flags;
1066
1067         spin_lock_irqsave(&coredev->clientslock, flags);
1068
1069
1070         while (!list_empty(&client->idlist)) {
1071                 smscore_idlist_t *identry =
1072                         (smscore_idlist_t*) client->idlist.next;
1073                 list_del(&identry->entry);
1074                 kfree(identry);
1075         }
1076
1077         printk(KERN_INFO "%s %p\n", __func__, client->context);
1078
1079         list_del(&client->entry);
1080         kfree(client);
1081
1082         spin_unlock_irqrestore(&coredev->clientslock, flags);
1083 }
1084
1085 /**
1086  * verifies that source id is not taken by another client,
1087  * calls device handler to send requests to the device
1088  *
1089  * @param client pointer to smsclient object returned by smscore_register_client
1090  * @param buffer pointer to a request buffer
1091  * @param size size (in bytes) of request buffer
1092  *
1093  * @return 0 on success, <0 on error.
1094  */
1095 int smsclient_sendrequest(smscore_client_t *client, void *buffer, size_t size)
1096 {
1097         smscore_device_t *coredev;
1098         SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) buffer;
1099         int rc;
1100
1101         if (client == NULL) {
1102                 printk(KERN_ERR "%s Got NULL client\n", __func__);
1103                 return -EINVAL;
1104         }
1105
1106         coredev = client->coredev;
1107
1108         /* check that no other channel with same id exists */
1109         if (coredev == NULL) {
1110                 printk(KERN_ERR "%s Got NULL coredev\n", __func__);
1111                 return -EINVAL;
1112         }
1113
1114         rc = smscore_validate_client(client->coredev, client, 0,
1115                                      phdr->msgSrcId);
1116         if (rc < 0)
1117                 return rc;
1118
1119         return coredev->sendrequest_handler(coredev->context, buffer, size);
1120 }
1121
1122 /**
1123  * return the size of large (common) buffer
1124  *
1125  * @param coredev pointer to a coredev object from clients hotplug
1126  *
1127  * @return size (in bytes) of the buffer
1128  */
1129 int smscore_get_common_buffer_size(smscore_device_t *coredev)
1130 {
1131         return coredev->common_buffer_size;
1132 }
1133
1134 /**
1135  * maps common buffer (if supported by platform)
1136  *
1137  * @param coredev pointer to a coredev object from clients hotplug
1138  * @param vma pointer to vma struct from mmap handler
1139  *
1140  * @return 0 on success, <0 on error.
1141  */
1142 int smscore_map_common_buffer(smscore_device_t *coredev,
1143                                struct vm_area_struct *vma)
1144 {
1145         unsigned long end = vma->vm_end,
1146                       start = vma->vm_start,
1147                       size = PAGE_ALIGN(coredev->common_buffer_size);
1148
1149         if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
1150              (vma->vm_flags & VM_WRITE)) {
1151                 printk(KERN_INFO "%s invalid vm flags\n", __func__);
1152                 return -EINVAL;
1153         }
1154
1155         if ((end - start) != size) {
1156                 printk(KERN_INFO "%s invalid size %d expected %d\n",
1157                        __func__, (int)(end - start), (int) size);
1158                 return -EINVAL;
1159         }
1160
1161         if (remap_pfn_range(vma, start,
1162                             coredev->common_buffer_phys >> PAGE_SHIFT,
1163                             size, pgprot_noncached(vma->vm_page_prot)))
1164         {
1165                 printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1166                 return -EAGAIN;
1167         }
1168
1169         return 0;
1170 }
1171
1172 int smscore_module_init(void)
1173 {
1174         int rc = 0;
1175
1176         INIT_LIST_HEAD(&g_smscore_notifyees);
1177         INIT_LIST_HEAD(&g_smscore_devices);
1178         kmutex_init(&g_smscore_deviceslock);
1179
1180         INIT_LIST_HEAD(&g_smscore_registry);
1181         kmutex_init(&g_smscore_registrylock);
1182
1183         /* USB Register */
1184         rc = smsusb_register();
1185
1186         /* DVB Register */
1187         rc = smsdvb_register();
1188
1189         printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1190
1191         return rc;
1192 }
1193
1194 void smscore_module_exit(void)
1195 {
1196
1197         kmutex_lock(&g_smscore_deviceslock);
1198         while (!list_empty(&g_smscore_notifyees)) {
1199                 smscore_device_notifyee_t *notifyee =
1200                         (smscore_device_notifyee_t *) g_smscore_notifyees.next;
1201
1202                 list_del(&notifyee->entry);
1203                 kfree(notifyee);
1204         }
1205         kmutex_unlock(&g_smscore_deviceslock);
1206
1207         kmutex_lock(&g_smscore_registrylock);
1208         while (!list_empty(&g_smscore_registry)) {
1209                 smscore_registry_entry_t *entry =
1210                         (smscore_registry_entry_t *) g_smscore_registry.next;
1211
1212                 list_del(&entry->entry);
1213                 kfree(entry);
1214         }
1215         kmutex_unlock(&g_smscore_registrylock);
1216
1217         /* DVB UnRegister */
1218         smsdvb_unregister();
1219
1220         /* Unregister USB */
1221         smsusb_unregister();
1222
1223         printk(KERN_INFO "%s\n", __func__);
1224 }
1225
1226 module_init(smscore_module_init);
1227 module_exit(smscore_module_exit);
1228
1229 MODULE_DESCRIPTION("smscore");
1230 MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1231 MODULE_LICENSE("GPL");