keyspan_pda.c 25 KB

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