Make Huawei E220 change the mode from device with single umass interface to
[dragonfly.git] / sys / dev / usbmisc / ugensa / ugensa.c
1 /* $DragonFly: src/sys/dev/usbmisc/ugensa/ugensa.c,v 1.7 2008/08/22 06:57:33 hasso Exp $ */
2 /* $OpenBSD: umsm.c,v 1.15 2007/06/14 10:11:16 mbalmer Exp $ */
3
4 /*
5  * Copyright (c) 2006 Jonathan Gray <jsg@openbsd.org>
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19
20 /*
21  * Generic USB serial driver used for devices where hardware specific
22  * don't apply or doesn't make sense (for example Qualcomm MSM EVDO, UMTS
23  * and other similar communication devices).
24  */
25
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/kernel.h>
29 #include <sys/device.h>
30 #include <sys/conf.h>
31 #include <sys/tty.h>
32 #include <sys/types.h>
33 #include <sys/bus.h>
34 #include <sys/module.h>
35
36 #include <bus/usb/usb.h>
37 #include <bus/usb/usbdi.h>
38 #include <bus/usb/usbdi_util.h>
39 #include <bus/usb/usbcdc.h>
40 #include <dev/usbmisc/ucom/ucomvar.h>
41
42 #ifdef UGENSA_DEBUG
43 static int      ugensadebug = 1;
44 #define DPRINTFN(n, x)  do { if (ugensadebug > (n)) kprintf x; } while (0)
45 #else
46 #define DPRINTFN(n, x)
47 #endif
48 #define DPRINTF(x) DPRINTFN(0, x)
49
50 #define UGENSABUFSZ             4096
51 #define UGENSA_INTR_INTERVAL    100     /* ms */
52
53 struct ugensa_softc {
54         struct ucom_softc        sc_ucom;
55         int                      sc_iface_no;
56
57         /* interrupt ep */
58         int                      sc_intr_number;
59         usbd_pipe_handle         sc_intr_pipe;
60         u_char                  *sc_intr_buf;
61         int                      sc_isize;
62
63         u_char                   sc_lsr;        /* Local status register */
64         u_char                   sc_msr;        /* Status register */
65         u_char                   sc_dtr;        /* Current DTR state */
66         u_char                   sc_rts;        /* Current RTS state */
67 };
68
69 static device_probe_t ugensa_match;
70 static device_attach_t ugensa_attach;
71 static device_detach_t ugensa_detach;
72
73 static int  ugensa_open(void *, int);
74 static void ugensa_close(void *, int);
75 static void ugensa_intr(usbd_xfer_handle, usbd_private_handle, usbd_status);
76 static void ugensa_get_status(void *, int, u_char *, u_char *);
77 static void ugensa_set(void *, int, int, int);
78
79 static void ugensa_e220_changemode(usbd_device_handle);
80
81 static device_method_t ugensa_methods[] = {
82         /* Device interface */
83         DEVMETHOD(device_probe, ugensa_match),
84         DEVMETHOD(device_attach, ugensa_attach),
85         DEVMETHOD(device_detach, ugensa_detach),
86         { 0, 0 }
87 };
88
89 static driver_t ugensa_driver = { 
90         "ucom",
91         ugensa_methods,
92         sizeof (struct ugensa_softc)
93 };
94
95 struct ucom_callback ugensa_callback = {
96         ugensa_get_status,
97         ugensa_set,
98         NULL,
99         NULL,
100         ugensa_open,
101         ugensa_close,
102         NULL,
103         NULL
104 };
105
106 static const struct usb_devno ugensa_devs[] = {
107         { USB_DEVICE(0x05c6, 0x6000) }, /* Qualcomm HSDPA MSM */
108         { USB_DEVICE(0x05c6, 0x6613) }, /* Qualcomm HSDPA MSM */
109         { USB_DEVICE(0x0c88, 0x17da) }, /* Kyocera KPC650 */
110         { USB_DEVICE(0x0f3d, 0x0112) }, /* AirPrime PC5220 */
111         { USB_DEVICE(0x1199, 0x0017) }, /* Sierra Wireless EM5625 */
112         { USB_DEVICE(0x1199, 0x0018) }, /* Sierra Wireless MC5720 */
113         { USB_DEVICE(0x1199, 0x0019) }, /* Sierra Wireless AirCard 595 */
114         { USB_DEVICE(0x1199, 0x0020) }, /* Sierra Wireless MC5725 */
115         { USB_DEVICE(0x1199, 0x0021) }, /* Sierra Wireless AirCard 597E */
116         { USB_DEVICE(0x1199, 0x0112) }, /* Sierra Wireless AirCard 580 */
117         { USB_DEVICE(0x1199, 0x0120) }, /* Sierra Wireless AirCard 595U */
118         { USB_DEVICE(0x1199, 0x0218) }, /* Sierra Wireless MC5720 */
119         { USB_DEVICE(0x1199, 0x0220) }, /* Sierra Wireless MC5725 */
120         { USB_DEVICE(0x1199, 0x6802) }, /* Sierra Wireless MC8755 */
121         { USB_DEVICE(0x1199, 0x6803) }, /* Sierra Wireless MC8765 */
122         { USB_DEVICE(0x1199, 0x6804) }, /* Sierra Wireless MC8755 */
123         { USB_DEVICE(0x1199, 0x6812) }, /* Sierra Wireless MC8775 */
124         { USB_DEVICE(0x1199, 0x6813) }, /* Sierra Wireless MC8755 */
125         { USB_DEVICE(0x1199, 0x6820) }, /* Sierra Wireless AirCard 875 */
126         { USB_DEVICE(0x1199, 0x6832) }, /* Sierra Wireless MC8780 */
127         { USB_DEVICE(0x1199, 0x6833) }, /* Sierra Wireless MC8781 */
128         { USB_DEVICE(0x1199, 0x6850) }, /* Sierra Wireless AirCard 880 */
129         { USB_DEVICE(0x1199, 0x6851) }, /* Sierra Wireless AirCard 881 */
130         { USB_DEVICE(0x1199, 0x6852) }, /* Sierra Wireless AirCard 880E */
131         { USB_DEVICE(0x1199, 0x6853) }, /* Sierra Wireless AirCard 881E */
132         { USB_DEVICE(0x1199, 0x6855) }, /* Sierra Wireless AirCard 880U */
133         { USB_DEVICE(0x1199, 0x6856) }, /* Sierra Wireless AirCard 881U */
134         { USB_DEVICE(0x12d1, 0x1001) }, /* Huawei Mobile Connect */
135         { USB_DEVICE(0x12d1, 0x1003) }, /* Huawei Mobile E220 */
136         { USB_DEVICE(0x12d1, 0x1004) }, /* Huawei Mobile E220 */
137         { USB_DEVICE(0x1410, 0x1100) }, /* Novatel Wireless Merlin XS620/S640 */
138         { USB_DEVICE(0x1410, 0x1110) }, /* Novatel Wireless Merlin S620/V620 */
139         { USB_DEVICE(0x1410, 0x1120) }, /* Novatel Wireless Merlin EX720 */
140         { USB_DEVICE(0x1410, 0x1130) }, /* Novatel Wireless Merlin S720 */
141         { USB_DEVICE(0x1410, 0x1400) }, /* Novatel Wireless Merlin U730 */
142         { USB_DEVICE(0x1410, 0x1410) }, /* Novatel Wireless Merlin U740 */
143         { USB_DEVICE(0x1410, 0x1420) }, /* Novatel Wireless Expedite EU870D */
144         { USB_DEVICE(0x1410, 0x1430) }, /* Novatel Wireless Merlin XU870 */
145         { USB_DEVICE(0x1410, 0x2100) }, /* Novatel Wireless Expedite EV620 */
146         { USB_DEVICE(0x1410, 0x2110) }, /* Novatel Wireless Merlin ES620,
147                                            Merlin ES720, Ovation U720 */
148         { USB_DEVICE(0x1410, 0x2130) }, /* Novatel Wireless Merlin ES620 */
149         { USB_DEVICE(0x1410, 0x2410) }, /* Novatel Wireless Expedite EU740 */
150         { USB_DEVICE(0x1410, 0x4100) }, /* Novatel Wireless Ovation MC727 */
151         { USB_DEVICE(0x1410, 0x4400) }, /* Novatel Wireless Ovation MC950D */
152         { USB_DEVICE(0x16d5, 0x6501) }, /* AnyDATA ADU-E100A/D/H */
153         { USB_DEVICE(0x413c, 0x8114) }, /* Dell Wireless 5700 */
154         { USB_DEVICE(0x413c, 0x8115) }, /* Dell Wireless 5500 */
155         { USB_DEVICE(0x413c, 0x8116) }, /* Dell Wireless 5505 */
156         { USB_DEVICE(0x413c, 0x8117) }, /* Dell Wireless 5700 */
157         { USB_DEVICE(0x413c, 0x8118) }, /* Dell Wireless 5510 */
158         { USB_DEVICE(0x413c, 0x8128) }, /* Dell Wireless 5700 */
159         { USB_DEVICE(0x413c, 0x8136) }, /* Dell Wireless 5520 */
160         { USB_DEVICE(0x413c, 0x8137) }, /* Dell Wireless 5520 */
161 };
162
163 DRIVER_MODULE(ugensa, uhub, ugensa_driver, ucom_devclass, usbd_driver_load, 0);
164 MODULE_DEPEND(ugensa, usb, 1, 1, 1);
165 MODULE_DEPEND(ugensa, ucom, UCOM_MINVER, UCOM_PREFVER, UCOM_MAXVER);
166 MODULE_VERSION(ugensa, 1);
167
168 static int
169 ugensa_match(device_t self)
170 {
171         struct usb_attach_arg *uaa = device_get_ivars(self);
172         usb_interface_descriptor_t *id;
173
174         if (uaa->iface == NULL)
175                 return UMATCH_NONE;
176
177         /*
178          * Some devices have massstorage interfaces - don't claim ownership
179          * of these ... in general.
180          * 
181          * Some devices (most notably Huawei E220) need special handling
182          * though. These come up with single umass interface, waiting for
183          * magic sent to it, detach and attach again with three interfaces.
184          * We have to claim such mass storage interface to send a magic to
185          * it.
186          */
187         id = usbd_get_interface_descriptor(uaa->iface);
188         if (id == NULL || id->bInterfaceClass == UICLASS_MASS) {
189                 if ((uaa->vendor == 0x12d1 && uaa->product == 0x1003) ||
190                     (uaa->vendor == 0x12d1 && uaa->product == 0x1004)) {
191                         if (uaa->nifaces == 1)
192                                 return UMATCH_VENDOR_IFACESUBCLASS;
193                         else
194                                 return UMATCH_NONE;
195                 } else
196                         return UMATCH_NONE;
197         }
198
199         return (usb_lookup(ugensa_devs, uaa->vendor, uaa->product) != NULL) ?
200             UMATCH_VENDOR_IFACESUBCLASS : UMATCH_NONE;
201 }
202
203 static int
204 ugensa_attach(device_t self)
205 {
206         struct ugensa_softc *sc = device_get_softc(self);
207         struct usb_attach_arg *uaa = device_get_ivars(self);
208         struct ucom_softc *ucom;
209         usb_interface_descriptor_t *id;
210         usb_endpoint_descriptor_t *ed;
211         int i;
212
213         ucom = &sc->sc_ucom;
214         bzero(sc, sizeof (struct ugensa_softc));
215
216         ucom->sc_dev = self;
217         ucom->sc_udev = uaa->device;
218         ucom->sc_iface = uaa->iface;
219
220         id = usbd_get_interface_descriptor(ucom->sc_iface);
221         if (id == NULL || id->bInterfaceClass == UICLASS_MASS) {
222                 if ((uaa->vendor == 0x12d1 && uaa->product == 0x1003) ||
223                     (uaa->vendor == 0x12d1 && uaa->product == 0x1004)) {
224                         ugensa_e220_changemode(uaa->device);
225                 }
226                 return ENXIO;
227         }
228
229         sc->sc_iface_no = id->bInterfaceNumber;
230         ucom->sc_bulkin_no = ucom->sc_bulkout_no = -1;
231         for (i = 0; i < id->bNumEndpoints; i++) {
232                 ed = usbd_interface2endpoint_descriptor(ucom->sc_iface, i);
233                 if (ed == NULL) {
234                         device_printf(ucom->sc_dev, "no endpoint descriptor "
235                                       "found for %d\n", i);
236                         goto error;
237                 }
238
239                 if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
240                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_INTERRUPT) {
241                         sc->sc_intr_number = ed->bEndpointAddress;
242                         sc->sc_isize = UGETW(ed->wMaxPacketSize);
243                 } else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_IN &&
244                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
245                         ucom->sc_bulkin_no = ed->bEndpointAddress;
246                 else if (UE_GET_DIR(ed->bEndpointAddress) == UE_DIR_OUT &&
247                     UE_GET_XFERTYPE(ed->bmAttributes) == UE_BULK)
248                         ucom->sc_bulkout_no = ed->bEndpointAddress;
249         }
250         if (ucom->sc_bulkin_no == -1 || ucom->sc_bulkout_no == -1) {
251                 device_printf(ucom->sc_dev, "missing endpoint\n");
252                 goto error;
253         }
254
255         sc->sc_dtr = sc->sc_rts = -1;
256
257         ucom->sc_parent = sc;
258         ucom->sc_portno = UCOM_UNK_PORTNO;
259         ucom->sc_ibufsize = UGENSABUFSZ;
260         ucom->sc_obufsize = UGENSABUFSZ;
261         ucom->sc_ibufsizepad = UGENSABUFSZ;
262         ucom->sc_opkthdrlen = 0;
263         ucom->sc_callback = &ugensa_callback;
264
265         usbd_add_drv_event(USB_EVENT_DRIVER_ATTACH, ucom->sc_udev,
266                            ucom->sc_dev);
267
268         DPRINTF(("%s: in = 0x%x, out = 0x%x\n",
269                  device_get_nameunit(ucom->sc_dev), ucom->sc_bulkin_no,
270                  ucom->sc_bulkout_no));
271
272         ucom_attach(&sc->sc_ucom);
273
274         return 0;
275
276 error:
277         ucom->sc_dying = 1;
278         return ENXIO;
279 }
280
281 static int
282 ugensa_detach(device_t self)
283 {
284         struct ugensa_softc *sc = device_get_softc(self);
285         int rv = 0;
286
287         /* close the interrupt endpoint if that is opened */
288         if (sc->sc_intr_pipe != NULL) {
289                 usbd_abort_pipe(sc->sc_intr_pipe);
290                 usbd_close_pipe(sc->sc_intr_pipe);
291                 kfree(sc->sc_intr_buf, M_USBDEV);
292                 sc->sc_intr_pipe = NULL;
293         }
294
295         DPRINTF(("ugensa_detach: sc=%p\n", sc));
296         sc->sc_ucom.sc_dying = 1;
297         rv = ucom_detach(&sc->sc_ucom);
298         usbd_add_drv_event(USB_EVENT_DRIVER_DETACH, sc->sc_ucom.sc_udev,
299                            sc->sc_ucom.sc_dev);
300
301         return (rv);
302 }
303
304 #if 0 /* not yet */
305 int
306 ugensa_activate(struct device *self, enum devact act)
307 {
308         struct ugensa_softc *sc = (struct ugensa_softc *)self;
309         int rv = 0;
310
311         switch (act) {
312         case DVACT_ACTIVATE:
313                 break;
314
315         case DVACT_DEACTIVATE:
316                 if (sc->sc_subdev != NULL)
317                         rv = config_deactivate(sc->sc_subdev);
318                 sc->sc_dying = 1;
319                 break;
320         }
321         return (rv);
322 }
323 #endif
324
325 static int
326 ugensa_open(void *addr, int portno)
327 {
328         struct ugensa_softc *sc = addr;
329         int err;
330
331         if (sc->sc_ucom.sc_dying)
332                 return (ENXIO);
333
334         if (sc->sc_intr_number != -1 && sc->sc_intr_pipe == NULL) {
335                 sc->sc_intr_buf = kmalloc(sc->sc_isize, M_USBDEV, M_WAITOK);
336                 err = usbd_open_pipe_intr(sc->sc_ucom.sc_iface,
337                     sc->sc_intr_number, USBD_SHORT_XFER_OK, &sc->sc_intr_pipe,
338                     sc, sc->sc_intr_buf, sc->sc_isize, ugensa_intr,
339                     UGENSA_INTR_INTERVAL);
340                 if (err) {
341                         device_printf(sc->sc_ucom.sc_dev,
342                             "cannot open interrupt pipe (addr %d)\n",
343                             sc->sc_intr_number);
344                         return (EIO);
345                 }
346         }
347
348         return (0);
349 }
350
351 static void
352 ugensa_close(void *addr, int portno)
353 {
354         struct ugensa_softc *sc = addr;
355         int err;
356
357         if (sc->sc_ucom.sc_dying)
358                 return;
359
360         if (sc->sc_intr_pipe != NULL) {
361                 err = usbd_abort_pipe(sc->sc_intr_pipe);
362                 if (err)
363                         device_printf(sc->sc_ucom.sc_dev,
364                             "abort interrupt pipe failed: %s\n",
365                             usbd_errstr(err));
366                 err = usbd_close_pipe(sc->sc_intr_pipe);
367                 if (err)
368                         device_printf(sc->sc_ucom.sc_dev,
369                             "close interrupt pipe failed: %s\n",
370                             usbd_errstr(err));
371                 kfree(sc->sc_intr_buf, M_USBDEV);
372                 sc->sc_intr_pipe = NULL;
373         }
374
375 }
376
377 static void
378 ugensa_intr(usbd_xfer_handle xfer, usbd_private_handle priv, usbd_status status)
379 {
380         struct ugensa_softc *sc = priv;
381         usb_cdc_notification_t *buf;
382         u_char mstatus;
383
384         buf = (usb_cdc_notification_t *)sc->sc_intr_buf;
385         if (sc->sc_ucom.sc_dying)
386                 return;
387
388         if (status != USBD_NORMAL_COMPLETION) {
389                 if (status == USBD_NOT_STARTED || status == USBD_CANCELLED)
390                         return;
391
392                 device_printf(sc->sc_ucom.sc_dev,
393                     "ugensa_intr: abnormal status: %s\n", usbd_errstr(status));
394                 usbd_clear_endpoint_stall_async(sc->sc_intr_pipe);
395                 return;
396         }
397
398         if (buf->bmRequestType != UCDC_NOTIFICATION) {
399                 DPRINTF(("%s: umsm_intr: unknown message type(0x%02x)\n",
400                     sc->sc_dev.dv_xname, buf->bmRequestType));
401                 return;
402         }
403
404         if (buf->bNotification == UCDC_N_SERIAL_STATE) {
405                 /* invalid message length, discard it */
406                 if (UGETW(buf->wLength) != 2)
407                         return;
408                 /* XXX: sc_lsr is always 0 */
409                 sc->sc_lsr = sc->sc_msr = 0;
410                 mstatus = buf->data[0];
411                 if (ISSET(mstatus, UCDC_N_SERIAL_RI))
412                         sc->sc_msr |= UMSR_RI;
413                 if (ISSET(mstatus, UCDC_N_SERIAL_DSR))
414                         sc->sc_msr |= UMSR_DSR;
415                 if (ISSET(mstatus, UCDC_N_SERIAL_DCD))
416                         sc->sc_msr |= UMSR_DCD;
417         } else if (buf->bNotification != UCDC_N_CONNECTION_SPEED_CHANGE) {
418                 DPRINTF(("%s: umsm_intr: unknown notify message (0x%02x)\n",
419                     sc->sc_dev.dv_xname, buf->bNotification));
420                 return;
421         }
422
423         ucom_status_change(&sc->sc_ucom);
424 }
425
426 static void
427 ugensa_get_status(void *addr, int portno, u_char *lsr, u_char *msr)
428 {
429         struct ugensa_softc *sc = addr;
430
431         if (lsr != NULL)
432                 *lsr = sc->sc_lsr;
433         if (msr != NULL)
434                 *msr = sc->sc_msr;
435 }
436
437 static void
438 ugensa_set(void *addr, int portno, int reg, int onoff)
439 {
440         struct ugensa_softc *sc = addr;
441         usb_device_request_t req;
442         int ls;
443
444         switch (reg) {
445         case UCOM_SET_DTR:
446                 if (sc->sc_dtr == onoff)
447                         return;
448                 sc->sc_dtr = onoff;
449                 break;
450         case UCOM_SET_RTS:
451                 if (sc->sc_rts == onoff)
452                         return;
453                 sc->sc_rts = onoff;
454                 break;
455         default:
456                 return;
457         }
458
459         /* build an usb request */
460         ls = (sc->sc_dtr ? UCDC_LINE_DTR : 0) |
461             (sc->sc_rts ? UCDC_LINE_RTS : 0);
462         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
463         req.bRequest = UCDC_SET_CONTROL_LINE_STATE;
464         USETW(req.wValue, ls);
465         USETW(req.wIndex, sc->sc_iface_no);
466         USETW(req.wLength, 0);
467
468         (void)usbd_do_request(sc->sc_ucom.sc_udev, &req, 0);
469 }
470
471 static void
472 ugensa_e220_changemode(usbd_device_handle dev)
473 {
474         usb_device_request_t req;
475
476         req.bmRequestType = UT_WRITE_DEVICE;
477         req.bRequest = UR_SET_FEATURE;
478         USETW(req.wValue, UF_DEVICE_REMOTE_WAKEUP);
479         USETW(req.wIndex, 0x2);
480         USETW(req.wLength, 0);
481
482         usbd_do_request(dev, &req, 0);
483 }
484