mos7720.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774
  1. /*
  2. * mos7720.c
  3. * Controls the Moschip 7720 usb to dual port serial convertor
  4. *
  5. * Copyright 2006 Moschip Semiconductor Tech. Ltd.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, version 2 of the License.
  10. *
  11. * Developed by:
  12. * Vijaya Kumar <vijaykumar.gn@gmail.com>
  13. * Ajay Kumar <naanuajay@yahoo.com>
  14. * Gurudeva <ngurudeva@yahoo.com>
  15. *
  16. * Cleaned up from the original by:
  17. * Greg Kroah-Hartman <gregkh@suse.de>
  18. *
  19. * Originally based on drivers/usb/serial/io_edgeport.c which is:
  20. * Copyright (C) 2000 Inside Out Networks, All rights reserved.
  21. * Copyright (C) 2001-2002 Greg Kroah-Hartman <greg@kroah.com>
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/errno.h>
  25. #include <linux/init.h>
  26. #include <linux/slab.h>
  27. #include <linux/tty.h>
  28. #include <linux/tty_driver.h>
  29. #include <linux/tty_flip.h>
  30. #include <linux/module.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/serial.h>
  33. #include <linux/serial_reg.h>
  34. #include <linux/usb.h>
  35. #include <linux/usb/serial.h>
  36. #include <linux/uaccess.h>
  37. /*
  38. * Version Information
  39. */
  40. #define DRIVER_VERSION "1.0.0.4F"
  41. #define DRIVER_AUTHOR "Aspire Communications pvt Ltd."
  42. #define DRIVER_DESC "Moschip USB Serial Driver"
  43. /* default urb timeout */
  44. #define MOS_WDR_TIMEOUT (HZ * 5)
  45. #define MOS_PORT1 0x0200
  46. #define MOS_PORT2 0x0300
  47. #define MOS_VENREG 0x0000
  48. #define MOS_MAX_PORT 0x02
  49. #define MOS_WRITE 0x0E
  50. #define MOS_READ 0x0D
  51. /* Interrupt Rotinue Defines */
  52. #define SERIAL_IIR_RLS 0x06
  53. #define SERIAL_IIR_RDA 0x04
  54. #define SERIAL_IIR_CTI 0x0c
  55. #define SERIAL_IIR_THR 0x02
  56. #define SERIAL_IIR_MS 0x00
  57. #define NUM_URBS 16 /* URB Count */
  58. #define URB_TRANSFER_BUFFER_SIZE 32 /* URB Size */
  59. /* This structure holds all of the local port information */
  60. struct moschip_port {
  61. __u8 shadowLCR; /* last LCR value received */
  62. __u8 shadowMCR; /* last MCR value received */
  63. __u8 shadowMSR; /* last MSR value received */
  64. char open;
  65. struct async_icount icount;
  66. struct usb_serial_port *port; /* loop back to the owner */
  67. struct urb *write_urb_pool[NUM_URBS];
  68. };
  69. /* This structure holds all of the individual serial device information */
  70. struct moschip_serial {
  71. int interrupt_started;
  72. };
  73. static int debug;
  74. static struct usb_serial_driver moschip7720_2port_driver;
  75. #define USB_VENDOR_ID_MOSCHIP 0x9710
  76. #define MOSCHIP_DEVICE_ID_7720 0x7720
  77. #define MOSCHIP_DEVICE_ID_7715 0x7715
  78. static const struct usb_device_id moschip_port_id_table[] = {
  79. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7720) },
  80. { USB_DEVICE(USB_VENDOR_ID_MOSCHIP, MOSCHIP_DEVICE_ID_7715) },
  81. { } /* terminating entry */
  82. };
  83. MODULE_DEVICE_TABLE(usb, moschip_port_id_table);
  84. /*
  85. * mos7720_interrupt_callback
  86. * this is the callback function for when we have received data on the
  87. * interrupt endpoint.
  88. */
  89. static void mos7720_interrupt_callback(struct urb *urb)
  90. {
  91. int result;
  92. int length;
  93. int status = urb->status;
  94. __u8 *data;
  95. __u8 sp1;
  96. __u8 sp2;
  97. dbg(" : Entering");
  98. switch (status) {
  99. case 0:
  100. /* success */
  101. break;
  102. case -ECONNRESET:
  103. case -ENOENT:
  104. case -ESHUTDOWN:
  105. /* this urb is terminated, clean up */
  106. dbg("%s - urb shutting down with status: %d", __func__,
  107. status);
  108. return;
  109. default:
  110. dbg("%s - nonzero urb status received: %d", __func__,
  111. status);
  112. goto exit;
  113. }
  114. length = urb->actual_length;
  115. data = urb->transfer_buffer;
  116. /* Moschip get 4 bytes
  117. * Byte 1 IIR Port 1 (port.number is 0)
  118. * Byte 2 IIR Port 2 (port.number is 1)
  119. * Byte 3 --------------
  120. * Byte 4 FIFO status for both */
  121. /* the above description is inverted
  122. * oneukum 2007-03-14 */
  123. if (unlikely(length != 4)) {
  124. dbg("Wrong data !!!");
  125. return;
  126. }
  127. sp1 = data[3];
  128. sp2 = data[2];
  129. if ((sp1 | sp2) & 0x01) {
  130. /* No Interrupt Pending in both the ports */
  131. dbg("No Interrupt !!!");
  132. } else {
  133. switch (sp1 & 0x0f) {
  134. case SERIAL_IIR_RLS:
  135. dbg("Serial Port 1: Receiver status error or address "
  136. "bit detected in 9-bit mode\n");
  137. break;
  138. case SERIAL_IIR_CTI:
  139. dbg("Serial Port 1: Receiver time out");
  140. break;
  141. case SERIAL_IIR_MS:
  142. dbg("Serial Port 1: Modem status change");
  143. break;
  144. }
  145. switch (sp2 & 0x0f) {
  146. case SERIAL_IIR_RLS:
  147. dbg("Serial Port 2: Receiver status error or address "
  148. "bit detected in 9-bit mode");
  149. break;
  150. case SERIAL_IIR_CTI:
  151. dbg("Serial Port 2: Receiver time out");
  152. break;
  153. case SERIAL_IIR_MS:
  154. dbg("Serial Port 2: Modem status change");
  155. break;
  156. }
  157. }
  158. exit:
  159. result = usb_submit_urb(urb, GFP_ATOMIC);
  160. if (result)
  161. dev_err(&urb->dev->dev,
  162. "%s - Error %d submitting control urb\n",
  163. __func__, result);
  164. return;
  165. }
  166. /*
  167. * mos7715_interrupt_callback
  168. * this is the 7715's callback function for when we have received data on
  169. * the interrupt endpoint.
  170. */
  171. static void mos7715_interrupt_callback(struct urb *urb)
  172. {
  173. int result;
  174. int length;
  175. int status = urb->status;
  176. __u8 *data;
  177. __u8 iir;
  178. switch (status) {
  179. case 0:
  180. /* success */
  181. break;
  182. case -ECONNRESET:
  183. case -ENOENT:
  184. case -ESHUTDOWN:
  185. /* this urb is terminated, clean up */
  186. dbg("%s - urb shutting down with status: %d", __func__,
  187. status);
  188. return;
  189. default:
  190. dbg("%s - nonzero urb status received: %d", __func__,
  191. status);
  192. goto exit;
  193. }
  194. length = urb->actual_length;
  195. data = urb->transfer_buffer;
  196. /* Structure of data from 7715 device:
  197. * Byte 1: IIR serial Port
  198. * Byte 2: unused
  199. * Byte 2: DSR parallel port
  200. * Byte 4: FIFO status for both */
  201. if (unlikely(length != 4)) {
  202. dbg("Wrong data !!!");
  203. return;
  204. }
  205. iir = data[0];
  206. if (!(iir & 0x01)) { /* serial port interrupt pending */
  207. switch (iir & 0x0f) {
  208. case SERIAL_IIR_RLS:
  209. dbg("Serial Port: Receiver status error or address "
  210. "bit detected in 9-bit mode\n");
  211. break;
  212. case SERIAL_IIR_CTI:
  213. dbg("Serial Port: Receiver time out");
  214. break;
  215. case SERIAL_IIR_MS:
  216. dbg("Serial Port: Modem status change");
  217. break;
  218. }
  219. }
  220. exit:
  221. result = usb_submit_urb(urb, GFP_ATOMIC);
  222. if (result)
  223. dev_err(&urb->dev->dev,
  224. "%s - Error %d submitting control urb\n",
  225. __func__, result);
  226. return;
  227. }
  228. /*
  229. * mos7720_bulk_in_callback
  230. * this is the callback function for when we have received data on the
  231. * bulk in endpoint.
  232. */
  233. static void mos7720_bulk_in_callback(struct urb *urb)
  234. {
  235. int retval;
  236. unsigned char *data ;
  237. struct usb_serial_port *port;
  238. struct moschip_port *mos7720_port;
  239. struct tty_struct *tty;
  240. int status = urb->status;
  241. if (status) {
  242. dbg("nonzero read bulk status received: %d", status);
  243. return;
  244. }
  245. mos7720_port = urb->context;
  246. if (!mos7720_port) {
  247. dbg("NULL mos7720_port pointer");
  248. return ;
  249. }
  250. port = mos7720_port->port;
  251. dbg("Entering...%s", __func__);
  252. data = urb->transfer_buffer;
  253. tty = tty_port_tty_get(&port->port);
  254. if (tty && urb->actual_length) {
  255. tty_insert_flip_string(tty, data, urb->actual_length);
  256. tty_flip_buffer_push(tty);
  257. }
  258. tty_kref_put(tty);
  259. if (!port->read_urb) {
  260. dbg("URB KILLED !!!");
  261. return;
  262. }
  263. if (port->read_urb->status != -EINPROGRESS) {
  264. port->read_urb->dev = port->serial->dev;
  265. retval = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  266. if (retval)
  267. dbg("usb_submit_urb(read bulk) failed, retval = %d",
  268. retval);
  269. }
  270. }
  271. /*
  272. * mos7720_bulk_out_data_callback
  273. * this is the callback function for when we have finished sending serial
  274. * data on the bulk out endpoint.
  275. */
  276. static void mos7720_bulk_out_data_callback(struct urb *urb)
  277. {
  278. struct moschip_port *mos7720_port;
  279. struct tty_struct *tty;
  280. int status = urb->status;
  281. if (status) {
  282. dbg("nonzero write bulk status received:%d", status);
  283. return;
  284. }
  285. mos7720_port = urb->context;
  286. if (!mos7720_port) {
  287. dbg("NULL mos7720_port pointer");
  288. return ;
  289. }
  290. dbg("Entering .........");
  291. tty = tty_port_tty_get(&mos7720_port->port->port);
  292. if (tty && mos7720_port->open)
  293. tty_wakeup(tty);
  294. tty_kref_put(tty);
  295. }
  296. /*
  297. * send_mos_cmd
  298. * this function will be used for sending command to device
  299. */
  300. static int send_mos_cmd(struct usb_serial *serial, __u8 request, __u16 value,
  301. __u16 index, u8 *data)
  302. {
  303. int status;
  304. u8 *buf;
  305. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  306. if (value < MOS_MAX_PORT) {
  307. if (product == MOSCHIP_DEVICE_ID_7715)
  308. value = 0x0200; /* identifies the 7715's serial port */
  309. else
  310. value = value*0x100+0x200;
  311. } else {
  312. value = 0x0000;
  313. if ((product == MOSCHIP_DEVICE_ID_7715) &&
  314. (index != 0x08)) {
  315. dbg("serial->product== MOSCHIP_DEVICE_ID_7715");
  316. /* index = 0x01 ; */
  317. }
  318. }
  319. if (request == MOS_WRITE) {
  320. value = value + *data;
  321. status = usb_control_msg(serial->dev,
  322. usb_sndctrlpipe(serial->dev, 0), MOS_WRITE,
  323. 0x40, value, index, NULL, 0, MOS_WDR_TIMEOUT);
  324. } else {
  325. buf = kmalloc(1, GFP_KERNEL);
  326. if (!buf) {
  327. status = -ENOMEM;
  328. goto out;
  329. }
  330. status = usb_control_msg(serial->dev,
  331. usb_rcvctrlpipe(serial->dev, 0), MOS_READ,
  332. 0xc0, value, index, buf, 1, MOS_WDR_TIMEOUT);
  333. *data = *buf;
  334. kfree(buf);
  335. }
  336. out:
  337. if (status < 0)
  338. dbg("Command Write failed Value %x index %x", value, index);
  339. return status;
  340. }
  341. /*
  342. * mos77xx_probe
  343. * this function installs the appropriate read interrupt endpoint callback
  344. * depending on whether the device is a 7720 or 7715, thus avoiding costly
  345. * run-time checks in the high-frequency callback routine itself.
  346. */
  347. static int mos77xx_probe(struct usb_serial *serial,
  348. const struct usb_device_id *id)
  349. {
  350. if (id->idProduct == MOSCHIP_DEVICE_ID_7715)
  351. moschip7720_2port_driver.read_int_callback =
  352. mos7715_interrupt_callback;
  353. else
  354. moschip7720_2port_driver.read_int_callback =
  355. mos7720_interrupt_callback;
  356. return 0;
  357. }
  358. static int mos77xx_calc_num_ports(struct usb_serial *serial)
  359. {
  360. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  361. if (product == MOSCHIP_DEVICE_ID_7715)
  362. return 1;
  363. return 2;
  364. }
  365. static int mos7720_open(struct tty_struct *tty, struct usb_serial_port *port)
  366. {
  367. struct usb_serial *serial;
  368. struct usb_serial_port *port0;
  369. struct urb *urb;
  370. struct moschip_serial *mos7720_serial;
  371. struct moschip_port *mos7720_port;
  372. int response;
  373. int port_number;
  374. char data;
  375. int allocated_urbs = 0;
  376. int j;
  377. serial = port->serial;
  378. mos7720_port = usb_get_serial_port_data(port);
  379. if (mos7720_port == NULL)
  380. return -ENODEV;
  381. port0 = serial->port[0];
  382. mos7720_serial = usb_get_serial_data(serial);
  383. if (mos7720_serial == NULL || port0 == NULL)
  384. return -ENODEV;
  385. usb_clear_halt(serial->dev, port->write_urb->pipe);
  386. usb_clear_halt(serial->dev, port->read_urb->pipe);
  387. /* Initialising the write urb pool */
  388. for (j = 0; j < NUM_URBS; ++j) {
  389. urb = usb_alloc_urb(0, GFP_KERNEL);
  390. mos7720_port->write_urb_pool[j] = urb;
  391. if (urb == NULL) {
  392. dev_err(&port->dev, "No more urbs???\n");
  393. continue;
  394. }
  395. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  396. GFP_KERNEL);
  397. if (!urb->transfer_buffer) {
  398. dev_err(&port->dev,
  399. "%s-out of memory for urb buffers.\n",
  400. __func__);
  401. usb_free_urb(mos7720_port->write_urb_pool[j]);
  402. mos7720_port->write_urb_pool[j] = NULL;
  403. continue;
  404. }
  405. allocated_urbs++;
  406. }
  407. if (!allocated_urbs)
  408. return -ENOMEM;
  409. /* Initialize MCS7720 -- Write Init values to corresponding Registers
  410. *
  411. * Register Index
  412. * 0 : THR/RHR
  413. * 1 : IER
  414. * 2 : FCR
  415. * 3 : LCR
  416. * 4 : MCR
  417. * 5 : LSR
  418. * 6 : MSR
  419. * 7 : SPR
  420. *
  421. * 0x08 : SP1/2 Control Reg
  422. */
  423. port_number = port->number - port->serial->minor;
  424. send_mos_cmd(port->serial, MOS_READ, port_number, UART_LSR, &data);
  425. dbg("SS::%p LSR:%x", mos7720_port, data);
  426. dbg("Check:Sending Command ..........");
  427. data = 0x02;
  428. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x01, &data);
  429. data = 0x02;
  430. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x02, &data);
  431. data = 0x00;
  432. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  433. data = 0x00;
  434. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  435. data = 0xCF;
  436. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  437. data = 0x03;
  438. mos7720_port->shadowLCR = data;
  439. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  440. data = 0x0b;
  441. mos7720_port->shadowMCR = data;
  442. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  443. data = 0x0b;
  444. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  445. data = 0x00;
  446. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  447. data = 0x00;
  448. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  449. /* data = 0x00;
  450. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, port_number + 1, &data);
  451. data = 0x03;
  452. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  453. data = 0x00;
  454. send_mos_cmd(port->serial, MOS_WRITE, MOS_MAX_PORT,
  455. port_number + 1, &data);
  456. */
  457. data = 0x00;
  458. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  459. data = data | (port->number - port->serial->minor + 1);
  460. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  461. data = 0x83;
  462. mos7720_port->shadowLCR = data;
  463. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  464. data = 0x0c;
  465. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  466. data = 0x00;
  467. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  468. data = 0x03;
  469. mos7720_port->shadowLCR = data;
  470. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  471. data = 0x0c;
  472. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  473. data = 0x0c;
  474. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  475. /* see if we've set up our endpoint info yet *
  476. * (can't set it up in mos7720_startup as the *
  477. * structures were not set up at that time.) */
  478. if (!mos7720_serial->interrupt_started) {
  479. dbg("Interrupt buffer NULL !!!");
  480. /* not set up yet, so do it now */
  481. mos7720_serial->interrupt_started = 1;
  482. dbg("To Submit URB !!!");
  483. /* set up our interrupt urb */
  484. usb_fill_int_urb(port0->interrupt_in_urb, serial->dev,
  485. usb_rcvintpipe(serial->dev,
  486. port->interrupt_in_endpointAddress),
  487. port0->interrupt_in_buffer,
  488. port0->interrupt_in_urb->transfer_buffer_length,
  489. mos7720_interrupt_callback, mos7720_port,
  490. port0->interrupt_in_urb->interval);
  491. /* start interrupt read for this mos7720 this interrupt *
  492. * will continue as long as the mos7720 is connected */
  493. dbg("Submit URB over !!!");
  494. response = usb_submit_urb(port0->interrupt_in_urb, GFP_KERNEL);
  495. if (response)
  496. dev_err(&port->dev,
  497. "%s - Error %d submitting control urb\n",
  498. __func__, response);
  499. }
  500. /* set up our bulk in urb */
  501. usb_fill_bulk_urb(port->read_urb, serial->dev,
  502. usb_rcvbulkpipe(serial->dev,
  503. port->bulk_in_endpointAddress),
  504. port->bulk_in_buffer,
  505. port->read_urb->transfer_buffer_length,
  506. mos7720_bulk_in_callback, mos7720_port);
  507. response = usb_submit_urb(port->read_urb, GFP_KERNEL);
  508. if (response)
  509. dev_err(&port->dev, "%s - Error %d submitting read urb\n",
  510. __func__, response);
  511. /* initialize our icount structure */
  512. memset(&(mos7720_port->icount), 0x00, sizeof(mos7720_port->icount));
  513. /* initialize our port settings */
  514. mos7720_port->shadowMCR = UART_MCR_OUT2; /* Must set to enable ints! */
  515. /* send a open port command */
  516. mos7720_port->open = 1;
  517. return 0;
  518. }
  519. /*
  520. * mos7720_chars_in_buffer
  521. * this function is called by the tty driver when it wants to know how many
  522. * bytes of data we currently have outstanding in the port (data that has
  523. * been written, but hasn't made it out the port yet)
  524. * If successful, we return the number of bytes left to be written in the
  525. * system,
  526. * Otherwise we return a negative error number.
  527. */
  528. static int mos7720_chars_in_buffer(struct tty_struct *tty)
  529. {
  530. struct usb_serial_port *port = tty->driver_data;
  531. int i;
  532. int chars = 0;
  533. struct moschip_port *mos7720_port;
  534. dbg("%s:entering ...........", __func__);
  535. mos7720_port = usb_get_serial_port_data(port);
  536. if (mos7720_port == NULL) {
  537. dbg("%s:leaving ...........", __func__);
  538. return 0;
  539. }
  540. for (i = 0; i < NUM_URBS; ++i) {
  541. if (mos7720_port->write_urb_pool[i] &&
  542. mos7720_port->write_urb_pool[i]->status == -EINPROGRESS)
  543. chars += URB_TRANSFER_BUFFER_SIZE;
  544. }
  545. dbg("%s - returns %d", __func__, chars);
  546. return chars;
  547. }
  548. static void mos7720_close(struct usb_serial_port *port)
  549. {
  550. struct usb_serial *serial;
  551. struct moschip_port *mos7720_port;
  552. char data;
  553. int j;
  554. dbg("mos7720_close:entering...");
  555. serial = port->serial;
  556. mos7720_port = usb_get_serial_port_data(port);
  557. if (mos7720_port == NULL)
  558. return;
  559. for (j = 0; j < NUM_URBS; ++j)
  560. usb_kill_urb(mos7720_port->write_urb_pool[j]);
  561. /* Freeing Write URBs */
  562. for (j = 0; j < NUM_URBS; ++j) {
  563. if (mos7720_port->write_urb_pool[j]) {
  564. kfree(mos7720_port->write_urb_pool[j]->transfer_buffer);
  565. usb_free_urb(mos7720_port->write_urb_pool[j]);
  566. }
  567. }
  568. /* While closing port, shutdown all bulk read, write *
  569. * and interrupt read if they exists, otherwise nop */
  570. dbg("Shutdown bulk write");
  571. usb_kill_urb(port->write_urb);
  572. dbg("Shutdown bulk read");
  573. usb_kill_urb(port->read_urb);
  574. mutex_lock(&serial->disc_mutex);
  575. /* these commands must not be issued if the device has
  576. * been disconnected */
  577. if (!serial->disconnected) {
  578. data = 0x00;
  579. send_mos_cmd(serial, MOS_WRITE,
  580. port->number - port->serial->minor, 0x04, &data);
  581. data = 0x00;
  582. send_mos_cmd(serial, MOS_WRITE,
  583. port->number - port->serial->minor, 0x01, &data);
  584. }
  585. mutex_unlock(&serial->disc_mutex);
  586. mos7720_port->open = 0;
  587. dbg("Leaving %s", __func__);
  588. }
  589. static void mos7720_break(struct tty_struct *tty, int break_state)
  590. {
  591. struct usb_serial_port *port = tty->driver_data;
  592. unsigned char data;
  593. struct usb_serial *serial;
  594. struct moschip_port *mos7720_port;
  595. dbg("Entering %s", __func__);
  596. serial = port->serial;
  597. mos7720_port = usb_get_serial_port_data(port);
  598. if (mos7720_port == NULL)
  599. return;
  600. if (break_state == -1)
  601. data = mos7720_port->shadowLCR | UART_LCR_SBC;
  602. else
  603. data = mos7720_port->shadowLCR & ~UART_LCR_SBC;
  604. mos7720_port->shadowLCR = data;
  605. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  606. 0x03, &data);
  607. return;
  608. }
  609. /*
  610. * mos7720_write_room
  611. * this function is called by the tty driver when it wants to know how many
  612. * bytes of data we can accept for a specific port.
  613. * If successful, we return the amount of room that we have for this port
  614. * Otherwise we return a negative error number.
  615. */
  616. static int mos7720_write_room(struct tty_struct *tty)
  617. {
  618. struct usb_serial_port *port = tty->driver_data;
  619. struct moschip_port *mos7720_port;
  620. int room = 0;
  621. int i;
  622. dbg("%s:entering ...........", __func__);
  623. mos7720_port = usb_get_serial_port_data(port);
  624. if (mos7720_port == NULL) {
  625. dbg("%s:leaving ...........", __func__);
  626. return -ENODEV;
  627. }
  628. /* FIXME: Locking */
  629. for (i = 0; i < NUM_URBS; ++i) {
  630. if (mos7720_port->write_urb_pool[i] &&
  631. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS)
  632. room += URB_TRANSFER_BUFFER_SIZE;
  633. }
  634. dbg("%s - returns %d", __func__, room);
  635. return room;
  636. }
  637. static int mos7720_write(struct tty_struct *tty, struct usb_serial_port *port,
  638. const unsigned char *data, int count)
  639. {
  640. int status;
  641. int i;
  642. int bytes_sent = 0;
  643. int transfer_size;
  644. struct moschip_port *mos7720_port;
  645. struct usb_serial *serial;
  646. struct urb *urb;
  647. const unsigned char *current_position = data;
  648. dbg("%s:entering ...........", __func__);
  649. serial = port->serial;
  650. mos7720_port = usb_get_serial_port_data(port);
  651. if (mos7720_port == NULL) {
  652. dbg("mos7720_port is NULL");
  653. return -ENODEV;
  654. }
  655. /* try to find a free urb in the list */
  656. urb = NULL;
  657. for (i = 0; i < NUM_URBS; ++i) {
  658. if (mos7720_port->write_urb_pool[i] &&
  659. mos7720_port->write_urb_pool[i]->status != -EINPROGRESS) {
  660. urb = mos7720_port->write_urb_pool[i];
  661. dbg("URB:%d", i);
  662. break;
  663. }
  664. }
  665. if (urb == NULL) {
  666. dbg("%s - no more free urbs", __func__);
  667. goto exit;
  668. }
  669. if (urb->transfer_buffer == NULL) {
  670. urb->transfer_buffer = kmalloc(URB_TRANSFER_BUFFER_SIZE,
  671. GFP_KERNEL);
  672. if (urb->transfer_buffer == NULL) {
  673. dev_err(&port->dev, "%s no more kernel memory...\n",
  674. __func__);
  675. goto exit;
  676. }
  677. }
  678. transfer_size = min(count, URB_TRANSFER_BUFFER_SIZE);
  679. memcpy(urb->transfer_buffer, current_position, transfer_size);
  680. usb_serial_debug_data(debug, &port->dev, __func__, transfer_size,
  681. urb->transfer_buffer);
  682. /* fill urb with data and submit */
  683. usb_fill_bulk_urb(urb, serial->dev,
  684. usb_sndbulkpipe(serial->dev,
  685. port->bulk_out_endpointAddress),
  686. urb->transfer_buffer, transfer_size,
  687. mos7720_bulk_out_data_callback, mos7720_port);
  688. /* send it down the pipe */
  689. status = usb_submit_urb(urb, GFP_ATOMIC);
  690. if (status) {
  691. dev_err(&port->dev, "%s - usb_submit_urb(write bulk) failed "
  692. "with status = %d\n", __func__, status);
  693. bytes_sent = status;
  694. goto exit;
  695. }
  696. bytes_sent = transfer_size;
  697. exit:
  698. return bytes_sent;
  699. }
  700. static void mos7720_throttle(struct tty_struct *tty)
  701. {
  702. struct usb_serial_port *port = tty->driver_data;
  703. struct moschip_port *mos7720_port;
  704. int status;
  705. dbg("%s- port %d", __func__, port->number);
  706. mos7720_port = usb_get_serial_port_data(port);
  707. if (mos7720_port == NULL)
  708. return;
  709. if (!mos7720_port->open) {
  710. dbg("port not opened");
  711. return;
  712. }
  713. dbg("%s: Entering ..........", __func__);
  714. /* if we are implementing XON/XOFF, send the stop character */
  715. if (I_IXOFF(tty)) {
  716. unsigned char stop_char = STOP_CHAR(tty);
  717. status = mos7720_write(tty, port, &stop_char, 1);
  718. if (status <= 0)
  719. return;
  720. }
  721. /* if we are implementing RTS/CTS, toggle that line */
  722. if (tty->termios->c_cflag & CRTSCTS) {
  723. mos7720_port->shadowMCR &= ~UART_MCR_RTS;
  724. status = send_mos_cmd(port->serial, MOS_WRITE,
  725. port->number - port->serial->minor,
  726. UART_MCR, &mos7720_port->shadowMCR);
  727. if (status != 0)
  728. return;
  729. }
  730. }
  731. static void mos7720_unthrottle(struct tty_struct *tty)
  732. {
  733. struct usb_serial_port *port = tty->driver_data;
  734. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  735. int status;
  736. if (mos7720_port == NULL)
  737. return;
  738. if (!mos7720_port->open) {
  739. dbg("%s - port not opened", __func__);
  740. return;
  741. }
  742. dbg("%s: Entering ..........", __func__);
  743. /* if we are implementing XON/XOFF, send the start character */
  744. if (I_IXOFF(tty)) {
  745. unsigned char start_char = START_CHAR(tty);
  746. status = mos7720_write(tty, port, &start_char, 1);
  747. if (status <= 0)
  748. return;
  749. }
  750. /* if we are implementing RTS/CTS, toggle that line */
  751. if (tty->termios->c_cflag & CRTSCTS) {
  752. mos7720_port->shadowMCR |= UART_MCR_RTS;
  753. status = send_mos_cmd(port->serial, MOS_WRITE,
  754. port->number - port->serial->minor,
  755. UART_MCR, &mos7720_port->shadowMCR);
  756. if (status != 0)
  757. return;
  758. }
  759. }
  760. static int set_higher_rates(struct moschip_port *mos7720_port,
  761. unsigned int baud)
  762. {
  763. unsigned char data;
  764. struct usb_serial_port *port;
  765. struct usb_serial *serial;
  766. int port_number;
  767. if (mos7720_port == NULL)
  768. return -EINVAL;
  769. port = mos7720_port->port;
  770. serial = port->serial;
  771. /***********************************************
  772. * Init Sequence for higher rates
  773. ***********************************************/
  774. dbg("Sending Setting Commands ..........");
  775. port_number = port->number - port->serial->minor;
  776. data = 0x000;
  777. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  778. data = 0x000;
  779. send_mos_cmd(serial, MOS_WRITE, port_number, 0x02, &data);
  780. data = 0x0CF;
  781. send_mos_cmd(serial, MOS_WRITE, port->number, 0x02, &data);
  782. data = 0x00b;
  783. mos7720_port->shadowMCR = data;
  784. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  785. data = 0x00b;
  786. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  787. data = 0x000;
  788. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  789. data = 0x000;
  790. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  791. /***********************************************
  792. * Set for higher rates *
  793. ***********************************************/
  794. data = baud * 0x10;
  795. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, port_number + 1, &data);
  796. data = 0x003;
  797. send_mos_cmd(serial, MOS_READ, MOS_MAX_PORT, 0x08, &data);
  798. data = 0x003;
  799. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT, 0x08, &data);
  800. data = 0x02b;
  801. mos7720_port->shadowMCR = data;
  802. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  803. data = 0x02b;
  804. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  805. /***********************************************
  806. * Set DLL/DLM
  807. ***********************************************/
  808. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  809. mos7720_port->shadowLCR = data;
  810. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  811. data = 0x001; /* DLL */
  812. send_mos_cmd(serial, MOS_WRITE, port_number, 0x00, &data);
  813. data = 0x000; /* DLM */
  814. send_mos_cmd(serial, MOS_WRITE, port_number, 0x01, &data);
  815. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  816. mos7720_port->shadowLCR = data;
  817. send_mos_cmd(serial, MOS_WRITE, port_number, 0x03, &data);
  818. return 0;
  819. }
  820. /* baud rate information */
  821. struct divisor_table_entry {
  822. __u32 baudrate;
  823. __u16 divisor;
  824. };
  825. /* Define table of divisors for moschip 7720 hardware *
  826. * These assume a 3.6864MHz crystal, the standard /16, and *
  827. * MCR.7 = 0. */
  828. static struct divisor_table_entry divisor_table[] = {
  829. { 50, 2304},
  830. { 110, 1047}, /* 2094.545455 => 230450 => .0217 % over */
  831. { 134, 857}, /* 1713.011152 => 230398.5 => .00065% under */
  832. { 150, 768},
  833. { 300, 384},
  834. { 600, 192},
  835. { 1200, 96},
  836. { 1800, 64},
  837. { 2400, 48},
  838. { 4800, 24},
  839. { 7200, 16},
  840. { 9600, 12},
  841. { 19200, 6},
  842. { 38400, 3},
  843. { 57600, 2},
  844. { 115200, 1},
  845. };
  846. /*****************************************************************************
  847. * calc_baud_rate_divisor
  848. * this function calculates the proper baud rate divisor for the specified
  849. * baud rate.
  850. *****************************************************************************/
  851. static int calc_baud_rate_divisor(int baudrate, int *divisor)
  852. {
  853. int i;
  854. __u16 custom;
  855. __u16 round1;
  856. __u16 round;
  857. dbg("%s - %d", __func__, baudrate);
  858. for (i = 0; i < ARRAY_SIZE(divisor_table); i++) {
  859. if (divisor_table[i].baudrate == baudrate) {
  860. *divisor = divisor_table[i].divisor;
  861. return 0;
  862. }
  863. }
  864. /* After trying for all the standard baud rates *
  865. * Try calculating the divisor for this baud rate */
  866. if (baudrate > 75 && baudrate < 230400) {
  867. /* get the divisor */
  868. custom = (__u16)(230400L / baudrate);
  869. /* Check for round off */
  870. round1 = (__u16)(2304000L / baudrate);
  871. round = (__u16)(round1 - (custom * 10));
  872. if (round > 4)
  873. custom++;
  874. *divisor = custom;
  875. dbg("Baud %d = %d", baudrate, custom);
  876. return 0;
  877. }
  878. dbg("Baud calculation Failed...");
  879. return -EINVAL;
  880. }
  881. /*
  882. * send_cmd_write_baud_rate
  883. * this function sends the proper command to change the baud rate of the
  884. * specified port.
  885. */
  886. static int send_cmd_write_baud_rate(struct moschip_port *mos7720_port,
  887. int baudrate)
  888. {
  889. struct usb_serial_port *port;
  890. struct usb_serial *serial;
  891. int divisor;
  892. int status;
  893. unsigned char data;
  894. unsigned char number;
  895. if (mos7720_port == NULL)
  896. return -1;
  897. port = mos7720_port->port;
  898. serial = port->serial;
  899. dbg("%s: Entering ..........", __func__);
  900. number = port->number - port->serial->minor;
  901. dbg("%s - port = %d, baud = %d", __func__, port->number, baudrate);
  902. /* Calculate the Divisor */
  903. status = calc_baud_rate_divisor(baudrate, &divisor);
  904. if (status) {
  905. dev_err(&port->dev, "%s - bad baud rate\n", __func__);
  906. return status;
  907. }
  908. /* Enable access to divisor latch */
  909. data = mos7720_port->shadowLCR | UART_LCR_DLAB;
  910. mos7720_port->shadowLCR = data;
  911. send_mos_cmd(serial, MOS_WRITE, number, UART_LCR, &data);
  912. /* Write the divisor */
  913. data = ((unsigned char)(divisor & 0xff));
  914. send_mos_cmd(serial, MOS_WRITE, number, 0x00, &data);
  915. data = ((unsigned char)((divisor & 0xff00) >> 8));
  916. send_mos_cmd(serial, MOS_WRITE, number, 0x01, &data);
  917. /* Disable access to divisor latch */
  918. data = mos7720_port->shadowLCR & ~UART_LCR_DLAB;
  919. mos7720_port->shadowLCR = data;
  920. send_mos_cmd(serial, MOS_WRITE, number, 0x03, &data);
  921. return status;
  922. }
  923. /*
  924. * change_port_settings
  925. * This routine is called to set the UART on the device to match
  926. * the specified new settings.
  927. */
  928. static void change_port_settings(struct tty_struct *tty,
  929. struct moschip_port *mos7720_port,
  930. struct ktermios *old_termios)
  931. {
  932. struct usb_serial_port *port;
  933. struct usb_serial *serial;
  934. int baud;
  935. unsigned cflag;
  936. unsigned iflag;
  937. __u8 mask = 0xff;
  938. __u8 lData;
  939. __u8 lParity;
  940. __u8 lStop;
  941. int status;
  942. int port_number;
  943. char data;
  944. if (mos7720_port == NULL)
  945. return ;
  946. port = mos7720_port->port;
  947. serial = port->serial;
  948. port_number = port->number - port->serial->minor;
  949. dbg("%s - port %d", __func__, port->number);
  950. if (!mos7720_port->open) {
  951. dbg("%s - port not opened", __func__);
  952. return;
  953. }
  954. dbg("%s: Entering ..........", __func__);
  955. lData = UART_LCR_WLEN8;
  956. lStop = 0x00; /* 1 stop bit */
  957. lParity = 0x00; /* No parity */
  958. cflag = tty->termios->c_cflag;
  959. iflag = tty->termios->c_iflag;
  960. /* Change the number of bits */
  961. switch (cflag & CSIZE) {
  962. case CS5:
  963. lData = UART_LCR_WLEN5;
  964. mask = 0x1f;
  965. break;
  966. case CS6:
  967. lData = UART_LCR_WLEN6;
  968. mask = 0x3f;
  969. break;
  970. case CS7:
  971. lData = UART_LCR_WLEN7;
  972. mask = 0x7f;
  973. break;
  974. default:
  975. case CS8:
  976. lData = UART_LCR_WLEN8;
  977. break;
  978. }
  979. /* Change the Parity bit */
  980. if (cflag & PARENB) {
  981. if (cflag & PARODD) {
  982. lParity = UART_LCR_PARITY;
  983. dbg("%s - parity = odd", __func__);
  984. } else {
  985. lParity = (UART_LCR_EPAR | UART_LCR_PARITY);
  986. dbg("%s - parity = even", __func__);
  987. }
  988. } else {
  989. dbg("%s - parity = none", __func__);
  990. }
  991. if (cflag & CMSPAR)
  992. lParity = lParity | 0x20;
  993. /* Change the Stop bit */
  994. if (cflag & CSTOPB) {
  995. lStop = UART_LCR_STOP;
  996. dbg("%s - stop bits = 2", __func__);
  997. } else {
  998. lStop = 0x00;
  999. dbg("%s - stop bits = 1", __func__);
  1000. }
  1001. #define LCR_BITS_MASK 0x03 /* Mask for bits/char field */
  1002. #define LCR_STOP_MASK 0x04 /* Mask for stop bits field */
  1003. #define LCR_PAR_MASK 0x38 /* Mask for parity field */
  1004. /* Update the LCR with the correct value */
  1005. mos7720_port->shadowLCR &=
  1006. ~(LCR_BITS_MASK | LCR_STOP_MASK | LCR_PAR_MASK);
  1007. mos7720_port->shadowLCR |= (lData | lParity | lStop);
  1008. /* Disable Interrupts */
  1009. data = 0x00;
  1010. send_mos_cmd(serial, MOS_WRITE, port->number - port->serial->minor,
  1011. UART_IER, &data);
  1012. data = 0x00;
  1013. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  1014. data = 0xcf;
  1015. send_mos_cmd(serial, MOS_WRITE, port_number, UART_FCR, &data);
  1016. /* Send the updated LCR value to the mos7720 */
  1017. data = mos7720_port->shadowLCR;
  1018. send_mos_cmd(serial, MOS_WRITE, port_number, UART_LCR, &data);
  1019. data = 0x00b;
  1020. mos7720_port->shadowMCR = data;
  1021. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  1022. data = 0x00b;
  1023. send_mos_cmd(serial, MOS_WRITE, port_number, 0x04, &data);
  1024. /* set up the MCR register and send it to the mos7720 */
  1025. mos7720_port->shadowMCR = UART_MCR_OUT2;
  1026. if (cflag & CBAUD)
  1027. mos7720_port->shadowMCR |= (UART_MCR_DTR | UART_MCR_RTS);
  1028. if (cflag & CRTSCTS) {
  1029. mos7720_port->shadowMCR |= (UART_MCR_XONANY);
  1030. /* To set hardware flow control to the specified *
  1031. * serial port, in SP1/2_CONTROL_REG */
  1032. if (port->number) {
  1033. data = 0x001;
  1034. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  1035. 0x08, &data);
  1036. } else {
  1037. data = 0x002;
  1038. send_mos_cmd(serial, MOS_WRITE, MOS_MAX_PORT,
  1039. 0x08, &data);
  1040. }
  1041. } else {
  1042. mos7720_port->shadowMCR &= ~(UART_MCR_XONANY);
  1043. }
  1044. data = mos7720_port->shadowMCR;
  1045. send_mos_cmd(serial, MOS_WRITE, port_number, UART_MCR, &data);
  1046. /* Determine divisor based on baud rate */
  1047. baud = tty_get_baud_rate(tty);
  1048. if (!baud) {
  1049. /* pick a default, any default... */
  1050. dbg("Picked default baud...");
  1051. baud = 9600;
  1052. }
  1053. if (baud >= 230400) {
  1054. set_higher_rates(mos7720_port, baud);
  1055. /* Enable Interrupts */
  1056. data = 0x0c;
  1057. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  1058. return;
  1059. }
  1060. dbg("%s - baud rate = %d", __func__, baud);
  1061. status = send_cmd_write_baud_rate(mos7720_port, baud);
  1062. /* FIXME: needs to write actual resulting baud back not just
  1063. blindly do so */
  1064. if (cflag & CBAUD)
  1065. tty_encode_baud_rate(tty, baud, baud);
  1066. /* Enable Interrupts */
  1067. data = 0x0c;
  1068. send_mos_cmd(serial, MOS_WRITE, port_number, UART_IER, &data);
  1069. if (port->read_urb->status != -EINPROGRESS) {
  1070. port->read_urb->dev = serial->dev;
  1071. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1072. if (status)
  1073. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1074. status);
  1075. }
  1076. return;
  1077. }
  1078. /*
  1079. * mos7720_set_termios
  1080. * this function is called by the tty driver when it wants to change the
  1081. * termios structure.
  1082. */
  1083. static void mos7720_set_termios(struct tty_struct *tty,
  1084. struct usb_serial_port *port, struct ktermios *old_termios)
  1085. {
  1086. int status;
  1087. unsigned int cflag;
  1088. struct usb_serial *serial;
  1089. struct moschip_port *mos7720_port;
  1090. serial = port->serial;
  1091. mos7720_port = usb_get_serial_port_data(port);
  1092. if (mos7720_port == NULL)
  1093. return;
  1094. if (!mos7720_port->open) {
  1095. dbg("%s - port not opened", __func__);
  1096. return;
  1097. }
  1098. dbg("setting termios - ASPIRE");
  1099. cflag = tty->termios->c_cflag;
  1100. dbg("%s - cflag %08x iflag %08x", __func__,
  1101. tty->termios->c_cflag,
  1102. RELEVANT_IFLAG(tty->termios->c_iflag));
  1103. dbg("%s - old cflag %08x old iflag %08x", __func__,
  1104. old_termios->c_cflag,
  1105. RELEVANT_IFLAG(old_termios->c_iflag));
  1106. dbg("%s - port %d", __func__, port->number);
  1107. /* change the port settings to the new ones specified */
  1108. change_port_settings(tty, mos7720_port, old_termios);
  1109. if (!port->read_urb) {
  1110. dbg("URB KILLED !!!!!");
  1111. return;
  1112. }
  1113. if (port->read_urb->status != -EINPROGRESS) {
  1114. port->read_urb->dev = serial->dev;
  1115. status = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  1116. if (status)
  1117. dbg("usb_submit_urb(read bulk) failed, status = %d",
  1118. status);
  1119. }
  1120. return;
  1121. }
  1122. /*
  1123. * get_lsr_info - get line status register info
  1124. *
  1125. * Purpose: Let user call ioctl() to get info when the UART physically
  1126. * is emptied. On bus types like RS485, the transmitter must
  1127. * release the bus after transmitting. This must be done when
  1128. * the transmit shift register is empty, not be done when the
  1129. * transmit holding register is empty. This functionality
  1130. * allows an RS485 driver to be written in user space.
  1131. */
  1132. static int get_lsr_info(struct tty_struct *tty,
  1133. struct moschip_port *mos7720_port, unsigned int __user *value)
  1134. {
  1135. struct usb_serial_port *port = tty->driver_data;
  1136. unsigned int result = 0;
  1137. unsigned char data = 0;
  1138. int port_number = port->number - port->serial->minor;
  1139. int count;
  1140. count = mos7720_chars_in_buffer(tty);
  1141. if (count == 0) {
  1142. send_mos_cmd(port->serial, MOS_READ, port_number,
  1143. UART_LSR, &data);
  1144. if ((data & (UART_LSR_TEMT | UART_LSR_THRE))
  1145. == (UART_LSR_TEMT | UART_LSR_THRE)) {
  1146. dbg("%s -- Empty", __func__);
  1147. result = TIOCSER_TEMT;
  1148. }
  1149. }
  1150. if (copy_to_user(value, &result, sizeof(int)))
  1151. return -EFAULT;
  1152. return 0;
  1153. }
  1154. static int mos7720_tiocmget(struct tty_struct *tty, struct file *file)
  1155. {
  1156. struct usb_serial_port *port = tty->driver_data;
  1157. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1158. unsigned int result = 0;
  1159. unsigned int mcr ;
  1160. unsigned int msr ;
  1161. dbg("%s - port %d", __func__, port->number);
  1162. mcr = mos7720_port->shadowMCR;
  1163. msr = mos7720_port->shadowMSR;
  1164. result = ((mcr & UART_MCR_DTR) ? TIOCM_DTR : 0) /* 0x002 */
  1165. | ((mcr & UART_MCR_RTS) ? TIOCM_RTS : 0) /* 0x004 */
  1166. | ((msr & UART_MSR_CTS) ? TIOCM_CTS : 0) /* 0x020 */
  1167. | ((msr & UART_MSR_DCD) ? TIOCM_CAR : 0) /* 0x040 */
  1168. | ((msr & UART_MSR_RI) ? TIOCM_RI : 0) /* 0x080 */
  1169. | ((msr & UART_MSR_DSR) ? TIOCM_DSR : 0); /* 0x100 */
  1170. dbg("%s -- %x", __func__, result);
  1171. return result;
  1172. }
  1173. static int mos7720_tiocmset(struct tty_struct *tty, struct file *file,
  1174. unsigned int set, unsigned int clear)
  1175. {
  1176. struct usb_serial_port *port = tty->driver_data;
  1177. struct moschip_port *mos7720_port = usb_get_serial_port_data(port);
  1178. unsigned int mcr ;
  1179. unsigned char lmcr;
  1180. dbg("%s - port %d", __func__, port->number);
  1181. dbg("he was at tiocmget");
  1182. mcr = mos7720_port->shadowMCR;
  1183. if (set & TIOCM_RTS)
  1184. mcr |= UART_MCR_RTS;
  1185. if (set & TIOCM_DTR)
  1186. mcr |= UART_MCR_DTR;
  1187. if (set & TIOCM_LOOP)
  1188. mcr |= UART_MCR_LOOP;
  1189. if (clear & TIOCM_RTS)
  1190. mcr &= ~UART_MCR_RTS;
  1191. if (clear & TIOCM_DTR)
  1192. mcr &= ~UART_MCR_DTR;
  1193. if (clear & TIOCM_LOOP)
  1194. mcr &= ~UART_MCR_LOOP;
  1195. mos7720_port->shadowMCR = mcr;
  1196. lmcr = mos7720_port->shadowMCR;
  1197. send_mos_cmd(port->serial, MOS_WRITE,
  1198. port->number - port->serial->minor, UART_MCR, &lmcr);
  1199. return 0;
  1200. }
  1201. static int set_modem_info(struct moschip_port *mos7720_port, unsigned int cmd,
  1202. unsigned int __user *value)
  1203. {
  1204. unsigned int mcr ;
  1205. unsigned int arg;
  1206. unsigned char data;
  1207. struct usb_serial_port *port;
  1208. if (mos7720_port == NULL)
  1209. return -1;
  1210. port = (struct usb_serial_port *)mos7720_port->port;
  1211. mcr = mos7720_port->shadowMCR;
  1212. if (copy_from_user(&arg, value, sizeof(int)))
  1213. return -EFAULT;
  1214. switch (cmd) {
  1215. case TIOCMBIS:
  1216. if (arg & TIOCM_RTS)
  1217. mcr |= UART_MCR_RTS;
  1218. if (arg & TIOCM_DTR)
  1219. mcr |= UART_MCR_RTS;
  1220. if (arg & TIOCM_LOOP)
  1221. mcr |= UART_MCR_LOOP;
  1222. break;
  1223. case TIOCMBIC:
  1224. if (arg & TIOCM_RTS)
  1225. mcr &= ~UART_MCR_RTS;
  1226. if (arg & TIOCM_DTR)
  1227. mcr &= ~UART_MCR_RTS;
  1228. if (arg & TIOCM_LOOP)
  1229. mcr &= ~UART_MCR_LOOP;
  1230. break;
  1231. }
  1232. mos7720_port->shadowMCR = mcr;
  1233. data = mos7720_port->shadowMCR;
  1234. send_mos_cmd(port->serial, MOS_WRITE,
  1235. port->number - port->serial->minor, UART_MCR, &data);
  1236. return 0;
  1237. }
  1238. static int get_serial_info(struct moschip_port *mos7720_port,
  1239. struct serial_struct __user *retinfo)
  1240. {
  1241. struct serial_struct tmp;
  1242. if (!retinfo)
  1243. return -EFAULT;
  1244. memset(&tmp, 0, sizeof(tmp));
  1245. tmp.type = PORT_16550A;
  1246. tmp.line = mos7720_port->port->serial->minor;
  1247. tmp.port = mos7720_port->port->number;
  1248. tmp.irq = 0;
  1249. tmp.flags = ASYNC_SKIP_TEST | ASYNC_AUTO_IRQ;
  1250. tmp.xmit_fifo_size = NUM_URBS * URB_TRANSFER_BUFFER_SIZE;
  1251. tmp.baud_base = 9600;
  1252. tmp.close_delay = 5*HZ;
  1253. tmp.closing_wait = 30*HZ;
  1254. if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
  1255. return -EFAULT;
  1256. return 0;
  1257. }
  1258. static int mos7720_ioctl(struct tty_struct *tty, struct file *file,
  1259. unsigned int cmd, unsigned long arg)
  1260. {
  1261. struct usb_serial_port *port = tty->driver_data;
  1262. struct moschip_port *mos7720_port;
  1263. struct async_icount cnow;
  1264. struct async_icount cprev;
  1265. struct serial_icounter_struct icount;
  1266. mos7720_port = usb_get_serial_port_data(port);
  1267. if (mos7720_port == NULL)
  1268. return -ENODEV;
  1269. dbg("%s - port %d, cmd = 0x%x", __func__, port->number, cmd);
  1270. switch (cmd) {
  1271. case TIOCSERGETLSR:
  1272. dbg("%s (%d) TIOCSERGETLSR", __func__, port->number);
  1273. return get_lsr_info(tty, mos7720_port,
  1274. (unsigned int __user *)arg);
  1275. return 0;
  1276. /* FIXME: These should be using the mode methods */
  1277. case TIOCMBIS:
  1278. case TIOCMBIC:
  1279. dbg("%s (%d) TIOCMSET/TIOCMBIC/TIOCMSET",
  1280. __func__, port->number);
  1281. return set_modem_info(mos7720_port, cmd,
  1282. (unsigned int __user *)arg);
  1283. case TIOCGSERIAL:
  1284. dbg("%s (%d) TIOCGSERIAL", __func__, port->number);
  1285. return get_serial_info(mos7720_port,
  1286. (struct serial_struct __user *)arg);
  1287. case TIOCMIWAIT:
  1288. dbg("%s (%d) TIOCMIWAIT", __func__, port->number);
  1289. cprev = mos7720_port->icount;
  1290. while (1) {
  1291. if (signal_pending(current))
  1292. return -ERESTARTSYS;
  1293. cnow = mos7720_port->icount;
  1294. if (cnow.rng == cprev.rng && cnow.dsr == cprev.dsr &&
  1295. cnow.dcd == cprev.dcd && cnow.cts == cprev.cts)
  1296. return -EIO; /* no change => error */
  1297. if (((arg & TIOCM_RNG) && (cnow.rng != cprev.rng)) ||
  1298. ((arg & TIOCM_DSR) && (cnow.dsr != cprev.dsr)) ||
  1299. ((arg & TIOCM_CD) && (cnow.dcd != cprev.dcd)) ||
  1300. ((arg & TIOCM_CTS) && (cnow.cts != cprev.cts))) {
  1301. return 0;
  1302. }
  1303. cprev = cnow;
  1304. }
  1305. /* NOTREACHED */
  1306. break;
  1307. case TIOCGICOUNT:
  1308. cnow = mos7720_port->icount;
  1309. icount.cts = cnow.cts;
  1310. icount.dsr = cnow.dsr;
  1311. icount.rng = cnow.rng;
  1312. icount.dcd = cnow.dcd;
  1313. icount.rx = cnow.rx;
  1314. icount.tx = cnow.tx;
  1315. icount.frame = cnow.frame;
  1316. icount.overrun = cnow.overrun;
  1317. icount.parity = cnow.parity;
  1318. icount.brk = cnow.brk;
  1319. icount.buf_overrun = cnow.buf_overrun;
  1320. dbg("%s (%d) TIOCGICOUNT RX=%d, TX=%d", __func__,
  1321. port->number, icount.rx, icount.tx);
  1322. if (copy_to_user((void __user *)arg, &icount, sizeof(icount)))
  1323. return -EFAULT;
  1324. return 0;
  1325. }
  1326. return -ENOIOCTLCMD;
  1327. }
  1328. static int mos7720_startup(struct usb_serial *serial)
  1329. {
  1330. struct moschip_serial *mos7720_serial;
  1331. struct moschip_port *mos7720_port;
  1332. struct usb_device *dev;
  1333. int i;
  1334. char data;
  1335. u16 product = le16_to_cpu(serial->dev->descriptor.idProduct);
  1336. dbg("%s: Entering ..........", __func__);
  1337. if (!serial) {
  1338. dbg("Invalid Handler");
  1339. return -ENODEV;
  1340. }
  1341. dev = serial->dev;
  1342. /* create our private serial structure */
  1343. mos7720_serial = kzalloc(sizeof(struct moschip_serial), GFP_KERNEL);
  1344. if (mos7720_serial == NULL) {
  1345. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1346. return -ENOMEM;
  1347. }
  1348. usb_set_serial_data(serial, mos7720_serial);
  1349. /*
  1350. * The 7715 uses the first bulk in/out endpoint pair for the parallel
  1351. * port, and the second for the serial port. Because the usbserial core
  1352. * assumes both pairs are serial ports, we must engage in a bit of
  1353. * subterfuge and swap the pointers for ports 0 and 1 in order to make
  1354. * port 0 point to the serial port. However, both moschip devices use a
  1355. * single interrupt-in endpoint for both ports (as mentioned a little
  1356. * further down), and this endpoint was assigned to port 0. So after
  1357. * the swap, we must copy the interrupt endpoint elements from port 1
  1358. * (as newly assigned) to port 0, and null out port 1 pointers.
  1359. */
  1360. if (product == MOSCHIP_DEVICE_ID_7715) {
  1361. struct usb_serial_port *tmp = serial->port[0];
  1362. serial->port[0] = serial->port[1];
  1363. serial->port[1] = tmp;
  1364. serial->port[0]->interrupt_in_urb = tmp->interrupt_in_urb;
  1365. serial->port[0]->interrupt_in_buffer = tmp->interrupt_in_buffer;
  1366. serial->port[0]->interrupt_in_endpointAddress =
  1367. tmp->interrupt_in_endpointAddress;
  1368. serial->port[1]->interrupt_in_urb = NULL;
  1369. serial->port[1]->interrupt_in_buffer = NULL;
  1370. }
  1371. /* we set up the pointers to the endpoints in the mos7720_open *
  1372. * function, as the structures aren't created yet. */
  1373. /* set up port private structures */
  1374. for (i = 0; i < serial->num_ports; ++i) {
  1375. mos7720_port = kzalloc(sizeof(struct moschip_port), GFP_KERNEL);
  1376. if (mos7720_port == NULL) {
  1377. dev_err(&dev->dev, "%s - Out of memory\n", __func__);
  1378. usb_set_serial_data(serial, NULL);
  1379. kfree(mos7720_serial);
  1380. return -ENOMEM;
  1381. }
  1382. /* Initialize all port interrupt end point to port 0 int
  1383. * endpoint. Our device has only one interrupt endpoint
  1384. * common to all ports */
  1385. serial->port[i]->interrupt_in_endpointAddress =
  1386. serial->port[0]->interrupt_in_endpointAddress;
  1387. mos7720_port->port = serial->port[i];
  1388. usb_set_serial_port_data(serial->port[i], mos7720_port);
  1389. dbg("port number is %d", serial->port[i]->number);
  1390. dbg("serial number is %d", serial->minor);
  1391. }
  1392. /* setting configuration feature to one */
  1393. usb_control_msg(serial->dev, usb_sndctrlpipe(serial->dev, 0),
  1394. (__u8)0x03, 0x00, 0x01, 0x00, NULL, 0x00, 5*HZ);
  1395. /* LSR For Port 1 */
  1396. send_mos_cmd(serial, MOS_READ, 0x00, UART_LSR, &data);
  1397. dbg("LSR:%x", data);
  1398. /* LSR For Port 2 */
  1399. send_mos_cmd(serial, MOS_READ, 0x01, UART_LSR, &data);
  1400. dbg("LSR:%x", data);
  1401. return 0;
  1402. }
  1403. static void mos7720_release(struct usb_serial *serial)
  1404. {
  1405. int i;
  1406. /* free private structure allocated for serial port */
  1407. for (i = 0; i < serial->num_ports; ++i)
  1408. kfree(usb_get_serial_port_data(serial->port[i]));
  1409. /* free private structure allocated for serial device */
  1410. kfree(usb_get_serial_data(serial));
  1411. }
  1412. static struct usb_driver usb_driver = {
  1413. .name = "moschip7720",
  1414. .probe = usb_serial_probe,
  1415. .disconnect = usb_serial_disconnect,
  1416. .id_table = moschip_port_id_table,
  1417. .no_dynamic_id = 1,
  1418. };
  1419. static struct usb_serial_driver moschip7720_2port_driver = {
  1420. .driver = {
  1421. .owner = THIS_MODULE,
  1422. .name = "moschip7720",
  1423. },
  1424. .description = "Moschip 2 port adapter",
  1425. .usb_driver = &usb_driver,
  1426. .id_table = moschip_port_id_table,
  1427. .calc_num_ports = mos77xx_calc_num_ports,
  1428. .open = mos7720_open,
  1429. .close = mos7720_close,
  1430. .throttle = mos7720_throttle,
  1431. .unthrottle = mos7720_unthrottle,
  1432. .probe = mos77xx_probe,
  1433. .attach = mos7720_startup,
  1434. .release = mos7720_release,
  1435. .ioctl = mos7720_ioctl,
  1436. .tiocmget = mos7720_tiocmget,
  1437. .tiocmset = mos7720_tiocmset,
  1438. .set_termios = mos7720_set_termios,
  1439. .write = mos7720_write,
  1440. .write_room = mos7720_write_room,
  1441. .chars_in_buffer = mos7720_chars_in_buffer,
  1442. .break_ctl = mos7720_break,
  1443. .read_bulk_callback = mos7720_bulk_in_callback,
  1444. .read_int_callback = NULL /* dynamically assigned in probe() */
  1445. };
  1446. static int __init moschip7720_init(void)
  1447. {
  1448. int retval;
  1449. dbg("%s: Entering ..........", __func__);
  1450. /* Register with the usb serial */
  1451. retval = usb_serial_register(&moschip7720_2port_driver);
  1452. if (retval)
  1453. goto failed_port_device_register;
  1454. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_VERSION ":"
  1455. DRIVER_DESC "\n");
  1456. /* Register with the usb */
  1457. retval = usb_register(&usb_driver);
  1458. if (retval)
  1459. goto failed_usb_register;
  1460. return 0;
  1461. failed_usb_register:
  1462. usb_serial_deregister(&moschip7720_2port_driver);
  1463. failed_port_device_register:
  1464. return retval;
  1465. }
  1466. static void __exit moschip7720_exit(void)
  1467. {
  1468. usb_deregister(&usb_driver);
  1469. usb_serial_deregister(&moschip7720_2port_driver);
  1470. }
  1471. module_init(moschip7720_init);
  1472. module_exit(moschip7720_exit);
  1473. /* Module information */
  1474. MODULE_AUTHOR(DRIVER_AUTHOR);
  1475. MODULE_DESCRIPTION(DRIVER_DESC);
  1476. MODULE_LICENSE("GPL");
  1477. module_param(debug, bool, S_IRUGO | S_IWUSR);
  1478. MODULE_PARM_DESC(debug, "Debug enabled or not");