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