.. | .. |
---|
| 1 | +/* |
---|
| 2 | + * USB serial driver for USB to UART(s) chip ch342/ch343/ch344/ch347/ch9101/ch9102/ch9103/ch9104, etc. |
---|
| 3 | + * |
---|
| 4 | + * Copyright (C) 2022 Nanjing Qinheng Microelectronics Co., Ltd. |
---|
| 5 | + * Web: http://wch.cn |
---|
| 6 | + * Author: WCH <tech@wch.cn> |
---|
| 7 | + * |
---|
| 8 | + * This program is free software; you can redistribute it and/or modify |
---|
| 9 | + * it under the terms of the GNU General Public License as published by |
---|
| 10 | + * the Free Software Foundation; either version 2 of the License, or |
---|
| 11 | + * (at your option) any later version. |
---|
| 12 | + * |
---|
| 13 | + * System required: |
---|
| 14 | + * Kernel version beyond 3.4.x |
---|
| 15 | + * Update Log: |
---|
| 16 | + * V1.0 - initial version |
---|
| 17 | + * V1.1 - added support of chip ch344, ch9101 and ch9103 |
---|
| 18 | + * V1.2 - added gpio support of chip ch344 |
---|
| 19 | + * V1.3 - added support of chip ch347 |
---|
| 20 | + * V1.4 - added support of chip ch9104 |
---|
| 21 | + * V1.5 - added gpio character device |
---|
| 22 | + * - added supports for kernel version beyond 5.14.x |
---|
| 23 | + * - removed the gpio ioctl commands |
---|
| 24 | + */ |
---|
| 25 | + |
---|
| 26 | +#define DEBUG |
---|
| 27 | +#define VERBOSE_DEBUG |
---|
| 28 | + |
---|
| 29 | +#undef DEBUG |
---|
| 30 | +#undef VERBOSE_DEBUG |
---|
| 31 | + |
---|
| 32 | +#include <asm/byteorder.h> |
---|
| 33 | +#include <asm/unaligned.h> |
---|
| 34 | +#include <linux/errno.h> |
---|
| 35 | +#include <linux/idr.h> |
---|
| 36 | +#include <linux/init.h> |
---|
| 37 | +#include <linux/kernel.h> |
---|
| 38 | +#include <linux/list.h> |
---|
| 39 | +#include <linux/module.h> |
---|
| 40 | +#include <linux/mutex.h> |
---|
| 41 | +#include <linux/serial.h> |
---|
| 42 | +#include <linux/slab.h> |
---|
| 43 | +#include <linux/tty.h> |
---|
| 44 | +#include <linux/tty_driver.h> |
---|
| 45 | +#include <linux/tty_flip.h> |
---|
| 46 | +#include <linux/uaccess.h> |
---|
| 47 | +#include <linux/usb.h> |
---|
| 48 | +#include <linux/usb/cdc.h> |
---|
| 49 | +#include <linux/version.h> |
---|
| 50 | + |
---|
| 51 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0)) |
---|
| 52 | +#include <linux/sched/signal.h> |
---|
| 53 | +#endif |
---|
| 54 | + |
---|
| 55 | +#include "ch343.h" |
---|
| 56 | + |
---|
| 57 | +#define DRIVER_AUTHOR "WCH" |
---|
| 58 | +#define DRIVER_DESC "USB serial driver for ch342/ch343/ch344/ch347/ch9101/ch9102/ch9103/ch9104, etc." |
---|
| 59 | +#define VERSION_DESC "V1.5 On 2022.12" |
---|
| 60 | + |
---|
| 61 | +#define IOCTL_MAGIC 'W' |
---|
| 62 | +#define IOCTL_CMD_GETCHIPTYPE _IOR(IOCTL_MAGIC, 0x84, u16) |
---|
| 63 | +#define IOCTL_CMD_CTRLIN _IOWR(IOCTL_MAGIC, 0x90, u16) |
---|
| 64 | +#define IOCTL_CMD_CTRLOUT _IOW(IOCTL_MAGIC, 0x91, u16) |
---|
| 65 | +#define IOCTL_CMD_GICOUNT _IOR(IOCTL_MAGIC, 0x92, u16) |
---|
| 66 | + |
---|
| 67 | +static struct usb_driver ch343_driver; |
---|
| 68 | +static struct tty_driver *ch343_tty_driver; |
---|
| 69 | +static struct usb_interface *g_intf; |
---|
| 70 | + |
---|
| 71 | +static DEFINE_IDR(ch343_minors); |
---|
| 72 | +static DEFINE_MUTEX(ch343_minors_lock); |
---|
| 73 | + |
---|
| 74 | +static void ch343_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old); |
---|
| 75 | + |
---|
| 76 | +/* |
---|
| 77 | + * Look up an ch343 structure by minor. If found and not disconnected, increment |
---|
| 78 | + * its refcount and return it with its mutex held. |
---|
| 79 | + */ |
---|
| 80 | +static struct ch343 *ch343_get_by_minor(unsigned int minor) |
---|
| 81 | +{ |
---|
| 82 | + struct ch343 *ch343; |
---|
| 83 | + |
---|
| 84 | + mutex_lock(&ch343_minors_lock); |
---|
| 85 | + ch343 = idr_find(&ch343_minors, minor); |
---|
| 86 | + if (ch343) { |
---|
| 87 | + mutex_lock(&ch343->mutex); |
---|
| 88 | + if (ch343->disconnected) { |
---|
| 89 | + mutex_unlock(&ch343->mutex); |
---|
| 90 | + ch343 = NULL; |
---|
| 91 | + } else { |
---|
| 92 | + tty_port_get(&ch343->port); |
---|
| 93 | + mutex_unlock(&ch343->mutex); |
---|
| 94 | + } |
---|
| 95 | + } |
---|
| 96 | + mutex_unlock(&ch343_minors_lock); |
---|
| 97 | + return ch343; |
---|
| 98 | +} |
---|
| 99 | + |
---|
| 100 | +/* |
---|
| 101 | + * Try to find an available minor number and if found, associate it with 'ch343'. |
---|
| 102 | + */ |
---|
| 103 | +static int ch343_alloc_minor(struct ch343 *ch343) |
---|
| 104 | +{ |
---|
| 105 | + int minor; |
---|
| 106 | + |
---|
| 107 | + mutex_lock(&ch343_minors_lock); |
---|
| 108 | + minor = idr_alloc(&ch343_minors, ch343, 0, CH343_TTY_MINORS, GFP_KERNEL); |
---|
| 109 | + mutex_unlock(&ch343_minors_lock); |
---|
| 110 | + |
---|
| 111 | + return minor; |
---|
| 112 | +} |
---|
| 113 | + |
---|
| 114 | +/* Release the minor number associated with 'ch343'. */ |
---|
| 115 | +static void ch343_release_minor(struct ch343 *ch343) |
---|
| 116 | +{ |
---|
| 117 | + mutex_lock(&ch343_minors_lock); |
---|
| 118 | + idr_remove(&ch343_minors, ch343->minor); |
---|
| 119 | + mutex_unlock(&ch343_minors_lock); |
---|
| 120 | +} |
---|
| 121 | + |
---|
| 122 | +/* |
---|
| 123 | + * Functions for CH343 control messages. |
---|
| 124 | + */ |
---|
| 125 | +static int ch343_control_out(struct ch343 *ch343, u8 request, u16 value, u16 index) |
---|
| 126 | +{ |
---|
| 127 | + int retval; |
---|
| 128 | + |
---|
| 129 | + retval = usb_autopm_get_interface(ch343->control); |
---|
| 130 | + if (retval) |
---|
| 131 | + return retval; |
---|
| 132 | + |
---|
| 133 | + retval = usb_control_msg(ch343->dev, usb_sndctrlpipe(ch343->dev, 0), request, |
---|
| 134 | + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_OUT, value, index, NULL, 0, DEFAULT_TIMEOUT); |
---|
| 135 | + |
---|
| 136 | + usb_autopm_put_interface(ch343->control); |
---|
| 137 | + |
---|
| 138 | + return retval; |
---|
| 139 | +} |
---|
| 140 | + |
---|
| 141 | +static int ch343_control_in(struct ch343 *ch343, u8 request, u16 value, u16 index, char *buf, unsigned bufsize) |
---|
| 142 | +{ |
---|
| 143 | + int retval; |
---|
| 144 | + |
---|
| 145 | + retval = usb_autopm_get_interface(ch343->control); |
---|
| 146 | + if (retval) |
---|
| 147 | + return retval; |
---|
| 148 | + |
---|
| 149 | + retval = |
---|
| 150 | + usb_control_msg(ch343->dev, usb_rcvctrlpipe(ch343->dev, 0), request, |
---|
| 151 | + USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_DIR_IN, value, index, buf, bufsize, DEFAULT_TIMEOUT); |
---|
| 152 | + |
---|
| 153 | + usb_autopm_put_interface(ch343->control); |
---|
| 154 | + |
---|
| 155 | + return retval; |
---|
| 156 | +} |
---|
| 157 | + |
---|
| 158 | +static int ch343_control_msg_out(struct ch343 *ch343, u8 request, u8 requesttype, u16 value, u16 index, void *buf, |
---|
| 159 | + unsigned bufsize) |
---|
| 160 | +{ |
---|
| 161 | + int retval; |
---|
| 162 | + char *buffer; |
---|
| 163 | + |
---|
| 164 | + buffer = kmalloc(bufsize, GFP_KERNEL); |
---|
| 165 | + if (!buffer) |
---|
| 166 | + return -ENOMEM; |
---|
| 167 | + |
---|
| 168 | + retval = copy_from_user(buffer, (char __user *)buf, bufsize); |
---|
| 169 | + if (retval) |
---|
| 170 | + goto out; |
---|
| 171 | + |
---|
| 172 | + retval = usb_autopm_get_interface(ch343->control); |
---|
| 173 | + if (retval) |
---|
| 174 | + goto out; |
---|
| 175 | + |
---|
| 176 | + retval = usb_control_msg(ch343->dev, usb_sndctrlpipe(ch343->dev, 0), request, requesttype, value, index, buf, |
---|
| 177 | + bufsize, DEFAULT_TIMEOUT); |
---|
| 178 | + |
---|
| 179 | + usb_autopm_put_interface(ch343->control); |
---|
| 180 | + |
---|
| 181 | +out: |
---|
| 182 | + kfree(buffer); |
---|
| 183 | + return retval; |
---|
| 184 | +} |
---|
| 185 | + |
---|
| 186 | +static int ch343_control_msg_in(struct ch343 *ch343, u8 request, u8 requesttype, u16 value, u16 index, void *buf, |
---|
| 187 | + unsigned bufsize) |
---|
| 188 | +{ |
---|
| 189 | + int retval; |
---|
| 190 | + char *buffer; |
---|
| 191 | + |
---|
| 192 | + buffer = kmalloc(bufsize, GFP_KERNEL); |
---|
| 193 | + if (!buffer) |
---|
| 194 | + return -ENOMEM; |
---|
| 195 | + |
---|
| 196 | + retval = usb_autopm_get_interface(ch343->control); |
---|
| 197 | + if (retval) |
---|
| 198 | + goto out; |
---|
| 199 | + |
---|
| 200 | + retval = usb_control_msg(ch343->dev, usb_rcvctrlpipe(ch343->dev, 0), request, requesttype, value, index, buffer, |
---|
| 201 | + bufsize, DEFAULT_TIMEOUT); |
---|
| 202 | + if (retval > 0) { |
---|
| 203 | + if (copy_to_user((char __user *)buf, buffer, retval)) { |
---|
| 204 | + retval = -EFAULT; |
---|
| 205 | + } |
---|
| 206 | + } |
---|
| 207 | + |
---|
| 208 | + usb_autopm_put_interface(ch343->control); |
---|
| 209 | + |
---|
| 210 | +out: |
---|
| 211 | + kfree(buffer); |
---|
| 212 | + return retval; |
---|
| 213 | +} |
---|
| 214 | + |
---|
| 215 | +static inline int ch343_set_control(struct ch343 *ch343, int control) |
---|
| 216 | +{ |
---|
| 217 | + if (ch343->iface <= 1) |
---|
| 218 | + return ch343_control_out(ch343, CMD_C2 + ch343->iface, ~control, 0x0000); |
---|
| 219 | + else if (ch343->iface <= 3) |
---|
| 220 | + return ch343_control_out(ch343, CMD_C2 + 0x10 + (ch343->iface - 2), ~control, 0x0000); |
---|
| 221 | + else |
---|
| 222 | + return -1; |
---|
| 223 | +} |
---|
| 224 | + |
---|
| 225 | +static inline int ch343_set_line(struct ch343 *ch343, struct usb_cdc_line_coding *line) |
---|
| 226 | +{ |
---|
| 227 | + return 0; |
---|
| 228 | +} |
---|
| 229 | + |
---|
| 230 | +static int ch343_get_status(struct ch343 *ch343) |
---|
| 231 | +{ |
---|
| 232 | + char *buffer; |
---|
| 233 | + int retval; |
---|
| 234 | + const unsigned size = 2; |
---|
| 235 | + unsigned long flags; |
---|
| 236 | + |
---|
| 237 | + buffer = kmalloc(size, GFP_KERNEL); |
---|
| 238 | + if (!buffer) |
---|
| 239 | + return -ENOMEM; |
---|
| 240 | + |
---|
| 241 | + retval = ch343_control_in(ch343, CMD_R, CMD_C3 + ch343->iface, 0, buffer, size); |
---|
| 242 | + if (retval != size) |
---|
| 243 | + goto out; |
---|
| 244 | + |
---|
| 245 | + /* setup the private status if available */ |
---|
| 246 | + spin_lock_irqsave(&ch343->read_lock, flags); |
---|
| 247 | + ch343->ctrlin = (~(*buffer)) & CH343_CTI_ST; |
---|
| 248 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 249 | + |
---|
| 250 | +out: |
---|
| 251 | + kfree(buffer); |
---|
| 252 | + return retval; |
---|
| 253 | +} |
---|
| 254 | + |
---|
| 255 | +/* -------------------------------------------------------------------------- */ |
---|
| 256 | + |
---|
| 257 | +static int ch343_configure(struct ch343 *ch343) |
---|
| 258 | +{ |
---|
| 259 | + char *buffer; |
---|
| 260 | + int r; |
---|
| 261 | + const unsigned size = 2; |
---|
| 262 | + u8 chiptype; |
---|
| 263 | + |
---|
| 264 | + buffer = kmalloc(size, GFP_KERNEL); |
---|
| 265 | + if (!buffer) |
---|
| 266 | + return -ENOMEM; |
---|
| 267 | + |
---|
| 268 | + r = ch343_control_in(ch343, CMD_C6, 0, 0, buffer, size); |
---|
| 269 | + if (r != size) |
---|
| 270 | + goto out; |
---|
| 271 | + |
---|
| 272 | + chiptype = buffer[1]; |
---|
| 273 | + |
---|
| 274 | + switch (ch343->idProduct) { |
---|
| 275 | + case 0x55D2: |
---|
| 276 | + if (chiptype == 0x48) |
---|
| 277 | + ch343->chiptype = CHIP_CH342F; |
---|
| 278 | + else if (chiptype == 0x41) |
---|
| 279 | + ch343->chiptype = CHIP_CH342K; |
---|
| 280 | + break; |
---|
| 281 | + case 0x55D3: |
---|
| 282 | + if (chiptype == 0x08) |
---|
| 283 | + ch343->chiptype = CHIP_CH343GP; |
---|
| 284 | + else if (chiptype == 0x02) |
---|
| 285 | + ch343->chiptype = CHIP_CH343J; |
---|
| 286 | + else if (chiptype == 0x01) |
---|
| 287 | + ch343->chiptype = CHIP_CH343K; |
---|
| 288 | + else if (chiptype == 0x18) |
---|
| 289 | + ch343->chiptype = CHIP_CH343G_AUTOBAUD; |
---|
| 290 | + break; |
---|
| 291 | + case 0x55D4: |
---|
| 292 | + if (chiptype == 0x08) |
---|
| 293 | + ch343->chiptype = CHIP_CH9102F; |
---|
| 294 | + else if (chiptype == 0x09) |
---|
| 295 | + ch343->chiptype = CHIP_CH9102X; |
---|
| 296 | + break; |
---|
| 297 | + case 0x55D5: |
---|
| 298 | + if (chiptype == 0xC0) { |
---|
| 299 | + if ((buffer[0] & 0xF0) == 0x40) |
---|
| 300 | + ch343->chiptype = CHIP_CH344L; |
---|
| 301 | + else |
---|
| 302 | + ch343->chiptype = CHIP_CH344L_V2; |
---|
| 303 | + } else |
---|
| 304 | + ch343->chiptype = CHIP_CH344Q; |
---|
| 305 | + break; |
---|
| 306 | + case 0x55D7: |
---|
| 307 | + if (chiptype == 0x4B) |
---|
| 308 | + ch343->chiptype = CHIP_CH9103M; |
---|
| 309 | + break; |
---|
| 310 | + case 0x55D8: |
---|
| 311 | + if (chiptype == 0x08) |
---|
| 312 | + ch343->chiptype = CHIP_CH9101UH; |
---|
| 313 | + else if (chiptype == 0x0A) |
---|
| 314 | + ch343->chiptype = CHIP_CH9101RY; |
---|
| 315 | + break; |
---|
| 316 | + case 0x55DA: |
---|
| 317 | + case 0x55DB: |
---|
| 318 | + case 0x55DD: |
---|
| 319 | + ch343->chiptype = CHIP_CH347T; |
---|
| 320 | + break; |
---|
| 321 | + case 0x55DF: |
---|
| 322 | + ch343->chiptype = CHIP_CH9104L; |
---|
| 323 | + break; |
---|
| 324 | + default: |
---|
| 325 | + break; |
---|
| 326 | + } |
---|
| 327 | + |
---|
| 328 | + if (ch343->chiptype != CHIP_CH344L && ch343->chiptype != CHIP_CH344L_V2 && ch343->chiptype != CHIP_CH9104L) { |
---|
| 329 | + r = ch343_get_status(ch343); |
---|
| 330 | + if (r < 0) |
---|
| 331 | + goto out; |
---|
| 332 | + } |
---|
| 333 | + |
---|
| 334 | + dev_dbg(&ch343->data->dev, "%s - chip hver : 0x%2x, sver : 0x%2x, chip : %d\n", __func__, buffer[0], buffer[1], |
---|
| 335 | + ch343->chiptype); |
---|
| 336 | +out: |
---|
| 337 | + kfree(buffer); |
---|
| 338 | + return r < 0 ? r : 0; |
---|
| 339 | +} |
---|
| 340 | + |
---|
| 341 | +/* |
---|
| 342 | + * Write buffer management. |
---|
| 343 | + * All of these assume proper locks taken by the caller. |
---|
| 344 | + */ |
---|
| 345 | +static int ch343_wb_alloc(struct ch343 *ch343) |
---|
| 346 | +{ |
---|
| 347 | + int i, wbn; |
---|
| 348 | + struct ch343_wb *wb; |
---|
| 349 | + |
---|
| 350 | + wbn = 0; |
---|
| 351 | + i = 0; |
---|
| 352 | + for (;;) { |
---|
| 353 | + wb = &ch343->wb[wbn]; |
---|
| 354 | + if (!wb->use) { |
---|
| 355 | + wb->use = 1; |
---|
| 356 | + return wbn; |
---|
| 357 | + } |
---|
| 358 | + wbn = (wbn + 1) % CH343_NW; |
---|
| 359 | + if (++i >= CH343_NW) |
---|
| 360 | + return -1; |
---|
| 361 | + } |
---|
| 362 | +} |
---|
| 363 | + |
---|
| 364 | +static int ch343_wb_is_avail(struct ch343 *ch343) |
---|
| 365 | +{ |
---|
| 366 | + int i, n; |
---|
| 367 | + unsigned long flags; |
---|
| 368 | + |
---|
| 369 | + n = CH343_NW; |
---|
| 370 | + spin_lock_irqsave(&ch343->write_lock, flags); |
---|
| 371 | + for (i = 0; i < CH343_NW; i++) |
---|
| 372 | + n -= ch343->wb[i].use; |
---|
| 373 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 374 | + return n; |
---|
| 375 | +} |
---|
| 376 | + |
---|
| 377 | +/* |
---|
| 378 | + * Finish write. Caller must hold ch343->write_lock |
---|
| 379 | + */ |
---|
| 380 | +static void ch343_write_done(struct ch343 *ch343, struct ch343_wb *wb) |
---|
| 381 | +{ |
---|
| 382 | + wb->use = 0; |
---|
| 383 | + ch343->transmitting--; |
---|
| 384 | + usb_autopm_put_interface_async(ch343->control); |
---|
| 385 | +} |
---|
| 386 | + |
---|
| 387 | +/* |
---|
| 388 | + * Poke write. |
---|
| 389 | + * |
---|
| 390 | + * the caller is responsible for locking |
---|
| 391 | + */ |
---|
| 392 | +static int ch343_start_wb(struct ch343 *ch343, struct ch343_wb *wb) |
---|
| 393 | +{ |
---|
| 394 | + int rc; |
---|
| 395 | + |
---|
| 396 | + ch343->transmitting++; |
---|
| 397 | + |
---|
| 398 | + wb->urb->transfer_buffer = wb->buf; |
---|
| 399 | + wb->urb->transfer_dma = wb->dmah; |
---|
| 400 | + wb->urb->transfer_buffer_length = wb->len; |
---|
| 401 | + wb->urb->dev = ch343->dev; |
---|
| 402 | + |
---|
| 403 | + rc = usb_submit_urb(wb->urb, GFP_ATOMIC); |
---|
| 404 | + if (rc < 0) { |
---|
| 405 | + dev_err(&ch343->data->dev, "%s - usb_submit_urb(write bulk) failed: %d\n", __func__, rc); |
---|
| 406 | + ch343_write_done(ch343, wb); |
---|
| 407 | + } |
---|
| 408 | + return rc; |
---|
| 409 | +} |
---|
| 410 | + |
---|
| 411 | +static void ch343_update_status(struct ch343 *ch343, unsigned char *data, size_t len) |
---|
| 412 | +{ |
---|
| 413 | + unsigned long flags; |
---|
| 414 | + u8 status; |
---|
| 415 | + u8 difference; |
---|
| 416 | + u8 type = data[0]; |
---|
| 417 | + u8 handled = 0; |
---|
| 418 | + |
---|
| 419 | + if (len < 4) |
---|
| 420 | + return; |
---|
| 421 | + |
---|
| 422 | + if (ch343->chiptype == CHIP_CH344L) { |
---|
| 423 | + if (data[0] != 0x00) |
---|
| 424 | + return; |
---|
| 425 | + type = data[1]; |
---|
| 426 | + } else if (ch343->chiptype == CHIP_CH344Q || ch343->chiptype == CHIP_CH344L_V2 || ch343->chiptype == CHIP_CH9104L) { |
---|
| 427 | + type = data[1]; |
---|
| 428 | + } |
---|
| 429 | + |
---|
| 430 | + if (type & CH343_CTT_M) { |
---|
| 431 | + status = ~data[len - 1] & CH343_CTI_ST; |
---|
| 432 | + if (ch343->chiptype == CHIP_CH344L || ch343->chiptype == CHIP_CH344L_V2) |
---|
| 433 | + status &= CH343_CTI_C; |
---|
| 434 | + |
---|
| 435 | + if (!ch343->clocal && (ch343->ctrlin & status & CH343_CTI_DC)) { |
---|
| 436 | + tty_port_tty_hangup(&ch343->port, false); |
---|
| 437 | + } |
---|
| 438 | + |
---|
| 439 | + spin_lock_irqsave(&ch343->read_lock, flags); |
---|
| 440 | + difference = status ^ ch343->ctrlin; |
---|
| 441 | + ch343->ctrlin = status; |
---|
| 442 | + ch343->oldcount = ch343->iocount; |
---|
| 443 | + |
---|
| 444 | + if (difference) { |
---|
| 445 | + if (difference & CH343_CTI_C) { |
---|
| 446 | + ch343->iocount.cts++; |
---|
| 447 | + } |
---|
| 448 | + if (difference & CH343_CTI_DS) { |
---|
| 449 | + ch343->iocount.dsr++; |
---|
| 450 | + } |
---|
| 451 | + if (difference & CH343_CTI_R) { |
---|
| 452 | + ch343->iocount.rng++; |
---|
| 453 | + } |
---|
| 454 | + if (difference & CH343_CTI_DC) { |
---|
| 455 | + ch343->iocount.dcd++; |
---|
| 456 | + } |
---|
| 457 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 458 | + wake_up_interruptible(&ch343->wioctl); |
---|
| 459 | + } else |
---|
| 460 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 461 | + handled = 1; |
---|
| 462 | + } |
---|
| 463 | + if (type & CH343_CTT_O) { |
---|
| 464 | + spin_lock_irqsave(&ch343->read_lock, flags); |
---|
| 465 | + ch343->oldcount = ch343->iocount; |
---|
| 466 | + ch343->iocount.overrun++; |
---|
| 467 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 468 | + handled = 1; |
---|
| 469 | + } |
---|
| 470 | + if ((type & CH343_CTT_F) == CH343_CTT_F) { |
---|
| 471 | + spin_lock_irqsave(&ch343->read_lock, flags); |
---|
| 472 | + ch343->oldcount = ch343->iocount; |
---|
| 473 | + ch343->iocount.frame++; |
---|
| 474 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 475 | + handled = 1; |
---|
| 476 | + } else if (type & CH343_CTT_P) { |
---|
| 477 | + spin_lock_irqsave(&ch343->read_lock, flags); |
---|
| 478 | + ch343->oldcount = ch343->iocount; |
---|
| 479 | + ch343->iocount.parity++; |
---|
| 480 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 481 | + handled = 1; |
---|
| 482 | + } |
---|
| 483 | + if (!handled) |
---|
| 484 | + dev_err(&ch343->control->dev, |
---|
| 485 | + "%s - unknown status received:" |
---|
| 486 | + "len:%d, data0:0x%x, data1:0x%x\n", |
---|
| 487 | + __func__, (int)len, data[0], data[1]); |
---|
| 488 | +} |
---|
| 489 | + |
---|
| 490 | +/* Reports status changes with "interrupt" transfers */ |
---|
| 491 | +static void ch343_ctrl_irq(struct urb *urb) |
---|
| 492 | +{ |
---|
| 493 | + struct ch343 *ch343 = urb->context; |
---|
| 494 | + unsigned char *data = urb->transfer_buffer; |
---|
| 495 | + unsigned int len = urb->actual_length; |
---|
| 496 | + int status = urb->status; |
---|
| 497 | + int retval; |
---|
| 498 | + |
---|
| 499 | + switch (status) { |
---|
| 500 | + case 0: |
---|
| 501 | + /* success */ |
---|
| 502 | + break; |
---|
| 503 | + case -ECONNRESET: |
---|
| 504 | + case -ENOENT: |
---|
| 505 | + case -ESHUTDOWN: |
---|
| 506 | + /* this urb is terminated, clean up */ |
---|
| 507 | + dev_dbg(&ch343->control->dev, "%s - urb shutting down with status: %d\n", __func__, status); |
---|
| 508 | + return; |
---|
| 509 | + default: |
---|
| 510 | + dev_dbg(&ch343->control->dev, "%s - nonzero urb status received: %d\n", __func__, status); |
---|
| 511 | + goto exit; |
---|
| 512 | + } |
---|
| 513 | + |
---|
| 514 | + usb_mark_last_busy(ch343->dev); |
---|
| 515 | + ch343_update_status(ch343, data, len); |
---|
| 516 | +exit: |
---|
| 517 | + retval = usb_submit_urb(urb, GFP_ATOMIC); |
---|
| 518 | + if (retval && retval != -EPERM) |
---|
| 519 | + dev_err(&ch343->control->dev, "%s - usb_submit_urb failed: %d\n", __func__, retval); |
---|
| 520 | +} |
---|
| 521 | + |
---|
| 522 | +static int ch343_submit_read_urb(struct ch343 *ch343, int index, gfp_t mem_flags) |
---|
| 523 | +{ |
---|
| 524 | + int res; |
---|
| 525 | + |
---|
| 526 | + if (!test_and_clear_bit(index, &ch343->read_urbs_free)) |
---|
| 527 | + return 0; |
---|
| 528 | + |
---|
| 529 | + dev_vdbg(&ch343->data->dev, "%s - urb %d\n", __func__, index); |
---|
| 530 | + |
---|
| 531 | + res = usb_submit_urb(ch343->read_urbs[index], mem_flags); |
---|
| 532 | + if (res) { |
---|
| 533 | + if (res != -EPERM) { |
---|
| 534 | + dev_err(&ch343->data->dev, "%s - usb_submit_urb failed: %d\n", __func__, res); |
---|
| 535 | + } |
---|
| 536 | + set_bit(index, &ch343->read_urbs_free); |
---|
| 537 | + return res; |
---|
| 538 | + } |
---|
| 539 | + |
---|
| 540 | + return 0; |
---|
| 541 | +} |
---|
| 542 | + |
---|
| 543 | +static int ch343_submit_read_urbs(struct ch343 *ch343, gfp_t mem_flags) |
---|
| 544 | +{ |
---|
| 545 | + int res; |
---|
| 546 | + int i; |
---|
| 547 | + |
---|
| 548 | + for (i = 0; i < ch343->rx_buflimit; ++i) { |
---|
| 549 | + res = ch343_submit_read_urb(ch343, i, mem_flags); |
---|
| 550 | + if (res) |
---|
| 551 | + return res; |
---|
| 552 | + } |
---|
| 553 | + |
---|
| 554 | + return 0; |
---|
| 555 | +} |
---|
| 556 | + |
---|
| 557 | +static void ch343_process_read_urb(struct ch343 *ch343, struct urb *urb) |
---|
| 558 | +{ |
---|
| 559 | + if (!urb->actual_length) |
---|
| 560 | + return; |
---|
| 561 | + |
---|
| 562 | + tty_insert_flip_string(&ch343->port, urb->transfer_buffer, urb->actual_length); |
---|
| 563 | + tty_flip_buffer_push(&ch343->port); |
---|
| 564 | +} |
---|
| 565 | + |
---|
| 566 | +static void ch343_read_bulk_callback(struct urb *urb) |
---|
| 567 | +{ |
---|
| 568 | + struct ch343_rb *rb = urb->context; |
---|
| 569 | + struct ch343 *ch343 = rb->instance; |
---|
| 570 | + int status = urb->status; |
---|
| 571 | + |
---|
| 572 | + dev_vdbg(&ch343->data->dev, "%s - urb %d, len %d\n", __func__, rb->index, urb->actual_length); |
---|
| 573 | + |
---|
| 574 | + if (!ch343->dev) { |
---|
| 575 | + set_bit(rb->index, &ch343->read_urbs_free); |
---|
| 576 | + dev_dbg(&ch343->data->dev, "%s - disconnected\n", __func__); |
---|
| 577 | + return; |
---|
| 578 | + } |
---|
| 579 | + |
---|
| 580 | + if (status) { |
---|
| 581 | + set_bit(rb->index, &ch343->read_urbs_free); |
---|
| 582 | + dev_dbg(&ch343->data->dev, "%s - non-zero urb status: %d\n", __func__, status); |
---|
| 583 | + return; |
---|
| 584 | + } |
---|
| 585 | + |
---|
| 586 | + usb_mark_last_busy(ch343->dev); |
---|
| 587 | + ch343_process_read_urb(ch343, urb); |
---|
| 588 | + set_bit(rb->index, &ch343->read_urbs_free); |
---|
| 589 | + ch343_submit_read_urb(ch343, rb->index, GFP_ATOMIC); |
---|
| 590 | +} |
---|
| 591 | + |
---|
| 592 | +/* data interface wrote those outgoing bytes */ |
---|
| 593 | +static void ch343_write_bulk(struct urb *urb) |
---|
| 594 | +{ |
---|
| 595 | + struct ch343_wb *wb = urb->context; |
---|
| 596 | + struct ch343 *ch343 = wb->instance; |
---|
| 597 | + unsigned long flags; |
---|
| 598 | + int status = urb->status; |
---|
| 599 | + |
---|
| 600 | + dev_vdbg(&ch343->data->dev, "%s, len %d\n", __func__, urb->actual_length); |
---|
| 601 | + if (status || (urb->actual_length != urb->transfer_buffer_length)) |
---|
| 602 | + dev_vdbg(&ch343->data->dev, "%s - len %d/%d, status %d\n", __func__, urb->actual_length, |
---|
| 603 | + urb->transfer_buffer_length, status); |
---|
| 604 | + |
---|
| 605 | + spin_lock_irqsave(&ch343->write_lock, flags); |
---|
| 606 | + ch343_write_done(ch343, wb); |
---|
| 607 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 608 | + schedule_work(&ch343->work); |
---|
| 609 | +} |
---|
| 610 | + |
---|
| 611 | +static void ch343_softint(struct work_struct *work) |
---|
| 612 | +{ |
---|
| 613 | + struct ch343 *ch343 = container_of(work, struct ch343, work); |
---|
| 614 | + |
---|
| 615 | + dev_dbg(&ch343->data->dev, "%s\n", __func__); |
---|
| 616 | + |
---|
| 617 | + tty_port_tty_wakeup(&ch343->port); |
---|
| 618 | +} |
---|
| 619 | + |
---|
| 620 | +/* |
---|
| 621 | + * TTY handlers |
---|
| 622 | + */ |
---|
| 623 | +static int ch343_tty_install(struct tty_driver *driver, struct tty_struct *tty) |
---|
| 624 | +{ |
---|
| 625 | + struct ch343 *ch343; |
---|
| 626 | + int retval; |
---|
| 627 | + |
---|
| 628 | + dev_dbg(tty->dev, "%s\n", __func__); |
---|
| 629 | + |
---|
| 630 | + ch343 = ch343_get_by_minor(tty->index); |
---|
| 631 | + if (!ch343) |
---|
| 632 | + return -ENODEV; |
---|
| 633 | + |
---|
| 634 | + retval = tty_standard_install(driver, tty); |
---|
| 635 | + if (retval) |
---|
| 636 | + goto error_init_termios; |
---|
| 637 | + |
---|
| 638 | + tty->driver_data = ch343; |
---|
| 639 | + |
---|
| 640 | + return 0; |
---|
| 641 | + |
---|
| 642 | +error_init_termios: |
---|
| 643 | + tty_port_put(&ch343->port); |
---|
| 644 | + return retval; |
---|
| 645 | +} |
---|
| 646 | + |
---|
| 647 | +static int ch343_tty_open(struct tty_struct *tty, struct file *filp) |
---|
| 648 | +{ |
---|
| 649 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 650 | + |
---|
| 651 | + dev_dbg(tty->dev, "%s\n", __func__); |
---|
| 652 | + |
---|
| 653 | + return tty_port_open(&ch343->port, tty, filp); |
---|
| 654 | +} |
---|
| 655 | + |
---|
| 656 | +static void ch343_port_dtr_rts(struct tty_port *port, int raise) |
---|
| 657 | +{ |
---|
| 658 | + struct ch343 *ch343 = container_of(port, struct ch343, port); |
---|
| 659 | + int res; |
---|
| 660 | + |
---|
| 661 | + dev_dbg(&ch343->data->dev, "%s, raise:%d\n", __func__, raise); |
---|
| 662 | + |
---|
| 663 | + if (raise) |
---|
| 664 | + ch343->ctrlout |= CH343_CTO_D | CH343_CTO_R; |
---|
| 665 | + else |
---|
| 666 | + ch343->ctrlout &= ~(CH343_CTO_D | CH343_CTO_R); |
---|
| 667 | + |
---|
| 668 | + res = ch343_set_control(ch343, ch343->ctrlout); |
---|
| 669 | + if (res) |
---|
| 670 | + dev_err(&ch343->control->dev, "failed to set dtr/rts\n"); |
---|
| 671 | +} |
---|
| 672 | + |
---|
| 673 | +static int ch343_port_activate(struct tty_port *port, struct tty_struct *tty) |
---|
| 674 | +{ |
---|
| 675 | + struct ch343 *ch343 = container_of(port, struct ch343, port); |
---|
| 676 | + int retval = -ENODEV; |
---|
| 677 | + int i; |
---|
| 678 | + |
---|
| 679 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 680 | + |
---|
| 681 | + mutex_lock(&ch343->mutex); |
---|
| 682 | + if (ch343->disconnected) |
---|
| 683 | + goto disconnected; |
---|
| 684 | + |
---|
| 685 | + retval = usb_autopm_get_interface(ch343->control); |
---|
| 686 | + if (retval) |
---|
| 687 | + goto error_get_interface; |
---|
| 688 | + |
---|
| 689 | + /* |
---|
| 690 | + * FIXME: Why do we need this? Allocating 64K of physically contiguous |
---|
| 691 | + * memory is really nasty... |
---|
| 692 | + */ |
---|
| 693 | + set_bit(TTY_NO_WRITE_SPLIT, &tty->flags); |
---|
| 694 | + ch343->control->needs_remote_wakeup = 1; |
---|
| 695 | + |
---|
| 696 | + // retval = ch343_configure(ch343); |
---|
| 697 | + // if (retval) |
---|
| 698 | + // goto error_configure; |
---|
| 699 | + |
---|
| 700 | + ch343_tty_set_termios(tty, NULL); |
---|
| 701 | + |
---|
| 702 | + retval = usb_submit_urb(ch343->ctrlurb, GFP_KERNEL); |
---|
| 703 | + if (retval) { |
---|
| 704 | + dev_err(&ch343->control->dev, "%s - usb_submit_urb(ctrl cmd) failed\n", __func__); |
---|
| 705 | + goto error_submit_urb; |
---|
| 706 | + } |
---|
| 707 | + |
---|
| 708 | + retval = ch343_submit_read_urbs(ch343, GFP_KERNEL); |
---|
| 709 | + if (retval) |
---|
| 710 | + goto error_submit_read_urbs; |
---|
| 711 | + usb_autopm_put_interface(ch343->control); |
---|
| 712 | + |
---|
| 713 | + mutex_unlock(&ch343->mutex); |
---|
| 714 | + |
---|
| 715 | + return 0; |
---|
| 716 | + |
---|
| 717 | +error_submit_read_urbs: |
---|
| 718 | + for (i = 0; i < ch343->rx_buflimit; i++) |
---|
| 719 | + usb_kill_urb(ch343->read_urbs[i]); |
---|
| 720 | +error_submit_urb: |
---|
| 721 | + usb_kill_urb(ch343->ctrlurb); |
---|
| 722 | + // error_configure: |
---|
| 723 | + usb_autopm_put_interface(ch343->control); |
---|
| 724 | +error_get_interface: |
---|
| 725 | +disconnected: |
---|
| 726 | + mutex_unlock(&ch343->mutex); |
---|
| 727 | + |
---|
| 728 | + return usb_translate_errors(retval); |
---|
| 729 | +} |
---|
| 730 | + |
---|
| 731 | +static void ch343_port_destruct(struct tty_port *port) |
---|
| 732 | +{ |
---|
| 733 | + struct ch343 *ch343 = container_of(port, struct ch343, port); |
---|
| 734 | + |
---|
| 735 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 736 | + |
---|
| 737 | + ch343_release_minor(ch343); |
---|
| 738 | + usb_put_intf(ch343->control); |
---|
| 739 | + kfree(ch343); |
---|
| 740 | +} |
---|
| 741 | + |
---|
| 742 | +static void ch343_port_shutdown(struct tty_port *port) |
---|
| 743 | +{ |
---|
| 744 | + struct ch343 *ch343 = container_of(port, struct ch343, port); |
---|
| 745 | + struct urb *urb; |
---|
| 746 | + struct ch343_wb *wb; |
---|
| 747 | + int i; |
---|
| 748 | + |
---|
| 749 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 750 | + |
---|
| 751 | + usb_autopm_get_interface_no_resume(ch343->control); |
---|
| 752 | + ch343->control->needs_remote_wakeup = 0; |
---|
| 753 | + usb_autopm_put_interface(ch343->control); |
---|
| 754 | + |
---|
| 755 | + for (;;) { |
---|
| 756 | + urb = usb_get_from_anchor(&ch343->delayed); |
---|
| 757 | + if (!urb) |
---|
| 758 | + break; |
---|
| 759 | + wb = urb->context; |
---|
| 760 | + wb->use = 0; |
---|
| 761 | + usb_autopm_put_interface_async(ch343->control); |
---|
| 762 | + } |
---|
| 763 | + |
---|
| 764 | + usb_kill_urb(ch343->ctrlurb); |
---|
| 765 | + for (i = 0; i < CH343_NW; i++) |
---|
| 766 | + usb_kill_urb(ch343->wb[i].urb); |
---|
| 767 | + for (i = 0; i < ch343->rx_buflimit; i++) |
---|
| 768 | + usb_kill_urb(ch343->read_urbs[i]); |
---|
| 769 | +} |
---|
| 770 | + |
---|
| 771 | +static void ch343_tty_cleanup(struct tty_struct *tty) |
---|
| 772 | +{ |
---|
| 773 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 774 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 775 | + tty_port_put(&ch343->port); |
---|
| 776 | +} |
---|
| 777 | + |
---|
| 778 | +static void ch343_tty_hangup(struct tty_struct *tty) |
---|
| 779 | +{ |
---|
| 780 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 781 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 782 | + tty_port_hangup(&ch343->port); |
---|
| 783 | +} |
---|
| 784 | + |
---|
| 785 | +static void ch343_tty_close(struct tty_struct *tty, struct file *filp) |
---|
| 786 | +{ |
---|
| 787 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 788 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 789 | + tty_port_close(&ch343->port, tty, filp); |
---|
| 790 | +} |
---|
| 791 | + |
---|
| 792 | +static int ch343_tty_write(struct tty_struct *tty, const unsigned char *buf, int count) |
---|
| 793 | +{ |
---|
| 794 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 795 | + int stat; |
---|
| 796 | + unsigned long flags; |
---|
| 797 | + int wbn; |
---|
| 798 | + struct ch343_wb *wb; |
---|
| 799 | + |
---|
| 800 | + if (!count) |
---|
| 801 | + return 0; |
---|
| 802 | + |
---|
| 803 | + dev_vdbg(&ch343->data->dev, "%s - count %d\n", __func__, count); |
---|
| 804 | + |
---|
| 805 | + spin_lock_irqsave(&ch343->write_lock, flags); |
---|
| 806 | + wbn = ch343_wb_alloc(ch343); |
---|
| 807 | + if (wbn < 0) { |
---|
| 808 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 809 | + return 0; |
---|
| 810 | + } |
---|
| 811 | + wb = &ch343->wb[wbn]; |
---|
| 812 | + |
---|
| 813 | + if (!ch343->dev) { |
---|
| 814 | + wb->use = 0; |
---|
| 815 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 816 | + return -ENODEV; |
---|
| 817 | + } |
---|
| 818 | + |
---|
| 819 | + count = (count > ch343->writesize) ? ch343->writesize : count; |
---|
| 820 | + |
---|
| 821 | + memcpy(wb->buf, buf, count); |
---|
| 822 | + wb->len = count; |
---|
| 823 | + |
---|
| 824 | + stat = usb_autopm_get_interface_async(ch343->control); |
---|
| 825 | + if (stat) { |
---|
| 826 | + wb->use = 0; |
---|
| 827 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 828 | + return stat; |
---|
| 829 | + } |
---|
| 830 | + |
---|
| 831 | + if (ch343->susp_count) { |
---|
| 832 | + usb_anchor_urb(wb->urb, &ch343->delayed); |
---|
| 833 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 834 | + return count; |
---|
| 835 | + } |
---|
| 836 | + |
---|
| 837 | + stat = ch343_start_wb(ch343, wb); |
---|
| 838 | + spin_unlock_irqrestore(&ch343->write_lock, flags); |
---|
| 839 | + |
---|
| 840 | + if (stat < 0) |
---|
| 841 | + return stat; |
---|
| 842 | + return count; |
---|
| 843 | +} |
---|
| 844 | + |
---|
| 845 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) |
---|
| 846 | +static unsigned int ch343_tty_write_room(struct tty_struct *tty) |
---|
| 847 | +#else |
---|
| 848 | +static int ch343_tty_write_room(struct tty_struct *tty) |
---|
| 849 | +#endif |
---|
| 850 | +{ |
---|
| 851 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 852 | + /* |
---|
| 853 | + * Do not let the line discipline to know that we have a reserve, |
---|
| 854 | + * or it might get too enthusiastic. |
---|
| 855 | + */ |
---|
| 856 | + return ch343_wb_is_avail(ch343) ? ch343->writesize : 0; |
---|
| 857 | +} |
---|
| 858 | + |
---|
| 859 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) |
---|
| 860 | +static unsigned int ch343_tty_chars_in_buffer(struct tty_struct *tty) |
---|
| 861 | +#else |
---|
| 862 | +static int ch343_tty_chars_in_buffer(struct tty_struct *tty) |
---|
| 863 | +#endif |
---|
| 864 | +{ |
---|
| 865 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 866 | + /* |
---|
| 867 | + * if the device was unplugged then any remaining characters fell out |
---|
| 868 | + * of the connector ;) |
---|
| 869 | + */ |
---|
| 870 | + if (ch343->disconnected) |
---|
| 871 | + return 0; |
---|
| 872 | + /* |
---|
| 873 | + * This is inaccurate (overcounts), but it works. |
---|
| 874 | + */ |
---|
| 875 | + return (CH343_NW - ch343_wb_is_avail(ch343)) * ch343->writesize; |
---|
| 876 | +} |
---|
| 877 | + |
---|
| 878 | +static int ch343_tty_break_ctl(struct tty_struct *tty, int state) |
---|
| 879 | +{ |
---|
| 880 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 881 | + int retval; |
---|
| 882 | + uint16_t reg_contents; |
---|
| 883 | + uint8_t *regbuf; |
---|
| 884 | + |
---|
| 885 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 886 | + |
---|
| 887 | + regbuf = kmalloc(2, GFP_KERNEL); |
---|
| 888 | + if (!regbuf) |
---|
| 889 | + return -1; |
---|
| 890 | + |
---|
| 891 | + if (state != 0) { |
---|
| 892 | + if ((ch343->chiptype == CHIP_CH344L) || (ch343->chiptype == CHIP_CH344Q) || |
---|
| 893 | + (ch343->chiptype == CHIP_CH344L_V2) || (ch343->chiptype == CHIP_CH9104L)) { |
---|
| 894 | + regbuf[0] = ch343->iface; |
---|
| 895 | + regbuf[1] = 0x01; |
---|
| 896 | + } else { |
---|
| 897 | + regbuf[0] = CH343_N_B; |
---|
| 898 | + regbuf[1] = 0x00; |
---|
| 899 | + } |
---|
| 900 | + } else { |
---|
| 901 | + if ((ch343->chiptype == CHIP_CH344L) || (ch343->chiptype == CHIP_CH344Q) || |
---|
| 902 | + (ch343->chiptype == CHIP_CH344L_V2) || (ch343->chiptype == CHIP_CH9104L)) { |
---|
| 903 | + regbuf[0] = ch343->iface; |
---|
| 904 | + regbuf[1] = 0x00; |
---|
| 905 | + } else { |
---|
| 906 | + regbuf[0] = CH343_N_B | CH343_N_AB; |
---|
| 907 | + regbuf[1] = 0x00; |
---|
| 908 | + } |
---|
| 909 | + } |
---|
| 910 | + reg_contents = get_unaligned_le16(regbuf); |
---|
| 911 | + |
---|
| 912 | + if ((ch343->chiptype == CHIP_CH344L) || (ch343->chiptype == CHIP_CH344Q) || (ch343->chiptype == CHIP_CH344L_V2) || |
---|
| 913 | + (ch343->chiptype == CHIP_CH9104L)) { |
---|
| 914 | + retval = ch343_control_out(ch343, CMD_C4, reg_contents, 0x00); |
---|
| 915 | + } else { |
---|
| 916 | + if (ch343->iface) |
---|
| 917 | + retval = ch343_control_out(ch343, CMD_C4, 0x00, reg_contents); |
---|
| 918 | + else |
---|
| 919 | + retval = ch343_control_out(ch343, CMD_C4, reg_contents, 0x00); |
---|
| 920 | + } |
---|
| 921 | + |
---|
| 922 | + if (retval < 0) |
---|
| 923 | + dev_err(&ch343->control->dev, "%s - USB control write error (%d)\n", __func__, retval); |
---|
| 924 | + |
---|
| 925 | + kfree(regbuf); |
---|
| 926 | + |
---|
| 927 | + return retval; |
---|
| 928 | +} |
---|
| 929 | + |
---|
| 930 | +static int ch343_tty_tiocmget(struct tty_struct *tty) |
---|
| 931 | +{ |
---|
| 932 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 933 | + unsigned long flags; |
---|
| 934 | + unsigned int result; |
---|
| 935 | + |
---|
| 936 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 937 | + |
---|
| 938 | + spin_lock_irqsave(&ch343->read_lock, flags); |
---|
| 939 | + result = (ch343->ctrlout & CH343_CTO_D ? TIOCM_DTR : 0) | (ch343->ctrlout & CH343_CTO_R ? TIOCM_RTS : 0) | |
---|
| 940 | + (ch343->ctrlin & CH343_CTI_C ? TIOCM_CTS : 0) | (ch343->ctrlin & CH343_CTI_DS ? TIOCM_DSR : 0) | |
---|
| 941 | + (ch343->ctrlin & CH343_CTI_R ? TIOCM_RI : 0) | (ch343->ctrlin & CH343_CTI_DC ? TIOCM_CD : 0); |
---|
| 942 | + spin_unlock_irqrestore(&ch343->read_lock, flags); |
---|
| 943 | + |
---|
| 944 | + return result; |
---|
| 945 | +} |
---|
| 946 | + |
---|
| 947 | +static int ch343_tty_tiocmset(struct tty_struct *tty, unsigned int set, unsigned int clear) |
---|
| 948 | +{ |
---|
| 949 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 950 | + unsigned int newctrl; |
---|
| 951 | + |
---|
| 952 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 953 | + |
---|
| 954 | + newctrl = ch343->ctrlout; |
---|
| 955 | + set = (set & TIOCM_DTR ? CH343_CTO_D : 0) | (set & TIOCM_RTS ? CH343_CTO_R : 0); |
---|
| 956 | + clear = (clear & TIOCM_DTR ? CH343_CTO_D : 0) | (clear & TIOCM_RTS ? CH343_CTO_R : 0); |
---|
| 957 | + |
---|
| 958 | + newctrl = (newctrl & ~clear) | set; |
---|
| 959 | + |
---|
| 960 | + if (ch343->ctrlout == newctrl) { |
---|
| 961 | + return 0; |
---|
| 962 | + } |
---|
| 963 | + |
---|
| 964 | + return ch343_set_control(ch343, ch343->ctrlout = newctrl); |
---|
| 965 | +} |
---|
| 966 | + |
---|
| 967 | +static int ch343_get_serial_info(struct ch343 *ch343, struct serial_struct __user *info) |
---|
| 968 | +{ |
---|
| 969 | + struct serial_struct tmp; |
---|
| 970 | + |
---|
| 971 | + if (!info) |
---|
| 972 | + return -EINVAL; |
---|
| 973 | + |
---|
| 974 | + memset(&tmp, 0, sizeof(tmp)); |
---|
| 975 | + tmp.flags = ASYNC_LOW_LATENCY; |
---|
| 976 | + tmp.xmit_fifo_size = ch343->writesize; |
---|
| 977 | + tmp.baud_base = le32_to_cpu(ch343->line.dwDTERate); |
---|
| 978 | + tmp.close_delay = ch343->port.close_delay / 10; |
---|
| 979 | + tmp.closing_wait = |
---|
| 980 | + ch343->port.closing_wait == ASYNC_CLOSING_WAIT_NONE ? ASYNC_CLOSING_WAIT_NONE : ch343->port.closing_wait / 10; |
---|
| 981 | + |
---|
| 982 | + if (copy_to_user(info, &tmp, sizeof(tmp))) |
---|
| 983 | + return -EFAULT; |
---|
| 984 | + else |
---|
| 985 | + return 0; |
---|
| 986 | +} |
---|
| 987 | + |
---|
| 988 | +static int ch343_set_serial_info(struct ch343 *ch343, struct serial_struct __user *newinfo) |
---|
| 989 | +{ |
---|
| 990 | + struct serial_struct new_serial; |
---|
| 991 | + unsigned int closing_wait, close_delay; |
---|
| 992 | + int retval = 0; |
---|
| 993 | + |
---|
| 994 | + if (copy_from_user(&new_serial, newinfo, sizeof(new_serial))) |
---|
| 995 | + return -EFAULT; |
---|
| 996 | + |
---|
| 997 | + close_delay = new_serial.close_delay * 10; |
---|
| 998 | + closing_wait = |
---|
| 999 | + new_serial.closing_wait == ASYNC_CLOSING_WAIT_NONE ? ASYNC_CLOSING_WAIT_NONE : new_serial.closing_wait * 10; |
---|
| 1000 | + |
---|
| 1001 | + mutex_lock(&ch343->port.mutex); |
---|
| 1002 | + |
---|
| 1003 | + if (!capable(CAP_SYS_ADMIN)) { |
---|
| 1004 | + if ((close_delay != ch343->port.close_delay) || (closing_wait != ch343->port.closing_wait)) |
---|
| 1005 | + retval = -EPERM; |
---|
| 1006 | + else |
---|
| 1007 | + retval = -EOPNOTSUPP; |
---|
| 1008 | + } else { |
---|
| 1009 | + ch343->port.close_delay = close_delay; |
---|
| 1010 | + ch343->port.closing_wait = closing_wait; |
---|
| 1011 | + } |
---|
| 1012 | + |
---|
| 1013 | + mutex_unlock(&ch343->port.mutex); |
---|
| 1014 | + return retval; |
---|
| 1015 | +} |
---|
| 1016 | + |
---|
| 1017 | +static int ch343_wait_serial_change(struct ch343 *ch343, unsigned long arg) |
---|
| 1018 | +{ |
---|
| 1019 | + int rv = 0; |
---|
| 1020 | + DECLARE_WAITQUEUE(wait, current); |
---|
| 1021 | + struct async_icount old, new; |
---|
| 1022 | + |
---|
| 1023 | + do { |
---|
| 1024 | + spin_lock_irq(&ch343->read_lock); |
---|
| 1025 | + old = ch343->oldcount; |
---|
| 1026 | + new = ch343->iocount; |
---|
| 1027 | + ch343->oldcount = new; |
---|
| 1028 | + spin_unlock_irq(&ch343->read_lock); |
---|
| 1029 | + |
---|
| 1030 | + if ((arg & TIOCM_CTS) && old.cts != new.cts) |
---|
| 1031 | + break; |
---|
| 1032 | + if ((arg & TIOCM_DSR) && old.dsr != new.dsr) |
---|
| 1033 | + break; |
---|
| 1034 | + if ((arg & TIOCM_RI) && old.rng != new.rng) |
---|
| 1035 | + break; |
---|
| 1036 | + if ((arg & TIOCM_CD) && old.dcd != new.dcd) |
---|
| 1037 | + break; |
---|
| 1038 | + |
---|
| 1039 | + add_wait_queue(&ch343->wioctl, &wait); |
---|
| 1040 | + set_current_state(TASK_INTERRUPTIBLE); |
---|
| 1041 | + schedule(); |
---|
| 1042 | + remove_wait_queue(&ch343->wioctl, &wait); |
---|
| 1043 | + if (ch343->disconnected) { |
---|
| 1044 | + if (arg & TIOCM_CD) |
---|
| 1045 | + break; |
---|
| 1046 | + else |
---|
| 1047 | + rv = -ENODEV; |
---|
| 1048 | + } else { |
---|
| 1049 | + if (signal_pending(current)) |
---|
| 1050 | + rv = -ERESTARTSYS; |
---|
| 1051 | + } |
---|
| 1052 | + } while (!rv); |
---|
| 1053 | + |
---|
| 1054 | + return rv; |
---|
| 1055 | +} |
---|
| 1056 | + |
---|
| 1057 | +static int ch343_get_serial_usage(struct ch343 *ch343, struct serial_icounter_struct __user *count) |
---|
| 1058 | +{ |
---|
| 1059 | + struct serial_icounter_struct icount; |
---|
| 1060 | + int rv = 0; |
---|
| 1061 | + |
---|
| 1062 | + memset(&icount, 0, sizeof(icount)); |
---|
| 1063 | + icount.cts = ch343->iocount.cts; |
---|
| 1064 | + icount.dsr = ch343->iocount.dsr; |
---|
| 1065 | + icount.rng = ch343->iocount.rng; |
---|
| 1066 | + icount.dcd = ch343->iocount.dcd; |
---|
| 1067 | + icount.frame = ch343->iocount.frame; |
---|
| 1068 | + icount.overrun = ch343->iocount.overrun; |
---|
| 1069 | + icount.parity = ch343->iocount.parity; |
---|
| 1070 | + icount.brk = ch343->iocount.brk; |
---|
| 1071 | + |
---|
| 1072 | + if (copy_to_user(count, &icount, sizeof(icount)) > 0) |
---|
| 1073 | + rv = -EFAULT; |
---|
| 1074 | + |
---|
| 1075 | + return rv; |
---|
| 1076 | +} |
---|
| 1077 | + |
---|
| 1078 | +static int ch343_tty_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg) |
---|
| 1079 | +{ |
---|
| 1080 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 1081 | + int rv = 0; |
---|
| 1082 | + unsigned long arg1, arg2, arg3, arg4, arg5, arg6; |
---|
| 1083 | + u32 __user *argval = (u32 __user *)arg; |
---|
| 1084 | + u8 *buffer; |
---|
| 1085 | + |
---|
| 1086 | + dev_dbg(&ch343->control->dev, "%s\n", __func__); |
---|
| 1087 | + |
---|
| 1088 | + buffer = kmalloc(512, GFP_KERNEL); |
---|
| 1089 | + if (!buffer) |
---|
| 1090 | + return -ENOMEM; |
---|
| 1091 | + |
---|
| 1092 | + switch (cmd) { |
---|
| 1093 | + case TIOCGSERIAL: /* gets serial port data */ |
---|
| 1094 | + rv = ch343_get_serial_info(ch343, (struct serial_struct __user *)arg); |
---|
| 1095 | + break; |
---|
| 1096 | + case TIOCSSERIAL: |
---|
| 1097 | + rv = ch343_set_serial_info(ch343, (struct serial_struct __user *)arg); |
---|
| 1098 | + break; |
---|
| 1099 | + case TIOCMIWAIT: |
---|
| 1100 | + rv = usb_autopm_get_interface(ch343->control); |
---|
| 1101 | + if (rv < 0) { |
---|
| 1102 | + rv = -EIO; |
---|
| 1103 | + break; |
---|
| 1104 | + } |
---|
| 1105 | + rv = ch343_wait_serial_change(ch343, arg); |
---|
| 1106 | + usb_autopm_put_interface(ch343->control); |
---|
| 1107 | + break; |
---|
| 1108 | + case IOCTL_CMD_GICOUNT: |
---|
| 1109 | + case TIOCGICOUNT: |
---|
| 1110 | + rv = ch343_get_serial_usage(ch343, (struct serial_icounter_struct __user *)arg); |
---|
| 1111 | + break; |
---|
| 1112 | + case IOCTL_CMD_GETCHIPTYPE: |
---|
| 1113 | + if (put_user(ch343->chiptype, argval)) { |
---|
| 1114 | + rv = -EFAULT; |
---|
| 1115 | + goto out; |
---|
| 1116 | + } |
---|
| 1117 | + break; |
---|
| 1118 | + case IOCTL_CMD_CTRLIN: |
---|
| 1119 | + get_user(arg1, (u8 __user *)arg); |
---|
| 1120 | + get_user(arg2, ((u8 __user *)arg + 1)); |
---|
| 1121 | + get_user(arg3, (u16 __user *)((u8 *)arg + 2)); |
---|
| 1122 | + get_user(arg4, (u16 __user *)((u8 *)arg + 4)); |
---|
| 1123 | + get_user(arg5, (u16 __user *)((u8 *)arg + 6)); |
---|
| 1124 | + arg6 = (unsigned long)((u8 __user *)arg + 8); |
---|
| 1125 | + rv = ch343_control_msg_in(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6, (u16)arg5); |
---|
| 1126 | + break; |
---|
| 1127 | + case IOCTL_CMD_CTRLOUT: |
---|
| 1128 | + get_user(arg1, (u8 __user *)arg); |
---|
| 1129 | + get_user(arg2, ((u8 __user *)arg + 1)); |
---|
| 1130 | + get_user(arg3, (u16 __user *)((u8 *)arg + 2)); |
---|
| 1131 | + get_user(arg4, (u16 __user *)((u8 *)arg + 4)); |
---|
| 1132 | + get_user(arg5, (u16 __user *)((u8 *)arg + 6)); |
---|
| 1133 | + arg6 = (unsigned long)((u8 __user *)arg + 8); |
---|
| 1134 | + rv = ch343_control_msg_out(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6, (u16)arg5); |
---|
| 1135 | + if (rv != (u16)arg5) { |
---|
| 1136 | + rv = -EINVAL; |
---|
| 1137 | + goto out; |
---|
| 1138 | + } |
---|
| 1139 | + break; |
---|
| 1140 | + default: |
---|
| 1141 | + rv = -ENOIOCTLCMD; |
---|
| 1142 | + break; |
---|
| 1143 | + } |
---|
| 1144 | + |
---|
| 1145 | +out: |
---|
| 1146 | + kfree(buffer); |
---|
| 1147 | + return rv < 0 ? rv : 0; |
---|
| 1148 | +} |
---|
| 1149 | + |
---|
| 1150 | +static int ch343_get(CHIPTYPE chiptype, unsigned int bval, unsigned char *fct, unsigned char *dvs) |
---|
| 1151 | +{ |
---|
| 1152 | + unsigned char a; |
---|
| 1153 | + unsigned char b; |
---|
| 1154 | + unsigned long c; |
---|
| 1155 | + |
---|
| 1156 | + if (((chiptype == CHIP_CH347T) || (chiptype == CHIP_CH344Q) || (chiptype == CHIP_CH9104L)) && bval >= 2000000) { |
---|
| 1157 | + *fct = (unsigned char)(bval / 200); |
---|
| 1158 | + *dvs = (unsigned char)((bval / 200) >> 8); |
---|
| 1159 | + } |
---|
| 1160 | + |
---|
| 1161 | + switch (bval) { |
---|
| 1162 | + case 6000000: |
---|
| 1163 | + case 4000000: |
---|
| 1164 | + case 2400000: |
---|
| 1165 | + case 921600: |
---|
| 1166 | + case 307200: |
---|
| 1167 | + case 256000: |
---|
| 1168 | + b = 7; |
---|
| 1169 | + c = 12000000; |
---|
| 1170 | + break; |
---|
| 1171 | + default: |
---|
| 1172 | + if (bval > 6000000 / 255) { |
---|
| 1173 | + b = 3; |
---|
| 1174 | + c = 6000000; |
---|
| 1175 | + } else if (bval > 750000 / 255) { |
---|
| 1176 | + b = 2; |
---|
| 1177 | + c = 750000; |
---|
| 1178 | + } else if (bval > 93750 / 255) { |
---|
| 1179 | + b = 1; |
---|
| 1180 | + c = 93750; |
---|
| 1181 | + } else { |
---|
| 1182 | + b = 0; |
---|
| 1183 | + c = 11719; |
---|
| 1184 | + } |
---|
| 1185 | + break; |
---|
| 1186 | + } |
---|
| 1187 | + a = (unsigned char)(c / bval); |
---|
| 1188 | + if (a == 0 || a == 0xFF) |
---|
| 1189 | + return -EINVAL; |
---|
| 1190 | + if ((c / a - bval) > (bval - c / (a + 1))) |
---|
| 1191 | + a++; |
---|
| 1192 | + a = 256 - a; |
---|
| 1193 | + |
---|
| 1194 | + *fct = a; |
---|
| 1195 | + *dvs = b; |
---|
| 1196 | + |
---|
| 1197 | + return 0; |
---|
| 1198 | +} |
---|
| 1199 | + |
---|
| 1200 | +static void ch343_tty_set_termios(struct tty_struct *tty, struct ktermios *termios_old) |
---|
| 1201 | +{ |
---|
| 1202 | + struct ch343 *ch343 = tty->driver_data; |
---|
| 1203 | + struct ktermios *termios = &tty->termios; |
---|
| 1204 | + struct usb_ch343_line_coding newline; |
---|
| 1205 | + int newctrl = ch343->ctrlout; |
---|
| 1206 | + |
---|
| 1207 | + unsigned char dvs = 0; |
---|
| 1208 | + unsigned char reg_count = 0; |
---|
| 1209 | + unsigned char fct = 0; |
---|
| 1210 | + unsigned char reg_value = 0; |
---|
| 1211 | + unsigned short value = 0; |
---|
| 1212 | + unsigned short index = 0; |
---|
| 1213 | + |
---|
| 1214 | + dev_dbg(tty->dev, "%s\n", __func__); |
---|
| 1215 | + |
---|
| 1216 | + if (termios_old && !tty_termios_hw_change(&tty->termios, termios_old)) { |
---|
| 1217 | + return; |
---|
| 1218 | + } |
---|
| 1219 | + |
---|
| 1220 | + newline.dwDTERate = tty_get_baud_rate(tty); |
---|
| 1221 | + |
---|
| 1222 | + if (newline.dwDTERate == 0) |
---|
| 1223 | + newline.dwDTERate = 9600; |
---|
| 1224 | + ch343_get(ch343->chiptype, newline.dwDTERate, &fct, &dvs); |
---|
| 1225 | + |
---|
| 1226 | + newline.bCharFormat = termios->c_cflag & CSTOPB ? 2 : 1; |
---|
| 1227 | + if (newline.bCharFormat == 2) |
---|
| 1228 | + reg_value |= CH343_L_SB; |
---|
| 1229 | + |
---|
| 1230 | + newline.bParityType = |
---|
| 1231 | + termios->c_cflag & PARENB ? (termios->c_cflag & PARODD ? 1 : 2) + (termios->c_cflag & CMSPAR ? 2 : 0) : 0; |
---|
| 1232 | + |
---|
| 1233 | + switch (newline.bParityType) { |
---|
| 1234 | + case 0x01: |
---|
| 1235 | + reg_value |= CH343_L_P_O; |
---|
| 1236 | + break; |
---|
| 1237 | + case 0x02: |
---|
| 1238 | + reg_value |= CH343_L_P_E; |
---|
| 1239 | + break; |
---|
| 1240 | + case 0x03: |
---|
| 1241 | + reg_value |= CH343_L_P_M; |
---|
| 1242 | + break; |
---|
| 1243 | + case 0x04: |
---|
| 1244 | + reg_value |= CH343_L_P_S; |
---|
| 1245 | + break; |
---|
| 1246 | + default: |
---|
| 1247 | + break; |
---|
| 1248 | + } |
---|
| 1249 | + |
---|
| 1250 | + switch (termios->c_cflag & CSIZE) { |
---|
| 1251 | + case CS5: |
---|
| 1252 | + newline.bDataBits = 5; |
---|
| 1253 | + reg_value |= CH343_L_C5; |
---|
| 1254 | + break; |
---|
| 1255 | + case CS6: |
---|
| 1256 | + newline.bDataBits = 6; |
---|
| 1257 | + reg_value |= CH343_L_C6; |
---|
| 1258 | + break; |
---|
| 1259 | + case CS7: |
---|
| 1260 | + newline.bDataBits = 7; |
---|
| 1261 | + reg_value |= CH343_L_C7; |
---|
| 1262 | + break; |
---|
| 1263 | + case CS8: |
---|
| 1264 | + default: |
---|
| 1265 | + newline.bDataBits = 8; |
---|
| 1266 | + reg_value |= CH343_L_C8; |
---|
| 1267 | + break; |
---|
| 1268 | + } |
---|
| 1269 | + |
---|
| 1270 | + /* FIXME: Needs to clear unsupported bits in the termios */ |
---|
| 1271 | + ch343->clocal = ((termios->c_cflag & CLOCAL) != 0); |
---|
| 1272 | + |
---|
| 1273 | + if (C_BAUD(tty) == B0) { |
---|
| 1274 | + newline.dwDTERate = ch343->line.dwDTERate; |
---|
| 1275 | + newctrl &= ~CH343_CTO_D; |
---|
| 1276 | + } else if (termios_old && (termios_old->c_cflag & CBAUD) == B0) { |
---|
| 1277 | + newctrl |= CH343_CTO_D; |
---|
| 1278 | + } |
---|
| 1279 | + |
---|
| 1280 | + reg_value |= CH343_L_E_R | CH343_L_E_T; |
---|
| 1281 | + reg_count |= CH343_L_R_CT | CH343_L_R_CL | CH343_L_R_T; |
---|
| 1282 | + |
---|
| 1283 | + value |= reg_count; |
---|
| 1284 | + value |= (unsigned short)reg_value << 8; |
---|
| 1285 | + |
---|
| 1286 | + index |= 0x00 | dvs; |
---|
| 1287 | + index |= (unsigned short)fct << 8; |
---|
| 1288 | + if (ch343->iface <= 1) |
---|
| 1289 | + ch343_control_out(ch343, CMD_C1 + ch343->iface, value, index); |
---|
| 1290 | + else if (ch343->iface <= 3) |
---|
| 1291 | + ch343_control_out(ch343, CMD_C1 + 0x10 + (ch343->iface - 2), value, index); |
---|
| 1292 | + |
---|
| 1293 | + if (memcmp(&ch343->line, &newline, sizeof newline)) { |
---|
| 1294 | + memcpy(&ch343->line, &newline, sizeof newline); |
---|
| 1295 | + dev_dbg(&ch343->control->dev, "%s - set line: %d %d %d %d\n", __func__, newline.dwDTERate, newline.bCharFormat, |
---|
| 1296 | + newline.bParityType, newline.bDataBits); |
---|
| 1297 | + } |
---|
| 1298 | + |
---|
| 1299 | + if (C_CRTSCTS(tty)) { |
---|
| 1300 | + newctrl |= CH343_CTO_A | CH343_CTO_R; |
---|
| 1301 | + } else |
---|
| 1302 | + newctrl &= ~CH343_CTO_A; |
---|
| 1303 | + |
---|
| 1304 | + if (newctrl != ch343->ctrlout) |
---|
| 1305 | + ch343_set_control(ch343, ch343->ctrlout = newctrl); |
---|
| 1306 | +} |
---|
| 1307 | + |
---|
| 1308 | +static const struct tty_port_operations ch343_port_ops = { |
---|
| 1309 | + .dtr_rts = ch343_port_dtr_rts, |
---|
| 1310 | + .shutdown = ch343_port_shutdown, |
---|
| 1311 | + .activate = ch343_port_activate, |
---|
| 1312 | + .destruct = ch343_port_destruct, |
---|
| 1313 | +}; |
---|
| 1314 | + |
---|
| 1315 | +/* Little helpers: write/read buffers free */ |
---|
| 1316 | +static void ch343_write_buffers_free(struct ch343 *ch343) |
---|
| 1317 | +{ |
---|
| 1318 | + int i; |
---|
| 1319 | + struct ch343_wb *wb; |
---|
| 1320 | + struct usb_device *usb_dev = interface_to_usbdev(ch343->control); |
---|
| 1321 | + |
---|
| 1322 | + for (wb = &ch343->wb[0], i = 0; i < CH343_NW; i++, wb++) |
---|
| 1323 | + usb_free_coherent(usb_dev, ch343->writesize, wb->buf, wb->dmah); |
---|
| 1324 | +} |
---|
| 1325 | + |
---|
| 1326 | +static void ch343_read_buffers_free(struct ch343 *ch343) |
---|
| 1327 | +{ |
---|
| 1328 | + struct usb_device *usb_dev = interface_to_usbdev(ch343->control); |
---|
| 1329 | + int i; |
---|
| 1330 | + |
---|
| 1331 | + for (i = 0; i < ch343->rx_buflimit; i++) |
---|
| 1332 | + usb_free_coherent(usb_dev, ch343->readsize, ch343->read_buffers[i].base, ch343->read_buffers[i].dma); |
---|
| 1333 | +} |
---|
| 1334 | + |
---|
| 1335 | +/* Little helper: write buffers allocate */ |
---|
| 1336 | +static int ch343_write_buffers_alloc(struct ch343 *ch343) |
---|
| 1337 | +{ |
---|
| 1338 | + int i; |
---|
| 1339 | + struct ch343_wb *wb; |
---|
| 1340 | + |
---|
| 1341 | + for (wb = &ch343->wb[0], i = 0; i < CH343_NW; i++, wb++) { |
---|
| 1342 | + wb->buf = usb_alloc_coherent(ch343->dev, ch343->writesize, GFP_KERNEL, &wb->dmah); |
---|
| 1343 | + if (!wb->buf) { |
---|
| 1344 | + while (i != 0) { |
---|
| 1345 | + --i; |
---|
| 1346 | + --wb; |
---|
| 1347 | + usb_free_coherent(ch343->dev, ch343->writesize, wb->buf, wb->dmah); |
---|
| 1348 | + } |
---|
| 1349 | + return -ENOMEM; |
---|
| 1350 | + } |
---|
| 1351 | + } |
---|
| 1352 | + return 0; |
---|
| 1353 | +} |
---|
| 1354 | + |
---|
| 1355 | +static int ch343_open(struct inode *inode, struct file *file) |
---|
| 1356 | +{ |
---|
| 1357 | + struct ch343 *ch343; |
---|
| 1358 | + struct usb_interface *interface; |
---|
| 1359 | + int subminor; |
---|
| 1360 | + int retval = 0; |
---|
| 1361 | + |
---|
| 1362 | + subminor = iminor(inode); |
---|
| 1363 | + |
---|
| 1364 | + interface = usb_find_interface(&ch343_driver, subminor); |
---|
| 1365 | + if (!interface) { |
---|
| 1366 | + pr_err("%s - error, can't find device for minor %d\n", __func__, subminor); |
---|
| 1367 | + retval = -ENODEV; |
---|
| 1368 | + goto exit; |
---|
| 1369 | + } |
---|
| 1370 | + |
---|
| 1371 | + ch343 = usb_get_intfdata(interface); |
---|
| 1372 | + if (!ch343) { |
---|
| 1373 | + retval = -ENODEV; |
---|
| 1374 | + goto exit; |
---|
| 1375 | + } |
---|
| 1376 | + |
---|
| 1377 | + /* save our object in the file's private structure */ |
---|
| 1378 | + file->private_data = ch343; |
---|
| 1379 | + |
---|
| 1380 | +exit: |
---|
| 1381 | + return retval; |
---|
| 1382 | +} |
---|
| 1383 | + |
---|
| 1384 | +static int ch343_release(struct inode *inode, struct file *file) |
---|
| 1385 | +{ |
---|
| 1386 | + struct ch343 *ch343; |
---|
| 1387 | + |
---|
| 1388 | + ch343 = file->private_data; |
---|
| 1389 | + if (ch343 == NULL) |
---|
| 1390 | + return -ENODEV; |
---|
| 1391 | + |
---|
| 1392 | + return 0; |
---|
| 1393 | +} |
---|
| 1394 | + |
---|
| 1395 | +static long ch343_ioctl(struct file *file, unsigned int cmd, unsigned long arg) |
---|
| 1396 | +{ |
---|
| 1397 | + struct ch343 *ch343; |
---|
| 1398 | + int rv = 0; |
---|
| 1399 | + u8 *buffer; |
---|
| 1400 | + unsigned long arg1, arg2, arg3, arg4, arg5, arg6; |
---|
| 1401 | + u32 __user *argval = (u32 __user *)arg; |
---|
| 1402 | + |
---|
| 1403 | + ch343 = file->private_data; |
---|
| 1404 | + if (ch343 == NULL) |
---|
| 1405 | + return -ENODEV; |
---|
| 1406 | + |
---|
| 1407 | + buffer = kmalloc(512, GFP_KERNEL); |
---|
| 1408 | + if (!buffer) |
---|
| 1409 | + return -ENOMEM; |
---|
| 1410 | + |
---|
| 1411 | + switch (cmd) { |
---|
| 1412 | + case IOCTL_CMD_GETCHIPTYPE: |
---|
| 1413 | + if (put_user(ch343->chiptype, argval)) { |
---|
| 1414 | + rv = -EFAULT; |
---|
| 1415 | + goto out; |
---|
| 1416 | + } |
---|
| 1417 | + break; |
---|
| 1418 | + case IOCTL_CMD_CTRLIN: |
---|
| 1419 | + get_user(arg1, (u8 __user *)arg); |
---|
| 1420 | + get_user(arg2, ((u8 __user *)arg + 1)); |
---|
| 1421 | + get_user(arg3, (u16 __user *)((u8 *)arg + 2)); |
---|
| 1422 | + get_user(arg4, (u16 __user *)((u8 *)arg + 4)); |
---|
| 1423 | + get_user(arg5, (u16 __user *)((u8 *)arg + 6)); |
---|
| 1424 | + arg6 = (unsigned long)((u8 __user *)arg + 8); |
---|
| 1425 | + rv = ch343_control_msg_in(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6, (u16)arg5); |
---|
| 1426 | + break; |
---|
| 1427 | + case IOCTL_CMD_CTRLOUT: |
---|
| 1428 | + get_user(arg1, (u8 __user *)arg); |
---|
| 1429 | + get_user(arg2, ((u8 __user *)arg + 1)); |
---|
| 1430 | + get_user(arg3, (u16 __user *)((u8 *)arg + 2)); |
---|
| 1431 | + get_user(arg4, (u16 __user *)((u8 *)arg + 4)); |
---|
| 1432 | + get_user(arg5, (u16 __user *)((u8 *)arg + 6)); |
---|
| 1433 | + arg6 = (unsigned long)((u8 __user *)arg + 8); |
---|
| 1434 | + rv = ch343_control_msg_out(ch343, (u8)arg1, (u8)arg2, (u16)arg3, (u16)arg4, (u8 __user *)arg6, (u16)arg5); |
---|
| 1435 | + if (rv != (u16)arg5) { |
---|
| 1436 | + rv = -EINVAL; |
---|
| 1437 | + goto out; |
---|
| 1438 | + } |
---|
| 1439 | + break; |
---|
| 1440 | + default: |
---|
| 1441 | + rv = -ENOIOCTLCMD; |
---|
| 1442 | + break; |
---|
| 1443 | + } |
---|
| 1444 | + |
---|
| 1445 | +out: |
---|
| 1446 | + kfree(buffer); |
---|
| 1447 | + return rv; |
---|
| 1448 | +} |
---|
| 1449 | + |
---|
| 1450 | +static const struct file_operations ch343_fops = { |
---|
| 1451 | + .owner = THIS_MODULE, |
---|
| 1452 | + .open = ch343_open, |
---|
| 1453 | + .unlocked_ioctl = ch343_ioctl, |
---|
| 1454 | + .release = ch343_release, |
---|
| 1455 | +}; |
---|
| 1456 | + |
---|
| 1457 | +/* |
---|
| 1458 | + * usb class driver info in order to get a minor number from the usb core, |
---|
| 1459 | + * and to have the device registered with the driver core |
---|
| 1460 | + */ |
---|
| 1461 | +static struct usb_class_driver ch343_class = { |
---|
| 1462 | + .name = "ch343_iodev%d", |
---|
| 1463 | + .fops = &ch343_fops, |
---|
| 1464 | + .minor_base = USB_MINOR_BASE, |
---|
| 1465 | +}; |
---|
| 1466 | + |
---|
| 1467 | +/* |
---|
| 1468 | + * USB probe and disconnect routines. |
---|
| 1469 | + */ |
---|
| 1470 | +static int ch343_probe(struct usb_interface *intf, const struct usb_device_id *id) |
---|
| 1471 | +{ |
---|
| 1472 | + struct usb_cdc_union_desc *union_header = NULL; |
---|
| 1473 | + unsigned char *buffer = intf->altsetting->extra; |
---|
| 1474 | + int buflen = intf->altsetting->extralen; |
---|
| 1475 | + struct usb_interface *control_interface; |
---|
| 1476 | + struct usb_interface *data_interface; |
---|
| 1477 | + struct usb_endpoint_descriptor *epctrl = NULL; |
---|
| 1478 | + struct usb_endpoint_descriptor *epread = NULL; |
---|
| 1479 | + struct usb_endpoint_descriptor *epwrite = NULL; |
---|
| 1480 | + struct usb_device *usb_dev = interface_to_usbdev(intf); |
---|
| 1481 | + struct ch343 *ch343; |
---|
| 1482 | + int minor; |
---|
| 1483 | + int ctrlsize, readsize; |
---|
| 1484 | + u8 *buf; |
---|
| 1485 | + unsigned long quirks; |
---|
| 1486 | + int num_rx_buf = CH343_NR; |
---|
| 1487 | + int i; |
---|
| 1488 | + unsigned int elength = 0; |
---|
| 1489 | + struct device *tty_dev; |
---|
| 1490 | + int rv = -ENOMEM; |
---|
| 1491 | + |
---|
| 1492 | + /* normal quirks */ |
---|
| 1493 | + quirks = (unsigned long)id->driver_info; |
---|
| 1494 | + if (!buffer) { |
---|
| 1495 | + dev_err(&intf->dev, "Weird descriptor references\n"); |
---|
| 1496 | + return -EINVAL; |
---|
| 1497 | + } |
---|
| 1498 | + |
---|
| 1499 | + while (buflen > 0) { |
---|
| 1500 | + elength = buffer[0]; |
---|
| 1501 | + if (!elength) { |
---|
| 1502 | + dev_err(&intf->dev, "skipping garbage byte\n"); |
---|
| 1503 | + elength = 1; |
---|
| 1504 | + goto next_desc; |
---|
| 1505 | + } |
---|
| 1506 | + if (buffer[1] != USB_DT_CS_INTERFACE) { |
---|
| 1507 | + dev_err(&intf->dev, "skipping garbage\n"); |
---|
| 1508 | + goto next_desc; |
---|
| 1509 | + } |
---|
| 1510 | + |
---|
| 1511 | + switch (buffer[2]) { |
---|
| 1512 | + case USB_CDC_UNION_TYPE: /* we've found it */ |
---|
| 1513 | + if (elength < sizeof(struct usb_cdc_union_desc)) |
---|
| 1514 | + goto next_desc; |
---|
| 1515 | + if (union_header) { |
---|
| 1516 | + dev_err(&intf->dev, |
---|
| 1517 | + "More than one " |
---|
| 1518 | + "union descriptor, skipping ...\n"); |
---|
| 1519 | + goto next_desc; |
---|
| 1520 | + } |
---|
| 1521 | + union_header = (struct usb_cdc_union_desc *)buffer; |
---|
| 1522 | + break; |
---|
| 1523 | + default: |
---|
| 1524 | + /* |
---|
| 1525 | + * there are LOTS more CDC descriptors that |
---|
| 1526 | + * could legitimately be found here. |
---|
| 1527 | + */ |
---|
| 1528 | + break; |
---|
| 1529 | + } |
---|
| 1530 | + next_desc: |
---|
| 1531 | + buflen -= elength; |
---|
| 1532 | + buffer += elength; |
---|
| 1533 | + } |
---|
| 1534 | + |
---|
| 1535 | + control_interface = usb_ifnum_to_if(usb_dev, union_header->bMasterInterface0); |
---|
| 1536 | + data_interface = usb_ifnum_to_if(usb_dev, union_header->bSlaveInterface0); |
---|
| 1537 | + |
---|
| 1538 | + if (intf != control_interface) |
---|
| 1539 | + return -ENODEV; |
---|
| 1540 | + |
---|
| 1541 | + if (usb_interface_claimed(data_interface)) { |
---|
| 1542 | + dev_dbg(&intf->dev, "The data interface isn't available\n"); |
---|
| 1543 | + return -EBUSY; |
---|
| 1544 | + } |
---|
| 1545 | + |
---|
| 1546 | + if (data_interface->cur_altsetting->desc.bNumEndpoints < 2 || |
---|
| 1547 | + control_interface->cur_altsetting->desc.bNumEndpoints == 0) |
---|
| 1548 | + return -EINVAL; |
---|
| 1549 | + |
---|
| 1550 | + epctrl = &control_interface->cur_altsetting->endpoint[0].desc; |
---|
| 1551 | + epwrite = &data_interface->cur_altsetting->endpoint[0].desc; |
---|
| 1552 | + epread = &data_interface->cur_altsetting->endpoint[1].desc; |
---|
| 1553 | + |
---|
| 1554 | + /* workaround for switched endpoints */ |
---|
| 1555 | + if (!usb_endpoint_dir_in(epread)) { |
---|
| 1556 | + /* descriptors are swapped */ |
---|
| 1557 | + dev_dbg(&intf->dev, "The data interface has switched endpoints\n"); |
---|
| 1558 | + swap(epread, epwrite); |
---|
| 1559 | + } |
---|
| 1560 | + |
---|
| 1561 | + ch343 = kzalloc(sizeof(struct ch343), GFP_KERNEL); |
---|
| 1562 | + if (ch343 == NULL) |
---|
| 1563 | + goto alloc_fail; |
---|
| 1564 | + |
---|
| 1565 | + ch343->idVendor = id->idVendor; |
---|
| 1566 | + ch343->idProduct = id->idProduct; |
---|
| 1567 | + ch343->iface = control_interface->cur_altsetting->desc.bInterfaceNumber / 2; |
---|
| 1568 | + |
---|
| 1569 | + minor = ch343_alloc_minor(ch343); |
---|
| 1570 | + if (minor < 0) { |
---|
| 1571 | + dev_err(&intf->dev, "no more free ch343 devices\n"); |
---|
| 1572 | + kfree(ch343); |
---|
| 1573 | + return -ENODEV; |
---|
| 1574 | + } |
---|
| 1575 | + |
---|
| 1576 | + ctrlsize = usb_endpoint_maxp(epctrl); |
---|
| 1577 | + readsize = usb_endpoint_maxp(epread); |
---|
| 1578 | + ch343->writesize = usb_endpoint_maxp(epwrite) * 20; |
---|
| 1579 | + ch343->control = control_interface; |
---|
| 1580 | + ch343->data = data_interface; |
---|
| 1581 | + ch343->minor = minor; |
---|
| 1582 | + ch343->dev = usb_dev; |
---|
| 1583 | + ch343->ctrlsize = ctrlsize; |
---|
| 1584 | + ch343->readsize = readsize; |
---|
| 1585 | + ch343->rx_buflimit = num_rx_buf; |
---|
| 1586 | + |
---|
| 1587 | + dev_dbg(&intf->dev, "ep%d ctrl: %d, ep%d read: %d, ep%d write: %d\n", usb_endpoint_num(epctrl), |
---|
| 1588 | + usb_endpoint_maxp(epctrl), usb_endpoint_num(epread), usb_endpoint_maxp(epread), usb_endpoint_num(epwrite), |
---|
| 1589 | + usb_endpoint_maxp(epwrite)); |
---|
| 1590 | + |
---|
| 1591 | + INIT_WORK(&ch343->work, ch343_softint); |
---|
| 1592 | + init_waitqueue_head(&ch343->wioctl); |
---|
| 1593 | + spin_lock_init(&ch343->write_lock); |
---|
| 1594 | + spin_lock_init(&ch343->read_lock); |
---|
| 1595 | + mutex_init(&ch343->mutex); |
---|
| 1596 | + ch343->rx_endpoint = usb_rcvbulkpipe(usb_dev, epread->bEndpointAddress); |
---|
| 1597 | + tty_port_init(&ch343->port); |
---|
| 1598 | + ch343->port.ops = &ch343_port_ops; |
---|
| 1599 | + init_usb_anchor(&ch343->delayed); |
---|
| 1600 | + ch343->quirks = quirks; |
---|
| 1601 | + |
---|
| 1602 | + buf = usb_alloc_coherent(usb_dev, ctrlsize, GFP_KERNEL, &ch343->ctrl_dma); |
---|
| 1603 | + if (!buf) |
---|
| 1604 | + goto alloc_fail2; |
---|
| 1605 | + ch343->ctrl_buffer = buf; |
---|
| 1606 | + |
---|
| 1607 | + if (ch343_write_buffers_alloc(ch343) < 0) |
---|
| 1608 | + goto alloc_fail4; |
---|
| 1609 | + |
---|
| 1610 | + ch343->ctrlurb = usb_alloc_urb(0, GFP_KERNEL); |
---|
| 1611 | + if (!ch343->ctrlurb) |
---|
| 1612 | + goto alloc_fail5; |
---|
| 1613 | + |
---|
| 1614 | + for (i = 0; i < num_rx_buf; i++) { |
---|
| 1615 | + struct ch343_rb *rb = &(ch343->read_buffers[i]); |
---|
| 1616 | + struct urb *urb; |
---|
| 1617 | + |
---|
| 1618 | + rb->base = usb_alloc_coherent(ch343->dev, readsize, GFP_KERNEL, &rb->dma); |
---|
| 1619 | + if (!rb->base) |
---|
| 1620 | + goto alloc_fail6; |
---|
| 1621 | + rb->index = i; |
---|
| 1622 | + rb->instance = ch343; |
---|
| 1623 | + |
---|
| 1624 | + urb = usb_alloc_urb(0, GFP_KERNEL); |
---|
| 1625 | + if (!urb) |
---|
| 1626 | + goto alloc_fail6; |
---|
| 1627 | + |
---|
| 1628 | + urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
---|
| 1629 | + urb->transfer_dma = rb->dma; |
---|
| 1630 | + usb_fill_bulk_urb(urb, ch343->dev, ch343->rx_endpoint, rb->base, ch343->readsize, ch343_read_bulk_callback, rb); |
---|
| 1631 | + |
---|
| 1632 | + ch343->read_urbs[i] = urb; |
---|
| 1633 | + __set_bit(i, &ch343->read_urbs_free); |
---|
| 1634 | + } |
---|
| 1635 | + for (i = 0; i < CH343_NW; i++) { |
---|
| 1636 | + struct ch343_wb *snd = &(ch343->wb[i]); |
---|
| 1637 | + |
---|
| 1638 | + snd->urb = usb_alloc_urb(0, GFP_KERNEL); |
---|
| 1639 | + if (snd->urb == NULL) |
---|
| 1640 | + goto alloc_fail7; |
---|
| 1641 | + |
---|
| 1642 | + usb_fill_bulk_urb(snd->urb, usb_dev, usb_sndbulkpipe(usb_dev, epwrite->bEndpointAddress), NULL, |
---|
| 1643 | + ch343->writesize, ch343_write_bulk, snd); |
---|
| 1644 | + snd->urb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
---|
| 1645 | + snd->instance = ch343; |
---|
| 1646 | + } |
---|
| 1647 | + |
---|
| 1648 | + usb_set_intfdata(intf, ch343); |
---|
| 1649 | + |
---|
| 1650 | + usb_fill_int_urb(ch343->ctrlurb, usb_dev, usb_rcvintpipe(usb_dev, epctrl->bEndpointAddress), ch343->ctrl_buffer, |
---|
| 1651 | + ctrlsize, ch343_ctrl_irq, ch343, epctrl->bInterval ? epctrl->bInterval : 16); |
---|
| 1652 | + ch343->ctrlurb->transfer_flags |= URB_NO_TRANSFER_DMA_MAP; |
---|
| 1653 | + ch343->ctrlurb->transfer_dma = ch343->ctrl_dma; |
---|
| 1654 | + |
---|
| 1655 | + dev_info(&intf->dev, "ttyXRUSB%d: usb to uart device\n", minor); |
---|
| 1656 | + |
---|
| 1657 | + usb_driver_claim_interface(&ch343_driver, data_interface, ch343); |
---|
| 1658 | + usb_set_intfdata(data_interface, ch343); |
---|
| 1659 | + usb_get_intf(control_interface); |
---|
| 1660 | + |
---|
| 1661 | + rv = ch343_configure(ch343); |
---|
| 1662 | + if (rv) |
---|
| 1663 | + goto alloc_fail7; |
---|
| 1664 | + |
---|
| 1665 | + if (ch343->iface == 0) { |
---|
| 1666 | + /* register the device now, as it is ready */ |
---|
| 1667 | + rv = usb_register_dev(intf, &ch343_class); |
---|
| 1668 | + if (rv) { |
---|
| 1669 | + /* error when registering this driver */ |
---|
| 1670 | + dev_err(&intf->dev, "Not able to get a minor for this device.\n"); |
---|
| 1671 | + } else { |
---|
| 1672 | + g_intf = intf; |
---|
| 1673 | + } |
---|
| 1674 | + } |
---|
| 1675 | + |
---|
| 1676 | + tty_dev = tty_port_register_device(&ch343->port, ch343_tty_driver, minor, &control_interface->dev); |
---|
| 1677 | + if (IS_ERR(tty_dev)) { |
---|
| 1678 | + rv = PTR_ERR(tty_dev); |
---|
| 1679 | + goto alloc_fail7; |
---|
| 1680 | + } |
---|
| 1681 | + |
---|
| 1682 | + return 0; |
---|
| 1683 | + |
---|
| 1684 | +alloc_fail7: |
---|
| 1685 | + usb_set_intfdata(intf, NULL); |
---|
| 1686 | + for (i = 0; i < CH343_NW; i++) |
---|
| 1687 | + usb_free_urb(ch343->wb[i].urb); |
---|
| 1688 | +alloc_fail6: |
---|
| 1689 | + for (i = 0; i < num_rx_buf; i++) |
---|
| 1690 | + usb_free_urb(ch343->read_urbs[i]); |
---|
| 1691 | + ch343_read_buffers_free(ch343); |
---|
| 1692 | + usb_free_urb(ch343->ctrlurb); |
---|
| 1693 | +alloc_fail5: |
---|
| 1694 | + ch343_write_buffers_free(ch343); |
---|
| 1695 | +alloc_fail4: |
---|
| 1696 | + usb_free_coherent(usb_dev, ctrlsize, ch343->ctrl_buffer, ch343->ctrl_dma); |
---|
| 1697 | +alloc_fail2: |
---|
| 1698 | + ch343_release_minor(ch343); |
---|
| 1699 | + kfree(ch343); |
---|
| 1700 | +alloc_fail: |
---|
| 1701 | + return rv; |
---|
| 1702 | +} |
---|
| 1703 | + |
---|
| 1704 | +static void stop_data_traffic(struct ch343 *ch343) |
---|
| 1705 | +{ |
---|
| 1706 | + int i; |
---|
| 1707 | + |
---|
| 1708 | + usb_kill_urb(ch343->ctrlurb); |
---|
| 1709 | + for (i = 0; i < CH343_NW; i++) |
---|
| 1710 | + usb_kill_urb(ch343->wb[i].urb); |
---|
| 1711 | + for (i = 0; i < ch343->rx_buflimit; i++) |
---|
| 1712 | + usb_kill_urb(ch343->read_urbs[i]); |
---|
| 1713 | + cancel_work_sync(&ch343->work); |
---|
| 1714 | +} |
---|
| 1715 | + |
---|
| 1716 | +static void ch343_disconnect(struct usb_interface *intf) |
---|
| 1717 | +{ |
---|
| 1718 | + struct ch343 *ch343 = usb_get_intfdata(intf); |
---|
| 1719 | + struct usb_device *usb_dev = interface_to_usbdev(intf); |
---|
| 1720 | + struct tty_struct *tty; |
---|
| 1721 | + int i; |
---|
| 1722 | + |
---|
| 1723 | + dev_dbg(&intf->dev, "%s\n", __func__); |
---|
| 1724 | + |
---|
| 1725 | + /* sibling interface is already cleaning up */ |
---|
| 1726 | + if (!ch343) |
---|
| 1727 | + return; |
---|
| 1728 | + |
---|
| 1729 | + /* give back minor */ |
---|
| 1730 | + if ((ch343->iface == 0) && (g_intf != NULL)) { |
---|
| 1731 | + usb_deregister_dev(g_intf, &ch343_class); |
---|
| 1732 | + } |
---|
| 1733 | + |
---|
| 1734 | + mutex_lock(&ch343->mutex); |
---|
| 1735 | + ch343->disconnected = true; |
---|
| 1736 | + wake_up_all(&ch343->wioctl); |
---|
| 1737 | + usb_set_intfdata(ch343->control, NULL); |
---|
| 1738 | + usb_set_intfdata(ch343->data, NULL); |
---|
| 1739 | + mutex_unlock(&ch343->mutex); |
---|
| 1740 | + |
---|
| 1741 | + tty = tty_port_tty_get(&ch343->port); |
---|
| 1742 | + if (tty) { |
---|
| 1743 | + tty_vhangup(tty); |
---|
| 1744 | + tty_kref_put(tty); |
---|
| 1745 | + } |
---|
| 1746 | + |
---|
| 1747 | + stop_data_traffic(ch343); |
---|
| 1748 | + |
---|
| 1749 | + tty_unregister_device(ch343_tty_driver, ch343->minor); |
---|
| 1750 | + |
---|
| 1751 | + usb_free_urb(ch343->ctrlurb); |
---|
| 1752 | + for (i = 0; i < CH343_NW; i++) |
---|
| 1753 | + usb_free_urb(ch343->wb[i].urb); |
---|
| 1754 | + for (i = 0; i < ch343->rx_buflimit; i++) |
---|
| 1755 | + usb_free_urb(ch343->read_urbs[i]); |
---|
| 1756 | + ch343_write_buffers_free(ch343); |
---|
| 1757 | + usb_free_coherent(usb_dev, ch343->ctrlsize, ch343->ctrl_buffer, ch343->ctrl_dma); |
---|
| 1758 | + ch343_read_buffers_free(ch343); |
---|
| 1759 | + |
---|
| 1760 | + usb_driver_release_interface(&ch343_driver, intf == ch343->control ? ch343->data : ch343->control); |
---|
| 1761 | + |
---|
| 1762 | + tty_port_put(&ch343->port); |
---|
| 1763 | + dev_info(&intf->dev, "%s\n", "ch343 usb device disconnect."); |
---|
| 1764 | +} |
---|
| 1765 | + |
---|
| 1766 | +#ifdef CONFIG_PM |
---|
| 1767 | +static int ch343_suspend(struct usb_interface *intf, pm_message_t message) |
---|
| 1768 | +{ |
---|
| 1769 | + struct ch343 *ch343 = usb_get_intfdata(intf); |
---|
| 1770 | + int cnt; |
---|
| 1771 | + |
---|
| 1772 | + dev_dbg(&intf->dev, "%s\n", __func__); |
---|
| 1773 | + |
---|
| 1774 | + spin_lock_irq(&ch343->write_lock); |
---|
| 1775 | + if (PMSG_IS_AUTO(message)) { |
---|
| 1776 | + if (ch343->transmitting) { |
---|
| 1777 | + spin_unlock_irq(&ch343->write_lock); |
---|
| 1778 | + return -EBUSY; |
---|
| 1779 | + } |
---|
| 1780 | + } |
---|
| 1781 | + cnt = ch343->susp_count++; |
---|
| 1782 | + spin_unlock_irq(&ch343->write_lock); |
---|
| 1783 | + |
---|
| 1784 | + if (cnt) |
---|
| 1785 | + return 0; |
---|
| 1786 | + |
---|
| 1787 | + stop_data_traffic(ch343); |
---|
| 1788 | + |
---|
| 1789 | + return 0; |
---|
| 1790 | +} |
---|
| 1791 | + |
---|
| 1792 | +static int ch343_resume(struct usb_interface *intf) |
---|
| 1793 | +{ |
---|
| 1794 | + struct ch343 *ch343 = usb_get_intfdata(intf); |
---|
| 1795 | + struct urb *urb; |
---|
| 1796 | + int rv = 0; |
---|
| 1797 | + |
---|
| 1798 | + dev_dbg(&intf->dev, "%s\n", __func__); |
---|
| 1799 | + |
---|
| 1800 | + spin_lock_irq(&ch343->write_lock); |
---|
| 1801 | + |
---|
| 1802 | + if (--ch343->susp_count) |
---|
| 1803 | + goto out; |
---|
| 1804 | + |
---|
| 1805 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) |
---|
| 1806 | + if (tty_port_initialized(&ch343->port)) { |
---|
| 1807 | +#else |
---|
| 1808 | + if (test_bit(ASYNCB_INITIALIZED, &ch343->port.flags)) { |
---|
| 1809 | +#endif |
---|
| 1810 | + rv = usb_submit_urb(ch343->ctrlurb, GFP_ATOMIC); |
---|
| 1811 | + |
---|
| 1812 | + for (;;) { |
---|
| 1813 | + urb = usb_get_from_anchor(&ch343->delayed); |
---|
| 1814 | + if (!urb) |
---|
| 1815 | + break; |
---|
| 1816 | + |
---|
| 1817 | + ch343_start_wb(ch343, urb->context); |
---|
| 1818 | + } |
---|
| 1819 | + |
---|
| 1820 | + /* |
---|
| 1821 | + * delayed error checking because we must |
---|
| 1822 | + * do the write path at all cost |
---|
| 1823 | + */ |
---|
| 1824 | + if (rv < 0) |
---|
| 1825 | + goto out; |
---|
| 1826 | + |
---|
| 1827 | + rv = ch343_submit_read_urbs(ch343, GFP_ATOMIC); |
---|
| 1828 | + } |
---|
| 1829 | +out: |
---|
| 1830 | + spin_unlock_irq(&ch343->write_lock); |
---|
| 1831 | + |
---|
| 1832 | + return rv; |
---|
| 1833 | +} |
---|
| 1834 | + |
---|
| 1835 | +static int ch343_reset_resume(struct usb_interface *intf) |
---|
| 1836 | +{ |
---|
| 1837 | + struct ch343 *ch343 = usb_get_intfdata(intf); |
---|
| 1838 | + |
---|
| 1839 | + dev_dbg(&intf->dev, "%s\n", __func__); |
---|
| 1840 | + |
---|
| 1841 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(4, 7, 0)) |
---|
| 1842 | + if (tty_port_initialized(&ch343->port)) |
---|
| 1843 | +#else |
---|
| 1844 | + if (test_bit(ASYNCB_INITIALIZED, &ch343->port.flags)) |
---|
| 1845 | +#endif |
---|
| 1846 | + tty_port_tty_hangup(&ch343->port, false); |
---|
| 1847 | + |
---|
| 1848 | + return ch343_resume(intf); |
---|
| 1849 | +} |
---|
| 1850 | + |
---|
| 1851 | +#endif /* CONFIG_PM */ |
---|
| 1852 | + |
---|
| 1853 | +/* |
---|
| 1854 | + * USB driver structure. |
---|
| 1855 | + */ |
---|
| 1856 | + |
---|
| 1857 | +static const struct usb_device_id ch343_ids[] = {{USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55D2, /* ch342 chip */ |
---|
| 1858 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1859 | + |
---|
| 1860 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55D3, /* ch343 chip */ |
---|
| 1861 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1862 | + |
---|
| 1863 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55D5, /* ch344 chip */ |
---|
| 1864 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1865 | + |
---|
| 1866 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55DA, /* ch347 chip mode0*/ |
---|
| 1867 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1868 | + |
---|
| 1869 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55DB, /* ch347 chip mode1*/ |
---|
| 1870 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1871 | + |
---|
| 1872 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55DD, /* ch347 chip mode3*/ |
---|
| 1873 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1874 | + |
---|
| 1875 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55D8, /* ch9101 chip */ |
---|
| 1876 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1877 | + |
---|
| 1878 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55D4, /* ch9102 chip */ |
---|
| 1879 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1880 | + |
---|
| 1881 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55D7, /* ch9103 chip */ |
---|
| 1882 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1883 | + |
---|
| 1884 | + {USB_DEVICE_INTERFACE_PROTOCOL(0x1a86, 0x55DF, /* ch9104 chip */ |
---|
| 1885 | + USB_CDC_ACM_PROTO_AT_V25TER)}, |
---|
| 1886 | + |
---|
| 1887 | + {}}; |
---|
| 1888 | + |
---|
| 1889 | +MODULE_DEVICE_TABLE(usb, ch343_ids); |
---|
| 1890 | + |
---|
| 1891 | +static struct usb_driver ch343_driver = { |
---|
| 1892 | + .name = "usb_ch343", |
---|
| 1893 | + .probe = ch343_probe, |
---|
| 1894 | + .disconnect = ch343_disconnect, |
---|
| 1895 | +#ifdef CONFIG_PM |
---|
| 1896 | + .suspend = ch343_suspend, |
---|
| 1897 | + .resume = ch343_resume, |
---|
| 1898 | + .reset_resume = ch343_reset_resume, |
---|
| 1899 | +#endif |
---|
| 1900 | + .id_table = ch343_ids, |
---|
| 1901 | +#ifdef CONFIG_PM |
---|
| 1902 | + .supports_autosuspend = 1, |
---|
| 1903 | +#endif |
---|
| 1904 | + .disable_hub_initiated_lpm = 1, |
---|
| 1905 | +}; |
---|
| 1906 | + |
---|
| 1907 | +/* |
---|
| 1908 | + * TTY driver structures. |
---|
| 1909 | + */ |
---|
| 1910 | +static const struct tty_operations ch343_ops = { |
---|
| 1911 | + .install = ch343_tty_install, |
---|
| 1912 | + .open = ch343_tty_open, |
---|
| 1913 | + .close = ch343_tty_close, |
---|
| 1914 | + .cleanup = ch343_tty_cleanup, |
---|
| 1915 | + .hangup = ch343_tty_hangup, |
---|
| 1916 | + .write = ch343_tty_write, |
---|
| 1917 | + .write_room = ch343_tty_write_room, |
---|
| 1918 | + .ioctl = ch343_tty_ioctl, |
---|
| 1919 | + .chars_in_buffer = ch343_tty_chars_in_buffer, |
---|
| 1920 | + .break_ctl = ch343_tty_break_ctl, |
---|
| 1921 | + .set_termios = ch343_tty_set_termios, |
---|
| 1922 | + .tiocmget = ch343_tty_tiocmget, |
---|
| 1923 | + .tiocmset = ch343_tty_tiocmset, |
---|
| 1924 | +}; |
---|
| 1925 | + |
---|
| 1926 | +/* |
---|
| 1927 | + * Init / exit. |
---|
| 1928 | + */ |
---|
| 1929 | +static int __init ch343_init(void) |
---|
| 1930 | +{ |
---|
| 1931 | + int retval; |
---|
| 1932 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) |
---|
| 1933 | + ch343_tty_driver = tty_alloc_driver(CH343_TTY_MINORS, TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV); |
---|
| 1934 | + if (IS_ERR(ch343_tty_driver)) |
---|
| 1935 | + return PTR_ERR(ch343_tty_driver); |
---|
| 1936 | +#else |
---|
| 1937 | + ch343_tty_driver = alloc_tty_driver(CH343_TTY_MINORS); |
---|
| 1938 | + if (!ch343_tty_driver) |
---|
| 1939 | + return -ENOMEM; |
---|
| 1940 | +#endif |
---|
| 1941 | + ch343_tty_driver->driver_name = "usbch343", ch343_tty_driver->name = "ttyXRUSB", |
---|
| 1942 | + ch343_tty_driver->major = CH343_TTY_MAJOR, ch343_tty_driver->minor_start = 0, |
---|
| 1943 | + ch343_tty_driver->type = TTY_DRIVER_TYPE_SERIAL, ch343_tty_driver->subtype = SERIAL_TYPE_NORMAL, |
---|
| 1944 | +#if (LINUX_VERSION_CODE < KERNEL_VERSION(5, 15, 0)) |
---|
| 1945 | + ch343_tty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_DYNAMIC_DEV; |
---|
| 1946 | +#endif |
---|
| 1947 | + ch343_tty_driver->init_termios = tty_std_termios; |
---|
| 1948 | + ch343_tty_driver->init_termios.c_cflag = B9600 | CS8 | CREAD | HUPCL | CLOCAL; |
---|
| 1949 | + tty_set_operations(ch343_tty_driver, &ch343_ops); |
---|
| 1950 | + |
---|
| 1951 | + retval = tty_register_driver(ch343_tty_driver); |
---|
| 1952 | + if (retval) { |
---|
| 1953 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) |
---|
| 1954 | + tty_driver_kref_put(ch343_tty_driver); |
---|
| 1955 | +#else |
---|
| 1956 | + put_tty_driver(ch343_tty_driver); |
---|
| 1957 | +#endif |
---|
| 1958 | + return retval; |
---|
| 1959 | + } |
---|
| 1960 | + |
---|
| 1961 | + retval = usb_register(&ch343_driver); |
---|
| 1962 | + if (retval) { |
---|
| 1963 | + tty_unregister_driver(ch343_tty_driver); |
---|
| 1964 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) |
---|
| 1965 | + tty_driver_kref_put(ch343_tty_driver); |
---|
| 1966 | +#else |
---|
| 1967 | + put_tty_driver(ch343_tty_driver); |
---|
| 1968 | +#endif |
---|
| 1969 | + return retval; |
---|
| 1970 | + } |
---|
| 1971 | + |
---|
| 1972 | + printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n"); |
---|
| 1973 | + printk(KERN_INFO KBUILD_MODNAME ": " VERSION_DESC "\n"); |
---|
| 1974 | + |
---|
| 1975 | + return 0; |
---|
| 1976 | +} |
---|
| 1977 | + |
---|
| 1978 | +static void __exit ch343_exit(void) |
---|
| 1979 | +{ |
---|
| 1980 | + usb_deregister(&ch343_driver); |
---|
| 1981 | + tty_unregister_driver(ch343_tty_driver); |
---|
| 1982 | +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(5, 15, 0)) |
---|
| 1983 | + tty_driver_kref_put(ch343_tty_driver); |
---|
| 1984 | +#else |
---|
| 1985 | + put_tty_driver(ch343_tty_driver); |
---|
| 1986 | +#endif |
---|
| 1987 | + idr_destroy(&ch343_minors); |
---|
| 1988 | + printk(KERN_INFO KBUILD_MODNAME |
---|
| 1989 | + ": " |
---|
| 1990 | + "ch343 driver exit.\n"); |
---|
| 1991 | +} |
---|
| 1992 | + |
---|
| 1993 | +module_init(ch343_init); |
---|
| 1994 | +module_exit(ch343_exit); |
---|
| 1995 | + |
---|
| 1996 | +MODULE_AUTHOR(DRIVER_AUTHOR); |
---|
| 1997 | +MODULE_DESCRIPTION(DRIVER_DESC); |
---|
| 1998 | +MODULE_VERSION(VERSION_DESC); |
---|
| 1999 | +MODULE_LICENSE("GPL"); |
---|
| 2000 | +MODULE_ALIAS_CHARDEV_MAJOR(CH343_TTY_MAJOR); |
---|