blob: 7c905305406b6f18ddfa1e9d7f403fa77873aa6d [file] [log] [blame]
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001/*
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>,
Tilman Schmidt70440cf2006-04-10 22:55:14 -07006 * Stefan Eilers.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08007 *
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08008 * =====================================================================
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 * =====================================================================
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080014 */
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 */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070027#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080028#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"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080044#define GIGASET_DEVNAME "ttyGB"
45
Tilman Schmidt73a88812006-04-22 02:35:30 -070046/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
47#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080048
49/* Values for the Gigaset 307x */
50#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070051#define USB_3070_PRODUCT_ID 0x0001
52#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080053#define USB_SX303_PRODUCT_ID 0x0021
54#define USB_SX353_PRODUCT_ID 0x0022
55
56/* table of devices that work with this driver */
Tilman Schmidt35dc8452007-03-29 01:20:34 -070057static const struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070058 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080060 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
61 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
62 { } /* Terminating entry */
63};
64
65MODULE_DEVICE_TABLE(usb, gigaset_table);
66
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068
Tilman Schmidt06163f82006-06-26 00:25:33 -070069/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080070static int gigaset_probe(struct usb_interface *interface,
71 const struct usb_device_id *id);
72
73/* Function will be called if the device is unplugged */
74static void gigaset_disconnect(struct usb_interface *interface);
75
Tilman Schmidt024fd292008-02-06 01:38:26 -080076/* functions called before/after suspend */
77static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
78static int gigaset_resume(struct usb_interface *intf);
79
80/* functions called before/after device reset */
81static int gigaset_pre_reset(struct usb_interface *intf);
82static int gigaset_post_reset(struct usb_interface *intf);
83
Tilman Schmidt06163f82006-06-26 00:25:33 -070084static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070085static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070086static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070087static int atwrite_submit(struct cardstate *, unsigned char *, int);
88static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089
Tilman Schmidt06163f82006-06-26 00:25:33 -070090/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080091
92struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070093 struct usb_device *udev; /* USB device pointer */
94 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080095 unsigned char minor; /* starting minor number */
96
Tilman Schmidt784d5852006-04-10 22:55:04 -070097 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080098 struct usb_ctrlrequest dr_ctrl;
99 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700100 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800101
102 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700103 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800104 struct usb_ctrlrequest dr_cmd_out;
105 int retry_cmd_out;
106
Tilman Schmidt784d5852006-04-10 22:55:04 -0700107 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800108 struct usb_ctrlrequest dr_cmd_in;
109 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800111
Tilman Schmidt784d5852006-04-10 22:55:04 -0700112 struct urb *urb_int_in; /* URB for interrupt pipe */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800113 unsigned char int_in_buf[3];
114
115 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800116 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800117 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800118 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800119 int rcvbuf_size; /* size of AT receive buffer */
120 /* 0: no receive in progress */
121 int retry_cmd_in; /* receive req retry count */
122};
123
124/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700125#define BS_ATOPEN 0x001 /* AT channel open */
126#define BS_B1OPEN 0x002 /* B channel 1 open */
127#define BS_B2OPEN 0x004 /* B channel 2 open */
128#define BS_ATREADY 0x008 /* base ready for AT command */
129#define BS_INIT 0x010 /* base has signalled INIT_OK */
130#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
131#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
132#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800133#define BS_SUSPEND 0x100 /* USB port suspended */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800134
135
136static struct gigaset_driver *driver = NULL;
137static struct cardstate *cardstate = NULL;
138
139/* usb specific object needed to register this driver with the usb subsystem */
140static struct usb_driver gigaset_usb_driver = {
141 .name = GIGASET_MODULENAME,
142 .probe = gigaset_probe,
143 .disconnect = gigaset_disconnect,
144 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800145 .suspend = gigaset_suspend,
146 .resume = gigaset_resume,
147 .reset_resume = gigaset_post_reset,
148 .pre_reset = gigaset_pre_reset,
149 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800150};
151
Tilman Schmidt73a88812006-04-22 02:35:30 -0700152/* get message text for usb_submit_urb return code
153 */
154static char *get_usb_rcmsg(int rc)
155{
156 static char unkmsg[28];
157
158 switch (rc) {
159 case 0:
160 return "success";
161 case -ENOMEM:
162 return "out of memory";
163 case -ENODEV:
164 return "device not present";
165 case -ENOENT:
166 return "endpoint not present";
167 case -ENXIO:
168 return "URB type not supported";
169 case -EINVAL:
170 return "invalid argument";
171 case -EAGAIN:
172 return "start frame too early or too much scheduled";
173 case -EFBIG:
174 return "too many isochronous frames requested";
175 case -EPIPE:
176 return "endpoint stalled";
177 case -EMSGSIZE:
178 return "invalid packet size";
179 case -ENOSPC:
180 return "would overcommit USB bandwidth";
181 case -ESHUTDOWN:
182 return "device shut down";
183 case -EPERM:
184 return "reject flag set";
185 case -EHOSTUNREACH:
186 return "device suspended";
187 default:
188 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
189 return unkmsg;
190 }
191}
192
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800193/* get message text for USB status code
194 */
195static char *get_usb_statmsg(int status)
196{
197 static char unkmsg[28];
198
199 switch (status) {
200 case 0:
201 return "success";
202 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700203 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800204 case -EINPROGRESS:
205 return "pending";
206 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700207 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800208 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700209 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700210 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800211 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700212 case -EPIPE:
213 return "endpoint stalled";
214 case -ECOMM:
215 return "IN buffer overrun";
216 case -ENOSR:
217 return "OUT buffer underrun";
218 case -EOVERFLOW:
219 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800220 case -EREMOTEIO:
221 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700222 case -ENODEV:
223 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800224 case -EXDEV:
225 return "partial isochronous transfer";
226 case -EINVAL:
227 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700228 case -ECONNRESET:
229 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800230 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700231 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800232 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700233 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800234 return unkmsg;
235 }
236}
237
238/* usb_pipetype_str
239 * retrieve string representation of USB pipe type
240 */
241static inline char *usb_pipetype_str(int pipe)
242{
243 if (usb_pipeisoc(pipe))
244 return "Isoc";
245 if (usb_pipeint(pipe))
246 return "Int";
247 if (usb_pipecontrol(pipe))
248 return "Ctrl";
249 if (usb_pipebulk(pipe))
250 return "Bulk";
251 return "?";
252}
253
254/* dump_urb
255 * write content of URB to syslog for debugging
256 */
257static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700258 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800259{
260#ifdef CONFIG_GIGASET_DEBUG
261 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700262 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800263 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700264 gig_dbg(level,
265 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800266 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700267 (unsigned long) urb->dev,
268 usb_pipetype_str(urb->pipe),
269 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
270 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800271 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700272 urb->transfer_flags);
273 gig_dbg(level,
274 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800275 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700276 (unsigned long) urb->transfer_buffer,
277 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800278 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700279 gig_dbg(level,
280 " start_frame=%d, number_of_packets=%d, interval=%d, "
281 "error_count=%d,",
282 urb->start_frame, urb->number_of_packets, urb->interval,
283 urb->error_count);
284 gig_dbg(level,
285 " context=0x%08lx, complete=0x%08lx, "
286 "iso_frame_desc[]={",
287 (unsigned long) urb->context,
288 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800289 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700290 struct usb_iso_packet_descriptor *pifd
291 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700292 gig_dbg(level,
293 " {offset=%u, length=%u, actual_length=%u, "
294 "status=%u}",
295 pifd->offset, pifd->length, pifd->actual_length,
296 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800297 }
298 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700299 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800300#endif
301}
302
303/* read/set modem control bits etc. (m10x only) */
304static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700305 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800306{
307 return -EINVAL;
308}
309
310static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
311{
312 return -EINVAL;
313}
314
315static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
316{
317 return -EINVAL;
318}
319
320/* error_hangup
321 * hang up any existing connection because of an unrecoverable error
322 * This function may be called from any context and takes care of scheduling
323 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700324 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800325 * argument:
326 * B channel control structure
327 */
328static inline void error_hangup(struct bc_state *bcs)
329{
330 struct cardstate *cs = bcs->cs;
331
Tilman Schmidt784d5852006-04-10 22:55:04 -0700332 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
333 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800334
Tilman Schmidt73a88812006-04-22 02:35:30 -0700335 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
336 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800337
338 gigaset_schedule_event(cs);
339}
340
341/* error_reset
342 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700343 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700344 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700345 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800346 * argument:
347 * controller state structure
348 */
349static inline void error_reset(struct cardstate *cs)
350{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700351 /* close AT command channel to recover (ignore errors) */
352 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
353
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800354 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700355 dev_err(cs->dev,
356 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800357}
358
359/* check_pending
360 * check for completion of pending control request
361 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700362 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800363 */
364static void check_pending(struct bas_cardstate *ucs)
365{
366 unsigned long flags;
367
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800368 spin_lock_irqsave(&ucs->lock, flags);
369 switch (ucs->pending) {
370 case 0:
371 break;
372 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800373 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800374 ucs->pending = 0;
375 break;
376 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800377 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800378 ucs->pending = 0;
379 break;
380 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800381 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800382 ucs->pending = 0;
383 break;
384 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800385 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800386 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800387 break;
388 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800389 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800390 ucs->pending = 0;
391 break;
392 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800393 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800394 ucs->pending = 0;
395 break;
396 case HD_DEVICE_INIT_ACK: /* no reply expected */
397 ucs->pending = 0;
398 break;
399 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
400 * are handled separately and should never end up here
401 */
402 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700403 dev_warn(&ucs->interface->dev,
404 "unknown pending request 0x%02x cleared\n",
405 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800406 ucs->pending = 0;
407 }
408
409 if (!ucs->pending)
410 del_timer(&ucs->timer_ctrl);
411
412 spin_unlock_irqrestore(&ucs->lock, flags);
413}
414
415/* cmd_in_timeout
416 * timeout routine for command input request
417 * argument:
418 * controller state structure
419 */
420static void cmd_in_timeout(unsigned long data)
421{
422 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700423 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700424 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800425
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800426 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700427 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800428 return;
429 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800430
Tilman Schmidt06163f82006-06-26 00:25:33 -0700431 if (ucs->retry_cmd_in++ < BAS_RETRY) {
432 dev_notice(cs->dev, "control read: timeout, retry %d\n",
433 ucs->retry_cmd_in);
434 rc = atread_submit(cs, BAS_TIMEOUT);
435 if (rc >= 0 || rc == -ENODEV)
436 /* resubmitted or disconnected */
437 /* - bypass regular exit block */
438 return;
439 } else {
440 dev_err(cs->dev,
441 "control read: timeout, giving up after %d tries\n",
442 ucs->retry_cmd_in);
443 }
444 kfree(ucs->rcvbuf);
445 ucs->rcvbuf = NULL;
446 ucs->rcvbuf_size = 0;
447 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800448}
449
Tilman Schmidt73a88812006-04-22 02:35:30 -0700450/* set/clear bits in base connection state, return previous state
451 */
452inline static int update_basstate(struct bas_cardstate *ucs,
453 int set, int clear)
454{
455 unsigned long flags;
456 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800457
Tilman Schmidt73a88812006-04-22 02:35:30 -0700458 spin_lock_irqsave(&ucs->lock, flags);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800459 state = ucs->basstate;
460 ucs->basstate = (state & ~clear) | set;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700461 spin_unlock_irqrestore(&ucs->lock, flags);
462 return state;
463}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800464
Tilman Schmidt06163f82006-06-26 00:25:33 -0700465/* 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 */
David Howells7d12e782006-10-05 14:55:46 +0100472static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700473{
474 struct inbuf_t *inbuf = urb->context;
475 struct cardstate *cs = inbuf->cs;
476 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800477 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700478 int have_data = 0;
479 unsigned numbytes;
480 int rc;
481
482 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800483 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700484
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
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800492 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700493 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",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800526 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700527 break;
528
529 default: /* severe trouble */
530 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800531 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700532 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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800557/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700558 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800559 * parameters:
560 * cs controller state structure
561 * timeout timeout in 1/10 sec., 0: none
562 * return value:
563 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800564 * -EBUSY if another request is pending
565 * any URB submission error code
566 */
567static int atread_submit(struct cardstate *cs, int timeout)
568{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700569 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800570 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800571 int ret;
572
Tilman Schmidt784d5852006-04-10 22:55:04 -0700573 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
574 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575
Tilman Schmidt024fd292008-02-06 01:38:26 -0800576 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
577 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 dev_err(cs->dev,
579 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580 return -EBUSY;
581 }
582
Tilman Schmidt024fd292008-02-06 01:38:26 -0800583 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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800592 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,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700598 usb_rcvctrlpipe(ucs->udev, 0),
599 (unsigned char*) & ucs->dr_cmd_in,
600 ucs->rcvbuf, ucs->rcvbuf_size,
601 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800602
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800603 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700604 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700605 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700606 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800607 return ret;
608 }
609
610 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700611 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800612 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
613 ucs->timer_cmd_in.data = (unsigned long) cs;
614 ucs->timer_cmd_in.function = cmd_in_timeout;
615 add_timer(&ucs->timer_cmd_in);
616 }
617 return 0;
618}
619
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800620/* read_int_callback
621 * USB completion handler for interrupt pipe input
622 * called by the USB subsystem in interrupt context
623 * parameter:
624 * urb USB request block
625 * urb->context = controller state structure
626 */
David Howells7d12e782006-10-05 14:55:46 +0100627static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800628{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700629 struct cardstate *cs = urb->context;
630 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800631 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800632 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800633 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700634 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800635 unsigned l;
636 int channel;
637
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800638 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800639 case 0: /* success */
640 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700641 case -ENOENT: /* cancelled */
642 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800643 case -EINPROGRESS: /* pending */
644 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700645 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800646 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800647 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700648 case -ENODEV: /* device removed */
649 case -ESHUTDOWN: /* device shut down */
650 //FIXME use this as disconnect indicator?
651 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
652 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800653 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700654 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800655 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800656 //FIXME corrective action? resubmission always ok?
657 goto resubmit;
658 }
659
Tilman Schmidt73a88812006-04-22 02:35:30 -0700660 /* drop incomplete packets even if the missing bytes wouldn't matter */
661 if (unlikely(urb->actual_length < 3)) {
662 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
663 urb->actual_length);
664 goto resubmit;
665 }
666
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800667 l = (unsigned) ucs->int_in_buf[1] +
668 (((unsigned) ucs->int_in_buf[2]) << 8);
669
Tilman Schmidt784d5852006-04-10 22:55:04 -0700670 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
671 urb->actual_length, (int)ucs->int_in_buf[0], l,
672 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800673
674 channel = 0;
675
676 switch (ucs->int_in_buf[0]) {
677 case HD_DEVICE_INIT_OK:
678 update_basstate(ucs, BS_INIT, 0);
679 break;
680
681 case HD_READY_SEND_ATDATA:
682 del_timer(&ucs->timer_atrdy);
683 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
684 start_cbsend(cs);
685 break;
686
687 case HD_OPEN_B2CHANNEL_ACK:
688 ++channel;
689 case HD_OPEN_B1CHANNEL_ACK:
690 bcs = cs->bcs + channel;
691 update_basstate(ucs, BS_B1OPEN << channel, 0);
692 gigaset_bchannel_up(bcs);
693 break;
694
695 case HD_OPEN_ATCHANNEL_ACK:
696 update_basstate(ucs, BS_ATOPEN, 0);
697 start_cbsend(cs);
698 break;
699
700 case HD_CLOSE_B2CHANNEL_ACK:
701 ++channel;
702 case HD_CLOSE_B1CHANNEL_ACK:
703 bcs = cs->bcs + channel;
704 update_basstate(ucs, 0, BS_B1OPEN << channel);
705 stopurbs(bcs->hw.bas);
706 gigaset_bchannel_down(bcs);
707 break;
708
709 case HD_CLOSE_ATCHANNEL_ACK:
710 update_basstate(ucs, 0, BS_ATOPEN);
711 break;
712
713 case HD_B2_FLOW_CONTROL:
714 ++channel;
715 case HD_B1_FLOW_CONTROL:
716 bcs = cs->bcs + channel;
717 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700718 &bcs->hw.bas->corrbytes);
719 gig_dbg(DEBUG_ISO,
720 "Flow control (channel %d, sub %d): 0x%02x => %d",
721 channel, bcs->hw.bas->numsub, l,
722 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800723 break;
724
725 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
726 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700727 dev_warn(cs->dev,
728 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800729 break;
730 }
731 spin_lock_irqsave(&cs->lock, flags);
732 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700733 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700734 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700735 "receive AT data overrun, %d bytes lost\n",
736 ucs->rcvbuf_size);
737 kfree(ucs->rcvbuf);
738 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800739 }
740 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
741 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700742 dev_err(cs->dev, "out of memory receiving AT data\n");
743 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800744 break;
745 }
746 ucs->rcvbuf_size = l;
747 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700748 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800749 kfree(ucs->rcvbuf);
750 ucs->rcvbuf = NULL;
751 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700752 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700753 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700754 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700755 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700756 break;
757 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800758 }
759 spin_unlock_irqrestore(&cs->lock, flags);
760 break;
761
762 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700763 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800764 break;
765
766 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700767 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800768 break;
769
770 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700771 dev_warn(cs->dev,
772 "unknown Gigaset signal 0x%02x (%u) ignored\n",
773 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800774 }
775
776 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800777 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800778
779resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800780 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700781 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700782 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700783 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800784 error_reset(cs);
785 }
786}
787
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800788/* read_iso_callback
789 * USB completion handler for B channel isochronous input
790 * called by the USB subsystem in interrupt context
791 * parameter:
792 * urb USB request block of completed request
793 * urb->context = bc_state structure
794 */
David Howells7d12e782006-10-05 14:55:46 +0100795static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800796{
797 struct bc_state *bcs;
798 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800799 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800800 unsigned long flags;
801 int i, rc;
802
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800803 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800804 if (unlikely(status == -ENOENT ||
805 status == -ECONNRESET ||
806 status == -EINPROGRESS ||
807 status == -ENODEV ||
808 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700809 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800810 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800811 return;
812 }
813
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700814 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816
817 spin_lock_irqsave(&ubc->isoinlock, flags);
818 if (likely(ubc->isoindone == NULL)) {
819 /* pass URB to tasklet */
820 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800821 ubc->isoinstatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800822 tasklet_schedule(&ubc->rcvd_tasklet);
823 } else {
824 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800825 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800826 for (i = 0; i < BAS_NUMFRAMES; i++) {
827 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
828 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700829 urb->iso_frame_desc[i].status !=
830 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800831 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800832 urb->iso_frame_desc[i].status = 0;
833 urb->iso_frame_desc[i].actual_length = 0;
834 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800835 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700836 /* urb->dev is clobbered by USB subsystem */
837 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800838 urb->transfer_flags = URB_ISO_ASAP;
839 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700840 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
841 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800842 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700843 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700844 dev_err(bcs->cs->dev,
845 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700846 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800847 dump_urb(DEBUG_ISO, "isoc read", urb);
848 error_hangup(bcs);
849 }
850 }
851 }
852 spin_unlock_irqrestore(&ubc->isoinlock, flags);
853}
854
855/* write_iso_callback
856 * USB completion handler for B channel isochronous output
857 * called by the USB subsystem in interrupt context
858 * parameter:
859 * urb USB request block of completed request
860 * urb->context = isow_urbctx_t structure
861 */
David Howells7d12e782006-10-05 14:55:46 +0100862static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800863{
864 struct isow_urbctx_t *ucx;
865 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800866 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800867 unsigned long flags;
868
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800869 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800870 if (unlikely(status == -ENOENT ||
871 status == -ECONNRESET ||
872 status == -EINPROGRESS ||
873 status == -ENODEV ||
874 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700875 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800876 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800877 return;
878 }
879
880 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700881 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800882 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800883 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800884
885 spin_lock_irqsave(&ubc->isooutlock, flags);
886 ubc->isooutovfl = ubc->isooutdone;
887 ubc->isooutdone = ucx;
888 spin_unlock_irqrestore(&ubc->isooutlock, flags);
889 tasklet_schedule(&ubc->sent_tasklet);
890}
891
892/* starturbs
893 * prepare and submit USB request blocks for isochronous input and output
894 * argument:
895 * B channel control structure
896 * return value:
897 * 0 on success
898 * < 0 on error (no URBs submitted)
899 */
900static int starturbs(struct bc_state *bcs)
901{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700902 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800903 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800904 int j, k;
905 int rc;
906
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800907 /* initialize L2 reception */
908 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
909 bcs->inputstate |= INS_flag_hunt;
910
911 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800912 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800913 for (k = 0; k < BAS_INURBS; k++) {
914 urb = ubc->isoinurbs[k];
915 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800916 rc = -EFAULT;
917 goto error;
918 }
919
920 urb->dev = bcs->cs->hw.bas->udev;
921 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
922 urb->transfer_flags = URB_ISO_ASAP;
923 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
924 urb->transfer_buffer_length = BAS_INBUFSIZE;
925 urb->number_of_packets = BAS_NUMFRAMES;
926 urb->interval = BAS_FRAMETIME;
927 urb->complete = read_iso_callback;
928 urb->context = bcs;
929 for (j = 0; j < BAS_NUMFRAMES; j++) {
930 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
931 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
932 urb->iso_frame_desc[j].status = 0;
933 urb->iso_frame_desc[j].actual_length = 0;
934 }
935
936 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800937 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800938 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800939 }
940
941 /* initialize L2 transmission */
942 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
943
944 /* set up isochronous output URBs for flag idling */
945 for (k = 0; k < BAS_OUTURBS; ++k) {
946 urb = ubc->isoouturbs[k].urb;
947 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800948 rc = -EFAULT;
949 goto error;
950 }
951 urb->dev = bcs->cs->hw.bas->udev;
952 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
953 urb->transfer_flags = URB_ISO_ASAP;
954 urb->transfer_buffer = ubc->isooutbuf->data;
955 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
956 urb->number_of_packets = BAS_NUMFRAMES;
957 urb->interval = BAS_FRAMETIME;
958 urb->complete = write_iso_callback;
959 urb->context = &ubc->isoouturbs[k];
960 for (j = 0; j < BAS_NUMFRAMES; ++j) {
961 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
962 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
963 urb->iso_frame_desc[j].status = 0;
964 urb->iso_frame_desc[j].actual_length = 0;
965 }
966 ubc->isoouturbs[k].limit = -1;
967 }
968
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800969 /* keep one URB free, submit the others */
970 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800971 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800972 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700973 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800974 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800975 }
976 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800977 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800978 ubc->isooutdone = ubc->isooutovfl = NULL;
979 return 0;
980 error:
981 stopurbs(ubc);
982 return rc;
983}
984
985/* stopurbs
986 * cancel the USB request blocks for isochronous input and output
987 * errors are silently ignored
988 * argument:
989 * B channel control structure
990 */
991static void stopurbs(struct bas_bc_state *ubc)
992{
993 int k, rc;
994
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800995 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800996
997 for (k = 0; k < BAS_INURBS; ++k) {
998 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700999 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001000 "%s: isoc input URB %d unlinked, result = %s",
1001 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001002 }
1003
1004 for (k = 0; k < BAS_OUTURBS; ++k) {
1005 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001006 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001007 "%s: isoc output URB %d unlinked, result = %s",
1008 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001009 }
1010}
1011
1012/* Isochronous Write - Bottom Half */
1013/* =============================== */
1014
1015/* submit_iso_write_urb
1016 * fill and submit the next isochronous write URB
1017 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001018 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001019 * return value:
1020 * number of frames submitted in URB
1021 * 0 if URB not submitted because no data available (isooutbuf busy)
1022 * error code < 0 on error
1023 */
1024static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1025{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001026 struct urb *urb = ucx->urb;
1027 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001028 struct usb_iso_packet_descriptor *ifd;
1029 int corrbytes, nframe, rc;
1030
Tilman Schmidt784d5852006-04-10 22:55:04 -07001031 /* urb->dev is clobbered by USB subsystem */
1032 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001033 urb->transfer_flags = URB_ISO_ASAP;
1034 urb->transfer_buffer = ubc->isooutbuf->data;
1035 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1036
1037 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1038 ifd = &urb->iso_frame_desc[nframe];
1039
1040 /* compute frame length according to flow control */
1041 ifd->length = BAS_NORMFRAME;
1042 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001043 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1044 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001045 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1046 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1047 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1048 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1049 ifd->length += corrbytes;
1050 atomic_add(-corrbytes, &ubc->corrbytes);
1051 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001052
1053 /* retrieve block of data to send */
Tilman Schmidt917f5082006-04-10 22:55:00 -07001054 ifd->offset = gigaset_isowbuf_getbytes(ubc->isooutbuf,
1055 ifd->length);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001056 if (ifd->offset < 0) {
1057 if (ifd->offset == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001058 gig_dbg(DEBUG_ISO,
1059 "%s: buffer busy at frame %d",
1060 __func__, nframe);
1061 /* tasklet will be restarted from
1062 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001063 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001064 dev_err(ucx->bcs->cs->dev,
1065 "%s: buffer error %d at frame %d\n",
1066 __func__, ifd->offset, nframe);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001067 return ifd->offset;
1068 }
1069 break;
1070 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001071 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001072 ifd->status = 0;
1073 ifd->actual_length = 0;
1074 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001075 if (unlikely(nframe == 0))
1076 return 0; /* no data to send */
1077 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001078
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001079 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001080 if (unlikely(rc)) {
1081 if (rc == -ENODEV)
1082 /* device removed - give up silently */
1083 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1084 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001085 dev_err(ucx->bcs->cs->dev,
1086 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001087 get_usb_rcmsg(rc));
1088 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001089 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001090 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001091 return nframe;
1092}
1093
1094/* write_iso_tasklet
1095 * tasklet scheduled when an isochronous output URB from the Gigaset device
1096 * has completed
1097 * parameter:
1098 * data B channel state structure
1099 */
1100static void write_iso_tasklet(unsigned long data)
1101{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001102 struct bc_state *bcs = (struct bc_state *) data;
1103 struct bas_bc_state *ubc = bcs->hw.bas;
1104 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001105 struct isow_urbctx_t *done, *next, *ovfl;
1106 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001107 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001108 struct usb_iso_packet_descriptor *ifd;
1109 int offset;
1110 unsigned long flags;
1111 int i;
1112 struct sk_buff *skb;
1113 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001114 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001115
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001116 /* loop while completed URBs arrive in time */
1117 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001118 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001119 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001120 return;
1121 }
1122
1123 /* retrieve completed URBs */
1124 spin_lock_irqsave(&ubc->isooutlock, flags);
1125 done = ubc->isooutdone;
1126 ubc->isooutdone = NULL;
1127 ovfl = ubc->isooutovfl;
1128 ubc->isooutovfl = NULL;
1129 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1130 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001131 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001132 error_hangup(bcs);
1133 break;
1134 }
1135 if (!done)
1136 break;
1137
1138 /* submit free URB if available */
1139 spin_lock_irqsave(&ubc->isooutlock, flags);
1140 next = ubc->isooutfree;
1141 ubc->isooutfree = NULL;
1142 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1143 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001144 rc = submit_iso_write_urb(next);
1145 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001146 /* could not submit URB, put it back */
1147 spin_lock_irqsave(&ubc->isooutlock, flags);
1148 if (ubc->isooutfree == NULL) {
1149 ubc->isooutfree = next;
1150 next = NULL;
1151 }
1152 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1153 if (next) {
1154 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001155 dev_err(cs->dev,
1156 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001157 error_hangup(bcs);
1158 }
1159 }
1160 }
1161
1162 /* process completed URB */
1163 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001164 status = done->status;
1165 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001166 case -EXDEV: /* partial completion */
1167 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1168 __func__);
1169 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001170 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001171 /* inspect individual frames
1172 * assumptions (for lack of documentation):
1173 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001174 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001175 * - all following frames are not sent at all
1176 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001177 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001178 for (i = 0; i < BAS_NUMFRAMES; i++) {
1179 ifd = &urb->iso_frame_desc[i];
1180 if (ifd->status ||
1181 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001182 dev_warn(cs->dev,
1183 "isochronous write: frame %d: %s, "
1184 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001185 i, get_usb_statmsg(ifd->status),
1186 ifd->actual_length, ifd->length);
1187 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001188 ifd->actual_length)
1189 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001190 break;
1191 }
1192 }
1193#ifdef CONFIG_GIGASET_DEBUG
1194 /* check assumption on remaining frames */
1195 for (; i < BAS_NUMFRAMES; i++) {
1196 ifd = &urb->iso_frame_desc[i];
1197 if (ifd->status != -EINPROGRESS
1198 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001199 dev_warn(cs->dev,
1200 "isochronous write: frame %d: %s, "
1201 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001202 i, get_usb_statmsg(ifd->status),
1203 ifd->actual_length, ifd->length);
1204 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001205 ifd->actual_length)
1206 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001207 break;
1208 }
1209 }
1210#endif
1211 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001212 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001213 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001214 error_hangup(bcs);
1215 break;
1216 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001217 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001218 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001219 }
1220
1221 /* mark the write buffer area covered by this URB as free */
1222 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001223 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001224
1225 /* mark URB as free */
1226 spin_lock_irqsave(&ubc->isooutlock, flags);
1227 next = ubc->isooutfree;
1228 ubc->isooutfree = done;
1229 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1230 if (next) {
1231 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001232 rc = submit_iso_write_urb(next);
1233 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001234 /* couldn't submit */
1235 error_hangup(bcs);
1236 }
1237 }
1238 }
1239
1240 /* process queued SKBs */
1241 while ((skb = skb_dequeue(&bcs->squeue))) {
1242 /* copy to output buffer, doing L2 encapsulation */
1243 len = skb->len;
1244 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1245 /* insufficient buffer space, push back onto queue */
1246 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001247 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1248 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001249 break;
1250 }
1251 skb_pull(skb, len);
1252 gigaset_skb_sent(bcs, skb);
1253 dev_kfree_skb_any(skb);
1254 }
1255}
1256
1257/* Isochronous Read - Bottom Half */
1258/* ============================== */
1259
1260/* read_iso_tasklet
1261 * tasklet scheduled when an isochronous input URB from the Gigaset device
1262 * has completed
1263 * parameter:
1264 * data B channel state structure
1265 */
1266static void read_iso_tasklet(unsigned long data)
1267{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001268 struct bc_state *bcs = (struct bc_state *) data;
1269 struct bas_bc_state *ubc = bcs->hw.bas;
1270 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001271 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001272 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001273 char *rcvbuf;
1274 unsigned long flags;
1275 int totleft, numbytes, offset, frame, rc;
1276
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001277 /* loop while more completed URBs arrive in the meantime */
1278 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001279 /* retrieve URB */
1280 spin_lock_irqsave(&ubc->isoinlock, flags);
1281 if (!(urb = ubc->isoindone)) {
1282 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1283 return;
1284 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001285 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001286 ubc->isoindone = NULL;
1287 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001288 dev_warn(cs->dev,
1289 "isochronous read overrun, "
1290 "dropped URB with status: %s, %d bytes lost\n",
1291 get_usb_statmsg(ubc->loststatus),
1292 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001293 ubc->loststatus = -EINPROGRESS;
1294 }
1295 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1296
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001297 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001298 gig_dbg(DEBUG_ISO,
1299 "%s: channel not running, "
1300 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001301 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001302 return;
1303 }
1304
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001305 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001306 case 0: /* normal completion */
1307 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001308 case -EXDEV: /* inspect individual frames
1309 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001310 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1311 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001312 break;
1313 case -ENOENT:
1314 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001315 case -EINPROGRESS:
1316 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001317 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001318 continue; /* -> skip */
1319 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001320 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001321 error_hangup(bcs);
1322 continue; /* -> skip */
1323 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001324 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001325 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001326 goto error;
1327 }
1328
1329 rcvbuf = urb->transfer_buffer;
1330 totleft = urb->actual_length;
1331 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1332 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001333 dev_warn(cs->dev,
1334 "isochronous read: frame %d: %s\n",
1335 frame,
1336 get_usb_statmsg(
1337 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001338 break;
1339 }
1340 numbytes = urb->iso_frame_desc[frame].actual_length;
1341 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001342 dev_warn(cs->dev,
1343 "isochronous read: frame %d: "
1344 "numbytes (%d) > BAS_MAXFRAME\n",
1345 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001346 break;
1347 }
1348 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001349 dev_warn(cs->dev,
1350 "isochronous read: frame %d: "
1351 "numbytes (%d) > totleft (%d)\n",
1352 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001353 break;
1354 }
1355 offset = urb->iso_frame_desc[frame].offset;
1356 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001357 dev_warn(cs->dev,
1358 "isochronous read: frame %d: "
1359 "offset (%d) + numbytes (%d) "
1360 "> BAS_INBUFSIZE\n",
1361 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001362 break;
1363 }
1364 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1365 totleft -= numbytes;
1366 }
1367 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001368 dev_warn(cs->dev,
1369 "isochronous read: %d data bytes missing\n",
1370 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001371
1372 error:
1373 /* URB processed, resubmit */
1374 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1375 urb->iso_frame_desc[frame].status = 0;
1376 urb->iso_frame_desc[frame].actual_length = 0;
1377 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001378 /* urb->dev is clobbered by USB subsystem */
1379 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001380 urb->transfer_flags = URB_ISO_ASAP;
1381 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001382 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001383 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_err(cs->dev,
1385 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001386 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001387 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;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001404 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001405 int pending;
1406 unsigned long flags;
1407
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001408 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 */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001417 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001418 break;
1419
1420 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001421 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001422 error_reset(bcs->cs);
1423 break;
1424
1425 case HD_OPEN_B2CHANNEL:
1426 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001427 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1428 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001429 error_hangup(bcs);
1430 break;
1431
1432 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001433 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001434 break;
1435
1436 case HD_CLOSE_B2CHANNEL:
1437 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001438 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1439 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001440 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001441 break;
1442
1443 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001444 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1445 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001446 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001447
1448 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001449}
1450
1451/* write_ctrl_callback
1452 * USB completion handler for control pipe output
1453 * called by the USB subsystem in interrupt context
1454 * parameter:
1455 * urb USB request block of completed request
1456 * urb->context = hardware specific controller state structure
1457 */
David Howells7d12e782006-10-05 14:55:46 +01001458static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001459{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001460 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001461 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001462 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001463 unsigned long flags;
1464
Tilman Schmidt06163f82006-06-26 00:25:33 -07001465 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001466 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001467 case 0: /* normal completion */
1468 spin_lock_irqsave(&ucs->lock, flags);
1469 switch (ucs->pending) {
1470 case HD_DEVICE_INIT_ACK: /* no reply expected */
1471 del_timer(&ucs->timer_ctrl);
1472 ucs->pending = 0;
1473 break;
1474 }
1475 spin_unlock_irqrestore(&ucs->lock, flags);
1476 return;
1477
1478 case -ENOENT: /* cancelled */
1479 case -ECONNRESET: /* cancelled (async) */
1480 case -EINPROGRESS: /* pending */
1481 case -ENODEV: /* device removed */
1482 case -ESHUTDOWN: /* device shut down */
1483 /* ignore silently */
1484 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001485 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001486 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001487
1488 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001489 /* don't retry if suspend requested */
1490 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001491 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001492 dev_err(&ucs->interface->dev,
1493 "control request 0x%02x failed: %s\n",
1494 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001495 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001496 break; /* give up */
1497 }
1498 dev_notice(&ucs->interface->dev,
1499 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001500 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001501 ucs->retry_ctrl);
1502 /* urb->dev is clobbered by USB subsystem */
1503 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001504 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001505 if (unlikely(rc)) {
1506 dev_err(&ucs->interface->dev,
1507 "could not resubmit request 0x%02x: %s\n",
1508 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1509 break;
1510 }
1511 /* resubmitted */
1512 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001513 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001514
1515 /* failed, clear pending request */
1516 spin_lock_irqsave(&ucs->lock, flags);
1517 del_timer(&ucs->timer_ctrl);
1518 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001519 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001520 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001521}
1522
1523/* req_submit
1524 * submit a control output request without message buffer to the Gigaset base
1525 * and optionally start a timeout
1526 * parameters:
1527 * bcs B channel control structure
1528 * req control request code (HD_*)
1529 * val control request parameter value (set to 0 if unused)
1530 * timeout timeout in seconds (0: no timeout)
1531 * return value:
1532 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001533 * -EBUSY if another request is pending
1534 * any URB submission error code
1535 */
1536static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1537{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001538 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001539 int ret;
1540 unsigned long flags;
1541
Tilman Schmidt784d5852006-04-10 22:55:04 -07001542 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001543
1544 spin_lock_irqsave(&ucs->lock, flags);
1545 if (ucs->pending) {
1546 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001547 dev_err(bcs->cs->dev,
1548 "submission of request 0x%02x failed: "
1549 "request 0x%02x still pending\n",
1550 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001551 return -EBUSY;
1552 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001553
1554 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1555 ucs->dr_ctrl.bRequest = req;
1556 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1557 ucs->dr_ctrl.wIndex = 0;
1558 ucs->dr_ctrl.wLength = 0;
1559 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001560 usb_sndctrlpipe(ucs->udev, 0),
1561 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1562 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001563 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001564 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001565 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001566 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001567 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001568 spin_unlock_irqrestore(&ucs->lock, flags);
1569 return ret;
1570 }
1571 ucs->pending = req;
1572
1573 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001574 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001575 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1576 ucs->timer_ctrl.data = (unsigned long) bcs;
1577 ucs->timer_ctrl.function = req_timeout;
1578 add_timer(&ucs->timer_ctrl);
1579 }
1580
1581 spin_unlock_irqrestore(&ucs->lock, flags);
1582 return 0;
1583}
1584
1585/* gigaset_init_bchannel
1586 * called by common.c to connect a B channel
1587 * initialize isochronous I/O and tell the Gigaset base to open the channel
1588 * argument:
1589 * B channel control structure
1590 * return value:
1591 * 0 on success, error code < 0 on error
1592 */
1593static int gigaset_init_bchannel(struct bc_state *bcs)
1594{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001595 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001596 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001597 unsigned long flags;
1598
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001599 spin_lock_irqsave(&cs->lock, flags);
1600 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001601 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001602 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001603 return -ENODEV;
1604 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001605
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001606 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001607 dev_notice(cs->dev,
1608 "not starting isochronous I/O, "
1609 "suspend in progress\n");
1610 spin_unlock_irqrestore(&cs->lock, flags);
1611 return -EHOSTUNREACH;
1612 }
1613
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001614 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001615 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001616 "could not start isochronous I/O for channel B%d: %s\n",
1617 bcs->channel + 1,
1618 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1619 if (ret != -ENODEV)
1620 error_hangup(bcs);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001621 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001622 return ret;
1623 }
1624
1625 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1626 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001627 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001628 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001629 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001630 if (ret != -ENODEV)
1631 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001632 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001633
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001634 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001635 return ret;
1636}
1637
1638/* gigaset_close_bchannel
1639 * called by common.c to disconnect a B channel
1640 * tell the Gigaset base to close the channel
1641 * stopping isochronous I/O and LL notification will be done when the
1642 * acknowledgement for the close arrives
1643 * argument:
1644 * B channel control structure
1645 * return value:
1646 * 0 on success, error code < 0 on error
1647 */
1648static int gigaset_close_bchannel(struct bc_state *bcs)
1649{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001650 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001651 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001652 unsigned long flags;
1653
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001654 spin_lock_irqsave(&cs->lock, flags);
1655 if (unlikely(!cs->connected)) {
1656 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001657 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1658 return -ENODEV;
1659 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001660
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001661 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001662 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001663 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001664 gigaset_bchannel_down(bcs);
1665 return 0;
1666 }
1667
Tilman Schmidt73a88812006-04-22 02:35:30 -07001668 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001669 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1670 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001671 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001672 bcs->channel + 1);
1673
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001674 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001675 return ret;
1676}
1677
1678/* Device Operations */
1679/* ================= */
1680
1681/* complete_cb
1682 * unqueue first command buffer from queue, waking any sleepers
1683 * must be called with cs->cmdlock held
1684 * parameter:
1685 * cs controller state structure
1686 */
1687static void complete_cb(struct cardstate *cs)
1688{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001689 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001690
1691 /* unqueue completed buffer */
1692 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001693 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1694 "write_command: sent %u bytes, %u left",
1695 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001696 if ((cs->cmdbuf = cb->next) != NULL) {
1697 cs->cmdbuf->prev = NULL;
1698 cs->curlen = cs->cmdbuf->len;
1699 } else {
1700 cs->lastcmdbuf = NULL;
1701 cs->curlen = 0;
1702 }
1703
1704 if (cb->wake_tasklet)
1705 tasklet_schedule(cb->wake_tasklet);
1706
1707 kfree(cb);
1708}
1709
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001710/* write_command_callback
1711 * USB completion handler for AT command transmission
1712 * called by the USB subsystem in interrupt context
1713 * parameter:
1714 * urb USB request block of completed request
1715 * urb->context = controller state structure
1716 */
David Howells7d12e782006-10-05 14:55:46 +01001717static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001718{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001719 struct cardstate *cs = urb->context;
1720 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001721 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001722 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001723
Tilman Schmidt73a88812006-04-22 02:35:30 -07001724 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001725 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001726
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001727 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001728 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001729 case 0: /* normal completion */
1730 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001731 case -ENOENT: /* cancelled */
1732 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001733 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001734 case -ENODEV: /* device removed */
1735 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001736 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001737 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001738 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001739 return;
1740 default: /* any failure */
1741 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001742 dev_warn(cs->dev,
1743 "command write: %s, "
1744 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001745 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001746 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001747 break;
1748 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001749 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001750 dev_warn(cs->dev,
1751 "command write: %s, "
1752 "won't retry - suspend requested\n",
1753 get_usb_statmsg(status));
1754 break;
1755 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001756 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001757 dev_warn(cs->dev,
1758 "command write: %s, "
1759 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001760 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001761 break;
1762 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001763 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001764 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001765 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1766 /* resubmitted - bypass regular exit block */
1767 return;
1768 /* command send failed, assume base still waiting */
1769 update_basstate(ucs, BS_ATREADY, 0);
1770 }
1771
1772 spin_lock_irqsave(&cs->cmdlock, flags);
1773 if (cs->cmdbuf != NULL)
1774 complete_cb(cs);
1775 spin_unlock_irqrestore(&cs->cmdlock, flags);
1776}
1777
1778/* atrdy_timeout
1779 * timeout routine for AT command transmission
1780 * argument:
1781 * controller state structure
1782 */
1783static void atrdy_timeout(unsigned long data)
1784{
1785 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001786 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001787
Tilman Schmidt784d5852006-04-10 22:55:04 -07001788 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001789
1790 /* fake the missing signal - what else can I do? */
1791 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1792 start_cbsend(cs);
1793}
1794
1795/* atwrite_submit
1796 * submit an HD_WRITE_ATMESSAGE command URB
1797 * parameters:
1798 * cs controller state structure
1799 * buf buffer containing command to send
1800 * len length of command to send
1801 * return value:
1802 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001803 * -EBUSY if another request is pending
1804 * any URB submission error code
1805 */
1806static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1807{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001808 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001809 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001810
Tilman Schmidt784d5852006-04-10 22:55:04 -07001811 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001812
Tilman Schmidt73a88812006-04-22 02:35:30 -07001813 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001814 dev_err(cs->dev,
1815 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001816 return -EBUSY;
1817 }
1818
1819 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1820 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1821 ucs->dr_cmd_out.wValue = 0;
1822 ucs->dr_cmd_out.wIndex = 0;
1823 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1824 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1825 usb_sndctrlpipe(ucs->udev, 0),
1826 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1827 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001828 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001829 if (unlikely(rc)) {
1830 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001831 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001832 get_usb_rcmsg(rc));
1833 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001834 }
1835
Tilman Schmidt73a88812006-04-22 02:35:30 -07001836 /* submitted successfully, start timeout if necessary */
1837 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001838 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1839 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001840 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1841 ucs->timer_atrdy.data = (unsigned long) cs;
1842 ucs->timer_atrdy.function = atrdy_timeout;
1843 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001844 }
1845 return 0;
1846}
1847
1848/* start_cbsend
1849 * start transmission of AT command queue if necessary
1850 * parameter:
1851 * cs controller state structure
1852 * return value:
1853 * 0 on success
1854 * error code < 0 on error
1855 */
1856static int start_cbsend(struct cardstate *cs)
1857{
1858 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001859 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001860 unsigned long flags;
1861 int rc;
1862 int retval = 0;
1863
Tilman Schmidt024fd292008-02-06 01:38:26 -08001864 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001865 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001866 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1867 return -EHOSTUNREACH;
1868 }
1869
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001870 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001871 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001872 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001873 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1874 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001875 /* flush command queue */
1876 spin_lock_irqsave(&cs->cmdlock, flags);
1877 while (cs->cmdbuf != NULL)
1878 complete_cb(cs);
1879 spin_unlock_irqrestore(&cs->cmdlock, flags);
1880 }
1881 return rc;
1882 }
1883
1884 /* try to send first command in queue */
1885 spin_lock_irqsave(&cs->cmdlock, flags);
1886
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001887 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001888 ucs->retry_cmd_out = 0;
1889 rc = atwrite_submit(cs, cb->buf, cb->len);
1890 if (unlikely(rc)) {
1891 retval = rc;
1892 complete_cb(cs);
1893 }
1894 }
1895
1896 spin_unlock_irqrestore(&cs->cmdlock, flags);
1897 return retval;
1898}
1899
1900/* gigaset_write_cmd
1901 * This function is called by the device independent part of the driver
1902 * to transmit an AT command string to the Gigaset device.
1903 * It encapsulates the device specific method for transmission over the
1904 * direct USB connection to the base.
1905 * The command string is added to the queue of commands to send, and
1906 * USB transmission is started if necessary.
1907 * parameters:
1908 * cs controller state structure
1909 * buf command string to send
1910 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001911 * wake_tasklet tasklet to run when transmission is completed
1912 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001913 * return value:
1914 * number of bytes queued on success
1915 * error code < 0 on error
1916 */
1917static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001918 const unsigned char *buf, int len,
1919 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001920{
1921 struct cmdbuf_t *cb;
1922 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001923 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001924
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001925 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001926 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001927 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001928
Tilman Schmidt39f07222006-12-13 00:33:52 -08001929 if (len <= 0) {
1930 /* nothing to do */
1931 rc = 0;
1932 goto notqueued;
1933 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001934
1935 if (len > IF_WRITEBUF)
1936 len = IF_WRITEBUF;
1937 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001938 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001939 rc = -ENOMEM;
1940 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001941 }
1942
1943 memcpy(cb->buf, buf, len);
1944 cb->len = len;
1945 cb->offset = 0;
1946 cb->next = NULL;
1947 cb->wake_tasklet = wake_tasklet;
1948
1949 spin_lock_irqsave(&cs->cmdlock, flags);
1950 cb->prev = cs->lastcmdbuf;
1951 if (cs->lastcmdbuf)
1952 cs->lastcmdbuf->next = cb;
1953 else {
1954 cs->cmdbuf = cb;
1955 cs->curlen = len;
1956 }
1957 cs->cmdbytes += len;
1958 cs->lastcmdbuf = cb;
1959 spin_unlock_irqrestore(&cs->cmdlock, flags);
1960
Tilman Schmidt73a88812006-04-22 02:35:30 -07001961 spin_lock_irqsave(&cs->lock, flags);
1962 if (unlikely(!cs->connected)) {
1963 spin_unlock_irqrestore(&cs->lock, flags);
1964 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001965 /* flush command queue */
1966 spin_lock_irqsave(&cs->cmdlock, flags);
1967 while (cs->cmdbuf != NULL)
1968 complete_cb(cs);
1969 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001970 return -ENODEV;
1971 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001972 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001973 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001974 return rc < 0 ? rc : len;
1975
1976notqueued: /* request handled without queuing */
1977 if (wake_tasklet)
1978 tasklet_schedule(wake_tasklet);
1979 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001980}
1981
1982/* gigaset_write_room
1983 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001984 * return number of characters the driver will accept to be written via
1985 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001986 * parameter:
1987 * controller state structure
1988 * return value:
1989 * number of characters
1990 */
1991static int gigaset_write_room(struct cardstate *cs)
1992{
1993 return IF_WRITEBUF;
1994}
1995
1996/* gigaset_chars_in_buffer
1997 * tty_driver.chars_in_buffer interface routine
1998 * return number of characters waiting to be sent
1999 * parameter:
2000 * controller state structure
2001 * return value:
2002 * number of characters
2003 */
2004static int gigaset_chars_in_buffer(struct cardstate *cs)
2005{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002006 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002007}
2008
2009/* gigaset_brkchars
2010 * implementation of ioctl(GIGASET_BRKCHARS)
2011 * parameter:
2012 * controller state structure
2013 * return value:
2014 * -EINVAL (unimplemented function)
2015 */
2016static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2017{
2018 return -EINVAL;
2019}
2020
2021
2022/* Device Initialization/Shutdown */
2023/* ============================== */
2024
2025/* Free hardware dependent part of the B channel structure
2026 * parameter:
2027 * bcs B channel structure
2028 * return value:
2029 * !=0 on success
2030 */
2031static int gigaset_freebcshw(struct bc_state *bcs)
2032{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002033 struct bas_bc_state *ubc = bcs->hw.bas;
2034 int i;
2035
2036 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002037 return 0;
2038
Tilman Schmidt73a88812006-04-22 02:35:30 -07002039 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002040 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002041 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2042 for (i = 0; i < BAS_OUTURBS; ++i) {
2043 usb_kill_urb(ubc->isoouturbs[i].urb);
2044 usb_free_urb(ubc->isoouturbs[i].urb);
2045 }
2046 for (i = 0; i < BAS_INURBS; ++i) {
2047 usb_kill_urb(ubc->isoinurbs[i]);
2048 usb_free_urb(ubc->isoinurbs[i]);
2049 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002050 tasklet_kill(&ubc->sent_tasklet);
2051 tasklet_kill(&ubc->rcvd_tasklet);
2052 kfree(ubc->isooutbuf);
2053 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002054 bcs->hw.bas = NULL;
2055 return 1;
2056}
2057
2058/* Initialize hardware dependent part of the B channel structure
2059 * parameter:
2060 * bcs B channel structure
2061 * return value:
2062 * !=0 on success
2063 */
2064static int gigaset_initbcshw(struct bc_state *bcs)
2065{
2066 int i;
2067 struct bas_bc_state *ubc;
2068
2069 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2070 if (!ubc) {
2071 err("could not allocate bas_bc_state");
2072 return 0;
2073 }
2074
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002075 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002076 atomic_set(&ubc->corrbytes, 0);
2077 spin_lock_init(&ubc->isooutlock);
2078 for (i = 0; i < BAS_OUTURBS; ++i) {
2079 ubc->isoouturbs[i].urb = NULL;
2080 ubc->isoouturbs[i].bcs = bcs;
2081 }
2082 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2083 ubc->numsub = 0;
2084 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2085 err("could not allocate isochronous output buffer");
2086 kfree(ubc);
2087 bcs->hw.bas = NULL;
2088 return 0;
2089 }
2090 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002091 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002092
2093 spin_lock_init(&ubc->isoinlock);
2094 for (i = 0; i < BAS_INURBS; ++i)
2095 ubc->isoinurbs[i] = NULL;
2096 ubc->isoindone = NULL;
2097 ubc->loststatus = -EINPROGRESS;
2098 ubc->isoinlost = 0;
2099 ubc->seqlen = 0;
2100 ubc->inbyte = 0;
2101 ubc->inbits = 0;
2102 ubc->goodbytes = 0;
2103 ubc->alignerrs = 0;
2104 ubc->fcserrs = 0;
2105 ubc->frameerrs = 0;
2106 ubc->giants = 0;
2107 ubc->runts = 0;
2108 ubc->aborts = 0;
2109 ubc->shared0s = 0;
2110 ubc->stolen0s = 0;
2111 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002112 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002113 return 1;
2114}
2115
2116static void gigaset_reinitbcshw(struct bc_state *bcs)
2117{
2118 struct bas_bc_state *ubc = bcs->hw.bas;
2119
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002120 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002121 atomic_set(&bcs->hw.bas->corrbytes, 0);
2122 bcs->hw.bas->numsub = 0;
2123 spin_lock_init(&ubc->isooutlock);
2124 spin_lock_init(&ubc->isoinlock);
2125 ubc->loststatus = -EINPROGRESS;
2126}
2127
2128static void gigaset_freecshw(struct cardstate *cs)
2129{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002130 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002131 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002132 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002133}
2134
2135static int gigaset_initcshw(struct cardstate *cs)
2136{
2137 struct bas_cardstate *ucs;
2138
2139 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2140 if (!ucs)
2141 return 0;
2142
2143 ucs->urb_cmd_in = NULL;
2144 ucs->urb_cmd_out = NULL;
2145 ucs->rcvbuf = NULL;
2146 ucs->rcvbuf_size = 0;
2147
2148 spin_lock_init(&ucs->lock);
2149 ucs->pending = 0;
2150
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002151 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002152 init_timer(&ucs->timer_ctrl);
2153 init_timer(&ucs->timer_atrdy);
2154 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002155 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002156
2157 return 1;
2158}
2159
2160/* freeurbs
2161 * unlink and deallocate all URBs unconditionally
2162 * caller must make sure that no commands are still in progress
2163 * parameter:
2164 * cs controller state structure
2165 */
2166static void freeurbs(struct cardstate *cs)
2167{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002168 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002169 struct bas_bc_state *ubc;
2170 int i, j;
2171
Tilman Schmidt39f07222006-12-13 00:33:52 -08002172 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002173 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002174 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002175 for (i = 0; i < BAS_OUTURBS; ++i) {
2176 usb_kill_urb(ubc->isoouturbs[i].urb);
2177 usb_free_urb(ubc->isoouturbs[i].urb);
2178 ubc->isoouturbs[i].urb = NULL;
2179 }
2180 for (i = 0; i < BAS_INURBS; ++i) {
2181 usb_kill_urb(ubc->isoinurbs[i]);
2182 usb_free_urb(ubc->isoinurbs[i]);
2183 ubc->isoinurbs[i] = NULL;
2184 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002185 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002186 usb_kill_urb(ucs->urb_int_in);
2187 usb_free_urb(ucs->urb_int_in);
2188 ucs->urb_int_in = NULL;
2189 usb_kill_urb(ucs->urb_cmd_out);
2190 usb_free_urb(ucs->urb_cmd_out);
2191 ucs->urb_cmd_out = NULL;
2192 usb_kill_urb(ucs->urb_cmd_in);
2193 usb_free_urb(ucs->urb_cmd_in);
2194 ucs->urb_cmd_in = NULL;
2195 usb_kill_urb(ucs->urb_ctrl);
2196 usb_free_urb(ucs->urb_ctrl);
2197 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002198}
2199
2200/* gigaset_probe
2201 * This function is called when a new USB device is connected.
2202 * It checks whether the new device is handled by this driver.
2203 */
2204static int gigaset_probe(struct usb_interface *interface,
2205 const struct usb_device_id *id)
2206{
2207 struct usb_host_interface *hostif;
2208 struct usb_device *udev = interface_to_usbdev(interface);
2209 struct cardstate *cs = NULL;
2210 struct bas_cardstate *ucs = NULL;
2211 struct bas_bc_state *ubc;
2212 struct usb_endpoint_descriptor *endpoint;
2213 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002214 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002215
Tilman Schmidt784d5852006-04-10 22:55:04 -07002216 gig_dbg(DEBUG_ANY,
2217 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2218 __func__, le16_to_cpu(udev->descriptor.idVendor),
2219 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002220
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002221 /* set required alternate setting */
2222 hostif = interface->cur_altsetting;
2223 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002224 gig_dbg(DEBUG_ANY,
2225 "%s: wrong alternate setting %d - trying to switch",
2226 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002227 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002228 dev_warn(&udev->dev, "usb_set_interface failed, "
2229 "device %d interface %d altsetting %d\n",
2230 udev->devnum, hostif->desc.bInterfaceNumber,
2231 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002232 return -ENODEV;
2233 }
2234 hostif = interface->cur_altsetting;
2235 }
2236
2237 /* Reject application specific interfaces
2238 */
2239 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002240 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2241 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002242 return -ENODEV;
2243 }
2244
Tilman Schmidt784d5852006-04-10 22:55:04 -07002245 dev_info(&udev->dev,
2246 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2247 __func__, le16_to_cpu(udev->descriptor.idVendor),
2248 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002249
2250 cs = gigaset_getunassignedcs(driver);
2251 if (!cs) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002252 dev_err(&udev->dev, "no free cardstate\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002253 return -ENODEV;
2254 }
2255 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002256
2257 /* save off device structure ptrs for later use */
2258 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002259 ucs->udev = udev;
2260 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002261 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002262
2263 /* allocate URBs:
2264 * - one for the interrupt pipe
2265 * - three for the different uses of the default control pipe
2266 * - three for each isochronous pipe
2267 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002268 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2269 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2270 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2271 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002272 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002273
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002274 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002275 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002276 for (i = 0; i < BAS_OUTURBS; ++i)
2277 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002278 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002279 goto allocerr;
2280 for (i = 0; i < BAS_INURBS; ++i)
2281 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002282 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002283 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002284 }
2285
2286 ucs->rcvbuf = NULL;
2287 ucs->rcvbuf_size = 0;
2288
2289 /* Fill the interrupt urb and send it to the core */
2290 endpoint = &hostif->endpoint[0].desc;
2291 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002292 usb_rcvintpipe(udev,
2293 (endpoint->bEndpointAddress) & 0x0f),
2294 ucs->int_in_buf, 3, read_int_callback, cs,
2295 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002296 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002297 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002298 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002299 goto error;
2300 }
2301
2302 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002303 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002304 goto error;
2305
2306 /* tell common part that the device is ready */
2307 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002308 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002309
2310 /* save address of controller structure */
2311 usb_set_intfdata(interface, cs);
2312
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002313 if (!gigaset_start(cs))
2314 goto error;
2315
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002316 return 0;
2317
Tilman Schmidt73a88812006-04-22 02:35:30 -07002318allocerr:
2319 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002320error:
2321 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002322 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002323 gigaset_unassign(cs);
2324 return -ENODEV;
2325}
2326
2327/* gigaset_disconnect
2328 * This function is called when the Gigaset base is unplugged.
2329 */
2330static void gigaset_disconnect(struct usb_interface *interface)
2331{
2332 struct cardstate *cs;
2333 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002334 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002335
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002336 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002337
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002338 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002339
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002340 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002341
2342 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002343 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002344
2345 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002346 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002347 gigaset_bchannel_down(cs->bcs + j);
2348
2349 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002350 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002351
2352 /* stop timers and URBs, free ressources */
2353 del_timer_sync(&ucs->timer_ctrl);
2354 del_timer_sync(&ucs->timer_atrdy);
2355 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002356 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002357 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002358 kfree(ucs->rcvbuf);
2359 ucs->rcvbuf = NULL;
2360 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002361 usb_put_dev(ucs->udev);
2362 ucs->interface = NULL;
2363 ucs->udev = NULL;
2364 cs->dev = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002365 gigaset_unassign(cs);
2366}
2367
Tilman Schmidt024fd292008-02-06 01:38:26 -08002368/* gigaset_suspend
2369 * This function is called before the USB connection is suspended.
2370 */
2371static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2372{
2373 struct cardstate *cs = usb_get_intfdata(intf);
2374 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002375 int rc;
2376
2377 /* set suspend flag; this stops AT command/response traffic */
2378 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2379 gig_dbg(DEBUG_SUSPEND, "already suspended");
2380 return 0;
2381 }
2382
2383 /* wait a bit for blocking conditions to go away */
2384 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002385 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002386 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2387 BAS_TIMEOUT*HZ/10);
2388 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2389
2390 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002391 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002392 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002393 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002394 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002395 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002396 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002397 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002398 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002399 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002400 dev_warn(cs->dev, " sending AT command\n");
2401 update_basstate(ucs, 0, BS_SUSPEND);
2402 return -EBUSY;
2403 }
2404
2405 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002406 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002407 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2408 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2409 if (rc) {
2410 update_basstate(ucs, 0, BS_SUSPEND);
2411 return rc;
2412 }
2413 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2414 BAS_TIMEOUT*HZ/10);
2415 /* in case of timeout, proceed anyway */
2416 }
2417
2418 /* kill all URBs and timers that might still be pending */
2419 usb_kill_urb(ucs->urb_ctrl);
2420 usb_kill_urb(ucs->urb_int_in);
2421 del_timer_sync(&ucs->timer_ctrl);
2422
2423 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2424 return 0;
2425}
2426
2427/* gigaset_resume
2428 * This function is called after the USB connection has been resumed.
2429 */
2430static int gigaset_resume(struct usb_interface *intf)
2431{
2432 struct cardstate *cs = usb_get_intfdata(intf);
2433 struct bas_cardstate *ucs = cs->hw.bas;
2434 int rc;
2435
2436 /* resubmit interrupt URB for spontaneous messages from base */
2437 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2438 if (rc) {
2439 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2440 get_usb_rcmsg(rc));
2441 return rc;
2442 }
2443
2444 /* clear suspend flag to reallow activity */
2445 update_basstate(ucs, 0, BS_SUSPEND);
2446
2447 gig_dbg(DEBUG_SUSPEND, "resume complete");
2448 return 0;
2449}
2450
2451/* gigaset_pre_reset
2452 * This function is called before the USB connection is reset.
2453 */
2454static int gigaset_pre_reset(struct usb_interface *intf)
2455{
2456 /* handle just like suspend */
2457 return gigaset_suspend(intf, PMSG_ON);
2458}
2459
2460/* gigaset_post_reset
2461 * This function is called after the USB connection has been reset.
2462 */
2463static int gigaset_post_reset(struct usb_interface *intf)
2464{
2465 /* FIXME: send HD_DEVICE_INIT_ACK? */
2466
2467 /* resume operations */
2468 return gigaset_resume(intf);
2469}
2470
2471
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002472static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002473 gigaset_write_cmd,
2474 gigaset_write_room,
2475 gigaset_chars_in_buffer,
2476 gigaset_brkchars,
2477 gigaset_init_bchannel,
2478 gigaset_close_bchannel,
2479 gigaset_initbcshw,
2480 gigaset_freebcshw,
2481 gigaset_reinitbcshw,
2482 gigaset_initcshw,
2483 gigaset_freecshw,
2484 gigaset_set_modem_ctrl,
2485 gigaset_baud_rate,
2486 gigaset_set_line_ctrl,
2487 gigaset_isoc_send_skb,
2488 gigaset_isoc_input,
2489};
2490
2491/* bas_gigaset_init
2492 * This function is called after the kernel module is loaded.
2493 */
2494static int __init bas_gigaset_init(void)
2495{
2496 int result;
2497
2498 /* allocate memory for our driver state and intialize it */
2499 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002500 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002501 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002502 goto error;
2503
2504 /* allocate memory for our device state and intialize it */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002505 cardstate = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
Tilman Schmidt917f5082006-04-10 22:55:00 -07002506 GIGASET_MODULENAME);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002507 if (!cardstate)
2508 goto error;
2509
2510 /* register this driver with the USB subsystem */
2511 result = usb_register(&gigaset_usb_driver);
2512 if (result < 0) {
2513 err("usb_register failed (error %d)", -result);
2514 goto error;
2515 }
2516
2517 info(DRIVER_AUTHOR);
2518 info(DRIVER_DESC);
2519 return 0;
2520
2521error: if (cardstate)
2522 gigaset_freecs(cardstate);
2523 cardstate = NULL;
2524 if (driver)
2525 gigaset_freedriver(driver);
2526 driver = NULL;
2527 return -1;
2528}
2529
2530/* bas_gigaset_exit
2531 * This function is called before the kernel module is unloaded.
2532 */
2533static void __exit bas_gigaset_exit(void)
2534{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002535 struct bas_cardstate *ucs = cardstate->hw.bas;
2536
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002537 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002538 * => no gigaset_start any more
2539 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002540
2541 gigaset_shutdown(cardstate);
2542 /* from now on, no isdn callback should be possible */
2543
Tilman Schmidt73a88812006-04-22 02:35:30 -07002544 /* close all still open channels */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002545 if (ucs->basstate & BS_B1OPEN) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07002546 gig_dbg(DEBUG_INIT, "closing B1 channel");
2547 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2548 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ, 0, 0,
2549 NULL, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002550 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002551 if (ucs->basstate & BS_B2OPEN) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07002552 gig_dbg(DEBUG_INIT, "closing B2 channel");
2553 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2554 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ, 0, 0,
2555 NULL, 0, BAS_TIMEOUT);
2556 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002557 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07002558 gig_dbg(DEBUG_INIT, "closing AT channel");
2559 usb_control_msg(ucs->udev, usb_sndctrlpipe(ucs->udev, 0),
2560 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ, 0, 0,
2561 NULL, 0, BAS_TIMEOUT);
2562 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002563 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002564
2565 /* deregister this driver with the USB subsystem */
2566 usb_deregister(&gigaset_usb_driver);
2567 /* this will call the disconnect-callback */
2568 /* from now on, no disconnect/probe callback should be running */
2569
2570 gigaset_freecs(cardstate);
2571 cardstate = NULL;
2572 gigaset_freedriver(driver);
2573 driver = NULL;
2574}
2575
2576
2577module_init(bas_gigaset_init);
2578module_exit(bas_gigaset_exit);
2579
2580MODULE_AUTHOR(DRIVER_AUTHOR);
2581MODULE_DESCRIPTION(DRIVER_DESC);
2582MODULE_LICENSE("GPL");