oti6858.c 34 KB

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