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 = NULL;
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 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
351 __func__, bcs->channel);
353 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
354 dev_err(cs->dev, "event queue full\n");
356 gigaset_schedule_event(cs);
360 * reset Gigaset device because of an unrecoverable error
361 * This function may be called from any context, and takes care of
362 * scheduling the necessary actions for execution outside of interrupt context.
363 * cs->lock must not be held.
365 * controller state structure
367 static inline void error_reset(struct cardstate *cs)
369 /* reset interrupt pipe to recover (ignore errors) */
370 update_basstate(cs->hw.bas, BS_RESETTING, 0);
371 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
375 * check for completion of pending control request
377 * ucs hardware specific controller state structure
379 static void check_pending(struct bas_cardstate *ucs)
383 spin_lock_irqsave(&ucs->lock, flags);
384 switch (ucs->pending) {
387 case HD_OPEN_ATCHANNEL:
388 if (ucs->basstate & BS_ATOPEN)
391 case HD_OPEN_B1CHANNEL:
392 if (ucs->basstate & BS_B1OPEN)
395 case HD_OPEN_B2CHANNEL:
396 if (ucs->basstate & BS_B2OPEN)
399 case HD_CLOSE_ATCHANNEL:
400 if (!(ucs->basstate & BS_ATOPEN))
403 case HD_CLOSE_B1CHANNEL:
404 if (!(ucs->basstate & BS_B1OPEN))
407 case HD_CLOSE_B2CHANNEL:
408 if (!(ucs->basstate & BS_B2OPEN))
411 case HD_DEVICE_INIT_ACK: /* no reply expected */
414 case HD_RESET_INTERRUPT_PIPE:
415 if (!(ucs->basstate & BS_RESETTING))
419 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
420 * and should never end up here
423 dev_warn(&ucs->interface->dev,
424 "unknown pending request 0x%02x cleared\n",
430 del_timer(&ucs->timer_ctrl);
432 spin_unlock_irqrestore(&ucs->lock, flags);
436 * timeout routine for command input request
438 * controller state structure
440 static void cmd_in_timeout(unsigned long data)
442 struct cardstate *cs = (struct cardstate *) data;
443 struct bas_cardstate *ucs = cs->hw.bas;
446 if (!ucs->rcvbuf_size) {
447 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
451 if (ucs->retry_cmd_in++ < BAS_RETRY) {
452 dev_notice(cs->dev, "control read: timeout, retry %d\n",
454 rc = atread_submit(cs, BAS_TIMEOUT);
455 if (rc >= 0 || rc == -ENODEV)
456 /* resubmitted or disconnected */
457 /* - bypass regular exit block */
461 "control read: timeout, giving up after %d tries\n",
466 ucs->rcvbuf_size = 0;
470 /* read_ctrl_callback
471 * USB completion handler for control pipe input
472 * called by the USB subsystem in interrupt context
474 * urb USB request block
475 * urb->context = inbuf structure for controller state
477 static void read_ctrl_callback(struct urb *urb)
479 struct inbuf_t *inbuf = urb->context;
480 struct cardstate *cs = inbuf->cs;
481 struct bas_cardstate *ucs = cs->hw.bas;
482 int status = urb->status;
487 update_basstate(ucs, 0, BS_ATRDPEND);
488 wake_up(&ucs->waitqueue);
490 if (!ucs->rcvbuf_size) {
491 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
495 del_timer(&ucs->timer_cmd_in);
498 case 0: /* normal completion */
499 numbytes = urb->actual_length;
500 if (unlikely(numbytes != ucs->rcvbuf_size)) {
502 "control read: received %d chars, expected %d\n",
503 numbytes, ucs->rcvbuf_size);
504 if (numbytes > ucs->rcvbuf_size)
505 numbytes = ucs->rcvbuf_size;
508 /* copy received bytes to inbuf */
509 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
511 if (unlikely(numbytes < ucs->rcvbuf_size)) {
512 /* incomplete - resubmit for remaining bytes */
513 ucs->rcvbuf_size -= numbytes;
514 ucs->retry_cmd_in = 0;
515 rc = atread_submit(cs, BAS_TIMEOUT);
516 if (rc >= 0 || rc == -ENODEV)
517 /* resubmitted or disconnected */
518 /* - bypass regular exit block */
524 case -ENOENT: /* cancelled */
525 case -ECONNRESET: /* cancelled (async) */
526 case -EINPROGRESS: /* pending */
527 case -ENODEV: /* device removed */
528 case -ESHUTDOWN: /* device shut down */
529 /* no action necessary */
530 gig_dbg(DEBUG_USBREQ, "%s: %s",
531 __func__, get_usb_statmsg(status));
534 default: /* severe trouble */
535 dev_warn(cs->dev, "control read: %s\n",
536 get_usb_statmsg(status));
537 if (ucs->retry_cmd_in++ < BAS_RETRY) {
538 dev_notice(cs->dev, "control read: retry %d\n",
540 rc = atread_submit(cs, BAS_TIMEOUT);
541 if (rc >= 0 || rc == -ENODEV)
542 /* resubmitted or disconnected */
543 /* - bypass regular exit block */
547 "control read: giving up after %d tries\n",
555 ucs->rcvbuf_size = 0;
557 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
558 gigaset_schedule_event(cs);
563 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
565 * cs controller state structure
566 * timeout timeout in 1/10 sec., 0: none
569 * -EBUSY if another request is pending
570 * any URB submission error code
572 static int atread_submit(struct cardstate *cs, int timeout)
574 struct bas_cardstate *ucs = cs->hw.bas;
578 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
581 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
582 if (basstate & BS_ATRDPEND) {
584 "could not submit HD_READ_ATMESSAGE: URB busy\n");
588 if (basstate & BS_SUSPEND) {
590 "HD_READ_ATMESSAGE not submitted, "
591 "suspend in progress\n");
592 update_basstate(ucs, 0, BS_ATRDPEND);
593 /* treat like disconnect */
597 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
598 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
599 ucs->dr_cmd_in.wValue = 0;
600 ucs->dr_cmd_in.wIndex = 0;
601 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
602 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
603 usb_rcvctrlpipe(ucs->udev, 0),
604 (unsigned char*) & ucs->dr_cmd_in,
605 ucs->rcvbuf, ucs->rcvbuf_size,
606 read_ctrl_callback, cs->inbuf);
608 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
609 update_basstate(ucs, 0, BS_ATRDPEND);
610 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
616 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
617 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
618 ucs->timer_cmd_in.data = (unsigned long) cs;
619 ucs->timer_cmd_in.function = cmd_in_timeout;
620 add_timer(&ucs->timer_cmd_in);
626 * USB completion handler for interrupt pipe input
627 * called by the USB subsystem in interrupt context
629 * urb USB request block
630 * urb->context = controller state structure
632 static void read_int_callback(struct urb *urb)
634 struct cardstate *cs = urb->context;
635 struct bas_cardstate *ucs = cs->hw.bas;
636 struct bc_state *bcs;
637 int status = urb->status;
644 case 0: /* success */
646 case -ENOENT: /* cancelled */
647 case -ECONNRESET: /* cancelled (async) */
648 case -EINPROGRESS: /* pending */
649 /* ignore silently */
650 gig_dbg(DEBUG_USBREQ, "%s: %s",
651 __func__, get_usb_statmsg(status));
653 case -ENODEV: /* device removed */
654 case -ESHUTDOWN: /* device shut down */
655 //FIXME use this as disconnect indicator?
656 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
658 default: /* severe trouble */
659 dev_warn(cs->dev, "interrupt read: %s\n",
660 get_usb_statmsg(status));
661 //FIXME corrective action? resubmission always ok?
665 /* drop incomplete packets even if the missing bytes wouldn't matter */
666 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
667 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
672 l = (unsigned) ucs->int_in_buf[1] +
673 (((unsigned) ucs->int_in_buf[2]) << 8);
675 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
676 urb->actual_length, (int)ucs->int_in_buf[0], l,
677 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
681 switch (ucs->int_in_buf[0]) {
682 case HD_DEVICE_INIT_OK:
683 update_basstate(ucs, BS_INIT, 0);
686 case HD_READY_SEND_ATDATA:
687 del_timer(&ucs->timer_atrdy);
688 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
692 case HD_OPEN_B2CHANNEL_ACK:
694 case HD_OPEN_B1CHANNEL_ACK:
695 bcs = cs->bcs + channel;
696 update_basstate(ucs, BS_B1OPEN << channel, 0);
697 gigaset_bchannel_up(bcs);
700 case HD_OPEN_ATCHANNEL_ACK:
701 update_basstate(ucs, BS_ATOPEN, 0);
705 case HD_CLOSE_B2CHANNEL_ACK:
707 case HD_CLOSE_B1CHANNEL_ACK:
708 bcs = cs->bcs + channel;
709 update_basstate(ucs, 0, BS_B1OPEN << channel);
710 stopurbs(bcs->hw.bas);
711 gigaset_bchannel_down(bcs);
714 case HD_CLOSE_ATCHANNEL_ACK:
715 update_basstate(ucs, 0, BS_ATOPEN);
718 case HD_B2_FLOW_CONTROL:
720 case HD_B1_FLOW_CONTROL:
721 bcs = cs->bcs + channel;
722 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
723 &bcs->hw.bas->corrbytes);
725 "Flow control (channel %d, sub %d): 0x%02x => %d",
726 channel, bcs->hw.bas->numsub, l,
727 atomic_read(&bcs->hw.bas->corrbytes));
730 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
733 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
736 spin_lock_irqsave(&cs->lock, flags);
737 if (ucs->rcvbuf_size) {
738 /* throw away previous buffer - we have no queue */
740 "receive AT data overrun, %d bytes lost\n",
743 ucs->rcvbuf_size = 0;
745 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
746 spin_unlock_irqrestore(&cs->lock, flags);
747 dev_err(cs->dev, "out of memory receiving AT data\n");
751 ucs->rcvbuf_size = l;
752 ucs->retry_cmd_in = 0;
753 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
756 ucs->rcvbuf_size = 0;
758 //FIXME corrective action?
759 spin_unlock_irqrestore(&cs->lock, flags);
764 spin_unlock_irqrestore(&cs->lock, flags);
767 case HD_RESET_INTERRUPT_PIPE_ACK:
768 update_basstate(ucs, 0, BS_RESETTING);
769 dev_notice(cs->dev, "interrupt pipe reset\n");
773 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
778 "unknown Gigaset signal 0x%02x (%u) ignored\n",
779 (int) ucs->int_in_buf[0], l);
783 wake_up(&ucs->waitqueue);
786 rc = usb_submit_urb(urb, GFP_ATOMIC);
787 if (unlikely(rc != 0 && rc != -ENODEV)) {
788 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
795 * USB completion handler for B channel isochronous input
796 * called by the USB subsystem in interrupt context
798 * urb USB request block of completed request
799 * urb->context = bc_state structure
801 static void read_iso_callback(struct urb *urb)
803 struct bc_state *bcs;
804 struct bas_bc_state *ubc;
805 int status = urb->status;
809 /* status codes not worth bothering the tasklet with */
810 if (unlikely(status == -ENOENT ||
811 status == -ECONNRESET ||
812 status == -EINPROGRESS ||
814 status == -ESHUTDOWN)) {
815 gig_dbg(DEBUG_ISO, "%s: %s",
816 __func__, get_usb_statmsg(status));
823 spin_lock_irqsave(&ubc->isoinlock, flags);
824 if (likely(ubc->isoindone == NULL)) {
825 /* pass URB to tasklet */
826 ubc->isoindone = urb;
827 ubc->isoinstatus = status;
828 tasklet_hi_schedule(&ubc->rcvd_tasklet);
830 /* tasklet still busy, drop data and resubmit URB */
831 ubc->loststatus = status;
832 for (i = 0; i < BAS_NUMFRAMES; i++) {
833 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
834 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
835 urb->iso_frame_desc[i].status !=
837 ubc->loststatus = urb->iso_frame_desc[i].status;
838 urb->iso_frame_desc[i].status = 0;
839 urb->iso_frame_desc[i].actual_length = 0;
841 if (likely(ubc->running)) {
842 /* urb->dev is clobbered by USB subsystem */
843 urb->dev = bcs->cs->hw.bas->udev;
844 urb->transfer_flags = URB_ISO_ASAP;
845 urb->number_of_packets = BAS_NUMFRAMES;
846 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
848 rc = usb_submit_urb(urb, GFP_ATOMIC);
849 if (unlikely(rc != 0 && rc != -ENODEV)) {
850 dev_err(bcs->cs->dev,
851 "could not resubmit isochronous read "
852 "URB: %s\n", get_usb_rcmsg(rc));
853 dump_urb(DEBUG_ISO, "isoc read", urb);
858 spin_unlock_irqrestore(&ubc->isoinlock, flags);
861 /* write_iso_callback
862 * USB completion handler for B channel isochronous output
863 * called by the USB subsystem in interrupt context
865 * urb USB request block of completed request
866 * urb->context = isow_urbctx_t structure
868 static void write_iso_callback(struct urb *urb)
870 struct isow_urbctx_t *ucx;
871 struct bas_bc_state *ubc;
872 int status = urb->status;
875 /* status codes not worth bothering the tasklet with */
876 if (unlikely(status == -ENOENT ||
877 status == -ECONNRESET ||
878 status == -EINPROGRESS ||
880 status == -ESHUTDOWN)) {
881 gig_dbg(DEBUG_ISO, "%s: %s",
882 __func__, get_usb_statmsg(status));
886 /* pass URB context to tasklet */
888 ubc = ucx->bcs->hw.bas;
889 ucx->status = status;
891 spin_lock_irqsave(&ubc->isooutlock, flags);
892 ubc->isooutovfl = ubc->isooutdone;
893 ubc->isooutdone = ucx;
894 spin_unlock_irqrestore(&ubc->isooutlock, flags);
895 tasklet_hi_schedule(&ubc->sent_tasklet);
899 * prepare and submit USB request blocks for isochronous input and output
901 * B channel control structure
904 * < 0 on error (no URBs submitted)
906 static int starturbs(struct bc_state *bcs)
908 struct bas_bc_state *ubc = bcs->hw.bas;
913 /* initialize L2 reception */
914 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
915 bcs->inputstate |= INS_flag_hunt;
917 /* submit all isochronous input URBs */
919 for (k = 0; k < BAS_INURBS; k++) {
920 urb = ubc->isoinurbs[k];
926 urb->dev = bcs->cs->hw.bas->udev;
927 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
928 urb->transfer_flags = URB_ISO_ASAP;
929 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
930 urb->transfer_buffer_length = BAS_INBUFSIZE;
931 urb->number_of_packets = BAS_NUMFRAMES;
932 urb->interval = BAS_FRAMETIME;
933 urb->complete = read_iso_callback;
935 for (j = 0; j < BAS_NUMFRAMES; j++) {
936 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
937 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
938 urb->iso_frame_desc[j].status = 0;
939 urb->iso_frame_desc[j].actual_length = 0;
942 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
943 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
947 /* initialize L2 transmission */
948 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
950 /* set up isochronous output URBs for flag idling */
951 for (k = 0; k < BAS_OUTURBS; ++k) {
952 urb = ubc->isoouturbs[k].urb;
957 urb->dev = bcs->cs->hw.bas->udev;
958 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
959 urb->transfer_flags = URB_ISO_ASAP;
960 urb->transfer_buffer = ubc->isooutbuf->data;
961 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
962 urb->number_of_packets = BAS_NUMFRAMES;
963 urb->interval = BAS_FRAMETIME;
964 urb->complete = write_iso_callback;
965 urb->context = &ubc->isoouturbs[k];
966 for (j = 0; j < BAS_NUMFRAMES; ++j) {
967 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
968 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
969 urb->iso_frame_desc[j].status = 0;
970 urb->iso_frame_desc[j].actual_length = 0;
972 ubc->isoouturbs[k].limit = -1;
975 /* keep one URB free, submit the others */
976 for (k = 0; k < BAS_OUTURBS-1; ++k) {
977 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
978 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
982 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
983 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
984 ubc->isooutdone = ubc->isooutovfl = NULL;
992 * cancel the USB request blocks for isochronous input and output
993 * errors are silently ignored
995 * B channel control structure
997 static void stopurbs(struct bas_bc_state *ubc)
1003 for (k = 0; k < BAS_INURBS; ++k) {
1004 rc = usb_unlink_urb(ubc->isoinurbs[k]);
1006 "%s: isoc input URB %d unlinked, result = %s",
1007 __func__, k, get_usb_rcmsg(rc));
1010 for (k = 0; k < BAS_OUTURBS; ++k) {
1011 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1013 "%s: isoc output URB %d unlinked, result = %s",
1014 __func__, k, get_usb_rcmsg(rc));
1018 /* Isochronous Write - Bottom Half */
1019 /* =============================== */
1021 /* submit_iso_write_urb
1022 * fill and submit the next isochronous write URB
1024 * ucx context structure containing URB
1026 * number of frames submitted in URB
1027 * 0 if URB not submitted because no data available (isooutbuf busy)
1028 * error code < 0 on error
1030 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1032 struct urb *urb = ucx->urb;
1033 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1034 struct usb_iso_packet_descriptor *ifd;
1035 int corrbytes, nframe, rc;
1037 /* urb->dev is clobbered by USB subsystem */
1038 urb->dev = ucx->bcs->cs->hw.bas->udev;
1039 urb->transfer_flags = URB_ISO_ASAP;
1040 urb->transfer_buffer = ubc->isooutbuf->data;
1041 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1043 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1044 ifd = &urb->iso_frame_desc[nframe];
1046 /* compute frame length according to flow control */
1047 ifd->length = BAS_NORMFRAME;
1048 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
1049 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1050 __func__, corrbytes);
1051 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1052 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1053 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1054 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1055 ifd->length += corrbytes;
1056 atomic_add(-corrbytes, &ubc->corrbytes);
1059 /* retrieve block of data to send */
1060 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1064 "%s: buffer busy at frame %d",
1066 /* tasklet will be restarted from
1067 gigaset_send_skb() */
1069 dev_err(ucx->bcs->cs->dev,
1070 "%s: buffer error %d at frame %d\n",
1071 __func__, rc, nframe);
1077 ucx->limit = ubc->isooutbuf->nextread;
1079 ifd->actual_length = 0;
1081 if (unlikely(nframe == 0))
1082 return 0; /* no data to send */
1083 urb->number_of_packets = nframe;
1085 rc = usb_submit_urb(urb, GFP_ATOMIC);
1088 /* device removed - give up silently */
1089 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1091 dev_err(ucx->bcs->cs->dev,
1092 "could not submit isochronous write URB: %s\n",
1100 /* write_iso_tasklet
1101 * tasklet scheduled when an isochronous output URB from the Gigaset device
1104 * data B channel state structure
1106 static void write_iso_tasklet(unsigned long data)
1108 struct bc_state *bcs = (struct bc_state *) data;
1109 struct bas_bc_state *ubc = bcs->hw.bas;
1110 struct cardstate *cs = bcs->cs;
1111 struct isow_urbctx_t *done, *next, *ovfl;
1114 struct usb_iso_packet_descriptor *ifd;
1116 unsigned long flags;
1118 struct sk_buff *skb;
1122 /* loop while completed URBs arrive in time */
1124 if (unlikely(!(ubc->running))) {
1125 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1129 /* retrieve completed URBs */
1130 spin_lock_irqsave(&ubc->isooutlock, flags);
1131 done = ubc->isooutdone;
1132 ubc->isooutdone = NULL;
1133 ovfl = ubc->isooutovfl;
1134 ubc->isooutovfl = NULL;
1135 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1137 dev_err(cs->dev, "isochronous write buffer underrun\n");
1144 /* submit free URB if available */
1145 spin_lock_irqsave(&ubc->isooutlock, flags);
1146 next = ubc->isooutfree;
1147 ubc->isooutfree = NULL;
1148 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1150 rc = submit_iso_write_urb(next);
1151 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1152 /* could not submit URB, put it back */
1153 spin_lock_irqsave(&ubc->isooutlock, flags);
1154 if (ubc->isooutfree == NULL) {
1155 ubc->isooutfree = next;
1158 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1160 /* couldn't put it back */
1162 "losing isochronous write URB\n");
1168 /* process completed URB */
1170 status = done->status;
1172 case -EXDEV: /* partial completion */
1173 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1175 /* fall through - what's the difference anyway? */
1176 case 0: /* normal completion */
1177 /* inspect individual frames
1178 * assumptions (for lack of documentation):
1179 * - actual_length bytes of first frame in error are
1181 * - all following frames are not sent at all
1183 offset = done->limit; /* default (no error) */
1184 for (i = 0; i < BAS_NUMFRAMES; i++) {
1185 ifd = &urb->iso_frame_desc[i];
1187 ifd->actual_length != ifd->length) {
1189 "isochronous write: frame %d: %s, "
1190 "only %d of %d bytes sent\n",
1191 i, get_usb_statmsg(ifd->status),
1192 ifd->actual_length, ifd->length);
1193 offset = (ifd->offset +
1199 #ifdef CONFIG_GIGASET_DEBUG
1200 /* check assumption on remaining frames */
1201 for (; i < BAS_NUMFRAMES; i++) {
1202 ifd = &urb->iso_frame_desc[i];
1203 if (ifd->status != -EINPROGRESS
1204 || ifd->actual_length != 0) {
1206 "isochronous write: frame %d: %s, "
1207 "%d of %d bytes sent\n",
1208 i, get_usb_statmsg(ifd->status),
1209 ifd->actual_length, ifd->length);
1210 offset = (ifd->offset +
1218 case -EPIPE: /* stall - probably underrun */
1219 dev_err(cs->dev, "isochronous write stalled\n");
1222 default: /* severe trouble */
1223 dev_warn(cs->dev, "isochronous write: %s\n",
1224 get_usb_statmsg(status));
1227 /* mark the write buffer area covered by this URB as free */
1228 if (done->limit >= 0)
1229 ubc->isooutbuf->read = done->limit;
1231 /* mark URB as free */
1232 spin_lock_irqsave(&ubc->isooutlock, flags);
1233 next = ubc->isooutfree;
1234 ubc->isooutfree = done;
1235 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1237 /* only one URB still active - resubmit one */
1238 rc = submit_iso_write_urb(next);
1239 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1240 /* couldn't submit */
1246 /* process queued SKBs */
1247 while ((skb = skb_dequeue(&bcs->squeue))) {
1248 /* copy to output buffer, doing L2 encapsulation */
1250 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1251 /* insufficient buffer space, push back onto queue */
1252 skb_queue_head(&bcs->squeue, skb);
1253 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1254 __func__, skb_queue_len(&bcs->squeue));
1258 gigaset_skb_sent(bcs, skb);
1259 dev_kfree_skb_any(skb);
1263 /* Isochronous Read - Bottom Half */
1264 /* ============================== */
1267 * tasklet scheduled when an isochronous input URB from the Gigaset device
1270 * data B channel state structure
1272 static void read_iso_tasklet(unsigned long data)
1274 struct bc_state *bcs = (struct bc_state *) data;
1275 struct bas_bc_state *ubc = bcs->hw.bas;
1276 struct cardstate *cs = bcs->cs;
1280 unsigned long flags;
1281 int totleft, numbytes, offset, frame, rc;
1283 /* loop while more completed URBs arrive in the meantime */
1286 spin_lock_irqsave(&ubc->isoinlock, flags);
1287 if (!(urb = ubc->isoindone)) {
1288 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1291 status = ubc->isoinstatus;
1292 ubc->isoindone = NULL;
1293 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1295 "isochronous read overrun, "
1296 "dropped URB with status: %s, %d bytes lost\n",
1297 get_usb_statmsg(ubc->loststatus),
1299 ubc->loststatus = -EINPROGRESS;
1301 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1303 if (unlikely(!(ubc->running))) {
1305 "%s: channel not running, "
1306 "dropped URB with status: %s",
1307 __func__, get_usb_statmsg(status));
1312 case 0: /* normal completion */
1314 case -EXDEV: /* inspect individual frames
1315 (we do that anyway) */
1316 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1322 gig_dbg(DEBUG_ISO, "%s: %s",
1323 __func__, get_usb_statmsg(status));
1324 continue; /* -> skip */
1326 dev_err(cs->dev, "isochronous read stalled\n");
1328 continue; /* -> skip */
1329 default: /* severe trouble */
1330 dev_warn(cs->dev, "isochronous read: %s\n",
1331 get_usb_statmsg(status));
1335 rcvbuf = urb->transfer_buffer;
1336 totleft = urb->actual_length;
1337 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1338 numbytes = urb->iso_frame_desc[frame].actual_length;
1339 if (unlikely(urb->iso_frame_desc[frame].status))
1341 "isochronous read: frame %d[%d]: %s\n",
1344 urb->iso_frame_desc[frame].status));
1345 if (unlikely(numbytes > BAS_MAXFRAME))
1347 "isochronous read: frame %d: "
1348 "numbytes (%d) > BAS_MAXFRAME\n",
1350 if (unlikely(numbytes > totleft)) {
1352 "isochronous read: frame %d: "
1353 "numbytes (%d) > totleft (%d)\n",
1354 frame, numbytes, totleft);
1357 offset = urb->iso_frame_desc[frame].offset;
1358 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1360 "isochronous read: frame %d: "
1361 "offset (%d) + numbytes (%d) "
1362 "> BAS_INBUFSIZE\n",
1363 frame, offset, numbytes);
1364 numbytes = BAS_INBUFSIZE - offset;
1366 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1367 totleft -= numbytes;
1369 if (unlikely(totleft > 0))
1371 "isochronous read: %d data bytes missing\n",
1375 /* URB processed, resubmit */
1376 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1377 urb->iso_frame_desc[frame].status = 0;
1378 urb->iso_frame_desc[frame].actual_length = 0;
1380 /* urb->dev is clobbered by USB subsystem */
1381 urb->dev = bcs->cs->hw.bas->udev;
1382 urb->transfer_flags = URB_ISO_ASAP;
1383 urb->number_of_packets = BAS_NUMFRAMES;
1384 rc = usb_submit_urb(urb, GFP_ATOMIC);
1385 if (unlikely(rc != 0 && rc != -ENODEV)) {
1387 "could not resubmit isochronous read URB: %s\n",
1389 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1395 /* Channel Operations */
1396 /* ================== */
1399 * timeout routine for control output request
1401 * B channel control structure
1403 static void req_timeout(unsigned long data)
1405 struct bc_state *bcs = (struct bc_state *) data;
1406 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1408 unsigned long flags;
1412 spin_lock_irqsave(&ucs->lock, flags);
1413 pending = ucs->pending;
1415 spin_unlock_irqrestore(&ucs->lock, flags);
1418 case 0: /* no pending request */
1419 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1422 case HD_OPEN_ATCHANNEL:
1423 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1424 error_reset(bcs->cs);
1427 case HD_OPEN_B2CHANNEL:
1428 case HD_OPEN_B1CHANNEL:
1429 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1434 case HD_CLOSE_ATCHANNEL:
1435 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1436 error_reset(bcs->cs);
1439 case HD_CLOSE_B2CHANNEL:
1440 case HD_CLOSE_B1CHANNEL:
1441 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1443 error_reset(bcs->cs);
1446 case HD_RESET_INTERRUPT_PIPE:
1447 /* error recovery escalation */
1448 dev_err(bcs->cs->dev,
1449 "reset interrupt pipe timeout, attempting USB reset\n");
1450 usb_queue_reset_device(bcs->cs->hw.bas->interface);
1454 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1458 wake_up(&ucs->waitqueue);
1461 /* write_ctrl_callback
1462 * USB completion handler for control pipe output
1463 * called by the USB subsystem in interrupt context
1465 * urb USB request block of completed request
1466 * urb->context = hardware specific controller state structure
1468 static void write_ctrl_callback(struct urb *urb)
1470 struct bas_cardstate *ucs = urb->context;
1471 int status = urb->status;
1473 unsigned long flags;
1477 case 0: /* normal completion */
1478 spin_lock_irqsave(&ucs->lock, flags);
1479 switch (ucs->pending) {
1480 case HD_DEVICE_INIT_ACK: /* no reply expected */
1481 del_timer(&ucs->timer_ctrl);
1485 spin_unlock_irqrestore(&ucs->lock, flags);
1488 case -ENOENT: /* cancelled */
1489 case -ECONNRESET: /* cancelled (async) */
1490 case -EINPROGRESS: /* pending */
1491 case -ENODEV: /* device removed */
1492 case -ESHUTDOWN: /* device shut down */
1493 /* ignore silently */
1494 gig_dbg(DEBUG_USBREQ, "%s: %s",
1495 __func__, get_usb_statmsg(status));
1498 default: /* any failure */
1499 /* don't retry if suspend requested */
1500 if (++ucs->retry_ctrl > BAS_RETRY ||
1501 (ucs->basstate & BS_SUSPEND)) {
1502 dev_err(&ucs->interface->dev,
1503 "control request 0x%02x failed: %s\n",
1504 ucs->dr_ctrl.bRequest,
1505 get_usb_statmsg(status));
1506 break; /* give up */
1508 dev_notice(&ucs->interface->dev,
1509 "control request 0x%02x: %s, retry %d\n",
1510 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1512 /* urb->dev is clobbered by USB subsystem */
1513 urb->dev = ucs->udev;
1514 rc = usb_submit_urb(urb, GFP_ATOMIC);
1516 dev_err(&ucs->interface->dev,
1517 "could not resubmit request 0x%02x: %s\n",
1518 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1525 /* failed, clear pending request */
1526 spin_lock_irqsave(&ucs->lock, flags);
1527 del_timer(&ucs->timer_ctrl);
1529 spin_unlock_irqrestore(&ucs->lock, flags);
1530 wake_up(&ucs->waitqueue);
1534 * submit a control output request without message buffer to the Gigaset base
1535 * and optionally start a timeout
1537 * bcs B channel control structure
1538 * req control request code (HD_*)
1539 * val control request parameter value (set to 0 if unused)
1540 * timeout timeout in seconds (0: no timeout)
1543 * -EBUSY if another request is pending
1544 * any URB submission error code
1546 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1548 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1550 unsigned long flags;
1552 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1554 spin_lock_irqsave(&ucs->lock, flags);
1556 spin_unlock_irqrestore(&ucs->lock, flags);
1557 dev_err(bcs->cs->dev,
1558 "submission of request 0x%02x failed: "
1559 "request 0x%02x still pending\n",
1564 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1565 ucs->dr_ctrl.bRequest = req;
1566 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1567 ucs->dr_ctrl.wIndex = 0;
1568 ucs->dr_ctrl.wLength = 0;
1569 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1570 usb_sndctrlpipe(ucs->udev, 0),
1571 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1572 write_ctrl_callback, ucs);
1573 ucs->retry_ctrl = 0;
1574 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1575 if (unlikely(ret)) {
1576 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1577 req, get_usb_rcmsg(ret));
1578 spin_unlock_irqrestore(&ucs->lock, flags);
1584 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1585 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1586 ucs->timer_ctrl.data = (unsigned long) bcs;
1587 ucs->timer_ctrl.function = req_timeout;
1588 add_timer(&ucs->timer_ctrl);
1591 spin_unlock_irqrestore(&ucs->lock, flags);
1595 /* gigaset_init_bchannel
1596 * called by common.c to connect a B channel
1597 * initialize isochronous I/O and tell the Gigaset base to open the channel
1599 * B channel control structure
1601 * 0 on success, error code < 0 on error
1603 static int gigaset_init_bchannel(struct bc_state *bcs)
1605 struct cardstate *cs = bcs->cs;
1607 unsigned long flags;
1609 spin_lock_irqsave(&cs->lock, flags);
1610 if (unlikely(!cs->connected)) {
1611 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1612 spin_unlock_irqrestore(&cs->lock, flags);
1616 if (cs->hw.bas->basstate & BS_SUSPEND) {
1618 "not starting isochronous I/O, "
1619 "suspend in progress\n");
1620 spin_unlock_irqrestore(&cs->lock, flags);
1621 return -EHOSTUNREACH;
1624 if ((ret = starturbs(bcs)) < 0) {
1626 "could not start isochronous I/O for channel B%d: %s\n",
1628 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1631 spin_unlock_irqrestore(&cs->lock, flags);
1635 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1636 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
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 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1681 dev_err(cs->dev, "closing channel B%d failed\n",
1684 spin_unlock_irqrestore(&cs->lock, flags);
1688 /* Device Operations */
1689 /* ================= */
1692 * unqueue first command buffer from queue, waking any sleepers
1693 * must be called with cs->cmdlock held
1695 * cs controller state structure
1697 static void complete_cb(struct cardstate *cs)
1699 struct cmdbuf_t *cb = cs->cmdbuf;
1701 /* unqueue completed buffer */
1702 cs->cmdbytes -= cs->curlen;
1703 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1704 "write_command: sent %u bytes, %u left",
1705 cs->curlen, cs->cmdbytes);
1706 if ((cs->cmdbuf = cb->next) != NULL) {
1707 cs->cmdbuf->prev = NULL;
1708 cs->curlen = cs->cmdbuf->len;
1710 cs->lastcmdbuf = NULL;
1714 if (cb->wake_tasklet)
1715 tasklet_schedule(cb->wake_tasklet);
1720 /* write_command_callback
1721 * USB completion handler for AT command transmission
1722 * called by the USB subsystem in interrupt context
1724 * urb USB request block of completed request
1725 * urb->context = controller state structure
1727 static void write_command_callback(struct urb *urb)
1729 struct cardstate *cs = urb->context;
1730 struct bas_cardstate *ucs = cs->hw.bas;
1731 int status = urb->status;
1732 unsigned long flags;
1734 update_basstate(ucs, 0, BS_ATWRPEND);
1735 wake_up(&ucs->waitqueue);
1739 case 0: /* normal completion */
1741 case -ENOENT: /* cancelled */
1742 case -ECONNRESET: /* cancelled (async) */
1743 case -EINPROGRESS: /* pending */
1744 case -ENODEV: /* device removed */
1745 case -ESHUTDOWN: /* device shut down */
1746 /* ignore silently */
1747 gig_dbg(DEBUG_USBREQ, "%s: %s",
1748 __func__, get_usb_statmsg(status));
1750 default: /* any failure */
1751 if (++ucs->retry_cmd_out > BAS_RETRY) {
1753 "command write: %s, "
1754 "giving up after %d retries\n",
1755 get_usb_statmsg(status),
1756 ucs->retry_cmd_out);
1759 if (ucs->basstate & BS_SUSPEND) {
1761 "command write: %s, "
1762 "won't retry - suspend requested\n",
1763 get_usb_statmsg(status));
1766 if (cs->cmdbuf == NULL) {
1768 "command write: %s, "
1769 "cannot retry - cmdbuf gone\n",
1770 get_usb_statmsg(status));
1773 dev_notice(cs->dev, "command write: %s, retry %d\n",
1774 get_usb_statmsg(status), ucs->retry_cmd_out);
1775 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1776 /* resubmitted - bypass regular exit block */
1778 /* command send failed, assume base still waiting */
1779 update_basstate(ucs, BS_ATREADY, 0);
1782 spin_lock_irqsave(&cs->cmdlock, flags);
1783 if (cs->cmdbuf != NULL)
1785 spin_unlock_irqrestore(&cs->cmdlock, flags);
1789 * timeout routine for AT command transmission
1791 * controller state structure
1793 static void atrdy_timeout(unsigned long data)
1795 struct cardstate *cs = (struct cardstate *) data;
1796 struct bas_cardstate *ucs = cs->hw.bas;
1798 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1800 /* fake the missing signal - what else can I do? */
1801 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1806 * submit an HD_WRITE_ATMESSAGE command URB
1808 * cs controller state structure
1809 * buf buffer containing command to send
1810 * len length of command to send
1813 * -EBUSY if another request is pending
1814 * any URB submission error code
1816 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1818 struct bas_cardstate *ucs = cs->hw.bas;
1821 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1823 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1825 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1829 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1830 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1831 ucs->dr_cmd_out.wValue = 0;
1832 ucs->dr_cmd_out.wIndex = 0;
1833 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1834 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1835 usb_sndctrlpipe(ucs->udev, 0),
1836 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1837 write_command_callback, cs);
1838 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1840 update_basstate(ucs, 0, BS_ATWRPEND);
1841 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1846 /* submitted successfully, start timeout if necessary */
1847 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1848 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1850 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1851 ucs->timer_atrdy.data = (unsigned long) cs;
1852 ucs->timer_atrdy.function = atrdy_timeout;
1853 add_timer(&ucs->timer_atrdy);
1859 * start transmission of AT command queue if necessary
1861 * cs controller state structure
1864 * error code < 0 on error
1866 static int start_cbsend(struct cardstate *cs)
1868 struct cmdbuf_t *cb;
1869 struct bas_cardstate *ucs = cs->hw.bas;
1870 unsigned long flags;
1874 /* check if suspend requested */
1875 if (ucs->basstate & BS_SUSPEND) {
1876 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1877 return -EHOSTUNREACH;
1880 /* check if AT channel is open */
1881 if (!(ucs->basstate & BS_ATOPEN)) {
1882 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
1883 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1885 /* flush command queue */
1886 spin_lock_irqsave(&cs->cmdlock, flags);
1887 while (cs->cmdbuf != NULL)
1889 spin_unlock_irqrestore(&cs->cmdlock, flags);
1894 /* try to send first command in queue */
1895 spin_lock_irqsave(&cs->cmdlock, flags);
1897 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1898 ucs->retry_cmd_out = 0;
1899 rc = atwrite_submit(cs, cb->buf, cb->len);
1906 spin_unlock_irqrestore(&cs->cmdlock, flags);
1910 /* gigaset_write_cmd
1911 * This function is called by the device independent part of the driver
1912 * to transmit an AT command string to the Gigaset device.
1913 * It encapsulates the device specific method for transmission over the
1914 * direct USB connection to the base.
1915 * The command string is added to the queue of commands to send, and
1916 * USB transmission is started if necessary.
1918 * cs controller state structure
1919 * buf command string to send
1920 * len number of bytes to send (max. IF_WRITEBUF)
1921 * wake_tasklet tasklet to run when transmission is completed
1924 * number of bytes queued on success
1925 * error code < 0 on error
1927 static int gigaset_write_cmd(struct cardstate *cs,
1928 const unsigned char *buf, int len,
1929 struct tasklet_struct *wake_tasklet)
1931 struct cmdbuf_t *cb;
1932 unsigned long flags;
1935 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1936 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1937 "CMD Transmit", len, buf);
1945 /* translate "+++" escape sequence sent as a single separate command
1946 * into "close AT channel" command for error recovery
1947 * The next command will reopen the AT channel automatically.
1949 if (len == 3 && !memcmp(buf, "+++", 3)) {
1950 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
1954 if (len > IF_WRITEBUF)
1956 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1957 dev_err(cs->dev, "%s: out of memory\n", __func__);
1962 memcpy(cb->buf, buf, len);
1966 cb->wake_tasklet = wake_tasklet;
1968 spin_lock_irqsave(&cs->cmdlock, flags);
1969 cb->prev = cs->lastcmdbuf;
1971 cs->lastcmdbuf->next = cb;
1976 cs->cmdbytes += len;
1977 cs->lastcmdbuf = cb;
1978 spin_unlock_irqrestore(&cs->cmdlock, flags);
1980 spin_lock_irqsave(&cs->lock, flags);
1981 if (unlikely(!cs->connected)) {
1982 spin_unlock_irqrestore(&cs->lock, flags);
1983 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1984 /* flush command queue */
1985 spin_lock_irqsave(&cs->cmdlock, flags);
1986 while (cs->cmdbuf != NULL)
1988 spin_unlock_irqrestore(&cs->cmdlock, flags);
1991 rc = start_cbsend(cs);
1992 spin_unlock_irqrestore(&cs->lock, flags);
1993 return rc < 0 ? rc : len;
1995 notqueued: /* request handled without queuing */
1997 tasklet_schedule(wake_tasklet);
2001 /* gigaset_write_room
2002 * tty_driver.write_room interface routine
2003 * return number of characters the driver will accept to be written via
2006 * controller state structure
2008 * number of characters
2010 static int gigaset_write_room(struct cardstate *cs)
2015 /* gigaset_chars_in_buffer
2016 * tty_driver.chars_in_buffer interface routine
2017 * return number of characters waiting to be sent
2019 * controller state structure
2021 * number of characters
2023 static int gigaset_chars_in_buffer(struct cardstate *cs)
2025 return cs->cmdbytes;
2029 * implementation of ioctl(GIGASET_BRKCHARS)
2031 * controller state structure
2033 * -EINVAL (unimplemented function)
2035 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2041 /* Device Initialization/Shutdown */
2042 /* ============================== */
2044 /* Free hardware dependent part of the B channel structure
2046 * bcs B channel structure
2050 static int gigaset_freebcshw(struct bc_state *bcs)
2052 struct bas_bc_state *ubc = bcs->hw.bas;
2058 /* kill URBs and tasklets before freeing - better safe than sorry */
2060 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2061 for (i = 0; i < BAS_OUTURBS; ++i) {
2062 usb_kill_urb(ubc->isoouturbs[i].urb);
2063 usb_free_urb(ubc->isoouturbs[i].urb);
2065 for (i = 0; i < BAS_INURBS; ++i) {
2066 usb_kill_urb(ubc->isoinurbs[i]);
2067 usb_free_urb(ubc->isoinurbs[i]);
2069 tasklet_kill(&ubc->sent_tasklet);
2070 tasklet_kill(&ubc->rcvd_tasklet);
2071 kfree(ubc->isooutbuf);
2077 /* Initialize hardware dependent part of the B channel structure
2079 * bcs B channel structure
2083 static int gigaset_initbcshw(struct bc_state *bcs)
2086 struct bas_bc_state *ubc;
2088 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2090 pr_err("out of memory\n");
2095 atomic_set(&ubc->corrbytes, 0);
2096 spin_lock_init(&ubc->isooutlock);
2097 for (i = 0; i < BAS_OUTURBS; ++i) {
2098 ubc->isoouturbs[i].urb = NULL;
2099 ubc->isoouturbs[i].bcs = bcs;
2101 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2103 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2104 pr_err("out of memory\n");
2109 tasklet_init(&ubc->sent_tasklet,
2110 &write_iso_tasklet, (unsigned long) bcs);
2112 spin_lock_init(&ubc->isoinlock);
2113 for (i = 0; i < BAS_INURBS; ++i)
2114 ubc->isoinurbs[i] = NULL;
2115 ubc->isoindone = NULL;
2116 ubc->loststatus = -EINPROGRESS;
2130 tasklet_init(&ubc->rcvd_tasklet,
2131 &read_iso_tasklet, (unsigned long) bcs);
2135 static void gigaset_reinitbcshw(struct bc_state *bcs)
2137 struct bas_bc_state *ubc = bcs->hw.bas;
2139 bcs->hw.bas->running = 0;
2140 atomic_set(&bcs->hw.bas->corrbytes, 0);
2141 bcs->hw.bas->numsub = 0;
2142 spin_lock_init(&ubc->isooutlock);
2143 spin_lock_init(&ubc->isoinlock);
2144 ubc->loststatus = -EINPROGRESS;
2147 static void gigaset_freecshw(struct cardstate *cs)
2149 /* timers, URBs and rcvbuf are disposed of in disconnect */
2150 kfree(cs->hw.bas->int_in_buf);
2155 static int gigaset_initcshw(struct cardstate *cs)
2157 struct bas_cardstate *ucs;
2159 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2161 pr_err("out of memory\n");
2164 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2165 if (!ucs->int_in_buf) {
2167 pr_err("out of memory\n");
2171 ucs->urb_cmd_in = NULL;
2172 ucs->urb_cmd_out = NULL;
2174 ucs->rcvbuf_size = 0;
2176 spin_lock_init(&ucs->lock);
2180 init_timer(&ucs->timer_ctrl);
2181 init_timer(&ucs->timer_atrdy);
2182 init_timer(&ucs->timer_cmd_in);
2183 init_waitqueue_head(&ucs->waitqueue);
2189 * unlink and deallocate all URBs unconditionally
2190 * caller must make sure that no commands are still in progress
2192 * cs controller state structure
2194 static void freeurbs(struct cardstate *cs)
2196 struct bas_cardstate *ucs = cs->hw.bas;
2197 struct bas_bc_state *ubc;
2200 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
2201 for (j = 0; j < BAS_CHANNELS; ++j) {
2202 ubc = cs->bcs[j].hw.bas;
2203 for (i = 0; i < BAS_OUTURBS; ++i) {
2204 usb_kill_urb(ubc->isoouturbs[i].urb);
2205 usb_free_urb(ubc->isoouturbs[i].urb);
2206 ubc->isoouturbs[i].urb = NULL;
2208 for (i = 0; i < BAS_INURBS; ++i) {
2209 usb_kill_urb(ubc->isoinurbs[i]);
2210 usb_free_urb(ubc->isoinurbs[i]);
2211 ubc->isoinurbs[i] = NULL;
2214 usb_kill_urb(ucs->urb_int_in);
2215 usb_free_urb(ucs->urb_int_in);
2216 ucs->urb_int_in = NULL;
2217 usb_kill_urb(ucs->urb_cmd_out);
2218 usb_free_urb(ucs->urb_cmd_out);
2219 ucs->urb_cmd_out = NULL;
2220 usb_kill_urb(ucs->urb_cmd_in);
2221 usb_free_urb(ucs->urb_cmd_in);
2222 ucs->urb_cmd_in = NULL;
2223 usb_kill_urb(ucs->urb_ctrl);
2224 usb_free_urb(ucs->urb_ctrl);
2225 ucs->urb_ctrl = NULL;
2229 * This function is called when a new USB device is connected.
2230 * It checks whether the new device is handled by this driver.
2232 static int gigaset_probe(struct usb_interface *interface,
2233 const struct usb_device_id *id)
2235 struct usb_host_interface *hostif;
2236 struct usb_device *udev = interface_to_usbdev(interface);
2237 struct cardstate *cs = NULL;
2238 struct bas_cardstate *ucs = NULL;
2239 struct bas_bc_state *ubc;
2240 struct usb_endpoint_descriptor *endpoint;
2245 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2246 __func__, le16_to_cpu(udev->descriptor.idVendor),
2247 le16_to_cpu(udev->descriptor.idProduct));
2249 /* set required alternate setting */
2250 hostif = interface->cur_altsetting;
2251 if (hostif->desc.bAlternateSetting != 3) {
2253 "%s: wrong alternate setting %d - trying to switch",
2254 __func__, hostif->desc.bAlternateSetting);
2255 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2256 dev_warn(&udev->dev, "usb_set_interface failed, "
2257 "device %d interface %d altsetting %d\n",
2258 udev->devnum, hostif->desc.bInterfaceNumber,
2259 hostif->desc.bAlternateSetting);
2262 hostif = interface->cur_altsetting;
2265 /* Reject application specific interfaces
2267 if (hostif->desc.bInterfaceClass != 255) {
2268 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2269 __func__, hostif->desc.bInterfaceClass);
2273 dev_info(&udev->dev,
2274 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2275 __func__, le16_to_cpu(udev->descriptor.idVendor),
2276 le16_to_cpu(udev->descriptor.idProduct));
2278 /* allocate memory for our device state and intialize it */
2279 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2280 GIGASET_MODULENAME);
2285 /* save off device structure ptrs for later use */
2288 ucs->interface = interface;
2289 cs->dev = &interface->dev;
2292 * - one for the interrupt pipe
2293 * - three for the different uses of the default control pipe
2294 * - three for each isochronous pipe
2296 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2297 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2298 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2299 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2302 for (j = 0; j < BAS_CHANNELS; ++j) {
2303 ubc = cs->bcs[j].hw.bas;
2304 for (i = 0; i < BAS_OUTURBS; ++i)
2305 if (!(ubc->isoouturbs[i].urb =
2306 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2308 for (i = 0; i < BAS_INURBS; ++i)
2309 if (!(ubc->isoinurbs[i] =
2310 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2315 ucs->rcvbuf_size = 0;
2317 /* Fill the interrupt urb and send it to the core */
2318 endpoint = &hostif->endpoint[0].desc;
2319 usb_fill_int_urb(ucs->urb_int_in, udev,
2320 usb_rcvintpipe(udev,
2321 (endpoint->bEndpointAddress) & 0x0f),
2322 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
2323 endpoint->bInterval);
2324 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
2325 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2330 /* tell the device that the driver is ready */
2331 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2334 /* tell common part that the device is ready */
2335 if (startmode == SM_LOCKED)
2336 cs->mstate = MS_LOCKED;
2338 /* save address of controller structure */
2339 usb_set_intfdata(interface, cs);
2341 if (!gigaset_start(cs))
2347 dev_err(cs->dev, "could not allocate URBs\n");
2350 usb_set_intfdata(interface, NULL);
2355 /* gigaset_disconnect
2356 * This function is called when the Gigaset base is unplugged.
2358 static void gigaset_disconnect(struct usb_interface *interface)
2360 struct cardstate *cs;
2361 struct bas_cardstate *ucs;
2364 cs = usb_get_intfdata(interface);
2368 dev_info(cs->dev, "disconnecting Gigaset base\n");
2370 /* mark base as not ready, all channels disconnected */
2373 /* tell LL all channels are down */
2374 for (j = 0; j < BAS_CHANNELS; ++j)
2375 gigaset_bchannel_down(cs->bcs + j);
2377 /* stop driver (common part) */
2380 /* stop timers and URBs, free ressources */
2381 del_timer_sync(&ucs->timer_ctrl);
2382 del_timer_sync(&ucs->timer_atrdy);
2383 del_timer_sync(&ucs->timer_cmd_in);
2385 usb_set_intfdata(interface, NULL);
2388 ucs->rcvbuf_size = 0;
2389 usb_put_dev(ucs->udev);
2390 ucs->interface = NULL;
2397 * This function is called before the USB connection is suspended.
2399 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2401 struct cardstate *cs = usb_get_intfdata(intf);
2402 struct bas_cardstate *ucs = cs->hw.bas;
2405 /* set suspend flag; this stops AT command/response traffic */
2406 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2407 gig_dbg(DEBUG_SUSPEND, "already suspended");
2411 /* wait a bit for blocking conditions to go away */
2412 rc = wait_event_timeout(ucs->waitqueue,
2414 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2416 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2418 /* check for conditions preventing suspend */
2419 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
2420 dev_warn(cs->dev, "cannot suspend:\n");
2421 if (ucs->basstate & BS_B1OPEN)
2422 dev_warn(cs->dev, " B channel 1 open\n");
2423 if (ucs->basstate & BS_B2OPEN)
2424 dev_warn(cs->dev, " B channel 2 open\n");
2425 if (ucs->basstate & BS_ATRDPEND)
2426 dev_warn(cs->dev, " receiving AT reply\n");
2427 if (ucs->basstate & BS_ATWRPEND)
2428 dev_warn(cs->dev, " sending AT command\n");
2429 update_basstate(ucs, 0, BS_SUSPEND);
2433 /* close AT channel if open */
2434 if (ucs->basstate & BS_ATOPEN) {
2435 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2436 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2438 update_basstate(ucs, 0, BS_SUSPEND);
2441 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2443 /* in case of timeout, proceed anyway */
2446 /* kill all URBs and timers that might still be pending */
2447 usb_kill_urb(ucs->urb_ctrl);
2448 usb_kill_urb(ucs->urb_int_in);
2449 del_timer_sync(&ucs->timer_ctrl);
2451 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2456 * This function is called after the USB connection has been resumed.
2458 static int gigaset_resume(struct usb_interface *intf)
2460 struct cardstate *cs = usb_get_intfdata(intf);
2461 struct bas_cardstate *ucs = cs->hw.bas;
2464 /* resubmit interrupt URB for spontaneous messages from base */
2465 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2467 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2472 /* clear suspend flag to reallow activity */
2473 update_basstate(ucs, 0, BS_SUSPEND);
2475 gig_dbg(DEBUG_SUSPEND, "resume complete");
2479 /* gigaset_pre_reset
2480 * This function is called before the USB connection is reset.
2482 static int gigaset_pre_reset(struct usb_interface *intf)
2484 /* handle just like suspend */
2485 return gigaset_suspend(intf, PMSG_ON);
2488 /* gigaset_post_reset
2489 * This function is called after the USB connection has been reset.
2491 static int gigaset_post_reset(struct usb_interface *intf)
2493 /* FIXME: send HD_DEVICE_INIT_ACK? */
2495 /* resume operations */
2496 return gigaset_resume(intf);
2500 static const struct gigaset_ops gigops = {
2503 gigaset_chars_in_buffer,
2505 gigaset_init_bchannel,
2506 gigaset_close_bchannel,
2509 gigaset_reinitbcshw,
2512 gigaset_set_modem_ctrl,
2514 gigaset_set_line_ctrl,
2515 gigaset_isoc_send_skb,
2520 * This function is called after the kernel module is loaded.
2522 static int __init bas_gigaset_init(void)
2526 /* allocate memory for our driver state and intialize it */
2527 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2528 GIGASET_MODULENAME, GIGASET_DEVNAME,
2529 &gigops, THIS_MODULE)) == NULL)
2532 /* register this driver with the USB subsystem */
2533 result = usb_register(&gigaset_usb_driver);
2535 pr_err("error %d registering USB driver\n", -result);
2539 pr_info(DRIVER_DESC "\n");
2544 gigaset_freedriver(driver);
2550 * This function is called before the kernel module is unloaded.
2552 static void __exit bas_gigaset_exit(void)
2554 struct bas_cardstate *ucs;
2557 gigaset_blockdriver(driver); /* => probe will fail
2558 * => no gigaset_start any more
2561 /* stop all connected devices */
2562 for (i = 0; i < driver->minors; i++) {
2563 if (gigaset_shutdown(driver->cs + i) < 0)
2564 continue; /* no device */
2565 /* from now on, no isdn callback should be possible */
2567 /* close all still open channels */
2568 ucs = driver->cs[i].hw.bas;
2569 if (ucs->basstate & BS_B1OPEN) {
2570 gig_dbg(DEBUG_INIT, "closing B1 channel");
2571 usb_control_msg(ucs->udev,
2572 usb_sndctrlpipe(ucs->udev, 0),
2573 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2574 0, 0, NULL, 0, BAS_TIMEOUT);
2576 if (ucs->basstate & BS_B2OPEN) {
2577 gig_dbg(DEBUG_INIT, "closing B2 channel");
2578 usb_control_msg(ucs->udev,
2579 usb_sndctrlpipe(ucs->udev, 0),
2580 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2581 0, 0, NULL, 0, BAS_TIMEOUT);
2583 if (ucs->basstate & BS_ATOPEN) {
2584 gig_dbg(DEBUG_INIT, "closing AT channel");
2585 usb_control_msg(ucs->udev,
2586 usb_sndctrlpipe(ucs->udev, 0),
2587 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2588 0, 0, NULL, 0, BAS_TIMEOUT);
2593 /* deregister this driver with the USB subsystem */
2594 usb_deregister(&gigaset_usb_driver);
2595 /* this will call the disconnect-callback */
2596 /* from now on, no disconnect/probe callback should be running */
2598 gigaset_freedriver(driver);
2603 module_init(bas_gigaset_init);
2604 module_exit(bas_gigaset_exit);
2606 MODULE_AUTHOR(DRIVER_AUTHOR);
2607 MODULE_DESCRIPTION(DRIVER_DESC);
2608 MODULE_LICENSE("GPL");