whiteheat.c 25 KB

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