whiteheat.c 44 KB

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