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
,
153 /* get message text for usb_submit_urb return code
155 static char *get_usb_rcmsg(int rc
)
157 static char unkmsg
[28];
163 return "out of memory";
165 return "device not present";
167 return "endpoint not present";
169 return "URB type not supported";
171 return "invalid argument";
173 return "start frame too early or too much scheduled";
175 return "too many isoc frames requested";
177 return "endpoint stalled";
179 return "invalid packet size";
181 return "would overcommit USB bandwidth";
183 return "device shut down";
185 return "reject flag set";
187 return "device suspended";
189 snprintf(unkmsg
, sizeof(unkmsg
), "unknown error %d", rc
);
194 /* get message text for USB status code
196 static char *get_usb_statmsg(int status
)
198 static char unkmsg
[28];
204 return "unlinked (sync)";
206 return "URB still pending";
208 return "bitstuff error, timeout, or unknown USB error";
210 return "CRC mismatch, timeout, or unknown USB error";
212 return "USB response timeout";
214 return "endpoint stalled";
216 return "IN buffer overrun";
218 return "OUT buffer underrun";
220 return "endpoint babble";
222 return "short packet";
224 return "device removed";
226 return "partial isoc transfer";
228 return "ISO madness";
230 return "unlinked (async)";
232 return "device shut down";
234 snprintf(unkmsg
, sizeof(unkmsg
), "unknown status %d", status
);
240 * retrieve string representation of USB pipe type
242 static inline char *usb_pipetype_str(int pipe
)
244 if (usb_pipeisoc(pipe
))
246 if (usb_pipeint(pipe
))
248 if (usb_pipecontrol(pipe
))
250 if (usb_pipebulk(pipe
))
256 * write content of URB to syslog for debugging
258 static inline void dump_urb(enum debuglevel level
, const char *tag
,
261 #ifdef CONFIG_GIGASET_DEBUG
263 gig_dbg(level
, "%s urb(0x%08lx)->{", tag
, (unsigned long) urb
);
266 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
267 "hcpriv=0x%08lx, transfer_flags=0x%x,",
268 (unsigned long) urb
->dev
,
269 usb_pipetype_str(urb
->pipe
),
270 usb_pipeendpoint(urb
->pipe
), usb_pipedevice(urb
->pipe
),
271 usb_pipein(urb
->pipe
) ? "in" : "out",
272 (unsigned long) urb
->hcpriv
,
273 urb
->transfer_flags
);
275 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
276 "setup_packet=0x%08lx,",
277 (unsigned long) urb
->transfer_buffer
,
278 urb
->transfer_buffer_length
, urb
->actual_length
,
279 (unsigned long) urb
->setup_packet
);
281 " start_frame=%d, number_of_packets=%d, interval=%d, "
283 urb
->start_frame
, urb
->number_of_packets
, urb
->interval
,
286 " context=0x%08lx, complete=0x%08lx, "
287 "iso_frame_desc[]={",
288 (unsigned long) urb
->context
,
289 (unsigned long) urb
->complete
);
290 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
291 struct usb_iso_packet_descriptor
*pifd
292 = &urb
->iso_frame_desc
[i
];
294 " {offset=%u, length=%u, actual_length=%u, "
296 pifd
->offset
, pifd
->length
, pifd
->actual_length
,
300 gig_dbg(level
, "}}");
304 /* read/set modem control bits etc. (m10x only) */
305 static int gigaset_set_modem_ctrl(struct cardstate
*cs
, unsigned old_state
,
311 static int gigaset_baud_rate(struct cardstate
*cs
, unsigned cflag
)
316 static int gigaset_set_line_ctrl(struct cardstate
*cs
, unsigned cflag
)
321 /* set/clear bits in base connection state, return previous state
323 static inline int update_basstate(struct bas_cardstate
*ucs
,
329 spin_lock_irqsave(&ucs
->lock
, flags
);
330 state
= ucs
->basstate
;
331 ucs
->basstate
= (state
& ~clear
) | set
;
332 spin_unlock_irqrestore(&ucs
->lock
, flags
);
337 * hang up any existing connection because of an unrecoverable error
338 * This function may be called from any context and takes care of scheduling
339 * the necessary actions for execution outside of interrupt context.
340 * cs->lock must not be held.
342 * B channel control structure
344 static inline void error_hangup(struct bc_state
*bcs
)
346 struct cardstate
*cs
= bcs
->cs
;
348 gigaset_add_event(cs
, &bcs
->at_state
, EV_HUP
, NULL
, 0, NULL
);
349 gigaset_schedule_event(cs
);
353 * reset Gigaset device because of an unrecoverable error
354 * This function may be called from any context, and takes care of
355 * scheduling the necessary actions for execution outside of interrupt context.
356 * cs->hw.bas->lock must not be held.
358 * controller state structure
360 static inline void error_reset(struct cardstate
*cs
)
362 /* reset interrupt pipe to recover (ignore errors) */
363 update_basstate(cs
->hw
.bas
, BS_RESETTING
, 0);
364 if (req_submit(cs
->bcs
, HD_RESET_INTERRUPT_PIPE
, 0, BAS_TIMEOUT
))
365 /* submission failed, escalate to USB port reset */
366 usb_queue_reset_device(cs
->hw
.bas
->interface
);
370 * check for completion of pending control request
372 * ucs hardware specific controller state structure
374 static void check_pending(struct bas_cardstate
*ucs
)
378 spin_lock_irqsave(&ucs
->lock
, flags
);
379 switch (ucs
->pending
) {
382 case HD_OPEN_ATCHANNEL
:
383 if (ucs
->basstate
& BS_ATOPEN
)
386 case HD_OPEN_B1CHANNEL
:
387 if (ucs
->basstate
& BS_B1OPEN
)
390 case HD_OPEN_B2CHANNEL
:
391 if (ucs
->basstate
& BS_B2OPEN
)
394 case HD_CLOSE_ATCHANNEL
:
395 if (!(ucs
->basstate
& BS_ATOPEN
))
398 case HD_CLOSE_B1CHANNEL
:
399 if (!(ucs
->basstate
& BS_B1OPEN
))
402 case HD_CLOSE_B2CHANNEL
:
403 if (!(ucs
->basstate
& BS_B2OPEN
))
406 case HD_DEVICE_INIT_ACK
: /* no reply expected */
409 case HD_RESET_INTERRUPT_PIPE
:
410 if (!(ucs
->basstate
& BS_RESETTING
))
414 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
415 * and should never end up here
418 dev_warn(&ucs
->interface
->dev
,
419 "unknown pending request 0x%02x cleared\n",
425 del_timer(&ucs
->timer_ctrl
);
427 spin_unlock_irqrestore(&ucs
->lock
, flags
);
431 * timeout routine for command input request
433 * controller state structure
435 static void cmd_in_timeout(unsigned long data
)
437 struct cardstate
*cs
= (struct cardstate
*) data
;
438 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
441 if (!ucs
->rcvbuf_size
) {
442 gig_dbg(DEBUG_USBREQ
, "%s: no receive in progress", __func__
);
446 if (ucs
->retry_cmd_in
++ >= BAS_RETRY
) {
448 "control read: timeout, giving up after %d tries\n",
452 ucs
->rcvbuf_size
= 0;
457 gig_dbg(DEBUG_USBREQ
, "%s: timeout, retry %d",
458 __func__
, ucs
->retry_cmd_in
);
459 rc
= atread_submit(cs
, BAS_TIMEOUT
);
463 ucs
->rcvbuf_size
= 0;
469 /* read_ctrl_callback
470 * USB completion handler for control pipe input
471 * called by the USB subsystem in interrupt context
473 * urb USB request block
474 * urb->context = inbuf structure for controller state
476 static void read_ctrl_callback(struct urb
*urb
)
478 struct inbuf_t
*inbuf
= urb
->context
;
479 struct cardstate
*cs
= inbuf
->cs
;
480 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
481 int status
= urb
->status
;
485 update_basstate(ucs
, 0, BS_ATRDPEND
);
486 wake_up(&ucs
->waitqueue
);
487 del_timer(&ucs
->timer_cmd_in
);
490 case 0: /* normal completion */
491 numbytes
= urb
->actual_length
;
492 if (unlikely(numbytes
!= ucs
->rcvbuf_size
)) {
494 "control read: received %d chars, expected %d\n",
495 numbytes
, ucs
->rcvbuf_size
);
496 if (numbytes
> ucs
->rcvbuf_size
)
497 numbytes
= ucs
->rcvbuf_size
;
500 /* copy received bytes to inbuf, notify event layer */
501 if (gigaset_fill_inbuf(inbuf
, ucs
->rcvbuf
, numbytes
)) {
502 gig_dbg(DEBUG_INTR
, "%s-->BH", __func__
);
503 gigaset_schedule_event(cs
);
507 case -ENOENT
: /* cancelled */
508 case -ECONNRESET
: /* cancelled (async) */
509 case -EINPROGRESS
: /* pending */
510 case -ENODEV
: /* device removed */
511 case -ESHUTDOWN
: /* device shut down */
512 /* no further action necessary */
513 gig_dbg(DEBUG_USBREQ
, "%s: %s",
514 __func__
, get_usb_statmsg(status
));
517 default: /* other errors: retry */
518 if (ucs
->retry_cmd_in
++ < BAS_RETRY
) {
519 gig_dbg(DEBUG_USBREQ
, "%s: %s, retry %d", __func__
,
520 get_usb_statmsg(status
), ucs
->retry_cmd_in
);
521 rc
= atread_submit(cs
, BAS_TIMEOUT
);
523 /* successfully resubmitted, skip freeing */
526 /* disconnect, no further action necessary */
529 dev_err(cs
->dev
, "control read: %s, giving up after %d tries\n",
530 get_usb_statmsg(status
), ucs
->retry_cmd_in
);
534 /* read finished, free buffer */
537 ucs
->rcvbuf_size
= 0;
541 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
543 * cs controller state structure
544 * timeout timeout in 1/10 sec., 0: none
547 * -EBUSY if another request is pending
548 * any URB submission error code
550 static int atread_submit(struct cardstate
*cs
, int timeout
)
552 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
556 gig_dbg(DEBUG_USBREQ
, "-------> HD_READ_ATMESSAGE (%d)",
559 basstate
= update_basstate(ucs
, BS_ATRDPEND
, 0);
560 if (basstate
& BS_ATRDPEND
) {
562 "could not submit HD_READ_ATMESSAGE: URB busy\n");
566 if (basstate
& BS_SUSPEND
) {
568 "HD_READ_ATMESSAGE not submitted, "
569 "suspend in progress\n");
570 update_basstate(ucs
, 0, BS_ATRDPEND
);
571 /* treat like disconnect */
575 ucs
->dr_cmd_in
.bRequestType
= IN_VENDOR_REQ
;
576 ucs
->dr_cmd_in
.bRequest
= HD_READ_ATMESSAGE
;
577 ucs
->dr_cmd_in
.wValue
= 0;
578 ucs
->dr_cmd_in
.wIndex
= 0;
579 ucs
->dr_cmd_in
.wLength
= cpu_to_le16(ucs
->rcvbuf_size
);
580 usb_fill_control_urb(ucs
->urb_cmd_in
, ucs
->udev
,
581 usb_rcvctrlpipe(ucs
->udev
, 0),
582 (unsigned char *) &ucs
->dr_cmd_in
,
583 ucs
->rcvbuf
, ucs
->rcvbuf_size
,
584 read_ctrl_callback
, cs
->inbuf
);
586 ret
= usb_submit_urb(ucs
->urb_cmd_in
, GFP_ATOMIC
);
588 update_basstate(ucs
, 0, BS_ATRDPEND
);
589 dev_err(cs
->dev
, "could not submit HD_READ_ATMESSAGE: %s\n",
595 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
596 mod_timer(&ucs
->timer_cmd_in
, jiffies
+ timeout
* HZ
/ 10);
602 * workqueue routine to clear halt on interrupt in endpoint
605 static void int_in_work(struct work_struct
*work
)
607 struct bas_cardstate
*ucs
=
608 container_of(work
, struct bas_cardstate
, int_in_wq
);
609 struct urb
*urb
= ucs
->urb_int_in
;
610 struct cardstate
*cs
= urb
->context
;
613 /* clear halt condition */
614 rc
= usb_clear_halt(ucs
->udev
, urb
->pipe
);
615 gig_dbg(DEBUG_USBREQ
, "clear_halt: %s", get_usb_rcmsg(rc
));
617 /* success, resubmit interrupt read URB */
618 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
619 if (rc
!= 0 && rc
!= -ENODEV
) {
620 dev_err(cs
->dev
, "clear halt failed: %s\n", get_usb_rcmsg(rc
));
621 rc
= usb_lock_device_for_reset(ucs
->udev
, ucs
->interface
);
623 rc
= usb_reset_device(ucs
->udev
);
624 usb_unlock_device(ucs
->udev
);
627 ucs
->retry_int_in
= 0;
631 * timer routine for interrupt read delayed resubmit
633 * controller state structure
635 static void int_in_resubmit(unsigned long data
)
637 struct cardstate
*cs
= (struct cardstate
*) data
;
638 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
641 if (ucs
->retry_int_in
++ >= BAS_RETRY
) {
642 dev_err(cs
->dev
, "interrupt read: giving up after %d tries\n",
644 usb_queue_reset_device(ucs
->interface
);
648 gig_dbg(DEBUG_USBREQ
, "%s: retry %d", __func__
, ucs
->retry_int_in
);
649 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_ATOMIC
);
650 if (rc
!= 0 && rc
!= -ENODEV
) {
651 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
653 usb_queue_reset_device(ucs
->interface
);
658 * USB completion handler for interrupt pipe input
659 * called by the USB subsystem in interrupt context
661 * urb USB request block
662 * urb->context = controller state structure
664 static void read_int_callback(struct urb
*urb
)
666 struct cardstate
*cs
= urb
->context
;
667 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
668 struct bc_state
*bcs
;
669 int status
= urb
->status
;
676 case 0: /* success */
677 ucs
->retry_int_in
= 0;
679 case -EPIPE
: /* endpoint stalled */
680 schedule_work(&ucs
->int_in_wq
);
682 case -ENOENT
: /* cancelled */
683 case -ECONNRESET
: /* cancelled (async) */
684 case -EINPROGRESS
: /* pending */
685 case -ENODEV
: /* device removed */
686 case -ESHUTDOWN
: /* device shut down */
687 /* no further action necessary */
688 gig_dbg(DEBUG_USBREQ
, "%s: %s",
689 __func__
, get_usb_statmsg(status
));
691 case -EPROTO
: /* protocol error or unplug */
694 /* resubmit after delay */
695 gig_dbg(DEBUG_USBREQ
, "%s: %s",
696 __func__
, get_usb_statmsg(status
));
697 mod_timer(&ucs
->timer_int_in
, jiffies
+ HZ
/ 10);
699 default: /* other errors: just resubmit */
700 dev_warn(cs
->dev
, "interrupt read: %s\n",
701 get_usb_statmsg(status
));
705 /* drop incomplete packets even if the missing bytes wouldn't matter */
706 if (unlikely(urb
->actual_length
< IP_MSGSIZE
)) {
707 dev_warn(cs
->dev
, "incomplete interrupt packet (%d bytes)\n",
712 l
= (unsigned) ucs
->int_in_buf
[1] +
713 (((unsigned) ucs
->int_in_buf
[2]) << 8);
715 gig_dbg(DEBUG_USBREQ
, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
716 urb
->actual_length
, (int)ucs
->int_in_buf
[0], l
,
717 (int)ucs
->int_in_buf
[1], (int)ucs
->int_in_buf
[2]);
721 switch (ucs
->int_in_buf
[0]) {
722 case HD_DEVICE_INIT_OK
:
723 update_basstate(ucs
, BS_INIT
, 0);
726 case HD_READY_SEND_ATDATA
:
727 del_timer(&ucs
->timer_atrdy
);
728 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
732 case HD_OPEN_B2CHANNEL_ACK
:
734 case HD_OPEN_B1CHANNEL_ACK
:
735 bcs
= cs
->bcs
+ channel
;
736 update_basstate(ucs
, BS_B1OPEN
<< channel
, 0);
737 gigaset_bchannel_up(bcs
);
740 case HD_OPEN_ATCHANNEL_ACK
:
741 update_basstate(ucs
, BS_ATOPEN
, 0);
745 case HD_CLOSE_B2CHANNEL_ACK
:
747 case HD_CLOSE_B1CHANNEL_ACK
:
748 bcs
= cs
->bcs
+ channel
;
749 update_basstate(ucs
, 0, BS_B1OPEN
<< channel
);
750 stopurbs(bcs
->hw
.bas
);
751 gigaset_bchannel_down(bcs
);
754 case HD_CLOSE_ATCHANNEL_ACK
:
755 update_basstate(ucs
, 0, BS_ATOPEN
);
758 case HD_B2_FLOW_CONTROL
:
760 case HD_B1_FLOW_CONTROL
:
761 bcs
= cs
->bcs
+ channel
;
762 atomic_add((l
- BAS_NORMFRAME
) * BAS_CORRFRAMES
,
763 &bcs
->hw
.bas
->corrbytes
);
765 "Flow control (channel %d, sub %d): 0x%02x => %d",
766 channel
, bcs
->hw
.bas
->numsub
, l
,
767 atomic_read(&bcs
->hw
.bas
->corrbytes
));
770 case HD_RECEIVEATDATA_ACK
: /* AT response ready to be received */
773 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
776 spin_lock_irqsave(&cs
->lock
, flags
);
777 if (ucs
->basstate
& BS_ATRDPEND
) {
778 spin_unlock_irqrestore(&cs
->lock
, flags
);
780 "HD_RECEIVEATDATA_ACK(%d) during HD_READ_ATMESSAGE(%d) ignored\n",
781 l
, ucs
->rcvbuf_size
);
784 if (ucs
->rcvbuf_size
) {
785 /* throw away previous buffer - we have no queue */
787 "receive AT data overrun, %d bytes lost\n",
790 ucs
->rcvbuf_size
= 0;
792 ucs
->rcvbuf
= kmalloc(l
, GFP_ATOMIC
);
793 if (ucs
->rcvbuf
== NULL
) {
794 spin_unlock_irqrestore(&cs
->lock
, flags
);
795 dev_err(cs
->dev
, "out of memory receiving AT data\n");
798 ucs
->rcvbuf_size
= l
;
799 ucs
->retry_cmd_in
= 0;
800 rc
= atread_submit(cs
, BAS_TIMEOUT
);
804 ucs
->rcvbuf_size
= 0;
806 spin_unlock_irqrestore(&cs
->lock
, flags
);
807 if (rc
< 0 && rc
!= -ENODEV
)
811 case HD_RESET_INTERRUPT_PIPE_ACK
:
812 update_basstate(ucs
, 0, BS_RESETTING
);
813 dev_notice(cs
->dev
, "interrupt pipe reset\n");
817 gig_dbg(DEBUG_USBREQ
, "HD_SUSPEND_END");
822 "unknown Gigaset signal 0x%02x (%u) ignored\n",
823 (int) ucs
->int_in_buf
[0], l
);
827 wake_up(&ucs
->waitqueue
);
830 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
831 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
832 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
839 * USB completion handler for B channel isochronous input
840 * called by the USB subsystem in interrupt context
842 * urb USB request block of completed request
843 * urb->context = bc_state structure
845 static void read_iso_callback(struct urb
*urb
)
847 struct bc_state
*bcs
;
848 struct bas_bc_state
*ubc
;
849 int status
= urb
->status
;
853 /* status codes not worth bothering the tasklet with */
854 if (unlikely(status
== -ENOENT
||
855 status
== -ECONNRESET
||
856 status
== -EINPROGRESS
||
858 status
== -ESHUTDOWN
)) {
859 gig_dbg(DEBUG_ISO
, "%s: %s",
860 __func__
, get_usb_statmsg(status
));
867 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
868 if (likely(ubc
->isoindone
== NULL
)) {
869 /* pass URB to tasklet */
870 ubc
->isoindone
= urb
;
871 ubc
->isoinstatus
= status
;
872 tasklet_hi_schedule(&ubc
->rcvd_tasklet
);
874 /* tasklet still busy, drop data and resubmit URB */
875 gig_dbg(DEBUG_ISO
, "%s: overrun", __func__
);
876 ubc
->loststatus
= status
;
877 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
878 ubc
->isoinlost
+= urb
->iso_frame_desc
[i
].actual_length
;
879 if (unlikely(urb
->iso_frame_desc
[i
].status
!= 0 &&
880 urb
->iso_frame_desc
[i
].status
!=
882 ubc
->loststatus
= urb
->iso_frame_desc
[i
].status
;
883 urb
->iso_frame_desc
[i
].status
= 0;
884 urb
->iso_frame_desc
[i
].actual_length
= 0;
886 if (likely(ubc
->running
)) {
887 /* urb->dev is clobbered by USB subsystem */
888 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
889 urb
->transfer_flags
= URB_ISO_ASAP
;
890 urb
->number_of_packets
= BAS_NUMFRAMES
;
891 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
892 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
893 dev_err(bcs
->cs
->dev
,
894 "could not resubmit isoc read URB: %s\n",
896 dump_urb(DEBUG_ISO
, "isoc read", urb
);
901 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
904 /* write_iso_callback
905 * USB completion handler for B channel isochronous output
906 * called by the USB subsystem in interrupt context
908 * urb USB request block of completed request
909 * urb->context = isow_urbctx_t structure
911 static void write_iso_callback(struct urb
*urb
)
913 struct isow_urbctx_t
*ucx
;
914 struct bas_bc_state
*ubc
;
915 int status
= urb
->status
;
918 /* status codes not worth bothering the tasklet with */
919 if (unlikely(status
== -ENOENT
||
920 status
== -ECONNRESET
||
921 status
== -EINPROGRESS
||
923 status
== -ESHUTDOWN
)) {
924 gig_dbg(DEBUG_ISO
, "%s: %s",
925 __func__
, get_usb_statmsg(status
));
929 /* pass URB context to tasklet */
931 ubc
= ucx
->bcs
->hw
.bas
;
932 ucx
->status
= status
;
934 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
935 ubc
->isooutovfl
= ubc
->isooutdone
;
936 ubc
->isooutdone
= ucx
;
937 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
938 tasklet_hi_schedule(&ubc
->sent_tasklet
);
942 * prepare and submit USB request blocks for isochronous input and output
944 * B channel control structure
947 * < 0 on error (no URBs submitted)
949 static int starturbs(struct bc_state
*bcs
)
951 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
956 /* initialize L2 reception */
957 if (bcs
->proto2
== L2_HDLC
)
958 bcs
->inputstate
|= INS_flag_hunt
;
960 /* submit all isochronous input URBs */
962 for (k
= 0; k
< BAS_INURBS
; k
++) {
963 urb
= ubc
->isoinurbs
[k
];
969 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
970 urb
->pipe
= usb_rcvisocpipe(urb
->dev
, 3 + 2 * bcs
->channel
);
971 urb
->transfer_flags
= URB_ISO_ASAP
;
972 urb
->transfer_buffer
= ubc
->isoinbuf
+ k
* BAS_INBUFSIZE
;
973 urb
->transfer_buffer_length
= BAS_INBUFSIZE
;
974 urb
->number_of_packets
= BAS_NUMFRAMES
;
975 urb
->interval
= BAS_FRAMETIME
;
976 urb
->complete
= read_iso_callback
;
978 for (j
= 0; j
< BAS_NUMFRAMES
; j
++) {
979 urb
->iso_frame_desc
[j
].offset
= j
* BAS_MAXFRAME
;
980 urb
->iso_frame_desc
[j
].length
= BAS_MAXFRAME
;
981 urb
->iso_frame_desc
[j
].status
= 0;
982 urb
->iso_frame_desc
[j
].actual_length
= 0;
985 dump_urb(DEBUG_ISO
, "Initial isoc read", urb
);
986 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
991 /* initialize L2 transmission */
992 gigaset_isowbuf_init(ubc
->isooutbuf
, PPP_FLAG
);
994 /* set up isochronous output URBs for flag idling */
995 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
996 urb
= ubc
->isoouturbs
[k
].urb
;
1001 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1002 urb
->pipe
= usb_sndisocpipe(urb
->dev
, 4 + 2 * bcs
->channel
);
1003 urb
->transfer_flags
= URB_ISO_ASAP
;
1004 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1005 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1006 urb
->number_of_packets
= BAS_NUMFRAMES
;
1007 urb
->interval
= BAS_FRAMETIME
;
1008 urb
->complete
= write_iso_callback
;
1009 urb
->context
= &ubc
->isoouturbs
[k
];
1010 for (j
= 0; j
< BAS_NUMFRAMES
; ++j
) {
1011 urb
->iso_frame_desc
[j
].offset
= BAS_OUTBUFSIZE
;
1012 urb
->iso_frame_desc
[j
].length
= BAS_NORMFRAME
;
1013 urb
->iso_frame_desc
[j
].status
= 0;
1014 urb
->iso_frame_desc
[j
].actual_length
= 0;
1016 ubc
->isoouturbs
[k
].limit
= -1;
1019 /* keep one URB free, submit the others */
1020 for (k
= 0; k
< BAS_OUTURBS
-1; ++k
) {
1021 dump_urb(DEBUG_ISO
, "Initial isoc write", urb
);
1022 rc
= usb_submit_urb(ubc
->isoouturbs
[k
].urb
, GFP_ATOMIC
);
1026 dump_urb(DEBUG_ISO
, "Initial isoc write (free)", urb
);
1027 ubc
->isooutfree
= &ubc
->isoouturbs
[BAS_OUTURBS
-1];
1028 ubc
->isooutdone
= ubc
->isooutovfl
= NULL
;
1036 * cancel the USB request blocks for isochronous input and output
1037 * errors are silently ignored
1039 * B channel control structure
1041 static void stopurbs(struct bas_bc_state
*ubc
)
1047 for (k
= 0; k
< BAS_INURBS
; ++k
) {
1048 rc
= usb_unlink_urb(ubc
->isoinurbs
[k
]);
1050 "%s: isoc input URB %d unlinked, result = %s",
1051 __func__
, k
, get_usb_rcmsg(rc
));
1054 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
1055 rc
= usb_unlink_urb(ubc
->isoouturbs
[k
].urb
);
1057 "%s: isoc output URB %d unlinked, result = %s",
1058 __func__
, k
, get_usb_rcmsg(rc
));
1062 /* Isochronous Write - Bottom Half */
1063 /* =============================== */
1065 /* submit_iso_write_urb
1066 * fill and submit the next isochronous write URB
1068 * ucx context structure containing URB
1070 * number of frames submitted in URB
1071 * 0 if URB not submitted because no data available (isooutbuf busy)
1072 * error code < 0 on error
1074 static int submit_iso_write_urb(struct isow_urbctx_t
*ucx
)
1076 struct urb
*urb
= ucx
->urb
;
1077 struct bas_bc_state
*ubc
= ucx
->bcs
->hw
.bas
;
1078 struct usb_iso_packet_descriptor
*ifd
;
1079 int corrbytes
, nframe
, rc
;
1081 /* urb->dev is clobbered by USB subsystem */
1082 urb
->dev
= ucx
->bcs
->cs
->hw
.bas
->udev
;
1083 urb
->transfer_flags
= URB_ISO_ASAP
;
1084 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1085 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1087 for (nframe
= 0; nframe
< BAS_NUMFRAMES
; nframe
++) {
1088 ifd
= &urb
->iso_frame_desc
[nframe
];
1090 /* compute frame length according to flow control */
1091 ifd
->length
= BAS_NORMFRAME
;
1092 corrbytes
= atomic_read(&ubc
->corrbytes
);
1093 if (corrbytes
!= 0) {
1094 gig_dbg(DEBUG_ISO
, "%s: corrbytes=%d",
1095 __func__
, corrbytes
);
1096 if (corrbytes
> BAS_HIGHFRAME
- BAS_NORMFRAME
)
1097 corrbytes
= BAS_HIGHFRAME
- BAS_NORMFRAME
;
1098 else if (corrbytes
< BAS_LOWFRAME
- BAS_NORMFRAME
)
1099 corrbytes
= BAS_LOWFRAME
- BAS_NORMFRAME
;
1100 ifd
->length
+= corrbytes
;
1101 atomic_add(-corrbytes
, &ubc
->corrbytes
);
1104 /* retrieve block of data to send */
1105 rc
= gigaset_isowbuf_getbytes(ubc
->isooutbuf
, ifd
->length
);
1109 "%s: buffer busy at frame %d",
1111 /* tasklet will be restarted from
1112 gigaset_isoc_send_skb() */
1114 dev_err(ucx
->bcs
->cs
->dev
,
1115 "%s: buffer error %d at frame %d\n",
1116 __func__
, rc
, nframe
);
1122 ucx
->limit
= ubc
->isooutbuf
->nextread
;
1124 ifd
->actual_length
= 0;
1126 if (unlikely(nframe
== 0))
1127 return 0; /* no data to send */
1128 urb
->number_of_packets
= nframe
;
1130 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1133 /* device removed - give up silently */
1134 gig_dbg(DEBUG_ISO
, "%s: disconnected", __func__
);
1136 dev_err(ucx
->bcs
->cs
->dev
,
1137 "could not submit isoc write URB: %s\n",
1145 /* write_iso_tasklet
1146 * tasklet scheduled when an isochronous output URB from the Gigaset device
1149 * data B channel state structure
1151 static void write_iso_tasklet(unsigned long data
)
1153 struct bc_state
*bcs
= (struct bc_state
*) data
;
1154 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1155 struct cardstate
*cs
= bcs
->cs
;
1156 struct isow_urbctx_t
*done
, *next
, *ovfl
;
1159 struct usb_iso_packet_descriptor
*ifd
;
1160 unsigned long flags
;
1162 struct sk_buff
*skb
;
1166 /* loop while completed URBs arrive in time */
1168 if (unlikely(!(ubc
->running
))) {
1169 gig_dbg(DEBUG_ISO
, "%s: not running", __func__
);
1173 /* retrieve completed URBs */
1174 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1175 done
= ubc
->isooutdone
;
1176 ubc
->isooutdone
= NULL
;
1177 ovfl
= ubc
->isooutovfl
;
1178 ubc
->isooutovfl
= NULL
;
1179 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1181 dev_err(cs
->dev
, "isoc write underrun\n");
1188 /* submit free URB if available */
1189 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1190 next
= ubc
->isooutfree
;
1191 ubc
->isooutfree
= NULL
;
1192 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1194 rc
= submit_iso_write_urb(next
);
1195 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1196 /* could not submit URB, put it back */
1197 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1198 if (ubc
->isooutfree
== NULL
) {
1199 ubc
->isooutfree
= next
;
1202 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1204 /* couldn't put it back */
1206 "losing isoc write URB\n");
1212 /* process completed URB */
1214 status
= done
->status
;
1216 case -EXDEV
: /* partial completion */
1217 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1219 /* fall through - what's the difference anyway? */
1220 case 0: /* normal completion */
1221 /* inspect individual frames
1222 * assumptions (for lack of documentation):
1223 * - actual_length bytes of first frame in error are
1225 * - all following frames are not sent at all
1227 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
1228 ifd
= &urb
->iso_frame_desc
[i
];
1230 ifd
->actual_length
!= ifd
->length
) {
1232 "isoc write: frame %d[%d/%d]: %s\n",
1233 i
, ifd
->actual_length
,
1235 get_usb_statmsg(ifd
->status
));
1240 case -EPIPE
: /* stall - probably underrun */
1241 dev_err(cs
->dev
, "isoc write: stalled\n");
1244 default: /* other errors */
1245 dev_warn(cs
->dev
, "isoc write: %s\n",
1246 get_usb_statmsg(status
));
1249 /* mark the write buffer area covered by this URB as free */
1250 if (done
->limit
>= 0)
1251 ubc
->isooutbuf
->read
= done
->limit
;
1253 /* mark URB as free */
1254 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1255 next
= ubc
->isooutfree
;
1256 ubc
->isooutfree
= done
;
1257 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1259 /* only one URB still active - resubmit one */
1260 rc
= submit_iso_write_urb(next
);
1261 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1262 /* couldn't submit */
1268 /* process queued SKBs */
1269 while ((skb
= skb_dequeue(&bcs
->squeue
))) {
1270 /* copy to output buffer, doing L2 encapsulation */
1272 if (gigaset_isoc_buildframe(bcs
, skb
->data
, len
) == -EAGAIN
) {
1273 /* insufficient buffer space, push back onto queue */
1274 skb_queue_head(&bcs
->squeue
, skb
);
1275 gig_dbg(DEBUG_ISO
, "%s: skb requeued, qlen=%d",
1276 __func__
, skb_queue_len(&bcs
->squeue
));
1280 gigaset_skb_sent(bcs
, skb
);
1281 dev_kfree_skb_any(skb
);
1285 /* Isochronous Read - Bottom Half */
1286 /* ============================== */
1289 * tasklet scheduled when an isochronous input URB from the Gigaset device
1292 * data B channel state structure
1294 static void read_iso_tasklet(unsigned long data
)
1296 struct bc_state
*bcs
= (struct bc_state
*) data
;
1297 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1298 struct cardstate
*cs
= bcs
->cs
;
1301 struct usb_iso_packet_descriptor
*ifd
;
1303 unsigned long flags
;
1304 int totleft
, numbytes
, offset
, frame
, rc
;
1306 /* loop while more completed URBs arrive in the meantime */
1309 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
1310 urb
= ubc
->isoindone
;
1312 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1315 status
= ubc
->isoinstatus
;
1316 ubc
->isoindone
= NULL
;
1317 if (unlikely(ubc
->loststatus
!= -EINPROGRESS
)) {
1319 "isoc read overrun, URB dropped (status: %s, %d bytes)\n",
1320 get_usb_statmsg(ubc
->loststatus
),
1322 ubc
->loststatus
= -EINPROGRESS
;
1324 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1326 if (unlikely(!(ubc
->running
))) {
1328 "%s: channel not running, "
1329 "dropped URB with status: %s",
1330 __func__
, get_usb_statmsg(status
));
1335 case 0: /* normal completion */
1337 case -EXDEV
: /* inspect individual frames
1338 (we do that anyway) */
1339 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1345 gig_dbg(DEBUG_ISO
, "%s: %s",
1346 __func__
, get_usb_statmsg(status
));
1347 continue; /* -> skip */
1349 dev_err(cs
->dev
, "isoc read: stalled\n");
1351 continue; /* -> skip */
1352 default: /* other error */
1353 dev_warn(cs
->dev
, "isoc read: %s\n",
1354 get_usb_statmsg(status
));
1358 rcvbuf
= urb
->transfer_buffer
;
1359 totleft
= urb
->actual_length
;
1360 for (frame
= 0; totleft
> 0 && frame
< BAS_NUMFRAMES
; frame
++) {
1361 ifd
= &urb
->iso_frame_desc
[frame
];
1362 numbytes
= ifd
->actual_length
;
1363 switch (ifd
->status
) {
1364 case 0: /* success */
1366 case -EPROTO
: /* protocol error or unplug */
1369 /* probably just disconnected, ignore */
1371 "isoc read: frame %d[%d]: %s\n",
1373 get_usb_statmsg(ifd
->status
));
1375 default: /* other error */
1376 /* report, assume transferred bytes are ok */
1378 "isoc read: frame %d[%d]: %s\n",
1380 get_usb_statmsg(ifd
->status
));
1382 if (unlikely(numbytes
> BAS_MAXFRAME
))
1384 "isoc read: frame %d[%d]: %s\n",
1386 "exceeds max frame size");
1387 if (unlikely(numbytes
> totleft
)) {
1389 "isoc read: frame %d[%d]: %s\n",
1391 "exceeds total transfer length");
1394 offset
= ifd
->offset
;
1395 if (unlikely(offset
+ numbytes
> BAS_INBUFSIZE
)) {
1397 "isoc read: frame %d[%d]: %s\n",
1399 "exceeds end of buffer");
1400 numbytes
= BAS_INBUFSIZE
- offset
;
1402 gigaset_isoc_receive(rcvbuf
+ offset
, numbytes
, bcs
);
1403 totleft
-= numbytes
;
1405 if (unlikely(totleft
> 0))
1406 dev_warn(cs
->dev
, "isoc read: %d data bytes missing\n",
1410 /* URB processed, resubmit */
1411 for (frame
= 0; frame
< BAS_NUMFRAMES
; frame
++) {
1412 urb
->iso_frame_desc
[frame
].status
= 0;
1413 urb
->iso_frame_desc
[frame
].actual_length
= 0;
1415 /* urb->dev is clobbered by USB subsystem */
1416 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1417 urb
->transfer_flags
= URB_ISO_ASAP
;
1418 urb
->number_of_packets
= BAS_NUMFRAMES
;
1419 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1420 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
1422 "could not resubmit isoc read URB: %s\n",
1424 dump_urb(DEBUG_ISO
, "resubmit isoc read", urb
);
1430 /* Channel Operations */
1431 /* ================== */
1434 * timeout routine for control output request
1436 * controller state structure
1438 static void req_timeout(unsigned long data
)
1440 struct cardstate
*cs
= (struct cardstate
*) data
;
1441 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1443 unsigned long flags
;
1447 spin_lock_irqsave(&ucs
->lock
, flags
);
1448 pending
= ucs
->pending
;
1450 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1453 case 0: /* no pending request */
1454 gig_dbg(DEBUG_USBREQ
, "%s: no request pending", __func__
);
1457 case HD_OPEN_ATCHANNEL
:
1458 dev_err(cs
->dev
, "timeout opening AT channel\n");
1462 case HD_OPEN_B1CHANNEL
:
1463 dev_err(cs
->dev
, "timeout opening channel 1\n");
1464 error_hangup(&cs
->bcs
[0]);
1467 case HD_OPEN_B2CHANNEL
:
1468 dev_err(cs
->dev
, "timeout opening channel 2\n");
1469 error_hangup(&cs
->bcs
[1]);
1472 case HD_CLOSE_ATCHANNEL
:
1473 dev_err(cs
->dev
, "timeout closing AT channel\n");
1477 case HD_CLOSE_B1CHANNEL
:
1478 dev_err(cs
->dev
, "timeout closing channel 1\n");
1482 case HD_CLOSE_B2CHANNEL
:
1483 dev_err(cs
->dev
, "timeout closing channel 2\n");
1487 case HD_RESET_INTERRUPT_PIPE
:
1488 /* error recovery escalation */
1490 "reset interrupt pipe timeout, attempting USB reset\n");
1491 usb_queue_reset_device(ucs
->interface
);
1495 dev_warn(cs
->dev
, "request 0x%02x timed out, clearing\n",
1499 wake_up(&ucs
->waitqueue
);
1502 /* write_ctrl_callback
1503 * USB completion handler for control pipe output
1504 * called by the USB subsystem in interrupt context
1506 * urb USB request block of completed request
1507 * urb->context = hardware specific controller state structure
1509 static void write_ctrl_callback(struct urb
*urb
)
1511 struct bas_cardstate
*ucs
= urb
->context
;
1512 int status
= urb
->status
;
1514 unsigned long flags
;
1518 case 0: /* normal completion */
1519 spin_lock_irqsave(&ucs
->lock
, flags
);
1520 switch (ucs
->pending
) {
1521 case HD_DEVICE_INIT_ACK
: /* no reply expected */
1522 del_timer(&ucs
->timer_ctrl
);
1526 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1529 case -ENOENT
: /* cancelled */
1530 case -ECONNRESET
: /* cancelled (async) */
1531 case -EINPROGRESS
: /* pending */
1532 case -ENODEV
: /* device removed */
1533 case -ESHUTDOWN
: /* device shut down */
1534 /* ignore silently */
1535 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1536 __func__
, get_usb_statmsg(status
));
1539 default: /* any failure */
1540 /* don't retry if suspend requested */
1541 if (++ucs
->retry_ctrl
> BAS_RETRY
||
1542 (ucs
->basstate
& BS_SUSPEND
)) {
1543 dev_err(&ucs
->interface
->dev
,
1544 "control request 0x%02x failed: %s\n",
1545 ucs
->dr_ctrl
.bRequest
,
1546 get_usb_statmsg(status
));
1547 break; /* give up */
1549 dev_notice(&ucs
->interface
->dev
,
1550 "control request 0x%02x: %s, retry %d\n",
1551 ucs
->dr_ctrl
.bRequest
, get_usb_statmsg(status
),
1553 /* urb->dev is clobbered by USB subsystem */
1554 urb
->dev
= ucs
->udev
;
1555 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1557 dev_err(&ucs
->interface
->dev
,
1558 "could not resubmit request 0x%02x: %s\n",
1559 ucs
->dr_ctrl
.bRequest
, get_usb_rcmsg(rc
));
1566 /* failed, clear pending request */
1567 spin_lock_irqsave(&ucs
->lock
, flags
);
1568 del_timer(&ucs
->timer_ctrl
);
1570 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1571 wake_up(&ucs
->waitqueue
);
1575 * submit a control output request without message buffer to the Gigaset base
1576 * and optionally start a timeout
1578 * bcs B channel control structure
1579 * req control request code (HD_*)
1580 * val control request parameter value (set to 0 if unused)
1581 * timeout timeout in seconds (0: no timeout)
1584 * -EBUSY if another request is pending
1585 * any URB submission error code
1587 static int req_submit(struct bc_state
*bcs
, int req
, int val
, int timeout
)
1589 struct bas_cardstate
*ucs
= bcs
->cs
->hw
.bas
;
1591 unsigned long flags
;
1593 gig_dbg(DEBUG_USBREQ
, "-------> 0x%02x (%d)", req
, val
);
1595 spin_lock_irqsave(&ucs
->lock
, flags
);
1597 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1598 dev_err(bcs
->cs
->dev
,
1599 "submission of request 0x%02x failed: "
1600 "request 0x%02x still pending\n",
1605 ucs
->dr_ctrl
.bRequestType
= OUT_VENDOR_REQ
;
1606 ucs
->dr_ctrl
.bRequest
= req
;
1607 ucs
->dr_ctrl
.wValue
= cpu_to_le16(val
);
1608 ucs
->dr_ctrl
.wIndex
= 0;
1609 ucs
->dr_ctrl
.wLength
= 0;
1610 usb_fill_control_urb(ucs
->urb_ctrl
, ucs
->udev
,
1611 usb_sndctrlpipe(ucs
->udev
, 0),
1612 (unsigned char *) &ucs
->dr_ctrl
, NULL
, 0,
1613 write_ctrl_callback
, ucs
);
1614 ucs
->retry_ctrl
= 0;
1615 ret
= usb_submit_urb(ucs
->urb_ctrl
, GFP_ATOMIC
);
1616 if (unlikely(ret
)) {
1617 dev_err(bcs
->cs
->dev
, "could not submit request 0x%02x: %s\n",
1618 req
, get_usb_rcmsg(ret
));
1619 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1625 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
1626 mod_timer(&ucs
->timer_ctrl
, jiffies
+ timeout
* HZ
/ 10);
1629 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1633 /* gigaset_init_bchannel
1634 * called by common.c to connect a B channel
1635 * initialize isochronous I/O and tell the Gigaset base to open the channel
1637 * B channel control structure
1639 * 0 on success, error code < 0 on error
1641 static int gigaset_init_bchannel(struct bc_state
*bcs
)
1643 struct cardstate
*cs
= bcs
->cs
;
1645 unsigned long flags
;
1647 spin_lock_irqsave(&cs
->lock
, flags
);
1648 if (unlikely(!cs
->connected
)) {
1649 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1650 spin_unlock_irqrestore(&cs
->lock
, flags
);
1654 if (cs
->hw
.bas
->basstate
& BS_SUSPEND
) {
1656 "not starting isoc I/O, suspend in progress\n");
1657 spin_unlock_irqrestore(&cs
->lock
, flags
);
1658 return -EHOSTUNREACH
;
1661 ret
= starturbs(bcs
);
1663 spin_unlock_irqrestore(&cs
->lock
, flags
);
1665 "could not start isoc I/O for channel B%d: %s\n",
1667 ret
== -EFAULT
? "null URB" : get_usb_rcmsg(ret
));
1673 req
= bcs
->channel
? HD_OPEN_B2CHANNEL
: HD_OPEN_B1CHANNEL
;
1674 ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
);
1676 dev_err(cs
->dev
, "could not open channel B%d\n",
1678 stopurbs(bcs
->hw
.bas
);
1681 spin_unlock_irqrestore(&cs
->lock
, flags
);
1682 if (ret
< 0 && ret
!= -ENODEV
)
1687 /* gigaset_close_bchannel
1688 * called by common.c to disconnect a B channel
1689 * tell the Gigaset base to close the channel
1690 * stopping isochronous I/O and LL notification will be done when the
1691 * acknowledgement for the close arrives
1693 * B channel control structure
1695 * 0 on success, error code < 0 on error
1697 static int gigaset_close_bchannel(struct bc_state
*bcs
)
1699 struct cardstate
*cs
= bcs
->cs
;
1701 unsigned long flags
;
1703 spin_lock_irqsave(&cs
->lock
, flags
);
1704 if (unlikely(!cs
->connected
)) {
1705 spin_unlock_irqrestore(&cs
->lock
, flags
);
1706 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1710 if (!(cs
->hw
.bas
->basstate
& (bcs
->channel
? BS_B2OPEN
: BS_B1OPEN
))) {
1711 /* channel not running: just signal common.c */
1712 spin_unlock_irqrestore(&cs
->lock
, flags
);
1713 gigaset_bchannel_down(bcs
);
1717 /* channel running: tell device to close it */
1718 req
= bcs
->channel
? HD_CLOSE_B2CHANNEL
: HD_CLOSE_B1CHANNEL
;
1719 ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
);
1721 dev_err(cs
->dev
, "closing channel B%d failed\n",
1724 spin_unlock_irqrestore(&cs
->lock
, flags
);
1728 /* Device Operations */
1729 /* ================= */
1732 * unqueue first command buffer from queue, waking any sleepers
1733 * must be called with cs->cmdlock held
1735 * cs controller state structure
1737 static void complete_cb(struct cardstate
*cs
)
1739 struct cmdbuf_t
*cb
= cs
->cmdbuf
;
1741 /* unqueue completed buffer */
1742 cs
->cmdbytes
-= cs
->curlen
;
1743 gig_dbg(DEBUG_OUTPUT
, "write_command: sent %u bytes, %u left",
1744 cs
->curlen
, cs
->cmdbytes
);
1745 if (cb
->next
!= NULL
) {
1746 cs
->cmdbuf
= cb
->next
;
1747 cs
->cmdbuf
->prev
= NULL
;
1748 cs
->curlen
= cs
->cmdbuf
->len
;
1751 cs
->lastcmdbuf
= NULL
;
1755 if (cb
->wake_tasklet
)
1756 tasklet_schedule(cb
->wake_tasklet
);
1761 /* write_command_callback
1762 * USB completion handler for AT command transmission
1763 * called by the USB subsystem in interrupt context
1765 * urb USB request block of completed request
1766 * urb->context = controller state structure
1768 static void write_command_callback(struct urb
*urb
)
1770 struct cardstate
*cs
= urb
->context
;
1771 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1772 int status
= urb
->status
;
1773 unsigned long flags
;
1775 update_basstate(ucs
, 0, BS_ATWRPEND
);
1776 wake_up(&ucs
->waitqueue
);
1780 case 0: /* normal completion */
1782 case -ENOENT
: /* cancelled */
1783 case -ECONNRESET
: /* cancelled (async) */
1784 case -EINPROGRESS
: /* pending */
1785 case -ENODEV
: /* device removed */
1786 case -ESHUTDOWN
: /* device shut down */
1787 /* ignore silently */
1788 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1789 __func__
, get_usb_statmsg(status
));
1791 default: /* any failure */
1792 if (++ucs
->retry_cmd_out
> BAS_RETRY
) {
1794 "command write: %s, "
1795 "giving up after %d retries\n",
1796 get_usb_statmsg(status
),
1797 ucs
->retry_cmd_out
);
1800 if (ucs
->basstate
& BS_SUSPEND
) {
1802 "command write: %s, "
1803 "won't retry - suspend requested\n",
1804 get_usb_statmsg(status
));
1807 if (cs
->cmdbuf
== NULL
) {
1809 "command write: %s, "
1810 "cannot retry - cmdbuf gone\n",
1811 get_usb_statmsg(status
));
1814 dev_notice(cs
->dev
, "command write: %s, retry %d\n",
1815 get_usb_statmsg(status
), ucs
->retry_cmd_out
);
1816 if (atwrite_submit(cs
, cs
->cmdbuf
->buf
, cs
->cmdbuf
->len
) >= 0)
1817 /* resubmitted - bypass regular exit block */
1819 /* command send failed, assume base still waiting */
1820 update_basstate(ucs
, BS_ATREADY
, 0);
1823 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1824 if (cs
->cmdbuf
!= NULL
)
1826 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1830 * timeout routine for AT command transmission
1832 * controller state structure
1834 static void atrdy_timeout(unsigned long data
)
1836 struct cardstate
*cs
= (struct cardstate
*) data
;
1837 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1839 dev_warn(cs
->dev
, "timeout waiting for HD_READY_SEND_ATDATA\n");
1841 /* fake the missing signal - what else can I do? */
1842 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
1847 * submit an HD_WRITE_ATMESSAGE command URB
1849 * cs controller state structure
1850 * buf buffer containing command to send
1851 * len length of command to send
1854 * -EBUSY if another request is pending
1855 * any URB submission error code
1857 static int atwrite_submit(struct cardstate
*cs
, unsigned char *buf
, int len
)
1859 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1862 gig_dbg(DEBUG_USBREQ
, "-------> HD_WRITE_ATMESSAGE (%d)", len
);
1864 if (update_basstate(ucs
, BS_ATWRPEND
, 0) & BS_ATWRPEND
) {
1866 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1870 ucs
->dr_cmd_out
.bRequestType
= OUT_VENDOR_REQ
;
1871 ucs
->dr_cmd_out
.bRequest
= HD_WRITE_ATMESSAGE
;
1872 ucs
->dr_cmd_out
.wValue
= 0;
1873 ucs
->dr_cmd_out
.wIndex
= 0;
1874 ucs
->dr_cmd_out
.wLength
= cpu_to_le16(len
);
1875 usb_fill_control_urb(ucs
->urb_cmd_out
, ucs
->udev
,
1876 usb_sndctrlpipe(ucs
->udev
, 0),
1877 (unsigned char *) &ucs
->dr_cmd_out
, buf
, len
,
1878 write_command_callback
, cs
);
1879 rc
= usb_submit_urb(ucs
->urb_cmd_out
, GFP_ATOMIC
);
1881 update_basstate(ucs
, 0, BS_ATWRPEND
);
1882 dev_err(cs
->dev
, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1887 /* submitted successfully, start timeout if necessary */
1888 if (!(update_basstate(ucs
, BS_ATTIMER
, BS_ATREADY
) & BS_ATTIMER
)) {
1889 gig_dbg(DEBUG_OUTPUT
, "setting ATREADY timeout of %d/10 secs",
1891 mod_timer(&ucs
->timer_atrdy
, jiffies
+ ATRDY_TIMEOUT
* HZ
/ 10);
1897 * start transmission of AT command queue if necessary
1899 * cs controller state structure
1902 * error code < 0 on error
1904 static int start_cbsend(struct cardstate
*cs
)
1906 struct cmdbuf_t
*cb
;
1907 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1908 unsigned long flags
;
1912 /* check if suspend requested */
1913 if (ucs
->basstate
& BS_SUSPEND
) {
1914 gig_dbg(DEBUG_OUTPUT
, "suspending");
1915 return -EHOSTUNREACH
;
1918 /* check if AT channel is open */
1919 if (!(ucs
->basstate
& BS_ATOPEN
)) {
1920 gig_dbg(DEBUG_OUTPUT
, "AT channel not open");
1921 rc
= req_submit(cs
->bcs
, HD_OPEN_ATCHANNEL
, 0, BAS_TIMEOUT
);
1923 /* flush command queue */
1924 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1925 while (cs
->cmdbuf
!= NULL
)
1927 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1932 /* try to send first command in queue */
1933 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1935 while ((cb
= cs
->cmdbuf
) != NULL
&& (ucs
->basstate
& BS_ATREADY
)) {
1936 ucs
->retry_cmd_out
= 0;
1937 rc
= atwrite_submit(cs
, cb
->buf
, cb
->len
);
1944 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1948 /* gigaset_write_cmd
1949 * This function is called by the device independent part of the driver
1950 * to transmit an AT command string to the Gigaset device.
1951 * It encapsulates the device specific method for transmission over the
1952 * direct USB connection to the base.
1953 * The command string is added to the queue of commands to send, and
1954 * USB transmission is started if necessary.
1956 * cs controller state structure
1957 * cb command buffer structure
1959 * number of bytes queued on success
1960 * error code < 0 on error
1962 static int gigaset_write_cmd(struct cardstate
*cs
, struct cmdbuf_t
*cb
)
1964 unsigned long flags
;
1967 gigaset_dbg_buffer(cs
->mstate
!= MS_LOCKED
?
1968 DEBUG_TRANSCMD
: DEBUG_LOCKCMD
,
1969 "CMD Transmit", cb
->len
, cb
->buf
);
1971 /* translate "+++" escape sequence sent as a single separate command
1972 * into "close AT channel" command for error recovery
1973 * The next command will reopen the AT channel automatically.
1975 if (cb
->len
== 3 && !memcmp(cb
->buf
, "+++", 3)) {
1976 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1977 * because of an error, the base never sends another one.
1978 * The response channel is thus effectively blocked.
1979 * Closing and reopening the AT channel does *not* clear
1981 * As a stopgap measure, submit a zero-length AT read
1982 * before closing the AT channel. This has the undocumented
1983 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1984 * from the base if necessary.
1985 * The subsequent AT channel close then discards any pending
1988 spin_lock_irqsave(&cs
->lock
, flags
);
1989 if (!(cs
->hw
.bas
->basstate
& BS_ATRDPEND
)) {
1990 kfree(cs
->hw
.bas
->rcvbuf
);
1991 cs
->hw
.bas
->rcvbuf
= NULL
;
1992 cs
->hw
.bas
->rcvbuf_size
= 0;
1993 cs
->hw
.bas
->retry_cmd_in
= 0;
1994 atread_submit(cs
, 0);
1996 spin_unlock_irqrestore(&cs
->lock
, flags
);
1998 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, BAS_TIMEOUT
);
1999 if (cb
->wake_tasklet
)
2000 tasklet_schedule(cb
->wake_tasklet
);
2007 spin_lock_irqsave(&cs
->cmdlock
, flags
);
2008 cb
->prev
= cs
->lastcmdbuf
;
2010 cs
->lastcmdbuf
->next
= cb
;
2013 cs
->curlen
= cb
->len
;
2015 cs
->cmdbytes
+= cb
->len
;
2016 cs
->lastcmdbuf
= cb
;
2017 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
2019 spin_lock_irqsave(&cs
->lock
, flags
);
2020 if (unlikely(!cs
->connected
)) {
2021 spin_unlock_irqrestore(&cs
->lock
, flags
);
2022 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
2023 /* flush command queue */
2024 spin_lock_irqsave(&cs
->cmdlock
, flags
);
2025 while (cs
->cmdbuf
!= NULL
)
2027 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
2030 rc
= start_cbsend(cs
);
2031 spin_unlock_irqrestore(&cs
->lock
, flags
);
2032 return rc
< 0 ? rc
: cb
->len
;
2035 /* gigaset_write_room
2036 * tty_driver.write_room interface routine
2037 * return number of characters the driver will accept to be written via
2040 * controller state structure
2042 * number of characters
2044 static int gigaset_write_room(struct cardstate
*cs
)
2049 /* gigaset_chars_in_buffer
2050 * tty_driver.chars_in_buffer interface routine
2051 * return number of characters waiting to be sent
2053 * controller state structure
2055 * number of characters
2057 static int gigaset_chars_in_buffer(struct cardstate
*cs
)
2059 return cs
->cmdbytes
;
2063 * implementation of ioctl(GIGASET_BRKCHARS)
2065 * controller state structure
2067 * -EINVAL (unimplemented function)
2069 static int gigaset_brkchars(struct cardstate
*cs
, const unsigned char buf
[6])
2075 /* Device Initialization/Shutdown */
2076 /* ============================== */
2078 /* Free hardware dependent part of the B channel structure
2080 * bcs B channel structure
2084 static int gigaset_freebcshw(struct bc_state
*bcs
)
2086 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2092 /* kill URBs and tasklets before freeing - better safe than sorry */
2094 gig_dbg(DEBUG_INIT
, "%s: killing isoc URBs", __func__
);
2095 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2096 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2097 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2099 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2100 usb_kill_urb(ubc
->isoinurbs
[i
]);
2101 usb_free_urb(ubc
->isoinurbs
[i
]);
2103 tasklet_kill(&ubc
->sent_tasklet
);
2104 tasklet_kill(&ubc
->rcvd_tasklet
);
2105 kfree(ubc
->isooutbuf
);
2111 /* Initialize hardware dependent part of the B channel structure
2113 * bcs B channel structure
2117 static int gigaset_initbcshw(struct bc_state
*bcs
)
2120 struct bas_bc_state
*ubc
;
2122 bcs
->hw
.bas
= ubc
= kmalloc(sizeof(struct bas_bc_state
), GFP_KERNEL
);
2124 pr_err("out of memory\n");
2129 atomic_set(&ubc
->corrbytes
, 0);
2130 spin_lock_init(&ubc
->isooutlock
);
2131 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2132 ubc
->isoouturbs
[i
].urb
= NULL
;
2133 ubc
->isoouturbs
[i
].bcs
= bcs
;
2135 ubc
->isooutdone
= ubc
->isooutfree
= ubc
->isooutovfl
= NULL
;
2137 ubc
->isooutbuf
= kmalloc(sizeof(struct isowbuf_t
), GFP_KERNEL
);
2138 if (!ubc
->isooutbuf
) {
2139 pr_err("out of memory\n");
2144 tasklet_init(&ubc
->sent_tasklet
,
2145 write_iso_tasklet
, (unsigned long) bcs
);
2147 spin_lock_init(&ubc
->isoinlock
);
2148 for (i
= 0; i
< BAS_INURBS
; ++i
)
2149 ubc
->isoinurbs
[i
] = NULL
;
2150 ubc
->isoindone
= NULL
;
2151 ubc
->loststatus
= -EINPROGRESS
;
2165 tasklet_init(&ubc
->rcvd_tasklet
,
2166 read_iso_tasklet
, (unsigned long) bcs
);
2170 static void gigaset_reinitbcshw(struct bc_state
*bcs
)
2172 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2174 bcs
->hw
.bas
->running
= 0;
2175 atomic_set(&bcs
->hw
.bas
->corrbytes
, 0);
2176 bcs
->hw
.bas
->numsub
= 0;
2177 spin_lock_init(&ubc
->isooutlock
);
2178 spin_lock_init(&ubc
->isoinlock
);
2179 ubc
->loststatus
= -EINPROGRESS
;
2182 static void gigaset_freecshw(struct cardstate
*cs
)
2184 /* timers, URBs and rcvbuf are disposed of in disconnect */
2185 kfree(cs
->hw
.bas
->int_in_buf
);
2190 static int gigaset_initcshw(struct cardstate
*cs
)
2192 struct bas_cardstate
*ucs
;
2194 cs
->hw
.bas
= ucs
= kmalloc(sizeof *ucs
, GFP_KERNEL
);
2196 pr_err("out of memory\n");
2199 ucs
->int_in_buf
= kmalloc(IP_MSGSIZE
, GFP_KERNEL
);
2200 if (!ucs
->int_in_buf
) {
2202 pr_err("out of memory\n");
2206 ucs
->urb_cmd_in
= NULL
;
2207 ucs
->urb_cmd_out
= NULL
;
2209 ucs
->rcvbuf_size
= 0;
2211 spin_lock_init(&ucs
->lock
);
2215 setup_timer(&ucs
->timer_ctrl
, req_timeout
, (unsigned long) cs
);
2216 setup_timer(&ucs
->timer_atrdy
, atrdy_timeout
, (unsigned long) cs
);
2217 setup_timer(&ucs
->timer_cmd_in
, cmd_in_timeout
, (unsigned long) cs
);
2218 setup_timer(&ucs
->timer_int_in
, int_in_resubmit
, (unsigned long) cs
);
2219 init_waitqueue_head(&ucs
->waitqueue
);
2220 INIT_WORK(&ucs
->int_in_wq
, int_in_work
);
2226 * unlink and deallocate all URBs unconditionally
2227 * caller must make sure that no commands are still in progress
2229 * cs controller state structure
2231 static void freeurbs(struct cardstate
*cs
)
2233 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2234 struct bas_bc_state
*ubc
;
2237 gig_dbg(DEBUG_INIT
, "%s: killing URBs", __func__
);
2238 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2239 ubc
= cs
->bcs
[j
].hw
.bas
;
2240 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2241 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2242 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2243 ubc
->isoouturbs
[i
].urb
= NULL
;
2245 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2246 usb_kill_urb(ubc
->isoinurbs
[i
]);
2247 usb_free_urb(ubc
->isoinurbs
[i
]);
2248 ubc
->isoinurbs
[i
] = NULL
;
2251 usb_kill_urb(ucs
->urb_int_in
);
2252 usb_free_urb(ucs
->urb_int_in
);
2253 ucs
->urb_int_in
= NULL
;
2254 usb_kill_urb(ucs
->urb_cmd_out
);
2255 usb_free_urb(ucs
->urb_cmd_out
);
2256 ucs
->urb_cmd_out
= NULL
;
2257 usb_kill_urb(ucs
->urb_cmd_in
);
2258 usb_free_urb(ucs
->urb_cmd_in
);
2259 ucs
->urb_cmd_in
= NULL
;
2260 usb_kill_urb(ucs
->urb_ctrl
);
2261 usb_free_urb(ucs
->urb_ctrl
);
2262 ucs
->urb_ctrl
= NULL
;
2266 * This function is called when a new USB device is connected.
2267 * It checks whether the new device is handled by this driver.
2269 static int gigaset_probe(struct usb_interface
*interface
,
2270 const struct usb_device_id
*id
)
2272 struct usb_host_interface
*hostif
;
2273 struct usb_device
*udev
= interface_to_usbdev(interface
);
2274 struct cardstate
*cs
= NULL
;
2275 struct bas_cardstate
*ucs
= NULL
;
2276 struct bas_bc_state
*ubc
;
2277 struct usb_endpoint_descriptor
*endpoint
;
2282 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2283 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2284 le16_to_cpu(udev
->descriptor
.idProduct
));
2286 /* set required alternate setting */
2287 hostif
= interface
->cur_altsetting
;
2288 if (hostif
->desc
.bAlternateSetting
!= 3) {
2290 "%s: wrong alternate setting %d - trying to switch",
2291 __func__
, hostif
->desc
.bAlternateSetting
);
2292 if (usb_set_interface(udev
, hostif
->desc
.bInterfaceNumber
, 3)
2294 dev_warn(&udev
->dev
, "usb_set_interface failed, "
2295 "device %d interface %d altsetting %d\n",
2296 udev
->devnum
, hostif
->desc
.bInterfaceNumber
,
2297 hostif
->desc
.bAlternateSetting
);
2300 hostif
= interface
->cur_altsetting
;
2303 /* Reject application specific interfaces
2305 if (hostif
->desc
.bInterfaceClass
!= 255) {
2306 dev_warn(&udev
->dev
, "%s: bInterfaceClass == %d\n",
2307 __func__
, hostif
->desc
.bInterfaceClass
);
2311 dev_info(&udev
->dev
,
2312 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2313 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2314 le16_to_cpu(udev
->descriptor
.idProduct
));
2316 /* allocate memory for our device state and initialize it */
2317 cs
= gigaset_initcs(driver
, BAS_CHANNELS
, 0, 0, cidmode
,
2318 GIGASET_MODULENAME
);
2323 /* save off device structure ptrs for later use */
2326 ucs
->interface
= interface
;
2327 cs
->dev
= &interface
->dev
;
2330 * - one for the interrupt pipe
2331 * - three for the different uses of the default control pipe
2332 * - three for each isochronous pipe
2334 if (!(ucs
->urb_int_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2335 !(ucs
->urb_cmd_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2336 !(ucs
->urb_cmd_out
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2337 !(ucs
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
)))
2340 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2341 ubc
= cs
->bcs
[j
].hw
.bas
;
2342 for (i
= 0; i
< BAS_OUTURBS
; ++i
)
2343 if (!(ubc
->isoouturbs
[i
].urb
=
2344 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2346 for (i
= 0; i
< BAS_INURBS
; ++i
)
2347 if (!(ubc
->isoinurbs
[i
] =
2348 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2353 ucs
->rcvbuf_size
= 0;
2355 /* Fill the interrupt urb and send it to the core */
2356 endpoint
= &hostif
->endpoint
[0].desc
;
2357 usb_fill_int_urb(ucs
->urb_int_in
, udev
,
2358 usb_rcvintpipe(udev
,
2359 (endpoint
->bEndpointAddress
) & 0x0f),
2360 ucs
->int_in_buf
, IP_MSGSIZE
, read_int_callback
, cs
,
2361 endpoint
->bInterval
);
2362 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2364 dev_err(cs
->dev
, "could not submit interrupt URB: %s\n",
2368 ucs
->retry_int_in
= 0;
2370 /* tell the device that the driver is ready */
2371 rc
= req_submit(cs
->bcs
, HD_DEVICE_INIT_ACK
, 0, 0);
2375 /* tell common part that the device is ready */
2376 if (startmode
== SM_LOCKED
)
2377 cs
->mstate
= MS_LOCKED
;
2379 /* save address of controller structure */
2380 usb_set_intfdata(interface
, cs
);
2382 if (!gigaset_start(cs
))
2388 dev_err(cs
->dev
, "could not allocate URBs\n");
2391 usb_set_intfdata(interface
, NULL
);
2396 /* gigaset_disconnect
2397 * This function is called when the Gigaset base is unplugged.
2399 static void gigaset_disconnect(struct usb_interface
*interface
)
2401 struct cardstate
*cs
;
2402 struct bas_cardstate
*ucs
;
2405 cs
= usb_get_intfdata(interface
);
2409 dev_info(cs
->dev
, "disconnecting Gigaset base\n");
2411 /* mark base as not ready, all channels disconnected */
2414 /* tell LL all channels are down */
2415 for (j
= 0; j
< BAS_CHANNELS
; ++j
)
2416 gigaset_bchannel_down(cs
->bcs
+ j
);
2418 /* stop driver (common part) */
2421 /* stop delayed work and URBs, free ressources */
2422 del_timer_sync(&ucs
->timer_ctrl
);
2423 del_timer_sync(&ucs
->timer_atrdy
);
2424 del_timer_sync(&ucs
->timer_cmd_in
);
2425 del_timer_sync(&ucs
->timer_int_in
);
2426 cancel_work_sync(&ucs
->int_in_wq
);
2428 usb_set_intfdata(interface
, NULL
);
2431 ucs
->rcvbuf_size
= 0;
2432 usb_put_dev(ucs
->udev
);
2433 ucs
->interface
= NULL
;
2440 * This function is called before the USB connection is suspended.
2442 static int gigaset_suspend(struct usb_interface
*intf
, pm_message_t message
)
2444 struct cardstate
*cs
= usb_get_intfdata(intf
);
2445 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2448 /* set suspend flag; this stops AT command/response traffic */
2449 if (update_basstate(ucs
, BS_SUSPEND
, 0) & BS_SUSPEND
) {
2450 gig_dbg(DEBUG_SUSPEND
, "already suspended");
2454 /* wait a bit for blocking conditions to go away */
2455 rc
= wait_event_timeout(ucs
->waitqueue
,
2457 (BS_B1OPEN
|BS_B2OPEN
|BS_ATRDPEND
|BS_ATWRPEND
)),
2459 gig_dbg(DEBUG_SUSPEND
, "wait_event_timeout() -> %d", rc
);
2461 /* check for conditions preventing suspend */
2462 if (ucs
->basstate
& (BS_B1OPEN
|BS_B2OPEN
|BS_ATRDPEND
|BS_ATWRPEND
)) {
2463 dev_warn(cs
->dev
, "cannot suspend:\n");
2464 if (ucs
->basstate
& BS_B1OPEN
)
2465 dev_warn(cs
->dev
, " B channel 1 open\n");
2466 if (ucs
->basstate
& BS_B2OPEN
)
2467 dev_warn(cs
->dev
, " B channel 2 open\n");
2468 if (ucs
->basstate
& BS_ATRDPEND
)
2469 dev_warn(cs
->dev
, " receiving AT reply\n");
2470 if (ucs
->basstate
& BS_ATWRPEND
)
2471 dev_warn(cs
->dev
, " sending AT command\n");
2472 update_basstate(ucs
, 0, BS_SUSPEND
);
2476 /* close AT channel if open */
2477 if (ucs
->basstate
& BS_ATOPEN
) {
2478 gig_dbg(DEBUG_SUSPEND
, "closing AT channel");
2479 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, 0);
2481 update_basstate(ucs
, 0, BS_SUSPEND
);
2484 wait_event_timeout(ucs
->waitqueue
, !ucs
->pending
,
2486 /* in case of timeout, proceed anyway */
2489 /* kill all URBs and delayed work that might still be pending */
2490 usb_kill_urb(ucs
->urb_ctrl
);
2491 usb_kill_urb(ucs
->urb_int_in
);
2492 del_timer_sync(&ucs
->timer_ctrl
);
2493 del_timer_sync(&ucs
->timer_atrdy
);
2494 del_timer_sync(&ucs
->timer_cmd_in
);
2495 del_timer_sync(&ucs
->timer_int_in
);
2496 cancel_work_sync(&ucs
->int_in_wq
);
2498 gig_dbg(DEBUG_SUSPEND
, "suspend complete");
2503 * This function is called after the USB connection has been resumed.
2505 static int gigaset_resume(struct usb_interface
*intf
)
2507 struct cardstate
*cs
= usb_get_intfdata(intf
);
2508 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2511 /* resubmit interrupt URB for spontaneous messages from base */
2512 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2514 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
2518 ucs
->retry_int_in
= 0;
2520 /* clear suspend flag to reallow activity */
2521 update_basstate(ucs
, 0, BS_SUSPEND
);
2523 gig_dbg(DEBUG_SUSPEND
, "resume complete");
2527 /* gigaset_pre_reset
2528 * This function is called before the USB connection is reset.
2530 static int gigaset_pre_reset(struct usb_interface
*intf
)
2532 /* handle just like suspend */
2533 return gigaset_suspend(intf
, PMSG_ON
);
2536 /* gigaset_post_reset
2537 * This function is called after the USB connection has been reset.
2539 static int gigaset_post_reset(struct usb_interface
*intf
)
2541 /* FIXME: send HD_DEVICE_INIT_ACK? */
2543 /* resume operations */
2544 return gigaset_resume(intf
);
2548 static const struct gigaset_ops gigops
= {
2551 gigaset_chars_in_buffer
,
2553 gigaset_init_bchannel
,
2554 gigaset_close_bchannel
,
2557 gigaset_reinitbcshw
,
2560 gigaset_set_modem_ctrl
,
2562 gigaset_set_line_ctrl
,
2563 gigaset_isoc_send_skb
,
2568 * This function is called after the kernel module is loaded.
2570 static int __init
bas_gigaset_init(void)
2574 /* allocate memory for our driver state and initialize it */
2575 driver
= gigaset_initdriver(GIGASET_MINOR
, GIGASET_MINORS
,
2576 GIGASET_MODULENAME
, GIGASET_DEVNAME
,
2577 &gigops
, THIS_MODULE
);
2581 /* register this driver with the USB subsystem */
2582 result
= usb_register(&gigaset_usb_driver
);
2584 pr_err("error %d registering USB driver\n", -result
);
2588 pr_info(DRIVER_DESC
"\n");
2593 gigaset_freedriver(driver
);
2599 * This function is called before the kernel module is unloaded.
2601 static void __exit
bas_gigaset_exit(void)
2603 struct bas_cardstate
*ucs
;
2606 gigaset_blockdriver(driver
); /* => probe will fail
2607 * => no gigaset_start any more
2610 /* stop all connected devices */
2611 for (i
= 0; i
< driver
->minors
; i
++) {
2612 if (gigaset_shutdown(driver
->cs
+ i
) < 0)
2613 continue; /* no device */
2614 /* from now on, no isdn callback should be possible */
2616 /* close all still open channels */
2617 ucs
= driver
->cs
[i
].hw
.bas
;
2618 if (ucs
->basstate
& BS_B1OPEN
) {
2619 gig_dbg(DEBUG_INIT
, "closing B1 channel");
2620 usb_control_msg(ucs
->udev
,
2621 usb_sndctrlpipe(ucs
->udev
, 0),
2622 HD_CLOSE_B1CHANNEL
, OUT_VENDOR_REQ
,
2623 0, 0, NULL
, 0, BAS_TIMEOUT
);
2625 if (ucs
->basstate
& BS_B2OPEN
) {
2626 gig_dbg(DEBUG_INIT
, "closing B2 channel");
2627 usb_control_msg(ucs
->udev
,
2628 usb_sndctrlpipe(ucs
->udev
, 0),
2629 HD_CLOSE_B2CHANNEL
, OUT_VENDOR_REQ
,
2630 0, 0, NULL
, 0, BAS_TIMEOUT
);
2632 if (ucs
->basstate
& BS_ATOPEN
) {
2633 gig_dbg(DEBUG_INIT
, "closing AT channel");
2634 usb_control_msg(ucs
->udev
,
2635 usb_sndctrlpipe(ucs
->udev
, 0),
2636 HD_CLOSE_ATCHANNEL
, OUT_VENDOR_REQ
,
2637 0, 0, NULL
, 0, BAS_TIMEOUT
);
2642 /* deregister this driver with the USB subsystem */
2643 usb_deregister(&gigaset_usb_driver
);
2644 /* this will call the disconnect-callback */
2645 /* from now on, no disconnect/probe callback should be running */
2647 gigaset_freedriver(driver
);
2652 module_init(bas_gigaset_init
);
2653 module_exit(bas_gigaset_exit
);
2655 MODULE_AUTHOR(DRIVER_AUTHOR
);
2656 MODULE_DESCRIPTION(DRIVER_DESC
);
2657 MODULE_LICENSE("GPL");