oti6858.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  1. /*
  2. * Ours Technology Inc. OTi-6858 USB to serial adapter driver.
  3. *
  4. * Copyleft (C) 2007 Kees Lemmens (adapted for kernel 2.6.20)
  5. * Copyright (C) 2006 Tomasz Michal Lukaszewski (FIXME: add e-mail)
  6. * Copyright (C) 2001-2004 Greg Kroah-Hartman (greg@kroah.com)
  7. * Copyright (C) 2003 IBM Corp.
  8. *
  9. * Many thanks to the authors of pl2303 driver: all functions in this file
  10. * are heavily based on pl2303 code, buffering code is a 1-to-1 copy.
  11. *
  12. * Warning! You use this driver on your own risk! The only official
  13. * description of this device I have is datasheet from manufacturer,
  14. * and it doesn't contain almost any information needed to write a driver.
  15. * Almost all knowlegde used while writing this driver was gathered by:
  16. * - analyzing traffic between device and the M$ Windows 2000 driver,
  17. * - trying different bit combinations and checking pin states
  18. * with a voltmeter,
  19. * - receiving malformed frames and producing buffer overflows
  20. * to learn how errors are reported,
  21. * So, THIS CODE CAN DESTROY OTi-6858 AND ANY OTHER DEVICES, THAT ARE
  22. * CONNECTED TO IT!
  23. *
  24. * This program is free software; you can redistribute it and/or modify
  25. * it under the terms of the GNU General Public License as published by
  26. * the Free Software Foundation; either version 2 of the License.
  27. *
  28. * See Documentation/usb/usb-serial.txt for more information on using this driver
  29. *
  30. * TODO:
  31. * - implement correct flushing for ioctls and oti6858_close()
  32. * - check how errors (rx overflow, parity error, framing error) are reported
  33. * - implement oti6858_break_ctl()
  34. * - implement more ioctls
  35. * - test/implement flow control
  36. * - allow setting custom baud rates
  37. */
  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 <linux/usb.h>
  50. #include <linux/usb/serial.h>
  51. #include <asm/uaccess.h>
  52. #include "oti6858.h"
  53. #define OTI6858_DESCRIPTION \
  54. "Ours Technology Inc. OTi-6858 USB to serial adapter driver"
  55. #define OTI6858_AUTHOR "Tomasz Michal Lukaszewski <FIXME@FIXME>"
  56. #define OTI6858_VERSION "0.1"
  57. static struct usb_device_id id_table [] = {
  58. { USB_DEVICE(OTI6858_VENDOR_ID, OTI6858_PRODUCT_ID) },
  59. { }
  60. };
  61. MODULE_DEVICE_TABLE(usb, id_table);
  62. static struct usb_driver oti6858_driver = {
  63. .name = "oti6858",
  64. .probe = usb_serial_probe,
  65. .disconnect = usb_serial_disconnect,
  66. .id_table = id_table,
  67. .no_dynamic_id = 1,
  68. };
  69. static int debug;
  70. /* buffering code, copied from pl2303 driver */
  71. #define PL2303_BUF_SIZE 1024
  72. #define PL2303_TMP_BUF_SIZE 1024
  73. struct pl2303_buf {
  74. unsigned int buf_size;
  75. char *buf_buf;
  76. char *buf_get;
  77. char *buf_put;
  78. };
  79. /* requests */
  80. #define OTI6858_REQ_GET_STATUS (USB_DIR_IN | USB_TYPE_VENDOR | 0x00)
  81. #define OTI6858_REQ_T_GET_STATUS 0x01
  82. #define OTI6858_REQ_SET_LINE (USB_DIR_OUT | USB_TYPE_VENDOR | 0x00)
  83. #define OTI6858_REQ_T_SET_LINE 0x00
  84. #define OTI6858_REQ_CHECK_TXBUFF (USB_DIR_IN | USB_TYPE_VENDOR | 0x01)
  85. #define OTI6858_REQ_T_CHECK_TXBUFF 0x00
  86. /* format of the control packet */
  87. struct oti6858_control_pkt {
  88. u16 divisor; /* baud rate = 96000000 / (16 * divisor), LE */
  89. #define OTI6858_MAX_BAUD_RATE 3000000
  90. u8 frame_fmt;
  91. #define FMT_STOP_BITS_MASK 0xc0
  92. #define FMT_STOP_BITS_1 0x00
  93. #define FMT_STOP_BITS_2 0x40 /* 1.5 stop bits if FMT_DATA_BITS_5 */
  94. #define FMT_PARITY_MASK 0x38
  95. #define FMT_PARITY_NONE 0x00
  96. #define FMT_PARITY_ODD 0x08
  97. #define FMT_PARITY_EVEN 0x18
  98. #define FMT_PARITY_MARK 0x28
  99. #define FMT_PARITY_SPACE 0x38
  100. #define FMT_DATA_BITS_MASK 0x03
  101. #define FMT_DATA_BITS_5 0x00
  102. #define FMT_DATA_BITS_6 0x01
  103. #define FMT_DATA_BITS_7 0x02
  104. #define FMT_DATA_BITS_8 0x03
  105. u8 something; /* always equals 0x43 */
  106. u8 control; /* settings of flow control lines */
  107. #define CONTROL_MASK 0x0c
  108. #define CONTROL_DTR_HIGH 0x08
  109. #define CONTROL_RTS_HIGH 0x04
  110. u8 tx_status;
  111. #define TX_BUFFER_EMPTIED 0x09
  112. u8 pin_state;
  113. #define PIN_MASK 0x3f
  114. #define PIN_RTS 0x20 /* output pin */
  115. #define PIN_CTS 0x10 /* input pin, active low */
  116. #define PIN_DSR 0x08 /* input pin, active low */
  117. #define PIN_DTR 0x04 /* output pin */
  118. #define PIN_RI 0x02 /* input pin, active low */
  119. #define PIN_DCD 0x01 /* input pin, active low */
  120. u8 rx_bytes_avail; /* number of bytes in rx buffer */;
  121. };
  122. #define OTI6858_CTRL_PKT_SIZE sizeof(struct oti6858_control_pkt)
  123. #define OTI6858_CTRL_EQUALS_PENDING(a, priv) \
  124. ( ((a)->divisor == (priv)->pending_setup.divisor) \
  125. && ((a)->control == (priv)->pending_setup.control) \
  126. && ((a)->frame_fmt == (priv)->pending_setup.frame_fmt) )
  127. /* function prototypes */
  128. static int oti6858_open(struct usb_serial_port *port, struct file *filp);
  129. static void oti6858_close(struct usb_serial_port *port, struct file *filp);
  130. static void oti6858_set_termios(struct usb_serial_port *port,
  131. struct ktermios *old);
  132. static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
  133. unsigned int cmd, unsigned long arg);
  134. static void oti6858_read_int_callback(struct urb *urb);
  135. static void oti6858_read_bulk_callback(struct urb *urb);
  136. static void oti6858_write_bulk_callback(struct urb *urb);
  137. static int oti6858_write(struct usb_serial_port *port,
  138. const unsigned char *buf, int count);
  139. static int oti6858_write_room(struct usb_serial_port *port);
  140. static void oti6858_break_ctl(struct usb_serial_port *port, int break_state);
  141. static int oti6858_chars_in_buffer(struct usb_serial_port *port);
  142. static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file);
  143. static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
  144. unsigned int set, unsigned int clear);
  145. static int oti6858_startup(struct usb_serial *serial);
  146. static void oti6858_shutdown(struct usb_serial *serial);
  147. /* functions operating on buffers */
  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. /* device info */
  158. static struct usb_serial_driver oti6858_device = {
  159. .driver = {
  160. .owner = THIS_MODULE,
  161. .name = "oti6858",
  162. },
  163. .id_table = id_table,
  164. .num_interrupt_in = 1,
  165. .num_bulk_in = 1,
  166. .num_bulk_out = 1,
  167. .num_ports = 1,
  168. .open = oti6858_open,
  169. .close = oti6858_close,
  170. .write = oti6858_write,
  171. .ioctl = oti6858_ioctl,
  172. .break_ctl = oti6858_break_ctl,
  173. .set_termios = oti6858_set_termios,
  174. .tiocmget = oti6858_tiocmget,
  175. .tiocmset = oti6858_tiocmset,
  176. .read_bulk_callback = oti6858_read_bulk_callback,
  177. .read_int_callback = oti6858_read_int_callback,
  178. .write_bulk_callback = oti6858_write_bulk_callback,
  179. .write_room = oti6858_write_room,
  180. .chars_in_buffer = oti6858_chars_in_buffer,
  181. .attach = oti6858_startup,
  182. .shutdown = oti6858_shutdown,
  183. };
  184. struct oti6858_private {
  185. spinlock_t lock;
  186. struct pl2303_buf *buf;
  187. struct oti6858_control_pkt status;
  188. struct {
  189. u8 read_urb_in_use;
  190. u8 write_urb_in_use;
  191. u8 termios_initialized;
  192. } flags;
  193. struct delayed_work delayed_write_work;
  194. struct {
  195. u16 divisor;
  196. u8 frame_fmt;
  197. u8 control;
  198. } pending_setup;
  199. u8 transient;
  200. u8 setup_done;
  201. struct delayed_work delayed_setup_work;
  202. wait_queue_head_t intr_wait;
  203. struct usb_serial_port *port; /* USB port with which associated */
  204. };
  205. #undef dbg
  206. /* #define dbg(format, arg...) printk(KERN_INFO "%s: " format "\n", __FILE__, ## arg) */
  207. #define dbg(format, arg...) printk(KERN_INFO "" format "\n", ## arg)
  208. static void setup_line(struct work_struct *work)
  209. {
  210. struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_setup_work.work);
  211. struct usb_serial_port *port = priv->port;
  212. struct oti6858_control_pkt *new_setup;
  213. unsigned long flags;
  214. int result;
  215. dbg("%s(port = %d)", __FUNCTION__, port->number);
  216. if ((new_setup = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
  217. dev_err(&port->dev, "%s(): out of memory!\n", __FUNCTION__);
  218. /* we will try again */
  219. schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
  220. return;
  221. }
  222. result = usb_control_msg(port->serial->dev,
  223. usb_rcvctrlpipe(port->serial->dev, 0),
  224. OTI6858_REQ_T_GET_STATUS,
  225. OTI6858_REQ_GET_STATUS,
  226. 0, 0,
  227. new_setup, OTI6858_CTRL_PKT_SIZE,
  228. 100);
  229. if (result != OTI6858_CTRL_PKT_SIZE) {
  230. dev_err(&port->dev, "%s(): error reading status\n", __FUNCTION__);
  231. kfree(new_setup);
  232. /* we will try again */
  233. schedule_delayed_work(&priv->delayed_setup_work, msecs_to_jiffies(2));
  234. return;
  235. }
  236. spin_lock_irqsave(&priv->lock, flags);
  237. if (!OTI6858_CTRL_EQUALS_PENDING(new_setup, priv)) {
  238. new_setup->divisor = priv->pending_setup.divisor;
  239. new_setup->control = priv->pending_setup.control;
  240. new_setup->frame_fmt = priv->pending_setup.frame_fmt;
  241. spin_unlock_irqrestore(&priv->lock, flags);
  242. result = usb_control_msg(port->serial->dev,
  243. usb_sndctrlpipe(port->serial->dev, 0),
  244. OTI6858_REQ_T_SET_LINE,
  245. OTI6858_REQ_SET_LINE,
  246. 0, 0,
  247. new_setup, OTI6858_CTRL_PKT_SIZE,
  248. 100);
  249. } else {
  250. spin_unlock_irqrestore(&priv->lock, flags);
  251. result = 0;
  252. }
  253. kfree(new_setup);
  254. spin_lock_irqsave(&priv->lock, flags);
  255. if (result != OTI6858_CTRL_PKT_SIZE)
  256. priv->transient = 0;
  257. priv->setup_done = 1;
  258. spin_unlock_irqrestore(&priv->lock, flags);
  259. dbg("%s(): submitting interrupt urb", __FUNCTION__);
  260. port->interrupt_in_urb->dev = port->serial->dev;
  261. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  262. if (result != 0) {
  263. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  264. " with error %d\n", __FUNCTION__, result);
  265. }
  266. }
  267. void send_data(struct work_struct *work)
  268. {
  269. struct oti6858_private *priv = container_of(work, struct oti6858_private, delayed_write_work.work);
  270. struct usb_serial_port *port = priv->port;
  271. int count = 0, result;
  272. unsigned long flags;
  273. unsigned char allow;
  274. dbg("%s(port = %d)", __FUNCTION__, port->number);
  275. spin_lock_irqsave(&priv->lock, flags);
  276. if (priv->flags.write_urb_in_use) {
  277. spin_unlock_irqrestore(&priv->lock, flags);
  278. schedule_delayed_work(&priv->delayed_write_work, msecs_to_jiffies(2));
  279. return;
  280. }
  281. priv->flags.write_urb_in_use = 1;
  282. count = pl2303_buf_data_avail(priv->buf);
  283. spin_unlock_irqrestore(&priv->lock, flags);
  284. if (count > port->bulk_out_size)
  285. count = port->bulk_out_size;
  286. if (count != 0) {
  287. result = usb_control_msg(port->serial->dev,
  288. usb_rcvctrlpipe(port->serial->dev, 0),
  289. OTI6858_REQ_T_CHECK_TXBUFF,
  290. OTI6858_REQ_CHECK_TXBUFF,
  291. count, 0, &allow, 1, 100);
  292. if (result != 1 || allow != 0)
  293. count = 0;
  294. }
  295. if (count == 0) {
  296. priv->flags.write_urb_in_use = 0;
  297. dbg("%s(): submitting interrupt urb", __FUNCTION__);
  298. port->interrupt_in_urb->dev = port->serial->dev;
  299. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  300. if (result != 0) {
  301. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  302. " with error %d\n", __FUNCTION__, result);
  303. }
  304. return;
  305. }
  306. spin_lock_irqsave(&priv->lock, flags);
  307. pl2303_buf_get(priv->buf, port->write_urb->transfer_buffer, count);
  308. spin_unlock_irqrestore(&priv->lock, flags);
  309. port->write_urb->transfer_buffer_length = count;
  310. port->write_urb->dev = port->serial->dev;
  311. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  312. if (result != 0) {
  313. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  314. " with error %d\n", __FUNCTION__, result);
  315. priv->flags.write_urb_in_use = 0;
  316. }
  317. usb_serial_port_softint(port);
  318. }
  319. static int oti6858_startup(struct usb_serial *serial)
  320. {
  321. struct usb_serial_port *port = serial->port[0];
  322. struct oti6858_private *priv;
  323. int i;
  324. for (i = 0; i < serial->num_ports; ++i) {
  325. priv = kzalloc(sizeof(struct oti6858_private), GFP_KERNEL);
  326. if (!priv)
  327. break;
  328. priv->buf = pl2303_buf_alloc(PL2303_BUF_SIZE);
  329. if (priv->buf == NULL) {
  330. kfree(priv);
  331. break;
  332. }
  333. spin_lock_init(&priv->lock);
  334. init_waitqueue_head(&priv->intr_wait);
  335. // INIT_WORK(&priv->setup_work, setup_line, serial->port[i]);
  336. // INIT_WORK(&priv->write_work, send_data, serial->port[i]);
  337. priv->port = port;
  338. INIT_DELAYED_WORK(&priv->delayed_setup_work, setup_line);
  339. INIT_DELAYED_WORK(&priv->delayed_write_work, send_data);
  340. usb_set_serial_port_data(serial->port[i], priv);
  341. }
  342. if (i == serial->num_ports)
  343. return 0;
  344. for (--i; i >= 0; --i) {
  345. priv = usb_get_serial_port_data(serial->port[i]);
  346. pl2303_buf_free(priv->buf);
  347. kfree(priv);
  348. usb_set_serial_port_data(serial->port[i], NULL);
  349. }
  350. return -ENOMEM;
  351. }
  352. static int oti6858_write(struct usb_serial_port *port,
  353. const unsigned char *buf, int count)
  354. {
  355. struct oti6858_private *priv = usb_get_serial_port_data(port);
  356. unsigned long flags;
  357. dbg("%s(port = %d, count = %d)", __FUNCTION__, port->number, count);
  358. if (!count)
  359. return count;
  360. spin_lock_irqsave(&priv->lock, flags);
  361. count = pl2303_buf_put(priv->buf, buf, count);
  362. spin_unlock_irqrestore(&priv->lock, flags);
  363. return count;
  364. }
  365. static int oti6858_write_room(struct usb_serial_port *port)
  366. {
  367. struct oti6858_private *priv = usb_get_serial_port_data(port);
  368. int room = 0;
  369. unsigned long flags;
  370. dbg("%s(port = %d)", __FUNCTION__, port->number);
  371. spin_lock_irqsave(&priv->lock, flags);
  372. room = pl2303_buf_space_avail(priv->buf);
  373. spin_unlock_irqrestore(&priv->lock, flags);
  374. return room;
  375. }
  376. static int oti6858_chars_in_buffer(struct usb_serial_port *port)
  377. {
  378. struct oti6858_private *priv = usb_get_serial_port_data(port);
  379. int chars = 0;
  380. unsigned long flags;
  381. dbg("%s(port = %d)", __FUNCTION__, port->number);
  382. spin_lock_irqsave(&priv->lock, flags);
  383. chars = pl2303_buf_data_avail(priv->buf);
  384. spin_unlock_irqrestore(&priv->lock, flags);
  385. return chars;
  386. }
  387. static void oti6858_set_termios(struct usb_serial_port *port,
  388. struct ktermios *old_termios)
  389. {
  390. struct oti6858_private *priv = usb_get_serial_port_data(port);
  391. unsigned long flags;
  392. unsigned int cflag;
  393. u8 frame_fmt, control;
  394. u16 divisor;
  395. int br;
  396. dbg("%s(port = %d)", __FUNCTION__, port->number);
  397. if ((!port->tty) || (!port->tty->termios)) {
  398. dbg("%s(): no tty structures", __FUNCTION__);
  399. return;
  400. }
  401. spin_lock_irqsave(&priv->lock, flags);
  402. if (!priv->flags.termios_initialized) {
  403. *(port->tty->termios) = tty_std_termios;
  404. port->tty->termios->c_cflag = B38400 | CS8 | CREAD | HUPCL | CLOCAL;
  405. priv->flags.termios_initialized = 1;
  406. }
  407. spin_unlock_irqrestore(&priv->lock, flags);
  408. cflag = port->tty->termios->c_cflag;
  409. spin_lock_irqsave(&priv->lock, flags);
  410. divisor = priv->pending_setup.divisor;
  411. frame_fmt = priv->pending_setup.frame_fmt;
  412. control = priv->pending_setup.control;
  413. spin_unlock_irqrestore(&priv->lock, flags);
  414. frame_fmt &= ~FMT_DATA_BITS_MASK;
  415. switch (cflag & CSIZE) {
  416. case CS5:
  417. frame_fmt |= FMT_DATA_BITS_5;
  418. break;
  419. case CS6:
  420. frame_fmt |= FMT_DATA_BITS_6;
  421. break;
  422. case CS7:
  423. frame_fmt |= FMT_DATA_BITS_7;
  424. break;
  425. default:
  426. case CS8:
  427. frame_fmt |= FMT_DATA_BITS_8;
  428. break;
  429. }
  430. /* manufacturer claims that this device can work with baud rates
  431. * up to 3 Mbps; I've tested it only on 115200 bps, so I can't
  432. * guarantee that any other baud rate will work (especially
  433. * the higher ones)
  434. */
  435. br = tty_get_baud_rate(port->tty);
  436. if (br == 0) {
  437. divisor = 0;
  438. } else if (br <= OTI6858_MAX_BAUD_RATE) {
  439. int real_br;
  440. divisor = (96000000 + 8 * br) / (16 * br);
  441. real_br = 96000000 / (16 * divisor);
  442. if ((((real_br - br) * 100 + br - 1) / br) > 2) {
  443. dbg("%s(): baud rate %d is invalid", __FUNCTION__, br);
  444. return;
  445. }
  446. divisor = cpu_to_le16(divisor);
  447. } else {
  448. dbg("%s(): baud rate %d is too high", __FUNCTION__, br);
  449. return;
  450. }
  451. frame_fmt &= ~FMT_STOP_BITS_MASK;
  452. if ((cflag & CSTOPB) != 0) {
  453. frame_fmt |= FMT_STOP_BITS_2;
  454. } else {
  455. frame_fmt |= FMT_STOP_BITS_1;
  456. }
  457. frame_fmt &= ~FMT_PARITY_MASK;
  458. if ((cflag & PARENB) != 0) {
  459. if ((cflag & PARODD) != 0) {
  460. frame_fmt |= FMT_PARITY_ODD;
  461. } else {
  462. frame_fmt |= FMT_PARITY_EVEN;
  463. }
  464. } else {
  465. frame_fmt |= FMT_PARITY_NONE;
  466. }
  467. control &= ~CONTROL_MASK;
  468. if ((cflag & CRTSCTS) != 0)
  469. control |= (CONTROL_DTR_HIGH | CONTROL_RTS_HIGH);
  470. /* change control lines if we are switching to or from B0 */
  471. /* FIXME:
  472. spin_lock_irqsave(&priv->lock, flags);
  473. control = priv->line_control;
  474. if ((cflag & CBAUD) == B0)
  475. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  476. else
  477. priv->line_control |= (CONTROL_DTR | CONTROL_RTS);
  478. if (control != priv->line_control) {
  479. control = priv->line_control;
  480. spin_unlock_irqrestore(&priv->lock, flags);
  481. set_control_lines(serial->dev, control);
  482. } else {
  483. spin_unlock_irqrestore(&priv->lock, flags);
  484. }
  485. */
  486. spin_lock_irqsave(&priv->lock, flags);
  487. if (divisor != priv->pending_setup.divisor
  488. || control != priv->pending_setup.control
  489. || frame_fmt != priv->pending_setup.frame_fmt) {
  490. priv->pending_setup.divisor = divisor;
  491. priv->pending_setup.control = control;
  492. priv->pending_setup.frame_fmt = frame_fmt;
  493. }
  494. spin_unlock_irqrestore(&priv->lock, flags);
  495. }
  496. static int oti6858_open(struct usb_serial_port *port, struct file *filp)
  497. {
  498. struct oti6858_private *priv = usb_get_serial_port_data(port);
  499. struct ktermios tmp_termios;
  500. struct usb_serial *serial = port->serial;
  501. struct oti6858_control_pkt *buf;
  502. unsigned long flags;
  503. int result;
  504. dbg("%s(port = %d)", __FUNCTION__, port->number);
  505. usb_clear_halt(serial->dev, port->write_urb->pipe);
  506. usb_clear_halt(serial->dev, port->read_urb->pipe);
  507. if (port->open_count != 1)
  508. return 0;
  509. if ((buf = kmalloc(OTI6858_CTRL_PKT_SIZE, GFP_KERNEL)) == NULL) {
  510. dev_err(&port->dev, "%s(): out of memory!\n", __FUNCTION__);
  511. return -ENOMEM;
  512. }
  513. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  514. OTI6858_REQ_T_GET_STATUS,
  515. OTI6858_REQ_GET_STATUS,
  516. 0, 0,
  517. buf, OTI6858_CTRL_PKT_SIZE,
  518. 100);
  519. if (result != OTI6858_CTRL_PKT_SIZE) {
  520. /* assume default (after power-on reset) values */
  521. buf->divisor = cpu_to_le16(0x009c); /* 38400 bps */
  522. buf->frame_fmt = 0x03; /* 8N1 */
  523. buf->something = 0x43;
  524. buf->control = 0x4c; /* DTR, RTS */
  525. buf->tx_status = 0x00;
  526. buf->pin_state = 0x5b; /* RTS, CTS, DSR, DTR, RI, DCD */
  527. buf->rx_bytes_avail = 0x00;
  528. }
  529. spin_lock_irqsave(&priv->lock, flags);
  530. memcpy(&priv->status, buf, OTI6858_CTRL_PKT_SIZE);
  531. priv->pending_setup.divisor = buf->divisor;
  532. priv->pending_setup.frame_fmt = buf->frame_fmt;
  533. priv->pending_setup.control = buf->control;
  534. spin_unlock_irqrestore(&priv->lock, flags);
  535. kfree(buf);
  536. dbg("%s(): submitting interrupt urb", __FUNCTION__);
  537. port->interrupt_in_urb->dev = serial->dev;
  538. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  539. if (result != 0) {
  540. dev_err(&port->dev, "%s(): usb_submit_urb() failed"
  541. " with error %d\n", __FUNCTION__, result);
  542. oti6858_close(port, NULL);
  543. return -EPROTO;
  544. }
  545. /* setup termios */
  546. if (port->tty)
  547. oti6858_set_termios(port, &tmp_termios);
  548. return 0;
  549. }
  550. static void oti6858_close(struct usb_serial_port *port, struct file *filp)
  551. {
  552. struct oti6858_private *priv = usb_get_serial_port_data(port);
  553. unsigned long flags;
  554. long timeout;
  555. wait_queue_t wait;
  556. dbg("%s(port = %d)", __FUNCTION__, port->number);
  557. /* wait for data to drain from the buffer */
  558. spin_lock_irqsave(&priv->lock, flags);
  559. timeout = 30 * HZ; /* PL2303_CLOSING_WAIT */
  560. init_waitqueue_entry(&wait, current);
  561. add_wait_queue(&port->tty->write_wait, &wait);
  562. dbg("%s(): entering wait loop", __FUNCTION__);
  563. for (;;) {
  564. set_current_state(TASK_INTERRUPTIBLE);
  565. if (pl2303_buf_data_avail(priv->buf) == 0
  566. || timeout == 0 || signal_pending(current)
  567. || !usb_get_intfdata(port->serial->interface)) /* disconnect */
  568. break;
  569. spin_unlock_irqrestore(&priv->lock, flags);
  570. timeout = schedule_timeout(timeout);
  571. spin_lock_irqsave(&priv->lock, flags);
  572. }
  573. set_current_state(TASK_RUNNING);
  574. remove_wait_queue(&port->tty->write_wait, &wait);
  575. dbg("%s(): after wait loop", __FUNCTION__);
  576. /* clear out any remaining data in the buffer */
  577. pl2303_buf_clear(priv->buf);
  578. spin_unlock_irqrestore(&priv->lock, flags);
  579. /* wait for characters to drain from the device */
  580. /* (this is long enough for the entire 256 byte */
  581. /* pl2303 hardware buffer to drain with no flow */
  582. /* control for data rates of 1200 bps or more, */
  583. /* for lower rates we should really know how much */
  584. /* data is in the buffer to compute a delay */
  585. /* that is not unnecessarily long) */
  586. /* FIXME
  587. bps = tty_get_baud_rate(port->tty);
  588. if (bps > 1200)
  589. timeout = max((HZ*2560)/bps,HZ/10);
  590. else
  591. */
  592. timeout = 2*HZ;
  593. schedule_timeout_interruptible(timeout);
  594. dbg("%s(): after schedule_timeout_interruptible()", __FUNCTION__);
  595. /* cancel scheduled setup */
  596. cancel_delayed_work(&priv->delayed_setup_work);
  597. cancel_delayed_work(&priv->delayed_write_work);
  598. flush_scheduled_work();
  599. /* shutdown our urbs */
  600. dbg("%s(): shutting down urbs", __FUNCTION__);
  601. usb_kill_urb(port->write_urb);
  602. usb_kill_urb(port->read_urb);
  603. usb_kill_urb(port->interrupt_in_urb);
  604. /*
  605. if (port->tty && (port->tty->termios->c_cflag) & HUPCL) {
  606. // drop DTR and RTS
  607. spin_lock_irqsave(&priv->lock, flags);
  608. priv->pending_setup.control &= ~CONTROL_MASK;
  609. spin_unlock_irqrestore(&priv->lock, flags);
  610. }
  611. */
  612. }
  613. static int oti6858_tiocmset(struct usb_serial_port *port, struct file *file,
  614. unsigned int set, unsigned int clear)
  615. {
  616. struct oti6858_private *priv = usb_get_serial_port_data(port);
  617. unsigned long flags;
  618. u8 control;
  619. dbg("%s(port = %d, set = 0x%08x, clear = 0x%08x)",
  620. __FUNCTION__, port->number, set, clear);
  621. if (!usb_get_intfdata(port->serial->interface))
  622. return -ENODEV;
  623. /* FIXME: check if this is correct (active high/low) */
  624. spin_lock_irqsave(&priv->lock, flags);
  625. control = priv->pending_setup.control;
  626. if ((set & TIOCM_RTS) != 0)
  627. control |= CONTROL_RTS_HIGH;
  628. if ((set & TIOCM_DTR) != 0)
  629. control |= CONTROL_DTR_HIGH;
  630. if ((clear & TIOCM_RTS) != 0)
  631. control &= ~CONTROL_RTS_HIGH;
  632. if ((clear & TIOCM_DTR) != 0)
  633. control &= ~CONTROL_DTR_HIGH;
  634. if (control != priv->pending_setup.control) {
  635. priv->pending_setup.control = control;
  636. }
  637. spin_unlock_irqrestore(&priv->lock, flags);
  638. return 0;
  639. }
  640. static int oti6858_tiocmget(struct usb_serial_port *port, struct file *file)
  641. {
  642. struct oti6858_private *priv = usb_get_serial_port_data(port);
  643. unsigned long flags;
  644. unsigned pin_state;
  645. unsigned result = 0;
  646. dbg("%s(port = %d)", __FUNCTION__, port->number);
  647. if (!usb_get_intfdata(port->serial->interface))
  648. return -ENODEV;
  649. spin_lock_irqsave(&priv->lock, flags);
  650. pin_state = priv->status.pin_state & PIN_MASK;
  651. spin_unlock_irqrestore(&priv->lock, flags);
  652. /* FIXME: check if this is correct (active high/low) */
  653. if ((pin_state & PIN_RTS) != 0)
  654. result |= TIOCM_RTS;
  655. if ((pin_state & PIN_CTS) != 0)
  656. result |= TIOCM_CTS;
  657. if ((pin_state & PIN_DSR) != 0)
  658. result |= TIOCM_DSR;
  659. if ((pin_state & PIN_DTR) != 0)
  660. result |= TIOCM_DTR;
  661. if ((pin_state & PIN_RI) != 0)
  662. result |= TIOCM_RI;
  663. if ((pin_state & PIN_DCD) != 0)
  664. result |= TIOCM_CD;
  665. dbg("%s() = 0x%08x", __FUNCTION__, result);
  666. return result;
  667. }
  668. static int wait_modem_info(struct usb_serial_port *port, unsigned int arg)
  669. {
  670. struct oti6858_private *priv = usb_get_serial_port_data(port);
  671. unsigned long flags;
  672. unsigned int prev, status;
  673. unsigned int changed;
  674. spin_lock_irqsave(&priv->lock, flags);
  675. prev = priv->status.pin_state;
  676. spin_unlock_irqrestore(&priv->lock, flags);
  677. while (1) {
  678. wait_event_interruptible(priv->intr_wait, priv->status.pin_state != prev);
  679. if (signal_pending(current))
  680. return -ERESTARTSYS;
  681. spin_lock_irqsave(&priv->lock, flags);
  682. status = priv->status.pin_state & PIN_MASK;
  683. spin_unlock_irqrestore(&priv->lock, flags);
  684. changed = prev ^ status;
  685. /* FIXME: check if this is correct (active high/low) */
  686. if ( ((arg & TIOCM_RNG) && (changed & PIN_RI)) ||
  687. ((arg & TIOCM_DSR) && (changed & PIN_DSR)) ||
  688. ((arg & TIOCM_CD) && (changed & PIN_DCD)) ||
  689. ((arg & TIOCM_CTS) && (changed & PIN_CTS))) {
  690. return 0;
  691. }
  692. prev = status;
  693. }
  694. /* NOTREACHED */
  695. return 0;
  696. }
  697. static int oti6858_ioctl(struct usb_serial_port *port, struct file *file,
  698. unsigned int cmd, unsigned long arg)
  699. {
  700. void __user *user_arg = (void __user *) arg;
  701. unsigned int x;
  702. dbg("%s(port = %d, cmd = 0x%04x, arg = 0x%08lx)",
  703. __FUNCTION__, port->number, cmd, arg);
  704. switch (cmd) {
  705. case TCFLSH:
  706. /* FIXME */
  707. return 0;
  708. case TIOCMBIS:
  709. if (copy_from_user(&x, user_arg, sizeof(x)))
  710. return -EFAULT;
  711. return oti6858_tiocmset(port, NULL, x, 0);
  712. case TIOCMBIC:
  713. if (copy_from_user(&x, user_arg, sizeof(x)))
  714. return -EFAULT;
  715. return oti6858_tiocmset(port, NULL, 0, x);
  716. case TIOCGSERIAL:
  717. if (copy_to_user(user_arg, port->tty->termios,
  718. sizeof(struct ktermios))) {
  719. return -EFAULT;
  720. }
  721. return 0;
  722. case TIOCSSERIAL:
  723. if (copy_from_user(port->tty->termios, user_arg,
  724. sizeof(struct ktermios))) {
  725. return -EFAULT;
  726. }
  727. oti6858_set_termios(port, NULL);
  728. return 0;
  729. case TIOCMIWAIT:
  730. dbg("%s(): TIOCMIWAIT", __FUNCTION__);
  731. return wait_modem_info(port, arg);
  732. default:
  733. dbg("%s(): 0x%04x not supported", __FUNCTION__, cmd);
  734. break;
  735. }
  736. return -ENOIOCTLCMD;
  737. }
  738. static void oti6858_break_ctl(struct usb_serial_port *port, int break_state)
  739. {
  740. int state;
  741. dbg("%s(port = %d)", __FUNCTION__, port->number);
  742. state = (break_state == 0) ? 0 : 1;
  743. dbg("%s(): turning break %s", __FUNCTION__, state ? "on" : "off");
  744. /* FIXME */
  745. /*
  746. result = usb_control_msg (serial->dev, usb_sndctrlpipe (serial->dev, 0),
  747. BREAK_REQUEST, BREAK_REQUEST_TYPE, state,
  748. 0, NULL, 0, 100);
  749. if (result != 0)
  750. dbg("%s(): error sending break", __FUNCTION__);
  751. */
  752. }
  753. static void oti6858_shutdown(struct usb_serial *serial)
  754. {
  755. struct oti6858_private *priv;
  756. int i;
  757. dbg("%s()", __FUNCTION__);
  758. for (i = 0; i < serial->num_ports; ++i) {
  759. priv = usb_get_serial_port_data(serial->port[i]);
  760. if (priv) {
  761. pl2303_buf_free(priv->buf);
  762. kfree(priv);
  763. usb_set_serial_port_data(serial->port[i], NULL);
  764. }
  765. }
  766. }
  767. static void oti6858_read_int_callback(struct urb *urb)
  768. {
  769. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  770. struct oti6858_private *priv = usb_get_serial_port_data(port);
  771. int transient = 0, can_recv = 0, resubmit = 1;
  772. int status = urb->status;
  773. dbg("%s(port = %d, status = %d)",
  774. __FUNCTION__, port->number, status);
  775. switch (status) {
  776. case 0:
  777. /* success */
  778. break;
  779. case -ECONNRESET:
  780. case -ENOENT:
  781. case -ESHUTDOWN:
  782. /* this urb is terminated, clean up */
  783. dbg("%s(): urb shutting down with status: %d",
  784. __FUNCTION__, status);
  785. return;
  786. default:
  787. dbg("%s(): nonzero urb status received: %d",
  788. __FUNCTION__, status);
  789. break;
  790. }
  791. if (status == 0 && urb->actual_length == OTI6858_CTRL_PKT_SIZE) {
  792. struct oti6858_control_pkt *xs = urb->transfer_buffer;
  793. unsigned long flags;
  794. spin_lock_irqsave(&priv->lock, flags);
  795. if (!priv->transient) {
  796. if (!OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  797. if (xs->rx_bytes_avail == 0) {
  798. priv->transient = 4;
  799. priv->setup_done = 0;
  800. resubmit = 0;
  801. dbg("%s(): scheduling setup_line()",
  802. __FUNCTION__);
  803. schedule_delayed_work(&priv->delayed_setup_work, 0);
  804. }
  805. }
  806. } else {
  807. if (OTI6858_CTRL_EQUALS_PENDING(xs, priv)) {
  808. priv->transient = 0;
  809. } else if (!priv->setup_done) {
  810. resubmit = 0;
  811. } else if (--priv->transient == 0) {
  812. if (xs->rx_bytes_avail == 0) {
  813. priv->transient = 4;
  814. priv->setup_done = 0;
  815. resubmit = 0;
  816. dbg("%s(): scheduling setup_line()",
  817. __FUNCTION__);
  818. schedule_delayed_work(&priv->delayed_setup_work, 0);
  819. }
  820. }
  821. }
  822. if (!priv->transient) {
  823. if (xs->pin_state != priv->status.pin_state)
  824. wake_up_interruptible(&priv->intr_wait);
  825. memcpy(&priv->status, xs, OTI6858_CTRL_PKT_SIZE);
  826. }
  827. if (!priv->transient && xs->rx_bytes_avail != 0) {
  828. can_recv = xs->rx_bytes_avail;
  829. priv->flags.read_urb_in_use = 1;
  830. }
  831. transient = priv->transient;
  832. spin_unlock_irqrestore(&priv->lock, flags);
  833. }
  834. if (can_recv) {
  835. int result;
  836. port->read_urb->dev = port->serial->dev;
  837. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  838. if (result != 0) {
  839. priv->flags.read_urb_in_use = 0;
  840. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  841. " error %d\n", __FUNCTION__, result);
  842. } else {
  843. resubmit = 0;
  844. }
  845. } else if (!transient) {
  846. unsigned long flags;
  847. spin_lock_irqsave(&priv->lock, flags);
  848. if (priv->flags.write_urb_in_use == 0
  849. && pl2303_buf_data_avail(priv->buf) != 0) {
  850. schedule_delayed_work(&priv->delayed_write_work,0);
  851. resubmit = 0;
  852. }
  853. spin_unlock_irqrestore(&priv->lock, flags);
  854. }
  855. if (resubmit) {
  856. int result;
  857. // dbg("%s(): submitting interrupt urb", __FUNCTION__);
  858. urb->dev = port->serial->dev;
  859. result = usb_submit_urb(urb, GFP_ATOMIC);
  860. if (result != 0) {
  861. dev_err(&urb->dev->dev,
  862. "%s(): usb_submit_urb() failed with"
  863. " error %d\n", __FUNCTION__, result);
  864. }
  865. }
  866. }
  867. static void oti6858_read_bulk_callback(struct urb *urb)
  868. {
  869. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  870. struct oti6858_private *priv = usb_get_serial_port_data(port);
  871. struct tty_struct *tty;
  872. unsigned char *data = urb->transfer_buffer;
  873. unsigned long flags;
  874. int i, result;
  875. int status = urb->status;
  876. char tty_flag;
  877. dbg("%s(port = %d, status = %d)",
  878. __FUNCTION__, port->number, status);
  879. spin_lock_irqsave(&priv->lock, flags);
  880. priv->flags.read_urb_in_use = 0;
  881. spin_unlock_irqrestore(&priv->lock, flags);
  882. if (status != 0) {
  883. if (!port->open_count) {
  884. dbg("%s(): port is closed, exiting", __FUNCTION__);
  885. return;
  886. }
  887. /*
  888. if (status == -EPROTO) {
  889. // PL2303 mysteriously fails with -EPROTO reschedule the read
  890. dbg("%s - caught -EPROTO, resubmitting the urb", __FUNCTION__);
  891. result = usb_submit_urb(urb, GFP_ATOMIC);
  892. if (result)
  893. dev_err(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n", __FUNCTION__, result);
  894. return;
  895. }
  896. */
  897. dbg("%s(): unable to handle the error, exiting", __FUNCTION__);
  898. return;
  899. }
  900. // get tty_flag from status
  901. tty_flag = TTY_NORMAL;
  902. /* FIXME: probably, errors will be signalled using interrupt pipe! */
  903. /*
  904. // break takes precedence over parity,
  905. // which takes precedence over framing errors
  906. if (status & UART_BREAK_ERROR )
  907. tty_flag = TTY_BREAK;
  908. else if (status & UART_PARITY_ERROR)
  909. tty_flag = TTY_PARITY;
  910. else if (status & UART_FRAME_ERROR)
  911. tty_flag = TTY_FRAME;
  912. dbg("%s - tty_flag = %d", __FUNCTION__, tty_flag);
  913. */
  914. tty = port->tty;
  915. if (tty != NULL && urb->actual_length > 0) {
  916. tty_buffer_request_room(tty, urb->actual_length);
  917. for (i = 0; i < urb->actual_length; ++i)
  918. tty_insert_flip_char(tty, data[i], tty_flag);
  919. tty_flip_buffer_push(tty);
  920. }
  921. // schedule the interrupt urb if we are still open */
  922. if (port->open_count != 0) {
  923. port->interrupt_in_urb->dev = port->serial->dev;
  924. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  925. if (result != 0) {
  926. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  927. " error %d\n", __FUNCTION__, result);
  928. }
  929. }
  930. }
  931. static void oti6858_write_bulk_callback(struct urb *urb)
  932. {
  933. struct usb_serial_port *port = (struct usb_serial_port *) urb->context;
  934. struct oti6858_private *priv = usb_get_serial_port_data(port);
  935. int status = urb->status;
  936. int result;
  937. dbg("%s(port = %d, status = %d)",
  938. __FUNCTION__, port->number, status);
  939. switch (status) {
  940. case 0:
  941. /* success */
  942. break;
  943. case -ECONNRESET:
  944. case -ENOENT:
  945. case -ESHUTDOWN:
  946. /* this urb is terminated, clean up */
  947. dbg("%s(): urb shutting down with status: %d",
  948. __FUNCTION__, status);
  949. priv->flags.write_urb_in_use = 0;
  950. return;
  951. default:
  952. /* error in the urb, so we have to resubmit it */
  953. dbg("%s(): nonzero write bulk status received: %d",
  954. __FUNCTION__, status);
  955. dbg("%s(): overflow in write", __FUNCTION__);
  956. port->write_urb->transfer_buffer_length = 1;
  957. port->write_urb->dev = port->serial->dev;
  958. result = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  959. if (result) {
  960. dev_err(&port->dev, "%s(): usb_submit_urb() failed,"
  961. " error %d\n", __FUNCTION__, result);
  962. } else {
  963. return;
  964. }
  965. }
  966. priv->flags.write_urb_in_use = 0;
  967. // schedule the interrupt urb if we are still open */
  968. port->interrupt_in_urb->dev = port->serial->dev;
  969. dbg("%s(): submitting interrupt urb", __FUNCTION__);
  970. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  971. if (result != 0) {
  972. dev_err(&port->dev, "%s(): failed submitting int urb,"
  973. " error %d\n", __FUNCTION__, result);
  974. }
  975. }
  976. /*
  977. * pl2303_buf_alloc
  978. *
  979. * Allocate a circular buffer and all associated memory.
  980. */
  981. static struct pl2303_buf *pl2303_buf_alloc(unsigned int size)
  982. {
  983. struct pl2303_buf *pb;
  984. if (size == 0)
  985. return NULL;
  986. pb = kmalloc(sizeof(struct pl2303_buf), GFP_KERNEL);
  987. if (pb == NULL)
  988. return NULL;
  989. pb->buf_buf = kmalloc(size, GFP_KERNEL);
  990. if (pb->buf_buf == NULL) {
  991. kfree(pb);
  992. return NULL;
  993. }
  994. pb->buf_size = size;
  995. pb->buf_get = pb->buf_put = pb->buf_buf;
  996. return pb;
  997. }
  998. /*
  999. * pl2303_buf_free
  1000. *
  1001. * Free the buffer and all associated memory.
  1002. */
  1003. static void pl2303_buf_free(struct pl2303_buf *pb)
  1004. {
  1005. if (pb) {
  1006. kfree(pb->buf_buf);
  1007. kfree(pb);
  1008. }
  1009. }
  1010. /*
  1011. * pl2303_buf_clear
  1012. *
  1013. * Clear out all data in the circular buffer.
  1014. */
  1015. static void pl2303_buf_clear(struct pl2303_buf *pb)
  1016. {
  1017. if (pb != NULL) {
  1018. /* equivalent to a get of all data available */
  1019. pb->buf_get = pb->buf_put;
  1020. }
  1021. }
  1022. /*
  1023. * pl2303_buf_data_avail
  1024. *
  1025. * Return the number of bytes of data available in the circular
  1026. * buffer.
  1027. */
  1028. static unsigned int pl2303_buf_data_avail(struct pl2303_buf *pb)
  1029. {
  1030. if (pb == NULL)
  1031. return 0;
  1032. return ((pb->buf_size + pb->buf_put - pb->buf_get) % pb->buf_size);
  1033. }
  1034. /*
  1035. * pl2303_buf_space_avail
  1036. *
  1037. * Return the number of bytes of space available in the circular
  1038. * buffer.
  1039. */
  1040. static unsigned int pl2303_buf_space_avail(struct pl2303_buf *pb)
  1041. {
  1042. if (pb == NULL)
  1043. return 0;
  1044. return ((pb->buf_size + pb->buf_get - pb->buf_put - 1) % pb->buf_size);
  1045. }
  1046. /*
  1047. * pl2303_buf_put
  1048. *
  1049. * Copy data data from a user buffer and put it into the circular buffer.
  1050. * Restrict to the amount of space available.
  1051. *
  1052. * Return the number of bytes copied.
  1053. */
  1054. static unsigned int pl2303_buf_put(struct pl2303_buf *pb, const char *buf,
  1055. unsigned int count)
  1056. {
  1057. unsigned int len;
  1058. if (pb == NULL)
  1059. return 0;
  1060. len = pl2303_buf_space_avail(pb);
  1061. if (count > len)
  1062. count = len;
  1063. if (count == 0)
  1064. return 0;
  1065. len = pb->buf_buf + pb->buf_size - pb->buf_put;
  1066. if (count > len) {
  1067. memcpy(pb->buf_put, buf, len);
  1068. memcpy(pb->buf_buf, buf+len, count - len);
  1069. pb->buf_put = pb->buf_buf + count - len;
  1070. } else {
  1071. memcpy(pb->buf_put, buf, count);
  1072. if (count < len)
  1073. pb->buf_put += count;
  1074. else /* count == len */
  1075. pb->buf_put = pb->buf_buf;
  1076. }
  1077. return count;
  1078. }
  1079. /*
  1080. * pl2303_buf_get
  1081. *
  1082. * Get data from the circular buffer and copy to the given buffer.
  1083. * Restrict to the amount of data available.
  1084. *
  1085. * Return the number of bytes copied.
  1086. */
  1087. static unsigned int pl2303_buf_get(struct pl2303_buf *pb, char *buf,
  1088. unsigned int count)
  1089. {
  1090. unsigned int len;
  1091. if (pb == NULL)
  1092. return 0;
  1093. len = pl2303_buf_data_avail(pb);
  1094. if (count > len)
  1095. count = len;
  1096. if (count == 0)
  1097. return 0;
  1098. len = pb->buf_buf + pb->buf_size - pb->buf_get;
  1099. if (count > len) {
  1100. memcpy(buf, pb->buf_get, len);
  1101. memcpy(buf+len, pb->buf_buf, count - len);
  1102. pb->buf_get = pb->buf_buf + count - len;
  1103. } else {
  1104. memcpy(buf, pb->buf_get, count);
  1105. if (count < len)
  1106. pb->buf_get += count;
  1107. else /* count == len */
  1108. pb->buf_get = pb->buf_buf;
  1109. }
  1110. return count;
  1111. }
  1112. /* module description and (de)initialization */
  1113. static int __init oti6858_init(void)
  1114. {
  1115. int retval;
  1116. if ((retval = usb_serial_register(&oti6858_device)) == 0) {
  1117. if ((retval = usb_register(&oti6858_driver)) != 0)
  1118. usb_serial_deregister(&oti6858_device);
  1119. else
  1120. return 0;
  1121. }
  1122. return retval;
  1123. }
  1124. static void __exit oti6858_exit(void)
  1125. {
  1126. usb_deregister(&oti6858_driver);
  1127. usb_serial_deregister(&oti6858_device);
  1128. }
  1129. module_init(oti6858_init);
  1130. module_exit(oti6858_exit);
  1131. MODULE_DESCRIPTION(OTI6858_DESCRIPTION);
  1132. MODULE_AUTHOR(OTI6858_AUTHOR);
  1133. MODULE_VERSION(OTI6858_VERSION);
  1134. MODULE_LICENSE("GPL");
  1135. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1136. MODULE_PARM_DESC(debug, "enable debug output");