keyspan_pda.c 21 KB

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