keyspan_pda.c 21 KB

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