isdn/gigaset: reset tty->receive_room when attaching ser_gigaset
[pandora-kernel.git] / drivers / isdn / gigaset / ser-gigaset.c
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 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/platform_device.h>
17 #include <linux/completion.h>
18
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
31 MODULE_AUTHOR(DRIVER_AUTHOR);
32 MODULE_DESCRIPTION(DRIVER_DESC);
33 MODULE_LICENSE("GPL");
34 MODULE_ALIAS_LDISC(N_GIGASET_M101);
35
36 static int startmode = SM_ISDN;
37 module_param(startmode, int, S_IRUGO);
38 MODULE_PARM_DESC(startmode, "initial operation mode");
39 static int cidmode = 1;
40 module_param(cidmode, int, S_IRUGO);
41 MODULE_PARM_DESC(cidmode, "stay in CID mode when idle");
42
43 static struct gigaset_driver *driver;
44
45 struct ser_cardstate {
46         struct platform_device  dev;
47         struct tty_struct       *tty;
48         atomic_t                refcnt;
49         struct completion       dead_cmp;
50 };
51
52 static struct platform_driver device_driver = {
53         .driver = {
54                 .name = GIGASET_MODULENAME,
55         },
56 };
57
58 static 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  */
63 static 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;
68         int sent = -EOPNOTSUPP;
69
70         if (!tty || !tty->driver || !skb)
71                 return -EINVAL;
72
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);
80         if (tty->ops->write)
81                 sent = tty->ops->write(tty, skb->data, skb->len);
82         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  */
105 static 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);
121                 sent = tty->ops->write(tty, cb->buf + cb->offset, cb->len);
122                 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  */
161 static void gigaset_modem_fill(unsigned long data)
162 {
163         struct cardstate *cs = (struct cardstate *) data;
164         struct bc_state *bcs;
165         struct sk_buff *nextskb;
166         int sent = 0;
167
168         if (!cs) {
169                 gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
170                 return;
171         }
172         bcs = cs->bcs;
173         if (!bcs) {
174                 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 */
186                 nextskb = skb_dequeue(&bcs->squeue);
187                 if (!nextskb)
188                         /* no skb either, nothing to do */
189                         return;
190                 bcs->tx_skb = nextskb;
191
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  */
205 static 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  */
244 static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
245 {
246         unsigned long flags;
247
248         gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
249                                 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
250                            "CMD Transmit", cb->len, cb->buf);
251
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;
258                 cs->curlen = cb->len;
259         }
260         cs->cmdbytes += cb->len;
261         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);
268         return cb->len;
269 }
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  */
279 static 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  */
295 static 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  */
307 static 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  */
317 static 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  */
328 static 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  */
339 static 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  */
350 static 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  */
360 static 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  */
369 static void gigaset_freecshw(struct cardstate *cs)
370 {
371         tasklet_kill(&cs->write_tasklet);
372         if (!cs->hw.ser)
373                 return;
374         platform_device_unregister(&cs->hw.ser->dev);
375 }
376
377 static void gigaset_device_release(struct device *dev)
378 {
379         struct platform_device *pdev = to_platform_device(dev);
380         struct cardstate *cs = dev_get_drvdata(dev);
381
382         /* adapted from platform_device_release() in drivers/base/platform.c */
383         kfree(dev->platform_data);
384         kfree(pdev->resource);
385
386         if (!cs)
387                 return;
388         dev_set_drvdata(dev, NULL);
389         kfree(cs->hw.ser);
390         cs->hw.ser = NULL;
391 }
392
393 /*
394  * Set up hardware specific device data
395  * This is called by "gigaset_initcs" in common.c
396  */
397 static int gigaset_initcshw(struct cardstate *cs)
398 {
399         int rc;
400         struct ser_cardstate *scs;
401
402         scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
403         if (!scs) {
404                 pr_err("out of memory\n");
405                 return 0;
406         }
407         cs->hw.ser = scs;
408
409         cs->hw.ser->dev.name = GIGASET_MODULENAME;
410         cs->hw.ser->dev.id = cs->minor_index;
411         cs->hw.ser->dev.dev.release = gigaset_device_release;
412         rc = platform_device_register(&cs->hw.ser->dev);
413         if (rc != 0) {
414                 pr_err("error %d registering platform device\n", rc);
415                 kfree(cs->hw.ser);
416                 cs->hw.ser = NULL;
417                 return 0;
418         }
419         dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
420
421         tasklet_init(&cs->write_tasklet,
422                      gigaset_modem_fill, (unsigned long) cs);
423         return 1;
424 }
425
426 /*
427  * set modem control lines
428  * Parameters:
429  *      card state structure
430  *      modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
431  * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
432  * and by "if_lock" and "if_termios" in interface.c
433  */
434 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
435                                   unsigned new_state)
436 {
437         struct tty_struct *tty = cs->hw.ser->tty;
438         unsigned int set, clear;
439
440         if (!tty || !tty->driver || !tty->ops->tiocmset)
441                 return -EINVAL;
442         set = new_state & ~old_state;
443         clear = old_state & ~new_state;
444         if (!set && !clear)
445                 return 0;
446         gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
447         return tty->ops->tiocmset(tty, set, clear);
448 }
449
450 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
451 {
452         return -EINVAL;
453 }
454
455 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
456 {
457         return -EINVAL;
458 }
459
460 static const struct gigaset_ops ops = {
461         gigaset_write_cmd,
462         gigaset_write_room,
463         gigaset_chars_in_buffer,
464         gigaset_brkchars,
465         gigaset_init_bchannel,
466         gigaset_close_bchannel,
467         gigaset_initbcshw,
468         gigaset_freebcshw,
469         gigaset_reinitbcshw,
470         gigaset_initcshw,
471         gigaset_freecshw,
472         gigaset_set_modem_ctrl,
473         gigaset_baud_rate,
474         gigaset_set_line_ctrl,
475         gigaset_m10x_send_skb,  /* asyncdata.c */
476         gigaset_m10x_input,     /* asyncdata.c */
477 };
478
479
480 /* Line Discipline Interface */
481 /* ========================= */
482
483 /* helper functions for cardstate refcounting */
484 static struct cardstate *cs_get(struct tty_struct *tty)
485 {
486         struct cardstate *cs = tty->disc_data;
487
488         if (!cs || !cs->hw.ser) {
489                 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
490                 return NULL;
491         }
492         atomic_inc(&cs->hw.ser->refcnt);
493         return cs;
494 }
495
496 static void cs_put(struct cardstate *cs)
497 {
498         if (atomic_dec_and_test(&cs->hw.ser->refcnt))
499                 complete(&cs->hw.ser->dead_cmp);
500 }
501
502 /*
503  * Called by the tty driver when the line discipline is pushed onto the tty.
504  * Called in process context.
505  */
506 static int
507 gigaset_tty_open(struct tty_struct *tty)
508 {
509         struct cardstate *cs;
510
511         gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
512
513         pr_info(DRIVER_DESC "\n");
514
515         if (!driver) {
516                 pr_err("%s: no driver structure\n", __func__);
517                 return -ENODEV;
518         }
519
520         /* allocate memory for our device state and initialize it */
521         cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
522         if (!cs)
523                 goto error;
524
525         cs->dev = &cs->hw.ser->dev.dev;
526         cs->hw.ser->tty = tty;
527         atomic_set(&cs->hw.ser->refcnt, 1);
528         init_completion(&cs->hw.ser->dead_cmp);
529         tty->disc_data = cs;
530
531         /* Set the amount of data we're willing to receive per call
532          * from the hardware driver to half of the input buffer size
533          * to leave some reserve.
534          * Note: We don't do flow control towards the hardware driver.
535          * If more data is received than will fit into the input buffer,
536          * it will be dropped and an error will be logged. This should
537          * never happen as the device is slow and the buffer size ample.
538          */
539         tty->receive_room = RBUFSIZE/2;
540
541         /* OK.. Initialization of the datastructures and the HW is done.. Now
542          * startup system and notify the LL that we are ready to run
543          */
544         if (startmode == SM_LOCKED)
545                 cs->mstate = MS_LOCKED;
546         if (!gigaset_start(cs)) {
547                 tasklet_kill(&cs->write_tasklet);
548                 goto error;
549         }
550
551         gig_dbg(DEBUG_INIT, "Startup of HLL done");
552         return 0;
553
554 error:
555         gig_dbg(DEBUG_INIT, "Startup of HLL failed");
556         tty->disc_data = NULL;
557         gigaset_freecs(cs);
558         return -ENODEV;
559 }
560
561 /*
562  * Called by the tty driver when the line discipline is removed.
563  * Called from process context.
564  */
565 static void
566 gigaset_tty_close(struct tty_struct *tty)
567 {
568         struct cardstate *cs = tty->disc_data;
569
570         gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
571
572         if (!cs) {
573                 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
574                 return;
575         }
576
577         /* prevent other callers from entering ldisc methods */
578         tty->disc_data = NULL;
579
580         if (!cs->hw.ser)
581                 pr_err("%s: no hw cardstate\n", __func__);
582         else {
583                 /* wait for running methods to finish */
584                 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
585                         wait_for_completion(&cs->hw.ser->dead_cmp);
586         }
587
588         /* stop operations */
589         gigaset_stop(cs);
590         tasklet_kill(&cs->write_tasklet);
591         flush_send_queue(cs);
592         cs->dev = NULL;
593         gigaset_freecs(cs);
594
595         gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
596 }
597
598 /*
599  * Called by the tty driver when the tty line is hung up.
600  * Wait for I/O to driver to complete and unregister ISDN device.
601  * This is already done by the close routine, so just call that.
602  * Called from process context.
603  */
604 static int gigaset_tty_hangup(struct tty_struct *tty)
605 {
606         gigaset_tty_close(tty);
607         return 0;
608 }
609
610 /*
611  * Read on the tty.
612  * Unused, received data goes only to the Gigaset driver.
613  */
614 static ssize_t
615 gigaset_tty_read(struct tty_struct *tty, struct file *file,
616                  unsigned char __user *buf, size_t count)
617 {
618         return -EAGAIN;
619 }
620
621 /*
622  * Write on the tty.
623  * Unused, transmit data comes only from the Gigaset driver.
624  */
625 static ssize_t
626 gigaset_tty_write(struct tty_struct *tty, struct file *file,
627                   const unsigned char *buf, size_t count)
628 {
629         return -EAGAIN;
630 }
631
632 /*
633  * Ioctl on the tty.
634  * Called in process context only.
635  * May be re-entered by multiple ioctl calling threads.
636  */
637 static int
638 gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
639                   unsigned int cmd, unsigned long arg)
640 {
641         struct cardstate *cs = cs_get(tty);
642         int rc, val;
643         int __user *p = (int __user *)arg;
644
645         if (!cs)
646                 return -ENXIO;
647
648         switch (cmd) {
649
650         case FIONREAD:
651                 /* unused, always return zero */
652                 val = 0;
653                 rc = put_user(val, p);
654                 break;
655
656         case TCFLSH:
657                 /* flush our buffers and the serial port's buffer */
658                 switch (arg) {
659                 case TCIFLUSH:
660                         /* no own input buffer to flush */
661                         break;
662                 case TCIOFLUSH:
663                 case TCOFLUSH:
664                         flush_send_queue(cs);
665                         break;
666                 }
667                 /* Pass through */
668
669         default:
670                 /* pass through to underlying serial device */
671                 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
672                 break;
673         }
674         cs_put(cs);
675         return rc;
676 }
677
678 /*
679  * Called by the tty driver when a block of data has been received.
680  * Will not be re-entered while running but other ldisc functions
681  * may be called in parallel.
682  * Can be called from hard interrupt level as well as soft interrupt
683  * level or mainline.
684  * Parameters:
685  *      tty     tty structure
686  *      buf     buffer containing received characters
687  *      cflags  buffer containing error flags for received characters (ignored)
688  *      count   number of received characters
689  */
690 static void
691 gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
692                     char *cflags, int count)
693 {
694         struct cardstate *cs = cs_get(tty);
695         unsigned tail, head, n;
696         struct inbuf_t *inbuf;
697
698         if (!cs)
699                 return;
700         inbuf = cs->inbuf;
701         if (!inbuf) {
702                 dev_err(cs->dev, "%s: no inbuf\n", __func__);
703                 cs_put(cs);
704                 return;
705         }
706
707         tail = inbuf->tail;
708         head = inbuf->head;
709         gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
710                 head, tail, count);
711
712         if (head <= tail) {
713                 /* possible buffer wraparound */
714                 n = min_t(unsigned, count, RBUFSIZE - tail);
715                 memcpy(inbuf->data + tail, buf, n);
716                 tail = (tail + n) % RBUFSIZE;
717                 buf += n;
718                 count -= n;
719         }
720
721         if (count > 0) {
722                 /* tail < head and some data left */
723                 n = head - tail - 1;
724                 if (count > n) {
725                         dev_err(cs->dev,
726                                 "inbuf overflow, discarding %d bytes\n",
727                                 count - n);
728                         count = n;
729                 }
730                 memcpy(inbuf->data + tail, buf, count);
731                 tail += count;
732         }
733
734         gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
735         inbuf->tail = tail;
736
737         /* Everything was received .. Push data into handler */
738         gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
739         gigaset_schedule_event(cs);
740         cs_put(cs);
741 }
742
743 /*
744  * Called by the tty driver when there's room for more data to send.
745  */
746 static void
747 gigaset_tty_wakeup(struct tty_struct *tty)
748 {
749         struct cardstate *cs = cs_get(tty);
750
751         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
752         if (!cs)
753                 return;
754         tasklet_schedule(&cs->write_tasklet);
755         cs_put(cs);
756 }
757
758 static struct tty_ldisc_ops gigaset_ldisc = {
759         .owner          = THIS_MODULE,
760         .magic          = TTY_LDISC_MAGIC,
761         .name           = "ser_gigaset",
762         .open           = gigaset_tty_open,
763         .close          = gigaset_tty_close,
764         .hangup         = gigaset_tty_hangup,
765         .read           = gigaset_tty_read,
766         .write          = gigaset_tty_write,
767         .ioctl          = gigaset_tty_ioctl,
768         .receive_buf    = gigaset_tty_receive,
769         .write_wakeup   = gigaset_tty_wakeup,
770 };
771
772
773 /* Initialization / Shutdown */
774 /* ========================= */
775
776 static int __init ser_gigaset_init(void)
777 {
778         int rc;
779
780         gig_dbg(DEBUG_INIT, "%s", __func__);
781         rc = platform_driver_register(&device_driver);
782         if (rc != 0) {
783                 pr_err("error %d registering platform driver\n", rc);
784                 return rc;
785         }
786
787         /* allocate memory for our driver state and initialize it */
788         driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
789                                           GIGASET_MODULENAME, GIGASET_DEVNAME,
790                                           &ops, THIS_MODULE);
791         if (!driver) {
792                 rc = -ENOMEM;
793                 goto error;
794         }
795
796         rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
797         if (rc != 0) {
798                 pr_err("error %d registering line discipline\n", rc);
799                 goto error;
800         }
801
802         return 0;
803
804 error:
805         if (driver) {
806                 gigaset_freedriver(driver);
807                 driver = NULL;
808         }
809         platform_driver_unregister(&device_driver);
810         return rc;
811 }
812
813 static void __exit ser_gigaset_exit(void)
814 {
815         int rc;
816
817         gig_dbg(DEBUG_INIT, "%s", __func__);
818
819         if (driver) {
820                 gigaset_freedriver(driver);
821                 driver = NULL;
822         }
823
824         rc = tty_unregister_ldisc(N_GIGASET_M101);
825         if (rc != 0)
826                 pr_err("error %d unregistering line discipline\n", rc);
827
828         platform_driver_unregister(&device_driver);
829 }
830
831 module_init(ser_gigaset_init);
832 module_exit(ser_gigaset_exit);