2 USB Driver layer for GSM modems
4 Copyright (C) 2005 Matthias Urlichs <smurf@smurf.noris.de>
6 This driver is free software; you can redistribute it and/or modify
7 it under the terms of Version 2 of the GNU General Public License as
8 published by the Free Software Foundation.
10 Portions copied from the Keyspan driver by Hugh Blemings <hugh@blemings.org>
12 History: see the git log.
14 Work sponsored by: Sigos GmbH, Germany <info@sigos.de>
16 This driver exists because the "normal" serial driver doesn't work too well
17 with GSM modems. Issues:
18 - data loss -- one single Receive URB is not nearly enough
19 - controlling the baud rate doesn't make sense
22 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
23 #define DRIVER_DESC "USB Driver for GSM modems"
25 #include <linux/kernel.h>
26 #include <linux/jiffies.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/tty.h>
30 #include <linux/tty_flip.h>
31 #include <linux/module.h>
32 #include <linux/bitops.h>
33 #include <linux/uaccess.h>
34 #include <linux/usb.h>
35 #include <linux/usb/serial.h>
36 #include <linux/serial.h>
40 * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
43 static int usb_wwan_send_setup(struct usb_serial_port *port)
45 struct usb_serial *serial = port->serial;
46 struct usb_wwan_port_private *portdata;
51 portdata = usb_get_serial_port_data(port);
53 if (portdata->dtr_state)
55 if (portdata->rts_state)
58 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
60 res = usb_autopm_get_interface(serial->interface);
64 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
65 0x22, 0x21, val, ifnum, NULL, 0,
66 USB_CTRL_SET_TIMEOUT);
68 usb_autopm_put_interface(port->serial->interface);
73 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
75 struct usb_wwan_port_private *portdata;
76 struct usb_wwan_intf_private *intfdata;
78 intfdata = usb_get_serial_data(port->serial);
80 if (!intfdata->use_send_setup)
83 portdata = usb_get_serial_port_data(port);
85 portdata->rts_state = on;
86 portdata->dtr_state = on;
88 usb_wwan_send_setup(port);
90 EXPORT_SYMBOL(usb_wwan_dtr_rts);
92 int usb_wwan_tiocmget(struct tty_struct *tty)
94 struct usb_serial_port *port = tty->driver_data;
96 struct usb_wwan_port_private *portdata;
98 portdata = usb_get_serial_port_data(port);
100 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
101 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
102 ((portdata->cts_state) ? TIOCM_CTS : 0) |
103 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
104 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
105 ((portdata->ri_state) ? TIOCM_RNG : 0);
109 EXPORT_SYMBOL(usb_wwan_tiocmget);
111 int usb_wwan_tiocmset(struct tty_struct *tty,
112 unsigned int set, unsigned int clear)
114 struct usb_serial_port *port = tty->driver_data;
115 struct usb_wwan_port_private *portdata;
116 struct usb_wwan_intf_private *intfdata;
118 portdata = usb_get_serial_port_data(port);
119 intfdata = usb_get_serial_data(port->serial);
121 if (!intfdata->use_send_setup)
124 /* FIXME: what locks portdata fields ? */
126 portdata->rts_state = 1;
128 portdata->dtr_state = 1;
130 if (clear & TIOCM_RTS)
131 portdata->rts_state = 0;
132 if (clear & TIOCM_DTR)
133 portdata->dtr_state = 0;
134 return usb_wwan_send_setup(port);
136 EXPORT_SYMBOL(usb_wwan_tiocmset);
138 static int get_serial_info(struct usb_serial_port *port,
139 struct serial_struct __user *retinfo)
141 struct serial_struct tmp;
143 memset(&tmp, 0, sizeof(tmp));
144 tmp.line = port->minor;
145 tmp.port = port->port_number;
146 tmp.baud_base = tty_get_baud_rate(port->port.tty);
147 tmp.close_delay = port->port.close_delay / 10;
148 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
149 ASYNC_CLOSING_WAIT_NONE :
150 port->port.closing_wait / 10;
152 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
157 static int set_serial_info(struct usb_serial_port *port,
158 struct serial_struct __user *newinfo)
160 struct serial_struct new_serial;
161 unsigned int closing_wait, close_delay;
164 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
167 close_delay = new_serial.close_delay * 10;
168 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
169 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
171 mutex_lock(&port->port.mutex);
173 if (!capable(CAP_SYS_ADMIN)) {
174 if ((close_delay != port->port.close_delay) ||
175 (closing_wait != port->port.closing_wait))
178 retval = -EOPNOTSUPP;
180 port->port.close_delay = close_delay;
181 port->port.closing_wait = closing_wait;
184 mutex_unlock(&port->port.mutex);
188 int usb_wwan_ioctl(struct tty_struct *tty,
189 unsigned int cmd, unsigned long arg)
191 struct usb_serial_port *port = tty->driver_data;
193 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
197 return get_serial_info(port,
198 (struct serial_struct __user *) arg);
200 return set_serial_info(port,
201 (struct serial_struct __user *) arg);
206 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
210 EXPORT_SYMBOL(usb_wwan_ioctl);
212 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
213 const unsigned char *buf, int count)
215 struct usb_wwan_port_private *portdata;
216 struct usb_wwan_intf_private *intfdata;
219 struct urb *this_urb = NULL; /* spurious */
223 portdata = usb_get_serial_port_data(port);
224 intfdata = usb_get_serial_data(port->serial);
226 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
230 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
232 if (todo > OUT_BUFLEN)
235 this_urb = portdata->out_urbs[i];
236 if (test_and_set_bit(i, &portdata->out_busy)) {
237 if (time_before(jiffies,
238 portdata->tx_start_time[i] + 10 * HZ))
240 usb_unlink_urb(this_urb);
243 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
244 usb_pipeendpoint(this_urb->pipe), i);
246 err = usb_autopm_get_interface_async(port->serial->interface);
248 clear_bit(i, &portdata->out_busy);
253 memcpy(this_urb->transfer_buffer, buf, todo);
254 this_urb->transfer_buffer_length = todo;
256 spin_lock_irqsave(&intfdata->susp_lock, flags);
257 if (intfdata->suspended) {
258 usb_anchor_urb(this_urb, &portdata->delayed);
259 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
261 intfdata->in_flight++;
262 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
263 err = usb_submit_urb(this_urb, GFP_ATOMIC);
266 "%s: submit urb %d failed: %d\n",
268 clear_bit(i, &portdata->out_busy);
269 spin_lock_irqsave(&intfdata->susp_lock, flags);
270 intfdata->in_flight--;
271 spin_unlock_irqrestore(&intfdata->susp_lock,
273 usb_autopm_put_interface_async(port->serial->interface);
278 portdata->tx_start_time[i] = jiffies;
284 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
287 EXPORT_SYMBOL(usb_wwan_write);
289 static void usb_wwan_indat_callback(struct urb *urb)
293 struct usb_serial_port *port;
295 unsigned char *data = urb->transfer_buffer;
296 int status = urb->status;
298 endpoint = usb_pipeendpoint(urb->pipe);
303 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
304 __func__, status, endpoint);
306 /* don't resubmit on fatal errors */
307 if (status == -ESHUTDOWN || status == -ENOENT)
310 if (urb->actual_length) {
311 tty_insert_flip_string(&port->port, data,
313 tty_flip_buffer_push(&port->port);
315 dev_dbg(dev, "%s: empty read urb received\n", __func__);
317 /* Resubmit urb so we continue receiving */
318 err = usb_submit_urb(urb, GFP_ATOMIC);
320 if (err != -EPERM && err != -ENODEV) {
321 dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
323 /* busy also in error unless we are killed */
324 usb_mark_last_busy(port->serial->dev);
327 usb_mark_last_busy(port->serial->dev);
331 static void usb_wwan_outdat_callback(struct urb *urb)
333 struct usb_serial_port *port;
334 struct usb_wwan_port_private *portdata;
335 struct usb_wwan_intf_private *intfdata;
339 intfdata = usb_get_serial_data(port->serial);
341 usb_serial_port_softint(port);
342 usb_autopm_put_interface_async(port->serial->interface);
343 portdata = usb_get_serial_port_data(port);
344 spin_lock(&intfdata->susp_lock);
345 intfdata->in_flight--;
346 spin_unlock(&intfdata->susp_lock);
348 for (i = 0; i < N_OUT_URB; ++i) {
349 if (portdata->out_urbs[i] == urb) {
350 smp_mb__before_atomic();
351 clear_bit(i, &portdata->out_busy);
357 int usb_wwan_write_room(struct tty_struct *tty)
359 struct usb_serial_port *port = tty->driver_data;
360 struct usb_wwan_port_private *portdata;
363 struct urb *this_urb;
365 portdata = usb_get_serial_port_data(port);
367 for (i = 0; i < N_OUT_URB; i++) {
368 this_urb = portdata->out_urbs[i];
369 if (this_urb && !test_bit(i, &portdata->out_busy))
370 data_len += OUT_BUFLEN;
373 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
376 EXPORT_SYMBOL(usb_wwan_write_room);
378 int usb_wwan_chars_in_buffer(struct tty_struct *tty)
380 struct usb_serial_port *port = tty->driver_data;
381 struct usb_wwan_port_private *portdata;
384 struct urb *this_urb;
386 portdata = usb_get_serial_port_data(port);
388 for (i = 0; i < N_OUT_URB; i++) {
389 this_urb = portdata->out_urbs[i];
390 /* FIXME: This locking is insufficient as this_urb may
391 go unused during the test */
392 if (this_urb && test_bit(i, &portdata->out_busy))
393 data_len += this_urb->transfer_buffer_length;
395 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
398 EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
400 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
402 struct usb_wwan_port_private *portdata;
403 struct usb_wwan_intf_private *intfdata;
404 struct usb_serial *serial = port->serial;
408 portdata = usb_get_serial_port_data(port);
409 intfdata = usb_get_serial_data(serial);
411 if (port->interrupt_in_urb) {
412 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
414 dev_err(&port->dev, "%s: submit int urb failed: %d\n",
419 /* Start reading from the IN endpoint */
420 for (i = 0; i < N_IN_URB; i++) {
421 urb = portdata->in_urbs[i];
424 err = usb_submit_urb(urb, GFP_KERNEL);
427 "%s: submit read urb %d failed: %d\n",
432 spin_lock_irq(&intfdata->susp_lock);
433 if (++intfdata->open_ports == 1)
434 serial->interface->needs_remote_wakeup = 1;
435 spin_unlock_irq(&intfdata->susp_lock);
436 /* this balances a get in the generic USB serial code */
437 usb_autopm_put_interface(serial->interface);
441 EXPORT_SYMBOL(usb_wwan_open);
443 static void unbusy_queued_urb(struct urb *urb,
444 struct usb_wwan_port_private *portdata)
448 for (i = 0; i < N_OUT_URB; i++) {
449 if (urb == portdata->out_urbs[i]) {
450 clear_bit(i, &portdata->out_busy);
456 void usb_wwan_close(struct usb_serial_port *port)
459 struct usb_serial *serial = port->serial;
460 struct usb_wwan_port_private *portdata;
461 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
464 portdata = usb_get_serial_port_data(port);
467 * Need to take susp_lock to make sure port is not already being
468 * resumed, but no need to hold it due to the tty-port initialized
471 spin_lock_irq(&intfdata->susp_lock);
472 if (--intfdata->open_ports == 0)
473 serial->interface->needs_remote_wakeup = 0;
474 spin_unlock_irq(&intfdata->susp_lock);
477 urb = usb_get_from_anchor(&portdata->delayed);
480 unbusy_queued_urb(urb, portdata);
481 usb_autopm_put_interface_async(serial->interface);
484 for (i = 0; i < N_IN_URB; i++)
485 usb_kill_urb(portdata->in_urbs[i]);
486 for (i = 0; i < N_OUT_URB; i++)
487 usb_kill_urb(portdata->out_urbs[i]);
488 usb_kill_urb(port->interrupt_in_urb);
490 usb_autopm_get_interface_no_resume(serial->interface);
492 EXPORT_SYMBOL(usb_wwan_close);
494 static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
496 int dir, void *ctx, char *buf, int len,
497 void (*callback) (struct urb *))
499 struct usb_serial *serial = port->serial;
500 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
503 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
507 usb_fill_bulk_urb(urb, serial->dev,
508 usb_sndbulkpipe(serial->dev, endpoint) | dir,
509 buf, len, callback, ctx);
511 if (intfdata->use_zlp && dir == USB_DIR_OUT)
512 urb->transfer_flags |= URB_ZERO_PACKET;
517 int usb_wwan_port_probe(struct usb_serial_port *port)
519 struct usb_wwan_port_private *portdata;
524 if (!port->bulk_in_size || !port->bulk_out_size)
527 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
531 init_usb_anchor(&portdata->delayed);
533 for (i = 0; i < N_IN_URB; i++) {
534 buffer = (u8 *)__get_free_page(GFP_KERNEL);
537 portdata->in_buffer[i] = buffer;
539 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
542 usb_wwan_indat_callback);
543 portdata->in_urbs[i] = urb;
546 for (i = 0; i < N_OUT_URB; i++) {
547 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
549 goto bail_out_error2;
550 portdata->out_buffer[i] = buffer;
552 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
555 usb_wwan_outdat_callback);
556 portdata->out_urbs[i] = urb;
559 usb_set_serial_port_data(port, portdata);
564 for (i = 0; i < N_OUT_URB; i++) {
565 usb_free_urb(portdata->out_urbs[i]);
566 kfree(portdata->out_buffer[i]);
569 for (i = 0; i < N_IN_URB; i++) {
570 usb_free_urb(portdata->in_urbs[i]);
571 free_page((unsigned long)portdata->in_buffer[i]);
577 EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
579 int usb_wwan_port_remove(struct usb_serial_port *port)
582 struct usb_wwan_port_private *portdata;
584 portdata = usb_get_serial_port_data(port);
585 usb_set_serial_port_data(port, NULL);
587 for (i = 0; i < N_IN_URB; i++) {
588 usb_free_urb(portdata->in_urbs[i]);
589 free_page((unsigned long)portdata->in_buffer[i]);
591 for (i = 0; i < N_OUT_URB; i++) {
592 usb_free_urb(portdata->out_urbs[i]);
593 kfree(portdata->out_buffer[i]);
600 EXPORT_SYMBOL(usb_wwan_port_remove);
603 static void stop_urbs(struct usb_serial *serial)
606 struct usb_serial_port *port;
607 struct usb_wwan_port_private *portdata;
609 for (i = 0; i < serial->num_ports; ++i) {
610 port = serial->port[i];
611 portdata = usb_get_serial_port_data(port);
614 for (j = 0; j < N_IN_URB; j++)
615 usb_kill_urb(portdata->in_urbs[j]);
616 for (j = 0; j < N_OUT_URB; j++)
617 usb_kill_urb(portdata->out_urbs[j]);
618 usb_kill_urb(port->interrupt_in_urb);
622 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
624 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
626 spin_lock_irq(&intfdata->susp_lock);
627 if (PMSG_IS_AUTO(message)) {
628 if (intfdata->in_flight) {
629 spin_unlock_irq(&intfdata->susp_lock);
633 intfdata->suspended = 1;
634 spin_unlock_irq(&intfdata->susp_lock);
640 EXPORT_SYMBOL(usb_wwan_suspend);
642 /* Caller must hold susp_lock. */
643 static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
645 struct usb_serial *serial = port->serial;
646 struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
647 struct usb_wwan_port_private *portdata;
652 portdata = usb_get_serial_port_data(port);
655 urb = usb_get_from_anchor(&portdata->delayed);
659 err = usb_submit_urb(urb, GFP_ATOMIC);
661 dev_err(&port->dev, "%s: submit urb failed: %d\n",
664 unbusy_queued_urb(urb, portdata);
665 usb_autopm_put_interface_async(serial->interface);
677 int usb_wwan_resume(struct usb_serial *serial)
680 struct usb_serial_port *port;
681 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
682 struct usb_wwan_port_private *portdata;
687 spin_lock_irq(&intfdata->susp_lock);
688 for (i = 0; i < serial->num_ports; i++) {
689 port = serial->port[i];
691 if (!tty_port_initialized(&port->port))
694 portdata = usb_get_serial_port_data(port);
696 if (port->interrupt_in_urb) {
697 err = usb_submit_urb(port->interrupt_in_urb,
701 "%s: submit int urb failed: %d\n",
707 err = usb_wwan_submit_delayed_urbs(port);
711 for (j = 0; j < N_IN_URB; j++) {
712 urb = portdata->in_urbs[j];
713 err = usb_submit_urb(urb, GFP_ATOMIC);
716 "%s: submit read urb %d failed: %d\n",
722 intfdata->suspended = 0;
723 spin_unlock_irq(&intfdata->susp_lock);
730 EXPORT_SYMBOL(usb_wwan_resume);
733 MODULE_AUTHOR(DRIVER_AUTHOR);
734 MODULE_DESCRIPTION(DRIVER_DESC);
735 MODULE_LICENSE("GPL");