dumchar.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346
  1. /*
  2. * Copyright (C) 2015 MediaTek Inc.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define DEBUG
  14. #include <linux/module.h>
  15. #include <linux/moduleparam.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h> /* pr_debug() */
  18. #include <linux/slab.h> /* kmalloc() */
  19. #include <linux/fs.h> /* everything... filp_open */
  20. #include <linux/errno.h> /* error codes */
  21. #include <linux/types.h> /* size_t */
  22. #include <linux/proc_fs.h> /*proc */
  23. #include <linux/fcntl.h> /* O_ACCMODE */
  24. #include <linux/aio.h>
  25. #include <asm/uaccess.h> /*set_fs get_fs mm_segment_t */
  26. #include <linux/miscdevice.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/unistd.h>
  29. #include <linux/cdev.h>
  30. #include <linux/device.h>
  31. #include <linux/mtd/mtd.h>
  32. #include <generated/autoconf.h>
  33. #include <linux/sched.h> /* show_stack(current,NULL) */
  34. #include <env.h>
  35. #include "dumchar.h" /* local definitions */
  36. #ifdef CONFIG_MTK_EMMC_SUPPORT
  37. #include "pmt.h"
  38. #else
  39. #if defined(CONFIG_MTK_TLC_NAND_SUPPORT)
  40. #include "partition_define_tlc.h"
  41. #else
  42. #include "partition_define_mlc.h"
  43. #endif
  44. #endif
  45. #include <linux/mmc/host.h>
  46. /* #include <linux/mmc/sd_misc.h> */
  47. /* #include "../mmc-host/mt_sd.h" */
  48. #include <linux/genhd.h>
  49. /* #include "partition_dumchar.h" */
  50. #include <linux/of.h>
  51. #include <linux/of_address.h>
  52. #include <linux/of_irq.h>
  53. #include <linux/syscalls.h>
  54. struct device;
  55. static struct class *dumchar_class;
  56. static struct device *dumchar_device[PART_MAX_COUNT];
  57. static struct dumchar_dev *dumchar_devices; /* store all dum char info, allocated in dumchar_init */
  58. static struct proc_dir_entry *dumchar_proc_entry;
  59. static unsigned int major;
  60. int IsEmmc(void)
  61. {
  62. #ifdef CONFIG_MTK_EMMC_SUPPORT
  63. return 1;
  64. #else
  65. return 0;
  66. #endif
  67. }
  68. EXPORT_SYMBOL(IsEmmc);
  69. #ifdef CONFIG_MTK_EMMC_SUPPORT
  70. static int init_sd_cmd(struct msdc_ioctl *cmd, loff_t addr,
  71. u32 __user *buffer, int host_num, int iswrite,
  72. u32 totalsize, int transtype, Region part)
  73. {
  74. if (!cmd) {
  75. pr_debug("DumChar ERROR:no space for msdc_ioctl\n");
  76. return -EINVAL;
  77. }
  78. if (addr < 0) {
  79. pr_debug("DumChar ERROR:Wrong Address %llx for emmc!\n", addr);
  80. return -EINVAL;
  81. }
  82. if (totalsize > MAX_SD_BUFFER) {
  83. pr_debug("DumChar ERROR:too mucn bytes for msdc\n");
  84. return -EINVAL;
  85. }
  86. memset(cmd, 0, sizeof(struct msdc_ioctl));
  87. if (addr % 512 == 0)
  88. cmd->address = addr / 512;
  89. else {
  90. pr_debug("DumChar ERROR: Wrong Address\n");
  91. return -EINVAL;
  92. }
  93. /* cmd->address =0x100000; */
  94. cmd->buffer = buffer;
  95. cmd->clock_freq = 0;
  96. cmd->host_num = host_num;
  97. cmd->iswrite = iswrite;
  98. cmd->result = -1;
  99. cmd->trans_type = transtype;
  100. cmd->total_size = totalsize;
  101. /* cmd->region = part; */
  102. cmd->partition = part;
  103. /* cmdtype:MSDC_SINGLE_READ_WRITE while MAX_SD_BUFFER >totalsize >512 byte;
  104. MSDC_MULTIPLE_READ_WRITE while totalsize <=512 byte;
  105. */
  106. if (totalsize <= 512)
  107. cmd->opcode = MSDC_SINGLE_READ_WRITE;
  108. else
  109. cmd->opcode = MSDC_MULTIPLE_READ_WRITE;
  110. /*
  111. pr_debug("*****************************\nDumCharDebug:in init_sd_cmd:\n");
  112. pr_debug("cmd->opcode=%d MSDC_SINGLE_READ_WRITE =(2) MSDC_MULTIPLE_READ_WRITE =(3)\n",cmd->opcode);
  113. pr_debug("cmd->host_num=%d supose=1\n",cmd->host_num);
  114. pr_debug("cmd->iswrite=%d write=1 read=0\n",cmd->iswrite);
  115. pr_debug("cmd->trans_type=%d\n",cmd->trans_type);
  116. pr_debug("cmd->total_size=%d\n",cmd->total_size);
  117. pr_debug("cmd->address=%d\n",cmd->address);
  118. pr_debug("cmd->buffer=%p\n",cmd->buffer);
  119. pr_debug("cmd->cmd_driving=%d\n",cmd->cmd_driving);
  120. pr_debug("cmd->dat_driving=%d\n",cmd->dat_driving);
  121. pr_debug("cmd->clock_freq=%d\n",cmd->clock_freq);
  122. pr_debug("cmd->result=%d\n",cmd->result);
  123. pr_debug("***************************\n");
  124. */
  125. return 0;
  126. }
  127. #endif
  128. #ifdef CONFIG_MTK_EMMC_SUPPORT
  129. int eMMC_rw_x(loff_t addr, u32 *buffer, int host_num, int iswrite, u32 totalsize, int transtype,
  130. Region part)
  131. {
  132. struct msdc_ioctl cmd;
  133. int result = 0;
  134. if (addr < 0) {
  135. pr_debug("DumChar ERROR:Wrong Address %llx for emmc!\n", addr);
  136. return -EINVAL;
  137. }
  138. memset(&cmd, 0, sizeof(struct msdc_ioctl));
  139. if (addr % 512 == 0)
  140. cmd.address = addr / 512;
  141. else {
  142. pr_debug("DumChar ERROR: Wrong Address\n");
  143. return -EINVAL;
  144. }
  145. /* cmd->address =0x100000; */
  146. cmd.buffer = buffer;
  147. cmd.clock_freq = 0;
  148. cmd.host_num = host_num;
  149. cmd.iswrite = iswrite;
  150. cmd.result = -1;
  151. cmd.trans_type = transtype;
  152. cmd.total_size = totalsize;
  153. /* cmd.region = part; */
  154. cmd.partition = part;
  155. cmd.opcode = MSDC_CARD_DUNM_FUNC;
  156. result = simple_sd_ioctl_rw(&cmd);
  157. return result;
  158. }
  159. EXPORT_SYMBOL(eMMC_rw_x);
  160. #if defined(CONFIG_MTK_EMMC_SUPPORT)
  161. #include <linux/syscalls.h>
  162. void emmc_create_sys_symlink(struct mmc_card *card)
  163. {
  164. int i = 0;
  165. struct disk_part_iter piter;
  166. struct hd_struct *part;
  167. struct gendisk *disk;
  168. char link_target[256];
  169. char link_name[256];
  170. struct storage_info s_info = { 0 };
  171. /* emmc always in slot0 */
  172. if (!mmc_card_mmc(card)) {
  173. pr_debug("mtk-msdc %s: NOT emmc card,\n", __func__);
  174. return;
  175. }
  176. if (msdc_get_info(EMMC_CARD_BOOT, DISK_INFO, &s_info))
  177. disk = s_info.disk;
  178. else
  179. BUG();
  180. disk_part_iter_init(&piter, disk, 0);
  181. while ((part = disk_part_iter_next(&piter))) {
  182. for (i = 0; i < PART_NUM; i++) {
  183. if (PartInfo[i].partition_idx != 0
  184. && PartInfo[i].partition_idx == part->partno) {
  185. sprintf(link_target, "/dev/block/%sp%d", disk->disk_name,
  186. part->partno);
  187. sprintf(link_name, "/emmc@%s", PartInfo[i].name);
  188. pr_debug("%s: target=%s, name=%s\n", __func__,
  189. link_target, link_name);
  190. sys_symlink(link_target, link_name);
  191. break;
  192. }
  193. }
  194. }
  195. disk_part_iter_exit(&piter);
  196. }
  197. #endif /* CONFIG_MTK_EMMC_SUPPORT */
  198. #endif
  199. #ifdef CONFIG_MTK_EMMC_SUPPORT
  200. static ssize_t sd_single_read(struct file *filp, char __user *buf, size_t count, loff_t addr,
  201. Region part)
  202. {
  203. struct file_obj *fo = filp->private_data;
  204. struct msdc_ioctl cmd;
  205. ssize_t result = 0;
  206. if (init_sd_cmd(&cmd, addr, (u32 *) buf, 0, 0, count, 0, part)) {
  207. pr_debug("DumChar:init sd_cmd fail\n");
  208. return -EINVAL;
  209. }
  210. if (fo->act_filp->f_op->unlocked_ioctl)
  211. result = fo->act_filp->f_op->unlocked_ioctl(fo->act_filp, 1, (unsigned long)&cmd);
  212. else if (fo->act_filp->f_op->compat_ioctl)
  213. result = fo->act_filp->f_op->compat_ioctl(fo->act_filp, 1, (unsigned long)&cmd);
  214. if (result == 0)
  215. result = count;
  216. return result;
  217. }
  218. static ssize_t sd_read(struct file *filp, char __user *buf, size_t count, loff_t *pos,
  219. Region part)
  220. {
  221. struct file_obj *fo = filp->private_data;
  222. struct dumchar_dev *dev = dumchar_devices + fo->index;
  223. size_t total_retlen = 0;
  224. int retlen = 0;
  225. int len;
  226. loff_t addr = *pos;
  227. if (*pos - dev->start_address + count > dev->size)
  228. count = dev->size - (*pos - dev->start_address);
  229. if (!count)
  230. return 0;
  231. if (addr % ALIE_LEN != 0 || (addr + count) % ALIE_LEN != 0) {
  232. loff_t startaddr = addr;
  233. loff_t endaddr = addr + count;
  234. loff_t startaddr2, endaddr2;
  235. loff_t buflen;
  236. char *pbuf;
  237. char *pbuf2;
  238. mm_segment_t curr_fs;
  239. if (addr % ALIE_LEN != 0)
  240. startaddr = (addr / ALIE_LEN) * ALIE_LEN;
  241. if ((addr + count) % ALIE_LEN != 0)
  242. endaddr = ((addr + count) / ALIE_LEN + 1) * ALIE_LEN;
  243. buflen = endaddr - startaddr;
  244. startaddr2 = startaddr;
  245. endaddr2 = endaddr;
  246. pbuf = kmalloc(buflen, GFP_KERNEL);
  247. if (!pbuf)
  248. return -ENOMEM;
  249. memset(pbuf, 0, buflen);
  250. pbuf2 = pbuf;
  251. curr_fs = get_fs();
  252. set_fs(KERNEL_DS);
  253. while (buflen > 0) {
  254. if (buflen > MAX_SD_BUFFER)
  255. len = MAX_SD_BUFFER;
  256. else
  257. len = buflen;
  258. retlen = sd_single_read(filp, pbuf2, len, startaddr2, part);
  259. if (retlen > 0) {
  260. startaddr2 += retlen;
  261. total_retlen += retlen;
  262. buflen -= retlen;
  263. pbuf2 += retlen;
  264. pr_debug("while retlen > 0 total_retlen=%d\n", (int)total_retlen);
  265. } else
  266. break;
  267. }
  268. set_fs(curr_fs);
  269. #if defined(PrintBuff)
  270. int iter = 0;
  271. pr_debug
  272. ("******************************\nGet %d bytes from %d to %d in %s in kernel\n",
  273. (int)total_retlen, (int)startaddr, (int)endaddr, dev->dumname);
  274. for (iter = 0; iter < total_retlen; iter++) {
  275. if (iter % 16 == 0)
  276. pr_debug("\n");
  277. pr_debug(" %02x", pbuf[iter]);
  278. }
  279. pr_debug("\n********************************************************************\n");
  280. #endif
  281. if (total_retlen == (endaddr - startaddr)) {
  282. int n = copy_to_user(buf, pbuf + (addr - startaddr), count);
  283. if (n != 0)
  284. pr_debug("read fail in DumChar_sd_read\n");
  285. total_retlen = count - n;
  286. } else
  287. pr_debug("read fail DumChar_sd_read!\n");
  288. #if defined(PrintBuff)
  289. pr_debug("******************************\nGet %ld bytes from %d in %s in user:\n",
  290. count, (int)*pos, dev->dumname);
  291. for (iter = 0; iter < count; iter++) {
  292. if (iter % 16 == 0)
  293. pr_debug("\n");
  294. pr_debug(" %02x", buf[iter]);
  295. }
  296. pr_debug("\n********************************************************************\n");
  297. #endif
  298. kfree(pbuf);
  299. } else {
  300. while (count > 0) {
  301. if (count > MAX_SD_BUFFER)
  302. len = MAX_SD_BUFFER;
  303. else
  304. len = count;
  305. retlen = sd_single_read(filp, buf, len, addr, part);
  306. if (retlen > 0) {
  307. addr += retlen;
  308. total_retlen += retlen;
  309. count -= retlen;
  310. buf += retlen;
  311. } else
  312. break;
  313. }
  314. }
  315. *pos += total_retlen;
  316. return total_retlen;
  317. } /* mtd_read */
  318. #endif
  319. ssize_t dumchar_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
  320. {
  321. struct file_obj *fo = filp->private_data;
  322. struct dumchar_dev *dev = dumchar_devices + fo->index;
  323. ssize_t result = 0;
  324. #ifdef CONFIG_MTK_EMMC_SUPPORT
  325. loff_t pos = 0;
  326. #endif
  327. if (fo->act_filp == (struct file *)0xffffffff) {
  328. pr_debug
  329. ("[%s] Forbid to access %s with dumchar(/dev/%s), the %s partition is managed by filesystem!\n",
  330. __func__, dev->dumname, dev->dumname, dev->dumname);
  331. pr_debug("[dumchar_read] show_stack*************************************\n");
  332. show_stack(NULL, NULL);
  333. return -EINVAL;
  334. }
  335. if (dev->type != NAND && dev->type != EMMC) {
  336. pr_debug("DumChar:Wrong Dummy device Type %d ,it should be MTD or SDCARD!\n",
  337. dev->type);
  338. return -EINVAL;
  339. }
  340. if (dev->type == EMMC) {
  341. #ifdef CONFIG_MTK_EMMC_SUPPORT
  342. pos = *f_pos + dev->start_address;
  343. switch (dev->region) {
  344. case USER:
  345. result = vfs_read(fo->act_filp, buf, count, &pos);
  346. break;
  347. case BOOT_1:
  348. result = sd_read(filp, buf, count, &pos, BOOT_1);
  349. break;
  350. default:
  351. pr_debug("DumChar: Wrong EMMC Region\n");
  352. return -EINVAL;
  353. }
  354. fo->act_filp->f_pos = pos - dev->start_address;
  355. *f_pos = pos - dev->start_address;
  356. #endif
  357. } else {
  358. result = vfs_read(fo->act_filp, buf, count, f_pos);
  359. fo->act_filp->f_pos = *f_pos;
  360. }
  361. return result;
  362. }
  363. #ifdef CONFIG_MTK_EMMC_SUPPORT
  364. static ssize_t sd_single_write(struct file *filp, const char __user *buf, size_t count,
  365. loff_t addr, Region part)
  366. {
  367. struct file_obj *fo = filp->private_data;
  368. struct msdc_ioctl cmd;
  369. int result = 0;
  370. /*mt6573_sd0 host0 Todo: Only support host 0 */
  371. if (init_sd_cmd(&cmd, addr, (u32 *) buf, 0, 1, count, 0, part)) {
  372. pr_debug("DumChar:init sd_cmd fail\n");
  373. return -EINVAL;
  374. }
  375. if (fo->act_filp->f_op->unlocked_ioctl)
  376. result = fo->act_filp->f_op->unlocked_ioctl(fo->act_filp, 1, (unsigned long)&cmd);
  377. else if (fo->act_filp->f_op->compat_ioctl) {
  378. result = fo->act_filp->f_op->compat_ioctl(fo->act_filp, 1, (unsigned long)&cmd);
  379. if (result >= 0)
  380. result = count;
  381. else
  382. result = 0;
  383. return result;
  384. }
  385. static ssize_t sd_write(struct file *filp, const char __user *buf, size_t count, loff_t *pos,
  386. Region part)
  387. {
  388. struct file_obj *fo = filp->private_data;
  389. struct dumchar_dev *dev = dumchar_devices + fo->index;
  390. size_t total_retlen = 0;
  391. int retlen = 0;
  392. int len;
  393. loff_t addr = *pos; /* +dev->start_address; */
  394. if (*pos + count > dev->size)
  395. count = dev->size - *pos;
  396. if (!count)
  397. return 0;
  398. while (count > 0) {
  399. if (count > MAX_SD_BUFFER)
  400. len = MAX_SD_BUFFER;
  401. else
  402. len = count;
  403. retlen = sd_single_write(filp, buf, len, addr, part);
  404. if (retlen > 0) {
  405. addr += retlen;
  406. total_retlen += retlen;
  407. count -= retlen;
  408. buf += retlen;
  409. } else {
  410. return total_retlen;
  411. }
  412. }
  413. *pos += total_retlen;
  414. return total_retlen;
  415. }
  416. #endif
  417. ssize_t dumchar_write(struct file *filp, const char __user *buf, size_t count, loff_t *f_pos)
  418. {
  419. struct file_obj *fo = filp->private_data;
  420. struct dumchar_dev *dev = dumchar_devices + fo->index;
  421. ssize_t result = 0;
  422. #ifdef CONFIG_MTK_EMMC_SUPPORT
  423. loff_t pos = 0;
  424. #endif
  425. #if defined(CONFIG_MTK_COMBO_NAND_SUPPORT) || defined(CONFIG_MTK_TLC_NAND_SUPPORT)
  426. struct mtd_info *mtd;
  427. unsigned int writesize = -1;
  428. #endif
  429. if (fo->act_filp == (struct file *)0xffffffff) {
  430. pr_debug
  431. ("[%s] Forbidded to access %s with dumchar(/dev/%s), the %s partition is managed by filesystem!\n",
  432. __func__, dev->dumname, dev->dumname, dev->dumname);
  433. pr_debug("[dumchar_write] show_stack*************************************\n");
  434. show_stack(NULL, NULL);
  435. return -EINVAL;
  436. }
  437. #if defined(CONFIG_MTK_COMBO_NAND_SUPPORT) || defined(CONFIG_MTK_TLC_NAND_SUPPORT)
  438. mtd_for_each_device(mtd) {
  439. if (dev->mtd_index == mtd->index) {
  440. writesize = mtd->writesize;
  441. break;
  442. }
  443. }
  444. if (writesize < 0) {
  445. pr_debug("[dumchar_write] Get writesize fail\n");
  446. show_stack(NULL, NULL);
  447. return -EINVAL;
  448. }
  449. if ((__u32) *f_pos % (writesize) != 0) {
  450. pr_debug("[dumchar_write] Address Not alignment, write_size=%d byte, address =%lld\n",
  451. writesize, *f_pos);
  452. show_stack(NULL, NULL);
  453. return -EINVAL;
  454. }
  455. #else
  456. if (*f_pos % (WRITE_SIZE_Byte) != 0) {
  457. pr_debug("[dumchar_write] Address Not alignment, write_size=%d byte, address =%lld\n",
  458. WRITE_SIZE_Byte, *f_pos);
  459. show_stack(NULL, NULL);
  460. return -EINVAL;
  461. }
  462. #endif
  463. if (dev->type != NAND && dev->type != EMMC) {
  464. pr_debug("DumChar:Wrong Dummy device Type %d ,it should be MTD or SDCARD!\n",
  465. dev->type);
  466. return -EINVAL;
  467. }
  468. if (dev->type == EMMC) {
  469. #ifdef CONFIG_MTK_EMMC_SUPPORT
  470. pos = *f_pos + dev->start_address;
  471. switch (dev->region) {
  472. case USER:
  473. result = vfs_write(fo->act_filp, buf, count, &pos);
  474. break;
  475. case BOOT_1:
  476. result = sd_write(filp, buf, count, &pos, BOOT_1);
  477. break;
  478. default:
  479. pr_debug("DumChar: Wrong EMMC Region\n");
  480. return -EINVAL;
  481. }
  482. fo->act_filp->f_pos = pos - dev->start_address;
  483. *f_pos = pos - dev->start_address;
  484. #endif
  485. } else {
  486. result = vfs_write(fo->act_filp, buf, count, f_pos);
  487. fo->act_filp->f_pos = *f_pos;
  488. }
  489. return result;
  490. }
  491. static long dumchar_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  492. {
  493. struct file_obj *fo = filp->private_data;
  494. struct dumchar_dev *dev = dumchar_devices + fo->index;
  495. long result = 0;
  496. #ifdef CONFIG_MTK_EMMC_SUPPORT
  497. long i;
  498. struct mtd_info_user info;
  499. struct erase_info_user erase_info;
  500. void __user *argp = (void __user *)arg;
  501. u_long size;
  502. mm_segment_t curr_fs;
  503. char *ebuf;
  504. loff_t addr;
  505. __u32 count;
  506. #endif
  507. if (fo->act_filp == (struct file *)0xffffffff) {
  508. pr_debug
  509. ("[%s] Forbidded to access %s with dumchar(/dev/%s),the %s partition is managed by filesystem!\n",
  510. __func__, dev->dumname, dev->dumname, dev->dumname);
  511. pr_debug("[dumchar_loctl] show_stack*************************************\n");
  512. show_stack(NULL, NULL);
  513. return -EINVAL;
  514. }
  515. if (dev->type != NAND && dev->type != EMMC) {
  516. pr_debug("DumChar:Wrong Dummy device Type %d ,it should be MTD or SDCARD!\n",
  517. dev->type);
  518. return -EINVAL;
  519. }
  520. if (dev->type == NAND) {
  521. if (fo->act_filp->f_op->unlocked_ioctl)
  522. result = fo->act_filp->f_op->unlocked_ioctl(fo->act_filp, cmd, arg);
  523. else if (fo->act_filp->f_op->compat_ioctl)
  524. result = fo->act_filp->f_op->compat_ioctl(fo->act_filp, cmd, arg);
  525. } else {
  526. #if 0
  527. if (!strcmp(dev->dumname, "pmt")) {
  528. #ifdef CONFIG_MTK_EMMC_SUPPORT
  529. switch (cmd) {
  530. case PMT_READ:
  531. pr_debug("PMT IOCTL: PMT_READ\n");
  532. result = read_pmt(argp);
  533. break;
  534. case PMT_WRITE:
  535. pr_debug("PMT IOCTL: PMT_WRITE\n");
  536. result = write_pmt(argp);
  537. break;
  538. default:
  539. result = -EINVAL;
  540. }
  541. return result;
  542. #endif
  543. } else {
  544. #endif
  545. #ifdef CONFIG_MTK_EMMC_SUPPORT
  546. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  547. if (cmd & IOC_IN) {
  548. if (!access_ok(VERIFY_READ, argp, size))
  549. return -EFAULT;
  550. }
  551. if (cmd & IOC_OUT) {
  552. if (!access_ok(VERIFY_WRITE, argp, size))
  553. return -EFAULT;
  554. }
  555. switch (cmd) {
  556. case MEMGETREGIONCOUNT:
  557. break;
  558. case MEMGETREGIONINFO:
  559. break;
  560. case MEMGETINFO:
  561. info.type = MTD_NANDFLASH;
  562. info.flags = MTD_WRITEABLE;
  563. info.size = dev->size;
  564. info.erasesize = 128 * 1024;
  565. info.writesize = 512;
  566. info.oobsize = 0;
  567. info.padding = 0;
  568. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  569. return -EFAULT;
  570. break;
  571. case MEMERASE:
  572. if (copy_from_user
  573. (&erase_info, argp, sizeof(struct erase_info_user)))
  574. return -EFAULT;
  575. addr = (loff_t) erase_info.start + dev->start_address;
  576. count = erase_info.length;
  577. ebuf = kmalloc(MAX_SD_BUFFER, GFP_KERNEL);
  578. if (!ebuf) {
  579. pr_debug("DumChar: malloc ebuf buffer fail\n");
  580. return -ENOMEM;
  581. }
  582. memset(ebuf, 0xFF, MAX_SD_BUFFER);
  583. curr_fs = get_fs();
  584. set_fs(KERNEL_DS);
  585. switch (dev->region) {
  586. case USER:
  587. for (i = 0; i < (count + MAX_SD_BUFFER - 1) / MAX_SD_BUFFER;
  588. i++) {
  589. result =
  590. vfs_write(fo->act_filp, ebuf, MAX_SD_BUFFER,
  591. &addr);
  592. }
  593. break;
  594. case BOOT_1:
  595. for (i = 0; i < (count + MAX_SD_BUFFER - 1) / MAX_SD_BUFFER;
  596. i++) {
  597. result =
  598. sd_write(filp, ebuf, MAX_SD_BUFFER, &addr,
  599. BOOT_1);
  600. }
  601. break;
  602. default:
  603. pr_debug("DumChar: Wrong EMMC Region\n");
  604. return -EINVAL;
  605. }
  606. set_fs(curr_fs);
  607. kfree(ebuf);
  608. break;
  609. case MEMERASE64:
  610. break;
  611. case MEMWRITEOOB:
  612. break;
  613. case MEMREADOOB:
  614. break;
  615. case MEMWRITEOOB64:
  616. break;
  617. case MEMREADOOB64:
  618. break;
  619. case MEMLOCK:
  620. break;
  621. case MEMUNLOCK:
  622. break;
  623. case MEMGETOOBSEL:
  624. break;
  625. case MEMGETBADBLOCK:
  626. break;
  627. case MEMSETBADBLOCK:
  628. break;
  629. case ECCGETLAYOUT:
  630. break;
  631. case ECCGETSTATS:
  632. break;
  633. case MTDFILEMODE:
  634. break;
  635. case MTD_FILE_MODE_NORMAL:
  636. break;
  637. default:
  638. result = -EINVAL;
  639. }
  640. #endif
  641. #if 0
  642. }
  643. #endif
  644. }
  645. return result;
  646. }
  647. loff_t dumchar_llseek(struct file *filp, loff_t off, int whence)
  648. {
  649. struct file_obj *fo = filp->private_data;
  650. struct dumchar_dev *dev = dumchar_devices + fo->index;
  651. loff_t newpos;
  652. if (fo->act_filp == (struct file *)0xffffffff) {
  653. pr_debug
  654. ("[%s] Forbidded to access %s with dumchar(/dev/%s), the %s partition is managed by filesystem!\n",
  655. __func__, dev->dumname, dev->dumname, dev->dumname);
  656. pr_debug("[dumchar_llseek] show_stack*************************************\n");
  657. show_stack(NULL, NULL);
  658. return -EINVAL;
  659. }
  660. if (dev->type != NAND && dev->type != EMMC) {
  661. pr_debug("DumChar:Wrong Dummy device Type %d ,it should be MTD or SDCARD!\n",
  662. dev->type);
  663. return -EINVAL;
  664. }
  665. if (fo->act_filp->f_op->llseek) {
  666. newpos = fo->act_filp->f_op->llseek(fo->act_filp, off, whence);
  667. } else {
  668. switch (whence) {
  669. case SEEK_SET:
  670. newpos = off;
  671. break;
  672. case SEEK_CUR:
  673. newpos = fo->act_filp->f_pos + off;
  674. break;
  675. case SEEK_END:
  676. newpos = dev->size + off;
  677. break;
  678. default:
  679. return -EINVAL;
  680. }
  681. }
  682. if (newpos >= 0 && newpos <= dev->size) {
  683. fo->act_filp->f_pos = newpos;
  684. filp->f_pos = newpos;
  685. return fo->act_filp->f_pos;
  686. }
  687. return -EINVAL;
  688. }
  689. #if 0
  690. static int dumchar_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
  691. {
  692. int i, len = 0;
  693. len += sprintf(page + len, "Part_Name\tSize\tStartAddr\tType\tMapTo\n");
  694. for (i = 0; i < PART_NUM; i++) {
  695. if (dumchar_devices[i].type == NAND) {
  696. len += sprintf(page + len, "%-10s 0x%016llx 0x%016llx 1 %s\n",
  697. dumchar_devices[i].dumname,
  698. dumchar_devices[i].size, dumchar_devices[i].start_address,
  699. /* "NAND", */
  700. dumchar_devices[i].actname);
  701. } else {
  702. if (PartInfo[i].partition_idx == 0) {
  703. len +=
  704. sprintf(page + len, "%-10s 0x%016llx 0x%016llx 2 %s\n",
  705. dumchar_devices[i].dumname, dumchar_devices[i].size,
  706. dumchar_devices[i].start_address,
  707. /*"EMMC", */
  708. dumchar_devices[i].actname);
  709. } else {
  710. len +=
  711. sprintf(page + len,
  712. "%-10s 0x%016llx 0x%016llx 2 /dev/block/mmcblk0p%d\n",
  713. dumchar_devices[i].dumname, dumchar_devices[i].size,
  714. dumchar_devices[i].start_address,
  715. /* "EMMC", */
  716. PartInfo[i].partition_idx);
  717. }
  718. }
  719. }
  720. if (dumchar_devices[i].type == NAND) {
  721. len +=
  722. sprintf(page + len,
  723. "Part_Name:Partition name you should open;\nSize:size of partition\nStartAddr:Index (MTD);\nType:Type of partition(MTD=1,EMMC=2);\nMapTo:actual device you operate\n");
  724. } else {
  725. len +=
  726. sprintf(page + len,
  727. "Part_Name:Partition name you should open;\nSize:size of partition\nStartAddr:Start Address of partition;\nType:Type of partition(MTD=1,EMMC=2)\nMapTo:actual device you operate\n");
  728. }
  729. *eof = 1;
  730. return len;
  731. }
  732. #endif
  733. int dumchar_open(struct inode *inode, struct file *filp)
  734. {
  735. struct dumchar_dev *dev; /* device information */
  736. struct file_obj *fo;
  737. int i, result = -1, found = 0;
  738. fo = kmalloc(sizeof(struct file_obj), GFP_KERNEL);
  739. if (!fo)
  740. return -ENOMEM;
  741. filp->private_data = fo;
  742. for (i = 0; i < PART_NUM; i++) {
  743. if (!strcmp(filp->f_path.dentry->d_name.name, dumchar_devices[i].dumname)) {
  744. dev = &(dumchar_devices[i]);
  745. fo->index = i;
  746. found = 1;
  747. /* pr_debug("DumChar: find dev %s index=%d\n",dumchar_devices[i].dumname,i); */
  748. break;
  749. }
  750. }
  751. if (found == 0) {
  752. pr_debug(" DumChar:ERROR:No Such Dummy Device %s\n ",
  753. filp->f_path.dentry->d_name.name);
  754. return -EINVAL;
  755. }
  756. if (!strcmp(dev->dumname, "usrdata") || !strcmp(dev->dumname, "cache")
  757. || !strcmp(dev->dumname, "android") || !strcmp(dev->dumname, "fat")) {
  758. pr_debug
  759. ("[%s] Forbidded to access %s with dumchar(/dev/%s), the %s partition is managed by filesystem!\n",
  760. __func__, dev->dumname, dev->dumname, dev->dumname);
  761. pr_debug("[dumchar_open] show_stack*************************************\n");
  762. show_stack(NULL, NULL);
  763. fo->act_filp = (struct file *)0xffffffff;
  764. kfree(fo);
  765. return -EINVAL;
  766. } else {
  767. pr_debug("[dumchar_open][%s] will open %s for %s!\n", current->comm, dev->actname,
  768. filp->f_path.dentry->d_name.name);
  769. fo->act_filp = filp_open(dev->actname, filp->f_flags, 0777);
  770. if (IS_ERR(fo->act_filp)) {
  771. result = PTR_ERR(fo->act_filp);
  772. pr_debug
  773. (" DumChar: [%s] open %s failed (%s ). fo->act_filp=%p!, result=%d\n",
  774. current->comm, dev->actname, filp->f_path.dentry->d_name.name,
  775. fo->act_filp, result);
  776. pr_debug("[dumchar_open] show_stack*************************************\n");
  777. show_stack(NULL, NULL);
  778. /* pr_debug("[dumchar_open] BUG_ON*************************************\n"); */
  779. /* BUG_ON(1); */
  780. /* pr_debug("[dumchar_open] ************\n"); */
  781. goto open_fail2;
  782. } else {
  783. if (!(fo->act_filp->f_op)) {
  784. pr_debug
  785. (" DumChar:open %s failed (%s ). has no file operations registered!\n",
  786. dev->actname, filp->f_path.dentry->d_name.name);
  787. result = -EIO;
  788. goto open_fail1;
  789. }
  790. }
  791. }
  792. return 0; /* success */
  793. open_fail1:
  794. filp_close(fo->act_filp, NULL);
  795. open_fail2:
  796. fo->act_filp = NULL;
  797. kfree(fo);
  798. return result;
  799. }
  800. #ifndef CONFIG_MTK_EMMC_SUPPORT
  801. int mtd_create_symlink(void)
  802. {
  803. char *link_target;
  804. char link_name[256];
  805. /* char ahead_target[256]; */
  806. /* char ahead_link[256]; */
  807. int i;
  808. for (i = 0; i < PART_NUM; i++) {
  809. if (dumchar_devices[i].actname) {
  810. memset(link_name, 0x0, sizeof(link_name));
  811. link_target = dumchar_devices[i].actname;
  812. sprintf(link_name, "/mtd@%s", dumchar_devices[i].dumname);
  813. pr_debug("[mtd_create_symlink]: target=%s, name=%s\n", link_target,
  814. link_name);
  815. sys_symlink(link_target, link_name);
  816. }
  817. #if 0
  818. if ((!strcmp(dumchar_devices[i].dumname, "usrdata"))
  819. || (!strcmp(dumchar_devices[i].dumname, "cache"))
  820. || (!strcmp(dumchar_devices[i].dumname, "android"))) {
  821. memset(ahead_target, 0x0, sizeof(ahead_target));
  822. memset(ahead_link, 0x0, sizeof(ahead_link));
  823. sprintf(ahead_target, "/sys/block/mtdblock%d/queue/read_ahead_kb",
  824. dumchar_devices[i].mtd_index);
  825. sprintf(ahead_link, "/read_ahead_kb@%s", dumchar_devices[i].dumname);
  826. sys_symlink(ahead_target, ahead_link);
  827. pr_debug("[mtd_create_symlink]: target=%s, name=%s\n", ahead_target,
  828. ahead_link);
  829. }
  830. #endif
  831. }
  832. return 0;
  833. }
  834. #if 0
  835. int mtd_close_read_ahead(void)
  836. {
  837. char target[256];
  838. u32 i, ret, origin, value;
  839. struct file *filp;
  840. for (i = 0; i < PART_NUM; i++) {
  841. if ((!strcmp(dumchar_devices[i].dumname, "usrdata"))
  842. || (!strcmp(dumchar_devices[i].dumname, "cache"))
  843. || (!strcmp(dumchar_devices[i].dumname, "android"))) {
  844. origin = 0;
  845. value = 0;
  846. memset(target, 0x0, sizeof(target));
  847. sprintf(target, "/sys/block/mtdblock%d/queue/read_ahead_kb",
  848. dumchar_devices[i].mtd_index);
  849. filp = filp_open(target, O_RDWR, 0666);
  850. if (IS_ERR(filp)) {
  851. ret = PTR_ERR(filp);
  852. pr_debug(
  853. "[mtd_close_read_ahead]Open %s partition fail! errno=%d\n",
  854. target, ret);
  855. continue;
  856. }
  857. ret = filp->f_op->read(filp, &origin, sizeof(origin), &(filp->f_pos));
  858. if (sizeof(origin) != ret) {
  859. pr_debug("[mtd_close_read_ahead]read fail!errno=%d\n", ret);
  860. filp_close(filp, NULL);
  861. continue;
  862. }
  863. ret = filp->f_op->write(filp, &value, sizeof(value), &(filp->f_pos));
  864. if (sizeof(value) != ret) {
  865. pr_debug("[mtd_close_read_ahead]write fail!errno=%d\n", ret);
  866. filp_close(filp, NULL);
  867. continue;
  868. }
  869. filp_close(filp, NULL);
  870. pr_debug("[mtd_close_read_ahead]:update succeed!%s from %x to %x\n",
  871. target, origin, value);
  872. }
  873. }
  874. }
  875. #endif
  876. #endif
  877. int dumchar_release(struct inode *inode, struct file *filp)
  878. {
  879. struct file_obj *fo = filp->private_data;
  880. struct dumchar_dev *dev = &dumchar_devices[fo->index];
  881. if (!strcmp(dev->dumname, "usrdata") || !strcmp(dev->dumname, "cache")
  882. || !strcmp(dev->dumname, "android") || !strcmp(dev->dumname, "fat")) {
  883. pr_debug
  884. ("[%s] Forbidded to access %s with dumchar(/dev/%s), the %s partition is managed by filesystem!\n",
  885. __func__, dev->dumname, dev->dumname, dev->dumname);
  886. } else {
  887. filp_close(fo->act_filp, NULL);
  888. }
  889. kfree(fo);
  890. return 0;
  891. }
  892. const struct file_operations dumchar_fops = {
  893. .owner = THIS_MODULE,
  894. .llseek = dumchar_llseek,
  895. .read = dumchar_read,
  896. .write = dumchar_write,
  897. .unlocked_ioctl = dumchar_ioctl,
  898. .open = dumchar_open,
  899. .release = dumchar_release,
  900. };
  901. int dumchar_probe(struct platform_device *dev)
  902. {
  903. int result, i, m, l;
  904. dev_t devno;
  905. loff_t misc_addr = 0;
  906. #ifdef CONFIG_MTK_MTD_NAND
  907. struct mtd_info *mtd;
  908. int mtd_num = 0;
  909. #endif
  910. #ifdef CONFIG_MTK_EMMC_SUPPORT
  911. struct storage_info s_info = { 0 };
  912. /* struct msdc_host *host_ctl; */
  913. pr_debug("[Dumchar_probe]*******************Introduction******************\n");
  914. pr_debug("[Dumchar_probe]There are 3 address in eMMC Project: Linear, Logical and Physical Address\n");
  915. pr_debug("[Dumchar_probe]Linear Address: Used in scatter file, uboot, preloader,flash tool etc.\n");
  916. pr_debug("[Dumchar_probe]MBR linear address is fixed in eMMCComo.mk, that is same for all chips\n");
  917. pr_debug("[Dumchar_probe]Logical Address: Used in /proc/dumchar_info, mmcblk0 etc. MBR logical address is 0\n");
  918. pr_debug("[Dumchar_probe]Physical Address: Used in eMMC driver");
  919. pr_debug("MBR Physical Address = MBR Linear Address - (BOOT1 size + BOOT2 Size + RPMB Size)\n");
  920. pr_debug("[Dumchar_probe]define User_Region_Header (BOOT1 size + BOOT2 Size + RPMB Size)\n");
  921. pr_debug("[Dumchar_probe]*******************Introduction******************\n");
  922. /* host_ctl = mtk_msdc_host[0]; */
  923. /* BUG_ON(!host_ctl); */
  924. /* BUG_ON(!host_ctl->mmc); */
  925. /* BUG_ON(!host_ctl->mmc->card); */
  926. init_pmt();
  927. msdc_get_info(EMMC_CARD_BOOT, EMMC_USER_CAPACITY, &s_info);
  928. msdc_get_info(EMMC_CARD_BOOT, EMMC_RESERVE, &s_info);
  929. pr_debug("[Dumchar]MBR address %x\n", MBR_START_ADDRESS_BYTE);
  930. #endif
  931. result = alloc_chrdev_region(&devno, 0, PART_NUM, "DumChar");
  932. if (result < 0) {
  933. pr_debug("DumChar: Get chrdev region fail\n");
  934. goto fail_alloc_chrdev_region;
  935. }
  936. major = MAJOR(devno);
  937. for (i = 0; i < PART_NUM; i++) {
  938. dumchar_devices[i].dumname = PartInfo[i].name;
  939. sema_init(&(dumchar_devices[i].sem), 1);
  940. devno = MKDEV(major, i);
  941. cdev_init(&dumchar_devices[i].cdev, &dumchar_fops);
  942. dumchar_devices[i].cdev.owner = THIS_MODULE;
  943. dumchar_devices[i].cdev.ops = &dumchar_fops;
  944. result = cdev_add(&dumchar_devices[i].cdev, devno, 1);
  945. if (result) {
  946. pr_debug("DumChar: register char device dumchar fail!\n");
  947. goto fail_register_chrdev;
  948. }
  949. dumchar_devices[i].type = PartInfo[i].type;
  950. if (dumchar_devices[i].type == EMMC) {
  951. #ifdef CONFIG_MTK_EMMC_SUPPORT
  952. dumchar_devices[i].region = PartInfo[i].region;
  953. switch (dumchar_devices[i].region) {
  954. case BOOT_1:
  955. sprintf(dumchar_devices[i].actname, MSDC_RAW_DEVICE);
  956. dumchar_devices[i].start_address = PartInfo[i].start_address;
  957. break;
  958. case USER:
  959. sprintf(dumchar_devices[i].actname, "/dev/block/mmcblk0");
  960. dumchar_devices[i].start_address =
  961. PartInfo[i].start_address - MBR_START_ADDRESS_BYTE;
  962. break;
  963. default:
  964. pr_debug("Error! the emmc region is not supportted!\n");
  965. goto fail_register_chrdev;
  966. }
  967. dumchar_devices[i].size = PartInfo[i].size;
  968. if (!strcmp(PartInfo[i].name, "fat")) {
  969. dumchar_devices[i].size =
  970. s_info.emmc_user_capacity * 512 -
  971. dumchar_devices[i].start_address - s_info.emmc_reserve * 512;
  972. }
  973. #ifdef CONFIG_MTK_SHARED_SDCARD
  974. if (!strcmp(PartInfo[i].name, "usrdata")) {
  975. dumchar_devices[i].size =
  976. s_info.emmc_user_capacity * 512 -
  977. dumchar_devices[i].start_address - s_info.emmc_reserve * 512;
  978. }
  979. #endif
  980. /* if(!strcmp(PartInfo[i].name,"otp")||!strcmp(PartInfo[i].name,"bmtpool")){ */
  981. /* dumchar_devices[i].size = (PartInfo[i].start_address & 0x0000FFFF)*128*1024; */
  982. /* } */
  983. pr_debug("[Dumchar] %s start address=%llx size=%llx\n",
  984. dumchar_devices[i].dumname, dumchar_devices[i].start_address,
  985. dumchar_devices[i].size);
  986. #endif
  987. } else {
  988. #ifdef CONFIG_MTK_MTD_NAND
  989. char *mtdname = dumchar_devices[i].dumname;
  990. if (!strcmp(dumchar_devices[i].dumname, "seccfg"))
  991. mtdname = "seccnfg";
  992. if (!strcmp(dumchar_devices[i].dumname, "bootimg"))
  993. mtdname = "boot";
  994. if (!strcmp(dumchar_devices[i].dumname, "sec_ro"))
  995. mtdname = "secstatic";
  996. if (!strcmp(dumchar_devices[i].dumname, "android"))
  997. mtdname = "system";
  998. if (!strcmp(dumchar_devices[i].dumname, "usrdata"))
  999. mtdname = "userdata";
  1000. if (!strcmp(dumchar_devices[i].dumname, "misc"))
  1001. mtd_num = i;
  1002. mtd_for_each_device(mtd) {
  1003. if (!strcmp(mtd->name, mtdname)) {
  1004. dumchar_devices[i].mtd_index = mtd->index;
  1005. sprintf(dumchar_devices[i].actname, "/dev/mtd/mtd%d",
  1006. mtd->index);
  1007. dumchar_devices[i].size = mtd->size;
  1008. dumchar_devices[i].start_address =
  1009. mtd_partition_start_address(mtd);
  1010. break;
  1011. }
  1012. }
  1013. #endif
  1014. }
  1015. }
  1016. dumchar_class = class_create(THIS_MODULE, "dumchar");
  1017. for (l = 0; l < PART_NUM; l++) {
  1018. if (!strcmp(dumchar_devices[l].dumname, "otp")) {
  1019. dumchar_device[l] =
  1020. device_create(dumchar_class, NULL, MKDEV(major, l), NULL, "otp_bak");
  1021. } else {
  1022. dumchar_device[l] =
  1023. device_create(dumchar_class, NULL, MKDEV(major, l), NULL,
  1024. dumchar_devices[l].dumname);
  1025. }
  1026. if (!strcmp(dumchar_devices[l].dumname, "misc"))
  1027. misc_addr = dumchar_devices[l].start_address;
  1028. if (IS_ERR(dumchar_device[l])) {
  1029. result = PTR_ERR(dumchar_device[l]);
  1030. pr_debug("DumChar: fail in device_create name = %s minor = %d\n",
  1031. dumchar_devices[l].dumname, l);
  1032. goto fail_create_device;
  1033. }
  1034. }
  1035. #ifdef CONFIG_MTK_EMMC_SUPPORT
  1036. env_init(misc_addr);
  1037. #else
  1038. env_init(misc_addr, mtd_num);
  1039. #endif
  1040. #ifdef CONFIG_MTK_MTD_NAND
  1041. mtd_create_symlink();
  1042. #endif
  1043. return 0;
  1044. fail_create_device:
  1045. for (m = 0; m < l; m++)
  1046. device_destroy(dumchar_class, MKDEV(major, m));
  1047. class_destroy(dumchar_class);
  1048. fail_register_chrdev:
  1049. for (m = 0; m < i; m++)
  1050. cdev_del(&dumchar_devices[m].cdev);
  1051. unregister_chrdev_region(MKDEV(major, 0), PART_NUM);
  1052. fail_alloc_chrdev_region:
  1053. return result;
  1054. }
  1055. int dumchar_remove(struct platform_device *dev)
  1056. {
  1057. int i;
  1058. pr_debug("DumCharDebug: in dumchar_remove\n");
  1059. for (i = 0; i < PART_NUM; i++) {
  1060. device_destroy(dumchar_class, MKDEV(major, i));
  1061. cdev_del(&dumchar_devices[i].cdev);
  1062. }
  1063. class_destroy(dumchar_class);
  1064. unregister_chrdev_region(MKDEV(major, 0), PART_NUM);
  1065. return 0;
  1066. }
  1067. #ifdef CONFIG_MTK_MTD_NAND
  1068. static const struct of_device_id dumchar_of_ids[] = {
  1069. {.compatible = "mediatek,dummy_char",},
  1070. {}
  1071. };
  1072. #endif
  1073. static struct platform_driver dumchar_driver = {
  1074. .probe = dumchar_probe,
  1075. .remove = dumchar_remove,
  1076. .driver = {
  1077. .name = "dummy_char",
  1078. .owner = THIS_MODULE,
  1079. #ifdef CONFIG_MTK_MTD_NAND
  1080. .of_match_table = dumchar_of_ids,
  1081. #endif
  1082. },
  1083. };
  1084. static int dumchar_proc_show(struct seq_file *m, void *v)
  1085. {
  1086. int i;
  1087. seq_puts(m, "Part_Name\tSize\tStartAddr\tType\tMapTo\n");
  1088. for (i = 0; i < PART_NUM; i++) {
  1089. if (dumchar_devices[i].type == NAND) {
  1090. seq_printf(m, "%-10s 0x%016llx 0x%016llx 1 %s\n",
  1091. dumchar_devices[i].dumname,
  1092. dumchar_devices[i].size, dumchar_devices[i].start_address,
  1093. /* "NAND", */
  1094. dumchar_devices[i].actname);
  1095. } else {
  1096. if (PartInfo[i].partition_idx == 0) {
  1097. seq_printf(m, "%-10s 0x%016llx 0x%016llx 2 %s\n",
  1098. dumchar_devices[i].dumname,
  1099. dumchar_devices[i].size,
  1100. dumchar_devices[i].start_address,
  1101. /*"EMMC", */
  1102. dumchar_devices[i].actname);
  1103. } else {
  1104. seq_printf(m,
  1105. "%-10s 0x%016llx 0x%016llx 2 /dev/block/mmcblk0p%d\n",
  1106. dumchar_devices[i].dumname, dumchar_devices[i].size,
  1107. dumchar_devices[i].start_address,
  1108. /* "EMMC", */
  1109. PartInfo[i].partition_idx);
  1110. }
  1111. }
  1112. }
  1113. seq_printf(m, "%-10s 0x0000000000000000 0x0000000000000000 1\n", "bmtpool");
  1114. if (dumchar_devices[i].type == NAND) {
  1115. seq_printf(m,
  1116. "Part_Name:Partition name you should open;\nSize:size of partition\nStartAddr:Index (MTD);\nType:Type of partition(MTD=1,EMMC=2);\nMapTo:actual device you operate\n");
  1117. } else {
  1118. seq_printf(m,
  1119. "Part_Name:Partition name you should open;\nSize:size of partition\nStartAddr:Start Address of partition;\nType:Type of partition(MTD=1,EMMC=2)\nMapTo:actual device you operate\n");
  1120. }
  1121. return 0;
  1122. }
  1123. static int dumchar_proc_open(struct inode *inode, struct file *file)
  1124. {
  1125. return single_open(file, dumchar_proc_show, NULL);
  1126. }
  1127. static const struct file_operations dumchar_proc_ops = {
  1128. .open = dumchar_proc_open,
  1129. .read = seq_read,
  1130. .llseek = seq_lseek,
  1131. .release = single_release,
  1132. };
  1133. static struct platform_device dummychar_device = {
  1134. .name = "dummy_char",
  1135. .id = 0,
  1136. };
  1137. static int __init dumchar_init(void)
  1138. {
  1139. int result;
  1140. pr_debug("dumchar_int\n");
  1141. dumchar_devices = kcalloc(PART_MAX_COUNT, sizeof(struct dumchar_dev), GFP_KERNEL);
  1142. if (!dumchar_devices) {
  1143. result = -ENOMEM;
  1144. pr_debug("DumChar: malloc dumchar_dev fail\n");
  1145. goto fail_malloc;
  1146. }
  1147. memset(dumchar_devices, 0, PART_MAX_COUNT * sizeof(struct dumchar_dev));
  1148. dumchar_proc_entry =
  1149. proc_create("dumchar_info", S_IFREG | S_IRUGO, NULL, &dumchar_proc_ops);
  1150. if (dumchar_proc_entry)
  1151. pr_debug("dumchar: register /proc/dumchar_info success\n");
  1152. else {
  1153. pr_debug("dumchar: unable to register /proc/dumchar_info\n");
  1154. result = -ENOMEM;
  1155. goto fail_create_proc;
  1156. }
  1157. result = platform_driver_register(&dumchar_driver);
  1158. if (result) {
  1159. pr_debug("DUMCHAR: Can't register driver\n");
  1160. goto fail_driver_register;
  1161. }
  1162. result = platform_device_register(&dummychar_device);
  1163. pr_debug("[%s]: dummychar_device, retval=%d\n!", __func__, result);
  1164. if (result != 0)
  1165. goto fail_driver_register;
  1166. pr_debug("DumChar: init USIF Done!\n");
  1167. return 0;
  1168. fail_driver_register:
  1169. remove_proc_entry("dumchar_info", NULL);
  1170. fail_create_proc:
  1171. kfree(dumchar_devices);
  1172. fail_malloc:
  1173. return result;
  1174. }
  1175. static void __exit dumchar_cleanup(void)
  1176. {
  1177. remove_proc_entry("dumchar_info", NULL);
  1178. platform_driver_unregister(&dumchar_driver);
  1179. kfree(dumchar_devices);
  1180. }
  1181. late_initcall_sync(dumchar_init);
  1182. module_exit(dumchar_cleanup);
  1183. MODULE_LICENSE("GPL");
  1184. MODULE_DESCRIPTION("MediaTek Dummy Char Device Driver");
  1185. MODULE_AUTHOR("Kai Zhu <kai.zhu@mediatek.com>");