1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * USB driver for Gigaset 307x directly or using M105 Data.
4 *
5 * Copyright (c) 2001 by Stefan Eilers
6 * and Hansjoerg Lipp <hjlipp@web.de>.
7 *
8 * This driver was derived from the USB skeleton driver by
9 * Greg Kroah-Hartman <greg@kroah.com>
10 *
11 * =====================================================================
12 * =====================================================================
13 */
14
15 #include "gigaset.h"
16 #include <linux/usb.h>
17 #include <linux/module.h>
18 #include <linux/moduleparam.h>
19
20 /* Version Information */
21 #define DRIVER_AUTHOR "Hansjoerg Lipp <hjlipp@web.de>, Stefan Eilers"
22 #define DRIVER_DESC "USB Driver for Gigaset 307x using M105"
23
24 /* Module parameters */
25
26 static int startmode = SM_ISDN;
27 static int cidmode = 1;
28
29 module_param(startmode, int, S_IRUGO);
30 module_param(cidmode, int, S_IRUGO);
31 MODULE_PARM_DESC(startmode, "start in isdn4linux mode");
32 MODULE_PARM_DESC(cidmode, "Call-ID mode");
33
34 #define GIGASET_MINORS 1
35 #define GIGASET_MINOR 8
36 #define GIGASET_MODULENAME "usb_gigaset"
37 #define GIGASET_DEVNAME "ttyGU"
38
39 /* length limit according to Siemens 3070usb-protokoll.doc ch. 2.1 */
40 #define IF_WRITEBUF 264
41
42 /* Values for the Gigaset M105 Data */
43 #define USB_M105_VENDOR_ID 0x0681
44 #define USB_M105_PRODUCT_ID 0x0009
45
46 /* table of devices that work with this driver */
47 static const struct usb_device_id gigaset_table[] = {
48 { USB_DEVICE(USB_M105_VENDOR_ID, USB_M105_PRODUCT_ID) },
49 { } /* Terminating entry */
50 };
51
52 MODULE_DEVICE_TABLE(usb, gigaset_table);
53
54 /*
55 * Control requests (empty fields: 00)
56 *
57 * RT|RQ|VALUE|INDEX|LEN |DATA
58 * In:
59 * C1 08 01
60 * Get flags (1 byte). Bits: 0=dtr,1=rts,3-7:?
61 * C1 0F ll ll
62 * Get device information/status (llll: 0x200 and 0x40 seen).
63 * Real size: I only saw MIN(llll,0x64).
64 * Contents: seems to be always the same...
65 * offset 0x00: Length of this structure (0x64) (len: 1,2,3 bytes)
66 * offset 0x3c: String (16 bit chars): "MCCI USB Serial V2.0"
67 * rest: ?
68 * Out:
69 * 41 11
70 * Initialize/reset device ?
71 * 41 00 xx 00
72 * ? (xx=00 or 01; 01 on start, 00 on close)
73 * 41 07 vv mm
74 * Set/clear flags vv=value, mm=mask (see RQ 08)
75 * 41 12 xx
76 * Used before the following configuration requests are issued
77 * (with xx=0x0f). I've seen other values<0xf, though.
78 * 41 01 xx xx
79 * Set baud rate. xxxx=ceil(0x384000/rate)=trunc(0x383fff/rate)+1.
80 * 41 03 ps bb
81 * Set byte size and parity. p: 0x20=even,0x10=odd,0x00=no parity
82 * [ 0x30: m, 0x40: s ]
83 * [s: 0: 1 stop bit; 1: 1.5; 2: 2]
84 * bb: bits/byte (seen 7 and 8)
85 * 41 13 -- -- -- -- 10 00 ww 00 00 00 xx 00 00 00 yy 00 00 00 zz 00 00 00
86 * ??
87 * Initialization: 01, 40, 00, 00
88 * Open device: 00 40, 00, 00
89 * yy and zz seem to be equal, either 0x00 or 0x0a
90 * (ww,xx) pairs seen: (00,00), (00,40), (01,40), (09,80), (19,80)
91 * 41 19 -- -- -- -- 06 00 00 00 00 xx 11 13
92 * Used after every "configuration sequence" (RQ 12, RQs 01/03/13).
93 * xx is usually 0x00 but was 0x7e before starting data transfer
94 * in unimodem mode. So, this might be an array of characters that
95 * need special treatment ("commit all bufferd data"?), 11=^Q, 13=^S.
96 *
97 * Unimodem mode: use "modprobe ppp_async flag_time=0" as the device _needs_ two
98 * flags per packet.
99 */
100
101 /* functions called if a device of this driver is connected/disconnected */
102 static int gigaset_probe(struct usb_interface *interface,
103 const struct usb_device_id *id);
104 static void gigaset_disconnect(struct usb_interface *interface);
105
106 /* functions called before/after suspend */
107 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message);
108 static int gigaset_resume(struct usb_interface *intf);
109 static int gigaset_pre_reset(struct usb_interface *intf);
110
111 static struct gigaset_driver *driver;
112
113 /* usb specific object needed to register this driver with the usb subsystem */
114 static struct usb_driver gigaset_usb_driver = {
115 .name = GIGASET_MODULENAME,
116 .probe = gigaset_probe,
117 .disconnect = gigaset_disconnect,
118 .id_table = gigaset_table,
119 .suspend = gigaset_suspend,
120 .resume = gigaset_resume,
121 .reset_resume = gigaset_resume,
122 .pre_reset = gigaset_pre_reset,
123 .post_reset = gigaset_resume,
124 .disable_hub_initiated_lpm = 1,
125 };
126
127 struct usb_cardstate {
128 struct usb_device *udev; /* usb device pointer */
129 struct usb_interface *interface; /* interface for this device */
130 int busy; /* bulk output in progress */
131
132 /* Output buffer */
133 unsigned char *bulk_out_buffer;
134 int bulk_out_size;
135 int bulk_out_epnum;
136 struct urb *bulk_out_urb;
137
138 /* Input buffer */
139 unsigned char *rcvbuf;
140 int rcvbuf_size;
141 struct urb *read_urb;
142
143 char bchars[6]; /* for request 0x19 */
144 };
145
tiocm_to_gigaset(unsigned state)146 static inline unsigned tiocm_to_gigaset(unsigned state)
147 {
148 return ((state & TIOCM_DTR) ? 1 : 0) | ((state & TIOCM_RTS) ? 2 : 0);
149 }
150
gigaset_set_modem_ctrl(struct cardstate * cs,unsigned old_state,unsigned new_state)151 static int gigaset_set_modem_ctrl(struct cardstate *cs, unsigned old_state,
152 unsigned new_state)
153 {
154 struct usb_device *udev = cs->hw.usb->udev;
155 unsigned mask, val;
156 int r;
157
158 mask = tiocm_to_gigaset(old_state ^ new_state);
159 val = tiocm_to_gigaset(new_state);
160
161 gig_dbg(DEBUG_USBREQ, "set flags 0x%02x with mask 0x%02x", val, mask);
162 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 7, 0x41,
163 (val & 0xff) | ((mask & 0xff) << 8), 0,
164 NULL, 0, 2000 /* timeout? */);
165 if (r < 0)
166 return r;
167 return 0;
168 }
169
170 /*
171 * Set M105 configuration value
172 * using undocumented device commands reverse engineered from USB traces
173 * of the Siemens Windows driver
174 */
set_value(struct cardstate * cs,u8 req,u16 val)175 static int set_value(struct cardstate *cs, u8 req, u16 val)
176 {
177 struct usb_device *udev = cs->hw.usb->udev;
178 int r, r2;
179
180 gig_dbg(DEBUG_USBREQ, "request %02x (%04x)",
181 (unsigned)req, (unsigned)val);
182 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x12, 0x41,
183 0xf /*?*/, 0, NULL, 0, 2000 /*?*/);
184 /* no idea what this does */
185 if (r < 0) {
186 dev_err(&udev->dev, "error %d on request 0x12\n", -r);
187 return r;
188 }
189
190 r = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), req, 0x41,
191 val, 0, NULL, 0, 2000 /*?*/);
192 if (r < 0)
193 dev_err(&udev->dev, "error %d on request 0x%02x\n",
194 -r, (unsigned)req);
195
196 r2 = usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
197 0, 0, cs->hw.usb->bchars, 6, 2000 /*?*/);
198 if (r2 < 0)
199 dev_err(&udev->dev, "error %d on request 0x19\n", -r2);
200
201 return r < 0 ? r : (r2 < 0 ? r2 : 0);
202 }
203
204 /*
205 * set the baud rate on the internal serial adapter
206 * using the undocumented parameter setting command
207 */
gigaset_baud_rate(struct cardstate * cs,unsigned cflag)208 static int gigaset_baud_rate(struct cardstate *cs, unsigned cflag)
209 {
210 u16 val;
211 u32 rate;
212
213 cflag &= CBAUD;
214
215 switch (cflag) {
216 case B300: rate = 300; break;
217 case B600: rate = 600; break;
218 case B1200: rate = 1200; break;
219 case B2400: rate = 2400; break;
220 case B4800: rate = 4800; break;
221 case B9600: rate = 9600; break;
222 case B19200: rate = 19200; break;
223 case B38400: rate = 38400; break;
224 case B57600: rate = 57600; break;
225 case B115200: rate = 115200; break;
226 default:
227 rate = 9600;
228 dev_err(cs->dev, "unsupported baudrate request 0x%x,"
229 " using default of B9600\n", cflag);
230 }
231
232 val = 0x383fff / rate + 1;
233
234 return set_value(cs, 1, val);
235 }
236
237 /*
238 * set the line format on the internal serial adapter
239 * using the undocumented parameter setting command
240 */
gigaset_set_line_ctrl(struct cardstate * cs,unsigned cflag)241 static int gigaset_set_line_ctrl(struct cardstate *cs, unsigned cflag)
242 {
243 u16 val = 0;
244
245 /* set the parity */
246 if (cflag & PARENB)
247 val |= (cflag & PARODD) ? 0x10 : 0x20;
248
249 /* set the number of data bits */
250 switch (cflag & CSIZE) {
251 case CS5:
252 val |= 5 << 8; break;
253 case CS6:
254 val |= 6 << 8; break;
255 case CS7:
256 val |= 7 << 8; break;
257 case CS8:
258 val |= 8 << 8; break;
259 default:
260 dev_err(cs->dev, "CSIZE was not CS5-CS8, using default of 8\n");
261 val |= 8 << 8;
262 break;
263 }
264
265 /* set the number of stop bits */
266 if (cflag & CSTOPB) {
267 if ((cflag & CSIZE) == CS5)
268 val |= 1; /* 1.5 stop bits */
269 else
270 val |= 2; /* 2 stop bits */
271 }
272
273 return set_value(cs, 3, val);
274 }
275
276
277 /*============================================================================*/
gigaset_init_bchannel(struct bc_state * bcs)278 static int gigaset_init_bchannel(struct bc_state *bcs)
279 {
280 /* nothing to do for M10x */
281 gigaset_bchannel_up(bcs);
282 return 0;
283 }
284
gigaset_close_bchannel(struct bc_state * bcs)285 static int gigaset_close_bchannel(struct bc_state *bcs)
286 {
287 /* nothing to do for M10x */
288 gigaset_bchannel_down(bcs);
289 return 0;
290 }
291
292 static int write_modem(struct cardstate *cs);
293 static int send_cb(struct cardstate *cs);
294
295
296 /* Write tasklet handler: Continue sending current skb, or send command, or
297 * start sending an skb from the send queue.
298 */
gigaset_modem_fill(unsigned long data)299 static void gigaset_modem_fill(unsigned long data)
300 {
301 struct cardstate *cs = (struct cardstate *) data;
302 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
303
304 gig_dbg(DEBUG_OUTPUT, "modem_fill");
305
306 if (cs->hw.usb->busy) {
307 gig_dbg(DEBUG_OUTPUT, "modem_fill: busy");
308 return;
309 }
310
311 again:
312 if (!bcs->tx_skb) { /* no skb is being sent */
313 if (cs->cmdbuf) { /* commands to send? */
314 gig_dbg(DEBUG_OUTPUT, "modem_fill: cb");
315 if (send_cb(cs) < 0) {
316 gig_dbg(DEBUG_OUTPUT,
317 "modem_fill: send_cb failed");
318 goto again; /* no callback will be called! */
319 }
320 return;
321 }
322
323 /* skbs to send? */
324 bcs->tx_skb = skb_dequeue(&bcs->squeue);
325 if (!bcs->tx_skb)
326 return;
327
328 gig_dbg(DEBUG_INTR, "Dequeued skb (Adr: %lx)!",
329 (unsigned long) bcs->tx_skb);
330 }
331
332 gig_dbg(DEBUG_OUTPUT, "modem_fill: tx_skb");
333 if (write_modem(cs) < 0) {
334 gig_dbg(DEBUG_OUTPUT, "modem_fill: write_modem failed");
335 goto again; /* no callback will be called! */
336 }
337 }
338
339 /*
340 * Interrupt Input URB completion routine
341 */
gigaset_read_int_callback(struct urb * urb)342 static void gigaset_read_int_callback(struct urb *urb)
343 {
344 struct cardstate *cs = urb->context;
345 struct inbuf_t *inbuf = cs->inbuf;
346 int status = urb->status;
347 int r;
348 unsigned numbytes;
349 unsigned char *src;
350 unsigned long flags;
351
352 if (!status) {
353 numbytes = urb->actual_length;
354
355 if (numbytes) {
356 src = cs->hw.usb->rcvbuf;
357 if (unlikely(*src))
358 dev_warn(cs->dev,
359 "%s: There was no leading 0, but 0x%02x!\n",
360 __func__, (unsigned) *src);
361 ++src; /* skip leading 0x00 */
362 --numbytes;
363 if (gigaset_fill_inbuf(inbuf, src, numbytes)) {
364 gig_dbg(DEBUG_INTR, "%s-->BH", __func__);
365 gigaset_schedule_event(inbuf->cs);
366 }
367 } else
368 gig_dbg(DEBUG_INTR, "Received zero block length");
369 } else {
370 /* The urb might have been killed. */
371 gig_dbg(DEBUG_ANY, "%s - nonzero status received: %d",
372 __func__, status);
373 if (status == -ENOENT || status == -ESHUTDOWN)
374 /* killed or endpoint shutdown: don't resubmit */
375 return;
376 }
377
378 /* resubmit URB */
379 spin_lock_irqsave(&cs->lock, flags);
380 if (!cs->connected) {
381 spin_unlock_irqrestore(&cs->lock, flags);
382 pr_err("%s: disconnected\n", __func__);
383 return;
384 }
385 r = usb_submit_urb(urb, GFP_ATOMIC);
386 spin_unlock_irqrestore(&cs->lock, flags);
387 if (r)
388 dev_err(cs->dev, "error %d resubmitting URB\n", -r);
389 }
390
391
392 /* This callback routine is called when data was transmitted to the device. */
gigaset_write_bulk_callback(struct urb * urb)393 static void gigaset_write_bulk_callback(struct urb *urb)
394 {
395 struct cardstate *cs = urb->context;
396 int status = urb->status;
397 unsigned long flags;
398
399 switch (status) {
400 case 0: /* normal completion */
401 break;
402 case -ENOENT: /* killed */
403 gig_dbg(DEBUG_ANY, "%s: killed", __func__);
404 cs->hw.usb->busy = 0;
405 return;
406 default:
407 dev_err(cs->dev, "bulk transfer failed (status %d)\n",
408 -status);
409 /* That's all we can do. Communication problems
410 are handled by timeouts or network protocols. */
411 }
412
413 spin_lock_irqsave(&cs->lock, flags);
414 if (!cs->connected) {
415 pr_err("%s: disconnected\n", __func__);
416 } else {
417 cs->hw.usb->busy = 0;
418 tasklet_schedule(&cs->write_tasklet);
419 }
420 spin_unlock_irqrestore(&cs->lock, flags);
421 }
422
send_cb(struct cardstate * cs)423 static int send_cb(struct cardstate *cs)
424 {
425 struct cmdbuf_t *cb = cs->cmdbuf;
426 unsigned long flags;
427 int count;
428 int status = -ENOENT;
429 struct usb_cardstate *ucs = cs->hw.usb;
430
431 do {
432 if (!cb->len) {
433 spin_lock_irqsave(&cs->cmdlock, flags);
434 cs->cmdbytes -= cs->curlen;
435 gig_dbg(DEBUG_OUTPUT, "send_cb: sent %u bytes, %u left",
436 cs->curlen, cs->cmdbytes);
437 cs->cmdbuf = cb->next;
438 if (cs->cmdbuf) {
439 cs->cmdbuf->prev = NULL;
440 cs->curlen = cs->cmdbuf->len;
441 } else {
442 cs->lastcmdbuf = NULL;
443 cs->curlen = 0;
444 }
445 spin_unlock_irqrestore(&cs->cmdlock, flags);
446
447 if (cb->wake_tasklet)
448 tasklet_schedule(cb->wake_tasklet);
449 kfree(cb);
450
451 cb = cs->cmdbuf;
452 }
453
454 if (cb) {
455 count = min(cb->len, ucs->bulk_out_size);
456 gig_dbg(DEBUG_OUTPUT, "send_cb: send %d bytes", count);
457
458 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
459 usb_sndbulkpipe(ucs->udev,
460 ucs->bulk_out_epnum),
461 cb->buf + cb->offset, count,
462 gigaset_write_bulk_callback, cs);
463
464 cb->offset += count;
465 cb->len -= count;
466 ucs->busy = 1;
467
468 spin_lock_irqsave(&cs->lock, flags);
469 status = cs->connected ?
470 usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC) :
471 -ENODEV;
472 spin_unlock_irqrestore(&cs->lock, flags);
473
474 if (status) {
475 ucs->busy = 0;
476 dev_err(cs->dev,
477 "could not submit urb (error %d)\n",
478 -status);
479 cb->len = 0; /* skip urb => remove cb+wakeup
480 in next loop cycle */
481 }
482 }
483 } while (cb && status); /* next command on error */
484
485 return status;
486 }
487
488 /* Send command to device. */
gigaset_write_cmd(struct cardstate * cs,struct cmdbuf_t * cb)489 static int gigaset_write_cmd(struct cardstate *cs, struct cmdbuf_t *cb)
490 {
491 unsigned long flags;
492 int len;
493
494 gigaset_dbg_buffer(cs->mstate != MS_LOCKED ?
495 DEBUG_TRANSCMD : DEBUG_LOCKCMD,
496 "CMD Transmit", cb->len, cb->buf);
497
498 spin_lock_irqsave(&cs->cmdlock, flags);
499 cb->prev = cs->lastcmdbuf;
500 if (cs->lastcmdbuf)
501 cs->lastcmdbuf->next = cb;
502 else {
503 cs->cmdbuf = cb;
504 cs->curlen = cb->len;
505 }
506 cs->cmdbytes += cb->len;
507 cs->lastcmdbuf = cb;
508 spin_unlock_irqrestore(&cs->cmdlock, flags);
509
510 spin_lock_irqsave(&cs->lock, flags);
511 len = cb->len;
512 if (cs->connected)
513 tasklet_schedule(&cs->write_tasklet);
514 spin_unlock_irqrestore(&cs->lock, flags);
515 return len;
516 }
517
gigaset_write_room(struct cardstate * cs)518 static int gigaset_write_room(struct cardstate *cs)
519 {
520 unsigned bytes;
521
522 bytes = cs->cmdbytes;
523 return bytes < IF_WRITEBUF ? IF_WRITEBUF - bytes : 0;
524 }
525
gigaset_chars_in_buffer(struct cardstate * cs)526 static int gigaset_chars_in_buffer(struct cardstate *cs)
527 {
528 return cs->cmdbytes;
529 }
530
531 /*
532 * set the break characters on the internal serial adapter
533 * using undocumented device commands reverse engineered from USB traces
534 * of the Siemens Windows driver
535 */
gigaset_brkchars(struct cardstate * cs,const unsigned char buf[6])536 static int gigaset_brkchars(struct cardstate *cs, const unsigned char buf[6])
537 {
538 struct usb_device *udev = cs->hw.usb->udev;
539
540 gigaset_dbg_buffer(DEBUG_USBREQ, "brkchars", 6, buf);
541 memcpy(cs->hw.usb->bchars, buf, 6);
542 return usb_control_msg(udev, usb_sndctrlpipe(udev, 0), 0x19, 0x41,
543 0, 0, &buf, 6, 2000);
544 }
545
gigaset_freebcshw(struct bc_state * bcs)546 static void gigaset_freebcshw(struct bc_state *bcs)
547 {
548 /* unused */
549 }
550
551 /* Initialize the b-channel structure */
gigaset_initbcshw(struct bc_state * bcs)552 static int gigaset_initbcshw(struct bc_state *bcs)
553 {
554 /* unused */
555 bcs->hw.usb = NULL;
556 return 0;
557 }
558
gigaset_reinitbcshw(struct bc_state * bcs)559 static void gigaset_reinitbcshw(struct bc_state *bcs)
560 {
561 /* nothing to do for M10x */
562 }
563
gigaset_freecshw(struct cardstate * cs)564 static void gigaset_freecshw(struct cardstate *cs)
565 {
566 tasklet_kill(&cs->write_tasklet);
567 kfree(cs->hw.usb);
568 }
569
gigaset_initcshw(struct cardstate * cs)570 static int gigaset_initcshw(struct cardstate *cs)
571 {
572 struct usb_cardstate *ucs;
573
574 cs->hw.usb = ucs = kzalloc(sizeof(struct usb_cardstate), GFP_KERNEL);
575 if (!ucs) {
576 pr_err("out of memory\n");
577 return -ENOMEM;
578 }
579
580 ucs->bchars[0] = 0;
581 ucs->bchars[1] = 0;
582 ucs->bchars[2] = 0;
583 ucs->bchars[3] = 0;
584 ucs->bchars[4] = 0x11;
585 ucs->bchars[5] = 0x13;
586 tasklet_init(&cs->write_tasklet,
587 gigaset_modem_fill, (unsigned long) cs);
588
589 return 0;
590 }
591
592 /* Send data from current skb to the device. */
write_modem(struct cardstate * cs)593 static int write_modem(struct cardstate *cs)
594 {
595 int ret = 0;
596 int count;
597 struct bc_state *bcs = &cs->bcs[0]; /* only one channel */
598 struct usb_cardstate *ucs = cs->hw.usb;
599 unsigned long flags;
600
601 gig_dbg(DEBUG_OUTPUT, "len: %d...", bcs->tx_skb->len);
602
603 if (!bcs->tx_skb->len) {
604 dev_kfree_skb_any(bcs->tx_skb);
605 bcs->tx_skb = NULL;
606 return -EINVAL;
607 }
608
609 /* Copy data to bulk out buffer and transmit data */
610 count = min(bcs->tx_skb->len, (unsigned) ucs->bulk_out_size);
611 skb_copy_from_linear_data(bcs->tx_skb, ucs->bulk_out_buffer, count);
612 skb_pull(bcs->tx_skb, count);
613 ucs->busy = 1;
614 gig_dbg(DEBUG_OUTPUT, "write_modem: send %d bytes", count);
615
616 spin_lock_irqsave(&cs->lock, flags);
617 if (cs->connected) {
618 usb_fill_bulk_urb(ucs->bulk_out_urb, ucs->udev,
619 usb_sndbulkpipe(ucs->udev,
620 ucs->bulk_out_epnum),
621 ucs->bulk_out_buffer, count,
622 gigaset_write_bulk_callback, cs);
623 ret = usb_submit_urb(ucs->bulk_out_urb, GFP_ATOMIC);
624 } else {
625 ret = -ENODEV;
626 }
627 spin_unlock_irqrestore(&cs->lock, flags);
628
629 if (ret) {
630 dev_err(cs->dev, "could not submit urb (error %d)\n", -ret);
631 ucs->busy = 0;
632 }
633
634 if (!bcs->tx_skb->len) {
635 /* skb sent completely */
636 gigaset_skb_sent(bcs, bcs->tx_skb);
637
638 gig_dbg(DEBUG_INTR, "kfree skb (Adr: %lx)!",
639 (unsigned long) bcs->tx_skb);
640 dev_kfree_skb_any(bcs->tx_skb);
641 bcs->tx_skb = NULL;
642 }
643
644 return ret;
645 }
646
gigaset_probe(struct usb_interface * interface,const struct usb_device_id * id)647 static int gigaset_probe(struct usb_interface *interface,
648 const struct usb_device_id *id)
649 {
650 int retval;
651 struct usb_device *udev = interface_to_usbdev(interface);
652 struct usb_host_interface *hostif = interface->cur_altsetting;
653 struct cardstate *cs = NULL;
654 struct usb_cardstate *ucs = NULL;
655 struct usb_endpoint_descriptor *endpoint;
656 int buffer_size;
657
658 gig_dbg(DEBUG_ANY, "%s: Check if device matches ...", __func__);
659
660 /* See if the device offered us matches what we can accept */
661 if ((le16_to_cpu(udev->descriptor.idVendor) != USB_M105_VENDOR_ID) ||
662 (le16_to_cpu(udev->descriptor.idProduct) != USB_M105_PRODUCT_ID)) {
663 gig_dbg(DEBUG_ANY, "device ID (0x%x, 0x%x) not for me - skip",
664 le16_to_cpu(udev->descriptor.idVendor),
665 le16_to_cpu(udev->descriptor.idProduct));
666 return -ENODEV;
667 }
668 if (hostif->desc.bInterfaceNumber != 0) {
669 gig_dbg(DEBUG_ANY, "interface %d not for me - skip",
670 hostif->desc.bInterfaceNumber);
671 return -ENODEV;
672 }
673 if (hostif->desc.bAlternateSetting != 0) {
674 dev_notice(&udev->dev, "unsupported altsetting %d - skip",
675 hostif->desc.bAlternateSetting);
676 return -ENODEV;
677 }
678 if (hostif->desc.bInterfaceClass != 255) {
679 dev_notice(&udev->dev, "unsupported interface class %d - skip",
680 hostif->desc.bInterfaceClass);
681 return -ENODEV;
682 }
683
684 if (hostif->desc.bNumEndpoints < 2) {
685 dev_err(&interface->dev, "missing endpoints\n");
686 return -ENODEV;
687 }
688
689 dev_info(&udev->dev, "%s: Device matched ... !\n", __func__);
690
691 /* allocate memory for our device state and initialize it */
692 cs = gigaset_initcs(driver, 1, 1, 0, cidmode, GIGASET_MODULENAME);
693 if (!cs)
694 return -ENODEV;
695 ucs = cs->hw.usb;
696
697 /* save off device structure ptrs for later use */
698 usb_get_dev(udev);
699 ucs->udev = udev;
700 ucs->interface = interface;
701 cs->dev = &interface->dev;
702
703 /* save address of controller structure */
704 usb_set_intfdata(interface, cs);
705
706 endpoint = &hostif->endpoint[0].desc;
707
708 if (!usb_endpoint_is_bulk_out(endpoint)) {
709 dev_err(&interface->dev, "missing bulk-out endpoint\n");
710 retval = -ENODEV;
711 goto error;
712 }
713
714 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
715 ucs->bulk_out_size = buffer_size;
716 ucs->bulk_out_epnum = usb_endpoint_num(endpoint);
717 ucs->bulk_out_buffer = kmalloc(buffer_size, GFP_KERNEL);
718 if (!ucs->bulk_out_buffer) {
719 dev_err(cs->dev, "Couldn't allocate bulk_out_buffer\n");
720 retval = -ENOMEM;
721 goto error;
722 }
723
724 ucs->bulk_out_urb = usb_alloc_urb(0, GFP_KERNEL);
725 if (!ucs->bulk_out_urb) {
726 dev_err(cs->dev, "Couldn't allocate bulk_out_urb\n");
727 retval = -ENOMEM;
728 goto error;
729 }
730
731 endpoint = &hostif->endpoint[1].desc;
732
733 if (!usb_endpoint_is_int_in(endpoint)) {
734 dev_err(&interface->dev, "missing int-in endpoint\n");
735 retval = -ENODEV;
736 goto error;
737 }
738
739 ucs->busy = 0;
740
741 ucs->read_urb = usb_alloc_urb(0, GFP_KERNEL);
742 if (!ucs->read_urb) {
743 dev_err(cs->dev, "No free urbs available\n");
744 retval = -ENOMEM;
745 goto error;
746 }
747 buffer_size = le16_to_cpu(endpoint->wMaxPacketSize);
748 ucs->rcvbuf_size = buffer_size;
749 ucs->rcvbuf = kmalloc(buffer_size, GFP_KERNEL);
750 if (!ucs->rcvbuf) {
751 dev_err(cs->dev, "Couldn't allocate rcvbuf\n");
752 retval = -ENOMEM;
753 goto error;
754 }
755 /* Fill the interrupt urb and send it to the core */
756 usb_fill_int_urb(ucs->read_urb, udev,
757 usb_rcvintpipe(udev, usb_endpoint_num(endpoint)),
758 ucs->rcvbuf, buffer_size,
759 gigaset_read_int_callback,
760 cs, endpoint->bInterval);
761
762 retval = usb_submit_urb(ucs->read_urb, GFP_KERNEL);
763 if (retval) {
764 dev_err(cs->dev, "Could not submit URB (error %d)\n", -retval);
765 goto error;
766 }
767
768 /* tell common part that the device is ready */
769 if (startmode == SM_LOCKED)
770 cs->mstate = MS_LOCKED;
771
772 retval = gigaset_start(cs);
773 if (retval < 0) {
774 tasklet_kill(&cs->write_tasklet);
775 goto error;
776 }
777 return 0;
778
779 error:
780 usb_kill_urb(ucs->read_urb);
781 kfree(ucs->bulk_out_buffer);
782 usb_free_urb(ucs->bulk_out_urb);
783 kfree(ucs->rcvbuf);
784 usb_free_urb(ucs->read_urb);
785 usb_set_intfdata(interface, NULL);
786 ucs->read_urb = ucs->bulk_out_urb = NULL;
787 ucs->rcvbuf = ucs->bulk_out_buffer = NULL;
788 usb_put_dev(ucs->udev);
789 ucs->udev = NULL;
790 ucs->interface = NULL;
791 gigaset_freecs(cs);
792 return retval;
793 }
794
gigaset_disconnect(struct usb_interface * interface)795 static void gigaset_disconnect(struct usb_interface *interface)
796 {
797 struct cardstate *cs;
798 struct usb_cardstate *ucs;
799
800 cs = usb_get_intfdata(interface);
801 ucs = cs->hw.usb;
802
803 dev_info(cs->dev, "disconnecting Gigaset USB adapter\n");
804
805 usb_kill_urb(ucs->read_urb);
806
807 gigaset_stop(cs);
808
809 usb_set_intfdata(interface, NULL);
810 tasklet_kill(&cs->write_tasklet);
811
812 usb_kill_urb(ucs->bulk_out_urb);
813
814 kfree(ucs->bulk_out_buffer);
815 usb_free_urb(ucs->bulk_out_urb);
816 kfree(ucs->rcvbuf);
817 usb_free_urb(ucs->read_urb);
818 ucs->read_urb = ucs->bulk_out_urb = NULL;
819 ucs->rcvbuf = ucs->bulk_out_buffer = NULL;
820
821 usb_put_dev(ucs->udev);
822 ucs->interface = NULL;
823 ucs->udev = NULL;
824 cs->dev = NULL;
825 gigaset_freecs(cs);
826 }
827
828 /* gigaset_suspend
829 * This function is called before the USB connection is suspended or reset.
830 */
gigaset_suspend(struct usb_interface * intf,pm_message_t message)831 static int gigaset_suspend(struct usb_interface *intf, pm_message_t message)
832 {
833 struct cardstate *cs = usb_get_intfdata(intf);
834
835 /* stop activity */
836 cs->connected = 0; /* prevent rescheduling */
837 usb_kill_urb(cs->hw.usb->read_urb);
838 tasklet_kill(&cs->write_tasklet);
839 usb_kill_urb(cs->hw.usb->bulk_out_urb);
840
841 gig_dbg(DEBUG_SUSPEND, "suspend complete");
842 return 0;
843 }
844
845 /* gigaset_resume
846 * This function is called after the USB connection has been resumed or reset.
847 */
gigaset_resume(struct usb_interface * intf)848 static int gigaset_resume(struct usb_interface *intf)
849 {
850 struct cardstate *cs = usb_get_intfdata(intf);
851 int rc;
852
853 /* resubmit interrupt URB */
854 cs->connected = 1;
855 rc = usb_submit_urb(cs->hw.usb->read_urb, GFP_KERNEL);
856 if (rc) {
857 dev_err(cs->dev, "Could not submit read URB (error %d)\n", -rc);
858 return rc;
859 }
860
861 gig_dbg(DEBUG_SUSPEND, "resume complete");
862 return 0;
863 }
864
865 /* gigaset_pre_reset
866 * This function is called before the USB connection is reset.
867 */
gigaset_pre_reset(struct usb_interface * intf)868 static int gigaset_pre_reset(struct usb_interface *intf)
869 {
870 /* same as suspend */
871 return gigaset_suspend(intf, PMSG_ON);
872 }
873
874 static const struct gigaset_ops ops = {
875 .write_cmd = gigaset_write_cmd,
876 .write_room = gigaset_write_room,
877 .chars_in_buffer = gigaset_chars_in_buffer,
878 .brkchars = gigaset_brkchars,
879 .init_bchannel = gigaset_init_bchannel,
880 .close_bchannel = gigaset_close_bchannel,
881 .initbcshw = gigaset_initbcshw,
882 .freebcshw = gigaset_freebcshw,
883 .reinitbcshw = gigaset_reinitbcshw,
884 .initcshw = gigaset_initcshw,
885 .freecshw = gigaset_freecshw,
886 .set_modem_ctrl = gigaset_set_modem_ctrl,
887 .baud_rate = gigaset_baud_rate,
888 .set_line_ctrl = gigaset_set_line_ctrl,
889 .send_skb = gigaset_m10x_send_skb,
890 .handle_input = gigaset_m10x_input,
891 };
892
893 /*
894 * This function is called while kernel-module is loaded
895 */
usb_gigaset_init(void)896 static int __init usb_gigaset_init(void)
897 {
898 int result;
899
900 /* allocate memory for our driver state and initialize it */
901 driver = gigaset_initdriver(GIGASET_MINOR, GIGASET_MINORS,
902 GIGASET_MODULENAME, GIGASET_DEVNAME,
903 &ops, THIS_MODULE);
904 if (driver == NULL) {
905 result = -ENOMEM;
906 goto error;
907 }
908
909 /* register this driver with the USB subsystem */
910 result = usb_register(&gigaset_usb_driver);
911 if (result < 0) {
912 pr_err("error %d registering USB driver\n", -result);
913 goto error;
914 }
915
916 pr_info(DRIVER_DESC "\n");
917 return 0;
918
919 error:
920 if (driver)
921 gigaset_freedriver(driver);
922 driver = NULL;
923 return result;
924 }
925
926 /*
927 * This function is called while unloading the kernel-module
928 */
usb_gigaset_exit(void)929 static void __exit usb_gigaset_exit(void)
930 {
931 int i;
932
933 gigaset_blockdriver(driver); /* => probe will fail
934 * => no gigaset_start any more
935 */
936
937 /* stop all connected devices */
938 for (i = 0; i < driver->minors; i++)
939 gigaset_shutdown(driver->cs + i);
940
941 /* from now on, no isdn callback should be possible */
942
943 /* deregister this driver with the USB subsystem */
944 usb_deregister(&gigaset_usb_driver);
945 /* this will call the disconnect-callback */
946 /* from now on, no disconnect/probe callback should be running */
947
948 gigaset_freedriver(driver);
949 driver = NULL;
950 }
951
952
953 module_init(usb_gigaset_init);
954 module_exit(usb_gigaset_exit);
955
956 MODULE_AUTHOR(DRIVER_AUTHOR);
957 MODULE_DESCRIPTION(DRIVER_DESC);
958
959 MODULE_LICENSE("GPL");
960