whiteheat.c 24 KB

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