Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
usb_wwan.c
Go to the documentation of this file.
1 /*
2  USB Driver layer for GSM modems
3 
4  Copyright (C) 2005 Matthias Urlichs <[email protected]>
5 
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.
9 
10  Portions copied from the Keyspan driver by Hugh Blemings <[email protected]>
11 
12  History: see the git log.
13 
14  Work sponsored by: Sigos GmbH, Germany <[email protected]>
15 
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
20 */
21 
22 #define DRIVER_VERSION "v0.7.2"
23 #define DRIVER_AUTHOR "Matthias Urlichs <[email protected]>"
24 #define DRIVER_DESC "USB Driver for GSM modems"
25 
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>
38 #include "usb-wwan.h"
39 
40 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
41 {
42  struct usb_serial *serial = port->serial;
43  struct usb_wwan_port_private *portdata;
44  struct usb_wwan_intf_private *intfdata;
45 
46  intfdata = port->serial->private;
47 
48  if (!intfdata->send_setup)
49  return;
50 
51  portdata = usb_get_serial_port_data(port);
52  mutex_lock(&serial->disc_mutex);
53  portdata->rts_state = on;
54  portdata->dtr_state = on;
55  if (serial->dev)
56  intfdata->send_setup(port);
57  mutex_unlock(&serial->disc_mutex);
58 }
60 
62  struct usb_serial_port *port,
63  struct ktermios *old_termios)
64 {
65  struct usb_wwan_intf_private *intfdata = port->serial->private;
66 
67  /* Doesn't support option setting */
68  tty_termios_copy_hw(&tty->termios, old_termios);
69 
70  if (intfdata->send_setup)
71  intfdata->send_setup(port);
72 }
74 
75 int usb_wwan_tiocmget(struct tty_struct *tty)
76 {
77  struct usb_serial_port *port = tty->driver_data;
78  unsigned int value;
79  struct usb_wwan_port_private *portdata;
80 
81  portdata = usb_get_serial_port_data(port);
82 
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);
89 
90  return value;
91 }
93 
94 int usb_wwan_tiocmset(struct tty_struct *tty,
95  unsigned int set, unsigned int clear)
96 {
97  struct usb_serial_port *port = tty->driver_data;
98  struct usb_wwan_port_private *portdata;
99  struct usb_wwan_intf_private *intfdata;
100 
101  portdata = usb_get_serial_port_data(port);
102  intfdata = port->serial->private;
103 
104  if (!intfdata->send_setup)
105  return -EINVAL;
106 
107  /* FIXME: what locks portdata fields ? */
108  if (set & TIOCM_RTS)
109  portdata->rts_state = 1;
110  if (set & TIOCM_DTR)
111  portdata->dtr_state = 1;
112 
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);
118 }
120 
121 static int get_serial_info(struct usb_serial_port *port,
122  struct serial_struct __user *retinfo)
123 {
124  struct serial_struct tmp;
125 
126  if (!retinfo)
127  return -EFAULT;
128 
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 ?
136  port->port.closing_wait / 10;
137 
138  if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
139  return -EFAULT;
140  return 0;
141 }
142 
143 static int set_serial_info(struct usb_serial_port *port,
144  struct serial_struct __user *newinfo)
145 {
146  struct serial_struct new_serial;
147  unsigned int closing_wait, close_delay;
148  int retval = 0;
149 
150  if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
151  return -EFAULT;
152 
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;
156 
157  mutex_lock(&port->port.mutex);
158 
159  if (!capable(CAP_SYS_ADMIN)) {
160  if ((close_delay != port->port.close_delay) ||
161  (closing_wait != port->port.closing_wait))
162  retval = -EPERM;
163  else
164  retval = -EOPNOTSUPP;
165  } else {
166  port->port.close_delay = close_delay;
167  port->port.closing_wait = closing_wait;
168  }
169 
170  mutex_unlock(&port->port.mutex);
171  return retval;
172 }
173 
174 int usb_wwan_ioctl(struct tty_struct *tty,
175  unsigned int cmd, unsigned long arg)
176 {
177  struct usb_serial_port *port = tty->driver_data;
178 
179  dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
180 
181  switch (cmd) {
182  case TIOCGSERIAL:
183  return get_serial_info(port,
184  (struct serial_struct __user *) arg);
185  case TIOCSSERIAL:
186  return set_serial_info(port,
187  (struct serial_struct __user *) arg);
188  default:
189  break;
190  }
191 
192  dev_dbg(&port->dev, "%s arg not supported\n", __func__);
193 
194  return -ENOIOCTLCMD;
195 }
197 
198 /* Write */
199 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
200  const unsigned char *buf, int count)
201 {
202  struct usb_wwan_port_private *portdata;
203  struct usb_wwan_intf_private *intfdata;
204  int i;
205  int left, todo;
206  struct urb *this_urb = NULL; /* spurious */
207  int err;
208  unsigned long flags;
209 
210  portdata = usb_get_serial_port_data(port);
211  intfdata = port->serial->private;
212 
213  dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
214 
215  i = 0;
216  left = count;
217  for (i = 0; left > 0 && i < N_OUT_URB; i++) {
218  todo = left;
219  if (todo > OUT_BUFLEN)
220  todo = OUT_BUFLEN;
221 
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))
226  continue;
227  usb_unlink_urb(this_urb);
228  continue;
229  }
230  dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
231  usb_pipeendpoint(this_urb->pipe), i);
232 
233  err = usb_autopm_get_interface_async(port->serial->interface);
234  if (err < 0)
235  break;
236 
237  /* send the data */
238  memcpy(this_urb->transfer_buffer, buf, todo);
239  this_urb->transfer_buffer_length = todo;
240 
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);
245  } else {
246  intfdata->in_flight++;
247  spin_unlock_irqrestore(&intfdata->susp_lock, flags);
248  err = usb_submit_urb(this_urb, GFP_ATOMIC);
249  if (err) {
250  dev_dbg(&port->dev,
251  "usb_submit_urb %p (write bulk) failed (%d)\n",
252  this_urb, err);
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,
257  flags);
258  usb_autopm_put_interface_async(port->serial->interface);
259  break;
260  }
261  }
262 
263  portdata->tx_start_time[i] = jiffies;
264  buf += todo;
265  left -= todo;
266  }
267 
268  count -= left;
269  dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
270  return count;
271 }
273 
274 static void usb_wwan_indat_callback(struct urb *urb)
275 {
276  int err;
277  int endpoint;
278  struct usb_serial_port *port;
279  struct tty_struct *tty;
280  struct device *dev;
281  unsigned char *data = urb->transfer_buffer;
282  int status = urb->status;
283 
284  endpoint = usb_pipeendpoint(urb->pipe);
285  port = urb->context;
286  dev = &port->dev;
287 
288  if (status) {
289  dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
290  __func__, status, endpoint);
291  } else {
292  tty = tty_port_tty_get(&port->port);
293  if (tty) {
294  if (urb->actual_length) {
295  tty_insert_flip_string(tty, data,
296  urb->actual_length);
298  } else
299  dev_dbg(dev, "%s: empty read urb received\n", __func__);
300  tty_kref_put(tty);
301  }
302 
303  /* Resubmit urb so we continue receiving */
304  err = usb_submit_urb(urb, GFP_ATOMIC);
305  if (err) {
306  if (err != -EPERM) {
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);
310  }
311  } else {
312  usb_mark_last_busy(port->serial->dev);
313  }
314  }
315 }
316 
317 static void usb_wwan_outdat_callback(struct urb *urb)
318 {
319  struct usb_serial_port *port;
320  struct usb_wwan_port_private *portdata;
321  struct usb_wwan_intf_private *intfdata;
322  int i;
323 
324  port = urb->context;
325  intfdata = port->serial->private;
326 
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);
333 
334  for (i = 0; i < N_OUT_URB; ++i) {
335  if (portdata->out_urbs[i] == urb) {
337  clear_bit(i, &portdata->out_busy);
338  break;
339  }
340  }
341 }
342 
344 {
345  struct usb_serial_port *port = tty->driver_data;
346  struct usb_wwan_port_private *portdata;
347  int i;
348  int data_len = 0;
349  struct urb *this_urb;
350 
351  portdata = usb_get_serial_port_data(port);
352 
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;
357  }
358 
359  dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
360  return data_len;
361 }
363 
365 {
366  struct usb_serial_port *port = tty->driver_data;
367  struct usb_wwan_port_private *portdata;
368  int i;
369  int data_len = 0;
370  struct urb *this_urb;
371 
372  portdata = usb_get_serial_port_data(port);
373 
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;
380  }
381  dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
382  return data_len;
383 }
385 
386 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
387 {
388  struct usb_wwan_port_private *portdata;
389  struct usb_wwan_intf_private *intfdata;
390  struct usb_serial *serial = port->serial;
391  int i, err;
392  struct urb *urb;
393 
394  portdata = usb_get_serial_port_data(port);
395  intfdata = serial->private;
396 
397  /* Start reading from the IN endpoint */
398  for (i = 0; i < N_IN_URB; i++) {
399  urb = portdata->in_urbs[i];
400  if (!urb)
401  continue;
402  err = usb_submit_urb(urb, GFP_KERNEL);
403  if (err) {
404  dev_dbg(&port->dev, "%s: submit urb %d failed (%d) %d\n",
405  __func__, i, err, urb->transfer_buffer_length);
406  }
407  }
408 
409  if (intfdata->send_setup)
410  intfdata->send_setup(port);
411 
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);
418 
419  return 0;
420 }
422 
423 void usb_wwan_close(struct usb_serial_port *port)
424 {
425  int i;
426  struct usb_serial *serial = port->serial;
427  struct usb_wwan_port_private *portdata;
428  struct usb_wwan_intf_private *intfdata = port->serial->private;
429 
430  portdata = usb_get_serial_port_data(port);
431 
432  if (serial->dev) {
433  /* Stop reading/writing urbs */
434  spin_lock_irq(&intfdata->susp_lock);
435  portdata->opened = 0;
436  spin_unlock_irq(&intfdata->susp_lock);
437 
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;
445  }
446 }
448 
449 /* Helper functions used by usb_wwan_setup_urbs */
450 static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
451  int endpoint,
452  int dir, void *ctx, char *buf, int len,
453  void (*callback) (struct urb *))
454 {
455  struct usb_serial *serial = port->serial;
456  struct urb *urb;
457 
458  urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
459  if (urb == NULL) {
460  dev_dbg(&serial->interface->dev,
461  "%s: alloc for endpoint %d failed.\n", __func__,
462  endpoint);
463  return NULL;
464  }
465 
466  /* Fill URB using supplied data. */
467  usb_fill_bulk_urb(urb, serial->dev,
468  usb_sndbulkpipe(serial->dev, endpoint) | dir,
469  buf, len, callback, ctx);
470 
471  return urb;
472 }
473 
475 {
476  struct usb_wwan_port_private *portdata;
477  struct urb *urb;
478  u8 *buffer;
479  int err;
480  int i;
481 
482  portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
483  if (!portdata)
484  return -ENOMEM;
485 
486  init_usb_anchor(&portdata->delayed);
487 
488  for (i = 0; i < N_IN_URB; i++) {
489  if (!port->bulk_in_size)
490  break;
491 
492  buffer = (u8 *)__get_free_page(GFP_KERNEL);
493  if (!buffer)
494  goto bail_out_error;
495  portdata->in_buffer[i] = buffer;
496 
497  urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
498  USB_DIR_IN, port,
499  buffer, IN_BUFLEN,
500  usb_wwan_indat_callback);
501  portdata->in_urbs[i] = urb;
502  }
503 
504  for (i = 0; i < N_OUT_URB; i++) {
505  if (!port->bulk_out_size)
506  break;
507 
508  buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
509  if (!buffer)
510  goto bail_out_error2;
511  portdata->out_buffer[i] = buffer;
512 
513  urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
514  USB_DIR_OUT, port,
515  buffer, OUT_BUFLEN,
516  usb_wwan_outdat_callback);
517  portdata->out_urbs[i] = urb;
518  }
519 
520  usb_set_serial_port_data(port, portdata);
521 
522  if (port->interrupt_in_urb) {
524  if (err)
525  dev_dbg(&port->dev, "%s: submit irq_in urb failed %d\n",
526  __func__, err);
527  }
528 
529  return 0;
530 
531 bail_out_error2:
532  for (i = 0; i < N_OUT_URB; i++) {
533  usb_free_urb(portdata->out_urbs[i]);
534  kfree(portdata->out_buffer[i]);
535  }
536 bail_out_error:
537  for (i = 0; i < N_IN_URB; i++) {
538  usb_free_urb(portdata->in_urbs[i]);
539  free_page((unsigned long)portdata->in_buffer[i]);
540  }
541  kfree(portdata);
542 
543  return -ENOMEM;
544 }
546 
548 {
549  int i;
550  struct usb_wwan_port_private *portdata;
551 
552  portdata = usb_get_serial_port_data(port);
553  usb_set_serial_port_data(port, NULL);
554 
555  /* Stop reading/writing urbs and free them */
556  for (i = 0; i < N_IN_URB; i++) {
557  usb_kill_urb(portdata->in_urbs[i]);
558  usb_free_urb(portdata->in_urbs[i]);
559  free_page((unsigned long)portdata->in_buffer[i]);
560  }
561  for (i = 0; i < N_OUT_URB; i++) {
562  usb_kill_urb(portdata->out_urbs[i]);
563  usb_free_urb(portdata->out_urbs[i]);
564  kfree(portdata->out_buffer[i]);
565  }
566 
567  /* Now free port private data */
568  kfree(portdata);
569  return 0;
570 }
572 
573 #ifdef CONFIG_PM
574 static void stop_read_write_urbs(struct usb_serial *serial)
575 {
576  int i, j;
577  struct usb_serial_port *port;
578  struct usb_wwan_port_private *portdata;
579 
580  /* Stop reading/writing urbs */
581  for (i = 0; i < serial->num_ports; ++i) {
582  port = serial->port[i];
583  portdata = usb_get_serial_port_data(port);
584  if (!portdata)
585  continue;
586  for (j = 0; j < N_IN_URB; j++)
587  usb_kill_urb(portdata->in_urbs[j]);
588  for (j = 0; j < N_OUT_URB; j++)
589  usb_kill_urb(portdata->out_urbs[j]);
590  }
591 }
592 
593 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
594 {
595  struct usb_wwan_intf_private *intfdata = serial->private;
596  int b;
597 
598  if (PMSG_IS_AUTO(message)) {
599  spin_lock_irq(&intfdata->susp_lock);
600  b = intfdata->in_flight;
601  spin_unlock_irq(&intfdata->susp_lock);
602 
603  if (b)
604  return -EBUSY;
605  }
606 
607  spin_lock_irq(&intfdata->susp_lock);
608  intfdata->suspended = 1;
609  spin_unlock_irq(&intfdata->susp_lock);
610  stop_read_write_urbs(serial);
611 
612  return 0;
613 }
614 EXPORT_SYMBOL(usb_wwan_suspend);
615 
616 static void unbusy_queued_urb(struct urb *urb, struct usb_wwan_port_private *portdata)
617 {
618  int i;
619 
620  for (i = 0; i < N_OUT_URB; i++) {
621  if (urb == portdata->out_urbs[i]) {
622  clear_bit(i, &portdata->out_busy);
623  break;
624  }
625  }
626 }
627 
628 static void play_delayed(struct usb_serial_port *port)
629 {
630  struct usb_wwan_intf_private *data;
631  struct usb_wwan_port_private *portdata;
632  struct urb *urb;
633  int err;
634 
635  portdata = usb_get_serial_port_data(port);
636  data = port->serial->private;
637  while ((urb = usb_get_from_anchor(&portdata->delayed))) {
638  err = usb_submit_urb(urb, GFP_ATOMIC);
639  if (!err) {
640  data->in_flight++;
641  } else {
642  /* we have to throw away the rest */
643  do {
644  unbusy_queued_urb(urb, portdata);
645  usb_autopm_put_interface_no_suspend(port->serial->interface);
646  } while ((urb = usb_get_from_anchor(&portdata->delayed)));
647  break;
648  }
649  }
650 }
651 
652 int usb_wwan_resume(struct usb_serial *serial)
653 {
654  int i, j;
655  struct usb_serial_port *port;
656  struct usb_wwan_intf_private *intfdata = serial->private;
657  struct usb_wwan_port_private *portdata;
658  struct urb *urb;
659  int err = 0;
660 
661  /* get the interrupt URBs resubmitted unconditionally */
662  for (i = 0; i < serial->num_ports; i++) {
663  port = serial->port[i];
664  if (!port->interrupt_in_urb) {
665  dev_dbg(&port->dev, "%s: No interrupt URB for port\n", __func__);
666  continue;
667  }
669  dev_dbg(&port->dev, "Submitted interrupt URB for port (result %d)\n", err);
670  if (err < 0) {
671  dev_err(&port->dev, "%s: Error %d for interrupt URB\n",
672  __func__, err);
673  goto err_out;
674  }
675  }
676 
677  for (i = 0; i < serial->num_ports; i++) {
678  /* walk all ports */
679  port = serial->port[i];
680  portdata = usb_get_serial_port_data(port);
681 
682  /* skip closed ports */
683  spin_lock_irq(&intfdata->susp_lock);
684  if (!portdata || !portdata->opened) {
685  spin_unlock_irq(&intfdata->susp_lock);
686  continue;
687  }
688 
689  for (j = 0; j < N_IN_URB; j++) {
690  urb = portdata->in_urbs[j];
691  err = usb_submit_urb(urb, GFP_ATOMIC);
692  if (err < 0) {
693  dev_err(&port->dev, "%s: Error %d for bulk URB %d\n",
694  __func__, err, i);
695  spin_unlock_irq(&intfdata->susp_lock);
696  goto err_out;
697  }
698  }
699  play_delayed(port);
700  spin_unlock_irq(&intfdata->susp_lock);
701  }
702  spin_lock_irq(&intfdata->susp_lock);
703  intfdata->suspended = 0;
704  spin_unlock_irq(&intfdata->susp_lock);
705 err_out:
706  return err;
707 }
708 EXPORT_SYMBOL(usb_wwan_resume);
709 #endif
710 
714 MODULE_LICENSE("GPL");