keyspan_pda.c 25 KB

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