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