keyspan_pda.c 25 KB

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