umodem.c revision 1.38 1 /* $NetBSD: umodem.c,v 1.38 2001/01/23 21:56:17 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 (lennart (at) augustsson.net) 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/devclass/usbcdc10.pdf
42 * http://www.usb.org/developers/data/devclass/usbcdc11.pdf
43 */
44
45 /*
46 * TODO:
47 * - Add error recovery in various places; the big problem is what
48 * to do in a callback if there is an error.
49 * - Implement a Call Device for modems without multiplexed commands.
50 *
51 */
52
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/ioctl.h>
57 #include <sys/conf.h>
58 #include <sys/tty.h>
59 #include <sys/file.h>
60 #include <sys/select.h>
61 #include <sys/proc.h>
62 #include <sys/vnode.h>
63 #include <sys/device.h>
64 #include <sys/poll.h>
65
66 #include <dev/usb/usb.h>
67 #include <dev/usb/usbcdc.h>
68
69 #include <dev/usb/usbdi.h>
70 #include <dev/usb/usbdi_util.h>
71 #include <dev/usb/usbdevs.h>
72 #include <dev/usb/usb_quirks.h>
73
74 #include <dev/usb/usbdevs.h>
75 #include <dev/usb/ucomvar.h>
76
77 #ifdef UMODEM_DEBUG
78 #define DPRINTFN(n, x) if (umodemdebug > (n)) logprintf x
79 int umodemdebug = 0;
80 #else
81 #define DPRINTFN(n, x)
82 #endif
83 #define DPRINTF(x) DPRINTFN(0, x)
84
85 /*
86 * These are the maximum number of bytes transferred per frame.
87 * If some really high speed devices should use this driver they
88 * may need to be increased, but this is good enough for normal modems.
89 */
90 #define UMODEMIBUFSIZE 64
91 #define UMODEMOBUFSIZE 256
92
93 struct umodem_softc {
94 USBBASEDEVICE sc_dev; /* base device */
95
96 usbd_device_handle sc_udev; /* USB device */
97
98 int sc_ctl_iface_no;
99 usbd_interface_handle sc_ctl_iface; /* control interface */
100 int sc_data_iface_no;
101 usbd_interface_handle sc_data_iface; /* data interface */
102
103 int sc_cm_cap; /* CM capabilities */
104 int sc_acm_cap; /* ACM capabilities */
105
106 int sc_cm_over_data;
107
108 usb_cdc_line_state_t sc_line_state; /* current line state */
109 u_char sc_dtr; /* current DTR state */
110 u_char sc_rts; /* current RTS state */
111
112 device_ptr_t sc_subdev; /* ucom device */
113
114 u_char sc_opening; /* lock during open */
115 u_char sc_dying; /* disconnecting */
116 };
117
118 Static void *umodem_get_desc(usbd_device_handle dev, int type, int subtype);
119 Static usbd_status umodem_set_comm_feature(struct umodem_softc *sc,
120 int feature, int state);
121 Static usbd_status umodem_set_line_coding(struct umodem_softc *sc,
122 usb_cdc_line_state_t *state);
123
124 Static void umodem_get_caps(usbd_device_handle, int *, int *);
125
126 Static void umodem_get_status(void *, int portno, u_char *lsr, u_char *msr);
127 Static void umodem_set(void *, int, int, int);
128 Static void umodem_dtr(struct umodem_softc *, int);
129 Static void umodem_rts(struct umodem_softc *, int);
130 Static void umodem_break(struct umodem_softc *, int);
131 Static void umodem_set_line_state(struct umodem_softc *);
132 Static int umodem_param(void *, int, struct termios *);
133 Static int umodem_ioctl(void *, int, u_long, caddr_t, int, struct proc *);
134
135 Static struct ucom_methods umodem_methods = {
136 umodem_get_status,
137 umodem_set,
138 umodem_param,
139 umodem_ioctl,
140 NULL,
141 NULL,
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 (usbd_get_quirks(sc->sc_udev)->uq_flags & UQ_ASSUME_CM_OVER_DATA) {
263 sc->sc_cm_over_data = 1;
264 } else {
265 if (sc->sc_cm_cap & USB_CDC_CM_OVER_DATA) {
266 if (sc->sc_acm_cap & USB_CDC_ACM_HAS_FEATURE)
267 err = umodem_set_comm_feature(sc,
268 UCDC_ABSTRACT_STATE, UCDC_DATA_MULTIPLEXED);
269 else
270 err = 0;
271 if (err) {
272 printf("%s: could not set data multiplex mode\n",
273 USBDEVNAME(sc->sc_dev));
274 goto bad;
275 }
276 sc->sc_cm_over_data = 1;
277 }
278 }
279 sc->sc_dtr = -1;
280
281 uca.portno = UCOM_UNK_PORTNO;
282 /* bulkin, bulkout set above */
283 uca.ibufsize = UMODEMIBUFSIZE;
284 uca.obufsize = UMODEMOBUFSIZE;
285 uca.ibufsizepad = UMODEMIBUFSIZE;
286 uca.opkthdrlen = 0;
287 uca.device = sc->sc_udev;
288 uca.iface = sc->sc_data_iface;
289 uca.methods = &umodem_methods;
290 uca.arg = sc;
291 uca.info = NULL;
292
293 usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, sc->sc_udev,
294 USBDEV(sc->sc_dev));
295
296 DPRINTF(("umodem_attach: sc=%p\n", sc));
297 sc->sc_subdev = config_found_sm(self, &uca, ucomprint, ucomsubmatch);
298
299 USB_ATTACH_SUCCESS_RETURN;
300
301 bad:
302 sc->sc_dying = 1;
303 USB_ATTACH_ERROR_RETURN;
304 }
305
306 void
307 umodem_get_caps(usbd_device_handle dev, int *cm, int *acm)
308 {
309 usb_cdc_cm_descriptor_t *cmd;
310 usb_cdc_acm_descriptor_t *cad;
311
312 *cm = *acm = 0;
313
314 cmd = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_CM);
315 if (cmd == NULL) {
316 DPRINTF(("umodem_get_desc: no CM desc\n"));
317 return;
318 }
319 *cm = cmd->bmCapabilities;
320
321 cad = umodem_get_desc(dev, UDESC_CS_INTERFACE, UDESCSUB_CDC_ACM);
322 if (cad == NULL) {
323 DPRINTF(("umodem_get_desc: no ACM desc\n"));
324 return;
325 }
326 *acm = cad->bmCapabilities;
327 }
328
329 void
330 umodem_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
331 {
332 DPRINTF(("umodem_get_status:\n"));
333
334 if (lsr != NULL)
335 *lsr = 0; /* XXX */
336 if (msr != NULL)
337 *msr = 0; /* XXX */
338 }
339
340 int
341 umodem_param(void *addr, int portno, struct termios *t)
342 {
343 struct umodem_softc *sc = addr;
344 usbd_status err;
345 usb_cdc_line_state_t ls;
346
347 DPRINTF(("umodem_param: sc=%p\n", sc));
348
349 USETDW(ls.dwDTERate, t->c_ospeed);
350 if (ISSET(t->c_cflag, CSTOPB))
351 ls.bCharFormat = UCDC_STOP_BIT_2;
352 else
353 ls.bCharFormat = UCDC_STOP_BIT_1;
354 if (ISSET(t->c_cflag, PARENB)) {
355 if (ISSET(t->c_cflag, PARODD))
356 ls.bParityType = UCDC_PARITY_ODD;
357 else
358 ls.bParityType = UCDC_PARITY_EVEN;
359 } else
360 ls.bParityType = UCDC_PARITY_NONE;
361 switch (ISSET(t->c_cflag, CSIZE)) {
362 case CS5:
363 ls.bDataBits = 5;
364 break;
365 case CS6:
366 ls.bDataBits = 6;
367 break;
368 case CS7:
369 ls.bDataBits = 7;
370 break;
371 case CS8:
372 ls.bDataBits = 8;
373 break;
374 }
375
376 err = umodem_set_line_coding(sc, &ls);
377 if (err) {
378 DPRINTF(("umodem_param: err=%s\n", usbd_errstr(err)));
379 return (1);
380 }
381 return (0);
382 }
383
384 int
385 umodem_ioctl(void *addr, int portno, u_long cmd, caddr_t data, int flag,
386 struct proc *p)
387 {
388 struct umodem_softc *sc = addr;
389 int error = 0;
390
391 if (sc->sc_dying)
392 return (EIO);
393
394 DPRINTF(("umodemioctl: cmd=0x%08lx\n", cmd));
395
396 switch (cmd) {
397 case USB_GET_CM_OVER_DATA:
398 *(int *)data = sc->sc_cm_over_data;
399 break;
400
401 case USB_SET_CM_OVER_DATA:
402 if (*(int *)data != sc->sc_cm_over_data) {
403 /* XXX change it */
404 }
405 break;
406
407 default:
408 DPRINTF(("umodemioctl: unknown\n"));
409 error = ENOTTY;
410 break;
411 }
412
413 return (error);
414 }
415
416 void
417 umodem_dtr(struct umodem_softc *sc, int onoff)
418 {
419 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
420
421 if (sc->sc_dtr == onoff)
422 return;
423 sc->sc_dtr = onoff;
424
425 umodem_set_line_state(sc);
426 }
427
428 void
429 umodem_rts(struct umodem_softc *sc, int onoff)
430 {
431 DPRINTF(("umodem_modem: onoff=%d\n", onoff));
432
433 if (sc->sc_rts == onoff)
434 return;
435 sc->sc_rts = onoff;
436
437 umodem_set_line_state(sc);
438 }
439
440 void
441 umodem_set_line_state(struct umodem_softc *sc)
442 {
443 usb_device_request_t req;
444 int ls;
445
446 ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
447 (sc->sc_rts ? UCDC_LINE_RTS : 0);
448 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
449 req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
450 USETW(req.wValue, ls);
451 USETW(req.wIndex, sc->sc_ctl_iface_no);
452 USETW(req.wLength, 0);
453
454 (void)usbd_do_request(sc->sc_udev, &req, 0);
455
456 }
457
458 void
459 umodem_break(struct umodem_softc *sc, int onoff)
460 {
461 usb_device_request_t req;
462
463 DPRINTF(("umodem_break: onoff=%d\n", onoff));
464
465 if (!(sc->sc_acm_cap & USB_CDC_ACM_HAS_BREAK))
466 return;
467
468 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
469 req.bRequest = UCDC_SEND_BREAK;
470 USETW(req.wValue, onoff ? UCDC_BREAK_ON : UCDC_BREAK_OFF);
471 USETW(req.wIndex, sc->sc_ctl_iface_no);
472 USETW(req.wLength, 0);
473
474 (void)usbd_do_request(sc->sc_udev, &req, 0);
475 }
476
477 void
478 umodem_set(void *addr, int portno, int reg, int onoff)
479 {
480 struct umodem_softc *sc = addr;
481
482 switch (reg) {
483 case UCOM_SET_DTR:
484 umodem_dtr(sc, onoff);
485 break;
486 case UCOM_SET_RTS:
487 umodem_rts(sc, onoff);
488 break;
489 case UCOM_SET_BREAK:
490 umodem_break(sc, onoff);
491 break;
492 default:
493 break;
494 }
495 }
496
497 usbd_status
498 umodem_set_line_coding(struct umodem_softc *sc, usb_cdc_line_state_t *state)
499 {
500 usb_device_request_t req;
501 usbd_status err;
502
503 DPRINTF(("umodem_set_line_coding: rate=%d fmt=%d parity=%d bits=%d\n",
504 UGETDW(state->dwDTERate), state->bCharFormat,
505 state->bParityType, state->bDataBits));
506
507 if (memcmp(state, &sc->sc_line_state, UCDC_LINE_STATE_LENGTH) == 0) {
508 DPRINTF(("umodem_set_line_coding: already set\n"));
509 return (USBD_NORMAL_COMPLETION);
510 }
511
512 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
513 req.bRequest = UCDC_SET_LINE_CODING;
514 USETW(req.wValue, 0);
515 USETW(req.wIndex, sc->sc_ctl_iface_no);
516 USETW(req.wLength, UCDC_LINE_STATE_LENGTH);
517
518 err = usbd_do_request(sc->sc_udev, &req, state);
519 if (err) {
520 DPRINTF(("umodem_set_line_coding: failed, err=%s\n",
521 usbd_errstr(err)));
522 return (err);
523 }
524
525 sc->sc_line_state = *state;
526
527 return (USBD_NORMAL_COMPLETION);
528 }
529
530 void *
531 umodem_get_desc(usbd_device_handle dev, int type, int subtype)
532 {
533 usb_descriptor_t *desc;
534 usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev);
535 uByte *p = (uByte *)cd;
536 uByte *end = p + UGETW(cd->wTotalLength);
537
538 while (p < end) {
539 desc = (usb_descriptor_t *)p;
540 if (desc->bDescriptorType == type &&
541 desc->bDescriptorSubtype == subtype)
542 return (desc);
543 p += desc->bLength;
544 }
545
546 return (0);
547 }
548
549 usbd_status
550 umodem_set_comm_feature(struct umodem_softc *sc, int feature, int state)
551 {
552 usb_device_request_t req;
553 usbd_status err;
554 usb_cdc_abstract_state_t ast;
555
556 DPRINTF(("umodem_set_comm_feature: feature=%d state=%d\n", feature,
557 state));
558
559 req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
560 req.bRequest = UCDC_SET_COMM_FEATURE;
561 USETW(req.wValue, feature);
562 USETW(req.wIndex, sc->sc_ctl_iface_no);
563 USETW(req.wLength, UCDC_ABSTRACT_STATE_LENGTH);
564 USETW(ast.wState, state);
565
566 err = usbd_do_request(sc->sc_udev, &req, &ast);
567 if (err) {
568 DPRINTF(("umodem_set_comm_feature: feature=%d, err=%s\n",
569 feature, usbd_errstr(err)));
570 return (err);
571 }
572
573 return (USBD_NORMAL_COMPLETION);
574 }
575
576 int
577 umodem_activate(device_ptr_t self, enum devact act)
578 {
579 struct umodem_softc *sc = (struct umodem_softc *)self;
580 int rv = 0;
581
582 switch (act) {
583 case DVACT_ACTIVATE:
584 return (EOPNOTSUPP);
585 break;
586
587 case DVACT_DEACTIVATE:
588 sc->sc_dying = 1;
589 if (sc->sc_subdev)
590 rv = config_deactivate(sc->sc_subdev);
591 break;
592 }
593 return (rv);
594 }
595
596 USB_DETACH(umodem)
597 {
598 USB_DETACH_START(umodem, sc);
599 int rv = 0;
600
601 DPRINTF(("umodem_detach: sc=%p flags=%d\n", sc, flags));
602
603 sc->sc_dying = 1;
604
605 if (sc->sc_subdev != NULL)
606 rv = config_detach(sc->sc_subdev, flags);
607
608 usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_udev,
609 USBDEV(sc->sc_dev));
610
611 return (rv);
612 }
613