]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/usb/serial/ucycom.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / usb / serial / ucycom.c
1 #include <sys/cdefs.h>
2 __FBSDID("$FreeBSD$");
3
4 /*-
5  * Copyright (c) 2004 Dag-Erling Coïdan Smørgrav
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer
13  *    in this position and unchanged.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 /*
33  * Device driver for Cypress CY7C637xx and CY7C640/1xx series USB to
34  * RS232 bridges.
35  */
36
37 #include <sys/stdint.h>
38 #include <sys/stddef.h>
39 #include <sys/param.h>
40 #include <sys/queue.h>
41 #include <sys/types.h>
42 #include <sys/systm.h>
43 #include <sys/kernel.h>
44 #include <sys/bus.h>
45 #include <sys/module.h>
46 #include <sys/lock.h>
47 #include <sys/mutex.h>
48 #include <sys/condvar.h>
49 #include <sys/sysctl.h>
50 #include <sys/sx.h>
51 #include <sys/unistd.h>
52 #include <sys/callout.h>
53 #include <sys/malloc.h>
54 #include <sys/priv.h>
55
56 #include <dev/usb/usb.h>
57 #include <dev/usb/usbdi.h>
58 #include <dev/usb/usbdi_util.h>
59 #include <dev/usb/usbhid.h>
60 #include "usbdevs.h"
61
62 #define USB_DEBUG_VAR usb_debug
63 #include <dev/usb/usb_debug.h>
64 #include <dev/usb/usb_process.h>
65
66 #include <dev/usb/serial/usb_serial.h>
67
68 #define UCYCOM_MAX_IOLEN        (1024 + 2)      /* bytes */
69
70 #define UCYCOM_IFACE_INDEX      0
71
72 enum {
73         UCYCOM_CTRL_RD,
74         UCYCOM_INTR_RD,
75         UCYCOM_N_TRANSFER,
76 };
77
78 struct ucycom_softc {
79         struct ucom_super_softc sc_super_ucom;
80         struct ucom_softc sc_ucom;
81
82         struct usb_device *sc_udev;
83         struct usb_xfer *sc_xfer[UCYCOM_N_TRANSFER];
84         struct mtx sc_mtx;
85
86         uint32_t sc_model;
87 #define MODEL_CY7C63743         0x63743
88 #define MODEL_CY7C64013         0x64013
89
90         uint16_t sc_flen;               /* feature report length */
91         uint16_t sc_ilen;               /* input report length */
92         uint16_t sc_olen;               /* output report length */
93
94         uint8_t sc_fid;                 /* feature report id */
95         uint8_t sc_iid;                 /* input report id */
96         uint8_t sc_oid;                 /* output report id */
97         uint8_t sc_cfg;
98 #define UCYCOM_CFG_RESET        0x80
99 #define UCYCOM_CFG_PARODD       0x20
100 #define UCYCOM_CFG_PAREN        0x10
101 #define UCYCOM_CFG_STOPB        0x08
102 #define UCYCOM_CFG_DATAB        0x03
103         uint8_t sc_ist;                 /* status flags from last input */
104         uint8_t sc_name[16];
105         uint8_t sc_iface_no;
106         uint8_t sc_temp_cfg[32];
107 };
108
109 /* prototypes */
110
111 static device_probe_t ucycom_probe;
112 static device_attach_t ucycom_attach;
113 static device_detach_t ucycom_detach;
114
115 static usb_callback_t ucycom_ctrl_write_callback;
116 static usb_callback_t ucycom_intr_read_callback;
117
118 static void     ucycom_cfg_open(struct ucom_softc *);
119 static void     ucycom_start_read(struct ucom_softc *);
120 static void     ucycom_stop_read(struct ucom_softc *);
121 static void     ucycom_start_write(struct ucom_softc *);
122 static void     ucycom_stop_write(struct ucom_softc *);
123 static void     ucycom_cfg_write(struct ucycom_softc *, uint32_t, uint8_t);
124 static int      ucycom_pre_param(struct ucom_softc *, struct termios *);
125 static void     ucycom_cfg_param(struct ucom_softc *, struct termios *);
126 static void     ucycom_poll(struct ucom_softc *ucom);
127
128 static const struct usb_config ucycom_config[UCYCOM_N_TRANSFER] = {
129
130         [UCYCOM_CTRL_RD] = {
131                 .type = UE_CONTROL,
132                 .endpoint = 0x00,       /* Control pipe */
133                 .direction = UE_DIR_ANY,
134                 .bufsize = (sizeof(struct usb_device_request) + UCYCOM_MAX_IOLEN),
135                 .callback = &ucycom_ctrl_write_callback,
136                 .timeout = 1000,        /* 1 second */
137         },
138
139         [UCYCOM_INTR_RD] = {
140                 .type = UE_INTERRUPT,
141                 .endpoint = UE_ADDR_ANY,
142                 .direction = UE_DIR_IN,
143                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
144                 .bufsize = UCYCOM_MAX_IOLEN,
145                 .callback = &ucycom_intr_read_callback,
146         },
147 };
148
149 static const struct ucom_callback ucycom_callback = {
150         .ucom_cfg_param = &ucycom_cfg_param,
151         .ucom_cfg_open = &ucycom_cfg_open,
152         .ucom_pre_param = &ucycom_pre_param,
153         .ucom_start_read = &ucycom_start_read,
154         .ucom_stop_read = &ucycom_stop_read,
155         .ucom_start_write = &ucycom_start_write,
156         .ucom_stop_write = &ucycom_stop_write,
157         .ucom_poll = &ucycom_poll,
158 };
159
160 static device_method_t ucycom_methods[] = {
161         DEVMETHOD(device_probe, ucycom_probe),
162         DEVMETHOD(device_attach, ucycom_attach),
163         DEVMETHOD(device_detach, ucycom_detach),
164         {0, 0}
165 };
166
167 static devclass_t ucycom_devclass;
168
169 static driver_t ucycom_driver = {
170         .name = "ucycom",
171         .methods = ucycom_methods,
172         .size = sizeof(struct ucycom_softc),
173 };
174
175 DRIVER_MODULE(ucycom, uhub, ucycom_driver, ucycom_devclass, NULL, 0);
176 MODULE_DEPEND(ucycom, ucom, 1, 1, 1);
177 MODULE_DEPEND(ucycom, usb, 1, 1, 1);
178 MODULE_VERSION(ucycom, 1);
179
180 /*
181  * Supported devices
182  */
183 static const STRUCT_USB_HOST_ID ucycom_devs[] = {
184         {USB_VPI(USB_VENDOR_DELORME, USB_PRODUCT_DELORME_EARTHMATE, MODEL_CY7C64013)},
185 };
186
187 #define UCYCOM_DEFAULT_RATE      4800
188 #define UCYCOM_DEFAULT_CFG       0x03   /* N-8-1 */
189
190 static int
191 ucycom_probe(device_t dev)
192 {
193         struct usb_attach_arg *uaa = device_get_ivars(dev);
194
195         if (uaa->usb_mode != USB_MODE_HOST) {
196                 return (ENXIO);
197         }
198         if (uaa->info.bConfigIndex != 0) {
199                 return (ENXIO);
200         }
201         if (uaa->info.bIfaceIndex != UCYCOM_IFACE_INDEX) {
202                 return (ENXIO);
203         }
204         return (usbd_lookup_id_by_uaa(ucycom_devs, sizeof(ucycom_devs), uaa));
205 }
206
207 static int
208 ucycom_attach(device_t dev)
209 {
210         struct usb_attach_arg *uaa = device_get_ivars(dev);
211         struct ucycom_softc *sc = device_get_softc(dev);
212         void *urd_ptr = NULL;
213         int32_t error;
214         uint16_t urd_len;
215         uint8_t iface_index;
216
217         sc->sc_udev = uaa->device;
218
219         device_set_usb_desc(dev);
220         mtx_init(&sc->sc_mtx, "ucycom", NULL, MTX_DEF);
221
222         snprintf(sc->sc_name, sizeof(sc->sc_name),
223             "%s", device_get_nameunit(dev));
224
225         DPRINTF("\n");
226
227         /* get chip model */
228         sc->sc_model = USB_GET_DRIVER_INFO(uaa);
229         if (sc->sc_model == 0) {
230                 device_printf(dev, "unsupported device\n");
231                 goto detach;
232         }
233         device_printf(dev, "Cypress CY7C%X USB to RS232 bridge\n", sc->sc_model);
234
235         /* get report descriptor */
236
237         error = usbd_req_get_hid_desc(uaa->device, NULL,
238             &urd_ptr, &urd_len, M_USBDEV,
239             UCYCOM_IFACE_INDEX);
240
241         if (error) {
242                 device_printf(dev, "failed to get report "
243                     "descriptor: %s\n",
244                     usbd_errstr(error));
245                 goto detach;
246         }
247         /* get report sizes */
248
249         sc->sc_flen = hid_report_size(urd_ptr, urd_len, hid_feature, &sc->sc_fid);
250         sc->sc_ilen = hid_report_size(urd_ptr, urd_len, hid_input, &sc->sc_iid);
251         sc->sc_olen = hid_report_size(urd_ptr, urd_len, hid_output, &sc->sc_oid);
252
253         if ((sc->sc_ilen > UCYCOM_MAX_IOLEN) || (sc->sc_ilen < 1) ||
254             (sc->sc_olen > UCYCOM_MAX_IOLEN) || (sc->sc_olen < 2) ||
255             (sc->sc_flen > UCYCOM_MAX_IOLEN) || (sc->sc_flen < 5)) {
256                 device_printf(dev, "invalid report size i=%d, o=%d, f=%d, max=%d\n",
257                     sc->sc_ilen, sc->sc_olen, sc->sc_flen,
258                     UCYCOM_MAX_IOLEN);
259                 goto detach;
260         }
261         sc->sc_iface_no = uaa->info.bIfaceNum;
262
263         iface_index = UCYCOM_IFACE_INDEX;
264         error = usbd_transfer_setup(uaa->device, &iface_index,
265             sc->sc_xfer, ucycom_config, UCYCOM_N_TRANSFER,
266             sc, &sc->sc_mtx);
267         if (error) {
268                 device_printf(dev, "allocating USB "
269                     "transfers failed\n");
270                 goto detach;
271         }
272         error = ucom_attach(&sc->sc_super_ucom, &sc->sc_ucom, 1, sc,
273             &ucycom_callback, &sc->sc_mtx);
274         if (error) {
275                 goto detach;
276         }
277         ucom_set_pnpinfo_usb(&sc->sc_super_ucom, dev);
278
279         if (urd_ptr) {
280                 free(urd_ptr, M_USBDEV);
281         }
282
283         return (0);                     /* success */
284
285 detach:
286         if (urd_ptr) {
287                 free(urd_ptr, M_USBDEV);
288         }
289         ucycom_detach(dev);
290         return (ENXIO);
291 }
292
293 static int
294 ucycom_detach(device_t dev)
295 {
296         struct ucycom_softc *sc = device_get_softc(dev);
297
298         ucom_detach(&sc->sc_super_ucom, &sc->sc_ucom);
299         usbd_transfer_unsetup(sc->sc_xfer, UCYCOM_N_TRANSFER);
300         mtx_destroy(&sc->sc_mtx);
301
302         return (0);
303 }
304
305 static void
306 ucycom_cfg_open(struct ucom_softc *ucom)
307 {
308         struct ucycom_softc *sc = ucom->sc_parent;
309
310         /* set default configuration */
311         ucycom_cfg_write(sc, UCYCOM_DEFAULT_RATE, UCYCOM_DEFAULT_CFG);
312 }
313
314 static void
315 ucycom_start_read(struct ucom_softc *ucom)
316 {
317         struct ucycom_softc *sc = ucom->sc_parent;
318
319         usbd_transfer_start(sc->sc_xfer[UCYCOM_INTR_RD]);
320 }
321
322 static void
323 ucycom_stop_read(struct ucom_softc *ucom)
324 {
325         struct ucycom_softc *sc = ucom->sc_parent;
326
327         usbd_transfer_stop(sc->sc_xfer[UCYCOM_INTR_RD]);
328 }
329
330 static void
331 ucycom_start_write(struct ucom_softc *ucom)
332 {
333         struct ucycom_softc *sc = ucom->sc_parent;
334
335         usbd_transfer_start(sc->sc_xfer[UCYCOM_CTRL_RD]);
336 }
337
338 static void
339 ucycom_stop_write(struct ucom_softc *ucom)
340 {
341         struct ucycom_softc *sc = ucom->sc_parent;
342
343         usbd_transfer_stop(sc->sc_xfer[UCYCOM_CTRL_RD]);
344 }
345
346 static void
347 ucycom_ctrl_write_callback(struct usb_xfer *xfer, usb_error_t error)
348 {
349         struct ucycom_softc *sc = usbd_xfer_softc(xfer);
350         struct usb_device_request req;
351         struct usb_page_cache *pc0, *pc1;
352         uint8_t data[2];
353         uint8_t offset;
354         uint32_t actlen;
355
356         pc0 = usbd_xfer_get_frame(xfer, 0);
357         pc1 = usbd_xfer_get_frame(xfer, 1);
358
359         switch (USB_GET_STATE(xfer)) {
360         case USB_ST_TRANSFERRED:
361 tr_transferred:
362         case USB_ST_SETUP:
363
364                 switch (sc->sc_model) {
365                 case MODEL_CY7C63743:
366                         offset = 1;
367                         break;
368                 case MODEL_CY7C64013:
369                         offset = 2;
370                         break;
371                 default:
372                         offset = 0;
373                         break;
374                 }
375
376                 if (ucom_get_data(&sc->sc_ucom, pc1, offset,
377                     sc->sc_olen - offset, &actlen)) {
378
379                         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
380                         req.bRequest = UR_SET_REPORT;
381                         USETW2(req.wValue, UHID_OUTPUT_REPORT, sc->sc_oid);
382                         req.wIndex[0] = sc->sc_iface_no;
383                         req.wIndex[1] = 0;
384                         USETW(req.wLength, sc->sc_olen);
385
386                         switch (sc->sc_model) {
387                         case MODEL_CY7C63743:
388                                 data[0] = actlen;
389                                 break;
390                         case MODEL_CY7C64013:
391                                 data[0] = 0;
392                                 data[1] = actlen;
393                                 break;
394                         default:
395                                 break;
396                         }
397
398                         usbd_copy_in(pc0, 0, &req, sizeof(req));
399                         usbd_copy_in(pc1, 0, data, offset);
400
401                         usbd_xfer_set_frame_len(xfer, 0, sizeof(req));
402                         usbd_xfer_set_frame_len(xfer, 1, sc->sc_olen);
403                         usbd_xfer_set_frames(xfer, sc->sc_olen ? 2 : 1);
404                         usbd_transfer_submit(xfer);
405                 }
406                 return;
407
408         default:                        /* Error */
409                 if (error == USB_ERR_CANCELLED) {
410                         return;
411                 }
412                 DPRINTF("error=%s\n",
413                     usbd_errstr(error));
414                 goto tr_transferred;
415         }
416 }
417
418 static void
419 ucycom_cfg_write(struct ucycom_softc *sc, uint32_t baud, uint8_t cfg)
420 {
421         struct usb_device_request req;
422         uint16_t len;
423         usb_error_t err;
424
425         len = sc->sc_flen;
426         if (len > sizeof(sc->sc_temp_cfg)) {
427                 len = sizeof(sc->sc_temp_cfg);
428         }
429         sc->sc_cfg = cfg;
430
431         req.bmRequestType = UT_WRITE_CLASS_INTERFACE;
432         req.bRequest = UR_SET_REPORT;
433         USETW2(req.wValue, UHID_FEATURE_REPORT, sc->sc_fid);
434         req.wIndex[0] = sc->sc_iface_no;
435         req.wIndex[1] = 0;
436         USETW(req.wLength, len);
437
438         sc->sc_temp_cfg[0] = (baud & 0xff);
439         sc->sc_temp_cfg[1] = (baud >> 8) & 0xff;
440         sc->sc_temp_cfg[2] = (baud >> 16) & 0xff;
441         sc->sc_temp_cfg[3] = (baud >> 24) & 0xff;
442         sc->sc_temp_cfg[4] = cfg;
443
444         err = ucom_cfg_do_request(sc->sc_udev, &sc->sc_ucom,
445             &req, sc->sc_temp_cfg, 0, 1000);
446         if (err) {
447                 DPRINTFN(0, "device request failed, err=%s "
448                     "(ignored)\n", usbd_errstr(err));
449         }
450 }
451
452 static int
453 ucycom_pre_param(struct ucom_softc *ucom, struct termios *t)
454 {
455         switch (t->c_ospeed) {
456                 case 600:
457                 case 1200:
458                 case 2400:
459                 case 4800:
460                 case 9600:
461                 case 19200:
462                 case 38400:
463                 case 57600:
464 #if 0
465                 /*
466                  * Stock chips only support standard baud rates in the 600 - 57600
467                  * range, but higher rates can be achieved using custom firmware.
468                  */
469                 case 115200:
470                 case 153600:
471                 case 192000:
472 #endif
473                 break;
474         default:
475                 return (EINVAL);
476         }
477         return (0);
478 }
479
480 static void
481 ucycom_cfg_param(struct ucom_softc *ucom, struct termios *t)
482 {
483         struct ucycom_softc *sc = ucom->sc_parent;
484         uint8_t cfg;
485
486         DPRINTF("\n");
487
488         if (t->c_cflag & CIGNORE) {
489                 cfg = sc->sc_cfg;
490         } else {
491                 cfg = 0;
492                 switch (t->c_cflag & CSIZE) {
493                 default:
494                 case CS8:
495                         ++cfg;
496                 case CS7:
497                         ++cfg;
498                 case CS6:
499                         ++cfg;
500                 case CS5:
501                         break;
502                 }
503
504                 if (t->c_cflag & CSTOPB)
505                         cfg |= UCYCOM_CFG_STOPB;
506                 if (t->c_cflag & PARENB)
507                         cfg |= UCYCOM_CFG_PAREN;
508                 if (t->c_cflag & PARODD)
509                         cfg |= UCYCOM_CFG_PARODD;
510         }
511
512         ucycom_cfg_write(sc, t->c_ospeed, cfg);
513 }
514
515 static void
516 ucycom_intr_read_callback(struct usb_xfer *xfer, usb_error_t error)
517 {
518         struct ucycom_softc *sc = usbd_xfer_softc(xfer);
519         struct usb_page_cache *pc;
520         uint8_t buf[2];
521         uint32_t offset;
522         uint32_t len;
523         int actlen;
524
525         usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL);
526         pc = usbd_xfer_get_frame(xfer, 0);
527
528         switch (USB_GET_STATE(xfer)) {
529         case USB_ST_TRANSFERRED:
530                 switch (sc->sc_model) {
531                 case MODEL_CY7C63743:
532                         if (actlen < 1) {
533                                 goto tr_setup;
534                         }
535                         usbd_copy_out(pc, 0, buf, 1);
536
537                         sc->sc_ist = buf[0] & ~0x07;
538                         len = buf[0] & 0x07;
539
540                         actlen--;
541                         offset = 1;
542
543                         break;
544
545                 case MODEL_CY7C64013:
546                         if (actlen < 2) {
547                                 goto tr_setup;
548                         }
549                         usbd_copy_out(pc, 0, buf, 2);
550
551                         sc->sc_ist = buf[0] & ~0x07;
552                         len = buf[1];
553
554                         actlen -= 2;
555                         offset = 2;
556
557                         break;
558
559                 default:
560                         DPRINTFN(0, "unsupported model number\n");
561                         goto tr_setup;
562                 }
563
564                 if (len > actlen)
565                         len = actlen;
566                 if (len)
567                         ucom_put_data(&sc->sc_ucom, pc, offset, len);
568                 /* FALLTHROUGH */
569         case USB_ST_SETUP:
570 tr_setup:
571                 usbd_xfer_set_frame_len(xfer, 0, sc->sc_ilen);
572                 usbd_transfer_submit(xfer);
573                 return;
574
575         default:                        /* Error */
576                 if (error != USB_ERR_CANCELLED) {
577                         /* try to clear stall first */
578                         usbd_xfer_set_stall(xfer);
579                         goto tr_setup;
580                 }
581                 return;
582
583         }
584 }
585
586 static void
587 ucycom_poll(struct ucom_softc *ucom)
588 {
589         struct ucycom_softc *sc = ucom->sc_parent;
590         usbd_transfer_poll(sc->sc_xfer, UCYCOM_N_TRANSFER);
591 }