blob: 540f6d0bb75422fb7299f23f342e290eaabceff7 [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"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080017#include <linux/usb.h>
18#include <linux/module.h>
19#include <linux/moduleparam.h>
20
21/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070022#define DRIVER_AUTHOR "Tilman Schmidt <tilman@imap.cc>, Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080023#define DRIVER_DESC "USB Driver for Gigaset 307x"
24
25
26/* Module parameters */
27
28static int startmode = SM_ISDN;
29static int cidmode = 1;
30
31module_param(startmode, int, S_IRUGO);
32module_param(cidmode, int, S_IRUGO);
33MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
34MODULE_PARM_DESC(cidmode, "Call-ID mode");
35
36#define GIGASET_MINORS 1
37#define GIGASET_MINOR 16
38#define GIGASET_MODULENAME "bas_gigaset"
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080039#define GIGASET_DEVNAME "ttyGB"
40
Tilman Schmidt73a88812006-04-22 02:35:30 -070041/* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
42#define IF_WRITEBUF 264
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080043
Tilman Schmidt170ebf82009-03-18 23:44:23 -070044/* interrupt pipe message size according to ibid. ch. 2.2 */
45#define IP_MSGSIZE 3
46
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080047/* Values for the Gigaset 307x */
48#define USB_GIGA_VENDOR_ID 0x0681
Tilman Schmidt73a88812006-04-22 02:35:30 -070049#define USB_3070_PRODUCT_ID 0x0001
50#define USB_3075_PRODUCT_ID 0x0002
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080051#define USB_SX303_PRODUCT_ID 0x0021
52#define USB_SX353_PRODUCT_ID 0x0022
53
54/* table of devices that work with this driver */
Tilman Schmidt7891adf2009-10-25 09:30:37 +000055static const struct usb_device_id gigaset_table[] = {
Tilman Schmidt73a88812006-04-22 02:35:30 -070056 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3070_PRODUCT_ID) },
57 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_3075_PRODUCT_ID) },
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080058 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX303_PRODUCT_ID) },
59 { USB_DEVICE(USB_GIGA_VENDOR_ID, USB_SX353_PRODUCT_ID) },
60 { } /* Terminating entry */
61};
62
63MODULE_DEVICE_TABLE(usb, gigaset_table);
64
Tilman Schmidt06163f82006-06-26 00:25:33 -070065/*======================= local function prototypes ==========================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080066
Tilman Schmidt06163f82006-06-26 00:25:33 -070067/* function called if a new device belonging to this driver is connected */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080068static int gigaset_probe(struct usb_interface *interface,
69 const struct usb_device_id *id);
70
71/* Function will be called if the device is unplugged */
72static void gigaset_disconnect(struct usb_interface *interface);
73
Tilman Schmidt024fd292008-02-06 01:38:26 -080074/* functions called before/after suspend */
75static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
76static int gigaset_resume(struct usb_interface *intf);
77
78/* functions called before/after device reset */
79static int gigaset_pre_reset(struct usb_interface *intf);
80static int gigaset_post_reset(struct usb_interface *intf);
81
Tilman Schmidt06163f82006-06-26 00:25:33 -070082static int atread_submit(struct cardstate *, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070083static void stopurbs(struct bas_bc_state *);
Tilman Schmidt06163f82006-06-26 00:25:33 -070084static int req_submit(struct bc_state *, int, int, int);
Tilman Schmidt73a88812006-04-22 02:35:30 -070085static int atwrite_submit(struct cardstate *, unsigned char *, int);
86static int start_cbsend(struct cardstate *);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080087
Tilman Schmidt06163f82006-06-26 00:25:33 -070088/*============================================================================*/
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080089
90struct bas_cardstate {
Tilman Schmidt784d5852006-04-10 22:55:04 -070091 struct usb_device *udev; /* USB device pointer */
92 struct usb_interface *interface; /* interface for this device */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080093 unsigned char minor; /* starting minor number */
94
Tilman Schmidt784d5852006-04-10 22:55:04 -070095 struct urb *urb_ctrl; /* control pipe default URB */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080096 struct usb_ctrlrequest dr_ctrl;
97 struct timer_list timer_ctrl; /* control request timeout */
Tilman Schmidt06163f82006-06-26 00:25:33 -070098 int retry_ctrl;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -080099
100 struct timer_list timer_atrdy; /* AT command ready timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700101 struct urb *urb_cmd_out; /* for sending AT commands */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800102 struct usb_ctrlrequest dr_cmd_out;
103 int retry_cmd_out;
104
Tilman Schmidt784d5852006-04-10 22:55:04 -0700105 struct urb *urb_cmd_in; /* for receiving AT replies */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800106 struct usb_ctrlrequest dr_cmd_in;
107 struct timer_list timer_cmd_in; /* receive request timeout */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700108 unsigned char *rcvbuf; /* AT reply receive buffer */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800109
Tilman Schmidt784d5852006-04-10 22:55:04 -0700110 struct urb *urb_int_in; /* URB for interrupt pipe */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700111 unsigned char *int_in_buf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800112
113 spinlock_t lock; /* locks all following */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800114 int basstate; /* bitmap (BS_*) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800115 int pending; /* uncompleted base request */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800116 wait_queue_head_t waitqueue;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800117 int rcvbuf_size; /* size of AT receive buffer */
118 /* 0: no receive in progress */
119 int retry_cmd_in; /* receive req retry count */
120};
121
122/* status of direct USB connection to 307x base (bits in basstate) */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700123#define BS_ATOPEN 0x001 /* AT channel open */
124#define BS_B1OPEN 0x002 /* B channel 1 open */
125#define BS_B2OPEN 0x004 /* B channel 2 open */
126#define BS_ATREADY 0x008 /* base ready for AT command */
127#define BS_INIT 0x010 /* base has signalled INIT_OK */
128#define BS_ATTIMER 0x020 /* waiting for HD_READY_SEND_ATDATA */
129#define BS_ATRDPEND 0x040 /* urb_cmd_in in use */
130#define BS_ATWRPEND 0x080 /* urb_cmd_out in use */
Tilman Schmidt024fd292008-02-06 01:38:26 -0800131#define BS_SUSPEND 0x100 /* USB port suspended */
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000132#define BS_RESETTING 0x200 /* waiting for HD_RESET_INTERRUPT_PIPE_ACK */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800133
134
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000135static struct gigaset_driver *driver;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800136
137/* usb specific object needed to register this driver with the usb subsystem */
138static struct usb_driver gigaset_usb_driver = {
139 .name = GIGASET_MODULENAME,
140 .probe = gigaset_probe,
141 .disconnect = gigaset_disconnect,
142 .id_table = gigaset_table,
Tilman Schmidt024fd292008-02-06 01:38:26 -0800143 .suspend = gigaset_suspend,
144 .resume = gigaset_resume,
145 .reset_resume = gigaset_post_reset,
146 .pre_reset = gigaset_pre_reset,
147 .post_reset = gigaset_post_reset,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800148};
149
Tilman Schmidt73a88812006-04-22 02:35:30 -0700150/* get message text for usb_submit_urb return code
151 */
152static char *get_usb_rcmsg(int rc)
153{
154 static char unkmsg[28];
155
156 switch (rc) {
157 case 0:
158 return "success";
159 case -ENOMEM:
160 return "out of memory";
161 case -ENODEV:
162 return "device not present";
163 case -ENOENT:
164 return "endpoint not present";
165 case -ENXIO:
166 return "URB type not supported";
167 case -EINVAL:
168 return "invalid argument";
169 case -EAGAIN:
170 return "start frame too early or too much scheduled";
171 case -EFBIG:
172 return "too many isochronous frames requested";
173 case -EPIPE:
174 return "endpoint stalled";
175 case -EMSGSIZE:
176 return "invalid packet size";
177 case -ENOSPC:
178 return "would overcommit USB bandwidth";
179 case -ESHUTDOWN:
180 return "device shut down";
181 case -EPERM:
182 return "reject flag set";
183 case -EHOSTUNREACH:
184 return "device suspended";
185 default:
186 snprintf(unkmsg, sizeof(unkmsg), "unknown error %d", rc);
187 return unkmsg;
188 }
189}
190
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800191/* get message text for USB status code
192 */
193static char *get_usb_statmsg(int status)
194{
195 static char unkmsg[28];
196
197 switch (status) {
198 case 0:
199 return "success";
200 case -ENOENT:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700201 return "unlinked (sync)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800202 case -EINPROGRESS:
203 return "pending";
204 case -EPROTO:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700205 return "bit stuffing error, timeout, or unknown USB error";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800206 case -EILSEQ:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700207 return "CRC mismatch, timeout, or unknown USB error";
Pete Zaitcev38e2bfc2006-09-18 22:49:02 -0700208 case -ETIME:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800209 return "timed out";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700210 case -EPIPE:
211 return "endpoint stalled";
212 case -ECOMM:
213 return "IN buffer overrun";
214 case -ENOSR:
215 return "OUT buffer underrun";
216 case -EOVERFLOW:
217 return "too much data";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800218 case -EREMOTEIO:
219 return "short packet detected";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700220 case -ENODEV:
221 return "device removed";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800222 case -EXDEV:
223 return "partial isochronous transfer";
224 case -EINVAL:
225 return "invalid argument";
Tilman Schmidt73a88812006-04-22 02:35:30 -0700226 case -ECONNRESET:
227 return "unlinked (async)";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800228 case -ESHUTDOWN:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700229 return "device shut down";
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800230 default:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700231 snprintf(unkmsg, sizeof(unkmsg), "unknown status %d", status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800232 return unkmsg;
233 }
234}
235
236/* usb_pipetype_str
237 * retrieve string representation of USB pipe type
238 */
239static inline char *usb_pipetype_str(int pipe)
240{
241 if (usb_pipeisoc(pipe))
242 return "Isoc";
243 if (usb_pipeint(pipe))
244 return "Int";
245 if (usb_pipecontrol(pipe))
246 return "Ctrl";
247 if (usb_pipebulk(pipe))
248 return "Bulk";
249 return "?";
250}
251
252/* dump_urb
253 * write content of URB to syslog for debugging
254 */
255static inline void dump_urb(enum debuglevel level, const char *tag,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700256 struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800257{
258#ifdef CONFIG_GIGASET_DEBUG
259 int i;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700260 gig_dbg(level, "%s urb(0x%08lx)->{", tag, (unsigned long) urb);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800261 if (urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700262 gig_dbg(level,
263 " dev=0x%08lx, pipe=%s:EP%d/DV%d:%s, "
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800264 "hcpriv=0x%08lx, transfer_flags=0x%x,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700265 (unsigned long) urb->dev,
266 usb_pipetype_str(urb->pipe),
267 usb_pipeendpoint(urb->pipe), usb_pipedevice(urb->pipe),
268 usb_pipein(urb->pipe) ? "in" : "out",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800269 (unsigned long) urb->hcpriv,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700270 urb->transfer_flags);
271 gig_dbg(level,
272 " transfer_buffer=0x%08lx[%d], actual_length=%d, "
Andrew Morton249b0612007-02-10 01:46:49 -0800273 "setup_packet=0x%08lx,",
Tilman Schmidt784d5852006-04-10 22:55:04 -0700274 (unsigned long) urb->transfer_buffer,
275 urb->transfer_buffer_length, urb->actual_length,
Andrew Morton249b0612007-02-10 01:46:49 -0800276 (unsigned long) urb->setup_packet);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700277 gig_dbg(level,
278 " start_frame=%d, number_of_packets=%d, interval=%d, "
279 "error_count=%d,",
280 urb->start_frame, urb->number_of_packets, urb->interval,
281 urb->error_count);
282 gig_dbg(level,
283 " context=0x%08lx, complete=0x%08lx, "
284 "iso_frame_desc[]={",
285 (unsigned long) urb->context,
286 (unsigned long) urb->complete);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800287 for (i = 0; i < urb->number_of_packets; i++) {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700288 struct usb_iso_packet_descriptor *pifd
289 = &urb->iso_frame_desc[i];
Tilman Schmidt784d5852006-04-10 22:55:04 -0700290 gig_dbg(level,
291 " {offset=%u, length=%u, actual_length=%u, "
292 "status=%u}",
293 pifd->offset, pifd->length, pifd->actual_length,
294 pifd->status);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800295 }
296 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700297 gig_dbg(level, "}}");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800298#endif
299}
300
301/* read/set modem control bits etc. (m10x only) */
302static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700303 unsigned new_state)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800304{
305 return -EINVAL;
306}
307
308static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
309{
310 return -EINVAL;
311}
312
313static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
314{
315 return -EINVAL;
316}
317
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000318/* set/clear bits in base connection state, return previous state
319 */
320static inline int update_basstate(struct bas_cardstate *ucs,
321 int set, int clear)
322{
323 unsigned long flags;
324 int state;
325
326 spin_lock_irqsave(&ucs->lock, flags);
327 state = ucs->basstate;
328 ucs->basstate = (state & ~clear) | set;
329 spin_unlock_irqrestore(&ucs->lock, flags);
330 return state;
331}
332
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800333/* error_hangup
334 * hang up any existing connection because of an unrecoverable error
335 * This function may be called from any context and takes care of scheduling
336 * the necessary actions for execution outside of interrupt context.
Tilman Schmidt06163f82006-06-26 00:25:33 -0700337 * cs->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800338 * argument:
339 * B channel control structure
340 */
341static inline void error_hangup(struct bc_state *bcs)
342{
343 struct cardstate *cs = bcs->cs;
344
Tilman Schmidt1528b182010-02-22 13:09:52 +0000345 gigaset_add_event(cs, &bcs->at_state, EV_HUP, NULL, 0, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800346 gigaset_schedule_event(cs);
347}
348
349/* error_reset
350 * reset Gigaset device because of an unrecoverable error
Tilman Schmidt06163f82006-06-26 00:25:33 -0700351 * This function may be called from any context, and takes care of
Tilman Schmidt73a88812006-04-22 02:35:30 -0700352 * scheduling the necessary actions for execution outside of interrupt context.
Tilman Schmidt60798c682010-09-30 13:35:21 +0000353 * cs->hw.bas->lock must not be held.
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800354 * argument:
355 * controller state structure
356 */
357static inline void error_reset(struct cardstate *cs)
358{
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000359 /* reset interrupt pipe to recover (ignore errors) */
360 update_basstate(cs->hw.bas, BS_RESETTING, 0);
Tilman Schmidt60798c682010-09-30 13:35:21 +0000361 if (req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT))
362 /* submission failed, escalate to USB port reset */
363 usb_queue_reset_device(cs->hw.bas->interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800364}
365
366/* check_pending
367 * check for completion of pending control request
368 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700369 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800370 */
371static void check_pending(struct bas_cardstate *ucs)
372{
373 unsigned long flags;
374
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800375 spin_lock_irqsave(&ucs->lock, flags);
376 switch (ucs->pending) {
377 case 0:
378 break;
379 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800380 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800381 ucs->pending = 0;
382 break;
383 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800384 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800385 ucs->pending = 0;
386 break;
387 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800388 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800389 ucs->pending = 0;
390 break;
391 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800392 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800393 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800394 break;
395 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800396 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800397 ucs->pending = 0;
398 break;
399 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800400 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800401 ucs->pending = 0;
402 break;
403 case HD_DEVICE_INIT_ACK: /* no reply expected */
404 ucs->pending = 0;
405 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000406 case HD_RESET_INTERRUPT_PIPE:
407 if (!(ucs->basstate & BS_RESETTING))
408 ucs->pending = 0;
409 break;
410 /*
411 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
412 * and should never end up here
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800413 */
414 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700415 dev_warn(&ucs->interface->dev,
416 "unknown pending request 0x%02x cleared\n",
417 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800418 ucs->pending = 0;
419 }
420
421 if (!ucs->pending)
422 del_timer(&ucs->timer_ctrl);
423
424 spin_unlock_irqrestore(&ucs->lock, flags);
425}
426
427/* cmd_in_timeout
428 * timeout routine for command input request
429 * argument:
430 * controller state structure
431 */
432static void cmd_in_timeout(unsigned long data)
433{
434 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700435 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700436 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800437
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800438 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700439 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800440 return;
441 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800442
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000443 if (ucs->retry_cmd_in++ >= BAS_RETRY) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700444 dev_err(cs->dev,
445 "control read: timeout, giving up after %d tries\n",
446 ucs->retry_cmd_in);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000447 kfree(ucs->rcvbuf);
448 ucs->rcvbuf = NULL;
449 ucs->rcvbuf_size = 0;
450 error_reset(cs);
451 return;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700452 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000453
454 gig_dbg(DEBUG_USBREQ, "%s: timeout, retry %d",
455 __func__, ucs->retry_cmd_in);
456 rc = atread_submit(cs, BAS_TIMEOUT);
457 if (rc < 0) {
458 kfree(ucs->rcvbuf);
459 ucs->rcvbuf = NULL;
460 ucs->rcvbuf_size = 0;
461 if (rc != -ENODEV)
462 error_reset(cs);
463 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800464}
465
Tilman Schmidt06163f82006-06-26 00:25:33 -0700466/* read_ctrl_callback
467 * USB completion handler for control pipe input
468 * called by the USB subsystem in interrupt context
469 * parameter:
470 * urb USB request block
471 * urb->context = inbuf structure for controller state
472 */
David Howells7d12e782006-10-05 14:55:46 +0100473static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700474{
475 struct inbuf_t *inbuf = urb->context;
476 struct cardstate *cs = inbuf->cs;
477 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800478 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700479 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 del_timer(&ucs->timer_cmd_in);
485
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800486 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700487 case 0: /* normal completion */
488 numbytes = urb->actual_length;
489 if (unlikely(numbytes != ucs->rcvbuf_size)) {
490 dev_warn(cs->dev,
491 "control read: received %d chars, expected %d\n",
492 numbytes, ucs->rcvbuf_size);
493 if (numbytes > ucs->rcvbuf_size)
494 numbytes = ucs->rcvbuf_size;
495 }
496
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000497 /* copy received bytes to inbuf, notify event layer */
498 if (gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes)) {
499 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
500 gigaset_schedule_event(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700501 }
502 break;
503
504 case -ENOENT: /* cancelled */
505 case -ECONNRESET: /* cancelled (async) */
506 case -EINPROGRESS: /* pending */
507 case -ENODEV: /* device removed */
508 case -ESHUTDOWN: /* device shut down */
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000509 /* no further action necessary */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700510 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800511 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700512 break;
513
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000514 default: /* other errors: retry */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700515 if (ucs->retry_cmd_in++ < BAS_RETRY) {
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000516 gig_dbg(DEBUG_USBREQ, "%s: %s, retry %d", __func__,
517 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700518 rc = atread_submit(cs, BAS_TIMEOUT);
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000519 if (rc >= 0)
520 /* successfully resubmitted, skip freeing */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700521 return;
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000522 if (rc == -ENODEV)
523 /* disconnect, no further action necessary */
524 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700525 }
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000526 dev_err(cs->dev, "control read: %s, giving up after %d tries\n",
527 get_usb_statmsg(status), ucs->retry_cmd_in);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700528 error_reset(cs);
529 }
530
Tilman Schmidtc8701a02010-09-30 13:34:40 +0000531 /* read finished, free buffer */
Tilman Schmidt06163f82006-06-26 00:25:33 -0700532 kfree(ucs->rcvbuf);
533 ucs->rcvbuf = NULL;
534 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700535}
536
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800537/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700538 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800539 * parameters:
540 * cs controller state structure
541 * timeout timeout in 1/10 sec., 0: none
542 * return value:
543 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800544 * -EBUSY if another request is pending
545 * any URB submission error code
546 */
547static int atread_submit(struct cardstate *cs, int timeout)
548{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700549 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800550 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800551 int ret;
552
Tilman Schmidt784d5852006-04-10 22:55:04 -0700553 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
554 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800555
Tilman Schmidt024fd292008-02-06 01:38:26 -0800556 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
557 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700558 dev_err(cs->dev,
559 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800560 return -EBUSY;
561 }
562
Tilman Schmidt024fd292008-02-06 01:38:26 -0800563 if (basstate & BS_SUSPEND) {
564 dev_notice(cs->dev,
565 "HD_READ_ATMESSAGE not submitted, "
566 "suspend in progress\n");
567 update_basstate(ucs, 0, BS_ATRDPEND);
568 /* treat like disconnect */
569 return -ENODEV;
570 }
571
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800572 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
573 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
574 ucs->dr_cmd_in.wValue = 0;
575 ucs->dr_cmd_in.wIndex = 0;
576 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
577 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700578 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000579 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700580 ucs->rcvbuf, ucs->rcvbuf_size,
581 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800582
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000583 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
584 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700585 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700586 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700587 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800588 return ret;
589 }
590
591 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700592 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +0000593 mod_timer(&ucs->timer_cmd_in, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800594 }
595 return 0;
596}
597
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800598/* read_int_callback
599 * USB completion handler for interrupt pipe input
600 * called by the USB subsystem in interrupt context
601 * parameter:
602 * urb USB request block
603 * urb->context = controller state structure
604 */
David Howells7d12e782006-10-05 14:55:46 +0100605static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800606{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700607 struct cardstate *cs = urb->context;
608 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800609 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800610 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800611 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700612 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800613 unsigned l;
614 int channel;
615
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800616 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800617 case 0: /* success */
618 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700619 case -ENOENT: /* cancelled */
620 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800621 case -EINPROGRESS: /* pending */
622 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700623 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800624 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800625 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700626 case -ENODEV: /* device removed */
627 case -ESHUTDOWN: /* device shut down */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700628 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
629 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800630 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700631 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800632 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800633 goto resubmit;
634 }
635
Tilman Schmidt73a88812006-04-22 02:35:30 -0700636 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700637 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700638 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
639 urb->actual_length);
640 goto resubmit;
641 }
642
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800643 l = (unsigned) ucs->int_in_buf[1] +
644 (((unsigned) ucs->int_in_buf[2]) << 8);
645
Tilman Schmidt784d5852006-04-10 22:55:04 -0700646 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
647 urb->actual_length, (int)ucs->int_in_buf[0], l,
648 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800649
650 channel = 0;
651
652 switch (ucs->int_in_buf[0]) {
653 case HD_DEVICE_INIT_OK:
654 update_basstate(ucs, BS_INIT, 0);
655 break;
656
657 case HD_READY_SEND_ATDATA:
658 del_timer(&ucs->timer_atrdy);
659 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
660 start_cbsend(cs);
661 break;
662
663 case HD_OPEN_B2CHANNEL_ACK:
664 ++channel;
665 case HD_OPEN_B1CHANNEL_ACK:
666 bcs = cs->bcs + channel;
667 update_basstate(ucs, BS_B1OPEN << channel, 0);
668 gigaset_bchannel_up(bcs);
669 break;
670
671 case HD_OPEN_ATCHANNEL_ACK:
672 update_basstate(ucs, BS_ATOPEN, 0);
673 start_cbsend(cs);
674 break;
675
676 case HD_CLOSE_B2CHANNEL_ACK:
677 ++channel;
678 case HD_CLOSE_B1CHANNEL_ACK:
679 bcs = cs->bcs + channel;
680 update_basstate(ucs, 0, BS_B1OPEN << channel);
681 stopurbs(bcs->hw.bas);
682 gigaset_bchannel_down(bcs);
683 break;
684
685 case HD_CLOSE_ATCHANNEL_ACK:
686 update_basstate(ucs, 0, BS_ATOPEN);
687 break;
688
689 case HD_B2_FLOW_CONTROL:
690 ++channel;
691 case HD_B1_FLOW_CONTROL:
692 bcs = cs->bcs + channel;
693 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700694 &bcs->hw.bas->corrbytes);
695 gig_dbg(DEBUG_ISO,
696 "Flow control (channel %d, sub %d): 0x%02x => %d",
697 channel, bcs->hw.bas->numsub, l,
698 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800699 break;
700
701 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
702 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700703 dev_warn(cs->dev,
704 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800705 break;
706 }
707 spin_lock_irqsave(&cs->lock, flags);
708 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700709 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700710 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700711 "receive AT data overrun, %d bytes lost\n",
712 ucs->rcvbuf_size);
713 kfree(ucs->rcvbuf);
714 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800715 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000716 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
717 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800718 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700719 dev_err(cs->dev, "out of memory receiving AT data\n");
720 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800721 break;
722 }
723 ucs->rcvbuf_size = l;
724 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000725 rc = atread_submit(cs, BAS_TIMEOUT);
726 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800727 kfree(ucs->rcvbuf);
728 ucs->rcvbuf = NULL;
729 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700730 if (rc != -ENODEV) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700731 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700732 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700733 break;
734 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800735 }
736 spin_unlock_irqrestore(&cs->lock, flags);
737 break;
738
739 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000740 update_basstate(ucs, 0, BS_RESETTING);
741 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800742 break;
743
744 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700745 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800746 break;
747
748 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700749 dev_warn(cs->dev,
750 "unknown Gigaset signal 0x%02x (%u) ignored\n",
751 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800752 }
753
754 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800755 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800756
757resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800758 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700759 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700760 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700761 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800762 error_reset(cs);
763 }
764}
765
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800766/* read_iso_callback
767 * USB completion handler for B channel isochronous input
768 * called by the USB subsystem in interrupt context
769 * parameter:
770 * urb USB request block of completed request
771 * urb->context = bc_state structure
772 */
David Howells7d12e782006-10-05 14:55:46 +0100773static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800774{
775 struct bc_state *bcs;
776 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800777 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800778 unsigned long flags;
779 int i, rc;
780
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800781 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800782 if (unlikely(status == -ENOENT ||
783 status == -ECONNRESET ||
784 status == -EINPROGRESS ||
785 status == -ENODEV ||
786 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700787 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800788 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800789 return;
790 }
791
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700792 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800793 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800794
795 spin_lock_irqsave(&ubc->isoinlock, flags);
796 if (likely(ubc->isoindone == NULL)) {
797 /* pass URB to tasklet */
798 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800799 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000800 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800801 } else {
802 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800803 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800804 for (i = 0; i < BAS_NUMFRAMES; i++) {
805 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
806 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700807 urb->iso_frame_desc[i].status !=
808 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800809 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800810 urb->iso_frame_desc[i].status = 0;
811 urb->iso_frame_desc[i].actual_length = 0;
812 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800813 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700814 /* urb->dev is clobbered by USB subsystem */
815 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800816 urb->transfer_flags = URB_ISO_ASAP;
817 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700818 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
819 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800820 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700821 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700822 dev_err(bcs->cs->dev,
823 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700824 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800825 dump_urb(DEBUG_ISO, "isoc read", urb);
826 error_hangup(bcs);
827 }
828 }
829 }
830 spin_unlock_irqrestore(&ubc->isoinlock, flags);
831}
832
833/* write_iso_callback
834 * USB completion handler for B channel isochronous output
835 * called by the USB subsystem in interrupt context
836 * parameter:
837 * urb USB request block of completed request
838 * urb->context = isow_urbctx_t structure
839 */
David Howells7d12e782006-10-05 14:55:46 +0100840static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800841{
842 struct isow_urbctx_t *ucx;
843 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800844 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800845 unsigned long flags;
846
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800847 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800848 if (unlikely(status == -ENOENT ||
849 status == -ECONNRESET ||
850 status == -EINPROGRESS ||
851 status == -ENODEV ||
852 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700853 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800854 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800855 return;
856 }
857
858 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700859 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800860 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800861 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800862
863 spin_lock_irqsave(&ubc->isooutlock, flags);
864 ubc->isooutovfl = ubc->isooutdone;
865 ubc->isooutdone = ucx;
866 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000867 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800868}
869
870/* starturbs
871 * prepare and submit USB request blocks for isochronous input and output
872 * argument:
873 * B channel control structure
874 * return value:
875 * 0 on success
876 * < 0 on error (no URBs submitted)
877 */
878static int starturbs(struct bc_state *bcs)
879{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700880 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800881 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800882 int j, k;
883 int rc;
884
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800885 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000886 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800887 bcs->inputstate |= INS_flag_hunt;
888
889 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800890 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800891 for (k = 0; k < BAS_INURBS; k++) {
892 urb = ubc->isoinurbs[k];
893 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800894 rc = -EFAULT;
895 goto error;
896 }
897
898 urb->dev = bcs->cs->hw.bas->udev;
899 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
900 urb->transfer_flags = URB_ISO_ASAP;
901 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
902 urb->transfer_buffer_length = BAS_INBUFSIZE;
903 urb->number_of_packets = BAS_NUMFRAMES;
904 urb->interval = BAS_FRAMETIME;
905 urb->complete = read_iso_callback;
906 urb->context = bcs;
907 for (j = 0; j < BAS_NUMFRAMES; j++) {
908 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
909 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
910 urb->iso_frame_desc[j].status = 0;
911 urb->iso_frame_desc[j].actual_length = 0;
912 }
913
914 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000915 rc = usb_submit_urb(urb, GFP_ATOMIC);
916 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800917 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800918 }
919
920 /* initialize L2 transmission */
921 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
922
923 /* set up isochronous output URBs for flag idling */
924 for (k = 0; k < BAS_OUTURBS; ++k) {
925 urb = ubc->isoouturbs[k].urb;
926 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800927 rc = -EFAULT;
928 goto error;
929 }
930 urb->dev = bcs->cs->hw.bas->udev;
931 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
932 urb->transfer_flags = URB_ISO_ASAP;
933 urb->transfer_buffer = ubc->isooutbuf->data;
934 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
935 urb->number_of_packets = BAS_NUMFRAMES;
936 urb->interval = BAS_FRAMETIME;
937 urb->complete = write_iso_callback;
938 urb->context = &ubc->isoouturbs[k];
939 for (j = 0; j < BAS_NUMFRAMES; ++j) {
940 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
941 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
942 urb->iso_frame_desc[j].status = 0;
943 urb->iso_frame_desc[j].actual_length = 0;
944 }
945 ubc->isoouturbs[k].limit = -1;
946 }
947
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800948 /* keep one URB free, submit the others */
949 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800950 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800951 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700952 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800953 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800954 }
955 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800956 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800957 ubc->isooutdone = ubc->isooutovfl = NULL;
958 return 0;
959 error:
960 stopurbs(ubc);
961 return rc;
962}
963
964/* stopurbs
965 * cancel the USB request blocks for isochronous input and output
966 * errors are silently ignored
967 * argument:
968 * B channel control structure
969 */
970static void stopurbs(struct bas_bc_state *ubc)
971{
972 int k, rc;
973
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800974 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800975
976 for (k = 0; k < BAS_INURBS; ++k) {
977 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700978 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700979 "%s: isoc input URB %d unlinked, result = %s",
980 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800981 }
982
983 for (k = 0; k < BAS_OUTURBS; ++k) {
984 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700985 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700986 "%s: isoc output URB %d unlinked, result = %s",
987 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800988 }
989}
990
991/* Isochronous Write - Bottom Half */
992/* =============================== */
993
994/* submit_iso_write_urb
995 * fill and submit the next isochronous write URB
996 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -0700997 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800998 * return value:
999 * number of frames submitted in URB
1000 * 0 if URB not submitted because no data available (isooutbuf busy)
1001 * error code < 0 on error
1002 */
1003static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1004{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001005 struct urb *urb = ucx->urb;
1006 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001007 struct usb_iso_packet_descriptor *ifd;
1008 int corrbytes, nframe, rc;
1009
Tilman Schmidt784d5852006-04-10 22:55:04 -07001010 /* urb->dev is clobbered by USB subsystem */
1011 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001012 urb->transfer_flags = URB_ISO_ASAP;
1013 urb->transfer_buffer = ubc->isooutbuf->data;
1014 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1015
1016 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1017 ifd = &urb->iso_frame_desc[nframe];
1018
1019 /* compute frame length according to flow control */
1020 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001021 corrbytes = atomic_read(&ubc->corrbytes);
1022 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001023 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1024 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001025 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1026 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1027 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1028 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1029 ifd->length += corrbytes;
1030 atomic_add(-corrbytes, &ubc->corrbytes);
1031 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001032
1033 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001034 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1035 if (rc < 0) {
1036 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001037 gig_dbg(DEBUG_ISO,
1038 "%s: buffer busy at frame %d",
1039 __func__, nframe);
1040 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001041 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001042 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001043 dev_err(ucx->bcs->cs->dev,
1044 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001045 __func__, rc, nframe);
1046 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001047 }
1048 break;
1049 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001050 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001051 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001052 ifd->status = 0;
1053 ifd->actual_length = 0;
1054 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001055 if (unlikely(nframe == 0))
1056 return 0; /* no data to send */
1057 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001058
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001059 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001060 if (unlikely(rc)) {
1061 if (rc == -ENODEV)
1062 /* device removed - give up silently */
1063 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1064 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001065 dev_err(ucx->bcs->cs->dev,
1066 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001067 get_usb_rcmsg(rc));
1068 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001069 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001070 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001071 return nframe;
1072}
1073
1074/* write_iso_tasklet
1075 * tasklet scheduled when an isochronous output URB from the Gigaset device
1076 * has completed
1077 * parameter:
1078 * data B channel state structure
1079 */
1080static void write_iso_tasklet(unsigned long data)
1081{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001082 struct bc_state *bcs = (struct bc_state *) data;
1083 struct bas_bc_state *ubc = bcs->hw.bas;
1084 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001085 struct isow_urbctx_t *done, *next, *ovfl;
1086 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001087 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001088 struct usb_iso_packet_descriptor *ifd;
1089 int offset;
1090 unsigned long flags;
1091 int i;
1092 struct sk_buff *skb;
1093 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001094 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001095
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001096 /* loop while completed URBs arrive in time */
1097 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001098 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001099 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001100 return;
1101 }
1102
1103 /* retrieve completed URBs */
1104 spin_lock_irqsave(&ubc->isooutlock, flags);
1105 done = ubc->isooutdone;
1106 ubc->isooutdone = NULL;
1107 ovfl = ubc->isooutovfl;
1108 ubc->isooutovfl = NULL;
1109 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1110 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001111 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001112 error_hangup(bcs);
1113 break;
1114 }
1115 if (!done)
1116 break;
1117
1118 /* submit free URB if available */
1119 spin_lock_irqsave(&ubc->isooutlock, flags);
1120 next = ubc->isooutfree;
1121 ubc->isooutfree = NULL;
1122 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1123 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001124 rc = submit_iso_write_urb(next);
1125 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001126 /* could not submit URB, put it back */
1127 spin_lock_irqsave(&ubc->isooutlock, flags);
1128 if (ubc->isooutfree == NULL) {
1129 ubc->isooutfree = next;
1130 next = NULL;
1131 }
1132 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1133 if (next) {
1134 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001135 dev_err(cs->dev,
1136 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001137 error_hangup(bcs);
1138 }
1139 }
1140 }
1141
1142 /* process completed URB */
1143 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001144 status = done->status;
1145 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001146 case -EXDEV: /* partial completion */
1147 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1148 __func__);
1149 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001150 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001151 /* inspect individual frames
1152 * assumptions (for lack of documentation):
1153 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001154 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001155 * - all following frames are not sent at all
1156 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001157 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001158 for (i = 0; i < BAS_NUMFRAMES; i++) {
1159 ifd = &urb->iso_frame_desc[i];
1160 if (ifd->status ||
1161 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001162 dev_warn(cs->dev,
1163 "isochronous write: frame %d: %s, "
1164 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001165 i, get_usb_statmsg(ifd->status),
1166 ifd->actual_length, ifd->length);
1167 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001168 ifd->actual_length)
1169 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001170 break;
1171 }
1172 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001173 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001174 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001175 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001176 error_hangup(bcs);
1177 break;
1178 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001179 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001180 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001181 }
1182
1183 /* mark the write buffer area covered by this URB as free */
1184 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001185 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001186
1187 /* mark URB as free */
1188 spin_lock_irqsave(&ubc->isooutlock, flags);
1189 next = ubc->isooutfree;
1190 ubc->isooutfree = done;
1191 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1192 if (next) {
1193 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001194 rc = submit_iso_write_urb(next);
1195 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001196 /* couldn't submit */
1197 error_hangup(bcs);
1198 }
1199 }
1200 }
1201
1202 /* process queued SKBs */
1203 while ((skb = skb_dequeue(&bcs->squeue))) {
1204 /* copy to output buffer, doing L2 encapsulation */
1205 len = skb->len;
1206 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1207 /* insufficient buffer space, push back onto queue */
1208 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001209 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1210 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001211 break;
1212 }
1213 skb_pull(skb, len);
1214 gigaset_skb_sent(bcs, skb);
1215 dev_kfree_skb_any(skb);
1216 }
1217}
1218
1219/* Isochronous Read - Bottom Half */
1220/* ============================== */
1221
1222/* read_iso_tasklet
1223 * tasklet scheduled when an isochronous input URB from the Gigaset device
1224 * has completed
1225 * parameter:
1226 * data B channel state structure
1227 */
1228static void read_iso_tasklet(unsigned long data)
1229{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001230 struct bc_state *bcs = (struct bc_state *) data;
1231 struct bas_bc_state *ubc = bcs->hw.bas;
1232 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001233 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001234 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001235 char *rcvbuf;
1236 unsigned long flags;
1237 int totleft, numbytes, offset, frame, rc;
1238
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001239 /* loop while more completed URBs arrive in the meantime */
1240 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001241 /* retrieve URB */
1242 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001243 urb = ubc->isoindone;
1244 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001245 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1246 return;
1247 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001248 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001249 ubc->isoindone = NULL;
1250 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001251 dev_warn(cs->dev,
1252 "isochronous read overrun, "
1253 "dropped URB with status: %s, %d bytes lost\n",
1254 get_usb_statmsg(ubc->loststatus),
1255 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001256 ubc->loststatus = -EINPROGRESS;
1257 }
1258 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1259
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001260 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001261 gig_dbg(DEBUG_ISO,
1262 "%s: channel not running, "
1263 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001264 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001265 return;
1266 }
1267
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001268 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001269 case 0: /* normal completion */
1270 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001271 case -EXDEV: /* inspect individual frames
1272 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001273 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1274 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001275 break;
1276 case -ENOENT:
1277 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001278 case -EINPROGRESS:
1279 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001280 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001281 continue; /* -> skip */
1282 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001283 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001284 error_hangup(bcs);
1285 continue; /* -> skip */
1286 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001287 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001288 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001289 goto error;
1290 }
1291
1292 rcvbuf = urb->transfer_buffer;
1293 totleft = urb->actual_length;
1294 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001295 numbytes = urb->iso_frame_desc[frame].actual_length;
1296 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001297 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001298 "isochronous read: frame %d[%d]: %s\n",
1299 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001300 get_usb_statmsg(
1301 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001302 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001303 dev_warn(cs->dev,
1304 "isochronous read: frame %d: "
1305 "numbytes (%d) > BAS_MAXFRAME\n",
1306 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001307 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001308 dev_warn(cs->dev,
1309 "isochronous read: frame %d: "
1310 "numbytes (%d) > totleft (%d)\n",
1311 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001312 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001313 }
1314 offset = urb->iso_frame_desc[frame].offset;
1315 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001316 dev_warn(cs->dev,
1317 "isochronous read: frame %d: "
1318 "offset (%d) + numbytes (%d) "
1319 "> BAS_INBUFSIZE\n",
1320 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001321 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001322 }
1323 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1324 totleft -= numbytes;
1325 }
1326 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001327 dev_warn(cs->dev,
1328 "isochronous read: %d data bytes missing\n",
1329 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001330
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001331error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001332 /* URB processed, resubmit */
1333 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1334 urb->iso_frame_desc[frame].status = 0;
1335 urb->iso_frame_desc[frame].actual_length = 0;
1336 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001337 /* urb->dev is clobbered by USB subsystem */
1338 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001339 urb->transfer_flags = URB_ISO_ASAP;
1340 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001341 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001342 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001343 dev_err(cs->dev,
1344 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001345 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001346 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1347 error_hangup(bcs);
1348 }
1349 }
1350}
1351
1352/* Channel Operations */
1353/* ================== */
1354
1355/* req_timeout
1356 * timeout routine for control output request
1357 * argument:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001358 * controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001359 */
1360static void req_timeout(unsigned long data)
1361{
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001362 struct cardstate *cs = (struct cardstate *) data;
1363 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001364 int pending;
1365 unsigned long flags;
1366
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001367 check_pending(ucs);
1368
1369 spin_lock_irqsave(&ucs->lock, flags);
1370 pending = ucs->pending;
1371 ucs->pending = 0;
1372 spin_unlock_irqrestore(&ucs->lock, flags);
1373
1374 switch (pending) {
1375 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001376 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001377 break;
1378
1379 case HD_OPEN_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001380 dev_err(cs->dev, "timeout opening AT channel\n");
1381 error_reset(cs);
1382 break;
1383
1384 case HD_OPEN_B1CHANNEL:
1385 dev_err(cs->dev, "timeout opening channel 1\n");
1386 error_hangup(&cs->bcs[0]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001387 break;
1388
1389 case HD_OPEN_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001390 dev_err(cs->dev, "timeout opening channel 2\n");
1391 error_hangup(&cs->bcs[1]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001392 break;
1393
1394 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001395 dev_err(cs->dev, "timeout closing AT channel\n");
1396 error_reset(cs);
1397 break;
1398
1399 case HD_CLOSE_B1CHANNEL:
1400 dev_err(cs->dev, "timeout closing channel 1\n");
1401 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001402 break;
1403
1404 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001405 dev_err(cs->dev, "timeout closing channel 2\n");
1406 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001407 break;
1408
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001409 case HD_RESET_INTERRUPT_PIPE:
1410 /* error recovery escalation */
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001411 dev_err(cs->dev,
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001412 "reset interrupt pipe timeout, attempting USB reset\n");
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001413 usb_queue_reset_device(ucs->interface);
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001414 break;
1415
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001416 default:
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001417 dev_warn(cs->dev, "request 0x%02x timed out, clearing\n",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001418 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001419 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001420
1421 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001422}
1423
1424/* write_ctrl_callback
1425 * USB completion handler for control pipe output
1426 * called by the USB subsystem in interrupt context
1427 * parameter:
1428 * urb USB request block of completed request
1429 * urb->context = hardware specific controller state structure
1430 */
David Howells7d12e782006-10-05 14:55:46 +01001431static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001432{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001433 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001434 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001435 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001436 unsigned long flags;
1437
Tilman Schmidt06163f82006-06-26 00:25:33 -07001438 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001439 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001440 case 0: /* normal completion */
1441 spin_lock_irqsave(&ucs->lock, flags);
1442 switch (ucs->pending) {
1443 case HD_DEVICE_INIT_ACK: /* no reply expected */
1444 del_timer(&ucs->timer_ctrl);
1445 ucs->pending = 0;
1446 break;
1447 }
1448 spin_unlock_irqrestore(&ucs->lock, flags);
1449 return;
1450
1451 case -ENOENT: /* cancelled */
1452 case -ECONNRESET: /* cancelled (async) */
1453 case -EINPROGRESS: /* pending */
1454 case -ENODEV: /* device removed */
1455 case -ESHUTDOWN: /* device shut down */
1456 /* ignore silently */
1457 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001458 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001459 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001460
1461 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001462 /* don't retry if suspend requested */
1463 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001464 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001465 dev_err(&ucs->interface->dev,
1466 "control request 0x%02x failed: %s\n",
1467 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001468 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001469 break; /* give up */
1470 }
1471 dev_notice(&ucs->interface->dev,
1472 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001473 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001474 ucs->retry_ctrl);
1475 /* urb->dev is clobbered by USB subsystem */
1476 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001477 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001478 if (unlikely(rc)) {
1479 dev_err(&ucs->interface->dev,
1480 "could not resubmit request 0x%02x: %s\n",
1481 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1482 break;
1483 }
1484 /* resubmitted */
1485 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001486 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001487
1488 /* failed, clear pending request */
1489 spin_lock_irqsave(&ucs->lock, flags);
1490 del_timer(&ucs->timer_ctrl);
1491 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001492 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001493 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001494}
1495
1496/* req_submit
1497 * submit a control output request without message buffer to the Gigaset base
1498 * and optionally start a timeout
1499 * parameters:
1500 * bcs B channel control structure
1501 * req control request code (HD_*)
1502 * val control request parameter value (set to 0 if unused)
1503 * timeout timeout in seconds (0: no timeout)
1504 * return value:
1505 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001506 * -EBUSY if another request is pending
1507 * any URB submission error code
1508 */
1509static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1510{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001511 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001512 int ret;
1513 unsigned long flags;
1514
Tilman Schmidt784d5852006-04-10 22:55:04 -07001515 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001516
1517 spin_lock_irqsave(&ucs->lock, flags);
1518 if (ucs->pending) {
1519 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001520 dev_err(bcs->cs->dev,
1521 "submission of request 0x%02x failed: "
1522 "request 0x%02x still pending\n",
1523 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001524 return -EBUSY;
1525 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001526
1527 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1528 ucs->dr_ctrl.bRequest = req;
1529 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1530 ucs->dr_ctrl.wIndex = 0;
1531 ucs->dr_ctrl.wLength = 0;
1532 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001533 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001534 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001535 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001536 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001537 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001538 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001539 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001540 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001541 spin_unlock_irqrestore(&ucs->lock, flags);
1542 return ret;
1543 }
1544 ucs->pending = req;
1545
1546 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001547 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001548 mod_timer(&ucs->timer_ctrl, jiffies + timeout * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001549 }
1550
1551 spin_unlock_irqrestore(&ucs->lock, flags);
1552 return 0;
1553}
1554
1555/* gigaset_init_bchannel
1556 * called by common.c to connect a B channel
1557 * initialize isochronous I/O and tell the Gigaset base to open the channel
1558 * argument:
1559 * B channel control structure
1560 * return value:
1561 * 0 on success, error code < 0 on error
1562 */
1563static int gigaset_init_bchannel(struct bc_state *bcs)
1564{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001565 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001566 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001567 unsigned long flags;
1568
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001569 spin_lock_irqsave(&cs->lock, flags);
1570 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001571 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001572 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001573 return -ENODEV;
1574 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001575
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001576 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001577 dev_notice(cs->dev,
1578 "not starting isochronous I/O, "
1579 "suspend in progress\n");
1580 spin_unlock_irqrestore(&cs->lock, flags);
1581 return -EHOSTUNREACH;
1582 }
1583
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001584 ret = starturbs(bcs);
1585 if (ret < 0) {
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001586 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001587 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001588 "could not start isochronous I/O for channel B%d: %s\n",
1589 bcs->channel + 1,
1590 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1591 if (ret != -ENODEV)
1592 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001593 return ret;
1594 }
1595
1596 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001597 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1598 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001599 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001600 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001601 stopurbs(bcs->hw.bas);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001602 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001603
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001604 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001605 if (ret < 0 && ret != -ENODEV)
1606 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001607 return ret;
1608}
1609
1610/* gigaset_close_bchannel
1611 * called by common.c to disconnect a B channel
1612 * tell the Gigaset base to close the channel
1613 * stopping isochronous I/O and LL notification will be done when the
1614 * acknowledgement for the close arrives
1615 * argument:
1616 * B channel control structure
1617 * return value:
1618 * 0 on success, error code < 0 on error
1619 */
1620static int gigaset_close_bchannel(struct bc_state *bcs)
1621{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001622 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001623 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001624 unsigned long flags;
1625
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001626 spin_lock_irqsave(&cs->lock, flags);
1627 if (unlikely(!cs->connected)) {
1628 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001629 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1630 return -ENODEV;
1631 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001632
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001633 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001634 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001635 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001636 gigaset_bchannel_down(bcs);
1637 return 0;
1638 }
1639
Tilman Schmidt73a88812006-04-22 02:35:30 -07001640 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001641 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001642 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1643 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001644 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001645 bcs->channel + 1);
1646
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001647 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001648 return ret;
1649}
1650
1651/* Device Operations */
1652/* ================= */
1653
1654/* complete_cb
1655 * unqueue first command buffer from queue, waking any sleepers
1656 * must be called with cs->cmdlock held
1657 * parameter:
1658 * cs controller state structure
1659 */
1660static void complete_cb(struct cardstate *cs)
1661{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001662 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001663
1664 /* unqueue completed buffer */
1665 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001666 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001667 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001668 if (cb->next != NULL) {
1669 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001670 cs->cmdbuf->prev = NULL;
1671 cs->curlen = cs->cmdbuf->len;
1672 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001673 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001674 cs->lastcmdbuf = NULL;
1675 cs->curlen = 0;
1676 }
1677
1678 if (cb->wake_tasklet)
1679 tasklet_schedule(cb->wake_tasklet);
1680
1681 kfree(cb);
1682}
1683
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001684/* write_command_callback
1685 * USB completion handler for AT command transmission
1686 * called by the USB subsystem in interrupt context
1687 * parameter:
1688 * urb USB request block of completed request
1689 * urb->context = controller state structure
1690 */
David Howells7d12e782006-10-05 14:55:46 +01001691static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001692{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001693 struct cardstate *cs = urb->context;
1694 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001695 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001696 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001697
Tilman Schmidt73a88812006-04-22 02:35:30 -07001698 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001699 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001700
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001701 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001702 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001703 case 0: /* normal completion */
1704 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001705 case -ENOENT: /* cancelled */
1706 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001707 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001708 case -ENODEV: /* device removed */
1709 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001710 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001711 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001712 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001713 return;
1714 default: /* any failure */
1715 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001716 dev_warn(cs->dev,
1717 "command write: %s, "
1718 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001719 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001720 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001721 break;
1722 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001723 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001724 dev_warn(cs->dev,
1725 "command write: %s, "
1726 "won't retry - suspend requested\n",
1727 get_usb_statmsg(status));
1728 break;
1729 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001730 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001731 dev_warn(cs->dev,
1732 "command write: %s, "
1733 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001734 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001735 break;
1736 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001737 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001738 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001739 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1740 /* resubmitted - bypass regular exit block */
1741 return;
1742 /* command send failed, assume base still waiting */
1743 update_basstate(ucs, BS_ATREADY, 0);
1744 }
1745
1746 spin_lock_irqsave(&cs->cmdlock, flags);
1747 if (cs->cmdbuf != NULL)
1748 complete_cb(cs);
1749 spin_unlock_irqrestore(&cs->cmdlock, flags);
1750}
1751
1752/* atrdy_timeout
1753 * timeout routine for AT command transmission
1754 * argument:
1755 * controller state structure
1756 */
1757static void atrdy_timeout(unsigned long data)
1758{
1759 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001760 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001761
Tilman Schmidt784d5852006-04-10 22:55:04 -07001762 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001763
1764 /* fake the missing signal - what else can I do? */
1765 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1766 start_cbsend(cs);
1767}
1768
1769/* atwrite_submit
1770 * submit an HD_WRITE_ATMESSAGE command URB
1771 * parameters:
1772 * cs controller state structure
1773 * buf buffer containing command to send
1774 * len length of command to send
1775 * return value:
1776 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001777 * -EBUSY if another request is pending
1778 * any URB submission error code
1779 */
1780static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1781{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001782 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001783 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001784
Tilman Schmidt784d5852006-04-10 22:55:04 -07001785 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001786
Tilman Schmidt73a88812006-04-22 02:35:30 -07001787 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001788 dev_err(cs->dev,
1789 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001790 return -EBUSY;
1791 }
1792
1793 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1794 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1795 ucs->dr_cmd_out.wValue = 0;
1796 ucs->dr_cmd_out.wIndex = 0;
1797 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1798 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1799 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001800 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001801 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001802 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001803 if (unlikely(rc)) {
1804 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001805 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001806 get_usb_rcmsg(rc));
1807 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001808 }
1809
Tilman Schmidt73a88812006-04-22 02:35:30 -07001810 /* submitted successfully, start timeout if necessary */
1811 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001812 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1813 ATRDY_TIMEOUT);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00001814 mod_timer(&ucs->timer_atrdy, jiffies + ATRDY_TIMEOUT * HZ / 10);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001815 }
1816 return 0;
1817}
1818
1819/* start_cbsend
1820 * start transmission of AT command queue if necessary
1821 * parameter:
1822 * cs controller state structure
1823 * return value:
1824 * 0 on success
1825 * error code < 0 on error
1826 */
1827static int start_cbsend(struct cardstate *cs)
1828{
1829 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001830 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001831 unsigned long flags;
1832 int rc;
1833 int retval = 0;
1834
Tilman Schmidt024fd292008-02-06 01:38:26 -08001835 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001836 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001837 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001838 return -EHOSTUNREACH;
1839 }
1840
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001841 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001842 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001843 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001844 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1845 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001846 /* flush command queue */
1847 spin_lock_irqsave(&cs->cmdlock, flags);
1848 while (cs->cmdbuf != NULL)
1849 complete_cb(cs);
1850 spin_unlock_irqrestore(&cs->cmdlock, flags);
1851 }
1852 return rc;
1853 }
1854
1855 /* try to send first command in queue */
1856 spin_lock_irqsave(&cs->cmdlock, flags);
1857
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001858 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001859 ucs->retry_cmd_out = 0;
1860 rc = atwrite_submit(cs, cb->buf, cb->len);
1861 if (unlikely(rc)) {
1862 retval = rc;
1863 complete_cb(cs);
1864 }
1865 }
1866
1867 spin_unlock_irqrestore(&cs->cmdlock, flags);
1868 return retval;
1869}
1870
1871/* gigaset_write_cmd
1872 * This function is called by the device independent part of the driver
1873 * to transmit an AT command string to the Gigaset device.
1874 * It encapsulates the device specific method for transmission over the
1875 * direct USB connection to the base.
1876 * The command string is added to the queue of commands to send, and
1877 * USB transmission is started if necessary.
1878 * parameters:
1879 * cs controller state structure
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001880 * cb command buffer structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001881 * return value:
1882 * number of bytes queued on success
1883 * error code < 0 on error
1884 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001885static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001886{
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001887 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001888 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001889
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001890 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001891 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001892 "CMD Transmit", cb->len, cb->buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001893
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001894 /* translate "+++" escape sequence sent as a single separate command
1895 * into "close AT channel" command for error recovery
1896 * The next command will reopen the AT channel automatically.
1897 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001898 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
Tilman Schmidt4cb5e422010-09-30 13:35:31 +00001899 /* If an HD_RECEIVEATDATA_ACK message remains unhandled
1900 * because of an error, the base never sends another one.
1901 * The response channel is thus effectively blocked.
1902 * Closing and reopening the AT channel does *not* clear
1903 * this condition.
1904 * As a stopgap measure, submit a zero-length AT read
1905 * before closing the AT channel. This has the undocumented
1906 * effect of triggering a new HD_RECEIVEATDATA_ACK message
1907 * from the base if necessary.
1908 * The subsequent AT channel close then discards any pending
1909 * messages.
1910 */
1911 spin_lock_irqsave(&cs->lock, flags);
1912 if (!(cs->hw.bas->basstate & BS_ATRDPEND)) {
1913 kfree(cs->hw.bas->rcvbuf);
1914 cs->hw.bas->rcvbuf = NULL;
1915 cs->hw.bas->rcvbuf_size = 0;
1916 cs->hw.bas->retry_cmd_in = 0;
1917 atread_submit(cs, 0);
1918 }
1919 spin_unlock_irqrestore(&cs->lock, flags);
1920
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001921 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001922 if (cb->wake_tasklet)
1923 tasklet_schedule(cb->wake_tasklet);
Dan Carpenter8bcfbd02010-08-05 22:21:26 +00001924 if (!rc)
1925 rc = cb->len;
1926 kfree(cb);
1927 return rc;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001928 }
1929
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001930 spin_lock_irqsave(&cs->cmdlock, flags);
1931 cb->prev = cs->lastcmdbuf;
1932 if (cs->lastcmdbuf)
1933 cs->lastcmdbuf->next = cb;
1934 else {
1935 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001936 cs->curlen = cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001937 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001938 cs->cmdbytes += cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001939 cs->lastcmdbuf = cb;
1940 spin_unlock_irqrestore(&cs->cmdlock, flags);
1941
Tilman Schmidt73a88812006-04-22 02:35:30 -07001942 spin_lock_irqsave(&cs->lock, flags);
1943 if (unlikely(!cs->connected)) {
1944 spin_unlock_irqrestore(&cs->lock, flags);
1945 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001946 /* flush command queue */
1947 spin_lock_irqsave(&cs->cmdlock, flags);
1948 while (cs->cmdbuf != NULL)
1949 complete_cb(cs);
1950 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001951 return -ENODEV;
1952 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001953 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001954 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001955 return rc < 0 ? rc : cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001956}
1957
1958/* gigaset_write_room
1959 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001960 * return number of characters the driver will accept to be written via
1961 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001962 * parameter:
1963 * controller state structure
1964 * return value:
1965 * number of characters
1966 */
1967static int gigaset_write_room(struct cardstate *cs)
1968{
1969 return IF_WRITEBUF;
1970}
1971
1972/* gigaset_chars_in_buffer
1973 * tty_driver.chars_in_buffer interface routine
1974 * return number of characters waiting to be sent
1975 * parameter:
1976 * controller state structure
1977 * return value:
1978 * number of characters
1979 */
1980static int gigaset_chars_in_buffer(struct cardstate *cs)
1981{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07001982 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001983}
1984
1985/* gigaset_brkchars
1986 * implementation of ioctl(GIGASET_BRKCHARS)
1987 * parameter:
1988 * controller state structure
1989 * return value:
1990 * -EINVAL (unimplemented function)
1991 */
1992static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1993{
1994 return -EINVAL;
1995}
1996
1997
1998/* Device Initialization/Shutdown */
1999/* ============================== */
2000
2001/* Free hardware dependent part of the B channel structure
2002 * parameter:
2003 * bcs B channel structure
2004 * return value:
2005 * !=0 on success
2006 */
2007static int gigaset_freebcshw(struct bc_state *bcs)
2008{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002009 struct bas_bc_state *ubc = bcs->hw.bas;
2010 int i;
2011
2012 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002013 return 0;
2014
Tilman Schmidt73a88812006-04-22 02:35:30 -07002015 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002016 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002017 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2018 for (i = 0; i < BAS_OUTURBS; ++i) {
2019 usb_kill_urb(ubc->isoouturbs[i].urb);
2020 usb_free_urb(ubc->isoouturbs[i].urb);
2021 }
2022 for (i = 0; i < BAS_INURBS; ++i) {
2023 usb_kill_urb(ubc->isoinurbs[i]);
2024 usb_free_urb(ubc->isoinurbs[i]);
2025 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002026 tasklet_kill(&ubc->sent_tasklet);
2027 tasklet_kill(&ubc->rcvd_tasklet);
2028 kfree(ubc->isooutbuf);
2029 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002030 bcs->hw.bas = NULL;
2031 return 1;
2032}
2033
2034/* Initialize hardware dependent part of the B channel structure
2035 * parameter:
2036 * bcs B channel structure
2037 * return value:
2038 * !=0 on success
2039 */
2040static int gigaset_initbcshw(struct bc_state *bcs)
2041{
2042 int i;
2043 struct bas_bc_state *ubc;
2044
2045 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2046 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002047 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002048 return 0;
2049 }
2050
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002051 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002052 atomic_set(&ubc->corrbytes, 0);
2053 spin_lock_init(&ubc->isooutlock);
2054 for (i = 0; i < BAS_OUTURBS; ++i) {
2055 ubc->isoouturbs[i].urb = NULL;
2056 ubc->isoouturbs[i].bcs = bcs;
2057 }
2058 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2059 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002060 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2061 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002062 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002063 kfree(ubc);
2064 bcs->hw.bas = NULL;
2065 return 0;
2066 }
2067 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002068 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002069
2070 spin_lock_init(&ubc->isoinlock);
2071 for (i = 0; i < BAS_INURBS; ++i)
2072 ubc->isoinurbs[i] = NULL;
2073 ubc->isoindone = NULL;
2074 ubc->loststatus = -EINPROGRESS;
2075 ubc->isoinlost = 0;
2076 ubc->seqlen = 0;
2077 ubc->inbyte = 0;
2078 ubc->inbits = 0;
2079 ubc->goodbytes = 0;
2080 ubc->alignerrs = 0;
2081 ubc->fcserrs = 0;
2082 ubc->frameerrs = 0;
2083 ubc->giants = 0;
2084 ubc->runts = 0;
2085 ubc->aborts = 0;
2086 ubc->shared0s = 0;
2087 ubc->stolen0s = 0;
2088 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002089 read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002090 return 1;
2091}
2092
2093static void gigaset_reinitbcshw(struct bc_state *bcs)
2094{
2095 struct bas_bc_state *ubc = bcs->hw.bas;
2096
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002097 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002098 atomic_set(&bcs->hw.bas->corrbytes, 0);
2099 bcs->hw.bas->numsub = 0;
2100 spin_lock_init(&ubc->isooutlock);
2101 spin_lock_init(&ubc->isoinlock);
2102 ubc->loststatus = -EINPROGRESS;
2103}
2104
2105static void gigaset_freecshw(struct cardstate *cs)
2106{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002107 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002108 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002109 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002110 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002111}
2112
2113static int gigaset_initcshw(struct cardstate *cs)
2114{
2115 struct bas_cardstate *ucs;
2116
2117 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002118 if (!ucs) {
2119 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002120 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002121 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002122 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2123 if (!ucs->int_in_buf) {
2124 kfree(ucs);
2125 pr_err("out of memory\n");
2126 return 0;
2127 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002128
2129 ucs->urb_cmd_in = NULL;
2130 ucs->urb_cmd_out = NULL;
2131 ucs->rcvbuf = NULL;
2132 ucs->rcvbuf_size = 0;
2133
2134 spin_lock_init(&ucs->lock);
2135 ucs->pending = 0;
2136
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002137 ucs->basstate = 0;
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002138 setup_timer(&ucs->timer_ctrl, req_timeout, (unsigned long) cs);
2139 setup_timer(&ucs->timer_atrdy, atrdy_timeout, (unsigned long) cs);
2140 setup_timer(&ucs->timer_cmd_in, cmd_in_timeout, (unsigned long) cs);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002141 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002142
2143 return 1;
2144}
2145
2146/* freeurbs
2147 * unlink and deallocate all URBs unconditionally
2148 * caller must make sure that no commands are still in progress
2149 * parameter:
2150 * cs controller state structure
2151 */
2152static void freeurbs(struct cardstate *cs)
2153{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002154 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002155 struct bas_bc_state *ubc;
2156 int i, j;
2157
Tilman Schmidt39f07222006-12-13 00:33:52 -08002158 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002159 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002160 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002161 for (i = 0; i < BAS_OUTURBS; ++i) {
2162 usb_kill_urb(ubc->isoouturbs[i].urb);
2163 usb_free_urb(ubc->isoouturbs[i].urb);
2164 ubc->isoouturbs[i].urb = NULL;
2165 }
2166 for (i = 0; i < BAS_INURBS; ++i) {
2167 usb_kill_urb(ubc->isoinurbs[i]);
2168 usb_free_urb(ubc->isoinurbs[i]);
2169 ubc->isoinurbs[i] = NULL;
2170 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002171 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002172 usb_kill_urb(ucs->urb_int_in);
2173 usb_free_urb(ucs->urb_int_in);
2174 ucs->urb_int_in = NULL;
2175 usb_kill_urb(ucs->urb_cmd_out);
2176 usb_free_urb(ucs->urb_cmd_out);
2177 ucs->urb_cmd_out = NULL;
2178 usb_kill_urb(ucs->urb_cmd_in);
2179 usb_free_urb(ucs->urb_cmd_in);
2180 ucs->urb_cmd_in = NULL;
2181 usb_kill_urb(ucs->urb_ctrl);
2182 usb_free_urb(ucs->urb_ctrl);
2183 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002184}
2185
2186/* gigaset_probe
2187 * This function is called when a new USB device is connected.
2188 * It checks whether the new device is handled by this driver.
2189 */
2190static int gigaset_probe(struct usb_interface *interface,
2191 const struct usb_device_id *id)
2192{
2193 struct usb_host_interface *hostif;
2194 struct usb_device *udev = interface_to_usbdev(interface);
2195 struct cardstate *cs = NULL;
2196 struct bas_cardstate *ucs = NULL;
2197 struct bas_bc_state *ubc;
2198 struct usb_endpoint_descriptor *endpoint;
2199 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002200 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002201
Tilman Schmidt1528b182010-02-22 13:09:52 +00002202 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002203 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2204 __func__, le16_to_cpu(udev->descriptor.idVendor),
2205 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002206
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002207 /* set required alternate setting */
2208 hostif = interface->cur_altsetting;
2209 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002210 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002211 "%s: wrong alternate setting %d - trying to switch",
2212 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002213 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2214 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002215 dev_warn(&udev->dev, "usb_set_interface failed, "
2216 "device %d interface %d altsetting %d\n",
2217 udev->devnum, hostif->desc.bInterfaceNumber,
2218 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002219 return -ENODEV;
2220 }
2221 hostif = interface->cur_altsetting;
2222 }
2223
2224 /* Reject application specific interfaces
2225 */
2226 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002227 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2228 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002229 return -ENODEV;
2230 }
2231
Tilman Schmidt784d5852006-04-10 22:55:04 -07002232 dev_info(&udev->dev,
2233 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2234 __func__, le16_to_cpu(udev->descriptor.idVendor),
2235 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002236
Tilman Schmidte468c042008-02-06 01:38:29 -08002237 /* allocate memory for our device state and intialize it */
2238 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2239 GIGASET_MODULENAME);
2240 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002241 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002242 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002243
2244 /* save off device structure ptrs for later use */
2245 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002246 ucs->udev = udev;
2247 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002248 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002249
2250 /* allocate URBs:
2251 * - one for the interrupt pipe
2252 * - three for the different uses of the default control pipe
2253 * - three for each isochronous pipe
2254 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002255 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2256 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2257 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2258 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002259 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002260
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002261 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002262 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002263 for (i = 0; i < BAS_OUTURBS; ++i)
2264 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002265 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002266 goto allocerr;
2267 for (i = 0; i < BAS_INURBS; ++i)
2268 if (!(ubc->isoinurbs[i] =
Christoph Lametere94b1762006-12-06 20:33:17 -08002269 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002270 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002271 }
2272
2273 ucs->rcvbuf = NULL;
2274 ucs->rcvbuf_size = 0;
2275
2276 /* Fill the interrupt urb and send it to the core */
2277 endpoint = &hostif->endpoint[0].desc;
2278 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002279 usb_rcvintpipe(udev,
2280 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002281 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002282 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002283 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2284 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002285 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002286 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002287 goto error;
2288 }
2289
2290 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002291 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2292 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002293 goto error;
2294
2295 /* tell common part that the device is ready */
2296 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002297 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002298
2299 /* save address of controller structure */
2300 usb_set_intfdata(interface, cs);
2301
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002302 if (!gigaset_start(cs))
2303 goto error;
2304
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002305 return 0;
2306
Tilman Schmidt73a88812006-04-22 02:35:30 -07002307allocerr:
2308 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002309error:
2310 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002311 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002312 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002313 return -ENODEV;
2314}
2315
2316/* gigaset_disconnect
2317 * This function is called when the Gigaset base is unplugged.
2318 */
2319static void gigaset_disconnect(struct usb_interface *interface)
2320{
2321 struct cardstate *cs;
2322 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002323 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002324
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002325 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002326
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002327 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002328
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002329 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002330
2331 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002332 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002333
2334 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002335 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002336 gigaset_bchannel_down(cs->bcs + j);
2337
2338 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002339 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002340
2341 /* stop timers and URBs, free ressources */
2342 del_timer_sync(&ucs->timer_ctrl);
2343 del_timer_sync(&ucs->timer_atrdy);
2344 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002345 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002346 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002347 kfree(ucs->rcvbuf);
2348 ucs->rcvbuf = NULL;
2349 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002350 usb_put_dev(ucs->udev);
2351 ucs->interface = NULL;
2352 ucs->udev = NULL;
2353 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002354 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002355}
2356
Tilman Schmidt024fd292008-02-06 01:38:26 -08002357/* gigaset_suspend
2358 * This function is called before the USB connection is suspended.
2359 */
2360static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2361{
2362 struct cardstate *cs = usb_get_intfdata(intf);
2363 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002364 int rc;
2365
2366 /* set suspend flag; this stops AT command/response traffic */
2367 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2368 gig_dbg(DEBUG_SUSPEND, "already suspended");
2369 return 0;
2370 }
2371
2372 /* wait a bit for blocking conditions to go away */
2373 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002374 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002375 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2376 BAS_TIMEOUT*HZ/10);
2377 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2378
2379 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002380 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002381 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002382 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002383 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002384 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002385 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002386 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002387 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002388 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002389 dev_warn(cs->dev, " sending AT command\n");
2390 update_basstate(ucs, 0, BS_SUSPEND);
2391 return -EBUSY;
2392 }
2393
2394 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002395 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002396 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2397 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2398 if (rc) {
2399 update_basstate(ucs, 0, BS_SUSPEND);
2400 return rc;
2401 }
2402 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2403 BAS_TIMEOUT*HZ/10);
2404 /* in case of timeout, proceed anyway */
2405 }
2406
2407 /* kill all URBs and timers that might still be pending */
2408 usb_kill_urb(ucs->urb_ctrl);
2409 usb_kill_urb(ucs->urb_int_in);
2410 del_timer_sync(&ucs->timer_ctrl);
Tilman Schmidt1d5a9ed2010-09-30 13:35:11 +00002411 del_timer_sync(&ucs->timer_atrdy);
2412 del_timer_sync(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002413
2414 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2415 return 0;
2416}
2417
2418/* gigaset_resume
2419 * This function is called after the USB connection has been resumed.
2420 */
2421static int gigaset_resume(struct usb_interface *intf)
2422{
2423 struct cardstate *cs = usb_get_intfdata(intf);
2424 struct bas_cardstate *ucs = cs->hw.bas;
2425 int rc;
2426
2427 /* resubmit interrupt URB for spontaneous messages from base */
2428 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2429 if (rc) {
2430 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2431 get_usb_rcmsg(rc));
2432 return rc;
2433 }
2434
2435 /* clear suspend flag to reallow activity */
2436 update_basstate(ucs, 0, BS_SUSPEND);
2437
2438 gig_dbg(DEBUG_SUSPEND, "resume complete");
2439 return 0;
2440}
2441
2442/* gigaset_pre_reset
2443 * This function is called before the USB connection is reset.
2444 */
2445static int gigaset_pre_reset(struct usb_interface *intf)
2446{
2447 /* handle just like suspend */
2448 return gigaset_suspend(intf, PMSG_ON);
2449}
2450
2451/* gigaset_post_reset
2452 * This function is called after the USB connection has been reset.
2453 */
2454static int gigaset_post_reset(struct usb_interface *intf)
2455{
2456 /* FIXME: send HD_DEVICE_INIT_ACK? */
2457
2458 /* resume operations */
2459 return gigaset_resume(intf);
2460}
2461
2462
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002463static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002464 gigaset_write_cmd,
2465 gigaset_write_room,
2466 gigaset_chars_in_buffer,
2467 gigaset_brkchars,
2468 gigaset_init_bchannel,
2469 gigaset_close_bchannel,
2470 gigaset_initbcshw,
2471 gigaset_freebcshw,
2472 gigaset_reinitbcshw,
2473 gigaset_initcshw,
2474 gigaset_freecshw,
2475 gigaset_set_modem_ctrl,
2476 gigaset_baud_rate,
2477 gigaset_set_line_ctrl,
2478 gigaset_isoc_send_skb,
2479 gigaset_isoc_input,
2480};
2481
2482/* bas_gigaset_init
2483 * This function is called after the kernel module is loaded.
2484 */
2485static int __init bas_gigaset_init(void)
2486{
2487 int result;
2488
2489 /* allocate memory for our driver state and intialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002490 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2491 GIGASET_MODULENAME, GIGASET_DEVNAME,
2492 &gigops, THIS_MODULE);
2493 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002494 goto error;
2495
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002496 /* register this driver with the USB subsystem */
2497 result = usb_register(&gigaset_usb_driver);
2498 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002499 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002500 goto error;
2501 }
2502
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002503 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002504 return 0;
2505
Tilman Schmidte468c042008-02-06 01:38:29 -08002506error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002507 if (driver)
2508 gigaset_freedriver(driver);
2509 driver = NULL;
2510 return -1;
2511}
2512
2513/* bas_gigaset_exit
2514 * This function is called before the kernel module is unloaded.
2515 */
2516static void __exit bas_gigaset_exit(void)
2517{
Tilman Schmidte468c042008-02-06 01:38:29 -08002518 struct bas_cardstate *ucs;
2519 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002520
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002521 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002522 * => no gigaset_start any more
2523 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002524
Tilman Schmidte468c042008-02-06 01:38:29 -08002525 /* stop all connected devices */
2526 for (i = 0; i < driver->minors; i++) {
2527 if (gigaset_shutdown(driver->cs + i) < 0)
2528 continue; /* no device */
2529 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002530
Tilman Schmidte468c042008-02-06 01:38:29 -08002531 /* close all still open channels */
2532 ucs = driver->cs[i].hw.bas;
2533 if (ucs->basstate & BS_B1OPEN) {
2534 gig_dbg(DEBUG_INIT, "closing B1 channel");
2535 usb_control_msg(ucs->udev,
2536 usb_sndctrlpipe(ucs->udev, 0),
2537 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2538 0, 0, NULL, 0, BAS_TIMEOUT);
2539 }
2540 if (ucs->basstate & BS_B2OPEN) {
2541 gig_dbg(DEBUG_INIT, "closing B2 channel");
2542 usb_control_msg(ucs->udev,
2543 usb_sndctrlpipe(ucs->udev, 0),
2544 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2545 0, 0, NULL, 0, BAS_TIMEOUT);
2546 }
2547 if (ucs->basstate & BS_ATOPEN) {
2548 gig_dbg(DEBUG_INIT, "closing AT channel");
2549 usb_control_msg(ucs->udev,
2550 usb_sndctrlpipe(ucs->udev, 0),
2551 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2552 0, 0, NULL, 0, BAS_TIMEOUT);
2553 }
2554 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002555 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002556
2557 /* deregister this driver with the USB subsystem */
2558 usb_deregister(&gigaset_usb_driver);
2559 /* this will call the disconnect-callback */
2560 /* from now on, no disconnect/probe callback should be running */
2561
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002562 gigaset_freedriver(driver);
2563 driver = NULL;
2564}
2565
2566
2567module_init(bas_gigaset_init);
2568module_exit(bas_gigaset_exit);
2569
2570MODULE_AUTHOR(DRIVER_AUTHOR);
2571MODULE_DESCRIPTION(DRIVER_DESC);
2572MODULE_LICENSE("GPL");