keyspan_pda.c 22 KB

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