blob: 0be15c70c16d65d28a3882f44ca84b1be33d3b48 [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 Schmidt7891adf2009-10-25 09:30:37 +000060static 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
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000140static struct gigaset_driver *driver;
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 Schmidt1528b182010-02-22 13:09:52 +0000350 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800351 gigaset_schedule_event(cs);
352}
353
354/* error_reset
355 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700356 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700357 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700358 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800359 * argument:
360 * controller state structure
361 */
362static inline void error_reset(struct cardstate *cs)
363{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000364 /* reset interrupt pipe to recover (ignore errors) */
365 update_basstate(cs->hw.bas, BS_RESETTING, 0);
366 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800367}
368
369/* check_pending
370 * check for completion of pending control request
371 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700372 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800373 */
374static void check_pending(struct bas_cardstate *ucs)
375{
376 unsigned long flags;
377
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800378 spin_lock_irqsave(&ucs->lock, flags);
379 switch (ucs->pending) {
380 case 0:
381 break;
382 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800383 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800384 ucs->pending = 0;
385 break;
386 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800387 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800388 ucs->pending = 0;
389 break;
390 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800391 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800392 ucs->pending = 0;
393 break;
394 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800395 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800396 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800397 break;
398 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800399 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800400 ucs->pending = 0;
401 break;
402 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800403 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800404 ucs->pending = 0;
405 break;
406 case HD_DEVICE_INIT_ACK: /* no reply expected */
407 ucs->pending = 0;
408 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000409 case HD_RESET_INTERRUPT_PIPE:
410 if (!(ucs->basstate & BS_RESETTING))
411 ucs->pending = 0;
412 break;
413 /*
414 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
415 * and should never end up here
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800416 */
417 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700418 dev_warn(&ucs->interface->dev,
419 "unknown pending request 0x%02x cleared\n",
420 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800421 ucs->pending = 0;
422 }
423
424 if (!ucs->pending)
425 del_timer(&ucs->timer_ctrl);
426
427 spin_unlock_irqrestore(&ucs->lock, flags);
428}
429
430/* cmd_in_timeout
431 * timeout routine for command input request
432 * argument:
433 * controller state structure
434 */
435static void cmd_in_timeout(unsigned long data)
436{
437 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700438 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700439 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800440
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800441 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700442 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800443 return;
444 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800445
Tilman Schmidt06163f82006-06-26 00:25:33 -0700446 if (ucs->retry_cmd_in++ < BAS_RETRY) {
447 dev_notice(cs->dev, "control read: timeout, retry %d\n",
448 ucs->retry_cmd_in);
449 rc = atread_submit(cs, BAS_TIMEOUT);
450 if (rc >= 0 || rc == -ENODEV)
451 /* resubmitted or disconnected */
452 /* - bypass regular exit block */
453 return;
454 } else {
455 dev_err(cs->dev,
456 "control read: timeout, giving up after %d tries\n",
457 ucs->retry_cmd_in);
458 }
459 kfree(ucs->rcvbuf);
460 ucs->rcvbuf = NULL;
461 ucs->rcvbuf_size = 0;
462 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800463}
464
Tilman Schmidt06163f82006-06-26 00:25:33 -0700465/* read_ctrl_callback
466 * USB completion handler for control pipe input
467 * called by the USB subsystem in interrupt context
468 * parameter:
469 * urb USB request block
470 * urb->context = inbuf structure for controller state
471 */
David Howells7d12e782006-10-05 14:55:46 +0100472static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700473{
474 struct inbuf_t *inbuf = urb->context;
475 struct cardstate *cs = inbuf->cs;
476 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800477 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700478 int have_data = 0;
479 unsigned numbytes;
480 int rc;
481
482 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800483 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700484
485 if (!ucs->rcvbuf_size) {
486 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
487 return;
488 }
489
490 del_timer(&ucs->timer_cmd_in);
491
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800492 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700493 case 0: /* normal completion */
494 numbytes = urb->actual_length;
495 if (unlikely(numbytes != ucs->rcvbuf_size)) {
496 dev_warn(cs->dev,
497 "control read: received %d chars, expected %d\n",
498 numbytes, ucs->rcvbuf_size);
499 if (numbytes > ucs->rcvbuf_size)
500 numbytes = ucs->rcvbuf_size;
501 }
502
503 /* copy received bytes to inbuf */
504 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
505
506 if (unlikely(numbytes < ucs->rcvbuf_size)) {
507 /* incomplete - resubmit for remaining bytes */
508 ucs->rcvbuf_size -= numbytes;
509 ucs->retry_cmd_in = 0;
510 rc = atread_submit(cs, BAS_TIMEOUT);
511 if (rc >= 0 || rc == -ENODEV)
512 /* resubmitted or disconnected */
513 /* - bypass regular exit block */
514 return;
515 error_reset(cs);
516 }
517 break;
518
519 case -ENOENT: /* cancelled */
520 case -ECONNRESET: /* cancelled (async) */
521 case -EINPROGRESS: /* pending */
522 case -ENODEV: /* device removed */
523 case -ESHUTDOWN: /* device shut down */
524 /* no action necessary */
525 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800526 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700527 break;
528
529 default: /* severe trouble */
530 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800531 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700532 if (ucs->retry_cmd_in++ < BAS_RETRY) {
533 dev_notice(cs->dev, "control read: retry %d\n",
534 ucs->retry_cmd_in);
535 rc = atread_submit(cs, BAS_TIMEOUT);
536 if (rc >= 0 || rc == -ENODEV)
537 /* resubmitted or disconnected */
538 /* - bypass regular exit block */
539 return;
540 } else {
541 dev_err(cs->dev,
542 "control read: giving up after %d tries\n",
543 ucs->retry_cmd_in);
544 }
545 error_reset(cs);
546 }
547
548 kfree(ucs->rcvbuf);
549 ucs->rcvbuf = NULL;
550 ucs->rcvbuf_size = 0;
551 if (have_data) {
552 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
553 gigaset_schedule_event(cs);
554 }
555}
556
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800557/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700558 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800559 * parameters:
560 * cs controller state structure
561 * timeout timeout in 1/10 sec., 0: none
562 * return value:
563 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800564 * -EBUSY if another request is pending
565 * any URB submission error code
566 */
567static int atread_submit(struct cardstate *cs, int timeout)
568{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700569 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800570 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800571 int ret;
572
Tilman Schmidt784d5852006-04-10 22:55:04 -0700573 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
574 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575
Tilman Schmidt024fd292008-02-06 01:38:26 -0800576 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
577 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 dev_err(cs->dev,
579 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800580 return -EBUSY;
581 }
582
Tilman Schmidt024fd292008-02-06 01:38:26 -0800583 if (basstate & BS_SUSPEND) {
584 dev_notice(cs->dev,
585 "HD_READ_ATMESSAGE not submitted, "
586 "suspend in progress\n");
587 update_basstate(ucs, 0, BS_ATRDPEND);
588 /* treat like disconnect */
589 return -ENODEV;
590 }
591
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800592 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
593 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
594 ucs->dr_cmd_in.wValue = 0;
595 ucs->dr_cmd_in.wIndex = 0;
596 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
597 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700598 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000599 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700600 ucs->rcvbuf, ucs->rcvbuf_size,
601 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800602
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000603 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
604 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700605 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700606 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700607 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800608 return ret;
609 }
610
611 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700612 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800613 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
614 ucs->timer_cmd_in.data = (unsigned long) cs;
615 ucs->timer_cmd_in.function = cmd_in_timeout;
616 add_timer(&ucs->timer_cmd_in);
617 }
618 return 0;
619}
620
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800621/* read_int_callback
622 * USB completion handler for interrupt pipe input
623 * called by the USB subsystem in interrupt context
624 * parameter:
625 * urb USB request block
626 * urb->context = controller state structure
627 */
David Howells7d12e782006-10-05 14:55:46 +0100628static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800629{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700630 struct cardstate *cs = urb->context;
631 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800632 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800633 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800634 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700635 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800636 unsigned l;
637 int channel;
638
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800639 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800640 case 0: /* success */
641 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700642 case -ENOENT: /* cancelled */
643 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800644 case -EINPROGRESS: /* pending */
645 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700646 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800647 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800648 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700649 case -ENODEV: /* device removed */
650 case -ESHUTDOWN: /* device shut down */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700651 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
652 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800653 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700654 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800655 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800656 goto resubmit;
657 }
658
Tilman Schmidt73a88812006-04-22 02:35:30 -0700659 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700660 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700661 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
662 urb->actual_length);
663 goto resubmit;
664 }
665
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800666 l = (unsigned) ucs->int_in_buf[1] +
667 (((unsigned) ucs->int_in_buf[2]) << 8);
668
Tilman Schmidt784d5852006-04-10 22:55:04 -0700669 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
670 urb->actual_length, (int)ucs->int_in_buf[0], l,
671 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800672
673 channel = 0;
674
675 switch (ucs->int_in_buf[0]) {
676 case HD_DEVICE_INIT_OK:
677 update_basstate(ucs, BS_INIT, 0);
678 break;
679
680 case HD_READY_SEND_ATDATA:
681 del_timer(&ucs->timer_atrdy);
682 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
683 start_cbsend(cs);
684 break;
685
686 case HD_OPEN_B2CHANNEL_ACK:
687 ++channel;
688 case HD_OPEN_B1CHANNEL_ACK:
689 bcs = cs->bcs + channel;
690 update_basstate(ucs, BS_B1OPEN << channel, 0);
691 gigaset_bchannel_up(bcs);
692 break;
693
694 case HD_OPEN_ATCHANNEL_ACK:
695 update_basstate(ucs, BS_ATOPEN, 0);
696 start_cbsend(cs);
697 break;
698
699 case HD_CLOSE_B2CHANNEL_ACK:
700 ++channel;
701 case HD_CLOSE_B1CHANNEL_ACK:
702 bcs = cs->bcs + channel;
703 update_basstate(ucs, 0, BS_B1OPEN << channel);
704 stopurbs(bcs->hw.bas);
705 gigaset_bchannel_down(bcs);
706 break;
707
708 case HD_CLOSE_ATCHANNEL_ACK:
709 update_basstate(ucs, 0, BS_ATOPEN);
710 break;
711
712 case HD_B2_FLOW_CONTROL:
713 ++channel;
714 case HD_B1_FLOW_CONTROL:
715 bcs = cs->bcs + channel;
716 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700717 &bcs->hw.bas->corrbytes);
718 gig_dbg(DEBUG_ISO,
719 "Flow control (channel %d, sub %d): 0x%02x => %d",
720 channel, bcs->hw.bas->numsub, l,
721 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800722 break;
723
724 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
725 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700726 dev_warn(cs->dev,
727 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800728 break;
729 }
730 spin_lock_irqsave(&cs->lock, flags);
731 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700732 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700733 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700734 "receive AT data overrun, %d bytes lost\n",
735 ucs->rcvbuf_size);
736 kfree(ucs->rcvbuf);
737 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800738 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000739 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
740 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800741 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700742 dev_err(cs->dev, "out of memory receiving AT data\n");
743 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800744 break;
745 }
746 ucs->rcvbuf_size = l;
747 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000748 rc = atread_submit(cs, BAS_TIMEOUT);
749 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800750 kfree(ucs->rcvbuf);
751 ucs->rcvbuf = NULL;
752 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700753 if (rc != -ENODEV) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700754 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700755 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700756 break;
757 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800758 }
759 spin_unlock_irqrestore(&cs->lock, flags);
760 break;
761
762 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000763 update_basstate(ucs, 0, BS_RESETTING);
764 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800765 break;
766
767 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700768 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800769 break;
770
771 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700772 dev_warn(cs->dev,
773 "unknown Gigaset signal 0x%02x (%u) ignored\n",
774 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800775 }
776
777 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800778 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800779
780resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800781 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700782 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700783 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700784 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800785 error_reset(cs);
786 }
787}
788
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800789/* read_iso_callback
790 * USB completion handler for B channel isochronous input
791 * called by the USB subsystem in interrupt context
792 * parameter:
793 * urb USB request block of completed request
794 * urb->context = bc_state structure
795 */
David Howells7d12e782006-10-05 14:55:46 +0100796static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800797{
798 struct bc_state *bcs;
799 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800800 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800801 unsigned long flags;
802 int i, rc;
803
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800804 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800805 if (unlikely(status == -ENOENT ||
806 status == -ECONNRESET ||
807 status == -EINPROGRESS ||
808 status == -ENODEV ||
809 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700810 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800811 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800812 return;
813 }
814
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700815 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800817
818 spin_lock_irqsave(&ubc->isoinlock, flags);
819 if (likely(ubc->isoindone == NULL)) {
820 /* pass URB to tasklet */
821 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800822 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000823 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800824 } else {
825 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800826 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800827 for (i = 0; i < BAS_NUMFRAMES; i++) {
828 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
829 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700830 urb->iso_frame_desc[i].status !=
831 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800832 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800833 urb->iso_frame_desc[i].status = 0;
834 urb->iso_frame_desc[i].actual_length = 0;
835 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800836 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700837 /* urb->dev is clobbered by USB subsystem */
838 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800839 urb->transfer_flags = URB_ISO_ASAP;
840 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700841 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
842 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800843 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700844 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700845 dev_err(bcs->cs->dev,
846 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700847 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800848 dump_urb(DEBUG_ISO, "isoc read", urb);
849 error_hangup(bcs);
850 }
851 }
852 }
853 spin_unlock_irqrestore(&ubc->isoinlock, flags);
854}
855
856/* write_iso_callback
857 * USB completion handler for B channel isochronous output
858 * called by the USB subsystem in interrupt context
859 * parameter:
860 * urb USB request block of completed request
861 * urb->context = isow_urbctx_t structure
862 */
David Howells7d12e782006-10-05 14:55:46 +0100863static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800864{
865 struct isow_urbctx_t *ucx;
866 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800867 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800868 unsigned long flags;
869
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800870 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800871 if (unlikely(status == -ENOENT ||
872 status == -ECONNRESET ||
873 status == -EINPROGRESS ||
874 status == -ENODEV ||
875 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700876 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800877 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800878 return;
879 }
880
881 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700882 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800883 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800884 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800885
886 spin_lock_irqsave(&ubc->isooutlock, flags);
887 ubc->isooutovfl = ubc->isooutdone;
888 ubc->isooutdone = ucx;
889 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000890 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800891}
892
893/* starturbs
894 * prepare and submit USB request blocks for isochronous input and output
895 * argument:
896 * B channel control structure
897 * return value:
898 * 0 on success
899 * < 0 on error (no URBs submitted)
900 */
901static int starturbs(struct bc_state *bcs)
902{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700903 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800904 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800905 int j, k;
906 int rc;
907
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800908 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000909 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800910 bcs->inputstate |= INS_flag_hunt;
911
912 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800913 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800914 for (k = 0; k < BAS_INURBS; k++) {
915 urb = ubc->isoinurbs[k];
916 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800917 rc = -EFAULT;
918 goto error;
919 }
920
921 urb->dev = bcs->cs->hw.bas->udev;
922 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
923 urb->transfer_flags = URB_ISO_ASAP;
924 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
925 urb->transfer_buffer_length = BAS_INBUFSIZE;
926 urb->number_of_packets = BAS_NUMFRAMES;
927 urb->interval = BAS_FRAMETIME;
928 urb->complete = read_iso_callback;
929 urb->context = bcs;
930 for (j = 0; j < BAS_NUMFRAMES; j++) {
931 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
932 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
933 urb->iso_frame_desc[j].status = 0;
934 urb->iso_frame_desc[j].actual_length = 0;
935 }
936
937 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000938 rc = usb_submit_urb(urb, GFP_ATOMIC);
939 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800940 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800941 }
942
943 /* initialize L2 transmission */
944 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
945
946 /* set up isochronous output URBs for flag idling */
947 for (k = 0; k < BAS_OUTURBS; ++k) {
948 urb = ubc->isoouturbs[k].urb;
949 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800950 rc = -EFAULT;
951 goto error;
952 }
953 urb->dev = bcs->cs->hw.bas->udev;
954 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
955 urb->transfer_flags = URB_ISO_ASAP;
956 urb->transfer_buffer = ubc->isooutbuf->data;
957 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
958 urb->number_of_packets = BAS_NUMFRAMES;
959 urb->interval = BAS_FRAMETIME;
960 urb->complete = write_iso_callback;
961 urb->context = &ubc->isoouturbs[k];
962 for (j = 0; j < BAS_NUMFRAMES; ++j) {
963 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
964 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
965 urb->iso_frame_desc[j].status = 0;
966 urb->iso_frame_desc[j].actual_length = 0;
967 }
968 ubc->isoouturbs[k].limit = -1;
969 }
970
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800971 /* keep one URB free, submit the others */
972 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800973 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800974 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700975 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800976 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800977 }
978 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800979 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800980 ubc->isooutdone = ubc->isooutovfl = NULL;
981 return 0;
982 error:
983 stopurbs(ubc);
984 return rc;
985}
986
987/* stopurbs
988 * cancel the USB request blocks for isochronous input and output
989 * errors are silently ignored
990 * argument:
991 * B channel control structure
992 */
993static void stopurbs(struct bas_bc_state *ubc)
994{
995 int k, rc;
996
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800997 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800998
999 for (k = 0; k < BAS_INURBS; ++k) {
1000 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001001 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001002 "%s: isoc input URB %d unlinked, result = %s",
1003 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001004 }
1005
1006 for (k = 0; k < BAS_OUTURBS; ++k) {
1007 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001008 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001009 "%s: isoc output URB %d unlinked, result = %s",
1010 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001011 }
1012}
1013
1014/* Isochronous Write - Bottom Half */
1015/* =============================== */
1016
1017/* submit_iso_write_urb
1018 * fill and submit the next isochronous write URB
1019 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001020 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001021 * return value:
1022 * number of frames submitted in URB
1023 * 0 if URB not submitted because no data available (isooutbuf busy)
1024 * error code < 0 on error
1025 */
1026static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1027{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001028 struct urb *urb = ucx->urb;
1029 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001030 struct usb_iso_packet_descriptor *ifd;
1031 int corrbytes, nframe, rc;
1032
Tilman Schmidt784d5852006-04-10 22:55:04 -07001033 /* urb->dev is clobbered by USB subsystem */
1034 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001035 urb->transfer_flags = URB_ISO_ASAP;
1036 urb->transfer_buffer = ubc->isooutbuf->data;
1037 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1038
1039 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1040 ifd = &urb->iso_frame_desc[nframe];
1041
1042 /* compute frame length according to flow control */
1043 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001044 corrbytes = atomic_read(&ubc->corrbytes);
1045 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001046 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1047 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001048 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1049 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1050 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1051 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1052 ifd->length += corrbytes;
1053 atomic_add(-corrbytes, &ubc->corrbytes);
1054 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001055
1056 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001057 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1058 if (rc < 0) {
1059 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001060 gig_dbg(DEBUG_ISO,
1061 "%s: buffer busy at frame %d",
1062 __func__, nframe);
1063 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001064 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001065 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001066 dev_err(ucx->bcs->cs->dev,
1067 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001068 __func__, rc, nframe);
1069 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001070 }
1071 break;
1072 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001073 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001074 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001075 ifd->status = 0;
1076 ifd->actual_length = 0;
1077 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001078 if (unlikely(nframe == 0))
1079 return 0; /* no data to send */
1080 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001081
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001082 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001083 if (unlikely(rc)) {
1084 if (rc == -ENODEV)
1085 /* device removed - give up silently */
1086 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1087 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001088 dev_err(ucx->bcs->cs->dev,
1089 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001090 get_usb_rcmsg(rc));
1091 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001092 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001093 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001094 return nframe;
1095}
1096
1097/* write_iso_tasklet
1098 * tasklet scheduled when an isochronous output URB from the Gigaset device
1099 * has completed
1100 * parameter:
1101 * data B channel state structure
1102 */
1103static void write_iso_tasklet(unsigned long data)
1104{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001105 struct bc_state *bcs = (struct bc_state *) data;
1106 struct bas_bc_state *ubc = bcs->hw.bas;
1107 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001108 struct isow_urbctx_t *done, *next, *ovfl;
1109 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001110 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001111 struct usb_iso_packet_descriptor *ifd;
1112 int offset;
1113 unsigned long flags;
1114 int i;
1115 struct sk_buff *skb;
1116 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001117 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001119 /* loop while completed URBs arrive in time */
1120 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001121 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001122 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001123 return;
1124 }
1125
1126 /* retrieve completed URBs */
1127 spin_lock_irqsave(&ubc->isooutlock, flags);
1128 done = ubc->isooutdone;
1129 ubc->isooutdone = NULL;
1130 ovfl = ubc->isooutovfl;
1131 ubc->isooutovfl = NULL;
1132 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1133 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001134 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001135 error_hangup(bcs);
1136 break;
1137 }
1138 if (!done)
1139 break;
1140
1141 /* submit free URB if available */
1142 spin_lock_irqsave(&ubc->isooutlock, flags);
1143 next = ubc->isooutfree;
1144 ubc->isooutfree = NULL;
1145 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1146 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001147 rc = submit_iso_write_urb(next);
1148 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001149 /* could not submit URB, put it back */
1150 spin_lock_irqsave(&ubc->isooutlock, flags);
1151 if (ubc->isooutfree == NULL) {
1152 ubc->isooutfree = next;
1153 next = NULL;
1154 }
1155 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1156 if (next) {
1157 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001158 dev_err(cs->dev,
1159 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001160 error_hangup(bcs);
1161 }
1162 }
1163 }
1164
1165 /* process completed URB */
1166 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001167 status = done->status;
1168 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001169 case -EXDEV: /* partial completion */
1170 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1171 __func__);
1172 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001173 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001174 /* inspect individual frames
1175 * assumptions (for lack of documentation):
1176 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001177 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001178 * - all following frames are not sent at all
1179 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001180 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001181 for (i = 0; i < BAS_NUMFRAMES; i++) {
1182 ifd = &urb->iso_frame_desc[i];
1183 if (ifd->status ||
1184 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001185 dev_warn(cs->dev,
1186 "isochronous write: frame %d: %s, "
1187 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001188 i, get_usb_statmsg(ifd->status),
1189 ifd->actual_length, ifd->length);
1190 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001191 ifd->actual_length)
1192 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001193 break;
1194 }
1195 }
1196#ifdef CONFIG_GIGASET_DEBUG
1197 /* check assumption on remaining frames */
1198 for (; i < BAS_NUMFRAMES; i++) {
1199 ifd = &urb->iso_frame_desc[i];
1200 if (ifd->status != -EINPROGRESS
1201 || ifd->actual_length != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001202 dev_warn(cs->dev,
1203 "isochronous write: frame %d: %s, "
1204 "%d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001205 i, get_usb_statmsg(ifd->status),
1206 ifd->actual_length, ifd->length);
1207 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001208 ifd->actual_length)
1209 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001210 break;
1211 }
1212 }
1213#endif
1214 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001215 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001216 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001217 error_hangup(bcs);
1218 break;
1219 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001220 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001221 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001222 }
1223
1224 /* mark the write buffer area covered by this URB as free */
1225 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001226 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001227
1228 /* mark URB as free */
1229 spin_lock_irqsave(&ubc->isooutlock, flags);
1230 next = ubc->isooutfree;
1231 ubc->isooutfree = done;
1232 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1233 if (next) {
1234 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001235 rc = submit_iso_write_urb(next);
1236 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001237 /* couldn't submit */
1238 error_hangup(bcs);
1239 }
1240 }
1241 }
1242
1243 /* process queued SKBs */
1244 while ((skb = skb_dequeue(&bcs->squeue))) {
1245 /* copy to output buffer, doing L2 encapsulation */
1246 len = skb->len;
1247 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1248 /* insufficient buffer space, push back onto queue */
1249 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001250 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1251 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001252 break;
1253 }
1254 skb_pull(skb, len);
1255 gigaset_skb_sent(bcs, skb);
1256 dev_kfree_skb_any(skb);
1257 }
1258}
1259
1260/* Isochronous Read - Bottom Half */
1261/* ============================== */
1262
1263/* read_iso_tasklet
1264 * tasklet scheduled when an isochronous input URB from the Gigaset device
1265 * has completed
1266 * parameter:
1267 * data B channel state structure
1268 */
1269static void read_iso_tasklet(unsigned long data)
1270{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001271 struct bc_state *bcs = (struct bc_state *) data;
1272 struct bas_bc_state *ubc = bcs->hw.bas;
1273 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001274 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001275 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001276 char *rcvbuf;
1277 unsigned long flags;
1278 int totleft, numbytes, offset, frame, rc;
1279
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001280 /* loop while more completed URBs arrive in the meantime */
1281 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001282 /* retrieve URB */
1283 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001284 urb = ubc->isoindone;
1285 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001286 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1287 return;
1288 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001289 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001290 ubc->isoindone = NULL;
1291 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001292 dev_warn(cs->dev,
1293 "isochronous read overrun, "
1294 "dropped URB with status: %s, %d bytes lost\n",
1295 get_usb_statmsg(ubc->loststatus),
1296 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001297 ubc->loststatus = -EINPROGRESS;
1298 }
1299 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1300
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001301 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001302 gig_dbg(DEBUG_ISO,
1303 "%s: channel not running, "
1304 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001305 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001306 return;
1307 }
1308
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001309 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001310 case 0: /* normal completion */
1311 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001312 case -EXDEV: /* inspect individual frames
1313 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001314 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1315 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001316 break;
1317 case -ENOENT:
1318 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001319 case -EINPROGRESS:
1320 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001321 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001322 continue; /* -> skip */
1323 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001324 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001325 error_hangup(bcs);
1326 continue; /* -> skip */
1327 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001328 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001329 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001330 goto error;
1331 }
1332
1333 rcvbuf = urb->transfer_buffer;
1334 totleft = urb->actual_length;
1335 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001336 numbytes = urb->iso_frame_desc[frame].actual_length;
1337 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001338 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001339 "isochronous read: frame %d[%d]: %s\n",
1340 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001341 get_usb_statmsg(
1342 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001343 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001344 dev_warn(cs->dev,
1345 "isochronous read: frame %d: "
1346 "numbytes (%d) > BAS_MAXFRAME\n",
1347 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001348 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001349 dev_warn(cs->dev,
1350 "isochronous read: frame %d: "
1351 "numbytes (%d) > totleft (%d)\n",
1352 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001353 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001354 }
1355 offset = urb->iso_frame_desc[frame].offset;
1356 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001357 dev_warn(cs->dev,
1358 "isochronous read: frame %d: "
1359 "offset (%d) + numbytes (%d) "
1360 "> BAS_INBUFSIZE\n",
1361 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001362 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001363 }
1364 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1365 totleft -= numbytes;
1366 }
1367 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001368 dev_warn(cs->dev,
1369 "isochronous read: %d data bytes missing\n",
1370 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001371
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001372error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001373 /* URB processed, resubmit */
1374 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1375 urb->iso_frame_desc[frame].status = 0;
1376 urb->iso_frame_desc[frame].actual_length = 0;
1377 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001378 /* urb->dev is clobbered by USB subsystem */
1379 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001380 urb->transfer_flags = URB_ISO_ASAP;
1381 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001382 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001383 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001384 dev_err(cs->dev,
1385 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001386 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001387 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1388 error_hangup(bcs);
1389 }
1390 }
1391}
1392
1393/* Channel Operations */
1394/* ================== */
1395
1396/* req_timeout
1397 * timeout routine for control output request
1398 * argument:
1399 * B channel control structure
1400 */
1401static void req_timeout(unsigned long data)
1402{
1403 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001404 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001405 int pending;
1406 unsigned long flags;
1407
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001408 check_pending(ucs);
1409
1410 spin_lock_irqsave(&ucs->lock, flags);
1411 pending = ucs->pending;
1412 ucs->pending = 0;
1413 spin_unlock_irqrestore(&ucs->lock, flags);
1414
1415 switch (pending) {
1416 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001417 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001418 break;
1419
1420 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001421 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001422 error_reset(bcs->cs);
1423 break;
1424
1425 case HD_OPEN_B2CHANNEL:
1426 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001427 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1428 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001429 error_hangup(bcs);
1430 break;
1431
1432 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001433 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001434 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001435 break;
1436
1437 case HD_CLOSE_B2CHANNEL:
1438 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001439 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1440 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001441 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001442 break;
1443
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001444 case HD_RESET_INTERRUPT_PIPE:
1445 /* error recovery escalation */
1446 dev_err(bcs->cs->dev,
1447 "reset interrupt pipe timeout, attempting USB reset\n");
1448 usb_queue_reset_device(bcs->cs->hw.bas->interface);
1449 break;
1450
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001451 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001452 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1453 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001454 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001455
1456 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001457}
1458
1459/* write_ctrl_callback
1460 * USB completion handler for control pipe output
1461 * called by the USB subsystem in interrupt context
1462 * parameter:
1463 * urb USB request block of completed request
1464 * urb->context = hardware specific controller state structure
1465 */
David Howells7d12e782006-10-05 14:55:46 +01001466static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001467{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001468 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001469 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001470 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001471 unsigned long flags;
1472
Tilman Schmidt06163f82006-06-26 00:25:33 -07001473 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001474 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001475 case 0: /* normal completion */
1476 spin_lock_irqsave(&ucs->lock, flags);
1477 switch (ucs->pending) {
1478 case HD_DEVICE_INIT_ACK: /* no reply expected */
1479 del_timer(&ucs->timer_ctrl);
1480 ucs->pending = 0;
1481 break;
1482 }
1483 spin_unlock_irqrestore(&ucs->lock, flags);
1484 return;
1485
1486 case -ENOENT: /* cancelled */
1487 case -ECONNRESET: /* cancelled (async) */
1488 case -EINPROGRESS: /* pending */
1489 case -ENODEV: /* device removed */
1490 case -ESHUTDOWN: /* device shut down */
1491 /* ignore silently */
1492 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001493 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001494 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001495
1496 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001497 /* don't retry if suspend requested */
1498 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001499 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001500 dev_err(&ucs->interface->dev,
1501 "control request 0x%02x failed: %s\n",
1502 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001503 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001504 break; /* give up */
1505 }
1506 dev_notice(&ucs->interface->dev,
1507 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001508 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001509 ucs->retry_ctrl);
1510 /* urb->dev is clobbered by USB subsystem */
1511 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001512 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001513 if (unlikely(rc)) {
1514 dev_err(&ucs->interface->dev,
1515 "could not resubmit request 0x%02x: %s\n",
1516 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1517 break;
1518 }
1519 /* resubmitted */
1520 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001521 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001522
1523 /* failed, clear pending request */
1524 spin_lock_irqsave(&ucs->lock, flags);
1525 del_timer(&ucs->timer_ctrl);
1526 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001527 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001528 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001529}
1530
1531/* req_submit
1532 * submit a control output request without message buffer to the Gigaset base
1533 * and optionally start a timeout
1534 * parameters:
1535 * bcs B channel control structure
1536 * req control request code (HD_*)
1537 * val control request parameter value (set to 0 if unused)
1538 * timeout timeout in seconds (0: no timeout)
1539 * return value:
1540 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001541 * -EBUSY if another request is pending
1542 * any URB submission error code
1543 */
1544static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1545{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001546 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001547 int ret;
1548 unsigned long flags;
1549
Tilman Schmidt784d5852006-04-10 22:55:04 -07001550 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001551
1552 spin_lock_irqsave(&ucs->lock, flags);
1553 if (ucs->pending) {
1554 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001555 dev_err(bcs->cs->dev,
1556 "submission of request 0x%02x failed: "
1557 "request 0x%02x still pending\n",
1558 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001559 return -EBUSY;
1560 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001561
1562 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1563 ucs->dr_ctrl.bRequest = req;
1564 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1565 ucs->dr_ctrl.wIndex = 0;
1566 ucs->dr_ctrl.wLength = 0;
1567 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001568 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001569 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001570 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001571 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001572 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001573 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001574 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001575 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001576 spin_unlock_irqrestore(&ucs->lock, flags);
1577 return ret;
1578 }
1579 ucs->pending = req;
1580
1581 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001582 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001583 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1584 ucs->timer_ctrl.data = (unsigned long) bcs;
1585 ucs->timer_ctrl.function = req_timeout;
1586 add_timer(&ucs->timer_ctrl);
1587 }
1588
1589 spin_unlock_irqrestore(&ucs->lock, flags);
1590 return 0;
1591}
1592
1593/* gigaset_init_bchannel
1594 * called by common.c to connect a B channel
1595 * initialize isochronous I/O and tell the Gigaset base to open the channel
1596 * argument:
1597 * B channel control structure
1598 * return value:
1599 * 0 on success, error code < 0 on error
1600 */
1601static int gigaset_init_bchannel(struct bc_state *bcs)
1602{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001603 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001604 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001605 unsigned long flags;
1606
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001607 spin_lock_irqsave(&cs->lock, flags);
1608 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001609 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001610 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001611 return -ENODEV;
1612 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001613
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001614 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001615 dev_notice(cs->dev,
1616 "not starting isochronous I/O, "
1617 "suspend in progress\n");
1618 spin_unlock_irqrestore(&cs->lock, flags);
1619 return -EHOSTUNREACH;
1620 }
1621
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001622 ret = starturbs(bcs);
1623 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001624 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001625 "could not start isochronous I/O for channel B%d: %s\n",
1626 bcs->channel + 1,
1627 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1628 if (ret != -ENODEV)
1629 error_hangup(bcs);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001630 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001631 return ret;
1632 }
1633
1634 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001635 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1636 if (ret < 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;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001680 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1681 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001682 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001683 bcs->channel + 1);
1684
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001685 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001686 return ret;
1687}
1688
1689/* Device Operations */
1690/* ================= */
1691
1692/* complete_cb
1693 * unqueue first command buffer from queue, waking any sleepers
1694 * must be called with cs->cmdlock held
1695 * parameter:
1696 * cs controller state structure
1697 */
1698static void complete_cb(struct cardstate *cs)
1699{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001700 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001701
1702 /* unqueue completed buffer */
1703 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001704 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001705 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001706 if (cb->next != NULL) {
1707 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001708 cs->cmdbuf->prev = NULL;
1709 cs->curlen = cs->cmdbuf->len;
1710 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001711 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001712 cs->lastcmdbuf = NULL;
1713 cs->curlen = 0;
1714 }
1715
1716 if (cb->wake_tasklet)
1717 tasklet_schedule(cb->wake_tasklet);
1718
1719 kfree(cb);
1720}
1721
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001722/* write_command_callback
1723 * USB completion handler for AT command transmission
1724 * called by the USB subsystem in interrupt context
1725 * parameter:
1726 * urb USB request block of completed request
1727 * urb->context = controller state structure
1728 */
David Howells7d12e782006-10-05 14:55:46 +01001729static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001730{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001731 struct cardstate *cs = urb->context;
1732 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001733 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001734 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001735
Tilman Schmidt73a88812006-04-22 02:35:30 -07001736 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001737 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001738
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001739 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001740 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001741 case 0: /* normal completion */
1742 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001743 case -ENOENT: /* cancelled */
1744 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001745 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001746 case -ENODEV: /* device removed */
1747 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001748 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001749 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001750 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001751 return;
1752 default: /* any failure */
1753 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001754 dev_warn(cs->dev,
1755 "command write: %s, "
1756 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001757 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001758 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001759 break;
1760 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001761 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001762 dev_warn(cs->dev,
1763 "command write: %s, "
1764 "won't retry - suspend requested\n",
1765 get_usb_statmsg(status));
1766 break;
1767 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001768 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001769 dev_warn(cs->dev,
1770 "command write: %s, "
1771 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001772 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001773 break;
1774 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001775 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001776 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001777 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1778 /* resubmitted - bypass regular exit block */
1779 return;
1780 /* command send failed, assume base still waiting */
1781 update_basstate(ucs, BS_ATREADY, 0);
1782 }
1783
1784 spin_lock_irqsave(&cs->cmdlock, flags);
1785 if (cs->cmdbuf != NULL)
1786 complete_cb(cs);
1787 spin_unlock_irqrestore(&cs->cmdlock, flags);
1788}
1789
1790/* atrdy_timeout
1791 * timeout routine for AT command transmission
1792 * argument:
1793 * controller state structure
1794 */
1795static void atrdy_timeout(unsigned long data)
1796{
1797 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001798 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799
Tilman Schmidt784d5852006-04-10 22:55:04 -07001800 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001801
1802 /* fake the missing signal - what else can I do? */
1803 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1804 start_cbsend(cs);
1805}
1806
1807/* atwrite_submit
1808 * submit an HD_WRITE_ATMESSAGE command URB
1809 * parameters:
1810 * cs controller state structure
1811 * buf buffer containing command to send
1812 * len length of command to send
1813 * return value:
1814 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001815 * -EBUSY if another request is pending
1816 * any URB submission error code
1817 */
1818static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1819{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001820 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001821 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001822
Tilman Schmidt784d5852006-04-10 22:55:04 -07001823 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001824
Tilman Schmidt73a88812006-04-22 02:35:30 -07001825 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001826 dev_err(cs->dev,
1827 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001828 return -EBUSY;
1829 }
1830
1831 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1832 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1833 ucs->dr_cmd_out.wValue = 0;
1834 ucs->dr_cmd_out.wIndex = 0;
1835 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1836 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1837 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001838 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001839 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001840 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001841 if (unlikely(rc)) {
1842 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001843 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001844 get_usb_rcmsg(rc));
1845 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001846 }
1847
Tilman Schmidt73a88812006-04-22 02:35:30 -07001848 /* submitted successfully, start timeout if necessary */
1849 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001850 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1851 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001852 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1853 ucs->timer_atrdy.data = (unsigned long) cs;
1854 ucs->timer_atrdy.function = atrdy_timeout;
1855 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001856 }
1857 return 0;
1858}
1859
1860/* start_cbsend
1861 * start transmission of AT command queue if necessary
1862 * parameter:
1863 * cs controller state structure
1864 * return value:
1865 * 0 on success
1866 * error code < 0 on error
1867 */
1868static int start_cbsend(struct cardstate *cs)
1869{
1870 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001871 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001872 unsigned long flags;
1873 int rc;
1874 int retval = 0;
1875
Tilman Schmidt024fd292008-02-06 01:38:26 -08001876 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001877 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001878 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001879 return -EHOSTUNREACH;
1880 }
1881
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001882 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001883 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001884 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001885 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1886 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001887 /* flush command queue */
1888 spin_lock_irqsave(&cs->cmdlock, flags);
1889 while (cs->cmdbuf != NULL)
1890 complete_cb(cs);
1891 spin_unlock_irqrestore(&cs->cmdlock, flags);
1892 }
1893 return rc;
1894 }
1895
1896 /* try to send first command in queue */
1897 spin_lock_irqsave(&cs->cmdlock, flags);
1898
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001899 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001900 ucs->retry_cmd_out = 0;
1901 rc = atwrite_submit(cs, cb->buf, cb->len);
1902 if (unlikely(rc)) {
1903 retval = rc;
1904 complete_cb(cs);
1905 }
1906 }
1907
1908 spin_unlock_irqrestore(&cs->cmdlock, flags);
1909 return retval;
1910}
1911
1912/* gigaset_write_cmd
1913 * This function is called by the device independent part of the driver
1914 * to transmit an AT command string to the Gigaset device.
1915 * It encapsulates the device specific method for transmission over the
1916 * direct USB connection to the base.
1917 * The command string is added to the queue of commands to send, and
1918 * USB transmission is started if necessary.
1919 * parameters:
1920 * cs controller state structure
1921 * buf command string to send
1922 * len number of bytes to send (max. IF_WRITEBUF)
Tilman Schmidt917f5082006-04-10 22:55:00 -07001923 * wake_tasklet tasklet to run when transmission is completed
1924 * (NULL if none)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001925 * return value:
1926 * number of bytes queued on success
1927 * error code < 0 on error
1928 */
1929static int gigaset_write_cmd(struct cardstate *cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001930 const unsigned char *buf, int len,
1931 struct tasklet_struct *wake_tasklet)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001932{
1933 struct cmdbuf_t *cb;
1934 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001935 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001936
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001937 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001938 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -07001939 "CMD Transmit", len, buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001940
Tilman Schmidt39f07222006-12-13 00:33:52 -08001941 if (len <= 0) {
1942 /* nothing to do */
1943 rc = 0;
1944 goto notqueued;
1945 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001946
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001947 /* translate "+++" escape sequence sent as a single separate command
1948 * into "close AT channel" command for error recovery
1949 * The next command will reopen the AT channel automatically.
1950 */
1951 if (len == 3 && !memcmp(buf, "+++", 3)) {
1952 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
1953 goto notqueued;
1954 }
1955
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001956 if (len > IF_WRITEBUF)
1957 len = IF_WRITEBUF;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001958 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
1959 if (!cb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001960 dev_err(cs->dev, "%s: out of memory\n", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001961 rc = -ENOMEM;
1962 goto notqueued;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001963 }
1964
1965 memcpy(cb->buf, buf, len);
1966 cb->len = len;
1967 cb->offset = 0;
1968 cb->next = NULL;
1969 cb->wake_tasklet = wake_tasklet;
1970
1971 spin_lock_irqsave(&cs->cmdlock, flags);
1972 cb->prev = cs->lastcmdbuf;
1973 if (cs->lastcmdbuf)
1974 cs->lastcmdbuf->next = cb;
1975 else {
1976 cs->cmdbuf = cb;
1977 cs->curlen = len;
1978 }
1979 cs->cmdbytes += len;
1980 cs->lastcmdbuf = cb;
1981 spin_unlock_irqrestore(&cs->cmdlock, flags);
1982
Tilman Schmidt73a88812006-04-22 02:35:30 -07001983 spin_lock_irqsave(&cs->lock, flags);
1984 if (unlikely(!cs->connected)) {
1985 spin_unlock_irqrestore(&cs->lock, flags);
1986 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001987 /* flush command queue */
1988 spin_lock_irqsave(&cs->cmdlock, flags);
1989 while (cs->cmdbuf != NULL)
1990 complete_cb(cs);
1991 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001992 return -ENODEV;
1993 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001994 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001995 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001996 return rc < 0 ? rc : len;
1997
1998notqueued: /* request handled without queuing */
1999 if (wake_tasklet)
2000 tasklet_schedule(wake_tasklet);
2001 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002002}
2003
2004/* gigaset_write_room
2005 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07002006 * return number of characters the driver will accept to be written via
2007 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002008 * parameter:
2009 * controller state structure
2010 * return value:
2011 * number of characters
2012 */
2013static int gigaset_write_room(struct cardstate *cs)
2014{
2015 return IF_WRITEBUF;
2016}
2017
2018/* gigaset_chars_in_buffer
2019 * tty_driver.chars_in_buffer interface routine
2020 * return number of characters waiting to be sent
2021 * parameter:
2022 * controller state structure
2023 * return value:
2024 * number of characters
2025 */
2026static int gigaset_chars_in_buffer(struct cardstate *cs)
2027{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07002028 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002029}
2030
2031/* gigaset_brkchars
2032 * implementation of ioctl(GIGASET_BRKCHARS)
2033 * parameter:
2034 * controller state structure
2035 * return value:
2036 * -EINVAL (unimplemented function)
2037 */
2038static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
2039{
2040 return -EINVAL;
2041}
2042
2043
2044/* Device Initialization/Shutdown */
2045/* ============================== */
2046
2047/* Free hardware dependent part of the B channel structure
2048 * parameter:
2049 * bcs B channel structure
2050 * return value:
2051 * !=0 on success
2052 */
2053static int gigaset_freebcshw(struct bc_state *bcs)
2054{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002055 struct bas_bc_state *ubc = bcs->hw.bas;
2056 int i;
2057
2058 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002059 return 0;
2060
Tilman Schmidt73a88812006-04-22 02:35:30 -07002061 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002062 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002063 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2064 for (i = 0; i < BAS_OUTURBS; ++i) {
2065 usb_kill_urb(ubc->isoouturbs[i].urb);
2066 usb_free_urb(ubc->isoouturbs[i].urb);
2067 }
2068 for (i = 0; i < BAS_INURBS; ++i) {
2069 usb_kill_urb(ubc->isoinurbs[i]);
2070 usb_free_urb(ubc->isoinurbs[i]);
2071 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002072 tasklet_kill(&ubc->sent_tasklet);
2073 tasklet_kill(&ubc->rcvd_tasklet);
2074 kfree(ubc->isooutbuf);
2075 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002076 bcs->hw.bas = NULL;
2077 return 1;
2078}
2079
2080/* Initialize hardware dependent part of the B channel structure
2081 * parameter:
2082 * bcs B channel structure
2083 * return value:
2084 * !=0 on success
2085 */
2086static int gigaset_initbcshw(struct bc_state *bcs)
2087{
2088 int i;
2089 struct bas_bc_state *ubc;
2090
2091 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2092 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002093 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002094 return 0;
2095 }
2096
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002097 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002098 atomic_set(&ubc->corrbytes, 0);
2099 spin_lock_init(&ubc->isooutlock);
2100 for (i = 0; i < BAS_OUTURBS; ++i) {
2101 ubc->isoouturbs[i].urb = NULL;
2102 ubc->isoouturbs[i].bcs = bcs;
2103 }
2104 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2105 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002106 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2107 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002108 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002109 kfree(ubc);
2110 bcs->hw.bas = NULL;
2111 return 0;
2112 }
2113 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002114 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002115
2116 spin_lock_init(&ubc->isoinlock);
2117 for (i = 0; i < BAS_INURBS; ++i)
2118 ubc->isoinurbs[i] = NULL;
2119 ubc->isoindone = NULL;
2120 ubc->loststatus = -EINPROGRESS;
2121 ubc->isoinlost = 0;
2122 ubc->seqlen = 0;
2123 ubc->inbyte = 0;
2124 ubc->inbits = 0;
2125 ubc->goodbytes = 0;
2126 ubc->alignerrs = 0;
2127 ubc->fcserrs = 0;
2128 ubc->frameerrs = 0;
2129 ubc->giants = 0;
2130 ubc->runts = 0;
2131 ubc->aborts = 0;
2132 ubc->shared0s = 0;
2133 ubc->stolen0s = 0;
2134 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002135 read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002136 return 1;
2137}
2138
2139static void gigaset_reinitbcshw(struct bc_state *bcs)
2140{
2141 struct bas_bc_state *ubc = bcs->hw.bas;
2142
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002143 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002144 atomic_set(&bcs->hw.bas->corrbytes, 0);
2145 bcs->hw.bas->numsub = 0;
2146 spin_lock_init(&ubc->isooutlock);
2147 spin_lock_init(&ubc->isoinlock);
2148 ubc->loststatus = -EINPROGRESS;
2149}
2150
2151static void gigaset_freecshw(struct cardstate *cs)
2152{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002153 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002154 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002155 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002156 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002157}
2158
2159static int gigaset_initcshw(struct cardstate *cs)
2160{
2161 struct bas_cardstate *ucs;
2162
2163 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002164 if (!ucs) {
2165 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002166 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002167 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002168 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2169 if (!ucs->int_in_buf) {
2170 kfree(ucs);
2171 pr_err("out of memory\n");
2172 return 0;
2173 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002174
2175 ucs->urb_cmd_in = NULL;
2176 ucs->urb_cmd_out = NULL;
2177 ucs->rcvbuf = NULL;
2178 ucs->rcvbuf_size = 0;
2179
2180 spin_lock_init(&ucs->lock);
2181 ucs->pending = 0;
2182
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002183 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002184 init_timer(&ucs->timer_ctrl);
2185 init_timer(&ucs->timer_atrdy);
2186 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002187 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002188
2189 return 1;
2190}
2191
2192/* freeurbs
2193 * unlink and deallocate all URBs unconditionally
2194 * caller must make sure that no commands are still in progress
2195 * parameter:
2196 * cs controller state structure
2197 */
2198static void freeurbs(struct cardstate *cs)
2199{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002200 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002201 struct bas_bc_state *ubc;
2202 int i, j;
2203
Tilman Schmidt39f07222006-12-13 00:33:52 -08002204 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002205 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002207 for (i = 0; i < BAS_OUTURBS; ++i) {
2208 usb_kill_urb(ubc->isoouturbs[i].urb);
2209 usb_free_urb(ubc->isoouturbs[i].urb);
2210 ubc->isoouturbs[i].urb = NULL;
2211 }
2212 for (i = 0; i < BAS_INURBS; ++i) {
2213 usb_kill_urb(ubc->isoinurbs[i]);
2214 usb_free_urb(ubc->isoinurbs[i]);
2215 ubc->isoinurbs[i] = NULL;
2216 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002217 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002218 usb_kill_urb(ucs->urb_int_in);
2219 usb_free_urb(ucs->urb_int_in);
2220 ucs->urb_int_in = NULL;
2221 usb_kill_urb(ucs->urb_cmd_out);
2222 usb_free_urb(ucs->urb_cmd_out);
2223 ucs->urb_cmd_out = NULL;
2224 usb_kill_urb(ucs->urb_cmd_in);
2225 usb_free_urb(ucs->urb_cmd_in);
2226 ucs->urb_cmd_in = NULL;
2227 usb_kill_urb(ucs->urb_ctrl);
2228 usb_free_urb(ucs->urb_ctrl);
2229 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002230}
2231
2232/* gigaset_probe
2233 * This function is called when a new USB device is connected.
2234 * It checks whether the new device is handled by this driver.
2235 */
2236static int gigaset_probe(struct usb_interface *interface,
2237 const struct usb_device_id *id)
2238{
2239 struct usb_host_interface *hostif;
2240 struct usb_device *udev = interface_to_usbdev(interface);
2241 struct cardstate *cs = NULL;
2242 struct bas_cardstate *ucs = NULL;
2243 struct bas_bc_state *ubc;
2244 struct usb_endpoint_descriptor *endpoint;
2245 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002246 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002247
Tilman Schmidt1528b182010-02-22 13:09:52 +00002248 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002249 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2250 __func__, le16_to_cpu(udev->descriptor.idVendor),
2251 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002252
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002253 /* set required alternate setting */
2254 hostif = interface->cur_altsetting;
2255 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002256 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002257 "%s: wrong alternate setting %d - trying to switch",
2258 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002259 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2260 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002261 dev_warn(&udev->dev, "usb_set_interface failed, "
2262 "device %d interface %d altsetting %d\n",
2263 udev->devnum, hostif->desc.bInterfaceNumber,
2264 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002265 return -ENODEV;
2266 }
2267 hostif = interface->cur_altsetting;
2268 }
2269
2270 /* Reject application specific interfaces
2271 */
2272 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002273 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2274 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002275 return -ENODEV;
2276 }
2277
Tilman Schmidt784d5852006-04-10 22:55:04 -07002278 dev_info(&udev->dev,
2279 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2280 __func__, le16_to_cpu(udev->descriptor.idVendor),
2281 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002282
Tilman Schmidte468c042008-02-06 01:38:29 -08002283 /* allocate memory for our device state and intialize it */
2284 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2285 GIGASET_MODULENAME);
2286 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002287 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002288 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002289
2290 /* save off device structure ptrs for later use */
2291 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002292 ucs->udev = udev;
2293 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002294 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002295
2296 /* allocate URBs:
2297 * - one for the interrupt pipe
2298 * - three for the different uses of the default control pipe
2299 * - three for each isochronous pipe
2300 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002301 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2302 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2303 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2304 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002305 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002306
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002307 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002308 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002309 for (i = 0; i < BAS_OUTURBS; ++i)
2310 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002311 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002312 goto allocerr;
2313 for (i = 0; i < BAS_INURBS; ++i)
2314 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002315 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002316 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002317 }
2318
2319 ucs->rcvbuf = NULL;
2320 ucs->rcvbuf_size = 0;
2321
2322 /* Fill the interrupt urb and send it to the core */
2323 endpoint = &hostif->endpoint[0].desc;
2324 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002325 usb_rcvintpipe(udev,
2326 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002327 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002328 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002329 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2330 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002331 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002332 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002333 goto error;
2334 }
2335
2336 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002337 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2338 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002339 goto error;
2340
2341 /* tell common part that the device is ready */
2342 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002343 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002344
2345 /* save address of controller structure */
2346 usb_set_intfdata(interface, cs);
2347
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002348 if (!gigaset_start(cs))
2349 goto error;
2350
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002351 return 0;
2352
Tilman Schmidt73a88812006-04-22 02:35:30 -07002353allocerr:
2354 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002355error:
2356 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002357 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002358 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002359 return -ENODEV;
2360}
2361
2362/* gigaset_disconnect
2363 * This function is called when the Gigaset base is unplugged.
2364 */
2365static void gigaset_disconnect(struct usb_interface *interface)
2366{
2367 struct cardstate *cs;
2368 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002369 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002370
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002371 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002372
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002373 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002374
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002375 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002376
2377 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002378 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002379
2380 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002381 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002382 gigaset_bchannel_down(cs->bcs + j);
2383
2384 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002385 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002386
2387 /* stop timers and URBs, free ressources */
2388 del_timer_sync(&ucs->timer_ctrl);
2389 del_timer_sync(&ucs->timer_atrdy);
2390 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002391 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002392 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002393 kfree(ucs->rcvbuf);
2394 ucs->rcvbuf = NULL;
2395 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002396 usb_put_dev(ucs->udev);
2397 ucs->interface = NULL;
2398 ucs->udev = NULL;
2399 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002400 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002401}
2402
Tilman Schmidt024fd292008-02-06 01:38:26 -08002403/* gigaset_suspend
2404 * This function is called before the USB connection is suspended.
2405 */
2406static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2407{
2408 struct cardstate *cs = usb_get_intfdata(intf);
2409 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002410 int rc;
2411
2412 /* set suspend flag; this stops AT command/response traffic */
2413 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2414 gig_dbg(DEBUG_SUSPEND, "already suspended");
2415 return 0;
2416 }
2417
2418 /* wait a bit for blocking conditions to go away */
2419 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002420 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002421 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2422 BAS_TIMEOUT*HZ/10);
2423 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2424
2425 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002426 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002427 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002428 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002429 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002430 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002431 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002432 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002433 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002434 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002435 dev_warn(cs->dev, " sending AT command\n");
2436 update_basstate(ucs, 0, BS_SUSPEND);
2437 return -EBUSY;
2438 }
2439
2440 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002441 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002442 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2443 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2444 if (rc) {
2445 update_basstate(ucs, 0, BS_SUSPEND);
2446 return rc;
2447 }
2448 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2449 BAS_TIMEOUT*HZ/10);
2450 /* in case of timeout, proceed anyway */
2451 }
2452
2453 /* kill all URBs and timers that might still be pending */
2454 usb_kill_urb(ucs->urb_ctrl);
2455 usb_kill_urb(ucs->urb_int_in);
2456 del_timer_sync(&ucs->timer_ctrl);
2457
2458 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2459 return 0;
2460}
2461
2462/* gigaset_resume
2463 * This function is called after the USB connection has been resumed.
2464 */
2465static int gigaset_resume(struct usb_interface *intf)
2466{
2467 struct cardstate *cs = usb_get_intfdata(intf);
2468 struct bas_cardstate *ucs = cs->hw.bas;
2469 int rc;
2470
2471 /* resubmit interrupt URB for spontaneous messages from base */
2472 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2473 if (rc) {
2474 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2475 get_usb_rcmsg(rc));
2476 return rc;
2477 }
2478
2479 /* clear suspend flag to reallow activity */
2480 update_basstate(ucs, 0, BS_SUSPEND);
2481
2482 gig_dbg(DEBUG_SUSPEND, "resume complete");
2483 return 0;
2484}
2485
2486/* gigaset_pre_reset
2487 * This function is called before the USB connection is reset.
2488 */
2489static int gigaset_pre_reset(struct usb_interface *intf)
2490{
2491 /* handle just like suspend */
2492 return gigaset_suspend(intf, PMSG_ON);
2493}
2494
2495/* gigaset_post_reset
2496 * This function is called after the USB connection has been reset.
2497 */
2498static int gigaset_post_reset(struct usb_interface *intf)
2499{
2500 /* FIXME: send HD_DEVICE_INIT_ACK? */
2501
2502 /* resume operations */
2503 return gigaset_resume(intf);
2504}
2505
2506
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002507static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002508 gigaset_write_cmd,
2509 gigaset_write_room,
2510 gigaset_chars_in_buffer,
2511 gigaset_brkchars,
2512 gigaset_init_bchannel,
2513 gigaset_close_bchannel,
2514 gigaset_initbcshw,
2515 gigaset_freebcshw,
2516 gigaset_reinitbcshw,
2517 gigaset_initcshw,
2518 gigaset_freecshw,
2519 gigaset_set_modem_ctrl,
2520 gigaset_baud_rate,
2521 gigaset_set_line_ctrl,
2522 gigaset_isoc_send_skb,
2523 gigaset_isoc_input,
2524};
2525
2526/* bas_gigaset_init
2527 * This function is called after the kernel module is loaded.
2528 */
2529static int __init bas_gigaset_init(void)
2530{
2531 int result;
2532
2533 /* allocate memory for our driver state and intialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002534 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2535 GIGASET_MODULENAME, GIGASET_DEVNAME,
2536 &gigops, THIS_MODULE);
2537 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002538 goto error;
2539
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002540 /* register this driver with the USB subsystem */
2541 result = usb_register(&gigaset_usb_driver);
2542 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002543 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002544 goto error;
2545 }
2546
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002547 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002548 return 0;
2549
Tilman Schmidte468c042008-02-06 01:38:29 -08002550error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002551 if (driver)
2552 gigaset_freedriver(driver);
2553 driver = NULL;
2554 return -1;
2555}
2556
2557/* bas_gigaset_exit
2558 * This function is called before the kernel module is unloaded.
2559 */
2560static void __exit bas_gigaset_exit(void)
2561{
Tilman Schmidte468c042008-02-06 01:38:29 -08002562 struct bas_cardstate *ucs;
2563 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002564
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002565 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002566 * => no gigaset_start any more
2567 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002568
Tilman Schmidte468c042008-02-06 01:38:29 -08002569 /* stop all connected devices */
2570 for (i = 0; i < driver->minors; i++) {
2571 if (gigaset_shutdown(driver->cs + i) < 0)
2572 continue; /* no device */
2573 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002574
Tilman Schmidte468c042008-02-06 01:38:29 -08002575 /* close all still open channels */
2576 ucs = driver->cs[i].hw.bas;
2577 if (ucs->basstate & BS_B1OPEN) {
2578 gig_dbg(DEBUG_INIT, "closing B1 channel");
2579 usb_control_msg(ucs->udev,
2580 usb_sndctrlpipe(ucs->udev, 0),
2581 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2582 0, 0, NULL, 0, BAS_TIMEOUT);
2583 }
2584 if (ucs->basstate & BS_B2OPEN) {
2585 gig_dbg(DEBUG_INIT, "closing B2 channel");
2586 usb_control_msg(ucs->udev,
2587 usb_sndctrlpipe(ucs->udev, 0),
2588 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2589 0, 0, NULL, 0, BAS_TIMEOUT);
2590 }
2591 if (ucs->basstate & BS_ATOPEN) {
2592 gig_dbg(DEBUG_INIT, "closing AT channel");
2593 usb_control_msg(ucs->udev,
2594 usb_sndctrlpipe(ucs->udev, 0),
2595 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2596 0, 0, NULL, 0, BAS_TIMEOUT);
2597 }
2598 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002599 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002600
2601 /* deregister this driver with the USB subsystem */
2602 usb_deregister(&gigaset_usb_driver);
2603 /* this will call the disconnect-callback */
2604 /* from now on, no disconnect/probe callback should be running */
2605
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002606 gigaset_freedriver(driver);
2607 driver = NULL;
2608}
2609
2610
2611module_init(bas_gigaset_init);
2612module_exit(bas_gigaset_exit);
2613
2614MODULE_AUTHOR(DRIVER_AUTHOR);
2615MODULE_DESCRIPTION(DRIVER_DESC);
2616MODULE_LICENSE("GPL");