cp210x.c (0407f1ce8fce8e93a249833617221c9905210e5b) cp210x.c (cf5276ce7867d6d87c02fbe4977646ed342e323a)
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 unchanged lines hidden (view full) ---

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>
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 unchanged lines hidden (view full) ---

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 <linux/gpio/driver.h>
27#include <linux/bitops.h>
28#include <linux/mutex.h>
26
27#define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
28
29/*
30 * Function Prototypes
31 */
32static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
33static void cp210x_close(struct usb_serial_port *);

--- 5 unchanged lines hidden (view full) ---

39static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
40 struct ktermios*);
41static bool cp210x_tx_empty(struct usb_serial_port *port);
42static int cp210x_tiocmget(struct tty_struct *);
43static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
44static int cp210x_tiocmset_port(struct usb_serial_port *port,
45 unsigned int, unsigned int);
46static void cp210x_break_ctl(struct tty_struct *, int);
29
30#define DRIVER_DESC "Silicon Labs CP210x RS232 serial adaptor driver"
31
32/*
33 * Function Prototypes
34 */
35static int cp210x_open(struct tty_struct *tty, struct usb_serial_port *);
36static void cp210x_close(struct usb_serial_port *);

--- 5 unchanged lines hidden (view full) ---

42static void cp210x_set_termios(struct tty_struct *, struct usb_serial_port *,
43 struct ktermios*);
44static bool cp210x_tx_empty(struct usb_serial_port *port);
45static int cp210x_tiocmget(struct tty_struct *);
46static int cp210x_tiocmset(struct tty_struct *, unsigned int, unsigned int);
47static int cp210x_tiocmset_port(struct usb_serial_port *port,
48 unsigned int, unsigned int);
49static void cp210x_break_ctl(struct tty_struct *, int);
50static int cp210x_attach(struct usb_serial *);
51static void cp210x_disconnect(struct usb_serial *);
52static void cp210x_release(struct usb_serial *);
47static int cp210x_port_probe(struct usb_serial_port *);
48static int cp210x_port_remove(struct usb_serial_port *);
49static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
50
51static const struct usb_device_id id_table[] = {
52 { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
53 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
54 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */

--- 148 unchanged lines hidden (view full) ---

203 { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
204 { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
205 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
206 { } /* Terminating Entry */
207};
208
209MODULE_DEVICE_TABLE(usb, id_table);
210
53static int cp210x_port_probe(struct usb_serial_port *);
54static int cp210x_port_remove(struct usb_serial_port *);
55static void cp210x_dtr_rts(struct usb_serial_port *p, int on);
56
57static const struct usb_device_id id_table[] = {
58 { USB_DEVICE(0x045B, 0x0053) }, /* Renesas RX610 RX-Stick */
59 { USB_DEVICE(0x0471, 0x066A) }, /* AKTAKOM ACE-1001 cable */
60 { USB_DEVICE(0x0489, 0xE000) }, /* Pirelli Broadband S.p.A, DP-L10 SIP/GSM Mobile */

--- 148 unchanged lines hidden (view full) ---

209 { USB_DEVICE(0x3195, 0xF280) }, /* Link Instruments MSO-28 */
210 { USB_DEVICE(0x3195, 0xF281) }, /* Link Instruments MSO-28 */
211 { USB_DEVICE(0x413C, 0x9500) }, /* DW700 GPS USB interface */
212 { } /* Terminating Entry */
213};
214
215MODULE_DEVICE_TABLE(usb, id_table);
216
217struct cp210x_serial_private {
218#ifdef CONFIG_GPIOLIB
219 struct gpio_chip gc;
220 u8 config;
221 u8 gpio_mode;
222 u8 gpio_registered;
223#endif
224 u8 partnum;
225};
226
211struct cp210x_port_private {
212 __u8 bInterfaceNumber;
213 bool has_swapped_line_ctl;
214};
215
216static struct usb_serial_driver cp210x_device = {
217 .driver = {
218 .owner = THIS_MODULE,

--- 5 unchanged lines hidden (view full) ---

224 .bulk_out_size = 256,
225 .open = cp210x_open,
226 .close = cp210x_close,
227 .break_ctl = cp210x_break_ctl,
228 .set_termios = cp210x_set_termios,
229 .tx_empty = cp210x_tx_empty,
230 .tiocmget = cp210x_tiocmget,
231 .tiocmset = cp210x_tiocmset,
227struct cp210x_port_private {
228 __u8 bInterfaceNumber;
229 bool has_swapped_line_ctl;
230};
231
232static struct usb_serial_driver cp210x_device = {
233 .driver = {
234 .owner = THIS_MODULE,

--- 5 unchanged lines hidden (view full) ---

240 .bulk_out_size = 256,
241 .open = cp210x_open,
242 .close = cp210x_close,
243 .break_ctl = cp210x_break_ctl,
244 .set_termios = cp210x_set_termios,
245 .tx_empty = cp210x_tx_empty,
246 .tiocmget = cp210x_tiocmget,
247 .tiocmset = cp210x_tiocmset,
248 .attach = cp210x_attach,
249 .disconnect = cp210x_disconnect,
250 .release = cp210x_release,
232 .port_probe = cp210x_port_probe,
233 .port_remove = cp210x_port_remove,
234 .dtr_rts = cp210x_dtr_rts
235};
236
237static struct usb_serial_driver * const serial_drivers[] = {
238 &cp210x_device, NULL
239};

--- 26 unchanged lines hidden (view full) ---

266#define CP210X_PURGE 0x12
267#define CP210X_SET_FLOW 0x13
268#define CP210X_GET_FLOW 0x14
269#define CP210X_EMBED_EVENTS 0x15
270#define CP210X_GET_EVENTSTATE 0x16
271#define CP210X_SET_CHARS 0x19
272#define CP210X_GET_BAUDRATE 0x1D
273#define CP210X_SET_BAUDRATE 0x1E
251 .port_probe = cp210x_port_probe,
252 .port_remove = cp210x_port_remove,
253 .dtr_rts = cp210x_dtr_rts
254};
255
256static struct usb_serial_driver * const serial_drivers[] = {
257 &cp210x_device, NULL
258};

--- 26 unchanged lines hidden (view full) ---

285#define CP210X_PURGE 0x12
286#define CP210X_SET_FLOW 0x13
287#define CP210X_GET_FLOW 0x14
288#define CP210X_EMBED_EVENTS 0x15
289#define CP210X_GET_EVENTSTATE 0x16
290#define CP210X_SET_CHARS 0x19
291#define CP210X_GET_BAUDRATE 0x1D
292#define CP210X_SET_BAUDRATE 0x1E
293#define CP210X_VENDOR_SPECIFIC 0xFF
274
275/* CP210X_IFC_ENABLE */
276#define UART_ENABLE 0x0001
277#define UART_DISABLE 0x0000
278
279/* CP210X_(SET|GET)_BAUDDIV */
280#define BAUD_RATE_GEN_FREQ 0x384000
281

--- 26 unchanged lines hidden (view full) ---

308#define CONTROL_RTS 0x0002
309#define CONTROL_CTS 0x0010
310#define CONTROL_DSR 0x0020
311#define CONTROL_RING 0x0040
312#define CONTROL_DCD 0x0080
313#define CONTROL_WRITE_DTR 0x0100
314#define CONTROL_WRITE_RTS 0x0200
315
294
295/* CP210X_IFC_ENABLE */
296#define UART_ENABLE 0x0001
297#define UART_DISABLE 0x0000
298
299/* CP210X_(SET|GET)_BAUDDIV */
300#define BAUD_RATE_GEN_FREQ 0x384000
301

--- 26 unchanged lines hidden (view full) ---

328#define CONTROL_RTS 0x0002
329#define CONTROL_CTS 0x0010
330#define CONTROL_DSR 0x0020
331#define CONTROL_RING 0x0040
332#define CONTROL_DCD 0x0080
333#define CONTROL_WRITE_DTR 0x0100
334#define CONTROL_WRITE_RTS 0x0200
335
336/* CP210X_VENDOR_SPECIFIC values */
337#define CP210X_READ_LATCH 0x00C2
338#define CP210X_GET_PARTNUM 0x370B
339#define CP210X_GET_PORTCONFIG 0x370C
340#define CP210X_GET_DEVICEMODE 0x3711
341#define CP210X_WRITE_LATCH 0x37E1
342
343/* Part number definitions */
344#define CP210X_PARTNUM_CP2101 0x01
345#define CP210X_PARTNUM_CP2102 0x02
346#define CP210X_PARTNUM_CP2103 0x03
347#define CP210X_PARTNUM_CP2104 0x04
348#define CP210X_PARTNUM_CP2105 0x05
349#define CP210X_PARTNUM_CP2108 0x08
350
316/* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
317struct cp210x_comm_status {
318 __le32 ulErrors;
319 __le32 ulHoldReasons;
320 __le32 ulAmountInInQueue;
321 __le32 ulAmountInOutQueue;
322 u8 bEofReceived;
323 u8 bWaitForImmediate;

--- 39 unchanged lines hidden (view full) ---

363#define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
364#define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
365
366/* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
367#define CP210X_SERIAL_RTS_INACTIVE 0
368#define CP210X_SERIAL_RTS_ACTIVE 1
369#define CP210X_SERIAL_RTS_FLOW_CTL 2
370
351/* CP210X_GET_COMM_STATUS returns these 0x13 bytes */
352struct cp210x_comm_status {
353 __le32 ulErrors;
354 __le32 ulHoldReasons;
355 __le32 ulAmountInInQueue;
356 __le32 ulAmountInOutQueue;
357 u8 bEofReceived;
358 u8 bWaitForImmediate;

--- 39 unchanged lines hidden (view full) ---

398#define CP210X_SERIAL_RTS_SHIFT(_mode) (_mode << 6)
399#define CP210X_SERIAL_XOFF_CONTINUE BIT(31)
400
401/* values for cp210x_flow_ctl::ulFlowReplace::CP210X_SERIAL_RTS_MASK */
402#define CP210X_SERIAL_RTS_INACTIVE 0
403#define CP210X_SERIAL_RTS_ACTIVE 1
404#define CP210X_SERIAL_RTS_FLOW_CTL 2
405
406/* CP210X_VENDOR_SPECIFIC, CP210X_GET_DEVICEMODE call reads these 0x2 bytes. */
407struct cp210x_pin_mode {
408 u8 eci;
409 u8 sci;
410} __packed;
411
412#define CP210X_PIN_MODE_MODEM 0
413#define CP210X_PIN_MODE_GPIO BIT(0)
414
371/*
415/*
416 * CP210X_VENDOR_SPECIFIC, CP210X_GET_PORTCONFIG call reads these 0xf bytes.
417 * Structure needs padding due to unused/unspecified bytes.
418 */
419struct cp210x_config {
420 __le16 gpio_mode;
421 u8 __pad0[2];
422 __le16 reset_state;
423 u8 __pad1[4];
424 __le16 suspend_state;
425 u8 sci_cfg;
426 u8 eci_cfg;
427 u8 device_cfg;
428} __packed;
429
430/* GPIO modes */
431#define CP210X_SCI_GPIO_MODE_OFFSET 9
432#define CP210X_SCI_GPIO_MODE_MASK GENMASK(11, 9)
433
434#define CP210X_ECI_GPIO_MODE_OFFSET 2
435#define CP210X_ECI_GPIO_MODE_MASK GENMASK(3, 2)
436
437/* CP2105 port configuration values */
438#define CP2105_GPIO0_TXLED_MODE BIT(0)
439#define CP2105_GPIO1_RXLED_MODE BIT(1)
440#define CP2105_GPIO1_RS485_MODE BIT(2)
441
442/* CP210X_VENDOR_SPECIFIC, CP210X_WRITE_LATCH call writes these 0x2 bytes. */
443struct cp210x_gpio_write {
444 u8 mask;
445 u8 state;
446} __packed;
447
448/*
449 * Helper to get interface number when we only have struct usb_serial.
450 */
451static u8 cp210x_interface_num(struct usb_serial *serial)
452{
453 struct usb_host_interface *cur_altsetting;
454
455 cur_altsetting = serial->interface->cur_altsetting;
456
457 return cur_altsetting->desc.bInterfaceNumber;
458}
459
460/*
372 * Reads a variable-sized block of CP210X_ registers, identified by req.
373 * Returns data into buf in native USB byte order.
374 */
375static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
376 void *buf, int bufsize)
377{
378 struct usb_serial *serial = port->serial;
379 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);

--- 79 unchanged lines hidden (view full) ---

459 * Reads any 8-bit CP210X_ register identified by req.
460 */
461static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
462{
463 return cp210x_read_reg_block(port, req, val, sizeof(*val));
464}
465
466/*
461 * Reads a variable-sized block of CP210X_ registers, identified by req.
462 * Returns data into buf in native USB byte order.
463 */
464static int cp210x_read_reg_block(struct usb_serial_port *port, u8 req,
465 void *buf, int bufsize)
466{
467 struct usb_serial *serial = port->serial;
468 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);

--- 79 unchanged lines hidden (view full) ---

548 * Reads any 8-bit CP210X_ register identified by req.
549 */
550static int cp210x_read_u8_reg(struct usb_serial_port *port, u8 req, u8 *val)
551{
552 return cp210x_read_reg_block(port, req, val, sizeof(*val));
553}
554
555/*
556 * Reads a variable-sized vendor block of CP210X_ registers, identified by val.
557 * Returns data into buf in native USB byte order.
558 */
559static int cp210x_read_vendor_block(struct usb_serial *serial, u8 type, u16 val,
560 void *buf, int bufsize)
561{
562 void *dmabuf;
563 int result;
564
565 dmabuf = kmalloc(bufsize, GFP_KERNEL);
566 if (!dmabuf)
567 return -ENOMEM;
568
569 result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
570 CP210X_VENDOR_SPECIFIC, type, val,
571 cp210x_interface_num(serial), dmabuf, bufsize,
572 USB_CTRL_GET_TIMEOUT);
573 if (result == bufsize) {
574 memcpy(buf, dmabuf, bufsize);
575 result = 0;
576 } else {
577 dev_err(&serial->interface->dev,
578 "failed to get vendor val 0x%04x size %d: %d\n", val,
579 bufsize, result);
580 if (result >= 0)
581 result = -EIO;
582 }
583
584 kfree(dmabuf);
585
586 return result;
587}
588
589/*
467 * Writes any 16-bit CP210X_ register (req) whose value is passed
468 * entirely in the wValue field of the USB request.
469 */
470static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
471{
472 struct usb_serial *serial = port->serial;
473 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
474 int result;

--- 52 unchanged lines hidden (view full) ---

527{
528 __le32 le32_val;
529
530 le32_val = cpu_to_le32(val);
531
532 return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
533}
534
590 * Writes any 16-bit CP210X_ register (req) whose value is passed
591 * entirely in the wValue field of the USB request.
592 */
593static int cp210x_write_u16_reg(struct usb_serial_port *port, u8 req, u16 val)
594{
595 struct usb_serial *serial = port->serial;
596 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);
597 int result;

--- 52 unchanged lines hidden (view full) ---

650{
651 __le32 le32_val;
652
653 le32_val = cpu_to_le32(val);
654
655 return cp210x_write_reg_block(port, req, &le32_val, sizeof(le32_val));
656}
657
658#ifdef CONFIG_GPIOLIB
535/*
659/*
660 * Writes a variable-sized vendor block of CP210X_ registers, identified by val.
661 * Data in buf must be in native USB byte order.
662 */
663static int cp210x_write_vendor_block(struct usb_serial *serial, u8 type,
664 u16 val, void *buf, int bufsize)
665{
666 void *dmabuf;
667 int result;
668
669 dmabuf = kmemdup(buf, bufsize, GFP_KERNEL);
670 if (!dmabuf)
671 return -ENOMEM;
672
673 result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
674 CP210X_VENDOR_SPECIFIC, type, val,
675 cp210x_interface_num(serial), dmabuf, bufsize,
676 USB_CTRL_SET_TIMEOUT);
677
678 kfree(dmabuf);
679
680 if (result == bufsize) {
681 result = 0;
682 } else {
683 dev_err(&serial->interface->dev,
684 "failed to set vendor val 0x%04x size %d: %d\n", val,
685 bufsize, result);
686 if (result >= 0)
687 result = -EIO;
688 }
689
690 return result;
691}
692#endif
693
694/*
536 * Detect CP2108 GET_LINE_CTL bug and activate workaround.
537 * Write a known good value 0x800, read it back.
538 * If it comes back swapped the bug is detected.
539 * Preserve the original register value.
540 */
541static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
542{
543 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);

--- 549 unchanged lines hidden (view full) ---

1093 state = BREAK_OFF;
1094 else
1095 state = BREAK_ON;
1096 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1097 state == BREAK_OFF ? "off" : "on");
1098 cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
1099}
1100
695 * Detect CP2108 GET_LINE_CTL bug and activate workaround.
696 * Write a known good value 0x800, read it back.
697 * If it comes back swapped the bug is detected.
698 * Preserve the original register value.
699 */
700static int cp210x_detect_swapped_line_ctl(struct usb_serial_port *port)
701{
702 struct cp210x_port_private *port_priv = usb_get_serial_port_data(port);

--- 549 unchanged lines hidden (view full) ---

1252 state = BREAK_OFF;
1253 else
1254 state = BREAK_ON;
1255 dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
1256 state == BREAK_OFF ? "off" : "on");
1257 cp210x_write_u16_reg(port, CP210X_SET_BREAK, state);
1258}
1259
1260#ifdef CONFIG_GPIOLIB
1261static int cp210x_gpio_request(struct gpio_chip *gc, unsigned int offset)
1262{
1263 struct usb_serial *serial = gpiochip_get_data(gc);
1264 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1265
1266 switch (offset) {
1267 case 0:
1268 if (priv->config & CP2105_GPIO0_TXLED_MODE)
1269 return -ENODEV;
1270 break;
1271 case 1:
1272 if (priv->config & (CP2105_GPIO1_RXLED_MODE |
1273 CP2105_GPIO1_RS485_MODE))
1274 return -ENODEV;
1275 break;
1276 }
1277
1278 return 0;
1279}
1280
1281static int cp210x_gpio_get(struct gpio_chip *gc, unsigned int gpio)
1282{
1283 struct usb_serial *serial = gpiochip_get_data(gc);
1284 int result;
1285 u8 buf;
1286
1287 result = cp210x_read_vendor_block(serial, REQTYPE_INTERFACE_TO_HOST,
1288 CP210X_READ_LATCH, &buf, sizeof(buf));
1289 if (result < 0)
1290 return result;
1291
1292 return !!(buf & BIT(gpio));
1293}
1294
1295static void cp210x_gpio_set(struct gpio_chip *gc, unsigned int gpio, int value)
1296{
1297 struct usb_serial *serial = gpiochip_get_data(gc);
1298 struct cp210x_gpio_write buf;
1299
1300 if (value == 1)
1301 buf.state = BIT(gpio);
1302 else
1303 buf.state = 0;
1304
1305 buf.mask = BIT(gpio);
1306
1307 cp210x_write_vendor_block(serial, REQTYPE_HOST_TO_INTERFACE,
1308 CP210X_WRITE_LATCH, &buf, sizeof(buf));
1309}
1310
1311static int cp210x_gpio_direction_get(struct gpio_chip *gc, unsigned int gpio)
1312{
1313 /* Hardware does not support an input mode */
1314 return 0;
1315}
1316
1317static int cp210x_gpio_direction_input(struct gpio_chip *gc, unsigned int gpio)
1318{
1319 /* Hardware does not support an input mode */
1320 return -ENOTSUPP;
1321}
1322
1323static int cp210x_gpio_direction_output(struct gpio_chip *gc, unsigned int gpio,
1324 int value)
1325{
1326 return 0;
1327}
1328
1329static int cp210x_gpio_set_single_ended(struct gpio_chip *gc, unsigned int gpio,
1330 enum single_ended_mode mode)
1331{
1332 struct usb_serial *serial = gpiochip_get_data(gc);
1333 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1334
1335 /* Succeed only if in correct mode (this can't be set at runtime) */
1336 if ((mode == LINE_MODE_PUSH_PULL) && (priv->gpio_mode & BIT(gpio)))
1337 return 0;
1338
1339 if ((mode == LINE_MODE_OPEN_DRAIN) && !(priv->gpio_mode & BIT(gpio)))
1340 return 0;
1341
1342 return -ENOTSUPP;
1343}
1344
1345/*
1346 * This function is for configuring GPIO using shared pins, where other signals
1347 * are made unavailable by configuring the use of GPIO. This is believed to be
1348 * only applicable to the cp2105 at this point, the other devices supported by
1349 * this driver that provide GPIO do so in a way that does not impact other
1350 * signals and are thus expected to have very different initialisation.
1351 */
1352static int cp2105_shared_gpio_init(struct usb_serial *serial)
1353{
1354 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1355 struct cp210x_pin_mode mode;
1356 struct cp210x_config config;
1357 u8 intf_num = cp210x_interface_num(serial);
1358 int result;
1359
1360 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1361 CP210X_GET_DEVICEMODE, &mode,
1362 sizeof(mode));
1363 if (result < 0)
1364 return result;
1365
1366 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1367 CP210X_GET_PORTCONFIG, &config,
1368 sizeof(config));
1369 if (result < 0)
1370 return result;
1371
1372 /* 2 banks of GPIO - One for the pins taken from each serial port */
1373 if (intf_num == 0) {
1374 if (mode.eci == CP210X_PIN_MODE_MODEM)
1375 return 0;
1376
1377 priv->config = config.eci_cfg;
1378 priv->gpio_mode = (u8)((le16_to_cpu(config.gpio_mode) &
1379 CP210X_ECI_GPIO_MODE_MASK) >>
1380 CP210X_ECI_GPIO_MODE_OFFSET);
1381 priv->gc.ngpio = 2;
1382 } else if (intf_num == 1) {
1383 if (mode.sci == CP210X_PIN_MODE_MODEM)
1384 return 0;
1385
1386 priv->config = config.sci_cfg;
1387 priv->gpio_mode = (u8)((le16_to_cpu(config.gpio_mode) &
1388 CP210X_SCI_GPIO_MODE_MASK) >>
1389 CP210X_SCI_GPIO_MODE_OFFSET);
1390 priv->gc.ngpio = 3;
1391 } else {
1392 return -ENODEV;
1393 }
1394
1395 priv->gc.label = "cp210x";
1396 priv->gc.request = cp210x_gpio_request;
1397 priv->gc.get_direction = cp210x_gpio_direction_get;
1398 priv->gc.direction_input = cp210x_gpio_direction_input;
1399 priv->gc.direction_output = cp210x_gpio_direction_output;
1400 priv->gc.get = cp210x_gpio_get;
1401 priv->gc.set = cp210x_gpio_set;
1402 priv->gc.set_single_ended = cp210x_gpio_set_single_ended;
1403 priv->gc.owner = THIS_MODULE;
1404 priv->gc.parent = &serial->interface->dev;
1405 priv->gc.base = -1;
1406 priv->gc.can_sleep = true;
1407
1408 result = gpiochip_add_data(&priv->gc, serial);
1409 if (!result)
1410 priv->gpio_registered = 1;
1411
1412 return result;
1413}
1414
1415static void cp210x_gpio_remove(struct usb_serial *serial)
1416{
1417 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1418
1419 if (priv->gpio_registered) {
1420 gpiochip_remove(&priv->gc);
1421 priv->gpio_registered = 0;
1422 }
1423}
1424
1425#else
1426
1427static int cp2105_shared_gpio_init(struct usb_serial *serial)
1428{
1429 return 0;
1430}
1431
1432static void cp210x_gpio_remove(struct usb_serial *serial)
1433{
1434 /* Nothing to do */
1435}
1436
1437#endif
1438
1101static int cp210x_port_probe(struct usb_serial_port *port)
1102{
1103 struct usb_serial *serial = port->serial;
1439static int cp210x_port_probe(struct usb_serial_port *port)
1440{
1441 struct usb_serial *serial = port->serial;
1104 struct usb_host_interface *cur_altsetting;
1105 struct cp210x_port_private *port_priv;
1106 int ret;
1107
1108 port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1109 if (!port_priv)
1110 return -ENOMEM;
1111
1442 struct cp210x_port_private *port_priv;
1443 int ret;
1444
1445 port_priv = kzalloc(sizeof(*port_priv), GFP_KERNEL);
1446 if (!port_priv)
1447 return -ENOMEM;
1448
1112 cur_altsetting = serial->interface->cur_altsetting;
1113 port_priv->bInterfaceNumber = cur_altsetting->desc.bInterfaceNumber;
1449 port_priv->bInterfaceNumber = cp210x_interface_num(serial);
1114
1115 usb_set_serial_port_data(port, port_priv);
1116
1117 ret = cp210x_detect_swapped_line_ctl(port);
1118 if (ret) {
1119 kfree(port_priv);
1120 return ret;
1121 }

--- 6 unchanged lines hidden (view full) ---

1128 struct cp210x_port_private *port_priv;
1129
1130 port_priv = usb_get_serial_port_data(port);
1131 kfree(port_priv);
1132
1133 return 0;
1134}
1135
1450
1451 usb_set_serial_port_data(port, port_priv);
1452
1453 ret = cp210x_detect_swapped_line_ctl(port);
1454 if (ret) {
1455 kfree(port_priv);
1456 return ret;
1457 }

--- 6 unchanged lines hidden (view full) ---

1464 struct cp210x_port_private *port_priv;
1465
1466 port_priv = usb_get_serial_port_data(port);
1467 kfree(port_priv);
1468
1469 return 0;
1470}
1471
1472static int cp210x_attach(struct usb_serial *serial)
1473{
1474 int result;
1475 struct cp210x_serial_private *priv;
1476
1477 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1478 if (!priv)
1479 return -ENOMEM;
1480
1481 result = cp210x_read_vendor_block(serial, REQTYPE_DEVICE_TO_HOST,
1482 CP210X_GET_PARTNUM, &priv->partnum,
1483 sizeof(priv->partnum));
1484 if (result < 0)
1485 goto err_free_priv;
1486
1487 usb_set_serial_data(serial, priv);
1488
1489 if (priv->partnum == CP210X_PARTNUM_CP2105) {
1490 result = cp2105_shared_gpio_init(serial);
1491 if (result < 0) {
1492 dev_err(&serial->interface->dev,
1493 "GPIO initialisation failed, continuing without GPIO support\n");
1494 }
1495 }
1496
1497 return 0;
1498err_free_priv:
1499 kfree(priv);
1500
1501 return result;
1502}
1503
1504static void cp210x_disconnect(struct usb_serial *serial)
1505{
1506 cp210x_gpio_remove(serial);
1507}
1508
1509static void cp210x_release(struct usb_serial *serial)
1510{
1511 struct cp210x_serial_private *priv = usb_get_serial_data(serial);
1512
1513 cp210x_gpio_remove(serial);
1514
1515 kfree(priv);
1516}
1517
1136module_usb_serial_driver(serial_drivers, id_table);
1137
1138MODULE_DESCRIPTION(DRIVER_DESC);
1139MODULE_LICENSE("GPL");
1518module_usb_serial_driver(serial_drivers, id_table);
1519
1520MODULE_DESCRIPTION(DRIVER_DESC);
1521MODULE_LICENSE("GPL");