whiteheat.c 27 KB

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