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>
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;
44 struct usb_wwan_intf_private *intfdata;
46 intfdata = port->serial->private;
48 if (!intfdata->send_setup)
51 portdata = usb_get_serial_port_data(port);
52 mutex_lock(&serial->disc_mutex);
53 portdata->rts_state = on;
54 portdata->dtr_state = on;
56 intfdata->send_setup(port);
57 mutex_unlock(&serial->disc_mutex);
59 EXPORT_SYMBOL(usb_wwan_dtr_rts);
61 void usb_wwan_set_termios(struct tty_struct *tty,
62 struct usb_serial_port *port,
63 struct ktermios *old_termios)
65 struct usb_wwan_intf_private *intfdata = port->serial->private;
67 /* Doesn't support option setting */
68 tty_termios_copy_hw(&tty->termios, old_termios);
70 if (intfdata->send_setup)
71 intfdata->send_setup(port);
73 EXPORT_SYMBOL(usb_wwan_set_termios);
75 int usb_wwan_tiocmget(struct tty_struct *tty)
77 struct usb_serial_port *port = tty->driver_data;
79 struct usb_wwan_port_private *portdata;
81 portdata = usb_get_serial_port_data(port);
83 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
84 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
85 ((portdata->cts_state) ? TIOCM_CTS : 0) |
86 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
87 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
88 ((portdata->ri_state) ? TIOCM_RNG : 0);
92 EXPORT_SYMBOL(usb_wwan_tiocmget);
94 int usb_wwan_tiocmset(struct tty_struct *tty,
95 unsigned int set, unsigned int clear)
97 struct usb_serial_port *port = tty->driver_data;
98 struct usb_wwan_port_private *portdata;
99 struct usb_wwan_intf_private *intfdata;
101 portdata = usb_get_serial_port_data(port);
102 intfdata = port->serial->private;
104 if (!intfdata->send_setup)
107 /* FIXME: what locks portdata fields ? */
109 portdata->rts_state = 1;
111 portdata->dtr_state = 1;
113 if (clear & TIOCM_RTS)
114 portdata->rts_state = 0;
115 if (clear & TIOCM_DTR)
116 portdata->dtr_state = 0;
117 return intfdata->send_setup(port);
119 EXPORT_SYMBOL(usb_wwan_tiocmset);
121 static int get_serial_info(struct usb_serial_port *port,
122 struct serial_struct __user *retinfo)
124 struct serial_struct tmp;
129 memset(&tmp, 0, sizeof(tmp));
130 tmp.line = port->serial->minor;
131 tmp.port = port->number;
132 tmp.baud_base = tty_get_baud_rate(port->port.tty);
133 tmp.close_delay = port->port.close_delay / 10;
134 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
135 ASYNC_CLOSING_WAIT_NONE :
136 port->port.closing_wait / 10;
138 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
143 static int set_serial_info(struct usb_serial_port *port,
144 struct serial_struct __user *newinfo)
146 struct serial_struct new_serial;
147 unsigned int closing_wait, close_delay;
150 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
153 close_delay = new_serial.close_delay * 10;
154 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
155 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
157 mutex_lock(&port->port.mutex);
159 if (!capable(CAP_SYS_ADMIN)) {
160 if ((close_delay != port->port.close_delay) ||
161 (closing_wait != port->port.closing_wait))
164 retval = -EOPNOTSUPP;
166 port->port.close_delay = close_delay;
167 port->port.closing_wait = closing_wait;
170 mutex_unlock(&port->port.mutex);
174 int usb_wwan_ioctl(struct tty_struct *tty,
175 unsigned int cmd, unsigned long arg)
177 struct usb_serial_port *port = tty->driver_data;
179 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
183 return get_serial_info(port,
184 (struct serial_struct __user *) arg);
186 return set_serial_info(port,
187 (struct serial_struct __user *) arg);
192 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
196 EXPORT_SYMBOL(usb_wwan_ioctl);
199 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
200 const unsigned char *buf, int count)
202 struct usb_wwan_port_private *portdata;
203 struct usb_wwan_intf_private *intfdata;
206 struct urb *this_urb = NULL; /* spurious */
210 portdata = usb_get_serial_port_data(port);
211 intfdata = port->serial->private;
213 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
217 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
219 if (todo > OUT_BUFLEN)
222 this_urb = portdata->out_urbs[i];
223 if (test_and_set_bit(i, &portdata->out_busy)) {
224 if (time_before(jiffies,
225 portdata->tx_start_time[i] + 10 * HZ))
227 usb_unlink_urb(this_urb);
230 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
231 usb_pipeendpoint(this_urb->pipe), i);
233 err = usb_autopm_get_interface_async(port->serial->interface);
238 memcpy(this_urb->transfer_buffer, buf, todo);
239 this_urb->transfer_buffer_length = todo;
241 spin_lock_irqsave(&intfdata->susp_lock, flags);
242 if (intfdata->suspended) {
243 usb_anchor_urb(this_urb, &portdata->delayed);
244 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
246 intfdata->in_flight++;
247 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
248 err = usb_submit_urb(this_urb, GFP_ATOMIC);
251 "usb_submit_urb %p (write bulk) failed (%d)\n",
253 clear_bit(i, &portdata->out_busy);
254 spin_lock_irqsave(&intfdata->susp_lock, flags);
255 intfdata->in_flight--;
256 spin_unlock_irqrestore(&intfdata->susp_lock,
258 usb_autopm_put_interface_async(port->serial->interface);
263 portdata->tx_start_time[i] = jiffies;
269 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
272 EXPORT_SYMBOL(usb_wwan_write);
274 static void usb_wwan_indat_callback(struct urb *urb)
278 struct usb_serial_port *port;
279 struct tty_struct *tty;
281 unsigned char *data = urb->transfer_buffer;
282 int status = urb->status;
284 endpoint = usb_pipeendpoint(urb->pipe);
289 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
290 __func__, status, endpoint);
292 tty = tty_port_tty_get(&port->port);
294 if (urb->actual_length) {
295 tty_insert_flip_string(tty, data,
297 tty_flip_buffer_push(tty);
299 dev_dbg(dev, "%s: empty read urb received\n", __func__);
303 /* Resubmit urb so we continue receiving */
304 err = usb_submit_urb(urb, GFP_ATOMIC);
307 dev_err(dev, "%s: resubmit read urb failed. (%d)\n", __func__, err);
308 /* busy also in error unless we are killed */
309 usb_mark_last_busy(port->serial->dev);
312 usb_mark_last_busy(port->serial->dev);
317 static void usb_wwan_outdat_callback(struct urb *urb)
319 struct usb_serial_port *port;
320 struct usb_wwan_port_private *portdata;
321 struct usb_wwan_intf_private *intfdata;
325 intfdata = port->serial->private;
327 usb_serial_port_softint(port);
328 usb_autopm_put_interface_async(port->serial->interface);
329 portdata = usb_get_serial_port_data(port);
330 spin_lock(&intfdata->susp_lock);
331 intfdata->in_flight--;
332 spin_unlock(&intfdata->susp_lock);
334 for (i = 0; i < N_OUT_URB; ++i) {
335 if (portdata->out_urbs[i] == urb) {
336 smp_mb__before_clear_bit();
337 clear_bit(i, &portdata->out_busy);
343 int usb_wwan_write_room(struct tty_struct *tty)
345 struct usb_serial_port *port = tty->driver_data;
346 struct usb_wwan_port_private *portdata;
349 struct urb *this_urb;
351 portdata = usb_get_serial_port_data(port);
353 for (i = 0; i < N_OUT_URB; i++) {
354 this_urb = portdata->out_urbs[i];
355 if (this_urb && !test_bit(i, &portdata->out_busy))
356 data_len += OUT_BUFLEN;
359 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
362 EXPORT_SYMBOL(usb_wwan_write_room);
364 int usb_wwan_chars_in_buffer(struct tty_struct *tty)
366 struct usb_serial_port *port = tty->driver_data;
367 struct usb_wwan_port_private *portdata;
370 struct urb *this_urb;
372 portdata = usb_get_serial_port_data(port);
374 for (i = 0; i < N_OUT_URB; i++) {
375 this_urb = portdata->out_urbs[i];
376 /* FIXME: This locking is insufficient as this_urb may
377 go unused during the test */
378 if (this_urb && test_bit(i, &portdata->out_busy))
379 data_len += this_urb->transfer_buffer_length;
381 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
384 EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
386 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
388 struct usb_wwan_port_private *portdata;
389 struct usb_wwan_intf_private *intfdata;
390 struct usb_serial *serial = port->serial;
394 portdata = usb_get_serial_port_data(port);
395 intfdata = serial->private;
397 /* Start reading from the IN endpoint */
398 for (i = 0; i < N_IN_URB; i++) {
399 urb = portdata->in_urbs[i];
402 err = usb_submit_urb(urb, GFP_KERNEL);
404 dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n",
405 __func__, i, err, urb->transfer_buffer_length);
409 if (intfdata->send_setup)
410 intfdata->send_setup(port);
412 serial->interface->needs_remote_wakeup = 1;
413 spin_lock_irq(&intfdata->susp_lock);
414 portdata->opened = 1;
415 spin_unlock_irq(&intfdata->susp_lock);
416 /* this balances a get in the generic USB serial code */
417 usb_autopm_put_interface(serial->interface);
421 EXPORT_SYMBOL(usb_wwan_open);
423 void usb_wwan_close(struct usb_serial_port *port)
426 struct usb_serial *serial = port->serial;
427 struct usb_wwan_port_private *portdata;
428 struct usb_wwan_intf_private *intfdata = port->serial->private;
430 portdata = usb_get_serial_port_data(port);
433 /* Stop reading/writing urbs */
434 spin_lock_irq(&intfdata->susp_lock);
435 portdata->opened = 0;
436 spin_unlock_irq(&intfdata->susp_lock);
438 for (i = 0; i < N_IN_URB; i++)
439 usb_kill_urb(portdata->in_urbs[i]);
440 for (i = 0; i < N_OUT_URB; i++)
441 usb_kill_urb(portdata->out_urbs[i]);
442 /* balancing - important as an error cannot be handled*/
443 usb_autopm_get_interface_no_resume(serial->interface);
444 serial->interface->needs_remote_wakeup = 0;
447 EXPORT_SYMBOL(usb_wwan_close);
449 /* Helper functions used by usb_wwan_setup_urbs */
450 static struct urb *usb_wwan_setup_urb(struct usb_serial *serial, int endpoint,
451 int dir, void *ctx, char *buf, int len,
452 void (*callback) (struct urb *))
457 return NULL; /* endpoint not needed */
459 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
461 dev_dbg(&serial->interface->dev,
462 "%s: alloc for endpoint %d failed.\n", __func__,
467 /* Fill URB using supplied data. */
468 usb_fill_bulk_urb(urb, serial->dev,
469 usb_sndbulkpipe(serial->dev, endpoint) | dir,
470 buf, len, callback, ctx);
476 static void usb_wwan_setup_urbs(struct usb_serial *serial)
479 struct usb_serial_port *port;
480 struct usb_wwan_port_private *portdata;
482 for (i = 0; i < serial->num_ports; i++) {
483 port = serial->port[i];
484 portdata = usb_get_serial_port_data(port);
486 /* Do indat endpoints first */
487 for (j = 0; j < N_IN_URB; ++j) {
488 portdata->in_urbs[j] = usb_wwan_setup_urb(serial,
490 bulk_in_endpointAddress,
496 usb_wwan_indat_callback);
499 /* outdat endpoints */
500 for (j = 0; j < N_OUT_URB; ++j) {
501 portdata->out_urbs[j] = usb_wwan_setup_urb(serial,
503 bulk_out_endpointAddress,
510 usb_wwan_outdat_callback);
515 int usb_wwan_startup(struct usb_serial *serial)
518 struct usb_serial_port *port;
519 struct usb_wwan_port_private *portdata;
522 /* Now setup per port private data */
523 for (i = 0; i < serial->num_ports; i++) {
524 port = serial->port[i];
525 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
527 dev_dbg(&port->dev, "%s: kmalloc for usb_wwan_port_private (%d) failed!.\n",
531 init_usb_anchor(&portdata->delayed);
533 for (j = 0; j < N_IN_URB; j++) {
534 buffer = (u8 *) __get_free_page(GFP_KERNEL);
537 portdata->in_buffer[j] = buffer;
540 for (j = 0; j < N_OUT_URB; j++) {
541 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
543 goto bail_out_error2;
544 portdata->out_buffer[j] = buffer;
547 usb_set_serial_port_data(port, portdata);
549 if (!port->interrupt_in_urb)
551 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
553 dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
556 usb_wwan_setup_urbs(serial);
560 for (j = 0; j < N_OUT_URB; j++)
561 kfree(portdata->out_buffer[j]);
563 for (j = 0; j < N_IN_URB; j++)
564 if (portdata->in_buffer[j])
565 free_page((unsigned long)portdata->in_buffer[j]);
569 EXPORT_SYMBOL(usb_wwan_startup);
571 int usb_wwan_port_remove(struct usb_serial_port *port)
574 struct usb_wwan_port_private *portdata;
576 portdata = usb_get_serial_port_data(port);
577 usb_set_serial_port_data(port, NULL);
579 /* Stop reading/writing urbs and free them */
580 for (i = 0; i < N_IN_URB; i++) {
581 usb_kill_urb(portdata->in_urbs[i]);
582 usb_free_urb(portdata->in_urbs[i]);
583 free_page((unsigned long)portdata->in_buffer[i]);
585 for (i = 0; i < N_OUT_URB; i++) {
586 usb_kill_urb(portdata->out_urbs[i]);
587 usb_free_urb(portdata->out_urbs[i]);
588 kfree(portdata->out_buffer[i]);
591 /* Now free port private data */
595 EXPORT_SYMBOL(usb_wwan_port_remove);
598 static void stop_read_write_urbs(struct usb_serial *serial)
601 struct usb_serial_port *port;
602 struct usb_wwan_port_private *portdata;
604 /* Stop reading/writing urbs */
605 for (i = 0; i < serial->num_ports; ++i) {
606 port = serial->port[i];
607 portdata = usb_get_serial_port_data(port);
610 for (j = 0; j < N_IN_URB; j++)
611 usb_kill_urb(portdata->in_urbs[j]);
612 for (j = 0; j < N_OUT_URB; j++)
613 usb_kill_urb(portdata->out_urbs[j]);
617 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
619 struct usb_wwan_intf_private *intfdata = serial->private;
622 if (PMSG_IS_AUTO(message)) {
623 spin_lock_irq(&intfdata->susp_lock);
624 b = intfdata->in_flight;
625 spin_unlock_irq(&intfdata->susp_lock);
631 spin_lock_irq(&intfdata->susp_lock);
632 intfdata->suspended = 1;
633 spin_unlock_irq(&intfdata->susp_lock);
634 stop_read_write_urbs(serial);
638 EXPORT_SYMBOL(usb_wwan_suspend);
640 static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
644 for (i = 0; i < N_OUT_URB; i++) {
645 if (urb == portdata->out_urbs[i]) {
646 clear_bit(i, &portdata->out_busy);
652 static void play_delayed(struct usb_serial_port *port)
654 struct usb_wwan_intf_private *data;
655 struct usb_wwan_port_private *portdata;
659 portdata = usb_get_serial_port_data(port);
660 data = port->serial->private;
661 while ((urb = usb_get_from_anchor(&portdata->delayed))) {
662 err = usb_submit_urb(urb, GFP_ATOMIC);
666 /* we have to throw away the rest */
668 unbusy_queued_urb(urb, portdata);
669 usb_autopm_put_interface_no_suspend(port->serial->interface);
670 } while ((urb = usb_get_from_anchor(&portdata->delayed)));
676 int usb_wwan_resume(struct usb_serial *serial)
679 struct usb_serial_port *port;
680 struct usb_wwan_intf_private *intfdata = serial->private;
681 struct usb_wwan_port_private *portdata;
685 /* get the interrupt URBs resubmitted unconditionally */
686 for (i = 0; i < serial->num_ports; i++) {
687 port = serial->port[i];
688 if (!port->interrupt_in_urb) {
689 dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__);
692 err = usb_submit_urb(port->interrupt_in_urb, GFP_NOIO);
693 dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err);
695 dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
701 for (i = 0; i < serial->num_ports; i++) {
703 port = serial->port[i];
704 portdata = usb_get_serial_port_data(port);
706 /* skip closed ports */
707 spin_lock_irq(&intfdata->susp_lock);
708 if (!portdata || !portdata->opened) {
709 spin_unlock_irq(&intfdata->susp_lock);
713 for (j = 0; j < N_IN_URB; j++) {
714 urb = portdata->in_urbs[j];
715 err = usb_submit_urb(urb, GFP_ATOMIC);
717 dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
719 spin_unlock_irq(&intfdata->susp_lock);
724 spin_unlock_irq(&intfdata->susp_lock);
726 spin_lock_irq(&intfdata->susp_lock);
727 intfdata->suspended = 0;
728 spin_unlock_irq(&intfdata->susp_lock);
732 EXPORT_SYMBOL(usb_wwan_resume);
735 MODULE_AUTHOR(DRIVER_AUTHOR);
736 MODULE_DESCRIPTION(DRIVER_DESC);
737 MODULE_VERSION(DRIVER_VERSION);
738 MODULE_LICENSE("GPL");