whiteheat.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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 const 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 const 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 const 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);
  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,
  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);
  148. static int whiteheat_tiocmset(struct tty_struct *tty,
  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 void 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. }
  595. static int whiteheat_open(struct tty_struct *tty, struct usb_serial_port *port)
  596. {
  597. int retval = 0;
  598. dbg("%s - port %d", __func__, port->number);
  599. retval = start_command_port(port->serial);
  600. if (retval)
  601. goto exit;
  602. if (tty)
  603. tty->low_latency = 1;
  604. /* send an open port command */
  605. retval = firm_open(port);
  606. if (retval) {
  607. stop_command_port(port->serial);
  608. goto exit;
  609. }
  610. retval = firm_purge(port, WHITEHEAT_PURGE_RX | WHITEHEAT_PURGE_TX);
  611. if (retval) {
  612. firm_close(port);
  613. stop_command_port(port->serial);
  614. goto exit;
  615. }
  616. if (tty)
  617. firm_setup_port(tty);
  618. /* Work around HCD bugs */
  619. usb_clear_halt(port->serial->dev, port->read_urb->pipe);
  620. usb_clear_halt(port->serial->dev, port->write_urb->pipe);
  621. /* Start reading from the device */
  622. retval = start_port_read(port);
  623. if (retval) {
  624. dev_err(&port->dev,
  625. "%s - failed submitting read urb, error %d\n",
  626. __func__, retval);
  627. firm_close(port);
  628. stop_command_port(port->serial);
  629. goto exit;
  630. }
  631. exit:
  632. dbg("%s - exit, retval = %d", __func__, retval);
  633. return retval;
  634. }
  635. static void whiteheat_close(struct usb_serial_port *port)
  636. {
  637. struct whiteheat_private *info = usb_get_serial_port_data(port);
  638. struct whiteheat_urb_wrap *wrap;
  639. struct urb *urb;
  640. struct list_head *tmp;
  641. struct list_head *tmp2;
  642. dbg("%s - port %d", __func__, port->number);
  643. firm_report_tx_done(port);
  644. firm_close(port);
  645. /* shutdown our bulk reads and writes */
  646. mutex_lock(&info->deathwarrant);
  647. spin_lock_irq(&info->lock);
  648. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  649. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  650. urb = wrap->urb;
  651. list_del(tmp);
  652. spin_unlock_irq(&info->lock);
  653. usb_kill_urb(urb);
  654. spin_lock_irq(&info->lock);
  655. list_add(tmp, &info->rx_urbs_free);
  656. }
  657. list_for_each_safe(tmp, tmp2, &info->rx_urb_q)
  658. list_move(tmp, &info->rx_urbs_free);
  659. list_for_each_safe(tmp, tmp2, &info->tx_urbs_submitted) {
  660. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  661. urb = wrap->urb;
  662. list_del(tmp);
  663. spin_unlock_irq(&info->lock);
  664. usb_kill_urb(urb);
  665. spin_lock_irq(&info->lock);
  666. list_add(tmp, &info->tx_urbs_free);
  667. }
  668. spin_unlock_irq(&info->lock);
  669. mutex_unlock(&info->deathwarrant);
  670. stop_command_port(port->serial);
  671. }
  672. static int whiteheat_write(struct tty_struct *tty,
  673. struct usb_serial_port *port, const unsigned char *buf, int count)
  674. {
  675. struct usb_serial *serial = port->serial;
  676. struct whiteheat_private *info = usb_get_serial_port_data(port);
  677. struct whiteheat_urb_wrap *wrap;
  678. struct urb *urb;
  679. int result;
  680. int bytes;
  681. int sent = 0;
  682. unsigned long flags;
  683. struct list_head *tmp;
  684. dbg("%s - port %d", __func__, port->number);
  685. if (count == 0) {
  686. dbg("%s - write request of 0 bytes", __func__);
  687. return (0);
  688. }
  689. while (count) {
  690. spin_lock_irqsave(&info->lock, flags);
  691. if (list_empty(&info->tx_urbs_free)) {
  692. spin_unlock_irqrestore(&info->lock, flags);
  693. break;
  694. }
  695. tmp = list_first(&info->tx_urbs_free);
  696. list_del(tmp);
  697. spin_unlock_irqrestore(&info->lock, flags);
  698. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  699. urb = wrap->urb;
  700. bytes = (count > port->bulk_out_size) ?
  701. port->bulk_out_size : count;
  702. memcpy(urb->transfer_buffer, buf + sent, bytes);
  703. usb_serial_debug_data(debug, &port->dev,
  704. __func__, bytes, urb->transfer_buffer);
  705. urb->dev = serial->dev;
  706. urb->transfer_buffer_length = bytes;
  707. result = usb_submit_urb(urb, GFP_ATOMIC);
  708. if (result) {
  709. dev_err(&port->dev,
  710. "%s - failed submitting write urb, error %d\n",
  711. __func__, result);
  712. sent = result;
  713. spin_lock_irqsave(&info->lock, flags);
  714. list_add(tmp, &info->tx_urbs_free);
  715. spin_unlock_irqrestore(&info->lock, flags);
  716. break;
  717. } else {
  718. sent += bytes;
  719. count -= bytes;
  720. spin_lock_irqsave(&info->lock, flags);
  721. list_add(tmp, &info->tx_urbs_submitted);
  722. spin_unlock_irqrestore(&info->lock, flags);
  723. }
  724. }
  725. return sent;
  726. }
  727. static int whiteheat_write_room(struct tty_struct *tty)
  728. {
  729. struct usb_serial_port *port = tty->driver_data;
  730. struct whiteheat_private *info = usb_get_serial_port_data(port);
  731. struct list_head *tmp;
  732. int room = 0;
  733. unsigned long flags;
  734. dbg("%s - port %d", __func__, port->number);
  735. spin_lock_irqsave(&info->lock, flags);
  736. list_for_each(tmp, &info->tx_urbs_free)
  737. room++;
  738. spin_unlock_irqrestore(&info->lock, flags);
  739. room *= port->bulk_out_size;
  740. dbg("%s - returns %d", __func__, room);
  741. return (room);
  742. }
  743. static int whiteheat_tiocmget(struct tty_struct *tty)
  744. {
  745. struct usb_serial_port *port = tty->driver_data;
  746. struct whiteheat_private *info = usb_get_serial_port_data(port);
  747. unsigned int modem_signals = 0;
  748. dbg("%s - port %d", __func__, port->number);
  749. firm_get_dtr_rts(port);
  750. if (info->mcr & UART_MCR_DTR)
  751. modem_signals |= TIOCM_DTR;
  752. if (info->mcr & UART_MCR_RTS)
  753. modem_signals |= TIOCM_RTS;
  754. return modem_signals;
  755. }
  756. static int whiteheat_tiocmset(struct tty_struct *tty,
  757. unsigned int set, unsigned int clear)
  758. {
  759. struct usb_serial_port *port = tty->driver_data;
  760. struct whiteheat_private *info = usb_get_serial_port_data(port);
  761. dbg("%s - port %d", __func__, port->number);
  762. if (set & TIOCM_RTS)
  763. info->mcr |= UART_MCR_RTS;
  764. if (set & TIOCM_DTR)
  765. info->mcr |= UART_MCR_DTR;
  766. if (clear & TIOCM_RTS)
  767. info->mcr &= ~UART_MCR_RTS;
  768. if (clear & TIOCM_DTR)
  769. info->mcr &= ~UART_MCR_DTR;
  770. firm_set_dtr(port, info->mcr & UART_MCR_DTR);
  771. firm_set_rts(port, info->mcr & UART_MCR_RTS);
  772. return 0;
  773. }
  774. static int whiteheat_ioctl(struct tty_struct *tty,
  775. unsigned int cmd, unsigned long arg)
  776. {
  777. struct usb_serial_port *port = tty->driver_data;
  778. struct serial_struct serstruct;
  779. void __user *user_arg = (void __user *)arg;
  780. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  781. switch (cmd) {
  782. case TIOCGSERIAL:
  783. memset(&serstruct, 0, sizeof(serstruct));
  784. serstruct.type = PORT_16654;
  785. serstruct.line = port->serial->minor;
  786. serstruct.port = port->number;
  787. serstruct.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  788. serstruct.xmit_fifo_size = port->bulk_out_size;
  789. serstruct.custom_divisor = 0;
  790. serstruct.baud_base = 460800;
  791. serstruct.close_delay = CLOSING_DELAY;
  792. serstruct.closing_wait = CLOSING_DELAY;
  793. if (copy_to_user(user_arg, &serstruct, sizeof(serstruct)))
  794. return -EFAULT;
  795. break;
  796. default:
  797. break;
  798. }
  799. return -ENOIOCTLCMD;
  800. }
  801. static void whiteheat_set_termios(struct tty_struct *tty,
  802. struct usb_serial_port *port, struct ktermios *old_termios)
  803. {
  804. firm_setup_port(tty);
  805. }
  806. static void whiteheat_break_ctl(struct tty_struct *tty, int break_state)
  807. {
  808. struct usb_serial_port *port = tty->driver_data;
  809. firm_set_break(port, break_state);
  810. }
  811. static int whiteheat_chars_in_buffer(struct tty_struct *tty)
  812. {
  813. struct usb_serial_port *port = tty->driver_data;
  814. struct whiteheat_private *info = usb_get_serial_port_data(port);
  815. struct list_head *tmp;
  816. struct whiteheat_urb_wrap *wrap;
  817. int chars = 0;
  818. unsigned long flags;
  819. dbg("%s - port %d", __func__, port->number);
  820. spin_lock_irqsave(&info->lock, flags);
  821. list_for_each(tmp, &info->tx_urbs_submitted) {
  822. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  823. chars += wrap->urb->transfer_buffer_length;
  824. }
  825. spin_unlock_irqrestore(&info->lock, flags);
  826. dbg("%s - returns %d", __func__, chars);
  827. return chars;
  828. }
  829. static void whiteheat_throttle(struct tty_struct *tty)
  830. {
  831. struct usb_serial_port *port = tty->driver_data;
  832. struct whiteheat_private *info = usb_get_serial_port_data(port);
  833. dbg("%s - port %d", __func__, port->number);
  834. spin_lock_irq(&info->lock);
  835. info->flags |= THROTTLED;
  836. spin_unlock_irq(&info->lock);
  837. }
  838. static void whiteheat_unthrottle(struct tty_struct *tty)
  839. {
  840. struct usb_serial_port *port = tty->driver_data;
  841. struct whiteheat_private *info = usb_get_serial_port_data(port);
  842. int actually_throttled;
  843. dbg("%s - port %d", __func__, port->number);
  844. spin_lock_irq(&info->lock);
  845. actually_throttled = info->flags & ACTUALLY_THROTTLED;
  846. info->flags &= ~(THROTTLED | ACTUALLY_THROTTLED);
  847. spin_unlock_irq(&info->lock);
  848. if (actually_throttled)
  849. rx_data_softint(&info->rx_work);
  850. }
  851. /*****************************************************************************
  852. * Connect Tech's White Heat callback routines
  853. *****************************************************************************/
  854. static void command_port_write_callback(struct urb *urb)
  855. {
  856. int status = urb->status;
  857. dbg("%s", __func__);
  858. if (status) {
  859. dbg("nonzero urb status: %d", status);
  860. return;
  861. }
  862. }
  863. static void command_port_read_callback(struct urb *urb)
  864. {
  865. struct usb_serial_port *command_port = urb->context;
  866. struct whiteheat_command_private *command_info;
  867. int status = urb->status;
  868. unsigned char *data = urb->transfer_buffer;
  869. int result;
  870. dbg("%s", __func__);
  871. command_info = usb_get_serial_port_data(command_port);
  872. if (!command_info) {
  873. dbg("%s - command_info is NULL, exiting.", __func__);
  874. return;
  875. }
  876. if (status) {
  877. dbg("%s - nonzero urb status: %d", __func__, status);
  878. if (status != -ENOENT)
  879. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  880. wake_up(&command_info->wait_command);
  881. return;
  882. }
  883. usb_serial_debug_data(debug, &command_port->dev,
  884. __func__, urb->actual_length, data);
  885. if (data[0] == WHITEHEAT_CMD_COMPLETE) {
  886. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  887. wake_up(&command_info->wait_command);
  888. } else if (data[0] == WHITEHEAT_CMD_FAILURE) {
  889. command_info->command_finished = WHITEHEAT_CMD_FAILURE;
  890. wake_up(&command_info->wait_command);
  891. } else if (data[0] == WHITEHEAT_EVENT) {
  892. /* These are unsolicited reports from the firmware, hence no
  893. waiting command to wakeup */
  894. dbg("%s - event received", __func__);
  895. } else if (data[0] == WHITEHEAT_GET_DTR_RTS) {
  896. memcpy(command_info->result_buffer, &data[1],
  897. urb->actual_length - 1);
  898. command_info->command_finished = WHITEHEAT_CMD_COMPLETE;
  899. wake_up(&command_info->wait_command);
  900. } else
  901. dbg("%s - bad reply from firmware", __func__);
  902. /* Continue trying to always read */
  903. command_port->read_urb->dev = command_port->serial->dev;
  904. result = usb_submit_urb(command_port->read_urb, GFP_ATOMIC);
  905. if (result)
  906. dbg("%s - failed resubmitting read urb, error %d",
  907. __func__, result);
  908. }
  909. static void whiteheat_read_callback(struct urb *urb)
  910. {
  911. struct usb_serial_port *port = urb->context;
  912. struct whiteheat_urb_wrap *wrap;
  913. unsigned char *data = urb->transfer_buffer;
  914. struct whiteheat_private *info = usb_get_serial_port_data(port);
  915. int status = urb->status;
  916. dbg("%s - port %d", __func__, port->number);
  917. spin_lock(&info->lock);
  918. wrap = urb_to_wrap(urb, &info->rx_urbs_submitted);
  919. if (!wrap) {
  920. spin_unlock(&info->lock);
  921. dev_err(&port->dev, "%s - Not my urb!\n", __func__);
  922. return;
  923. }
  924. list_del(&wrap->list);
  925. spin_unlock(&info->lock);
  926. if (status) {
  927. dbg("%s - nonzero read bulk status received: %d",
  928. __func__, status);
  929. spin_lock(&info->lock);
  930. list_add(&wrap->list, &info->rx_urbs_free);
  931. spin_unlock(&info->lock);
  932. return;
  933. }
  934. usb_serial_debug_data(debug, &port->dev,
  935. __func__, urb->actual_length, data);
  936. spin_lock(&info->lock);
  937. list_add_tail(&wrap->list, &info->rx_urb_q);
  938. if (info->flags & THROTTLED) {
  939. info->flags |= ACTUALLY_THROTTLED;
  940. spin_unlock(&info->lock);
  941. return;
  942. }
  943. spin_unlock(&info->lock);
  944. schedule_work(&info->rx_work);
  945. }
  946. static void whiteheat_write_callback(struct urb *urb)
  947. {
  948. struct usb_serial_port *port = urb->context;
  949. struct whiteheat_private *info = usb_get_serial_port_data(port);
  950. struct whiteheat_urb_wrap *wrap;
  951. int status = urb->status;
  952. dbg("%s - port %d", __func__, port->number);
  953. spin_lock(&info->lock);
  954. wrap = urb_to_wrap(urb, &info->tx_urbs_submitted);
  955. if (!wrap) {
  956. spin_unlock(&info->lock);
  957. dev_err(&port->dev, "%s - Not my urb!\n", __func__);
  958. return;
  959. }
  960. list_move(&wrap->list, &info->tx_urbs_free);
  961. spin_unlock(&info->lock);
  962. if (status) {
  963. dbg("%s - nonzero write bulk status received: %d",
  964. __func__, status);
  965. return;
  966. }
  967. usb_serial_port_softint(port);
  968. }
  969. /*****************************************************************************
  970. * Connect Tech's White Heat firmware interface
  971. *****************************************************************************/
  972. static int firm_send_command(struct usb_serial_port *port, __u8 command,
  973. __u8 *data, __u8 datasize)
  974. {
  975. struct usb_serial_port *command_port;
  976. struct whiteheat_command_private *command_info;
  977. struct whiteheat_private *info;
  978. __u8 *transfer_buffer;
  979. int retval = 0;
  980. int t;
  981. dbg("%s - command %d", __func__, command);
  982. command_port = port->serial->port[COMMAND_PORT];
  983. command_info = usb_get_serial_port_data(command_port);
  984. mutex_lock(&command_info->mutex);
  985. command_info->command_finished = false;
  986. transfer_buffer = (__u8 *)command_port->write_urb->transfer_buffer;
  987. transfer_buffer[0] = command;
  988. memcpy(&transfer_buffer[1], data, datasize);
  989. command_port->write_urb->transfer_buffer_length = datasize + 1;
  990. command_port->write_urb->dev = port->serial->dev;
  991. retval = usb_submit_urb(command_port->write_urb, GFP_NOIO);
  992. if (retval) {
  993. dbg("%s - submit urb failed", __func__);
  994. goto exit;
  995. }
  996. /* wait for the command to complete */
  997. t = wait_event_timeout(command_info->wait_command,
  998. (bool)command_info->command_finished, COMMAND_TIMEOUT);
  999. if (!t)
  1000. usb_kill_urb(command_port->write_urb);
  1001. if (command_info->command_finished == false) {
  1002. dbg("%s - command timed out.", __func__);
  1003. retval = -ETIMEDOUT;
  1004. goto exit;
  1005. }
  1006. if (command_info->command_finished == WHITEHEAT_CMD_FAILURE) {
  1007. dbg("%s - command failed.", __func__);
  1008. retval = -EIO;
  1009. goto exit;
  1010. }
  1011. if (command_info->command_finished == WHITEHEAT_CMD_COMPLETE) {
  1012. dbg("%s - command completed.", __func__);
  1013. switch (command) {
  1014. case WHITEHEAT_GET_DTR_RTS:
  1015. info = usb_get_serial_port_data(port);
  1016. memcpy(&info->mcr, command_info->result_buffer,
  1017. sizeof(struct whiteheat_dr_info));
  1018. break;
  1019. }
  1020. }
  1021. exit:
  1022. mutex_unlock(&command_info->mutex);
  1023. return retval;
  1024. }
  1025. static int firm_open(struct usb_serial_port *port)
  1026. {
  1027. struct whiteheat_simple open_command;
  1028. open_command.port = port->number - port->serial->minor + 1;
  1029. return firm_send_command(port, WHITEHEAT_OPEN,
  1030. (__u8 *)&open_command, sizeof(open_command));
  1031. }
  1032. static int firm_close(struct usb_serial_port *port)
  1033. {
  1034. struct whiteheat_simple close_command;
  1035. close_command.port = port->number - port->serial->minor + 1;
  1036. return firm_send_command(port, WHITEHEAT_CLOSE,
  1037. (__u8 *)&close_command, sizeof(close_command));
  1038. }
  1039. static void firm_setup_port(struct tty_struct *tty)
  1040. {
  1041. struct usb_serial_port *port = tty->driver_data;
  1042. struct whiteheat_port_settings port_settings;
  1043. unsigned int cflag = tty->termios->c_cflag;
  1044. port_settings.port = port->number + 1;
  1045. /* get the byte size */
  1046. switch (cflag & CSIZE) {
  1047. case CS5: port_settings.bits = 5; break;
  1048. case CS6: port_settings.bits = 6; break;
  1049. case CS7: port_settings.bits = 7; break;
  1050. default:
  1051. case CS8: port_settings.bits = 8; break;
  1052. }
  1053. dbg("%s - data bits = %d", __func__, port_settings.bits);
  1054. /* determine the parity */
  1055. if (cflag & PARENB)
  1056. if (cflag & CMSPAR)
  1057. if (cflag & PARODD)
  1058. port_settings.parity = WHITEHEAT_PAR_MARK;
  1059. else
  1060. port_settings.parity = WHITEHEAT_PAR_SPACE;
  1061. else
  1062. if (cflag & PARODD)
  1063. port_settings.parity = WHITEHEAT_PAR_ODD;
  1064. else
  1065. port_settings.parity = WHITEHEAT_PAR_EVEN;
  1066. else
  1067. port_settings.parity = WHITEHEAT_PAR_NONE;
  1068. dbg("%s - parity = %c", __func__, port_settings.parity);
  1069. /* figure out the stop bits requested */
  1070. if (cflag & CSTOPB)
  1071. port_settings.stop = 2;
  1072. else
  1073. port_settings.stop = 1;
  1074. dbg("%s - stop bits = %d", __func__, port_settings.stop);
  1075. /* figure out the flow control settings */
  1076. if (cflag & CRTSCTS)
  1077. port_settings.hflow = (WHITEHEAT_HFLOW_CTS |
  1078. WHITEHEAT_HFLOW_RTS);
  1079. else
  1080. port_settings.hflow = WHITEHEAT_HFLOW_NONE;
  1081. dbg("%s - hardware flow control = %s %s %s %s", __func__,
  1082. (port_settings.hflow & WHITEHEAT_HFLOW_CTS) ? "CTS" : "",
  1083. (port_settings.hflow & WHITEHEAT_HFLOW_RTS) ? "RTS" : "",
  1084. (port_settings.hflow & WHITEHEAT_HFLOW_DSR) ? "DSR" : "",
  1085. (port_settings.hflow & WHITEHEAT_HFLOW_DTR) ? "DTR" : "");
  1086. /* determine software flow control */
  1087. if (I_IXOFF(tty))
  1088. port_settings.sflow = WHITEHEAT_SFLOW_RXTX;
  1089. else
  1090. port_settings.sflow = WHITEHEAT_SFLOW_NONE;
  1091. dbg("%s - software flow control = %c", __func__, port_settings.sflow);
  1092. port_settings.xon = START_CHAR(tty);
  1093. port_settings.xoff = STOP_CHAR(tty);
  1094. dbg("%s - XON = %2x, XOFF = %2x",
  1095. __func__, port_settings.xon, port_settings.xoff);
  1096. /* get the baud rate wanted */
  1097. port_settings.baud = tty_get_baud_rate(tty);
  1098. dbg("%s - baud rate = %d", __func__, port_settings.baud);
  1099. /* fixme: should set validated settings */
  1100. tty_encode_baud_rate(tty, port_settings.baud, port_settings.baud);
  1101. /* handle any settings that aren't specified in the tty structure */
  1102. port_settings.lloop = 0;
  1103. /* now send the message to the device */
  1104. firm_send_command(port, WHITEHEAT_SETUP_PORT,
  1105. (__u8 *)&port_settings, sizeof(port_settings));
  1106. }
  1107. static int firm_set_rts(struct usb_serial_port *port, __u8 onoff)
  1108. {
  1109. struct whiteheat_set_rdb rts_command;
  1110. rts_command.port = port->number - port->serial->minor + 1;
  1111. rts_command.state = onoff;
  1112. return firm_send_command(port, WHITEHEAT_SET_RTS,
  1113. (__u8 *)&rts_command, sizeof(rts_command));
  1114. }
  1115. static int firm_set_dtr(struct usb_serial_port *port, __u8 onoff)
  1116. {
  1117. struct whiteheat_set_rdb dtr_command;
  1118. dtr_command.port = port->number - port->serial->minor + 1;
  1119. dtr_command.state = onoff;
  1120. return firm_send_command(port, WHITEHEAT_SET_DTR,
  1121. (__u8 *)&dtr_command, sizeof(dtr_command));
  1122. }
  1123. static int firm_set_break(struct usb_serial_port *port, __u8 onoff)
  1124. {
  1125. struct whiteheat_set_rdb break_command;
  1126. break_command.port = port->number - port->serial->minor + 1;
  1127. break_command.state = onoff;
  1128. return firm_send_command(port, WHITEHEAT_SET_BREAK,
  1129. (__u8 *)&break_command, sizeof(break_command));
  1130. }
  1131. static int firm_purge(struct usb_serial_port *port, __u8 rxtx)
  1132. {
  1133. struct whiteheat_purge purge_command;
  1134. purge_command.port = port->number - port->serial->minor + 1;
  1135. purge_command.what = rxtx;
  1136. return firm_send_command(port, WHITEHEAT_PURGE,
  1137. (__u8 *)&purge_command, sizeof(purge_command));
  1138. }
  1139. static int firm_get_dtr_rts(struct usb_serial_port *port)
  1140. {
  1141. struct whiteheat_simple get_dr_command;
  1142. get_dr_command.port = port->number - port->serial->minor + 1;
  1143. return firm_send_command(port, WHITEHEAT_GET_DTR_RTS,
  1144. (__u8 *)&get_dr_command, sizeof(get_dr_command));
  1145. }
  1146. static int firm_report_tx_done(struct usb_serial_port *port)
  1147. {
  1148. struct whiteheat_simple close_command;
  1149. close_command.port = port->number - port->serial->minor + 1;
  1150. return firm_send_command(port, WHITEHEAT_REPORT_TX_DONE,
  1151. (__u8 *)&close_command, sizeof(close_command));
  1152. }
  1153. /*****************************************************************************
  1154. * Connect Tech's White Heat utility functions
  1155. *****************************************************************************/
  1156. static int start_command_port(struct usb_serial *serial)
  1157. {
  1158. struct usb_serial_port *command_port;
  1159. struct whiteheat_command_private *command_info;
  1160. int retval = 0;
  1161. command_port = serial->port[COMMAND_PORT];
  1162. command_info = usb_get_serial_port_data(command_port);
  1163. mutex_lock(&command_info->mutex);
  1164. if (!command_info->port_running) {
  1165. /* Work around HCD bugs */
  1166. usb_clear_halt(serial->dev, command_port->read_urb->pipe);
  1167. command_port->read_urb->dev = serial->dev;
  1168. retval = usb_submit_urb(command_port->read_urb, GFP_KERNEL);
  1169. if (retval) {
  1170. dev_err(&serial->dev->dev,
  1171. "%s - failed submitting read urb, error %d\n",
  1172. __func__, retval);
  1173. goto exit;
  1174. }
  1175. }
  1176. command_info->port_running++;
  1177. exit:
  1178. mutex_unlock(&command_info->mutex);
  1179. return retval;
  1180. }
  1181. static void stop_command_port(struct usb_serial *serial)
  1182. {
  1183. struct usb_serial_port *command_port;
  1184. struct whiteheat_command_private *command_info;
  1185. command_port = serial->port[COMMAND_PORT];
  1186. command_info = usb_get_serial_port_data(command_port);
  1187. mutex_lock(&command_info->mutex);
  1188. command_info->port_running--;
  1189. if (!command_info->port_running)
  1190. usb_kill_urb(command_port->read_urb);
  1191. mutex_unlock(&command_info->mutex);
  1192. }
  1193. static int start_port_read(struct usb_serial_port *port)
  1194. {
  1195. struct whiteheat_private *info = usb_get_serial_port_data(port);
  1196. struct whiteheat_urb_wrap *wrap;
  1197. struct urb *urb;
  1198. int retval = 0;
  1199. unsigned long flags;
  1200. struct list_head *tmp;
  1201. struct list_head *tmp2;
  1202. spin_lock_irqsave(&info->lock, flags);
  1203. list_for_each_safe(tmp, tmp2, &info->rx_urbs_free) {
  1204. list_del(tmp);
  1205. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1206. urb = wrap->urb;
  1207. urb->dev = port->serial->dev;
  1208. spin_unlock_irqrestore(&info->lock, flags);
  1209. retval = usb_submit_urb(urb, GFP_KERNEL);
  1210. if (retval) {
  1211. spin_lock_irqsave(&info->lock, flags);
  1212. list_add(tmp, &info->rx_urbs_free);
  1213. list_for_each_safe(tmp, tmp2, &info->rx_urbs_submitted) {
  1214. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1215. urb = wrap->urb;
  1216. list_del(tmp);
  1217. spin_unlock_irqrestore(&info->lock, flags);
  1218. usb_kill_urb(urb);
  1219. spin_lock_irqsave(&info->lock, flags);
  1220. list_add(tmp, &info->rx_urbs_free);
  1221. }
  1222. break;
  1223. }
  1224. spin_lock_irqsave(&info->lock, flags);
  1225. list_add(tmp, &info->rx_urbs_submitted);
  1226. }
  1227. spin_unlock_irqrestore(&info->lock, flags);
  1228. return retval;
  1229. }
  1230. static struct whiteheat_urb_wrap *urb_to_wrap(struct urb *urb,
  1231. struct list_head *head)
  1232. {
  1233. struct whiteheat_urb_wrap *wrap;
  1234. struct list_head *tmp;
  1235. list_for_each(tmp, head) {
  1236. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1237. if (wrap->urb == urb)
  1238. return wrap;
  1239. }
  1240. return NULL;
  1241. }
  1242. static struct list_head *list_first(struct list_head *head)
  1243. {
  1244. return head->next;
  1245. }
  1246. static void rx_data_softint(struct work_struct *work)
  1247. {
  1248. struct whiteheat_private *info =
  1249. container_of(work, struct whiteheat_private, rx_work);
  1250. struct usb_serial_port *port = info->port;
  1251. struct tty_struct *tty = tty_port_tty_get(&port->port);
  1252. struct whiteheat_urb_wrap *wrap;
  1253. struct urb *urb;
  1254. unsigned long flags;
  1255. struct list_head *tmp;
  1256. struct list_head *tmp2;
  1257. int result;
  1258. int sent = 0;
  1259. spin_lock_irqsave(&info->lock, flags);
  1260. if (info->flags & THROTTLED) {
  1261. spin_unlock_irqrestore(&info->lock, flags);
  1262. goto out;
  1263. }
  1264. list_for_each_safe(tmp, tmp2, &info->rx_urb_q) {
  1265. list_del(tmp);
  1266. spin_unlock_irqrestore(&info->lock, flags);
  1267. wrap = list_entry(tmp, struct whiteheat_urb_wrap, list);
  1268. urb = wrap->urb;
  1269. if (tty && urb->actual_length)
  1270. sent += tty_insert_flip_string(tty,
  1271. urb->transfer_buffer, urb->actual_length);
  1272. urb->dev = port->serial->dev;
  1273. result = usb_submit_urb(urb, GFP_ATOMIC);
  1274. if (result) {
  1275. dev_err(&port->dev,
  1276. "%s - failed resubmitting read urb, error %d\n",
  1277. __func__, result);
  1278. spin_lock_irqsave(&info->lock, flags);
  1279. list_add(tmp, &info->rx_urbs_free);
  1280. continue;
  1281. }
  1282. spin_lock_irqsave(&info->lock, flags);
  1283. list_add(tmp, &info->rx_urbs_submitted);
  1284. }
  1285. spin_unlock_irqrestore(&info->lock, flags);
  1286. if (sent)
  1287. tty_flip_buffer_push(tty);
  1288. out:
  1289. tty_kref_put(tty);
  1290. }
  1291. /*****************************************************************************
  1292. * Connect Tech's White Heat module functions
  1293. *****************************************************************************/
  1294. static int __init whiteheat_init(void)
  1295. {
  1296. int retval;
  1297. retval = usb_serial_register(&whiteheat_fake_device);
  1298. if (retval)
  1299. goto failed_fake_register;
  1300. retval = usb_serial_register(&whiteheat_device);
  1301. if (retval)
  1302. goto failed_device_register;
  1303. retval = usb_register(&whiteheat_driver);
  1304. if (retval)
  1305. goto failed_usb_register;
  1306. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1307. DRIVER_DESC "\n");
  1308. return 0;
  1309. failed_usb_register:
  1310. usb_serial_deregister(&whiteheat_device);
  1311. failed_device_register:
  1312. usb_serial_deregister(&whiteheat_fake_device);
  1313. failed_fake_register:
  1314. return retval;
  1315. }
  1316. static void __exit whiteheat_exit(void)
  1317. {
  1318. usb_deregister(&whiteheat_driver);
  1319. usb_serial_deregister(&whiteheat_fake_device);
  1320. usb_serial_deregister(&whiteheat_device);
  1321. }
  1322. module_init(whiteheat_init);
  1323. module_exit(whiteheat_exit);
  1324. MODULE_AUTHOR(DRIVER_AUTHOR);
  1325. MODULE_DESCRIPTION(DRIVER_DESC);
  1326. MODULE_LICENSE("GPL");
  1327. MODULE_FIRMWARE("whiteheat.fw");
  1328. MODULE_FIRMWARE("whiteheat_loader.fw");
  1329. module_param(urb_pool_size, int, 0);
  1330. MODULE_PARM_DESC(urb_pool_size, "Number of urbs to use for buffering");
  1331. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1332. MODULE_PARM_DESC(debug, "Debug enabled or not");