Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 2 | * Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/device.h> |
| 17 | #include <linux/delay.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/termios.h> |
| 20 | #include <mach/msm_smd.h> |
| 21 | #include <linux/debugfs.h> |
| 22 | #include <linux/bitops.h> |
| 23 | #include <linux/termios.h> |
| 24 | |
| 25 | #include "u_rmnet.h" |
| 26 | |
Anna Perel | 2151516 | 2012-02-02 20:50:02 +0200 | [diff] [blame] | 27 | #define NR_CTRL_SMD_PORTS 3 |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 28 | static int n_rmnet_ctrl_ports; |
Anna Perel | 2151516 | 2012-02-02 20:50:02 +0200 | [diff] [blame] | 29 | static char *rmnet_ctrl_names[] = {"DATA40_CNTL", "DATA39_CNTL", "DATA38_CNTL"}; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 30 | static struct workqueue_struct *grmnet_ctrl_wq; |
| 31 | |
| 32 | #define SMD_CH_MAX_LEN 20 |
| 33 | #define CH_OPENED 0 |
| 34 | #define CH_READY 1 |
| 35 | struct smd_ch_info { |
| 36 | struct smd_channel *ch; |
| 37 | char *name; |
| 38 | unsigned long flags; |
| 39 | wait_queue_head_t wait; |
| 40 | unsigned dtr; |
| 41 | |
| 42 | struct list_head tx_q; |
| 43 | unsigned long tx_len; |
| 44 | |
| 45 | struct work_struct read_w; |
| 46 | struct work_struct write_w; |
| 47 | |
| 48 | struct rmnet_ctrl_port *port; |
| 49 | |
| 50 | int cbits_tomodem; |
| 51 | /* stats */ |
| 52 | unsigned long to_modem; |
| 53 | unsigned long to_host; |
| 54 | }; |
| 55 | |
| 56 | struct rmnet_ctrl_port { |
| 57 | struct smd_ch_info ctrl_ch; |
| 58 | unsigned int port_num; |
| 59 | struct grmnet *port_usb; |
| 60 | |
| 61 | spinlock_t port_lock; |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 62 | struct delayed_work connect_w; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | static struct rmnet_ctrl_ports { |
| 66 | struct rmnet_ctrl_port *port; |
| 67 | struct platform_driver pdrv; |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 68 | } ctrl_smd_ports[NR_CTRL_SMD_PORTS]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 69 | |
| 70 | |
| 71 | /*---------------misc functions---------------- */ |
| 72 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 73 | static struct rmnet_ctrl_pkt *alloc_rmnet_ctrl_pkt(unsigned len, gfp_t flags) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 74 | { |
| 75 | struct rmnet_ctrl_pkt *pkt; |
| 76 | |
| 77 | pkt = kzalloc(sizeof(struct rmnet_ctrl_pkt), flags); |
| 78 | if (!pkt) |
| 79 | return ERR_PTR(-ENOMEM); |
| 80 | |
| 81 | pkt->buf = kmalloc(len, flags); |
| 82 | if (!pkt->buf) { |
| 83 | kfree(pkt); |
| 84 | return ERR_PTR(-ENOMEM); |
| 85 | } |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 86 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 87 | pkt->len = len; |
| 88 | |
| 89 | return pkt; |
| 90 | } |
| 91 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 92 | static void free_rmnet_ctrl_pkt(struct rmnet_ctrl_pkt *pkt) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 93 | { |
| 94 | kfree(pkt->buf); |
| 95 | kfree(pkt); |
| 96 | } |
| 97 | |
| 98 | /*--------------------------------------------- */ |
| 99 | |
| 100 | /*---------------control/smd channel functions---------------- */ |
| 101 | |
| 102 | static void grmnet_ctrl_smd_read_w(struct work_struct *w) |
| 103 | { |
| 104 | struct smd_ch_info *c = container_of(w, struct smd_ch_info, read_w); |
| 105 | struct rmnet_ctrl_port *port = c->port; |
| 106 | int sz; |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 107 | size_t len; |
| 108 | void *buf; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 109 | unsigned long flags; |
| 110 | |
Jack Pham | 1b236d1 | 2012-03-19 15:27:18 -0700 | [diff] [blame] | 111 | spin_lock_irqsave(&port->port_lock, flags); |
| 112 | while (c->ch) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 113 | sz = smd_cur_packet_size(c->ch); |
Jack Pham | 1b236d1 | 2012-03-19 15:27:18 -0700 | [diff] [blame] | 114 | if (sz <= 0) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 115 | break; |
| 116 | |
| 117 | if (smd_read_avail(c->ch) < sz) |
| 118 | break; |
| 119 | |
Jack Pham | 1b236d1 | 2012-03-19 15:27:18 -0700 | [diff] [blame] | 120 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 121 | |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 122 | buf = kmalloc(sz, GFP_KERNEL); |
| 123 | if (!buf) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 124 | return; |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 125 | |
| 126 | len = smd_read(c->ch, buf, sz); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | |
| 128 | /* send it to USB here */ |
| 129 | spin_lock_irqsave(&port->port_lock, flags); |
| 130 | if (port->port_usb && port->port_usb->send_cpkt_response) { |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 131 | port->port_usb->send_cpkt_response(port->port_usb, |
| 132 | buf, len); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 133 | c->to_host++; |
| 134 | } |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 135 | kfree(buf); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 136 | } |
Jack Pham | 1b236d1 | 2012-03-19 15:27:18 -0700 | [diff] [blame] | 137 | spin_unlock_irqrestore(&port->port_lock, flags); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static void grmnet_ctrl_smd_write_w(struct work_struct *w) |
| 141 | { |
| 142 | struct smd_ch_info *c = container_of(w, struct smd_ch_info, write_w); |
| 143 | struct rmnet_ctrl_port *port = c->port; |
| 144 | unsigned long flags; |
| 145 | struct rmnet_ctrl_pkt *cpkt; |
| 146 | int ret; |
| 147 | |
| 148 | spin_lock_irqsave(&port->port_lock, flags); |
Jack Pham | 1b236d1 | 2012-03-19 15:27:18 -0700 | [diff] [blame] | 149 | while (c->ch) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 150 | if (list_empty(&c->tx_q)) |
| 151 | break; |
| 152 | |
| 153 | cpkt = list_first_entry(&c->tx_q, struct rmnet_ctrl_pkt, list); |
| 154 | |
| 155 | if (smd_write_avail(c->ch) < cpkt->len) |
| 156 | break; |
| 157 | |
| 158 | list_del(&cpkt->list); |
| 159 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 160 | ret = smd_write(c->ch, cpkt->buf, cpkt->len); |
| 161 | spin_lock_irqsave(&port->port_lock, flags); |
| 162 | if (ret != cpkt->len) { |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 163 | pr_err("%s: smd_write failed err:%d\n", __func__, ret); |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 164 | free_rmnet_ctrl_pkt(cpkt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 165 | break; |
| 166 | } |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 167 | free_rmnet_ctrl_pkt(cpkt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 168 | c->to_modem++; |
| 169 | } |
| 170 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 171 | } |
| 172 | |
| 173 | static int |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 174 | grmnet_ctrl_smd_send_cpkt_tomodem(u8 portno, |
| 175 | void *buf, size_t len) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 176 | { |
| 177 | unsigned long flags; |
| 178 | struct rmnet_ctrl_port *port; |
| 179 | struct smd_ch_info *c; |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 180 | struct rmnet_ctrl_pkt *cpkt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 181 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 182 | if (portno >= n_rmnet_ctrl_ports) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 183 | pr_err("%s: Invalid portno#%d\n", __func__, portno); |
| 184 | return -ENODEV; |
| 185 | } |
| 186 | |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 187 | port = ctrl_smd_ports[portno].port; |
| 188 | |
| 189 | cpkt = alloc_rmnet_ctrl_pkt(len, GFP_ATOMIC); |
| 190 | if (IS_ERR(cpkt)) { |
| 191 | pr_err("%s: Unable to allocate ctrl pkt\n", __func__); |
| 192 | return -ENOMEM; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 195 | memcpy(cpkt->buf, buf, len); |
| 196 | cpkt->len = len; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 197 | |
| 198 | spin_lock_irqsave(&port->port_lock, flags); |
| 199 | c = &port->ctrl_ch; |
| 200 | |
| 201 | /* drop cpkt if ch is not open */ |
| 202 | if (!test_bit(CH_OPENED, &c->flags)) { |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 203 | free_rmnet_ctrl_pkt(cpkt); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 205 | return 0; |
| 206 | } |
| 207 | |
| 208 | list_add_tail(&cpkt->list, &c->tx_q); |
| 209 | queue_work(grmnet_ctrl_wq, &c->write_w); |
| 210 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 215 | #define RMNET_CTRL_DTR 0x01 |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 216 | static void |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 217 | gsmd_ctrl_send_cbits_tomodem(void *gptr, u8 portno, int cbits) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 218 | { |
| 219 | struct rmnet_ctrl_port *port; |
| 220 | struct smd_ch_info *c; |
| 221 | int set_bits = 0; |
| 222 | int clear_bits = 0; |
| 223 | int temp = 0; |
| 224 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 225 | if (portno >= n_rmnet_ctrl_ports) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 226 | pr_err("%s: Invalid portno#%d\n", __func__, portno); |
| 227 | return; |
| 228 | } |
| 229 | |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 230 | if (!gptr) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 231 | pr_err("%s: grmnet is null\n", __func__); |
| 232 | return; |
| 233 | } |
| 234 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 235 | port = ctrl_smd_ports[portno].port; |
| 236 | cbits = cbits & RMNET_CTRL_DTR; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 237 | c = &port->ctrl_ch; |
| 238 | |
| 239 | /* host driver will only send DTR, but to have generic |
| 240 | * set and clear bit implementation using two separate |
| 241 | * checks |
| 242 | */ |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 243 | if (cbits & RMNET_CTRL_DTR) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 244 | set_bits |= TIOCM_DTR; |
| 245 | else |
| 246 | clear_bits |= TIOCM_DTR; |
| 247 | |
| 248 | temp |= set_bits; |
| 249 | temp &= ~clear_bits; |
| 250 | |
| 251 | if (temp == c->cbits_tomodem) |
| 252 | return; |
| 253 | |
| 254 | c->cbits_tomodem = temp; |
| 255 | |
| 256 | if (!test_bit(CH_OPENED, &c->flags)) |
| 257 | return; |
| 258 | |
| 259 | pr_debug("%s: ctrl_tomodem:%d ctrl_bits:%d setbits:%d clearbits:%d\n", |
| 260 | __func__, temp, cbits, set_bits, clear_bits); |
| 261 | |
| 262 | smd_tiocmset(c->ch, set_bits, clear_bits); |
| 263 | } |
| 264 | |
| 265 | static char *get_smd_event(unsigned event) |
| 266 | { |
| 267 | switch (event) { |
| 268 | case SMD_EVENT_DATA: |
| 269 | return "DATA"; |
| 270 | case SMD_EVENT_OPEN: |
| 271 | return "OPEN"; |
| 272 | case SMD_EVENT_CLOSE: |
| 273 | return "CLOSE"; |
| 274 | } |
| 275 | |
| 276 | return "UNDEFINED"; |
| 277 | } |
| 278 | |
| 279 | static void grmnet_ctrl_smd_notify(void *p, unsigned event) |
| 280 | { |
| 281 | struct rmnet_ctrl_port *port = p; |
| 282 | struct smd_ch_info *c = &port->ctrl_ch; |
Vamsi Krishna | 9e9921a | 2011-10-04 16:09:31 -0700 | [diff] [blame] | 283 | struct rmnet_ctrl_pkt *cpkt; |
| 284 | unsigned long flags; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 285 | |
| 286 | pr_debug("%s: EVENT_(%s)\n", __func__, get_smd_event(event)); |
| 287 | |
| 288 | switch (event) { |
| 289 | case SMD_EVENT_DATA: |
| 290 | if (smd_read_avail(c->ch)) |
| 291 | queue_work(grmnet_ctrl_wq, &c->read_w); |
| 292 | if (smd_write_avail(c->ch)) |
| 293 | queue_work(grmnet_ctrl_wq, &c->write_w); |
| 294 | break; |
| 295 | case SMD_EVENT_OPEN: |
| 296 | set_bit(CH_OPENED, &c->flags); |
Vamsi Krishna | 9e9921a | 2011-10-04 16:09:31 -0700 | [diff] [blame] | 297 | |
| 298 | if (port && port->port_usb && port->port_usb->connect) |
| 299 | port->port_usb->connect(port->port_usb); |
| 300 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 301 | break; |
| 302 | case SMD_EVENT_CLOSE: |
| 303 | clear_bit(CH_OPENED, &c->flags); |
Vamsi Krishna | 9e9921a | 2011-10-04 16:09:31 -0700 | [diff] [blame] | 304 | |
| 305 | if (port && port->port_usb && port->port_usb->disconnect) |
| 306 | port->port_usb->disconnect(port->port_usb); |
| 307 | |
| 308 | spin_lock_irqsave(&port->port_lock, flags); |
| 309 | while (!list_empty(&c->tx_q)) { |
| 310 | cpkt = list_first_entry(&c->tx_q, |
| 311 | struct rmnet_ctrl_pkt, list); |
| 312 | |
| 313 | list_del(&cpkt->list); |
| 314 | free_rmnet_ctrl_pkt(cpkt); |
| 315 | } |
| 316 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 317 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 318 | break; |
| 319 | } |
| 320 | } |
| 321 | /*------------------------------------------------------------ */ |
| 322 | |
| 323 | static void grmnet_ctrl_smd_connect_w(struct work_struct *w) |
| 324 | { |
| 325 | struct rmnet_ctrl_port *port = |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 326 | container_of(w, struct rmnet_ctrl_port, connect_w.work); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 327 | struct smd_ch_info *c = &port->ctrl_ch; |
| 328 | unsigned long flags; |
Amit Blay | c2b0179 | 2012-07-11 18:37:35 +0300 | [diff] [blame^] | 329 | int set_bits = 0; |
| 330 | int clear_bits = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 331 | int ret; |
| 332 | |
| 333 | pr_debug("%s:\n", __func__); |
| 334 | |
| 335 | if (!test_bit(CH_READY, &c->flags)) |
| 336 | return; |
| 337 | |
| 338 | ret = smd_open(c->name, &c->ch, port, grmnet_ctrl_smd_notify); |
| 339 | if (ret) { |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 340 | if (ret == -EAGAIN) { |
| 341 | /* port not ready - retry */ |
| 342 | pr_debug("%s: SMD port not ready - rescheduling:%s err:%d\n", |
| 343 | __func__, c->name, ret); |
| 344 | queue_delayed_work(grmnet_ctrl_wq, &port->connect_w, |
| 345 | msecs_to_jiffies(250)); |
| 346 | } else { |
| 347 | pr_err("%s: unable to open smd port:%s err:%d\n", |
| 348 | __func__, c->name, ret); |
| 349 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 350 | return; |
| 351 | } |
| 352 | |
Amit Blay | c2b0179 | 2012-07-11 18:37:35 +0300 | [diff] [blame^] | 353 | set_bits = c->cbits_tomodem; |
| 354 | clear_bits = ~(c->cbits_tomodem | TIOCM_RTS); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 355 | spin_lock_irqsave(&port->port_lock, flags); |
| 356 | if (port->port_usb) |
Amit Blay | c2b0179 | 2012-07-11 18:37:35 +0300 | [diff] [blame^] | 357 | smd_tiocmset(c->ch, set_bits, clear_bits); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 358 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 359 | } |
| 360 | |
| 361 | int gsmd_ctrl_connect(struct grmnet *gr, int port_num) |
| 362 | { |
| 363 | struct rmnet_ctrl_port *port; |
| 364 | struct smd_ch_info *c; |
| 365 | unsigned long flags; |
| 366 | |
| 367 | pr_debug("%s: grmnet:%p port#%d\n", __func__, gr, port_num); |
| 368 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 369 | if (port_num >= n_rmnet_ctrl_ports) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 370 | pr_err("%s: invalid portno#%d\n", __func__, port_num); |
| 371 | return -ENODEV; |
| 372 | } |
| 373 | |
| 374 | if (!gr) { |
| 375 | pr_err("%s: grmnet port is null\n", __func__); |
| 376 | return -ENODEV; |
| 377 | } |
| 378 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 379 | port = ctrl_smd_ports[port_num].port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 380 | c = &port->ctrl_ch; |
| 381 | |
| 382 | spin_lock_irqsave(&port->port_lock, flags); |
| 383 | port->port_usb = gr; |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 384 | gr->send_encap_cmd = grmnet_ctrl_smd_send_cpkt_tomodem; |
| 385 | gr->notify_modem = gsmd_ctrl_send_cbits_tomodem; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 386 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 387 | |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 388 | queue_delayed_work(grmnet_ctrl_wq, &port->connect_w, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 389 | |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | void gsmd_ctrl_disconnect(struct grmnet *gr, u8 port_num) |
| 394 | { |
| 395 | struct rmnet_ctrl_port *port; |
| 396 | unsigned long flags; |
| 397 | struct smd_ch_info *c; |
Vamsi Krishna | 9e9921a | 2011-10-04 16:09:31 -0700 | [diff] [blame] | 398 | struct rmnet_ctrl_pkt *cpkt; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 399 | |
| 400 | pr_debug("%s: grmnet:%p port#%d\n", __func__, gr, port_num); |
| 401 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 402 | if (port_num >= n_rmnet_ctrl_ports) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 403 | pr_err("%s: invalid portno#%d\n", __func__, port_num); |
| 404 | return; |
| 405 | } |
| 406 | |
| 407 | if (!gr) { |
| 408 | pr_err("%s: grmnet port is null\n", __func__); |
| 409 | return; |
| 410 | } |
| 411 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 412 | port = ctrl_smd_ports[port_num].port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 413 | c = &port->ctrl_ch; |
| 414 | |
| 415 | spin_lock_irqsave(&port->port_lock, flags); |
| 416 | port->port_usb = 0; |
Hemant Kumar | f60c025 | 2011-11-03 12:37:07 -0700 | [diff] [blame] | 417 | gr->send_encap_cmd = 0; |
| 418 | gr->notify_modem = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 419 | c->cbits_tomodem = 0; |
Vamsi Krishna | 9e9921a | 2011-10-04 16:09:31 -0700 | [diff] [blame] | 420 | |
| 421 | while (!list_empty(&c->tx_q)) { |
| 422 | cpkt = list_first_entry(&c->tx_q, struct rmnet_ctrl_pkt, list); |
| 423 | |
| 424 | list_del(&cpkt->list); |
| 425 | free_rmnet_ctrl_pkt(cpkt); |
| 426 | } |
| 427 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 428 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 429 | |
Jack Pham | c02d481 | 2012-01-25 15:52:04 -0800 | [diff] [blame] | 430 | if (test_and_clear_bit(CH_OPENED, &c->flags)) |
| 431 | /* send dtr zero */ |
| 432 | smd_tiocmset(c->ch, c->cbits_tomodem, ~c->cbits_tomodem); |
| 433 | |
| 434 | if (c->ch) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 435 | smd_close(c->ch); |
Jack Pham | c02d481 | 2012-01-25 15:52:04 -0800 | [diff] [blame] | 436 | c->ch = NULL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 437 | } |
| 438 | } |
| 439 | |
| 440 | #define SMD_CH_MAX_LEN 20 |
| 441 | static int grmnet_ctrl_smd_ch_probe(struct platform_device *pdev) |
| 442 | { |
| 443 | struct rmnet_ctrl_port *port; |
| 444 | struct smd_ch_info *c; |
| 445 | int i; |
| 446 | unsigned long flags; |
| 447 | |
| 448 | pr_debug("%s: name:%s\n", __func__, pdev->name); |
| 449 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 450 | for (i = 0; i < n_rmnet_ctrl_ports; i++) { |
| 451 | port = ctrl_smd_ports[i].port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 452 | c = &port->ctrl_ch; |
| 453 | |
| 454 | if (!strncmp(c->name, pdev->name, SMD_CH_MAX_LEN)) { |
| 455 | set_bit(CH_READY, &c->flags); |
| 456 | |
| 457 | /* if usb is online, try opening smd_ch */ |
| 458 | spin_lock_irqsave(&port->port_lock, flags); |
| 459 | if (port->port_usb) |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 460 | queue_delayed_work(grmnet_ctrl_wq, |
| 461 | &port->connect_w, 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 462 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 463 | |
| 464 | break; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | static int grmnet_ctrl_smd_ch_remove(struct platform_device *pdev) |
| 472 | { |
| 473 | struct rmnet_ctrl_port *port; |
| 474 | struct smd_ch_info *c; |
| 475 | int i; |
| 476 | |
| 477 | pr_debug("%s: name:%s\n", __func__, pdev->name); |
| 478 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 479 | for (i = 0; i < n_rmnet_ctrl_ports; i++) { |
| 480 | port = ctrl_smd_ports[i].port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 481 | c = &port->ctrl_ch; |
| 482 | |
| 483 | if (!strncmp(c->name, pdev->name, SMD_CH_MAX_LEN)) { |
| 484 | clear_bit(CH_READY, &c->flags); |
| 485 | clear_bit(CH_OPENED, &c->flags); |
Jack Pham | c02d481 | 2012-01-25 15:52:04 -0800 | [diff] [blame] | 486 | if (c->ch) { |
| 487 | smd_close(c->ch); |
| 488 | c->ch = NULL; |
| 489 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 490 | break; |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | |
| 498 | static void grmnet_ctrl_smd_port_free(int portno) |
| 499 | { |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 500 | struct rmnet_ctrl_port *port = ctrl_smd_ports[portno].port; |
Jack Pham | effd4ae | 2011-08-03 16:49:36 -0700 | [diff] [blame] | 501 | struct platform_driver *pdrv = &ctrl_smd_ports[portno].pdrv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 502 | |
Jack Pham | effd4ae | 2011-08-03 16:49:36 -0700 | [diff] [blame] | 503 | if (port) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 504 | kfree(port); |
Jack Pham | effd4ae | 2011-08-03 16:49:36 -0700 | [diff] [blame] | 505 | platform_driver_unregister(pdrv); |
| 506 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | static int grmnet_ctrl_smd_port_alloc(int portno) |
| 510 | { |
| 511 | struct rmnet_ctrl_port *port; |
| 512 | struct smd_ch_info *c; |
| 513 | struct platform_driver *pdrv; |
| 514 | |
| 515 | port = kzalloc(sizeof(struct rmnet_ctrl_port), GFP_KERNEL); |
| 516 | if (!port) |
| 517 | return -ENOMEM; |
| 518 | |
| 519 | port->port_num = portno; |
| 520 | |
| 521 | spin_lock_init(&port->port_lock); |
Chiranjeevi Velempati | 40608bb | 2012-01-27 14:09:47 +0530 | [diff] [blame] | 522 | INIT_DELAYED_WORK(&port->connect_w, grmnet_ctrl_smd_connect_w); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 523 | |
| 524 | c = &port->ctrl_ch; |
| 525 | c->name = rmnet_ctrl_names[portno]; |
| 526 | c->port = port; |
| 527 | init_waitqueue_head(&c->wait); |
| 528 | INIT_LIST_HEAD(&c->tx_q); |
| 529 | INIT_WORK(&c->read_w, grmnet_ctrl_smd_read_w); |
| 530 | INIT_WORK(&c->write_w, grmnet_ctrl_smd_write_w); |
| 531 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 532 | ctrl_smd_ports[portno].port = port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 533 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 534 | pdrv = &ctrl_smd_ports[portno].pdrv; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 535 | pdrv->probe = grmnet_ctrl_smd_ch_probe; |
| 536 | pdrv->remove = grmnet_ctrl_smd_ch_remove; |
| 537 | pdrv->driver.name = c->name; |
| 538 | pdrv->driver.owner = THIS_MODULE; |
| 539 | |
| 540 | platform_driver_register(pdrv); |
| 541 | |
| 542 | pr_debug("%s: port:%p portno:%d\n", __func__, port, portno); |
| 543 | |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | int gsmd_ctrl_setup(unsigned int count) |
| 548 | { |
| 549 | int i; |
| 550 | int ret; |
| 551 | |
| 552 | pr_debug("%s: requested ports:%d\n", __func__, count); |
| 553 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 554 | if (!count || count > NR_CTRL_SMD_PORTS) { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 555 | pr_err("%s: Invalid num of ports count:%d\n", |
| 556 | __func__, count); |
| 557 | return -EINVAL; |
| 558 | } |
| 559 | |
| 560 | grmnet_ctrl_wq = alloc_workqueue("gsmd_ctrl", |
| 561 | WQ_UNBOUND | WQ_MEM_RECLAIM, 1); |
| 562 | if (!grmnet_ctrl_wq) { |
| 563 | pr_err("%s: Unable to create workqueue grmnet_ctrl\n", |
| 564 | __func__); |
| 565 | return -ENOMEM; |
| 566 | } |
| 567 | |
| 568 | for (i = 0; i < count; i++) { |
Ofir Cohen | a1c2a87 | 2011-12-14 10:26:34 +0200 | [diff] [blame] | 569 | n_rmnet_ctrl_ports++; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 570 | ret = grmnet_ctrl_smd_port_alloc(i); |
| 571 | if (ret) { |
| 572 | pr_err("%s: Unable to alloc port:%d\n", __func__, i); |
Ofir Cohen | a1c2a87 | 2011-12-14 10:26:34 +0200 | [diff] [blame] | 573 | n_rmnet_ctrl_ports--; |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 574 | goto free_ctrl_smd_ports; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 575 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | return 0; |
| 579 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 580 | free_ctrl_smd_ports: |
| 581 | for (i = 0; i < n_rmnet_ctrl_ports; i++) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 582 | grmnet_ctrl_smd_port_free(i); |
| 583 | |
| 584 | destroy_workqueue(grmnet_ctrl_wq); |
| 585 | |
| 586 | return ret; |
| 587 | } |
| 588 | |
| 589 | #if defined(CONFIG_DEBUG_FS) |
| 590 | #define DEBUG_BUF_SIZE 1024 |
| 591 | static ssize_t gsmd_ctrl_read_stats(struct file *file, char __user *ubuf, |
| 592 | size_t count, loff_t *ppos) |
| 593 | { |
| 594 | struct rmnet_ctrl_port *port; |
| 595 | struct smd_ch_info *c; |
| 596 | char *buf; |
| 597 | unsigned long flags; |
| 598 | int ret; |
| 599 | int i; |
| 600 | int temp = 0; |
| 601 | |
| 602 | buf = kzalloc(sizeof(char) * DEBUG_BUF_SIZE, GFP_KERNEL); |
| 603 | if (!buf) |
| 604 | return -ENOMEM; |
| 605 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 606 | for (i = 0; i < n_rmnet_ctrl_ports; i++) { |
| 607 | port = ctrl_smd_ports[i].port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 608 | if (!port) |
| 609 | continue; |
| 610 | spin_lock_irqsave(&port->port_lock, flags); |
| 611 | |
| 612 | c = &port->ctrl_ch; |
| 613 | |
| 614 | temp += scnprintf(buf + temp, DEBUG_BUF_SIZE - temp, |
| 615 | "#PORT:%d port:%p ctrl_ch:%p#\n" |
| 616 | "to_usbhost: %lu\n" |
| 617 | "to_modem: %lu\n" |
| 618 | "DTR: %s\n" |
| 619 | "ch_open: %d\n" |
| 620 | "ch_ready: %d\n" |
| 621 | "read_avail: %d\n" |
| 622 | "write_avail:%d\n", |
| 623 | i, port, &port->ctrl_ch, |
| 624 | c->to_host, c->to_modem, |
| 625 | c->cbits_tomodem ? "HIGH" : "LOW", |
| 626 | test_bit(CH_OPENED, &c->flags), |
| 627 | test_bit(CH_READY, &c->flags), |
Jack Pham | c02d481 | 2012-01-25 15:52:04 -0800 | [diff] [blame] | 628 | c->ch ? smd_read_avail(c->ch) : 0, |
| 629 | c->ch ? smd_write_avail(c->ch) : 0); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 630 | |
| 631 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 632 | } |
| 633 | |
| 634 | ret = simple_read_from_buffer(ubuf, count, ppos, buf, temp); |
| 635 | |
| 636 | kfree(buf); |
| 637 | |
| 638 | return ret; |
| 639 | } |
| 640 | |
| 641 | static ssize_t gsmd_ctrl_reset_stats(struct file *file, const char __user *buf, |
| 642 | size_t count, loff_t *ppos) |
| 643 | { |
| 644 | struct rmnet_ctrl_port *port; |
| 645 | struct smd_ch_info *c; |
| 646 | int i; |
| 647 | unsigned long flags; |
| 648 | |
Manu Gautam | 2b0234a | 2011-09-07 16:47:52 +0530 | [diff] [blame] | 649 | for (i = 0; i < n_rmnet_ctrl_ports; i++) { |
| 650 | port = ctrl_smd_ports[i].port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 651 | if (!port) |
| 652 | continue; |
| 653 | |
| 654 | spin_lock_irqsave(&port->port_lock, flags); |
| 655 | |
| 656 | c = &port->ctrl_ch; |
| 657 | |
| 658 | c->to_host = 0; |
| 659 | c->to_modem = 0; |
| 660 | |
| 661 | spin_unlock_irqrestore(&port->port_lock, flags); |
| 662 | } |
| 663 | return count; |
| 664 | } |
| 665 | |
| 666 | const struct file_operations gsmd_ctrl_stats_ops = { |
| 667 | .read = gsmd_ctrl_read_stats, |
| 668 | .write = gsmd_ctrl_reset_stats, |
| 669 | }; |
| 670 | |
| 671 | struct dentry *smd_ctrl_dent; |
| 672 | struct dentry *smd_ctrl_dfile; |
| 673 | static void gsmd_ctrl_debugfs_init(void) |
| 674 | { |
| 675 | smd_ctrl_dent = debugfs_create_dir("usb_rmnet_ctrl_smd", 0); |
| 676 | if (IS_ERR(smd_ctrl_dent)) |
| 677 | return; |
| 678 | |
| 679 | smd_ctrl_dfile = debugfs_create_file("status", 0444, smd_ctrl_dent, 0, |
| 680 | &gsmd_ctrl_stats_ops); |
| 681 | if (!smd_ctrl_dfile || IS_ERR(smd_ctrl_dfile)) |
| 682 | debugfs_remove(smd_ctrl_dent); |
| 683 | } |
| 684 | |
| 685 | static void gsmd_ctrl_debugfs_exit(void) |
| 686 | { |
| 687 | debugfs_remove(smd_ctrl_dfile); |
| 688 | debugfs_remove(smd_ctrl_dent); |
| 689 | } |
| 690 | |
| 691 | #else |
| 692 | static void gsmd_ctrl_debugfs_init(void) { } |
| 693 | static void gsmd_ctrl_debugfs_exit(void) { } |
| 694 | #endif |
| 695 | |
| 696 | static int __init gsmd_ctrl_init(void) |
| 697 | { |
| 698 | gsmd_ctrl_debugfs_init(); |
| 699 | |
| 700 | return 0; |
| 701 | } |
| 702 | module_init(gsmd_ctrl_init); |
| 703 | |
| 704 | static void __exit gsmd_ctrl_exit(void) |
| 705 | { |
| 706 | gsmd_ctrl_debugfs_exit(); |
| 707 | } |
| 708 | module_exit(gsmd_ctrl_exit); |
| 709 | MODULE_DESCRIPTION("smd control driver"); |
| 710 | MODULE_LICENSE("GPL v2"); |