2 * USB driver for Gigaset 307x base via direct USB connection.
4 * Copyright (c) 2001 by Hansjoerg Lipp <hjlipp@web.de>,
5 * Tilman Schmidt <tilman@imap.cc>,
8 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
17 #include <linux/usb.h>
18 #include <linux/module.h>
19 #include <linux/moduleparam.h>
21 /* Version Information */
22 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
23 #define DRIVER_DESC "USB Driver for Gigaset 307x"
26 /* Module parameters */
28 static int startmode
= SM_ISDN
;
29 static int cidmode
= 1;
31 module_param(startmode
, int, S_IRUGO
);
32 module_param(cidmode
, int, S_IRUGO
);
33 MODULE_PARM_DESC(startmode
, "start in isdn4linux mode");
34 MODULE_PARM_DESC(cidmode
, "Call-ID mode");
36 #define GIGASET_MINORS 1
37 #define GIGASET_MINOR 16
38 #define GIGASET_MODULENAME "bas_gigaset"
39 #define GIGASET_DEVNAME "ttyGB"
41 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
42 #define IF_WRITEBUF 264
44 /* interrupt pipe message size according to ibid. ch. 2.2 */
47 /* Values for the Gigaset 307x */
48 #define USB_GIGA_VENDOR_ID 0x0681
49 #define USB_3070_PRODUCT_ID 0x0001
50 #define USB_3075_PRODUCT_ID 0x0002
51 #define USB_SX303_PRODUCT_ID 0x0021
52 #define USB_SX353_PRODUCT_ID 0x0022
54 /* table of devices that work with this driver */
55 static const struct usb_device_id gigaset_table
[] = {
56 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_3070_PRODUCT_ID
) },
57 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_3075_PRODUCT_ID
) },
58 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_SX303_PRODUCT_ID
) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_SX353_PRODUCT_ID
) },
60 { } /* Terminating entry */
63 MODULE_DEVICE_TABLE(usb
, gigaset_table
);
65 /*======================= local function prototypes ==========================*/
67 /* function called if a new device belonging to this driver is connected */
68 static int gigaset_probe(struct usb_interface
*interface
,
69 const struct usb_device_id
*id
);
71 /* Function will be called if the device is unplugged */
72 static void gigaset_disconnect(struct usb_interface
*interface
);
74 /* functions called before/after suspend */
75 static int gigaset_suspend(struct usb_interface
*intf
, pm_message_t message
);
76 static int gigaset_resume(struct usb_interface
*intf
);
78 /* functions called before/after device reset */
79 static int gigaset_pre_reset(struct usb_interface
*intf
);
80 static int gigaset_post_reset(struct usb_interface
*intf
);
82 static int atread_submit(struct cardstate
*, int);
83 static void stopurbs(struct bas_bc_state
*);
84 static int req_submit(struct bc_state
*, int, int, int);
85 static int atwrite_submit(struct cardstate
*, unsigned char *, int);
86 static int start_cbsend(struct cardstate
*);
88 /*============================================================================*/
90 struct bas_cardstate
{
91 struct usb_device
*udev
; /* USB device pointer */
93 struct usb_interface
*interface
; /* interface for this device */
94 unsigned char minor
; /* starting minor number */
96 struct urb
*urb_ctrl
; /* control pipe default URB */
97 struct usb_ctrlrequest dr_ctrl
;
98 struct timer_list timer_ctrl
; /* control request timeout */
101 struct timer_list timer_atrdy
; /* AT command ready timeout */
102 struct urb
*urb_cmd_out
; /* for sending AT commands */
103 struct usb_ctrlrequest dr_cmd_out
;
106 struct urb
*urb_cmd_in
; /* for receiving AT replies */
107 struct usb_ctrlrequest dr_cmd_in
;
108 struct timer_list timer_cmd_in
; /* receive request timeout */
109 unsigned char *rcvbuf
; /* AT reply receive buffer */
111 struct urb
*urb_int_in
; /* URB for interrupt pipe */
112 unsigned char *int_in_buf
;
113 struct work_struct int_in_wq
; /* for usb_clear_halt() */
114 struct timer_list timer_int_in
; /* int read retry delay */
117 spinlock_t lock
; /* locks all following */
118 int basstate
; /* bitmap (BS_*) */
119 int pending
; /* uncompleted base request */
120 wait_queue_head_t waitqueue
;
121 int rcvbuf_size
; /* size of AT receive buffer */
122 /* 0: no receive in progress */
123 int retry_cmd_in
; /* receive req retry count */
126 /* status of direct USB connection to 307x base (bits in basstate) */
127 #define BS_ATOPEN 0x001 /* AT channel open */
128 #define BS_B1OPEN 0x002 /* B channel 1 open */
129 #define BS_B2OPEN 0x004 /* B channel 2 open */
130 #define BS_ATREADY 0x008 /* base ready for AT command */
131 #define BS_INIT 0x010 /* base has signalled INIT_OK */
132 #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
133 #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
134 #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
135 #define BS_SUSPEND 0x100 /* USB port suspended */
136 #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
139 static struct gigaset_driver
*driver
;
141 /* usb specific object needed to register this driver with the usb subsystem */
142 static struct usb_driver gigaset_usb_driver
= {
143 .name
= GIGASET_MODULENAME
,
144 .probe
= gigaset_probe
,
145 .disconnect
= gigaset_disconnect
,
146 .id_table
= gigaset_table
,
147 .suspend
= gigaset_suspend
,
148 .resume
= gigaset_resume
,
149 .reset_resume
= gigaset_post_reset
,
150 .pre_reset
= gigaset_pre_reset
,
151 .post_reset
= gigaset_post_reset
,
152 .disable_hub_initiated_lpm
= 1,
155 /* get message text for usb_submit_urb return code
157 static char *get_usb_rcmsg(int rc
)
159 static char unkmsg
[28];
165 return "out of memory";
167 return "device not present";
169 return "endpoint not present";
171 return "URB type not supported";
173 return "invalid argument";
175 return "start frame too early or too much scheduled";
177 return "too many isoc frames requested";
179 return "endpoint stalled";
181 return "invalid packet size";
183 return "would overcommit USB bandwidth";
185 return "device shut down";
187 return "reject flag set";
189 return "device suspended";
191 snprintf(unkmsg
, sizeof(unkmsg
), "unknown error %d", rc
);
196 /* get message text for USB status code
198 static char *get_usb_statmsg(int status
)
200 static char unkmsg
[28];
206 return "unlinked (sync)";
208 return "URB still pending";
210 return "bitstuff error, timeout, or unknown USB error";
212 return "CRC mismatch, timeout, or unknown USB error";
214 return "USB response timeout";
216 return "endpoint stalled";
218 return "IN buffer overrun";
220 return "OUT buffer underrun";
222 return "endpoint babble";
224 return "short packet";
226 return "device removed";
228 return "partial isoc transfer";
230 return "ISO madness";
232 return "unlinked (async)";
234 return "device shut down";
236 snprintf(unkmsg
, sizeof(unkmsg
), "unknown status %d", status
);
242 * retrieve string representation of USB pipe type
244 static inline char *usb_pipetype_str(int pipe
)
246 if (usb_pipeisoc(pipe
))
248 if (usb_pipeint(pipe
))
250 if (usb_pipecontrol(pipe
))
252 if (usb_pipebulk(pipe
))
258 * write content of URB to syslog for debugging
260 static inline void dump_urb(enum debuglevel level
, const char *tag
,
263 #ifdef CONFIG_GIGASET_DEBUG
265 gig_dbg(level
, "%s urb(0x%08lx)->{", tag
, (unsigned long) urb
);
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
270 (unsigned long) urb
->dev
,
271 usb_pipetype_str(urb
->pipe
),
272 usb_pipeendpoint(urb
->pipe
), usb_pipedevice(urb
->pipe
),
273 usb_pipein(urb
->pipe
) ? "in" : "out",
274 (unsigned long) urb
->hcpriv
,
275 urb
->transfer_flags
);
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
278 "setup_packet=0x%08lx,",
279 (unsigned long) urb
->transfer_buffer
,
280 urb
->transfer_buffer_length
, urb
->actual_length
,
281 (unsigned long) urb
->setup_packet
);
283 " start_frame=%d, number_of_packets=%d, interval=%d, "
285 urb
->start_frame
, urb
->number_of_packets
, urb
->interval
,
288 " context=0x%08lx, complete=0x%08lx, "
289 "iso_frame_desc[]={",
290 (unsigned long) urb
->context
,
291 (unsigned long) urb
->complete
);
292 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
293 struct usb_iso_packet_descriptor
*pifd
294 = &urb
->iso_frame_desc
[i
];
296 " {offset=%u, length=%u, actual_length=%u, "
298 pifd
->offset
, pifd
->length
, pifd
->actual_length
,
302 gig_dbg(level
, "}}");
306 /* read/set modem control bits etc. (m10x only) */
307 static int gigaset_set_modem_ctrl(struct cardstate
*cs
, unsigned old_state
,
313 static int gigaset_baud_rate(struct cardstate
*cs
, unsigned cflag
)
318 static int gigaset_set_line_ctrl(struct cardstate
*cs
, unsigned cflag
)
323 /* set/clear bits in base connection state, return previous state
325 static inline int update_basstate(struct bas_cardstate
*ucs
,
331 spin_lock_irqsave(&ucs
->lock
, flags
);
332 state
= ucs
->basstate
;
333 ucs
->basstate
= (state
& ~clear
) | set
;
334 spin_unlock_irqrestore(&ucs
->lock
, flags
);
339 * hang up any existing connection because of an unrecoverable error
340 * This function may be called from any context and takes care of scheduling
341 * the necessary actions for execution outside of interrupt context.
342 * cs->lock must not be held.
344 * B channel control structure
346 static inline void error_hangup(struct bc_state
*bcs
)
348 struct cardstate
*cs
= bcs
->cs
;
350 gigaset_add_event(cs
, &bcs
->at_state
, EV_HUP
, NULL
, 0, NULL
);
351 gigaset_schedule_event(cs
);
355 * reset Gigaset device because of an unrecoverable error
356 * This function may be called from any context, and takes care of
357 * scheduling the necessary actions for execution outside of interrupt context.
358 * cs->hw.bas->lock must not be held.
360 * controller state structure
362 static inline void error_reset(struct cardstate
*cs
)
364 /* reset interrupt pipe to recover (ignore errors) */
365 update_basstate(cs
->hw
.bas
, BS_RESETTING
, 0);
366 if (req_submit(cs
->bcs
, HD_RESET_INTERRUPT_PIPE
, 0, BAS_TIMEOUT
))
367 /* submission failed, escalate to USB port reset */
368 usb_queue_reset_device(cs
->hw
.bas
->interface
);
372 * check for completion of pending control request
374 * ucs hardware specific controller state structure
376 static void check_pending(struct bas_cardstate
*ucs
)
380 spin_lock_irqsave(&ucs
->lock
, flags
);
381 switch (ucs
->pending
) {
384 case HD_OPEN_ATCHANNEL
:
385 if (ucs
->basstate
& BS_ATOPEN
)
388 case HD_OPEN_B1CHANNEL
:
389 if (ucs
->basstate
& BS_B1OPEN
)
392 case HD_OPEN_B2CHANNEL
:
393 if (ucs
->basstate
& BS_B2OPEN
)
396 case HD_CLOSE_ATCHANNEL
:
397 if (!(ucs
->basstate
& BS_ATOPEN
))
400 case HD_CLOSE_B1CHANNEL
:
401 if (!(ucs
->basstate
& BS_B1OPEN
))
404 case HD_CLOSE_B2CHANNEL
:
405 if (!(ucs
->basstate
& BS_B2OPEN
))
408 case HD_DEVICE_INIT_ACK
: /* no reply expected */
411 case HD_RESET_INTERRUPT_PIPE
:
412 if (!(ucs
->basstate
& BS_RESETTING
))
416 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
417 * and should never end up here
420 dev_warn(&ucs
->interface
->dev
,
421 "unknown pending request 0x%02x cleared\n",
427 del_timer(&ucs
->timer_ctrl
);
429 spin_unlock_irqrestore(&ucs
->lock
, flags
);
433 * timeout routine for command input request
435 * controller state structure
437 static void cmd_in_timeout(struct timer_list
*t
)
439 struct bas_cardstate
*ucs
= from_timer(ucs
, t
, timer_cmd_in
);
440 struct cardstate
*cs
= ucs
->cs
;
443 if (!ucs
->rcvbuf_size
) {
444 gig_dbg(DEBUG_USBREQ
, "%s: no receive in progress", __func__
);
448 if (ucs
->retry_cmd_in
++ >= BAS_RETRY
) {
450 "control read: timeout, giving up after %d tries\n",
454 ucs
->rcvbuf_size
= 0;
459 gig_dbg(DEBUG_USBREQ
, "%s: timeout, retry %d",
460 __func__
, ucs
->retry_cmd_in
);
461 rc
= atread_submit(cs
, BAS_TIMEOUT
);
465 ucs
->rcvbuf_size
= 0;
471 /* read_ctrl_callback
472 * USB completion handler for control pipe input
473 * called by the USB subsystem in interrupt context
475 * urb USB request block
476 * urb->context = inbuf structure for controller state
478 static void read_ctrl_callback(struct urb
*urb
)
480 struct inbuf_t
*inbuf
= urb
->context
;
481 struct cardstate
*cs
= inbuf
->cs
;
482 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
483 int status
= urb
->status
;
487 update_basstate(ucs
, 0, BS_ATRDPEND
);
488 wake_up(&ucs
->waitqueue
);
489 del_timer(&ucs
->timer_cmd_in
);
492 case 0: /* normal completion */
493 numbytes
= urb
->actual_length
;
494 if (unlikely(numbytes
!= ucs
->rcvbuf_size
)) {
496 "control read: received %d chars, expected %d\n",
497 numbytes
, ucs
->rcvbuf_size
);
498 if (numbytes
> ucs
->rcvbuf_size
)
499 numbytes
= ucs
->rcvbuf_size
;
502 /* copy received bytes to inbuf, notify event layer */
503 if (gigaset_fill_inbuf(inbuf
, ucs
->rcvbuf
, numbytes
)) {
504 gig_dbg(DEBUG_INTR
, "%s-->BH", __func__
);
505 gigaset_schedule_event(cs
);
509 case -ENOENT
: /* cancelled */
510 case -ECONNRESET
: /* cancelled (async) */
511 case -EINPROGRESS
: /* pending */
512 case -ENODEV
: /* device removed */
513 case -ESHUTDOWN
: /* device shut down */
514 /* no further action necessary */
515 gig_dbg(DEBUG_USBREQ
, "%s: %s",
516 __func__
, get_usb_statmsg(status
));
519 default: /* other errors: retry */
520 if (ucs
->retry_cmd_in
++ < BAS_RETRY
) {
521 gig_dbg(DEBUG_USBREQ
, "%s: %s, retry %d", __func__
,
522 get_usb_statmsg(status
), ucs
->retry_cmd_in
);
523 rc
= atread_submit(cs
, BAS_TIMEOUT
);
525 /* successfully resubmitted, skip freeing */
528 /* disconnect, no further action necessary */
531 dev_err(cs
->dev
, "control read: %s, giving up after %d tries\n",
532 get_usb_statmsg(status
), ucs
->retry_cmd_in
);
536 /* read finished, free buffer */
539 ucs
->rcvbuf_size
= 0;
543 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
545 * cs controller state structure
546 * timeout timeout in 1/10 sec., 0: none
549 * -EBUSY if another request is pending
550 * any URB submission error code
552 static int atread_submit(struct cardstate
*cs
, int timeout
)
554 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
558 gig_dbg(DEBUG_USBREQ
, "-------> HD_READ_ATMESSAGE (%d)",
561 basstate
= update_basstate(ucs
, BS_ATRDPEND
, 0);
562 if (basstate
& BS_ATRDPEND
) {
564 "could not submit HD_READ_ATMESSAGE: URB busy\n");
568 if (basstate
& BS_SUSPEND
) {
570 "HD_READ_ATMESSAGE not submitted, "
571 "suspend in progress\n");
572 update_basstate(ucs
, 0, BS_ATRDPEND
);
573 /* treat like disconnect */
577 ucs
->dr_cmd_in
.bRequestType
= IN_VENDOR_REQ
;
578 ucs
->dr_cmd_in
.bRequest
= HD_READ_ATMESSAGE
;
579 ucs
->dr_cmd_in
.wValue
= 0;
580 ucs
->dr_cmd_in
.wIndex
= 0;
581 ucs
->dr_cmd_in
.wLength
= cpu_to_le16(ucs
->rcvbuf_size
);
582 usb_fill_control_urb(ucs
->urb_cmd_in
, ucs
->udev
,
583 usb_rcvctrlpipe(ucs
->udev
, 0),
584 (unsigned char *) &ucs
->dr_cmd_in
,
585 ucs
->rcvbuf
, ucs
->rcvbuf_size
,
586 read_ctrl_callback
, cs
->inbuf
);
588 ret
= usb_submit_urb(ucs
->urb_cmd_in
, GFP_ATOMIC
);
590 update_basstate(ucs
, 0, BS_ATRDPEND
);
591 dev_err(cs
->dev
, "could not submit HD_READ_ATMESSAGE: %s\n",
597 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
598 mod_timer(&ucs
->timer_cmd_in
, jiffies
+ timeout
* HZ
/ 10);
604 * workqueue routine to clear halt on interrupt in endpoint
607 static void int_in_work(struct work_struct
*work
)
609 struct bas_cardstate
*ucs
=
610 container_of(work
, struct bas_cardstate
, int_in_wq
);
611 struct urb
*urb
= ucs
->urb_int_in
;
612 struct cardstate
*cs
= urb
->context
;
615 /* clear halt condition */
616 rc
= usb_clear_halt(ucs
->udev
, urb
->pipe
);
617 gig_dbg(DEBUG_USBREQ
, "clear_halt: %s", get_usb_rcmsg(rc
));
619 /* success, resubmit interrupt read URB */
620 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
623 case 0: /* success */
624 case -ENODEV
: /* device gone */
625 case -EINVAL
: /* URB already resubmitted, or terminal badness */
627 default: /* failure: try to recover by resetting the device */
628 dev_err(cs
->dev
, "clear halt failed: %s\n", get_usb_rcmsg(rc
));
629 rc
= usb_lock_device_for_reset(ucs
->udev
, ucs
->interface
);
631 rc
= usb_reset_device(ucs
->udev
);
632 usb_unlock_device(ucs
->udev
);
635 ucs
->retry_int_in
= 0;
639 * timer routine for interrupt read delayed resubmit
641 * controller state structure
643 static void int_in_resubmit(struct timer_list
*t
)
645 struct bas_cardstate
*ucs
= from_timer(ucs
, t
, timer_int_in
);
646 struct cardstate
*cs
= ucs
->cs
;
649 if (ucs
->retry_int_in
++ >= BAS_RETRY
) {
650 dev_err(cs
->dev
, "interrupt read: giving up after %d tries\n",
652 usb_queue_reset_device(ucs
->interface
);
656 gig_dbg(DEBUG_USBREQ
, "%s: retry %d", __func__
, ucs
->retry_int_in
);
657 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_ATOMIC
);
658 if (rc
!= 0 && rc
!= -ENODEV
) {
659 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
661 usb_queue_reset_device(ucs
->interface
);
666 * USB completion handler for interrupt pipe input
667 * called by the USB subsystem in interrupt context
669 * urb USB request block
670 * urb->context = controller state structure
672 static void read_int_callback(struct urb
*urb
)
674 struct cardstate
*cs
= urb
->context
;
675 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
676 struct bc_state
*bcs
;
677 int status
= urb
->status
;
684 case 0: /* success */
685 ucs
->retry_int_in
= 0;
687 case -EPIPE
: /* endpoint stalled */
688 schedule_work(&ucs
->int_in_wq
);
690 case -ENOENT
: /* cancelled */
691 case -ECONNRESET
: /* cancelled (async) */
692 case -EINPROGRESS
: /* pending */
693 case -ENODEV
: /* device removed */
694 case -ESHUTDOWN
: /* device shut down */
695 /* no further action necessary */
696 gig_dbg(DEBUG_USBREQ
, "%s: %s",
697 __func__
, get_usb_statmsg(status
));
699 case -EPROTO
: /* protocol error or unplug */
702 /* resubmit after delay */
703 gig_dbg(DEBUG_USBREQ
, "%s: %s",
704 __func__
, get_usb_statmsg(status
));
705 mod_timer(&ucs
->timer_int_in
, jiffies
+ HZ
/ 10);
707 default: /* other errors: just resubmit */
708 dev_warn(cs
->dev
, "interrupt read: %s\n",
709 get_usb_statmsg(status
));
713 /* drop incomplete packets even if the missing bytes wouldn't matter */
714 if (unlikely(urb
->actual_length
< IP_MSGSIZE
)) {
715 dev_warn(cs
->dev
, "incomplete interrupt packet (%d bytes)\n",
720 l
= (unsigned) ucs
->int_in_buf
[1] +
721 (((unsigned) ucs
->int_in_buf
[2]) << 8);
723 gig_dbg(DEBUG_USBREQ
, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
724 urb
->actual_length
, (int)ucs
->int_in_buf
[0], l
,
725 (int)ucs
->int_in_buf
[1], (int)ucs
->int_in_buf
[2]);
729 switch (ucs
->int_in_buf
[0]) {
730 case HD_DEVICE_INIT_OK
:
731 update_basstate(ucs
, BS_INIT
, 0);
734 case HD_READY_SEND_ATDATA
:
735 del_timer(&ucs
->timer_atrdy
);
736 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
740 case HD_OPEN_B2CHANNEL_ACK
:
742 case HD_OPEN_B1CHANNEL_ACK
:
743 bcs
= cs
->bcs
+ channel
;
744 update_basstate(ucs
, BS_B1OPEN
<< channel
, 0);
745 gigaset_bchannel_up(bcs
);
748 case HD_OPEN_ATCHANNEL_ACK
:
749 update_basstate(ucs
, BS_ATOPEN
, 0);
753 case HD_CLOSE_B2CHANNEL_ACK
:
755 case HD_CLOSE_B1CHANNEL_ACK
:
756 bcs
= cs
->bcs
+ channel
;
757 update_basstate(ucs
, 0, BS_B1OPEN
<< channel
);
758 stopurbs(bcs
->hw
.bas
);
759 gigaset_bchannel_down(bcs
);
762 case HD_CLOSE_ATCHANNEL_ACK
:
763 update_basstate(ucs
, 0, BS_ATOPEN
);
766 case HD_B2_FLOW_CONTROL
:
768 case HD_B1_FLOW_CONTROL
:
769 bcs
= cs
->bcs
+ channel
;
770 atomic_add((l
- BAS_NORMFRAME
) * BAS_CORRFRAMES
,
771 &bcs
->hw
.bas
->corrbytes
);
773 "Flow control (channel %d, sub %d): 0x%02x => %d",
774 channel
, bcs
->hw
.bas
->numsub
, l
,
775 atomic_read(&bcs
->hw
.bas
->corrbytes
));
778 case HD_RECEIVEATDATA_ACK
: /* AT response ready to be received */
781 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
784 spin_lock_irqsave(&cs
->lock
, flags
);
785 if (ucs
->basstate
& BS_ATRDPEND
) {
786 spin_unlock_irqrestore(&cs
->lock
, flags
);
788 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
789 l
, ucs
->rcvbuf_size
);
792 if (ucs
->rcvbuf_size
) {
793 /* throw away previous buffer - we have no queue */
795 "receive AT data overrun, %d bytes lost\n",
798 ucs
->rcvbuf_size
= 0;
800 ucs
->rcvbuf
= kmalloc(l
, GFP_ATOMIC
);
801 if (ucs
->rcvbuf
== NULL
) {
802 spin_unlock_irqrestore(&cs
->lock
, flags
);
803 dev_err(cs
->dev
, "out of memory receiving AT data\n");
806 ucs
->rcvbuf_size
= l
;
807 ucs
->retry_cmd_in
= 0;
808 rc
= atread_submit(cs
, BAS_TIMEOUT
);
812 ucs
->rcvbuf_size
= 0;
814 spin_unlock_irqrestore(&cs
->lock
, flags
);
815 if (rc
< 0 && rc
!= -ENODEV
)
819 case HD_RESET_INTERRUPT_PIPE_ACK
:
820 update_basstate(ucs
, 0, BS_RESETTING
);
821 dev_notice(cs
->dev
, "interrupt pipe reset\n");
825 gig_dbg(DEBUG_USBREQ
, "HD_SUSPEND_END");
830 "unknown Gigaset signal 0x%02x (%u) ignored\n",
831 (int) ucs
->int_in_buf
[0], l
);
835 wake_up(&ucs
->waitqueue
);
838 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
839 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
840 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
847 * USB completion handler for B channel isochronous input
848 * called by the USB subsystem in interrupt context
850 * urb USB request block of completed request
851 * urb->context = bc_state structure
853 static void read_iso_callback(struct urb
*urb
)
855 struct bc_state
*bcs
;
856 struct bas_bc_state
*ubc
;
857 int status
= urb
->status
;
861 /* status codes not worth bothering the tasklet with */
862 if (unlikely(status
== -ENOENT
||
863 status
== -ECONNRESET
||
864 status
== -EINPROGRESS
||
866 status
== -ESHUTDOWN
)) {
867 gig_dbg(DEBUG_ISO
, "%s: %s",
868 __func__
, get_usb_statmsg(status
));
875 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
876 if (likely(ubc
->isoindone
== NULL
)) {
877 /* pass URB to tasklet */
878 ubc
->isoindone
= urb
;
879 ubc
->isoinstatus
= status
;
880 tasklet_hi_schedule(&ubc
->rcvd_tasklet
);
882 /* tasklet still busy, drop data and resubmit URB */
883 gig_dbg(DEBUG_ISO
, "%s: overrun", __func__
);
884 ubc
->loststatus
= status
;
885 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
886 ubc
->isoinlost
+= urb
->iso_frame_desc
[i
].actual_length
;
887 if (unlikely(urb
->iso_frame_desc
[i
].status
!= 0 &&
888 urb
->iso_frame_desc
[i
].status
!= -EINPROGRESS
))
889 ubc
->loststatus
= urb
->iso_frame_desc
[i
].status
;
890 urb
->iso_frame_desc
[i
].status
= 0;
891 urb
->iso_frame_desc
[i
].actual_length
= 0;
893 if (likely(ubc
->running
)) {
894 /* urb->dev is clobbered by USB subsystem */
895 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
896 urb
->transfer_flags
= URB_ISO_ASAP
;
897 urb
->number_of_packets
= BAS_NUMFRAMES
;
898 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
899 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
900 dev_err(bcs
->cs
->dev
,
901 "could not resubmit isoc read URB: %s\n",
903 dump_urb(DEBUG_ISO
, "isoc read", urb
);
908 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
911 /* write_iso_callback
912 * USB completion handler for B channel isochronous output
913 * called by the USB subsystem in interrupt context
915 * urb USB request block of completed request
916 * urb->context = isow_urbctx_t structure
918 static void write_iso_callback(struct urb
*urb
)
920 struct isow_urbctx_t
*ucx
;
921 struct bas_bc_state
*ubc
;
922 int status
= urb
->status
;
925 /* status codes not worth bothering the tasklet with */
926 if (unlikely(status
== -ENOENT
||
927 status
== -ECONNRESET
||
928 status
== -EINPROGRESS
||
930 status
== -ESHUTDOWN
)) {
931 gig_dbg(DEBUG_ISO
, "%s: %s",
932 __func__
, get_usb_statmsg(status
));
936 /* pass URB context to tasklet */
938 ubc
= ucx
->bcs
->hw
.bas
;
939 ucx
->status
= status
;
941 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
942 ubc
->isooutovfl
= ubc
->isooutdone
;
943 ubc
->isooutdone
= ucx
;
944 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
945 tasklet_hi_schedule(&ubc
->sent_tasklet
);
949 * prepare and submit USB request blocks for isochronous input and output
951 * B channel control structure
954 * < 0 on error (no URBs submitted)
956 static int starturbs(struct bc_state
*bcs
)
958 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
963 /* initialize L2 reception */
964 if (bcs
->proto2
== L2_HDLC
)
965 bcs
->inputstate
|= INS_flag_hunt
;
967 /* submit all isochronous input URBs */
969 for (k
= 0; k
< BAS_INURBS
; k
++) {
970 urb
= ubc
->isoinurbs
[k
];
976 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
977 urb
->pipe
= usb_rcvisocpipe(urb
->dev
, 3 + 2 * bcs
->channel
);
978 urb
->transfer_flags
= URB_ISO_ASAP
;
979 urb
->transfer_buffer
= ubc
->isoinbuf
+ k
* BAS_INBUFSIZE
;
980 urb
->transfer_buffer_length
= BAS_INBUFSIZE
;
981 urb
->number_of_packets
= BAS_NUMFRAMES
;
982 urb
->interval
= BAS_FRAMETIME
;
983 urb
->complete
= read_iso_callback
;
985 for (j
= 0; j
< BAS_NUMFRAMES
; j
++) {
986 urb
->iso_frame_desc
[j
].offset
= j
* BAS_MAXFRAME
;
987 urb
->iso_frame_desc
[j
].length
= BAS_MAXFRAME
;
988 urb
->iso_frame_desc
[j
].status
= 0;
989 urb
->iso_frame_desc
[j
].actual_length
= 0;
992 dump_urb(DEBUG_ISO
, "Initial isoc read", urb
);
993 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
998 /* initialize L2 transmission */
999 gigaset_isowbuf_init(ubc
->isooutbuf
, PPP_FLAG
);
1001 /* set up isochronous output URBs for flag idling */
1002 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
1003 urb
= ubc
->isoouturbs
[k
].urb
;
1008 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1009 urb
->pipe
= usb_sndisocpipe(urb
->dev
, 4 + 2 * bcs
->channel
);
1010 urb
->transfer_flags
= URB_ISO_ASAP
;
1011 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1012 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1013 urb
->number_of_packets
= BAS_NUMFRAMES
;
1014 urb
->interval
= BAS_FRAMETIME
;
1015 urb
->complete
= write_iso_callback
;
1016 urb
->context
= &ubc
->isoouturbs
[k
];
1017 for (j
= 0; j
< BAS_NUMFRAMES
; ++j
) {
1018 urb
->iso_frame_desc
[j
].offset
= BAS_OUTBUFSIZE
;
1019 urb
->iso_frame_desc
[j
].length
= BAS_NORMFRAME
;
1020 urb
->iso_frame_desc
[j
].status
= 0;
1021 urb
->iso_frame_desc
[j
].actual_length
= 0;
1023 ubc
->isoouturbs
[k
].limit
= -1;
1026 /* keep one URB free, submit the others */
1027 for (k
= 0; k
< BAS_OUTURBS
- 1; ++k
) {
1028 dump_urb(DEBUG_ISO
, "Initial isoc write", urb
);
1029 rc
= usb_submit_urb(ubc
->isoouturbs
[k
].urb
, GFP_ATOMIC
);
1033 dump_urb(DEBUG_ISO
, "Initial isoc write (free)", urb
);
1034 ubc
->isooutfree
= &ubc
->isoouturbs
[BAS_OUTURBS
- 1];
1035 ubc
->isooutdone
= ubc
->isooutovfl
= NULL
;
1043 * cancel the USB request blocks for isochronous input and output
1044 * errors are silently ignored
1046 * B channel control structure
1048 static void stopurbs(struct bas_bc_state
*ubc
)
1054 for (k
= 0; k
< BAS_INURBS
; ++k
) {
1055 rc
= usb_unlink_urb(ubc
->isoinurbs
[k
]);
1057 "%s: isoc input URB %d unlinked, result = %s",
1058 __func__
, k
, get_usb_rcmsg(rc
));
1061 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
1062 rc
= usb_unlink_urb(ubc
->isoouturbs
[k
].urb
);
1064 "%s: isoc output URB %d unlinked, result = %s",
1065 __func__
, k
, get_usb_rcmsg(rc
));
1069 /* Isochronous Write - Bottom Half */
1070 /* =============================== */
1072 /* submit_iso_write_urb
1073 * fill and submit the next isochronous write URB
1075 * ucx context structure containing URB
1077 * number of frames submitted in URB
1078 * 0 if URB not submitted because no data available (isooutbuf busy)
1079 * error code < 0 on error
1081 static int submit_iso_write_urb(struct isow_urbctx_t
*ucx
)
1083 struct urb
*urb
= ucx
->urb
;
1084 struct bas_bc_state
*ubc
= ucx
->bcs
->hw
.bas
;
1085 struct usb_iso_packet_descriptor
*ifd
;
1086 int corrbytes
, nframe
, rc
;
1088 /* urb->dev is clobbered by USB subsystem */
1089 urb
->dev
= ucx
->bcs
->cs
->hw
.bas
->udev
;
1090 urb
->transfer_flags
= URB_ISO_ASAP
;
1091 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1092 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1094 for (nframe
= 0; nframe
< BAS_NUMFRAMES
; nframe
++) {
1095 ifd
= &urb
->iso_frame_desc
[nframe
];
1097 /* compute frame length according to flow control */
1098 ifd
->length
= BAS_NORMFRAME
;
1099 corrbytes
= atomic_read(&ubc
->corrbytes
);
1100 if (corrbytes
!= 0) {
1101 gig_dbg(DEBUG_ISO
, "%s: corrbytes=%d",
1102 __func__
, corrbytes
);
1103 if (corrbytes
> BAS_HIGHFRAME
- BAS_NORMFRAME
)
1104 corrbytes
= BAS_HIGHFRAME
- BAS_NORMFRAME
;
1105 else if (corrbytes
< BAS_LOWFRAME
- BAS_NORMFRAME
)
1106 corrbytes
= BAS_LOWFRAME
- BAS_NORMFRAME
;
1107 ifd
->length
+= corrbytes
;
1108 atomic_add(-corrbytes
, &ubc
->corrbytes
);
1111 /* retrieve block of data to send */
1112 rc
= gigaset_isowbuf_getbytes(ubc
->isooutbuf
, ifd
->length
);
1116 "%s: buffer busy at frame %d",
1118 /* tasklet will be restarted from
1119 gigaset_isoc_send_skb() */
1121 dev_err(ucx
->bcs
->cs
->dev
,
1122 "%s: buffer error %d at frame %d\n",
1123 __func__
, rc
, nframe
);
1129 ucx
->limit
= ubc
->isooutbuf
->nextread
;
1131 ifd
->actual_length
= 0;
1133 if (unlikely(nframe
== 0))
1134 return 0; /* no data to send */
1135 urb
->number_of_packets
= nframe
;
1137 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1140 /* device removed - give up silently */
1141 gig_dbg(DEBUG_ISO
, "%s: disconnected", __func__
);
1143 dev_err(ucx
->bcs
->cs
->dev
,
1144 "could not submit isoc write URB: %s\n",
1152 /* write_iso_tasklet
1153 * tasklet scheduled when an isochronous output URB from the Gigaset device
1156 * data B channel state structure
1158 static void write_iso_tasklet(unsigned long data
)
1160 struct bc_state
*bcs
= (struct bc_state
*) data
;
1161 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1162 struct cardstate
*cs
= bcs
->cs
;
1163 struct isow_urbctx_t
*done
, *next
, *ovfl
;
1166 struct usb_iso_packet_descriptor
*ifd
;
1167 unsigned long flags
;
1169 struct sk_buff
*skb
;
1173 /* loop while completed URBs arrive in time */
1175 if (unlikely(!(ubc
->running
))) {
1176 gig_dbg(DEBUG_ISO
, "%s: not running", __func__
);
1180 /* retrieve completed URBs */
1181 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1182 done
= ubc
->isooutdone
;
1183 ubc
->isooutdone
= NULL
;
1184 ovfl
= ubc
->isooutovfl
;
1185 ubc
->isooutovfl
= NULL
;
1186 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1188 dev_err(cs
->dev
, "isoc write underrun\n");
1195 /* submit free URB if available */
1196 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1197 next
= ubc
->isooutfree
;
1198 ubc
->isooutfree
= NULL
;
1199 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1201 rc
= submit_iso_write_urb(next
);
1202 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1203 /* could not submit URB, put it back */
1204 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1205 if (ubc
->isooutfree
== NULL
) {
1206 ubc
->isooutfree
= next
;
1209 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1211 /* couldn't put it back */
1213 "losing isoc write URB\n");
1219 /* process completed URB */
1221 status
= done
->status
;
1223 case -EXDEV
: /* partial completion */
1224 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1226 /* fall through - what's the difference anyway? */
1227 case 0: /* normal completion */
1228 /* inspect individual frames
1229 * assumptions (for lack of documentation):
1230 * - actual_length bytes of first frame in error are
1232 * - all following frames are not sent at all
1234 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
1235 ifd
= &urb
->iso_frame_desc
[i
];
1237 ifd
->actual_length
!= ifd
->length
) {
1239 "isoc write: frame %d[%d/%d]: %s\n",
1240 i
, ifd
->actual_length
,
1242 get_usb_statmsg(ifd
->status
));
1247 case -EPIPE
: /* stall - probably underrun */
1248 dev_err(cs
->dev
, "isoc write: stalled\n");
1251 default: /* other errors */
1252 dev_warn(cs
->dev
, "isoc write: %s\n",
1253 get_usb_statmsg(status
));
1256 /* mark the write buffer area covered by this URB as free */
1257 if (done
->limit
>= 0)
1258 ubc
->isooutbuf
->read
= done
->limit
;
1260 /* mark URB as free */
1261 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1262 next
= ubc
->isooutfree
;
1263 ubc
->isooutfree
= done
;
1264 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1266 /* only one URB still active - resubmit one */
1267 rc
= submit_iso_write_urb(next
);
1268 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1269 /* couldn't submit */
1275 /* process queued SKBs */
1276 while ((skb
= skb_dequeue(&bcs
->squeue
))) {
1277 /* copy to output buffer, doing L2 encapsulation */
1279 if (gigaset_isoc_buildframe(bcs
, skb
->data
, len
) == -EAGAIN
) {
1280 /* insufficient buffer space, push back onto queue */
1281 skb_queue_head(&bcs
->squeue
, skb
);
1282 gig_dbg(DEBUG_ISO
, "%s: skb requeued, qlen=%d",
1283 __func__
, skb_queue_len(&bcs
->squeue
));
1287 gigaset_skb_sent(bcs
, skb
);
1288 dev_kfree_skb_any(skb
);
1292 /* Isochronous Read - Bottom Half */
1293 /* ============================== */
1296 * tasklet scheduled when an isochronous input URB from the Gigaset device
1299 * data B channel state structure
1301 static void read_iso_tasklet(unsigned long data
)
1303 struct bc_state
*bcs
= (struct bc_state
*) data
;
1304 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1305 struct cardstate
*cs
= bcs
->cs
;
1308 struct usb_iso_packet_descriptor
*ifd
;
1310 unsigned long flags
;
1311 int totleft
, numbytes
, offset
, frame
, rc
;
1313 /* loop while more completed URBs arrive in the meantime */
1316 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
1317 urb
= ubc
->isoindone
;
1319 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1322 status
= ubc
->isoinstatus
;
1323 ubc
->isoindone
= NULL
;
1324 if (unlikely(ubc
->loststatus
!= -EINPROGRESS
)) {
1326 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
1327 get_usb_statmsg(ubc
->loststatus
),
1329 ubc
->loststatus
= -EINPROGRESS
;
1331 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1333 if (unlikely(!(ubc
->running
))) {
1335 "%s: channel not running, "
1336 "dropped URB with status: %s",
1337 __func__
, get_usb_statmsg(status
));
1342 case 0: /* normal completion */
1344 case -EXDEV
: /* inspect individual frames
1345 (we do that anyway) */
1346 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1352 gig_dbg(DEBUG_ISO
, "%s: %s",
1353 __func__
, get_usb_statmsg(status
));
1354 continue; /* -> skip */
1356 dev_err(cs
->dev
, "isoc read: stalled\n");
1358 continue; /* -> skip */
1359 default: /* other error */
1360 dev_warn(cs
->dev
, "isoc read: %s\n",
1361 get_usb_statmsg(status
));
1365 rcvbuf
= urb
->transfer_buffer
;
1366 totleft
= urb
->actual_length
;
1367 for (frame
= 0; totleft
> 0 && frame
< BAS_NUMFRAMES
; frame
++) {
1368 ifd
= &urb
->iso_frame_desc
[frame
];
1369 numbytes
= ifd
->actual_length
;
1370 switch (ifd
->status
) {
1371 case 0: /* success */
1373 case -EPROTO
: /* protocol error or unplug */
1376 /* probably just disconnected, ignore */
1378 "isoc read: frame %d[%d]: %s\n",
1380 get_usb_statmsg(ifd
->status
));
1382 default: /* other error */
1383 /* report, assume transferred bytes are ok */
1385 "isoc read: frame %d[%d]: %s\n",
1387 get_usb_statmsg(ifd
->status
));
1389 if (unlikely(numbytes
> BAS_MAXFRAME
))
1391 "isoc read: frame %d[%d]: %s\n",
1393 "exceeds max frame size");
1394 if (unlikely(numbytes
> totleft
)) {
1396 "isoc read: frame %d[%d]: %s\n",
1398 "exceeds total transfer length");
1401 offset
= ifd
->offset
;
1402 if (unlikely(offset
+ numbytes
> BAS_INBUFSIZE
)) {
1404 "isoc read: frame %d[%d]: %s\n",
1406 "exceeds end of buffer");
1407 numbytes
= BAS_INBUFSIZE
- offset
;
1409 gigaset_isoc_receive(rcvbuf
+ offset
, numbytes
, bcs
);
1410 totleft
-= numbytes
;
1412 if (unlikely(totleft
> 0))
1413 dev_warn(cs
->dev
, "isoc read: %d data bytes missing\n",
1417 /* URB processed, resubmit */
1418 for (frame
= 0; frame
< BAS_NUMFRAMES
; frame
++) {
1419 urb
->iso_frame_desc
[frame
].status
= 0;
1420 urb
->iso_frame_desc
[frame
].actual_length
= 0;
1422 /* urb->dev is clobbered by USB subsystem */
1423 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1424 urb
->transfer_flags
= URB_ISO_ASAP
;
1425 urb
->number_of_packets
= BAS_NUMFRAMES
;
1426 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1427 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
1429 "could not resubmit isoc read URB: %s\n",
1431 dump_urb(DEBUG_ISO
, "resubmit isoc read", urb
);
1437 /* Channel Operations */
1438 /* ================== */
1441 * timeout routine for control output request
1443 * controller state structure
1445 static void req_timeout(struct timer_list
*t
)
1447 struct bas_cardstate
*ucs
= from_timer(ucs
, t
, timer_ctrl
);
1448 struct cardstate
*cs
= ucs
->cs
;
1450 unsigned long flags
;
1454 spin_lock_irqsave(&ucs
->lock
, flags
);
1455 pending
= ucs
->pending
;
1457 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1460 case 0: /* no pending request */
1461 gig_dbg(DEBUG_USBREQ
, "%s: no request pending", __func__
);
1464 case HD_OPEN_ATCHANNEL
:
1465 dev_err(cs
->dev
, "timeout opening AT channel\n");
1469 case HD_OPEN_B1CHANNEL
:
1470 dev_err(cs
->dev
, "timeout opening channel 1\n");
1471 error_hangup(&cs
->bcs
[0]);
1474 case HD_OPEN_B2CHANNEL
:
1475 dev_err(cs
->dev
, "timeout opening channel 2\n");
1476 error_hangup(&cs
->bcs
[1]);
1479 case HD_CLOSE_ATCHANNEL
:
1480 dev_err(cs
->dev
, "timeout closing AT channel\n");
1484 case HD_CLOSE_B1CHANNEL
:
1485 dev_err(cs
->dev
, "timeout closing channel 1\n");
1489 case HD_CLOSE_B2CHANNEL
:
1490 dev_err(cs
->dev
, "timeout closing channel 2\n");
1494 case HD_RESET_INTERRUPT_PIPE
:
1495 /* error recovery escalation */
1497 "reset interrupt pipe timeout, attempting USB reset\n");
1498 usb_queue_reset_device(ucs
->interface
);
1502 dev_warn(cs
->dev
, "request 0x%02x timed out, clearing\n",
1506 wake_up(&ucs
->waitqueue
);
1509 /* write_ctrl_callback
1510 * USB completion handler for control pipe output
1511 * called by the USB subsystem in interrupt context
1513 * urb USB request block of completed request
1514 * urb->context = hardware specific controller state structure
1516 static void write_ctrl_callback(struct urb
*urb
)
1518 struct bas_cardstate
*ucs
= urb
->context
;
1519 int status
= urb
->status
;
1521 unsigned long flags
;
1525 case 0: /* normal completion */
1526 spin_lock_irqsave(&ucs
->lock
, flags
);
1527 switch (ucs
->pending
) {
1528 case HD_DEVICE_INIT_ACK
: /* no reply expected */
1529 del_timer(&ucs
->timer_ctrl
);
1533 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1536 case -ENOENT
: /* cancelled */
1537 case -ECONNRESET
: /* cancelled (async) */
1538 case -EINPROGRESS
: /* pending */
1539 case -ENODEV
: /* device removed */
1540 case -ESHUTDOWN
: /* device shut down */
1541 /* ignore silently */
1542 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1543 __func__
, get_usb_statmsg(status
));
1546 default: /* any failure */
1547 /* don't retry if suspend requested */
1548 if (++ucs
->retry_ctrl
> BAS_RETRY
||
1549 (ucs
->basstate
& BS_SUSPEND
)) {
1550 dev_err(&ucs
->interface
->dev
,
1551 "control request 0x%02x failed: %s\n",
1552 ucs
->dr_ctrl
.bRequest
,
1553 get_usb_statmsg(status
));
1554 break; /* give up */
1556 dev_notice(&ucs
->interface
->dev
,
1557 "control request 0x%02x: %s, retry %d\n",
1558 ucs
->dr_ctrl
.bRequest
, get_usb_statmsg(status
),
1560 /* urb->dev is clobbered by USB subsystem */
1561 urb
->dev
= ucs
->udev
;
1562 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1564 dev_err(&ucs
->interface
->dev
,
1565 "could not resubmit request 0x%02x: %s\n",
1566 ucs
->dr_ctrl
.bRequest
, get_usb_rcmsg(rc
));
1573 /* failed, clear pending request */
1574 spin_lock_irqsave(&ucs
->lock
, flags
);
1575 del_timer(&ucs
->timer_ctrl
);
1577 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1578 wake_up(&ucs
->waitqueue
);
1582 * submit a control output request without message buffer to the Gigaset base
1583 * and optionally start a timeout
1585 * bcs B channel control structure
1586 * req control request code (HD_*)
1587 * val control request parameter value (set to 0 if unused)
1588 * timeout timeout in seconds (0: no timeout)
1591 * -EBUSY if another request is pending
1592 * any URB submission error code
1594 static int req_submit(struct bc_state
*bcs
, int req
, int val
, int timeout
)
1596 struct bas_cardstate
*ucs
= bcs
->cs
->hw
.bas
;
1598 unsigned long flags
;
1600 gig_dbg(DEBUG_USBREQ
, "-------> 0x%02x (%d)", req
, val
);
1602 spin_lock_irqsave(&ucs
->lock
, flags
);
1604 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1605 dev_err(bcs
->cs
->dev
,
1606 "submission of request 0x%02x failed: "
1607 "request 0x%02x still pending\n",
1612 ucs
->dr_ctrl
.bRequestType
= OUT_VENDOR_REQ
;
1613 ucs
->dr_ctrl
.bRequest
= req
;
1614 ucs
->dr_ctrl
.wValue
= cpu_to_le16(val
);
1615 ucs
->dr_ctrl
.wIndex
= 0;
1616 ucs
->dr_ctrl
.wLength
= 0;
1617 usb_fill_control_urb(ucs
->urb_ctrl
, ucs
->udev
,
1618 usb_sndctrlpipe(ucs
->udev
, 0),
1619 (unsigned char *) &ucs
->dr_ctrl
, NULL
, 0,
1620 write_ctrl_callback
, ucs
);
1621 ucs
->retry_ctrl
= 0;
1622 ret
= usb_submit_urb(ucs
->urb_ctrl
, GFP_ATOMIC
);
1623 if (unlikely(ret
)) {
1624 dev_err(bcs
->cs
->dev
, "could not submit request 0x%02x: %s\n",
1625 req
, get_usb_rcmsg(ret
));
1626 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1632 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
1633 mod_timer(&ucs
->timer_ctrl
, jiffies
+ timeout
* HZ
/ 10);
1636 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1640 /* gigaset_init_bchannel
1641 * called by common.c to connect a B channel
1642 * initialize isochronous I/O and tell the Gigaset base to open the channel
1644 * B channel control structure
1646 * 0 on success, error code < 0 on error
1648 static int gigaset_init_bchannel(struct bc_state
*bcs
)
1650 struct cardstate
*cs
= bcs
->cs
;
1652 unsigned long flags
;
1654 spin_lock_irqsave(&cs
->lock
, flags
);
1655 if (unlikely(!cs
->connected
)) {
1656 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1657 spin_unlock_irqrestore(&cs
->lock
, flags
);
1661 if (cs
->hw
.bas
->basstate
& BS_SUSPEND
) {
1663 "not starting isoc I/O, suspend in progress\n");
1664 spin_unlock_irqrestore(&cs
->lock
, flags
);
1665 return -EHOSTUNREACH
;
1668 ret
= starturbs(bcs
);
1670 spin_unlock_irqrestore(&cs
->lock
, flags
);
1672 "could not start isoc I/O for channel B%d: %s\n",
1674 ret
== -EFAULT
? "null URB" : get_usb_rcmsg(ret
));
1680 req
= bcs
->channel
? HD_OPEN_B2CHANNEL
: HD_OPEN_B1CHANNEL
;
1681 ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
);
1683 dev_err(cs
->dev
, "could not open channel B%d\n",
1685 stopurbs(bcs
->hw
.bas
);
1688 spin_unlock_irqrestore(&cs
->lock
, flags
);
1689 if (ret
< 0 && ret
!= -ENODEV
)
1694 /* gigaset_close_bchannel
1695 * called by common.c to disconnect a B channel
1696 * tell the Gigaset base to close the channel
1697 * stopping isochronous I/O and LL notification will be done when the
1698 * acknowledgement for the close arrives
1700 * B channel control structure
1702 * 0 on success, error code < 0 on error
1704 static int gigaset_close_bchannel(struct bc_state
*bcs
)
1706 struct cardstate
*cs
= bcs
->cs
;
1708 unsigned long flags
;
1710 spin_lock_irqsave(&cs
->lock
, flags
);
1711 if (unlikely(!cs
->connected
)) {
1712 spin_unlock_irqrestore(&cs
->lock
, flags
);
1713 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1717 if (!(cs
->hw
.bas
->basstate
& (bcs
->channel
? BS_B2OPEN
: BS_B1OPEN
))) {
1718 /* channel not running: just signal common.c */
1719 spin_unlock_irqrestore(&cs
->lock
, flags
);
1720 gigaset_bchannel_down(bcs
);
1724 /* channel running: tell device to close it */
1725 req
= bcs
->channel
? HD_CLOSE_B2CHANNEL
: HD_CLOSE_B1CHANNEL
;
1726 ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
);
1728 dev_err(cs
->dev
, "closing channel B%d failed\n",
1731 spin_unlock_irqrestore(&cs
->lock
, flags
);
1735 /* Device Operations */
1736 /* ================= */
1739 * unqueue first command buffer from queue, waking any sleepers
1740 * must be called with cs->cmdlock held
1742 * cs controller state structure
1744 static void complete_cb(struct cardstate
*cs
)
1746 struct cmdbuf_t
*cb
= cs
->cmdbuf
;
1748 /* unqueue completed buffer */
1749 cs
->cmdbytes
-= cs
->curlen
;
1750 gig_dbg(DEBUG_OUTPUT
, "write_command: sent %u bytes, %u left",
1751 cs
->curlen
, cs
->cmdbytes
);
1752 if (cb
->next
!= NULL
) {
1753 cs
->cmdbuf
= cb
->next
;
1754 cs
->cmdbuf
->prev
= NULL
;
1755 cs
->curlen
= cs
->cmdbuf
->len
;
1758 cs
->lastcmdbuf
= NULL
;
1762 if (cb
->wake_tasklet
)
1763 tasklet_schedule(cb
->wake_tasklet
);
1768 /* write_command_callback
1769 * USB completion handler for AT command transmission
1770 * called by the USB subsystem in interrupt context
1772 * urb USB request block of completed request
1773 * urb->context = controller state structure
1775 static void write_command_callback(struct urb
*urb
)
1777 struct cardstate
*cs
= urb
->context
;
1778 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1779 int status
= urb
->status
;
1780 unsigned long flags
;
1782 update_basstate(ucs
, 0, BS_ATWRPEND
);
1783 wake_up(&ucs
->waitqueue
);
1787 case 0: /* normal completion */
1789 case -ENOENT
: /* cancelled */
1790 case -ECONNRESET
: /* cancelled (async) */
1791 case -EINPROGRESS
: /* pending */
1792 case -ENODEV
: /* device removed */
1793 case -ESHUTDOWN
: /* device shut down */
1794 /* ignore silently */
1795 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1796 __func__
, get_usb_statmsg(status
));
1798 default: /* any failure */
1799 if (++ucs
->retry_cmd_out
> BAS_RETRY
) {
1801 "command write: %s, "
1802 "giving up after %d retries\n",
1803 get_usb_statmsg(status
),
1804 ucs
->retry_cmd_out
);
1807 if (ucs
->basstate
& BS_SUSPEND
) {
1809 "command write: %s, "
1810 "won't retry - suspend requested\n",
1811 get_usb_statmsg(status
));
1814 if (cs
->cmdbuf
== NULL
) {
1816 "command write: %s, "
1817 "cannot retry - cmdbuf gone\n",
1818 get_usb_statmsg(status
));
1821 dev_notice(cs
->dev
, "command write: %s, retry %d\n",
1822 get_usb_statmsg(status
), ucs
->retry_cmd_out
);
1823 if (atwrite_submit(cs
, cs
->cmdbuf
->buf
, cs
->cmdbuf
->len
) >= 0)
1824 /* resubmitted - bypass regular exit block */
1826 /* command send failed, assume base still waiting */
1827 update_basstate(ucs
, BS_ATREADY
, 0);
1830 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1831 if (cs
->cmdbuf
!= NULL
)
1833 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1837 * timeout routine for AT command transmission
1839 * controller state structure
1841 static void atrdy_timeout(struct timer_list
*t
)
1843 struct bas_cardstate
*ucs
= from_timer(ucs
, t
, timer_atrdy
);
1844 struct cardstate
*cs
= ucs
->cs
;
1846 dev_warn(cs
->dev
, "timeout waiting for HD_READY_SEND_ATDATA\n");
1848 /* fake the missing signal - what else can I do? */
1849 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
1854 * submit an HD_WRITE_ATMESSAGE command URB
1856 * cs controller state structure
1857 * buf buffer containing command to send
1858 * len length of command to send
1861 * -EBUSY if another request is pending
1862 * any URB submission error code
1864 static int atwrite_submit(struct cardstate
*cs
, unsigned char *buf
, int len
)
1866 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1869 gig_dbg(DEBUG_USBREQ
, "-------> HD_WRITE_ATMESSAGE (%d)", len
);
1871 if (update_basstate(ucs
, BS_ATWRPEND
, 0) & BS_ATWRPEND
) {
1873 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1877 ucs
->dr_cmd_out
.bRequestType
= OUT_VENDOR_REQ
;
1878 ucs
->dr_cmd_out
.bRequest
= HD_WRITE_ATMESSAGE
;
1879 ucs
->dr_cmd_out
.wValue
= 0;
1880 ucs
->dr_cmd_out
.wIndex
= 0;
1881 ucs
->dr_cmd_out
.wLength
= cpu_to_le16(len
);
1882 usb_fill_control_urb(ucs
->urb_cmd_out
, ucs
->udev
,
1883 usb_sndctrlpipe(ucs
->udev
, 0),
1884 (unsigned char *) &ucs
->dr_cmd_out
, buf
, len
,
1885 write_command_callback
, cs
);
1886 rc
= usb_submit_urb(ucs
->urb_cmd_out
, GFP_ATOMIC
);
1888 update_basstate(ucs
, 0, BS_ATWRPEND
);
1889 dev_err(cs
->dev
, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1894 /* submitted successfully, start timeout if necessary */
1895 if (!(update_basstate(ucs
, BS_ATTIMER
, BS_ATREADY
) & BS_ATTIMER
)) {
1896 gig_dbg(DEBUG_OUTPUT
, "setting ATREADY timeout of %d/10 secs",
1898 mod_timer(&ucs
->timer_atrdy
, jiffies
+ ATRDY_TIMEOUT
* HZ
/ 10);
1904 * start transmission of AT command queue if necessary
1906 * cs controller state structure
1909 * error code < 0 on error
1911 static int start_cbsend(struct cardstate
*cs
)
1913 struct cmdbuf_t
*cb
;
1914 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1915 unsigned long flags
;
1919 /* check if suspend requested */
1920 if (ucs
->basstate
& BS_SUSPEND
) {
1921 gig_dbg(DEBUG_OUTPUT
, "suspending");
1922 return -EHOSTUNREACH
;
1925 /* check if AT channel is open */
1926 if (!(ucs
->basstate
& BS_ATOPEN
)) {
1927 gig_dbg(DEBUG_OUTPUT
, "AT channel not open");
1928 rc
= req_submit(cs
->bcs
, HD_OPEN_ATCHANNEL
, 0, BAS_TIMEOUT
);
1930 /* flush command queue */
1931 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1932 while (cs
->cmdbuf
!= NULL
)
1934 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1939 /* try to send first command in queue */
1940 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1942 while ((cb
= cs
->cmdbuf
) != NULL
&& (ucs
->basstate
& BS_ATREADY
)) {
1943 ucs
->retry_cmd_out
= 0;
1944 rc
= atwrite_submit(cs
, cb
->buf
, cb
->len
);
1951 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1955 /* gigaset_write_cmd
1956 * This function is called by the device independent part of the driver
1957 * to transmit an AT command string to the Gigaset device.
1958 * It encapsulates the device specific method for transmission over the
1959 * direct USB connection to the base.
1960 * The command string is added to the queue of commands to send, and
1961 * USB transmission is started if necessary.
1963 * cs controller state structure
1964 * cb command buffer structure
1966 * number of bytes queued on success
1967 * error code < 0 on error
1969 static int gigaset_write_cmd(struct cardstate
*cs
, struct cmdbuf_t
*cb
)
1971 unsigned long flags
;
1974 gigaset_dbg_buffer(cs
->mstate
!= MS_LOCKED
?
1975 DEBUG_TRANSCMD
: DEBUG_LOCKCMD
,
1976 "CMD Transmit", cb
->len
, cb
->buf
);
1978 /* translate "+++" escape sequence sent as a single separate command
1979 * into "close AT channel" command for error recovery
1980 * The next command will reopen the AT channel automatically.
1982 if (cb
->len
== 3 && !memcmp(cb
->buf
, "+++", 3)) {
1983 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1984 * because of an error, the base never sends another one.
1985 * The response channel is thus effectively blocked.
1986 * Closing and reopening the AT channel does *not* clear
1988 * As a stopgap measure, submit a zero-length AT read
1989 * before closing the AT channel. This has the undocumented
1990 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1991 * from the base if necessary.
1992 * The subsequent AT channel close then discards any pending
1995 spin_lock_irqsave(&cs
->lock
, flags
);
1996 if (!(cs
->hw
.bas
->basstate
& BS_ATRDPEND
)) {
1997 kfree(cs
->hw
.bas
->rcvbuf
);
1998 cs
->hw
.bas
->rcvbuf
= NULL
;
1999 cs
->hw
.bas
->rcvbuf_size
= 0;
2000 cs
->hw
.bas
->retry_cmd_in
= 0;
2001 atread_submit(cs
, 0);
2003 spin_unlock_irqrestore(&cs
->lock
, flags
);
2005 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, BAS_TIMEOUT
);
2006 if (cb
->wake_tasklet
)
2007 tasklet_schedule(cb
->wake_tasklet
);
2014 spin_lock_irqsave(&cs
->cmdlock
, flags
);
2015 cb
->prev
= cs
->lastcmdbuf
;
2017 cs
->lastcmdbuf
->next
= cb
;
2020 cs
->curlen
= cb
->len
;
2022 cs
->cmdbytes
+= cb
->len
;
2023 cs
->lastcmdbuf
= cb
;
2024 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
2026 spin_lock_irqsave(&cs
->lock
, flags
);
2027 if (unlikely(!cs
->connected
)) {
2028 spin_unlock_irqrestore(&cs
->lock
, flags
);
2029 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
2030 /* flush command queue */
2031 spin_lock_irqsave(&cs
->cmdlock
, flags
);
2032 while (cs
->cmdbuf
!= NULL
)
2034 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
2037 rc
= start_cbsend(cs
);
2038 spin_unlock_irqrestore(&cs
->lock
, flags
);
2039 return rc
< 0 ? rc
: cb
->len
;
2042 /* gigaset_write_room
2043 * tty_driver.write_room interface routine
2044 * return number of characters the driver will accept to be written via
2047 * controller state structure
2049 * number of characters
2051 static int gigaset_write_room(struct cardstate
*cs
)
2056 /* gigaset_chars_in_buffer
2057 * tty_driver.chars_in_buffer interface routine
2058 * return number of characters waiting to be sent
2060 * controller state structure
2062 * number of characters
2064 static int gigaset_chars_in_buffer(struct cardstate
*cs
)
2066 return cs
->cmdbytes
;
2070 * implementation of ioctl(GIGASET_BRKCHARS)
2072 * controller state structure
2074 * -EINVAL (unimplemented function)
2076 static int gigaset_brkchars(struct cardstate
*cs
, const unsigned char buf
[6])
2082 /* Device Initialization/Shutdown */
2083 /* ============================== */
2085 /* Free hardware dependent part of the B channel structure
2087 * bcs B channel structure
2089 static void gigaset_freebcshw(struct bc_state
*bcs
)
2091 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2097 /* kill URBs and tasklets before freeing - better safe than sorry */
2099 gig_dbg(DEBUG_INIT
, "%s: killing isoc URBs", __func__
);
2100 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2101 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2102 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2104 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2105 usb_kill_urb(ubc
->isoinurbs
[i
]);
2106 usb_free_urb(ubc
->isoinurbs
[i
]);
2108 tasklet_kill(&ubc
->sent_tasklet
);
2109 tasklet_kill(&ubc
->rcvd_tasklet
);
2110 kfree(ubc
->isooutbuf
);
2115 /* Initialize hardware dependent part of the B channel structure
2117 * bcs B channel structure
2119 * 0 on success, error code < 0 on failure
2121 static int gigaset_initbcshw(struct bc_state
*bcs
)
2124 struct bas_bc_state
*ubc
;
2126 bcs
->hw
.bas
= ubc
= kmalloc(sizeof(struct bas_bc_state
), GFP_KERNEL
);
2128 pr_err("out of memory\n");
2133 atomic_set(&ubc
->corrbytes
, 0);
2134 spin_lock_init(&ubc
->isooutlock
);
2135 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2136 ubc
->isoouturbs
[i
].urb
= NULL
;
2137 ubc
->isoouturbs
[i
].bcs
= bcs
;
2139 ubc
->isooutdone
= ubc
->isooutfree
= ubc
->isooutovfl
= NULL
;
2141 ubc
->isooutbuf
= kmalloc(sizeof(struct isowbuf_t
), GFP_KERNEL
);
2142 if (!ubc
->isooutbuf
) {
2143 pr_err("out of memory\n");
2148 tasklet_init(&ubc
->sent_tasklet
,
2149 write_iso_tasklet
, (unsigned long) bcs
);
2151 spin_lock_init(&ubc
->isoinlock
);
2152 for (i
= 0; i
< BAS_INURBS
; ++i
)
2153 ubc
->isoinurbs
[i
] = NULL
;
2154 ubc
->isoindone
= NULL
;
2155 ubc
->loststatus
= -EINPROGRESS
;
2169 tasklet_init(&ubc
->rcvd_tasklet
,
2170 read_iso_tasklet
, (unsigned long) bcs
);
2174 static void gigaset_reinitbcshw(struct bc_state
*bcs
)
2176 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2178 bcs
->hw
.bas
->running
= 0;
2179 atomic_set(&bcs
->hw
.bas
->corrbytes
, 0);
2180 bcs
->hw
.bas
->numsub
= 0;
2181 spin_lock_init(&ubc
->isooutlock
);
2182 spin_lock_init(&ubc
->isoinlock
);
2183 ubc
->loststatus
= -EINPROGRESS
;
2186 static void gigaset_freecshw(struct cardstate
*cs
)
2188 /* timers, URBs and rcvbuf are disposed of in disconnect */
2189 kfree(cs
->hw
.bas
->int_in_buf
);
2194 /* Initialize hardware dependent part of the cardstate structure
2196 * cs cardstate structure
2198 * 0 on success, error code < 0 on failure
2200 static int gigaset_initcshw(struct cardstate
*cs
)
2202 struct bas_cardstate
*ucs
;
2204 cs
->hw
.bas
= ucs
= kzalloc(sizeof(*ucs
), GFP_KERNEL
);
2206 pr_err("out of memory\n");
2209 ucs
->int_in_buf
= kmalloc(IP_MSGSIZE
, GFP_KERNEL
);
2210 if (!ucs
->int_in_buf
) {
2212 pr_err("out of memory\n");
2216 spin_lock_init(&ucs
->lock
);
2218 timer_setup(&ucs
->timer_ctrl
, req_timeout
, 0);
2219 timer_setup(&ucs
->timer_atrdy
, atrdy_timeout
, 0);
2220 timer_setup(&ucs
->timer_cmd_in
, cmd_in_timeout
, 0);
2221 timer_setup(&ucs
->timer_int_in
, int_in_resubmit
, 0);
2222 init_waitqueue_head(&ucs
->waitqueue
);
2223 INIT_WORK(&ucs
->int_in_wq
, int_in_work
);
2229 * unlink and deallocate all URBs unconditionally
2230 * caller must make sure that no commands are still in progress
2232 * cs controller state structure
2234 static void freeurbs(struct cardstate
*cs
)
2236 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2237 struct bas_bc_state
*ubc
;
2240 gig_dbg(DEBUG_INIT
, "%s: killing URBs", __func__
);
2241 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2242 ubc
= cs
->bcs
[j
].hw
.bas
;
2243 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2244 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2245 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2246 ubc
->isoouturbs
[i
].urb
= NULL
;
2248 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2249 usb_kill_urb(ubc
->isoinurbs
[i
]);
2250 usb_free_urb(ubc
->isoinurbs
[i
]);
2251 ubc
->isoinurbs
[i
] = NULL
;
2254 usb_kill_urb(ucs
->urb_int_in
);
2255 usb_free_urb(ucs
->urb_int_in
);
2256 ucs
->urb_int_in
= NULL
;
2257 usb_kill_urb(ucs
->urb_cmd_out
);
2258 usb_free_urb(ucs
->urb_cmd_out
);
2259 ucs
->urb_cmd_out
= NULL
;
2260 usb_kill_urb(ucs
->urb_cmd_in
);
2261 usb_free_urb(ucs
->urb_cmd_in
);
2262 ucs
->urb_cmd_in
= NULL
;
2263 usb_kill_urb(ucs
->urb_ctrl
);
2264 usb_free_urb(ucs
->urb_ctrl
);
2265 ucs
->urb_ctrl
= NULL
;
2269 * This function is called when a new USB device is connected.
2270 * It checks whether the new device is handled by this driver.
2272 static int gigaset_probe(struct usb_interface
*interface
,
2273 const struct usb_device_id
*id
)
2275 struct usb_host_interface
*hostif
;
2276 struct usb_device
*udev
= interface_to_usbdev(interface
);
2277 struct cardstate
*cs
= NULL
;
2278 struct bas_cardstate
*ucs
= NULL
;
2279 struct bas_bc_state
*ubc
;
2280 struct usb_endpoint_descriptor
*endpoint
;
2285 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2286 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2287 le16_to_cpu(udev
->descriptor
.idProduct
));
2289 /* set required alternate setting */
2290 hostif
= interface
->cur_altsetting
;
2291 if (hostif
->desc
.bAlternateSetting
!= 3) {
2293 "%s: wrong alternate setting %d - trying to switch",
2294 __func__
, hostif
->desc
.bAlternateSetting
);
2295 if (usb_set_interface(udev
, hostif
->desc
.bInterfaceNumber
, 3)
2297 dev_warn(&udev
->dev
, "usb_set_interface failed, "
2298 "device %d interface %d altsetting %d\n",
2299 udev
->devnum
, hostif
->desc
.bInterfaceNumber
,
2300 hostif
->desc
.bAlternateSetting
);
2303 hostif
= interface
->cur_altsetting
;
2306 /* Reject application specific interfaces
2308 if (hostif
->desc
.bInterfaceClass
!= 255) {
2309 dev_warn(&udev
->dev
, "%s: bInterfaceClass == %d\n",
2310 __func__
, hostif
->desc
.bInterfaceClass
);
2314 if (hostif
->desc
.bNumEndpoints
< 1)
2317 dev_info(&udev
->dev
,
2318 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2319 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2320 le16_to_cpu(udev
->descriptor
.idProduct
));
2322 /* allocate memory for our device state and initialize it */
2323 cs
= gigaset_initcs(driver
, BAS_CHANNELS
, 0, 0, cidmode
,
2324 GIGASET_MODULENAME
);
2329 /* save off device structure ptrs for later use */
2332 ucs
->interface
= interface
;
2333 cs
->dev
= &interface
->dev
;
2336 * - one for the interrupt pipe
2337 * - three for the different uses of the default control pipe
2338 * - three for each isochronous pipe
2340 if (!(ucs
->urb_int_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2341 !(ucs
->urb_cmd_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2342 !(ucs
->urb_cmd_out
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2343 !(ucs
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
)))
2346 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2347 ubc
= cs
->bcs
[j
].hw
.bas
;
2348 for (i
= 0; i
< BAS_OUTURBS
; ++i
)
2349 if (!(ubc
->isoouturbs
[i
].urb
=
2350 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2352 for (i
= 0; i
< BAS_INURBS
; ++i
)
2353 if (!(ubc
->isoinurbs
[i
] =
2354 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2359 ucs
->rcvbuf_size
= 0;
2361 /* Fill the interrupt urb and send it to the core */
2362 endpoint
= &hostif
->endpoint
[0].desc
;
2363 usb_fill_int_urb(ucs
->urb_int_in
, udev
,
2364 usb_rcvintpipe(udev
,
2365 usb_endpoint_num(endpoint
)),
2366 ucs
->int_in_buf
, IP_MSGSIZE
, read_int_callback
, cs
,
2367 endpoint
->bInterval
);
2368 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2370 dev_err(cs
->dev
, "could not submit interrupt URB: %s\n",
2374 ucs
->retry_int_in
= 0;
2376 /* tell the device that the driver is ready */
2377 rc
= req_submit(cs
->bcs
, HD_DEVICE_INIT_ACK
, 0, 0);
2381 /* tell common part that the device is ready */
2382 if (startmode
== SM_LOCKED
)
2383 cs
->mstate
= MS_LOCKED
;
2385 /* save address of controller structure */
2386 usb_set_intfdata(interface
, cs
);
2388 rc
= gigaset_start(cs
);
2395 dev_err(cs
->dev
, "could not allocate URBs\n");
2399 usb_set_intfdata(interface
, NULL
);
2405 /* gigaset_disconnect
2406 * This function is called when the Gigaset base is unplugged.
2408 static void gigaset_disconnect(struct usb_interface
*interface
)
2410 struct cardstate
*cs
;
2411 struct bas_cardstate
*ucs
;
2414 cs
= usb_get_intfdata(interface
);
2418 dev_info(cs
->dev
, "disconnecting Gigaset base\n");
2420 /* mark base as not ready, all channels disconnected */
2423 /* tell LL all channels are down */
2424 for (j
= 0; j
< BAS_CHANNELS
; ++j
)
2425 gigaset_bchannel_down(cs
->bcs
+ j
);
2427 /* stop driver (common part) */
2430 /* stop delayed work and URBs, free ressources */
2431 del_timer_sync(&ucs
->timer_ctrl
);
2432 del_timer_sync(&ucs
->timer_atrdy
);
2433 del_timer_sync(&ucs
->timer_cmd_in
);
2434 del_timer_sync(&ucs
->timer_int_in
);
2435 cancel_work_sync(&ucs
->int_in_wq
);
2437 usb_set_intfdata(interface
, NULL
);
2440 ucs
->rcvbuf_size
= 0;
2441 usb_put_dev(ucs
->udev
);
2442 ucs
->interface
= NULL
;
2449 * This function is called before the USB connection is suspended
2450 * or before the USB device is reset.
2451 * In the latter case, message == PMSG_ON.
2453 static int gigaset_suspend(struct usb_interface
*intf
, pm_message_t message
)
2455 struct cardstate
*cs
= usb_get_intfdata(intf
);
2456 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2459 /* set suspend flag; this stops AT command/response traffic */
2460 if (update_basstate(ucs
, BS_SUSPEND
, 0) & BS_SUSPEND
) {
2461 gig_dbg(DEBUG_SUSPEND
, "already suspended");
2465 /* wait a bit for blocking conditions to go away */
2466 rc
= wait_event_timeout(ucs
->waitqueue
,
2468 (BS_B1OPEN
| BS_B2OPEN
| BS_ATRDPEND
| BS_ATWRPEND
)),
2469 BAS_TIMEOUT
* HZ
/ 10);
2470 gig_dbg(DEBUG_SUSPEND
, "wait_event_timeout() -> %d", rc
);
2472 /* check for conditions preventing suspend */
2473 if (ucs
->basstate
& (BS_B1OPEN
| BS_B2OPEN
| BS_ATRDPEND
| BS_ATWRPEND
)) {
2474 dev_warn(cs
->dev
, "cannot suspend:\n");
2475 if (ucs
->basstate
& BS_B1OPEN
)
2476 dev_warn(cs
->dev
, " B channel 1 open\n");
2477 if (ucs
->basstate
& BS_B2OPEN
)
2478 dev_warn(cs
->dev
, " B channel 2 open\n");
2479 if (ucs
->basstate
& BS_ATRDPEND
)
2480 dev_warn(cs
->dev
, " receiving AT reply\n");
2481 if (ucs
->basstate
& BS_ATWRPEND
)
2482 dev_warn(cs
->dev
, " sending AT command\n");
2483 update_basstate(ucs
, 0, BS_SUSPEND
);
2487 /* close AT channel if open */
2488 if (ucs
->basstate
& BS_ATOPEN
) {
2489 gig_dbg(DEBUG_SUSPEND
, "closing AT channel");
2490 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, 0);
2492 update_basstate(ucs
, 0, BS_SUSPEND
);
2495 wait_event_timeout(ucs
->waitqueue
, !ucs
->pending
,
2496 BAS_TIMEOUT
* HZ
/ 10);
2497 /* in case of timeout, proceed anyway */
2500 /* kill all URBs and delayed work that might still be pending */
2501 usb_kill_urb(ucs
->urb_ctrl
);
2502 usb_kill_urb(ucs
->urb_int_in
);
2503 del_timer_sync(&ucs
->timer_ctrl
);
2504 del_timer_sync(&ucs
->timer_atrdy
);
2505 del_timer_sync(&ucs
->timer_cmd_in
);
2506 del_timer_sync(&ucs
->timer_int_in
);
2508 /* don't try to cancel int_in_wq from within reset as it
2509 * might be the one requesting the reset
2511 if (message
.event
!= PM_EVENT_ON
)
2512 cancel_work_sync(&ucs
->int_in_wq
);
2514 gig_dbg(DEBUG_SUSPEND
, "suspend complete");
2519 * This function is called after the USB connection has been resumed.
2521 static int gigaset_resume(struct usb_interface
*intf
)
2523 struct cardstate
*cs
= usb_get_intfdata(intf
);
2524 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2527 /* resubmit interrupt URB for spontaneous messages from base */
2528 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2530 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
2534 ucs
->retry_int_in
= 0;
2536 /* clear suspend flag to reallow activity */
2537 update_basstate(ucs
, 0, BS_SUSPEND
);
2539 gig_dbg(DEBUG_SUSPEND
, "resume complete");
2543 /* gigaset_pre_reset
2544 * This function is called before the USB connection is reset.
2546 static int gigaset_pre_reset(struct usb_interface
*intf
)
2548 /* handle just like suspend */
2549 return gigaset_suspend(intf
, PMSG_ON
);
2552 /* gigaset_post_reset
2553 * This function is called after the USB connection has been reset.
2555 static int gigaset_post_reset(struct usb_interface
*intf
)
2557 /* FIXME: send HD_DEVICE_INIT_ACK? */
2559 /* resume operations */
2560 return gigaset_resume(intf
);
2564 static const struct gigaset_ops gigops
= {
2565 .write_cmd
= gigaset_write_cmd
,
2566 .write_room
= gigaset_write_room
,
2567 .chars_in_buffer
= gigaset_chars_in_buffer
,
2568 .brkchars
= gigaset_brkchars
,
2569 .init_bchannel
= gigaset_init_bchannel
,
2570 .close_bchannel
= gigaset_close_bchannel
,
2571 .initbcshw
= gigaset_initbcshw
,
2572 .freebcshw
= gigaset_freebcshw
,
2573 .reinitbcshw
= gigaset_reinitbcshw
,
2574 .initcshw
= gigaset_initcshw
,
2575 .freecshw
= gigaset_freecshw
,
2576 .set_modem_ctrl
= gigaset_set_modem_ctrl
,
2577 .baud_rate
= gigaset_baud_rate
,
2578 .set_line_ctrl
= gigaset_set_line_ctrl
,
2579 .send_skb
= gigaset_isoc_send_skb
,
2580 .handle_input
= gigaset_isoc_input
,
2584 * This function is called after the kernel module is loaded.
2586 static int __init
bas_gigaset_init(void)
2590 /* allocate memory for our driver state and initialize it */
2591 driver
= gigaset_initdriver(GIGASET_MINOR
, GIGASET_MINORS
,
2592 GIGASET_MODULENAME
, GIGASET_DEVNAME
,
2593 &gigops
, THIS_MODULE
);
2597 /* register this driver with the USB subsystem */
2598 result
= usb_register(&gigaset_usb_driver
);
2600 pr_err("error %d registering USB driver\n", -result
);
2604 pr_info(DRIVER_DESC
"\n");
2609 gigaset_freedriver(driver
);
2615 * This function is called before the kernel module is unloaded.
2617 static void __exit
bas_gigaset_exit(void)
2619 struct bas_cardstate
*ucs
;
2622 gigaset_blockdriver(driver
); /* => probe will fail
2623 * => no gigaset_start any more
2626 /* stop all connected devices */
2627 for (i
= 0; i
< driver
->minors
; i
++) {
2628 if (gigaset_shutdown(driver
->cs
+ i
) < 0)
2629 continue; /* no device */
2630 /* from now on, no isdn callback should be possible */
2632 /* close all still open channels */
2633 ucs
= driver
->cs
[i
].hw
.bas
;
2634 if (ucs
->basstate
& BS_B1OPEN
) {
2635 gig_dbg(DEBUG_INIT
, "closing B1 channel");
2636 usb_control_msg(ucs
->udev
,
2637 usb_sndctrlpipe(ucs
->udev
, 0),
2638 HD_CLOSE_B1CHANNEL
, OUT_VENDOR_REQ
,
2639 0, 0, NULL
, 0, BAS_TIMEOUT
);
2641 if (ucs
->basstate
& BS_B2OPEN
) {
2642 gig_dbg(DEBUG_INIT
, "closing B2 channel");
2643 usb_control_msg(ucs
->udev
,
2644 usb_sndctrlpipe(ucs
->udev
, 0),
2645 HD_CLOSE_B2CHANNEL
, OUT_VENDOR_REQ
,
2646 0, 0, NULL
, 0, BAS_TIMEOUT
);
2648 if (ucs
->basstate
& BS_ATOPEN
) {
2649 gig_dbg(DEBUG_INIT
, "closing AT channel");
2650 usb_control_msg(ucs
->udev
,
2651 usb_sndctrlpipe(ucs
->udev
, 0),
2652 HD_CLOSE_ATCHANNEL
, OUT_VENDOR_REQ
,
2653 0, 0, NULL
, 0, BAS_TIMEOUT
);
2658 /* deregister this driver with the USB subsystem */
2659 usb_deregister(&gigaset_usb_driver
);
2660 /* this will call the disconnect-callback */
2661 /* from now on, no disconnect/probe callback should be running */
2663 gigaset_freedriver(driver
);
2668 module_init(bas_gigaset_init
);
2669 module_exit(bas_gigaset_exit
);
2671 MODULE_AUTHOR(DRIVER_AUTHOR
);
2672 MODULE_DESCRIPTION(DRIVER_DESC
);
2673 MODULE_LICENSE("GPL");