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