whiteheat.c 25 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/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty.h>
  23. #include <linux/tty_driver.h>
  24. #include <linux/tty_flip.h>
  25. #include <linux/module.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/mutex.h>
  28. #include <linux/uaccess.h>
  29. #include <asm/termbits.h>
  30. #include <linux/usb.h>
  31. #include <linux/serial_reg.h>
  32. #include <linux/serial.h>
  33. #include <linux/usb/serial.h>
  34. #include <linux/usb/ezusb.h>
  35. #include "whiteheat.h" /* WhiteHEAT specific commands */
  36. #ifndef CMSPAR
  37. #define CMSPAR 0
  38. #endif
  39. /*
  40. * Version Information
  41. */
  42. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
  43. #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
  44. #define CONNECT_TECH_VENDOR_ID 0x0710
  45. #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
  46. #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
  47. /*
  48. ID tables for whiteheat are unusual, because we want to different
  49. things for different versions of the device. Eventually, this
  50. will be doable from a single table. But, for now, we define two
  51. separate ID tables, and then a third table that combines them
  52. just for the purpose of exporting the autoloading information.
  53. */
  54. static const struct usb_device_id id_table_std[] = {
  55. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  56. { } /* Terminating entry */
  57. };
  58. static const struct usb_device_id id_table_prerenumeration[] = {
  59. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  60. { } /* Terminating entry */
  61. };
  62. static const struct usb_device_id id_table_combined[] = {
  63. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  64. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  65. { } /* Terminating entry */
  66. };
  67. MODULE_DEVICE_TABLE(usb, id_table_combined);
  68. /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
  69. static int whiteheat_firmware_download(struct usb_serial *serial,
  70. const struct usb_device_id *id);
  71. static int whiteheat_firmware_attach(struct usb_serial *serial);
  72. /* function prototypes for the Connect Tech WhiteHEAT serial converter */
  73. static int whiteheat_attach(struct usb_serial *serial);
  74. static void whiteheat_release(struct usb_serial *serial);
  75. static int whiteheat_port_probe(struct usb_serial_port *port);
  76. static int whiteheat_port_remove(struct usb_serial_port *port);
  77. static int whiteheat_open(struct tty_struct *tty,
  78. struct usb_serial_port *port);
  79. static void whiteheat_close(struct usb_serial_port *port);
  80. static int whiteheat_ioctl(struct tty_struct *tty,
  81. unsigned int cmd, unsigned long arg);
  82. static void whiteheat_set_termios(struct tty_struct *tty,
  83. struct usb_serial_port *port, struct ktermios *old);
  84. static int whiteheat_tiocmget(struct tty_struct *tty);
  85. static int whiteheat_tiocmset(struct tty_struct *tty,
  86. unsigned int set, unsigned int clear);
  87. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state);
  88. static struct usb_serial_driver whiteheat_fake_device = {
  89. .driver = {
  90. .owner = THIS_MODULE,
  91. .name = "whiteheatnofirm",
  92. },
  93. .description = "Connect Tech - WhiteHEAT - (prerenumeration)",
  94. .id_table = id_table_prerenumeration,
  95. .num_ports = 1,
  96. .probe = whiteheat_firmware_download,
  97. .attach = whiteheat_firmware_attach,
  98. };
  99. static struct usb_serial_driver whiteheat_device = {
  100. .driver = {
  101. .owner = THIS_MODULE,
  102. .name = "whiteheat",
  103. },
  104. .description = "Connect Tech - WhiteHEAT",
  105. .id_table = id_table_std,
  106. .num_ports = 4,
  107. .attach = whiteheat_attach,
  108. .release = whiteheat_release,
  109. .port_probe = whiteheat_port_probe,
  110. .port_remove = whiteheat_port_remove,
  111. .open = whiteheat_open,
  112. .close = whiteheat_close,
  113. .ioctl = whiteheat_ioctl,
  114. .set_termios = whiteheat_set_termios,
  115. .break_ctl = whiteheat_break_ctl,
  116. .tiocmget = whiteheat_tiocmget,
  117. .tiocmset = whiteheat_tiocmset,
  118. .throttle = usb_serial_generic_throttle,
  119. .unthrottle = usb_serial_generic_unthrottle,
  120. };
  121. static struct usb_serial_driver * const serial_drivers[] = {
  122. &whiteheat_fake_device, &whiteheat_device, NULL
  123. };
  124. struct whiteheat_command_private {
  125. struct mutex mutex;
  126. __u8 port_running;
  127. __u8 command_finished;
  128. wait_queue_head_t wait_command; /* for handling sleeping whilst
  129. waiting for a command to
  130. finish */
  131. __u8 result_buffer[64];
  132. };
  133. struct whiteheat_private {
  134. __u8 mcr; /* FIXME: no locking on mcr */
  135. };
  136. /* local function prototypes */
  137. static int start_command_port(struct usb_serial *serial);
  138. static void stop_command_port(struct usb_serial *serial);
  139. static void command_port_write_callback(struct urb *urb);
  140. static void command_port_read_callback(struct urb *urb);
  141. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  142. __u8 *data, __u8 datasize);
  143. static int firm_open(struct usb_serial_port *port);
  144. static int firm_close(struct usb_serial_port *port);
  145. static void firm_setup_port(struct tty_struct *tty);
  146. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
  147. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
  148. static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
  149. static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
  150. static int firm_get_dtr_rts(struct usb_serial_port *port);
  151. static int firm_report_tx_done(struct usb_serial_port *port);
  152. #define COMMAND_PORT 4
  153. #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
  154. #define COMMAND_TIMEOUT_MS 2000
  155. #define CLOSING_DELAY (30 * HZ)
  156. /*****************************************************************************
  157. * Connect Tech's White Heat prerenumeration driver functions
  158. *****************************************************************************/
  159. /* steps to download the firmware to the WhiteHEAT device:
  160. - hold the reset (by writing to the reset bit of the CPUCS register)
  161. - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
  162. - release the reset (by writing to the CPUCS register)
  163. - download the WH.HEX file for all addresses greater than 0x1b3f using
  164. VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
  165. - hold the reset
  166. - download the WH.HEX file for all addresses less than 0x1b40 using
  167. VENDOR_REQUEST_ANCHOR_LOAD
  168. - release the reset
  169. - device renumerated itself and comes up as new device id with all
  170. firmware download completed.
  171. */
  172. static int whiteheat_firmware_download(struct usb_serial *serial,
  173. const struct usb_device_id *id)
  174. {
  175. int response;
  176. response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat_loader.fw");
  177. if (response >= 0) {
  178. response = ezusb_fx1_ihex_firmware_download(serial->dev, "whiteheat.fw");
  179. if (response >= 0)
  180. return 0;
  181. }
  182. return -ENOENT;
  183. }
  184. static int whiteheat_firmware_attach(struct usb_serial *serial)
  185. {
  186. /* We want this device to fail to have a driver assigned to it */
  187. return 1;
  188. }
  189. /*****************************************************************************
  190. * Connect Tech's White Heat serial driver functions
  191. *****************************************************************************/
  192. static int whiteheat_attach(struct usb_serial *serial)
  193. {
  194. struct usb_serial_port *command_port;
  195. struct whiteheat_command_private *command_info;
  196. struct whiteheat_hw_info *hw_info;
  197. int pipe;
  198. int ret;
  199. int alen;
  200. __u8 *command;
  201. __u8 *result;
  202. command_port = serial->port[COMMAND_PORT];
  203. pipe = usb_sndbulkpipe(serial->dev,
  204. command_port->bulk_out_endpointAddress);
  205. command = kmalloc(2, GFP_KERNEL);
  206. if (!command)
  207. goto no_command_buffer;
  208. command[0] = WHITEHEAT_GET_HW_INFO;
  209. command[1] = 0;
  210. result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
  211. if (!result)
  212. goto no_result_buffer;
  213. /*
  214. * When the module is reloaded the firmware is still there and
  215. * the endpoints are still in the usb core unchanged. This is the
  216. * unlinking bug in disguise. Same for the call below.
  217. */
  218. usb_clear_halt(serial->dev, pipe);
  219. ret = usb_bulk_msg(serial->dev, pipe, command, 2,
  220. &alen, COMMAND_TIMEOUT_MS);
  221. if (ret) {
  222. dev_err(&serial->dev->dev, "%s: Couldn't send command [%d]\n",
  223. serial->type->description, ret);
  224. goto no_firmware;
  225. } else if (alen != 2) {
  226. dev_err(&serial->dev->dev, "%s: Send command incomplete [%d]\n",
  227. serial->type->description, alen);
  228. goto no_firmware;
  229. }
  230. pipe = usb_rcvbulkpipe(serial->dev,
  231. command_port->bulk_in_endpointAddress);
  232. /* See the comment on the usb_clear_halt() above */
  233. usb_clear_halt(serial->dev, pipe);
  234. ret = usb_bulk_msg(serial->dev, pipe, result,
  235. sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
  236. if (ret) {
  237. dev_err(&serial->dev->dev, "%s: Couldn't get results [%d]\n",
  238. serial->type->description, ret);
  239. goto no_firmware;
  240. } else if (alen != sizeof(*hw_info) + 1) {
  241. dev_err(&serial->dev->dev, "%s: Get results incomplete [%d]\n",
  242. serial->type->description, alen);
  243. goto no_firmware;
  244. } else if (result[0] != command[0]) {
  245. dev_err(&serial->dev->dev, "%s: Command failed [%d]\n",
  246. serial->type->description, result[0]);
  247. goto no_firmware;
  248. }
  249. hw_info = (struct whiteheat_hw_info *)&result[1];
  250. dev_info(&serial->dev->dev, "%s: Firmware v%d.%02d\n",
  251. serial->type->description,
  252. hw_info->sw_major_rev, hw_info->sw_minor_rev);
  253. command_info = kmalloc(sizeof(struct whiteheat_command_private),
  254. GFP_KERNEL);
  255. if (command_info == NULL) {
  256. dev_err(&serial->dev->dev,
  257. "%s: Out of memory for port structures\n",
  258. serial->type->description);
  259. goto no_command_private;
  260. }
  261. mutex_init(&command_info->mutex);
  262. command_info->port_running = 0;
  263. init_waitqueue_head(&command_info->wait_command);
  264. usb_set_serial_port_data(command_port, command_info);
  265. command_port->write_urb->complete = command_port_write_callback;
  266. command_port->read_urb->complete = command_port_read_callback;
  267. kfree(result);
  268. kfree(command);
  269. return 0;
  270. no_firmware:
  271. /* Firmware likely not running */
  272. dev_err(&serial->dev->dev,
  273. "%s: Unable to retrieve firmware version, try replugging\n",
  274. serial->type->description);
  275. dev_err(&serial->dev->dev,
  276. "%s: If the firmware is not running (status led not blinking)\n",
  277. serial->type->description);
  278. dev_err(&serial->dev->dev,
  279. "%s: please contact support@connecttech.com\n",
  280. serial->type->description);
  281. kfree(result);
  282. kfree(command);
  283. return -ENODEV;
  284. no_command_private:
  285. kfree(result);
  286. no_result_buffer:
  287. kfree(command);
  288. no_command_buffer:
  289. return -ENOMEM;
  290. }
  291. static void whiteheat_release(struct usb_serial *serial)
  292. {
  293. struct usb_serial_port *command_port;
  294. /* free up our private data for our command port */
  295. command_port = serial->port[COMMAND_PORT];
  296. kfree(usb_get_serial_port_data(command_port));
  297. }
  298. static int whiteheat_port_probe(struct usb_serial_port *port)
  299. {
  300. struct whiteheat_private *info;
  301. info = kzalloc(sizeof(*info), GFP_KERNEL);
  302. if (!info)
  303. return -ENOMEM;
  304. usb_set_serial_port_data(port, info);
  305. return 0;
  306. }
  307. static int whiteheat_port_remove(struct usb_serial_port *port)
  308. {
  309. struct whiteheat_private *info;
  310. info = usb_get_serial_port_data(port);
  311. kfree(info);
  312. return 0;
  313. }
  314. static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
  315. {
  316. int retval;
  317. retval = start_command_port(port->serial);
  318. if (retval)
  319. goto exit;
  320. /* send an open port command */
  321. retval = firm_open(port);
  322. if (retval) {
  323. stop_command_port(port->serial);
  324. goto exit;
  325. }
  326. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  327. if (retval) {
  328. firm_close(port);
  329. stop_command_port(port->serial);
  330. goto exit;
  331. }
  332. if (tty)
  333. firm_setup_port(tty);
  334. /* Work around HCD bugs */
  335. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  336. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  337. retval = usb_serial_generic_open(tty, port);
  338. if (retval) {
  339. firm_close(port);
  340. stop_command_port(port->serial);
  341. goto exit;
  342. }
  343. exit:
  344. return retval;
  345. }
  346. static void whiteheat_close(struct usb_serial_port *port)
  347. {
  348. firm_report_tx_done(port);
  349. firm_close(port);
  350. usb_serial_generic_close(port);
  351. stop_command_port(port->serial);
  352. }
  353. static int whiteheat_tiocmget(struct tty_struct *tty)
  354. {
  355. struct usb_serial_port *port = tty->driver_data;
  356. struct whiteheat_private *info = usb_get_serial_port_data(port);
  357. unsigned int modem_signals = 0;
  358. firm_get_dtr_rts(port);
  359. if (info->mcr & UART_MCR_DTR)
  360. modem_signals |= TIOCM_DTR;
  361. if (info->mcr & UART_MCR_RTS)
  362. modem_signals |= TIOCM_RTS;
  363. return modem_signals;
  364. }
  365. static int whiteheat_tiocmset(struct tty_struct *tty,
  366. unsigned int set, unsigned int clear)
  367. {
  368. struct usb_serial_port *port = tty->driver_data;
  369. struct whiteheat_private *info = usb_get_serial_port_data(port);
  370. if (set & TIOCM_RTS)
  371. info->mcr |= UART_MCR_RTS;
  372. if (set & TIOCM_DTR)
  373. info->mcr |= UART_MCR_DTR;
  374. if (clear & TIOCM_RTS)
  375. info->mcr &= ~UART_MCR_RTS;
  376. if (clear & TIOCM_DTR)
  377. info->mcr &= ~UART_MCR_DTR;
  378. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  379. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  380. return 0;
  381. }
  382. static int whiteheat_ioctl(struct tty_struct *tty,
  383. unsigned int cmd, unsigned long arg)
  384. {
  385. struct usb_serial_port *port = tty->driver_data;
  386. struct serial_struct serstruct;
  387. void __user *user_arg = (void __user *)arg;
  388. dev_dbg(&port->dev, "%s - cmd 0x%.4x\n", __func__, cmd);
  389. switch (cmd) {
  390. case TIOCGSERIAL:
  391. memset(&serstruct, 0, sizeof(serstruct));
  392. serstruct.type = PORT_16654;
  393. serstruct.line = port->serial->minor;
  394. serstruct.port = port->number;
  395. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  396. serstruct.xmit_fifo_size = kfifo_size(&port->write_fifo);
  397. serstruct.custom_divisor = 0;
  398. serstruct.baud_base = 460800;
  399. serstruct.close_delay = CLOSING_DELAY;
  400. serstruct.closing_wait = CLOSING_DELAY;
  401. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  402. return -EFAULT;
  403. break;
  404. default:
  405. break;
  406. }
  407. return -ENOIOCTLCMD;
  408. }
  409. static void whiteheat_set_termios(struct tty_struct *tty,
  410. struct usb_serial_port *port, struct ktermios *old_termios)
  411. {
  412. firm_setup_port(tty);
  413. }
  414. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
  415. {
  416. struct usb_serial_port *port = tty->driver_data;
  417. firm_set_break(port, break_state);
  418. }
  419. /*****************************************************************************
  420. * Connect Tech's White Heat callback routines
  421. *****************************************************************************/
  422. static void command_port_write_callback(struct urb *urb)
  423. {
  424. int status = urb->status;
  425. if (status) {
  426. dev_dbg(&urb->dev->dev, "nonzero urb status: %d\n", status);
  427. return;
  428. }
  429. }
  430. static void command_port_read_callback(struct urb *urb)
  431. {
  432. struct usb_serial_port *command_port = urb->context;
  433. struct whiteheat_command_private *command_info;
  434. int status = urb->status;
  435. unsigned char *data = urb->transfer_buffer;
  436. int result;
  437. command_info = usb_get_serial_port_data(command_port);
  438. if (!command_info) {
  439. dev_dbg(&urb->dev->dev, "%s - command_info is NULL, exiting.\n", __func__);
  440. return;
  441. }
  442. if (status) {
  443. dev_dbg(&urb->dev->dev, "%s - nonzero urb status: %d\n", __func__, status);
  444. if (status != -ENOENT)
  445. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  446. wake_up(&command_info->wait_command);
  447. return;
  448. }
  449. usb_serial_debug_data(&command_port->dev, __func__, urb->actual_length, data);
  450. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  451. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  452. wake_up(&command_info->wait_command);
  453. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  454. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  455. wake_up(&command_info->wait_command);
  456. } else if (data[0] == WHITEHEAT_EVENT) {
  457. /* These are unsolicited reports from the firmware, hence no
  458. waiting command to wakeup */
  459. dev_dbg(&urb->dev->dev, "%s - event received\n", __func__);
  460. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  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->number - port->serial->minor + 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->number - port->serial->minor + 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->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->number - port->serial->minor + 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->number - port->serial->minor + 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->number - port->serial->minor + 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->number - port->serial->minor + 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->number - port->serial->minor + 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->number - port->serial->minor + 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");