sipc5_io_device.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. /* /linux/drivers/misc/modem_if_v2/sipc5_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/platform_data/modem_v2.h>
  28. #include "modem_prj.h"
  29. #include "modem_utils.h"
  30. #define SIPC5_CFG_MASK 0b11111000
  31. #define SIPC5_SIZE_OF_CFG 1
  32. #define SIPC5_CFG_PADDING_MASK 0b00000100
  33. #define SIPC5_CFG_EXT_FIELD_MASK 0b00000010
  34. #define SIPC5_CFG_CTL_FIELD_MASK 0b00000001
  35. #define SIPC5_MIN_SIZE_OF_HEADER 3 /* Ch ID: 1B, Len: 2B */
  36. #define SIPC5_MAX_SIZE_OF_HEADER 4 /* + Ex Field(Cont or Ex Len): 1B */
  37. struct sipc5_hdr {
  38. u8 ch_id;
  39. u16 len;
  40. u8 ext_field;
  41. } __packed;
  42. static ssize_t show_waketime(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. unsigned int msec;
  46. char *p = buf;
  47. struct miscdevice *miscdev = dev_get_drvdata(dev);
  48. struct io_device *iod = container_of(miscdev, struct io_device,
  49. miscdev);
  50. msec = jiffies_to_msecs(iod->waketime);
  51. p += sprintf(buf, "raw waketime : %ums\n", msec);
  52. return p - buf;
  53. }
  54. static ssize_t store_waketime(struct device *dev,
  55. struct device_attribute *attr, const char *buf, size_t count)
  56. {
  57. unsigned long msec;
  58. int ret;
  59. struct miscdevice *miscdev = dev_get_drvdata(dev);
  60. struct io_device *iod = container_of(miscdev, struct io_device,
  61. miscdev);
  62. ret = strict_strtoul(buf, 10, &msec);
  63. if (ret)
  64. return count;
  65. iod->waketime = msecs_to_jiffies(msec);
  66. return count;
  67. }
  68. static struct device_attribute attr_waketime =
  69. __ATTR(waketime, S_IRUGO | S_IWUSR, show_waketime, store_waketime);
  70. static int get_header_size(char cfg)
  71. {
  72. if (cfg & SIPC5_CFG_EXT_FIELD_MASK)
  73. return SIPC5_MAX_SIZE_OF_HEADER;
  74. else
  75. return SIPC5_MIN_SIZE_OF_HEADER;
  76. }
  77. static int get_hdlc_size(char *buf)
  78. {
  79. struct sipc5_hdr *sipc5_header;
  80. sipc5_header = (struct sipc5_hdr *)buf;
  81. return sipc5_header->len;
  82. }
  83. static u8 get_cfg_field(struct io_device *iod, struct link_device *ld,
  84. ssize_t count)
  85. {
  86. u8 config = SIPC5_CFG_MASK;
  87. if (ld->aligned)
  88. config |= SIPC5_CFG_PADDING_MASK;
  89. switch (iod->format) {
  90. case IPC_FMT:
  91. if (count > MAX_IPC_TX_SIZE)
  92. config |= SIPC5_CFG_EXT_FIELD_MASK |
  93. SIPC5_CFG_CTL_FIELD_MASK;
  94. return config;
  95. case IPC_RAW:
  96. case IPC_MULTI_RAW:
  97. if (count > MAX_MTU_TX_DATA_SIZE)
  98. config |= SIPC5_CFG_EXT_FIELD_MASK |
  99. SIPC5_CFG_CTL_FIELD_MASK;
  100. return config;
  101. case IPC_RFS:
  102. case IPC_CMD:
  103. case IPC_BOOT:
  104. case IPC_RAMDUMP:
  105. default:
  106. if (count > 0xFFFF)
  107. config |= SIPC5_CFG_EXT_FIELD_MASK;
  108. return config;
  109. }
  110. }
  111. static void *get_header(struct io_device *iod, size_t count,
  112. char *frame_header_buf, u8 cfg)
  113. {
  114. struct sipc5_hdr *sipc5_h = (struct sipc5_hdr *)frame_header_buf;
  115. switch (iod->format) {
  116. case IPC_FMT:
  117. sipc5_h->ch_id = iod->id;
  118. sipc5_h->len = (u16)count + get_header_size(cfg)
  119. + SIPC5_SIZE_OF_CFG;
  120. /* ToDo - Add handling for over size data */
  121. sipc5_h->ext_field = 0;
  122. return (void *)frame_header_buf;
  123. case IPC_RAW:
  124. case IPC_MULTI_RAW:
  125. sipc5_h->ch_id = iod->id & 0x1F;
  126. sipc5_h->len = (u16)count + get_header_size(cfg)
  127. + SIPC5_SIZE_OF_CFG;
  128. /* ToDo - Add handling for over size data */
  129. sipc5_h->ext_field = 0;
  130. return (void *)frame_header_buf;
  131. case IPC_RFS:
  132. sipc5_h->ch_id = iod->id;
  133. sipc5_h->len = (u16)count + get_header_size(cfg)
  134. + SIPC5_SIZE_OF_CFG;
  135. /* ToDo - Add handling for over size data */
  136. sipc5_h->ext_field = 0;
  137. return (void *)frame_header_buf;
  138. case IPC_CMD:
  139. case IPC_BOOT:
  140. case IPC_RAMDUMP:
  141. default:
  142. return NULL;
  143. }
  144. }
  145. static inline int calc_padding_size(struct link_device *ld, unsigned len)
  146. {
  147. if (ld->aligned)
  148. return (4 - (len & 0x3)) & 0x3;
  149. else
  150. return 0;
  151. }
  152. static inline int rx_hdlc_head_start_check(char *buf)
  153. {
  154. /* check hdlc head and return size of start byte */
  155. return ((buf[0] & SIPC5_CFG_MASK) == SIPC5_CFG_MASK) ?
  156. SIPC5_SIZE_OF_CFG : -EBADMSG;
  157. }
  158. /* remove hdlc header and store IPC header */
  159. static int rx_hdlc_head_check(struct io_device *iod, struct link_device *ld,
  160. char *buf, unsigned rest)
  161. {
  162. struct header_data *hdr = &fragdata(iod, ld)->h_data;
  163. int head_size;
  164. int done_len = 0;
  165. int len = 0;
  166. /* first frame, remove start header 7F */
  167. if (!hdr->start) {
  168. len = rx_hdlc_head_start_check(buf);
  169. if (len < 0) {
  170. mif_err("Wrong HDLC start: 0x%x(%s)\n",
  171. *buf, iod->name);
  172. return len; /*Wrong hdlc start*/
  173. }
  174. mif_debug("check len : %d, rest : %d (%d)\n", len,
  175. rest, __LINE__);
  176. /* set the start flag of current packet */
  177. hdr->start = buf[0]; /* config field for sipc5 */
  178. hdr->len = 0;
  179. buf += len;
  180. done_len += len;
  181. rest -= len; /* rest, call by value */
  182. }
  183. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  184. __LINE__);
  185. /* get header size without config field size */
  186. head_size = get_header_size(hdr->start);
  187. /* store the HDLC header to iod priv */
  188. if (hdr->len < head_size) {
  189. len = min(rest, head_size - hdr->len);
  190. memcpy(hdr->hdr + hdr->len, buf, len);
  191. hdr->len += len;
  192. done_len += len;
  193. }
  194. mif_debug("check done_len : %d, rest : %d (%d)\n", done_len,
  195. rest, __LINE__);
  196. return done_len;
  197. }
  198. static int rx_iodev_skb(struct sk_buff *skb);
  199. static int rx_hdlc_data_check(struct io_device *iod, struct link_device *ld,
  200. char *buf, unsigned rest)
  201. {
  202. struct header_data *hdr = &fragdata(iod, ld)->h_data;
  203. struct sk_buff *skb = fragdata(iod, ld)->skb_recv;
  204. int head_size = get_header_size(hdr->start);
  205. int data_size = get_hdlc_size(hdr->hdr) - head_size - SIPC5_SIZE_OF_CFG;
  206. int alloc_size;
  207. int len = 0;
  208. int done_len = 0;
  209. int rest_len = data_size - hdr->frag_len;
  210. int continue_len = fragdata(iod, ld)->realloc_offset;
  211. mif_debug("head_size : %d, data_size : %d (%d)\n", head_size,
  212. data_size, __LINE__);
  213. if (continue_len) {
  214. /* check the HDLC header*/
  215. if (rx_hdlc_head_start_check(buf) == SIPC5_SIZE_OF_CFG) {
  216. head_size = get_header_size(buf[0]);
  217. rest_len -= (head_size + SIPC5_SIZE_OF_CFG);
  218. continue_len += (head_size + SIPC5_SIZE_OF_CFG);
  219. }
  220. buf += continue_len;
  221. rest -= continue_len;
  222. done_len += continue_len;
  223. fragdata(iod, ld)->realloc_offset = 0;
  224. mif_debug("realloc_offset = %d\n", continue_len);
  225. }
  226. /* first payload data - alloc skb */
  227. if (!skb) {
  228. /* make skb data size under MAX_RXDATA_SIZE */
  229. alloc_size = min(data_size, MAX_RXDATA_SIZE);
  230. alloc_size = min(alloc_size, rest_len);
  231. /* exceptional case for RFS channel
  232. * make skb for header info first
  233. */
  234. if (iod->format == IPC_RFS && !hdr->frag_len) {
  235. skb = rx_alloc_skb(head_size, iod, ld);
  236. if (unlikely(!skb))
  237. return -ENOMEM;
  238. memcpy(skb_put(skb, head_size), hdr->hdr, head_size);
  239. rx_iodev_skb(skb);
  240. }
  241. /* allocate first packet for data, when its size exceed
  242. * MAX_RXDATA_SIZE, this packet will split to
  243. * multiple packets
  244. */
  245. skb = rx_alloc_skb(alloc_size, iod, ld);
  246. if (unlikely(!skb)) {
  247. fragdata(iod, ld)->realloc_offset = continue_len;
  248. return -ENOMEM;
  249. }
  250. fragdata(iod, ld)->skb_recv = skb;
  251. }
  252. while (rest) {
  253. /* copy length cannot exceed rest_len */
  254. len = min_t(int, rest_len, rest);
  255. /* copy length should be under skb tailroom size */
  256. len = min(len, skb_tailroom(skb));
  257. /* when skb tailroom is bigger than MAX_RXDATA_SIZE
  258. * restrict its size to MAX_RXDATA_SIZE just for convinience */
  259. len = min(len, MAX_RXDATA_SIZE);
  260. /* copy bytes to skb */
  261. memcpy(skb_put(skb, len), buf, len);
  262. /* adjusting variables */
  263. buf += len;
  264. rest -= len;
  265. done_len += len;
  266. rest_len -= len;
  267. hdr->frag_len += len;
  268. /* check if it is final for this packet sequence */
  269. if (!rest_len || !rest)
  270. break;
  271. /* more bytes are remain for this packet sequence
  272. * pass fully loaded skb to rx queue
  273. * and allocate another skb for continues data recv chain
  274. */
  275. rx_iodev_skb(skb);
  276. fragdata(iod, ld)->skb_recv = NULL;
  277. alloc_size = min(rest_len, MAX_RXDATA_SIZE);
  278. skb = rx_alloc_skb(alloc_size, iod, ld);
  279. if (unlikely(!skb)) {
  280. fragdata(iod, ld)->realloc_offset = done_len;
  281. return -ENOMEM;
  282. }
  283. fragdata(iod, ld)->skb_recv = skb;
  284. }
  285. mif_debug("rest : %d, alloc_size : %d , len : %d (%d)\n",
  286. rest, alloc_size, skb->len, __LINE__);
  287. return done_len;
  288. }
  289. static int rx_multipdp(struct sk_buff *skb)
  290. {
  291. int err = 0;
  292. struct io_device *iod = skbpriv(skb)->real_iod;
  293. struct net_device *ndev = NULL;
  294. struct iphdr *ip_header = NULL;
  295. struct ethhdr *ehdr = NULL;
  296. const char source[ETH_ALEN] = SOURCE_MAC_ADDR;
  297. switch (iod->io_typ) {
  298. case IODEV_MISC:
  299. mif_debug("<%s> sk_rx_q.qlen = %d\n",
  300. iod->name, iod->sk_rx_q.qlen);
  301. skb_queue_tail(&iod->sk_rx_q, skb);
  302. wake_up(&iod->wq);
  303. return 0;
  304. case IODEV_NET:
  305. ndev = iod->ndev;
  306. if (!ndev)
  307. return NET_RX_DROP;
  308. skb->dev = ndev;
  309. ndev->stats.rx_packets++;
  310. ndev->stats.rx_bytes += skb->len;
  311. /* check the version of IP */
  312. ip_header = (struct iphdr *)skb->data;
  313. if (ip_header->version == IP6VERSION)
  314. skb->protocol = htons(ETH_P_IPV6);
  315. else
  316. skb->protocol = htons(ETH_P_IP);
  317. if (iod->use_handover) {
  318. skb_push(skb, sizeof(struct ethhdr));
  319. ehdr = (void *)skb->data;
  320. memcpy(ehdr->h_dest, ndev->dev_addr, ETH_ALEN);
  321. memcpy(ehdr->h_source, source, ETH_ALEN);
  322. ehdr->h_proto = skb->protocol;
  323. skb->ip_summed = CHECKSUM_UNNECESSARY;
  324. skb_reset_mac_header(skb);
  325. skb_pull(skb, sizeof(struct ethhdr));
  326. }
  327. if (in_irq())
  328. err = netif_rx(skb);
  329. else
  330. err = netif_rx_ni(skb);
  331. if (err != NET_RX_SUCCESS)
  332. mif_err("ERR: <%s> netif_rx fail (err %d)\n",
  333. iod->name, err);
  334. return err;
  335. default:
  336. mif_err("wrong io_type : %d\n", iod->io_typ);
  337. return -EINVAL;
  338. }
  339. }
  340. /* de-mux function draft */
  341. static int rx_iodev_skb(struct sk_buff *skb)
  342. {
  343. u8 ch;
  344. struct io_device *iod = skbpriv(skb)->iod;
  345. struct io_device *real_iod = NULL;
  346. struct link_device *ld = skbpriv(skb)->ld;
  347. struct sipc5_hdr *sipc5_header;
  348. switch (iod->format) {
  349. case IPC_RAW:
  350. case IPC_MULTI_RAW:
  351. sipc5_header =
  352. (struct sipc5_hdr *)fragdata(iod, ld)->h_data.hdr;
  353. ch = sipc5_header->ch_id;
  354. real_iod = link_get_iod_with_channel(ld, 0x20 | ch);
  355. if (!real_iod) {
  356. mif_err("wrong channel %d\n", ch);
  357. return -1;
  358. }
  359. skbpriv(skb)->real_iod = real_iod;
  360. return rx_multipdp(skb);
  361. case IPC_CMD:
  362. case IPC_FMT:
  363. case IPC_RFS:
  364. case IPC_BOOT:
  365. case IPC_RAMDUMP:
  366. default:
  367. skb_queue_tail(&iod->sk_rx_q, skb);
  368. mif_debug("wake up wq of %s\n", iod->name);
  369. wake_up(&iod->wq);
  370. return 0;
  371. }
  372. }
  373. static int rx_hdlc_packet(struct io_device *iod, struct link_device *ld,
  374. const char *data, unsigned recv_size)
  375. {
  376. int rest = (int)recv_size;
  377. char *buf = (char *)data;
  378. int err = 0;
  379. int len = 0;
  380. unsigned rcvd = 0;
  381. if (rest <= 0)
  382. goto exit;
  383. mif_debug("RX_SIZE = %d, ld: %s\n", rest, ld->name);
  384. if (fragdata(iod, ld)->h_data.frag_len) {
  385. /*
  386. If the fragdata(iod, ld)->h_data.frag_len field is
  387. not zero, there is a HDLC frame that is waiting for more data
  388. or HDLC_END in the skb (fragdata(iod, ld)->skb_recv).
  389. In this case, rx_hdlc_head_check() must be skipped.
  390. */
  391. goto data_check;
  392. }
  393. next_frame:
  394. err = len = rx_hdlc_head_check(iod, ld, buf, rest);
  395. if (err < 0)
  396. goto exit;
  397. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  398. __LINE__);
  399. buf += len;
  400. rest -= len;
  401. if (rest <= 0)
  402. goto exit;
  403. data_check:
  404. /*
  405. If the return value of rx_hdlc_data_check() is zero, there remains
  406. only HDLC_END that will be received.
  407. */
  408. err = len = rx_hdlc_data_check(iod, ld, buf, rest);
  409. if (err < 0)
  410. goto exit;
  411. mif_debug("check len : %d, rest : %d (%d)\n", len, rest,
  412. __LINE__);
  413. buf += len;
  414. rest -= len;
  415. if (!rest && fragdata(iod, ld)->h_data.frag_len) {
  416. /*
  417. Data is being received and more data or HDLC_END does not
  418. arrive yet, but there is no more data in the buffer. More
  419. data may come within the next frame from the link device.
  420. */
  421. return 0;
  422. } else if (rest <= 0)
  423. goto exit;
  424. /* At this point, one complete HDLC frame has been received. */
  425. /*
  426. The padding size is applied for the next HDLC frame. Zero will be
  427. returned by calc_padding_size() if the link device does not require
  428. 4-byte aligned access.
  429. */
  430. rcvd = get_hdlc_size(fragdata(iod, ld)->h_data.hdr) +
  431. SIPC5_SIZE_OF_CFG;
  432. len = calc_padding_size(ld, rcvd);
  433. buf += len;
  434. rest -= len;
  435. if (rest < 0)
  436. goto exit;
  437. err = rx_iodev_skb(fragdata(iod, ld)->skb_recv);
  438. if (err < 0)
  439. goto exit;
  440. /* initialize header & skb */
  441. fragdata(iod, ld)->skb_recv = NULL;
  442. memset(&fragdata(iod, ld)->h_data, 0x00,
  443. sizeof(struct header_data));
  444. fragdata(iod, ld)->realloc_offset = 0;
  445. if (rest)
  446. goto next_frame;
  447. exit:
  448. if (rest < 0)
  449. err = -ERANGE;
  450. if (err == -ENOMEM) {
  451. if (!(fragdata(iod, ld)->h_data.frag_len))
  452. memset(&fragdata(iod, ld)->h_data, 0x00,
  453. sizeof(struct header_data));
  454. return err;
  455. }
  456. if (err < 0 && fragdata(iod, ld)->skb_recv) {
  457. dev_kfree_skb_any(fragdata(iod, ld)->skb_recv);
  458. fragdata(iod, ld)->skb_recv = NULL;
  459. /* clear headers */
  460. memset(&fragdata(iod, ld)->h_data, 0x00,
  461. sizeof(struct header_data));
  462. fragdata(iod, ld)->realloc_offset = 0;
  463. }
  464. return err;
  465. }
  466. /* called from link device when a packet arrives for this io device */
  467. static int io_dev_recv_data_from_link_dev(struct io_device *iod,
  468. struct link_device *ld, const char *data, unsigned int len)
  469. {
  470. struct sk_buff *skb;
  471. int err;
  472. switch (iod->format) {
  473. case IPC_RFS:
  474. case IPC_FMT:
  475. case IPC_RAW:
  476. case IPC_MULTI_RAW:
  477. if (iod->waketime)
  478. wake_lock_timeout(&iod->wakelock, iod->waketime);
  479. err = rx_hdlc_packet(iod, ld, data, len);
  480. if (err < 0)
  481. mif_err("fail process HDLC frame\n");
  482. return err;
  483. case IPC_CMD:
  484. case IPC_BOOT:
  485. case IPC_RAMDUMP:
  486. /* save packet to sk_buff */
  487. skb = rx_alloc_skb(len, iod, ld);
  488. if (!skb) {
  489. mif_err("fail alloc skb (%d)\n", __LINE__);
  490. return -ENOMEM;
  491. }
  492. mif_debug("boot len : %d\n", len);
  493. memcpy(skb_put(skb, len), data, len);
  494. skb_queue_tail(&iod->sk_rx_q, skb);
  495. mif_debug("skb len : %d\n", skb->len);
  496. wake_up(&iod->wq);
  497. return len;
  498. default:
  499. return -EINVAL;
  500. }
  501. }
  502. /* inform the IO device that the modem is now online or offline or
  503. * crashing or whatever...
  504. */
  505. static void io_dev_modem_state_changed(struct io_device *iod,
  506. enum modem_state state)
  507. {
  508. iod->mc->phone_state = state;
  509. mif_err("modem state changed. (iod: %s, state: %d)\n",
  510. iod->name, state);
  511. if ((state == STATE_CRASH_RESET) || (state == STATE_CRASH_EXIT)
  512. || (state == STATE_NV_REBUILDING))
  513. wake_up(&iod->wq);
  514. }
  515. /**
  516. * io_dev_sim_state_changed
  517. * @iod: IPC's io_device
  518. * @sim_online: SIM is online?
  519. */
  520. static void io_dev_sim_state_changed(struct io_device *iod, bool sim_online)
  521. {
  522. if (atomic_read(&iod->opened) == 0)
  523. mif_info("iod is not opened: %s\n",
  524. iod->name);
  525. else if (iod->mc->sim_state.online == sim_online)
  526. mif_info("sim state not changed.\n");
  527. else {
  528. iod->mc->sim_state.online = sim_online;
  529. iod->mc->sim_state.changed = true;
  530. mif_err("sim state changed. (iod: %s, state: "
  531. "[online=%d, changed=%d])\n",
  532. iod->name, iod->mc->sim_state.online,
  533. iod->mc->sim_state.changed);
  534. wake_up(&iod->wq);
  535. }
  536. }
  537. static int misc_open(struct inode *inode, struct file *filp)
  538. {
  539. struct io_device *iod = to_io_device(filp->private_data);
  540. struct modem_shared *msd = iod->msd;
  541. struct link_device *ld;
  542. int ret;
  543. filp->private_data = (void *)iod;
  544. mif_err("iod = %s\n", iod->name);
  545. atomic_inc(&iod->opened);
  546. list_for_each_entry(ld, &msd->link_dev_list, list) {
  547. if (IS_CONNECTED(iod, ld) && ld->init_comm) {
  548. ret = ld->init_comm(ld, iod);
  549. if (ret < 0) {
  550. mif_err("%s: init_comm error: %d\n",
  551. ld->name, ret);
  552. return ret;
  553. }
  554. }
  555. }
  556. return 0;
  557. }
  558. static int misc_release(struct inode *inode, struct file *filp)
  559. {
  560. struct io_device *iod = (struct io_device *)filp->private_data;
  561. struct modem_shared *msd = iod->msd;
  562. struct link_device *ld;
  563. mif_err("iod = %s\n", iod->name);
  564. atomic_dec(&iod->opened);
  565. skb_queue_purge(&iod->sk_rx_q);
  566. list_for_each_entry(ld, &msd->link_dev_list, list) {
  567. if (IS_CONNECTED(iod, ld) && ld->terminate_comm)
  568. ld->terminate_comm(ld, iod);
  569. }
  570. return 0;
  571. }
  572. static unsigned int misc_poll(struct file *filp, struct poll_table_struct *wait)
  573. {
  574. struct io_device *iod = (struct io_device *)filp->private_data;
  575. poll_wait(filp, &iod->wq, wait);
  576. if ((!skb_queue_empty(&iod->sk_rx_q))
  577. && (iod->mc->phone_state != STATE_OFFLINE))
  578. return POLLIN | POLLRDNORM;
  579. else if ((iod->mc->phone_state == STATE_CRASH_RESET) ||
  580. (iod->mc->phone_state == STATE_CRASH_EXIT) ||
  581. (iod->mc->phone_state == STATE_NV_REBUILDING) ||
  582. iod->mc->sim_state.changed)
  583. return POLLHUP;
  584. else
  585. return 0;
  586. }
  587. static long misc_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  588. {
  589. int s_state;
  590. struct io_device *iod = (struct io_device *)filp->private_data;
  591. char cpinfo_buf[530] = "CP Crash ";
  592. char str[TASK_COMM_LEN];
  593. mif_debug("cmd = 0x%x\n", cmd);
  594. switch (cmd) {
  595. case IOCTL_MODEM_ON:
  596. mif_debug("misc_ioctl : IOCTL_MODEM_ON\n");
  597. return iod->mc->ops.modem_on(iod->mc);
  598. case IOCTL_MODEM_OFF:
  599. mif_debug("misc_ioctl : IOCTL_MODEM_OFF\n");
  600. return iod->mc->ops.modem_off(iod->mc);
  601. case IOCTL_MODEM_RESET:
  602. mif_debug("misc_ioctl : IOCTL_MODEM_RESET\n");
  603. return iod->mc->ops.modem_reset(iod->mc);
  604. case IOCTL_MODEM_BOOT_DONE:
  605. mif_debug("misc_ioctl : IOCTL_MODEM_START\n");
  606. return 0;
  607. case IOCTL_MODEM_STATUS:
  608. mif_debug("misc_ioctl : IOCTL_MODEM_STATUS\n");
  609. if (iod->mc->sim_state.changed &&
  610. !strcmp(get_task_comm(str, get_current()), "rild")) {
  611. s_state = iod->mc->sim_state.online ?
  612. STATE_SIM_ATTACH : STATE_SIM_DETACH;
  613. iod->mc->sim_state.changed = false;
  614. mif_info("SIM states (%d) to %s\n", s_state, str);
  615. return s_state;
  616. }
  617. if (iod->mc->phone_state == STATE_NV_REBUILDING) {
  618. mif_info("send nv rebuild state : %d\n",
  619. iod->mc->phone_state);
  620. iod->mc->phone_state = STATE_ONLINE;
  621. }
  622. return iod->mc->phone_state;
  623. case IOCTL_MODEM_FORCE_CRASH_EXIT:
  624. mif_debug("misc_ioctl : IOCTL_MODEM_FORCE_CRASH_EXIT\n");
  625. if (iod->mc->ops.modem_force_crash_exit)
  626. return iod->mc->ops.modem_force_crash_exit(iod->mc);
  627. return -EINVAL;
  628. case IOCTL_MODEM_CP_UPLOAD:
  629. mif_err("misc_ioctl : IOCTL_MODEM_CP_UPLOAD\n");
  630. if (copy_from_user(cpinfo_buf + strlen(cpinfo_buf),
  631. (void __user *)arg, MAX_CPINFO_SIZE) != 0)
  632. panic("CP Crash");
  633. else
  634. panic(cpinfo_buf);
  635. return 0;
  636. default:
  637. mif_err("misc_ioctl : ioctl 0x%X is not defined.\n", cmd);
  638. return -EINVAL;
  639. }
  640. }
  641. static ssize_t misc_write(struct file *filp, const char __user *buf,
  642. size_t count, loff_t *ppos)
  643. {
  644. struct io_device *iod = (struct io_device *)filp->private_data;
  645. struct link_device *ld = get_current_link(iod);
  646. int frame_len = 0;
  647. char frame_header_buf[sizeof(struct sipc5_hdr)];
  648. struct sk_buff *skb;
  649. int err;
  650. size_t tx_size;
  651. u8 config = get_cfg_field(iod, ld, count);
  652. /* ToDo - Add handling for over size data */
  653. frame_len = SIPC5_SIZE_OF_CFG +
  654. get_header_size(config) +
  655. count;
  656. if (ld->aligned)
  657. frame_len += MAX_LINK_PADDING_SIZE;
  658. skb = alloc_skb(frame_len, GFP_KERNEL);
  659. if (!skb) {
  660. mif_err("fail alloc skb (%d)\n", __LINE__);
  661. return -ENOMEM;
  662. }
  663. switch (iod->format) {
  664. case IPC_CMD:
  665. case IPC_BOOT:
  666. case IPC_RAMDUMP:
  667. if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
  668. dev_kfree_skb_any(skb);
  669. return -EFAULT;
  670. }
  671. break;
  672. case IPC_FMT:
  673. case IPC_RAW:
  674. case IPC_MULTI_RAW:
  675. case IPC_RFS:
  676. default:
  677. memcpy(skb_put(skb, SIPC5_SIZE_OF_CFG), (void *)&config,
  678. SIPC5_SIZE_OF_CFG);
  679. memcpy(skb_put(skb, get_header_size(config)),
  680. get_header(iod, count, frame_header_buf,
  681. config),
  682. get_header_size(config));
  683. if (copy_from_user(skb_put(skb, count), buf, count) != 0) {
  684. dev_kfree_skb_any(skb);
  685. return -EFAULT;
  686. }
  687. break;
  688. }
  689. if (ld->aligned)
  690. skb_put(skb, calc_padding_size(ld, skb->len));
  691. /* send data with sk_buff, link device will put sk_buff
  692. * into the specific sk_buff_q and run work-q to send data
  693. */
  694. tx_size = skb->len;
  695. skbpriv(skb)->iod = iod;
  696. skbpriv(skb)->ld = ld;
  697. err = ld->send(ld, iod, skb);
  698. if (err < 0)
  699. return err;
  700. if (err != tx_size)
  701. mif_err("WARNNING: wrong tx size: %s, format=%d "
  702. "count=%d, tx_size=%d, return_size=%d",
  703. iod->name, iod->format, count, tx_size, err);
  704. return count;
  705. }
  706. static ssize_t misc_read(struct file *filp, char *buf, size_t count,
  707. loff_t *f_pos)
  708. {
  709. struct io_device *iod = (struct io_device *)filp->private_data;
  710. struct sk_buff *skb = NULL;
  711. int pktsize = 0;
  712. skb = skb_dequeue(&iod->sk_rx_q);
  713. if (!skb) {
  714. printk_ratelimited(KERN_ERR "mif: no data from sk_rx_q, "
  715. "modem_state : %d(%s)\n",
  716. iod->mc->phone_state, iod->name);
  717. return 0;
  718. }
  719. if (skb->len > count) {
  720. mif_err("skb len is too big = %d,%d!(%d)\n",
  721. count, skb->len, __LINE__);
  722. dev_kfree_skb_any(skb);
  723. return -EIO;
  724. }
  725. pktsize = skb->len;
  726. if (copy_to_user(buf, skb->data, pktsize) != 0) {
  727. dev_kfree_skb_any(skb);
  728. return -EIO;
  729. }
  730. dev_kfree_skb_any(skb);
  731. return pktsize;
  732. }
  733. static const struct file_operations misc_io_fops = {
  734. .owner = THIS_MODULE,
  735. .open = misc_open,
  736. .release = misc_release,
  737. .poll = misc_poll,
  738. .unlocked_ioctl = misc_ioctl,
  739. .write = misc_write,
  740. .read = misc_read,
  741. };
  742. static int vnet_open(struct net_device *ndev)
  743. {
  744. struct vnet *vnet = netdev_priv(ndev);
  745. netif_start_queue(ndev);
  746. atomic_inc(&vnet->iod->opened);
  747. return 0;
  748. }
  749. static int vnet_stop(struct net_device *ndev)
  750. {
  751. struct vnet *vnet = netdev_priv(ndev);
  752. atomic_dec(&vnet->iod->opened);
  753. netif_stop_queue(ndev);
  754. return 0;
  755. }
  756. static int vnet_xmit(struct sk_buff *skb, struct net_device *ndev)
  757. {
  758. int ret;
  759. int headroom = 0;
  760. int tailroom = 0;
  761. struct sk_buff *skb_new;
  762. struct vnet *vnet = netdev_priv(ndev);
  763. struct io_device *iod = vnet->iod;
  764. struct link_device *ld = get_current_link(iod);
  765. struct sipc5_hdr hd;
  766. u8 config = get_cfg_field(iod, ld, skb->len);
  767. int hd_size = get_header_size(config);
  768. /* ToDo - Add handling for over size data */
  769. /* When use `handover' with Network Bridge,
  770. * user -> TCP/IP(kernel) -> bridge device -> TCP/IP(kernel) -> this.
  771. *
  772. * We remove the one ethernet header of skb before using skb->len,
  773. * because the skb has two ethernet headers.
  774. */
  775. if (iod->use_handover) {
  776. if (iod->id >= PSD_DATA_CHID_BEGIN &&
  777. iod->id <= PSD_DATA_CHID_END)
  778. skb_pull(skb, sizeof(struct ethhdr));
  779. }
  780. hd.len = skb->len + hd_size + SIPC5_SIZE_OF_CFG;
  781. hd.ext_field = 0;
  782. hd.ch_id = iod->id & 0x1F;
  783. headroom = hd_size + SIPC5_SIZE_OF_CFG;
  784. if (ld->aligned)
  785. tailroom = MAX_LINK_PADDING_SIZE;
  786. if (skb_headroom(skb) < headroom || skb_tailroom(skb) < tailroom) {
  787. skb_new = skb_copy_expand(skb, headroom, tailroom, GFP_ATOMIC);
  788. /* skb_copy_expand success or not, free old skb from caller */
  789. dev_kfree_skb_any(skb);
  790. if (!skb_new)
  791. return -ENOMEM;
  792. } else
  793. skb_new = skb;
  794. memcpy(skb_push(skb_new, hd_size), &hd, hd_size);
  795. memcpy(skb_push(skb_new, SIPC5_SIZE_OF_CFG), (void *)&config,
  796. SIPC5_SIZE_OF_CFG);
  797. skb_put(skb_new, calc_padding_size(ld, skb_new->len));
  798. skbpriv(skb_new)->iod = iod;
  799. skbpriv(skb_new)->ld = ld;
  800. ret = ld->send(ld, iod, skb_new);
  801. if (ret < 0) {
  802. netif_stop_queue(ndev);
  803. dev_kfree_skb_any(skb_new);
  804. return NETDEV_TX_BUSY;
  805. }
  806. ndev->stats.tx_packets++;
  807. ndev->stats.tx_bytes += skb->len;
  808. return NETDEV_TX_OK;
  809. }
  810. static struct net_device_ops vnet_ops = {
  811. .ndo_open = vnet_open,
  812. .ndo_stop = vnet_stop,
  813. .ndo_start_xmit = vnet_xmit,
  814. };
  815. static void vnet_setup(struct net_device *ndev)
  816. {
  817. ndev->netdev_ops = &vnet_ops;
  818. ndev->type = ARPHRD_PPP;
  819. ndev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
  820. ndev->addr_len = 0;
  821. ndev->hard_header_len = 0;
  822. ndev->tx_queue_len = 1000;
  823. ndev->mtu = ETH_DATA_LEN;
  824. ndev->watchdog_timeo = 5 * HZ;
  825. }
  826. static void vnet_setup_ether(struct net_device *ndev)
  827. {
  828. ndev->netdev_ops = &vnet_ops;
  829. ndev->type = ARPHRD_ETHER;
  830. ndev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST | IFF_SLAVE;
  831. ndev->addr_len = ETH_ALEN;
  832. random_ether_addr(ndev->dev_addr);
  833. ndev->hard_header_len = 0;
  834. ndev->tx_queue_len = 1000;
  835. ndev->mtu = ETH_DATA_LEN;
  836. ndev->watchdog_timeo = 5 * HZ;
  837. }
  838. int sipc5_init_io_device(struct io_device *iod)
  839. {
  840. int ret = 0;
  841. struct vnet *vnet;
  842. /* get modem state from modem control device */
  843. iod->modem_state_changed = io_dev_modem_state_changed;
  844. /* to send SIM change event */
  845. iod->sim_state_changed = io_dev_sim_state_changed;
  846. /* get data from link device */
  847. iod->recv = io_dev_recv_data_from_link_dev;
  848. /* register misc or net drv */
  849. switch (iod->io_typ) {
  850. case IODEV_MISC:
  851. init_waitqueue_head(&iod->wq);
  852. skb_queue_head_init(&iod->sk_rx_q);
  853. iod->miscdev.minor = MISC_DYNAMIC_MINOR;
  854. iod->miscdev.name = iod->name;
  855. iod->miscdev.fops = &misc_io_fops;
  856. ret = misc_register(&iod->miscdev);
  857. if (ret)
  858. mif_err("failed to register misc io device : %s\n",
  859. iod->name);
  860. break;
  861. case IODEV_NET:
  862. skb_queue_head_init(&iod->sk_rx_q);
  863. if (iod->use_handover)
  864. iod->ndev = alloc_netdev(0, iod->name,
  865. vnet_setup_ether);
  866. else
  867. iod->ndev = alloc_netdev(0, iod->name, vnet_setup);
  868. if (!iod->ndev) {
  869. mif_err("failed to alloc netdev\n");
  870. return -ENOMEM;
  871. }
  872. ret = register_netdev(iod->ndev);
  873. if (ret)
  874. free_netdev(iod->ndev);
  875. mif_debug("(iod:0x%p)\n", iod);
  876. vnet = netdev_priv(iod->ndev);
  877. mif_debug("(vnet:0x%p)\n", vnet);
  878. vnet->iod = iod;
  879. break;
  880. case IODEV_DUMMY:
  881. skb_queue_head_init(&iod->sk_rx_q);
  882. iod->miscdev.minor = MISC_DYNAMIC_MINOR;
  883. iod->miscdev.name = iod->name;
  884. iod->miscdev.fops = &misc_io_fops;
  885. ret = misc_register(&iod->miscdev);
  886. if (ret)
  887. mif_err("failed to register misc io device : %s\n",
  888. iod->name);
  889. ret = device_create_file(iod->miscdev.this_device,
  890. &attr_waketime);
  891. if (ret)
  892. mif_err("failed to create sysfs file : %s\n",
  893. iod->name);
  894. break;
  895. default:
  896. mif_err("wrong io_type : %d\n", iod->io_typ);
  897. return -EINVAL;
  898. }
  899. mif_debug("%s(%d) : init_io_device() done : %d\n",
  900. iod->name, iod->io_typ, ret);
  901. return ret;
  902. }