whiteheat.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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 driver
  16. *
  17. * (10/09/2002) Stuart MacDonald (stuartm@connecttech.com)
  18. * Upgrade to full working driver
  19. *
  20. * (05/30/2001) gkh
  21. * switched from using spinlock to a semaphore, which fixes lots of problems.
  22. *
  23. * (04/08/2001) gb
  24. * Identify version on module load.
  25. *
  26. * 2001_Mar_19 gkh
  27. * Fixed MOD_INC and MOD_DEC logic, the ability to open a port more
  28. * than once, and the got the proper usb_device_id table entries so
  29. * the driver works again.
  30. *
  31. * (11/01/2000) Adam J. Richter
  32. * usb_device_id table support
  33. *
  34. * (10/05/2000) gkh
  35. * Fixed bug with urb->dev not being set properly, now that the usb
  36. * core needs it.
  37. *
  38. * (10/03/2000) smd
  39. * firmware is improved to guard against crap sent to device
  40. * firmware now replies CMD_FAILURE on bad things
  41. * read_callback fix you provided for private info struct
  42. * command_finished now indicates success or fail
  43. * setup_port struct now packed to avoid gcc padding
  44. * firmware uses 1 based port numbering, driver now handles that
  45. *
  46. * (09/11/2000) gkh
  47. * Removed DEBUG #ifdefs with call to usb_serial_debug_data
  48. *
  49. * (07/19/2000) gkh
  50. * Added module_init and module_exit functions to handle the fact that this
  51. * driver is a loadable module now.
  52. * Fixed bug with port->minor that was found by Al Borchers
  53. *
  54. * (07/04/2000) gkh
  55. * Added support for port settings. Baud rate can now be changed. Line signals
  56. * are not transferred to and from the tty layer yet, but things seem to be
  57. * working well now.
  58. *
  59. * (05/04/2000) gkh
  60. * First cut at open and close commands. Data can flow through the ports at
  61. * default speeds now.
  62. *
  63. * (03/26/2000) gkh
  64. * Split driver up into device specific pieces.
  65. *
  66. */
  67. #include <linux/config.h>
  68. #include <linux/kernel.h>
  69. #include <linux/errno.h>
  70. #include <linux/init.h>
  71. #include <linux/slab.h>
  72. #include <linux/tty.h>
  73. #include <linux/tty_driver.h>
  74. #include <linux/tty_flip.h>
  75. #include <linux/module.h>
  76. #include <linux/spinlock.h>
  77. #include <asm/uaccess.h>
  78. #include <asm/termbits.h>
  79. #include <linux/usb.h>
  80. #include <linux/serial_reg.h>
  81. #include <linux/serial.h>
  82. #include "usb-serial.h"
  83. #include "whiteheat_fw.h" /* firmware for the ConnectTech WhiteHEAT device */
  84. #include "whiteheat.h" /* WhiteHEAT specific commands */
  85. static int debug;
  86. #ifndef CMSPAR
  87. #define CMSPAR 0
  88. #endif
  89. /*
  90. * Version Information
  91. */
  92. #define DRIVER_VERSION "v2.0"
  93. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>, Stuart MacDonald <stuartm@connecttech.com>"
  94. #define DRIVER_DESC "USB ConnectTech WhiteHEAT driver"
  95. #define CONNECT_TECH_VENDOR_ID 0x0710
  96. #define CONNECT_TECH_FAKE_WHITE_HEAT_ID 0x0001
  97. #define CONNECT_TECH_WHITE_HEAT_ID 0x8001
  98. /*
  99. ID tables for whiteheat are unusual, because we want to different
  100. things for different versions of the device. Eventually, this
  101. will be doable from a single table. But, for now, we define two
  102. separate ID tables, and then a third table that combines them
  103. just for the purpose of exporting the autoloading information.
  104. */
  105. static struct usb_device_id id_table_std [] = {
  106. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  107. { } /* Terminating entry */
  108. };
  109. static struct usb_device_id id_table_prerenumeration [] = {
  110. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  111. { } /* Terminating entry */
  112. };
  113. static struct usb_device_id id_table_combined [] = {
  114. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_WHITE_HEAT_ID) },
  115. { USB_DEVICE(CONNECT_TECH_VENDOR_ID, CONNECT_TECH_FAKE_WHITE_HEAT_ID) },
  116. { } /* Terminating entry */
  117. };
  118. MODULE_DEVICE_TABLE (usb, id_table_combined);
  119. static struct usb_driver whiteheat_driver = {
  120. .owner = THIS_MODULE,
  121. .name = "whiteheat",
  122. .probe = usb_serial_probe,
  123. .disconnect = usb_serial_disconnect,
  124. .id_table = id_table_combined,
  125. };
  126. /* function prototypes for the Connect Tech WhiteHEAT prerenumeration device */
  127. static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id);
  128. static int whiteheat_firmware_attach (struct usb_serial *serial);
  129. /* function prototypes for the Connect Tech WhiteHEAT serial converter */
  130. static int whiteheat_attach (struct usb_serial *serial);
  131. static void whiteheat_shutdown (struct usb_serial *serial);
  132. static int whiteheat_open (struct usb_serial_port *port, struct file *filp);
  133. static void whiteheat_close (struct usb_serial_port *port, struct file *filp);
  134. static int whiteheat_write (struct usb_serial_port *port, const unsigned char *buf, int count);
  135. static int whiteheat_write_room (struct usb_serial_port *port);
  136. static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg);
  137. static void whiteheat_set_termios (struct usb_serial_port *port, struct termios * old);
  138. static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file);
  139. static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file, unsigned int set, unsigned int clear);
  140. static void whiteheat_break_ctl (struct usb_serial_port *port, int break_state);
  141. static int whiteheat_chars_in_buffer (struct usb_serial_port *port);
  142. static void whiteheat_throttle (struct usb_serial_port *port);
  143. static void whiteheat_unthrottle (struct usb_serial_port *port);
  144. static void whiteheat_read_callback (struct urb *urb, struct pt_regs *regs);
  145. static void whiteheat_write_callback (struct urb *urb, struct pt_regs *regs);
  146. static struct usb_serial_device_type whiteheat_fake_device = {
  147. .owner = THIS_MODULE,
  148. .name = "Connect Tech - WhiteHEAT - (prerenumeration)",
  149. .short_name = "whiteheatnofirm",
  150. .id_table = id_table_prerenumeration,
  151. .num_interrupt_in = NUM_DONT_CARE,
  152. .num_bulk_in = NUM_DONT_CARE,
  153. .num_bulk_out = NUM_DONT_CARE,
  154. .num_ports = 1,
  155. .probe = whiteheat_firmware_download,
  156. .attach = whiteheat_firmware_attach,
  157. };
  158. static struct usb_serial_device_type whiteheat_device = {
  159. .owner = THIS_MODULE,
  160. .name = "Connect Tech - WhiteHEAT",
  161. .short_name = "whiteheat",
  162. .id_table = id_table_std,
  163. .num_interrupt_in = NUM_DONT_CARE,
  164. .num_bulk_in = NUM_DONT_CARE,
  165. .num_bulk_out = NUM_DONT_CARE,
  166. .num_ports = 4,
  167. .attach = whiteheat_attach,
  168. .shutdown = whiteheat_shutdown,
  169. .open = whiteheat_open,
  170. .close = whiteheat_close,
  171. .write = whiteheat_write,
  172. .write_room = whiteheat_write_room,
  173. .ioctl = whiteheat_ioctl,
  174. .set_termios = whiteheat_set_termios,
  175. .break_ctl = whiteheat_break_ctl,
  176. .tiocmget = whiteheat_tiocmget,
  177. .tiocmset = whiteheat_tiocmset,
  178. .chars_in_buffer = whiteheat_chars_in_buffer,
  179. .throttle = whiteheat_throttle,
  180. .unthrottle = whiteheat_unthrottle,
  181. .read_bulk_callback = whiteheat_read_callback,
  182. .write_bulk_callback = whiteheat_write_callback,
  183. };
  184. struct whiteheat_command_private {
  185. spinlock_t lock;
  186. __u8 port_running;
  187. __u8 command_finished;
  188. wait_queue_head_t wait_command; /* for handling sleeping while waiting for a command to finish */
  189. __u8 result_buffer[64];
  190. };
  191. #define THROTTLED 0x01
  192. #define ACTUALLY_THROTTLED 0x02
  193. static int urb_pool_size = 8;
  194. struct whiteheat_urb_wrap {
  195. struct list_head list;
  196. struct urb *urb;
  197. };
  198. struct whiteheat_private {
  199. spinlock_t lock;
  200. __u8 flags;
  201. __u8 mcr;
  202. struct list_head rx_urbs_free;
  203. struct list_head rx_urbs_submitted;
  204. struct list_head rx_urb_q;
  205. struct work_struct rx_work;
  206. struct list_head tx_urbs_free;
  207. struct list_head tx_urbs_submitted;
  208. };
  209. /* local function prototypes */
  210. static int start_command_port(struct usb_serial *serial);
  211. static void stop_command_port(struct usb_serial *serial);
  212. static void command_port_write_callback(struct urb *urb, struct pt_regs *regs);
  213. static void command_port_read_callback(struct urb *urb, struct pt_regs *regs);
  214. static int start_port_read(struct usb_serial_port *port);
  215. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb, struct list_head *head);
  216. static struct list_head *list_first(struct list_head *head);
  217. static void rx_data_softint(void *private);
  218. static int firm_send_command(struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize);
  219. static int firm_open(struct usb_serial_port *port);
  220. static int firm_close(struct usb_serial_port *port);
  221. static int firm_setup_port(struct usb_serial_port *port);
  222. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff);
  223. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff);
  224. static int firm_set_break(struct usb_serial_port *port, __u8 onoff);
  225. static int firm_purge(struct usb_serial_port *port, __u8 rxtx);
  226. static int firm_get_dtr_rts(struct usb_serial_port *port);
  227. static int firm_report_tx_done(struct usb_serial_port *port);
  228. #define COMMAND_PORT 4
  229. #define COMMAND_TIMEOUT (2*HZ) /* 2 second timeout for a command */
  230. #define COMMAND_TIMEOUT_MS 2000
  231. #define CLOSING_DELAY (30 * HZ)
  232. /*****************************************************************************
  233. * Connect Tech's White Heat prerenumeration driver functions
  234. *****************************************************************************/
  235. /* steps to download the firmware to the WhiteHEAT device:
  236. - hold the reset (by writing to the reset bit of the CPUCS register)
  237. - download the VEND_AX.HEX file to the chip using VENDOR_REQUEST-ANCHOR_LOAD
  238. - release the reset (by writing to the CPUCS register)
  239. - download the WH.HEX file for all addresses greater than 0x1b3f using
  240. VENDOR_REQUEST-ANCHOR_EXTERNAL_RAM_LOAD
  241. - hold the reset
  242. - download the WH.HEX file for all addresses less than 0x1b40 using
  243. VENDOR_REQUEST_ANCHOR_LOAD
  244. - release the reset
  245. - device renumerated itself and comes up as new device id with all
  246. firmware download completed.
  247. */
  248. static int whiteheat_firmware_download (struct usb_serial *serial, const struct usb_device_id *id)
  249. {
  250. int response;
  251. const struct whiteheat_hex_record *record;
  252. dbg("%s", __FUNCTION__);
  253. response = ezusb_set_reset (serial, 1);
  254. record = &whiteheat_loader[0];
  255. while (record->address != 0xffff) {
  256. response = ezusb_writememory (serial, record->address,
  257. (unsigned char *)record->data, record->data_size, 0xa0);
  258. if (response < 0) {
  259. err("%s - ezusb_writememory failed for loader (%d %04X %p %d)",
  260. __FUNCTION__, response, record->address, record->data, record->data_size);
  261. break;
  262. }
  263. ++record;
  264. }
  265. response = ezusb_set_reset (serial, 0);
  266. record = &whiteheat_firmware[0];
  267. while (record->address < 0x1b40) {
  268. ++record;
  269. }
  270. while (record->address != 0xffff) {
  271. response = ezusb_writememory (serial, record->address,
  272. (unsigned char *)record->data, record->data_size, 0xa3);
  273. if (response < 0) {
  274. err("%s - ezusb_writememory failed for first firmware step (%d %04X %p %d)",
  275. __FUNCTION__, response, record->address, record->data, record->data_size);
  276. break;
  277. }
  278. ++record;
  279. }
  280. response = ezusb_set_reset (serial, 1);
  281. record = &whiteheat_firmware[0];
  282. while (record->address < 0x1b40) {
  283. response = ezusb_writememory (serial, record->address,
  284. (unsigned char *)record->data, record->data_size, 0xa0);
  285. if (response < 0) {
  286. err("%s - ezusb_writememory failed for second firmware step (%d %04X %p %d)",
  287. __FUNCTION__, response, record->address, record->data, record->data_size);
  288. break;
  289. }
  290. ++record;
  291. }
  292. response = ezusb_set_reset (serial, 0);
  293. return 0;
  294. }
  295. static int whiteheat_firmware_attach (struct usb_serial *serial)
  296. {
  297. /* We want this device to fail to have a driver assigned to it */
  298. return 1;
  299. }
  300. /*****************************************************************************
  301. * Connect Tech's White Heat serial driver functions
  302. *****************************************************************************/
  303. static int whiteheat_attach (struct usb_serial *serial)
  304. {
  305. struct usb_serial_port *command_port;
  306. struct whiteheat_command_private *command_info;
  307. struct usb_serial_port *port;
  308. struct whiteheat_private *info;
  309. struct whiteheat_hw_info *hw_info;
  310. int pipe;
  311. int ret;
  312. int alen;
  313. __u8 *command;
  314. __u8 *result;
  315. int i;
  316. int j;
  317. struct urb *urb;
  318. int buf_size;
  319. struct whiteheat_urb_wrap *wrap;
  320. struct list_head *tmp;
  321. command_port = serial->port[COMMAND_PORT];
  322. pipe = usb_sndbulkpipe (serial->dev, command_port->bulk_out_endpointAddress);
  323. command = kmalloc(2, GFP_KERNEL);
  324. if (!command)
  325. goto no_command_buffer;
  326. command[0] = WHITEHEAT_GET_HW_INFO;
  327. command[1] = 0;
  328. result = kmalloc(sizeof(*hw_info) + 1, GFP_KERNEL);
  329. if (!result)
  330. goto no_result_buffer;
  331. /*
  332. * When the module is reloaded the firmware is still there and
  333. * the endpoints are still in the usb core unchanged. This is the
  334. * unlinking bug in disguise. Same for the call below.
  335. */
  336. usb_clear_halt(serial->dev, pipe);
  337. ret = usb_bulk_msg (serial->dev, pipe, command, 2, &alen, COMMAND_TIMEOUT_MS);
  338. if (ret) {
  339. err("%s: Couldn't send command [%d]", serial->type->name, ret);
  340. goto no_firmware;
  341. } else if (alen != sizeof(command)) {
  342. err("%s: Send command incomplete [%d]", serial->type->name, alen);
  343. goto no_firmware;
  344. }
  345. pipe = usb_rcvbulkpipe (serial->dev, command_port->bulk_in_endpointAddress);
  346. /* See the comment on the usb_clear_halt() above */
  347. usb_clear_halt(serial->dev, pipe);
  348. ret = usb_bulk_msg (serial->dev, pipe, result, sizeof(*hw_info) + 1, &alen, COMMAND_TIMEOUT_MS);
  349. if (ret) {
  350. err("%s: Couldn't get results [%d]", serial->type->name, ret);
  351. goto no_firmware;
  352. } else if (alen != sizeof(result)) {
  353. err("%s: Get results incomplete [%d]", serial->type->name, alen);
  354. goto no_firmware;
  355. } else if (result[0] != command[0]) {
  356. err("%s: Command failed [%d]", serial->type->name, result[0]);
  357. goto no_firmware;
  358. }
  359. hw_info = (struct whiteheat_hw_info *)&result[1];
  360. info("%s: Driver %s: Firmware v%d.%02d", serial->type->name,
  361. DRIVER_VERSION, hw_info->sw_major_rev, hw_info->sw_minor_rev);
  362. for (i = 0; i < serial->num_ports; i++) {
  363. port = serial->port[i];
  364. info = (struct whiteheat_private *)kmalloc(sizeof(struct whiteheat_private), GFP_KERNEL);
  365. if (info == NULL) {
  366. err("%s: Out of memory for port structures\n", serial->type->name);
  367. goto no_private;
  368. }
  369. spin_lock_init(&info->lock);
  370. info->flags = 0;
  371. info->mcr = 0;
  372. INIT_WORK(&info->rx_work, rx_data_softint, port);
  373. INIT_LIST_HEAD(&info->rx_urbs_free);
  374. INIT_LIST_HEAD(&info->rx_urbs_submitted);
  375. INIT_LIST_HEAD(&info->rx_urb_q);
  376. INIT_LIST_HEAD(&info->tx_urbs_free);
  377. INIT_LIST_HEAD(&info->tx_urbs_submitted);
  378. for (j = 0; j < urb_pool_size; j++) {
  379. urb = usb_alloc_urb(0, GFP_KERNEL);
  380. if (!urb) {
  381. err("No free urbs available");
  382. goto no_rx_urb;
  383. }
  384. buf_size = port->read_urb->transfer_buffer_length;
  385. urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
  386. if (!urb->transfer_buffer) {
  387. err("Couldn't allocate urb buffer");
  388. goto no_rx_buf;
  389. }
  390. wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
  391. if (!wrap) {
  392. err("Couldn't allocate urb wrapper");
  393. goto no_rx_wrap;
  394. }
  395. usb_fill_bulk_urb(urb, serial->dev,
  396. usb_rcvbulkpipe(serial->dev,
  397. port->bulk_in_endpointAddress),
  398. urb->transfer_buffer, buf_size,
  399. whiteheat_read_callback, port);
  400. wrap->urb = urb;
  401. list_add(&wrap->list, &info->rx_urbs_free);
  402. urb = usb_alloc_urb(0, GFP_KERNEL);
  403. if (!urb) {
  404. err("No free urbs available");
  405. goto no_tx_urb;
  406. }
  407. buf_size = port->write_urb->transfer_buffer_length;
  408. urb->transfer_buffer = kmalloc(buf_size, GFP_KERNEL);
  409. if (!urb->transfer_buffer) {
  410. err("Couldn't allocate urb buffer");
  411. goto no_tx_buf;
  412. }
  413. wrap = kmalloc(sizeof(*wrap), GFP_KERNEL);
  414. if (!wrap) {
  415. err("Couldn't allocate urb wrapper");
  416. goto no_tx_wrap;
  417. }
  418. usb_fill_bulk_urb(urb, serial->dev,
  419. usb_sndbulkpipe(serial->dev,
  420. port->bulk_out_endpointAddress),
  421. urb->transfer_buffer, buf_size,
  422. whiteheat_write_callback, port);
  423. wrap->urb = urb;
  424. list_add(&wrap->list, &info->tx_urbs_free);
  425. }
  426. usb_set_serial_port_data(port, info);
  427. }
  428. command_info = (struct whiteheat_command_private *)kmalloc(sizeof(struct whiteheat_command_private), GFP_KERNEL);
  429. if (command_info == NULL) {
  430. err("%s: Out of memory for port structures\n", serial->type->name);
  431. goto no_command_private;
  432. }
  433. spin_lock_init(&command_info->lock);
  434. command_info->port_running = 0;
  435. init_waitqueue_head(&command_info->wait_command);
  436. usb_set_serial_port_data(command_port, command_info);
  437. command_port->write_urb->complete = command_port_write_callback;
  438. command_port->read_urb->complete = command_port_read_callback;
  439. kfree(result);
  440. kfree(command);
  441. return 0;
  442. no_firmware:
  443. /* Firmware likely not running */
  444. err("%s: Unable to retrieve firmware version, try replugging\n", serial->type->name);
  445. err("%s: If the firmware is not running (status led not blinking)\n", serial->type->name);
  446. err("%s: please contact support@connecttech.com\n", serial->type->name);
  447. return -ENODEV;
  448. no_command_private:
  449. for (i = serial->num_ports - 1; i >= 0; i--) {
  450. port = serial->port[i];
  451. info = usb_get_serial_port_data(port);
  452. for (j = urb_pool_size - 1; j >= 0; j--) {
  453. tmp = list_first(&info->tx_urbs_free);
  454. list_del(tmp);
  455. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  456. urb = wrap->urb;
  457. kfree(wrap);
  458. no_tx_wrap:
  459. kfree(urb->transfer_buffer);
  460. no_tx_buf:
  461. usb_free_urb(urb);
  462. no_tx_urb:
  463. tmp = list_first(&info->rx_urbs_free);
  464. list_del(tmp);
  465. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  466. urb = wrap->urb;
  467. kfree(wrap);
  468. no_rx_wrap:
  469. kfree(urb->transfer_buffer);
  470. no_rx_buf:
  471. usb_free_urb(urb);
  472. no_rx_urb:
  473. ;
  474. }
  475. kfree(info);
  476. no_private:
  477. ;
  478. }
  479. kfree(result);
  480. no_result_buffer:
  481. kfree(command);
  482. no_command_buffer:
  483. return -ENOMEM;
  484. }
  485. static void whiteheat_shutdown (struct usb_serial *serial)
  486. {
  487. struct usb_serial_port *command_port;
  488. struct usb_serial_port *port;
  489. struct whiteheat_private *info;
  490. struct whiteheat_urb_wrap *wrap;
  491. struct urb *urb;
  492. struct list_head *tmp;
  493. struct list_head *tmp2;
  494. int i;
  495. dbg("%s", __FUNCTION__);
  496. /* free up our private data for our command port */
  497. command_port = serial->port[COMMAND_PORT];
  498. kfree (usb_get_serial_port_data(command_port));
  499. for (i = 0; i < serial->num_ports; i++) {
  500. port = serial->port[i];
  501. info = usb_get_serial_port_data(port);
  502. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  503. list_del(tmp);
  504. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  505. urb = wrap->urb;
  506. kfree(wrap);
  507. kfree(urb->transfer_buffer);
  508. usb_free_urb(urb);
  509. }
  510. list_for_each_safe(tmp, tmp2, &info->tx_urbs_free) {
  511. list_del(tmp);
  512. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  513. urb = wrap->urb;
  514. kfree(wrap);
  515. kfree(urb->transfer_buffer);
  516. usb_free_urb(urb);
  517. }
  518. kfree(info);
  519. }
  520. return;
  521. }
  522. static int whiteheat_open (struct usb_serial_port *port, struct file *filp)
  523. {
  524. int retval = 0;
  525. struct termios old_term;
  526. dbg("%s - port %d", __FUNCTION__, port->number);
  527. retval = start_command_port(port->serial);
  528. if (retval)
  529. goto exit;
  530. if (port->tty)
  531. port->tty->low_latency = 1;
  532. /* send an open port command */
  533. retval = firm_open(port);
  534. if (retval) {
  535. stop_command_port(port->serial);
  536. goto exit;
  537. }
  538. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  539. if (retval) {
  540. firm_close(port);
  541. stop_command_port(port->serial);
  542. goto exit;
  543. }
  544. old_term.c_cflag = ~port->tty->termios->c_cflag;
  545. old_term.c_iflag = ~port->tty->termios->c_iflag;
  546. whiteheat_set_termios(port, &old_term);
  547. /* Work around HCD bugs */
  548. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  549. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  550. /* Start reading from the device */
  551. retval = start_port_read(port);
  552. if (retval) {
  553. err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
  554. firm_close(port);
  555. stop_command_port(port->serial);
  556. goto exit;
  557. }
  558. exit:
  559. dbg("%s - exit, retval = %d", __FUNCTION__, retval);
  560. return retval;
  561. }
  562. static void whiteheat_close(struct usb_serial_port *port, struct file * filp)
  563. {
  564. struct whiteheat_private *info = usb_get_serial_port_data(port);
  565. struct whiteheat_urb_wrap *wrap;
  566. struct urb *urb;
  567. struct list_head *tmp;
  568. struct list_head *tmp2;
  569. unsigned long flags;
  570. dbg("%s - port %d", __FUNCTION__, port->number);
  571. /* filp is NULL when called from usb_serial_disconnect */
  572. if (filp && (tty_hung_up_p(filp))) {
  573. return;
  574. }
  575. port->tty->closing = 1;
  576. /*
  577. * Not currently in use; tty_wait_until_sent() calls
  578. * serial_chars_in_buffer() which deadlocks on the second semaphore
  579. * acquisition. This should be fixed at some point. Greg's been
  580. * notified.
  581. if ((filp->f_flags & (O_NDELAY | O_NONBLOCK)) == 0) {
  582. tty_wait_until_sent(port->tty, CLOSING_DELAY);
  583. }
  584. */
  585. if (port->tty->driver->flush_buffer)
  586. port->tty->driver->flush_buffer(port->tty);
  587. tty_ldisc_flush(port->tty);
  588. firm_report_tx_done(port);
  589. firm_close(port);
  590. /* shutdown our bulk reads and writes */
  591. spin_lock_irqsave(&info->lock, flags);
  592. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  593. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  594. urb = wrap->urb;
  595. usb_kill_urb(urb);
  596. list_del(tmp);
  597. list_add(tmp, &info->rx_urbs_free);
  598. }
  599. list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
  600. list_del(tmp);
  601. list_add(tmp, &info->rx_urbs_free);
  602. }
  603. list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
  604. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  605. urb = wrap->urb;
  606. usb_kill_urb(urb);
  607. list_del(tmp);
  608. list_add(tmp, &info->tx_urbs_free);
  609. }
  610. spin_unlock_irqrestore(&info->lock, flags);
  611. stop_command_port(port->serial);
  612. port->tty->closing = 0;
  613. }
  614. static int whiteheat_write(struct usb_serial_port *port, const unsigned char *buf, int count)
  615. {
  616. struct usb_serial *serial = port->serial;
  617. struct whiteheat_private *info = usb_get_serial_port_data(port);
  618. struct whiteheat_urb_wrap *wrap;
  619. struct urb *urb;
  620. int result;
  621. int bytes;
  622. int sent = 0;
  623. unsigned long flags;
  624. struct list_head *tmp;
  625. dbg("%s - port %d", __FUNCTION__, port->number);
  626. if (count == 0) {
  627. dbg("%s - write request of 0 bytes", __FUNCTION__);
  628. return (0);
  629. }
  630. while (count) {
  631. spin_lock_irqsave(&info->lock, flags);
  632. if (list_empty(&info->tx_urbs_free)) {
  633. spin_unlock_irqrestore(&info->lock, flags);
  634. break;
  635. }
  636. tmp = list_first(&info->tx_urbs_free);
  637. list_del(tmp);
  638. spin_unlock_irqrestore(&info->lock, flags);
  639. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  640. urb = wrap->urb;
  641. bytes = (count > port->bulk_out_size) ? port->bulk_out_size : count;
  642. memcpy (urb->transfer_buffer, buf + sent, bytes);
  643. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, bytes, urb->transfer_buffer);
  644. urb->dev = serial->dev;
  645. urb->transfer_buffer_length = bytes;
  646. result = usb_submit_urb(urb, GFP_ATOMIC);
  647. if (result) {
  648. err("%s - failed submitting write urb, error %d", __FUNCTION__, result);
  649. sent = result;
  650. spin_lock_irqsave(&info->lock, flags);
  651. list_add(tmp, &info->tx_urbs_free);
  652. spin_unlock_irqrestore(&info->lock, flags);
  653. break;
  654. } else {
  655. sent += bytes;
  656. count -= bytes;
  657. spin_lock_irqsave(&info->lock, flags);
  658. list_add(tmp, &info->tx_urbs_submitted);
  659. spin_unlock_irqrestore(&info->lock, flags);
  660. }
  661. }
  662. return sent;
  663. }
  664. static int whiteheat_write_room(struct usb_serial_port *port)
  665. {
  666. struct whiteheat_private *info = usb_get_serial_port_data(port);
  667. struct list_head *tmp;
  668. int room = 0;
  669. unsigned long flags;
  670. dbg("%s - port %d", __FUNCTION__, port->number);
  671. spin_lock_irqsave(&info->lock, flags);
  672. list_for_each(tmp, &info->tx_urbs_free)
  673. room++;
  674. spin_unlock_irqrestore(&info->lock, flags);
  675. room *= port->bulk_out_size;
  676. dbg("%s - returns %d", __FUNCTION__, room);
  677. return (room);
  678. }
  679. static int whiteheat_tiocmget (struct usb_serial_port *port, struct file *file)
  680. {
  681. struct whiteheat_private *info = usb_get_serial_port_data(port);
  682. unsigned int modem_signals = 0;
  683. dbg("%s - port %d", __FUNCTION__, port->number);
  684. firm_get_dtr_rts(port);
  685. if (info->mcr & UART_MCR_DTR)
  686. modem_signals |= TIOCM_DTR;
  687. if (info->mcr & UART_MCR_RTS)
  688. modem_signals |= TIOCM_RTS;
  689. return modem_signals;
  690. }
  691. static int whiteheat_tiocmset (struct usb_serial_port *port, struct file *file,
  692. unsigned int set, unsigned int clear)
  693. {
  694. struct whiteheat_private *info = usb_get_serial_port_data(port);
  695. dbg("%s - port %d", __FUNCTION__, port->number);
  696. if (set & TIOCM_RTS)
  697. info->mcr |= UART_MCR_RTS;
  698. if (set & TIOCM_DTR)
  699. info->mcr |= UART_MCR_DTR;
  700. if (clear & TIOCM_RTS)
  701. info->mcr &= ~UART_MCR_RTS;
  702. if (clear & TIOCM_DTR)
  703. info->mcr &= ~UART_MCR_DTR;
  704. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  705. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  706. return 0;
  707. }
  708. static int whiteheat_ioctl (struct usb_serial_port *port, struct file * file, unsigned int cmd, unsigned long arg)
  709. {
  710. struct serial_struct serstruct;
  711. void __user *user_arg = (void __user *)arg;
  712. dbg("%s - port %d, cmd 0x%.4x", __FUNCTION__, port->number, cmd);
  713. switch (cmd) {
  714. case TIOCGSERIAL:
  715. memset(&serstruct, 0, sizeof(serstruct));
  716. serstruct.type = PORT_16654;
  717. serstruct.line = port->serial->minor;
  718. serstruct.port = port->number;
  719. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  720. serstruct.xmit_fifo_size = port->bulk_out_size;
  721. serstruct.custom_divisor = 0;
  722. serstruct.baud_base = 460800;
  723. serstruct.close_delay = CLOSING_DELAY;
  724. serstruct.closing_wait = CLOSING_DELAY;
  725. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  726. return -EFAULT;
  727. break;
  728. case TIOCSSERIAL:
  729. if (copy_from_user(&serstruct, user_arg, sizeof(serstruct)))
  730. return -EFAULT;
  731. /*
  732. * For now this is ignored. dip sets the ASYNC_[V]HI flags
  733. * but this isn't used by us at all. Maybe someone somewhere
  734. * will need the custom_divisor setting.
  735. */
  736. break;
  737. default:
  738. break;
  739. }
  740. return -ENOIOCTLCMD;
  741. }
  742. static void whiteheat_set_termios (struct usb_serial_port *port, struct termios *old_termios)
  743. {
  744. dbg("%s -port %d", __FUNCTION__, port->number);
  745. if ((!port->tty) || (!port->tty->termios)) {
  746. dbg("%s - no tty structures", __FUNCTION__);
  747. goto exit;
  748. }
  749. /* check that they really want us to change something */
  750. if (old_termios) {
  751. if ((port->tty->termios->c_cflag == old_termios->c_cflag) &&
  752. (port->tty->termios->c_iflag == old_termios->c_iflag)) {
  753. dbg("%s - nothing to change...", __FUNCTION__);
  754. goto exit;
  755. }
  756. }
  757. firm_setup_port(port);
  758. exit:
  759. return;
  760. }
  761. static void whiteheat_break_ctl(struct usb_serial_port *port, int break_state) {
  762. firm_set_break(port, break_state);
  763. }
  764. static int whiteheat_chars_in_buffer(struct usb_serial_port *port)
  765. {
  766. struct whiteheat_private *info = usb_get_serial_port_data(port);
  767. struct list_head *tmp;
  768. struct whiteheat_urb_wrap *wrap;
  769. int chars = 0;
  770. unsigned long flags;
  771. dbg("%s - port %d", __FUNCTION__, port->number);
  772. spin_lock_irqsave(&info->lock, flags);
  773. list_for_each(tmp, &info->tx_urbs_submitted) {
  774. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  775. chars += wrap->urb->transfer_buffer_length;
  776. }
  777. spin_unlock_irqrestore(&info->lock, flags);
  778. dbg ("%s - returns %d", __FUNCTION__, chars);
  779. return (chars);
  780. }
  781. static void whiteheat_throttle (struct usb_serial_port *port)
  782. {
  783. struct whiteheat_private *info = usb_get_serial_port_data(port);
  784. unsigned long flags;
  785. dbg("%s - port %d", __FUNCTION__, port->number);
  786. spin_lock_irqsave(&info->lock, flags);
  787. info->flags |= THROTTLED;
  788. spin_unlock_irqrestore(&info->lock, flags);
  789. return;
  790. }
  791. static void whiteheat_unthrottle (struct usb_serial_port *port)
  792. {
  793. struct whiteheat_private *info = usb_get_serial_port_data(port);
  794. int actually_throttled;
  795. unsigned long flags;
  796. dbg("%s - port %d", __FUNCTION__, port->number);
  797. spin_lock_irqsave(&info->lock, flags);
  798. actually_throttled = info->flags & ACTUALLY_THROTTLED;
  799. info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  800. spin_unlock_irqrestore(&info->lock, flags);
  801. if (actually_throttled)
  802. rx_data_softint(port);
  803. return;
  804. }
  805. /*****************************************************************************
  806. * Connect Tech's White Heat callback routines
  807. *****************************************************************************/
  808. static void command_port_write_callback (struct urb *urb, struct pt_regs *regs)
  809. {
  810. dbg("%s", __FUNCTION__);
  811. if (urb->status) {
  812. dbg ("nonzero urb status: %d", urb->status);
  813. return;
  814. }
  815. }
  816. static void command_port_read_callback (struct urb *urb, struct pt_regs *regs)
  817. {
  818. struct usb_serial_port *command_port = (struct usb_serial_port *)urb->context;
  819. struct whiteheat_command_private *command_info;
  820. unsigned char *data = urb->transfer_buffer;
  821. int result;
  822. unsigned long flags;
  823. dbg("%s", __FUNCTION__);
  824. if (urb->status) {
  825. dbg("%s - nonzero urb status: %d", __FUNCTION__, urb->status);
  826. return;
  827. }
  828. usb_serial_debug_data(debug, &command_port->dev, __FUNCTION__, urb->actual_length, data);
  829. command_info = usb_get_serial_port_data(command_port);
  830. if (!command_info) {
  831. dbg ("%s - command_info is NULL, exiting.", __FUNCTION__);
  832. return;
  833. }
  834. spin_lock_irqsave(&command_info->lock, flags);
  835. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  836. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  837. wake_up_interruptible(&command_info->wait_command);
  838. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  839. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  840. wake_up_interruptible(&command_info->wait_command);
  841. } else if (data[0] == WHITEHEAT_EVENT) {
  842. /* These are unsolicited reports from the firmware, hence no waiting command to wakeup */
  843. dbg("%s - event received", __FUNCTION__);
  844. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  845. memcpy(command_info->result_buffer, &data[1], urb->actual_length - 1);
  846. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  847. wake_up_interruptible(&command_info->wait_command);
  848. } else {
  849. dbg("%s - bad reply from firmware", __FUNCTION__);
  850. }
  851. /* Continue trying to always read */
  852. command_port->read_urb->dev = command_port->serial->dev;
  853. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  854. spin_unlock_irqrestore(&command_info->lock, flags);
  855. if (result)
  856. dbg("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  857. }
  858. static void whiteheat_read_callback(struct urb *urb, struct pt_regs *regs)
  859. {
  860. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  861. struct whiteheat_urb_wrap *wrap;
  862. unsigned char *data = urb->transfer_buffer;
  863. struct whiteheat_private *info = usb_get_serial_port_data(port);
  864. dbg("%s - port %d", __FUNCTION__, port->number);
  865. spin_lock(&info->lock);
  866. wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
  867. if (!wrap) {
  868. spin_unlock(&info->lock);
  869. err("%s - Not my urb!", __FUNCTION__);
  870. return;
  871. }
  872. list_del(&wrap->list);
  873. spin_unlock(&info->lock);
  874. if (urb->status) {
  875. dbg("%s - nonzero read bulk status received: %d", __FUNCTION__, urb->status);
  876. spin_lock(&info->lock);
  877. list_add(&wrap->list, &info->rx_urbs_free);
  878. spin_unlock(&info->lock);
  879. return;
  880. }
  881. usb_serial_debug_data(debug, &port->dev, __FUNCTION__, urb->actual_length, data);
  882. spin_lock(&info->lock);
  883. list_add_tail(&wrap->list, &info->rx_urb_q);
  884. if (info->flags & THROTTLED) {
  885. info->flags |= ACTUALLY_THROTTLED;
  886. spin_unlock(&info->lock);
  887. return;
  888. }
  889. spin_unlock(&info->lock);
  890. schedule_work(&info->rx_work);
  891. }
  892. static void whiteheat_write_callback(struct urb *urb, struct pt_regs *regs)
  893. {
  894. struct usb_serial_port *port = (struct usb_serial_port *)urb->context;
  895. struct whiteheat_private *info = usb_get_serial_port_data(port);
  896. struct whiteheat_urb_wrap *wrap;
  897. dbg("%s - port %d", __FUNCTION__, port->number);
  898. spin_lock(&info->lock);
  899. wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
  900. if (!wrap) {
  901. spin_unlock(&info->lock);
  902. err("%s - Not my urb!", __FUNCTION__);
  903. return;
  904. }
  905. list_del(&wrap->list);
  906. list_add(&wrap->list, &info->tx_urbs_free);
  907. spin_unlock(&info->lock);
  908. if (urb->status) {
  909. dbg("%s - nonzero write bulk status received: %d", __FUNCTION__, urb->status);
  910. return;
  911. }
  912. usb_serial_port_softint((void *)port);
  913. schedule_work(&port->work);
  914. }
  915. /*****************************************************************************
  916. * Connect Tech's White Heat firmware interface
  917. *****************************************************************************/
  918. static int firm_send_command (struct usb_serial_port *port, __u8 command, __u8 *data, __u8 datasize)
  919. {
  920. struct usb_serial_port *command_port;
  921. struct whiteheat_command_private *command_info;
  922. struct whiteheat_private *info;
  923. __u8 *transfer_buffer;
  924. int retval = 0;
  925. unsigned long flags;
  926. dbg("%s - command %d", __FUNCTION__, command);
  927. command_port = port->serial->port[COMMAND_PORT];
  928. command_info = usb_get_serial_port_data(command_port);
  929. spin_lock_irqsave(&command_info->lock, flags);
  930. command_info->command_finished = FALSE;
  931. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  932. transfer_buffer[0] = command;
  933. memcpy (&transfer_buffer[1], data, datasize);
  934. command_port->write_urb->transfer_buffer_length = datasize + 1;
  935. command_port->write_urb->dev = port->serial->dev;
  936. retval = usb_submit_urb (command_port->write_urb, GFP_KERNEL);
  937. if (retval) {
  938. dbg("%s - submit urb failed", __FUNCTION__);
  939. goto exit;
  940. }
  941. spin_unlock_irqrestore(&command_info->lock, flags);
  942. /* wait for the command to complete */
  943. wait_event_interruptible_timeout(command_info->wait_command,
  944. (command_info->command_finished != FALSE), COMMAND_TIMEOUT);
  945. spin_lock_irqsave(&command_info->lock, flags);
  946. if (command_info->command_finished == FALSE) {
  947. dbg("%s - command timed out.", __FUNCTION__);
  948. retval = -ETIMEDOUT;
  949. goto exit;
  950. }
  951. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  952. dbg("%s - command failed.", __FUNCTION__);
  953. retval = -EIO;
  954. goto exit;
  955. }
  956. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  957. dbg("%s - command completed.", __FUNCTION__);
  958. switch (command) {
  959. case WHITEHEAT_GET_DTR_RTS:
  960. info = usb_get_serial_port_data(port);
  961. memcpy(&info->mcr, command_info->result_buffer, sizeof(struct whiteheat_dr_info));
  962. break;
  963. }
  964. }
  965. exit:
  966. spin_unlock_irqrestore(&command_info->lock, flags);
  967. return retval;
  968. }
  969. static int firm_open(struct usb_serial_port *port) {
  970. struct whiteheat_simple open_command;
  971. open_command.port = port->number - port->serial->minor + 1;
  972. return firm_send_command(port, WHITEHEAT_OPEN, (__u8 *)&open_command, sizeof(open_command));
  973. }
  974. static int firm_close(struct usb_serial_port *port) {
  975. struct whiteheat_simple close_command;
  976. close_command.port = port->number - port->serial->minor + 1;
  977. return firm_send_command(port, WHITEHEAT_CLOSE, (__u8 *)&close_command, sizeof(close_command));
  978. }
  979. static int firm_setup_port(struct usb_serial_port *port) {
  980. struct whiteheat_port_settings port_settings;
  981. unsigned int cflag = port->tty->termios->c_cflag;
  982. port_settings.port = port->number + 1;
  983. /* get the byte size */
  984. switch (cflag & CSIZE) {
  985. case CS5: port_settings.bits = 5; break;
  986. case CS6: port_settings.bits = 6; break;
  987. case CS7: port_settings.bits = 7; break;
  988. default:
  989. case CS8: port_settings.bits = 8; break;
  990. }
  991. dbg("%s - data bits = %d", __FUNCTION__, port_settings.bits);
  992. /* determine the parity */
  993. if (cflag & PARENB)
  994. if (cflag & CMSPAR)
  995. if (cflag & PARODD)
  996. port_settings.parity = WHITEHEAT_PAR_MARK;
  997. else
  998. port_settings.parity = WHITEHEAT_PAR_SPACE;
  999. else
  1000. if (cflag & PARODD)
  1001. port_settings.parity = WHITEHEAT_PAR_ODD;
  1002. else
  1003. port_settings.parity = WHITEHEAT_PAR_EVEN;
  1004. else
  1005. port_settings.parity = WHITEHEAT_PAR_NONE;
  1006. dbg("%s - parity = %c", __FUNCTION__, port_settings.parity);
  1007. /* figure out the stop bits requested */
  1008. if (cflag & CSTOPB)
  1009. port_settings.stop = 2;
  1010. else
  1011. port_settings.stop = 1;
  1012. dbg("%s - stop bits = %d", __FUNCTION__, port_settings.stop);
  1013. /* figure out the flow control settings */
  1014. if (cflag & CRTSCTS)
  1015. port_settings.hflow = (WHITEHEAT_HFLOW_CTS | WHITEHEAT_HFLOW_RTS);
  1016. else
  1017. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  1018. dbg("%s - hardware flow control = %s %s %s %s", __FUNCTION__,
  1019. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  1020. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  1021. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  1022. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  1023. /* determine software flow control */
  1024. if (I_IXOFF(port->tty))
  1025. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  1026. else
  1027. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  1028. dbg("%s - software flow control = %c", __FUNCTION__, port_settings.sflow);
  1029. port_settings.xon = START_CHAR(port->tty);
  1030. port_settings.xoff = STOP_CHAR(port->tty);
  1031. dbg("%s - XON = %2x, XOFF = %2x", __FUNCTION__, port_settings.xon, port_settings.xoff);
  1032. /* get the baud rate wanted */
  1033. port_settings.baud = tty_get_baud_rate(port->tty);
  1034. dbg("%s - baud rate = %d", __FUNCTION__, port_settings.baud);
  1035. /* handle any settings that aren't specified in the tty structure */
  1036. port_settings.lloop = 0;
  1037. /* now send the message to the device */
  1038. return firm_send_command(port, WHITEHEAT_SETUP_PORT, (__u8 *)&port_settings, sizeof(port_settings));
  1039. }
  1040. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff) {
  1041. struct whiteheat_set_rdb rts_command;
  1042. rts_command.port = port->number - port->serial->minor + 1;
  1043. rts_command.state = onoff;
  1044. return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&rts_command, sizeof(rts_command));
  1045. }
  1046. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff) {
  1047. struct whiteheat_set_rdb dtr_command;
  1048. dtr_command.port = port->number - port->serial->minor + 1;
  1049. dtr_command.state = onoff;
  1050. return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&dtr_command, sizeof(dtr_command));
  1051. }
  1052. static int firm_set_break(struct usb_serial_port *port, __u8 onoff) {
  1053. struct whiteheat_set_rdb break_command;
  1054. break_command.port = port->number - port->serial->minor + 1;
  1055. break_command.state = onoff;
  1056. return firm_send_command(port, WHITEHEAT_SET_RTS, (__u8 *)&break_command, sizeof(break_command));
  1057. }
  1058. static int firm_purge(struct usb_serial_port *port, __u8 rxtx) {
  1059. struct whiteheat_purge purge_command;
  1060. purge_command.port = port->number - port->serial->minor + 1;
  1061. purge_command.what = rxtx;
  1062. return firm_send_command(port, WHITEHEAT_PURGE, (__u8 *)&purge_command, sizeof(purge_command));
  1063. }
  1064. static int firm_get_dtr_rts(struct usb_serial_port *port) {
  1065. struct whiteheat_simple get_dr_command;
  1066. get_dr_command.port = port->number - port->serial->minor + 1;
  1067. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS, (__u8 *)&get_dr_command, sizeof(get_dr_command));
  1068. }
  1069. static int firm_report_tx_done(struct usb_serial_port *port) {
  1070. struct whiteheat_simple close_command;
  1071. close_command.port = port->number - port->serial->minor + 1;
  1072. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE, (__u8 *)&close_command, sizeof(close_command));
  1073. }
  1074. /*****************************************************************************
  1075. * Connect Tech's White Heat utility functions
  1076. *****************************************************************************/
  1077. static int start_command_port(struct usb_serial *serial)
  1078. {
  1079. struct usb_serial_port *command_port;
  1080. struct whiteheat_command_private *command_info;
  1081. unsigned long flags;
  1082. int retval = 0;
  1083. command_port = serial->port[COMMAND_PORT];
  1084. command_info = usb_get_serial_port_data(command_port);
  1085. spin_lock_irqsave(&command_info->lock, flags);
  1086. if (!command_info->port_running) {
  1087. /* Work around HCD bugs */
  1088. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  1089. command_port->read_urb->dev = serial->dev;
  1090. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  1091. if (retval) {
  1092. err("%s - failed submitting read urb, error %d", __FUNCTION__, retval);
  1093. goto exit;
  1094. }
  1095. }
  1096. command_info->port_running++;
  1097. exit:
  1098. spin_unlock_irqrestore(&command_info->lock, flags);
  1099. return retval;
  1100. }
  1101. static void stop_command_port(struct usb_serial *serial)
  1102. {
  1103. struct usb_serial_port *command_port;
  1104. struct whiteheat_command_private *command_info;
  1105. unsigned long flags;
  1106. command_port = serial->port[COMMAND_PORT];
  1107. command_info = usb_get_serial_port_data(command_port);
  1108. spin_lock_irqsave(&command_info->lock, flags);
  1109. command_info->port_running--;
  1110. if (!command_info->port_running)
  1111. usb_kill_urb(command_port->read_urb);
  1112. spin_unlock_irqrestore(&command_info->lock, flags);
  1113. }
  1114. static int start_port_read(struct usb_serial_port *port)
  1115. {
  1116. struct whiteheat_private *info = usb_get_serial_port_data(port);
  1117. struct whiteheat_urb_wrap *wrap;
  1118. struct urb *urb;
  1119. int retval = 0;
  1120. unsigned long flags;
  1121. struct list_head *tmp;
  1122. struct list_head *tmp2;
  1123. spin_lock_irqsave(&info->lock, flags);
  1124. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  1125. list_del(tmp);
  1126. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1127. urb = wrap->urb;
  1128. urb->dev = port->serial->dev;
  1129. retval = usb_submit_urb(urb, GFP_KERNEL);
  1130. if (retval) {
  1131. list_add(tmp, &info->rx_urbs_free);
  1132. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  1133. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1134. urb = wrap->urb;
  1135. usb_kill_urb(urb);
  1136. list_del(tmp);
  1137. list_add(tmp, &info->rx_urbs_free);
  1138. }
  1139. break;
  1140. }
  1141. list_add(tmp, &info->rx_urbs_submitted);
  1142. }
  1143. spin_unlock_irqrestore(&info->lock, flags);
  1144. return retval;
  1145. }
  1146. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb* urb, struct list_head *head)
  1147. {
  1148. struct whiteheat_urb_wrap *wrap;
  1149. struct list_head *tmp;
  1150. list_for_each(tmp, head) {
  1151. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1152. if (wrap->urb == urb)
  1153. return wrap;
  1154. }
  1155. return NULL;
  1156. }
  1157. static struct list_head *list_first(struct list_head *head)
  1158. {
  1159. return head->next;
  1160. }
  1161. static void rx_data_softint(void *private)
  1162. {
  1163. struct usb_serial_port *port = (struct usb_serial_port *)private;
  1164. struct whiteheat_private *info = usb_get_serial_port_data(port);
  1165. struct tty_struct *tty = port->tty;
  1166. struct whiteheat_urb_wrap *wrap;
  1167. struct urb *urb;
  1168. unsigned long flags;
  1169. struct list_head *tmp;
  1170. struct list_head *tmp2;
  1171. int result;
  1172. int sent = 0;
  1173. spin_lock_irqsave(&info->lock, flags);
  1174. if (info->flags & THROTTLED) {
  1175. spin_unlock_irqrestore(&info->lock, flags);
  1176. return;
  1177. }
  1178. list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
  1179. list_del(tmp);
  1180. spin_unlock_irqrestore(&info->lock, flags);
  1181. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1182. urb = wrap->urb;
  1183. if (tty && urb->actual_length) {
  1184. if (urb->actual_length > TTY_FLIPBUF_SIZE - tty->flip.count) {
  1185. spin_lock_irqsave(&info->lock, flags);
  1186. list_add(tmp, &info->rx_urb_q);
  1187. spin_unlock_irqrestore(&info->lock, flags);
  1188. tty_flip_buffer_push(tty);
  1189. schedule_work(&info->rx_work);
  1190. return;
  1191. }
  1192. memcpy(tty->flip.char_buf_ptr, urb->transfer_buffer, urb->actual_length);
  1193. tty->flip.char_buf_ptr += urb->actual_length;
  1194. tty->flip.count += urb->actual_length;
  1195. sent += urb->actual_length;
  1196. }
  1197. urb->dev = port->serial->dev;
  1198. result = usb_submit_urb(urb, GFP_ATOMIC);
  1199. if (result) {
  1200. err("%s - failed resubmitting read urb, error %d", __FUNCTION__, result);
  1201. spin_lock_irqsave(&info->lock, flags);
  1202. list_add(tmp, &info->rx_urbs_free);
  1203. continue;
  1204. }
  1205. spin_lock_irqsave(&info->lock, flags);
  1206. list_add(tmp, &info->rx_urbs_submitted);
  1207. }
  1208. spin_unlock_irqrestore(&info->lock, flags);
  1209. if (sent)
  1210. tty_flip_buffer_push(tty);
  1211. }
  1212. /*****************************************************************************
  1213. * Connect Tech's White Heat module functions
  1214. *****************************************************************************/
  1215. static int __init whiteheat_init (void)
  1216. {
  1217. int retval;
  1218. retval = usb_serial_register(&whiteheat_fake_device);
  1219. if (retval)
  1220. goto failed_fake_register;
  1221. retval = usb_serial_register(&whiteheat_device);
  1222. if (retval)
  1223. goto failed_device_register;
  1224. retval = usb_register(&whiteheat_driver);
  1225. if (retval)
  1226. goto failed_usb_register;
  1227. info(DRIVER_DESC " " DRIVER_VERSION);
  1228. return 0;
  1229. failed_usb_register:
  1230. usb_serial_deregister(&whiteheat_device);
  1231. failed_device_register:
  1232. usb_serial_deregister(&whiteheat_fake_device);
  1233. failed_fake_register:
  1234. return retval;
  1235. }
  1236. static void __exit whiteheat_exit (void)
  1237. {
  1238. usb_deregister (&whiteheat_driver);
  1239. usb_serial_deregister (&whiteheat_fake_device);
  1240. usb_serial_deregister (&whiteheat_device);
  1241. }
  1242. module_init(whiteheat_init);
  1243. module_exit(whiteheat_exit);
  1244. MODULE_AUTHOR( DRIVER_AUTHOR );
  1245. MODULE_DESCRIPTION( DRIVER_DESC );
  1246. MODULE_LICENSE("GPL");
  1247. module_param(urb_pool_size, int, 0);
  1248. MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
  1249. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1250. MODULE_PARM_DESC(debug, "Debug enabled or not");