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