cypress_m8.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652
  1. /*
  2. * USB Cypress M8 driver
  3. *
  4. * Copyright (C) 2004
  5. * Lonnie Mendez (dignome@gmail.com)
  6. * Copyright (C) 2003,2004
  7. * Neil Whelchel (koyama@firstlight.net)
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * See Documentation/usb/usb-serial.txt for more information on using this
  15. * driver
  16. *
  17. * See http://geocities.com/i0xox0i for information on this driver and the
  18. * earthmate usb device.
  19. *
  20. * Lonnie Mendez <dignome@gmail.com>
  21. * 4-29-2005
  22. * Fixed problem where setting or retreiving the serial config would fail
  23. * with EPIPE. Removed CRTS toggling so the driver behaves more like
  24. * other usbserial adapters. Issued new interval of 1ms instead of the
  25. * default 10ms. As a result, transfer speed has been substantially
  26. * increased from avg. 850bps to avg. 3300bps. initial termios has also
  27. * been modified. Cleaned up code and formatting issues so it is more
  28. * readable. Replaced the C++ style comments.
  29. *
  30. * Lonnie Mendez <dignome@gmail.com>
  31. * 12-15-2004
  32. * Incorporated write buffering from pl2303 driver. Fixed bug with line
  33. * handling so both lines are raised in cypress_open. (was dropping rts)
  34. * Various code cleanups made as well along with other misc bug fixes.
  35. *
  36. * Lonnie Mendez <dignome@gmail.com>
  37. * 04-10-2004
  38. * Driver modified to support dynamic line settings. Various improvments
  39. * and features.
  40. *
  41. * Neil Whelchel
  42. * 10-2003
  43. * Driver first released.
  44. *
  45. */
  46. /* Thanks to Neil Whelchel for writing the first cypress m8 implementation
  47. for linux. */
  48. /* Thanks to cypress for providing references for the hid reports. */
  49. /* Thanks to Jiang Zhang for providing links and for general help. */
  50. /* Code originates and was built up from ftdi_sio, belkin, pl2303 and others.*/
  51. #include <linux/kernel.h>
  52. #include <linux/errno.h>
  53. #include <linux/init.h>
  54. #include <linux/slab.h>
  55. #include <linux/tty.h>
  56. #include <linux/tty_driver.h>
  57. #include <linux/tty_flip.h>
  58. #include <linux/module.h>
  59. #include <linux/moduleparam.h>
  60. #include <linux/spinlock.h>
  61. #include <linux/usb.h>
  62. #include <linux/usb/serial.h>
  63. #include <linux/serial.h>
  64. #include <linux/delay.h>
  65. #include <linux/uaccess.h>
  66. #include "cypress_m8.h"
  67. #ifdef CONFIG_USB_SERIAL_DEBUG
  68. static int debug = 1;
  69. #else
  70. static int debug;
  71. #endif
  72. static int stats;
  73. static int interval;
  74. /*
  75. * Version Information
  76. */
  77. #define DRIVER_VERSION "v1.09"
  78. #define DRIVER_AUTHOR "Lonnie Mendez <dignome@gmail.com>, Neil Whelchel <koyama@firstlight.net>"
  79. #define DRIVER_DESC "Cypress USB to Serial Driver"
  80. /* write buffer size defines */
  81. #define CYPRESS_BUF_SIZE 1024
  82. #define CYPRESS_CLOSING_WAIT (30*HZ)
  83. static struct usb_device_id id_table_earthmate [] = {
  84. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  85. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  86. { } /* Terminating entry */
  87. };
  88. static struct usb_device_id id_table_cyphidcomrs232 [] = {
  89. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  90. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  91. { } /* Terminating entry */
  92. };
  93. static struct usb_device_id id_table_nokiaca42v2 [] = {
  94. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  95. { } /* Terminating entry */
  96. };
  97. static struct usb_device_id id_table_combined [] = {
  98. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB) },
  99. { USB_DEVICE(VENDOR_ID_DELORME, PRODUCT_ID_EARTHMATEUSB_LT20) },
  100. { USB_DEVICE(VENDOR_ID_CYPRESS, PRODUCT_ID_CYPHIDCOM) },
  101. { USB_DEVICE(VENDOR_ID_POWERCOM, PRODUCT_ID_UPS) },
  102. { USB_DEVICE(VENDOR_ID_DAZZLE, PRODUCT_ID_CA42) },
  103. { } /* Terminating entry */
  104. };
  105. MODULE_DEVICE_TABLE(usb, id_table_combined);
  106. static struct usb_driver cypress_driver = {
  107. .name = "cypress",
  108. .probe = usb_serial_probe,
  109. .disconnect = usb_serial_disconnect,
  110. .id_table = id_table_combined,
  111. .no_dynamic_id = 1,
  112. };
  113. enum packet_format {
  114. packet_format_1, /* b0:status, b1:payload count */
  115. packet_format_2 /* b0[7:3]:status, b0[2:0]:payload count */
  116. };
  117. struct cypress_private {
  118. spinlock_t lock; /* private lock */
  119. int chiptype; /* identifier of device, for quirks/etc */
  120. int bytes_in; /* used for statistics */
  121. int bytes_out; /* used for statistics */
  122. int cmd_count; /* used for statistics */
  123. int cmd_ctrl; /* always set this to 1 before issuing a command */
  124. struct cypress_buf *buf; /* write buffer */
  125. int write_urb_in_use; /* write urb in use indicator */
  126. int write_urb_interval; /* interval to use for write urb */
  127. int read_urb_interval; /* interval to use for read urb */
  128. int comm_is_ok; /* true if communication is (still) ok */
  129. int termios_initialized;
  130. __u8 line_control; /* holds dtr / rts value */
  131. __u8 current_status; /* received from last read - info on dsr,cts,cd,ri,etc */
  132. __u8 current_config; /* stores the current configuration byte */
  133. __u8 rx_flags; /* throttling - used from whiteheat/ftdi_sio */
  134. enum packet_format pkt_fmt; /* format to use for packet send / receive */
  135. int get_cfg_unsafe; /* If true, the CYPRESS_GET_CONFIG is unsafe */
  136. int baud_rate; /* stores current baud rate in
  137. integer form */
  138. int isthrottled; /* if throttled, discard reads */
  139. wait_queue_head_t delta_msr_wait; /* used for TIOCMIWAIT */
  140. char prev_status, diff_status; /* used for TIOCMIWAIT */
  141. /* we pass a pointer to this as the arguement sent to
  142. cypress_set_termios old_termios */
  143. struct ktermios tmp_termios; /* stores the old termios settings */
  144. };
  145. /* write buffer structure */
  146. struct cypress_buf {
  147. unsigned int buf_size;
  148. char *buf_buf;
  149. char *buf_get;
  150. char *buf_put;
  151. };
  152. /* function prototypes for the Cypress USB to serial device */
  153. static int cypress_earthmate_startup(struct usb_serial *serial);
  154. static int cypress_hidcom_startup(struct usb_serial *serial);
  155. static int cypress_ca42v2_startup(struct usb_serial *serial);
  156. static void cypress_release(struct usb_serial *serial);
  157. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port);
  158. static void cypress_close(struct usb_serial_port *port);
  159. static void cypress_dtr_rts(struct usb_serial_port *port, int on);
  160. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  161. const unsigned char *buf, int count);
  162. static void cypress_send(struct usb_serial_port *port);
  163. static int cypress_write_room(struct tty_struct *tty);
  164. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  165. unsigned int cmd, unsigned long arg);
  166. static void cypress_set_termios(struct tty_struct *tty,
  167. struct usb_serial_port *port, struct ktermios *old);
  168. static int cypress_tiocmget(struct tty_struct *tty, struct file *file);
  169. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  170. unsigned int set, unsigned int clear);
  171. static int cypress_chars_in_buffer(struct tty_struct *tty);
  172. static void cypress_throttle(struct tty_struct *tty);
  173. static void cypress_unthrottle(struct tty_struct *tty);
  174. static void cypress_set_dead(struct usb_serial_port *port);
  175. static void cypress_read_int_callback(struct urb *urb);
  176. static void cypress_write_int_callback(struct urb *urb);
  177. /* write buffer functions */
  178. static struct cypress_buf *cypress_buf_alloc(unsigned int size);
  179. static void cypress_buf_free(struct cypress_buf *cb);
  180. static void cypress_buf_clear(struct cypress_buf *cb);
  181. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb);
  182. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb);
  183. static unsigned int cypress_buf_put(struct cypress_buf *cb,
  184. const char *buf, unsigned int count);
  185. static unsigned int cypress_buf_get(struct cypress_buf *cb,
  186. char *buf, unsigned int count);
  187. static struct usb_serial_driver cypress_earthmate_device = {
  188. .driver = {
  189. .owner = THIS_MODULE,
  190. .name = "earthmate",
  191. },
  192. .description = "DeLorme Earthmate USB",
  193. .usb_driver = &cypress_driver,
  194. .id_table = id_table_earthmate,
  195. .num_ports = 1,
  196. .attach = cypress_earthmate_startup,
  197. .release = cypress_release,
  198. .open = cypress_open,
  199. .close = cypress_close,
  200. .dtr_rts = cypress_dtr_rts,
  201. .write = cypress_write,
  202. .write_room = cypress_write_room,
  203. .ioctl = cypress_ioctl,
  204. .set_termios = cypress_set_termios,
  205. .tiocmget = cypress_tiocmget,
  206. .tiocmset = cypress_tiocmset,
  207. .chars_in_buffer = cypress_chars_in_buffer,
  208. .throttle = cypress_throttle,
  209. .unthrottle = cypress_unthrottle,
  210. .read_int_callback = cypress_read_int_callback,
  211. .write_int_callback = cypress_write_int_callback,
  212. };
  213. static struct usb_serial_driver cypress_hidcom_device = {
  214. .driver = {
  215. .owner = THIS_MODULE,
  216. .name = "cyphidcom",
  217. },
  218. .description = "HID->COM RS232 Adapter",
  219. .usb_driver = &cypress_driver,
  220. .id_table = id_table_cyphidcomrs232,
  221. .num_ports = 1,
  222. .attach = cypress_hidcom_startup,
  223. .release = cypress_release,
  224. .open = cypress_open,
  225. .close = cypress_close,
  226. .dtr_rts = cypress_dtr_rts,
  227. .write = cypress_write,
  228. .write_room = cypress_write_room,
  229. .ioctl = cypress_ioctl,
  230. .set_termios = cypress_set_termios,
  231. .tiocmget = cypress_tiocmget,
  232. .tiocmset = cypress_tiocmset,
  233. .chars_in_buffer = cypress_chars_in_buffer,
  234. .throttle = cypress_throttle,
  235. .unthrottle = cypress_unthrottle,
  236. .read_int_callback = cypress_read_int_callback,
  237. .write_int_callback = cypress_write_int_callback,
  238. };
  239. static struct usb_serial_driver cypress_ca42v2_device = {
  240. .driver = {
  241. .owner = THIS_MODULE,
  242. .name = "nokiaca42v2",
  243. },
  244. .description = "Nokia CA-42 V2 Adapter",
  245. .usb_driver = &cypress_driver,
  246. .id_table = id_table_nokiaca42v2,
  247. .num_ports = 1,
  248. .attach = cypress_ca42v2_startup,
  249. .release = cypress_release,
  250. .open = cypress_open,
  251. .close = cypress_close,
  252. .dtr_rts = cypress_dtr_rts,
  253. .write = cypress_write,
  254. .write_room = cypress_write_room,
  255. .ioctl = cypress_ioctl,
  256. .set_termios = cypress_set_termios,
  257. .tiocmget = cypress_tiocmget,
  258. .tiocmset = cypress_tiocmset,
  259. .chars_in_buffer = cypress_chars_in_buffer,
  260. .throttle = cypress_throttle,
  261. .unthrottle = cypress_unthrottle,
  262. .read_int_callback = cypress_read_int_callback,
  263. .write_int_callback = cypress_write_int_callback,
  264. };
  265. /*****************************************************************************
  266. * Cypress serial helper functions
  267. *****************************************************************************/
  268. static int analyze_baud_rate(struct usb_serial_port *port, speed_t new_rate)
  269. {
  270. struct cypress_private *priv;
  271. priv = usb_get_serial_port_data(port);
  272. /*
  273. * The general purpose firmware for the Cypress M8 allows for
  274. * a maximum speed of 57600bps (I have no idea whether DeLorme
  275. * chose to use the general purpose firmware or not), if you
  276. * need to modify this speed setting for your own project
  277. * please add your own chiptype and modify the code likewise.
  278. * The Cypress HID->COM device will work successfully up to
  279. * 115200bps (but the actual throughput is around 3kBps).
  280. */
  281. if (port->serial->dev->speed == USB_SPEED_LOW) {
  282. /*
  283. * Mike Isely <isely@pobox.com> 2-Feb-2008: The
  284. * Cypress app note that describes this mechanism
  285. * states the the low-speed part can't handle more
  286. * than 800 bytes/sec, in which case 4800 baud is the
  287. * safest speed for a part like that.
  288. */
  289. if (new_rate > 4800) {
  290. dbg("%s - failed setting baud rate, device incapable "
  291. "speed %d", __func__, new_rate);
  292. return -1;
  293. }
  294. }
  295. switch (priv->chiptype) {
  296. case CT_EARTHMATE:
  297. if (new_rate <= 600) {
  298. /* 300 and 600 baud rates are supported under
  299. * the generic firmware, but are not used with
  300. * NMEA and SiRF protocols */
  301. dbg("%s - failed setting baud rate, unsupported speed "
  302. "of %d on Earthmate GPS", __func__, new_rate);
  303. return -1;
  304. }
  305. break;
  306. default:
  307. break;
  308. }
  309. return new_rate;
  310. }
  311. /* This function can either set or retrieve the current serial line settings */
  312. static int cypress_serial_control(struct tty_struct *tty,
  313. struct usb_serial_port *port, speed_t baud_rate, int data_bits,
  314. int stop_bits, int parity_enable, int parity_type, int reset,
  315. int cypress_request_type)
  316. {
  317. int new_baudrate = 0, retval = 0, tries = 0;
  318. struct cypress_private *priv;
  319. __u8 feature_buffer[5];
  320. unsigned long flags;
  321. dbg("%s", __func__);
  322. priv = usb_get_serial_port_data(port);
  323. if (!priv->comm_is_ok)
  324. return -ENODEV;
  325. switch (cypress_request_type) {
  326. case CYPRESS_SET_CONFIG:
  327. new_baudrate = priv->baud_rate;
  328. /* 0 means 'Hang up' so doesn't change the true bit rate */
  329. if (baud_rate == 0)
  330. new_baudrate = priv->baud_rate;
  331. /* Change of speed ? */
  332. else if (baud_rate != priv->baud_rate) {
  333. dbg("%s - baud rate is changing", __func__);
  334. retval = analyze_baud_rate(port, baud_rate);
  335. if (retval >= 0) {
  336. new_baudrate = retval;
  337. dbg("%s - New baud rate set to %d",
  338. __func__, new_baudrate);
  339. }
  340. }
  341. dbg("%s - baud rate is being sent as %d",
  342. __func__, new_baudrate);
  343. memset(feature_buffer, 0, sizeof(feature_buffer));
  344. /* fill the feature_buffer with new configuration */
  345. *((u_int32_t *)feature_buffer) = new_baudrate;
  346. feature_buffer[4] |= data_bits; /* assign data bits in 2 bit space ( max 3 ) */
  347. /* 1 bit gap */
  348. feature_buffer[4] |= (stop_bits << 3); /* assign stop bits in 1 bit space */
  349. feature_buffer[4] |= (parity_enable << 4); /* assign parity flag in 1 bit space */
  350. feature_buffer[4] |= (parity_type << 5); /* assign parity type in 1 bit space */
  351. /* 1 bit gap */
  352. feature_buffer[4] |= (reset << 7); /* assign reset at end of byte, 1 bit space */
  353. dbg("%s - device is being sent this feature report:",
  354. __func__);
  355. dbg("%s - %02X - %02X - %02X - %02X - %02X", __func__,
  356. feature_buffer[0], feature_buffer[1],
  357. feature_buffer[2], feature_buffer[3],
  358. feature_buffer[4]);
  359. do {
  360. retval = usb_control_msg(port->serial->dev,
  361. usb_sndctrlpipe(port->serial->dev, 0),
  362. HID_REQ_SET_REPORT,
  363. USB_DIR_OUT | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  364. 0x0300, 0, feature_buffer,
  365. sizeof(feature_buffer), 500);
  366. if (tries++ >= 3)
  367. break;
  368. } while (retval != sizeof(feature_buffer) &&
  369. retval != -ENODEV);
  370. if (retval != sizeof(feature_buffer)) {
  371. dev_err(&port->dev, "%s - failed sending serial "
  372. "line settings - %d\n", __func__, retval);
  373. cypress_set_dead(port);
  374. } else {
  375. spin_lock_irqsave(&priv->lock, flags);
  376. priv->baud_rate = new_baudrate;
  377. priv->current_config = feature_buffer[4];
  378. spin_unlock_irqrestore(&priv->lock, flags);
  379. /* If we asked for a speed change encode it */
  380. if (baud_rate)
  381. tty_encode_baud_rate(tty,
  382. new_baudrate, new_baudrate);
  383. }
  384. break;
  385. case CYPRESS_GET_CONFIG:
  386. if (priv->get_cfg_unsafe) {
  387. /* Not implemented for this device,
  388. and if we try to do it we're likely
  389. to crash the hardware. */
  390. return -ENOTTY;
  391. }
  392. dbg("%s - retreiving serial line settings", __func__);
  393. /* set initial values in feature buffer */
  394. memset(feature_buffer, 0, sizeof(feature_buffer));
  395. do {
  396. retval = usb_control_msg(port->serial->dev,
  397. usb_rcvctrlpipe(port->serial->dev, 0),
  398. HID_REQ_GET_REPORT,
  399. USB_DIR_IN | USB_RECIP_INTERFACE | USB_TYPE_CLASS,
  400. 0x0300, 0, feature_buffer,
  401. sizeof(feature_buffer), 500);
  402. if (tries++ >= 3)
  403. break;
  404. } while (retval != sizeof(feature_buffer)
  405. && retval != -ENODEV);
  406. if (retval != sizeof(feature_buffer)) {
  407. dev_err(&port->dev, "%s - failed to retrieve serial "
  408. "line settings - %d\n", __func__, retval);
  409. cypress_set_dead(port);
  410. return retval;
  411. } else {
  412. spin_lock_irqsave(&priv->lock, flags);
  413. /* store the config in one byte, and later
  414. use bit masks to check values */
  415. priv->current_config = feature_buffer[4];
  416. priv->baud_rate = *((u_int32_t *)feature_buffer);
  417. spin_unlock_irqrestore(&priv->lock, flags);
  418. }
  419. }
  420. spin_lock_irqsave(&priv->lock, flags);
  421. ++priv->cmd_count;
  422. spin_unlock_irqrestore(&priv->lock, flags);
  423. return retval;
  424. } /* cypress_serial_control */
  425. static void cypress_set_dead(struct usb_serial_port *port)
  426. {
  427. struct cypress_private *priv = usb_get_serial_port_data(port);
  428. unsigned long flags;
  429. spin_lock_irqsave(&priv->lock, flags);
  430. if (!priv->comm_is_ok) {
  431. spin_unlock_irqrestore(&priv->lock, flags);
  432. return;
  433. }
  434. priv->comm_is_ok = 0;
  435. spin_unlock_irqrestore(&priv->lock, flags);
  436. dev_err(&port->dev, "cypress_m8 suspending failing port %d - "
  437. "interval might be too short\n", port->number);
  438. }
  439. /*****************************************************************************
  440. * Cypress serial driver functions
  441. *****************************************************************************/
  442. static int generic_startup(struct usb_serial *serial)
  443. {
  444. struct cypress_private *priv;
  445. struct usb_serial_port *port = serial->port[0];
  446. dbg("%s - port %d", __func__, port->number);
  447. priv = kzalloc(sizeof(struct cypress_private), GFP_KERNEL);
  448. if (!priv)
  449. return -ENOMEM;
  450. priv->comm_is_ok = !0;
  451. spin_lock_init(&priv->lock);
  452. priv->buf = cypress_buf_alloc(CYPRESS_BUF_SIZE);
  453. if (priv->buf == NULL) {
  454. kfree(priv);
  455. return -ENOMEM;
  456. }
  457. init_waitqueue_head(&priv->delta_msr_wait);
  458. usb_reset_configuration(serial->dev);
  459. priv->cmd_ctrl = 0;
  460. priv->line_control = 0;
  461. priv->termios_initialized = 0;
  462. priv->rx_flags = 0;
  463. /* Default packet format setting is determined by packet size.
  464. Anything with a size larger then 9 must have a separate
  465. count field since the 3 bit count field is otherwise too
  466. small. Otherwise we can use the slightly more compact
  467. format. This is in accordance with the cypress_m8 serial
  468. converter app note. */
  469. if (port->interrupt_out_size > 9)
  470. priv->pkt_fmt = packet_format_1;
  471. else
  472. priv->pkt_fmt = packet_format_2;
  473. if (interval > 0) {
  474. priv->write_urb_interval = interval;
  475. priv->read_urb_interval = interval;
  476. dbg("%s - port %d read & write intervals forced to %d",
  477. __func__, port->number, interval);
  478. } else {
  479. priv->write_urb_interval = port->interrupt_out_urb->interval;
  480. priv->read_urb_interval = port->interrupt_in_urb->interval;
  481. dbg("%s - port %d intervals: read=%d write=%d",
  482. __func__, port->number,
  483. priv->read_urb_interval, priv->write_urb_interval);
  484. }
  485. usb_set_serial_port_data(port, priv);
  486. return 0;
  487. }
  488. static int cypress_earthmate_startup(struct usb_serial *serial)
  489. {
  490. struct cypress_private *priv;
  491. struct usb_serial_port *port = serial->port[0];
  492. dbg("%s", __func__);
  493. if (generic_startup(serial)) {
  494. dbg("%s - Failed setting up port %d", __func__,
  495. port->number);
  496. return 1;
  497. }
  498. priv = usb_get_serial_port_data(port);
  499. priv->chiptype = CT_EARTHMATE;
  500. /* All Earthmate devices use the separated-count packet
  501. format! Idiotic. */
  502. priv->pkt_fmt = packet_format_1;
  503. if (serial->dev->descriptor.idProduct !=
  504. cpu_to_le16(PRODUCT_ID_EARTHMATEUSB)) {
  505. /* The old original USB Earthmate seemed able to
  506. handle GET_CONFIG requests; everything they've
  507. produced since that time crashes if this command is
  508. attempted :-( */
  509. dbg("%s - Marking this device as unsafe for GET_CONFIG "
  510. "commands", __func__);
  511. priv->get_cfg_unsafe = !0;
  512. }
  513. return 0;
  514. } /* cypress_earthmate_startup */
  515. static int cypress_hidcom_startup(struct usb_serial *serial)
  516. {
  517. struct cypress_private *priv;
  518. dbg("%s", __func__);
  519. if (generic_startup(serial)) {
  520. dbg("%s - Failed setting up port %d", __func__,
  521. serial->port[0]->number);
  522. return 1;
  523. }
  524. priv = usb_get_serial_port_data(serial->port[0]);
  525. priv->chiptype = CT_CYPHIDCOM;
  526. return 0;
  527. } /* cypress_hidcom_startup */
  528. static int cypress_ca42v2_startup(struct usb_serial *serial)
  529. {
  530. struct cypress_private *priv;
  531. dbg("%s", __func__);
  532. if (generic_startup(serial)) {
  533. dbg("%s - Failed setting up port %d", __func__,
  534. serial->port[0]->number);
  535. return 1;
  536. }
  537. priv = usb_get_serial_port_data(serial->port[0]);
  538. priv->chiptype = CT_CA42V2;
  539. return 0;
  540. } /* cypress_ca42v2_startup */
  541. static void cypress_release(struct usb_serial *serial)
  542. {
  543. struct cypress_private *priv;
  544. dbg("%s - port %d", __func__, serial->port[0]->number);
  545. /* all open ports are closed at this point */
  546. priv = usb_get_serial_port_data(serial->port[0]);
  547. if (priv) {
  548. cypress_buf_free(priv->buf);
  549. kfree(priv);
  550. }
  551. }
  552. static int cypress_open(struct tty_struct *tty, struct usb_serial_port *port)
  553. {
  554. struct cypress_private *priv = usb_get_serial_port_data(port);
  555. struct usb_serial *serial = port->serial;
  556. unsigned long flags;
  557. int result = 0;
  558. dbg("%s - port %d", __func__, port->number);
  559. if (!priv->comm_is_ok)
  560. return -EIO;
  561. /* clear halts before open */
  562. usb_clear_halt(serial->dev, 0x81);
  563. usb_clear_halt(serial->dev, 0x02);
  564. spin_lock_irqsave(&priv->lock, flags);
  565. /* reset read/write statistics */
  566. priv->bytes_in = 0;
  567. priv->bytes_out = 0;
  568. priv->cmd_count = 0;
  569. priv->rx_flags = 0;
  570. spin_unlock_irqrestore(&priv->lock, flags);
  571. /* Set termios */
  572. cypress_send(port);
  573. if (tty)
  574. cypress_set_termios(tty, port, &priv->tmp_termios);
  575. /* setup the port and start reading from the device */
  576. if (!port->interrupt_in_urb) {
  577. dev_err(&port->dev, "%s - interrupt_in_urb is empty!\n",
  578. __func__);
  579. return -1;
  580. }
  581. usb_fill_int_urb(port->interrupt_in_urb, serial->dev,
  582. usb_rcvintpipe(serial->dev, port->interrupt_in_endpointAddress),
  583. port->interrupt_in_urb->transfer_buffer,
  584. port->interrupt_in_urb->transfer_buffer_length,
  585. cypress_read_int_callback, port, priv->read_urb_interval);
  586. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  587. if (result) {
  588. dev_err(&port->dev,
  589. "%s - failed submitting read urb, error %d\n",
  590. __func__, result);
  591. cypress_set_dead(port);
  592. }
  593. port->port.drain_delay = 256;
  594. return result;
  595. } /* cypress_open */
  596. static void cypress_dtr_rts(struct usb_serial_port *port, int on)
  597. {
  598. struct cypress_private *priv = usb_get_serial_port_data(port);
  599. /* drop dtr and rts */
  600. priv = usb_get_serial_port_data(port);
  601. spin_lock_irq(&priv->lock);
  602. if (on == 0)
  603. priv->line_control = 0;
  604. else
  605. priv->line_control = CONTROL_DTR | CONTROL_RTS;
  606. priv->cmd_ctrl = 1;
  607. spin_unlock_irq(&priv->lock);
  608. cypress_write(NULL, port, NULL, 0);
  609. }
  610. static void cypress_close(struct usb_serial_port *port)
  611. {
  612. struct cypress_private *priv = usb_get_serial_port_data(port);
  613. dbg("%s - port %d", __func__, port->number);
  614. /* writing is potentially harmful, lock must be taken */
  615. mutex_lock(&port->serial->disc_mutex);
  616. if (port->serial->disconnected) {
  617. mutex_unlock(&port->serial->disc_mutex);
  618. return;
  619. }
  620. cypress_buf_clear(priv->buf);
  621. dbg("%s - stopping urbs", __func__);
  622. usb_kill_urb(port->interrupt_in_urb);
  623. usb_kill_urb(port->interrupt_out_urb);
  624. if (stats)
  625. dev_info(&port->dev, "Statistics: %d Bytes In | %d Bytes Out | %d Commands Issued\n",
  626. priv->bytes_in, priv->bytes_out, priv->cmd_count);
  627. mutex_unlock(&port->serial->disc_mutex);
  628. } /* cypress_close */
  629. static int cypress_write(struct tty_struct *tty, struct usb_serial_port *port,
  630. const unsigned char *buf, int count)
  631. {
  632. struct cypress_private *priv = usb_get_serial_port_data(port);
  633. unsigned long flags;
  634. dbg("%s - port %d, %d bytes", __func__, port->number, count);
  635. /* line control commands, which need to be executed immediately,
  636. are not put into the buffer for obvious reasons.
  637. */
  638. if (priv->cmd_ctrl) {
  639. count = 0;
  640. goto finish;
  641. }
  642. if (!count)
  643. return count;
  644. spin_lock_irqsave(&priv->lock, flags);
  645. count = cypress_buf_put(priv->buf, buf, count);
  646. spin_unlock_irqrestore(&priv->lock, flags);
  647. finish:
  648. cypress_send(port);
  649. return count;
  650. } /* cypress_write */
  651. static void cypress_send(struct usb_serial_port *port)
  652. {
  653. int count = 0, result, offset, actual_size;
  654. struct cypress_private *priv = usb_get_serial_port_data(port);
  655. unsigned long flags;
  656. if (!priv->comm_is_ok)
  657. return;
  658. dbg("%s - port %d", __func__, port->number);
  659. dbg("%s - interrupt out size is %d", __func__,
  660. port->interrupt_out_size);
  661. spin_lock_irqsave(&priv->lock, flags);
  662. if (priv->write_urb_in_use) {
  663. dbg("%s - can't write, urb in use", __func__);
  664. spin_unlock_irqrestore(&priv->lock, flags);
  665. return;
  666. }
  667. spin_unlock_irqrestore(&priv->lock, flags);
  668. /* clear buffer */
  669. memset(port->interrupt_out_urb->transfer_buffer, 0,
  670. port->interrupt_out_size);
  671. spin_lock_irqsave(&priv->lock, flags);
  672. switch (priv->pkt_fmt) {
  673. default:
  674. case packet_format_1:
  675. /* this is for the CY7C64013... */
  676. offset = 2;
  677. port->interrupt_out_buffer[0] = priv->line_control;
  678. break;
  679. case packet_format_2:
  680. /* this is for the CY7C63743... */
  681. offset = 1;
  682. port->interrupt_out_buffer[0] = priv->line_control;
  683. break;
  684. }
  685. if (priv->line_control & CONTROL_RESET)
  686. priv->line_control &= ~CONTROL_RESET;
  687. if (priv->cmd_ctrl) {
  688. priv->cmd_count++;
  689. dbg("%s - line control command being issued", __func__);
  690. spin_unlock_irqrestore(&priv->lock, flags);
  691. goto send;
  692. } else
  693. spin_unlock_irqrestore(&priv->lock, flags);
  694. count = cypress_buf_get(priv->buf, &port->interrupt_out_buffer[offset],
  695. port->interrupt_out_size-offset);
  696. if (count == 0)
  697. return;
  698. switch (priv->pkt_fmt) {
  699. default:
  700. case packet_format_1:
  701. port->interrupt_out_buffer[1] = count;
  702. break;
  703. case packet_format_2:
  704. port->interrupt_out_buffer[0] |= count;
  705. }
  706. dbg("%s - count is %d", __func__, count);
  707. send:
  708. spin_lock_irqsave(&priv->lock, flags);
  709. priv->write_urb_in_use = 1;
  710. spin_unlock_irqrestore(&priv->lock, flags);
  711. if (priv->cmd_ctrl)
  712. actual_size = 1;
  713. else
  714. actual_size = count +
  715. (priv->pkt_fmt == packet_format_1 ? 2 : 1);
  716. usb_serial_debug_data(debug, &port->dev, __func__,
  717. port->interrupt_out_size,
  718. port->interrupt_out_urb->transfer_buffer);
  719. usb_fill_int_urb(port->interrupt_out_urb, port->serial->dev,
  720. usb_sndintpipe(port->serial->dev, port->interrupt_out_endpointAddress),
  721. port->interrupt_out_buffer, port->interrupt_out_size,
  722. cypress_write_int_callback, port, priv->write_urb_interval);
  723. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  724. if (result) {
  725. dev_err(&port->dev,
  726. "%s - failed submitting write urb, error %d\n",
  727. __func__, result);
  728. priv->write_urb_in_use = 0;
  729. cypress_set_dead(port);
  730. }
  731. spin_lock_irqsave(&priv->lock, flags);
  732. if (priv->cmd_ctrl)
  733. priv->cmd_ctrl = 0;
  734. /* do not count the line control and size bytes */
  735. priv->bytes_out += count;
  736. spin_unlock_irqrestore(&priv->lock, flags);
  737. usb_serial_port_softint(port);
  738. } /* cypress_send */
  739. /* returns how much space is available in the soft buffer */
  740. static int cypress_write_room(struct tty_struct *tty)
  741. {
  742. struct usb_serial_port *port = tty->driver_data;
  743. struct cypress_private *priv = usb_get_serial_port_data(port);
  744. int room = 0;
  745. unsigned long flags;
  746. dbg("%s - port %d", __func__, port->number);
  747. spin_lock_irqsave(&priv->lock, flags);
  748. room = cypress_buf_space_avail(priv->buf);
  749. spin_unlock_irqrestore(&priv->lock, flags);
  750. dbg("%s - returns %d", __func__, room);
  751. return room;
  752. }
  753. static int cypress_tiocmget(struct tty_struct *tty, struct file *file)
  754. {
  755. struct usb_serial_port *port = tty->driver_data;
  756. struct cypress_private *priv = usb_get_serial_port_data(port);
  757. __u8 status, control;
  758. unsigned int result = 0;
  759. unsigned long flags;
  760. dbg("%s - port %d", __func__, port->number);
  761. spin_lock_irqsave(&priv->lock, flags);
  762. control = priv->line_control;
  763. status = priv->current_status;
  764. spin_unlock_irqrestore(&priv->lock, flags);
  765. result = ((control & CONTROL_DTR) ? TIOCM_DTR : 0)
  766. | ((control & CONTROL_RTS) ? TIOCM_RTS : 0)
  767. | ((status & UART_CTS) ? TIOCM_CTS : 0)
  768. | ((status & UART_DSR) ? TIOCM_DSR : 0)
  769. | ((status & UART_RI) ? TIOCM_RI : 0)
  770. | ((status & UART_CD) ? TIOCM_CD : 0);
  771. dbg("%s - result = %x", __func__, result);
  772. return result;
  773. }
  774. static int cypress_tiocmset(struct tty_struct *tty, struct file *file,
  775. unsigned int set, unsigned int clear)
  776. {
  777. struct usb_serial_port *port = tty->driver_data;
  778. struct cypress_private *priv = usb_get_serial_port_data(port);
  779. unsigned long flags;
  780. dbg("%s - port %d", __func__, port->number);
  781. spin_lock_irqsave(&priv->lock, flags);
  782. if (set & TIOCM_RTS)
  783. priv->line_control |= CONTROL_RTS;
  784. if (set & TIOCM_DTR)
  785. priv->line_control |= CONTROL_DTR;
  786. if (clear & TIOCM_RTS)
  787. priv->line_control &= ~CONTROL_RTS;
  788. if (clear & TIOCM_DTR)
  789. priv->line_control &= ~CONTROL_DTR;
  790. priv->cmd_ctrl = 1;
  791. spin_unlock_irqrestore(&priv->lock, flags);
  792. return cypress_write(tty, port, NULL, 0);
  793. }
  794. static int cypress_ioctl(struct tty_struct *tty, struct file *file,
  795. unsigned int cmd, unsigned long arg)
  796. {
  797. struct usb_serial_port *port = tty->driver_data;
  798. struct cypress_private *priv = usb_get_serial_port_data(port);
  799. dbg("%s - port %d, cmd 0x%.4x", __func__, port->number, cmd);
  800. switch (cmd) {
  801. /* This code comes from drivers/char/serial.c and ftdi_sio.c */
  802. case TIOCMIWAIT:
  803. while (priv != NULL) {
  804. interruptible_sleep_on(&priv->delta_msr_wait);
  805. /* see if a signal did it */
  806. if (signal_pending(current))
  807. return -ERESTARTSYS;
  808. else {
  809. char diff = priv->diff_status;
  810. if (diff == 0)
  811. return -EIO; /* no change => error */
  812. /* consume all events */
  813. priv->diff_status = 0;
  814. /* return 0 if caller wanted to know about
  815. these bits */
  816. if (((arg & TIOCM_RNG) && (diff & UART_RI)) ||
  817. ((arg & TIOCM_DSR) && (diff & UART_DSR)) ||
  818. ((arg & TIOCM_CD) && (diff & UART_CD)) ||
  819. ((arg & TIOCM_CTS) && (diff & UART_CTS)))
  820. return 0;
  821. /* otherwise caller can't care less about what
  822. * happened, and so we continue to wait for
  823. * more events.
  824. */
  825. }
  826. }
  827. return 0;
  828. default:
  829. break;
  830. }
  831. dbg("%s - arg not supported - it was 0x%04x - check include/asm/ioctls.h", __func__, cmd);
  832. return -ENOIOCTLCMD;
  833. } /* cypress_ioctl */
  834. static void cypress_set_termios(struct tty_struct *tty,
  835. struct usb_serial_port *port, struct ktermios *old_termios)
  836. {
  837. struct cypress_private *priv = usb_get_serial_port_data(port);
  838. int data_bits, stop_bits, parity_type, parity_enable;
  839. unsigned cflag, iflag;
  840. unsigned long flags;
  841. __u8 oldlines;
  842. int linechange = 0;
  843. dbg("%s - port %d", __func__, port->number);
  844. spin_lock_irqsave(&priv->lock, flags);
  845. /* We can't clean this one up as we don't know the device type
  846. early enough */
  847. if (!priv->termios_initialized) {
  848. if (priv->chiptype == CT_EARTHMATE) {
  849. *(tty->termios) = tty_std_termios;
  850. tty->termios->c_cflag = B4800 | CS8 | CREAD | HUPCL |
  851. CLOCAL;
  852. tty->termios->c_ispeed = 4800;
  853. tty->termios->c_ospeed = 4800;
  854. } else if (priv->chiptype == CT_CYPHIDCOM) {
  855. *(tty->termios) = tty_std_termios;
  856. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  857. CLOCAL;
  858. tty->termios->c_ispeed = 9600;
  859. tty->termios->c_ospeed = 9600;
  860. } else if (priv->chiptype == CT_CA42V2) {
  861. *(tty->termios) = tty_std_termios;
  862. tty->termios->c_cflag = B9600 | CS8 | CREAD | HUPCL |
  863. CLOCAL;
  864. tty->termios->c_ispeed = 9600;
  865. tty->termios->c_ospeed = 9600;
  866. }
  867. priv->termios_initialized = 1;
  868. }
  869. spin_unlock_irqrestore(&priv->lock, flags);
  870. /* Unsupported features need clearing */
  871. tty->termios->c_cflag &= ~(CMSPAR|CRTSCTS);
  872. cflag = tty->termios->c_cflag;
  873. iflag = tty->termios->c_iflag;
  874. /* check if there are new settings */
  875. if (old_termios) {
  876. spin_lock_irqsave(&priv->lock, flags);
  877. priv->tmp_termios = *(tty->termios);
  878. spin_unlock_irqrestore(&priv->lock, flags);
  879. }
  880. /* set number of data bits, parity, stop bits */
  881. /* when parity is disabled the parity type bit is ignored */
  882. /* 1 means 2 stop bits, 0 means 1 stop bit */
  883. stop_bits = cflag & CSTOPB ? 1 : 0;
  884. if (cflag & PARENB) {
  885. parity_enable = 1;
  886. /* 1 means odd parity, 0 means even parity */
  887. parity_type = cflag & PARODD ? 1 : 0;
  888. } else
  889. parity_enable = parity_type = 0;
  890. switch (cflag & CSIZE) {
  891. case CS5:
  892. data_bits = 0;
  893. break;
  894. case CS6:
  895. data_bits = 1;
  896. break;
  897. case CS7:
  898. data_bits = 2;
  899. break;
  900. case CS8:
  901. data_bits = 3;
  902. break;
  903. default:
  904. dev_err(&port->dev, "%s - CSIZE was set, but not CS5-CS8\n",
  905. __func__);
  906. data_bits = 3;
  907. }
  908. spin_lock_irqsave(&priv->lock, flags);
  909. oldlines = priv->line_control;
  910. if ((cflag & CBAUD) == B0) {
  911. /* drop dtr and rts */
  912. dbg("%s - dropping the lines, baud rate 0bps", __func__);
  913. priv->line_control &= ~(CONTROL_DTR | CONTROL_RTS);
  914. } else
  915. priv->line_control = (CONTROL_DTR | CONTROL_RTS);
  916. spin_unlock_irqrestore(&priv->lock, flags);
  917. dbg("%s - sending %d stop_bits, %d parity_enable, %d parity_type, "
  918. "%d data_bits (+5)", __func__, stop_bits,
  919. parity_enable, parity_type, data_bits);
  920. cypress_serial_control(tty, port, tty_get_baud_rate(tty),
  921. data_bits, stop_bits,
  922. parity_enable, parity_type,
  923. 0, CYPRESS_SET_CONFIG);
  924. /* we perform a CYPRESS_GET_CONFIG so that the current settings are
  925. * filled into the private structure this should confirm that all is
  926. * working if it returns what we just set */
  927. cypress_serial_control(tty, port, 0, 0, 0, 0, 0, 0, CYPRESS_GET_CONFIG);
  928. /* Here we can define custom tty settings for devices; the main tty
  929. * termios flag base comes from empeg.c */
  930. spin_lock_irqsave(&priv->lock, flags);
  931. if (priv->chiptype == CT_EARTHMATE && priv->baud_rate == 4800) {
  932. dbg("Using custom termios settings for a baud rate of "
  933. "4800bps.");
  934. /* define custom termios settings for NMEA protocol */
  935. tty->termios->c_iflag /* input modes - */
  936. &= ~(IGNBRK /* disable ignore break */
  937. | BRKINT /* disable break causes interrupt */
  938. | PARMRK /* disable mark parity errors */
  939. | ISTRIP /* disable clear high bit of input char */
  940. | INLCR /* disable translate NL to CR */
  941. | IGNCR /* disable ignore CR */
  942. | ICRNL /* disable translate CR to NL */
  943. | IXON); /* disable enable XON/XOFF flow control */
  944. tty->termios->c_oflag /* output modes */
  945. &= ~OPOST; /* disable postprocess output char */
  946. tty->termios->c_lflag /* line discipline modes */
  947. &= ~(ECHO /* disable echo input characters */
  948. | ECHONL /* disable echo new line */
  949. | ICANON /* disable erase, kill, werase, and rprnt
  950. special characters */
  951. | ISIG /* disable interrupt, quit, and suspend
  952. special characters */
  953. | IEXTEN); /* disable non-POSIX special characters */
  954. } /* CT_CYPHIDCOM: Application should handle this for device */
  955. linechange = (priv->line_control != oldlines);
  956. spin_unlock_irqrestore(&priv->lock, flags);
  957. /* if necessary, set lines */
  958. if (linechange) {
  959. priv->cmd_ctrl = 1;
  960. cypress_write(tty, port, NULL, 0);
  961. }
  962. } /* cypress_set_termios */
  963. /* returns amount of data still left in soft buffer */
  964. static int cypress_chars_in_buffer(struct tty_struct *tty)
  965. {
  966. struct usb_serial_port *port = tty->driver_data;
  967. struct cypress_private *priv = usb_get_serial_port_data(port);
  968. int chars = 0;
  969. unsigned long flags;
  970. dbg("%s - port %d", __func__, port->number);
  971. spin_lock_irqsave(&priv->lock, flags);
  972. chars = cypress_buf_data_avail(priv->buf);
  973. spin_unlock_irqrestore(&priv->lock, flags);
  974. dbg("%s - returns %d", __func__, chars);
  975. return chars;
  976. }
  977. static void cypress_throttle(struct tty_struct *tty)
  978. {
  979. struct usb_serial_port *port = tty->driver_data;
  980. struct cypress_private *priv = usb_get_serial_port_data(port);
  981. dbg("%s - port %d", __func__, port->number);
  982. spin_lock_irq(&priv->lock);
  983. priv->rx_flags = THROTTLED;
  984. spin_unlock_irq(&priv->lock);
  985. }
  986. static void cypress_unthrottle(struct tty_struct *tty)
  987. {
  988. struct usb_serial_port *port = tty->driver_data;
  989. struct cypress_private *priv = usb_get_serial_port_data(port);
  990. int actually_throttled, result;
  991. dbg("%s - port %d", __func__, port->number);
  992. spin_lock_irq(&priv->lock);
  993. actually_throttled = priv->rx_flags & ACTUALLY_THROTTLED;
  994. priv->rx_flags = 0;
  995. spin_unlock_irq(&priv->lock);
  996. if (!priv->comm_is_ok)
  997. return;
  998. if (actually_throttled) {
  999. port->interrupt_in_urb->dev = port->serial->dev;
  1000. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  1001. if (result) {
  1002. dev_err(&port->dev, "%s - failed submitting read urb, "
  1003. "error %d\n", __func__, result);
  1004. cypress_set_dead(port);
  1005. }
  1006. }
  1007. }
  1008. static void cypress_read_int_callback(struct urb *urb)
  1009. {
  1010. struct usb_serial_port *port = urb->context;
  1011. struct cypress_private *priv = usb_get_serial_port_data(port);
  1012. struct tty_struct *tty;
  1013. unsigned char *data = urb->transfer_buffer;
  1014. unsigned long flags;
  1015. char tty_flag = TTY_NORMAL;
  1016. int havedata = 0;
  1017. int bytes = 0;
  1018. int result;
  1019. int i = 0;
  1020. int status = urb->status;
  1021. dbg("%s - port %d", __func__, port->number);
  1022. switch (status) {
  1023. case 0: /* success */
  1024. break;
  1025. case -ECONNRESET:
  1026. case -ENOENT:
  1027. case -ESHUTDOWN:
  1028. /* precursor to disconnect so just go away */
  1029. return;
  1030. case -EPIPE:
  1031. /* Can't call usb_clear_halt while in_interrupt */
  1032. /* FALLS THROUGH */
  1033. default:
  1034. /* something ugly is going on... */
  1035. dev_err(&urb->dev->dev,
  1036. "%s - unexpected nonzero read status received: %d\n",
  1037. __func__, status);
  1038. cypress_set_dead(port);
  1039. return;
  1040. }
  1041. spin_lock_irqsave(&priv->lock, flags);
  1042. if (priv->rx_flags & THROTTLED) {
  1043. dbg("%s - now throttling", __func__);
  1044. priv->rx_flags |= ACTUALLY_THROTTLED;
  1045. spin_unlock_irqrestore(&priv->lock, flags);
  1046. return;
  1047. }
  1048. spin_unlock_irqrestore(&priv->lock, flags);
  1049. tty = tty_port_tty_get(&port->port);
  1050. if (!tty) {
  1051. dbg("%s - bad tty pointer - exiting", __func__);
  1052. return;
  1053. }
  1054. spin_lock_irqsave(&priv->lock, flags);
  1055. result = urb->actual_length;
  1056. switch (priv->pkt_fmt) {
  1057. default:
  1058. case packet_format_1:
  1059. /* This is for the CY7C64013... */
  1060. priv->current_status = data[0] & 0xF8;
  1061. bytes = data[1] + 2;
  1062. i = 2;
  1063. if (bytes > 2)
  1064. havedata = 1;
  1065. break;
  1066. case packet_format_2:
  1067. /* This is for the CY7C63743... */
  1068. priv->current_status = data[0] & 0xF8;
  1069. bytes = (data[0] & 0x07) + 1;
  1070. i = 1;
  1071. if (bytes > 1)
  1072. havedata = 1;
  1073. break;
  1074. }
  1075. spin_unlock_irqrestore(&priv->lock, flags);
  1076. if (result < bytes) {
  1077. dbg("%s - wrong packet size - received %d bytes but packet "
  1078. "said %d bytes", __func__, result, bytes);
  1079. goto continue_read;
  1080. }
  1081. usb_serial_debug_data(debug, &port->dev, __func__,
  1082. urb->actual_length, data);
  1083. spin_lock_irqsave(&priv->lock, flags);
  1084. /* check to see if status has changed */
  1085. if (priv->current_status != priv->prev_status) {
  1086. priv->diff_status |= priv->current_status ^
  1087. priv->prev_status;
  1088. wake_up_interruptible(&priv->delta_msr_wait);
  1089. priv->prev_status = priv->current_status;
  1090. }
  1091. spin_unlock_irqrestore(&priv->lock, flags);
  1092. /* hangup, as defined in acm.c... this might be a bad place for it
  1093. * though */
  1094. if (tty && !(tty->termios->c_cflag & CLOCAL) &&
  1095. !(priv->current_status & UART_CD)) {
  1096. dbg("%s - calling hangup", __func__);
  1097. tty_hangup(tty);
  1098. goto continue_read;
  1099. }
  1100. /* There is one error bit... I'm assuming it is a parity error
  1101. * indicator as the generic firmware will set this bit to 1 if a
  1102. * parity error occurs.
  1103. * I can not find reference to any other error events. */
  1104. spin_lock_irqsave(&priv->lock, flags);
  1105. if (priv->current_status & CYP_ERROR) {
  1106. spin_unlock_irqrestore(&priv->lock, flags);
  1107. tty_flag = TTY_PARITY;
  1108. dbg("%s - Parity Error detected", __func__);
  1109. } else
  1110. spin_unlock_irqrestore(&priv->lock, flags);
  1111. /* process read if there is data other than line status */
  1112. if (tty && (bytes > i)) {
  1113. bytes = tty_buffer_request_room(tty, bytes);
  1114. for (; i < bytes ; ++i) {
  1115. dbg("pushing byte number %d - %d - %c", i, data[i],
  1116. data[i]);
  1117. tty_insert_flip_char(tty, data[i], tty_flag);
  1118. }
  1119. tty_flip_buffer_push(tty);
  1120. }
  1121. spin_lock_irqsave(&priv->lock, flags);
  1122. /* control and status byte(s) are also counted */
  1123. priv->bytes_in += bytes;
  1124. spin_unlock_irqrestore(&priv->lock, flags);
  1125. continue_read:
  1126. tty_kref_put(tty);
  1127. /* Continue trying to always read... unless the port has closed. */
  1128. if (port->port.count > 0 && priv->comm_is_ok) {
  1129. usb_fill_int_urb(port->interrupt_in_urb, port->serial->dev,
  1130. usb_rcvintpipe(port->serial->dev,
  1131. port->interrupt_in_endpointAddress),
  1132. port->interrupt_in_urb->transfer_buffer,
  1133. port->interrupt_in_urb->transfer_buffer_length,
  1134. cypress_read_int_callback, port,
  1135. priv->read_urb_interval);
  1136. result = usb_submit_urb(port->interrupt_in_urb, GFP_ATOMIC);
  1137. if (result) {
  1138. dev_err(&urb->dev->dev, "%s - failed resubmitting "
  1139. "read urb, error %d\n", __func__,
  1140. result);
  1141. cypress_set_dead(port);
  1142. }
  1143. }
  1144. return;
  1145. } /* cypress_read_int_callback */
  1146. static void cypress_write_int_callback(struct urb *urb)
  1147. {
  1148. struct usb_serial_port *port = urb->context;
  1149. struct cypress_private *priv = usb_get_serial_port_data(port);
  1150. int result;
  1151. int status = urb->status;
  1152. dbg("%s - port %d", __func__, port->number);
  1153. switch (status) {
  1154. case 0:
  1155. /* success */
  1156. break;
  1157. case -ECONNRESET:
  1158. case -ENOENT:
  1159. case -ESHUTDOWN:
  1160. /* this urb is terminated, clean up */
  1161. dbg("%s - urb shutting down with status: %d",
  1162. __func__, status);
  1163. priv->write_urb_in_use = 0;
  1164. return;
  1165. case -EPIPE: /* no break needed; clear halt and resubmit */
  1166. if (!priv->comm_is_ok)
  1167. break;
  1168. usb_clear_halt(port->serial->dev, 0x02);
  1169. /* error in the urb, so we have to resubmit it */
  1170. dbg("%s - nonzero write bulk status received: %d",
  1171. __func__, status);
  1172. port->interrupt_out_urb->transfer_buffer_length = 1;
  1173. port->interrupt_out_urb->dev = port->serial->dev;
  1174. result = usb_submit_urb(port->interrupt_out_urb, GFP_ATOMIC);
  1175. if (!result)
  1176. return;
  1177. dev_err(&urb->dev->dev,
  1178. "%s - failed resubmitting write urb, error %d\n",
  1179. __func__, result);
  1180. cypress_set_dead(port);
  1181. break;
  1182. default:
  1183. dev_err(&urb->dev->dev,
  1184. "%s - unexpected nonzero write status received: %d\n",
  1185. __func__, status);
  1186. cypress_set_dead(port);
  1187. break;
  1188. }
  1189. priv->write_urb_in_use = 0;
  1190. /* send any buffered data */
  1191. cypress_send(port);
  1192. }
  1193. /*****************************************************************************
  1194. * Write buffer functions - buffering code from pl2303 used
  1195. *****************************************************************************/
  1196. /*
  1197. * cypress_buf_alloc
  1198. *
  1199. * Allocate a circular buffer and all associated memory.
  1200. */
  1201. static struct cypress_buf *cypress_buf_alloc(unsigned int size)
  1202. {
  1203. struct cypress_buf *cb;
  1204. if (size == 0)
  1205. return NULL;
  1206. cb = kmalloc(sizeof(struct cypress_buf), GFP_KERNEL);
  1207. if (cb == NULL)
  1208. return NULL;
  1209. cb->buf_buf = kmalloc(size, GFP_KERNEL);
  1210. if (cb->buf_buf == NULL) {
  1211. kfree(cb);
  1212. return NULL;
  1213. }
  1214. cb->buf_size = size;
  1215. cb->buf_get = cb->buf_put = cb->buf_buf;
  1216. return cb;
  1217. }
  1218. /*
  1219. * cypress_buf_free
  1220. *
  1221. * Free the buffer and all associated memory.
  1222. */
  1223. static void cypress_buf_free(struct cypress_buf *cb)
  1224. {
  1225. if (cb) {
  1226. kfree(cb->buf_buf);
  1227. kfree(cb);
  1228. }
  1229. }
  1230. /*
  1231. * cypress_buf_clear
  1232. *
  1233. * Clear out all data in the circular buffer.
  1234. */
  1235. static void cypress_buf_clear(struct cypress_buf *cb)
  1236. {
  1237. if (cb != NULL)
  1238. cb->buf_get = cb->buf_put;
  1239. /* equivalent to a get of all data available */
  1240. }
  1241. /*
  1242. * cypress_buf_data_avail
  1243. *
  1244. * Return the number of bytes of data available in the circular
  1245. * buffer.
  1246. */
  1247. static unsigned int cypress_buf_data_avail(struct cypress_buf *cb)
  1248. {
  1249. if (cb != NULL)
  1250. return (cb->buf_size + cb->buf_put - cb->buf_get)
  1251. % cb->buf_size;
  1252. else
  1253. return 0;
  1254. }
  1255. /*
  1256. * cypress_buf_space_avail
  1257. *
  1258. * Return the number of bytes of space available in the circular
  1259. * buffer.
  1260. */
  1261. static unsigned int cypress_buf_space_avail(struct cypress_buf *cb)
  1262. {
  1263. if (cb != NULL)
  1264. return (cb->buf_size + cb->buf_get - cb->buf_put - 1)
  1265. % cb->buf_size;
  1266. else
  1267. return 0;
  1268. }
  1269. /*
  1270. * cypress_buf_put
  1271. *
  1272. * Copy data data from a user buffer and put it into the circular buffer.
  1273. * Restrict to the amount of space available.
  1274. *
  1275. * Return the number of bytes copied.
  1276. */
  1277. static unsigned int cypress_buf_put(struct cypress_buf *cb, const char *buf,
  1278. unsigned int count)
  1279. {
  1280. unsigned int len;
  1281. if (cb == NULL)
  1282. return 0;
  1283. len = cypress_buf_space_avail(cb);
  1284. if (count > len)
  1285. count = len;
  1286. if (count == 0)
  1287. return 0;
  1288. len = cb->buf_buf + cb->buf_size - cb->buf_put;
  1289. if (count > len) {
  1290. memcpy(cb->buf_put, buf, len);
  1291. memcpy(cb->buf_buf, buf+len, count - len);
  1292. cb->buf_put = cb->buf_buf + count - len;
  1293. } else {
  1294. memcpy(cb->buf_put, buf, count);
  1295. if (count < len)
  1296. cb->buf_put += count;
  1297. else /* count == len */
  1298. cb->buf_put = cb->buf_buf;
  1299. }
  1300. return count;
  1301. }
  1302. /*
  1303. * cypress_buf_get
  1304. *
  1305. * Get data from the circular buffer and copy to the given buffer.
  1306. * Restrict to the amount of data available.
  1307. *
  1308. * Return the number of bytes copied.
  1309. */
  1310. static unsigned int cypress_buf_get(struct cypress_buf *cb, char *buf,
  1311. unsigned int count)
  1312. {
  1313. unsigned int len;
  1314. if (cb == NULL)
  1315. return 0;
  1316. len = cypress_buf_data_avail(cb);
  1317. if (count > len)
  1318. count = len;
  1319. if (count == 0)
  1320. return 0;
  1321. len = cb->buf_buf + cb->buf_size - cb->buf_get;
  1322. if (count > len) {
  1323. memcpy(buf, cb->buf_get, len);
  1324. memcpy(buf+len, cb->buf_buf, count - len);
  1325. cb->buf_get = cb->buf_buf + count - len;
  1326. } else {
  1327. memcpy(buf, cb->buf_get, count);
  1328. if (count < len)
  1329. cb->buf_get += count;
  1330. else /* count == len */
  1331. cb->buf_get = cb->buf_buf;
  1332. }
  1333. return count;
  1334. }
  1335. /*****************************************************************************
  1336. * Module functions
  1337. *****************************************************************************/
  1338. static int __init cypress_init(void)
  1339. {
  1340. int retval;
  1341. dbg("%s", __func__);
  1342. retval = usb_serial_register(&cypress_earthmate_device);
  1343. if (retval)
  1344. goto failed_em_register;
  1345. retval = usb_serial_register(&cypress_hidcom_device);
  1346. if (retval)
  1347. goto failed_hidcom_register;
  1348. retval = usb_serial_register(&cypress_ca42v2_device);
  1349. if (retval)
  1350. goto failed_ca42v2_register;
  1351. retval = usb_register(&cypress_driver);
  1352. if (retval)
  1353. goto failed_usb_register;
  1354. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1355. DRIVER_DESC "\n");
  1356. return 0;
  1357. failed_usb_register:
  1358. usb_serial_deregister(&cypress_ca42v2_device);
  1359. failed_ca42v2_register:
  1360. usb_serial_deregister(&cypress_hidcom_device);
  1361. failed_hidcom_register:
  1362. usb_serial_deregister(&cypress_earthmate_device);
  1363. failed_em_register:
  1364. return retval;
  1365. }
  1366. static void __exit cypress_exit(void)
  1367. {
  1368. dbg("%s", __func__);
  1369. usb_deregister(&cypress_driver);
  1370. usb_serial_deregister(&cypress_earthmate_device);
  1371. usb_serial_deregister(&cypress_hidcom_device);
  1372. usb_serial_deregister(&cypress_ca42v2_device);
  1373. }
  1374. module_init(cypress_init);
  1375. module_exit(cypress_exit);
  1376. MODULE_AUTHOR(DRIVER_AUTHOR);
  1377. MODULE_DESCRIPTION(DRIVER_DESC);
  1378. MODULE_VERSION(DRIVER_VERSION);
  1379. MODULE_LICENSE("GPL");
  1380. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1381. MODULE_PARM_DESC(debug, "Debug enabled or not");
  1382. module_param(stats, bool, S_IRUGO | S_IWUSR);
  1383. MODULE_PARM_DESC(stats, "Enable statistics or not");
  1384. module_param(interval, int, S_IRUGO | S_IWUSR);
  1385. MODULE_PARM_DESC(interval, "Overrides interrupt interval");