cp210x.c 24 KB

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