Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 1 | /* This is the serial hardware link layer (HLL) for the Gigaset 307x isdn |
| 2 | * DECT base (aka Sinus 45 isdn) using the RS232 DECT data module M101, |
| 3 | * written as a line discipline. |
| 4 | * |
| 5 | * ===================================================================== |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License as |
| 8 | * published by the Free Software Foundation; either version 2 of |
| 9 | * the License, or (at your option) any later version. |
| 10 | * ===================================================================== |
| 11 | */ |
| 12 | |
| 13 | #include "gigaset.h" |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/moduleparam.h> |
| 16 | #include <linux/platform_device.h> |
Tilman Schmidt | ee51ef0 | 2008-02-06 01:38:30 -0800 | [diff] [blame] | 17 | #include <linux/completion.h> |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 18 | |
| 19 | /* Version Information */ |
| 20 | #define DRIVER_AUTHOR "Tilman Schmidt" |
| 21 | #define DRIVER_DESC "Serial Driver for Gigaset 307x using Siemens M101" |
| 22 | |
| 23 | #define GIGASET_MINORS 1 |
| 24 | #define GIGASET_MINOR 0 |
| 25 | #define GIGASET_MODULENAME "ser_gigaset" |
| 26 | #define GIGASET_DEVNAME "ttyGS" |
| 27 | |
| 28 | /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */ |
| 29 | #define IF_WRITEBUF 264 |
| 30 | |
| 31 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 32 | MODULE_DESCRIPTION(DRIVER_DESC); |
| 33 | MODULE_LICENSE("GPL"); |
| 34 | MODULE_ALIAS_LDISC(N_GIGASET_M101); |
| 35 | |
| 36 | static int startmode = SM_ISDN; |
| 37 | module_param(startmode, int, S_IRUGO); |
| 38 | MODULE_PARM_DESC(startmode, "initial operation mode"); |
| 39 | static int cidmode = 1; |
| 40 | module_param(cidmode, int, S_IRUGO); |
| 41 | MODULE_PARM_DESC(cidmode, "stay in CID mode when idle"); |
| 42 | |
| 43 | static struct gigaset_driver *driver; |
| 44 | |
| 45 | struct ser_cardstate { |
| 46 | struct platform_device dev; |
| 47 | struct tty_struct *tty; |
| 48 | atomic_t refcnt; |
Tilman Schmidt | ee51ef0 | 2008-02-06 01:38:30 -0800 | [diff] [blame] | 49 | struct completion dead_cmp; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | static struct platform_driver device_driver = { |
| 53 | .driver = { |
| 54 | .name = GIGASET_MODULENAME, |
| 55 | }, |
| 56 | }; |
| 57 | |
| 58 | static void flush_send_queue(struct cardstate *); |
| 59 | |
| 60 | /* transmit data from current open skb |
| 61 | * result: number of bytes sent or error code < 0 |
| 62 | */ |
| 63 | static int write_modem(struct cardstate *cs) |
| 64 | { |
| 65 | struct tty_struct *tty = cs->hw.ser->tty; |
| 66 | struct bc_state *bcs = &cs->bcs[0]; /* only one channel */ |
| 67 | struct sk_buff *skb = bcs->tx_skb; |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 68 | int sent = -EOPNOTSUPP; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 69 | |
Alan Cox | ede03d3 | 2015-12-15 18:11:29 +0100 | [diff] [blame] | 70 | WARN_ON(!tty || !tty->ops || !skb); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 71 | |
| 72 | if (!skb->len) { |
| 73 | dev_kfree_skb_any(skb); |
| 74 | bcs->tx_skb = NULL; |
| 75 | return -EINVAL; |
| 76 | } |
| 77 | |
| 78 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 79 | if (tty->ops->write) |
| 80 | sent = tty->ops->write(tty, skb->data, skb->len); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 81 | gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent); |
| 82 | if (sent < 0) { |
| 83 | /* error */ |
| 84 | flush_send_queue(cs); |
| 85 | return sent; |
| 86 | } |
| 87 | skb_pull(skb, sent); |
| 88 | if (!skb->len) { |
| 89 | /* skb sent completely */ |
| 90 | gigaset_skb_sent(bcs, skb); |
| 91 | |
| 92 | gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!", |
| 93 | (unsigned long) skb); |
| 94 | dev_kfree_skb_any(skb); |
| 95 | bcs->tx_skb = NULL; |
| 96 | } |
| 97 | return sent; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * transmit first queued command buffer |
| 102 | * result: number of bytes sent or error code < 0 |
| 103 | */ |
| 104 | static int send_cb(struct cardstate *cs) |
| 105 | { |
| 106 | struct tty_struct *tty = cs->hw.ser->tty; |
| 107 | struct cmdbuf_t *cb, *tcb; |
| 108 | unsigned long flags; |
| 109 | int sent = 0; |
| 110 | |
Alan Cox | ede03d3 | 2015-12-15 18:11:29 +0100 | [diff] [blame] | 111 | WARN_ON(!tty || !tty->ops); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 112 | |
| 113 | cb = cs->cmdbuf; |
| 114 | if (!cb) |
| 115 | return 0; /* nothing to do */ |
| 116 | |
| 117 | if (cb->len) { |
| 118 | set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 119 | sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 120 | if (sent < 0) { |
| 121 | /* error */ |
| 122 | gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent); |
| 123 | flush_send_queue(cs); |
| 124 | return sent; |
| 125 | } |
| 126 | cb->offset += sent; |
| 127 | cb->len -= sent; |
| 128 | gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u", |
| 129 | sent, cb->len, cs->cmdbytes); |
| 130 | } |
| 131 | |
| 132 | while (cb && !cb->len) { |
| 133 | spin_lock_irqsave(&cs->cmdlock, flags); |
| 134 | cs->cmdbytes -= cs->curlen; |
| 135 | tcb = cb; |
| 136 | cs->cmdbuf = cb = cb->next; |
| 137 | if (cb) { |
| 138 | cb->prev = NULL; |
| 139 | cs->curlen = cb->len; |
| 140 | } else { |
| 141 | cs->lastcmdbuf = NULL; |
| 142 | cs->curlen = 0; |
| 143 | } |
| 144 | spin_unlock_irqrestore(&cs->cmdlock, flags); |
| 145 | |
| 146 | if (tcb->wake_tasklet) |
| 147 | tasklet_schedule(tcb->wake_tasklet); |
| 148 | kfree(tcb); |
| 149 | } |
| 150 | return sent; |
| 151 | } |
| 152 | |
| 153 | /* |
| 154 | * send queue tasklet |
| 155 | * If there is already a skb opened, put data to the transfer buffer |
| 156 | * by calling "write_modem". |
| 157 | * Otherwise take a new skb out of the queue. |
| 158 | */ |
| 159 | static void gigaset_modem_fill(unsigned long data) |
| 160 | { |
| 161 | struct cardstate *cs = (struct cardstate *) data; |
| 162 | struct bc_state *bcs; |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 163 | struct sk_buff *nextskb; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 164 | int sent = 0; |
| 165 | |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 166 | if (!cs) { |
| 167 | gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__); |
| 168 | return; |
| 169 | } |
| 170 | bcs = cs->bcs; |
| 171 | if (!bcs) { |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 172 | gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__); |
| 173 | return; |
| 174 | } |
| 175 | if (!bcs->tx_skb) { |
| 176 | /* no skb is being sent; send command if any */ |
| 177 | sent = send_cb(cs); |
| 178 | gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent); |
| 179 | if (sent) |
| 180 | /* something sent or error */ |
| 181 | return; |
| 182 | |
| 183 | /* no command to send; get skb */ |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 184 | nextskb = skb_dequeue(&bcs->squeue); |
| 185 | if (!nextskb) |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 186 | /* no skb either, nothing to do */ |
| 187 | return; |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 188 | bcs->tx_skb = nextskb; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 189 | |
| 190 | gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)", |
| 191 | (unsigned long) bcs->tx_skb); |
| 192 | } |
| 193 | |
| 194 | /* send skb */ |
| 195 | gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__); |
| 196 | if (write_modem(cs) < 0) |
| 197 | gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__); |
| 198 | } |
| 199 | |
| 200 | /* |
| 201 | * throw away all data queued for sending |
| 202 | */ |
| 203 | static void flush_send_queue(struct cardstate *cs) |
| 204 | { |
| 205 | struct sk_buff *skb; |
| 206 | struct cmdbuf_t *cb; |
| 207 | unsigned long flags; |
| 208 | |
| 209 | /* command queue */ |
| 210 | spin_lock_irqsave(&cs->cmdlock, flags); |
| 211 | while ((cb = cs->cmdbuf) != NULL) { |
| 212 | cs->cmdbuf = cb->next; |
| 213 | if (cb->wake_tasklet) |
| 214 | tasklet_schedule(cb->wake_tasklet); |
| 215 | kfree(cb); |
| 216 | } |
| 217 | cs->cmdbuf = cs->lastcmdbuf = NULL; |
| 218 | cs->cmdbytes = cs->curlen = 0; |
| 219 | spin_unlock_irqrestore(&cs->cmdlock, flags); |
| 220 | |
| 221 | /* data queue */ |
| 222 | if (cs->bcs->tx_skb) |
| 223 | dev_kfree_skb_any(cs->bcs->tx_skb); |
| 224 | while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL) |
| 225 | dev_kfree_skb_any(skb); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | /* Gigaset Driver Interface */ |
| 230 | /* ======================== */ |
| 231 | |
| 232 | /* |
| 233 | * queue an AT command string for transmission to the Gigaset device |
| 234 | * parameters: |
| 235 | * cs controller state structure |
| 236 | * buf buffer containing the string to send |
| 237 | * len number of characters to send |
| 238 | * wake_tasklet tasklet to run when transmission is complete, or NULL |
| 239 | * return value: |
| 240 | * number of bytes queued, or error code < 0 |
| 241 | */ |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 242 | static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb) |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 243 | { |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 244 | unsigned long flags; |
| 245 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 246 | gigaset_dbg_buffer(cs->mstate != MS_LOCKED ? |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 247 | DEBUG_TRANSCMD : DEBUG_LOCKCMD, |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 248 | "CMD Transmit", cb->len, cb->buf); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 249 | |
| 250 | spin_lock_irqsave(&cs->cmdlock, flags); |
| 251 | cb->prev = cs->lastcmdbuf; |
| 252 | if (cs->lastcmdbuf) |
| 253 | cs->lastcmdbuf->next = cb; |
| 254 | else { |
| 255 | cs->cmdbuf = cb; |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 256 | cs->curlen = cb->len; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 257 | } |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 258 | cs->cmdbytes += cb->len; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 259 | cs->lastcmdbuf = cb; |
| 260 | spin_unlock_irqrestore(&cs->cmdlock, flags); |
| 261 | |
| 262 | spin_lock_irqsave(&cs->lock, flags); |
| 263 | if (cs->connected) |
| 264 | tasklet_schedule(&cs->write_tasklet); |
| 265 | spin_unlock_irqrestore(&cs->lock, flags); |
Tilman Schmidt | e3628dd | 2010-07-05 14:18:43 +0000 | [diff] [blame] | 266 | return cb->len; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * tty_driver.write_room interface routine |
| 271 | * return number of characters the driver will accept to be written |
| 272 | * parameter: |
| 273 | * controller state structure |
| 274 | * return value: |
| 275 | * number of characters |
| 276 | */ |
| 277 | static int gigaset_write_room(struct cardstate *cs) |
| 278 | { |
| 279 | unsigned bytes; |
| 280 | |
| 281 | bytes = cs->cmdbytes; |
| 282 | return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0; |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * tty_driver.chars_in_buffer interface routine |
| 287 | * return number of characters waiting to be sent |
| 288 | * parameter: |
| 289 | * controller state structure |
| 290 | * return value: |
| 291 | * number of characters |
| 292 | */ |
| 293 | static int gigaset_chars_in_buffer(struct cardstate *cs) |
| 294 | { |
| 295 | return cs->cmdbytes; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * implementation of ioctl(GIGASET_BRKCHARS) |
| 300 | * parameter: |
| 301 | * controller state structure |
| 302 | * return value: |
| 303 | * -EINVAL (unimplemented function) |
| 304 | */ |
| 305 | static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6]) |
| 306 | { |
| 307 | /* not implemented */ |
| 308 | return -EINVAL; |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * Open B channel |
| 313 | * Called by "do_action" in ev-layer.c |
| 314 | */ |
| 315 | static int gigaset_init_bchannel(struct bc_state *bcs) |
| 316 | { |
| 317 | /* nothing to do for M10x */ |
| 318 | gigaset_bchannel_up(bcs); |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Close B channel |
| 324 | * Called by "do_action" in ev-layer.c |
| 325 | */ |
| 326 | static int gigaset_close_bchannel(struct bc_state *bcs) |
| 327 | { |
| 328 | /* nothing to do for M10x */ |
| 329 | gigaset_bchannel_down(bcs); |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Set up B channel structure |
| 335 | * This is called by "gigaset_initcs" in common.c |
| 336 | */ |
| 337 | static int gigaset_initbcshw(struct bc_state *bcs) |
| 338 | { |
| 339 | /* unused */ |
| 340 | bcs->hw.ser = NULL; |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 341 | return 0; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Free B channel structure |
| 346 | * Called by "gigaset_freebcs" in common.c |
| 347 | */ |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 348 | static void gigaset_freebcshw(struct bc_state *bcs) |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 349 | { |
| 350 | /* unused */ |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Reinitialize B channel structure |
| 355 | * This is called by "bcs_reinit" in common.c |
| 356 | */ |
| 357 | static void gigaset_reinitbcshw(struct bc_state *bcs) |
| 358 | { |
| 359 | /* nothing to do for M10x */ |
| 360 | } |
| 361 | |
| 362 | /* |
| 363 | * Free hardware specific device data |
| 364 | * This will be called by "gigaset_freecs" in common.c |
| 365 | */ |
| 366 | static void gigaset_freecshw(struct cardstate *cs) |
| 367 | { |
| 368 | tasklet_kill(&cs->write_tasklet); |
| 369 | if (!cs->hw.ser) |
| 370 | return; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 371 | platform_device_unregister(&cs->hw.ser->dev); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void gigaset_device_release(struct device *dev) |
| 375 | { |
Paul Bolle | 8d2c3ab | 2016-02-18 21:29:08 +0100 | [diff] [blame] | 376 | kfree(container_of(dev, struct ser_cardstate, dev.dev)); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | /* |
| 380 | * Set up hardware specific device data |
| 381 | * This is called by "gigaset_initcs" in common.c |
| 382 | */ |
| 383 | static int gigaset_initcshw(struct cardstate *cs) |
| 384 | { |
| 385 | int rc; |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 386 | struct ser_cardstate *scs; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 387 | |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 388 | scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL); |
| 389 | if (!scs) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 390 | pr_err("out of memory\n"); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 391 | return -ENOMEM; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 392 | } |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 393 | cs->hw.ser = scs; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 394 | |
| 395 | cs->hw.ser->dev.name = GIGASET_MODULENAME; |
| 396 | cs->hw.ser->dev.id = cs->minor_index; |
| 397 | cs->hw.ser->dev.dev.release = gigaset_device_release; |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 398 | rc = platform_device_register(&cs->hw.ser->dev); |
| 399 | if (rc != 0) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 400 | pr_err("error %d registering platform device\n", rc); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 401 | kfree(cs->hw.ser); |
| 402 | cs->hw.ser = NULL; |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 403 | return rc; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 404 | } |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 405 | |
| 406 | tasklet_init(&cs->write_tasklet, |
Joe Perches | 5452fee | 2009-11-19 09:55:53 +0000 | [diff] [blame] | 407 | gigaset_modem_fill, (unsigned long) cs); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 408 | return 0; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /* |
| 412 | * set modem control lines |
| 413 | * Parameters: |
| 414 | * card state structure |
| 415 | * modem control line state ([TIOCM_DTR]|[TIOCM_RTS]) |
| 416 | * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c |
| 417 | * and by "if_lock" and "if_termios" in interface.c |
| 418 | */ |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 419 | static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state, |
| 420 | unsigned new_state) |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 421 | { |
| 422 | struct tty_struct *tty = cs->hw.ser->tty; |
| 423 | unsigned int set, clear; |
| 424 | |
Alan Cox | ede03d3 | 2015-12-15 18:11:29 +0100 | [diff] [blame] | 425 | WARN_ON(!tty || !tty->ops); |
| 426 | /* tiocmset is an optional tty driver method */ |
| 427 | if (!tty->ops->tiocmset) |
Alan Cox | f34d7a5 | 2008-04-30 00:54:13 -0700 | [diff] [blame] | 428 | return -EINVAL; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 429 | set = new_state & ~old_state; |
| 430 | clear = old_state & ~new_state; |
| 431 | if (!set && !clear) |
| 432 | return 0; |
| 433 | gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear); |
Alan Cox | 20b9d17 | 2011-02-14 16:26:50 +0000 | [diff] [blame] | 434 | return tty->ops->tiocmset(tty, set, clear); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag) |
| 438 | { |
| 439 | return -EINVAL; |
| 440 | } |
| 441 | |
| 442 | static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag) |
| 443 | { |
| 444 | return -EINVAL; |
| 445 | } |
| 446 | |
Tilman Schmidt | 35dc845 | 2007-03-29 01:20:34 -0700 | [diff] [blame] | 447 | static const struct gigaset_ops ops = { |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 448 | gigaset_write_cmd, |
| 449 | gigaset_write_room, |
| 450 | gigaset_chars_in_buffer, |
| 451 | gigaset_brkchars, |
| 452 | gigaset_init_bchannel, |
| 453 | gigaset_close_bchannel, |
| 454 | gigaset_initbcshw, |
| 455 | gigaset_freebcshw, |
| 456 | gigaset_reinitbcshw, |
| 457 | gigaset_initcshw, |
| 458 | gigaset_freecshw, |
| 459 | gigaset_set_modem_ctrl, |
| 460 | gigaset_baud_rate, |
| 461 | gigaset_set_line_ctrl, |
| 462 | gigaset_m10x_send_skb, /* asyncdata.c */ |
| 463 | gigaset_m10x_input, /* asyncdata.c */ |
| 464 | }; |
| 465 | |
| 466 | |
| 467 | /* Line Discipline Interface */ |
| 468 | /* ========================= */ |
| 469 | |
| 470 | /* helper functions for cardstate refcounting */ |
| 471 | static struct cardstate *cs_get(struct tty_struct *tty) |
| 472 | { |
| 473 | struct cardstate *cs = tty->disc_data; |
| 474 | |
| 475 | if (!cs || !cs->hw.ser) { |
| 476 | gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__); |
| 477 | return NULL; |
| 478 | } |
| 479 | atomic_inc(&cs->hw.ser->refcnt); |
| 480 | return cs; |
| 481 | } |
| 482 | |
| 483 | static void cs_put(struct cardstate *cs) |
| 484 | { |
| 485 | if (atomic_dec_and_test(&cs->hw.ser->refcnt)) |
Tilman Schmidt | ee51ef0 | 2008-02-06 01:38:30 -0800 | [diff] [blame] | 486 | complete(&cs->hw.ser->dead_cmp); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | /* |
| 490 | * Called by the tty driver when the line discipline is pushed onto the tty. |
| 491 | * Called in process context. |
| 492 | */ |
| 493 | static int |
| 494 | gigaset_tty_open(struct tty_struct *tty) |
| 495 | { |
| 496 | struct cardstate *cs; |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 497 | int rc; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 498 | |
| 499 | gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101"); |
| 500 | |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 501 | pr_info(DRIVER_DESC "\n"); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 502 | |
| 503 | if (!driver) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 504 | pr_err("%s: no driver structure\n", __func__); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 505 | return -ENODEV; |
| 506 | } |
| 507 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 508 | /* allocate memory for our device state and initialize it */ |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 509 | cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 510 | if (!cs) { |
| 511 | rc = -ENODEV; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 512 | goto error; |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 513 | } |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 514 | |
| 515 | cs->dev = &cs->hw.ser->dev.dev; |
| 516 | cs->hw.ser->tty = tty; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 517 | atomic_set(&cs->hw.ser->refcnt, 1); |
Tilman Schmidt | ee51ef0 | 2008-02-06 01:38:30 -0800 | [diff] [blame] | 518 | init_completion(&cs->hw.ser->dead_cmp); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 519 | tty->disc_data = cs; |
| 520 | |
Tilman Schmidt | fd98e94 | 2015-07-14 00:37:13 +0200 | [diff] [blame] | 521 | /* Set the amount of data we're willing to receive per call |
| 522 | * from the hardware driver to half of the input buffer size |
| 523 | * to leave some reserve. |
| 524 | * Note: We don't do flow control towards the hardware driver. |
| 525 | * If more data is received than will fit into the input buffer, |
| 526 | * it will be dropped and an error will be logged. This should |
| 527 | * never happen as the device is slow and the buffer size ample. |
| 528 | */ |
| 529 | tty->receive_room = RBUFSIZE/2; |
| 530 | |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 531 | /* OK.. Initialization of the datastructures and the HW is done.. Now |
| 532 | * startup system and notify the LL that we are ready to run |
| 533 | */ |
| 534 | if (startmode == SM_LOCKED) |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 535 | cs->mstate = MS_LOCKED; |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 536 | rc = gigaset_start(cs); |
| 537 | if (rc < 0) { |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 538 | tasklet_kill(&cs->write_tasklet); |
| 539 | goto error; |
| 540 | } |
| 541 | |
| 542 | gig_dbg(DEBUG_INIT, "Startup of HLL done"); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 543 | return 0; |
| 544 | |
| 545 | error: |
| 546 | gig_dbg(DEBUG_INIT, "Startup of HLL failed"); |
| 547 | tty->disc_data = NULL; |
| 548 | gigaset_freecs(cs); |
Tilman Schmidt | 81fa7b8 | 2012-04-25 13:02:20 +0000 | [diff] [blame] | 549 | return rc; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | /* |
| 553 | * Called by the tty driver when the line discipline is removed. |
| 554 | * Called from process context. |
| 555 | */ |
| 556 | static void |
| 557 | gigaset_tty_close(struct tty_struct *tty) |
| 558 | { |
| 559 | struct cardstate *cs = tty->disc_data; |
| 560 | |
| 561 | gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101"); |
| 562 | |
| 563 | if (!cs) { |
| 564 | gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__); |
| 565 | return; |
| 566 | } |
| 567 | |
| 568 | /* prevent other callers from entering ldisc methods */ |
| 569 | tty->disc_data = NULL; |
| 570 | |
| 571 | if (!cs->hw.ser) |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 572 | pr_err("%s: no hw cardstate\n", __func__); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 573 | else { |
| 574 | /* wait for running methods to finish */ |
| 575 | if (!atomic_dec_and_test(&cs->hw.ser->refcnt)) |
Tilman Schmidt | ee51ef0 | 2008-02-06 01:38:30 -0800 | [diff] [blame] | 576 | wait_for_completion(&cs->hw.ser->dead_cmp); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | /* stop operations */ |
| 580 | gigaset_stop(cs); |
| 581 | tasklet_kill(&cs->write_tasklet); |
| 582 | flush_send_queue(cs); |
| 583 | cs->dev = NULL; |
| 584 | gigaset_freecs(cs); |
| 585 | |
| 586 | gig_dbg(DEBUG_INIT, "Shutdown of HLL done"); |
| 587 | } |
| 588 | |
| 589 | /* |
| 590 | * Called by the tty driver when the tty line is hung up. |
| 591 | * Wait for I/O to driver to complete and unregister ISDN device. |
| 592 | * This is already done by the close routine, so just call that. |
| 593 | * Called from process context. |
| 594 | */ |
| 595 | static int gigaset_tty_hangup(struct tty_struct *tty) |
| 596 | { |
| 597 | gigaset_tty_close(tty); |
| 598 | return 0; |
| 599 | } |
| 600 | |
| 601 | /* |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 602 | * Ioctl on the tty. |
| 603 | * Called in process context only. |
| 604 | * May be re-entered by multiple ioctl calling threads. |
| 605 | */ |
| 606 | static int |
| 607 | gigaset_tty_ioctl(struct tty_struct *tty, struct file *file, |
| 608 | unsigned int cmd, unsigned long arg) |
| 609 | { |
| 610 | struct cardstate *cs = cs_get(tty); |
| 611 | int rc, val; |
| 612 | int __user *p = (int __user *)arg; |
| 613 | |
| 614 | if (!cs) |
| 615 | return -ENXIO; |
| 616 | |
| 617 | switch (cmd) { |
Alan Cox | ea1afd2 | 2008-10-13 10:44:43 +0100 | [diff] [blame] | 618 | |
| 619 | case FIONREAD: |
| 620 | /* unused, always return zero */ |
| 621 | val = 0; |
| 622 | rc = put_user(val, p); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 623 | break; |
| 624 | |
| 625 | case TCFLSH: |
| 626 | /* flush our buffers and the serial port's buffer */ |
| 627 | switch (arg) { |
| 628 | case TCIFLUSH: |
| 629 | /* no own input buffer to flush */ |
| 630 | break; |
| 631 | case TCIOFLUSH: |
| 632 | case TCOFLUSH: |
| 633 | flush_send_queue(cs); |
| 634 | break; |
| 635 | } |
Alan Cox | ea1afd2 | 2008-10-13 10:44:43 +0100 | [diff] [blame] | 636 | /* Pass through */ |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 637 | |
| 638 | default: |
Alan Cox | ea1afd2 | 2008-10-13 10:44:43 +0100 | [diff] [blame] | 639 | /* pass through to underlying serial device */ |
| 640 | rc = n_tty_ioctl_helper(tty, file, cmd, arg); |
| 641 | break; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 642 | } |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 643 | cs_put(cs); |
| 644 | return rc; |
| 645 | } |
| 646 | |
| 647 | /* |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 648 | * Called by the tty driver when a block of data has been received. |
| 649 | * Will not be re-entered while running but other ldisc functions |
| 650 | * may be called in parallel. |
| 651 | * Can be called from hard interrupt level as well as soft interrupt |
| 652 | * level or mainline. |
| 653 | * Parameters: |
| 654 | * tty tty structure |
| 655 | * buf buffer containing received characters |
| 656 | * cflags buffer containing error flags for received characters (ignored) |
| 657 | * count number of received characters |
| 658 | */ |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 659 | static void |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 660 | gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf, |
| 661 | char *cflags, int count) |
| 662 | { |
| 663 | struct cardstate *cs = cs_get(tty); |
| 664 | unsigned tail, head, n; |
| 665 | struct inbuf_t *inbuf; |
| 666 | |
| 667 | if (!cs) |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 668 | return; |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 669 | inbuf = cs->inbuf; |
| 670 | if (!inbuf) { |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 671 | dev_err(cs->dev, "%s: no inbuf\n", __func__); |
| 672 | cs_put(cs); |
Linus Torvalds | 55db4c6 | 2011-06-04 06:33:24 +0900 | [diff] [blame] | 673 | return; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 674 | } |
| 675 | |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 676 | tail = inbuf->tail; |
| 677 | head = inbuf->head; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 678 | gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes", |
| 679 | head, tail, count); |
| 680 | |
| 681 | if (head <= tail) { |
| 682 | /* possible buffer wraparound */ |
| 683 | n = min_t(unsigned, count, RBUFSIZE - tail); |
| 684 | memcpy(inbuf->data + tail, buf, n); |
| 685 | tail = (tail + n) % RBUFSIZE; |
| 686 | buf += n; |
| 687 | count -= n; |
| 688 | } |
| 689 | |
| 690 | if (count > 0) { |
| 691 | /* tail < head and some data left */ |
| 692 | n = head - tail - 1; |
| 693 | if (count > n) { |
| 694 | dev_err(cs->dev, |
| 695 | "inbuf overflow, discarding %d bytes\n", |
| 696 | count - n); |
| 697 | count = n; |
| 698 | } |
| 699 | memcpy(inbuf->data + tail, buf, count); |
| 700 | tail += count; |
| 701 | } |
| 702 | |
| 703 | gig_dbg(DEBUG_INTR, "setting tail to %u", tail); |
Tilman Schmidt | 9d4bee2 | 2008-02-06 01:38:28 -0800 | [diff] [blame] | 704 | inbuf->tail = tail; |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 705 | |
| 706 | /* Everything was received .. Push data into handler */ |
| 707 | gig_dbg(DEBUG_INTR, "%s-->BH", __func__); |
| 708 | gigaset_schedule_event(cs); |
| 709 | cs_put(cs); |
| 710 | } |
| 711 | |
| 712 | /* |
| 713 | * Called by the tty driver when there's room for more data to send. |
| 714 | */ |
| 715 | static void |
| 716 | gigaset_tty_wakeup(struct tty_struct *tty) |
| 717 | { |
| 718 | struct cardstate *cs = cs_get(tty); |
| 719 | |
| 720 | clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags); |
| 721 | if (!cs) |
| 722 | return; |
| 723 | tasklet_schedule(&cs->write_tasklet); |
| 724 | cs_put(cs); |
| 725 | } |
| 726 | |
Alan Cox | a352def | 2008-07-16 21:53:12 +0100 | [diff] [blame] | 727 | static struct tty_ldisc_ops gigaset_ldisc = { |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 728 | .owner = THIS_MODULE, |
| 729 | .magic = TTY_LDISC_MAGIC, |
| 730 | .name = "ser_gigaset", |
| 731 | .open = gigaset_tty_open, |
| 732 | .close = gigaset_tty_close, |
| 733 | .hangup = gigaset_tty_hangup, |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 734 | .ioctl = gigaset_tty_ioctl, |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 735 | .receive_buf = gigaset_tty_receive, |
| 736 | .write_wakeup = gigaset_tty_wakeup, |
| 737 | }; |
| 738 | |
| 739 | |
| 740 | /* Initialization / Shutdown */ |
| 741 | /* ========================= */ |
| 742 | |
| 743 | static int __init ser_gigaset_init(void) |
| 744 | { |
| 745 | int rc; |
| 746 | |
| 747 | gig_dbg(DEBUG_INIT, "%s", __func__); |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 748 | rc = platform_driver_register(&device_driver); |
| 749 | if (rc != 0) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 750 | pr_err("error %d registering platform driver\n", rc); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 751 | return rc; |
| 752 | } |
| 753 | |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 754 | /* allocate memory for our driver state and initialize it */ |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 755 | driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 756 | GIGASET_MODULENAME, GIGASET_DEVNAME, |
| 757 | &ops, THIS_MODULE); |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 758 | if (!driver) |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 759 | goto error; |
| 760 | |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 761 | rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc); |
| 762 | if (rc != 0) { |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 763 | pr_err("error %d registering line discipline\n", rc); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 764 | goto error; |
| 765 | } |
| 766 | |
| 767 | return 0; |
| 768 | |
| 769 | error: |
| 770 | if (driver) { |
| 771 | gigaset_freedriver(driver); |
| 772 | driver = NULL; |
| 773 | } |
| 774 | platform_driver_unregister(&device_driver); |
| 775 | return rc; |
| 776 | } |
| 777 | |
| 778 | static void __exit ser_gigaset_exit(void) |
| 779 | { |
| 780 | int rc; |
| 781 | |
| 782 | gig_dbg(DEBUG_INIT, "%s", __func__); |
| 783 | |
| 784 | if (driver) { |
| 785 | gigaset_freedriver(driver); |
| 786 | driver = NULL; |
| 787 | } |
| 788 | |
Tilman Schmidt | ae67d7d | 2009-10-25 09:30:27 +0000 | [diff] [blame] | 789 | rc = tty_unregister_ldisc(N_GIGASET_M101); |
| 790 | if (rc != 0) |
Tilman Schmidt | c8770dc | 2008-12-26 01:21:29 -0800 | [diff] [blame] | 791 | pr_err("error %d unregistering line discipline\n", rc); |
Tilman Schmidt | 2869b23 | 2007-02-12 00:52:34 -0800 | [diff] [blame] | 792 | |
| 793 | platform_driver_unregister(&device_driver); |
| 794 | } |
| 795 | |
| 796 | module_init(ser_gigaset_init); |
| 797 | module_exit(ser_gigaset_exit); |