blob: 388e63a8ae948d889897e5681c8d4e355ab94346 [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
Tilman Schmidt170ebf82009-03-18 23:44:23 -070049/* interrupt pipe message size according to ibid. ch. 2.2 */
50#define IP_MSGSIZE 3
51
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080052/* Values for the Gigaset 307x */
53#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070054#define USB_3070_PRODUCT_ID 0x0001
55#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080056#define USB_SX303_PRODUCT_ID 0x0021
57#define USB_SX353_PRODUCT_ID 0x0022
58
59/* table of devices that work with this driver */
Tilman Schmidt35dc8452007-03-29 01:20:34 -070060static const struct usb_device_id gigaset_table [] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070061 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
62 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080063 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
64 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
65 { } /* Terminating entry */
66};
67
68MODULE_DEVICE_TABLE(usb, gigaset_table);
69
Tilman Schmidt06163f82006-06-26 00:25:33 -070070/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080071
Tilman Schmidt06163f82006-06-26 00:25:33 -070072/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080073static int gigaset_probe(struct usb_interface *interface,
74 const struct usb_device_id *id);
75
76/* Function will be called if the device is unplugged */
77static void gigaset_disconnect(struct usb_interface *interface);
78
Tilman Schmidt024fd292008-02-06 01:38:26 -080079/* functions called before/after suspend */
80static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
81static int gigaset_resume(struct usb_interface *intf);
82
83/* functions called before/after device reset */
84static int gigaset_pre_reset(struct usb_interface *intf);
85static int gigaset_post_reset(struct usb_interface *intf);
86
Tilman Schmidt06163f82006-06-26 00:25:33 -070087static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070088static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070089static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070090static int atwrite_submit(struct cardstate *, unsigned char *, int);
91static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080092
Tilman Schmidt06163f82006-06-26 00:25:33 -070093/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080094
95struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070096 struct usb_device *udev; /* USB device pointer */
97 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080098 unsigned char minor; /* starting minor number */
99
Tilman Schmidt784d5852006-04-10 22:55:04 -0700100 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800101 struct usb_ctrlrequest dr_ctrl;
102 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700103 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800104
105 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700106 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800107 struct usb_ctrlrequest dr_cmd_out;
108 int retry_cmd_out;
109
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800111 struct usb_ctrlrequest dr_cmd_in;
112 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700113 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800114
Tilman Schmidt784d5852006-04-10 22:55:04 -0700115 struct urb *urb_int_in; /* URB for interrupt pipe */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700116 unsigned char *int_in_buf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800117
118 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800119 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800120 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800121 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800122 int rcvbuf_size; /* size of AT receive buffer */
123 /* 0: no receive in progress */
124 int retry_cmd_in; /* receive req retry count */
125};
126
127/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700128#define BS_ATOPEN 0x001 /* AT channel open */
129#define BS_B1OPEN 0x002 /* B channel 1 open */
130#define BS_B2OPEN 0x004 /* B channel 2 open */
131#define BS_ATREADY 0x008 /* base ready for AT command */
132#define BS_INIT 0x010 /* base has signalled INIT_OK */
133#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
134#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
135#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800136#define BS_SUSPEND 0x100 /* USB port suspended */
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000137#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800138
139
140static struct gigaset_driver *driver = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800141
142/* usb specific object needed to register this driver with the usb subsystem */
143static struct usb_driver gigaset_usb_driver = {
144 .name = GIGASET_MODULENAME,
145 .probe = gigaset_probe,
146 .disconnect = gigaset_disconnect,
147 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800148 .suspend = gigaset_suspend,
149 .resume = gigaset_resume,
150 .reset_resume = gigaset_post_reset,
151 .pre_reset = gigaset_pre_reset,
152 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800153};
154
Tilman Schmidt73a88812006-04-22 02:35:30 -0700155/* get message text for usb_submit_urb return code
156 */
157static char *get_usb_rcmsg(int rc)
158{
159 static char unkmsg[28];
160
161 switch (rc) {
162 case 0:
163 return "success";
164 case -ENOMEM:
165 return "out of memory";
166 case -ENODEV:
167 return "device not present";
168 case -ENOENT:
169 return "endpoint not present";
170 case -ENXIO:
171 return "URB type not supported";
172 case -EINVAL:
173 return "invalid argument";
174 case -EAGAIN:
175 return "start frame too early or too much scheduled";
176 case -EFBIG:
177 return "too many isochronous frames requested";
178 case -EPIPE:
179 return "endpoint stalled";
180 case -EMSGSIZE:
181 return "invalid packet size";
182 case -ENOSPC:
183 return "would overcommit USB bandwidth";
184 case -ESHUTDOWN:
185 return "device shut down";
186 case -EPERM:
187 return "reject flag set";
188 case -EHOSTUNREACH:
189 return "device suspended";
190 default:
191 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
192 return unkmsg;
193 }
194}
195
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800196/* get message text for USB status code
197 */
198static char *get_usb_statmsg(int status)
199{
200 static char unkmsg[28];
201
202 switch (status) {
203 case 0:
204 return "success";
205 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700206 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800207 case -EINPROGRESS:
208 return "pending";
209 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700210 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800211 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700212 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700213 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800214 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700215 case -EPIPE:
216 return "endpoint stalled";
217 case -ECOMM:
218 return "IN buffer overrun";
219 case -ENOSR:
220 return "OUT buffer underrun";
221 case -EOVERFLOW:
222 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800223 case -EREMOTEIO:
224 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700225 case -ENODEV:
226 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800227 case -EXDEV:
228 return "partial isochronous transfer";
229 case -EINVAL:
230 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700231 case -ECONNRESET:
232 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800233 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700234 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800235 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700236 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800237 return unkmsg;
238 }
239}
240
241/* usb_pipetype_str
242 * retrieve string representation of USB pipe type
243 */
244static inline char *usb_pipetype_str(int pipe)
245{
246 if (usb_pipeisoc(pipe))
247 return "Isoc";
248 if (usb_pipeint(pipe))
249 return "Int";
250 if (usb_pipecontrol(pipe))
251 return "Ctrl";
252 if (usb_pipebulk(pipe))
253 return "Bulk";
254 return "?";
255}
256
257/* dump_urb
258 * write content of URB to syslog for debugging
259 */
260static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700261 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800262{
263#ifdef CONFIG_GIGASET_DEBUG
264 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700265 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800266 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700267 gig_dbg(level,
268 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800269 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700270 (unsigned long) urb->dev,
271 usb_pipetype_str(urb->pipe),
272 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
273 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800274 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700275 urb->transfer_flags);
276 gig_dbg(level,
277 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800278 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700279 (unsigned long) urb->transfer_buffer,
280 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800281 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700282 gig_dbg(level,
283 " start_frame=%d, number_of_packets=%d, interval=%d, "
284 "error_count=%d,",
285 urb->start_frame, urb->number_of_packets, urb->interval,
286 urb->error_count);
287 gig_dbg(level,
288 " context=0x%08lx, complete=0x%08lx, "
289 "iso_frame_desc[]={",
290 (unsigned long) urb->context,
291 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800292 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700293 struct usb_iso_packet_descriptor *pifd
294 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700295 gig_dbg(level,
296 " {offset=%u, length=%u, actual_length=%u, "
297 "status=%u}",
298 pifd->offset, pifd->length, pifd->actual_length,
299 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800300 }
301 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700302 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800303#endif
304}
305
306/* read/set modem control bits etc. (m10x only) */
307static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700308 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800309{
310 return -EINVAL;
311}
312
313static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
314{
315 return -EINVAL;
316}
317
318static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
319{
320 return -EINVAL;
321}
322
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000323/* set/clear bits in base connection state, return previous state
324 */
325static inline int update_basstate(struct bas_cardstate *ucs,
326 int set, int clear)
327{
328 unsigned long flags;
329 int state;
330
331 spin_lock_irqsave(&ucs->lock, flags);
332 state = ucs->basstate;
333 ucs->basstate = (state & ~clear) | set;
334 spin_unlock_irqrestore(&ucs->lock, flags);
335 return state;
336}
337
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800338/* error_hangup
339 * hang up any existing connection because of an unrecoverable error
340 * This function may be called from any context and takes care of scheduling
341 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700342 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800343 * argument:
344 * B channel control structure
345 */
346static inline void error_hangup(struct bc_state *bcs)
347{
348 struct cardstate *cs = bcs->cs;
349
Tilman Schmidt784d5852006-04-10 22:55:04 -0700350 gig_dbg(DEBUG_ANY, "%s: scheduling HUP for channel %d",
351 __func__, bcs->channel);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800352
Tilman Schmidt73a88812006-04-22 02:35:30 -0700353 if (!gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL))
354 dev_err(cs->dev, "event queue full\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800355
356 gigaset_schedule_event(cs);
357}
358
359/* error_reset
360 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700361 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700362 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700363 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800364 * argument:
365 * controller state structure
366 */
367static inline void error_reset(struct cardstate *cs)
368{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000369 /* reset interrupt pipe to recover (ignore errors) */
370 update_basstate(cs->hw.bas, BS_RESETTING, 0);
371 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800372}
373
374/* check_pending
375 * check for completion of pending control request
376 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700377 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800378 */
379static void check_pending(struct bas_cardstate *ucs)
380{
381 unsigned long flags;
382
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800383 spin_lock_irqsave(&ucs->lock, flags);
384 switch (ucs->pending) {
385 case 0:
386 break;
387 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800388 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800389 ucs->pending = 0;
390 break;
391 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800392 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800393 ucs->pending = 0;
394 break;
395 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800396 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800397 ucs->pending = 0;
398 break;
399 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800400 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800401 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800402 break;
403 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800404 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800405 ucs->pending = 0;
406 break;
407 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800408 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800409 ucs->pending = 0;
410 break;
411 case HD_DEVICE_INIT_ACK: /* no reply expected */
412 ucs->pending = 0;
413 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000414 case HD_RESET_INTERRUPT_PIPE:
415 if (!(ucs->basstate & BS_RESETTING))
416 ucs->pending = 0;
417 break;
418 /*
419 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
420 * and should never end up here
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800421 */
422 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700423 dev_warn(&ucs->interface->dev,
424 "unknown pending request 0x%02x cleared\n",
425 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800426 ucs->pending = 0;
427 }
428
429 if (!ucs->pending)
430 del_timer(&ucs->timer_ctrl);
431
432 spin_unlock_irqrestore(&ucs->lock, flags);
433}
434
435/* cmd_in_timeout
436 * timeout routine for command input request
437 * argument:
438 * controller state structure
439 */
440static void cmd_in_timeout(unsigned long data)
441{
442 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700443 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700444 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800445
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800446 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700447 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800448 return;
449 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800450
Tilman Schmidt06163f82006-06-26 00:25:33 -0700451 if (ucs->retry_cmd_in++ < BAS_RETRY) {
452 dev_notice(cs->dev, "control read: timeout, retry %d\n",
453 ucs->retry_cmd_in);
454 rc = atread_submit(cs, BAS_TIMEOUT);
455 if (rc >= 0 || rc == -ENODEV)
456 /* resubmitted or disconnected */
457 /* - bypass regular exit block */
458 return;
459 } else {
460 dev_err(cs->dev,
461 "control read: timeout, giving up after %d tries\n",
462 ucs->retry_cmd_in);
463 }
464 kfree(ucs->rcvbuf);
465 ucs->rcvbuf = NULL;
466 ucs->rcvbuf_size = 0;
467 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800468}
469
Tilman Schmidt06163f82006-06-26 00:25:33 -0700470/* read_ctrl_callback
471 * USB completion handler for control pipe input
472 * called by the USB subsystem in interrupt context
473 * parameter:
474 * urb USB request block
475 * urb->context = inbuf structure for controller state
476 */
David Howells7d12e782006-10-05 14:55:46 +0100477static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700478{
479 struct inbuf_t *inbuf = urb->context;
480 struct cardstate *cs = inbuf->cs;
481 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800482 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700483 int have_data = 0;
484 unsigned numbytes;
485 int rc;
486
487 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800488 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700489
490 if (!ucs->rcvbuf_size) {
491 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
492 return;
493 }
494
495 del_timer(&ucs->timer_cmd_in);
496
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800497 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700498 case 0: /* normal completion */
499 numbytes = urb->actual_length;
500 if (unlikely(numbytes != ucs->rcvbuf_size)) {
501 dev_warn(cs->dev,
502 "control read: received %d chars, expected %d\n",
503 numbytes, ucs->rcvbuf_size);
504 if (numbytes > ucs->rcvbuf_size)
505 numbytes = ucs->rcvbuf_size;
506 }
507
508 /* copy received bytes to inbuf */
509 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
510
511 if (unlikely(numbytes < ucs->rcvbuf_size)) {
512 /* incomplete - resubmit for remaining bytes */
513 ucs->rcvbuf_size -= numbytes;
514 ucs->retry_cmd_in = 0;
515 rc = atread_submit(cs, BAS_TIMEOUT);
516 if (rc >= 0 || rc == -ENODEV)
517 /* resubmitted or disconnected */
518 /* - bypass regular exit block */
519 return;
520 error_reset(cs);
521 }
522 break;
523
524 case -ENOENT: /* cancelled */
525 case -ECONNRESET: /* cancelled (async) */
526 case -EINPROGRESS: /* pending */
527 case -ENODEV: /* device removed */
528 case -ESHUTDOWN: /* device shut down */
529 /* no action necessary */
530 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800531 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700532 break;
533
534 default: /* severe trouble */
535 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800536 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700537 if (ucs->retry_cmd_in++ < BAS_RETRY) {
538 dev_notice(cs->dev, "control read: retry %d\n",
539 ucs->retry_cmd_in);
540 rc = atread_submit(cs, BAS_TIMEOUT);
541 if (rc >= 0 || rc == -ENODEV)
542 /* resubmitted or disconnected */
543 /* - bypass regular exit block */
544 return;
545 } else {
546 dev_err(cs->dev,
547 "control read: giving up after %d tries\n",
548 ucs->retry_cmd_in);
549 }
550 error_reset(cs);
551 }
552
553 kfree(ucs->rcvbuf);
554 ucs->rcvbuf = NULL;
555 ucs->rcvbuf_size = 0;
556 if (have_data) {
557 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
558 gigaset_schedule_event(cs);
559 }
560}
561
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800562/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700563 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800564 * parameters:
565 * cs controller state structure
566 * timeout timeout in 1/10 sec., 0: none
567 * return value:
568 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800569 * -EBUSY if another request is pending
570 * any URB submission error code
571 */
572static int atread_submit(struct cardstate *cs, int timeout)
573{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700574 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800575 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800576 int ret;
577
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
579 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580
Tilman Schmidt024fd292008-02-06 01:38:26 -0800581 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
582 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700583 dev_err(cs->dev,
584 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800585 return -EBUSY;
586 }
587
Tilman Schmidt024fd292008-02-06 01:38:26 -0800588 if (basstate & BS_SUSPEND) {
589 dev_notice(cs->dev,
590 "HD_READ_ATMESSAGE not submitted, "
591 "suspend in progress\n");
592 update_basstate(ucs, 0, BS_ATRDPEND);
593 /* treat like disconnect */
594 return -ENODEV;
595 }
596
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800597 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
598 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
599 ucs->dr_cmd_in.wValue = 0;
600 ucs->dr_cmd_in.wIndex = 0;
601 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
602 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700603 usb_rcvctrlpipe(ucs->udev, 0),
604 (unsigned char*) & ucs->dr_cmd_in,
605 ucs->rcvbuf, ucs->rcvbuf_size,
606 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800607
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800608 if ((ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC)) != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700609 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700610 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700611 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800612 return ret;
613 }
614
615 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700616 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800617 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
618 ucs->timer_cmd_in.data = (unsigned long) cs;
619 ucs->timer_cmd_in.function = cmd_in_timeout;
620 add_timer(&ucs->timer_cmd_in);
621 }
622 return 0;
623}
624
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800625/* read_int_callback
626 * USB completion handler for interrupt pipe input
627 * called by the USB subsystem in interrupt context
628 * parameter:
629 * urb USB request block
630 * urb->context = controller state structure
631 */
David Howells7d12e782006-10-05 14:55:46 +0100632static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800633{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700634 struct cardstate *cs = urb->context;
635 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800636 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800637 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800638 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700639 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800640 unsigned l;
641 int channel;
642
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800643 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800644 case 0: /* success */
645 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700646 case -ENOENT: /* cancelled */
647 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800648 case -EINPROGRESS: /* pending */
649 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700650 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800651 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800652 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700653 case -ENODEV: /* device removed */
654 case -ESHUTDOWN: /* device shut down */
655 //FIXME use this as disconnect indicator?
656 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
657 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800658 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700659 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800660 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800661 //FIXME corrective action? resubmission always ok?
662 goto resubmit;
663 }
664
Tilman Schmidt73a88812006-04-22 02:35:30 -0700665 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700666 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700667 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
668 urb->actual_length);
669 goto resubmit;
670 }
671
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800672 l = (unsigned) ucs->int_in_buf[1] +
673 (((unsigned) ucs->int_in_buf[2]) << 8);
674
Tilman Schmidt784d5852006-04-10 22:55:04 -0700675 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
676 urb->actual_length, (int)ucs->int_in_buf[0], l,
677 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800678
679 channel = 0;
680
681 switch (ucs->int_in_buf[0]) {
682 case HD_DEVICE_INIT_OK:
683 update_basstate(ucs, BS_INIT, 0);
684 break;
685
686 case HD_READY_SEND_ATDATA:
687 del_timer(&ucs->timer_atrdy);
688 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
689 start_cbsend(cs);
690 break;
691
692 case HD_OPEN_B2CHANNEL_ACK:
693 ++channel;
694 case HD_OPEN_B1CHANNEL_ACK:
695 bcs = cs->bcs + channel;
696 update_basstate(ucs, BS_B1OPEN << channel, 0);
697 gigaset_bchannel_up(bcs);
698 break;
699
700 case HD_OPEN_ATCHANNEL_ACK:
701 update_basstate(ucs, BS_ATOPEN, 0);
702 start_cbsend(cs);
703 break;
704
705 case HD_CLOSE_B2CHANNEL_ACK:
706 ++channel;
707 case HD_CLOSE_B1CHANNEL_ACK:
708 bcs = cs->bcs + channel;
709 update_basstate(ucs, 0, BS_B1OPEN << channel);
710 stopurbs(bcs->hw.bas);
711 gigaset_bchannel_down(bcs);
712 break;
713
714 case HD_CLOSE_ATCHANNEL_ACK:
715 update_basstate(ucs, 0, BS_ATOPEN);
716 break;
717
718 case HD_B2_FLOW_CONTROL:
719 ++channel;
720 case HD_B1_FLOW_CONTROL:
721 bcs = cs->bcs + channel;
722 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700723 &bcs->hw.bas->corrbytes);
724 gig_dbg(DEBUG_ISO,
725 "Flow control (channel %d, sub %d): 0x%02x => %d",
726 channel, bcs->hw.bas->numsub, l,
727 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800728 break;
729
730 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
731 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700732 dev_warn(cs->dev,
733 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800734 break;
735 }
736 spin_lock_irqsave(&cs->lock, flags);
737 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700738 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700739 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700740 "receive AT data overrun, %d bytes lost\n",
741 ucs->rcvbuf_size);
742 kfree(ucs->rcvbuf);
743 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800744 }
745 if ((ucs->rcvbuf = kmalloc(l, GFP_ATOMIC)) == NULL) {
746 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700747 dev_err(cs->dev, "out of memory receiving AT data\n");
748 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800749 break;
750 }
751 ucs->rcvbuf_size = l;
752 ucs->retry_cmd_in = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700753 if ((rc = atread_submit(cs, BAS_TIMEOUT)) < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800754 kfree(ucs->rcvbuf);
755 ucs->rcvbuf = NULL;
756 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700757 if (rc != -ENODEV) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700758 //FIXME corrective action?
Tilman Schmidt06163f82006-06-26 00:25:33 -0700759 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700760 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700761 break;
762 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800763 }
764 spin_unlock_irqrestore(&cs->lock, flags);
765 break;
766
767 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000768 update_basstate(ucs, 0, BS_RESETTING);
769 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800770 break;
771
772 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700773 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800774 break;
775
776 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700777 dev_warn(cs->dev,
778 "unknown Gigaset signal 0x%02x (%u) ignored\n",
779 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800780 }
781
782 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800783 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800784
785resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800786 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700787 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700788 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700789 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800790 error_reset(cs);
791 }
792}
793
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800794/* read_iso_callback
795 * USB completion handler for B channel isochronous input
796 * called by the USB subsystem in interrupt context
797 * parameter:
798 * urb USB request block of completed request
799 * urb->context = bc_state structure
800 */
David Howells7d12e782006-10-05 14:55:46 +0100801static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800802{
803 struct bc_state *bcs;
804 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800805 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800806 unsigned long flags;
807 int i, rc;
808
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800809 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800810 if (unlikely(status == -ENOENT ||
811 status == -ECONNRESET ||
812 status == -EINPROGRESS ||
813 status == -ENODEV ||
814 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700815 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800816 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800817 return;
818 }
819
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700820 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800821 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800822
823 spin_lock_irqsave(&ubc->isoinlock, flags);
824 if (likely(ubc->isoindone == NULL)) {
825 /* pass URB to tasklet */
826 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800827 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000828 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800829 } else {
830 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800831 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800832 for (i = 0; i < BAS_NUMFRAMES; i++) {
833 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
834 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700835 urb->iso_frame_desc[i].status !=
836 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800837 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800838 urb->iso_frame_desc[i].status = 0;
839 urb->iso_frame_desc[i].actual_length = 0;
840 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800841 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700842 /* urb->dev is clobbered by USB subsystem */
843 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800844 urb->transfer_flags = URB_ISO_ASAP;
845 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700846 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
847 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800848 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700849 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700850 dev_err(bcs->cs->dev,
851 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700852 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800853 dump_urb(DEBUG_ISO, "isoc read", urb);
854 error_hangup(bcs);
855 }
856 }
857 }
858 spin_unlock_irqrestore(&ubc->isoinlock, flags);
859}
860
861/* write_iso_callback
862 * USB completion handler for B channel isochronous output
863 * called by the USB subsystem in interrupt context
864 * parameter:
865 * urb USB request block of completed request
866 * urb->context = isow_urbctx_t structure
867 */
David Howells7d12e782006-10-05 14:55:46 +0100868static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800869{
870 struct isow_urbctx_t *ucx;
871 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800872 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800873 unsigned long flags;
874
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800875 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800876 if (unlikely(status == -ENOENT ||
877 status == -ECONNRESET ||
878 status == -EINPROGRESS ||
879 status == -ENODEV ||
880 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700881 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800882 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883 return;
884 }
885
886 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700887 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800888 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800889 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800890
891 spin_lock_irqsave(&ubc->isooutlock, flags);
892 ubc->isooutovfl = ubc->isooutdone;
893 ubc->isooutdone = ucx;
894 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000895 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800896}
897
898/* starturbs
899 * prepare and submit USB request blocks for isochronous input and output
900 * argument:
901 * B channel control structure
902 * return value:
903 * 0 on success
904 * < 0 on error (no URBs submitted)
905 */
906static int starturbs(struct bc_state *bcs)
907{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700908 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800909 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800910 int j, k;
911 int rc;
912
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800913 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000914 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800915 bcs->inputstate |= INS_flag_hunt;
916
917 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800918 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800919 for (k = 0; k < BAS_INURBS; k++) {
920 urb = ubc->isoinurbs[k];
921 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800922 rc = -EFAULT;
923 goto error;
924 }
925
926 urb->dev = bcs->cs->hw.bas->udev;
927 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
928 urb->transfer_flags = URB_ISO_ASAP;
929 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
930 urb->transfer_buffer_length = BAS_INBUFSIZE;
931 urb->number_of_packets = BAS_NUMFRAMES;
932 urb->interval = BAS_FRAMETIME;
933 urb->complete = read_iso_callback;
934 urb->context = bcs;
935 for (j = 0; j < BAS_NUMFRAMES; j++) {
936 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
937 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
938 urb->iso_frame_desc[j].status = 0;
939 urb->iso_frame_desc[j].actual_length = 0;
940 }
941
942 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800943 if ((rc = usb_submit_urb(urb, GFP_ATOMIC)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800944 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800945 }
946
947 /* initialize L2 transmission */
948 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
949
950 /* set up isochronous output URBs for flag idling */
951 for (k = 0; k < BAS_OUTURBS; ++k) {
952 urb = ubc->isoouturbs[k].urb;
953 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800954 rc = -EFAULT;
955 goto error;
956 }
957 urb->dev = bcs->cs->hw.bas->udev;
958 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
959 urb->transfer_flags = URB_ISO_ASAP;
960 urb->transfer_buffer = ubc->isooutbuf->data;
961 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
962 urb->number_of_packets = BAS_NUMFRAMES;
963 urb->interval = BAS_FRAMETIME;
964 urb->complete = write_iso_callback;
965 urb->context = &ubc->isoouturbs[k];
966 for (j = 0; j < BAS_NUMFRAMES; ++j) {
967 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
968 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
969 urb->iso_frame_desc[j].status = 0;
970 urb->iso_frame_desc[j].actual_length = 0;
971 }
972 ubc->isoouturbs[k].limit = -1;
973 }
974
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800975 /* keep one URB free, submit the others */
976 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800977 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800978 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700979 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800980 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800981 }
982 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800983 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800984 ubc->isooutdone = ubc->isooutovfl = NULL;
985 return 0;
986 error:
987 stopurbs(ubc);
988 return rc;
989}
990
991/* stopurbs
992 * cancel the USB request blocks for isochronous input and output
993 * errors are silently ignored
994 * argument:
995 * B channel control structure
996 */
997static void stopurbs(struct bas_bc_state *ubc)
998{
999 int k, rc;
1000
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001001 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001002
1003 for (k = 0; k < BAS_INURBS; ++k) {
1004 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001005 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001006 "%s: isoc input URB %d unlinked, result = %s",
1007 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001008 }
1009
1010 for (k = 0; k < BAS_OUTURBS; ++k) {
1011 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001012 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001013 "%s: isoc output URB %d unlinked, result = %s",
1014 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001015 }
1016}
1017
1018/* Isochronous Write - Bottom Half */
1019/* =============================== */
1020
1021/* submit_iso_write_urb
1022 * fill and submit the next isochronous write URB
1023 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001024 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001025 * return value:
1026 * number of frames submitted in URB
1027 * 0 if URB not submitted because no data available (isooutbuf busy)
1028 * error code < 0 on error
1029 */
1030static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1031{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001032 struct urb *urb = ucx->urb;
1033 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001034 struct usb_iso_packet_descriptor *ifd;
1035 int corrbytes, nframe, rc;
1036
Tilman Schmidt784d5852006-04-10 22:55:04 -07001037 /* urb->dev is clobbered by USB subsystem */
1038 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001039 urb->transfer_flags = URB_ISO_ASAP;
1040 urb->transfer_buffer = ubc->isooutbuf->data;
1041 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1042
1043 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1044 ifd = &urb->iso_frame_desc[nframe];
1045
1046 /* compute frame length according to flow control */
1047 ifd->length = BAS_NORMFRAME;
1048 if ((corrbytes = atomic_read(&ubc->corrbytes)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001049 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1050 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001051 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1052 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1053 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1054 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1055 ifd->length += corrbytes;
1056 atomic_add(-corrbytes, &ubc->corrbytes);
1057 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001058
1059 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001060 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1061 if (rc < 0) {
1062 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001063 gig_dbg(DEBUG_ISO,
1064 "%s: buffer busy at frame %d",
1065 __func__, nframe);
1066 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001067 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001068 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001069 dev_err(ucx->bcs->cs->dev,
1070 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001071 __func__, rc, nframe);
1072 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001073 }
1074 break;
1075 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001076 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001077 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001078 ifd->status = 0;
1079 ifd->actual_length = 0;
1080 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001081 if (unlikely(nframe == 0))
1082 return 0; /* no data to send */
1083 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001084
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001085 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001086 if (unlikely(rc)) {
1087 if (rc == -ENODEV)
1088 /* device removed - give up silently */
1089 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1090 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001091 dev_err(ucx->bcs->cs->dev,
1092 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001093 get_usb_rcmsg(rc));
1094 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001095 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001096 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001097 return nframe;
1098}
1099
1100/* write_iso_tasklet
1101 * tasklet scheduled when an isochronous output URB from the Gigaset device
1102 * has completed
1103 * parameter:
1104 * data B channel state structure
1105 */
1106static void write_iso_tasklet(unsigned long data)
1107{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001108 struct bc_state *bcs = (struct bc_state *) data;
1109 struct bas_bc_state *ubc = bcs->hw.bas;
1110 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001111 struct isow_urbctx_t *done, *next, *ovfl;
1112 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001113 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001114 struct usb_iso_packet_descriptor *ifd;
1115 int offset;
1116 unsigned long flags;
1117 int i;
1118 struct sk_buff *skb;
1119 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001120 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001121
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001122 /* loop while completed URBs arrive in time */
1123 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001124 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001125 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001126 return;
1127 }
1128
1129 /* retrieve completed URBs */
1130 spin_lock_irqsave(&ubc->isooutlock, flags);
1131 done = ubc->isooutdone;
1132 ubc->isooutdone = NULL;
1133 ovfl = ubc->isooutovfl;
1134 ubc->isooutovfl = NULL;
1135 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1136 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001137 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001138 error_hangup(bcs);
1139 break;
1140 }
1141 if (!done)
1142 break;
1143
1144 /* submit free URB if available */
1145 spin_lock_irqsave(&ubc->isooutlock, flags);
1146 next = ubc->isooutfree;
1147 ubc->isooutfree = NULL;
1148 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1149 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001150 rc = submit_iso_write_urb(next);
1151 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001152 /* could not submit URB, put it back */
1153 spin_lock_irqsave(&ubc->isooutlock, flags);
1154 if (ubc->isooutfree == NULL) {
1155 ubc->isooutfree = next;
1156 next = NULL;
1157 }
1158 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1159 if (next) {
1160 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001161 dev_err(cs->dev,
1162 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001163 error_hangup(bcs);
1164 }
1165 }
1166 }
1167
1168 /* process completed URB */
1169 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001170 status = done->status;
1171 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001172 case -EXDEV: /* partial completion */
1173 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1174 __func__);
1175 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001176 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001177 /* inspect individual frames
1178 * assumptions (for lack of documentation):
1179 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001180 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001181 * - all following frames are not sent at all
1182 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001183 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001184 for (i = 0; i < BAS_NUMFRAMES; i++) {
1185 ifd = &urb->iso_frame_desc[i];
1186 if (ifd->status ||
1187 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001188 dev_warn(cs->dev,
1189 "isochronous write: frame %d: %s, "
1190 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001191 i, get_usb_statmsg(ifd->status),
1192 ifd->actual_length, ifd->length);
1193 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001194 ifd->actual_length)
1195 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001196 break;
1197 }
1198 }
1199#ifdef CONFIG_GIGASET_DEBUG
1200 /* check assumption on remaining frames */
1201 for (; i < BAS_NUMFRAMES; i++) {
1202 ifd = &urb->iso_frame_desc[i];
1203 if (ifd->status != -EINPROGRESS
1204 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001205 dev_warn(cs->dev,
1206 "isochronous write: frame %d: %s, "
1207 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001208 i, get_usb_statmsg(ifd->status),
1209 ifd->actual_length, ifd->length);
1210 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001211 ifd->actual_length)
1212 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001213 break;
1214 }
1215 }
1216#endif
1217 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001218 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001219 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001220 error_hangup(bcs);
1221 break;
1222 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001223 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001224 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001225 }
1226
1227 /* mark the write buffer area covered by this URB as free */
1228 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001229 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001230
1231 /* mark URB as free */
1232 spin_lock_irqsave(&ubc->isooutlock, flags);
1233 next = ubc->isooutfree;
1234 ubc->isooutfree = done;
1235 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1236 if (next) {
1237 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001238 rc = submit_iso_write_urb(next);
1239 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001240 /* couldn't submit */
1241 error_hangup(bcs);
1242 }
1243 }
1244 }
1245
1246 /* process queued SKBs */
1247 while ((skb = skb_dequeue(&bcs->squeue))) {
1248 /* copy to output buffer, doing L2 encapsulation */
1249 len = skb->len;
1250 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1251 /* insufficient buffer space, push back onto queue */
1252 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001253 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1254 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001255 break;
1256 }
1257 skb_pull(skb, len);
1258 gigaset_skb_sent(bcs, skb);
1259 dev_kfree_skb_any(skb);
1260 }
1261}
1262
1263/* Isochronous Read - Bottom Half */
1264/* ============================== */
1265
1266/* read_iso_tasklet
1267 * tasklet scheduled when an isochronous input URB from the Gigaset device
1268 * has completed
1269 * parameter:
1270 * data B channel state structure
1271 */
1272static void read_iso_tasklet(unsigned long data)
1273{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001274 struct bc_state *bcs = (struct bc_state *) data;
1275 struct bas_bc_state *ubc = bcs->hw.bas;
1276 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001277 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001278 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001279 char *rcvbuf;
1280 unsigned long flags;
1281 int totleft, numbytes, offset, frame, rc;
1282
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001283 /* loop while more completed URBs arrive in the meantime */
1284 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001285 /* retrieve URB */
1286 spin_lock_irqsave(&ubc->isoinlock, flags);
1287 if (!(urb = ubc->isoindone)) {
1288 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1289 return;
1290 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001291 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001292 ubc->isoindone = NULL;
1293 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001294 dev_warn(cs->dev,
1295 "isochronous read overrun, "
1296 "dropped URB with status: %s, %d bytes lost\n",
1297 get_usb_statmsg(ubc->loststatus),
1298 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001299 ubc->loststatus = -EINPROGRESS;
1300 }
1301 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1302
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001303 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001304 gig_dbg(DEBUG_ISO,
1305 "%s: channel not running, "
1306 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001307 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001308 return;
1309 }
1310
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001311 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001312 case 0: /* normal completion */
1313 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001314 case -EXDEV: /* inspect individual frames
1315 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001316 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1317 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001318 break;
1319 case -ENOENT:
1320 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001321 case -EINPROGRESS:
1322 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001323 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001324 continue; /* -> skip */
1325 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001326 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001327 error_hangup(bcs);
1328 continue; /* -> skip */
1329 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001330 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001331 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001332 goto error;
1333 }
1334
1335 rcvbuf = urb->transfer_buffer;
1336 totleft = urb->actual_length;
1337 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001338 numbytes = urb->iso_frame_desc[frame].actual_length;
1339 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001340 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001341 "isochronous read: frame %d[%d]: %s\n",
1342 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001343 get_usb_statmsg(
1344 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001345 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001346 dev_warn(cs->dev,
1347 "isochronous read: frame %d: "
1348 "numbytes (%d) > BAS_MAXFRAME\n",
1349 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001350 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001351 dev_warn(cs->dev,
1352 "isochronous read: frame %d: "
1353 "numbytes (%d) > totleft (%d)\n",
1354 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001355 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001356 }
1357 offset = urb->iso_frame_desc[frame].offset;
1358 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001359 dev_warn(cs->dev,
1360 "isochronous read: frame %d: "
1361 "offset (%d) + numbytes (%d) "
1362 "> BAS_INBUFSIZE\n",
1363 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001364 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001365 }
1366 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1367 totleft -= numbytes;
1368 }
1369 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001370 dev_warn(cs->dev,
1371 "isochronous read: %d data bytes missing\n",
1372 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001373
1374 error:
1375 /* URB processed, resubmit */
1376 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1377 urb->iso_frame_desc[frame].status = 0;
1378 urb->iso_frame_desc[frame].actual_length = 0;
1379 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001380 /* urb->dev is clobbered by USB subsystem */
1381 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001382 urb->transfer_flags = URB_ISO_ASAP;
1383 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001384 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001385 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001386 dev_err(cs->dev,
1387 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001388 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001389 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1390 error_hangup(bcs);
1391 }
1392 }
1393}
1394
1395/* Channel Operations */
1396/* ================== */
1397
1398/* req_timeout
1399 * timeout routine for control output request
1400 * argument:
1401 * B channel control structure
1402 */
1403static void req_timeout(unsigned long data)
1404{
1405 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001406 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001407 int pending;
1408 unsigned long flags;
1409
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001410 check_pending(ucs);
1411
1412 spin_lock_irqsave(&ucs->lock, flags);
1413 pending = ucs->pending;
1414 ucs->pending = 0;
1415 spin_unlock_irqrestore(&ucs->lock, flags);
1416
1417 switch (pending) {
1418 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001419 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001420 break;
1421
1422 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001423 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001424 error_reset(bcs->cs);
1425 break;
1426
1427 case HD_OPEN_B2CHANNEL:
1428 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001429 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1430 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001431 error_hangup(bcs);
1432 break;
1433
1434 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001435 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001436 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001437 break;
1438
1439 case HD_CLOSE_B2CHANNEL:
1440 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001441 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1442 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001443 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001444 break;
1445
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001446 case HD_RESET_INTERRUPT_PIPE:
1447 /* error recovery escalation */
1448 dev_err(bcs->cs->dev,
1449 "reset interrupt pipe timeout, attempting USB reset\n");
1450 usb_queue_reset_device(bcs->cs->hw.bas->interface);
1451 break;
1452
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001453 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001454 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1455 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001456 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001457
1458 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001459}
1460
1461/* write_ctrl_callback
1462 * USB completion handler for control pipe output
1463 * called by the USB subsystem in interrupt context
1464 * parameter:
1465 * urb USB request block of completed request
1466 * urb->context = hardware specific controller state structure
1467 */
David Howells7d12e782006-10-05 14:55:46 +01001468static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001469{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001470 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001471 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001472 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001473 unsigned long flags;
1474
Tilman Schmidt06163f82006-06-26 00:25:33 -07001475 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001476 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001477 case 0: /* normal completion */
1478 spin_lock_irqsave(&ucs->lock, flags);
1479 switch (ucs->pending) {
1480 case HD_DEVICE_INIT_ACK: /* no reply expected */
1481 del_timer(&ucs->timer_ctrl);
1482 ucs->pending = 0;
1483 break;
1484 }
1485 spin_unlock_irqrestore(&ucs->lock, flags);
1486 return;
1487
1488 case -ENOENT: /* cancelled */
1489 case -ECONNRESET: /* cancelled (async) */
1490 case -EINPROGRESS: /* pending */
1491 case -ENODEV: /* device removed */
1492 case -ESHUTDOWN: /* device shut down */
1493 /* ignore silently */
1494 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001495 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001496 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001497
1498 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001499 /* don't retry if suspend requested */
1500 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001501 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001502 dev_err(&ucs->interface->dev,
1503 "control request 0x%02x failed: %s\n",
1504 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001505 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001506 break; /* give up */
1507 }
1508 dev_notice(&ucs->interface->dev,
1509 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001510 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001511 ucs->retry_ctrl);
1512 /* urb->dev is clobbered by USB subsystem */
1513 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001514 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001515 if (unlikely(rc)) {
1516 dev_err(&ucs->interface->dev,
1517 "could not resubmit request 0x%02x: %s\n",
1518 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1519 break;
1520 }
1521 /* resubmitted */
1522 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001523 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001524
1525 /* failed, clear pending request */
1526 spin_lock_irqsave(&ucs->lock, flags);
1527 del_timer(&ucs->timer_ctrl);
1528 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001529 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001530 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001531}
1532
1533/* req_submit
1534 * submit a control output request without message buffer to the Gigaset base
1535 * and optionally start a timeout
1536 * parameters:
1537 * bcs B channel control structure
1538 * req control request code (HD_*)
1539 * val control request parameter value (set to 0 if unused)
1540 * timeout timeout in seconds (0: no timeout)
1541 * return value:
1542 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001543 * -EBUSY if another request is pending
1544 * any URB submission error code
1545 */
1546static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1547{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001548 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001549 int ret;
1550 unsigned long flags;
1551
Tilman Schmidt784d5852006-04-10 22:55:04 -07001552 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001553
1554 spin_lock_irqsave(&ucs->lock, flags);
1555 if (ucs->pending) {
1556 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001557 dev_err(bcs->cs->dev,
1558 "submission of request 0x%02x failed: "
1559 "request 0x%02x still pending\n",
1560 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001561 return -EBUSY;
1562 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001563
1564 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1565 ucs->dr_ctrl.bRequest = req;
1566 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1567 ucs->dr_ctrl.wIndex = 0;
1568 ucs->dr_ctrl.wLength = 0;
1569 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001570 usb_sndctrlpipe(ucs->udev, 0),
1571 (unsigned char*) &ucs->dr_ctrl, NULL, 0,
1572 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001573 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001574 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001575 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001576 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001577 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001578 spin_unlock_irqrestore(&ucs->lock, flags);
1579 return ret;
1580 }
1581 ucs->pending = req;
1582
1583 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001584 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001585 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1586 ucs->timer_ctrl.data = (unsigned long) bcs;
1587 ucs->timer_ctrl.function = req_timeout;
1588 add_timer(&ucs->timer_ctrl);
1589 }
1590
1591 spin_unlock_irqrestore(&ucs->lock, flags);
1592 return 0;
1593}
1594
1595/* gigaset_init_bchannel
1596 * called by common.c to connect a B channel
1597 * initialize isochronous I/O and tell the Gigaset base to open the channel
1598 * argument:
1599 * B channel control structure
1600 * return value:
1601 * 0 on success, error code < 0 on error
1602 */
1603static int gigaset_init_bchannel(struct bc_state *bcs)
1604{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001605 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001606 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001607 unsigned long flags;
1608
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001609 spin_lock_irqsave(&cs->lock, flags);
1610 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001611 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001612 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001613 return -ENODEV;
1614 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001615
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001616 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001617 dev_notice(cs->dev,
1618 "not starting isochronous I/O, "
1619 "suspend in progress\n");
1620 spin_unlock_irqrestore(&cs->lock, flags);
1621 return -EHOSTUNREACH;
1622 }
1623
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001624 if ((ret = starturbs(bcs)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001625 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001626 "could not start isochronous I/O for channel B%d: %s\n",
1627 bcs->channel + 1,
1628 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1629 if (ret != -ENODEV)
1630 error_hangup(bcs);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001631 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001632 return ret;
1633 }
1634
1635 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
1636 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001637 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001638 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001639 stopurbs(bcs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001640 if (ret != -ENODEV)
1641 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001642 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001643
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001644 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001645 return ret;
1646}
1647
1648/* gigaset_close_bchannel
1649 * called by common.c to disconnect a B channel
1650 * tell the Gigaset base to close the channel
1651 * stopping isochronous I/O and LL notification will be done when the
1652 * acknowledgement for the close arrives
1653 * argument:
1654 * B channel control structure
1655 * return value:
1656 * 0 on success, error code < 0 on error
1657 */
1658static int gigaset_close_bchannel(struct bc_state *bcs)
1659{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001660 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001661 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001662 unsigned long flags;
1663
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001664 spin_lock_irqsave(&cs->lock, flags);
1665 if (unlikely(!cs->connected)) {
1666 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001667 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1668 return -ENODEV;
1669 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001670
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001671 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001672 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001673 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001674 gigaset_bchannel_down(bcs);
1675 return 0;
1676 }
1677
Tilman Schmidt73a88812006-04-22 02:35:30 -07001678 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001679 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
1680 if ((ret = req_submit(bcs, req, 0, BAS_TIMEOUT)) < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001681 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001682 bcs->channel + 1);
1683
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001684 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001685 return ret;
1686}
1687
1688/* Device Operations */
1689/* ================= */
1690
1691/* complete_cb
1692 * unqueue first command buffer from queue, waking any sleepers
1693 * must be called with cs->cmdlock held
1694 * parameter:
1695 * cs controller state structure
1696 */
1697static void complete_cb(struct cardstate *cs)
1698{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001699 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001700
1701 /* unqueue completed buffer */
1702 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -07001703 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD,
1704 "write_command: sent %u bytes, %u left",
1705 cs->curlen, cs->cmdbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001706 if ((cs->cmdbuf = cb->next) != NULL) {
1707 cs->cmdbuf->prev = NULL;
1708 cs->curlen = cs->cmdbuf->len;
1709 } else {
1710 cs->lastcmdbuf = NULL;
1711 cs->curlen = 0;
1712 }
1713
1714 if (cb->wake_tasklet)
1715 tasklet_schedule(cb->wake_tasklet);
1716
1717 kfree(cb);
1718}
1719
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001720/* write_command_callback
1721 * USB completion handler for AT command transmission
1722 * called by the USB subsystem in interrupt context
1723 * parameter:
1724 * urb USB request block of completed request
1725 * urb->context = controller state structure
1726 */
David Howells7d12e782006-10-05 14:55:46 +01001727static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001728{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001729 struct cardstate *cs = urb->context;
1730 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001731 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001732 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001733
Tilman Schmidt73a88812006-04-22 02:35:30 -07001734 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001735 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001736
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001737 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001738 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001739 case 0: /* normal completion */
1740 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001741 case -ENOENT: /* cancelled */
1742 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001743 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001744 case -ENODEV: /* device removed */
1745 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001746 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001747 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001748 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001749 return;
1750 default: /* any failure */
1751 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001752 dev_warn(cs->dev,
1753 "command write: %s, "
1754 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001755 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001756 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001757 break;
1758 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001759 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001760 dev_warn(cs->dev,
1761 "command write: %s, "
1762 "won't retry - suspend requested\n",
1763 get_usb_statmsg(status));
1764 break;
1765 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001766 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001767 dev_warn(cs->dev,
1768 "command write: %s, "
1769 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001770 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001771 break;
1772 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001773 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001774 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001775 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1776 /* resubmitted - bypass regular exit block */
1777 return;
1778 /* command send failed, assume base still waiting */
1779 update_basstate(ucs, BS_ATREADY, 0);
1780 }
1781
1782 spin_lock_irqsave(&cs->cmdlock, flags);
1783 if (cs->cmdbuf != NULL)
1784 complete_cb(cs);
1785 spin_unlock_irqrestore(&cs->cmdlock, flags);
1786}
1787
1788/* atrdy_timeout
1789 * timeout routine for AT command transmission
1790 * argument:
1791 * controller state structure
1792 */
1793static void atrdy_timeout(unsigned long data)
1794{
1795 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001796 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001797
Tilman Schmidt784d5852006-04-10 22:55:04 -07001798 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799
1800 /* fake the missing signal - what else can I do? */
1801 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1802 start_cbsend(cs);
1803}
1804
1805/* atwrite_submit
1806 * submit an HD_WRITE_ATMESSAGE command URB
1807 * parameters:
1808 * cs controller state structure
1809 * buf buffer containing command to send
1810 * len length of command to send
1811 * return value:
1812 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001813 * -EBUSY if another request is pending
1814 * any URB submission error code
1815 */
1816static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1817{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001818 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001819 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001820
Tilman Schmidt784d5852006-04-10 22:55:04 -07001821 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001822
Tilman Schmidt73a88812006-04-22 02:35:30 -07001823 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001824 dev_err(cs->dev,
1825 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001826 return -EBUSY;
1827 }
1828
1829 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1830 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1831 ucs->dr_cmd_out.wValue = 0;
1832 ucs->dr_cmd_out.wIndex = 0;
1833 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1834 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1835 usb_sndctrlpipe(ucs->udev, 0),
1836 (unsigned char*) &ucs->dr_cmd_out, buf, len,
1837 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001838 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001839 if (unlikely(rc)) {
1840 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001841 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001842 get_usb_rcmsg(rc));
1843 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001844 }
1845
Tilman Schmidt73a88812006-04-22 02:35:30 -07001846 /* submitted successfully, start timeout if necessary */
1847 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001848 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1849 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001850 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1851 ucs->timer_atrdy.data = (unsigned long) cs;
1852 ucs->timer_atrdy.function = atrdy_timeout;
1853 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001854 }
1855 return 0;
1856}
1857
1858/* start_cbsend
1859 * start transmission of AT command queue if necessary
1860 * parameter:
1861 * cs controller state structure
1862 * return value:
1863 * 0 on success
1864 * error code < 0 on error
1865 */
1866static int start_cbsend(struct cardstate *cs)
1867{
1868 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001869 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001870 unsigned long flags;
1871 int rc;
1872 int retval = 0;
1873
Tilman Schmidt024fd292008-02-06 01:38:26 -08001874 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001875 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001876 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "suspending");
1877 return -EHOSTUNREACH;
1878 }
1879
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001880 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001881 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001882 gig_dbg(DEBUG_TRANSCMD|DEBUG_LOCKCMD, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001883 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1884 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001885 /* flush command queue */
1886 spin_lock_irqsave(&cs->cmdlock, flags);
1887 while (cs->cmdbuf != NULL)
1888 complete_cb(cs);
1889 spin_unlock_irqrestore(&cs->cmdlock, flags);
1890 }
1891 return rc;
1892 }
1893
1894 /* try to send first command in queue */
1895 spin_lock_irqsave(&cs->cmdlock, flags);
1896
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001897 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001898 ucs->retry_cmd_out = 0;
1899 rc = atwrite_submit(cs, cb->buf, cb->len);
1900 if (unlikely(rc)) {
1901 retval = rc;
1902 complete_cb(cs);
1903 }
1904 }
1905
1906 spin_unlock_irqrestore(&cs->cmdlock, flags);
1907 return retval;
1908}
1909
1910/* gigaset_write_cmd
1911 * This function is called by the device independent part of the driver
1912 * to transmit an AT command string to the Gigaset device.
1913 * It encapsulates the device specific method for transmission over the
1914 * direct USB connection to the base.
1915 * The command string is added to the queue of commands to send, and
1916 * USB transmission is started if necessary.
1917 * parameters:
1918 * cs controller state structure
1919 * buf command string to send
1920 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001921 * wake_tasklet tasklet to run when transmission is completed
1922 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001923 * return value:
1924 * number of bytes queued on success
1925 * error code < 0 on error
1926 */
1927static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001928 const unsigned char *buf, int len,
1929 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001930{
1931 struct cmdbuf_t *cb;
1932 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001933 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001934
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001935 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001936 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001937 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001938
Tilman Schmidt39f07222006-12-13 00:33:52 -08001939 if (len <= 0) {
1940 /* nothing to do */
1941 rc = 0;
1942 goto notqueued;
1943 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001944
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001945 /* translate "+++" escape sequence sent as a single separate command
1946 * into "close AT channel" command for error recovery
1947 * The next command will reopen the AT channel automatically.
1948 */
1949 if (len == 3 && !memcmp(buf, "+++", 3)) {
1950 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
1951 goto notqueued;
1952 }
1953
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001954 if (len > IF_WRITEBUF)
1955 len = IF_WRITEBUF;
1956 if (!(cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001957 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001958 rc = -ENOMEM;
1959 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001960 }
1961
1962 memcpy(cb->buf, buf, len);
1963 cb->len = len;
1964 cb->offset = 0;
1965 cb->next = NULL;
1966 cb->wake_tasklet = wake_tasklet;
1967
1968 spin_lock_irqsave(&cs->cmdlock, flags);
1969 cb->prev = cs->lastcmdbuf;
1970 if (cs->lastcmdbuf)
1971 cs->lastcmdbuf->next = cb;
1972 else {
1973 cs->cmdbuf = cb;
1974 cs->curlen = len;
1975 }
1976 cs->cmdbytes += len;
1977 cs->lastcmdbuf = cb;
1978 spin_unlock_irqrestore(&cs->cmdlock, flags);
1979
Tilman Schmidt73a88812006-04-22 02:35:30 -07001980 spin_lock_irqsave(&cs->lock, flags);
1981 if (unlikely(!cs->connected)) {
1982 spin_unlock_irqrestore(&cs->lock, flags);
1983 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001984 /* flush command queue */
1985 spin_lock_irqsave(&cs->cmdlock, flags);
1986 while (cs->cmdbuf != NULL)
1987 complete_cb(cs);
1988 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001989 return -ENODEV;
1990 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001991 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001992 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001993 return rc < 0 ? rc : len;
1994
1995notqueued: /* request handled without queuing */
1996 if (wake_tasklet)
1997 tasklet_schedule(wake_tasklet);
1998 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001999}
2000
2001/* gigaset_write_room
2002 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07002003 * return number of characters the driver will accept to be written via
2004 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002005 * parameter:
2006 * controller state structure
2007 * return value:
2008 * number of characters
2009 */
2010static int gigaset_write_room(struct cardstate *cs)
2011{
2012 return IF_WRITEBUF;
2013}
2014
2015/* gigaset_chars_in_buffer
2016 * tty_driver.chars_in_buffer interface routine
2017 * return number of characters waiting to be sent
2018 * parameter:
2019 * controller state structure
2020 * return value:
2021 * number of characters
2022 */
2023static int gigaset_chars_in_buffer(struct cardstate *cs)
2024{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002025 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002026}
2027
2028/* gigaset_brkchars
2029 * implementation of ioctl(GIGASET_BRKCHARS)
2030 * parameter:
2031 * controller state structure
2032 * return value:
2033 * -EINVAL (unimplemented function)
2034 */
2035static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2036{
2037 return -EINVAL;
2038}
2039
2040
2041/* Device Initialization/Shutdown */
2042/* ============================== */
2043
2044/* Free hardware dependent part of the B channel structure
2045 * parameter:
2046 * bcs B channel structure
2047 * return value:
2048 * !=0 on success
2049 */
2050static int gigaset_freebcshw(struct bc_state *bcs)
2051{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002052 struct bas_bc_state *ubc = bcs->hw.bas;
2053 int i;
2054
2055 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002056 return 0;
2057
Tilman Schmidt73a88812006-04-22 02:35:30 -07002058 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002059 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002060 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2061 for (i = 0; i < BAS_OUTURBS; ++i) {
2062 usb_kill_urb(ubc->isoouturbs[i].urb);
2063 usb_free_urb(ubc->isoouturbs[i].urb);
2064 }
2065 for (i = 0; i < BAS_INURBS; ++i) {
2066 usb_kill_urb(ubc->isoinurbs[i]);
2067 usb_free_urb(ubc->isoinurbs[i]);
2068 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002069 tasklet_kill(&ubc->sent_tasklet);
2070 tasklet_kill(&ubc->rcvd_tasklet);
2071 kfree(ubc->isooutbuf);
2072 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002073 bcs->hw.bas = NULL;
2074 return 1;
2075}
2076
2077/* Initialize hardware dependent part of the B channel structure
2078 * parameter:
2079 * bcs B channel structure
2080 * return value:
2081 * !=0 on success
2082 */
2083static int gigaset_initbcshw(struct bc_state *bcs)
2084{
2085 int i;
2086 struct bas_bc_state *ubc;
2087
2088 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2089 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002090 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002091 return 0;
2092 }
2093
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002094 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002095 atomic_set(&ubc->corrbytes, 0);
2096 spin_lock_init(&ubc->isooutlock);
2097 for (i = 0; i < BAS_OUTURBS; ++i) {
2098 ubc->isoouturbs[i].urb = NULL;
2099 ubc->isoouturbs[i].bcs = bcs;
2100 }
2101 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2102 ubc->numsub = 0;
2103 if (!(ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL))) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002104 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002105 kfree(ubc);
2106 bcs->hw.bas = NULL;
2107 return 0;
2108 }
2109 tasklet_init(&ubc->sent_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002110 &write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002111
2112 spin_lock_init(&ubc->isoinlock);
2113 for (i = 0; i < BAS_INURBS; ++i)
2114 ubc->isoinurbs[i] = NULL;
2115 ubc->isoindone = NULL;
2116 ubc->loststatus = -EINPROGRESS;
2117 ubc->isoinlost = 0;
2118 ubc->seqlen = 0;
2119 ubc->inbyte = 0;
2120 ubc->inbits = 0;
2121 ubc->goodbytes = 0;
2122 ubc->alignerrs = 0;
2123 ubc->fcserrs = 0;
2124 ubc->frameerrs = 0;
2125 ubc->giants = 0;
2126 ubc->runts = 0;
2127 ubc->aborts = 0;
2128 ubc->shared0s = 0;
2129 ubc->stolen0s = 0;
2130 tasklet_init(&ubc->rcvd_tasklet,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002131 &read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002132 return 1;
2133}
2134
2135static void gigaset_reinitbcshw(struct bc_state *bcs)
2136{
2137 struct bas_bc_state *ubc = bcs->hw.bas;
2138
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002139 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002140 atomic_set(&bcs->hw.bas->corrbytes, 0);
2141 bcs->hw.bas->numsub = 0;
2142 spin_lock_init(&ubc->isooutlock);
2143 spin_lock_init(&ubc->isoinlock);
2144 ubc->loststatus = -EINPROGRESS;
2145}
2146
2147static void gigaset_freecshw(struct cardstate *cs)
2148{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002149 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002150 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002151 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002152 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002153}
2154
2155static int gigaset_initcshw(struct cardstate *cs)
2156{
2157 struct bas_cardstate *ucs;
2158
2159 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002160 if (!ucs) {
2161 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002162 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002163 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002164 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2165 if (!ucs->int_in_buf) {
2166 kfree(ucs);
2167 pr_err("out of memory\n");
2168 return 0;
2169 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002170
2171 ucs->urb_cmd_in = NULL;
2172 ucs->urb_cmd_out = NULL;
2173 ucs->rcvbuf = NULL;
2174 ucs->rcvbuf_size = 0;
2175
2176 spin_lock_init(&ucs->lock);
2177 ucs->pending = 0;
2178
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002179 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002180 init_timer(&ucs->timer_ctrl);
2181 init_timer(&ucs->timer_atrdy);
2182 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002183 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002184
2185 return 1;
2186}
2187
2188/* freeurbs
2189 * unlink and deallocate all URBs unconditionally
2190 * caller must make sure that no commands are still in progress
2191 * parameter:
2192 * cs controller state structure
2193 */
2194static void freeurbs(struct cardstate *cs)
2195{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002196 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002197 struct bas_bc_state *ubc;
2198 int i, j;
2199
Tilman Schmidt39f07222006-12-13 00:33:52 -08002200 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002201 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002202 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002203 for (i = 0; i < BAS_OUTURBS; ++i) {
2204 usb_kill_urb(ubc->isoouturbs[i].urb);
2205 usb_free_urb(ubc->isoouturbs[i].urb);
2206 ubc->isoouturbs[i].urb = NULL;
2207 }
2208 for (i = 0; i < BAS_INURBS; ++i) {
2209 usb_kill_urb(ubc->isoinurbs[i]);
2210 usb_free_urb(ubc->isoinurbs[i]);
2211 ubc->isoinurbs[i] = NULL;
2212 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002213 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002214 usb_kill_urb(ucs->urb_int_in);
2215 usb_free_urb(ucs->urb_int_in);
2216 ucs->urb_int_in = NULL;
2217 usb_kill_urb(ucs->urb_cmd_out);
2218 usb_free_urb(ucs->urb_cmd_out);
2219 ucs->urb_cmd_out = NULL;
2220 usb_kill_urb(ucs->urb_cmd_in);
2221 usb_free_urb(ucs->urb_cmd_in);
2222 ucs->urb_cmd_in = NULL;
2223 usb_kill_urb(ucs->urb_ctrl);
2224 usb_free_urb(ucs->urb_ctrl);
2225 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002226}
2227
2228/* gigaset_probe
2229 * This function is called when a new USB device is connected.
2230 * It checks whether the new device is handled by this driver.
2231 */
2232static int gigaset_probe(struct usb_interface *interface,
2233 const struct usb_device_id *id)
2234{
2235 struct usb_host_interface *hostif;
2236 struct usb_device *udev = interface_to_usbdev(interface);
2237 struct cardstate *cs = NULL;
2238 struct bas_cardstate *ucs = NULL;
2239 struct bas_bc_state *ubc;
2240 struct usb_endpoint_descriptor *endpoint;
2241 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002242 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002243
Tilman Schmidt784d5852006-04-10 22:55:04 -07002244 gig_dbg(DEBUG_ANY,
2245 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2246 __func__, le16_to_cpu(udev->descriptor.idVendor),
2247 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002248
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002249 /* set required alternate setting */
2250 hostif = interface->cur_altsetting;
2251 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002252 gig_dbg(DEBUG_ANY,
2253 "%s: wrong alternate setting %d - trying to switch",
2254 __func__, hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002255 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002256 dev_warn(&udev->dev, "usb_set_interface failed, "
2257 "device %d interface %d altsetting %d\n",
2258 udev->devnum, hostif->desc.bInterfaceNumber,
2259 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002260 return -ENODEV;
2261 }
2262 hostif = interface->cur_altsetting;
2263 }
2264
2265 /* Reject application specific interfaces
2266 */
2267 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002268 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2269 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002270 return -ENODEV;
2271 }
2272
Tilman Schmidt784d5852006-04-10 22:55:04 -07002273 dev_info(&udev->dev,
2274 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2275 __func__, le16_to_cpu(udev->descriptor.idVendor),
2276 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002277
Tilman Schmidte468c042008-02-06 01:38:29 -08002278 /* allocate memory for our device state and intialize it */
2279 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2280 GIGASET_MODULENAME);
2281 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002282 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002283 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002284
2285 /* save off device structure ptrs for later use */
2286 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002287 ucs->udev = udev;
2288 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002289 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002290
2291 /* allocate URBs:
2292 * - one for the interrupt pipe
2293 * - three for the different uses of the default control pipe
2294 * - three for each isochronous pipe
2295 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002296 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2297 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2298 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2299 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002300 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002301
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002302 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002303 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002304 for (i = 0; i < BAS_OUTURBS; ++i)
2305 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002306 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002307 goto allocerr;
2308 for (i = 0; i < BAS_INURBS; ++i)
2309 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002310 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002311 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002312 }
2313
2314 ucs->rcvbuf = NULL;
2315 ucs->rcvbuf_size = 0;
2316
2317 /* Fill the interrupt urb and send it to the core */
2318 endpoint = &hostif->endpoint[0].desc;
2319 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002320 usb_rcvintpipe(udev,
2321 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002322 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002323 endpoint->bInterval);
Christoph Lametere94b1762006-12-06 20:33:17 -08002324 if ((rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL)) != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002325 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002326 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002327 goto error;
2328 }
2329
2330 /* tell the device that the driver is ready */
Tilman Schmidt73a88812006-04-22 02:35:30 -07002331 if ((rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0)) != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002332 goto error;
2333
2334 /* tell common part that the device is ready */
2335 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002336 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002337
2338 /* save address of controller structure */
2339 usb_set_intfdata(interface, cs);
2340
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002341 if (!gigaset_start(cs))
2342 goto error;
2343
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002344 return 0;
2345
Tilman Schmidt73a88812006-04-22 02:35:30 -07002346allocerr:
2347 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002348error:
2349 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002350 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002351 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002352 return -ENODEV;
2353}
2354
2355/* gigaset_disconnect
2356 * This function is called when the Gigaset base is unplugged.
2357 */
2358static void gigaset_disconnect(struct usb_interface *interface)
2359{
2360 struct cardstate *cs;
2361 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002362 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002363
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002364 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002365
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002366 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002367
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002368 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002369
2370 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002371 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002372
2373 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002374 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002375 gigaset_bchannel_down(cs->bcs + j);
2376
2377 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002378 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002379
2380 /* stop timers and URBs, free ressources */
2381 del_timer_sync(&ucs->timer_ctrl);
2382 del_timer_sync(&ucs->timer_atrdy);
2383 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002384 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002385 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002386 kfree(ucs->rcvbuf);
2387 ucs->rcvbuf = NULL;
2388 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002389 usb_put_dev(ucs->udev);
2390 ucs->interface = NULL;
2391 ucs->udev = NULL;
2392 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002393 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002394}
2395
Tilman Schmidt024fd292008-02-06 01:38:26 -08002396/* gigaset_suspend
2397 * This function is called before the USB connection is suspended.
2398 */
2399static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2400{
2401 struct cardstate *cs = usb_get_intfdata(intf);
2402 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002403 int rc;
2404
2405 /* set suspend flag; this stops AT command/response traffic */
2406 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2407 gig_dbg(DEBUG_SUSPEND, "already suspended");
2408 return 0;
2409 }
2410
2411 /* wait a bit for blocking conditions to go away */
2412 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002413 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002414 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2415 BAS_TIMEOUT*HZ/10);
2416 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2417
2418 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002419 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002420 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002421 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002422 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002423 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002424 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002425 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002426 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002427 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002428 dev_warn(cs->dev, " sending AT command\n");
2429 update_basstate(ucs, 0, BS_SUSPEND);
2430 return -EBUSY;
2431 }
2432
2433 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002434 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002435 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2436 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2437 if (rc) {
2438 update_basstate(ucs, 0, BS_SUSPEND);
2439 return rc;
2440 }
2441 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2442 BAS_TIMEOUT*HZ/10);
2443 /* in case of timeout, proceed anyway */
2444 }
2445
2446 /* kill all URBs and timers that might still be pending */
2447 usb_kill_urb(ucs->urb_ctrl);
2448 usb_kill_urb(ucs->urb_int_in);
2449 del_timer_sync(&ucs->timer_ctrl);
2450
2451 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2452 return 0;
2453}
2454
2455/* gigaset_resume
2456 * This function is called after the USB connection has been resumed.
2457 */
2458static int gigaset_resume(struct usb_interface *intf)
2459{
2460 struct cardstate *cs = usb_get_intfdata(intf);
2461 struct bas_cardstate *ucs = cs->hw.bas;
2462 int rc;
2463
2464 /* resubmit interrupt URB for spontaneous messages from base */
2465 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2466 if (rc) {
2467 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2468 get_usb_rcmsg(rc));
2469 return rc;
2470 }
2471
2472 /* clear suspend flag to reallow activity */
2473 update_basstate(ucs, 0, BS_SUSPEND);
2474
2475 gig_dbg(DEBUG_SUSPEND, "resume complete");
2476 return 0;
2477}
2478
2479/* gigaset_pre_reset
2480 * This function is called before the USB connection is reset.
2481 */
2482static int gigaset_pre_reset(struct usb_interface *intf)
2483{
2484 /* handle just like suspend */
2485 return gigaset_suspend(intf, PMSG_ON);
2486}
2487
2488/* gigaset_post_reset
2489 * This function is called after the USB connection has been reset.
2490 */
2491static int gigaset_post_reset(struct usb_interface *intf)
2492{
2493 /* FIXME: send HD_DEVICE_INIT_ACK? */
2494
2495 /* resume operations */
2496 return gigaset_resume(intf);
2497}
2498
2499
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002500static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002501 gigaset_write_cmd,
2502 gigaset_write_room,
2503 gigaset_chars_in_buffer,
2504 gigaset_brkchars,
2505 gigaset_init_bchannel,
2506 gigaset_close_bchannel,
2507 gigaset_initbcshw,
2508 gigaset_freebcshw,
2509 gigaset_reinitbcshw,
2510 gigaset_initcshw,
2511 gigaset_freecshw,
2512 gigaset_set_modem_ctrl,
2513 gigaset_baud_rate,
2514 gigaset_set_line_ctrl,
2515 gigaset_isoc_send_skb,
2516 gigaset_isoc_input,
2517};
2518
2519/* bas_gigaset_init
2520 * This function is called after the kernel module is loaded.
2521 */
2522static int __init bas_gigaset_init(void)
2523{
2524 int result;
2525
2526 /* allocate memory for our driver state and intialize it */
2527 if ((driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002528 GIGASET_MODULENAME, GIGASET_DEVNAME,
Greg Kroah-Hartmanf4eaa372005-06-20 21:15:16 -07002529 &gigops, THIS_MODULE)) == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002530 goto error;
2531
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002532 /* register this driver with the USB subsystem */
2533 result = usb_register(&gigaset_usb_driver);
2534 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002535 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002536 goto error;
2537 }
2538
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002539 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002540 return 0;
2541
Tilman Schmidte468c042008-02-06 01:38:29 -08002542error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002543 if (driver)
2544 gigaset_freedriver(driver);
2545 driver = NULL;
2546 return -1;
2547}
2548
2549/* bas_gigaset_exit
2550 * This function is called before the kernel module is unloaded.
2551 */
2552static void __exit bas_gigaset_exit(void)
2553{
Tilman Schmidte468c042008-02-06 01:38:29 -08002554 struct bas_cardstate *ucs;
2555 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002556
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002557 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002558 * => no gigaset_start any more
2559 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002560
Tilman Schmidte468c042008-02-06 01:38:29 -08002561 /* stop all connected devices */
2562 for (i = 0; i < driver->minors; i++) {
2563 if (gigaset_shutdown(driver->cs + i) < 0)
2564 continue; /* no device */
2565 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002566
Tilman Schmidte468c042008-02-06 01:38:29 -08002567 /* close all still open channels */
2568 ucs = driver->cs[i].hw.bas;
2569 if (ucs->basstate & BS_B1OPEN) {
2570 gig_dbg(DEBUG_INIT, "closing B1 channel");
2571 usb_control_msg(ucs->udev,
2572 usb_sndctrlpipe(ucs->udev, 0),
2573 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2574 0, 0, NULL, 0, BAS_TIMEOUT);
2575 }
2576 if (ucs->basstate & BS_B2OPEN) {
2577 gig_dbg(DEBUG_INIT, "closing B2 channel");
2578 usb_control_msg(ucs->udev,
2579 usb_sndctrlpipe(ucs->udev, 0),
2580 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2581 0, 0, NULL, 0, BAS_TIMEOUT);
2582 }
2583 if (ucs->basstate & BS_ATOPEN) {
2584 gig_dbg(DEBUG_INIT, "closing AT channel");
2585 usb_control_msg(ucs->udev,
2586 usb_sndctrlpipe(ucs->udev, 0),
2587 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2588 0, 0, NULL, 0, BAS_TIMEOUT);
2589 }
2590 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002591 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002592
2593 /* deregister this driver with the USB subsystem */
2594 usb_deregister(&gigaset_usb_driver);
2595 /* this will call the disconnect-callback */
2596 /* from now on, no disconnect/probe callback should be running */
2597
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002598 gigaset_freedriver(driver);
2599 driver = NULL;
2600}
2601
2602
2603module_init(bas_gigaset_init);
2604module_exit(bas_gigaset_exit);
2605
2606MODULE_AUTHOR(DRIVER_AUTHOR);
2607MODULE_DESCRIPTION(DRIVER_DESC);
2608MODULE_LICENSE("GPL");