cp2101.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. /*
  2. * Silicon Laboratories CP2101/CP2102 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. * Outstanding Issues:
  15. * Buffers are not flushed when the port is opened.
  16. * Multiple calls to write() may fail with "Resource temporarily unavailable"
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/usb.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/usb/serial.h>
  29. /*
  30. * Version Information
  31. */
  32. #define DRIVER_VERSION "v0.07"
  33. #define DRIVER_DESC "Silicon Labs CP2101/CP2102 RS232 serial adaptor driver"
  34. /*
  35. * Function Prototypes
  36. */
  37. static int cp2101_open(struct usb_serial_port*, struct file*);
  38. static void cp2101_cleanup(struct usb_serial_port*);
  39. static void cp2101_close(struct usb_serial_port*, struct file*);
  40. static void cp2101_get_termios(struct usb_serial_port*);
  41. static void cp2101_set_termios(struct usb_serial_port*, struct ktermios*);
  42. static int cp2101_tiocmget (struct usb_serial_port *, struct file *);
  43. static int cp2101_tiocmset (struct usb_serial_port *, struct file *,
  44. unsigned int, unsigned int);
  45. static void cp2101_break_ctl(struct usb_serial_port*, int);
  46. static int cp2101_startup (struct usb_serial *);
  47. static void cp2101_shutdown(struct usb_serial*);
  48. static int debug;
  49. static struct usb_device_id id_table [] = {
  50. { USB_DEVICE(0x08e6, 0x5501) }, /* Gemalto Prox-PU/CU contactless smartcard reader */
  51. { USB_DEVICE(0x0FCF, 0x1003) }, /* Dynastream ANT development board */
  52. { USB_DEVICE(0x0FCF, 0x1004) }, /* Dynastream ANT2USB */
  53. { USB_DEVICE(0x10A6, 0xAA26) }, /* Knock-off DCU-11 cable */
  54. { USB_DEVICE(0x10AB, 0x10C5) }, /* Siemens MC60 Cable */
  55. { USB_DEVICE(0x10B5, 0xAC70) }, /* Nokia CA-42 USB */
  56. { USB_DEVICE(0x10C4, 0x800A) }, /* SPORTident BSM7-D-USB main station */
  57. { USB_DEVICE(0x10C4, 0x803B) }, /* Pololu USB-serial converter */
  58. { USB_DEVICE(0x10C4, 0x8053) }, /* Enfora EDG1228 */
  59. { USB_DEVICE(0x10C4, 0x8066) }, /* Argussoft In-System Programmer */
  60. { USB_DEVICE(0x10C4, 0x807A) }, /* Crumb128 board */
  61. { USB_DEVICE(0x10C4, 0x80CA) }, /* Degree Controls Inc */
  62. { USB_DEVICE(0x10C4, 0x80DD) }, /* Tracient RFID */
  63. { USB_DEVICE(0x10C4, 0x80F6) }, /* Suunto sports instrument */
  64. { USB_DEVICE(0x10C4, 0x813D) }, /* Burnside Telecom Deskmobile */
  65. { USB_DEVICE(0x10C4, 0x814A) }, /* West Mountain Radio RIGblaster P&P */
  66. { USB_DEVICE(0x10C4, 0x814B) }, /* West Mountain Radio RIGtalk */
  67. { USB_DEVICE(0x10C4, 0x815E) }, /* Helicomm IP-Link 1220-DVM */
  68. { USB_DEVICE(0x10C4, 0x81C8) }, /* Lipowsky Industrie Elektronik GmbH, Baby-JTAG */
  69. { USB_DEVICE(0x10C4, 0x81E2) }, /* Lipowsky Industrie Elektronik GmbH, Baby-LIN */
  70. { USB_DEVICE(0x10C4, 0x81E7) }, /* Aerocomm Radio */
  71. { USB_DEVICE(0x10C4, 0x8218) }, /* Lipowsky Industrie Elektronik GmbH, HARP-1 */
  72. { USB_DEVICE(0x10C4, 0xEA60) }, /* Silicon Labs factory default */
  73. { USB_DEVICE(0x10C4, 0xEA61) }, /* Silicon Labs factory default */
  74. { USB_DEVICE(0x10C4, 0xF001) }, /* Elan Digital Systems USBscope50 */
  75. { USB_DEVICE(0x10C4, 0xF002) }, /* Elan Digital Systems USBwave12 */
  76. { USB_DEVICE(0x10C4, 0xF003) }, /* Elan Digital Systems USBpulse100 */
  77. { USB_DEVICE(0x10C4, 0xF004) }, /* Elan Digital Systems USBcount50 */
  78. { USB_DEVICE(0x10C5, 0xEA61) }, /* Silicon Labs MobiData GPRS USB Modem */
  79. { USB_DEVICE(0x13AD, 0x9999) }, /* Baltech card reader */
  80. { USB_DEVICE(0x166A, 0x0303) }, /* Clipsal 5500PCU C-Bus USB interface */
  81. { USB_DEVICE(0x16D6, 0x0001) }, /* Jablotron serial interface */
  82. { } /* Terminating Entry */
  83. };
  84. MODULE_DEVICE_TABLE (usb, id_table);
  85. static struct usb_driver cp2101_driver = {
  86. .name = "cp2101",
  87. .probe = usb_serial_probe,
  88. .disconnect = usb_serial_disconnect,
  89. .id_table = id_table,
  90. .no_dynamic_id = 1,
  91. };
  92. static struct usb_serial_driver cp2101_device = {
  93. .driver = {
  94. .owner = THIS_MODULE,
  95. .name = "cp2101",
  96. },
  97. .usb_driver = &cp2101_driver,
  98. .id_table = id_table,
  99. .num_interrupt_in = 0,
  100. .num_bulk_in = NUM_DONT_CARE,
  101. .num_bulk_out = NUM_DONT_CARE,
  102. .num_ports = 1,
  103. .open = cp2101_open,
  104. .close = cp2101_close,
  105. .break_ctl = cp2101_break_ctl,
  106. .set_termios = cp2101_set_termios,
  107. .tiocmget = cp2101_tiocmget,
  108. .tiocmset = cp2101_tiocmset,
  109. .attach = cp2101_startup,
  110. .shutdown = cp2101_shutdown,
  111. };
  112. /* Config request types */
  113. #define REQTYPE_HOST_TO_DEVICE 0x41
  114. #define REQTYPE_DEVICE_TO_HOST 0xc1
  115. /* Config SET requests. To GET, add 1 to the request number */
  116. #define CP2101_UART 0x00 /* Enable / Disable */
  117. #define CP2101_BAUDRATE 0x01 /* (BAUD_RATE_GEN_FREQ / baudrate) */
  118. #define CP2101_BITS 0x03 /* 0x(0)(databits)(parity)(stopbits) */
  119. #define CP2101_BREAK 0x05 /* On / Off */
  120. #define CP2101_CONTROL 0x07 /* Flow control line states */
  121. #define CP2101_MODEMCTL 0x13 /* Modem controls */
  122. #define CP2101_CONFIG_6 0x19 /* 6 bytes of config data ??? */
  123. /* CP2101_UART */
  124. #define UART_ENABLE 0x0001
  125. #define UART_DISABLE 0x0000
  126. /* CP2101_BAUDRATE */
  127. #define BAUD_RATE_GEN_FREQ 0x384000
  128. /* CP2101_BITS */
  129. #define BITS_DATA_MASK 0X0f00
  130. #define BITS_DATA_5 0X0500
  131. #define BITS_DATA_6 0X0600
  132. #define BITS_DATA_7 0X0700
  133. #define BITS_DATA_8 0X0800
  134. #define BITS_DATA_9 0X0900
  135. #define BITS_PARITY_MASK 0x00f0
  136. #define BITS_PARITY_NONE 0x0000
  137. #define BITS_PARITY_ODD 0x0010
  138. #define BITS_PARITY_EVEN 0x0020
  139. #define BITS_PARITY_MARK 0x0030
  140. #define BITS_PARITY_SPACE 0x0040
  141. #define BITS_STOP_MASK 0x000f
  142. #define BITS_STOP_1 0x0000
  143. #define BITS_STOP_1_5 0x0001
  144. #define BITS_STOP_2 0x0002
  145. /* CP2101_BREAK */
  146. #define BREAK_ON 0x0000
  147. #define BREAK_OFF 0x0001
  148. /* CP2101_CONTROL */
  149. #define CONTROL_DTR 0x0001
  150. #define CONTROL_RTS 0x0002
  151. #define CONTROL_CTS 0x0010
  152. #define CONTROL_DSR 0x0020
  153. #define CONTROL_RING 0x0040
  154. #define CONTROL_DCD 0x0080
  155. #define CONTROL_WRITE_DTR 0x0100
  156. #define CONTROL_WRITE_RTS 0x0200
  157. /*
  158. * cp2101_get_config
  159. * Reads from the CP2101 configuration registers
  160. * 'size' is specified in bytes.
  161. * 'data' is a pointer to a pre-allocated array of integers large
  162. * enough to hold 'size' bytes (with 4 bytes to each integer)
  163. */
  164. static int cp2101_get_config(struct usb_serial_port* port, u8 request,
  165. unsigned int *data, int size)
  166. {
  167. struct usb_serial *serial = port->serial;
  168. __le32 *buf;
  169. int result, i, length;
  170. /* Number of integers required to contain the array */
  171. length = (((size - 1) | 3) + 1)/4;
  172. buf = kcalloc(length, sizeof(__le32), GFP_KERNEL);
  173. if (!buf) {
  174. dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
  175. return -ENOMEM;
  176. }
  177. /* For get requests, the request number must be incremented */
  178. request++;
  179. /* Issue the request, attempting to read 'size' bytes */
  180. result = usb_control_msg (serial->dev,usb_rcvctrlpipe (serial->dev, 0),
  181. request, REQTYPE_DEVICE_TO_HOST, 0x0000,
  182. 0, buf, size, 300);
  183. /* Convert data into an array of integers */
  184. for (i=0; i<length; i++)
  185. data[i] = le32_to_cpu(buf[i]);
  186. kfree(buf);
  187. if (result != size) {
  188. dev_err(&port->dev, "%s - Unable to send config request, "
  189. "request=0x%x size=%d result=%d\n",
  190. __FUNCTION__, request, size, result);
  191. return -EPROTO;
  192. }
  193. return 0;
  194. }
  195. /*
  196. * cp2101_set_config
  197. * Writes to the CP2101 configuration registers
  198. * Values less than 16 bits wide are sent directly
  199. * 'size' is specified in bytes.
  200. */
  201. static int cp2101_set_config(struct usb_serial_port* port, u8 request,
  202. unsigned int *data, int size)
  203. {
  204. struct usb_serial *serial = port->serial;
  205. __le32 *buf;
  206. int result, i, length;
  207. /* Number of integers required to contain the array */
  208. length = (((size - 1) | 3) + 1)/4;
  209. buf = kmalloc(length * sizeof(__le32), GFP_KERNEL);
  210. if (!buf) {
  211. dev_err(&port->dev, "%s - out of memory.\n",
  212. __FUNCTION__);
  213. return -ENOMEM;
  214. }
  215. /* Array of integers into bytes */
  216. for (i = 0; i < length; i++)
  217. buf[i] = cpu_to_le32(data[i]);
  218. if (size > 2) {
  219. result = usb_control_msg (serial->dev,
  220. usb_sndctrlpipe(serial->dev, 0),
  221. request, REQTYPE_HOST_TO_DEVICE, 0x0000,
  222. 0, buf, size, 300);
  223. } else {
  224. result = usb_control_msg (serial->dev,
  225. usb_sndctrlpipe(serial->dev, 0),
  226. request, REQTYPE_HOST_TO_DEVICE, data[0],
  227. 0, NULL, 0, 300);
  228. }
  229. kfree(buf);
  230. if ((size > 2 && result != size) || result < 0) {
  231. dev_err(&port->dev, "%s - Unable to send request, "
  232. "request=0x%x size=%d result=%d\n",
  233. __FUNCTION__, request, size, result);
  234. return -EPROTO;
  235. }
  236. /* Single data value */
  237. result = usb_control_msg (serial->dev,
  238. usb_sndctrlpipe(serial->dev, 0),
  239. request, REQTYPE_HOST_TO_DEVICE, data[0],
  240. 0, NULL, 0, 300);
  241. return 0;
  242. }
  243. /*
  244. * cp2101_set_config_single
  245. * Convenience function for calling cp2101_set_config on single data values
  246. * without requiring an integer pointer
  247. */
  248. static inline int cp2101_set_config_single(struct usb_serial_port* port,
  249. u8 request, unsigned int data)
  250. {
  251. return cp2101_set_config(port, request, &data, 2);
  252. }
  253. static int cp2101_open (struct usb_serial_port *port, struct file *filp)
  254. {
  255. struct usb_serial *serial = port->serial;
  256. int result;
  257. dbg("%s - port %d", __FUNCTION__, port->number);
  258. if (cp2101_set_config_single(port, CP2101_UART, UART_ENABLE)) {
  259. dev_err(&port->dev, "%s - Unable to enable UART\n",
  260. __FUNCTION__);
  261. return -EPROTO;
  262. }
  263. /* Start reading from the device */
  264. usb_fill_bulk_urb (port->read_urb, serial->dev,
  265. usb_rcvbulkpipe(serial->dev,
  266. port->bulk_in_endpointAddress),
  267. port->read_urb->transfer_buffer,
  268. port->read_urb->transfer_buffer_length,
  269. serial->type->read_bulk_callback,
  270. port);
  271. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  272. if (result) {
  273. dev_err(&port->dev, "%s - failed resubmitting read urb, "
  274. "error %d\n", __FUNCTION__, result);
  275. return result;
  276. }
  277. /* Configure the termios structure */
  278. cp2101_get_termios(port);
  279. /* Set the DTR and RTS pins low */
  280. cp2101_tiocmset(port, NULL, TIOCM_DTR | TIOCM_RTS, 0);
  281. return 0;
  282. }
  283. static void cp2101_cleanup (struct usb_serial_port *port)
  284. {
  285. struct usb_serial *serial = port->serial;
  286. dbg("%s - port %d", __FUNCTION__, port->number);
  287. if (serial->dev) {
  288. /* shutdown any bulk reads that might be going on */
  289. if (serial->num_bulk_out)
  290. usb_kill_urb(port->write_urb);
  291. if (serial->num_bulk_in)
  292. usb_kill_urb(port->read_urb);
  293. }
  294. }
  295. static void cp2101_close (struct usb_serial_port *port, struct file * filp)
  296. {
  297. dbg("%s - port %d", __FUNCTION__, port->number);
  298. /* shutdown our urbs */
  299. dbg("%s - shutting down urbs", __FUNCTION__);
  300. usb_kill_urb(port->write_urb);
  301. usb_kill_urb(port->read_urb);
  302. cp2101_set_config_single(port, CP2101_UART, UART_DISABLE);
  303. }
  304. /*
  305. * cp2101_get_termios
  306. * Reads the baud rate, data bits, parity, stop bits and flow control mode
  307. * from the device, corrects any unsupported values, and configures the
  308. * termios structure to reflect the state of the device
  309. */
  310. static void cp2101_get_termios (struct usb_serial_port *port)
  311. {
  312. unsigned int cflag, modem_ctl[4];
  313. int baud;
  314. int bits;
  315. dbg("%s - port %d", __FUNCTION__, port->number);
  316. if (!port->tty || !port->tty->termios) {
  317. dbg("%s - no tty structures", __FUNCTION__);
  318. return;
  319. }
  320. cp2101_get_config(port, CP2101_BAUDRATE, &baud, 2);
  321. /* Convert to baudrate */
  322. if (baud)
  323. baud = BAUD_RATE_GEN_FREQ / baud;
  324. dbg("%s - baud rate = %d", __FUNCTION__, baud);
  325. tty_encode_baud_rate(port->tty, baud, baud);
  326. cflag = port->tty->termios->c_cflag;
  327. cp2101_get_config(port, CP2101_BITS, &bits, 2);
  328. cflag &= ~CSIZE;
  329. switch(bits & BITS_DATA_MASK) {
  330. case BITS_DATA_5:
  331. dbg("%s - data bits = 5", __FUNCTION__);
  332. cflag |= CS5;
  333. break;
  334. case BITS_DATA_6:
  335. dbg("%s - data bits = 6", __FUNCTION__);
  336. cflag |= CS6;
  337. break;
  338. case BITS_DATA_7:
  339. dbg("%s - data bits = 7", __FUNCTION__);
  340. cflag |= CS7;
  341. break;
  342. case BITS_DATA_8:
  343. dbg("%s - data bits = 8", __FUNCTION__);
  344. cflag |= CS8;
  345. break;
  346. case BITS_DATA_9:
  347. dbg("%s - data bits = 9 (not supported, "
  348. "using 8 data bits)", __FUNCTION__);
  349. cflag |= CS8;
  350. bits &= ~BITS_DATA_MASK;
  351. bits |= BITS_DATA_8;
  352. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  353. break;
  354. default:
  355. dbg("%s - Unknown number of data bits, "
  356. "using 8", __FUNCTION__);
  357. cflag |= CS8;
  358. bits &= ~BITS_DATA_MASK;
  359. bits |= BITS_DATA_8;
  360. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  361. break;
  362. }
  363. switch(bits & BITS_PARITY_MASK) {
  364. case BITS_PARITY_NONE:
  365. dbg("%s - parity = NONE", __FUNCTION__);
  366. cflag &= ~PARENB;
  367. break;
  368. case BITS_PARITY_ODD:
  369. dbg("%s - parity = ODD", __FUNCTION__);
  370. cflag |= (PARENB|PARODD);
  371. break;
  372. case BITS_PARITY_EVEN:
  373. dbg("%s - parity = EVEN", __FUNCTION__);
  374. cflag &= ~PARODD;
  375. cflag |= PARENB;
  376. break;
  377. case BITS_PARITY_MARK:
  378. dbg("%s - parity = MARK (not supported, "
  379. "disabling parity)", __FUNCTION__);
  380. cflag &= ~PARENB;
  381. bits &= ~BITS_PARITY_MASK;
  382. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  383. break;
  384. case BITS_PARITY_SPACE:
  385. dbg("%s - parity = SPACE (not supported, "
  386. "disabling parity)", __FUNCTION__);
  387. cflag &= ~PARENB;
  388. bits &= ~BITS_PARITY_MASK;
  389. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  390. break;
  391. default:
  392. dbg("%s - Unknown parity mode, "
  393. "disabling parity", __FUNCTION__);
  394. cflag &= ~PARENB;
  395. bits &= ~BITS_PARITY_MASK;
  396. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  397. break;
  398. }
  399. cflag &= ~CSTOPB;
  400. switch(bits & BITS_STOP_MASK) {
  401. case BITS_STOP_1:
  402. dbg("%s - stop bits = 1", __FUNCTION__);
  403. break;
  404. case BITS_STOP_1_5:
  405. dbg("%s - stop bits = 1.5 (not supported, "
  406. "using 1 stop bit)", __FUNCTION__);
  407. bits &= ~BITS_STOP_MASK;
  408. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  409. break;
  410. case BITS_STOP_2:
  411. dbg("%s - stop bits = 2", __FUNCTION__);
  412. cflag |= CSTOPB;
  413. break;
  414. default:
  415. dbg("%s - Unknown number of stop bits, "
  416. "using 1 stop bit", __FUNCTION__);
  417. bits &= ~BITS_STOP_MASK;
  418. cp2101_set_config(port, CP2101_BITS, &bits, 2);
  419. break;
  420. }
  421. cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
  422. if (modem_ctl[0] & 0x0008) {
  423. dbg("%s - flow control = CRTSCTS", __FUNCTION__);
  424. cflag |= CRTSCTS;
  425. } else {
  426. dbg("%s - flow control = NONE", __FUNCTION__);
  427. cflag &= ~CRTSCTS;
  428. }
  429. port->tty->termios->c_cflag = cflag;
  430. }
  431. static void cp2101_set_termios (struct usb_serial_port *port,
  432. struct ktermios *old_termios)
  433. {
  434. unsigned int cflag, old_cflag;
  435. int baud=0, bits;
  436. unsigned int modem_ctl[4];
  437. dbg("%s - port %d", __FUNCTION__, port->number);
  438. if (!port->tty || !port->tty->termios) {
  439. dbg("%s - no tty structures", __FUNCTION__);
  440. return;
  441. }
  442. port->tty->termios->c_cflag &= ~CMSPAR;
  443. cflag = port->tty->termios->c_cflag;
  444. old_cflag = old_termios->c_cflag;
  445. baud = tty_get_baud_rate(port->tty);
  446. /* If the baud rate is to be updated*/
  447. if (baud != tty_termios_baud_rate(old_termios)) {
  448. switch (baud) {
  449. case 0:
  450. case 600:
  451. case 1200:
  452. case 1800:
  453. case 2400:
  454. case 4800:
  455. case 7200:
  456. case 9600:
  457. case 14400:
  458. case 19200:
  459. case 28800:
  460. case 38400:
  461. case 55854:
  462. case 57600:
  463. case 115200:
  464. case 127117:
  465. case 230400:
  466. case 460800:
  467. case 921600:
  468. case 3686400:
  469. break;
  470. default:
  471. baud = 9600;
  472. break;
  473. }
  474. if (baud) {
  475. dbg("%s - Setting baud rate to %d baud", __FUNCTION__,
  476. baud);
  477. if (cp2101_set_config_single(port, CP2101_BAUDRATE,
  478. (BAUD_RATE_GEN_FREQ / baud))) {
  479. dev_err(&port->dev, "Baud rate requested not "
  480. "supported by device\n");
  481. baud = tty_termios_baud_rate(old_termios);
  482. }
  483. }
  484. }
  485. /* Report back the resulting baud rate */
  486. tty_encode_baud_rate(port->tty, baud, baud);
  487. /* If the number of data bits is to be updated */
  488. if ((cflag & CSIZE) != (old_cflag & CSIZE)) {
  489. cp2101_get_config(port, CP2101_BITS, &bits, 2);
  490. bits &= ~BITS_DATA_MASK;
  491. switch (cflag & CSIZE) {
  492. case CS5:
  493. bits |= BITS_DATA_5;
  494. dbg("%s - data bits = 5", __FUNCTION__);
  495. break;
  496. case CS6:
  497. bits |= BITS_DATA_6;
  498. dbg("%s - data bits = 6", __FUNCTION__);
  499. break;
  500. case CS7:
  501. bits |= BITS_DATA_7;
  502. dbg("%s - data bits = 7", __FUNCTION__);
  503. break;
  504. case CS8:
  505. bits |= BITS_DATA_8;
  506. dbg("%s - data bits = 8", __FUNCTION__);
  507. break;
  508. /*case CS9:
  509. bits |= BITS_DATA_9;
  510. dbg("%s - data bits = 9", __FUNCTION__);
  511. break;*/
  512. default:
  513. dev_err(&port->dev, "cp2101 driver does not "
  514. "support the number of bits requested,"
  515. " using 8 bit mode\n");
  516. bits |= BITS_DATA_8;
  517. break;
  518. }
  519. if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
  520. dev_err(&port->dev, "Number of data bits requested "
  521. "not supported by device\n");
  522. }
  523. if ((cflag & (PARENB|PARODD)) != (old_cflag & (PARENB|PARODD))) {
  524. cp2101_get_config(port, CP2101_BITS, &bits, 2);
  525. bits &= ~BITS_PARITY_MASK;
  526. if (cflag & PARENB) {
  527. if (cflag & PARODD) {
  528. bits |= BITS_PARITY_ODD;
  529. dbg("%s - parity = ODD", __FUNCTION__);
  530. } else {
  531. bits |= BITS_PARITY_EVEN;
  532. dbg("%s - parity = EVEN", __FUNCTION__);
  533. }
  534. }
  535. if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
  536. dev_err(&port->dev, "Parity mode not supported "
  537. "by device\n");
  538. }
  539. if ((cflag & CSTOPB) != (old_cflag & CSTOPB)) {
  540. cp2101_get_config(port, CP2101_BITS, &bits, 2);
  541. bits &= ~BITS_STOP_MASK;
  542. if (cflag & CSTOPB) {
  543. bits |= BITS_STOP_2;
  544. dbg("%s - stop bits = 2", __FUNCTION__);
  545. } else {
  546. bits |= BITS_STOP_1;
  547. dbg("%s - stop bits = 1", __FUNCTION__);
  548. }
  549. if (cp2101_set_config(port, CP2101_BITS, &bits, 2))
  550. dev_err(&port->dev, "Number of stop bits requested "
  551. "not supported by device\n");
  552. }
  553. if ((cflag & CRTSCTS) != (old_cflag & CRTSCTS)) {
  554. cp2101_get_config(port, CP2101_MODEMCTL, modem_ctl, 16);
  555. dbg("%s - read modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
  556. __FUNCTION__, modem_ctl[0], modem_ctl[1],
  557. modem_ctl[2], modem_ctl[3]);
  558. if (cflag & CRTSCTS) {
  559. modem_ctl[0] &= ~0x7B;
  560. modem_ctl[0] |= 0x09;
  561. modem_ctl[1] = 0x80;
  562. dbg("%s - flow control = CRTSCTS", __FUNCTION__);
  563. } else {
  564. modem_ctl[0] &= ~0x7B;
  565. modem_ctl[0] |= 0x01;
  566. modem_ctl[1] |= 0x40;
  567. dbg("%s - flow control = NONE", __FUNCTION__);
  568. }
  569. dbg("%s - write modem controls = 0x%.4x 0x%.4x 0x%.4x 0x%.4x",
  570. __FUNCTION__, modem_ctl[0], modem_ctl[1],
  571. modem_ctl[2], modem_ctl[3]);
  572. cp2101_set_config(port, CP2101_MODEMCTL, modem_ctl, 16);
  573. }
  574. }
  575. static int cp2101_tiocmset (struct usb_serial_port *port, struct file *file,
  576. unsigned int set, unsigned int clear)
  577. {
  578. int control = 0;
  579. dbg("%s - port %d", __FUNCTION__, port->number);
  580. if (set & TIOCM_RTS) {
  581. control |= CONTROL_RTS;
  582. control |= CONTROL_WRITE_RTS;
  583. }
  584. if (set & TIOCM_DTR) {
  585. control |= CONTROL_DTR;
  586. control |= CONTROL_WRITE_DTR;
  587. }
  588. if (clear & TIOCM_RTS) {
  589. control &= ~CONTROL_RTS;
  590. control |= CONTROL_WRITE_RTS;
  591. }
  592. if (clear & TIOCM_DTR) {
  593. control &= ~CONTROL_DTR;
  594. control |= CONTROL_WRITE_DTR;
  595. }
  596. dbg("%s - control = 0x%.4x", __FUNCTION__, control);
  597. return cp2101_set_config(port, CP2101_CONTROL, &control, 2);
  598. }
  599. static int cp2101_tiocmget (struct usb_serial_port *port, struct file *file)
  600. {
  601. int control, result;
  602. dbg("%s - port %d", __FUNCTION__, port->number);
  603. cp2101_get_config(port, CP2101_CONTROL, &control, 1);
  604. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  605. |((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  606. |((control & CONTROL_CTS) ? TIOCM_CTS : 0)
  607. |((control & CONTROL_DSR) ? TIOCM_DSR : 0)
  608. |((control & CONTROL_RING)? TIOCM_RI : 0)
  609. |((control & CONTROL_DCD) ? TIOCM_CD : 0);
  610. dbg("%s - control = 0x%.2x", __FUNCTION__, control);
  611. return result;
  612. }
  613. static void cp2101_break_ctl (struct usb_serial_port *port, int break_state)
  614. {
  615. int state;
  616. dbg("%s - port %d", __FUNCTION__, port->number);
  617. if (break_state == 0)
  618. state = BREAK_OFF;
  619. else
  620. state = BREAK_ON;
  621. dbg("%s - turning break %s", __FUNCTION__,
  622. state==BREAK_OFF ? "off" : "on");
  623. cp2101_set_config(port, CP2101_BREAK, &state, 2);
  624. }
  625. static int cp2101_startup (struct usb_serial *serial)
  626. {
  627. /* CP2101 buffers behave strangely unless device is reset */
  628. usb_reset_device(serial->dev);
  629. return 0;
  630. }
  631. static void cp2101_shutdown (struct usb_serial *serial)
  632. {
  633. int i;
  634. dbg("%s", __FUNCTION__);
  635. /* Stop reads and writes on all ports */
  636. for (i=0; i < serial->num_ports; ++i) {
  637. cp2101_cleanup(serial->port[i]);
  638. }
  639. }
  640. static int __init cp2101_init (void)
  641. {
  642. int retval;
  643. retval = usb_serial_register(&cp2101_device);
  644. if (retval)
  645. return retval; /* Failed to register */
  646. retval = usb_register(&cp2101_driver);
  647. if (retval) {
  648. /* Failed to register */
  649. usb_serial_deregister(&cp2101_device);
  650. return retval;
  651. }
  652. /* Success */
  653. info(DRIVER_DESC " " DRIVER_VERSION);
  654. return 0;
  655. }
  656. static void __exit cp2101_exit (void)
  657. {
  658. usb_deregister (&cp2101_driver);
  659. usb_serial_deregister (&cp2101_device);
  660. }
  661. module_init(cp2101_init);
  662. module_exit(cp2101_exit);
  663. MODULE_DESCRIPTION(DRIVER_DESC);
  664. MODULE_VERSION(DRIVER_VERSION);
  665. MODULE_LICENSE("GPL");
  666. module_param(debug, bool, S_IRUGO | S_IWUSR);
  667. MODULE_PARM_DESC(debug, "Enable verbose debugging messages");