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>,
6 * Stefan Eilers <Eilers.Stefan@epost.de>.
8 * Based on usb-gigaset.c.
10 * =====================================================================
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 * =====================================================================
17 * =====================================================================
18 * Version: $Id: bas-gigaset.c,v 1.52.4.19 2006/02/04 18:28:16 hjlipp Exp $
19 * =====================================================================
24 #include <linux/errno.h>
25 #include <linux/init.h>
26 #include <linux/slab.h>
27 #include <linux/timer.h>
28 #include <linux/usb.h>
29 #include <linux/module.h>
30 #include <linux/moduleparam.h>
32 /* Version Information */
33 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
34 #define DRIVER_DESC "USB Driver for Gigaset 307x"
37 /* Module parameters */
39 static int startmode = SM_ISDN;
40 static int cidmode = 1;
42 module_param(startmode, int, S_IRUGO);
43 module_param(cidmode, int, S_IRUGO);
44 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
45 MODULE_PARM_DESC(cidmode, "Call-ID mode");
47 #define GIGASET_MINORS 1
48 #define GIGASET_MINOR 16
49 #define GIGASET_MODULENAME "bas_gigaset"
50 #define GIGASET_DEVFSNAME "gig/bas/"
51 #define GIGASET_DEVNAME "ttyGB"
53 #define IF_WRITEBUF 256 //FIXME
55 /* Values for the Gigaset 307x */
56 #define USB_GIGA_VENDOR_ID 0x0681
57 #define USB_GIGA_PRODUCT_ID 0x0001
58 #define USB_4175_PRODUCT_ID 0x0002
59 #define USB_SX303_PRODUCT_ID 0x0021
60 #define USB_SX353_PRODUCT_ID 0x0022
62 /* table of devices that work with this driver */
63 static struct usb_device_id gigaset_table [] = {
64 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_GIGA_PRODUCT_ID) },
65 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_4175_PRODUCT_ID) },
66 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
67 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
68 { } /* Terminating entry */
71 MODULE_DEVICE_TABLE(usb, gigaset_table);
73 /* Get a minor range for your devices from the usb maintainer */
74 #define USB_SKEL_MINOR_BASE 200
76 /*======================= local function prototypes =============================*/
78 /* This function is called if a new device is connected to the USB port. It
79 * checks whether this new device belongs to this driver.
81 static int gigaset_probe(struct usb_interface *interface,
82 const struct usb_device_id *id);
84 /* Function will be called if the device is unplugged */
85 static void gigaset_disconnect(struct usb_interface *interface);
88 /*==============================================================================*/
90 struct bas_cardstate {
91 struct usb_device *udev; /* USB device pointer */
92 struct usb_interface *interface; /* interface for this device */
93 unsigned char minor; /* starting minor number */
95 struct urb *urb_ctrl; /* control pipe default URB */
96 struct usb_ctrlrequest dr_ctrl;
97 struct timer_list timer_ctrl; /* control request timeout */
99 struct timer_list timer_atrdy; /* AT command ready timeout */
100 struct urb *urb_cmd_out; /* for sending AT commands */
101 struct usb_ctrlrequest dr_cmd_out;
104 struct urb *urb_cmd_in; /* for receiving AT replies */
105 struct usb_ctrlrequest dr_cmd_in;
106 struct timer_list timer_cmd_in; /* receive request timeout */
107 unsigned char *rcvbuf; /* AT reply receive buffer */
109 struct urb *urb_int_in; /* URB for interrupt pipe */
110 unsigned char int_in_buf[3];
112 spinlock_t lock; /* locks all following */
113 atomic_t basstate; /* bitmap (BS_*) */
114 int pending; /* uncompleted base request */
115 int rcvbuf_size; /* size of AT receive buffer */
116 /* 0: no receive in progress */
117 int retry_cmd_in; /* receive req retry count */
120 /* status of direct USB connection to 307x base (bits in basstate) */
121 #define BS_ATOPEN 0x001
122 #define BS_B1OPEN 0x002
123 #define BS_B2OPEN 0x004
124 #define BS_ATREADY 0x008
125 #define BS_INIT 0x010
126 #define BS_ATTIMER 0x020
129 static struct gigaset_driver *driver = NULL;
130 static struct cardstate *cardstate = NULL;
132 /* usb specific object needed to register this driver with the usb subsystem */
133 static struct usb_driver gigaset_usb_driver = {
134 .name = GIGASET_MODULENAME,
135 .probe = gigaset_probe,
136 .disconnect = gigaset_disconnect,
137 .id_table = gigaset_table,
140 /* get message text for USB status code
142 static char *get_usb_statmsg(int status)
144 static char unkmsg[28];
152 return "canceled (async)";
156 return "bit stuffing or unknown USB error";
158 return "Illegal byte sequence (CRC mismatch)";
160 return "babble detect or endpoint stalled";
162 return "buffer error";
166 return "device not present";
168 return "short packet detected";
170 return "partial isochronous transfer";
172 return "invalid argument";
174 return "URB already queued";
176 return "isochronous start frame too early or too much scheduled";
178 return "too many isochronous frames requested";
180 return "endpoint message size zero";
182 return "endpoint shutdown";
184 return "another request pending";
186 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", status);
192 * retrieve string representation of USB pipe type
194 static inline char *usb_pipetype_str(int pipe)
196 if (usb_pipeisoc(pipe))
198 if (usb_pipeint(pipe))
200 if (usb_pipecontrol(pipe))
202 if (usb_pipebulk(pipe))
208 * write content of URB to syslog for debugging
210 static inline void dump_urb(enum debuglevel level, const char *tag,
213 #ifdef CONFIG_GIGASET_DEBUG
216 dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
219 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
220 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
221 (unsigned long) urb->dev,
222 usb_pipetype_str(urb->pipe),
223 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
224 usb_pipein(urb->pipe) ? "in" : "out",
225 urb->status, (unsigned long) urb->hcpriv,
226 urb->transfer_flags);
228 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
229 "bandwidth=%d, setup_packet=0x%08lx,",
230 (unsigned long) urb->transfer_buffer,
231 urb->transfer_buffer_length, urb->actual_length,
232 urb->bandwidth, (unsigned long) urb->setup_packet);
234 " start_frame=%d, number_of_packets=%d, interval=%d, "
236 urb->start_frame, urb->number_of_packets, urb->interval,
239 " context=0x%08lx, complete=0x%08lx, iso_frame_desc[]={",
240 (unsigned long) urb->context,
241 (unsigned long) urb->complete);
242 for (i = 0; i < urb->number_of_packets; i++) {
243 struct usb_iso_packet_descriptor *pifd = &urb->iso_frame_desc[i];
245 " {offset=%u, length=%u, actual_length=%u, "
247 pifd->offset, pifd->length, pifd->actual_length,
255 /* read/set modem control bits etc. (m10x only) */
256 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
262 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
267 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
273 * hang up any existing connection because of an unrecoverable error
274 * This function may be called from any context and takes care of scheduling
275 * the necessary actions for execution outside of interrupt context.
277 * B channel control structure
279 static inline void error_hangup(struct bc_state *bcs)
281 struct cardstate *cs = bcs->cs;
284 "%s: scheduling HUP for channel %d", __func__, bcs->channel);
286 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
287 //FIXME what should we do?
291 gigaset_schedule_event(cs);
295 * reset Gigaset device because of an unrecoverable error
296 * This function may be called from any context and takes care of scheduling
297 * the necessary actions for execution outside of interrupt context.
299 * controller state structure
301 static inline void error_reset(struct cardstate *cs)
303 //FIXME try to recover without bothering the user
304 err("unrecoverable error - please disconnect the Gigaset base to reset");
308 * check for completion of pending control request
310 * urb USB request block of completed request
311 * urb->context = hardware specific controller state structure
313 static void check_pending(struct bas_cardstate *ucs)
318 IFNULLRET(cardstate);
320 spin_lock_irqsave(&ucs->lock, flags);
321 switch (ucs->pending) {
324 case HD_OPEN_ATCHANNEL:
325 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
328 case HD_OPEN_B1CHANNEL:
329 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
332 case HD_OPEN_B2CHANNEL:
333 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
336 case HD_CLOSE_ATCHANNEL:
337 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
339 //wake_up_interruptible(cs->initwait);
340 //FIXME need own wait queue?
342 case HD_CLOSE_B1CHANNEL:
343 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
346 case HD_CLOSE_B2CHANNEL:
347 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
350 case HD_DEVICE_INIT_ACK: /* no reply expected */
353 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
354 * are handled separately and should never end up here
357 warn("unknown pending request 0x%02x cleared", ucs->pending);
362 del_timer(&ucs->timer_ctrl);
364 spin_unlock_irqrestore(&ucs->lock, flags);
368 * timeout routine for command input request
370 * controller state structure
372 static void cmd_in_timeout(unsigned long data)
374 struct cardstate *cs = (struct cardstate *) data;
375 struct bas_cardstate *ucs;
382 spin_lock_irqsave(&cs->lock, flags);
383 if (!atomic_read(&cs->connected)) {
384 dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
385 spin_unlock_irqrestore(&cs->lock, flags);
388 if (!ucs->rcvbuf_size) {
389 dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
390 spin_unlock_irqrestore(&cs->lock, flags);
393 spin_unlock_irqrestore(&cs->lock, flags);
395 err("timeout reading AT response");
396 error_reset(cs); //FIXME retry?
400 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);
403 * submit an HD_READ_ATMESSAGE command URB
405 * cs controller state structure
406 * timeout timeout in 1/10 sec., 0: none
409 * -EINVAL if a NULL pointer is encountered somewhere
410 * -EBUSY if another request is pending
411 * any URB submission error code
413 static int atread_submit(struct cardstate *cs, int timeout)
415 struct bas_cardstate *ucs;
418 IFNULLRETVAL(cs, -EINVAL);
420 IFNULLRETVAL(ucs, -EINVAL);
421 IFNULLRETVAL(ucs->urb_cmd_in, -EINVAL);
423 dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)", ucs->rcvbuf_size);
425 if (ucs->urb_cmd_in->status == -EINPROGRESS) {
426 err("could not submit HD_READ_ATMESSAGE: URB busy");
430 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
431 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
432 ucs->dr_cmd_in.wValue = 0;
433 ucs->dr_cmd_in.wIndex = 0;
434 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
435 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
436 usb_rcvctrlpipe(ucs->udev, 0),
437 (unsigned char*) & ucs->dr_cmd_in,
438 ucs->rcvbuf, ucs->rcvbuf_size,
439 read_ctrl_callback, cs->inbuf);
441 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
442 err("could not submit HD_READ_ATMESSAGE: %s",
443 get_usb_statmsg(ret));
448 dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
449 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
450 ucs->timer_cmd_in.data = (unsigned long) cs;
451 ucs->timer_cmd_in.function = cmd_in_timeout;
452 add_timer(&ucs->timer_cmd_in);
457 static void stopurbs(struct bas_bc_state *);
458 static int start_cbsend(struct cardstate *);
460 /* set/clear bits in base connection state
462 inline static void update_basstate(struct bas_cardstate *ucs,
468 spin_lock_irqsave(&ucs->lock, flags);
469 state = atomic_read(&ucs->basstate);
472 atomic_set(&ucs->basstate, state);
473 spin_unlock_irqrestore(&ucs->lock, flags);
478 * USB completion handler for interrupt pipe input
479 * called by the USB subsystem in interrupt context
481 * urb USB request block
482 * urb->context = controller state structure
484 static void read_int_callback(struct urb *urb, struct pt_regs *regs)
486 struct cardstate *cs;
487 struct bas_cardstate *ucs;
488 struct bc_state *bcs;
495 cs = (struct cardstate *) urb->context;
500 if (unlikely(!atomic_read(&cs->connected))) {
501 warn("%s: disconnected", __func__);
505 switch (urb->status) {
506 case 0: /* success */
508 case -ENOENT: /* canceled */
509 case -ECONNRESET: /* canceled (async) */
510 case -EINPROGRESS: /* pending */
511 /* ignore silently */
513 "%s: %s", __func__, get_usb_statmsg(urb->status));
515 default: /* severe trouble */
516 warn("interrupt read: %s", get_usb_statmsg(urb->status));
517 //FIXME corrective action? resubmission always ok?
521 l = (unsigned) ucs->int_in_buf[1] +
522 (((unsigned) ucs->int_in_buf[2]) << 8);
525 "<-------%d: 0x%02x (%u [0x%02x 0x%02x])", urb->actual_length,
526 (int)ucs->int_in_buf[0], l,
527 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
531 switch (ucs->int_in_buf[0]) {
532 case HD_DEVICE_INIT_OK:
533 update_basstate(ucs, BS_INIT, 0);
536 case HD_READY_SEND_ATDATA:
537 del_timer(&ucs->timer_atrdy);
538 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
542 case HD_OPEN_B2CHANNEL_ACK:
544 case HD_OPEN_B1CHANNEL_ACK:
545 bcs = cs->bcs + channel;
546 update_basstate(ucs, BS_B1OPEN << channel, 0);
547 gigaset_bchannel_up(bcs);
550 case HD_OPEN_ATCHANNEL_ACK:
551 update_basstate(ucs, BS_ATOPEN, 0);
555 case HD_CLOSE_B2CHANNEL_ACK:
557 case HD_CLOSE_B1CHANNEL_ACK:
558 bcs = cs->bcs + channel;
559 update_basstate(ucs, 0, BS_B1OPEN << channel);
560 stopurbs(bcs->hw.bas);
561 gigaset_bchannel_down(bcs);
564 case HD_CLOSE_ATCHANNEL_ACK:
565 update_basstate(ucs, 0, BS_ATOPEN);
568 case HD_B2_FLOW_CONTROL:
570 case HD_B1_FLOW_CONTROL:
571 bcs = cs->bcs + channel;
572 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
573 &bcs->hw.bas->corrbytes);
575 "Flow control (channel %d, sub %d): 0x%02x => %d",
576 channel, bcs->hw.bas->numsub, l,
577 atomic_read(&bcs->hw.bas->corrbytes));
580 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
582 warn("HD_RECEIVEATDATA_ACK with length 0 ignored");
585 spin_lock_irqsave(&cs->lock, flags);
586 if (ucs->rcvbuf_size) {
587 spin_unlock_irqrestore(&cs->lock, flags);
588 err("receive AT data overrun, %d bytes lost", l);
589 error_reset(cs); //FIXME reschedule
592 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
593 spin_unlock_irqrestore(&cs->lock, flags);
594 err("%s: out of memory, %d bytes lost", __func__, l);
595 error_reset(cs); //FIXME reschedule
598 ucs->rcvbuf_size = l;
599 ucs->retry_cmd_in = 0;
600 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
603 ucs->rcvbuf_size = 0;
604 error_reset(cs); //FIXME reschedule
606 spin_unlock_irqrestore(&cs->lock, flags);
609 case HD_RESET_INTERRUPT_PIPE_ACK:
610 dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
614 dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
618 warn("unknown Gigaset signal 0x%02x (%u) ignored",
619 (int) ucs->int_in_buf[0], l);
625 status = usb_submit_urb(urb, SLAB_ATOMIC);
626 if (unlikely(status)) {
627 err("could not resubmit interrupt URB: %s",
628 get_usb_statmsg(status));
633 /* read_ctrl_callback
634 * USB completion handler for control pipe input
635 * called by the USB subsystem in interrupt context
637 * urb USB request block
638 * urb->context = inbuf structure for controller state
640 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
642 struct cardstate *cs;
643 struct bas_cardstate *ucs;
646 struct inbuf_t *inbuf;
650 inbuf = (struct inbuf_t *) urb->context;
657 spin_lock_irqsave(&cs->lock, flags);
658 if (!atomic_read(&cs->connected)) {
659 warn("%s: disconnected", __func__);
660 spin_unlock_irqrestore(&cs->lock, flags);
664 if (!ucs->rcvbuf_size) {
665 warn("%s: no receive in progress", __func__);
666 spin_unlock_irqrestore(&cs->lock, flags);
670 del_timer(&ucs->timer_cmd_in);
672 switch (urb->status) {
673 case 0: /* normal completion */
674 numbytes = urb->actual_length;
675 if (unlikely(numbytes == 0)) {
676 warn("control read: empty block received");
679 if (unlikely(numbytes != ucs->rcvbuf_size)) {
680 warn("control read: received %d chars, expected %d",
681 numbytes, ucs->rcvbuf_size);
682 if (numbytes > ucs->rcvbuf_size)
683 numbytes = ucs->rcvbuf_size;
686 /* copy received bytes to inbuf */
687 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
689 if (unlikely(numbytes < ucs->rcvbuf_size)) {
690 /* incomplete - resubmit for remaining bytes */
691 ucs->rcvbuf_size -= numbytes;
692 ucs->retry_cmd_in = 0;
697 case -ENOENT: /* canceled */
698 case -ECONNRESET: /* canceled (async) */
699 case -EINPROGRESS: /* pending */
700 /* no action necessary */
702 "%s: %s", __func__, get_usb_statmsg(urb->status));
705 default: /* severe trouble */
706 warn("control read: %s", get_usb_statmsg(urb->status));
708 if (ucs->retry_cmd_in++ < BAS_RETRY) {
709 notice("control read: retry %d", ucs->retry_cmd_in);
710 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
711 /* resubmitted - bypass regular exit block */
712 spin_unlock_irqrestore(&cs->lock, flags);
716 err("control read: giving up after %d tries",
724 ucs->rcvbuf_size = 0;
725 spin_unlock_irqrestore(&cs->lock, flags);
727 dbg(DEBUG_INTR, "%s-->BH", __func__);
728 gigaset_schedule_event(cs);
733 * USB completion handler for B channel isochronous input
734 * called by the USB subsystem in interrupt context
736 * urb USB request block of completed request
737 * urb->context = bc_state structure
739 static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
741 struct bc_state *bcs;
742 struct bas_bc_state *ubc;
747 IFNULLRET(urb->context);
748 IFNULLRET(cardstate);
750 /* status codes not worth bothering the tasklet with */
751 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
752 urb->status == -EINPROGRESS)) {
754 "%s: %s", __func__, get_usb_statmsg(urb->status));
758 bcs = (struct bc_state *) urb->context;
762 spin_lock_irqsave(&ubc->isoinlock, flags);
763 if (likely(ubc->isoindone == NULL)) {
764 /* pass URB to tasklet */
765 ubc->isoindone = urb;
766 tasklet_schedule(&ubc->rcvd_tasklet);
768 /* tasklet still busy, drop data and resubmit URB */
769 ubc->loststatus = urb->status;
770 for (i = 0; i < BAS_NUMFRAMES; i++) {
771 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
772 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
773 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
774 ubc->loststatus = urb->iso_frame_desc[i].status;
776 urb->iso_frame_desc[i].status = 0;
777 urb->iso_frame_desc[i].actual_length = 0;
779 if (likely(atomic_read(&ubc->running))) {
780 urb->dev = bcs->cs->hw.bas->udev; /* clobbered by USB subsystem */
781 urb->transfer_flags = URB_ISO_ASAP;
782 urb->number_of_packets = BAS_NUMFRAMES;
783 dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit", __func__);
784 rc = usb_submit_urb(urb, SLAB_ATOMIC);
785 if (unlikely(rc != 0)) {
786 err("could not resubmit isochronous read URB: %s",
787 get_usb_statmsg(rc));
788 dump_urb(DEBUG_ISO, "isoc read", urb);
793 spin_unlock_irqrestore(&ubc->isoinlock, flags);
796 /* write_iso_callback
797 * USB completion handler for B channel isochronous output
798 * called by the USB subsystem in interrupt context
800 * urb USB request block of completed request
801 * urb->context = isow_urbctx_t structure
803 static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
805 struct isow_urbctx_t *ucx;
806 struct bas_bc_state *ubc;
810 IFNULLRET(urb->context);
811 IFNULLRET(cardstate);
813 /* status codes not worth bothering the tasklet with */
814 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
815 urb->status == -EINPROGRESS)) {
817 "%s: %s", __func__, get_usb_statmsg(urb->status));
821 /* pass URB context to tasklet */
822 ucx = (struct isow_urbctx_t *) urb->context;
824 ubc = ucx->bcs->hw.bas;
827 spin_lock_irqsave(&ubc->isooutlock, flags);
828 ubc->isooutovfl = ubc->isooutdone;
829 ubc->isooutdone = ucx;
830 spin_unlock_irqrestore(&ubc->isooutlock, flags);
831 tasklet_schedule(&ubc->sent_tasklet);
835 * prepare and submit USB request blocks for isochronous input and output
837 * B channel control structure
840 * < 0 on error (no URBs submitted)
842 static int starturbs(struct bc_state *bcs)
845 struct bas_bc_state *ubc;
849 IFNULLRETVAL(bcs, -EFAULT);
851 IFNULLRETVAL(ubc, -EFAULT);
853 /* initialize L2 reception */
854 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
855 bcs->inputstate |= INS_flag_hunt;
857 /* submit all isochronous input URBs */
858 atomic_set(&ubc->running, 1);
859 for (k = 0; k < BAS_INURBS; k++) {
860 urb = ubc->isoinurbs[k];
862 err("isoinurbs[%d]==NULL", k);
867 urb->dev = bcs->cs->hw.bas->udev;
868 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
869 urb->transfer_flags = URB_ISO_ASAP;
870 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
871 urb->transfer_buffer_length = BAS_INBUFSIZE;
872 urb->number_of_packets = BAS_NUMFRAMES;
873 urb->interval = BAS_FRAMETIME;
874 urb->complete = read_iso_callback;
876 for (j = 0; j < BAS_NUMFRAMES; j++) {
877 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
878 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
879 urb->iso_frame_desc[j].status = 0;
880 urb->iso_frame_desc[j].actual_length = 0;
883 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
884 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
885 err("could not submit isochronous read URB %d: %s",
886 k, get_usb_statmsg(rc));
891 /* initialize L2 transmission */
892 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
894 /* set up isochronous output URBs for flag idling */
895 for (k = 0; k < BAS_OUTURBS; ++k) {
896 urb = ubc->isoouturbs[k].urb;
898 err("isoouturbs[%d].urb==NULL", k);
902 urb->dev = bcs->cs->hw.bas->udev;
903 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
904 urb->transfer_flags = URB_ISO_ASAP;
905 urb->transfer_buffer = ubc->isooutbuf->data;
906 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
907 urb->number_of_packets = BAS_NUMFRAMES;
908 urb->interval = BAS_FRAMETIME;
909 urb->complete = write_iso_callback;
910 urb->context = &ubc->isoouturbs[k];
911 for (j = 0; j < BAS_NUMFRAMES; ++j) {
912 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
913 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
914 urb->iso_frame_desc[j].status = 0;
915 urb->iso_frame_desc[j].actual_length = 0;
917 ubc->isoouturbs[k].limit = -1;
920 /* submit two URBs, keep third one */
921 for (k = 0; k < 2; ++k) {
922 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
923 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
925 err("could not submit isochronous write URB %d: %s",
926 k, get_usb_statmsg(rc));
930 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
931 ubc->isooutfree = &ubc->isoouturbs[2];
932 ubc->isooutdone = ubc->isooutovfl = NULL;
940 * cancel the USB request blocks for isochronous input and output
941 * errors are silently ignored
943 * B channel control structure
945 static void stopurbs(struct bas_bc_state *ubc)
951 atomic_set(&ubc->running, 0);
953 for (k = 0; k < BAS_INURBS; ++k) {
954 rc = usb_unlink_urb(ubc->isoinurbs[k]);
955 dbg(DEBUG_ISO, "%s: isoc input URB %d unlinked, result = %d",
959 for (k = 0; k < BAS_OUTURBS; ++k) {
960 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
961 dbg(DEBUG_ISO, "%s: isoc output URB %d unlinked, result = %d",
966 /* Isochronous Write - Bottom Half */
967 /* =============================== */
969 /* submit_iso_write_urb
970 * fill and submit the next isochronous write URB
972 * bcs B channel state structure
974 * number of frames submitted in URB
975 * 0 if URB not submitted because no data available (isooutbuf busy)
976 * error code < 0 on error
978 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
981 struct bas_bc_state *ubc;
982 struct usb_iso_packet_descriptor *ifd;
983 int corrbytes, nframe, rc;
985 IFNULLRETVAL(ucx, -EFAULT);
987 IFNULLRETVAL(urb, -EFAULT);
988 IFNULLRETVAL(ucx->bcs, -EFAULT);
989 ubc = ucx->bcs->hw.bas;
990 IFNULLRETVAL(ubc, -EFAULT);
992 urb->dev = ucx->bcs->cs->hw.bas->udev; /* clobbered by USB subsystem */
993 urb->transfer_flags = URB_ISO_ASAP;
994 urb->transfer_buffer = ubc->isooutbuf->data;
995 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
997 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
998 ifd = &urb->iso_frame_desc[nframe];
1000 /* compute frame length according to flow control */
1001 ifd->length = BAS_NORMFRAME;
1002 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
1003 dbg(DEBUG_ISO, "%s: corrbytes=%d", __func__, corrbytes);
1004 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1005 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1006 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1007 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1008 ifd->length += corrbytes;
1009 atomic_add(-corrbytes, &ubc->corrbytes);
1011 //dbg(DEBUG_ISO, "%s: frame %d length=%d", __func__, nframe, ifd->length);
1013 /* retrieve block of data to send */
1014 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1015 if (ifd->offset < 0) {
1016 if (ifd->offset == -EBUSY) {
1017 dbg(DEBUG_ISO, "%s: buffer busy at frame %d",
1019 /* tasklet will be restarted from gigaset_send_skb() */
1021 err("%s: buffer error %d at frame %d",
1022 __func__, ifd->offset, nframe);
1027 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1029 ifd->actual_length = 0;
1031 if ((urb->number_of_packets = nframe) > 0) {
1032 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1033 err("could not submit isochronous write URB: %s",
1034 get_usb_statmsg(rc));
1035 dump_urb(DEBUG_ISO, "isoc write", urb);
1043 /* write_iso_tasklet
1044 * tasklet scheduled when an isochronous output URB from the Gigaset device
1047 * data B channel state structure
1049 static void write_iso_tasklet(unsigned long data)
1051 struct bc_state *bcs;
1052 struct bas_bc_state *ubc;
1053 struct cardstate *cs;
1054 struct isow_urbctx_t *done, *next, *ovfl;
1056 struct usb_iso_packet_descriptor *ifd;
1058 unsigned long flags;
1060 struct sk_buff *skb;
1063 bcs = (struct bc_state *) data;
1070 /* loop while completed URBs arrive in time */
1072 if (unlikely(!atomic_read(&cs->connected))) {
1073 warn("%s: disconnected", __func__);
1077 if (unlikely(!(atomic_read(&ubc->running)))) {
1078 dbg(DEBUG_ISO, "%s: not running", __func__);
1082 /* retrieve completed URBs */
1083 spin_lock_irqsave(&ubc->isooutlock, flags);
1084 done = ubc->isooutdone;
1085 ubc->isooutdone = NULL;
1086 ovfl = ubc->isooutovfl;
1087 ubc->isooutovfl = NULL;
1088 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1090 err("isochronous write buffer underrun - buy a faster machine :-)");
1097 /* submit free URB if available */
1098 spin_lock_irqsave(&ubc->isooutlock, flags);
1099 next = ubc->isooutfree;
1100 ubc->isooutfree = NULL;
1101 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1103 if (submit_iso_write_urb(next) <= 0) {
1104 /* could not submit URB, put it back */
1105 spin_lock_irqsave(&ubc->isooutlock, flags);
1106 if (ubc->isooutfree == NULL) {
1107 ubc->isooutfree = next;
1110 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1112 /* couldn't put it back */
1113 err("losing isochronous write URB");
1119 /* process completed URB */
1121 switch (urb->status) {
1122 case 0: /* normal completion */
1124 case -EXDEV: /* inspect individual frames */
1125 /* assumptions (for lack of documentation):
1126 * - actual_length bytes of the frame in error are successfully sent
1127 * - all following frames are not sent at all
1129 dbg(DEBUG_ISO, "%s: URB partially completed", __func__);
1130 offset = done->limit; /* just in case */
1131 for (i = 0; i < BAS_NUMFRAMES; i++) {
1132 ifd = &urb->iso_frame_desc[i];
1134 ifd->actual_length != ifd->length) {
1135 warn("isochronous write: frame %d: %s, "
1136 "only %d of %d bytes sent",
1137 i, get_usb_statmsg(ifd->status),
1138 ifd->actual_length, ifd->length);
1139 offset = (ifd->offset +
1145 #ifdef CONFIG_GIGASET_DEBUG
1146 /* check assumption on remaining frames */
1147 for (; i < BAS_NUMFRAMES; i++) {
1148 ifd = &urb->iso_frame_desc[i];
1149 if (ifd->status != -EINPROGRESS
1150 || ifd->actual_length != 0) {
1151 warn("isochronous write: frame %d: %s, "
1152 "%d of %d bytes sent",
1153 i, get_usb_statmsg(ifd->status),
1154 ifd->actual_length, ifd->length);
1155 offset = (ifd->offset +
1163 case -EPIPE: //FIXME is this the code for "underrun"?
1164 err("isochronous write stalled");
1167 default: /* severe trouble */
1168 warn("isochronous write: %s",
1169 get_usb_statmsg(urb->status));
1172 /* mark the write buffer area covered by this URB as free */
1173 if (done->limit >= 0)
1174 atomic_set(&ubc->isooutbuf->read, done->limit);
1176 /* mark URB as free */
1177 spin_lock_irqsave(&ubc->isooutlock, flags);
1178 next = ubc->isooutfree;
1179 ubc->isooutfree = done;
1180 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1182 /* only one URB still active - resubmit one */
1183 if (submit_iso_write_urb(next) <= 0) {
1184 /* couldn't submit */
1190 /* process queued SKBs */
1191 while ((skb = skb_dequeue(&bcs->squeue))) {
1192 /* copy to output buffer, doing L2 encapsulation */
1194 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1195 /* insufficient buffer space, push back onto queue */
1196 skb_queue_head(&bcs->squeue, skb);
1197 dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1198 __func__, skb_queue_len(&bcs->squeue));
1202 gigaset_skb_sent(bcs, skb);
1203 dev_kfree_skb_any(skb);
1207 /* Isochronous Read - Bottom Half */
1208 /* ============================== */
1211 * tasklet scheduled when an isochronous input URB from the Gigaset device
1214 * data B channel state structure
1216 static void read_iso_tasklet(unsigned long data)
1218 struct bc_state *bcs;
1219 struct bas_bc_state *ubc;
1220 struct cardstate *cs;
1223 unsigned long flags;
1224 int totleft, numbytes, offset, frame, rc;
1226 bcs = (struct bc_state *) data;
1233 /* loop while more completed URBs arrive in the meantime */
1235 if (!atomic_read(&cs->connected)) {
1236 warn("%s: disconnected", __func__);
1241 spin_lock_irqsave(&ubc->isoinlock, flags);
1242 if (!(urb = ubc->isoindone)) {
1243 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1246 ubc->isoindone = NULL;
1247 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1248 warn("isochronous read overrun, dropped URB with status: %s, %d bytes lost",
1249 get_usb_statmsg(ubc->loststatus), ubc->isoinlost);
1250 ubc->loststatus = -EINPROGRESS;
1252 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1254 if (unlikely(!(atomic_read(&ubc->running)))) {
1255 dbg(DEBUG_ISO, "%s: channel not running, dropped URB with status: %s",
1256 __func__, get_usb_statmsg(urb->status));
1260 switch (urb->status) {
1261 case 0: /* normal completion */
1263 case -EXDEV: /* inspect individual frames (we do that anyway) */
1264 dbg(DEBUG_ISO, "%s: URB partially completed", __func__);
1268 dbg(DEBUG_ISO, "%s: URB canceled", __func__);
1269 continue; /* -> skip */
1270 case -EINPROGRESS: /* huh? */
1271 dbg(DEBUG_ISO, "%s: URB still pending", __func__);
1272 continue; /* -> skip */
1274 err("isochronous read stalled");
1276 continue; /* -> skip */
1277 default: /* severe trouble */
1278 warn("isochronous read: %s",
1279 get_usb_statmsg(urb->status));
1283 rcvbuf = urb->transfer_buffer;
1284 totleft = urb->actual_length;
1285 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1286 if (unlikely(urb->iso_frame_desc[frame].status)) {
1287 warn("isochronous read: frame %d: %s",
1288 frame, get_usb_statmsg(urb->iso_frame_desc[frame].status));
1291 numbytes = urb->iso_frame_desc[frame].actual_length;
1292 if (unlikely(numbytes > BAS_MAXFRAME)) {
1293 warn("isochronous read: frame %d: numbytes (%d) > BAS_MAXFRAME",
1297 if (unlikely(numbytes > totleft)) {
1298 warn("isochronous read: frame %d: numbytes (%d) > totleft (%d)",
1299 frame, numbytes, totleft);
1302 offset = urb->iso_frame_desc[frame].offset;
1303 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1304 warn("isochronous read: frame %d: offset (%d) + numbytes (%d) > BAS_INBUFSIZE",
1305 frame, offset, numbytes);
1308 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1309 totleft -= numbytes;
1311 if (unlikely(totleft > 0))
1312 warn("isochronous read: %d data bytes missing",
1316 /* URB processed, resubmit */
1317 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1318 urb->iso_frame_desc[frame].status = 0;
1319 urb->iso_frame_desc[frame].actual_length = 0;
1321 urb->dev = bcs->cs->hw.bas->udev; /* clobbered by USB subsystem */
1322 urb->transfer_flags = URB_ISO_ASAP;
1323 urb->number_of_packets = BAS_NUMFRAMES;
1324 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1325 err("could not resubmit isochronous read URB: %s",
1326 get_usb_statmsg(rc));
1327 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1333 /* Channel Operations */
1334 /* ================== */
1337 * timeout routine for control output request
1339 * B channel control structure
1341 static void req_timeout(unsigned long data)
1343 struct bc_state *bcs = (struct bc_state *) data;
1344 struct bas_cardstate *ucs;
1346 unsigned long flags;
1350 ucs = bcs->cs->hw.bas;
1355 spin_lock_irqsave(&ucs->lock, flags);
1356 pending = ucs->pending;
1358 spin_unlock_irqrestore(&ucs->lock, flags);
1361 case 0: /* no pending request */
1362 dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1365 case HD_OPEN_ATCHANNEL:
1366 err("timeout opening AT channel");
1367 error_reset(bcs->cs);
1370 case HD_OPEN_B2CHANNEL:
1371 case HD_OPEN_B1CHANNEL:
1372 err("timeout opening channel %d", bcs->channel + 1);
1376 case HD_CLOSE_ATCHANNEL:
1377 err("timeout closing AT channel");
1378 //wake_up_interruptible(cs->initwait);
1379 //FIXME need own wait queue?
1382 case HD_CLOSE_B2CHANNEL:
1383 case HD_CLOSE_B1CHANNEL:
1384 err("timeout closing channel %d", bcs->channel + 1);
1388 warn("request 0x%02x timed out, clearing", pending);
1392 /* write_ctrl_callback
1393 * USB completion handler for control pipe output
1394 * called by the USB subsystem in interrupt context
1396 * urb USB request block of completed request
1397 * urb->context = hardware specific controller state structure
1399 static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1401 struct bas_cardstate *ucs;
1402 unsigned long flags;
1405 IFNULLRET(urb->context);
1406 IFNULLRET(cardstate);
1408 ucs = (struct bas_cardstate *) urb->context;
1409 spin_lock_irqsave(&ucs->lock, flags);
1410 if (urb->status && ucs->pending) {
1411 err("control request 0x%02x failed: %s",
1412 ucs->pending, get_usb_statmsg(urb->status));
1413 del_timer(&ucs->timer_ctrl);
1416 /* individual handling of specific request types */
1417 switch (ucs->pending) {
1418 case HD_DEVICE_INIT_ACK: /* no reply expected */
1422 spin_unlock_irqrestore(&ucs->lock, flags);
1426 * submit a control output request without message buffer to the Gigaset base
1427 * and optionally start a timeout
1429 * bcs B channel control structure
1430 * req control request code (HD_*)
1431 * val control request parameter value (set to 0 if unused)
1432 * timeout timeout in seconds (0: no timeout)
1435 * -EINVAL if a NULL pointer is encountered somewhere
1436 * -EBUSY if another request is pending
1437 * any URB submission error code
1439 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1441 struct bas_cardstate *ucs;
1443 unsigned long flags;
1445 IFNULLRETVAL(bcs, -EINVAL);
1446 IFNULLRETVAL(bcs->cs, -EINVAL);
1447 ucs = bcs->cs->hw.bas;
1448 IFNULLRETVAL(ucs, -EINVAL);
1449 IFNULLRETVAL(ucs->urb_ctrl, -EINVAL);
1451 dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1453 spin_lock_irqsave(&ucs->lock, flags);
1455 spin_unlock_irqrestore(&ucs->lock, flags);
1456 err("submission of request 0x%02x failed: request 0x%02x still pending",
1460 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1461 spin_unlock_irqrestore(&ucs->lock, flags);
1462 err("could not submit request 0x%02x: URB busy", req);
1466 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1467 ucs->dr_ctrl.bRequest = req;
1468 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1469 ucs->dr_ctrl.wIndex = 0;
1470 ucs->dr_ctrl.wLength = 0;
1471 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1472 usb_sndctrlpipe(ucs->udev, 0),
1473 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1474 write_ctrl_callback, ucs);
1475 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
1476 err("could not submit request 0x%02x: %s",
1477 req, get_usb_statmsg(ret));
1478 spin_unlock_irqrestore(&ucs->lock, flags);
1484 dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1485 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1486 ucs->timer_ctrl.data = (unsigned long) bcs;
1487 ucs->timer_ctrl.function = req_timeout;
1488 add_timer(&ucs->timer_ctrl);
1491 spin_unlock_irqrestore(&ucs->lock, flags);
1495 /* gigaset_init_bchannel
1496 * called by common.c to connect a B channel
1497 * initialize isochronous I/O and tell the Gigaset base to open the channel
1499 * B channel control structure
1501 * 0 on success, error code < 0 on error
1503 static int gigaset_init_bchannel(struct bc_state *bcs)
1507 IFNULLRETVAL(bcs, -EINVAL);
1509 if ((ret = starturbs(bcs)) < 0) {
1510 err("could not start isochronous I/O for channel %d",
1516 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1517 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1518 err("could not open channel %d: %s",
1519 bcs->channel + 1, get_usb_statmsg(ret));
1520 stopurbs(bcs->hw.bas);
1526 /* gigaset_close_bchannel
1527 * called by common.c to disconnect a B channel
1528 * tell the Gigaset base to close the channel
1529 * stopping isochronous I/O and LL notification will be done when the
1530 * acknowledgement for the close arrives
1532 * B channel control structure
1534 * 0 on success, error code < 0 on error
1536 static int gigaset_close_bchannel(struct bc_state *bcs)
1540 IFNULLRETVAL(bcs, -EINVAL);
1542 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1543 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1544 /* channel not running: just signal common.c */
1545 gigaset_bchannel_down(bcs);
1549 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1550 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1551 err("could not submit HD_CLOSE_BxCHANNEL request: %s",
1552 get_usb_statmsg(ret));
1556 /* Device Operations */
1557 /* ================= */
1560 * unqueue first command buffer from queue, waking any sleepers
1561 * must be called with cs->cmdlock held
1563 * cs controller state structure
1565 static void complete_cb(struct cardstate *cs)
1567 struct cmdbuf_t *cb;
1573 /* unqueue completed buffer */
1574 cs->cmdbytes -= cs->curlen;
1575 dbg(DEBUG_TRANSCMD | DEBUG_LOCKCMD,
1576 "write_command: sent %u bytes, %u left",
1577 cs->curlen, cs->cmdbytes);
1578 if ((cs->cmdbuf = cb->next) != NULL) {
1579 cs->cmdbuf->prev = NULL;
1580 cs->curlen = cs->cmdbuf->len;
1582 cs->lastcmdbuf = NULL;
1586 if (cb->wake_tasklet)
1587 tasklet_schedule(cb->wake_tasklet);
1592 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1594 /* write_command_callback
1595 * USB completion handler for AT command transmission
1596 * called by the USB subsystem in interrupt context
1598 * urb USB request block of completed request
1599 * urb->context = controller state structure
1601 static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1603 struct cardstate *cs;
1604 unsigned long flags;
1605 struct bas_cardstate *ucs;
1608 cs = (struct cardstate *) urb->context;
1614 switch (urb->status) {
1615 case 0: /* normal completion */
1617 case -ENOENT: /* canceled */
1618 case -ECONNRESET: /* canceled (async) */
1619 case -EINPROGRESS: /* pending */
1620 /* ignore silently */
1622 "%s: %s", __func__, get_usb_statmsg(urb->status));
1624 default: /* any failure */
1625 if (++ucs->retry_cmd_out > BAS_RETRY) {
1626 warn("command write: %s, giving up after %d retries",
1627 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1630 if (cs->cmdbuf == NULL) {
1631 warn("command write: %s, cannot retry - cmdbuf gone",
1632 get_usb_statmsg(urb->status));
1635 notice("command write: %s, retry %d",
1636 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1637 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1638 /* resubmitted - bypass regular exit block */
1640 /* command send failed, assume base still waiting */
1641 update_basstate(ucs, BS_ATREADY, 0);
1644 spin_lock_irqsave(&cs->cmdlock, flags);
1645 if (cs->cmdbuf != NULL)
1647 spin_unlock_irqrestore(&cs->cmdlock, flags);
1651 * timeout routine for AT command transmission
1653 * controller state structure
1655 static void atrdy_timeout(unsigned long data)
1657 struct cardstate *cs = (struct cardstate *) data;
1658 struct bas_cardstate *ucs;
1664 warn("timeout waiting for HD_READY_SEND_ATDATA");
1666 /* fake the missing signal - what else can I do? */
1667 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1672 * submit an HD_WRITE_ATMESSAGE command URB
1674 * cs controller state structure
1675 * buf buffer containing command to send
1676 * len length of command to send
1679 * -EFAULT if a NULL pointer is encountered somewhere
1680 * -EBUSY if another request is pending
1681 * any URB submission error code
1683 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1685 struct bas_cardstate *ucs;
1688 IFNULLRETVAL(cs, -EFAULT);
1690 IFNULLRETVAL(ucs, -EFAULT);
1691 IFNULLRETVAL(ucs->urb_cmd_out, -EFAULT);
1693 dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1695 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
1696 err("could not submit HD_WRITE_ATMESSAGE: URB busy");
1700 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1701 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1702 ucs->dr_cmd_out.wValue = 0;
1703 ucs->dr_cmd_out.wIndex = 0;
1704 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1705 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1706 usb_sndctrlpipe(ucs->udev, 0),
1707 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1708 write_command_callback, cs);
1710 if ((ret = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC)) != 0) {
1711 err("could not submit HD_WRITE_ATMESSAGE: %s",
1712 get_usb_statmsg(ret));
1716 /* submitted successfully */
1717 update_basstate(ucs, 0, BS_ATREADY);
1719 /* start timeout if necessary */
1720 if (!(atomic_read(&ucs->basstate) & BS_ATTIMER)) {
1722 "setting ATREADY timeout of %d/10 secs", ATRDY_TIMEOUT);
1723 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1724 ucs->timer_atrdy.data = (unsigned long) cs;
1725 ucs->timer_atrdy.function = atrdy_timeout;
1726 add_timer(&ucs->timer_atrdy);
1727 update_basstate(ucs, BS_ATTIMER, 0);
1733 * start transmission of AT command queue if necessary
1735 * cs controller state structure
1738 * error code < 0 on error
1740 static int start_cbsend(struct cardstate *cs)
1742 struct cmdbuf_t *cb;
1743 struct bas_cardstate *ucs;
1744 unsigned long flags;
1748 IFNULLRETVAL(cs, -EFAULT);
1750 IFNULLRETVAL(ucs, -EFAULT);
1752 /* check if AT channel is open */
1753 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
1754 dbg(DEBUG_TRANSCMD | DEBUG_LOCKCMD, "AT channel not open");
1755 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1757 err("could not open AT channel");
1758 /* flush command queue */
1759 spin_lock_irqsave(&cs->cmdlock, flags);
1760 while (cs->cmdbuf != NULL)
1762 spin_unlock_irqrestore(&cs->cmdlock, flags);
1767 /* try to send first command in queue */
1768 spin_lock_irqsave(&cs->cmdlock, flags);
1770 while ((cb = cs->cmdbuf) != NULL &&
1771 atomic_read(&ucs->basstate) & BS_ATREADY) {
1772 ucs->retry_cmd_out = 0;
1773 rc = atwrite_submit(cs, cb->buf, cb->len);
1780 spin_unlock_irqrestore(&cs->cmdlock, flags);
1784 /* gigaset_write_cmd
1785 * This function is called by the device independent part of the driver
1786 * to transmit an AT command string to the Gigaset device.
1787 * It encapsulates the device specific method for transmission over the
1788 * direct USB connection to the base.
1789 * The command string is added to the queue of commands to send, and
1790 * USB transmission is started if necessary.
1792 * cs controller state structure
1793 * buf command string to send
1794 * len number of bytes to send (max. IF_WRITEBUF)
1795 * wake_tasklet tasklet to run when transmission is completed (NULL if none)
1797 * number of bytes queued on success
1798 * error code < 0 on error
1800 static int gigaset_write_cmd(struct cardstate *cs,
1801 const unsigned char *buf, int len,
1802 struct tasklet_struct *wake_tasklet)
1804 struct cmdbuf_t *cb;
1805 unsigned long flags;
1808 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
1809 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1810 "CMD Transmit", len, buf, 0);
1812 if (!atomic_read(&cs->connected)) {
1813 err("%s: not connected", __func__);
1818 return 0; /* nothing to do */
1820 if (len > IF_WRITEBUF)
1822 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1823 err("%s: out of memory", __func__);
1827 memcpy(cb->buf, buf, len);
1831 cb->wake_tasklet = wake_tasklet;
1833 spin_lock_irqsave(&cs->cmdlock, flags);
1834 cb->prev = cs->lastcmdbuf;
1836 cs->lastcmdbuf->next = cb;
1841 cs->cmdbytes += len;
1842 cs->lastcmdbuf = cb;
1843 spin_unlock_irqrestore(&cs->cmdlock, flags);
1845 status = start_cbsend(cs);
1847 return status < 0 ? status : len;
1850 /* gigaset_write_room
1851 * tty_driver.write_room interface routine
1852 * return number of characters the driver will accept to be written via gigaset_write_cmd
1854 * controller state structure
1856 * number of characters
1858 static int gigaset_write_room(struct cardstate *cs)
1863 /* gigaset_chars_in_buffer
1864 * tty_driver.chars_in_buffer interface routine
1865 * return number of characters waiting to be sent
1867 * controller state structure
1869 * number of characters
1871 static int gigaset_chars_in_buffer(struct cardstate *cs)
1873 unsigned long flags;
1876 spin_lock_irqsave(&cs->cmdlock, flags);
1877 bytes = cs->cmdbytes;
1878 spin_unlock_irqrestore(&cs->cmdlock, flags);
1884 * implementation of ioctl(GIGASET_BRKCHARS)
1886 * controller state structure
1888 * -EINVAL (unimplemented function)
1890 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1896 /* Device Initialization/Shutdown */
1897 /* ============================== */
1899 /* Free hardware dependent part of the B channel structure
1901 * bcs B channel structure
1905 static int gigaset_freebcshw(struct bc_state *bcs)
1910 if (bcs->hw.bas->isooutbuf)
1911 kfree(bcs->hw.bas->isooutbuf);
1917 /* Initialize hardware dependent part of the B channel structure
1919 * bcs B channel structure
1923 static int gigaset_initbcshw(struct bc_state *bcs)
1926 struct bas_bc_state *ubc;
1928 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1930 err("could not allocate bas_bc_state");
1934 atomic_set(&ubc->running, 0);
1935 atomic_set(&ubc->corrbytes, 0);
1936 spin_lock_init(&ubc->isooutlock);
1937 for (i = 0; i < BAS_OUTURBS; ++i) {
1938 ubc->isoouturbs[i].urb = NULL;
1939 ubc->isoouturbs[i].bcs = bcs;
1941 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
1943 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
1944 err("could not allocate isochronous output buffer");
1949 tasklet_init(&ubc->sent_tasklet,
1950 &write_iso_tasklet, (unsigned long) bcs);
1952 spin_lock_init(&ubc->isoinlock);
1953 for (i = 0; i < BAS_INURBS; ++i)
1954 ubc->isoinurbs[i] = NULL;
1955 ubc->isoindone = NULL;
1956 ubc->loststatus = -EINPROGRESS;
1970 tasklet_init(&ubc->rcvd_tasklet,
1971 &read_iso_tasklet, (unsigned long) bcs);
1975 static void gigaset_reinitbcshw(struct bc_state *bcs)
1977 struct bas_bc_state *ubc = bcs->hw.bas;
1979 atomic_set(&bcs->hw.bas->running, 0);
1980 atomic_set(&bcs->hw.bas->corrbytes, 0);
1981 bcs->hw.bas->numsub = 0;
1982 spin_lock_init(&ubc->isooutlock);
1983 spin_lock_init(&ubc->isoinlock);
1984 ubc->loststatus = -EINPROGRESS;
1987 static void gigaset_freecshw(struct cardstate *cs)
1989 struct bas_cardstate *ucs = cs->hw.bas;
1991 del_timer(&ucs->timer_ctrl);
1992 del_timer(&ucs->timer_atrdy);
1993 del_timer(&ucs->timer_cmd_in);
1998 static int gigaset_initcshw(struct cardstate *cs)
2000 struct bas_cardstate *ucs;
2002 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2006 ucs->urb_cmd_in = NULL;
2007 ucs->urb_cmd_out = NULL;
2009 ucs->rcvbuf_size = 0;
2011 spin_lock_init(&ucs->lock);
2014 atomic_set(&ucs->basstate, 0);
2015 init_timer(&ucs->timer_ctrl);
2016 init_timer(&ucs->timer_atrdy);
2017 init_timer(&ucs->timer_cmd_in);
2023 * unlink and deallocate all URBs unconditionally
2024 * caller must make sure that no commands are still in progress
2026 * cs controller state structure
2028 static void freeurbs(struct cardstate *cs)
2030 struct bas_cardstate *ucs;
2031 struct bas_bc_state *ubc;
2038 for (j = 0; j < 2; ++j) {
2039 ubc = cs->bcs[j].hw.bas;
2041 for (i = 0; i < BAS_OUTURBS; ++i)
2042 if (ubc->isoouturbs[i].urb) {
2043 usb_kill_urb(ubc->isoouturbs[i].urb);
2045 "%s: isoc output URB %d/%d unlinked",
2047 usb_free_urb(ubc->isoouturbs[i].urb);
2048 ubc->isoouturbs[i].urb = NULL;
2050 for (i = 0; i < BAS_INURBS; ++i)
2051 if (ubc->isoinurbs[i]) {
2052 usb_kill_urb(ubc->isoinurbs[i]);
2054 "%s: isoc input URB %d/%d unlinked",
2056 usb_free_urb(ubc->isoinurbs[i]);
2057 ubc->isoinurbs[i] = NULL;
2060 if (ucs->urb_int_in) {
2061 usb_kill_urb(ucs->urb_int_in);
2062 dbg(DEBUG_INIT, "%s: interrupt input URB unlinked", __func__);
2063 usb_free_urb(ucs->urb_int_in);
2064 ucs->urb_int_in = NULL;
2066 if (ucs->urb_cmd_out) {
2067 usb_kill_urb(ucs->urb_cmd_out);
2068 dbg(DEBUG_INIT, "%s: command output URB unlinked", __func__);
2069 usb_free_urb(ucs->urb_cmd_out);
2070 ucs->urb_cmd_out = NULL;
2072 if (ucs->urb_cmd_in) {
2073 usb_kill_urb(ucs->urb_cmd_in);
2074 dbg(DEBUG_INIT, "%s: command input URB unlinked", __func__);
2075 usb_free_urb(ucs->urb_cmd_in);
2076 ucs->urb_cmd_in = NULL;
2078 if (ucs->urb_ctrl) {
2079 usb_kill_urb(ucs->urb_ctrl);
2080 dbg(DEBUG_INIT, "%s: control output URB unlinked", __func__);
2081 usb_free_urb(ucs->urb_ctrl);
2082 ucs->urb_ctrl = NULL;
2087 * This function is called when a new USB device is connected.
2088 * It checks whether the new device is handled by this driver.
2090 static int gigaset_probe(struct usb_interface *interface,
2091 const struct usb_device_id *id)
2093 struct usb_host_interface *hostif;
2094 struct usb_device *udev = interface_to_usbdev(interface);
2095 struct cardstate *cs = NULL;
2096 struct bas_cardstate *ucs = NULL;
2097 struct bas_bc_state *ubc;
2098 struct usb_endpoint_descriptor *endpoint;
2102 IFNULLRETVAL(udev, -ENODEV);
2105 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2106 __func__, le16_to_cpu(udev->descriptor.idVendor),
2107 le16_to_cpu(udev->descriptor.idProduct));
2109 /* See if the device offered us matches what we can accept */
2110 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2111 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2112 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2113 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2114 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
2115 dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
2119 /* set required alternate setting */
2120 hostif = interface->cur_altsetting;
2121 if (hostif->desc.bAlternateSetting != 3) {
2123 "%s: wrong alternate setting %d - trying to switch",
2124 __func__, hostif->desc.bAlternateSetting);
2125 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2126 warn("usb_set_interface failed, device %d interface %d altsetting %d",
2127 udev->devnum, hostif->desc.bInterfaceNumber,
2128 hostif->desc.bAlternateSetting);
2131 hostif = interface->cur_altsetting;
2134 /* Reject application specific interfaces
2136 if (hostif->desc.bInterfaceClass != 255) {
2137 warn("%s: bInterfaceClass == %d",
2138 __func__, hostif->desc.bInterfaceClass);
2142 info("%s: Device matched (Vendor: 0x%x, Product: 0x%x)",
2143 __func__, le16_to_cpu(udev->descriptor.idVendor),
2144 le16_to_cpu(udev->descriptor.idProduct));
2146 cs = gigaset_getunassignedcs(driver);
2148 err("%s: no free cardstate", __func__);
2153 ucs->interface = interface;
2156 * - one for the interrupt pipe
2157 * - three for the different uses of the default control pipe
2158 * - three for each isochronous pipe
2160 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2161 if (!ucs->urb_int_in) {
2162 err("No free urbs available");
2165 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2166 if (!ucs->urb_cmd_in) {
2167 err("No free urbs available");
2170 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2171 if (!ucs->urb_cmd_out) {
2172 err("No free urbs available");
2175 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2176 if (!ucs->urb_ctrl) {
2177 err("No free urbs available");
2181 for (j = 0; j < 2; ++j) {
2182 ubc = cs->bcs[j].hw.bas;
2183 for (i = 0; i < BAS_OUTURBS; ++i) {
2184 ubc->isoouturbs[i].urb =
2185 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2186 if (!ubc->isoouturbs[i].urb) {
2187 err("No free urbs available");
2191 for (i = 0; i < BAS_INURBS; ++i) {
2193 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2194 if (!ubc->isoinurbs[i]) {
2195 err("No free urbs available");
2202 ucs->rcvbuf_size = 0;
2204 /* Fill the interrupt urb and send it to the core */
2205 endpoint = &hostif->endpoint[0].desc;
2206 usb_fill_int_urb(ucs->urb_int_in, udev,
2207 usb_rcvintpipe(udev,
2208 (endpoint->bEndpointAddress) & 0x0f),
2209 ucs->int_in_buf, 3, read_int_callback, cs,
2210 endpoint->bInterval);
2211 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2213 err("could not submit interrupt URB: %s", get_usb_statmsg(ret));
2217 /* tell the device that the driver is ready */
2218 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2221 /* tell common part that the device is ready */
2222 if (startmode == SM_LOCKED)
2223 atomic_set(&cs->mstate, MS_LOCKED);
2224 if (!gigaset_start(cs))
2227 /* save address of controller structure */
2228 usb_set_intfdata(interface, cs);
2230 /* set up device sysfs */
2231 gigaset_init_dev_sysfs(interface);
2236 gigaset_unassign(cs);
2240 /* gigaset_disconnect
2241 * This function is called when the Gigaset base is unplugged.
2243 static void gigaset_disconnect(struct usb_interface *interface)
2245 struct cardstate *cs;
2246 struct bas_cardstate *ucs;
2248 /* clear device sysfs */
2249 gigaset_free_dev_sysfs(interface);
2251 cs = usb_get_intfdata(interface);
2252 usb_set_intfdata(interface, NULL);
2258 info("disconnecting GigaSet base");
2263 ucs->rcvbuf_size = 0;
2264 atomic_set(&ucs->basstate, 0);
2265 gigaset_unassign(cs);
2268 static struct gigaset_ops gigops = {
2271 gigaset_chars_in_buffer,
2273 gigaset_init_bchannel,
2274 gigaset_close_bchannel,
2277 gigaset_reinitbcshw,
2280 gigaset_set_modem_ctrl,
2282 gigaset_set_line_ctrl,
2283 gigaset_isoc_send_skb,
2288 * This function is called after the kernel module is loaded.
2290 static int __init bas_gigaset_init(void)
2294 /* allocate memory for our driver state and intialize it */
2295 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2296 GIGASET_MODULENAME, GIGASET_DEVNAME,
2297 GIGASET_DEVFSNAME, &gigops,
2298 THIS_MODULE)) == NULL)
2301 /* allocate memory for our device state and intialize it */
2302 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode, GIGASET_MODULENAME);
2306 /* register this driver with the USB subsystem */
2307 result = usb_register(&gigaset_usb_driver);
2309 err("usb_register failed (error %d)", -result);
2313 info(DRIVER_AUTHOR);
2317 error: if (cardstate)
2318 gigaset_freecs(cardstate);
2321 gigaset_freedriver(driver);
2327 * This function is called before the kernel module is unloaded.
2329 static void __exit bas_gigaset_exit(void)
2331 gigaset_blockdriver(driver); /* => probe will fail
2332 * => no gigaset_start any more
2335 gigaset_shutdown(cardstate);
2336 /* from now on, no isdn callback should be possible */
2338 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
2339 dbg(DEBUG_ANY, "closing AT channel");
2340 if (req_submit(cardstate->bcs,
2341 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2342 /* successfully submitted - wait for completion */
2343 //wait_event_interruptible(cs->initwait, !cs->hw.bas->pending);
2344 //FIXME need own wait queue? wakeup?
2348 /* deregister this driver with the USB subsystem */
2349 usb_deregister(&gigaset_usb_driver);
2350 /* this will call the disconnect-callback */
2351 /* from now on, no disconnect/probe callback should be running */
2353 gigaset_freecs(cardstate);
2355 gigaset_freedriver(driver);
2360 module_init(bas_gigaset_init);
2361 module_exit(bas_gigaset_exit);
2363 MODULE_AUTHOR(DRIVER_AUTHOR);
2364 MODULE_DESCRIPTION(DRIVER_DESC);
2365 MODULE_LICENSE("GPL");