]> oss.titaniummirror.com Git - cp210x.git/blob - src/cp210x.c.karmic
Remove unnecessary file.
[cp210x.git] / src / cp210x.c.karmic
1 /*
2 * Silicon Laboratories CP210x USB to RS232 serial adaptor driver
3 *
4 * Copyright (C) 2005 Craig Shelley (craig@microtron.org.uk)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License version
8 * 2 as published by the Free Software Foundation.
9 *
10 * Support to set flow control line levels using TIOCMGET and TIOCMSET
11 * thanks to Karl Hiramoto karl@hiramoto.org. RTSCTS hardware flow
12 * control thanks to Munir Nassar nassarmu@real-time.com
13 *
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/errno.h>
18 #include <linux/slab.h>
19 #include <linux/tty.h>
20 #include <linux/tty_flip.h>
21 #include <linux/module.h>
22 #include <linux/moduleparam.h>
23 #include <linux/usb.h>
24 #include <linux/uaccess.h>
25 #include <linux/usb/serial.h>
26 #include "cp210x.h"
27
28 /*
29 * Version Information
30 */
31 #define DRIVER_VERSION "vxxPKGVERxxtmi"
32 #define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
33
34 /*
35 * Function Prototypes
36 */
37 static int cp210x_open(struct tty_struct *, struct usb_serial_port *,
38 struct file *);
39 static void cp210x_cleanup(struct usb_serial_port *);
40 static void cp210x_close(struct usb_serial_port *);
41 static void cp210x_get_termios(struct tty_struct *,
42 struct usb_serial_port *port);
43 static void cp210x_get_termios_port(struct usb_serial_port *port,
44 unsigned int *cflagp, unsigned int *baudp);
45 static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
46 struct ktermios*);
47 static int cp210x_tiocmget(struct tty_struct *, struct file *);
48 static int cp210x_tiocmset(struct tty_struct *, struct file *,
49 unsigned int, unsigned int);
50 static int cp210x_tiocmset_port(struct usb_serial_port *port, struct file *,
51 unsigned int, unsigned int);
52 static void cp210x_break_ctl(struct tty_struct *, int);
53 static int cp210x_startup(struct usb_serial *);
54 static void cp210x_disconnect(struct usb_serial *);
55 static int cp210x_ioctl(struct tty_struct *, struct file *,
56 unsigned int, unsigned long);
57
58 static int debug;
59
60 static const struct usb_device_id id_table[] = {
61 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
62 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */
63 { USB_DEVICE(0x0745, 0x1000) }, /* CipherLab USB CCD Barcode Scanner 1000 */
64 { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
65 { USB_DEVICE(0x08FD, 0x000A) }, /* Digianswer A/S , ZigBee/802.15.4 MAC Device */
66 { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
67 { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
68 { USB_DEVICE(0x0FCF, 0x1006) }, /* Dynastream ANT development board */
69 { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
70 { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
71 { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
72 { USB_DEVICE(0x10C4, 0x0F91) }, /* Vstabi */
73 { USB_DEVICE(0x10C4, 0x1101) }, /* Arkham Technology DS101 Bus Monitor */
74 { USB_DEVICE(0x10C4, 0x1601) }, /* Arkham Technology DS101 Adapter */
75 { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
76 { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
77 { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
78 { USB_DEVICE(0x10C4, 0x8054) }, /* Enfora GSM2228 */
79 { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
80 { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
81 { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
82 { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
83 { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
84 { USB_DEVICE(0x10C4, 0x8115) }, /* Arygon NFC/Mifare Reader */
85 { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
86 { USB_DEVICE(0x10C4, 0x813F) }, /* Tams Master Easy Control */
87 { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
88 { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
89 { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
90 { USB_DEVICE(0x10C4, 0x819F) }, /* MJS USB Toslink Switcher */
91 { USB_DEVICE(0x10C4, 0x81A6) }, /* ThinkOptics WavIt */
92 { USB_DEVICE(0x10C4, 0x81AC) }, /* MSD Dash Hawk */
93 { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
94 { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
95 { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
96 { USB_DEVICE(0x10C4, 0x81E8) }, /* Zephyr Bioharness */
97 { USB_DEVICE(0x10C4, 0x81F2) }, /* C1007 HF band RFID controller */
98 { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
99 { USB_DEVICE(0x10C4, 0x822B) }, /* Modem EDGE(GSM) Comander 2 */
100 { USB_DEVICE(0x10C4, 0x826B) }, /* Cygnal Integrated Products, Inc., Fasttrax GPS demostration module */
101 { USB_DEVICE(0x10C4, 0x8293) }, /* Telegesys ETRX2USB */
102 { USB_DEVICE(0x10C4, 0x82F9) }, /* Procyon AVS */
103 { USB_DEVICE(0x10C4, 0x8341) }, /* Siemens MC35PU GPRS Modem */
104 { USB_DEVICE(0x10C4, 0x8382) }, /* Cygnal Integrated Products, Inc. */
105 { USB_DEVICE(0x10C4, 0x83A8) }, /* Amber Wireless AMB2560 */
106 { USB_DEVICE(0x10C4, 0x8411) }, /* Kyocera GPS Module */
107 { USB_DEVICE(0x10C4, 0x846E) }, /* BEI USB Sensor Interface (VCP) */
108 { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
109 { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
110 { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
111 { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
112 { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
113 { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
114 { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
115 { USB_DEVICE(0x10CE, 0xEA6A) }, /* Silicon Labs MobiData GPRS USB Modem 100EU */
116 { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
117 { USB_DEVICE(0x1555, 0x0004) }, /* Owen AC4 USB-RS485 Converter */
118 { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
119 { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
120 { USB_DEVICE(0x18EF, 0xE00F) }, /* ELV USB-I2C-Interface */
121 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
122 { } /* Terminating Entry */
123 };
124
125 MODULE_DEVICE_TABLE(usb, id_table);
126
127 static struct usb_driver cp210x_driver = {
128 .name = "cp210x",
129 .probe = usb_serial_probe,
130 .disconnect = usb_serial_disconnect,
131 .id_table = id_table,
132 .no_dynamic_id = 1,
133 };
134
135 static struct usb_serial_driver cp210x_device = {
136 .driver = {
137 .owner = THIS_MODULE,
138 .name = "cp210x",
139 },
140 .usb_driver = &cp210x_driver,
141 .id_table = id_table,
142 .num_ports = 1,
143 .open = cp210x_open,
144 .close = cp210x_close,
145 .ioctl = cp210x_ioctl,
146 .break_ctl = cp210x_break_ctl,
147 .set_termios = cp210x_set_termios,
148 .tiocmget = cp210x_tiocmget,
149 .tiocmset = cp210x_tiocmset,
150 .attach = cp210x_startup,
151 .disconnect = cp210x_disconnect,
152 };
153
154 /* Config request types */
155 #define REQTYPE_HOST_TO_DEVICE 0x41
156 #define REQTYPE_DEVICE_TO_HOST 0xc1
157
158 /* Config request codes */
159 #define CP210X_IFC_ENABLE 0x00
160 #define CP210X_SET_BAUDDIV 0x01
161 #define CP210X_GET_BAUDDIV 0x02
162 #define CP210X_SET_LINE_CTL 0x03
163 #define CP210X_GET_LINE_CTL 0x04
164 #define CP210X_SET_BREAK 0x05
165 #define CP210X_IMM_CHAR 0x06
166 #define CP210X_SET_MHS 0x07
167 #define CP210X_GET_MDMSTS 0x08
168 #define CP210X_SET_XON 0x09
169 #define CP210X_SET_XOFF 0x0A
170 #define CP210X_SET_EVENTMASK 0x0B
171 #define CP210X_GET_EVENTMASK 0x0C
172 #define CP210X_SET_CHAR 0x0D
173 #define CP210X_GET_CHARS 0x0E
174 #define CP210X_GET_PROPS 0x0F
175 #define CP210X_GET_COMM_STATUS 0x10
176 #define CP210X_RESET 0x11
177 #define CP210X_PURGE 0x12
178 #define CP210X_SET_FLOW 0x13
179 #define CP210X_GET_FLOW 0x14
180 #define CP210X_EMBED_EVENTS 0x15
181 #define CP210X_GET_EVENTSTATE 0x16
182 #define CP210X_SET_CHARS 0x19
183
184 /* CP210X_IFC_ENABLE */
185 #define UART_ENABLE 0x0001
186 #define UART_DISABLE 0x0000
187
188 /* CP210X_(SET|GET)_BAUDDIV */
189 #define BAUD_RATE_GEN_FREQ 0x384000
190
191 /* CP210X_(SET|GET)_LINE_CTL */
192 #define BITS_DATA_MASK 0X0f00
193 #define BITS_DATA_5 0X0500
194 #define BITS_DATA_6 0X0600
195 #define BITS_DATA_7 0X0700
196 #define BITS_DATA_8 0X0800
197 #define BITS_DATA_9 0X0900
198
199 #define BITS_PARITY_MASK 0x00f0
200 #define BITS_PARITY_NONE 0x0000
201 #define BITS_PARITY_ODD 0x0010
202 #define BITS_PARITY_EVEN 0x0020
203 #define BITS_PARITY_MARK 0x0030
204 #define BITS_PARITY_SPACE 0x0040
205
206 #define BITS_STOP_MASK 0x000f
207 #define BITS_STOP_1 0x0000
208 #define BITS_STOP_1_5 0x0001
209 #define BITS_STOP_2 0x0002
210
211 /* CP210X_SET_BREAK */
212 #define BREAK_ON 0x0000
213 #define BREAK_OFF 0x0001
214
215 /* CP210X_(SET_MHS|GET_MDMSTS) */
216 #define CONTROL_DTR 0x0001
217 #define CONTROL_RTS 0x0002
218 #define CONTROL_CTS 0x0010
219 #define CONTROL_DSR 0x0020
220 #define CONTROL_RING 0x0040
221 #define CONTROL_DCD 0x0080
222 #define CONTROL_WRITE_DTR 0x0100
223 #define CONTROL_WRITE_RTS 0x0200
224
225 /* cp210x_get_partnum() return codes */
226 #define CP210x_PART_UNKNOWN 0x00
227 #define CP210x_PART_CP2101 0x01
228 #define CP210x_PART_CP2102 0x02
229 #define CP210x_PART_CP2103 0x03
230
231 /* Helper to make usb string size */
232 #define USBSTRLEN(x) (x * 2 + 2)
233
234 /* Populates usbstr with: (len) + (0x03) + unicode(str). Each char in str
235 * takes two bytes in unicode format.
236 * Returns the resulting length of the string in usbstr.
237 * This function can accept overlapping usbstr and str as long as the overlap
238 * does not cause data written to usbstr to overwrite data not yet read from
239 * str.
240 */
241 static int make_usb_string(char *usbstr, size_t usblen, char *src,
242 size_t srclen)
243 {
244 int len = 0;
245
246 if (usbstr && usblen >= 2 && src && *src && srclen) {
247 char *p;
248
249 if (usblen > 255)
250 usblen = 255;
251
252 p = usbstr + 1;
253 *p++ = 0x03;
254 len = 2;
255 while (srclen && len < usblen) {
256 *p++ = *src++;
257 *p++ = 0;
258 len += 2;
259 srclen--;
260 }
261 *usbstr = (char)len;
262 }
263 return len;
264 }
265
266 /*
267 * cp210x_usbstr_from_user
268 * Populate kbuf with a usb string derived from a user space variable. klen
269 * is the size of the buffer at kbuf.
270 * Returns the number of bytes used in kbuf.
271 */
272 static size_t cp210x_usbstr_from_user(char *kbuf, unsigned long ubuf, size_t klen)
273 {
274 struct cp210x_buffer t;
275 char* str;
276 size_t slen;
277
278 if (!kbuf || !ubuf || !klen)
279 return 0;
280 if (copy_from_user(&t, (struct cp210x_buffer __user *)ubuf, sizeof(t)))
281 return 0;
282 if (!t.buf || !t.len || USBSTRLEN(t.len) > klen)
283 return 0;
284 slen = (klen - 2) / 2;
285 if (t.len < slen)
286 slen = t.len;
287 str = kbuf + klen - slen;
288 if (copy_from_user(str, (u8 __user *)t.buf, slen))
289 return 0;
290 return make_usb_string(kbuf, klen, str, slen);
291 }
292
293 /* cp210x_has_setmfg
294 * Returns 1 if the CP210X part includes firmware that allows setting the
295 * USB MFG descriptor, else 0. As of this writing, no CP210X firmware allows
296 * this. SiLabs has suggested this may change in future firmware versions or
297 * parts.
298 */
299 static inline int cp210x_has_setmfg(void)
300 {
301 return 0;
302 }
303
304 /*
305 * cp210x_ctlmsg
306 * A generic usb control message interface.
307 * Returns the actual size of the data read or written within the message, 0
308 * if no data were read or written, or a negative value to indicate an error.
309 */
310 static int cp210x_ctlmsg(struct usb_serial_port *port, u8 request,
311 u8 requestype, u16 value, u16 index, void *data, u16 size)
312 {
313 struct usb_device *dev = port->serial->dev;
314 u8 *tbuf;
315 int ret;
316
317 if (!(tbuf = kmalloc(size, GFP_KERNEL)))
318 return -ENOMEM;
319 if (requestype & 0x80) {
320 ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), request,
321 requestype, value, index, tbuf, size, 300);
322 if (ret > 0 && size)
323 memcpy(data, tbuf, size);
324 } else {
325 if (size)
326 memcpy(tbuf, data, size);
327 ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), request,
328 requestype, value, index, tbuf, size, 300);
329 }
330 kfree(tbuf);
331 if (ret < 0 && ret != -EPIPE) {
332 dev_printk(KERN_DEBUG, &dev->dev, "cp210x: control failed cmd rqt %u "
333 "rq %u len %u ret %d\n", requestype, request, size, ret);
334 }
335 return ret;
336 }
337
338 static int cp210x_reset(struct usb_serial_port *port)
339 {
340 dbg("%s", __func__);
341
342 #if 1
343 /* Is this better than usb_device_reset? It may be. Once a client issues
344 * the reset ioctl, it must disconnect and reconnect, since the USB
345 * connections are torn down. We also ignore the error return, since
346 * the part resets and doesn't send one...
347 */
348 cp210x_ctlmsg(port, 0xff, 0x40, 0x0008, 0x00, NULL, 0);
349 #else
350 usb_reset_device(port->serial->dev);
351 #endif
352 return 0;
353 }
354
355 static int cp210x_get_partnum(struct usb_serial_port *port)
356 {
357 static u8 _partnum = CP210x_PART_UNKNOWN;
358
359 if (_partnum == CP210x_PART_UNKNOWN) {
360 u8 addr = port->serial->dev->actconfig->interface[0]->
361 cur_altsetting->endpoint[0].desc.bEndpointAddress &
362 USB_ENDPOINT_NUMBER_MASK;
363
364 if (addr == 0x03 || addr == 0x02)
365 _partnum = CP210x_PART_CP2101;
366 else if (addr == 0x01) {
367 /* Must query part to determine part number */
368 if (cp210x_ctlmsg(port, 0xff, 0xc0, 0x370b, 0x00,
369 &_partnum, 1) != 1)
370 _partnum = CP210x_PART_UNKNOWN;
371 }
372 }
373 dbg("%s - partnum %u", __func__, _partnum);
374 return _partnum;
375 }
376
377 static inline int cp210x_setu16(struct usb_serial_port *port, int cmd,
378 unsigned int value)
379 {
380 return cp210x_ctlmsg(port, 0xff, 0x40, 0x3700 | (cmd & 0xff),
381 value, NULL, 0);
382 }
383
384 /*
385 * cp210x_setstr
386 *
387 * Set a USB string descriptor using proprietary cp210x control messages.
388 * Return the number of characters actually written.
389 */
390 static int cp210x_setstr(struct usb_serial_port *port, int cmd, char *usbstr)
391 {
392 unsigned len = usbstr[0];
393 int ret = cp210x_ctlmsg(port, 0xff, 0x40, 0x3700 | (cmd & 0xff), 0,
394 usbstr, len);
395 dbg("%s - cmd 0x%02x len %d ret %d", __func__, cmd, len, ret);
396 return ret;
397 }
398
399 /* Set all gpio simultaneously */
400 static int cp210x_gpioset(struct usb_serial_port *port, u8 gpio)
401 {
402 dbg("%s - port %d, gpio = 0x%.2x", __func__, port->number, gpio);
403
404 return cp210x_ctlmsg(port, 0xff, 0x40, 0x37e1,
405 ((uint16_t)gpio << 8) | GPIO_MASK, NULL, 0);
406 }
407
408 /* Set select gpio bits */
409 static int cp210x_gpiosetb(struct usb_serial_port *port, u8 set, u8 clear)
410 {
411 u16 gpio = 0;
412
413 /* The bitmask is in the LSB, the values in the MSB */
414 if (set & GPIO_0)
415 gpio |= (GPIO_0 << 8)|GPIO_0;
416 if (set & GPIO_1)
417 gpio |= (GPIO_1 << 8)|GPIO_1;
418 if (set & GPIO_2)
419 gpio |= (GPIO_2 << 8)|GPIO_2;
420 if (set & GPIO_3)
421 gpio |= (GPIO_3 << 8)|GPIO_3;
422 if (clear & GPIO_0)
423 gpio = (gpio & ~(GPIO_0 << 8))|GPIO_0;
424 if (clear & GPIO_1)
425 gpio = (gpio & ~(GPIO_1 << 8))|GPIO_1;
426 if (clear & GPIO_2)
427 gpio = (gpio & ~(GPIO_2 << 8))|GPIO_2;
428 if (clear & GPIO_3)
429 gpio = (gpio & ~(GPIO_3 << 8))|GPIO_3;
430
431 dbg("%s - port %d, gpiob = 0x%.4x", __func__, port->number, gpio);
432
433 /* FIXME: how about REQTYPE_HOST_TO_DEVICE instead of 0x40? */
434 return cp210x_ctlmsg(port, 0xff, 0x40, 0x37e1, gpio, NULL, 0);
435 }
436
437 static int cp210x_gpioget(struct usb_serial_port *port, u8 *gpio)
438 {
439 int ret;
440
441 dbg("%s - port %d", __func__, port->number);
442
443 /* FIXME: how about REQTYPE_DEVICE_TO_HOST instead of 0xc0? */
444 ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x00c2, 0, gpio, 1);
445
446 dbg("%s - gpio = 0x%.2x (%d)", __func__, *gpio, ret);
447
448 return (ret == 1) ? 0 : -1;
449 }
450
451 static int cp210x_portconfset(struct usb_serial_port *port,
452 struct cp210x_port_config *config)
453 {
454 struct cp210x_port_config lconfig;
455 int ret;
456
457 dbg("%s", __func__);
458
459 memcpy(&lconfig, config, sizeof(lconfig));
460
461 /* Words from cp2103 are MSB */
462 lconfig.reset.mode = cpu_to_be16(config->reset.mode);
463 lconfig.reset.latch = cpu_to_be16(config->reset.latch);
464 lconfig.suspend.mode = cpu_to_be16(config->suspend.mode);
465 lconfig.suspend.latch = cpu_to_be16(config->suspend.latch);
466 #if 0 /* apparently not implemented by CP210X firmware */
467 lconfig.reset.lowPower = cpu_to_be16(config->reset.lowPower);
468 lconfig.suspend.lowPower = cpu_to_be16(config->suspend.lowPower);
469 #else
470 lconfig.suspend.lowPower = 0;
471 lconfig.reset.lowPower = 0;
472 #endif
473
474 ret = cp210x_ctlmsg(port, 0xff, 0x40, 0x370c, 0, &lconfig,
475 sizeof(struct cp210x_port_config));
476 if (ret == sizeof(struct cp210x_port_config))
477 return 0;
478 else if (ret >= 0)
479 return -1;
480 else
481 return ret;
482 }
483
484 static int cp210x_portconfget(struct usb_serial_port *port,
485 struct cp210x_port_config *config)
486 {
487 int ret;
488
489 dbg("%s", __func__);
490
491 ret = cp210x_ctlmsg(port, 0xff, 0xc0, 0x370c, 0, config,
492 sizeof(struct cp210x_port_config));
493 if (ret == sizeof(struct cp210x_port_config)) {
494 /* Words from cp2103 are MSB */
495 config->reset.mode = be16_to_cpu(config->reset.mode);
496 config->reset.lowPower = be16_to_cpu(config->reset.lowPower);
497 config->reset.latch = be16_to_cpu(config->reset.latch);
498 config->suspend.mode = be16_to_cpu(config->suspend.mode);
499 config->suspend.lowPower =
500 be16_to_cpu(config->suspend.lowPower);
501 config->suspend.latch = be16_to_cpu(config->suspend.latch);
502
503 /* apparently not implemented yet */
504 config->reset.lowPower = 0;
505 config->suspend.lowPower = 0;
506
507 return 0;
508 } else if (ret >= 0)
509 return -1;
510 else
511 return ret;
512
513 }
514
515 /*
516 * cp210x_get_config
517 * Reads from the CP210x configuration registers
518 * 'size' is specified in bytes.
519 * 'data' is a pointer to a pre-allocated array of integers large
520 * enough to hold 'size' bytes (with 4 bytes to each integer)
521 */
522 static int cp210x_get_config(struct usb_serial_port *port, u8 request,
523 unsigned int *data, int size)
524 {
525 struct usb_serial *serial = port->serial;
526 __le32 *buf;
527 int result, i, length;
528
529 /* Number of integers required to contain the array */
530 length = (((size - 1) | 3) + 1)/4;
531
532 buf = kcalloc(length, sizeof(__le32), GFP_KERNEL);
533 if (!buf) {
534 dev_err(&port->dev, "%s - out of memory.\n", __func__);
535 return -ENOMEM;
536 }
537
538 /* Issue the request, attempting to read 'size' bytes */
539 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
540 request, REQTYPE_DEVICE_TO_HOST, 0x0000,
541 0, buf, size, 300);
542
543 /* Convert data into an array of integers */
544 for (i = 0; i < length; i++)
545 data[i] = le32_to_cpu(buf[i]);
546
547 kfree(buf);
548
549 if (result != size) {
550 dbg("%s - Unable to send config request, "
551 "request=0x%x size=%d result=%d\n",
552 __func__, request, size, result);
553 return -EPROTO;
554 }
555
556 return 0;
557 }
558
559 /*
560 * cp210x_set_config
561 * Writes to the CP210x configuration registers
562 * Values less than 16 bits wide are sent directly
563 * 'size' is specified in bytes.
564 */
565 static int cp210x_set_config(struct usb_serial_port *port, u8 request,
566 unsigned int *data, int size)
567 {
568 struct usb_serial *serial = port->serial;
569 __le32 *buf;
570 int result, i, length;
571
572 /* Number of integers required to contain the array */
573 length = (((size - 1) | 3) + 1)/4;
574
575 buf = kmalloc(length * sizeof(__le32), GFP_KERNEL);
576 if (!buf) {
577 dev_err(&port->dev, "%s - out of memory.\n",
578 __func__);
579 return -ENOMEM;
580 }
581
582 /* Array of integers into bytes */
583 for (i = 0; i < length; i++)
584 buf[i] = cpu_to_le32(data[i]);
585
586 if (size > 2) {
587 result = usb_control_msg(serial->dev,
588 usb_sndctrlpipe(serial->dev, 0),
589 request, REQTYPE_HOST_TO_DEVICE, 0x0000,
590 0, buf, size, 300);
591 } else {
592 result = usb_control_msg(serial->dev,
593 usb_sndctrlpipe(serial->dev, 0),
594 request, REQTYPE_HOST_TO_DEVICE, data[0],
595 0, NULL, 0, 300);
596 }
597
598 kfree(buf);
599
600 if ((size > 2 && result != size) || result < 0) {
601 dbg("%s - Unable to send request, "
602 "request=0x%x size=%d result=%d\n",
603 __func__, request, size, result);
604 return -EPROTO;
605 }
606
607 return 0;
608 }
609
610 /*
611 * cp210x_set_config_single
612 * Convenience function for calling cp210x_set_config on single data values
613 * without requiring an integer pointer
614 */
615 static inline int cp210x_set_config_single(struct usb_serial_port *port,
616 u8 request, unsigned int data)
617 {
618 return cp210x_set_config(port, request, &data, 2);
619 }
620
621 /*
622 * cp210x_quantise_baudrate
623 * Quantises the baud rate as per AN205 Table 1
624 */
625 static unsigned int cp210x_quantise_baudrate(unsigned int baud) {
626 if (baud <= 56) baud = 0;
627 else if (baud <= 300) baud = 300;
628 else if (baud <= 600) baud = 600;
629 else if (baud <= 1200) baud = 1200;
630 else if (baud <= 1800) baud = 1800;
631 else if (baud <= 2400) baud = 2400;
632 else if (baud <= 4000) baud = 4000;
633 else if (baud <= 4803) baud = 4800;
634 else if (baud <= 7207) baud = 7200;
635 else if (baud <= 9612) baud = 9600;
636 else if (baud <= 14428) baud = 14400;
637 else if (baud <= 16062) baud = 16000;
638 else if (baud <= 19250) baud = 19200;
639 else if (baud <= 28912) baud = 28800;
640 else if (baud <= 38601) baud = 38400;
641 else if (baud <= 51558) baud = 51200;
642 else if (baud <= 56280) baud = 56000;
643 else if (baud <= 58053) baud = 57600;
644 else if (baud <= 64111) baud = 64000;
645 else if (baud <= 77608) baud = 76800;
646 else if (baud <= 117028) baud = 115200;
647 else if (baud <= 129347) baud = 128000;
648 else if (baud <= 156868) baud = 153600;
649 else if (baud <= 237832) baud = 230400;
650 else if (baud <= 254234) baud = 250000;
651 else if (baud <= 273066) baud = 256000;
652 else if (baud <= 491520) baud = 460800;
653 else if (baud <= 567138) baud = 500000;
654 else if (baud <= 670254) baud = 576000;
655 else if (baud <= 1053257) baud = 921600;
656 else if (baud <= 1474560) baud = 1228800;
657 else if (baud <= 2457600) baud = 1843200;
658 else baud = 3686400;
659 return baud;
660 }
661
662 static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *port,
663 struct file *filp)
664 {
665 struct usb_serial *serial = port->serial;
666 int result;
667
668 dbg("%s - port %d", __func__, port->number);
669
670 if (cp210x_set_config_single(port, CP210X_IFC_ENABLE, UART_ENABLE)) {
671 dev_err(&port->dev, "%s - Unable to enable UART\n",
672 __func__);
673 return -EPROTO;
674 }
675
676 /* Start reading from the device */
677 usb_fill_bulk_urb(port->read_urb, serial->dev,
678 usb_rcvbulkpipe(serial->dev,
679 port->bulk_in_endpointAddress),
680 port->read_urb->transfer_buffer,
681 port->read_urb->transfer_buffer_length,
682 serial->type->read_bulk_callback,
683 port);
684 result = usb_submit_urb(port->read_urb, GFP_KERNEL);
685 if (result) {
686 dev_err(&port->dev, "%s - failed resubmitting read urb, "
687 "error %d\n", __func__, result);
688 return result;
689 }
690
691 /* Configure the termios structure */
692 cp210x_get_termios(tty, port);
693
694 /* Set the DTR and RTS pins low */
695 cp210x_tiocmset_port(tty ? (struct usb_serial_port *) tty->driver_data
696 : port,
697 NULL, TIOCM_DTR | TIOCM_RTS, 0);
698
699 return 0;
700 }
701
702 static void cp210x_cleanup(struct usb_serial_port *port)
703 {
704 struct usb_serial *serial = port->serial;
705
706 dbg("%s - port %d", __func__, port->number);
707
708 if (serial->dev) {
709 /* shutdown any bulk reads that might be going on */
710 if (serial->num_bulk_out)
711 usb_kill_urb(port->write_urb);
712 if (serial->num_bulk_in)
713 usb_kill_urb(port->read_urb);
714 }
715 }
716
717 static void cp210x_close(struct usb_serial_port *port)
718 {
719 dbg("%s - port %d", __func__, port->number);
720
721 /* shutdown our urbs */
722 dbg("%s - shutting down urbs", __func__);
723 usb_kill_urb(port->write_urb);
724 usb_kill_urb(port->read_urb);
725
726 mutex_lock(&port->serial->disc_mutex);
727 if (!port->serial->disconnected)
728 cp210x_set_config_single(port, CP210X_IFC_ENABLE, UART_DISABLE);
729 mutex_unlock(&port->serial->disc_mutex);
730 }
731
732 static int cp210x_ioctl(struct tty_struct *tty, struct file *file,
733 unsigned int cmd, unsigned long arg)
734 {
735 struct usb_serial_port *port = tty->driver_data;
736
737 dbg("%s (%d) cmd = 0x%04x", __func__, port->number, cmd);
738
739 switch (cmd) {
740
741 case TIOCMGET:
742 {
743 int result = cp210x_tiocmget(tty, file);
744 if (copy_to_user((int __user *)&arg, &result, sizeof(int)))
745 return -EFAULT;
746 return 0;
747 }
748 break;
749
750 case TIOCMSET:
751 case TIOCMBIS:
752 case TIOCMBIC:
753 {
754 int val = 0;
755
756 if (copy_from_user(&val, (int __user *)&arg, sizeof(int)))
757 return -EFAULT;
758
759 /* this looks wrong: TIOCMSET isn't going to work right */
760 if (cp210x_tiocmset(tty, file, cmd==TIOCMBIC ? 0 : val,
761 cmd==TIOCMBIC ? val : 0))
762 return -EFAULT;
763 return 0;
764 }
765 break;
766
767 case CP210x_IOCTL_GPIOGET:
768 if (cp210x_get_partnum(port) == CP210x_PART_CP2103) {
769 u8 gpio = 0;
770 if (!cp210x_gpioget(port, &gpio) && !copy_to_user(
771 (u8 __user *)arg, &gpio, sizeof(gpio)))
772 return 0;
773 }
774 return -EFAULT;
775 break;
776
777 case CP210x_IOCTL_GPIOSET:
778 if (cp210x_get_partnum(port) == CP210x_PART_CP2103 &&
779 !cp210x_gpioset(port, arg))
780 return 0;
781 return -EFAULT;
782 break;
783
784 case CP210x_IOCTL_GPIOBIC:
785 case CP210x_IOCTL_GPIOBIS:
786 if (cp210x_get_partnum(port) == CP210x_PART_CP2103 &&
787 !cp210x_gpiosetb(port,
788 (cmd==CP210x_IOCTL_GPIOBIC) ? 0 : arg,
789 (cmd==CP210x_IOCTL_GPIOBIC) ? arg : 0))
790 return 0;
791 return -EFAULT;
792 break;
793
794 case CP210x_IOCTL_DEVICERESET:
795 return cp210x_reset(port);
796 break;
797
798 case CP210x_IOCTL_PORTCONFGET:
799 {
800 struct cp210x_port_config config;
801 if (!cp210x_portconfget(port, &config) && !copy_to_user(
802 (struct cp210x_port_config __user *)arg,
803 &config, sizeof(config)))
804 return 0;
805 }
806 return -EFAULT;
807 break;
808
809 case CP210x_IOCTL_PORTCONFSET:
810 {
811 struct cp210x_port_config config;
812 if (!copy_from_user(&config, (struct cp210x_port_config __user *)arg,
813 sizeof(config)) &&
814 !cp210x_portconfset(port, &config))
815 return 0;
816 return -EFAULT;
817 }
818 break;
819
820 case CP210x_IOCTL_SETVID:
821 {
822 unsigned int vid;
823 if (!copy_from_user(&vid, (unsigned int __user *)arg,
824 sizeof(unsigned int)) &&
825 !cp210x_setu16(port, 0x01, vid))
826 return 0;
827 return -EFAULT;
828 }
829 break;
830
831 case CP210x_IOCTL_SETPID:
832 {
833 unsigned int pid;
834 if (!copy_from_user(&pid, (unsigned int __user *)arg,
835 sizeof(unsigned int)) &&
836 !cp210x_setu16(port, 0x02, pid))
837 return 0;
838 return -EFAULT;
839 }
840 break;
841
842 case CP210x_IOCTL_SETMFG:
843 if (cp210x_has_setmfg()) {
844 char usbstr[USBSTRLEN(CP210x_MAX_MFG_STRLEN)];
845 size_t len = cp210x_usbstr_from_user(usbstr, arg,
846 sizeof(usbstr));
847 if (len && cp210x_setstr(port, 0x00, usbstr) == len)
848 return 0;
849 }
850 return -EFAULT;
851 break;
852
853 case CP210x_IOCTL_SETPRODUCT:
854 {
855 char usbstr[USBSTRLEN(CP210x_MAX_PRODUCT_STRLEN)];
856 size_t len = cp210x_usbstr_from_user(usbstr, arg,
857 sizeof(usbstr));
858 if (len && cp210x_setstr(port, 0x03, usbstr) == len)
859 return 0;
860 return -EFAULT;
861 }
862 break;
863
864 case CP210x_IOCTL_SETSERIAL:
865 {
866 char usbstr[USBSTRLEN(CP210x_MAX_SERIAL_STRLEN)];
867 size_t len = cp210x_usbstr_from_user(usbstr, arg,
868 sizeof(usbstr));
869 if (len && cp210x_setstr(port, 0x04, usbstr) == len)
870 return 0;
871 return -EFAULT;
872 }
873 break;
874
875 case CP210x_IOCTL_SETDEVVER:
876 {
877 unsigned int ver;
878 if (!copy_from_user(&ver, (unsigned int __user *)arg,
879 sizeof(unsigned int)) &&
880 !cp210x_setu16(port, 0x07, ver))
881 return 0;
882 return -EFAULT;
883 }
884 break;
885
886 default:
887 dbg("%s not supported = 0x%04x", __func__, cmd);
888 break;
889 }
890
891 return -ENOIOCTLCMD;
892 }
893
894 /*
895 * cp210x_get_termios
896 * Reads the baud rate, data bits, parity, stop bits and flow control mode
897 * from the device, corrects any unsupported values, and configures the
898 * termios structure to reflect the state of the device
899 */
900 static void cp210x_get_termios(struct tty_struct *tty,
901 struct usb_serial_port *port)
902 {
903 unsigned int baud;
904
905 if (tty) {
906 cp210x_get_termios_port(tty->driver_data,
907 &tty->termios->c_cflag, &baud);
908 tty_encode_baud_rate(tty, baud, baud);
909 }
910
911 else {
912 unsigned int cflag;
913 cflag = 0;
914 cp210x_get_termios_port(port, &cflag, &baud);
915 }
916 }
917
918 /*
919 * cp210x_get_termios_port
920 * This is the heart of cp210x_get_termios which always uses a &usb_serial_port.
921 */
922 static void cp210x_get_termios_port(struct usb_serial_port *port,
923 unsigned int *cflagp, unsigned int *baudp)
924 {
925 unsigned int cflag, modem_ctl[4];
926 unsigned int baud;
927 unsigned int bits;
928
929 dbg("%s - port %d", __func__, port->number);
930
931 cp210x_get_config(port, CP210X_GET_BAUDDIV, &baud, 2);
932 /* Convert to baudrate */
933 if (baud)
934 baud = cp210x_quantise_baudrate((BAUD_RATE_GEN_FREQ + baud/2)/ baud);
935
936 dbg("%s - baud rate = %d", __func__, baud);
937 *baudp = baud;
938
939 cflag = *cflagp;
940
941 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
942 cflag &= ~CSIZE;
943 switch (bits & BITS_DATA_MASK) {
944 case BITS_DATA_5:
945 dbg("%s - data bits = 5", __func__);
946 cflag |= CS5;
947 break;
948 case BITS_DATA_6:
949 dbg("%s - data bits = 6", __func__);
950 cflag |= CS6;
951 break;
952 case BITS_DATA_7:
953 dbg("%s - data bits = 7", __func__);
954 cflag |= CS7;
955 break;
956 case BITS_DATA_8:
957 dbg("%s - data bits = 8", __func__);
958 cflag |= CS8;
959 break;
960 case BITS_DATA_9:
961 dbg("%s - data bits = 9 (not supported, using 8 data bits)",
962 __func__);
963 cflag |= CS8;
964 bits &= ~BITS_DATA_MASK;
965 bits |= BITS_DATA_8;
966 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
967 break;
968 default:
969 dbg("%s - Unknown number of data bits, using 8", __func__);
970 cflag |= CS8;
971 bits &= ~BITS_DATA_MASK;
972 bits |= BITS_DATA_8;
973 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
974 break;
975 }
976
977 switch (bits & BITS_PARITY_MASK) {
978 case BITS_PARITY_NONE:
979 dbg("%s - parity = NONE", __func__);
980 cflag &= ~PARENB;
981 break;
982 case BITS_PARITY_ODD:
983 dbg("%s - parity = ODD", __func__);
984 cflag |= (PARENB|PARODD);
985 break;
986 case BITS_PARITY_EVEN:
987 dbg("%s - parity = EVEN", __func__);
988 cflag &= ~PARODD;
989 cflag |= PARENB;
990 break;
991 case BITS_PARITY_MARK:
992 dbg("%s - parity = MARK (not supported, disabling parity)",
993 __func__);
994 cflag &= ~PARENB;
995 bits &= ~BITS_PARITY_MASK;
996 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
997 break;
998 case BITS_PARITY_SPACE:
999 dbg("%s - parity = SPACE (not supported, disabling parity)",
1000 __func__);
1001 cflag &= ~PARENB;
1002 bits &= ~BITS_PARITY_MASK;
1003 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
1004 break;
1005 default:
1006 dbg("%s - Unknown parity mode, disabling parity", __func__);
1007 cflag &= ~PARENB;
1008 bits &= ~BITS_PARITY_MASK;
1009 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
1010 break;
1011 }
1012
1013 cflag &= ~CSTOPB;
1014 switch (bits & BITS_STOP_MASK) {
1015 case BITS_STOP_1:
1016 dbg("%s - stop bits = 1", __func__);
1017 break;
1018 case BITS_STOP_1_5:
1019 dbg("%s - stop bits = 1.5 (not supported, using 1 stop bit)",
1020 __func__);
1021 bits &= ~BITS_STOP_MASK;
1022 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
1023 break;
1024 case BITS_STOP_2:
1025 dbg("%s - stop bits = 2", __func__);
1026 cflag |= CSTOPB;
1027 break;
1028 default:
1029 dbg("%s - Unknown number of stop bits, using 1 stop bit",
1030 __func__);
1031 bits &= ~BITS_STOP_MASK;
1032 cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2);
1033 break;
1034 }
1035
1036 cp210x_get_config(port, CP210X_GET_FLOW, modem_ctl, 16);
1037 if (modem_ctl[0] & 0x0008) {
1038 dbg("%s - flow control = CRTSCTS", __func__);
1039 cflag |= CRTSCTS;
1040 } else {
1041 dbg("%s - flow control = NONE", __func__);
1042 cflag &= ~CRTSCTS;
1043 }
1044
1045 *cflagp = cflag;
1046 }
1047
1048 static void cp210x_set_termios(struct tty_struct *tty,
1049 struct usb_serial_port *port, struct ktermios *old_termios)
1050 {
1051 unsigned int cflag, old_cflag;
1052 unsigned int baud = 0, bits;
1053 unsigned int modem_ctl[4];
1054
1055 dbg("%s - port %d", __func__, port->number);
1056
1057 if (!tty)
1058 return;
1059
1060 tty->termios->c_cflag &= ~CMSPAR;
1061 cflag = tty->termios->c_cflag;
1062 old_cflag = old_termios->c_cflag;
1063 baud = cp210x_quantise_baudrate(tty_get_baud_rate(tty));
1064
1065 /* If the baud rate is to be updated*/
1066 if (baud != tty_termios_baud_rate(old_termios) && baud != 0) {
1067 dbg("%s - Setting baud rate to %d baud", __func__,
1068 baud);
1069 if (cp210x_set_config_single(port, CP210X_SET_BAUDDIV,
1070 ((BAUD_RATE_GEN_FREQ + baud/2) / baud))) {
1071 dbg("Baud rate requested not supported by device");
1072 baud = tty_termios_baud_rate(old_termios);
1073 }
1074 }
1075 /* Report back the resulting baud rate */
1076 tty_encode_baud_rate(tty, baud, baud);
1077
1078 /* If the number of data bits is to be updated */
1079 if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
1080 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
1081 bits &= ~BITS_DATA_MASK;
1082 switch (cflag & CSIZE) {
1083 case CS5:
1084 bits |= BITS_DATA_5;
1085 dbg("%s - data bits = 5", __func__);
1086 break;
1087 case CS6:
1088 bits |= BITS_DATA_6;
1089 dbg("%s - data bits = 6", __func__);
1090 break;
1091 case CS7:
1092 bits |= BITS_DATA_7;
1093 dbg("%s - data bits = 7", __func__);
1094 break;
1095 case CS8:
1096 bits |= BITS_DATA_8;
1097 dbg("%s - data bits = 8", __func__);
1098 break;
1099 /*case CS9:
1100 bits |= BITS_DATA_9;
1101 dbg("%s - data bits = 9", __func__);
1102 break;*/
1103 default:
1104 dbg("cp210x driver does not "
1105 "support the number of bits requested,"
1106 " using 8 bit mode\n");
1107 bits |= BITS_DATA_8;
1108 break;
1109 }
1110 if (cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2))
1111 dbg("Number of data bits requested "
1112 "not supported by device\n");
1113 }
1114
1115 if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
1116 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
1117 bits &= ~BITS_PARITY_MASK;
1118 if (cflag & PARENB) {
1119 if (cflag & PARODD) {
1120 bits |= BITS_PARITY_ODD;
1121 dbg("%s - parity = ODD", __func__);
1122 } else {
1123 bits |= BITS_PARITY_EVEN;
1124 dbg("%s - parity = EVEN", __func__);
1125 }
1126 }
1127 if (cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2))
1128 dbg("Parity mode not supported "
1129 "by device\n");
1130 }
1131
1132 if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
1133 cp210x_get_config(port, CP210X_GET_LINE_CTL, &bits, 2);
1134 bits &= ~BITS_STOP_MASK;
1135 if (cflag & CSTOPB) {
1136 bits |= BITS_STOP_2;
1137 dbg("%s - stop bits = 2", __func__);
1138 } else {
1139 bits |= BITS_STOP_1;
1140 dbg("%s - stop bits = 1", __func__);
1141 }
1142 if (cp210x_set_config(port, CP210X_SET_LINE_CTL, &bits, 2))
1143 dbg("Number of stop bits requested "
1144 "not supported by device\n");
1145 }
1146
1147 if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
1148 cp210x_get_config(port, CP210X_GET_FLOW, modem_ctl, 16);
1149 dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
1150 __func__, modem_ctl[0], modem_ctl[1],
1151 modem_ctl[2], modem_ctl[3]);
1152
1153 if (cflag & CRTSCTS) {
1154 modem_ctl[0] &= ~0x7B;
1155 modem_ctl[0] |= 0x09;
1156 modem_ctl[1] = 0x80;
1157 dbg("%s - flow control = CRTSCTS", __func__);
1158 } else {
1159 modem_ctl[0] &= ~0x7B;
1160 modem_ctl[0] |= 0x01;
1161 modem_ctl[1] |= 0x40;
1162 dbg("%s - flow control = NONE", __func__);
1163 }
1164
1165 dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
1166 __func__, modem_ctl[0], modem_ctl[1],
1167 modem_ctl[2], modem_ctl[3]);
1168 cp210x_set_config(port, CP210X_SET_FLOW, modem_ctl, 16);
1169 }
1170
1171 }
1172
1173 static int cp210x_tiocmset (struct tty_struct *tty, struct file *file,
1174 unsigned int set, unsigned int clear)
1175 {
1176 struct usb_serial_port *port = tty->driver_data;
1177 return cp210x_tiocmset_port(port, file, set, clear);
1178 }
1179
1180 static int cp210x_tiocmset_port(struct usb_serial_port *port, struct file *file,
1181 unsigned int set, unsigned int clear)
1182 {
1183 unsigned int control = 0;
1184
1185 dbg("%s - port %d", __func__, port->number);
1186
1187 if (set & TIOCM_RTS) {
1188 control |= CONTROL_RTS;
1189 control |= CONTROL_WRITE_RTS;
1190 }
1191 if (set & TIOCM_DTR) {
1192 control |= CONTROL_DTR;
1193 control |= CONTROL_WRITE_DTR;
1194 }
1195 if (clear & TIOCM_RTS) {
1196 control &= ~CONTROL_RTS;
1197 control |= CONTROL_WRITE_RTS;
1198 }
1199 if (clear & TIOCM_DTR) {
1200 control &= ~CONTROL_DTR;
1201 control |= CONTROL_WRITE_DTR;
1202 }
1203
1204 dbg("%s - control = 0x%.4x", __func__, control);
1205
1206 return cp210x_set_config(port, CP210X_SET_MHS, &control, 2);
1207 }
1208
1209 static int cp210x_tiocmget (struct tty_struct *tty, struct file *file)
1210 {
1211 struct usb_serial_port *port = tty->driver_data;
1212 unsigned int control;
1213 int result;
1214
1215 dbg("%s - port %d", __func__, port->number);
1216
1217 cp210x_get_config(port, CP210X_GET_MDMSTS, &control, 1);
1218
1219 result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
1220 |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
1221 |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
1222 |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
1223 |((control & CONTROL_RING)? TIOCM_RI : 0)
1224 |((control & CONTROL_DCD) ? TIOCM_CD : 0);
1225
1226 dbg("%s - control = 0x%.2x", __func__, control);
1227
1228 return result;
1229 }
1230
1231 static void cp210x_break_ctl (struct tty_struct *tty, int break_state)
1232 {
1233 struct usb_serial_port *port = tty->driver_data;
1234 unsigned int state;
1235
1236 dbg("%s - port %d", __func__, port->number);
1237 if (break_state == 0)
1238 state = BREAK_OFF;
1239 else
1240 state = BREAK_ON;
1241 dbg("%s - turning break %s", __func__,
1242 state == BREAK_OFF ? "off" : "on");
1243 cp210x_set_config(port, CP210X_SET_BREAK, &state, 2);
1244 }
1245
1246 static int cp210x_startup(struct usb_serial *serial)
1247 {
1248 /* cp210x buffers behave strangely unless device is reset */
1249 usb_reset_device(serial->dev);
1250 return 0;
1251 }
1252
1253 static void cp210x_disconnect(struct usb_serial *serial)
1254 {
1255 int i;
1256
1257 dbg("%s", __func__);
1258
1259 /* Stop reads and writes on all ports */
1260 for (i = 0; i < serial->num_ports; ++i)
1261 cp210x_cleanup(serial->port[i]);
1262 }
1263
1264 static int __init cp210x_init(void)
1265 {
1266 int retval;
1267
1268 retval = usb_serial_register(&cp210x_device);
1269 if (retval)
1270 return retval; /* Failed to register */
1271
1272 retval = usb_register(&cp210x_driver);
1273 if (retval) {
1274 /* Failed to register */
1275 usb_serial_deregister(&cp210x_device);
1276 return retval;
1277 }
1278
1279 /* Success */
1280 printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
1281 DRIVER_DESC "\n");
1282 return 0;
1283 }
1284
1285 static void __exit cp210x_exit(void)
1286 {
1287 usb_deregister(&cp210x_driver);
1288 usb_serial_deregister(&cp210x_device);
1289 }
1290
1291 module_init(cp210x_init);
1292 module_exit(cp210x_exit);
1293
1294 MODULE_DESCRIPTION(DRIVER_DESC);
1295 MODULE_VERSION(DRIVER_VERSION);
1296 MODULE_LICENSE("GPL");
1297
1298 module_param(debug, bool, S_IRUGO | S_IWUSR);
1299 MODULE_PARM_DESC(debug, "Enable verbose debugging messages");