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_VERSION "v0.7.2"
23 #define DRIVER_AUTHOR "Matthias Urlichs <smurf@smurf.noris.de>"
24 #define DRIVER_DESC "USB Driver for GSM modems"
26 #include <linux/kernel.h>
27 #include <linux/jiffies.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/tty.h>
31 #include <linux/tty_flip.h>
32 #include <linux/module.h>
33 #include <linux/bitops.h>
34 #include <linux/uaccess.h>
35 #include <linux/usb.h>
36 #include <linux/usb/serial.h>
37 #include <linux/serial.h>
42 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
44 struct usb_serial *serial = port->serial;
45 struct usb_wwan_port_private *portdata;
47 struct usb_wwan_intf_private *intfdata;
51 intfdata = port->serial->private;
53 if (!intfdata->send_setup)
56 portdata = usb_get_serial_port_data(port);
57 mutex_lock(&serial->disc_mutex);
58 portdata->rts_state = on;
59 portdata->dtr_state = on;
61 intfdata->send_setup(port);
62 mutex_unlock(&serial->disc_mutex);
64 EXPORT_SYMBOL(usb_wwan_dtr_rts);
66 void usb_wwan_set_termios(struct tty_struct *tty,
67 struct usb_serial_port *port,
68 struct ktermios *old_termios)
70 struct usb_wwan_intf_private *intfdata = port->serial->private;
74 /* Doesn't support option setting */
75 tty_termios_copy_hw(tty->termios, old_termios);
77 if (intfdata->send_setup)
78 intfdata->send_setup(port);
80 EXPORT_SYMBOL(usb_wwan_set_termios);
82 int usb_wwan_tiocmget(struct tty_struct *tty, struct file *file)
84 struct usb_serial_port *port = tty->driver_data;
86 struct usb_wwan_port_private *portdata;
88 portdata = usb_get_serial_port_data(port);
90 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
91 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
92 ((portdata->cts_state) ? TIOCM_CTS : 0) |
93 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
94 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
95 ((portdata->ri_state) ? TIOCM_RNG : 0);
99 EXPORT_SYMBOL(usb_wwan_tiocmget);
101 int usb_wwan_tiocmset(struct tty_struct *tty, struct file *file,
102 unsigned int set, unsigned int clear)
104 struct usb_serial_port *port = tty->driver_data;
105 struct usb_wwan_port_private *portdata;
106 struct usb_wwan_intf_private *intfdata;
108 portdata = usb_get_serial_port_data(port);
109 intfdata = port->serial->private;
111 if (!intfdata->send_setup)
114 /* FIXME: what locks portdata fields ? */
116 portdata->rts_state = 1;
118 portdata->dtr_state = 1;
120 if (clear & TIOCM_RTS)
121 portdata->rts_state = 0;
122 if (clear & TIOCM_DTR)
123 portdata->dtr_state = 0;
124 return intfdata->send_setup(port);
126 EXPORT_SYMBOL(usb_wwan_tiocmset);
128 static int get_serial_info(struct usb_serial_port *port,
129 struct serial_struct __user *retinfo)
131 struct serial_struct tmp;
136 memset(&tmp, 0, sizeof(tmp));
137 tmp.line = port->serial->minor;
138 tmp.port = port->number;
139 tmp.baud_base = tty_get_baud_rate(port->port.tty);
140 tmp.close_delay = port->port.close_delay / 10;
141 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
142 ASYNC_CLOSING_WAIT_NONE :
143 port->port.closing_wait / 10;
145 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
150 static int set_serial_info(struct usb_serial_port *port,
151 struct serial_struct __user *newinfo)
153 struct serial_struct new_serial;
154 unsigned int closing_wait, close_delay;
157 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
160 close_delay = new_serial.close_delay * 10;
161 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
162 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
164 mutex_lock(&port->port.mutex);
166 if (!capable(CAP_SYS_ADMIN)) {
167 if ((close_delay != port->port.close_delay) ||
168 (closing_wait != port->port.closing_wait))
171 retval = -EOPNOTSUPP;
173 port->port.close_delay = close_delay;
174 port->port.closing_wait = closing_wait;
177 mutex_unlock(&port->port.mutex);
181 int usb_wwan_ioctl(struct tty_struct *tty, struct file *file,
182 unsigned int cmd, unsigned long arg)
184 struct usb_serial_port *port = tty->driver_data;
186 dbg("%s cmd 0x%04x", __func__, cmd);
190 return get_serial_info(port,
191 (struct serial_struct __user *) arg);
193 return set_serial_info(port,
194 (struct serial_struct __user *) arg);
199 dbg("%s arg not supported", __func__);
203 EXPORT_SYMBOL(usb_wwan_ioctl);
206 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
207 const unsigned char *buf, int count)
209 struct usb_wwan_port_private *portdata;
210 struct usb_wwan_intf_private *intfdata;
213 struct urb *this_urb = NULL; /* spurious */
217 portdata = usb_get_serial_port_data(port);
218 intfdata = port->serial->private;
220 dbg("%s: write (%d chars)", __func__, count);
224 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
226 if (todo > OUT_BUFLEN)
229 this_urb = portdata->out_urbs[i];
230 if (test_and_set_bit(i, &portdata->out_busy)) {
231 if (time_before(jiffies,
232 portdata->tx_start_time[i] + 10 * HZ))
234 usb_unlink_urb(this_urb);
237 dbg("%s: endpoint %d buf %d", __func__,
238 usb_pipeendpoint(this_urb->pipe), i);
240 err = usb_autopm_get_interface_async(port->serial->interface);
245 memcpy(this_urb->transfer_buffer, buf, todo);
246 this_urb->transfer_buffer_length = todo;
248 spin_lock_irqsave(&intfdata->susp_lock, flags);
249 if (intfdata->suspended) {
250 usb_anchor_urb(this_urb, &portdata->delayed);
251 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
253 intfdata->in_flight++;
254 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
255 err = usb_submit_urb(this_urb, GFP_ATOMIC);
257 dbg("usb_submit_urb %p (write bulk) failed "
258 "(%d)", this_urb, err);
259 clear_bit(i, &portdata->out_busy);
260 spin_lock_irqsave(&intfdata->susp_lock, flags);
261 intfdata->in_flight--;
262 spin_unlock_irqrestore(&intfdata->susp_lock,
268 portdata->tx_start_time[i] = jiffies;
274 dbg("%s: wrote (did %d)", __func__, count);
277 EXPORT_SYMBOL(usb_wwan_write);
279 static void usb_wwan_indat_callback(struct urb *urb)
283 struct usb_serial_port *port;
284 struct tty_struct *tty;
285 unsigned char *data = urb->transfer_buffer;
286 int status = urb->status;
288 dbg("%s: %p", __func__, urb);
290 endpoint = usb_pipeendpoint(urb->pipe);
294 dbg("%s: nonzero status: %d on endpoint %02x.",
295 __func__, status, endpoint);
297 tty = tty_port_tty_get(&port->port);
299 if (urb->actual_length) {
300 tty_insert_flip_string(tty, data,
302 tty_flip_buffer_push(tty);
304 dbg("%s: empty read urb received", __func__);
308 /* Resubmit urb so we continue receiving */
309 if (status != -ESHUTDOWN) {
310 err = usb_submit_urb(urb, GFP_ATOMIC);
311 if (err && err != -EPERM)
312 printk(KERN_ERR "%s: resubmit read urb failed. "
313 "(%d)", __func__, err);
315 usb_mark_last_busy(port->serial->dev);
321 static void usb_wwan_outdat_callback(struct urb *urb)
323 struct usb_serial_port *port;
324 struct usb_wwan_port_private *portdata;
325 struct usb_wwan_intf_private *intfdata;
331 intfdata = port->serial->private;
333 usb_serial_port_softint(port);
334 usb_autopm_put_interface_async(port->serial->interface);
335 portdata = usb_get_serial_port_data(port);
336 spin_lock(&intfdata->susp_lock);
337 intfdata->in_flight--;
338 spin_unlock(&intfdata->susp_lock);
340 for (i = 0; i < N_OUT_URB; ++i) {
341 if (portdata->out_urbs[i] == urb) {
342 smp_mb__before_clear_bit();
343 clear_bit(i, &portdata->out_busy);
349 int usb_wwan_write_room(struct tty_struct *tty)
351 struct usb_serial_port *port = tty->driver_data;
352 struct usb_wwan_port_private *portdata;
355 struct urb *this_urb;
357 portdata = usb_get_serial_port_data(port);
359 for (i = 0; i < N_OUT_URB; i++) {
360 this_urb = portdata->out_urbs[i];
361 if (this_urb && !test_bit(i, &portdata->out_busy))
362 data_len += OUT_BUFLEN;
365 dbg("%s: %d", __func__, data_len);
368 EXPORT_SYMBOL(usb_wwan_write_room);
370 int usb_wwan_chars_in_buffer(struct tty_struct *tty)
372 struct usb_serial_port *port = tty->driver_data;
373 struct usb_wwan_port_private *portdata;
376 struct urb *this_urb;
378 portdata = usb_get_serial_port_data(port);
380 for (i = 0; i < N_OUT_URB; i++) {
381 this_urb = portdata->out_urbs[i];
382 /* FIXME: This locking is insufficient as this_urb may
383 go unused during the test */
384 if (this_urb && test_bit(i, &portdata->out_busy))
385 data_len += this_urb->transfer_buffer_length;
387 dbg("%s: %d", __func__, data_len);
390 EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
392 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
394 struct usb_wwan_port_private *portdata;
395 struct usb_wwan_intf_private *intfdata;
396 struct usb_serial *serial = port->serial;
400 portdata = usb_get_serial_port_data(port);
401 intfdata = serial->private;
405 /* Start reading from the IN endpoint */
406 for (i = 0; i < N_IN_URB; i++) {
407 urb = portdata->in_urbs[i];
410 err = usb_submit_urb(urb, GFP_KERNEL);
412 dbg("%s: submit urb %d failed (%d) %d",
413 __func__, i, err, urb->transfer_buffer_length);
417 if (intfdata->send_setup)
418 intfdata->send_setup(port);
420 serial->interface->needs_remote_wakeup = 1;
421 spin_lock_irq(&intfdata->susp_lock);
422 portdata->opened = 1;
423 spin_unlock_irq(&intfdata->susp_lock);
424 usb_autopm_put_interface(serial->interface);
428 EXPORT_SYMBOL(usb_wwan_open);
430 void usb_wwan_close(struct usb_serial_port *port)
433 struct usb_serial *serial = port->serial;
434 struct usb_wwan_port_private *portdata;
435 struct usb_wwan_intf_private *intfdata = port->serial->private;
438 portdata = usb_get_serial_port_data(port);
441 /* Stop reading/writing urbs */
442 spin_lock_irq(&intfdata->susp_lock);
443 portdata->opened = 0;
444 spin_unlock_irq(&intfdata->susp_lock);
446 for (i = 0; i < N_IN_URB; i++)
447 usb_kill_urb(portdata->in_urbs[i]);
448 for (i = 0; i < N_OUT_URB; i++)
449 usb_kill_urb(portdata->out_urbs[i]);
450 usb_autopm_get_interface(serial->interface);
451 serial->interface->needs_remote_wakeup = 0;
454 EXPORT_SYMBOL(usb_wwan_close);
456 /* Helper functions used by usb_wwan_setup_urbs */
457 static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
458 int dir, void *ctx, char *buf, int len,
459 void (*callback) (struct urb *))
464 return NULL; /* endpoint not needed */
466 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
468 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
472 /* Fill URB using supplied data. */
473 usb_fill_bulk_urb(urb, serial->dev,
474 usb_sndbulkpipe(serial->dev, endpoint) | dir,
475 buf, len, callback, ctx);
481 static void usb_wwan_setup_urbs(struct usb_serial *serial)
484 struct usb_serial_port *port;
485 struct usb_wwan_port_private *portdata;
489 for (i = 0; i < serial->num_ports; i++) {
490 port = serial->port[i];
491 portdata = usb_get_serial_port_data(port);
493 /* Do indat endpoints first */
494 for (j = 0; j < N_IN_URB; ++j) {
495 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
497 bulk_in_endpointAddress,
503 usb_wwan_indat_callback);
506 /* outdat endpoints */
507 for (j = 0; j < N_OUT_URB; ++j) {
508 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
510 bulk_out_endpointAddress,
517 usb_wwan_outdat_callback);
522 int usb_wwan_startup(struct usb_serial *serial)
525 struct usb_serial_port *port;
526 struct usb_wwan_port_private *portdata;
531 /* Now setup per port private data */
532 for (i = 0; i < serial->num_ports; i++) {
533 port = serial->port[i];
534 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
536 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
540 init_usb_anchor(&portdata->delayed);
542 for (j = 0; j < N_IN_URB; j++) {
543 buffer = (u8 *) __get_free_page(GFP_KERNEL);
546 portdata->in_buffer[j] = buffer;
549 for (j = 0; j < N_OUT_URB; j++) {
550 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
552 goto bail_out_error2;
553 portdata->out_buffer[j] = buffer;
556 usb_set_serial_port_data(port, portdata);
558 if (!port->interrupt_in_urb)
560 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
562 dbg("%s: submit irq_in urb failed %d", __func__, err);
564 usb_wwan_setup_urbs(serial);
568 for (j = 0; j < N_OUT_URB; j++)
569 kfree(portdata->out_buffer[j]);
571 for (j = 0; j < N_IN_URB; j++)
572 if (portdata->in_buffer[j])
573 free_page((unsigned long)portdata->in_buffer[j]);
577 EXPORT_SYMBOL(usb_wwan_startup);
579 static void stop_read_write_urbs(struct usb_serial *serial)
582 struct usb_serial_port *port;
583 struct usb_wwan_port_private *portdata;
585 /* Stop reading/writing urbs */
586 for (i = 0; i < serial->num_ports; ++i) {
587 port = serial->port[i];
588 portdata = usb_get_serial_port_data(port);
589 for (j = 0; j < N_IN_URB; j++)
590 usb_kill_urb(portdata->in_urbs[j]);
591 for (j = 0; j < N_OUT_URB; j++)
592 usb_kill_urb(portdata->out_urbs[j]);
596 void usb_wwan_disconnect(struct usb_serial *serial)
600 stop_read_write_urbs(serial);
602 EXPORT_SYMBOL(usb_wwan_disconnect);
604 void usb_wwan_release(struct usb_serial *serial)
607 struct usb_serial_port *port;
608 struct usb_wwan_port_private *portdata;
613 for (i = 0; i < serial->num_ports; ++i) {
614 port = serial->port[i];
615 portdata = usb_get_serial_port_data(port);
617 for (j = 0; j < N_IN_URB; j++) {
618 usb_free_urb(portdata->in_urbs[j]);
619 free_page((unsigned long)
620 portdata->in_buffer[j]);
621 portdata->in_urbs[j] = NULL;
623 for (j = 0; j < N_OUT_URB; j++) {
624 usb_free_urb(portdata->out_urbs[j]);
625 kfree(portdata->out_buffer[j]);
626 portdata->out_urbs[j] = NULL;
630 /* Now free per port private data */
631 for (i = 0; i < serial->num_ports; i++) {
632 port = serial->port[i];
633 kfree(usb_get_serial_port_data(port));
636 EXPORT_SYMBOL(usb_wwan_release);
639 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
641 struct usb_wwan_intf_private *intfdata = serial->private;
644 dbg("%s entered", __func__);
646 if (message.event & PM_EVENT_AUTO) {
647 spin_lock_irq(&intfdata->susp_lock);
648 b = intfdata->in_flight;
649 spin_unlock_irq(&intfdata->susp_lock);
655 spin_lock_irq(&intfdata->susp_lock);
656 intfdata->suspended = 1;
657 spin_unlock_irq(&intfdata->susp_lock);
658 stop_read_write_urbs(serial);
662 EXPORT_SYMBOL(usb_wwan_suspend);
664 static void play_delayed(struct usb_serial_port *port)
666 struct usb_wwan_intf_private *data;
667 struct usb_wwan_port_private *portdata;
671 portdata = usb_get_serial_port_data(port);
672 data = port->serial->private;
673 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
674 err = usb_submit_urb(urb, GFP_ATOMIC);
680 int usb_wwan_resume(struct usb_serial *serial)
683 struct usb_serial_port *port;
684 struct usb_wwan_intf_private *intfdata = serial->private;
685 struct usb_wwan_port_private *portdata;
689 dbg("%s entered", __func__);
690 /* get the interrupt URBs resubmitted unconditionally */
691 for (i = 0; i < serial->num_ports; i++) {
692 port = serial->port[i];
693 if (!port->interrupt_in_urb) {
694 dbg("%s: No interrupt URB for port %d", __func__, i);
697 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
698 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
700 err("%s: Error %d for interrupt URB of port%d",
706 for (i = 0; i < serial->num_ports; i++) {
708 port = serial->port[i];
709 portdata = usb_get_serial_port_data(port);
711 /* skip closed ports */
712 spin_lock_irq(&intfdata->susp_lock);
713 if (!portdata->opened) {
714 spin_unlock_irq(&intfdata->susp_lock);
718 for (j = 0; j < N_IN_URB; j++) {
719 urb = portdata->in_urbs[j];
720 err = usb_submit_urb(urb, GFP_ATOMIC);
722 err("%s: Error %d for bulk URB %d",
724 spin_unlock_irq(&intfdata->susp_lock);
729 spin_unlock_irq(&intfdata->susp_lock);
731 spin_lock_irq(&intfdata->susp_lock);
732 intfdata->suspended = 0;
733 spin_unlock_irq(&intfdata->susp_lock);
737 EXPORT_SYMBOL(usb_wwan_resume);
740 MODULE_AUTHOR(DRIVER_AUTHOR);
741 MODULE_DESCRIPTION(DRIVER_DESC);
742 MODULE_VERSION(DRIVER_VERSION);
743 MODULE_LICENSE("GPL");
745 module_param(debug, bool, S_IRUGO | S_IWUSR);
746 MODULE_PARM_DESC(debug, "Debug messages");