blob: f48c32b8f4bd24d1fa5aef690b31557ec62e34db [file] [log] [blame]
Stephen Boydb5038712012-04-25 11:40:56 -07001/* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
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>
Alhad Purnapatred0d12e72011-10-26 14:04:42 -070028#include "sdio_al_private.h"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070029#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
Stephen Boydb5038712012-04-25 11:40:56 -0700533static int bootloader_debugfs_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534{
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
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001143 /* init waiting event of the write callback */
1144 init_waitqueue_head(&sdio_dld->write_callback_event.wait_event);
1145
1146 /* init waiting event of the main loop */
1147 init_waitqueue_head(&sdio_dld->main_loop_event.wait_event);
1148
1149 /* configure and init the timer */
1150 sdio_dld->poll_ms = TIMER_DURATION;
1151 init_timer(&sdio_dld->timer);
1152 sdio_dld->timer.data = (unsigned long) sdio_dld;
1153 sdio_dld->timer.function = sdio_dld_timer_handler;
1154 sdio_dld->timer.expires = jiffies +
1155 msecs_to_jiffies(sdio_dld->poll_ms);
1156 add_timer(&sdio_dld->timer);
1157
1158 sdio_dld->push_timer_ms = PUSH_TIMER_DURATION;
1159 init_timer(&sdio_dld->push_timer);
1160 sdio_dld->push_timer.data = (unsigned long) sdio_dld;
1161 sdio_dld->push_timer.function = sdio_dld_push_timer_handler;
1162
Venkat Gopalakrishnan6f407252012-05-16 19:54:09 -07001163 status = sdio_dld_create_thread();
1164 if (status) {
1165 del_timer_sync(&sdio_dld->timer);
1166 del_timer_sync(&sdio_dld->push_timer);
1167 sdio_dld_dealloc_local_buffers();
1168 pr_err(MODULE_NAME ": %s, failed in sdio_dld_create_thread()."
1169 "status=%d\n", __func__, status);
1170 return status;
1171 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001172 return 0;
1173}
1174
1175/**
1176 * sdio_dld_close
1177 * this is the close callback of the tty driver. it requests
1178 * the main thread to exit, and waits for notification of it.
1179 * it also de-allocates the buffers, and unregisters the tty
1180 * driver and device.
1181 *
1182 * @tty: a pointer to the tty struct.
1183 * @file: file descriptor.
1184 * @return None.
1185 */
1186static void sdio_dld_close(struct tty_struct *tty, struct file *file)
1187{
1188 int status = 0;
1189 struct sdioc_reg_chunk *reg = &sdio_dld->sdio_dloader_data.sdioc_reg;
1190
1191 /* informing the SDIOC that it can exit boot phase */
1192 sdio_dld->sdio_dloader_data.sdioc_reg.good_to_exit_ptr.reg_val =
1193 SDIOC_EXIT_CODE;
1194
1195 atomic_set(&sdio_dld->dld_main_thread.please_close, 1);
1196
1197 pr_debug(MODULE_NAME ": %s - CLOSING - WAITING...", __func__);
1198
1199 wait_event(sdio_dld->dld_main_thread.exit_wait.wait_event,
1200 sdio_dld->dld_main_thread.exit_wait.wake_up_signal);
1201 pr_debug(MODULE_NAME ": %s - CLOSING - WOKE UP...", __func__);
1202
1203 del_timer_sync(&sdio_dld->timer);
1204 del_timer_sync(&sdio_dld->push_timer);
1205
1206 sdio_dld_dealloc_local_buffers();
1207
1208 tty_unregister_device(sdio_dld->tty_drv, 0);
1209
1210 status = tty_unregister_driver(sdio_dld->tty_drv);
1211
1212 if (status) {
1213 pr_err(MODULE_NAME ": %s - tty_unregister_driver() failed\n",
1214 __func__);
1215 }
1216
1217#ifdef CONFIG_DEBUG_FS
1218 gd.curr_i = curr_index;
1219 gd.duration_ms = sdio_dld_info.time_msec;
1220 gd.global_bytes_sent = sdio_dld_info.global_bytes_write_toio;
1221 gd.global_bytes_received = 0;
1222 gd.throughput_Mbs = sdio_dld_info.throughput;
1223 gd.host_outgoing_buffer_size_KB = sdio_dld->sdio_dloader_data.
1224 outgoing_data.buffer_size/BYTES_IN_KB;
1225 gd.client_up_buffer_size_KB = reg->ul_buff_size.reg_val/BYTES_IN_KB;
1226 gd.client_dl_buffer_size_KB = reg->dl_buff_size.reg_val/BYTES_IN_KB;
1227 gd.client_dl_buffer_address = reg->dl_buff_address.reg_val;
1228 gd.client_up_buffer_address = reg->up_buff_address.reg_val;
1229 gd.global_bytes_received = sdio_dld_info.global_bytes_read_fromio;
1230 gd.global_bytes_pushed = sdio_dld_info.global_bytes_push_tty;
1231#endif
1232
1233 /* saving register values before deallocating sdio_dld
1234 in order to use it in sdio_dld_print_info() through shell command */
1235 sdio_dld_info.cl_dl_rd_ptr = reg->dl_rd_ptr.reg_val;
1236 sdio_dld_info.cl_dl_wr_ptr = reg->dl_wr_ptr.reg_val;
1237 sdio_dld_info.cl_up_rd_ptr = reg->up_rd_ptr.reg_val;
1238 sdio_dld_info.cl_up_wr_ptr = reg->up_wr_ptr.reg_val;
1239
1240 sdio_dld_info.host_read_ptr =
1241 sdio_dld->sdio_dloader_data.outgoing_data.offset_read_p;
1242
1243 sdio_dld_info.host_write_ptr =
1244 sdio_dld->sdio_dloader_data.outgoing_data.offset_write_p;
1245
1246 sdio_dld_info.cl_dl_buffer_size =
1247 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_size.reg_val;
1248
1249 sdio_dld_info.cl_up_buffer_size =
1250 sdio_dld->sdio_dloader_data.sdioc_reg.ul_buff_size.reg_val;
1251
1252 sdio_dld_info.host_outgoing_buffer_size =
1253 sdio_dld->sdio_dloader_data.outgoing_data.buffer_size;
1254
1255 sdio_dld_info.cl_dl_buffer_address =
1256 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_address.reg_val;
1257
1258 sdio_dld_info.cl_up_buffer_address =
1259 sdio_dld->sdio_dloader_data.sdioc_reg.up_buff_address.reg_val;
1260
1261 sdio_dld_print_info();
1262
1263 if (sdio_dld->done_callback)
1264 sdio_dld->done_callback();
1265
1266 pr_info(MODULE_NAME ": %s - Freeing sdio_dld data structure, and "
1267 " returning...", __func__);
1268 kfree(sdio_dld);
1269}
1270
1271/**
1272 * writing_size_to_buf
1273 * writes from src buffer into dest buffer. if dest buffer
1274 * reaches its end, rollover happens.
1275 *
1276 * @dest: destination buffer.
1277 * @src: source buffer.
1278 * @dest_wr_ptr: writing pointer in destination buffer.
1279 * @dest_size: destination buffer size.
1280 * @dest_rd_ptr: reading pointer in destination buffer.
1281 * @size_to_write: size of bytes to write.
1282 * @return -how many bytes actually written to destination
1283 * buffer.
1284 *
1285 * ONLY destination buffer is treated as cyclic buffer.
1286 */
1287static int writing_size_to_buf(char *dest,
1288 const unsigned char *src,
1289 int *dest_wr_ptr,
1290 int dest_size,
1291 int dest_rd_ptr,
1292 int size_to_write)
1293{
1294 int actually_written = 0;
1295 int size_to_add = *dest_wr_ptr;
1296
1297 if (!dest) {
1298 pr_err(MODULE_NAME ": %s - param ""dest"" is NULL.\n",
1299 __func__);
1300 return -EINVAL;
1301 }
1302
1303 if (!src) {
1304 pr_err(MODULE_NAME ": %s - param ""src"" is NULL.\n",
1305 __func__);
1306 return -EINVAL;
1307 }
1308
1309 if (!dest_wr_ptr) {
1310 pr_err(MODULE_NAME ": %s - param ""dest_wr_ptr"" is NULL.\n",
1311 __func__);
1312 return -EINVAL;
1313 }
1314
1315 for (actually_written = 0 ;
1316 actually_written < size_to_write ; ++actually_written) {
1317 /* checking if buffer is full */
1318 if (((size_to_add + 1) % dest_size) == dest_rd_ptr) {
1319 *dest_wr_ptr = size_to_add;
1320 return actually_written;
1321 }
1322
1323 dest[size_to_add] = src[actually_written];
1324 size_to_add = (size_to_add+1)%dest_size;
1325 }
1326
1327 *dest_wr_ptr = size_to_add;
1328
1329 return actually_written;
1330}
1331
1332/**
1333 * sdioc_bytes_till_end_of_buffer - this routine calculates how many bytes are
1334 * empty/in use. if calculation requires rap around - it will ignore the rap
1335 * around and will do the calculation untill the end of the buffer
1336 *
1337 * @write_ptr: writing pointer.
1338 * @read_ptr: reading pointer.
1339 * @total_size: buffer size.
1340 * @free_bytes: return value-how many free bytes.
1341 * @bytes_in_use: return value-how many bytes in use.
1342 * @return 0 on success or negative value on error.
1343 *
1344 * buffer is treated as a cyclic buffer.
1345 */
1346static int sdioc_bytes_till_end_of_buffer(int write_ptr,
1347 int read_ptr,
1348 int total_size,
1349 int *free_bytes,
1350 int *bytes_in_use)
1351{
1352 if (!free_bytes) {
1353 pr_err(MODULE_NAME ": %s - param ""free_bytes"" is NULL.\n",
1354 __func__);
1355 return -EINVAL;
1356 }
1357
1358 if (!bytes_in_use) {
1359 pr_err(MODULE_NAME ": %s - param ""bytes_in_use"" is NULL.\n",
1360 __func__);
1361 return -EINVAL;
1362 }
1363
1364 if (write_ptr >= read_ptr) {
1365 if (read_ptr == 0)
1366 *free_bytes = total_size - write_ptr - 1;
1367 else
1368 *free_bytes = total_size - write_ptr;
1369 *bytes_in_use = write_ptr - read_ptr;
1370 } else {
1371 *bytes_in_use = total_size - read_ptr;
1372 *free_bytes = read_ptr - write_ptr - 1;
1373 }
1374
1375 return 0;
1376}
1377
1378/**
1379 * sdioc_bytes_free_in_buffer
1380 * this routine calculates how many bytes are free in a buffer
1381 * and how many are in use, according to its reading and
1382 * writing pointer offsets.
1383 *
1384 * @write_ptr: writing pointer.
1385 * @read_ptr: reading pointer.
1386 * @total_size: buffer size.
1387 * @free_bytes: return value-how many free bytes in buffer.
1388 * @bytes_in_use: return value-how many bytes in use in buffer.
1389 * @return 0 on success or negative value on error.
1390 *
1391 * buffer is treated as a cyclic buffer.
1392 */
1393static int sdioc_bytes_free_in_buffer(int write_ptr,
1394 int read_ptr,
1395 int total_size,
1396 int *free_bytes,
1397 int *bytes_in_use)
1398{
1399 if (!free_bytes) {
1400 pr_err(MODULE_NAME ": %s - param ""free_bytes"" is NULL.\n",
1401 __func__);
1402 return -EINVAL;
1403 }
1404
1405 if (!bytes_in_use) {
1406 pr_err(MODULE_NAME ": %s - param ""bytes_in_use"" is NULL.\n",
1407 __func__);
1408 return -EINVAL;
1409 }
1410
1411 /* if pointers equel - buffers are empty. nothing to read/write */
1412
1413 if (write_ptr >= read_ptr)
1414 *bytes_in_use = write_ptr - read_ptr;
1415 else
1416 *bytes_in_use = total_size - (read_ptr - write_ptr);
1417
1418 *free_bytes = total_size - *bytes_in_use - 1;
1419
1420 return 0;
1421}
1422
1423/*
1424* sdio_dld_write_room
1425*
1426* This is the write_room function of the tty driver.
1427*
1428* @tty: pointer to tty struct.
1429* @return free bytes for write.
1430*
1431*/
1432static int sdio_dld_write_room(struct tty_struct *tty)
1433{
1434 return sdio_dld->sdio_dloader_data.outgoing_data.buffer_size;
1435}
1436
1437/**
1438 * sdio_dld_write_callback
1439 * this is the write callback of the tty driver.
1440 *
1441 * @tty: pointer to tty struct.
1442 * @buf: buffer to write from.
1443 * @count: number of bytes to write.
1444 * @return bytes written or negative value on error.
1445 *
1446 * if destination buffer has not enough room for the incoming
1447 * data, returns an error.
1448 */
1449static int sdio_dld_write_callback(struct tty_struct *tty,
1450 const unsigned char *buf, int count)
1451{
1452 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
1453 int dst_free_bytes = 0;
1454 int dummy = 0;
1455 int status = 0;
1456 int bytes_written = 0;
1457 int total_written = 0;
1458 static int write_retry;
1459 int pending_to_write = count;
1460
1461#ifdef CONFIG_DEBUG_FS
1462 debugfs_glob.global_count = count;
1463 update_gd(SDIO_DLD_DEBUGFS_CASE_5_CODE);
1464#endif
1465
1466 pr_debug(MODULE_NAME ": %s - WRITING CALLBACK CALLED WITH %d bytes\n",
1467 __func__, count);
1468
1469 if (!outgoing->data) {
1470 pr_err(MODULE_NAME ": %s - param ""outgoing->data"" is NULL.\n",
1471 __func__);
1472 return -EINVAL;
1473 }
1474
1475 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK size to write to outgoing"
1476 " buffer %d\n", __func__, count);
1477
1478 /* as long as there is something to write to outgoing buffer */
1479 do {
1480 int bytes_to_write = 0;
1481 status = sdioc_bytes_free_in_buffer(
1482 outgoing->offset_write_p,
1483 outgoing->offset_read_p,
1484 outgoing->buffer_size,
1485 &dst_free_bytes,
1486 &dummy);
1487
1488 if (status) {
1489 pr_err(MODULE_NAME ": %s - Failure in Function "
1490 "sdioc_bytes_free_in_buffer(). status=%d\n",
1491 __func__, status);
1492 return status;
1493 }
1494
1495 /*
1496 * if there is free room in outgoing buffer
1497 * lock mutex and request trigger notification from the main
1498 * task. unlock mutex, and wait for sinal
1499 */
1500 if (dst_free_bytes > 0) {
1501 write_retry = 0;
1502 /*
1503 * if there is more data to write to outgoing buffer
1504 * than it can receive, wait for signal from main task
1505 */
1506 if (pending_to_write > dst_free_bytes) {
1507
1508 /* sampling updated dst_free_bytes */
1509 status = sdioc_bytes_free_in_buffer(
1510 outgoing->offset_write_p,
1511 outgoing->offset_read_p,
1512 outgoing->buffer_size,
1513 &dst_free_bytes,
1514 &dummy);
1515
1516 if (status) {
1517 pr_err(MODULE_NAME ": %s - Failure in "
1518 "Function "
1519 "sdioc_bytes_free_in_buffer(). "
1520 "status=%d\n", __func__, status);
1521 return status;
1522 }
1523 }
1524
1525 bytes_to_write = min(pending_to_write, dst_free_bytes);
1526 bytes_written =
1527 writing_size_to_buf(outgoing->data,
1528 buf+total_written,
1529 &outgoing->offset_write_p,
1530 outgoing->buffer_size,
1531 outgoing->offset_read_p,
1532 bytes_to_write);
1533
1534 /* keeping sdio_dld_info up to date */
1535 sdio_dld_info.host_write_ptr =
1536 sdio_dld->sdio_dloader_data.
1537 outgoing_data.offset_write_p;
1538
1539#ifdef CONFIG_DEBUG_FS
1540 debugfs_glob.global_write_tty = bytes_written;
1541 update_gd(SDIO_DLD_DEBUGFS_CASE_3_CODE);
1542#endif
1543 sdio_dld_info.global_bytes_write_tty += bytes_written;
1544
1545 spin_lock_irqsave(&lock2, lock_flags2);
1546 if (sdio_dld->main_loop_event.wake_up_signal == 0) {
1547 sdio_dld->main_loop_event.wake_up_signal = 1;
1548 wake_up(&sdio_dld->main_loop_event.wait_event);
1549 }
1550 spin_unlock_irqrestore(&lock2, lock_flags2);
1551
1552 /*
1553 * although outgoing buffer has enough room, writing
1554 * failed
1555 */
1556 if (bytes_written != bytes_to_write) {
1557 pr_err(MODULE_NAME ": %s - couldn't write "
1558 "%d bytes to " "outgoing buffer."
1559 "bytes_written=%d\n",
1560 __func__, bytes_to_write,
1561 bytes_written);
1562 return -EIO;
1563 }
1564
1565 total_written += bytes_written;
1566 pending_to_write -= bytes_written;
1567 outgoing->num_of_bytes_in_use += bytes_written;
1568
1569 pr_debug(MODULE_NAME ": %s - WRITE CHUNK to outgoing "
1570 "buffer. pending_to_write=%d, "
1571 "outgoing_free_bytes=%d, "
1572 "bytes_written=%d\n",
1573 __func__,
1574 pending_to_write,
1575 dst_free_bytes,
1576 bytes_written);
1577
1578 } else {
1579 write_retry++;
1580
1581 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK - NO ROOM."
1582 " pending_to_write=%d, write_retry=%d\n",
1583 __func__,
1584 pending_to_write,
1585 write_retry);
1586
1587 spin_lock_irqsave(&lock1, lock_flags1);
1588 sdio_dld->write_callback_event.wake_up_signal = 0;
1589 spin_unlock_irqrestore(&lock1, lock_flags1);
1590
1591 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK - "
1592 "WAITING...", __func__);
1593#ifdef CONFIG_DEBUG_FS
1594 update_gd(SDIO_DLD_DEBUGFS_CASE_8_CODE);
1595#endif
1596 wait_event(sdio_dld->write_callback_event.wait_event,
1597 sdio_dld->write_callback_event.
1598 wake_up_signal);
1599#ifdef CONFIG_DEBUG_FS
1600 update_gd(SDIO_DLD_DEBUGFS_CASE_9_CODE);
1601#endif
1602 pr_debug(MODULE_NAME ": %s - WRITE CALLBACK - "
1603 "WOKE UP...", __func__);
1604 }
1605 } while (pending_to_write > 0 && write_retry < WRITE_RETRIES);
1606
1607 if (pending_to_write > 0) {
1608
1609 pr_err(MODULE_NAME ": %s - WRITE CALLBACK - pending data is "
1610 "%d out of %d > 0. total written in this "
1611 "callback = %d\n",
1612 __func__, pending_to_write, count, total_written);
1613 }
1614
1615 if (write_retry == WRITE_RETRIES) {
1616 pr_err(MODULE_NAME ": %s, write_retry=%d= max\n",
1617 __func__, write_retry);
1618 }
1619
1620#ifdef CONFIG_DEBUG_FS
1621 debugfs_glob.global_bytes_cb_tty = total_written;
1622 update_gd(SDIO_DLD_DEBUGFS_CASE_10_CODE);
1623#endif
1624
1625 return total_written;
1626}
1627
1628/**
1629 * sdio_memcpy_fromio_wrapper -
1630 * reads from sdioc, and updats the sdioc registers according
1631 * to how many bytes were actually read.
1632 *
1633 * @str_func: a pointer to func struct.
1634 * @client_rd_ptr: sdioc value of downlink read ptr.
1635 * @client_wr_ptr: sdioc value of downlink write ptr.
1636 * @buffer_to_store: buffer to store incoming data.
1637 * @address_to_read: address to start reading from in sdioc.
1638 * @size_to_read: size of bytes to read.
1639 * @client_buffer_size: sdioc downlink buffer size.
1640 * @return 0 on success or negative value on error.
1641 */
1642static int sdio_memcpy_fromio_wrapper(struct sdio_func *str_func,
1643 unsigned int client_rd_ptr,
1644 unsigned int client_wr_ptr,
1645 void *buffer_to_store,
1646 unsigned int address_to_read_from,
1647 int size_to_read,
1648 int client_buffer_size)
1649{
1650 int status = 0;
1651 struct sdioc_reg_chunk *reg_str =
1652 &sdio_dld->sdio_dloader_data.sdioc_reg;
1653
1654 if (!str_func) {
1655 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1656 __func__);
1657 return -EINVAL;
1658 }
1659
1660 if (!buffer_to_store) {
1661 pr_err(MODULE_NAME ": %s - param ""buffer_to_store"" is "
1662 "NULL.\n",
1663 __func__);
1664 return -EINVAL;
1665 }
1666
1667 if (size_to_read < 0) {
1668 pr_err(MODULE_NAME ": %s - invalid size to read=%d\n",
1669 __func__, size_to_read);
1670 return -EINVAL;
1671 }
1672
1673 sdio_claim_host(str_func);
1674
1675 pr_debug(MODULE_NAME ": %s, READING DATA - from add %d, "
1676 "size_to_read=%d\n",
1677 __func__, address_to_read_from, size_to_read);
1678
1679 status = sdio_memcpy_fromio(str_func,
1680 (void *)buffer_to_store,
1681 address_to_read_from,
1682 size_to_read);
1683 if (status) {
1684 pr_err(MODULE_NAME ": %s - sdio_memcpy_fromio()"
1685 " DATA failed. status=%d.\n",
1686 __func__, status);
1687 sdio_release_host(str_func);
1688 return status;
1689 }
1690
1691 /* updating an offset according to cyclic buffer size */
1692 reg_str->dl_rd_ptr.reg_val =
1693 (reg_str->dl_rd_ptr.reg_val + size_to_read) %
1694 client_buffer_size;
1695 /* keeping sdio_dld_info up to date */
1696 sdio_dld_info.cl_dl_rd_ptr = reg_str->dl_rd_ptr.reg_val;
1697
1698 status = sdio_memcpy_toio(str_func,
1699 reg_str->dl_rd_ptr.reg_offset,
1700 (void *)&reg_str->dl_rd_ptr.reg_val,
1701 sizeof(reg_str->dl_rd_ptr.reg_val));
1702
1703 if (status) {
1704 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
1705 "UPDATE PTR failed. status=%d.\n",
1706 __func__, status);
1707 }
1708
1709 sdio_release_host(str_func);
1710 return status;
1711}
1712
1713/**
1714 * sdio_memcpy_toio_wrapper
1715 * writes to sdioc, and updats the sdioc registers according
1716 * to how many bytes were actually read.
1717 *
1718 * @str_func: a pointer to func struct.
1719 * @client_wr_ptr: sdioc downlink write ptr.
1720 * @h_read_ptr: host incoming read ptrs
1721 * @buf_write_from: buffer to write from.
1722 * @bytes_to_write: number of bytes to write.
1723 * @return 0 on success or negative value on error.
1724 */
1725static int sdio_memcpy_toio_wrapper(struct sdio_func *str_func,
1726 unsigned int client_wr_ptr,
1727 unsigned int h_read_ptr,
1728 void *buf_write_from,
1729 int bytes_to_write)
1730{
1731 int status = 0;
1732 struct sdioc_reg_chunk *reg_str =
1733 &sdio_dld->sdio_dloader_data.sdioc_reg;
1734 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
1735
1736 if (!str_func) {
1737 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1738 __func__);
1739 return -EINVAL;
1740 }
1741
1742 if (!buf_write_from) {
1743 pr_err(MODULE_NAME ": %s - param ""buf_write_from"" is NULL.\n",
1744 __func__);
1745 return -EINVAL;
1746 }
1747
1748 sdio_claim_host(str_func);
1749
1750 pr_debug(MODULE_NAME ": %s, WRITING DATA TOIO to address 0x%x, "
1751 "bytes_to_write=%d\n",
1752 __func__,
1753 reg_str->up_buff_address.reg_val + reg_str->up_wr_ptr.reg_val,
1754 bytes_to_write);
1755
1756 status = sdio_memcpy_toio(str_func,
1757 reg_str->up_buff_address.reg_val +
1758 reg_str->up_wr_ptr.reg_val,
1759 (void *) (outgoing->data + h_read_ptr),
1760 bytes_to_write);
1761
1762 if (status) {
1763 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
1764 "DATA failed. status=%d.\n", __func__, status);
1765 sdio_release_host(str_func);
1766 return status;
1767 }
1768
1769 sdio_dld_info.global_bytes_write_toio += bytes_to_write;
1770 outgoing->num_of_bytes_in_use -= bytes_to_write;
1771
1772 /*
1773 * if writing to client succeeded, then
1774 * 1. update the client up_wr_ptr
1775 * 2. update the host outgoing rd ptr
1776 **/
1777 reg_str->up_wr_ptr.reg_val =
1778 ((reg_str->up_wr_ptr.reg_val + bytes_to_write) %
1779 reg_str->ul_buff_size.reg_val);
1780
1781 /* keeping sdio_dld_info up to date */
1782 sdio_dld_info.cl_up_wr_ptr = reg_str->up_wr_ptr.reg_val;
1783
1784 outgoing->offset_read_p =
1785 ((outgoing->offset_read_p + bytes_to_write) %
1786 outgoing->buffer_size);
1787
1788 /* keeping sdio_dld_info up to date*/
1789 sdio_dld_info.host_read_ptr = outgoing->offset_read_p;
1790
1791#ifdef CONFIG_DEBUG_FS
1792 debugfs_glob.global_write_toio = bytes_to_write;
1793 update_gd(SDIO_DLD_DEBUGFS_CASE_4_CODE);
1794#endif
1795
1796 /* updating uplink write pointer according to size that was written */
1797 status = sdio_memcpy_toio(str_func,
1798 reg_str->up_wr_ptr.reg_offset,
1799 (void *)(&reg_str->up_wr_ptr.reg_val),
1800 sizeof(reg_str->up_wr_ptr.reg_val));
1801 if (status) {
1802 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
1803 "UPDATE PTR failed. status=%d.\n",
1804 __func__, status);
1805 }
1806
1807 sdio_release_host(str_func);
1808 return status;
1809}
1810
1811/**
1812 * sdio_dld_read
1813 * reads from sdioc
1814 *
1815 * @client_rd_ptr: sdioc downlink read ptr.
1816 * @client_wr_ptr: sdioc downlink write ptr.
1817 * @reg_str: sdioc register shadowing struct.
1818 * @str_func: a pointer to func struct.
1819 * @bytes_read:how many bytes read.
1820 * @return 0 on success or negative value on error.
1821 */
1822static int sdio_dld_read(unsigned int client_rd_ptr,
1823 unsigned int client_wr_ptr,
1824 struct sdioc_reg_chunk *reg_str,
1825 struct sdio_func *str_func,
1826 int *bytes_read)
1827{
1828 int status = 0;
1829 struct sdio_data *incoming = &sdio_dld->sdio_dloader_data.incoming_data;
1830
1831 if (!reg_str) {
1832 pr_err(MODULE_NAME ": %s - param ""reg_str"" is NULL.\n",
1833 __func__);
1834 return -EINVAL;
1835 }
1836
1837 if (!str_func) {
1838 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1839 __func__);
1840 return -EINVAL;
1841 }
1842
1843 if (!bytes_read) {
1844 pr_err(MODULE_NAME ": %s - param ""bytes_read"" is NULL.\n",
1845 __func__);
1846 return -EINVAL;
1847 }
1848
1849 /* there is data to read in ONE chunk */
1850 if (client_wr_ptr > client_rd_ptr) {
1851 status = sdio_memcpy_fromio_wrapper(
1852 str_func,
1853 client_rd_ptr,
1854 client_wr_ptr,
1855 (void *)incoming->data,
1856 reg_str->dl_buff_address.reg_val + client_rd_ptr,
1857 client_wr_ptr - client_rd_ptr,
1858 reg_str->dl_buff_size.reg_val);
1859
1860 if (status) {
1861 pr_err(MODULE_NAME ": %s - Failure in Function "
1862 "sdio_memcpy_fromio_wrapper(). "
1863 "SINGLE CHUNK READ. status=%d\n",
1864 __func__, status);
1865 return status;
1866 }
1867
1868 incoming->num_of_bytes_in_use += client_wr_ptr - client_rd_ptr;
1869 *bytes_read = client_wr_ptr - client_rd_ptr;
1870
1871#ifdef CONFIG_DEBUG_FS
1872 debugfs_glob.global_to_read =
1873 client_wr_ptr - client_rd_ptr;
1874 update_gd(SDIO_DLD_DEBUGFS_CASE_11_CODE);
1875#endif
1876 }
1877
1878 /* there is data to read in TWO chunks */
1879 else {
1880 int dl_buf_size = reg_str->dl_buff_size.reg_val;
1881 int tail_size = dl_buf_size - client_rd_ptr;
1882
1883 /* reading chunk#1: from rd_ptr to the end of the buffer */
1884 status = sdio_memcpy_fromio_wrapper(
1885 str_func,
1886 client_rd_ptr,
1887 dl_buf_size,
1888 (void *)incoming->data,
1889 reg_str->dl_buff_address.reg_val + client_rd_ptr,
1890 tail_size,
1891 dl_buf_size);
1892
1893 if (status) {
1894 pr_err(MODULE_NAME ": %s - Failure in Function "
1895 "sdio_memcpy_fromio_wrapper(). "
1896 "1 of 2 CHUNKS READ. status=%d\n",
1897 __func__, status);
1898 return status;
1899 }
1900
1901 incoming->num_of_bytes_in_use += tail_size;
1902 *bytes_read = tail_size;
1903
1904#ifdef CONFIG_DEBUG_FS
1905 debugfs_glob.global_to_read = tail_size;
1906 update_gd(SDIO_DLD_DEBUGFS_CASE_11_CODE);
1907#endif
1908
1909 /* reading chunk#2: reading from beginning buffer */
1910 status = sdio_memcpy_fromio_wrapper(
1911 str_func,
1912 client_rd_ptr,
1913 client_wr_ptr,
1914 (void *)(incoming->data + tail_size),
1915 reg_str->dl_buff_address.reg_val,
1916 client_wr_ptr,
1917 reg_str->dl_buff_size.reg_val);
1918
1919 if (status) {
1920 pr_err(MODULE_NAME ": %s - Failure in Function "
1921 "sdio_memcpy_fromio_wrapper(). "
1922 "2 of 2 CHUNKS READ. status=%d\n",
1923 __func__, status);
1924 return status;
1925 }
1926
1927 incoming->num_of_bytes_in_use += client_wr_ptr;
1928 *bytes_read += client_wr_ptr;
1929
1930#ifdef CONFIG_DEBUG_FS
1931 debugfs_glob.global_to_read = client_wr_ptr;
1932 update_gd(SDIO_DLD_DEBUGFS_CASE_11_CODE);
1933#endif
1934 }
1935 return 0;
1936}
1937
1938/**
1939 * sdio_dld_main_task
1940 * sdio downloader main task. reads mailboxf checks if there is
1941 * anything to read, checks if host has anything to
1942 * write.
1943 *
1944 * @card: a pointer to mmc_card.
1945 * @return 0 on success or negative value on error.
1946 */
1947static int sdio_dld_main_task(void *card)
1948{
1949 int status = 0;
1950 struct tty_struct *tty = sdio_dld->tty_str;
1951 struct sdioc_reg_chunk *reg_str =
1952 &sdio_dld->sdio_dloader_data.sdioc_reg;
1953 int func = sdio_dld->sdioc_boot_func;
1954 struct sdio_func *str_func = NULL;
1955 struct sdio_data *outgoing = &sdio_dld->sdio_dloader_data.outgoing_data;
1956 struct sdio_data *incoming = &sdio_dld->sdio_dloader_data.incoming_data;
1957 struct sdio_dld_task *task = &sdio_dld->dld_main_thread;
1958 int retries = 0;
1959#ifdef PUSH_STRING
1960 int bytes_pushed = 0;
1961#endif
1962
1963 msleep(SLEEP_MS);
1964
1965 if (!card) {
1966 pr_err(MODULE_NAME ": %s - param ""card"" is NULL.\n",
1967 __func__);
1968 return -EINVAL;
1969 }
1970
1971 if (!tty) {
1972 pr_err(MODULE_NAME ": %s - param ""tty"" is NULL.\n",
1973 __func__);
1974 return -EINVAL;
1975 }
1976
1977 str_func = ((struct mmc_card *)card)->
1978 sdio_func[REAL_FUNC_TO_FUNC_IN_ARRAY(func)];
1979
1980 if (!str_func) {
1981 pr_err(MODULE_NAME ": %s - param ""str_func"" is NULL.\n",
1982 __func__);
1983 return -EINVAL;
1984 }
1985
1986 while (true) {
1987 /* client pointers for both buffers */
1988 int client_ul_wr_ptr = 0;
1989 int client_ul_rd_ptr = 0;
1990 int client_dl_wr_ptr = 0;
1991 int client_dl_rd_ptr = 0;
1992
1993 /* host pointer for outgoing buffer */
1994 int h_out_wr_ptr = 0;
1995 int h_out_rd_ptr = 0;
1996
1997 int h_bytes_rdy_wr = 0;
1998 int c_bytes_rdy_rcve = 0;
1999
2000 int need_to_write = 0;
2001 int need_to_read = 0;
2002
2003 /*
2004 * forever, checking for signal to die, then read MailBox.
2005 * if nothing to read or nothing to write to client, sleep,
2006 * and again read MailBox
2007 */
2008 do {
2009 int dummy = 0;
2010
2011 /* checking if a signal to die was sent */
2012 if (atomic_read(&task->please_close) == 1) {
2013
2014 pr_debug(MODULE_NAME ": %s - 0x%x was written "
2015 "to 9K\n", __func__, SDIOC_EXIT_CODE);
2016
2017 sdio_claim_host(str_func);
2018
2019 /* returned value is not checked on purpose */
2020 sdio_memcpy_toio(
2021 str_func,
2022 reg_str->good_to_exit_ptr.reg_offset,
2023 (void *)&reg_str->good_to_exit_ptr.
2024 reg_val,
2025 sizeof(reg_str->good_to_exit_ptr.
2026 reg_val));
2027
2028 sdio_release_host(str_func);
2029
2030 task->exit_wait.wake_up_signal = 1;
2031 wake_up(&task->exit_wait.wait_event);
2032 return 0;
2033 }
2034
2035 status = mailbox_to_seq_chunk_read_ptrs(str_func);
2036 if (status) {
2037 pr_err(MODULE_NAME ": %s - Failure in Function "
2038 "mailbox_to_seq_chunk_read_ptrs(). "
2039 "status=%d\n", __func__, status);
2040 return status;
2041 }
2042
2043 /* calculate how many bytes the host has send */
2044 h_out_wr_ptr = outgoing->offset_write_p;
2045 h_out_rd_ptr = outgoing->offset_read_p;
2046
2047 status = sdioc_bytes_till_end_of_buffer(
2048 h_out_wr_ptr,
2049 h_out_rd_ptr,
2050 outgoing->buffer_size,
2051 &dummy,
2052 &h_bytes_rdy_wr);
2053
2054 if (status) {
2055 pr_err(MODULE_NAME ": %s - Failure in Function "
2056 "sdioc_bytes_till_end_of_buffer(). "
2057 "status=%d\n", __func__, status);
2058 return status;
2059 }
2060
2061 /* is there something to read from client */
2062 client_dl_wr_ptr = reg_str->dl_wr_ptr.reg_val;
2063 client_dl_rd_ptr = reg_str->dl_rd_ptr.reg_val;
2064
2065 if (client_dl_rd_ptr != client_dl_wr_ptr)
2066 need_to_read = 1;
2067
2068 /*
2069 * calculate how many bytes the client can receive
2070 * from host
2071 */
2072 client_ul_wr_ptr = reg_str->up_wr_ptr.reg_val;
2073 client_ul_rd_ptr = reg_str->up_rd_ptr.reg_val;
2074
2075 status = sdioc_bytes_till_end_of_buffer(
2076 client_ul_wr_ptr,
2077 client_ul_rd_ptr,
2078 reg_str->ul_buff_size.reg_val,
2079 &c_bytes_rdy_rcve,
2080 &dummy);
2081
2082 if (status) {
2083 pr_err(MODULE_NAME ": %s - Failure in Function "
2084 "sdioc_bytes_till_end_of_buffer(). "
2085 "status=%d\n", __func__, status);
2086 return status;
2087 }
2088
2089 /* if host has anything to write */
2090 if (h_bytes_rdy_wr > 0)
2091 need_to_write = 1;
2092
2093 if (need_to_write || need_to_read)
2094 break;
2095
2096 spin_lock_irqsave(&lock2, lock_flags2);
2097 sdio_dld->main_loop_event.wake_up_signal = 0;
2098 spin_unlock_irqrestore(&lock2, lock_flags2);
2099
2100 pr_debug(MODULE_NAME ": %s - MAIN LOOP - WAITING...\n",
2101 __func__);
2102#ifdef CONFIG_DEBUG_FS
2103 update_gd(SDIO_DLD_DEBUGFS_CASE_6_CODE);
2104#endif
2105 wait_event(sdio_dld->main_loop_event.wait_event,
2106 sdio_dld->main_loop_event.wake_up_signal);
2107#ifdef CONFIG_DEBUG_FS
2108 update_gd(SDIO_DLD_DEBUGFS_CASE_7_CODE);
2109#endif
2110
2111 pr_debug(MODULE_NAME ": %s - MAIN LOOP - WOKE UP...\n",
2112 __func__);
2113
2114 } while (1);
2115
2116 /* CHECK IF THERE IS ANYTHING TO READ IN CLIENT */
2117 if (need_to_read) {
2118#ifdef PUSH_STRING
2119 int num_push = 0;
2120 int left = 0;
2121 int bytes_read;
2122#else
2123 int i;
2124#endif
2125 need_to_read = 0;
2126
2127 status = sdio_dld_read(client_dl_rd_ptr,
2128 client_dl_wr_ptr,
2129 reg_str,
2130 str_func,
2131 &bytes_read);
2132
2133 if (status) {
2134 pr_err(MODULE_NAME ": %s - Failure in Function "
2135 "sdio_dld_read(). status=%d\n",
2136 __func__, status);
2137 return status;
2138 }
2139
2140 sdio_dld_info.global_bytes_read_fromio +=
2141 bytes_read;
2142
2143 bytes_pushed = 0;
2144#ifdef PUSH_STRING
2145 left = incoming->num_of_bytes_in_use;
2146 start_timer(&sdio_dld->push_timer,
2147 sdio_dld->push_timer_ms);
2148 do {
2149 num_push = tty_insert_flip_string(
2150 tty,
2151 incoming->data+bytes_pushed,
2152 left);
2153
2154 bytes_pushed += num_push;
2155 left -= num_push;
2156 tty_flip_buffer_push(tty);
2157 } while (left != 0);
2158
2159 del_timer(&sdio_dld->push_timer);
2160
2161 if (bytes_pushed != incoming->num_of_bytes_in_use) {
2162 pr_err(MODULE_NAME ": %s - failed\n",
2163 __func__);
2164 }
2165#else
2166 pr_debug(MODULE_NAME ": %s - NEED TO READ %d\n",
2167 __func__, incoming->num_of_bytes_in_use);
2168
2169 for (i = 0 ; i < incoming->num_of_bytes_in_use ; ++i) {
2170 int err = 0;
2171 err = tty_insert_flip_char(tty,
2172 incoming->data[i],
2173 TTY_NORMAL);
2174 tty_flip_buffer_push(tty);
2175 }
2176
2177 pr_debug(MODULE_NAME ": %s - JUST READ\n", __func__);
2178#endif /*PUSH_STRING*/
2179 sdio_dld_info.global_bytes_push_tty +=
2180 incoming->num_of_bytes_in_use;
2181#ifdef CONFIG_DEBUG_FS
2182 debugfs_glob.global_push_to_tty = bytes_read;
2183 update_gd(SDIO_DLD_DEBUGFS_CASE_12_CODE);
2184#endif
2185 incoming->num_of_bytes_in_use = 0;
2186 tty_flip_buffer_push(tty);
2187 }
2188
2189 /* CHECK IF THERE IS ANYTHING TO WRITE IN HOST AND HOW MUCH */
2190 if (need_to_write) {
2191 int dummy = 0;
2192
2193 do {
2194 int bytes_to_write = min(c_bytes_rdy_rcve,
2195 h_bytes_rdy_wr);
2196
2197 /*
2198 * in case nothing to send or no room to
2199 * receive
2200 */
2201 if (bytes_to_write == 0)
2202 break;
2203
2204 if (client_ul_rd_ptr == 0 &&
2205 (client_ul_rd_ptr != client_ul_wr_ptr))
2206 break;
2207
2208 /*
2209 * if client_rd_ptr points to start, but there
2210 * is data to read wait until WRITE_TILL_END
2211 * before writing a chunk of data, to avoid
2212 * writing until (BUF_SIZE - 1), because it will
2213 * yield an extra write of "1" bytes
2214 */
2215 if (client_ul_rd_ptr == 0 &&
2216 (client_ul_rd_ptr != client_ul_wr_ptr) &&
2217 retries < WRITE_TILL_END_RETRIES) {
2218 retries++;
2219 break;
2220 }
2221 retries = 0;
2222
2223#ifdef CONFIG_DEBUG_FS
2224 debugfs_glob.global_8k_has = h_bytes_rdy_wr;
2225 debugfs_glob.global_9k_has = c_bytes_rdy_rcve;
2226 debugfs_glob.global_min = bytes_to_write;
2227 update_gd(SDIO_DLD_DEBUGFS_CASE_2_CODE);
2228#endif
2229 need_to_write = 0;
2230
2231 pr_debug(MODULE_NAME ": %s - NEED TO WRITE "
2232 "TOIO %d\n",
2233 __func__, bytes_to_write);
2234
2235 status = sdio_memcpy_toio_wrapper(
2236 str_func,
2237 reg_str->up_wr_ptr.reg_val,
2238 outgoing->offset_read_p,
2239 (void *)((char *)outgoing->data +
2240 outgoing->offset_read_p),
2241 bytes_to_write);
2242
2243 if (status) {
2244 pr_err(MODULE_NAME ": %s - Failure in "
2245 "Function "
2246 "sdio_memcpy_toio_wrapper(). "
2247 "SINGLE CHUNK WRITE. "
2248 "status=%d\n",
2249 __func__, status);
2250 return status;
2251 }
2252
2253 sdio_claim_host(str_func);
2254
2255 status = sdio_memcpy_fromio(
2256 str_func,
2257 (void *)&reg_str->up_rd_ptr.reg_val,
2258 SDIOC_UL_RD_PTR,
2259 sizeof(reg_str->up_rd_ptr.reg_val));
2260
2261 if (status) {
2262 pr_err(MODULE_NAME ": %s - "
2263 "sdio_memcpy_fromio() "
2264 "failed. status=%d\n",
2265 __func__, status);
2266 sdio_release_host(str_func);
2267
2268 return status;
2269 }
2270
2271 sdio_release_host(str_func);
2272
2273 spin_lock_irqsave(&lock1, lock_flags1);
2274 if (sdio_dld->write_callback_event.
2275 wake_up_signal == 0) {
2276 sdio_dld->write_callback_event.
2277 wake_up_signal = 1;
2278 wake_up(&sdio_dld->
2279 write_callback_event.
2280 wait_event);
2281 }
2282
2283 spin_unlock_irqrestore(&lock1, lock_flags1);
2284 client_ul_wr_ptr = reg_str->up_wr_ptr.reg_val;
2285 client_ul_rd_ptr = reg_str->up_rd_ptr.reg_val;
2286
2287 status = sdioc_bytes_till_end_of_buffer(
2288 client_ul_wr_ptr,
2289 client_ul_rd_ptr,
2290 reg_str->ul_buff_size.reg_val,
2291 &c_bytes_rdy_rcve,
2292 &dummy);
2293
2294 /* calculate how many bytes host has to send */
2295 h_out_wr_ptr = outgoing->offset_write_p;
2296 h_out_rd_ptr = outgoing->offset_read_p;
2297
2298 status = sdioc_bytes_till_end_of_buffer(
2299 h_out_wr_ptr,
2300 h_out_rd_ptr,
2301 outgoing->buffer_size,
2302 &dummy,
2303 &h_bytes_rdy_wr);
2304
2305 } while (h_out_wr_ptr != h_out_rd_ptr);
2306 }
2307 }
2308 return 0;
2309}
2310
2311/**
2312 * sdio_dld_init_global
2313 * initialization of sdio_dld global struct
2314 *
2315 * @card: a pointer to mmc_card.
2316 * @return 0 on success or negative value on error.
2317 */
2318static int sdio_dld_init_global(struct mmc_card *card,
2319 int(*done)(void))
2320{
2321 if (!card) {
2322 pr_err(MODULE_NAME ": %s - param ""card"" is NULL.\n",
2323 __func__);
2324 return -EINVAL;
2325 }
2326
2327 if (!done) {
2328 pr_err(MODULE_NAME ": %s - param ""done"" is NULL.\n",
2329 __func__);
2330 return -EINVAL;
2331 }
2332
2333 sdio_dld->done_callback = done;
2334 sdio_dld->card = card;
2335 init_waitqueue_head(&sdio_dld->dld_main_thread.exit_wait.wait_event);
2336 sdio_dld->write_callback_event.wake_up_signal = 1;
2337 sdio_dld->main_loop_event.wake_up_signal = 1;
2338
2339 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_size.reg_offset =
2340 SDIOC_DL_BUFF_SIZE_OFFSET;
2341
2342 sdio_dld->sdio_dloader_data.sdioc_reg.dl_rd_ptr.reg_offset =
2343 SDIOC_DL_RD_PTR;
2344
2345 sdio_dld->sdio_dloader_data.sdioc_reg.dl_wr_ptr.reg_offset =
2346 SDIOC_DL_WR_PTR;
2347
2348 sdio_dld->sdio_dloader_data.sdioc_reg.ul_buff_size.reg_offset =
2349 SDIOC_UP_BUFF_SIZE_OFFSET;
2350
2351 sdio_dld->sdio_dloader_data.sdioc_reg.up_rd_ptr.reg_offset =
2352 SDIOC_UL_RD_PTR;
2353
2354 sdio_dld->sdio_dloader_data.sdioc_reg.up_wr_ptr.reg_offset =
2355 SDIOC_UL_WR_PTR;
2356
2357 sdio_dld->sdio_dloader_data.sdioc_reg.good_to_exit_ptr.reg_offset =
2358 SDIOC_EXIT_PTR;
2359
2360 sdio_dld->sdio_dloader_data.sdioc_reg.dl_buff_address.reg_offset =
2361 SDIOC_DL_BUFF_ADDRESS;
2362
2363 sdio_dld->sdio_dloader_data.sdioc_reg.up_buff_address.reg_offset =
2364 SDIOC_UP_BUFF_ADDRESS;
2365
2366 sdio_dld_set_op_mode(SDIO_DLD_NORMAL_MODE);
2367
2368 return 0;
2369}
2370
2371/**
2372 * sdio_downloader_setup
2373 * initializes the TTY driver
2374 *
2375 * @card: a pointer to mmc_card.
2376 * @num_of_devices: number of devices.
2377 * @channel_number: channel number.
2378 * @return 0 on success or negative value on error.
2379 *
2380 * The TTY stack needs to know in advance how many devices it should
2381 * plan to manage. Use this call to set up the ports that will
2382 * be exported through SDIO.
2383 */
2384int sdio_downloader_setup(struct mmc_card *card,
2385 unsigned int num_of_devices,
2386 int channel_number,
2387 int(*done)(void))
2388{
2389 int status = 0;
2390 int result = 0;
2391 int func_in_array = 0;
2392 struct sdio_func *str_func = NULL;
2393 struct device *tty_dev;
2394
2395 if (num_of_devices == 0 || num_of_devices > MAX_NUM_DEVICES) {
2396 pr_err(MODULE_NAME ": %s - invalid number of devices\n",
2397 __func__);
2398 return -EINVAL;
2399 }
2400
2401 if (!card) {
2402 pr_err(MODULE_NAME ": %s - param ""card"" is NULL.\n",
2403 __func__);
2404 return -EINVAL;
2405 }
2406
2407 if (!done) {
2408 pr_err(MODULE_NAME ": %s - param ""done"" is NULL.\n",
2409 __func__);
2410 return -EINVAL;
2411 }
2412
2413 sdio_dld = kzalloc(sizeof(struct sdio_downloader), GFP_KERNEL);
2414 if (!sdio_dld) {
2415 pr_err(MODULE_NAME ": %s - couldn't allocate sdio_dld data "
2416 "structure.", __func__);
2417 return -ENOMEM;
2418 }
2419
2420#ifdef CONFIG_DEBUG_FS
2421 bootloader_debugfs_init();
2422#endif /* CONFIG_DEBUG_FS */
2423
2424 status = sdio_dld_init_global(card, done);
2425
2426 if (status) {
2427 pr_err(MODULE_NAME ": %s - Failure in Function "
2428 "sdio_dld_init_global(). status=%d\n",
2429 __func__, status);
2430 kfree(sdio_dld);
2431 return status;
2432 }
2433
2434 sdio_dld->tty_drv = alloc_tty_driver(num_of_devices);
2435
2436 if (!sdio_dld->tty_drv) {
2437 pr_err(MODULE_NAME ": %s - param ""sdio_dld->tty_drv"" is "
2438 "NULL.\n", __func__);
2439 kfree(sdio_dld);
2440 return -EINVAL;
2441 }
2442
2443 sdio_dld_set_op_mode((enum sdio_dld_op_mode)sdio_op_mode);
2444
2445 /* according to op_mode, a different tty device is created */
2446 if (sdio_dld->op_mode == SDIO_DLD_BOOT_TEST_MODE)
2447 sdio_dld->tty_drv->name = TTY_SDIO_DEV_TEST;
2448 else
2449 sdio_dld->tty_drv->name = TTY_SDIO_DEV;
2450
2451 sdio_dld->tty_drv->owner = THIS_MODULE;
2452 sdio_dld->tty_drv->driver_name = "SDIO_Dloader";
2453
2454 /* uses dynamically assigned dev_t values */
2455 sdio_dld->tty_drv->type = TTY_DRIVER_TYPE_SERIAL;
2456 sdio_dld->tty_drv->subtype = SERIAL_TYPE_NORMAL;
2457 sdio_dld->tty_drv->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV
2458 | TTY_DRIVER_RESET_TERMIOS;
2459
2460 /* initializing the tty driver */
2461 sdio_dld->tty_drv->init_termios = tty_std_termios;
2462 sdio_dld->tty_drv->init_termios.c_cflag =
2463 B4800 | CS8 | CREAD | HUPCL | CLOCAL;
2464 sdio_dld->tty_drv->init_termios.c_ispeed = INPUT_SPEED;
2465 sdio_dld->tty_drv->init_termios.c_ospeed = OUTPUT_SPEED;
2466
2467 tty_set_operations(sdio_dld->tty_drv, &sdio_dloader_tty_ops);
2468
2469 status = tty_register_driver(sdio_dld->tty_drv);
2470 if (status) {
2471 put_tty_driver(sdio_dld->tty_drv);
2472 pr_err(MODULE_NAME ": %s - tty_register_driver() failed\n",
2473 __func__);
2474
2475 sdio_dld->tty_drv = NULL;
2476 kfree(sdio_dld);
2477 return status;
2478 }
2479
2480 tty_dev = tty_register_device(sdio_dld->tty_drv, 0, NULL);
2481 if (IS_ERR(tty_dev)) {
2482 pr_err(MODULE_NAME ": %s - tty_register_device() "
2483 "failed\n", __func__);
2484 tty_unregister_driver(sdio_dld->tty_drv);
2485 kfree(sdio_dld);
2486 return PTR_ERR(tty_dev);
2487 }
2488
2489 sdio_dld->sdioc_boot_func = SDIOC_CHAN_TO_FUNC_NUM(channel_number);
2490 func_in_array = REAL_FUNC_TO_FUNC_IN_ARRAY(sdio_dld->sdioc_boot_func);
2491 str_func = sdio_dld->card->sdio_func[func_in_array];
2492 status = sdio_dld_init_func(str_func);
2493 if (status) {
2494 pr_err(MODULE_NAME ": %s - Failure in Function "
2495 "sdio_dld_init_func(). status=%d\n",
2496 __func__, status);
2497 goto exit_err;
2498 }
2499
2500#ifdef CONFIG_DEBUG_FS
2501 sdio_dld_debug_init();
2502#endif
2503
2504 sdio_claim_host(str_func);
2505
2506 /*
2507 * notifing the client by writing what mode we are by writing
2508 * to a special register
2509 */
2510 status = sdio_memcpy_toio(str_func,
2511 SDIOC_OP_MODE_PTR,
2512 (void *)&sdio_dld->op_mode,
2513 sizeof(sdio_dld->op_mode));
2514
2515 sdio_release_host(str_func);
2516
2517 if (status) {
2518 pr_err(MODULE_NAME ": %s - sdio_memcpy_toio() "
2519 "writing to OP_MODE_REGISTER failed. "
2520 "status=%d.\n",
2521 __func__, status);
2522 goto exit_err;
2523 }
2524
2525 return 0;
2526
2527exit_err:
2528 tty_unregister_device(sdio_dld->tty_drv, 0);
2529 result = tty_unregister_driver(sdio_dld->tty_drv);
2530 if (result)
2531 pr_err(MODULE_NAME ": %s - tty_unregister_driver() "
2532 "failed. result=%d\n", __func__, -result);
2533 kfree(sdio_dld);
2534 return status;
2535}
2536
2537MODULE_LICENSE("GPL v2");
2538MODULE_DESCRIPTION("SDIO Downloader");
2539MODULE_AUTHOR("Yaniv Gardi <ygardi@codeaurora.org>");
2540MODULE_VERSION(DRV_VERSION);
2541