Initial commit
[kernel/linux-3.0.git] / fs / j4fs / llid_kernel.c
1 /*
2  * This software is the confidential and proprietary information
3  * of Samsung Electronics, Inc. ("Confidential Information").  You
4  * shall not disclose such Confidential Information and shall use
5  * it only in accordance with the terms of the license agreement
6  * you entered into with Samsung.
7  */
8 /*
9  * llid.c
10  *
11  * Low-Level Interface Driver
12  *
13  * COPYRIGHT(C) Samsung Electronics Co.Ltd. 2009-2010 All Right Reserved.
14  *
15  * 2009.02 - First editing by SungHwan.yun <sunghwan.yun@samsung.com> @LDK@
16  *
17  * 2009.03 - Currently managed by  SungHwan.yun <sunghwan.yun@samsung.com> @LDK@
18  *
19  */
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include "j4fs.h"
23
24 #if defined(J4FS_USE_XSR)
25
26 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
27 #include "./Inc/XsrTypes.h"
28 #include "./Inc/STL.h"
29 #else
30 #include "../../drivers/txsr/Inc/XsrTypes.h"
31 #include "../../drivers/txsr/Inc/STL.h"
32 #endif
33
34 #elif defined(J4FS_USE_FSR)
35
36 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 28))
37 #include "../../tfsr/Inc/FSR.h"
38 #include "../../tfsr/Inc/FSR_STL.h"
39 #else
40 #include "../fsr/Inc/FSR.h"
41 #include "../fsr/Inc/FSR_STL.h"
42 #endif
43
44 #elif defined(J4FS_USE_MOVI)
45 /* j4fs device node name */
46 #define J4FS_DEVNAME                    CONFIG_J4FS_DEVNAME
47 static struct file *j4fs_filp;
48
49 #else
50 'compile error'
51 #endif
52
53 extern j4fs_device_info device_info;
54 extern unsigned int j4fs_traceMask;
55
56 /*
57   * Fills the specified buffer with the number of bytes defined by length from the device's absolute physical address
58   * parameters
59   *   offset  : start physical page number
60   *   length  : bytes to write
61   *   buffer  : buffer to write data
62   * return value
63   *   The return code should indicate ERROR (1) or OK(0). If the return code indicates ERROR, the dev_ptr->status field should indicate
64   *   the return error code.
65   */
66 int FlashDevRead(j4fs_device_info *dev_ptr, DWORD offset, DWORD length, BYTE *buffer)
67 {
68 #ifndef J4FS_USE_MOVI
69         DWORD nVol=0;
70         int part_id=dev_ptr->device;
71 #endif
72         int ret=-1;
73
74 #ifdef J4FS_USE_MOVI
75         mm_segment_t oldfs;
76 #endif
77
78 #if defined(J4FS_USE_XSR)
79         ret = STL_Read(nVol, part_id, offset/512, length/512, buffer);
80         if (ret != STL_SUCCESS) {
81                 T(J4FS_TRACE_ALWAYS,("%s %d: Error(offset,length,j4fs_end,nErr)=(0x%x,0x%x,0x%x,0x%x)\n",__FUNCTION__,__LINE__,offset,length,device_info.j4fs_end,ret));
82                 return J4FS_FAIL;
83         }
84 #elif defined(J4FS_USE_FSR)
85         ret = FSR_STL_Read(nVol, part_id, offset/512, length/512, buffer, FSR_STL_FLAG_DEFAULT);
86         if (ret != FSR_STL_SUCCESS) {
87                 T(J4FS_TRACE_ALWAYS,("%s %d: Error(offset,length,j4fs_end,nErr)=(0x%x,0x%x,0x%x,0x%x)\n",__FUNCTION__,__LINE__,offset,length,device_info.j4fs_end,ret));
88                 return J4FS_FAIL;
89         }
90 #elif defined(J4FS_USE_MOVI)
91         if (IS_ERR(j4fs_filp)) {
92                 printk("J4FS not available\n");
93                 return J4FS_FAIL;
94         }
95         j4fs_filp->f_flags |= O_NONBLOCK;
96         oldfs = get_fs(); set_fs(get_ds());
97         ret = j4fs_filp->f_op->llseek(j4fs_filp, offset, SEEK_SET);
98         ret = j4fs_filp->f_op->read(j4fs_filp, buffer, length, &j4fs_filp->f_pos);
99         set_fs(oldfs);
100         j4fs_filp->f_flags &= ~O_NONBLOCK;
101         if (ret < 0) {
102                 printk("j4fs_filp->read() failed: %d\n", ret);
103                 return J4FS_FAIL;
104         }
105 #else
106 'compile error'
107 #endif
108
109         return J4FS_SUCCESS;
110 }
111
112 /*
113   * This function writes length bytes of data from a specified buffer to the destination address within the device
114   * parameters
115   *   offset  : start physical page number
116   *   length  : bytes to write
117   *   buffer  : buffer to write data
118   * return value
119   *   The return code should indicate ERROR (1) or OK(0). If the return code indicates ERROR, the dev_ptr->status field should indicate
120   *   the return error code.
121   */
122 int FlashDevWrite(j4fs_device_info *dev_ptr, DWORD offset, DWORD length, BYTE *buffer)
123 {
124 #ifndef J4FS_USE_MOVI
125         DWORD nVol=0;
126         int part_id=dev_ptr->device;
127 #endif
128         int ret=-1;
129
130 #ifdef J4FS_USE_MOVI
131         mm_segment_t oldfs;
132 #endif
133
134 #if defined(J4FS_USE_XSR)
135         ret = STL_Write(nVol, part_id, offset/512, length/512, buffer);
136         if (ret != STL_SUCCESS) {
137                 T(J4FS_TRACE_ALWAYS,("%s %d: Error(offset,length,j4fs_end,nErr)=(0x%x,0x%x,0x%x,0x%x)\n",__FUNCTION__,__LINE__,offset,length,device_info.j4fs_end,ret));
138                 return J4FS_FAIL;
139         }
140 #elif defined(J4FS_USE_FSR)
141         ret = FSR_STL_Write(nVol, part_id, offset/512, length/512, buffer, FSR_STL_FLAG_DEFAULT);
142         if (ret != FSR_STL_SUCCESS) {
143                 T(J4FS_TRACE_ALWAYS,("%s %d: Error(offset,length,j4fs_end,nErr)=(0x%x,0x%x,0x%x,0x%x)\n",__FUNCTION__,__LINE__,offset,length,device_info.j4fs_end,ret));
144                 return J4FS_FAIL;
145         }
146 #elif defined(J4FS_USE_MOVI)
147         if (IS_ERR(j4fs_filp)) {
148                 printk("J4FS not available\n");
149                 return J4FS_FAIL;
150         }
151         j4fs_filp->f_flags |= O_NONBLOCK;
152         oldfs = get_fs(); set_fs(get_ds());
153         ret = j4fs_filp->f_op->llseek(j4fs_filp, offset, SEEK_SET);
154         ret = j4fs_filp->f_op->write(j4fs_filp, buffer, length, &j4fs_filp->f_pos);
155         set_fs(oldfs);
156         j4fs_filp->f_flags &= ~O_NONBLOCK;
157         if (ret < 0) {
158                 printk("j4fs_filp->write() failed: %d\n", ret);
159                 return J4FS_FAIL;
160         }
161 #else
162 'compile error'
163 #endif
164
165         return J4FS_SUCCESS;
166 }
167
168 /*
169   * In order to reuse the flash media, an erase command must be provided for the FSD. This command erases a single flash erase-block beginning
170   * at the address specified by the aux field in the DEVICE_INFO structure. The blocksize field of the DEVICE_INFO structure is used to force the aux ptr
171   * to a block boundary.
172   * parameters
173   *   dev_ptr->aux : start block address to be erased
174   */
175 int FlashDevErase(j4fs_device_info *dev_ptr)
176 {
177         // TODO
178         return J4FS_SUCCESS;
179 }
180
181 int FlashDevSpecial(j4fs_device_info *dev_ptr, DWORD scmd)
182 {
183         switch(scmd)
184         {
185                 case J4FS_INIT:
186                         // Initialize the internal FSD structures to use a device
187                         FlashDevMount();
188
189                         break;
190
191                 case J4FS_GET_INFO:
192                         break;
193
194                 case J4FS_EXIT:
195                         FlashDevUnmount();
196
197                 default:
198                         break;
199         }
200
201         return J4FS_SUCCESS;
202 }
203
204 int FlashDevMount()
205 {
206         DWORD media_status_table_size=1;                //  Media Status Table occupys 1 block
207
208 #ifdef J4FS_USE_MOVI
209         j4fs_filp = filp_open(J4FS_DEVNAME, O_RDWR|O_SYNC, 0);
210         if (IS_ERR(j4fs_filp)) {
211                 printk("FlashDevMount : filp_open() failed~!: %ld\n", PTR_ERR(j4fs_filp));
212                 return J4FS_FAIL;
213         }
214         printk("FlashDevMount : filp_open() OK....!\n");
215 #endif
216
217         device_info.device=J4FS_PARTITION_ID;
218         device_info.blocksize=PHYSICAL_BLOCK_SIZE;
219         device_info.pagesize=PHYSICAL_PAGE_SIZE;
220         device_info.j4fs_offset=media_status_table_size*device_info.blocksize;   // j4fs_offset follows the Media Status Table.
221         return J4FS_SUCCESS;
222 }
223
224 int FlashDevUnmount()
225 {
226 #ifdef J4FS_USE_MOVI
227         filp_close(j4fs_filp, NULL);
228         printk("FlashDevUnmount : filp_close() OK....!\n");
229 #endif
230
231         return J4FS_SUCCESS;
232 }