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