keyspan_pda.c 22 KB

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