umodem.c revision 1.26 1 /* $NetBSD: umodem.c,v 1.26 2000/04/06 13:32:28 augustss Exp $ */
2
3 /*
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (augustss (at) carlstedt.se) at
9 * Carlstedt Research & Technology.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the NetBSD
22 * Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40 /*
41 * Comm Class spec: http://www.usb.org/developers/data/usbcdc11.pdf
42 */
43
44 /*
45 * TODO:
46 * - Add error recovery in various places; the big problem is what
47 * to do in a callback if there is an error.
48 * - Implement a Call Device for modems without multiplexed commands.
49 *
50 */
51
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/ioctl.h>
56 #include <sys/conf.h>
57 #include <sys/tty.h>
58 #include <sys/file.h>
59 #include <sys/select.h>
60 #include <sys/proc.h>
61 #include <sys/vnode.h>
62 #include <sys/device.h>
63 #include <sys/poll.h>
64
65 #include <dev/usb/usb.h>
66 #include <dev/usb/usbcdc.h>
67
68 #include <dev/usb/usbdi.h>
69 #include <dev/usb/usbdi_util.h>
70 #include <dev/usb/usbdevs.h>
71 #include <dev/usb/usb_quirks.h>
72
73 #include <dev/usb/usbdevs.h>
74 #include <dev/usb/ucomvar.h>
75
76 #ifdef UMODEM_DEBUG
77 #define DPRINTFN(n, x) if (umodemdebug > (n)) logprintf x
78 int umodemdebug = 0;
79 #else
80 #define DPRINTFN(n, x)
81 #endif
82 #define DPRINTF(x) DPRINTFN(0, x)
83
84 /*
85 * These are the maximum number of bytes transferred per frame.
86 * If some really high speed devices should use this driver they
87 * may need to be increased, but this is good enough for normal modems.
88 */
89 #define UMODEMIBUFSIZE 64
90 #define UMODEMOBUFSIZE 256
91
92 struct umodem_softc {
93 USBBASEDEVICE sc_dev; /* base device */
94
95 usbd_device_handle sc_udev; /* USB device */
96
97 int sc_ctl_iface_no;
98 usbd_interface_handle sc_ctl_iface; /* control interface */
99 int sc_data_iface_no;
100 usbd_interface_handle sc_data_iface; /* data interface */
101
102 int sc_cm_cap; /* CM capabilities */
103 int sc_acm_cap; /* ACM capabilities */
104
105 int sc_cm_over_data;
106
107 usb_cdc_line_state_t sc_line_state; /* current line state */
108 u_char sc_dtr; /* current DTR state */
109 u_char sc_rts; /* current RTS state */
110
111 device_ptr_t sc_subdev; /* ucom device */
112
113 u_char sc_opening; /* lock during open */
114 u_char sc_dying; /* disconnecting */
115 };
116
117 Static void *umodem_get_desc
118 __P((usbd_device_handle dev, int type, int subtype));
119 Static usbd_status umodem_set_comm_feature
120 __P((struct umodem_softc *sc, int feature, int state));
121 Static usbd_status umodem_set_line_coding
122 __P((struct umodem_softc *sc, usb_cdc_line_state_t *state));
123
124 Static void umodem_get_caps __P((usbd_device_handle, int *, int *));
125
126 Static void umodem_get_status
127 __P((void *, int portno, u_char *lsr, u_char *msr));
128 Static void umodem_set __P((void *, int, int, int));
129 Static void umodem_dtr __P((struct umodem_softc *, int));
130 Static void umodem_rts __P((struct umodem_softc *, int));
131 Static void umodem_break __P((struct umodem_softc *, int));
132 Static void umodem_set_line_state __P((struct umodem_softc *));
133 Static int umodem_param __P((void *, int, struct termios *));
134 Static int umodem_ioctl __P((void *, int, u_long, caddr_t, int,
135 struct proc *));
136
137 Static struct ucom_methods umodem_methods = {
138 umodem_get_status,
139 umodem_set,
140 umodem_param,
141 umodem_ioctl,
142 NULL,
143 NULL,
144 };
145
146 USB_DECLARE_DRIVER(umodem);
147
148 USB_MATCH(umodem)
149 {
150 USB_MATCH_START(umodem, uaa);
151 usb_interface_descriptor_t *id;
152 int cm, acm;
153
154 if (uaa->iface == NULL)
155 return (UMATCH_NONE);
156
157 id = usbd_get_interface_descriptor(uaa->iface);
158 if (id == NULL ||
159 id->bInterfaceClass != UICLASS_CDC ||
160 id->bInterfaceSubClass != UISUBCLASS_ABSTRACT_CONTROL_MODEL ||
161 id->bInterfaceProtocol != UIPROTO_CDC_AT)
162 return (UMATCH_NONE);
163
164 umodem_get_caps(uaa->device, &cm, &acm);
165 if (!(cm & USB_CDC_CM_DOES_CM) ||
166 !(cm & USB_CDC_CM_OVER_DATA) ||
167 !(acm & USB_CDC_ACM_HAS_LINE))
168 return (UMATCH_NONE);
169
170 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
171 }
172
173 USB_ATTACH(umodem)
174 {
175 USB_ATTACH_START(umodem, sc, uaa);
176 usbd_device_handle dev = uaa->device;
177 usb_interface_descriptor_t *id;
178 usb_endpoint_descriptor_t *ed;
179 usb_cdc_cm_descriptor_t *cmd;
180 char devinfo[1024];
181 usbd_status err;
182 int data_ifcno;
183 int i;
184 struct ucom_attach_args uca;
185
186 usbd_devinfo(uaa->device, 0, devinfo);
187 USB_ATTACH_SETUP;
188
189 sc->sc_udev = dev;
190 sc->sc_ctl_iface = uaa->iface;
191
192 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
193 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
194 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
195 sc->sc_ctl_iface_no = id->bInterfaceNumber;
196
197 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
198
199 /* Get the data interface no. */
200 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
201 if (cmd == NULL) {
202 printf("%s: no CM descriptor\n", USBDEVNAME(sc->sc_dev));
203 goto bad;
204 }
205 sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
206
207 printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
208 USBDEVNAME(sc->sc_dev), data_ifcno,
209 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
210 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
211
212
213 /* Get the data interface too. */
214 for (i = 0; i < uaa->nifaces; i++) {
215 if (uaa->ifaces[i] != NULL) {
216 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
217 if (id != NULL && id->bInterfaceNumber == data_ifcno) {
218 sc->sc_data_iface = uaa->ifaces[i];
219 uaa->ifaces[i] = NULL;
220 }
221 }
222 }
223 if (sc->sc_data_iface == NULL) {
224 printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
225 goto bad;
226 }
227
228 /*
229 * Find the bulk endpoints.
230 * Iterate over all endpoints in the data interface and take note.
231 */
232 uca.bulkin = uca.bulkout = -1;
233
234 id = usbd_get_interface_descriptor(sc->sc_data_iface);
235 for (i = 0; i < id->bNumEndpoints; i++) {
236 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
237 if (ed == NULL) {
238 printf("%s: no endpoint descriptor for %d\n",
239 USBDEVNAME(sc->sc_dev), i);
240 goto bad;
241 }
242 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
243 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
244 uca.bulkin = ed->bEndpointAddress;
245 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
246 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
247 uca.bulkout = ed->bEndpointAddress;
248 }
249 }
250
251 if (uca.bulkin == -1) {
252 printf("%s: Could not find data bulk in\n",
253 USBDEVNAME(sc->sc_dev));
254 goto bad;
255 }
256 if (uca.bulkout == -1) {
257 printf("%s: Could not find data bulk out\n",
258 USBDEVNAME(sc->sc_dev));
259 goto bad;
260 }
261
262 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
263 err = umodem_set_comm_feature(sc, UCDC_ABSTRACT_STATE,
264 UCDC_DATA_MULTIPLEXED);
265 if (err) {
266 printf("%s: could not set data multiplex mode\n",
267 USBDEVNAME(sc->sc_dev));
268 goto bad;
269 }
270 sc->sc_cm_over_data = 1;
271 }
272
273 sc->sc_dtr = -1;
274
275 uca.portno = UCOM_UNK_PORTNO;
276 /* bulkin, bulkout set above */
277 uca.ibufsize = UMODEMIBUFSIZE;
278 uca.obufsize = UMODEMOBUFSIZE;
279 uca.device = sc->sc_udev;
280 uca.iface = sc->sc_data_iface;
281 uca.methods = &umodem_methods;
282 uca.arg = sc;
283
284 DPRINTF(("umodem_attach: sc=%p\n", sc));
285 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
286
287 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
288 USBDEV(sc->sc_dev));
289
290 USB_ATTACH_SUCCESS_RETURN;
291
292 bad:
293 sc->sc_dying = 1;
294 USB_ATTACH_ERROR_RETURN;
295 }
296
297 void
298 umodem_get_caps(dev, cm, acm)
299 usbd_device_handle dev;
300 int *cm, *acm;
301 {
302 usb_cdc_cm_descriptor_t *cmd;
303 usb_cdc_acm_descriptor_t *cad;
304
305 *cm = *acm = 0;
306
307 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
308 if (cmd == NULL) {
309 DPRINTF(("umodem_get_desc: no CM desc\n"));
310 return;
311 }
312 *cm = cmd->bmCapabilities;
313
314 cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
315 if (cad == NULL) {
316 DPRINTF(("umodem_get_desc: no ACM desc\n"));
317 return;
318 }
319 *acm = cad->bmCapabilities;
320 }
321
322 void
323 umodem_get_status(addr, portno, lsr, msr)
324 void *addr;
325 int portno;
326 u_char *lsr, *msr;
327 {
328 DPRINTF(("umodem_get_status:\n"));
329
330 if (lsr != NULL)
331 *lsr = 0; /* XXX */
332 if (msr != NULL)
333 *msr = 0; /* XXX */
334 }
335
336 int
337 umodem_param(addr, portno, t)
338 void *addr;
339 int portno;
340 struct termios *t;
341 {
342 struct umodem_softc *sc = addr;
343 usbd_status err;
344 usb_cdc_line_state_t ls;
345
346 DPRINTF(("umodem_param: sc=%p\n", sc));
347
348 USETDW(ls.dwDTERate, t->c_ospeed);
349 if (ISSET(t->c_cflag, CSTOPB))
350 ls.bCharFormat = UCDC_STOP_BIT_2;
351 else
352 ls.bCharFormat = UCDC_STOP_BIT_1;
353 if (ISSET(t->c_cflag, PARENB)) {
354 if (ISSET(t->c_cflag, PARODD))
355 ls.bParityType = UCDC_PARITY_ODD;
356 else
357 ls.bParityType = UCDC_PARITY_EVEN;
358 } else
359 ls.bParityType = UCDC_PARITY_NONE;
360 switch (ISSET(t->c_cflag, CSIZE)) {
361 case CS5:
362 ls.bDataBits = 5;
363 break;
364 case CS6:
365 ls.bDataBits = 6;
366 break;
367 case CS7:
368 ls.bDataBits = 7;
369 break;
370 case CS8:
371 ls.bDataBits = 8;
372 break;
373 }
374
375 err = umodem_set_line_coding(sc, &ls);
376 if (err) {
377 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
378 return (1);
379 }
380 return (0);
381 }
382
383 int
384 umodem_ioctl(addr, portno, cmd, data, flag, p)
385 void *addr;
386 int portno;
387 u_long cmd;
388 caddr_t data;
389 int flag;
390 struct proc *p;
391 {
392 struct umodem_softc *sc = addr;
393 int error = 0;
394
395 if (sc->sc_dying)
396 return (EIO);
397
398 DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
399
400 switch (cmd) {
401 case USB_GET_CM_OVER_DATA:
402 *(int *)data = sc->sc_cm_over_data;
403 break;
404
405 case USB_SET_CM_OVER_DATA:
406 if (*(int *)data != sc->sc_cm_over_data) {
407 /* XXX change it */
408 }
409 break;
410
411 default:
412 DPRINTF(("umodemioctl: unknown\n"));
413 error = ENOTTY;
414 break;
415 }
416
417 return (error);
418 }
419
420 void
421 umodem_dtr(sc, onoff)
422 struct umodem_softc *sc;
423 int onoff;
424 {
425 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
426
427 if (sc->sc_dtr == onoff)
428 return;
429 sc->sc_dtr = onoff;
430
431 umodem_set_line_state(sc);
432 }
433
434 void
435 umodem_rts(sc, onoff)
436 struct umodem_softc *sc;
437 int onoff;
438 {
439 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
440
441 if (sc->sc_rts == onoff)
442 return;
443 sc->sc_rts = onoff;
444
445 umodem_set_line_state(sc);
446 }
447
448 void
449 umodem_set_line_state(sc)
450 struct umodem_softc *sc;
451 {
452 usb_device_request_t req;
453 int ls;
454
455 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
456 (sc->sc_rts ? UCDC_LINE_RTS : 0);
457 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
458 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
459 USETW(req.wValue, ls);
460 USETW(req.wIndex, sc->sc_ctl_iface_no);
461 USETW(req.wLength, 0);
462
463 (void)usbd_do_request(sc->sc_udev, &req, 0);
464
465 }
466
467 void
468 umodem_break(sc, onoff)
469 struct umodem_softc *sc;
470 int onoff;
471 {
472 usb_device_request_t req;
473
474 DPRINTF(("umodem_break: onoff=%d\n", onoff));
475
476 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
477 return;
478
479 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
480 req.bRequest = UCDC_SEND_BREAK;
481 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
482 USETW(req.wIndex, sc->sc_ctl_iface_no);
483 USETW(req.wLength, 0);
484
485 (void)usbd_do_request(sc->sc_udev, &req, 0);
486 }
487
488 void
489 umodem_set(addr, portno, reg, onoff)
490 void *addr;
491 int portno;
492 int onoff;
493 {
494 struct umodem_softc *sc = addr;
495
496 switch (reg) {
497 case UCOM_SET_DTR:
498 umodem_dtr(sc, onoff);
499 break;
500 case UCOM_SET_RTS:
501 umodem_rts(sc, onoff);
502 break;
503 case UCOM_SET_BREAK:
504 umodem_break(sc, onoff);
505 break;
506 default:
507 break;
508 }
509 }
510
511 usbd_status
512 umodem_set_line_coding(sc, state)
513 struct umodem_softc *sc;
514 usb_cdc_line_state_t *state;
515 {
516 usb_device_request_t req;
517 usbd_status err;
518
519 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
520 UGETDW(state->dwDTERate), state->bCharFormat,
521 state->bParityType, state->bDataBits));
522
523 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
524 DPRINTF(("umodem_set_line_coding: already set\n"));
525 return (USBD_NORMAL_COMPLETION);
526 }
527
528 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
529 req.bRequest = UCDC_SET_LINE_CODING;
530 USETW(req.wValue, 0);
531 USETW(req.wIndex, sc->sc_ctl_iface_no);
532 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
533
534 err = usbd_do_request(sc->sc_udev, &req, state);
535 if (err) {
536 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
537 usbd_errstr(err)));
538 return (err);
539 }
540
541 sc->sc_line_state = *state;
542
543 return (USBD_NORMAL_COMPLETION);
544 }
545
546 void *
547 umodem_get_desc(dev, type, subtype)
548 usbd_device_handle dev;
549 int type;
550 int subtype;
551 {
552 usb_descriptor_t *desc;
553 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
554 uByte *p = (uByte *)cd;
555 uByte *end = p + UGETW(cd->wTotalLength);
556
557 while (p < end) {
558 desc = (usb_descriptor_t *)p;
559 if (desc->bDescriptorType == type &&
560 desc->bDescriptorSubtype == subtype)
561 return (desc);
562 p += desc->bLength;
563 }
564
565 return (0);
566 }
567
568 usbd_status
569 umodem_set_comm_feature(sc, feature, state)
570 struct umodem_softc *sc;
571 int feature;
572 int state;
573 {
574 usb_device_request_t req;
575 usbd_status err;
576 usb_cdc_abstract_state_t ast;
577
578 DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
579 state));
580
581 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
582 req.bRequest = UCDC_SET_COMM_FEATURE;
583 USETW(req.wValue, feature);
584 USETW(req.wIndex, sc->sc_ctl_iface_no);
585 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
586 USETW(ast.wState, state);
587
588 err = usbd_do_request(sc->sc_udev, &req, &ast);
589 if (err) {
590 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
591 feature, usbd_errstr(err)));
592 return (err);
593 }
594
595 return (USBD_NORMAL_COMPLETION);
596 }
597
598 int
599 umodem_activate(self, act)
600 device_ptr_t self;
601 enum devact act;
602 {
603 struct umodem_softc *sc = (struct umodem_softc *)self;
604 int rv = 0;
605
606 switch (act) {
607 case DVACT_ACTIVATE:
608 return (EOPNOTSUPP);
609 break;
610
611 case DVACT_DEACTIVATE:
612 sc->sc_dying = 1;
613 if (sc->sc_subdev)
614 rv = config_deactivate(sc->sc_subdev);
615 break;
616 }
617 return (rv);
618 }
619
620 USB_DETACH(umodem)
621 {
622 USB_DETACH_START(umodem, sc);
623 int rv = 0;
624
625 DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
626
627 sc->sc_dying = 1;
628
629 if (sc->sc_subdev != NULL)
630 rv = config_detach(sc->sc_subdev, flags);
631
632 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
633 USBDEV(sc->sc_dev));
634
635 return (rv);
636 }
637