whiteheat.c 44 KB

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