1// SPDX-License-Identifier: GPL-2.0
2/*
3  USB Driver layer for GSM modems
4
5  Copyright (C) 2005  Matthias Urlichs <smurf@smurf.noris.de>
6
7  Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
8
9  History: see the git log.
10
11  Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
12
13  This driver exists because the "normal" serial driver doesn't work too well
14  with GSM modems. Issues:
15  - data loss -- one single Receive URB is not nearly enough
16  - controlling the baud rate doesn't make sense
17*/
18
19#define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
20#define DRIVER_DESC "USB Driver for GSM modems"
21
22#include <linux/kernel.h>
23#include <linux/jiffies.h>
24#include <linux/errno.h>
25#include <linux/slab.h>
26#include <linux/tty.h>
27#include <linux/tty_flip.h>
28#include <linux/module.h>
29#include <linux/bitops.h>
30#include <linux/uaccess.h>
31#include <linux/usb.h>
32#include <linux/usb/serial.h>
33#include <linux/serial.h>
34#include "usb-wwan.h"
35
36/*
37 * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
38 * in CDC ACM.
39 */
40static int usb_wwan_send_setup(struct usb_serial_port *port)
41{
42	struct usb_serial *serial = port->serial;
43	struct usb_wwan_port_private *portdata;
44	int val = 0;
45	int ifnum;
46	int res;
47
48	portdata = usb_get_serial_port_data(port);
49
50	if (portdata->dtr_state)
51		val |= 0x01;
52	if (portdata->rts_state)
53		val |= 0x02;
54
55	ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
56
57	res = usb_autopm_get_interface(serial->interface);
58	if (res)
59		return res;
60
61	res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
62				0x22, 0x21, val, ifnum, NULL, 0,
63				USB_CTRL_SET_TIMEOUT);
64
65	usb_autopm_put_interface(port->serial->interface);
66
67	return res;
68}
69
70void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
71{
72	struct usb_wwan_port_private *portdata;
73	struct usb_wwan_intf_private *intfdata;
74
75	intfdata = usb_get_serial_data(port->serial);
76
77	if (!intfdata->use_send_setup)
78		return;
79
80	portdata = usb_get_serial_port_data(port);
81	/* FIXME: locking */
82	portdata->rts_state = on;
83	portdata->dtr_state = on;
84
85	usb_wwan_send_setup(port);
86}
87EXPORT_SYMBOL(usb_wwan_dtr_rts);
88
89int usb_wwan_tiocmget(struct tty_struct *tty)
90{
91	struct usb_serial_port *port = tty->driver_data;
92	unsigned int value;
93	struct usb_wwan_port_private *portdata;
94
95	portdata = usb_get_serial_port_data(port);
96
97	value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
98	    ((portdata->dtr_state) ? TIOCM_DTR : 0) |
99	    ((portdata->cts_state) ? TIOCM_CTS : 0) |
100	    ((portdata->dsr_state) ? TIOCM_DSR : 0) |
101	    ((portdata->dcd_state) ? TIOCM_CAR : 0) |
102	    ((portdata->ri_state) ? TIOCM_RNG : 0);
103
104	return value;
105}
106EXPORT_SYMBOL(usb_wwan_tiocmget);
107
108int usb_wwan_tiocmset(struct tty_struct *tty,
109		      unsigned int set, unsigned int clear)
110{
111	struct usb_serial_port *port = tty->driver_data;
112	struct usb_wwan_port_private *portdata;
113	struct usb_wwan_intf_private *intfdata;
114
115	portdata = usb_get_serial_port_data(port);
116	intfdata = usb_get_serial_data(port->serial);
117
118	if (!intfdata->use_send_setup)
119		return -EINVAL;
120
121	/* FIXME: what locks portdata fields ? */
122	if (set & TIOCM_RTS)
123		portdata->rts_state = 1;
124	if (set & TIOCM_DTR)
125		portdata->dtr_state = 1;
126
127	if (clear & TIOCM_RTS)
128		portdata->rts_state = 0;
129	if (clear & TIOCM_DTR)
130		portdata->dtr_state = 0;
131	return usb_wwan_send_setup(port);
132}
133EXPORT_SYMBOL(usb_wwan_tiocmset);
134
135int usb_wwan_get_serial_info(struct tty_struct *tty,
136			   struct serial_struct *ss)
137{
138	struct usb_serial_port *port = tty->driver_data;
139
140	ss->line            = port->minor;
141	ss->port            = port->port_number;
142	ss->baud_base       = tty_get_baud_rate(port->port.tty);
143	ss->close_delay	    = jiffies_to_msecs(port->port.close_delay) / 10;
144	ss->closing_wait    = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
145				 ASYNC_CLOSING_WAIT_NONE :
146				 jiffies_to_msecs(port->port.closing_wait) / 10;
147	return 0;
148}
149EXPORT_SYMBOL(usb_wwan_get_serial_info);
150
151int usb_wwan_set_serial_info(struct tty_struct *tty,
152			   struct serial_struct *ss)
153{
154	struct usb_serial_port *port = tty->driver_data;
155	unsigned int closing_wait, close_delay;
156	int retval = 0;
157
158	close_delay = msecs_to_jiffies(ss->close_delay * 10);
159	closing_wait = ss->closing_wait == ASYNC_CLOSING_WAIT_NONE ?
160			ASYNC_CLOSING_WAIT_NONE :
161			msecs_to_jiffies(ss->closing_wait * 10);
162
163	mutex_lock(&port->port.mutex);
164
165	if (!capable(CAP_SYS_ADMIN)) {
166		if ((close_delay != port->port.close_delay) ||
167		    (closing_wait != port->port.closing_wait))
168			retval = -EPERM;
169		else
170			retval = -EOPNOTSUPP;
171	} else {
172		port->port.close_delay  = close_delay;
173		port->port.closing_wait = closing_wait;
174	}
175
176	mutex_unlock(&port->port.mutex);
177	return retval;
178}
179EXPORT_SYMBOL(usb_wwan_set_serial_info);
180
181int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
182		   const unsigned char *buf, int count)
183{
184	struct usb_wwan_port_private *portdata;
185	struct usb_wwan_intf_private *intfdata;
186	int i;
187	int left, todo;
188	struct urb *this_urb = NULL;	/* spurious */
189	int err;
190	unsigned long flags;
191
192	portdata = usb_get_serial_port_data(port);
193	intfdata = usb_get_serial_data(port->serial);
194
195	dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
196
197	i = 0;
198	left = count;
199	for (i = 0; left > 0 && i < N_OUT_URB; i++) {
200		todo = left;
201		if (todo > OUT_BUFLEN)
202			todo = OUT_BUFLEN;
203
204		this_urb = portdata->out_urbs[i];
205		if (test_and_set_bit(i, &portdata->out_busy)) {
206			if (time_before(jiffies,
207					portdata->tx_start_time[i] + 10 * HZ))
208				continue;
209			usb_unlink_urb(this_urb);
210			continue;
211		}
212		dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
213			usb_pipeendpoint(this_urb->pipe), i);
214
215		err = usb_autopm_get_interface_async(port->serial->interface);
216		if (err < 0) {
217			clear_bit(i, &portdata->out_busy);
218			break;
219		}
220
221		/* send the data */
222		memcpy(this_urb->transfer_buffer, buf, todo);
223		this_urb->transfer_buffer_length = todo;
224
225		spin_lock_irqsave(&intfdata->susp_lock, flags);
226		if (intfdata->suspended) {
227			usb_anchor_urb(this_urb, &portdata->delayed);
228			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
229		} else {
230			intfdata->in_flight++;
231			spin_unlock_irqrestore(&intfdata->susp_lock, flags);
232			err = usb_submit_urb(this_urb, GFP_ATOMIC);
233			if (err) {
234				dev_err(&port->dev,
235					"%s: submit urb %d failed: %d\n",
236					__func__, i, err);
237				clear_bit(i, &portdata->out_busy);
238				spin_lock_irqsave(&intfdata->susp_lock, flags);
239				intfdata->in_flight--;
240				spin_unlock_irqrestore(&intfdata->susp_lock,
241						       flags);
242				usb_autopm_put_interface_async(port->serial->interface);
243				break;
244			}
245		}
246
247		portdata->tx_start_time[i] = jiffies;
248		buf += todo;
249		left -= todo;
250	}
251
252	count -= left;
253	dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
254	return count;
255}
256EXPORT_SYMBOL(usb_wwan_write);
257
258static void usb_wwan_indat_callback(struct urb *urb)
259{
260	int err;
261	int endpoint;
262	struct usb_serial_port *port;
263	struct device *dev;
264	unsigned char *data = urb->transfer_buffer;
265	int status = urb->status;
266
267	endpoint = usb_pipeendpoint(urb->pipe);
268	port = urb->context;
269	dev = &port->dev;
270
271	if (status) {
272		dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
273			__func__, status, endpoint);
274
275		/* don't resubmit on fatal errors */
276		if (status == -ESHUTDOWN || status == -ENOENT)
277			return;
278	} else {
279		if (urb->actual_length) {
280			tty_insert_flip_string(&port->port, data,
281					urb->actual_length);
282			tty_flip_buffer_push(&port->port);
283		} else
284			dev_dbg(dev, "%s: empty read urb received\n", __func__);
285	}
286	/* Resubmit urb so we continue receiving */
287	err = usb_submit_urb(urb, GFP_ATOMIC);
288	if (err) {
289		if (err != -EPERM && err != -ENODEV) {
290			dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
291				__func__, err);
292			/* busy also in error unless we are killed */
293			usb_mark_last_busy(port->serial->dev);
294		}
295	} else {
296		usb_mark_last_busy(port->serial->dev);
297	}
298}
299
300static void usb_wwan_outdat_callback(struct urb *urb)
301{
302	struct usb_serial_port *port;
303	struct usb_wwan_port_private *portdata;
304	struct usb_wwan_intf_private *intfdata;
305	unsigned long flags;
306	int i;
307
308	port = urb->context;
309	intfdata = usb_get_serial_data(port->serial);
310
311	usb_serial_port_softint(port);
312	usb_autopm_put_interface_async(port->serial->interface);
313	portdata = usb_get_serial_port_data(port);
314	spin_lock_irqsave(&intfdata->susp_lock, flags);
315	intfdata->in_flight--;
316	spin_unlock_irqrestore(&intfdata->susp_lock, flags);
317
318	for (i = 0; i < N_OUT_URB; ++i) {
319		if (portdata->out_urbs[i] == urb) {
320			smp_mb__before_atomic();
321			clear_bit(i, &portdata->out_busy);
322			break;
323		}
324	}
325}
326
327int usb_wwan_write_room(struct tty_struct *tty)
328{
329	struct usb_serial_port *port = tty->driver_data;
330	struct usb_wwan_port_private *portdata;
331	int i;
332	int data_len = 0;
333	struct urb *this_urb;
334
335	portdata = usb_get_serial_port_data(port);
336
337	for (i = 0; i < N_OUT_URB; i++) {
338		this_urb = portdata->out_urbs[i];
339		if (this_urb && !test_bit(i, &portdata->out_busy))
340			data_len += OUT_BUFLEN;
341	}
342
343	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
344	return data_len;
345}
346EXPORT_SYMBOL(usb_wwan_write_room);
347
348int usb_wwan_chars_in_buffer(struct tty_struct *tty)
349{
350	struct usb_serial_port *port = tty->driver_data;
351	struct usb_wwan_port_private *portdata;
352	int i;
353	int data_len = 0;
354	struct urb *this_urb;
355
356	portdata = usb_get_serial_port_data(port);
357
358	for (i = 0; i < N_OUT_URB; i++) {
359		this_urb = portdata->out_urbs[i];
360		/* FIXME: This locking is insufficient as this_urb may
361		   go unused during the test */
362		if (this_urb && test_bit(i, &portdata->out_busy))
363			data_len += this_urb->transfer_buffer_length;
364	}
365	dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
366	return data_len;
367}
368EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
369
370int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
371{
372	struct usb_wwan_port_private *portdata;
373	struct usb_wwan_intf_private *intfdata;
374	struct usb_serial *serial = port->serial;
375	int i, err;
376	struct urb *urb;
377
378	portdata = usb_get_serial_port_data(port);
379	intfdata = usb_get_serial_data(serial);
380
381	if (port->interrupt_in_urb) {
382		err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
383		if (err) {
384			dev_err(&port->dev, "%s: submit int urb failed: %d\n",
385				__func__, err);
386		}
387	}
388
389	/* Start reading from the IN endpoint */
390	for (i = 0; i < N_IN_URB; i++) {
391		urb = portdata->in_urbs[i];
392		if (!urb)
393			continue;
394		err = usb_submit_urb(urb, GFP_KERNEL);
395		if (err) {
396			dev_err(&port->dev,
397				"%s: submit read urb %d failed: %d\n",
398				__func__, i, err);
399		}
400	}
401
402	spin_lock_irq(&intfdata->susp_lock);
403	if (++intfdata->open_ports == 1)
404		serial->interface->needs_remote_wakeup = 1;
405	spin_unlock_irq(&intfdata->susp_lock);
406	/* this balances a get in the generic USB serial code */
407	usb_autopm_put_interface(serial->interface);
408
409	return 0;
410}
411EXPORT_SYMBOL(usb_wwan_open);
412
413static void unbusy_queued_urb(struct urb *urb,
414					struct usb_wwan_port_private *portdata)
415{
416	int i;
417
418	for (i = 0; i < N_OUT_URB; i++) {
419		if (urb == portdata->out_urbs[i]) {
420			clear_bit(i, &portdata->out_busy);
421			break;
422		}
423	}
424}
425
426void usb_wwan_close(struct usb_serial_port *port)
427{
428	int i;
429	struct usb_serial *serial = port->serial;
430	struct usb_wwan_port_private *portdata;
431	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
432	struct urb *urb;
433
434	portdata = usb_get_serial_port_data(port);
435
436	/*
437	 * Need to take susp_lock to make sure port is not already being
438	 * resumed, but no need to hold it due to the tty-port initialized
439	 * flag.
440	 */
441	spin_lock_irq(&intfdata->susp_lock);
442	if (--intfdata->open_ports == 0)
443		serial->interface->needs_remote_wakeup = 0;
444	spin_unlock_irq(&intfdata->susp_lock);
445
446	for (;;) {
447		urb = usb_get_from_anchor(&portdata->delayed);
448		if (!urb)
449			break;
450		unbusy_queued_urb(urb, portdata);
451		usb_autopm_put_interface_async(serial->interface);
452	}
453
454	for (i = 0; i < N_IN_URB; i++)
455		usb_kill_urb(portdata->in_urbs[i]);
456	for (i = 0; i < N_OUT_URB; i++)
457		usb_kill_urb(portdata->out_urbs[i]);
458	usb_kill_urb(port->interrupt_in_urb);
459
460	usb_autopm_get_interface_no_resume(serial->interface);
461}
462EXPORT_SYMBOL(usb_wwan_close);
463
464static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
465				      int endpoint,
466				      int dir, void *ctx, char *buf, int len,
467				      void (*callback) (struct urb *))
468{
469	struct usb_serial *serial = port->serial;
470	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
471	struct urb *urb;
472
473	urb = usb_alloc_urb(0, GFP_KERNEL);	/* No ISO */
474	if (!urb)
475		return NULL;
476
477	usb_fill_bulk_urb(urb, serial->dev,
478			  usb_sndbulkpipe(serial->dev, endpoint) | dir,
479			  buf, len, callback, ctx);
480
481	if (intfdata->use_zlp && dir == USB_DIR_OUT)
482		urb->transfer_flags |= URB_ZERO_PACKET;
483
484	return urb;
485}
486
487int usb_wwan_port_probe(struct usb_serial_port *port)
488{
489	struct usb_wwan_port_private *portdata;
490	struct urb *urb;
491	u8 *buffer;
492	int i;
493
494	if (!port->bulk_in_size || !port->bulk_out_size)
495		return -ENODEV;
496
497	portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
498	if (!portdata)
499		return -ENOMEM;
500
501	init_usb_anchor(&portdata->delayed);
502
503	for (i = 0; i < N_IN_URB; i++) {
504		buffer = (u8 *)__get_free_page(GFP_KERNEL);
505		if (!buffer)
506			goto bail_out_error;
507		portdata->in_buffer[i] = buffer;
508
509		urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
510						USB_DIR_IN, port,
511						buffer, IN_BUFLEN,
512						usb_wwan_indat_callback);
513		portdata->in_urbs[i] = urb;
514	}
515
516	for (i = 0; i < N_OUT_URB; i++) {
517		buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
518		if (!buffer)
519			goto bail_out_error2;
520		portdata->out_buffer[i] = buffer;
521
522		urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
523						USB_DIR_OUT, port,
524						buffer, OUT_BUFLEN,
525						usb_wwan_outdat_callback);
526		portdata->out_urbs[i] = urb;
527	}
528
529	usb_set_serial_port_data(port, portdata);
530
531	return 0;
532
533bail_out_error2:
534	for (i = 0; i < N_OUT_URB; i++) {
535		usb_free_urb(portdata->out_urbs[i]);
536		kfree(portdata->out_buffer[i]);
537	}
538bail_out_error:
539	for (i = 0; i < N_IN_URB; i++) {
540		usb_free_urb(portdata->in_urbs[i]);
541		free_page((unsigned long)portdata->in_buffer[i]);
542	}
543	kfree(portdata);
544
545	return -ENOMEM;
546}
547EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
548
549int usb_wwan_port_remove(struct usb_serial_port *port)
550{
551	int i;
552	struct usb_wwan_port_private *portdata;
553
554	portdata = usb_get_serial_port_data(port);
555	usb_set_serial_port_data(port, NULL);
556
557	for (i = 0; i < N_IN_URB; i++) {
558		usb_free_urb(portdata->in_urbs[i]);
559		free_page((unsigned long)portdata->in_buffer[i]);
560	}
561	for (i = 0; i < N_OUT_URB; i++) {
562		usb_free_urb(portdata->out_urbs[i]);
563		kfree(portdata->out_buffer[i]);
564	}
565
566	kfree(portdata);
567
568	return 0;
569}
570EXPORT_SYMBOL(usb_wwan_port_remove);
571
572#ifdef CONFIG_PM
573static void stop_urbs(struct usb_serial *serial)
574{
575	int i, j;
576	struct usb_serial_port *port;
577	struct usb_wwan_port_private *portdata;
578
579	for (i = 0; i < serial->num_ports; ++i) {
580		port = serial->port[i];
581		portdata = usb_get_serial_port_data(port);
582		if (!portdata)
583			continue;
584		for (j = 0; j < N_IN_URB; j++)
585			usb_kill_urb(portdata->in_urbs[j]);
586		for (j = 0; j < N_OUT_URB; j++)
587			usb_kill_urb(portdata->out_urbs[j]);
588		usb_kill_urb(port->interrupt_in_urb);
589	}
590}
591
592int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
593{
594	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
595
596	spin_lock_irq(&intfdata->susp_lock);
597	if (PMSG_IS_AUTO(message)) {
598		if (intfdata->in_flight) {
599			spin_unlock_irq(&intfdata->susp_lock);
600			return -EBUSY;
601		}
602	}
603	intfdata->suspended = 1;
604	spin_unlock_irq(&intfdata->susp_lock);
605
606	stop_urbs(serial);
607
608	return 0;
609}
610EXPORT_SYMBOL(usb_wwan_suspend);
611
612/* Caller must hold susp_lock. */
613static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
614{
615	struct usb_serial *serial = port->serial;
616	struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
617	struct usb_wwan_port_private *portdata;
618	struct urb *urb;
619	int err_count = 0;
620	int err;
621
622	portdata = usb_get_serial_port_data(port);
623
624	for (;;) {
625		urb = usb_get_from_anchor(&portdata->delayed);
626		if (!urb)
627			break;
628
629		err = usb_submit_urb(urb, GFP_ATOMIC);
630		if (err) {
631			dev_err(&port->dev, "%s: submit urb failed: %d\n",
632					__func__, err);
633			err_count++;
634			unbusy_queued_urb(urb, portdata);
635			usb_autopm_put_interface_async(serial->interface);
636			continue;
637		}
638		data->in_flight++;
639	}
640
641	if (err_count)
642		return -EIO;
643
644	return 0;
645}
646
647int usb_wwan_resume(struct usb_serial *serial)
648{
649	int i, j;
650	struct usb_serial_port *port;
651	struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
652	struct usb_wwan_port_private *portdata;
653	struct urb *urb;
654	int err;
655	int err_count = 0;
656
657	spin_lock_irq(&intfdata->susp_lock);
658	for (i = 0; i < serial->num_ports; i++) {
659		port = serial->port[i];
660
661		if (!tty_port_initialized(&port->port))
662			continue;
663
664		portdata = usb_get_serial_port_data(port);
665
666		if (port->interrupt_in_urb) {
667			err = usb_submit_urb(port->interrupt_in_urb,
668					GFP_ATOMIC);
669			if (err) {
670				dev_err(&port->dev,
671					"%s: submit int urb failed: %d\n",
672					__func__, err);
673				err_count++;
674			}
675		}
676
677		err = usb_wwan_submit_delayed_urbs(port);
678		if (err)
679			err_count++;
680
681		for (j = 0; j < N_IN_URB; j++) {
682			urb = portdata->in_urbs[j];
683			err = usb_submit_urb(urb, GFP_ATOMIC);
684			if (err < 0) {
685				dev_err(&port->dev,
686					"%s: submit read urb %d failed: %d\n",
687					__func__, i, err);
688				err_count++;
689			}
690		}
691	}
692	intfdata->suspended = 0;
693	spin_unlock_irq(&intfdata->susp_lock);
694
695	if (err_count)
696		return -EIO;
697
698	return 0;
699}
700EXPORT_SYMBOL(usb_wwan_resume);
701#endif
702
703MODULE_AUTHOR(DRIVER_AUTHOR);
704MODULE_DESCRIPTION(DRIVER_DESC);
705MODULE_LICENSE("GPL v2");
706