cp2101.c 21 KB

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