pl2303.c 32 KB

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