whiteheat.c 43 KB

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