cp2101.c 22 KB

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