pl2303.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. * Prolific PL2303 USB to serial adaptor driver
  3. *
  4. * Copyright (C) 2001-2007 Greg Kroah-Hartman (greg@kroah.com)
  5. * Copyright (C) 2003 IBM Corp.
  6. *
  7. * Original driver for 2.2.x by anonymous
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * See Documentation/usb/usb-serial.txt for more information on using this
  14. * driver
  15. *
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/errno.h>
  19. #include <linux/init.h>
  20. #include <linux/slab.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_driver.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/serial.h>
  25. #include <linux/module.h>
  26. #include <linux/moduleparam.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/uaccess.h>
  29. #include <linux/usb.h>
  30. #include <linux/usb/serial.h>
  31. #include "pl2303.h"
  32. /*
  33. * Version Information
  34. */
  35. #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
  36. static const struct usb_device_id id_table[] = {
  37. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
  38. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
  39. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
  40. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
  41. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
  42. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_ALDIGA) },
  43. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MMX) },
  44. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_GPRS) },
  45. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_HCR331) },
  46. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_MOTOROLA) },
  47. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
  48. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID_RSAQ5) },
  49. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
  50. { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
  51. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
  52. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
  53. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
  54. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
  55. { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
  56. { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
  57. { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
  58. { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
  59. { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
  60. { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
  61. { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
  62. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
  63. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) },
  64. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
  65. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
  66. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_EF81) },
  67. { USB_DEVICE(BENQ_VENDOR_ID, BENQ_PRODUCT_ID_S81) }, /* Benq/Siemens S81 */
  68. { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
  69. { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
  70. { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
  71. { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
  72. { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
  73. { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
  74. { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
  75. { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
  76. { USB_DEVICE(ALCOR_VENDOR_ID, ALCOR_PRODUCT_ID) },
  77. { USB_DEVICE(WS002IN_VENDOR_ID, WS002IN_PRODUCT_ID) },
  78. { USB_DEVICE(COREGA_VENDOR_ID, COREGA_PRODUCT_ID) },
  79. { USB_DEVICE(YCCABLE_VENDOR_ID, YCCABLE_PRODUCT_ID) },
  80. { USB_DEVICE(SUPERIAL_VENDOR_ID, SUPERIAL_PRODUCT_ID) },
  81. { USB_DEVICE(HP_VENDOR_ID, HP_LD220_PRODUCT_ID) },
  82. { USB_DEVICE(CRESSI_VENDOR_ID, CRESSI_EDY_PRODUCT_ID) },
  83. { USB_DEVICE(ZEAGLE_VENDOR_ID, ZEAGLE_N2ITION3_PRODUCT_ID) },
  84. { USB_DEVICE(SONY_VENDOR_ID, SONY_QN3USB_PRODUCT_ID) },
  85. { USB_DEVICE(SANWA_VENDOR_ID, SANWA_PRODUCT_ID) },
  86. { USB_DEVICE(ADLINK_VENDOR_ID, ADLINK_ND6530_PRODUCT_ID) },
  87. { USB_DEVICE(SMART_VENDOR_ID, SMART_PRODUCT_ID) },
  88. { } /* Terminating entry */
  89. };
  90. MODULE_DEVICE_TABLE(usb, id_table);
  91. #define SET_LINE_REQUEST_TYPE 0x21
  92. #define SET_LINE_REQUEST 0x20
  93. #define SET_CONTROL_REQUEST_TYPE 0x21
  94. #define SET_CONTROL_REQUEST 0x22
  95. #define CONTROL_DTR 0x01
  96. #define CONTROL_RTS 0x02
  97. #define BREAK_REQUEST_TYPE 0x21
  98. #define BREAK_REQUEST 0x23
  99. #define BREAK_ON 0xffff
  100. #define BREAK_OFF 0x0000
  101. #define GET_LINE_REQUEST_TYPE 0xa1
  102. #define GET_LINE_REQUEST 0x21
  103. #define VENDOR_WRITE_REQUEST_TYPE 0x40
  104. #define VENDOR_WRITE_REQUEST 0x01
  105. #define VENDOR_READ_REQUEST_TYPE 0xc0
  106. #define VENDOR_READ_REQUEST 0x01
  107. #define UART_STATE 0x08
  108. #define UART_STATE_TRANSIENT_MASK 0x74
  109. #define UART_DCD 0x01
  110. #define UART_DSR 0x02
  111. #define UART_BREAK_ERROR 0x04
  112. #define UART_RING 0x08
  113. #define UART_FRAME_ERROR 0x10
  114. #define UART_PARITY_ERROR 0x20
  115. #define UART_OVERRUN_ERROR 0x40
  116. #define UART_CTS 0x80
  117. enum pl2303_type {
  118. type_0, /* don't know the difference between type 0 and */
  119. type_1, /* type 1, until someone from prolific tells us... */
  120. HX, /* HX version of the pl2303 chip */
  121. };
  122. struct pl2303_serial_private {
  123. enum pl2303_type type;
  124. };
  125. struct pl2303_private {
  126. spinlock_t lock;
  127. u8 line_control;
  128. u8 line_status;
  129. };
  130. static int pl2303_vendor_read(__u16 value, __u16 index,
  131. struct usb_serial *serial, unsigned char *buf)
  132. {
  133. int res = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  134. VENDOR_READ_REQUEST, VENDOR_READ_REQUEST_TYPE,
  135. value, index, buf, 1, 100);
  136. dev_dbg(&serial->dev->dev, "0x%x:0x%x:0x%x:0x%x %d - %x\n",
  137. VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, value, index,
  138. res, buf[0]);
  139. return res;
  140. }
  141. static int pl2303_vendor_write(__u16 value, __u16 index,
  142. struct usb_serial *serial)
  143. {
  144. int res = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  145. VENDOR_WRITE_REQUEST, VENDOR_WRITE_REQUEST_TYPE,
  146. value, index, NULL, 0, 100);
  147. dev_dbg(&serial->dev->dev, "0x%x:0x%x:0x%x:0x%x %d\n",
  148. VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, value, index,
  149. res);
  150. return res;
  151. }
  152. static int pl2303_startup(struct usb_serial *serial)
  153. {
  154. struct pl2303_serial_private *spriv;
  155. enum pl2303_type type = type_0;
  156. unsigned char *buf;
  157. spriv = kzalloc(sizeof(*spriv), GFP_KERNEL);
  158. if (!spriv)
  159. return -ENOMEM;
  160. buf = kmalloc(10, GFP_KERNEL);
  161. if (!buf) {
  162. kfree(spriv);
  163. return -ENOMEM;
  164. }
  165. if (serial->dev->descriptor.bDeviceClass == 0x02)
  166. type = type_0;
  167. else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
  168. type = HX;
  169. else if (serial->dev->descriptor.bDeviceClass == 0x00)
  170. type = type_1;
  171. else if (serial->dev->descriptor.bDeviceClass == 0xFF)
  172. type = type_1;
  173. dev_dbg(&serial->interface->dev, "device type: %d\n", type);
  174. spriv->type = type;
  175. usb_set_serial_data(serial, spriv);
  176. pl2303_vendor_read(0x8484, 0, serial, buf);
  177. pl2303_vendor_write(0x0404, 0, serial);
  178. pl2303_vendor_read(0x8484, 0, serial, buf);
  179. pl2303_vendor_read(0x8383, 0, serial, buf);
  180. pl2303_vendor_read(0x8484, 0, serial, buf);
  181. pl2303_vendor_write(0x0404, 1, serial);
  182. pl2303_vendor_read(0x8484, 0, serial, buf);
  183. pl2303_vendor_read(0x8383, 0, serial, buf);
  184. pl2303_vendor_write(0, 1, serial);
  185. pl2303_vendor_write(1, 0, serial);
  186. if (type == HX)
  187. pl2303_vendor_write(2, 0x44, serial);
  188. else
  189. pl2303_vendor_write(2, 0x24, serial);
  190. kfree(buf);
  191. return 0;
  192. }
  193. static void pl2303_release(struct usb_serial *serial)
  194. {
  195. struct pl2303_serial_private *spriv;
  196. spriv = usb_get_serial_data(serial);
  197. kfree(spriv);
  198. }
  199. static int pl2303_port_probe(struct usb_serial_port *port)
  200. {
  201. struct pl2303_private *priv;
  202. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  203. if (!priv)
  204. return -ENOMEM;
  205. spin_lock_init(&priv->lock);
  206. usb_set_serial_port_data(port, priv);
  207. return 0;
  208. }
  209. static int pl2303_port_remove(struct usb_serial_port *port)
  210. {
  211. struct pl2303_private *priv;
  212. priv = usb_get_serial_port_data(port);
  213. kfree(priv);
  214. return 0;
  215. }
  216. static int set_control_lines(struct usb_device *dev, u8 value)
  217. {
  218. int retval;
  219. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  220. SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
  221. value, 0, NULL, 0, 100);
  222. dev_dbg(&dev->dev, "%s - value = %d, retval = %d\n", __func__,
  223. value, retval);
  224. return retval;
  225. }
  226. static void pl2303_set_termios(struct tty_struct *tty,
  227. struct usb_serial_port *port, struct ktermios *old_termios)
  228. {
  229. struct usb_serial *serial = port->serial;
  230. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  231. struct pl2303_private *priv = usb_get_serial_port_data(port);
  232. unsigned long flags;
  233. unsigned int cflag;
  234. unsigned char *buf;
  235. int baud;
  236. int i;
  237. u8 control;
  238. const int baud_sup[] = { 75, 150, 300, 600, 1200, 1800, 2400, 3600,
  239. 4800, 7200, 9600, 14400, 19200, 28800, 38400,
  240. 57600, 115200, 230400, 460800, 614400,
  241. 921600, 1228800, 2457600, 3000000, 6000000 };
  242. int baud_floor, baud_ceil;
  243. int k;
  244. /* The PL2303 is reported to lose bytes if you change
  245. serial settings even to the same values as before. Thus
  246. we actually need to filter in this specific case */
  247. if (!tty_termios_hw_change(&tty->termios, old_termios))
  248. return;
  249. cflag = tty->termios.c_cflag;
  250. buf = kzalloc(7, GFP_KERNEL);
  251. if (!buf) {
  252. dev_err(&port->dev, "%s - out of memory.\n", __func__);
  253. /* Report back no change occurred */
  254. tty->termios = *old_termios;
  255. return;
  256. }
  257. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  258. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  259. 0, 0, buf, 7, 100);
  260. dev_dbg(&port->dev, "0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x\n", i,
  261. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
  262. if (cflag & CSIZE) {
  263. switch (cflag & CSIZE) {
  264. case CS5:
  265. buf[6] = 5;
  266. break;
  267. case CS6:
  268. buf[6] = 6;
  269. break;
  270. case CS7:
  271. buf[6] = 7;
  272. break;
  273. default:
  274. case CS8:
  275. buf[6] = 8;
  276. break;
  277. }
  278. dev_dbg(&port->dev, "data bits = %d\n", buf[6]);
  279. }
  280. /* For reference buf[0]:buf[3] baud rate value */
  281. /* NOTE: Only the values defined in baud_sup are supported !
  282. * => if unsupported values are set, the PL2303 seems to use
  283. * 9600 baud (at least my PL2303X always does)
  284. */
  285. baud = tty_get_baud_rate(tty);
  286. dev_dbg(&port->dev, "baud requested = %d\n", baud);
  287. if (baud) {
  288. /* Set baudrate to nearest supported value */
  289. for (k=0; k<ARRAY_SIZE(baud_sup); k++) {
  290. if (baud_sup[k] / baud) {
  291. baud_ceil = baud_sup[k];
  292. if (k==0) {
  293. baud = baud_ceil;
  294. } else {
  295. baud_floor = baud_sup[k-1];
  296. if ((baud_ceil % baud)
  297. > (baud % baud_floor))
  298. baud = baud_floor;
  299. else
  300. baud = baud_ceil;
  301. }
  302. break;
  303. }
  304. }
  305. if (baud > 1228800) {
  306. /* type_0, type_1 only support up to 1228800 baud */
  307. if (spriv->type != HX)
  308. baud = 1228800;
  309. else if (baud > 6000000)
  310. baud = 6000000;
  311. }
  312. dev_dbg(&port->dev, "baud set = %d\n", baud);
  313. if (baud <= 115200) {
  314. buf[0] = baud & 0xff;
  315. buf[1] = (baud >> 8) & 0xff;
  316. buf[2] = (baud >> 16) & 0xff;
  317. buf[3] = (baud >> 24) & 0xff;
  318. } else {
  319. /* apparently the formula for higher speeds is:
  320. * baudrate = 12M * 32 / (2^buf[1]) / buf[0]
  321. */
  322. unsigned tmp = 12*1000*1000*32 / baud;
  323. buf[3] = 0x80;
  324. buf[2] = 0;
  325. buf[1] = (tmp >= 256);
  326. while (tmp >= 256) {
  327. tmp >>= 2;
  328. buf[1] <<= 1;
  329. }
  330. buf[0] = tmp;
  331. }
  332. }
  333. /* For reference buf[4]=0 is 1 stop bits */
  334. /* For reference buf[4]=1 is 1.5 stop bits */
  335. /* For reference buf[4]=2 is 2 stop bits */
  336. if (cflag & CSTOPB) {
  337. /* NOTE: Comply with "real" UARTs / RS232:
  338. * use 1.5 instead of 2 stop bits with 5 data bits
  339. */
  340. if ((cflag & CSIZE) == CS5) {
  341. buf[4] = 1;
  342. dev_dbg(&port->dev, "stop bits = 1.5\n");
  343. } else {
  344. buf[4] = 2;
  345. dev_dbg(&port->dev, "stop bits = 2\n");
  346. }
  347. } else {
  348. buf[4] = 0;
  349. dev_dbg(&port->dev, "stop bits = 1\n");
  350. }
  351. if (cflag & PARENB) {
  352. /* For reference buf[5]=0 is none parity */
  353. /* For reference buf[5]=1 is odd parity */
  354. /* For reference buf[5]=2 is even parity */
  355. /* For reference buf[5]=3 is mark parity */
  356. /* For reference buf[5]=4 is space parity */
  357. if (cflag & PARODD) {
  358. if (cflag & CMSPAR) {
  359. buf[5] = 3;
  360. dev_dbg(&port->dev, "parity = mark\n");
  361. } else {
  362. buf[5] = 1;
  363. dev_dbg(&port->dev, "parity = odd\n");
  364. }
  365. } else {
  366. if (cflag & CMSPAR) {
  367. buf[5] = 4;
  368. dev_dbg(&port->dev, "parity = space\n");
  369. } else {
  370. buf[5] = 2;
  371. dev_dbg(&port->dev, "parity = even\n");
  372. }
  373. }
  374. } else {
  375. buf[5] = 0;
  376. dev_dbg(&port->dev, "parity = none\n");
  377. }
  378. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  379. SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
  380. 0, 0, buf, 7, 100);
  381. dev_dbg(&port->dev, "0x21:0x20:0:0 %d\n", i);
  382. /* change control lines if we are switching to or from B0 */
  383. spin_lock_irqsave(&priv->lock, flags);
  384. control = priv->line_control;
  385. if ((cflag & CBAUD) == B0)
  386. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  387. else if ((old_termios->c_cflag & CBAUD) == B0)
  388. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  389. if (control != priv->line_control) {
  390. control = priv->line_control;
  391. spin_unlock_irqrestore(&priv->lock, flags);
  392. set_control_lines(serial->dev, control);
  393. } else {
  394. spin_unlock_irqrestore(&priv->lock, flags);
  395. }
  396. buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
  397. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  398. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  399. 0, 0, buf, 7, 100);
  400. dev_dbg(&port->dev, "0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x\n", i,
  401. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
  402. if (cflag & CRTSCTS) {
  403. if (spriv->type == HX)
  404. pl2303_vendor_write(0x0, 0x61, serial);
  405. else
  406. pl2303_vendor_write(0x0, 0x41, serial);
  407. } else {
  408. pl2303_vendor_write(0x0, 0x0, serial);
  409. }
  410. /* Save resulting baud rate */
  411. if (baud)
  412. tty_encode_baud_rate(tty, baud, baud);
  413. kfree(buf);
  414. }
  415. static void pl2303_dtr_rts(struct usb_serial_port *port, int on)
  416. {
  417. struct pl2303_private *priv = usb_get_serial_port_data(port);
  418. unsigned long flags;
  419. u8 control;
  420. spin_lock_irqsave(&priv->lock, flags);
  421. /* Change DTR and RTS */
  422. if (on)
  423. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  424. else
  425. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  426. control = priv->line_control;
  427. spin_unlock_irqrestore(&priv->lock, flags);
  428. set_control_lines(port->serial->dev, control);
  429. }
  430. static void pl2303_close(struct usb_serial_port *port)
  431. {
  432. usb_serial_generic_close(port);
  433. usb_kill_urb(port->interrupt_in_urb);
  434. }
  435. static int pl2303_open(struct tty_struct *tty, struct usb_serial_port *port)
  436. {
  437. struct ktermios tmp_termios;
  438. struct usb_serial *serial = port->serial;
  439. struct pl2303_serial_private *spriv = usb_get_serial_data(serial);
  440. int result;
  441. if (spriv->type != HX) {
  442. usb_clear_halt(serial->dev, port->write_urb->pipe);
  443. usb_clear_halt(serial->dev, port->read_urb->pipe);
  444. } else {
  445. /* reset upstream data pipes */
  446. pl2303_vendor_write(8, 0, serial);
  447. pl2303_vendor_write(9, 0, serial);
  448. }
  449. /* Setup termios */
  450. if (tty)
  451. pl2303_set_termios(tty, port, &tmp_termios);
  452. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  453. if (result) {
  454. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  455. " error %d\n", __func__, result);
  456. return result;
  457. }
  458. result = usb_serial_generic_open(tty, port);
  459. if (result) {
  460. usb_kill_urb(port->interrupt_in_urb);
  461. return result;
  462. }
  463. port->port.drain_delay = 256;
  464. return 0;
  465. }
  466. static int pl2303_tiocmset(struct tty_struct *tty,
  467. unsigned int set, unsigned int clear)
  468. {
  469. struct usb_serial_port *port = tty->driver_data;
  470. struct usb_serial *serial = port->serial;
  471. struct pl2303_private *priv = usb_get_serial_port_data(port);
  472. unsigned long flags;
  473. u8 control;
  474. int ret;
  475. spin_lock_irqsave(&priv->lock, flags);
  476. if (set & TIOCM_RTS)
  477. priv->line_control |= CONTROL_RTS;
  478. if (set & TIOCM_DTR)
  479. priv->line_control |= CONTROL_DTR;
  480. if (clear & TIOCM_RTS)
  481. priv->line_control &= ~CONTROL_RTS;
  482. if (clear & TIOCM_DTR)
  483. priv->line_control &= ~CONTROL_DTR;
  484. control = priv->line_control;
  485. spin_unlock_irqrestore(&priv->lock, flags);
  486. mutex_lock(&serial->disc_mutex);
  487. if (!serial->disconnected)
  488. ret = set_control_lines(serial->dev, control);
  489. else
  490. ret = -ENODEV;
  491. mutex_unlock(&serial->disc_mutex);
  492. return ret;
  493. }
  494. static int pl2303_tiocmget(struct tty_struct *tty)
  495. {
  496. struct usb_serial_port *port = tty->driver_data;
  497. struct pl2303_private *priv = usb_get_serial_port_data(port);
  498. unsigned long flags;
  499. unsigned int mcr;
  500. unsigned int status;
  501. unsigned int result;
  502. spin_lock_irqsave(&priv->lock, flags);
  503. mcr = priv->line_control;
  504. status = priv->line_status;
  505. spin_unlock_irqrestore(&priv->lock, flags);
  506. result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
  507. | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
  508. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  509. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  510. | ((status & UART_RING) ? TIOCM_RI : 0)
  511. | ((status & UART_DCD) ? TIOCM_CD : 0);
  512. dev_dbg(&port->dev, "%s - result = %x\n", __func__, result);
  513. return result;
  514. }
  515. static int pl2303_carrier_raised(struct usb_serial_port *port)
  516. {
  517. struct pl2303_private *priv = usb_get_serial_port_data(port);
  518. if (priv->line_status & UART_DCD)
  519. return 1;
  520. return 0;
  521. }
  522. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  523. {
  524. struct pl2303_private *priv = usb_get_serial_port_data(port);
  525. unsigned long flags;
  526. unsigned int prevstatus;
  527. unsigned int status;
  528. unsigned int changed;
  529. spin_lock_irqsave(&priv->lock, flags);
  530. prevstatus = priv->line_status;
  531. spin_unlock_irqrestore(&priv->lock, flags);
  532. while (1) {
  533. interruptible_sleep_on(&port->delta_msr_wait);
  534. /* see if a signal did it */
  535. if (signal_pending(current))
  536. return -ERESTARTSYS;
  537. if (port->serial->disconnected)
  538. return -EIO;
  539. spin_lock_irqsave(&priv->lock, flags);
  540. status = priv->line_status;
  541. spin_unlock_irqrestore(&priv->lock, flags);
  542. changed = prevstatus ^ status;
  543. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  544. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  545. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  546. ((arg & TIOCM_CTS) && (changed & UART_CTS))) {
  547. return 0;
  548. }
  549. prevstatus = status;
  550. }
  551. /* NOTREACHED */
  552. return 0;
  553. }
  554. static int pl2303_ioctl(struct tty_struct *tty,
  555. unsigned int cmd, unsigned long arg)
  556. {
  557. struct serial_struct ser;
  558. struct usb_serial_port *port = tty->driver_data;
  559. dev_dbg(&port->dev, "%s cmd = 0x%04x\n", __func__, cmd);
  560. switch (cmd) {
  561. case TIOCGSERIAL:
  562. memset(&ser, 0, sizeof ser);
  563. ser.type = PORT_16654;
  564. ser.line = port->serial->minor;
  565. ser.port = port->number;
  566. ser.baud_base = 460800;
  567. if (copy_to_user((void __user *)arg, &ser, sizeof ser))
  568. return -EFAULT;
  569. return 0;
  570. case TIOCMIWAIT:
  571. dev_dbg(&port->dev, "%s TIOCMIWAIT\n", __func__);
  572. return wait_modem_info(port, arg);
  573. default:
  574. dev_dbg(&port->dev, "%s not supported = 0x%04x\n", __func__, cmd);
  575. break;
  576. }
  577. return -ENOIOCTLCMD;
  578. }
  579. static void pl2303_break_ctl(struct tty_struct *tty, int break_state)
  580. {
  581. struct usb_serial_port *port = tty->driver_data;
  582. struct usb_serial *serial = port->serial;
  583. u16 state;
  584. int result;
  585. if (break_state == 0)
  586. state = BREAK_OFF;
  587. else
  588. state = BREAK_ON;
  589. dev_dbg(&port->dev, "%s - turning break %s\n", __func__,
  590. state == BREAK_OFF ? "off" : "on");
  591. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  592. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  593. 0, NULL, 0, 100);
  594. if (result)
  595. dev_err(&port->dev, "error sending break = %d\n", result);
  596. }
  597. static void pl2303_update_line_status(struct usb_serial_port *port,
  598. unsigned char *data,
  599. unsigned int actual_length)
  600. {
  601. struct pl2303_private *priv = usb_get_serial_port_data(port);
  602. struct tty_struct *tty;
  603. unsigned long flags;
  604. u8 status_idx = UART_STATE;
  605. u8 length = UART_STATE + 1;
  606. u8 prev_line_status;
  607. u16 idv, idp;
  608. idv = le16_to_cpu(port->serial->dev->descriptor.idVendor);
  609. idp = le16_to_cpu(port->serial->dev->descriptor.idProduct);
  610. if (idv == SIEMENS_VENDOR_ID) {
  611. if (idp == SIEMENS_PRODUCT_ID_X65 ||
  612. idp == SIEMENS_PRODUCT_ID_SX1 ||
  613. idp == SIEMENS_PRODUCT_ID_X75) {
  614. length = 1;
  615. status_idx = 0;
  616. }
  617. }
  618. if (actual_length < length)
  619. return;
  620. /* Save off the uart status for others to look at */
  621. spin_lock_irqsave(&priv->lock, flags);
  622. prev_line_status = priv->line_status;
  623. priv->line_status = data[status_idx];
  624. spin_unlock_irqrestore(&priv->lock, flags);
  625. if (priv->line_status & UART_BREAK_ERROR)
  626. usb_serial_handle_break(port);
  627. wake_up_interruptible(&port->delta_msr_wait);
  628. tty = tty_port_tty_get(&port->port);
  629. if (!tty)
  630. return;
  631. if ((priv->line_status ^ prev_line_status) & UART_DCD)
  632. usb_serial_handle_dcd_change(port, tty,
  633. priv->line_status & UART_DCD);
  634. tty_kref_put(tty);
  635. }
  636. static void pl2303_read_int_callback(struct urb *urb)
  637. {
  638. struct usb_serial_port *port = urb->context;
  639. unsigned char *data = urb->transfer_buffer;
  640. unsigned int actual_length = urb->actual_length;
  641. int status = urb->status;
  642. int retval;
  643. switch (status) {
  644. case 0:
  645. /* success */
  646. break;
  647. case -ECONNRESET:
  648. case -ENOENT:
  649. case -ESHUTDOWN:
  650. /* this urb is terminated, clean up */
  651. dev_dbg(&port->dev, "%s - urb shutting down with status: %d\n",
  652. __func__, status);
  653. return;
  654. default:
  655. dev_dbg(&port->dev, "%s - nonzero urb status received: %d\n",
  656. __func__, status);
  657. goto exit;
  658. }
  659. usb_serial_debug_data(&port->dev, __func__,
  660. urb->actual_length, urb->transfer_buffer);
  661. pl2303_update_line_status(port, data, actual_length);
  662. exit:
  663. retval = usb_submit_urb(urb, GFP_ATOMIC);
  664. if (retval)
  665. dev_err(&port->dev,
  666. "%s - usb_submit_urb failed with result %d\n",
  667. __func__, retval);
  668. }
  669. static void pl2303_process_read_urb(struct urb *urb)
  670. {
  671. struct usb_serial_port *port = urb->context;
  672. struct pl2303_private *priv = usb_get_serial_port_data(port);
  673. unsigned char *data = urb->transfer_buffer;
  674. char tty_flag = TTY_NORMAL;
  675. unsigned long flags;
  676. u8 line_status;
  677. int i;
  678. /* update line status */
  679. spin_lock_irqsave(&priv->lock, flags);
  680. line_status = priv->line_status;
  681. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  682. spin_unlock_irqrestore(&priv->lock, flags);
  683. wake_up_interruptible(&port->delta_msr_wait);
  684. if (!urb->actual_length)
  685. return;
  686. /* break takes precedence over parity, */
  687. /* which takes precedence over framing errors */
  688. if (line_status & UART_BREAK_ERROR)
  689. tty_flag = TTY_BREAK;
  690. else if (line_status & UART_PARITY_ERROR)
  691. tty_flag = TTY_PARITY;
  692. else if (line_status & UART_FRAME_ERROR)
  693. tty_flag = TTY_FRAME;
  694. dev_dbg(&port->dev, "%s - tty_flag = %d\n", __func__, tty_flag);
  695. /* overrun is special, not associated with a char */
  696. if (line_status & UART_OVERRUN_ERROR)
  697. tty_insert_flip_char(&port->port, 0, TTY_OVERRUN);
  698. if (port->port.console && port->sysrq) {
  699. for (i = 0; i < urb->actual_length; ++i)
  700. if (!usb_serial_handle_sysrq_char(port, data[i]))
  701. tty_insert_flip_char(&port->port, data[i],
  702. tty_flag);
  703. } else {
  704. tty_insert_flip_string_fixed_flag(&port->port, data, tty_flag,
  705. urb->actual_length);
  706. }
  707. tty_flip_buffer_push(&port->port);
  708. }
  709. /* All of the device info needed for the PL2303 SIO serial converter */
  710. static struct usb_serial_driver pl2303_device = {
  711. .driver = {
  712. .owner = THIS_MODULE,
  713. .name = "pl2303",
  714. },
  715. .id_table = id_table,
  716. .num_ports = 1,
  717. .bulk_in_size = 256,
  718. .bulk_out_size = 256,
  719. .open = pl2303_open,
  720. .close = pl2303_close,
  721. .dtr_rts = pl2303_dtr_rts,
  722. .carrier_raised = pl2303_carrier_raised,
  723. .ioctl = pl2303_ioctl,
  724. .break_ctl = pl2303_break_ctl,
  725. .set_termios = pl2303_set_termios,
  726. .tiocmget = pl2303_tiocmget,
  727. .tiocmset = pl2303_tiocmset,
  728. .process_read_urb = pl2303_process_read_urb,
  729. .read_int_callback = pl2303_read_int_callback,
  730. .attach = pl2303_startup,
  731. .release = pl2303_release,
  732. .port_probe = pl2303_port_probe,
  733. .port_remove = pl2303_port_remove,
  734. };
  735. static struct usb_serial_driver * const serial_drivers[] = {
  736. &pl2303_device, NULL
  737. };
  738. module_usb_serial_driver(serial_drivers, id_table);
  739. MODULE_DESCRIPTION(DRIVER_DESC);
  740. MODULE_LICENSE("GPL");