smd_qmi.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. /* arch/arm/mach-msm/smd_qmi.c
  2. *
  3. * QMI Control Driver -- Manages network data connections.
  4. *
  5. * Copyright (C) 2007 Google, Inc.
  6. * Author: Brian Swetland <swetland@google.com>
  7. *
  8. * This software is licensed under the terms of the GNU General Public
  9. * License version 2, as published by the Free Software Foundation, and
  10. * may be copied, distributed, and modified under those terms.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/fs.h>
  20. #include <linux/cdev.h>
  21. #include <linux/device.h>
  22. #include <linux/sched.h>
  23. #include <linux/wait.h>
  24. #include <linux/miscdevice.h>
  25. #include <linux/workqueue.h>
  26. #include <linux/wakelock.h>
  27. #include <asm/uaccess.h>
  28. #include <mach/msm_smd.h>
  29. #define QMI_CTL 0x00
  30. #define QMI_WDS 0x01
  31. #define QMI_DMS 0x02
  32. #define QMI_NAS 0x03
  33. #define QMI_RESULT_SUCCESS 0x0000
  34. #define QMI_RESULT_FAILURE 0x0001
  35. struct qmi_msg {
  36. unsigned char service;
  37. unsigned char client_id;
  38. unsigned short txn_id;
  39. unsigned short type;
  40. unsigned short size;
  41. unsigned char *tlv;
  42. };
  43. #define qmi_ctl_client_id 0
  44. #define STATE_OFFLINE 0
  45. #define STATE_QUERYING 1
  46. #define STATE_ONLINE 2
  47. struct qmi_ctxt {
  48. struct miscdevice misc;
  49. struct mutex lock;
  50. unsigned char ctl_txn_id;
  51. unsigned char wds_client_id;
  52. unsigned short wds_txn_id;
  53. unsigned wds_busy;
  54. unsigned wds_handle;
  55. unsigned state_dirty;
  56. unsigned state;
  57. unsigned char addr[4];
  58. unsigned char mask[4];
  59. unsigned char gateway[4];
  60. unsigned char dns1[4];
  61. unsigned char dns2[4];
  62. smd_channel_t *ch;
  63. const char *ch_name;
  64. struct wake_lock wake_lock;
  65. struct work_struct open_work;
  66. struct work_struct read_work;
  67. };
  68. static struct qmi_ctxt *qmi_minor_to_ctxt(unsigned n);
  69. static void qmi_read_work(struct work_struct *ws);
  70. static void qmi_open_work(struct work_struct *work);
  71. void qmi_ctxt_init(struct qmi_ctxt *ctxt, unsigned n)
  72. {
  73. mutex_init(&ctxt->lock);
  74. INIT_WORK(&ctxt->read_work, qmi_read_work);
  75. INIT_WORK(&ctxt->open_work, qmi_open_work);
  76. wake_lock_init(&ctxt->wake_lock, WAKE_LOCK_SUSPEND, ctxt->misc.name);
  77. ctxt->ctl_txn_id = 1;
  78. ctxt->wds_txn_id = 1;
  79. ctxt->wds_busy = 1;
  80. ctxt->state = STATE_OFFLINE;
  81. }
  82. static struct workqueue_struct *qmi_wq;
  83. static int verbose = 0;
  84. /* anyone waiting for a state change waits here */
  85. static DECLARE_WAIT_QUEUE_HEAD(qmi_wait_queue);
  86. static void qmi_dump_msg(struct qmi_msg *msg, const char *prefix)
  87. {
  88. unsigned sz, n;
  89. unsigned char *x;
  90. if (!verbose)
  91. return;
  92. printk(KERN_INFO
  93. "qmi: %s: svc=%02x cid=%02x tid=%04x type=%04x size=%04x\n",
  94. prefix, msg->service, msg->client_id,
  95. msg->txn_id, msg->type, msg->size);
  96. x = msg->tlv;
  97. sz = msg->size;
  98. while (sz >= 3) {
  99. sz -= 3;
  100. n = x[1] | (x[2] << 8);
  101. if (n > sz)
  102. break;
  103. printk(KERN_INFO "qmi: %s: tlv: %02x %04x { ",
  104. prefix, x[0], n);
  105. x += 3;
  106. sz -= n;
  107. while (n-- > 0)
  108. printk("%02x ", *x++);
  109. printk("}\n");
  110. }
  111. }
  112. int qmi_add_tlv(struct qmi_msg *msg,
  113. unsigned type, unsigned size, const void *data)
  114. {
  115. unsigned char *x = msg->tlv + msg->size;
  116. x[0] = type;
  117. x[1] = size;
  118. x[2] = size >> 8;
  119. memcpy(x + 3, data, size);
  120. msg->size += (size + 3);
  121. return 0;
  122. }
  123. /* Extract a tagged item from a qmi message buffer,
  124. ** taking care not to overrun the buffer.
  125. */
  126. static int qmi_get_tlv(struct qmi_msg *msg,
  127. unsigned type, unsigned size, void *data)
  128. {
  129. unsigned char *x = msg->tlv;
  130. unsigned len = msg->size;
  131. unsigned n;
  132. while (len >= 3) {
  133. len -= 3;
  134. /* size of this item */
  135. n = x[1] | (x[2] << 8);
  136. if (n > len)
  137. break;
  138. if (x[0] == type) {
  139. if (n != size)
  140. return -1;
  141. memcpy(data, x + 3, size);
  142. return 0;
  143. }
  144. x += (n + 3);
  145. len -= n;
  146. }
  147. return -1;
  148. }
  149. static unsigned qmi_get_status(struct qmi_msg *msg, unsigned *error)
  150. {
  151. unsigned short status[2];
  152. if (qmi_get_tlv(msg, 0x02, sizeof(status), status)) {
  153. *error = 0;
  154. return QMI_RESULT_FAILURE;
  155. } else {
  156. *error = status[1];
  157. return status[0];
  158. }
  159. }
  160. /* 0x01 <qmux-header> <payload> */
  161. #define QMUX_HEADER 13
  162. /* should be >= HEADER + FOOTER */
  163. #define QMUX_OVERHEAD 16
  164. static int qmi_send(struct qmi_ctxt *ctxt, struct qmi_msg *msg)
  165. {
  166. unsigned char *data;
  167. unsigned hlen;
  168. unsigned len;
  169. int r;
  170. qmi_dump_msg(msg, "send");
  171. if (msg->service == QMI_CTL) {
  172. hlen = QMUX_HEADER - 1;
  173. } else {
  174. hlen = QMUX_HEADER;
  175. }
  176. /* QMUX length is total header + total payload - IFC selector */
  177. len = hlen + msg->size - 1;
  178. if (len > 0xffff)
  179. return -1;
  180. data = msg->tlv - hlen;
  181. /* prepend encap and qmux header */
  182. *data++ = 0x01; /* ifc selector */
  183. /* qmux header */
  184. *data++ = len;
  185. *data++ = len >> 8;
  186. *data++ = 0x00; /* flags: client */
  187. *data++ = msg->service;
  188. *data++ = msg->client_id;
  189. /* qmi header */
  190. *data++ = 0x00; /* flags: send */
  191. *data++ = msg->txn_id;
  192. if (msg->service != QMI_CTL)
  193. *data++ = msg->txn_id >> 8;
  194. *data++ = msg->type;
  195. *data++ = msg->type >> 8;
  196. *data++ = msg->size;
  197. *data++ = msg->size >> 8;
  198. /* len + 1 takes the interface selector into account */
  199. r = smd_write(ctxt->ch, msg->tlv - hlen, len + 1);
  200. if (r != len) {
  201. return -1;
  202. } else {
  203. return 0;
  204. }
  205. }
  206. static void qmi_process_ctl_msg(struct qmi_ctxt *ctxt, struct qmi_msg *msg)
  207. {
  208. unsigned err;
  209. if (msg->type == 0x0022) {
  210. unsigned char n[2];
  211. if (qmi_get_status(msg, &err))
  212. return;
  213. if (qmi_get_tlv(msg, 0x01, sizeof(n), n))
  214. return;
  215. if (n[0] == QMI_WDS) {
  216. printk(KERN_INFO
  217. "qmi: ctl: wds use client_id 0x%02x\n", n[1]);
  218. ctxt->wds_client_id = n[1];
  219. ctxt->wds_busy = 0;
  220. }
  221. }
  222. }
  223. static int qmi_network_get_profile(struct qmi_ctxt *ctxt);
  224. static void swapaddr(unsigned char *src, unsigned char *dst)
  225. {
  226. dst[0] = src[3];
  227. dst[1] = src[2];
  228. dst[2] = src[1];
  229. dst[3] = src[0];
  230. }
  231. static unsigned char zero[4];
  232. static void qmi_read_runtime_profile(struct qmi_ctxt *ctxt, struct qmi_msg *msg)
  233. {
  234. unsigned char tmp[4];
  235. unsigned r;
  236. r = qmi_get_tlv(msg, 0x1e, 4, tmp);
  237. swapaddr(r ? zero : tmp, ctxt->addr);
  238. r = qmi_get_tlv(msg, 0x21, 4, tmp);
  239. swapaddr(r ? zero : tmp, ctxt->mask);
  240. r = qmi_get_tlv(msg, 0x20, 4, tmp);
  241. swapaddr(r ? zero : tmp, ctxt->gateway);
  242. r = qmi_get_tlv(msg, 0x15, 4, tmp);
  243. swapaddr(r ? zero : tmp, ctxt->dns1);
  244. r = qmi_get_tlv(msg, 0x16, 4, tmp);
  245. swapaddr(r ? zero : tmp, ctxt->dns2);
  246. }
  247. static void qmi_process_unicast_wds_msg(struct qmi_ctxt *ctxt,
  248. struct qmi_msg *msg)
  249. {
  250. unsigned err;
  251. switch (msg->type) {
  252. case 0x0021:
  253. if (qmi_get_status(msg, &err)) {
  254. printk(KERN_ERR
  255. "qmi: wds: network stop failed (%04x)\n", err);
  256. } else {
  257. printk(KERN_INFO
  258. "qmi: wds: network stopped\n");
  259. ctxt->state = STATE_OFFLINE;
  260. ctxt->state_dirty = 1;
  261. }
  262. break;
  263. case 0x0020:
  264. if (qmi_get_status(msg, &err)) {
  265. printk(KERN_ERR
  266. "qmi: wds: network start failed (%04x)\n", err);
  267. } else if (qmi_get_tlv(msg, 0x01, sizeof(ctxt->wds_handle), &ctxt->wds_handle)) {
  268. printk(KERN_INFO
  269. "qmi: wds no handle?\n");
  270. } else {
  271. printk(KERN_INFO
  272. "qmi: wds: got handle 0x%08x\n",
  273. ctxt->wds_handle);
  274. }
  275. break;
  276. case 0x002D:
  277. printk("qmi: got network profile\n");
  278. if (ctxt->state == STATE_QUERYING) {
  279. qmi_read_runtime_profile(ctxt, msg);
  280. ctxt->state = STATE_ONLINE;
  281. ctxt->state_dirty = 1;
  282. }
  283. break;
  284. default:
  285. printk(KERN_ERR "qmi: unknown msg type 0x%04x\n", msg->type);
  286. }
  287. ctxt->wds_busy = 0;
  288. }
  289. static void qmi_process_broadcast_wds_msg(struct qmi_ctxt *ctxt,
  290. struct qmi_msg *msg)
  291. {
  292. if (msg->type == 0x0022) {
  293. unsigned char n[2];
  294. if (qmi_get_tlv(msg, 0x01, sizeof(n), n))
  295. return;
  296. switch (n[0]) {
  297. case 1:
  298. printk(KERN_INFO "qmi: wds: DISCONNECTED\n");
  299. ctxt->state = STATE_OFFLINE;
  300. ctxt->state_dirty = 1;
  301. break;
  302. case 2:
  303. printk(KERN_INFO "qmi: wds: CONNECTED\n");
  304. ctxt->state = STATE_QUERYING;
  305. ctxt->state_dirty = 1;
  306. qmi_network_get_profile(ctxt);
  307. break;
  308. case 3:
  309. printk(KERN_INFO "qmi: wds: SUSPENDED\n");
  310. ctxt->state = STATE_OFFLINE;
  311. ctxt->state_dirty = 1;
  312. }
  313. } else {
  314. printk(KERN_ERR "qmi: unknown bcast msg type 0x%04x\n", msg->type);
  315. }
  316. }
  317. static void qmi_process_wds_msg(struct qmi_ctxt *ctxt,
  318. struct qmi_msg *msg)
  319. {
  320. printk("wds: %04x @ %02x\n", msg->type, msg->client_id);
  321. if (msg->client_id == ctxt->wds_client_id) {
  322. qmi_process_unicast_wds_msg(ctxt, msg);
  323. } else if (msg->client_id == 0xff) {
  324. qmi_process_broadcast_wds_msg(ctxt, msg);
  325. } else {
  326. printk(KERN_ERR
  327. "qmi_process_wds_msg client id 0x%02x unknown\n",
  328. msg->client_id);
  329. }
  330. }
  331. static void qmi_process_qmux(struct qmi_ctxt *ctxt,
  332. unsigned char *buf, unsigned sz)
  333. {
  334. struct qmi_msg msg;
  335. /* require a full header */
  336. if (sz < 5)
  337. return;
  338. /* require a size that matches the buffer size */
  339. if (sz != (buf[0] | (buf[1] << 8)))
  340. return;
  341. /* only messages from a service (bit7=1) are allowed */
  342. if (buf[2] != 0x80)
  343. return;
  344. msg.service = buf[3];
  345. msg.client_id = buf[4];
  346. /* annoyingly, CTL messages have a shorter TID */
  347. if (buf[3] == 0) {
  348. if (sz < 7)
  349. return;
  350. msg.txn_id = buf[6];
  351. buf += 7;
  352. sz -= 7;
  353. } else {
  354. if (sz < 8)
  355. return;
  356. msg.txn_id = buf[6] | (buf[7] << 8);
  357. buf += 8;
  358. sz -= 8;
  359. }
  360. /* no type and size!? */
  361. if (sz < 4)
  362. return;
  363. sz -= 4;
  364. msg.type = buf[0] | (buf[1] << 8);
  365. msg.size = buf[2] | (buf[3] << 8);
  366. msg.tlv = buf + 4;
  367. if (sz != msg.size)
  368. return;
  369. qmi_dump_msg(&msg, "recv");
  370. mutex_lock(&ctxt->lock);
  371. switch (msg.service) {
  372. case QMI_CTL:
  373. qmi_process_ctl_msg(ctxt, &msg);
  374. break;
  375. case QMI_WDS:
  376. qmi_process_wds_msg(ctxt, &msg);
  377. break;
  378. default:
  379. printk(KERN_ERR "qmi: msg from unknown svc 0x%02x\n",
  380. msg.service);
  381. break;
  382. }
  383. mutex_unlock(&ctxt->lock);
  384. wake_up(&qmi_wait_queue);
  385. }
  386. #define QMI_MAX_PACKET (256 + QMUX_OVERHEAD)
  387. static void qmi_read_work(struct work_struct *ws)
  388. {
  389. struct qmi_ctxt *ctxt = container_of(ws, struct qmi_ctxt, read_work);
  390. struct smd_channel *ch = ctxt->ch;
  391. unsigned char buf[QMI_MAX_PACKET];
  392. int sz;
  393. for (;;) {
  394. sz = smd_cur_packet_size(ch);
  395. if (sz == 0)
  396. break;
  397. if (sz < smd_read_avail(ch))
  398. break;
  399. if (sz > QMI_MAX_PACKET) {
  400. smd_read(ch, 0, sz);
  401. continue;
  402. }
  403. if (smd_read(ch, buf, sz) != sz) {
  404. printk(KERN_ERR "qmi: not enough data?!\n");
  405. continue;
  406. }
  407. /* interface selector must be 1 */
  408. if (buf[0] != 0x01)
  409. continue;
  410. qmi_process_qmux(ctxt, buf + 1, sz - 1);
  411. }
  412. }
  413. static int qmi_request_wds_cid(struct qmi_ctxt *ctxt);
  414. static void qmi_open_work(struct work_struct *ws)
  415. {
  416. struct qmi_ctxt *ctxt = container_of(ws, struct qmi_ctxt, open_work);
  417. mutex_lock(&ctxt->lock);
  418. qmi_request_wds_cid(ctxt);
  419. mutex_unlock(&ctxt->lock);
  420. }
  421. static void qmi_notify(void *priv, unsigned event)
  422. {
  423. struct qmi_ctxt *ctxt = priv;
  424. switch (event) {
  425. case SMD_EVENT_DATA: {
  426. int sz;
  427. sz = smd_cur_packet_size(ctxt->ch);
  428. if ((sz > 0) && (sz <= smd_read_avail(ctxt->ch))) {
  429. wake_lock_timeout(&ctxt->wake_lock, HZ / 2);
  430. queue_work(qmi_wq, &ctxt->read_work);
  431. }
  432. break;
  433. }
  434. case SMD_EVENT_OPEN:
  435. printk(KERN_INFO "qmi: smd opened\n");
  436. queue_work(qmi_wq, &ctxt->open_work);
  437. break;
  438. case SMD_EVENT_CLOSE:
  439. printk(KERN_INFO "qmi: smd closed\n");
  440. break;
  441. }
  442. }
  443. static int qmi_request_wds_cid(struct qmi_ctxt *ctxt)
  444. {
  445. unsigned char data[64 + QMUX_OVERHEAD];
  446. struct qmi_msg msg;
  447. unsigned char n;
  448. msg.service = QMI_CTL;
  449. msg.client_id = qmi_ctl_client_id;
  450. msg.txn_id = ctxt->ctl_txn_id;
  451. msg.type = 0x0022;
  452. msg.size = 0;
  453. msg.tlv = data + QMUX_HEADER;
  454. ctxt->ctl_txn_id += 2;
  455. n = QMI_WDS;
  456. qmi_add_tlv(&msg, 0x01, 0x01, &n);
  457. return qmi_send(ctxt, &msg);
  458. }
  459. static int qmi_network_get_profile(struct qmi_ctxt *ctxt)
  460. {
  461. unsigned char data[96 + QMUX_OVERHEAD];
  462. struct qmi_msg msg;
  463. msg.service = QMI_WDS;
  464. msg.client_id = ctxt->wds_client_id;
  465. msg.txn_id = ctxt->wds_txn_id;
  466. msg.type = 0x002D;
  467. msg.size = 0;
  468. msg.tlv = data + QMUX_HEADER;
  469. ctxt->wds_txn_id += 2;
  470. return qmi_send(ctxt, &msg);
  471. }
  472. static int qmi_network_up(struct qmi_ctxt *ctxt, char *apn)
  473. {
  474. unsigned char data[96 + QMUX_OVERHEAD];
  475. struct qmi_msg msg;
  476. char *user;
  477. char *pass;
  478. for (user = apn; *user; user++) {
  479. if (*user == ' ') {
  480. *user++ = 0;
  481. break;
  482. }
  483. }
  484. for (pass = user; *pass; pass++) {
  485. if (*pass == ' ') {
  486. *pass++ = 0;
  487. break;
  488. }
  489. }
  490. msg.service = QMI_WDS;
  491. msg.client_id = ctxt->wds_client_id;
  492. msg.txn_id = ctxt->wds_txn_id;
  493. msg.type = 0x0020;
  494. msg.size = 0;
  495. msg.tlv = data + QMUX_HEADER;
  496. ctxt->wds_txn_id += 2;
  497. qmi_add_tlv(&msg, 0x14, strlen(apn), apn);
  498. if (*user) {
  499. unsigned char x;
  500. x = 3;
  501. qmi_add_tlv(&msg, 0x16, 1, &x);
  502. qmi_add_tlv(&msg, 0x17, strlen(user), user);
  503. if (*pass)
  504. qmi_add_tlv(&msg, 0x18, strlen(pass), pass);
  505. }
  506. return qmi_send(ctxt, &msg);
  507. }
  508. static int qmi_network_down(struct qmi_ctxt *ctxt)
  509. {
  510. unsigned char data[16 + QMUX_OVERHEAD];
  511. struct qmi_msg msg;
  512. msg.service = QMI_WDS;
  513. msg.client_id = ctxt->wds_client_id;
  514. msg.txn_id = ctxt->wds_txn_id;
  515. msg.type = 0x0021;
  516. msg.size = 0;
  517. msg.tlv = data + QMUX_HEADER;
  518. ctxt->wds_txn_id += 2;
  519. qmi_add_tlv(&msg, 0x01, sizeof(ctxt->wds_handle), &ctxt->wds_handle);
  520. return qmi_send(ctxt, &msg);
  521. }
  522. static int qmi_print_state(struct qmi_ctxt *ctxt, char *buf, int max)
  523. {
  524. int i;
  525. char *statename;
  526. if (ctxt->state == STATE_ONLINE) {
  527. statename = "up";
  528. } else if (ctxt->state == STATE_OFFLINE) {
  529. statename = "down";
  530. } else {
  531. statename = "busy";
  532. }
  533. i = scnprintf(buf, max, "STATE=%s\n", statename);
  534. i += scnprintf(buf + i, max - i, "CID=%d\n",ctxt->wds_client_id);
  535. if (ctxt->state != STATE_ONLINE){
  536. return i;
  537. }
  538. i += scnprintf(buf + i, max - i, "ADDR=%d.%d.%d.%d\n",
  539. ctxt->addr[0], ctxt->addr[1], ctxt->addr[2], ctxt->addr[3]);
  540. i += scnprintf(buf + i, max - i, "MASK=%d.%d.%d.%d\n",
  541. ctxt->mask[0], ctxt->mask[1], ctxt->mask[2], ctxt->mask[3]);
  542. i += scnprintf(buf + i, max - i, "GATEWAY=%d.%d.%d.%d\n",
  543. ctxt->gateway[0], ctxt->gateway[1], ctxt->gateway[2],
  544. ctxt->gateway[3]);
  545. i += scnprintf(buf + i, max - i, "DNS1=%d.%d.%d.%d\n",
  546. ctxt->dns1[0], ctxt->dns1[1], ctxt->dns1[2], ctxt->dns1[3]);
  547. i += scnprintf(buf + i, max - i, "DNS2=%d.%d.%d.%d\n",
  548. ctxt->dns2[0], ctxt->dns2[1], ctxt->dns2[2], ctxt->dns2[3]);
  549. return i;
  550. }
  551. static ssize_t qmi_read(struct file *fp, char __user *buf,
  552. size_t count, loff_t *pos)
  553. {
  554. struct qmi_ctxt *ctxt = fp->private_data;
  555. char msg[256];
  556. int len;
  557. int r;
  558. mutex_lock(&ctxt->lock);
  559. for (;;) {
  560. if (ctxt->state_dirty) {
  561. ctxt->state_dirty = 0;
  562. len = qmi_print_state(ctxt, msg, 256);
  563. break;
  564. }
  565. mutex_unlock(&ctxt->lock);
  566. r = wait_event_interruptible(qmi_wait_queue, ctxt->state_dirty);
  567. if (r < 0)
  568. return r;
  569. mutex_lock(&ctxt->lock);
  570. }
  571. mutex_unlock(&ctxt->lock);
  572. if (len > count)
  573. len = count;
  574. if (copy_to_user(buf, msg, len))
  575. return -EFAULT;
  576. return len;
  577. }
  578. static ssize_t qmi_write(struct file *fp, const char __user *buf,
  579. size_t count, loff_t *pos)
  580. {
  581. struct qmi_ctxt *ctxt = fp->private_data;
  582. unsigned char cmd[64];
  583. int len;
  584. int r;
  585. if (count < 1)
  586. return 0;
  587. len = count > 63 ? 63 : count;
  588. if (copy_from_user(cmd, buf, len))
  589. return -EFAULT;
  590. cmd[len] = 0;
  591. /* lazy */
  592. if (cmd[len-1] == '\n') {
  593. cmd[len-1] = 0;
  594. len--;
  595. }
  596. if (!strncmp(cmd, "verbose", 7)) {
  597. verbose = 1;
  598. } else if (!strncmp(cmd, "terse", 5)) {
  599. verbose = 0;
  600. } else if (!strncmp(cmd, "poll", 4)) {
  601. ctxt->state_dirty = 1;
  602. wake_up(&qmi_wait_queue);
  603. } else if (!strncmp(cmd, "down", 4)) {
  604. retry_down:
  605. mutex_lock(&ctxt->lock);
  606. if (ctxt->wds_busy) {
  607. mutex_unlock(&ctxt->lock);
  608. r = wait_event_interruptible(qmi_wait_queue, !ctxt->wds_busy);
  609. if (r < 0)
  610. return r;
  611. goto retry_down;
  612. }
  613. ctxt->wds_busy = 1;
  614. qmi_network_down(ctxt);
  615. mutex_unlock(&ctxt->lock);
  616. } else if (!strncmp(cmd, "up:", 3)) {
  617. retry_up:
  618. mutex_lock(&ctxt->lock);
  619. if (ctxt->wds_busy) {
  620. mutex_unlock(&ctxt->lock);
  621. r = wait_event_interruptible(qmi_wait_queue, !ctxt->wds_busy);
  622. if (r < 0)
  623. return r;
  624. goto retry_up;
  625. }
  626. ctxt->wds_busy = 1;
  627. qmi_network_up(ctxt, cmd+3);
  628. mutex_unlock(&ctxt->lock);
  629. } else {
  630. return -EINVAL;
  631. }
  632. return count;
  633. }
  634. static int qmi_open(struct inode *ip, struct file *fp)
  635. {
  636. struct qmi_ctxt *ctxt = qmi_minor_to_ctxt(MINOR(ip->i_rdev));
  637. int r = 0;
  638. if (!ctxt) {
  639. printk(KERN_ERR "unknown qmi misc %d\n", MINOR(ip->i_rdev));
  640. return -ENODEV;
  641. }
  642. fp->private_data = ctxt;
  643. mutex_lock(&ctxt->lock);
  644. if (ctxt->ch == 0)
  645. r = smd_open(ctxt->ch_name, &ctxt->ch, ctxt, qmi_notify);
  646. if (r == 0)
  647. wake_up(&qmi_wait_queue);
  648. mutex_unlock(&ctxt->lock);
  649. return r;
  650. }
  651. static int qmi_release(struct inode *ip, struct file *fp)
  652. {
  653. return 0;
  654. }
  655. static struct file_operations qmi_fops = {
  656. .owner = THIS_MODULE,
  657. .read = qmi_read,
  658. .write = qmi_write,
  659. .open = qmi_open,
  660. .release = qmi_release,
  661. };
  662. static struct qmi_ctxt qmi_device0 = {
  663. .ch_name = "SMD_DATA5_CNTL",
  664. .misc = {
  665. .minor = MISC_DYNAMIC_MINOR,
  666. .name = "qmi0",
  667. .fops = &qmi_fops,
  668. }
  669. };
  670. static struct qmi_ctxt qmi_device1 = {
  671. .ch_name = "SMD_DATA6_CNTL",
  672. .misc = {
  673. .minor = MISC_DYNAMIC_MINOR,
  674. .name = "qmi1",
  675. .fops = &qmi_fops,
  676. }
  677. };
  678. static struct qmi_ctxt qmi_device2 = {
  679. .ch_name = "SMD_DATA7_CNTL",
  680. .misc = {
  681. .minor = MISC_DYNAMIC_MINOR,
  682. .name = "qmi2",
  683. .fops = &qmi_fops,
  684. }
  685. };
  686. static struct qmi_ctxt *qmi_minor_to_ctxt(unsigned n)
  687. {
  688. if (n == qmi_device0.misc.minor)
  689. return &qmi_device0;
  690. if (n == qmi_device1.misc.minor)
  691. return &qmi_device1;
  692. if (n == qmi_device2.misc.minor)
  693. return &qmi_device2;
  694. return 0;
  695. }
  696. static int __init qmi_init(void)
  697. {
  698. int ret;
  699. qmi_wq = create_singlethread_workqueue("qmi");
  700. if (qmi_wq == 0)
  701. return -ENOMEM;
  702. qmi_ctxt_init(&qmi_device0, 0);
  703. qmi_ctxt_init(&qmi_device1, 1);
  704. qmi_ctxt_init(&qmi_device2, 2);
  705. ret = misc_register(&qmi_device0.misc);
  706. if (ret == 0)
  707. ret = misc_register(&qmi_device1.misc);
  708. if (ret == 0)
  709. ret = misc_register(&qmi_device2.misc);
  710. return ret;
  711. }
  712. module_init(qmi_init);