Linux 2.6.34-rc3
[pohmelfs.git] / drivers / isdn / gigaset / bas-gigaset.c
blob0be15c70c16d65d28a3882f44ca84b1be33d3b48
1 /*
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.
8 * =====================================================================
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
13 * =====================================================================
16 #include "gigaset.h"
18 #include <linux/errno.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/timer.h>
22 #include <linux/usb.h>
23 #include <linux/module.h>
24 #include <linux/moduleparam.h>
26 /* Version Information */
27 #define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
28 #define DRIVER_DESC "USB Driver for Gigaset 307x"
31 /* Module parameters */
33 static int startmode = SM_ISDN;
34 static int cidmode = 1;
36 module_param(startmode, int, S_IRUGO);
37 module_param(cidmode, int, S_IRUGO);
38 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39 MODULE_PARM_DESC(cidmode, "Call-ID mode");
41 #define GIGASET_MINORS 1
42 #define GIGASET_MINOR 16
43 #define GIGASET_MODULENAME "bas_gigaset"
44 #define GIGASET_DEVNAME "ttyGB"
46 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47 #define IF_WRITEBUF 264
49 /* interrupt pipe message size according to ibid. ch. 2.2 */
50 #define IP_MSGSIZE 3
52 /* Values for the Gigaset 307x */
53 #define USB_GIGA_VENDOR_ID 0x0681
54 #define USB_3070_PRODUCT_ID 0x0001
55 #define USB_3075_PRODUCT_ID 0x0002
56 #define USB_SX303_PRODUCT_ID 0x0021
57 #define USB_SX353_PRODUCT_ID 0x0022
59 /* table of devices that work with this driver */
60 static const struct usb_device_id gigaset_table[] = {
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
63 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
64 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
65 { } /* Terminating entry */
68 MODULE_DEVICE_TABLE(usb, gigaset_table);
70 /*======================= local function prototypes ==========================*/
72 /* function called if a new device belonging to this driver is connected */
73 static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
76 /* Function will be called if the device is unplugged */
77 static void gigaset_disconnect(struct usb_interface *interface);
79 /* functions called before/after suspend */
80 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
81 static int gigaset_resume(struct usb_interface *intf);
83 /* functions called before/after device reset */
84 static int gigaset_pre_reset(struct usb_interface *intf);
85 static int gigaset_post_reset(struct usb_interface *intf);
87 static int atread_submit(struct cardstate *, int);
88 static void stopurbs(struct bas_bc_state *);
89 static int req_submit(struct bc_state *, int, int, int);
90 static int atwrite_submit(struct cardstate *, unsigned char *, int);
91 static int start_cbsend(struct cardstate *);
93 /*============================================================================*/
95 struct bas_cardstate {
96 struct usb_device *udev; /* USB device pointer */
97 struct usb_interface *interface; /* interface for this device */
98 unsigned char minor; /* starting minor number */
100 struct urb *urb_ctrl; /* control pipe default URB */
101 struct usb_ctrlrequest dr_ctrl;
102 struct timer_list timer_ctrl; /* control request timeout */
103 int retry_ctrl;
105 struct timer_list timer_atrdy; /* AT command ready timeout */
106 struct urb *urb_cmd_out; /* for sending AT commands */
107 struct usb_ctrlrequest dr_cmd_out;
108 int retry_cmd_out;
110 struct urb *urb_cmd_in; /* for receiving AT replies */
111 struct usb_ctrlrequest dr_cmd_in;
112 struct timer_list timer_cmd_in; /* receive request timeout */
113 unsigned char *rcvbuf; /* AT reply receive buffer */
115 struct urb *urb_int_in; /* URB for interrupt pipe */
116 unsigned char *int_in_buf;
118 spinlock_t lock; /* locks all following */
119 int basstate; /* bitmap (BS_*) */
120 int pending; /* uncompleted base request */
121 wait_queue_head_t waitqueue;
122 int rcvbuf_size; /* size of AT receive buffer */
123 /* 0: no receive in progress */
124 int retry_cmd_in; /* receive req retry count */
127 /* status of direct USB connection to 307x base (bits in basstate) */
128 #define BS_ATOPEN 0x001 /* AT channel open */
129 #define BS_B1OPEN 0x002 /* B channel 1 open */
130 #define BS_B2OPEN 0x004 /* B channel 2 open */
131 #define BS_ATREADY 0x008 /* base ready for AT command */
132 #define BS_INIT 0x010 /* base has signalled INIT_OK */
133 #define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
134 #define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
135 #define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
136 #define BS_SUSPEND 0x100 /* USB port suspended */
137 #define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
140 static struct gigaset_driver *driver;
142 /* usb specific object needed to register this driver with the usb subsystem */
143 static struct usb_driver gigaset_usb_driver = {
144 .name = GIGASET_MODULENAME,
145 .probe = gigaset_probe,
146 .disconnect = gigaset_disconnect,
147 .id_table = gigaset_table,
148 .suspend = gigaset_suspend,
149 .resume = gigaset_resume,
150 .reset_resume = gigaset_post_reset,
151 .pre_reset = gigaset_pre_reset,
152 .post_reset = gigaset_post_reset,
155 /* get message text for usb_submit_urb return code
157 static char *get_usb_rcmsg(int rc)
159 static char unkmsg[28];
161 switch (rc) {
162 case 0:
163 return "success";
164 case -ENOMEM:
165 return "out of memory";
166 case -ENODEV:
167 return "device not present";
168 case -ENOENT:
169 return "endpoint not present";
170 case -ENXIO:
171 return "URB type not supported";
172 case -EINVAL:
173 return "invalid argument";
174 case -EAGAIN:
175 return "start frame too early or too much scheduled";
176 case -EFBIG:
177 return "too many isochronous frames requested";
178 case -EPIPE:
179 return "endpoint stalled";
180 case -EMSGSIZE:
181 return "invalid packet size";
182 case -ENOSPC:
183 return "would overcommit USB bandwidth";
184 case -ESHUTDOWN:
185 return "device shut down";
186 case -EPERM:
187 return "reject flag set";
188 case -EHOSTUNREACH:
189 return "device suspended";
190 default:
191 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
192 return unkmsg;
196 /* get message text for USB status code
198 static char *get_usb_statmsg(int status)
200 static char unkmsg[28];
202 switch (status) {
203 case 0:
204 return "success";
205 case -ENOENT:
206 return "unlinked (sync)";
207 case -EINPROGRESS:
208 return "pending";
209 case -EPROTO:
210 return "bit stuffing error, timeout, or unknown USB error";
211 case -EILSEQ:
212 return "CRC mismatch, timeout, or unknown USB error";
213 case -ETIME:
214 return "timed out";
215 case -EPIPE:
216 return "endpoint stalled";
217 case -ECOMM:
218 return "IN buffer overrun";
219 case -ENOSR:
220 return "OUT buffer underrun";
221 case -EOVERFLOW:
222 return "too much data";
223 case -EREMOTEIO:
224 return "short packet detected";
225 case -ENODEV:
226 return "device removed";
227 case -EXDEV:
228 return "partial isochronous transfer";
229 case -EINVAL:
230 return "invalid argument";
231 case -ECONNRESET:
232 return "unlinked (async)";
233 case -ESHUTDOWN:
234 return "device shut down";
235 default:
236 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
237 return unkmsg;
241 /* usb_pipetype_str
242 * retrieve string representation of USB pipe type
244 static inline char *usb_pipetype_str(int pipe)
246 if (usb_pipeisoc(pipe))
247 return "Isoc";
248 if (usb_pipeint(pipe))
249 return "Int";
250 if (usb_pipecontrol(pipe))
251 return "Ctrl";
252 if (usb_pipebulk(pipe))
253 return "Bulk";
254 return "?";
257 /* dump_urb
258 * write content of URB to syslog for debugging
260 static inline void dump_urb(enum debuglevel level, const char *tag,
261 struct urb *urb)
263 #ifdef CONFIG_GIGASET_DEBUG
264 int i;
265 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
266 if (urb) {
267 gig_dbg(level,
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
270 (unsigned long) urb->dev,
271 usb_pipetype_str(urb->pipe),
272 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
273 usb_pipein(urb->pipe) ? "in" : "out",
274 (unsigned long) urb->hcpriv,
275 urb->transfer_flags);
276 gig_dbg(level,
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
278 "setup_packet=0x%08lx,",
279 (unsigned long) urb->transfer_buffer,
280 urb->transfer_buffer_length, urb->actual_length,
281 (unsigned long) urb->setup_packet);
282 gig_dbg(level,
283 " start_frame=%d, number_of_packets=%d, interval=%d, "
284 "error_count=%d,",
285 urb->start_frame, urb->number_of_packets, urb->interval,
286 urb->error_count);
287 gig_dbg(level,
288 " context=0x%08lx, complete=0x%08lx, "
289 "iso_frame_desc[]={",
290 (unsigned long) urb->context,
291 (unsigned long) urb->complete);
292 for (i = 0; i < urb->number_of_packets; i++) {
293 struct usb_iso_packet_descriptor *pifd
294 = &urb->iso_frame_desc[i];
295 gig_dbg(level,
296 " {offset=%u, length=%u, actual_length=%u, "
297 "status=%u}",
298 pifd->offset, pifd->length, pifd->actual_length,
299 pifd->status);
302 gig_dbg(level, "}}");
303 #endif
306 /* read/set modem control bits etc. (m10x only) */
307 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
308 unsigned new_state)
310 return -EINVAL;
313 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
315 return -EINVAL;
318 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
320 return -EINVAL;
323 /* set/clear bits in base connection state, return previous state
325 static inline int update_basstate(struct bas_cardstate *ucs,
326 int set, int clear)
328 unsigned long flags;
329 int state;
331 spin_lock_irqsave(&ucs->lock, flags);
332 state = ucs->basstate;
333 ucs->basstate = (state & ~clear) | set;
334 spin_unlock_irqrestore(&ucs->lock, flags);
335 return state;
338 /* error_hangup
339 * hang up any existing connection because of an unrecoverable error
340 * This function may be called from any context and takes care of scheduling
341 * the necessary actions for execution outside of interrupt context.
342 * cs->lock must not be held.
343 * argument:
344 * B channel control structure
346 static inline void error_hangup(struct bc_state *bcs)
348 struct cardstate *cs = bcs->cs;
350 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
351 gigaset_schedule_event(cs);
354 /* error_reset
355 * reset Gigaset device because of an unrecoverable error
356 * This function may be called from any context, and takes care of
357 * scheduling the necessary actions for execution outside of interrupt context.
358 * cs->lock must not be held.
359 * argument:
360 * controller state structure
362 static inline void error_reset(struct cardstate *cs)
364 /* reset interrupt pipe to recover (ignore errors) */
365 update_basstate(cs->hw.bas, BS_RESETTING, 0);
366 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
369 /* check_pending
370 * check for completion of pending control request
371 * parameter:
372 * ucs hardware specific controller state structure
374 static void check_pending(struct bas_cardstate *ucs)
376 unsigned long flags;
378 spin_lock_irqsave(&ucs->lock, flags);
379 switch (ucs->pending) {
380 case 0:
381 break;
382 case HD_OPEN_ATCHANNEL:
383 if (ucs->basstate & BS_ATOPEN)
384 ucs->pending = 0;
385 break;
386 case HD_OPEN_B1CHANNEL:
387 if (ucs->basstate & BS_B1OPEN)
388 ucs->pending = 0;
389 break;
390 case HD_OPEN_B2CHANNEL:
391 if (ucs->basstate & BS_B2OPEN)
392 ucs->pending = 0;
393 break;
394 case HD_CLOSE_ATCHANNEL:
395 if (!(ucs->basstate & BS_ATOPEN))
396 ucs->pending = 0;
397 break;
398 case HD_CLOSE_B1CHANNEL:
399 if (!(ucs->basstate & BS_B1OPEN))
400 ucs->pending = 0;
401 break;
402 case HD_CLOSE_B2CHANNEL:
403 if (!(ucs->basstate & BS_B2OPEN))
404 ucs->pending = 0;
405 break;
406 case HD_DEVICE_INIT_ACK: /* no reply expected */
407 ucs->pending = 0;
408 break;
409 case HD_RESET_INTERRUPT_PIPE:
410 if (!(ucs->basstate & BS_RESETTING))
411 ucs->pending = 0;
412 break;
414 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
415 * and should never end up here
417 default:
418 dev_warn(&ucs->interface->dev,
419 "unknown pending request 0x%02x cleared\n",
420 ucs->pending);
421 ucs->pending = 0;
424 if (!ucs->pending)
425 del_timer(&ucs->timer_ctrl);
427 spin_unlock_irqrestore(&ucs->lock, flags);
430 /* cmd_in_timeout
431 * timeout routine for command input request
432 * argument:
433 * controller state structure
435 static void cmd_in_timeout(unsigned long data)
437 struct cardstate *cs = (struct cardstate *) data;
438 struct bas_cardstate *ucs = cs->hw.bas;
439 int rc;
441 if (!ucs->rcvbuf_size) {
442 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
443 return;
446 if (ucs->retry_cmd_in++ < BAS_RETRY) {
447 dev_notice(cs->dev, "control read: timeout, retry %d\n",
448 ucs->retry_cmd_in);
449 rc = atread_submit(cs, BAS_TIMEOUT);
450 if (rc >= 0 || rc == -ENODEV)
451 /* resubmitted or disconnected */
452 /* - bypass regular exit block */
453 return;
454 } else {
455 dev_err(cs->dev,
456 "control read: timeout, giving up after %d tries\n",
457 ucs->retry_cmd_in);
459 kfree(ucs->rcvbuf);
460 ucs->rcvbuf = NULL;
461 ucs->rcvbuf_size = 0;
462 error_reset(cs);
465 /* read_ctrl_callback
466 * USB completion handler for control pipe input
467 * called by the USB subsystem in interrupt context
468 * parameter:
469 * urb USB request block
470 * urb->context = inbuf structure for controller state
472 static void read_ctrl_callback(struct urb *urb)
474 struct inbuf_t *inbuf = urb->context;
475 struct cardstate *cs = inbuf->cs;
476 struct bas_cardstate *ucs = cs->hw.bas;
477 int status = urb->status;
478 int have_data = 0;
479 unsigned numbytes;
480 int rc;
482 update_basstate(ucs, 0, BS_ATRDPEND);
483 wake_up(&ucs->waitqueue);
485 if (!ucs->rcvbuf_size) {
486 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
487 return;
490 del_timer(&ucs->timer_cmd_in);
492 switch (status) {
493 case 0: /* normal completion */
494 numbytes = urb->actual_length;
495 if (unlikely(numbytes != ucs->rcvbuf_size)) {
496 dev_warn(cs->dev,
497 "control read: received %d chars, expected %d\n",
498 numbytes, ucs->rcvbuf_size);
499 if (numbytes > ucs->rcvbuf_size)
500 numbytes = ucs->rcvbuf_size;
503 /* copy received bytes to inbuf */
504 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
506 if (unlikely(numbytes < ucs->rcvbuf_size)) {
507 /* incomplete - resubmit for remaining bytes */
508 ucs->rcvbuf_size -= numbytes;
509 ucs->retry_cmd_in = 0;
510 rc = atread_submit(cs, BAS_TIMEOUT);
511 if (rc >= 0 || rc == -ENODEV)
512 /* resubmitted or disconnected */
513 /* - bypass regular exit block */
514 return;
515 error_reset(cs);
517 break;
519 case -ENOENT: /* cancelled */
520 case -ECONNRESET: /* cancelled (async) */
521 case -EINPROGRESS: /* pending */
522 case -ENODEV: /* device removed */
523 case -ESHUTDOWN: /* device shut down */
524 /* no action necessary */
525 gig_dbg(DEBUG_USBREQ, "%s: %s",
526 __func__, get_usb_statmsg(status));
527 break;
529 default: /* severe trouble */
530 dev_warn(cs->dev, "control read: %s\n",
531 get_usb_statmsg(status));
532 if (ucs->retry_cmd_in++ < BAS_RETRY) {
533 dev_notice(cs->dev, "control read: retry %d\n",
534 ucs->retry_cmd_in);
535 rc = atread_submit(cs, BAS_TIMEOUT);
536 if (rc >= 0 || rc == -ENODEV)
537 /* resubmitted or disconnected */
538 /* - bypass regular exit block */
539 return;
540 } else {
541 dev_err(cs->dev,
542 "control read: giving up after %d tries\n",
543 ucs->retry_cmd_in);
545 error_reset(cs);
548 kfree(ucs->rcvbuf);
549 ucs->rcvbuf = NULL;
550 ucs->rcvbuf_size = 0;
551 if (have_data) {
552 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
553 gigaset_schedule_event(cs);
557 /* atread_submit
558 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
559 * parameters:
560 * cs controller state structure
561 * timeout timeout in 1/10 sec., 0: none
562 * return value:
563 * 0 on success
564 * -EBUSY if another request is pending
565 * any URB submission error code
567 static int atread_submit(struct cardstate *cs, int timeout)
569 struct bas_cardstate *ucs = cs->hw.bas;
570 int basstate;
571 int ret;
573 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
574 ucs->rcvbuf_size);
576 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
577 if (basstate & BS_ATRDPEND) {
578 dev_err(cs->dev,
579 "could not submit HD_READ_ATMESSAGE: URB busy\n");
580 return -EBUSY;
583 if (basstate & BS_SUSPEND) {
584 dev_notice(cs->dev,
585 "HD_READ_ATMESSAGE not submitted, "
586 "suspend in progress\n");
587 update_basstate(ucs, 0, BS_ATRDPEND);
588 /* treat like disconnect */
589 return -ENODEV;
592 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
593 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
594 ucs->dr_cmd_in.wValue = 0;
595 ucs->dr_cmd_in.wIndex = 0;
596 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
597 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
598 usb_rcvctrlpipe(ucs->udev, 0),
599 (unsigned char *) &ucs->dr_cmd_in,
600 ucs->rcvbuf, ucs->rcvbuf_size,
601 read_ctrl_callback, cs->inbuf);
603 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
604 if (ret != 0) {
605 update_basstate(ucs, 0, BS_ATRDPEND);
606 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
607 get_usb_rcmsg(ret));
608 return ret;
611 if (timeout > 0) {
612 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
613 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
614 ucs->timer_cmd_in.data = (unsigned long) cs;
615 ucs->timer_cmd_in.function = cmd_in_timeout;
616 add_timer(&ucs->timer_cmd_in);
618 return 0;
621 /* read_int_callback
622 * USB completion handler for interrupt pipe input
623 * called by the USB subsystem in interrupt context
624 * parameter:
625 * urb USB request block
626 * urb->context = controller state structure
628 static void read_int_callback(struct urb *urb)
630 struct cardstate *cs = urb->context;
631 struct bas_cardstate *ucs = cs->hw.bas;
632 struct bc_state *bcs;
633 int status = urb->status;
634 unsigned long flags;
635 int rc;
636 unsigned l;
637 int channel;
639 switch (status) {
640 case 0: /* success */
641 break;
642 case -ENOENT: /* cancelled */
643 case -ECONNRESET: /* cancelled (async) */
644 case -EINPROGRESS: /* pending */
645 /* ignore silently */
646 gig_dbg(DEBUG_USBREQ, "%s: %s",
647 __func__, get_usb_statmsg(status));
648 return;
649 case -ENODEV: /* device removed */
650 case -ESHUTDOWN: /* device shut down */
651 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
652 return;
653 default: /* severe trouble */
654 dev_warn(cs->dev, "interrupt read: %s\n",
655 get_usb_statmsg(status));
656 goto resubmit;
659 /* drop incomplete packets even if the missing bytes wouldn't matter */
660 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
661 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
662 urb->actual_length);
663 goto resubmit;
666 l = (unsigned) ucs->int_in_buf[1] +
667 (((unsigned) ucs->int_in_buf[2]) << 8);
669 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
670 urb->actual_length, (int)ucs->int_in_buf[0], l,
671 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
673 channel = 0;
675 switch (ucs->int_in_buf[0]) {
676 case HD_DEVICE_INIT_OK:
677 update_basstate(ucs, BS_INIT, 0);
678 break;
680 case HD_READY_SEND_ATDATA:
681 del_timer(&ucs->timer_atrdy);
682 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
683 start_cbsend(cs);
684 break;
686 case HD_OPEN_B2CHANNEL_ACK:
687 ++channel;
688 case HD_OPEN_B1CHANNEL_ACK:
689 bcs = cs->bcs + channel;
690 update_basstate(ucs, BS_B1OPEN << channel, 0);
691 gigaset_bchannel_up(bcs);
692 break;
694 case HD_OPEN_ATCHANNEL_ACK:
695 update_basstate(ucs, BS_ATOPEN, 0);
696 start_cbsend(cs);
697 break;
699 case HD_CLOSE_B2CHANNEL_ACK:
700 ++channel;
701 case HD_CLOSE_B1CHANNEL_ACK:
702 bcs = cs->bcs + channel;
703 update_basstate(ucs, 0, BS_B1OPEN << channel);
704 stopurbs(bcs->hw.bas);
705 gigaset_bchannel_down(bcs);
706 break;
708 case HD_CLOSE_ATCHANNEL_ACK:
709 update_basstate(ucs, 0, BS_ATOPEN);
710 break;
712 case HD_B2_FLOW_CONTROL:
713 ++channel;
714 case HD_B1_FLOW_CONTROL:
715 bcs = cs->bcs + channel;
716 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
717 &bcs->hw.bas->corrbytes);
718 gig_dbg(DEBUG_ISO,
719 "Flow control (channel %d, sub %d): 0x%02x => %d",
720 channel, bcs->hw.bas->numsub, l,
721 atomic_read(&bcs->hw.bas->corrbytes));
722 break;
724 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
725 if (!l) {
726 dev_warn(cs->dev,
727 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
728 break;
730 spin_lock_irqsave(&cs->lock, flags);
731 if (ucs->rcvbuf_size) {
732 /* throw away previous buffer - we have no queue */
733 dev_err(cs->dev,
734 "receive AT data overrun, %d bytes lost\n",
735 ucs->rcvbuf_size);
736 kfree(ucs->rcvbuf);
737 ucs->rcvbuf_size = 0;
739 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
740 if (ucs->rcvbuf == NULL) {
741 spin_unlock_irqrestore(&cs->lock, flags);
742 dev_err(cs->dev, "out of memory receiving AT data\n");
743 error_reset(cs);
744 break;
746 ucs->rcvbuf_size = l;
747 ucs->retry_cmd_in = 0;
748 rc = atread_submit(cs, BAS_TIMEOUT);
749 if (rc < 0) {
750 kfree(ucs->rcvbuf);
751 ucs->rcvbuf = NULL;
752 ucs->rcvbuf_size = 0;
753 if (rc != -ENODEV) {
754 spin_unlock_irqrestore(&cs->lock, flags);
755 error_reset(cs);
756 break;
759 spin_unlock_irqrestore(&cs->lock, flags);
760 break;
762 case HD_RESET_INTERRUPT_PIPE_ACK:
763 update_basstate(ucs, 0, BS_RESETTING);
764 dev_notice(cs->dev, "interrupt pipe reset\n");
765 break;
767 case HD_SUSPEND_END:
768 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
769 break;
771 default:
772 dev_warn(cs->dev,
773 "unknown Gigaset signal 0x%02x (%u) ignored\n",
774 (int) ucs->int_in_buf[0], l);
777 check_pending(ucs);
778 wake_up(&ucs->waitqueue);
780 resubmit:
781 rc = usb_submit_urb(urb, GFP_ATOMIC);
782 if (unlikely(rc != 0 && rc != -ENODEV)) {
783 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
784 get_usb_rcmsg(rc));
785 error_reset(cs);
789 /* read_iso_callback
790 * USB completion handler for B channel isochronous input
791 * called by the USB subsystem in interrupt context
792 * parameter:
793 * urb USB request block of completed request
794 * urb->context = bc_state structure
796 static void read_iso_callback(struct urb *urb)
798 struct bc_state *bcs;
799 struct bas_bc_state *ubc;
800 int status = urb->status;
801 unsigned long flags;
802 int i, rc;
804 /* status codes not worth bothering the tasklet with */
805 if (unlikely(status == -ENOENT ||
806 status == -ECONNRESET ||
807 status == -EINPROGRESS ||
808 status == -ENODEV ||
809 status == -ESHUTDOWN)) {
810 gig_dbg(DEBUG_ISO, "%s: %s",
811 __func__, get_usb_statmsg(status));
812 return;
815 bcs = urb->context;
816 ubc = bcs->hw.bas;
818 spin_lock_irqsave(&ubc->isoinlock, flags);
819 if (likely(ubc->isoindone == NULL)) {
820 /* pass URB to tasklet */
821 ubc->isoindone = urb;
822 ubc->isoinstatus = status;
823 tasklet_hi_schedule(&ubc->rcvd_tasklet);
824 } else {
825 /* tasklet still busy, drop data and resubmit URB */
826 ubc->loststatus = status;
827 for (i = 0; i < BAS_NUMFRAMES; i++) {
828 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
829 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
830 urb->iso_frame_desc[i].status !=
831 -EINPROGRESS))
832 ubc->loststatus = urb->iso_frame_desc[i].status;
833 urb->iso_frame_desc[i].status = 0;
834 urb->iso_frame_desc[i].actual_length = 0;
836 if (likely(ubc->running)) {
837 /* urb->dev is clobbered by USB subsystem */
838 urb->dev = bcs->cs->hw.bas->udev;
839 urb->transfer_flags = URB_ISO_ASAP;
840 urb->number_of_packets = BAS_NUMFRAMES;
841 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
842 __func__);
843 rc = usb_submit_urb(urb, GFP_ATOMIC);
844 if (unlikely(rc != 0 && rc != -ENODEV)) {
845 dev_err(bcs->cs->dev,
846 "could not resubmit isochronous read "
847 "URB: %s\n", get_usb_rcmsg(rc));
848 dump_urb(DEBUG_ISO, "isoc read", urb);
849 error_hangup(bcs);
853 spin_unlock_irqrestore(&ubc->isoinlock, flags);
856 /* write_iso_callback
857 * USB completion handler for B channel isochronous output
858 * called by the USB subsystem in interrupt context
859 * parameter:
860 * urb USB request block of completed request
861 * urb->context = isow_urbctx_t structure
863 static void write_iso_callback(struct urb *urb)
865 struct isow_urbctx_t *ucx;
866 struct bas_bc_state *ubc;
867 int status = urb->status;
868 unsigned long flags;
870 /* status codes not worth bothering the tasklet with */
871 if (unlikely(status == -ENOENT ||
872 status == -ECONNRESET ||
873 status == -EINPROGRESS ||
874 status == -ENODEV ||
875 status == -ESHUTDOWN)) {
876 gig_dbg(DEBUG_ISO, "%s: %s",
877 __func__, get_usb_statmsg(status));
878 return;
881 /* pass URB context to tasklet */
882 ucx = urb->context;
883 ubc = ucx->bcs->hw.bas;
884 ucx->status = status;
886 spin_lock_irqsave(&ubc->isooutlock, flags);
887 ubc->isooutovfl = ubc->isooutdone;
888 ubc->isooutdone = ucx;
889 spin_unlock_irqrestore(&ubc->isooutlock, flags);
890 tasklet_hi_schedule(&ubc->sent_tasklet);
893 /* starturbs
894 * prepare and submit USB request blocks for isochronous input and output
895 * argument:
896 * B channel control structure
897 * return value:
898 * 0 on success
899 * < 0 on error (no URBs submitted)
901 static int starturbs(struct bc_state *bcs)
903 struct bas_bc_state *ubc = bcs->hw.bas;
904 struct urb *urb;
905 int j, k;
906 int rc;
908 /* initialize L2 reception */
909 if (bcs->proto2 == L2_HDLC)
910 bcs->inputstate |= INS_flag_hunt;
912 /* submit all isochronous input URBs */
913 ubc->running = 1;
914 for (k = 0; k < BAS_INURBS; k++) {
915 urb = ubc->isoinurbs[k];
916 if (!urb) {
917 rc = -EFAULT;
918 goto error;
921 urb->dev = bcs->cs->hw.bas->udev;
922 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
923 urb->transfer_flags = URB_ISO_ASAP;
924 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
925 urb->transfer_buffer_length = BAS_INBUFSIZE;
926 urb->number_of_packets = BAS_NUMFRAMES;
927 urb->interval = BAS_FRAMETIME;
928 urb->complete = read_iso_callback;
929 urb->context = bcs;
930 for (j = 0; j < BAS_NUMFRAMES; j++) {
931 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
932 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
933 urb->iso_frame_desc[j].status = 0;
934 urb->iso_frame_desc[j].actual_length = 0;
937 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
938 rc = usb_submit_urb(urb, GFP_ATOMIC);
939 if (rc != 0)
940 goto error;
943 /* initialize L2 transmission */
944 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
946 /* set up isochronous output URBs for flag idling */
947 for (k = 0; k < BAS_OUTURBS; ++k) {
948 urb = ubc->isoouturbs[k].urb;
949 if (!urb) {
950 rc = -EFAULT;
951 goto error;
953 urb->dev = bcs->cs->hw.bas->udev;
954 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
955 urb->transfer_flags = URB_ISO_ASAP;
956 urb->transfer_buffer = ubc->isooutbuf->data;
957 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
958 urb->number_of_packets = BAS_NUMFRAMES;
959 urb->interval = BAS_FRAMETIME;
960 urb->complete = write_iso_callback;
961 urb->context = &ubc->isoouturbs[k];
962 for (j = 0; j < BAS_NUMFRAMES; ++j) {
963 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
964 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
965 urb->iso_frame_desc[j].status = 0;
966 urb->iso_frame_desc[j].actual_length = 0;
968 ubc->isoouturbs[k].limit = -1;
971 /* keep one URB free, submit the others */
972 for (k = 0; k < BAS_OUTURBS-1; ++k) {
973 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
974 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
975 if (rc != 0)
976 goto error;
978 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
979 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
980 ubc->isooutdone = ubc->isooutovfl = NULL;
981 return 0;
982 error:
983 stopurbs(ubc);
984 return rc;
987 /* stopurbs
988 * cancel the USB request blocks for isochronous input and output
989 * errors are silently ignored
990 * argument:
991 * B channel control structure
993 static void stopurbs(struct bas_bc_state *ubc)
995 int k, rc;
997 ubc->running = 0;
999 for (k = 0; k < BAS_INURBS; ++k) {
1000 rc = usb_unlink_urb(ubc->isoinurbs[k]);
1001 gig_dbg(DEBUG_ISO,
1002 "%s: isoc input URB %d unlinked, result = %s",
1003 __func__, k, get_usb_rcmsg(rc));
1006 for (k = 0; k < BAS_OUTURBS; ++k) {
1007 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
1008 gig_dbg(DEBUG_ISO,
1009 "%s: isoc output URB %d unlinked, result = %s",
1010 __func__, k, get_usb_rcmsg(rc));
1014 /* Isochronous Write - Bottom Half */
1015 /* =============================== */
1017 /* submit_iso_write_urb
1018 * fill and submit the next isochronous write URB
1019 * parameters:
1020 * ucx context structure containing URB
1021 * return value:
1022 * number of frames submitted in URB
1023 * 0 if URB not submitted because no data available (isooutbuf busy)
1024 * error code < 0 on error
1026 static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1028 struct urb *urb = ucx->urb;
1029 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
1030 struct usb_iso_packet_descriptor *ifd;
1031 int corrbytes, nframe, rc;
1033 /* urb->dev is clobbered by USB subsystem */
1034 urb->dev = ucx->bcs->cs->hw.bas->udev;
1035 urb->transfer_flags = URB_ISO_ASAP;
1036 urb->transfer_buffer = ubc->isooutbuf->data;
1037 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1039 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1040 ifd = &urb->iso_frame_desc[nframe];
1042 /* compute frame length according to flow control */
1043 ifd->length = BAS_NORMFRAME;
1044 corrbytes = atomic_read(&ubc->corrbytes);
1045 if (corrbytes != 0) {
1046 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1047 __func__, corrbytes);
1048 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1049 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1050 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1051 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1052 ifd->length += corrbytes;
1053 atomic_add(-corrbytes, &ubc->corrbytes);
1056 /* retrieve block of data to send */
1057 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1058 if (rc < 0) {
1059 if (rc == -EBUSY) {
1060 gig_dbg(DEBUG_ISO,
1061 "%s: buffer busy at frame %d",
1062 __func__, nframe);
1063 /* tasklet will be restarted from
1064 gigaset_isoc_send_skb() */
1065 } else {
1066 dev_err(ucx->bcs->cs->dev,
1067 "%s: buffer error %d at frame %d\n",
1068 __func__, rc, nframe);
1069 return rc;
1071 break;
1073 ifd->offset = rc;
1074 ucx->limit = ubc->isooutbuf->nextread;
1075 ifd->status = 0;
1076 ifd->actual_length = 0;
1078 if (unlikely(nframe == 0))
1079 return 0; /* no data to send */
1080 urb->number_of_packets = nframe;
1082 rc = usb_submit_urb(urb, GFP_ATOMIC);
1083 if (unlikely(rc)) {
1084 if (rc == -ENODEV)
1085 /* device removed - give up silently */
1086 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1087 else
1088 dev_err(ucx->bcs->cs->dev,
1089 "could not submit isochronous write URB: %s\n",
1090 get_usb_rcmsg(rc));
1091 return rc;
1093 ++ubc->numsub;
1094 return nframe;
1097 /* write_iso_tasklet
1098 * tasklet scheduled when an isochronous output URB from the Gigaset device
1099 * has completed
1100 * parameter:
1101 * data B channel state structure
1103 static void write_iso_tasklet(unsigned long data)
1105 struct bc_state *bcs = (struct bc_state *) data;
1106 struct bas_bc_state *ubc = bcs->hw.bas;
1107 struct cardstate *cs = bcs->cs;
1108 struct isow_urbctx_t *done, *next, *ovfl;
1109 struct urb *urb;
1110 int status;
1111 struct usb_iso_packet_descriptor *ifd;
1112 int offset;
1113 unsigned long flags;
1114 int i;
1115 struct sk_buff *skb;
1116 int len;
1117 int rc;
1119 /* loop while completed URBs arrive in time */
1120 for (;;) {
1121 if (unlikely(!(ubc->running))) {
1122 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
1123 return;
1126 /* retrieve completed URBs */
1127 spin_lock_irqsave(&ubc->isooutlock, flags);
1128 done = ubc->isooutdone;
1129 ubc->isooutdone = NULL;
1130 ovfl = ubc->isooutovfl;
1131 ubc->isooutovfl = NULL;
1132 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1133 if (ovfl) {
1134 dev_err(cs->dev, "isochronous write buffer underrun\n");
1135 error_hangup(bcs);
1136 break;
1138 if (!done)
1139 break;
1141 /* submit free URB if available */
1142 spin_lock_irqsave(&ubc->isooutlock, flags);
1143 next = ubc->isooutfree;
1144 ubc->isooutfree = NULL;
1145 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1146 if (next) {
1147 rc = submit_iso_write_urb(next);
1148 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1149 /* could not submit URB, put it back */
1150 spin_lock_irqsave(&ubc->isooutlock, flags);
1151 if (ubc->isooutfree == NULL) {
1152 ubc->isooutfree = next;
1153 next = NULL;
1155 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1156 if (next) {
1157 /* couldn't put it back */
1158 dev_err(cs->dev,
1159 "losing isochronous write URB\n");
1160 error_hangup(bcs);
1165 /* process completed URB */
1166 urb = done->urb;
1167 status = done->status;
1168 switch (status) {
1169 case -EXDEV: /* partial completion */
1170 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1171 __func__);
1172 /* fall through - what's the difference anyway? */
1173 case 0: /* normal completion */
1174 /* inspect individual frames
1175 * assumptions (for lack of documentation):
1176 * - actual_length bytes of first frame in error are
1177 * successfully sent
1178 * - all following frames are not sent at all
1180 offset = done->limit; /* default (no error) */
1181 for (i = 0; i < BAS_NUMFRAMES; i++) {
1182 ifd = &urb->iso_frame_desc[i];
1183 if (ifd->status ||
1184 ifd->actual_length != ifd->length) {
1185 dev_warn(cs->dev,
1186 "isochronous write: frame %d: %s, "
1187 "only %d of %d bytes sent\n",
1188 i, get_usb_statmsg(ifd->status),
1189 ifd->actual_length, ifd->length);
1190 offset = (ifd->offset +
1191 ifd->actual_length)
1192 % BAS_OUTBUFSIZE;
1193 break;
1196 #ifdef CONFIG_GIGASET_DEBUG
1197 /* check assumption on remaining frames */
1198 for (; i < BAS_NUMFRAMES; i++) {
1199 ifd = &urb->iso_frame_desc[i];
1200 if (ifd->status != -EINPROGRESS
1201 || ifd->actual_length != 0) {
1202 dev_warn(cs->dev,
1203 "isochronous write: frame %d: %s, "
1204 "%d of %d bytes sent\n",
1205 i, get_usb_statmsg(ifd->status),
1206 ifd->actual_length, ifd->length);
1207 offset = (ifd->offset +
1208 ifd->actual_length)
1209 % BAS_OUTBUFSIZE;
1210 break;
1213 #endif
1214 break;
1215 case -EPIPE: /* stall - probably underrun */
1216 dev_err(cs->dev, "isochronous write stalled\n");
1217 error_hangup(bcs);
1218 break;
1219 default: /* severe trouble */
1220 dev_warn(cs->dev, "isochronous write: %s\n",
1221 get_usb_statmsg(status));
1224 /* mark the write buffer area covered by this URB as free */
1225 if (done->limit >= 0)
1226 ubc->isooutbuf->read = done->limit;
1228 /* mark URB as free */
1229 spin_lock_irqsave(&ubc->isooutlock, flags);
1230 next = ubc->isooutfree;
1231 ubc->isooutfree = done;
1232 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1233 if (next) {
1234 /* only one URB still active - resubmit one */
1235 rc = submit_iso_write_urb(next);
1236 if (unlikely(rc <= 0 && rc != -ENODEV)) {
1237 /* couldn't submit */
1238 error_hangup(bcs);
1243 /* process queued SKBs */
1244 while ((skb = skb_dequeue(&bcs->squeue))) {
1245 /* copy to output buffer, doing L2 encapsulation */
1246 len = skb->len;
1247 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1248 /* insufficient buffer space, push back onto queue */
1249 skb_queue_head(&bcs->squeue, skb);
1250 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1251 __func__, skb_queue_len(&bcs->squeue));
1252 break;
1254 skb_pull(skb, len);
1255 gigaset_skb_sent(bcs, skb);
1256 dev_kfree_skb_any(skb);
1260 /* Isochronous Read - Bottom Half */
1261 /* ============================== */
1263 /* read_iso_tasklet
1264 * tasklet scheduled when an isochronous input URB from the Gigaset device
1265 * has completed
1266 * parameter:
1267 * data B channel state structure
1269 static void read_iso_tasklet(unsigned long data)
1271 struct bc_state *bcs = (struct bc_state *) data;
1272 struct bas_bc_state *ubc = bcs->hw.bas;
1273 struct cardstate *cs = bcs->cs;
1274 struct urb *urb;
1275 int status;
1276 char *rcvbuf;
1277 unsigned long flags;
1278 int totleft, numbytes, offset, frame, rc;
1280 /* loop while more completed URBs arrive in the meantime */
1281 for (;;) {
1282 /* retrieve URB */
1283 spin_lock_irqsave(&ubc->isoinlock, flags);
1284 urb = ubc->isoindone;
1285 if (!urb) {
1286 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1287 return;
1289 status = ubc->isoinstatus;
1290 ubc->isoindone = NULL;
1291 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
1292 dev_warn(cs->dev,
1293 "isochronous read overrun, "
1294 "dropped URB with status: %s, %d bytes lost\n",
1295 get_usb_statmsg(ubc->loststatus),
1296 ubc->isoinlost);
1297 ubc->loststatus = -EINPROGRESS;
1299 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1301 if (unlikely(!(ubc->running))) {
1302 gig_dbg(DEBUG_ISO,
1303 "%s: channel not running, "
1304 "dropped URB with status: %s",
1305 __func__, get_usb_statmsg(status));
1306 return;
1309 switch (status) {
1310 case 0: /* normal completion */
1311 break;
1312 case -EXDEV: /* inspect individual frames
1313 (we do that anyway) */
1314 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1315 __func__);
1316 break;
1317 case -ENOENT:
1318 case -ECONNRESET:
1319 case -EINPROGRESS:
1320 gig_dbg(DEBUG_ISO, "%s: %s",
1321 __func__, get_usb_statmsg(status));
1322 continue; /* -> skip */
1323 case -EPIPE:
1324 dev_err(cs->dev, "isochronous read stalled\n");
1325 error_hangup(bcs);
1326 continue; /* -> skip */
1327 default: /* severe trouble */
1328 dev_warn(cs->dev, "isochronous read: %s\n",
1329 get_usb_statmsg(status));
1330 goto error;
1333 rcvbuf = urb->transfer_buffer;
1334 totleft = urb->actual_length;
1335 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1336 numbytes = urb->iso_frame_desc[frame].actual_length;
1337 if (unlikely(urb->iso_frame_desc[frame].status))
1338 dev_warn(cs->dev,
1339 "isochronous read: frame %d[%d]: %s\n",
1340 frame, numbytes,
1341 get_usb_statmsg(
1342 urb->iso_frame_desc[frame].status));
1343 if (unlikely(numbytes > BAS_MAXFRAME))
1344 dev_warn(cs->dev,
1345 "isochronous read: frame %d: "
1346 "numbytes (%d) > BAS_MAXFRAME\n",
1347 frame, numbytes);
1348 if (unlikely(numbytes > totleft)) {
1349 dev_warn(cs->dev,
1350 "isochronous read: frame %d: "
1351 "numbytes (%d) > totleft (%d)\n",
1352 frame, numbytes, totleft);
1353 numbytes = totleft;
1355 offset = urb->iso_frame_desc[frame].offset;
1356 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
1357 dev_warn(cs->dev,
1358 "isochronous read: frame %d: "
1359 "offset (%d) + numbytes (%d) "
1360 "> BAS_INBUFSIZE\n",
1361 frame, offset, numbytes);
1362 numbytes = BAS_INBUFSIZE - offset;
1364 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1365 totleft -= numbytes;
1367 if (unlikely(totleft > 0))
1368 dev_warn(cs->dev,
1369 "isochronous read: %d data bytes missing\n",
1370 totleft);
1372 error:
1373 /* URB processed, resubmit */
1374 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1375 urb->iso_frame_desc[frame].status = 0;
1376 urb->iso_frame_desc[frame].actual_length = 0;
1378 /* urb->dev is clobbered by USB subsystem */
1379 urb->dev = bcs->cs->hw.bas->udev;
1380 urb->transfer_flags = URB_ISO_ASAP;
1381 urb->number_of_packets = BAS_NUMFRAMES;
1382 rc = usb_submit_urb(urb, GFP_ATOMIC);
1383 if (unlikely(rc != 0 && rc != -ENODEV)) {
1384 dev_err(cs->dev,
1385 "could not resubmit isochronous read URB: %s\n",
1386 get_usb_rcmsg(rc));
1387 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1388 error_hangup(bcs);
1393 /* Channel Operations */
1394 /* ================== */
1396 /* req_timeout
1397 * timeout routine for control output request
1398 * argument:
1399 * B channel control structure
1401 static void req_timeout(unsigned long data)
1403 struct bc_state *bcs = (struct bc_state *) data;
1404 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1405 int pending;
1406 unsigned long flags;
1408 check_pending(ucs);
1410 spin_lock_irqsave(&ucs->lock, flags);
1411 pending = ucs->pending;
1412 ucs->pending = 0;
1413 spin_unlock_irqrestore(&ucs->lock, flags);
1415 switch (pending) {
1416 case 0: /* no pending request */
1417 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
1418 break;
1420 case HD_OPEN_ATCHANNEL:
1421 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
1422 error_reset(bcs->cs);
1423 break;
1425 case HD_OPEN_B2CHANNEL:
1426 case HD_OPEN_B1CHANNEL:
1427 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1428 bcs->channel + 1);
1429 error_hangup(bcs);
1430 break;
1432 case HD_CLOSE_ATCHANNEL:
1433 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
1434 error_reset(bcs->cs);
1435 break;
1437 case HD_CLOSE_B2CHANNEL:
1438 case HD_CLOSE_B1CHANNEL:
1439 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1440 bcs->channel + 1);
1441 error_reset(bcs->cs);
1442 break;
1444 case HD_RESET_INTERRUPT_PIPE:
1445 /* error recovery escalation */
1446 dev_err(bcs->cs->dev,
1447 "reset interrupt pipe timeout, attempting USB reset\n");
1448 usb_queue_reset_device(bcs->cs->hw.bas->interface);
1449 break;
1451 default:
1452 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1453 pending);
1456 wake_up(&ucs->waitqueue);
1459 /* write_ctrl_callback
1460 * USB completion handler for control pipe output
1461 * called by the USB subsystem in interrupt context
1462 * parameter:
1463 * urb USB request block of completed request
1464 * urb->context = hardware specific controller state structure
1466 static void write_ctrl_callback(struct urb *urb)
1468 struct bas_cardstate *ucs = urb->context;
1469 int status = urb->status;
1470 int rc;
1471 unsigned long flags;
1473 /* check status */
1474 switch (status) {
1475 case 0: /* normal completion */
1476 spin_lock_irqsave(&ucs->lock, flags);
1477 switch (ucs->pending) {
1478 case HD_DEVICE_INIT_ACK: /* no reply expected */
1479 del_timer(&ucs->timer_ctrl);
1480 ucs->pending = 0;
1481 break;
1483 spin_unlock_irqrestore(&ucs->lock, flags);
1484 return;
1486 case -ENOENT: /* cancelled */
1487 case -ECONNRESET: /* cancelled (async) */
1488 case -EINPROGRESS: /* pending */
1489 case -ENODEV: /* device removed */
1490 case -ESHUTDOWN: /* device shut down */
1491 /* ignore silently */
1492 gig_dbg(DEBUG_USBREQ, "%s: %s",
1493 __func__, get_usb_statmsg(status));
1494 break;
1496 default: /* any failure */
1497 /* don't retry if suspend requested */
1498 if (++ucs->retry_ctrl > BAS_RETRY ||
1499 (ucs->basstate & BS_SUSPEND)) {
1500 dev_err(&ucs->interface->dev,
1501 "control request 0x%02x failed: %s\n",
1502 ucs->dr_ctrl.bRequest,
1503 get_usb_statmsg(status));
1504 break; /* give up */
1506 dev_notice(&ucs->interface->dev,
1507 "control request 0x%02x: %s, retry %d\n",
1508 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
1509 ucs->retry_ctrl);
1510 /* urb->dev is clobbered by USB subsystem */
1511 urb->dev = ucs->udev;
1512 rc = usb_submit_urb(urb, GFP_ATOMIC);
1513 if (unlikely(rc)) {
1514 dev_err(&ucs->interface->dev,
1515 "could not resubmit request 0x%02x: %s\n",
1516 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1517 break;
1519 /* resubmitted */
1520 return;
1523 /* failed, clear pending request */
1524 spin_lock_irqsave(&ucs->lock, flags);
1525 del_timer(&ucs->timer_ctrl);
1526 ucs->pending = 0;
1527 spin_unlock_irqrestore(&ucs->lock, flags);
1528 wake_up(&ucs->waitqueue);
1531 /* req_submit
1532 * submit a control output request without message buffer to the Gigaset base
1533 * and optionally start a timeout
1534 * parameters:
1535 * bcs B channel control structure
1536 * req control request code (HD_*)
1537 * val control request parameter value (set to 0 if unused)
1538 * timeout timeout in seconds (0: no timeout)
1539 * return value:
1540 * 0 on success
1541 * -EBUSY if another request is pending
1542 * any URB submission error code
1544 static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1546 struct bas_cardstate *ucs = bcs->cs->hw.bas;
1547 int ret;
1548 unsigned long flags;
1550 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
1552 spin_lock_irqsave(&ucs->lock, flags);
1553 if (ucs->pending) {
1554 spin_unlock_irqrestore(&ucs->lock, flags);
1555 dev_err(bcs->cs->dev,
1556 "submission of request 0x%02x failed: "
1557 "request 0x%02x still pending\n",
1558 req, ucs->pending);
1559 return -EBUSY;
1562 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1563 ucs->dr_ctrl.bRequest = req;
1564 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1565 ucs->dr_ctrl.wIndex = 0;
1566 ucs->dr_ctrl.wLength = 0;
1567 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
1568 usb_sndctrlpipe(ucs->udev, 0),
1569 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
1570 write_ctrl_callback, ucs);
1571 ucs->retry_ctrl = 0;
1572 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
1573 if (unlikely(ret)) {
1574 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
1575 req, get_usb_rcmsg(ret));
1576 spin_unlock_irqrestore(&ucs->lock, flags);
1577 return ret;
1579 ucs->pending = req;
1581 if (timeout > 0) {
1582 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
1583 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1584 ucs->timer_ctrl.data = (unsigned long) bcs;
1585 ucs->timer_ctrl.function = req_timeout;
1586 add_timer(&ucs->timer_ctrl);
1589 spin_unlock_irqrestore(&ucs->lock, flags);
1590 return 0;
1593 /* gigaset_init_bchannel
1594 * called by common.c to connect a B channel
1595 * initialize isochronous I/O and tell the Gigaset base to open the channel
1596 * argument:
1597 * B channel control structure
1598 * return value:
1599 * 0 on success, error code < 0 on error
1601 static int gigaset_init_bchannel(struct bc_state *bcs)
1603 struct cardstate *cs = bcs->cs;
1604 int req, ret;
1605 unsigned long flags;
1607 spin_lock_irqsave(&cs->lock, flags);
1608 if (unlikely(!cs->connected)) {
1609 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1610 spin_unlock_irqrestore(&cs->lock, flags);
1611 return -ENODEV;
1614 if (cs->hw.bas->basstate & BS_SUSPEND) {
1615 dev_notice(cs->dev,
1616 "not starting isochronous I/O, "
1617 "suspend in progress\n");
1618 spin_unlock_irqrestore(&cs->lock, flags);
1619 return -EHOSTUNREACH;
1622 ret = starturbs(bcs);
1623 if (ret < 0) {
1624 dev_err(cs->dev,
1625 "could not start isochronous I/O for channel B%d: %s\n",
1626 bcs->channel + 1,
1627 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1628 if (ret != -ENODEV)
1629 error_hangup(bcs);
1630 spin_unlock_irqrestore(&cs->lock, flags);
1631 return ret;
1634 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1635 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1636 if (ret < 0) {
1637 dev_err(cs->dev, "could not open channel B%d\n",
1638 bcs->channel + 1);
1639 stopurbs(bcs->hw.bas);
1640 if (ret != -ENODEV)
1641 error_hangup(bcs);
1644 spin_unlock_irqrestore(&cs->lock, flags);
1645 return ret;
1648 /* gigaset_close_bchannel
1649 * called by common.c to disconnect a B channel
1650 * tell the Gigaset base to close the channel
1651 * stopping isochronous I/O and LL notification will be done when the
1652 * acknowledgement for the close arrives
1653 * argument:
1654 * B channel control structure
1655 * return value:
1656 * 0 on success, error code < 0 on error
1658 static int gigaset_close_bchannel(struct bc_state *bcs)
1660 struct cardstate *cs = bcs->cs;
1661 int req, ret;
1662 unsigned long flags;
1664 spin_lock_irqsave(&cs->lock, flags);
1665 if (unlikely(!cs->connected)) {
1666 spin_unlock_irqrestore(&cs->lock, flags);
1667 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1668 return -ENODEV;
1671 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
1672 /* channel not running: just signal common.c */
1673 spin_unlock_irqrestore(&cs->lock, flags);
1674 gigaset_bchannel_down(bcs);
1675 return 0;
1678 /* channel running: tell device to close it */
1679 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1680 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1681 if (ret < 0)
1682 dev_err(cs->dev, "closing channel B%d failed\n",
1683 bcs->channel + 1);
1685 spin_unlock_irqrestore(&cs->lock, flags);
1686 return ret;
1689 /* Device Operations */
1690 /* ================= */
1692 /* complete_cb
1693 * unqueue first command buffer from queue, waking any sleepers
1694 * must be called with cs->cmdlock held
1695 * parameter:
1696 * cs controller state structure
1698 static void complete_cb(struct cardstate *cs)
1700 struct cmdbuf_t *cb = cs->cmdbuf;
1702 /* unqueue completed buffer */
1703 cs->cmdbytes -= cs->curlen;
1704 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
1705 cs->curlen, cs->cmdbytes);
1706 if (cb->next != NULL) {
1707 cs->cmdbuf = cb->next;
1708 cs->cmdbuf->prev = NULL;
1709 cs->curlen = cs->cmdbuf->len;
1710 } else {
1711 cs->cmdbuf = NULL;
1712 cs->lastcmdbuf = NULL;
1713 cs->curlen = 0;
1716 if (cb->wake_tasklet)
1717 tasklet_schedule(cb->wake_tasklet);
1719 kfree(cb);
1722 /* write_command_callback
1723 * USB completion handler for AT command transmission
1724 * called by the USB subsystem in interrupt context
1725 * parameter:
1726 * urb USB request block of completed request
1727 * urb->context = controller state structure
1729 static void write_command_callback(struct urb *urb)
1731 struct cardstate *cs = urb->context;
1732 struct bas_cardstate *ucs = cs->hw.bas;
1733 int status = urb->status;
1734 unsigned long flags;
1736 update_basstate(ucs, 0, BS_ATWRPEND);
1737 wake_up(&ucs->waitqueue);
1739 /* check status */
1740 switch (status) {
1741 case 0: /* normal completion */
1742 break;
1743 case -ENOENT: /* cancelled */
1744 case -ECONNRESET: /* cancelled (async) */
1745 case -EINPROGRESS: /* pending */
1746 case -ENODEV: /* device removed */
1747 case -ESHUTDOWN: /* device shut down */
1748 /* ignore silently */
1749 gig_dbg(DEBUG_USBREQ, "%s: %s",
1750 __func__, get_usb_statmsg(status));
1751 return;
1752 default: /* any failure */
1753 if (++ucs->retry_cmd_out > BAS_RETRY) {
1754 dev_warn(cs->dev,
1755 "command write: %s, "
1756 "giving up after %d retries\n",
1757 get_usb_statmsg(status),
1758 ucs->retry_cmd_out);
1759 break;
1761 if (ucs->basstate & BS_SUSPEND) {
1762 dev_warn(cs->dev,
1763 "command write: %s, "
1764 "won't retry - suspend requested\n",
1765 get_usb_statmsg(status));
1766 break;
1768 if (cs->cmdbuf == NULL) {
1769 dev_warn(cs->dev,
1770 "command write: %s, "
1771 "cannot retry - cmdbuf gone\n",
1772 get_usb_statmsg(status));
1773 break;
1775 dev_notice(cs->dev, "command write: %s, retry %d\n",
1776 get_usb_statmsg(status), ucs->retry_cmd_out);
1777 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1778 /* resubmitted - bypass regular exit block */
1779 return;
1780 /* command send failed, assume base still waiting */
1781 update_basstate(ucs, BS_ATREADY, 0);
1784 spin_lock_irqsave(&cs->cmdlock, flags);
1785 if (cs->cmdbuf != NULL)
1786 complete_cb(cs);
1787 spin_unlock_irqrestore(&cs->cmdlock, flags);
1790 /* atrdy_timeout
1791 * timeout routine for AT command transmission
1792 * argument:
1793 * controller state structure
1795 static void atrdy_timeout(unsigned long data)
1797 struct cardstate *cs = (struct cardstate *) data;
1798 struct bas_cardstate *ucs = cs->hw.bas;
1800 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
1802 /* fake the missing signal - what else can I do? */
1803 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1804 start_cbsend(cs);
1807 /* atwrite_submit
1808 * submit an HD_WRITE_ATMESSAGE command URB
1809 * parameters:
1810 * cs controller state structure
1811 * buf buffer containing command to send
1812 * len length of command to send
1813 * return value:
1814 * 0 on success
1815 * -EBUSY if another request is pending
1816 * any URB submission error code
1818 static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1820 struct bas_cardstate *ucs = cs->hw.bas;
1821 int rc;
1823 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
1825 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
1826 dev_err(cs->dev,
1827 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
1828 return -EBUSY;
1831 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1832 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1833 ucs->dr_cmd_out.wValue = 0;
1834 ucs->dr_cmd_out.wIndex = 0;
1835 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1836 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1837 usb_sndctrlpipe(ucs->udev, 0),
1838 (unsigned char *) &ucs->dr_cmd_out, buf, len,
1839 write_command_callback, cs);
1840 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
1841 if (unlikely(rc)) {
1842 update_basstate(ucs, 0, BS_ATWRPEND);
1843 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
1844 get_usb_rcmsg(rc));
1845 return rc;
1848 /* submitted successfully, start timeout if necessary */
1849 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
1850 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1851 ATRDY_TIMEOUT);
1852 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1853 ucs->timer_atrdy.data = (unsigned long) cs;
1854 ucs->timer_atrdy.function = atrdy_timeout;
1855 add_timer(&ucs->timer_atrdy);
1857 return 0;
1860 /* start_cbsend
1861 * start transmission of AT command queue if necessary
1862 * parameter:
1863 * cs controller state structure
1864 * return value:
1865 * 0 on success
1866 * error code < 0 on error
1868 static int start_cbsend(struct cardstate *cs)
1870 struct cmdbuf_t *cb;
1871 struct bas_cardstate *ucs = cs->hw.bas;
1872 unsigned long flags;
1873 int rc;
1874 int retval = 0;
1876 /* check if suspend requested */
1877 if (ucs->basstate & BS_SUSPEND) {
1878 gig_dbg(DEBUG_OUTPUT, "suspending");
1879 return -EHOSTUNREACH;
1882 /* check if AT channel is open */
1883 if (!(ucs->basstate & BS_ATOPEN)) {
1884 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
1885 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1886 if (rc < 0) {
1887 /* flush command queue */
1888 spin_lock_irqsave(&cs->cmdlock, flags);
1889 while (cs->cmdbuf != NULL)
1890 complete_cb(cs);
1891 spin_unlock_irqrestore(&cs->cmdlock, flags);
1893 return rc;
1896 /* try to send first command in queue */
1897 spin_lock_irqsave(&cs->cmdlock, flags);
1899 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
1900 ucs->retry_cmd_out = 0;
1901 rc = atwrite_submit(cs, cb->buf, cb->len);
1902 if (unlikely(rc)) {
1903 retval = rc;
1904 complete_cb(cs);
1908 spin_unlock_irqrestore(&cs->cmdlock, flags);
1909 return retval;
1912 /* gigaset_write_cmd
1913 * This function is called by the device independent part of the driver
1914 * to transmit an AT command string to the Gigaset device.
1915 * It encapsulates the device specific method for transmission over the
1916 * direct USB connection to the base.
1917 * The command string is added to the queue of commands to send, and
1918 * USB transmission is started if necessary.
1919 * parameters:
1920 * cs controller state structure
1921 * buf command string to send
1922 * len number of bytes to send (max. IF_WRITEBUF)
1923 * wake_tasklet tasklet to run when transmission is completed
1924 * (NULL if none)
1925 * return value:
1926 * number of bytes queued on success
1927 * error code < 0 on error
1929 static int gigaset_write_cmd(struct cardstate *cs,
1930 const unsigned char *buf, int len,
1931 struct tasklet_struct *wake_tasklet)
1933 struct cmdbuf_t *cb;
1934 unsigned long flags;
1935 int rc;
1937 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
1938 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
1939 "CMD Transmit", len, buf);
1941 if (len <= 0) {
1942 /* nothing to do */
1943 rc = 0;
1944 goto notqueued;
1947 /* translate "+++" escape sequence sent as a single separate command
1948 * into "close AT channel" command for error recovery
1949 * The next command will reopen the AT channel automatically.
1951 if (len == 3 && !memcmp(buf, "+++", 3)) {
1952 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
1953 goto notqueued;
1956 if (len > IF_WRITEBUF)
1957 len = IF_WRITEBUF;
1958 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
1959 if (!cb) {
1960 dev_err(cs->dev, "%s: out of memory\n", __func__);
1961 rc = -ENOMEM;
1962 goto notqueued;
1965 memcpy(cb->buf, buf, len);
1966 cb->len = len;
1967 cb->offset = 0;
1968 cb->next = NULL;
1969 cb->wake_tasklet = wake_tasklet;
1971 spin_lock_irqsave(&cs->cmdlock, flags);
1972 cb->prev = cs->lastcmdbuf;
1973 if (cs->lastcmdbuf)
1974 cs->lastcmdbuf->next = cb;
1975 else {
1976 cs->cmdbuf = cb;
1977 cs->curlen = len;
1979 cs->cmdbytes += len;
1980 cs->lastcmdbuf = cb;
1981 spin_unlock_irqrestore(&cs->cmdlock, flags);
1983 spin_lock_irqsave(&cs->lock, flags);
1984 if (unlikely(!cs->connected)) {
1985 spin_unlock_irqrestore(&cs->lock, flags);
1986 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1987 /* flush command queue */
1988 spin_lock_irqsave(&cs->cmdlock, flags);
1989 while (cs->cmdbuf != NULL)
1990 complete_cb(cs);
1991 spin_unlock_irqrestore(&cs->cmdlock, flags);
1992 return -ENODEV;
1994 rc = start_cbsend(cs);
1995 spin_unlock_irqrestore(&cs->lock, flags);
1996 return rc < 0 ? rc : len;
1998 notqueued: /* request handled without queuing */
1999 if (wake_tasklet)
2000 tasklet_schedule(wake_tasklet);
2001 return rc;
2004 /* gigaset_write_room
2005 * tty_driver.write_room interface routine
2006 * return number of characters the driver will accept to be written via
2007 * gigaset_write_cmd
2008 * parameter:
2009 * controller state structure
2010 * return value:
2011 * number of characters
2013 static int gigaset_write_room(struct cardstate *cs)
2015 return IF_WRITEBUF;
2018 /* gigaset_chars_in_buffer
2019 * tty_driver.chars_in_buffer interface routine
2020 * return number of characters waiting to be sent
2021 * parameter:
2022 * controller state structure
2023 * return value:
2024 * number of characters
2026 static int gigaset_chars_in_buffer(struct cardstate *cs)
2028 return cs->cmdbytes;
2031 /* gigaset_brkchars
2032 * implementation of ioctl(GIGASET_BRKCHARS)
2033 * parameter:
2034 * controller state structure
2035 * return value:
2036 * -EINVAL (unimplemented function)
2038 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2040 return -EINVAL;
2044 /* Device Initialization/Shutdown */
2045 /* ============================== */
2047 /* Free hardware dependent part of the B channel structure
2048 * parameter:
2049 * bcs B channel structure
2050 * return value:
2051 * !=0 on success
2053 static int gigaset_freebcshw(struct bc_state *bcs)
2055 struct bas_bc_state *ubc = bcs->hw.bas;
2056 int i;
2058 if (!ubc)
2059 return 0;
2061 /* kill URBs and tasklets before freeing - better safe than sorry */
2062 ubc->running = 0;
2063 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2064 for (i = 0; i < BAS_OUTURBS; ++i) {
2065 usb_kill_urb(ubc->isoouturbs[i].urb);
2066 usb_free_urb(ubc->isoouturbs[i].urb);
2068 for (i = 0; i < BAS_INURBS; ++i) {
2069 usb_kill_urb(ubc->isoinurbs[i]);
2070 usb_free_urb(ubc->isoinurbs[i]);
2072 tasklet_kill(&ubc->sent_tasklet);
2073 tasklet_kill(&ubc->rcvd_tasklet);
2074 kfree(ubc->isooutbuf);
2075 kfree(ubc);
2076 bcs->hw.bas = NULL;
2077 return 1;
2080 /* Initialize hardware dependent part of the B channel structure
2081 * parameter:
2082 * bcs B channel structure
2083 * return value:
2084 * !=0 on success
2086 static int gigaset_initbcshw(struct bc_state *bcs)
2088 int i;
2089 struct bas_bc_state *ubc;
2091 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2092 if (!ubc) {
2093 pr_err("out of memory\n");
2094 return 0;
2097 ubc->running = 0;
2098 atomic_set(&ubc->corrbytes, 0);
2099 spin_lock_init(&ubc->isooutlock);
2100 for (i = 0; i < BAS_OUTURBS; ++i) {
2101 ubc->isoouturbs[i].urb = NULL;
2102 ubc->isoouturbs[i].bcs = bcs;
2104 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2105 ubc->numsub = 0;
2106 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2107 if (!ubc->isooutbuf) {
2108 pr_err("out of memory\n");
2109 kfree(ubc);
2110 bcs->hw.bas = NULL;
2111 return 0;
2113 tasklet_init(&ubc->sent_tasklet,
2114 write_iso_tasklet, (unsigned long) bcs);
2116 spin_lock_init(&ubc->isoinlock);
2117 for (i = 0; i < BAS_INURBS; ++i)
2118 ubc->isoinurbs[i] = NULL;
2119 ubc->isoindone = NULL;
2120 ubc->loststatus = -EINPROGRESS;
2121 ubc->isoinlost = 0;
2122 ubc->seqlen = 0;
2123 ubc->inbyte = 0;
2124 ubc->inbits = 0;
2125 ubc->goodbytes = 0;
2126 ubc->alignerrs = 0;
2127 ubc->fcserrs = 0;
2128 ubc->frameerrs = 0;
2129 ubc->giants = 0;
2130 ubc->runts = 0;
2131 ubc->aborts = 0;
2132 ubc->shared0s = 0;
2133 ubc->stolen0s = 0;
2134 tasklet_init(&ubc->rcvd_tasklet,
2135 read_iso_tasklet, (unsigned long) bcs);
2136 return 1;
2139 static void gigaset_reinitbcshw(struct bc_state *bcs)
2141 struct bas_bc_state *ubc = bcs->hw.bas;
2143 bcs->hw.bas->running = 0;
2144 atomic_set(&bcs->hw.bas->corrbytes, 0);
2145 bcs->hw.bas->numsub = 0;
2146 spin_lock_init(&ubc->isooutlock);
2147 spin_lock_init(&ubc->isoinlock);
2148 ubc->loststatus = -EINPROGRESS;
2151 static void gigaset_freecshw(struct cardstate *cs)
2153 /* timers, URBs and rcvbuf are disposed of in disconnect */
2154 kfree(cs->hw.bas->int_in_buf);
2155 kfree(cs->hw.bas);
2156 cs->hw.bas = NULL;
2159 static int gigaset_initcshw(struct cardstate *cs)
2161 struct bas_cardstate *ucs;
2163 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2164 if (!ucs) {
2165 pr_err("out of memory\n");
2166 return 0;
2168 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2169 if (!ucs->int_in_buf) {
2170 kfree(ucs);
2171 pr_err("out of memory\n");
2172 return 0;
2175 ucs->urb_cmd_in = NULL;
2176 ucs->urb_cmd_out = NULL;
2177 ucs->rcvbuf = NULL;
2178 ucs->rcvbuf_size = 0;
2180 spin_lock_init(&ucs->lock);
2181 ucs->pending = 0;
2183 ucs->basstate = 0;
2184 init_timer(&ucs->timer_ctrl);
2185 init_timer(&ucs->timer_atrdy);
2186 init_timer(&ucs->timer_cmd_in);
2187 init_waitqueue_head(&ucs->waitqueue);
2189 return 1;
2192 /* freeurbs
2193 * unlink and deallocate all URBs unconditionally
2194 * caller must make sure that no commands are still in progress
2195 * parameter:
2196 * cs controller state structure
2198 static void freeurbs(struct cardstate *cs)
2200 struct bas_cardstate *ucs = cs->hw.bas;
2201 struct bas_bc_state *ubc;
2202 int i, j;
2204 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
2205 for (j = 0; j < BAS_CHANNELS; ++j) {
2206 ubc = cs->bcs[j].hw.bas;
2207 for (i = 0; i < BAS_OUTURBS; ++i) {
2208 usb_kill_urb(ubc->isoouturbs[i].urb);
2209 usb_free_urb(ubc->isoouturbs[i].urb);
2210 ubc->isoouturbs[i].urb = NULL;
2212 for (i = 0; i < BAS_INURBS; ++i) {
2213 usb_kill_urb(ubc->isoinurbs[i]);
2214 usb_free_urb(ubc->isoinurbs[i]);
2215 ubc->isoinurbs[i] = NULL;
2218 usb_kill_urb(ucs->urb_int_in);
2219 usb_free_urb(ucs->urb_int_in);
2220 ucs->urb_int_in = NULL;
2221 usb_kill_urb(ucs->urb_cmd_out);
2222 usb_free_urb(ucs->urb_cmd_out);
2223 ucs->urb_cmd_out = NULL;
2224 usb_kill_urb(ucs->urb_cmd_in);
2225 usb_free_urb(ucs->urb_cmd_in);
2226 ucs->urb_cmd_in = NULL;
2227 usb_kill_urb(ucs->urb_ctrl);
2228 usb_free_urb(ucs->urb_ctrl);
2229 ucs->urb_ctrl = NULL;
2232 /* gigaset_probe
2233 * This function is called when a new USB device is connected.
2234 * It checks whether the new device is handled by this driver.
2236 static int gigaset_probe(struct usb_interface *interface,
2237 const struct usb_device_id *id)
2239 struct usb_host_interface *hostif;
2240 struct usb_device *udev = interface_to_usbdev(interface);
2241 struct cardstate *cs = NULL;
2242 struct bas_cardstate *ucs = NULL;
2243 struct bas_bc_state *ubc;
2244 struct usb_endpoint_descriptor *endpoint;
2245 int i, j;
2246 int rc;
2248 gig_dbg(DEBUG_INIT,
2249 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2250 __func__, le16_to_cpu(udev->descriptor.idVendor),
2251 le16_to_cpu(udev->descriptor.idProduct));
2253 /* set required alternate setting */
2254 hostif = interface->cur_altsetting;
2255 if (hostif->desc.bAlternateSetting != 3) {
2256 gig_dbg(DEBUG_INIT,
2257 "%s: wrong alternate setting %d - trying to switch",
2258 __func__, hostif->desc.bAlternateSetting);
2259 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2260 < 0) {
2261 dev_warn(&udev->dev, "usb_set_interface failed, "
2262 "device %d interface %d altsetting %d\n",
2263 udev->devnum, hostif->desc.bInterfaceNumber,
2264 hostif->desc.bAlternateSetting);
2265 return -ENODEV;
2267 hostif = interface->cur_altsetting;
2270 /* Reject application specific interfaces
2272 if (hostif->desc.bInterfaceClass != 255) {
2273 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2274 __func__, hostif->desc.bInterfaceClass);
2275 return -ENODEV;
2278 dev_info(&udev->dev,
2279 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2280 __func__, le16_to_cpu(udev->descriptor.idVendor),
2281 le16_to_cpu(udev->descriptor.idProduct));
2283 /* allocate memory for our device state and intialize it */
2284 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2285 GIGASET_MODULENAME);
2286 if (!cs)
2287 return -ENODEV;
2288 ucs = cs->hw.bas;
2290 /* save off device structure ptrs for later use */
2291 usb_get_dev(udev);
2292 ucs->udev = udev;
2293 ucs->interface = interface;
2294 cs->dev = &interface->dev;
2296 /* allocate URBs:
2297 * - one for the interrupt pipe
2298 * - three for the different uses of the default control pipe
2299 * - three for each isochronous pipe
2301 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2302 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2303 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2304 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
2305 goto allocerr;
2307 for (j = 0; j < BAS_CHANNELS; ++j) {
2308 ubc = cs->bcs[j].hw.bas;
2309 for (i = 0; i < BAS_OUTURBS; ++i)
2310 if (!(ubc->isoouturbs[i].urb =
2311 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2312 goto allocerr;
2313 for (i = 0; i < BAS_INURBS; ++i)
2314 if (!(ubc->isoinurbs[i] =
2315 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
2316 goto allocerr;
2319 ucs->rcvbuf = NULL;
2320 ucs->rcvbuf_size = 0;
2322 /* Fill the interrupt urb and send it to the core */
2323 endpoint = &hostif->endpoint[0].desc;
2324 usb_fill_int_urb(ucs->urb_int_in, udev,
2325 usb_rcvintpipe(udev,
2326 (endpoint->bEndpointAddress) & 0x0f),
2327 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
2328 endpoint->bInterval);
2329 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2330 if (rc != 0) {
2331 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
2332 get_usb_rcmsg(rc));
2333 goto error;
2336 /* tell the device that the driver is ready */
2337 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2338 if (rc != 0)
2339 goto error;
2341 /* tell common part that the device is ready */
2342 if (startmode == SM_LOCKED)
2343 cs->mstate = MS_LOCKED;
2345 /* save address of controller structure */
2346 usb_set_intfdata(interface, cs);
2348 if (!gigaset_start(cs))
2349 goto error;
2351 return 0;
2353 allocerr:
2354 dev_err(cs->dev, "could not allocate URBs\n");
2355 error:
2356 freeurbs(cs);
2357 usb_set_intfdata(interface, NULL);
2358 gigaset_freecs(cs);
2359 return -ENODEV;
2362 /* gigaset_disconnect
2363 * This function is called when the Gigaset base is unplugged.
2365 static void gigaset_disconnect(struct usb_interface *interface)
2367 struct cardstate *cs;
2368 struct bas_cardstate *ucs;
2369 int j;
2371 cs = usb_get_intfdata(interface);
2373 ucs = cs->hw.bas;
2375 dev_info(cs->dev, "disconnecting Gigaset base\n");
2377 /* mark base as not ready, all channels disconnected */
2378 ucs->basstate = 0;
2380 /* tell LL all channels are down */
2381 for (j = 0; j < BAS_CHANNELS; ++j)
2382 gigaset_bchannel_down(cs->bcs + j);
2384 /* stop driver (common part) */
2385 gigaset_stop(cs);
2387 /* stop timers and URBs, free ressources */
2388 del_timer_sync(&ucs->timer_ctrl);
2389 del_timer_sync(&ucs->timer_atrdy);
2390 del_timer_sync(&ucs->timer_cmd_in);
2391 freeurbs(cs);
2392 usb_set_intfdata(interface, NULL);
2393 kfree(ucs->rcvbuf);
2394 ucs->rcvbuf = NULL;
2395 ucs->rcvbuf_size = 0;
2396 usb_put_dev(ucs->udev);
2397 ucs->interface = NULL;
2398 ucs->udev = NULL;
2399 cs->dev = NULL;
2400 gigaset_freecs(cs);
2403 /* gigaset_suspend
2404 * This function is called before the USB connection is suspended.
2406 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2408 struct cardstate *cs = usb_get_intfdata(intf);
2409 struct bas_cardstate *ucs = cs->hw.bas;
2410 int rc;
2412 /* set suspend flag; this stops AT command/response traffic */
2413 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2414 gig_dbg(DEBUG_SUSPEND, "already suspended");
2415 return 0;
2418 /* wait a bit for blocking conditions to go away */
2419 rc = wait_event_timeout(ucs->waitqueue,
2420 !(ucs->basstate &
2421 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2422 BAS_TIMEOUT*HZ/10);
2423 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2425 /* check for conditions preventing suspend */
2426 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
2427 dev_warn(cs->dev, "cannot suspend:\n");
2428 if (ucs->basstate & BS_B1OPEN)
2429 dev_warn(cs->dev, " B channel 1 open\n");
2430 if (ucs->basstate & BS_B2OPEN)
2431 dev_warn(cs->dev, " B channel 2 open\n");
2432 if (ucs->basstate & BS_ATRDPEND)
2433 dev_warn(cs->dev, " receiving AT reply\n");
2434 if (ucs->basstate & BS_ATWRPEND)
2435 dev_warn(cs->dev, " sending AT command\n");
2436 update_basstate(ucs, 0, BS_SUSPEND);
2437 return -EBUSY;
2440 /* close AT channel if open */
2441 if (ucs->basstate & BS_ATOPEN) {
2442 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2443 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2444 if (rc) {
2445 update_basstate(ucs, 0, BS_SUSPEND);
2446 return rc;
2448 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2449 BAS_TIMEOUT*HZ/10);
2450 /* in case of timeout, proceed anyway */
2453 /* kill all URBs and timers that might still be pending */
2454 usb_kill_urb(ucs->urb_ctrl);
2455 usb_kill_urb(ucs->urb_int_in);
2456 del_timer_sync(&ucs->timer_ctrl);
2458 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2459 return 0;
2462 /* gigaset_resume
2463 * This function is called after the USB connection has been resumed.
2465 static int gigaset_resume(struct usb_interface *intf)
2467 struct cardstate *cs = usb_get_intfdata(intf);
2468 struct bas_cardstate *ucs = cs->hw.bas;
2469 int rc;
2471 /* resubmit interrupt URB for spontaneous messages from base */
2472 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2473 if (rc) {
2474 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2475 get_usb_rcmsg(rc));
2476 return rc;
2479 /* clear suspend flag to reallow activity */
2480 update_basstate(ucs, 0, BS_SUSPEND);
2482 gig_dbg(DEBUG_SUSPEND, "resume complete");
2483 return 0;
2486 /* gigaset_pre_reset
2487 * This function is called before the USB connection is reset.
2489 static int gigaset_pre_reset(struct usb_interface *intf)
2491 /* handle just like suspend */
2492 return gigaset_suspend(intf, PMSG_ON);
2495 /* gigaset_post_reset
2496 * This function is called after the USB connection has been reset.
2498 static int gigaset_post_reset(struct usb_interface *intf)
2500 /* FIXME: send HD_DEVICE_INIT_ACK? */
2502 /* resume operations */
2503 return gigaset_resume(intf);
2507 static const struct gigaset_ops gigops = {
2508 gigaset_write_cmd,
2509 gigaset_write_room,
2510 gigaset_chars_in_buffer,
2511 gigaset_brkchars,
2512 gigaset_init_bchannel,
2513 gigaset_close_bchannel,
2514 gigaset_initbcshw,
2515 gigaset_freebcshw,
2516 gigaset_reinitbcshw,
2517 gigaset_initcshw,
2518 gigaset_freecshw,
2519 gigaset_set_modem_ctrl,
2520 gigaset_baud_rate,
2521 gigaset_set_line_ctrl,
2522 gigaset_isoc_send_skb,
2523 gigaset_isoc_input,
2526 /* bas_gigaset_init
2527 * This function is called after the kernel module is loaded.
2529 static int __init bas_gigaset_init(void)
2531 int result;
2533 /* allocate memory for our driver state and intialize it */
2534 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2535 GIGASET_MODULENAME, GIGASET_DEVNAME,
2536 &gigops, THIS_MODULE);
2537 if (driver == NULL)
2538 goto error;
2540 /* register this driver with the USB subsystem */
2541 result = usb_register(&gigaset_usb_driver);
2542 if (result < 0) {
2543 pr_err("error %d registering USB driver\n", -result);
2544 goto error;
2547 pr_info(DRIVER_DESC "\n");
2548 return 0;
2550 error:
2551 if (driver)
2552 gigaset_freedriver(driver);
2553 driver = NULL;
2554 return -1;
2557 /* bas_gigaset_exit
2558 * This function is called before the kernel module is unloaded.
2560 static void __exit bas_gigaset_exit(void)
2562 struct bas_cardstate *ucs;
2563 int i;
2565 gigaset_blockdriver(driver); /* => probe will fail
2566 * => no gigaset_start any more
2569 /* stop all connected devices */
2570 for (i = 0; i < driver->minors; i++) {
2571 if (gigaset_shutdown(driver->cs + i) < 0)
2572 continue; /* no device */
2573 /* from now on, no isdn callback should be possible */
2575 /* close all still open channels */
2576 ucs = driver->cs[i].hw.bas;
2577 if (ucs->basstate & BS_B1OPEN) {
2578 gig_dbg(DEBUG_INIT, "closing B1 channel");
2579 usb_control_msg(ucs->udev,
2580 usb_sndctrlpipe(ucs->udev, 0),
2581 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2582 0, 0, NULL, 0, BAS_TIMEOUT);
2584 if (ucs->basstate & BS_B2OPEN) {
2585 gig_dbg(DEBUG_INIT, "closing B2 channel");
2586 usb_control_msg(ucs->udev,
2587 usb_sndctrlpipe(ucs->udev, 0),
2588 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2589 0, 0, NULL, 0, BAS_TIMEOUT);
2591 if (ucs->basstate & BS_ATOPEN) {
2592 gig_dbg(DEBUG_INIT, "closing AT channel");
2593 usb_control_msg(ucs->udev,
2594 usb_sndctrlpipe(ucs->udev, 0),
2595 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2596 0, 0, NULL, 0, BAS_TIMEOUT);
2598 ucs->basstate = 0;
2601 /* deregister this driver with the USB subsystem */
2602 usb_deregister(&gigaset_usb_driver);
2603 /* this will call the disconnect-callback */
2604 /* from now on, no disconnect/probe callback should be running */
2606 gigaset_freedriver(driver);
2607 driver = NULL;
2611 module_init(bas_gigaset_init);
2612 module_exit(bas_gigaset_exit);
2614 MODULE_AUTHOR(DRIVER_AUTHOR);
2615 MODULE_DESCRIPTION(DRIVER_DESC);
2616 MODULE_LICENSE("GPL");