whiteheat.c 41 KB

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