blob: 2a506fe0c8a4500a16e83b1aba70a6ed2d192185 [file] [log] [blame]
Tilman Schmidt2869b232007-02-12 00:52:34 -08001/* 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 Schmidt2869b232007-02-12 00:52:34 -080014#include <linux/module.h>
15#include <linux/moduleparam.h>
16#include <linux/platform_device.h>
Tilman Schmidtee51ef02008-02-06 01:38:30 -080017#include <linux/completion.h>
Tilman Schmidt2869b232007-02-12 00:52:34 -080018
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
31MODULE_AUTHOR(DRIVER_AUTHOR);
32MODULE_DESCRIPTION(DRIVER_DESC);
33MODULE_LICENSE("GPL");
34MODULE_ALIAS_LDISC(N_GIGASET_M101);
35
36static int startmode = SM_ISDN;
37module_param(startmode, int, S_IRUGO);
38MODULE_PARM_DESC(startmode, "initial operation mode");
39static int cidmode = 1;
40module_param(cidmode, int, S_IRUGO);
41MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
42
43static struct gigaset_driver *driver;
44
45struct ser_cardstate {
46 struct platform_device dev;
47 struct tty_struct *tty;
48 atomic_t refcnt;
Tilman Schmidtee51ef02008-02-06 01:38:30 -080049 struct completion dead_cmp;
Tilman Schmidt2869b232007-02-12 00:52:34 -080050};
51
52static struct platform_driver device_driver = {
53 .driver = {
54 .name = GIGASET_MODULENAME,
55 },
56};
57
58static 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 */
63static 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 Coxf34d7a52008-04-30 00:54:13 -070068 int sent = -EOPNOTSUPP;
Tilman Schmidt2869b232007-02-12 00:52:34 -080069
Alan Coxede03d32015-12-15 18:11:29 +010070 WARN_ON(!tty || !tty->ops || !skb);
Tilman Schmidt2869b232007-02-12 00:52:34 -080071
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 Coxf34d7a52008-04-30 00:54:13 -070079 if (tty->ops->write)
80 sent = tty->ops->write(tty, skb->data, skb->len);
Tilman Schmidt2869b232007-02-12 00:52:34 -080081 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 */
104static 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 Coxede03d32015-12-15 18:11:29 +0100111 WARN_ON(!tty || !tty->ops);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800112
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 Coxf34d7a52008-04-30 00:54:13 -0700119 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800120 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 */
159static void gigaset_modem_fill(unsigned long data)
160{
161 struct cardstate *cs = (struct cardstate *) data;
162 struct bc_state *bcs;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000163 struct sk_buff *nextskb;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800164 int sent = 0;
165
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000166 if (!cs) {
167 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
168 return;
169 }
170 bcs = cs->bcs;
171 if (!bcs) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800172 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 Schmidtae67d7d2009-10-25 09:30:27 +0000184 nextskb = skb_dequeue(&bcs->squeue);
185 if (!nextskb)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800186 /* no skb either, nothing to do */
187 return;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000188 bcs->tx_skb = nextskb;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800189
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 */
203static 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 Schmidte3628dd2010-07-05 14:18:43 +0000242static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800243{
Tilman Schmidt2869b232007-02-12 00:52:34 -0800244 unsigned long flags;
245
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800246 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Joe Perches475be4d2012-02-19 19:52:38 -0800247 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000248 "CMD Transmit", cb->len, cb->buf);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800249
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 Schmidte3628dd2010-07-05 14:18:43 +0000256 cs->curlen = cb->len;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800257 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000258 cs->cmdbytes += cb->len;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800259 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 Schmidte3628dd2010-07-05 14:18:43 +0000266 return cb->len;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800267}
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 */
277static 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 */
293static 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 */
305static 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 */
315static 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 */
326static 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 */
337static int gigaset_initbcshw(struct bc_state *bcs)
338{
339 /* unused */
340 bcs->hw.ser = NULL;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000341 return 0;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800342}
343
344/*
345 * Free B channel structure
346 * Called by "gigaset_freebcs" in common.c
347 */
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000348static void gigaset_freebcshw(struct bc_state *bcs)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800349{
350 /* unused */
Tilman Schmidt2869b232007-02-12 00:52:34 -0800351}
352
353/*
354 * Reinitialize B channel structure
355 * This is called by "bcs_reinit" in common.c
356 */
357static 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 */
366static void gigaset_freecshw(struct cardstate *cs)
367{
368 tasklet_kill(&cs->write_tasklet);
369 if (!cs->hw.ser)
370 return;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800371 platform_device_unregister(&cs->hw.ser->dev);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800372}
373
374static void gigaset_device_release(struct device *dev)
375{
Tilman Schmidt4c5e3542015-12-15 18:11:30 +0100376 struct cardstate *cs = dev_get_drvdata(dev);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800377
Tilman Schmidt4c5e3542015-12-15 18:11:30 +0100378 if (!cs)
379 return;
380 dev_set_drvdata(dev, NULL);
381 kfree(cs->hw.ser);
382 cs->hw.ser = NULL;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800383}
384
385/*
386 * Set up hardware specific device data
387 * This is called by "gigaset_initcs" in common.c
388 */
389static int gigaset_initcshw(struct cardstate *cs)
390{
391 int rc;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000392 struct ser_cardstate *scs;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800393
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000394 scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
395 if (!scs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800396 pr_err("out of memory\n");
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000397 return -ENOMEM;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800398 }
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000399 cs->hw.ser = scs;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800400
401 cs->hw.ser->dev.name = GIGASET_MODULENAME;
402 cs->hw.ser->dev.id = cs->minor_index;
403 cs->hw.ser->dev.dev.release = gigaset_device_release;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000404 rc = platform_device_register(&cs->hw.ser->dev);
405 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800406 pr_err("error %d registering platform device\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800407 kfree(cs->hw.ser);
408 cs->hw.ser = NULL;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000409 return rc;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800410 }
411 dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
412
413 tasklet_init(&cs->write_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +0000414 gigaset_modem_fill, (unsigned long) cs);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000415 return 0;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800416}
417
418/*
419 * set modem control lines
420 * Parameters:
421 * card state structure
422 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
423 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
424 * and by "if_lock" and "if_termios" in interface.c
425 */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000426static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
427 unsigned new_state)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800428{
429 struct tty_struct *tty = cs->hw.ser->tty;
430 unsigned int set, clear;
431
Alan Coxede03d32015-12-15 18:11:29 +0100432 WARN_ON(!tty || !tty->ops);
433 /* tiocmset is an optional tty driver method */
434 if (!tty->ops->tiocmset)
Alan Coxf34d7a52008-04-30 00:54:13 -0700435 return -EINVAL;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800436 set = new_state & ~old_state;
437 clear = old_state & ~new_state;
438 if (!set && !clear)
439 return 0;
440 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
Alan Cox20b9d172011-02-14 16:26:50 +0000441 return tty->ops->tiocmset(tty, set, clear);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800442}
443
444static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
445{
446 return -EINVAL;
447}
448
449static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
450{
451 return -EINVAL;
452}
453
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700454static const struct gigaset_ops ops = {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800455 gigaset_write_cmd,
456 gigaset_write_room,
457 gigaset_chars_in_buffer,
458 gigaset_brkchars,
459 gigaset_init_bchannel,
460 gigaset_close_bchannel,
461 gigaset_initbcshw,
462 gigaset_freebcshw,
463 gigaset_reinitbcshw,
464 gigaset_initcshw,
465 gigaset_freecshw,
466 gigaset_set_modem_ctrl,
467 gigaset_baud_rate,
468 gigaset_set_line_ctrl,
469 gigaset_m10x_send_skb, /* asyncdata.c */
470 gigaset_m10x_input, /* asyncdata.c */
471};
472
473
474/* Line Discipline Interface */
475/* ========================= */
476
477/* helper functions for cardstate refcounting */
478static struct cardstate *cs_get(struct tty_struct *tty)
479{
480 struct cardstate *cs = tty->disc_data;
481
482 if (!cs || !cs->hw.ser) {
483 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
484 return NULL;
485 }
486 atomic_inc(&cs->hw.ser->refcnt);
487 return cs;
488}
489
490static void cs_put(struct cardstate *cs)
491{
492 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800493 complete(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800494}
495
496/*
497 * Called by the tty driver when the line discipline is pushed onto the tty.
498 * Called in process context.
499 */
500static int
501gigaset_tty_open(struct tty_struct *tty)
502{
503 struct cardstate *cs;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000504 int rc;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800505
506 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
507
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800508 pr_info(DRIVER_DESC "\n");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800509
510 if (!driver) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800511 pr_err("%s: no driver structure\n", __func__);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800512 return -ENODEV;
513 }
514
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400515 /* allocate memory for our device state and initialize it */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000516 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000517 if (!cs) {
518 rc = -ENODEV;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800519 goto error;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000520 }
Tilman Schmidt2869b232007-02-12 00:52:34 -0800521
522 cs->dev = &cs->hw.ser->dev.dev;
523 cs->hw.ser->tty = tty;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800524 atomic_set(&cs->hw.ser->refcnt, 1);
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800525 init_completion(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800526 tty->disc_data = cs;
527
Tilman Schmidtfd98e942015-07-14 00:37:13 +0200528 /* Set the amount of data we're willing to receive per call
529 * from the hardware driver to half of the input buffer size
530 * to leave some reserve.
531 * Note: We don't do flow control towards the hardware driver.
532 * If more data is received than will fit into the input buffer,
533 * it will be dropped and an error will be logged. This should
534 * never happen as the device is slow and the buffer size ample.
535 */
536 tty->receive_room = RBUFSIZE/2;
537
Tilman Schmidt2869b232007-02-12 00:52:34 -0800538 /* OK.. Initialization of the datastructures and the HW is done.. Now
539 * startup system and notify the LL that we are ready to run
540 */
541 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800542 cs->mstate = MS_LOCKED;
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000543 rc = gigaset_start(cs);
544 if (rc < 0) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800545 tasklet_kill(&cs->write_tasklet);
546 goto error;
547 }
548
549 gig_dbg(DEBUG_INIT, "Startup of HLL done");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800550 return 0;
551
552error:
553 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
554 tty->disc_data = NULL;
555 gigaset_freecs(cs);
Tilman Schmidt81fa7b82012-04-25 13:02:20 +0000556 return rc;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800557}
558
559/*
560 * Called by the tty driver when the line discipline is removed.
561 * Called from process context.
562 */
563static void
564gigaset_tty_close(struct tty_struct *tty)
565{
566 struct cardstate *cs = tty->disc_data;
567
568 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
569
570 if (!cs) {
571 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
572 return;
573 }
574
575 /* prevent other callers from entering ldisc methods */
576 tty->disc_data = NULL;
577
578 if (!cs->hw.ser)
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800579 pr_err("%s: no hw cardstate\n", __func__);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800580 else {
581 /* wait for running methods to finish */
582 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800583 wait_for_completion(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800584 }
585
586 /* stop operations */
587 gigaset_stop(cs);
588 tasklet_kill(&cs->write_tasklet);
589 flush_send_queue(cs);
590 cs->dev = NULL;
591 gigaset_freecs(cs);
592
593 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
594}
595
596/*
597 * Called by the tty driver when the tty line is hung up.
598 * Wait for I/O to driver to complete and unregister ISDN device.
599 * This is already done by the close routine, so just call that.
600 * Called from process context.
601 */
602static int gigaset_tty_hangup(struct tty_struct *tty)
603{
604 gigaset_tty_close(tty);
605 return 0;
606}
607
608/*
Tilman Schmidt2869b232007-02-12 00:52:34 -0800609 * Ioctl on the tty.
610 * Called in process context only.
611 * May be re-entered by multiple ioctl calling threads.
612 */
613static int
614gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
615 unsigned int cmd, unsigned long arg)
616{
617 struct cardstate *cs = cs_get(tty);
618 int rc, val;
619 int __user *p = (int __user *)arg;
620
621 if (!cs)
622 return -ENXIO;
623
624 switch (cmd) {
Alan Coxea1afd22008-10-13 10:44:43 +0100625
626 case FIONREAD:
627 /* unused, always return zero */
628 val = 0;
629 rc = put_user(val, p);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800630 break;
631
632 case TCFLSH:
633 /* flush our buffers and the serial port's buffer */
634 switch (arg) {
635 case TCIFLUSH:
636 /* no own input buffer to flush */
637 break;
638 case TCIOFLUSH:
639 case TCOFLUSH:
640 flush_send_queue(cs);
641 break;
642 }
Alan Coxea1afd22008-10-13 10:44:43 +0100643 /* Pass through */
Tilman Schmidt2869b232007-02-12 00:52:34 -0800644
645 default:
Alan Coxea1afd22008-10-13 10:44:43 +0100646 /* pass through to underlying serial device */
647 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
648 break;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800649 }
Tilman Schmidt2869b232007-02-12 00:52:34 -0800650 cs_put(cs);
651 return rc;
652}
653
654/*
Tilman Schmidt2869b232007-02-12 00:52:34 -0800655 * Called by the tty driver when a block of data has been received.
656 * Will not be re-entered while running but other ldisc functions
657 * may be called in parallel.
658 * Can be called from hard interrupt level as well as soft interrupt
659 * level or mainline.
660 * Parameters:
661 * tty tty structure
662 * buf buffer containing received characters
663 * cflags buffer containing error flags for received characters (ignored)
664 * count number of received characters
665 */
Linus Torvalds55db4c62011-06-04 06:33:24 +0900666static void
Tilman Schmidt2869b232007-02-12 00:52:34 -0800667gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
668 char *cflags, int count)
669{
670 struct cardstate *cs = cs_get(tty);
671 unsigned tail, head, n;
672 struct inbuf_t *inbuf;
673
674 if (!cs)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900675 return;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000676 inbuf = cs->inbuf;
677 if (!inbuf) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800678 dev_err(cs->dev, "%s: no inbuf\n", __func__);
679 cs_put(cs);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900680 return;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800681 }
682
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800683 tail = inbuf->tail;
684 head = inbuf->head;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800685 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
686 head, tail, count);
687
688 if (head <= tail) {
689 /* possible buffer wraparound */
690 n = min_t(unsigned, count, RBUFSIZE - tail);
691 memcpy(inbuf->data + tail, buf, n);
692 tail = (tail + n) % RBUFSIZE;
693 buf += n;
694 count -= n;
695 }
696
697 if (count > 0) {
698 /* tail < head and some data left */
699 n = head - tail - 1;
700 if (count > n) {
701 dev_err(cs->dev,
702 "inbuf overflow, discarding %d bytes\n",
703 count - n);
704 count = n;
705 }
706 memcpy(inbuf->data + tail, buf, count);
707 tail += count;
708 }
709
710 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800711 inbuf->tail = tail;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800712
713 /* Everything was received .. Push data into handler */
714 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
715 gigaset_schedule_event(cs);
716 cs_put(cs);
717}
718
719/*
720 * Called by the tty driver when there's room for more data to send.
721 */
722static void
723gigaset_tty_wakeup(struct tty_struct *tty)
724{
725 struct cardstate *cs = cs_get(tty);
726
727 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
728 if (!cs)
729 return;
730 tasklet_schedule(&cs->write_tasklet);
731 cs_put(cs);
732}
733
Alan Coxa352def2008-07-16 21:53:12 +0100734static struct tty_ldisc_ops gigaset_ldisc = {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800735 .owner = THIS_MODULE,
736 .magic = TTY_LDISC_MAGIC,
737 .name = "ser_gigaset",
738 .open = gigaset_tty_open,
739 .close = gigaset_tty_close,
740 .hangup = gigaset_tty_hangup,
Tilman Schmidt2869b232007-02-12 00:52:34 -0800741 .ioctl = gigaset_tty_ioctl,
Tilman Schmidt2869b232007-02-12 00:52:34 -0800742 .receive_buf = gigaset_tty_receive,
743 .write_wakeup = gigaset_tty_wakeup,
744};
745
746
747/* Initialization / Shutdown */
748/* ========================= */
749
750static int __init ser_gigaset_init(void)
751{
752 int rc;
753
754 gig_dbg(DEBUG_INIT, "%s", __func__);
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000755 rc = platform_driver_register(&device_driver);
756 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800757 pr_err("error %d registering platform driver\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800758 return rc;
759 }
760
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400761 /* allocate memory for our driver state and initialize it */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000762 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Joe Perches475be4d2012-02-19 19:52:38 -0800763 GIGASET_MODULENAME, GIGASET_DEVNAME,
764 &ops, THIS_MODULE);
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000765 if (!driver)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800766 goto error;
767
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000768 rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
769 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800770 pr_err("error %d registering line discipline\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800771 goto error;
772 }
773
774 return 0;
775
776error:
777 if (driver) {
778 gigaset_freedriver(driver);
779 driver = NULL;
780 }
781 platform_driver_unregister(&device_driver);
782 return rc;
783}
784
785static void __exit ser_gigaset_exit(void)
786{
787 int rc;
788
789 gig_dbg(DEBUG_INIT, "%s", __func__);
790
791 if (driver) {
792 gigaset_freedriver(driver);
793 driver = NULL;
794 }
795
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000796 rc = tty_unregister_ldisc(N_GIGASET_M101);
797 if (rc != 0)
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800798 pr_err("error %d unregistering line discipline\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800799
800 platform_driver_unregister(&device_driver);
801}
802
803module_init(ser_gigaset_init);
804module_exit(ser_gigaset_exit);