gigaset: reduce syslog clutter
[deliverable/linux.git] / drivers / isdn / gigaset / bas-gigaset.c
CommitLineData
cf7776dc
HL
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>,
70440cf2 6 * Stefan Eilers.
cf7776dc 7 *
cf7776dc
HL
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 * =====================================================================
cf7776dc
HL
14 */
15
16#include "gigaset.h"
17
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>
25
26/* Version Information */
70440cf2 27#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
cf7776dc
HL
28#define DRIVER_DESC "USB Driver for Gigaset 307x"
29
30
31/* Module parameters */
32
33static int startmode = SM_ISDN;
34static int cidmode = 1;
35
36module_param(startmode, int, S_IRUGO);
37module_param(cidmode, int, S_IRUGO);
38MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
39MODULE_PARM_DESC(cidmode, "Call-ID mode");
40
41#define GIGASET_MINORS 1
42#define GIGASET_MINOR 16
43#define GIGASET_MODULENAME "bas_gigaset"
cf7776dc
HL
44#define GIGASET_DEVNAME "ttyGB"
45
73a88814
TS
46/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47#define IF_WRITEBUF 264
cf7776dc 48
170ebf85
TS
49/* interrupt pipe message size according to ibid. ch. 2.2 */
50#define IP_MSGSIZE 3
51
cf7776dc
HL
52/* Values for the Gigaset 307x */
53#define USB_GIGA_VENDOR_ID 0x0681
73a88814
TS
54#define USB_3070_PRODUCT_ID 0x0001
55#define USB_3075_PRODUCT_ID 0x0002
cf7776dc
HL
56#define USB_SX303_PRODUCT_ID 0x0021
57#define USB_SX353_PRODUCT_ID 0x0022
58
59/* table of devices that work with this driver */
7891adf1 60static const struct usb_device_id gigaset_table[] = {
73a88814
TS
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
cf7776dc
HL
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 */
66};
67
68MODULE_DEVICE_TABLE(usb, gigaset_table);
69
06163f86 70/*======================= local function prototypes ==========================*/
cf7776dc 71
06163f86 72/* function called if a new device belonging to this driver is connected */
cf7776dc
HL
73static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
75
76/* Function will be called if the device is unplugged */
77static void gigaset_disconnect(struct usb_interface *interface);
78
024fd299
TS
79/* functions called before/after suspend */
80static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
81static int gigaset_resume(struct usb_interface *intf);
82
83/* functions called before/after device reset */
84static int gigaset_pre_reset(struct usb_interface *intf);
85static int gigaset_post_reset(struct usb_interface *intf);
86
06163f86 87static int atread_submit(struct cardstate *, int);
73a88814 88static void stopurbs(struct bas_bc_state *);
06163f86 89static int req_submit(struct bc_state *, int, int, int);
73a88814
TS
90static int atwrite_submit(struct cardstate *, unsigned char *, int);
91static int start_cbsend(struct cardstate *);
cf7776dc 92
06163f86 93/*============================================================================*/
cf7776dc
HL
94
95struct bas_cardstate {
784d5858
TS
96 struct usb_device *udev; /* USB device pointer */
97 struct usb_interface *interface; /* interface for this device */
cf7776dc
HL
98 unsigned char minor; /* starting minor number */
99
784d5858 100 struct urb *urb_ctrl; /* control pipe default URB */
cf7776dc
HL
101 struct usb_ctrlrequest dr_ctrl;
102 struct timer_list timer_ctrl; /* control request timeout */
06163f86 103 int retry_ctrl;
cf7776dc
HL
104
105 struct timer_list timer_atrdy; /* AT command ready timeout */
784d5858 106 struct urb *urb_cmd_out; /* for sending AT commands */
cf7776dc
HL
107 struct usb_ctrlrequest dr_cmd_out;
108 int retry_cmd_out;
109
784d5858 110 struct urb *urb_cmd_in; /* for receiving AT replies */
cf7776dc
HL
111 struct usb_ctrlrequest dr_cmd_in;
112 struct timer_list timer_cmd_in; /* receive request timeout */
784d5858 113 unsigned char *rcvbuf; /* AT reply receive buffer */
cf7776dc 114
784d5858 115 struct urb *urb_int_in; /* URB for interrupt pipe */
170ebf85 116 unsigned char *int_in_buf;
cf7776dc
HL
117
118 spinlock_t lock; /* locks all following */
9d4bee2b 119 int basstate; /* bitmap (BS_*) */
cf7776dc 120 int pending; /* uncompleted base request */
024fd299 121 wait_queue_head_t waitqueue;
cf7776dc
HL
122 int rcvbuf_size; /* size of AT receive buffer */
123 /* 0: no receive in progress */
124 int retry_cmd_in; /* receive req retry count */
125};
126
127/* status of direct USB connection to 307x base (bits in basstate) */
73a88814
TS
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 */
024fd299 136#define BS_SUSPEND 0x100 /* USB port suspended */
b5f581d5 137#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
cf7776dc
HL
138
139
7891adf1 140static struct gigaset_driver *driver;
cf7776dc
HL
141
142/* usb specific object needed to register this driver with the usb subsystem */
143static struct usb_driver gigaset_usb_driver = {
144 .name = GIGASET_MODULENAME,
145 .probe = gigaset_probe,
146 .disconnect = gigaset_disconnect,
147 .id_table = gigaset_table,
024fd299
TS
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,
cf7776dc
HL
153};
154
73a88814
TS
155/* get message text for usb_submit_urb return code
156 */
157static char *get_usb_rcmsg(int rc)
158{
159 static char unkmsg[28];
160
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;
193 }
194}
195
cf7776dc
HL
196/* get message text for USB status code
197 */
198static char *get_usb_statmsg(int status)
199{
200 static char unkmsg[28];
201
202 switch (status) {
203 case 0:
204 return "success";
205 case -ENOENT:
73a88814 206 return "unlinked (sync)";
cf7776dc
HL
207 case -EINPROGRESS:
208 return "pending";
209 case -EPROTO:
73a88814 210 return "bit stuffing error, timeout, or unknown USB error";
cf7776dc 211 case -EILSEQ:
73a88814 212 return "CRC mismatch, timeout, or unknown USB error";
38e2bfc9 213 case -ETIME:
cf7776dc 214 return "timed out";
73a88814
TS
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";
cf7776dc
HL
223 case -EREMOTEIO:
224 return "short packet detected";
73a88814
TS
225 case -ENODEV:
226 return "device removed";
cf7776dc
HL
227 case -EXDEV:
228 return "partial isochronous transfer";
229 case -EINVAL:
230 return "invalid argument";
73a88814
TS
231 case -ECONNRESET:
232 return "unlinked (async)";
cf7776dc 233 case -ESHUTDOWN:
73a88814 234 return "device shut down";
cf7776dc 235 default:
73a88814 236 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
cf7776dc
HL
237 return unkmsg;
238 }
239}
240
241/* usb_pipetype_str
242 * retrieve string representation of USB pipe type
243 */
244static inline char *usb_pipetype_str(int pipe)
245{
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 "?";
255}
256
257/* dump_urb
258 * write content of URB to syslog for debugging
259 */
260static inline void dump_urb(enum debuglevel level, const char *tag,
784d5858 261 struct urb *urb)
cf7776dc
HL
262{
263#ifdef CONFIG_GIGASET_DEBUG
264 int i;
784d5858 265 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
cf7776dc 266 if (urb) {
784d5858
TS
267 gig_dbg(level,
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
dbd98231 269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
784d5858
TS
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",
dbd98231 274 (unsigned long) urb->hcpriv,
784d5858
TS
275 urb->transfer_flags);
276 gig_dbg(level,
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
249b061a 278 "setup_packet=0x%08lx,",
784d5858
TS
279 (unsigned long) urb->transfer_buffer,
280 urb->transfer_buffer_length, urb->actual_length,
249b061a 281 (unsigned long) urb->setup_packet);
784d5858
TS
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);
cf7776dc 292 for (i = 0; i < urb->number_of_packets; i++) {
917f5085
TS
293 struct usb_iso_packet_descriptor *pifd
294 = &urb->iso_frame_desc[i];
784d5858
TS
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);
cf7776dc
HL
300 }
301 }
784d5858 302 gig_dbg(level, "}}");
cf7776dc
HL
303#endif
304}
305
306/* read/set modem control bits etc. (m10x only) */
307static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
784d5858 308 unsigned new_state)
cf7776dc
HL
309{
310 return -EINVAL;
311}
312
313static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
314{
315 return -EINVAL;
316}
317
318static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
319{
320 return -EINVAL;
321}
322
b5f581d5
TS
323/* set/clear bits in base connection state, return previous state
324 */
325static inline int update_basstate(struct bas_cardstate *ucs,
326 int set, int clear)
327{
328 unsigned long flags;
329 int state;
330
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;
336}
337
cf7776dc
HL
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.
06163f86 342 * cs->lock must not be held.
cf7776dc
HL
343 * argument:
344 * B channel control structure
345 */
346static inline void error_hangup(struct bc_state *bcs)
347{
348 struct cardstate *cs = bcs->cs;
349
1528b18f 350 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
cf7776dc
HL
351 gigaset_schedule_event(cs);
352}
353
354/* error_reset
355 * reset Gigaset device because of an unrecoverable error
06163f86 356 * This function may be called from any context, and takes care of
73a88814 357 * scheduling the necessary actions for execution outside of interrupt context.
06163f86 358 * cs->lock must not be held.
cf7776dc
HL
359 * argument:
360 * controller state structure
361 */
362static inline void error_reset(struct cardstate *cs)
363{
b5f581d5
TS
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);
cf7776dc
HL
367}
368
369/* check_pending
370 * check for completion of pending control request
371 * parameter:
784d5858 372 * ucs hardware specific controller state structure
cf7776dc
HL
373 */
374static void check_pending(struct bas_cardstate *ucs)
375{
376 unsigned long flags;
377
cf7776dc
HL
378 spin_lock_irqsave(&ucs->lock, flags);
379 switch (ucs->pending) {
380 case 0:
381 break;
382 case HD_OPEN_ATCHANNEL:
9d4bee2b 383 if (ucs->basstate & BS_ATOPEN)
cf7776dc
HL
384 ucs->pending = 0;
385 break;
386 case HD_OPEN_B1CHANNEL:
9d4bee2b 387 if (ucs->basstate & BS_B1OPEN)
cf7776dc
HL
388 ucs->pending = 0;
389 break;
390 case HD_OPEN_B2CHANNEL:
9d4bee2b 391 if (ucs->basstate & BS_B2OPEN)
cf7776dc
HL
392 ucs->pending = 0;
393 break;
394 case HD_CLOSE_ATCHANNEL:
9d4bee2b 395 if (!(ucs->basstate & BS_ATOPEN))
cf7776dc 396 ucs->pending = 0;
cf7776dc
HL
397 break;
398 case HD_CLOSE_B1CHANNEL:
9d4bee2b 399 if (!(ucs->basstate & BS_B1OPEN))
cf7776dc
HL
400 ucs->pending = 0;
401 break;
402 case HD_CLOSE_B2CHANNEL:
9d4bee2b 403 if (!(ucs->basstate & BS_B2OPEN))
cf7776dc
HL
404 ucs->pending = 0;
405 break;
406 case HD_DEVICE_INIT_ACK: /* no reply expected */
407 ucs->pending = 0;
408 break;
b5f581d5
TS
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
cf7776dc
HL
416 */
417 default:
784d5858
TS
418 dev_warn(&ucs->interface->dev,
419 "unknown pending request 0x%02x cleared\n",
420 ucs->pending);
cf7776dc
HL
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 */
435static void cmd_in_timeout(unsigned long data)
436{
437 struct cardstate *cs = (struct cardstate *) data;
d48c7784 438 struct bas_cardstate *ucs = cs->hw.bas;
06163f86 439 int rc;
cf7776dc 440
cf7776dc 441 if (!ucs->rcvbuf_size) {
784d5858 442 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
cf7776dc
HL
443 return;
444 }
cf7776dc 445
06163f86
TS
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);
458 }
459 kfree(ucs->rcvbuf);
460 ucs->rcvbuf = NULL;
461 ucs->rcvbuf_size = 0;
462 error_reset(cs);
cf7776dc
HL
463}
464
06163f86
TS
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
471 */
7d12e780 472static void read_ctrl_callback(struct urb *urb)
06163f86
TS
473{
474 struct inbuf_t *inbuf = urb->context;
475 struct cardstate *cs = inbuf->cs;
476 struct bas_cardstate *ucs = cs->hw.bas;
dbd98231 477 int status = urb->status;
06163f86
TS
478 int have_data = 0;
479 unsigned numbytes;
480 int rc;
481
482 update_basstate(ucs, 0, BS_ATRDPEND);
024fd299 483 wake_up(&ucs->waitqueue);
06163f86
TS
484
485 if (!ucs->rcvbuf_size) {
486 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
487 return;
488 }
489
490 del_timer(&ucs->timer_cmd_in);
491
dbd98231 492 switch (status) {
06163f86
TS
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;
501 }
502
503 /* copy received bytes to inbuf */
504 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
505
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);
516 }
517 break;
518
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",
dbd98231 526 __func__, get_usb_statmsg(status));
06163f86
TS
527 break;
528
529 default: /* severe trouble */
530 dev_warn(cs->dev, "control read: %s\n",
dbd98231 531 get_usb_statmsg(status));
06163f86
TS
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);
544 }
545 error_reset(cs);
546 }
547
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);
554 }
555}
556
cf7776dc 557/* atread_submit
73a88814 558 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
cf7776dc
HL
559 * parameters:
560 * cs controller state structure
561 * timeout timeout in 1/10 sec., 0: none
562 * return value:
563 * 0 on success
cf7776dc
HL
564 * -EBUSY if another request is pending
565 * any URB submission error code
566 */
567static int atread_submit(struct cardstate *cs, int timeout)
568{
d48c7784 569 struct bas_cardstate *ucs = cs->hw.bas;
024fd299 570 int basstate;
cf7776dc
HL
571 int ret;
572
784d5858
TS
573 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
574 ucs->rcvbuf_size);
cf7776dc 575
024fd299
TS
576 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
577 if (basstate & BS_ATRDPEND) {
784d5858
TS
578 dev_err(cs->dev,
579 "could not submit HD_READ_ATMESSAGE: URB busy\n");
cf7776dc
HL
580 return -EBUSY;
581 }
582
024fd299
TS
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;
590 }
591
cf7776dc
HL
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,
784d5858 598 usb_rcvctrlpipe(ucs->udev, 0),
7891adf1 599 (unsigned char *) &ucs->dr_cmd_in,
784d5858
TS
600 ucs->rcvbuf, ucs->rcvbuf_size,
601 read_ctrl_callback, cs->inbuf);
cf7776dc 602
7891adf1
TS
603 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
604 if (ret != 0) {
73a88814 605 update_basstate(ucs, 0, BS_ATRDPEND);
784d5858 606 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
06163f86 607 get_usb_rcmsg(ret));
cf7776dc
HL
608 return ret;
609 }
610
611 if (timeout > 0) {
784d5858 612 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
cf7776dc
HL
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);
617 }
618 return 0;
619}
620
cf7776dc
HL
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
627 */
7d12e780 628static void read_int_callback(struct urb *urb)
cf7776dc 629{
d48c7784
TS
630 struct cardstate *cs = urb->context;
631 struct bas_cardstate *ucs = cs->hw.bas;
cf7776dc 632 struct bc_state *bcs;
dbd98231 633 int status = urb->status;
cf7776dc 634 unsigned long flags;
73a88814 635 int rc;
cf7776dc
HL
636 unsigned l;
637 int channel;
638
dbd98231 639 switch (status) {
cf7776dc
HL
640 case 0: /* success */
641 break;
73a88814
TS
642 case -ENOENT: /* cancelled */
643 case -ECONNRESET: /* cancelled (async) */
cf7776dc
HL
644 case -EINPROGRESS: /* pending */
645 /* ignore silently */
784d5858 646 gig_dbg(DEBUG_USBREQ, "%s: %s",
dbd98231 647 __func__, get_usb_statmsg(status));
cf7776dc 648 return;
73a88814
TS
649 case -ENODEV: /* device removed */
650 case -ESHUTDOWN: /* device shut down */
73a88814
TS
651 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
652 return;
cf7776dc 653 default: /* severe trouble */
784d5858 654 dev_warn(cs->dev, "interrupt read: %s\n",
dbd98231 655 get_usb_statmsg(status));
cf7776dc
HL
656 goto resubmit;
657 }
658
73a88814 659 /* drop incomplete packets even if the missing bytes wouldn't matter */
170ebf85 660 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
73a88814
TS
661 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
662 urb->actual_length);
663 goto resubmit;
664 }
665
cf7776dc
HL
666 l = (unsigned) ucs->int_in_buf[1] +
667 (((unsigned) ucs->int_in_buf[2]) << 8);
668
784d5858
TS
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]);
cf7776dc
HL
672
673 channel = 0;
674
675 switch (ucs->int_in_buf[0]) {
676 case HD_DEVICE_INIT_OK:
677 update_basstate(ucs, BS_INIT, 0);
678 break;
679
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;
685
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;
693
694 case HD_OPEN_ATCHANNEL_ACK:
695 update_basstate(ucs, BS_ATOPEN, 0);
696 start_cbsend(cs);
697 break;
698
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;
707
708 case HD_CLOSE_ATCHANNEL_ACK:
709 update_basstate(ucs, 0, BS_ATOPEN);
710 break;
711
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,
784d5858
TS
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));
cf7776dc
HL
722 break;
723
724 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
725 if (!l) {
784d5858
TS
726 dev_warn(cs->dev,
727 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
cf7776dc
HL
728 break;
729 }
730 spin_lock_irqsave(&cs->lock, flags);
731 if (ucs->rcvbuf_size) {
73a88814 732 /* throw away previous buffer - we have no queue */
784d5858 733 dev_err(cs->dev,
73a88814
TS
734 "receive AT data overrun, %d bytes lost\n",
735 ucs->rcvbuf_size);
736 kfree(ucs->rcvbuf);
737 ucs->rcvbuf_size = 0;
cf7776dc 738 }
7891adf1
TS
739 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
740 if (ucs->rcvbuf == NULL) {
cf7776dc 741 spin_unlock_irqrestore(&cs->lock, flags);
73a88814
TS
742 dev_err(cs->dev, "out of memory receiving AT data\n");
743 error_reset(cs);
cf7776dc
HL
744 break;
745 }
746 ucs->rcvbuf_size = l;
747 ucs->retry_cmd_in = 0;
7891adf1
TS
748 rc = atread_submit(cs, BAS_TIMEOUT);
749 if (rc < 0) {
cf7776dc
HL
750 kfree(ucs->rcvbuf);
751 ucs->rcvbuf = NULL;
752 ucs->rcvbuf_size = 0;
06163f86 753 if (rc != -ENODEV) {
06163f86 754 spin_unlock_irqrestore(&cs->lock, flags);
73a88814 755 error_reset(cs);
06163f86
TS
756 break;
757 }
cf7776dc
HL
758 }
759 spin_unlock_irqrestore(&cs->lock, flags);
760 break;
761
762 case HD_RESET_INTERRUPT_PIPE_ACK:
b5f581d5
TS
763 update_basstate(ucs, 0, BS_RESETTING);
764 dev_notice(cs->dev, "interrupt pipe reset\n");
cf7776dc
HL
765 break;
766
767 case HD_SUSPEND_END:
784d5858 768 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
cf7776dc
HL
769 break;
770
771 default:
784d5858
TS
772 dev_warn(cs->dev,
773 "unknown Gigaset signal 0x%02x (%u) ignored\n",
774 (int) ucs->int_in_buf[0], l);
cf7776dc
HL
775 }
776
777 check_pending(ucs);
024fd299 778 wake_up(&ucs->waitqueue);
cf7776dc
HL
779
780resubmit:
54e6ecb2 781 rc = usb_submit_urb(urb, GFP_ATOMIC);
73a88814 782 if (unlikely(rc != 0 && rc != -ENODEV)) {
784d5858 783 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
73a88814 784 get_usb_rcmsg(rc));
cf7776dc
HL
785 error_reset(cs);
786 }
787}
788
cf7776dc
HL
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
795 */
7d12e780 796static void read_iso_callback(struct urb *urb)
cf7776dc
HL
797{
798 struct bc_state *bcs;
799 struct bas_bc_state *ubc;
dbd98231 800 int status = urb->status;
cf7776dc
HL
801 unsigned long flags;
802 int i, rc;
803
cf7776dc 804 /* status codes not worth bothering the tasklet with */
dbd98231
TS
805 if (unlikely(status == -ENOENT ||
806 status == -ECONNRESET ||
807 status == -EINPROGRESS ||
808 status == -ENODEV ||
809 status == -ESHUTDOWN)) {
784d5858 810 gig_dbg(DEBUG_ISO, "%s: %s",
dbd98231 811 __func__, get_usb_statmsg(status));
cf7776dc
HL
812 return;
813 }
814
d48c7784 815 bcs = urb->context;
cf7776dc 816 ubc = bcs->hw.bas;
cf7776dc
HL
817
818 spin_lock_irqsave(&ubc->isoinlock, flags);
819 if (likely(ubc->isoindone == NULL)) {
820 /* pass URB to tasklet */
821 ubc->isoindone = urb;
dbd98231 822 ubc->isoinstatus = status;
368fd81d 823 tasklet_hi_schedule(&ubc->rcvd_tasklet);
cf7776dc
HL
824 } else {
825 /* tasklet still busy, drop data and resubmit URB */
dbd98231 826 ubc->loststatus = status;
cf7776dc
HL
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 &&
73a88814
TS
830 urb->iso_frame_desc[i].status !=
831 -EINPROGRESS))
cf7776dc 832 ubc->loststatus = urb->iso_frame_desc[i].status;
cf7776dc
HL
833 urb->iso_frame_desc[i].status = 0;
834 urb->iso_frame_desc[i].actual_length = 0;
835 }
9d4bee2b 836 if (likely(ubc->running)) {
784d5858
TS
837 /* urb->dev is clobbered by USB subsystem */
838 urb->dev = bcs->cs->hw.bas->udev;
cf7776dc
HL
839 urb->transfer_flags = URB_ISO_ASAP;
840 urb->number_of_packets = BAS_NUMFRAMES;
784d5858
TS
841 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
842 __func__);
54e6ecb2 843 rc = usb_submit_urb(urb, GFP_ATOMIC);
73a88814 844 if (unlikely(rc != 0 && rc != -ENODEV)) {
784d5858
TS
845 dev_err(bcs->cs->dev,
846 "could not resubmit isochronous read "
73a88814 847 "URB: %s\n", get_usb_rcmsg(rc));
cf7776dc
HL
848 dump_urb(DEBUG_ISO, "isoc read", urb);
849 error_hangup(bcs);
850 }
851 }
852 }
853 spin_unlock_irqrestore(&ubc->isoinlock, flags);
854}
855
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
862 */
7d12e780 863static void write_iso_callback(struct urb *urb)
cf7776dc
HL
864{
865 struct isow_urbctx_t *ucx;
866 struct bas_bc_state *ubc;
dbd98231 867 int status = urb->status;
cf7776dc
HL
868 unsigned long flags;
869
cf7776dc 870 /* status codes not worth bothering the tasklet with */
dbd98231
TS
871 if (unlikely(status == -ENOENT ||
872 status == -ECONNRESET ||
873 status == -EINPROGRESS ||
874 status == -ENODEV ||
875 status == -ESHUTDOWN)) {
784d5858 876 gig_dbg(DEBUG_ISO, "%s: %s",
dbd98231 877 __func__, get_usb_statmsg(status));
cf7776dc
HL
878 return;
879 }
880
881 /* pass URB context to tasklet */
d48c7784 882 ucx = urb->context;
cf7776dc 883 ubc = ucx->bcs->hw.bas;
dbd98231 884 ucx->status = status;
cf7776dc
HL
885
886 spin_lock_irqsave(&ubc->isooutlock, flags);
887 ubc->isooutovfl = ubc->isooutdone;
888 ubc->isooutdone = ucx;
889 spin_unlock_irqrestore(&ubc->isooutlock, flags);
368fd81d 890 tasklet_hi_schedule(&ubc->sent_tasklet);
cf7776dc
HL
891}
892
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)
900 */
901static int starturbs(struct bc_state *bcs)
902{
d48c7784 903 struct bas_bc_state *ubc = bcs->hw.bas;
cf7776dc 904 struct urb *urb;
cf7776dc
HL
905 int j, k;
906 int rc;
907
cf7776dc 908 /* initialize L2 reception */
088ec0cc 909 if (bcs->proto2 == L2_HDLC)
cf7776dc
HL
910 bcs->inputstate |= INS_flag_hunt;
911
912 /* submit all isochronous input URBs */
9d4bee2b 913 ubc->running = 1;
cf7776dc
HL
914 for (k = 0; k < BAS_INURBS; k++) {
915 urb = ubc->isoinurbs[k];
916 if (!urb) {
cf7776dc
HL
917 rc = -EFAULT;
918 goto error;
919 }
920
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;
935 }
936
937 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
7891adf1
TS
938 rc = usb_submit_urb(urb, GFP_ATOMIC);
939 if (rc != 0)
cf7776dc 940 goto error;
cf7776dc
HL
941 }
942
943 /* initialize L2 transmission */
944 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
945
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) {
cf7776dc
HL
950 rc = -EFAULT;
951 goto error;
952 }
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;
967 }
968 ubc->isoouturbs[k].limit = -1;
969 }
970
c652cbd8
TS
971 /* keep one URB free, submit the others */
972 for (k = 0; k < BAS_OUTURBS-1; ++k) {
cf7776dc 973 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
54e6ecb2 974 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
73a88814 975 if (rc != 0)
cf7776dc 976 goto error;
cf7776dc
HL
977 }
978 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
c652cbd8 979 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
cf7776dc
HL
980 ubc->isooutdone = ubc->isooutovfl = NULL;
981 return 0;
982 error:
983 stopurbs(ubc);
984 return rc;
985}
986
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
992 */
993static void stopurbs(struct bas_bc_state *ubc)
994{
995 int k, rc;
996
9d4bee2b 997 ubc->running = 0;
cf7776dc
HL
998
999 for (k = 0; k < BAS_INURBS; ++k) {
1000 rc = usb_unlink_urb(ubc->isoinurbs[k]);
784d5858 1001 gig_dbg(DEBUG_ISO,
73a88814
TS
1002 "%s: isoc input URB %d unlinked, result = %s",
1003 __func__, k, get_usb_rcmsg(rc));
cf7776dc
HL
1004 }
1005
1006 for (k = 0; k < BAS_OUTURBS; ++k) {
1007 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
784d5858 1008 gig_dbg(DEBUG_ISO,
73a88814
TS
1009 "%s: isoc output URB %d unlinked, result = %s",
1010 __func__, k, get_usb_rcmsg(rc));
cf7776dc
HL
1011 }
1012}
1013
1014/* Isochronous Write - Bottom Half */
1015/* =============================== */
1016
1017/* submit_iso_write_urb
1018 * fill and submit the next isochronous write URB
1019 * parameters:
73a88814 1020 * ucx context structure containing URB
cf7776dc
HL
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
1025 */
1026static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1027{
d48c7784
TS
1028 struct urb *urb = ucx->urb;
1029 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
cf7776dc
HL
1030 struct usb_iso_packet_descriptor *ifd;
1031 int corrbytes, nframe, rc;
1032
784d5858
TS
1033 /* urb->dev is clobbered by USB subsystem */
1034 urb->dev = ucx->bcs->cs->hw.bas->udev;
cf7776dc
HL
1035 urb->transfer_flags = URB_ISO_ASAP;
1036 urb->transfer_buffer = ubc->isooutbuf->data;
1037 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1038
1039 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1040 ifd = &urb->iso_frame_desc[nframe];
1041
1042 /* compute frame length according to flow control */
1043 ifd->length = BAS_NORMFRAME;
7891adf1
TS
1044 corrbytes = atomic_read(&ubc->corrbytes);
1045 if (corrbytes != 0) {
784d5858
TS
1046 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1047 __func__, corrbytes);
cf7776dc
HL
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);
1054 }
cf7776dc
HL
1055
1056 /* retrieve block of data to send */
5f09c4c7
TS
1057 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1058 if (rc < 0) {
1059 if (rc == -EBUSY) {
784d5858
TS
1060 gig_dbg(DEBUG_ISO,
1061 "%s: buffer busy at frame %d",
1062 __func__, nframe);
1063 /* tasklet will be restarted from
088ec0cc 1064 gigaset_isoc_send_skb() */
cf7776dc 1065 } else {
784d5858
TS
1066 dev_err(ucx->bcs->cs->dev,
1067 "%s: buffer error %d at frame %d\n",
5f09c4c7
TS
1068 __func__, rc, nframe);
1069 return rc;
cf7776dc
HL
1070 }
1071 break;
1072 }
5f09c4c7 1073 ifd->offset = rc;
9d4bee2b 1074 ucx->limit = ubc->isooutbuf->nextread;
cf7776dc
HL
1075 ifd->status = 0;
1076 ifd->actual_length = 0;
1077 }
73a88814
TS
1078 if (unlikely(nframe == 0))
1079 return 0; /* no data to send */
1080 urb->number_of_packets = nframe;
69049cc8 1081
54e6ecb2 1082 rc = usb_submit_urb(urb, GFP_ATOMIC);
73a88814
TS
1083 if (unlikely(rc)) {
1084 if (rc == -ENODEV)
1085 /* device removed - give up silently */
1086 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1087 else
784d5858
TS
1088 dev_err(ucx->bcs->cs->dev,
1089 "could not submit isochronous write URB: %s\n",
73a88814
TS
1090 get_usb_rcmsg(rc));
1091 return rc;
cf7776dc 1092 }
73a88814 1093 ++ubc->numsub;
cf7776dc
HL
1094 return nframe;
1095}
1096
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
1102 */
1103static void write_iso_tasklet(unsigned long data)
1104{
d48c7784
TS
1105 struct bc_state *bcs = (struct bc_state *) data;
1106 struct bas_bc_state *ubc = bcs->hw.bas;
1107 struct cardstate *cs = bcs->cs;
cf7776dc
HL
1108 struct isow_urbctx_t *done, *next, *ovfl;
1109 struct urb *urb;
dbd98231 1110 int status;
cf7776dc
HL
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;
73a88814 1117 int rc;
cf7776dc 1118
cf7776dc
HL
1119 /* loop while completed URBs arrive in time */
1120 for (;;) {
9d4bee2b 1121 if (unlikely(!(ubc->running))) {
784d5858 1122 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
cf7776dc
HL
1123 return;
1124 }
1125
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) {
784d5858 1134 dev_err(cs->dev, "isochronous write buffer underrun\n");
cf7776dc
HL
1135 error_hangup(bcs);
1136 break;
1137 }
1138 if (!done)
1139 break;
1140
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) {
73a88814
TS
1147 rc = submit_iso_write_urb(next);
1148 if (unlikely(rc <= 0 && rc != -ENODEV)) {
cf7776dc
HL
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;
1154 }
1155 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1156 if (next) {
1157 /* couldn't put it back */
784d5858
TS
1158 dev_err(cs->dev,
1159 "losing isochronous write URB\n");
cf7776dc
HL
1160 error_hangup(bcs);
1161 }
1162 }
1163 }
1164
1165 /* process completed URB */
1166 urb = done->urb;
dbd98231
TS
1167 status = done->status;
1168 switch (status) {
73a88814
TS
1169 case -EXDEV: /* partial completion */
1170 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1171 __func__);
1172 /* fall through - what's the difference anyway? */
cf7776dc 1173 case 0: /* normal completion */
73a88814
TS
1174 /* inspect individual frames
1175 * assumptions (for lack of documentation):
1176 * - actual_length bytes of first frame in error are
917f5085 1177 * successfully sent
cf7776dc
HL
1178 * - all following frames are not sent at all
1179 */
73a88814 1180 offset = done->limit; /* default (no error) */
cf7776dc
HL
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) {
784d5858
TS
1185 dev_warn(cs->dev,
1186 "isochronous write: frame %d: %s, "
1187 "only %d of %d bytes sent\n",
cf7776dc
HL
1188 i, get_usb_statmsg(ifd->status),
1189 ifd->actual_length, ifd->length);
1190 offset = (ifd->offset +
784d5858
TS
1191 ifd->actual_length)
1192 % BAS_OUTBUFSIZE;
cf7776dc
HL
1193 break;
1194 }
1195 }
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) {
784d5858
TS
1202 dev_warn(cs->dev,
1203 "isochronous write: frame %d: %s, "
1204 "%d of %d bytes sent\n",
cf7776dc
HL
1205 i, get_usb_statmsg(ifd->status),
1206 ifd->actual_length, ifd->length);
1207 offset = (ifd->offset +
784d5858
TS
1208 ifd->actual_length)
1209 % BAS_OUTBUFSIZE;
cf7776dc
HL
1210 break;
1211 }
1212 }
1213#endif
1214 break;
73a88814 1215 case -EPIPE: /* stall - probably underrun */
784d5858 1216 dev_err(cs->dev, "isochronous write stalled\n");
cf7776dc
HL
1217 error_hangup(bcs);
1218 break;
1219 default: /* severe trouble */
784d5858 1220 dev_warn(cs->dev, "isochronous write: %s\n",
dbd98231 1221 get_usb_statmsg(status));
cf7776dc
HL
1222 }
1223
1224 /* mark the write buffer area covered by this URB as free */
1225 if (done->limit >= 0)
9d4bee2b 1226 ubc->isooutbuf->read = done->limit;
cf7776dc
HL
1227
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 */
73a88814
TS
1235 rc = submit_iso_write_urb(next);
1236 if (unlikely(rc <= 0 && rc != -ENODEV)) {
cf7776dc
HL
1237 /* couldn't submit */
1238 error_hangup(bcs);
1239 }
1240 }
1241 }
1242
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);
784d5858
TS
1250 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1251 __func__, skb_queue_len(&bcs->squeue));
cf7776dc
HL
1252 break;
1253 }
1254 skb_pull(skb, len);
1255 gigaset_skb_sent(bcs, skb);
1256 dev_kfree_skb_any(skb);
1257 }
1258}
1259
1260/* Isochronous Read - Bottom Half */
1261/* ============================== */
1262
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
1268 */
1269static void read_iso_tasklet(unsigned long data)
1270{
d48c7784
TS
1271 struct bc_state *bcs = (struct bc_state *) data;
1272 struct bas_bc_state *ubc = bcs->hw.bas;
1273 struct cardstate *cs = bcs->cs;
cf7776dc 1274 struct urb *urb;
dbd98231 1275 int status;
cf7776dc
HL
1276 char *rcvbuf;
1277 unsigned long flags;
1278 int totleft, numbytes, offset, frame, rc;
1279
cf7776dc
HL
1280 /* loop while more completed URBs arrive in the meantime */
1281 for (;;) {
cf7776dc
HL
1282 /* retrieve URB */
1283 spin_lock_irqsave(&ubc->isoinlock, flags);
7891adf1
TS
1284 urb = ubc->isoindone;
1285 if (!urb) {
cf7776dc
HL
1286 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1287 return;
1288 }
dbd98231 1289 status = ubc->isoinstatus;
cf7776dc
HL
1290 ubc->isoindone = NULL;
1291 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
784d5858
TS
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);
cf7776dc
HL
1297 ubc->loststatus = -EINPROGRESS;
1298 }
1299 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1300
9d4bee2b 1301 if (unlikely(!(ubc->running))) {
784d5858
TS
1302 gig_dbg(DEBUG_ISO,
1303 "%s: channel not running, "
1304 "dropped URB with status: %s",
dbd98231 1305 __func__, get_usb_statmsg(status));
cf7776dc
HL
1306 return;
1307 }
1308
dbd98231 1309 switch (status) {
cf7776dc
HL
1310 case 0: /* normal completion */
1311 break;
917f5085
TS
1312 case -EXDEV: /* inspect individual frames
1313 (we do that anyway) */
784d5858
TS
1314 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1315 __func__);
cf7776dc
HL
1316 break;
1317 case -ENOENT:
1318 case -ECONNRESET:
73a88814
TS
1319 case -EINPROGRESS:
1320 gig_dbg(DEBUG_ISO, "%s: %s",
dbd98231 1321 __func__, get_usb_statmsg(status));
cf7776dc
HL
1322 continue; /* -> skip */
1323 case -EPIPE:
784d5858 1324 dev_err(cs->dev, "isochronous read stalled\n");
cf7776dc
HL
1325 error_hangup(bcs);
1326 continue; /* -> skip */
1327 default: /* severe trouble */
784d5858 1328 dev_warn(cs->dev, "isochronous read: %s\n",
dbd98231 1329 get_usb_statmsg(status));
cf7776dc
HL
1330 goto error;
1331 }
1332
1333 rcvbuf = urb->transfer_buffer;
1334 totleft = urb->actual_length;
1335 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
eb4459ff
TS
1336 numbytes = urb->iso_frame_desc[frame].actual_length;
1337 if (unlikely(urb->iso_frame_desc[frame].status))
784d5858 1338 dev_warn(cs->dev,
eb4459ff
TS
1339 "isochronous read: frame %d[%d]: %s\n",
1340 frame, numbytes,
784d5858
TS
1341 get_usb_statmsg(
1342 urb->iso_frame_desc[frame].status));
eb4459ff 1343 if (unlikely(numbytes > BAS_MAXFRAME))
784d5858
TS
1344 dev_warn(cs->dev,
1345 "isochronous read: frame %d: "
1346 "numbytes (%d) > BAS_MAXFRAME\n",
1347 frame, numbytes);
cf7776dc 1348 if (unlikely(numbytes > totleft)) {
784d5858
TS
1349 dev_warn(cs->dev,
1350 "isochronous read: frame %d: "
1351 "numbytes (%d) > totleft (%d)\n",
1352 frame, numbytes, totleft);
eb4459ff 1353 numbytes = totleft;
cf7776dc
HL
1354 }
1355 offset = urb->iso_frame_desc[frame].offset;
1356 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
784d5858
TS
1357 dev_warn(cs->dev,
1358 "isochronous read: frame %d: "
1359 "offset (%d) + numbytes (%d) "
1360 "> BAS_INBUFSIZE\n",
1361 frame, offset, numbytes);
eb4459ff 1362 numbytes = BAS_INBUFSIZE - offset;
cf7776dc
HL
1363 }
1364 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1365 totleft -= numbytes;
1366 }
1367 if (unlikely(totleft > 0))
784d5858
TS
1368 dev_warn(cs->dev,
1369 "isochronous read: %d data bytes missing\n",
1370 totleft);
cf7776dc 1371
7891adf1 1372error:
cf7776dc
HL
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;
1377 }
784d5858
TS
1378 /* urb->dev is clobbered by USB subsystem */
1379 urb->dev = bcs->cs->hw.bas->udev;
cf7776dc
HL
1380 urb->transfer_flags = URB_ISO_ASAP;
1381 urb->number_of_packets = BAS_NUMFRAMES;
54e6ecb2 1382 rc = usb_submit_urb(urb, GFP_ATOMIC);
73a88814 1383 if (unlikely(rc != 0 && rc != -ENODEV)) {
784d5858
TS
1384 dev_err(cs->dev,
1385 "could not resubmit isochronous read URB: %s\n",
73a88814 1386 get_usb_rcmsg(rc));
cf7776dc
HL
1387 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1388 error_hangup(bcs);
1389 }
1390 }
1391}
1392
1393/* Channel Operations */
1394/* ================== */
1395
1396/* req_timeout
1397 * timeout routine for control output request
1398 * argument:
1399 * B channel control structure
1400 */
1401static void req_timeout(unsigned long data)
1402{
1403 struct bc_state *bcs = (struct bc_state *) data;
d48c7784 1404 struct bas_cardstate *ucs = bcs->cs->hw.bas;
cf7776dc
HL
1405 int pending;
1406 unsigned long flags;
1407
cf7776dc
HL
1408 check_pending(ucs);
1409
1410 spin_lock_irqsave(&ucs->lock, flags);
1411 pending = ucs->pending;
1412 ucs->pending = 0;
1413 spin_unlock_irqrestore(&ucs->lock, flags);
1414
1415 switch (pending) {
1416 case 0: /* no pending request */
784d5858 1417 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
cf7776dc
HL
1418 break;
1419
1420 case HD_OPEN_ATCHANNEL:
784d5858 1421 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
cf7776dc
HL
1422 error_reset(bcs->cs);
1423 break;
1424
1425 case HD_OPEN_B2CHANNEL:
1426 case HD_OPEN_B1CHANNEL:
784d5858
TS
1427 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1428 bcs->channel + 1);
cf7776dc
HL
1429 error_hangup(bcs);
1430 break;
1431
1432 case HD_CLOSE_ATCHANNEL:
784d5858 1433 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
b5f581d5 1434 error_reset(bcs->cs);
cf7776dc
HL
1435 break;
1436
1437 case HD_CLOSE_B2CHANNEL:
1438 case HD_CLOSE_B1CHANNEL:
784d5858
TS
1439 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1440 bcs->channel + 1);
06163f86 1441 error_reset(bcs->cs);
cf7776dc
HL
1442 break;
1443
b5f581d5
TS
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;
1450
cf7776dc 1451 default:
784d5858
TS
1452 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1453 pending);
cf7776dc 1454 }
024fd299
TS
1455
1456 wake_up(&ucs->waitqueue);
cf7776dc
HL
1457}
1458
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
1465 */
7d12e780 1466static void write_ctrl_callback(struct urb *urb)
cf7776dc 1467{
d48c7784 1468 struct bas_cardstate *ucs = urb->context;
dbd98231 1469 int status = urb->status;
06163f86 1470 int rc;
cf7776dc
HL
1471 unsigned long flags;
1472
06163f86 1473 /* check status */
dbd98231 1474 switch (status) {
06163f86
TS
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;
1482 }
1483 spin_unlock_irqrestore(&ucs->lock, flags);
1484 return;
1485
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",
dbd98231 1493 __func__, get_usb_statmsg(status));
cf7776dc 1494 break;
06163f86
TS
1495
1496 default: /* any failure */
024fd299
TS
1497 /* don't retry if suspend requested */
1498 if (++ucs->retry_ctrl > BAS_RETRY ||
9d4bee2b 1499 (ucs->basstate & BS_SUSPEND)) {
06163f86
TS
1500 dev_err(&ucs->interface->dev,
1501 "control request 0x%02x failed: %s\n",
1502 ucs->dr_ctrl.bRequest,
dbd98231 1503 get_usb_statmsg(status));
06163f86
TS
1504 break; /* give up */
1505 }
1506 dev_notice(&ucs->interface->dev,
1507 "control request 0x%02x: %s, retry %d\n",
dbd98231 1508 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
06163f86
TS
1509 ucs->retry_ctrl);
1510 /* urb->dev is clobbered by USB subsystem */
1511 urb->dev = ucs->udev;
54e6ecb2 1512 rc = usb_submit_urb(urb, GFP_ATOMIC);
06163f86
TS
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;
1518 }
1519 /* resubmitted */
1520 return;
cf7776dc 1521 }
06163f86
TS
1522
1523 /* failed, clear pending request */
1524 spin_lock_irqsave(&ucs->lock, flags);
1525 del_timer(&ucs->timer_ctrl);
1526 ucs->pending = 0;
cf7776dc 1527 spin_unlock_irqrestore(&ucs->lock, flags);
024fd299 1528 wake_up(&ucs->waitqueue);
cf7776dc
HL
1529}
1530
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
cf7776dc
HL
1541 * -EBUSY if another request is pending
1542 * any URB submission error code
1543 */
1544static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1545{
d48c7784 1546 struct bas_cardstate *ucs = bcs->cs->hw.bas;
cf7776dc
HL
1547 int ret;
1548 unsigned long flags;
1549
784d5858 1550 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
cf7776dc
HL
1551
1552 spin_lock_irqsave(&ucs->lock, flags);
1553 if (ucs->pending) {
1554 spin_unlock_irqrestore(&ucs->lock, flags);
784d5858
TS
1555 dev_err(bcs->cs->dev,
1556 "submission of request 0x%02x failed: "
1557 "request 0x%02x still pending\n",
1558 req, ucs->pending);
cf7776dc
HL
1559 return -EBUSY;
1560 }
cf7776dc
HL
1561
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,
784d5858 1568 usb_sndctrlpipe(ucs->udev, 0),
7891adf1 1569 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
784d5858 1570 write_ctrl_callback, ucs);
06163f86 1571 ucs->retry_ctrl = 0;
54e6ecb2 1572 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
06163f86 1573 if (unlikely(ret)) {
784d5858 1574 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
06163f86 1575 req, get_usb_rcmsg(ret));
cf7776dc
HL
1576 spin_unlock_irqrestore(&ucs->lock, flags);
1577 return ret;
1578 }
1579 ucs->pending = req;
1580
1581 if (timeout > 0) {
784d5858 1582 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
cf7776dc
HL
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);
1587 }
1588
1589 spin_unlock_irqrestore(&ucs->lock, flags);
1590 return 0;
1591}
1592
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
1600 */
1601static int gigaset_init_bchannel(struct bc_state *bcs)
1602{
c652cbd8 1603 struct cardstate *cs = bcs->cs;
cf7776dc 1604 int req, ret;
73a88814
TS
1605 unsigned long flags;
1606
c652cbd8
TS
1607 spin_lock_irqsave(&cs->lock, flags);
1608 if (unlikely(!cs->connected)) {
73a88814 1609 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
c652cbd8 1610 spin_unlock_irqrestore(&cs->lock, flags);
73a88814
TS
1611 return -ENODEV;
1612 }
cf7776dc 1613
9d4bee2b 1614 if (cs->hw.bas->basstate & BS_SUSPEND) {
024fd299
TS
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;
1620 }
1621
7891adf1
TS
1622 ret = starturbs(bcs);
1623 if (ret < 0) {
c652cbd8 1624 dev_err(cs->dev,
73a88814
TS
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);
c652cbd8 1630 spin_unlock_irqrestore(&cs->lock, flags);
cf7776dc
HL
1631 return ret;
1632 }
1633
1634 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
7891adf1
TS
1635 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1636 if (ret < 0) {
c652cbd8 1637 dev_err(cs->dev, "could not open channel B%d\n",
73a88814 1638 bcs->channel + 1);
cf7776dc 1639 stopurbs(bcs->hw.bas);
73a88814
TS
1640 if (ret != -ENODEV)
1641 error_hangup(bcs);
cf7776dc 1642 }
73a88814 1643
c652cbd8 1644 spin_unlock_irqrestore(&cs->lock, flags);
cf7776dc
HL
1645 return ret;
1646}
1647
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
1657 */
1658static int gigaset_close_bchannel(struct bc_state *bcs)
1659{
c652cbd8 1660 struct cardstate *cs = bcs->cs;
cf7776dc 1661 int req, ret;
73a88814
TS
1662 unsigned long flags;
1663
c652cbd8
TS
1664 spin_lock_irqsave(&cs->lock, flags);
1665 if (unlikely(!cs->connected)) {
1666 spin_unlock_irqrestore(&cs->lock, flags);
73a88814
TS
1667 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1668 return -ENODEV;
1669 }
cf7776dc 1670
9d4bee2b 1671 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
cf7776dc 1672 /* channel not running: just signal common.c */
c652cbd8 1673 spin_unlock_irqrestore(&cs->lock, flags);
cf7776dc
HL
1674 gigaset_bchannel_down(bcs);
1675 return 0;
1676 }
1677
73a88814 1678 /* channel running: tell device to close it */
cf7776dc 1679 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
7891adf1
TS
1680 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1681 if (ret < 0)
c652cbd8 1682 dev_err(cs->dev, "closing channel B%d failed\n",
73a88814
TS
1683 bcs->channel + 1);
1684
c652cbd8 1685 spin_unlock_irqrestore(&cs->lock, flags);
cf7776dc
HL
1686 return ret;
1687}
1688
1689/* Device Operations */
1690/* ================= */
1691
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
1697 */
1698static void complete_cb(struct cardstate *cs)
1699{
d48c7784 1700 struct cmdbuf_t *cb = cs->cmdbuf;
cf7776dc
HL
1701
1702 /* unqueue completed buffer */
1703 cs->cmdbytes -= cs->curlen;
1528b18f 1704 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
784d5858 1705 cs->curlen, cs->cmdbytes);
7891adf1
TS
1706 if (cb->next != NULL) {
1707 cs->cmdbuf = cb->next;
cf7776dc
HL
1708 cs->cmdbuf->prev = NULL;
1709 cs->curlen = cs->cmdbuf->len;
1710 } else {
7891adf1 1711 cs->cmdbuf = NULL;
cf7776dc
HL
1712 cs->lastcmdbuf = NULL;
1713 cs->curlen = 0;
1714 }
1715
1716 if (cb->wake_tasklet)
1717 tasklet_schedule(cb->wake_tasklet);
1718
1719 kfree(cb);
1720}
1721
cf7776dc
HL
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
1728 */
7d12e780 1729static void write_command_callback(struct urb *urb)
cf7776dc 1730{
d48c7784
TS
1731 struct cardstate *cs = urb->context;
1732 struct bas_cardstate *ucs = cs->hw.bas;
dbd98231 1733 int status = urb->status;
cf7776dc 1734 unsigned long flags;
cf7776dc 1735
73a88814 1736 update_basstate(ucs, 0, BS_ATWRPEND);
024fd299 1737 wake_up(&ucs->waitqueue);
73a88814 1738
cf7776dc 1739 /* check status */
dbd98231 1740 switch (status) {
cf7776dc
HL
1741 case 0: /* normal completion */
1742 break;
73a88814
TS
1743 case -ENOENT: /* cancelled */
1744 case -ECONNRESET: /* cancelled (async) */
cf7776dc 1745 case -EINPROGRESS: /* pending */
73a88814
TS
1746 case -ENODEV: /* device removed */
1747 case -ESHUTDOWN: /* device shut down */
cf7776dc 1748 /* ignore silently */
784d5858 1749 gig_dbg(DEBUG_USBREQ, "%s: %s",
dbd98231 1750 __func__, get_usb_statmsg(status));
cf7776dc
HL
1751 return;
1752 default: /* any failure */
1753 if (++ucs->retry_cmd_out > BAS_RETRY) {
784d5858
TS
1754 dev_warn(cs->dev,
1755 "command write: %s, "
1756 "giving up after %d retries\n",
dbd98231 1757 get_usb_statmsg(status),
784d5858 1758 ucs->retry_cmd_out);
cf7776dc
HL
1759 break;
1760 }
9d4bee2b 1761 if (ucs->basstate & BS_SUSPEND) {
024fd299
TS
1762 dev_warn(cs->dev,
1763 "command write: %s, "
1764 "won't retry - suspend requested\n",
1765 get_usb_statmsg(status));
1766 break;
1767 }
cf7776dc 1768 if (cs->cmdbuf == NULL) {
784d5858
TS
1769 dev_warn(cs->dev,
1770 "command write: %s, "
1771 "cannot retry - cmdbuf gone\n",
dbd98231 1772 get_usb_statmsg(status));
cf7776dc
HL
1773 break;
1774 }
784d5858 1775 dev_notice(cs->dev, "command write: %s, retry %d\n",
dbd98231 1776 get_usb_statmsg(status), ucs->retry_cmd_out);
cf7776dc
HL
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);
1782 }
1783
1784 spin_lock_irqsave(&cs->cmdlock, flags);
1785 if (cs->cmdbuf != NULL)
1786 complete_cb(cs);
1787 spin_unlock_irqrestore(&cs->cmdlock, flags);
1788}
1789
1790/* atrdy_timeout
1791 * timeout routine for AT command transmission
1792 * argument:
1793 * controller state structure
1794 */
1795static void atrdy_timeout(unsigned long data)
1796{
1797 struct cardstate *cs = (struct cardstate *) data;
d48c7784 1798 struct bas_cardstate *ucs = cs->hw.bas;
cf7776dc 1799
784d5858 1800 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
cf7776dc
HL
1801
1802 /* fake the missing signal - what else can I do? */
1803 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1804 start_cbsend(cs);
1805}
1806
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
cf7776dc
HL
1815 * -EBUSY if another request is pending
1816 * any URB submission error code
1817 */
1818static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1819{
d48c7784 1820 struct bas_cardstate *ucs = cs->hw.bas;
73a88814 1821 int rc;
cf7776dc 1822
784d5858 1823 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
cf7776dc 1824
73a88814 1825 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
784d5858
TS
1826 dev_err(cs->dev,
1827 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
cf7776dc
HL
1828 return -EBUSY;
1829 }
1830
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),
7891adf1 1838 (unsigned char *) &ucs->dr_cmd_out, buf, len,
cf7776dc 1839 write_command_callback, cs);
54e6ecb2 1840 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
73a88814
TS
1841 if (unlikely(rc)) {
1842 update_basstate(ucs, 0, BS_ATWRPEND);
784d5858 1843 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
73a88814
TS
1844 get_usb_rcmsg(rc));
1845 return rc;
cf7776dc
HL
1846 }
1847
73a88814
TS
1848 /* submitted successfully, start timeout if necessary */
1849 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
784d5858
TS
1850 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1851 ATRDY_TIMEOUT);
cf7776dc
HL
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);
cf7776dc
HL
1856 }
1857 return 0;
1858}
1859
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
1867 */
1868static int start_cbsend(struct cardstate *cs)
1869{
1870 struct cmdbuf_t *cb;
d48c7784 1871 struct bas_cardstate *ucs = cs->hw.bas;
cf7776dc
HL
1872 unsigned long flags;
1873 int rc;
1874 int retval = 0;
1875
024fd299 1876 /* check if suspend requested */
9d4bee2b 1877 if (ucs->basstate & BS_SUSPEND) {
1528b18f 1878 gig_dbg(DEBUG_OUTPUT, "suspending");
024fd299
TS
1879 return -EHOSTUNREACH;
1880 }
1881
cf7776dc 1882 /* check if AT channel is open */
9d4bee2b 1883 if (!(ucs->basstate & BS_ATOPEN)) {
1528b18f 1884 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
cf7776dc
HL
1885 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1886 if (rc < 0) {
cf7776dc
HL
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);
1892 }
1893 return rc;
1894 }
1895
1896 /* try to send first command in queue */
1897 spin_lock_irqsave(&cs->cmdlock, flags);
1898
9d4bee2b 1899 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
cf7776dc
HL
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);
1905 }
1906 }
1907
1908 spin_unlock_irqrestore(&cs->cmdlock, flags);
1909 return retval;
1910}
1911
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)
917f5085
TS
1923 * wake_tasklet tasklet to run when transmission is completed
1924 * (NULL if none)
cf7776dc
HL
1925 * return value:
1926 * number of bytes queued on success
1927 * error code < 0 on error
1928 */
1929static int gigaset_write_cmd(struct cardstate *cs,
784d5858
TS
1930 const unsigned char *buf, int len,
1931 struct tasklet_struct *wake_tasklet)
cf7776dc
HL
1932{
1933 struct cmdbuf_t *cb;
1934 unsigned long flags;
39f07223 1935 int rc;
cf7776dc 1936
9d4bee2b 1937 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
784d5858 1938 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
01371500 1939 "CMD Transmit", len, buf);
cf7776dc 1940
39f07223
TS
1941 if (len <= 0) {
1942 /* nothing to do */
1943 rc = 0;
1944 goto notqueued;
1945 }
cf7776dc 1946
b5f581d5
TS
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.
1950 */
1951 if (len == 3 && !memcmp(buf, "+++", 3)) {
1952 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
1953 goto notqueued;
1954 }
1955
cf7776dc
HL
1956 if (len > IF_WRITEBUF)
1957 len = IF_WRITEBUF;
7891adf1
TS
1958 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
1959 if (!cb) {
784d5858 1960 dev_err(cs->dev, "%s: out of memory\n", __func__);
39f07223
TS
1961 rc = -ENOMEM;
1962 goto notqueued;
cf7776dc
HL
1963 }
1964
1965 memcpy(cb->buf, buf, len);
1966 cb->len = len;
1967 cb->offset = 0;
1968 cb->next = NULL;
1969 cb->wake_tasklet = wake_tasklet;
1970
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;
1978 }
1979 cs->cmdbytes += len;
1980 cs->lastcmdbuf = cb;
1981 spin_unlock_irqrestore(&cs->cmdlock, flags);
1982
73a88814
TS
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__);
39f07223
TS
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);
73a88814
TS
1992 return -ENODEV;
1993 }
39f07223 1994 rc = start_cbsend(cs);
73a88814 1995 spin_unlock_irqrestore(&cs->lock, flags);
39f07223
TS
1996 return rc < 0 ? rc : len;
1997
1998notqueued: /* request handled without queuing */
1999 if (wake_tasklet)
2000 tasklet_schedule(wake_tasklet);
2001 return rc;
cf7776dc
HL
2002}
2003
2004/* gigaset_write_room
2005 * tty_driver.write_room interface routine
917f5085
TS
2006 * return number of characters the driver will accept to be written via
2007 * gigaset_write_cmd
cf7776dc
HL
2008 * parameter:
2009 * controller state structure
2010 * return value:
2011 * number of characters
2012 */
2013static int gigaset_write_room(struct cardstate *cs)
2014{
2015 return IF_WRITEBUF;
2016}
2017
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
2025 */
2026static int gigaset_chars_in_buffer(struct cardstate *cs)
2027{
4d1ff582 2028 return cs->cmdbytes;
cf7776dc
HL
2029}
2030
2031/* gigaset_brkchars
2032 * implementation of ioctl(GIGASET_BRKCHARS)
2033 * parameter:
2034 * controller state structure
2035 * return value:
2036 * -EINVAL (unimplemented function)
2037 */
2038static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2039{
2040 return -EINVAL;
2041}
2042
2043
2044/* Device Initialization/Shutdown */
2045/* ============================== */
2046
2047/* Free hardware dependent part of the B channel structure
2048 * parameter:
2049 * bcs B channel structure
2050 * return value:
2051 * !=0 on success
2052 */
2053static int gigaset_freebcshw(struct bc_state *bcs)
2054{
73a88814
TS
2055 struct bas_bc_state *ubc = bcs->hw.bas;
2056 int i;
2057
2058 if (!ubc)
cf7776dc
HL
2059 return 0;
2060
73a88814 2061 /* kill URBs and tasklets before freeing - better safe than sorry */
9d4bee2b 2062 ubc->running = 0;
39f07223
TS
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);
2067 }
2068 for (i = 0; i < BAS_INURBS; ++i) {
2069 usb_kill_urb(ubc->isoinurbs[i]);
2070 usb_free_urb(ubc->isoinurbs[i]);
2071 }
73a88814
TS
2072 tasklet_kill(&ubc->sent_tasklet);
2073 tasklet_kill(&ubc->rcvd_tasklet);
2074 kfree(ubc->isooutbuf);
2075 kfree(ubc);
cf7776dc
HL
2076 bcs->hw.bas = NULL;
2077 return 1;
2078}
2079
2080/* Initialize hardware dependent part of the B channel structure
2081 * parameter:
2082 * bcs B channel structure
2083 * return value:
2084 * !=0 on success
2085 */
2086static int gigaset_initbcshw(struct bc_state *bcs)
2087{
2088 int i;
2089 struct bas_bc_state *ubc;
2090
2091 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2092 if (!ubc) {
c8770dca 2093 pr_err("out of memory\n");
cf7776dc
HL
2094 return 0;
2095 }
2096
9d4bee2b 2097 ubc->running = 0;
cf7776dc
HL
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;
2103 }
2104 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2105 ubc->numsub = 0;
7891adf1
TS
2106 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2107 if (!ubc->isooutbuf) {
c8770dca 2108 pr_err("out of memory\n");
cf7776dc
HL
2109 kfree(ubc);
2110 bcs->hw.bas = NULL;
2111 return 0;
2112 }
2113 tasklet_init(&ubc->sent_tasklet,
5452fee2 2114 write_iso_tasklet, (unsigned long) bcs);
cf7776dc
HL
2115
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,
5452fee2 2135 read_iso_tasklet, (unsigned long) bcs);
cf7776dc
HL
2136 return 1;
2137}
2138
2139static void gigaset_reinitbcshw(struct bc_state *bcs)
2140{
2141 struct bas_bc_state *ubc = bcs->hw.bas;
2142
9d4bee2b 2143 bcs->hw.bas->running = 0;
cf7776dc
HL
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;
2149}
2150
2151static void gigaset_freecshw(struct cardstate *cs)
2152{
73a88814 2153 /* timers, URBs and rcvbuf are disposed of in disconnect */
170ebf85 2154 kfree(cs->hw.bas->int_in_buf);
cf7776dc 2155 kfree(cs->hw.bas);
73a88814 2156 cs->hw.bas = NULL;
cf7776dc
HL
2157}
2158
2159static int gigaset_initcshw(struct cardstate *cs)
2160{
2161 struct bas_cardstate *ucs;
2162
2163 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
c8770dca
TS
2164 if (!ucs) {
2165 pr_err("out of memory\n");
cf7776dc 2166 return 0;
c8770dca 2167 }
170ebf85
TS
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;
2173 }
cf7776dc
HL
2174
2175 ucs->urb_cmd_in = NULL;
2176 ucs->urb_cmd_out = NULL;
2177 ucs->rcvbuf = NULL;
2178 ucs->rcvbuf_size = 0;
2179
2180 spin_lock_init(&ucs->lock);
2181 ucs->pending = 0;
2182
9d4bee2b 2183 ucs->basstate = 0;
cf7776dc
HL
2184 init_timer(&ucs->timer_ctrl);
2185 init_timer(&ucs->timer_atrdy);
2186 init_timer(&ucs->timer_cmd_in);
024fd299 2187 init_waitqueue_head(&ucs->waitqueue);
cf7776dc
HL
2188
2189 return 1;
2190}
2191
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
2197 */
2198static void freeurbs(struct cardstate *cs)
2199{
d48c7784 2200 struct bas_cardstate *ucs = cs->hw.bas;
cf7776dc
HL
2201 struct bas_bc_state *ubc;
2202 int i, j;
2203
39f07223 2204 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
c652cbd8 2205 for (j = 0; j < BAS_CHANNELS; ++j) {
cf7776dc 2206 ubc = cs->bcs[j].hw.bas;
39f07223
TS
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;
2211 }
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;
2216 }
cf7776dc 2217 }
39f07223
TS
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;
cf7776dc
HL
2230}
2231
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.
2235 */
2236static int gigaset_probe(struct usb_interface *interface,
2237 const struct usb_device_id *id)
2238{
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;
73a88814 2246 int rc;
cf7776dc 2247
1528b18f 2248 gig_dbg(DEBUG_INIT,
784d5858
TS
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));
cf7776dc 2252
cf7776dc
HL
2253 /* set required alternate setting */
2254 hostif = interface->cur_altsetting;
2255 if (hostif->desc.bAlternateSetting != 3) {
1528b18f 2256 gig_dbg(DEBUG_INIT,
784d5858
TS
2257 "%s: wrong alternate setting %d - trying to switch",
2258 __func__, hostif->desc.bAlternateSetting);
7891adf1
TS
2259 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2260 < 0) {
784d5858
TS
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);
cf7776dc
HL
2265 return -ENODEV;
2266 }
2267 hostif = interface->cur_altsetting;
2268 }
2269
2270 /* Reject application specific interfaces
2271 */
2272 if (hostif->desc.bInterfaceClass != 255) {
784d5858
TS
2273 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2274 __func__, hostif->desc.bInterfaceClass);
cf7776dc
HL
2275 return -ENODEV;
2276 }
2277
784d5858
TS
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));
cf7776dc 2282
e468c048
TS
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)
cf7776dc 2287 return -ENODEV;
cf7776dc 2288 ucs = cs->hw.bas;
784d5858
TS
2289
2290 /* save off device structure ptrs for later use */
2291 usb_get_dev(udev);
cf7776dc
HL
2292 ucs->udev = udev;
2293 ucs->interface = interface;
b1d47464 2294 cs->dev = &interface->dev;
cf7776dc
HL
2295
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
2300 */
e94b1766
CL
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)))
73a88814 2305 goto allocerr;
cf7776dc 2306
c652cbd8 2307 for (j = 0; j < BAS_CHANNELS; ++j) {
cf7776dc 2308 ubc = cs->bcs[j].hw.bas;
73a88814
TS
2309 for (i = 0; i < BAS_OUTURBS; ++i)
2310 if (!(ubc->isoouturbs[i].urb =
e94b1766 2311 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
73a88814
TS
2312 goto allocerr;
2313 for (i = 0; i < BAS_INURBS; ++i)
2314 if (!(ubc->isoinurbs[i] =
e94b1766 2315 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
73a88814 2316 goto allocerr;
cf7776dc
HL
2317 }
2318
2319 ucs->rcvbuf = NULL;
2320 ucs->rcvbuf_size = 0;
2321
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,
784d5858
TS
2325 usb_rcvintpipe(udev,
2326 (endpoint->bEndpointAddress) & 0x0f),
170ebf85 2327 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
784d5858 2328 endpoint->bInterval);
7891adf1
TS
2329 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2330 if (rc != 0) {
784d5858 2331 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
73a88814 2332 get_usb_rcmsg(rc));
cf7776dc
HL
2333 goto error;
2334 }
2335
2336 /* tell the device that the driver is ready */
7891adf1
TS
2337 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2338 if (rc != 0)
cf7776dc
HL
2339 goto error;
2340
2341 /* tell common part that the device is ready */
2342 if (startmode == SM_LOCKED)
9d4bee2b 2343 cs->mstate = MS_LOCKED;
cf7776dc
HL
2344
2345 /* save address of controller structure */
2346 usb_set_intfdata(interface, cs);
2347
b1d47464
TS
2348 if (!gigaset_start(cs))
2349 goto error;
2350
cf7776dc
HL
2351 return 0;
2352
73a88814
TS
2353allocerr:
2354 dev_err(cs->dev, "could not allocate URBs\n");
cf7776dc
HL
2355error:
2356 freeurbs(cs);
69049cc8 2357 usb_set_intfdata(interface, NULL);
e468c048 2358 gigaset_freecs(cs);
cf7776dc
HL
2359 return -ENODEV;
2360}
2361
2362/* gigaset_disconnect
2363 * This function is called when the Gigaset base is unplugged.
2364 */
2365static void gigaset_disconnect(struct usb_interface *interface)
2366{
2367 struct cardstate *cs;
2368 struct bas_cardstate *ucs;
73a88814 2369 int j;
cf7776dc 2370
cf7776dc 2371 cs = usb_get_intfdata(interface);
cf7776dc 2372
cf7776dc 2373 ucs = cs->hw.bas;
cf7776dc 2374
b1d47464 2375 dev_info(cs->dev, "disconnecting Gigaset base\n");
73a88814
TS
2376
2377 /* mark base as not ready, all channels disconnected */
9d4bee2b 2378 ucs->basstate = 0;
73a88814
TS
2379
2380 /* tell LL all channels are down */
c652cbd8 2381 for (j = 0; j < BAS_CHANNELS; ++j)
73a88814
TS
2382 gigaset_bchannel_down(cs->bcs + j);
2383
2384 /* stop driver (common part) */
cf7776dc 2385 gigaset_stop(cs);
73a88814
TS
2386
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);
cf7776dc 2391 freeurbs(cs);
b1d47464 2392 usb_set_intfdata(interface, NULL);
cf7776dc
HL
2393 kfree(ucs->rcvbuf);
2394 ucs->rcvbuf = NULL;
2395 ucs->rcvbuf_size = 0;
b1d47464
TS
2396 usb_put_dev(ucs->udev);
2397 ucs->interface = NULL;
2398 ucs->udev = NULL;
2399 cs->dev = NULL;
e468c048 2400 gigaset_freecs(cs);
cf7776dc
HL
2401}
2402
024fd299
TS
2403/* gigaset_suspend
2404 * This function is called before the USB connection is suspended.
2405 */
2406static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2407{
2408 struct cardstate *cs = usb_get_intfdata(intf);
2409 struct bas_cardstate *ucs = cs->hw.bas;
024fd299
TS
2410 int rc;
2411
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;
2416 }
2417
2418 /* wait a bit for blocking conditions to go away */
2419 rc = wait_event_timeout(ucs->waitqueue,
9d4bee2b 2420 !(ucs->basstate &
024fd299
TS
2421 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2422 BAS_TIMEOUT*HZ/10);
2423 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2424
2425 /* check for conditions preventing suspend */
9d4bee2b 2426 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
024fd299 2427 dev_warn(cs->dev, "cannot suspend:\n");
9d4bee2b 2428 if (ucs->basstate & BS_B1OPEN)
024fd299 2429 dev_warn(cs->dev, " B channel 1 open\n");
9d4bee2b 2430 if (ucs->basstate & BS_B2OPEN)
024fd299 2431 dev_warn(cs->dev, " B channel 2 open\n");
9d4bee2b 2432 if (ucs->basstate & BS_ATRDPEND)
024fd299 2433 dev_warn(cs->dev, " receiving AT reply\n");
9d4bee2b 2434 if (ucs->basstate & BS_ATWRPEND)
024fd299
TS
2435 dev_warn(cs->dev, " sending AT command\n");
2436 update_basstate(ucs, 0, BS_SUSPEND);
2437 return -EBUSY;
2438 }
2439
2440 /* close AT channel if open */
9d4bee2b 2441 if (ucs->basstate & BS_ATOPEN) {
024fd299
TS
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;
2447 }
2448 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2449 BAS_TIMEOUT*HZ/10);
2450 /* in case of timeout, proceed anyway */
2451 }
2452
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);
2457
2458 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2459 return 0;
2460}
2461
2462/* gigaset_resume
2463 * This function is called after the USB connection has been resumed.
2464 */
2465static int gigaset_resume(struct usb_interface *intf)
2466{
2467 struct cardstate *cs = usb_get_intfdata(intf);
2468 struct bas_cardstate *ucs = cs->hw.bas;
2469 int rc;
2470
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;
2477 }
2478
2479 /* clear suspend flag to reallow activity */
2480 update_basstate(ucs, 0, BS_SUSPEND);
2481
2482 gig_dbg(DEBUG_SUSPEND, "resume complete");
2483 return 0;
2484}
2485
2486/* gigaset_pre_reset
2487 * This function is called before the USB connection is reset.
2488 */
2489static int gigaset_pre_reset(struct usb_interface *intf)
2490{
2491 /* handle just like suspend */
2492 return gigaset_suspend(intf, PMSG_ON);
2493}
2494
2495/* gigaset_post_reset
2496 * This function is called after the USB connection has been reset.
2497 */
2498static int gigaset_post_reset(struct usb_interface *intf)
2499{
2500 /* FIXME: send HD_DEVICE_INIT_ACK? */
2501
2502 /* resume operations */
2503 return gigaset_resume(intf);
2504}
2505
2506
35dc8457 2507static const struct gigaset_ops gigops = {
cf7776dc
HL
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,
2524};
2525
2526/* bas_gigaset_init
2527 * This function is called after the kernel module is loaded.
2528 */
2529static int __init bas_gigaset_init(void)
2530{
2531 int result;
2532
2533 /* allocate memory for our driver state and intialize it */
7891adf1
TS
2534 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2535 GIGASET_MODULENAME, GIGASET_DEVNAME,
2536 &gigops, THIS_MODULE);
2537 if (driver == NULL)
cf7776dc
HL
2538 goto error;
2539
cf7776dc
HL
2540 /* register this driver with the USB subsystem */
2541 result = usb_register(&gigaset_usb_driver);
2542 if (result < 0) {
c8770dca 2543 pr_err("error %d registering USB driver\n", -result);
cf7776dc
HL
2544 goto error;
2545 }
2546
c8770dca 2547 pr_info(DRIVER_DESC "\n");
cf7776dc
HL
2548 return 0;
2549
e468c048 2550error:
cf7776dc
HL
2551 if (driver)
2552 gigaset_freedriver(driver);
2553 driver = NULL;
2554 return -1;
2555}
2556
2557/* bas_gigaset_exit
2558 * This function is called before the kernel module is unloaded.
2559 */
2560static void __exit bas_gigaset_exit(void)
2561{
e468c048
TS
2562 struct bas_cardstate *ucs;
2563 int i;
73a88814 2564
cf7776dc 2565 gigaset_blockdriver(driver); /* => probe will fail
784d5858
TS
2566 * => no gigaset_start any more
2567 */
cf7776dc 2568
e468c048
TS
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 */
2574
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);
2583 }
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);
2590 }
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);
2597 }
2598 ucs->basstate = 0;
cf7776dc
HL
2599 }
2600
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 */
2605
cf7776dc
HL
2606 gigaset_freedriver(driver);
2607 driver = NULL;
2608}
2609
2610
2611module_init(bas_gigaset_init);
2612module_exit(bas_gigaset_exit);
2613
2614MODULE_AUTHOR(DRIVER_AUTHOR);
2615MODULE_DESCRIPTION(DRIVER_DESC);
2616MODULE_LICENSE("GPL");
This page took 0.573611 seconds and 5 git commands to generate.