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
15#include <linux/module.h>
16#include <linux/moduleparam.h>
17#include <linux/platform_device.h>
18#include <linux/tty.h>
19#include <linux/poll.h>
20
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;
51	struct mutex		dead_mutex;
52};
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;
70	int sent;
71
72	if (!tty || !tty->driver || !skb)
73		return -EFAULT;
74
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);
82	sent = tty->driver->write(tty, skb->data, skb->len);
83	gig_dbg(DEBUG_OUTPUT, "write_modem: sent %d", sent);
84	if (sent < 0) {
85		/* error */
86		flush_send_queue(cs);
87		return sent;
88	}
89	skb_pull(skb, sent);
90	if (!skb->len) {
91		/* skb sent completely */
92		gigaset_skb_sent(bcs, skb);
93
94		gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
95			(unsigned long) skb);
96		dev_kfree_skb_any(skb);
97		bcs->tx_skb = NULL;
98	}
99	return sent;
100}
101
102/*
103 * transmit first queued command buffer
104 * result: number of bytes sent or error code < 0
105 */
106static int send_cb(struct cardstate *cs)
107{
108	struct tty_struct *tty = cs->hw.ser->tty;
109	struct cmdbuf_t *cb, *tcb;
110	unsigned long flags;
111	int sent = 0;
112
113	if (!tty || !tty->driver)
114		return -EFAULT;
115
116	cb = cs->cmdbuf;
117	if (!cb)
118		return 0;	/* nothing to do */
119
120	if (cb->len) {
121		set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
122		sent = tty->driver->write(tty, cb->buf + cb->offset, cb->len);
123		if (sent < 0) {
124			/* error */
125			gig_dbg(DEBUG_OUTPUT, "send_cb: write error %d", sent);
126			flush_send_queue(cs);
127			return sent;
128		}
129		cb->offset += sent;
130		cb->len -= sent;
131		gig_dbg(DEBUG_OUTPUT, "send_cb: sent %d, left %u, queued %u",
132			sent, cb->len, cs->cmdbytes);
133	}
134
135	while (cb && !cb->len) {
136		spin_lock_irqsave(&cs->cmdlock, flags);
137		cs->cmdbytes -= cs->curlen;
138		tcb = cb;
139		cs->cmdbuf = cb = cb->next;
140		if (cb) {
141			cb->prev = NULL;
142			cs->curlen = cb->len;
143		} else {
144			cs->lastcmdbuf = NULL;
145			cs->curlen = 0;
146		}
147		spin_unlock_irqrestore(&cs->cmdlock, flags);
148
149		if (tcb->wake_tasklet)
150			tasklet_schedule(tcb->wake_tasklet);
151		kfree(tcb);
152	}
153	return sent;
154}
155
156/*
157 * send queue tasklet
158 * If there is already a skb opened, put data to the transfer buffer
159 * by calling "write_modem".
160 * Otherwise take a new skb out of the queue.
161 */
162static void gigaset_modem_fill(unsigned long data)
163{
164	struct cardstate *cs = (struct cardstate *) data;
165	struct bc_state *bcs;
166	int sent = 0;
167
168	if (!cs || !(bcs = cs->bcs)) {
169		gig_dbg(DEBUG_OUTPUT, "%s: no cardstate", __func__);
170		return;
171	}
172	if (!bcs->tx_skb) {
173		/* no skb is being sent; send command if any */
174		sent = send_cb(cs);
175		gig_dbg(DEBUG_OUTPUT, "%s: send_cb -> %d", __func__, sent);
176		if (sent)
177			/* something sent or error */
178			return;
179
180		/* no command to send; get skb */
181		if (!(bcs->tx_skb = skb_dequeue(&bcs->squeue)))
182			/* no skb either, nothing to do */
183			return;
184
185		gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)",
186			(unsigned long) bcs->tx_skb);
187	}
188
189	/* send skb */
190	gig_dbg(DEBUG_OUTPUT, "%s: tx_skb", __func__);
191	if (write_modem(cs) < 0)
192		gig_dbg(DEBUG_OUTPUT, "%s: write_modem failed", __func__);
193}
194
195/*
196 * throw away all data queued for sending
197 */
198static void flush_send_queue(struct cardstate *cs)
199{
200	struct sk_buff *skb;
201	struct cmdbuf_t *cb;
202	unsigned long flags;
203
204	/* command queue */
205	spin_lock_irqsave(&cs->cmdlock, flags);
206	while ((cb = cs->cmdbuf) != NULL) {
207		cs->cmdbuf = cb->next;
208		if (cb->wake_tasklet)
209			tasklet_schedule(cb->wake_tasklet);
210		kfree(cb);
211	}
212	cs->cmdbuf = cs->lastcmdbuf = NULL;
213	cs->cmdbytes = cs->curlen = 0;
214	spin_unlock_irqrestore(&cs->cmdlock, flags);
215
216	/* data queue */
217	if (cs->bcs->tx_skb)
218		dev_kfree_skb_any(cs->bcs->tx_skb);
219	while ((skb = skb_dequeue(&cs->bcs->squeue)) != NULL)
220		dev_kfree_skb_any(skb);
221}
222
223
224/* Gigaset Driver Interface */
225/* ======================== */
226
227/*
228 * queue an AT command string for transmission to the Gigaset device
229 * parameters:
230 *	cs		controller state structure
231 *	buf		buffer containing the string to send
232 *	len		number of characters to send
233 *	wake_tasklet	tasklet to run when transmission is complete, or NULL
234 * return value:
235 *	number of bytes queued, or error code < 0
236 */
237static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
238                             int len, struct tasklet_struct *wake_tasklet)
239{
240	struct cmdbuf_t *cb;
241	unsigned long flags;
242
243	gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
244	                     DEBUG_TRANSCMD : DEBUG_LOCKCMD,
245	                   "CMD Transmit", len, buf);
246
247	if (len <= 0)
248		return 0;
249
250	if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
251		dev_err(cs->dev, "%s: out of memory!\n", __func__);
252		return -ENOMEM;
253	}
254
255	memcpy(cb->buf, buf, len);
256	cb->len = len;
257	cb->offset = 0;
258	cb->next = NULL;
259	cb->wake_tasklet = wake_tasklet;
260
261	spin_lock_irqsave(&cs->cmdlock, flags);
262	cb->prev = cs->lastcmdbuf;
263	if (cs->lastcmdbuf)
264		cs->lastcmdbuf->next = cb;
265	else {
266		cs->cmdbuf = cb;
267		cs->curlen = len;
268	}
269	cs->cmdbytes += len;
270	cs->lastcmdbuf = cb;
271	spin_unlock_irqrestore(&cs->cmdlock, flags);
272
273	spin_lock_irqsave(&cs->lock, flags);
274	if (cs->connected)
275		tasklet_schedule(&cs->write_tasklet);
276	spin_unlock_irqrestore(&cs->lock, flags);
277	return len;
278}
279
280/*
281 * tty_driver.write_room interface routine
282 * return number of characters the driver will accept to be written
283 * parameter:
284 *	controller state structure
285 * return value:
286 *	number of characters
287 */
288static int gigaset_write_room(struct cardstate *cs)
289{
290	unsigned bytes;
291
292	bytes = cs->cmdbytes;
293	return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
294}
295
296/*
297 * tty_driver.chars_in_buffer interface routine
298 * return number of characters waiting to be sent
299 * parameter:
300 *	controller state structure
301 * return value:
302 *	number of characters
303 */
304static int gigaset_chars_in_buffer(struct cardstate *cs)
305{
306	return cs->cmdbytes;
307}
308
309/*
310 * implementation of ioctl(GIGASET_BRKCHARS)
311 * parameter:
312 *	controller state structure
313 * return value:
314 *	-EINVAL (unimplemented function)
315 */
316static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
317{
318	/* not implemented */
319	return -EINVAL;
320}
321
322/*
323 * Open B channel
324 * Called by "do_action" in ev-layer.c
325 */
326static int gigaset_init_bchannel(struct bc_state *bcs)
327{
328	/* nothing to do for M10x */
329	gigaset_bchannel_up(bcs);
330	return 0;
331}
332
333/*
334 * Close B channel
335 * Called by "do_action" in ev-layer.c
336 */
337static int gigaset_close_bchannel(struct bc_state *bcs)
338{
339	/* nothing to do for M10x */
340	gigaset_bchannel_down(bcs);
341	return 0;
342}
343
344/*
345 * Set up B channel structure
346 * This is called by "gigaset_initcs" in common.c
347 */
348static int gigaset_initbcshw(struct bc_state *bcs)
349{
350	/* unused */
351	bcs->hw.ser = NULL;
352	return 1;
353}
354
355/*
356 * Free B channel structure
357 * Called by "gigaset_freebcs" in common.c
358 */
359static int gigaset_freebcshw(struct bc_state *bcs)
360{
361	/* unused */
362	return 1;
363}
364
365/*
366 * Reinitialize B channel structure
367 * This is called by "bcs_reinit" in common.c
368 */
369static void gigaset_reinitbcshw(struct bc_state *bcs)
370{
371	/* nothing to do for M10x */
372}
373
374/*
375 * Free hardware specific device data
376 * This will be called by "gigaset_freecs" in common.c
377 */
378static void gigaset_freecshw(struct cardstate *cs)
379{
380	tasklet_kill(&cs->write_tasklet);
381	if (!cs->hw.ser)
382		return;
383	dev_set_drvdata(&cs->hw.ser->dev.dev, NULL);
384	platform_device_unregister(&cs->hw.ser->dev);
385	kfree(cs->hw.ser);
386	cs->hw.ser = NULL;
387}
388
389static void gigaset_device_release(struct device *dev)
390{
391	struct platform_device *pdev =
392		container_of(dev, struct platform_device, dev);
393
394	/* adapted from platform_device_release() in drivers/base/platform.c */
395	kfree(dev->platform_data);
396	kfree(pdev->resource);
397}
398
399/*
400 * Set up hardware specific device data
401 * This is called by "gigaset_initcs" in common.c
402 */
403static int gigaset_initcshw(struct cardstate *cs)
404{
405	int rc;
406
407	if (!(cs->hw.ser = kzalloc(sizeof(struct ser_cardstate), GFP_KERNEL))) {
408		err("%s: out of memory!", __func__);
409		return 0;
410	}
411
412	cs->hw.ser->dev.name = GIGASET_MODULENAME;
413	cs->hw.ser->dev.id = cs->minor_index;
414	cs->hw.ser->dev.dev.release = gigaset_device_release;
415	if ((rc = platform_device_register(&cs->hw.ser->dev)) != 0) {
416		err("error %d registering platform device", rc);
417		kfree(cs->hw.ser);
418		cs->hw.ser = NULL;
419		return 0;
420	}
421	dev_set_drvdata(&cs->hw.ser->dev.dev, cs);
422
423	tasklet_init(&cs->write_tasklet,
424	             &gigaset_modem_fill, (unsigned long) cs);
425	return 1;
426}
427
428/*
429 * set modem control lines
430 * Parameters:
431 *	card state structure
432 *	modem control line state ([TIOCM_DTR]|[TIOCM_RTS])
433 * Called by "gigaset_start" and "gigaset_enterconfigmode" in common.c
434 * and by "if_lock" and "if_termios" in interface.c
435 */
436static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state, unsigned new_state)
437{
438	struct tty_struct *tty = cs->hw.ser->tty;
439	unsigned int set, clear;
440
441	if (!tty || !tty->driver || !tty->driver->tiocmset)
442		return -EFAULT;
443	set = new_state & ~old_state;
444	clear = old_state & ~new_state;
445	if (!set && !clear)
446		return 0;
447	gig_dbg(DEBUG_IF, "tiocmset set %x clear %x", set, clear);
448	return tty->driver->tiocmset(tty, NULL, set, clear);
449}
450
451static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
452{
453	return -EINVAL;
454}
455
456static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
457{
458	return -EINVAL;
459}
460
461static const struct gigaset_ops ops = {
462	gigaset_write_cmd,
463	gigaset_write_room,
464	gigaset_chars_in_buffer,
465	gigaset_brkchars,
466	gigaset_init_bchannel,
467	gigaset_close_bchannel,
468	gigaset_initbcshw,
469	gigaset_freebcshw,
470	gigaset_reinitbcshw,
471	gigaset_initcshw,
472	gigaset_freecshw,
473	gigaset_set_modem_ctrl,
474	gigaset_baud_rate,
475	gigaset_set_line_ctrl,
476	gigaset_m10x_send_skb,	/* asyncdata.c */
477	gigaset_m10x_input,	/* asyncdata.c */
478};
479
480
481/* Line Discipline Interface */
482/* ========================= */
483
484/* helper functions for cardstate refcounting */
485static struct cardstate *cs_get(struct tty_struct *tty)
486{
487	struct cardstate *cs = tty->disc_data;
488
489	if (!cs || !cs->hw.ser) {
490		gig_dbg(DEBUG_ANY, "%s: no cardstate", __func__);
491		return NULL;
492	}
493	atomic_inc(&cs->hw.ser->refcnt);
494	return cs;
495}
496
497static void cs_put(struct cardstate *cs)
498{
499	if (atomic_dec_and_test(&cs->hw.ser->refcnt))
500		mutex_unlock(&cs->hw.ser->dead_mutex);
501}
502
503/*
504 * Called by the tty driver when the line discipline is pushed onto the tty.
505 * Called in process context.
506 */
507static int
508gigaset_tty_open(struct tty_struct *tty)
509{
510	struct cardstate *cs;
511
512	gig_dbg(DEBUG_INIT, "Starting HLL for Gigaset M101");
513
514	info(DRIVER_AUTHOR);
515	info(DRIVER_DESC);
516
517	if (!driver) {
518		err("%s: no driver structure", __func__);
519		return -ENODEV;
520	}
521
522	/* allocate memory for our device state and intialize it */
523	if (!(cs = gigaset_initcs(driver, 1, 1, 0, cidmode,
524				  GIGASET_MODULENAME)))
525		goto error;
526
527	cs->dev = &cs->hw.ser->dev.dev;
528	cs->hw.ser->tty = tty;
529	mutex_init(&cs->hw.ser->dead_mutex);
530	atomic_set(&cs->hw.ser->refcnt, 1);
531
532	tty->disc_data = cs;
533
534	/* OK.. Initialization of the datastructures and the HW is done.. Now
535	 * startup system and notify the LL that we are ready to run
536	 */
537	if (startmode == SM_LOCKED)
538		atomic_set(&cs->mstate, MS_LOCKED);
539	if (!gigaset_start(cs)) {
540		tasklet_kill(&cs->write_tasklet);
541		goto error;
542	}
543
544	gig_dbg(DEBUG_INIT, "Startup of HLL done");
545	mutex_lock(&cs->hw.ser->dead_mutex);
546	return 0;
547
548error:
549	gig_dbg(DEBUG_INIT, "Startup of HLL failed");
550	tty->disc_data = NULL;
551	gigaset_freecs(cs);
552	return -ENODEV;
553}
554
555/*
556 * Called by the tty driver when the line discipline is removed.
557 * Called from process context.
558 */
559static void
560gigaset_tty_close(struct tty_struct *tty)
561{
562	struct cardstate *cs = tty->disc_data;
563
564	gig_dbg(DEBUG_INIT, "Stopping HLL for Gigaset M101");
565
566	if (!cs) {
567		gig_dbg(DEBUG_INIT, "%s: no cardstate", __func__);
568		return;
569	}
570
571	/* prevent other callers from entering ldisc methods */
572	tty->disc_data = NULL;
573
574	if (!cs->hw.ser)
575		err("%s: no hw cardstate", __func__);
576	else {
577		/* wait for running methods to finish */
578		if (!atomic_dec_and_test(&cs->hw.ser->refcnt))
579			mutex_lock(&cs->hw.ser->dead_mutex);
580	}
581
582	/* stop operations */
583	gigaset_stop(cs);
584	tasklet_kill(&cs->write_tasklet);
585	flush_send_queue(cs);
586	cs->dev = NULL;
587	gigaset_freecs(cs);
588
589	gig_dbg(DEBUG_INIT, "Shutdown of HLL done");
590}
591
592/*
593 * Called by the tty driver when the tty line is hung up.
594 * Wait for I/O to driver to complete and unregister ISDN device.
595 * This is already done by the close routine, so just call that.
596 * Called from process context.
597 */
598static int gigaset_tty_hangup(struct tty_struct *tty)
599{
600	gigaset_tty_close(tty);
601	return 0;
602}
603
604/*
605 * Read on the tty.
606 * Unused, received data goes only to the Gigaset driver.
607 */
608static ssize_t
609gigaset_tty_read(struct tty_struct *tty, struct file *file,
610		 unsigned char __user *buf, size_t count)
611{
612	return -EAGAIN;
613}
614
615/*
616 * Write on the tty.
617 * Unused, transmit data comes only from the Gigaset driver.
618 */
619static ssize_t
620gigaset_tty_write(struct tty_struct *tty, struct file *file,
621		  const unsigned char *buf, size_t count)
622{
623	return -EAGAIN;
624}
625
626/*
627 * Ioctl on the tty.
628 * Called in process context only.
629 * May be re-entered by multiple ioctl calling threads.
630 */
631static int
632gigaset_tty_ioctl(struct tty_struct *tty, struct file *file,
633		  unsigned int cmd, unsigned long arg)
634{
635	struct cardstate *cs = cs_get(tty);
636	int rc, val;
637	int __user *p = (int __user *)arg;
638
639	if (!cs)
640		return -ENXIO;
641
642	switch (cmd) {
643	case TCGETS:
644	case TCGETA:
645		/* pass through to underlying serial device */
646		rc = n_tty_ioctl(tty, file, cmd, arg);
647		break;
648
649	case TCFLSH:
650		/* flush our buffers and the serial port's buffer */
651		switch (arg) {
652		case TCIFLUSH:
653			/* no own input buffer to flush */
654			break;
655		case TCIOFLUSH:
656		case TCOFLUSH:
657			flush_send_queue(cs);
658			break;
659		}
660		/* flush the serial port's buffer */
661		rc = n_tty_ioctl(tty, file, cmd, arg);
662		break;
663
664	case FIONREAD:
665		/* unused, always return zero */
666		val = 0;
667		rc = put_user(val, p);
668		break;
669
670	default:
671		rc = -ENOIOCTLCMD;
672	}
673
674	cs_put(cs);
675	return rc;
676}
677
678/*
679 * Poll on the tty.
680 * Unused, always return zero.
681 */
682static unsigned int
683gigaset_tty_poll(struct tty_struct *tty, struct file *file, poll_table *wait)
684{
685	return 0;
686}
687
688/*
689 * Called by the tty driver when a block of data has been received.
690 * Will not be re-entered while running but other ldisc functions
691 * may be called in parallel.
692 * Can be called from hard interrupt level as well as soft interrupt
693 * level or mainline.
694 * Parameters:
695 *	tty	tty structure
696 *	buf	buffer containing received characters
697 *	cflags	buffer containing error flags for received characters (ignored)
698 *	count	number of received characters
699 */
700static void
701gigaset_tty_receive(struct tty_struct *tty, const unsigned char *buf,
702		    char *cflags, int count)
703{
704	struct cardstate *cs = cs_get(tty);
705	unsigned tail, head, n;
706	struct inbuf_t *inbuf;
707
708	if (!cs)
709		return;
710	if (!(inbuf = cs->inbuf)) {
711		dev_err(cs->dev, "%s: no inbuf\n", __func__);
712		cs_put(cs);
713		return;
714	}
715
716	tail = atomic_read(&inbuf->tail);
717	head = atomic_read(&inbuf->head);
718	gig_dbg(DEBUG_INTR, "buffer state: %u -> %u, receive %u bytes",
719		head, tail, count);
720
721	if (head <= tail) {
722		/* possible buffer wraparound */
723		n = min_t(unsigned, count, RBUFSIZE - tail);
724		memcpy(inbuf->data + tail, buf, n);
725		tail = (tail + n) % RBUFSIZE;
726		buf += n;
727		count -= n;
728	}
729
730	if (count > 0) {
731		/* tail < head and some data left */
732		n = head - tail - 1;
733		if (count > n) {
734			dev_err(cs->dev,
735				"inbuf overflow, discarding %d bytes\n",
736				count - n);
737			count = n;
738		}
739		memcpy(inbuf->data + tail, buf, count);
740		tail += count;
741	}
742
743	gig_dbg(DEBUG_INTR, "setting tail to %u", tail);
744	atomic_set(&inbuf->tail, tail);
745
746	/* Everything was received .. Push data into handler */
747	gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
748	gigaset_schedule_event(cs);
749	cs_put(cs);
750}
751
752/*
753 * Called by the tty driver when there's room for more data to send.
754 */
755static void
756gigaset_tty_wakeup(struct tty_struct *tty)
757{
758	struct cardstate *cs = cs_get(tty);
759
760	clear_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
761	if (!cs)
762		return;
763	tasklet_schedule(&cs->write_tasklet);
764	cs_put(cs);
765}
766
767static struct tty_ldisc gigaset_ldisc = {
768	.owner		= THIS_MODULE,
769	.magic		= TTY_LDISC_MAGIC,
770	.name		= "ser_gigaset",
771	.open		= gigaset_tty_open,
772	.close		= gigaset_tty_close,
773	.hangup		= gigaset_tty_hangup,
774	.read		= gigaset_tty_read,
775	.write		= gigaset_tty_write,
776	.ioctl		= gigaset_tty_ioctl,
777	.poll		= gigaset_tty_poll,
778	.receive_buf	= gigaset_tty_receive,
779	.write_wakeup	= gigaset_tty_wakeup,
780};
781
782
783/* Initialization / Shutdown */
784/* ========================= */
785
786static int __init ser_gigaset_init(void)
787{
788	int rc;
789
790	gig_dbg(DEBUG_INIT, "%s", __func__);
791	if ((rc = platform_driver_register(&device_driver)) != 0) {
792		err("error %d registering platform driver", rc);
793		return rc;
794	}
795
796	/* allocate memory for our driver state and intialize it */
797	if (!(driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
798					  GIGASET_MODULENAME, GIGASET_DEVNAME,
799					  &ops, THIS_MODULE)))
800		goto error;
801
802	if ((rc = tty_register_ldisc(N_GIGASET_M101, &gigaset_ldisc)) != 0) {
803		err("error %d registering line discipline", rc);
804		goto error;
805	}
806
807	return 0;
808
809error:
810	if (driver) {
811		gigaset_freedriver(driver);
812		driver = NULL;
813	}
814	platform_driver_unregister(&device_driver);
815	return rc;
816}
817
818static void __exit ser_gigaset_exit(void)
819{
820	int rc;
821
822	gig_dbg(DEBUG_INIT, "%s", __func__);
823
824	if (driver) {
825		gigaset_freedriver(driver);
826		driver = NULL;
827	}
828
829	if ((rc = tty_unregister_ldisc(N_GIGASET_M101)) != 0)
830		err("error %d unregistering line discipline", rc);
831
832	platform_driver_unregister(&device_driver);
833}
834
835module_init(ser_gigaset_init);
836module_exit(ser_gigaset_exit);
837