keyspan_pda.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /*
  2. * USB Keyspan PDA / Xircom / Entrega Converter driver
  3. *
  4. * Copyright (C) 1999 - 2001 Greg Kroah-Hartman <greg@kroah.com>
  5. * Copyright (C) 1999, 2000 Brian Warner <warner@lothar.com>
  6. * Copyright (C) 2000 Al Borchers <borchers@steinerpoint.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * See Documentation/usb/usb-serial.txt for more information on using this
  14. * driver
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/slab.h>
  19. #include <linux/tty.h>
  20. #include <linux/tty_driver.h>
  21. #include <linux/tty_flip.h>
  22. #include <linux/module.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/uaccess.h>
  26. #include <linux/usb.h>
  27. #include <linux/usb/serial.h>
  28. #include <linux/usb/ezusb.h>
  29. /* make a simple define to handle if we are compiling keyspan_pda or xircom support */
  30. #if IS_ENABLED(CONFIG_USB_SERIAL_KEYSPAN_PDA)
  31. #define KEYSPAN
  32. #else
  33. #undef KEYSPAN
  34. #endif
  35. #if IS_ENABLED(CONFIG_USB_SERIAL_XIRCOM)
  36. #define XIRCOM
  37. #else
  38. #undef XIRCOM
  39. #endif
  40. #define DRIVER_AUTHOR "Brian Warner <warner@lothar.com>"
  41. #define DRIVER_DESC "USB Keyspan PDA Converter driver"
  42. #define KEYSPAN_TX_THRESHOLD 16
  43. struct keyspan_pda_private {
  44. int tx_room;
  45. int tx_throttled;
  46. struct work_struct unthrottle_work;
  47. struct usb_serial *serial;
  48. struct usb_serial_port *port;
  49. };
  50. #define KEYSPAN_VENDOR_ID 0x06cd
  51. #define KEYSPAN_PDA_FAKE_ID 0x0103
  52. #define KEYSPAN_PDA_ID 0x0104 /* no clue */
  53. /* For Xircom PGSDB9 and older Entrega version of the same device */
  54. #define XIRCOM_VENDOR_ID 0x085a
  55. #define XIRCOM_FAKE_ID 0x8027
  56. #define XIRCOM_FAKE_ID_2 0x8025 /* "PGMFHUB" serial */
  57. #define ENTREGA_VENDOR_ID 0x1645
  58. #define ENTREGA_FAKE_ID 0x8093
  59. static const struct usb_device_id id_table_combined[] = {
  60. #ifdef KEYSPAN
  61. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
  62. #endif
  63. #ifdef XIRCOM
  64. { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
  65. { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
  66. { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
  67. #endif
  68. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
  69. { } /* Terminating entry */
  70. };
  71. MODULE_DEVICE_TABLE(usb, id_table_combined);
  72. static const struct usb_device_id id_table_std[] = {
  73. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_ID) },
  74. { } /* Terminating entry */
  75. };
  76. #ifdef KEYSPAN
  77. static const struct usb_device_id id_table_fake[] = {
  78. { USB_DEVICE(KEYSPAN_VENDOR_ID, KEYSPAN_PDA_FAKE_ID) },
  79. { } /* Terminating entry */
  80. };
  81. #endif
  82. #ifdef XIRCOM
  83. static const struct usb_device_id id_table_fake_xircom[] = {
  84. { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID) },
  85. { USB_DEVICE(XIRCOM_VENDOR_ID, XIRCOM_FAKE_ID_2) },
  86. { USB_DEVICE(ENTREGA_VENDOR_ID, ENTREGA_FAKE_ID) },
  87. { }
  88. };
  89. #endif
  90. static void keyspan_pda_request_unthrottle(struct work_struct *work)
  91. {
  92. struct keyspan_pda_private *priv =
  93. container_of(work, struct keyspan_pda_private, unthrottle_work);
  94. struct usb_serial *serial = priv->serial;
  95. int result;
  96. /* ask the device to tell us when the tx buffer becomes
  97. sufficiently empty */
  98. result = usb_control_msg(serial->dev,
  99. usb_sndctrlpipe(serial->dev, 0),
  100. 7, /* request_unthrottle */
  101. USB_TYPE_VENDOR | USB_RECIP_INTERFACE
  102. | USB_DIR_OUT,
  103. KEYSPAN_TX_THRESHOLD,
  104. 0, /* index */
  105. NULL,
  106. 0,
  107. 2000);
  108. if (result < 0)
  109. dev_dbg(&serial->dev->dev, "%s - error %d from usb_control_msg\n",
  110. __func__, result);
  111. }
  112. static void keyspan_pda_rx_interrupt(struct urb *urb)
  113. {
  114. struct usb_serial_port *port = urb->context;
  115. unsigned char *data = urb->transfer_buffer;
  116. unsigned int len = urb->actual_length;
  117. int retval;
  118. int status = urb->status;
  119. struct keyspan_pda_private *priv;
  120. unsigned long flags;
  121. priv = usb_get_serial_port_data(port);
  122. switch (status) {
  123. case 0:
  124. /* success */
  125. break;
  126. case -ECONNRESET:
  127. case -ENOENT:
  128. case -ESHUTDOWN:
  129. /* this urb is terminated, clean up */
  130. dev_dbg(&urb->dev->dev, "%s - urb shutting down with status: %d\n", __func__, status);
  131. return;
  132. default:
  133. dev_dbg(&urb->dev->dev, "%s - nonzero urb status received: %d\n", __func__, status);
  134. goto exit;
  135. }
  136. if (len < 1) {
  137. dev_warn(&port->dev, "short message received\n");
  138. goto exit;
  139. }
  140. /* see if the message is data or a status interrupt */
  141. switch (data[0]) {
  142. case 0:
  143. /* rest of message is rx data */
  144. if (len < 2)
  145. break;
  146. tty_insert_flip_string(&port->port, data + 1, len - 1);
  147. tty_flip_buffer_push(&port->port);
  148. break;
  149. case 1:
  150. /* status interrupt */
  151. if (len < 2) {
  152. dev_warn(&port->dev, "short interrupt message received\n");
  153. break;
  154. }
  155. dev_dbg(&port->dev, "rx int, d1=%d\n", data[1]);
  156. switch (data[1]) {
  157. case 1: /* modemline change */
  158. break;
  159. case 2: /* tx unthrottle interrupt */
  160. spin_lock_irqsave(&port->lock, flags);
  161. priv->tx_throttled = 0;
  162. priv->tx_room = max(priv->tx_room, KEYSPAN_TX_THRESHOLD);
  163. spin_unlock_irqrestore(&port->lock, flags);
  164. /* queue up a wakeup at scheduler time */
  165. usb_serial_port_softint(port);
  166. break;
  167. default:
  168. break;
  169. }
  170. break;
  171. default:
  172. break;
  173. }
  174. exit:
  175. retval = usb_submit_urb(urb, GFP_ATOMIC);
  176. if (retval)
  177. dev_err(&port->dev,
  178. "%s - usb_submit_urb failed with result %d\n",
  179. __func__, retval);
  180. }
  181. static void keyspan_pda_rx_throttle(struct tty_struct *tty)
  182. {
  183. /* stop receiving characters. We just turn off the URB request, and
  184. let chars pile up in the device. If we're doing hardware
  185. flowcontrol, the device will signal the other end when its buffer
  186. fills up. If we're doing XON/XOFF, this would be a good time to
  187. send an XOFF, although it might make sense to foist that off
  188. upon the device too. */
  189. struct usb_serial_port *port = tty->driver_data;
  190. usb_kill_urb(port->interrupt_in_urb);
  191. }
  192. static void keyspan_pda_rx_unthrottle(struct tty_struct *tty)
  193. {
  194. struct usb_serial_port *port = tty->driver_data;
  195. /* just restart the receive interrupt URB */
  196. if (usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL))
  197. dev_dbg(&port->dev, "usb_submit_urb(read urb) failed\n");
  198. }
  199. static speed_t keyspan_pda_setbaud(struct usb_serial *serial, speed_t baud)
  200. {
  201. int rc;
  202. int bindex;
  203. switch (baud) {
  204. case 110:
  205. bindex = 0;
  206. break;
  207. case 300:
  208. bindex = 1;
  209. break;
  210. case 1200:
  211. bindex = 2;
  212. break;
  213. case 2400:
  214. bindex = 3;
  215. break;
  216. case 4800:
  217. bindex = 4;
  218. break;
  219. case 9600:
  220. bindex = 5;
  221. break;
  222. case 19200:
  223. bindex = 6;
  224. break;
  225. case 38400:
  226. bindex = 7;
  227. break;
  228. case 57600:
  229. bindex = 8;
  230. break;
  231. case 115200:
  232. bindex = 9;
  233. break;
  234. default:
  235. bindex = 5; /* Default to 9600 */
  236. baud = 9600;
  237. }
  238. /* rather than figure out how to sleep while waiting for this
  239. to complete, I just use the "legacy" API. */
  240. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  241. 0, /* set baud */
  242. USB_TYPE_VENDOR
  243. | USB_RECIP_INTERFACE
  244. | USB_DIR_OUT, /* type */
  245. bindex, /* value */
  246. 0, /* index */
  247. NULL, /* &data */
  248. 0, /* size */
  249. 2000); /* timeout */
  250. if (rc < 0)
  251. return 0;
  252. return baud;
  253. }
  254. static void keyspan_pda_break_ctl(struct tty_struct *tty, int break_state)
  255. {
  256. struct usb_serial_port *port = tty->driver_data;
  257. struct usb_serial *serial = port->serial;
  258. int value;
  259. int result;
  260. if (break_state == -1)
  261. value = 1; /* start break */
  262. else
  263. value = 0; /* clear break */
  264. result = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  265. 4, /* set break */
  266. USB_TYPE_VENDOR | USB_RECIP_INTERFACE | USB_DIR_OUT,
  267. value, 0, NULL, 0, 2000);
  268. if (result < 0)
  269. dev_dbg(&port->dev, "%s - error %d from usb_control_msg\n",
  270. __func__, result);
  271. /* there is something funky about this.. the TCSBRK that 'cu' performs
  272. ought to translate into a break_ctl(-1),break_ctl(0) pair HZ/4
  273. seconds apart, but it feels like the break sent isn't as long as it
  274. is on /dev/ttyS0 */
  275. }
  276. static void keyspan_pda_set_termios(struct tty_struct *tty,
  277. struct usb_serial_port *port, struct ktermios *old_termios)
  278. {
  279. struct usb_serial *serial = port->serial;
  280. speed_t speed;
  281. /* cflag specifies lots of stuff: number of stop bits, parity, number
  282. of data bits, baud. What can the device actually handle?:
  283. CSTOPB (1 stop bit or 2)
  284. PARENB (parity)
  285. CSIZE (5bit .. 8bit)
  286. There is minimal hw support for parity (a PSW bit seems to hold the
  287. parity of whatever is in the accumulator). The UART either deals
  288. with 10 bits (start, 8 data, stop) or 11 bits (start, 8 data,
  289. 1 special, stop). So, with firmware changes, we could do:
  290. 8N1: 10 bit
  291. 8N2: 11 bit, extra bit always (mark?)
  292. 8[EOMS]1: 11 bit, extra bit is parity
  293. 7[EOMS]1: 10 bit, b0/b7 is parity
  294. 7[EOMS]2: 11 bit, b0/b7 is parity, extra bit always (mark?)
  295. HW flow control is dictated by the tty->termios.c_cflags & CRTSCTS
  296. bit.
  297. For now, just do baud. */
  298. speed = tty_get_baud_rate(tty);
  299. speed = keyspan_pda_setbaud(serial, speed);
  300. if (speed == 0) {
  301. dev_dbg(&port->dev, "can't handle requested baud rate\n");
  302. /* It hasn't changed so.. */
  303. speed = tty_termios_baud_rate(old_termios);
  304. }
  305. /* Only speed can change so copy the old h/w parameters
  306. then encode the new speed */
  307. tty_termios_copy_hw(&tty->termios, old_termios);
  308. tty_encode_baud_rate(tty, speed, speed);
  309. }
  310. /* modem control pins: DTR and RTS are outputs and can be controlled.
  311. DCD, RI, DSR, CTS are inputs and can be read. All outputs can also be
  312. read. The byte passed is: DTR(b7) DCD RI DSR CTS RTS(b2) unused unused */
  313. static int keyspan_pda_get_modem_info(struct usb_serial *serial,
  314. unsigned char *value)
  315. {
  316. int rc;
  317. u8 *data;
  318. data = kmalloc(1, GFP_KERNEL);
  319. if (!data)
  320. return -ENOMEM;
  321. rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  322. 3, /* get pins */
  323. USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_IN,
  324. 0, 0, data, 1, 2000);
  325. if (rc == 1)
  326. *value = *data;
  327. else if (rc >= 0)
  328. rc = -EIO;
  329. kfree(data);
  330. return rc;
  331. }
  332. static int keyspan_pda_set_modem_info(struct usb_serial *serial,
  333. unsigned char value)
  334. {
  335. int rc;
  336. rc = usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  337. 3, /* set pins */
  338. USB_TYPE_VENDOR|USB_RECIP_INTERFACE|USB_DIR_OUT,
  339. value, 0, NULL, 0, 2000);
  340. return rc;
  341. }
  342. static int keyspan_pda_tiocmget(struct tty_struct *tty)
  343. {
  344. struct usb_serial_port *port = tty->driver_data;
  345. struct usb_serial *serial = port->serial;
  346. int rc;
  347. unsigned char status;
  348. int value;
  349. rc = keyspan_pda_get_modem_info(serial, &status);
  350. if (rc < 0)
  351. return rc;
  352. value =
  353. ((status & (1<<7)) ? TIOCM_DTR : 0) |
  354. ((status & (1<<6)) ? TIOCM_CAR : 0) |
  355. ((status & (1<<5)) ? TIOCM_RNG : 0) |
  356. ((status & (1<<4)) ? TIOCM_DSR : 0) |
  357. ((status & (1<<3)) ? TIOCM_CTS : 0) |
  358. ((status & (1<<2)) ? TIOCM_RTS : 0);
  359. return value;
  360. }
  361. static int keyspan_pda_tiocmset(struct tty_struct *tty,
  362. unsigned int set, unsigned int clear)
  363. {
  364. struct usb_serial_port *port = tty->driver_data;
  365. struct usb_serial *serial = port->serial;
  366. int rc;
  367. unsigned char status;
  368. rc = keyspan_pda_get_modem_info(serial, &status);
  369. if (rc < 0)
  370. return rc;
  371. if (set & TIOCM_RTS)
  372. status |= (1<<2);
  373. if (set & TIOCM_DTR)
  374. status |= (1<<7);
  375. if (clear & TIOCM_RTS)
  376. status &= ~(1<<2);
  377. if (clear & TIOCM_DTR)
  378. status &= ~(1<<7);
  379. rc = keyspan_pda_set_modem_info(serial, status);
  380. return rc;
  381. }
  382. static int keyspan_pda_write(struct tty_struct *tty,
  383. struct usb_serial_port *port, const unsigned char *buf, int count)
  384. {
  385. struct usb_serial *serial = port->serial;
  386. int request_unthrottle = 0;
  387. int rc = 0;
  388. struct keyspan_pda_private *priv;
  389. unsigned long flags;
  390. priv = usb_get_serial_port_data(port);
  391. /* guess how much room is left in the device's ring buffer, and if we
  392. want to send more than that, check first, updating our notion of
  393. what is left. If our write will result in no room left, ask the
  394. device to give us an interrupt when the room available rises above
  395. a threshold, and hold off all writers (eventually, those using
  396. select() or poll() too) until we receive that unthrottle interrupt.
  397. Block if we can't write anything at all, otherwise write as much as
  398. we can. */
  399. if (count == 0) {
  400. dev_dbg(&port->dev, "write request of 0 bytes\n");
  401. return 0;
  402. }
  403. /* we might block because of:
  404. the TX urb is in-flight (wait until it completes)
  405. the device is full (wait until it says there is room)
  406. */
  407. spin_lock_irqsave(&port->lock, flags);
  408. if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled) {
  409. spin_unlock_irqrestore(&port->lock, flags);
  410. return 0;
  411. }
  412. clear_bit(0, &port->write_urbs_free);
  413. spin_unlock_irqrestore(&port->lock, flags);
  414. /* At this point the URB is in our control, nobody else can submit it
  415. again (the only sudden transition was the one from EINPROGRESS to
  416. finished). Also, the tx process is not throttled. So we are
  417. ready to write. */
  418. count = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  419. /* Check if we might overrun the Tx buffer. If so, ask the
  420. device how much room it really has. This is done only on
  421. scheduler time, since usb_control_msg() sleeps. */
  422. if (count > priv->tx_room && !in_interrupt()) {
  423. u8 *room;
  424. room = kmalloc(1, GFP_KERNEL);
  425. if (!room) {
  426. rc = -ENOMEM;
  427. goto exit;
  428. }
  429. rc = usb_control_msg(serial->dev,
  430. usb_rcvctrlpipe(serial->dev, 0),
  431. 6, /* write_room */
  432. USB_TYPE_VENDOR | USB_RECIP_INTERFACE
  433. | USB_DIR_IN,
  434. 0, /* value: 0 means "remaining room" */
  435. 0, /* index */
  436. room,
  437. 1,
  438. 2000);
  439. if (rc > 0) {
  440. dev_dbg(&port->dev, "roomquery says %d\n", *room);
  441. priv->tx_room = *room;
  442. }
  443. kfree(room);
  444. if (rc < 0) {
  445. dev_dbg(&port->dev, "roomquery failed\n");
  446. goto exit;
  447. }
  448. if (rc == 0) {
  449. dev_dbg(&port->dev, "roomquery returned 0 bytes\n");
  450. rc = -EIO; /* device didn't return any data */
  451. goto exit;
  452. }
  453. }
  454. if (count >= priv->tx_room) {
  455. /* we're about to completely fill the Tx buffer, so
  456. we'll be throttled afterwards. */
  457. count = priv->tx_room;
  458. request_unthrottle = 1;
  459. }
  460. if (count) {
  461. /* now transfer data */
  462. memcpy(port->write_urb->transfer_buffer, buf, count);
  463. /* send the data out the bulk port */
  464. port->write_urb->transfer_buffer_length = count;
  465. priv->tx_room -= count;
  466. rc = usb_submit_urb(port->write_urb, GFP_ATOMIC);
  467. if (rc) {
  468. dev_dbg(&port->dev, "usb_submit_urb(write bulk) failed\n");
  469. goto exit;
  470. }
  471. } else {
  472. /* There wasn't any room left, so we are throttled until
  473. the buffer empties a bit */
  474. request_unthrottle = 1;
  475. }
  476. if (request_unthrottle) {
  477. priv->tx_throttled = 1; /* block writers */
  478. schedule_work(&priv->unthrottle_work);
  479. }
  480. rc = count;
  481. exit:
  482. if (rc <= 0)
  483. set_bit(0, &port->write_urbs_free);
  484. return rc;
  485. }
  486. static void keyspan_pda_write_bulk_callback(struct urb *urb)
  487. {
  488. struct usb_serial_port *port = urb->context;
  489. set_bit(0, &port->write_urbs_free);
  490. /* queue up a wakeup at scheduler time */
  491. usb_serial_port_softint(port);
  492. }
  493. static int keyspan_pda_write_room(struct tty_struct *tty)
  494. {
  495. struct usb_serial_port *port = tty->driver_data;
  496. struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
  497. unsigned long flags;
  498. int room = 0;
  499. spin_lock_irqsave(&port->lock, flags);
  500. if (test_bit(0, &port->write_urbs_free) && !priv->tx_throttled)
  501. room = priv->tx_room;
  502. spin_unlock_irqrestore(&port->lock, flags);
  503. return room;
  504. }
  505. static int keyspan_pda_chars_in_buffer(struct tty_struct *tty)
  506. {
  507. struct usb_serial_port *port = tty->driver_data;
  508. struct keyspan_pda_private *priv;
  509. unsigned long flags;
  510. int ret = 0;
  511. priv = usb_get_serial_port_data(port);
  512. /* when throttled, return at least WAKEUP_CHARS to tell select() (via
  513. n_tty.c:normal_poll() ) that we're not writeable. */
  514. spin_lock_irqsave(&port->lock, flags);
  515. if (!test_bit(0, &port->write_urbs_free) || priv->tx_throttled)
  516. ret = 256;
  517. spin_unlock_irqrestore(&port->lock, flags);
  518. return ret;
  519. }
  520. static void keyspan_pda_dtr_rts(struct usb_serial_port *port, int on)
  521. {
  522. struct usb_serial *serial = port->serial;
  523. if (on)
  524. keyspan_pda_set_modem_info(serial, (1 << 7) | (1 << 2));
  525. else
  526. keyspan_pda_set_modem_info(serial, 0);
  527. }
  528. static int keyspan_pda_open(struct tty_struct *tty,
  529. struct usb_serial_port *port)
  530. {
  531. struct usb_serial *serial = port->serial;
  532. u8 *room;
  533. int rc = 0;
  534. struct keyspan_pda_private *priv;
  535. /* find out how much room is in the Tx ring */
  536. room = kmalloc(1, GFP_KERNEL);
  537. if (!room)
  538. return -ENOMEM;
  539. rc = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  540. 6, /* write_room */
  541. USB_TYPE_VENDOR | USB_RECIP_INTERFACE
  542. | USB_DIR_IN,
  543. 0, /* value */
  544. 0, /* index */
  545. room,
  546. 1,
  547. 2000);
  548. if (rc < 0) {
  549. dev_dbg(&port->dev, "%s - roomquery failed\n", __func__);
  550. goto error;
  551. }
  552. if (rc == 0) {
  553. dev_dbg(&port->dev, "%s - roomquery returned 0 bytes\n", __func__);
  554. rc = -EIO;
  555. goto error;
  556. }
  557. priv = usb_get_serial_port_data(port);
  558. priv->tx_room = *room;
  559. priv->tx_throttled = *room ? 0 : 1;
  560. /*Start reading from the device*/
  561. rc = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  562. if (rc) {
  563. dev_dbg(&port->dev, "%s - usb_submit_urb(read int) failed\n", __func__);
  564. goto error;
  565. }
  566. error:
  567. kfree(room);
  568. return rc;
  569. }
  570. static void keyspan_pda_close(struct usb_serial_port *port)
  571. {
  572. struct keyspan_pda_private *priv = usb_get_serial_port_data(port);
  573. usb_kill_urb(port->write_urb);
  574. usb_kill_urb(port->interrupt_in_urb);
  575. cancel_work_sync(&priv->unthrottle_work);
  576. }
  577. /* download the firmware to a "fake" device (pre-renumeration) */
  578. static int keyspan_pda_fake_startup(struct usb_serial *serial)
  579. {
  580. int response;
  581. const char *fw_name;
  582. /* download the firmware here ... */
  583. response = ezusb_fx1_set_reset(serial->dev, 1);
  584. if (0) { ; }
  585. #ifdef KEYSPAN
  586. else if (le16_to_cpu(serial->dev->descriptor.idVendor) == KEYSPAN_VENDOR_ID)
  587. fw_name = "keyspan_pda/keyspan_pda.fw";
  588. #endif
  589. #ifdef XIRCOM
  590. else if ((le16_to_cpu(serial->dev->descriptor.idVendor) == XIRCOM_VENDOR_ID) ||
  591. (le16_to_cpu(serial->dev->descriptor.idVendor) == ENTREGA_VENDOR_ID))
  592. fw_name = "keyspan_pda/xircom_pgs.fw";
  593. #endif
  594. else {
  595. dev_err(&serial->dev->dev, "%s: unknown vendor, aborting.\n",
  596. __func__);
  597. return -ENODEV;
  598. }
  599. if (ezusb_fx1_ihex_firmware_download(serial->dev, fw_name) < 0) {
  600. dev_err(&serial->dev->dev, "failed to load firmware \"%s\"\n",
  601. fw_name);
  602. return -ENOENT;
  603. }
  604. /* after downloading firmware Renumeration will occur in a
  605. moment and the new device will bind to the real driver */
  606. /* we want this device to fail to have a driver assigned to it. */
  607. return 1;
  608. }
  609. #ifdef KEYSPAN
  610. MODULE_FIRMWARE("keyspan_pda/keyspan_pda.fw");
  611. #endif
  612. #ifdef XIRCOM
  613. MODULE_FIRMWARE("keyspan_pda/xircom_pgs.fw");
  614. #endif
  615. static int keyspan_pda_port_probe(struct usb_serial_port *port)
  616. {
  617. struct keyspan_pda_private *priv;
  618. priv = kmalloc(sizeof(struct keyspan_pda_private), GFP_KERNEL);
  619. if (!priv)
  620. return -ENOMEM;
  621. INIT_WORK(&priv->unthrottle_work, keyspan_pda_request_unthrottle);
  622. priv->serial = port->serial;
  623. priv->port = port;
  624. usb_set_serial_port_data(port, priv);
  625. return 0;
  626. }
  627. static int keyspan_pda_port_remove(struct usb_serial_port *port)
  628. {
  629. struct keyspan_pda_private *priv;
  630. priv = usb_get_serial_port_data(port);
  631. kfree(priv);
  632. return 0;
  633. }
  634. #ifdef KEYSPAN
  635. static struct usb_serial_driver keyspan_pda_fake_device = {
  636. .driver = {
  637. .owner = THIS_MODULE,
  638. .name = "keyspan_pda_pre",
  639. },
  640. .description = "Keyspan PDA - (prerenumeration)",
  641. .id_table = id_table_fake,
  642. .num_ports = 1,
  643. .attach = keyspan_pda_fake_startup,
  644. };
  645. #endif
  646. #ifdef XIRCOM
  647. static struct usb_serial_driver xircom_pgs_fake_device = {
  648. .driver = {
  649. .owner = THIS_MODULE,
  650. .name = "xircom_no_firm",
  651. },
  652. .description = "Xircom / Entrega PGS - (prerenumeration)",
  653. .id_table = id_table_fake_xircom,
  654. .num_ports = 1,
  655. .attach = keyspan_pda_fake_startup,
  656. };
  657. #endif
  658. static struct usb_serial_driver keyspan_pda_device = {
  659. .driver = {
  660. .owner = THIS_MODULE,
  661. .name = "keyspan_pda",
  662. },
  663. .description = "Keyspan PDA",
  664. .id_table = id_table_std,
  665. .num_ports = 1,
  666. .num_bulk_out = 1,
  667. .num_interrupt_in = 1,
  668. .dtr_rts = keyspan_pda_dtr_rts,
  669. .open = keyspan_pda_open,
  670. .close = keyspan_pda_close,
  671. .write = keyspan_pda_write,
  672. .write_room = keyspan_pda_write_room,
  673. .write_bulk_callback = keyspan_pda_write_bulk_callback,
  674. .read_int_callback = keyspan_pda_rx_interrupt,
  675. .chars_in_buffer = keyspan_pda_chars_in_buffer,
  676. .throttle = keyspan_pda_rx_throttle,
  677. .unthrottle = keyspan_pda_rx_unthrottle,
  678. .set_termios = keyspan_pda_set_termios,
  679. .break_ctl = keyspan_pda_break_ctl,
  680. .tiocmget = keyspan_pda_tiocmget,
  681. .tiocmset = keyspan_pda_tiocmset,
  682. .port_probe = keyspan_pda_port_probe,
  683. .port_remove = keyspan_pda_port_remove,
  684. };
  685. static struct usb_serial_driver * const serial_drivers[] = {
  686. &keyspan_pda_device,
  687. #ifdef KEYSPAN
  688. &keyspan_pda_fake_device,
  689. #endif
  690. #ifdef XIRCOM
  691. &xircom_pgs_fake_device,
  692. #endif
  693. NULL
  694. };
  695. module_usb_serial_driver(serial_drivers, id_table_combined);
  696. MODULE_AUTHOR(DRIVER_AUTHOR);
  697. MODULE_DESCRIPTION(DRIVER_DESC);
  698. MODULE_LICENSE("GPL");