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/usb.h>
35 #include <linux/usb/serial.h>
40 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
42 struct usb_serial *serial = port->serial;
43 struct usb_wwan_port_private *portdata;
45 struct usb_wwan_intf_private *intfdata;
49 intfdata = port->serial->private;
51 if (!intfdata->send_setup)
54 portdata = usb_get_serial_port_data(port);
55 mutex_lock(&serial->disc_mutex);
56 portdata->rts_state = on;
57 portdata->dtr_state = on;
59 intfdata->send_setup(port);
60 mutex_unlock(&serial->disc_mutex);
62 EXPORT_SYMBOL(usb_wwan_dtr_rts);
64 void usb_wwan_set_termios(struct tty_struct *tty,
65 struct usb_serial_port *port,
66 struct ktermios *old_termios)
68 struct usb_wwan_intf_private *intfdata = port->serial->private;
72 /* Doesn't support option setting */
73 tty_termios_copy_hw(tty->termios, old_termios);
75 if (intfdata->send_setup)
76 intfdata->send_setup(port);
78 EXPORT_SYMBOL(usb_wwan_set_termios);
80 int usb_wwan_tiocmget(struct tty_struct *tty, struct file *file)
82 struct usb_serial_port *port = tty->driver_data;
84 struct usb_wwan_port_private *portdata;
86 portdata = usb_get_serial_port_data(port);
88 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
89 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
90 ((portdata->cts_state) ? TIOCM_CTS : 0) |
91 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
92 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
93 ((portdata->ri_state) ? TIOCM_RNG : 0);
97 EXPORT_SYMBOL(usb_wwan_tiocmget);
99 int usb_wwan_tiocmset(struct tty_struct *tty, struct file *file,
100 unsigned int set, unsigned int clear)
102 struct usb_serial_port *port = tty->driver_data;
103 struct usb_wwan_port_private *portdata;
104 struct usb_wwan_intf_private *intfdata;
106 portdata = usb_get_serial_port_data(port);
107 intfdata = port->serial->private;
109 if (!intfdata->send_setup)
112 /* FIXME: what locks portdata fields ? */
114 portdata->rts_state = 1;
116 portdata->dtr_state = 1;
118 if (clear & TIOCM_RTS)
119 portdata->rts_state = 0;
120 if (clear & TIOCM_DTR)
121 portdata->dtr_state = 0;
122 return intfdata->send_setup(port);
124 EXPORT_SYMBOL(usb_wwan_tiocmset);
127 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
128 const unsigned char *buf, int count)
130 struct usb_wwan_port_private *portdata;
131 struct usb_wwan_intf_private *intfdata;
134 struct urb *this_urb = NULL; /* spurious */
138 portdata = usb_get_serial_port_data(port);
139 intfdata = port->serial->private;
141 dbg("%s: write (%d chars)", __func__, count);
145 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
147 if (todo > OUT_BUFLEN)
150 this_urb = portdata->out_urbs[i];
151 if (test_and_set_bit(i, &portdata->out_busy)) {
152 if (time_before(jiffies,
153 portdata->tx_start_time[i] + 10 * HZ))
155 usb_unlink_urb(this_urb);
158 dbg("%s: endpoint %d buf %d", __func__,
159 usb_pipeendpoint(this_urb->pipe), i);
161 err = usb_autopm_get_interface_async(port->serial->interface);
166 memcpy(this_urb->transfer_buffer, buf, todo);
167 this_urb->transfer_buffer_length = todo;
169 spin_lock_irqsave(&intfdata->susp_lock, flags);
170 if (intfdata->suspended) {
171 usb_anchor_urb(this_urb, &portdata->delayed);
172 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
174 intfdata->in_flight++;
175 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
176 err = usb_submit_urb(this_urb, GFP_ATOMIC);
178 dbg("usb_submit_urb %p (write bulk) failed "
179 "(%d)", this_urb, err);
180 clear_bit(i, &portdata->out_busy);
181 spin_lock_irqsave(&intfdata->susp_lock, flags);
182 intfdata->in_flight--;
183 spin_unlock_irqrestore(&intfdata->susp_lock,
189 portdata->tx_start_time[i] = jiffies;
195 dbg("%s: wrote (did %d)", __func__, count);
198 EXPORT_SYMBOL(usb_wwan_write);
200 static void usb_wwan_indat_callback(struct urb *urb)
204 struct usb_serial_port *port;
205 struct tty_struct *tty;
206 unsigned char *data = urb->transfer_buffer;
207 int status = urb->status;
209 dbg("%s: %p", __func__, urb);
211 endpoint = usb_pipeendpoint(urb->pipe);
215 dbg("%s: nonzero status: %d on endpoint %02x.",
216 __func__, status, endpoint);
218 tty = tty_port_tty_get(&port->port);
219 if (urb->actual_length) {
220 tty_insert_flip_string(tty, data, urb->actual_length);
221 tty_flip_buffer_push(tty);
223 dbg("%s: empty read urb received", __func__);
226 /* Resubmit urb so we continue receiving */
227 if (status != -ESHUTDOWN) {
228 err = usb_submit_urb(urb, GFP_ATOMIC);
229 if (err && err != -EPERM)
230 printk(KERN_ERR "%s: resubmit read urb failed. "
231 "(%d)", __func__, err);
233 usb_mark_last_busy(port->serial->dev);
240 static void usb_wwan_outdat_callback(struct urb *urb)
242 struct usb_serial_port *port;
243 struct usb_wwan_port_private *portdata;
244 struct usb_wwan_intf_private *intfdata;
250 intfdata = port->serial->private;
252 usb_serial_port_softint(port);
253 usb_autopm_put_interface_async(port->serial->interface);
254 portdata = usb_get_serial_port_data(port);
255 spin_lock(&intfdata->susp_lock);
256 intfdata->in_flight--;
257 spin_unlock(&intfdata->susp_lock);
259 for (i = 0; i < N_OUT_URB; ++i) {
260 if (portdata->out_urbs[i] == urb) {
261 smp_mb__before_clear_bit();
262 clear_bit(i, &portdata->out_busy);
268 int usb_wwan_write_room(struct tty_struct *tty)
270 struct usb_serial_port *port = tty->driver_data;
271 struct usb_wwan_port_private *portdata;
274 struct urb *this_urb;
276 portdata = usb_get_serial_port_data(port);
278 for (i = 0; i < N_OUT_URB; i++) {
279 this_urb = portdata->out_urbs[i];
280 if (this_urb && !test_bit(i, &portdata->out_busy))
281 data_len += OUT_BUFLEN;
284 dbg("%s: %d", __func__, data_len);
287 EXPORT_SYMBOL(usb_wwan_write_room);
289 int usb_wwan_chars_in_buffer(struct tty_struct *tty)
291 struct usb_serial_port *port = tty->driver_data;
292 struct usb_wwan_port_private *portdata;
295 struct urb *this_urb;
297 portdata = usb_get_serial_port_data(port);
299 for (i = 0; i < N_OUT_URB; i++) {
300 this_urb = portdata->out_urbs[i];
301 /* FIXME: This locking is insufficient as this_urb may
302 go unused during the test */
303 if (this_urb && test_bit(i, &portdata->out_busy))
304 data_len += this_urb->transfer_buffer_length;
306 dbg("%s: %d", __func__, data_len);
309 EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
311 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
313 struct usb_wwan_port_private *portdata;
314 struct usb_wwan_intf_private *intfdata;
315 struct usb_serial *serial = port->serial;
319 portdata = usb_get_serial_port_data(port);
320 intfdata = serial->private;
324 /* Start reading from the IN endpoint */
325 for (i = 0; i < N_IN_URB; i++) {
326 urb = portdata->in_urbs[i];
329 err = usb_submit_urb(urb, GFP_KERNEL);
331 dbg("%s: submit urb %d failed (%d) %d",
332 __func__, i, err, urb->transfer_buffer_length);
336 if (intfdata->send_setup)
337 intfdata->send_setup(port);
339 serial->interface->needs_remote_wakeup = 1;
340 spin_lock_irq(&intfdata->susp_lock);
341 portdata->opened = 1;
342 spin_unlock_irq(&intfdata->susp_lock);
343 usb_autopm_put_interface(serial->interface);
347 EXPORT_SYMBOL(usb_wwan_open);
349 void usb_wwan_close(struct usb_serial_port *port)
352 struct usb_serial *serial = port->serial;
353 struct usb_wwan_port_private *portdata;
354 struct usb_wwan_intf_private *intfdata = port->serial->private;
357 portdata = usb_get_serial_port_data(port);
360 /* Stop reading/writing urbs */
361 spin_lock_irq(&intfdata->susp_lock);
362 portdata->opened = 0;
363 spin_unlock_irq(&intfdata->susp_lock);
365 for (i = 0; i < N_IN_URB; i++)
366 usb_kill_urb(portdata->in_urbs[i]);
367 for (i = 0; i < N_OUT_URB; i++)
368 usb_kill_urb(portdata->out_urbs[i]);
369 usb_autopm_get_interface(serial->interface);
370 serial->interface->needs_remote_wakeup = 0;
373 EXPORT_SYMBOL(usb_wwan_close);
375 /* Helper functions used by usb_wwan_setup_urbs */
376 static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
377 int dir, void *ctx, char *buf, int len,
378 void (*callback) (struct urb *))
383 return NULL; /* endpoint not needed */
385 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
387 dbg("%s: alloc for endpoint %d failed.", __func__, endpoint);
391 /* Fill URB using supplied data. */
392 usb_fill_bulk_urb(urb, serial->dev,
393 usb_sndbulkpipe(serial->dev, endpoint) | dir,
394 buf, len, callback, ctx);
400 static void usb_wwan_setup_urbs(struct usb_serial *serial)
403 struct usb_serial_port *port;
404 struct usb_wwan_port_private *portdata;
408 for (i = 0; i < serial->num_ports; i++) {
409 port = serial->port[i];
410 portdata = usb_get_serial_port_data(port);
412 /* Do indat endpoints first */
413 for (j = 0; j < N_IN_URB; ++j) {
414 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
416 bulk_in_endpointAddress,
422 usb_wwan_indat_callback);
425 /* outdat endpoints */
426 for (j = 0; j < N_OUT_URB; ++j) {
427 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
429 bulk_out_endpointAddress,
436 usb_wwan_outdat_callback);
441 int usb_wwan_startup(struct usb_serial *serial)
444 struct usb_serial_port *port;
445 struct usb_wwan_port_private *portdata;
450 /* Now setup per port private data */
451 for (i = 0; i < serial->num_ports; i++) {
452 port = serial->port[i];
453 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
455 dbg("%s: kmalloc for usb_wwan_port_private (%d) failed!.",
459 init_usb_anchor(&portdata->delayed);
461 for (j = 0; j < N_IN_URB; j++) {
462 buffer = (u8 *) __get_free_page(GFP_KERNEL);
465 portdata->in_buffer[j] = buffer;
468 for (j = 0; j < N_OUT_URB; j++) {
469 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
471 goto bail_out_error2;
472 portdata->out_buffer[j] = buffer;
475 usb_set_serial_port_data(port, portdata);
477 if (!port->interrupt_in_urb)
479 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
481 dbg("%s: submit irq_in urb failed %d", __func__, err);
483 usb_wwan_setup_urbs(serial);
487 for (j = 0; j < N_OUT_URB; j++)
488 kfree(portdata->out_buffer[j]);
490 for (j = 0; j < N_IN_URB; j++)
491 if (portdata->in_buffer[j])
492 free_page((unsigned long)portdata->in_buffer[j]);
496 EXPORT_SYMBOL(usb_wwan_startup);
498 static void stop_read_write_urbs(struct usb_serial *serial)
501 struct usb_serial_port *port;
502 struct usb_wwan_port_private *portdata;
504 /* Stop reading/writing urbs */
505 for (i = 0; i < serial->num_ports; ++i) {
506 port = serial->port[i];
507 portdata = usb_get_serial_port_data(port);
508 for (j = 0; j < N_IN_URB; j++)
509 usb_kill_urb(portdata->in_urbs[j]);
510 for (j = 0; j < N_OUT_URB; j++)
511 usb_kill_urb(portdata->out_urbs[j]);
515 void usb_wwan_disconnect(struct usb_serial *serial)
519 stop_read_write_urbs(serial);
521 EXPORT_SYMBOL(usb_wwan_disconnect);
523 void usb_wwan_release(struct usb_serial *serial)
526 struct usb_serial_port *port;
527 struct usb_wwan_port_private *portdata;
532 for (i = 0; i < serial->num_ports; ++i) {
533 port = serial->port[i];
534 portdata = usb_get_serial_port_data(port);
536 for (j = 0; j < N_IN_URB; j++) {
537 usb_free_urb(portdata->in_urbs[j]);
538 free_page((unsigned long)
539 portdata->in_buffer[j]);
540 portdata->in_urbs[j] = NULL;
542 for (j = 0; j < N_OUT_URB; j++) {
543 usb_free_urb(portdata->out_urbs[j]);
544 kfree(portdata->out_buffer[j]);
545 portdata->out_urbs[j] = NULL;
549 /* Now free per port private data */
550 for (i = 0; i < serial->num_ports; i++) {
551 port = serial->port[i];
552 kfree(usb_get_serial_port_data(port));
555 EXPORT_SYMBOL(usb_wwan_release);
558 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
560 struct usb_wwan_intf_private *intfdata = serial->private;
563 dbg("%s entered", __func__);
565 if (message.event & PM_EVENT_AUTO) {
566 spin_lock_irq(&intfdata->susp_lock);
567 b = intfdata->in_flight;
568 spin_unlock_irq(&intfdata->susp_lock);
574 spin_lock_irq(&intfdata->susp_lock);
575 intfdata->suspended = 1;
576 spin_unlock_irq(&intfdata->susp_lock);
577 stop_read_write_urbs(serial);
581 EXPORT_SYMBOL(usb_wwan_suspend);
583 static void play_delayed(struct usb_serial_port *port)
585 struct usb_wwan_intf_private *data;
586 struct usb_wwan_port_private *portdata;
590 portdata = usb_get_serial_port_data(port);
591 data = port->serial->private;
592 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
593 err = usb_submit_urb(urb, GFP_ATOMIC);
599 int usb_wwan_resume(struct usb_serial *serial)
602 struct usb_serial_port *port;
603 struct usb_wwan_intf_private *intfdata = serial->private;
604 struct usb_wwan_port_private *portdata;
608 dbg("%s entered", __func__);
609 /* get the interrupt URBs resubmitted unconditionally */
610 for (i = 0; i < serial->num_ports; i++) {
611 port = serial->port[i];
612 if (!port->interrupt_in_urb) {
613 dbg("%s: No interrupt URB for port %d", __func__, i);
616 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
617 dbg("Submitted interrupt URB for port %d (result %d)", i, err);
619 err("%s: Error %d for interrupt URB of port%d",
625 for (i = 0; i < serial->num_ports; i++) {
627 port = serial->port[i];
628 portdata = usb_get_serial_port_data(port);
630 /* skip closed ports */
631 spin_lock_irq(&intfdata->susp_lock);
632 if (!portdata->opened) {
633 spin_unlock_irq(&intfdata->susp_lock);
637 for (j = 0; j < N_IN_URB; j++) {
638 urb = portdata->in_urbs[j];
639 err = usb_submit_urb(urb, GFP_ATOMIC);
641 err("%s: Error %d for bulk URB %d",
643 spin_unlock_irq(&intfdata->susp_lock);
648 spin_unlock_irq(&intfdata->susp_lock);
650 spin_lock_irq(&intfdata->susp_lock);
651 intfdata->suspended = 0;
652 spin_unlock_irq(&intfdata->susp_lock);
656 EXPORT_SYMBOL(usb_wwan_resume);
659 MODULE_AUTHOR(DRIVER_AUTHOR);
660 MODULE_DESCRIPTION(DRIVER_DESC);
661 MODULE_VERSION(DRIVER_VERSION);
662 MODULE_LICENSE("GPL");
664 module_param(debug, bool, S_IRUGO | S_IWUSR);
665 MODULE_PARM_DESC(debug, "Debug messages");