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