blob: e143050e1b5ccb42aba74ce3ca314c706dd01043 [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 Schmidt06163f82006-06-26 00:25:33 -0700353 * cs->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);
361 req_submit(cs->bcs, HD_RESET_INTERRUPT_PIPE, 0, BAS_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800362}
363
364/* check_pending
365 * check for completion of pending control request
366 * parameter:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700367 * ucs hardware specific controller state structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800368 */
369static void check_pending(struct bas_cardstate *ucs)
370{
371 unsigned long flags;
372
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800373 spin_lock_irqsave(&ucs->lock, flags);
374 switch (ucs->pending) {
375 case 0:
376 break;
377 case HD_OPEN_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800378 if (ucs->basstate & BS_ATOPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800379 ucs->pending = 0;
380 break;
381 case HD_OPEN_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800382 if (ucs->basstate & BS_B1OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800383 ucs->pending = 0;
384 break;
385 case HD_OPEN_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800386 if (ucs->basstate & BS_B2OPEN)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800387 ucs->pending = 0;
388 break;
389 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800390 if (!(ucs->basstate & BS_ATOPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800391 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800392 break;
393 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800394 if (!(ucs->basstate & BS_B1OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800395 ucs->pending = 0;
396 break;
397 case HD_CLOSE_B2CHANNEL:
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800398 if (!(ucs->basstate & BS_B2OPEN))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800399 ucs->pending = 0;
400 break;
401 case HD_DEVICE_INIT_ACK: /* no reply expected */
402 ucs->pending = 0;
403 break;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000404 case HD_RESET_INTERRUPT_PIPE:
405 if (!(ucs->basstate & BS_RESETTING))
406 ucs->pending = 0;
407 break;
408 /*
409 * HD_READ_ATMESSAGE and HD_WRITE_ATMESSAGE are handled separately
410 * and should never end up here
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800411 */
412 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700413 dev_warn(&ucs->interface->dev,
414 "unknown pending request 0x%02x cleared\n",
415 ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800416 ucs->pending = 0;
417 }
418
419 if (!ucs->pending)
420 del_timer(&ucs->timer_ctrl);
421
422 spin_unlock_irqrestore(&ucs->lock, flags);
423}
424
425/* cmd_in_timeout
426 * timeout routine for command input request
427 * argument:
428 * controller state structure
429 */
430static void cmd_in_timeout(unsigned long data)
431{
432 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700433 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700434 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800435
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800436 if (!ucs->rcvbuf_size) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700437 gig_dbg(DEBUG_USBREQ, "%s: no receive in progress", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800438 return;
439 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800440
Tilman Schmidt06163f82006-06-26 00:25:33 -0700441 if (ucs->retry_cmd_in++ < BAS_RETRY) {
442 dev_notice(cs->dev, "control read: timeout, retry %d\n",
443 ucs->retry_cmd_in);
444 rc = atread_submit(cs, BAS_TIMEOUT);
445 if (rc >= 0 || rc == -ENODEV)
446 /* resubmitted or disconnected */
447 /* - bypass regular exit block */
448 return;
449 } else {
450 dev_err(cs->dev,
451 "control read: timeout, giving up after %d tries\n",
452 ucs->retry_cmd_in);
453 }
454 kfree(ucs->rcvbuf);
455 ucs->rcvbuf = NULL;
456 ucs->rcvbuf_size = 0;
457 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800458}
459
Tilman Schmidt06163f82006-06-26 00:25:33 -0700460/* read_ctrl_callback
461 * USB completion handler for control pipe input
462 * called by the USB subsystem in interrupt context
463 * parameter:
464 * urb USB request block
465 * urb->context = inbuf structure for controller state
466 */
David Howells7d12e782006-10-05 14:55:46 +0100467static void read_ctrl_callback(struct urb *urb)
Tilman Schmidt06163f82006-06-26 00:25:33 -0700468{
469 struct inbuf_t *inbuf = urb->context;
470 struct cardstate *cs = inbuf->cs;
471 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800472 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700473 int have_data = 0;
474 unsigned numbytes;
475 int rc;
476
477 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800478 wake_up(&ucs->waitqueue);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700479
480 if (!ucs->rcvbuf_size) {
481 dev_warn(cs->dev, "%s: no receive in progress\n", __func__);
482 return;
483 }
484
485 del_timer(&ucs->timer_cmd_in);
486
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800487 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700488 case 0: /* normal completion */
489 numbytes = urb->actual_length;
490 if (unlikely(numbytes != ucs->rcvbuf_size)) {
491 dev_warn(cs->dev,
492 "control read: received %d chars, expected %d\n",
493 numbytes, ucs->rcvbuf_size);
494 if (numbytes > ucs->rcvbuf_size)
495 numbytes = ucs->rcvbuf_size;
496 }
497
498 /* copy received bytes to inbuf */
499 have_data = gigaset_fill_inbuf(inbuf, ucs->rcvbuf, numbytes);
500
501 if (unlikely(numbytes < ucs->rcvbuf_size)) {
502 /* incomplete - resubmit for remaining bytes */
503 ucs->rcvbuf_size -= numbytes;
504 ucs->retry_cmd_in = 0;
505 rc = atread_submit(cs, BAS_TIMEOUT);
506 if (rc >= 0 || rc == -ENODEV)
507 /* resubmitted or disconnected */
508 /* - bypass regular exit block */
509 return;
510 error_reset(cs);
511 }
512 break;
513
514 case -ENOENT: /* cancelled */
515 case -ECONNRESET: /* cancelled (async) */
516 case -EINPROGRESS: /* pending */
517 case -ENODEV: /* device removed */
518 case -ESHUTDOWN: /* device shut down */
519 /* no action necessary */
520 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800521 __func__, get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700522 break;
523
524 default: /* severe trouble */
525 dev_warn(cs->dev, "control read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800526 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -0700527 if (ucs->retry_cmd_in++ < BAS_RETRY) {
528 dev_notice(cs->dev, "control read: retry %d\n",
529 ucs->retry_cmd_in);
530 rc = atread_submit(cs, BAS_TIMEOUT);
531 if (rc >= 0 || rc == -ENODEV)
532 /* resubmitted or disconnected */
533 /* - bypass regular exit block */
534 return;
535 } else {
536 dev_err(cs->dev,
537 "control read: giving up after %d tries\n",
538 ucs->retry_cmd_in);
539 }
540 error_reset(cs);
541 }
542
543 kfree(ucs->rcvbuf);
544 ucs->rcvbuf = NULL;
545 ucs->rcvbuf_size = 0;
546 if (have_data) {
547 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
548 gigaset_schedule_event(cs);
549 }
550}
551
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800552/* atread_submit
Tilman Schmidt73a88812006-04-22 02:35:30 -0700553 * submit an HD_READ_ATMESSAGE command URB and optionally start a timeout
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800554 * parameters:
555 * cs controller state structure
556 * timeout timeout in 1/10 sec., 0: none
557 * return value:
558 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800559 * -EBUSY if another request is pending
560 * any URB submission error code
561 */
562static int atread_submit(struct cardstate *cs, int timeout)
563{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700564 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -0800565 int basstate;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800566 int ret;
567
Tilman Schmidt784d5852006-04-10 22:55:04 -0700568 gig_dbg(DEBUG_USBREQ, "-------> HD_READ_ATMESSAGE (%d)",
569 ucs->rcvbuf_size);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800570
Tilman Schmidt024fd292008-02-06 01:38:26 -0800571 basstate = update_basstate(ucs, BS_ATRDPEND, 0);
572 if (basstate & BS_ATRDPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700573 dev_err(cs->dev,
574 "could not submit HD_READ_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800575 return -EBUSY;
576 }
577
Tilman Schmidt024fd292008-02-06 01:38:26 -0800578 if (basstate & BS_SUSPEND) {
579 dev_notice(cs->dev,
580 "HD_READ_ATMESSAGE not submitted, "
581 "suspend in progress\n");
582 update_basstate(ucs, 0, BS_ATRDPEND);
583 /* treat like disconnect */
584 return -ENODEV;
585 }
586
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800587 ucs->dr_cmd_in.bRequestType = IN_VENDOR_REQ;
588 ucs->dr_cmd_in.bRequest = HD_READ_ATMESSAGE;
589 ucs->dr_cmd_in.wValue = 0;
590 ucs->dr_cmd_in.wIndex = 0;
591 ucs->dr_cmd_in.wLength = cpu_to_le16(ucs->rcvbuf_size);
592 usb_fill_control_urb(ucs->urb_cmd_in, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700593 usb_rcvctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000594 (unsigned char *) &ucs->dr_cmd_in,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700595 ucs->rcvbuf, ucs->rcvbuf_size,
596 read_ctrl_callback, cs->inbuf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800597
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000598 ret = usb_submit_urb(ucs->urb_cmd_in, GFP_ATOMIC);
599 if (ret != 0) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700600 update_basstate(ucs, 0, BS_ATRDPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700601 dev_err(cs->dev, "could not submit HD_READ_ATMESSAGE: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -0700602 get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800603 return ret;
604 }
605
606 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700607 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800608 ucs->timer_cmd_in.expires = jiffies + timeout * HZ / 10;
609 ucs->timer_cmd_in.data = (unsigned long) cs;
610 ucs->timer_cmd_in.function = cmd_in_timeout;
611 add_timer(&ucs->timer_cmd_in);
612 }
613 return 0;
614}
615
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800616/* read_int_callback
617 * USB completion handler for interrupt pipe input
618 * called by the USB subsystem in interrupt context
619 * parameter:
620 * urb USB request block
621 * urb->context = controller state structure
622 */
David Howells7d12e782006-10-05 14:55:46 +0100623static void read_int_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800624{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700625 struct cardstate *cs = urb->context;
626 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800627 struct bc_state *bcs;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800628 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800629 unsigned long flags;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700630 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800631 unsigned l;
632 int channel;
633
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800634 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800635 case 0: /* success */
636 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700637 case -ENOENT: /* cancelled */
638 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800639 case -EINPROGRESS: /* pending */
640 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700641 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800642 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800643 return;
Tilman Schmidt73a88812006-04-22 02:35:30 -0700644 case -ENODEV: /* device removed */
645 case -ESHUTDOWN: /* device shut down */
Tilman Schmidt73a88812006-04-22 02:35:30 -0700646 gig_dbg(DEBUG_USBREQ, "%s: device disconnected", __func__);
647 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800648 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700649 dev_warn(cs->dev, "interrupt read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800650 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800651 goto resubmit;
652 }
653
Tilman Schmidt73a88812006-04-22 02:35:30 -0700654 /* drop incomplete packets even if the missing bytes wouldn't matter */
Tilman Schmidt170ebf82009-03-18 23:44:23 -0700655 if (unlikely(urb->actual_length < IP_MSGSIZE)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700656 dev_warn(cs->dev, "incomplete interrupt packet (%d bytes)\n",
657 urb->actual_length);
658 goto resubmit;
659 }
660
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800661 l = (unsigned) ucs->int_in_buf[1] +
662 (((unsigned) ucs->int_in_buf[2]) << 8);
663
Tilman Schmidt784d5852006-04-10 22:55:04 -0700664 gig_dbg(DEBUG_USBREQ, "<-------%d: 0x%02x (%u [0x%02x 0x%02x])",
665 urb->actual_length, (int)ucs->int_in_buf[0], l,
666 (int)ucs->int_in_buf[1], (int)ucs->int_in_buf[2]);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800667
668 channel = 0;
669
670 switch (ucs->int_in_buf[0]) {
671 case HD_DEVICE_INIT_OK:
672 update_basstate(ucs, BS_INIT, 0);
673 break;
674
675 case HD_READY_SEND_ATDATA:
676 del_timer(&ucs->timer_atrdy);
677 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
678 start_cbsend(cs);
679 break;
680
681 case HD_OPEN_B2CHANNEL_ACK:
682 ++channel;
683 case HD_OPEN_B1CHANNEL_ACK:
684 bcs = cs->bcs + channel;
685 update_basstate(ucs, BS_B1OPEN << channel, 0);
686 gigaset_bchannel_up(bcs);
687 break;
688
689 case HD_OPEN_ATCHANNEL_ACK:
690 update_basstate(ucs, BS_ATOPEN, 0);
691 start_cbsend(cs);
692 break;
693
694 case HD_CLOSE_B2CHANNEL_ACK:
695 ++channel;
696 case HD_CLOSE_B1CHANNEL_ACK:
697 bcs = cs->bcs + channel;
698 update_basstate(ucs, 0, BS_B1OPEN << channel);
699 stopurbs(bcs->hw.bas);
700 gigaset_bchannel_down(bcs);
701 break;
702
703 case HD_CLOSE_ATCHANNEL_ACK:
704 update_basstate(ucs, 0, BS_ATOPEN);
705 break;
706
707 case HD_B2_FLOW_CONTROL:
708 ++channel;
709 case HD_B1_FLOW_CONTROL:
710 bcs = cs->bcs + channel;
711 atomic_add((l - BAS_NORMFRAME) * BAS_CORRFRAMES,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700712 &bcs->hw.bas->corrbytes);
713 gig_dbg(DEBUG_ISO,
714 "Flow control (channel %d, sub %d): 0x%02x => %d",
715 channel, bcs->hw.bas->numsub, l,
716 atomic_read(&bcs->hw.bas->corrbytes));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800717 break;
718
719 case HD_RECEIVEATDATA_ACK: /* AT response ready to be received */
720 if (!l) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700721 dev_warn(cs->dev,
722 "HD_RECEIVEATDATA_ACK with length 0 ignored\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800723 break;
724 }
725 spin_lock_irqsave(&cs->lock, flags);
726 if (ucs->rcvbuf_size) {
Tilman Schmidt73a88812006-04-22 02:35:30 -0700727 /* throw away previous buffer - we have no queue */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700728 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700729 "receive AT data overrun, %d bytes lost\n",
730 ucs->rcvbuf_size);
731 kfree(ucs->rcvbuf);
732 ucs->rcvbuf_size = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800733 }
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000734 ucs->rcvbuf = kmalloc(l, GFP_ATOMIC);
735 if (ucs->rcvbuf == NULL) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800736 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700737 dev_err(cs->dev, "out of memory receiving AT data\n");
738 error_reset(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800739 break;
740 }
741 ucs->rcvbuf_size = l;
742 ucs->retry_cmd_in = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000743 rc = atread_submit(cs, BAS_TIMEOUT);
744 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800745 kfree(ucs->rcvbuf);
746 ucs->rcvbuf = NULL;
747 ucs->rcvbuf_size = 0;
Tilman Schmidt06163f82006-06-26 00:25:33 -0700748 if (rc != -ENODEV) {
Tilman Schmidt06163f82006-06-26 00:25:33 -0700749 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700750 error_reset(cs);
Tilman Schmidt06163f82006-06-26 00:25:33 -0700751 break;
752 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800753 }
754 spin_unlock_irqrestore(&cs->lock, flags);
755 break;
756
757 case HD_RESET_INTERRUPT_PIPE_ACK:
Tilman Schmidtb5f581d2009-10-06 12:18:51 +0000758 update_basstate(ucs, 0, BS_RESETTING);
759 dev_notice(cs->dev, "interrupt pipe reset\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800760 break;
761
762 case HD_SUSPEND_END:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700763 gig_dbg(DEBUG_USBREQ, "HD_SUSPEND_END");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800764 break;
765
766 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700767 dev_warn(cs->dev,
768 "unknown Gigaset signal 0x%02x (%u) ignored\n",
769 (int) ucs->int_in_buf[0], l);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800770 }
771
772 check_pending(ucs);
Tilman Schmidt024fd292008-02-06 01:38:26 -0800773 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800774
775resubmit:
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800776 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700777 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700778 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -0700779 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800780 error_reset(cs);
781 }
782}
783
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800784/* read_iso_callback
785 * USB completion handler for B channel isochronous input
786 * called by the USB subsystem in interrupt context
787 * parameter:
788 * urb USB request block of completed request
789 * urb->context = bc_state structure
790 */
David Howells7d12e782006-10-05 14:55:46 +0100791static void read_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800792{
793 struct bc_state *bcs;
794 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800795 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800796 unsigned long flags;
797 int i, rc;
798
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800799 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800800 if (unlikely(status == -ENOENT ||
801 status == -ECONNRESET ||
802 status == -EINPROGRESS ||
803 status == -ENODEV ||
804 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700805 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800806 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800807 return;
808 }
809
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700810 bcs = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800811 ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800812
813 spin_lock_irqsave(&ubc->isoinlock, flags);
814 if (likely(ubc->isoindone == NULL)) {
815 /* pass URB to tasklet */
816 ubc->isoindone = urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800817 ubc->isoinstatus = status;
Tilman Schmidt368fd812009-04-05 06:39:33 +0000818 tasklet_hi_schedule(&ubc->rcvd_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800819 } else {
820 /* tasklet still busy, drop data and resubmit URB */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800821 ubc->loststatus = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800822 for (i = 0; i < BAS_NUMFRAMES; i++) {
823 ubc->isoinlost += urb->iso_frame_desc[i].actual_length;
824 if (unlikely(urb->iso_frame_desc[i].status != 0 &&
Tilman Schmidt73a88812006-04-22 02:35:30 -0700825 urb->iso_frame_desc[i].status !=
826 -EINPROGRESS))
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800827 ubc->loststatus = urb->iso_frame_desc[i].status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800828 urb->iso_frame_desc[i].status = 0;
829 urb->iso_frame_desc[i].actual_length = 0;
830 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800831 if (likely(ubc->running)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700832 /* urb->dev is clobbered by USB subsystem */
833 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800834 urb->transfer_flags = URB_ISO_ASAP;
835 urb->number_of_packets = BAS_NUMFRAMES;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700836 gig_dbg(DEBUG_ISO, "%s: isoc read overrun/resubmit",
837 __func__);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800838 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700839 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700840 dev_err(bcs->cs->dev,
841 "could not resubmit isochronous read "
Tilman Schmidt73a88812006-04-22 02:35:30 -0700842 "URB: %s\n", get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800843 dump_urb(DEBUG_ISO, "isoc read", urb);
844 error_hangup(bcs);
845 }
846 }
847 }
848 spin_unlock_irqrestore(&ubc->isoinlock, flags);
849}
850
851/* write_iso_callback
852 * USB completion handler for B channel isochronous output
853 * called by the USB subsystem in interrupt context
854 * parameter:
855 * urb USB request block of completed request
856 * urb->context = isow_urbctx_t structure
857 */
David Howells7d12e782006-10-05 14:55:46 +0100858static void write_iso_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800859{
860 struct isow_urbctx_t *ucx;
861 struct bas_bc_state *ubc;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800862 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800863 unsigned long flags;
864
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800865 /* status codes not worth bothering the tasklet with */
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800866 if (unlikely(status == -ENOENT ||
867 status == -ECONNRESET ||
868 status == -EINPROGRESS ||
869 status == -ENODEV ||
870 status == -ESHUTDOWN)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700871 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800872 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800873 return;
874 }
875
876 /* pass URB context to tasklet */
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700877 ucx = urb->context;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800878 ubc = ucx->bcs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800879 ucx->status = status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800880
881 spin_lock_irqsave(&ubc->isooutlock, flags);
882 ubc->isooutovfl = ubc->isooutdone;
883 ubc->isooutdone = ucx;
884 spin_unlock_irqrestore(&ubc->isooutlock, flags);
Tilman Schmidt368fd812009-04-05 06:39:33 +0000885 tasklet_hi_schedule(&ubc->sent_tasklet);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800886}
887
888/* starturbs
889 * prepare and submit USB request blocks for isochronous input and output
890 * argument:
891 * B channel control structure
892 * return value:
893 * 0 on success
894 * < 0 on error (no URBs submitted)
895 */
896static int starturbs(struct bc_state *bcs)
897{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700898 struct bas_bc_state *ubc = bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800899 struct urb *urb;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800900 int j, k;
901 int rc;
902
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800903 /* initialize L2 reception */
Tilman Schmidt088ec0c2009-10-06 12:19:07 +0000904 if (bcs->proto2 == L2_HDLC)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800905 bcs->inputstate |= INS_flag_hunt;
906
907 /* submit all isochronous input URBs */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800908 ubc->running = 1;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800909 for (k = 0; k < BAS_INURBS; k++) {
910 urb = ubc->isoinurbs[k];
911 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800912 rc = -EFAULT;
913 goto error;
914 }
915
916 urb->dev = bcs->cs->hw.bas->udev;
917 urb->pipe = usb_rcvisocpipe(urb->dev, 3 + 2 * bcs->channel);
918 urb->transfer_flags = URB_ISO_ASAP;
919 urb->transfer_buffer = ubc->isoinbuf + k * BAS_INBUFSIZE;
920 urb->transfer_buffer_length = BAS_INBUFSIZE;
921 urb->number_of_packets = BAS_NUMFRAMES;
922 urb->interval = BAS_FRAMETIME;
923 urb->complete = read_iso_callback;
924 urb->context = bcs;
925 for (j = 0; j < BAS_NUMFRAMES; j++) {
926 urb->iso_frame_desc[j].offset = j * BAS_MAXFRAME;
927 urb->iso_frame_desc[j].length = BAS_MAXFRAME;
928 urb->iso_frame_desc[j].status = 0;
929 urb->iso_frame_desc[j].actual_length = 0;
930 }
931
932 dump_urb(DEBUG_ISO, "Initial isoc read", urb);
Tilman Schmidt7891adf2009-10-25 09:30:37 +0000933 rc = usb_submit_urb(urb, GFP_ATOMIC);
934 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800935 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800936 }
937
938 /* initialize L2 transmission */
939 gigaset_isowbuf_init(ubc->isooutbuf, PPP_FLAG);
940
941 /* set up isochronous output URBs for flag idling */
942 for (k = 0; k < BAS_OUTURBS; ++k) {
943 urb = ubc->isoouturbs[k].urb;
944 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800945 rc = -EFAULT;
946 goto error;
947 }
948 urb->dev = bcs->cs->hw.bas->udev;
949 urb->pipe = usb_sndisocpipe(urb->dev, 4 + 2 * bcs->channel);
950 urb->transfer_flags = URB_ISO_ASAP;
951 urb->transfer_buffer = ubc->isooutbuf->data;
952 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
953 urb->number_of_packets = BAS_NUMFRAMES;
954 urb->interval = BAS_FRAMETIME;
955 urb->complete = write_iso_callback;
956 urb->context = &ubc->isoouturbs[k];
957 for (j = 0; j < BAS_NUMFRAMES; ++j) {
958 urb->iso_frame_desc[j].offset = BAS_OUTBUFSIZE;
959 urb->iso_frame_desc[j].length = BAS_NORMFRAME;
960 urb->iso_frame_desc[j].status = 0;
961 urb->iso_frame_desc[j].actual_length = 0;
962 }
963 ubc->isoouturbs[k].limit = -1;
964 }
965
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800966 /* keep one URB free, submit the others */
967 for (k = 0; k < BAS_OUTURBS-1; ++k) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800968 dump_urb(DEBUG_ISO, "Initial isoc write", urb);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800969 rc = usb_submit_urb(ubc->isoouturbs[k].urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -0700970 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800971 goto error;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800972 }
973 dump_urb(DEBUG_ISO, "Initial isoc write (free)", urb);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800974 ubc->isooutfree = &ubc->isoouturbs[BAS_OUTURBS-1];
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800975 ubc->isooutdone = ubc->isooutovfl = NULL;
976 return 0;
977 error:
978 stopurbs(ubc);
979 return rc;
980}
981
982/* stopurbs
983 * cancel the USB request blocks for isochronous input and output
984 * errors are silently ignored
985 * argument:
986 * B channel control structure
987 */
988static void stopurbs(struct bas_bc_state *ubc)
989{
990 int k, rc;
991
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800992 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800993
994 for (k = 0; k < BAS_INURBS; ++k) {
995 rc = usb_unlink_urb(ubc->isoinurbs[k]);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700996 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -0700997 "%s: isoc input URB %d unlinked, result = %s",
998 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -0800999 }
1000
1001 for (k = 0; k < BAS_OUTURBS; ++k) {
1002 rc = usb_unlink_urb(ubc->isoouturbs[k].urb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001003 gig_dbg(DEBUG_ISO,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001004 "%s: isoc output URB %d unlinked, result = %s",
1005 __func__, k, get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001006 }
1007}
1008
1009/* Isochronous Write - Bottom Half */
1010/* =============================== */
1011
1012/* submit_iso_write_urb
1013 * fill and submit the next isochronous write URB
1014 * parameters:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001015 * ucx context structure containing URB
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001016 * return value:
1017 * number of frames submitted in URB
1018 * 0 if URB not submitted because no data available (isooutbuf busy)
1019 * error code < 0 on error
1020 */
1021static int submit_iso_write_urb(struct isow_urbctx_t *ucx)
1022{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001023 struct urb *urb = ucx->urb;
1024 struct bas_bc_state *ubc = ucx->bcs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001025 struct usb_iso_packet_descriptor *ifd;
1026 int corrbytes, nframe, rc;
1027
Tilman Schmidt784d5852006-04-10 22:55:04 -07001028 /* urb->dev is clobbered by USB subsystem */
1029 urb->dev = ucx->bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001030 urb->transfer_flags = URB_ISO_ASAP;
1031 urb->transfer_buffer = ubc->isooutbuf->data;
1032 urb->transfer_buffer_length = sizeof(ubc->isooutbuf->data);
1033
1034 for (nframe = 0; nframe < BAS_NUMFRAMES; nframe++) {
1035 ifd = &urb->iso_frame_desc[nframe];
1036
1037 /* compute frame length according to flow control */
1038 ifd->length = BAS_NORMFRAME;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001039 corrbytes = atomic_read(&ubc->corrbytes);
1040 if (corrbytes != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001041 gig_dbg(DEBUG_ISO, "%s: corrbytes=%d",
1042 __func__, corrbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001043 if (corrbytes > BAS_HIGHFRAME - BAS_NORMFRAME)
1044 corrbytes = BAS_HIGHFRAME - BAS_NORMFRAME;
1045 else if (corrbytes < BAS_LOWFRAME - BAS_NORMFRAME)
1046 corrbytes = BAS_LOWFRAME - BAS_NORMFRAME;
1047 ifd->length += corrbytes;
1048 atomic_add(-corrbytes, &ubc->corrbytes);
1049 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001050
1051 /* retrieve block of data to send */
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001052 rc = gigaset_isowbuf_getbytes(ubc->isooutbuf, ifd->length);
1053 if (rc < 0) {
1054 if (rc == -EBUSY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001055 gig_dbg(DEBUG_ISO,
1056 "%s: buffer busy at frame %d",
1057 __func__, nframe);
1058 /* tasklet will be restarted from
Tilman Schmidt088ec0c2009-10-06 12:19:07 +00001059 gigaset_isoc_send_skb() */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001060 } else {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001061 dev_err(ucx->bcs->cs->dev,
1062 "%s: buffer error %d at frame %d\n",
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001063 __func__, rc, nframe);
1064 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001065 }
1066 break;
1067 }
Tilman Schmidt5f09c4c2008-07-23 21:28:27 -07001068 ifd->offset = rc;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001069 ucx->limit = ubc->isooutbuf->nextread;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001070 ifd->status = 0;
1071 ifd->actual_length = 0;
1072 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001073 if (unlikely(nframe == 0))
1074 return 0; /* no data to send */
1075 urb->number_of_packets = nframe;
Tilman Schmidt69049cc2006-04-10 22:55:16 -07001076
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001077 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001078 if (unlikely(rc)) {
1079 if (rc == -ENODEV)
1080 /* device removed - give up silently */
1081 gig_dbg(DEBUG_ISO, "%s: disconnected", __func__);
1082 else
Tilman Schmidt784d5852006-04-10 22:55:04 -07001083 dev_err(ucx->bcs->cs->dev,
1084 "could not submit isochronous write URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001085 get_usb_rcmsg(rc));
1086 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001087 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001088 ++ubc->numsub;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001089 return nframe;
1090}
1091
1092/* write_iso_tasklet
1093 * tasklet scheduled when an isochronous output URB from the Gigaset device
1094 * has completed
1095 * parameter:
1096 * data B channel state structure
1097 */
1098static void write_iso_tasklet(unsigned long data)
1099{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001100 struct bc_state *bcs = (struct bc_state *) data;
1101 struct bas_bc_state *ubc = bcs->hw.bas;
1102 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001103 struct isow_urbctx_t *done, *next, *ovfl;
1104 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001105 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001106 struct usb_iso_packet_descriptor *ifd;
1107 int offset;
1108 unsigned long flags;
1109 int i;
1110 struct sk_buff *skb;
1111 int len;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001112 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001113
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001114 /* loop while completed URBs arrive in time */
1115 for (;;) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001116 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001117 gig_dbg(DEBUG_ISO, "%s: not running", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001118 return;
1119 }
1120
1121 /* retrieve completed URBs */
1122 spin_lock_irqsave(&ubc->isooutlock, flags);
1123 done = ubc->isooutdone;
1124 ubc->isooutdone = NULL;
1125 ovfl = ubc->isooutovfl;
1126 ubc->isooutovfl = NULL;
1127 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1128 if (ovfl) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001129 dev_err(cs->dev, "isochronous write buffer underrun\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001130 error_hangup(bcs);
1131 break;
1132 }
1133 if (!done)
1134 break;
1135
1136 /* submit free URB if available */
1137 spin_lock_irqsave(&ubc->isooutlock, flags);
1138 next = ubc->isooutfree;
1139 ubc->isooutfree = NULL;
1140 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1141 if (next) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001142 rc = submit_iso_write_urb(next);
1143 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001144 /* could not submit URB, put it back */
1145 spin_lock_irqsave(&ubc->isooutlock, flags);
1146 if (ubc->isooutfree == NULL) {
1147 ubc->isooutfree = next;
1148 next = NULL;
1149 }
1150 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1151 if (next) {
1152 /* couldn't put it back */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001153 dev_err(cs->dev,
1154 "losing isochronous write URB\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001155 error_hangup(bcs);
1156 }
1157 }
1158 }
1159
1160 /* process completed URB */
1161 urb = done->urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001162 status = done->status;
1163 switch (status) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001164 case -EXDEV: /* partial completion */
1165 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1166 __func__);
1167 /* fall through - what's the difference anyway? */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001168 case 0: /* normal completion */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001169 /* inspect individual frames
1170 * assumptions (for lack of documentation):
1171 * - actual_length bytes of first frame in error are
Tilman Schmidt917f5082006-04-10 22:55:00 -07001172 * successfully sent
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001173 * - all following frames are not sent at all
1174 */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001175 offset = done->limit; /* default (no error) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001176 for (i = 0; i < BAS_NUMFRAMES; i++) {
1177 ifd = &urb->iso_frame_desc[i];
1178 if (ifd->status ||
1179 ifd->actual_length != ifd->length) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001180 dev_warn(cs->dev,
1181 "isochronous write: frame %d: %s, "
1182 "only %d of %d bytes sent\n",
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001183 i, get_usb_statmsg(ifd->status),
1184 ifd->actual_length, ifd->length);
1185 offset = (ifd->offset +
Tilman Schmidt784d5852006-04-10 22:55:04 -07001186 ifd->actual_length)
1187 % BAS_OUTBUFSIZE;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001188 break;
1189 }
1190 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001191 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001192 case -EPIPE: /* stall - probably underrun */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001193 dev_err(cs->dev, "isochronous write stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001194 error_hangup(bcs);
1195 break;
1196 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001197 dev_warn(cs->dev, "isochronous write: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001198 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001199 }
1200
1201 /* mark the write buffer area covered by this URB as free */
1202 if (done->limit >= 0)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001203 ubc->isooutbuf->read = done->limit;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001204
1205 /* mark URB as free */
1206 spin_lock_irqsave(&ubc->isooutlock, flags);
1207 next = ubc->isooutfree;
1208 ubc->isooutfree = done;
1209 spin_unlock_irqrestore(&ubc->isooutlock, flags);
1210 if (next) {
1211 /* only one URB still active - resubmit one */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001212 rc = submit_iso_write_urb(next);
1213 if (unlikely(rc <= 0 && rc != -ENODEV)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001214 /* couldn't submit */
1215 error_hangup(bcs);
1216 }
1217 }
1218 }
1219
1220 /* process queued SKBs */
1221 while ((skb = skb_dequeue(&bcs->squeue))) {
1222 /* copy to output buffer, doing L2 encapsulation */
1223 len = skb->len;
1224 if (gigaset_isoc_buildframe(bcs, skb->data, len) == -EAGAIN) {
1225 /* insufficient buffer space, push back onto queue */
1226 skb_queue_head(&bcs->squeue, skb);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001227 gig_dbg(DEBUG_ISO, "%s: skb requeued, qlen=%d",
1228 __func__, skb_queue_len(&bcs->squeue));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001229 break;
1230 }
1231 skb_pull(skb, len);
1232 gigaset_skb_sent(bcs, skb);
1233 dev_kfree_skb_any(skb);
1234 }
1235}
1236
1237/* Isochronous Read - Bottom Half */
1238/* ============================== */
1239
1240/* read_iso_tasklet
1241 * tasklet scheduled when an isochronous input URB from the Gigaset device
1242 * has completed
1243 * parameter:
1244 * data B channel state structure
1245 */
1246static void read_iso_tasklet(unsigned long data)
1247{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001248 struct bc_state *bcs = (struct bc_state *) data;
1249 struct bas_bc_state *ubc = bcs->hw.bas;
1250 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001251 struct urb *urb;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001252 int status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001253 char *rcvbuf;
1254 unsigned long flags;
1255 int totleft, numbytes, offset, frame, rc;
1256
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001257 /* loop while more completed URBs arrive in the meantime */
1258 for (;;) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001259 /* retrieve URB */
1260 spin_lock_irqsave(&ubc->isoinlock, flags);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001261 urb = ubc->isoindone;
1262 if (!urb) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001263 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1264 return;
1265 }
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001266 status = ubc->isoinstatus;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001267 ubc->isoindone = NULL;
1268 if (unlikely(ubc->loststatus != -EINPROGRESS)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001269 dev_warn(cs->dev,
1270 "isochronous read overrun, "
1271 "dropped URB with status: %s, %d bytes lost\n",
1272 get_usb_statmsg(ubc->loststatus),
1273 ubc->isoinlost);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001274 ubc->loststatus = -EINPROGRESS;
1275 }
1276 spin_unlock_irqrestore(&ubc->isoinlock, flags);
1277
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001278 if (unlikely(!(ubc->running))) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001279 gig_dbg(DEBUG_ISO,
1280 "%s: channel not running, "
1281 "dropped URB with status: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001282 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001283 return;
1284 }
1285
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001286 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001287 case 0: /* normal completion */
1288 break;
Tilman Schmidt917f5082006-04-10 22:55:00 -07001289 case -EXDEV: /* inspect individual frames
1290 (we do that anyway) */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001291 gig_dbg(DEBUG_ISO, "%s: URB partially completed",
1292 __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001293 break;
1294 case -ENOENT:
1295 case -ECONNRESET:
Tilman Schmidt73a88812006-04-22 02:35:30 -07001296 case -EINPROGRESS:
1297 gig_dbg(DEBUG_ISO, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001298 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001299 continue; /* -> skip */
1300 case -EPIPE:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001301 dev_err(cs->dev, "isochronous read stalled\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001302 error_hangup(bcs);
1303 continue; /* -> skip */
1304 default: /* severe trouble */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001305 dev_warn(cs->dev, "isochronous read: %s\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001306 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001307 goto error;
1308 }
1309
1310 rcvbuf = urb->transfer_buffer;
1311 totleft = urb->actual_length;
1312 for (frame = 0; totleft > 0 && frame < BAS_NUMFRAMES; frame++) {
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001313 numbytes = urb->iso_frame_desc[frame].actual_length;
1314 if (unlikely(urb->iso_frame_desc[frame].status))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001315 dev_warn(cs->dev,
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001316 "isochronous read: frame %d[%d]: %s\n",
1317 frame, numbytes,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001318 get_usb_statmsg(
1319 urb->iso_frame_desc[frame].status));
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001320 if (unlikely(numbytes > BAS_MAXFRAME))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001321 dev_warn(cs->dev,
1322 "isochronous read: frame %d: "
1323 "numbytes (%d) > BAS_MAXFRAME\n",
1324 frame, numbytes);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001325 if (unlikely(numbytes > totleft)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001326 dev_warn(cs->dev,
1327 "isochronous read: frame %d: "
1328 "numbytes (%d) > totleft (%d)\n",
1329 frame, numbytes, totleft);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001330 numbytes = totleft;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001331 }
1332 offset = urb->iso_frame_desc[frame].offset;
1333 if (unlikely(offset + numbytes > BAS_INBUFSIZE)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001334 dev_warn(cs->dev,
1335 "isochronous read: frame %d: "
1336 "offset (%d) + numbytes (%d) "
1337 "> BAS_INBUFSIZE\n",
1338 frame, offset, numbytes);
Tilman Schmidteb4459f2009-10-06 12:18:36 +00001339 numbytes = BAS_INBUFSIZE - offset;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001340 }
1341 gigaset_isoc_receive(rcvbuf + offset, numbytes, bcs);
1342 totleft -= numbytes;
1343 }
1344 if (unlikely(totleft > 0))
Tilman Schmidt784d5852006-04-10 22:55:04 -07001345 dev_warn(cs->dev,
1346 "isochronous read: %d data bytes missing\n",
1347 totleft);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001348
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001349error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001350 /* URB processed, resubmit */
1351 for (frame = 0; frame < BAS_NUMFRAMES; frame++) {
1352 urb->iso_frame_desc[frame].status = 0;
1353 urb->iso_frame_desc[frame].actual_length = 0;
1354 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001355 /* urb->dev is clobbered by USB subsystem */
1356 urb->dev = bcs->cs->hw.bas->udev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001357 urb->transfer_flags = URB_ISO_ASAP;
1358 urb->number_of_packets = BAS_NUMFRAMES;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001359 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001360 if (unlikely(rc != 0 && rc != -ENODEV)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001361 dev_err(cs->dev,
1362 "could not resubmit isochronous read URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001363 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001364 dump_urb(DEBUG_ISO, "resubmit iso read", urb);
1365 error_hangup(bcs);
1366 }
1367 }
1368}
1369
1370/* Channel Operations */
1371/* ================== */
1372
1373/* req_timeout
1374 * timeout routine for control output request
1375 * argument:
1376 * B channel control structure
1377 */
1378static void req_timeout(unsigned long data)
1379{
1380 struct bc_state *bcs = (struct bc_state *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001381 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001382 int pending;
1383 unsigned long flags;
1384
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001385 check_pending(ucs);
1386
1387 spin_lock_irqsave(&ucs->lock, flags);
1388 pending = ucs->pending;
1389 ucs->pending = 0;
1390 spin_unlock_irqrestore(&ucs->lock, flags);
1391
1392 switch (pending) {
1393 case 0: /* no pending request */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001394 gig_dbg(DEBUG_USBREQ, "%s: no request pending", __func__);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001395 break;
1396
1397 case HD_OPEN_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001398 dev_err(bcs->cs->dev, "timeout opening AT channel\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001399 error_reset(bcs->cs);
1400 break;
1401
1402 case HD_OPEN_B2CHANNEL:
1403 case HD_OPEN_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001404 dev_err(bcs->cs->dev, "timeout opening channel %d\n",
1405 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001406 error_hangup(bcs);
1407 break;
1408
1409 case HD_CLOSE_ATCHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001410 dev_err(bcs->cs->dev, "timeout closing AT channel\n");
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001411 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001412 break;
1413
1414 case HD_CLOSE_B2CHANNEL:
1415 case HD_CLOSE_B1CHANNEL:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001416 dev_err(bcs->cs->dev, "timeout closing channel %d\n",
1417 bcs->channel + 1);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001418 error_reset(bcs->cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001419 break;
1420
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001421 case HD_RESET_INTERRUPT_PIPE:
1422 /* error recovery escalation */
1423 dev_err(bcs->cs->dev,
1424 "reset interrupt pipe timeout, attempting USB reset\n");
1425 usb_queue_reset_device(bcs->cs->hw.bas->interface);
1426 break;
1427
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001428 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -07001429 dev_warn(bcs->cs->dev, "request 0x%02x timed out, clearing\n",
1430 pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001431 }
Tilman Schmidt024fd292008-02-06 01:38:26 -08001432
1433 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001434}
1435
1436/* write_ctrl_callback
1437 * USB completion handler for control pipe output
1438 * called by the USB subsystem in interrupt context
1439 * parameter:
1440 * urb USB request block of completed request
1441 * urb->context = hardware specific controller state structure
1442 */
David Howells7d12e782006-10-05 14:55:46 +01001443static void write_ctrl_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001444{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001445 struct bas_cardstate *ucs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001446 int status = urb->status;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001447 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001448 unsigned long flags;
1449
Tilman Schmidt06163f82006-06-26 00:25:33 -07001450 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001451 switch (status) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001452 case 0: /* normal completion */
1453 spin_lock_irqsave(&ucs->lock, flags);
1454 switch (ucs->pending) {
1455 case HD_DEVICE_INIT_ACK: /* no reply expected */
1456 del_timer(&ucs->timer_ctrl);
1457 ucs->pending = 0;
1458 break;
1459 }
1460 spin_unlock_irqrestore(&ucs->lock, flags);
1461 return;
1462
1463 case -ENOENT: /* cancelled */
1464 case -ECONNRESET: /* cancelled (async) */
1465 case -EINPROGRESS: /* pending */
1466 case -ENODEV: /* device removed */
1467 case -ESHUTDOWN: /* device shut down */
1468 /* ignore silently */
1469 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001470 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001471 break;
Tilman Schmidt06163f82006-06-26 00:25:33 -07001472
1473 default: /* any failure */
Tilman Schmidt024fd292008-02-06 01:38:26 -08001474 /* don't retry if suspend requested */
1475 if (++ucs->retry_ctrl > BAS_RETRY ||
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001476 (ucs->basstate & BS_SUSPEND)) {
Tilman Schmidt06163f82006-06-26 00:25:33 -07001477 dev_err(&ucs->interface->dev,
1478 "control request 0x%02x failed: %s\n",
1479 ucs->dr_ctrl.bRequest,
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001480 get_usb_statmsg(status));
Tilman Schmidt06163f82006-06-26 00:25:33 -07001481 break; /* give up */
1482 }
1483 dev_notice(&ucs->interface->dev,
1484 "control request 0x%02x: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001485 ucs->dr_ctrl.bRequest, get_usb_statmsg(status),
Tilman Schmidt06163f82006-06-26 00:25:33 -07001486 ucs->retry_ctrl);
1487 /* urb->dev is clobbered by USB subsystem */
1488 urb->dev = ucs->udev;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001489 rc = usb_submit_urb(urb, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001490 if (unlikely(rc)) {
1491 dev_err(&ucs->interface->dev,
1492 "could not resubmit request 0x%02x: %s\n",
1493 ucs->dr_ctrl.bRequest, get_usb_rcmsg(rc));
1494 break;
1495 }
1496 /* resubmitted */
1497 return;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001498 }
Tilman Schmidt06163f82006-06-26 00:25:33 -07001499
1500 /* failed, clear pending request */
1501 spin_lock_irqsave(&ucs->lock, flags);
1502 del_timer(&ucs->timer_ctrl);
1503 ucs->pending = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001504 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001505 wake_up(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001506}
1507
1508/* req_submit
1509 * submit a control output request without message buffer to the Gigaset base
1510 * and optionally start a timeout
1511 * parameters:
1512 * bcs B channel control structure
1513 * req control request code (HD_*)
1514 * val control request parameter value (set to 0 if unused)
1515 * timeout timeout in seconds (0: no timeout)
1516 * return value:
1517 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001518 * -EBUSY if another request is pending
1519 * any URB submission error code
1520 */
1521static int req_submit(struct bc_state *bcs, int req, int val, int timeout)
1522{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001523 struct bas_cardstate *ucs = bcs->cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001524 int ret;
1525 unsigned long flags;
1526
Tilman Schmidt784d5852006-04-10 22:55:04 -07001527 gig_dbg(DEBUG_USBREQ, "-------> 0x%02x (%d)", req, val);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001528
1529 spin_lock_irqsave(&ucs->lock, flags);
1530 if (ucs->pending) {
1531 spin_unlock_irqrestore(&ucs->lock, flags);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001532 dev_err(bcs->cs->dev,
1533 "submission of request 0x%02x failed: "
1534 "request 0x%02x still pending\n",
1535 req, ucs->pending);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001536 return -EBUSY;
1537 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001538
1539 ucs->dr_ctrl.bRequestType = OUT_VENDOR_REQ;
1540 ucs->dr_ctrl.bRequest = req;
1541 ucs->dr_ctrl.wValue = cpu_to_le16(val);
1542 ucs->dr_ctrl.wIndex = 0;
1543 ucs->dr_ctrl.wLength = 0;
1544 usb_fill_control_urb(ucs->urb_ctrl, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001545 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001546 (unsigned char *) &ucs->dr_ctrl, NULL, 0,
Tilman Schmidt784d5852006-04-10 22:55:04 -07001547 write_ctrl_callback, ucs);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001548 ucs->retry_ctrl = 0;
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001549 ret = usb_submit_urb(ucs->urb_ctrl, GFP_ATOMIC);
Tilman Schmidt06163f82006-06-26 00:25:33 -07001550 if (unlikely(ret)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001551 dev_err(bcs->cs->dev, "could not submit request 0x%02x: %s\n",
Tilman Schmidt06163f82006-06-26 00:25:33 -07001552 req, get_usb_rcmsg(ret));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001553 spin_unlock_irqrestore(&ucs->lock, flags);
1554 return ret;
1555 }
1556 ucs->pending = req;
1557
1558 if (timeout > 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001559 gig_dbg(DEBUG_USBREQ, "setting timeout of %d/10 secs", timeout);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001560 ucs->timer_ctrl.expires = jiffies + timeout * HZ / 10;
1561 ucs->timer_ctrl.data = (unsigned long) bcs;
1562 ucs->timer_ctrl.function = req_timeout;
1563 add_timer(&ucs->timer_ctrl);
1564 }
1565
1566 spin_unlock_irqrestore(&ucs->lock, flags);
1567 return 0;
1568}
1569
1570/* gigaset_init_bchannel
1571 * called by common.c to connect a B channel
1572 * initialize isochronous I/O and tell the Gigaset base to open the channel
1573 * argument:
1574 * B channel control structure
1575 * return value:
1576 * 0 on success, error code < 0 on error
1577 */
1578static int gigaset_init_bchannel(struct bc_state *bcs)
1579{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001580 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001581 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001582 unsigned long flags;
1583
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001584 spin_lock_irqsave(&cs->lock, flags);
1585 if (unlikely(!cs->connected)) {
Tilman Schmidt73a88812006-04-22 02:35:30 -07001586 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001587 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001588 return -ENODEV;
1589 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001590
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001591 if (cs->hw.bas->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001592 dev_notice(cs->dev,
1593 "not starting isochronous I/O, "
1594 "suspend in progress\n");
1595 spin_unlock_irqrestore(&cs->lock, flags);
1596 return -EHOSTUNREACH;
1597 }
1598
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001599 ret = starturbs(bcs);
1600 if (ret < 0) {
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001601 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001602 dev_err(cs->dev,
Tilman Schmidt73a88812006-04-22 02:35:30 -07001603 "could not start isochronous I/O for channel B%d: %s\n",
1604 bcs->channel + 1,
1605 ret == -EFAULT ? "null URB" : get_usb_rcmsg(ret));
1606 if (ret != -ENODEV)
1607 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001608 return ret;
1609 }
1610
1611 req = bcs->channel ? HD_OPEN_B2CHANNEL : HD_OPEN_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001612 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1613 if (ret < 0) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001614 dev_err(cs->dev, "could not open channel B%d\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001615 bcs->channel + 1);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001616 stopurbs(bcs->hw.bas);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001617 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07001618
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001619 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtb33ffa52010-09-30 13:34:30 +00001620 if (ret < 0 && ret != -ENODEV)
1621 error_hangup(bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001622 return ret;
1623}
1624
1625/* gigaset_close_bchannel
1626 * called by common.c to disconnect a B channel
1627 * tell the Gigaset base to close the channel
1628 * stopping isochronous I/O and LL notification will be done when the
1629 * acknowledgement for the close arrives
1630 * argument:
1631 * B channel control structure
1632 * return value:
1633 * 0 on success, error code < 0 on error
1634 */
1635static int gigaset_close_bchannel(struct bc_state *bcs)
1636{
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001637 struct cardstate *cs = bcs->cs;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001638 int req, ret;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001639 unsigned long flags;
1640
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001641 spin_lock_irqsave(&cs->lock, flags);
1642 if (unlikely(!cs->connected)) {
1643 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001644 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
1645 return -ENODEV;
1646 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001647
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001648 if (!(cs->hw.bas->basstate & (bcs->channel ? BS_B2OPEN : BS_B1OPEN))) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001649 /* channel not running: just signal common.c */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001650 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001651 gigaset_bchannel_down(bcs);
1652 return 0;
1653 }
1654
Tilman Schmidt73a88812006-04-22 02:35:30 -07001655 /* channel running: tell device to close it */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001656 req = bcs->channel ? HD_CLOSE_B2CHANNEL : HD_CLOSE_B1CHANNEL;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001657 ret = req_submit(bcs, req, 0, BAS_TIMEOUT);
1658 if (ret < 0)
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001659 dev_err(cs->dev, "closing channel B%d failed\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001660 bcs->channel + 1);
1661
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08001662 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001663 return ret;
1664}
1665
1666/* Device Operations */
1667/* ================= */
1668
1669/* complete_cb
1670 * unqueue first command buffer from queue, waking any sleepers
1671 * must be called with cs->cmdlock held
1672 * parameter:
1673 * cs controller state structure
1674 */
1675static void complete_cb(struct cardstate *cs)
1676{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001677 struct cmdbuf_t *cb = cs->cmdbuf;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001678
1679 /* unqueue completed buffer */
1680 cs->cmdbytes -= cs->curlen;
Tilman Schmidt1528b182010-02-22 13:09:52 +00001681 gig_dbg(DEBUG_OUTPUT, "write_command: sent %u bytes, %u left",
Tilman Schmidt784d5852006-04-10 22:55:04 -07001682 cs->curlen, cs->cmdbytes);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001683 if (cb->next != NULL) {
1684 cs->cmdbuf = cb->next;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001685 cs->cmdbuf->prev = NULL;
1686 cs->curlen = cs->cmdbuf->len;
1687 } else {
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001688 cs->cmdbuf = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001689 cs->lastcmdbuf = NULL;
1690 cs->curlen = 0;
1691 }
1692
1693 if (cb->wake_tasklet)
1694 tasklet_schedule(cb->wake_tasklet);
1695
1696 kfree(cb);
1697}
1698
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001699/* write_command_callback
1700 * USB completion handler for AT command transmission
1701 * called by the USB subsystem in interrupt context
1702 * parameter:
1703 * urb USB request block of completed request
1704 * urb->context = controller state structure
1705 */
David Howells7d12e782006-10-05 14:55:46 +01001706static void write_command_callback(struct urb *urb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001707{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001708 struct cardstate *cs = urb->context;
1709 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001710 int status = urb->status;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001711 unsigned long flags;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001712
Tilman Schmidt73a88812006-04-22 02:35:30 -07001713 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt024fd292008-02-06 01:38:26 -08001714 wake_up(&ucs->waitqueue);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001715
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001716 /* check status */
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001717 switch (status) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001718 case 0: /* normal completion */
1719 break;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001720 case -ENOENT: /* cancelled */
1721 case -ECONNRESET: /* cancelled (async) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001722 case -EINPROGRESS: /* pending */
Tilman Schmidt73a88812006-04-22 02:35:30 -07001723 case -ENODEV: /* device removed */
1724 case -ESHUTDOWN: /* device shut down */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001725 /* ignore silently */
Tilman Schmidt784d5852006-04-10 22:55:04 -07001726 gig_dbg(DEBUG_USBREQ, "%s: %s",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001727 __func__, get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001728 return;
1729 default: /* any failure */
1730 if (++ucs->retry_cmd_out > BAS_RETRY) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001731 dev_warn(cs->dev,
1732 "command write: %s, "
1733 "giving up after %d retries\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001734 get_usb_statmsg(status),
Tilman Schmidt784d5852006-04-10 22:55:04 -07001735 ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001736 break;
1737 }
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001738 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08001739 dev_warn(cs->dev,
1740 "command write: %s, "
1741 "won't retry - suspend requested\n",
1742 get_usb_statmsg(status));
1743 break;
1744 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001745 if (cs->cmdbuf == NULL) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001746 dev_warn(cs->dev,
1747 "command write: %s, "
1748 "cannot retry - cmdbuf gone\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001749 get_usb_statmsg(status));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001750 break;
1751 }
Tilman Schmidt784d5852006-04-10 22:55:04 -07001752 dev_notice(cs->dev, "command write: %s, retry %d\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -08001753 get_usb_statmsg(status), ucs->retry_cmd_out);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001754 if (atwrite_submit(cs, cs->cmdbuf->buf, cs->cmdbuf->len) >= 0)
1755 /* resubmitted - bypass regular exit block */
1756 return;
1757 /* command send failed, assume base still waiting */
1758 update_basstate(ucs, BS_ATREADY, 0);
1759 }
1760
1761 spin_lock_irqsave(&cs->cmdlock, flags);
1762 if (cs->cmdbuf != NULL)
1763 complete_cb(cs);
1764 spin_unlock_irqrestore(&cs->cmdlock, flags);
1765}
1766
1767/* atrdy_timeout
1768 * timeout routine for AT command transmission
1769 * argument:
1770 * controller state structure
1771 */
1772static void atrdy_timeout(unsigned long data)
1773{
1774 struct cardstate *cs = (struct cardstate *) data;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001775 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001776
Tilman Schmidt784d5852006-04-10 22:55:04 -07001777 dev_warn(cs->dev, "timeout waiting for HD_READY_SEND_ATDATA\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001778
1779 /* fake the missing signal - what else can I do? */
1780 update_basstate(ucs, BS_ATREADY, BS_ATTIMER);
1781 start_cbsend(cs);
1782}
1783
1784/* atwrite_submit
1785 * submit an HD_WRITE_ATMESSAGE command URB
1786 * parameters:
1787 * cs controller state structure
1788 * buf buffer containing command to send
1789 * len length of command to send
1790 * return value:
1791 * 0 on success
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001792 * -EBUSY if another request is pending
1793 * any URB submission error code
1794 */
1795static int atwrite_submit(struct cardstate *cs, unsigned char *buf, int len)
1796{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001797 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07001798 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001799
Tilman Schmidt784d5852006-04-10 22:55:04 -07001800 gig_dbg(DEBUG_USBREQ, "-------> HD_WRITE_ATMESSAGE (%d)", len);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001801
Tilman Schmidt73a88812006-04-22 02:35:30 -07001802 if (update_basstate(ucs, BS_ATWRPEND, 0) & BS_ATWRPEND) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001803 dev_err(cs->dev,
1804 "could not submit HD_WRITE_ATMESSAGE: URB busy\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001805 return -EBUSY;
1806 }
1807
1808 ucs->dr_cmd_out.bRequestType = OUT_VENDOR_REQ;
1809 ucs->dr_cmd_out.bRequest = HD_WRITE_ATMESSAGE;
1810 ucs->dr_cmd_out.wValue = 0;
1811 ucs->dr_cmd_out.wIndex = 0;
1812 ucs->dr_cmd_out.wLength = cpu_to_le16(len);
1813 usb_fill_control_urb(ucs->urb_cmd_out, ucs->udev,
1814 usb_sndctrlpipe(ucs->udev, 0),
Tilman Schmidt7891adf2009-10-25 09:30:37 +00001815 (unsigned char *) &ucs->dr_cmd_out, buf, len,
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001816 write_command_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -08001817 rc = usb_submit_urb(ucs->urb_cmd_out, GFP_ATOMIC);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001818 if (unlikely(rc)) {
1819 update_basstate(ucs, 0, BS_ATWRPEND);
Tilman Schmidt784d5852006-04-10 22:55:04 -07001820 dev_err(cs->dev, "could not submit HD_WRITE_ATMESSAGE: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07001821 get_usb_rcmsg(rc));
1822 return rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001823 }
1824
Tilman Schmidt73a88812006-04-22 02:35:30 -07001825 /* submitted successfully, start timeout if necessary */
1826 if (!(update_basstate(ucs, BS_ATTIMER, BS_ATREADY) & BS_ATTIMER)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07001827 gig_dbg(DEBUG_OUTPUT, "setting ATREADY timeout of %d/10 secs",
1828 ATRDY_TIMEOUT);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001829 ucs->timer_atrdy.expires = jiffies + ATRDY_TIMEOUT * HZ / 10;
1830 ucs->timer_atrdy.data = (unsigned long) cs;
1831 ucs->timer_atrdy.function = atrdy_timeout;
1832 add_timer(&ucs->timer_atrdy);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001833 }
1834 return 0;
1835}
1836
1837/* start_cbsend
1838 * start transmission of AT command queue if necessary
1839 * parameter:
1840 * cs controller state structure
1841 * return value:
1842 * 0 on success
1843 * error code < 0 on error
1844 */
1845static int start_cbsend(struct cardstate *cs)
1846{
1847 struct cmdbuf_t *cb;
Tilman Schmidtd48c7782006-04-10 22:55:08 -07001848 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001849 unsigned long flags;
1850 int rc;
1851 int retval = 0;
1852
Tilman Schmidt024fd292008-02-06 01:38:26 -08001853 /* check if suspend requested */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001854 if (ucs->basstate & BS_SUSPEND) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001855 gig_dbg(DEBUG_OUTPUT, "suspending");
Tilman Schmidt024fd292008-02-06 01:38:26 -08001856 return -EHOSTUNREACH;
1857 }
1858
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001859 /* check if AT channel is open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001860 if (!(ucs->basstate & BS_ATOPEN)) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00001861 gig_dbg(DEBUG_OUTPUT, "AT channel not open");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001862 rc = req_submit(cs->bcs, HD_OPEN_ATCHANNEL, 0, BAS_TIMEOUT);
1863 if (rc < 0) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001864 /* flush command queue */
1865 spin_lock_irqsave(&cs->cmdlock, flags);
1866 while (cs->cmdbuf != NULL)
1867 complete_cb(cs);
1868 spin_unlock_irqrestore(&cs->cmdlock, flags);
1869 }
1870 return rc;
1871 }
1872
1873 /* try to send first command in queue */
1874 spin_lock_irqsave(&cs->cmdlock, flags);
1875
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001876 while ((cb = cs->cmdbuf) != NULL && (ucs->basstate & BS_ATREADY)) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001877 ucs->retry_cmd_out = 0;
1878 rc = atwrite_submit(cs, cb->buf, cb->len);
1879 if (unlikely(rc)) {
1880 retval = rc;
1881 complete_cb(cs);
1882 }
1883 }
1884
1885 spin_unlock_irqrestore(&cs->cmdlock, flags);
1886 return retval;
1887}
1888
1889/* gigaset_write_cmd
1890 * This function is called by the device independent part of the driver
1891 * to transmit an AT command string to the Gigaset device.
1892 * It encapsulates the device specific method for transmission over the
1893 * direct USB connection to the base.
1894 * The command string is added to the queue of commands to send, and
1895 * USB transmission is started if necessary.
1896 * parameters:
1897 * cs controller state structure
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001898 * cb command buffer structure
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001899 * return value:
1900 * number of bytes queued on success
1901 * error code < 0 on error
1902 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001903static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001904{
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001905 unsigned long flags;
Tilman Schmidt39f07222006-12-13 00:33:52 -08001906 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001907
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08001908 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -07001909 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001910 "CMD Transmit", cb->len, cb->buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001911
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001912 /* translate "+++" escape sequence sent as a single separate command
1913 * into "close AT channel" command for error recovery
1914 * The next command will reopen the AT channel automatically.
1915 */
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001916 if (cb->len == 3 && !memcmp(cb->buf, "+++", 3)) {
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001917 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, BAS_TIMEOUT);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001918 if (cb->wake_tasklet)
1919 tasklet_schedule(cb->wake_tasklet);
Dan Carpenter8bcfbd02010-08-05 22:21:26 +00001920 if (!rc)
1921 rc = cb->len;
1922 kfree(cb);
1923 return rc;
Tilman Schmidtb5f581d2009-10-06 12:18:51 +00001924 }
1925
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001926 spin_lock_irqsave(&cs->cmdlock, flags);
1927 cb->prev = cs->lastcmdbuf;
1928 if (cs->lastcmdbuf)
1929 cs->lastcmdbuf->next = cb;
1930 else {
1931 cs->cmdbuf = cb;
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001932 cs->curlen = cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001933 }
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001934 cs->cmdbytes += cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001935 cs->lastcmdbuf = cb;
1936 spin_unlock_irqrestore(&cs->cmdlock, flags);
1937
Tilman Schmidt73a88812006-04-22 02:35:30 -07001938 spin_lock_irqsave(&cs->lock, flags);
1939 if (unlikely(!cs->connected)) {
1940 spin_unlock_irqrestore(&cs->lock, flags);
1941 gig_dbg(DEBUG_USBREQ, "%s: not connected", __func__);
Tilman Schmidt39f07222006-12-13 00:33:52 -08001942 /* flush command queue */
1943 spin_lock_irqsave(&cs->cmdlock, flags);
1944 while (cs->cmdbuf != NULL)
1945 complete_cb(cs);
1946 spin_unlock_irqrestore(&cs->cmdlock, flags);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001947 return -ENODEV;
1948 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08001949 rc = start_cbsend(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07001950 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidte3628dd2010-07-05 14:18:43 +00001951 return rc < 0 ? rc : cb->len;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001952}
1953
1954/* gigaset_write_room
1955 * tty_driver.write_room interface routine
Tilman Schmidt917f5082006-04-10 22:55:00 -07001956 * return number of characters the driver will accept to be written via
1957 * gigaset_write_cmd
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001958 * parameter:
1959 * controller state structure
1960 * return value:
1961 * number of characters
1962 */
1963static int gigaset_write_room(struct cardstate *cs)
1964{
1965 return IF_WRITEBUF;
1966}
1967
1968/* gigaset_chars_in_buffer
1969 * tty_driver.chars_in_buffer interface routine
1970 * return number of characters waiting to be sent
1971 * parameter:
1972 * controller state structure
1973 * return value:
1974 * number of characters
1975 */
1976static int gigaset_chars_in_buffer(struct cardstate *cs)
1977{
Tilman Schmidt4d1ff582007-10-16 01:27:50 -07001978 return cs->cmdbytes;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08001979}
1980
1981/* gigaset_brkchars
1982 * implementation of ioctl(GIGASET_BRKCHARS)
1983 * parameter:
1984 * controller state structure
1985 * return value:
1986 * -EINVAL (unimplemented function)
1987 */
1988static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
1989{
1990 return -EINVAL;
1991}
1992
1993
1994/* Device Initialization/Shutdown */
1995/* ============================== */
1996
1997/* Free hardware dependent part of the B channel structure
1998 * parameter:
1999 * bcs B channel structure
2000 * return value:
2001 * !=0 on success
2002 */
2003static int gigaset_freebcshw(struct bc_state *bcs)
2004{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002005 struct bas_bc_state *ubc = bcs->hw.bas;
2006 int i;
2007
2008 if (!ubc)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002009 return 0;
2010
Tilman Schmidt73a88812006-04-22 02:35:30 -07002011 /* kill URBs and tasklets before freeing - better safe than sorry */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002012 ubc->running = 0;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002013 gig_dbg(DEBUG_INIT, "%s: killing iso URBs", __func__);
2014 for (i = 0; i < BAS_OUTURBS; ++i) {
2015 usb_kill_urb(ubc->isoouturbs[i].urb);
2016 usb_free_urb(ubc->isoouturbs[i].urb);
2017 }
2018 for (i = 0; i < BAS_INURBS; ++i) {
2019 usb_kill_urb(ubc->isoinurbs[i]);
2020 usb_free_urb(ubc->isoinurbs[i]);
2021 }
Tilman Schmidt73a88812006-04-22 02:35:30 -07002022 tasklet_kill(&ubc->sent_tasklet);
2023 tasklet_kill(&ubc->rcvd_tasklet);
2024 kfree(ubc->isooutbuf);
2025 kfree(ubc);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002026 bcs->hw.bas = NULL;
2027 return 1;
2028}
2029
2030/* Initialize hardware dependent part of the B channel structure
2031 * parameter:
2032 * bcs B channel structure
2033 * return value:
2034 * !=0 on success
2035 */
2036static int gigaset_initbcshw(struct bc_state *bcs)
2037{
2038 int i;
2039 struct bas_bc_state *ubc;
2040
2041 bcs->hw.bas = ubc = kmalloc(sizeof(struct bas_bc_state), GFP_KERNEL);
2042 if (!ubc) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002043 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002044 return 0;
2045 }
2046
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002047 ubc->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002048 atomic_set(&ubc->corrbytes, 0);
2049 spin_lock_init(&ubc->isooutlock);
2050 for (i = 0; i < BAS_OUTURBS; ++i) {
2051 ubc->isoouturbs[i].urb = NULL;
2052 ubc->isoouturbs[i].bcs = bcs;
2053 }
2054 ubc->isooutdone = ubc->isooutfree = ubc->isooutovfl = NULL;
2055 ubc->numsub = 0;
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002056 ubc->isooutbuf = kmalloc(sizeof(struct isowbuf_t), GFP_KERNEL);
2057 if (!ubc->isooutbuf) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002058 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002059 kfree(ubc);
2060 bcs->hw.bas = NULL;
2061 return 0;
2062 }
2063 tasklet_init(&ubc->sent_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002064 write_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002065
2066 spin_lock_init(&ubc->isoinlock);
2067 for (i = 0; i < BAS_INURBS; ++i)
2068 ubc->isoinurbs[i] = NULL;
2069 ubc->isoindone = NULL;
2070 ubc->loststatus = -EINPROGRESS;
2071 ubc->isoinlost = 0;
2072 ubc->seqlen = 0;
2073 ubc->inbyte = 0;
2074 ubc->inbits = 0;
2075 ubc->goodbytes = 0;
2076 ubc->alignerrs = 0;
2077 ubc->fcserrs = 0;
2078 ubc->frameerrs = 0;
2079 ubc->giants = 0;
2080 ubc->runts = 0;
2081 ubc->aborts = 0;
2082 ubc->shared0s = 0;
2083 ubc->stolen0s = 0;
2084 tasklet_init(&ubc->rcvd_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +00002085 read_iso_tasklet, (unsigned long) bcs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002086 return 1;
2087}
2088
2089static void gigaset_reinitbcshw(struct bc_state *bcs)
2090{
2091 struct bas_bc_state *ubc = bcs->hw.bas;
2092
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002093 bcs->hw.bas->running = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002094 atomic_set(&bcs->hw.bas->corrbytes, 0);
2095 bcs->hw.bas->numsub = 0;
2096 spin_lock_init(&ubc->isooutlock);
2097 spin_lock_init(&ubc->isoinlock);
2098 ubc->loststatus = -EINPROGRESS;
2099}
2100
2101static void gigaset_freecshw(struct cardstate *cs)
2102{
Tilman Schmidt73a88812006-04-22 02:35:30 -07002103 /* timers, URBs and rcvbuf are disposed of in disconnect */
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002104 kfree(cs->hw.bas->int_in_buf);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002105 kfree(cs->hw.bas);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002106 cs->hw.bas = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002107}
2108
2109static int gigaset_initcshw(struct cardstate *cs)
2110{
2111 struct bas_cardstate *ucs;
2112
2113 cs->hw.bas = ucs = kmalloc(sizeof *ucs, GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002114 if (!ucs) {
2115 pr_err("out of memory\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002116 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002117 }
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002118 ucs->int_in_buf = kmalloc(IP_MSGSIZE, GFP_KERNEL);
2119 if (!ucs->int_in_buf) {
2120 kfree(ucs);
2121 pr_err("out of memory\n");
2122 return 0;
2123 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002124
2125 ucs->urb_cmd_in = NULL;
2126 ucs->urb_cmd_out = NULL;
2127 ucs->rcvbuf = NULL;
2128 ucs->rcvbuf_size = 0;
2129
2130 spin_lock_init(&ucs->lock);
2131 ucs->pending = 0;
2132
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002133 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002134 init_timer(&ucs->timer_ctrl);
2135 init_timer(&ucs->timer_atrdy);
2136 init_timer(&ucs->timer_cmd_in);
Tilman Schmidt024fd292008-02-06 01:38:26 -08002137 init_waitqueue_head(&ucs->waitqueue);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002138
2139 return 1;
2140}
2141
2142/* freeurbs
2143 * unlink and deallocate all URBs unconditionally
2144 * caller must make sure that no commands are still in progress
2145 * parameter:
2146 * cs controller state structure
2147 */
2148static void freeurbs(struct cardstate *cs)
2149{
Tilman Schmidtd48c7782006-04-10 22:55:08 -07002150 struct bas_cardstate *ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002151 struct bas_bc_state *ubc;
2152 int i, j;
2153
Tilman Schmidt39f07222006-12-13 00:33:52 -08002154 gig_dbg(DEBUG_INIT, "%s: killing URBs", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002155 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002156 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt39f07222006-12-13 00:33:52 -08002157 for (i = 0; i < BAS_OUTURBS; ++i) {
2158 usb_kill_urb(ubc->isoouturbs[i].urb);
2159 usb_free_urb(ubc->isoouturbs[i].urb);
2160 ubc->isoouturbs[i].urb = NULL;
2161 }
2162 for (i = 0; i < BAS_INURBS; ++i) {
2163 usb_kill_urb(ubc->isoinurbs[i]);
2164 usb_free_urb(ubc->isoinurbs[i]);
2165 ubc->isoinurbs[i] = NULL;
2166 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002167 }
Tilman Schmidt39f07222006-12-13 00:33:52 -08002168 usb_kill_urb(ucs->urb_int_in);
2169 usb_free_urb(ucs->urb_int_in);
2170 ucs->urb_int_in = NULL;
2171 usb_kill_urb(ucs->urb_cmd_out);
2172 usb_free_urb(ucs->urb_cmd_out);
2173 ucs->urb_cmd_out = NULL;
2174 usb_kill_urb(ucs->urb_cmd_in);
2175 usb_free_urb(ucs->urb_cmd_in);
2176 ucs->urb_cmd_in = NULL;
2177 usb_kill_urb(ucs->urb_ctrl);
2178 usb_free_urb(ucs->urb_ctrl);
2179 ucs->urb_ctrl = NULL;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002180}
2181
2182/* gigaset_probe
2183 * This function is called when a new USB device is connected.
2184 * It checks whether the new device is handled by this driver.
2185 */
2186static int gigaset_probe(struct usb_interface *interface,
2187 const struct usb_device_id *id)
2188{
2189 struct usb_host_interface *hostif;
2190 struct usb_device *udev = interface_to_usbdev(interface);
2191 struct cardstate *cs = NULL;
2192 struct bas_cardstate *ucs = NULL;
2193 struct bas_bc_state *ubc;
2194 struct usb_endpoint_descriptor *endpoint;
2195 int i, j;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002196 int rc;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002197
Tilman Schmidt1528b182010-02-22 13:09:52 +00002198 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002199 "%s: Check if device matches .. (Vendor: 0x%x, Product: 0x%x)",
2200 __func__, le16_to_cpu(udev->descriptor.idVendor),
2201 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002202
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002203 /* set required alternate setting */
2204 hostif = interface->cur_altsetting;
2205 if (hostif->desc.bAlternateSetting != 3) {
Tilman Schmidt1528b182010-02-22 13:09:52 +00002206 gig_dbg(DEBUG_INIT,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002207 "%s: wrong alternate setting %d - trying to switch",
2208 __func__, hostif->desc.bAlternateSetting);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002209 if (usb_set_interface(udev, hostif->desc.bInterfaceNumber, 3)
2210 < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002211 dev_warn(&udev->dev, "usb_set_interface failed, "
2212 "device %d interface %d altsetting %d\n",
2213 udev->devnum, hostif->desc.bInterfaceNumber,
2214 hostif->desc.bAlternateSetting);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002215 return -ENODEV;
2216 }
2217 hostif = interface->cur_altsetting;
2218 }
2219
2220 /* Reject application specific interfaces
2221 */
2222 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002223 dev_warn(&udev->dev, "%s: bInterfaceClass == %d\n",
2224 __func__, hostif->desc.bInterfaceClass);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002225 return -ENODEV;
2226 }
2227
Tilman Schmidt784d5852006-04-10 22:55:04 -07002228 dev_info(&udev->dev,
2229 "%s: Device matched (Vendor: 0x%x, Product: 0x%x)\n",
2230 __func__, le16_to_cpu(udev->descriptor.idVendor),
2231 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002232
Tilman Schmidte468c042008-02-06 01:38:29 -08002233 /* allocate memory for our device state and intialize it */
2234 cs = gigaset_initcs(driver, BAS_CHANNELS, 0, 0, cidmode,
2235 GIGASET_MODULENAME);
2236 if (!cs)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002237 return -ENODEV;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002238 ucs = cs->hw.bas;
Tilman Schmidt784d5852006-04-10 22:55:04 -07002239
2240 /* save off device structure ptrs for later use */
2241 usb_get_dev(udev);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002242 ucs->udev = udev;
2243 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002244 cs->dev = &interface->dev;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002245
2246 /* allocate URBs:
2247 * - one for the interrupt pipe
2248 * - three for the different uses of the default control pipe
2249 * - three for each isochronous pipe
2250 */
Christoph Lametere94b1762006-12-06 20:33:17 -08002251 if (!(ucs->urb_int_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2252 !(ucs->urb_cmd_in = usb_alloc_urb(0, GFP_KERNEL)) ||
2253 !(ucs->urb_cmd_out = usb_alloc_urb(0, GFP_KERNEL)) ||
2254 !(ucs->urb_ctrl = usb_alloc_urb(0, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002255 goto allocerr;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002256
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002257 for (j = 0; j < BAS_CHANNELS; ++j) {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002258 ubc = cs->bcs[j].hw.bas;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002259 for (i = 0; i < BAS_OUTURBS; ++i)
2260 if (!(ubc->isoouturbs[i].urb =
Christoph Lametere94b1762006-12-06 20:33:17 -08002261 usb_alloc_urb(BAS_NUMFRAMES, GFP_KERNEL)))
Tilman Schmidt73a88812006-04-22 02:35:30 -07002262 goto allocerr;
2263 for (i = 0; i < BAS_INURBS; ++i)
2264 if (!(ubc->isoinurbs[i] =
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;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002267 }
2268
2269 ucs->rcvbuf = NULL;
2270 ucs->rcvbuf_size = 0;
2271
2272 /* Fill the interrupt urb and send it to the core */
2273 endpoint = &hostif->endpoint[0].desc;
2274 usb_fill_int_urb(ucs->urb_int_in, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002275 usb_rcvintpipe(udev,
2276 (endpoint->bEndpointAddress) & 0x0f),
Tilman Schmidt170ebf82009-03-18 23:44:23 -07002277 ucs->int_in_buf, IP_MSGSIZE, read_int_callback, cs,
Tilman Schmidt784d5852006-04-10 22:55:04 -07002278 endpoint->bInterval);
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002279 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2280 if (rc != 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -07002281 dev_err(cs->dev, "could not submit interrupt URB: %s\n",
Tilman Schmidt73a88812006-04-22 02:35:30 -07002282 get_usb_rcmsg(rc));
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002283 goto error;
2284 }
2285
2286 /* tell the device that the driver is ready */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002287 rc = req_submit(cs->bcs, HD_DEVICE_INIT_ACK, 0, 0);
2288 if (rc != 0)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002289 goto error;
2290
2291 /* tell common part that the device is ready */
2292 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002293 cs->mstate = MS_LOCKED;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002294
2295 /* save address of controller structure */
2296 usb_set_intfdata(interface, cs);
2297
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002298 if (!gigaset_start(cs))
2299 goto error;
2300
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002301 return 0;
2302
Tilman Schmidt73a88812006-04-22 02:35:30 -07002303allocerr:
2304 dev_err(cs->dev, "could not allocate URBs\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002305error:
2306 freeurbs(cs);
Tilman Schmidt69049cc2006-04-10 22:55:16 -07002307 usb_set_intfdata(interface, NULL);
Tilman Schmidte468c042008-02-06 01:38:29 -08002308 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002309 return -ENODEV;
2310}
2311
2312/* gigaset_disconnect
2313 * This function is called when the Gigaset base is unplugged.
2314 */
2315static void gigaset_disconnect(struct usb_interface *interface)
2316{
2317 struct cardstate *cs;
2318 struct bas_cardstate *ucs;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002319 int j;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002320
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002321 cs = usb_get_intfdata(interface);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002322
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002323 ucs = cs->hw.bas;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002324
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002325 dev_info(cs->dev, "disconnecting Gigaset base\n");
Tilman Schmidt73a88812006-04-22 02:35:30 -07002326
2327 /* mark base as not ready, all channels disconnected */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002328 ucs->basstate = 0;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002329
2330 /* tell LL all channels are down */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -08002331 for (j = 0; j < BAS_CHANNELS; ++j)
Tilman Schmidt73a88812006-04-22 02:35:30 -07002332 gigaset_bchannel_down(cs->bcs + j);
2333
2334 /* stop driver (common part) */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002335 gigaset_stop(cs);
Tilman Schmidt73a88812006-04-22 02:35:30 -07002336
2337 /* stop timers and URBs, free ressources */
2338 del_timer_sync(&ucs->timer_ctrl);
2339 del_timer_sync(&ucs->timer_atrdy);
2340 del_timer_sync(&ucs->timer_cmd_in);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002341 freeurbs(cs);
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002342 usb_set_intfdata(interface, NULL);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002343 kfree(ucs->rcvbuf);
2344 ucs->rcvbuf = NULL;
2345 ucs->rcvbuf_size = 0;
Tilman Schmidtb1d47462006-04-10 22:55:07 -07002346 usb_put_dev(ucs->udev);
2347 ucs->interface = NULL;
2348 ucs->udev = NULL;
2349 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -08002350 gigaset_freecs(cs);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002351}
2352
Tilman Schmidt024fd292008-02-06 01:38:26 -08002353/* gigaset_suspend
2354 * This function is called before the USB connection is suspended.
2355 */
2356static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
2357{
2358 struct cardstate *cs = usb_get_intfdata(intf);
2359 struct bas_cardstate *ucs = cs->hw.bas;
Tilman Schmidt024fd292008-02-06 01:38:26 -08002360 int rc;
2361
2362 /* set suspend flag; this stops AT command/response traffic */
2363 if (update_basstate(ucs, BS_SUSPEND, 0) & BS_SUSPEND) {
2364 gig_dbg(DEBUG_SUSPEND, "already suspended");
2365 return 0;
2366 }
2367
2368 /* wait a bit for blocking conditions to go away */
2369 rc = wait_event_timeout(ucs->waitqueue,
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002370 !(ucs->basstate &
Tilman Schmidt024fd292008-02-06 01:38:26 -08002371 (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)),
2372 BAS_TIMEOUT*HZ/10);
2373 gig_dbg(DEBUG_SUSPEND, "wait_event_timeout() -> %d", rc);
2374
2375 /* check for conditions preventing suspend */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002376 if (ucs->basstate & (BS_B1OPEN|BS_B2OPEN|BS_ATRDPEND|BS_ATWRPEND)) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002377 dev_warn(cs->dev, "cannot suspend:\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002378 if (ucs->basstate & BS_B1OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002379 dev_warn(cs->dev, " B channel 1 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002380 if (ucs->basstate & BS_B2OPEN)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002381 dev_warn(cs->dev, " B channel 2 open\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002382 if (ucs->basstate & BS_ATRDPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002383 dev_warn(cs->dev, " receiving AT reply\n");
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002384 if (ucs->basstate & BS_ATWRPEND)
Tilman Schmidt024fd292008-02-06 01:38:26 -08002385 dev_warn(cs->dev, " sending AT command\n");
2386 update_basstate(ucs, 0, BS_SUSPEND);
2387 return -EBUSY;
2388 }
2389
2390 /* close AT channel if open */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -08002391 if (ucs->basstate & BS_ATOPEN) {
Tilman Schmidt024fd292008-02-06 01:38:26 -08002392 gig_dbg(DEBUG_SUSPEND, "closing AT channel");
2393 rc = req_submit(cs->bcs, HD_CLOSE_ATCHANNEL, 0, 0);
2394 if (rc) {
2395 update_basstate(ucs, 0, BS_SUSPEND);
2396 return rc;
2397 }
2398 wait_event_timeout(ucs->waitqueue, !ucs->pending,
2399 BAS_TIMEOUT*HZ/10);
2400 /* in case of timeout, proceed anyway */
2401 }
2402
2403 /* kill all URBs and timers that might still be pending */
2404 usb_kill_urb(ucs->urb_ctrl);
2405 usb_kill_urb(ucs->urb_int_in);
2406 del_timer_sync(&ucs->timer_ctrl);
2407
2408 gig_dbg(DEBUG_SUSPEND, "suspend complete");
2409 return 0;
2410}
2411
2412/* gigaset_resume
2413 * This function is called after the USB connection has been resumed.
2414 */
2415static int gigaset_resume(struct usb_interface *intf)
2416{
2417 struct cardstate *cs = usb_get_intfdata(intf);
2418 struct bas_cardstate *ucs = cs->hw.bas;
2419 int rc;
2420
2421 /* resubmit interrupt URB for spontaneous messages from base */
2422 rc = usb_submit_urb(ucs->urb_int_in, GFP_KERNEL);
2423 if (rc) {
2424 dev_err(cs->dev, "could not resubmit interrupt URB: %s\n",
2425 get_usb_rcmsg(rc));
2426 return rc;
2427 }
2428
2429 /* clear suspend flag to reallow activity */
2430 update_basstate(ucs, 0, BS_SUSPEND);
2431
2432 gig_dbg(DEBUG_SUSPEND, "resume complete");
2433 return 0;
2434}
2435
2436/* gigaset_pre_reset
2437 * This function is called before the USB connection is reset.
2438 */
2439static int gigaset_pre_reset(struct usb_interface *intf)
2440{
2441 /* handle just like suspend */
2442 return gigaset_suspend(intf, PMSG_ON);
2443}
2444
2445/* gigaset_post_reset
2446 * This function is called after the USB connection has been reset.
2447 */
2448static int gigaset_post_reset(struct usb_interface *intf)
2449{
2450 /* FIXME: send HD_DEVICE_INIT_ACK? */
2451
2452 /* resume operations */
2453 return gigaset_resume(intf);
2454}
2455
2456
Tilman Schmidt35dc8452007-03-29 01:20:34 -07002457static const struct gigaset_ops gigops = {
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002458 gigaset_write_cmd,
2459 gigaset_write_room,
2460 gigaset_chars_in_buffer,
2461 gigaset_brkchars,
2462 gigaset_init_bchannel,
2463 gigaset_close_bchannel,
2464 gigaset_initbcshw,
2465 gigaset_freebcshw,
2466 gigaset_reinitbcshw,
2467 gigaset_initcshw,
2468 gigaset_freecshw,
2469 gigaset_set_modem_ctrl,
2470 gigaset_baud_rate,
2471 gigaset_set_line_ctrl,
2472 gigaset_isoc_send_skb,
2473 gigaset_isoc_input,
2474};
2475
2476/* bas_gigaset_init
2477 * This function is called after the kernel module is loaded.
2478 */
2479static int __init bas_gigaset_init(void)
2480{
2481 int result;
2482
2483 /* allocate memory for our driver state and intialize it */
Tilman Schmidt7891adf2009-10-25 09:30:37 +00002484 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
2485 GIGASET_MODULENAME, GIGASET_DEVNAME,
2486 &gigops, THIS_MODULE);
2487 if (driver == NULL)
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002488 goto error;
2489
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002490 /* register this driver with the USB subsystem */
2491 result = usb_register(&gigaset_usb_driver);
2492 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002493 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002494 goto error;
2495 }
2496
Tilman Schmidtc8770dc2008-12-26 01:21:29 -08002497 pr_info(DRIVER_DESC "\n");
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002498 return 0;
2499
Tilman Schmidte468c042008-02-06 01:38:29 -08002500error:
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002501 if (driver)
2502 gigaset_freedriver(driver);
2503 driver = NULL;
2504 return -1;
2505}
2506
2507/* bas_gigaset_exit
2508 * This function is called before the kernel module is unloaded.
2509 */
2510static void __exit bas_gigaset_exit(void)
2511{
Tilman Schmidte468c042008-02-06 01:38:29 -08002512 struct bas_cardstate *ucs;
2513 int i;
Tilman Schmidt73a88812006-04-22 02:35:30 -07002514
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002515 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -07002516 * => no gigaset_start any more
2517 */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002518
Tilman Schmidte468c042008-02-06 01:38:29 -08002519 /* stop all connected devices */
2520 for (i = 0; i < driver->minors; i++) {
2521 if (gigaset_shutdown(driver->cs + i) < 0)
2522 continue; /* no device */
2523 /* from now on, no isdn callback should be possible */
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002524
Tilman Schmidte468c042008-02-06 01:38:29 -08002525 /* close all still open channels */
2526 ucs = driver->cs[i].hw.bas;
2527 if (ucs->basstate & BS_B1OPEN) {
2528 gig_dbg(DEBUG_INIT, "closing B1 channel");
2529 usb_control_msg(ucs->udev,
2530 usb_sndctrlpipe(ucs->udev, 0),
2531 HD_CLOSE_B1CHANNEL, OUT_VENDOR_REQ,
2532 0, 0, NULL, 0, BAS_TIMEOUT);
2533 }
2534 if (ucs->basstate & BS_B2OPEN) {
2535 gig_dbg(DEBUG_INIT, "closing B2 channel");
2536 usb_control_msg(ucs->udev,
2537 usb_sndctrlpipe(ucs->udev, 0),
2538 HD_CLOSE_B2CHANNEL, OUT_VENDOR_REQ,
2539 0, 0, NULL, 0, BAS_TIMEOUT);
2540 }
2541 if (ucs->basstate & BS_ATOPEN) {
2542 gig_dbg(DEBUG_INIT, "closing AT channel");
2543 usb_control_msg(ucs->udev,
2544 usb_sndctrlpipe(ucs->udev, 0),
2545 HD_CLOSE_ATCHANNEL, OUT_VENDOR_REQ,
2546 0, 0, NULL, 0, BAS_TIMEOUT);
2547 }
2548 ucs->basstate = 0;
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002549 }
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002550
2551 /* deregister this driver with the USB subsystem */
2552 usb_deregister(&gigaset_usb_driver);
2553 /* this will call the disconnect-callback */
2554 /* from now on, no disconnect/probe callback should be running */
2555
Hansjoerg Lippcf7776d2006-03-26 01:38:34 -08002556 gigaset_freedriver(driver);
2557 driver = NULL;
2558}
2559
2560
2561module_init(bas_gigaset_init);
2562module_exit(bas_gigaset_exit);
2563
2564MODULE_AUTHOR(DRIVER_AUTHOR);
2565MODULE_DESCRIPTION(DRIVER_DESC);
2566MODULE_LICENSE("GPL");