umodem.c revision 1.21 1 /* $NetBSD: umodem.c,v 1.21 2000/02/02 13:18:47 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 struct umodem_softc {
85 USBBASEDEVICE sc_dev; /* base device */
86
87 usbd_device_handle sc_udev; /* USB device */
88
89 int sc_ctl_iface_no;
90 usbd_interface_handle sc_ctl_iface; /* control interface */
91 int sc_data_iface_no;
92 usbd_interface_handle sc_data_iface; /* data interface */
93
94 int sc_cm_cap; /* CM capabilities */
95 int sc_acm_cap; /* ACM capabilities */
96
97 int sc_cm_over_data;
98
99 usb_cdc_line_state_t sc_line_state; /* current line state */
100 u_char sc_dtr; /* current DTR state */
101 u_char sc_rts; /* current RTS state */
102
103 device_ptr_t sc_subdev; /* ucom device */
104
105 u_char sc_opening; /* lock during open */
106 u_char sc_dying; /* disconnecting */
107 };
108
109 static void *umodem_get_desc
110 __P((usbd_device_handle dev, int type, int subtype));
111 static usbd_status umodem_set_comm_feature
112 __P((struct umodem_softc *sc, int feature, int state));
113 static usbd_status umodem_set_line_coding
114 __P((struct umodem_softc *sc, usb_cdc_line_state_t *state));
115
116 static void umodem_get_caps __P((usbd_device_handle, int *, int *));
117
118 static void umodem_get_status
119 __P((void *, int portno, u_char *lsr, u_char *msr));
120 static void umodem_set __P((void *, int, int, int));
121 static void umodem_dtr __P((struct umodem_softc *, int));
122 static void umodem_rts __P((struct umodem_softc *, int));
123 static void umodem_break __P((struct umodem_softc *, int));
124 static void umodem_set_line_state __P((struct umodem_softc *));
125 static int umodem_param __P((void *, int, struct termios *));
126 static int umodem_ioctl __P((void *, int, u_long, caddr_t, int,
127 struct proc *));
128
129 static struct ucom_methods umodem_methods = {
130 umodem_get_status,
131 umodem_set,
132 umodem_param,
133 umodem_ioctl,
134 };
135
136 USB_DECLARE_DRIVER(umodem);
137
138 USB_MATCH(umodem)
139 {
140 USB_MATCH_START(umodem, uaa);
141 usb_interface_descriptor_t *id;
142 int cm, acm;
143
144 if (uaa->iface == NULL)
145 return (UMATCH_NONE);
146
147 id = usbd_get_interface_descriptor(uaa->iface);
148 if (id == NULL ||
149 id->bInterfaceClass != UCLASS_CDC ||
150 id->bInterfaceSubClass != USUBCLASS_ABSTRACT_CONTROL_MODEL ||
151 id->bInterfaceProtocol != UPROTO_CDC_AT)
152 return (UMATCH_NONE);
153
154 umodem_get_caps(uaa->device, &cm, &acm);
155 if (!(cm & USB_CDC_CM_DOES_CM) ||
156 !(cm & USB_CDC_CM_OVER_DATA) ||
157 !(acm & USB_CDC_ACM_HAS_LINE))
158 return (UMATCH_NONE);
159
160 return (UMATCH_IFACECLASS_IFACESUBCLASS_IFACEPROTO);
161 }
162
163 USB_ATTACH(umodem)
164 {
165 USB_ATTACH_START(umodem, sc, uaa);
166 usbd_device_handle dev = uaa->device;
167 usb_interface_descriptor_t *id;
168 usb_endpoint_descriptor_t *ed;
169 usb_cdc_cm_descriptor_t *cmd;
170 char devinfo[1024];
171 usbd_status err;
172 int data_ifcno;
173 int i;
174 struct ucom_attach_args uca;
175
176 usbd_devinfo(uaa->device, 0, devinfo);
177 USB_ATTACH_SETUP;
178
179 sc->sc_udev = dev;
180 sc->sc_ctl_iface = uaa->iface;
181
182 id = usbd_get_interface_descriptor(sc->sc_ctl_iface);
183 printf("%s: %s, iclass %d/%d\n", USBDEVNAME(sc->sc_dev),
184 devinfo, id->bInterfaceClass, id->bInterfaceSubClass);
185 sc->sc_ctl_iface_no = id->bInterfaceNumber;
186
187 umodem_get_caps(dev, &sc->sc_cm_cap, &sc->sc_acm_cap);
188
189 /* Get the data interface no. */
190 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
191 if (cmd == NULL) {
192 printf("%s: no CM descriptor\n", USBDEVNAME(sc->sc_dev));
193 goto bad;
194 }
195 sc->sc_data_iface_no = data_ifcno = cmd->bDataInterface;
196
197 printf("%s: data interface %d, has %sCM over data, has %sbreak\n",
198 USBDEVNAME(sc->sc_dev), data_ifcno,
199 sc->sc_cm_cap & USB_CDC_CM_OVER_DATA ? "" : "no ",
200 sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK ? "" : "no ");
201
202
203 /* Get the data interface too. */
204 for (i = 0; i < uaa->nifaces; i++) {
205 if (uaa->ifaces[i] != NULL) {
206 id = usbd_get_interface_descriptor(uaa->ifaces[i]);
207 if (id != NULL && id->bInterfaceNumber == data_ifcno) {
208 sc->sc_data_iface = uaa->ifaces[i];
209 uaa->ifaces[i] = NULL;
210 }
211 }
212 }
213 if (sc->sc_data_iface == NULL) {
214 printf("%s: no data interface\n", USBDEVNAME(sc->sc_dev));
215 goto bad;
216 }
217
218 /*
219 * Find the bulk endpoints.
220 * Iterate over all endpoints in the data interface and take note.
221 */
222 uca.bulkin = uca.bulkout = -1;
223
224 id = usbd_get_interface_descriptor(sc->sc_data_iface);
225 for (i = 0; i < id->bNumEndpoints; i++) {
226 ed = usbd_interface2endpoint_descriptor(sc->sc_data_iface, i);
227 if (ed == NULL) {
228 printf("%s: no endpoint descriptor for %d\n",
229 USBDEVNAME(sc->sc_dev), i);
230 goto bad;
231 }
232 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
233 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
234 uca.bulkin = ed->bEndpointAddress;
235 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
236 (ed->bmAttributes & UE_XFERTYPE) == UE_BULK) {
237 uca.bulkout = ed->bEndpointAddress;
238 }
239 }
240
241 if (uca.bulkin == -1) {
242 printf("%s: Could not find data bulk in\n",
243 USBDEVNAME(sc->sc_dev));
244 goto bad;
245 }
246 if (uca.bulkout == -1) {
247 printf("%s: Could not find data bulk out\n",
248 USBDEVNAME(sc->sc_dev));
249 goto bad;
250 }
251
252 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
253 err = umodem_set_comm_feature(sc, UCDC_ABSTRACT_STATE,
254 UCDC_DATA_MULTIPLEXED);
255 if (err) {
256 printf("%s: could not set data multiplex mode\n",
257 USBDEVNAME(sc->sc_dev));
258 goto bad;
259 }
260 sc->sc_cm_over_data = 1;
261 }
262
263 sc->sc_dtr = -1;
264
265 uca.portno = UCOM_UNK_PORTNO;
266 /* bulkin, bulkout set above */
267 uca.device = sc->sc_udev;
268 uca.iface = sc->sc_data_iface;
269 uca.methods = &umodem_methods;
270 uca.arg = sc;
271
272 DPRINTF(("umodem_attach: sc=%p\n", sc));
273 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
274
275 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
276 USBDEV(sc->sc_dev));
277
278 USB_ATTACH_SUCCESS_RETURN;
279
280 bad:
281 sc->sc_dying = 1;
282 USB_ATTACH_ERROR_RETURN;
283 }
284
285 void
286 umodem_get_caps(dev, cm, acm)
287 usbd_device_handle dev;
288 int *cm, *acm;
289 {
290 usb_cdc_cm_descriptor_t *cmd;
291 usb_cdc_acm_descriptor_t *cad;
292
293 *cm = *acm = 0;
294
295 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
296 if (cmd == NULL) {
297 DPRINTF(("umodem_get_desc: no CM desc\n"));
298 return;
299 }
300 *cm = cmd->bmCapabilities;
301
302 cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
303 if (cad == NULL) {
304 DPRINTF(("umodem_get_desc: no ACM desc\n"));
305 return;
306 }
307 *acm = cad->bmCapabilities;
308 }
309
310 void
311 umodem_get_status(addr, portno, lsr, msr)
312 void *addr;
313 int portno;
314 u_char *lsr, *msr;
315 {
316 DPRINTF(("umodem_get_status:\n"));
317
318 if (lsr != NULL)
319 *lsr = 0; /* XXX */
320 if (msr != NULL)
321 *msr = 0; /* XXX */
322 }
323
324 int
325 umodem_param(addr, portno, t)
326 void *addr;
327 int portno;
328 struct termios *t;
329 {
330 struct umodem_softc *sc = addr;
331 usbd_status err;
332 usb_cdc_line_state_t ls;
333
334 DPRINTF(("umodem_param: sc=%p\n", sc));
335
336 USETDW(ls.dwDTERate, t->c_ospeed);
337 if (ISSET(t->c_cflag, CSTOPB))
338 ls.bCharFormat = UCDC_STOP_BIT_2;
339 else
340 ls.bCharFormat = UCDC_STOP_BIT_1;
341 if (ISSET(t->c_cflag, PARENB)) {
342 if (ISSET(t->c_cflag, PARODD))
343 ls.bParityType = UCDC_PARITY_ODD;
344 else
345 ls.bParityType = UCDC_PARITY_EVEN;
346 } else
347 ls.bParityType = UCDC_PARITY_NONE;
348 switch (ISSET(t->c_cflag, CSIZE)) {
349 case CS5:
350 ls.bDataBits = 5;
351 break;
352 case CS6:
353 ls.bDataBits = 6;
354 break;
355 case CS7:
356 ls.bDataBits = 7;
357 break;
358 case CS8:
359 ls.bDataBits = 8;
360 break;
361 }
362
363 err = umodem_set_line_coding(sc, &ls);
364 if (err) {
365 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
366 return (1);
367 }
368 return (0);
369 }
370
371 int
372 umodem_ioctl(addr, portno, cmd, data, flag, p)
373 void *addr;
374 int portno;
375 u_long cmd;
376 caddr_t data;
377 int flag;
378 struct proc *p;
379 {
380 struct umodem_softc *sc = addr;
381 int error;
382
383 if (sc->sc_dying)
384 return (EIO);
385
386 DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
387
388 switch (cmd) {
389 case USB_GET_CM_OVER_DATA:
390 *(int *)data = sc->sc_cm_over_data;
391 break;
392
393 case USB_SET_CM_OVER_DATA:
394 if (*(int *)data != sc->sc_cm_over_data) {
395 /* XXX change it */
396 }
397 break;
398
399 default:
400 DPRINTF(("umodemioctl: unknown\n"));
401 error = ENOTTY;
402 break;
403 }
404
405 return (error);
406 }
407
408 void
409 umodem_dtr(sc, onoff)
410 struct umodem_softc *sc;
411 int onoff;
412 {
413 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
414
415 if (sc->sc_dtr == onoff)
416 return;
417 sc->sc_dtr = onoff;
418
419 umodem_set_line_state(sc);
420 }
421
422 void
423 umodem_rts(sc, onoff)
424 struct umodem_softc *sc;
425 int onoff;
426 {
427 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
428
429 if (sc->sc_rts == onoff)
430 return;
431 sc->sc_rts = onoff;
432
433 umodem_set_line_state(sc);
434 }
435
436 void
437 umodem_set_line_state(sc)
438 struct umodem_softc *sc;
439 {
440 usb_device_request_t req;
441 int ls;
442
443 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
444 (sc->sc_rts ? UCDC_LINE_RTS : 0);
445 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
446 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
447 USETW(req.wValue, ls);
448 USETW(req.wIndex, sc->sc_ctl_iface_no);
449 USETW(req.wLength, 0);
450
451 (void)usbd_do_request(sc->sc_udev, &req, 0);
452
453 }
454
455 void
456 umodem_break(sc, onoff)
457 struct umodem_softc *sc;
458 int onoff;
459 {
460 usb_device_request_t req;
461
462 DPRINTF(("umodem_break: onoff=%d\n", onoff));
463
464 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
465 return;
466
467 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
468 req.bRequest = UCDC_SEND_BREAK;
469 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
470 USETW(req.wIndex, sc->sc_ctl_iface_no);
471 USETW(req.wLength, 0);
472
473 (void)usbd_do_request(sc->sc_udev, &req, 0);
474 }
475
476 void
477 umodem_set(addr, portno, reg, onoff)
478 void *addr;
479 int portno;
480 int onoff;
481 {
482 struct umodem_softc *sc = addr;
483
484 switch (reg) {
485 case UCOM_SET_DTR:
486 umodem_dtr(sc, onoff);
487 break;
488 case UCOM_SET_RTS:
489 umodem_rts(sc, onoff);
490 break;
491 case UCOM_SET_BREAK:
492 umodem_break(sc, onoff);
493 break;
494 default:
495 break;
496 }
497 }
498
499 usbd_status
500 umodem_set_line_coding(sc, state)
501 struct umodem_softc *sc;
502 usb_cdc_line_state_t *state;
503 {
504 usb_device_request_t req;
505 usbd_status err;
506
507 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
508 UGETDW(state->dwDTERate), state->bCharFormat,
509 state->bParityType, state->bDataBits));
510
511 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
512 DPRINTF(("umodem_set_line_coding: already set\n"));
513 return (USBD_NORMAL_COMPLETION);
514 }
515
516 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
517 req.bRequest = UCDC_SET_LINE_CODING;
518 USETW(req.wValue, 0);
519 USETW(req.wIndex, sc->sc_ctl_iface_no);
520 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
521
522 err = usbd_do_request(sc->sc_udev, &req, state);
523 if (err) {
524 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
525 usbd_errstr(err)));
526 return (err);
527 }
528
529 sc->sc_line_state = *state;
530
531 return (USBD_NORMAL_COMPLETION);
532 }
533
534 void *
535 umodem_get_desc(dev, type, subtype)
536 usbd_device_handle dev;
537 int type;
538 int subtype;
539 {
540 usb_descriptor_t *desc;
541 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
542 uByte *p = (uByte *)cd;
543 uByte *end = p + UGETW(cd->wTotalLength);
544
545 while (p < end) {
546 desc = (usb_descriptor_t *)p;
547 if (desc->bDescriptorType == type &&
548 desc->bDescriptorSubtype == subtype)
549 return (desc);
550 p += desc->bLength;
551 }
552
553 return (0);
554 }
555
556 usbd_status
557 umodem_set_comm_feature(sc, feature, state)
558 struct umodem_softc *sc;
559 int feature;
560 int state;
561 {
562 usb_device_request_t req;
563 usbd_status err;
564 usb_cdc_abstract_state_t ast;
565
566 DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
567 state));
568
569 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
570 req.bRequest = UCDC_SET_COMM_FEATURE;
571 USETW(req.wValue, feature);
572 USETW(req.wIndex, sc->sc_ctl_iface_no);
573 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
574 USETW(ast.wState, state);
575
576 err = usbd_do_request(sc->sc_udev, &req, &ast);
577 if (err) {
578 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
579 feature, usbd_errstr(err)));
580 return (err);
581 }
582
583 return (USBD_NORMAL_COMPLETION);
584 }
585
586 int
587 umodem_activate(self, act)
588 device_ptr_t self;
589 enum devact act;
590 {
591 struct umodem_softc *sc = (struct umodem_softc *)self;
592 int rv = 0;
593
594 switch (act) {
595 case DVACT_ACTIVATE:
596 return (EOPNOTSUPP);
597 break;
598
599 case DVACT_DEACTIVATE:
600 sc->sc_dying = 1;
601 if (sc->sc_subdev)
602 rv = config_deactivate(sc->sc_subdev);
603 break;
604 }
605 return (rv);
606 }
607
608 USB_DETACH(umodem)
609 {
610 USB_DETACH_START(umodem, sc);
611 int rv = 0;
612
613 DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
614
615 sc->sc_dying = 1;
616
617 if (sc->sc_subdev != NULL)
618 rv = config_detach(sc->sc_subdev, flags);
619
620 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
621 USBDEV(sc->sc_dev));
622
623 return (rv);
624 }
625