blob: a172d2bea501b986eeb40008b38053d4758d5617 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13/*
14 * SDIO-Downloader
15 *
16 * To be used with Qualcomm's SDIO-Client connected to this host.
17 */
18
19/* INCLUDES */
20#include <linux/slab.h>
21#include <linux/module.h>
22#include <linux/delay.h>
23#include <linux/wakelock.h>
24#include <linux/mmc/card.h>
25#include <linux/dma-mapping.h>
26#include <mach/dma.h>
27#include <linux/mmc/sdio_func.h>
28#include <sdio_al_private.h>
29#include <linux/tty.h>
30#include <linux/tty_flip.h>
31#include <linux/kthread.h>
32#include <linux/version.h>
33#include <linux/errno.h>
34#include <linux/debugfs.h>
35
36/* DEFINES AND MACROS */
37#define MAX_NUM_DEVICES 1
38#define TTY_SDIO_DEV "tty_sdio_0"
39#define TTY_SDIO_DEV_TEST "tty_sdio_test_0"
40#define SDIOC_MAILBOX_ADDRESS 0
41#define SDIO_DL_BLOCK_SIZE 512
42#define SDIO_DL_MAIN_THREAD_NAME "sdio_tty_main_thread"
43#define SDIOC_DL_BUFF_ADDRESS 0
44#define SDIOC_UP_BUFF_ADDRESS 0x4
45#define SDIOC_DL_BUFF_SIZE_OFFSET 0x8
46#define SDIOC_UP_BUFF_SIZE_OFFSET 0xC
47#define SDIOC_DL_WR_PTR 0x10
48#define SDIOC_DL_RD_PTR 0x14
49#define SDIOC_UL_WR_PTR 0x18
50#define SDIOC_UL_RD_PTR 0x1C
51#define SDIOC_EXIT_PTR 0x20
52#define SDIOC_OP_MODE_PTR 0x24
53#define SDIOC_PTRS_OFFSET 0x10
54#define SDIOC_PTR_REGS_SIZE 0x10
55#define SDIOC_CFG_REGS_SIZE 0x10
56#define WRITE_RETRIES 0xFFFFFFFF
57#define INPUT_SPEED 4800
58#define OUTPUT_SPEED 4800
59#define SDIOC_EXIT_CODE 0xDEADDEAD
60#define SLEEP_MS 10
61#define PRINTING_GAP 200
62#define TIMER_DURATION 10
63#define PUSH_TIMER_DURATION 5000
64#define MULTIPLE_RATIO 1
65#define MS_IN_SEC 1000
66#define BITS_IN_BYTE 8
67#define BYTES_IN_KB 1024
68#define WRITE_TILL_END_RETRIES 5
69#define SDIO_DLD_NORMAL_MODE_NAME "SDIO DLD NORMAL MODE"
70#define SDIO_DLD_BOOT_TEST_MODE_NAME "SDIO DLD BOOT TEST MODE"
71#define SDIO_DLD_AMSS_TEST_MODE_NAME "SDIO DLD AMSS TEST MODE"
72#define TEST_NAME_MAX_SIZE 30
73#define PUSH_STRING
74#define SDIO_DLD_OUTGOING_BUFFER_SIZE (48*1024*MULTIPLE_RATIO)
75
76/* FORWARD DECLARATIONS */
77static int sdio_dld_open(struct tty_struct *tty, struct file *file);
78static void sdio_dld_close(struct tty_struct *tty, struct file *file);
79static int sdio_dld_write_callback(struct tty_struct *tty,
80 const unsigned char *buf, int count);
81static int sdio_dld_write_room(struct tty_struct *tty);
82static int sdio_dld_main_task(void *card);
83static void sdio_dld_print_info(void);
84#ifdef CONFIG_DEBUG_FS
85static int sdio_dld_debug_info_open(struct inode *inode, struct file *file);
86static ssize_t sdio_dld_debug_info_write(struct file *file,
87 const char __user *buf, size_t count, loff_t *ppos);
88#endif
89
90
91/* STRUCTURES AND TYPES */
92enum sdio_dld_op_mode {
93 SDIO_DLD_NO_MODE = 0,
94 SDIO_DLD_NORMAL_MODE = 1,
95 SDIO_DLD_BOOT_TEST_MODE = 2,
96 SDIO_DLD_AMSS_TEST_MODE = 3,
97 SDIO_DLD_NUM_OF_MODES,
98};
99
100struct sdioc_reg_sequential_chunk_ptrs {
101 unsigned int dl_wr_ptr;
102 unsigned int dl_rd_ptr;
103 unsigned int up_wr_ptr;
104 unsigned int up_rd_ptr;
105};
106
107struct sdioc_reg_sequential_chunk_cfg {
108 unsigned int dl_buff_address;
109 unsigned int up_buff_address;
110 unsigned int dl_buff_size;
111 unsigned int ul_buff_size;
112};
113
114struct sdioc_reg {
115 unsigned int reg_val;
116 unsigned int reg_offset;
117};
118
119struct sdioc_reg_chunk {
120 struct sdioc_reg dl_buff_address;
121 struct sdioc_reg up_buff_address;
122 struct sdioc_reg dl_buff_size;
123 struct sdioc_reg ul_buff_size;
124 struct sdioc_reg dl_wr_ptr;
125 struct sdioc_reg dl_rd_ptr;
126 struct sdioc_reg up_wr_ptr;
127 struct sdioc_reg up_rd_ptr;
128 struct sdioc_reg good_to_exit_ptr;
129};
130
131struct sdio_data {
132 char *data;
133 int offset_read_p;
134 int offset_write_p;
135 int buffer_size;
136 int num_of_bytes_in_use;
137};
138
139struct sdio_dld_data {
140 struct sdioc_reg_chunk sdioc_reg;
141 struct sdio_data incoming_data;
142 struct sdio_data outgoing_data;
143};
144
145struct sdio_dld_wait_event {
146 wait_queue_head_t wait_event;
147 int wake_up_signal;
148};
149
150struct sdio_dld_task {
151 struct task_struct *dld_task;
152 const char *task_name;
153 struct sdio_dld_wait_event exit_wait;
154 atomic_t please_close;
155};
156
157#ifdef CONFIG_DEBUG_FS
158struct sdio_dloader_debug {
159 struct dentry *sdio_dld_debug_root;
160 struct dentry *sdio_al_dloader;
161};
162
163const struct file_operations sdio_dld_debug_info_ops = {
164 .open = sdio_dld_debug_info_open,
165 .write = sdio_dld_debug_info_write,
166};
167#endif
168
169struct sdio_downloader {
170 int sdioc_boot_func;
171 struct sdio_dld_wait_event write_callback_event;
172 struct sdio_dld_task dld_main_thread;
173 struct tty_driver *tty_drv;
174 struct tty_struct *tty_str;
175 struct sdio_dld_data sdio_dloader_data;
176 struct mmc_card *card;
177 int(*done_callback)(void);
178 struct sdio_dld_wait_event main_loop_event;
179 struct timer_list timer;
180 unsigned int poll_ms;
181 struct timer_list push_timer;
182 unsigned int push_timer_ms;
183 enum sdio_dld_op_mode op_mode;
184 char op_mode_name[TEST_NAME_MAX_SIZE];
185};
186
187struct sdio_dld_global_info {
188 int global_bytes_write_toio;
189 int global_bytes_write_tty;
190 int global_bytes_read_fromio;
191 int global_bytes_push_tty;
192 u64 start_time;
193 u64 end_time;
194 u64 delta_jiffies;
195 unsigned int time_msec;
196 unsigned int throughput;
197 int cl_dl_wr_ptr;
198 int cl_dl_rd_ptr;
199 int cl_up_wr_ptr;
200 int cl_up_rd_ptr;
201 int host_read_ptr;
202 int host_write_ptr;
203 int cl_dl_buffer_size;
204 int cl_up_buffer_size;
205 int host_outgoing_buffer_size;
206 int cl_dl_buffer_address;
207 int cl_up_buffer_address;
208};
209
210static const struct tty_operations sdio_dloader_tty_ops = {
211 .open = sdio_dld_open,
212 .close = sdio_dld_close,
213 .write = sdio_dld_write_callback,
214 .write_room = sdio_dld_write_room,
215};
216
217/* GLOBAL VARIABLES */
218struct sdio_downloader *sdio_dld;
219struct sdio_dld_global_info sdio_dld_info;
220static char outgoing_data_buffer[SDIO_DLD_OUTGOING_BUFFER_SIZE];
221
222static DEFINE_SPINLOCK(lock1);
223static unsigned long lock_flags1;
224static DEFINE_SPINLOCK(lock2);
225static unsigned long lock_flags2;
226
227/*
228 * sdio_op_mode sets the operation mode of the sdio_dloader -
229 * it may be in NORMAL_MODE, BOOT_TEST_MODE or AMSS_TEST_MODE
230 */
231static int sdio_op_mode = (int)SDIO_DLD_NORMAL_MODE;
232module_param(sdio_op_mode, int, 0);
233
234#ifdef CONFIG_DEBUG_FS
235
236struct sdio_dloader_debug sdio_dld_debug;
237
238#define ARR_SIZE 30000
239#define SDIO_DLD_DEBUGFS_INIT_VALUE 87654321
240#define SDIO_DLD_DEBUGFS_CASE_1_CODE 11111111
241#define SDIO_DLD_DEBUGFS_CASE_2_CODE 22222222
242#define SDIO_DLD_DEBUGFS_CASE_3_CODE 33333333
243#define SDIO_DLD_DEBUGFS_CASE_4_CODE 44444444
244#define SDIO_DLD_DEBUGFS_CASE_5_CODE 55555555
245#define SDIO_DLD_DEBUGFS_CASE_6_CODE 66666666
246#define SDIO_DLD_DEBUGFS_CASE_7_CODE 77777777
247#define SDIO_DLD_DEBUGFS_CASE_8_CODE 88888888
248#define SDIO_DLD_DEBUGFS_CASE_9_CODE 99999999
249#define SDIO_DLD_DEBUGFS_CASE_10_CODE 10101010
250#define SDIO_DLD_DEBUGFS_CASE_11_CODE 11001100
251#define SDIO_DLD_DEBUGFS_CASE_12_CODE 12001200
252#define SDIO_DLD_DEBUGFS_LOOP_WAIT 7
253#define SDIO_DLD_DEBUGFS_LOOP_WAKEUP 8
254#define SDIO_DLD_DEBUGFS_CB_WAIT 3
255#define SDIO_DLD_DEBUGFS_CB_WAKEUP 4
256
257static int curr_index;
258struct ptrs {
259 int h_w_ptr;
260 int h_r_ptr;
261 int c_u_w_ptr;
262 int c_u_r_ptr;
263 int code;
264 int h_has_to_send;
265 int c_has_to_receive;
266 int min_of;
267 int reserve2;
268 int tty_count;
269 int write_tty;
270 int write_toio;
271 int loop_wait_wake;
272 int cb_wait_wake;
273 int c_d_w_ptr;
274 int c_d_r_ptr;
275 int to_read;
276 int push_to_tty;
277 int global_tty_send;
278 int global_sdio_send;
279 int global_tty_received;
280 int global_sdio_received;
281 int reserve22;
282 int reserve23;
283 int reserve24;
284 int reserve25;
285 int reserve26;
286 int reserve27;
287 int reserve28;
288 int reserve29;
289 int reserve30;
290 int reserve31;
291};
292
293struct global_data {
294 int curr_i;
295 int duration_ms;
296 int global_bytes_sent;
297 int throughput_Mbs;
298 int host_outgoing_buffer_size_KB;
299 int client_up_buffer_size_KB;
300 int client_dl_buffer_size_KB;
301 int client_dl_buffer_address;
302 int client_up_buffer_address;
303 int global_bytes_received;
304 int global_bytes_pushed;
305 int reserve11;
306 int reserve12;
307 int reserve13;
308 int reserve14;
309 int reserve15;
310 int reserve16;
311 int reserve17;
312 int reserve18;
313 int reserve19;
314 int reserve20;
315 int reserve21;
316 int reserve22;
317 int reserve23;
318 int reserve24;
319 int reserve25;
320 int reserve26;
321 int reserve27;
322 int reserve28;
323 int reserve29;
324 int reserve30;
325 int reserve31;
326 struct ptrs ptr_array[ARR_SIZE];
327};
328
329static struct global_data gd;
330static struct debugfs_blob_wrapper blob;
331static struct dentry *root;
332static struct dentry *dld;
333
334struct debugfs_global {
335 int global_8k_has;
336 int global_9k_has;
337 int global_min;
338 int global_count;
339 int global_write_tty;
340 int global_write_toio;
341 int global_bytes_cb_tty;
342 int global_to_read;
343 int global_push_to_tty;
344 int global_tty_send;
345 int global_sdio_send;
346 int global_sdio_received;
347 int global_tty_push;
348};
349
350static struct debugfs_global debugfs_glob;
351
352static void update_standard_fields(int index)
353{
354
355 gd.ptr_array[index].global_tty_send =
356 sdio_dld_info.global_bytes_write_tty;
357 gd.ptr_array[index].global_sdio_send =
358 sdio_dld_info.global_bytes_write_toio;
359 gd.ptr_array[index].global_tty_received =
360 sdio_dld_info.global_bytes_push_tty;
361 gd.ptr_array[index].global_sdio_received =
362 sdio_dld_info.global_bytes_read_fromio;
363}
364
365static void update_gd(int code)
366{
367 struct sdioc_reg_chunk *reg_str =
368 &sdio_dld->sdio_dloader_data.sdioc_reg;
369 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
370 int index = curr_index%ARR_SIZE;
371
372 gd.curr_i = curr_index;
373 gd.duration_ms = 0;
374 gd.global_bytes_sent = 0;
375 gd.throughput_Mbs = 0;
376 gd.host_outgoing_buffer_size_KB = 0;
377 gd.client_up_buffer_size_KB = 0;
378 gd.client_dl_buffer_size_KB = 0;
379 gd.client_dl_buffer_address = 0;
380 gd.client_up_buffer_address = 0;
381 gd.global_bytes_received = 0;
382 gd.global_bytes_pushed = 0;
383 gd.reserve11 = 0;
384 gd.reserve12 = 0;
385 gd.reserve13 = 0;
386 gd.reserve14 = 0;
387 gd.reserve15 = 0;
388 gd.reserve16 = 0;
389 gd.reserve17 = 0;
390 gd.reserve18 = 0;
391 gd.reserve19 = 0;
392 gd.reserve20 = 0;
393 gd.reserve21 = 0;
394 gd.reserve22 = 0;
395 gd.reserve23 = 0;
396 gd.reserve24 = 0;
397 gd.reserve25 = 0;
398 gd.reserve26 = 0;
399 gd.reserve27 = 0;
400 gd.reserve28 = 0;
401 gd.reserve29 = 0;
402 gd.reserve30 = 0;
403 gd.reserve31 = 0;
404
405 gd.ptr_array[index].h_w_ptr = SDIO_DLD_DEBUGFS_INIT_VALUE; /*0*/
406 gd.ptr_array[index].h_r_ptr = SDIO_DLD_DEBUGFS_INIT_VALUE; /*1*/
407 gd.ptr_array[index].c_u_w_ptr = SDIO_DLD_DEBUGFS_INIT_VALUE; /*2*/
408 gd.ptr_array[index].c_u_r_ptr = SDIO_DLD_DEBUGFS_INIT_VALUE; /*3*/
409 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_INIT_VALUE; /*4*/
410 gd.ptr_array[index].h_has_to_send = SDIO_DLD_DEBUGFS_INIT_VALUE;/*5*/
411 gd.ptr_array[index].c_has_to_receive =
412 SDIO_DLD_DEBUGFS_INIT_VALUE; /*6*/
413 gd.ptr_array[index].min_of = SDIO_DLD_DEBUGFS_INIT_VALUE; /*7*/
414 gd.ptr_array[index].reserve2 = SDIO_DLD_DEBUGFS_INIT_VALUE; /*8*/
415 gd.ptr_array[index].tty_count = SDIO_DLD_DEBUGFS_INIT_VALUE; /*9*/
416 gd.ptr_array[index].write_tty = SDIO_DLD_DEBUGFS_INIT_VALUE; /*A*/
417 gd.ptr_array[index].write_toio = SDIO_DLD_DEBUGFS_INIT_VALUE; /*B*/
418 gd.ptr_array[index].loop_wait_wake =
419 SDIO_DLD_DEBUGFS_INIT_VALUE; /*C*/
420 gd.ptr_array[index].cb_wait_wake = SDIO_DLD_DEBUGFS_INIT_VALUE; /*D*/
421 gd.ptr_array[index].c_d_w_ptr = SDIO_DLD_DEBUGFS_INIT_VALUE; /*E*/
422 gd.ptr_array[index].c_d_r_ptr = SDIO_DLD_DEBUGFS_INIT_VALUE; /*F*/
423 gd.ptr_array[index].to_read =
424 SDIO_DLD_DEBUGFS_INIT_VALUE; /*0x10*/
425 gd.ptr_array[index].push_to_tty =
426 SDIO_DLD_DEBUGFS_INIT_VALUE; /*0x11*/
427 gd.ptr_array[index].global_tty_send =
428 SDIO_DLD_DEBUGFS_INIT_VALUE; /*0x12*/
429 gd.ptr_array[index].global_sdio_send =
430 SDIO_DLD_DEBUGFS_INIT_VALUE; /*0x13*/
431 gd.ptr_array[index].global_tty_received =
432 SDIO_DLD_DEBUGFS_INIT_VALUE; /*0x14*/
433 gd.ptr_array[index].global_sdio_received =
434 SDIO_DLD_DEBUGFS_INIT_VALUE; /*0x15*/
435 gd.ptr_array[index].reserve22 = SDIO_DLD_DEBUGFS_INIT_VALUE;
436 gd.ptr_array[index].reserve23 = SDIO_DLD_DEBUGFS_INIT_VALUE;
437 gd.ptr_array[index].reserve24 = SDIO_DLD_DEBUGFS_INIT_VALUE;
438 gd.ptr_array[index].reserve25 = SDIO_DLD_DEBUGFS_INIT_VALUE;
439 gd.ptr_array[index].reserve26 = SDIO_DLD_DEBUGFS_INIT_VALUE;
440 gd.ptr_array[index].reserve27 = SDIO_DLD_DEBUGFS_INIT_VALUE;
441 gd.ptr_array[index].reserve28 = SDIO_DLD_DEBUGFS_INIT_VALUE;
442 gd.ptr_array[index].reserve29 = SDIO_DLD_DEBUGFS_INIT_VALUE;
443 gd.ptr_array[index].reserve30 = SDIO_DLD_DEBUGFS_INIT_VALUE;
444 gd.ptr_array[index].reserve31 = SDIO_DLD_DEBUGFS_INIT_VALUE;
445
446 switch (code) {
447 case SDIO_DLD_DEBUGFS_CASE_1_CODE:
448 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_1_CODE;
449 gd.ptr_array[index].h_w_ptr = outgoing->offset_write_p;
450 gd.ptr_array[index].h_r_ptr = outgoing->offset_read_p;
451 gd.ptr_array[index].c_u_w_ptr = reg_str->up_wr_ptr.reg_val;
452 gd.ptr_array[index].c_u_r_ptr = reg_str->up_rd_ptr.reg_val;
453 gd.ptr_array[index].c_d_w_ptr = reg_str->dl_wr_ptr.reg_val;
454 gd.ptr_array[index].c_d_r_ptr = reg_str->dl_rd_ptr.reg_val;
455 break;
456
457 case SDIO_DLD_DEBUGFS_CASE_2_CODE:
458 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_2_CODE;
459 gd.ptr_array[index].c_u_r_ptr = reg_str->up_rd_ptr.reg_val;
460 gd.ptr_array[index].c_u_w_ptr = reg_str->up_wr_ptr.reg_val;
461 gd.ptr_array[index].h_has_to_send = debugfs_glob.global_8k_has;
462 gd.ptr_array[index].c_has_to_receive =
463 debugfs_glob.global_9k_has;
464 gd.ptr_array[index].min_of = debugfs_glob.global_min;
465 break;
466
467 case SDIO_DLD_DEBUGFS_CASE_3_CODE:
468 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_3_CODE;
469 gd.ptr_array[index].h_w_ptr = outgoing->offset_write_p;
470 gd.ptr_array[index].h_r_ptr = outgoing->offset_read_p;
471 gd.ptr_array[index].write_tty = debugfs_glob.global_write_tty;
472 break;
473
474 case SDIO_DLD_DEBUGFS_CASE_4_CODE:
475 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_4_CODE;
476 gd.ptr_array[index].h_w_ptr = outgoing->offset_write_p;
477 gd.ptr_array[index].h_r_ptr = outgoing->offset_read_p;
478 gd.ptr_array[index].c_u_r_ptr = reg_str->up_rd_ptr.reg_val;
479 gd.ptr_array[index].c_u_w_ptr = reg_str->up_wr_ptr.reg_val;
480 gd.ptr_array[index].write_toio =
481 debugfs_glob.global_write_toio;
482 break;
483
484 case SDIO_DLD_DEBUGFS_CASE_5_CODE:
485 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_5_CODE;
486 gd.ptr_array[index].tty_count = debugfs_glob.global_count;
487 break;
488
489 case SDIO_DLD_DEBUGFS_CASE_6_CODE:
490 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_6_CODE;
491 gd.ptr_array[index].loop_wait_wake = 7;
492 break;
493
494 case SDIO_DLD_DEBUGFS_CASE_7_CODE:
495 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_7_CODE;
496 gd.ptr_array[index].loop_wait_wake = 8;
497 break;
498
499 case SDIO_DLD_DEBUGFS_CASE_8_CODE:
500 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_8_CODE;
501 gd.ptr_array[index].cb_wait_wake = 3;
502 break;
503
504 case SDIO_DLD_DEBUGFS_CASE_9_CODE:
505 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_9_CODE;
506 gd.ptr_array[index].cb_wait_wake = 4;
507 break;
508
509 case SDIO_DLD_DEBUGFS_CASE_10_CODE:
510 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_10_CODE;
511 gd.ptr_array[index].cb_wait_wake =
512 debugfs_glob.global_bytes_cb_tty;
513 break;
514
515 case SDIO_DLD_DEBUGFS_CASE_11_CODE:
516 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_11_CODE;
517 gd.ptr_array[index].to_read = debugfs_glob.global_to_read;
518 break;
519
520 case SDIO_DLD_DEBUGFS_CASE_12_CODE:
521 gd.ptr_array[index].code = SDIO_DLD_DEBUGFS_CASE_12_CODE;
522 gd.ptr_array[index].push_to_tty =
523 debugfs_glob.global_push_to_tty;
524 break;
525
526 default:
527 break;
528 }
529 update_standard_fields(index);
530 curr_index++;
531}
532
533static int __init bootloader_debugfs_init(void)
534{
535 /* /sys/kernel/debug/bootloader there will be dld_arr file */
536 root = debugfs_create_dir("bootloader", NULL);
537 if (!root) {
538 pr_info(MODULE_NAME ": %s - creating root dir "
539 "failed\n", __func__);
540 return -ENODEV;
541 }
542
543 blob.data = &gd;
544 blob.size = sizeof(struct global_data);
545 dld = debugfs_create_blob("dld_arr", S_IRUGO, root, &blob);
546 if (!dld) {
547 debugfs_remove_recursive(root);
548 pr_err(MODULE_NAME ": %s, failed to create debugfs entry\n",
549 __func__);
550 return -ENODEV;
551 }
552
553 return 0;
554}
555
556/*
557* for triggering the sdio_dld info use:
558* echo 1 > /sys/kernel/debug/sdio_al_dld/sdio_al_dloader_info
559*/
560static int sdio_dld_debug_init(void)
561{
562 sdio_dld_debug.sdio_dld_debug_root =
563 debugfs_create_dir("sdio_al_dld", NULL);
564 if (!sdio_dld_debug.sdio_dld_debug_root) {
565 pr_err(MODULE_NAME ": %s - Failed to create folder. "
566 "sdio_dld_debug_root is NULL",
567 __func__);
568 return -ENOENT;
569 }
570
571 sdio_dld_debug.sdio_al_dloader = debugfs_create_file(
572 "sdio_al_dloader_info",
573 S_IRUGO | S_IWUGO,
574 sdio_dld_debug.sdio_dld_debug_root,
575 NULL,
576 &sdio_dld_debug_info_ops);
577
578 if (!sdio_dld_debug.sdio_al_dloader) {
579 pr_err(MODULE_NAME ": %s - Failed to create a file. "
580 "sdio_al_dloader is NULL",
581 __func__);
582 debugfs_remove(sdio_dld_debug.sdio_dld_debug_root);
583 sdio_dld_debug.sdio_dld_debug_root = NULL;
584 return -ENOENT;
585 }
586
587 return 0;
588}
589
590static int sdio_dld_debug_info_open(struct inode *inode, struct file *file)
591{
592 file->private_data = inode->i_private;
593 return 0;
594}
595
596static ssize_t sdio_dld_debug_info_write(struct file *file,
597 const char __user *buf, size_t count, loff_t *ppos)
598{
599 sdio_dld_print_info();
600 return count;
601}
602#endif /* CONFIG_DEBUG_FS */
603
604static void sdio_dld_print_info(void)
605{
606
607 sdio_dld_info.end_time = get_jiffies_64(); /* read the current time */
608 sdio_dld_info.delta_jiffies =
609 sdio_dld_info.end_time - sdio_dld_info.start_time;
610 sdio_dld_info.time_msec = jiffies_to_msecs(sdio_dld_info.delta_jiffies);
611
612 sdio_dld_info.throughput = sdio_dld_info.global_bytes_write_toio *
613 BITS_IN_BYTE / sdio_dld_info.time_msec;
614 sdio_dld_info.throughput /= MS_IN_SEC;
615
616 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - DURATION IN MSEC = %d\n",
617 __func__,
618 sdio_dld_info.time_msec);
619
620 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - BYTES WRITTEN ON SDIO BUS "
621 "= %d...BYTES SENT BY TTY = %d",
622 __func__,
623 sdio_dld_info.global_bytes_write_toio,
624 sdio_dld_info.global_bytes_write_tty);
625
626 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - BYTES RECEIVED ON SDIO BUS "
627 "= %d...BYTES SENT TO TTY = %d",
628 __func__,
629 sdio_dld_info.global_bytes_read_fromio,
630 sdio_dld_info.global_bytes_push_tty);
631
632 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - THROUGHPUT=%d Mbit/Sec",
633 __func__, sdio_dld_info.throughput);
634
635 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT DL_BUFFER_SIZE=%d"
636 " KB..CLIENT UL_BUFFER=%d KB\n",
637 __func__,
638 sdio_dld_info.cl_dl_buffer_size/BYTES_IN_KB,
639 sdio_dld_info.cl_up_buffer_size/BYTES_IN_KB);
640
641 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - HOST OUTGOING BUFFER_SIZE"
642 "=%d KB",
643 __func__,
644 sdio_dld_info.host_outgoing_buffer_size/BYTES_IN_KB);
645
646 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT DL BUFFER "
647 "ADDRESS = 0x%x", __func__,
648 sdio_dld_info.cl_dl_buffer_address);
649
650 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT UP BUFFER "
651 "ADDRESS = 0x%x",
652 __func__,
653 sdio_dld_info.cl_up_buffer_address);
654
655 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT - UPLINK BUFFER - "
656 "READ POINTER = %d", __func__,
657 sdio_dld_info.cl_up_rd_ptr);
658
659 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT - UPLINK BUFFER - "
660 "WRITE POINTER = %d", __func__,
661 sdio_dld_info.cl_up_wr_ptr);
662
663 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT - DOWNLINK BUFFER - "
664 "READ POINTER = %d", __func__,
665 sdio_dld_info.cl_dl_rd_ptr);
666
667 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - CLIENT - DOWNLINK BUFFER - "
668 "WRITE POINTER = %d", __func__,
669 sdio_dld_info.cl_dl_wr_ptr);
670
671 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - HOST - OUTGOING BUFFER - "
672 "READ POINTER = %d", __func__,
673 sdio_dld_info.host_read_ptr);
674
675 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - HOST - OUTGOING BUFFER - "
676 "WRITE POINTER = %d", __func__,
677 sdio_dld_info.host_write_ptr);
678
679 pr_info(MODULE_NAME ": %s, FLASHLESS BOOT - END DEBUG INFO", __func__);
680}
681
682/**
683 * sdio_dld_set_op_mode
684 * sets the op_mode and the name of the op_mode. Also, in case
685 * it's invalid mode sets op_mode to SDIO_DLD_NORMAL_MODE
686 *
687 * @op_mode: the operation mode to be set
688 * @return NONE
689 */
690static void sdio_dld_set_op_mode(enum sdio_dld_op_mode op_mode)
691{
692 sdio_dld->op_mode = op_mode;
693
694 switch (op_mode) {
695 case SDIO_DLD_NORMAL_MODE:
696 memcpy(sdio_dld->op_mode_name,
697 SDIO_DLD_NORMAL_MODE_NAME, TEST_NAME_MAX_SIZE);
698 break;
699 case SDIO_DLD_BOOT_TEST_MODE:
700 memcpy(sdio_dld->op_mode_name,
701 SDIO_DLD_BOOT_TEST_MODE_NAME, TEST_NAME_MAX_SIZE);
702 break;
703 case SDIO_DLD_AMSS_TEST_MODE:
704 memcpy(sdio_dld->op_mode_name,
705 SDIO_DLD_AMSS_TEST_MODE_NAME, TEST_NAME_MAX_SIZE);
706 break;
707 default:
708 sdio_dld->op_mode = SDIO_DLD_NORMAL_MODE;
709 pr_err(MODULE_NAME ": %s - Invalid Op_Mode = %d. Settings "
710 "Op_Mode to default - NORMAL_MODE\n",
711 __func__, op_mode);
712 memcpy(sdio_dld->op_mode_name,
713 SDIO_DLD_NORMAL_MODE_NAME, TEST_NAME_MAX_SIZE);
714 break;
715 }
716
717 if (sdio_dld->op_mode_name != NULL) {
718 pr_info(MODULE_NAME ": %s - FLASHLESS BOOT - Op_Mode is set to "
719 "%s\n", __func__, sdio_dld->op_mode_name);
720 } else {
721 pr_info(MODULE_NAME ": %s - FLASHLESS BOOT - op_mode_name is "
722 "NULL\n", __func__);
723 }
724}
725
726/**
727 * sdio_dld_allocate_local_buffers
728 * allocates local outgoing and incoming buffers and also sets
729 * threshold for outgoing data.
730 *
731 * @return 0 on success or negative value on error.
732 */
733static int sdio_dld_allocate_local_buffers(void)
734{
735 struct sdioc_reg_chunk *reg_str = &sdio_dld->sdio_dloader_data.
736 sdioc_reg;
737 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
738 struct sdio_data *incoming = &sdio_dld->sdio_dloader_data.incoming_data;
739
740 incoming->data =
741 kzalloc(reg_str->dl_buff_size.reg_val, GFP_KERNEL);
742
743 if (!incoming->data) {
744 pr_err(MODULE_NAME ": %s - param ""incoming->data"" is NULL. "
745 "Couldn't allocate incoming_data local buffer\n",
746 __func__);
747 return -ENOMEM;
748 }
749
750 incoming->buffer_size = reg_str->dl_buff_size.reg_val;
751
752 outgoing->data = outgoing_data_buffer;
753
754 outgoing->buffer_size = SDIO_DLD_OUTGOING_BUFFER_SIZE;
755
756 if (outgoing->buffer_size !=
757 reg_str->ul_buff_size.reg_val*MULTIPLE_RATIO) {
758 pr_err(MODULE_NAME ": %s - HOST outgoing buffer size (%d bytes)"
759 "must be a multiple of ClIENT uplink buffer size (%d "
760 "bytes). HOST_SIZE == n*CLIENT_SIZE.(n=1,2,3...)\n",
761 __func__,
762 SDIO_DLD_OUTGOING_BUFFER_SIZE,
763 reg_str->ul_buff_size.reg_val);
764 kfree(incoming->data);
765 return -EINVAL;
766 }
767
768 /* keep sdio_dld_info up to date */
769 sdio_dld_info.host_outgoing_buffer_size = outgoing->buffer_size;
770
771 return 0;
772}
773
774/**
775 * sdio_dld_dealloc_local_buffers frees incoming and outgoing
776 * buffers.
777 *
778 * @return None.
779 */
780static void sdio_dld_dealloc_local_buffers(void)
781{
782 kfree((void *)sdio_dld->sdio_dloader_data.incoming_data.data);
783}
784
785/**
786 * mailbox_to_seq_chunk_read_cfg
787 * reads 4 configuration registers of mailbox from str_func, as
788 * a sequentail chunk in memory, and updates global struct
789 * accordingly.
790 *
791 * @str_func: a pointer to func struct.
792 * @return 0 on success or negative value on error.
793 */
794static int mailbox_to_seq_chunk_read_cfg(struct sdio_func *str_func)
795{
796 struct sdioc_reg_sequential_chunk_cfg seq_chunk;
797 struct sdioc_reg_chunk *reg = &sdio_dld->sdio_dloader_data.sdioc_reg;
798 int status = 0;
799
800 if (!str_func) {
801 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
802 __func__);
803 return -EINVAL;
804 }
805
806 sdio_claim_host(str_func);
807
808 /* reading SDIOC_MAILBOX_SIZE bytes from SDIOC_MAILBOX_ADDRESS */
809 status = sdio_memcpy_fromio(str_func,
810 (void *)&seq_chunk,
811 SDIOC_MAILBOX_ADDRESS,
812 SDIOC_CFG_REGS_SIZE);
813 if (status) {
814 pr_err(MODULE_NAME ": %s - sdio_memcpy_fromio()"
815 " READING CFG MAILBOX failed. status=%d.\n",
816 __func__, status);
817 }
818
819 sdio_release_host(str_func);
820
821 reg->dl_buff_address.reg_val = seq_chunk.dl_buff_address;
822 reg->up_buff_address.reg_val = seq_chunk.up_buff_address;
823 reg->dl_buff_size.reg_val = seq_chunk.dl_buff_size;
824 reg->ul_buff_size.reg_val = seq_chunk.ul_buff_size;
825
826 /* keep sdio_dld_info up to date */
827 sdio_dld_info.cl_dl_buffer_size = seq_chunk.dl_buff_size;
828 sdio_dld_info.cl_up_buffer_size = seq_chunk.ul_buff_size;
829 sdio_dld_info.cl_dl_buffer_address = seq_chunk.dl_buff_address;
830 sdio_dld_info.cl_up_buffer_address = seq_chunk.up_buff_address;
831
832 return status;
833}
834
835/**
836 * mailbox_to_seq_chunk_read_ptrs
837 * reads 4 pointers registers of mailbox from str_func, as a
838 * sequentail chunk in memory, and updates global struct
839 * accordingly.
840 *
841 * @str_func: a pointer to func struct.
842 * @return 0 on success or negative value on error.
843 */
844static int mailbox_to_seq_chunk_read_ptrs(struct sdio_func *str_func)
845{
846 struct sdioc_reg_sequential_chunk_ptrs seq_chunk;
847 struct sdioc_reg_chunk *reg = &sdio_dld->sdio_dloader_data.sdioc_reg;
848 int status = 0;
849
850 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
851 static int counter = 1;
852 static int offset_write_p;
853 static int offset_read_p;
854 static int up_wr_ptr;
855 static int up_rd_ptr;
856 static int dl_wr_ptr;
857 static int dl_rd_ptr;
858
859 if (!str_func) {
860 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
861 __func__);
862 return -EINVAL;
863 }
864
865 sdio_claim_host(str_func);
866
867 /* reading SDIOC_MAILBOX_SIZE bytes from SDIOC_MAILBOX_ADDRESS */
868 status = sdio_memcpy_fromio(str_func,
869 (void *)&seq_chunk,
870 SDIOC_PTRS_OFFSET,
871 SDIOC_PTR_REGS_SIZE);
872 if (status) {
873 pr_err(MODULE_NAME ": %s - sdio_memcpy_fromio()"
874 " READING PTRS MAILBOX failed. status=%d.\n",
875 __func__, status);
876 }
877
878 sdio_release_host(str_func);
879
880 reg->dl_rd_ptr.reg_val = seq_chunk.dl_rd_ptr;
881 reg->dl_wr_ptr.reg_val = seq_chunk.dl_wr_ptr;
882 reg->up_rd_ptr.reg_val = seq_chunk.up_rd_ptr;
883 reg->up_wr_ptr.reg_val = seq_chunk.up_wr_ptr;
884
885 /* keeping sdio_dld_info up to date */
886 sdio_dld_info.cl_dl_rd_ptr = seq_chunk.dl_rd_ptr;
887 sdio_dld_info.cl_dl_wr_ptr = seq_chunk.dl_wr_ptr;
888 sdio_dld_info.cl_up_rd_ptr = seq_chunk.up_rd_ptr;
889 sdio_dld_info.cl_up_wr_ptr = seq_chunk.up_wr_ptr;
890
891
892 /* DEBUG - if there was a change in value */
893 if ((offset_write_p != outgoing->offset_write_p) ||
894 (offset_read_p != outgoing->offset_read_p) ||
895 (up_wr_ptr != reg->up_wr_ptr.reg_val) ||
896 (up_rd_ptr != reg->up_rd_ptr.reg_val) ||
897 (dl_wr_ptr != reg->dl_wr_ptr.reg_val) ||
898 (dl_rd_ptr != reg->dl_rd_ptr.reg_val) ||
899 (counter % PRINTING_GAP == 0)) {
900 counter = 1;
901 pr_debug(MODULE_NAME ": %s MailBox pointers: BLOCK_SIZE=%d, "
902 "hw=%d, hr=%d, cuw=%d, cur=%d, cdw=%d, cdr=%d\n",
903 __func__,
904 SDIO_DL_BLOCK_SIZE,
905 outgoing->offset_write_p,
906 outgoing->offset_read_p,
907 reg->up_wr_ptr.reg_val,
908 reg->up_rd_ptr.reg_val,
909 reg->dl_wr_ptr.reg_val,
910 reg->dl_rd_ptr.reg_val);
911
912#ifdef CONFIG_DEBUG_FS
913 update_gd(SDIO_DLD_DEBUGFS_CASE_1_CODE);
914#endif
915 /* update static variables */
916 offset_write_p = outgoing->offset_write_p;
917 offset_read_p = outgoing->offset_read_p;
918 up_wr_ptr = reg->up_wr_ptr.reg_val;
919 up_rd_ptr = reg->up_rd_ptr.reg_val;
920 dl_wr_ptr = reg->dl_wr_ptr.reg_val;
921 dl_rd_ptr = reg->dl_rd_ptr.reg_val;
922 } else {
923 counter++;
924 }
925 return status;
926}
927
928/**
929 * sdio_dld_init_func
930 * enables the sdio func, and sets the func block size.
931 *
932 * @str_func: a pointer to func struct.
933 * @return 0 on success or negative value on error.
934 */
935static int sdio_dld_init_func(struct sdio_func *str_func)
936{
937 int status1 = 0;
938 int status2 = 0;
939
940 if (!str_func) {
941 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
942 __func__);
943 return -EINVAL;
944 }
945
946 sdio_claim_host(str_func);
947
948 status1 = sdio_enable_func(str_func);
949 if (status1) {
950 sdio_release_host(str_func);
951 pr_err(MODULE_NAME ": %s - sdio_enable_func() failed. "
952 "status=%d\n", __func__, status1);
953 return status1;
954 }
955
956 status2 = sdio_set_block_size(str_func, SDIO_DL_BLOCK_SIZE);
957 if (status2) {
958 pr_err(MODULE_NAME ": %s - sdio_set_block_size() failed. "
959 "status=%d\n", __func__, status2);
960 status1 = sdio_disable_func(str_func);
961 if (status1) {
962 pr_err(MODULE_NAME ": %s - sdio_disable_func() "
963 "failed. status=%d\n", __func__, status1);
964 }
965 sdio_release_host(str_func);
966 return status2;
967 }
968
969 sdio_release_host(str_func);
970 str_func->max_blksize = SDIO_DL_BLOCK_SIZE;
971 return 0;
972}
973
974/**
975 * sdio_dld_allocate_buffers
976 * initializes the sdio func, and then reads the mailbox, in
977 * order to allocate incoming and outgoing buffers according to
978 * the size that was read from the mailbox.
979 *
980 * @str_func: a pointer to func struct.
981 * @return 0 on success or negative value on error.
982 */
983static int sdio_dld_allocate_buffers(struct sdio_func *str_func)
984{
985 int status = 0;
986
987 if (!str_func) {
988 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
989 __func__);
990 return -EINVAL;
991 }
992
993 status = mailbox_to_seq_chunk_read_cfg(str_func);
994 if (status) {
995 pr_err(MODULE_NAME ": %s - Failure in Function "
996 "mailbox_to_seq_chunk_read_cfg(). status=%d\n",
997 __func__, status);
998 return status;
999 }
1000
1001 status = sdio_dld_allocate_local_buffers();
1002 if (status) {
1003 pr_err(MODULE_NAME ": %s - Failure in Function "
1004 "sdio_dld_allocate_local_buffers(). status=%d\n",
1005 __func__, status);
1006 return status;
1007 }
1008 return 0;
1009}
1010
1011/**
1012 * sdio_dld_create_thread
1013 * creates thread and wakes it up.
1014 *
1015 * @return 0 on success or negative value on error.
1016 */
1017static int sdio_dld_create_thread(void)
1018{
1019 sdio_dld->dld_main_thread.task_name = SDIO_DL_MAIN_THREAD_NAME;
1020
1021 sdio_dld->dld_main_thread.dld_task =
1022 kthread_create(sdio_dld_main_task,
1023 (void *)(sdio_dld->card),
1024 sdio_dld->dld_main_thread.task_name);
1025
1026 if (IS_ERR(sdio_dld->dld_main_thread.dld_task)) {
1027 pr_err(MODULE_NAME ": %s - kthread_create() failed\n",
1028 __func__);
1029 return -ENOMEM;
1030 }
1031 wake_up_process(sdio_dld->dld_main_thread.dld_task);
1032 return 0;
1033}
1034
1035/**
1036 * start_timer
1037 * sets the timer and starts.
1038 *
1039 * @timer: the timer to configure and add
1040 * @ms: the ms until it expires
1041 * @return None.
1042 */
1043static void start_timer(struct timer_list *timer, unsigned int ms)
1044{
1045 if ((ms == 0) || (timer == NULL)) {
1046 pr_err(MODULE_NAME ": %s - invalid parameter", __func__);
1047 } else {
1048 timer->expires = jiffies +
1049 msecs_to_jiffies(ms);
1050 add_timer(timer);
1051 }
1052}
1053
1054/**
1055 * sdio_dld_timer_handler
1056 * this is the timer handler. whenever it is invoked, it wakes
1057 * up the main loop task, and the write callback, and starts
1058 * the timer again.
1059 *
1060 * @data: a pointer to the tty device driver structure.
1061 * @return None.
1062 */
1063
1064static void sdio_dld_timer_handler(unsigned long data)
1065{
1066 pr_debug(MODULE_NAME " Timer Expired\n");
1067 spin_lock_irqsave(&lock2, lock_flags2);
1068 if (sdio_dld->main_loop_event.wake_up_signal == 0) {
1069 sdio_dld->main_loop_event.wake_up_signal = 1;
1070 wake_up(&sdio_dld->main_loop_event.wait_event);
1071 }
1072 spin_unlock_irqrestore(&lock2, lock_flags2);
1073
1074 sdio_dld->write_callback_event.wake_up_signal = 1;
1075 wake_up(&sdio_dld->write_callback_event.wait_event);
1076
1077 start_timer(&sdio_dld->timer, sdio_dld->poll_ms);
1078}
1079
1080/**
1081 * sdio_dld_push_timer_handler
1082 * this is a timer handler of the push_timer.
1083 *
1084 * @data: a pointer to the tty device driver structure.
1085 * @return None.
1086 */
1087static void sdio_dld_push_timer_handler(unsigned long data)
1088{
1089 pr_err(MODULE_NAME " %s - Push Timer Expired... Trying to "
1090 "push data to TTY Core for over then %d ms.\n",
1091 __func__, sdio_dld->push_timer_ms);
1092}
1093
1094/**
1095 * sdio_dld_open
1096 * this is the open callback of the tty driver.
1097 * it initializes the sdio func, allocates the buffers, and
1098 * creates the main thread.
1099 *
1100 * @tty: a pointer to the tty struct.
1101 * @file: file descriptor.
1102 * @return 0 on success or negative value on error.
1103 */
1104static int sdio_dld_open(struct tty_struct *tty, struct file *file)
1105{
1106 int status = 0;
1107 int func_in_array =
1108 REAL_FUNC_TO_FUNC_IN_ARRAY(sdio_dld->sdioc_boot_func);
1109 struct sdio_func *str_func = sdio_dld->card->sdio_func[func_in_array];
1110
1111 sdio_dld->tty_str = tty;
1112 sdio_dld->tty_str->low_latency = 1;
1113 sdio_dld->tty_str->icanon = 0;
1114 set_bit(TTY_NO_WRITE_SPLIT, &sdio_dld->tty_str->flags);
1115
1116 pr_info(MODULE_NAME ": %s, TTY DEVICE FOR FLASHLESS BOOT OPENED\n",
1117 __func__);
1118 sdio_dld_info.start_time = get_jiffies_64(); /* read the current time */
1119
1120 if (!tty) {
1121 pr_err(MODULE_NAME ": %s - param ""tty"" is NULL.\n",
1122 __func__);
1123 return -EINVAL;
1124 }
1125
1126 if (!str_func) {
1127 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1128 __func__);
1129 return -EINVAL;
1130 }
1131
1132 atomic_set(&sdio_dld->dld_main_thread.please_close, 0);
1133 sdio_dld->dld_main_thread.exit_wait.wake_up_signal = 0;
1134
1135 status = sdio_dld_allocate_buffers(str_func);
1136 if (status) {
1137 pr_err(MODULE_NAME ": %s, failed in "
1138 "sdio_dld_allocate_buffers(). status=%d\n",
1139 __func__, status);
1140 return status;
1141 }
1142
1143 status = sdio_dld_create_thread();
1144 if (status) {
1145 sdio_dld_dealloc_local_buffers();
1146 pr_err(MODULE_NAME ": %s, failed in sdio_dld_create_thread()."
1147 "status=%d\n", __func__, status);
1148 return status;
1149 }
1150
1151 /* init waiting event of the write callback */
1152 init_waitqueue_head(&sdio_dld->write_callback_event.wait_event);
1153
1154 /* init waiting event of the main loop */
1155 init_waitqueue_head(&sdio_dld->main_loop_event.wait_event);
1156
1157 /* configure and init the timer */
1158 sdio_dld->poll_ms = TIMER_DURATION;
1159 init_timer(&sdio_dld->timer);
1160 sdio_dld->timer.data = (unsigned long) sdio_dld;
1161 sdio_dld->timer.function = sdio_dld_timer_handler;
1162 sdio_dld->timer.expires = jiffies +
1163 msecs_to_jiffies(sdio_dld->poll_ms);
1164 add_timer(&sdio_dld->timer);
1165
1166 sdio_dld->push_timer_ms = PUSH_TIMER_DURATION;
1167 init_timer(&sdio_dld->push_timer);
1168 sdio_dld->push_timer.data = (unsigned long) sdio_dld;
1169 sdio_dld->push_timer.function = sdio_dld_push_timer_handler;
1170
1171 return 0;
1172}
1173
1174/**
1175 * sdio_dld_close
1176 * this is the close callback of the tty driver. it requests
1177 * the main thread to exit, and waits for notification of it.
1178 * it also de-allocates the buffers, and unregisters the tty
1179 * driver and device.
1180 *
1181 * @tty: a pointer to the tty struct.
1182 * @file: file descriptor.
1183 * @return None.
1184 */
1185static void sdio_dld_close(struct tty_struct *tty, struct file *file)
1186{
1187 int status = 0;
1188 struct sdioc_reg_chunk *reg = &sdio_dld->sdio_dloader_data.sdioc_reg;
1189
1190 /* informing the SDIOC that it can exit boot phase */
1191 sdio_dld->sdio_dloader_data.sdioc_reg.good_to_exit_ptr.reg_val =
1192 SDIOC_EXIT_CODE;
1193
1194 atomic_set(&sdio_dld->dld_main_thread.please_close, 1);
1195
1196 pr_debug(MODULE_NAME ": %s - CLOSING - WAITING...", __func__);
1197
1198 wait_event(sdio_dld->dld_main_thread.exit_wait.wait_event,
1199 sdio_dld->dld_main_thread.exit_wait.wake_up_signal);
1200 pr_debug(MODULE_NAME ": %s - CLOSING - WOKE UP...", __func__);
1201
1202 del_timer_sync(&sdio_dld->timer);
1203 del_timer_sync(&sdio_dld->push_timer);
1204
1205 sdio_dld_dealloc_local_buffers();
1206
1207 tty_unregister_device(sdio_dld->tty_drv, 0);
1208
1209 status = tty_unregister_driver(sdio_dld->tty_drv);
1210
1211 if (status) {
1212 pr_err(MODULE_NAME ": %s - tty_unregister_driver() failed\n",
1213 __func__);
1214 }
1215
1216#ifdef CONFIG_DEBUG_FS
1217 gd.curr_i = curr_index;
1218 gd.duration_ms = sdio_dld_info.time_msec;
1219 gd.global_bytes_sent = sdio_dld_info.global_bytes_write_toio;
1220 gd.global_bytes_received = 0;
1221 gd.throughput_Mbs = sdio_dld_info.throughput;
1222 gd.host_outgoing_buffer_size_KB = sdio_dld->sdio_dloader_data.
1223 outgoing_data.buffer_size/BYTES_IN_KB;
1224 gd.client_up_buffer_size_KB = reg->ul_buff_size.reg_val/BYTES_IN_KB;
1225 gd.client_dl_buffer_size_KB = reg->dl_buff_size.reg_val/BYTES_IN_KB;
1226 gd.client_dl_buffer_address = reg->dl_buff_address.reg_val;
1227 gd.client_up_buffer_address = reg->up_buff_address.reg_val;
1228 gd.global_bytes_received = sdio_dld_info.global_bytes_read_fromio;
1229 gd.global_bytes_pushed = sdio_dld_info.global_bytes_push_tty;
1230#endif
1231
1232 /* saving register values before deallocating sdio_dld
1233 in order to use it in sdio_dld_print_info() through shell command */
1234 sdio_dld_info.cl_dl_rd_ptr = reg->dl_rd_ptr.reg_val;
1235 sdio_dld_info.cl_dl_wr_ptr = reg->dl_wr_ptr.reg_val;
1236 sdio_dld_info.cl_up_rd_ptr = reg->up_rd_ptr.reg_val;
1237 sdio_dld_info.cl_up_wr_ptr = reg->up_wr_ptr.reg_val;
1238
1239 sdio_dld_info.host_read_ptr =
1240 sdio_dld->sdio_dloader_data.outgoing_data.offset_read_p;
1241
1242 sdio_dld_info.host_write_ptr =
1243 sdio_dld->sdio_dloader_data.outgoing_data.offset_write_p;
1244
1245 sdio_dld_info.cl_dl_buffer_size =
1246 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_size.reg_val;
1247
1248 sdio_dld_info.cl_up_buffer_size =
1249 sdio_dld->sdio_dloader_data.sdioc_reg.ul_buff_size.reg_val;
1250
1251 sdio_dld_info.host_outgoing_buffer_size =
1252 sdio_dld->sdio_dloader_data.outgoing_data.buffer_size;
1253
1254 sdio_dld_info.cl_dl_buffer_address =
1255 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_address.reg_val;
1256
1257 sdio_dld_info.cl_up_buffer_address =
1258 sdio_dld->sdio_dloader_data.sdioc_reg.up_buff_address.reg_val;
1259
1260 sdio_dld_print_info();
1261
1262 if (sdio_dld->done_callback)
1263 sdio_dld->done_callback();
1264
1265 pr_info(MODULE_NAME ": %s - Freeing sdio_dld data structure, and "
1266 " returning...", __func__);
1267 kfree(sdio_dld);
1268}
1269
1270/**
1271 * writing_size_to_buf
1272 * writes from src buffer into dest buffer. if dest buffer
1273 * reaches its end, rollover happens.
1274 *
1275 * @dest: destination buffer.
1276 * @src: source buffer.
1277 * @dest_wr_ptr: writing pointer in destination buffer.
1278 * @dest_size: destination buffer size.
1279 * @dest_rd_ptr: reading pointer in destination buffer.
1280 * @size_to_write: size of bytes to write.
1281 * @return -how many bytes actually written to destination
1282 * buffer.
1283 *
1284 * ONLY destination buffer is treated as cyclic buffer.
1285 */
1286static int writing_size_to_buf(char *dest,
1287 const unsigned char *src,
1288 int *dest_wr_ptr,
1289 int dest_size,
1290 int dest_rd_ptr,
1291 int size_to_write)
1292{
1293 int actually_written = 0;
1294 int size_to_add = *dest_wr_ptr;
1295
1296 if (!dest) {
1297 pr_err(MODULE_NAME ": %s - param ""dest"" is NULL.\n",
1298 __func__);
1299 return -EINVAL;
1300 }
1301
1302 if (!src) {
1303 pr_err(MODULE_NAME ": %s - param ""src"" is NULL.\n",
1304 __func__);
1305 return -EINVAL;
1306 }
1307
1308 if (!dest_wr_ptr) {
1309 pr_err(MODULE_NAME ": %s - param ""dest_wr_ptr"" is NULL.\n",
1310 __func__);
1311 return -EINVAL;
1312 }
1313
1314 for (actually_written = 0 ;
1315 actually_written < size_to_write ; ++actually_written) {
1316 /* checking if buffer is full */
1317 if (((size_to_add + 1) % dest_size) == dest_rd_ptr) {
1318 *dest_wr_ptr = size_to_add;
1319 return actually_written;
1320 }
1321
1322 dest[size_to_add] = src[actually_written];
1323 size_to_add = (size_to_add+1)%dest_size;
1324 }
1325
1326 *dest_wr_ptr = size_to_add;
1327
1328 return actually_written;
1329}
1330
1331/**
1332 * sdioc_bytes_till_end_of_buffer - this routine calculates how many bytes are
1333 * empty/in use. if calculation requires rap around - it will ignore the rap
1334 * around and will do the calculation untill the end of the buffer
1335 *
1336 * @write_ptr: writing pointer.
1337 * @read_ptr: reading pointer.
1338 * @total_size: buffer size.
1339 * @free_bytes: return value-how many free bytes.
1340 * @bytes_in_use: return value-how many bytes in use.
1341 * @return 0 on success or negative value on error.
1342 *
1343 * buffer is treated as a cyclic buffer.
1344 */
1345static int sdioc_bytes_till_end_of_buffer(int write_ptr,
1346 int read_ptr,
1347 int total_size,
1348 int *free_bytes,
1349 int *bytes_in_use)
1350{
1351 if (!free_bytes) {
1352 pr_err(MODULE_NAME ": %s - param ""free_bytes"" is NULL.\n",
1353 __func__);
1354 return -EINVAL;
1355 }
1356
1357 if (!bytes_in_use) {
1358 pr_err(MODULE_NAME ": %s - param ""bytes_in_use"" is NULL.\n",
1359 __func__);
1360 return -EINVAL;
1361 }
1362
1363 if (write_ptr >= read_ptr) {
1364 if (read_ptr == 0)
1365 *free_bytes = total_size - write_ptr - 1;
1366 else
1367 *free_bytes = total_size - write_ptr;
1368 *bytes_in_use = write_ptr - read_ptr;
1369 } else {
1370 *bytes_in_use = total_size - read_ptr;
1371 *free_bytes = read_ptr - write_ptr - 1;
1372 }
1373
1374 return 0;
1375}
1376
1377/**
1378 * sdioc_bytes_free_in_buffer
1379 * this routine calculates how many bytes are free in a buffer
1380 * and how many are in use, according to its reading and
1381 * writing pointer offsets.
1382 *
1383 * @write_ptr: writing pointer.
1384 * @read_ptr: reading pointer.
1385 * @total_size: buffer size.
1386 * @free_bytes: return value-how many free bytes in buffer.
1387 * @bytes_in_use: return value-how many bytes in use in buffer.
1388 * @return 0 on success or negative value on error.
1389 *
1390 * buffer is treated as a cyclic buffer.
1391 */
1392static int sdioc_bytes_free_in_buffer(int write_ptr,
1393 int read_ptr,
1394 int total_size,
1395 int *free_bytes,
1396 int *bytes_in_use)
1397{
1398 if (!free_bytes) {
1399 pr_err(MODULE_NAME ": %s - param ""free_bytes"" is NULL.\n",
1400 __func__);
1401 return -EINVAL;
1402 }
1403
1404 if (!bytes_in_use) {
1405 pr_err(MODULE_NAME ": %s - param ""bytes_in_use"" is NULL.\n",
1406 __func__);
1407 return -EINVAL;
1408 }
1409
1410 /* if pointers equel - buffers are empty. nothing to read/write */
1411
1412 if (write_ptr >= read_ptr)
1413 *bytes_in_use = write_ptr - read_ptr;
1414 else
1415 *bytes_in_use = total_size - (read_ptr - write_ptr);
1416
1417 *free_bytes = total_size - *bytes_in_use - 1;
1418
1419 return 0;
1420}
1421
1422/*
1423* sdio_dld_write_room
1424*
1425* This is the write_room function of the tty driver.
1426*
1427* @tty: pointer to tty struct.
1428* @return free bytes for write.
1429*
1430*/
1431static int sdio_dld_write_room(struct tty_struct *tty)
1432{
1433 return sdio_dld->sdio_dloader_data.outgoing_data.buffer_size;
1434}
1435
1436/**
1437 * sdio_dld_write_callback
1438 * this is the write callback of the tty driver.
1439 *
1440 * @tty: pointer to tty struct.
1441 * @buf: buffer to write from.
1442 * @count: number of bytes to write.
1443 * @return bytes written or negative value on error.
1444 *
1445 * if destination buffer has not enough room for the incoming
1446 * data, returns an error.
1447 */
1448static int sdio_dld_write_callback(struct tty_struct *tty,
1449 const unsigned char *buf, int count)
1450{
1451 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
1452 int dst_free_bytes = 0;
1453 int dummy = 0;
1454 int status = 0;
1455 int bytes_written = 0;
1456 int total_written = 0;
1457 static int write_retry;
1458 int pending_to_write = count;
1459
1460#ifdef CONFIG_DEBUG_FS
1461 debugfs_glob.global_count = count;
1462 update_gd(SDIO_DLD_DEBUGFS_CASE_5_CODE);
1463#endif
1464
1465 pr_debug(MODULE_NAME ": %s - WRITING CALLBACK CALLED WITH %d bytes\n",
1466 __func__, count);
1467
1468 if (!outgoing->data) {
1469 pr_err(MODULE_NAME ": %s - param ""outgoing->data"" is NULL.\n",
1470 __func__);
1471 return -EINVAL;
1472 }
1473
1474 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK size to write to outgoing"
1475 " buffer %d\n", __func__, count);
1476
1477 /* as long as there is something to write to outgoing buffer */
1478 do {
1479 int bytes_to_write = 0;
1480 status = sdioc_bytes_free_in_buffer(
1481 outgoing->offset_write_p,
1482 outgoing->offset_read_p,
1483 outgoing->buffer_size,
1484 &dst_free_bytes,
1485 &dummy);
1486
1487 if (status) {
1488 pr_err(MODULE_NAME ": %s - Failure in Function "
1489 "sdioc_bytes_free_in_buffer(). status=%d\n",
1490 __func__, status);
1491 return status;
1492 }
1493
1494 /*
1495 * if there is free room in outgoing buffer
1496 * lock mutex and request trigger notification from the main
1497 * task. unlock mutex, and wait for sinal
1498 */
1499 if (dst_free_bytes > 0) {
1500 write_retry = 0;
1501 /*
1502 * if there is more data to write to outgoing buffer
1503 * than it can receive, wait for signal from main task
1504 */
1505 if (pending_to_write > dst_free_bytes) {
1506
1507 /* sampling updated dst_free_bytes */
1508 status = sdioc_bytes_free_in_buffer(
1509 outgoing->offset_write_p,
1510 outgoing->offset_read_p,
1511 outgoing->buffer_size,
1512 &dst_free_bytes,
1513 &dummy);
1514
1515 if (status) {
1516 pr_err(MODULE_NAME ": %s - Failure in "
1517 "Function "
1518 "sdioc_bytes_free_in_buffer(). "
1519 "status=%d\n", __func__, status);
1520 return status;
1521 }
1522 }
1523
1524 bytes_to_write = min(pending_to_write, dst_free_bytes);
1525 bytes_written =
1526 writing_size_to_buf(outgoing->data,
1527 buf+total_written,
1528 &outgoing->offset_write_p,
1529 outgoing->buffer_size,
1530 outgoing->offset_read_p,
1531 bytes_to_write);
1532
1533 /* keeping sdio_dld_info up to date */
1534 sdio_dld_info.host_write_ptr =
1535 sdio_dld->sdio_dloader_data.
1536 outgoing_data.offset_write_p;
1537
1538#ifdef CONFIG_DEBUG_FS
1539 debugfs_glob.global_write_tty = bytes_written;
1540 update_gd(SDIO_DLD_DEBUGFS_CASE_3_CODE);
1541#endif
1542 sdio_dld_info.global_bytes_write_tty += bytes_written;
1543
1544 spin_lock_irqsave(&lock2, lock_flags2);
1545 if (sdio_dld->main_loop_event.wake_up_signal == 0) {
1546 sdio_dld->main_loop_event.wake_up_signal = 1;
1547 wake_up(&sdio_dld->main_loop_event.wait_event);
1548 }
1549 spin_unlock_irqrestore(&lock2, lock_flags2);
1550
1551 /*
1552 * although outgoing buffer has enough room, writing
1553 * failed
1554 */
1555 if (bytes_written != bytes_to_write) {
1556 pr_err(MODULE_NAME ": %s - couldn't write "
1557 "%d bytes to " "outgoing buffer."
1558 "bytes_written=%d\n",
1559 __func__, bytes_to_write,
1560 bytes_written);
1561 return -EIO;
1562 }
1563
1564 total_written += bytes_written;
1565 pending_to_write -= bytes_written;
1566 outgoing->num_of_bytes_in_use += bytes_written;
1567
1568 pr_debug(MODULE_NAME ": %s - WRITE CHUNK to outgoing "
1569 "buffer. pending_to_write=%d, "
1570 "outgoing_free_bytes=%d, "
1571 "bytes_written=%d\n",
1572 __func__,
1573 pending_to_write,
1574 dst_free_bytes,
1575 bytes_written);
1576
1577 } else {
1578 write_retry++;
1579
1580 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK - NO ROOM."
1581 " pending_to_write=%d, write_retry=%d\n",
1582 __func__,
1583 pending_to_write,
1584 write_retry);
1585
1586 spin_lock_irqsave(&lock1, lock_flags1);
1587 sdio_dld->write_callback_event.wake_up_signal = 0;
1588 spin_unlock_irqrestore(&lock1, lock_flags1);
1589
1590 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK - "
1591 "WAITING...", __func__);
1592#ifdef CONFIG_DEBUG_FS
1593 update_gd(SDIO_DLD_DEBUGFS_CASE_8_CODE);
1594#endif
1595 wait_event(sdio_dld->write_callback_event.wait_event,
1596 sdio_dld->write_callback_event.
1597 wake_up_signal);
1598#ifdef CONFIG_DEBUG_FS
1599 update_gd(SDIO_DLD_DEBUGFS_CASE_9_CODE);
1600#endif
1601 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK - "
1602 "WOKE UP...", __func__);
1603 }
1604 } while (pending_to_write > 0 && write_retry < WRITE_RETRIES);
1605
1606 if (pending_to_write > 0) {
1607
1608 pr_err(MODULE_NAME ": %s - WRITE CALLBACK - pending data is "
1609 "%d out of %d > 0. total written in this "
1610 "callback = %d\n",
1611 __func__, pending_to_write, count, total_written);
1612 }
1613
1614 if (write_retry == WRITE_RETRIES) {
1615 pr_err(MODULE_NAME ": %s, write_retry=%d= max\n",
1616 __func__, write_retry);
1617 }
1618
1619#ifdef CONFIG_DEBUG_FS
1620 debugfs_glob.global_bytes_cb_tty = total_written;
1621 update_gd(SDIO_DLD_DEBUGFS_CASE_10_CODE);
1622#endif
1623
1624 return total_written;
1625}
1626
1627/**
1628 * sdio_memcpy_fromio_wrapper -
1629 * reads from sdioc, and updats the sdioc registers according
1630 * to how many bytes were actually read.
1631 *
1632 * @str_func: a pointer to func struct.
1633 * @client_rd_ptr: sdioc value of downlink read ptr.
1634 * @client_wr_ptr: sdioc value of downlink write ptr.
1635 * @buffer_to_store: buffer to store incoming data.
1636 * @address_to_read: address to start reading from in sdioc.
1637 * @size_to_read: size of bytes to read.
1638 * @client_buffer_size: sdioc downlink buffer size.
1639 * @return 0 on success or negative value on error.
1640 */
1641static int sdio_memcpy_fromio_wrapper(struct sdio_func *str_func,
1642 unsigned int client_rd_ptr,
1643 unsigned int client_wr_ptr,
1644 void *buffer_to_store,
1645 unsigned int address_to_read_from,
1646 int size_to_read,
1647 int client_buffer_size)
1648{
1649 int status = 0;
1650 struct sdioc_reg_chunk *reg_str =
1651 &sdio_dld->sdio_dloader_data.sdioc_reg;
1652
1653 if (!str_func) {
1654 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1655 __func__);
1656 return -EINVAL;
1657 }
1658
1659 if (!buffer_to_store) {
1660 pr_err(MODULE_NAME ": %s - param ""buffer_to_store"" is "
1661 "NULL.\n",
1662 __func__);
1663 return -EINVAL;
1664 }
1665
1666 if (size_to_read < 0) {
1667 pr_err(MODULE_NAME ": %s - invalid size to read=%d\n",
1668 __func__, size_to_read);
1669 return -EINVAL;
1670 }
1671
1672 sdio_claim_host(str_func);
1673
1674 pr_debug(MODULE_NAME ": %s, READING DATA - from add %d, "
1675 "size_to_read=%d\n",
1676 __func__, address_to_read_from, size_to_read);
1677
1678 status = sdio_memcpy_fromio(str_func,
1679 (void *)buffer_to_store,
1680 address_to_read_from,
1681 size_to_read);
1682 if (status) {
1683 pr_err(MODULE_NAME ": %s - sdio_memcpy_fromio()"
1684 " DATA failed. status=%d.\n",
1685 __func__, status);
1686 sdio_release_host(str_func);
1687 return status;
1688 }
1689
1690 /* updating an offset according to cyclic buffer size */
1691 reg_str->dl_rd_ptr.reg_val =
1692 (reg_str->dl_rd_ptr.reg_val + size_to_read) %
1693 client_buffer_size;
1694 /* keeping sdio_dld_info up to date */
1695 sdio_dld_info.cl_dl_rd_ptr = reg_str->dl_rd_ptr.reg_val;
1696
1697 status = sdio_memcpy_toio(str_func,
1698 reg_str->dl_rd_ptr.reg_offset,
1699 (void *)&reg_str->dl_rd_ptr.reg_val,
1700 sizeof(reg_str->dl_rd_ptr.reg_val));
1701
1702 if (status) {
1703 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
1704 "UPDATE PTR failed. status=%d.\n",
1705 __func__, status);
1706 }
1707
1708 sdio_release_host(str_func);
1709 return status;
1710}
1711
1712/**
1713 * sdio_memcpy_toio_wrapper
1714 * writes to sdioc, and updats the sdioc registers according
1715 * to how many bytes were actually read.
1716 *
1717 * @str_func: a pointer to func struct.
1718 * @client_wr_ptr: sdioc downlink write ptr.
1719 * @h_read_ptr: host incoming read ptrs
1720 * @buf_write_from: buffer to write from.
1721 * @bytes_to_write: number of bytes to write.
1722 * @return 0 on success or negative value on error.
1723 */
1724static int sdio_memcpy_toio_wrapper(struct sdio_func *str_func,
1725 unsigned int client_wr_ptr,
1726 unsigned int h_read_ptr,
1727 void *buf_write_from,
1728 int bytes_to_write)
1729{
1730 int status = 0;
1731 struct sdioc_reg_chunk *reg_str =
1732 &sdio_dld->sdio_dloader_data.sdioc_reg;
1733 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
1734
1735 if (!str_func) {
1736 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1737 __func__);
1738 return -EINVAL;
1739 }
1740
1741 if (!buf_write_from) {
1742 pr_err(MODULE_NAME ": %s - param ""buf_write_from"" is NULL.\n",
1743 __func__);
1744 return -EINVAL;
1745 }
1746
1747 sdio_claim_host(str_func);
1748
1749 pr_debug(MODULE_NAME ": %s, WRITING DATA TOIO to address 0x%x, "
1750 "bytes_to_write=%d\n",
1751 __func__,
1752 reg_str->up_buff_address.reg_val + reg_str->up_wr_ptr.reg_val,
1753 bytes_to_write);
1754
1755 status = sdio_memcpy_toio(str_func,
1756 reg_str->up_buff_address.reg_val +
1757 reg_str->up_wr_ptr.reg_val,
1758 (void *) (outgoing->data + h_read_ptr),
1759 bytes_to_write);
1760
1761 if (status) {
1762 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
1763 "DATA failed. status=%d.\n", __func__, status);
1764 sdio_release_host(str_func);
1765 return status;
1766 }
1767
1768 sdio_dld_info.global_bytes_write_toio += bytes_to_write;
1769 outgoing->num_of_bytes_in_use -= bytes_to_write;
1770
1771 /*
1772 * if writing to client succeeded, then
1773 * 1. update the client up_wr_ptr
1774 * 2. update the host outgoing rd ptr
1775 **/
1776 reg_str->up_wr_ptr.reg_val =
1777 ((reg_str->up_wr_ptr.reg_val + bytes_to_write) %
1778 reg_str->ul_buff_size.reg_val);
1779
1780 /* keeping sdio_dld_info up to date */
1781 sdio_dld_info.cl_up_wr_ptr = reg_str->up_wr_ptr.reg_val;
1782
1783 outgoing->offset_read_p =
1784 ((outgoing->offset_read_p + bytes_to_write) %
1785 outgoing->buffer_size);
1786
1787 /* keeping sdio_dld_info up to date*/
1788 sdio_dld_info.host_read_ptr = outgoing->offset_read_p;
1789
1790#ifdef CONFIG_DEBUG_FS
1791 debugfs_glob.global_write_toio = bytes_to_write;
1792 update_gd(SDIO_DLD_DEBUGFS_CASE_4_CODE);
1793#endif
1794
1795 /* updating uplink write pointer according to size that was written */
1796 status = sdio_memcpy_toio(str_func,
1797 reg_str->up_wr_ptr.reg_offset,
1798 (void *)(&reg_str->up_wr_ptr.reg_val),
1799 sizeof(reg_str->up_wr_ptr.reg_val));
1800 if (status) {
1801 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
1802 "UPDATE PTR failed. status=%d.\n",
1803 __func__, status);
1804 }
1805
1806 sdio_release_host(str_func);
1807 return status;
1808}
1809
1810/**
1811 * sdio_dld_read
1812 * reads from sdioc
1813 *
1814 * @client_rd_ptr: sdioc downlink read ptr.
1815 * @client_wr_ptr: sdioc downlink write ptr.
1816 * @reg_str: sdioc register shadowing struct.
1817 * @str_func: a pointer to func struct.
1818 * @bytes_read:how many bytes read.
1819 * @return 0 on success or negative value on error.
1820 */
1821static int sdio_dld_read(unsigned int client_rd_ptr,
1822 unsigned int client_wr_ptr,
1823 struct sdioc_reg_chunk *reg_str,
1824 struct sdio_func *str_func,
1825 int *bytes_read)
1826{
1827 int status = 0;
1828 struct sdio_data *incoming = &sdio_dld->sdio_dloader_data.incoming_data;
1829
1830 if (!reg_str) {
1831 pr_err(MODULE_NAME ": %s - param ""reg_str"" is NULL.\n",
1832 __func__);
1833 return -EINVAL;
1834 }
1835
1836 if (!str_func) {
1837 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1838 __func__);
1839 return -EINVAL;
1840 }
1841
1842 if (!bytes_read) {
1843 pr_err(MODULE_NAME ": %s - param ""bytes_read"" is NULL.\n",
1844 __func__);
1845 return -EINVAL;
1846 }
1847
1848 /* there is data to read in ONE chunk */
1849 if (client_wr_ptr > client_rd_ptr) {
1850 status = sdio_memcpy_fromio_wrapper(
1851 str_func,
1852 client_rd_ptr,
1853 client_wr_ptr,
1854 (void *)incoming->data,
1855 reg_str->dl_buff_address.reg_val + client_rd_ptr,
1856 client_wr_ptr - client_rd_ptr,
1857 reg_str->dl_buff_size.reg_val);
1858
1859 if (status) {
1860 pr_err(MODULE_NAME ": %s - Failure in Function "
1861 "sdio_memcpy_fromio_wrapper(). "
1862 "SINGLE CHUNK READ. status=%d\n",
1863 __func__, status);
1864 return status;
1865 }
1866
1867 incoming->num_of_bytes_in_use += client_wr_ptr - client_rd_ptr;
1868 *bytes_read = client_wr_ptr - client_rd_ptr;
1869
1870#ifdef CONFIG_DEBUG_FS
1871 debugfs_glob.global_to_read =
1872 client_wr_ptr - client_rd_ptr;
1873 update_gd(SDIO_DLD_DEBUGFS_CASE_11_CODE);
1874#endif
1875 }
1876
1877 /* there is data to read in TWO chunks */
1878 else {
1879 int dl_buf_size = reg_str->dl_buff_size.reg_val;
1880 int tail_size = dl_buf_size - client_rd_ptr;
1881
1882 /* reading chunk#1: from rd_ptr to the end of the buffer */
1883 status = sdio_memcpy_fromio_wrapper(
1884 str_func,
1885 client_rd_ptr,
1886 dl_buf_size,
1887 (void *)incoming->data,
1888 reg_str->dl_buff_address.reg_val + client_rd_ptr,
1889 tail_size,
1890 dl_buf_size);
1891
1892 if (status) {
1893 pr_err(MODULE_NAME ": %s - Failure in Function "
1894 "sdio_memcpy_fromio_wrapper(). "
1895 "1 of 2 CHUNKS READ. status=%d\n",
1896 __func__, status);
1897 return status;
1898 }
1899
1900 incoming->num_of_bytes_in_use += tail_size;
1901 *bytes_read = tail_size;
1902
1903#ifdef CONFIG_DEBUG_FS
1904 debugfs_glob.global_to_read = tail_size;
1905 update_gd(SDIO_DLD_DEBUGFS_CASE_11_CODE);
1906#endif
1907
1908 /* reading chunk#2: reading from beginning buffer */
1909 status = sdio_memcpy_fromio_wrapper(
1910 str_func,
1911 client_rd_ptr,
1912 client_wr_ptr,
1913 (void *)(incoming->data + tail_size),
1914 reg_str->dl_buff_address.reg_val,
1915 client_wr_ptr,
1916 reg_str->dl_buff_size.reg_val);
1917
1918 if (status) {
1919 pr_err(MODULE_NAME ": %s - Failure in Function "
1920 "sdio_memcpy_fromio_wrapper(). "
1921 "2 of 2 CHUNKS READ. status=%d\n",
1922 __func__, status);
1923 return status;
1924 }
1925
1926 incoming->num_of_bytes_in_use += client_wr_ptr;
1927 *bytes_read += client_wr_ptr;
1928
1929#ifdef CONFIG_DEBUG_FS
1930 debugfs_glob.global_to_read = client_wr_ptr;
1931 update_gd(SDIO_DLD_DEBUGFS_CASE_11_CODE);
1932#endif
1933 }
1934 return 0;
1935}
1936
1937/**
1938 * sdio_dld_main_task
1939 * sdio downloader main task. reads mailboxf checks if there is
1940 * anything to read, checks if host has anything to
1941 * write.
1942 *
1943 * @card: a pointer to mmc_card.
1944 * @return 0 on success or negative value on error.
1945 */
1946static int sdio_dld_main_task(void *card)
1947{
1948 int status = 0;
1949 struct tty_struct *tty = sdio_dld->tty_str;
1950 struct sdioc_reg_chunk *reg_str =
1951 &sdio_dld->sdio_dloader_data.sdioc_reg;
1952 int func = sdio_dld->sdioc_boot_func;
1953 struct sdio_func *str_func = NULL;
1954 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
1955 struct sdio_data *incoming = &sdio_dld->sdio_dloader_data.incoming_data;
1956 struct sdio_dld_task *task = &sdio_dld->dld_main_thread;
1957 int retries = 0;
1958#ifdef PUSH_STRING
1959 int bytes_pushed = 0;
1960#endif
1961
1962 msleep(SLEEP_MS);
1963
1964 if (!card) {
1965 pr_err(MODULE_NAME ": %s - param ""card"" is NULL.\n",
1966 __func__);
1967 return -EINVAL;
1968 }
1969
1970 if (!tty) {
1971 pr_err(MODULE_NAME ": %s - param ""tty"" is NULL.\n",
1972 __func__);
1973 return -EINVAL;
1974 }
1975
1976 str_func = ((struct mmc_card *)card)->
1977 sdio_func[REAL_FUNC_TO_FUNC_IN_ARRAY(func)];
1978
1979 if (!str_func) {
1980 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1981 __func__);
1982 return -EINVAL;
1983 }
1984
1985 while (true) {
1986 /* client pointers for both buffers */
1987 int client_ul_wr_ptr = 0;
1988 int client_ul_rd_ptr = 0;
1989 int client_dl_wr_ptr = 0;
1990 int client_dl_rd_ptr = 0;
1991
1992 /* host pointer for outgoing buffer */
1993 int h_out_wr_ptr = 0;
1994 int h_out_rd_ptr = 0;
1995
1996 int h_bytes_rdy_wr = 0;
1997 int c_bytes_rdy_rcve = 0;
1998
1999 int need_to_write = 0;
2000 int need_to_read = 0;
2001
2002 /*
2003 * forever, checking for signal to die, then read MailBox.
2004 * if nothing to read or nothing to write to client, sleep,
2005 * and again read MailBox
2006 */
2007 do {
2008 int dummy = 0;
2009
2010 /* checking if a signal to die was sent */
2011 if (atomic_read(&task->please_close) == 1) {
2012
2013 pr_debug(MODULE_NAME ": %s - 0x%x was written "
2014 "to 9K\n", __func__, SDIOC_EXIT_CODE);
2015
2016 sdio_claim_host(str_func);
2017
2018 /* returned value is not checked on purpose */
2019 sdio_memcpy_toio(
2020 str_func,
2021 reg_str->good_to_exit_ptr.reg_offset,
2022 (void *)&reg_str->good_to_exit_ptr.
2023 reg_val,
2024 sizeof(reg_str->good_to_exit_ptr.
2025 reg_val));
2026
2027 sdio_release_host(str_func);
2028
2029 task->exit_wait.wake_up_signal = 1;
2030 wake_up(&task->exit_wait.wait_event);
2031 return 0;
2032 }
2033
2034 status = mailbox_to_seq_chunk_read_ptrs(str_func);
2035 if (status) {
2036 pr_err(MODULE_NAME ": %s - Failure in Function "
2037 "mailbox_to_seq_chunk_read_ptrs(). "
2038 "status=%d\n", __func__, status);
2039 return status;
2040 }
2041
2042 /* calculate how many bytes the host has send */
2043 h_out_wr_ptr = outgoing->offset_write_p;
2044 h_out_rd_ptr = outgoing->offset_read_p;
2045
2046 status = sdioc_bytes_till_end_of_buffer(
2047 h_out_wr_ptr,
2048 h_out_rd_ptr,
2049 outgoing->buffer_size,
2050 &dummy,
2051 &h_bytes_rdy_wr);
2052
2053 if (status) {
2054 pr_err(MODULE_NAME ": %s - Failure in Function "
2055 "sdioc_bytes_till_end_of_buffer(). "
2056 "status=%d\n", __func__, status);
2057 return status;
2058 }
2059
2060 /* is there something to read from client */
2061 client_dl_wr_ptr = reg_str->dl_wr_ptr.reg_val;
2062 client_dl_rd_ptr = reg_str->dl_rd_ptr.reg_val;
2063
2064 if (client_dl_rd_ptr != client_dl_wr_ptr)
2065 need_to_read = 1;
2066
2067 /*
2068 * calculate how many bytes the client can receive
2069 * from host
2070 */
2071 client_ul_wr_ptr = reg_str->up_wr_ptr.reg_val;
2072 client_ul_rd_ptr = reg_str->up_rd_ptr.reg_val;
2073
2074 status = sdioc_bytes_till_end_of_buffer(
2075 client_ul_wr_ptr,
2076 client_ul_rd_ptr,
2077 reg_str->ul_buff_size.reg_val,
2078 &c_bytes_rdy_rcve,
2079 &dummy);
2080
2081 if (status) {
2082 pr_err(MODULE_NAME ": %s - Failure in Function "
2083 "sdioc_bytes_till_end_of_buffer(). "
2084 "status=%d\n", __func__, status);
2085 return status;
2086 }
2087
2088 /* if host has anything to write */
2089 if (h_bytes_rdy_wr > 0)
2090 need_to_write = 1;
2091
2092 if (need_to_write || need_to_read)
2093 break;
2094
2095 spin_lock_irqsave(&lock2, lock_flags2);
2096 sdio_dld->main_loop_event.wake_up_signal = 0;
2097 spin_unlock_irqrestore(&lock2, lock_flags2);
2098
2099 pr_debug(MODULE_NAME ": %s - MAIN LOOP - WAITING...\n",
2100 __func__);
2101#ifdef CONFIG_DEBUG_FS
2102 update_gd(SDIO_DLD_DEBUGFS_CASE_6_CODE);
2103#endif
2104 wait_event(sdio_dld->main_loop_event.wait_event,
2105 sdio_dld->main_loop_event.wake_up_signal);
2106#ifdef CONFIG_DEBUG_FS
2107 update_gd(SDIO_DLD_DEBUGFS_CASE_7_CODE);
2108#endif
2109
2110 pr_debug(MODULE_NAME ": %s - MAIN LOOP - WOKE UP...\n",
2111 __func__);
2112
2113 } while (1);
2114
2115 /* CHECK IF THERE IS ANYTHING TO READ IN CLIENT */
2116 if (need_to_read) {
2117#ifdef PUSH_STRING
2118 int num_push = 0;
2119 int left = 0;
2120 int bytes_read;
2121#else
2122 int i;
2123#endif
2124 need_to_read = 0;
2125
2126 status = sdio_dld_read(client_dl_rd_ptr,
2127 client_dl_wr_ptr,
2128 reg_str,
2129 str_func,
2130 &bytes_read);
2131
2132 if (status) {
2133 pr_err(MODULE_NAME ": %s - Failure in Function "
2134 "sdio_dld_read(). status=%d\n",
2135 __func__, status);
2136 return status;
2137 }
2138
2139 sdio_dld_info.global_bytes_read_fromio +=
2140 bytes_read;
2141
2142 bytes_pushed = 0;
2143#ifdef PUSH_STRING
2144 left = incoming->num_of_bytes_in_use;
2145 start_timer(&sdio_dld->push_timer,
2146 sdio_dld->push_timer_ms);
2147 do {
2148 num_push = tty_insert_flip_string(
2149 tty,
2150 incoming->data+bytes_pushed,
2151 left);
2152
2153 bytes_pushed += num_push;
2154 left -= num_push;
2155 tty_flip_buffer_push(tty);
2156 } while (left != 0);
2157
2158 del_timer(&sdio_dld->push_timer);
2159
2160 if (bytes_pushed != incoming->num_of_bytes_in_use) {
2161 pr_err(MODULE_NAME ": %s - failed\n",
2162 __func__);
2163 }
2164#else
2165 pr_debug(MODULE_NAME ": %s - NEED TO READ %d\n",
2166 __func__, incoming->num_of_bytes_in_use);
2167
2168 for (i = 0 ; i < incoming->num_of_bytes_in_use ; ++i) {
2169 int err = 0;
2170 err = tty_insert_flip_char(tty,
2171 incoming->data[i],
2172 TTY_NORMAL);
2173 tty_flip_buffer_push(tty);
2174 }
2175
2176 pr_debug(MODULE_NAME ": %s - JUST READ\n", __func__);
2177#endif /*PUSH_STRING*/
2178 sdio_dld_info.global_bytes_push_tty +=
2179 incoming->num_of_bytes_in_use;
2180#ifdef CONFIG_DEBUG_FS
2181 debugfs_glob.global_push_to_tty = bytes_read;
2182 update_gd(SDIO_DLD_DEBUGFS_CASE_12_CODE);
2183#endif
2184 incoming->num_of_bytes_in_use = 0;
2185 tty_flip_buffer_push(tty);
2186 }
2187
2188 /* CHECK IF THERE IS ANYTHING TO WRITE IN HOST AND HOW MUCH */
2189 if (need_to_write) {
2190 int dummy = 0;
2191
2192 do {
2193 int bytes_to_write = min(c_bytes_rdy_rcve,
2194 h_bytes_rdy_wr);
2195
2196 /*
2197 * in case nothing to send or no room to
2198 * receive
2199 */
2200 if (bytes_to_write == 0)
2201 break;
2202
2203 if (client_ul_rd_ptr == 0 &&
2204 (client_ul_rd_ptr != client_ul_wr_ptr))
2205 break;
2206
2207 /*
2208 * if client_rd_ptr points to start, but there
2209 * is data to read wait until WRITE_TILL_END
2210 * before writing a chunk of data, to avoid
2211 * writing until (BUF_SIZE - 1), because it will
2212 * yield an extra write of "1" bytes
2213 */
2214 if (client_ul_rd_ptr == 0 &&
2215 (client_ul_rd_ptr != client_ul_wr_ptr) &&
2216 retries < WRITE_TILL_END_RETRIES) {
2217 retries++;
2218 break;
2219 }
2220 retries = 0;
2221
2222#ifdef CONFIG_DEBUG_FS
2223 debugfs_glob.global_8k_has = h_bytes_rdy_wr;
2224 debugfs_glob.global_9k_has = c_bytes_rdy_rcve;
2225 debugfs_glob.global_min = bytes_to_write;
2226 update_gd(SDIO_DLD_DEBUGFS_CASE_2_CODE);
2227#endif
2228 need_to_write = 0;
2229
2230 pr_debug(MODULE_NAME ": %s - NEED TO WRITE "
2231 "TOIO %d\n",
2232 __func__, bytes_to_write);
2233
2234 status = sdio_memcpy_toio_wrapper(
2235 str_func,
2236 reg_str->up_wr_ptr.reg_val,
2237 outgoing->offset_read_p,
2238 (void *)((char *)outgoing->data +
2239 outgoing->offset_read_p),
2240 bytes_to_write);
2241
2242 if (status) {
2243 pr_err(MODULE_NAME ": %s - Failure in "
2244 "Function "
2245 "sdio_memcpy_toio_wrapper(). "
2246 "SINGLE CHUNK WRITE. "
2247 "status=%d\n",
2248 __func__, status);
2249 return status;
2250 }
2251
2252 sdio_claim_host(str_func);
2253
2254 status = sdio_memcpy_fromio(
2255 str_func,
2256 (void *)&reg_str->up_rd_ptr.reg_val,
2257 SDIOC_UL_RD_PTR,
2258 sizeof(reg_str->up_rd_ptr.reg_val));
2259
2260 if (status) {
2261 pr_err(MODULE_NAME ": %s - "
2262 "sdio_memcpy_fromio() "
2263 "failed. status=%d\n",
2264 __func__, status);
2265 sdio_release_host(str_func);
2266
2267 return status;
2268 }
2269
2270 sdio_release_host(str_func);
2271
2272 spin_lock_irqsave(&lock1, lock_flags1);
2273 if (sdio_dld->write_callback_event.
2274 wake_up_signal == 0) {
2275 sdio_dld->write_callback_event.
2276 wake_up_signal = 1;
2277 wake_up(&sdio_dld->
2278 write_callback_event.
2279 wait_event);
2280 }
2281
2282 spin_unlock_irqrestore(&lock1, lock_flags1);
2283 client_ul_wr_ptr = reg_str->up_wr_ptr.reg_val;
2284 client_ul_rd_ptr = reg_str->up_rd_ptr.reg_val;
2285
2286 status = sdioc_bytes_till_end_of_buffer(
2287 client_ul_wr_ptr,
2288 client_ul_rd_ptr,
2289 reg_str->ul_buff_size.reg_val,
2290 &c_bytes_rdy_rcve,
2291 &dummy);
2292
2293 /* calculate how many bytes host has to send */
2294 h_out_wr_ptr = outgoing->offset_write_p;
2295 h_out_rd_ptr = outgoing->offset_read_p;
2296
2297 status = sdioc_bytes_till_end_of_buffer(
2298 h_out_wr_ptr,
2299 h_out_rd_ptr,
2300 outgoing->buffer_size,
2301 &dummy,
2302 &h_bytes_rdy_wr);
2303
2304 } while (h_out_wr_ptr != h_out_rd_ptr);
2305 }
2306 }
2307 return 0;
2308}
2309
2310/**
2311 * sdio_dld_init_global
2312 * initialization of sdio_dld global struct
2313 *
2314 * @card: a pointer to mmc_card.
2315 * @return 0 on success or negative value on error.
2316 */
2317static int sdio_dld_init_global(struct mmc_card *card,
2318 int(*done)(void))
2319{
2320 if (!card) {
2321 pr_err(MODULE_NAME ": %s - param ""card"" is NULL.\n",
2322 __func__);
2323 return -EINVAL;
2324 }
2325
2326 if (!done) {
2327 pr_err(MODULE_NAME ": %s - param ""done"" is NULL.\n",
2328 __func__);
2329 return -EINVAL;
2330 }
2331
2332 sdio_dld->done_callback = done;
2333 sdio_dld->card = card;
2334 init_waitqueue_head(&sdio_dld->dld_main_thread.exit_wait.wait_event);
2335 sdio_dld->write_callback_event.wake_up_signal = 1;
2336 sdio_dld->main_loop_event.wake_up_signal = 1;
2337
2338 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_size.reg_offset =
2339 SDIOC_DL_BUFF_SIZE_OFFSET;
2340
2341 sdio_dld->sdio_dloader_data.sdioc_reg.dl_rd_ptr.reg_offset =
2342 SDIOC_DL_RD_PTR;
2343
2344 sdio_dld->sdio_dloader_data.sdioc_reg.dl_wr_ptr.reg_offset =
2345 SDIOC_DL_WR_PTR;
2346
2347 sdio_dld->sdio_dloader_data.sdioc_reg.ul_buff_size.reg_offset =
2348 SDIOC_UP_BUFF_SIZE_OFFSET;
2349
2350 sdio_dld->sdio_dloader_data.sdioc_reg.up_rd_ptr.reg_offset =
2351 SDIOC_UL_RD_PTR;
2352
2353 sdio_dld->sdio_dloader_data.sdioc_reg.up_wr_ptr.reg_offset =
2354 SDIOC_UL_WR_PTR;
2355
2356 sdio_dld->sdio_dloader_data.sdioc_reg.good_to_exit_ptr.reg_offset =
2357 SDIOC_EXIT_PTR;
2358
2359 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_address.reg_offset =
2360 SDIOC_DL_BUFF_ADDRESS;
2361
2362 sdio_dld->sdio_dloader_data.sdioc_reg.up_buff_address.reg_offset =
2363 SDIOC_UP_BUFF_ADDRESS;
2364
2365 sdio_dld_set_op_mode(SDIO_DLD_NORMAL_MODE);
2366
2367 return 0;
2368}
2369
2370/**
2371 * sdio_downloader_setup
2372 * initializes the TTY driver
2373 *
2374 * @card: a pointer to mmc_card.
2375 * @num_of_devices: number of devices.
2376 * @channel_number: channel number.
2377 * @return 0 on success or negative value on error.
2378 *
2379 * The TTY stack needs to know in advance how many devices it should
2380 * plan to manage. Use this call to set up the ports that will
2381 * be exported through SDIO.
2382 */
2383int sdio_downloader_setup(struct mmc_card *card,
2384 unsigned int num_of_devices,
2385 int channel_number,
2386 int(*done)(void))
2387{
2388 int status = 0;
2389 int result = 0;
2390 int func_in_array = 0;
2391 struct sdio_func *str_func = NULL;
2392 struct device *tty_dev;
2393
2394 if (num_of_devices == 0 || num_of_devices > MAX_NUM_DEVICES) {
2395 pr_err(MODULE_NAME ": %s - invalid number of devices\n",
2396 __func__);
2397 return -EINVAL;
2398 }
2399
2400 if (!card) {
2401 pr_err(MODULE_NAME ": %s - param ""card"" is NULL.\n",
2402 __func__);
2403 return -EINVAL;
2404 }
2405
2406 if (!done) {
2407 pr_err(MODULE_NAME ": %s - param ""done"" is NULL.\n",
2408 __func__);
2409 return -EINVAL;
2410 }
2411
2412 sdio_dld = kzalloc(sizeof(struct sdio_downloader), GFP_KERNEL);
2413 if (!sdio_dld) {
2414 pr_err(MODULE_NAME ": %s - couldn't allocate sdio_dld data "
2415 "structure.", __func__);
2416 return -ENOMEM;
2417 }
2418
2419#ifdef CONFIG_DEBUG_FS
2420 bootloader_debugfs_init();
2421#endif /* CONFIG_DEBUG_FS */
2422
2423 status = sdio_dld_init_global(card, done);
2424
2425 if (status) {
2426 pr_err(MODULE_NAME ": %s - Failure in Function "
2427 "sdio_dld_init_global(). status=%d\n",
2428 __func__, status);
2429 kfree(sdio_dld);
2430 return status;
2431 }
2432
2433 sdio_dld->tty_drv = alloc_tty_driver(num_of_devices);
2434
2435 if (!sdio_dld->tty_drv) {
2436 pr_err(MODULE_NAME ": %s - param ""sdio_dld->tty_drv"" is "
2437 "NULL.\n", __func__);
2438 kfree(sdio_dld);
2439 return -EINVAL;
2440 }
2441
2442 sdio_dld_set_op_mode((enum sdio_dld_op_mode)sdio_op_mode);
2443
2444 /* according to op_mode, a different tty device is created */
2445 if (sdio_dld->op_mode == SDIO_DLD_BOOT_TEST_MODE)
2446 sdio_dld->tty_drv->name = TTY_SDIO_DEV_TEST;
2447 else
2448 sdio_dld->tty_drv->name = TTY_SDIO_DEV;
2449
2450 sdio_dld->tty_drv->owner = THIS_MODULE;
2451 sdio_dld->tty_drv->driver_name = "SDIO_Dloader";
2452
2453 /* uses dynamically assigned dev_t values */
2454 sdio_dld->tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2455 sdio_dld->tty_drv->subtype = SERIAL_TYPE_NORMAL;
2456 sdio_dld->tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
2457 | TTY_DRIVER_RESET_TERMIOS;
2458
2459 /* initializing the tty driver */
2460 sdio_dld->tty_drv->init_termios = tty_std_termios;
2461 sdio_dld->tty_drv->init_termios.c_cflag =
2462 B4800 | CS8 | CREAD | HUPCL | CLOCAL;
2463 sdio_dld->tty_drv->init_termios.c_ispeed = INPUT_SPEED;
2464 sdio_dld->tty_drv->init_termios.c_ospeed = OUTPUT_SPEED;
2465
2466 tty_set_operations(sdio_dld->tty_drv, &sdio_dloader_tty_ops);
2467
2468 status = tty_register_driver(sdio_dld->tty_drv);
2469 if (status) {
2470 put_tty_driver(sdio_dld->tty_drv);
2471 pr_err(MODULE_NAME ": %s - tty_register_driver() failed\n",
2472 __func__);
2473
2474 sdio_dld->tty_drv = NULL;
2475 kfree(sdio_dld);
2476 return status;
2477 }
2478
2479 tty_dev = tty_register_device(sdio_dld->tty_drv, 0, NULL);
2480 if (IS_ERR(tty_dev)) {
2481 pr_err(MODULE_NAME ": %s - tty_register_device() "
2482 "failed\n", __func__);
2483 tty_unregister_driver(sdio_dld->tty_drv);
2484 kfree(sdio_dld);
2485 return PTR_ERR(tty_dev);
2486 }
2487
2488 sdio_dld->sdioc_boot_func = SDIOC_CHAN_TO_FUNC_NUM(channel_number);
2489 func_in_array = REAL_FUNC_TO_FUNC_IN_ARRAY(sdio_dld->sdioc_boot_func);
2490 str_func = sdio_dld->card->sdio_func[func_in_array];
2491 status = sdio_dld_init_func(str_func);
2492 if (status) {
2493 pr_err(MODULE_NAME ": %s - Failure in Function "
2494 "sdio_dld_init_func(). status=%d\n",
2495 __func__, status);
2496 goto exit_err;
2497 }
2498
2499#ifdef CONFIG_DEBUG_FS
2500 sdio_dld_debug_init();
2501#endif
2502
2503 sdio_claim_host(str_func);
2504
2505 /*
2506 * notifing the client by writing what mode we are by writing
2507 * to a special register
2508 */
2509 status = sdio_memcpy_toio(str_func,
2510 SDIOC_OP_MODE_PTR,
2511 (void *)&sdio_dld->op_mode,
2512 sizeof(sdio_dld->op_mode));
2513
2514 sdio_release_host(str_func);
2515
2516 if (status) {
2517 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
2518 "writing to OP_MODE_REGISTER failed. "
2519 "status=%d.\n",
2520 __func__, status);
2521 goto exit_err;
2522 }
2523
2524 return 0;
2525
2526exit_err:
2527 tty_unregister_device(sdio_dld->tty_drv, 0);
2528 result = tty_unregister_driver(sdio_dld->tty_drv);
2529 if (result)
2530 pr_err(MODULE_NAME ": %s - tty_unregister_driver() "
2531 "failed. result=%d\n", __func__, -result);
2532 kfree(sdio_dld);
2533 return status;
2534}
2535
2536MODULE_LICENSE("GPL v2");
2537MODULE_DESCRIPTION("SDIO Downloader");
2538MODULE_AUTHOR("Yaniv Gardi <ygardi@codeaurora.org>");
2539MODULE_VERSION(DRV_VERSION);
2540