whiteheat.c 44 KB

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