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 /* interrupt pipe message size according to ibid. ch. 2.2 */
52 /* Values for the Gigaset 307x */
53 #define USB_GIGA_VENDOR_ID 0x0681
54 #define USB_3070_PRODUCT_ID 0x0001
55 #define USB_3075_PRODUCT_ID 0x0002
56 #define USB_SX303_PRODUCT_ID 0x0021
57 #define USB_SX353_PRODUCT_ID 0x0022
59 /* table of devices that work with this driver */
60 static const struct usb_device_id gigaset_table[] = {
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
63 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
64 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
65 { } /* Terminating entry */
68 MODULE_DEVICE_TABLE(usb, gigaset_table);
70 /*======================= local function prototypes ==========================*/
72 /* function called if a new device belonging to this driver is connected */
73 static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
76 /* Function will be called if the device is unplugged */
77 static void gigaset_disconnect(struct usb_interface *interface);
79 /* functions called before/after suspend */
80 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
81 static int gigaset_resume(struct usb_interface *intf);
83 /* functions called before/after device reset */
84 static int gigaset_pre_reset(struct usb_interface *intf);
85 static int gigaset_post_reset(struct usb_interface *intf);
87 static int atread_submit(struct cardstate *, int);
88 static void stopurbs(struct bas_bc_state *);
89 static int req_submit(struct bc_state *, int, int, int);
90 static int atwrite_submit(struct cardstate *, unsigned char *, int);
91 static int start_cbsend(struct cardstate *);
93 /*============================================================================*/
95 struct bas_cardstate {
96 struct usb_device *udev; /* USB device pointer */
97 struct usb_interface *interface; /* interface for this device */
98 unsigned char minor; /* starting minor number */
100 struct urb *urb_ctrl; /* control pipe default URB */
101 struct usb_ctrlrequest dr_ctrl;
102 struct timer_list timer_ctrl; /* control request timeout */
105 struct timer_list timer_atrdy; /* AT command ready timeout */
106 struct urb *urb_cmd_out; /* for sending AT commands */
107 struct usb_ctrlrequest dr_cmd_out;
110 struct urb *urb_cmd_in; /* for receiving AT replies */
111 struct usb_ctrlrequest dr_cmd_in;
112 struct timer_list timer_cmd_in; /* receive request timeout */
113 unsigned char *rcvbuf; /* AT reply receive buffer */
115 struct urb *urb_int_in; /* URB for interrupt pipe */
116 unsigned char *int_in_buf;
118 spinlock_t lock; /* locks all following */
119 int basstate; /* bitmap (BS_*) */
120 int pending; /* uncompleted base request */
121 wait_queue_head_t waitqueue;
122 int rcvbuf_size; /* size of AT receive buffer */
123 /* 0: no receive in progress */
124 int retry_cmd_in; /* receive req retry count */
127 /* status of direct USB connection to 307x base (bits in basstate) */
128 #define BS_ATOPEN 0x001 /* AT channel open */
129 #define BS_B1OPEN 0x002 /* B channel 1 open */
130 #define BS_B2OPEN 0x004 /* B channel 2 open */
131 #define BS_ATREADY 0x008 /* base ready for AT command */
132 #define BS_INIT 0x010 /* base has signalled INIT_OK */
133 #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
134 #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
135 #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
136 #define BS_SUSPEND 0x100 /* USB port suspended */
137 #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
140 static struct gigaset_driver *driver;
142 /* usb specific object needed to register this driver with the usb subsystem */
143 static struct usb_driver gigaset_usb_driver = {
144 .name = GIGASET_MODULENAME,
145 .probe = gigaset_probe,
146 .disconnect = gigaset_disconnect,
147 .id_table = gigaset_table,
148 .suspend = gigaset_suspend,
149 .resume = gigaset_resume,
150 .reset_resume = gigaset_post_reset,
151 .pre_reset = gigaset_pre_reset,
152 .post_reset = gigaset_post_reset,
155 /* get message text for usb_submit_urb return code
157 static char *get_usb_rcmsg(int rc)
159 static char unkmsg[28];
165 return "out of memory";
167 return "device not present";
169 return "endpoint not present";
171 return "URB type not supported";
173 return "invalid argument";
175 return "start frame too early or too much scheduled";
177 return "too many isochronous frames requested";
179 return "endpoint stalled";
181 return "invalid packet size";
183 return "would overcommit USB bandwidth";
185 return "device shut down";
187 return "reject flag set";
189 return "device suspended";
191 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
196 /* get message text for USB status code
198 static char *get_usb_statmsg(int status)
200 static char unkmsg[28];
206 return "unlinked (sync)";
210 return "bit stuffing error, timeout, or unknown USB error";
212 return "CRC mismatch, timeout, or unknown USB error";
216 return "endpoint stalled";
218 return "IN buffer overrun";
220 return "OUT buffer underrun";
222 return "too much data";
224 return "short packet detected";
226 return "device removed";
228 return "partial isochronous transfer";
230 return "invalid argument";
232 return "unlinked (async)";
234 return "device shut down";
236 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
242 * retrieve string representation of USB pipe type
244 static inline char *usb_pipetype_str(int pipe)
246 if (usb_pipeisoc(pipe))
248 if (usb_pipeint(pipe))
250 if (usb_pipecontrol(pipe))
252 if (usb_pipebulk(pipe))
258 * write content of URB to syslog for debugging
260 static inline void dump_urb(enum debuglevel level, const char *tag,
263 #ifdef CONFIG_GIGASET_DEBUG
265 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
270 (unsigned long) urb->dev,
271 usb_pipetype_str(urb->pipe),
272 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
273 usb_pipein(urb->pipe) ? "in" : "out",
274 (unsigned long) urb->hcpriv,
275 urb->transfer_flags);
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
278 "setup_packet=0x%08lx,",
279 (unsigned long) urb->transfer_buffer,
280 urb->transfer_buffer_length, urb->actual_length,
281 (unsigned long) urb->setup_packet);
283 " start_frame=%d, number_of_packets=%d, interval=%d, "
285 urb->start_frame, urb->number_of_packets, urb->interval,
288 " context=0x%08lx, complete=0x%08lx, "
289 "iso_frame_desc[]={",
290 (unsigned long) urb->context,
291 (unsigned long) urb->complete);
292 for (i = 0; i < urb->number_of_packets; i++) {
293 struct usb_iso_packet_descriptor *pifd
294 = &urb->iso_frame_desc[i];
296 " {offset=%u, length=%u, actual_length=%u, "
298 pifd->offset, pifd->length, pifd->actual_length,
302 gig_dbg(level, "}}");
306 /* read/set modem control bits etc. (m10x only) */
307 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
313 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
318 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
323 /* set/clear bits in base connection state, return previous state
325 static inline int update_basstate(struct bas_cardstate *ucs,
331 spin_lock_irqsave(&ucs->lock, flags);
332 state = ucs->basstate;
333 ucs->basstate = (state & ~clear) | set;
334 spin_unlock_irqrestore(&ucs->lock, flags);
339 * hang up any existing connection because of an unrecoverable error
340 * This function may be called from any context and takes care of scheduling
341 * the necessary actions for execution outside of interrupt context.
342 * cs->lock must not be held.
344 * B channel control structure
346 static inline void error_hangup(struct bc_state *bcs)
348 struct cardstate *cs = bcs->cs;
350 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
351 gigaset_schedule_event(cs);
355 * reset Gigaset device because of an unrecoverable error
356 * This function may be called from any context, and takes care of
357 * scheduling the necessary actions for execution outside of interrupt context.
358 * cs->lock must not be held.
360 * controller state structure
362 static inline void error_reset(struct cardstate *cs)
364 /* reset interrupt pipe to recover (ignore errors) */
365 update_basstate(cs->hw.bas, BS_RESETTING, 0);
366 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
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) {
447 dev_notice(cs->dev, "control read: timeout, retry %d\n",
449 rc = atread_submit(cs, BAS_TIMEOUT);
450 if (rc >= 0 || rc == -ENODEV)
451 /* resubmitted or disconnected */
452 /* - bypass regular exit block */
456 "control read: timeout, giving up after %d tries\n",
461 ucs->rcvbuf_size = 0;
465 /* read_ctrl_callback
466 * USB completion handler for control pipe input
467 * called by the USB subsystem in interrupt context
469 * urb USB request block
470 * urb->context = inbuf structure for controller state
472 static void read_ctrl_callback(struct urb *urb)
474 struct inbuf_t *inbuf = urb->context;
475 struct cardstate *cs = inbuf->cs;
476 struct bas_cardstate *ucs = cs->hw.bas;
477 int status = urb->status;
482 update_basstate(ucs, 0, BS_ATRDPEND);
483 wake_up(&ucs->waitqueue);
485 if (!ucs->rcvbuf_size) {
486 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
490 del_timer(&ucs->timer_cmd_in);
493 case 0: /* normal completion */
494 numbytes = urb->actual_length;
495 if (unlikely(numbytes != ucs->rcvbuf_size)) {
497 "control read: received %d chars, expected %d\n",
498 numbytes, ucs->rcvbuf_size);
499 if (numbytes > ucs->rcvbuf_size)
500 numbytes = ucs->rcvbuf_size;
503 /* copy received bytes to inbuf */
504 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
506 if (unlikely(numbytes < ucs->rcvbuf_size)) {
507 /* incomplete - resubmit for remaining bytes */
508 ucs->rcvbuf_size -= numbytes;
509 ucs->retry_cmd_in = 0;
510 rc = atread_submit(cs, BAS_TIMEOUT);
511 if (rc >= 0 || rc == -ENODEV)
512 /* resubmitted or disconnected */
513 /* - bypass regular exit block */
519 case -ENOENT: /* cancelled */
520 case -ECONNRESET: /* cancelled (async) */
521 case -EINPROGRESS: /* pending */
522 case -ENODEV: /* device removed */
523 case -ESHUTDOWN: /* device shut down */
524 /* no action necessary */
525 gig_dbg(DEBUG_USBREQ, "%s: %s",
526 __func__, get_usb_statmsg(status));
529 default: /* severe trouble */
530 dev_warn(cs->dev, "control read: %s\n",
531 get_usb_statmsg(status));
532 if (ucs->retry_cmd_in++ < BAS_RETRY) {
533 dev_notice(cs->dev, "control read: retry %d\n",
535 rc = atread_submit(cs, BAS_TIMEOUT);
536 if (rc >= 0 || rc == -ENODEV)
537 /* resubmitted or disconnected */
538 /* - bypass regular exit block */
542 "control read: giving up after %d tries\n",
550 ucs->rcvbuf_size = 0;
552 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
553 gigaset_schedule_event(cs);
558 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
560 * cs controller state structure
561 * timeout timeout in 1/10 sec., 0: none
564 * -EBUSY if another request is pending
565 * any URB submission error code
567 static int atread_submit(struct cardstate *cs, int timeout)
569 struct bas_cardstate *ucs = cs->hw.bas;
573 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
576 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
577 if (basstate & BS_ATRDPEND) {
579 "could not submit HD_READ_ATMESSAGE: URB busy\n");
583 if (basstate & BS_SUSPEND) {
585 "HD_READ_ATMESSAGE not submitted, "
586 "suspend in progress\n");
587 update_basstate(ucs, 0, BS_ATRDPEND);
588 /* treat like disconnect */
592 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
593 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
594 ucs->dr_cmd_in.wValue = 0;
595 ucs->dr_cmd_in.wIndex = 0;
596 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
597 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
598 usb_rcvctrlpipe(ucs->udev, 0),
599 (unsigned char *) &ucs->dr_cmd_in,
600 ucs->rcvbuf, ucs->rcvbuf_size,
601 read_ctrl_callback, cs->inbuf);
603 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
605 update_basstate(ucs, 0, BS_ATRDPEND);
606 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
612 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
613 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
614 ucs->timer_cmd_in.data = (unsigned long) cs;
615 ucs->timer_cmd_in.function = cmd_in_timeout;
616 add_timer(&ucs->timer_cmd_in);
622 * USB completion handler for interrupt pipe input
623 * called by the USB subsystem in interrupt context
625 * urb USB request block
626 * urb->context = controller state structure
628 static void read_int_callback(struct urb *urb)
630 struct cardstate *cs = urb->context;
631 struct bas_cardstate *ucs = cs->hw.bas;
632 struct bc_state *bcs;
633 int status = urb->status;
640 case 0: /* success */
642 case -ENOENT: /* cancelled */
643 case -ECONNRESET: /* cancelled (async) */
644 case -EINPROGRESS: /* pending */
645 /* ignore silently */
646 gig_dbg(DEBUG_USBREQ, "%s: %s",
647 __func__, get_usb_statmsg(status));
649 case -ENODEV: /* device removed */
650 case -ESHUTDOWN: /* device shut down */
651 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
653 default: /* severe trouble */
654 dev_warn(cs->dev, "interrupt read: %s\n",
655 get_usb_statmsg(status));
659 /* drop incomplete packets even if the missing bytes wouldn't matter */
660 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
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 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
740 if (ucs->rcvbuf == NULL) {
741 spin_unlock_irqrestore(&cs->lock, flags);
742 dev_err(cs->dev, "out of memory receiving AT data\n");
746 ucs->rcvbuf_size = l;
747 ucs->retry_cmd_in = 0;
748 rc = atread_submit(cs, BAS_TIMEOUT);
752 ucs->rcvbuf_size = 0;
754 spin_unlock_irqrestore(&cs->lock, flags);
759 spin_unlock_irqrestore(&cs->lock, flags);
762 case HD_RESET_INTERRUPT_PIPE_ACK:
763 update_basstate(ucs, 0, BS_RESETTING);
764 dev_notice(cs->dev, "interrupt pipe reset\n");
768 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
773 "unknown Gigaset signal 0x%02x (%u) ignored\n",
774 (int) ucs->int_in_buf[0], l);
778 wake_up(&ucs->waitqueue);
781 rc = usb_submit_urb(urb, GFP_ATOMIC);
782 if (unlikely(rc != 0 && rc != -ENODEV)) {
783 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
790 * USB completion handler for B channel isochronous input
791 * called by the USB subsystem in interrupt context
793 * urb USB request block of completed request
794 * urb->context = bc_state structure
796 static void read_iso_callback(struct urb *urb)
798 struct bc_state *bcs;
799 struct bas_bc_state *ubc;
800 int status = urb->status;
804 /* status codes not worth bothering the tasklet with */
805 if (unlikely(status == -ENOENT ||
806 status == -ECONNRESET ||
807 status == -EINPROGRESS ||
809 status == -ESHUTDOWN)) {
810 gig_dbg(DEBUG_ISO, "%s: %s",
811 __func__, get_usb_statmsg(status));
818 spin_lock_irqsave(&ubc->isoinlock, flags);
819 if (likely(ubc->isoindone == NULL)) {
820 /* pass URB to tasklet */
821 ubc->isoindone = urb;
822 ubc->isoinstatus = status;
823 tasklet_hi_schedule(&ubc->rcvd_tasklet);
825 /* tasklet still busy, drop data and resubmit URB */
826 ubc->loststatus = status;
827 for (i = 0; i < BAS_NUMFRAMES; i++) {
828 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
829 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
830 urb->iso_frame_desc[i].status !=
832 ubc->loststatus = urb->iso_frame_desc[i].status;
833 urb->iso_frame_desc[i].status = 0;
834 urb->iso_frame_desc[i].actual_length = 0;
836 if (likely(ubc->running)) {
837 /* urb->dev is clobbered by USB subsystem */
838 urb->dev = bcs->cs->hw.bas->udev;
839 urb->transfer_flags = URB_ISO_ASAP;
840 urb->number_of_packets = BAS_NUMFRAMES;
841 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
843 rc = usb_submit_urb(urb, GFP_ATOMIC);
844 if (unlikely(rc != 0 && rc != -ENODEV)) {
845 dev_err(bcs->cs->dev,
846 "could not resubmit isochronous read "
847 "URB: %s\n", get_usb_rcmsg(rc));
848 dump_urb(DEBUG_ISO, "isoc read", urb);
853 spin_unlock_irqrestore(&ubc->isoinlock, flags);
856 /* write_iso_callback
857 * USB completion handler for B channel isochronous output
858 * called by the USB subsystem in interrupt context
860 * urb USB request block of completed request
861 * urb->context = isow_urbctx_t structure
863 static void write_iso_callback(struct urb *urb)
865 struct isow_urbctx_t *ucx;
866 struct bas_bc_state *ubc;
867 int status = urb->status;
870 /* status codes not worth bothering the tasklet with */
871 if (unlikely(status == -ENOENT ||
872 status == -ECONNRESET ||
873 status == -EINPROGRESS ||
875 status == -ESHUTDOWN)) {
876 gig_dbg(DEBUG_ISO, "%s: %s",
877 __func__, get_usb_statmsg(status));
881 /* pass URB context to tasklet */
883 ubc = ucx->bcs->hw.bas;
884 ucx->status = status;
886 spin_lock_irqsave(&ubc->isooutlock, flags);
887 ubc->isooutovfl = ubc->isooutdone;
888 ubc->isooutdone = ucx;
889 spin_unlock_irqrestore(&ubc->isooutlock, flags);
890 tasklet_hi_schedule(&ubc->sent_tasklet);
894 * prepare and submit USB request blocks for isochronous input and output
896 * B channel control structure
899 * < 0 on error (no URBs submitted)
901 static int starturbs(struct bc_state *bcs)
903 struct bas_bc_state *ubc = bcs->hw.bas;
908 /* initialize L2 reception */
909 if (bcs->proto2 == L2_HDLC)
910 bcs->inputstate |= INS_flag_hunt;
912 /* submit all isochronous input URBs */
914 for (k = 0; k < BAS_INURBS; k++) {
915 urb = ubc->isoinurbs[k];
921 urb->dev = bcs->cs->hw.bas->udev;
922 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
923 urb->transfer_flags = URB_ISO_ASAP;
924 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
925 urb->transfer_buffer_length = BAS_INBUFSIZE;
926 urb->number_of_packets = BAS_NUMFRAMES;
927 urb->interval = BAS_FRAMETIME;
928 urb->complete = read_iso_callback;
930 for (j = 0; j < BAS_NUMFRAMES; j++) {
931 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
932 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
933 urb->iso_frame_desc[j].status = 0;
934 urb->iso_frame_desc[j].actual_length = 0;
937 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
938 rc = usb_submit_urb(urb, GFP_ATOMIC);
943 /* initialize L2 transmission */
944 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
946 /* set up isochronous output URBs for flag idling */
947 for (k = 0; k < BAS_OUTURBS; ++k) {
948 urb = ubc->isoouturbs[k].urb;
953 urb->dev = bcs->cs->hw.bas->udev;
954 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
955 urb->transfer_flags = URB_ISO_ASAP;
956 urb->transfer_buffer = ubc->isooutbuf->data;
957 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
958 urb->number_of_packets = BAS_NUMFRAMES;
959 urb->interval = BAS_FRAMETIME;
960 urb->complete = write_iso_callback;
961 urb->context = &ubc->isoouturbs[k];
962 for (j = 0; j < BAS_NUMFRAMES; ++j) {
963 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
964 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
965 urb->iso_frame_desc[j].status = 0;
966 urb->iso_frame_desc[j].actual_length = 0;
968 ubc->isoouturbs[k].limit = -1;
971 /* keep one URB free, submit the others */
972 for (k = 0; k < BAS_OUTURBS-1; ++k) {
973 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
974 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
978 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
979 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
980 ubc->isooutdone = ubc->isooutovfl = NULL;
988 * cancel the USB request blocks for isochronous input and output
989 * errors are silently ignored
991 * B channel control structure
993 static void stopurbs(struct bas_bc_state *ubc)
999 for (k = 0; k < BAS_INURBS; ++k) {
1000 rc = usb_unlink_urb(ubc->isoinurbs[k]);
1002 "%s: isoc input URB %d unlinked, result = %s",
1003 __func__, k, get_usb_rcmsg(rc));
1006 for (k = 0; k < BAS_OUTURBS; ++k) {
1007 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1009 "%s: isoc output URB %d unlinked, result = %s",
1010 __func__, k, get_usb_rcmsg(rc));
1014 /* Isochronous Write - Bottom Half */
1015 /* =============================== */
1017 /* submit_iso_write_urb
1018 * fill and submit the next isochronous write URB
1020 * ucx context structure containing URB
1022 * number of frames submitted in URB
1023 * 0 if URB not submitted because no data available (isooutbuf busy)
1024 * error code < 0 on error
1026 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1028 struct urb *urb = ucx->urb;
1029 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1030 struct usb_iso_packet_descriptor *ifd;
1031 int corrbytes, nframe, rc;
1033 /* urb->dev is clobbered by USB subsystem */
1034 urb->dev = ucx->bcs->cs->hw.bas->udev;
1035 urb->transfer_flags = URB_ISO_ASAP;
1036 urb->transfer_buffer = ubc->isooutbuf->data;
1037 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1039 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1040 ifd = &urb->iso_frame_desc[nframe];
1042 /* compute frame length according to flow control */
1043 ifd->length = BAS_NORMFRAME;
1044 corrbytes = atomic_read(&ubc->corrbytes);
1045 if (corrbytes != 0) {
1046 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1047 __func__, corrbytes);
1048 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1049 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1050 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1051 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1052 ifd->length += corrbytes;
1053 atomic_add(-corrbytes, &ubc->corrbytes);
1056 /* retrieve block of data to send */
1057 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1061 "%s: buffer busy at frame %d",
1063 /* tasklet will be restarted from
1064 gigaset_isoc_send_skb() */
1066 dev_err(ucx->bcs->cs->dev,
1067 "%s: buffer error %d at frame %d\n",
1068 __func__, rc, nframe);
1074 ucx->limit = ubc->isooutbuf->nextread;
1076 ifd->actual_length = 0;
1078 if (unlikely(nframe == 0))
1079 return 0; /* no data to send */
1080 urb->number_of_packets = nframe;
1082 rc = usb_submit_urb(urb, GFP_ATOMIC);
1085 /* device removed - give up silently */
1086 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1088 dev_err(ucx->bcs->cs->dev,
1089 "could not submit isochronous write URB: %s\n",
1097 /* write_iso_tasklet
1098 * tasklet scheduled when an isochronous output URB from the Gigaset device
1101 * data B channel state structure
1103 static void write_iso_tasklet(unsigned long data)
1105 struct bc_state *bcs = (struct bc_state *) data;
1106 struct bas_bc_state *ubc = bcs->hw.bas;
1107 struct cardstate *cs = bcs->cs;
1108 struct isow_urbctx_t *done, *next, *ovfl;
1111 struct usb_iso_packet_descriptor *ifd;
1113 unsigned long flags;
1115 struct sk_buff *skb;
1119 /* loop while completed URBs arrive in time */
1121 if (unlikely(!(ubc->running))) {
1122 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1126 /* retrieve completed URBs */
1127 spin_lock_irqsave(&ubc->isooutlock, flags);
1128 done = ubc->isooutdone;
1129 ubc->isooutdone = NULL;
1130 ovfl = ubc->isooutovfl;
1131 ubc->isooutovfl = NULL;
1132 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1134 dev_err(cs->dev, "isochronous write buffer underrun\n");
1141 /* submit free URB if available */
1142 spin_lock_irqsave(&ubc->isooutlock, flags);
1143 next = ubc->isooutfree;
1144 ubc->isooutfree = NULL;
1145 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1147 rc = submit_iso_write_urb(next);
1148 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1149 /* could not submit URB, put it back */
1150 spin_lock_irqsave(&ubc->isooutlock, flags);
1151 if (ubc->isooutfree == NULL) {
1152 ubc->isooutfree = next;
1155 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1157 /* couldn't put it back */
1159 "losing isochronous write URB\n");
1165 /* process completed URB */
1167 status = done->status;
1169 case -EXDEV: /* partial completion */
1170 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1172 /* fall through - what's the difference anyway? */
1173 case 0: /* normal completion */
1174 /* inspect individual frames
1175 * assumptions (for lack of documentation):
1176 * - actual_length bytes of first frame in error are
1178 * - all following frames are not sent at all
1180 offset = done->limit; /* default (no error) */
1181 for (i = 0; i < BAS_NUMFRAMES; i++) {
1182 ifd = &urb->iso_frame_desc[i];
1184 ifd->actual_length != ifd->length) {
1186 "isochronous write: frame %d: %s, "
1187 "only %d of %d bytes sent\n",
1188 i, get_usb_statmsg(ifd->status),
1189 ifd->actual_length, ifd->length);
1190 offset = (ifd->offset +
1196 #ifdef CONFIG_GIGASET_DEBUG
1197 /* check assumption on remaining frames */
1198 for (; i < BAS_NUMFRAMES; i++) {
1199 ifd = &urb->iso_frame_desc[i];
1200 if (ifd->status != -EINPROGRESS
1201 || ifd->actual_length != 0) {
1203 "isochronous write: frame %d: %s, "
1204 "%d of %d bytes sent\n",
1205 i, get_usb_statmsg(ifd->status),
1206 ifd->actual_length, ifd->length);
1207 offset = (ifd->offset +
1215 case -EPIPE: /* stall - probably underrun */
1216 dev_err(cs->dev, "isochronous write stalled\n");
1219 default: /* severe trouble */
1220 dev_warn(cs->dev, "isochronous write: %s\n",
1221 get_usb_statmsg(status));
1224 /* mark the write buffer area covered by this URB as free */
1225 if (done->limit >= 0)
1226 ubc->isooutbuf->read = done->limit;
1228 /* mark URB as free */
1229 spin_lock_irqsave(&ubc->isooutlock, flags);
1230 next = ubc->isooutfree;
1231 ubc->isooutfree = done;
1232 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1234 /* only one URB still active - resubmit one */
1235 rc = submit_iso_write_urb(next);
1236 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1237 /* couldn't submit */
1243 /* process queued SKBs */
1244 while ((skb = skb_dequeue(&bcs->squeue))) {
1245 /* copy to output buffer, doing L2 encapsulation */
1247 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1248 /* insufficient buffer space, push back onto queue */
1249 skb_queue_head(&bcs->squeue, skb);
1250 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1251 __func__, skb_queue_len(&bcs->squeue));
1255 gigaset_skb_sent(bcs, skb);
1256 dev_kfree_skb_any(skb);
1260 /* Isochronous Read - Bottom Half */
1261 /* ============================== */
1264 * tasklet scheduled when an isochronous input URB from the Gigaset device
1267 * data B channel state structure
1269 static void read_iso_tasklet(unsigned long data)
1271 struct bc_state *bcs = (struct bc_state *) data;
1272 struct bas_bc_state *ubc = bcs->hw.bas;
1273 struct cardstate *cs = bcs->cs;
1277 unsigned long flags;
1278 int totleft, numbytes, offset, frame, rc;
1280 /* loop while more completed URBs arrive in the meantime */
1283 spin_lock_irqsave(&ubc->isoinlock, flags);
1284 urb = ubc->isoindone;
1286 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1289 status = ubc->isoinstatus;
1290 ubc->isoindone = NULL;
1291 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1293 "isochronous read overrun, "
1294 "dropped URB with status: %s, %d bytes lost\n",
1295 get_usb_statmsg(ubc->loststatus),
1297 ubc->loststatus = -EINPROGRESS;
1299 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1301 if (unlikely(!(ubc->running))) {
1303 "%s: channel not running, "
1304 "dropped URB with status: %s",
1305 __func__, get_usb_statmsg(status));
1310 case 0: /* normal completion */
1312 case -EXDEV: /* inspect individual frames
1313 (we do that anyway) */
1314 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1320 gig_dbg(DEBUG_ISO, "%s: %s",
1321 __func__, get_usb_statmsg(status));
1322 continue; /* -> skip */
1324 dev_err(cs->dev, "isochronous read stalled\n");
1326 continue; /* -> skip */
1327 default: /* severe trouble */
1328 dev_warn(cs->dev, "isochronous read: %s\n",
1329 get_usb_statmsg(status));
1333 rcvbuf = urb->transfer_buffer;
1334 totleft = urb->actual_length;
1335 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1336 numbytes = urb->iso_frame_desc[frame].actual_length;
1337 if (unlikely(urb->iso_frame_desc[frame].status))
1339 "isochronous read: frame %d[%d]: %s\n",
1342 urb->iso_frame_desc[frame].status));
1343 if (unlikely(numbytes > BAS_MAXFRAME))
1345 "isochronous read: frame %d: "
1346 "numbytes (%d) > BAS_MAXFRAME\n",
1348 if (unlikely(numbytes > totleft)) {
1350 "isochronous read: frame %d: "
1351 "numbytes (%d) > totleft (%d)\n",
1352 frame, numbytes, totleft);
1355 offset = urb->iso_frame_desc[frame].offset;
1356 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1358 "isochronous read: frame %d: "
1359 "offset (%d) + numbytes (%d) "
1360 "> BAS_INBUFSIZE\n",
1361 frame, offset, numbytes);
1362 numbytes = BAS_INBUFSIZE - offset;
1364 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1365 totleft -= numbytes;
1367 if (unlikely(totleft > 0))
1369 "isochronous read: %d data bytes missing\n",
1373 /* URB processed, resubmit */
1374 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1375 urb->iso_frame_desc[frame].status = 0;
1376 urb->iso_frame_desc[frame].actual_length = 0;
1378 /* urb->dev is clobbered by USB subsystem */
1379 urb->dev = bcs->cs->hw.bas->udev;
1380 urb->transfer_flags = URB_ISO_ASAP;
1381 urb->number_of_packets = BAS_NUMFRAMES;
1382 rc = usb_submit_urb(urb, GFP_ATOMIC);
1383 if (unlikely(rc != 0 && rc != -ENODEV)) {
1385 "could not resubmit isochronous read URB: %s\n",
1387 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1393 /* Channel Operations */
1394 /* ================== */
1397 * timeout routine for control output request
1399 * B channel control structure
1401 static void req_timeout(unsigned long data)
1403 struct bc_state *bcs = (struct bc_state *) data;
1404 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1406 unsigned long flags;
1410 spin_lock_irqsave(&ucs->lock, flags);
1411 pending = ucs->pending;
1413 spin_unlock_irqrestore(&ucs->lock, flags);
1416 case 0: /* no pending request */
1417 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1420 case HD_OPEN_ATCHANNEL:
1421 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1422 error_reset(bcs->cs);
1425 case HD_OPEN_B2CHANNEL:
1426 case HD_OPEN_B1CHANNEL:
1427 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1432 case HD_CLOSE_ATCHANNEL:
1433 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1434 error_reset(bcs->cs);
1437 case HD_CLOSE_B2CHANNEL:
1438 case HD_CLOSE_B1CHANNEL:
1439 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1441 error_reset(bcs->cs);
1444 case HD_RESET_INTERRUPT_PIPE:
1445 /* error recovery escalation */
1446 dev_err(bcs->cs->dev,
1447 "reset interrupt pipe timeout, attempting USB reset\n");
1448 usb_queue_reset_device(bcs->cs->hw.bas->interface);
1452 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1456 wake_up(&ucs->waitqueue);
1459 /* write_ctrl_callback
1460 * USB completion handler for control pipe output
1461 * called by the USB subsystem in interrupt context
1463 * urb USB request block of completed request
1464 * urb->context = hardware specific controller state structure
1466 static void write_ctrl_callback(struct urb *urb)
1468 struct bas_cardstate *ucs = urb->context;
1469 int status = urb->status;
1471 unsigned long flags;
1475 case 0: /* normal completion */
1476 spin_lock_irqsave(&ucs->lock, flags);
1477 switch (ucs->pending) {
1478 case HD_DEVICE_INIT_ACK: /* no reply expected */
1479 del_timer(&ucs->timer_ctrl);
1483 spin_unlock_irqrestore(&ucs->lock, flags);
1486 case -ENOENT: /* cancelled */
1487 case -ECONNRESET: /* cancelled (async) */
1488 case -EINPROGRESS: /* pending */
1489 case -ENODEV: /* device removed */
1490 case -ESHUTDOWN: /* device shut down */
1491 /* ignore silently */
1492 gig_dbg(DEBUG_USBREQ, "%s: %s",
1493 __func__, get_usb_statmsg(status));
1496 default: /* any failure */
1497 /* don't retry if suspend requested */
1498 if (++ucs->retry_ctrl > BAS_RETRY ||
1499 (ucs->basstate & BS_SUSPEND)) {
1500 dev_err(&ucs->interface->dev,
1501 "control request 0x%02x failed: %s\n",
1502 ucs->dr_ctrl.bRequest,
1503 get_usb_statmsg(status));
1504 break; /* give up */
1506 dev_notice(&ucs->interface->dev,
1507 "control request 0x%02x: %s, retry %d\n",
1508 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1510 /* urb->dev is clobbered by USB subsystem */
1511 urb->dev = ucs->udev;
1512 rc = usb_submit_urb(urb, GFP_ATOMIC);
1514 dev_err(&ucs->interface->dev,
1515 "could not resubmit request 0x%02x: %s\n",
1516 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1523 /* failed, clear pending request */
1524 spin_lock_irqsave(&ucs->lock, flags);
1525 del_timer(&ucs->timer_ctrl);
1527 spin_unlock_irqrestore(&ucs->lock, flags);
1528 wake_up(&ucs->waitqueue);
1532 * submit a control output request without message buffer to the Gigaset base
1533 * and optionally start a timeout
1535 * bcs B channel control structure
1536 * req control request code (HD_*)
1537 * val control request parameter value (set to 0 if unused)
1538 * timeout timeout in seconds (0: no timeout)
1541 * -EBUSY if another request is pending
1542 * any URB submission error code
1544 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1546 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1548 unsigned long flags;
1550 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1552 spin_lock_irqsave(&ucs->lock, flags);
1554 spin_unlock_irqrestore(&ucs->lock, flags);
1555 dev_err(bcs->cs->dev,
1556 "submission of request 0x%02x failed: "
1557 "request 0x%02x still pending\n",
1562 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1563 ucs->dr_ctrl.bRequest = req;
1564 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1565 ucs->dr_ctrl.wIndex = 0;
1566 ucs->dr_ctrl.wLength = 0;
1567 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1568 usb_sndctrlpipe(ucs->udev, 0),
1569 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
1570 write_ctrl_callback, ucs);
1571 ucs->retry_ctrl = 0;
1572 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1573 if (unlikely(ret)) {
1574 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1575 req, get_usb_rcmsg(ret));
1576 spin_unlock_irqrestore(&ucs->lock, flags);
1582 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1583 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1584 ucs->timer_ctrl.data = (unsigned long) bcs;
1585 ucs->timer_ctrl.function = req_timeout;
1586 add_timer(&ucs->timer_ctrl);
1589 spin_unlock_irqrestore(&ucs->lock, flags);
1593 /* gigaset_init_bchannel
1594 * called by common.c to connect a B channel
1595 * initialize isochronous I/O and tell the Gigaset base to open the channel
1597 * B channel control structure
1599 * 0 on success, error code < 0 on error
1601 static int gigaset_init_bchannel(struct bc_state *bcs)
1603 struct cardstate *cs = bcs->cs;
1605 unsigned long flags;
1607 spin_lock_irqsave(&cs->lock, flags);
1608 if (unlikely(!cs->connected)) {
1609 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1610 spin_unlock_irqrestore(&cs->lock, flags);
1614 if (cs->hw.bas->basstate & BS_SUSPEND) {
1616 "not starting isochronous I/O, "
1617 "suspend in progress\n");
1618 spin_unlock_irqrestore(&cs->lock, flags);
1619 return -EHOSTUNREACH;
1622 ret = starturbs(bcs);
1625 "could not start isochronous I/O for channel B%d: %s\n",
1627 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1630 spin_unlock_irqrestore(&cs->lock, flags);
1634 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1635 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1637 dev_err(cs->dev, "could not open channel B%d\n",
1639 stopurbs(bcs->hw.bas);
1644 spin_unlock_irqrestore(&cs->lock, flags);
1648 /* gigaset_close_bchannel
1649 * called by common.c to disconnect a B channel
1650 * tell the Gigaset base to close the channel
1651 * stopping isochronous I/O and LL notification will be done when the
1652 * acknowledgement for the close arrives
1654 * B channel control structure
1656 * 0 on success, error code < 0 on error
1658 static int gigaset_close_bchannel(struct bc_state *bcs)
1660 struct cardstate *cs = bcs->cs;
1662 unsigned long flags;
1664 spin_lock_irqsave(&cs->lock, flags);
1665 if (unlikely(!cs->connected)) {
1666 spin_unlock_irqrestore(&cs->lock, flags);
1667 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1671 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1672 /* channel not running: just signal common.c */
1673 spin_unlock_irqrestore(&cs->lock, flags);
1674 gigaset_bchannel_down(bcs);
1678 /* channel running: tell device to close it */
1679 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1680 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1682 dev_err(cs->dev, "closing channel B%d failed\n",
1685 spin_unlock_irqrestore(&cs->lock, flags);
1689 /* Device Operations */
1690 /* ================= */
1693 * unqueue first command buffer from queue, waking any sleepers
1694 * must be called with cs->cmdlock held
1696 * cs controller state structure
1698 static void complete_cb(struct cardstate *cs)
1700 struct cmdbuf_t *cb = cs->cmdbuf;
1702 /* unqueue completed buffer */
1703 cs->cmdbytes -= cs->curlen;
1704 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
1705 cs->curlen, cs->cmdbytes);
1706 if (cb->next != NULL) {
1707 cs->cmdbuf = cb->next;
1708 cs->cmdbuf->prev = NULL;
1709 cs->curlen = cs->cmdbuf->len;
1712 cs->lastcmdbuf = NULL;
1716 if (cb->wake_tasklet)
1717 tasklet_schedule(cb->wake_tasklet);
1722 /* write_command_callback
1723 * USB completion handler for AT command transmission
1724 * called by the USB subsystem in interrupt context
1726 * urb USB request block of completed request
1727 * urb->context = controller state structure
1729 static void write_command_callback(struct urb *urb)
1731 struct cardstate *cs = urb->context;
1732 struct bas_cardstate *ucs = cs->hw.bas;
1733 int status = urb->status;
1734 unsigned long flags;
1736 update_basstate(ucs, 0, BS_ATWRPEND);
1737 wake_up(&ucs->waitqueue);
1741 case 0: /* normal completion */
1743 case -ENOENT: /* cancelled */
1744 case -ECONNRESET: /* cancelled (async) */
1745 case -EINPROGRESS: /* pending */
1746 case -ENODEV: /* device removed */
1747 case -ESHUTDOWN: /* device shut down */
1748 /* ignore silently */
1749 gig_dbg(DEBUG_USBREQ, "%s: %s",
1750 __func__, get_usb_statmsg(status));
1752 default: /* any failure */
1753 if (++ucs->retry_cmd_out > BAS_RETRY) {
1755 "command write: %s, "
1756 "giving up after %d retries\n",
1757 get_usb_statmsg(status),
1758 ucs->retry_cmd_out);
1761 if (ucs->basstate & BS_SUSPEND) {
1763 "command write: %s, "
1764 "won't retry - suspend requested\n",
1765 get_usb_statmsg(status));
1768 if (cs->cmdbuf == NULL) {
1770 "command write: %s, "
1771 "cannot retry - cmdbuf gone\n",
1772 get_usb_statmsg(status));
1775 dev_notice(cs->dev, "command write: %s, retry %d\n",
1776 get_usb_statmsg(status), ucs->retry_cmd_out);
1777 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1778 /* resubmitted - bypass regular exit block */
1780 /* command send failed, assume base still waiting */
1781 update_basstate(ucs, BS_ATREADY, 0);
1784 spin_lock_irqsave(&cs->cmdlock, flags);
1785 if (cs->cmdbuf != NULL)
1787 spin_unlock_irqrestore(&cs->cmdlock, flags);
1791 * timeout routine for AT command transmission
1793 * controller state structure
1795 static void atrdy_timeout(unsigned long data)
1797 struct cardstate *cs = (struct cardstate *) data;
1798 struct bas_cardstate *ucs = cs->hw.bas;
1800 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1802 /* fake the missing signal - what else can I do? */
1803 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1808 * submit an HD_WRITE_ATMESSAGE command URB
1810 * cs controller state structure
1811 * buf buffer containing command to send
1812 * len length of command to send
1815 * -EBUSY if another request is pending
1816 * any URB submission error code
1818 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1820 struct bas_cardstate *ucs = cs->hw.bas;
1823 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1825 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1827 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1831 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1832 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1833 ucs->dr_cmd_out.wValue = 0;
1834 ucs->dr_cmd_out.wIndex = 0;
1835 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1836 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1837 usb_sndctrlpipe(ucs->udev, 0),
1838 (unsigned char *) &ucs->dr_cmd_out, buf, len,
1839 write_command_callback, cs);
1840 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1842 update_basstate(ucs, 0, BS_ATWRPEND);
1843 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1848 /* submitted successfully, start timeout if necessary */
1849 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1850 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1852 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1853 ucs->timer_atrdy.data = (unsigned long) cs;
1854 ucs->timer_atrdy.function = atrdy_timeout;
1855 add_timer(&ucs->timer_atrdy);
1861 * start transmission of AT command queue if necessary
1863 * cs controller state structure
1866 * error code < 0 on error
1868 static int start_cbsend(struct cardstate *cs)
1870 struct cmdbuf_t *cb;
1871 struct bas_cardstate *ucs = cs->hw.bas;
1872 unsigned long flags;
1876 /* check if suspend requested */
1877 if (ucs->basstate & BS_SUSPEND) {
1878 gig_dbg(DEBUG_OUTPUT, "suspending");
1879 return -EHOSTUNREACH;
1882 /* check if AT channel is open */
1883 if (!(ucs->basstate & BS_ATOPEN)) {
1884 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
1885 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1887 /* flush command queue */
1888 spin_lock_irqsave(&cs->cmdlock, flags);
1889 while (cs->cmdbuf != NULL)
1891 spin_unlock_irqrestore(&cs->cmdlock, flags);
1896 /* try to send first command in queue */
1897 spin_lock_irqsave(&cs->cmdlock, flags);
1899 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1900 ucs->retry_cmd_out = 0;
1901 rc = atwrite_submit(cs, cb->buf, cb->len);
1908 spin_unlock_irqrestore(&cs->cmdlock, flags);
1912 /* gigaset_write_cmd
1913 * This function is called by the device independent part of the driver
1914 * to transmit an AT command string to the Gigaset device.
1915 * It encapsulates the device specific method for transmission over the
1916 * direct USB connection to the base.
1917 * The command string is added to the queue of commands to send, and
1918 * USB transmission is started if necessary.
1920 * cs controller state structure
1921 * buf command string to send
1922 * len number of bytes to send (max. IF_WRITEBUF)
1923 * wake_tasklet tasklet to run when transmission is completed
1926 * number of bytes queued on success
1927 * error code < 0 on error
1929 static int gigaset_write_cmd(struct cardstate *cs,
1930 const unsigned char *buf, int len,
1931 struct tasklet_struct *wake_tasklet)
1933 struct cmdbuf_t *cb;
1934 unsigned long flags;
1937 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1938 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1939 "CMD Transmit", len, buf);
1947 /* translate "+++" escape sequence sent as a single separate command
1948 * into "close AT channel" command for error recovery
1949 * The next command will reopen the AT channel automatically.
1951 if (len == 3 && !memcmp(buf, "+++", 3)) {
1952 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
1956 if (len > IF_WRITEBUF)
1958 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
1960 dev_err(cs->dev, "%s: out of memory\n", __func__);
1965 memcpy(cb->buf, buf, len);
1969 cb->wake_tasklet = wake_tasklet;
1971 spin_lock_irqsave(&cs->cmdlock, flags);
1972 cb->prev = cs->lastcmdbuf;
1974 cs->lastcmdbuf->next = cb;
1979 cs->cmdbytes += len;
1980 cs->lastcmdbuf = cb;
1981 spin_unlock_irqrestore(&cs->cmdlock, flags);
1983 spin_lock_irqsave(&cs->lock, flags);
1984 if (unlikely(!cs->connected)) {
1985 spin_unlock_irqrestore(&cs->lock, flags);
1986 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1987 /* flush command queue */
1988 spin_lock_irqsave(&cs->cmdlock, flags);
1989 while (cs->cmdbuf != NULL)
1991 spin_unlock_irqrestore(&cs->cmdlock, flags);
1994 rc = start_cbsend(cs);
1995 spin_unlock_irqrestore(&cs->lock, flags);
1996 return rc < 0 ? rc : len;
1998 notqueued: /* request handled without queuing */
2000 tasklet_schedule(wake_tasklet);
2004 /* gigaset_write_room
2005 * tty_driver.write_room interface routine
2006 * return number of characters the driver will accept to be written via
2009 * controller state structure
2011 * number of characters
2013 static int gigaset_write_room(struct cardstate *cs)
2018 /* gigaset_chars_in_buffer
2019 * tty_driver.chars_in_buffer interface routine
2020 * return number of characters waiting to be sent
2022 * controller state structure
2024 * number of characters
2026 static int gigaset_chars_in_buffer(struct cardstate *cs)
2028 return cs->cmdbytes;
2032 * implementation of ioctl(GIGASET_BRKCHARS)
2034 * controller state structure
2036 * -EINVAL (unimplemented function)
2038 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2044 /* Device Initialization/Shutdown */
2045 /* ============================== */
2047 /* Free hardware dependent part of the B channel structure
2049 * bcs B channel structure
2053 static int gigaset_freebcshw(struct bc_state *bcs)
2055 struct bas_bc_state *ubc = bcs->hw.bas;
2061 /* kill URBs and tasklets before freeing - better safe than sorry */
2063 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2064 for (i = 0; i < BAS_OUTURBS; ++i) {
2065 usb_kill_urb(ubc->isoouturbs[i].urb);
2066 usb_free_urb(ubc->isoouturbs[i].urb);
2068 for (i = 0; i < BAS_INURBS; ++i) {
2069 usb_kill_urb(ubc->isoinurbs[i]);
2070 usb_free_urb(ubc->isoinurbs[i]);
2072 tasklet_kill(&ubc->sent_tasklet);
2073 tasklet_kill(&ubc->rcvd_tasklet);
2074 kfree(ubc->isooutbuf);
2080 /* Initialize hardware dependent part of the B channel structure
2082 * bcs B channel structure
2086 static int gigaset_initbcshw(struct bc_state *bcs)
2089 struct bas_bc_state *ubc;
2091 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2093 pr_err("out of memory\n");
2098 atomic_set(&ubc->corrbytes, 0);
2099 spin_lock_init(&ubc->isooutlock);
2100 for (i = 0; i < BAS_OUTURBS; ++i) {
2101 ubc->isoouturbs[i].urb = NULL;
2102 ubc->isoouturbs[i].bcs = bcs;
2104 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2106 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2107 if (!ubc->isooutbuf) {
2108 pr_err("out of memory\n");
2113 tasklet_init(&ubc->sent_tasklet,
2114 write_iso_tasklet, (unsigned long) bcs);
2116 spin_lock_init(&ubc->isoinlock);
2117 for (i = 0; i < BAS_INURBS; ++i)
2118 ubc->isoinurbs[i] = NULL;
2119 ubc->isoindone = NULL;
2120 ubc->loststatus = -EINPROGRESS;
2134 tasklet_init(&ubc->rcvd_tasklet,
2135 read_iso_tasklet, (unsigned long) bcs);
2139 static void gigaset_reinitbcshw(struct bc_state *bcs)
2141 struct bas_bc_state *ubc = bcs->hw.bas;
2143 bcs->hw.bas->running = 0;
2144 atomic_set(&bcs->hw.bas->corrbytes, 0);
2145 bcs->hw.bas->numsub = 0;
2146 spin_lock_init(&ubc->isooutlock);
2147 spin_lock_init(&ubc->isoinlock);
2148 ubc->loststatus = -EINPROGRESS;
2151 static void gigaset_freecshw(struct cardstate *cs)
2153 /* timers, URBs and rcvbuf are disposed of in disconnect */
2154 kfree(cs->hw.bas->int_in_buf);
2159 static int gigaset_initcshw(struct cardstate *cs)
2161 struct bas_cardstate *ucs;
2163 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2165 pr_err("out of memory\n");
2168 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2169 if (!ucs->int_in_buf) {
2171 pr_err("out of memory\n");
2175 ucs->urb_cmd_in = NULL;
2176 ucs->urb_cmd_out = NULL;
2178 ucs->rcvbuf_size = 0;
2180 spin_lock_init(&ucs->lock);
2184 init_timer(&ucs->timer_ctrl);
2185 init_timer(&ucs->timer_atrdy);
2186 init_timer(&ucs->timer_cmd_in);
2187 init_waitqueue_head(&ucs->waitqueue);
2193 * unlink and deallocate all URBs unconditionally
2194 * caller must make sure that no commands are still in progress
2196 * cs controller state structure
2198 static void freeurbs(struct cardstate *cs)
2200 struct bas_cardstate *ucs = cs->hw.bas;
2201 struct bas_bc_state *ubc;
2204 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
2205 for (j = 0; j < BAS_CHANNELS; ++j) {
2206 ubc = cs->bcs[j].hw.bas;
2207 for (i = 0; i < BAS_OUTURBS; ++i) {
2208 usb_kill_urb(ubc->isoouturbs[i].urb);
2209 usb_free_urb(ubc->isoouturbs[i].urb);
2210 ubc->isoouturbs[i].urb = NULL;
2212 for (i = 0; i < BAS_INURBS; ++i) {
2213 usb_kill_urb(ubc->isoinurbs[i]);
2214 usb_free_urb(ubc->isoinurbs[i]);
2215 ubc->isoinurbs[i] = NULL;
2218 usb_kill_urb(ucs->urb_int_in);
2219 usb_free_urb(ucs->urb_int_in);
2220 ucs->urb_int_in = NULL;
2221 usb_kill_urb(ucs->urb_cmd_out);
2222 usb_free_urb(ucs->urb_cmd_out);
2223 ucs->urb_cmd_out = NULL;
2224 usb_kill_urb(ucs->urb_cmd_in);
2225 usb_free_urb(ucs->urb_cmd_in);
2226 ucs->urb_cmd_in = NULL;
2227 usb_kill_urb(ucs->urb_ctrl);
2228 usb_free_urb(ucs->urb_ctrl);
2229 ucs->urb_ctrl = NULL;
2233 * This function is called when a new USB device is connected.
2234 * It checks whether the new device is handled by this driver.
2236 static int gigaset_probe(struct usb_interface *interface,
2237 const struct usb_device_id *id)
2239 struct usb_host_interface *hostif;
2240 struct usb_device *udev = interface_to_usbdev(interface);
2241 struct cardstate *cs = NULL;
2242 struct bas_cardstate *ucs = NULL;
2243 struct bas_bc_state *ubc;
2244 struct usb_endpoint_descriptor *endpoint;
2249 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2250 __func__, le16_to_cpu(udev->descriptor.idVendor),
2251 le16_to_cpu(udev->descriptor.idProduct));
2253 /* set required alternate setting */
2254 hostif = interface->cur_altsetting;
2255 if (hostif->desc.bAlternateSetting != 3) {
2257 "%s: wrong alternate setting %d - trying to switch",
2258 __func__, hostif->desc.bAlternateSetting);
2259 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2261 dev_warn(&udev->dev, "usb_set_interface failed, "
2262 "device %d interface %d altsetting %d\n",
2263 udev->devnum, hostif->desc.bInterfaceNumber,
2264 hostif->desc.bAlternateSetting);
2267 hostif = interface->cur_altsetting;
2270 /* Reject application specific interfaces
2272 if (hostif->desc.bInterfaceClass != 255) {
2273 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2274 __func__, hostif->desc.bInterfaceClass);
2278 dev_info(&udev->dev,
2279 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2280 __func__, le16_to_cpu(udev->descriptor.idVendor),
2281 le16_to_cpu(udev->descriptor.idProduct));
2283 /* allocate memory for our device state and intialize it */
2284 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2285 GIGASET_MODULENAME);
2290 /* save off device structure ptrs for later use */
2293 ucs->interface = interface;
2294 cs->dev = &interface->dev;
2297 * - one for the interrupt pipe
2298 * - three for the different uses of the default control pipe
2299 * - three for each isochronous pipe
2301 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2302 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2303 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2304 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2307 for (j = 0; j < BAS_CHANNELS; ++j) {
2308 ubc = cs->bcs[j].hw.bas;
2309 for (i = 0; i < BAS_OUTURBS; ++i)
2310 if (!(ubc->isoouturbs[i].urb =
2311 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2313 for (i = 0; i < BAS_INURBS; ++i)
2314 if (!(ubc->isoinurbs[i] =
2315 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2320 ucs->rcvbuf_size = 0;
2322 /* Fill the interrupt urb and send it to the core */
2323 endpoint = &hostif->endpoint[0].desc;
2324 usb_fill_int_urb(ucs->urb_int_in, udev,
2325 usb_rcvintpipe(udev,
2326 (endpoint->bEndpointAddress) & 0x0f),
2327 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
2328 endpoint->bInterval);
2329 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2331 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2336 /* tell the device that the driver is ready */
2337 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2341 /* tell common part that the device is ready */
2342 if (startmode == SM_LOCKED)
2343 cs->mstate = MS_LOCKED;
2345 /* save address of controller structure */
2346 usb_set_intfdata(interface, cs);
2348 if (!gigaset_start(cs))
2354 dev_err(cs->dev, "could not allocate URBs\n");
2357 usb_set_intfdata(interface, NULL);
2362 /* gigaset_disconnect
2363 * This function is called when the Gigaset base is unplugged.
2365 static void gigaset_disconnect(struct usb_interface *interface)
2367 struct cardstate *cs;
2368 struct bas_cardstate *ucs;
2371 cs = usb_get_intfdata(interface);
2375 dev_info(cs->dev, "disconnecting Gigaset base\n");
2377 /* mark base as not ready, all channels disconnected */
2380 /* tell LL all channels are down */
2381 for (j = 0; j < BAS_CHANNELS; ++j)
2382 gigaset_bchannel_down(cs->bcs + j);
2384 /* stop driver (common part) */
2387 /* stop timers and URBs, free ressources */
2388 del_timer_sync(&ucs->timer_ctrl);
2389 del_timer_sync(&ucs->timer_atrdy);
2390 del_timer_sync(&ucs->timer_cmd_in);
2392 usb_set_intfdata(interface, NULL);
2395 ucs->rcvbuf_size = 0;
2396 usb_put_dev(ucs->udev);
2397 ucs->interface = NULL;
2404 * This function is called before the USB connection is suspended.
2406 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2408 struct cardstate *cs = usb_get_intfdata(intf);
2409 struct bas_cardstate *ucs = cs->hw.bas;
2412 /* set suspend flag; this stops AT command/response traffic */
2413 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2414 gig_dbg(DEBUG_SUSPEND, "already suspended");
2418 /* wait a bit for blocking conditions to go away */
2419 rc = wait_event_timeout(ucs->waitqueue,
2421 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2423 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2425 /* check for conditions preventing suspend */
2426 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
2427 dev_warn(cs->dev, "cannot suspend:\n");
2428 if (ucs->basstate & BS_B1OPEN)
2429 dev_warn(cs->dev, " B channel 1 open\n");
2430 if (ucs->basstate & BS_B2OPEN)
2431 dev_warn(cs->dev, " B channel 2 open\n");
2432 if (ucs->basstate & BS_ATRDPEND)
2433 dev_warn(cs->dev, " receiving AT reply\n");
2434 if (ucs->basstate & BS_ATWRPEND)
2435 dev_warn(cs->dev, " sending AT command\n");
2436 update_basstate(ucs, 0, BS_SUSPEND);
2440 /* close AT channel if open */
2441 if (ucs->basstate & BS_ATOPEN) {
2442 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2443 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2445 update_basstate(ucs, 0, BS_SUSPEND);
2448 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2450 /* in case of timeout, proceed anyway */
2453 /* kill all URBs and timers that might still be pending */
2454 usb_kill_urb(ucs->urb_ctrl);
2455 usb_kill_urb(ucs->urb_int_in);
2456 del_timer_sync(&ucs->timer_ctrl);
2458 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2463 * This function is called after the USB connection has been resumed.
2465 static int gigaset_resume(struct usb_interface *intf)
2467 struct cardstate *cs = usb_get_intfdata(intf);
2468 struct bas_cardstate *ucs = cs->hw.bas;
2471 /* resubmit interrupt URB for spontaneous messages from base */
2472 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2474 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2479 /* clear suspend flag to reallow activity */
2480 update_basstate(ucs, 0, BS_SUSPEND);
2482 gig_dbg(DEBUG_SUSPEND, "resume complete");
2486 /* gigaset_pre_reset
2487 * This function is called before the USB connection is reset.
2489 static int gigaset_pre_reset(struct usb_interface *intf)
2491 /* handle just like suspend */
2492 return gigaset_suspend(intf, PMSG_ON);
2495 /* gigaset_post_reset
2496 * This function is called after the USB connection has been reset.
2498 static int gigaset_post_reset(struct usb_interface *intf)
2500 /* FIXME: send HD_DEVICE_INIT_ACK? */
2502 /* resume operations */
2503 return gigaset_resume(intf);
2507 static const struct gigaset_ops gigops = {
2510 gigaset_chars_in_buffer,
2512 gigaset_init_bchannel,
2513 gigaset_close_bchannel,
2516 gigaset_reinitbcshw,
2519 gigaset_set_modem_ctrl,
2521 gigaset_set_line_ctrl,
2522 gigaset_isoc_send_skb,
2527 * This function is called after the kernel module is loaded.
2529 static int __init bas_gigaset_init(void)
2533 /* allocate memory for our driver state and intialize it */
2534 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2535 GIGASET_MODULENAME, GIGASET_DEVNAME,
2536 &gigops, THIS_MODULE);
2540 /* register this driver with the USB subsystem */
2541 result = usb_register(&gigaset_usb_driver);
2543 pr_err("error %d registering USB driver\n", -result);
2547 pr_info(DRIVER_DESC "\n");
2552 gigaset_freedriver(driver);
2558 * This function is called before the kernel module is unloaded.
2560 static void __exit bas_gigaset_exit(void)
2562 struct bas_cardstate *ucs;
2565 gigaset_blockdriver(driver); /* => probe will fail
2566 * => no gigaset_start any more
2569 /* stop all connected devices */
2570 for (i = 0; i < driver->minors; i++) {
2571 if (gigaset_shutdown(driver->cs + i) < 0)
2572 continue; /* no device */
2573 /* from now on, no isdn callback should be possible */
2575 /* close all still open channels */
2576 ucs = driver->cs[i].hw.bas;
2577 if (ucs->basstate & BS_B1OPEN) {
2578 gig_dbg(DEBUG_INIT, "closing B1 channel");
2579 usb_control_msg(ucs->udev,
2580 usb_sndctrlpipe(ucs->udev, 0),
2581 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2582 0, 0, NULL, 0, BAS_TIMEOUT);
2584 if (ucs->basstate & BS_B2OPEN) {
2585 gig_dbg(DEBUG_INIT, "closing B2 channel");
2586 usb_control_msg(ucs->udev,
2587 usb_sndctrlpipe(ucs->udev, 0),
2588 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2589 0, 0, NULL, 0, BAS_TIMEOUT);
2591 if (ucs->basstate & BS_ATOPEN) {
2592 gig_dbg(DEBUG_INIT, "closing AT channel");
2593 usb_control_msg(ucs->udev,
2594 usb_sndctrlpipe(ucs->udev, 0),
2595 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2596 0, 0, NULL, 0, BAS_TIMEOUT);
2601 /* deregister this driver with the USB subsystem */
2602 usb_deregister(&gigaset_usb_driver);
2603 /* this will call the disconnect-callback */
2604 /* from now on, no disconnect/probe callback should be running */
2606 gigaset_freedriver(driver);
2611 module_init(bas_gigaset_init);
2612 module_exit(bas_gigaset_exit);
2614 MODULE_AUTHOR(DRIVER_AUTHOR);
2615 MODULE_DESCRIPTION(DRIVER_DESC);
2616 MODULE_LICENSE("GPL");