blob: 168d585d64d8550e9bfe62fb6dcef5371cf5e069 [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"
14
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/platform_device.h>
18#include <linux/tty.h>
Tilman Schmidtee51ef02008-02-06 01:38:30 -080019#include <linux/completion.h>
Tilman Schmidt2869b232007-02-12 00:52:34 -080020
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
33MODULE_AUTHOR(DRIVER_AUTHOR);
34MODULE_DESCRIPTION(DRIVER_DESC);
35MODULE_LICENSE("GPL");
36MODULE_ALIAS_LDISC(N_GIGASET_M101);
37
38static int startmode = SM_ISDN;
39module_param(startmode, int, S_IRUGO);
40MODULE_PARM_DESC(startmode, "initial operation mode");
41static int cidmode = 1;
42module_param(cidmode, int, S_IRUGO);
43MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
44
45static struct gigaset_driver *driver;
46
47struct ser_cardstate {
48 struct platform_device dev;
49 struct tty_struct *tty;
50 atomic_t refcnt;
Tilman Schmidtee51ef02008-02-06 01:38:30 -080051 struct completion dead_cmp;
Tilman Schmidt2869b232007-02-12 00:52:34 -080052};
53
54static struct platform_driver device_driver = {
55 .driver = {
56 .name = GIGASET_MODULENAME,
57 },
58};
59
60static 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 */
65static 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 Coxf34d7a52008-04-30 00:54:13 -070070 int sent = -EOPNOTSUPP;
Tilman Schmidt2869b232007-02-12 00:52:34 -080071
72 if (!tty || !tty->driver || !skb)
Alan Coxf34d7a52008-04-30 00:54:13 -070073 return -EINVAL;
Tilman Schmidt2869b232007-02-12 00:52:34 -080074
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 Coxf34d7a52008-04-30 00:54:13 -070082 if (tty->ops->write)
83 sent = tty->ops->write(tty, skb->data, skb->len);
Tilman Schmidt2869b232007-02-12 00:52:34 -080084 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 */
107static 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 Coxf34d7a52008-04-30 00:54:13 -0700123 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800124 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 */
163static void gigaset_modem_fill(unsigned long data)
164{
165 struct cardstate *cs = (struct cardstate *) data;
166 struct bc_state *bcs;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000167 struct sk_buff *nextskb;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800168 int sent = 0;
169
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000170 if (!cs) {
171 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
172 return;
173 }
174 bcs = cs->bcs;
175 if (!bcs) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800176 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
177 return;
178 }
179 if (!bcs->tx_skb) {
180 /* no skb is being sent; send command if any */
181 sent = send_cb(cs);
182 gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
183 if (sent)
184 /* something sent or error */
185 return;
186
187 /* no command to send; get skb */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000188 nextskb = skb_dequeue(&bcs->squeue);
189 if (!nextskb)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800190 /* no skb either, nothing to do */
191 return;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000192 bcs->tx_skb = nextskb;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800193
194 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
195 (unsigned long) bcs->tx_skb);
196 }
197
198 /* send skb */
199 gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
200 if (write_modem(cs) < 0)
201 gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
202}
203
204/*
205 * throw away all data queued for sending
206 */
207static void flush_send_queue(struct cardstate *cs)
208{
209 struct sk_buff *skb;
210 struct cmdbuf_t *cb;
211 unsigned long flags;
212
213 /* command queue */
214 spin_lock_irqsave(&cs->cmdlock, flags);
215 while ((cb = cs->cmdbuf) != NULL) {
216 cs->cmdbuf = cb->next;
217 if (cb->wake_tasklet)
218 tasklet_schedule(cb->wake_tasklet);
219 kfree(cb);
220 }
221 cs->cmdbuf = cs->lastcmdbuf = NULL;
222 cs->cmdbytes = cs->curlen = 0;
223 spin_unlock_irqrestore(&cs->cmdlock, flags);
224
225 /* data queue */
226 if (cs->bcs->tx_skb)
227 dev_kfree_skb_any(cs->bcs->tx_skb);
228 while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
229 dev_kfree_skb_any(skb);
230}
231
232
233/* Gigaset Driver Interface */
234/* ======================== */
235
236/*
237 * queue an AT command string for transmission to the Gigaset device
238 * parameters:
239 * cs controller state structure
240 * buf buffer containing the string to send
241 * len number of characters to send
242 * wake_tasklet tasklet to run when transmission is complete, or NULL
243 * return value:
244 * number of bytes queued, or error code < 0
245 */
246static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000247 int len, struct tasklet_struct *wake_tasklet)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800248{
249 struct cmdbuf_t *cb;
250 unsigned long flags;
251
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800252 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000253 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
254 "CMD Transmit", len, buf);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800255
256 if (len <= 0)
257 return 0;
258
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000259 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
260 if (!cb) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800261 dev_err(cs->dev, "%s: out of memory!\n", __func__);
262 return -ENOMEM;
263 }
264
265 memcpy(cb->buf, buf, len);
266 cb->len = len;
267 cb->offset = 0;
268 cb->next = NULL;
269 cb->wake_tasklet = wake_tasklet;
270
271 spin_lock_irqsave(&cs->cmdlock, flags);
272 cb->prev = cs->lastcmdbuf;
273 if (cs->lastcmdbuf)
274 cs->lastcmdbuf->next = cb;
275 else {
276 cs->cmdbuf = cb;
277 cs->curlen = len;
278 }
279 cs->cmdbytes += len;
280 cs->lastcmdbuf = cb;
281 spin_unlock_irqrestore(&cs->cmdlock, flags);
282
283 spin_lock_irqsave(&cs->lock, flags);
284 if (cs->connected)
285 tasklet_schedule(&cs->write_tasklet);
286 spin_unlock_irqrestore(&cs->lock, flags);
287 return len;
288}
289
290/*
291 * tty_driver.write_room interface routine
292 * return number of characters the driver will accept to be written
293 * parameter:
294 * controller state structure
295 * return value:
296 * number of characters
297 */
298static int gigaset_write_room(struct cardstate *cs)
299{
300 unsigned bytes;
301
302 bytes = cs->cmdbytes;
303 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
304}
305
306/*
307 * tty_driver.chars_in_buffer interface routine
308 * return number of characters waiting to be sent
309 * parameter:
310 * controller state structure
311 * return value:
312 * number of characters
313 */
314static int gigaset_chars_in_buffer(struct cardstate *cs)
315{
316 return cs->cmdbytes;
317}
318
319/*
320 * implementation of ioctl(GIGASET_BRKCHARS)
321 * parameter:
322 * controller state structure
323 * return value:
324 * -EINVAL (unimplemented function)
325 */
326static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
327{
328 /* not implemented */
329 return -EINVAL;
330}
331
332/*
333 * Open B channel
334 * Called by "do_action" in ev-layer.c
335 */
336static int gigaset_init_bchannel(struct bc_state *bcs)
337{
338 /* nothing to do for M10x */
339 gigaset_bchannel_up(bcs);
340 return 0;
341}
342
343/*
344 * Close B channel
345 * Called by "do_action" in ev-layer.c
346 */
347static int gigaset_close_bchannel(struct bc_state *bcs)
348{
349 /* nothing to do for M10x */
350 gigaset_bchannel_down(bcs);
351 return 0;
352}
353
354/*
355 * Set up B channel structure
356 * This is called by "gigaset_initcs" in common.c
357 */
358static int gigaset_initbcshw(struct bc_state *bcs)
359{
360 /* unused */
361 bcs->hw.ser = NULL;
362 return 1;
363}
364
365/*
366 * Free B channel structure
367 * Called by "gigaset_freebcs" in common.c
368 */
369static int gigaset_freebcshw(struct bc_state *bcs)
370{
371 /* unused */
372 return 1;
373}
374
375/*
376 * Reinitialize B channel structure
377 * This is called by "bcs_reinit" in common.c
378 */
379static void gigaset_reinitbcshw(struct bc_state *bcs)
380{
381 /* nothing to do for M10x */
382}
383
384/*
385 * Free hardware specific device data
386 * This will be called by "gigaset_freecs" in common.c
387 */
388static void gigaset_freecshw(struct cardstate *cs)
389{
390 tasklet_kill(&cs->write_tasklet);
391 if (!cs->hw.ser)
392 return;
393 dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
394 platform_device_unregister(&cs->hw.ser->dev);
395 kfree(cs->hw.ser);
396 cs->hw.ser = NULL;
397}
398
399static void gigaset_device_release(struct device *dev)
400{
Eric Miao71b3e0c2009-01-31 22:47:44 +0800401 struct platform_device *pdev = to_platform_device(dev);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800402
403 /* adapted from platform_device_release() in drivers/base/platform.c */
Tilman Schmidt2869b232007-02-12 00:52:34 -0800404 kfree(dev->platform_data);
405 kfree(pdev->resource);
406}
407
408/*
409 * Set up hardware specific device data
410 * This is called by "gigaset_initcs" in common.c
411 */
412static int gigaset_initcshw(struct cardstate *cs)
413{
414 int rc;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000415 struct ser_cardstate *scs;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800416
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000417 scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
418 if (!scs) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800419 pr_err("out of memory\n");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800420 return 0;
421 }
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000422 cs->hw.ser = scs;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800423
424 cs->hw.ser->dev.name = GIGASET_MODULENAME;
425 cs->hw.ser->dev.id = cs->minor_index;
426 cs->hw.ser->dev.dev.release = gigaset_device_release;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000427 rc = platform_device_register(&cs->hw.ser->dev);
428 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800429 pr_err("error %d registering platform device\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800430 kfree(cs->hw.ser);
431 cs->hw.ser = NULL;
432 return 0;
433 }
434 dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
435
436 tasklet_init(&cs->write_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +0000437 gigaset_modem_fill, (unsigned long) cs);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800438 return 1;
439}
440
441/*
442 * set modem control lines
443 * Parameters:
444 * card state structure
445 * modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
446 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
447 * and by "if_lock" and "if_termios" in interface.c
448 */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000449static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
450 unsigned new_state)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800451{
452 struct tty_struct *tty = cs->hw.ser->tty;
453 unsigned int set, clear;
454
Alan Coxf34d7a52008-04-30 00:54:13 -0700455 if (!tty || !tty->driver || !tty->ops->tiocmset)
456 return -EINVAL;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800457 set = new_state & ~old_state;
458 clear = old_state & ~new_state;
459 if (!set && !clear)
460 return 0;
461 gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
Alan Coxf34d7a52008-04-30 00:54:13 -0700462 return tty->ops->tiocmset(tty, NULL, set, clear);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800463}
464
465static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
466{
467 return -EINVAL;
468}
469
470static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
471{
472 return -EINVAL;
473}
474
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700475static const struct gigaset_ops ops = {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800476 gigaset_write_cmd,
477 gigaset_write_room,
478 gigaset_chars_in_buffer,
479 gigaset_brkchars,
480 gigaset_init_bchannel,
481 gigaset_close_bchannel,
482 gigaset_initbcshw,
483 gigaset_freebcshw,
484 gigaset_reinitbcshw,
485 gigaset_initcshw,
486 gigaset_freecshw,
487 gigaset_set_modem_ctrl,
488 gigaset_baud_rate,
489 gigaset_set_line_ctrl,
490 gigaset_m10x_send_skb, /* asyncdata.c */
491 gigaset_m10x_input, /* asyncdata.c */
492};
493
494
495/* Line Discipline Interface */
496/* ========================= */
497
498/* helper functions for cardstate refcounting */
499static struct cardstate *cs_get(struct tty_struct *tty)
500{
501 struct cardstate *cs = tty->disc_data;
502
503 if (!cs || !cs->hw.ser) {
504 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
505 return NULL;
506 }
507 atomic_inc(&cs->hw.ser->refcnt);
508 return cs;
509}
510
511static void cs_put(struct cardstate *cs)
512{
513 if (atomic_dec_and_test(&cs->hw.ser->refcnt))
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800514 complete(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800515}
516
517/*
518 * Called by the tty driver when the line discipline is pushed onto the tty.
519 * Called in process context.
520 */
521static int
522gigaset_tty_open(struct tty_struct *tty)
523{
524 struct cardstate *cs;
525
526 gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
527
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800528 pr_info(DRIVER_DESC "\n");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800529
530 if (!driver) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800531 pr_err("%s: no driver structure\n", __func__);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800532 return -ENODEV;
533 }
534
535 /* allocate memory for our device state and intialize it */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000536 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
537 if (!cs)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800538 goto error;
539
540 cs->dev = &cs->hw.ser->dev.dev;
541 cs->hw.ser->tty = tty;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800542 atomic_set(&cs->hw.ser->refcnt, 1);
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800543 init_completion(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800544
545 tty->disc_data = cs;
546
547 /* OK.. Initialization of the datastructures and the HW is done.. Now
548 * startup system and notify the LL that we are ready to run
549 */
550 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800551 cs->mstate = MS_LOCKED;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800552 if (!gigaset_start(cs)) {
553 tasklet_kill(&cs->write_tasklet);
554 goto error;
555 }
556
557 gig_dbg(DEBUG_INIT, "Startup of HLL done");
Tilman Schmidt2869b232007-02-12 00:52:34 -0800558 return 0;
559
560error:
561 gig_dbg(DEBUG_INIT, "Startup of HLL failed");
562 tty->disc_data = NULL;
563 gigaset_freecs(cs);
564 return -ENODEV;
565}
566
567/*
568 * Called by the tty driver when the line discipline is removed.
569 * Called from process context.
570 */
571static void
572gigaset_tty_close(struct tty_struct *tty)
573{
574 struct cardstate *cs = tty->disc_data;
575
576 gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
577
578 if (!cs) {
579 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
580 return;
581 }
582
583 /* prevent other callers from entering ldisc methods */
584 tty->disc_data = NULL;
585
586 if (!cs->hw.ser)
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800587 pr_err("%s: no hw cardstate\n", __func__);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800588 else {
589 /* wait for running methods to finish */
590 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
Tilman Schmidtee51ef02008-02-06 01:38:30 -0800591 wait_for_completion(&cs->hw.ser->dead_cmp);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800592 }
593
594 /* stop operations */
595 gigaset_stop(cs);
596 tasklet_kill(&cs->write_tasklet);
597 flush_send_queue(cs);
598 cs->dev = NULL;
599 gigaset_freecs(cs);
600
601 gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
602}
603
604/*
605 * Called by the tty driver when the tty line is hung up.
606 * Wait for I/O to driver to complete and unregister ISDN device.
607 * This is already done by the close routine, so just call that.
608 * Called from process context.
609 */
610static int gigaset_tty_hangup(struct tty_struct *tty)
611{
612 gigaset_tty_close(tty);
613 return 0;
614}
615
616/*
617 * Read on the tty.
618 * Unused, received data goes only to the Gigaset driver.
619 */
620static ssize_t
621gigaset_tty_read(struct tty_struct *tty, struct file *file,
622 unsigned char __user *buf, size_t count)
623{
624 return -EAGAIN;
625}
626
627/*
628 * Write on the tty.
629 * Unused, transmit data comes only from the Gigaset driver.
630 */
631static ssize_t
632gigaset_tty_write(struct tty_struct *tty, struct file *file,
633 const unsigned char *buf, size_t count)
634{
635 return -EAGAIN;
636}
637
638/*
639 * Ioctl on the tty.
640 * Called in process context only.
641 * May be re-entered by multiple ioctl calling threads.
642 */
643static int
644gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
645 unsigned int cmd, unsigned long arg)
646{
647 struct cardstate *cs = cs_get(tty);
648 int rc, val;
649 int __user *p = (int __user *)arg;
650
651 if (!cs)
652 return -ENXIO;
653
654 switch (cmd) {
Alan Coxea1afd22008-10-13 10:44:43 +0100655
656 case FIONREAD:
657 /* unused, always return zero */
658 val = 0;
659 rc = put_user(val, p);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800660 break;
661
662 case TCFLSH:
663 /* flush our buffers and the serial port's buffer */
664 switch (arg) {
665 case TCIFLUSH:
666 /* no own input buffer to flush */
667 break;
668 case TCIOFLUSH:
669 case TCOFLUSH:
670 flush_send_queue(cs);
671 break;
672 }
Alan Coxea1afd22008-10-13 10:44:43 +0100673 /* Pass through */
Tilman Schmidt2869b232007-02-12 00:52:34 -0800674
675 default:
Alan Coxea1afd22008-10-13 10:44:43 +0100676 /* pass through to underlying serial device */
677 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
678 break;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800679 }
Tilman Schmidt2869b232007-02-12 00:52:34 -0800680 cs_put(cs);
681 return rc;
682}
683
684/*
Tilman Schmidt2869b232007-02-12 00:52:34 -0800685 * Called by the tty driver when a block of data has been received.
686 * Will not be re-entered while running but other ldisc functions
687 * may be called in parallel.
688 * Can be called from hard interrupt level as well as soft interrupt
689 * level or mainline.
690 * Parameters:
691 * tty tty structure
692 * buf buffer containing received characters
693 * cflags buffer containing error flags for received characters (ignored)
694 * count number of received characters
695 */
696static void
697gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
698 char *cflags, int count)
699{
700 struct cardstate *cs = cs_get(tty);
701 unsigned tail, head, n;
702 struct inbuf_t *inbuf;
703
704 if (!cs)
705 return;
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000706 inbuf = cs->inbuf;
707 if (!inbuf) {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800708 dev_err(cs->dev, "%s: no inbuf\n", __func__);
709 cs_put(cs);
710 return;
711 }
712
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800713 tail = inbuf->tail;
714 head = inbuf->head;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800715 gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
716 head, tail, count);
717
718 if (head <= tail) {
719 /* possible buffer wraparound */
720 n = min_t(unsigned, count, RBUFSIZE - tail);
721 memcpy(inbuf->data + tail, buf, n);
722 tail = (tail + n) % RBUFSIZE;
723 buf += n;
724 count -= n;
725 }
726
727 if (count > 0) {
728 /* tail < head and some data left */
729 n = head - tail - 1;
730 if (count > n) {
731 dev_err(cs->dev,
732 "inbuf overflow, discarding %d bytes\n",
733 count - n);
734 count = n;
735 }
736 memcpy(inbuf->data + tail, buf, count);
737 tail += count;
738 }
739
740 gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800741 inbuf->tail = tail;
Tilman Schmidt2869b232007-02-12 00:52:34 -0800742
743 /* Everything was received .. Push data into handler */
744 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
745 gigaset_schedule_event(cs);
746 cs_put(cs);
747}
748
749/*
750 * Called by the tty driver when there's room for more data to send.
751 */
752static void
753gigaset_tty_wakeup(struct tty_struct *tty)
754{
755 struct cardstate *cs = cs_get(tty);
756
757 clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
758 if (!cs)
759 return;
760 tasklet_schedule(&cs->write_tasklet);
761 cs_put(cs);
762}
763
Alan Coxa352def2008-07-16 21:53:12 +0100764static struct tty_ldisc_ops gigaset_ldisc = {
Tilman Schmidt2869b232007-02-12 00:52:34 -0800765 .owner = THIS_MODULE,
766 .magic = TTY_LDISC_MAGIC,
767 .name = "ser_gigaset",
768 .open = gigaset_tty_open,
769 .close = gigaset_tty_close,
770 .hangup = gigaset_tty_hangup,
771 .read = gigaset_tty_read,
772 .write = gigaset_tty_write,
773 .ioctl = gigaset_tty_ioctl,
Tilman Schmidt2869b232007-02-12 00:52:34 -0800774 .receive_buf = gigaset_tty_receive,
775 .write_wakeup = gigaset_tty_wakeup,
776};
777
778
779/* Initialization / Shutdown */
780/* ========================= */
781
782static int __init ser_gigaset_init(void)
783{
784 int rc;
785
786 gig_dbg(DEBUG_INIT, "%s", __func__);
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000787 rc = platform_driver_register(&device_driver);
788 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800789 pr_err("error %d registering platform driver\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800790 return rc;
791 }
792
793 /* allocate memory for our driver state and intialize it */
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000794 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt2869b232007-02-12 00:52:34 -0800795 GIGASET_MODULENAME, GIGASET_DEVNAME,
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000796 &ops, THIS_MODULE);
797 if (!driver)
Tilman Schmidt2869b232007-02-12 00:52:34 -0800798 goto error;
799
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000800 rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
801 if (rc != 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800802 pr_err("error %d registering line discipline\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800803 goto error;
804 }
805
806 return 0;
807
808error:
809 if (driver) {
810 gigaset_freedriver(driver);
811 driver = NULL;
812 }
813 platform_driver_unregister(&device_driver);
814 return rc;
815}
816
817static 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
Tilman Schmidtae67d7d2009-10-25 09:30:27 +0000828 rc = tty_unregister_ldisc(N_GIGASET_M101);
829 if (rc != 0)
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800830 pr_err("error %d unregistering line discipline\n", rc);
Tilman Schmidt2869b232007-02-12 00:52:34 -0800831
832 platform_driver_unregister(&device_driver);
833}
834
835module_init(ser_gigaset_init);
836module_exit(ser_gigaset_exit);