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 * =====================================================================
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
22 #include <linux/usb.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
26 /* Version Information */
27 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
28 #define DRIVER_DESC "USB Driver for Gigaset 307x"
31 /* Module parameters */
33 static int startmode
= SM_ISDN
;
34 static int cidmode
= 1;
36 module_param(startmode
, int, S_IRUGO
);
37 module_param(cidmode
, int, S_IRUGO
);
38 MODULE_PARM_DESC(startmode
, "start in isdn4linux mode");
39 MODULE_PARM_DESC(cidmode
, "Call-ID mode");
41 #define GIGASET_MINORS 1
42 #define GIGASET_MINOR 16
43 #define GIGASET_MODULENAME "bas_gigaset"
44 #define GIGASET_DEVNAME "ttyGB"
46 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47 #define IF_WRITEBUF 264
49 /* Values for the Gigaset 307x */
50 #define USB_GIGA_VENDOR_ID 0x0681
51 #define USB_3070_PRODUCT_ID 0x0001
52 #define USB_3075_PRODUCT_ID 0x0002
53 #define USB_SX303_PRODUCT_ID 0x0021
54 #define USB_SX353_PRODUCT_ID 0x0022
56 /* table of devices that work with this driver */
57 static const struct usb_device_id gigaset_table
[] = {
58 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_3070_PRODUCT_ID
) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_3075_PRODUCT_ID
) },
60 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_SX303_PRODUCT_ID
) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID
, USB_SX353_PRODUCT_ID
) },
62 { } /* Terminating entry */
65 MODULE_DEVICE_TABLE(usb
, gigaset_table
);
67 /*======================= local function prototypes ==========================*/
69 /* function called if a new device belonging to this driver is connected */
70 static int gigaset_probe(struct usb_interface
*interface
,
71 const struct usb_device_id
*id
);
73 /* Function will be called if the device is unplugged */
74 static void gigaset_disconnect(struct usb_interface
*interface
);
76 /* functions called before/after suspend */
77 static int gigaset_suspend(struct usb_interface
*intf
, pm_message_t message
);
78 static int gigaset_resume(struct usb_interface
*intf
);
80 /* functions called before/after device reset */
81 static int gigaset_pre_reset(struct usb_interface
*intf
);
82 static int gigaset_post_reset(struct usb_interface
*intf
);
84 static int atread_submit(struct cardstate
*, int);
85 static void stopurbs(struct bas_bc_state
*);
86 static int req_submit(struct bc_state
*, int, int, int);
87 static int atwrite_submit(struct cardstate
*, unsigned char *, int);
88 static int start_cbsend(struct cardstate
*);
90 /*============================================================================*/
92 struct bas_cardstate
{
93 struct usb_device
*udev
; /* USB device pointer */
94 struct usb_interface
*interface
; /* interface for this device */
95 unsigned char minor
; /* starting minor number */
97 struct urb
*urb_ctrl
; /* control pipe default URB */
98 struct usb_ctrlrequest dr_ctrl
;
99 struct timer_list timer_ctrl
; /* control request timeout */
102 struct timer_list timer_atrdy
; /* AT command ready timeout */
103 struct urb
*urb_cmd_out
; /* for sending AT commands */
104 struct usb_ctrlrequest dr_cmd_out
;
107 struct urb
*urb_cmd_in
; /* for receiving AT replies */
108 struct usb_ctrlrequest dr_cmd_in
;
109 struct timer_list timer_cmd_in
; /* receive request timeout */
110 unsigned char *rcvbuf
; /* AT reply receive buffer */
112 struct urb
*urb_int_in
; /* URB for interrupt pipe */
113 unsigned char int_in_buf
[3];
115 spinlock_t lock
; /* locks all following */
116 int basstate
; /* bitmap (BS_*) */
117 int pending
; /* uncompleted base request */
118 wait_queue_head_t waitqueue
;
119 int rcvbuf_size
; /* size of AT receive buffer */
120 /* 0: no receive in progress */
121 int retry_cmd_in
; /* receive req retry count */
124 /* status of direct USB connection to 307x base (bits in basstate) */
125 #define BS_ATOPEN 0x001 /* AT channel open */
126 #define BS_B1OPEN 0x002 /* B channel 1 open */
127 #define BS_B2OPEN 0x004 /* B channel 2 open */
128 #define BS_ATREADY 0x008 /* base ready for AT command */
129 #define BS_INIT 0x010 /* base has signalled INIT_OK */
130 #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
131 #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
132 #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
133 #define BS_SUSPEND 0x100 /* USB port suspended */
136 static struct gigaset_driver
*driver
= NULL
;
138 /* usb specific object needed to register this driver with the usb subsystem */
139 static struct usb_driver gigaset_usb_driver
= {
140 .name
= GIGASET_MODULENAME
,
141 .probe
= gigaset_probe
,
142 .disconnect
= gigaset_disconnect
,
143 .id_table
= gigaset_table
,
144 .suspend
= gigaset_suspend
,
145 .resume
= gigaset_resume
,
146 .reset_resume
= gigaset_post_reset
,
147 .pre_reset
= gigaset_pre_reset
,
148 .post_reset
= gigaset_post_reset
,
151 /* get message text for usb_submit_urb return code
153 static char *get_usb_rcmsg(int rc
)
155 static char unkmsg
[28];
161 return "out of memory";
163 return "device not present";
165 return "endpoint not present";
167 return "URB type not supported";
169 return "invalid argument";
171 return "start frame too early or too much scheduled";
173 return "too many isochronous frames requested";
175 return "endpoint stalled";
177 return "invalid packet size";
179 return "would overcommit USB bandwidth";
181 return "device shut down";
183 return "reject flag set";
185 return "device suspended";
187 snprintf(unkmsg
, sizeof(unkmsg
), "unknown error %d", rc
);
192 /* get message text for USB status code
194 static char *get_usb_statmsg(int status
)
196 static char unkmsg
[28];
202 return "unlinked (sync)";
206 return "bit stuffing error, timeout, or unknown USB error";
208 return "CRC mismatch, timeout, or unknown USB error";
212 return "endpoint stalled";
214 return "IN buffer overrun";
216 return "OUT buffer underrun";
218 return "too much data";
220 return "short packet detected";
222 return "device removed";
224 return "partial isochronous transfer";
226 return "invalid argument";
228 return "unlinked (async)";
230 return "device shut down";
232 snprintf(unkmsg
, sizeof(unkmsg
), "unknown status %d", status
);
238 * retrieve string representation of USB pipe type
240 static inline char *usb_pipetype_str(int pipe
)
242 if (usb_pipeisoc(pipe
))
244 if (usb_pipeint(pipe
))
246 if (usb_pipecontrol(pipe
))
248 if (usb_pipebulk(pipe
))
254 * write content of URB to syslog for debugging
256 static inline void dump_urb(enum debuglevel level
, const char *tag
,
259 #ifdef CONFIG_GIGASET_DEBUG
261 gig_dbg(level
, "%s urb(0x%08lx)->{", tag
, (unsigned long) urb
);
264 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
265 "hcpriv=0x%08lx, transfer_flags=0x%x,",
266 (unsigned long) urb
->dev
,
267 usb_pipetype_str(urb
->pipe
),
268 usb_pipeendpoint(urb
->pipe
), usb_pipedevice(urb
->pipe
),
269 usb_pipein(urb
->pipe
) ? "in" : "out",
270 (unsigned long) urb
->hcpriv
,
271 urb
->transfer_flags
);
273 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
274 "setup_packet=0x%08lx,",
275 (unsigned long) urb
->transfer_buffer
,
276 urb
->transfer_buffer_length
, urb
->actual_length
,
277 (unsigned long) urb
->setup_packet
);
279 " start_frame=%d, number_of_packets=%d, interval=%d, "
281 urb
->start_frame
, urb
->number_of_packets
, urb
->interval
,
284 " context=0x%08lx, complete=0x%08lx, "
285 "iso_frame_desc[]={",
286 (unsigned long) urb
->context
,
287 (unsigned long) urb
->complete
);
288 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
289 struct usb_iso_packet_descriptor
*pifd
290 = &urb
->iso_frame_desc
[i
];
292 " {offset=%u, length=%u, actual_length=%u, "
294 pifd
->offset
, pifd
->length
, pifd
->actual_length
,
298 gig_dbg(level
, "}}");
302 /* read/set modem control bits etc. (m10x only) */
303 static int gigaset_set_modem_ctrl(struct cardstate
*cs
, unsigned old_state
,
309 static int gigaset_baud_rate(struct cardstate
*cs
, unsigned cflag
)
314 static int gigaset_set_line_ctrl(struct cardstate
*cs
, unsigned cflag
)
320 * hang up any existing connection because of an unrecoverable error
321 * This function may be called from any context and takes care of scheduling
322 * the necessary actions for execution outside of interrupt context.
323 * cs->lock must not be held.
325 * B channel control structure
327 static inline void error_hangup(struct bc_state
*bcs
)
329 struct cardstate
*cs
= bcs
->cs
;
331 gig_dbg(DEBUG_ANY
, "%s: scheduling HUP for channel %d",
332 __func__
, bcs
->channel
);
334 if (!gigaset_add_event(cs
, &bcs
->at_state
, EV_HUP
, NULL
, 0, NULL
))
335 dev_err(cs
->dev
, "event queue full\n");
337 gigaset_schedule_event(cs
);
341 * reset Gigaset device because of an unrecoverable error
342 * This function may be called from any context, and takes care of
343 * scheduling the necessary actions for execution outside of interrupt context.
344 * cs->lock must not be held.
346 * controller state structure
348 static inline void error_reset(struct cardstate
*cs
)
350 /* close AT command channel to recover (ignore errors) */
351 req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, BAS_TIMEOUT
);
353 //FIXME try to recover without bothering the user
355 "unrecoverable error - please disconnect Gigaset base to reset\n");
359 * check for completion of pending control request
361 * ucs hardware specific controller state structure
363 static void check_pending(struct bas_cardstate
*ucs
)
367 spin_lock_irqsave(&ucs
->lock
, flags
);
368 switch (ucs
->pending
) {
371 case HD_OPEN_ATCHANNEL
:
372 if (ucs
->basstate
& BS_ATOPEN
)
375 case HD_OPEN_B1CHANNEL
:
376 if (ucs
->basstate
& BS_B1OPEN
)
379 case HD_OPEN_B2CHANNEL
:
380 if (ucs
->basstate
& BS_B2OPEN
)
383 case HD_CLOSE_ATCHANNEL
:
384 if (!(ucs
->basstate
& BS_ATOPEN
))
387 case HD_CLOSE_B1CHANNEL
:
388 if (!(ucs
->basstate
& BS_B1OPEN
))
391 case HD_CLOSE_B2CHANNEL
:
392 if (!(ucs
->basstate
& BS_B2OPEN
))
395 case HD_DEVICE_INIT_ACK
: /* no reply expected */
398 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
399 * are handled separately and should never end up here
402 dev_warn(&ucs
->interface
->dev
,
403 "unknown pending request 0x%02x cleared\n",
409 del_timer(&ucs
->timer_ctrl
);
411 spin_unlock_irqrestore(&ucs
->lock
, flags
);
415 * timeout routine for command input request
417 * controller state structure
419 static void cmd_in_timeout(unsigned long data
)
421 struct cardstate
*cs
= (struct cardstate
*) data
;
422 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
425 if (!ucs
->rcvbuf_size
) {
426 gig_dbg(DEBUG_USBREQ
, "%s: no receive in progress", __func__
);
430 if (ucs
->retry_cmd_in
++ < BAS_RETRY
) {
431 dev_notice(cs
->dev
, "control read: timeout, retry %d\n",
433 rc
= atread_submit(cs
, BAS_TIMEOUT
);
434 if (rc
>= 0 || rc
== -ENODEV
)
435 /* resubmitted or disconnected */
436 /* - bypass regular exit block */
440 "control read: timeout, giving up after %d tries\n",
445 ucs
->rcvbuf_size
= 0;
449 /* set/clear bits in base connection state, return previous state
451 inline static int update_basstate(struct bas_cardstate
*ucs
,
457 spin_lock_irqsave(&ucs
->lock
, flags
);
458 state
= ucs
->basstate
;
459 ucs
->basstate
= (state
& ~clear
) | set
;
460 spin_unlock_irqrestore(&ucs
->lock
, flags
);
464 /* read_ctrl_callback
465 * USB completion handler for control pipe input
466 * called by the USB subsystem in interrupt context
468 * urb USB request block
469 * urb->context = inbuf structure for controller state
471 static void read_ctrl_callback(struct urb
*urb
)
473 struct inbuf_t
*inbuf
= urb
->context
;
474 struct cardstate
*cs
= inbuf
->cs
;
475 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
476 int status
= urb
->status
;
481 update_basstate(ucs
, 0, BS_ATRDPEND
);
482 wake_up(&ucs
->waitqueue
);
484 if (!ucs
->rcvbuf_size
) {
485 dev_warn(cs
->dev
, "%s: no receive in progress\n", __func__
);
489 del_timer(&ucs
->timer_cmd_in
);
492 case 0: /* normal completion */
493 numbytes
= urb
->actual_length
;
494 if (unlikely(numbytes
!= ucs
->rcvbuf_size
)) {
496 "control read: received %d chars, expected %d\n",
497 numbytes
, ucs
->rcvbuf_size
);
498 if (numbytes
> ucs
->rcvbuf_size
)
499 numbytes
= ucs
->rcvbuf_size
;
502 /* copy received bytes to inbuf */
503 have_data
= gigaset_fill_inbuf(inbuf
, ucs
->rcvbuf
, numbytes
);
505 if (unlikely(numbytes
< ucs
->rcvbuf_size
)) {
506 /* incomplete - resubmit for remaining bytes */
507 ucs
->rcvbuf_size
-= numbytes
;
508 ucs
->retry_cmd_in
= 0;
509 rc
= atread_submit(cs
, BAS_TIMEOUT
);
510 if (rc
>= 0 || rc
== -ENODEV
)
511 /* resubmitted or disconnected */
512 /* - bypass regular exit block */
518 case -ENOENT
: /* cancelled */
519 case -ECONNRESET
: /* cancelled (async) */
520 case -EINPROGRESS
: /* pending */
521 case -ENODEV
: /* device removed */
522 case -ESHUTDOWN
: /* device shut down */
523 /* no action necessary */
524 gig_dbg(DEBUG_USBREQ
, "%s: %s",
525 __func__
, get_usb_statmsg(status
));
528 default: /* severe trouble */
529 dev_warn(cs
->dev
, "control read: %s\n",
530 get_usb_statmsg(status
));
531 if (ucs
->retry_cmd_in
++ < BAS_RETRY
) {
532 dev_notice(cs
->dev
, "control read: retry %d\n",
534 rc
= atread_submit(cs
, BAS_TIMEOUT
);
535 if (rc
>= 0 || rc
== -ENODEV
)
536 /* resubmitted or disconnected */
537 /* - bypass regular exit block */
541 "control read: giving up after %d tries\n",
549 ucs
->rcvbuf_size
= 0;
551 gig_dbg(DEBUG_INTR
, "%s-->BH", __func__
);
552 gigaset_schedule_event(cs
);
557 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
559 * cs controller state structure
560 * timeout timeout in 1/10 sec., 0: none
563 * -EBUSY if another request is pending
564 * any URB submission error code
566 static int atread_submit(struct cardstate
*cs
, int timeout
)
568 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
572 gig_dbg(DEBUG_USBREQ
, "-------> HD_READ_ATMESSAGE (%d)",
575 basstate
= update_basstate(ucs
, BS_ATRDPEND
, 0);
576 if (basstate
& BS_ATRDPEND
) {
578 "could not submit HD_READ_ATMESSAGE: URB busy\n");
582 if (basstate
& BS_SUSPEND
) {
584 "HD_READ_ATMESSAGE not submitted, "
585 "suspend in progress\n");
586 update_basstate(ucs
, 0, BS_ATRDPEND
);
587 /* treat like disconnect */
591 ucs
->dr_cmd_in
.bRequestType
= IN_VENDOR_REQ
;
592 ucs
->dr_cmd_in
.bRequest
= HD_READ_ATMESSAGE
;
593 ucs
->dr_cmd_in
.wValue
= 0;
594 ucs
->dr_cmd_in
.wIndex
= 0;
595 ucs
->dr_cmd_in
.wLength
= cpu_to_le16(ucs
->rcvbuf_size
);
596 usb_fill_control_urb(ucs
->urb_cmd_in
, ucs
->udev
,
597 usb_rcvctrlpipe(ucs
->udev
, 0),
598 (unsigned char*) & ucs
->dr_cmd_in
,
599 ucs
->rcvbuf
, ucs
->rcvbuf_size
,
600 read_ctrl_callback
, cs
->inbuf
);
602 if ((ret
= usb_submit_urb(ucs
->urb_cmd_in
, GFP_ATOMIC
)) != 0) {
603 update_basstate(ucs
, 0, BS_ATRDPEND
);
604 dev_err(cs
->dev
, "could not submit HD_READ_ATMESSAGE: %s\n",
610 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
611 ucs
->timer_cmd_in
.expires
= jiffies
+ timeout
* HZ
/ 10;
612 ucs
->timer_cmd_in
.data
= (unsigned long) cs
;
613 ucs
->timer_cmd_in
.function
= cmd_in_timeout
;
614 add_timer(&ucs
->timer_cmd_in
);
620 * USB completion handler for interrupt pipe input
621 * called by the USB subsystem in interrupt context
623 * urb USB request block
624 * urb->context = controller state structure
626 static void read_int_callback(struct urb
*urb
)
628 struct cardstate
*cs
= urb
->context
;
629 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
630 struct bc_state
*bcs
;
631 int status
= urb
->status
;
638 case 0: /* success */
640 case -ENOENT
: /* cancelled */
641 case -ECONNRESET
: /* cancelled (async) */
642 case -EINPROGRESS
: /* pending */
643 /* ignore silently */
644 gig_dbg(DEBUG_USBREQ
, "%s: %s",
645 __func__
, get_usb_statmsg(status
));
647 case -ENODEV
: /* device removed */
648 case -ESHUTDOWN
: /* device shut down */
649 //FIXME use this as disconnect indicator?
650 gig_dbg(DEBUG_USBREQ
, "%s: device disconnected", __func__
);
652 default: /* severe trouble */
653 dev_warn(cs
->dev
, "interrupt read: %s\n",
654 get_usb_statmsg(status
));
655 //FIXME corrective action? resubmission always ok?
659 /* drop incomplete packets even if the missing bytes wouldn't matter */
660 if (unlikely(urb
->actual_length
< 3)) {
661 dev_warn(cs
->dev
, "incomplete interrupt packet (%d bytes)\n",
666 l
= (unsigned) ucs
->int_in_buf
[1] +
667 (((unsigned) ucs
->int_in_buf
[2]) << 8);
669 gig_dbg(DEBUG_USBREQ
, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
670 urb
->actual_length
, (int)ucs
->int_in_buf
[0], l
,
671 (int)ucs
->int_in_buf
[1], (int)ucs
->int_in_buf
[2]);
675 switch (ucs
->int_in_buf
[0]) {
676 case HD_DEVICE_INIT_OK
:
677 update_basstate(ucs
, BS_INIT
, 0);
680 case HD_READY_SEND_ATDATA
:
681 del_timer(&ucs
->timer_atrdy
);
682 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
686 case HD_OPEN_B2CHANNEL_ACK
:
688 case HD_OPEN_B1CHANNEL_ACK
:
689 bcs
= cs
->bcs
+ channel
;
690 update_basstate(ucs
, BS_B1OPEN
<< channel
, 0);
691 gigaset_bchannel_up(bcs
);
694 case HD_OPEN_ATCHANNEL_ACK
:
695 update_basstate(ucs
, BS_ATOPEN
, 0);
699 case HD_CLOSE_B2CHANNEL_ACK
:
701 case HD_CLOSE_B1CHANNEL_ACK
:
702 bcs
= cs
->bcs
+ channel
;
703 update_basstate(ucs
, 0, BS_B1OPEN
<< channel
);
704 stopurbs(bcs
->hw
.bas
);
705 gigaset_bchannel_down(bcs
);
708 case HD_CLOSE_ATCHANNEL_ACK
:
709 update_basstate(ucs
, 0, BS_ATOPEN
);
712 case HD_B2_FLOW_CONTROL
:
714 case HD_B1_FLOW_CONTROL
:
715 bcs
= cs
->bcs
+ channel
;
716 atomic_add((l
- BAS_NORMFRAME
) * BAS_CORRFRAMES
,
717 &bcs
->hw
.bas
->corrbytes
);
719 "Flow control (channel %d, sub %d): 0x%02x => %d",
720 channel
, bcs
->hw
.bas
->numsub
, l
,
721 atomic_read(&bcs
->hw
.bas
->corrbytes
));
724 case HD_RECEIVEATDATA_ACK
: /* AT response ready to be received */
727 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
730 spin_lock_irqsave(&cs
->lock
, flags
);
731 if (ucs
->rcvbuf_size
) {
732 /* throw away previous buffer - we have no queue */
734 "receive AT data overrun, %d bytes lost\n",
737 ucs
->rcvbuf_size
= 0;
739 if ((ucs
->rcvbuf
= kmalloc(l
, GFP_ATOMIC
)) == NULL
) {
740 spin_unlock_irqrestore(&cs
->lock
, flags
);
741 dev_err(cs
->dev
, "out of memory receiving AT data\n");
745 ucs
->rcvbuf_size
= l
;
746 ucs
->retry_cmd_in
= 0;
747 if ((rc
= atread_submit(cs
, BAS_TIMEOUT
)) < 0) {
750 ucs
->rcvbuf_size
= 0;
752 //FIXME corrective action?
753 spin_unlock_irqrestore(&cs
->lock
, flags
);
758 spin_unlock_irqrestore(&cs
->lock
, flags
);
761 case HD_RESET_INTERRUPT_PIPE_ACK
:
762 gig_dbg(DEBUG_USBREQ
, "HD_RESET_INTERRUPT_PIPE_ACK");
766 gig_dbg(DEBUG_USBREQ
, "HD_SUSPEND_END");
771 "unknown Gigaset signal 0x%02x (%u) ignored\n",
772 (int) ucs
->int_in_buf
[0], l
);
776 wake_up(&ucs
->waitqueue
);
779 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
780 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
781 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
788 * USB completion handler for B channel isochronous input
789 * called by the USB subsystem in interrupt context
791 * urb USB request block of completed request
792 * urb->context = bc_state structure
794 static void read_iso_callback(struct urb
*urb
)
796 struct bc_state
*bcs
;
797 struct bas_bc_state
*ubc
;
798 int status
= urb
->status
;
802 /* status codes not worth bothering the tasklet with */
803 if (unlikely(status
== -ENOENT
||
804 status
== -ECONNRESET
||
805 status
== -EINPROGRESS
||
807 status
== -ESHUTDOWN
)) {
808 gig_dbg(DEBUG_ISO
, "%s: %s",
809 __func__
, get_usb_statmsg(status
));
816 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
817 if (likely(ubc
->isoindone
== NULL
)) {
818 /* pass URB to tasklet */
819 ubc
->isoindone
= urb
;
820 ubc
->isoinstatus
= status
;
821 tasklet_schedule(&ubc
->rcvd_tasklet
);
823 /* tasklet still busy, drop data and resubmit URB */
824 ubc
->loststatus
= status
;
825 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
826 ubc
->isoinlost
+= urb
->iso_frame_desc
[i
].actual_length
;
827 if (unlikely(urb
->iso_frame_desc
[i
].status
!= 0 &&
828 urb
->iso_frame_desc
[i
].status
!=
830 ubc
->loststatus
= urb
->iso_frame_desc
[i
].status
;
831 urb
->iso_frame_desc
[i
].status
= 0;
832 urb
->iso_frame_desc
[i
].actual_length
= 0;
834 if (likely(ubc
->running
)) {
835 /* urb->dev is clobbered by USB subsystem */
836 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
837 urb
->transfer_flags
= URB_ISO_ASAP
;
838 urb
->number_of_packets
= BAS_NUMFRAMES
;
839 gig_dbg(DEBUG_ISO
, "%s: isoc read overrun/resubmit",
841 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
842 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
843 dev_err(bcs
->cs
->dev
,
844 "could not resubmit isochronous read "
845 "URB: %s\n", get_usb_rcmsg(rc
));
846 dump_urb(DEBUG_ISO
, "isoc read", urb
);
851 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
854 /* write_iso_callback
855 * USB completion handler for B channel isochronous output
856 * called by the USB subsystem in interrupt context
858 * urb USB request block of completed request
859 * urb->context = isow_urbctx_t structure
861 static void write_iso_callback(struct urb
*urb
)
863 struct isow_urbctx_t
*ucx
;
864 struct bas_bc_state
*ubc
;
865 int status
= urb
->status
;
868 /* status codes not worth bothering the tasklet with */
869 if (unlikely(status
== -ENOENT
||
870 status
== -ECONNRESET
||
871 status
== -EINPROGRESS
||
873 status
== -ESHUTDOWN
)) {
874 gig_dbg(DEBUG_ISO
, "%s: %s",
875 __func__
, get_usb_statmsg(status
));
879 /* pass URB context to tasklet */
881 ubc
= ucx
->bcs
->hw
.bas
;
882 ucx
->status
= status
;
884 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
885 ubc
->isooutovfl
= ubc
->isooutdone
;
886 ubc
->isooutdone
= ucx
;
887 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
888 tasklet_schedule(&ubc
->sent_tasklet
);
892 * prepare and submit USB request blocks for isochronous input and output
894 * B channel control structure
897 * < 0 on error (no URBs submitted)
899 static int starturbs(struct bc_state
*bcs
)
901 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
906 /* initialize L2 reception */
907 if (bcs
->proto2
== ISDN_PROTO_L2_HDLC
)
908 bcs
->inputstate
|= INS_flag_hunt
;
910 /* submit all isochronous input URBs */
912 for (k
= 0; k
< BAS_INURBS
; k
++) {
913 urb
= ubc
->isoinurbs
[k
];
919 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
920 urb
->pipe
= usb_rcvisocpipe(urb
->dev
, 3 + 2 * bcs
->channel
);
921 urb
->transfer_flags
= URB_ISO_ASAP
;
922 urb
->transfer_buffer
= ubc
->isoinbuf
+ k
* BAS_INBUFSIZE
;
923 urb
->transfer_buffer_length
= BAS_INBUFSIZE
;
924 urb
->number_of_packets
= BAS_NUMFRAMES
;
925 urb
->interval
= BAS_FRAMETIME
;
926 urb
->complete
= read_iso_callback
;
928 for (j
= 0; j
< BAS_NUMFRAMES
; j
++) {
929 urb
->iso_frame_desc
[j
].offset
= j
* BAS_MAXFRAME
;
930 urb
->iso_frame_desc
[j
].length
= BAS_MAXFRAME
;
931 urb
->iso_frame_desc
[j
].status
= 0;
932 urb
->iso_frame_desc
[j
].actual_length
= 0;
935 dump_urb(DEBUG_ISO
, "Initial isoc read", urb
);
936 if ((rc
= usb_submit_urb(urb
, GFP_ATOMIC
)) != 0)
940 /* initialize L2 transmission */
941 gigaset_isowbuf_init(ubc
->isooutbuf
, PPP_FLAG
);
943 /* set up isochronous output URBs for flag idling */
944 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
945 urb
= ubc
->isoouturbs
[k
].urb
;
950 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
951 urb
->pipe
= usb_sndisocpipe(urb
->dev
, 4 + 2 * bcs
->channel
);
952 urb
->transfer_flags
= URB_ISO_ASAP
;
953 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
954 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
955 urb
->number_of_packets
= BAS_NUMFRAMES
;
956 urb
->interval
= BAS_FRAMETIME
;
957 urb
->complete
= write_iso_callback
;
958 urb
->context
= &ubc
->isoouturbs
[k
];
959 for (j
= 0; j
< BAS_NUMFRAMES
; ++j
) {
960 urb
->iso_frame_desc
[j
].offset
= BAS_OUTBUFSIZE
;
961 urb
->iso_frame_desc
[j
].length
= BAS_NORMFRAME
;
962 urb
->iso_frame_desc
[j
].status
= 0;
963 urb
->iso_frame_desc
[j
].actual_length
= 0;
965 ubc
->isoouturbs
[k
].limit
= -1;
968 /* keep one URB free, submit the others */
969 for (k
= 0; k
< BAS_OUTURBS
-1; ++k
) {
970 dump_urb(DEBUG_ISO
, "Initial isoc write", urb
);
971 rc
= usb_submit_urb(ubc
->isoouturbs
[k
].urb
, GFP_ATOMIC
);
975 dump_urb(DEBUG_ISO
, "Initial isoc write (free)", urb
);
976 ubc
->isooutfree
= &ubc
->isoouturbs
[BAS_OUTURBS
-1];
977 ubc
->isooutdone
= ubc
->isooutovfl
= NULL
;
985 * cancel the USB request blocks for isochronous input and output
986 * errors are silently ignored
988 * B channel control structure
990 static void stopurbs(struct bas_bc_state
*ubc
)
996 for (k
= 0; k
< BAS_INURBS
; ++k
) {
997 rc
= usb_unlink_urb(ubc
->isoinurbs
[k
]);
999 "%s: isoc input URB %d unlinked, result = %s",
1000 __func__
, k
, get_usb_rcmsg(rc
));
1003 for (k
= 0; k
< BAS_OUTURBS
; ++k
) {
1004 rc
= usb_unlink_urb(ubc
->isoouturbs
[k
].urb
);
1006 "%s: isoc output URB %d unlinked, result = %s",
1007 __func__
, k
, get_usb_rcmsg(rc
));
1011 /* Isochronous Write - Bottom Half */
1012 /* =============================== */
1014 /* submit_iso_write_urb
1015 * fill and submit the next isochronous write URB
1017 * ucx context structure containing URB
1019 * number of frames submitted in URB
1020 * 0 if URB not submitted because no data available (isooutbuf busy)
1021 * error code < 0 on error
1023 static int submit_iso_write_urb(struct isow_urbctx_t
*ucx
)
1025 struct urb
*urb
= ucx
->urb
;
1026 struct bas_bc_state
*ubc
= ucx
->bcs
->hw
.bas
;
1027 struct usb_iso_packet_descriptor
*ifd
;
1028 int corrbytes
, nframe
, rc
;
1030 /* urb->dev is clobbered by USB subsystem */
1031 urb
->dev
= ucx
->bcs
->cs
->hw
.bas
->udev
;
1032 urb
->transfer_flags
= URB_ISO_ASAP
;
1033 urb
->transfer_buffer
= ubc
->isooutbuf
->data
;
1034 urb
->transfer_buffer_length
= sizeof(ubc
->isooutbuf
->data
);
1036 for (nframe
= 0; nframe
< BAS_NUMFRAMES
; nframe
++) {
1037 ifd
= &urb
->iso_frame_desc
[nframe
];
1039 /* compute frame length according to flow control */
1040 ifd
->length
= BAS_NORMFRAME
;
1041 if ((corrbytes
= atomic_read(&ubc
->corrbytes
)) != 0) {
1042 gig_dbg(DEBUG_ISO
, "%s: corrbytes=%d",
1043 __func__
, corrbytes
);
1044 if (corrbytes
> BAS_HIGHFRAME
- BAS_NORMFRAME
)
1045 corrbytes
= BAS_HIGHFRAME
- BAS_NORMFRAME
;
1046 else if (corrbytes
< BAS_LOWFRAME
- BAS_NORMFRAME
)
1047 corrbytes
= BAS_LOWFRAME
- BAS_NORMFRAME
;
1048 ifd
->length
+= corrbytes
;
1049 atomic_add(-corrbytes
, &ubc
->corrbytes
);
1052 /* retrieve block of data to send */
1053 ifd
->offset
= gigaset_isowbuf_getbytes(ubc
->isooutbuf
,
1055 if (ifd
->offset
< 0) {
1056 if (ifd
->offset
== -EBUSY
) {
1058 "%s: buffer busy at frame %d",
1060 /* tasklet will be restarted from
1061 gigaset_send_skb() */
1063 dev_err(ucx
->bcs
->cs
->dev
,
1064 "%s: buffer error %d at frame %d\n",
1065 __func__
, ifd
->offset
, nframe
);
1070 ucx
->limit
= ubc
->isooutbuf
->nextread
;
1072 ifd
->actual_length
= 0;
1074 if (unlikely(nframe
== 0))
1075 return 0; /* no data to send */
1076 urb
->number_of_packets
= nframe
;
1078 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1081 /* device removed - give up silently */
1082 gig_dbg(DEBUG_ISO
, "%s: disconnected", __func__
);
1084 dev_err(ucx
->bcs
->cs
->dev
,
1085 "could not submit isochronous write URB: %s\n",
1093 /* write_iso_tasklet
1094 * tasklet scheduled when an isochronous output URB from the Gigaset device
1097 * data B channel state structure
1099 static void write_iso_tasklet(unsigned long data
)
1101 struct bc_state
*bcs
= (struct bc_state
*) data
;
1102 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1103 struct cardstate
*cs
= bcs
->cs
;
1104 struct isow_urbctx_t
*done
, *next
, *ovfl
;
1107 struct usb_iso_packet_descriptor
*ifd
;
1109 unsigned long flags
;
1111 struct sk_buff
*skb
;
1115 /* loop while completed URBs arrive in time */
1117 if (unlikely(!(ubc
->running
))) {
1118 gig_dbg(DEBUG_ISO
, "%s: not running", __func__
);
1122 /* retrieve completed URBs */
1123 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1124 done
= ubc
->isooutdone
;
1125 ubc
->isooutdone
= NULL
;
1126 ovfl
= ubc
->isooutovfl
;
1127 ubc
->isooutovfl
= NULL
;
1128 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1130 dev_err(cs
->dev
, "isochronous write buffer underrun\n");
1137 /* submit free URB if available */
1138 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1139 next
= ubc
->isooutfree
;
1140 ubc
->isooutfree
= NULL
;
1141 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1143 rc
= submit_iso_write_urb(next
);
1144 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1145 /* could not submit URB, put it back */
1146 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1147 if (ubc
->isooutfree
== NULL
) {
1148 ubc
->isooutfree
= next
;
1151 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1153 /* couldn't put it back */
1155 "losing isochronous write URB\n");
1161 /* process completed URB */
1163 status
= done
->status
;
1165 case -EXDEV
: /* partial completion */
1166 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1168 /* fall through - what's the difference anyway? */
1169 case 0: /* normal completion */
1170 /* inspect individual frames
1171 * assumptions (for lack of documentation):
1172 * - actual_length bytes of first frame in error are
1174 * - all following frames are not sent at all
1176 offset
= done
->limit
; /* default (no error) */
1177 for (i
= 0; i
< BAS_NUMFRAMES
; i
++) {
1178 ifd
= &urb
->iso_frame_desc
[i
];
1180 ifd
->actual_length
!= ifd
->length
) {
1182 "isochronous write: frame %d: %s, "
1183 "only %d of %d bytes sent\n",
1184 i
, get_usb_statmsg(ifd
->status
),
1185 ifd
->actual_length
, ifd
->length
);
1186 offset
= (ifd
->offset
+
1192 #ifdef CONFIG_GIGASET_DEBUG
1193 /* check assumption on remaining frames */
1194 for (; i
< BAS_NUMFRAMES
; i
++) {
1195 ifd
= &urb
->iso_frame_desc
[i
];
1196 if (ifd
->status
!= -EINPROGRESS
1197 || ifd
->actual_length
!= 0) {
1199 "isochronous write: frame %d: %s, "
1200 "%d of %d bytes sent\n",
1201 i
, get_usb_statmsg(ifd
->status
),
1202 ifd
->actual_length
, ifd
->length
);
1203 offset
= (ifd
->offset
+
1211 case -EPIPE
: /* stall - probably underrun */
1212 dev_err(cs
->dev
, "isochronous write stalled\n");
1215 default: /* severe trouble */
1216 dev_warn(cs
->dev
, "isochronous write: %s\n",
1217 get_usb_statmsg(status
));
1220 /* mark the write buffer area covered by this URB as free */
1221 if (done
->limit
>= 0)
1222 ubc
->isooutbuf
->read
= done
->limit
;
1224 /* mark URB as free */
1225 spin_lock_irqsave(&ubc
->isooutlock
, flags
);
1226 next
= ubc
->isooutfree
;
1227 ubc
->isooutfree
= done
;
1228 spin_unlock_irqrestore(&ubc
->isooutlock
, flags
);
1230 /* only one URB still active - resubmit one */
1231 rc
= submit_iso_write_urb(next
);
1232 if (unlikely(rc
<= 0 && rc
!= -ENODEV
)) {
1233 /* couldn't submit */
1239 /* process queued SKBs */
1240 while ((skb
= skb_dequeue(&bcs
->squeue
))) {
1241 /* copy to output buffer, doing L2 encapsulation */
1243 if (gigaset_isoc_buildframe(bcs
, skb
->data
, len
) == -EAGAIN
) {
1244 /* insufficient buffer space, push back onto queue */
1245 skb_queue_head(&bcs
->squeue
, skb
);
1246 gig_dbg(DEBUG_ISO
, "%s: skb requeued, qlen=%d",
1247 __func__
, skb_queue_len(&bcs
->squeue
));
1251 gigaset_skb_sent(bcs
, skb
);
1252 dev_kfree_skb_any(skb
);
1256 /* Isochronous Read - Bottom Half */
1257 /* ============================== */
1260 * tasklet scheduled when an isochronous input URB from the Gigaset device
1263 * data B channel state structure
1265 static void read_iso_tasklet(unsigned long data
)
1267 struct bc_state
*bcs
= (struct bc_state
*) data
;
1268 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
1269 struct cardstate
*cs
= bcs
->cs
;
1273 unsigned long flags
;
1274 int totleft
, numbytes
, offset
, frame
, rc
;
1276 /* loop while more completed URBs arrive in the meantime */
1279 spin_lock_irqsave(&ubc
->isoinlock
, flags
);
1280 if (!(urb
= ubc
->isoindone
)) {
1281 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1284 status
= ubc
->isoinstatus
;
1285 ubc
->isoindone
= NULL
;
1286 if (unlikely(ubc
->loststatus
!= -EINPROGRESS
)) {
1288 "isochronous read overrun, "
1289 "dropped URB with status: %s, %d bytes lost\n",
1290 get_usb_statmsg(ubc
->loststatus
),
1292 ubc
->loststatus
= -EINPROGRESS
;
1294 spin_unlock_irqrestore(&ubc
->isoinlock
, flags
);
1296 if (unlikely(!(ubc
->running
))) {
1298 "%s: channel not running, "
1299 "dropped URB with status: %s",
1300 __func__
, get_usb_statmsg(status
));
1305 case 0: /* normal completion */
1307 case -EXDEV
: /* inspect individual frames
1308 (we do that anyway) */
1309 gig_dbg(DEBUG_ISO
, "%s: URB partially completed",
1315 gig_dbg(DEBUG_ISO
, "%s: %s",
1316 __func__
, get_usb_statmsg(status
));
1317 continue; /* -> skip */
1319 dev_err(cs
->dev
, "isochronous read stalled\n");
1321 continue; /* -> skip */
1322 default: /* severe trouble */
1323 dev_warn(cs
->dev
, "isochronous read: %s\n",
1324 get_usb_statmsg(status
));
1328 rcvbuf
= urb
->transfer_buffer
;
1329 totleft
= urb
->actual_length
;
1330 for (frame
= 0; totleft
> 0 && frame
< BAS_NUMFRAMES
; frame
++) {
1331 if (unlikely(urb
->iso_frame_desc
[frame
].status
)) {
1333 "isochronous read: frame %d: %s\n",
1336 urb
->iso_frame_desc
[frame
].status
));
1339 numbytes
= urb
->iso_frame_desc
[frame
].actual_length
;
1340 if (unlikely(numbytes
> BAS_MAXFRAME
)) {
1342 "isochronous read: frame %d: "
1343 "numbytes (%d) > BAS_MAXFRAME\n",
1347 if (unlikely(numbytes
> totleft
)) {
1349 "isochronous read: frame %d: "
1350 "numbytes (%d) > totleft (%d)\n",
1351 frame
, numbytes
, totleft
);
1354 offset
= urb
->iso_frame_desc
[frame
].offset
;
1355 if (unlikely(offset
+ numbytes
> BAS_INBUFSIZE
)) {
1357 "isochronous read: frame %d: "
1358 "offset (%d) + numbytes (%d) "
1359 "> BAS_INBUFSIZE\n",
1360 frame
, offset
, numbytes
);
1363 gigaset_isoc_receive(rcvbuf
+ offset
, numbytes
, bcs
);
1364 totleft
-= numbytes
;
1366 if (unlikely(totleft
> 0))
1368 "isochronous read: %d data bytes missing\n",
1372 /* URB processed, resubmit */
1373 for (frame
= 0; frame
< BAS_NUMFRAMES
; frame
++) {
1374 urb
->iso_frame_desc
[frame
].status
= 0;
1375 urb
->iso_frame_desc
[frame
].actual_length
= 0;
1377 /* urb->dev is clobbered by USB subsystem */
1378 urb
->dev
= bcs
->cs
->hw
.bas
->udev
;
1379 urb
->transfer_flags
= URB_ISO_ASAP
;
1380 urb
->number_of_packets
= BAS_NUMFRAMES
;
1381 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1382 if (unlikely(rc
!= 0 && rc
!= -ENODEV
)) {
1384 "could not resubmit isochronous read URB: %s\n",
1386 dump_urb(DEBUG_ISO
, "resubmit iso read", urb
);
1392 /* Channel Operations */
1393 /* ================== */
1396 * timeout routine for control output request
1398 * B channel control structure
1400 static void req_timeout(unsigned long data
)
1402 struct bc_state
*bcs
= (struct bc_state
*) data
;
1403 struct bas_cardstate
*ucs
= bcs
->cs
->hw
.bas
;
1405 unsigned long flags
;
1409 spin_lock_irqsave(&ucs
->lock
, flags
);
1410 pending
= ucs
->pending
;
1412 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1415 case 0: /* no pending request */
1416 gig_dbg(DEBUG_USBREQ
, "%s: no request pending", __func__
);
1419 case HD_OPEN_ATCHANNEL
:
1420 dev_err(bcs
->cs
->dev
, "timeout opening AT channel\n");
1421 error_reset(bcs
->cs
);
1424 case HD_OPEN_B2CHANNEL
:
1425 case HD_OPEN_B1CHANNEL
:
1426 dev_err(bcs
->cs
->dev
, "timeout opening channel %d\n",
1431 case HD_CLOSE_ATCHANNEL
:
1432 dev_err(bcs
->cs
->dev
, "timeout closing AT channel\n");
1435 case HD_CLOSE_B2CHANNEL
:
1436 case HD_CLOSE_B1CHANNEL
:
1437 dev_err(bcs
->cs
->dev
, "timeout closing channel %d\n",
1439 error_reset(bcs
->cs
);
1443 dev_warn(bcs
->cs
->dev
, "request 0x%02x timed out, clearing\n",
1447 wake_up(&ucs
->waitqueue
);
1450 /* write_ctrl_callback
1451 * USB completion handler for control pipe output
1452 * called by the USB subsystem in interrupt context
1454 * urb USB request block of completed request
1455 * urb->context = hardware specific controller state structure
1457 static void write_ctrl_callback(struct urb
*urb
)
1459 struct bas_cardstate
*ucs
= urb
->context
;
1460 int status
= urb
->status
;
1462 unsigned long flags
;
1466 case 0: /* normal completion */
1467 spin_lock_irqsave(&ucs
->lock
, flags
);
1468 switch (ucs
->pending
) {
1469 case HD_DEVICE_INIT_ACK
: /* no reply expected */
1470 del_timer(&ucs
->timer_ctrl
);
1474 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1477 case -ENOENT
: /* cancelled */
1478 case -ECONNRESET
: /* cancelled (async) */
1479 case -EINPROGRESS
: /* pending */
1480 case -ENODEV
: /* device removed */
1481 case -ESHUTDOWN
: /* device shut down */
1482 /* ignore silently */
1483 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1484 __func__
, get_usb_statmsg(status
));
1487 default: /* any failure */
1488 /* don't retry if suspend requested */
1489 if (++ucs
->retry_ctrl
> BAS_RETRY
||
1490 (ucs
->basstate
& BS_SUSPEND
)) {
1491 dev_err(&ucs
->interface
->dev
,
1492 "control request 0x%02x failed: %s\n",
1493 ucs
->dr_ctrl
.bRequest
,
1494 get_usb_statmsg(status
));
1495 break; /* give up */
1497 dev_notice(&ucs
->interface
->dev
,
1498 "control request 0x%02x: %s, retry %d\n",
1499 ucs
->dr_ctrl
.bRequest
, get_usb_statmsg(status
),
1501 /* urb->dev is clobbered by USB subsystem */
1502 urb
->dev
= ucs
->udev
;
1503 rc
= usb_submit_urb(urb
, GFP_ATOMIC
);
1505 dev_err(&ucs
->interface
->dev
,
1506 "could not resubmit request 0x%02x: %s\n",
1507 ucs
->dr_ctrl
.bRequest
, get_usb_rcmsg(rc
));
1514 /* failed, clear pending request */
1515 spin_lock_irqsave(&ucs
->lock
, flags
);
1516 del_timer(&ucs
->timer_ctrl
);
1518 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1519 wake_up(&ucs
->waitqueue
);
1523 * submit a control output request without message buffer to the Gigaset base
1524 * and optionally start a timeout
1526 * bcs B channel control structure
1527 * req control request code (HD_*)
1528 * val control request parameter value (set to 0 if unused)
1529 * timeout timeout in seconds (0: no timeout)
1532 * -EBUSY if another request is pending
1533 * any URB submission error code
1535 static int req_submit(struct bc_state
*bcs
, int req
, int val
, int timeout
)
1537 struct bas_cardstate
*ucs
= bcs
->cs
->hw
.bas
;
1539 unsigned long flags
;
1541 gig_dbg(DEBUG_USBREQ
, "-------> 0x%02x (%d)", req
, val
);
1543 spin_lock_irqsave(&ucs
->lock
, flags
);
1545 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1546 dev_err(bcs
->cs
->dev
,
1547 "submission of request 0x%02x failed: "
1548 "request 0x%02x still pending\n",
1553 ucs
->dr_ctrl
.bRequestType
= OUT_VENDOR_REQ
;
1554 ucs
->dr_ctrl
.bRequest
= req
;
1555 ucs
->dr_ctrl
.wValue
= cpu_to_le16(val
);
1556 ucs
->dr_ctrl
.wIndex
= 0;
1557 ucs
->dr_ctrl
.wLength
= 0;
1558 usb_fill_control_urb(ucs
->urb_ctrl
, ucs
->udev
,
1559 usb_sndctrlpipe(ucs
->udev
, 0),
1560 (unsigned char*) &ucs
->dr_ctrl
, NULL
, 0,
1561 write_ctrl_callback
, ucs
);
1562 ucs
->retry_ctrl
= 0;
1563 ret
= usb_submit_urb(ucs
->urb_ctrl
, GFP_ATOMIC
);
1564 if (unlikely(ret
)) {
1565 dev_err(bcs
->cs
->dev
, "could not submit request 0x%02x: %s\n",
1566 req
, get_usb_rcmsg(ret
));
1567 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1573 gig_dbg(DEBUG_USBREQ
, "setting timeout of %d/10 secs", timeout
);
1574 ucs
->timer_ctrl
.expires
= jiffies
+ timeout
* HZ
/ 10;
1575 ucs
->timer_ctrl
.data
= (unsigned long) bcs
;
1576 ucs
->timer_ctrl
.function
= req_timeout
;
1577 add_timer(&ucs
->timer_ctrl
);
1580 spin_unlock_irqrestore(&ucs
->lock
, flags
);
1584 /* gigaset_init_bchannel
1585 * called by common.c to connect a B channel
1586 * initialize isochronous I/O and tell the Gigaset base to open the channel
1588 * B channel control structure
1590 * 0 on success, error code < 0 on error
1592 static int gigaset_init_bchannel(struct bc_state
*bcs
)
1594 struct cardstate
*cs
= bcs
->cs
;
1596 unsigned long flags
;
1598 spin_lock_irqsave(&cs
->lock
, flags
);
1599 if (unlikely(!cs
->connected
)) {
1600 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1601 spin_unlock_irqrestore(&cs
->lock
, flags
);
1605 if (cs
->hw
.bas
->basstate
& BS_SUSPEND
) {
1607 "not starting isochronous I/O, "
1608 "suspend in progress\n");
1609 spin_unlock_irqrestore(&cs
->lock
, flags
);
1610 return -EHOSTUNREACH
;
1613 if ((ret
= starturbs(bcs
)) < 0) {
1615 "could not start isochronous I/O for channel B%d: %s\n",
1617 ret
== -EFAULT
? "null URB" : get_usb_rcmsg(ret
));
1620 spin_unlock_irqrestore(&cs
->lock
, flags
);
1624 req
= bcs
->channel
? HD_OPEN_B2CHANNEL
: HD_OPEN_B1CHANNEL
;
1625 if ((ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
)) < 0) {
1626 dev_err(cs
->dev
, "could not open channel B%d\n",
1628 stopurbs(bcs
->hw
.bas
);
1633 spin_unlock_irqrestore(&cs
->lock
, flags
);
1637 /* gigaset_close_bchannel
1638 * called by common.c to disconnect a B channel
1639 * tell the Gigaset base to close the channel
1640 * stopping isochronous I/O and LL notification will be done when the
1641 * acknowledgement for the close arrives
1643 * B channel control structure
1645 * 0 on success, error code < 0 on error
1647 static int gigaset_close_bchannel(struct bc_state
*bcs
)
1649 struct cardstate
*cs
= bcs
->cs
;
1651 unsigned long flags
;
1653 spin_lock_irqsave(&cs
->lock
, flags
);
1654 if (unlikely(!cs
->connected
)) {
1655 spin_unlock_irqrestore(&cs
->lock
, flags
);
1656 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1660 if (!(cs
->hw
.bas
->basstate
& (bcs
->channel
? BS_B2OPEN
: BS_B1OPEN
))) {
1661 /* channel not running: just signal common.c */
1662 spin_unlock_irqrestore(&cs
->lock
, flags
);
1663 gigaset_bchannel_down(bcs
);
1667 /* channel running: tell device to close it */
1668 req
= bcs
->channel
? HD_CLOSE_B2CHANNEL
: HD_CLOSE_B1CHANNEL
;
1669 if ((ret
= req_submit(bcs
, req
, 0, BAS_TIMEOUT
)) < 0)
1670 dev_err(cs
->dev
, "closing channel B%d failed\n",
1673 spin_unlock_irqrestore(&cs
->lock
, flags
);
1677 /* Device Operations */
1678 /* ================= */
1681 * unqueue first command buffer from queue, waking any sleepers
1682 * must be called with cs->cmdlock held
1684 * cs controller state structure
1686 static void complete_cb(struct cardstate
*cs
)
1688 struct cmdbuf_t
*cb
= cs
->cmdbuf
;
1690 /* unqueue completed buffer */
1691 cs
->cmdbytes
-= cs
->curlen
;
1692 gig_dbg(DEBUG_TRANSCMD
|DEBUG_LOCKCMD
,
1693 "write_command: sent %u bytes, %u left",
1694 cs
->curlen
, cs
->cmdbytes
);
1695 if ((cs
->cmdbuf
= cb
->next
) != NULL
) {
1696 cs
->cmdbuf
->prev
= NULL
;
1697 cs
->curlen
= cs
->cmdbuf
->len
;
1699 cs
->lastcmdbuf
= NULL
;
1703 if (cb
->wake_tasklet
)
1704 tasklet_schedule(cb
->wake_tasklet
);
1709 /* write_command_callback
1710 * USB completion handler for AT command transmission
1711 * called by the USB subsystem in interrupt context
1713 * urb USB request block of completed request
1714 * urb->context = controller state structure
1716 static void write_command_callback(struct urb
*urb
)
1718 struct cardstate
*cs
= urb
->context
;
1719 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1720 int status
= urb
->status
;
1721 unsigned long flags
;
1723 update_basstate(ucs
, 0, BS_ATWRPEND
);
1724 wake_up(&ucs
->waitqueue
);
1728 case 0: /* normal completion */
1730 case -ENOENT
: /* cancelled */
1731 case -ECONNRESET
: /* cancelled (async) */
1732 case -EINPROGRESS
: /* pending */
1733 case -ENODEV
: /* device removed */
1734 case -ESHUTDOWN
: /* device shut down */
1735 /* ignore silently */
1736 gig_dbg(DEBUG_USBREQ
, "%s: %s",
1737 __func__
, get_usb_statmsg(status
));
1739 default: /* any failure */
1740 if (++ucs
->retry_cmd_out
> BAS_RETRY
) {
1742 "command write: %s, "
1743 "giving up after %d retries\n",
1744 get_usb_statmsg(status
),
1745 ucs
->retry_cmd_out
);
1748 if (ucs
->basstate
& BS_SUSPEND
) {
1750 "command write: %s, "
1751 "won't retry - suspend requested\n",
1752 get_usb_statmsg(status
));
1755 if (cs
->cmdbuf
== NULL
) {
1757 "command write: %s, "
1758 "cannot retry - cmdbuf gone\n",
1759 get_usb_statmsg(status
));
1762 dev_notice(cs
->dev
, "command write: %s, retry %d\n",
1763 get_usb_statmsg(status
), ucs
->retry_cmd_out
);
1764 if (atwrite_submit(cs
, cs
->cmdbuf
->buf
, cs
->cmdbuf
->len
) >= 0)
1765 /* resubmitted - bypass regular exit block */
1767 /* command send failed, assume base still waiting */
1768 update_basstate(ucs
, BS_ATREADY
, 0);
1771 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1772 if (cs
->cmdbuf
!= NULL
)
1774 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1778 * timeout routine for AT command transmission
1780 * controller state structure
1782 static void atrdy_timeout(unsigned long data
)
1784 struct cardstate
*cs
= (struct cardstate
*) data
;
1785 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1787 dev_warn(cs
->dev
, "timeout waiting for HD_READY_SEND_ATDATA\n");
1789 /* fake the missing signal - what else can I do? */
1790 update_basstate(ucs
, BS_ATREADY
, BS_ATTIMER
);
1795 * submit an HD_WRITE_ATMESSAGE command URB
1797 * cs controller state structure
1798 * buf buffer containing command to send
1799 * len length of command to send
1802 * -EBUSY if another request is pending
1803 * any URB submission error code
1805 static int atwrite_submit(struct cardstate
*cs
, unsigned char *buf
, int len
)
1807 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1810 gig_dbg(DEBUG_USBREQ
, "-------> HD_WRITE_ATMESSAGE (%d)", len
);
1812 if (update_basstate(ucs
, BS_ATWRPEND
, 0) & BS_ATWRPEND
) {
1814 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1818 ucs
->dr_cmd_out
.bRequestType
= OUT_VENDOR_REQ
;
1819 ucs
->dr_cmd_out
.bRequest
= HD_WRITE_ATMESSAGE
;
1820 ucs
->dr_cmd_out
.wValue
= 0;
1821 ucs
->dr_cmd_out
.wIndex
= 0;
1822 ucs
->dr_cmd_out
.wLength
= cpu_to_le16(len
);
1823 usb_fill_control_urb(ucs
->urb_cmd_out
, ucs
->udev
,
1824 usb_sndctrlpipe(ucs
->udev
, 0),
1825 (unsigned char*) &ucs
->dr_cmd_out
, buf
, len
,
1826 write_command_callback
, cs
);
1827 rc
= usb_submit_urb(ucs
->urb_cmd_out
, GFP_ATOMIC
);
1829 update_basstate(ucs
, 0, BS_ATWRPEND
);
1830 dev_err(cs
->dev
, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1835 /* submitted successfully, start timeout if necessary */
1836 if (!(update_basstate(ucs
, BS_ATTIMER
, BS_ATREADY
) & BS_ATTIMER
)) {
1837 gig_dbg(DEBUG_OUTPUT
, "setting ATREADY timeout of %d/10 secs",
1839 ucs
->timer_atrdy
.expires
= jiffies
+ ATRDY_TIMEOUT
* HZ
/ 10;
1840 ucs
->timer_atrdy
.data
= (unsigned long) cs
;
1841 ucs
->timer_atrdy
.function
= atrdy_timeout
;
1842 add_timer(&ucs
->timer_atrdy
);
1848 * start transmission of AT command queue if necessary
1850 * cs controller state structure
1853 * error code < 0 on error
1855 static int start_cbsend(struct cardstate
*cs
)
1857 struct cmdbuf_t
*cb
;
1858 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
1859 unsigned long flags
;
1863 /* check if suspend requested */
1864 if (ucs
->basstate
& BS_SUSPEND
) {
1865 gig_dbg(DEBUG_TRANSCMD
|DEBUG_LOCKCMD
, "suspending");
1866 return -EHOSTUNREACH
;
1869 /* check if AT channel is open */
1870 if (!(ucs
->basstate
& BS_ATOPEN
)) {
1871 gig_dbg(DEBUG_TRANSCMD
|DEBUG_LOCKCMD
, "AT channel not open");
1872 rc
= req_submit(cs
->bcs
, HD_OPEN_ATCHANNEL
, 0, BAS_TIMEOUT
);
1874 /* flush command queue */
1875 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1876 while (cs
->cmdbuf
!= NULL
)
1878 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1883 /* try to send first command in queue */
1884 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1886 while ((cb
= cs
->cmdbuf
) != NULL
&& (ucs
->basstate
& BS_ATREADY
)) {
1887 ucs
->retry_cmd_out
= 0;
1888 rc
= atwrite_submit(cs
, cb
->buf
, cb
->len
);
1895 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1899 /* gigaset_write_cmd
1900 * This function is called by the device independent part of the driver
1901 * to transmit an AT command string to the Gigaset device.
1902 * It encapsulates the device specific method for transmission over the
1903 * direct USB connection to the base.
1904 * The command string is added to the queue of commands to send, and
1905 * USB transmission is started if necessary.
1907 * cs controller state structure
1908 * buf command string to send
1909 * len number of bytes to send (max. IF_WRITEBUF)
1910 * wake_tasklet tasklet to run when transmission is completed
1913 * number of bytes queued on success
1914 * error code < 0 on error
1916 static int gigaset_write_cmd(struct cardstate
*cs
,
1917 const unsigned char *buf
, int len
,
1918 struct tasklet_struct
*wake_tasklet
)
1920 struct cmdbuf_t
*cb
;
1921 unsigned long flags
;
1924 gigaset_dbg_buffer(cs
->mstate
!= MS_LOCKED
?
1925 DEBUG_TRANSCMD
: DEBUG_LOCKCMD
,
1926 "CMD Transmit", len
, buf
);
1934 if (len
> IF_WRITEBUF
)
1936 if (!(cb
= kmalloc(sizeof(struct cmdbuf_t
) + len
, GFP_ATOMIC
))) {
1937 dev_err(cs
->dev
, "%s: out of memory\n", __func__
);
1942 memcpy(cb
->buf
, buf
, len
);
1946 cb
->wake_tasklet
= wake_tasklet
;
1948 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1949 cb
->prev
= cs
->lastcmdbuf
;
1951 cs
->lastcmdbuf
->next
= cb
;
1956 cs
->cmdbytes
+= len
;
1957 cs
->lastcmdbuf
= cb
;
1958 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1960 spin_lock_irqsave(&cs
->lock
, flags
);
1961 if (unlikely(!cs
->connected
)) {
1962 spin_unlock_irqrestore(&cs
->lock
, flags
);
1963 gig_dbg(DEBUG_USBREQ
, "%s: not connected", __func__
);
1964 /* flush command queue */
1965 spin_lock_irqsave(&cs
->cmdlock
, flags
);
1966 while (cs
->cmdbuf
!= NULL
)
1968 spin_unlock_irqrestore(&cs
->cmdlock
, flags
);
1971 rc
= start_cbsend(cs
);
1972 spin_unlock_irqrestore(&cs
->lock
, flags
);
1973 return rc
< 0 ? rc
: len
;
1975 notqueued
: /* request handled without queuing */
1977 tasklet_schedule(wake_tasklet
);
1981 /* gigaset_write_room
1982 * tty_driver.write_room interface routine
1983 * return number of characters the driver will accept to be written via
1986 * controller state structure
1988 * number of characters
1990 static int gigaset_write_room(struct cardstate
*cs
)
1995 /* gigaset_chars_in_buffer
1996 * tty_driver.chars_in_buffer interface routine
1997 * return number of characters waiting to be sent
1999 * controller state structure
2001 * number of characters
2003 static int gigaset_chars_in_buffer(struct cardstate
*cs
)
2005 return cs
->cmdbytes
;
2009 * implementation of ioctl(GIGASET_BRKCHARS)
2011 * controller state structure
2013 * -EINVAL (unimplemented function)
2015 static int gigaset_brkchars(struct cardstate
*cs
, const unsigned char buf
[6])
2021 /* Device Initialization/Shutdown */
2022 /* ============================== */
2024 /* Free hardware dependent part of the B channel structure
2026 * bcs B channel structure
2030 static int gigaset_freebcshw(struct bc_state
*bcs
)
2032 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2038 /* kill URBs and tasklets before freeing - better safe than sorry */
2040 gig_dbg(DEBUG_INIT
, "%s: killing iso URBs", __func__
);
2041 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2042 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2043 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2045 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2046 usb_kill_urb(ubc
->isoinurbs
[i
]);
2047 usb_free_urb(ubc
->isoinurbs
[i
]);
2049 tasklet_kill(&ubc
->sent_tasklet
);
2050 tasklet_kill(&ubc
->rcvd_tasklet
);
2051 kfree(ubc
->isooutbuf
);
2057 /* Initialize hardware dependent part of the B channel structure
2059 * bcs B channel structure
2063 static int gigaset_initbcshw(struct bc_state
*bcs
)
2066 struct bas_bc_state
*ubc
;
2068 bcs
->hw
.bas
= ubc
= kmalloc(sizeof(struct bas_bc_state
), GFP_KERNEL
);
2070 err("could not allocate bas_bc_state");
2075 atomic_set(&ubc
->corrbytes
, 0);
2076 spin_lock_init(&ubc
->isooutlock
);
2077 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2078 ubc
->isoouturbs
[i
].urb
= NULL
;
2079 ubc
->isoouturbs
[i
].bcs
= bcs
;
2081 ubc
->isooutdone
= ubc
->isooutfree
= ubc
->isooutovfl
= NULL
;
2083 if (!(ubc
->isooutbuf
= kmalloc(sizeof(struct isowbuf_t
), GFP_KERNEL
))) {
2084 err("could not allocate isochronous output buffer");
2089 tasklet_init(&ubc
->sent_tasklet
,
2090 &write_iso_tasklet
, (unsigned long) bcs
);
2092 spin_lock_init(&ubc
->isoinlock
);
2093 for (i
= 0; i
< BAS_INURBS
; ++i
)
2094 ubc
->isoinurbs
[i
] = NULL
;
2095 ubc
->isoindone
= NULL
;
2096 ubc
->loststatus
= -EINPROGRESS
;
2110 tasklet_init(&ubc
->rcvd_tasklet
,
2111 &read_iso_tasklet
, (unsigned long) bcs
);
2115 static void gigaset_reinitbcshw(struct bc_state
*bcs
)
2117 struct bas_bc_state
*ubc
= bcs
->hw
.bas
;
2119 bcs
->hw
.bas
->running
= 0;
2120 atomic_set(&bcs
->hw
.bas
->corrbytes
, 0);
2121 bcs
->hw
.bas
->numsub
= 0;
2122 spin_lock_init(&ubc
->isooutlock
);
2123 spin_lock_init(&ubc
->isoinlock
);
2124 ubc
->loststatus
= -EINPROGRESS
;
2127 static void gigaset_freecshw(struct cardstate
*cs
)
2129 /* timers, URBs and rcvbuf are disposed of in disconnect */
2134 static int gigaset_initcshw(struct cardstate
*cs
)
2136 struct bas_cardstate
*ucs
;
2138 cs
->hw
.bas
= ucs
= kmalloc(sizeof *ucs
, GFP_KERNEL
);
2142 ucs
->urb_cmd_in
= NULL
;
2143 ucs
->urb_cmd_out
= NULL
;
2145 ucs
->rcvbuf_size
= 0;
2147 spin_lock_init(&ucs
->lock
);
2151 init_timer(&ucs
->timer_ctrl
);
2152 init_timer(&ucs
->timer_atrdy
);
2153 init_timer(&ucs
->timer_cmd_in
);
2154 init_waitqueue_head(&ucs
->waitqueue
);
2160 * unlink and deallocate all URBs unconditionally
2161 * caller must make sure that no commands are still in progress
2163 * cs controller state structure
2165 static void freeurbs(struct cardstate
*cs
)
2167 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2168 struct bas_bc_state
*ubc
;
2171 gig_dbg(DEBUG_INIT
, "%s: killing URBs", __func__
);
2172 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2173 ubc
= cs
->bcs
[j
].hw
.bas
;
2174 for (i
= 0; i
< BAS_OUTURBS
; ++i
) {
2175 usb_kill_urb(ubc
->isoouturbs
[i
].urb
);
2176 usb_free_urb(ubc
->isoouturbs
[i
].urb
);
2177 ubc
->isoouturbs
[i
].urb
= NULL
;
2179 for (i
= 0; i
< BAS_INURBS
; ++i
) {
2180 usb_kill_urb(ubc
->isoinurbs
[i
]);
2181 usb_free_urb(ubc
->isoinurbs
[i
]);
2182 ubc
->isoinurbs
[i
] = NULL
;
2185 usb_kill_urb(ucs
->urb_int_in
);
2186 usb_free_urb(ucs
->urb_int_in
);
2187 ucs
->urb_int_in
= NULL
;
2188 usb_kill_urb(ucs
->urb_cmd_out
);
2189 usb_free_urb(ucs
->urb_cmd_out
);
2190 ucs
->urb_cmd_out
= NULL
;
2191 usb_kill_urb(ucs
->urb_cmd_in
);
2192 usb_free_urb(ucs
->urb_cmd_in
);
2193 ucs
->urb_cmd_in
= NULL
;
2194 usb_kill_urb(ucs
->urb_ctrl
);
2195 usb_free_urb(ucs
->urb_ctrl
);
2196 ucs
->urb_ctrl
= NULL
;
2200 * This function is called when a new USB device is connected.
2201 * It checks whether the new device is handled by this driver.
2203 static int gigaset_probe(struct usb_interface
*interface
,
2204 const struct usb_device_id
*id
)
2206 struct usb_host_interface
*hostif
;
2207 struct usb_device
*udev
= interface_to_usbdev(interface
);
2208 struct cardstate
*cs
= NULL
;
2209 struct bas_cardstate
*ucs
= NULL
;
2210 struct bas_bc_state
*ubc
;
2211 struct usb_endpoint_descriptor
*endpoint
;
2216 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2217 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2218 le16_to_cpu(udev
->descriptor
.idProduct
));
2220 /* set required alternate setting */
2221 hostif
= interface
->cur_altsetting
;
2222 if (hostif
->desc
.bAlternateSetting
!= 3) {
2224 "%s: wrong alternate setting %d - trying to switch",
2225 __func__
, hostif
->desc
.bAlternateSetting
);
2226 if (usb_set_interface(udev
, hostif
->desc
.bInterfaceNumber
, 3) < 0) {
2227 dev_warn(&udev
->dev
, "usb_set_interface failed, "
2228 "device %d interface %d altsetting %d\n",
2229 udev
->devnum
, hostif
->desc
.bInterfaceNumber
,
2230 hostif
->desc
.bAlternateSetting
);
2233 hostif
= interface
->cur_altsetting
;
2236 /* Reject application specific interfaces
2238 if (hostif
->desc
.bInterfaceClass
!= 255) {
2239 dev_warn(&udev
->dev
, "%s: bInterfaceClass == %d\n",
2240 __func__
, hostif
->desc
.bInterfaceClass
);
2244 dev_info(&udev
->dev
,
2245 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2246 __func__
, le16_to_cpu(udev
->descriptor
.idVendor
),
2247 le16_to_cpu(udev
->descriptor
.idProduct
));
2249 /* allocate memory for our device state and intialize it */
2250 cs
= gigaset_initcs(driver
, BAS_CHANNELS
, 0, 0, cidmode
,
2251 GIGASET_MODULENAME
);
2256 /* save off device structure ptrs for later use */
2259 ucs
->interface
= interface
;
2260 cs
->dev
= &interface
->dev
;
2263 * - one for the interrupt pipe
2264 * - three for the different uses of the default control pipe
2265 * - three for each isochronous pipe
2267 if (!(ucs
->urb_int_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2268 !(ucs
->urb_cmd_in
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2269 !(ucs
->urb_cmd_out
= usb_alloc_urb(0, GFP_KERNEL
)) ||
2270 !(ucs
->urb_ctrl
= usb_alloc_urb(0, GFP_KERNEL
)))
2273 for (j
= 0; j
< BAS_CHANNELS
; ++j
) {
2274 ubc
= cs
->bcs
[j
].hw
.bas
;
2275 for (i
= 0; i
< BAS_OUTURBS
; ++i
)
2276 if (!(ubc
->isoouturbs
[i
].urb
=
2277 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2279 for (i
= 0; i
< BAS_INURBS
; ++i
)
2280 if (!(ubc
->isoinurbs
[i
] =
2281 usb_alloc_urb(BAS_NUMFRAMES
, GFP_KERNEL
)))
2286 ucs
->rcvbuf_size
= 0;
2288 /* Fill the interrupt urb and send it to the core */
2289 endpoint
= &hostif
->endpoint
[0].desc
;
2290 usb_fill_int_urb(ucs
->urb_int_in
, udev
,
2291 usb_rcvintpipe(udev
,
2292 (endpoint
->bEndpointAddress
) & 0x0f),
2293 ucs
->int_in_buf
, 3, read_int_callback
, cs
,
2294 endpoint
->bInterval
);
2295 if ((rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
)) != 0) {
2296 dev_err(cs
->dev
, "could not submit interrupt URB: %s\n",
2301 /* tell the device that the driver is ready */
2302 if ((rc
= req_submit(cs
->bcs
, HD_DEVICE_INIT_ACK
, 0, 0)) != 0)
2305 /* tell common part that the device is ready */
2306 if (startmode
== SM_LOCKED
)
2307 cs
->mstate
= MS_LOCKED
;
2309 /* save address of controller structure */
2310 usb_set_intfdata(interface
, cs
);
2312 if (!gigaset_start(cs
))
2318 dev_err(cs
->dev
, "could not allocate URBs\n");
2321 usb_set_intfdata(interface
, NULL
);
2326 /* gigaset_disconnect
2327 * This function is called when the Gigaset base is unplugged.
2329 static void gigaset_disconnect(struct usb_interface
*interface
)
2331 struct cardstate
*cs
;
2332 struct bas_cardstate
*ucs
;
2335 cs
= usb_get_intfdata(interface
);
2339 dev_info(cs
->dev
, "disconnecting Gigaset base\n");
2341 /* mark base as not ready, all channels disconnected */
2344 /* tell LL all channels are down */
2345 for (j
= 0; j
< BAS_CHANNELS
; ++j
)
2346 gigaset_bchannel_down(cs
->bcs
+ j
);
2348 /* stop driver (common part) */
2351 /* stop timers and URBs, free ressources */
2352 del_timer_sync(&ucs
->timer_ctrl
);
2353 del_timer_sync(&ucs
->timer_atrdy
);
2354 del_timer_sync(&ucs
->timer_cmd_in
);
2356 usb_set_intfdata(interface
, NULL
);
2359 ucs
->rcvbuf_size
= 0;
2360 usb_put_dev(ucs
->udev
);
2361 ucs
->interface
= NULL
;
2368 * This function is called before the USB connection is suspended.
2370 static int gigaset_suspend(struct usb_interface
*intf
, pm_message_t message
)
2372 struct cardstate
*cs
= usb_get_intfdata(intf
);
2373 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2376 /* set suspend flag; this stops AT command/response traffic */
2377 if (update_basstate(ucs
, BS_SUSPEND
, 0) & BS_SUSPEND
) {
2378 gig_dbg(DEBUG_SUSPEND
, "already suspended");
2382 /* wait a bit for blocking conditions to go away */
2383 rc
= wait_event_timeout(ucs
->waitqueue
,
2385 (BS_B1OPEN
|BS_B2OPEN
|BS_ATRDPEND
|BS_ATWRPEND
)),
2387 gig_dbg(DEBUG_SUSPEND
, "wait_event_timeout() -> %d", rc
);
2389 /* check for conditions preventing suspend */
2390 if (ucs
->basstate
& (BS_B1OPEN
|BS_B2OPEN
|BS_ATRDPEND
|BS_ATWRPEND
)) {
2391 dev_warn(cs
->dev
, "cannot suspend:\n");
2392 if (ucs
->basstate
& BS_B1OPEN
)
2393 dev_warn(cs
->dev
, " B channel 1 open\n");
2394 if (ucs
->basstate
& BS_B2OPEN
)
2395 dev_warn(cs
->dev
, " B channel 2 open\n");
2396 if (ucs
->basstate
& BS_ATRDPEND
)
2397 dev_warn(cs
->dev
, " receiving AT reply\n");
2398 if (ucs
->basstate
& BS_ATWRPEND
)
2399 dev_warn(cs
->dev
, " sending AT command\n");
2400 update_basstate(ucs
, 0, BS_SUSPEND
);
2404 /* close AT channel if open */
2405 if (ucs
->basstate
& BS_ATOPEN
) {
2406 gig_dbg(DEBUG_SUSPEND
, "closing AT channel");
2407 rc
= req_submit(cs
->bcs
, HD_CLOSE_ATCHANNEL
, 0, 0);
2409 update_basstate(ucs
, 0, BS_SUSPEND
);
2412 wait_event_timeout(ucs
->waitqueue
, !ucs
->pending
,
2414 /* in case of timeout, proceed anyway */
2417 /* kill all URBs and timers that might still be pending */
2418 usb_kill_urb(ucs
->urb_ctrl
);
2419 usb_kill_urb(ucs
->urb_int_in
);
2420 del_timer_sync(&ucs
->timer_ctrl
);
2422 gig_dbg(DEBUG_SUSPEND
, "suspend complete");
2427 * This function is called after the USB connection has been resumed.
2429 static int gigaset_resume(struct usb_interface
*intf
)
2431 struct cardstate
*cs
= usb_get_intfdata(intf
);
2432 struct bas_cardstate
*ucs
= cs
->hw
.bas
;
2435 /* resubmit interrupt URB for spontaneous messages from base */
2436 rc
= usb_submit_urb(ucs
->urb_int_in
, GFP_KERNEL
);
2438 dev_err(cs
->dev
, "could not resubmit interrupt URB: %s\n",
2443 /* clear suspend flag to reallow activity */
2444 update_basstate(ucs
, 0, BS_SUSPEND
);
2446 gig_dbg(DEBUG_SUSPEND
, "resume complete");
2450 /* gigaset_pre_reset
2451 * This function is called before the USB connection is reset.
2453 static int gigaset_pre_reset(struct usb_interface
*intf
)
2455 /* handle just like suspend */
2456 return gigaset_suspend(intf
, PMSG_ON
);
2459 /* gigaset_post_reset
2460 * This function is called after the USB connection has been reset.
2462 static int gigaset_post_reset(struct usb_interface
*intf
)
2464 /* FIXME: send HD_DEVICE_INIT_ACK? */
2466 /* resume operations */
2467 return gigaset_resume(intf
);
2471 static const struct gigaset_ops gigops
= {
2474 gigaset_chars_in_buffer
,
2476 gigaset_init_bchannel
,
2477 gigaset_close_bchannel
,
2480 gigaset_reinitbcshw
,
2483 gigaset_set_modem_ctrl
,
2485 gigaset_set_line_ctrl
,
2486 gigaset_isoc_send_skb
,
2491 * This function is called after the kernel module is loaded.
2493 static int __init
bas_gigaset_init(void)
2497 /* allocate memory for our driver state and intialize it */
2498 if ((driver
= gigaset_initdriver(GIGASET_MINOR
, GIGASET_MINORS
,
2499 GIGASET_MODULENAME
, GIGASET_DEVNAME
,
2500 &gigops
, THIS_MODULE
)) == NULL
)
2503 /* register this driver with the USB subsystem */
2504 result
= usb_register(&gigaset_usb_driver
);
2506 err("usb_register failed (error %d)", -result
);
2510 info(DRIVER_AUTHOR
);
2516 gigaset_freedriver(driver
);
2522 * This function is called before the kernel module is unloaded.
2524 static void __exit
bas_gigaset_exit(void)
2526 struct bas_cardstate
*ucs
;
2529 gigaset_blockdriver(driver
); /* => probe will fail
2530 * => no gigaset_start any more
2533 /* stop all connected devices */
2534 for (i
= 0; i
< driver
->minors
; i
++) {
2535 if (gigaset_shutdown(driver
->cs
+ i
) < 0)
2536 continue; /* no device */
2537 /* from now on, no isdn callback should be possible */
2539 /* close all still open channels */
2540 ucs
= driver
->cs
[i
].hw
.bas
;
2541 if (ucs
->basstate
& BS_B1OPEN
) {
2542 gig_dbg(DEBUG_INIT
, "closing B1 channel");
2543 usb_control_msg(ucs
->udev
,
2544 usb_sndctrlpipe(ucs
->udev
, 0),
2545 HD_CLOSE_B1CHANNEL
, OUT_VENDOR_REQ
,
2546 0, 0, NULL
, 0, BAS_TIMEOUT
);
2548 if (ucs
->basstate
& BS_B2OPEN
) {
2549 gig_dbg(DEBUG_INIT
, "closing B2 channel");
2550 usb_control_msg(ucs
->udev
,
2551 usb_sndctrlpipe(ucs
->udev
, 0),
2552 HD_CLOSE_B2CHANNEL
, OUT_VENDOR_REQ
,
2553 0, 0, NULL
, 0, BAS_TIMEOUT
);
2555 if (ucs
->basstate
& BS_ATOPEN
) {
2556 gig_dbg(DEBUG_INIT
, "closing AT channel");
2557 usb_control_msg(ucs
->udev
,
2558 usb_sndctrlpipe(ucs
->udev
, 0),
2559 HD_CLOSE_ATCHANNEL
, OUT_VENDOR_REQ
,
2560 0, 0, NULL
, 0, BAS_TIMEOUT
);
2565 /* deregister this driver with the USB subsystem */
2566 usb_deregister(&gigaset_usb_driver
);
2567 /* this will call the disconnect-callback */
2568 /* from now on, no disconnect/probe callback should be running */
2570 gigaset_freedriver(driver
);
2575 module_init(bas_gigaset_init
);
2576 module_exit(bas_gigaset_exit
);
2578 MODULE_AUTHOR(DRIVER_AUTHOR
);
2579 MODULE_DESCRIPTION(DRIVER_DESC
);
2580 MODULE_LICENSE("GPL");