pty: Repair TIOCGPTPEER
[cris-mirror.git] / drivers / usb / serial / usb_wwan.c
blob59bfcb3da116c4a49398750a1678f1d0349678fd
1 /*
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>
37 #include "usb-wwan.h"
40 * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
41 * in CDC ACM.
43 static int usb_wwan_send_setup(struct usb_serial_port *port)
45 struct usb_serial *serial = port->serial;
46 struct usb_wwan_port_private *portdata;
47 int val = 0;
48 int ifnum;
49 int res;
51 portdata = usb_get_serial_port_data(port);
53 if (portdata->dtr_state)
54 val |= 0x01;
55 if (portdata->rts_state)
56 val |= 0x02;
58 ifnum = serial->interface->cur_altsetting->desc.bInterfaceNumber;
60 res = usb_autopm_get_interface(serial->interface);
61 if (res)
62 return res;
64 res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
65 0x22, 0x21, val, ifnum, NULL, 0,
66 USB_CTRL_SET_TIMEOUT);
68 usb_autopm_put_interface(port->serial->interface);
70 return res;
73 void usb_wwan_dtr_rts(struct usb_serial_port *port, int on)
75 struct usb_wwan_port_private *portdata;
76 struct usb_wwan_intf_private *intfdata;
78 intfdata = usb_get_serial_data(port->serial);
80 if (!intfdata->use_send_setup)
81 return;
83 portdata = usb_get_serial_port_data(port);
84 /* FIXME: locking */
85 portdata->rts_state = on;
86 portdata->dtr_state = on;
88 usb_wwan_send_setup(port);
90 EXPORT_SYMBOL(usb_wwan_dtr_rts);
92 int usb_wwan_tiocmget(struct tty_struct *tty)
94 struct usb_serial_port *port = tty->driver_data;
95 unsigned int value;
96 struct usb_wwan_port_private *portdata;
98 portdata = usb_get_serial_port_data(port);
100 value = ((portdata->rts_state) ? TIOCM_RTS : 0) |
101 ((portdata->dtr_state) ? TIOCM_DTR : 0) |
102 ((portdata->cts_state) ? TIOCM_CTS : 0) |
103 ((portdata->dsr_state) ? TIOCM_DSR : 0) |
104 ((portdata->dcd_state) ? TIOCM_CAR : 0) |
105 ((portdata->ri_state) ? TIOCM_RNG : 0);
107 return value;
109 EXPORT_SYMBOL(usb_wwan_tiocmget);
111 int usb_wwan_tiocmset(struct tty_struct *tty,
112 unsigned int set, unsigned int clear)
114 struct usb_serial_port *port = tty->driver_data;
115 struct usb_wwan_port_private *portdata;
116 struct usb_wwan_intf_private *intfdata;
118 portdata = usb_get_serial_port_data(port);
119 intfdata = usb_get_serial_data(port->serial);
121 if (!intfdata->use_send_setup)
122 return -EINVAL;
124 /* FIXME: what locks portdata fields ? */
125 if (set & TIOCM_RTS)
126 portdata->rts_state = 1;
127 if (set & TIOCM_DTR)
128 portdata->dtr_state = 1;
130 if (clear & TIOCM_RTS)
131 portdata->rts_state = 0;
132 if (clear & TIOCM_DTR)
133 portdata->dtr_state = 0;
134 return usb_wwan_send_setup(port);
136 EXPORT_SYMBOL(usb_wwan_tiocmset);
138 static int get_serial_info(struct usb_serial_port *port,
139 struct serial_struct __user *retinfo)
141 struct serial_struct tmp;
143 memset(&tmp, 0, sizeof(tmp));
144 tmp.line = port->minor;
145 tmp.port = port->port_number;
146 tmp.baud_base = tty_get_baud_rate(port->port.tty);
147 tmp.close_delay = port->port.close_delay / 10;
148 tmp.closing_wait = port->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
149 ASYNC_CLOSING_WAIT_NONE :
150 port->port.closing_wait / 10;
152 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
153 return -EFAULT;
154 return 0;
157 static int set_serial_info(struct usb_serial_port *port,
158 struct serial_struct __user *newinfo)
160 struct serial_struct new_serial;
161 unsigned int closing_wait, close_delay;
162 int retval = 0;
164 if (copy_from_user(&new_serial, newinfo, sizeof(new_serial)))
165 return -EFAULT;
167 close_delay = new_serial.close_delay * 10;
168 closing_wait = new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ?
169 ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10;
171 mutex_lock(&port->port.mutex);
173 if (!capable(CAP_SYS_ADMIN)) {
174 if ((close_delay != port->port.close_delay) ||
175 (closing_wait != port->port.closing_wait))
176 retval = -EPERM;
177 else
178 retval = -EOPNOTSUPP;
179 } else {
180 port->port.close_delay = close_delay;
181 port->port.closing_wait = closing_wait;
184 mutex_unlock(&port->port.mutex);
185 return retval;
188 int usb_wwan_ioctl(struct tty_struct *tty,
189 unsigned int cmd, unsigned long arg)
191 struct usb_serial_port *port = tty->driver_data;
193 dev_dbg(&port->dev, "%s cmd 0x%04x\n", __func__, cmd);
195 switch (cmd) {
196 case TIOCGSERIAL:
197 return get_serial_info(port,
198 (struct serial_struct __user *) arg);
199 case TIOCSSERIAL:
200 return set_serial_info(port,
201 (struct serial_struct __user *) arg);
202 default:
203 break;
206 dev_dbg(&port->dev, "%s arg not supported\n", __func__);
208 return -ENOIOCTLCMD;
210 EXPORT_SYMBOL(usb_wwan_ioctl);
212 int usb_wwan_write(struct tty_struct *tty, struct usb_serial_port *port,
213 const unsigned char *buf, int count)
215 struct usb_wwan_port_private *portdata;
216 struct usb_wwan_intf_private *intfdata;
217 int i;
218 int left, todo;
219 struct urb *this_urb = NULL; /* spurious */
220 int err;
221 unsigned long flags;
223 portdata = usb_get_serial_port_data(port);
224 intfdata = usb_get_serial_data(port->serial);
226 dev_dbg(&port->dev, "%s: write (%d chars)\n", __func__, count);
228 i = 0;
229 left = count;
230 for (i = 0; left > 0 && i < N_OUT_URB; i++) {
231 todo = left;
232 if (todo > OUT_BUFLEN)
233 todo = OUT_BUFLEN;
235 this_urb = portdata->out_urbs[i];
236 if (test_and_set_bit(i, &portdata->out_busy)) {
237 if (time_before(jiffies,
238 portdata->tx_start_time[i] + 10 * HZ))
239 continue;
240 usb_unlink_urb(this_urb);
241 continue;
243 dev_dbg(&port->dev, "%s: endpoint %d buf %d\n", __func__,
244 usb_pipeendpoint(this_urb->pipe), i);
246 err = usb_autopm_get_interface_async(port->serial->interface);
247 if (err < 0) {
248 clear_bit(i, &portdata->out_busy);
249 break;
252 /* send the data */
253 memcpy(this_urb->transfer_buffer, buf, todo);
254 this_urb->transfer_buffer_length = todo;
256 spin_lock_irqsave(&intfdata->susp_lock, flags);
257 if (intfdata->suspended) {
258 usb_anchor_urb(this_urb, &portdata->delayed);
259 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
260 } else {
261 intfdata->in_flight++;
262 spin_unlock_irqrestore(&intfdata->susp_lock, flags);
263 err = usb_submit_urb(this_urb, GFP_ATOMIC);
264 if (err) {
265 dev_err(&port->dev,
266 "%s: submit urb %d failed: %d\n",
267 __func__, i, err);
268 clear_bit(i, &portdata->out_busy);
269 spin_lock_irqsave(&intfdata->susp_lock, flags);
270 intfdata->in_flight--;
271 spin_unlock_irqrestore(&intfdata->susp_lock,
272 flags);
273 usb_autopm_put_interface_async(port->serial->interface);
274 break;
278 portdata->tx_start_time[i] = jiffies;
279 buf += todo;
280 left -= todo;
283 count -= left;
284 dev_dbg(&port->dev, "%s: wrote (did %d)\n", __func__, count);
285 return count;
287 EXPORT_SYMBOL(usb_wwan_write);
289 static void usb_wwan_indat_callback(struct urb *urb)
291 int err;
292 int endpoint;
293 struct usb_serial_port *port;
294 struct device *dev;
295 unsigned char *data = urb->transfer_buffer;
296 int status = urb->status;
298 endpoint = usb_pipeendpoint(urb->pipe);
299 port = urb->context;
300 dev = &port->dev;
302 if (status) {
303 dev_dbg(dev, "%s: nonzero status: %d on endpoint %02x.\n",
304 __func__, status, endpoint);
305 } else {
306 if (urb->actual_length) {
307 tty_insert_flip_string(&port->port, data,
308 urb->actual_length);
309 tty_flip_buffer_push(&port->port);
310 } else
311 dev_dbg(dev, "%s: empty read urb received\n", __func__);
313 /* Resubmit urb so we continue receiving */
314 err = usb_submit_urb(urb, GFP_ATOMIC);
315 if (err) {
316 if (err != -EPERM && err != -ENODEV) {
317 dev_err(dev, "%s: resubmit read urb failed. (%d)\n",
318 __func__, err);
319 /* busy also in error unless we are killed */
320 usb_mark_last_busy(port->serial->dev);
322 } else {
323 usb_mark_last_busy(port->serial->dev);
327 static void usb_wwan_outdat_callback(struct urb *urb)
329 struct usb_serial_port *port;
330 struct usb_wwan_port_private *portdata;
331 struct usb_wwan_intf_private *intfdata;
332 int i;
334 port = urb->context;
335 intfdata = usb_get_serial_data(port->serial);
337 usb_serial_port_softint(port);
338 usb_autopm_put_interface_async(port->serial->interface);
339 portdata = usb_get_serial_port_data(port);
340 spin_lock(&intfdata->susp_lock);
341 intfdata->in_flight--;
342 spin_unlock(&intfdata->susp_lock);
344 for (i = 0; i < N_OUT_URB; ++i) {
345 if (portdata->out_urbs[i] == urb) {
346 smp_mb__before_atomic();
347 clear_bit(i, &portdata->out_busy);
348 break;
353 int usb_wwan_write_room(struct tty_struct *tty)
355 struct usb_serial_port *port = tty->driver_data;
356 struct usb_wwan_port_private *portdata;
357 int i;
358 int data_len = 0;
359 struct urb *this_urb;
361 portdata = usb_get_serial_port_data(port);
363 for (i = 0; i < N_OUT_URB; i++) {
364 this_urb = portdata->out_urbs[i];
365 if (this_urb && !test_bit(i, &portdata->out_busy))
366 data_len += OUT_BUFLEN;
369 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
370 return data_len;
372 EXPORT_SYMBOL(usb_wwan_write_room);
374 int usb_wwan_chars_in_buffer(struct tty_struct *tty)
376 struct usb_serial_port *port = tty->driver_data;
377 struct usb_wwan_port_private *portdata;
378 int i;
379 int data_len = 0;
380 struct urb *this_urb;
382 portdata = usb_get_serial_port_data(port);
384 for (i = 0; i < N_OUT_URB; i++) {
385 this_urb = portdata->out_urbs[i];
386 /* FIXME: This locking is insufficient as this_urb may
387 go unused during the test */
388 if (this_urb && test_bit(i, &portdata->out_busy))
389 data_len += this_urb->transfer_buffer_length;
391 dev_dbg(&port->dev, "%s: %d\n", __func__, data_len);
392 return data_len;
394 EXPORT_SYMBOL(usb_wwan_chars_in_buffer);
396 int usb_wwan_open(struct tty_struct *tty, struct usb_serial_port *port)
398 struct usb_wwan_port_private *portdata;
399 struct usb_wwan_intf_private *intfdata;
400 struct usb_serial *serial = port->serial;
401 int i, err;
402 struct urb *urb;
404 portdata = usb_get_serial_port_data(port);
405 intfdata = usb_get_serial_data(serial);
407 if (port->interrupt_in_urb) {
408 err = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
409 if (err) {
410 dev_err(&port->dev, "%s: submit int urb failed: %d\n",
411 __func__, err);
415 /* Start reading from the IN endpoint */
416 for (i = 0; i < N_IN_URB; i++) {
417 urb = portdata->in_urbs[i];
418 if (!urb)
419 continue;
420 err = usb_submit_urb(urb, GFP_KERNEL);
421 if (err) {
422 dev_err(&port->dev,
423 "%s: submit read urb %d failed: %d\n",
424 __func__, i, err);
428 spin_lock_irq(&intfdata->susp_lock);
429 if (++intfdata->open_ports == 1)
430 serial->interface->needs_remote_wakeup = 1;
431 spin_unlock_irq(&intfdata->susp_lock);
432 /* this balances a get in the generic USB serial code */
433 usb_autopm_put_interface(serial->interface);
435 return 0;
437 EXPORT_SYMBOL(usb_wwan_open);
439 static void unbusy_queued_urb(struct urb *urb,
440 struct usb_wwan_port_private *portdata)
442 int i;
444 for (i = 0; i < N_OUT_URB; i++) {
445 if (urb == portdata->out_urbs[i]) {
446 clear_bit(i, &portdata->out_busy);
447 break;
452 void usb_wwan_close(struct usb_serial_port *port)
454 int i;
455 struct usb_serial *serial = port->serial;
456 struct usb_wwan_port_private *portdata;
457 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
458 struct urb *urb;
460 portdata = usb_get_serial_port_data(port);
463 * Need to take susp_lock to make sure port is not already being
464 * resumed, but no need to hold it due to initialized
466 spin_lock_irq(&intfdata->susp_lock);
467 if (--intfdata->open_ports == 0)
468 serial->interface->needs_remote_wakeup = 0;
469 spin_unlock_irq(&intfdata->susp_lock);
471 for (;;) {
472 urb = usb_get_from_anchor(&portdata->delayed);
473 if (!urb)
474 break;
475 unbusy_queued_urb(urb, portdata);
476 usb_autopm_put_interface_async(serial->interface);
479 for (i = 0; i < N_IN_URB; i++)
480 usb_kill_urb(portdata->in_urbs[i]);
481 for (i = 0; i < N_OUT_URB; i++)
482 usb_kill_urb(portdata->out_urbs[i]);
483 usb_kill_urb(port->interrupt_in_urb);
485 usb_autopm_get_interface_no_resume(serial->interface);
487 EXPORT_SYMBOL(usb_wwan_close);
489 static struct urb *usb_wwan_setup_urb(struct usb_serial_port *port,
490 int endpoint,
491 int dir, void *ctx, char *buf, int len,
492 void (*callback) (struct urb *))
494 struct usb_serial *serial = port->serial;
495 struct urb *urb;
497 urb = usb_alloc_urb(0, GFP_KERNEL); /* No ISO */
498 if (!urb)
499 return NULL;
501 usb_fill_bulk_urb(urb, serial->dev,
502 usb_sndbulkpipe(serial->dev, endpoint) | dir,
503 buf, len, callback, ctx);
505 return urb;
508 int usb_wwan_port_probe(struct usb_serial_port *port)
510 struct usb_wwan_port_private *portdata;
511 struct urb *urb;
512 u8 *buffer;
513 int i;
515 if (!port->bulk_in_size || !port->bulk_out_size)
516 return -ENODEV;
518 portdata = kzalloc(sizeof(*portdata), GFP_KERNEL);
519 if (!portdata)
520 return -ENOMEM;
522 init_usb_anchor(&portdata->delayed);
524 for (i = 0; i < N_IN_URB; i++) {
525 buffer = (u8 *)__get_free_page(GFP_KERNEL);
526 if (!buffer)
527 goto bail_out_error;
528 portdata->in_buffer[i] = buffer;
530 urb = usb_wwan_setup_urb(port, port->bulk_in_endpointAddress,
531 USB_DIR_IN, port,
532 buffer, IN_BUFLEN,
533 usb_wwan_indat_callback);
534 portdata->in_urbs[i] = urb;
537 for (i = 0; i < N_OUT_URB; i++) {
538 buffer = kmalloc(OUT_BUFLEN, GFP_KERNEL);
539 if (!buffer)
540 goto bail_out_error2;
541 portdata->out_buffer[i] = buffer;
543 urb = usb_wwan_setup_urb(port, port->bulk_out_endpointAddress,
544 USB_DIR_OUT, port,
545 buffer, OUT_BUFLEN,
546 usb_wwan_outdat_callback);
547 portdata->out_urbs[i] = urb;
550 usb_set_serial_port_data(port, portdata);
552 return 0;
554 bail_out_error2:
555 for (i = 0; i < N_OUT_URB; i++) {
556 usb_free_urb(portdata->out_urbs[i]);
557 kfree(portdata->out_buffer[i]);
559 bail_out_error:
560 for (i = 0; i < N_IN_URB; i++) {
561 usb_free_urb(portdata->in_urbs[i]);
562 free_page((unsigned long)portdata->in_buffer[i]);
564 kfree(portdata);
566 return -ENOMEM;
568 EXPORT_SYMBOL_GPL(usb_wwan_port_probe);
570 int usb_wwan_port_remove(struct usb_serial_port *port)
572 int i;
573 struct usb_wwan_port_private *portdata;
575 portdata = usb_get_serial_port_data(port);
576 usb_set_serial_port_data(port, NULL);
578 for (i = 0; i < N_IN_URB; i++) {
579 usb_free_urb(portdata->in_urbs[i]);
580 free_page((unsigned long)portdata->in_buffer[i]);
582 for (i = 0; i < N_OUT_URB; i++) {
583 usb_free_urb(portdata->out_urbs[i]);
584 kfree(portdata->out_buffer[i]);
587 kfree(portdata);
589 return 0;
591 EXPORT_SYMBOL(usb_wwan_port_remove);
593 #ifdef CONFIG_PM
594 static void stop_urbs(struct usb_serial *serial)
596 int i, j;
597 struct usb_serial_port *port;
598 struct usb_wwan_port_private *portdata;
600 for (i = 0; i < serial->num_ports; ++i) {
601 port = serial->port[i];
602 portdata = usb_get_serial_port_data(port);
603 if (!portdata)
604 continue;
605 for (j = 0; j < N_IN_URB; j++)
606 usb_kill_urb(portdata->in_urbs[j]);
607 for (j = 0; j < N_OUT_URB; j++)
608 usb_kill_urb(portdata->out_urbs[j]);
609 usb_kill_urb(port->interrupt_in_urb);
613 int usb_wwan_suspend(struct usb_serial *serial, pm_message_t message)
615 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
617 spin_lock_irq(&intfdata->susp_lock);
618 if (PMSG_IS_AUTO(message)) {
619 if (intfdata->in_flight) {
620 spin_unlock_irq(&intfdata->susp_lock);
621 return -EBUSY;
624 intfdata->suspended = 1;
625 spin_unlock_irq(&intfdata->susp_lock);
627 stop_urbs(serial);
629 return 0;
631 EXPORT_SYMBOL(usb_wwan_suspend);
633 /* Caller must hold susp_lock. */
634 static int usb_wwan_submit_delayed_urbs(struct usb_serial_port *port)
636 struct usb_serial *serial = port->serial;
637 struct usb_wwan_intf_private *data = usb_get_serial_data(serial);
638 struct usb_wwan_port_private *portdata;
639 struct urb *urb;
640 int err_count = 0;
641 int err;
643 portdata = usb_get_serial_port_data(port);
645 for (;;) {
646 urb = usb_get_from_anchor(&portdata->delayed);
647 if (!urb)
648 break;
650 err = usb_submit_urb(urb, GFP_ATOMIC);
651 if (err) {
652 dev_err(&port->dev, "%s: submit urb failed: %d\n",
653 __func__, err);
654 err_count++;
655 unbusy_queued_urb(urb, portdata);
656 usb_autopm_put_interface_async(serial->interface);
657 continue;
659 data->in_flight++;
662 if (err_count)
663 return -EIO;
665 return 0;
668 int usb_wwan_resume(struct usb_serial *serial)
670 int i, j;
671 struct usb_serial_port *port;
672 struct usb_wwan_intf_private *intfdata = usb_get_serial_data(serial);
673 struct usb_wwan_port_private *portdata;
674 struct urb *urb;
675 int err;
676 int err_count = 0;
678 spin_lock_irq(&intfdata->susp_lock);
679 for (i = 0; i < serial->num_ports; i++) {
680 port = serial->port[i];
682 if (!tty_port_initialized(&port->port))
683 continue;
685 portdata = usb_get_serial_port_data(port);
687 if (port->interrupt_in_urb) {
688 err = usb_submit_urb(port->interrupt_in_urb,
689 GFP_ATOMIC);
690 if (err) {
691 dev_err(&port->dev,
692 "%s: submit int urb failed: %d\n",
693 __func__, err);
694 err_count++;
698 err = usb_wwan_submit_delayed_urbs(port);
699 if (err)
700 err_count++;
702 for (j = 0; j < N_IN_URB; j++) {
703 urb = portdata->in_urbs[j];
704 err = usb_submit_urb(urb, GFP_ATOMIC);
705 if (err < 0) {
706 dev_err(&port->dev,
707 "%s: submit read urb %d failed: %d\n",
708 __func__, i, err);
709 err_count++;
713 intfdata->suspended = 0;
714 spin_unlock_irq(&intfdata->susp_lock);
716 if (err_count)
717 return -EIO;
719 return 0;
721 EXPORT_SYMBOL(usb_wwan_resume);
722 #endif
724 MODULE_AUTHOR(DRIVER_AUTHOR);
725 MODULE_DESCRIPTION(DRIVER_DESC);
726 MODULE_LICENSE("GPL");