visor.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * USB HandSpring Visor, Palm m50x, and Sony Clie driver
  3. * (supports all of the Palm OS USB devices)
  4. *
  5. * Copyright (C) 1999 - 2004
  6. * Greg Kroah-Hartman (greg@kroah.com)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version
  10. * 2 as published by the Free Software Foundation.
  11. *
  12. * See Documentation/usb/usb-serial.txt for more information on using this
  13. * driver
  14. *
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/errno.h>
  18. #include <linux/init.h>
  19. #include <linux/slab.h>
  20. #include <linux/tty.h>
  21. #include <linux/tty_driver.h>
  22. #include <linux/tty_flip.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/uaccess.h>
  27. #include <linux/usb.h>
  28. #include <linux/usb/serial.h>
  29. #include "visor.h"
  30. /*
  31. * Version Information
  32. */
  33. #define DRIVER_AUTHOR "Greg Kroah-Hartman <greg@kroah.com>"
  34. #define DRIVER_DESC "USB HandSpring Visor / Palm OS driver"
  35. /* function prototypes for a handspring visor */
  36. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port,
  37. struct file *filp);
  38. static void visor_close(struct usb_serial_port *port);
  39. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  40. const unsigned char *buf, int count);
  41. static int visor_write_room(struct tty_struct *tty);
  42. static void visor_throttle(struct tty_struct *tty);
  43. static void visor_unthrottle(struct tty_struct *tty);
  44. static int visor_probe(struct usb_serial *serial,
  45. const struct usb_device_id *id);
  46. static int visor_calc_num_ports(struct usb_serial *serial);
  47. static void visor_release(struct usb_serial *serial);
  48. static void visor_write_bulk_callback(struct urb *urb);
  49. static void visor_read_bulk_callback(struct urb *urb);
  50. static void visor_read_int_callback(struct urb *urb);
  51. static int clie_3_5_startup(struct usb_serial *serial);
  52. static int treo_attach(struct usb_serial *serial);
  53. static int clie_5_attach(struct usb_serial *serial);
  54. static int palm_os_3_probe(struct usb_serial *serial,
  55. const struct usb_device_id *id);
  56. static int palm_os_4_probe(struct usb_serial *serial,
  57. const struct usb_device_id *id);
  58. /* Parameters that may be passed into the module. */
  59. static int debug;
  60. static __u16 vendor;
  61. static __u16 product;
  62. static struct usb_device_id id_table [] = {
  63. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID),
  64. .driver_info = (kernel_ulong_t)&palm_os_3_probe },
  65. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID),
  66. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  67. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID),
  68. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  69. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID),
  70. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  71. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID),
  72. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  73. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID),
  74. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  75. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID),
  76. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  77. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID),
  78. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  79. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID),
  80. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  81. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID),
  82. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  83. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID),
  84. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  85. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID),
  86. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  87. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650),
  88. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  89. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID),
  90. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  91. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID),
  92. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  93. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID),
  94. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  95. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID),
  96. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  97. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID),
  98. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  99. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID),
  100. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  101. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID),
  102. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  103. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID),
  104. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  105. { USB_DEVICE(ACER_VENDOR_ID, ACER_S10_ID),
  106. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  107. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID),
  108. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  109. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID),
  110. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  111. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID),
  112. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  113. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID),
  114. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  115. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID),
  116. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  117. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID),
  118. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  119. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID),
  120. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  121. { }, /* optional parameter entry */
  122. { } /* Terminating entry */
  123. };
  124. static struct usb_device_id clie_id_5_table [] = {
  125. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID),
  126. .driver_info = (kernel_ulong_t)&palm_os_4_probe },
  127. { }, /* optional parameter entry */
  128. { } /* Terminating entry */
  129. };
  130. static struct usb_device_id clie_id_3_5_table [] = {
  131. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  132. { } /* Terminating entry */
  133. };
  134. static struct usb_device_id id_table_combined [] = {
  135. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_VISOR_ID) },
  136. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO_ID) },
  137. { USB_DEVICE(HANDSPRING_VENDOR_ID, HANDSPRING_TREO600_ID) },
  138. { USB_DEVICE(GSPDA_VENDOR_ID, GSPDA_XPLORE_M68_ID) },
  139. { USB_DEVICE(PALM_VENDOR_ID, PALM_M500_ID) },
  140. { USB_DEVICE(PALM_VENDOR_ID, PALM_M505_ID) },
  141. { USB_DEVICE(PALM_VENDOR_ID, PALM_M515_ID) },
  142. { USB_DEVICE(PALM_VENDOR_ID, PALM_I705_ID) },
  143. { USB_DEVICE(PALM_VENDOR_ID, PALM_M100_ID) },
  144. { USB_DEVICE(PALM_VENDOR_ID, PALM_M125_ID) },
  145. { USB_DEVICE(PALM_VENDOR_ID, PALM_M130_ID) },
  146. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_T_ID) },
  147. { USB_DEVICE(PALM_VENDOR_ID, PALM_TREO_650) },
  148. { USB_DEVICE(PALM_VENDOR_ID, PALM_TUNGSTEN_Z_ID) },
  149. { USB_DEVICE(PALM_VENDOR_ID, PALM_ZIRE_ID) },
  150. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_3_5_ID) },
  151. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_0_ID) },
  152. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_S360_ID) },
  153. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_4_1_ID) },
  154. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NX60_ID) },
  155. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_NZ90V_ID) },
  156. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_UX50_ID) },
  157. { USB_DEVICE(SONY_VENDOR_ID, SONY_CLIE_TJ25_ID) },
  158. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SCH_I330_ID) },
  159. { USB_DEVICE(SAMSUNG_VENDOR_ID, SAMSUNG_SPH_I500_ID) },
  160. { USB_DEVICE(TAPWAVE_VENDOR_ID, TAPWAVE_ZODIAC_ID) },
  161. { USB_DEVICE(GARMIN_VENDOR_ID, GARMIN_IQUE_3600_ID) },
  162. { USB_DEVICE(ACEECA_VENDOR_ID, ACEECA_MEZ1000_ID) },
  163. { USB_DEVICE(KYOCERA_VENDOR_ID, KYOCERA_7135_ID) },
  164. { USB_DEVICE(FOSSIL_VENDOR_ID, FOSSIL_ABACUS_ID) },
  165. { }, /* optional parameter entry */
  166. { } /* Terminating entry */
  167. };
  168. MODULE_DEVICE_TABLE(usb, id_table_combined);
  169. static struct usb_driver visor_driver = {
  170. .name = "visor",
  171. .probe = usb_serial_probe,
  172. .disconnect = usb_serial_disconnect,
  173. .id_table = id_table_combined,
  174. .no_dynamic_id = 1,
  175. };
  176. /* All of the device info needed for the Handspring Visor,
  177. and Palm 4.0 devices */
  178. static struct usb_serial_driver handspring_device = {
  179. .driver = {
  180. .owner = THIS_MODULE,
  181. .name = "visor",
  182. },
  183. .description = "Handspring Visor / Palm OS",
  184. .usb_driver = &visor_driver,
  185. .id_table = id_table,
  186. .num_ports = 2,
  187. .open = visor_open,
  188. .close = visor_close,
  189. .throttle = visor_throttle,
  190. .unthrottle = visor_unthrottle,
  191. .attach = treo_attach,
  192. .probe = visor_probe,
  193. .calc_num_ports = visor_calc_num_ports,
  194. .release = visor_release,
  195. .write = visor_write,
  196. .write_room = visor_write_room,
  197. .write_bulk_callback = visor_write_bulk_callback,
  198. .read_bulk_callback = visor_read_bulk_callback,
  199. .read_int_callback = visor_read_int_callback,
  200. };
  201. /* All of the device info needed for the Clie UX50, TH55 Palm 5.0 devices */
  202. static struct usb_serial_driver clie_5_device = {
  203. .driver = {
  204. .owner = THIS_MODULE,
  205. .name = "clie_5",
  206. },
  207. .description = "Sony Clie 5.0",
  208. .usb_driver = &visor_driver,
  209. .id_table = clie_id_5_table,
  210. .num_ports = 2,
  211. .open = visor_open,
  212. .close = visor_close,
  213. .throttle = visor_throttle,
  214. .unthrottle = visor_unthrottle,
  215. .attach = clie_5_attach,
  216. .probe = visor_probe,
  217. .calc_num_ports = visor_calc_num_ports,
  218. .release = visor_release,
  219. .write = visor_write,
  220. .write_room = visor_write_room,
  221. .write_bulk_callback = visor_write_bulk_callback,
  222. .read_bulk_callback = visor_read_bulk_callback,
  223. .read_int_callback = visor_read_int_callback,
  224. };
  225. /* device info for the Sony Clie OS version 3.5 */
  226. static struct usb_serial_driver clie_3_5_device = {
  227. .driver = {
  228. .owner = THIS_MODULE,
  229. .name = "clie_3.5",
  230. },
  231. .description = "Sony Clie 3.5",
  232. .usb_driver = &visor_driver,
  233. .id_table = clie_id_3_5_table,
  234. .num_ports = 1,
  235. .open = visor_open,
  236. .close = visor_close,
  237. .throttle = visor_throttle,
  238. .unthrottle = visor_unthrottle,
  239. .attach = clie_3_5_startup,
  240. .write = visor_write,
  241. .write_room = visor_write_room,
  242. .write_bulk_callback = visor_write_bulk_callback,
  243. .read_bulk_callback = visor_read_bulk_callback,
  244. };
  245. struct visor_private {
  246. spinlock_t lock;
  247. int bytes_in;
  248. int bytes_out;
  249. int outstanding_urbs;
  250. unsigned char throttled;
  251. unsigned char actually_throttled;
  252. };
  253. /* number of outstanding urbs to prevent userspace DoS from happening */
  254. #define URB_UPPER_LIMIT 42
  255. static int stats;
  256. /******************************************************************************
  257. * Handspring Visor specific driver functions
  258. ******************************************************************************/
  259. static int visor_open(struct tty_struct *tty, struct usb_serial_port *port,
  260. struct file *filp)
  261. {
  262. struct usb_serial *serial = port->serial;
  263. struct visor_private *priv = usb_get_serial_port_data(port);
  264. unsigned long flags;
  265. int result = 0;
  266. dbg("%s - port %d", __func__, port->number);
  267. if (!port->read_urb) {
  268. /* this is needed for some brain dead Sony devices */
  269. dev_err(&port->dev, "Device lied about number of ports, please use a lower one.\n");
  270. return -ENODEV;
  271. }
  272. spin_lock_irqsave(&priv->lock, flags);
  273. priv->bytes_in = 0;
  274. priv->bytes_out = 0;
  275. priv->throttled = 0;
  276. spin_unlock_irqrestore(&priv->lock, flags);
  277. /* Start reading from the device */
  278. usb_fill_bulk_urb(port->read_urb, serial->dev,
  279. usb_rcvbulkpipe(serial->dev,
  280. port->bulk_in_endpointAddress),
  281. port->read_urb->transfer_buffer,
  282. port->read_urb->transfer_buffer_length,
  283. visor_read_bulk_callback, port);
  284. result = usb_submit_urb(port->read_urb, GFP_KERNEL);
  285. if (result) {
  286. dev_err(&port->dev,
  287. "%s - failed submitting read urb, error %d\n",
  288. __func__, result);
  289. goto exit;
  290. }
  291. if (port->interrupt_in_urb) {
  292. dbg("%s - adding interrupt input for treo", __func__);
  293. result = usb_submit_urb(port->interrupt_in_urb, GFP_KERNEL);
  294. if (result)
  295. dev_err(&port->dev,
  296. "%s - failed submitting interrupt urb, error %d\n",
  297. __func__, result);
  298. }
  299. exit:
  300. return result;
  301. }
  302. static void visor_close(struct usb_serial_port *port)
  303. {
  304. struct visor_private *priv = usb_get_serial_port_data(port);
  305. unsigned char *transfer_buffer;
  306. dbg("%s - port %d", __func__, port->number);
  307. /* shutdown our urbs */
  308. usb_kill_urb(port->read_urb);
  309. usb_kill_urb(port->interrupt_in_urb);
  310. mutex_lock(&port->serial->disc_mutex);
  311. if (!port->serial->disconnected) {
  312. /* Try to send shutdown message, unless the device is gone */
  313. transfer_buffer = kmalloc(0x12, GFP_KERNEL);
  314. if (transfer_buffer) {
  315. usb_control_msg(port->serial->dev,
  316. usb_rcvctrlpipe(port->serial->dev, 0),
  317. VISOR_CLOSE_NOTIFICATION, 0xc2,
  318. 0x0000, 0x0000,
  319. transfer_buffer, 0x12, 300);
  320. kfree(transfer_buffer);
  321. }
  322. }
  323. mutex_unlock(&port->serial->disc_mutex);
  324. if (stats)
  325. dev_info(&port->dev, "Bytes In = %d Bytes Out = %d\n",
  326. priv->bytes_in, priv->bytes_out);
  327. }
  328. static int visor_write(struct tty_struct *tty, struct usb_serial_port *port,
  329. const unsigned char *buf, int count)
  330. {
  331. struct visor_private *priv = usb_get_serial_port_data(port);
  332. struct usb_serial *serial = port->serial;
  333. struct urb *urb;
  334. unsigned char *buffer;
  335. unsigned long flags;
  336. int status;
  337. dbg("%s - port %d", __func__, port->number);
  338. spin_lock_irqsave(&priv->lock, flags);
  339. if (priv->outstanding_urbs > URB_UPPER_LIMIT) {
  340. spin_unlock_irqrestore(&priv->lock, flags);
  341. dbg("%s - write limit hit\n", __func__);
  342. return 0;
  343. }
  344. priv->outstanding_urbs++;
  345. spin_unlock_irqrestore(&priv->lock, flags);
  346. buffer = kmalloc(count, GFP_ATOMIC);
  347. if (!buffer) {
  348. dev_err(&port->dev, "out of memory\n");
  349. count = -ENOMEM;
  350. goto error_no_buffer;
  351. }
  352. urb = usb_alloc_urb(0, GFP_ATOMIC);
  353. if (!urb) {
  354. dev_err(&port->dev, "no more free urbs\n");
  355. count = -ENOMEM;
  356. goto error_no_urb;
  357. }
  358. memcpy(buffer, buf, count);
  359. usb_serial_debug_data(debug, &port->dev, __func__, count, buffer);
  360. usb_fill_bulk_urb(urb, serial->dev,
  361. usb_sndbulkpipe(serial->dev,
  362. port->bulk_out_endpointAddress),
  363. buffer, count,
  364. visor_write_bulk_callback, port);
  365. /* send it down the pipe */
  366. status = usb_submit_urb(urb, GFP_ATOMIC);
  367. if (status) {
  368. dev_err(&port->dev,
  369. "%s - usb_submit_urb(write bulk) failed with status = %d\n",
  370. __func__, status);
  371. count = status;
  372. goto error;
  373. } else {
  374. spin_lock_irqsave(&priv->lock, flags);
  375. priv->bytes_out += count;
  376. spin_unlock_irqrestore(&priv->lock, flags);
  377. }
  378. /* we are done with this urb, so let the host driver
  379. * really free it when it is finished with it */
  380. usb_free_urb(urb);
  381. return count;
  382. error:
  383. usb_free_urb(urb);
  384. error_no_urb:
  385. kfree(buffer);
  386. error_no_buffer:
  387. spin_lock_irqsave(&priv->lock, flags);
  388. --priv->outstanding_urbs;
  389. spin_unlock_irqrestore(&priv->lock, flags);
  390. return count;
  391. }
  392. static int visor_write_room(struct tty_struct *tty)
  393. {
  394. struct usb_serial_port *port = tty->driver_data;
  395. struct visor_private *priv = usb_get_serial_port_data(port);
  396. unsigned long flags;
  397. dbg("%s - port %d", __func__, port->number);
  398. /*
  399. * We really can take anything the user throws at us
  400. * but let's pick a nice big number to tell the tty
  401. * layer that we have lots of free space, unless we don't.
  402. */
  403. spin_lock_irqsave(&priv->lock, flags);
  404. if (priv->outstanding_urbs > URB_UPPER_LIMIT * 2 / 3) {
  405. spin_unlock_irqrestore(&priv->lock, flags);
  406. dbg("%s - write limit hit\n", __func__);
  407. return 0;
  408. }
  409. spin_unlock_irqrestore(&priv->lock, flags);
  410. return 2048;
  411. }
  412. static void visor_write_bulk_callback(struct urb *urb)
  413. {
  414. struct usb_serial_port *port = urb->context;
  415. struct visor_private *priv = usb_get_serial_port_data(port);
  416. int status = urb->status;
  417. unsigned long flags;
  418. /* free up the transfer buffer, as usb_free_urb() does not do this */
  419. kfree(urb->transfer_buffer);
  420. dbg("%s - port %d", __func__, port->number);
  421. if (status)
  422. dbg("%s - nonzero write bulk status received: %d",
  423. __func__, status);
  424. spin_lock_irqsave(&priv->lock, flags);
  425. --priv->outstanding_urbs;
  426. spin_unlock_irqrestore(&priv->lock, flags);
  427. usb_serial_port_softint(port);
  428. }
  429. static void visor_read_bulk_callback(struct urb *urb)
  430. {
  431. struct usb_serial_port *port = urb->context;
  432. struct visor_private *priv = usb_get_serial_port_data(port);
  433. unsigned char *data = urb->transfer_buffer;
  434. int status = urb->status;
  435. struct tty_struct *tty;
  436. int result;
  437. int available_room = 0;
  438. dbg("%s - port %d", __func__, port->number);
  439. if (status) {
  440. dbg("%s - nonzero read bulk status received: %d",
  441. __func__, status);
  442. return;
  443. }
  444. usb_serial_debug_data(debug, &port->dev, __func__,
  445. urb->actual_length, data);
  446. if (urb->actual_length) {
  447. tty = tty_port_tty_get(&port->port);
  448. if (tty) {
  449. available_room = tty_buffer_request_room(tty,
  450. urb->actual_length);
  451. if (available_room) {
  452. tty_insert_flip_string(tty, data,
  453. available_room);
  454. tty_flip_buffer_push(tty);
  455. }
  456. tty_kref_put(tty);
  457. }
  458. spin_lock(&priv->lock);
  459. priv->bytes_in += available_room;
  460. } else {
  461. spin_lock(&priv->lock);
  462. }
  463. /* Continue trying to always read if we should */
  464. if (!priv->throttled) {
  465. usb_fill_bulk_urb(port->read_urb, port->serial->dev,
  466. usb_rcvbulkpipe(port->serial->dev,
  467. port->bulk_in_endpointAddress),
  468. port->read_urb->transfer_buffer,
  469. port->read_urb->transfer_buffer_length,
  470. visor_read_bulk_callback, port);
  471. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  472. if (result)
  473. dev_err(&port->dev,
  474. "%s - failed resubmitting read urb, error %d\n",
  475. __func__, result);
  476. } else
  477. priv->actually_throttled = 1;
  478. spin_unlock(&priv->lock);
  479. }
  480. static void visor_read_int_callback(struct urb *urb)
  481. {
  482. struct usb_serial_port *port = urb->context;
  483. int status = urb->status;
  484. int result;
  485. switch (status) {
  486. case 0:
  487. /* success */
  488. break;
  489. case -ECONNRESET:
  490. case -ENOENT:
  491. case -ESHUTDOWN:
  492. /* this urb is terminated, clean up */
  493. dbg("%s - urb shutting down with status: %d",
  494. __func__, status);
  495. return;
  496. default:
  497. dbg("%s - nonzero urb status received: %d",
  498. __func__, status);
  499. goto exit;
  500. }
  501. /*
  502. * This information is still unknown what it can be used for.
  503. * If anyone has an idea, please let the author know...
  504. *
  505. * Rumor has it this endpoint is used to notify when data
  506. * is ready to be read from the bulk ones.
  507. */
  508. usb_serial_debug_data(debug, &port->dev, __func__,
  509. urb->actual_length, urb->transfer_buffer);
  510. exit:
  511. result = usb_submit_urb(urb, GFP_ATOMIC);
  512. if (result)
  513. dev_err(&urb->dev->dev,
  514. "%s - Error %d submitting interrupt urb\n",
  515. __func__, result);
  516. }
  517. static void visor_throttle(struct tty_struct *tty)
  518. {
  519. struct usb_serial_port *port = tty->driver_data;
  520. struct visor_private *priv = usb_get_serial_port_data(port);
  521. unsigned long flags;
  522. dbg("%s - port %d", __func__, port->number);
  523. spin_lock_irqsave(&priv->lock, flags);
  524. priv->throttled = 1;
  525. spin_unlock_irqrestore(&priv->lock, flags);
  526. }
  527. static void visor_unthrottle(struct tty_struct *tty)
  528. {
  529. struct usb_serial_port *port = tty->driver_data;
  530. struct visor_private *priv = usb_get_serial_port_data(port);
  531. unsigned long flags;
  532. int result;
  533. dbg("%s - port %d", __func__, port->number);
  534. spin_lock_irqsave(&priv->lock, flags);
  535. priv->throttled = 0;
  536. priv->actually_throttled = 0;
  537. spin_unlock_irqrestore(&priv->lock, flags);
  538. port->read_urb->dev = port->serial->dev;
  539. result = usb_submit_urb(port->read_urb, GFP_ATOMIC);
  540. if (result)
  541. dev_err(&port->dev,
  542. "%s - failed submitting read urb, error %d\n",
  543. __func__, result);
  544. }
  545. static int palm_os_3_probe(struct usb_serial *serial,
  546. const struct usb_device_id *id)
  547. {
  548. struct device *dev = &serial->dev->dev;
  549. struct visor_connection_info *connection_info;
  550. unsigned char *transfer_buffer;
  551. char *string;
  552. int retval = 0;
  553. int i;
  554. int num_ports = 0;
  555. dbg("%s", __func__);
  556. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  557. if (!transfer_buffer) {
  558. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  559. sizeof(*connection_info));
  560. return -ENOMEM;
  561. }
  562. /* send a get connection info request */
  563. retval = usb_control_msg(serial->dev,
  564. usb_rcvctrlpipe(serial->dev, 0),
  565. VISOR_GET_CONNECTION_INFORMATION,
  566. 0xc2, 0x0000, 0x0000, transfer_buffer,
  567. sizeof(*connection_info), 300);
  568. if (retval < 0) {
  569. dev_err(dev, "%s - error %d getting connection information\n",
  570. __func__, retval);
  571. goto exit;
  572. }
  573. if (retval == sizeof(*connection_info)) {
  574. connection_info = (struct visor_connection_info *)
  575. transfer_buffer;
  576. num_ports = le16_to_cpu(connection_info->num_ports);
  577. for (i = 0; i < num_ports; ++i) {
  578. switch (
  579. connection_info->connections[i].port_function_id) {
  580. case VISOR_FUNCTION_GENERIC:
  581. string = "Generic";
  582. break;
  583. case VISOR_FUNCTION_DEBUGGER:
  584. string = "Debugger";
  585. break;
  586. case VISOR_FUNCTION_HOTSYNC:
  587. string = "HotSync";
  588. break;
  589. case VISOR_FUNCTION_CONSOLE:
  590. string = "Console";
  591. break;
  592. case VISOR_FUNCTION_REMOTE_FILE_SYS:
  593. string = "Remote File System";
  594. break;
  595. default:
  596. string = "unknown";
  597. break;
  598. }
  599. dev_info(dev, "%s: port %d, is for %s use\n",
  600. serial->type->description,
  601. connection_info->connections[i].port, string);
  602. }
  603. }
  604. /*
  605. * Handle devices that report invalid stuff here.
  606. */
  607. if (num_ports == 0 || num_ports > 2) {
  608. dev_warn(dev, "%s: No valid connect info available\n",
  609. serial->type->description);
  610. num_ports = 2;
  611. }
  612. dev_info(dev, "%s: Number of ports: %d\n", serial->type->description,
  613. num_ports);
  614. /*
  615. * save off our num_ports info so that we can use it in the
  616. * calc_num_ports callback
  617. */
  618. usb_set_serial_data(serial, (void *)(long)num_ports);
  619. /* ask for the number of bytes available, but ignore the
  620. response as it is broken */
  621. retval = usb_control_msg(serial->dev,
  622. usb_rcvctrlpipe(serial->dev, 0),
  623. VISOR_REQUEST_BYTES_AVAILABLE,
  624. 0xc2, 0x0000, 0x0005, transfer_buffer,
  625. 0x02, 300);
  626. if (retval < 0)
  627. dev_err(dev, "%s - error %d getting bytes available request\n",
  628. __func__, retval);
  629. retval = 0;
  630. exit:
  631. kfree(transfer_buffer);
  632. return retval;
  633. }
  634. static int palm_os_4_probe(struct usb_serial *serial,
  635. const struct usb_device_id *id)
  636. {
  637. struct device *dev = &serial->dev->dev;
  638. struct palm_ext_connection_info *connection_info;
  639. unsigned char *transfer_buffer;
  640. int retval;
  641. dbg("%s", __func__);
  642. transfer_buffer = kmalloc(sizeof(*connection_info), GFP_KERNEL);
  643. if (!transfer_buffer) {
  644. dev_err(dev, "%s - kmalloc(%Zd) failed.\n", __func__,
  645. sizeof(*connection_info));
  646. return -ENOMEM;
  647. }
  648. retval = usb_control_msg(serial->dev,
  649. usb_rcvctrlpipe(serial->dev, 0),
  650. PALM_GET_EXT_CONNECTION_INFORMATION,
  651. 0xc2, 0x0000, 0x0000, transfer_buffer,
  652. sizeof(*connection_info), 300);
  653. if (retval < 0)
  654. dev_err(dev, "%s - error %d getting connection info\n",
  655. __func__, retval);
  656. else
  657. usb_serial_debug_data(debug, &serial->dev->dev, __func__,
  658. retval, transfer_buffer);
  659. kfree(transfer_buffer);
  660. return 0;
  661. }
  662. static int visor_probe(struct usb_serial *serial,
  663. const struct usb_device_id *id)
  664. {
  665. int retval = 0;
  666. int (*startup)(struct usb_serial *serial,
  667. const struct usb_device_id *id);
  668. dbg("%s", __func__);
  669. if (serial->dev->actconfig->desc.bConfigurationValue != 1) {
  670. dev_err(&serial->dev->dev, "active config #%d != 1 ??\n",
  671. serial->dev->actconfig->desc.bConfigurationValue);
  672. return -ENODEV;
  673. }
  674. if (id->driver_info) {
  675. startup = (void *)id->driver_info;
  676. retval = startup(serial, id);
  677. }
  678. return retval;
  679. }
  680. static int visor_calc_num_ports(struct usb_serial *serial)
  681. {
  682. int num_ports = (int)(long)(usb_get_serial_data(serial));
  683. if (num_ports)
  684. usb_set_serial_data(serial, NULL);
  685. return num_ports;
  686. }
  687. static int generic_startup(struct usb_serial *serial)
  688. {
  689. struct usb_serial_port **ports = serial->port;
  690. struct visor_private *priv;
  691. int i;
  692. for (i = 0; i < serial->num_ports; ++i) {
  693. priv = kzalloc(sizeof(*priv), GFP_KERNEL);
  694. if (!priv) {
  695. while (i-- != 0) {
  696. priv = usb_get_serial_port_data(ports[i]);
  697. usb_set_serial_port_data(ports[i], NULL);
  698. kfree(priv);
  699. }
  700. return -ENOMEM;
  701. }
  702. spin_lock_init(&priv->lock);
  703. usb_set_serial_port_data(ports[i], priv);
  704. }
  705. return 0;
  706. }
  707. static int clie_3_5_startup(struct usb_serial *serial)
  708. {
  709. struct device *dev = &serial->dev->dev;
  710. int result;
  711. u8 data;
  712. dbg("%s", __func__);
  713. /*
  714. * Note that PEG-300 series devices expect the following two calls.
  715. */
  716. /* get the config number */
  717. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  718. USB_REQ_GET_CONFIGURATION, USB_DIR_IN,
  719. 0, 0, &data, 1, 3000);
  720. if (result < 0) {
  721. dev_err(dev, "%s: get config number failed: %d\n",
  722. __func__, result);
  723. return result;
  724. }
  725. if (result != 1) {
  726. dev_err(dev, "%s: get config number bad return length: %d\n",
  727. __func__, result);
  728. return -EIO;
  729. }
  730. /* get the interface number */
  731. result = usb_control_msg(serial->dev, usb_rcvctrlpipe(serial->dev, 0),
  732. USB_REQ_GET_INTERFACE,
  733. USB_DIR_IN | USB_RECIP_INTERFACE,
  734. 0, 0, &data, 1, 3000);
  735. if (result < 0) {
  736. dev_err(dev, "%s: get interface number failed: %d\n",
  737. __func__, result);
  738. return result;
  739. }
  740. if (result != 1) {
  741. dev_err(dev,
  742. "%s: get interface number bad return length: %d\n",
  743. __func__, result);
  744. return -EIO;
  745. }
  746. return generic_startup(serial);
  747. }
  748. static int treo_attach(struct usb_serial *serial)
  749. {
  750. struct usb_serial_port *swap_port;
  751. /* Only do this endpoint hack for the Handspring devices with
  752. * interrupt in endpoints, which for now are the Treo devices. */
  753. if (!((le16_to_cpu(serial->dev->descriptor.idVendor)
  754. == HANDSPRING_VENDOR_ID) ||
  755. (le16_to_cpu(serial->dev->descriptor.idVendor)
  756. == KYOCERA_VENDOR_ID)) ||
  757. (serial->num_interrupt_in == 0))
  758. goto generic_startup;
  759. dbg("%s", __func__);
  760. /*
  761. * It appears that Treos and Kyoceras want to use the
  762. * 1st bulk in endpoint to communicate with the 2nd bulk out endpoint,
  763. * so let's swap the 1st and 2nd bulk in and interrupt endpoints.
  764. * Note that swapping the bulk out endpoints would break lots of
  765. * apps that want to communicate on the second port.
  766. */
  767. #define COPY_PORT(dest, src) \
  768. do { \
  769. dest->read_urb = src->read_urb; \
  770. dest->bulk_in_endpointAddress = src->bulk_in_endpointAddress;\
  771. dest->bulk_in_buffer = src->bulk_in_buffer; \
  772. dest->interrupt_in_urb = src->interrupt_in_urb; \
  773. dest->interrupt_in_endpointAddress = \
  774. src->interrupt_in_endpointAddress;\
  775. dest->interrupt_in_buffer = src->interrupt_in_buffer; \
  776. } while (0);
  777. swap_port = kmalloc(sizeof(*swap_port), GFP_KERNEL);
  778. if (!swap_port)
  779. return -ENOMEM;
  780. COPY_PORT(swap_port, serial->port[0]);
  781. COPY_PORT(serial->port[0], serial->port[1]);
  782. COPY_PORT(serial->port[1], swap_port);
  783. kfree(swap_port);
  784. generic_startup:
  785. return generic_startup(serial);
  786. }
  787. static int clie_5_attach(struct usb_serial *serial)
  788. {
  789. dbg("%s", __func__);
  790. /* TH55 registers 2 ports.
  791. Communication in from the UX50/TH55 uses bulk_in_endpointAddress
  792. from port 0. Communication out to the UX50/TH55 uses
  793. bulk_out_endpointAddress from port 1
  794. Lets do a quick and dirty mapping
  795. */
  796. /* some sanity check */
  797. if (serial->num_ports < 2)
  798. return -1;
  799. /* port 0 now uses the modified endpoint Address */
  800. serial->port[0]->bulk_out_endpointAddress =
  801. serial->port[1]->bulk_out_endpointAddress;
  802. return generic_startup(serial);
  803. }
  804. static void visor_release(struct usb_serial *serial)
  805. {
  806. struct visor_private *priv;
  807. int i;
  808. dbg("%s", __func__);
  809. for (i = 0; i < serial->num_ports; i++) {
  810. priv = usb_get_serial_port_data(serial->port[i]);
  811. kfree(priv);
  812. }
  813. }
  814. static int __init visor_init(void)
  815. {
  816. int i, retval;
  817. /* Only if parameters were passed to us */
  818. if (vendor > 0 && product > 0) {
  819. struct usb_device_id usb_dev_temp[] = {
  820. {
  821. USB_DEVICE(vendor, product),
  822. .driver_info =
  823. (kernel_ulong_t) &palm_os_4_probe
  824. }
  825. };
  826. /* Find the last entry in id_table */
  827. for (i = 0;; i++) {
  828. if (id_table[i].idVendor == 0) {
  829. id_table[i] = usb_dev_temp[0];
  830. break;
  831. }
  832. }
  833. /* Find the last entry in id_table_combined */
  834. for (i = 0;; i++) {
  835. if (id_table_combined[i].idVendor == 0) {
  836. id_table_combined[i] = usb_dev_temp[0];
  837. break;
  838. }
  839. }
  840. printk(KERN_INFO KBUILD_MODNAME
  841. ": Untested USB device specified at time of module insertion\n");
  842. printk(KERN_INFO KBUILD_MODNAME
  843. ": Warning: This is not guaranteed to work\n");
  844. printk(KERN_INFO KBUILD_MODNAME
  845. ": Using a newer kernel is preferred to this method\n");
  846. printk(KERN_INFO KBUILD_MODNAME
  847. ": Adding Palm OS protocol 4.x support for unknown device: 0x%x/0x%x\n",
  848. vendor, product);
  849. }
  850. retval = usb_serial_register(&handspring_device);
  851. if (retval)
  852. goto failed_handspring_register;
  853. retval = usb_serial_register(&clie_3_5_device);
  854. if (retval)
  855. goto failed_clie_3_5_register;
  856. retval = usb_serial_register(&clie_5_device);
  857. if (retval)
  858. goto failed_clie_5_register;
  859. retval = usb_register(&visor_driver);
  860. if (retval)
  861. goto failed_usb_register;
  862. printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "\n");
  863. return 0;
  864. failed_usb_register:
  865. usb_serial_deregister(&clie_5_device);
  866. failed_clie_5_register:
  867. usb_serial_deregister(&clie_3_5_device);
  868. failed_clie_3_5_register:
  869. usb_serial_deregister(&handspring_device);
  870. failed_handspring_register:
  871. return retval;
  872. }
  873. static void __exit visor_exit (void)
  874. {
  875. usb_deregister(&visor_driver);
  876. usb_serial_deregister(&handspring_device);
  877. usb_serial_deregister(&clie_3_5_device);
  878. usb_serial_deregister(&clie_5_device);
  879. }
  880. module_init(visor_init);
  881. module_exit(visor_exit);
  882. MODULE_AUTHOR(DRIVER_AUTHOR);
  883. MODULE_DESCRIPTION(DRIVER_DESC);
  884. MODULE_LICENSE("GPL");
  885. module_param(debug, bool, S_IRUGO | S_IWUSR);
  886. MODULE_PARM_DESC(debug, "Debug enabled or not");
  887. module_param(stats, bool, S_IRUGO | S_IWUSR);
  888. MODULE_PARM_DESC(stats, "Enables statistics or not");
  889. module_param(vendor, ushort, 0);
  890. MODULE_PARM_DESC(vendor, "User specified vendor ID");
  891. module_param(product, ushort, 0);
  892. MODULE_PARM_DESC(product, "User specified product ID");