pl2303.c 32 KB

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