whiteheat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. /*
  2. * USB ConnectTech WhiteHEAT driver
  3. *
  4. * Copyright (C) 2002
  5. * Connect Tech Inc.
  6. *
  7. * Copyright (C) 1999 - 2001
  8. * Greg Kroah-Hartman (greg@kroah.com)
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * See Documentation/usb/usb-serial.txt for more information on using this
  16. * driver
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/slab.h>
  21. #include <linux/tty.h>
  22. #include <linux/tty_driver.h>
  23. #include <linux/tty_flip.h>
  24. #include <linux/module.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/mutex.h>
  27. #include <linux/uaccess.h>
  28. #include <asm/termbits.h>
  29. #include <linux/usb.h>
  30. #include <linux/serial_reg.h>
  31. #include <linux/serial.h>
  32. #include <linux/usb/serial.h>
  33. #include <linux/usb/ezusb.h>
  34. #include "whiteheat.h" /* WhiteHEAT specific commands */
  35. #ifndef CMSPAR
  36. #define CMSPAR 0
  37. #endif
  38. /*
  39. * Version Information
  40. */
  41. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
  42. #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
  43. #define CONNECT_TECH_VENDOR_ID 0x0710
  44. #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
  45. #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
  46. /*
  47. ID tables for whiteheat are unusual, because we want to different
  48. things for different versions of the device. Eventually, this
  49. will be doable from a single table. But, for now, we define two
  50. separate ID tables, and then a third table that combines them
  51. just for the purpose of exporting the autoloading information.
  52. */
  53. static const struct usb_device_id id_table_std[] = {
  54. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  55. { } /* Terminating entry */
  56. };
  57. static const struct usb_device_id id_table_prerenumeration[] = {
  58. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  59. { } /* Terminating entry */
  60. };
  61. static const struct usb_device_id id_table_combined[] = {
  62. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  63. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  64. { } /* Terminating entry */
  65. };
  66. MODULE_DEVICE_TABLE(usb, id_table_combined);
  67. /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
  68. static int whiteheat_firmware_download(struct usb_serial *serial,
  69. const struct usb_device_id *id);
  70. static int whiteheat_firmware_attach(struct usb_serial *serial);
  71. /* function prototypes for the Connect Tech WhiteHEAT serial converter */
  72. static int whiteheat_attach(struct usb_serial *serial);
  73. static void whiteheat_release(struct usb_serial *serial);
  74. static int whiteheat_port_probe(struct usb_serial_port *port);
  75. static int whiteheat_port_remove(struct usb_serial_port *port);
  76. static int whiteheat_open(struct tty_struct *tty,
  77. struct usb_serial_port *port);
  78. static void whiteheat_close(struct usb_serial_port *port);
  79. static int whiteheat_ioctl(struct tty_struct *tty,
  80. unsigned int cmd, unsigned long arg);
  81. static void whiteheat_set_termios(struct tty_struct *tty,
  82. struct usb_serial_port *port, struct ktermios *old);
  83. static int whiteheat_tiocmget(struct tty_struct *tty);
  84. static int whiteheat_tiocmset(struct tty_struct *tty,
  85. unsigned int set, unsigned int clear);
  86. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
  87. static struct usb_serial_driver whiteheat_fake_device = {
  88. .driver = {
  89. .owner = THIS_MODULE,
  90. .name = "whiteheatnofirm",
  91. },
  92. .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
  93. .id_table = id_table_prerenumeration,
  94. .num_ports = 1,
  95. .probe = whiteheat_firmware_download,
  96. .attach = whiteheat_firmware_attach,
  97. };
  98. static struct usb_serial_driver whiteheat_device = {
  99. .driver = {
  100. .owner = THIS_MODULE,
  101. .name = "whiteheat",
  102. },
  103. .description = "Connect Tech - WhiteHEAT",
  104. .id_table = id_table_std,
  105. .num_ports = 4,
  106. .num_bulk_in = 5,
  107. .num_bulk_out = 5,
  108. .attach = whiteheat_attach,
  109. .release = whiteheat_release,
  110. .port_probe = whiteheat_port_probe,
  111. .port_remove = whiteheat_port_remove,
  112. .open = whiteheat_open,
  113. .close = whiteheat_close,
  114. .ioctl = whiteheat_ioctl,
  115. .set_termios = whiteheat_set_termios,
  116. .break_ctl = whiteheat_break_ctl,
  117. .tiocmget = whiteheat_tiocmget,
  118. .tiocmset = whiteheat_tiocmset,
  119. .throttle = usb_serial_generic_throttle,
  120. .unthrottle = usb_serial_generic_unthrottle,
  121. };
  122. static struct usb_serial_driver * const serial_drivers[] = {
  123. &whiteheat_fake_device, &whiteheat_device, NULL
  124. };
  125. struct whiteheat_command_private {
  126. struct mutex mutex;
  127. __u8 port_running;
  128. __u8 command_finished;
  129. wait_queue_head_t wait_command; /* for handling sleeping whilst
  130. waiting for a command to
  131. finish */
  132. __u8 result_buffer[64];
  133. };
  134. struct whiteheat_private {
  135. __u8 mcr; /* FIXME: no locking on mcr */
  136. };
  137. /* local function prototypes */
  138. static int start_command_port(struct usb_serial *serial);
  139. static void stop_command_port(struct usb_serial *serial);
  140. static void command_port_write_callback(struct urb *urb);
  141. static void command_port_read_callback(struct urb *urb);
  142. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  143. __u8 *data, __u8 datasize);
  144. static int firm_open(struct usb_serial_port *port);
  145. static int firm_close(struct usb_serial_port *port);
  146. static void firm_setup_port(struct tty_struct *tty);
  147. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
  148. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
  149. static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
  150. static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
  151. static int firm_get_dtr_rts(struct usb_serial_port *port);
  152. static int firm_report_tx_done(struct usb_serial_port *port);
  153. #define COMMAND_PORT 4
  154. #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
  155. #define COMMAND_TIMEOUT_MS 2000
  156. #define CLOSING_DELAY (30 * HZ)
  157. /*****************************************************************************
  158. * Connect Tech's White Heat prerenumeration driver functions
  159. *****************************************************************************/
  160. /* steps to download the firmware to the WhiteHEAT device:
  161. - hold the reset (by writing to the reset bit of the CPUCS register)
  162. - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
  163. - release the reset (by writing to the CPUCS register)
  164. - download the WH.HEX file for all addresses greater than 0x1b3f using
  165. VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
  166. - hold the reset
  167. - download the WH.HEX file for all addresses less than 0x1b40 using
  168. VENDOR_REQUEST_ANCHOR_LOAD
  169. - release the reset
  170. - device renumerated itself and comes up as new device id with all
  171. firmware download completed.
  172. */
  173. static int whiteheat_firmware_download(struct usb_serial *serial,
  174. const struct usb_device_id *id)
  175. {
  176. int response;
  177. response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat_loader.fw");
  178. if (response >= 0) {
  179. response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat.fw");
  180. if (response >= 0)
  181. return 0;
  182. }
  183. return -ENOENT;
  184. }
  185. static int whiteheat_firmware_attach(struct usb_serial *serial)
  186. {
  187. /* We want this device to fail to have a driver assigned to it */
  188. return 1;
  189. }
  190. /*****************************************************************************
  191. * Connect Tech's White Heat serial driver functions
  192. *****************************************************************************/
  193. static int whiteheat_attach(struct usb_serial *serial)
  194. {
  195. struct usb_serial_port *command_port;
  196. struct whiteheat_command_private *command_info;
  197. struct whiteheat_hw_info *hw_info;
  198. int pipe;
  199. int ret;
  200. int alen;
  201. __u8 *command;
  202. __u8 *result;
  203. command_port = serial->port[COMMAND_PORT];
  204. pipe = usb_sndbulkpipe(serial->dev,
  205. command_port->bulk_out_endpointAddress);
  206. command = kmalloc(2, GFP_KERNEL);
  207. if (!command)
  208. goto no_command_buffer;
  209. command[0] = WHITEHEAT_GET_HW_INFO;
  210. command[1] = 0;
  211. result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
  212. if (!result)
  213. goto no_result_buffer;
  214. /*
  215. * When the module is reloaded the firmware is still there and
  216. * the endpoints are still in the usb core unchanged. This is the
  217. * unlinking bug in disguise. Same for the call below.
  218. */
  219. usb_clear_halt(serial->dev, pipe);
  220. ret = usb_bulk_msg(serial->dev, pipe, command, 2,
  221. &alen, COMMAND_TIMEOUT_MS);
  222. if (ret) {
  223. dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
  224. serial->type->description, ret);
  225. goto no_firmware;
  226. } else if (alen != 2) {
  227. dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
  228. serial->type->description, alen);
  229. goto no_firmware;
  230. }
  231. pipe = usb_rcvbulkpipe(serial->dev,
  232. command_port->bulk_in_endpointAddress);
  233. /* See the comment on the usb_clear_halt() above */
  234. usb_clear_halt(serial->dev, pipe);
  235. ret = usb_bulk_msg(serial->dev, pipe, result,
  236. sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
  237. if (ret) {
  238. dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
  239. serial->type->description, ret);
  240. goto no_firmware;
  241. } else if (alen != sizeof(*hw_info) + 1) {
  242. dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
  243. serial->type->description, alen);
  244. goto no_firmware;
  245. } else if (result[0] != command[0]) {
  246. dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
  247. serial->type->description, result[0]);
  248. goto no_firmware;
  249. }
  250. hw_info = (struct whiteheat_hw_info *)&result[1];
  251. dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
  252. serial->type->description,
  253. hw_info->sw_major_rev, hw_info->sw_minor_rev);
  254. command_info = kmalloc(sizeof(struct whiteheat_command_private),
  255. GFP_KERNEL);
  256. if (!command_info)
  257. goto no_command_private;
  258. mutex_init(&command_info->mutex);
  259. command_info->port_running = 0;
  260. init_waitqueue_head(&command_info->wait_command);
  261. usb_set_serial_port_data(command_port, command_info);
  262. command_port->write_urb->complete = command_port_write_callback;
  263. command_port->read_urb->complete = command_port_read_callback;
  264. kfree(result);
  265. kfree(command);
  266. return 0;
  267. no_firmware:
  268. /* Firmware likely not running */
  269. dev_err(&serial->dev->dev,
  270. "%s: Unable to retrieve firmware version, try replugging\n",
  271. serial->type->description);
  272. dev_err(&serial->dev->dev,
  273. "%s: If the firmware is not running (status led not blinking)\n",
  274. serial->type->description);
  275. dev_err(&serial->dev->dev,
  276. "%s: please contact support@connecttech.com\n",
  277. serial->type->description);
  278. kfree(result);
  279. kfree(command);
  280. return -ENODEV;
  281. no_command_private:
  282. kfree(result);
  283. no_result_buffer:
  284. kfree(command);
  285. no_command_buffer:
  286. return -ENOMEM;
  287. }
  288. static void whiteheat_release(struct usb_serial *serial)
  289. {
  290. struct usb_serial_port *command_port;
  291. /* free up our private data for our command port */
  292. command_port = serial->port[COMMAND_PORT];
  293. kfree(usb_get_serial_port_data(command_port));
  294. }
  295. static int whiteheat_port_probe(struct usb_serial_port *port)
  296. {
  297. struct whiteheat_private *info;
  298. info = kzalloc(sizeof(*info), GFP_KERNEL);
  299. if (!info)
  300. return -ENOMEM;
  301. usb_set_serial_port_data(port, info);
  302. return 0;
  303. }
  304. static int whiteheat_port_remove(struct usb_serial_port *port)
  305. {
  306. struct whiteheat_private *info;
  307. info = usb_get_serial_port_data(port);
  308. kfree(info);
  309. return 0;
  310. }
  311. static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
  312. {
  313. int retval;
  314. retval = start_command_port(port->serial);
  315. if (retval)
  316. goto exit;
  317. /* send an open port command */
  318. retval = firm_open(port);
  319. if (retval) {
  320. stop_command_port(port->serial);
  321. goto exit;
  322. }
  323. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  324. if (retval) {
  325. firm_close(port);
  326. stop_command_port(port->serial);
  327. goto exit;
  328. }
  329. if (tty)
  330. firm_setup_port(tty);
  331. /* Work around HCD bugs */
  332. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  333. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  334. retval = usb_serial_generic_open(tty, port);
  335. if (retval) {
  336. firm_close(port);
  337. stop_command_port(port->serial);
  338. goto exit;
  339. }
  340. exit:
  341. return retval;
  342. }
  343. static void whiteheat_close(struct usb_serial_port *port)
  344. {
  345. firm_report_tx_done(port);
  346. firm_close(port);
  347. usb_serial_generic_close(port);
  348. stop_command_port(port->serial);
  349. }
  350. static int whiteheat_tiocmget(struct tty_struct *tty)
  351. {
  352. struct usb_serial_port *port = tty->driver_data;
  353. struct whiteheat_private *info = usb_get_serial_port_data(port);
  354. unsigned int modem_signals = 0;
  355. firm_get_dtr_rts(port);
  356. if (info->mcr & UART_MCR_DTR)
  357. modem_signals |= TIOCM_DTR;
  358. if (info->mcr & UART_MCR_RTS)
  359. modem_signals |= TIOCM_RTS;
  360. return modem_signals;
  361. }
  362. static int whiteheat_tiocmset(struct tty_struct *tty,
  363. unsigned int set, unsigned int clear)
  364. {
  365. struct usb_serial_port *port = tty->driver_data;
  366. struct whiteheat_private *info = usb_get_serial_port_data(port);
  367. if (set & TIOCM_RTS)
  368. info->mcr |= UART_MCR_RTS;
  369. if (set & TIOCM_DTR)
  370. info->mcr |= UART_MCR_DTR;
  371. if (clear & TIOCM_RTS)
  372. info->mcr &= ~UART_MCR_RTS;
  373. if (clear & TIOCM_DTR)
  374. info->mcr &= ~UART_MCR_DTR;
  375. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  376. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  377. return 0;
  378. }
  379. static int whiteheat_ioctl(struct tty_struct *tty,
  380. unsigned int cmd, unsigned long arg)
  381. {
  382. struct usb_serial_port *port = tty->driver_data;
  383. struct serial_struct serstruct;
  384. void __user *user_arg = (void __user *)arg;
  385. switch (cmd) {
  386. case TIOCGSERIAL:
  387. memset(&serstruct, 0, sizeof(serstruct));
  388. serstruct.type = PORT_16654;
  389. serstruct.line = port->minor;
  390. serstruct.port = port->port_number;
  391. serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
  392. serstruct.custom_divisor = 0;
  393. serstruct.baud_base = 460800;
  394. serstruct.close_delay = CLOSING_DELAY;
  395. serstruct.closing_wait = CLOSING_DELAY;
  396. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  397. return -EFAULT;
  398. break;
  399. default:
  400. break;
  401. }
  402. return -ENOIOCTLCMD;
  403. }
  404. static void whiteheat_set_termios(struct tty_struct *tty,
  405. struct usb_serial_port *port, struct ktermios *old_termios)
  406. {
  407. firm_setup_port(tty);
  408. }
  409. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
  410. {
  411. struct usb_serial_port *port = tty->driver_data;
  412. firm_set_break(port, break_state);
  413. }
  414. /*****************************************************************************
  415. * Connect Tech's White Heat callback routines
  416. *****************************************************************************/
  417. static void command_port_write_callback(struct urb *urb)
  418. {
  419. int status = urb->status;
  420. if (status) {
  421. dev_dbg(&urb->dev->dev, "nonzero urb status: %d\n", status);
  422. return;
  423. }
  424. }
  425. static void command_port_read_callback(struct urb *urb)
  426. {
  427. struct usb_serial_port *command_port = urb->context;
  428. struct whiteheat_command_private *command_info;
  429. int status = urb->status;
  430. unsigned char *data = urb->transfer_buffer;
  431. int result;
  432. command_info = usb_get_serial_port_data(command_port);
  433. if (!command_info) {
  434. dev_dbg(&urb->dev->dev, "%s - command_info is NULL, exiting.\n", __func__);
  435. return;
  436. }
  437. if (!urb->actual_length) {
  438. dev_dbg(&urb->dev->dev, "%s - empty response, exiting.\n", __func__);
  439. return;
  440. }
  441. if (status) {
  442. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", __func__, status);
  443. if (status != -ENOENT)
  444. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  445. wake_up(&command_info->wait_command);
  446. return;
  447. }
  448. usb_serial_debug_data(&command_port->dev, __func__, urb->actual_length, data);
  449. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  450. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  451. wake_up(&command_info->wait_command);
  452. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  453. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  454. wake_up(&command_info->wait_command);
  455. } else if (data[0] == WHITEHEAT_EVENT) {
  456. /* These are unsolicited reports from the firmware, hence no
  457. waiting command to wakeup */
  458. dev_dbg(&urb->dev->dev, "%s - event received\n", __func__);
  459. } else if ((data[0] == WHITEHEAT_GET_DTR_RTS) &&
  460. (urb->actual_length - 1 <= sizeof(command_info->result_buffer))) {
  461. memcpy(command_info->result_buffer, &data[1],
  462. urb->actual_length - 1);
  463. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  464. wake_up(&command_info->wait_command);
  465. } else
  466. dev_dbg(&urb->dev->dev, "%s - bad reply from firmware\n", __func__);
  467. /* Continue trying to always read */
  468. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  469. if (result)
  470. dev_dbg(&urb->dev->dev, "%s - failed resubmitting read urb, error %d\n",
  471. __func__, result);
  472. }
  473. /*****************************************************************************
  474. * Connect Tech's White Heat firmware interface
  475. *****************************************************************************/
  476. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  477. __u8 *data, __u8 datasize)
  478. {
  479. struct usb_serial_port *command_port;
  480. struct whiteheat_command_private *command_info;
  481. struct whiteheat_private *info;
  482. struct device *dev = &port->dev;
  483. __u8 *transfer_buffer;
  484. int retval = 0;
  485. int t;
  486. dev_dbg(dev, "%s - command %d\n", __func__, command);
  487. command_port = port->serial->port[COMMAND_PORT];
  488. command_info = usb_get_serial_port_data(command_port);
  489. mutex_lock(&command_info->mutex);
  490. command_info->command_finished = false;
  491. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  492. transfer_buffer[0] = command;
  493. memcpy(&transfer_buffer[1], data, datasize);
  494. command_port->write_urb->transfer_buffer_length = datasize + 1;
  495. retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
  496. if (retval) {
  497. dev_dbg(dev, "%s - submit urb failed\n", __func__);
  498. goto exit;
  499. }
  500. /* wait for the command to complete */
  501. t = wait_event_timeout(command_info->wait_command,
  502. (bool)command_info->command_finished, COMMAND_TIMEOUT);
  503. if (!t)
  504. usb_kill_urb(command_port->write_urb);
  505. if (command_info->command_finished == false) {
  506. dev_dbg(dev, "%s - command timed out.\n", __func__);
  507. retval = -ETIMEDOUT;
  508. goto exit;
  509. }
  510. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  511. dev_dbg(dev, "%s - command failed.\n", __func__);
  512. retval = -EIO;
  513. goto exit;
  514. }
  515. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  516. dev_dbg(dev, "%s - command completed.\n", __func__);
  517. switch (command) {
  518. case WHITEHEAT_GET_DTR_RTS:
  519. info = usb_get_serial_port_data(port);
  520. memcpy(&info->mcr, command_info->result_buffer,
  521. sizeof(struct whiteheat_dr_info));
  522. break;
  523. }
  524. }
  525. exit:
  526. mutex_unlock(&command_info->mutex);
  527. return retval;
  528. }
  529. static int firm_open(struct usb_serial_port *port)
  530. {
  531. struct whiteheat_simple open_command;
  532. open_command.port = port->port_number + 1;
  533. return firm_send_command(port, WHITEHEAT_OPEN,
  534. (__u8 *)&open_command, sizeof(open_command));
  535. }
  536. static int firm_close(struct usb_serial_port *port)
  537. {
  538. struct whiteheat_simple close_command;
  539. close_command.port = port->port_number + 1;
  540. return firm_send_command(port, WHITEHEAT_CLOSE,
  541. (__u8 *)&close_command, sizeof(close_command));
  542. }
  543. static void firm_setup_port(struct tty_struct *tty)
  544. {
  545. struct usb_serial_port *port = tty->driver_data;
  546. struct device *dev = &port->dev;
  547. struct whiteheat_port_settings port_settings;
  548. unsigned int cflag = tty->termios.c_cflag;
  549. port_settings.port = port->port_number + 1;
  550. /* get the byte size */
  551. switch (cflag & CSIZE) {
  552. case CS5: port_settings.bits = 5; break;
  553. case CS6: port_settings.bits = 6; break;
  554. case CS7: port_settings.bits = 7; break;
  555. default:
  556. case CS8: port_settings.bits = 8; break;
  557. }
  558. dev_dbg(dev, "%s - data bits = %d\n", __func__, port_settings.bits);
  559. /* determine the parity */
  560. if (cflag & PARENB)
  561. if (cflag & CMSPAR)
  562. if (cflag & PARODD)
  563. port_settings.parity = WHITEHEAT_PAR_MARK;
  564. else
  565. port_settings.parity = WHITEHEAT_PAR_SPACE;
  566. else
  567. if (cflag & PARODD)
  568. port_settings.parity = WHITEHEAT_PAR_ODD;
  569. else
  570. port_settings.parity = WHITEHEAT_PAR_EVEN;
  571. else
  572. port_settings.parity = WHITEHEAT_PAR_NONE;
  573. dev_dbg(dev, "%s - parity = %c\n", __func__, port_settings.parity);
  574. /* figure out the stop bits requested */
  575. if (cflag & CSTOPB)
  576. port_settings.stop = 2;
  577. else
  578. port_settings.stop = 1;
  579. dev_dbg(dev, "%s - stop bits = %d\n", __func__, port_settings.stop);
  580. /* figure out the flow control settings */
  581. if (cflag & CRTSCTS)
  582. port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
  583. WHITEHEAT_HFLOW_RTS);
  584. else
  585. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  586. dev_dbg(dev, "%s - hardware flow control = %s %s %s %s\n", __func__,
  587. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  588. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  589. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  590. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  591. /* determine software flow control */
  592. if (I_IXOFF(tty))
  593. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  594. else
  595. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  596. dev_dbg(dev, "%s - software flow control = %c\n", __func__, port_settings.sflow);
  597. port_settings.xon = START_CHAR(tty);
  598. port_settings.xoff = STOP_CHAR(tty);
  599. dev_dbg(dev, "%s - XON = %2x, XOFF = %2x\n", __func__, port_settings.xon, port_settings.xoff);
  600. /* get the baud rate wanted */
  601. port_settings.baud = tty_get_baud_rate(tty);
  602. dev_dbg(dev, "%s - baud rate = %d\n", __func__, port_settings.baud);
  603. /* fixme: should set validated settings */
  604. tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
  605. /* handle any settings that aren't specified in the tty structure */
  606. port_settings.lloop = 0;
  607. /* now send the message to the device */
  608. firm_send_command(port, WHITEHEAT_SETUP_PORT,
  609. (__u8 *)&port_settings, sizeof(port_settings));
  610. }
  611. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
  612. {
  613. struct whiteheat_set_rdb rts_command;
  614. rts_command.port = port->port_number + 1;
  615. rts_command.state = onoff;
  616. return firm_send_command(port, WHITEHEAT_SET_RTS,
  617. (__u8 *)&rts_command, sizeof(rts_command));
  618. }
  619. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
  620. {
  621. struct whiteheat_set_rdb dtr_command;
  622. dtr_command.port = port->port_number + 1;
  623. dtr_command.state = onoff;
  624. return firm_send_command(port, WHITEHEAT_SET_DTR,
  625. (__u8 *)&dtr_command, sizeof(dtr_command));
  626. }
  627. static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
  628. {
  629. struct whiteheat_set_rdb break_command;
  630. break_command.port = port->port_number + 1;
  631. break_command.state = onoff;
  632. return firm_send_command(port, WHITEHEAT_SET_BREAK,
  633. (__u8 *)&break_command, sizeof(break_command));
  634. }
  635. static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
  636. {
  637. struct whiteheat_purge purge_command;
  638. purge_command.port = port->port_number + 1;
  639. purge_command.what = rxtx;
  640. return firm_send_command(port, WHITEHEAT_PURGE,
  641. (__u8 *)&purge_command, sizeof(purge_command));
  642. }
  643. static int firm_get_dtr_rts(struct usb_serial_port *port)
  644. {
  645. struct whiteheat_simple get_dr_command;
  646. get_dr_command.port = port->port_number + 1;
  647. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
  648. (__u8 *)&get_dr_command, sizeof(get_dr_command));
  649. }
  650. static int firm_report_tx_done(struct usb_serial_port *port)
  651. {
  652. struct whiteheat_simple close_command;
  653. close_command.port = port->port_number + 1;
  654. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
  655. (__u8 *)&close_command, sizeof(close_command));
  656. }
  657. /*****************************************************************************
  658. * Connect Tech's White Heat utility functions
  659. *****************************************************************************/
  660. static int start_command_port(struct usb_serial *serial)
  661. {
  662. struct usb_serial_port *command_port;
  663. struct whiteheat_command_private *command_info;
  664. int retval = 0;
  665. command_port = serial->port[COMMAND_PORT];
  666. command_info = usb_get_serial_port_data(command_port);
  667. mutex_lock(&command_info->mutex);
  668. if (!command_info->port_running) {
  669. /* Work around HCD bugs */
  670. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  671. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  672. if (retval) {
  673. dev_err(&serial->dev->dev,
  674. "%s - failed submitting read urb, error %d\n",
  675. __func__, retval);
  676. goto exit;
  677. }
  678. }
  679. command_info->port_running++;
  680. exit:
  681. mutex_unlock(&command_info->mutex);
  682. return retval;
  683. }
  684. static void stop_command_port(struct usb_serial *serial)
  685. {
  686. struct usb_serial_port *command_port;
  687. struct whiteheat_command_private *command_info;
  688. command_port = serial->port[COMMAND_PORT];
  689. command_info = usb_get_serial_port_data(command_port);
  690. mutex_lock(&command_info->mutex);
  691. command_info->port_running--;
  692. if (!command_info->port_running)
  693. usb_kill_urb(command_port->read_urb);
  694. mutex_unlock(&command_info->mutex);
  695. }
  696. module_usb_serial_driver(serial_drivers, id_table_combined);
  697. MODULE_AUTHOR(DRIVER_AUTHOR);
  698. MODULE_DESCRIPTION(DRIVER_DESC);
  699. MODULE_LICENSE("GPL");
  700. MODULE_FIRMWARE("whiteheat.fw");
  701. MODULE_FIRMWARE("whiteheat_loader.fw");