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 * =====================================================================
20 #include <linux/errno.h>
21 #include <linux/init.h>
22 #include <linux/slab.h>
23 #include <linux/timer.h>
24 #include <linux/usb.h>
25 #include <linux/module.h>
26 #include <linux/moduleparam.h>
28 /* Version Information */
29 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers <Eilers.Stefan@epost.de>"
30 #define DRIVER_DESC "USB Driver for Gigaset 307x"
33 /* Module parameters */
35 static int startmode = SM_ISDN;
36 static int cidmode = 1;
38 module_param(startmode, int, S_IRUGO);
39 module_param(cidmode, int, S_IRUGO);
40 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
41 MODULE_PARM_DESC(cidmode, "Call-ID mode");
43 #define GIGASET_MINORS 1
44 #define GIGASET_MINOR 16
45 #define GIGASET_MODULENAME "bas_gigaset"
46 #define GIGASET_DEVFSNAME "gig/bas/"
47 #define GIGASET_DEVNAME "ttyGB"
49 #define IF_WRITEBUF 256 //FIXME
51 /* Values for the Gigaset 307x */
52 #define USB_GIGA_VENDOR_ID 0x0681
53 #define USB_GIGA_PRODUCT_ID 0x0001
54 #define USB_4175_PRODUCT_ID 0x0002
55 #define USB_SX303_PRODUCT_ID 0x0021
56 #define USB_SX353_PRODUCT_ID 0x0022
58 /* table of devices that work with this driver */
59 static struct usb_device_id gigaset_table [] = {
60 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_GIGA_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_4175_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
63 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
64 { } /* Terminating entry */
67 MODULE_DEVICE_TABLE(usb, gigaset_table);
69 /*======================= local function prototypes =============================*/
71 /* This function is called if a new device is connected to the USB port. It
72 * checks whether this new device belongs to this driver.
74 static int gigaset_probe(struct usb_interface *interface,
75 const struct usb_device_id *id);
77 /* Function will be called if the device is unplugged */
78 static void gigaset_disconnect(struct usb_interface *interface);
81 /*==============================================================================*/
83 struct bas_cardstate {
84 struct usb_device *udev; /* USB device pointer */
85 struct usb_interface *interface; /* interface for this device */
86 unsigned char minor; /* starting minor number */
88 struct urb *urb_ctrl; /* control pipe default URB */
89 struct usb_ctrlrequest dr_ctrl;
90 struct timer_list timer_ctrl; /* control request timeout */
92 struct timer_list timer_atrdy; /* AT command ready timeout */
93 struct urb *urb_cmd_out; /* for sending AT commands */
94 struct usb_ctrlrequest dr_cmd_out;
97 struct urb *urb_cmd_in; /* for receiving AT replies */
98 struct usb_ctrlrequest dr_cmd_in;
99 struct timer_list timer_cmd_in; /* receive request timeout */
100 unsigned char *rcvbuf; /* AT reply receive buffer */
102 struct urb *urb_int_in; /* URB for interrupt pipe */
103 unsigned char int_in_buf[3];
105 spinlock_t lock; /* locks all following */
106 atomic_t basstate; /* bitmap (BS_*) */
107 int pending; /* uncompleted base request */
108 int rcvbuf_size; /* size of AT receive buffer */
109 /* 0: no receive in progress */
110 int retry_cmd_in; /* receive req retry count */
113 /* status of direct USB connection to 307x base (bits in basstate) */
114 #define BS_ATOPEN 0x001
115 #define BS_B1OPEN 0x002
116 #define BS_B2OPEN 0x004
117 #define BS_ATREADY 0x008
118 #define BS_INIT 0x010
119 #define BS_ATTIMER 0x020
122 static struct gigaset_driver *driver = NULL;
123 static struct cardstate *cardstate = NULL;
125 /* usb specific object needed to register this driver with the usb subsystem */
126 static struct usb_driver gigaset_usb_driver = {
127 .name = GIGASET_MODULENAME,
128 .probe = gigaset_probe,
129 .disconnect = gigaset_disconnect,
130 .id_table = gigaset_table,
133 /* get message text for USB status code
135 static char *get_usb_statmsg(int status)
137 static char unkmsg[28];
145 return "canceled (async)";
149 return "bit stuffing or unknown USB error";
151 return "Illegal byte sequence (CRC mismatch)";
153 return "babble detect or endpoint stalled";
155 return "buffer error";
159 return "device not present";
161 return "short packet detected";
163 return "partial isochronous transfer";
165 return "invalid argument";
167 return "URB already queued";
169 return "isochronous start frame too early or too much scheduled";
171 return "too many isochronous frames requested";
173 return "endpoint message size zero";
175 return "endpoint shutdown";
177 return "another request pending";
179 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", status);
185 * retrieve string representation of USB pipe type
187 static inline char *usb_pipetype_str(int pipe)
189 if (usb_pipeisoc(pipe))
191 if (usb_pipeint(pipe))
193 if (usb_pipecontrol(pipe))
195 if (usb_pipebulk(pipe))
201 * write content of URB to syslog for debugging
203 static inline void dump_urb(enum debuglevel level, const char *tag,
206 #ifdef CONFIG_GIGASET_DEBUG
209 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
212 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
213 "status=%d, hcpriv=0x%08lx, transfer_flags=0x%x,",
214 (unsigned long) urb->dev,
215 usb_pipetype_str(urb->pipe),
216 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
217 usb_pipein(urb->pipe) ? "in" : "out",
218 urb->status, (unsigned long) urb->hcpriv,
219 urb->transfer_flags);
221 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
222 "bandwidth=%d, setup_packet=0x%08lx,",
223 (unsigned long) urb->transfer_buffer,
224 urb->transfer_buffer_length, urb->actual_length,
225 urb->bandwidth, (unsigned long) urb->setup_packet);
227 " start_frame=%d, number_of_packets=%d, interval=%d, "
229 urb->start_frame, urb->number_of_packets, urb->interval,
232 " context=0x%08lx, complete=0x%08lx, "
233 "iso_frame_desc[]={",
234 (unsigned long) urb->context,
235 (unsigned long) urb->complete);
236 for (i = 0; i < urb->number_of_packets; i++) {
237 struct usb_iso_packet_descriptor *pifd
238 = &urb->iso_frame_desc[i];
240 " {offset=%u, length=%u, actual_length=%u, "
242 pifd->offset, pifd->length, pifd->actual_length,
246 gig_dbg(level, "}}");
250 /* read/set modem control bits etc. (m10x only) */
251 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
257 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
262 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
268 * hang up any existing connection because of an unrecoverable error
269 * This function may be called from any context and takes care of scheduling
270 * the necessary actions for execution outside of interrupt context.
272 * B channel control structure
274 static inline void error_hangup(struct bc_state *bcs)
276 struct cardstate *cs = bcs->cs;
278 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
279 __func__, bcs->channel);
281 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL)) {
282 //FIXME what should we do?
286 gigaset_schedule_event(cs);
290 * reset Gigaset device because of an unrecoverable error
291 * This function may be called from any context and takes care of scheduling
292 * the necessary actions for execution outside of interrupt context.
294 * controller state structure
296 static inline void error_reset(struct cardstate *cs)
298 //FIXME try to recover without bothering the user
300 "unrecoverable error - please disconnect Gigaset base to reset\n");
304 * check for completion of pending control request
306 * ucs hardware specific controller state structure
308 static void check_pending(struct bas_cardstate *ucs)
314 spin_lock_irqsave(&ucs->lock, flags);
315 switch (ucs->pending) {
318 case HD_OPEN_ATCHANNEL:
319 if (atomic_read(&ucs->basstate) & BS_ATOPEN)
322 case HD_OPEN_B1CHANNEL:
323 if (atomic_read(&ucs->basstate) & BS_B1OPEN)
326 case HD_OPEN_B2CHANNEL:
327 if (atomic_read(&ucs->basstate) & BS_B2OPEN)
330 case HD_CLOSE_ATCHANNEL:
331 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN))
334 case HD_CLOSE_B1CHANNEL:
335 if (!(atomic_read(&ucs->basstate) & BS_B1OPEN))
338 case HD_CLOSE_B2CHANNEL:
339 if (!(atomic_read(&ucs->basstate) & BS_B2OPEN))
342 case HD_DEVICE_INIT_ACK: /* no reply expected */
345 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
346 * are handled separately and should never end up here
349 dev_warn(&ucs->interface->dev,
350 "unknown pending request 0x%02x cleared\n",
356 del_timer(&ucs->timer_ctrl);
358 spin_unlock_irqrestore(&ucs->lock, flags);
362 * timeout routine for command input request
364 * controller state structure
366 static void cmd_in_timeout(unsigned long data)
368 struct cardstate *cs = (struct cardstate *) data;
369 struct bas_cardstate *ucs;
376 spin_lock_irqsave(&cs->lock, flags);
377 if (unlikely(!atomic_read(&cs->connected))) {
378 gig_dbg(DEBUG_USBREQ, "%s: disconnected", __func__);
379 spin_unlock_irqrestore(&cs->lock, flags);
382 if (!ucs->rcvbuf_size) {
383 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
384 spin_unlock_irqrestore(&cs->lock, flags);
387 spin_unlock_irqrestore(&cs->lock, flags);
389 dev_err(cs->dev, "timeout reading AT response\n");
390 error_reset(cs); //FIXME retry?
394 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs);
397 * submit an HD_READ_ATMESSAGE command URB
399 * cs controller state structure
400 * timeout timeout in 1/10 sec., 0: none
403 * -EINVAL if a NULL pointer is encountered somewhere
404 * -EBUSY if another request is pending
405 * any URB submission error code
407 static int atread_submit(struct cardstate *cs, int timeout)
409 struct bas_cardstate *ucs;
412 IFNULLRETVAL(cs, -EINVAL);
414 IFNULLRETVAL(ucs, -EINVAL);
415 IFNULLRETVAL(ucs->urb_cmd_in, -EINVAL);
417 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
420 if (ucs->urb_cmd_in->status == -EINPROGRESS) {
422 "could not submit HD_READ_ATMESSAGE: URB busy\n");
426 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
427 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
428 ucs->dr_cmd_in.wValue = 0;
429 ucs->dr_cmd_in.wIndex = 0;
430 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
431 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
432 usb_rcvctrlpipe(ucs->udev, 0),
433 (unsigned char*) & ucs->dr_cmd_in,
434 ucs->rcvbuf, ucs->rcvbuf_size,
435 read_ctrl_callback, cs->inbuf);
437 if ((ret = usb_submit_urb(ucs->urb_cmd_in, SLAB_ATOMIC)) != 0) {
438 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
439 get_usb_statmsg(ret));
444 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
445 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
446 ucs->timer_cmd_in.data = (unsigned long) cs;
447 ucs->timer_cmd_in.function = cmd_in_timeout;
448 add_timer(&ucs->timer_cmd_in);
453 static void stopurbs(struct bas_bc_state *);
454 static int start_cbsend(struct cardstate *);
456 /* set/clear bits in base connection state
458 inline static void update_basstate(struct bas_cardstate *ucs,
464 spin_lock_irqsave(&ucs->lock, flags);
465 state = atomic_read(&ucs->basstate);
468 atomic_set(&ucs->basstate, state);
469 spin_unlock_irqrestore(&ucs->lock, flags);
474 * USB completion handler for interrupt pipe input
475 * called by the USB subsystem in interrupt context
477 * urb USB request block
478 * urb->context = controller state structure
480 static void read_int_callback(struct urb *urb, struct pt_regs *regs)
482 struct cardstate *cs;
483 struct bas_cardstate *ucs;
484 struct bc_state *bcs;
491 cs = (struct cardstate *) urb->context;
496 if (unlikely(!atomic_read(&cs->connected))) {
497 warn("%s: disconnected", __func__);
501 switch (urb->status) {
502 case 0: /* success */
504 case -ENOENT: /* canceled */
505 case -ECONNRESET: /* canceled (async) */
506 case -EINPROGRESS: /* pending */
507 /* ignore silently */
508 gig_dbg(DEBUG_USBREQ, "%s: %s",
509 __func__, get_usb_statmsg(urb->status));
511 default: /* severe trouble */
512 dev_warn(cs->dev, "interrupt read: %s\n",
513 get_usb_statmsg(urb->status));
514 //FIXME corrective action? resubmission always ok?
518 l = (unsigned) ucs->int_in_buf[1] +
519 (((unsigned) ucs->int_in_buf[2]) << 8);
521 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
522 urb->actual_length, (int)ucs->int_in_buf[0], l,
523 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
527 switch (ucs->int_in_buf[0]) {
528 case HD_DEVICE_INIT_OK:
529 update_basstate(ucs, BS_INIT, 0);
532 case HD_READY_SEND_ATDATA:
533 del_timer(&ucs->timer_atrdy);
534 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
538 case HD_OPEN_B2CHANNEL_ACK:
540 case HD_OPEN_B1CHANNEL_ACK:
541 bcs = cs->bcs + channel;
542 update_basstate(ucs, BS_B1OPEN << channel, 0);
543 gigaset_bchannel_up(bcs);
546 case HD_OPEN_ATCHANNEL_ACK:
547 update_basstate(ucs, BS_ATOPEN, 0);
551 case HD_CLOSE_B2CHANNEL_ACK:
553 case HD_CLOSE_B1CHANNEL_ACK:
554 bcs = cs->bcs + channel;
555 update_basstate(ucs, 0, BS_B1OPEN << channel);
556 stopurbs(bcs->hw.bas);
557 gigaset_bchannel_down(bcs);
560 case HD_CLOSE_ATCHANNEL_ACK:
561 update_basstate(ucs, 0, BS_ATOPEN);
564 case HD_B2_FLOW_CONTROL:
566 case HD_B1_FLOW_CONTROL:
567 bcs = cs->bcs + channel;
568 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
569 &bcs->hw.bas->corrbytes);
571 "Flow control (channel %d, sub %d): 0x%02x => %d",
572 channel, bcs->hw.bas->numsub, l,
573 atomic_read(&bcs->hw.bas->corrbytes));
576 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
579 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
582 spin_lock_irqsave(&cs->lock, flags);
583 if (ucs->rcvbuf_size) {
584 spin_unlock_irqrestore(&cs->lock, flags);
586 "receive AT data overrun, %d bytes lost\n", l);
587 error_reset(cs); //FIXME reschedule
590 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
591 spin_unlock_irqrestore(&cs->lock, flags);
592 dev_err(cs->dev, "out of memory, %d bytes lost\n", l);
593 error_reset(cs); //FIXME reschedule
596 ucs->rcvbuf_size = l;
597 ucs->retry_cmd_in = 0;
598 if ((status = atread_submit(cs, BAS_TIMEOUT)) < 0) {
601 ucs->rcvbuf_size = 0;
602 error_reset(cs); //FIXME reschedule
604 spin_unlock_irqrestore(&cs->lock, flags);
607 case HD_RESET_INTERRUPT_PIPE_ACK:
608 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
612 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
617 "unknown Gigaset signal 0x%02x (%u) ignored\n",
618 (int) ucs->int_in_buf[0], l);
624 status = usb_submit_urb(urb, SLAB_ATOMIC);
625 if (unlikely(status)) {
626 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
627 get_usb_statmsg(status));
632 /* read_ctrl_callback
633 * USB completion handler for control pipe input
634 * called by the USB subsystem in interrupt context
636 * urb USB request block
637 * urb->context = inbuf structure for controller state
639 static void read_ctrl_callback(struct urb *urb, struct pt_regs *regs)
641 struct cardstate *cs;
642 struct bas_cardstate *ucs;
645 struct inbuf_t *inbuf;
649 inbuf = (struct inbuf_t *) urb->context;
656 spin_lock_irqsave(&cs->lock, flags);
657 if (unlikely(!atomic_read(&cs->connected))) {
658 warn("%s: disconnected", __func__);
659 spin_unlock_irqrestore(&cs->lock, flags);
663 if (!ucs->rcvbuf_size) {
664 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
665 spin_unlock_irqrestore(&cs->lock, flags);
669 del_timer(&ucs->timer_cmd_in);
671 switch (urb->status) {
672 case 0: /* normal completion */
673 numbytes = urb->actual_length;
674 if (unlikely(numbytes == 0)) {
676 "control read: empty block received\n");
679 if (unlikely(numbytes != ucs->rcvbuf_size)) {
681 "control read: received %d chars, expected %d\n",
682 numbytes, ucs->rcvbuf_size);
683 if (numbytes > ucs->rcvbuf_size)
684 numbytes = ucs->rcvbuf_size;
687 /* copy received bytes to inbuf */
688 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
690 if (unlikely(numbytes < ucs->rcvbuf_size)) {
691 /* incomplete - resubmit for remaining bytes */
692 ucs->rcvbuf_size -= numbytes;
693 ucs->retry_cmd_in = 0;
698 case -ENOENT: /* canceled */
699 case -ECONNRESET: /* canceled (async) */
700 case -EINPROGRESS: /* pending */
701 /* no action necessary */
702 gig_dbg(DEBUG_USBREQ, "%s: %s",
703 __func__, get_usb_statmsg(urb->status));
706 default: /* severe trouble */
707 dev_warn(cs->dev, "control read: %s\n",
708 get_usb_statmsg(urb->status));
710 if (ucs->retry_cmd_in++ < BAS_RETRY) {
711 dev_notice(cs->dev, "control read: retry %d\n",
713 if (atread_submit(cs, BAS_TIMEOUT) >= 0) {
714 /* resubmitted - bypass regular exit block */
715 spin_unlock_irqrestore(&cs->lock, flags);
720 "control read: giving up after %d tries\n",
728 ucs->rcvbuf_size = 0;
729 spin_unlock_irqrestore(&cs->lock, flags);
731 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
732 gigaset_schedule_event(cs);
737 * USB completion handler for B channel isochronous input
738 * called by the USB subsystem in interrupt context
740 * urb USB request block of completed request
741 * urb->context = bc_state structure
743 static void read_iso_callback(struct urb *urb, struct pt_regs *regs)
745 struct bc_state *bcs;
746 struct bas_bc_state *ubc;
751 IFNULLRET(urb->context);
752 IFNULLRET(cardstate);
754 /* status codes not worth bothering the tasklet with */
755 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
756 urb->status == -EINPROGRESS)) {
757 gig_dbg(DEBUG_ISO, "%s: %s",
758 __func__, get_usb_statmsg(urb->status));
762 bcs = (struct bc_state *) urb->context;
766 spin_lock_irqsave(&ubc->isoinlock, flags);
767 if (likely(ubc->isoindone == NULL)) {
768 /* pass URB to tasklet */
769 ubc->isoindone = urb;
770 tasklet_schedule(&ubc->rcvd_tasklet);
772 /* tasklet still busy, drop data and resubmit URB */
773 ubc->loststatus = urb->status;
774 for (i = 0; i < BAS_NUMFRAMES; i++) {
775 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
776 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
777 urb->iso_frame_desc[i].status != -EINPROGRESS)) {
778 ubc->loststatus = urb->iso_frame_desc[i].status;
780 urb->iso_frame_desc[i].status = 0;
781 urb->iso_frame_desc[i].actual_length = 0;
783 if (likely(atomic_read(&ubc->running))) {
784 /* urb->dev is clobbered by USB subsystem */
785 urb->dev = bcs->cs->hw.bas->udev;
786 urb->transfer_flags = URB_ISO_ASAP;
787 urb->number_of_packets = BAS_NUMFRAMES;
788 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
790 rc = usb_submit_urb(urb, SLAB_ATOMIC);
791 if (unlikely(rc != 0)) {
792 dev_err(bcs->cs->dev,
793 "could not resubmit isochronous read "
794 "URB: %s\n", get_usb_statmsg(rc));
795 dump_urb(DEBUG_ISO, "isoc read", urb);
800 spin_unlock_irqrestore(&ubc->isoinlock, flags);
803 /* write_iso_callback
804 * USB completion handler for B channel isochronous output
805 * called by the USB subsystem in interrupt context
807 * urb USB request block of completed request
808 * urb->context = isow_urbctx_t structure
810 static void write_iso_callback(struct urb *urb, struct pt_regs *regs)
812 struct isow_urbctx_t *ucx;
813 struct bas_bc_state *ubc;
817 IFNULLRET(urb->context);
818 IFNULLRET(cardstate);
820 /* status codes not worth bothering the tasklet with */
821 if (unlikely(urb->status == -ENOENT || urb->status == -ECONNRESET ||
822 urb->status == -EINPROGRESS)) {
823 gig_dbg(DEBUG_ISO, "%s: %s",
824 __func__, get_usb_statmsg(urb->status));
828 /* pass URB context to tasklet */
829 ucx = (struct isow_urbctx_t *) urb->context;
831 ubc = ucx->bcs->hw.bas;
834 spin_lock_irqsave(&ubc->isooutlock, flags);
835 ubc->isooutovfl = ubc->isooutdone;
836 ubc->isooutdone = ucx;
837 spin_unlock_irqrestore(&ubc->isooutlock, flags);
838 tasklet_schedule(&ubc->sent_tasklet);
842 * prepare and submit USB request blocks for isochronous input and output
844 * B channel control structure
847 * < 0 on error (no URBs submitted)
849 static int starturbs(struct bc_state *bcs)
852 struct bas_bc_state *ubc;
856 IFNULLRETVAL(bcs, -EFAULT);
858 IFNULLRETVAL(ubc, -EFAULT);
860 /* initialize L2 reception */
861 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
862 bcs->inputstate |= INS_flag_hunt;
864 /* submit all isochronous input URBs */
865 atomic_set(&ubc->running, 1);
866 for (k = 0; k < BAS_INURBS; k++) {
867 urb = ubc->isoinurbs[k];
869 dev_err(bcs->cs->dev, "isoinurbs[%d]==NULL\n", k);
874 urb->dev = bcs->cs->hw.bas->udev;
875 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
876 urb->transfer_flags = URB_ISO_ASAP;
877 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
878 urb->transfer_buffer_length = BAS_INBUFSIZE;
879 urb->number_of_packets = BAS_NUMFRAMES;
880 urb->interval = BAS_FRAMETIME;
881 urb->complete = read_iso_callback;
883 for (j = 0; j < BAS_NUMFRAMES; j++) {
884 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
885 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
886 urb->iso_frame_desc[j].status = 0;
887 urb->iso_frame_desc[j].actual_length = 0;
890 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
891 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
892 dev_err(bcs->cs->dev,
893 "could not submit isochronous read URB %d: %s\n",
894 k, get_usb_statmsg(rc));
899 /* initialize L2 transmission */
900 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
902 /* set up isochronous output URBs for flag idling */
903 for (k = 0; k < BAS_OUTURBS; ++k) {
904 urb = ubc->isoouturbs[k].urb;
906 dev_err(bcs->cs->dev, "isoouturbs[%d].urb==NULL\n", k);
910 urb->dev = bcs->cs->hw.bas->udev;
911 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
912 urb->transfer_flags = URB_ISO_ASAP;
913 urb->transfer_buffer = ubc->isooutbuf->data;
914 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
915 urb->number_of_packets = BAS_NUMFRAMES;
916 urb->interval = BAS_FRAMETIME;
917 urb->complete = write_iso_callback;
918 urb->context = &ubc->isoouturbs[k];
919 for (j = 0; j < BAS_NUMFRAMES; ++j) {
920 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
921 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
922 urb->iso_frame_desc[j].status = 0;
923 urb->iso_frame_desc[j].actual_length = 0;
925 ubc->isoouturbs[k].limit = -1;
928 /* submit two URBs, keep third one */
929 for (k = 0; k < 2; ++k) {
930 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
931 rc = usb_submit_urb(ubc->isoouturbs[k].urb, SLAB_ATOMIC);
933 dev_err(bcs->cs->dev,
934 "could not submit isochronous write URB %d: %s\n",
935 k, get_usb_statmsg(rc));
939 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
940 ubc->isooutfree = &ubc->isoouturbs[2];
941 ubc->isooutdone = ubc->isooutovfl = NULL;
949 * cancel the USB request blocks for isochronous input and output
950 * errors are silently ignored
952 * B channel control structure
954 static void stopurbs(struct bas_bc_state *ubc)
960 atomic_set(&ubc->running, 0);
962 for (k = 0; k < BAS_INURBS; ++k) {
963 rc = usb_unlink_urb(ubc->isoinurbs[k]);
965 "%s: isoc input URB %d unlinked, result = %d",
969 for (k = 0; k < BAS_OUTURBS; ++k) {
970 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
972 "%s: isoc output URB %d unlinked, result = %d",
977 /* Isochronous Write - Bottom Half */
978 /* =============================== */
980 /* submit_iso_write_urb
981 * fill and submit the next isochronous write URB
983 * bcs B channel state structure
985 * number of frames submitted in URB
986 * 0 if URB not submitted because no data available (isooutbuf busy)
987 * error code < 0 on error
989 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
992 struct bas_bc_state *ubc;
993 struct usb_iso_packet_descriptor *ifd;
994 int corrbytes, nframe, rc;
996 IFNULLRETVAL(ucx, -EFAULT);
998 IFNULLRETVAL(urb, -EFAULT);
999 IFNULLRETVAL(ucx->bcs, -EFAULT);
1000 ubc = ucx->bcs->hw.bas;
1001 IFNULLRETVAL(ubc, -EFAULT);
1003 /* urb->dev is clobbered by USB subsystem */
1004 urb->dev = ucx->bcs->cs->hw.bas->udev;
1005 urb->transfer_flags = URB_ISO_ASAP;
1006 urb->transfer_buffer = ubc->isooutbuf->data;
1007 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1009 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1010 ifd = &urb->iso_frame_desc[nframe];
1012 /* compute frame length according to flow control */
1013 ifd->length = BAS_NORMFRAME;
1014 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
1015 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1016 __func__, corrbytes);
1017 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1018 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1019 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1020 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1021 ifd->length += corrbytes;
1022 atomic_add(-corrbytes, &ubc->corrbytes);
1025 /* retrieve block of data to send */
1026 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1028 if (ifd->offset < 0) {
1029 if (ifd->offset == -EBUSY) {
1031 "%s: buffer busy at frame %d",
1033 /* tasklet will be restarted from
1034 gigaset_send_skb() */
1036 dev_err(ucx->bcs->cs->dev,
1037 "%s: buffer error %d at frame %d\n",
1038 __func__, ifd->offset, nframe);
1043 ucx->limit = atomic_read(&ubc->isooutbuf->nextread);
1045 ifd->actual_length = 0;
1047 if ((urb->number_of_packets = nframe) > 0) {
1048 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1049 dev_err(ucx->bcs->cs->dev,
1050 "could not submit isochronous write URB: %s\n",
1051 get_usb_statmsg(rc));
1052 dump_urb(DEBUG_ISO, "isoc write", urb);
1060 /* write_iso_tasklet
1061 * tasklet scheduled when an isochronous output URB from the Gigaset device
1064 * data B channel state structure
1066 static void write_iso_tasklet(unsigned long data)
1068 struct bc_state *bcs;
1069 struct bas_bc_state *ubc;
1070 struct cardstate *cs;
1071 struct isow_urbctx_t *done, *next, *ovfl;
1073 struct usb_iso_packet_descriptor *ifd;
1075 unsigned long flags;
1077 struct sk_buff *skb;
1080 bcs = (struct bc_state *) data;
1087 /* loop while completed URBs arrive in time */
1089 if (unlikely(!atomic_read(&cs->connected))) {
1090 warn("%s: disconnected", __func__);
1094 if (unlikely(!(atomic_read(&ubc->running)))) {
1095 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1099 /* retrieve completed URBs */
1100 spin_lock_irqsave(&ubc->isooutlock, flags);
1101 done = ubc->isooutdone;
1102 ubc->isooutdone = NULL;
1103 ovfl = ubc->isooutovfl;
1104 ubc->isooutovfl = NULL;
1105 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1107 dev_err(cs->dev, "isochronous write buffer underrun\n");
1114 /* submit free URB if available */
1115 spin_lock_irqsave(&ubc->isooutlock, flags);
1116 next = ubc->isooutfree;
1117 ubc->isooutfree = NULL;
1118 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1120 if (submit_iso_write_urb(next) <= 0) {
1121 /* could not submit URB, put it back */
1122 spin_lock_irqsave(&ubc->isooutlock, flags);
1123 if (ubc->isooutfree == NULL) {
1124 ubc->isooutfree = next;
1127 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1129 /* couldn't put it back */
1131 "losing isochronous write URB\n");
1137 /* process completed URB */
1139 switch (urb->status) {
1140 case 0: /* normal completion */
1142 case -EXDEV: /* inspect individual frames */
1143 /* assumptions (for lack of documentation):
1144 * - actual_length bytes of the frame in error are
1146 * - all following frames are not sent at all
1148 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1150 offset = done->limit; /* just in case */
1151 for (i = 0; i < BAS_NUMFRAMES; i++) {
1152 ifd = &urb->iso_frame_desc[i];
1154 ifd->actual_length != ifd->length) {
1156 "isochronous write: frame %d: %s, "
1157 "only %d of %d bytes sent\n",
1158 i, get_usb_statmsg(ifd->status),
1159 ifd->actual_length, ifd->length);
1160 offset = (ifd->offset +
1166 #ifdef CONFIG_GIGASET_DEBUG
1167 /* check assumption on remaining frames */
1168 for (; i < BAS_NUMFRAMES; i++) {
1169 ifd = &urb->iso_frame_desc[i];
1170 if (ifd->status != -EINPROGRESS
1171 || ifd->actual_length != 0) {
1173 "isochronous write: frame %d: %s, "
1174 "%d of %d bytes sent\n",
1175 i, get_usb_statmsg(ifd->status),
1176 ifd->actual_length, ifd->length);
1177 offset = (ifd->offset +
1185 case -EPIPE: //FIXME is this the code for "underrun"?
1186 dev_err(cs->dev, "isochronous write stalled\n");
1189 default: /* severe trouble */
1190 dev_warn(cs->dev, "isochronous write: %s\n",
1191 get_usb_statmsg(urb->status));
1194 /* mark the write buffer area covered by this URB as free */
1195 if (done->limit >= 0)
1196 atomic_set(&ubc->isooutbuf->read, done->limit);
1198 /* mark URB as free */
1199 spin_lock_irqsave(&ubc->isooutlock, flags);
1200 next = ubc->isooutfree;
1201 ubc->isooutfree = done;
1202 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1204 /* only one URB still active - resubmit one */
1205 if (submit_iso_write_urb(next) <= 0) {
1206 /* couldn't submit */
1212 /* process queued SKBs */
1213 while ((skb = skb_dequeue(&bcs->squeue))) {
1214 /* copy to output buffer, doing L2 encapsulation */
1216 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1217 /* insufficient buffer space, push back onto queue */
1218 skb_queue_head(&bcs->squeue, skb);
1219 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1220 __func__, skb_queue_len(&bcs->squeue));
1224 gigaset_skb_sent(bcs, skb);
1225 dev_kfree_skb_any(skb);
1229 /* Isochronous Read - Bottom Half */
1230 /* ============================== */
1233 * tasklet scheduled when an isochronous input URB from the Gigaset device
1236 * data B channel state structure
1238 static void read_iso_tasklet(unsigned long data)
1240 struct bc_state *bcs;
1241 struct bas_bc_state *ubc;
1242 struct cardstate *cs;
1245 unsigned long flags;
1246 int totleft, numbytes, offset, frame, rc;
1248 bcs = (struct bc_state *) data;
1255 /* loop while more completed URBs arrive in the meantime */
1257 if (unlikely(!atomic_read(&cs->connected))) {
1258 warn("%s: disconnected", __func__);
1263 spin_lock_irqsave(&ubc->isoinlock, flags);
1264 if (!(urb = ubc->isoindone)) {
1265 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1268 ubc->isoindone = NULL;
1269 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1271 "isochronous read overrun, "
1272 "dropped URB with status: %s, %d bytes lost\n",
1273 get_usb_statmsg(ubc->loststatus),
1275 ubc->loststatus = -EINPROGRESS;
1277 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1279 if (unlikely(!(atomic_read(&ubc->running)))) {
1281 "%s: channel not running, "
1282 "dropped URB with status: %s",
1283 __func__, get_usb_statmsg(urb->status));
1287 switch (urb->status) {
1288 case 0: /* normal completion */
1290 case -EXDEV: /* inspect individual frames
1291 (we do that anyway) */
1292 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1297 gig_dbg(DEBUG_ISO, "%s: URB canceled", __func__);
1298 continue; /* -> skip */
1299 case -EINPROGRESS: /* huh? */
1300 gig_dbg(DEBUG_ISO, "%s: URB still pending", __func__);
1301 continue; /* -> skip */
1303 dev_err(cs->dev, "isochronous read stalled\n");
1305 continue; /* -> skip */
1306 default: /* severe trouble */
1307 dev_warn(cs->dev, "isochronous read: %s\n",
1308 get_usb_statmsg(urb->status));
1312 rcvbuf = urb->transfer_buffer;
1313 totleft = urb->actual_length;
1314 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1315 if (unlikely(urb->iso_frame_desc[frame].status)) {
1317 "isochronous read: frame %d: %s\n",
1320 urb->iso_frame_desc[frame].status));
1323 numbytes = urb->iso_frame_desc[frame].actual_length;
1324 if (unlikely(numbytes > BAS_MAXFRAME)) {
1326 "isochronous read: frame %d: "
1327 "numbytes (%d) > BAS_MAXFRAME\n",
1331 if (unlikely(numbytes > totleft)) {
1333 "isochronous read: frame %d: "
1334 "numbytes (%d) > totleft (%d)\n",
1335 frame, numbytes, totleft);
1338 offset = urb->iso_frame_desc[frame].offset;
1339 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1341 "isochronous read: frame %d: "
1342 "offset (%d) + numbytes (%d) "
1343 "> BAS_INBUFSIZE\n",
1344 frame, offset, numbytes);
1347 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1348 totleft -= numbytes;
1350 if (unlikely(totleft > 0))
1352 "isochronous read: %d data bytes missing\n",
1356 /* URB processed, resubmit */
1357 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1358 urb->iso_frame_desc[frame].status = 0;
1359 urb->iso_frame_desc[frame].actual_length = 0;
1361 /* urb->dev is clobbered by USB subsystem */
1362 urb->dev = bcs->cs->hw.bas->udev;
1363 urb->transfer_flags = URB_ISO_ASAP;
1364 urb->number_of_packets = BAS_NUMFRAMES;
1365 if ((rc = usb_submit_urb(urb, SLAB_ATOMIC)) != 0) {
1367 "could not resubmit isochronous read URB: %s\n",
1368 get_usb_statmsg(rc));
1369 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1375 /* Channel Operations */
1376 /* ================== */
1379 * timeout routine for control output request
1381 * B channel control structure
1383 static void req_timeout(unsigned long data)
1385 struct bc_state *bcs = (struct bc_state *) data;
1386 struct bas_cardstate *ucs;
1388 unsigned long flags;
1392 ucs = bcs->cs->hw.bas;
1397 spin_lock_irqsave(&ucs->lock, flags);
1398 pending = ucs->pending;
1400 spin_unlock_irqrestore(&ucs->lock, flags);
1403 case 0: /* no pending request */
1404 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1407 case HD_OPEN_ATCHANNEL:
1408 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1409 error_reset(bcs->cs);
1412 case HD_OPEN_B2CHANNEL:
1413 case HD_OPEN_B1CHANNEL:
1414 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1419 case HD_CLOSE_ATCHANNEL:
1420 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1423 case HD_CLOSE_B2CHANNEL:
1424 case HD_CLOSE_B1CHANNEL:
1425 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1430 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1435 /* write_ctrl_callback
1436 * USB completion handler for control pipe output
1437 * called by the USB subsystem in interrupt context
1439 * urb USB request block of completed request
1440 * urb->context = hardware specific controller state structure
1442 static void write_ctrl_callback(struct urb *urb, struct pt_regs *regs)
1444 struct bas_cardstate *ucs;
1445 unsigned long flags;
1448 IFNULLRET(urb->context);
1449 IFNULLRET(cardstate);
1451 ucs = (struct bas_cardstate *) urb->context;
1452 spin_lock_irqsave(&ucs->lock, flags);
1453 if (urb->status && ucs->pending) {
1454 dev_err(&ucs->interface->dev,
1455 "control request 0x%02x failed: %s\n",
1456 ucs->pending, get_usb_statmsg(urb->status));
1457 del_timer(&ucs->timer_ctrl);
1460 /* individual handling of specific request types */
1461 switch (ucs->pending) {
1462 case HD_DEVICE_INIT_ACK: /* no reply expected */
1466 spin_unlock_irqrestore(&ucs->lock, flags);
1470 * submit a control output request without message buffer to the Gigaset base
1471 * and optionally start a timeout
1473 * bcs B channel control structure
1474 * req control request code (HD_*)
1475 * val control request parameter value (set to 0 if unused)
1476 * timeout timeout in seconds (0: no timeout)
1479 * -EINVAL if a NULL pointer is encountered somewhere
1480 * -EBUSY if another request is pending
1481 * any URB submission error code
1483 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1485 struct bas_cardstate *ucs;
1487 unsigned long flags;
1489 IFNULLRETVAL(bcs, -EINVAL);
1490 IFNULLRETVAL(bcs->cs, -EINVAL);
1491 ucs = bcs->cs->hw.bas;
1492 IFNULLRETVAL(ucs, -EINVAL);
1493 IFNULLRETVAL(ucs->urb_ctrl, -EINVAL);
1495 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1497 spin_lock_irqsave(&ucs->lock, flags);
1499 spin_unlock_irqrestore(&ucs->lock, flags);
1500 dev_err(bcs->cs->dev,
1501 "submission of request 0x%02x failed: "
1502 "request 0x%02x still pending\n",
1506 if (ucs->urb_ctrl->status == -EINPROGRESS) {
1507 spin_unlock_irqrestore(&ucs->lock, flags);
1508 dev_err(bcs->cs->dev,
1509 "could not submit request 0x%02x: URB busy\n", req);
1513 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1514 ucs->dr_ctrl.bRequest = req;
1515 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1516 ucs->dr_ctrl.wIndex = 0;
1517 ucs->dr_ctrl.wLength = 0;
1518 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1519 usb_sndctrlpipe(ucs->udev, 0),
1520 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1521 write_ctrl_callback, ucs);
1522 if ((ret = usb_submit_urb(ucs->urb_ctrl, SLAB_ATOMIC)) != 0) {
1523 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1524 req, get_usb_statmsg(ret));
1525 spin_unlock_irqrestore(&ucs->lock, flags);
1531 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1532 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1533 ucs->timer_ctrl.data = (unsigned long) bcs;
1534 ucs->timer_ctrl.function = req_timeout;
1535 add_timer(&ucs->timer_ctrl);
1538 spin_unlock_irqrestore(&ucs->lock, flags);
1542 /* gigaset_init_bchannel
1543 * called by common.c to connect a B channel
1544 * initialize isochronous I/O and tell the Gigaset base to open the channel
1546 * B channel control structure
1548 * 0 on success, error code < 0 on error
1550 static int gigaset_init_bchannel(struct bc_state *bcs)
1554 IFNULLRETVAL(bcs, -EINVAL);
1556 if ((ret = starturbs(bcs)) < 0) {
1557 dev_err(bcs->cs->dev,
1558 "could not start isochronous I/O for channel %d\n",
1564 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1565 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
1566 dev_err(bcs->cs->dev, "could not open channel %d: %s\n",
1567 bcs->channel + 1, get_usb_statmsg(ret));
1568 stopurbs(bcs->hw.bas);
1574 /* gigaset_close_bchannel
1575 * called by common.c to disconnect a B channel
1576 * tell the Gigaset base to close the channel
1577 * stopping isochronous I/O and LL notification will be done when the
1578 * acknowledgement for the close arrives
1580 * B channel control structure
1582 * 0 on success, error code < 0 on error
1584 static int gigaset_close_bchannel(struct bc_state *bcs)
1588 IFNULLRETVAL(bcs, -EINVAL);
1590 if (!(atomic_read(&bcs->cs->hw.bas->basstate) &
1591 (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1592 /* channel not running: just signal common.c */
1593 gigaset_bchannel_down(bcs);
1597 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1598 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
1599 dev_err(bcs->cs->dev,
1600 "could not submit HD_CLOSE_BxCHANNEL request: %s\n",
1601 get_usb_statmsg(ret));
1605 /* Device Operations */
1606 /* ================= */
1609 * unqueue first command buffer from queue, waking any sleepers
1610 * must be called with cs->cmdlock held
1612 * cs controller state structure
1614 static void complete_cb(struct cardstate *cs)
1616 struct cmdbuf_t *cb;
1622 /* unqueue completed buffer */
1623 cs->cmdbytes -= cs->curlen;
1624 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1625 "write_command: sent %u bytes, %u left",
1626 cs->curlen, cs->cmdbytes);
1627 if ((cs->cmdbuf = cb->next) != NULL) {
1628 cs->cmdbuf->prev = NULL;
1629 cs->curlen = cs->cmdbuf->len;
1631 cs->lastcmdbuf = NULL;
1635 if (cb->wake_tasklet)
1636 tasklet_schedule(cb->wake_tasklet);
1641 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len);
1643 /* write_command_callback
1644 * USB completion handler for AT command transmission
1645 * called by the USB subsystem in interrupt context
1647 * urb USB request block of completed request
1648 * urb->context = controller state structure
1650 static void write_command_callback(struct urb *urb, struct pt_regs *regs)
1652 struct cardstate *cs;
1653 unsigned long flags;
1654 struct bas_cardstate *ucs;
1657 cs = (struct cardstate *) urb->context;
1663 switch (urb->status) {
1664 case 0: /* normal completion */
1666 case -ENOENT: /* canceled */
1667 case -ECONNRESET: /* canceled (async) */
1668 case -EINPROGRESS: /* pending */
1669 /* ignore silently */
1670 gig_dbg(DEBUG_USBREQ, "%s: %s",
1671 __func__, get_usb_statmsg(urb->status));
1673 default: /* any failure */
1674 if (++ucs->retry_cmd_out > BAS_RETRY) {
1676 "command write: %s, "
1677 "giving up after %d retries\n",
1678 get_usb_statmsg(urb->status),
1679 ucs->retry_cmd_out);
1682 if (cs->cmdbuf == NULL) {
1684 "command write: %s, "
1685 "cannot retry - cmdbuf gone\n",
1686 get_usb_statmsg(urb->status));
1689 dev_notice(cs->dev, "command write: %s, retry %d\n",
1690 get_usb_statmsg(urb->status), ucs->retry_cmd_out);
1691 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1692 /* resubmitted - bypass regular exit block */
1694 /* command send failed, assume base still waiting */
1695 update_basstate(ucs, BS_ATREADY, 0);
1698 spin_lock_irqsave(&cs->cmdlock, flags);
1699 if (cs->cmdbuf != NULL)
1701 spin_unlock_irqrestore(&cs->cmdlock, flags);
1705 * timeout routine for AT command transmission
1707 * controller state structure
1709 static void atrdy_timeout(unsigned long data)
1711 struct cardstate *cs = (struct cardstate *) data;
1712 struct bas_cardstate *ucs;
1718 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1720 /* fake the missing signal - what else can I do? */
1721 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1726 * submit an HD_WRITE_ATMESSAGE command URB
1728 * cs controller state structure
1729 * buf buffer containing command to send
1730 * len length of command to send
1733 * -EFAULT if a NULL pointer is encountered somewhere
1734 * -EBUSY if another request is pending
1735 * any URB submission error code
1737 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1739 struct bas_cardstate *ucs;
1742 IFNULLRETVAL(cs, -EFAULT);
1744 IFNULLRETVAL(ucs, -EFAULT);
1745 IFNULLRETVAL(ucs->urb_cmd_out, -EFAULT);
1747 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1749 if (ucs->urb_cmd_out->status == -EINPROGRESS) {
1751 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1755 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1756 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1757 ucs->dr_cmd_out.wValue = 0;
1758 ucs->dr_cmd_out.wIndex = 0;
1759 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1760 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1761 usb_sndctrlpipe(ucs->udev, 0),
1762 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1763 write_command_callback, cs);
1765 if ((ret = usb_submit_urb(ucs->urb_cmd_out, SLAB_ATOMIC)) != 0) {
1766 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1767 get_usb_statmsg(ret));
1771 /* submitted successfully */
1772 update_basstate(ucs, 0, BS_ATREADY);
1774 /* start timeout if necessary */
1775 if (!(atomic_read(&ucs->basstate) & BS_ATTIMER)) {
1776 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1778 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1779 ucs->timer_atrdy.data = (unsigned long) cs;
1780 ucs->timer_atrdy.function = atrdy_timeout;
1781 add_timer(&ucs->timer_atrdy);
1782 update_basstate(ucs, BS_ATTIMER, 0);
1788 * start transmission of AT command queue if necessary
1790 * cs controller state structure
1793 * error code < 0 on error
1795 static int start_cbsend(struct cardstate *cs)
1797 struct cmdbuf_t *cb;
1798 struct bas_cardstate *ucs;
1799 unsigned long flags;
1803 IFNULLRETVAL(cs, -EFAULT);
1805 IFNULLRETVAL(ucs, -EFAULT);
1807 /* check if AT channel is open */
1808 if (!(atomic_read(&ucs->basstate) & BS_ATOPEN)) {
1809 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
1810 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1812 dev_err(cs->dev, "could not open AT channel\n");
1813 /* flush command queue */
1814 spin_lock_irqsave(&cs->cmdlock, flags);
1815 while (cs->cmdbuf != NULL)
1817 spin_unlock_irqrestore(&cs->cmdlock, flags);
1822 /* try to send first command in queue */
1823 spin_lock_irqsave(&cs->cmdlock, flags);
1825 while ((cb = cs->cmdbuf) != NULL &&
1826 atomic_read(&ucs->basstate) & BS_ATREADY) {
1827 ucs->retry_cmd_out = 0;
1828 rc = atwrite_submit(cs, cb->buf, cb->len);
1835 spin_unlock_irqrestore(&cs->cmdlock, flags);
1839 /* gigaset_write_cmd
1840 * This function is called by the device independent part of the driver
1841 * to transmit an AT command string to the Gigaset device.
1842 * It encapsulates the device specific method for transmission over the
1843 * direct USB connection to the base.
1844 * The command string is added to the queue of commands to send, and
1845 * USB transmission is started if necessary.
1847 * cs controller state structure
1848 * buf command string to send
1849 * len number of bytes to send (max. IF_WRITEBUF)
1850 * wake_tasklet tasklet to run when transmission is completed
1853 * number of bytes queued on success
1854 * error code < 0 on error
1856 static int gigaset_write_cmd(struct cardstate *cs,
1857 const unsigned char *buf, int len,
1858 struct tasklet_struct *wake_tasklet)
1860 struct cmdbuf_t *cb;
1861 unsigned long flags;
1864 gigaset_dbg_buffer(atomic_read(&cs->mstate) != MS_LOCKED ?
1865 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1866 "CMD Transmit", len, buf, 0);
1868 if (unlikely(!atomic_read(&cs->connected))) {
1869 err("%s: disconnected", __func__);
1874 return 0; /* nothing to do */
1876 if (len > IF_WRITEBUF)
1878 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
1879 dev_err(cs->dev, "%s: out of memory\n", __func__);
1883 memcpy(cb->buf, buf, len);
1887 cb->wake_tasklet = wake_tasklet;
1889 spin_lock_irqsave(&cs->cmdlock, flags);
1890 cb->prev = cs->lastcmdbuf;
1892 cs->lastcmdbuf->next = cb;
1897 cs->cmdbytes += len;
1898 cs->lastcmdbuf = cb;
1899 spin_unlock_irqrestore(&cs->cmdlock, flags);
1901 status = start_cbsend(cs);
1903 return status < 0 ? status : len;
1906 /* gigaset_write_room
1907 * tty_driver.write_room interface routine
1908 * return number of characters the driver will accept to be written via
1911 * controller state structure
1913 * number of characters
1915 static int gigaset_write_room(struct cardstate *cs)
1920 /* gigaset_chars_in_buffer
1921 * tty_driver.chars_in_buffer interface routine
1922 * return number of characters waiting to be sent
1924 * controller state structure
1926 * number of characters
1928 static int gigaset_chars_in_buffer(struct cardstate *cs)
1930 unsigned long flags;
1933 spin_lock_irqsave(&cs->cmdlock, flags);
1934 bytes = cs->cmdbytes;
1935 spin_unlock_irqrestore(&cs->cmdlock, flags);
1941 * implementation of ioctl(GIGASET_BRKCHARS)
1943 * controller state structure
1945 * -EINVAL (unimplemented function)
1947 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1953 /* Device Initialization/Shutdown */
1954 /* ============================== */
1956 /* Free hardware dependent part of the B channel structure
1958 * bcs B channel structure
1962 static int gigaset_freebcshw(struct bc_state *bcs)
1967 if (bcs->hw.bas->isooutbuf)
1968 kfree(bcs->hw.bas->isooutbuf);
1974 /* Initialize hardware dependent part of the B channel structure
1976 * bcs B channel structure
1980 static int gigaset_initbcshw(struct bc_state *bcs)
1983 struct bas_bc_state *ubc;
1985 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
1987 err("could not allocate bas_bc_state");
1991 atomic_set(&ubc->running, 0);
1992 atomic_set(&ubc->corrbytes, 0);
1993 spin_lock_init(&ubc->isooutlock);
1994 for (i = 0; i < BAS_OUTURBS; ++i) {
1995 ubc->isoouturbs[i].urb = NULL;
1996 ubc->isoouturbs[i].bcs = bcs;
1998 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2000 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2001 err("could not allocate isochronous output buffer");
2006 tasklet_init(&ubc->sent_tasklet,
2007 &write_iso_tasklet, (unsigned long) bcs);
2009 spin_lock_init(&ubc->isoinlock);
2010 for (i = 0; i < BAS_INURBS; ++i)
2011 ubc->isoinurbs[i] = NULL;
2012 ubc->isoindone = NULL;
2013 ubc->loststatus = -EINPROGRESS;
2027 tasklet_init(&ubc->rcvd_tasklet,
2028 &read_iso_tasklet, (unsigned long) bcs);
2032 static void gigaset_reinitbcshw(struct bc_state *bcs)
2034 struct bas_bc_state *ubc = bcs->hw.bas;
2036 atomic_set(&bcs->hw.bas->running, 0);
2037 atomic_set(&bcs->hw.bas->corrbytes, 0);
2038 bcs->hw.bas->numsub = 0;
2039 spin_lock_init(&ubc->isooutlock);
2040 spin_lock_init(&ubc->isoinlock);
2041 ubc->loststatus = -EINPROGRESS;
2044 static void gigaset_freecshw(struct cardstate *cs)
2046 struct bas_cardstate *ucs = cs->hw.bas;
2048 del_timer(&ucs->timer_ctrl);
2049 del_timer(&ucs->timer_atrdy);
2050 del_timer(&ucs->timer_cmd_in);
2055 static int gigaset_initcshw(struct cardstate *cs)
2057 struct bas_cardstate *ucs;
2059 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2063 ucs->urb_cmd_in = NULL;
2064 ucs->urb_cmd_out = NULL;
2066 ucs->rcvbuf_size = 0;
2068 spin_lock_init(&ucs->lock);
2071 atomic_set(&ucs->basstate, 0);
2072 init_timer(&ucs->timer_ctrl);
2073 init_timer(&ucs->timer_atrdy);
2074 init_timer(&ucs->timer_cmd_in);
2080 * unlink and deallocate all URBs unconditionally
2081 * caller must make sure that no commands are still in progress
2083 * cs controller state structure
2085 static void freeurbs(struct cardstate *cs)
2087 struct bas_cardstate *ucs;
2088 struct bas_bc_state *ubc;
2095 for (j = 0; j < 2; ++j) {
2096 ubc = cs->bcs[j].hw.bas;
2098 for (i = 0; i < BAS_OUTURBS; ++i)
2099 if (ubc->isoouturbs[i].urb) {
2100 usb_kill_urb(ubc->isoouturbs[i].urb);
2102 "%s: isoc output URB %d/%d unlinked",
2104 usb_free_urb(ubc->isoouturbs[i].urb);
2105 ubc->isoouturbs[i].urb = NULL;
2107 for (i = 0; i < BAS_INURBS; ++i)
2108 if (ubc->isoinurbs[i]) {
2109 usb_kill_urb(ubc->isoinurbs[i]);
2111 "%s: isoc input URB %d/%d unlinked",
2113 usb_free_urb(ubc->isoinurbs[i]);
2114 ubc->isoinurbs[i] = NULL;
2117 if (ucs->urb_int_in) {
2118 usb_kill_urb(ucs->urb_int_in);
2119 gig_dbg(DEBUG_INIT, "%s: interrupt input URB unlinked",
2121 usb_free_urb(ucs->urb_int_in);
2122 ucs->urb_int_in = NULL;
2124 if (ucs->urb_cmd_out) {
2125 usb_kill_urb(ucs->urb_cmd_out);
2126 gig_dbg(DEBUG_INIT, "%s: command output URB unlinked",
2128 usb_free_urb(ucs->urb_cmd_out);
2129 ucs->urb_cmd_out = NULL;
2131 if (ucs->urb_cmd_in) {
2132 usb_kill_urb(ucs->urb_cmd_in);
2133 gig_dbg(DEBUG_INIT, "%s: command input URB unlinked",
2135 usb_free_urb(ucs->urb_cmd_in);
2136 ucs->urb_cmd_in = NULL;
2138 if (ucs->urb_ctrl) {
2139 usb_kill_urb(ucs->urb_ctrl);
2140 gig_dbg(DEBUG_INIT, "%s: control output URB unlinked",
2142 usb_free_urb(ucs->urb_ctrl);
2143 ucs->urb_ctrl = NULL;
2148 * This function is called when a new USB device is connected.
2149 * It checks whether the new device is handled by this driver.
2151 static int gigaset_probe(struct usb_interface *interface,
2152 const struct usb_device_id *id)
2154 struct usb_host_interface *hostif;
2155 struct usb_device *udev = interface_to_usbdev(interface);
2156 struct cardstate *cs = NULL;
2157 struct bas_cardstate *ucs = NULL;
2158 struct bas_bc_state *ubc;
2159 struct usb_endpoint_descriptor *endpoint;
2163 IFNULLRETVAL(udev, -ENODEV);
2166 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2167 __func__, le16_to_cpu(udev->descriptor.idVendor),
2168 le16_to_cpu(udev->descriptor.idProduct));
2170 /* See if the device offered us matches what we can accept */
2171 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_GIGA_VENDOR_ID) ||
2172 (le16_to_cpu(udev->descriptor.idProduct) != USB_GIGA_PRODUCT_ID &&
2173 le16_to_cpu(udev->descriptor.idProduct) != USB_4175_PRODUCT_ID &&
2174 le16_to_cpu(udev->descriptor.idProduct) != USB_SX303_PRODUCT_ID &&
2175 le16_to_cpu(udev->descriptor.idProduct) != USB_SX353_PRODUCT_ID)) {
2176 gig_dbg(DEBUG_ANY, "%s: unmatched ID - exiting", __func__);
2180 /* set required alternate setting */
2181 hostif = interface->cur_altsetting;
2182 if (hostif->desc.bAlternateSetting != 3) {
2184 "%s: wrong alternate setting %d - trying to switch",
2185 __func__, hostif->desc.bAlternateSetting);
2186 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
2187 dev_warn(&udev->dev, "usb_set_interface failed, "
2188 "device %d interface %d altsetting %d\n",
2189 udev->devnum, hostif->desc.bInterfaceNumber,
2190 hostif->desc.bAlternateSetting);
2193 hostif = interface->cur_altsetting;
2196 /* Reject application specific interfaces
2198 if (hostif->desc.bInterfaceClass != 255) {
2199 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2200 __func__, hostif->desc.bInterfaceClass);
2204 dev_info(&udev->dev,
2205 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2206 __func__, le16_to_cpu(udev->descriptor.idVendor),
2207 le16_to_cpu(udev->descriptor.idProduct));
2209 cs = gigaset_getunassignedcs(driver);
2211 dev_err(&udev->dev, "no free cardstate\n");
2216 /* save off device structure ptrs for later use */
2219 ucs->interface = interface;
2220 cs->dev = &udev->dev;
2223 * - one for the interrupt pipe
2224 * - three for the different uses of the default control pipe
2225 * - three for each isochronous pipe
2227 ucs->urb_int_in = usb_alloc_urb(0, SLAB_KERNEL);
2228 if (!ucs->urb_int_in) {
2229 dev_err(cs->dev, "no free urbs available\n");
2232 ucs->urb_cmd_in = usb_alloc_urb(0, SLAB_KERNEL);
2233 if (!ucs->urb_cmd_in) {
2234 dev_err(cs->dev, "no free urbs available\n");
2237 ucs->urb_cmd_out = usb_alloc_urb(0, SLAB_KERNEL);
2238 if (!ucs->urb_cmd_out) {
2239 dev_err(cs->dev, "no free urbs available\n");
2242 ucs->urb_ctrl = usb_alloc_urb(0, SLAB_KERNEL);
2243 if (!ucs->urb_ctrl) {
2244 dev_err(cs->dev, "no free urbs available\n");
2248 for (j = 0; j < 2; ++j) {
2249 ubc = cs->bcs[j].hw.bas;
2250 for (i = 0; i < BAS_OUTURBS; ++i) {
2251 ubc->isoouturbs[i].urb =
2252 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2253 if (!ubc->isoouturbs[i].urb) {
2254 dev_err(cs->dev, "no free urbs available\n");
2258 for (i = 0; i < BAS_INURBS; ++i) {
2260 usb_alloc_urb(BAS_NUMFRAMES, SLAB_KERNEL);
2261 if (!ubc->isoinurbs[i]) {
2262 dev_err(cs->dev, "no free urbs available\n");
2269 ucs->rcvbuf_size = 0;
2271 /* Fill the interrupt urb and send it to the core */
2272 endpoint = &hostif->endpoint[0].desc;
2273 usb_fill_int_urb(ucs->urb_int_in, udev,
2274 usb_rcvintpipe(udev,
2275 (endpoint->bEndpointAddress) & 0x0f),
2276 ucs->int_in_buf, 3, read_int_callback, cs,
2277 endpoint->bInterval);
2278 ret = usb_submit_urb(ucs->urb_int_in, SLAB_KERNEL);
2280 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2281 get_usb_statmsg(ret));
2285 /* tell the device that the driver is ready */
2286 if ((ret = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
2289 /* tell common part that the device is ready */
2290 if (startmode == SM_LOCKED)
2291 atomic_set(&cs->mstate, MS_LOCKED);
2292 if (!gigaset_start(cs))
2295 /* save address of controller structure */
2296 usb_set_intfdata(interface, cs);
2298 /* set up device sysfs */
2299 gigaset_init_dev_sysfs(interface);
2304 gigaset_unassign(cs);
2308 /* gigaset_disconnect
2309 * This function is called when the Gigaset base is unplugged.
2311 static void gigaset_disconnect(struct usb_interface *interface)
2313 struct cardstate *cs;
2314 struct bas_cardstate *ucs;
2316 /* clear device sysfs */
2317 gigaset_free_dev_sysfs(interface);
2319 cs = usb_get_intfdata(interface);
2320 usb_set_intfdata(interface, NULL);
2326 dev_info(cs->dev, "disconnecting GigaSet base");
2331 ucs->rcvbuf_size = 0;
2332 atomic_set(&ucs->basstate, 0);
2333 gigaset_unassign(cs);
2336 static struct gigaset_ops gigops = {
2339 gigaset_chars_in_buffer,
2341 gigaset_init_bchannel,
2342 gigaset_close_bchannel,
2345 gigaset_reinitbcshw,
2348 gigaset_set_modem_ctrl,
2350 gigaset_set_line_ctrl,
2351 gigaset_isoc_send_skb,
2356 * This function is called after the kernel module is loaded.
2358 static int __init bas_gigaset_init(void)
2362 /* allocate memory for our driver state and intialize it */
2363 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2364 GIGASET_MODULENAME, GIGASET_DEVNAME,
2365 GIGASET_DEVFSNAME, &gigops,
2366 THIS_MODULE)) == NULL)
2369 /* allocate memory for our device state and intialize it */
2370 cardstate = gigaset_initcs(driver, 2, 0, 0, cidmode,
2371 GIGASET_MODULENAME);
2375 /* register this driver with the USB subsystem */
2376 result = usb_register(&gigaset_usb_driver);
2378 err("usb_register failed (error %d)", -result);
2382 info(DRIVER_AUTHOR);
2386 error: if (cardstate)
2387 gigaset_freecs(cardstate);
2390 gigaset_freedriver(driver);
2396 * This function is called before the kernel module is unloaded.
2398 static void __exit bas_gigaset_exit(void)
2400 gigaset_blockdriver(driver); /* => probe will fail
2401 * => no gigaset_start any more
2404 gigaset_shutdown(cardstate);
2405 /* from now on, no isdn callback should be possible */
2407 if (atomic_read(&cardstate->hw.bas->basstate) & BS_ATOPEN) {
2408 gig_dbg(DEBUG_ANY, "closing AT channel");
2409 if (req_submit(cardstate->bcs,
2410 HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT) >= 0) {
2411 /* successfully submitted */
2412 //FIXME wait for completion?
2416 /* deregister this driver with the USB subsystem */
2417 usb_deregister(&gigaset_usb_driver);
2418 /* this will call the disconnect-callback */
2419 /* from now on, no disconnect/probe callback should be running */
2421 gigaset_freecs(cardstate);
2423 gigaset_freedriver(driver);
2428 module_init(bas_gigaset_init);
2429 module_exit(bas_gigaset_exit);
2431 MODULE_AUTHOR(DRIVER_AUTHOR);
2432 MODULE_DESCRIPTION(DRIVER_DESC);
2433 MODULE_LICENSE("GPL");