keyspan_pda.c 25 KB

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