pl2303.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /*
  2. * Prolific PL2303 USB to serial adaptor driver
  3. *
  4. * Copyright (C) 2001-2004 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 modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License.
  12. *
  13. * See Documentation/usb/usb-serial.txt for more information on using this driver
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/serial.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/spinlock.h>
  27. #include <asm/uaccess.h>
  28. #include <linux/usb.h>
  29. #include <linux/usb/serial.h>
  30. #include "pl2303.h"
  31. /*
  32. * Version Information
  33. */
  34. #define DRIVER_DESC "Prolific PL2303 USB to serial adaptor driver"
  35. static int debug;
  36. #define PL2303_CLOSING_WAIT (30*HZ)
  37. #define PL2303_BUF_SIZE 1024
  38. #define PL2303_TMP_BUF_SIZE 1024
  39. struct pl2303_buf {
  40. unsigned int buf_size;
  41. char *buf_buf;
  42. char *buf_get;
  43. char *buf_put;
  44. };
  45. static struct usb_device_id id_table [] = {
  46. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID) },
  47. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ2) },
  48. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_DCU11) },
  49. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_RSAQ3) },
  50. { USB_DEVICE(PL2303_VENDOR_ID, PL2303_PRODUCT_ID_PHAROS) },
  51. { USB_DEVICE(IODATA_VENDOR_ID, IODATA_PRODUCT_ID) },
  52. { USB_DEVICE(ATEN_VENDOR_ID, ATEN_PRODUCT_ID) },
  53. { USB_DEVICE(ATEN_VENDOR_ID2, ATEN_PRODUCT_ID) },
  54. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID) },
  55. { USB_DEVICE(ELCOM_VENDOR_ID, ELCOM_PRODUCT_ID_UCSGT) },
  56. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID) },
  57. { USB_DEVICE(ITEGNO_VENDOR_ID, ITEGNO_PRODUCT_ID_2080) },
  58. { USB_DEVICE(MA620_VENDOR_ID, MA620_PRODUCT_ID) },
  59. { USB_DEVICE(RATOC_VENDOR_ID, RATOC_PRODUCT_ID) },
  60. { USB_DEVICE(TRIPP_VENDOR_ID, TRIPP_PRODUCT_ID) },
  61. { USB_DEVICE(RADIOSHACK_VENDOR_ID, RADIOSHACK_PRODUCT_ID) },
  62. { USB_DEVICE(DCU10_VENDOR_ID, DCU10_PRODUCT_ID) },
  63. { USB_DEVICE(SITECOM_VENDOR_ID, SITECOM_PRODUCT_ID) },
  64. { USB_DEVICE(ALCATEL_VENDOR_ID, ALCATEL_PRODUCT_ID) },
  65. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_PRODUCT_ID) },
  66. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_SX1) },
  67. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X65) },
  68. { USB_DEVICE(SIEMENS_VENDOR_ID, SIEMENS_PRODUCT_ID_X75) },
  69. { USB_DEVICE(SYNTECH_VENDOR_ID, SYNTECH_PRODUCT_ID) },
  70. { USB_DEVICE(NOKIA_CA42_VENDOR_ID, NOKIA_CA42_PRODUCT_ID) },
  71. { USB_DEVICE(CA_42_CA42_VENDOR_ID, CA_42_CA42_PRODUCT_ID) },
  72. { USB_DEVICE(SAGEM_VENDOR_ID, SAGEM_PRODUCT_ID) },
  73. { USB_DEVICE(LEADTEK_VENDOR_ID, LEADTEK_9531_PRODUCT_ID) },
  74. { USB_DEVICE(SPEEDDRAGON_VENDOR_ID, SPEEDDRAGON_PRODUCT_ID) },
  75. { USB_DEVICE(DATAPILOT_U2_VENDOR_ID, DATAPILOT_U2_PRODUCT_ID) },
  76. { USB_DEVICE(BELKIN_VENDOR_ID, BELKIN_PRODUCT_ID) },
  77. { } /* Terminating entry */
  78. };
  79. MODULE_DEVICE_TABLE(usb, id_table);
  80. static struct usb_driver pl2303_driver = {
  81. .name = "pl2303",
  82. .probe = usb_serial_probe,
  83. .disconnect = usb_serial_disconnect,
  84. .id_table = id_table,
  85. .no_dynamic_id = 1,
  86. };
  87. #define SET_LINE_REQUEST_TYPE 0x21
  88. #define SET_LINE_REQUEST 0x20
  89. #define SET_CONTROL_REQUEST_TYPE 0x21
  90. #define SET_CONTROL_REQUEST 0x22
  91. #define CONTROL_DTR 0x01
  92. #define CONTROL_RTS 0x02
  93. #define BREAK_REQUEST_TYPE 0x21
  94. #define BREAK_REQUEST 0x23
  95. #define BREAK_ON 0xffff
  96. #define BREAK_OFF 0x0000
  97. #define GET_LINE_REQUEST_TYPE 0xa1
  98. #define GET_LINE_REQUEST 0x21
  99. #define VENDOR_WRITE_REQUEST_TYPE 0x40
  100. #define VENDOR_WRITE_REQUEST 0x01
  101. #define VENDOR_READ_REQUEST_TYPE 0xc0
  102. #define VENDOR_READ_REQUEST 0x01
  103. #define UART_STATE 0x08
  104. #define UART_STATE_TRANSIENT_MASK 0x74
  105. #define UART_DCD 0x01
  106. #define UART_DSR 0x02
  107. #define UART_BREAK_ERROR 0x04
  108. #define UART_RING 0x08
  109. #define UART_FRAME_ERROR 0x10
  110. #define UART_PARITY_ERROR 0x20
  111. #define UART_OVERRUN_ERROR 0x40
  112. #define UART_CTS 0x80
  113. /* function prototypes for a PL2303 serial converter */
  114. static int pl2303_open (struct usb_serial_port *port, struct file *filp);
  115. static void pl2303_close (struct usb_serial_port *port, struct file *filp);
  116. static void pl2303_set_termios (struct usb_serial_port *port,
  117. struct termios *old);
  118. static int pl2303_ioctl (struct usb_serial_port *port, struct file *file,
  119. unsigned int cmd, unsigned long arg);
  120. static void pl2303_read_int_callback (struct urb *urb, struct pt_regs *regs);
  121. static void pl2303_read_bulk_callback (struct urb *urb, struct pt_regs *regs);
  122. static void pl2303_write_bulk_callback (struct urb *urb, struct pt_regs *regs);
  123. static int pl2303_write (struct usb_serial_port *port,
  124. const unsigned char *buf, int count);
  125. static void pl2303_send (struct usb_serial_port *port);
  126. static int pl2303_write_room(struct usb_serial_port *port);
  127. static int pl2303_chars_in_buffer(struct usb_serial_port *port);
  128. static void pl2303_break_ctl(struct usb_serial_port *port,int break_state);
  129. static int pl2303_tiocmget (struct usb_serial_port *port, struct file *file);
  130. static int pl2303_tiocmset (struct usb_serial_port *port, struct file *file,
  131. unsigned int set, unsigned int clear);
  132. static int pl2303_startup (struct usb_serial *serial);
  133. static void pl2303_shutdown (struct usb_serial *serial);
  134. static struct pl2303_buf *pl2303_buf_alloc(unsigned int size);
  135. static void pl2303_buf_free(struct pl2303_buf *pb);
  136. static void pl2303_buf_clear(struct pl2303_buf *pb);
  137. static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb);
  138. static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb);
  139. static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
  140. unsigned int count);
  141. static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
  142. unsigned int count);
  143. /* All of the device info needed for the PL2303 SIO serial converter */
  144. static struct usb_serial_driver pl2303_device = {
  145. .driver = {
  146. .owner = THIS_MODULE,
  147. .name = "pl2303",
  148. },
  149. .id_table = id_table,
  150. .num_interrupt_in = NUM_DONT_CARE,
  151. .num_bulk_in = 1,
  152. .num_bulk_out = 1,
  153. .num_ports = 1,
  154. .open = pl2303_open,
  155. .close = pl2303_close,
  156. .write = pl2303_write,
  157. .ioctl = pl2303_ioctl,
  158. .break_ctl = pl2303_break_ctl,
  159. .set_termios = pl2303_set_termios,
  160. .tiocmget = pl2303_tiocmget,
  161. .tiocmset = pl2303_tiocmset,
  162. .read_bulk_callback = pl2303_read_bulk_callback,
  163. .read_int_callback = pl2303_read_int_callback,
  164. .write_bulk_callback = pl2303_write_bulk_callback,
  165. .write_room = pl2303_write_room,
  166. .chars_in_buffer = pl2303_chars_in_buffer,
  167. .attach = pl2303_startup,
  168. .shutdown = pl2303_shutdown,
  169. };
  170. enum pl2303_type {
  171. type_0, /* don't know the difference between type 0 and */
  172. type_1, /* type 1, until someone from prolific tells us... */
  173. HX, /* HX version of the pl2303 chip */
  174. };
  175. struct pl2303_private {
  176. spinlock_t lock;
  177. struct pl2303_buf *buf;
  178. int write_urb_in_use;
  179. wait_queue_head_t delta_msr_wait;
  180. u8 line_control;
  181. u8 line_status;
  182. u8 termios_initialized;
  183. enum pl2303_type type;
  184. };
  185. static int pl2303_startup(struct usb_serial *serial)
  186. {
  187. struct pl2303_private *priv;
  188. enum pl2303_type type = type_0;
  189. int i;
  190. if (serial->dev->descriptor.bDeviceClass == 0x02)
  191. type = type_0;
  192. else if (serial->dev->descriptor.bMaxPacketSize0 == 0x40)
  193. type = HX;
  194. else if (serial->dev->descriptor.bDeviceClass == 0x00)
  195. type = type_1;
  196. else if (serial->dev->descriptor.bDeviceClass == 0xFF)
  197. type = type_1;
  198. dbg("device type: %d", type);
  199. for (i = 0; i < serial->num_ports; ++i) {
  200. priv = kzalloc(sizeof(struct pl2303_private), GFP_KERNEL);
  201. if (!priv)
  202. goto cleanup;
  203. spin_lock_init(&priv->lock);
  204. priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
  205. if (priv->buf == NULL) {
  206. kfree(priv);
  207. goto cleanup;
  208. }
  209. init_waitqueue_head(&priv->delta_msr_wait);
  210. priv->type = type;
  211. usb_set_serial_port_data(serial->port[i], priv);
  212. }
  213. return 0;
  214. cleanup:
  215. for (--i; i>=0; --i) {
  216. priv = usb_get_serial_port_data(serial->port[i]);
  217. pl2303_buf_free(priv->buf);
  218. kfree(priv);
  219. usb_set_serial_port_data(serial->port[i], NULL);
  220. }
  221. return -ENOMEM;
  222. }
  223. static int set_control_lines(struct usb_device *dev, u8 value)
  224. {
  225. int retval;
  226. retval = usb_control_msg(dev, usb_sndctrlpipe(dev, 0),
  227. SET_CONTROL_REQUEST, SET_CONTROL_REQUEST_TYPE,
  228. value, 0, NULL, 0, 100);
  229. dbg("%s - value = %d, retval = %d", __FUNCTION__, value, retval);
  230. return retval;
  231. }
  232. static int pl2303_write(struct usb_serial_port *port, const unsigned char *buf,
  233. int count)
  234. {
  235. struct pl2303_private *priv = usb_get_serial_port_data(port);
  236. unsigned long flags;
  237. dbg("%s - port %d, %d bytes", __FUNCTION__, port->number, count);
  238. if (!count)
  239. return count;
  240. spin_lock_irqsave(&priv->lock, flags);
  241. count = pl2303_buf_put(priv->buf, buf, count);
  242. spin_unlock_irqrestore(&priv->lock, flags);
  243. pl2303_send(port);
  244. return count;
  245. }
  246. static void pl2303_send(struct usb_serial_port *port)
  247. {
  248. int count, result;
  249. struct pl2303_private *priv = usb_get_serial_port_data(port);
  250. unsigned long flags;
  251. dbg("%s - port %d", __FUNCTION__, port->number);
  252. spin_lock_irqsave(&priv->lock, flags);
  253. if (priv->write_urb_in_use) {
  254. spin_unlock_irqrestore(&priv->lock, flags);
  255. return;
  256. }
  257. count = pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer,
  258. port->bulk_out_size);
  259. if (count == 0) {
  260. spin_unlock_irqrestore(&priv->lock, flags);
  261. return;
  262. }
  263. priv->write_urb_in_use = 1;
  264. spin_unlock_irqrestore(&priv->lock, flags);
  265. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, count,
  266. port->write_urb->transfer_buffer);
  267. port->write_urb->transfer_buffer_length = count;
  268. port->write_urb->dev = port->serial->dev;
  269. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  270. if (result) {
  271. dev_err(&port->dev, "%s - failed submitting write urb,"
  272. " error %d\n", __FUNCTION__, result);
  273. priv->write_urb_in_use = 0;
  274. // TODO: reschedule pl2303_send
  275. }
  276. usb_serial_port_softint(port);
  277. }
  278. static int pl2303_write_room(struct usb_serial_port *port)
  279. {
  280. struct pl2303_private *priv = usb_get_serial_port_data(port);
  281. int room = 0;
  282. unsigned long flags;
  283. dbg("%s - port %d", __FUNCTION__, port->number);
  284. spin_lock_irqsave(&priv->lock, flags);
  285. room = pl2303_buf_space_avail(priv->buf);
  286. spin_unlock_irqrestore(&priv->lock, flags);
  287. dbg("%s - returns %d", __FUNCTION__, room);
  288. return room;
  289. }
  290. static int pl2303_chars_in_buffer(struct usb_serial_port *port)
  291. {
  292. struct pl2303_private *priv = usb_get_serial_port_data(port);
  293. int chars = 0;
  294. unsigned long flags;
  295. dbg("%s - port %d", __FUNCTION__, port->number);
  296. spin_lock_irqsave(&priv->lock, flags);
  297. chars = pl2303_buf_data_avail(priv->buf);
  298. spin_unlock_irqrestore(&priv->lock, flags);
  299. dbg("%s - returns %d", __FUNCTION__, chars);
  300. return chars;
  301. }
  302. static void pl2303_set_termios(struct usb_serial_port *port,
  303. struct termios *old_termios)
  304. {
  305. struct usb_serial *serial = port->serial;
  306. struct pl2303_private *priv = usb_get_serial_port_data(port);
  307. unsigned long flags;
  308. unsigned int cflag;
  309. unsigned char *buf;
  310. int baud;
  311. int i;
  312. u8 control;
  313. dbg("%s - port %d", __FUNCTION__, port->number);
  314. if ((!port->tty) || (!port->tty->termios)) {
  315. dbg("%s - no tty structures", __FUNCTION__);
  316. return;
  317. }
  318. spin_lock_irqsave(&priv->lock, flags);
  319. if (!priv->termios_initialized) {
  320. *(port->tty->termios) = tty_std_termios;
  321. port->tty->termios->c_cflag = B9600 | CS8 | CREAD |
  322. HUPCL | CLOCAL;
  323. priv->termios_initialized = 1;
  324. }
  325. spin_unlock_irqrestore(&priv->lock, flags);
  326. cflag = port->tty->termios->c_cflag;
  327. /* check that they really want us to change something */
  328. if (old_termios) {
  329. if ((cflag == old_termios->c_cflag) &&
  330. (RELEVANT_IFLAG(port->tty->termios->c_iflag) ==
  331. RELEVANT_IFLAG(old_termios->c_iflag))) {
  332. dbg("%s - nothing to change...", __FUNCTION__);
  333. return;
  334. }
  335. }
  336. buf = kzalloc(7, GFP_KERNEL);
  337. if (!buf) {
  338. dev_err(&port->dev, "%s - out of memory.\n", __FUNCTION__);
  339. return;
  340. }
  341. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  342. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  343. 0, 0, buf, 7, 100);
  344. dbg("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
  345. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
  346. if (cflag & CSIZE) {
  347. switch (cflag & CSIZE) {
  348. case CS5: buf[6] = 5; break;
  349. case CS6: buf[6] = 6; break;
  350. case CS7: buf[6] = 7; break;
  351. default:
  352. case CS8: buf[6] = 8; break;
  353. }
  354. dbg("%s - data bits = %d", __FUNCTION__, buf[6]);
  355. }
  356. baud = 0;
  357. switch (cflag & CBAUD) {
  358. case B0: baud = 0; break;
  359. case B75: baud = 75; break;
  360. case B150: baud = 150; break;
  361. case B300: baud = 300; break;
  362. case B600: baud = 600; break;
  363. case B1200: baud = 1200; break;
  364. case B1800: baud = 1800; break;
  365. case B2400: baud = 2400; break;
  366. case B4800: baud = 4800; break;
  367. case B9600: baud = 9600; break;
  368. case B19200: baud = 19200; break;
  369. case B38400: baud = 38400; break;
  370. case B57600: baud = 57600; break;
  371. case B115200: baud = 115200; break;
  372. case B230400: baud = 230400; break;
  373. case B460800: baud = 460800; break;
  374. default:
  375. dev_err(&port->dev, "pl2303 driver does not support"
  376. " the baudrate requested (fix it)\n");
  377. break;
  378. }
  379. dbg("%s - baud = %d", __FUNCTION__, baud);
  380. if (baud) {
  381. buf[0] = baud & 0xff;
  382. buf[1] = (baud >> 8) & 0xff;
  383. buf[2] = (baud >> 16) & 0xff;
  384. buf[3] = (baud >> 24) & 0xff;
  385. }
  386. /* For reference buf[4]=0 is 1 stop bits */
  387. /* For reference buf[4]=1 is 1.5 stop bits */
  388. /* For reference buf[4]=2 is 2 stop bits */
  389. if (cflag & CSTOPB) {
  390. buf[4] = 2;
  391. dbg("%s - stop bits = 2", __FUNCTION__);
  392. } else {
  393. buf[4] = 0;
  394. dbg("%s - stop bits = 1", __FUNCTION__);
  395. }
  396. if (cflag & PARENB) {
  397. /* For reference buf[5]=0 is none parity */
  398. /* For reference buf[5]=1 is odd parity */
  399. /* For reference buf[5]=2 is even parity */
  400. /* For reference buf[5]=3 is mark parity */
  401. /* For reference buf[5]=4 is space parity */
  402. if (cflag & PARODD) {
  403. buf[5] = 1;
  404. dbg("%s - parity = odd", __FUNCTION__);
  405. } else {
  406. buf[5] = 2;
  407. dbg("%s - parity = even", __FUNCTION__);
  408. }
  409. } else {
  410. buf[5] = 0;
  411. dbg("%s - parity = none", __FUNCTION__);
  412. }
  413. i = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  414. SET_LINE_REQUEST, SET_LINE_REQUEST_TYPE,
  415. 0, 0, buf, 7, 100);
  416. dbg("0x21:0x20:0:0 %d", i);
  417. /* change control lines if we are switching to or from B0 */
  418. spin_lock_irqsave(&priv->lock, flags);
  419. control = priv->line_control;
  420. if ((cflag & CBAUD) == B0)
  421. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  422. else
  423. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  424. if (control != priv->line_control) {
  425. control = priv->line_control;
  426. spin_unlock_irqrestore(&priv->lock, flags);
  427. set_control_lines(serial->dev, control);
  428. } else {
  429. spin_unlock_irqrestore(&priv->lock, flags);
  430. }
  431. buf[0] = buf[1] = buf[2] = buf[3] = buf[4] = buf[5] = buf[6] = 0;
  432. i = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  433. GET_LINE_REQUEST, GET_LINE_REQUEST_TYPE,
  434. 0, 0, buf, 7, 100);
  435. dbg("0xa1:0x21:0:0 %d - %x %x %x %x %x %x %x", i,
  436. buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6]);
  437. if (cflag & CRTSCTS) {
  438. __u16 index;
  439. if (priv->type == HX)
  440. index = 0x61;
  441. else
  442. index = 0x41;
  443. i = usb_control_msg(serial->dev,
  444. usb_sndctrlpipe(serial->dev, 0),
  445. VENDOR_WRITE_REQUEST,
  446. VENDOR_WRITE_REQUEST_TYPE,
  447. 0x0, index, NULL, 0, 100);
  448. dbg("0x40:0x1:0x0:0x%x %d", index, i);
  449. }
  450. kfree(buf);
  451. }
  452. static int pl2303_open(struct usb_serial_port *port, struct file *filp)
  453. {
  454. struct termios tmp_termios;
  455. struct usb_serial *serial = port->serial;
  456. struct pl2303_private *priv = usb_get_serial_port_data(port);
  457. unsigned char *buf;
  458. int result;
  459. dbg("%s - port %d", __FUNCTION__, port->number);
  460. if (priv->type != HX) {
  461. usb_clear_halt(serial->dev, port->write_urb->pipe);
  462. usb_clear_halt(serial->dev, port->read_urb->pipe);
  463. }
  464. buf = kmalloc(10, GFP_KERNEL);
  465. if (buf==NULL)
  466. return -ENOMEM;
  467. #define FISH(a,b,c,d) \
  468. result=usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev,0), \
  469. b, a, c, d, buf, 1, 100); \
  470. dbg("0x%x:0x%x:0x%x:0x%x %d - %x",a,b,c,d,result,buf[0]);
  471. #define SOUP(a,b,c,d) \
  472. result=usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev,0), \
  473. b, a, c, d, NULL, 0, 100); \
  474. dbg("0x%x:0x%x:0x%x:0x%x %d",a,b,c,d,result);
  475. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  476. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 0);
  477. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  478. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
  479. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  480. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0x0404, 1);
  481. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8484, 0);
  482. FISH (VENDOR_READ_REQUEST_TYPE, VENDOR_READ_REQUEST, 0x8383, 0);
  483. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 0, 1);
  484. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 1, 0);
  485. if (priv->type == HX) {
  486. /* HX chip */
  487. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x44);
  488. /* reset upstream data pipes */
  489. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 8, 0);
  490. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 9, 0);
  491. } else {
  492. SOUP (VENDOR_WRITE_REQUEST_TYPE, VENDOR_WRITE_REQUEST, 2, 0x24);
  493. }
  494. kfree(buf);
  495. /* Setup termios */
  496. if (port->tty) {
  497. pl2303_set_termios(port, &tmp_termios);
  498. }
  499. //FIXME: need to assert RTS and DTR if CRTSCTS off
  500. dbg("%s - submitting read urb", __FUNCTION__);
  501. port->read_urb->dev = serial->dev;
  502. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  503. if (result) {
  504. dev_err(&port->dev, "%s - failed submitting read urb,"
  505. " error %d\n", __FUNCTION__, result);
  506. pl2303_close(port, NULL);
  507. return -EPROTO;
  508. }
  509. dbg("%s - submitting interrupt urb", __FUNCTION__);
  510. port->interrupt_in_urb->dev = serial->dev;
  511. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  512. if (result) {
  513. dev_err(&port->dev, "%s - failed submitting interrupt urb,"
  514. " error %d\n", __FUNCTION__, result);
  515. pl2303_close(port, NULL);
  516. return -EPROTO;
  517. }
  518. return 0;
  519. }
  520. static void pl2303_close(struct usb_serial_port *port, struct file *filp)
  521. {
  522. struct pl2303_private *priv = usb_get_serial_port_data(port);
  523. unsigned long flags;
  524. unsigned int c_cflag;
  525. int bps;
  526. long timeout;
  527. wait_queue_t wait;
  528. dbg("%s - port %d", __FUNCTION__, port->number);
  529. /* wait for data to drain from the buffer */
  530. spin_lock_irqsave(&priv->lock, flags);
  531. timeout = PL2303_CLOSING_WAIT;
  532. init_waitqueue_entry(&wait, current);
  533. add_wait_queue(&port->tty->write_wait, &wait);
  534. for (;;) {
  535. set_current_state(TASK_INTERRUPTIBLE);
  536. if (pl2303_buf_data_avail(priv->buf) == 0 ||
  537. timeout == 0 || signal_pending(current) ||
  538. !usb_get_intfdata(port->serial->interface)) /* disconnect */
  539. break;
  540. spin_unlock_irqrestore(&priv->lock, flags);
  541. timeout = schedule_timeout(timeout);
  542. spin_lock_irqsave(&priv->lock, flags);
  543. }
  544. set_current_state(TASK_RUNNING);
  545. remove_wait_queue(&port->tty->write_wait, &wait);
  546. /* clear out any remaining data in the buffer */
  547. pl2303_buf_clear(priv->buf);
  548. spin_unlock_irqrestore(&priv->lock, flags);
  549. /* wait for characters to drain from the device */
  550. /* (this is long enough for the entire 256 byte */
  551. /* pl2303 hardware buffer to drain with no flow */
  552. /* control for data rates of 1200 bps or more, */
  553. /* for lower rates we should really know how much */
  554. /* data is in the buffer to compute a delay */
  555. /* that is not unnecessarily long) */
  556. bps = tty_get_baud_rate(port->tty);
  557. if (bps > 1200)
  558. timeout = max((HZ*2560)/bps,HZ/10);
  559. else
  560. timeout = 2*HZ;
  561. schedule_timeout_interruptible(timeout);
  562. /* shutdown our urbs */
  563. dbg("%s - shutting down urbs", __FUNCTION__);
  564. usb_kill_urb(port->write_urb);
  565. usb_kill_urb(port->read_urb);
  566. usb_kill_urb(port->interrupt_in_urb);
  567. if (port->tty) {
  568. c_cflag = port->tty->termios->c_cflag;
  569. if (c_cflag & HUPCL) {
  570. /* drop DTR and RTS */
  571. spin_lock_irqsave(&priv->lock, flags);
  572. priv->line_control = 0;
  573. spin_unlock_irqrestore(&priv->lock, flags);
  574. set_control_lines(port->serial->dev, 0);
  575. }
  576. }
  577. }
  578. static int pl2303_tiocmset(struct usb_serial_port *port, struct file *file,
  579. unsigned int set, unsigned int clear)
  580. {
  581. struct pl2303_private *priv = usb_get_serial_port_data(port);
  582. unsigned long flags;
  583. u8 control;
  584. if (!usb_get_intfdata(port->serial->interface))
  585. return -ENODEV;
  586. spin_lock_irqsave(&priv->lock, flags);
  587. if (set & TIOCM_RTS)
  588. priv->line_control |= CONTROL_RTS;
  589. if (set & TIOCM_DTR)
  590. priv->line_control |= CONTROL_DTR;
  591. if (clear & TIOCM_RTS)
  592. priv->line_control &= ~CONTROL_RTS;
  593. if (clear & TIOCM_DTR)
  594. priv->line_control &= ~CONTROL_DTR;
  595. control = priv->line_control;
  596. spin_unlock_irqrestore(&priv->lock, flags);
  597. return set_control_lines(port->serial->dev, control);
  598. }
  599. static int pl2303_tiocmget(struct usb_serial_port *port, struct file *file)
  600. {
  601. struct pl2303_private *priv = usb_get_serial_port_data(port);
  602. unsigned long flags;
  603. unsigned int mcr;
  604. unsigned int status;
  605. unsigned int result;
  606. dbg("%s (%d)", __FUNCTION__, port->number);
  607. if (!usb_get_intfdata(port->serial->interface))
  608. return -ENODEV;
  609. spin_lock_irqsave(&priv->lock, flags);
  610. mcr = priv->line_control;
  611. status = priv->line_status;
  612. spin_unlock_irqrestore(&priv->lock, flags);
  613. result = ((mcr & CONTROL_DTR) ? TIOCM_DTR : 0)
  614. | ((mcr & CONTROL_RTS) ? TIOCM_RTS : 0)
  615. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  616. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  617. | ((status & UART_RING) ? TIOCM_RI : 0)
  618. | ((status & UART_DCD) ? TIOCM_CD : 0);
  619. dbg("%s - result = %x", __FUNCTION__, result);
  620. return result;
  621. }
  622. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  623. {
  624. struct pl2303_private *priv = usb_get_serial_port_data(port);
  625. unsigned long flags;
  626. unsigned int prevstatus;
  627. unsigned int status;
  628. unsigned int changed;
  629. spin_lock_irqsave(&priv->lock, flags);
  630. prevstatus = priv->line_status;
  631. spin_unlock_irqrestore(&priv->lock, flags);
  632. while (1) {
  633. interruptible_sleep_on(&priv->delta_msr_wait);
  634. /* see if a signal did it */
  635. if (signal_pending(current))
  636. return -ERESTARTSYS;
  637. spin_lock_irqsave(&priv->lock, flags);
  638. status = priv->line_status;
  639. spin_unlock_irqrestore(&priv->lock, flags);
  640. changed=prevstatus^status;
  641. if (((arg & TIOCM_RNG) && (changed & UART_RING)) ||
  642. ((arg & TIOCM_DSR) && (changed & UART_DSR)) ||
  643. ((arg & TIOCM_CD) && (changed & UART_DCD)) ||
  644. ((arg & TIOCM_CTS) && (changed & UART_CTS)) ) {
  645. return 0;
  646. }
  647. prevstatus = status;
  648. }
  649. /* NOTREACHED */
  650. return 0;
  651. }
  652. static int pl2303_ioctl(struct usb_serial_port *port, struct file *file,
  653. unsigned int cmd, unsigned long arg)
  654. {
  655. dbg("%s (%d) cmd = 0x%04x", __FUNCTION__, port->number, cmd);
  656. switch (cmd) {
  657. case TIOCMIWAIT:
  658. dbg("%s (%d) TIOCMIWAIT", __FUNCTION__, port->number);
  659. return wait_modem_info(port, arg);
  660. default:
  661. dbg("%s not supported = 0x%04x", __FUNCTION__, cmd);
  662. break;
  663. }
  664. return -ENOIOCTLCMD;
  665. }
  666. static void pl2303_break_ctl(struct usb_serial_port *port, int break_state)
  667. {
  668. struct usb_serial *serial = port->serial;
  669. u16 state;
  670. int result;
  671. dbg("%s - port %d", __FUNCTION__, port->number);
  672. if (break_state == 0)
  673. state = BREAK_OFF;
  674. else
  675. state = BREAK_ON;
  676. dbg("%s - turning break %s", __FUNCTION__, state==BREAK_OFF ? "off" : "on");
  677. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  678. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  679. 0, NULL, 0, 100);
  680. if (result)
  681. dbg("%s - error sending break = %d", __FUNCTION__, result);
  682. }
  683. static void pl2303_shutdown(struct usb_serial *serial)
  684. {
  685. int i;
  686. struct pl2303_private *priv;
  687. dbg("%s", __FUNCTION__);
  688. for (i = 0; i < serial->num_ports; ++i) {
  689. priv = usb_get_serial_port_data(serial->port[i]);
  690. if (priv) {
  691. pl2303_buf_free(priv->buf);
  692. kfree(priv);
  693. usb_set_serial_port_data(serial->port[i], NULL);
  694. }
  695. }
  696. }
  697. static void pl2303_update_line_status(struct usb_serial_port *port,
  698. unsigned char *data,
  699. unsigned int actual_length)
  700. {
  701. struct pl2303_private *priv = usb_get_serial_port_data(port);
  702. unsigned long flags;
  703. u8 status_idx = UART_STATE;
  704. u8 length = UART_STATE + 1;
  705. if ((le16_to_cpu(port->serial->dev->descriptor.idVendor) == SIEMENS_VENDOR_ID) &&
  706. (le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X65 ||
  707. le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_SX1 ||
  708. le16_to_cpu(port->serial->dev->descriptor.idProduct) == SIEMENS_PRODUCT_ID_X75)) {
  709. length = 1;
  710. status_idx = 0;
  711. }
  712. if (actual_length < length)
  713. return;
  714. /* Save off the uart status for others to look at */
  715. spin_lock_irqsave(&priv->lock, flags);
  716. priv->line_status = data[status_idx];
  717. spin_unlock_irqrestore(&priv->lock, flags);
  718. wake_up_interruptible(&priv->delta_msr_wait);
  719. }
  720. static void pl2303_read_int_callback(struct urb *urb, struct pt_regs *regs)
  721. {
  722. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  723. unsigned char *data = urb->transfer_buffer;
  724. unsigned int actual_length = urb->actual_length;
  725. int status;
  726. dbg("%s (%d)", __FUNCTION__, port->number);
  727. switch (urb->status) {
  728. case 0:
  729. /* success */
  730. break;
  731. case -ECONNRESET:
  732. case -ENOENT:
  733. case -ESHUTDOWN:
  734. /* this urb is terminated, clean up */
  735. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  736. urb->status);
  737. return;
  738. default:
  739. dbg("%s - nonzero urb status received: %d", __FUNCTION__,
  740. urb->status);
  741. goto exit;
  742. }
  743. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  744. urb->actual_length, urb->transfer_buffer);
  745. pl2303_update_line_status(port, data, actual_length);
  746. exit:
  747. status = usb_submit_urb(urb, GFP_ATOMIC);
  748. if (status)
  749. dev_err(&urb->dev->dev,
  750. "%s - usb_submit_urb failed with result %d\n",
  751. __FUNCTION__, status);
  752. }
  753. static void pl2303_read_bulk_callback(struct urb *urb, struct pt_regs *regs)
  754. {
  755. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  756. struct pl2303_private *priv = usb_get_serial_port_data(port);
  757. struct tty_struct *tty;
  758. unsigned char *data = urb->transfer_buffer;
  759. unsigned long flags;
  760. int i;
  761. int result;
  762. u8 status;
  763. char tty_flag;
  764. dbg("%s - port %d", __FUNCTION__, port->number);
  765. if (urb->status) {
  766. dbg("%s - urb->status = %d", __FUNCTION__, urb->status);
  767. if (!port->open_count) {
  768. dbg("%s - port is closed, exiting.", __FUNCTION__);
  769. return;
  770. }
  771. if (urb->status == -EPROTO) {
  772. /* PL2303 mysteriously fails with -EPROTO reschedule
  773. * the read */
  774. dbg("%s - caught -EPROTO, resubmitting the urb",
  775. __FUNCTION__);
  776. urb->status = 0;
  777. urb->dev = port->serial->dev;
  778. result = usb_submit_urb(urb, GFP_ATOMIC);
  779. if (result)
  780. dev_err(&urb->dev->dev, "%s - failed"
  781. " resubmitting read urb, error %d\n",
  782. __FUNCTION__, result);
  783. return;
  784. }
  785. dbg("%s - unable to handle the error, exiting.", __FUNCTION__);
  786. return;
  787. }
  788. usb_serial_debug_data(debug, &port->dev, __FUNCTION__,
  789. urb->actual_length, data);
  790. /* get tty_flag from status */
  791. tty_flag = TTY_NORMAL;
  792. spin_lock_irqsave(&priv->lock, flags);
  793. status = priv->line_status;
  794. priv->line_status &= ~UART_STATE_TRANSIENT_MASK;
  795. spin_unlock_irqrestore(&priv->lock, flags);
  796. wake_up_interruptible(&priv->delta_msr_wait);
  797. /* break takes precedence over parity, */
  798. /* which takes precedence over framing errors */
  799. if (status & UART_BREAK_ERROR )
  800. tty_flag = TTY_BREAK;
  801. else if (status & UART_PARITY_ERROR)
  802. tty_flag = TTY_PARITY;
  803. else if (status & UART_FRAME_ERROR)
  804. tty_flag = TTY_FRAME;
  805. dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
  806. tty = port->tty;
  807. if (tty && urb->actual_length) {
  808. tty_buffer_request_room(tty, urb->actual_length + 1);
  809. /* overrun is special, not associated with a char */
  810. if (status & UART_OVERRUN_ERROR)
  811. tty_insert_flip_char(tty, 0, TTY_OVERRUN);
  812. for (i = 0; i < urb->actual_length; ++i)
  813. tty_insert_flip_char(tty, data[i], tty_flag);
  814. tty_flip_buffer_push(tty);
  815. }
  816. /* Schedule the next read _if_ we are still open */
  817. if (port->open_count) {
  818. urb->dev = port->serial->dev;
  819. result = usb_submit_urb(urb, GFP_ATOMIC);
  820. if (result)
  821. dev_err(&urb->dev->dev, "%s - failed resubmitting"
  822. " read urb, error %d\n", __FUNCTION__, result);
  823. }
  824. return;
  825. }
  826. static void pl2303_write_bulk_callback(struct urb *urb, struct pt_regs *regs)
  827. {
  828. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  829. struct pl2303_private *priv = usb_get_serial_port_data(port);
  830. int result;
  831. dbg("%s - port %d", __FUNCTION__, port->number);
  832. switch (urb->status) {
  833. case 0:
  834. /* success */
  835. break;
  836. case -ECONNRESET:
  837. case -ENOENT:
  838. case -ESHUTDOWN:
  839. /* this urb is terminated, clean up */
  840. dbg("%s - urb shutting down with status: %d", __FUNCTION__,
  841. urb->status);
  842. priv->write_urb_in_use = 0;
  843. return;
  844. default:
  845. /* error in the urb, so we have to resubmit it */
  846. dbg("%s - Overflow in write", __FUNCTION__);
  847. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__,
  848. urb->status);
  849. port->write_urb->transfer_buffer_length = 1;
  850. port->write_urb->dev = port->serial->dev;
  851. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  852. if (result)
  853. dev_err(&urb->dev->dev, "%s - failed resubmitting write"
  854. " urb, error %d\n", __FUNCTION__, result);
  855. else
  856. return;
  857. }
  858. priv->write_urb_in_use = 0;
  859. /* send any buffered data */
  860. pl2303_send(port);
  861. }
  862. /*
  863. * pl2303_buf_alloc
  864. *
  865. * Allocate a circular buffer and all associated memory.
  866. */
  867. static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
  868. {
  869. struct pl2303_buf *pb;
  870. if (size == 0)
  871. return NULL;
  872. pb = (struct pl2303_buf *)kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
  873. if (pb == NULL)
  874. return NULL;
  875. pb->buf_buf = kmalloc(size, GFP_KERNEL);
  876. if (pb->buf_buf == NULL) {
  877. kfree(pb);
  878. return NULL;
  879. }
  880. pb->buf_size = size;
  881. pb->buf_get = pb->buf_put = pb->buf_buf;
  882. return pb;
  883. }
  884. /*
  885. * pl2303_buf_free
  886. *
  887. * Free the buffer and all associated memory.
  888. */
  889. static void pl2303_buf_free(struct pl2303_buf *pb)
  890. {
  891. if (pb) {
  892. kfree(pb->buf_buf);
  893. kfree(pb);
  894. }
  895. }
  896. /*
  897. * pl2303_buf_clear
  898. *
  899. * Clear out all data in the circular buffer.
  900. */
  901. static void pl2303_buf_clear(struct pl2303_buf *pb)
  902. {
  903. if (pb != NULL)
  904. pb->buf_get = pb->buf_put;
  905. /* equivalent to a get of all data available */
  906. }
  907. /*
  908. * pl2303_buf_data_avail
  909. *
  910. * Return the number of bytes of data available in the circular
  911. * buffer.
  912. */
  913. static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
  914. {
  915. if (pb != NULL)
  916. return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
  917. else
  918. return 0;
  919. }
  920. /*
  921. * pl2303_buf_space_avail
  922. *
  923. * Return the number of bytes of space available in the circular
  924. * buffer.
  925. */
  926. static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
  927. {
  928. if (pb != NULL)
  929. return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
  930. else
  931. return 0;
  932. }
  933. /*
  934. * pl2303_buf_put
  935. *
  936. * Copy data data from a user buffer and put it into the circular buffer.
  937. * Restrict to the amount of space available.
  938. *
  939. * Return the number of bytes copied.
  940. */
  941. static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
  942. unsigned int count)
  943. {
  944. unsigned int len;
  945. if (pb == NULL)
  946. return 0;
  947. len = pl2303_buf_space_avail(pb);
  948. if (count > len)
  949. count = len;
  950. if (count == 0)
  951. return 0;
  952. len = pb->buf_buf + pb->buf_size - pb->buf_put;
  953. if (count > len) {
  954. memcpy(pb->buf_put, buf, len);
  955. memcpy(pb->buf_buf, buf+len, count - len);
  956. pb->buf_put = pb->buf_buf + count - len;
  957. } else {
  958. memcpy(pb->buf_put, buf, count);
  959. if (count < len)
  960. pb->buf_put += count;
  961. else /* count == len */
  962. pb->buf_put = pb->buf_buf;
  963. }
  964. return count;
  965. }
  966. /*
  967. * pl2303_buf_get
  968. *
  969. * Get data from the circular buffer and copy to the given buffer.
  970. * Restrict to the amount of data available.
  971. *
  972. * Return the number of bytes copied.
  973. */
  974. static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
  975. unsigned int count)
  976. {
  977. unsigned int len;
  978. if (pb == NULL)
  979. return 0;
  980. len = pl2303_buf_data_avail(pb);
  981. if (count > len)
  982. count = len;
  983. if (count == 0)
  984. return 0;
  985. len = pb->buf_buf + pb->buf_size - pb->buf_get;
  986. if (count > len) {
  987. memcpy(buf, pb->buf_get, len);
  988. memcpy(buf+len, pb->buf_buf, count - len);
  989. pb->buf_get = pb->buf_buf + count - len;
  990. } else {
  991. memcpy(buf, pb->buf_get, count);
  992. if (count < len)
  993. pb->buf_get += count;
  994. else /* count == len */
  995. pb->buf_get = pb->buf_buf;
  996. }
  997. return count;
  998. }
  999. static int __init pl2303_init(void)
  1000. {
  1001. int retval;
  1002. retval = usb_serial_register(&pl2303_device);
  1003. if (retval)
  1004. goto failed_usb_serial_register;
  1005. retval = usb_register(&pl2303_driver);
  1006. if (retval)
  1007. goto failed_usb_register;
  1008. info(DRIVER_DESC);
  1009. return 0;
  1010. failed_usb_register:
  1011. usb_serial_deregister(&pl2303_device);
  1012. failed_usb_serial_register:
  1013. return retval;
  1014. }
  1015. static void __exit pl2303_exit(void)
  1016. {
  1017. usb_deregister(&pl2303_driver);
  1018. usb_serial_deregister(&pl2303_device);
  1019. }
  1020. module_init(pl2303_init);
  1021. module_exit(pl2303_exit);
  1022. MODULE_DESCRIPTION(DRIVER_DESC);
  1023. MODULE_LICENSE("GPL");
  1024. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1025. MODULE_PARM_DESC(debug, "Debug enabled or not");