blob: 3f11910c7ccdbf4c2edb31d360f3e40feba77d4b [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;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800137
138/* usb specific object needed to register this driver with the usb subsystem */
139static struct usb_driver gigaset_usb_driver = {
140 .name = GIGASET_MODULENAME,
141 .probe = gigaset_probe,
142 .disconnect = gigaset_disconnect,
143 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800144 .suspend = gigaset_suspend,
145 .resume = gigaset_resume,
146 .reset_resume = gigaset_post_reset,
147 .pre_reset = gigaset_pre_reset,
148 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800149};
150
Tilman Schmidt73a88812006-04-22 02:35:30 -0700151/* get message text for usb_submit_urb return code
152 */
153static char *get_usb_rcmsg(int rc)
154{
155 static char unkmsg[28];
156
157 switch (rc) {
158 case 0:
159 return "success";
160 case -ENOMEM:
161 return "out of memory";
162 case -ENODEV:
163 return "device not present";
164 case -ENOENT:
165 return "endpoint not present";
166 case -ENXIO:
167 return "URB type not supported";
168 case -EINVAL:
169 return "invalid argument";
170 case -EAGAIN:
171 return "start frame too early or too much scheduled";
172 case -EFBIG:
173 return "too many isochronous frames requested";
174 case -EPIPE:
175 return "endpoint stalled";
176 case -EMSGSIZE:
177 return "invalid packet size";
178 case -ENOSPC:
179 return "would overcommit USB bandwidth";
180 case -ESHUTDOWN:
181 return "device shut down";
182 case -EPERM:
183 return "reject flag set";
184 case -EHOSTUNREACH:
185 return "device suspended";
186 default:
187 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
188 return unkmsg;
189 }
190}
191
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800192/* get message text for USB status code
193 */
194static char *get_usb_statmsg(int status)
195{
196 static char unkmsg[28];
197
198 switch (status) {
199 case 0:
200 return "success";
201 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700202 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800203 case -EINPROGRESS:
204 return "pending";
205 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700206 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800207 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700208 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700209 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800210 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700211 case -EPIPE:
212 return "endpoint stalled";
213 case -ECOMM:
214 return "IN buffer overrun";
215 case -ENOSR:
216 return "OUT buffer underrun";
217 case -EOVERFLOW:
218 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800219 case -EREMOTEIO:
220 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700221 case -ENODEV:
222 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800223 case -EXDEV:
224 return "partial isochronous transfer";
225 case -EINVAL:
226 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700227 case -ECONNRESET:
228 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800229 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700230 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800231 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700232 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800233 return unkmsg;
234 }
235}
236
237/* usb_pipetype_str
238 * retrieve string representation of USB pipe type
239 */
240static inline char *usb_pipetype_str(int pipe)
241{
242 if (usb_pipeisoc(pipe))
243 return "Isoc";
244 if (usb_pipeint(pipe))
245 return "Int";
246 if (usb_pipecontrol(pipe))
247 return "Ctrl";
248 if (usb_pipebulk(pipe))
249 return "Bulk";
250 return "?";
251}
252
253/* dump_urb
254 * write content of URB to syslog for debugging
255 */
256static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700257 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800258{
259#ifdef CONFIG_GIGASET_DEBUG
260 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700261 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800262 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700263 gig_dbg(level,
264 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800265 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700266 (unsigned long) urb->dev,
267 usb_pipetype_str(urb->pipe),
268 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
269 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800270 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700271 urb->transfer_flags);
272 gig_dbg(level,
273 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800274 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700275 (unsigned long) urb->transfer_buffer,
276 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800277 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700278 gig_dbg(level,
279 " start_frame=%d, number_of_packets=%d, interval=%d, "
280 "error_count=%d,",
281 urb->start_frame, urb->number_of_packets, urb->interval,
282 urb->error_count);
283 gig_dbg(level,
284 " context=0x%08lx, complete=0x%08lx, "
285 "iso_frame_desc[]={",
286 (unsigned long) urb->context,
287 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800288 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700289 struct usb_iso_packet_descriptor *pifd
290 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700291 gig_dbg(level,
292 " {offset=%u, length=%u, actual_length=%u, "
293 "status=%u}",
294 pifd->offset, pifd->length, pifd->actual_length,
295 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800296 }
297 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700298 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800299#endif
300}
301
302/* read/set modem control bits etc. (m10x only) */
303static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700304 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800305{
306 return -EINVAL;
307}
308
309static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
310{
311 return -EINVAL;
312}
313
314static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
315{
316 return -EINVAL;
317}
318
319/* error_hangup
320 * hang up any existing connection because of an unrecoverable error
321 * This function may be called from any context and takes care of scheduling
322 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700323 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800324 * argument:
325 * B channel control structure
326 */
327static inline void error_hangup(struct bc_state *bcs)
328{
329 struct cardstate *cs = bcs->cs;
330
Tilman Schmidt784d5852006-04-10 22:55:04 -0700331 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
332 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800333
Tilman Schmidt73a88812006-04-22 02:35:30 -0700334 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
335 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800336
337 gigaset_schedule_event(cs);
338}
339
340/* error_reset
341 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700342 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700343 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700344 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800345 * argument:
346 * controller state structure
347 */
348static inline void error_reset(struct cardstate *cs)
349{
Tilman Schmidt06163f82006-06-26 00:25:33 -0700350 /* close AT command channel to recover (ignore errors) */
351 req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
352
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800353 //FIXME try to recover without bothering the user
Tilman Schmidt784d5852006-04-10 22:55:04 -0700354 dev_err(cs->dev,
355 "unrecoverable error - please disconnect Gigaset base to reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800356}
357
358/* check_pending
359 * check for completion of pending control request
360 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700361 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800362 */
363static void check_pending(struct bas_cardstate *ucs)
364{
365 unsigned long flags;
366
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800367 spin_lock_irqsave(&ucs->lock, flags);
368 switch (ucs->pending) {
369 case 0:
370 break;
371 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800372 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800373 ucs->pending = 0;
374 break;
375 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800376 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800377 ucs->pending = 0;
378 break;
379 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800380 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800381 ucs->pending = 0;
382 break;
383 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800384 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800385 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800386 break;
387 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800388 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800389 ucs->pending = 0;
390 break;
391 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800392 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800393 ucs->pending = 0;
394 break;
395 case HD_DEVICE_INIT_ACK: /* no reply expected */
396 ucs->pending = 0;
397 break;
398 /* HD_READ_ATMESSAGE, HD_WRITE_ATMESSAGE, HD_RESET_INTERRUPTPIPE
399 * are handled separately and should never end up here
400 */
401 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700402 dev_warn(&ucs->interface->dev,
403 "unknown pending request 0x%02x cleared\n",
404 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800405 ucs->pending = 0;
406 }
407
408 if (!ucs->pending)
409 del_timer(&ucs->timer_ctrl);
410
411 spin_unlock_irqrestore(&ucs->lock, flags);
412}
413
414/* cmd_in_timeout
415 * timeout routine for command input request
416 * argument:
417 * controller state structure
418 */
419static void cmd_in_timeout(unsigned long data)
420{
421 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700422 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700423 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800424
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800425 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700426 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800427 return;
428 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800429
Tilman Schmidt06163f82006-06-26 00:25:33 -0700430 if (ucs->retry_cmd_in++ < BAS_RETRY) {
431 dev_notice(cs->dev, "control read: timeout, retry %d\n",
432 ucs->retry_cmd_in);
433 rc = atread_submit(cs, BAS_TIMEOUT);
434 if (rc >= 0 || rc == -ENODEV)
435 /* resubmitted or disconnected */
436 /* - bypass regular exit block */
437 return;
438 } else {
439 dev_err(cs->dev,
440 "control read: timeout, giving up after %d tries\n",
441 ucs->retry_cmd_in);
442 }
443 kfree(ucs->rcvbuf);
444 ucs->rcvbuf = NULL;
445 ucs->rcvbuf_size = 0;
446 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800447}
448
Tilman Schmidt73a88812006-04-22 02:35:30 -0700449/* set/clear bits in base connection state, return previous state
450 */
451inline static int update_basstate(struct bas_cardstate *ucs,
452 int set, int clear)
453{
454 unsigned long flags;
455 int state;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800456
Tilman Schmidt73a88812006-04-22 02:35:30 -0700457 spin_lock_irqsave(&ucs->lock, flags);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800458 state = ucs->basstate;
459 ucs->basstate = (state & ~clear) | set;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700460 spin_unlock_irqrestore(&ucs->lock, flags);
461 return state;
462}
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800463
Tilman Schmidt06163f82006-06-26 00:25:33 -0700464/* read_ctrl_callback
465 * USB completion handler for control pipe input
466 * called by the USB subsystem in interrupt context
467 * parameter:
468 * urb USB request block
469 * urb->context = inbuf structure for controller state
470 */
David Howells7d12e782006-10-05 14:55:46 +0100471static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700472{
473 struct inbuf_t *inbuf = urb->context;
474 struct cardstate *cs = inbuf->cs;
475 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800476 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700477 int have_data = 0;
478 unsigned numbytes;
479 int rc;
480
481 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800482 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700483
484 if (!ucs->rcvbuf_size) {
485 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
486 return;
487 }
488
489 del_timer(&ucs->timer_cmd_in);
490
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800491 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700492 case 0: /* normal completion */
493 numbytes = urb->actual_length;
494 if (unlikely(numbytes != ucs->rcvbuf_size)) {
495 dev_warn(cs->dev,
496 "control read: received %d chars, expected %d\n",
497 numbytes, ucs->rcvbuf_size);
498 if (numbytes > ucs->rcvbuf_size)
499 numbytes = ucs->rcvbuf_size;
500 }
501
502 /* copy received bytes to inbuf */
503 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
504
505 if (unlikely(numbytes < ucs->rcvbuf_size)) {
506 /* incomplete - resubmit for remaining bytes */
507 ucs->rcvbuf_size -= numbytes;
508 ucs->retry_cmd_in = 0;
509 rc = atread_submit(cs, BAS_TIMEOUT);
510 if (rc >= 0 || rc == -ENODEV)
511 /* resubmitted or disconnected */
512 /* - bypass regular exit block */
513 return;
514 error_reset(cs);
515 }
516 break;
517
518 case -ENOENT: /* cancelled */
519 case -ECONNRESET: /* cancelled (async) */
520 case -EINPROGRESS: /* pending */
521 case -ENODEV: /* device removed */
522 case -ESHUTDOWN: /* device shut down */
523 /* no action necessary */
524 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800525 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700526 break;
527
528 default: /* severe trouble */
529 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800530 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700531 if (ucs->retry_cmd_in++ < BAS_RETRY) {
532 dev_notice(cs->dev, "control read: retry %d\n",
533 ucs->retry_cmd_in);
534 rc = atread_submit(cs, BAS_TIMEOUT);
535 if (rc >= 0 || rc == -ENODEV)
536 /* resubmitted or disconnected */
537 /* - bypass regular exit block */
538 return;
539 } else {
540 dev_err(cs->dev,
541 "control read: giving up after %d tries\n",
542 ucs->retry_cmd_in);
543 }
544 error_reset(cs);
545 }
546
547 kfree(ucs->rcvbuf);
548 ucs->rcvbuf = NULL;
549 ucs->rcvbuf_size = 0;
550 if (have_data) {
551 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
552 gigaset_schedule_event(cs);
553 }
554}
555
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800556/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700557 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800558 * parameters:
559 * cs controller state structure
560 * timeout timeout in 1/10 sec., 0: none
561 * return value:
562 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800563 * -EBUSY if another request is pending
564 * any URB submission error code
565 */
566static int atread_submit(struct cardstate *cs, int timeout)
567{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700568 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800569 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800570 int ret;
571
Tilman Schmidt784d5852006-04-10 22:55:04 -0700572 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
573 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800574
Tilman Schmidt024fd292008-02-06 01:38:26 -0800575 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
576 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700577 dev_err(cs->dev,
578 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800579 return -EBUSY;
580 }
581
Tilman Schmidt024fd292008-02-06 01:38:26 -0800582 if (basstate & BS_SUSPEND) {
583 dev_notice(cs->dev,
584 "HD_READ_ATMESSAGE not submitted, "
585 "suspend in progress\n");
586 update_basstate(ucs, 0, BS_ATRDPEND);
587 /* treat like disconnect */
588 return -ENODEV;
589 }
590
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800591 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
592 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
593 ucs->dr_cmd_in.wValue = 0;
594 ucs->dr_cmd_in.wIndex = 0;
595 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
596 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700597 usb_rcvctrlpipe(ucs->udev, 0),
598 (unsigned char*) & ucs->dr_cmd_in,
599 ucs->rcvbuf, ucs->rcvbuf_size,
600 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800601
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800602 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700603 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700604 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700605 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800606 return ret;
607 }
608
609 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700610 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800611 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
612 ucs->timer_cmd_in.data = (unsigned long) cs;
613 ucs->timer_cmd_in.function = cmd_in_timeout;
614 add_timer(&ucs->timer_cmd_in);
615 }
616 return 0;
617}
618
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800619/* read_int_callback
620 * USB completion handler for interrupt pipe input
621 * called by the USB subsystem in interrupt context
622 * parameter:
623 * urb USB request block
624 * urb->context = controller state structure
625 */
David Howells7d12e782006-10-05 14:55:46 +0100626static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800627{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700628 struct cardstate *cs = urb->context;
629 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800630 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800631 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800632 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700633 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800634 unsigned l;
635 int channel;
636
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800637 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800638 case 0: /* success */
639 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700640 case -ENOENT: /* cancelled */
641 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800642 case -EINPROGRESS: /* pending */
643 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700644 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800645 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800646 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700647 case -ENODEV: /* device removed */
648 case -ESHUTDOWN: /* device shut down */
649 //FIXME use this as disconnect indicator?
650 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
651 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800652 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700653 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800654 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800655 //FIXME corrective action? resubmission always ok?
656 goto resubmit;
657 }
658
Tilman Schmidt73a88812006-04-22 02:35:30 -0700659 /* drop incomplete packets even if the missing bytes wouldn't matter */
660 if (unlikely(urb->actual_length < 3)) {
661 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
662 urb->actual_length);
663 goto resubmit;
664 }
665
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800666 l = (unsigned) ucs->int_in_buf[1] +
667 (((unsigned) ucs->int_in_buf[2]) << 8);
668
Tilman Schmidt784d5852006-04-10 22:55:04 -0700669 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
670 urb->actual_length, (int)ucs->int_in_buf[0], l,
671 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800672
673 channel = 0;
674
675 switch (ucs->int_in_buf[0]) {
676 case HD_DEVICE_INIT_OK:
677 update_basstate(ucs, BS_INIT, 0);
678 break;
679
680 case HD_READY_SEND_ATDATA:
681 del_timer(&ucs->timer_atrdy);
682 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
683 start_cbsend(cs);
684 break;
685
686 case HD_OPEN_B2CHANNEL_ACK:
687 ++channel;
688 case HD_OPEN_B1CHANNEL_ACK:
689 bcs = cs->bcs + channel;
690 update_basstate(ucs, BS_B1OPEN << channel, 0);
691 gigaset_bchannel_up(bcs);
692 break;
693
694 case HD_OPEN_ATCHANNEL_ACK:
695 update_basstate(ucs, BS_ATOPEN, 0);
696 start_cbsend(cs);
697 break;
698
699 case HD_CLOSE_B2CHANNEL_ACK:
700 ++channel;
701 case HD_CLOSE_B1CHANNEL_ACK:
702 bcs = cs->bcs + channel;
703 update_basstate(ucs, 0, BS_B1OPEN << channel);
704 stopurbs(bcs->hw.bas);
705 gigaset_bchannel_down(bcs);
706 break;
707
708 case HD_CLOSE_ATCHANNEL_ACK:
709 update_basstate(ucs, 0, BS_ATOPEN);
710 break;
711
712 case HD_B2_FLOW_CONTROL:
713 ++channel;
714 case HD_B1_FLOW_CONTROL:
715 bcs = cs->bcs + channel;
716 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700717 &bcs->hw.bas->corrbytes);
718 gig_dbg(DEBUG_ISO,
719 "Flow control (channel %d, sub %d): 0x%02x => %d",
720 channel, bcs->hw.bas->numsub, l,
721 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800722 break;
723
724 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
725 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700726 dev_warn(cs->dev,
727 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800728 break;
729 }
730 spin_lock_irqsave(&cs->lock, flags);
731 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700732 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700733 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700734 "receive AT data overrun, %d bytes lost\n",
735 ucs->rcvbuf_size);
736 kfree(ucs->rcvbuf);
737 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800738 }
739 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
740 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700741 dev_err(cs->dev, "out of memory receiving AT data\n");
742 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800743 break;
744 }
745 ucs->rcvbuf_size = l;
746 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700747 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800748 kfree(ucs->rcvbuf);
749 ucs->rcvbuf = NULL;
750 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700751 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700752 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700753 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700754 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700755 break;
756 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800757 }
758 spin_unlock_irqrestore(&cs->lock, flags);
759 break;
760
761 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700762 gig_dbg(DEBUG_USBREQ, "HD_RESET_INTERRUPT_PIPE_ACK");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800763 break;
764
765 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700766 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800767 break;
768
769 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700770 dev_warn(cs->dev,
771 "unknown Gigaset signal 0x%02x (%u) ignored\n",
772 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800773 }
774
775 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800776 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800777
778resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800779 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700780 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700781 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700782 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800783 error_reset(cs);
784 }
785}
786
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800787/* read_iso_callback
788 * USB completion handler for B channel isochronous input
789 * called by the USB subsystem in interrupt context
790 * parameter:
791 * urb USB request block of completed request
792 * urb->context = bc_state structure
793 */
David Howells7d12e782006-10-05 14:55:46 +0100794static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800795{
796 struct bc_state *bcs;
797 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800798 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800799 unsigned long flags;
800 int i, rc;
801
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800802 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800803 if (unlikely(status == -ENOENT ||
804 status == -ECONNRESET ||
805 status == -EINPROGRESS ||
806 status == -ENODEV ||
807 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700808 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800809 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800810 return;
811 }
812
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700813 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800814 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800815
816 spin_lock_irqsave(&ubc->isoinlock, flags);
817 if (likely(ubc->isoindone == NULL)) {
818 /* pass URB to tasklet */
819 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800820 ubc->isoinstatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800821 tasklet_schedule(&ubc->rcvd_tasklet);
822 } else {
823 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800824 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800825 for (i = 0; i < BAS_NUMFRAMES; i++) {
826 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
827 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700828 urb->iso_frame_desc[i].status !=
829 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800830 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800831 urb->iso_frame_desc[i].status = 0;
832 urb->iso_frame_desc[i].actual_length = 0;
833 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800834 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700835 /* urb->dev is clobbered by USB subsystem */
836 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800837 urb->transfer_flags = URB_ISO_ASAP;
838 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700839 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
840 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800841 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700842 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700843 dev_err(bcs->cs->dev,
844 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700845 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800846 dump_urb(DEBUG_ISO, "isoc read", urb);
847 error_hangup(bcs);
848 }
849 }
850 }
851 spin_unlock_irqrestore(&ubc->isoinlock, flags);
852}
853
854/* write_iso_callback
855 * USB completion handler for B channel isochronous output
856 * called by the USB subsystem in interrupt context
857 * parameter:
858 * urb USB request block of completed request
859 * urb->context = isow_urbctx_t structure
860 */
David Howells7d12e782006-10-05 14:55:46 +0100861static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800862{
863 struct isow_urbctx_t *ucx;
864 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800865 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800866 unsigned long flags;
867
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800868 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800869 if (unlikely(status == -ENOENT ||
870 status == -ECONNRESET ||
871 status == -EINPROGRESS ||
872 status == -ENODEV ||
873 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700874 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800875 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800876 return;
877 }
878
879 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700880 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800881 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800882 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883
884 spin_lock_irqsave(&ubc->isooutlock, flags);
885 ubc->isooutovfl = ubc->isooutdone;
886 ubc->isooutdone = ucx;
887 spin_unlock_irqrestore(&ubc->isooutlock, flags);
888 tasklet_schedule(&ubc->sent_tasklet);
889}
890
891/* starturbs
892 * prepare and submit USB request blocks for isochronous input and output
893 * argument:
894 * B channel control structure
895 * return value:
896 * 0 on success
897 * < 0 on error (no URBs submitted)
898 */
899static int starturbs(struct bc_state *bcs)
900{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700901 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800902 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800903 int j, k;
904 int rc;
905
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800906 /* initialize L2 reception */
907 if (bcs->proto2 == ISDN_PROTO_L2_HDLC)
908 bcs->inputstate |= INS_flag_hunt;
909
910 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800911 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800912 for (k = 0; k < BAS_INURBS; k++) {
913 urb = ubc->isoinurbs[k];
914 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800915 rc = -EFAULT;
916 goto error;
917 }
918
919 urb->dev = bcs->cs->hw.bas->udev;
920 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
921 urb->transfer_flags = URB_ISO_ASAP;
922 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
923 urb->transfer_buffer_length = BAS_INBUFSIZE;
924 urb->number_of_packets = BAS_NUMFRAMES;
925 urb->interval = BAS_FRAMETIME;
926 urb->complete = read_iso_callback;
927 urb->context = bcs;
928 for (j = 0; j < BAS_NUMFRAMES; j++) {
929 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
930 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
931 urb->iso_frame_desc[j].status = 0;
932 urb->iso_frame_desc[j].actual_length = 0;
933 }
934
935 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800936 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800937 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800938 }
939
940 /* initialize L2 transmission */
941 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
942
943 /* set up isochronous output URBs for flag idling */
944 for (k = 0; k < BAS_OUTURBS; ++k) {
945 urb = ubc->isoouturbs[k].urb;
946 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800947 rc = -EFAULT;
948 goto error;
949 }
950 urb->dev = bcs->cs->hw.bas->udev;
951 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
952 urb->transfer_flags = URB_ISO_ASAP;
953 urb->transfer_buffer = ubc->isooutbuf->data;
954 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
955 urb->number_of_packets = BAS_NUMFRAMES;
956 urb->interval = BAS_FRAMETIME;
957 urb->complete = write_iso_callback;
958 urb->context = &ubc->isoouturbs[k];
959 for (j = 0; j < BAS_NUMFRAMES; ++j) {
960 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
961 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
962 urb->iso_frame_desc[j].status = 0;
963 urb->iso_frame_desc[j].actual_length = 0;
964 }
965 ubc->isoouturbs[k].limit = -1;
966 }
967
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800968 /* keep one URB free, submit the others */
969 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800970 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800971 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700972 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800973 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800974 }
975 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800976 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800977 ubc->isooutdone = ubc->isooutovfl = NULL;
978 return 0;
979 error:
980 stopurbs(ubc);
981 return rc;
982}
983
984/* stopurbs
985 * cancel the USB request blocks for isochronous input and output
986 * errors are silently ignored
987 * argument:
988 * B channel control structure
989 */
990static void stopurbs(struct bas_bc_state *ubc)
991{
992 int k, rc;
993
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800994 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800995
996 for (k = 0; k < BAS_INURBS; ++k) {
997 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700998 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700999 "%s: isoc input URB %d unlinked, result = %s",
1000 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001001 }
1002
1003 for (k = 0; k < BAS_OUTURBS; ++k) {
1004 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001005 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001006 "%s: isoc output URB %d unlinked, result = %s",
1007 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001008 }
1009}
1010
1011/* Isochronous Write - Bottom Half */
1012/* =============================== */
1013
1014/* submit_iso_write_urb
1015 * fill and submit the next isochronous write URB
1016 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001017 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001018 * return value:
1019 * number of frames submitted in URB
1020 * 0 if URB not submitted because no data available (isooutbuf busy)
1021 * error code < 0 on error
1022 */
1023static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1024{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001025 struct urb *urb = ucx->urb;
1026 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001027 struct usb_iso_packet_descriptor *ifd;
1028 int corrbytes, nframe, rc;
1029
Tilman Schmidt784d5852006-04-10 22:55:04 -07001030 /* urb->dev is clobbered by USB subsystem */
1031 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001032 urb->transfer_flags = URB_ISO_ASAP;
1033 urb->transfer_buffer = ubc->isooutbuf->data;
1034 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1035
1036 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1037 ifd = &urb->iso_frame_desc[nframe];
1038
1039 /* compute frame length according to flow control */
1040 ifd->length = BAS_NORMFRAME;
1041 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001042 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1043 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001044 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1045 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1046 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1047 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1048 ifd->length += corrbytes;
1049 atomic_add(-corrbytes, &ubc->corrbytes);
1050 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001051
1052 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001053 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1054 if (rc < 0) {
1055 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001056 gig_dbg(DEBUG_ISO,
1057 "%s: buffer busy at frame %d",
1058 __func__, nframe);
1059 /* tasklet will be restarted from
1060 gigaset_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001061 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001062 dev_err(ucx->bcs->cs->dev,
1063 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001064 __func__, rc, nframe);
1065 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001066 }
1067 break;
1068 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001069 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001070 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001071 ifd->status = 0;
1072 ifd->actual_length = 0;
1073 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001074 if (unlikely(nframe == 0))
1075 return 0; /* no data to send */
1076 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001077
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001078 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001079 if (unlikely(rc)) {
1080 if (rc == -ENODEV)
1081 /* device removed - give up silently */
1082 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1083 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001084 dev_err(ucx->bcs->cs->dev,
1085 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001086 get_usb_rcmsg(rc));
1087 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001088 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001089 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001090 return nframe;
1091}
1092
1093/* write_iso_tasklet
1094 * tasklet scheduled when an isochronous output URB from the Gigaset device
1095 * has completed
1096 * parameter:
1097 * data B channel state structure
1098 */
1099static void write_iso_tasklet(unsigned long data)
1100{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001101 struct bc_state *bcs = (struct bc_state *) data;
1102 struct bas_bc_state *ubc = bcs->hw.bas;
1103 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001104 struct isow_urbctx_t *done, *next, *ovfl;
1105 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001106 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001107 struct usb_iso_packet_descriptor *ifd;
1108 int offset;
1109 unsigned long flags;
1110 int i;
1111 struct sk_buff *skb;
1112 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001113 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001114
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001115 /* loop while completed URBs arrive in time */
1116 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001117 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001118 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001119 return;
1120 }
1121
1122 /* retrieve completed URBs */
1123 spin_lock_irqsave(&ubc->isooutlock, flags);
1124 done = ubc->isooutdone;
1125 ubc->isooutdone = NULL;
1126 ovfl = ubc->isooutovfl;
1127 ubc->isooutovfl = NULL;
1128 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1129 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001130 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001131 error_hangup(bcs);
1132 break;
1133 }
1134 if (!done)
1135 break;
1136
1137 /* submit free URB if available */
1138 spin_lock_irqsave(&ubc->isooutlock, flags);
1139 next = ubc->isooutfree;
1140 ubc->isooutfree = NULL;
1141 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1142 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001143 rc = submit_iso_write_urb(next);
1144 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001145 /* could not submit URB, put it back */
1146 spin_lock_irqsave(&ubc->isooutlock, flags);
1147 if (ubc->isooutfree == NULL) {
1148 ubc->isooutfree = next;
1149 next = NULL;
1150 }
1151 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1152 if (next) {
1153 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001154 dev_err(cs->dev,
1155 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001156 error_hangup(bcs);
1157 }
1158 }
1159 }
1160
1161 /* process completed URB */
1162 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001163 status = done->status;
1164 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001165 case -EXDEV: /* partial completion */
1166 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1167 __func__);
1168 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001169 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001170 /* inspect individual frames
1171 * assumptions (for lack of documentation):
1172 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001173 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001174 * - all following frames are not sent at all
1175 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001176 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001177 for (i = 0; i < BAS_NUMFRAMES; i++) {
1178 ifd = &urb->iso_frame_desc[i];
1179 if (ifd->status ||
1180 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001181 dev_warn(cs->dev,
1182 "isochronous write: frame %d: %s, "
1183 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001184 i, get_usb_statmsg(ifd->status),
1185 ifd->actual_length, ifd->length);
1186 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001187 ifd->actual_length)
1188 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001189 break;
1190 }
1191 }
1192#ifdef CONFIG_GIGASET_DEBUG
1193 /* check assumption on remaining frames */
1194 for (; i < BAS_NUMFRAMES; i++) {
1195 ifd = &urb->iso_frame_desc[i];
1196 if (ifd->status != -EINPROGRESS
1197 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001198 dev_warn(cs->dev,
1199 "isochronous write: frame %d: %s, "
1200 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001201 i, get_usb_statmsg(ifd->status),
1202 ifd->actual_length, ifd->length);
1203 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001204 ifd->actual_length)
1205 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001206 break;
1207 }
1208 }
1209#endif
1210 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001211 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001212 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001213 error_hangup(bcs);
1214 break;
1215 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001216 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001217 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001218 }
1219
1220 /* mark the write buffer area covered by this URB as free */
1221 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001222 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001223
1224 /* mark URB as free */
1225 spin_lock_irqsave(&ubc->isooutlock, flags);
1226 next = ubc->isooutfree;
1227 ubc->isooutfree = done;
1228 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1229 if (next) {
1230 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001231 rc = submit_iso_write_urb(next);
1232 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001233 /* couldn't submit */
1234 error_hangup(bcs);
1235 }
1236 }
1237 }
1238
1239 /* process queued SKBs */
1240 while ((skb = skb_dequeue(&bcs->squeue))) {
1241 /* copy to output buffer, doing L2 encapsulation */
1242 len = skb->len;
1243 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1244 /* insufficient buffer space, push back onto queue */
1245 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001246 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1247 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001248 break;
1249 }
1250 skb_pull(skb, len);
1251 gigaset_skb_sent(bcs, skb);
1252 dev_kfree_skb_any(skb);
1253 }
1254}
1255
1256/* Isochronous Read - Bottom Half */
1257/* ============================== */
1258
1259/* read_iso_tasklet
1260 * tasklet scheduled when an isochronous input URB from the Gigaset device
1261 * has completed
1262 * parameter:
1263 * data B channel state structure
1264 */
1265static void read_iso_tasklet(unsigned long data)
1266{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001267 struct bc_state *bcs = (struct bc_state *) data;
1268 struct bas_bc_state *ubc = bcs->hw.bas;
1269 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001270 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001271 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001272 char *rcvbuf;
1273 unsigned long flags;
1274 int totleft, numbytes, offset, frame, rc;
1275
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001276 /* loop while more completed URBs arrive in the meantime */
1277 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001278 /* retrieve URB */
1279 spin_lock_irqsave(&ubc->isoinlock, flags);
1280 if (!(urb = ubc->isoindone)) {
1281 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1282 return;
1283 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001284 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001285 ubc->isoindone = NULL;
1286 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001287 dev_warn(cs->dev,
1288 "isochronous read overrun, "
1289 "dropped URB with status: %s, %d bytes lost\n",
1290 get_usb_statmsg(ubc->loststatus),
1291 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001292 ubc->loststatus = -EINPROGRESS;
1293 }
1294 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1295
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001296 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001297 gig_dbg(DEBUG_ISO,
1298 "%s: channel not running, "
1299 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001300 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001301 return;
1302 }
1303
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001304 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001305 case 0: /* normal completion */
1306 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001307 case -EXDEV: /* inspect individual frames
1308 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001309 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1310 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001311 break;
1312 case -ENOENT:
1313 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001314 case -EINPROGRESS:
1315 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001316 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001317 continue; /* -> skip */
1318 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001319 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001320 error_hangup(bcs);
1321 continue; /* -> skip */
1322 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001323 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001324 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001325 goto error;
1326 }
1327
1328 rcvbuf = urb->transfer_buffer;
1329 totleft = urb->actual_length;
1330 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
1331 if (unlikely(urb->iso_frame_desc[frame].status)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001332 dev_warn(cs->dev,
1333 "isochronous read: frame %d: %s\n",
1334 frame,
1335 get_usb_statmsg(
1336 urb->iso_frame_desc[frame].status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001337 break;
1338 }
1339 numbytes = urb->iso_frame_desc[frame].actual_length;
1340 if (unlikely(numbytes > BAS_MAXFRAME)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001341 dev_warn(cs->dev,
1342 "isochronous read: frame %d: "
1343 "numbytes (%d) > BAS_MAXFRAME\n",
1344 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001345 break;
1346 }
1347 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001348 dev_warn(cs->dev,
1349 "isochronous read: frame %d: "
1350 "numbytes (%d) > totleft (%d)\n",
1351 frame, numbytes, totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001352 break;
1353 }
1354 offset = urb->iso_frame_desc[frame].offset;
1355 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001356 dev_warn(cs->dev,
1357 "isochronous read: frame %d: "
1358 "offset (%d) + numbytes (%d) "
1359 "> BAS_INBUFSIZE\n",
1360 frame, offset, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001361 break;
1362 }
1363 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1364 totleft -= numbytes;
1365 }
1366 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001367 dev_warn(cs->dev,
1368 "isochronous read: %d data bytes missing\n",
1369 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001370
1371 error:
1372 /* URB processed, resubmit */
1373 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1374 urb->iso_frame_desc[frame].status = 0;
1375 urb->iso_frame_desc[frame].actual_length = 0;
1376 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001377 /* urb->dev is clobbered by USB subsystem */
1378 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001379 urb->transfer_flags = URB_ISO_ASAP;
1380 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001381 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001382 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001383 dev_err(cs->dev,
1384 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001385 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001386 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1387 error_hangup(bcs);
1388 }
1389 }
1390}
1391
1392/* Channel Operations */
1393/* ================== */
1394
1395/* req_timeout
1396 * timeout routine for control output request
1397 * argument:
1398 * B channel control structure
1399 */
1400static void req_timeout(unsigned long data)
1401{
1402 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001403 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001404 int pending;
1405 unsigned long flags;
1406
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001407 check_pending(ucs);
1408
1409 spin_lock_irqsave(&ucs->lock, flags);
1410 pending = ucs->pending;
1411 ucs->pending = 0;
1412 spin_unlock_irqrestore(&ucs->lock, flags);
1413
1414 switch (pending) {
1415 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001416 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001417 break;
1418
1419 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001420 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001421 error_reset(bcs->cs);
1422 break;
1423
1424 case HD_OPEN_B2CHANNEL:
1425 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001426 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1427 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001428 error_hangup(bcs);
1429 break;
1430
1431 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001432 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001433 break;
1434
1435 case HD_CLOSE_B2CHANNEL:
1436 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001437 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1438 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001439 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001440 break;
1441
1442 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001443 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1444 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001445 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001446
1447 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001448}
1449
1450/* write_ctrl_callback
1451 * USB completion handler for control pipe output
1452 * called by the USB subsystem in interrupt context
1453 * parameter:
1454 * urb USB request block of completed request
1455 * urb->context = hardware specific controller state structure
1456 */
David Howells7d12e782006-10-05 14:55:46 +01001457static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001458{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001459 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001460 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001461 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001462 unsigned long flags;
1463
Tilman Schmidt06163f82006-06-26 00:25:33 -07001464 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001465 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001466 case 0: /* normal completion */
1467 spin_lock_irqsave(&ucs->lock, flags);
1468 switch (ucs->pending) {
1469 case HD_DEVICE_INIT_ACK: /* no reply expected */
1470 del_timer(&ucs->timer_ctrl);
1471 ucs->pending = 0;
1472 break;
1473 }
1474 spin_unlock_irqrestore(&ucs->lock, flags);
1475 return;
1476
1477 case -ENOENT: /* cancelled */
1478 case -ECONNRESET: /* cancelled (async) */
1479 case -EINPROGRESS: /* pending */
1480 case -ENODEV: /* device removed */
1481 case -ESHUTDOWN: /* device shut down */
1482 /* ignore silently */
1483 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001484 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001485 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001486
1487 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001488 /* don't retry if suspend requested */
1489 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001490 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001491 dev_err(&ucs->interface->dev,
1492 "control request 0x%02x failed: %s\n",
1493 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001494 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001495 break; /* give up */
1496 }
1497 dev_notice(&ucs->interface->dev,
1498 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001499 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001500 ucs->retry_ctrl);
1501 /* urb->dev is clobbered by USB subsystem */
1502 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001503 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001504 if (unlikely(rc)) {
1505 dev_err(&ucs->interface->dev,
1506 "could not resubmit request 0x%02x: %s\n",
1507 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1508 break;
1509 }
1510 /* resubmitted */
1511 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001512 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001513
1514 /* failed, clear pending request */
1515 spin_lock_irqsave(&ucs->lock, flags);
1516 del_timer(&ucs->timer_ctrl);
1517 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001518 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001519 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001520}
1521
1522/* req_submit
1523 * submit a control output request without message buffer to the Gigaset base
1524 * and optionally start a timeout
1525 * parameters:
1526 * bcs B channel control structure
1527 * req control request code (HD_*)
1528 * val control request parameter value (set to 0 if unused)
1529 * timeout timeout in seconds (0: no timeout)
1530 * return value:
1531 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001532 * -EBUSY if another request is pending
1533 * any URB submission error code
1534 */
1535static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1536{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001537 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001538 int ret;
1539 unsigned long flags;
1540
Tilman Schmidt784d5852006-04-10 22:55:04 -07001541 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001542
1543 spin_lock_irqsave(&ucs->lock, flags);
1544 if (ucs->pending) {
1545 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001546 dev_err(bcs->cs->dev,
1547 "submission of request 0x%02x failed: "
1548 "request 0x%02x still pending\n",
1549 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001550 return -EBUSY;
1551 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001552
1553 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1554 ucs->dr_ctrl.bRequest = req;
1555 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1556 ucs->dr_ctrl.wIndex = 0;
1557 ucs->dr_ctrl.wLength = 0;
1558 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001559 usb_sndctrlpipe(ucs->udev, 0),
1560 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1561 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001562 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001563 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001564 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001565 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001566 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001567 spin_unlock_irqrestore(&ucs->lock, flags);
1568 return ret;
1569 }
1570 ucs->pending = req;
1571
1572 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001573 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001574 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1575 ucs->timer_ctrl.data = (unsigned long) bcs;
1576 ucs->timer_ctrl.function = req_timeout;
1577 add_timer(&ucs->timer_ctrl);
1578 }
1579
1580 spin_unlock_irqrestore(&ucs->lock, flags);
1581 return 0;
1582}
1583
1584/* gigaset_init_bchannel
1585 * called by common.c to connect a B channel
1586 * initialize isochronous I/O and tell the Gigaset base to open the channel
1587 * argument:
1588 * B channel control structure
1589 * return value:
1590 * 0 on success, error code < 0 on error
1591 */
1592static int gigaset_init_bchannel(struct bc_state *bcs)
1593{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001594 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001595 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001596 unsigned long flags;
1597
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001598 spin_lock_irqsave(&cs->lock, flags);
1599 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001600 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001601 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001602 return -ENODEV;
1603 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001604
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001605 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001606 dev_notice(cs->dev,
1607 "not starting isochronous I/O, "
1608 "suspend in progress\n");
1609 spin_unlock_irqrestore(&cs->lock, flags);
1610 return -EHOSTUNREACH;
1611 }
1612
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001613 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001614 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001615 "could not start isochronous I/O for channel B%d: %s\n",
1616 bcs->channel + 1,
1617 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1618 if (ret != -ENODEV)
1619 error_hangup(bcs);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001620 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001621 return ret;
1622 }
1623
1624 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1625 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001626 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001627 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001628 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001629 if (ret != -ENODEV)
1630 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001631 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001632
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001633 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001634 return ret;
1635}
1636
1637/* gigaset_close_bchannel
1638 * called by common.c to disconnect a B channel
1639 * tell the Gigaset base to close the channel
1640 * stopping isochronous I/O and LL notification will be done when the
1641 * acknowledgement for the close arrives
1642 * argument:
1643 * B channel control structure
1644 * return value:
1645 * 0 on success, error code < 0 on error
1646 */
1647static int gigaset_close_bchannel(struct bc_state *bcs)
1648{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001649 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001650 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001651 unsigned long flags;
1652
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001653 spin_lock_irqsave(&cs->lock, flags);
1654 if (unlikely(!cs->connected)) {
1655 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001656 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1657 return -ENODEV;
1658 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001659
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001660 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001661 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001662 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001663 gigaset_bchannel_down(bcs);
1664 return 0;
1665 }
1666
Tilman Schmidt73a88812006-04-22 02:35:30 -07001667 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001668 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1669 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001670 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001671 bcs->channel + 1);
1672
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001673 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001674 return ret;
1675}
1676
1677/* Device Operations */
1678/* ================= */
1679
1680/* complete_cb
1681 * unqueue first command buffer from queue, waking any sleepers
1682 * must be called with cs->cmdlock held
1683 * parameter:
1684 * cs controller state structure
1685 */
1686static void complete_cb(struct cardstate *cs)
1687{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001688 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001689
1690 /* unqueue completed buffer */
1691 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001692 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1693 "write_command: sent %u bytes, %u left",
1694 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001695 if ((cs->cmdbuf = cb->next) != NULL) {
1696 cs->cmdbuf->prev = NULL;
1697 cs->curlen = cs->cmdbuf->len;
1698 } else {
1699 cs->lastcmdbuf = NULL;
1700 cs->curlen = 0;
1701 }
1702
1703 if (cb->wake_tasklet)
1704 tasklet_schedule(cb->wake_tasklet);
1705
1706 kfree(cb);
1707}
1708
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001709/* write_command_callback
1710 * USB completion handler for AT command transmission
1711 * called by the USB subsystem in interrupt context
1712 * parameter:
1713 * urb USB request block of completed request
1714 * urb->context = controller state structure
1715 */
David Howells7d12e782006-10-05 14:55:46 +01001716static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001717{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001718 struct cardstate *cs = urb->context;
1719 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001720 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001721 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001722
Tilman Schmidt73a88812006-04-22 02:35:30 -07001723 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001724 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001725
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001726 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001727 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001728 case 0: /* normal completion */
1729 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001730 case -ENOENT: /* cancelled */
1731 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001732 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001733 case -ENODEV: /* device removed */
1734 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001735 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001736 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001737 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001738 return;
1739 default: /* any failure */
1740 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001741 dev_warn(cs->dev,
1742 "command write: %s, "
1743 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001744 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001745 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001746 break;
1747 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001748 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001749 dev_warn(cs->dev,
1750 "command write: %s, "
1751 "won't retry - suspend requested\n",
1752 get_usb_statmsg(status));
1753 break;
1754 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001755 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001756 dev_warn(cs->dev,
1757 "command write: %s, "
1758 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001759 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001760 break;
1761 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001762 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001763 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001764 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1765 /* resubmitted - bypass regular exit block */
1766 return;
1767 /* command send failed, assume base still waiting */
1768 update_basstate(ucs, BS_ATREADY, 0);
1769 }
1770
1771 spin_lock_irqsave(&cs->cmdlock, flags);
1772 if (cs->cmdbuf != NULL)
1773 complete_cb(cs);
1774 spin_unlock_irqrestore(&cs->cmdlock, flags);
1775}
1776
1777/* atrdy_timeout
1778 * timeout routine for AT command transmission
1779 * argument:
1780 * controller state structure
1781 */
1782static void atrdy_timeout(unsigned long data)
1783{
1784 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001785 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001786
Tilman Schmidt784d5852006-04-10 22:55:04 -07001787 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001788
1789 /* fake the missing signal - what else can I do? */
1790 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1791 start_cbsend(cs);
1792}
1793
1794/* atwrite_submit
1795 * submit an HD_WRITE_ATMESSAGE command URB
1796 * parameters:
1797 * cs controller state structure
1798 * buf buffer containing command to send
1799 * len length of command to send
1800 * return value:
1801 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001802 * -EBUSY if another request is pending
1803 * any URB submission error code
1804 */
1805static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1806{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001807 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001808 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001809
Tilman Schmidt784d5852006-04-10 22:55:04 -07001810 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001811
Tilman Schmidt73a88812006-04-22 02:35:30 -07001812 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001813 dev_err(cs->dev,
1814 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001815 return -EBUSY;
1816 }
1817
1818 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1819 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1820 ucs->dr_cmd_out.wValue = 0;
1821 ucs->dr_cmd_out.wIndex = 0;
1822 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1823 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1824 usb_sndctrlpipe(ucs->udev, 0),
1825 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1826 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001827 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001828 if (unlikely(rc)) {
1829 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001830 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001831 get_usb_rcmsg(rc));
1832 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001833 }
1834
Tilman Schmidt73a88812006-04-22 02:35:30 -07001835 /* submitted successfully, start timeout if necessary */
1836 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001837 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1838 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001839 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1840 ucs->timer_atrdy.data = (unsigned long) cs;
1841 ucs->timer_atrdy.function = atrdy_timeout;
1842 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001843 }
1844 return 0;
1845}
1846
1847/* start_cbsend
1848 * start transmission of AT command queue if necessary
1849 * parameter:
1850 * cs controller state structure
1851 * return value:
1852 * 0 on success
1853 * error code < 0 on error
1854 */
1855static int start_cbsend(struct cardstate *cs)
1856{
1857 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001858 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001859 unsigned long flags;
1860 int rc;
1861 int retval = 0;
1862
Tilman Schmidt024fd292008-02-06 01:38:26 -08001863 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001864 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001865 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1866 return -EHOSTUNREACH;
1867 }
1868
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001869 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001870 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001871 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001872 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1873 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001874 /* flush command queue */
1875 spin_lock_irqsave(&cs->cmdlock, flags);
1876 while (cs->cmdbuf != NULL)
1877 complete_cb(cs);
1878 spin_unlock_irqrestore(&cs->cmdlock, flags);
1879 }
1880 return rc;
1881 }
1882
1883 /* try to send first command in queue */
1884 spin_lock_irqsave(&cs->cmdlock, flags);
1885
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001886 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001887 ucs->retry_cmd_out = 0;
1888 rc = atwrite_submit(cs, cb->buf, cb->len);
1889 if (unlikely(rc)) {
1890 retval = rc;
1891 complete_cb(cs);
1892 }
1893 }
1894
1895 spin_unlock_irqrestore(&cs->cmdlock, flags);
1896 return retval;
1897}
1898
1899/* gigaset_write_cmd
1900 * This function is called by the device independent part of the driver
1901 * to transmit an AT command string to the Gigaset device.
1902 * It encapsulates the device specific method for transmission over the
1903 * direct USB connection to the base.
1904 * The command string is added to the queue of commands to send, and
1905 * USB transmission is started if necessary.
1906 * parameters:
1907 * cs controller state structure
1908 * buf command string to send
1909 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001910 * wake_tasklet tasklet to run when transmission is completed
1911 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001912 * return value:
1913 * number of bytes queued on success
1914 * error code < 0 on error
1915 */
1916static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001917 const unsigned char *buf, int len,
1918 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001919{
1920 struct cmdbuf_t *cb;
1921 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001922 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001923
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001924 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001925 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001926 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001927
Tilman Schmidt39f07222006-12-13 00:33:52 -08001928 if (len <= 0) {
1929 /* nothing to do */
1930 rc = 0;
1931 goto notqueued;
1932 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001933
1934 if (len > IF_WRITEBUF)
1935 len = IF_WRITEBUF;
1936 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001937 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001938 rc = -ENOMEM;
1939 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001940 }
1941
1942 memcpy(cb->buf, buf, len);
1943 cb->len = len;
1944 cb->offset = 0;
1945 cb->next = NULL;
1946 cb->wake_tasklet = wake_tasklet;
1947
1948 spin_lock_irqsave(&cs->cmdlock, flags);
1949 cb->prev = cs->lastcmdbuf;
1950 if (cs->lastcmdbuf)
1951 cs->lastcmdbuf->next = cb;
1952 else {
1953 cs->cmdbuf = cb;
1954 cs->curlen = len;
1955 }
1956 cs->cmdbytes += len;
1957 cs->lastcmdbuf = cb;
1958 spin_unlock_irqrestore(&cs->cmdlock, flags);
1959
Tilman Schmidt73a88812006-04-22 02:35:30 -07001960 spin_lock_irqsave(&cs->lock, flags);
1961 if (unlikely(!cs->connected)) {
1962 spin_unlock_irqrestore(&cs->lock, flags);
1963 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001964 /* flush command queue */
1965 spin_lock_irqsave(&cs->cmdlock, flags);
1966 while (cs->cmdbuf != NULL)
1967 complete_cb(cs);
1968 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001969 return -ENODEV;
1970 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001971 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001972 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001973 return rc < 0 ? rc : len;
1974
1975notqueued: /* request handled without queuing */
1976 if (wake_tasklet)
1977 tasklet_schedule(wake_tasklet);
1978 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001979}
1980
1981/* gigaset_write_room
1982 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001983 * return number of characters the driver will accept to be written via
1984 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001985 * parameter:
1986 * controller state structure
1987 * return value:
1988 * number of characters
1989 */
1990static int gigaset_write_room(struct cardstate *cs)
1991{
1992 return IF_WRITEBUF;
1993}
1994
1995/* gigaset_chars_in_buffer
1996 * tty_driver.chars_in_buffer interface routine
1997 * return number of characters waiting to be sent
1998 * parameter:
1999 * controller state structure
2000 * return value:
2001 * number of characters
2002 */
2003static int gigaset_chars_in_buffer(struct cardstate *cs)
2004{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002005 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002006}
2007
2008/* gigaset_brkchars
2009 * implementation of ioctl(GIGASET_BRKCHARS)
2010 * parameter:
2011 * controller state structure
2012 * return value:
2013 * -EINVAL (unimplemented function)
2014 */
2015static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2016{
2017 return -EINVAL;
2018}
2019
2020
2021/* Device Initialization/Shutdown */
2022/* ============================== */
2023
2024/* Free hardware dependent part of the B channel structure
2025 * parameter:
2026 * bcs B channel structure
2027 * return value:
2028 * !=0 on success
2029 */
2030static int gigaset_freebcshw(struct bc_state *bcs)
2031{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002032 struct bas_bc_state *ubc = bcs->hw.bas;
2033 int i;
2034
2035 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002036 return 0;
2037
Tilman Schmidt73a88812006-04-22 02:35:30 -07002038 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002039 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002040 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2041 for (i = 0; i < BAS_OUTURBS; ++i) {
2042 usb_kill_urb(ubc->isoouturbs[i].urb);
2043 usb_free_urb(ubc->isoouturbs[i].urb);
2044 }
2045 for (i = 0; i < BAS_INURBS; ++i) {
2046 usb_kill_urb(ubc->isoinurbs[i]);
2047 usb_free_urb(ubc->isoinurbs[i]);
2048 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002049 tasklet_kill(&ubc->sent_tasklet);
2050 tasklet_kill(&ubc->rcvd_tasklet);
2051 kfree(ubc->isooutbuf);
2052 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002053 bcs->hw.bas = NULL;
2054 return 1;
2055}
2056
2057/* Initialize hardware dependent part of the B channel structure
2058 * parameter:
2059 * bcs B channel structure
2060 * return value:
2061 * !=0 on success
2062 */
2063static int gigaset_initbcshw(struct bc_state *bcs)
2064{
2065 int i;
2066 struct bas_bc_state *ubc;
2067
2068 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2069 if (!ubc) {
2070 err("could not allocate bas_bc_state");
2071 return 0;
2072 }
2073
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002074 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002075 atomic_set(&ubc->corrbytes, 0);
2076 spin_lock_init(&ubc->isooutlock);
2077 for (i = 0; i < BAS_OUTURBS; ++i) {
2078 ubc->isoouturbs[i].urb = NULL;
2079 ubc->isoouturbs[i].bcs = bcs;
2080 }
2081 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2082 ubc->numsub = 0;
2083 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
2084 err("could not allocate isochronous output buffer");
2085 kfree(ubc);
2086 bcs->hw.bas = NULL;
2087 return 0;
2088 }
2089 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002090 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002091
2092 spin_lock_init(&ubc->isoinlock);
2093 for (i = 0; i < BAS_INURBS; ++i)
2094 ubc->isoinurbs[i] = NULL;
2095 ubc->isoindone = NULL;
2096 ubc->loststatus = -EINPROGRESS;
2097 ubc->isoinlost = 0;
2098 ubc->seqlen = 0;
2099 ubc->inbyte = 0;
2100 ubc->inbits = 0;
2101 ubc->goodbytes = 0;
2102 ubc->alignerrs = 0;
2103 ubc->fcserrs = 0;
2104 ubc->frameerrs = 0;
2105 ubc->giants = 0;
2106 ubc->runts = 0;
2107 ubc->aborts = 0;
2108 ubc->shared0s = 0;
2109 ubc->stolen0s = 0;
2110 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002111 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002112 return 1;
2113}
2114
2115static void gigaset_reinitbcshw(struct bc_state *bcs)
2116{
2117 struct bas_bc_state *ubc = bcs->hw.bas;
2118
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002119 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002120 atomic_set(&bcs->hw.bas->corrbytes, 0);
2121 bcs->hw.bas->numsub = 0;
2122 spin_lock_init(&ubc->isooutlock);
2123 spin_lock_init(&ubc->isoinlock);
2124 ubc->loststatus = -EINPROGRESS;
2125}
2126
2127static void gigaset_freecshw(struct cardstate *cs)
2128{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002129 /* timers, URBs and rcvbuf are disposed of in disconnect */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002130 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002131 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002132}
2133
2134static int gigaset_initcshw(struct cardstate *cs)
2135{
2136 struct bas_cardstate *ucs;
2137
2138 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
2139 if (!ucs)
2140 return 0;
2141
2142 ucs->urb_cmd_in = NULL;
2143 ucs->urb_cmd_out = NULL;
2144 ucs->rcvbuf = NULL;
2145 ucs->rcvbuf_size = 0;
2146
2147 spin_lock_init(&ucs->lock);
2148 ucs->pending = 0;
2149
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002150 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002151 init_timer(&ucs->timer_ctrl);
2152 init_timer(&ucs->timer_atrdy);
2153 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002154 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002155
2156 return 1;
2157}
2158
2159/* freeurbs
2160 * unlink and deallocate all URBs unconditionally
2161 * caller must make sure that no commands are still in progress
2162 * parameter:
2163 * cs controller state structure
2164 */
2165static void freeurbs(struct cardstate *cs)
2166{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002167 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002168 struct bas_bc_state *ubc;
2169 int i, j;
2170
Tilman Schmidt39f07222006-12-13 00:33:52 -08002171 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002172 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002173 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002174 for (i = 0; i < BAS_OUTURBS; ++i) {
2175 usb_kill_urb(ubc->isoouturbs[i].urb);
2176 usb_free_urb(ubc->isoouturbs[i].urb);
2177 ubc->isoouturbs[i].urb = NULL;
2178 }
2179 for (i = 0; i < BAS_INURBS; ++i) {
2180 usb_kill_urb(ubc->isoinurbs[i]);
2181 usb_free_urb(ubc->isoinurbs[i]);
2182 ubc->isoinurbs[i] = NULL;
2183 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002184 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002185 usb_kill_urb(ucs->urb_int_in);
2186 usb_free_urb(ucs->urb_int_in);
2187 ucs->urb_int_in = NULL;
2188 usb_kill_urb(ucs->urb_cmd_out);
2189 usb_free_urb(ucs->urb_cmd_out);
2190 ucs->urb_cmd_out = NULL;
2191 usb_kill_urb(ucs->urb_cmd_in);
2192 usb_free_urb(ucs->urb_cmd_in);
2193 ucs->urb_cmd_in = NULL;
2194 usb_kill_urb(ucs->urb_ctrl);
2195 usb_free_urb(ucs->urb_ctrl);
2196 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002197}
2198
2199/* gigaset_probe
2200 * This function is called when a new USB device is connected.
2201 * It checks whether the new device is handled by this driver.
2202 */
2203static int gigaset_probe(struct usb_interface *interface,
2204 const struct usb_device_id *id)
2205{
2206 struct usb_host_interface *hostif;
2207 struct usb_device *udev = interface_to_usbdev(interface);
2208 struct cardstate *cs = NULL;
2209 struct bas_cardstate *ucs = NULL;
2210 struct bas_bc_state *ubc;
2211 struct usb_endpoint_descriptor *endpoint;
2212 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002213 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002214
Tilman Schmidt784d5852006-04-10 22:55:04 -07002215 gig_dbg(DEBUG_ANY,
2216 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2217 __func__, le16_to_cpu(udev->descriptor.idVendor),
2218 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002219
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002220 /* set required alternate setting */
2221 hostif = interface->cur_altsetting;
2222 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002223 gig_dbg(DEBUG_ANY,
2224 "%s: wrong alternate setting %d - trying to switch",
2225 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002226 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002227 dev_warn(&udev->dev, "usb_set_interface failed, "
2228 "device %d interface %d altsetting %d\n",
2229 udev->devnum, hostif->desc.bInterfaceNumber,
2230 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002231 return -ENODEV;
2232 }
2233 hostif = interface->cur_altsetting;
2234 }
2235
2236 /* Reject application specific interfaces
2237 */
2238 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002239 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2240 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002241 return -ENODEV;
2242 }
2243
Tilman Schmidt784d5852006-04-10 22:55:04 -07002244 dev_info(&udev->dev,
2245 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2246 __func__, le16_to_cpu(udev->descriptor.idVendor),
2247 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002248
Tilman Schmidte468c042008-02-06 01:38:29 -08002249 /* allocate memory for our device state and intialize it */
2250 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2251 GIGASET_MODULENAME);
2252 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002253 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002254 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002255
2256 /* save off device structure ptrs for later use */
2257 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002258 ucs->udev = udev;
2259 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002260 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002261
2262 /* allocate URBs:
2263 * - one for the interrupt pipe
2264 * - three for the different uses of the default control pipe
2265 * - three for each isochronous pipe
2266 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002267 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2268 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2269 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2270 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002271 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002272
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002273 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002274 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002275 for (i = 0; i < BAS_OUTURBS; ++i)
2276 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002277 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002278 goto allocerr;
2279 for (i = 0; i < BAS_INURBS; ++i)
2280 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002281 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002282 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002283 }
2284
2285 ucs->rcvbuf = NULL;
2286 ucs->rcvbuf_size = 0;
2287
2288 /* Fill the interrupt urb and send it to the core */
2289 endpoint = &hostif->endpoint[0].desc;
2290 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002291 usb_rcvintpipe(udev,
2292 (endpoint->bEndpointAddress) & 0x0f),
2293 ucs->int_in_buf, 3, read_int_callback, cs,
2294 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002295 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002296 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002297 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002298 goto error;
2299 }
2300
2301 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002302 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002303 goto error;
2304
2305 /* tell common part that the device is ready */
2306 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002307 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002308
2309 /* save address of controller structure */
2310 usb_set_intfdata(interface, cs);
2311
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002312 if (!gigaset_start(cs))
2313 goto error;
2314
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002315 return 0;
2316
Tilman Schmidt73a88812006-04-22 02:35:30 -07002317allocerr:
2318 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002319error:
2320 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002321 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002322 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002323 return -ENODEV;
2324}
2325
2326/* gigaset_disconnect
2327 * This function is called when the Gigaset base is unplugged.
2328 */
2329static void gigaset_disconnect(struct usb_interface *interface)
2330{
2331 struct cardstate *cs;
2332 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002333 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002334
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002335 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002336
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002337 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002338
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002339 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002340
2341 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002342 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002343
2344 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002345 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002346 gigaset_bchannel_down(cs->bcs + j);
2347
2348 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002349 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002350
2351 /* stop timers and URBs, free ressources */
2352 del_timer_sync(&ucs->timer_ctrl);
2353 del_timer_sync(&ucs->timer_atrdy);
2354 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002355 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002356 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002357 kfree(ucs->rcvbuf);
2358 ucs->rcvbuf = NULL;
2359 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002360 usb_put_dev(ucs->udev);
2361 ucs->interface = NULL;
2362 ucs->udev = NULL;
2363 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002364 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002365}
2366
Tilman Schmidt024fd292008-02-06 01:38:26 -08002367/* gigaset_suspend
2368 * This function is called before the USB connection is suspended.
2369 */
2370static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2371{
2372 struct cardstate *cs = usb_get_intfdata(intf);
2373 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002374 int rc;
2375
2376 /* set suspend flag; this stops AT command/response traffic */
2377 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2378 gig_dbg(DEBUG_SUSPEND, "already suspended");
2379 return 0;
2380 }
2381
2382 /* wait a bit for blocking conditions to go away */
2383 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002384 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002385 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2386 BAS_TIMEOUT*HZ/10);
2387 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2388
2389 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002390 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002391 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002392 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002393 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002394 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002395 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002396 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002397 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002398 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002399 dev_warn(cs->dev, " sending AT command\n");
2400 update_basstate(ucs, 0, BS_SUSPEND);
2401 return -EBUSY;
2402 }
2403
2404 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002405 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002406 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2407 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2408 if (rc) {
2409 update_basstate(ucs, 0, BS_SUSPEND);
2410 return rc;
2411 }
2412 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2413 BAS_TIMEOUT*HZ/10);
2414 /* in case of timeout, proceed anyway */
2415 }
2416
2417 /* kill all URBs and timers that might still be pending */
2418 usb_kill_urb(ucs->urb_ctrl);
2419 usb_kill_urb(ucs->urb_int_in);
2420 del_timer_sync(&ucs->timer_ctrl);
2421
2422 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2423 return 0;
2424}
2425
2426/* gigaset_resume
2427 * This function is called after the USB connection has been resumed.
2428 */
2429static int gigaset_resume(struct usb_interface *intf)
2430{
2431 struct cardstate *cs = usb_get_intfdata(intf);
2432 struct bas_cardstate *ucs = cs->hw.bas;
2433 int rc;
2434
2435 /* resubmit interrupt URB for spontaneous messages from base */
2436 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2437 if (rc) {
2438 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2439 get_usb_rcmsg(rc));
2440 return rc;
2441 }
2442
2443 /* clear suspend flag to reallow activity */
2444 update_basstate(ucs, 0, BS_SUSPEND);
2445
2446 gig_dbg(DEBUG_SUSPEND, "resume complete");
2447 return 0;
2448}
2449
2450/* gigaset_pre_reset
2451 * This function is called before the USB connection is reset.
2452 */
2453static int gigaset_pre_reset(struct usb_interface *intf)
2454{
2455 /* handle just like suspend */
2456 return gigaset_suspend(intf, PMSG_ON);
2457}
2458
2459/* gigaset_post_reset
2460 * This function is called after the USB connection has been reset.
2461 */
2462static int gigaset_post_reset(struct usb_interface *intf)
2463{
2464 /* FIXME: send HD_DEVICE_INIT_ACK? */
2465
2466 /* resume operations */
2467 return gigaset_resume(intf);
2468}
2469
2470
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002471static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002472 gigaset_write_cmd,
2473 gigaset_write_room,
2474 gigaset_chars_in_buffer,
2475 gigaset_brkchars,
2476 gigaset_init_bchannel,
2477 gigaset_close_bchannel,
2478 gigaset_initbcshw,
2479 gigaset_freebcshw,
2480 gigaset_reinitbcshw,
2481 gigaset_initcshw,
2482 gigaset_freecshw,
2483 gigaset_set_modem_ctrl,
2484 gigaset_baud_rate,
2485 gigaset_set_line_ctrl,
2486 gigaset_isoc_send_skb,
2487 gigaset_isoc_input,
2488};
2489
2490/* bas_gigaset_init
2491 * This function is called after the kernel module is loaded.
2492 */
2493static int __init bas_gigaset_init(void)
2494{
2495 int result;
2496
2497 /* allocate memory for our driver state and intialize it */
2498 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002499 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002500 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002501 goto error;
2502
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002503 /* register this driver with the USB subsystem */
2504 result = usb_register(&gigaset_usb_driver);
2505 if (result < 0) {
2506 err("usb_register failed (error %d)", -result);
2507 goto error;
2508 }
2509
2510 info(DRIVER_AUTHOR);
2511 info(DRIVER_DESC);
2512 return 0;
2513
Tilman Schmidte468c042008-02-06 01:38:29 -08002514error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002515 if (driver)
2516 gigaset_freedriver(driver);
2517 driver = NULL;
2518 return -1;
2519}
2520
2521/* bas_gigaset_exit
2522 * This function is called before the kernel module is unloaded.
2523 */
2524static void __exit bas_gigaset_exit(void)
2525{
Tilman Schmidte468c042008-02-06 01:38:29 -08002526 struct bas_cardstate *ucs;
2527 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002528
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002529 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002530 * => no gigaset_start any more
2531 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002532
Tilman Schmidte468c042008-02-06 01:38:29 -08002533 /* stop all connected devices */
2534 for (i = 0; i < driver->minors; i++) {
2535 if (gigaset_shutdown(driver->cs + i) < 0)
2536 continue; /* no device */
2537 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002538
Tilman Schmidte468c042008-02-06 01:38:29 -08002539 /* close all still open channels */
2540 ucs = driver->cs[i].hw.bas;
2541 if (ucs->basstate & BS_B1OPEN) {
2542 gig_dbg(DEBUG_INIT, "closing B1 channel");
2543 usb_control_msg(ucs->udev,
2544 usb_sndctrlpipe(ucs->udev, 0),
2545 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2546 0, 0, NULL, 0, BAS_TIMEOUT);
2547 }
2548 if (ucs->basstate & BS_B2OPEN) {
2549 gig_dbg(DEBUG_INIT, "closing B2 channel");
2550 usb_control_msg(ucs->udev,
2551 usb_sndctrlpipe(ucs->udev, 0),
2552 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2553 0, 0, NULL, 0, BAS_TIMEOUT);
2554 }
2555 if (ucs->basstate & BS_ATOPEN) {
2556 gig_dbg(DEBUG_INIT, "closing AT channel");
2557 usb_control_msg(ucs->udev,
2558 usb_sndctrlpipe(ucs->udev, 0),
2559 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2560 0, 0, NULL, 0, BAS_TIMEOUT);
2561 }
2562 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002563 }
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
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002570 gigaset_freedriver(driver);
2571 driver = NULL;
2572}
2573
2574
2575module_init(bas_gigaset_init);
2576module_exit(bas_gigaset_exit);
2577
2578MODULE_AUTHOR(DRIVER_AUTHOR);
2579MODULE_DESCRIPTION(DRIVER_DESC);
2580MODULE_LICENSE("GPL");