oti6858.c 34 KB

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