ccci_chrdev.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  1. #include <linux/module.h>
  2. #include <linux/wakelock.h>
  3. #include <linux/poll.h>
  4. #include <linux/uaccess.h>
  5. #include <linux/io.h>
  6. #include <linux/fs.h>
  7. #include <linux/semaphore.h>
  8. #include <ccci_chrdev.h>
  9. #include <ccci.h>
  10. /* extern unsigned int md_ex_type; */
  11. /* unsigned int push_data_fail = 0; */
  12. static struct chr_ctl_block_t *chr_ctlb[MAX_MD_NUM];
  13. static struct wake_lock chrdev_wakelock[MAX_MD_NUM];
  14. static struct wake_lock chrdev_wakelock_mdlogger[MAX_MD_NUM];
  15. char chrdev_wakelock_name[MAX_MD_NUM][32];
  16. char chrdev_wakelock_mdlog_name[MAX_MD_NUM][32];
  17. unsigned int md_img_exist[MD_IMG_MAX_CNT] = { 0 };
  18. unsigned int md_type_saving = 0;
  19. unsigned int curr_sim_mode[MAX_MD_NUM];
  20. struct ccci_dev_client *md_logger_client = NULL;
  21. static spinlock_t md_logger_lock;
  22. static unsigned int catch_more;
  23. #ifdef CONFIG_MTK_MD_SBP_CUSTOM_VALUE
  24. static unsigned int md_sbp_code;
  25. static unsigned int md_sbp_code_default;
  26. #endif /* CONFIG_MTK_MD_SBP_CUSTOM_VALUE */
  27. #ifndef CONFIG_MODEM_FIRMWARE_CIP_PATH
  28. #define CONFIG_MODEM_FIRMWARE_CIP_PATH "/custom/etc/firmware/"
  29. #endif
  30. #ifndef CONFIG_MODEM_FIRMWARE_PATH
  31. #define CONFIG_MODEM_FIRMWARE_PATH "/etc/firmware/"
  32. #endif
  33. static char *type_str[] = {[modem_invalid] = "invalid",
  34. [modem_2g] = "2g",
  35. [modem_3g] = "3g",
  36. [modem_wg] = "wg",
  37. [modem_tg] = "tg",
  38. };
  39. int scan_image_list(int md_id, char fmt[], unsigned int out_img_list[],
  40. int img_list_size)
  41. {
  42. int i;
  43. int img_num = 0;
  44. char full_path[64] = { 0 };
  45. char img_name[32] = { 0 };
  46. struct file *filp = NULL;
  47. for (i = 0; i < (sizeof(type_str) / sizeof(char *)); i++) {
  48. snprintf(img_name, 32, fmt, md_id + 1, type_str[i]);
  49. /* Find at CIP first */
  50. snprintf(full_path, 64, "%s%s", CONFIG_MODEM_FIRMWARE_CIP_PATH,
  51. img_name);
  52. CCCI_MSG_INF(md_id, "chr", "Find:%s\n", full_path);
  53. filp = filp_open(full_path, O_RDONLY, 0644);
  54. if (IS_ERR(filp)) {
  55. /* Find at default */
  56. snprintf(full_path, 64, "%s%s",
  57. CONFIG_MODEM_FIRMWARE_PATH, img_name);
  58. CCCI_MSG_INF(md_id, "chr", "Find:%s\n", full_path);
  59. filp = filp_open(full_path, O_RDONLY, 0644);
  60. if (IS_ERR(filp)) {
  61. CCCI_MSG_INF(md_id, "chr",
  62. "%s not found(%d,%d)\n", full_path,
  63. img_num, i);
  64. continue;
  65. }
  66. }
  67. /* Run here means open image success */
  68. filp_close(filp, NULL);
  69. CCCI_MSG_INF(md_id, "chr", "Image:%s found\n", full_path);
  70. if (img_num < img_list_size)
  71. out_img_list[img_num] = i;
  72. img_num++;
  73. }
  74. if (img_num < 1)
  75. CCCI_ERR_INF(md_id, "chr", "Error! - No Image found\n");
  76. return img_num;
  77. }
  78. unsigned int __weak get_sim_switch_type(void)
  79. {
  80. CCCI_MSG("%s is not implement!!! line:%d\n", __func__, __LINE__);
  81. return 0;
  82. }
  83. int ccci_misc_ipo_h_restore(int md_id)
  84. {
  85. int reset_mode = (0x5AA5 << 16);
  86. if (curr_sim_mode[md_id] != -1) {
  87. exec_ccci_kern_func(ID_SSW_SWITCH_MODE, (char *)(&reset_mode),
  88. sizeof(unsigned int));
  89. CCCI_MSG_INF(md_id, "chr", "restore sim mode to %08x\n",
  90. curr_sim_mode[md_id]);
  91. exec_ccci_kern_func(ID_SSW_SWITCH_MODE,
  92. (char *)(&curr_sim_mode[md_id]),
  93. sizeof(unsigned int));
  94. }
  95. return 0;
  96. }
  97. /* ============================================================== */
  98. /* CCCI Standard character device function */
  99. /* ============================================================== */
  100. static void ccci_client_init(struct ccci_dev_client *client, int ch, pid_t pid)
  101. {
  102. WARN_ON(client == NULL);
  103. spin_lock_init(&client->lock);
  104. atomic_set(&client->user, 1);
  105. client->pid = pid;
  106. client->ch_num = ch;
  107. INIT_LIST_HEAD(&client->dev_list);
  108. init_waitqueue_head(&client->wait_q);
  109. client->fasync = NULL;
  110. client->wakeup_waitq = 0;
  111. }
  112. static void release_client(struct ccci_dev_client *client)
  113. {
  114. unsigned long flags;
  115. struct chr_ctl_block_t *ctlb = (struct chr_ctl_block_t *) client->ctlb;
  116. WARN_ON(spin_is_locked(&client->lock) || list_empty(&client->dev_list));
  117. mutex_lock(&ctlb->chr_dev_mutex);
  118. if (client->ch_num == CCCI_MD_LOG_RX) {
  119. spin_lock_irqsave(&md_logger_lock, flags);
  120. md_logger_client = NULL;
  121. spin_unlock_irqrestore(&md_logger_lock, flags);
  122. }
  123. list_del(&client->dev_list);
  124. un_register_to_logic_ch(client->md_id, client->ch_num);
  125. kfree(client);
  126. mutex_unlock(&ctlb->chr_dev_mutex);
  127. }
  128. static inline void ccci_put_client(struct ccci_dev_client *client)
  129. {
  130. WARN_ON(client == NULL);
  131. if (atomic_dec_and_test(&client->user))
  132. release_client(client);
  133. }
  134. static void ccci_chrdev_callback(void *private)
  135. {
  136. struct logic_channel_info_t *ch_info = (struct logic_channel_info_t *) private;
  137. struct ccci_dev_client *client =
  138. (struct ccci_dev_client *)(ch_info->m_owner);
  139. client->wakeup_waitq = 1;
  140. wake_up_interruptible(&client->wait_q);
  141. if (client->ch_num != CCCI_MD_LOG_RX)
  142. wake_lock_timeout(&chrdev_wakelock[client->md_id], HZ / 2);
  143. else /* MD logger using 1s wake lock */
  144. wake_lock_timeout(&chrdev_wakelock_mdlogger[client->md_id], HZ);
  145. kill_fasync(&client->fasync, SIGIO, POLL_IN);
  146. }
  147. static struct ccci_dev_client *find_get_client(int md_id, int ch, pid_t pid)
  148. {
  149. struct ccci_dev_client *client = NULL;
  150. int ret;
  151. struct chr_ctl_block_t *ctlb = chr_ctlb[md_id];
  152. /* schedule_timeout(10*HZ); */
  153. mutex_lock(&ctlb->chr_dev_mutex);
  154. list_for_each_entry(client, &ctlb->chr_dev_list, dev_list) {
  155. if (client->ch_num == ch) {
  156. atomic_inc(&client->user);
  157. break;
  158. }
  159. }
  160. if (&client->dev_list == &ctlb->chr_dev_list) {
  161. CCCI_CHR_MSG(md_id, "Create a Client for CH%d\n", ch);
  162. client = kmalloc(sizeof(*client), GFP_KERNEL);
  163. if (client == NULL) {
  164. CCCI_MSG_INF(md_id, "chr", "kmalloc for create client fail\n");
  165. client = ERR_PTR(-ENOMEM);
  166. goto out;
  167. }
  168. ccci_client_init(client, ch, pid);
  169. client->md_id = md_id;
  170. client->ctlb = ctlb;
  171. list_add(&client->dev_list, &ctlb->chr_dev_list);
  172. ret =
  173. register_to_logic_ch(md_id, ch, ccci_chrdev_callback,
  174. client);
  175. if (ret) {
  176. /* CCCI_MSG_INF(md_id, "chr", "register ch fail: %d\n", ret); */
  177. kfree(client);
  178. client = ERR_PTR(ret);
  179. goto out;
  180. }
  181. }
  182. out:
  183. mutex_unlock(&ctlb->chr_dev_mutex);
  184. return client;
  185. }
  186. static int ccci_dev_open(struct inode *inode, struct file *file)
  187. {
  188. int minor = iminor(inode);
  189. int major = imajor(inode);
  190. int index, minor_start;
  191. int md_id;
  192. int ret = 0;
  193. struct ccci_dev_client *client = NULL;
  194. ret = get_md_id_by_dev_major(major);
  195. if (ret < 0) {
  196. CCCI_MSG("[Error]invalid md sys id: %d\n", ret);
  197. goto out;
  198. }
  199. md_id = ret;
  200. ret = get_dev_id_by_md_id(md_id, "std chr", NULL, &minor_start);
  201. if (ret < 0) {
  202. CCCI_MSG_INF(md_id, "chr", "get minor start fail(%d)\n", ret);
  203. goto out;
  204. }
  205. index = minor - minor_start;
  206. client = find_get_client(md_id, index, current->pid);
  207. CCCI_CHR_MSG(md_id, "Open by %s ch:%d\n", current->comm, index);
  208. if (IS_ERR(client)) {
  209. CCCI_MSG_INF(md_id, "chr", "find client fail\n");
  210. ret = PTR_ERR(client);
  211. goto out;
  212. }
  213. file->private_data = client;
  214. if (index == CCCI_MD_LOG_RX) {
  215. md_logger_client = client;
  216. catch_more = 0;
  217. }
  218. nonseekable_open(inode, file);
  219. out:
  220. return ret;
  221. }
  222. static int ccci_dev_release(struct inode *inode, struct file *file)
  223. {
  224. struct ccci_dev_client *client =
  225. (struct ccci_dev_client *)file->private_data;
  226. ccci_put_client(client);
  227. return 0;
  228. }
  229. static int ccci_dev_fasync(int fd, struct file *file, int on)
  230. {
  231. struct ccci_dev_client *client =
  232. (struct ccci_dev_client *)file->private_data;
  233. return fasync_helper(fd, file, on, &client->fasync);
  234. }
  235. static unsigned int ccci_dev_poll(struct file *file, poll_table *wait)
  236. {
  237. struct ccci_dev_client *client =
  238. (struct ccci_dev_client *)file->private_data;
  239. int ret = 0;
  240. struct logic_channel_info_t *ch_info;
  241. unsigned long flags;
  242. ch_info = get_logic_ch_info(client->md_id, client->ch_num);
  243. poll_wait(file, &client->wait_q, wait);
  244. spin_lock_irqsave(&client->lock, flags);
  245. if (ch_info && get_logic_ch_data_len(ch_info))
  246. ret |= POLLIN | POLLRDNORM;
  247. if ((client->ch_num == CCCI_MD_LOG_RX) && (catch_more)) {
  248. CCCI_MSG_INF(0, "chr", "add poll error\n");
  249. catch_more = 0;
  250. ret |= POLLERR;
  251. }
  252. spin_unlock_irqrestore(&client->lock, flags);
  253. return ret;
  254. }
  255. static ssize_t ccci_dev_write(struct file *file, const char __user *buf,
  256. size_t count, loff_t *ppos)
  257. {
  258. struct ccci_dev_client *client =
  259. (struct ccci_dev_client *)file->private_data;
  260. int md_id = client->md_id;
  261. int i = count / sizeof(struct ccci_msg_t);
  262. int j = 0;
  263. int ret = 0;
  264. int ch = client->ch_num;
  265. struct ccci_msg_t *buff = NULL;
  266. struct ccci_msg_t msg;
  267. WARN_ON(count % sizeof(struct ccci_msg_t));
  268. if (!buf || (i < 1)) {
  269. CCCI_MSG_INF(md_id, "chr", "count:%d, i:%d, buf:0x%x\n", count,
  270. i, ((unsigned int)buf));
  271. ret = -EINVAL;
  272. goto out;
  273. }
  274. buff = kmalloc_array(i, sizeof(struct ccci_msg_t), GFP_KERNEL);
  275. if (buff == NULL) {
  276. CCCI_MSG_INF(md_id, "chr", "kmalloc for ccci_msg_t fail\n");
  277. ret = -ENOMEM;
  278. goto out;
  279. }
  280. if (copy_from_user(buff, buf, i * sizeof(struct ccci_msg_t))) {
  281. CCCI_MSG_INF(md_id, "chr",
  282. "ccci_dev_write: copy from user fail\n");
  283. ret = -EFAULT;
  284. goto out_free;
  285. }
  286. if (lg_ch_tx_debug_enable[md_id] & (1ULL << buff->channel)) {
  287. CCCI_MSG_INF(md_id, "chr",
  288. "ccci_dev_write: PID: %d, client: %p, lg_ch: %d\n",
  289. client->pid, client, buff->channel);
  290. }
  291. for (j = 0; j < i; j++) {
  292. /* ret=ccci_write(ch,buff+j); */
  293. msg.magic = buff[j].data0;
  294. msg.id = buff[j].data1;
  295. msg.channel = ch;
  296. msg.reserved = buff[j].reserved;
  297. CCCI_CHR_MSG(md_id, "msg: %08X %08X %02d %08X\n", msg.magic,
  298. msg.id, msg.channel, msg.reserved);
  299. ret = ccci_message_send(md_id, &msg, 1);
  300. if (ret < 0) {
  301. CCCI_MSG_INF(md_id, "chr", "ccci_write fail: %d\n",
  302. ret);
  303. break;
  304. }
  305. }
  306. if (j)
  307. ret = sizeof(*buff) * j;
  308. out_free:
  309. kfree(buff);
  310. out:
  311. return ret;
  312. }
  313. static ssize_t ccci_dev_read(struct file *file, char *buf, size_t count,
  314. loff_t *ppos)
  315. {
  316. struct ccci_dev_client *client =
  317. (struct ccci_dev_client *)file->private_data;
  318. int ret = 0, i = 0;
  319. struct ccci_msg_t buff, *u_buff = (struct ccci_msg_t *) buf;
  320. int value, n;
  321. struct logic_channel_info_t *ch_info;
  322. int md_id;
  323. n = count / sizeof(struct ccci_msg_t);
  324. WARN_ON(client == NULL);
  325. WARN_ON(count % sizeof(struct ccci_msg_t));
  326. md_id = client->md_id;
  327. ch_info = get_logic_ch_info(client->md_id, client->ch_num);
  328. retry:
  329. for (; i < n; i++) {
  330. spin_lock_bh(&client->lock);
  331. ret = get_logic_ch_data(ch_info, &buff);
  332. spin_unlock_bh(&client->lock);
  333. if (ret == sizeof(struct ccci_msg_t)) {
  334. if (copy_to_user(u_buff + i, &buff, sizeof(struct ccci_msg_t))) {
  335. CCCI_MSG_INF(md_id, "chr",
  336. "read: [%d:%s]copy_to_user fail: %08X, %08X, %02d, %08X\n",
  337. client->pid, current->comm,
  338. buff.data0, buff.data1,
  339. buff.channel, buff.reserved);
  340. ret = -EFAULT;
  341. break;
  342. }
  343. if (lg_ch_rx_debug_enable[md_id] &
  344. (1ULL << buff.channel)) {
  345. CCCI_MSG_INF(md_id, "chr",
  346. "read: [%d:%s] %08X, %08X, %02d, %08X\n",
  347. client->pid, current->comm,
  348. buff.data0, buff.data1,
  349. buff.channel, buff.reserved);
  350. }
  351. } else {
  352. if (file->f_flags & O_NONBLOCK) {
  353. ret = -EAGAIN;
  354. goto out;
  355. }
  356. value =
  357. wait_event_interruptible(client->wait_q,
  358. client->wakeup_waitq);
  359. if (value == -ERESTARTSYS) {
  360. CCCI_CHR_MSG(md_id,
  361. "Interrupted syscall.signal_pend=0x%llx\n",
  362. *(long long *)current->
  363. pending.signal.sig);
  364. ret = -EINTR;
  365. goto out;
  366. } else if (value == 0) {
  367. client->wakeup_waitq = 0;
  368. if (lg_ch_rx_debug_enable[md_id] &
  369. (1ULL << buff.channel)) {
  370. CCCI_MSG_INF(md_id, "chr",
  371. "exit from schedule(%d):[%d:%s]\n",
  372. client->ch_num,
  373. current->pid,
  374. current->comm);
  375. }
  376. goto retry;
  377. }
  378. }
  379. }
  380. out:
  381. /* finish_wait(&client->wait_q,&wait); */
  382. if (i)
  383. ret = i * sizeof(struct ccci_msg_t);
  384. /* spin_unlock_bh(&client->lock); */
  385. return ret;
  386. }
  387. static long ccci_dev_ioctl(struct file *file, unsigned int cmd,
  388. unsigned long arg)
  389. {
  390. int addr, len, state, ret = 0;
  391. struct ccci_dev_client *client =
  392. (struct ccci_dev_client *)file->private_data;
  393. int md_id = client->md_id;
  394. int ch = client->ch_num;
  395. switch (cmd) {
  396. case CCCI_IOC_GET_MD_PROTOCOL_TYPE:
  397. {
  398. char md_protol[] = "AP_TST";
  399. unsigned int data_size =
  400. sizeof(md_protol) / sizeof(char);
  401. CCCI_MSG_INF(md_id, "chr",
  402. "Call CCCI_IOC_GET_MD_PROTOCOL_TYPE!\n");
  403. if (copy_to_user
  404. ((void __user *)arg, md_protol, data_size)) {
  405. CCCI_MSG_INF(md_id, "chr",
  406. "copy_to_user MD_PROTOCOL failed !!\n");
  407. return -EFAULT;
  408. }
  409. break;
  410. }
  411. case CCCI_IOC_GET_MD_STATE:
  412. state = get_curr_md_state(md_id);
  413. if (state >= 0) {
  414. /* CCCI_DBG_MSG(md_id, "chr", "MD%d state %d\n", md_id+1, state); */
  415. state += '0'; /* Make number to character */
  416. ret = put_user((unsigned int)state,
  417. (unsigned int __user *)arg);
  418. } else {
  419. CCCI_MSG_INF(md_id, "chr", "Get MD%d state fail: %d\n",
  420. md_id + 1, state);
  421. ret = state;
  422. }
  423. break;
  424. case CCCI_IOC_PCM_BASE_ADDR:
  425. if ((ch == CCCI_PCM_RX) || (ch == CCCI_PCM_TX)) {
  426. ccci_pcm_base_req(md_id, NULL, &addr, &len);
  427. /* audio used this address in ap side, so return ap view */
  428. ret = put_user((unsigned int)addr,
  429. (unsigned int __user *)arg);
  430. } else {
  431. CCCI_MSG_INF(md_id, "chr", "get PCM base fail: invalid user(%d)\n", ch);
  432. ret = -1;
  433. }
  434. break;
  435. case CCCI_IOC_PCM_LEN:
  436. if ((ch == CCCI_PCM_RX) || (ch == CCCI_PCM_TX)) {
  437. ccci_pcm_base_req(md_id, NULL, &addr, &len);
  438. ret = put_user((unsigned int)len,
  439. (unsigned int __user *)arg);
  440. } else {
  441. CCCI_MSG_INF(md_id, "chr",
  442. "get PCM len fail: invalid user(%d)\n",
  443. ch);
  444. ret = -1;
  445. }
  446. break;
  447. case CCCI_IOC_ALLOC_MD_LOG_MEM:
  448. if ((ch == CCCI_MD_LOG_RX) || (ch == CCCI_MD_LOG_TX)) {
  449. ccci_mdlog_base_req(md_id, NULL, &addr, &len);
  450. /* mdlogger send this address to md, so return md view */
  451. addr -= get_md2_ap_phy_addr_fixed();
  452. ret = addr;
  453. } else {
  454. CCCI_MSG_INF(md_id, "chr",
  455. "get MD log base fail: invalid user(%d)\n",
  456. ch);
  457. ret = -1;
  458. }
  459. break;
  460. case CCCI_IOC_MD_RESET:
  461. CCCI_MSG_INF(md_id, "chr", "MD reset ioctl(%d) called by %s\n",
  462. ch, current->comm);
  463. ret = send_md_reset_notify(md_id);
  464. break;
  465. case CCCI_IOC_FORCE_MD_ASSERT:
  466. CCCI_MSG_INF(md_id, "chr",
  467. "Force MD assert ioctl(%d) called by %s\n", ch,
  468. current->comm);
  469. ret = ccci_trigger_md_assert(md_id);
  470. break;
  471. default:
  472. CCCI_MSG_INF(md_id, "chr", "illegal IOCTL %X called by %s\n",
  473. cmd, current->comm);
  474. ret = -ENOTTY;
  475. break;
  476. }
  477. /* CCCI_DEBUG("ret=%d cmd=0x%x addr=%0x len=%d\n",ret,cmd,addr,len); */
  478. return ret;
  479. }
  480. static int ccci_dev_mmap(struct file *file, struct vm_area_struct *vma)
  481. {
  482. int pfn, len = 0;
  483. unsigned long addr;
  484. struct ccci_dev_client *client =
  485. (struct ccci_dev_client *)file->private_data;
  486. int md_id = client->md_id;
  487. /* only PCM buffer for PCM channels can be mapped */
  488. if (client->ch_num == CCCI_PCM_RX || client->ch_num == CCCI_PCM_TX) {
  489. ccci_pcm_base_req(md_id, NULL, &addr, &len);
  490. } else if (client->ch_num == CCCI_MD_LOG_RX
  491. || client->ch_num == CCCI_MD_LOG_TX) {
  492. ccci_mdlog_base_req(md_id, NULL, &addr, &len);
  493. }
  494. CCCI_CHR_MSG(md_id, "remap addr:0x%lx len:%d map-len:%lu\n", addr, len,
  495. vma->vm_end - vma->vm_start);
  496. if ((vma->vm_end - vma->vm_start) > len) {
  497. CCCI_DBG_MSG(md_id, "chr",
  498. "Get invalid mm size request from ch%d!\n",
  499. client->ch_num);
  500. return -1; /* mmap return -1 when fail */
  501. }
  502. len =
  503. (vma->vm_end - vma->vm_start) <
  504. len ? vma->vm_end - vma->vm_start : len;
  505. pfn = addr;
  506. pfn >>= PAGE_SHIFT;
  507. /* ensure that memory does not get swapped to disk */
  508. vma->vm_flags |= VM_IO;
  509. /* ensure non-cacheable */
  510. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  511. if (remap_pfn_range(vma, vma->vm_start, pfn, len, vma->vm_page_prot))
  512. return -EAGAIN;
  513. return 0;
  514. }
  515. static const struct file_operations ccci_chrdev_fops = {
  516. .owner = THIS_MODULE,
  517. .open = ccci_dev_open,
  518. .read = ccci_dev_read,
  519. .write = ccci_dev_write,
  520. .release = ccci_dev_release,
  521. .unlocked_ioctl = ccci_dev_ioctl,
  522. .fasync = ccci_dev_fasync,
  523. .poll = ccci_dev_poll,
  524. .mmap = ccci_dev_mmap,
  525. };
  526. int ccci_chrdev_init(int md_id)
  527. {
  528. int ret = 0;
  529. int major, minor;
  530. char name[16];
  531. struct chr_ctl_block_t *ctlb;
  532. ctlb = kmalloc(sizeof(struct chr_ctl_block_t), GFP_KERNEL);
  533. if (ctlb == NULL) {
  534. ret = -CCCI_ERR_GET_MEM_FAIL;
  535. goto out;
  536. }
  537. /* Init control struct */
  538. memset(ctlb, 0, sizeof(struct chr_ctl_block_t));
  539. mutex_init(&ctlb->chr_dev_mutex);
  540. ctlb->chr_dev_list.next = &ctlb->chr_dev_list;
  541. ctlb->chr_dev_list.prev = &ctlb->chr_dev_list;
  542. ctlb->md_id = md_id;
  543. ret = get_dev_id_by_md_id(md_id, "std chr", &major, &minor);
  544. if (ret < 0) {
  545. CCCI_MSG_INF(md_id, "chr", "get std chr dev id fail: %d\n",
  546. ret);
  547. ret = -1;
  548. goto out;
  549. }
  550. snprintf(name, 16, "%s%d", CCCI_DEV_NAME, md_id);
  551. if (register_chrdev_region(MKDEV(major, minor), STD_CHR_DEV_NUM, name)
  552. != 0) {
  553. CCCI_MSG_INF(md_id, "chr", "regsiter CCCI_CHRDEV fail\n");
  554. ret = -1;
  555. goto out;
  556. }
  557. cdev_init(&ctlb->ccci_chrdev, &ccci_chrdev_fops);
  558. ctlb->ccci_chrdev.owner = THIS_MODULE;
  559. ret =
  560. cdev_add(&ctlb->ccci_chrdev, MKDEV(major, minor), STD_CHR_DEV_NUM);
  561. if (ret) {
  562. CCCI_MSG_INF(md_id, "chr", "cdev_add fail\n");
  563. goto out_err0;
  564. }
  565. sprintf(chrdev_wakelock_name[md_id], "ccci%d_chr", (md_id + 1));
  566. wake_lock_init(&chrdev_wakelock[md_id], WAKE_LOCK_SUSPEND,
  567. chrdev_wakelock_name[md_id]);
  568. sprintf(chrdev_wakelock_mdlog_name[md_id], "ccci%d_chr_mdlog",
  569. (md_id + 1));
  570. wake_lock_init(&chrdev_wakelock_mdlogger[md_id], WAKE_LOCK_SUSPEND,
  571. chrdev_wakelock_mdlog_name[md_id]);
  572. spin_lock_init(&md_logger_lock);
  573. ctlb->major = major;
  574. ctlb->minor = minor;
  575. chr_ctlb[md_id] = ctlb;
  576. return ret;
  577. out_err0:
  578. unregister_chrdev_region(MKDEV(major, minor), STD_CHR_DEV_NUM);
  579. out:
  580. kfree(ctlb);
  581. return ret;
  582. }
  583. void ccci_chrdev_exit(int md_id)
  584. {
  585. if (chr_ctlb[md_id]) {
  586. unregister_chrdev_region(MKDEV
  587. (chr_ctlb[md_id]->major,
  588. chr_ctlb[md_id]->minor),
  589. CCCI_MAX_CH_NUM);
  590. cdev_del(&chr_ctlb[md_id]->ccci_chrdev);
  591. kfree(chr_ctlb[md_id]);
  592. chr_ctlb[md_id] = NULL;
  593. }
  594. wake_lock_destroy(&chrdev_wakelock[md_id]);
  595. wake_lock_destroy(&chrdev_wakelock_mdlogger[md_id]);
  596. }
  597. /* ======================================================= */
  598. /* CCCI Virtual character device function */
  599. /* ======================================================= */
  600. static struct vir_ctl_block_t *vir_chr_ctlb[MAX_MD_NUM];
  601. static void bind_system_msg_transfer(int md_id, struct ccci_vir_client_t *client)
  602. {
  603. unsigned long flags;
  604. struct vir_ctl_block_t *ctlb = vir_chr_ctlb[md_id];
  605. spin_lock_irqsave(&ctlb->bind_lock, flags);
  606. ctlb->system_msg_client = client;
  607. spin_unlock_irqrestore(&ctlb->bind_lock, flags);
  608. }
  609. static void remove_system_msg_transfer(int md_id)
  610. {
  611. unsigned long flags;
  612. struct vir_ctl_block_t *ctlb = vir_chr_ctlb[md_id];
  613. spin_lock_irqsave(&ctlb->bind_lock, flags);
  614. ctlb->system_msg_client = NULL;
  615. spin_unlock_irqrestore(&ctlb->bind_lock, flags);
  616. }
  617. void ccci_system_message(int md_id, unsigned int message, unsigned int resv)
  618. {
  619. unsigned long flags;
  620. struct vir_ctl_block_t *ctlb = vir_chr_ctlb[md_id];
  621. struct ccci_msg_t msg;
  622. struct kfifo *sys_msg_fifo;
  623. struct ccci_vir_client_t *client = ctlb->system_msg_client;
  624. msg.data0 = 0xFFFFFFFF;
  625. msg.data1 = message;
  626. msg.channel = CCCI_MONITOR_CH;
  627. msg.reserved = resv;
  628. spin_lock_irqsave(&ctlb->bind_lock, flags);
  629. if (client != NULL) {
  630. sys_msg_fifo = &client->private_fifo;
  631. if (kfifo_is_full(sys_msg_fifo)) {
  632. spin_unlock_irqrestore(&ctlb->bind_lock, flags);
  633. CCCI_MSG_INF(md_id, "chr",
  634. "send system msg fail: fifo full\n");
  635. return;
  636. }
  637. /* Push data */
  638. kfifo_in(&client->private_fifo, &msg,
  639. sizeof(struct ccci_msg_t));
  640. client->wakeup_waitq = 1;
  641. wake_up_interruptible(&client->wait_q);
  642. } else {
  643. spin_unlock_irqrestore(&ctlb->bind_lock, flags);
  644. CCCI_MSG_INF(md_id, "chr",
  645. "send sys msg fail: no bind client\n");
  646. return;
  647. }
  648. spin_unlock_irqrestore(&ctlb->bind_lock, flags);
  649. }
  650. static void ccci_vir_client_init(struct ccci_vir_client_t *client, int idx, pid_t pid)
  651. {
  652. WARN_ON(client == NULL);
  653. spin_lock_init(&client->lock);
  654. atomic_set(&client->user, 1);
  655. client->pid = pid;
  656. client->index = idx;
  657. INIT_LIST_HEAD(&client->dev_list);
  658. init_waitqueue_head(&client->wait_q);
  659. client->fasync = NULL;
  660. client->wakeup_waitq = 0;
  661. }
  662. static void release_vir_client(struct ccci_vir_client_t *client)
  663. {
  664. struct vir_ctl_block_t *ctlb = (struct vir_ctl_block_t *) client->ctlb;
  665. WARN_ON(spin_is_locked(&client->lock) || list_empty(&client->dev_list));
  666. mutex_lock(&ctlb->chr_dev_mutex);
  667. if (client->index == 0)
  668. remove_system_msg_transfer(ctlb->md_id);
  669. list_del(&client->dev_list);
  670. if (client->fifo_ready) {
  671. kfifo_free(&client->private_fifo);
  672. client->fifo_ready = 0;
  673. }
  674. kfree(client);
  675. mutex_unlock(&ctlb->chr_dev_mutex);
  676. }
  677. static inline void ccci_put_vir_client(struct ccci_vir_client_t *client)
  678. {
  679. WARN_ON(client == NULL);
  680. if (atomic_dec_and_test(&client->user))
  681. release_vir_client(client);
  682. }
  683. static struct ccci_vir_client_t *find_get_vir_client(int md_id, int idx, pid_t pid)
  684. {
  685. struct ccci_vir_client_t *client = NULL;
  686. struct vir_ctl_block_t *ctlb = vir_chr_ctlb[md_id];
  687. mutex_lock(&ctlb->chr_dev_mutex);
  688. list_for_each_entry(client, &ctlb->chr_dev_list, dev_list) {
  689. if (client->index == idx) {
  690. atomic_inc(&client->user);
  691. break;
  692. }
  693. }
  694. if (&client->dev_list == &ctlb->chr_dev_list) {
  695. CCCI_CHR_MSG(md_id, "Create a Vir Client %d\n", idx);
  696. client = kmalloc(sizeof(*client), GFP_KERNEL);
  697. if (client == NULL) {
  698. CCCI_MSG_INF(md_id, "chr",
  699. "kmalloc for create client fail\n");
  700. client = ERR_PTR(-ENOMEM);
  701. goto out;
  702. }
  703. memset(client, 0, sizeof(struct ccci_vir_client_t));
  704. if (idx == 0) {
  705. /* Vir char 0(transfer msg between md_init and ccci driver) need fifo */
  706. if (0 !=
  707. kfifo_alloc(&client->private_fifo,
  708. sizeof(struct ccci_msg_t) *
  709. CCCI_VIR_CHR_KFIFO_SIZE, GFP_KERNEL)) {
  710. CCCI_MSG_INF(md_id, "chr",
  711. "allocate kfifo fail for vir client0\n");
  712. client->fifo_ready = 0;
  713. kfree(client);
  714. client = NULL;
  715. goto out;
  716. } else {
  717. client->fifo_ready = 1;
  718. }
  719. }
  720. ccci_vir_client_init(client, idx, pid);
  721. client->md_id = md_id;
  722. client->ctlb = ctlb;
  723. list_add(&client->dev_list, &ctlb->chr_dev_list);
  724. }
  725. out:
  726. mutex_unlock(&ctlb->chr_dev_mutex);
  727. return client;
  728. }
  729. static int ccci_vir_chr_open(struct inode *inode, struct file *file)
  730. {
  731. int minor = iminor(inode);
  732. int major = imajor(inode);
  733. int index = -1, minor_start;
  734. int md_id;
  735. int ret = 0;
  736. struct ccci_vir_client_t *client = NULL;
  737. ret = get_md_id_by_dev_major(major);
  738. if (ret < 0) {
  739. CCCI_MSG("%s: get md id fail: %d\n", __func__, ret);
  740. goto out;
  741. }
  742. md_id = ret;
  743. ret = get_dev_id_by_md_id(md_id, "vir chr", NULL, &minor_start);
  744. if (ret < 0) {
  745. CCCI_MSG_INF(md_id, "chr", "%s: get dev minor id fail: %d\n",
  746. __func__, ret);
  747. goto out;
  748. }
  749. index = minor - minor_start;
  750. client = find_get_vir_client(md_id, index, current->pid);
  751. CCCI_CHR_MSG(md_id, "Vchar(ch%d) open by %s\n", index, current->comm);
  752. if (IS_ERR(client)) {
  753. CCCI_MSG_INF(md_id, "chr", "%s: find client fail\n",
  754. __func__);
  755. ret = PTR_ERR(client);
  756. goto out;
  757. }
  758. if (atomic_read(&client->user) > 1) {
  759. CCCI_MSG_INF(md_id, "chr",
  760. "%s: [Error]multi-open, not support it\n",
  761. __func__);
  762. return -EPERM;
  763. }
  764. /* CCCI_DBG_MSG(md_id, "chr", "idx:%d fifo%d\n", client->index, client->fifo_ready); */
  765. file->private_data = client;
  766. nonseekable_open(inode, file);
  767. out:
  768. if (index == 0) { /* Always using vir char 0 as MD monitor port, and bind it to system message port */
  769. bind_system_msg_transfer(md_id, client);
  770. }
  771. return ret;
  772. }
  773. static int ccci_vir_chr_release(struct inode *inode, struct file *file)
  774. {
  775. struct ccci_vir_client_t *client = (struct ccci_vir_client_t *) file->private_data;
  776. ccci_put_vir_client(client);
  777. return 0;
  778. }
  779. static int ccci_vir_chr_fasync(int fd, struct file *file, int on)
  780. {
  781. return -EACCES; /* Dummy function, not support fasync for user space */
  782. }
  783. static unsigned int ccci_vir_chr_poll(struct file *file, poll_table *wait)
  784. {
  785. return -EACCES; /* Dummy function, not support poll for user space */
  786. }
  787. static ssize_t ccci_vir_chr_write(struct file *file, const char __user *buf,
  788. size_t count, loff_t *ppos)
  789. {
  790. return -EACCES; /* Dummy function, not support write for user space */
  791. }
  792. static ssize_t ccci_vir_chr_read(struct file *file, char *buf, size_t count,
  793. loff_t *ppos)
  794. {
  795. struct ccci_vir_client_t *client = (struct ccci_vir_client_t *) file->private_data;
  796. struct ccci_msg_t buff, *u_buff = (struct ccci_msg_t *) buf;
  797. int ret = 0;
  798. int value;
  799. int md_id;
  800. WARN_ON(client == NULL);
  801. md_id = client->md_id;
  802. retry:
  803. /* Check fifo if has data */
  804. if (kfifo_is_empty(&client->private_fifo)) {
  805. ret = 0;
  806. } else {
  807. /* Pop data */
  808. ret =
  809. kfifo_out(&client->private_fifo, &buff, sizeof(struct ccci_msg_t));
  810. }
  811. if (ret == sizeof(buff)) {
  812. if (copy_to_user(u_buff, &buff, sizeof(buff))) {
  813. CCCI_MSG_INF(md_id, "chr",
  814. "%s: copy_to_user fail: %08X, %08X, %02d, %08X\n",
  815. __func__, buff.data0, buff.data1,
  816. buff.channel, buff.reserved);
  817. ret = -EFAULT;
  818. }
  819. } else {
  820. if (file->f_flags & O_NONBLOCK) {
  821. ret = -EAGAIN;
  822. goto out;
  823. }
  824. value =
  825. wait_event_interruptible(client->wait_q,
  826. client->wakeup_waitq);
  827. if (value == -ERESTARTSYS) {
  828. ret = -EINTR;
  829. goto out;
  830. } else if (value == 0) {
  831. client->wakeup_waitq = 0;
  832. goto retry;
  833. }
  834. }
  835. out:
  836. return ret;
  837. }
  838. void ccci_md_logger_notify(void)
  839. {
  840. unsigned long flags;
  841. spin_lock_irqsave(&md_logger_lock, flags);
  842. if (md_logger_client) {
  843. wake_up_interruptible(&md_logger_client->wait_q);
  844. /* MD logger using 1s wake lock */
  845. wake_lock_timeout(&chrdev_wakelock_mdlogger[md_logger_client->md_id], HZ);
  846. catch_more = 1;
  847. }
  848. spin_unlock_irqrestore(&md_logger_lock, flags);
  849. }
  850. static long ccci_vir_chr_ioctl(struct file *file, unsigned int cmd,
  851. unsigned long arg)
  852. {
  853. int addr, ret = 0;
  854. struct ccci_vir_client_t *client = (struct ccci_vir_client_t *) file->private_data;
  855. int md_id = client->md_id;
  856. int idx = client->index;
  857. unsigned int sim_mode;
  858. unsigned int sim_switch_type;
  859. unsigned int md_type;
  860. unsigned int sim_type;
  861. unsigned int enable_sim_type;
  862. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  863. unsigned int sim_id;
  864. #endif
  865. unsigned int sim_slot_cfg[3];
  866. int ccci_cfg_setting[2];
  867. int setting_num;
  868. int scanned_num = -1;
  869. switch (cmd) {
  870. case CCCI_IOC_GET_MD_PROTOCOL_TYPE:
  871. {
  872. char md_protol[] = "AP_TST";
  873. unsigned int data_size =
  874. sizeof(md_protol) / sizeof(char);
  875. CCCI_MSG_INF(md_id, "chr",
  876. "Call CCCI_IOC_GET_MD_PROTOCOL_TYPE!\n");
  877. if (copy_to_user
  878. ((void __user *)arg, md_protol, data_size)) {
  879. CCCI_MSG_INF(md_id, "chr",
  880. "copy_to_user MD_PROTOCOL failed !!\n");
  881. return -EFAULT;
  882. }
  883. break;
  884. }
  885. case CCCI_IOC_MD_RESET:
  886. CCCI_MSG_INF(md_id, "chr",
  887. "MD reset ioctl vir(%d) called by %s\n", idx,
  888. current->comm);
  889. ret = send_md_reset_notify(md_id);
  890. break;
  891. case CCCI_IOC_FORCE_MD_ASSERT:
  892. CCCI_MSG_INF(md_id, "chr",
  893. "Force MD assert ioctl(%d) called by %s\n", idx,
  894. current->comm);
  895. ret = ccci_trigger_md_assert(md_id);
  896. break;
  897. case CCCI_IOC_SEND_RUN_TIME_DATA:
  898. if (idx == 0) {
  899. ret = ccci_send_run_time_data(md_id);
  900. } else {
  901. CCCI_MSG_INF(md_id, "chr",
  902. "Set runtime by invalid user(%d) called by %s\n",
  903. idx, current->comm);
  904. ret = -1;
  905. }
  906. break;
  907. case CCCI_IOC_GET_MD_INFO:
  908. addr = is_modem_debug_ver(md_id);
  909. ret = put_user((unsigned int)addr, (unsigned int __user *)arg);
  910. break;
  911. case CCCI_IOC_GET_MD_EX_TYPE:
  912. ret = get_md_exception_type(md_id);
  913. CCCI_MSG_INF(md_id, "chr", "get modem exception type=%d\n",
  914. ret);
  915. break;
  916. case CCCI_IOC_SEND_STOP_MD_REQUEST:
  917. CCCI_MSG_INF(md_id, "chr",
  918. "stop MD request ioctl called by %s\n",
  919. current->comm);
  920. ret = send_md_stop_notify(md_id);
  921. break;
  922. case CCCI_IOC_SEND_START_MD_REQUEST:
  923. CCCI_MSG_INF(md_id, "chr",
  924. "start MD request ioctl called by %s\n",
  925. current->comm);
  926. ret = send_md_start_notify(md_id);
  927. break;
  928. case CCCI_IOC_DO_START_MD:
  929. CCCI_MSG_INF(md_id, "chr", "start MD ioctl called by %s\n",
  930. current->comm);
  931. ret = ccci_start_modem(md_id);
  932. break;
  933. case CCCI_IOC_DO_STOP_MD:
  934. CCCI_MSG_INF(md_id, "chr", "stop MD ioctl called by %s\n",
  935. current->comm);
  936. ret = ccci_stop_modem(md_id, 0);
  937. break;
  938. case CCCI_IOC_ENTER_DEEP_FLIGHT:
  939. CCCI_MSG_INF(md_id, "chr",
  940. "enter MD flight mode ioctl called by %s\n",
  941. current->comm);
  942. ret = send_enter_flight_mode_request(md_id);
  943. break;
  944. case CCCI_IOC_LEAVE_DEEP_FLIGHT:
  945. CCCI_MSG_INF(md_id, "chr",
  946. "leave MD flight mode ioctl called by %s\n",
  947. current->comm);
  948. ret = send_leave_flight_mode_request(md_id);
  949. break;
  950. case CCCI_IOC_POWER_ON_MD_REQUEST:
  951. CCCI_MSG_INF(md_id, "chr",
  952. "Power on MD request ioctl called by %s\n",
  953. current->comm);
  954. ret = send_power_on_md_request(md_id);
  955. break;
  956. case CCCI_IOC_POWER_OFF_MD_REQUEST:
  957. CCCI_MSG_INF(md_id, "chr",
  958. "Power off MD request ioctl called by %s\n",
  959. current->comm);
  960. ret = send_power_down_md_request(md_id);
  961. break;
  962. case CCCI_IOC_POWER_ON_MD:
  963. if (idx == 0) {
  964. ret = let_md_go(md_id);
  965. } else {
  966. CCCI_MSG_INF(md_id, "chr",
  967. "Power on MD by invalid user(%d) called by %s\n",
  968. idx, current->comm);
  969. ret = -1;
  970. }
  971. break;
  972. case CCCI_IOC_POWER_OFF_MD:
  973. if (idx == 0) {
  974. ret = let_md_stop(md_id, 1 * 1000); /* <<<< Fix this */
  975. } else {
  976. CCCI_MSG_INF(md_id, "chr",
  977. "Power off MD by invalid user(%d) called by %s\n",
  978. idx, current->comm);
  979. ret = -1;
  980. }
  981. break;
  982. case CCCI_IOC_SIM_SWITCH:
  983. if (copy_from_user
  984. (&sim_mode, (void __user *)arg, sizeof(unsigned int))) {
  985. CCCI_MSG_INF(md_id, "chr",
  986. "IOC_SIM_SWITCH: copy_from_user fail!\n");
  987. ret = -EFAULT;
  988. } else {
  989. /* switch_sim_mode(sim_mode); */
  990. ret = exec_ccci_kern_func(ID_SSW_SWITCH_MODE, (char *)(&sim_mode), sizeof(unsigned int));
  991. CCCI_MSG_INF(md_id, "chr", "IOC_SIM_SWITCH(%x): %d\n",
  992. sim_mode, ret);
  993. }
  994. break;
  995. case CCCI_IOC_UPDATE_SIM_SLOT_CFG:
  996. if (copy_from_user
  997. (&sim_slot_cfg, (void __user *)arg, sizeof(sim_slot_cfg))) {
  998. CCCI_MSG_INF(md_id, "chr",
  999. "CCCI_IOC_UPDATE_SIM_SLOT_CFG: copy_from_user fail!\n");
  1000. ret = -EFAULT;
  1001. } else {
  1002. CCCI_MSG_INF(md_id, "chr",
  1003. "CCCI_IOC_UPDATE_SIM_SLOT_CFG get s0:%d s1:%d en:%d\n",
  1004. sim_slot_cfg[0], sim_slot_cfg[1],
  1005. sim_slot_cfg[2]);
  1006. sim_mode =
  1007. (2 << 16) | (sim_slot_cfg[0] & 0x000000FF) |
  1008. ((sim_slot_cfg[1] << 8) & 0x0000FF00);
  1009. ret =
  1010. exec_ccci_kern_func(ID_SSW_SWITCH_MODE,
  1011. (char *)(&sim_mode),
  1012. sizeof(unsigned int));
  1013. if (ret == 0) {
  1014. curr_sim_mode[md_id] = sim_mode;
  1015. sim_mode = sim_mode & 0x0000FFFF;
  1016. sim_mode |= (1 << 24); /* SIM slot save to ccci nvram idx 1 */
  1017. if (sim_slot_cfg[2]) { /* Need save setting */
  1018. sim_mode |= (1 << 31); /* Set save nvram flag */
  1019. }
  1020. send_update_cfg_request(md_id, sim_mode);
  1021. } else {
  1022. CCCI_MSG_INF(md_id, "chr",
  1023. "CCCI_IOC_UPDATE_SIM_SLOT_CFG exec (%d)\n",
  1024. ret);
  1025. }
  1026. ret = 0;
  1027. }
  1028. break;
  1029. case CCCI_IOC_SIM_SWITCH_TYPE:
  1030. sim_switch_type = get_sim_switch_type();
  1031. ret = put_user(sim_switch_type, (unsigned int __user *)arg);
  1032. break;
  1033. case CCCI_IOC_SEND_BATTERY_INFO:
  1034. send_battery_info(md_id);
  1035. break;
  1036. #ifdef CONFIG_MTK_ICUSB_SUPPORT
  1037. case CCCI_IOC_SEND_ICUSB_NOTIFY:
  1038. if (copy_from_user
  1039. (&sim_id, (void __user *)arg, sizeof(unsigned int))) {
  1040. CCCI_MSG_INF(md_id, "chr",
  1041. "CCCI_IOC_SEND_ICUSB_NOTIFY: copy_from_user fail!\n");
  1042. ret = -EFAULT;
  1043. } else {
  1044. send_icusb_notify(md_id, sim_id);
  1045. }
  1046. break;
  1047. #endif
  1048. case CCCI_IOC_RELOAD_MD_TYPE:
  1049. if (copy_from_user
  1050. (&md_type, (void __user *)arg, sizeof(unsigned int))) {
  1051. CCCI_MSG_INF(md_id, "chr",
  1052. "IOC_RELOAD_MD_TYPE: copy_from_user fail!\n");
  1053. ret = -EFAULT;
  1054. } else {
  1055. CCCI_MSG_INF(md_id, "chr",
  1056. "IOC_RELOAD_MD_TYPE: storing md type(%d)!\n",
  1057. md_type);
  1058. set_modem_support(md_id, md_type);
  1059. ccci_set_reload_modem(md_id);
  1060. }
  1061. break;
  1062. case CCCI_IOC_GET_SIM_TYPE: /* for regional phone boot animation */
  1063. if (get_sim_type(md_id, &sim_type)) {
  1064. CCCI_MSG_INF(md_id, "chr",
  1065. "sim type may not be correct\n");
  1066. }
  1067. ret =
  1068. put_user((unsigned int)sim_type,
  1069. (unsigned int __user *)arg);
  1070. break;
  1071. case CCCI_IOC_ENABLE_GET_SIM_TYPE: /* for regional phone boot animation */
  1072. if (copy_from_user
  1073. (&enable_sim_type, (void __user *)arg,
  1074. sizeof(unsigned int))) {
  1075. CCCI_MSG_INF(md_id, "chr",
  1076. "CCCI_IOC_ENABLE_GET_SIM_TYPE: copy_from_user fail!\n");
  1077. ret = -EFAULT;
  1078. } else {
  1079. enable_get_sim_type(md_id, enable_sim_type);
  1080. }
  1081. break;
  1082. case CCCI_IOC_SET_MD_IMG_EXIST:
  1083. #if 0
  1084. if (copy_from_user
  1085. (&md_img_exist, (void __user *)arg, sizeof(md_img_exist))) {
  1086. CCCI_MSG_INF(md_id, "chr",
  1087. "CCCI_IOC_ENABLE_GET_SIM_TYPE: copy_from_user fail!\n");
  1088. ret = -EFAULT;
  1089. }
  1090. #endif
  1091. CCCI_MSG_INF(md_id, "chr",
  1092. "CCCI_IOC_ENABLE_GET_SIM_TYPE: need not set!\n");
  1093. break;
  1094. case CCCI_IOC_GET_MD_IMG_EXIST:
  1095. memset(md_img_exist, 0, sizeof(md_img_exist));
  1096. scanned_num =
  1097. scan_image_list(md_id, "modem_%d_%s_n.img", md_img_exist,
  1098. MD_IMG_MAX_CNT);
  1099. if (scanned_num < 1) {
  1100. CCCI_MSG_INF(md_id, "chr",
  1101. "CCCI_IOC_GET_MD_IMG_EXIST: scan md imfage fail!\n");
  1102. ret = -EFAULT;
  1103. } else {
  1104. if (copy_to_user
  1105. ((void __user *)arg, &md_img_exist,
  1106. sizeof(md_img_exist))) {
  1107. CCCI_MSG_INF(md_id, "chr",
  1108. "CCCI_IOC_GET_MD_IMG_EXIST: copy_to_user fail!\n");
  1109. ret = -EFAULT;
  1110. }
  1111. }
  1112. break;
  1113. case CCCI_IOC_GET_MD_TYPE:
  1114. md_type = get_modem_support(md_id);
  1115. ret =
  1116. put_user((unsigned int)md_type, (unsigned int __user *)arg);
  1117. break;
  1118. case CCCI_IOC_STORE_MD_TYPE:
  1119. CCCI_DBG_MSG(md_id, "chr",
  1120. "store md type ioctl called by %s!\n",
  1121. current->comm);
  1122. if (copy_from_user
  1123. (&md_type_saving, (void __user *)arg,
  1124. sizeof(unsigned int))) {
  1125. CCCI_MSG_INF(md_id, "chr",
  1126. "store md type fail: copy_from_user fail!\n");
  1127. ret = -EFAULT;
  1128. } else {
  1129. CCCI_MSG_INF(md_id, "chr",
  1130. "storing md type(%d) in kernel space!\n",
  1131. md_type_saving);
  1132. if (0x1 <= md_type_saving && md_type_saving <= 0x4) {
  1133. if (md_type_saving != get_modem_support(md_id))
  1134. CCCI_MSG_INF(md_id, "chr",
  1135. "Maybe Wrong: md type storing not equal with current setting!(%d %d)\n",
  1136. md_type,
  1137. get_modem_support(md_id));
  1138. /* Notify md_init daemon to store md type in nvram */
  1139. ccci_system_message(md_id,
  1140. CCCI_MD_MSG_STORE_NVRAM_MD_TYPE,
  1141. 0);
  1142. } else {
  1143. CCCI_MSG_INF(md_id, "chr",
  1144. "store md type fail: invalid md type(0x%x)\n",
  1145. md_type_saving);
  1146. }
  1147. }
  1148. break;
  1149. case CCCI_IOC_GET_MD_TYPE_SAVING:
  1150. ret = put_user(md_type_saving, (unsigned int __user *)arg);
  1151. break;
  1152. case CCCI_IOC_GET_CFG_SETTING:
  1153. setting_num = 2;
  1154. ret =
  1155. get_common_cfg_setting(md_id, ccci_cfg_setting,
  1156. &setting_num);
  1157. if (copy_to_user
  1158. ((void __user *)arg, ccci_cfg_setting,
  1159. sizeof(ccci_cfg_setting))) {
  1160. CCCI_MSG_INF(md_id, "chr",
  1161. "CCCI_IOC_GET_CFG_SETTING: copy_to_user fail\n");
  1162. ret = -EFAULT;
  1163. }
  1164. break;
  1165. #ifdef CONFIG_MTK_MD_SBP_CUSTOM_VALUE
  1166. case CCCI_IOC_GET_MD_SBP_CFG:
  1167. CCCI_MSG_INF(md_id, "chr", "SBP confg length:%d!\n",
  1168. strlen(CONFIG_MTK_MD_SBP_CUSTOM_VALUE));
  1169. if (strlen(CONFIG_MTK_MD_SBP_CUSTOM_VALUE) > 0) {
  1170. if (!md_sbp_code_default) {
  1171. int tmpret =
  1172. kstrtouint(CONFIG_MTK_MD_SBP_CUSTOM_VALUE,
  1173. 0, &md_sbp_code_default);
  1174. if (!tmpret) {
  1175. CCCI_MSG_INF(md_id, "chr",
  1176. "GET_MD_SBP_CFG: get config sbp code:%d!\n",
  1177. md_sbp_code_default);
  1178. } else {
  1179. CCCI_MSG_INF(md_id, "chr",
  1180. "GET_MD_SBP_CFG: get config sbp code fail! ret:%d, Config val:%s\n",
  1181. tmpret,
  1182. CONFIG_MTK_MD_SBP_CUSTOM_VALUE);
  1183. }
  1184. } else {
  1185. CCCI_MSG_INF(md_id, "chr",
  1186. "GET_MD_SBP_CFG: config sbp code:%d!\n",
  1187. md_sbp_code_default);
  1188. }
  1189. ret =
  1190. put_user(md_sbp_code_default,
  1191. (unsigned int __user *)arg);
  1192. } else {
  1193. ret = -ENOTTY;
  1194. }
  1195. break;
  1196. case CCCI_IOC_SET_MD_SBP_CFG:
  1197. if (copy_from_user
  1198. (&md_sbp_code, (void __user *)arg, sizeof(unsigned int))) {
  1199. CCCI_MSG_INF(md_id, "chr",
  1200. "SET_MD_SBP_CFG: copy_from_user fail!\n");
  1201. ret = -EFAULT;
  1202. } else {
  1203. CCCI_MSG_INF(md_id, "chr",
  1204. "SET_MD_SBP_CFG: set md sbp code:0x%x!\n",
  1205. md_sbp_code);
  1206. ccci_set_md_sbp(md_id, md_sbp_code);
  1207. }
  1208. break;
  1209. #endif /* CONFIG_MTK_MD_SBP_CUSTOM_VALUE */
  1210. default:
  1211. CCCI_MSG_INF(md_id, "chr", "illegal IOCTL %X called by %s\n",
  1212. cmd, current->comm);
  1213. ret = -ENOTTY;
  1214. break;
  1215. }
  1216. return ret;
  1217. }
  1218. static int ccci_vir_chr_mmap(struct file *file, struct vm_area_struct *vma)
  1219. {
  1220. return -1;
  1221. }
  1222. static const struct file_operations ccci_vir_chrdev_fops = {
  1223. .owner = THIS_MODULE,
  1224. .open = ccci_vir_chr_open,
  1225. .read = ccci_vir_chr_read,
  1226. .write = ccci_vir_chr_write,
  1227. .release = ccci_vir_chr_release,
  1228. .unlocked_ioctl = ccci_vir_chr_ioctl,
  1229. .fasync = ccci_vir_chr_fasync,
  1230. .poll = ccci_vir_chr_poll,
  1231. .mmap = ccci_vir_chr_mmap,
  1232. };
  1233. int ccci_vir_chrdev_init(int md_id)
  1234. {
  1235. int ret = 0;
  1236. int major, minor;
  1237. char name[16];
  1238. struct vir_ctl_block_t *ctlb;
  1239. curr_sim_mode[md_id] = -1;
  1240. ctlb = kmalloc(sizeof(struct vir_ctl_block_t), GFP_KERNEL);
  1241. if (ctlb == NULL) {
  1242. ret = -CCCI_ERR_GET_MEM_FAIL;
  1243. goto out;
  1244. }
  1245. /* Init control struct */
  1246. memset(ctlb, 0, sizeof(struct vir_ctl_block_t));
  1247. mutex_init(&ctlb->chr_dev_mutex);
  1248. ctlb->chr_dev_list.next = &ctlb->chr_dev_list;
  1249. ctlb->chr_dev_list.prev = &ctlb->chr_dev_list;
  1250. ctlb->md_id = md_id;
  1251. ret = get_dev_id_by_md_id(md_id, "vir chr", &major, &minor);
  1252. if (ret < 0) {
  1253. CCCI_MSG_INF(md_id, "chr", "Get ccci vir dev id fail(%d)!\n",
  1254. ret);
  1255. ret = -1;
  1256. goto out;
  1257. }
  1258. snprintf(name, 16, "vir_chr%d", md_id);
  1259. if (register_chrdev_region(MKDEV(major, minor), CCCI_MAX_VCHR_NUM, name)
  1260. != 0) {
  1261. CCCI_MSG_INF(md_id, "chr", "Regsiter CCCI_VCHRDEV failed!\n");
  1262. ret = -1;
  1263. goto out;
  1264. }
  1265. cdev_init(&ctlb->ccci_chrdev, &ccci_vir_chrdev_fops);
  1266. ctlb->ccci_chrdev.owner = THIS_MODULE;
  1267. ret =
  1268. cdev_add(&ctlb->ccci_chrdev, MKDEV(major, minor),
  1269. CCCI_MAX_VCHR_NUM);
  1270. if (ret) {
  1271. CCCI_MSG_INF(md_id, "chr", "cdev_add failed\n");
  1272. goto out_err0;
  1273. }
  1274. ctlb->major = major;
  1275. ctlb->minor = minor;
  1276. spin_lock_init(&ctlb->bind_lock);
  1277. vir_chr_ctlb[md_id] = ctlb;
  1278. return ret;
  1279. out_err0:
  1280. unregister_chrdev_region(MKDEV(major, minor), CCCI_MAX_VCHR_NUM);
  1281. out:
  1282. kfree(ctlb);
  1283. return ret;
  1284. }
  1285. void ccci_vir_chrdev_exit(int md_id)
  1286. {
  1287. if (vir_chr_ctlb[md_id]) {
  1288. unregister_chrdev_region(MKDEV
  1289. (vir_chr_ctlb[md_id]->major,
  1290. vir_chr_ctlb[md_id]->minor),
  1291. CCCI_MAX_CH_NUM);
  1292. cdev_del(&vir_chr_ctlb[md_id]->ccci_chrdev);
  1293. kfree(vir_chr_ctlb[md_id]);
  1294. vir_chr_ctlb[md_id] = NULL;
  1295. }
  1296. }