blob: 6f3fd4cf437881d254e793fee7050ebf2597c7fd [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
70 if (!tty || !tty->driver || !skb)
Alan Coxf34d7a52008-04-30 00:54:13 -070071 return -EINVAL;
Tilman Schmidt2869b232007-02-12 00:52:34 -080072
73 if (!skb->len) {
74 dev_kfree_skb_any(skb);
75 bcs->tx_skb = NULL;
76 return -EINVAL;
77 }
78
79 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Alan Coxf34d7a52008-04-30 00:54:13 -070080 if (tty->ops->write)
81 sent = tty->ops->write(tty, skb->data, skb->len);
Tilman Schmidt2869b232007-02-12 00:52:34 -080082 gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
83 if (sent < 0) {
84 /* error */
85 flush_send_queue(cs);
86 return sent;
87 }
88 skb_pull(skb, sent);
89 if (!skb->len) {
90 /* skb sent completely */
91 gigaset_skb_sent(bcs, skb);
92
93 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
94 (unsigned long) skb);
95 dev_kfree_skb_any(skb);
96 bcs->tx_skb = NULL;
97 }
98 return sent;
99}
100
101/*
102 * transmit first queued command buffer
103 * result: number of bytes sent or error code < 0
104 */
105static int send_cb(struct cardstate *cs)
106{
107 struct tty_struct *tty = cs->hw.ser->tty;
108 struct cmdbuf_t *cb, *tcb;
109 unsigned long flags;
110 int sent = 0;
111
112 if (!tty || !tty->driver)
113 return -EFAULT;
114
115 cb = cs->cmdbuf;
116 if (!cb)
117 return 0; /* nothing to do */
118
119 if (cb->len) {
120 set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
Alan Coxf34d7a52008-04-30 00:54:13 -0700121 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800122 if (sent < 0) {
123 /* error */
124 gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
125 flush_send_queue(cs);
126 return sent;
127 }
128 cb->offset += sent;
129 cb->len -= sent;
130 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
131 sent, cb->len, cs->cmdbytes);
132 }
133
134 while (cb && !cb->len) {
135 spin_lock_irqsave(&cs->cmdlock, flags);
136 cs->cmdbytes -= cs->curlen;
137 tcb = cb;
138 cs->cmdbuf = cb = cb->next;
139 if (cb) {
140 cb->prev = NULL;
141 cs->curlen = cb->len;
142 } else {
143 cs->lastcmdbuf = NULL;
144 cs->curlen = 0;
145 }
146 spin_unlock_irqrestore(&cs->cmdlock, flags);
147
148 if (tcb->wake_tasklet)
149 tasklet_schedule(tcb->wake_tasklet);
150 kfree(tcb);
151 }
152 return sent;
153}
154
155/*
156 * send queue tasklet
157 * If there is already a skb opened, put data to the transfer buffer
158 * by calling "write_modem".
159 * Otherwise take a new skb out of the queue.
160 */
161static void gigaset_modem_fill(unsigned long data)
162{
163 struct cardstate *cs = (struct cardstate *) data;
164 struct bc_state *bcs;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000165 struct sk_buff *nextskb;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800166 int sent = 0;
167
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000168 if (!cs) {
169 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
170 return;
171 }
172 bcs = cs->bcs;
173 if (!bcs) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800174 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
175 return;
176 }
177 if (!bcs->tx_skb) {
178 /* no skb is being sent; send command if any */
179 sent = send_cb(cs);
180 gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
181 if (sent)
182 /* something sent or error */
183 return;
184
185 /* no command to send; get skb */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000186 nextskb = skb_dequeue(&bcs->squeue);
187 if (!nextskb)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800188 /* no skb either, nothing to do */
189 return;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000190 bcs->tx_skb = nextskb;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800191
192 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
193 (unsigned long) bcs->tx_skb);
194 }
195
196 /* send skb */
197 gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
198 if (write_modem(cs) < 0)
199 gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
200}
201
202/*
203 * throw away all data queued for sending
204 */
205static void flush_send_queue(struct cardstate *cs)
206{
207 struct sk_buff *skb;
208 struct cmdbuf_t *cb;
209 unsigned long flags;
210
211 /* command queue */
212 spin_lock_irqsave(&cs->cmdlock, flags);
213 while ((cb = cs->cmdbuf) != NULL) {
214 cs->cmdbuf = cb->next;
215 if (cb->wake_tasklet)
216 tasklet_schedule(cb->wake_tasklet);
217 kfree(cb);
218 }
219 cs->cmdbuf = cs->lastcmdbuf = NULL;
220 cs->cmdbytes = cs->curlen = 0;
221 spin_unlock_irqrestore(&cs->cmdlock, flags);
222
223 /* data queue */
224 if (cs->bcs->tx_skb)
225 dev_kfree_skb_any(cs->bcs->tx_skb);
226 while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
227 dev_kfree_skb_any(skb);
228}
229
230
231/* Gigaset Driver Interface */
232/* ======================== */
233
234/*
235 * queue an AT command string for transmission to the Gigaset device
236 * parameters:
237 * cs controller state structure
238 * buf buffer containing the string to send
239 * len number of characters to send
240 * wake_tasklet tasklet to run when transmission is complete, or NULL
241 * return value:
242 * number of bytes queued, or error code < 0
243 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000244static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800245{
Tilman Schmidt2869b232007-02-12 00:52:34 -0800246 unsigned long flags;
247
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800248 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Joe Perches475be4d2012-02-19 19:52:38 -0800249 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000250 "CMD Transmit", cb->len, cb->buf);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800251
252 spin_lock_irqsave(&cs->cmdlock, flags);
253 cb->prev = cs->lastcmdbuf;
254 if (cs->lastcmdbuf)
255 cs->lastcmdbuf->next = cb;
256 else {
257 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000258 cs->curlen = cb->len;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800259 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000260 cs->cmdbytes += cb->len;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800261 cs->lastcmdbuf = cb;
262 spin_unlock_irqrestore(&cs->cmdlock, flags);
263
264 spin_lock_irqsave(&cs->lock, flags);
265 if (cs->connected)
266 tasklet_schedule(&cs->write_tasklet);
267 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +0000268 return cb->len;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800269}
270
271/*
272 * tty_driver.write_room interface routine
273 * return number of characters the driver will accept to be written
274 * parameter:
275 * controller state structure
276 * return value:
277 * number of characters
278 */
279static int gigaset_write_room(struct cardstate *cs)
280{
281 unsigned bytes;
282
283 bytes = cs->cmdbytes;
284 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
285}
286
287/*
288 * tty_driver.chars_in_buffer interface routine
289 * return number of characters waiting to be sent
290 * parameter:
291 * controller state structure
292 * return value:
293 * number of characters
294 */
295static int gigaset_chars_in_buffer(struct cardstate *cs)
296{
297 return cs->cmdbytes;
298}
299
300/*
301 * implementation of ioctl(GIGASET_BRKCHARS)
302 * parameter:
303 * controller state structure
304 * return value:
305 * -EINVAL (unimplemented function)
306 */
307static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
308{
309 /* not implemented */
310 return -EINVAL;
311}
312
313/*
314 * Open B channel
315 * Called by "do_action" in ev-layer.c
316 */
317static int gigaset_init_bchannel(struct bc_state *bcs)
318{
319 /* nothing to do for M10x */
320 gigaset_bchannel_up(bcs);
321 return 0;
322}
323
324/*
325 * Close B channel
326 * Called by "do_action" in ev-layer.c
327 */
328static int gigaset_close_bchannel(struct bc_state *bcs)
329{
330 /* nothing to do for M10x */
331 gigaset_bchannel_down(bcs);
332 return 0;
333}
334
335/*
336 * Set up B channel structure
337 * This is called by "gigaset_initcs" in common.c
338 */
339static int gigaset_initbcshw(struct bc_state *bcs)
340{
341 /* unused */
342 bcs->hw.ser = NULL;
343 return 1;
344}
345
346/*
347 * Free B channel structure
348 * Called by "gigaset_freebcs" in common.c
349 */
350static int gigaset_freebcshw(struct bc_state *bcs)
351{
352 /* unused */
353 return 1;
354}
355
356/*
357 * Reinitialize B channel structure
358 * This is called by "bcs_reinit" in common.c
359 */
360static void gigaset_reinitbcshw(struct bc_state *bcs)
361{
362 /* nothing to do for M10x */
363}
364
365/*
366 * Free hardware specific device data
367 * This will be called by "gigaset_freecs" in common.c
368 */
369static void gigaset_freecshw(struct cardstate *cs)
370{
371 tasklet_kill(&cs->write_tasklet);
372 if (!cs->hw.ser)
373 return;
374 dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
375 platform_device_unregister(&cs->hw.ser->dev);
376 kfree(cs->hw.ser);
377 cs->hw.ser = NULL;
378}
379
380static void gigaset_device_release(struct device *dev)
381{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800382 struct platform_device *pdev = to_platform_device(dev);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800383
384 /* adapted from platform_device_release() in drivers/base/platform.c */
Tilman Schmidt2869b232007-02-12 00:52:34 -0800385 kfree(dev->platform_data);
386 kfree(pdev->resource);
387}
388
389/*
390 * Set up hardware specific device data
391 * This is called by "gigaset_initcs" in common.c
392 */
393static int gigaset_initcshw(struct cardstate *cs)
394{
395 int rc;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000396 struct ser_cardstate *scs;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800397
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000398 scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
399 if (!scs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800400 pr_err("out of memory\n");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800401 return 0;
402 }
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000403 cs->hw.ser = scs;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800404
405 cs->hw.ser->dev.name = GIGASET_MODULENAME;
406 cs->hw.ser->dev.id = cs->minor_index;
407 cs->hw.ser->dev.dev.release = gigaset_device_release;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000408 rc = platform_device_register(&cs->hw.ser->dev);
409 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800410 pr_err("error %d registering platform device\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800411 kfree(cs->hw.ser);
412 cs->hw.ser = NULL;
413 return 0;
414 }
415 dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
416
417 tasklet_init(&cs->write_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +0000418 gigaset_modem_fill, (unsigned long) cs);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800419 return 1;
420}
421
422/*
423 * set modem control lines
424 * Parameters:
425 * card state structure
426 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
427 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
428 * and by "if_lock" and "if_termios" in interface.c
429 */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000430static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
431 unsigned new_state)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800432{
433 struct tty_struct *tty = cs->hw.ser->tty;
434 unsigned int set, clear;
435
Alan Coxf34d7a52008-04-30 00:54:13 -0700436 if (!tty || !tty->driver || !tty->ops->tiocmset)
437 return -EINVAL;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800438 set = new_state & ~old_state;
439 clear = old_state & ~new_state;
440 if (!set && !clear)
441 return 0;
442 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
Alan Cox20b9d172011-02-14 16:26:50 +0000443 return tty->ops->tiocmset(tty, set, clear);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800444}
445
446static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
447{
448 return -EINVAL;
449}
450
451static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
452{
453 return -EINVAL;
454}
455
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700456static const struct gigaset_ops ops = {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800457 gigaset_write_cmd,
458 gigaset_write_room,
459 gigaset_chars_in_buffer,
460 gigaset_brkchars,
461 gigaset_init_bchannel,
462 gigaset_close_bchannel,
463 gigaset_initbcshw,
464 gigaset_freebcshw,
465 gigaset_reinitbcshw,
466 gigaset_initcshw,
467 gigaset_freecshw,
468 gigaset_set_modem_ctrl,
469 gigaset_baud_rate,
470 gigaset_set_line_ctrl,
471 gigaset_m10x_send_skb, /* asyncdata.c */
472 gigaset_m10x_input, /* asyncdata.c */
473};
474
475
476/* Line Discipline Interface */
477/* ========================= */
478
479/* helper functions for cardstate refcounting */
480static struct cardstate *cs_get(struct tty_struct *tty)
481{
482 struct cardstate *cs = tty->disc_data;
483
484 if (!cs || !cs->hw.ser) {
485 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
486 return NULL;
487 }
488 atomic_inc(&cs->hw.ser->refcnt);
489 return cs;
490}
491
492static void cs_put(struct cardstate *cs)
493{
494 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800495 complete(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800496}
497
498/*
499 * Called by the tty driver when the line discipline is pushed onto the tty.
500 * Called in process context.
501 */
502static int
503gigaset_tty_open(struct tty_struct *tty)
504{
505 struct cardstate *cs;
506
507 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
508
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800509 pr_info(DRIVER_DESC "\n");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800510
511 if (!driver) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800512 pr_err("%s: no driver structure\n", __func__);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800513 return -ENODEV;
514 }
515
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400516 /* allocate memory for our device state and initialize it */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000517 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
518 if (!cs)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800519 goto error;
520
521 cs->dev = &cs->hw.ser->dev.dev;
522 cs->hw.ser->tty = tty;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800523 atomic_set(&cs->hw.ser->refcnt, 1);
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800524 init_completion(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800525
526 tty->disc_data = cs;
527
528 /* OK.. Initialization of the datastructures and the HW is done.. Now
529 * startup system and notify the LL that we are ready to run
530 */
531 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800532 cs->mstate = MS_LOCKED;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800533 if (!gigaset_start(cs)) {
534 tasklet_kill(&cs->write_tasklet);
535 goto error;
536 }
537
538 gig_dbg(DEBUG_INIT, "Startup of HLL done");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800539 return 0;
540
541error:
542 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
543 tty->disc_data = NULL;
544 gigaset_freecs(cs);
545 return -ENODEV;
546}
547
548/*
549 * Called by the tty driver when the line discipline is removed.
550 * Called from process context.
551 */
552static void
553gigaset_tty_close(struct tty_struct *tty)
554{
555 struct cardstate *cs = tty->disc_data;
556
557 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
558
559 if (!cs) {
560 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
561 return;
562 }
563
564 /* prevent other callers from entering ldisc methods */
565 tty->disc_data = NULL;
566
567 if (!cs->hw.ser)
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800568 pr_err("%s: no hw cardstate\n", __func__);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800569 else {
570 /* wait for running methods to finish */
571 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800572 wait_for_completion(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800573 }
574
575 /* stop operations */
576 gigaset_stop(cs);
577 tasklet_kill(&cs->write_tasklet);
578 flush_send_queue(cs);
579 cs->dev = NULL;
580 gigaset_freecs(cs);
581
582 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
583}
584
585/*
586 * Called by the tty driver when the tty line is hung up.
587 * Wait for I/O to driver to complete and unregister ISDN device.
588 * This is already done by the close routine, so just call that.
589 * Called from process context.
590 */
591static int gigaset_tty_hangup(struct tty_struct *tty)
592{
593 gigaset_tty_close(tty);
594 return 0;
595}
596
597/*
598 * Read on the tty.
599 * Unused, received data goes only to the Gigaset driver.
600 */
601static ssize_t
602gigaset_tty_read(struct tty_struct *tty, struct file *file,
603 unsigned char __user *buf, size_t count)
604{
605 return -EAGAIN;
606}
607
608/*
609 * Write on the tty.
610 * Unused, transmit data comes only from the Gigaset driver.
611 */
612static ssize_t
613gigaset_tty_write(struct tty_struct *tty, struct file *file,
614 const unsigned char *buf, size_t count)
615{
616 return -EAGAIN;
617}
618
619/*
620 * Ioctl on the tty.
621 * Called in process context only.
622 * May be re-entered by multiple ioctl calling threads.
623 */
624static int
625gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
626 unsigned int cmd, unsigned long arg)
627{
628 struct cardstate *cs = cs_get(tty);
629 int rc, val;
630 int __user *p = (int __user *)arg;
631
632 if (!cs)
633 return -ENXIO;
634
635 switch (cmd) {
Alan Coxea1afd22008-10-13 10:44:43 +0100636
637 case FIONREAD:
638 /* unused, always return zero */
639 val = 0;
640 rc = put_user(val, p);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800641 break;
642
643 case TCFLSH:
644 /* flush our buffers and the serial port's buffer */
645 switch (arg) {
646 case TCIFLUSH:
647 /* no own input buffer to flush */
648 break;
649 case TCIOFLUSH:
650 case TCOFLUSH:
651 flush_send_queue(cs);
652 break;
653 }
Alan Coxea1afd22008-10-13 10:44:43 +0100654 /* Pass through */
Tilman Schmidt2869b232007-02-12 00:52:34 -0800655
656 default:
Alan Coxea1afd22008-10-13 10:44:43 +0100657 /* pass through to underlying serial device */
658 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
659 break;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800660 }
Tilman Schmidt2869b232007-02-12 00:52:34 -0800661 cs_put(cs);
662 return rc;
663}
664
665/*
Tilman Schmidt2869b232007-02-12 00:52:34 -0800666 * Called by the tty driver when a block of data has been received.
667 * Will not be re-entered while running but other ldisc functions
668 * may be called in parallel.
669 * Can be called from hard interrupt level as well as soft interrupt
670 * level or mainline.
671 * Parameters:
672 * tty tty structure
673 * buf buffer containing received characters
674 * cflags buffer containing error flags for received characters (ignored)
675 * count number of received characters
676 */
Linus Torvalds55db4c62011-06-04 06:33:24 +0900677static void
Tilman Schmidt2869b232007-02-12 00:52:34 -0800678gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
679 char *cflags, int count)
680{
681 struct cardstate *cs = cs_get(tty);
682 unsigned tail, head, n;
683 struct inbuf_t *inbuf;
684
685 if (!cs)
Linus Torvalds55db4c62011-06-04 06:33:24 +0900686 return;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000687 inbuf = cs->inbuf;
688 if (!inbuf) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800689 dev_err(cs->dev, "%s: no inbuf\n", __func__);
690 cs_put(cs);
Linus Torvalds55db4c62011-06-04 06:33:24 +0900691 return;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800692 }
693
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800694 tail = inbuf->tail;
695 head = inbuf->head;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800696 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
697 head, tail, count);
698
699 if (head <= tail) {
700 /* possible buffer wraparound */
701 n = min_t(unsigned, count, RBUFSIZE - tail);
702 memcpy(inbuf->data + tail, buf, n);
703 tail = (tail + n) % RBUFSIZE;
704 buf += n;
705 count -= n;
706 }
707
708 if (count > 0) {
709 /* tail < head and some data left */
710 n = head - tail - 1;
711 if (count > n) {
712 dev_err(cs->dev,
713 "inbuf overflow, discarding %d bytes\n",
714 count - n);
715 count = n;
716 }
717 memcpy(inbuf->data + tail, buf, count);
718 tail += count;
719 }
720
721 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800722 inbuf->tail = tail;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800723
724 /* Everything was received .. Push data into handler */
725 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
726 gigaset_schedule_event(cs);
727 cs_put(cs);
728}
729
730/*
731 * Called by the tty driver when there's room for more data to send.
732 */
733static void
734gigaset_tty_wakeup(struct tty_struct *tty)
735{
736 struct cardstate *cs = cs_get(tty);
737
738 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
739 if (!cs)
740 return;
741 tasklet_schedule(&cs->write_tasklet);
742 cs_put(cs);
743}
744
Alan Coxa352def2008-07-16 21:53:12 +0100745static struct tty_ldisc_ops gigaset_ldisc = {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800746 .owner = THIS_MODULE,
747 .magic = TTY_LDISC_MAGIC,
748 .name = "ser_gigaset",
749 .open = gigaset_tty_open,
750 .close = gigaset_tty_close,
751 .hangup = gigaset_tty_hangup,
752 .read = gigaset_tty_read,
753 .write = gigaset_tty_write,
754 .ioctl = gigaset_tty_ioctl,
Tilman Schmidt2869b232007-02-12 00:52:34 -0800755 .receive_buf = gigaset_tty_receive,
756 .write_wakeup = gigaset_tty_wakeup,
757};
758
759
760/* Initialization / Shutdown */
761/* ========================= */
762
763static int __init ser_gigaset_init(void)
764{
765 int rc;
766
767 gig_dbg(DEBUG_INIT, "%s", __func__);
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000768 rc = platform_driver_register(&device_driver);
769 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800770 pr_err("error %d registering platform driver\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800771 return rc;
772 }
773
Uwe Kleine-Königb5950762010-11-01 15:38:34 -0400774 /* allocate memory for our driver state and initialize it */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000775 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Joe Perches475be4d2012-02-19 19:52:38 -0800776 GIGASET_MODULENAME, GIGASET_DEVNAME,
777 &ops, THIS_MODULE);
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000778 if (!driver)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800779 goto error;
780
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000781 rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
782 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800783 pr_err("error %d registering line discipline\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800784 goto error;
785 }
786
787 return 0;
788
789error:
790 if (driver) {
791 gigaset_freedriver(driver);
792 driver = NULL;
793 }
794 platform_driver_unregister(&device_driver);
795 return rc;
796}
797
798static void __exit ser_gigaset_exit(void)
799{
800 int rc;
801
802 gig_dbg(DEBUG_INIT, "%s", __func__);
803
804 if (driver) {
805 gigaset_freedriver(driver);
806 driver = NULL;
807 }
808
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000809 rc = tty_unregister_ldisc(N_GIGASET_M101);
810 if (rc != 0)
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800811 pr_err("error %d unregistering line discipline\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800812
813 platform_driver_unregister(&device_driver);
814}
815
816module_init(ser_gigaset_init);
817module_exit(ser_gigaset_exit);