Merge branch 'egalax' into for-linus
[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, const unsigned char *buf,
245                              int len, struct tasklet_struct *wake_tasklet)
246 {
247         struct cmdbuf_t *cb;
248         unsigned long flags;
249
250         gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
251                                 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
252                            "CMD Transmit", len, buf);
253
254         if (len <= 0)
255                 return 0;
256
257         cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
258         if (!cb) {
259                 dev_err(cs->dev, "%s: out of memory!\n", __func__);
260                 return -ENOMEM;
261         }
262
263         memcpy(cb->buf, buf, len);
264         cb->len = len;
265         cb->offset = 0;
266         cb->next = NULL;
267         cb->wake_tasklet = wake_tasklet;
268
269         spin_lock_irqsave(&cs->cmdlock, flags);
270         cb->prev = cs->lastcmdbuf;
271         if (cs->lastcmdbuf)
272                 cs->lastcmdbuf->next = cb;
273         else {
274                 cs->cmdbuf = cb;
275                 cs->curlen = len;
276         }
277         cs->cmdbytes += len;
278         cs->lastcmdbuf = cb;
279         spin_unlock_irqrestore(&cs->cmdlock, flags);
280
281         spin_lock_irqsave(&cs->lock, flags);
282         if (cs->connected)
283                 tasklet_schedule(&cs->write_tasklet);
284         spin_unlock_irqrestore(&cs->lock, flags);
285         return len;
286 }
287
288 /*
289  * tty_driver.write_room interface routine
290  * return number of characters the driver will accept to be written
291  * parameter:
292  *      controller state structure
293  * return value:
294  *      number of characters
295  */
296 static int gigaset_write_room(struct cardstate *cs)
297 {
298         unsigned bytes;
299
300         bytes = cs->cmdbytes;
301         return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
302 }
303
304 /*
305  * tty_driver.chars_in_buffer interface routine
306  * return number of characters waiting to be sent
307  * parameter:
308  *      controller state structure
309  * return value:
310  *      number of characters
311  */
312 static int gigaset_chars_in_buffer(struct cardstate *cs)
313 {
314         return cs->cmdbytes;
315 }
316
317 /*
318  * implementation of ioctl(GIGASET_BRKCHARS)
319  * parameter:
320  *      controller state structure
321  * return value:
322  *      -EINVAL (unimplemented function)
323  */
324 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
325 {
326         /* not implemented */
327         return -EINVAL;
328 }
329
330 /*
331  * Open B channel
332  * Called by "do_action" in ev-layer.c
333  */
334 static int gigaset_init_bchannel(struct bc_state *bcs)
335 {
336         /* nothing to do for M10x */
337         gigaset_bchannel_up(bcs);
338         return 0;
339 }
340
341 /*
342  * Close B channel
343  * Called by "do_action" in ev-layer.c
344  */
345 static int gigaset_close_bchannel(struct bc_state *bcs)
346 {
347         /* nothing to do for M10x */
348         gigaset_bchannel_down(bcs);
349         return 0;
350 }
351
352 /*
353  * Set up B channel structure
354  * This is called by "gigaset_initcs" in common.c
355  */
356 static int gigaset_initbcshw(struct bc_state *bcs)
357 {
358         /* unused */
359         bcs->hw.ser = NULL;
360         return 1;
361 }
362
363 /*
364  * Free B channel structure
365  * Called by "gigaset_freebcs" in common.c
366  */
367 static int gigaset_freebcshw(struct bc_state *bcs)
368 {
369         /* unused */
370         return 1;
371 }
372
373 /*
374  * Reinitialize B channel structure
375  * This is called by "bcs_reinit" in common.c
376  */
377 static void gigaset_reinitbcshw(struct bc_state *bcs)
378 {
379         /* nothing to do for M10x */
380 }
381
382 /*
383  * Free hardware specific device data
384  * This will be called by "gigaset_freecs" in common.c
385  */
386 static void gigaset_freecshw(struct cardstate *cs)
387 {
388         tasklet_kill(&cs->write_tasklet);
389         if (!cs->hw.ser)
390                 return;
391         dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
392         platform_device_unregister(&cs->hw.ser->dev);
393         kfree(cs->hw.ser);
394         cs->hw.ser = NULL;
395 }
396
397 static void gigaset_device_release(struct device *dev)
398 {
399         struct platform_device *pdev = to_platform_device(dev);
400
401         /* adapted from platform_device_release() in drivers/base/platform.c */
402         kfree(dev->platform_data);
403         kfree(pdev->resource);
404 }
405
406 /*
407  * Set up hardware specific device data
408  * This is called by "gigaset_initcs" in common.c
409  */
410 static int gigaset_initcshw(struct cardstate *cs)
411 {
412         int rc;
413         struct ser_cardstate *scs;
414
415         scs = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL);
416         if (!scs) {
417                 pr_err("out of memory\n");
418                 return 0;
419         }
420         cs->hw.ser = scs;
421
422         cs->hw.ser->dev.name = GIGASET_MODULENAME;
423         cs->hw.ser->dev.id = cs->minor_index;
424         cs->hw.ser->dev.dev.release = gigaset_device_release;
425         rc = platform_device_register(&cs->hw.ser->dev);
426         if (rc != 0) {
427                 pr_err("error %d registering platform device\n", rc);
428                 kfree(cs->hw.ser);
429                 cs->hw.ser = NULL;
430                 return 0;
431         }
432         dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
433
434         tasklet_init(&cs->write_tasklet,
435                      gigaset_modem_fill, (unsigned long) cs);
436         return 1;
437 }
438
439 /*
440  * set modem control lines
441  * Parameters:
442  *      card state structure
443  *      modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
444  * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
445  * and by "if_lock" and "if_termios" in interface.c
446  */
447 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
448                                   unsigned new_state)
449 {
450         struct tty_struct *tty = cs->hw.ser->tty;
451         unsigned int set, clear;
452
453         if (!tty || !tty->driver || !tty->ops->tiocmset)
454                 return -EINVAL;
455         set = new_state & ~old_state;
456         clear = old_state & ~new_state;
457         if (!set && !clear)
458                 return 0;
459         gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
460         return tty->ops->tiocmset(tty, NULL, set, clear);
461 }
462
463 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
464 {
465         return -EINVAL;
466 }
467
468 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
469 {
470         return -EINVAL;
471 }
472
473 static const struct gigaset_ops ops = {
474         gigaset_write_cmd,
475         gigaset_write_room,
476         gigaset_chars_in_buffer,
477         gigaset_brkchars,
478         gigaset_init_bchannel,
479         gigaset_close_bchannel,
480         gigaset_initbcshw,
481         gigaset_freebcshw,
482         gigaset_reinitbcshw,
483         gigaset_initcshw,
484         gigaset_freecshw,
485         gigaset_set_modem_ctrl,
486         gigaset_baud_rate,
487         gigaset_set_line_ctrl,
488         gigaset_m10x_send_skb,  /* asyncdata.c */
489         gigaset_m10x_input,     /* asyncdata.c */
490 };
491
492
493 /* Line Discipline Interface */
494 /* ========================= */
495
496 /* helper functions for cardstate refcounting */
497 static struct cardstate *cs_get(struct tty_struct *tty)
498 {
499         struct cardstate *cs = tty->disc_data;
500
501         if (!cs || !cs->hw.ser) {
502                 gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
503                 return NULL;
504         }
505         atomic_inc(&cs->hw.ser->refcnt);
506         return cs;
507 }
508
509 static void cs_put(struct cardstate *cs)
510 {
511         if (atomic_dec_and_test(&cs->hw.ser->refcnt))
512                 complete(&cs->hw.ser->dead_cmp);
513 }
514
515 /*
516  * Called by the tty driver when the line discipline is pushed onto the tty.
517  * Called in process context.
518  */
519 static int
520 gigaset_tty_open(struct tty_struct *tty)
521 {
522         struct cardstate *cs;
523
524         gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
525
526         pr_info(DRIVER_DESC "\n");
527
528         if (!driver) {
529                 pr_err("%s: no driver structure\n", __func__);
530                 return -ENODEV;
531         }
532
533         /* allocate memory for our device state and intialize it */
534         cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
535         if (!cs)
536                 goto error;
537
538         cs->dev = &cs->hw.ser->dev.dev;
539         cs->hw.ser->tty = tty;
540         atomic_set(&cs->hw.ser->refcnt, 1);
541         init_completion(&cs->hw.ser->dead_cmp);
542
543         tty->disc_data = cs;
544
545         /* OK.. Initialization of the datastructures and the HW is done.. Now
546          * startup system and notify the LL that we are ready to run
547          */
548         if (startmode == SM_LOCKED)
549                 cs->mstate = MS_LOCKED;
550         if (!gigaset_start(cs)) {
551                 tasklet_kill(&cs->write_tasklet);
552                 goto error;
553         }
554
555         gig_dbg(DEBUG_INIT, "Startup of HLL done");
556         return 0;
557
558 error:
559         gig_dbg(DEBUG_INIT, "Startup of HLL failed");
560         tty->disc_data = NULL;
561         gigaset_freecs(cs);
562         return -ENODEV;
563 }
564
565 /*
566  * Called by the tty driver when the line discipline is removed.
567  * Called from process context.
568  */
569 static void
570 gigaset_tty_close(struct tty_struct *tty)
571 {
572         struct cardstate *cs = tty->disc_data;
573
574         gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
575
576         if (!cs) {
577                 gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
578                 return;
579         }
580
581         /* prevent other callers from entering ldisc methods */
582         tty->disc_data = NULL;
583
584         if (!cs->hw.ser)
585                 pr_err("%s: no hw cardstate\n", __func__);
586         else {
587                 /* wait for running methods to finish */
588                 if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
589                         wait_for_completion(&cs->hw.ser->dead_cmp);
590         }
591
592         /* stop operations */
593         gigaset_stop(cs);
594         tasklet_kill(&cs->write_tasklet);
595         flush_send_queue(cs);
596         cs->dev = NULL;
597         gigaset_freecs(cs);
598
599         gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
600 }
601
602 /*
603  * Called by the tty driver when the tty line is hung up.
604  * Wait for I/O to driver to complete and unregister ISDN device.
605  * This is already done by the close routine, so just call that.
606  * Called from process context.
607  */
608 static int gigaset_tty_hangup(struct tty_struct *tty)
609 {
610         gigaset_tty_close(tty);
611         return 0;
612 }
613
614 /*
615  * Read on the tty.
616  * Unused, received data goes only to the Gigaset driver.
617  */
618 static ssize_t
619 gigaset_tty_read(struct tty_struct *tty, struct file *file,
620                  unsigned char __user *buf, size_t count)
621 {
622         return -EAGAIN;
623 }
624
625 /*
626  * Write on the tty.
627  * Unused, transmit data comes only from the Gigaset driver.
628  */
629 static ssize_t
630 gigaset_tty_write(struct tty_struct *tty, struct file *file,
631                   const unsigned char *buf, size_t count)
632 {
633         return -EAGAIN;
634 }
635
636 /*
637  * Ioctl on the tty.
638  * Called in process context only.
639  * May be re-entered by multiple ioctl calling threads.
640  */
641 static int
642 gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
643                   unsigned int cmd, unsigned long arg)
644 {
645         struct cardstate *cs = cs_get(tty);
646         int rc, val;
647         int __user *p = (int __user *)arg;
648
649         if (!cs)
650                 return -ENXIO;
651
652         switch (cmd) {
653
654         case FIONREAD:
655                 /* unused, always return zero */
656                 val = 0;
657                 rc = put_user(val, p);
658                 break;
659
660         case TCFLSH:
661                 /* flush our buffers and the serial port's buffer */
662                 switch (arg) {
663                 case TCIFLUSH:
664                         /* no own input buffer to flush */
665                         break;
666                 case TCIOFLUSH:
667                 case TCOFLUSH:
668                         flush_send_queue(cs);
669                         break;
670                 }
671                 /* Pass through */
672
673         default:
674                 /* pass through to underlying serial device */
675                 rc = n_tty_ioctl_helper(tty, file, cmd, arg);
676                 break;
677         }
678         cs_put(cs);
679         return rc;
680 }
681
682 /*
683  * Called by the tty driver when a block of data has been received.
684  * Will not be re-entered while running but other ldisc functions
685  * may be called in parallel.
686  * Can be called from hard interrupt level as well as soft interrupt
687  * level or mainline.
688  * Parameters:
689  *      tty     tty structure
690  *      buf     buffer containing received characters
691  *      cflags  buffer containing error flags for received characters (ignored)
692  *      count   number of received characters
693  */
694 static void
695 gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
696                     char *cflags, int count)
697 {
698         struct cardstate *cs = cs_get(tty);
699         unsigned tail, head, n;
700         struct inbuf_t *inbuf;
701
702         if (!cs)
703                 return;
704         inbuf = cs->inbuf;
705         if (!inbuf) {
706                 dev_err(cs->dev, "%s: no inbuf\n", __func__);
707                 cs_put(cs);
708                 return;
709         }
710
711         tail = inbuf->tail;
712         head = inbuf->head;
713         gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
714                 head, tail, count);
715
716         if (head <= tail) {
717                 /* possible buffer wraparound */
718                 n = min_t(unsigned, count, RBUFSIZE - tail);
719                 memcpy(inbuf->data + tail, buf, n);
720                 tail = (tail + n) % RBUFSIZE;
721                 buf += n;
722                 count -= n;
723         }
724
725         if (count > 0) {
726                 /* tail < head and some data left */
727                 n = head - tail - 1;
728                 if (count > n) {
729                         dev_err(cs->dev,
730                                 "inbuf overflow, discarding %d bytes\n",
731                                 count - n);
732                         count = n;
733                 }
734                 memcpy(inbuf->data + tail, buf, count);
735                 tail += count;
736         }
737
738         gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
739         inbuf->tail = tail;
740
741         /* Everything was received .. Push data into handler */
742         gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
743         gigaset_schedule_event(cs);
744         cs_put(cs);
745 }
746
747 /*
748  * Called by the tty driver when there's room for more data to send.
749  */
750 static void
751 gigaset_tty_wakeup(struct tty_struct *tty)
752 {
753         struct cardstate *cs = cs_get(tty);
754
755         clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
756         if (!cs)
757                 return;
758         tasklet_schedule(&cs->write_tasklet);
759         cs_put(cs);
760 }
761
762 static struct tty_ldisc_ops gigaset_ldisc = {
763         .owner          = THIS_MODULE,
764         .magic          = TTY_LDISC_MAGIC,
765         .name           = "ser_gigaset",
766         .open           = gigaset_tty_open,
767         .close          = gigaset_tty_close,
768         .hangup         = gigaset_tty_hangup,
769         .read           = gigaset_tty_read,
770         .write          = gigaset_tty_write,
771         .ioctl          = gigaset_tty_ioctl,
772         .receive_buf    = gigaset_tty_receive,
773         .write_wakeup   = gigaset_tty_wakeup,
774 };
775
776
777 /* Initialization / Shutdown */
778 /* ========================= */
779
780 static int __init ser_gigaset_init(void)
781 {
782         int rc;
783
784         gig_dbg(DEBUG_INIT, "%s", __func__);
785         rc = platform_driver_register(&device_driver);
786         if (rc != 0) {
787                 pr_err("error %d registering platform driver\n", rc);
788                 return rc;
789         }
790
791         /* allocate memory for our driver state and intialize it */
792         driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
793                                           GIGASET_MODULENAME, GIGASET_DEVNAME,
794                                           &ops, THIS_MODULE);
795         if (!driver)
796                 goto error;
797
798         rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc);
799         if (rc != 0) {
800                 pr_err("error %d registering line discipline\n", rc);
801                 goto error;
802         }
803
804         return 0;
805
806 error:
807         if (driver) {
808                 gigaset_freedriver(driver);
809                 driver = NULL;
810         }
811         platform_driver_unregister(&device_driver);
812         return rc;
813 }
814
815 static void __exit ser_gigaset_exit(void)
816 {
817         int rc;
818
819         gig_dbg(DEBUG_INIT, "%s", __func__);
820
821         if (driver) {
822                 gigaset_freedriver(driver);
823                 driver = NULL;
824         }
825
826         rc = tty_unregister_ldisc(N_GIGASET_M101);
827         if (rc != 0)
828                 pr_err("error %d unregistering line discipline\n", rc);
829
830         platform_driver_unregister(&device_driver);
831 }
832
833 module_init(ser_gigaset_init);
834 module_exit(ser_gigaset_exit);