blob: 76dbb20f3065aeb23f710b99c401286c91050bfa [file] [log] [blame]
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -08001/*
2 * USB driver for Gigaset 307x directly or using M105 Data.
3 *
Tilman Schmidt70440cf2006-04-10 22:55:14 -07004 * Copyright (c) 2001 by Stefan Eilers
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -08005 * and Hansjoerg Lipp <hjlipp@web.de>.
6 *
7 * This driver was derived from the USB skeleton driver by
8 * Greg Kroah-Hartman <greg@kroah.com>
9 *
10 * =====================================================================
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License as
13 * published by the Free Software Foundation; either version 2 of
14 * the License, or (at your option) any later version.
15 * =====================================================================
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080016 */
17
18#include "gigaset.h"
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080019#include <linux/usb.h>
20#include <linux/module.h>
21#include <linux/moduleparam.h>
22
23/* Version Information */
Tilman Schmidt70440cf2006-04-10 22:55:14 -070024#define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080025#define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
26
27/* Module parameters */
28
29static int startmode = SM_ISDN;
30static int cidmode = 1;
31
32module_param(startmode, int, S_IRUGO);
33module_param(cidmode, int, S_IRUGO);
34MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
35MODULE_PARM_DESC(cidmode, "Call-ID mode");
36
37#define GIGASET_MINORS 1
38#define GIGASET_MINOR 8
39#define GIGASET_MODULENAME "usb_gigaset"
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080040#define GIGASET_DEVNAME "ttyGU"
41
Tilman Schmidt2032e2c2009-10-25 09:30:07 +000042#define IF_WRITEBUF 2000 /* arbitrary limit */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080043
44/* Values for the Gigaset M105 Data */
45#define USB_M105_VENDOR_ID 0x0681
46#define USB_M105_PRODUCT_ID 0x0009
47
48/* table of devices that work with this driver */
Tilman Schmidt2032e2c2009-10-25 09:30:07 +000049static const struct usb_device_id gigaset_table[] = {
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080050 { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
51 { } /* Terminating entry */
52};
53
54MODULE_DEVICE_TABLE(usb, gigaset_table);
55
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080056/*
57 * Control requests (empty fields: 00)
58 *
59 * RT|RQ|VALUE|INDEX|LEN |DATA
60 * In:
61 * C1 08 01
62 * Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
63 * C1 0F ll ll
64 * Get device information/status (llll: 0x200 and 0x40 seen).
65 * Real size: I only saw MIN(llll,0x64).
66 * Contents: seems to be always the same...
67 * offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
68 * offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
69 * rest: ?
70 * Out:
71 * 41 11
72 * Initialize/reset device ?
73 * 41 00 xx 00
74 * ? (xx=00 or 01; 01 on start, 00 on close)
75 * 41 07 vv mm
76 * Set/clear flags vv=value, mm=mask (see RQ 08)
77 * 41 12 xx
78 * Used before the following configuration requests are issued
79 * (with xx=0x0f). I've seen other values<0xf, though.
80 * 41 01 xx xx
81 * Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
82 * 41 03 ps bb
83 * Set byte size and parity. p: 0x20=even,0x10=odd,0x00=no parity
84 * [ 0x30: m, 0x40: s ]
85 * [s: 0: 1 stop bit; 1: 1.5; 2: 2]
86 * bb: bits/byte (seen 7 and 8)
87 * 41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
88 * ??
89 * Initialization: 01, 40, 00, 00
90 * Open device: 00 40, 00, 00
91 * yy and zz seem to be equal, either 0x00 or 0x0a
92 * (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
93 * 41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
94 * Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
95 * xx is usually 0x00 but was 0x7e before starting data transfer
Tilman Schmidt2032e2c2009-10-25 09:30:07 +000096 * in unimodem mode. So, this might be an array of characters that
97 * need special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -080098 *
99 * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
100 * flags per packet.
101 */
102
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800103/* functions called if a device of this driver is connected/disconnected */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800104static int gigaset_probe(struct usb_interface *interface,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700105 const struct usb_device_id *id);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800106static void gigaset_disconnect(struct usb_interface *interface);
107
Tilman Schmidt1ff0a522008-02-06 01:38:27 -0800108/* functions called before/after suspend */
109static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
110static int gigaset_resume(struct usb_interface *intf);
111static int gigaset_pre_reset(struct usb_interface *intf);
112
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000113static struct gigaset_driver *driver;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800114
115/* usb specific object needed to register this driver with the usb subsystem */
116static struct usb_driver gigaset_usb_driver = {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700117 .name = GIGASET_MODULENAME,
118 .probe = gigaset_probe,
119 .disconnect = gigaset_disconnect,
120 .id_table = gigaset_table,
Tilman Schmidt1ff0a522008-02-06 01:38:27 -0800121 .suspend = gigaset_suspend,
122 .resume = gigaset_resume,
123 .reset_resume = gigaset_resume,
124 .pre_reset = gigaset_pre_reset,
125 .post_reset = gigaset_resume,
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800126};
127
128struct usb_cardstate {
Tilman Schmidt917f5082006-04-10 22:55:00 -0700129 struct usb_device *udev; /* usb device pointer */
130 struct usb_interface *interface; /* interface for this device */
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800131 int busy; /* bulk output in progress */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800132
Tilman Schmidt917f5082006-04-10 22:55:00 -0700133 /* Output buffer */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700134 unsigned char *bulk_out_buffer;
135 int bulk_out_size;
136 __u8 bulk_out_endpointAddr;
137 struct urb *bulk_out_urb;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800138
Tilman Schmidt917f5082006-04-10 22:55:00 -0700139 /* Input buffer */
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000140 unsigned char *rcvbuf;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700141 int rcvbuf_size;
142 struct urb *read_urb;
143 __u8 int_in_endpointAddr;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800144
Tilman Schmidt784d5852006-04-10 22:55:04 -0700145 char bchars[6]; /* for request 0x19 */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800146};
147
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800148static inline unsigned tiocm_to_gigaset(unsigned state)
149{
150 return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
151}
152
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800153static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700154 unsigned new_state)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800155{
Tilman Schmidt784d5852006-04-10 22:55:04 -0700156 struct usb_device *udev = cs->hw.usb->udev;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800157 unsigned mask, val;
158 int r;
159
160 mask = tiocm_to_gigaset(old_state ^ new_state);
161 val = tiocm_to_gigaset(new_state);
162
Tilman Schmidt784d5852006-04-10 22:55:04 -0700163 gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700164 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41,
165 (val & 0xff) | ((mask & 0xff) << 8), 0,
166 NULL, 0, 2000 /* timeout? */);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800167 if (r < 0)
168 return r;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800169 return 0;
170}
171
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000172/*
173 * Set M105 configuration value
174 * using undocumented device commands reverse engineered from USB traces
175 * of the Siemens Windows driver
176 */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800177static int set_value(struct cardstate *cs, u8 req, u16 val)
178{
Tilman Schmidt784d5852006-04-10 22:55:04 -0700179 struct usb_device *udev = cs->hw.usb->udev;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800180 int r, r2;
181
Tilman Schmidt784d5852006-04-10 22:55:04 -0700182 gig_dbg(DEBUG_USBREQ, "request %02x (%04x)",
183 (unsigned)req, (unsigned)val);
184 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x12, 0x41,
185 0xf /*?*/, 0, NULL, 0, 2000 /*?*/);
186 /* no idea what this does */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800187 if (r < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700188 dev_err(&udev->dev, "error %d on request 0x12\n", -r);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800189 return r;
190 }
191
Tilman Schmidt784d5852006-04-10 22:55:04 -0700192 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41,
193 val, 0, NULL, 0, 2000 /*?*/);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800194 if (r < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700195 dev_err(&udev->dev, "error %d on request 0x%02x\n",
196 -r, (unsigned)req);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800197
Tilman Schmidt784d5852006-04-10 22:55:04 -0700198 r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
199 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800200 if (r2 < 0)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700201 dev_err(&udev->dev, "error %d on request 0x19\n", -r2);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800202
203 return r < 0 ? r : (r2 < 0 ? r2 : 0);
204}
205
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000206/*
207 * set the baud rate on the internal serial adapter
208 * using the undocumented parameter setting command
209 */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800210static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
211{
212 u16 val;
213 u32 rate;
214
215 cflag &= CBAUD;
216
217 switch (cflag) {
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800218 case B300: rate = 300; break;
219 case B600: rate = 600; break;
220 case B1200: rate = 1200; break;
221 case B2400: rate = 2400; break;
222 case B4800: rate = 4800; break;
223 case B9600: rate = 9600; break;
224 case B19200: rate = 19200; break;
225 case B38400: rate = 38400; break;
226 case B57600: rate = 57600; break;
227 case B115200: rate = 115200; break;
228 default:
229 rate = 9600;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700230 dev_err(cs->dev, "unsupported baudrate request 0x%x,"
231 " using default of B9600\n", cflag);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800232 }
233
234 val = 0x383fff / rate + 1;
235
236 return set_value(cs, 1, val);
237}
238
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000239/*
240 * set the line format on the internal serial adapter
241 * using the undocumented parameter setting command
242 */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800243static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
244{
245 u16 val = 0;
246
247 /* set the parity */
248 if (cflag & PARENB)
249 val |= (cflag & PARODD) ? 0x10 : 0x20;
250
251 /* set the number of data bits */
252 switch (cflag & CSIZE) {
253 case CS5:
254 val |= 5 << 8; break;
255 case CS6:
256 val |= 6 << 8; break;
257 case CS7:
258 val |= 7 << 8; break;
259 case CS8:
260 val |= 8 << 8; break;
261 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700262 dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800263 val |= 8 << 8;
264 break;
265 }
266
267 /* set the number of stop bits */
268 if (cflag & CSTOPB) {
269 if ((cflag & CSIZE) == CS5)
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000270 val |= 1; /* 1.5 stop bits */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800271 else
272 val |= 2; /* 2 stop bits */
273 }
274
275 return set_value(cs, 3, val);
276}
277
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800278
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000279/*============================================================================*/
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800280static int gigaset_init_bchannel(struct bc_state *bcs)
281{
282 /* nothing to do for M10x */
283 gigaset_bchannel_up(bcs);
284 return 0;
285}
286
287static int gigaset_close_bchannel(struct bc_state *bcs)
288{
289 /* nothing to do for M10x */
290 gigaset_bchannel_down(bcs);
291 return 0;
292}
293
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800294static int write_modem(struct cardstate *cs);
295static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb);
296
297
Tilman Schmidt917f5082006-04-10 22:55:00 -0700298/* Write tasklet handler: Continue sending current skb, or send command, or
299 * start sending an skb from the send queue.
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800300 */
301static void gigaset_modem_fill(unsigned long data)
302{
303 struct cardstate *cs = (struct cardstate *) data;
304 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
305 struct cmdbuf_t *cb;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800306 int again;
307
Tilman Schmidt784d5852006-04-10 22:55:04 -0700308 gig_dbg(DEBUG_OUTPUT, "modem_fill");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800309
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800310 if (cs->hw.usb->busy) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700311 gig_dbg(DEBUG_OUTPUT, "modem_fill: busy");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800312 return;
313 }
314
315 do {
316 again = 0;
317 if (!bcs->tx_skb) { /* no skb is being sent */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800318 cb = cs->cmdbuf;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800319 if (cb) { /* commands to send? */
Tilman Schmidt784d5852006-04-10 22:55:04 -0700320 gig_dbg(DEBUG_OUTPUT, "modem_fill: cb");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800321 if (send_cb(cs, cb) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700322 gig_dbg(DEBUG_OUTPUT,
323 "modem_fill: send_cb failed");
Tilman Schmidt917f5082006-04-10 22:55:00 -0700324 again = 1; /* no callback will be
325 called! */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800326 }
327 } else { /* skbs to send? */
328 bcs->tx_skb = skb_dequeue(&bcs->squeue);
329 if (bcs->tx_skb)
Tilman Schmidt784d5852006-04-10 22:55:04 -0700330 gig_dbg(DEBUG_INTR,
331 "Dequeued skb (Adr: %lx)!",
332 (unsigned long) bcs->tx_skb);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800333 }
334 }
335
336 if (bcs->tx_skb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700337 gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800338 if (write_modem(cs) < 0) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700339 gig_dbg(DEBUG_OUTPUT,
340 "modem_fill: write_modem failed");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800341 again = 1; /* no callback will be called! */
342 }
343 }
344 } while (again);
345}
346
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000347/*
348 * Interrupt Input URB completion routine
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800349 */
David Howells7d12e782006-10-05 14:55:46 +0100350static void gigaset_read_int_callback(struct urb *urb)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800351{
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000352 struct cardstate *cs = urb->context;
353 struct inbuf_t *inbuf = cs->inbuf;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800354 int status = urb->status;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800355 int r;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800356 unsigned numbytes;
357 unsigned char *src;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700358 unsigned long flags;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800359
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800360 if (!status) {
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800361 numbytes = urb->actual_length;
362
363 if (numbytes) {
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000364 src = cs->hw.usb->rcvbuf;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800365 if (unlikely(*src))
Tilman Schmidt784d5852006-04-10 22:55:04 -0700366 dev_warn(cs->dev,
367 "%s: There was no leading 0, but 0x%02x!\n",
368 __func__, (unsigned) *src);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800369 ++src; /* skip leading 0x00 */
370 --numbytes;
371 if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700372 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800373 gigaset_schedule_event(inbuf->cs);
374 }
375 } else
Tilman Schmidt784d5852006-04-10 22:55:04 -0700376 gig_dbg(DEBUG_INTR, "Received zero block length");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800377 } else {
378 /* The urb might have been killed. */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800379 gig_dbg(DEBUG_ANY, "%s - nonzero status received: %d",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800380 __func__, status);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800381 if (status == -ENOENT || status == -ESHUTDOWN)
382 /* killed or endpoint shutdown: don't resubmit */
383 return;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800384 }
Tilman Schmidt784d5852006-04-10 22:55:04 -0700385
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800386 /* resubmit URB */
387 spin_lock_irqsave(&cs->lock, flags);
388 if (!cs->connected) {
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700389 spin_unlock_irqrestore(&cs->lock, flags);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800390 pr_err("%s: disconnected\n", __func__);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800391 return;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800392 }
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800393 r = usb_submit_urb(urb, GFP_ATOMIC);
394 spin_unlock_irqrestore(&cs->lock, flags);
395 if (r)
396 dev_err(cs->dev, "error %d resubmitting URB\n", -r);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800397}
398
399
Tilman Schmidt917f5082006-04-10 22:55:00 -0700400/* This callback routine is called when data was transmitted to the device. */
David Howells7d12e782006-10-05 14:55:46 +0100401static void gigaset_write_bulk_callback(struct urb *urb)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800402{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700403 struct cardstate *cs = urb->context;
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800404 int status = urb->status;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700405 unsigned long flags;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800406
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800407 switch (status) {
408 case 0: /* normal completion */
409 break;
410 case -ENOENT: /* killed */
411 gig_dbg(DEBUG_ANY, "%s: killed", __func__);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800412 cs->hw.usb->busy = 0;
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800413 return;
414 default:
Tilman Schmidt784d5852006-04-10 22:55:04 -0700415 dev_err(cs->dev, "bulk transfer failed (status %d)\n",
Tilman Schmidtdbd98232008-02-06 01:38:23 -0800416 -status);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700417 /* That's all we can do. Communication problems
Tilman Schmidt784d5852006-04-10 22:55:04 -0700418 are handled by timeouts or network protocols. */
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800419 }
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800420
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700421 spin_lock_irqsave(&cs->lock, flags);
422 if (!cs->connected) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800423 pr_err("%s: disconnected\n", __func__);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700424 } else {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800425 cs->hw.usb->busy = 0;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700426 tasklet_schedule(&cs->write_tasklet);
427 }
428 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800429}
430
431static int send_cb(struct cardstate *cs, struct cmdbuf_t *cb)
432{
433 struct cmdbuf_t *tcb;
434 unsigned long flags;
435 int count;
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000436 int status = -ENOENT;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800437 struct usb_cardstate *ucs = cs->hw.usb;
438
439 do {
440 if (!cb->len) {
441 tcb = cb;
442
443 spin_lock_irqsave(&cs->cmdlock, flags);
444 cs->cmdbytes -= cs->curlen;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700445 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
446 cs->curlen, cs->cmdbytes);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800447 cs->cmdbuf = cb = cb->next;
448 if (cb) {
449 cb->prev = NULL;
450 cs->curlen = cb->len;
451 } else {
452 cs->lastcmdbuf = NULL;
453 cs->curlen = 0;
454 }
455 spin_unlock_irqrestore(&cs->cmdlock, flags);
456
457 if (tcb->wake_tasklet)
458 tasklet_schedule(tcb->wake_tasklet);
459 kfree(tcb);
460 }
461 if (cb) {
462 count = min(cb->len, ucs->bulk_out_size);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700463 gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
464
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800465 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700466 usb_sndbulkpipe(ucs->udev,
467 ucs->bulk_out_endpointAddr & 0x0f),
468 cb->buf + cb->offset, count,
469 gigaset_write_bulk_callback, cs);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800470
471 cb->offset += count;
472 cb->len -= count;
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800473 ucs->busy = 1;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800474
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700475 spin_lock_irqsave(&cs->lock, flags);
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000476 status = cs->connected ?
477 usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC) :
478 -ENODEV;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700479 spin_unlock_irqrestore(&cs->lock, flags);
480
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800481 if (status) {
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800482 ucs->busy = 0;
Tilman Schmidt50027792008-07-23 21:28:27 -0700483 dev_err(cs->dev,
484 "could not submit urb (error %d)\n",
485 -status);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700486 cb->len = 0; /* skip urb => remove cb+wakeup
487 in next loop cycle */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800488 }
489 }
Tilman Schmidt917f5082006-04-10 22:55:00 -0700490 } while (cb && status); /* next command on error */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800491
492 return status;
493}
494
Tilman Schmidt917f5082006-04-10 22:55:00 -0700495/* Send command to device. */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800496static int gigaset_write_cmd(struct cardstate *cs, const unsigned char *buf,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700497 int len, struct tasklet_struct *wake_tasklet)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800498{
499 struct cmdbuf_t *cb;
500 unsigned long flags;
501
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800502 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
Tilman Schmidt784d5852006-04-10 22:55:04 -0700503 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
Tilman Schmidt01371502006-04-10 22:55:11 -0700504 "CMD Transmit", len, buf);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800505
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800506 if (len <= 0)
507 return 0;
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000508 cb = kmalloc(sizeof(struct cmdbuf_t) + len, GFP_ATOMIC);
509 if (!cb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700510 dev_err(cs->dev, "%s: out of memory\n", __func__);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800511 return -ENOMEM;
512 }
513
514 memcpy(cb->buf, buf, len);
515 cb->len = len;
516 cb->offset = 0;
517 cb->next = NULL;
518 cb->wake_tasklet = wake_tasklet;
519
520 spin_lock_irqsave(&cs->cmdlock, flags);
521 cb->prev = cs->lastcmdbuf;
522 if (cs->lastcmdbuf)
523 cs->lastcmdbuf->next = cb;
524 else {
525 cs->cmdbuf = cb;
526 cs->curlen = len;
527 }
528 cs->cmdbytes += len;
529 cs->lastcmdbuf = cb;
530 spin_unlock_irqrestore(&cs->cmdlock, flags);
531
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700532 spin_lock_irqsave(&cs->lock, flags);
533 if (cs->connected)
534 tasklet_schedule(&cs->write_tasklet);
535 spin_unlock_irqrestore(&cs->lock, flags);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800536 return len;
537}
538
539static int gigaset_write_room(struct cardstate *cs)
540{
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800541 unsigned bytes;
542
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800543 bytes = cs->cmdbytes;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800544 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
545}
546
547static int gigaset_chars_in_buffer(struct cardstate *cs)
548{
549 return cs->cmdbytes;
550}
551
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000552/*
553 * set the break characters on the internal serial adapter
554 * using undocumented device commands reverse engineered from USB traces
555 * of the Siemens Windows driver
556 */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800557static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
558{
Tilman Schmidt784d5852006-04-10 22:55:04 -0700559 struct usb_device *udev = cs->hw.usb->udev;
560
Tilman Schmidt01371502006-04-10 22:55:11 -0700561 gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800562 memcpy(cs->hw.usb->bchars, buf, 6);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700563 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
564 0, 0, &buf, 6, 2000);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800565}
566
567static int gigaset_freebcshw(struct bc_state *bcs)
568{
Tilman Schmidt21d36492007-05-08 20:27:03 -0700569 /* unused */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800570 return 1;
571}
572
573/* Initialize the b-channel structure */
574static int gigaset_initbcshw(struct bc_state *bcs)
575{
Tilman Schmidt21d36492007-05-08 20:27:03 -0700576 /* unused */
577 bcs->hw.usb = NULL;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800578 return 1;
579}
580
581static void gigaset_reinitbcshw(struct bc_state *bcs)
582{
Tilman Schmidt21d36492007-05-08 20:27:03 -0700583 /* nothing to do for M10x */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800584}
585
586static void gigaset_freecshw(struct cardstate *cs)
587{
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800588 tasklet_kill(&cs->write_tasklet);
589 kfree(cs->hw.usb);
590}
591
592static int gigaset_initcshw(struct cardstate *cs)
593{
594 struct usb_cardstate *ucs;
595
596 cs->hw.usb = ucs =
597 kmalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800598 if (!ucs) {
599 pr_err("out of memory\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800600 return 0;
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800601 }
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800602
603 ucs->bchars[0] = 0;
604 ucs->bchars[1] = 0;
605 ucs->bchars[2] = 0;
606 ucs->bchars[3] = 0;
607 ucs->bchars[4] = 0x11;
608 ucs->bchars[5] = 0x13;
609 ucs->bulk_out_buffer = NULL;
610 ucs->bulk_out_urb = NULL;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800611 ucs->read_urb = NULL;
612 tasklet_init(&cs->write_tasklet,
Joe Perches5452fee2009-11-19 09:55:53 +0000613 gigaset_modem_fill, (unsigned long) cs);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800614
615 return 1;
616}
617
Tilman Schmidt917f5082006-04-10 22:55:00 -0700618/* Send data from current skb to the device. */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800619static int write_modem(struct cardstate *cs)
620{
Tilman Schmidtd48c7782006-04-10 22:55:08 -0700621 int ret = 0;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800622 int count;
623 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
624 struct usb_cardstate *ucs = cs->hw.usb;
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700625 unsigned long flags;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800626
Tilman Schmidt1528b182010-02-22 13:09:52 +0000627 gig_dbg(DEBUG_OUTPUT, "len: %d...", bcs->tx_skb->len);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800628
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800629 if (!bcs->tx_skb->len) {
630 dev_kfree_skb_any(bcs->tx_skb);
631 bcs->tx_skb = NULL;
632 return -EINVAL;
633 }
634
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000635 /* Copy data to bulk out buffer and transmit data */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800636 count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300637 skb_copy_from_linear_data(bcs->tx_skb, ucs->bulk_out_buffer, count);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800638 skb_pull(bcs->tx_skb, count);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800639 ucs->busy = 1;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700640 gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800641
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700642 spin_lock_irqsave(&cs->lock, flags);
643 if (cs->connected) {
644 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
645 usb_sndbulkpipe(ucs->udev,
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000646 ucs->bulk_out_endpointAddr &
647 0x0f),
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700648 ucs->bulk_out_buffer, count,
649 gigaset_write_bulk_callback, cs);
Christoph Lameter54e6ecb2006-12-06 20:33:16 -0800650 ret = usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC);
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700651 } else {
652 ret = -ENODEV;
653 }
654 spin_unlock_irqrestore(&cs->lock, flags);
655
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800656 if (ret) {
Tilman Schmidt50027792008-07-23 21:28:27 -0700657 dev_err(cs->dev, "could not submit urb (error %d)\n", -ret);
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800658 ucs->busy = 0;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800659 }
Tilman Schmidt69049cc2006-04-10 22:55:16 -0700660
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800661 if (!bcs->tx_skb->len) {
662 /* skb sent completely */
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000663 gigaset_skb_sent(bcs, bcs->tx_skb);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800664
Tilman Schmidt784d5852006-04-10 22:55:04 -0700665 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
666 (unsigned long) bcs->tx_skb);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800667 dev_kfree_skb_any(bcs->tx_skb);
668 bcs->tx_skb = NULL;
669 }
670
671 return ret;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800672}
673
674static int gigaset_probe(struct usb_interface *interface,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700675 const struct usb_device_id *id)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800676{
677 int retval;
678 struct usb_device *udev = interface_to_usbdev(interface);
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800679 struct usb_host_interface *hostif = interface->cur_altsetting;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800680 struct cardstate *cs = NULL;
681 struct usb_cardstate *ucs = NULL;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800682 struct usb_endpoint_descriptor *endpoint;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800683 int buffer_size;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800684
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800685 gig_dbg(DEBUG_ANY, "%s: Check if device matches ...", __func__);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800686
687 /* See if the device offered us matches what we can accept */
Alexey Dobriyanbfe2e932006-05-15 09:44:35 -0700688 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_M105_VENDOR_ID) ||
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800689 (le16_to_cpu(udev->descriptor.idProduct) != USB_M105_PRODUCT_ID)) {
690 gig_dbg(DEBUG_ANY, "device ID (0x%x, 0x%x) not for me - skip",
691 le16_to_cpu(udev->descriptor.idVendor),
692 le16_to_cpu(udev->descriptor.idProduct));
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800693 return -ENODEV;
694 }
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800695 if (hostif->desc.bInterfaceNumber != 0) {
696 gig_dbg(DEBUG_ANY, "interface %d not for me - skip",
697 hostif->desc.bInterfaceNumber);
698 return -ENODEV;
699 }
700 if (hostif->desc.bAlternateSetting != 0) {
701 dev_notice(&udev->dev, "unsupported altsetting %d - skip",
702 hostif->desc.bAlternateSetting);
703 return -ENODEV;
704 }
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800705 if (hostif->desc.bInterfaceClass != 255) {
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800706 dev_notice(&udev->dev, "unsupported interface class %d - skip",
707 hostif->desc.bInterfaceClass);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800708 return -ENODEV;
709 }
710
Tilman Schmidt784d5852006-04-10 22:55:04 -0700711 dev_info(&udev->dev, "%s: Device matched ... !\n", __func__);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800712
Tilman Schmidte468c042008-02-06 01:38:29 -0800713 /* allocate memory for our device state and intialize it */
714 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
715 if (!cs)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800716 return -ENODEV;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800717 ucs = cs->hw.usb;
718
Tilman Schmidt784d5852006-04-10 22:55:04 -0700719 /* save off device structure ptrs for later use */
720 usb_get_dev(udev);
721 ucs->udev = udev;
722 ucs->interface = interface;
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700723 cs->dev = &interface->dev;
724
725 /* save address of controller structure */
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000726 usb_set_intfdata(interface, cs);
Tilman Schmidt784d5852006-04-10 22:55:04 -0700727
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800728 endpoint = &hostif->endpoint[0].desc;
729
730 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
731 ucs->bulk_out_size = buffer_size;
732 ucs->bulk_out_endpointAddr = endpoint->bEndpointAddress;
733 ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
734 if (!ucs->bulk_out_buffer) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700735 dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800736 retval = -ENOMEM;
737 goto error;
738 }
739
Christoph Lametere94b1762006-12-06 20:33:17 -0800740 ucs->bulk_out_urb = usb_alloc_urb(0, GFP_KERNEL);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800741 if (!ucs->bulk_out_urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700742 dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800743 retval = -ENOMEM;
744 goto error;
745 }
746
747 endpoint = &hostif->endpoint[1].desc;
748
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800749 ucs->busy = 0;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800750
Christoph Lametere94b1762006-12-06 20:33:17 -0800751 ucs->read_urb = usb_alloc_urb(0, GFP_KERNEL);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800752 if (!ucs->read_urb) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700753 dev_err(cs->dev, "No free urbs available\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800754 retval = -ENOMEM;
755 goto error;
756 }
757 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
758 ucs->rcvbuf_size = buffer_size;
759 ucs->int_in_endpointAddr = endpoint->bEndpointAddress;
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000760 ucs->rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
761 if (!ucs->rcvbuf) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700762 dev_err(cs->dev, "Couldn't allocate rcvbuf\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800763 retval = -ENOMEM;
764 goto error;
765 }
766 /* Fill the interrupt urb and send it to the core */
767 usb_fill_int_urb(ucs->read_urb, udev,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700768 usb_rcvintpipe(udev,
769 endpoint->bEndpointAddress & 0x0f),
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000770 ucs->rcvbuf, buffer_size,
Tilman Schmidt784d5852006-04-10 22:55:04 -0700771 gigaset_read_int_callback,
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000772 cs, endpoint->bInterval);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800773
Christoph Lametere94b1762006-12-06 20:33:17 -0800774 retval = usb_submit_urb(ucs->read_urb, GFP_KERNEL);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800775 if (retval) {
Tilman Schmidt784d5852006-04-10 22:55:04 -0700776 dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800777 goto error;
778 }
779
780 /* tell common part that the device is ready */
781 if (startmode == SM_LOCKED)
Tilman Schmidt9d4bee22008-02-06 01:38:28 -0800782 cs->mstate = MS_LOCKED;
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700783
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800784 if (!gigaset_start(cs)) {
785 tasklet_kill(&cs->write_tasklet);
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000786 retval = -ENODEV;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800787 goto error;
788 }
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800789 return 0;
790
791error:
Mariusz Kozlowskiead54fc2006-11-08 15:34:17 +0100792 usb_kill_urb(ucs->read_urb);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800793 kfree(ucs->bulk_out_buffer);
Mariusz Kozlowskiead54fc2006-11-08 15:34:17 +0100794 usb_free_urb(ucs->bulk_out_urb);
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000795 kfree(ucs->rcvbuf);
Mariusz Kozlowskiead54fc2006-11-08 15:34:17 +0100796 usb_free_urb(ucs->read_urb);
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700797 usb_set_intfdata(interface, NULL);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800798 ucs->read_urb = ucs->bulk_out_urb = NULL;
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000799 ucs->rcvbuf = ucs->bulk_out_buffer = NULL;
Tilman Schmidt784d5852006-04-10 22:55:04 -0700800 usb_put_dev(ucs->udev);
801 ucs->udev = NULL;
802 ucs->interface = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -0800803 gigaset_freecs(cs);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800804 return retval;
805}
806
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800807static void gigaset_disconnect(struct usb_interface *interface)
808{
809 struct cardstate *cs;
810 struct usb_cardstate *ucs;
811
812 cs = usb_get_intfdata(interface);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800813 ucs = cs->hw.usb;
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800814
815 dev_info(cs->dev, "disconnecting Gigaset USB adapter\n");
816
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800817 usb_kill_urb(ucs->read_urb);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800818
819 gigaset_stop(cs);
820
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700821 usb_set_intfdata(interface, NULL);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800822 tasklet_kill(&cs->write_tasklet);
823
Tilman Schmidtc652cbd2008-02-06 01:38:24 -0800824 usb_kill_urb(ucs->bulk_out_urb);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800825
826 kfree(ucs->bulk_out_buffer);
Mariusz Kozlowskiead54fc2006-11-08 15:34:17 +0100827 usb_free_urb(ucs->bulk_out_urb);
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000828 kfree(ucs->rcvbuf);
Mariusz Kozlowskiead54fc2006-11-08 15:34:17 +0100829 usb_free_urb(ucs->read_urb);
Tilman Schmidt917f5082006-04-10 22:55:00 -0700830 ucs->read_urb = ucs->bulk_out_urb = NULL;
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000831 ucs->rcvbuf = ucs->bulk_out_buffer = NULL;
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800832
Tilman Schmidtb1d47462006-04-10 22:55:07 -0700833 usb_put_dev(ucs->udev);
834 ucs->interface = NULL;
835 ucs->udev = NULL;
836 cs->dev = NULL;
Tilman Schmidte468c042008-02-06 01:38:29 -0800837 gigaset_freecs(cs);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800838}
839
Tilman Schmidt1ff0a522008-02-06 01:38:27 -0800840/* gigaset_suspend
841 * This function is called before the USB connection is suspended or reset.
842 */
843static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
844{
845 struct cardstate *cs = usb_get_intfdata(intf);
846
847 /* stop activity */
848 cs->connected = 0; /* prevent rescheduling */
849 usb_kill_urb(cs->hw.usb->read_urb);
850 tasklet_kill(&cs->write_tasklet);
851 usb_kill_urb(cs->hw.usb->bulk_out_urb);
852
853 gig_dbg(DEBUG_SUSPEND, "suspend complete");
854 return 0;
855}
856
857/* gigaset_resume
858 * This function is called after the USB connection has been resumed or reset.
859 */
860static int gigaset_resume(struct usb_interface *intf)
861{
862 struct cardstate *cs = usb_get_intfdata(intf);
863 int rc;
864
865 /* resubmit interrupt URB */
866 cs->connected = 1;
867 rc = usb_submit_urb(cs->hw.usb->read_urb, GFP_KERNEL);
868 if (rc) {
869 dev_err(cs->dev, "Could not submit read URB (error %d)\n", -rc);
870 return rc;
871 }
872
873 gig_dbg(DEBUG_SUSPEND, "resume complete");
874 return 0;
875}
876
877/* gigaset_pre_reset
878 * This function is called before the USB connection is reset.
879 */
880static int gigaset_pre_reset(struct usb_interface *intf)
881{
882 /* same as suspend */
883 return gigaset_suspend(intf, PMSG_ON);
884}
885
Tilman Schmidt35dc8452007-03-29 01:20:34 -0700886static const struct gigaset_ops ops = {
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800887 gigaset_write_cmd,
888 gigaset_write_room,
889 gigaset_chars_in_buffer,
890 gigaset_brkchars,
891 gigaset_init_bchannel,
892 gigaset_close_bchannel,
893 gigaset_initbcshw,
894 gigaset_freebcshw,
895 gigaset_reinitbcshw,
896 gigaset_initcshw,
897 gigaset_freecshw,
898 gigaset_set_modem_ctrl,
899 gigaset_baud_rate,
900 gigaset_set_line_ctrl,
901 gigaset_m10x_send_skb,
902 gigaset_m10x_input,
903};
904
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000905/*
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800906 * This function is called while kernel-module is loaded
907 */
908static int __init usb_gigaset_init(void)
909{
910 int result;
911
912 /* allocate memory for our driver state and intialize it */
Tilman Schmidt2032e2c2009-10-25 09:30:07 +0000913 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
914 GIGASET_MODULENAME, GIGASET_DEVNAME,
915 &ops, THIS_MODULE);
916 if (driver == NULL)
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800917 goto error;
918
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800919 /* register this driver with the USB subsystem */
920 result = usb_register(&gigaset_usb_driver);
921 if (result < 0) {
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800922 pr_err("error %d registering USB driver\n", -result);
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800923 goto error;
924 }
925
Tilman Schmidtc8770dc2008-12-26 01:21:29 -0800926 pr_info(DRIVER_DESC "\n");
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800927 return 0;
928
Tilman Schmidte468c042008-02-06 01:38:29 -0800929error:
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800930 if (driver)
931 gigaset_freedriver(driver);
932 driver = NULL;
933 return -1;
934}
935
Tilman Schmidtb88bd952009-05-13 12:44:18 +0000936/*
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800937 * This function is called while unloading the kernel-module
938 */
939static void __exit usb_gigaset_exit(void)
940{
Tilman Schmidte468c042008-02-06 01:38:29 -0800941 int i;
942
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800943 gigaset_blockdriver(driver); /* => probe will fail
Tilman Schmidt784d5852006-04-10 22:55:04 -0700944 * => no gigaset_start any more
945 */
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800946
Tilman Schmidte468c042008-02-06 01:38:29 -0800947 /* stop all connected devices */
948 for (i = 0; i < driver->minors; i++)
949 gigaset_shutdown(driver->cs + i);
950
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800951 /* from now on, no isdn callback should be possible */
952
953 /* deregister this driver with the USB subsystem */
954 usb_deregister(&gigaset_usb_driver);
955 /* this will call the disconnect-callback */
956 /* from now on, no disconnect/probe callback should be running */
957
Hansjoerg Lipp07dc1f92006-03-26 01:38:37 -0800958 gigaset_freedriver(driver);
959 driver = NULL;
960}
961
962
963module_init(usb_gigaset_init);
964module_exit(usb_gigaset_exit);
965
966MODULE_AUTHOR(DRIVER_AUTHOR);
967MODULE_DESCRIPTION(DRIVER_DESC);
968
969MODULE_LICENSE("GPL");