]> err.no Git - linux-2.6/blob - drivers/media/dvb/siano/smscoreapi.c
V4L/DVB (8283): sms1xxx: 80-column cleanups
[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)->
313                                 hotplug(coredev, device, arrival);
314                 if (rc < 0)
315                         break;
316         }
317
318         return rc;
319 }
320
321 smscore_buffer_t *smscore_createbuffer(u8 *buffer, void *common_buffer,
322                                        dma_addr_t common_buffer_phys)
323 {
324         smscore_buffer_t *cb = kmalloc(sizeof(smscore_buffer_t), GFP_KERNEL);
325         if (!cb) {
326                 printk(KERN_INFO "%s kmalloc(...) failed\n", __func__);
327                 return NULL;
328         }
329
330         cb->p = buffer;
331         cb->offset_in_common = buffer - (u8 *) common_buffer;
332         cb->phys = common_buffer_phys + cb->offset_in_common;
333
334         return cb;
335 }
336
337 /**
338  * creates coredev object for a device, prepares buffers,
339  * creates buffer mappings, notifies registered hotplugs about new device.
340  *
341  * @param params device pointer to struct with device specific parameters
342  *               and handlers
343  * @param coredev pointer to a value that receives created coredev object
344  *
345  * @return 0 on success, <0 on error.
346  */
347 int smscore_register_device(smsdevice_params_t *params,
348                             smscore_device_t **coredev)
349 {
350         smscore_device_t *dev;
351         u8 *buffer;
352
353         dev = kzalloc(sizeof(smscore_device_t), GFP_KERNEL);
354         if (!dev) {
355                 printk(KERN_INFO "%s kzalloc(...) failed\n", __func__);
356                 return -ENOMEM;
357         }
358
359         /* init list entry so it could be safe in smscore_unregister_device */
360         INIT_LIST_HEAD(&dev->entry);
361
362         /* init queues */
363         INIT_LIST_HEAD(&dev->clients);
364         INIT_LIST_HEAD(&dev->buffers);
365
366         /* init locks */
367         spin_lock_init(&dev->clientslock);
368         spin_lock_init(&dev->bufferslock);
369
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);
377
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);
385                 return -ENOMEM;
386         }
387
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);
395                 if (!cb) {
396                         smscore_unregister_device(dev);
397                         return -ENOMEM;
398                 }
399
400                 smscore_putbuffer(dev, cb);
401         }
402
403         printk(KERN_INFO "%s allocated %d buffers\n",
404                __func__, dev->num_buffers);
405
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;
414
415         dev->device_flags = params->flags;
416         strcpy(dev->devpath, params->devpath);
417
418         smscore_registry_settype(dev->devpath, params->device_type);
419
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);
424
425         *coredev = dev;
426
427         printk(KERN_INFO "%s device %p created\n", __func__, dev);
428
429         return 0;
430 }
431
432 /**
433  * sets initial device mode and notifies client hotplugs that device is ready
434  *
435  * @param coredev pointer to a coredev object returned by
436  *                smscore_register_device
437  *
438  * @return 0 on success, <0 on error.
439  */
440 int smscore_start_device(smscore_device_t *coredev)
441 {
442         int rc = smscore_set_device_mode(
443                         coredev, smscore_registry_getmode(coredev->devpath));
444         if (rc < 0) {
445                 printk(KERN_INFO "%s set device mode faile , rc %d\n",
446                        __func__, rc);
447                 return rc;
448         }
449
450         kmutex_lock(&g_smscore_deviceslock);
451
452         rc = smscore_notify_callbacks(coredev, coredev->device, 1);
453
454         printk(KERN_INFO "%s device %p started, rc %d\n",
455                __func__, coredev, rc);
456
457         kmutex_unlock(&g_smscore_deviceslock);
458
459         return rc;
460 }
461
462 int smscore_sendrequest_and_wait(smscore_device_t *coredev, void *buffer,
463                                  size_t size, struct completion *completion)
464 {
465         int rc = coredev->sendrequest_handler(coredev->context, buffer, size);
466         if (rc < 0) {
467                 printk(KERN_INFO "%s sendrequest returned error %d\n",
468                        __func__, rc);
469                 return rc;
470         }
471
472         return wait_for_completion_timeout(completion,
473                                            msecs_to_jiffies(10000)) ?
474                                                 0 : -ETIME;
475 }
476
477 int smscore_load_firmware_family2(smscore_device_t *coredev, void *buffer,
478                                   size_t size)
479 {
480         SmsFirmware_ST *firmware = (SmsFirmware_ST *) buffer;
481         SmsMsgHdr_ST *msg;
482         u32 mem_address = firmware->StartAddress;
483         u8 *payload = firmware->Payload;
484         int rc = 0;
485
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);
490                 if (rc < 0)
491                         return rc;
492         }
493
494         /* PAGE_SIZE buffer shall be enough and dma aligned */
495         msg = (SmsMsgHdr_ST *) kmalloc(PAGE_SIZE, GFP_KERNEL | GFP_DMA);
496         if (!msg)
497                 return -ENOMEM;
498
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,
504                                                   msg->msgLength,
505                                                   &coredev->reload_start_done);
506                 mem_address = *(u32 *) &payload[20];
507         }
508
509         while (size && rc >= 0) {
510                 SmsDataDownload_ST *DataMsg = (SmsDataDownload_ST *) msg;
511                 int payload_size = min((int) size, SMS_MAX_PAYLOAD_SIZE);
512
513                 SMS_INIT_MSG(msg, MSG_SMS_DATA_DOWNLOAD_REQ,
514                              (u16)(sizeof(SmsMsgHdr_ST) +
515                                       sizeof(u32) + payload_size));
516
517                 DataMsg->MemAddr = mem_address;
518                 memcpy(DataMsg->Payload, payload, payload_size);
519
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);
525                 else
526                         rc = smscore_sendrequest_and_wait(
527                                 coredev, DataMsg,
528                                 DataMsg->xMsgHeader.msgLength,
529                                 &coredev->data_download_done);
530
531                 payload += payload_size;
532                 size -= payload_size;
533                 mem_address += payload_size;
534         }
535
536         if (rc >= 0) {
537                 if (coredev->mode == DEVICE_MODE_NONE) {
538                         SmsMsgData_ST *TriggerMsg = (SmsMsgData_ST *) msg;
539
540                         SMS_INIT_MSG(msg, MSG_SMS_SWDOWNLOAD_TRIGGER_REQ,
541                                      sizeof(SmsMsgHdr_ST) +
542                                      sizeof(u32) * 5);
543
544                         TriggerMsg->msgData[0] = firmware->StartAddress;
545                                                 /* Entry point */
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 */
550
551                         if (coredev->device_flags & SMS_ROM_NO_RESPONSE) {
552                                 rc = coredev->sendrequest_handler(
553                                         coredev->context, TriggerMsg,
554                                         TriggerMsg->xMsgHeader.msgLength);
555                                 msleep(100);
556                         } else
557                                 rc = smscore_sendrequest_and_wait(
558                                         coredev, TriggerMsg,
559                                         TriggerMsg->xMsgHeader.msgLength,
560                                         &coredev->trigger_done);
561                 } else {
562                         SMS_INIT_MSG(msg, MSG_SW_RELOAD_EXEC_REQ,
563                                      sizeof(SmsMsgHdr_ST));
564
565                         rc = coredev->sendrequest_handler(coredev->context,
566                                                           msg, msg->msgLength);
567                 }
568                 msleep(500);
569         }
570
571         printk("%s rc=%d, postload=%p \n", __func__, rc,
572                coredev->postload_handler);
573
574         kfree(msg);
575
576         return ((rc >= 0) && coredev->postload_handler) ?
577                 coredev->postload_handler(coredev->context) :
578                 rc;
579 }
580
581 /**
582  * loads specified firmware into a buffer and calls device loadfirmware_handler
583  *
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
588  *
589  * @return 0 on success, <0 on error.
590  */
591 int smscore_load_firmware_from_file(smscore_device_t *coredev, char *filename,
592                                     loadfirmware_t loadfirmware_handler)
593 {
594         int rc = -ENOENT;
595
596         const struct firmware *fw;
597         u8 *fw_buffer;
598
599         if (loadfirmware_handler == NULL && !(coredev->device_flags &
600                                               SMS_DEVICE_FAMILY2))
601                 return -EINVAL;
602
603         rc = request_firmware(&fw, filename, coredev->device);
604         if (rc < 0) {
605                 printk(KERN_INFO "%s failed to open \"%s\"\n",
606                        __func__, filename);
607                 return rc;
608         }
609         printk(KERN_INFO "%s read FW %s, size=%d\"\n", __func__,
610                filename, fw->size);
611         fw_buffer = kmalloc(ALIGN(fw->size, SMS_ALLOC_ALIGNMENT),
612                             GFP_KERNEL | GFP_DMA);
613         if (fw_buffer) {
614                 memcpy(fw_buffer, fw->data, fw->size);
615
616                 rc = (coredev->device_flags & SMS_DEVICE_FAMILY2) ?
617                       smscore_load_firmware_family2(coredev,
618                                                     fw_buffer,
619                                                     fw->size) :
620                       loadfirmware_handler(coredev->context,
621                                            fw_buffer, fw->size);
622
623                 kfree(fw_buffer);
624         } else {
625                 printk(KERN_INFO "%s failed to allocate firmware buffer\n",
626                        __func__);
627                 rc = -ENOMEM;
628         }
629
630         release_firmware(fw);
631
632         return rc;
633 }
634
635 int smscore_load_firmware_from_buffer(smscore_device_t *coredev, u8 *buffer,
636                                       int size, int new_mode)
637 {
638         PERROR("Feature not implemented yet\n");
639         return -EFAULT;
640 }
641
642 /**
643  * notifies all clients registered with the device, notifies hotplugs,
644  * frees all buffers and coredev object
645  *
646  * @param coredev pointer to a coredev object returned by
647  *                smscore_register_device
648  *
649  * @return 0 on success, <0 on error.
650  */
651 void smscore_unregister_device(smscore_device_t *coredev)
652 {
653         smscore_buffer_t *cb;
654         int num_buffers = 0;
655         int retry = 0;
656
657         kmutex_lock(&g_smscore_deviceslock);
658
659         smscore_notify_clients(coredev);
660         smscore_notify_callbacks(coredev, NULL, 0);
661
662         /* at this point all buffers should be back
663          * onresponse must no longer be called */
664
665         while (1) {
666                 while ((cb = smscore_getbuffer(coredev))) {
667                         kfree(cb);
668                         num_buffers++;
669                 }
670                 if (num_buffers == coredev->num_buffers)
671                         break;
672                 if (++retry > 10) {
673                         printk(KERN_INFO "%s exiting although "
674                                "not all buffers released.\n", __func__);
675                         break;
676                 }
677
678                 printk(KERN_INFO "%s waiting for %d buffer(s)\n", __func__,
679                        coredev->num_buffers - num_buffers);
680                 msleep(100);
681         }
682
683         printk(KERN_INFO "%s freed %d buffers\n", __func__, num_buffers);
684
685         if (coredev->common_buffer)
686                 dma_free_coherent(NULL, coredev->common_buffer_size,
687                                   coredev->common_buffer,
688                                   coredev->common_buffer_phys);
689
690         list_del(&coredev->entry);
691         kfree(coredev);
692
693         kmutex_unlock(&g_smscore_deviceslock);
694
695         printk(KERN_INFO "%s device %p destroyed\n", __func__, coredev);
696 }
697
698 int smscore_detect_mode(smscore_device_t *coredev)
699 {
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);
703         int rc;
704
705         if (!buffer)
706                 return -ENOMEM;
707
708         SMS_INIT_MSG(msg, MSG_SMS_GET_VERSION_EX_REQ, sizeof(SmsMsgHdr_ST));
709
710         rc = smscore_sendrequest_and_wait(coredev, msg, msg->msgLength,
711                                           &coredev->version_ex_done);
712         if (rc == -ETIME) {
713                 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed first try\n",
714                        __func__);
715
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);
721                         if (rc < 0)
722                                 printk("%s: MSG_SMS_GET_VERSION_EX_REQ failed "
723                                        "second try, rc %d\n", __func__, rc);
724                 } else
725                         rc = -ETIME;
726         }
727
728         kfree(buffer);
729
730         return rc;
731 }
732
733 char *smscore_fw_lkup[][SMS_NUM_OF_DEVICE_TYPES] = {
734         /*Stellar                       NOVA A0                 Nova B0                         VEGA*/
735         /*DVBT*/  {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
736         /*DVBH*/  {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
737         /*TDMB*/  {"none", "tdmb_nova_12mhz.inp", "none", "none"},
738         /*DABIP*/ {"none", "none", "none", "none"},
739         /*BDA*/   {"none", "dvb_nova_12mhz.inp", "dvb_nova_12mhz_b0.inp", "none"},
740         /*ISDBT*/ {"none", "isdbt_nova_12mhz.inp", "dvb_nova_12mhz.inp", "none"},
741         /*ISDBTBDA*/{"none", "isdbt_nova_12mhz.inp", "isdbt_nova_12mhz_b0.inp", "none"},
742         /*CMMB*/  {"none", "none", "none", "cmmb_vega_12mhz.inp"}
743 };
744
745
746 /**
747  * calls device handler to change mode of operation
748  * NOTE: stellar/usb may disconnect when changing mode
749  *
750  * @param coredev pointer to a coredev object returned by
751  *                smscore_register_device
752  * @param mode requested mode of operation
753  *
754  * @return 0 on success, <0 on error.
755  */
756 int smscore_set_device_mode(smscore_device_t *coredev, int mode)
757 {
758         void *buffer;
759         int rc = 0;
760         sms_device_type_st type;
761
762         PDEBUG("set device mode to %d\n", mode);
763         if (coredev->device_flags & SMS_DEVICE_FAMILY2) {
764                 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_RAW_TUNER) {
765                         printk(KERN_INFO "%s invalid mode specified %d\n",
766                                __func__, mode);
767                         return -EINVAL;
768                 }
769
770                 smscore_registry_setmode(coredev->devpath, mode);
771
772                 if (!(coredev->device_flags & SMS_DEVICE_NOT_READY)) {
773                         rc = smscore_detect_mode(coredev);
774                         if (rc < 0) {
775                                 printk(KERN_INFO "%s mode detect failed %d\n",
776                                        __func__, rc);
777                                 return rc;
778                         }
779                 }
780
781                 if (coredev->mode == mode) {
782                         printk(KERN_INFO "%s device mode %d already set\n",
783                                __func__, mode);
784                         return 0;
785                 }
786
787                 if (!(coredev->modes_supported & (1 << mode))) {
788                         type = smscore_registry_gettype(coredev->devpath);
789                         rc = smscore_load_firmware_from_file(
790                                 coredev, smscore_fw_lkup[mode][type], NULL);
791                         if (rc < 0) {
792                                 printk(KERN_INFO "%s load firmware "
793                                        "failed %d\n", __func__, rc);
794                                 return rc;
795                         }
796                 } else
797                         printk(KERN_INFO "%s mode %d supported by running "
798                                "firmware\n", __func__, mode);
799
800                 buffer = kmalloc(sizeof(SmsMsgData_ST) + SMS_DMA_ALIGNMENT,
801                                  GFP_KERNEL | GFP_DMA);
802                 if (buffer) {
803                         SmsMsgData_ST *msg =
804                                 (SmsMsgData_ST *) SMS_ALIGN_ADDRESS(buffer);
805
806                         SMS_INIT_MSG(&msg->xMsgHeader, MSG_SMS_INIT_DEVICE_REQ,
807                                      sizeof(SmsMsgData_ST));
808                         msg->msgData[0] = mode;
809
810                         rc = smscore_sendrequest_and_wait(
811                                 coredev, msg, msg->xMsgHeader.msgLength,
812                                 &coredev->init_device_done);
813
814                         kfree(buffer);
815                 } else {
816                         printk(KERN_INFO "%s Could not allocate buffer for "
817                                "init device message.\n", __func__);
818                         rc = -ENOMEM;
819                 }
820         } else {
821                 if (mode < DEVICE_MODE_DVBT || mode > DEVICE_MODE_DVBT_BDA) {
822                         printk(KERN_INFO "%s invalid mode specified %d\n",
823                                __func__, mode);
824                         return -EINVAL;
825                 }
826
827                 smscore_registry_setmode(coredev->devpath, mode);
828
829                 if (coredev->detectmode_handler)
830                         coredev->detectmode_handler(coredev->context,
831                                                     &coredev->mode);
832
833                 if (coredev->mode != mode && coredev->setmode_handler)
834                         rc = coredev->setmode_handler(coredev->context, mode);
835         }
836
837         if (rc >= 0) {
838                 coredev->mode = mode;
839                 coredev->device_flags &= ~SMS_DEVICE_NOT_READY;
840         }
841
842         if (rc != 0)
843                 printk(KERN_INFO "%s return error code %d.\n", __func__, rc);
844         return rc;
845 }
846
847 /**
848  * calls device handler to get current mode of operation
849  *
850  * @param coredev pointer to a coredev object returned by
851  *                smscore_register_device
852  *
853  * @return current mode
854  */
855 int smscore_get_device_mode(smscore_device_t *coredev)
856 {
857         return coredev->mode;
858 }
859
860 /**
861  * find client by response id & type within the clients list.
862  * return client handle or NULL.
863  *
864  * @param coredev pointer to a coredev object returned by
865  *                smscore_register_device
866  * @param data_type client data type (SMS_DONT_CARE for all types)
867  * @param id client id (SMS_DONT_CARE for all id)
868  *
869  */
870 smscore_client_t *smscore_find_client(smscore_device_t *coredev,
871                                       int data_type, int id)
872 {
873         smscore_client_t *client = NULL;
874         struct list_head *next, *first;
875         unsigned long flags;
876         struct list_head *firstid, *nextid;
877
878
879         spin_lock_irqsave(&coredev->clientslock, flags);
880         first = &coredev->clients;
881         for (next = first->next;
882              (next != first) && !client;
883              next = next->next) {
884                 firstid = &((smscore_client_t *)next)->idlist;
885                 for (nextid = firstid->next;
886                      nextid != firstid;
887                      nextid = nextid->next) {
888                         if ((((smscore_idlist_t *)nextid)->id  == id) &&
889                             (((smscore_idlist_t *)nextid)->data_type  == data_type ||
890                             (((smscore_idlist_t *)nextid)->data_type  == 0))) {
891                                 client = (smscore_client_t *) next;
892                                 break;
893                         }
894                 }
895         }
896         spin_unlock_irqrestore(&coredev->clientslock, flags);
897         return client;
898 }
899
900 /**
901  * find client by response id/type, call clients onresponse handler
902  * return buffer to pool on error
903  *
904  * @param coredev pointer to a coredev object returned by
905  *                smscore_register_device
906  * @param cb pointer to response buffer descriptor
907  *
908  */
909 void smscore_onresponse(smscore_device_t *coredev, smscore_buffer_t *cb)
910 {
911         SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *)((u8 *) cb->p + cb->offset);
912         smscore_client_t *client =
913                 smscore_find_client(coredev, phdr->msgType, phdr->msgDstId);
914         int rc = -EBUSY;
915
916         static unsigned long last_sample_time = 0;
917         static int data_total = 0;
918         unsigned long time_now = jiffies_to_msecs(jiffies);
919
920         if (!last_sample_time)
921                 last_sample_time = time_now;
922
923         if (time_now - last_sample_time > 10000) {
924                 printk("\n%s data rate %d bytes/secs\n", __func__,
925                        (int)((data_total * 1000) /
926                              (time_now - last_sample_time)));
927
928                 last_sample_time = time_now;
929                 data_total = 0;
930         }
931
932         data_total += cb->size;
933         /* If no client registered for type & id,
934          * check for control client where type is not registered */
935         if (client)
936                 rc = client->onresponse_handler(client->context, cb);
937
938         if (rc < 0) {
939                 switch (phdr->msgType) {
940                 case MSG_SMS_GET_VERSION_EX_RES:
941                 {
942                         SmsVersionRes_ST *ver = (SmsVersionRes_ST *) phdr;
943                         printk("%s: MSG_SMS_GET_VERSION_EX_RES id %d "
944                                "prots 0x%x ver %d.%d\n", __func__,
945                                ver->FirmwareId, ver->SupportedProtocols,
946                                ver->RomVersionMajor, ver->RomVersionMinor);
947
948                         coredev->mode = ver->FirmwareId == 255 ?
949                                 DEVICE_MODE_NONE : ver->FirmwareId;
950                         coredev->modes_supported = ver->SupportedProtocols;
951
952                         complete(&coredev->version_ex_done);
953                         break;
954                 }
955                 case MSG_SMS_INIT_DEVICE_RES:
956                         printk("%s: MSG_SMS_INIT_DEVICE_RES\n", __func__);
957                         complete(&coredev->init_device_done);
958                         break;
959                 case MSG_SW_RELOAD_START_RES:
960                         printk("%s: MSG_SW_RELOAD_START_RES\n", __func__);
961                         complete(&coredev->reload_start_done);
962                         break;
963                 case MSG_SMS_DATA_DOWNLOAD_RES:
964                         complete(&coredev->data_download_done);
965                         break;
966                 case MSG_SW_RELOAD_EXEC_RES:
967                         printk("%s: MSG_SW_RELOAD_EXEC_RES\n", __func__);
968                         break;
969                 case MSG_SMS_SWDOWNLOAD_TRIGGER_RES:
970                         printk("%s: MSG_SMS_SWDOWNLOAD_TRIGGER_RES\n",
971                                __func__);
972                         complete(&coredev->trigger_done);
973                         break;
974                 case MSG_SMS_SLEEP_RESUME_COMP_IND:
975                         complete(&coredev->resume_done);
976                         break;
977                 default:
978                         break;
979                 }
980                 smscore_putbuffer(coredev, cb);
981         }
982 }
983
984 /**
985  * return pointer to next free buffer descriptor from core pool
986  *
987  * @param coredev pointer to a coredev object returned by
988  *                smscore_register_device
989  *
990  * @return pointer to descriptor on success, NULL on error.
991  */
992 smscore_buffer_t *smscore_getbuffer(smscore_device_t *coredev)
993 {
994         smscore_buffer_t *cb = NULL;
995         unsigned long flags;
996
997         spin_lock_irqsave(&coredev->bufferslock, flags);
998
999         if (!list_empty(&coredev->buffers)) {
1000                 cb = (smscore_buffer_t *) coredev->buffers.next;
1001                 list_del(&cb->entry);
1002         }
1003
1004         spin_unlock_irqrestore(&coredev->bufferslock, flags);
1005
1006         return cb;
1007 }
1008
1009 /**
1010  * return buffer descriptor to a pool
1011  *
1012  * @param coredev pointer to a coredev object returned by
1013  *                smscore_register_device
1014  * @param cb pointer buffer descriptor
1015  *
1016  */
1017 void smscore_putbuffer(smscore_device_t *coredev, smscore_buffer_t *cb)
1018 {
1019         list_add_locked(&cb->entry, &coredev->buffers, &coredev->bufferslock);
1020 }
1021
1022 int smscore_validate_client(smscore_device_t *coredev,
1023                             smscore_client_t *client, int data_type, int id)
1024 {
1025         smscore_idlist_t *listentry;
1026         smscore_client_t *registered_client;
1027
1028         if (!client) {
1029                 PERROR("bad parameter.\n");
1030                 return -EFAULT;
1031         }
1032         registered_client = smscore_find_client(coredev, data_type, id);
1033         if (registered_client == client)
1034                 return 0;
1035
1036         if (registered_client) {
1037                 PERROR("The msg ID already registered to another client.\n");
1038                 return -EEXIST;
1039         }
1040         listentry = kzalloc(sizeof(smscore_idlist_t), GFP_KERNEL);
1041         if (!listentry) {
1042                 PERROR("Can't allocate memory for client id.\n");
1043                 return -ENOMEM;
1044         }
1045         listentry->id = id;
1046         listentry->data_type = data_type;
1047         list_add_locked(&listentry->entry, &client->idlist,
1048                         &coredev->clientslock);
1049         return 0;
1050 }
1051
1052 /**
1053  * creates smsclient object, check that id is taken by another client
1054  *
1055  * @param coredev pointer to a coredev object from clients hotplug
1056  * @param initial_id all messages with this id would be sent to this client
1057  * @param data_type all messages of this type would be sent to this client
1058  * @param onresponse_handler client handler that is called to process incoming messages
1059  * @param onremove_handler client handler that is called when device is removed
1060  * @param context client-specific context
1061  * @param client pointer to a value that receives created smsclient object
1062  *
1063  * @return 0 on success, <0 on error.
1064  */
1065 int smscore_register_client(smscore_device_t *coredev,
1066                             smsclient_params_t *params,
1067                             smscore_client_t **client)
1068 {
1069         smscore_client_t *newclient;
1070         /* check that no other channel with same parameters exists */
1071         if (smscore_find_client(coredev, params->data_type,
1072                                 params->initial_id)) {
1073                 PERROR("Client already exist.\n");
1074                 return -EEXIST;
1075         }
1076
1077         newclient = kzalloc(sizeof(smscore_client_t), GFP_KERNEL);
1078         if (!newclient) {
1079                 PERROR("Failed to allocate memory for client.\n");
1080                 return -ENOMEM;
1081         }
1082
1083         INIT_LIST_HEAD(&newclient->idlist);
1084         newclient->coredev = coredev;
1085         newclient->onresponse_handler = params->onresponse_handler;
1086         newclient->onremove_handler = params->onremove_handler;
1087         newclient->context = params->context;
1088         list_add_locked(&newclient->entry, &coredev->clients,
1089                         &coredev->clientslock);
1090         smscore_validate_client(coredev, newclient, params->data_type,
1091                                 params->initial_id);
1092         *client = newclient;
1093         PDEBUG("%p %d %d\n", params->context, params->data_type,
1094                params->initial_id);
1095
1096         return 0;
1097 }
1098
1099 /**
1100  * frees smsclient object and all subclients associated with it
1101  *
1102  * @param client pointer to smsclient object returned by
1103  *               smscore_register_client
1104  *
1105  */
1106 void smscore_unregister_client(smscore_client_t *client)
1107 {
1108         smscore_device_t *coredev = client->coredev;
1109         unsigned long flags;
1110
1111         spin_lock_irqsave(&coredev->clientslock, flags);
1112
1113
1114         while (!list_empty(&client->idlist)) {
1115                 smscore_idlist_t *identry =
1116                         (smscore_idlist_t *) client->idlist.next;
1117                 list_del(&identry->entry);
1118                 kfree(identry);
1119         }
1120
1121         printk(KERN_INFO "%s %p\n", __func__, client->context);
1122
1123         list_del(&client->entry);
1124         kfree(client);
1125
1126         spin_unlock_irqrestore(&coredev->clientslock, flags);
1127 }
1128
1129 /**
1130  * verifies that source id is not taken by another client,
1131  * calls device handler to send requests to the device
1132  *
1133  * @param client pointer to smsclient object returned by
1134  *               smscore_register_client
1135  * @param buffer pointer to a request buffer
1136  * @param size size (in bytes) of request buffer
1137  *
1138  * @return 0 on success, <0 on error.
1139  */
1140 int smsclient_sendrequest(smscore_client_t *client, void *buffer, size_t size)
1141 {
1142         smscore_device_t *coredev;
1143         SmsMsgHdr_ST *phdr = (SmsMsgHdr_ST *) buffer;
1144         int rc;
1145
1146         if (client == NULL) {
1147                 printk(KERN_ERR "%s Got NULL client\n", __func__);
1148                 return -EINVAL;
1149         }
1150
1151         coredev = client->coredev;
1152
1153         /* check that no other channel with same id exists */
1154         if (coredev == NULL) {
1155                 printk(KERN_ERR "%s Got NULL coredev\n", __func__);
1156                 return -EINVAL;
1157         }
1158
1159         rc = smscore_validate_client(client->coredev, client, 0,
1160                                      phdr->msgSrcId);
1161         if (rc < 0)
1162                 return rc;
1163
1164         return coredev->sendrequest_handler(coredev->context, buffer, size);
1165 }
1166
1167 /**
1168  * return the size of large (common) buffer
1169  *
1170  * @param coredev pointer to a coredev object from clients hotplug
1171  *
1172  * @return size (in bytes) of the buffer
1173  */
1174 int smscore_get_common_buffer_size(smscore_device_t *coredev)
1175 {
1176         return coredev->common_buffer_size;
1177 }
1178
1179 /**
1180  * maps common buffer (if supported by platform)
1181  *
1182  * @param coredev pointer to a coredev object from clients hotplug
1183  * @param vma pointer to vma struct from mmap handler
1184  *
1185  * @return 0 on success, <0 on error.
1186  */
1187 int smscore_map_common_buffer(smscore_device_t *coredev,
1188                                struct vm_area_struct *vma)
1189 {
1190         unsigned long end = vma->vm_end,
1191                       start = vma->vm_start,
1192                       size = PAGE_ALIGN(coredev->common_buffer_size);
1193
1194         if (!(vma->vm_flags & (VM_READ | VM_SHARED)) ||
1195              (vma->vm_flags & VM_WRITE)) {
1196                 printk(KERN_INFO "%s invalid vm flags\n", __func__);
1197                 return -EINVAL;
1198         }
1199
1200         if ((end - start) != size) {
1201                 printk(KERN_INFO "%s invalid size %d expected %d\n",
1202                        __func__, (int)(end - start), (int) size);
1203                 return -EINVAL;
1204         }
1205
1206         if (remap_pfn_range(vma, start,
1207                             coredev->common_buffer_phys >> PAGE_SHIFT,
1208                             size, pgprot_noncached(vma->vm_page_prot)))
1209         {
1210                 printk(KERN_INFO "%s remap_page_range failed\n", __func__);
1211                 return -EAGAIN;
1212         }
1213
1214         return 0;
1215 }
1216
1217 int smscore_module_init(void)
1218 {
1219         int rc = 0;
1220
1221         INIT_LIST_HEAD(&g_smscore_notifyees);
1222         INIT_LIST_HEAD(&g_smscore_devices);
1223         kmutex_init(&g_smscore_deviceslock);
1224
1225         INIT_LIST_HEAD(&g_smscore_registry);
1226         kmutex_init(&g_smscore_registrylock);
1227
1228         /* USB Register */
1229         rc = smsusb_register();
1230
1231         /* DVB Register */
1232         rc = smsdvb_register();
1233
1234         printk(KERN_INFO "%s, rc %d\n", __func__, rc);
1235
1236         return rc;
1237 }
1238
1239 void smscore_module_exit(void)
1240 {
1241
1242         kmutex_lock(&g_smscore_deviceslock);
1243         while (!list_empty(&g_smscore_notifyees)) {
1244                 smscore_device_notifyee_t *notifyee =
1245                         (smscore_device_notifyee_t *) g_smscore_notifyees.next;
1246
1247                 list_del(&notifyee->entry);
1248                 kfree(notifyee);
1249         }
1250         kmutex_unlock(&g_smscore_deviceslock);
1251
1252         kmutex_lock(&g_smscore_registrylock);
1253         while (!list_empty(&g_smscore_registry)) {
1254                 smscore_registry_entry_t *entry =
1255                         (smscore_registry_entry_t *) g_smscore_registry.next;
1256
1257                 list_del(&entry->entry);
1258                 kfree(entry);
1259         }
1260         kmutex_unlock(&g_smscore_registrylock);
1261
1262         /* DVB UnRegister */
1263         smsdvb_unregister();
1264
1265         /* Unregister USB */
1266         smsusb_unregister();
1267
1268         printk(KERN_INFO "%s\n", __func__);
1269 }
1270
1271 module_init(smscore_module_init);
1272 module_exit(smscore_module_exit);
1273
1274 MODULE_DESCRIPTION("smscore");
1275 MODULE_AUTHOR("Siano Mobile Silicon,,, (doronc@siano-ms.com)");
1276 MODULE_LICENSE("GPL");