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 */
92 struct usb_interface
*interface
; /* interface for this device */
93 unsigned char minor
; /* starting minor number */
95 struct urb
*urb_ctrl
; /* control pipe default URB */
96 struct usb_ctrlrequest dr_ctrl
;
97 struct timer_list timer_ctrl
; /* control request timeout */
100 struct timer_list timer_atrdy
; /* AT command ready timeout */
101 struct urb
*urb_cmd_out
; /* for sending AT commands */
102 struct usb_ctrlrequest dr_cmd_out
;
105 struct urb
*urb_cmd_in
; /* for receiving AT replies */
106 struct usb_ctrlrequest dr_cmd_in
;
107 struct timer_list timer_cmd_in
; /* receive request timeout */
108 unsigned char *rcvbuf
; /* AT reply receive buffer */
110 struct urb
*urb_int_in
; /* URB for interrupt pipe */
111 unsigned char *int_in_buf
;
112 struct work_struct int_in_wq
; /* for usb_clear_halt() */
113 struct timer_list timer_int_in
; /* int read retry delay */
116 spinlock_t lock
; /* locks all following */
117 int basstate
; /* bitmap (BS_*) */
118 int pending
; /* uncompleted base request */
119 wait_queue_head_t waitqueue
;
120 int rcvbuf_size
; /* size of AT receive buffer */
121 /* 0: no receive in progress */
122 int retry_cmd_in
; /* receive req retry count */
125 /* status of direct USB connection to 307x base (bits in basstate) */
126 #define BS_ATOPEN 0x001 /* AT channel open */
127 #define BS_B1OPEN 0x002 /* B channel 1 open */
128 #define BS_B2OPEN 0x004 /* B channel 2 open */
129 #define BS_ATREADY 0x008 /* base ready for AT command */
130 #define BS_INIT 0x010 /* base has signalled INIT_OK */
131 #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
132 #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
133 #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
134 #define BS_SUSPEND 0x100 /* USB port suspended */
135 #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
138 static struct gigaset_driver
*driver
;
140 /* usb specific object needed to register this driver with the usb subsystem */
141 static struct usb_driver gigaset_usb_driver
= {
142 .name
= GIGASET_MODULENAME
,
143 .probe
= gigaset_probe
,
144 .disconnect
= gigaset_disconnect
,
145 .id_table
= gigaset_table
,
146 .suspend
= gigaset_suspend
,
147 .resume
= gigaset_resume
,
148 .reset_resume
= gigaset_post_reset
,
149 .pre_reset
= gigaset_pre_reset
,
150 .post_reset
= gigaset_post_reset
,
151 .disable_hub_initiated_lpm
= 1,
154 /* get message text for usb_submit_urb return code
156 static char *get_usb_rcmsg(int rc
)
158 static char unkmsg
[28];
164 return "out of memory";
166 return "device not present";
168 return "endpoint not present";
170 return "URB type not supported";
172 return "invalid argument";
174 return "start frame too early or too much scheduled";
176 return "too many isoc frames requested";
178 return "endpoint stalled";
180 return "invalid packet size";
182 return "would overcommit USB bandwidth";
184 return "device shut down";
186 return "reject flag set";
188 return "device suspended";
190 snprintf(unkmsg
, sizeof(unkmsg
), "unknown error %d", rc
);
195 /* get message text for USB status code
197 static char *get_usb_statmsg(int status
)
199 static char unkmsg
[28];
205 return "unlinked (sync)";
207 return "URB still pending";
209 return "bitstuff error, timeout, or unknown USB error";
211 return "CRC mismatch, timeout, or unknown USB error";
213 return "USB response timeout";
215 return "endpoint stalled";
217 return "IN buffer overrun";
219 return "OUT buffer underrun";
221 return "endpoint babble";
223 return "short packet";
225 return "device removed";
227 return "partial isoc transfer";
229 return "ISO madness";
231 return "unlinked (async)";
233 return "device shut down";
235 snprintf(unkmsg
, sizeof(unkmsg
), "unknown status %d", status
);
241 * retrieve string representation of USB pipe type
243 static inline char *usb_pipetype_str(int pipe
)
245 if (usb_pipeisoc(pipe
))
247 if (usb_pipeint(pipe
))
249 if (usb_pipecontrol(pipe
))
251 if (usb_pipebulk(pipe
))
257 * write content of URB to syslog for debugging
259 static inline void dump_urb(enum debuglevel level
, const char *tag
,
262 #ifdef CONFIG_GIGASET_DEBUG
264 gig_dbg(level
, "%s urb(0x%08lx)->{", tag
, (unsigned long) urb
);
267 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
268 "hcpriv=0x%08lx, transfer_flags=0x%x,",
269 (unsigned long) urb
->dev
,
270 usb_pipetype_str(urb
->pipe
),
271 usb_pipeendpoint(urb
->pipe
), usb_pipedevice(urb
->pipe
),
272 usb_pipein(urb
->pipe
) ? "in" : "out",
273 (unsigned long) urb
->hcpriv
,
274 urb
->transfer_flags
);
276 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
277 "setup_packet=0x%08lx,",
278 (unsigned long) urb
->transfer_buffer
,
279 urb
->transfer_buffer_length
, urb
->actual_length
,
280 (unsigned long) urb
->setup_packet
);
282 " start_frame=%d, number_of_packets=%d, interval=%d, "
284 urb
->start_frame
, urb
->number_of_packets
, urb
->interval
,
287 " context=0x%08lx, complete=0x%08lx, "
288 "iso_frame_desc[]={",
289 (unsigned long) urb
->context
,
290 (unsigned long) urb
->complete
);
291 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
292 struct usb_iso_packet_descriptor
*pifd
293 = &urb
->iso_frame_desc
[i
];
295 " {offset=%u, length=%u, actual_length=%u, "
297 pifd
->offset
, pifd
->length
, pifd
->actual_length
,
301 gig_dbg(level
, "}}");
305 /* read/set modem control bits etc. (m10x only) */
306 static int gigaset_set_modem_ctrl(struct cardstate
*cs
, unsigned old_state
,
312 static int gigaset_baud_rate(struct cardstate
*cs
, unsigned cflag
)
317 static int gigaset_set_line_ctrl(struct cardstate
*cs
, unsigned cflag
)
322 /* set/clear bits in base connection state, return previous state
324 static inline int update_basstate(struct bas_cardstate
*ucs
,
330 spin_lock_irqsave(&ucs
->lock
, flags
);
331 state
= ucs
->basstate
;
332 ucs
->basstate
= (state
& ~clear
) | set
;
333 spin_unlock_irqrestore(&ucs
->lock
, flags
);
338 * hang up any existing connection because of an unrecoverable error
339 * This function may be called from any context and takes care of scheduling
340 * the necessary actions for execution outside of interrupt context.
341 * cs->lock must not be held.
343 * B channel control structure
345 static inline void error_hangup(struct bc_state
*bcs
)
347 struct cardstate
*cs
= bcs
->cs
;
349 gigaset_add_event(cs
, &bcs
->at_state
, EV_HUP
, NULL
, 0, NULL
);
350 gigaset_schedule_event(cs
);
354 * reset Gigaset device because of an unrecoverable error
355 * This function may be called from any context, and takes care of
356 * scheduling the necessary actions for execution outside of interrupt context.
357 * cs->hw.bas->lock must not be held.
359 * controller state structure
361 static inline void error_reset(struct cardstate
*cs
)
363 /* reset interrupt pipe to recover (ignore errors) */
364 update_basstate(cs
->hw
.bas
, BS_RESETTING
, 0);
365 if (req_submit(cs
->bcs
, HD_RESET_INTERRUPT_PIPE
, 0, BAS_TIMEOUT
))
366 /* submission failed, escalate to USB port reset */
367 usb_queue_reset_device(cs
->hw
.bas
->interface
);
371 * check for completion of pending control request
373 * ucs hardware specific controller state structure
375 static void check_pending(struct bas_cardstate
*ucs
)
379 spin_lock_irqsave(&ucs
->lock
, flags
);
380 switch (ucs
->pending
) {
383 case HD_OPEN_ATCHANNEL
:
384 if (ucs
->basstate
& BS_ATOPEN
)
387 case HD_OPEN_B1CHANNEL
:
388 if (ucs
->basstate
& BS_B1OPEN
)
391 case HD_OPEN_B2CHANNEL
:
392 if (ucs
->basstate
& BS_B2OPEN
)
395 case HD_CLOSE_ATCHANNEL
:
396 if (!(ucs
->basstate
& BS_ATOPEN
))
399 case HD_CLOSE_B1CHANNEL
:
400 if (!(ucs
->basstate
& BS_B1OPEN
))
403 case HD_CLOSE_B2CHANNEL
:
404 if (!(ucs
->basstate
& BS_B2OPEN
))
407 case HD_DEVICE_INIT_ACK
: /* no reply expected */
410 case HD_RESET_INTERRUPT_PIPE
:
411 if (!(ucs
->basstate
& BS_RESETTING
))
415 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
416 * and should never end up here
419 dev_warn(&ucs
->interface
->dev
,
420 "unknown pending request 0x%02x cleared\n",
426 del_timer(&ucs
->timer_ctrl
);
428 spin_unlock_irqrestore(&ucs
->lock
, flags
);
432 * timeout routine for command input request
434 * controller state structure
436 static void cmd_in_timeout(unsigned long data
)
438 struct cardstate
*cs
= (struct cardstate
*) data
;
439 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
442 if (!ucs
->rcvbuf_size
) {
443 gig_dbg(DEBUG_USBREQ
, "%s: no receive in progress", __func__
);
447 if (ucs
->retry_cmd_in
++ >= BAS_RETRY
) {
449 "control read: timeout, giving up after %d tries\n",
453 ucs
->rcvbuf_size
= 0;
458 gig_dbg(DEBUG_USBREQ
, "%s: timeout, retry %d",
459 __func__
, ucs
->retry_cmd_in
);
460 rc
= atread_submit(cs
, BAS_TIMEOUT
);
464 ucs
->rcvbuf_size
= 0;
470 /* read_ctrl_callback
471 * USB completion handler for control pipe input
472 * called by the USB subsystem in interrupt context
474 * urb USB request block
475 * urb->context = inbuf structure for controller state
477 static void read_ctrl_callback(struct urb
*urb
)
479 struct inbuf_t
*inbuf
= urb
->context
;
480 struct cardstate
*cs
= inbuf
->cs
;
481 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
482 int status
= urb
->status
;
486 update_basstate(ucs
, 0, BS_ATRDPEND
);
487 wake_up(&ucs
->waitqueue
);
488 del_timer(&ucs
->timer_cmd_in
);
491 case 0: /* normal completion */
492 numbytes
= urb
->actual_length
;
493 if (unlikely(numbytes
!= ucs
->rcvbuf_size
)) {
495 "control read: received %d chars, expected %d\n",
496 numbytes
, ucs
->rcvbuf_size
);
497 if (numbytes
> ucs
->rcvbuf_size
)
498 numbytes
= ucs
->rcvbuf_size
;
501 /* copy received bytes to inbuf, notify event layer */
502 if (gigaset_fill_inbuf(inbuf
, ucs
->rcvbuf
, numbytes
)) {
503 gig_dbg(DEBUG_INTR
, "%s-->BH", __func__
);
504 gigaset_schedule_event(cs
);
508 case -ENOENT
: /* cancelled */
509 case -ECONNRESET
: /* cancelled (async) */
510 case -EINPROGRESS
: /* pending */
511 case -ENODEV
: /* device removed */
512 case -ESHUTDOWN
: /* device shut down */
513 /* no further action necessary */
514 gig_dbg(DEBUG_USBREQ
, "%s: %s",
515 __func__
, get_usb_statmsg(status
));
518 default: /* other errors: retry */
519 if (ucs
->retry_cmd_in
++ < BAS_RETRY
) {
520 gig_dbg(DEBUG_USBREQ
, "%s: %s, retry %d", __func__
,
521 get_usb_statmsg(status
), ucs
->retry_cmd_in
);
522 rc
= atread_submit(cs
, BAS_TIMEOUT
);
524 /* successfully resubmitted, skip freeing */
527 /* disconnect, no further action necessary */
530 dev_err(cs
->dev
, "control read: %s, giving up after %d tries\n",
531 get_usb_statmsg(status
), ucs
->retry_cmd_in
);
535 /* read finished, free buffer */
538 ucs
->rcvbuf_size
= 0;
542 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
544 * cs controller state structure
545 * timeout timeout in 1/10 sec., 0: none
548 * -EBUSY if another request is pending
549 * any URB submission error code
551 static int atread_submit(struct cardstate
*cs
, int timeout
)
553 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
557 gig_dbg(DEBUG_USBREQ
, "-------> HD_READ_ATMESSAGE (%d)",
560 basstate
= update_basstate(ucs
, BS_ATRDPEND
, 0);
561 if (basstate
& BS_ATRDPEND
) {
563 "could not submit HD_READ_ATMESSAGE: URB busy\n");
567 if (basstate
& BS_SUSPEND
) {
569 "HD_READ_ATMESSAGE not submitted, "
570 "suspend in progress\n");
571 update_basstate(ucs
, 0, BS_ATRDPEND
);
572 /* treat like disconnect */
576 ucs
->dr_cmd_in
.bRequestType
= IN_VENDOR_REQ
;
577 ucs
->dr_cmd_in
.bRequest
= HD_READ_ATMESSAGE
;
578 ucs
->dr_cmd_in
.wValue
= 0;
579 ucs
->dr_cmd_in
.wIndex
= 0;
580 ucs
->dr_cmd_in
.wLength
= cpu_to_le16(ucs
->rcvbuf_size
);
581 usb_fill_control_urb(ucs
->urb_cmd_in
, ucs
->udev
,
582 usb_rcvctrlpipe(ucs
->udev
, 0),
583 (unsigned char *) &ucs
->dr_cmd_in
,
584 ucs
->rcvbuf
, ucs
->rcvbuf_size
,
585 read_ctrl_callback
, cs
->inbuf
);
587 ret
= usb_submit_urb(ucs
->urb_cmd_in
, GFP_ATOMIC
);
589 update_basstate(ucs
, 0, BS_ATRDPEND
);
590 dev_err(cs
->dev
, "could not submit HD_READ_ATMESSAGE: %s\n",
596 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
597 mod_timer(&ucs
->timer_cmd_in
, jiffies
+ timeout
* HZ
/ 10);
603 * workqueue routine to clear halt on interrupt in endpoint
606 static void int_in_work(struct work_struct
*work
)
608 struct bas_cardstate
*ucs
=
609 container_of(work
, struct bas_cardstate
, int_in_wq
);
610 struct urb
*urb
= ucs
->urb_int_in
;
611 struct cardstate
*cs
= urb
->context
;
614 /* clear halt condition */
615 rc
= usb_clear_halt(ucs
->udev
, urb
->pipe
);
616 gig_dbg(DEBUG_USBREQ
, "clear_halt: %s", get_usb_rcmsg(rc
));
618 /* success, resubmit interrupt read URB */
619 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
620 if (rc
!= 0 && rc
!= -ENODEV
) {
621 dev_err(cs
->dev
, "clear halt failed: %s\n", get_usb_rcmsg(rc
));
622 rc
= usb_lock_device_for_reset(ucs
->udev
, ucs
->interface
);
624 rc
= usb_reset_device(ucs
->udev
);
625 usb_unlock_device(ucs
->udev
);
628 ucs
->retry_int_in
= 0;
632 * timer routine for interrupt read delayed resubmit
634 * controller state structure
636 static void int_in_resubmit(unsigned long data
)
638 struct cardstate
*cs
= (struct cardstate
*) data
;
639 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
642 if (ucs
->retry_int_in
++ >= BAS_RETRY
) {
643 dev_err(cs
->dev
, "interrupt read: giving up after %d tries\n",
645 usb_queue_reset_device(ucs
->interface
);
649 gig_dbg(DEBUG_USBREQ
, "%s: retry %d", __func__
, ucs
->retry_int_in
);
650 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_ATOMIC
);
651 if (rc
!= 0 && rc
!= -ENODEV
) {
652 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
654 usb_queue_reset_device(ucs
->interface
);
659 * USB completion handler for interrupt pipe input
660 * called by the USB subsystem in interrupt context
662 * urb USB request block
663 * urb->context = controller state structure
665 static void read_int_callback(struct urb
*urb
)
667 struct cardstate
*cs
= urb
->context
;
668 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
669 struct bc_state
*bcs
;
670 int status
= urb
->status
;
677 case 0: /* success */
678 ucs
->retry_int_in
= 0;
680 case -EPIPE
: /* endpoint stalled */
681 schedule_work(&ucs
->int_in_wq
);
683 case -ENOENT
: /* cancelled */
684 case -ECONNRESET
: /* cancelled (async) */
685 case -EINPROGRESS
: /* pending */
686 case -ENODEV
: /* device removed */
687 case -ESHUTDOWN
: /* device shut down */
688 /* no further action necessary */
689 gig_dbg(DEBUG_USBREQ
, "%s: %s",
690 __func__
, get_usb_statmsg(status
));
692 case -EPROTO
: /* protocol error or unplug */
695 /* resubmit after delay */
696 gig_dbg(DEBUG_USBREQ
, "%s: %s",
697 __func__
, get_usb_statmsg(status
));
698 mod_timer(&ucs
->timer_int_in
, jiffies
+ HZ
/ 10);
700 default: /* other errors: just resubmit */
701 dev_warn(cs
->dev
, "interrupt read: %s\n",
702 get_usb_statmsg(status
));
706 /* drop incomplete packets even if the missing bytes wouldn't matter */
707 if (unlikely(urb
->actual_length
< IP_MSGSIZE
)) {
708 dev_warn(cs
->dev
, "incomplete interrupt packet (%d bytes)\n",
713 l
= (unsigned) ucs
->int_in_buf
[1] +
714 (((unsigned) ucs
->int_in_buf
[2]) << 8);
716 gig_dbg(DEBUG_USBREQ
, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
717 urb
->actual_length
, (int)ucs
->int_in_buf
[0], l
,
718 (int)ucs
->int_in_buf
[1], (int)ucs
->int_in_buf
[2]);
722 switch (ucs
->int_in_buf
[0]) {
723 case HD_DEVICE_INIT_OK
:
724 update_basstate(ucs
, BS_INIT
, 0);
727 case HD_READY_SEND_ATDATA
:
728 del_timer(&ucs
->timer_atrdy
);
729 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
733 case HD_OPEN_B2CHANNEL_ACK
:
735 case HD_OPEN_B1CHANNEL_ACK
:
736 bcs
= cs
->bcs
+ channel
;
737 update_basstate(ucs
, BS_B1OPEN
<< channel
, 0);
738 gigaset_bchannel_up(bcs
);
741 case HD_OPEN_ATCHANNEL_ACK
:
742 update_basstate(ucs
, BS_ATOPEN
, 0);
746 case HD_CLOSE_B2CHANNEL_ACK
:
748 case HD_CLOSE_B1CHANNEL_ACK
:
749 bcs
= cs
->bcs
+ channel
;
750 update_basstate(ucs
, 0, BS_B1OPEN
<< channel
);
751 stopurbs(bcs
->hw
.bas
);
752 gigaset_bchannel_down(bcs
);
755 case HD_CLOSE_ATCHANNEL_ACK
:
756 update_basstate(ucs
, 0, BS_ATOPEN
);
759 case HD_B2_FLOW_CONTROL
:
761 case HD_B1_FLOW_CONTROL
:
762 bcs
= cs
->bcs
+ channel
;
763 atomic_add((l
- BAS_NORMFRAME
) * BAS_CORRFRAMES
,
764 &bcs
->hw
.bas
->corrbytes
);
766 "Flow control (channel %d, sub %d): 0x%02x => %d",
767 channel
, bcs
->hw
.bas
->numsub
, l
,
768 atomic_read(&bcs
->hw
.bas
->corrbytes
));
771 case HD_RECEIVEATDATA_ACK
: /* AT response ready to be received */
774 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
777 spin_lock_irqsave(&cs
->lock
, flags
);
778 if (ucs
->basstate
& BS_ATRDPEND
) {
779 spin_unlock_irqrestore(&cs
->lock
, flags
);
781 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
782 l
, ucs
->rcvbuf_size
);
785 if (ucs
->rcvbuf_size
) {
786 /* throw away previous buffer - we have no queue */
788 "receive AT data overrun, %d bytes lost\n",
791 ucs
->rcvbuf_size
= 0;
793 ucs
->rcvbuf
= kmalloc(l
, GFP_ATOMIC
);
794 if (ucs
->rcvbuf
== NULL
) {
795 spin_unlock_irqrestore(&cs
->lock
, flags
);
796 dev_err(cs
->dev
, "out of memory receiving AT data\n");
799 ucs
->rcvbuf_size
= l
;
800 ucs
->retry_cmd_in
= 0;
801 rc
= atread_submit(cs
, BAS_TIMEOUT
);
805 ucs
->rcvbuf_size
= 0;
807 spin_unlock_irqrestore(&cs
->lock
, flags
);
808 if (rc
< 0 && rc
!= -ENODEV
)
812 case HD_RESET_INTERRUPT_PIPE_ACK
:
813 update_basstate(ucs
, 0, BS_RESETTING
);
814 dev_notice(cs
->dev
, "interrupt pipe reset\n");
818 gig_dbg(DEBUG_USBREQ
, "HD_SUSPEND_END");
823 "unknown Gigaset signal 0x%02x (%u) ignored\n",
824 (int) ucs
->int_in_buf
[0], l
);
828 wake_up(&ucs
->waitqueue
);
831 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
832 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
833 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
840 * USB completion handler for B channel isochronous input
841 * called by the USB subsystem in interrupt context
843 * urb USB request block of completed request
844 * urb->context = bc_state structure
846 static void read_iso_callback(struct urb
*urb
)
848 struct bc_state
*bcs
;
849 struct bas_bc_state
*ubc
;
850 int status
= urb
->status
;
854 /* status codes not worth bothering the tasklet with */
855 if (unlikely(status
== -ENOENT
||
856 status
== -ECONNRESET
||
857 status
== -EINPROGRESS
||
859 status
== -ESHUTDOWN
)) {
860 gig_dbg(DEBUG_ISO
, "%s: %s",
861 __func__
, get_usb_statmsg(status
));
868 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
869 if (likely(ubc
->isoindone
== NULL
)) {
870 /* pass URB to tasklet */
871 ubc
->isoindone
= urb
;
872 ubc
->isoinstatus
= status
;
873 tasklet_hi_schedule(&ubc
->rcvd_tasklet
);
875 /* tasklet still busy, drop data and resubmit URB */
876 gig_dbg(DEBUG_ISO
, "%s: overrun", __func__
);
877 ubc
->loststatus
= status
;
878 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
879 ubc
->isoinlost
+= urb
->iso_frame_desc
[i
].actual_length
;
880 if (unlikely(urb
->iso_frame_desc
[i
].status
!= 0 &&
881 urb
->iso_frame_desc
[i
].status
!=
883 ubc
->loststatus
= urb
->iso_frame_desc
[i
].status
;
884 urb
->iso_frame_desc
[i
].status
= 0;
885 urb
->iso_frame_desc
[i
].actual_length
= 0;
887 if (likely(ubc
->running
)) {
888 /* urb->dev is clobbered by USB subsystem */
889 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
890 urb
->transfer_flags
= URB_ISO_ASAP
;
891 urb
->number_of_packets
= BAS_NUMFRAMES
;
892 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
893 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
894 dev_err(bcs
->cs
->dev
,
895 "could not resubmit isoc read URB: %s\n",
897 dump_urb(DEBUG_ISO
, "isoc read", urb
);
902 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
905 /* write_iso_callback
906 * USB completion handler for B channel isochronous output
907 * called by the USB subsystem in interrupt context
909 * urb USB request block of completed request
910 * urb->context = isow_urbctx_t structure
912 static void write_iso_callback(struct urb
*urb
)
914 struct isow_urbctx_t
*ucx
;
915 struct bas_bc_state
*ubc
;
916 int status
= urb
->status
;
919 /* status codes not worth bothering the tasklet with */
920 if (unlikely(status
== -ENOENT
||
921 status
== -ECONNRESET
||
922 status
== -EINPROGRESS
||
924 status
== -ESHUTDOWN
)) {
925 gig_dbg(DEBUG_ISO
, "%s: %s",
926 __func__
, get_usb_statmsg(status
));
930 /* pass URB context to tasklet */
932 ubc
= ucx
->bcs
->hw
.bas
;
933 ucx
->status
= status
;
935 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
936 ubc
->isooutovfl
= ubc
->isooutdone
;
937 ubc
->isooutdone
= ucx
;
938 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
939 tasklet_hi_schedule(&ubc
->sent_tasklet
);
943 * prepare and submit USB request blocks for isochronous input and output
945 * B channel control structure
948 * < 0 on error (no URBs submitted)
950 static int starturbs(struct bc_state
*bcs
)
952 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
957 /* initialize L2 reception */
958 if (bcs
->proto2
== L2_HDLC
)
959 bcs
->inputstate
|= INS_flag_hunt
;
961 /* submit all isochronous input URBs */
963 for (k
= 0; k
< BAS_INURBS
; k
++) {
964 urb
= ubc
->isoinurbs
[k
];
970 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
971 urb
->pipe
= usb_rcvisocpipe(urb
->dev
, 3 + 2 * bcs
->channel
);
972 urb
->transfer_flags
= URB_ISO_ASAP
;
973 urb
->transfer_buffer
= ubc
->isoinbuf
+ k
* BAS_INBUFSIZE
;
974 urb
->transfer_buffer_length
= BAS_INBUFSIZE
;
975 urb
->number_of_packets
= BAS_NUMFRAMES
;
976 urb
->interval
= BAS_FRAMETIME
;
977 urb
->complete
= read_iso_callback
;
979 for (j
= 0; j
< BAS_NUMFRAMES
; j
++) {
980 urb
->iso_frame_desc
[j
].offset
= j
* BAS_MAXFRAME
;
981 urb
->iso_frame_desc
[j
].length
= BAS_MAXFRAME
;
982 urb
->iso_frame_desc
[j
].status
= 0;
983 urb
->iso_frame_desc
[j
].actual_length
= 0;
986 dump_urb(DEBUG_ISO
, "Initial isoc read", urb
);
987 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
992 /* initialize L2 transmission */
993 gigaset_isowbuf_init(ubc
->isooutbuf
, PPP_FLAG
);
995 /* set up isochronous output URBs for flag idling */
996 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
997 urb
= ubc
->isoouturbs
[k
].urb
;
1002 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1003 urb
->pipe
= usb_sndisocpipe(urb
->dev
, 4 + 2 * bcs
->channel
);
1004 urb
->transfer_flags
= URB_ISO_ASAP
;
1005 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1006 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1007 urb
->number_of_packets
= BAS_NUMFRAMES
;
1008 urb
->interval
= BAS_FRAMETIME
;
1009 urb
->complete
= write_iso_callback
;
1010 urb
->context
= &ubc
->isoouturbs
[k
];
1011 for (j
= 0; j
< BAS_NUMFRAMES
; ++j
) {
1012 urb
->iso_frame_desc
[j
].offset
= BAS_OUTBUFSIZE
;
1013 urb
->iso_frame_desc
[j
].length
= BAS_NORMFRAME
;
1014 urb
->iso_frame_desc
[j
].status
= 0;
1015 urb
->iso_frame_desc
[j
].actual_length
= 0;
1017 ubc
->isoouturbs
[k
].limit
= -1;
1020 /* keep one URB free, submit the others */
1021 for (k
= 0; k
< BAS_OUTURBS
- 1; ++k
) {
1022 dump_urb(DEBUG_ISO
, "Initial isoc write", urb
);
1023 rc
= usb_submit_urb(ubc
->isoouturbs
[k
].urb
, GFP_ATOMIC
);
1027 dump_urb(DEBUG_ISO
, "Initial isoc write (free)", urb
);
1028 ubc
->isooutfree
= &ubc
->isoouturbs
[BAS_OUTURBS
- 1];
1029 ubc
->isooutdone
= ubc
->isooutovfl
= NULL
;
1037 * cancel the USB request blocks for isochronous input and output
1038 * errors are silently ignored
1040 * B channel control structure
1042 static void stopurbs(struct bas_bc_state
*ubc
)
1048 for (k
= 0; k
< BAS_INURBS
; ++k
) {
1049 rc
= usb_unlink_urb(ubc
->isoinurbs
[k
]);
1051 "%s: isoc input URB %d unlinked, result = %s",
1052 __func__
, k
, get_usb_rcmsg(rc
));
1055 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
1056 rc
= usb_unlink_urb(ubc
->isoouturbs
[k
].urb
);
1058 "%s: isoc output URB %d unlinked, result = %s",
1059 __func__
, k
, get_usb_rcmsg(rc
));
1063 /* Isochronous Write - Bottom Half */
1064 /* =============================== */
1066 /* submit_iso_write_urb
1067 * fill and submit the next isochronous write URB
1069 * ucx context structure containing URB
1071 * number of frames submitted in URB
1072 * 0 if URB not submitted because no data available (isooutbuf busy)
1073 * error code < 0 on error
1075 static int submit_iso_write_urb(struct isow_urbctx_t
*ucx
)
1077 struct urb
*urb
= ucx
->urb
;
1078 struct bas_bc_state
*ubc
= ucx
->bcs
->hw
.bas
;
1079 struct usb_iso_packet_descriptor
*ifd
;
1080 int corrbytes
, nframe
, rc
;
1082 /* urb->dev is clobbered by USB subsystem */
1083 urb
->dev
= ucx
->bcs
->cs
->hw
.bas
->udev
;
1084 urb
->transfer_flags
= URB_ISO_ASAP
;
1085 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1086 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1088 for (nframe
= 0; nframe
< BAS_NUMFRAMES
; nframe
++) {
1089 ifd
= &urb
->iso_frame_desc
[nframe
];
1091 /* compute frame length according to flow control */
1092 ifd
->length
= BAS_NORMFRAME
;
1093 corrbytes
= atomic_read(&ubc
->corrbytes
);
1094 if (corrbytes
!= 0) {
1095 gig_dbg(DEBUG_ISO
, "%s: corrbytes=%d",
1096 __func__
, corrbytes
);
1097 if (corrbytes
> BAS_HIGHFRAME
- BAS_NORMFRAME
)
1098 corrbytes
= BAS_HIGHFRAME
- BAS_NORMFRAME
;
1099 else if (corrbytes
< BAS_LOWFRAME
- BAS_NORMFRAME
)
1100 corrbytes
= BAS_LOWFRAME
- BAS_NORMFRAME
;
1101 ifd
->length
+= corrbytes
;
1102 atomic_add(-corrbytes
, &ubc
->corrbytes
);
1105 /* retrieve block of data to send */
1106 rc
= gigaset_isowbuf_getbytes(ubc
->isooutbuf
, ifd
->length
);
1110 "%s: buffer busy at frame %d",
1112 /* tasklet will be restarted from
1113 gigaset_isoc_send_skb() */
1115 dev_err(ucx
->bcs
->cs
->dev
,
1116 "%s: buffer error %d at frame %d\n",
1117 __func__
, rc
, nframe
);
1123 ucx
->limit
= ubc
->isooutbuf
->nextread
;
1125 ifd
->actual_length
= 0;
1127 if (unlikely(nframe
== 0))
1128 return 0; /* no data to send */
1129 urb
->number_of_packets
= nframe
;
1131 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1134 /* device removed - give up silently */
1135 gig_dbg(DEBUG_ISO
, "%s: disconnected", __func__
);
1137 dev_err(ucx
->bcs
->cs
->dev
,
1138 "could not submit isoc write URB: %s\n",
1146 /* write_iso_tasklet
1147 * tasklet scheduled when an isochronous output URB from the Gigaset device
1150 * data B channel state structure
1152 static void write_iso_tasklet(unsigned long data
)
1154 struct bc_state
*bcs
= (struct bc_state
*) data
;
1155 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1156 struct cardstate
*cs
= bcs
->cs
;
1157 struct isow_urbctx_t
*done
, *next
, *ovfl
;
1160 struct usb_iso_packet_descriptor
*ifd
;
1161 unsigned long flags
;
1163 struct sk_buff
*skb
;
1167 /* loop while completed URBs arrive in time */
1169 if (unlikely(!(ubc
->running
))) {
1170 gig_dbg(DEBUG_ISO
, "%s: not running", __func__
);
1174 /* retrieve completed URBs */
1175 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1176 done
= ubc
->isooutdone
;
1177 ubc
->isooutdone
= NULL
;
1178 ovfl
= ubc
->isooutovfl
;
1179 ubc
->isooutovfl
= NULL
;
1180 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1182 dev_err(cs
->dev
, "isoc write underrun\n");
1189 /* submit free URB if available */
1190 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1191 next
= ubc
->isooutfree
;
1192 ubc
->isooutfree
= NULL
;
1193 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1195 rc
= submit_iso_write_urb(next
);
1196 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1197 /* could not submit URB, put it back */
1198 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1199 if (ubc
->isooutfree
== NULL
) {
1200 ubc
->isooutfree
= next
;
1203 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1205 /* couldn't put it back */
1207 "losing isoc write URB\n");
1213 /* process completed URB */
1215 status
= done
->status
;
1217 case -EXDEV
: /* partial completion */
1218 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1220 /* fall through - what's the difference anyway? */
1221 case 0: /* normal completion */
1222 /* inspect individual frames
1223 * assumptions (for lack of documentation):
1224 * - actual_length bytes of first frame in error are
1226 * - all following frames are not sent at all
1228 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
1229 ifd
= &urb
->iso_frame_desc
[i
];
1231 ifd
->actual_length
!= ifd
->length
) {
1233 "isoc write: frame %d[%d/%d]: %s\n",
1234 i
, ifd
->actual_length
,
1236 get_usb_statmsg(ifd
->status
));
1241 case -EPIPE
: /* stall - probably underrun */
1242 dev_err(cs
->dev
, "isoc write: stalled\n");
1245 default: /* other errors */
1246 dev_warn(cs
->dev
, "isoc write: %s\n",
1247 get_usb_statmsg(status
));
1250 /* mark the write buffer area covered by this URB as free */
1251 if (done
->limit
>= 0)
1252 ubc
->isooutbuf
->read
= done
->limit
;
1254 /* mark URB as free */
1255 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1256 next
= ubc
->isooutfree
;
1257 ubc
->isooutfree
= done
;
1258 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1260 /* only one URB still active - resubmit one */
1261 rc
= submit_iso_write_urb(next
);
1262 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1263 /* couldn't submit */
1269 /* process queued SKBs */
1270 while ((skb
= skb_dequeue(&bcs
->squeue
))) {
1271 /* copy to output buffer, doing L2 encapsulation */
1273 if (gigaset_isoc_buildframe(bcs
, skb
->data
, len
) == -EAGAIN
) {
1274 /* insufficient buffer space, push back onto queue */
1275 skb_queue_head(&bcs
->squeue
, skb
);
1276 gig_dbg(DEBUG_ISO
, "%s: skb requeued, qlen=%d",
1277 __func__
, skb_queue_len(&bcs
->squeue
));
1281 gigaset_skb_sent(bcs
, skb
);
1282 dev_kfree_skb_any(skb
);
1286 /* Isochronous Read - Bottom Half */
1287 /* ============================== */
1290 * tasklet scheduled when an isochronous input URB from the Gigaset device
1293 * data B channel state structure
1295 static void read_iso_tasklet(unsigned long data
)
1297 struct bc_state
*bcs
= (struct bc_state
*) data
;
1298 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1299 struct cardstate
*cs
= bcs
->cs
;
1302 struct usb_iso_packet_descriptor
*ifd
;
1304 unsigned long flags
;
1305 int totleft
, numbytes
, offset
, frame
, rc
;
1307 /* loop while more completed URBs arrive in the meantime */
1310 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
1311 urb
= ubc
->isoindone
;
1313 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1316 status
= ubc
->isoinstatus
;
1317 ubc
->isoindone
= NULL
;
1318 if (unlikely(ubc
->loststatus
!= -EINPROGRESS
)) {
1320 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
1321 get_usb_statmsg(ubc
->loststatus
),
1323 ubc
->loststatus
= -EINPROGRESS
;
1325 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1327 if (unlikely(!(ubc
->running
))) {
1329 "%s: channel not running, "
1330 "dropped URB with status: %s",
1331 __func__
, get_usb_statmsg(status
));
1336 case 0: /* normal completion */
1338 case -EXDEV
: /* inspect individual frames
1339 (we do that anyway) */
1340 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1346 gig_dbg(DEBUG_ISO
, "%s: %s",
1347 __func__
, get_usb_statmsg(status
));
1348 continue; /* -> skip */
1350 dev_err(cs
->dev
, "isoc read: stalled\n");
1352 continue; /* -> skip */
1353 default: /* other error */
1354 dev_warn(cs
->dev
, "isoc read: %s\n",
1355 get_usb_statmsg(status
));
1359 rcvbuf
= urb
->transfer_buffer
;
1360 totleft
= urb
->actual_length
;
1361 for (frame
= 0; totleft
> 0 && frame
< BAS_NUMFRAMES
; frame
++) {
1362 ifd
= &urb
->iso_frame_desc
[frame
];
1363 numbytes
= ifd
->actual_length
;
1364 switch (ifd
->status
) {
1365 case 0: /* success */
1367 case -EPROTO
: /* protocol error or unplug */
1370 /* probably just disconnected, ignore */
1372 "isoc read: frame %d[%d]: %s\n",
1374 get_usb_statmsg(ifd
->status
));
1376 default: /* other error */
1377 /* report, assume transferred bytes are ok */
1379 "isoc read: frame %d[%d]: %s\n",
1381 get_usb_statmsg(ifd
->status
));
1383 if (unlikely(numbytes
> BAS_MAXFRAME
))
1385 "isoc read: frame %d[%d]: %s\n",
1387 "exceeds max frame size");
1388 if (unlikely(numbytes
> totleft
)) {
1390 "isoc read: frame %d[%d]: %s\n",
1392 "exceeds total transfer length");
1395 offset
= ifd
->offset
;
1396 if (unlikely(offset
+ numbytes
> BAS_INBUFSIZE
)) {
1398 "isoc read: frame %d[%d]: %s\n",
1400 "exceeds end of buffer");
1401 numbytes
= BAS_INBUFSIZE
- offset
;
1403 gigaset_isoc_receive(rcvbuf
+ offset
, numbytes
, bcs
);
1404 totleft
-= numbytes
;
1406 if (unlikely(totleft
> 0))
1407 dev_warn(cs
->dev
, "isoc read: %d data bytes missing\n",
1411 /* URB processed, resubmit */
1412 for (frame
= 0; frame
< BAS_NUMFRAMES
; frame
++) {
1413 urb
->iso_frame_desc
[frame
].status
= 0;
1414 urb
->iso_frame_desc
[frame
].actual_length
= 0;
1416 /* urb->dev is clobbered by USB subsystem */
1417 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1418 urb
->transfer_flags
= URB_ISO_ASAP
;
1419 urb
->number_of_packets
= BAS_NUMFRAMES
;
1420 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1421 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
1423 "could not resubmit isoc read URB: %s\n",
1425 dump_urb(DEBUG_ISO
, "resubmit isoc read", urb
);
1431 /* Channel Operations */
1432 /* ================== */
1435 * timeout routine for control output request
1437 * controller state structure
1439 static void req_timeout(unsigned long data
)
1441 struct cardstate
*cs
= (struct cardstate
*) data
;
1442 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1444 unsigned long flags
;
1448 spin_lock_irqsave(&ucs
->lock
, flags
);
1449 pending
= ucs
->pending
;
1451 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1454 case 0: /* no pending request */
1455 gig_dbg(DEBUG_USBREQ
, "%s: no request pending", __func__
);
1458 case HD_OPEN_ATCHANNEL
:
1459 dev_err(cs
->dev
, "timeout opening AT channel\n");
1463 case HD_OPEN_B1CHANNEL
:
1464 dev_err(cs
->dev
, "timeout opening channel 1\n");
1465 error_hangup(&cs
->bcs
[0]);
1468 case HD_OPEN_B2CHANNEL
:
1469 dev_err(cs
->dev
, "timeout opening channel 2\n");
1470 error_hangup(&cs
->bcs
[1]);
1473 case HD_CLOSE_ATCHANNEL
:
1474 dev_err(cs
->dev
, "timeout closing AT channel\n");
1478 case HD_CLOSE_B1CHANNEL
:
1479 dev_err(cs
->dev
, "timeout closing channel 1\n");
1483 case HD_CLOSE_B2CHANNEL
:
1484 dev_err(cs
->dev
, "timeout closing channel 2\n");
1488 case HD_RESET_INTERRUPT_PIPE
:
1489 /* error recovery escalation */
1491 "reset interrupt pipe timeout, attempting USB reset\n");
1492 usb_queue_reset_device(ucs
->interface
);
1496 dev_warn(cs
->dev
, "request 0x%02x timed out, clearing\n",
1500 wake_up(&ucs
->waitqueue
);
1503 /* write_ctrl_callback
1504 * USB completion handler for control pipe output
1505 * called by the USB subsystem in interrupt context
1507 * urb USB request block of completed request
1508 * urb->context = hardware specific controller state structure
1510 static void write_ctrl_callback(struct urb
*urb
)
1512 struct bas_cardstate
*ucs
= urb
->context
;
1513 int status
= urb
->status
;
1515 unsigned long flags
;
1519 case 0: /* normal completion */
1520 spin_lock_irqsave(&ucs
->lock
, flags
);
1521 switch (ucs
->pending
) {
1522 case HD_DEVICE_INIT_ACK
: /* no reply expected */
1523 del_timer(&ucs
->timer_ctrl
);
1527 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1530 case -ENOENT
: /* cancelled */
1531 case -ECONNRESET
: /* cancelled (async) */
1532 case -EINPROGRESS
: /* pending */
1533 case -ENODEV
: /* device removed */
1534 case -ESHUTDOWN
: /* device shut down */
1535 /* ignore silently */
1536 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1537 __func__
, get_usb_statmsg(status
));
1540 default: /* any failure */
1541 /* don't retry if suspend requested */
1542 if (++ucs
->retry_ctrl
> BAS_RETRY
||
1543 (ucs
->basstate
& BS_SUSPEND
)) {
1544 dev_err(&ucs
->interface
->dev
,
1545 "control request 0x%02x failed: %s\n",
1546 ucs
->dr_ctrl
.bRequest
,
1547 get_usb_statmsg(status
));
1548 break; /* give up */
1550 dev_notice(&ucs
->interface
->dev
,
1551 "control request 0x%02x: %s, retry %d\n",
1552 ucs
->dr_ctrl
.bRequest
, get_usb_statmsg(status
),
1554 /* urb->dev is clobbered by USB subsystem */
1555 urb
->dev
= ucs
->udev
;
1556 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1558 dev_err(&ucs
->interface
->dev
,
1559 "could not resubmit request 0x%02x: %s\n",
1560 ucs
->dr_ctrl
.bRequest
, get_usb_rcmsg(rc
));
1567 /* failed, clear pending request */
1568 spin_lock_irqsave(&ucs
->lock
, flags
);
1569 del_timer(&ucs
->timer_ctrl
);
1571 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1572 wake_up(&ucs
->waitqueue
);
1576 * submit a control output request without message buffer to the Gigaset base
1577 * and optionally start a timeout
1579 * bcs B channel control structure
1580 * req control request code (HD_*)
1581 * val control request parameter value (set to 0 if unused)
1582 * timeout timeout in seconds (0: no timeout)
1585 * -EBUSY if another request is pending
1586 * any URB submission error code
1588 static int req_submit(struct bc_state
*bcs
, int req
, int val
, int timeout
)
1590 struct bas_cardstate
*ucs
= bcs
->cs
->hw
.bas
;
1592 unsigned long flags
;
1594 gig_dbg(DEBUG_USBREQ
, "-------> 0x%02x (%d)", req
, val
);
1596 spin_lock_irqsave(&ucs
->lock
, flags
);
1598 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1599 dev_err(bcs
->cs
->dev
,
1600 "submission of request 0x%02x failed: "
1601 "request 0x%02x still pending\n",
1606 ucs
->dr_ctrl
.bRequestType
= OUT_VENDOR_REQ
;
1607 ucs
->dr_ctrl
.bRequest
= req
;
1608 ucs
->dr_ctrl
.wValue
= cpu_to_le16(val
);
1609 ucs
->dr_ctrl
.wIndex
= 0;
1610 ucs
->dr_ctrl
.wLength
= 0;
1611 usb_fill_control_urb(ucs
->urb_ctrl
, ucs
->udev
,
1612 usb_sndctrlpipe(ucs
->udev
, 0),
1613 (unsigned char *) &ucs
->dr_ctrl
, NULL
, 0,
1614 write_ctrl_callback
, ucs
);
1615 ucs
->retry_ctrl
= 0;
1616 ret
= usb_submit_urb(ucs
->urb_ctrl
, GFP_ATOMIC
);
1617 if (unlikely(ret
)) {
1618 dev_err(bcs
->cs
->dev
, "could not submit request 0x%02x: %s\n",
1619 req
, get_usb_rcmsg(ret
));
1620 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1626 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
1627 mod_timer(&ucs
->timer_ctrl
, jiffies
+ timeout
* HZ
/ 10);
1630 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1634 /* gigaset_init_bchannel
1635 * called by common.c to connect a B channel
1636 * initialize isochronous I/O and tell the Gigaset base to open the channel
1638 * B channel control structure
1640 * 0 on success, error code < 0 on error
1642 static int gigaset_init_bchannel(struct bc_state
*bcs
)
1644 struct cardstate
*cs
= bcs
->cs
;
1646 unsigned long flags
;
1648 spin_lock_irqsave(&cs
->lock
, flags
);
1649 if (unlikely(!cs
->connected
)) {
1650 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1651 spin_unlock_irqrestore(&cs
->lock
, flags
);
1655 if (cs
->hw
.bas
->basstate
& BS_SUSPEND
) {
1657 "not starting isoc I/O, suspend in progress\n");
1658 spin_unlock_irqrestore(&cs
->lock
, flags
);
1659 return -EHOSTUNREACH
;
1662 ret
= starturbs(bcs
);
1664 spin_unlock_irqrestore(&cs
->lock
, flags
);
1666 "could not start isoc I/O for channel B%d: %s\n",
1668 ret
== -EFAULT
? "null URB" : get_usb_rcmsg(ret
));
1674 req
= bcs
->channel
? HD_OPEN_B2CHANNEL
: HD_OPEN_B1CHANNEL
;
1675 ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
);
1677 dev_err(cs
->dev
, "could not open channel B%d\n",
1679 stopurbs(bcs
->hw
.bas
);
1682 spin_unlock_irqrestore(&cs
->lock
, flags
);
1683 if (ret
< 0 && ret
!= -ENODEV
)
1688 /* gigaset_close_bchannel
1689 * called by common.c to disconnect a B channel
1690 * tell the Gigaset base to close the channel
1691 * stopping isochronous I/O and LL notification will be done when the
1692 * acknowledgement for the close arrives
1694 * B channel control structure
1696 * 0 on success, error code < 0 on error
1698 static int gigaset_close_bchannel(struct bc_state
*bcs
)
1700 struct cardstate
*cs
= bcs
->cs
;
1702 unsigned long flags
;
1704 spin_lock_irqsave(&cs
->lock
, flags
);
1705 if (unlikely(!cs
->connected
)) {
1706 spin_unlock_irqrestore(&cs
->lock
, flags
);
1707 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1711 if (!(cs
->hw
.bas
->basstate
& (bcs
->channel
? BS_B2OPEN
: BS_B1OPEN
))) {
1712 /* channel not running: just signal common.c */
1713 spin_unlock_irqrestore(&cs
->lock
, flags
);
1714 gigaset_bchannel_down(bcs
);
1718 /* channel running: tell device to close it */
1719 req
= bcs
->channel
? HD_CLOSE_B2CHANNEL
: HD_CLOSE_B1CHANNEL
;
1720 ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
);
1722 dev_err(cs
->dev
, "closing channel B%d failed\n",
1725 spin_unlock_irqrestore(&cs
->lock
, flags
);
1729 /* Device Operations */
1730 /* ================= */
1733 * unqueue first command buffer from queue, waking any sleepers
1734 * must be called with cs->cmdlock held
1736 * cs controller state structure
1738 static void complete_cb(struct cardstate
*cs
)
1740 struct cmdbuf_t
*cb
= cs
->cmdbuf
;
1742 /* unqueue completed buffer */
1743 cs
->cmdbytes
-= cs
->curlen
;
1744 gig_dbg(DEBUG_OUTPUT
, "write_command: sent %u bytes, %u left",
1745 cs
->curlen
, cs
->cmdbytes
);
1746 if (cb
->next
!= NULL
) {
1747 cs
->cmdbuf
= cb
->next
;
1748 cs
->cmdbuf
->prev
= NULL
;
1749 cs
->curlen
= cs
->cmdbuf
->len
;
1752 cs
->lastcmdbuf
= NULL
;
1756 if (cb
->wake_tasklet
)
1757 tasklet_schedule(cb
->wake_tasklet
);
1762 /* write_command_callback
1763 * USB completion handler for AT command transmission
1764 * called by the USB subsystem in interrupt context
1766 * urb USB request block of completed request
1767 * urb->context = controller state structure
1769 static void write_command_callback(struct urb
*urb
)
1771 struct cardstate
*cs
= urb
->context
;
1772 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1773 int status
= urb
->status
;
1774 unsigned long flags
;
1776 update_basstate(ucs
, 0, BS_ATWRPEND
);
1777 wake_up(&ucs
->waitqueue
);
1781 case 0: /* normal completion */
1783 case -ENOENT
: /* cancelled */
1784 case -ECONNRESET
: /* cancelled (async) */
1785 case -EINPROGRESS
: /* pending */
1786 case -ENODEV
: /* device removed */
1787 case -ESHUTDOWN
: /* device shut down */
1788 /* ignore silently */
1789 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1790 __func__
, get_usb_statmsg(status
));
1792 default: /* any failure */
1793 if (++ucs
->retry_cmd_out
> BAS_RETRY
) {
1795 "command write: %s, "
1796 "giving up after %d retries\n",
1797 get_usb_statmsg(status
),
1798 ucs
->retry_cmd_out
);
1801 if (ucs
->basstate
& BS_SUSPEND
) {
1803 "command write: %s, "
1804 "won't retry - suspend requested\n",
1805 get_usb_statmsg(status
));
1808 if (cs
->cmdbuf
== NULL
) {
1810 "command write: %s, "
1811 "cannot retry - cmdbuf gone\n",
1812 get_usb_statmsg(status
));
1815 dev_notice(cs
->dev
, "command write: %s, retry %d\n",
1816 get_usb_statmsg(status
), ucs
->retry_cmd_out
);
1817 if (atwrite_submit(cs
, cs
->cmdbuf
->buf
, cs
->cmdbuf
->len
) >= 0)
1818 /* resubmitted - bypass regular exit block */
1820 /* command send failed, assume base still waiting */
1821 update_basstate(ucs
, BS_ATREADY
, 0);
1824 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1825 if (cs
->cmdbuf
!= NULL
)
1827 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1831 * timeout routine for AT command transmission
1833 * controller state structure
1835 static void atrdy_timeout(unsigned long data
)
1837 struct cardstate
*cs
= (struct cardstate
*) data
;
1838 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1840 dev_warn(cs
->dev
, "timeout waiting for HD_READY_SEND_ATDATA\n");
1842 /* fake the missing signal - what else can I do? */
1843 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
1848 * submit an HD_WRITE_ATMESSAGE command URB
1850 * cs controller state structure
1851 * buf buffer containing command to send
1852 * len length of command to send
1855 * -EBUSY if another request is pending
1856 * any URB submission error code
1858 static int atwrite_submit(struct cardstate
*cs
, unsigned char *buf
, int len
)
1860 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1863 gig_dbg(DEBUG_USBREQ
, "-------> HD_WRITE_ATMESSAGE (%d)", len
);
1865 if (update_basstate(ucs
, BS_ATWRPEND
, 0) & BS_ATWRPEND
) {
1867 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1871 ucs
->dr_cmd_out
.bRequestType
= OUT_VENDOR_REQ
;
1872 ucs
->dr_cmd_out
.bRequest
= HD_WRITE_ATMESSAGE
;
1873 ucs
->dr_cmd_out
.wValue
= 0;
1874 ucs
->dr_cmd_out
.wIndex
= 0;
1875 ucs
->dr_cmd_out
.wLength
= cpu_to_le16(len
);
1876 usb_fill_control_urb(ucs
->urb_cmd_out
, ucs
->udev
,
1877 usb_sndctrlpipe(ucs
->udev
, 0),
1878 (unsigned char *) &ucs
->dr_cmd_out
, buf
, len
,
1879 write_command_callback
, cs
);
1880 rc
= usb_submit_urb(ucs
->urb_cmd_out
, GFP_ATOMIC
);
1882 update_basstate(ucs
, 0, BS_ATWRPEND
);
1883 dev_err(cs
->dev
, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1888 /* submitted successfully, start timeout if necessary */
1889 if (!(update_basstate(ucs
, BS_ATTIMER
, BS_ATREADY
) & BS_ATTIMER
)) {
1890 gig_dbg(DEBUG_OUTPUT
, "setting ATREADY timeout of %d/10 secs",
1892 mod_timer(&ucs
->timer_atrdy
, jiffies
+ ATRDY_TIMEOUT
* HZ
/ 10);
1898 * start transmission of AT command queue if necessary
1900 * cs controller state structure
1903 * error code < 0 on error
1905 static int start_cbsend(struct cardstate
*cs
)
1907 struct cmdbuf_t
*cb
;
1908 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1909 unsigned long flags
;
1913 /* check if suspend requested */
1914 if (ucs
->basstate
& BS_SUSPEND
) {
1915 gig_dbg(DEBUG_OUTPUT
, "suspending");
1916 return -EHOSTUNREACH
;
1919 /* check if AT channel is open */
1920 if (!(ucs
->basstate
& BS_ATOPEN
)) {
1921 gig_dbg(DEBUG_OUTPUT
, "AT channel not open");
1922 rc
= req_submit(cs
->bcs
, HD_OPEN_ATCHANNEL
, 0, BAS_TIMEOUT
);
1924 /* flush command queue */
1925 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1926 while (cs
->cmdbuf
!= NULL
)
1928 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1933 /* try to send first command in queue */
1934 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1936 while ((cb
= cs
->cmdbuf
) != NULL
&& (ucs
->basstate
& BS_ATREADY
)) {
1937 ucs
->retry_cmd_out
= 0;
1938 rc
= atwrite_submit(cs
, cb
->buf
, cb
->len
);
1945 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1949 /* gigaset_write_cmd
1950 * This function is called by the device independent part of the driver
1951 * to transmit an AT command string to the Gigaset device.
1952 * It encapsulates the device specific method for transmission over the
1953 * direct USB connection to the base.
1954 * The command string is added to the queue of commands to send, and
1955 * USB transmission is started if necessary.
1957 * cs controller state structure
1958 * cb command buffer structure
1960 * number of bytes queued on success
1961 * error code < 0 on error
1963 static int gigaset_write_cmd(struct cardstate
*cs
, struct cmdbuf_t
*cb
)
1965 unsigned long flags
;
1968 gigaset_dbg_buffer(cs
->mstate
!= MS_LOCKED
?
1969 DEBUG_TRANSCMD
: DEBUG_LOCKCMD
,
1970 "CMD Transmit", cb
->len
, cb
->buf
);
1972 /* translate "+++" escape sequence sent as a single separate command
1973 * into "close AT channel" command for error recovery
1974 * The next command will reopen the AT channel automatically.
1976 if (cb
->len
== 3 && !memcmp(cb
->buf
, "+++", 3)) {
1977 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1978 * because of an error, the base never sends another one.
1979 * The response channel is thus effectively blocked.
1980 * Closing and reopening the AT channel does *not* clear
1982 * As a stopgap measure, submit a zero-length AT read
1983 * before closing the AT channel. This has the undocumented
1984 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1985 * from the base if necessary.
1986 * The subsequent AT channel close then discards any pending
1989 spin_lock_irqsave(&cs
->lock
, flags
);
1990 if (!(cs
->hw
.bas
->basstate
& BS_ATRDPEND
)) {
1991 kfree(cs
->hw
.bas
->rcvbuf
);
1992 cs
->hw
.bas
->rcvbuf
= NULL
;
1993 cs
->hw
.bas
->rcvbuf_size
= 0;
1994 cs
->hw
.bas
->retry_cmd_in
= 0;
1995 atread_submit(cs
, 0);
1997 spin_unlock_irqrestore(&cs
->lock
, flags
);
1999 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, BAS_TIMEOUT
);
2000 if (cb
->wake_tasklet
)
2001 tasklet_schedule(cb
->wake_tasklet
);
2008 spin_lock_irqsave(&cs
->cmdlock
, flags
);
2009 cb
->prev
= cs
->lastcmdbuf
;
2011 cs
->lastcmdbuf
->next
= cb
;
2014 cs
->curlen
= cb
->len
;
2016 cs
->cmdbytes
+= cb
->len
;
2017 cs
->lastcmdbuf
= cb
;
2018 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
2020 spin_lock_irqsave(&cs
->lock
, flags
);
2021 if (unlikely(!cs
->connected
)) {
2022 spin_unlock_irqrestore(&cs
->lock
, flags
);
2023 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
2024 /* flush command queue */
2025 spin_lock_irqsave(&cs
->cmdlock
, flags
);
2026 while (cs
->cmdbuf
!= NULL
)
2028 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
2031 rc
= start_cbsend(cs
);
2032 spin_unlock_irqrestore(&cs
->lock
, flags
);
2033 return rc
< 0 ? rc
: cb
->len
;
2036 /* gigaset_write_room
2037 * tty_driver.write_room interface routine
2038 * return number of characters the driver will accept to be written via
2041 * controller state structure
2043 * number of characters
2045 static int gigaset_write_room(struct cardstate
*cs
)
2050 /* gigaset_chars_in_buffer
2051 * tty_driver.chars_in_buffer interface routine
2052 * return number of characters waiting to be sent
2054 * controller state structure
2056 * number of characters
2058 static int gigaset_chars_in_buffer(struct cardstate
*cs
)
2060 return cs
->cmdbytes
;
2064 * implementation of ioctl(GIGASET_BRKCHARS)
2066 * controller state structure
2068 * -EINVAL (unimplemented function)
2070 static int gigaset_brkchars(struct cardstate
*cs
, const unsigned char buf
[6])
2076 /* Device Initialization/Shutdown */
2077 /* ============================== */
2079 /* Free hardware dependent part of the B channel structure
2081 * bcs B channel structure
2085 static int gigaset_freebcshw(struct bc_state
*bcs
)
2087 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2093 /* kill URBs and tasklets before freeing - better safe than sorry */
2095 gig_dbg(DEBUG_INIT
, "%s: killing isoc URBs", __func__
);
2096 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2097 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2098 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2100 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2101 usb_kill_urb(ubc
->isoinurbs
[i
]);
2102 usb_free_urb(ubc
->isoinurbs
[i
]);
2104 tasklet_kill(&ubc
->sent_tasklet
);
2105 tasklet_kill(&ubc
->rcvd_tasklet
);
2106 kfree(ubc
->isooutbuf
);
2112 /* Initialize hardware dependent part of the B channel structure
2114 * bcs B channel structure
2118 static int gigaset_initbcshw(struct bc_state
*bcs
)
2121 struct bas_bc_state
*ubc
;
2123 bcs
->hw
.bas
= ubc
= kmalloc(sizeof(struct bas_bc_state
), GFP_KERNEL
);
2125 pr_err("out of memory\n");
2130 atomic_set(&ubc
->corrbytes
, 0);
2131 spin_lock_init(&ubc
->isooutlock
);
2132 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2133 ubc
->isoouturbs
[i
].urb
= NULL
;
2134 ubc
->isoouturbs
[i
].bcs
= bcs
;
2136 ubc
->isooutdone
= ubc
->isooutfree
= ubc
->isooutovfl
= NULL
;
2138 ubc
->isooutbuf
= kmalloc(sizeof(struct isowbuf_t
), GFP_KERNEL
);
2139 if (!ubc
->isooutbuf
) {
2140 pr_err("out of memory\n");
2145 tasklet_init(&ubc
->sent_tasklet
,
2146 write_iso_tasklet
, (unsigned long) bcs
);
2148 spin_lock_init(&ubc
->isoinlock
);
2149 for (i
= 0; i
< BAS_INURBS
; ++i
)
2150 ubc
->isoinurbs
[i
] = NULL
;
2151 ubc
->isoindone
= NULL
;
2152 ubc
->loststatus
= -EINPROGRESS
;
2166 tasklet_init(&ubc
->rcvd_tasklet
,
2167 read_iso_tasklet
, (unsigned long) bcs
);
2171 static void gigaset_reinitbcshw(struct bc_state
*bcs
)
2173 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2175 bcs
->hw
.bas
->running
= 0;
2176 atomic_set(&bcs
->hw
.bas
->corrbytes
, 0);
2177 bcs
->hw
.bas
->numsub
= 0;
2178 spin_lock_init(&ubc
->isooutlock
);
2179 spin_lock_init(&ubc
->isoinlock
);
2180 ubc
->loststatus
= -EINPROGRESS
;
2183 static void gigaset_freecshw(struct cardstate
*cs
)
2185 /* timers, URBs and rcvbuf are disposed of in disconnect */
2186 kfree(cs
->hw
.bas
->int_in_buf
);
2191 static int gigaset_initcshw(struct cardstate
*cs
)
2193 struct bas_cardstate
*ucs
;
2195 cs
->hw
.bas
= ucs
= kmalloc(sizeof *ucs
, GFP_KERNEL
);
2197 pr_err("out of memory\n");
2200 ucs
->int_in_buf
= kmalloc(IP_MSGSIZE
, GFP_KERNEL
);
2201 if (!ucs
->int_in_buf
) {
2203 pr_err("out of memory\n");
2207 ucs
->urb_cmd_in
= NULL
;
2208 ucs
->urb_cmd_out
= NULL
;
2210 ucs
->rcvbuf_size
= 0;
2212 spin_lock_init(&ucs
->lock
);
2216 setup_timer(&ucs
->timer_ctrl
, req_timeout
, (unsigned long) cs
);
2217 setup_timer(&ucs
->timer_atrdy
, atrdy_timeout
, (unsigned long) cs
);
2218 setup_timer(&ucs
->timer_cmd_in
, cmd_in_timeout
, (unsigned long) cs
);
2219 setup_timer(&ucs
->timer_int_in
, int_in_resubmit
, (unsigned long) cs
);
2220 init_waitqueue_head(&ucs
->waitqueue
);
2221 INIT_WORK(&ucs
->int_in_wq
, int_in_work
);
2227 * unlink and deallocate all URBs unconditionally
2228 * caller must make sure that no commands are still in progress
2230 * cs controller state structure
2232 static void freeurbs(struct cardstate
*cs
)
2234 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2235 struct bas_bc_state
*ubc
;
2238 gig_dbg(DEBUG_INIT
, "%s: killing URBs", __func__
);
2239 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2240 ubc
= cs
->bcs
[j
].hw
.bas
;
2241 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2242 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2243 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2244 ubc
->isoouturbs
[i
].urb
= NULL
;
2246 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2247 usb_kill_urb(ubc
->isoinurbs
[i
]);
2248 usb_free_urb(ubc
->isoinurbs
[i
]);
2249 ubc
->isoinurbs
[i
] = NULL
;
2252 usb_kill_urb(ucs
->urb_int_in
);
2253 usb_free_urb(ucs
->urb_int_in
);
2254 ucs
->urb_int_in
= NULL
;
2255 usb_kill_urb(ucs
->urb_cmd_out
);
2256 usb_free_urb(ucs
->urb_cmd_out
);
2257 ucs
->urb_cmd_out
= NULL
;
2258 usb_kill_urb(ucs
->urb_cmd_in
);
2259 usb_free_urb(ucs
->urb_cmd_in
);
2260 ucs
->urb_cmd_in
= NULL
;
2261 usb_kill_urb(ucs
->urb_ctrl
);
2262 usb_free_urb(ucs
->urb_ctrl
);
2263 ucs
->urb_ctrl
= NULL
;
2267 * This function is called when a new USB device is connected.
2268 * It checks whether the new device is handled by this driver.
2270 static int gigaset_probe(struct usb_interface
*interface
,
2271 const struct usb_device_id
*id
)
2273 struct usb_host_interface
*hostif
;
2274 struct usb_device
*udev
= interface_to_usbdev(interface
);
2275 struct cardstate
*cs
= NULL
;
2276 struct bas_cardstate
*ucs
= NULL
;
2277 struct bas_bc_state
*ubc
;
2278 struct usb_endpoint_descriptor
*endpoint
;
2283 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2284 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2285 le16_to_cpu(udev
->descriptor
.idProduct
));
2287 /* set required alternate setting */
2288 hostif
= interface
->cur_altsetting
;
2289 if (hostif
->desc
.bAlternateSetting
!= 3) {
2291 "%s: wrong alternate setting %d - trying to switch",
2292 __func__
, hostif
->desc
.bAlternateSetting
);
2293 if (usb_set_interface(udev
, hostif
->desc
.bInterfaceNumber
, 3)
2295 dev_warn(&udev
->dev
, "usb_set_interface failed, "
2296 "device %d interface %d altsetting %d\n",
2297 udev
->devnum
, hostif
->desc
.bInterfaceNumber
,
2298 hostif
->desc
.bAlternateSetting
);
2301 hostif
= interface
->cur_altsetting
;
2304 /* Reject application specific interfaces
2306 if (hostif
->desc
.bInterfaceClass
!= 255) {
2307 dev_warn(&udev
->dev
, "%s: bInterfaceClass == %d\n",
2308 __func__
, hostif
->desc
.bInterfaceClass
);
2312 dev_info(&udev
->dev
,
2313 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2314 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2315 le16_to_cpu(udev
->descriptor
.idProduct
));
2317 /* allocate memory for our device state and initialize it */
2318 cs
= gigaset_initcs(driver
, BAS_CHANNELS
, 0, 0, cidmode
,
2319 GIGASET_MODULENAME
);
2324 /* save off device structure ptrs for later use */
2327 ucs
->interface
= interface
;
2328 cs
->dev
= &interface
->dev
;
2331 * - one for the interrupt pipe
2332 * - three for the different uses of the default control pipe
2333 * - three for each isochronous pipe
2335 if (!(ucs
->urb_int_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2336 !(ucs
->urb_cmd_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2337 !(ucs
->urb_cmd_out
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2338 !(ucs
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
)))
2341 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2342 ubc
= cs
->bcs
[j
].hw
.bas
;
2343 for (i
= 0; i
< BAS_OUTURBS
; ++i
)
2344 if (!(ubc
->isoouturbs
[i
].urb
=
2345 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2347 for (i
= 0; i
< BAS_INURBS
; ++i
)
2348 if (!(ubc
->isoinurbs
[i
] =
2349 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2354 ucs
->rcvbuf_size
= 0;
2356 /* Fill the interrupt urb and send it to the core */
2357 endpoint
= &hostif
->endpoint
[0].desc
;
2358 usb_fill_int_urb(ucs
->urb_int_in
, udev
,
2359 usb_rcvintpipe(udev
,
2360 (endpoint
->bEndpointAddress
) & 0x0f),
2361 ucs
->int_in_buf
, IP_MSGSIZE
, read_int_callback
, cs
,
2362 endpoint
->bInterval
);
2363 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2365 dev_err(cs
->dev
, "could not submit interrupt URB: %s\n",
2369 ucs
->retry_int_in
= 0;
2371 /* tell the device that the driver is ready */
2372 rc
= req_submit(cs
->bcs
, HD_DEVICE_INIT_ACK
, 0, 0);
2376 /* tell common part that the device is ready */
2377 if (startmode
== SM_LOCKED
)
2378 cs
->mstate
= MS_LOCKED
;
2380 /* save address of controller structure */
2381 usb_set_intfdata(interface
, cs
);
2383 if (!gigaset_start(cs
))
2389 dev_err(cs
->dev
, "could not allocate URBs\n");
2392 usb_set_intfdata(interface
, NULL
);
2397 /* gigaset_disconnect
2398 * This function is called when the Gigaset base is unplugged.
2400 static void gigaset_disconnect(struct usb_interface
*interface
)
2402 struct cardstate
*cs
;
2403 struct bas_cardstate
*ucs
;
2406 cs
= usb_get_intfdata(interface
);
2410 dev_info(cs
->dev
, "disconnecting Gigaset base\n");
2412 /* mark base as not ready, all channels disconnected */
2415 /* tell LL all channels are down */
2416 for (j
= 0; j
< BAS_CHANNELS
; ++j
)
2417 gigaset_bchannel_down(cs
->bcs
+ j
);
2419 /* stop driver (common part) */
2422 /* stop delayed work and URBs, free ressources */
2423 del_timer_sync(&ucs
->timer_ctrl
);
2424 del_timer_sync(&ucs
->timer_atrdy
);
2425 del_timer_sync(&ucs
->timer_cmd_in
);
2426 del_timer_sync(&ucs
->timer_int_in
);
2427 cancel_work_sync(&ucs
->int_in_wq
);
2429 usb_set_intfdata(interface
, NULL
);
2432 ucs
->rcvbuf_size
= 0;
2433 usb_put_dev(ucs
->udev
);
2434 ucs
->interface
= NULL
;
2441 * This function is called before the USB connection is suspended.
2443 static int gigaset_suspend(struct usb_interface
*intf
, pm_message_t message
)
2445 struct cardstate
*cs
= usb_get_intfdata(intf
);
2446 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2449 /* set suspend flag; this stops AT command/response traffic */
2450 if (update_basstate(ucs
, BS_SUSPEND
, 0) & BS_SUSPEND
) {
2451 gig_dbg(DEBUG_SUSPEND
, "already suspended");
2455 /* wait a bit for blocking conditions to go away */
2456 rc
= wait_event_timeout(ucs
->waitqueue
,
2458 (BS_B1OPEN
| BS_B2OPEN
| BS_ATRDPEND
| BS_ATWRPEND
)),
2459 BAS_TIMEOUT
* HZ
/ 10);
2460 gig_dbg(DEBUG_SUSPEND
, "wait_event_timeout() -> %d", rc
);
2462 /* check for conditions preventing suspend */
2463 if (ucs
->basstate
& (BS_B1OPEN
| BS_B2OPEN
| BS_ATRDPEND
| BS_ATWRPEND
)) {
2464 dev_warn(cs
->dev
, "cannot suspend:\n");
2465 if (ucs
->basstate
& BS_B1OPEN
)
2466 dev_warn(cs
->dev
, " B channel 1 open\n");
2467 if (ucs
->basstate
& BS_B2OPEN
)
2468 dev_warn(cs
->dev
, " B channel 2 open\n");
2469 if (ucs
->basstate
& BS_ATRDPEND
)
2470 dev_warn(cs
->dev
, " receiving AT reply\n");
2471 if (ucs
->basstate
& BS_ATWRPEND
)
2472 dev_warn(cs
->dev
, " sending AT command\n");
2473 update_basstate(ucs
, 0, BS_SUSPEND
);
2477 /* close AT channel if open */
2478 if (ucs
->basstate
& BS_ATOPEN
) {
2479 gig_dbg(DEBUG_SUSPEND
, "closing AT channel");
2480 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, 0);
2482 update_basstate(ucs
, 0, BS_SUSPEND
);
2485 wait_event_timeout(ucs
->waitqueue
, !ucs
->pending
,
2486 BAS_TIMEOUT
* HZ
/ 10);
2487 /* in case of timeout, proceed anyway */
2490 /* kill all URBs and delayed work that might still be pending */
2491 usb_kill_urb(ucs
->urb_ctrl
);
2492 usb_kill_urb(ucs
->urb_int_in
);
2493 del_timer_sync(&ucs
->timer_ctrl
);
2494 del_timer_sync(&ucs
->timer_atrdy
);
2495 del_timer_sync(&ucs
->timer_cmd_in
);
2496 del_timer_sync(&ucs
->timer_int_in
);
2497 cancel_work_sync(&ucs
->int_in_wq
);
2499 gig_dbg(DEBUG_SUSPEND
, "suspend complete");
2504 * This function is called after the USB connection has been resumed.
2506 static int gigaset_resume(struct usb_interface
*intf
)
2508 struct cardstate
*cs
= usb_get_intfdata(intf
);
2509 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2512 /* resubmit interrupt URB for spontaneous messages from base */
2513 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2515 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
2519 ucs
->retry_int_in
= 0;
2521 /* clear suspend flag to reallow activity */
2522 update_basstate(ucs
, 0, BS_SUSPEND
);
2524 gig_dbg(DEBUG_SUSPEND
, "resume complete");
2528 /* gigaset_pre_reset
2529 * This function is called before the USB connection is reset.
2531 static int gigaset_pre_reset(struct usb_interface
*intf
)
2533 /* handle just like suspend */
2534 return gigaset_suspend(intf
, PMSG_ON
);
2537 /* gigaset_post_reset
2538 * This function is called after the USB connection has been reset.
2540 static int gigaset_post_reset(struct usb_interface
*intf
)
2542 /* FIXME: send HD_DEVICE_INIT_ACK? */
2544 /* resume operations */
2545 return gigaset_resume(intf
);
2549 static const struct gigaset_ops gigops
= {
2552 gigaset_chars_in_buffer
,
2554 gigaset_init_bchannel
,
2555 gigaset_close_bchannel
,
2558 gigaset_reinitbcshw
,
2561 gigaset_set_modem_ctrl
,
2563 gigaset_set_line_ctrl
,
2564 gigaset_isoc_send_skb
,
2569 * This function is called after the kernel module is loaded.
2571 static int __init
bas_gigaset_init(void)
2575 /* allocate memory for our driver state and initialize it */
2576 driver
= gigaset_initdriver(GIGASET_MINOR
, GIGASET_MINORS
,
2577 GIGASET_MODULENAME
, GIGASET_DEVNAME
,
2578 &gigops
, THIS_MODULE
);
2582 /* register this driver with the USB subsystem */
2583 result
= usb_register(&gigaset_usb_driver
);
2585 pr_err("error %d registering USB driver\n", -result
);
2589 pr_info(DRIVER_DESC
"\n");
2594 gigaset_freedriver(driver
);
2600 * This function is called before the kernel module is unloaded.
2602 static void __exit
bas_gigaset_exit(void)
2604 struct bas_cardstate
*ucs
;
2607 gigaset_blockdriver(driver
); /* => probe will fail
2608 * => no gigaset_start any more
2611 /* stop all connected devices */
2612 for (i
= 0; i
< driver
->minors
; i
++) {
2613 if (gigaset_shutdown(driver
->cs
+ i
) < 0)
2614 continue; /* no device */
2615 /* from now on, no isdn callback should be possible */
2617 /* close all still open channels */
2618 ucs
= driver
->cs
[i
].hw
.bas
;
2619 if (ucs
->basstate
& BS_B1OPEN
) {
2620 gig_dbg(DEBUG_INIT
, "closing B1 channel");
2621 usb_control_msg(ucs
->udev
,
2622 usb_sndctrlpipe(ucs
->udev
, 0),
2623 HD_CLOSE_B1CHANNEL
, OUT_VENDOR_REQ
,
2624 0, 0, NULL
, 0, BAS_TIMEOUT
);
2626 if (ucs
->basstate
& BS_B2OPEN
) {
2627 gig_dbg(DEBUG_INIT
, "closing B2 channel");
2628 usb_control_msg(ucs
->udev
,
2629 usb_sndctrlpipe(ucs
->udev
, 0),
2630 HD_CLOSE_B2CHANNEL
, OUT_VENDOR_REQ
,
2631 0, 0, NULL
, 0, BAS_TIMEOUT
);
2633 if (ucs
->basstate
& BS_ATOPEN
) {
2634 gig_dbg(DEBUG_INIT
, "closing AT channel");
2635 usb_control_msg(ucs
->udev
,
2636 usb_sndctrlpipe(ucs
->udev
, 0),
2637 HD_CLOSE_ATCHANNEL
, OUT_VENDOR_REQ
,
2638 0, 0, NULL
, 0, BAS_TIMEOUT
);
2643 /* deregister this driver with the USB subsystem */
2644 usb_deregister(&gigaset_usb_driver
);
2645 /* this will call the disconnect-callback */
2646 /* from now on, no disconnect/probe callback should be running */
2648 gigaset_freedriver(driver
);
2653 module_init(bas_gigaset_init
);
2654 module_exit(bas_gigaset_exit
);
2656 MODULE_AUTHOR(DRIVER_AUTHOR
);
2657 MODULE_DESCRIPTION(DRIVER_DESC
);
2658 MODULE_LICENSE("GPL");