summaryrefslogtreecommitdiff
path: root/class/serial/usbh_pl2303.c
blob: 062f8128fdbac4346a383bf10963af4bc8ad1db8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
/*
 * Copyright (c) 2024 ~ 2025, sakumisu
 * Copyright (c) 2024, Derek Konigsberg
 *
 * SPDX-License-Identifier: Apache-2.0
 */
#include "usbh_core.h"
#include "usbh_serial.h"
#include "usbh_pl2303.h"

#undef USB_DBG_TAG
#define USB_DBG_TAG "usbh_pl2303"
#include "usb_log.h"

#define UART_STATE_INDEX          8
#define UART_STATE_MSR_MASK       0x8b
#define UART_STATE_TRANSIENT_MASK 0x74
#define UART_DCD                  0x01
#define UART_DSR                  0x02
#define UART_BREAK_ERROR          0x04
#define UART_RING                 0x08
#define UART_FRAME_ERROR          0x10
#define UART_PARITY_ERROR         0x20
#define UART_OVERRUN_ERROR        0x40
#define UART_CTS                  0x80

struct pl2303_type_data {
    const char *name;
    uint32_t max_baud_rate;
    unsigned long quirks;
    unsigned int no_autoxonxoff : 1;
    unsigned int no_divisors    : 1;
    unsigned int alt_divisors   : 1;
};

enum pl2303_type {
    TYPE_H,
    TYPE_HX,
    TYPE_TA,
    TYPE_TB,
    TYPE_HXD,
    TYPE_HXN,
    TYPE_COUNT
};

struct usbh_pl2303 {
    enum pl2303_type chip_type;
    uint32_t quirks;
    struct usb_endpoint_descriptor *intin;
    struct usbh_urb intin_urb;
    struct usb_osal_timer *modem_timer;
    uint16_t modem_status;
};

static const struct pl2303_type_data pl2303_type_data[TYPE_COUNT] = {
    [TYPE_H] = {
        .name = "PL2303H",
        .max_baud_rate = 1228800,
        .quirks = PL2303_QUIRK_LEGACY,
        .no_autoxonxoff = true,
    },
    [TYPE_HX] = {
        .name = "PL2303HX",
        .max_baud_rate = 6000000,
    },
    [TYPE_TA] = {
        .name = "PL2303TA",
        .max_baud_rate = 6000000,
        .alt_divisors = true,
    },
    [TYPE_TB] = {
        .name = "PL2303TB",
        .max_baud_rate = 12000000,
        .alt_divisors = true,
    },
    [TYPE_HXD] = {
        .name = "PL2303HXD",
        .max_baud_rate = 12000000,
    },
    [TYPE_HXN] = {
        .name = "PL2303G",
        .max_baud_rate = 12000000,
        .no_divisors = true,
    },
};

/*
 * Returns the nearest supported baud rate that can be set directly without
 * using divisors.
 */
static uint32_t pl2303_get_supported_baud_rate(uint32_t baud)
{
    static const uint32_t baud_sup[] = {
        75, 150, 300, 600, 1200, 1800, 2400, 3600, 4800, 7200, 9600,
        14400, 19200, 28800, 38400, 57600, 115200, 230400, 460800,
        614400, 921600, 1228800, 2457600, 3000000, 6000000
    };

    unsigned i;

    for (i = 0; i < ARRAY_SIZE(baud_sup); ++i) {
        if (baud_sup[i] > baud)
            break;
    }

    if (i == ARRAY_SIZE(baud_sup))
        baud = baud_sup[i - 1];
    else if (i > 0 && (baud_sup[i] - baud) > (baud - baud_sup[i - 1]))
        baud = baud_sup[i - 1];
    else
        baud = baud_sup[i];

    return baud;
}

/*
 * NOTE: If unsupported baud rates are set directly, the PL2303 seems to
 *       use 9600 baud.
 */
static uint32_t pl2303_encode_baud_rate_direct(unsigned char buf[4],
                                               uint32_t baud)
{
    memcpy(buf, &baud, 4);

    return baud;
}

static uint32_t pl2303_encode_baud_rate_divisor_alt(unsigned char buf[4],
                                                    uint32_t baud)
{
    unsigned int baseline, mantissa, exponent;

    /*
     * Apparently, for the TA version the formula is:
     *   baudrate = 12M * 32 / (mantissa * 2^exponent)
     * where
     *   mantissa = buf[10:0]
     *   exponent = buf[15:13 16]
     */
    baseline = 12000000 * 32;
    mantissa = baseline / baud;
    if (mantissa == 0)
        mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
    exponent = 0;
    while (mantissa >= 2048) {
        if (exponent < 15) {
            mantissa >>= 1; /* divide by 2 */
            exponent++;
        } else {
            /* Exponent is maxed. Trim mantissa and leave. */
            mantissa = 2047;
            break;
        }
    }

    buf[3] = 0x80;
    buf[2] = exponent & 0x01;
    buf[1] = (exponent & ~0x01) << 4 | mantissa >> 8;
    buf[0] = mantissa & 0xff;

    /* Calculate and return the exact baud rate. */
    baud = (baseline / mantissa) >> exponent;

    return baud;
}

static uint32_t pl2303_encode_baud_rate_divisor(unsigned char buf[4],
                                                uint32_t baud)
{
    unsigned int baseline, mantissa, exponent;

    /*
    * Apparently the formula is:
    *   baudrate = 12M * 32 / (mantissa * 4^exponent)
    * where
    *   mantissa = buf[8:0]
    *   exponent = buf[11:9]
    */
    baseline = 12000000 * 32;
    mantissa = baseline / baud;
    if (mantissa == 0)
        mantissa = 1; /* Avoid dividing by zero if baud > 32*12M. */
    exponent = 0;
    while (mantissa >= 512) {
        if (exponent < 7) {
            mantissa >>= 2; /* divide by 4 */
            exponent++;
        } else {
            /* Exponent is maxed. Trim mantissa and leave. */
            mantissa = 511;
            break;
        }
    }

    buf[3] = 0x80;
    buf[2] = 0;
    buf[1] = exponent << 1 | mantissa >> 8;
    buf[0] = mantissa & 0xff;

    /* Calculate and return the exact baud rate. */
    baud = (baseline / mantissa) >> (exponent << 1);

    return baud;
}

static int pl2303_vendor_write(struct usbh_serial *serial, uint16_t wValue, uint16_t wIndex)
{
    struct usb_setup_packet *setup;
    struct usbh_pl2303 *pl2303_class;

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }
    setup = serial->hport->setup;
    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
    setup->bRequest = pl2303_class->chip_type == TYPE_HXN ? PL2303_VENDOR_WRITE_NREQUEST : PL2303_VENDOR_WRITE_REQUEST;
    setup->wValue = wValue;
    setup->wIndex = wIndex;
    setup->wLength = 0;

    return usbh_control_transfer(serial->hport, setup, NULL);
}

static int pl2303_vendor_read(struct usbh_serial *serial, uint16_t wValue, uint8_t *data)
{
    struct usb_setup_packet *setup;
    struct usbh_pl2303 *pl2303_class;
    int ret;

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }
    setup = serial->hport->setup;
    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_VENDOR | USB_REQUEST_RECIPIENT_DEVICE;
    setup->bRequest = pl2303_class->chip_type == TYPE_HXN ? PL2303_VENDOR_READ_NREQUEST : PL2303_VENDOR_READ_REQUEST;
    setup->wValue = wValue;
    setup->wIndex = 0;
    setup->wLength = 1;

    ret = usbh_control_transfer(serial->hport, setup, serial->iobuffer);
    if (ret < 0) {
        return ret;
    }
    memcpy(data, serial->iobuffer, 1);

    return ret;
}

static bool pl2303_supports_hx_status(struct usbh_serial *serial)
{
    int ret;
    uint8_t buf;

    ret = pl2303_vendor_read(serial, PL2303_READ_TYPE_HX_STATUS, &buf);
    if (ret < 0) {
        return false;
    }

    return true;
}

static bool pl2303_is_hxd_clone(struct usbh_serial *serial)
{
    struct usb_setup_packet *setup;
    int ret;

    if (!serial || !serial->hport) {
        return -USB_ERR_INVAL;
    }
    setup = serial->hport->setup;

    setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_DEVICE;
    setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
    setup->wValue = 0;
    setup->wIndex = 0;
    setup->wLength = 7;

    ret = usbh_control_transfer(serial->hport, setup, serial->iobuffer);
    if (ret < 0) {
        return false;
    }
    return true;
}

static int pl2303_update_reg(struct usbh_serial *serial, uint8_t reg, uint8_t mask, uint8_t val)
{
    int ret;
    uint8_t buf[1];
    struct usbh_pl2303 *pl2303_class;

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }

    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    if (pl2303_class->chip_type == TYPE_HXN)
        ret = pl2303_vendor_read(serial, reg, buf);
    else
        ret = pl2303_vendor_read(serial, reg | 0x80, buf);

    if (ret < 0) {
        return ret;
    }

    *buf &= ~mask;
    *buf |= val & mask;

    return pl2303_vendor_write(serial, reg, *buf);
}

static int usbh_pl2303_get_chiptype(struct usbh_serial *serial)
{
    if (serial->hport->device_desc.bDeviceClass == 0x02) {
        return TYPE_H; /* variant 0 */
    }

    if (serial->hport->device_desc.bMaxPacketSize0 != 0x40) {
        if (serial->hport->device_desc.bDeviceClass == 0x00 || serial->hport->device_desc.bDeviceClass == 0xff)
            return TYPE_H; /* variant 1 */

        return TYPE_H; /* variant 0 */
    }

    switch (serial->hport->device_desc.bcdUSB) {
        case 0x101:
            /* USB 1.0.1? Let's assume they meant 1.1... */
        case 0x110:
            switch (serial->hport->device_desc.bcdDevice) {
                case 0x300:
                    return TYPE_HX;
                case 0x400:
                    return TYPE_HXD;
                default:
                    return TYPE_HX;
            }
            break;
        case 0x200:
            switch (serial->hport->device_desc.bcdDevice) {
                case 0x100: /* GC */
                case 0x105:
                    return TYPE_HXN;
                case 0x300: /* GT / TA */
                    if (pl2303_supports_hx_status(serial))
                        return TYPE_TA;
                    __attribute__((fallthrough));
                case 0x305:
                case 0x400: /* GL */
                case 0x405:
                    return TYPE_HXN;
                case 0x500: /* GE / TB */
                    if (pl2303_supports_hx_status(serial))
                        return TYPE_TB;
                    __attribute__((fallthrough));
                case 0x505:
                case 0x600: /* GS */
                case 0x605:
                case 0x700: /* GR */
                case 0x705:
                case 0x905:  /* GT-2AB */
                case 0x1005: /* GC-Q20 */
                    return TYPE_HXN;
            }
            break;
    }

    USB_LOG_ERR("Unsupported PL2303 Device\r\n");
    return -USB_ERR_NOTSUPP;
}

static int usbh_pl2303_attach(struct usbh_serial *serial)
{
    struct usbh_pl2303 *pl2303_class;
    struct usb_endpoint_descriptor *ep_desc;
    uint8_t type;
    int ret;

    ret = usbh_pl2303_get_chiptype(serial);
    if (ret < 0) {
        return ret;
    }

    pl2303_class = usb_osal_malloc(sizeof(struct usbh_pl2303));
    if (pl2303_class == NULL) {
        USB_LOG_ERR("Fail to alloc pl2303_class\r\n");
        return -USB_ERR_NOMEM;
    }
    memset(pl2303_class, 0, sizeof(struct usbh_pl2303));
    serial->priv = pl2303_class;

    for (uint8_t i = 0; i < serial->hport->config.intf[serial->intf].altsetting[0].intf_desc.bNumEndpoints; i++) {
        ep_desc = &serial->hport->config.intf[serial->intf].altsetting[0].ep[i].ep_desc;

        if (USB_GET_ENDPOINT_TYPE(ep_desc->bmAttributes) == USB_ENDPOINT_TYPE_INTERRUPT) {
            if (ep_desc->bEndpointAddress & 0x80) {
                USBH_EP_INIT(pl2303_class->intin, ep_desc);
                break;
            } else {
            }
        }
    }

    if (!pl2303_class->intin) {
        USB_LOG_ERR("Failed to find interrupt endpoint\r\n");
        ret = -USB_ERR_NODEV;
        goto errout;
    }

    type = (uint8_t)ret;
    pl2303_class->chip_type = type;
    pl2303_class->quirks = pl2303_type_data[pl2303_class->chip_type].quirks;

    USB_LOG_INFO("chip type: %s\r\n", pl2303_type_data[pl2303_class->chip_type].name);

    if (type == TYPE_HXD && pl2303_is_hxd_clone(serial)) {
        pl2303_class->quirks |= PL2303_QUIRK_NO_BREAK_GETLINE;
    }

    if (type != TYPE_HXN) {
        uint8_t buf[1];
        ret = pl2303_vendor_read(serial, 0x8484, buf);
        ret |= pl2303_vendor_write(serial, 0x0404, 0);
        ret |= pl2303_vendor_read(serial, 0x8484, buf);
        ret |= pl2303_vendor_read(serial, 0x8383, buf);
        ret |= pl2303_vendor_read(serial, 0x8484, buf);
        ret |= pl2303_vendor_write(serial, 0x0404, 1);
        ret |= pl2303_vendor_read(serial, 0x8484, buf);
        ret |= pl2303_vendor_read(serial, 0x8383, buf);
        ret |= pl2303_vendor_write(serial, 0, 1);
        ret |= pl2303_vendor_write(serial, 1, 0);
        if (pl2303_class->quirks & PL2303_QUIRK_LEGACY)
            ret |= pl2303_vendor_write(serial, 2, 0x24);
        else
            ret |= pl2303_vendor_write(serial, 2, 0x44);
    } else {
        ret = 0;
    }

    if (ret < 0) {
        USB_LOG_ERR("pl2303 init failed\r\n");
        goto errout;
    }

    return 0;
errout:
    serial->priv = NULL;
    usb_osal_free(pl2303_class);
    return ret;
}

static void usbh_pl2303_detach(struct usbh_serial *serial)
{
    struct usbh_pl2303 *pl2303_class;

    if (!serial || !serial->priv) {
        return;
    }

    pl2303_class = (struct usbh_pl2303 *)serial->priv;
    if (pl2303_class->intin) {
        usbh_kill_urb(&pl2303_class->intin_urb);
    }
    serial->priv = NULL;
    usb_osal_free(pl2303_class);
}

static int usbh_pl2303_set_flow_ctrl(struct usbh_serial *serial, bool hardctrl)
{
    struct usbh_pl2303 *pl2303_class;

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }

    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    if (hardctrl) {
        if (pl2303_class->quirks & PL2303_QUIRK_LEGACY) {
            return pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x40);
        } else if (pl2303_class->chip_type == TYPE_HXN) {
            return pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
                                     PL2303_HXN_FLOWCTRL_MASK,
                                     PL2303_HXN_FLOWCTRL_RTS_CTS);
        } else {
            return pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0x60);
        }
    } else {
        if (pl2303_class->chip_type == TYPE_HXN) {
            return pl2303_update_reg(serial, PL2303_HXN_FLOWCTRL_REG,
                                     PL2303_HXN_FLOWCTRL_MASK,
                                     PL2303_HXN_FLOWCTRL_NONE);
        } else {
            return pl2303_update_reg(serial, 0, PL2303_FLOWCTRL_MASK, 0);
        }
    }
}

static int usbh_pl2303_set_line_coding(struct usbh_serial *serial, struct cdc_line_coding *line_coding)
{
    struct usb_setup_packet *setup;
    struct usbh_pl2303 *pl2303_class;
    uint32_t baud;
    uint32_t baud_sup;
    uint8_t buf[7];

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }

    setup = serial->hport->setup;
    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
    setup->bRequest = CDC_REQUEST_SET_LINE_CODING;
    setup->wValue = 0;
    setup->wIndex = serial->intf;
    setup->wLength = 7;

    baud = line_coding->dwDTERate;
    if (pl2303_type_data[pl2303_class->chip_type].max_baud_rate) {
        baud = MIN(baud, pl2303_type_data[pl2303_class->chip_type].max_baud_rate);
    }
    /*
     * Use direct method for supported baud rates, otherwise use divisors.
     * Newer chip types do not support divisor encoding.
     */
    if (pl2303_type_data[pl2303_class->chip_type].no_divisors)
        baud_sup = baud;
    else
        baud_sup = pl2303_get_supported_baud_rate(baud);

    if (baud == baud_sup)
        baud = pl2303_encode_baud_rate_direct(buf, baud);
    else if (pl2303_type_data[pl2303_class->chip_type].alt_divisors)
        baud = pl2303_encode_baud_rate_divisor_alt(buf, baud);
    else
        baud = pl2303_encode_baud_rate_divisor(buf, baud);

    buf[4] = line_coding->bCharFormat;
    buf[5] = line_coding->bParityType;
    buf[6] = line_coding->bDataBits;

    memcpy(serial->iobuffer, buf, sizeof(struct cdc_line_coding));

    return usbh_control_transfer(serial->hport, setup, serial->iobuffer);
}

static int usbh_pl2303_get_line_coding(struct usbh_serial *serial, struct cdc_line_coding *line_coding)
{
    struct usb_setup_packet *setup;
    int ret;

    if (!serial || !serial->hport) {
        return -USB_ERR_INVAL;
    }
    setup = serial->hport->setup;

    setup->bmRequestType = USB_REQUEST_DIR_IN | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
    setup->bRequest = CDC_REQUEST_GET_LINE_CODING;
    setup->wValue = 0;
    setup->wIndex = serial->intf;
    setup->wLength = 7;

    ret = usbh_control_transfer(serial->hport, setup, serial->iobuffer);
    if (ret < 0) {
        return ret;
    }
    memcpy(line_coding, serial->iobuffer, sizeof(struct cdc_line_coding));
    return ret;
}

static int usbh_pl2303_set_line_state(struct usbh_serial *serial, bool dtr, bool rts)
{
    struct usb_setup_packet *setup;

    if (!serial || !serial->hport) {
        return -USB_ERR_INVAL;
    }
    setup = serial->hport->setup;

    setup->bmRequestType = USB_REQUEST_DIR_OUT | USB_REQUEST_CLASS | USB_REQUEST_RECIPIENT_INTERFACE;
    setup->bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE;
    setup->wValue = (dtr << 0) | (rts << 1);
    setup->wIndex = serial->intf;
    setup->wLength = 0;

    return usbh_control_transfer(serial->hport, setup, NULL);
}

static int usbh_pl2303_get_modem_status(struct usbh_serial *serial)
{
    struct usbh_pl2303 *pl2303_class;
    uintptr_t flags;
    uint16_t status;

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }

    flags = usb_osal_enter_critical_section();
    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    status = (pl2303_class->modem_status & UART_DSR ? USBH_SERIAL_TIOCM_DSR : 0) |
             (pl2303_class->modem_status & UART_CTS ? USBH_SERIAL_TIOCM_CTS : 0) |
             (pl2303_class->modem_status & UART_RING ? USBH_SERIAL_TIOCM_RI : 0) |
             (pl2303_class->modem_status & UART_DCD ? USBH_SERIAL_TIOCM_CD : 0) |
             (serial->line_state & USBH_SERIAL_TIOCM_DTR ? USBH_SERIAL_TIOCM_DTR : 0) |
             (serial->line_state & USBH_SERIAL_TIOCM_RTS ? USBH_SERIAL_TIOCM_RTS : 0);

    usb_osal_leave_critical_section(flags);

    return status;
}

#ifdef CONFIG_USBH_SERIAL_GET_MODEM_STATUS
static int __usbh_pl2303_get_modem_status(struct usbh_serial *serial)
{
    struct usbh_pl2303 *pl2303_class;
    uint8_t status = 0;
    uint16_t difference;
    uintptr_t flags;
    int ret;

    if (!serial || !serial->hport || !serial->priv) {
        return -USB_ERR_INVAL;
    }
    pl2303_class = (struct usbh_pl2303 *)serial->priv;

    usbh_int_urb_fill(&pl2303_class->intin_urb, serial->hport, pl2303_class->intin, &serial->iobuffer[USBH_SERIAL_INT_NOCACHE_OFFSET], pl2303_class->intin->wMaxPacketSize, 0xffffffff, NULL, NULL);
    ret = usbh_submit_urb(&pl2303_class->intin_urb);
    if (ret < 0) {
        return ret;
    }

    if (ret < 1) {
        return -USB_ERR_INVAL;
    }

    flags = usb_osal_enter_critical_section();

    status = serial->iobuffer[USBH_SERIAL_INT_NOCACHE_OFFSET];
    difference = pl2303_class->modem_status ^ status;
    pl2303_class->modem_status = status;

    if (status & UART_BREAK_ERROR)
        serial->iocount.brk++;

    if (difference & UART_STATE_MSR_MASK) {
        if (difference & UART_CTS)
            serial->iocount.cts++;
        if (difference & UART_DSR)
            serial->iocount.dsr++;
        if (difference & UART_RING)
            serial->iocount.rng++;
        if (difference & UART_DCD) {
            serial->iocount.dcd++;
        }
    }

    usb_osal_leave_critical_section(flags);

    return ret;
}
#endif

static const struct usbh_serial_driver pl2303_driver = {
    .driver_name = "pl2303",

    .ignore_rx_header = 0,
    .ignore_tx_header = 0,

    .attach = usbh_pl2303_attach,
    .detach = usbh_pl2303_detach,
    .set_flow_control = usbh_pl2303_set_flow_ctrl,
    .set_line_coding = usbh_pl2303_set_line_coding,
    .get_line_coding = usbh_pl2303_get_line_coding,
    .set_line_state = usbh_pl2303_set_line_state,
    .get_modem_status = usbh_pl2303_get_modem_status,
};

static int usbh_pl2303_connect(struct usbh_hubport *hport, uint8_t intf)
{
    return usbh_serial_probe(hport, intf, &pl2303_driver) ? 0 : -USB_ERR_NOMEM;
}

static int usbh_pl2303_disconnect(struct usbh_hubport *hport, uint8_t intf)
{
    struct usbh_serial *serial = (struct usbh_serial *)hport->config.intf[intf].priv;

    if (serial) {
        usbh_serial_remove(serial);
    }

    return 0;
}

static const uint16_t pl2303_id_table[][2] = {
    { 0x067B, 0x2303 }, // PL2303 Serial (ATEN/IOGEAR UC232A)
    { 0x067B, 0x2304 }, // PL2303HXN Serial, type TB
    { 0x067B, 0x23A3 }, // PL2303HXN Serial, type GC
    { 0x067B, 0x23B3 }, // PL2303HXN Serial, type GB
    { 0x067B, 0x23C3 }, // PL2303HXN Serial, type GT
    { 0x067B, 0x23D3 }, // PL2303HXN Serial, type GL
    { 0x067B, 0x23E3 }, // PL2303HXN Serial, type GE
    { 0x067B, 0x23F3 }, // PL2303HXN Serial, type GS
    { 0, 0 },
};

const struct usbh_class_driver pl2303_class_driver = {
    .driver_name = "pl2303",
    .connect = usbh_pl2303_connect,
    .disconnect = usbh_pl2303_disconnect
};

CLASS_INFO_DEFINE const struct usbh_class_info pl2303_class_info = {
    .match_flags = USB_CLASS_MATCH_VID_PID | USB_CLASS_MATCH_INTF_CLASS,
    .bInterfaceClass = 0xff,
    .bInterfaceSubClass = 0x00,
    .bInterfaceProtocol = 0x00,
    .id_table = pl2303_id_table,
    .class_driver = &pl2303_class_driver
};