sipc4_io_device.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /* /linux/drivers/misc/modem_if_v2/sipc4_io_device.c
  2. *
  3. * Copyright (C) 2012 Samsung Electronics.
  4. *
  5. * This software is licensed under the terms of the GNU General Public
  6. * License version 2, as published by the Free Software Foundation, and
  7. * may be copied, distributed, and modified under those terms.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. */
  15. #include <linux/init.h>
  16. #include <linux/sched.h>
  17. #include <linux/fs.h>
  18. #include <linux/poll.h>
  19. #include <linux/irq.h>
  20. #include <linux/gpio.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/ip.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/ratelimit.h>
  26. #include <linux/device.h>
  27. #include <linux/export.h>
  28. #include <linux/platform_data/modem.h>
  29. //#include <linux/platform_data/modem_v2.h>
  30. #include "modem_prj.h"
  31. #include "modem_utils.h"
  32. /*
  33. * MAX_RXDATA_SIZE is used at making skb, when it called with page size
  34. * it need more bytes to allocate itself (Ex, cache byte, shared info,
  35. * padding...)
  36. * So, give restriction to allocation size below 1 page to prevent
  37. * big pages broken.
  38. */
  39. #define MAX_RXDATA_SIZE 0x0E00 /* 4 * 1024 - 512 */
  40. #define MAX_BOOTDATA_SIZE 0x4008 /* EBL package format*/
  41. #define MAX_MULTI_FMT_SIZE 0x4000 /* 16 * 1024 */
  42. #define HDLC_START 0x7F
  43. #define HDLC_END 0x7E
  44. #define SIZE_OF_HDLC_START 1
  45. #define SIZE_OF_HDLC_END 1
  46. static const char hdlc_start[1] = { HDLC_START };
  47. static const char hdlc_end[1] = { HDLC_END };
  48. static ssize_t show_waketime(struct device *dev,
  49. struct device_attribute *attr, char *buf)
  50. {
  51. unsigned int msec;
  52. char *p = buf;
  53. struct miscdevice *miscdev = dev_get_drvdata(dev);
  54. struct io_device *iod = container_of(miscdev, struct io_device,
  55. miscdev);
  56. msec = jiffies_to_msecs(iod->waketime);
  57. p += sprintf(buf, "raw waketime : %ums\n", msec);
  58. return p - buf;
  59. }
  60. static ssize_t store_waketime(struct device *dev,
  61. struct device_attribute *attr, const char *buf, size_t count)
  62. {
  63. unsigned long msec;
  64. int ret;
  65. struct miscdevice *miscdev = dev_get_drvdata(dev);
  66. struct io_device *iod = container_of(miscdev, struct io_device,
  67. miscdev);
  68. ret = strict_strtoul(buf, 10, &msec);
  69. if (ret)
  70. return count;
  71. iod->waketime = msecs_to_jiffies(msec);
  72. return count;
  73. }
  74. static struct device_attribute attr_waketime =
  75. __ATTR(waketime, S_IRUGO | S_IWUSR, show_waketime, store_waketime);
  76. static int get_header_size(struct io_device *iod)
  77. {
  78. switch (iod->format) {
  79. case IPC_FMT:
  80. return sizeof(struct fmt_hdr);
  81. case IPC_RAW:
  82. case IPC_MULTI_RAW:
  83. return sizeof(struct raw_hdr);
  84. case IPC_RFS:
  85. return sizeof(struct rfs_hdr);
  86. case IPC_BOOT:
  87. case IPC_BOOT_2:
  88. /* minimum size for transaction align */
  89. return 4;
  90. case IPC_RAMDUMP:
  91. default:
  92. return 0;
  93. }
  94. }
  95. static int get_hdlc_size(struct io_device *iod, char *buf)
  96. {
  97. struct fmt_hdr *fmt_header;
  98. struct raw_hdr *raw_header;
  99. struct rfs_hdr *rfs_header;
  100. mif_debug("buf : %02x %02x %02x (%d)\n", *buf, *(buf + 1),
  101. *(buf + 2), __LINE__);
  102. switch (iod->format) {
  103. case IPC_FMT:
  104. fmt_header = (struct fmt_hdr *)buf;
  105. if (iod->mc->mdm_data->ipc_version == SIPC_VER_42)
  106. return fmt_header->len & 0x3FFF;
  107. else
  108. return fmt_header->len;
  109. case IPC_RAW:
  110. case IPC_MULTI_RAW:
  111. raw_header = (struct raw_hdr *)buf;
  112. return raw_header->len;
  113. case IPC_RFS:
  114. rfs_header = (struct rfs_hdr *)buf;
  115. return rfs_header->len;
  116. case IPC_CMD:
  117. case IPC_BOOT:
  118. case IPC_BOOT_2:
  119. case IPC_RAMDUMP:
  120. default:
  121. return 0;
  122. }
  123. }
  124. static void *get_header(struct io_device *iod, size_t count,
  125. char *frame_header_buf)
  126. {
  127. struct fmt_hdr *fmt_h;
  128. struct raw_hdr *raw_h;
  129. struct rfs_hdr *rfs_h;
  130. switch (iod->format) {
  131. case IPC_FMT:
  132. fmt_h = (struct fmt_hdr *)frame_header_buf;
  133. fmt_h->len = count + sizeof(struct fmt_hdr);
  134. if (iod->mc->mdm_data->ipc_version == SIPC_VER_42 && iod->id==1) {
  135. fmt_h->len = fmt_h->len | 0x4000;
  136. }
  137. fmt_h->control = 0;
  138. return (void *)frame_header_buf;
  139. case IPC_RAW:
  140. case IPC_MULTI_RAW:
  141. raw_h = (struct raw_hdr *)frame_header_buf;
  142. raw_h->len = count + sizeof(struct raw_hdr);
  143. raw_h->channel = iod->id & 0x1F;
  144. raw_h->control = 0;
  145. return (void *)frame_header_buf;
  146. case IPC_RFS:
  147. rfs_h = (struct rfs_hdr *)frame_header_buf;
  148. rfs_h->len = count + sizeof(struct raw_hdr);
  149. rfs_h->id = iod->id;
  150. return (void *)frame_header_buf;
  151. case IPC_CMD:
  152. case IPC_BOOT:
  153. case IPC_BOOT_2:
  154. case IPC_RAMDUMP:
  155. default:
  156. return NULL;
  157. }
  158. }
  159. static inline int calc_padding_size(struct link_device *ld, unsigned len)
  160. {
  161. if (ld->aligned)
  162. return (4 - (len & 0x3)) & 0x3;
  163. else
  164. return 0;
  165. }
  166. static inline int rx_hdlc_head_start_check(char *buf)
  167. {
  168. /* check hdlc head and return size of start byte */
  169. return (buf[0] == HDLC_START) ? SIZE_OF_HDLC_START : -EBADMSG;
  170. }
  171. static inline int rx_hdlc_tail_check(char *buf)
  172. {
  173. /* check hdlc tail and return size of tail byte */
  174. return (buf[0] == HDLC_END) ? SIZE_OF_HDLC_END : -EBADMSG;
  175. }
  176. /* remove hdlc header and store IPC header */
  177. static int rx_hdlc_head_check(struct io_device *iod, struct link_device *ld,
  178. char *buf, unsigned rest)
  179. {
  180. struct header_data *hdr = &fragdata(iod, ld)->h_data;
  181. int head_size;
  182. int done_len = 0;
  183. int len = 0;
  184. /* first frame, remove start header 7F */
  185. if (!hdr->start) {
  186. len = rx_hdlc_head_start_check(buf);
  187. if (len < 0) {
  188. mif_err("Wrong HDLC start: 0x%x(%s)\n",
  189. *buf, iod->name);
  190. return len; /*Wrong hdlc start*/
  191. }
  192. mif_debug("check len : %d, rest : %d (%d)\n", len,
  193. rest, __LINE__);
  194. /* set the start flag of current packet */
  195. hdr->start = HDLC_START;
  196. hdr->len = 0;
  197. buf += len;
  198. done_len += len;
  199. rest -= len; /* rest, call by value */
  200. }
  201. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  202. __LINE__);
  203. /* get header size without HDLC Start size */
  204. head_size = get_header_size(iod);
  205. /* store the HDLC header to iod priv */
  206. if (hdr->len < head_size) {
  207. len = min(rest, head_size - hdr->len);
  208. memcpy(hdr->hdr + hdr->len, buf, len);
  209. hdr->len += len;
  210. done_len += len;
  211. }
  212. mif_debug("check done_len : %d, rest : %d (%d)\n", done_len,
  213. rest, __LINE__);
  214. return done_len;
  215. }
  216. static int rx_iodev_skb(struct sk_buff *skb);
  217. static int rx_hdlc_data_check(struct io_device *iod, struct link_device *ld,
  218. char *buf, unsigned rest)
  219. {
  220. struct header_data *hdr = &fragdata(iod, ld)->h_data;
  221. struct sk_buff *skb = fragdata(iod, ld)->skb_recv;
  222. int head_size = get_header_size(iod);
  223. int data_size = get_hdlc_size(iod, hdr->hdr) - head_size;
  224. int alloc_size=0;
  225. int len = 0;
  226. int done_len = 0;
  227. int rest_len = data_size - hdr->frag_len;
  228. int continue_len = fragdata(iod, ld)->realloc_offset;
  229. mif_debug("head_size : %d, data_size : %d (%d)\n", head_size,
  230. data_size, __LINE__);
  231. if (continue_len) {
  232. /* check the HDLC header*/
  233. if (rx_hdlc_head_start_check(buf) == SIZE_OF_HDLC_START) {
  234. rest_len -= (head_size + SIZE_OF_HDLC_START);
  235. continue_len += (head_size + SIZE_OF_HDLC_START);
  236. }
  237. buf += continue_len;
  238. rest -= continue_len;
  239. done_len += continue_len;
  240. fragdata(iod, ld)->realloc_offset = 0;
  241. mif_debug("realloc_offset = %d\n", continue_len);
  242. }
  243. /* first payload data - alloc skb */
  244. if (!skb) {
  245. /* make skb data size under MAX_RXDATA_SIZE */
  246. alloc_size = min(data_size, MAX_RXDATA_SIZE);
  247. alloc_size = min(alloc_size, rest_len);
  248. /* exceptional case for RFS channel
  249. * make skb for header info first
  250. */
  251. if (iod->format == IPC_RFS && !hdr->frag_len) {
  252. skb = rx_alloc_skb(head_size, iod, ld);
  253. if (unlikely(!skb))
  254. return -ENOMEM;
  255. memcpy(skb_put(skb, head_size), hdr->hdr, head_size);
  256. rx_iodev_skb(skb);
  257. }
  258. /* allocate first packet for data, when its size exceed
  259. * MAX_RXDATA_SIZE, this packet will split to
  260. * multiple packets
  261. */
  262. skb = rx_alloc_skb(alloc_size, iod, ld);
  263. if (unlikely(!skb)) {
  264. fragdata(iod, ld)->realloc_offset = continue_len;
  265. return -ENOMEM;
  266. }
  267. fragdata(iod, ld)->skb_recv = skb;
  268. }
  269. while (rest) {
  270. /* copy length cannot exceed rest_len */
  271. len = min_t(int, rest_len, rest);
  272. /* copy length should be under skb tailroom size */
  273. len = min(len, skb_tailroom(skb));
  274. /* when skb tailroom is bigger than MAX_RXDATA_SIZE
  275. * restrict its size to MAX_RXDATA_SIZE just for convinience */
  276. len = min(len, MAX_RXDATA_SIZE);
  277. /* copy bytes to skb */
  278. memcpy(skb_put(skb, len), buf, len);
  279. /* adjusting variables */
  280. buf += len;
  281. rest -= len;
  282. done_len += len;
  283. rest_len -= len;
  284. hdr->frag_len += len;
  285. /* check if it is final for this packet sequence */
  286. if (!rest_len || !rest)
  287. break;
  288. /* more bytes are remain for this packet sequence
  289. * pass fully loaded skb to rx queue
  290. * and allocate another skb for continues data recv chain
  291. */
  292. rx_iodev_skb(skb);
  293. fragdata(iod, ld)->skb_recv = NULL;
  294. alloc_size = min(rest_len, MAX_RXDATA_SIZE);
  295. skb = rx_alloc_skb(alloc_size, iod, ld);
  296. if (unlikely(!skb)) {
  297. fragdata(iod, ld)->realloc_offset = done_len;
  298. return -ENOMEM;
  299. }
  300. fragdata(iod, ld)->skb_recv = skb;
  301. }
  302. mif_debug("rest : %d, alloc_size : %d , len : %d (%d)\n",
  303. rest, alloc_size, skb->len, __LINE__);
  304. return done_len;
  305. }
  306. static int rx_multipdp(struct sk_buff *skb)
  307. {
  308. int err = 0;
  309. struct io_device *iod = skbpriv(skb)->real_iod;
  310. struct net_device *ndev = NULL;
  311. struct iphdr *ip_header = NULL;
  312. struct ethhdr *ehdr = NULL;
  313. const char source[ETH_ALEN] = SOURCE_MAC_ADDR;
  314. switch (iod->io_typ) {
  315. case IODEV_MISC:
  316. mif_debug("<%s> sk_rx_q.qlen = %d\n",
  317. iod->name, iod->sk_rx_q.qlen);
  318. skb_queue_tail(&iod->sk_rx_q, skb);
  319. wake_up(&iod->wq);
  320. return 0;
  321. case IODEV_NET:
  322. ndev = iod->ndev;
  323. if (!ndev)
  324. return NET_RX_DROP;
  325. skb->dev = ndev;
  326. ndev->stats.rx_packets++;
  327. ndev->stats.rx_bytes += skb->len;
  328. /* check the version of IP */
  329. ip_header = (struct iphdr *)skb->data;
  330. if (ip_header->version == IP6VERSION)
  331. skb->protocol = htons(ETH_P_IPV6);
  332. else
  333. skb->protocol = htons(ETH_P_IP);
  334. if (iod->use_handover) {
  335. skb_push(skb, sizeof(struct ethhdr));
  336. ehdr = (void *)skb->data;
  337. memcpy(ehdr->h_dest, ndev->dev_addr, ETH_ALEN);
  338. memcpy(ehdr->h_source, source, ETH_ALEN);
  339. ehdr->h_proto = skb->protocol;
  340. skb->ip_summed = CHECKSUM_UNNECESSARY;
  341. skb_reset_mac_header(skb);
  342. skb_pull(skb, sizeof(struct ethhdr));
  343. }
  344. if (in_irq())
  345. err = netif_rx(skb);
  346. else
  347. err = netif_rx_ni(skb);
  348. if (err != NET_RX_SUCCESS)
  349. mif_err("ERR: <%s> netif_rx fail (err %d)\n",
  350. iod->name, err);
  351. return err;
  352. default:
  353. mif_err("wrong io_type : %d\n", iod->io_typ);
  354. return -EINVAL;
  355. }
  356. }
  357. /* handling modem intiated loopback packet
  358. * packet path: Modem -> LINK -> IOD -> LINK -> Modem
  359. */
  360. static int rx_data_loopback(struct sk_buff *skb, struct io_device *iod,
  361. struct link_device *ld)
  362. {
  363. int headroom, tailroom;
  364. struct sk_buff *skb_new;
  365. struct raw_hdr raw_header;
  366. mif_debug("CP LB DATA Received: size=%d\n", skb->len);
  367. headroom = sizeof(raw_header) + SIZE_OF_HDLC_START;
  368. tailroom = SIZE_OF_HDLC_END;
  369. if (unlikely(skb_headroom(skb) < headroom) ||
  370. unlikely(skb_tailroom(skb) < tailroom)) {
  371. skb_new = skb_copy_expand(skb, headroom, tailroom, GFP_ATOMIC);
  372. /* skb_copy_expand success or not, free old skb from caller */
  373. dev_kfree_skb_any(skb);
  374. if (!skb_new)
  375. return -ENOMEM;
  376. } else
  377. skb_new = skb;
  378. /* mark loopback header */
  379. raw_header.len = skb_new->len + sizeof(raw_header);
  380. raw_header.channel = CP_LOOPBACK_CHANNEL;
  381. raw_header.control = 0x03;
  382. /* fill header data and HDLC framing */
  383. memcpy(skb_push(skb_new, sizeof(raw_header)), &raw_header,
  384. sizeof(raw_header));
  385. memcpy(skb_push(skb_new, SIZE_OF_HDLC_START), hdlc_start,
  386. SIZE_OF_HDLC_START);
  387. memcpy(skb_put(skb_new, SIZE_OF_HDLC_END), hdlc_end, SIZE_OF_HDLC_END);
  388. skbpriv(skb_new)->iod = iod;
  389. skbpriv(skb_new)->ld = ld;
  390. ld->send(ld, iod, skb_new);
  391. return 0;
  392. }
  393. static int rx_multi_fmt_frame(struct sk_buff *rx_skb)
  394. {
  395. struct io_device *iod = skbpriv(rx_skb)->iod;
  396. struct link_device *ld = skbpriv(rx_skb)->ld;
  397. struct fmt_hdr *fh =
  398. (struct fmt_hdr *)fragdata(iod, ld)->h_data.hdr;
  399. unsigned int id = fh->control & 0x7F;
  400. struct sk_buff *skb = iod->skb[id];
  401. unsigned char *data = fragdata(iod, ld)->skb_recv->data;
  402. unsigned int rcvd = fragdata(iod, ld)->skb_recv->len;
  403. if (!skb) {
  404. /* If there has been no multiple frame with this ID */
  405. if (!(fh->control & 0x80)) {
  406. /* It is a single frame because the "more" bit is 0. */
  407. #if 0
  408. mif_err("\n<%s> Rx FMT frame (len %d)\n",
  409. iod->name, rcvd);
  410. print_sipc4_fmt_frame(data);
  411. mif_err("\n");
  412. #endif
  413. skb_queue_tail(&iod->sk_rx_q,
  414. fragdata(iod, ld)->skb_recv);
  415. mif_debug("wake up wq of %s\n", iod->name);
  416. wake_up(&iod->wq);
  417. return 0;
  418. } else {
  419. struct fmt_hdr *fh = NULL;
  420. skb = rx_alloc_skb(MAX_MULTI_FMT_SIZE, iod, ld);
  421. if (!skb) {
  422. mif_err("<%d> alloc_skb fail\n",
  423. __LINE__);
  424. return -ENOMEM;
  425. }
  426. iod->skb[id] = skb;
  427. fh = (struct fmt_hdr *)data;
  428. mif_info("Start multi-frame (ID %d, len %d)",
  429. id, fh->len);
  430. }
  431. }
  432. /* Start multi-frame processing */
  433. memcpy(skb_put(skb, rcvd), data, rcvd);
  434. dev_kfree_skb_any(fragdata(iod, ld)->skb_recv);
  435. if (fh->control & 0x80) {
  436. /* The last frame has not arrived yet. */
  437. mif_info("Receiving (ID %d, %d bytes)\n",
  438. id, skb->len);
  439. } else {
  440. /* It is the last frame because the "more" bit is 0. */
  441. mif_info("The Last (ID %d, %d bytes received)\n",
  442. id, skb->len);
  443. #if 0
  444. mif_err("\n<%s> Rx FMT frame (len %d)\n",
  445. iod->name, skb->len);
  446. print_sipc4_fmt_frame(skb->data);
  447. mif_err("\n");
  448. #endif
  449. skb_queue_tail(&iod->sk_rx_q, skb);
  450. iod->skb[id] = NULL;
  451. mif_info("wake up wq of %s\n", iod->name);
  452. wake_up(&iod->wq);
  453. }
  454. return 0;
  455. }
  456. static int rx_multi_fmt_frame_sipc42(struct sk_buff *rx_skb)
  457. {
  458. struct io_device *iod = skbpriv(rx_skb)->iod;
  459. struct link_device *ld = skbpriv(rx_skb)->ld;
  460. struct fmt_hdr *fh =
  461. (struct fmt_hdr *)fragdata(iod, ld)->h_data.hdr;
  462. unsigned int id = fh->control & 0x7F;
  463. struct sk_buff *skb = iod->skb[id];
  464. unsigned char *data = fragdata(iod, ld)->skb_recv->data;
  465. unsigned int rcvd = fragdata(iod, ld)->skb_recv->len;
  466. u8 ch;
  467. struct io_device *real_iod = NULL;
  468. ch = (fh->len & 0xC000) >> 14;
  469. fh->len = fh->len & 0x3FFF;
  470. real_iod = ld->fmt_iods[ch];
  471. if (!real_iod) {
  472. mif_err("wrong channel %d\n", ch);
  473. return -1;
  474. }
  475. skbpriv(rx_skb)->real_iod = real_iod;
  476. if (!skb) {
  477. /* If there has been no multiple frame with this ID */
  478. if (!(fh->control & 0x80)) {
  479. /* It is a single frame because the "more" bit is 0. */
  480. #if 0
  481. mif_err("\n<%s> Rx FMT frame (len %d)\n",
  482. iod->name, rcvd);
  483. print_sipc4_fmt_frame(data);
  484. mif_err("\n");
  485. #endif
  486. skb_queue_tail(&real_iod->sk_rx_q,
  487. fragdata(iod, ld)->skb_recv);
  488. mif_debug("wake up wq of %s\n", iod->name);
  489. wake_up(&real_iod->wq);
  490. return 0;
  491. } else {
  492. struct fmt_hdr *fh = NULL;
  493. skb = rx_alloc_skb(MAX_MULTI_FMT_SIZE, real_iod, ld);
  494. if (!skb) {
  495. mif_err("alloc_skb fail\n");
  496. return -ENOMEM;
  497. }
  498. real_iod->skb[id] = skb;
  499. fh = (struct fmt_hdr *)data;
  500. mif_err("Start multi-frame (ID %d, len %d)",
  501. id, fh->len);
  502. }
  503. }
  504. /* Start multi-frame processing */
  505. memcpy(skb_put(skb, rcvd), data, rcvd);
  506. dev_kfree_skb_any(fragdata(real_iod, ld)->skb_recv);
  507. if (fh->control & 0x80) {
  508. /* The last frame has not arrived yet. */
  509. mif_err("Receiving (ID %d, %d bytes)\n",
  510. id, skb->len);
  511. } else {
  512. /* It is the last frame because the "more" bit is 0. */
  513. mif_err("The Last (ID %d, %d bytes received)\n",
  514. id, skb->len);
  515. #if 0
  516. mif_err("\n<%s> Rx FMT frame (len %d)\n",
  517. iod->name, skb->len);
  518. print_sipc4_fmt_frame(skb->data);
  519. mif_err("\n");
  520. #endif
  521. skb_queue_tail(&real_iod->sk_rx_q, skb);
  522. real_iod->skb[id] = NULL;
  523. mif_info("wake up wq of %s\n", real_iod->name);
  524. wake_up(&real_iod->wq);
  525. }
  526. return 0;
  527. }
  528. /* de-mux function draft */
  529. static int rx_iodev_skb(struct sk_buff *skb)
  530. {
  531. u8 ch;
  532. struct io_device *iod = skbpriv(skb)->iod;
  533. struct io_device *real_iod = NULL;
  534. struct link_device *ld = skbpriv(skb)->ld;
  535. struct raw_hdr *raw_header;
  536. #ifdef CONFIG_LINK_DEVICE_SPI_RFS_DEBUG
  537. struct rfs_hdr *rfs_header;
  538. #endif
  539. switch (iod->format) {
  540. case IPC_RAW:
  541. case IPC_MULTI_RAW:
  542. raw_header = (struct raw_hdr *)fragdata(iod, ld)->h_data.hdr;
  543. ch = raw_header->channel;
  544. if (ch == CP_LOOPBACK_CHANNEL)
  545. return rx_data_loopback(skb, iod, ld);
  546. real_iod = link_get_iod_with_channel(ld, 0x20 | ch);
  547. if (!real_iod) {
  548. mif_err("wrong channel %d\n", ch);
  549. return -1;
  550. }
  551. skbpriv(skb)->real_iod = real_iod;
  552. return rx_multipdp(skb);
  553. case IPC_CMD:
  554. case IPC_FMT:
  555. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  556. mif_info("\n<%s> Rx FMT frame (len %d)\n",
  557. iod->name, fragdata(iod, ld)->skb_recv->len);
  558. print_sipc4_fmt_frame(fragdata(iod, ld)->skb_recv->data);
  559. mif_info("\n");
  560. #endif
  561. if (iod->mc->mdm_data->ipc_version == SIPC_VER_42)
  562. return rx_multi_fmt_frame_sipc42(skb);
  563. else
  564. return rx_multi_fmt_frame(skb);
  565. case IPC_RFS:
  566. #ifdef CONFIG_LINK_DEVICE_SPI_RFS_DEBUG
  567. rfs_header = (struct rfs_hdr *)fragdata(iod, ld)->h_data.hdr;
  568. mif_info("\n<%s> Rx RFS frame (CMD : 0x%02x) (ID : 0x%02x) (Len %d)\n",
  569. iod->name, rfs_header->cmd, rfs_header->id, rfs_header->len);
  570. #endif
  571. case IPC_BOOT:
  572. case IPC_BOOT_2:
  573. case IPC_RAMDUMP:
  574. default:
  575. skb_queue_tail(&iod->sk_rx_q, skb);
  576. mif_debug("wake up wq of %s\n", iod->name);
  577. wake_up(&iod->wq);
  578. return 0;
  579. }
  580. }
  581. static int rx_hdlc_packet(struct io_device *iod, struct link_device *ld,
  582. const char *data, unsigned recv_size)
  583. {
  584. int rest = (int)recv_size;
  585. char *buf = (char *)data;
  586. int err = 0;
  587. int len = 0;
  588. unsigned rcvd = 0;
  589. if (rest <= 0)
  590. goto exit;
  591. mif_debug("RX_SIZE = %d, ld: %s\n", rest, ld->name);
  592. if (fragdata(iod, ld)->h_data.frag_len) {
  593. /*
  594. If the fragdata(iod, ld)->h_data.frag_len field is
  595. not zero, there is a HDLC frame that is waiting for more data
  596. or HDLC_END in the skb (fragdata(iod, ld)->skb_recv).
  597. In this case, rx_hdlc_head_check() must be skipped.
  598. */
  599. goto data_check;
  600. }
  601. mif_info("\n<%s> Rx FMT frame (len %d)\n",iod->name, rest);
  602. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  603. mif_print_data((char*)data, rest);
  604. mif_info("\n");
  605. #endif
  606. next_frame:
  607. err = len = rx_hdlc_head_check(iod, ld, buf, rest);
  608. if (err < 0)
  609. goto exit;
  610. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  611. __LINE__);
  612. buf += len;
  613. rest -= len;
  614. if (rest <= 0)
  615. goto exit;
  616. data_check:
  617. /*
  618. If the return value of rx_hdlc_data_check() is zero, there remains
  619. only HDLC_END that will be received.
  620. */
  621. err = len = rx_hdlc_data_check(iod, ld, buf, rest);
  622. if (err < 0)
  623. goto exit;
  624. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  625. __LINE__);
  626. buf += len;
  627. rest -= len;
  628. if (!rest && fragdata(iod, ld)->h_data.frag_len) {
  629. /*
  630. Data is being received and more data or HDLC_END does not
  631. arrive yet, but there is no more data in the buffer. More
  632. data may come within the next frame from the link device.
  633. */
  634. return 0;
  635. } else if (rest <= 0)
  636. goto exit;
  637. /* At this point, one HDLC frame except HDLC_END has been received. */
  638. err = len = rx_hdlc_tail_check(buf);
  639. if (err < 0) {
  640. mif_err("Wrong HDLC end: 0x%x(%s), rest: %d,"
  641. " recv_size:%d\n", *buf, iod->name, rest, recv_size);
  642. goto exit;
  643. }
  644. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  645. __LINE__);
  646. buf += len;
  647. rest -= len;
  648. /* At this point, one complete HDLC frame has been received. */
  649. /*
  650. The padding size is applied for the next HDLC frame. Zero will be
  651. returned by calc_padding_size() if the link device does not require
  652. 4-byte aligned access.
  653. */
  654. rcvd = get_hdlc_size(iod, fragdata(iod, ld)->h_data.hdr) +
  655. (SIZE_OF_HDLC_START + SIZE_OF_HDLC_END);
  656. len = calc_padding_size(ld, rcvd);
  657. buf += len;
  658. rest -= len;
  659. if (rest < 0)
  660. goto exit;
  661. err = rx_iodev_skb(fragdata(iod, ld)->skb_recv);
  662. if (err < 0)
  663. goto exit;
  664. /* initialize header & skb */
  665. fragdata(iod, ld)->skb_recv = NULL;
  666. memset(&fragdata(iod, ld)->h_data, 0x00,
  667. sizeof(struct header_data));
  668. fragdata(iod, ld)->realloc_offset = 0;
  669. if (rest)
  670. goto next_frame;
  671. exit:
  672. if (rest < 0)
  673. err = -ERANGE;
  674. if (err == -ENOMEM) {
  675. if (!(fragdata(iod, ld)->h_data.frag_len))
  676. memset(&fragdata(iod, ld)->h_data, 0x00,
  677. sizeof(struct header_data));
  678. return err;
  679. }
  680. if (err < 0 && fragdata(iod, ld)->skb_recv) {
  681. dev_kfree_skb_any(fragdata(iod, ld)->skb_recv);
  682. fragdata(iod, ld)->skb_recv = NULL;
  683. /* clear headers */
  684. memset(&fragdata(iod, ld)->h_data, 0x00,
  685. sizeof(struct header_data));
  686. fragdata(iod, ld)->realloc_offset = 0;
  687. }
  688. return err;
  689. }
  690. /* called from link device when a packet arrives for this io device */
  691. static int io_dev_recv_data_from_link_dev(struct io_device *iod,
  692. struct link_device *ld, const char *data, unsigned int len)
  693. {
  694. struct sk_buff *skb;
  695. int err;
  696. switch (iod->format) {
  697. case IPC_RFS:
  698. case IPC_FMT:
  699. case IPC_RAW:
  700. case IPC_MULTI_RAW:
  701. if (iod->waketime)
  702. wake_lock_timeout(&iod->wakelock, iod->waketime);
  703. err = rx_hdlc_packet(iod, ld, data, len);
  704. if (err < 0)
  705. mif_err("fail process HDLC frame\n");
  706. return err;
  707. case IPC_CMD:
  708. case IPC_BOOT:
  709. case IPC_BOOT_2:
  710. case IPC_RAMDUMP:
  711. /* save packet to sk_buff */
  712. skb = rx_alloc_skb(len, iod, ld);
  713. if (!skb) {
  714. mif_err("fail alloc skb (%d)\n", __LINE__);
  715. return -ENOMEM;
  716. }
  717. mif_debug("boot len : %d\n", len);
  718. memcpy(skb_put(skb, len), data, len);
  719. skb_queue_tail(&iod->sk_rx_q, skb);
  720. mif_debug("skb len : %d\n", skb->len);
  721. wake_up(&iod->wq);
  722. return len;
  723. default:
  724. return -EINVAL;
  725. }
  726. }
  727. /* inform the IO device that the modem is now online or offline or
  728. * crashing or whatever...
  729. */
  730. static void io_dev_modem_state_changed(struct io_device *iod,
  731. enum modem_state state)
  732. {
  733. iod->mc->phone_state = state;
  734. mif_err("modem state changed. (iod: %s, state: %d)\n",
  735. iod->name, state);
  736. if ((state == STATE_CRASH_RESET) || (state == STATE_CRASH_EXIT)
  737. || (state == STATE_NV_REBUILDING))
  738. wake_up(&iod->wq);
  739. }
  740. /**
  741. * io_dev_sim_state_changed
  742. * @iod: IPC's io_device
  743. * @sim_online: SIM is online?
  744. */
  745. static void io_dev_sim_state_changed(struct io_device *iod, bool sim_online)
  746. {
  747. if (atomic_read(&iod->opened) == 0)
  748. mif_info("iod is not opened: %s\n",
  749. iod->name);
  750. else if (iod->mc->sim_state.online == sim_online)
  751. mif_info("sim state not changed.\n");
  752. else {
  753. iod->mc->sim_state.online = sim_online;
  754. iod->mc->sim_state.changed = true;
  755. mif_err("sim state changed. (iod: %s, state: "
  756. "[online=%d, changed=%d])\n",
  757. iod->name, iod->mc->sim_state.online,
  758. iod->mc->sim_state.changed);
  759. wake_up(&iod->wq);
  760. }
  761. }
  762. static int misc_open(struct inode *inode, struct file *filp)
  763. {
  764. struct io_device *iod = to_io_device(filp->private_data);
  765. struct modem_shared *msd = iod->msd;
  766. struct link_device *ld;
  767. int ret;
  768. filp->private_data = (void *)iod;
  769. mif_err("iod = %s\n", iod->name);
  770. atomic_inc(&iod->opened);
  771. list_for_each_entry(ld, &msd->link_dev_list, list) {
  772. if (IS_CONNECTED(iod, ld) && ld->init_comm) {
  773. ret = ld->init_comm(ld, iod);
  774. if (ret < 0) {
  775. mif_err("%s: init_comm error: %d\n",
  776. ld->name, ret);
  777. return ret;
  778. }
  779. }
  780. }
  781. return 0;
  782. }
  783. static int misc_release(struct inode *inode, struct file *filp)
  784. {
  785. struct io_device *iod = (struct io_device *)filp->private_data;
  786. struct modem_shared *msd = iod->msd;
  787. struct link_device *ld;
  788. mif_err("iod = %s\n", iod->name);
  789. atomic_dec(&iod->opened);
  790. skb_queue_purge(&iod->sk_rx_q);
  791. list_for_each_entry(ld, &msd->link_dev_list, list) {
  792. if (IS_CONNECTED(iod, ld) && ld->terminate_comm)
  793. ld->terminate_comm(ld, iod);
  794. }
  795. return 0;
  796. }
  797. static unsigned int misc_poll(struct file *filp, struct poll_table_struct *wait)
  798. {
  799. struct io_device *iod = (struct io_device *)filp->private_data;
  800. poll_wait(filp, &iod->wq, wait);
  801. if ((!skb_queue_empty(&iod->sk_rx_q))
  802. && (iod->mc->phone_state != STATE_OFFLINE))
  803. return POLLIN | POLLRDNORM;
  804. else if ((iod->mc->phone_state == STATE_CRASH_RESET) ||
  805. (iod->mc->phone_state == STATE_CRASH_EXIT) ||
  806. (iod->mc->phone_state == STATE_NV_REBUILDING) ||
  807. #if defined(CONFIG_SEC_DUAL_MODEM_MODE)
  808. (iod->mc->phone_state == STATE_MODEM_SWITCH) ||
  809. #endif
  810. iod->mc->sim_state.changed)
  811. return POLLHUP;
  812. else
  813. return 0;
  814. }
  815. static long misc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  816. {
  817. int s_state;
  818. struct io_device *iod = (struct io_device *)filp->private_data;
  819. struct link_device *ld = get_current_link(iod);
  820. char cpinfo_buf[530] = "CP Crash ";
  821. char str[TASK_COMM_LEN];
  822. int phone_active_value;
  823. mif_debug("cmd = 0x%x\n", cmd);
  824. switch (cmd) {
  825. case IOCTL_MODEM_ON:
  826. mif_debug("misc_ioctl : IOCTL_MODEM_ON\n");
  827. return iod->mc->ops.modem_on(iod->mc);
  828. case IOCTL_MODEM_OFF:
  829. mif_debug("misc_ioctl : IOCTL_MODEM_OFF\n");
  830. return iod->mc->ops.modem_off(iod->mc);
  831. case IOCTL_MODEM_RESET:
  832. mif_debug("misc_ioctl : IOCTL_MODEM_RESET\n");
  833. return iod->mc->ops.modem_reset(iod->mc);
  834. case IOCTL_MODEM_BOOT_ON:
  835. mif_debug("misc_ioctl : IOCTL_MODEM_BOOT_ON\n");
  836. return iod->mc->ops.modem_boot_on(iod->mc);
  837. case IOCTL_MODEM_BOOT_OFF:
  838. mif_debug("misc_ioctl : IOCTL_MODEM_BOOT_OFF\n");
  839. return iod->mc->ops.modem_boot_off(iod->mc);
  840. case IOCTL_MODEM_BOOT_DONE:
  841. mif_debug("misc_ioctl : IOCTL_MODEM_START\n");
  842. return 0;
  843. case IOCTL_MODEM_STATUS:
  844. mif_debug("misc_ioctl : IOCTL_MODEM_STATUS\n");
  845. if (iod->mc->sim_state.changed &&
  846. !strcmp(get_task_comm(str, get_current()), "rild")) {
  847. s_state = iod->mc->sim_state.online ?
  848. STATE_SIM_ATTACH : STATE_SIM_DETACH;
  849. iod->mc->sim_state.changed = false;
  850. mif_info("SIM states (%d) to %s\n", s_state, str);
  851. return s_state;
  852. }
  853. if (iod->mc->phone_state == STATE_NV_REBUILDING) {
  854. mif_info("send nv rebuild state : %d\n",
  855. iod->mc->phone_state);
  856. iod->mc->phone_state = STATE_ONLINE;
  857. }
  858. if(iod->mc->phone_state==STATE_BOOTING)
  859. {
  860. phone_active_value =gpio_get_value(iod->mc->gpio_phone_active);
  861. mif_info("phone active value: %d / %d\n" ,iod->mc->gpio_phone_active, phone_active_value);
  862. if(phone_active_value)
  863. iod->mc->phone_state = STATE_ONLINE;
  864. }
  865. return iod->mc->phone_state;
  866. case IOCTL_MODEM_FORCE_CRASH_EXIT:
  867. mif_debug("misc_ioctl : IOCTL_MODEM_FORCE_CRASH_EXIT\n");
  868. if (iod->mc->ops.modem_force_crash_exit)
  869. return iod->mc->ops.modem_force_crash_exit(iod->mc);
  870. return -EINVAL;
  871. case IOCTL_MODEM_CP_UPLOAD:
  872. mif_err("misc_ioctl : IOCTL_MODEM_CP_UPLOAD\n");
  873. if (copy_from_user(cpinfo_buf + strlen(cpinfo_buf),
  874. (void __user *)arg, MAX_CPINFO_SIZE) != 0)
  875. panic("CP Crash");
  876. else
  877. panic(cpinfo_buf);
  878. return 0;
  879. case IOCTL_MODEM_DUMP_RESET:
  880. mif_err("misc_ioctl : IOCTL_MODEM_DUMP_RESET\n");
  881. return iod->mc->ops.modem_dump_reset(iod->mc);
  882. #if defined(CONFIG_SEC_DUAL_MODEM_MODE)
  883. case IOCTL_MODEM_SWITCH_MODEM:
  884. mif_err("misc_ioctl : IOCTL_MODEM_SWITCH_MODEM\n");
  885. iod->mc->phone_state = STATE_MODEM_SWITCH;
  886. wake_up(&iod->wq);
  887. return 0;
  888. #endif
  889. default:
  890. /* If you need to handle the ioctl for specific link device,
  891. * then assign the link ioctl handler to ld->ioctl
  892. * It will be call for specific link ioctl */
  893. if (ld->ioctl) //SPI_SETUP
  894. return ld->ioctl(ld, iod, cmd, arg);
  895. mif_err("misc_ioctl : ioctl 0x%X is not defined.\n", cmd);
  896. return -EINVAL;
  897. }
  898. }
  899. static size_t _boot_write(struct io_device *iod, const char __user *buf,
  900. size_t count)
  901. {
  902. int rest_len = count, frame_len = 0;
  903. char *cur = (char *)buf;
  904. struct sk_buff *skb = NULL;
  905. struct link_device *ld = get_current_link(iod);
  906. int ret;
  907. while (rest_len) {
  908. frame_len = min(rest_len, MAX_BOOTDATA_SIZE);
  909. skb = alloc_skb(frame_len, GFP_KERNEL);
  910. if (!skb) {
  911. mif_err("fail alloc skb (%d)\n", __LINE__);
  912. return -ENOMEM;
  913. }
  914. if (copy_from_user(
  915. skb_put(skb, frame_len), cur, frame_len) != 0) {
  916. dev_kfree_skb_any(skb);
  917. return -EFAULT;
  918. }
  919. rest_len -= frame_len;
  920. cur += frame_len;
  921. skbpriv(skb)->iod = iod;
  922. skbpriv(skb)->ld = ld;
  923. ret = ld->send(ld, iod, skb);
  924. if (ret < 0) {
  925. dev_kfree_skb_any(skb);
  926. return ret;
  927. }
  928. }
  929. return count;
  930. }
  931. static ssize_t misc_write(struct file *filp, const char __user *buf,
  932. size_t count, loff_t *ppos)
  933. {
  934. struct io_device *iod = (struct io_device *)filp->private_data;
  935. struct link_device *ld = get_current_link(iod);
  936. int frame_len = 0;
  937. char frame_header_buf[sizeof(struct raw_hdr)];
  938. struct sk_buff *skb;
  939. int err;
  940. size_t tx_size;
  941. #ifdef CONFIG_LINK_DEVICE_SPI_RFS_DEBUG
  942. struct rfs_hdr *rfs_header;
  943. char *frm;
  944. #endif
  945. /* ToDo - Add handling for over size data */
  946. frame_len = SIZE_OF_HDLC_START +
  947. get_header_size(iod) +
  948. count +
  949. SIZE_OF_HDLC_END;
  950. if (ld->aligned)
  951. frame_len += MAX_LINK_PADDING_SIZE;
  952. skb = alloc_skb(frame_len, GFP_KERNEL);
  953. if (!skb) {
  954. if (frame_len > MAX_BOOTDATA_SIZE && iod->format == IPC_BOOT) {
  955. mif_info("large alloc fail\n");
  956. return _boot_write(iod, buf, count);
  957. }
  958. mif_err("fail alloc skb (%d)\n", __LINE__);
  959. return -ENOMEM;
  960. }
  961. switch (iod->format) {
  962. case IPC_CMD:
  963. case IPC_BOOT:
  964. case IPC_BOOT_2:
  965. case IPC_RAMDUMP:
  966. if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
  967. dev_kfree_skb_any(skb);
  968. return -EFAULT;
  969. }
  970. break;
  971. case IPC_RFS:
  972. memcpy(skb_put(skb, SIZE_OF_HDLC_START), hdlc_start,
  973. SIZE_OF_HDLC_START);
  974. if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
  975. dev_kfree_skb_any(skb);
  976. return -EFAULT;
  977. }
  978. memcpy(skb_put(skb, SIZE_OF_HDLC_END), hdlc_end,
  979. SIZE_OF_HDLC_END);
  980. break;
  981. case IPC_FMT:
  982. case IPC_RAW:
  983. case IPC_MULTI_RAW:
  984. default:
  985. memcpy(skb_put(skb, SIZE_OF_HDLC_START), hdlc_start,
  986. SIZE_OF_HDLC_START);
  987. memcpy(skb_put(skb, get_header_size(iod)),
  988. get_header(iod, count, frame_header_buf),
  989. get_header_size(iod));
  990. if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
  991. dev_kfree_skb_any(skb);
  992. return -EFAULT;
  993. }
  994. memcpy(skb_put(skb, SIZE_OF_HDLC_END), hdlc_end,
  995. SIZE_OF_HDLC_END);
  996. break;
  997. }
  998. if (ld->aligned)
  999. skb_put(skb, calc_padding_size(ld, skb->len));
  1000. #ifdef CONFIG_LINK_DEVICE_SPI_DEBUG
  1001. if (iod->format == IPC_FMT) {
  1002. mif_info("\n<%s> Tx HDLC FMT frame (len %d)\n",
  1003. iod->name, skb->len);
  1004. print_sipc4_hdlc_fmt_frame(skb->data);
  1005. mif_info("\n");
  1006. }
  1007. #endif
  1008. #ifdef CONFIG_LINK_DEVICE_SPI_RFS_DEBUG
  1009. if (iod->format == IPC_RFS) {
  1010. frm = (u8 *)(skb->data + 1);
  1011. rfs_header = (struct rfs_hdr*)(frm);
  1012. mif_info("\n<%s> Tx RFS frame (CMD : 0x%02x) (ID : 0x%02x) (Len %d)\n",
  1013. iod->name, rfs_header->cmd, rfs_header->id, rfs_header->len);
  1014. }
  1015. #endif
  1016. /* send data with sk_buff, link device will put sk_buff
  1017. * into the specific sk_buff_q and run work-q to send data
  1018. */
  1019. tx_size = skb->len;
  1020. skbpriv(skb)->iod = iod;
  1021. skbpriv(skb)->ld = ld;
  1022. err = ld->send(ld, iod, skb);
  1023. if (err < 0)
  1024. return err;
  1025. if (err != tx_size)
  1026. mif_err("WARNNING: wrong tx size: %s, format=%d "
  1027. "count=%d, tx_size=%d, return_size=%d",
  1028. iod->name, iod->format, count, tx_size, err);
  1029. return count;
  1030. }
  1031. static ssize_t misc_read(struct file *filp, char *buf, size_t count,
  1032. loff_t *f_pos)
  1033. {
  1034. struct io_device *iod = (struct io_device *)filp->private_data;
  1035. struct sk_buff *skb = NULL;
  1036. int pktsize = 0;
  1037. skb = skb_dequeue(&iod->sk_rx_q);
  1038. if (!skb) {
  1039. printk_ratelimited(KERN_ERR "mif: no data from sk_rx_q, "
  1040. "modem_state : %d(%s)\n",
  1041. iod->mc->phone_state, iod->name);
  1042. return 0;
  1043. }
  1044. if (skb->len > count) {
  1045. mif_err("skb len is too big = %d,%d!(%d)\n",
  1046. count, skb->len, __LINE__);
  1047. dev_kfree_skb_any(skb);
  1048. return -EIO;
  1049. }
  1050. pktsize = skb->len;
  1051. if (copy_to_user(buf, skb->data, pktsize) != 0) {
  1052. dev_kfree_skb_any(skb);
  1053. return -EIO;
  1054. }
  1055. dev_kfree_skb_any(skb);
  1056. return pktsize;
  1057. }
  1058. static const struct file_operations misc_io_fops = {
  1059. .owner = THIS_MODULE,
  1060. .open = misc_open,
  1061. .release = misc_release,
  1062. .poll = misc_poll,
  1063. .unlocked_ioctl = misc_ioctl,
  1064. .write = misc_write,
  1065. .read = misc_read,
  1066. };
  1067. static int vnet_open(struct net_device *ndev)
  1068. {
  1069. struct vnet *vnet = netdev_priv(ndev);
  1070. netif_start_queue(ndev);
  1071. atomic_inc(&vnet->iod->opened);
  1072. return 0;
  1073. }
  1074. static int vnet_stop(struct net_device *ndev)
  1075. {
  1076. struct vnet *vnet = netdev_priv(ndev);
  1077. atomic_dec(&vnet->iod->opened);
  1078. netif_stop_queue(ndev);
  1079. return 0;
  1080. }
  1081. static int vnet_xmit(struct sk_buff *skb, struct net_device *ndev)
  1082. {
  1083. int ret;
  1084. int headroom = 0;
  1085. int tailroom = 0;
  1086. struct sk_buff *skb_new;
  1087. struct vnet *vnet = netdev_priv(ndev);
  1088. struct io_device *iod = vnet->iod;
  1089. struct link_device *ld = get_current_link(iod);
  1090. struct raw_hdr hd;
  1091. /* ToDo - Add handling for over size data */
  1092. /* When use `handover' with Network Bridge,
  1093. * user -> TCP/IP(kernel) -> bridge device -> TCP/IP(kernel) -> this.
  1094. *
  1095. * We remove the one ethernet header of skb before using skb->len,
  1096. * because the skb has two ethernet headers.
  1097. */
  1098. if (iod->use_handover) {
  1099. if (iod->id >= PSD_DATA_CHID_BEGIN &&
  1100. iod->id <= PSD_DATA_CHID_END)
  1101. skb_pull(skb, sizeof(struct ethhdr));
  1102. }
  1103. hd.len = skb->len + sizeof(hd);
  1104. hd.control = 0;
  1105. hd.channel = iod->id & 0x1F;
  1106. headroom = sizeof(hd) + sizeof(hdlc_start);
  1107. tailroom = sizeof(hdlc_end);
  1108. if (ld->aligned)
  1109. tailroom += MAX_LINK_PADDING_SIZE;
  1110. if (skb_headroom(skb) < headroom || skb_tailroom(skb) < tailroom) {
  1111. skb_new = skb_copy_expand(skb, headroom, tailroom, GFP_ATOMIC);
  1112. /* skb_copy_expand success or not, free old skb from caller */
  1113. dev_kfree_skb_any(skb);
  1114. if (!skb_new)
  1115. return -ENOMEM;
  1116. } else
  1117. skb_new = skb;
  1118. memcpy(skb_push(skb_new, sizeof(hd)), &hd, sizeof(hd));
  1119. memcpy(skb_push(skb_new, sizeof(hdlc_start)), hdlc_start,
  1120. sizeof(hdlc_start));
  1121. memcpy(skb_put(skb_new, sizeof(hdlc_end)), hdlc_end, sizeof(hdlc_end));
  1122. skb_put(skb_new, calc_padding_size(ld, skb_new->len));
  1123. skbpriv(skb_new)->iod = iod;
  1124. skbpriv(skb_new)->ld = ld;
  1125. ret = ld->send(ld, iod, skb_new);
  1126. if (ret < 0) {
  1127. netif_stop_queue(ndev);
  1128. dev_kfree_skb_any(skb_new);
  1129. return NETDEV_TX_BUSY;
  1130. }
  1131. ndev->stats.tx_packets++;
  1132. ndev->stats.tx_bytes += skb->len;
  1133. return NETDEV_TX_OK;
  1134. }
  1135. static struct net_device_ops vnet_ops = {
  1136. .ndo_open = vnet_open,
  1137. .ndo_stop = vnet_stop,
  1138. .ndo_start_xmit = vnet_xmit,
  1139. };
  1140. static void vnet_setup(struct net_device *ndev)
  1141. {
  1142. ndev->netdev_ops = &vnet_ops;
  1143. ndev->type = ARPHRD_PPP;
  1144. ndev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  1145. ndev->addr_len = 0;
  1146. ndev->hard_header_len = 0;
  1147. ndev->tx_queue_len = 1000;
  1148. ndev->mtu = ETH_DATA_LEN;
  1149. ndev->watchdog_timeo = 5 * HZ;
  1150. }
  1151. static void vnet_setup_ether(struct net_device *ndev)
  1152. {
  1153. ndev->netdev_ops = &vnet_ops;
  1154. ndev->type = ARPHRD_ETHER;
  1155. ndev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST | IFF_SLAVE;
  1156. ndev->addr_len = ETH_ALEN;
  1157. random_ether_addr(ndev->dev_addr);
  1158. ndev->hard_header_len = 0;
  1159. ndev->tx_queue_len = 1000;
  1160. ndev->mtu = ETH_DATA_LEN;
  1161. ndev->watchdog_timeo = 5 * HZ;
  1162. }
  1163. int sipc4_init_io_device(struct io_device *iod)
  1164. {
  1165. int ret = 0;
  1166. struct vnet *vnet;
  1167. /* get modem state from modem control device */
  1168. iod->modem_state_changed = io_dev_modem_state_changed;
  1169. /* to send SIM change event */
  1170. iod->sim_state_changed = io_dev_sim_state_changed;
  1171. /* get data from link device */
  1172. iod->recv = io_dev_recv_data_from_link_dev;
  1173. /* register misc or net drv */
  1174. switch (iod->io_typ) {
  1175. case IODEV_MISC:
  1176. init_waitqueue_head(&iod->wq);
  1177. skb_queue_head_init(&iod->sk_rx_q);
  1178. iod->miscdev.minor = MISC_DYNAMIC_MINOR;
  1179. iod->miscdev.name = iod->name;
  1180. iod->miscdev.fops = &misc_io_fops;
  1181. ret = misc_register(&iod->miscdev);
  1182. if (ret)
  1183. mif_err("failed to register misc io device : %s\n",
  1184. iod->name);
  1185. break;
  1186. case IODEV_NET:
  1187. skb_queue_head_init(&iod->sk_rx_q);
  1188. if (iod->use_handover)
  1189. iod->ndev = alloc_netdev(0, iod->name,
  1190. vnet_setup_ether);
  1191. else
  1192. iod->ndev = alloc_netdev(0, iod->name, vnet_setup);
  1193. if (!iod->ndev) {
  1194. mif_err("failed to alloc netdev\n");
  1195. return -ENOMEM;
  1196. }
  1197. ret = register_netdev(iod->ndev);
  1198. if (ret)
  1199. free_netdev(iod->ndev);
  1200. mif_debug("(iod:0x%p)\n", iod);
  1201. vnet = netdev_priv(iod->ndev);
  1202. mif_debug("(vnet:0x%p)\n", vnet);
  1203. vnet->iod = iod;
  1204. break;
  1205. case IODEV_DUMMY:
  1206. skb_queue_head_init(&iod->sk_rx_q);
  1207. iod->miscdev.minor = MISC_DYNAMIC_MINOR;
  1208. iod->miscdev.name = iod->name;
  1209. iod->miscdev.fops = &misc_io_fops;
  1210. ret = misc_register(&iod->miscdev);
  1211. if (ret)
  1212. mif_err("failed to register misc io device : %s\n",
  1213. iod->name);
  1214. ret = device_create_file(iod->miscdev.this_device,
  1215. &attr_waketime);
  1216. if (ret)
  1217. mif_err("failed to create sysfs file : %s\n",
  1218. iod->name);
  1219. break;
  1220. default:
  1221. mif_err("wrong io_type : %d\n", iod->io_typ);
  1222. return -EINVAL;
  1223. }
  1224. mif_info("%s(%d) : init_io_device() done : %d\n",
  1225. iod->name, iod->io_typ, ret);
  1226. return ret;
  1227. }