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>
40 * Generate DTR/RTS signals on the port using the SET_CONTROL_LINE_STATE request
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
;
51 portdata
= usb_get_serial_port_data(port
);
53 if (portdata
->dtr_state
)
55 if (portdata
->rts_state
)
58 ifnum
= serial
->interface
->cur_altsetting
->desc
.bInterfaceNumber
;
60 res
= usb_autopm_get_interface(serial
->interface
);
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
);
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
)
83 portdata
= usb_get_serial_port_data(port
);
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
;
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);
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
)
124 /* FIXME: what locks portdata fields ? */
126 portdata
->rts_state
= 1;
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
)))
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
;
164 if (copy_from_user(&new_serial
, newinfo
, sizeof(new_serial
)))
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
))
178 retval
= -EOPNOTSUPP
;
180 port
->port
.close_delay
= close_delay
;
181 port
->port
.closing_wait
= closing_wait
;
184 mutex_unlock(&port
->port
.mutex
);
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
);
197 return get_serial_info(port
,
198 (struct serial_struct __user
*) arg
);
200 return set_serial_info(port
,
201 (struct serial_struct __user
*) arg
);
206 dev_dbg(&port
->dev
, "%s arg not supported\n", __func__
);
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
;
219 struct urb
*this_urb
= NULL
; /* spurious */
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
);
230 for (i
= 0; left
> 0 && i
< N_OUT_URB
; i
++) {
232 if (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
))
240 usb_unlink_urb(this_urb
);
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
);
248 clear_bit(i
, &portdata
->out_busy
);
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
);
261 intfdata
->in_flight
++;
262 spin_unlock_irqrestore(&intfdata
->susp_lock
, flags
);
263 err
= usb_submit_urb(this_urb
, GFP_ATOMIC
);
266 "%s: submit urb %d failed: %d\n",
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
,
273 usb_autopm_put_interface_async(port
->serial
->interface
);
278 portdata
->tx_start_time
[i
] = jiffies
;
284 dev_dbg(&port
->dev
, "%s: wrote (did %d)\n", __func__
, count
);
287 EXPORT_SYMBOL(usb_wwan_write
);
289 static void usb_wwan_indat_callback(struct urb
*urb
)
293 struct usb_serial_port
*port
;
295 unsigned char *data
= urb
->transfer_buffer
;
296 int status
= urb
->status
;
298 endpoint
= usb_pipeendpoint(urb
->pipe
);
303 dev_dbg(dev
, "%s: nonzero status: %d on endpoint %02x.\n",
304 __func__
, status
, endpoint
);
306 if (urb
->actual_length
) {
307 tty_insert_flip_string(&port
->port
, data
,
309 tty_flip_buffer_push(&port
->port
);
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
);
316 if (err
!= -EPERM
&& err
!= -ENODEV
) {
317 dev_err(dev
, "%s: resubmit read urb failed. (%d)\n",
319 /* busy also in error unless we are killed */
320 usb_mark_last_busy(port
->serial
->dev
);
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
;
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
);
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
;
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
);
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
;
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
);
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
;
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
);
410 dev_err(&port
->dev
, "%s: submit int urb failed: %d\n",
415 /* Start reading from the IN endpoint */
416 for (i
= 0; i
< N_IN_URB
; i
++) {
417 urb
= portdata
->in_urbs
[i
];
420 err
= usb_submit_urb(urb
, GFP_KERNEL
);
423 "%s: submit read urb %d failed: %d\n",
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
);
437 EXPORT_SYMBOL(usb_wwan_open
);
439 static void unbusy_queued_urb(struct urb
*urb
,
440 struct usb_wwan_port_private
*portdata
)
444 for (i
= 0; i
< N_OUT_URB
; i
++) {
445 if (urb
== portdata
->out_urbs
[i
]) {
446 clear_bit(i
, &portdata
->out_busy
);
452 void usb_wwan_close(struct usb_serial_port
*port
)
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
);
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
);
472 urb
= usb_get_from_anchor(&portdata
->delayed
);
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
,
491 int dir
, void *ctx
, char *buf
, int len
,
492 void (*callback
) (struct urb
*))
494 struct usb_serial
*serial
= port
->serial
;
497 urb
= usb_alloc_urb(0, GFP_KERNEL
); /* No ISO */
501 usb_fill_bulk_urb(urb
, serial
->dev
,
502 usb_sndbulkpipe(serial
->dev
, endpoint
) | dir
,
503 buf
, len
, callback
, ctx
);
508 int usb_wwan_port_probe(struct usb_serial_port
*port
)
510 struct usb_wwan_port_private
*portdata
;
515 if (!port
->bulk_in_size
|| !port
->bulk_out_size
)
518 portdata
= kzalloc(sizeof(*portdata
), GFP_KERNEL
);
522 init_usb_anchor(&portdata
->delayed
);
524 for (i
= 0; i
< N_IN_URB
; i
++) {
525 buffer
= (u8
*)__get_free_page(GFP_KERNEL
);
528 portdata
->in_buffer
[i
] = buffer
;
530 urb
= usb_wwan_setup_urb(port
, port
->bulk_in_endpointAddress
,
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
);
540 goto bail_out_error2
;
541 portdata
->out_buffer
[i
] = buffer
;
543 urb
= usb_wwan_setup_urb(port
, port
->bulk_out_endpointAddress
,
546 usb_wwan_outdat_callback
);
547 portdata
->out_urbs
[i
] = urb
;
550 usb_set_serial_port_data(port
, portdata
);
555 for (i
= 0; i
< N_OUT_URB
; i
++) {
556 usb_free_urb(portdata
->out_urbs
[i
]);
557 kfree(portdata
->out_buffer
[i
]);
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
]);
568 EXPORT_SYMBOL_GPL(usb_wwan_port_probe
);
570 int usb_wwan_port_remove(struct usb_serial_port
*port
)
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
]);
591 EXPORT_SYMBOL(usb_wwan_port_remove
);
594 static void stop_urbs(struct usb_serial
*serial
)
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
);
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
);
624 intfdata
->suspended
= 1;
625 spin_unlock_irq(&intfdata
->susp_lock
);
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
;
643 portdata
= usb_get_serial_port_data(port
);
646 urb
= usb_get_from_anchor(&portdata
->delayed
);
650 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
652 dev_err(&port
->dev
, "%s: submit urb failed: %d\n",
655 unbusy_queued_urb(urb
, portdata
);
656 usb_autopm_put_interface_async(serial
->interface
);
668 int usb_wwan_resume(struct usb_serial
*serial
)
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
;
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
))
685 portdata
= usb_get_serial_port_data(port
);
687 if (port
->interrupt_in_urb
) {
688 err
= usb_submit_urb(port
->interrupt_in_urb
,
692 "%s: submit int urb failed: %d\n",
698 err
= usb_wwan_submit_delayed_urbs(port
);
702 for (j
= 0; j
< N_IN_URB
; j
++) {
703 urb
= portdata
->in_urbs
[j
];
704 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
707 "%s: submit read urb %d failed: %d\n",
713 intfdata
->suspended
= 0;
714 spin_unlock_irq(&intfdata
->susp_lock
);
721 EXPORT_SYMBOL(usb_wwan_resume
);
724 MODULE_AUTHOR(DRIVER_AUTHOR
);
725 MODULE_DESCRIPTION(DRIVER_DESC
);
726 MODULE_LICENSE("GPL");