whiteheat.c 25 KB

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