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>
39 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
41 struct usb_serial *serial = port->serial;
42 struct usb_wwan_port_private *portdata;
43 struct usb_wwan_intf_private *intfdata;
45 intfdata = port->serial->private;
47 if (!intfdata->send_setup)
50 portdata = usb_get_serial_port_data(port);
51 mutex_lock(&serial->disc_mutex);
52 portdata->rts_state = on;
53 portdata->dtr_state = on;
55 intfdata->send_setup(port);
56 mutex_unlock(&serial->disc_mutex);
58 EXPORT_SYMBOL(usb_wwan_dtr_rts);
60 void usb_wwan_set_termios(struct tty_struct *tty,
61 struct usb_serial_port *port,
62 struct ktermios *old_termios)
64 struct usb_wwan_intf_private *intfdata = port->serial->private;
66 /* Doesn't support option setting */
67 tty_termios_copy_hw(&tty->termios, old_termios);
69 if (intfdata->send_setup)
70 intfdata->send_setup(port);
72 EXPORT_SYMBOL(usb_wwan_set_termios);
74 int usb_wwan_tiocmget(struct tty_struct *tty)
76 struct usb_serial_port *port = tty->driver_data;
78 struct usb_wwan_port_private *portdata;
80 portdata = usb_get_serial_port_data(port);
82 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
83 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
84 ((portdata->cts_state) ? TIOCM_CTS : 0) |
85 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
86 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
87 ((portdata->ri_state) ? TIOCM_RNG : 0);
91 EXPORT_SYMBOL(usb_wwan_tiocmget);
93 int usb_wwan_tiocmset(struct tty_struct *tty,
94 unsigned int set, unsigned int clear)
96 struct usb_serial_port *port = tty->driver_data;
97 struct usb_wwan_port_private *portdata;
98 struct usb_wwan_intf_private *intfdata;
100 portdata = usb_get_serial_port_data(port);
101 intfdata = port->serial->private;
103 if (!intfdata->send_setup)
106 /* FIXME: what locks portdata fields ? */
108 portdata->rts_state = 1;
110 portdata->dtr_state = 1;
112 if (clear & TIOCM_RTS)
113 portdata->rts_state = 0;
114 if (clear & TIOCM_DTR)
115 portdata->dtr_state = 0;
116 return intfdata->send_setup(port);
118 EXPORT_SYMBOL(usb_wwan_tiocmset);
120 static int get_serial_info(struct usb_serial_port *port,
121 struct serial_struct __user *retinfo)
123 struct serial_struct tmp;
128 memset(&tmp, 0, sizeof(tmp));
129 tmp.line = port->serial->minor;
130 tmp.port = port->number;
131 tmp.baud_base = tty_get_baud_rate(port->port.tty);
132 tmp.close_delay = port->port.close_delay / 10;
133 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
134 ASYNC_CLOSING_WAIT_NONE :
135 port->port.closing_wait / 10;
137 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
142 static int set_serial_info(struct usb_serial_port *port,
143 struct serial_struct __user *newinfo)
145 struct serial_struct new_serial;
146 unsigned int closing_wait, close_delay;
149 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
152 close_delay = new_serial.close_delay * 10;
153 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
154 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
156 mutex_lock(&port->port.mutex);
158 if (!capable(CAP_SYS_ADMIN)) {
159 if ((close_delay != port->port.close_delay) ||
160 (closing_wait != port->port.closing_wait))
163 retval = -EOPNOTSUPP;
165 port->port.close_delay = close_delay;
166 port->port.closing_wait = closing_wait;
169 mutex_unlock(&port->port.mutex);
173 int usb_wwan_ioctl(struct tty_struct *tty,
174 unsigned int cmd, unsigned long arg)
176 struct usb_serial_port *port = tty->driver_data;
178 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
182 return get_serial_info(port,
183 (struct serial_struct __user *) arg);
185 return set_serial_info(port,
186 (struct serial_struct __user *) arg);
191 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
195 EXPORT_SYMBOL(usb_wwan_ioctl);
198 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
199 const unsigned char *buf, int count)
201 struct usb_wwan_port_private *portdata;
202 struct usb_wwan_intf_private *intfdata;
205 struct urb *this_urb = NULL; /* spurious */
209 portdata = usb_get_serial_port_data(port);
210 intfdata = port->serial->private;
212 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
216 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
218 if (todo > OUT_BUFLEN)
221 this_urb = portdata->out_urbs[i];
222 if (test_and_set_bit(i, &portdata->out_busy)) {
223 if (time_before(jiffies,
224 portdata->tx_start_time[i] + 10 * HZ))
226 usb_unlink_urb(this_urb);
229 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
230 usb_pipeendpoint(this_urb->pipe), i);
232 err = usb_autopm_get_interface_async(port->serial->interface);
237 memcpy(this_urb->transfer_buffer, buf, todo);
238 this_urb->transfer_buffer_length = todo;
240 spin_lock_irqsave(&intfdata->susp_lock, flags);
241 if (intfdata->suspended) {
242 usb_anchor_urb(this_urb, &portdata->delayed);
243 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
245 intfdata->in_flight++;
246 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
247 err = usb_submit_urb(this_urb, GFP_ATOMIC);
250 "usb_submit_urb %p (write bulk) failed (%d)\n",
252 clear_bit(i, &portdata->out_busy);
253 spin_lock_irqsave(&intfdata->susp_lock, flags);
254 intfdata->in_flight--;
255 spin_unlock_irqrestore(&intfdata->susp_lock,
257 usb_autopm_put_interface_async(port->serial->interface);
262 portdata->tx_start_time[i] = jiffies;
268 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
271 EXPORT_SYMBOL(usb_wwan_write);
273 static void usb_wwan_indat_callback(struct urb *urb)
277 struct usb_serial_port *port;
278 struct tty_struct *tty;
280 unsigned char *data = urb->transfer_buffer;
281 int status = urb->status;
283 endpoint = usb_pipeendpoint(urb->pipe);
288 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
289 __func__, status, endpoint);
291 tty = tty_port_tty_get(&port->port);
293 if (urb->actual_length) {
294 tty_insert_flip_string(tty, data,
296 tty_flip_buffer_push(tty);
298 dev_dbg(dev, "%s: empty read urb received\n", __func__);
302 /* Resubmit urb so we continue receiving */
303 err = usb_submit_urb(urb, GFP_ATOMIC);
306 dev_err(dev, "%s: resubmit read urb failed. (%d)\n", __func__, err);
307 /* busy also in error unless we are killed */
308 usb_mark_last_busy(port->serial->dev);
311 usb_mark_last_busy(port->serial->dev);
316 static void usb_wwan_outdat_callback(struct urb *urb)
318 struct usb_serial_port *port;
319 struct usb_wwan_port_private *portdata;
320 struct usb_wwan_intf_private *intfdata;
324 intfdata = port->serial->private;
326 usb_serial_port_softint(port);
327 usb_autopm_put_interface_async(port->serial->interface);
328 portdata = usb_get_serial_port_data(port);
329 spin_lock(&intfdata->susp_lock);
330 intfdata->in_flight--;
331 spin_unlock(&intfdata->susp_lock);
333 for (i = 0; i < N_OUT_URB; ++i) {
334 if (portdata->out_urbs[i] == urb) {
335 smp_mb__before_clear_bit();
336 clear_bit(i, &portdata->out_busy);
342 int usb_wwan_write_room(struct tty_struct *tty)
344 struct usb_serial_port *port = tty->driver_data;
345 struct usb_wwan_port_private *portdata;
348 struct urb *this_urb;
350 portdata = usb_get_serial_port_data(port);
352 for (i = 0; i < N_OUT_URB; i++) {
353 this_urb = portdata->out_urbs[i];
354 if (this_urb && !test_bit(i, &portdata->out_busy))
355 data_len += OUT_BUFLEN;
358 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
361 EXPORT_SYMBOL(usb_wwan_write_room);
363 int usb_wwan_chars_in_buffer(struct tty_struct *tty)
365 struct usb_serial_port *port = tty->driver_data;
366 struct usb_wwan_port_private *portdata;
369 struct urb *this_urb;
371 portdata = usb_get_serial_port_data(port);
373 for (i = 0; i < N_OUT_URB; i++) {
374 this_urb = portdata->out_urbs[i];
375 /* FIXME: This locking is insufficient as this_urb may
376 go unused during the test */
377 if (this_urb && test_bit(i, &portdata->out_busy))
378 data_len += this_urb->transfer_buffer_length;
380 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
383 EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
385 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
387 struct usb_wwan_port_private *portdata;
388 struct usb_wwan_intf_private *intfdata;
389 struct usb_serial *serial = port->serial;
393 portdata = usb_get_serial_port_data(port);
394 intfdata = serial->private;
396 /* Start reading from the IN endpoint */
397 for (i = 0; i < N_IN_URB; i++) {
398 urb = portdata->in_urbs[i];
401 err = usb_submit_urb(urb, GFP_KERNEL);
403 dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n",
404 __func__, i, err, urb->transfer_buffer_length);
408 if (intfdata->send_setup)
409 intfdata->send_setup(port);
411 serial->interface->needs_remote_wakeup = 1;
412 spin_lock_irq(&intfdata->susp_lock);
413 portdata->opened = 1;
414 spin_unlock_irq(&intfdata->susp_lock);
415 /* this balances a get in the generic USB serial code */
416 usb_autopm_put_interface(serial->interface);
420 EXPORT_SYMBOL(usb_wwan_open);
422 void usb_wwan_close(struct usb_serial_port *port)
425 struct usb_serial *serial = port->serial;
426 struct usb_wwan_port_private *portdata;
427 struct usb_wwan_intf_private *intfdata = port->serial->private;
429 portdata = usb_get_serial_port_data(port);
432 /* Stop reading/writing urbs */
433 spin_lock_irq(&intfdata->susp_lock);
434 portdata->opened = 0;
435 spin_unlock_irq(&intfdata->susp_lock);
437 for (i = 0; i < N_IN_URB; i++)
438 usb_kill_urb(portdata->in_urbs[i]);
439 for (i = 0; i < N_OUT_URB; i++)
440 usb_kill_urb(portdata->out_urbs[i]);
441 /* balancing - important as an error cannot be handled*/
442 usb_autopm_get_interface_no_resume(serial->interface);
443 serial->interface->needs_remote_wakeup = 0;
446 EXPORT_SYMBOL(usb_wwan_close);
448 /* Helper functions used by usb_wwan_setup_urbs */
449 static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
451 int dir, void *ctx, char *buf, int len,
452 void (*callback) (struct urb *))
454 struct usb_serial *serial = port->serial;
457 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
459 dev_dbg(&serial->interface->dev,
460 "%s: alloc for endpoint %d failed.\n", __func__,
465 /* Fill URB using supplied data. */
466 usb_fill_bulk_urb(urb, serial->dev,
467 usb_sndbulkpipe(serial->dev, endpoint) | dir,
468 buf, len, callback, ctx);
473 int usb_wwan_port_probe(struct usb_serial_port *port)
475 struct usb_wwan_port_private *portdata;
481 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
485 init_usb_anchor(&portdata->delayed);
487 for (i = 0; i < N_IN_URB; i++) {
488 if (!port->bulk_in_size)
491 buffer = (u8 *)__get_free_page(GFP_KERNEL);
494 portdata->in_buffer[i] = buffer;
496 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
499 usb_wwan_indat_callback);
500 portdata->in_urbs[i] = urb;
503 for (i = 0; i < N_OUT_URB; i++) {
504 if (!port->bulk_out_size)
507 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
509 goto bail_out_error2;
510 portdata->out_buffer[i] = buffer;
512 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
515 usb_wwan_outdat_callback);
516 portdata->out_urbs[i] = urb;
519 usb_set_serial_port_data(port, portdata);
521 if (port->interrupt_in_urb) {
522 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
524 dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
531 for (i = 0; i < N_OUT_URB; i++) {
532 usb_free_urb(portdata->out_urbs[i]);
533 kfree(portdata->out_buffer[i]);
536 for (i = 0; i < N_IN_URB; i++) {
537 usb_free_urb(portdata->in_urbs[i]);
538 free_page((unsigned long)portdata->in_buffer[i]);
544 EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
546 int usb_wwan_port_remove(struct usb_serial_port *port)
549 struct usb_wwan_port_private *portdata;
551 portdata = usb_get_serial_port_data(port);
552 usb_set_serial_port_data(port, NULL);
554 /* Stop reading/writing urbs and free them */
555 for (i = 0; i < N_IN_URB; i++) {
556 usb_kill_urb(portdata->in_urbs[i]);
557 usb_free_urb(portdata->in_urbs[i]);
558 free_page((unsigned long)portdata->in_buffer[i]);
560 for (i = 0; i < N_OUT_URB; i++) {
561 usb_kill_urb(portdata->out_urbs[i]);
562 usb_free_urb(portdata->out_urbs[i]);
563 kfree(portdata->out_buffer[i]);
566 /* Now free port private data */
570 EXPORT_SYMBOL(usb_wwan_port_remove);
573 static void stop_read_write_urbs(struct usb_serial *serial)
576 struct usb_serial_port *port;
577 struct usb_wwan_port_private *portdata;
579 /* Stop reading/writing urbs */
580 for (i = 0; i < serial->num_ports; ++i) {
581 port = serial->port[i];
582 portdata = usb_get_serial_port_data(port);
585 for (j = 0; j < N_IN_URB; j++)
586 usb_kill_urb(portdata->in_urbs[j]);
587 for (j = 0; j < N_OUT_URB; j++)
588 usb_kill_urb(portdata->out_urbs[j]);
592 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
594 struct usb_wwan_intf_private *intfdata = serial->private;
597 if (PMSG_IS_AUTO(message)) {
598 spin_lock_irq(&intfdata->susp_lock);
599 b = intfdata->in_flight;
600 spin_unlock_irq(&intfdata->susp_lock);
606 spin_lock_irq(&intfdata->susp_lock);
607 intfdata->suspended = 1;
608 spin_unlock_irq(&intfdata->susp_lock);
609 stop_read_write_urbs(serial);
613 EXPORT_SYMBOL(usb_wwan_suspend);
615 static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
619 for (i = 0; i < N_OUT_URB; i++) {
620 if (urb == portdata->out_urbs[i]) {
621 clear_bit(i, &portdata->out_busy);
627 static void play_delayed(struct usb_serial_port *port)
629 struct usb_wwan_intf_private *data;
630 struct usb_wwan_port_private *portdata;
634 portdata = usb_get_serial_port_data(port);
635 data = port->serial->private;
636 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
637 err = usb_submit_urb(urb, GFP_ATOMIC);
641 /* we have to throw away the rest */
643 unbusy_queued_urb(urb, portdata);
644 usb_autopm_put_interface_no_suspend(port->serial->interface);
645 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
651 int usb_wwan_resume(struct usb_serial *serial)
654 struct usb_serial_port *port;
655 struct usb_wwan_intf_private *intfdata = serial->private;
656 struct usb_wwan_port_private *portdata;
660 /* get the interrupt URBs resubmitted unconditionally */
661 for (i = 0; i < serial->num_ports; i++) {
662 port = serial->port[i];
663 if (!port->interrupt_in_urb) {
664 dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__);
667 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
668 dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err);
670 dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
676 for (i = 0; i < serial->num_ports; i++) {
678 port = serial->port[i];
679 portdata = usb_get_serial_port_data(port);
681 /* skip closed ports */
682 spin_lock_irq(&intfdata->susp_lock);
683 if (!portdata || !portdata->opened) {
684 spin_unlock_irq(&intfdata->susp_lock);
688 for (j = 0; j < N_IN_URB; j++) {
689 urb = portdata->in_urbs[j];
690 err = usb_submit_urb(urb, GFP_ATOMIC);
692 dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
694 spin_unlock_irq(&intfdata->susp_lock);
699 spin_unlock_irq(&intfdata->susp_lock);
701 spin_lock_irq(&intfdata->susp_lock);
702 intfdata->suspended = 0;
703 spin_unlock_irq(&intfdata->susp_lock);
707 EXPORT_SYMBOL(usb_wwan_resume);
710 MODULE_AUTHOR(DRIVER_AUTHOR);
711 MODULE_DESCRIPTION(DRIVER_DESC);
712 MODULE_LICENSE("GPL");